-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchar_pattern.h
235 lines (205 loc) · 4 KB
/
char_pattern.h
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#pragma once
#include <list>
#include <string>
struct char_pattern_range
{
char first;
char last;
};
class char_pattern_group
{
public:
char_pattern_group()
{
_any = false;
_remove = false;
}
void add(char first, char last, bool any, bool remove)
{
_any = any;
_remove = remove;
_ranges.push_back(new char_pattern_range{ first, last });
}
std::list<char_pattern_range*>& ranges()
{
return _ranges;
}
bool check(char ch, bool& remove)
{
for (auto r : _ranges)
{
remove = _remove;
if (r->first == 0x7F)
return true;
if (ch >= r->first && ch <= r->last)
return true;
}
remove = false;
return false;
}
bool any()
{
return _any;
}
private:
std::list<char_pattern_range*> _ranges;
bool _any;
bool _remove;
friend class char_pattern;
};
class char_pattern
{
public:
char_pattern(const char* pat)
{
char_pattern_group* group = NULL;
auto any = false;
auto one_of = false;
auto spec = false;
_use_backslashes = true;
while (*pat)
{
if (!spec && !any && !one_of && pat[0] == '[' && pat[1] != ']' && pat[1] != 0)
{
any = true;
pat++;
continue;
}
if (!spec && any && *pat == ']')
{
any = false;
pat++;
group = NULL;
continue;
}
if (!spec && !any && !one_of && pat[0] == '(' && pat[1] != ')' && pat[1] != 0)
{
one_of = true;
pat++;
continue;
}
if (!spec && one_of && *pat == ')')
{
one_of = false;
pat++;
if (*pat == '\b')
{
group->_remove = true;
pat++;
}
group = NULL;
continue;
}
if (*pat == '\\' && !spec)
{
spec = true;
pat++;
continue;
}
if (group == NULL)
{
group = new char_pattern_group();
_groups.push_back(group);
}
if (!spec && pat[0] == '[' && pat[1] == ']')
{
group->add(0x7F, 0x7F, true, false);
pat += 2;
}
else if (!spec && pat[0] == '^')
{
group->add(1, pat[1] - 1, any, false);
group->add(pat[1] + 1, 0x7F, any, false);
pat += 2;
}
else if (pat[1] == '-' && pat[2] && pat[2] != ']' && pat[2] != ')')
{
group->add(pat[0], pat[2], any, false);
pat += 3;
}
else
{
group->add(*pat, *pat, any, pat[1] == '\b');
pat++;
if (*pat == '\b')
pat++;
}
if (!any && !one_of)
group = NULL;
spec = false;
}
}
~char_pattern()
{
for (auto it = std::begin(_groups); it != std::end(_groups); ++it)
delete (*it);
}
bool check(std::string::iterator str_it, const std::string::iterator str_end, std::string& res, size_t& advance)
{
return check(str_it, str_end, res, advance, std::begin(_groups));
}
bool check(std::string::iterator str_it, const std::string::iterator str_end, std::string& res, size_t& advance, std::list<char_pattern_group*>::iterator group)
{
auto match = false;
res.clear();
advance = 0;
if (str_it == str_end)
return false;
for (auto it = group; it != std::end(_groups); ++it)
{
auto g = *it;
if (str_it == str_end)
return false;
auto remove = false;
if (!g->check(*str_it, remove))
return false;
char last = *str_it;
if (!remove)
res += *str_it;
advance++;
str_it++;
if (g->any())
{
while (str_it != str_end)
{
if (last == '\\' && _use_backslashes)
{
res += *str_it;
advance++;
str_it++;
last = 0;
continue;
}
if (!g->check(*str_it, remove))
break;
last = *str_it;
std::string res_next;
std::string::iterator str_it_next = str_it;
std::list<char_pattern_group*>::iterator it_next = it;
++it_next;
if (it_next != std::end(_groups))
{
size_t advance_next = 0;
if (check(str_it_next, str_end, res_next, advance_next, it_next))
{
res += res_next;
advance += advance_next;
return true;
}
}
res += *str_it;
advance++;
str_it++;
}
}
}
return true;
}
void use_backslashes(bool value)
{
_use_backslashes = value;
}
private:
std::list<char_pattern_group*> _groups;
bool _use_backslashes;
};