"Fossies" - the Fresh Open Source Software Archive

Member "cb2bib-2.0.1/src/c2b/substringMatcher.h" (12 Feb 2021, 3504 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 "substringMatcher.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 SUBSTRINGMATCHER_H
    8 #define SUBSTRINGMATCHER_H
    9 
   10 #include "txtmatcher.h"
   11 
   12 #include <QVector>
   13 
   14 #define C2B_DEBUG_SUBSTRINGMATCHER 0
   15 
   16 
   17 class substringMatcher
   18 {
   19 
   20 public:
   21     substringMatcher();
   22     explicit substringMatcher(const QString& pattern, const Qt::CaseSensitivity cs = Qt::CaseSensitive);
   23     inline ~substringMatcher() {}
   24 
   25 
   26     int indexIn(const QString& str, const int from = 0) const;
   27     inline int matchedLength() const
   28     {
   29         return _matched_length;
   30     }
   31 
   32 
   33 private:
   34     inline int _index_in(const int s, const QString& str, const int p0) const
   35     {
   36         return _substrings.at(s).indexIn(str, p0);
   37     }
   38     inline int _index_in(const int s, const QString& str, const int p0, const int pn) const
   39     {
   40 #if C2B_DEBUG_SUBSTRINGMATCHER
   41         qDebug() << "Search for substring:" << _substrings.at(s).pattern() << "in interval" << p0 << pn;
   42         int i;
   43         if (p0 >= pn)
   44             i = -1;
   45         else
   46             i = _substrings.at(s).indexIn(str.unicode(), std::min(_pn, pn), p0);
   47         qDebug() << "substringMatcher: returned index:" << i;
   48         return i;
   49 #else
   50         return _substrings.at(s).indexIn(str.unicode(), std::min(_pn, pn), p0);
   51 #endif
   52     }
   53     inline int _p_back(const int s, const int p0) const
   54     {
   55         return std::max(_p0 + _acc_lengths.at(s), p0 - _stretches.at(s + 1) - _lengths.at(s));
   56     }
   57     inline int _p_forth(const int s, const int p0) const
   58     {
   59         return std::min(_pn, p0 + _stretches.at(s) + _lengths.at(s));
   60     }
   61     inline int _index_back(const int s, const QString& str, int p0, const int pn) const
   62     {
   63         while ((p0 = _index_in(s, str, p0, pn)) != -1)
   64         {
   65             if (s == 0)
   66                 return p0;
   67             const int i(_index_back(s - 1, str, _p_back(s - 1, p0), p0));
   68             if (i != -1)
   69                 return i;
   70             p0 += _lengths.at(s);
   71         }
   72         return -1;
   73     }
   74     inline int _index_forth(const int s, const QString& str, int p0, const int pn) const
   75     {
   76         while ((p0 = _index_in(s, str, p0, pn)) != -1)
   77         {
   78             p0 += _lengths.at(s);
   79             if (s + 1 == _substring_count)
   80                 return p0;
   81             const int i(_index_forth(s + 1, str, p0, _p_forth(s + 1, p0)));
   82             if (i != -1)
   83                 return i;
   84         }
   85         return -1;
   86     }
   87     inline int _index_around(const QString& str, const int phook) const
   88     {
   89         int bp(phook);
   90         if (_hook > 0)
   91         {
   92             bp = _index_back(_hook - 1, str, _p_back(_hook - 1, bp), bp);
   93             if (bp == -1)
   94                 return -1;
   95         }
   96         int fp(phook + _lengths.at(_hook));
   97         if (_hook + 1 < _substring_count)
   98         {
   99 
  100             fp = _index_forth(_hook + 1, str, fp, _p_forth(_hook + 1, fp));
  101             if (fp == -1)
  102                 return -1;
  103         }
  104         _matched_length = fp - bp;
  105         return bp;
  106     }
  107 
  108     QVector<txtmatcher> _substrings;
  109     QVector<int> _acc_lengths;
  110     QVector<int> _lengths;
  111     QVector<int> _stretches;
  112     int _hook;
  113     int _substring_count;
  114     mutable int _matched_length;
  115     mutable int _p0;
  116     mutable int _pn;
  117 };
  118 
  119 #endif