"Fossies" - the Fresh Open Source Software Archive

Member "cb2bib-2.0.1/src/c2b/substringMatcher.cpp" (12 Feb 2021, 3037 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.cpp" 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 #include "substringMatcher.h"
    8 
    9 #include "cb2bib_utilities.h"
   10 #include "triads.h"
   11 
   12 
   13 substringMatcher::substringMatcher() : _hook(-1), _substring_count(0), _matched_length(-1), _p0(0), _pn(0) {}
   14 
   15 substringMatcher::substringMatcher(const QString& pattern, const Qt::CaseSensitivity cs)
   16     : _hook(-1), _matched_length(-1), _p0(0), _pn(0)
   17 {
   18     QRegExp sep("\\.\\{(\\d+),(\\d+)\\}");
   19     QStringList substrings(pattern.split(sep, QString::SkipEmptyParts));
   20     Q_ASSERT_X(substrings.count() >= 2, "substringMatcher", "Expected at least two substrings in pattern");
   21     int s(0);
   22     QList<int> stretches;
   23     while ((s = sep.indexIn(pattern, s)) != -1)
   24     {
   25         Q_ASSERT_X(sep.captureCount() == 2, "substringMatcher", "Expected two digits in repetition");
   26         Q_ASSERT_X(sep.cap(1).toInt() == 0, "substringMatcher", "Expected zero in first repetition digit");
   27         stretches.append(sep.cap(2).toInt());
   28         s += sep.matchedLength();
   29     }
   30     Q_ASSERT_X(substrings.count() == stretches.count() + 1, "substringMatcher", "Mismatch in pattern");
   31 
   32     _substring_count = substrings.count();
   33     _substrings.resize(_substring_count);
   34     _lengths.resize(_substring_count);
   35     for (int i = 0; i < _substring_count; ++i)
   36     {
   37         _substrings[i] = txtmatcher(substrings.at(i), cs);
   38         _lengths[i] = substrings.at(i).length();
   39     }
   40     _stretches.resize(_substring_count);
   41     _stretches[0] = 0;
   42     for (int i = 1; i < _substring_count; ++i)
   43         _stretches[i] = stretches.at(i - 1);
   44     _acc_lengths.fill(0, _substring_count);
   45     for (int i = 1; i < _substring_count; ++i)
   46         _acc_lengths[i] = _acc_lengths[i - 1] + _lengths.at(i - 1);
   47 
   48     int lf(10000);
   49     for (int i = 0; i < _substring_count; ++i)
   50     {
   51         const int f(triads::textFrequency(substrings.at(i)));
   52         if (f < lf)
   53         {
   54             lf = f;
   55             _hook = i;
   56         }
   57     }
   58 #if C2B_DEBUG_SUBSTRINGMATCHER
   59     qDebug() << "substringMatcher: Pattern: " << pattern;
   60     qDebug() << "substringMatcher: Hook:    " << substrings.at(_hook);
   61 #endif
   62 }
   63 
   64 
   65 int substringMatcher::indexIn(const QString& str, const int from) const
   66 {
   67     _matched_length = -1;
   68     if (_hook == -1) // Uninitialized
   69         return -1;
   70     _p0 = from;
   71     if (_p0 < 0)
   72         _p0 = 0;
   73     _pn = str.length();
   74     if (_pn == 0)
   75         return -1;
   76 
   77     int hp(_p0);
   78     int p(-1);
   79     while (p == -1)
   80     {
   81         hp = _index_in(_hook, str, hp + _acc_lengths.at(_hook));
   82 #if C2B_DEBUG_SUBSTRINGMATCHER
   83         qDebug() << "substringMatcher: Hook at  " << hp;
   84 #endif
   85         if (hp == -1)
   86             return -1;
   87         p = _index_around(str, hp);
   88         hp += _lengths.at(_hook);
   89     }
   90     return p;
   91 }