-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathAlternativeExpander.cpp
179 lines (151 loc) · 4.79 KB
/
AlternativeExpander.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
#include "AlternativeExpander.h"
#include "StringUtils.h"
namespace uap_cpp {
namespace {
bool is_negative_lookahead_operator(const StringView& view) {
const char* s = view.start();
if (view.isEnd(s) || view.isEnd(s + 1)) {
return false;
}
return s[0] == '?' && s[1] == '!';
}
bool is_non_capture_operator(const StringView& view) {
const char* s = view.start();
if (view.isEnd(s) || view.isEnd(s + 1)) {
return false;
}
return s[0] == '?' && s[1] == ':';
}
typedef std::vector<StringView> Stack;
void expand(const StringView&,
std::string prefix,
Stack next_stack,
std::vector<std::string>&);
bool expand_root_level_alternatives(const StringView& view,
std::string& prefix,
Stack& next,
std::vector<std::string>& out) {
const char* s = view.start();
int level = 0;
bool prev_was_backslash = false;
while (!view.isEnd(s)) {
if (!prev_was_backslash) {
if (*s == '(') {
++level;
} else if (*s == ')') {
if (level > 0) {
--level;
}
} else if (*s == '[') {
const char* closing_parenthesis = get_closing_parenthesis(view.from(s));
if (closing_parenthesis) {
// Skip character-level alternative block
s = closing_parenthesis;
continue;
}
}
if (level == 0 && *s == '|') {
// Go through alternative on the left
expand(view.to(s), prefix, next, out);
// Go through alternative(s) on the right
expand(view.from(s + 1), std::move(prefix), std::move(next), out);
return true;
}
}
prev_was_backslash = *s == '\\' && !prev_was_backslash;
++s;
}
return false;
}
bool expand_root_level_parentheses(const StringView& view,
std::string& prefix,
Stack& next,
std::vector<std::string>& out) {
const char* s = view.start();
int level = 0;
bool prev_was_backslash = false;
while (!view.isEnd(s)) {
if (!prev_was_backslash) {
if (*s == '(') {
++level;
if (level == 1) {
const char* closing_parenthesis =
get_closing_parenthesis(view.from(s));
if (!closing_parenthesis) {
// Bad expression
--level;
++s;
continue;
}
if (is_optional_operator(view.from(closing_parenthesis + 1)) ||
is_negative_lookahead_operator(view.from(s + 1))) {
// Continue after parentheses
s = closing_parenthesis;
continue;
}
// Add ( or (?: to prefix
const char* inner_start = s + 1;
if (is_non_capture_operator(view.from(inner_start))) {
inner_start += 2;
}
prefix.append(view.start(), inner_start - view.start());
next.emplace_back(view.from(closing_parenthesis));
// Recursively go through what is enclosed by parentheses
expand(StringView(inner_start, closing_parenthesis),
std::move(prefix),
std::move(next),
out);
return true;
}
} else if (*s == ')') {
if (level > 0) {
--level;
}
} else if (*s == '[') {
const char* closing_parenthesis = get_closing_parenthesis(view.from(s));
if (closing_parenthesis) {
// Skip character-level alternative block
s = closing_parenthesis;
continue;
}
}
}
prev_was_backslash = *s == '\\' && !prev_was_backslash;
++s;
}
// No alternatives or parentheses, so add to to prefix
prefix.append(view.start(), s - view.start());
return false;
}
void expand(const StringView& view,
std::string prefix,
Stack next,
std::vector<std::string>& out) {
if (expand_root_level_alternatives(view, prefix, next, out)) {
// Root-level alternatives were handled recursively
return;
}
if (expand_root_level_parentheses(view, prefix, next, out)) {
// Root-level parentheses where handled recursively
return;
}
if (next.empty()) {
// Reached end of string, add what has been collected
out.emplace_back(std::move(prefix));
} else {
// Pop the stack and continue with rest of string
StringView next_view = next.back();
next.pop_back();
expand(next_view, std::move(prefix), std::move(next), out);
}
}
} // namespace
std::vector<std::string> AlternativeExpander::expand(
const StringView& expression) {
std::vector<std::string> out;
std::string prefix;
Stack next;
uap_cpp::expand(expression, std::move(prefix), std::move(next), out);
return out;
}
} // namespace uap_cpp