"Fossies" - the Fresh Open Source Software Archive

Member "cb2bib-2.0.1/src/c2b/searchPattern.h" (12 Feb 2021, 6292 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 "searchPattern.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 SEARCHPATTERN_H
    8 #define SEARCHPATTERN_H
    9 
   10 #include "approximatePattern.h"
   11 #include "documentContents.h"
   12 #include "scorer.h"
   13 #include "stemMatcher.h"
   14 #include "wordMatcher.h"
   15 #include "wordPattern.h"
   16 
   17 #include <QRegularExpression>
   18 
   19 
   20 class searchPattern
   21 {
   22 
   23 public:
   24     searchPattern(const QString& pattern, const QString& patternType);
   25     searchPattern(const bool NOT, const bool caseSensitive, const QString& patternType, const QString& scope,
   26                   const QChar& yearScope, const QString& pattern);
   27     inline ~searchPattern() {}
   28 
   29     enum Type
   30     {
   31         ApproximateString = 1,
   32         Context = 2,
   33         FixedStringAllWords = 3,
   34         FixedStringAnyWord = 4,
   35         FixedStringContext = 5,
   36         FixedString = 6,
   37         RegularExpression = 7,
   38         Wildcard = 8
   39     };
   40     static const QString type(const Type t);
   41     static const QStringList types();
   42 
   43     struct modifiers
   44     {
   45         QChar yearScope;
   46         QString scope;
   47         QString string;
   48         bool NOT;
   49     };
   50 
   51     inline const modifiers& modifier() const
   52     {
   53         return _modifier;
   54     }
   55     inline bool matches(const QString& contents) const
   56     {
   57         return _matcher.match(contents);
   58     }
   59     inline bool matches(const documentContents& contents) const
   60     {
   61         return _matcher.match(contents);
   62     }
   63     inline int indexIn(const QString& contents, const int from) const
   64     {
   65         return _matcher.index(contents, from);
   66     }
   67     inline int indexIn(const documentContents& contents, const int from) const
   68     {
   69         return _matcher.index(contents.text(), from);
   70     }
   71     inline int matchedLength() const
   72     {
   73         return _matcher.length;
   74     }
   75     inline void initializeScores() const
   76     {
   77         _scorer.initialize();
   78     }
   79     inline void updateScore() const
   80     {
   81         _scorer.update(_matcher.plength, _matcher.length);
   82     }
   83     inline int matchedCounter() const
   84     {
   85         return _scorer.nscores;
   86     }
   87     inline double matchedScore() const
   88     {
   89         return _scorer.score();
   90     }
   91     inline const QString toString() const
   92     {
   93         return _formatted_string;
   94     }
   95     inline bool operator<(const searchPattern& p) const
   96     {
   97         return (_rank < p._rank);
   98     }
   99 
  100 
  101 private:
  102     struct matcher
  103     {
  104         matcher(const QString& pattern, const Type t, const Qt::CaseSensitivity cs);
  105 
  106         QRegularExpression regexp;
  107         QString signature;
  108         QString typedpattern;
  109         QStringMatcher strexp;
  110         QVector<QString> subsignatures;
  111         Qt::CaseSensitivity csensitivity;
  112         Type type;
  113         approximatePattern appexp;
  114         int plength;
  115         mutable QRegularExpressionMatch regexp_match;
  116         mutable int length;
  117         stemMatcher cstemexp;
  118         wordMatcher cwordexp;
  119         wordPattern wordexp;
  120 
  121         bool match(const QString& contents) const;
  122         bool match(const documentContents& contents) const;
  123         int index(const QString& contents, const int from) const;
  124 
  125         static QString _wildcard_escaped(const QString& pattern);
  126         void _set_typed_pattern(const QString& pattern);
  127 
  128         static inline bool _match_signature(const QString& ps, const QString& cs)
  129         {
  130             const int pl(ps.length()); // Always pl > 0
  131             const int cl(cs.length());
  132             const ushort* ups(reinterpret_cast<const ushort*>(ps.unicode()));
  133             const ushort* p(ups);
  134             const ushort* pn(ups + pl);
  135             const ushort* ucs(reinterpret_cast<const ushort*>(cs.unicode()));
  136             const ushort* c(ucs - 1);
  137             const ushort* cn(ucs + cl);
  138             while (++c != cn)
  139                 if (*c == *p)
  140                     if (++p == pn)
  141                         return true;
  142             return false;
  143         }
  144         static inline bool _match(const QRegularExpression& submatcher, const QString& psignature,
  145                                   const documentContents& contents)
  146         {
  147 #ifdef C2B_DEBUG_SEARCHING_SKIP_SIGNATURE
  148             return submatcher.match(contents.text()).hasMatch();
  149 #else
  150             // Skip signature check for patterns that do not define it
  151             if (psignature.length() == 0 || _match_signature(psignature, contents.signature()))
  152                 return submatcher.match(contents.text()).hasMatch();
  153             else
  154                 return false;
  155 #endif
  156         }
  157         template <typename T>
  158         static inline bool _match(const T& submatcher, const QString& psignature, const documentContents& contents)
  159         {
  160 #ifdef C2B_DEBUG_SEARCHING_SKIP_SIGNATURE
  161             return submatcher.indexIn(contents.text()) != -1;
  162 #else
  163             // Skip signature check for patterns that do not define it
  164             if (psignature.length() == 0 || _match_signature(psignature, contents.signature()))
  165                 return submatcher.indexIn(contents.text()) != -1;
  166             else
  167                 return false;
  168 #endif
  169         }
  170         template <typename T>
  171         inline bool _match_all(const int n, const T& submatchers, const documentContents& contents) const
  172         {
  173             for (int i = n - 1; i >= 0; --i)
  174                 if (!_match(submatchers.at(i), subsignatures.at(i), contents))
  175                     return false;
  176             return true;
  177         }
  178         template <typename T>
  179         inline bool _match_any(const int n, const T& submatchers, const documentContents& contents) const
  180         {
  181             for (int i = 0; i < n; ++i)
  182                 if (_match(submatchers.at(i), subsignatures.at(i), contents))
  183                     return true;
  184             return false;
  185         }
  186     };
  187 
  188     static const QMap<QString, searchPattern::Type> typemap();
  189     static inline Qt::CaseSensitivity qtcase(bool caseSensitive)
  190     {
  191         return caseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive;
  192     }
  193 
  194     QString _formatted_string;
  195     int _rank;
  196     matcher _matcher;
  197     modifiers _modifier;
  198     mutable scorer _scorer;
  199 };
  200 
  201 #endif