-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathSnippetIndex.cpp
214 lines (185 loc) · 5.46 KB
/
SnippetIndex.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include "SnippetIndex.h"
#include "StringUtils.h"
namespace uap_cpp {
namespace {
inline bool is_snippet_char(char c, bool prev_was_backslash) {
if (('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') ||
('0' <= c && c <= '9')) {
// Letters and digits are snippet characters, unless preceded by backslash
return !prev_was_backslash;
}
switch (c) {
case ' ':
case '_':
case '-':
case '/':
case ',':
case ';':
case '=':
case '%':
// These are always snippet characters
return true;
default:
// Other characters are snippet characters if preceded by backslash
return prev_was_backslash;
}
}
inline bool possibly_skip_block(const char*& s, const StringView& view) {
if (*s == '(' || *s == '[' || *s == '{') {
bool had_alternative_operators = false;
const char* closing_parenthesis =
get_closing_parenthesis(view.from(s), &had_alternative_operators);
if (closing_parenthesis) {
if (*s == '{' || had_alternative_operators ||
(*s == '(' &&
is_optional_operator(view.from(closing_parenthesis + 1)))) {
// Always skip count blocks {1,2}
// Skip block with alteratives (ab|ba) or [ab]
// Skip optional block (a)? (b)* (a){0,}
s = closing_parenthesis;
return true;
}
}
}
return false;
}
bool has_root_level_alternatives(const StringView& view) {
const char* s = view.start();
bool prev_was_backslash = false;
int level = 0;
while (!view.isEnd(s)) {
if (!prev_was_backslash) {
if (*s == '(') {
++level;
} else if (*s == ')') {
--level;
} else if (*s == '|' && level == 0) {
return true;
} else if (*s == '[') {
// Must ignore character-level alternatives [a(b|c]
const char* closing_parenthesis = get_closing_parenthesis(view.from(s));
if (closing_parenthesis) {
s = closing_parenthesis;
}
}
}
prev_was_backslash = *s == '\\' && !prev_was_backslash;
++s;
}
return false;
}
inline uint8_t to_byte(char c, bool to_lowercase = true) {
uint8_t b = c;
// TODO: Duplicate nodes instead of having case-insensitive expressions?
if (to_lowercase && ('A' <= c && c <= 'Z')) {
b |= 0x20;
}
return b;
}
} // namespace
SnippetIndex::SnippetSet SnippetIndex::registerSnippets(
const StringView& expression) {
SnippetSet out;
if (has_root_level_alternatives(expression)) {
// Skip whole expression if alternatives on root level a|b
return out;
}
const char* s = expression.start();
const char* snippet_start = nullptr;
TrieNode* node = nullptr;
bool prev_was_backslash = false;
while (!expression.isEnd(s)) {
if (is_snippet_char(*s, prev_was_backslash)) {
if (!node) {
snippet_start = s;
node = &trieRootNode_;
}
TrieNode*& next_node = node->transitions_[to_byte(*s)];
if (!next_node) {
next_node = new TrieNode;
next_node->parent_ = node;
}
node = next_node;
} else {
if (node) {
const char* snippet_end = s;
if (is_optional_operator(expression.from(snippet_end))) {
// Do not include optional characters a? a*
--snippet_end;
node = node->parent_;
}
registerSnippet(snippet_start, snippet_end, node, out);
snippet_start = nullptr;
node = nullptr;
}
}
if (!prev_was_backslash) {
possibly_skip_block(s, expression);
}
prev_was_backslash = *s == '\\' && !prev_was_backslash;
++s;
}
if (node) {
registerSnippet(snippet_start, s, node, out);
}
return out;
}
void SnippetIndex::registerSnippet(const char* start,
const char* end,
TrieNode* node,
SnippetIndex::SnippetSet& out) {
if (node && end - start > 2) {
if (!node->snippetId_) {
node->snippetId_ = ++maxSnippetId_;
}
out.insert(node->snippetId_);
}
}
SnippetIndex::TrieNode::~TrieNode() {
for (auto* node : transitions_) {
delete node;
}
}
SnippetIndex::SnippetSet SnippetIndex::getSnippets(
const StringView& text) const {
SnippetSet out;
const char* snippet_start = text.start();
while (!text.isEnd(snippet_start)) {
const char* snippet_end = snippet_start;
const TrieNode* node = &trieRootNode_;
while (node && !text.isEnd(snippet_end)) {
// Every character can be the start of a snippet (actually, only snippet
// characters, but unconditionally looking it up in the array is faster)
node = node->transitions_[to_byte(*snippet_end)];
if (node && node->snippetId_) {
out.insert(node->snippetId_);
}
++snippet_end;
}
++snippet_start;
}
return out;
}
namespace {
template <class TrieNode, class Map>
void build_map(const TrieNode& node, const std::string& base_string, Map& map) {
if (node.snippetId_) {
map.insert(std::make_pair(node.snippetId_, base_string));
}
for (int i = 0; i < 256; i++) {
auto* next_node = node.transitions_[i];
if (next_node) {
std::string next_string(base_string);
next_string += static_cast<char>(i);
build_map(*next_node, next_string, map);
}
}
}
} // namespace
std::unordered_map<SnippetIndex::SnippetId, std::string>
SnippetIndex::getRegisteredSnippets() const {
std::unordered_map<SnippetId, std::string> map;
build_map(trieRootNode_, "", map);
return map;
}
} // namespace uap_cpp