forked from aligrudi/neatlibc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
regex.c
620 lines (580 loc) · 13.4 KB
/
regex.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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "regex.h"
#define NGRPS 64 /* maximum number of groups */
#define NREPS 128 /* maximum repetitions */
#define NDEPT 256 /* re_rec() recursion depth limit */
#define MAX(a, b) ((a) < (b) ? (b) : (a))
#define LEN(a) (sizeof(a) / sizeof((a)[0]))
/* regular expressions atoms */
#define RA_CHR '\0' /* character literal */
#define RA_BEG '^' /* string start */
#define RA_END '$' /* string end */
#define RA_ANY '.' /* any character */
#define RA_BRK '[' /* bracket expression */
#define RA_WBEG '<' /* word start */
#define RA_WEND '>' /* word end */
/* regular expression node types */
#define RN_ATOM '\0' /* regular expression */
#define RN_CAT 'c' /* concatenation */
#define RN_ALT '|' /* alternate expressions */
#define RN_GRP '(' /* pattern group */
/* regular expression program instructions */
#define RI_ATOM '\0' /* regular expression */
#define RI_FORK 'f' /* fork the execution */
#define RI_JUMP 'j' /* jump to the given instruction */
#define RI_MARK 'm' /* mark the current position */
#define RI_MATCH 'q' /* the pattern is matched */
/* regular expression atom */
struct ratom {
int ra; /* atom type (RA_*) */
char *s; /* atom argument */
};
/* regular expression instruction */
struct rinst {
struct ratom ra; /* regular expression atom (RI_ATOM) */
int ri; /* instruction type (RI_*) */
int a1, a2; /* destination of RI_FORK and RI_JUMP */
int mark; /* mark (RI_MARK) */
};
/* regular expression program */
struct regex {
struct rinst *p; /* the program */
int n; /* number of instructions */
int flg; /* regcomp() flags */
};
/* regular expression matching state */
struct rstate {
char *s; /* the current position in the string */
char *o; /* the beginning of the string */
int mark[NGRPS * 2]; /* marks for RI_MARK */
int pc; /* program counter */
int flg; /* flags passed to regcomp() and regexec() */
int dep; /* re_rec() depth */
};
/* regular expression tree; used for parsing */
struct rnode {
struct ratom ra; /* regular expression atom (RN_ATOM) */
struct rnode *c1, *c2; /* children */
int mincnt, maxcnt; /* number of repetitions */
int grp; /* group number */
int rn; /* node type (RN_*) */
};
static struct rnode *rnode_make(int rn, struct rnode *c1, struct rnode *c2)
{
struct rnode *rnode = malloc(sizeof(*rnode));
memset(rnode, 0, sizeof(*rnode));
rnode->rn = rn;
rnode->c1 = c1;
rnode->c2 = c2;
rnode->mincnt = 1;
rnode->maxcnt = 1;
return rnode;
}
static void rnode_free(struct rnode *rnode)
{
if (rnode->c1)
rnode_free(rnode->c1);
if (rnode->c2)
rnode_free(rnode->c2);
free(rnode->ra.s);
free(rnode);
}
static int uc_len(char *s)
{
int c = (unsigned char) s[0];
if (~c & 0xc0) /* ASCII or invalid */
return c > 0;
if (~c & 0x20)
return 2;
if (~c & 0x10)
return 3;
if (~c & 0x08)
return 4;
return 1;
}
static int uc_dec(char *s)
{
int c = (unsigned char) s[0];
if (~c & 0xc0) /* ASCII or invalid */
return c;
if (~c & 0x20)
return ((c & 0x1f) << 6) | (s[1] & 0x3f);
if (~c & 0x10)
return ((c & 0x0f) << 12) | ((s[1] & 0x3f) << 6) | (s[2] & 0x3f);
if (~c & 0x08)
return ((c & 0x07) << 18) | ((s[1] & 0x3f) << 12) | ((s[2] & 0x3f) << 6) | (s[3] & 0x3f);
return c;
}
static void ratom_copy(struct ratom *dst, struct ratom *src)
{
dst->ra = src->ra;
dst->s = NULL;
if (src->s) {
int len = strlen(src->s);
dst->s = malloc(len + 1);
memcpy(dst->s, src->s, len + 1);
}
}
static int brk_len(char *s)
{
int n = 1;
if (s[n] == '^') /* exclusion mark */
n++;
if (s[n] == ']') /* handling []a] */
n++;
while (s[n] && s[n] != ']') {
if (s[n] == '[' && (s[n + 1] == ':' || s[n + 1] == '='))
while (s[n] && s[n] != ']')
n++;
if (s[n])
n++;
}
return s[n] == ']' ? n + 1 : n;
}
static void ratom_readbrk(struct ratom *ra, char **pat)
{
int len = brk_len(*pat);
ra->ra = RA_BRK;
ra->s = malloc(len + 1);
memcpy(ra->s, *pat, len);
ra->s[len] = '\0';
*pat += len;
}
static void ratom_read(struct ratom *ra, char **pat)
{
int len;
switch ((unsigned char) **pat) {
case '.':
ra->ra = RA_ANY;
(*pat)++;
break;
case '^':
ra->ra = RA_BEG;
(*pat)++;
break;
case '$':
ra->ra = RA_END;
(*pat)++;
break;
case '[':
ratom_readbrk(ra, pat);
break;
case '\\':
if ((*pat)[1] == '<' || (*pat)[1] == '>') {
ra->ra = (*pat)[1] == '<' ? RA_WBEG : RA_WEND;
*pat += 2;
break;
}
(*pat)++;
default:
ra->ra = RA_CHR;
len = uc_len(*pat);
ra->s = malloc(8);
memcpy(ra->s, *pat, len);
ra->s[len] = '\0';
*pat += len;
}
}
static char *uc_beg(char *beg, char *s)
{
while (s > beg && (((unsigned char) *s) & 0xc0) == 0x80)
s--;
return s;
}
static int isword(char *s)
{
int c = (unsigned char) s[0];
return isalnum(c) || c == '_' || c > 127;
}
static char *brk_classes[][2] = {
{":alnum:", "a-zA-Z0-9"},
{":alpha:", "a-zA-Z"},
{":blank:", " \t"},
{":digit:", "0-9"},
{":lower:", "a-z"},
{":print:", "\x20-\x7e"},
{":punct:", "][!\"#$%&'()*+,./:;<=>?@\\^_`{|}~-"},
{":space:", " \t\r\n\v\f"},
{":upper:", "A-Z"},
{":word:", "a-zA-Z0-9_"},
{":xdigit:", "a-fA-F0-9"},
};
static int brk_match(char *brk, int c, int flg)
{
int beg, end;
int i;
int not = brk[0] == '^';
char *p = not ? brk + 1 : brk;
char *p0 = p;
if (flg & REG_ICASE && c < 128 && isupper(c))
c = tolower(c);
while (*p && (p == p0 || *p != ']')) {
if (p[0] == '[' && p[1] == ':') {
for (i = 0; i < LEN(brk_classes); i++) {
char *cc = brk_classes[i][0];
char *cp = brk_classes[i][1];
if (!strncmp(cc, p + 1, strlen(cc)))
if (!brk_match(cp, c, flg))
return not;
}
p += brk_len(p);
continue;
}
beg = uc_dec(p);
p += uc_len(p);
end = beg;
if (p[0] == '-' && p[1] && p[1] != ']') {
p++;
end = uc_dec(p);
p += uc_len(p);
}
if (flg & REG_ICASE && beg < 128 && isupper(beg))
beg = tolower(beg);
if (flg & REG_ICASE && end < 128 && isupper(end))
end = tolower(end);
if (c >= beg && c <= end)
return not;
}
return !not;
}
static int ratom_match(struct ratom *ra, struct rstate *rs)
{
if (ra->ra == RA_CHR) {
int c1 = uc_dec(ra->s);
int c2 = uc_dec(rs->s);
if (rs->flg & REG_ICASE && c1 < 128 && isupper(c1))
c1 = tolower(c1);
if (rs->flg & REG_ICASE && c2 < 128 && isupper(c2))
c2 = tolower(c2);
if (c1 != c2)
return 1;
rs->s += uc_len(ra->s);
return 0;
}
if (ra->ra == RA_ANY) {
if (!rs->s[0])
return 1;
rs->s += uc_len(rs->s);
return 0;
}
if (ra->ra == RA_BRK) {
int c = uc_dec(rs->s);
if (!c)
return 1;
rs->s += uc_len(rs->s);
return brk_match(ra->s + 1, c, rs->flg);
}
if (ra->ra == RA_BEG && !(rs->flg & REG_NOTBOL))
return rs->s != rs->o;
if (ra->ra == RA_END && !(rs->flg & REG_NOTEOL))
return rs->s[0] != '\0';
if (ra->ra == RA_WBEG)
return !((rs->s == rs->o || !isword(uc_beg(rs->o, rs->s - 1))) &&
isword(rs->s));
if (ra->ra == RA_WEND)
return !(rs->s != rs->o && isword(uc_beg(rs->o, rs->s - 1)) &&
(!rs->s[0] || !isword(rs->s)));
return 1;
}
static struct rnode *rnode_parse(char **pat);
static struct rnode *rnode_grp(char **pat)
{
struct rnode *rnode = NULL;
if ((*pat)[0] != '(')
return NULL;
*pat += 1;
if ((*pat)[0] != ')') {
rnode = rnode_parse(pat);
if (!rnode)
return NULL;
}
if ((*pat)[0] != ')') {
rnode_free(rnode);
return NULL;
}
*pat += 1;
return rnode_make(RN_GRP, rnode, NULL);
}
static struct rnode *rnode_atom(char **pat)
{
struct rnode *rnode;
if (!**pat)
return NULL;
if ((*pat)[0] == '|' || (*pat)[0] == ')')
return NULL;
if ((*pat)[0] == '(') {
rnode = rnode_grp(pat);
} else {
rnode = rnode_make(RN_ATOM, NULL, NULL);
ratom_read(&rnode->ra, pat);
}
if (!rnode)
return NULL;
if ((*pat)[0] == '*' || (*pat)[0] == '?') {
rnode->mincnt = 0;
rnode->maxcnt = (*pat)[0] == '*' ? -1 : 1;
*pat += 1;
}
if ((*pat)[0] == '+') {
rnode->mincnt = 1;
rnode->maxcnt = -1;
*pat += 1;
}
if ((*pat)[0] == '{') {
rnode->mincnt = 0;
rnode->maxcnt = 0;
*pat += 1;
while (isdigit((unsigned char) **pat))
rnode->mincnt = rnode->mincnt * 10 + *(*pat)++ - '0';
if (**pat == ',') {
(*pat)++;
if ((*pat)[0] == '}')
rnode->maxcnt = -1;
while (isdigit((unsigned char) **pat))
rnode->maxcnt = rnode->maxcnt * 10 + *(*pat)++ - '0';
} else {
rnode->maxcnt = rnode->mincnt;
}
*pat += 1;
if (rnode->mincnt > NREPS || rnode->maxcnt > NREPS) {
rnode_free(rnode);
return NULL;
}
}
return rnode;
}
static struct rnode *rnode_seq(char **pat)
{
struct rnode *c1 = rnode_atom(pat);
struct rnode *c2;
if (!c1)
return NULL;
c2 = rnode_seq(pat);
return c2 ? rnode_make(RN_CAT, c1, c2) : c1;
}
static struct rnode *rnode_parse(char **pat)
{
struct rnode *c1 = rnode_seq(pat);
struct rnode *c2;
if ((*pat)[0] != '|')
return c1;
*pat += 1;
c2 = rnode_parse(pat);
return c2 ? rnode_make(RN_ALT, c1, c2) : c1;
}
static int rnode_count(struct rnode *rnode)
{
int n = 1;
if (!rnode)
return 0;
if (rnode->rn == RN_CAT)
n = rnode_count(rnode->c1) + rnode_count(rnode->c2);
if (rnode->rn == RN_ALT)
n = rnode_count(rnode->c1) + rnode_count(rnode->c2) + 2;
if (rnode->rn == RN_GRP)
n = rnode_count(rnode->c1) + 2;
if (rnode->mincnt == 0 && rnode->maxcnt == 0)
return 0;
if (rnode->mincnt == 1 && rnode->maxcnt == 1)
return n;
if (rnode->maxcnt < 0) {
n = (rnode->mincnt + 1) * n + 1;
} else {
n = (rnode->mincnt + rnode->maxcnt) * n +
rnode->maxcnt - rnode->mincnt;
}
if (!rnode->mincnt)
n++;
return n;
}
static int rnode_grpnum(struct rnode *rnode, int num)
{
int cur = 0;
if (!rnode)
return 0;
if (rnode->rn == RN_GRP)
rnode->grp = num + cur++;
cur += rnode_grpnum(rnode->c1, num + cur);
cur += rnode_grpnum(rnode->c2, num + cur);
return cur;
}
static int re_insert(struct regex *p, int ri)
{
p->p[p->n++].ri = ri;
return p->n - 1;
}
static void rnode_emit(struct rnode *n, struct regex *p);
static void rnode_emitnorep(struct rnode *n, struct regex *p)
{
int fork, done, mark;
if (n->rn == RN_ALT) {
fork = re_insert(p, RI_FORK);
p->p[fork].a1 = p->n;
rnode_emit(n->c1, p);
done = re_insert(p, RI_JUMP);
p->p[fork].a2 = p->n;
rnode_emit(n->c2, p);
p->p[done].a1 = p->n;
}
if (n->rn == RN_CAT) {
rnode_emit(n->c1, p);
rnode_emit(n->c2, p);
}
if (n->rn == RN_GRP) {
mark = re_insert(p, RI_MARK);
p->p[mark].mark = 2 * n->grp;
rnode_emit(n->c1, p);
mark = re_insert(p, RI_MARK);
p->p[mark].mark = 2 * n->grp + 1;
}
if (n->rn == RN_ATOM) {
int atom = re_insert(p, RI_ATOM);
ratom_copy(&p->p[atom].ra, &n->ra);
}
}
static void rnode_emit(struct rnode *n, struct regex *p)
{
int last;
int jmpend[NREPS];
int jmpend_cnt = 0;
int i;
if (!n)
return;
if (n->mincnt == 0 && n->maxcnt == 0)
return;
if (n->mincnt == 1 && n->maxcnt == 1) {
rnode_emitnorep(n, p);
return;
}
if (n->mincnt == 0) {
int fork = re_insert(p, RI_FORK);
p->p[fork].a1 = p->n;
jmpend[jmpend_cnt++] = fork;
}
for (i = 0; i < MAX(1, n->mincnt); i++) {
last = p->n;
rnode_emitnorep(n, p);
}
if (n->maxcnt < 0) {
int fork;
fork = re_insert(p, RI_FORK);
p->p[fork].a1 = last;
p->p[fork].a2 = p->n;
}
for (i = MAX(1, n->mincnt); i < n->maxcnt; i++) {
int fork = re_insert(p, RI_FORK);
p->p[fork].a1 = p->n;
jmpend[jmpend_cnt++] = fork;
rnode_emitnorep(n, p);
}
for (i = 0; i < jmpend_cnt; i++)
p->p[jmpend[i]].a2 = p->n;
}
int regcomp(regex_t *preg, char *pat, int flg)
{
struct rnode *rnode = rnode_parse(&pat);
struct regex *re;
int n = rnode_count(rnode) + 3;
int mark;
if (!rnode)
return 1;
rnode_grpnum(rnode, 1);
re = malloc(sizeof(*re));
memset(re, 0, sizeof(*re));
re->p = malloc(n * sizeof(re->p[0]));
memset(re->p, 0, n * sizeof(re->p[0]));
mark = re_insert(re, RI_MARK);
re->p[mark].mark = 0;
rnode_emit(rnode, re);
mark = re_insert(re, RI_MARK);
re->p[mark].mark = 1;
mark = re_insert(re, RI_MATCH);
rnode_free(rnode);
re->flg = flg;
*preg = re;
return 0;
}
void regfree(regex_t *preg)
{
struct regex *re = *preg;
int i;
for (i = 0; i < re->n; i++)
if (re->p[i].ri == RI_ATOM)
free(re->p[i].ra.s);
free(re->p);
free(re);
}
static int re_rec(struct regex *re, struct rstate *rs)
{
struct rinst *ri = NULL;
if (++(rs->dep) >= NDEPT)
return 1;
while (1) {
ri = &re->p[rs->pc];
if (ri->ri == RI_ATOM) {
if (ratom_match(&ri->ra, rs))
return 1;
rs->pc++;
continue;
}
if (ri->ri == RI_MARK) {
if (ri->mark < NGRPS)
rs->mark[ri->mark] = rs->s - rs->o;
rs->pc++;
continue;
}
if (ri->ri == RI_JUMP) {
rs->pc = ri->a1;
continue;
}
if (ri->ri == RI_FORK) {
struct rstate base = *rs;
rs->pc = ri->a1;
if (!re_rec(re, rs))
return 0;
*rs = base;
rs->pc = ri->a2;
continue;
}
break;
}
return ri->ri != RI_MATCH;
}
static int re_recmatch(struct regex *re, struct rstate *rs, int nsub, regmatch_t *psub)
{
int i;
rs->pc = 0;
for (i = 0; i < LEN(rs->mark); i++)
rs->mark[i] = -1;
rs->dep = 0;
if (!re_rec(re, rs)) {
for (i = 0; i < nsub; i++) {
psub[i].rm_so = i * 2 < LEN(rs->mark) ? rs->mark[i * 2] : -1;
psub[i].rm_eo = i * 2 < LEN(rs->mark) ? rs->mark[i * 2 + 1] : -1;
}
return 0;
}
return 1;
}
int regexec(regex_t *preg, char *s, int nsub, regmatch_t psub[], int flg)
{
struct regex *re = *preg;
struct rstate rs;
memset(&rs, 0, sizeof(rs));
rs.flg = re->flg | flg;
rs.o = s;
while (*s) {
rs.s = s;
s += uc_len(s);
if (!re_recmatch(re, &rs, flg & REG_NOSUB ? 0 : nsub, psub))
return 0;
}
return 1;
}
int regerror(int errcode, regex_t *preg, char *errbuf, int errbuf_size)
{
return 0;
}