-
Notifications
You must be signed in to change notification settings - Fork 5
/
regex.c
477 lines (431 loc) · 10.8 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
/*
* This is a regular expression evaluator based on code from the article
* "Regular Expressions: Languages, algorithms, and software"
* By Brian W. Kernighan and Rob Pike in the April 1999 issue of
* Dr. Dobb's Journal
*
* I've found the article online at
* http://www.ddj.com/dept/architect/184410904
*
* See regex.h for more details
*
* This is free and unencumbered software released into the public domain.
* http://unlicense.org/
*/
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <assert.h>
#define CASE_INSENSITIVE 1
#define GREEDY 2
/* Special escape char in matches */
#define ESCAPE_CHAR '\\'
/* Character to be used to place the matchin part of the
* text in the substituted text in the rx_sub() and rx_gsub() functions
*/
#define GSUB_SUBMATCH '&'
/* Character to be used to escape GSUB_SUBMATCH in
* the rx_sub() and rx_gsub() functions
*/
#define GSUB_ESCAPE '/'
/* This structure is carried through the internals to carry
* match information and other parameters
*/
struct mstruct
{
char flags;
const char *start; /* Start of matching text */
const char *end; /* End of matching text */
};
/* Prototypes for static functions used internally: */
static int re_match (const char *text, const char *re, struct mstruct *m);
static int expr (const char *text, const char *re, struct mstruct *m);
static int star (const char *text, const char *s, const char *re,
struct mstruct *m);
static int plus (const char *text, const char *s, const char *re,
struct mstruct *m);
static int ques (const char *text, const char *s, const char *re,
struct mstruct *m);
static int atom (const char *text, const char *re, struct mstruct *m);
/*
* Matches text against the regular expression re.
* It will return non-zero if text matches the pattern re, otherwise it
* returns 0.
*/
int
rx_match (const char *text, const char *re)
{
struct mstruct m;
m.flags = 0;
return re_match (text, re, &m);
}
/*
* Matches text against the regular expression re and extracts the position
* of the matching text.
* If the text matches the pattern re, the pointers pointed to by beg and end
* will be set to point to the begining and end of the matching substring in
* text.
*/
int
rx_search (const char *text, const char *re, const char **beg,
const char **end)
{
struct mstruct m;
m.flags = GREEDY;
m.start = 0;
m.end = 0;
if (re_match (text, re, &m))
{
if (beg)
*beg = m.start;
if (end)
*end = m.end;
return 1;
}
if (beg)
*beg = 0;
if (end)
*end = 0;
return 0;
}
/*
* Internal function that does all the hard work.
* It matches the '^' anchor and calls the other matching functions.
*/
static int
re_match (const char *text, const char *re, struct mstruct *m)
{
if (re[0] == '^')
{
m->start = text;
return expr (text, re + 1, m);
}
else
do
{
m->start = text;
if (expr (text, re, m))
return 1;
}
while ((text++)[0] != '\0');
return 0;
}
/*
* Matches the first part of text against the first part of re
*/
static int
expr (const char *text, const char *re, struct mstruct *m)
{
int i = 0;
if (re[0] == '\0')
{
m->end = text;
return 1;
}
if (re[0] == '$' && re[1] == '\0')
{
m->end = text;
return text[0] == '\0';
}
if (re[0] == ESCAPE_CHAR)
{
if (re[1] == 'i')
{
m->flags |= CASE_INSENSITIVE;
return expr (text, re + 2, m);
}
else if (re[1] == 'I')
{
m->flags &= ~CASE_INSENSITIVE;
return expr (text, re + 2, m);
}
i++;
}
else if (re[0] == '[' && ++i)
while (re[i] != ']')
{
if (re[i] == '\0')
return 0; /* syntax error */
if (re[i] == ESCAPE_CHAR)
i++;
i++;
}
if (re[i + 1] == '*')
return star (text, re, re + i + 2, m);
if (re[i + 1] == '+')
return plus (text, re, re + i + 2, m);
if (re[i + 1] == '?')
return ques (text, re, re + i + 2, m);
if (text[0] && atom (text, re, m))
return expr (text + 1, re + i + 1, m);
return 0;
}
/*
* Matches the '*' operator in a regular expression
*/
static int
star (const char *text, const char *s, const char *re, struct mstruct *m)
{
if (m->flags & GREEDY)
{
const char *t;
for (t = text; t[0] && atom (t, s, m); t++);
do
{
if (expr (t, re, m))
return 1;
}
while (t-- > text);
}
else
{
do
{
if (expr (text, re, m))
return 1;
}
while (text[0] != '\0' && atom (text++, s, m));
}
return 0;
}
/*
* Matches the '+' operator in a regular expression
*/
static int
plus (const char *text, const char *s, const char *re, struct mstruct *m)
{
if (m->flags & GREEDY)
{
const char *t;
for (t = text; t[0] && atom (t, s, m); t++);
while (t > text)
{
if (expr (t, re, m))
return 1;
t--;
}
}
else
{
while (text[0] != '\0' && atom (text++, s, m))
if (expr (text, re, m))
return 1;
}
return 0;
}
/*
* Matches the '?' operator in a regular expression
*/
static int
ques (const char *text, const char *s, const char *re, struct mstruct *m)
{
if (m->flags & GREEDY)
{
if (text[0] != '\0' && atom (text, s, m))
if (expr (text + 1, re, m))
return 1;
if (expr (text, re, m))
return 1;
}
else
{
if (expr (text, re, m))
return 1;
if (text[0] != '\0' && atom (text++, s, m))
if (expr (text, re, m))
return 1;
}
return 0;
}
/*
* Matches the smallest elements in the regular expression: the character
* classes, the individual characters and the '.' operator
*/
static int
atom (const char *text, const char *re, struct mstruct *m)
{
const char *set;
if (re[0] == '[')
{
/* Character class */
int f = 0;
if (re[1] == '!')
re++;
for (set = re + 1; set[0] != ']'; set++)
{
if (set[0] == '\0')
return 0; /* syntax error */
if (set[0] == ESCAPE_CHAR)
set++;
else if (set[1] == '-')
{
if (set[2] == '\0')
return 0; /* syntax error */
else if(set[2] == ']') /* As in [abcde-] */
goto check1;
if (m->flags & CASE_INSENSITIVE)
{
if (tolower (text[0]) >= tolower (set[0]) &&
tolower (text[0]) <= tolower (set[2]))
f = 1;
}
else if (text[0] >= set[0] && text[0] <= set[2])
f = 1;
set += 2;
continue;
}
check1:
if (m->flags & CASE_INSENSITIVE)
{
if (tolower (set[0]) == tolower (text[0]))
f = 1;
}
else if (set[0] == text[0])
f = 1;
}
return (re[0] == '!') ? (!f) : (f);
}
else if (re[0] == ESCAPE_CHAR)
{
/* This implementation use ESCAPE_CHAR followed by a character to denote
* special character classes: \a for alphabetic, \d for digits, etc.
* So '\a' is the same as '[[:alpha:]]' in other (POSIX)
* implementations, for example.
*/
switch (re[1])
{
case 'a':
return isalpha (text[0]);
case 'w':
return isalnum (text[0]);
case 'd':
return isdigit (text[0]);
case 'u':
return isupper (text[0]);
case 'l':
return islower (text[0]);
case 'x':
return isxdigit (text[0]);
case 's':
return isspace (text[0]);
case 'A':
return !isalpha (text[0]);
case 'W':
return !isalnum (text[0]);
case 'D':
return !isdigit (text[0]);
case 'U':
return !isupper (text[0]);
case 'L':
return !islower (text[0]);
case 'X':
return !isxdigit (text[0]);
case 'S':
return !isspace (text[0]);
default:
re++;
break;
}
}
/* Compare an actual character or a '.' */
if (m->flags & CASE_INSENSITIVE)
return tolower (re[0]) == tolower (text[0]) || re[0] == '.';
return re[0] == text[0] || re[0] == '.';
}
#define RESIZE(q, o, len) do{\
if(q - o >= (int)len) \
{\
char *t;\
len += (len>2)?len >> 1:2; \
t = realloc(o, len + 1);\
if(!t) \
{\
free(o);\
return NULL; \
}\
q = t + (q - o);\
o = t;\
}\
} while(0)
/* Substitutes the first part of 'text' that matches 're' with 'sub'.
* Using a '&' in sub will replace that part of 'sub' with the part of
* Text that matched the
* For example, match_sub("#foooo#", "fo+", "|&|") will return "#|foooo|#"
* use a '/' to escape the '&' (eg '/&') and use a '//' to have a single
* '/' (I chose '/' to avoid C's use of the '\' causing confusion)
* For example match_sub("#foooo#", "fo+", "// /&") will return "#/ &#"
* It returns the result that should be free()'d afterwards.
* It may return NULL on a malloc() failure
*/
static char *
int_match_sub (const char *text, const char *re, const char *sub, char once)
{
const char *beg, *end, *p = text, *r, *u;
char *o, *q;
size_t len;
if (!rx_search (text, re, &beg, &end))
{
char *a;
size_t len = strlen (text);
a = malloc (len + 1);
if (!a)
return NULL;
memcpy (a, text, len + 1);
return a;
}
assert (beg <= end);
len = strlen (text);
o = malloc (len + 1);
if (!o)
return NULL;
q = o;
do
{
for (; p < beg && p[0]; p++, q++)
{
RESIZE (q, o, len);
q[0] = p[0];
}
for (r = sub; r[0]; r++, q++)
{
RESIZE (q, o, len); /* resize o? */
if (r[0] == GSUB_ESCAPE)
{
r++;
q[0] = r[0];
}
else if (r[0] == GSUB_SUBMATCH)
{
for (u = beg; u < end; u++, q++)
{
RESIZE (q, o, len); /* resize o? */
q[0] = u[0];
}
q--;
}
else
q[0] = r[0];
}
p = end;
}
while (rx_search (end, re, &beg, &end) && !once);
for (; p[0]; p++, q++)
{
RESIZE (q, o, len); /* resize o? */
q[0] = p[0];
}
q[0] = '\0';
/* If this fails something's wrong with the RESIZE above */
assert (strlen (o) <= len);
return o;
}
char *
rx_sub (const char *text, const char *re, const char *sub)
{
return int_match_sub (text, re, sub, 1);
}
char *
rx_gsub (const char *text, const char *re, const char *sub)
{
return int_match_sub (text, re, sub, 0);
}