~ubuntu-branches/debian/wheezy/cb2bib/wheezy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/***************************************************************************
 *   Copyright (C) 2004-2012 by Pere Constans
 *   constans@molspaces.com
 *   cb2Bib version 1.4.8. Licensed under the GNU GPL version 3.
 *   See the LICENSE file that comes with this distribution.
 ***************************************************************************/
#include "compositePattern.h"

#include "cb2bib_utilities.h"


template <typename T> class descending
{
public:
    descending(const T& data) : _data(data) {}
    inline bool operator()(const int i, const int j)
    {
        return _data.at(i) > _data.at(j);
    }
private:
    const T& _data;
};


compositePattern::compositePattern() :
    _case_sensitivity(Qt::CaseSensitive),
    _is_multipattern(false),
    _subpattern_count(0),
    _matched_length(-1)
{
    _regexp.setCaseSensitivity(Qt::CaseSensitive); // Setting _regexp case sensitive for efficiency
    _regexp.setMinimal(false);
    _regexp.setPatternSyntax(QRegExp::RegExp2);
}

compositePattern::compositePattern(const QString& pattern, const Qt::CaseSensitivity cs) :
    _string(pattern.simplified()),
    _case_sensitivity(cs),
    _is_multipattern(false),
    _subpattern_count(0),
    _matched_length(-1)
{
    _regexp.setCaseSensitivity(Qt::CaseSensitive); // Setting _regexp case sensitive for efficiency
    _regexp.setMinimal(false);
    _regexp.setPatternSyntax(QRegExp::RegExp2);
}


QString compositePattern::escape(const QString& str, const Qt::CaseSensitivity cs) const
{
    if (cs == Qt::CaseSensitive)
        return QRegExp::escape(str);
    QString scaped;
    for (int i = 0; i < str.length(); ++i)
        if (str.at(i).isLetter())
            scaped += QString("[%1%2]").arg(str.at(i).toUpper()).arg(str.at(i).toLower());
        else
            scaped += QRegExp::escape(str.at(i));
    return scaped;
}

void compositePattern::set_sort_index()
{
    Q_ASSERT_X(_ranks.count() == _subpattern_count, "compositePattern", "Mismatch in subexpression initialization");
    _index.resize(_subpattern_count);
    for (int i = 0; i < _subpattern_count; ++i)
        _index[i] = i;
    descending< QList<double> > sort(_ranks);
    qSort(_index.begin(), _index.end(), sort);
}