"Fossies" - the Fresh Open Source Software Archive

Member "cb2bib-2.0.1/src/c2b/compositePattern.cpp" (12 Feb 2021, 1868 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 "compositePattern.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 "compositePattern.h"
    8 
    9 #include "cb2bib_utilities.h"
   10 
   11 namespace
   12 {
   13 
   14 template <typename T>
   15 class descending
   16 {
   17 public:
   18     explicit descending(const T& data) : _data(data) {}
   19     inline bool operator()(const int i, const int j)
   20     {
   21         return _data.at(i) > _data.at(j);
   22     }
   23 
   24 private:
   25     const T& _data;
   26 };
   27 
   28 } // namespace
   29 
   30 
   31 compositePattern::compositePattern()
   32     : _case_sensitivity(Qt::CaseSensitive), _is_multipattern(false), _subpattern_count(0), _matched_length(-1) {}
   33 
   34 compositePattern::compositePattern(const QString& pattern, const Qt::CaseSensitivity cs)
   35     : _string(pattern), _case_sensitivity(cs), _is_multipattern(false), _subpattern_count(0), _matched_length(-1) {}
   36 
   37 
   38 QString compositePattern::escape(const QString& str, const Qt::CaseSensitivity cs)
   39 {
   40     if (cs == Qt::CaseSensitive)
   41         return QRegularExpression::escape(str);
   42     QString scaped;
   43     for (int i = 0; i < str.length(); ++i)
   44         if (str.at(i).isLetter())
   45             scaped += QString("[%1%2]").arg(str.at(i).toUpper()).arg(str.at(i).toLower());
   46         else
   47             scaped += QRegularExpression::escape(str.at(i));
   48     return scaped;
   49 }
   50 
   51 void compositePattern::set_sort_index()
   52 {
   53     Q_ASSERT_X(_ranks.count() == _subpattern_count, "compositePattern", "Mismatch in subexpression initialization");
   54     _index.resize(_subpattern_count);
   55     for (int i = 0; i < _subpattern_count; ++i)
   56         _index[i] = i;
   57     descending<QList<double>> sort(_ranks);
   58     std::sort(_index.begin(), _index.end(), sort);
   59 }