-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathre2_c.cxx
343 lines (284 loc) · 9.14 KB
/
re2_c.cxx
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#include <ctype.h> // for tolower()
#include <stdio.h> // for snprintf()
#include <re2/re2.h>
#include <re2/stringpiece.h>
#include "re2_c.h"
using namespace std;
#define likely(x) __builtin_expect((x),1)
#define unlikely(x) __builtin_expect((x),0)
#ifdef DEBUG
// Usage examples: ASSERT(a > b), ASSERT(foo() && "Opps, foo() reutrn 0");
#define ASSERT(c) if (!(c))\
{ fprintf(stderr, "%s:%d Assert: %s\n", __FILE__, __LINE__, #c); abort(); }
#else
#define ASSERT(c) ((void)0)
#endif
#define CAP_VECTOR_DEFAULT_LEN 64
/* This data structure is used to return some variable-length results back to
* caller.
*/
struct re2c_match_aux {
char* errstr;
re2::StringPiece* captures;
re2::StringPiece* captures_r; /* collections of all captures for Consume/FindAndConsume apis */
unsigned short errstr_buf_len;
unsigned short cap_vect_len; /* the capacity of captures vector */
unsigned short ncap; /* cache of RE2::NumberOfCapturingGroups() */
unsigned short cap_r_vect_len;
unsigned short cap_r_vect_max_len;
};
/* Record captures per match in captures_r vector.
* captures_r vector will be realloceted automatically. */
unsigned
re2c_record_capture(struct re2c_match_aux* aux) {
if (unlikely(!aux->captures))
return 0;
if (!aux->captures_r) {
aux->captures_r = new re2::StringPiece[CAP_VECTOR_DEFAULT_LEN];
if (!aux->captures_r) return 1;
}
if (aux->cap_r_vect_len + aux->cap_vect_len >= aux->cap_r_vect_max_len) {
aux->cap_r_vect_max_len *= 2;
re2::StringPiece *new_captures_r = new re2::StringPiece[aux->cap_r_vect_max_len];
if (!new_captures_r)
return 1;
for (int i = 0; i < aux->cap_r_vect_len; i++) {
new_captures_r[i] = aux->captures_r[i];
}
delete[] aux->captures_r;
aux->captures_r = new_captures_r;
}
for (int i = 0; i < aux->ncap; i++) {
aux->captures_r[aux->cap_r_vect_len] = aux->captures[i];
aux->cap_r_vect_len++;
}
return 0;
}
unsigned
re2c_get_capture_r_count(struct re2c_match_aux* aux) {
return aux->cap_r_vect_len;
}
const char*
re2c_get_capture_r(struct re2c_match_aux* aux, unsigned idx) {
if (unlikely(!aux->captures_r))
return 0;
if (unlikely(aux->cap_r_vect_len <= idx))
return 0;
return aux->captures_r[idx].data();
}
unsigned
re2c_get_capture_r_len(struct re2c_match_aux* aux, unsigned idx) {
if (unlikely(!aux->captures_r))
return 0;
if (unlikely(aux->cap_r_vect_len <= idx))
return 0;
return aux->captures_r[idx].size();
}
/* Return the "idx"-th capture. NOTE: Captures are not necessarily ended with
* '\0'.
*/
const char*
re2c_get_capture(struct re2c_match_aux* aux, unsigned idx) {
if (unlikely(!aux->captures))
return 0;
if (unlikely(aux->ncap <= idx))
return 0;
return aux->captures[idx].data();
}
unsigned
re2c_get_capture_len(struct re2c_match_aux* aux, unsigned idx) {
if (unlikely(!aux->captures))
return 0;
if (unlikely(aux->ncap <= idx))
return 0;
return aux->captures[idx].size();
}
struct re2c_match_aux*
re2c_alloc_aux(void) {
struct re2c_match_aux* p = new struct re2c_match_aux;
p->errstr = 0;
p->captures = 0;
p->errstr_buf_len = 0;
p->cap_vect_len = 0;
p->ncap = 0;
p->captures_r = 0;
p->cap_r_vect_len = 0;
p->cap_r_vect_max_len = CAP_VECTOR_DEFAULT_LEN;
return p;
}
void
re2c_free_aux(struct re2c_match_aux* p) {
delete[] p->errstr;
delete[] p->captures;
delete[] p->captures_r;
delete p;
}
const char*
re2c_get_errstr(struct re2c_match_aux* aux) {
return aux->errstr;
}
static void
copy_errstr(char* buffer, int buf_len, const string& src) {
if (!buffer)
return;
int copy_len = src.size();
if (copy_len > buf_len - 1)
copy_len = buf_len - 1;
strncpy(buffer, src.c_str(), copy_len);
buffer[copy_len] = '\0';
}
struct re2_pattern_t*
re2c_compile(const char* pattern, int pattern_len, const char* re2_options,
char* errstr, int errstrlen, unsigned max_mem) {
const char* ptn_ptr = pattern;
int ptn_len = pattern_len;
// Process the options
re2::RE2::Options opts;
opts.set_log_errors(false);
if (re2_options) {
const char* p = re2_options;
bool multiline = false;
opts.set_perl_classes(true);
opts.set_word_boundary(true);
while (char c = *p++) {
bool turn_on = true;
if (c >= 'A' && c <= 'Z') {
turn_on = false;
c = tolower(c);
}
switch (c) {
case 'u': opts.set_utf8(turn_on); break;
case 'p': opts.set_posix_syntax(turn_on); break;
case 'a': opts.set_longest_match(turn_on); break;
case 'e': opts.set_log_errors(turn_on); break;
case 'l': opts.set_literal(turn_on); break;
case 'n': opts.set_never_nl(turn_on); break;
case 's': opts.set_dot_nl(turn_on); break;
case 'c': opts.set_never_capture(turn_on); break;
case 'i': opts.set_case_sensitive(!turn_on); break;
case 'm': multiline = true; break;
default:
{
fprintf(stderr, "unsupport flag\n");
string s = "unsupport flags ";
s += c;
copy_errstr(errstr, errstrlen, s);
return 0;
}
}
}
if (max_mem == 0) {max_mem = 2048 * 1024; }
opts.set_max_mem(max_mem);
// FIXME:one-line mode is always turned on in non-posix mode. To
// workaround the problem, we enclose the pattern with "(?m:...)"
if (multiline) {
const char* prefix = "(?m:";
const char* postfix = ")";
char* t;
t = new char[ptn_len + strlen(prefix) + strlen(postfix) + 1];
strcpy(t, prefix);
memcpy(t + strlen(prefix), pattern, ptn_len);
strcpy(t + strlen(prefix) + ptn_len, postfix);
ptn_ptr = t;
ptn_len += strlen(prefix) + strlen(postfix);
}
}
// Now compile the pattern
RE2* pat = new RE2(re2::StringPiece(ptn_ptr, ptn_len), opts);
if (ptn_ptr != pattern)
delete[] ptn_ptr;
if (pat && !pat->ok()) {
copy_errstr(errstr, errstrlen, pat->error());
delete pat;
return 0;
}
return (re2_pattern_t*)(void*)pat;
}
void
re2c_free(struct re2_pattern_t* pat) {
delete (RE2*)(void*)pat;
}
/* Return the number of captures of the given pattern */
int
re2c_getncap(struct re2_pattern_t* pattern) {
RE2* pat = reinterpret_cast<RE2*>(pattern);
return pat->NumberOfCapturingGroups();
}
/* Return 0 if the pattern matches the given text, 1 otherwise. */
int
re2c_find(const char* text, int text_len, struct re2_pattern_t* pattern) {
RE2* re2 = (RE2*)(void*)pattern;
if (unlikely(!re2))
return 1;
bool match = re2->Match(re2::StringPiece(text, text_len),
0 /* startpos */, text_len /* endpos*/,
re2::RE2::UNANCHORED, 0, 0);
return match ? 0 : 1;
}
/* Return 0 if the pattern matches the given text, 1 otherwise; captures are
* returned via "aux".
*/
int
re2c_match(const char* text, int text_len, struct re2_pattern_t* pattern,
struct re2c_match_aux* aux) {
RE2* re2 = (RE2*)(void*)pattern;
if (unlikely(!re2))
return 1;
int ncap = re2->NumberOfCapturingGroups() + 1;
if (!aux->cap_vect_len || aux->cap_vect_len < ncap) {
delete[] aux->captures;
aux->captures = new re2::StringPiece[ncap];
aux->cap_vect_len = ncap;
}
aux->ncap = ncap;
bool match = re2->Match(re2::StringPiece(text, text_len),
0 /* startpos */, text_len /* endpos*/,
re2::RE2::UNANCHORED, aux->captures, ncap);
return match ? 0 : 1;
}
/* Return 0 if the pattern matches the given text, 1 otherwise; captures are
* returned via "aux".
*/
int
re2c_match_r(const char* text, int text_len, struct re2_pattern_t* pattern,
struct re2c_match_aux* aux) {
RE2* re2 = (RE2*)(void*)pattern;
if (unlikely(!re2)) {
return 1;
}
int ncap = re2->NumberOfCapturingGroups();
if (0 != aux->captures) {
delete[] aux->captures;
aux->cap_vect_len = 0;
}
aux->captures = new re2::StringPiece[ncap];
if (unlikely(!aux->captures)) {
return 1;
}
aux->cap_vect_len = ncap;
aux->ncap = ncap;
if (0 != aux->captures_r) {
aux->cap_r_vect_len = 0;
}
RE2::Arg* argv = new RE2::Arg[ncap];
if (unlikely(!argv)) {
return 1;
}
RE2::Arg** args = new RE2::Arg* [ncap];
if (unlikely(!args)) {
delete[] argv;
return 1;
}
for (int i = 0; i < ncap; i++) {
argv[i] = &aux->captures[i];
args[i] = &argv[i];
}
re2::StringPiece input(text, text_len);
bool match = false;
while (re2->FindAndConsumeN(&input, *re2, args, ncap)) {
match = true;
re2c_record_capture(aux);
}
delete[] args;
return match ? 0 : 1;
}