wordMatcher.h (cb2bib-2.0.0) | : | wordMatcher.h (cb2bib-2.0.1) | ||
---|---|---|---|---|
/*************************************************************************** | /*************************************************************************** | |||
* Copyright (C) 2004-2019 by Pere Constans | * Copyright (C) 2004-2021 by Pere Constans | |||
* constans@molspaces.com | * constans@molspaces.com | |||
* cb2Bib version 2.0.0. Licensed under the GNU GPL version 3. | * cb2Bib version 2.0.1. Licensed under the GNU GPL version 3. | |||
* See the LICENSE file that comes with this distribution. | * See the LICENSE file that comes with this distribution. | |||
***************************************************************************/ | ***************************************************************************/ | |||
#ifndef WORDMATCHER_H | #ifndef WORDMATCHER_H | |||
#define WORDMATCHER_H | #define WORDMATCHER_H | |||
#include <QStringMatcher> | #include "txtmatcher.h" | |||
#include <QStringList> | ||||
#include <QVector> | #include <QVector> | |||
class wordMatcher | class wordMatcher | |||
{ | { | |||
public: | public: | |||
wordMatcher(); | wordMatcher(); | |||
wordMatcher(const QString& pattern, const Qt::CaseSensitivity cs = Qt::CaseS ensitive); | explicit wordMatcher(const QString& pattern, const Qt::CaseSensitivity cs = Qt::CaseSensitive); | |||
inline ~wordMatcher() {} | inline ~wordMatcher() {} | |||
int indexIn(const QString& str, const int from = 0) const; | int indexIn(const QString& text, const int from = 0) const; | |||
void setPattern(const QString& pattern, const Qt::CaseSensitivity cs); | void setPattern(const QString& pattern, const Qt::CaseSensitivity cs); | |||
inline int matchedLength() const | inline int matchedLength() const | |||
{ | { | |||
return _matched_length; | return _matched_length; | |||
} | } | |||
inline const QStringList& subpatternStrings() const | ||||
{ | ||||
return _subpatterns; | ||||
} | ||||
private: | private: | |||
inline int _index_in(const int& s, const QString& str, const int& p0) const | inline int _index_in(const int s, const QString& text, const int p0) const | |||
{ | { | |||
return _substrings.at(s).indexIn(str, p0); | return _substrings.at(s).indexIn(text, p0); | |||
} | } | |||
inline int _index_in(const int& s, const QString& str, const int& p0, const int& pn) const | inline int _index_in(const int s, const QString& text, const int p0, const i nt pn) const | |||
{ | { | |||
if (p0 >= pn) | return _substrings.at(s).indexIn(text.unicode(), std::min(_pn, pn), p0); | |||
return -1; | ||||
return _substrings.at(s).indexIn(str.unicode(), std::min(_pn, pn), p0); | ||||
} | } | |||
inline int _index_around(const QString& str, const int phook) const | inline int _index_around(const QString& text, const int phook) const | |||
{ | { | |||
_sp0.fill(-1); | for (int i = 0; i < _substring_count; ++i) | |||
_sp0[i] = -1; | ||||
_sp0[_hook] = phook; | _sp0[_hook] = phook; | |||
const int bp0 = std::max(_p0, phook - _stretch); | const int bp0 = std::max(_p0, phook - _stretch); | |||
const int bpn = std::min(_pn, phook + _stretch); | const int bpn = std::min(_pn, phook + _stretch); | |||
for (int i = 0; i < _substring_count; ++i) | for (int i = 0; i < _substring_count; ++i) | |||
if (_sp0.at(i) == -1) | if (_sp0.at(i) == -1) | |||
{ | { | |||
const int p0(_index_in(i, str, bp0, bpn)); | const int p0(_index_in(i, text, bp0, bpn)); | |||
if (p0 == -1) | if (p0 == -1) | |||
return -1; | return -1; | |||
_sp0[i] = p0; | _sp0[i] = p0; | |||
} | } | |||
int bp(_pn); | int bp(_pn); | |||
int fp(_p0); | int fp(_p0); | |||
for (int i = 0; i < _substring_count; ++i) | for (int i = 0; i < _substring_count; ++i) | |||
{ | { | |||
const int p0 = _sp0.at(i); | const int p0(_sp0.at(i)); | |||
if (bp > p0) | if (bp > p0) | |||
bp = p0; | bp = p0; | |||
const int pn = p0 + _lengths.at(i); | const int pn(p0 + _lengths.at(i)); | |||
if (fp < pn) | if (fp < pn) | |||
fp = pn; | fp = pn; | |||
} | } | |||
_matched_length = fp - bp; | _matched_length = fp - bp; | |||
return bp; | return bp; | |||
} | } | |||
QVector<QStringMatcher> _substrings; | QStringList _subpatterns; | |||
QVector<int> _lengths; | QVector<int> _lengths; | |||
int _stretch; | QVector<txtmatcher> _substrings; | |||
int _hook; | int _hook; | |||
int _stretch; | ||||
int _substring_count; | int _substring_count; | |||
mutable QVector<int> _sp0; | mutable QVector<int> _sp0; | |||
mutable int _matched_length; | mutable int _matched_length; | |||
mutable int _p0; | mutable int _p0; | |||
mutable int _pn; | mutable int _pn; | |||
}; | }; | |||
#endif | #endif | |||
End of changes. 18 change blocks. | ||||
18 lines changed or deleted | 24 lines changed or added |