summaryrefslogtreecommitdiff
path: root/src/regress/lib/libc/regex
diff options
context:
space:
mode:
Diffstat (limited to 'src/regress/lib/libc/regex')
-rw-r--r--src/regress/lib/libc/regex/Makefile20
-rw-r--r--src/regress/lib/libc/regex/debug.c245
-rw-r--r--src/regress/lib/libc/regex/debug.ih17
-rw-r--r--src/regress/lib/libc/regex/main.c515
-rw-r--r--src/regress/lib/libc/regex/main.ih22
-rw-r--r--src/regress/lib/libc/regex/split.c319
-rw-r--r--src/regress/lib/libc/regex/tests478
7 files changed, 1616 insertions, 0 deletions
diff --git a/src/regress/lib/libc/regex/Makefile b/src/regress/lib/libc/regex/Makefile
new file mode 100644
index 0000000000..a29686bca4
--- /dev/null
+++ b/src/regress/lib/libc/regex/Makefile
@@ -0,0 +1,20 @@
1# $OpenBSD: Makefile,v 1.5 2002/09/02 20:01:43 avsm Exp $
2# $NetBSD: Makefile,v 1.2 1995/02/16 19:38:45 cgd Exp $
3
4PROG= re
5SRCS= main.c split.c debug.c
6
7CFLAGS+= -I${.CURDIR}/../../../../lib/libc/regex
8
9TESTS= ${.CURDIR}/tests
10
11REGRESS_TARGETS=do-reg do-reg-long do-reg-backref
12
13do-reg: ${PROG}
14 ./re < ${TESTS}
15do-reg-long: ${PROG}
16 ./re -el < ${TESTS}
17do-reg-backref: ${PROG}
18 ./re -er < ${TESTS}
19
20.include <bsd.regress.mk>
diff --git a/src/regress/lib/libc/regex/debug.c b/src/regress/lib/libc/regex/debug.c
new file mode 100644
index 0000000000..7834e970c7
--- /dev/null
+++ b/src/regress/lib/libc/regex/debug.c
@@ -0,0 +1,245 @@
1/* $OpenBSD: debug.c,v 1.3 2001/01/29 02:05:43 niklas Exp $ */
2/* $NetBSD: debug.c,v 1.2 1995/04/20 22:39:42 cgd Exp $ */
3
4#include <stdio.h>
5#include <string.h>
6#include <ctype.h>
7#include <limits.h>
8#include <stdlib.h>
9#include <sys/types.h>
10#include <regex.h>
11
12#include "utils.h"
13#include "regex2.h"
14#include "debug.ih"
15
16/*
17 - regprint - print a regexp for debugging
18 == void regprint(regex_t *r, FILE *d);
19 */
20void
21regprint(r, d)
22regex_t *r;
23FILE *d;
24{
25 register struct re_guts *g = r->re_g;
26 register int i;
27 register int c;
28 register int last;
29 int nincat[NC];
30
31 fprintf(d, "%ld states, %d categories", (long)g->nstates,
32 g->ncategories);
33 fprintf(d, ", first %ld last %ld", (long)g->firststate,
34 (long)g->laststate);
35 if (g->iflags&USEBOL)
36 fprintf(d, ", USEBOL");
37 if (g->iflags&USEEOL)
38 fprintf(d, ", USEEOL");
39 if (g->iflags&BAD)
40 fprintf(d, ", BAD");
41 if (g->nsub > 0)
42 fprintf(d, ", nsub=%ld", (long)g->nsub);
43 if (g->must != NULL)
44 fprintf(d, ", must(%ld) `%*s'", (long)g->mlen, (int)g->mlen,
45 g->must);
46 if (g->backrefs)
47 fprintf(d, ", backrefs");
48 if (g->nplus > 0)
49 fprintf(d, ", nplus %ld", (long)g->nplus);
50 fprintf(d, "\n");
51 s_print(g, d);
52 for (i = 0; i < g->ncategories; i++) {
53 nincat[i] = 0;
54 for (c = CHAR_MIN; c <= CHAR_MAX; c++)
55 if (g->categories[c] == i)
56 nincat[i]++;
57 }
58 fprintf(d, "cc0#%d", nincat[0]);
59 for (i = 1; i < g->ncategories; i++)
60 if (nincat[i] == 1) {
61 for (c = CHAR_MIN; c <= CHAR_MAX; c++)
62 if (g->categories[c] == i)
63 break;
64 fprintf(d, ", %d=%s", i, regchar(c));
65 }
66 fprintf(d, "\n");
67 for (i = 1; i < g->ncategories; i++)
68 if (nincat[i] != 1) {
69 fprintf(d, "cc%d\t", i);
70 last = -1;
71 for (c = CHAR_MIN; c <= CHAR_MAX+1; c++) /* +1 does flush */
72 if (c <= CHAR_MAX && g->categories[c] == i) {
73 if (last < 0) {
74 fprintf(d, "%s", regchar(c));
75 last = c;
76 }
77 } else {
78 if (last >= 0) {
79 if (last != c-1)
80 fprintf(d, "-%s",
81 regchar(c-1));
82 last = -1;
83 }
84 }
85 fprintf(d, "\n");
86 }
87}
88
89/*
90 - s_print - print the strip for debugging
91 == static void s_print(register struct re_guts *g, FILE *d);
92 */
93static void
94s_print(g, d)
95register struct re_guts *g;
96FILE *d;
97{
98 register sop *s;
99 register cset *cs;
100 register int i;
101 register int done = 0;
102 register sop opnd;
103 register int col = 0;
104 register int last;
105 register sopno offset = 2;
106# define GAP() { if (offset % 5 == 0) { \
107 if (col > 40) { \
108 fprintf(d, "\n\t"); \
109 col = 0; \
110 } else { \
111 fprintf(d, " "); \
112 col++; \
113 } \
114 } else \
115 col++; \
116 offset++; \
117 }
118
119 if (OP(g->strip[0]) != OEND)
120 fprintf(d, "missing initial OEND!\n");
121 for (s = &g->strip[1]; !done; s++) {
122 opnd = OPND(*s);
123 switch (OP(*s)) {
124 case OEND:
125 fprintf(d, "\n");
126 done = 1;
127 break;
128 case OCHAR:
129 if (strchr("\\|()^$.[+*?{}!<> ", (char)opnd) != NULL)
130 fprintf(d, "\\%c", (char)opnd);
131 else
132 fprintf(d, "%s", regchar((char)opnd));
133 break;
134 case OBOL:
135 fprintf(d, "^");
136 break;
137 case OEOL:
138 fprintf(d, "$");
139 break;
140 case OBOW:
141 fprintf(d, "\\{");
142 break;
143 case OEOW:
144 fprintf(d, "\\}");
145 break;
146 case OANY:
147 fprintf(d, ".");
148 break;
149 case OANYOF:
150 fprintf(d, "[(%ld)", (long)opnd);
151 cs = &g->sets[opnd];
152 last = -1;
153 for (i = 0; i < g->csetsize+1; i++) /* +1 flushes */
154 if (CHIN(cs, i) && i < g->csetsize) {
155 if (last < 0) {
156 fprintf(d, "%s", regchar(i));
157 last = i;
158 }
159 } else {
160 if (last >= 0) {
161 if (last != i-1)
162 fprintf(d, "-%s",
163 regchar(i-1));
164 last = -1;
165 }
166 }
167 fprintf(d, "]");
168 break;
169 case OBACK_:
170 fprintf(d, "(\\<%ld>", (long)opnd);
171 break;
172 case O_BACK:
173 fprintf(d, "<%ld>\\)", (long)opnd);
174 break;
175 case OPLUS_:
176 fprintf(d, "(+");
177 if (OP(*(s+opnd)) != O_PLUS)
178 fprintf(d, "<%ld>", (long)opnd);
179 break;
180 case O_PLUS:
181 if (OP(*(s-opnd)) != OPLUS_)
182 fprintf(d, "<%ld>", (long)opnd);
183 fprintf(d, "+)");
184 break;
185 case OQUEST_:
186 fprintf(d, "(?");
187 if (OP(*(s+opnd)) != O_QUEST)
188 fprintf(d, "<%ld>", (long)opnd);
189 break;
190 case O_QUEST:
191 if (OP(*(s-opnd)) != OQUEST_)
192 fprintf(d, "<%ld>", (long)opnd);
193 fprintf(d, "?)");
194 break;
195 case OLPAREN:
196 fprintf(d, "((<%ld>", (long)opnd);
197 break;
198 case ORPAREN:
199 fprintf(d, "<%ld>))", (long)opnd);
200 break;
201 case OCH_:
202 fprintf(d, "<");
203 if (OP(*(s+opnd)) != OOR2)
204 fprintf(d, "<%ld>", (long)opnd);
205 break;
206 case OOR1:
207 if (OP(*(s-opnd)) != OOR1 && OP(*(s-opnd)) != OCH_)
208 fprintf(d, "<%ld>", (long)opnd);
209 fprintf(d, "|");
210 break;
211 case OOR2:
212 fprintf(d, "|");
213 if (OP(*(s+opnd)) != OOR2 && OP(*(s+opnd)) != O_CH)
214 fprintf(d, "<%ld>", (long)opnd);
215 break;
216 case O_CH:
217 if (OP(*(s-opnd)) != OOR1)
218 fprintf(d, "<%ld>", (long)opnd);
219 fprintf(d, ">");
220 break;
221 default:
222 fprintf(d, "!%ld(%ld)!", (long)OP(*s), (long)opnd);
223 break;
224 }
225 if (!done)
226 GAP();
227 }
228}
229
230/*
231 - regchar - make a character printable
232 == static char *regchar(int ch);
233 */
234static char * /* -> representation */
235regchar(ch)
236int ch;
237{
238 static char buf[10];
239
240 if (isprint(ch) || ch == ' ')
241 sprintf(buf, "%c", ch);
242 else
243 sprintf(buf, "\\%o", ch);
244 return(buf);
245}
diff --git a/src/regress/lib/libc/regex/debug.ih b/src/regress/lib/libc/regex/debug.ih
new file mode 100644
index 0000000000..9eb313af23
--- /dev/null
+++ b/src/regress/lib/libc/regex/debug.ih
@@ -0,0 +1,17 @@
1/* $OpenBSD: debug.ih,v 1.3 2002/02/16 21:27:32 millert Exp $ */
2/* $NetBSD: debug.ih,v 1.2 1995/04/20 22:39:47 cgd Exp $ */
3
4/* ========= begin header generated by ./mkh ========= */
5#ifdef __cplusplus
6extern "C" {
7#endif
8
9/* === debug.c === */
10void regprint(regex_t *r, FILE *d);
11static void s_print(register struct re_guts *g, FILE *d);
12static char *regchar(int ch);
13
14#ifdef __cplusplus
15}
16#endif
17/* ========= end header generated by ./mkh ========= */
diff --git a/src/regress/lib/libc/regex/main.c b/src/regress/lib/libc/regex/main.c
new file mode 100644
index 0000000000..6e63ffc235
--- /dev/null
+++ b/src/regress/lib/libc/regex/main.c
@@ -0,0 +1,515 @@
1/* $OpenBSD: main.c,v 1.3 1997/01/15 23:41:07 millert Exp $ */
2/* $NetBSD: main.c,v 1.2 1995/04/20 22:39:51 cgd Exp $ */
3
4#include <stdio.h>
5#include <stdlib.h>
6#include <string.h>
7#include <sys/types.h>
8#include <regex.h>
9#include <assert.h>
10#include <unistd.h>
11
12#include "main.ih"
13
14char *progname;
15int debug = 0;
16int line = 0;
17int status = 0;
18
19int copts = REG_EXTENDED;
20int eopts = 0;
21regoff_t startoff = 0;
22regoff_t endoff = 0;
23
24
25extern int split();
26extern void regprint();
27
28/*
29 - main - do the simple case, hand off to regress() for regression
30 */
31int
32main(argc, argv)
33int argc;
34char *argv[];
35{
36 regex_t re;
37# define NS 10
38 regmatch_t subs[NS];
39 char erbuf[100];
40 int err;
41 size_t len;
42 int c;
43 int errflg = 0;
44 register int i;
45 extern int optind;
46 extern char *optarg;
47
48 progname = argv[0];
49
50 while ((c = getopt(argc, argv, "c:e:S:E:x")) != -1)
51 switch (c) {
52 case 'c': /* compile options */
53 copts = options('c', optarg);
54 break;
55 case 'e': /* execute options */
56 eopts = options('e', optarg);
57 break;
58 case 'S': /* start offset */
59 startoff = (regoff_t)atoi(optarg);
60 break;
61 case 'E': /* end offset */
62 endoff = (regoff_t)atoi(optarg);
63 break;
64 case 'x': /* Debugging. */
65 debug++;
66 break;
67 case '?':
68 default:
69 errflg++;
70 break;
71 }
72 if (errflg) {
73 fprintf(stderr, "usage: %s ", progname);
74 fprintf(stderr, "[-c copt][-C][-d] [re]\n");
75 exit(2);
76 }
77
78 if (optind >= argc) {
79 regress(stdin);
80 exit(status);
81 }
82
83 err = regcomp(&re, argv[optind++], copts);
84 if (err) {
85 len = regerror(err, &re, erbuf, sizeof(erbuf));
86 fprintf(stderr, "error %s, %d/%d `%s'\n",
87 eprint(err), len, sizeof(erbuf), erbuf);
88 exit(status);
89 }
90 regprint(&re, stdout);
91
92 if (optind >= argc) {
93 regfree(&re);
94 exit(status);
95 }
96
97 if (eopts&REG_STARTEND) {
98 subs[0].rm_so = startoff;
99 subs[0].rm_eo = strlen(argv[optind]) - endoff;
100 }
101 err = regexec(&re, argv[optind], (size_t)NS, subs, eopts);
102 if (err) {
103 len = regerror(err, &re, erbuf, sizeof(erbuf));
104 fprintf(stderr, "error %s, %d/%d `%s'\n",
105 eprint(err), len, sizeof(erbuf), erbuf);
106 exit(status);
107 }
108 if (!(copts&REG_NOSUB)) {
109 len = (size_t)(subs[0].rm_eo - subs[0].rm_so);
110 if (subs[0].rm_so != -1) {
111 if (len != 0)
112 printf("match `%.*s'\n", (int)len,
113 argv[optind] + subs[0].rm_so);
114 else
115 printf("match `'@%.1s\n",
116 argv[optind] + subs[0].rm_so);
117 }
118 for (i = 1; i < NS; i++)
119 if (subs[i].rm_so != -1)
120 printf("(%d) `%.*s'\n", i,
121 (int)(subs[i].rm_eo - subs[i].rm_so),
122 argv[optind] + subs[i].rm_so);
123 }
124 exit(status);
125}
126
127/*
128 - regress - main loop of regression test
129 == void regress(FILE *in);
130 */
131void
132regress(in)
133FILE *in;
134{
135 char inbuf[1000];
136# define MAXF 10
137 char *f[MAXF];
138 int nf;
139 int i;
140 char erbuf[100];
141 size_t ne;
142 char *badpat = "invalid regular expression";
143# define SHORT 10
144 char *bpname = "REG_BADPAT";
145 regex_t re;
146
147 while (fgets(inbuf, sizeof(inbuf), in) != NULL) {
148 line++;
149 if (inbuf[0] == '#' || inbuf[0] == '\n')
150 continue; /* NOTE CONTINUE */
151 inbuf[strlen(inbuf)-1] = '\0'; /* get rid of stupid \n */
152 if (debug)
153 fprintf(stdout, "%d:\n", line);
154 nf = split(inbuf, f, MAXF, "\t\t");
155 if (nf < 3) {
156 fprintf(stderr, "bad input, line %d\n", line);
157 exit(1);
158 }
159 for (i = 0; i < nf; i++)
160 if (strcmp(f[i], "\"\"") == 0)
161 f[i] = "";
162 if (nf <= 3)
163 f[3] = NULL;
164 if (nf <= 4)
165 f[4] = NULL;
166 try(f[0], f[1], f[2], f[3], f[4], options('c', f[1]));
167 if (opt('&', f[1])) /* try with either type of RE */
168 try(f[0], f[1], f[2], f[3], f[4],
169 options('c', f[1]) &~ REG_EXTENDED);
170 }
171
172 ne = regerror(REG_BADPAT, (regex_t *)NULL, erbuf, sizeof(erbuf));
173 if (strcmp(erbuf, badpat) != 0 || ne != strlen(badpat)+1) {
174 fprintf(stderr, "end: regerror() test gave `%s' not `%s'\n",
175 erbuf, badpat);
176 status = 1;
177 }
178 ne = regerror(REG_BADPAT, (regex_t *)NULL, erbuf, (size_t)SHORT);
179 if (strncmp(erbuf, badpat, SHORT-1) != 0 || erbuf[SHORT-1] != '\0' ||
180 ne != strlen(badpat)+1) {
181 fprintf(stderr, "end: regerror() short test gave `%s' not `%.*s'\n",
182 erbuf, SHORT-1, badpat);
183 status = 1;
184 }
185 ne = regerror(REG_ITOA|REG_BADPAT, (regex_t *)NULL, erbuf, sizeof(erbuf));
186 if (strcmp(erbuf, bpname) != 0 || ne != strlen(bpname)+1) {
187 fprintf(stderr, "end: regerror() ITOA test gave `%s' not `%s'\n",
188 erbuf, bpname);
189 status = 1;
190 }
191 re.re_endp = bpname;
192 ne = regerror(REG_ATOI, &re, erbuf, sizeof(erbuf));
193 if (atoi(erbuf) != (int)REG_BADPAT) {
194 fprintf(stderr, "end: regerror() ATOI test gave `%s' not `%ld'\n",
195 erbuf, (long)REG_BADPAT);
196 status = 1;
197 } else if (ne != strlen(erbuf)+1) {
198 fprintf(stderr, "end: regerror() ATOI test len(`%s') = %ld\n",
199 erbuf, (long)REG_BADPAT);
200 status = 1;
201 }
202}
203
204/*
205 - try - try it, and report on problems
206 == void try(char *f0, char *f1, char *f2, char *f3, char *f4, int opts);
207 */
208void
209try(f0, f1, f2, f3, f4, opts)
210char *f0;
211char *f1;
212char *f2;
213char *f3;
214char *f4;
215int opts; /* may not match f1 */
216{
217 regex_t re;
218# define NSUBS 10
219 regmatch_t subs[NSUBS];
220# define NSHOULD 15
221 char *should[NSHOULD];
222 int nshould;
223 char erbuf[100];
224 int err;
225 int len;
226 char *type = (opts & REG_EXTENDED) ? "ERE" : "BRE";
227 register int i;
228 char *grump;
229 char f0copy[1000];
230 char f2copy[1000];
231
232 strcpy(f0copy, f0);
233 re.re_endp = (opts&REG_PEND) ? f0copy + strlen(f0copy) : NULL;
234 fixstr(f0copy);
235 err = regcomp(&re, f0copy, opts);
236 if (err != 0 && (!opt('C', f1) || err != efind(f2))) {
237 /* unexpected error or wrong error */
238 len = regerror(err, &re, erbuf, sizeof(erbuf));
239 fprintf(stderr, "%d: %s error %s, %d/%d `%s'\n",
240 line, type, eprint(err), len,
241 sizeof(erbuf), erbuf);
242 status = 1;
243 } else if (err == 0 && opt('C', f1)) {
244 /* unexpected success */
245 fprintf(stderr, "%d: %s should have given REG_%s\n",
246 line, type, f2);
247 status = 1;
248 err = 1; /* so we won't try regexec */
249 }
250
251 if (err != 0) {
252 regfree(&re);
253 return;
254 }
255
256 strcpy(f2copy, f2);
257 fixstr(f2copy);
258
259 if (options('e', f1)&REG_STARTEND) {
260 if (strchr(f2, '(') == NULL || strchr(f2, ')') == NULL)
261 fprintf(stderr, "%d: bad STARTEND syntax\n", line);
262 subs[0].rm_so = strchr(f2, '(') - f2 + 1;
263 subs[0].rm_eo = strchr(f2, ')') - f2;
264 }
265 err = regexec(&re, f2copy, NSUBS, subs, options('e', f1));
266
267 if (err != 0 && (f3 != NULL || err != REG_NOMATCH)) {
268 /* unexpected error or wrong error */
269 len = regerror(err, &re, erbuf, sizeof(erbuf));
270 fprintf(stderr, "%d: %s exec error %s, %d/%d `%s'\n",
271 line, type, eprint(err), len,
272 sizeof(erbuf), erbuf);
273 status = 1;
274 } else if (err != 0) {
275 /* nothing more to check */
276 } else if (f3 == NULL) {
277 /* unexpected success */
278 fprintf(stderr, "%d: %s exec should have failed\n",
279 line, type);
280 status = 1;
281 err = 1; /* just on principle */
282 } else if (opts&REG_NOSUB) {
283 /* nothing more to check */
284 } else if ((grump = check(f2, subs[0], f3)) != NULL) {
285 fprintf(stderr, "%d: %s %s\n", line, type, grump);
286 status = 1;
287 err = 1;
288 }
289
290 if (err != 0 || f4 == NULL) {
291 regfree(&re);
292 return;
293 }
294
295 for (i = 1; i < NSHOULD; i++)
296 should[i] = NULL;
297 nshould = split(f4, should+1, NSHOULD-1, ",");
298 if (nshould == 0) {
299 nshould = 1;
300 should[1] = "";
301 }
302 for (i = 1; i < NSUBS; i++) {
303 grump = check(f2, subs[i], should[i]);
304 if (grump != NULL) {
305 fprintf(stderr, "%d: %s $%d %s\n", line,
306 type, i, grump);
307 status = 1;
308 err = 1;
309 }
310 }
311
312 regfree(&re);
313}
314
315/*
316 - options - pick options out of a regression-test string
317 == int options(int type, char *s);
318 */
319int
320options(type, s)
321int type; /* 'c' compile, 'e' exec */
322char *s;
323{
324 register char *p;
325 register int o = (type == 'c') ? copts : eopts;
326 register char *legal = (type == 'c') ? "bisnmp" : "^$#tl";
327
328 for (p = s; *p != '\0'; p++)
329 if (strchr(legal, *p) != NULL)
330 switch (*p) {
331 case 'b':
332 o &= ~REG_EXTENDED;
333 break;
334 case 'i':
335 o |= REG_ICASE;
336 break;
337 case 's':
338 o |= REG_NOSUB;
339 break;
340 case 'n':
341 o |= REG_NEWLINE;
342 break;
343 case 'm':
344 o &= ~REG_EXTENDED;
345 o |= REG_NOSPEC;
346 break;
347 case 'p':
348 o |= REG_PEND;
349 break;
350 case '^':
351 o |= REG_NOTBOL;
352 break;
353 case '$':
354 o |= REG_NOTEOL;
355 break;
356 case '#':
357 o |= REG_STARTEND;
358 break;
359 case 't': /* trace */
360 o |= REG_TRACE;
361 break;
362 case 'l': /* force long representation */
363 o |= REG_LARGE;
364 break;
365 case 'r': /* force backref use */
366 o |= REG_BACKR;
367 break;
368 }
369 return(o);
370}
371
372/*
373 - opt - is a particular option in a regression string?
374 == int opt(int c, char *s);
375 */
376int /* predicate */
377opt(c, s)
378int c;
379char *s;
380{
381 return(strchr(s, c) != NULL);
382}
383
384/*
385 - fixstr - transform magic characters in strings
386 == void fixstr(register char *p);
387 */
388void
389fixstr(p)
390register char *p;
391{
392 if (p == NULL)
393 return;
394
395 for (; *p != '\0'; p++)
396 if (*p == 'N')
397 *p = '\n';
398 else if (*p == 'T')
399 *p = '\t';
400 else if (*p == 'S')
401 *p = ' ';
402 else if (*p == 'Z')
403 *p = '\0';
404}
405
406/*
407 - check - check a substring match
408 == char *check(char *str, regmatch_t sub, char *should);
409 */
410char * /* NULL or complaint */
411check(str, sub, should)
412char *str;
413regmatch_t sub;
414char *should;
415{
416 register int len;
417 register int shlen;
418 register char *p;
419 static char grump[500];
420 register char *at = NULL;
421
422 if (should != NULL && strcmp(should, "-") == 0)
423 should = NULL;
424 if (should != NULL && should[0] == '@') {
425 at = should + 1;
426 should = "";
427 }
428
429 /* check rm_so and rm_eo for consistency */
430 if (sub.rm_so > sub.rm_eo || (sub.rm_so == -1 && sub.rm_eo != -1) ||
431 (sub.rm_so != -1 && sub.rm_eo == -1) ||
432 (sub.rm_so != -1 && sub.rm_so < 0) ||
433 (sub.rm_eo != -1 && sub.rm_eo < 0) ) {
434 sprintf(grump, "start %ld end %ld", (long)sub.rm_so,
435 (long)sub.rm_eo);
436 return(grump);
437 }
438
439 /* check for no match */
440 if (sub.rm_so == -1 && should == NULL)
441 return(NULL);
442 if (sub.rm_so == -1)
443 return("did not match");
444
445 /* check for in range */
446 if (sub.rm_eo > strlen(str)) {
447 sprintf(grump, "start %ld end %ld, past end of string",
448 (long)sub.rm_so, (long)sub.rm_eo);
449 return(grump);
450 }
451
452 len = (int)(sub.rm_eo - sub.rm_so);
453 shlen = (int)strlen(should);
454 p = str + sub.rm_so;
455
456 /* check for not supposed to match */
457 if (should == NULL) {
458 sprintf(grump, "matched `%.*s'", len, p);
459 return(grump);
460 }
461
462 /* check for wrong match */
463 if (len != shlen || strncmp(p, should, (size_t)shlen) != 0) {
464 sprintf(grump, "matched `%.*s' instead", len, p);
465 return(grump);
466 }
467 if (shlen > 0)
468 return(NULL);
469
470 /* check null match in right place */
471 if (at == NULL)
472 return(NULL);
473 shlen = strlen(at);
474 if (shlen == 0)
475 shlen = 1; /* force check for end-of-string */
476 if (strncmp(p, at, shlen) != 0) {
477 sprintf(grump, "matched null at `%.20s'", p);
478 return(grump);
479 }
480 return(NULL);
481}
482
483/*
484 - eprint - convert error number to name
485 == static char *eprint(int err);
486 */
487static char *
488eprint(err)
489int err;
490{
491 static char epbuf[100];
492 size_t len;
493
494 len = regerror(REG_ITOA|err, (regex_t *)NULL, epbuf, sizeof(epbuf));
495 assert(len <= sizeof(epbuf));
496 return(epbuf);
497}
498
499/*
500 - efind - convert error name to number
501 == static int efind(char *name);
502 */
503static int
504efind(name)
505char *name;
506{
507 static char efbuf[100];
508 regex_t re;
509
510 sprintf(efbuf, "REG_%s", name);
511 assert(strlen(efbuf) < sizeof(efbuf));
512 re.re_endp = efbuf;
513 (void) regerror(REG_ATOI, &re, efbuf, sizeof(efbuf));
514 return(atoi(efbuf));
515}
diff --git a/src/regress/lib/libc/regex/main.ih b/src/regress/lib/libc/regex/main.ih
new file mode 100644
index 0000000000..0860e26333
--- /dev/null
+++ b/src/regress/lib/libc/regex/main.ih
@@ -0,0 +1,22 @@
1/* $OpenBSD: main.ih,v 1.3 2002/02/16 21:27:32 millert Exp $ */
2/* $NetBSD: main.ih,v 1.2 1995/04/20 22:39:55 cgd Exp $ */
3
4/* ========= begin header generated by ./mkh ========= */
5#ifdef __cplusplus
6extern "C" {
7#endif
8
9/* === main.c === */
10void regress(FILE *in);
11void try(char *f0, char *f1, char *f2, char *f3, char *f4, int opts);
12int options(int type, char *s);
13int opt(int c, char *s);
14void fixstr(register char *p);
15char *check(char *str, regmatch_t sub, char *should);
16static char *eprint(int err);
17static int efind(char *name);
18
19#ifdef __cplusplus
20}
21#endif
22/* ========= end header generated by ./mkh ========= */
diff --git a/src/regress/lib/libc/regex/split.c b/src/regress/lib/libc/regex/split.c
new file mode 100644
index 0000000000..52cb6c681e
--- /dev/null
+++ b/src/regress/lib/libc/regex/split.c
@@ -0,0 +1,319 @@
1/* $OpenBSD: split.c,v 1.2 2001/01/29 02:05:44 niklas Exp $ */
2/* $NetBSD: split.c,v 1.2 1995/04/20 22:39:57 cgd Exp $ */
3
4#include <stdio.h>
5#include <string.h>
6
7/*
8 - split - divide a string into fields, like awk split()
9 = int split(char *string, char *fields[], int nfields, char *sep);
10 */
11int /* number of fields, including overflow */
12split(string, fields, nfields, sep)
13char *string;
14char *fields[]; /* list is not NULL-terminated */
15int nfields; /* number of entries available in fields[] */
16char *sep; /* "" white, "c" single char, "ab" [ab]+ */
17{
18 register char *p = string;
19 register char c; /* latest character */
20 register char sepc = sep[0];
21 register char sepc2;
22 register int fn;
23 register char **fp = fields;
24 register char *sepp;
25 register int trimtrail;
26
27 /* white space */
28 if (sepc == '\0') {
29 while ((c = *p++) == ' ' || c == '\t')
30 continue;
31 p--;
32 trimtrail = 1;
33 sep = " \t"; /* note, code below knows this is 2 long */
34 sepc = ' ';
35 } else
36 trimtrail = 0;
37 sepc2 = sep[1]; /* now we can safely pick this up */
38
39 /* catch empties */
40 if (*p == '\0')
41 return(0);
42
43 /* single separator */
44 if (sepc2 == '\0') {
45 fn = nfields;
46 for (;;) {
47 *fp++ = p;
48 fn--;
49 if (fn == 0)
50 break;
51 while ((c = *p++) != sepc)
52 if (c == '\0')
53 return(nfields - fn);
54 *(p-1) = '\0';
55 }
56 /* we have overflowed the fields vector -- just count them */
57 fn = nfields;
58 for (;;) {
59 while ((c = *p++) != sepc)
60 if (c == '\0')
61 return(fn);
62 fn++;
63 }
64 /* not reached */
65 }
66
67 /* two separators */
68 if (sep[2] == '\0') {
69 fn = nfields;
70 for (;;) {
71 *fp++ = p;
72 fn--;
73 while ((c = *p++) != sepc && c != sepc2)
74 if (c == '\0') {
75 if (trimtrail && **(fp-1) == '\0')
76 fn++;
77 return(nfields - fn);
78 }
79 if (fn == 0)
80 break;
81 *(p-1) = '\0';
82 while ((c = *p++) == sepc || c == sepc2)
83 continue;
84 p--;
85 }
86 /* we have overflowed the fields vector -- just count them */
87 fn = nfields;
88 while (c != '\0') {
89 while ((c = *p++) == sepc || c == sepc2)
90 continue;
91 p--;
92 fn++;
93 while ((c = *p++) != '\0' && c != sepc && c != sepc2)
94 continue;
95 }
96 /* might have to trim trailing white space */
97 if (trimtrail) {
98 p--;
99 while ((c = *--p) == sepc || c == sepc2)
100 continue;
101 p++;
102 if (*p != '\0') {
103 if (fn == nfields+1)
104 *p = '\0';
105 fn--;
106 }
107 }
108 return(fn);
109 }
110
111 /* n separators */
112 fn = 0;
113 for (;;) {
114 if (fn < nfields)
115 *fp++ = p;
116 fn++;
117 for (;;) {
118 c = *p++;
119 if (c == '\0')
120 return(fn);
121 sepp = sep;
122 while ((sepc = *sepp++) != '\0' && sepc != c)
123 continue;
124 if (sepc != '\0') /* it was a separator */
125 break;
126 }
127 if (fn < nfields)
128 *(p-1) = '\0';
129 for (;;) {
130 c = *p++;
131 sepp = sep;
132 while ((sepc = *sepp++) != '\0' && sepc != c)
133 continue;
134 if (sepc == '\0') /* it wasn't a separator */
135 break;
136 }
137 p--;
138 }
139
140 /* not reached */
141}
142
143#ifdef TEST_SPLIT
144
145
146/*
147 * test program
148 * pgm runs regression
149 * pgm sep splits stdin lines by sep
150 * pgm str sep splits str by sep
151 * pgm str sep n splits str by sep n times
152 */
153int
154main(argc, argv)
155int argc;
156char *argv[];
157{
158 char buf[512];
159 register int n;
160# define MNF 10
161 char *fields[MNF];
162
163 if (argc > 4)
164 for (n = atoi(argv[3]); n > 0; n--) {
165 (void) strcpy(buf, argv[1]);
166 }
167 else if (argc > 3)
168 for (n = atoi(argv[3]); n > 0; n--) {
169 (void) strcpy(buf, argv[1]);
170 (void) split(buf, fields, MNF, argv[2]);
171 }
172 else if (argc > 2)
173 dosplit(argv[1], argv[2]);
174 else if (argc > 1)
175 while (fgets(buf, sizeof(buf), stdin) != NULL) {
176 buf[strlen(buf)-1] = '\0'; /* stomp newline */
177 dosplit(buf, argv[1]);
178 }
179 else
180 regress();
181
182 exit(0);
183}
184
185dosplit(string, seps)
186char *string;
187char *seps;
188{
189# define NF 5
190 char *fields[NF];
191 register int nf;
192
193 nf = split(string, fields, NF, seps);
194 print(nf, NF, fields);
195}
196
197print(nf, nfp, fields)
198int nf;
199int nfp;
200char *fields[];
201{
202 register int fn;
203 register int bound;
204
205 bound = (nf > nfp) ? nfp : nf;
206 printf("%d:\t", nf);
207 for (fn = 0; fn < bound; fn++)
208 printf("\"%s\"%s", fields[fn], (fn+1 < nf) ? ", " : "\n");
209}
210
211#define RNF 5 /* some table entries know this */
212struct {
213 char *str;
214 char *seps;
215 int nf;
216 char *fi[RNF];
217} tests[] = {
218 "", " ", 0, { "" },
219 " ", " ", 2, { "", "" },
220 "x", " ", 1, { "x" },
221 "xy", " ", 1, { "xy" },
222 "x y", " ", 2, { "x", "y" },
223 "abc def g ", " ", 5, { "abc", "def", "", "g", "" },
224 " a bcd", " ", 4, { "", "", "a", "bcd" },
225 "a b c d e f", " ", 6, { "a", "b", "c", "d", "e f" },
226 " a b c d ", " ", 6, { "", "a", "b", "c", "d " },
227
228 "", " _", 0, { "" },
229 " ", " _", 2, { "", "" },
230 "x", " _", 1, { "x" },
231 "x y", " _", 2, { "x", "y" },
232 "ab _ cd", " _", 2, { "ab", "cd" },
233 " a_b c ", " _", 5, { "", "a", "b", "c", "" },
234 "a b c_d e f", " _", 6, { "a", "b", "c", "d", "e f" },
235 " a b c d ", " _", 6, { "", "a", "b", "c", "d " },
236
237 "", " _~", 0, { "" },
238 " ", " _~", 2, { "", "" },
239 "x", " _~", 1, { "x" },
240 "x y", " _~", 2, { "x", "y" },
241 "ab _~ cd", " _~", 2, { "ab", "cd" },
242 " a_b c~", " _~", 5, { "", "a", "b", "c", "" },
243 "a b_c d~e f", " _~", 6, { "a", "b", "c", "d", "e f" },
244 "~a b c d ", " _~", 6, { "", "a", "b", "c", "d " },
245
246 "", " _~-", 0, { "" },
247 " ", " _~-", 2, { "", "" },
248 "x", " _~-", 1, { "x" },
249 "x y", " _~-", 2, { "x", "y" },
250 "ab _~- cd", " _~-", 2, { "ab", "cd" },
251 " a_b c~", " _~-", 5, { "", "a", "b", "c", "" },
252 "a b_c-d~e f", " _~-", 6, { "a", "b", "c", "d", "e f" },
253 "~a-b c d ", " _~-", 6, { "", "a", "b", "c", "d " },
254
255 "", " ", 0, { "" },
256 " ", " ", 2, { "", "" },
257 "x", " ", 1, { "x" },
258 "xy", " ", 1, { "xy" },
259 "x y", " ", 2, { "x", "y" },
260 "abc def g ", " ", 4, { "abc", "def", "g", "" },
261 " a bcd", " ", 3, { "", "a", "bcd" },
262 "a b c d e f", " ", 6, { "a", "b", "c", "d", "e f" },
263 " a b c d ", " ", 6, { "", "a", "b", "c", "d " },
264
265 "", "", 0, { "" },
266 " ", "", 0, { "" },
267 "x", "", 1, { "x" },
268 "xy", "", 1, { "xy" },
269 "x y", "", 2, { "x", "y" },
270 "abc def g ", "", 3, { "abc", "def", "g" },
271 "\t a bcd", "", 2, { "a", "bcd" },
272 " a \tb\t c ", "", 3, { "a", "b", "c" },
273 "a b c d e ", "", 5, { "a", "b", "c", "d", "e" },
274 "a b\tc d e f", "", 6, { "a", "b", "c", "d", "e f" },
275 " a b c d e f ", "", 6, { "a", "b", "c", "d", "e f " },
276
277 NULL, NULL, 0, { NULL },
278};
279
280regress()
281{
282 char buf[512];
283 register int n;
284 char *fields[RNF+1];
285 register int nf;
286 register int i;
287 register int printit;
288 register char *f;
289
290 for (n = 0; tests[n].str != NULL; n++) {
291 (void) strcpy(buf, tests[n].str);
292 fields[RNF] = NULL;
293 nf = split(buf, fields, RNF, tests[n].seps);
294 printit = 0;
295 if (nf != tests[n].nf) {
296 printf("split `%s' by `%s' gave %d fields, not %d\n",
297 tests[n].str, tests[n].seps, nf, tests[n].nf);
298 printit = 1;
299 } else if (fields[RNF] != NULL) {
300 printf("split() went beyond array end\n");
301 printit = 1;
302 } else {
303 for (i = 0; i < nf && i < RNF; i++) {
304 f = fields[i];
305 if (f == NULL)
306 f = "(NULL)";
307 if (strcmp(f, tests[n].fi[i]) != 0) {
308 printf("split `%s' by `%s', field %d is `%s', not `%s'\n",
309 tests[n].str, tests[n].seps,
310 i, fields[i], tests[n].fi[i]);
311 printit = 1;
312 }
313 }
314 }
315 if (printit)
316 print(nf, RNF, fields);
317 }
318}
319#endif
diff --git a/src/regress/lib/libc/regex/tests b/src/regress/lib/libc/regex/tests
new file mode 100644
index 0000000000..c89b9ec164
--- /dev/null
+++ b/src/regress/lib/libc/regex/tests
@@ -0,0 +1,478 @@
1# $OpenBSD: tests,v 1.2 2001/01/29 02:05:44 niklas Exp $
2# $NetBSD: tests,v 1.5 1995/04/20 22:40:00 cgd Exp $
3
4# regular expression test set
5# Lines are at least three fields, separated by one or more tabs. "" stands
6# for an empty field. First field is an RE. Second field is flags. If
7# C flag given, regcomp() is expected to fail, and the third field is the
8# error name (minus the leading REG_).
9#
10# Otherwise it is expected to succeed, and the third field is the string to
11# try matching it against. If there is no fourth field, the match is
12# expected to fail. If there is a fourth field, it is the substring that
13# the RE is expected to match. If there is a fifth field, it is a comma-
14# separated list of what the subexpressions should match, with - indicating
15# no match for that one. In both the fourth and fifth fields, a (sub)field
16# starting with @ indicates that the (sub)expression is expected to match
17# a null string followed by the stuff after the @; this provides a way to
18# test where null strings match. The character `N' in REs and strings
19# is newline, `S' is space, `T' is tab, `Z' is NUL.
20#
21# The full list of flags:
22# - placeholder, does nothing
23# b RE is a BRE, not an ERE
24# & try it as both an ERE and a BRE
25# C regcomp() error expected, third field is error name
26# i REG_ICASE
27# m ("mundane") REG_NOSPEC
28# s REG_NOSUB (not really testable)
29# n REG_NEWLINE
30# ^ REG_NOTBOL
31# $ REG_NOTEOL
32# # REG_STARTEND (see below)
33# p REG_PEND
34#
35# For REG_STARTEND, the start/end offsets are those of the substring
36# enclosed in ().
37
38# basics
39a & a a
40abc & abc abc
41abc|de - abc abc
42a|b|c - abc a
43
44# parentheses and perversions thereof
45a(b)c - abc abc
46a\(b\)c b abc abc
47a( C EPAREN
48a( b a( a(
49a\( - a( a(
50a\( bC EPAREN
51a\(b bC EPAREN
52a(b C EPAREN
53a(b b a(b a(b
54# gag me with a right parenthesis -- 1003.2 goofed here (my fault, partly)
55a) - a) a)
56) - ) )
57# end gagging (in a just world, those *should* give EPAREN)
58a) b a) a)
59a\) bC EPAREN
60\) bC EPAREN
61a()b - ab ab
62a\(\)b b ab ab
63
64# anchoring and REG_NEWLINE
65^abc$ & abc abc
66a^b - a^b
67a^b b a^b a^b
68a$b - a$b
69a$b b a$b a$b
70^ & abc @abc
71$ & abc @
72^$ & "" @
73$^ - "" @
74\($\)\(^\) b "" @
75# stop retching, those are legitimate (although disgusting)
76^^ - "" @
77$$ - "" @
78b$ & abNc
79b$ &n abNc b
80^b$ & aNbNc
81^b$ &n aNbNc b
82^$ &n aNNb @Nb
83^$ n abc
84^$ n abcN @
85$^ n aNNb @Nb
86\($\)\(^\) bn aNNb @Nb
87^^ n^ aNNb @Nb
88$$ n aNNb @NN
89^a ^ a
90a$ $ a
91^a ^n aNb
92^b ^n aNb b
93a$ $n bNa
94b$ $n bNa b
95a*(^b$)c* - b b
96a*\(^b$\)c* b b b
97
98# certain syntax errors and non-errors
99| C EMPTY
100| b | |
101* C BADRPT
102* b * *
103+ C BADRPT
104? C BADRPT
105"" &C EMPTY
106() - abc @abc
107\(\) b abc @abc
108a||b C EMPTY
109|ab C EMPTY
110ab| C EMPTY
111(|a)b C EMPTY
112(a|)b C EMPTY
113(*a) C BADRPT
114(+a) C BADRPT
115(?a) C BADRPT
116({1}a) C BADRPT
117\(\{1\}a\) bC BADRPT
118(a|*b) C BADRPT
119(a|+b) C BADRPT
120(a|?b) C BADRPT
121(a|{1}b) C BADRPT
122^* C BADRPT
123^* b * *
124^+ C BADRPT
125^? C BADRPT
126^{1} C BADRPT
127^\{1\} bC BADRPT
128
129# metacharacters, backslashes
130a.c & abc abc
131a[bc]d & abd abd
132a\*c & a*c a*c
133a\\b & a\b a\b
134a\\\*b & a\*b a\*b
135a\bc & abc abc
136a\ &C EESCAPE
137a\\bc & a\bc a\bc
138\{ bC BADRPT
139a\[b & a[b a[b
140a[b &C EBRACK
141# trailing $ is a peculiar special case for the BRE code
142a$ & a a
143a$ & a$
144a\$ & a
145a\$ & a$ a$
146a\\$ & a
147a\\$ & a$
148a\\$ & a\$
149a\\$ & a\ a\
150
151# back references, ugh
152a\(b\)\2c bC ESUBREG
153a\(b\1\)c bC ESUBREG
154a\(b*\)c\1d b abbcbbd abbcbbd bb
155a\(b*\)c\1d b abbcbd
156a\(b*\)c\1d b abbcbbbd
157^\(.\)\1 b abc
158a\([bc]\)\1d b abcdabbd abbd b
159a\(\([bc]\)\2\)*d b abbccd abbccd
160a\(\([bc]\)\2\)*d b abbcbd
161# actually, this next one probably ought to fail, but the spec is unclear
162a\(\(b\)*\2\)*d b abbbd abbbd
163# here is a case that no NFA implementation does right
164\(ab*\)[ab]*\1 b ababaaa ababaaa a
165# check out normal matching in the presence of back refs
166\(a\)\1bcd b aabcd aabcd
167\(a\)\1bc*d b aabcd aabcd
168\(a\)\1bc*d b aabd aabd
169\(a\)\1bc*d b aabcccd aabcccd
170\(a\)\1bc*[ce]d b aabcccd aabcccd
171^\(a\)\1b\(c\)*cd$ b aabcccd aabcccd
172
173# ordinary repetitions
174ab*c & abc abc
175ab+c - abc abc
176ab?c - abc abc
177a\(*\)b b a*b a*b
178a\(**\)b b ab ab
179a\(***\)b bC BADRPT
180*a b *a *a
181**a b a a
182***a bC BADRPT
183
184# the dreaded bounded repetitions
185{ & { {
186{abc & {abc {abc
187{1 C BADRPT
188{1} C BADRPT
189a{b & a{b a{b
190a{1}b - ab ab
191a\{1\}b b ab ab
192a{1,}b - ab ab
193a\{1,\}b b ab ab
194a{1,2}b - aab aab
195a\{1,2\}b b aab aab
196a{1 C EBRACE
197a\{1 bC EBRACE
198a{1a C EBRACE
199a\{1a bC EBRACE
200a{1a} C BADBR
201a\{1a\} bC BADBR
202a{,2} - a{,2} a{,2}
203a\{,2\} bC BADBR
204a{,} - a{,} a{,}
205a\{,\} bC BADBR
206a{1,x} C BADBR
207a\{1,x\} bC BADBR
208a{1,x C EBRACE
209a\{1,x bC EBRACE
210a{300} C BADBR
211a\{300\} bC BADBR
212a{1,0} C BADBR
213a\{1,0\} bC BADBR
214ab{0,0}c - abcac ac
215ab\{0,0\}c b abcac ac
216ab{0,1}c - abcac abc
217ab\{0,1\}c b abcac abc
218ab{0,3}c - abbcac abbc
219ab\{0,3\}c b abbcac abbc
220ab{1,1}c - acabc abc
221ab\{1,1\}c b acabc abc
222ab{1,3}c - acabc abc
223ab\{1,3\}c b acabc abc
224ab{2,2}c - abcabbc abbc
225ab\{2,2\}c b abcabbc abbc
226ab{2,4}c - abcabbc abbc
227ab\{2,4\}c b abcabbc abbc
228((a{1,10}){1,10}){1,10} - a a a,a
229
230# multiple repetitions
231a** &C BADRPT
232a++ C BADRPT
233a?? C BADRPT
234a*+ C BADRPT
235a*? C BADRPT
236a+* C BADRPT
237a+? C BADRPT
238a?* C BADRPT
239a?+ C BADRPT
240a{1}{1} C BADRPT
241a*{1} C BADRPT
242a+{1} C BADRPT
243a?{1} C BADRPT
244a{1}* C BADRPT
245a{1}+ C BADRPT
246a{1}? C BADRPT
247a*{b} - a{b} a{b}
248a\{1\}\{1\} bC BADRPT
249a*\{1\} bC BADRPT
250a\{1\}* bC BADRPT
251
252# brackets, and numerous perversions thereof
253a[b]c & abc abc
254a[ab]c & abc abc
255a[^ab]c & adc adc
256a[]b]c & a]c a]c
257a[[b]c & a[c a[c
258a[-b]c & a-c a-c
259a[^]b]c & adc adc
260a[^-b]c & adc adc
261a[b-]c & a-c a-c
262a[b &C EBRACK
263a[] &C EBRACK
264a[1-3]c & a2c a2c
265a[3-1]c &C ERANGE
266a[1-3-5]c &C ERANGE
267a[[.-.]--]c & a-c a-c
268a[1- &C ERANGE
269a[[. &C EBRACK
270a[[.x &C EBRACK
271a[[.x. &C EBRACK
272a[[.x.] &C EBRACK
273a[[.x.]] & ax ax
274a[[.x,.]] &C ECOLLATE
275a[[.one.]]b & a1b a1b
276a[[.notdef.]]b &C ECOLLATE
277a[[.].]]b & a]b a]b
278a[[:alpha:]]c & abc abc
279a[[:notdef:]]c &C ECTYPE
280a[[: &C EBRACK
281a[[:alpha &C EBRACK
282a[[:alpha:] &C EBRACK
283a[[:alpha,:] &C ECTYPE
284a[[:]:]]b &C ECTYPE
285a[[:-:]]b &C ECTYPE
286a[[:alph:]] &C ECTYPE
287a[[:alphabet:]] &C ECTYPE
288[[:alnum:]]+ - -%@a0X- a0X
289[[:alpha:]]+ - -%@aX0- aX
290[[:blank:]]+ - aSSTb SST
291[[:cntrl:]]+ - aNTb NT
292[[:digit:]]+ - a019b 019
293[[:graph:]]+ - Sa%bS a%b
294[[:lower:]]+ - AabC ab
295[[:print:]]+ - NaSbN aSb
296[[:punct:]]+ - S%-&T %-&
297[[:space:]]+ - aSNTb SNT
298[[:upper:]]+ - aBCd BC
299[[:xdigit:]]+ - p0f3Cq 0f3C
300a[[=b=]]c & abc abc
301a[[= &C EBRACK
302a[[=b &C EBRACK
303a[[=b= &C EBRACK
304a[[=b=] &C EBRACK
305a[[=b,=]] &C ECOLLATE
306a[[=one=]]b & a1b a1b
307
308# complexities
309a(((b)))c - abc abc
310a(b|(c))d - abd abd
311a(b*|c)d - abbd abbd
312# just gotta have one DFA-buster, of course
313a[ab]{20} - aaaaabaaaabaaaabaaaab aaaaabaaaabaaaabaaaab
314# and an inline expansion in case somebody gets tricky
315a[ab][ab][ab][ab][ab][ab][ab][ab][ab][ab][ab][ab][ab][ab][ab][ab][ab][ab][ab][ab] - aaaaabaaaabaaaabaaaab aaaaabaaaabaaaabaaaab
316# and in case somebody just slips in an NFA...
317a[ab][ab][ab][ab][ab][ab][ab][ab][ab][ab][ab][ab][ab][ab][ab][ab][ab][ab][ab][ab](wee|week)(knights|night) - aaaaabaaaabaaaabaaaabweeknights aaaaabaaaabaaaabaaaabweeknights
318# fish for anomalies as the number of states passes 32
31912345678901234567890123456789 - a12345678901234567890123456789b 12345678901234567890123456789
320123456789012345678901234567890 - a123456789012345678901234567890b 123456789012345678901234567890
3211234567890123456789012345678901 - a1234567890123456789012345678901b 1234567890123456789012345678901
32212345678901234567890123456789012 - a12345678901234567890123456789012b 12345678901234567890123456789012
323123456789012345678901234567890123 - a123456789012345678901234567890123b 123456789012345678901234567890123
324# and one really big one, beyond any plausible word width
3251234567890123456789012345678901234567890123456789012345678901234567890 - a1234567890123456789012345678901234567890123456789012345678901234567890b 1234567890123456789012345678901234567890123456789012345678901234567890
326# fish for problems as brackets go past 8
327[ab][cd][ef][gh][ij][kl][mn] - xacegikmoq acegikm
328[ab][cd][ef][gh][ij][kl][mn][op] - xacegikmoq acegikmo
329[ab][cd][ef][gh][ij][kl][mn][op][qr] - xacegikmoqy acegikmoq
330[ab][cd][ef][gh][ij][kl][mn][op][q] - xacegikmoqy acegikmoq
331
332# subtleties of matching
333abc & xabcy abc
334a\(b\)?c\1d b acd
335aBc i Abc Abc
336a[Bc]*d i abBCcd abBCcd
3370[[:upper:]]1 &i 0a1 0a1
3380[[:lower:]]1 &i 0A1 0A1
339a[^b]c &i abc
340a[^b]c &i aBc
341a[^b]c &i adc adc
342[a]b[c] - abc abc
343[a]b[a] - aba aba
344[abc]b[abc] - abc abc
345[abc]b[abd] - abd abd
346a(b?c)+d - accd accd
347(wee|week)(knights|night) - weeknights weeknights
348(we|wee|week|frob)(knights|night|day) - weeknights weeknights
349a[bc]d - xyzaaabcaababdacd abd
350a[ab]c - aaabc abc
351abc s abc abc
352a* & b @b
353
354# Let's have some fun -- try to match a C comment.
355# first the obvious, which looks okay at first glance...
356/\*.*\*/ - /*x*/ /*x*/
357# but...
358/\*.*\*/ - /*x*/y/*z*/ /*x*/y/*z*/
359# okay, we must not match */ inside; try to do that...
360/\*([^*]|\*[^/])*\*/ - /*x*/ /*x*/
361/\*([^*]|\*[^/])*\*/ - /*x*/y/*z*/ /*x*/
362# but...
363/\*([^*]|\*[^/])*\*/ - /*x**/y/*z*/ /*x**/y/*z*/
364# and a still fancier version, which does it right (I think)...
365/\*([^*]|\*+[^*/])*\*+/ - /*x*/ /*x*/
366/\*([^*]|\*+[^*/])*\*+/ - /*x*/y/*z*/ /*x*/
367/\*([^*]|\*+[^*/])*\*+/ - /*x**/y/*z*/ /*x**/
368/\*([^*]|\*+[^*/])*\*+/ - /*x****/y/*z*/ /*x****/
369/\*([^*]|\*+[^*/])*\*+/ - /*x**x*/y/*z*/ /*x**x*/
370/\*([^*]|\*+[^*/])*\*+/ - /*x***x/y/*z*/ /*x***x/y/*z*/
371
372# subexpressions
373a(b)(c)d - abcd abcd b,c
374a(((b)))c - abc abc b,b,b
375a(b|(c))d - abd abd b,-
376a(b*|c|e)d - abbd abbd bb
377a(b*|c|e)d - acd acd c
378a(b*|c|e)d - ad ad @d
379a(b?)c - abc abc b
380a(b?)c - ac ac @c
381a(b+)c - abc abc b
382a(b+)c - abbbc abbbc bbb
383a(b*)c - ac ac @c
384(a|ab)(bc([de]+)f|cde) - abcdef abcdef a,bcdef,de
385# the regression tester only asks for 9 subexpressions
386a(b)(c)(d)(e)(f)(g)(h)(i)(j)k - abcdefghijk abcdefghijk b,c,d,e,f,g,h,i,j
387a(b)(c)(d)(e)(f)(g)(h)(i)(j)(k)l - abcdefghijkl abcdefghijkl b,c,d,e,f,g,h,i,j,k
388a([bc]?)c - abc abc b
389a([bc]?)c - ac ac @c
390a([bc]+)c - abc abc b
391a([bc]+)c - abcc abcc bc
392a([bc]+)bc - abcbc abcbc bc
393a(bb+|b)b - abb abb b
394a(bbb+|bb+|b)b - abb abb b
395a(bbb+|bb+|b)b - abbb abbb bb
396a(bbb+|bb+|b)bb - abbb abbb b
397(.*).* - abcdef abcdef abcdef
398(a*)* - bc @b @b
399
400# do we get the right subexpression when it is used more than once?
401a(b|c)*d - ad ad -
402a(b|c)*d - abcd abcd c
403a(b|c)+d - abd abd b
404a(b|c)+d - abcd abcd c
405a(b|c?)+d - ad ad @d
406a(b|c?)+d - abcd abcd @d
407a(b|c){0,0}d - ad ad -
408a(b|c){0,1}d - ad ad -
409a(b|c){0,1}d - abd abd b
410a(b|c){0,2}d - ad ad -
411a(b|c){0,2}d - abcd abcd c
412a(b|c){0,}d - ad ad -
413a(b|c){0,}d - abcd abcd c
414a(b|c){1,1}d - abd abd b
415a(b|c){1,1}d - acd acd c
416a(b|c){1,2}d - abd abd b
417a(b|c){1,2}d - abcd abcd c
418a(b|c){1,}d - abd abd b
419a(b|c){1,}d - abcd abcd c
420a(b|c){2,2}d - acbd acbd b
421a(b|c){2,2}d - abcd abcd c
422a(b|c){2,4}d - abcd abcd c
423a(b|c){2,4}d - abcbd abcbd b
424a(b|c){2,4}d - abcbcd abcbcd c
425a(b|c){2,}d - abcd abcd c
426a(b|c){2,}d - abcbd abcbd b
427a(b+|((c)*))+d - abd abd @d,@d,-
428a(b+|((c)*))+d - abcd abcd @d,@d,-
429
430# check out the STARTEND option
431[abc] &# a(b)c b
432[abc] &# a(d)c
433[abc] &# a(bc)d b
434[abc] &# a(dc)d c
435. &# a()c
436b.*c &# b(bc)c bc
437b.* &# b(bc)c bc
438.*c &# b(bc)c bc
439
440# plain strings, with the NOSPEC flag
441abc m abc abc
442abc m xabcy abc
443abc m xyz
444a*b m aba*b a*b
445a*b m ab
446"" mC EMPTY
447
448# cases involving NULs
449aZb & a a
450aZb &p a
451aZb &p# (aZb) aZb
452aZ*b &p# (ab) ab
453a.b &# (aZb) aZb
454a.* &# (aZb)c aZb
455
456# word boundaries (ick)
457[[:<:]]a & a a
458[[:<:]]a & ba
459[[:<:]]a & -a a
460a[[:>:]] & a a
461a[[:>:]] & ab
462a[[:>:]] & a- a
463[[:<:]]a.c[[:>:]] & axcd-dayc-dazce-abc abc
464[[:<:]]a.c[[:>:]] & axcd-dayc-dazce-abc-q abc
465[[:<:]]a.c[[:>:]] & axc-dayc-dazce-abc axc
466[[:<:]]b.c[[:>:]] & a_bxc-byc_d-bzc-q bzc
467[[:<:]].x..[[:>:]] & y_xa_-_xb_y-_xc_-axdc _xc_
468[[:<:]]a_b[[:>:]] & x_a_b
469
470# past problems, and suspected problems
471(A[1])|(A[2])|(A[3])|(A[4])|(A[5])|(A[6])|(A[7])|(A[8])|(A[9])|(A[A]) - A1 A1
472abcdefghijklmnop i abcdefghijklmnop abcdefghijklmnop
473abcdefghijklmnopqrstuv i abcdefghijklmnopqrstuv abcdefghijklmnopqrstuv
474(ALAK)|(ALT[AB])|(CC[123]1)|(CM[123]1)|(GAMC)|(LC[23][EO ])|(SEM[1234])|(SL[ES][12])|(SLWW)|(SLF )|(SLDT)|(VWH[12])|(WH[34][EW])|(WP1[ESN]) - CC11 CC11
475CC[13]1|a{21}[23][EO][123][Es][12]a{15}aa[34][EW]aaaaaaa[X]a - CC11 CC11
476Char \([a-z0-9_]*\)\[.* b Char xyz[k Char xyz[k xyz
477a?b - ab ab
478-\{0,1\}[0-9]*$ b -5 -5