-
Notifications
You must be signed in to change notification settings - Fork 4
/
rexp3.c
470 lines (427 loc) · 8.62 KB
/
rexp3.c
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/********************************************
rexp3.c
copyright 2008-2013,2014, Thomas E. Dickey
copyright 2010, Jonathan Nieder
copyright 1991-1992,1993, Michael D. Brennan
This is a source file for mawk, an implementation of
the AWK programming language.
Mawk is distributed without warranty under the terms of
the GNU General Public License, version 2, 1991.
********************************************/
/*
* $MawkId: rexp3.c,v 1.36 2014/09/12 23:19:45 tom Exp $
* @Log: rexp3.c,v @
* Revision 1.3 1993/07/24 17:55:15 mike
* more cleanup
*
* Revision 1.2 1993/07/23 13:21:48 mike
* cleanup rexp code
*
* Revision 1.1.1.1 1993/07/03 18:58:28 mike
* move source to cvs
*
* Revision 3.6 1992/12/24 00:44:53 mike
* fixed potential LMDOS bozo with M_STR+U_ON+END_ON
* fixed minor bug in M_CLASS+U_ON+END_ON
*
* Revision 3.5 1992/01/21 17:33:20 brennan
* added some casts so that character classes work with signed chars
*
* Revision 3.4 91/10/29 10:54:09 brennan
* SIZE_T
*
* Revision 3.3 91/08/13 09:10:18 brennan
* VERSION .9994
*
* Revision 3.2 91/06/10 16:18:17 brennan
* changes for V7
*
* Revision 3.1 91/06/07 10:33:28 brennan
* VERSION 0.995
*
* Revision 1.4 91/05/31 10:56:32 brennan
* stack_empty hack for DOS large model
*
*/
/* match a string against a machine */
#include "rexp.h"
#define push(mx,sx,px,ssx,ux) do { \
if (++stackp == RE_run_stack_limit) \
stackp = RE_new_run_stack() ;\
stackp->m = (mx); \
stackp->s = (sx); \
stackp->sp = (int) ((px) - RE_pos_stack_base); \
stackp->tp = (px)->prev_offset; \
stackp->ss = (ssx); \
stackp->u = (ux); \
} while(0)
#define CASE_UANY(x) case x + U_OFF : case x + U_ON
#define RE_TURN() \
if (cb_ss) { \
*lenp = (size_t) (cb_e - cb_ss); \
} \
TRACE(("=%i/%lu\n", (int) (s - str), (unsigned long) *lenp)); \
return cb_ss
/* returns start of first longest match and the length by
reference. If no match returns NULL and length zero */
char *
REmatch(char *str, /* string to test */
size_t str_len, /* ...its length */
PTR machine, /* compiled regular expression */
size_t *lenp, /* where to return matched-length */
int no_bol) /* disallow match at beginning of line */
{
register STATE *m = (STATE *) machine;
char *s = str;
char *ss;
register RT_STATE *stackp;
int u_flag, t;
char *str_end = s + str_len;
RT_POS_ENTRY *sp;
char *ts;
/* state of current best match stored here */
char *cb_ss; /* the start */
char *cb_e = 0; /* the end , pts at first char not matched */
*lenp = 0;
TRACE(("REmatch: %s \"%s\" ~ /pattern/", no_bol ? "any" : "1st", str));
/* check for the easy case */
if ((m + 1)->s_type == M_ACCEPT && m->s_type == M_STR) {
if ((ts = str_str(s, str_len, m->s_data.str, (size_t) m->s_len))) {
*lenp = m->s_len;
}
TRACE(("=%i/%lu\n", (int) (ts - str), (unsigned long) *lenp));
return ts;
}
u_flag = U_ON;
cb_ss = ss = (char *) 0;
stackp = RE_run_stack_empty;
sp = RE_pos_stack_empty;
RE_CASE();
refill:
if (stackp == RE_run_stack_empty) {
RE_TURN();
}
ss = stackp->ss;
s = (stackp--)->s;
if (cb_ss) { /* does new state start too late ? */
if (ss) {
if (cb_ss < ss || (cb_ss == ss && cb_e == str_end)) {
RE_FILL();
}
} else if (cb_ss < s || (cb_ss == s && cb_e == str_end)) {
RE_FILL();
}
}
m = (stackp + 1)->m;
sp = RE_pos_stack_base + (stackp + 1)->sp;
sp->prev_offset = (stackp + 1)->tp;
u_flag = (stackp + 1)->u;
reswitch:
switch (m->s_type + u_flag) {
case M_STR + U_OFF + END_OFF:
if (strncmp(s, m->s_data.str, (size_t) m->s_len)) {
RE_FILL();
}
if (!ss) {
if (cb_ss && s > cb_ss) {
RE_FILL();
} else {
ss = s;
}
}
s += m->s_len;
m++;
RE_CASE();
case M_STR + U_OFF + END_ON:
if (strcmp(s, m->s_data.str)) {
RE_FILL();
}
if (!ss) {
if (cb_ss && s > cb_ss) {
RE_FILL();
} else {
ss = s;
}
}
s += m->s_len;
m++;
RE_CASE();
case M_STR + U_ON + END_OFF:
if (s >= str_end) {
RE_FILL();
}
if (!(s = str_str(s, (size_t) (str_end - s), m->s_data.str, (size_t) m->s_len))) {
RE_FILL();
}
if (s >= str_end) {
RE_FILL();
}
push(m, s + 1, sp, ss, U_ON);
if (!ss) {
if (cb_ss && s > cb_ss) {
RE_FILL();
} else {
ss = s;
}
}
s += m->s_len;
m++;
u_flag = U_OFF;
RE_CASE();
case M_STR + U_ON + END_ON:
t = (int) ((SLen) (str_end - s) - m->s_len);
if (t < 0 || memcmp(ts = s + t, m->s_data.str, (size_t) m->s_len)) {
RE_FILL();
}
if (!ss) {
if (cb_ss && ts > cb_ss) {
RE_FILL();
} else {
ss = ts;
}
}
s = str_end;
m++;
u_flag = U_OFF;
RE_CASE();
case M_CLASS + U_OFF + END_OFF:
if (s >= str_end) {
RE_FILL();
}
if (!ison(*m->s_data.bvp, s[0])) {
RE_FILL();
}
if (!ss) {
if (cb_ss && s > cb_ss) {
RE_FILL();
} else {
ss = s;
}
}
s++;
m++;
RE_CASE();
case M_CLASS + U_OFF + END_ON:
if (s >= str_end) {
RE_FILL();
}
if (s[1] || !ison(*m->s_data.bvp, s[0])) {
RE_FILL();
}
if (!ss) {
if (cb_ss && s > cb_ss) {
RE_FILL();
} else {
ss = s;
}
}
s++;
m++;
RE_CASE();
case M_CLASS + U_ON + END_OFF:
if (s >= str_end) {
RE_FILL();
}
while (!ison(*m->s_data.bvp, s[0])) {
if (s >= str_end) {
RE_FILL();
} else {
s++;
}
}
if (s >= str_end) {
RE_FILL();
}
s++;
push(m, s, sp, ss, U_ON);
if (!ss) {
if (cb_ss && s - 1 > cb_ss) {
RE_FILL();
} else {
ss = s - 1;
}
}
m++;
u_flag = U_OFF;
RE_CASE();
case M_CLASS + U_ON + END_ON:
if ((s >= str_end) || !ison(*m->s_data.bvp, str_end[-1])) {
RE_FILL();
}
if (!ss) {
if (cb_ss && str_end - 1 > cb_ss) {
RE_FILL();
} else {
ss = str_end - 1;
}
}
s = str_end;
m++;
u_flag = U_OFF;
RE_CASE();
case M_ANY + U_OFF + END_OFF:
if (s >= str_end) {
RE_FILL();
}
if (!ss) {
if (cb_ss && s > cb_ss) {
RE_FILL();
} else {
ss = s;
}
}
s++;
m++;
RE_CASE();
case M_ANY + U_OFF + END_ON:
if ((s >= str_end) || ((s + 1) < str_end)) {
RE_FILL();
}
if (!ss) {
if (cb_ss && s > cb_ss) {
RE_FILL();
} else {
ss = s;
}
}
s++;
m++;
RE_CASE();
case M_ANY + U_ON + END_OFF:
if (s >= str_end) {
RE_FILL();
}
s++;
push(m, s, sp, ss, U_ON);
if (!ss) {
if (cb_ss && s - 1 > cb_ss) {
RE_FILL();
} else {
ss = s - 1;
}
}
m++;
u_flag = U_OFF;
RE_CASE();
case M_ANY + U_ON + END_ON:
if (s >= str_end) {
RE_FILL();
}
if (!ss) {
if (cb_ss && str_end - 1 > cb_ss) {
RE_FILL();
} else {
ss = str_end - 1;
}
}
s = str_end;
m++;
u_flag = U_OFF;
RE_CASE();
case M_START + U_OFF + END_OFF:
case M_START + U_ON + END_OFF:
if (s != str || no_bol) {
RE_FILL();
}
ss = s;
m++;
u_flag = U_OFF;
RE_CASE();
case M_START + U_OFF + END_ON:
case M_START + U_ON + END_ON:
if (s != str || no_bol || (s < str_end)) {
RE_FILL();
}
ss = s;
m++;
u_flag = U_OFF;
RE_CASE();
case M_END + U_OFF:
if (s < str_end) {
RE_FILL();
}
if (!ss) {
if (cb_ss && s > cb_ss) {
RE_FILL();
} else {
ss = s;
}
}
m++;
RE_CASE();
case M_END + U_ON:
s = str_end;
if (!ss) {
if (cb_ss && s > cb_ss) {
RE_FILL();
} else {
ss = s;
}
}
m++;
u_flag = U_OFF;
RE_CASE();
CASE_UANY(M_U):
if (!ss) {
if (cb_ss && s > cb_ss) {
RE_FILL();
} else {
ss = s;
}
}
u_flag = U_ON;
m++;
RE_CASE();
CASE_UANY(M_1J):
m += m->s_data.jump;
RE_CASE();
CASE_UANY(M_SAVE_POS): /* save position for a later M_2JC */
/* see also REtest */
sp = RE_pos_push(sp, stackp, s);
m++;
RE_CASE();
CASE_UANY(M_2JA): /* take the non jump branch */
push(m + m->s_data.jump, s, sp, ss, u_flag);
m++;
RE_CASE();
CASE_UANY(M_2JC): /* take the jump branch if position changed */
/* see REtest */
if (RE_pos_pop(&sp, stackp) == s) {
m++;
RE_CASE();
}
/* fall thru */
CASE_UANY(M_2JB): /* take the jump branch */
push(m + 1, s, sp, ss, u_flag);
m += m->s_data.jump;
RE_CASE();
case M_ACCEPT + U_OFF:
if (!ss)
ss = s;
if (!cb_ss || ss < cb_ss || (ss == cb_ss && s > cb_e)) {
/* we have a new current best */
cb_ss = ss;
cb_e = s;
} else if (ss == cb_ss && s == cb_e) {
RE_TURN();
}
RE_FILL();
case M_ACCEPT + U_ON:
if (!ss) {
ss = s;
} else {
s = str_end;
}
if (!cb_ss || ss < cb_ss || (ss == cb_ss && s > cb_e)) {
/* we have a new current best */
cb_ss = ss;
cb_e = s;
} else if (ss == cb_ss && s == cb_e) {
RE_TURN();
}
RE_FILL();
default:
RE_panic("unexpected case in REmatch");
}
}
#undef push