-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
skipparser.cpp
177 lines (164 loc) · 5.54 KB
/
skipparser.cpp
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* SPDX-FileCopyrightText: 2020 Andreas Cord-Landwehr <[email protected]>
*
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
#include "skipparser.h"
#include <QDebug>
#include <functional>
#include <optional>
#include <set>
// for performance reasons, we need to have to lists
// but they must be kept in sync
const QRegularExpression SkipParser::sSkipCharDetection("[ |\\\n|\\\t|/|\\-|\\*|#]");
constexpr bool isSkipChar(const QChar &character)
{
switch (character.toLatin1()) {
case ' ':
return true;
case '\n':
return true;
case '\t':
return true;
case '/':
return true;
case '-':
return true;
case '*':
return true;
case '#':
return true;
default:
return false;
}
return false;
}
std::optional<std::pair<int, int>> SkipParser::findMatchNaive(QString text, QString pattern) const
{
if (pattern.length() > text.length()) {
return {};
}
int start = 0;
while (start <= text.length() - pattern.length()) {
bool matchFound {true};
int patternSkipOffset {0};
int textSkipOffset {0};
for (int i = 0; i < pattern.length(); ++i) {
// skip pattern char if contained in skip-list
// outcondition: valid char that can be matched
if (isSkipChar(pattern.at(i))) {
++patternSkipOffset;
continue;
}
while (isSkipChar(text.at(start + textSkipOffset + i - patternSkipOffset))) {
if (start + textSkipOffset + i - patternSkipOffset + 1 >= text.length() - 1) {
matchFound = false; // we know this, because otherwise we would not be in this loop
break;
}
++textSkipOffset;
}
if (text.at(start + textSkipOffset + i - patternSkipOffset) != pattern.at(i)) {
matchFound = false;
break;
}
}
if (matchFound) {
return std::optional<std::pair<int, int>> {{start, start + textSkipOffset + pattern.length() - patternSkipOffset - 1}};
} else {
++start;
}
}
return {};
}
std::vector<int> SkipParser::computeKmpPrefix(const QString &pattern) const
{
const int length = pattern.length();
std::vector<int> prefix(length);
prefix[0] = 0;
int k = 0;
for (int q = 2; q <= length; ++q) {
while (k > 0 && pattern.at(k) != pattern.at(q - 1)) {
k = prefix.at(k - 1);
}
if (pattern.at(k) == pattern.at(q - 1)) {
k = k + 1;
}
prefix[q - 1] = k;
}
return prefix;
}
std::optional<std::pair<int, int>> SkipParser::findMatchKMP(std::vector<QChar> prunedText, std::vector<int> textSkipPrefix, QString pattern) const
{
// obtain prefix
std::vector<int> prefix;
if (mPrefixCache.contains(pattern)) {
prefix = mPrefixCache[pattern];
} else {
prefix = computeKmpPrefix(pattern);
mPrefixCache.insert(pattern, prefix);
}
// KMP Matcher
const int textLength = prunedText.size();
int q = 0;
for (int i = 1; i <= textLength; ++i) {
while (q > 0 && pattern.at(q) != prunedText.at(i - 1)) {
q = prefix.at(q - 1);
}
if (pattern.at(q) == prunedText.at(i - 1)) {
q = q + 1;
}
if (q == pattern.length()) {
return std::optional<std::pair<int, int>> {{i - q + textSkipPrefix.at(i - q), i - 1 + textSkipPrefix.at(i - 1)}};
}
}
return {};
}
std::optional<std::pair<int, int>> SkipParser::findMatch(QString text, QString pattern) const
{
auto textPreprocessing = computeTextSkipPrefix(text);
auto match = findMatchKMP(textPreprocessing.first, textPreprocessing.second, pattern.remove(sSkipCharDetection));
qDebug() << "text :" << text;
qDebug() << "pattern:" << pattern;
// qDebug() << "start: " << start;
// qDebug() << "skip: " << textSkipOffset << " / " << patternSkipOffset;
return match;
}
std::pair<std::vector<QChar>, std::vector<int>> SkipParser::computeTextSkipPrefix(const QString &text) const
{
// prune text and compute skip prefix
// skip prefix notes number of skipped chars for each position in "text"
std::vector<int> textSkipPrefix(text.size());
std::vector<QChar> prunedText;
prunedText.reserve(text.size());
int skipped = 0;
for (int i = 0; i < text.size(); ++i) {
if (isSkipChar(text.at(i))) {
textSkipPrefix.at(i - skipped) = skipped;
++skipped;
} else {
prunedText.push_back(text.at(i));
textSkipPrefix.at(i - skipped) = skipped;
}
}
prunedText.shrink_to_fit();
return {prunedText, textSkipPrefix};
}
std::optional<std::pair<int, int>> SkipParser::findMatch(QString text, QVector<QString> patterns) const
{
// KMP can work with pruned patterns
QSet<QString> prunedPatterns;
// prune all skip chars from pattern
for (const auto &pattern : patterns) {
QString tmpPattern = pattern;
tmpPattern.remove(sSkipCharDetection);
prunedPatterns.insert(tmpPattern);
}
// qDebug() << "Pruned canonical texts:" << (patterns.count() - prunedPatterns.count());
auto textPreprocessing = computeTextSkipPrefix(text);
for (const auto &pattern : prunedPatterns) {
if (auto match = findMatchKMP(textPreprocessing.first, textPreprocessing.second, pattern)) {
return match;
}
}
return {};
}