1 /**************************************************************************
3 * Copyright 2013-2014 RAD Game Tools and Valve Software
4 * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 **************************************************************************/
28 * The matching engine and friends. This file is #included by regexec.c
29 * after suitable #defines of a variety of macros used herein, so that
30 * different state representations can be used without duplicating masses
35 #define matcher smatcher
38 #define dissect sdissect
39 #define backref sbackref
46 #define matcher lmatcher
49 #define dissect ldissect
50 #define backref lbackref
57 /* another structure passed up and down to avoid zillions of parameters */
62 regmatch_t *pmatch; /* [nsub+1] (0 element unused) */
63 char *offp; /* offsets work from here */
64 char *beginp; /* start of string -- virtual NUL precedes */
65 char *endp; /* end of string -- virtual NUL here */
66 char *coldp; /* can be no match starting before here */
67 char **lastpos; /* [nplus+1] */
69 states st; /* current states */
70 states fresh; /* states for a fresh start */
71 states tmp; /* temporary */
72 states empty; /* empty set of states */
78 #define SP(t, s, c) print(m, t, s, c, stdout)
79 #define AT(t, p1, p2, s1, s2) at(m, t, p1, p2, s1, s2)
82 if (m->eflags & REG_TRACE) \
83 printf("=%s\n", (str)); \
86 #define SP(t, s, c) /* nothing */
87 #define AT(t, p1, p2, s1, s2) /* nothing */
88 #define NOTE(s) /* nothing */
92 - matcher - the actual matching engine
93 == static int matcher(register struct re_guts *g, char *string, \
94 == size_t nmatch, regmatch_t pmatch[], int eflags);
96 static int /* 0 success, REG_NOMATCH failure */
97 matcher(g, string, nmatch, pmatch, eflags) register struct re_guts *g;
106 register struct match *m = &mv;
108 register const sopno gf = g->firststate + 1; /* +1 for OEND */
109 register const sopno gl = g->laststate;
113 /* simplify the situation where possible */
114 if (g->cflags & REG_NOSUB)
116 if (eflags & REG_STARTEND)
118 start = string + pmatch[0].rm_so;
119 stop = string + pmatch[0].rm_eo;
124 stop = start + strlen(start);
129 /* prescreening; this does wonders for this rather slow code */
132 for (dp = start; dp < stop; dp++)
133 if (*dp == g->must[0] && stop - dp >= g->mlen &&
134 memcmp(dp, g->must, (size_t)g->mlen) == 0)
136 if (dp == stop) /* we didn't find g->must */
137 return (REG_NOMATCH);
140 /* match struct setup */
155 /* this loop does only one repetition except for backrefs */
158 endp = fast(m, start, stop, gf, gl);
159 if (endp == NULL) /* a miss */
162 return (REG_NOMATCH);
164 if (nmatch == 0 && !g->backrefs)
165 break; /* no further info needed */
168 assert(m->coldp != NULL);
171 NOTE("finding start");
172 endp = slow(m, m->coldp, stop, gf, gl);
175 assert(m->coldp < m->endp);
178 if (nmatch == 1 && !g->backrefs)
179 break; /* no further info needed */
181 /* oh my, he wants the subexpressions... */
182 if (m->pmatch == NULL)
183 m->pmatch = (regmatch_t *)regex_malloc((m->g->nsub + 1) *
185 if (m->pmatch == NULL)
190 for (i = 1; (size_t)i <= m->g->nsub; i++)
191 m->pmatch[i].rm_so = m->pmatch[i].rm_eo = -1;
192 if (!g->backrefs && !(m->eflags & REG_BACKR))
195 dp = dissect(m, m->coldp, endp, gf, gl);
199 if (g->nplus > 0 && m->lastpos == NULL)
200 m->lastpos = (char **)regex_malloc((g->nplus + 1) *
202 if (g->nplus > 0 && m->lastpos == NULL)
204 regex_free(m->pmatch);
208 NOTE("backref dissect");
209 dp = backref(m, m->coldp, endp, gf, gl, (sopno)0);
214 /* uh-oh... we couldn't find a subexpression-level match */
215 assert(g->backrefs); /* must be back references doing it */
216 assert(g->nplus == 0 || m->lastpos != NULL);
219 if (dp != NULL || endp <= m->coldp)
222 endp = slow(m, m->coldp, endp - 1, gf, gl);
225 /* try it on a shorter possibility */
227 for (i = 1; i <= m->g->nsub; i++)
229 assert(m->pmatch[i].rm_so == -1);
230 assert(m->pmatch[i].rm_eo == -1);
233 NOTE("backoff dissect");
234 dp = backref(m, m->coldp, endp, gf, gl, (sopno)0);
236 assert(dp == NULL || dp == endp);
237 if (dp != NULL) /* found a shorter one */
240 /* despite initial appearances, there is no match here */
242 start = m->coldp + 1; /* recycle starting later */
243 assert(start <= stop);
246 /* fill in the details if requested */
249 pmatch[0].rm_so = m->coldp - m->offp;
250 pmatch[0].rm_eo = endp - m->offp;
254 assert(m->pmatch != NULL);
255 for (i = 1; (size_t)i < nmatch; i++)
256 if ((size_t)i <= m->g->nsub)
257 pmatch[i] = m->pmatch[i];
260 pmatch[i].rm_so = -1;
261 pmatch[i].rm_eo = -1;
265 if (m->pmatch != NULL)
266 regex_free((char *)m->pmatch);
267 if (m->lastpos != NULL)
268 regex_free((char *)m->lastpos);
274 - dissect - figure out what matched what, no back references
275 == static char *dissect(register struct match *m, char *start, \
276 == char *stop, sopno startst, sopno stopst);
278 static char * /* == stop (success) always */
279 dissect(m, start, stop, startst, stopst) register struct match *m;
286 register sopno ss; /* start sop of current subRE */
287 register sopno es; /* end sop of current subRE */
288 register char *sp; /* start of string matched by it */
289 register char *stp; /* string matched by it cannot pass here */
290 register char *rest; /* start of rest of string */
291 register char *tail; /* string unmatched by rest of RE */
292 register sopno ssub; /* start sop of subsubRE */
293 register sopno esub; /* end sop of subsubRE */
294 register char *ssp; /* start of string matched by subsubRE */
295 register char *sep; /* end of string matched by subsubRE */
296 register char *oldssp; /* previous ssp */
300 AT("diss", start, stop, startst, stopst);
302 for (ss = startst; ss < stopst; ss = es)
304 /* identify end of subRE */
306 switch (OP(m->g->strip[es]))
310 es += OPND(m->g->strip[es]);
313 while (OP(m->g->strip[es]) != O_CH)
314 es += OPND(m->g->strip[es]);
319 /* figure out what it matched */
320 switch (OP(m->g->strip[ss]))
341 /* cases where length of match is hard to find */
346 /* how long could this one be? */
347 rest = slow(m, sp, stp, ss, es);
348 assert(rest != NULL); /* it did match */
349 /* could the rest match the rest? */
350 tail = slow(m, rest, stop, es, stopst);
353 /* no -- try a shorter match for this one */
355 assert(stp >= sp); /* it did work */
359 /* did innards match? */
360 if (slow(m, sp, rest, ssub, esub) != NULL)
362 dp = dissect(m, sp, rest, ssub, esub);
373 /* how long could this one be? */
374 rest = slow(m, sp, stp, ss, es);
375 assert(rest != NULL); /* it did match */
376 /* could the rest match the rest? */
377 tail = slow(m, rest, stop, es, stopst);
380 /* no -- try a shorter match for this one */
382 assert(stp >= sp); /* it did work */
388 for (;;) /* find last match of innards */
390 sep = slow(m, ssp, rest, ssub, esub);
391 if (sep == NULL || sep == ssp)
392 break; /* failed or matched null */
393 oldssp = ssp; /* on to next try */
398 /* last successful match */
402 assert(sep == rest); /* must exhaust substring */
403 assert(slow(m, ssp, sep, ssub, esub) == rest);
404 dp = dissect(m, ssp, sep, ssub, esub);
412 /* how long could this one be? */
413 rest = slow(m, sp, stp, ss, es);
414 assert(rest != NULL); /* it did match */
415 /* could the rest match the rest? */
416 tail = slow(m, rest, stop, es, stopst);
419 /* no -- try a shorter match for this one */
421 assert(stp >= sp); /* it did work */
424 esub = ss + OPND(m->g->strip[ss]) - 1;
425 assert(OP(m->g->strip[esub]) == OOR1);
426 for (;;) /* find first matching branch */
428 if (slow(m, sp, rest, ssub, esub) == rest)
429 break; /* it matched all of it */
430 /* that one missed, try next one */
431 assert(OP(m->g->strip[esub]) == OOR1);
433 assert(OP(m->g->strip[esub]) == OOR2);
435 esub += OPND(m->g->strip[esub]);
436 if (OP(m->g->strip[esub]) == OOR2)
439 assert(OP(m->g->strip[esub]) == O_CH);
441 dp = dissect(m, sp, rest, ssub, esub);
453 i = OPND(m->g->strip[ss]);
454 assert(0 < i && i <= m->g->nsub);
455 m->pmatch[i].rm_so = sp - m->offp;
458 i = OPND(m->g->strip[ss]);
459 assert(0 < i && i <= m->g->nsub);
460 m->pmatch[i].rm_eo = sp - m->offp;
473 - backref - figure out what matched what, figuring in back references
474 == static char *backref(register struct match *m, char *start, \
475 == char *stop, sopno startst, sopno stopst, sopno lev);
477 static char * /* == stop (success) or NULL (failure) */
478 backref(m, start, stop, startst, stopst, lev) register struct match *m;
483 sopno lev; /* PLUS nesting level */
486 register sopno ss; /* start sop of current subRE */
487 register char *sp; /* start of string matched by it */
488 register sopno ssub; /* start sop of subsubRE */
489 register sopno esub; /* end sop of subsubRE */
490 register char *ssp; /* start of string matched by subsubRE */
495 register regoff_t offsave;
498 AT("back", start, stop, startst, stopst);
501 /* get as far as we can with easy stuff */
503 for (ss = startst; !hard && ss < stopst; ss++)
504 switch (OP(s = m->g->strip[ss]))
507 if (sp == stop || *sp++ != (char)OPND(s))
516 cs = &m->g->sets[OPND(s)];
517 if (sp == stop || !CHIN(cs, *sp++))
521 if ((sp == m->beginp && !(m->eflags & REG_NOTBOL)) ||
522 (sp < m->endp && *(sp - 1) == '\n' &&
523 (m->g->cflags & REG_NEWLINE)))
531 if ((sp == m->endp && !(m->eflags & REG_NOTEOL)) ||
532 (sp < m->endp && *sp == '\n' &&
533 (m->g->cflags & REG_NEWLINE)))
541 if (((sp == m->beginp && !(m->eflags & REG_NOTBOL)) ||
542 (sp < m->endp && *(sp - 1) == '\n' &&
543 (m->g->cflags & REG_NEWLINE)) ||
545 !ISWORD(*(sp - 1)))) &&
546 (sp < m->endp && ISWORD(*sp)))
554 if (((sp == m->endp && !(m->eflags & REG_NOTEOL)) ||
555 (sp < m->endp && *sp == '\n' &&
556 (m->g->cflags & REG_NEWLINE)) ||
557 (sp < m->endp && !ISWORD(*sp))) &&
558 (sp > m->beginp && ISWORD(*(sp - 1))))
567 case OOR1: /* matches null but needs to skip */
572 assert(OP(s) == OOR2);
574 } while (OP(s = m->g->strip[ss]) != O_CH);
575 /* note that the ss++ gets us past the O_CH */
577 default: /* have to make a choice */
581 if (!hard) /* that was it! */
587 ss--; /* adjust for the for's final increment */
590 AT("hard", sp, stop, ss, stopst);
594 case OBACK_: /* the vilest depths */
596 assert(0 < i && i <= m->g->nsub);
597 if (m->pmatch[i].rm_eo == -1)
599 assert(m->pmatch[i].rm_so != -1);
600 len = m->pmatch[i].rm_eo - m->pmatch[i].rm_so;
601 assert(stop - m->beginp >= len);
603 return (NULL); /* not enough left to match */
604 ssp = m->offp + m->pmatch[i].rm_so;
605 if (memcmp(sp, ssp, len) != 0)
607 while (m->g->strip[ss] != SOP(O_BACK, i))
609 return (backref(m, sp + len, stop, ss + 1, stopst, lev));
611 case OQUEST_: /* to null or not */
612 dp = backref(m, sp, stop, ss + 1, stopst, lev);
614 return (dp); /* not */
615 return (backref(m, sp, stop, ss + OPND(s) + 1, stopst, lev));
618 assert(m->lastpos != NULL);
619 assert(lev + 1 <= m->g->nplus);
620 m->lastpos[lev + 1] = sp;
621 return (backref(m, sp, stop, ss + 1, stopst, lev + 1));
624 if (sp == m->lastpos[lev]) /* last pass matched null */
625 return (backref(m, sp, stop, ss + 1, stopst, lev - 1));
626 /* try another pass */
627 m->lastpos[lev] = sp;
628 dp = backref(m, sp, stop, ss - OPND(s) + 1, stopst, lev);
630 return (backref(m, sp, stop, ss + 1, stopst, lev - 1));
634 case OCH_: /* find the right one, if any */
636 esub = ss + OPND(s) - 1;
637 assert(OP(m->g->strip[esub]) == OOR1);
638 for (;;) /* find first matching branch */
640 dp = backref(m, sp, stop, ssub, esub, lev);
643 /* that one missed, try next one */
644 if (OP(m->g->strip[esub]) == O_CH)
645 return (NULL); /* there is none */
647 assert(OP(m->g->strip[esub]) == OOR2);
649 esub += OPND(m->g->strip[esub]);
650 if (OP(m->g->strip[esub]) == OOR2)
653 assert(OP(m->g->strip[esub]) == O_CH);
656 case OLPAREN: /* must undo assignment if rest fails */
658 assert(0 < i && i <= m->g->nsub);
659 offsave = m->pmatch[i].rm_so;
660 m->pmatch[i].rm_so = sp - m->offp;
661 dp = backref(m, sp, stop, ss + 1, stopst, lev);
664 m->pmatch[i].rm_so = offsave;
667 case ORPAREN: /* must undo assignment if rest fails */
669 assert(0 < i && i <= m->g->nsub);
670 offsave = m->pmatch[i].rm_eo;
671 m->pmatch[i].rm_eo = sp - m->offp;
672 dp = backref(m, sp, stop, ss + 1, stopst, lev);
675 m->pmatch[i].rm_eo = offsave;
686 return ((char *)NULL); /* dummy */
690 - fast - step through the string at top speed
691 == static char *fast(register struct match *m, char *start, \
692 == char *stop, sopno startst, sopno stopst);
694 static char * /* where tentative match ended, or NULL */
695 fast(m, start, stop, startst, stopst) register struct match *m;
701 register states st = m->st;
702 register states fresh = m->fresh;
703 register states tmp = m->tmp;
704 register char *p = start;
705 register int c = (start == m->beginp) ? OUT : *(start - 1);
706 register int lastc; /* previous c */
709 register char *coldp; /* last p after which no match was underway */
713 st = step(m->g, startst, stopst, st, NOTHING, st);
721 c = (p == m->endp) ? OUT : *p;
725 /* is there an EOL and/or BOL between lastc and c? */
728 if ((lastc == '\n' && m->g->cflags & REG_NEWLINE) ||
729 (lastc == OUT && !(m->eflags & REG_NOTBOL)))
734 if ((c == '\n' && m->g->cflags & REG_NEWLINE) ||
735 (c == OUT && !(m->eflags & REG_NOTEOL)))
737 flagch = (flagch == BOL) ? BOLEOL : EOL;
743 st = step(m->g, startst, stopst, st, flagch, st);
747 /* how about a word boundary? */
748 if ((flagch == BOL || (lastc != OUT && !ISWORD(lastc))) &&
749 (c != OUT && ISWORD(c)))
753 if ((lastc != OUT && ISWORD(lastc)) &&
754 (flagch == EOL || (c != OUT && !ISWORD(c))))
758 if (flagch == BOW || flagch == EOW)
760 st = step(m->g, startst, stopst, st, flagch, st);
765 if (ISSET(st, stopst) || p == stop)
766 break; /* NOTE BREAK OUT */
768 /* no, we must deal with this character */
772 st = step(m->g, startst, stopst, tmp, c, st);
774 assert(EQ(step(m->g, startst, stopst, st, NOTHING, st), st));
778 assert(coldp != NULL);
780 if (ISSET(st, stopst))
787 - slow - step through the string more deliberately
788 == static char *slow(register struct match *m, char *start, \
789 == char *stop, sopno startst, sopno stopst);
791 static char * /* where it ended */
792 slow(m, start, stop, startst, stopst) register struct match *m;
798 register states st = m->st;
799 register states empty = m->empty;
800 register states tmp = m->tmp;
801 register char *p = start;
802 register int c = (start == m->beginp) ? OUT : *(start - 1);
803 register int lastc; /* previous c */
806 register char *matchp; /* last p at which a match ended */
808 AT("slow", start, stop, startst, stopst);
811 SP("sstart", st, *p);
812 st = step(m->g, startst, stopst, st, NOTHING, st);
818 c = (p == m->endp) ? OUT : *p;
820 /* is there an EOL and/or BOL between lastc and c? */
823 if ((lastc == '\n' && m->g->cflags & REG_NEWLINE) ||
824 (lastc == OUT && !(m->eflags & REG_NOTBOL)))
829 if ((c == '\n' && m->g->cflags & REG_NEWLINE) ||
830 (c == OUT && !(m->eflags & REG_NOTEOL)))
832 flagch = (flagch == BOL) ? BOLEOL : EOL;
838 st = step(m->g, startst, stopst, st, flagch, st);
839 SP("sboleol", st, c);
842 /* how about a word boundary? */
843 if ((flagch == BOL || (lastc != OUT && !ISWORD(lastc))) &&
844 (c != OUT && ISWORD(c)))
848 if ((lastc != OUT && ISWORD(lastc)) &&
849 (flagch == EOL || (c != OUT && !ISWORD(c))))
853 if (flagch == BOW || flagch == EOW)
855 st = step(m->g, startst, stopst, st, flagch, st);
856 SP("sboweow", st, c);
860 if (ISSET(st, stopst))
862 if (EQ(st, empty) || p == stop)
863 break; /* NOTE BREAK OUT */
865 /* no, we must deal with this character */
869 st = step(m->g, startst, stopst, tmp, c, st);
871 assert(EQ(step(m->g, startst, stopst, st, NOTHING, st), st));
879 - step - map set of states reachable before char to set reachable after
880 == static states step(register struct re_guts *g, sopno start, sopno stop, \
881 == register states bef, int ch, register states aft);
882 == #define BOL (OUT+1)
883 == #define EOL (BOL+1)
884 == #define BOLEOL (BOL+2)
885 == #define NOTHING (BOL+3)
886 == #define BOW (BOL+4)
887 == #define EOW (BOL+5)
888 == #define CODEMAX (BOL+5) // highest code used
889 == #define NONCHAR(c) ((c) > CHAR_MAX)
890 == #define NNONCHAR (CODEMAX-CHAR_MAX)
893 step(g, start, stop, bef, ch, aft) register struct re_guts *g;
894 sopno start; /* start state within strip */
895 sopno stop; /* state after stop state within strip */
896 register states bef; /* states reachable before */
897 int ch; /* character or NONCHAR code */
898 register states aft; /* states already known reachable after */
903 register onestate here; /* note, macros know this name */
907 for (pc = start, INIT(here, pc); pc != stop; pc++, INC(here))
913 assert(pc == stop - 1);
916 /* only characters can match */
917 assert(!NONCHAR(ch) || ch != (char)OPND(s));
918 if (ch == (char)OPND(s))
922 if (ch == BOL || ch == BOLEOL)
926 if (ch == EOL || ch == BOLEOL)
942 cs = &g->sets[OPND(s)];
943 if (!NONCHAR(ch) && CHIN(cs, ch))
946 case OBACK_: /* ignored here */
950 case OPLUS_: /* forward, this is just an empty */
953 case O_PLUS: /* both forward and back */
955 i = ISSETBACK(aft, OPND(s));
956 BACK(aft, aft, OPND(s));
957 if (!i && ISSETBACK(aft, OPND(s)))
959 /* oho, must reconsider loop body */
964 case OQUEST_: /* two branches, both forward */
966 FWD(aft, aft, OPND(s));
968 case O_QUEST: /* just an empty */
971 case OLPAREN: /* not significant here */
975 case OCH_: /* mark the first two branches */
977 assert(OP(g->strip[pc + OPND(s)]) == OOR2);
978 FWD(aft, aft, OPND(s));
980 case OOR1: /* done a branch, find the O_CH */
981 if (ISSTATEIN(aft, here))
984 OP(s = g->strip[pc + look]) != O_CH;
986 assert(OP(s) == OOR2);
990 case OOR2: /* propagate OCH_'s marking */
992 if (OP(g->strip[pc + OPND(s)]) != O_CH)
994 assert(OP(g->strip[pc + OPND(s)]) == OOR2);
995 FWD(aft, aft, OPND(s));
998 case O_CH: /* just empty */
1001 default: /* ooooops... */
1012 - print - print a set of states
1014 == static void print(struct match *m, char *caption, states st, \
1015 == int ch, FILE *d);
1019 print(m, caption, st, ch, d) struct match *m;
1025 register struct re_guts *g = m->g;
1027 register int first = 1;
1029 if (!(m->eflags & REG_TRACE))
1032 fprintf(d, "%s", caption);
1034 fprintf(d, " %s", pchar(ch));
1035 for (i = 0; i < g->nstates; i++)
1038 fprintf(d, "%s%d", (first) ? "\t" : ", ", i);
1045 - at - print current situation
1047 == static void at(struct match *m, char *title, char *start, char *stop, \
1048 == sopno startst, sopno stopst);
1052 at(m, title, start, stop, startst, stopst) struct match *m;
1059 if (!(m->eflags & REG_TRACE))
1062 printf("%s %s-", title, pchar(*start));
1063 printf("%s ", pchar(*stop));
1064 printf("%ld-%ld\n", (long)startst, (long)stopst);
1068 #define PCHARDONE /* never again */
1070 - pchar - make a character printable
1072 == static char *pchar(int ch);
1075 * Is this identical to regchar() over in debug.c? Well, yes. But a
1076 * duplicate here avoids having a debugging-capable regexec.o tied to
1077 * a matching debug.o, and this is convenient. It all disappears in
1078 * the non-debug compilation anyway, so it doesn't matter much.
1080 static char * /* -> representation */
1083 static char pbuf[10];
1085 if (isprint(ch) || ch == ' ')
1086 sprintf(pbuf, "%c", ch);
1088 sprintf(pbuf, "\\%o", ch);