4 * The matching engine and friends. This file is #included by regexec.c
5 * after suitable #defines of a variety of macros used herein, so that
6 * different state representations can be used without duplicating masses
11 #define matcher smatcher
14 #define dissect sdissect
15 #define backref sbackref
22 #define matcher lmatcher
25 #define dissect ldissect
26 #define backref lbackref
33 /* another structure passed up and down to avoid zillions of parameters */
37 regmatch_t *pmatch; /* [nsub+1] (0 element unused) */
38 const char *offp; /* offsets work from here */
39 const char *beginp; /* start of string -- virtual NUL precedes */
40 const char *endp; /* end of string -- virtual NUL here */
41 const char *coldp; /* can be no match starting before here */
42 const char **lastpos; /* [nplus+1] */
44 states st; /* current states */
45 states fresh; /* states for a fresh start */
46 states tmp; /* temporary */
47 states empty; /* empty set of states */
53 #define SP(t, s, c) print(m, t, s, c, stdout)
54 #define AT(t, p1, p2, s1, s2) at(m, t, p1, p2, s1, s2)
55 #define NOTE(str) { if (m->eflags®_TRACE) printf("=%s\n", (str)); }
57 #define SP(t, s, c) /* nothing */
58 #define AT(t, p1, p2, s1, s2) /* nothing */
59 #define NOTE(s) /* nothing */
63 - matcher - the actual matching engine
64 == static int matcher(register struct re_guts *g, const char *string, \
65 == size_t nmatch, regmatch_t pmatch[], int eflags);
67 static int /* 0 success, REG_NOMATCH failure */
75 register const char *endp;
78 register struct match *m = &mv;
79 register const char *dp;
80 const sopno gf = g->firststate+1; /* +1 for OEND */
81 const sopno gl = g->laststate;
85 /* simplify the situation where possible */
86 if (g->cflags®_NOSUB)
88 if (eflags®_STARTEND) {
89 start = string + (int)pmatch[0].rm_so;
90 stop = string + (int)pmatch[0].rm_eo;
93 stop = start + strlen(start);
98 /* prescreening; this does wonders for this rather slow code */
99 if (g->must != NULL) {
100 for (dp = start; dp < stop; dp++) {
101 if ((*dp == g->must[0]) &&
102 ((sopno)(stop - dp) >= g->mlen) &&
103 (memcmp(dp, g->must, (size_t)g->mlen) == 0)) {
107 if (dp == stop) /* we didn't find g->must */
111 /* match struct setup */
112 memset(m, 0, SIZEOF(*m));
126 /* this loop does only one repetition except for backrefs */
128 endp = fast(m, start, stop, gf, gl);
129 if ((endp == NULL) || (m->coldp == NULL)) { /* a miss */
134 if (nmatch == 0 && !g->backrefs)
135 break; /* no further info needed */
138 NOTE("finding start");
139 endp = slow(m, m->coldp, stop, gf, gl);
142 assert(m->coldp < m->endp);
145 if (nmatch == 1 && !g->backrefs)
146 break; /* no further info needed */
148 /* oh my, he wants the subexpressions... */
149 if (m->pmatch == NULL)
150 m->pmatch = (regmatch_t *)alloc((m->g->nsub + 1) *
152 if (m->pmatch == NULL) {
156 for (i = 1; i <= m->g->nsub; i++)
157 m->pmatch[i].rm_so = m->pmatch[i].rm_eo = (regoff_t)-1;
158 if (!g->backrefs && !(m->eflags®_BACKR)) {
160 dp = dissect(m, m->coldp, endp, gf, gl);
162 if (g->nplus > 0 && m->lastpos == NULL) {
163 m->lastpos = (const char **)
164 alloc(((size_t)g->nplus+1) *
165 SIZEOF(const char *));
167 if (g->nplus > 0 && m->lastpos == NULL) {
172 NOTE("backref dissect");
173 dp = backref(m, m->coldp, endp, gf, gl, (sopno)0);
178 /* uh-oh... we couldn't find a subexpression-level match */
179 assert(g->backrefs); /* must be back references doing it */
180 assert(g->nplus == 0 || m->lastpos != NULL);
182 if (dp != NULL || endp <= m->coldp)
185 endp = slow(m, m->coldp, endp-1, gf, gl);
188 /* try it on a shorter possibility */
190 for (i = 1; i <= m->g->nsub; i++) {
191 assert(m->pmatch[i].rm_so == -1);
192 assert(m->pmatch[i].rm_eo == -1);
195 NOTE("backoff dissect");
196 dp = backref(m, m->coldp, endp, gf, gl, (sopno)0);
198 assert(dp == NULL || dp == endp);
199 if (dp != NULL) /* found a shorter one */
202 /* despite initial appearances, there is no match here */
204 start = m->coldp + 1; /* recycle starting later */
205 assert(start <= stop);
208 /* fill in the details if requested */
211 pmatch[0].rm_so = (regoff_t)(m->coldp - m->offp);
212 pmatch[0].rm_eo = (regoff_t)(endp - m->offp);
216 assert(m->pmatch != NULL);
217 for (i = 1; i < nmatch; i++)
219 pmatch[i] = m->pmatch[i];
221 pmatch[i].rm_so = (regoff_t)-1;
222 pmatch[i].rm_eo = (regoff_t)-1;
226 if (m->pmatch != NULL)
227 free((void *)m->pmatch);
228 if (m->lastpos != NULL)
229 free((void *)m->lastpos);
235 - dissect - figure out what matched what, no back references
236 == static const char *dissect(register struct match *m, const char *start, \
237 == const char *stop, sopno startst, sopno stopst);
239 static const char * /* == stop (success) always */
248 register sopno ss; /* start sop of current subRE */
249 register sopno es; /* end sop of current subRE */
250 register const char *sp; /* start of string matched by it */
251 register const char *stp; /* string matched by it cannot pass here */
252 register const char *rest = NULL;/* start of rest of string */
253 register const char *tail; /* string unmatched by rest of RE */
254 register sopno ssub; /* start sop of subsubRE */
255 register sopno esub; /* end sop of subsubRE */
256 register const char *ssp; /* start of string matched by subsubRE */
257 register const char *sep; /* end of string matched by subsubRE */
258 register const char *oldssp; /* previous ssp */
259 register const char *dp;
261 AT("diss", start, stop, startst, stopst);
263 for (ss = startst; ss < stopst; ss = es) {
264 /* identify end of subRE */
266 switch (OP(m->g->strip[es])) {
269 es += OPND(m->g->strip[es]);
273 while (OP(m->g->strip[es]) != O_CH)
274 es += OPND(m->g->strip[es]);
279 /* figure out what it matched */
280 switch (OP(m->g->strip[ss])) {
303 /* cases where length of match is hard to find */
306 rest = slow(m, sp, stp, ss, es);
308 /* how long could this one be? */
309 assert(rest != NULL); /* it did match */
310 /* could the rest match the rest? */
311 tail = slow(m, rest, stop, es, stopst);
314 /* no -- try a shorter match for this one */
316 assert(stp >= sp); /* it did work */
317 rest = slow(m, sp, stp, ss, es);
321 /* did innards match? */
322 if (slow(m, sp, rest, ssub, esub) != NULL) {
323 dp = dissect(m, sp, rest, ssub, esub);
332 /* how long could this one be? */
333 rest = slow(m, sp, stp, ss, es);
334 assert(rest != NULL); /* it did match */
335 /* could the rest match the rest? */
336 tail = slow(m, rest, stop, es, stopst);
339 /* no -- try a shorter match for this one */
341 assert(stp >= sp); /* it did work */
347 sep = slow(m, ssp, rest, ssub, esub);
348 for (;;) { /* find last match of innards */
349 if (sep == NULL || sep == ssp)
350 break; /* failed or matched null */
351 oldssp = ssp; /* on to next try */
353 sep = slow(m, ssp, rest, ssub, esub);
356 /* last successful match */
360 assert(sep == rest); /* must exhaust substring */
361 assert(slow(m, ssp, sep, ssub, esub) == rest);
362 dp = dissect(m, ssp, sep, ssub, esub);
370 /* how long could this one be? */
371 rest = slow(m, sp, stp, ss, es);
372 assert(rest != NULL); /* it did match */
373 /* could the rest match the rest? */
374 tail = slow(m, rest, stop, es, stopst);
377 /* no -- try a shorter match for this one */
379 assert(stp >= sp); /* it did work */
382 esub = ss + OPND(m->g->strip[ss]) - 1;
383 assert(OP(m->g->strip[esub]) == OOR1);
385 for (;;) { /* find first matching branch */
386 if (slow(m, sp, rest, ssub, esub) == rest)
387 break; /* it matched all of it */
388 /* that one missed, try next one */
389 assert(OP(m->g->strip[esub]) == OOR1);
391 assert(OP(m->g->strip[esub]) == OOR2);
393 esub += OPND(m->g->strip[esub]);
394 if (OP(m->g->strip[esub]) == OOR2)
397 assert(OP(m->g->strip[esub]) == O_CH);
399 dp = dissect(m, sp, rest, ssub, esub);
413 i = (size_t)OPND(m->g->strip[ss]);
414 assert(0 < i && i <= m->g->nsub);
415 m->pmatch[i].rm_so = (regoff_t)(sp - m->offp);
419 i = (size_t)OPND(m->g->strip[ss]);
420 assert(0 < i && i <= m->g->nsub);
421 m->pmatch[i].rm_eo = (regoff_t)(sp - m->offp);
434 - backref - figure out what matched what, figuring in back references
435 == static const char *backref(register struct match *m, const char *start, \
436 == const char *stop, sopno startst, sopno stopst, sopno lev);
438 static const char * /* == stop (success) or NULL (failure) */
445 sopno lev) /* PLUS nesting level */
448 register sopno ss; /* start sop of current subRE */
449 register const char *sp;/* start of string matched by it */
450 register sopno ssub; /* start sop of subsubRE */
451 register sopno esub; /* end sop of subsubRE */
452 register const char *ssp;/* start of string matched by subsubRE */
453 register const char *dp;
457 register regoff_t offsave;
460 AT("back", start, stop, startst, stopst);
463 /* get as far as we can with easy stuff */
465 for (ss = startst; !hard && ss < stopst; ss++)
466 switch (OP(s = m->g->strip[ss])) {
468 if (sp == stop || *sp++ != (char)OPND(s))
477 cs = &m->g->sets[OPND(s)];
478 if (sp == stop || !CHIN(cs, *sp++))
482 if (!((sp == m->beginp && !(m->eflags®_NOTBOL)) ||
483 (sp < m->endp && *(sp-1) == '\n' &&
484 (m->g->cflags®_NEWLINE)))) {
489 if (!((sp == m->endp && !(m->eflags®_NOTEOL)) ||
490 (sp < m->endp && *sp == '\n' &&
491 (m->g->cflags®_NEWLINE)))) {
496 if (!(((sp == m->beginp && !(m->eflags®_NOTBOL))
497 || ((sp < m->endp) && (*(sp-1) == '\n')
498 && (m->g->cflags & REG_NEWLINE))
499 || (((sp > m->beginp) && !ISWORD(*(sp-1)))
500 && (sp < m->endp && ISWORD(*sp)))))) {
505 if (!(((sp == m->endp && !(m->eflags®_NOTEOL)) ||
506 (sp < m->endp && *sp == '\n' &&
507 (m->g->cflags®_NEWLINE)) ||
508 (sp < m->endp && !ISWORD(*sp)) ) &&
509 (sp > m->beginp && ISWORD(*(sp-1))))) {
515 case OOR1: /* matches null but needs to skip */
519 assert(OP(s) == OOR2);
521 } while (OP(s = m->g->strip[ss]) != O_CH);
522 /* note that the ss++ gets us past the O_CH */
524 default: /* have to make a choice */
528 if (!hard) { /* that was it! */
533 ss--; /* adjust for the for's final increment */
536 AT("hard", sp, stop, ss, stopst);
539 case OBACK_: /* the vilest depths */
541 assert(0 < i && i <= m->g->nsub);
542 if (m->pmatch[i].rm_eo == (regoff_t)-1)
544 assert(m->pmatch[i].rm_so != (regoff_t)-1);
545 len = (size_t)(m->pmatch[i].rm_eo - m->pmatch[i].rm_so);
546 assert((size_t)(stop - m->beginp) >= len);
548 return(NULL); /* not enough left to match */
549 ssp = m->offp + (size_t)m->pmatch[i].rm_so;
550 if (memcmp(sp, ssp, len) != 0)
552 while (m->g->strip[ss] != SOP(O_BACK, i))
554 return(backref(m, sp+len, stop, ss+1, stopst, lev));
556 case OQUEST_: /* to null or not */
557 dp = backref(m, sp, stop, ss+1, stopst, lev);
559 return(dp); /* not */
560 return(backref(m, sp, stop, ss+OPND(s)+1, stopst, lev));
563 assert(m->lastpos != NULL);
564 assert(lev+1 <= m->g->nplus);
565 m->lastpos[lev+1] = sp;
566 return(backref(m, sp, stop, ss+1, stopst, lev+1));
569 if (sp == m->lastpos[lev]) /* last pass matched null */
570 return(backref(m, sp, stop, ss+1, stopst, lev-1));
571 /* try another pass */
572 m->lastpos[lev] = sp;
573 dp = backref(m, sp, stop, ss-OPND(s)+1, stopst, lev);
575 return(backref(m, sp, stop, ss+1, stopst, lev-1));
578 case OCH_: /* find the right one, if any */
580 esub = ss + OPND(s) - 1;
581 assert(OP(m->g->strip[esub]) == OOR1);
582 for (;;) { /* find first matching branch */
583 dp = backref(m, sp, stop, ssub, esub, lev);
586 /* that one missed, try next one */
587 if (OP(m->g->strip[esub]) == O_CH)
590 assert(OP(m->g->strip[esub]) == OOR2);
592 esub += OPND(m->g->strip[esub]);
593 if (OP(m->g->strip[esub]) == OOR2)
596 assert(OP(m->g->strip[esub]) == O_CH);
598 return(NULL); /* there is none */
600 case OLPAREN: /* must undo assignment if rest fails */
602 assert(0 < i && i <= m->g->nsub);
603 offsave = m->pmatch[i].rm_so;
604 m->pmatch[i].rm_so = (regoff_t)(sp - m->offp);
605 dp = backref(m, sp, stop, ss+1, stopst, lev);
608 m->pmatch[i].rm_so = offsave;
611 case ORPAREN: /* must undo assignment if rest fails */
613 assert(0 < i && i <= m->g->nsub);
614 offsave = m->pmatch[i].rm_eo;
615 m->pmatch[i].rm_eo = (regoff_t)(sp - m->offp);
616 dp = backref(m, sp, stop, ss+1, stopst, lev);
619 m->pmatch[i].rm_eo = offsave;
625 return((const char *)NULL); /* dummy */
629 - fast - step through the string at top speed
630 == static const char *fast(register struct match *m, const char *start, \
631 == const char *stop, sopno startst, sopno stopst);
633 static const char * /* where tentative match ended, or NULL */
641 register states st = m->st;
642 register states fresh = m->fresh;
643 register states tmp = m->tmp;
644 register const char *p = start;
645 register int c = (start == m->beginp) ? OUT : *(start-1);
646 register int lastc; /* previous c */
649 register const char *coldp;/* last p after which no match was underway */
653 st = step(m->g, startst, stopst, st, NOTHING, st);
660 c = (p == m->endp) ? OUT : *p;
664 /* is there an EOL and/or BOL between lastc and c? */
667 if ( (lastc == '\n' && m->g->cflags®_NEWLINE) ||
668 (lastc == OUT && !(m->eflags®_NOTBOL)) ) {
672 if ( (c == '\n' && m->g->cflags®_NEWLINE) ||
673 (c == OUT && !(m->eflags®_NOTEOL)) ) {
674 flagch = (flagch == BOL) ? BOLEOL : EOL;
679 st = step(m->g, startst, stopst, st, flagch, st);
683 /* how about a word boundary? */
684 if ( (flagch == BOL || (lastc != OUT && !ISWORD(lastc))) &&
685 (c != OUT && ISWORD(c)) ) {
688 if ( (lastc != OUT && ISWORD(lastc)) &&
689 (flagch == EOL || (c != OUT && !ISWORD(c))) ) {
692 if (flagch == BOW || flagch == EOW) {
693 st = step(m->g, startst, stopst, st, flagch, st);
698 if (ISSET(st, stopst) || p == stop)
699 break; /* NOTE BREAK OUT */
701 /* no, we must deal with this character */
705 st = step(m->g, startst, stopst, tmp, c, st);
707 assert(EQ(step(m->g, startst, stopst, st, NOTHING, st), st));
711 assert(coldp != NULL);
713 if (ISSET(st, stopst))
720 - slow - step through the string more deliberately
721 == static const char *slow(register struct match *m, const char *start, \
722 == const char *stop, sopno startst, sopno stopst);
724 static const char * /* where it ended */
732 register states st = m->st;
733 register states empty = m->empty;
734 register states tmp = m->tmp;
735 register const char *p = start;
736 register int c = (start == m->beginp) ? OUT : *(start-1);
737 register int lastc; /* previous c */
740 register const char *matchp; /* last p at which a match ended */
742 AT("slow", start, stop, startst, stopst);
745 SP("sstart", st, *p);
746 st = step(m->g, startst, stopst, st, NOTHING, st);
751 c = (p == m->endp) ? OUT : *p;
753 /* is there an EOL and/or BOL between lastc and c? */
756 if ( (lastc == '\n' && m->g->cflags®_NEWLINE) ||
757 (lastc == OUT && !(m->eflags®_NOTBOL)) ) {
761 if ( (c == '\n' && m->g->cflags®_NEWLINE) ||
762 (c == OUT && !(m->eflags®_NOTEOL)) ) {
763 flagch = (flagch == BOL) ? BOLEOL : EOL;
768 st = step(m->g, startst, stopst, st, flagch, st);
769 SP("sboleol", st, c);
772 /* how about a word boundary? */
773 if ( (flagch == BOL || (lastc != OUT && !ISWORD(lastc))) &&
774 (c != OUT && ISWORD(c)) ) {
777 if ( (lastc != OUT && ISWORD(lastc)) &&
778 (flagch == EOL || (c != OUT && !ISWORD(c))) ) {
781 if (flagch == BOW || flagch == EOW) {
782 st = step(m->g, startst, stopst, st, flagch, st);
783 SP("sboweow", st, c);
787 if (ISSET(st, stopst))
789 if (EQ(st, empty) || p == stop)
790 break; /* NOTE BREAK OUT */
792 /* no, we must deal with this character */
796 st = step(m->g, startst, stopst, tmp, c, st);
798 assert(EQ(step(m->g, startst, stopst, st, NOTHING, st), st));
807 - step - map set of states reachable before char to set reachable after
808 == static states step(register struct re_guts *g, sopno start, sopno stop, \
809 == register states bef, int ch, register states aft);
810 == #define BOL (OUT+1)
811 == #define EOL (BOL+1)
812 == #define BOLEOL (BOL+2)
813 == #define NOTHING (BOL+3)
814 == #define BOW (BOL+4)
815 == #define EOW (BOL+5)
816 == #define CODEMAX (BOL+5) // highest code used
817 == #define NONCHAR(c) ((c) > CHAR_MAX)
818 == #define NNONCHAR (CODEMAX-CHAR_MAX)
823 sopno start, /* start state within strip */
824 sopno stop, /* state after stop state within strip */
825 register states bef, /* states reachable before */
826 int ch, /* character or NONCHAR code */
827 states aft) /* states already known reachable after */
832 register sopno here; /* note, macros know this name */
834 register unsigned long i;
836 for (pc = start, INIT(here, pc); pc != stop; pc++, INC(here)) {
840 assert(pc == stop-1);
843 /* only characters can match */
844 assert(!NONCHAR(ch) || ch != (char)OPND(s));
845 if (ch == (char)OPND(s))
849 if (ch == BOL || ch == BOLEOL)
853 if (ch == EOL || ch == BOLEOL)
869 cs = &g->sets[OPND(s)];
870 if (!NONCHAR(ch) && CHIN(cs, ch))
873 case OBACK_: /* ignored here */
877 case OPLUS_: /* forward, this is just an empty */
880 case O_PLUS: /* both forward and back */
882 i = (unsigned long)ISSETBACK(aft, OPND(s));
883 BACK(aft, aft, OPND(s));
884 if (!i && ISSETBACK(aft, OPND(s))) {
885 /* oho, must reconsider loop body */
890 case OQUEST_: /* two branches, both forward */
892 FWD(aft, aft, OPND(s));
894 case O_QUEST: /* just an empty */
897 case OLPAREN: /* not significant here */
901 case OCH_: /* mark the first two branches */
903 assert(OP(g->strip[pc+OPND(s)]) == OOR2);
904 FWD(aft, aft, OPND(s));
906 case OOR1: /* done a branch, find the O_CH */
907 if (ISSTATEIN(aft, here)) {
909 OP(s = g->strip[pc+look]) != O_CH;
911 assert(OP(s) == OOR2);
915 case OOR2: /* propagate OCH_'s marking */
917 if (OP(g->strip[pc+OPND(s)]) != O_CH) {
918 assert(OP(g->strip[pc+OPND(s)]) == OOR2);
919 FWD(aft, aft, OPND(s));
922 case O_CH: /* just empty */
925 default: /* ooooops... */
935 - print - print a set of states
937 == static void print(struct match *m, char *caption, states st, \
949 register struct re_guts *g = m->g;
951 register int first = 1;
953 if (!(m->eflags®_TRACE))
956 fprintf(d, "%s", caption);
958 fprintf(d, " %s", pchar(ch));
959 for (i = 0; i < g->nstates; i++)
961 fprintf(d, "%s%d", (first) ? "\t" : ", ", i);
968 - at - print current situation
970 == static void at(struct match *m, char *title, const char *start, \
971 == const char *stop, sopno startst, sopno stopst);
983 if (!(m->eflags®_TRACE))
986 printf("%s %s-", title, pchar(*start));
987 printf("%s ", pchar(*stop));
988 printf("%ld-%ld\n", (long)startst, (long)stopst);
992 #define PCHARDONE /* never again */
994 - pchar - make a character printable
996 == static char *pchar(int ch);
999 * Is this identical to regchar() over in debug.c? Well, yes. But a
1000 * duplicate here avoids having a debugging-capable regexec.o tied to
1001 * a matching debug.o, and this is convenient. It all disappears in
1002 * the non-debug compilation anyway, so it doesn't matter much.
1004 static char * /* -> representation */
1008 static char pbuf[10];
1010 if (isprint(ch) || ch == ' ')
1011 snprintf(pbuf, SIZEOF(pbuf), "%c", ch);
1013 snprintf(pbuf, SIZEOF(pbuf), "\\%o", ch);