"Fossies" - the Fresh Open Source Software Archive

Member "cb2bib-2.0.1/src/c2b/wordMatcher.h" (12 Feb 2021, 2499 Bytes) of package /linux/privat/cb2bib-2.0.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "wordMatcher.h" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.0.0_vs_2.0.1.

    1 /***************************************************************************
    2  *   Copyright (C) 2004-2021 by Pere Constans
    3  *   constans@molspaces.com
    4  *   cb2Bib version 2.0.1. Licensed under the GNU GPL version 3.
    5  *   See the LICENSE file that comes with this distribution.
    6  ***************************************************************************/
    7 #ifndef WORDMATCHER_H
    8 #define WORDMATCHER_H
    9 
   10 #include "txtmatcher.h"
   11 
   12 #include <QStringList>
   13 #include <QVector>
   14 
   15 
   16 class wordMatcher
   17 {
   18 
   19 public:
   20     wordMatcher();
   21     explicit wordMatcher(const QString& pattern, const Qt::CaseSensitivity cs = Qt::CaseSensitive);
   22     inline ~wordMatcher() {}
   23 
   24 
   25     int indexIn(const QString& text, const int from = 0) const;
   26     void setPattern(const QString& pattern, const Qt::CaseSensitivity cs);
   27     inline int matchedLength() const
   28     {
   29         return _matched_length;
   30     }
   31     inline const QStringList& subpatternStrings() const
   32     {
   33         return _subpatterns;
   34     }
   35 
   36 
   37 private:
   38     inline int _index_in(const int s, const QString& text, const int p0) const
   39     {
   40         return _substrings.at(s).indexIn(text, p0);
   41     }
   42     inline int _index_in(const int s, const QString& text, const int p0, const int pn) const
   43     {
   44         return _substrings.at(s).indexIn(text.unicode(), std::min(_pn, pn), p0);
   45     }
   46     inline int _index_around(const QString& text, const int phook) const
   47     {
   48         for (int i = 0; i < _substring_count; ++i)
   49             _sp0[i] = -1;
   50         _sp0[_hook] = phook;
   51 
   52         const int bp0 = std::max(_p0, phook - _stretch);
   53         const int bpn = std::min(_pn, phook + _stretch);
   54         for (int i = 0; i < _substring_count; ++i)
   55             if (_sp0.at(i) == -1)
   56             {
   57                 const int p0(_index_in(i, text, bp0, bpn));
   58                 if (p0 == -1)
   59                     return -1;
   60                 _sp0[i] = p0;
   61             }
   62 
   63         int bp(_pn);
   64         int fp(_p0);
   65         for (int i = 0; i < _substring_count; ++i)
   66         {
   67             const int p0(_sp0.at(i));
   68             if (bp > p0)
   69                 bp = p0;
   70             const int pn(p0 + _lengths.at(i));
   71             if (fp < pn)
   72                 fp = pn;
   73         }
   74         _matched_length = fp - bp;
   75         return bp;
   76     }
   77 
   78     QStringList _subpatterns;
   79     QVector<int> _lengths;
   80     QVector<txtmatcher> _substrings;
   81     int _hook;
   82     int _stretch;
   83     int _substring_count;
   84     mutable QVector<int> _sp0;
   85     mutable int _matched_length;
   86     mutable int _p0;
   87     mutable int _pn;
   88 };
   89 
   90 #endif