~ubuntu-branches/debian/sid/baloo-kf5/sid

« back to all changes in this revision

Viewing changes to src/queryparser/patternmatcher.h

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2014-07-10 21:13:07 UTC
  • Revision ID: package-import@ubuntu.com-20140710211307-iku0qs6vlplgn06m
Tags: upstream-5.0.0b
ImportĀ upstreamĀ versionĀ 5.0.0b

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of the Baloo query parser
 
2
   Copyright (c) 2013 Denis Steckelmacher <steckdenis@yahoo.fr>
 
3
 
 
4
   This library is free software; you can redistribute it and/or
 
5
   modify it under the terms of the GNU Library General Public
 
6
   License version 2.1 as published by the Free Software Foundation,
 
7
   or any later version.
 
8
 
 
9
   This library is distributed in the hope that it will be useful,
 
10
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
   Library General Public License for more details.
 
13
 
 
14
   You should have received a copy of the GNU Library General Public License
 
15
   along with this library; see the file COPYING.LIB.  If not, write to
 
16
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
17
   Boston, MA 02110-1301, USA.
 
18
*/
 
19
 
 
20
#ifndef __PATTERNMATCHER_H__
 
21
#define __PATTERNMATCHER_H__
 
22
 
 
23
#include "completionproposal.h"
 
24
#include "utils.h"
 
25
 
 
26
#include "term.h"
 
27
 
 
28
#include <QtCore/QStringList>
 
29
 
 
30
namespace Baloo { class QueryParser; }
 
31
 
 
32
class PatternMatcher
 
33
{
 
34
    public:
 
35
        PatternMatcher(Baloo::QueryParser *parser,
 
36
                       QList<Baloo::Term> &terms,
 
37
                       int cursor_position,
 
38
                       const QStringList &pattern,
 
39
                       Baloo::CompletionProposal::Type completion_type,
 
40
                       const KLocalizedString &completion_description);
 
41
 
 
42
        template<typename T>
 
43
        void runPass(const T &pass)
 
44
        {
 
45
            QList<Baloo::Term> matched_terms;
 
46
 
 
47
            for (int i=0; i<capture_count; ++i) {
 
48
                matched_terms.append(Baloo::Term());
 
49
            }
 
50
 
 
51
            // Try to start to match the pattern at every position in the term list
 
52
            for (int index=0; index<terms.count(); ++index) {
 
53
                int start_position;
 
54
                int end_position;
 
55
                int matched_length = matchPattern(index, matched_terms, start_position, end_position);
 
56
 
 
57
                if (matched_length > 0) {
 
58
                    // The pattern matched, run the pass on the matching terms
 
59
                    QList<Baloo::Term> replacement = pass.run(matched_terms);
 
60
 
 
61
                    if (replacement.count() > 0) {
 
62
                        // Replace terms first_match_index..i with replacement
 
63
                        for (int i=0; i<matched_length; ++i) {
 
64
                            terms.removeAt(index);
 
65
                        }
 
66
 
 
67
                        for (int i=replacement.count()-1; i>=0; --i) {
 
68
                            terms.insert(index, replacement.at(i));
 
69
                        }
 
70
 
 
71
                        // If the pass returned only one replacement term, set
 
72
                        // its position. If more terms are returned, the pass
 
73
                        // must handle positions itself
 
74
                        if (replacement.count() == 1) {
 
75
                            setTermRange(terms[index], start_position, end_position);
 
76
                        }
 
77
 
 
78
                        // Re-explore the terms vector as indexes have changed
 
79
                        index = -1;
 
80
                    }
 
81
 
 
82
                    // If the pattern contains "%%", terms are appended to the end
 
83
                    // of matched_terms. Remove them now that they are not needed anymore
 
84
                    while (matched_terms.count() > capture_count) {
 
85
                        matched_terms.removeLast();
 
86
                    }
 
87
                }
 
88
            }
 
89
        }
 
90
 
 
91
    private:
 
92
        int captureCount() const;
 
93
        int matchPattern(int first_term_index,
 
94
                         QList<Baloo::Term> &matched_terms,
 
95
                         int &start_position,
 
96
                         int &end_position) const;
 
97
        bool matchTerm(const Baloo::Term &term, const QString &pattern, int &capture_index) const;
 
98
        void addCompletionProposal(int first_pattern_index_not_matching,
 
99
                                   int first_term_index_matching,
 
100
                                   int first_term_index_not_matching) const;
 
101
 
 
102
    private:
 
103
        Baloo::QueryParser *parser;
 
104
        QList<Baloo::Term> &terms;
 
105
        int cursor_position;
 
106
        QStringList pattern;
 
107
        Baloo::CompletionProposal::Type completion_type;
 
108
        KLocalizedString completion_description;
 
109
 
 
110
        int capture_count;
 
111
};
 
112
 
 
113
#endif
 
 
b'\\ No newline at end of file'