5 * "One Ring to rule them all, One Ring to find them..."
8 /* NOTE: this is derived from Henry Spencer's regexp code, and should not
9 * confused with the original package (see point 3 below). Thanks, Henry!
12 /* Additional note: this code is very heavily munged from Henry's version
13 * in places. In some spots I've traded clarity for efficiency, so don't
14 * blame Henry for some of the lack of readability.
17 /* The names of the functions have been changed from regcomp and
18 * regexec to pregcomp and pregexec in order to avoid conflicts
19 * with the POSIX routines of the same names.
24 * pregcomp and pregexec -- regsub and regerror are not used in perl
26 * Copyright (c) 1986 by University of Toronto.
27 * Written by Henry Spencer. Not derived from licensed software.
29 * Permission is granted to anyone to use this software for any
30 * purpose on any computer system, and to redistribute it freely,
31 * subject to the following restrictions:
33 * 1. The author is not responsible for the consequences of use of
34 * this software, no matter how awful, even if they arise
37 * 2. The origin of this software must not be misrepresented, either
38 * by explicit claim or by omission.
40 * 3. Altered versions must be plainly marked as such, and must not
41 * be misrepresented as being the original software.
43 **** Alterations to Henry's code are...
45 **** Copyright (c) 1991-1994, Larry Wall
47 **** You may distribute under the terms of either the GNU General Public
48 **** License or the Artistic License, as specified in the README file.
50 * Beware that some of this code is subtly aware of the way operator
51 * precedence is structured in regular expressions. Serious changes in
52 * regular-expression syntax might require a total rethink.
63 static I32 regnarrate = 0;
64 static char* regprogram = 0;
67 /* Current curly descriptor */
68 typedef struct curcur CURCUR;
70 int parenfloor; /* how far back to strip paren data */
71 int cur; /* how many instances of scan we've matched */
72 int min; /* the minimal number of scans to match */
73 int max; /* the maximal number of scans to match */
74 int minmod; /* whether to work our way up or down */
75 char * scan; /* the thing to match */
76 char * next; /* what has to match after it */
77 char * lastloc; /* where we started matching this scan */
78 CURCUR * oldcc; /* current curly before we started this one */
83 typedef I32 CHECKPOINT;
85 static CHECKPOINT regcppush _((I32 parenfloor));
86 static char * regcppop _((void));
92 int retval = savestack_ix;
93 int i = (regsize - parenfloor) * 3;
97 for (p = regsize; p > parenfloor; p--) {
98 SSPUSHPTR(regendp[p]);
99 SSPUSHPTR(regstartp[p]);
103 SSPUSHINT(*reglastparen);
106 SSPUSHINT(SAVEt_REGCONTEXT);
117 assert(i == SAVEt_REGCONTEXT);
119 input = (char *) SSPOPPTR;
120 *reglastparen = SSPOPINT;
122 for (i -= 3; i > 0; i -= 3) {
123 paren = (U32)SSPOPINT;
124 regstartp[paren] = (char *) SSPOPPTR;
125 tmps = (char*)SSPOPPTR;
126 if (paren <= *reglastparen)
127 regendp[paren] = tmps;
129 for (paren = *reglastparen + 1; paren <= regnpar; paren++) {
131 regstartp[paren] = Nullch;
132 regendp[paren] = Nullch;
137 #define regcpblow(cp) leave_scope(cp)
140 * pregexec and friends
147 static I32 regmatch _((char *prog));
148 static I32 regrepeat _((char *p, I32 max));
149 static I32 regtry _((regexp *prog, char *startpos));
150 static bool reginclass _((char *p, I32 c));
152 static bool regtainted; /* tainted information used? */
155 - pregexec - match a regexp against a string
158 pregexec(prog, stringarg, strend, strbeg, minend, screamer, safebase)
159 register regexp *prog;
161 register char *strend; /* pointer to null at end of string */
162 char *strbeg; /* real beginning of string */
163 I32 minend; /* end of match must be at least minend after stringarg */
165 I32 safebase; /* no need to remember string in subbase */
169 register char *startpos = stringarg;
171 I32 minlen = 0; /* must match at least this many chars */
172 I32 dontbother = 0; /* how many characters not to try at end */
180 regnarrate = debug & 512;
181 regprogram = prog->program;
185 if (prog == NULL || startpos == NULL) {
186 croak("NULL regexp parameter");
190 if (startpos == strbeg) /* is ^ valid at stringarg? */
193 regprev = stringarg[-1];
194 if (!multiline && regprev == '\n')
195 regprev = '\0'; /* force ^ to NOT match */
198 regprecomp = prog->precomp;
199 /* Check validity of program. */
200 if (UCHARAT(prog->program) != MAGIC) {
201 FAIL("corrupted regexp program");
204 regnpar = prog->nparens;
207 /* If there is a "must appear" string, look for it. */
209 if (prog->regmust != Nullsv &&
210 (!(prog->reganch & ROPT_ANCH)
211 || (multiline && prog->regback >= 0)) )
213 if (stringarg == strbeg && screamer) {
214 if (screamfirst[BmRARE(prog->regmust)] >= 0)
215 s = screaminstr(screamer,prog->regmust);
220 s = fbm_instr((unsigned char*)s, (unsigned char*)strend,
223 ++BmUSEFUL(prog->regmust); /* hooray */
224 goto phooey; /* not present */
226 else if (prog->regback >= 0) {
230 minlen = prog->regback + SvCUR(prog->regmust);
232 else if (!prog->naughty && --BmUSEFUL(prog->regmust) < 0) { /* boo */
233 SvREFCNT_dec(prog->regmust);
234 prog->regmust = Nullsv; /* disable regmust */
239 minlen = SvCUR(prog->regmust);
243 /* Mark beginning of line for ^ . */
246 /* Mark end of line for $ (and such) */
249 /* see how far we have to get to not match where we matched before */
250 regtill = startpos+minend;
252 /* Simplest case: anchored match need be tried only once. */
253 /* [unless multiline is set] */
254 if (prog->reganch & ROPT_ANCH) {
255 if (regtry(prog, startpos))
257 else if (multiline || (prog->reganch & ROPT_IMPLICIT)) {
259 dontbother = minlen - 1;
260 strend -= dontbother;
261 /* for multiline we only have to try after newlines */
266 if (s < strend && regtry(prog, s))
274 /* Messy cases: unanchored match. */
275 if (prog->regstart) {
276 if (prog->reganch & ROPT_SKIP) { /* we have /x+whatever/ */
277 /* it must be a one character string */
278 char ch = SvPVX(prog->regstart)[0];
284 while (s < strend && *s == ch)
290 else if (SvPOK(prog->regstart) == 3) {
291 /* We know what string it must start with. */
292 while ((s = fbm_instr((unsigned char*)s,
293 (unsigned char*)strend, prog->regstart)) != NULL)
301 c = SvPVX(prog->regstart);
302 while ((s = ninstr(s, strend, c, c + SvCUR(prog->regstart))) != NULL)
312 if (c = prog->regstclass) {
313 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
316 dontbother = minlen - 1;
317 strend -= dontbother; /* don't bother with what can't match */
319 /* We know what class it must start with. */
324 if (reginclass(c, *s)) {
325 if (tmp && regtry(prog, s))
340 dontbother++,strend--;
341 tmp = (s != startpos) ? UCHARAT(s - 1) : regprev;
342 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
344 if (tmp == !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
351 if ((minlen || tmp) && regtry(prog,s))
359 dontbother++,strend--;
360 tmp = (s != startpos) ? UCHARAT(s - 1) : regprev;
361 tmp = ((OP(c) == NBOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
363 if (tmp == !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
365 else if (regtry(prog, s))
369 if ((minlen || !tmp) && regtry(prog,s))
375 if (tmp && regtry(prog, s))
388 if (isALNUM_LC(*s)) {
389 if (tmp && regtry(prog, s))
402 if (tmp && regtry(prog, s))
415 if (!isALNUM_LC(*s)) {
416 if (tmp && regtry(prog, s))
429 if (tmp && regtry(prog, s))
442 if (isSPACE_LC(*s)) {
443 if (tmp && regtry(prog, s))
456 if (tmp && regtry(prog, s))
469 if (!isSPACE_LC(*s)) {
470 if (tmp && regtry(prog, s))
483 if (tmp && regtry(prog, s))
496 if (tmp && regtry(prog, s))
510 dontbother = minlen - 1;
511 strend -= dontbother;
512 /* We don't know much -- general case. */
516 } while (s++ < strend);
523 strend += dontbother; /* uncheat */
524 prog->subbeg = strbeg;
525 prog->subend = strend;
526 prog->exec_tainted = regtainted;
527 if (!safebase && (prog->nparens || sawampersand)) {
528 I32 i = strend - startpos + (stringarg - strbeg);
529 if (safebase) { /* no need for $digit later */
533 else if (strbeg != prog->subbase) {
534 s = savepvn(strbeg,i); /* so $digit will work later */
536 Safefree(prog->subbase);
537 prog->subbeg = prog->subbase = s;
541 prog->subbeg = s = prog->subbase;
544 s += (stringarg - strbeg);
545 for (i = 0; i <= prog->nparens; i++) {
547 prog->startp[i] = s + (prog->startp[i] - startpos);
548 prog->endp[i] = s + (prog->endp[i] - startpos);
559 - regtry - try match at specific point
561 static I32 /* 0 failure, 1 success */
562 regtry(prog, startpos)
571 regstartp = prog->startp;
572 regendp = prog->endp;
573 reglastparen = &prog->lastparen;
580 for (i = prog->nparens; i >= 0; i--) {
585 if (regmatch(prog->program + 1) && reginput >= regtill) {
586 prog->startp[0] = startpos;
587 prog->endp[0] = reginput;
595 - regmatch - main matching routine
597 * Conceptually the strategy is simple: check to see whether the current
598 * node matches, call self recursively to see whether the rest matches,
599 * and then act accordingly. In practice we make some effort to avoid
600 * recursion, in particular by going through "ordinary" nodes (that don't
601 * need to know whether the rest of the match failed) by a loop instead of
604 /* [lwall] I've hoisted the register declarations to the outer block in order to
605 * maybe save a little bit of pushing and popping on the stack. It also takes
606 * advantage of machines that use a register save mask on subroutine entry.
608 static I32 /* 0 failure, 1 success */
612 register char *scan; /* Current node. */
613 char *next; /* Next node. */
614 register I32 nextchar;
615 register I32 n; /* no or next */
616 register I32 ln; /* len or last */
617 register char *s; /* operand or save */
618 register char *locinput = reginput;
619 register I32 c1, c2; /* case fold search */
622 static int regindent = 0;
626 nextchar = UCHARAT(locinput);
628 while (scan != NULL) {
630 #define sayYES goto yes
631 #define sayNO goto no
632 #define saySAME(x) if (x) goto yes; else goto no
634 PerlIO_printf(Perl_debug_log, "%*s%2d%-8.8s\t<%.10s>\n", regindent*2, "",
635 scan - regprogram, regprop(scan), locinput);
638 #define sayYES return 1
639 #define sayNO return 0
640 #define saySAME(x) return x
644 next = scan + NEXT(scan);
648 next = regnext(scan);
653 if (locinput == regbol
655 : ((nextchar || locinput < regeol) && locinput[-1] == '\n') )
657 /* regtill = regbol; */
662 if (locinput == regbol
664 : ((nextchar || locinput < regeol) && locinput[-1] == '\n') )
670 if (locinput == regbol && regprev == '\n')
674 if (locinput == regbol)
684 if ((nextchar || locinput < regeol) && nextchar != '\n')
689 if ((nextchar || locinput < regeol) && nextchar != '\n')
691 if (regeol - locinput > 1)
695 if (!nextchar && locinput >= regeol)
697 nextchar = UCHARAT(++locinput);
700 if (!nextchar && locinput >= regeol || nextchar == '\n')
702 nextchar = UCHARAT(++locinput);
707 /* Inline the first character, for speed. */
708 if (UCHARAT(s) != nextchar)
710 if (regeol - locinput < ln)
712 if (ln > 1 && memNE(s, locinput, ln))
715 nextchar = UCHARAT(locinput);
723 /* Inline the first character, for speed. */
724 if (UCHARAT(s) != nextchar &&
725 UCHARAT(s) != ((OP(scan) == EXACTF)
726 ? fold : fold_locale)[nextchar])
728 if (regeol - locinput < ln)
730 if (ln > 1 && ((OP(scan) == EXACTF)
731 ? ibcmp : ibcmp_locale)(s, locinput, ln) != 0)
734 nextchar = UCHARAT(locinput);
739 nextchar = UCHARAT(locinput);
740 if (!reginclass(s, nextchar))
742 if (!nextchar && locinput >= regeol)
744 nextchar = UCHARAT(++locinput);
752 if (!(OP(scan) == ALNUM
753 ? isALNUM(nextchar) : isALNUM_LC(nextchar)))
755 nextchar = UCHARAT(++locinput);
761 if (!nextchar && locinput >= regeol)
763 if (OP(scan) == NALNUM
764 ? isALNUM(nextchar) : isALNUM_LC(nextchar))
766 nextchar = UCHARAT(++locinput);
774 /* was last char in word? */
775 ln = (locinput != regbol) ? UCHARAT(locinput - 1) : regprev;
776 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
778 n = isALNUM(nextchar);
782 n = isALNUM_LC(nextchar);
784 if (((!ln) == (!n)) == (OP(scan) == BOUND || OP(scan) == BOUNDL))
791 if (!nextchar && locinput >= regeol)
793 if (!(OP(scan) == SPACE
794 ? isSPACE(nextchar) : isSPACE_LC(nextchar)))
796 nextchar = UCHARAT(++locinput);
804 if (OP(scan) == SPACE
805 ? isSPACE(nextchar) : isSPACE_LC(nextchar))
807 nextchar = UCHARAT(++locinput);
810 if (!isDIGIT(nextchar))
812 nextchar = UCHARAT(++locinput);
815 if (!nextchar && locinput >= regeol)
817 if (isDIGIT(nextchar))
819 nextchar = UCHARAT(++locinput);
822 n = ARG1(scan); /* which paren pair */
830 /* Inline the first character, for speed. */
831 if (UCHARAT(s) != nextchar)
834 if (locinput + ln > regeol)
836 if (ln > 1 && memNE(s, locinput, ln))
839 nextchar = UCHARAT(locinput);
847 n = ARG1(scan); /* which paren pair */
848 regstartp[n] = locinput;
853 n = ARG1(scan); /* which paren pair */
854 regendp[n] = locinput;
855 if (n > *reglastparen)
860 CHECKPOINT cp = savestack_ix;
863 cc.parenfloor = *reglastparen;
867 cc.scan = NEXTOPER(scan) + 4;
872 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
880 * This is really hard to understand, because after we match
881 * what we're trying to match, we must make sure the rest of
882 * the RE is going to match for sure, and to do that we have
883 * to go back UP the parse tree by recursing ever deeper. And
884 * if it fails, we have to reset our parent's current state
885 * that we can try again after backing off.
889 n = cc->cur + 1; /* how many we know we matched */
894 PerlIO_printf(Perl_debug_log, "%*s %d %lx\n", regindent*2, "",
898 /* If degenerate scan matches "", assume scan done. */
900 if (locinput == cc->lastloc) {
903 if (regmatch(cc->next))
910 /* First just match a string of min scans. */
914 cc->lastloc = locinput;
915 if (regmatch(cc->scan))
921 /* Prefer next over scan for minimal matching. */
926 if (regmatch(cc->next))
927 sayYES; /* All done. */
931 if (n >= cc->max) /* Maximum greed exceeded? */
934 /* Try scanning more and see if it helps. */
937 cc->lastloc = locinput;
938 if (regmatch(cc->scan))
944 /* Prefer scan over next for maximal matching. */
946 if (n < cc->max) { /* More greed allowed? */
947 regcppush(cc->parenfloor);
949 cc->lastloc = locinput;
950 if (regmatch(cc->scan))
952 regcppop(); /* Restore some previous $<digit>s? */
956 /* Failed deeper matches of scan, so see if this one works. */
959 if (regmatch(cc->next))
968 if (OP(next) != BRANCH) /* No choice. */
969 next = NEXTOPER(scan);/* Avoid recursion. */
971 int lastparen = *reglastparen;
974 if (regmatch(NEXTOPER(scan)))
976 for (n = *reglastparen; n > lastparen; n--)
987 scan = regnext(scan);
989 } while (scan != NULL && OP(scan) == BRANCH);
999 ln = ARG1(scan); /* min to match */
1000 n = ARG2(scan); /* max to match */
1001 scan = NEXTOPER(scan) + 4;
1006 scan = NEXTOPER(scan);
1010 * Lookahead to avoid useless match attempts
1011 * when we know what character comes next.
1015 scan = NEXTOPER(scan);
1017 if (regkind[(U8)OP(next)] == EXACT) {
1018 c1 = UCHARAT(OPERAND(next) + 1);
1019 if (OP(next) == EXACTF)
1021 else if (OP(next) == EXACTFL)
1022 c2 = fold_locale[c1];
1028 reginput = locinput;
1031 if (ln && regrepeat(scan, ln) < ln)
1033 while (n >= ln || (n == 32767 && ln > 0)) { /* ln overflow ? */
1034 /* If it could work, try it. */
1036 UCHARAT(reginput) == c1 ||
1037 UCHARAT(reginput) == c2)
1042 /* Couldn't or didn't -- back up. */
1043 reginput = locinput + ln;
1044 if (regrepeat(scan, 1)) {
1046 reginput = locinput + ln;
1053 n = regrepeat(scan, n);
1054 if (ln < n && regkind[(U8)OP(next)] == EOL &&
1055 (!multiline || OP(next) == SEOL))
1056 ln = n; /* why back off? */
1058 /* If it could work, try it. */
1060 UCHARAT(reginput) == c1 ||
1061 UCHARAT(reginput) == c2)
1066 /* Couldn't or didn't -- back up. */
1068 reginput = locinput + n;
1074 reginput = locinput; /* put where regtry can find it */
1075 sayYES; /* Success! */
1077 reginput = locinput;
1078 scan = NEXTOPER(scan);
1079 if (!regmatch(scan))
1083 reginput = locinput;
1084 scan = NEXTOPER(scan);
1089 PerlIO_printf(PerlIO_stderr(), "%x %d\n",(unsigned)scan,scan[1]);
1090 FAIL("regexp memory corruption");
1096 * We get here only if there's trouble -- normally "case END" is
1097 * the terminating point.
1099 FAIL("corrupted regexp pointers");
1117 - regrepeat - repeatedly match something simple, report how many
1120 * [This routine now assumes that it will only match on things of length 1.
1121 * That was true before, but now we assume scan - reginput is the count,
1122 * rather than incrementing count on every character.]
1129 register char *scan;
1130 register char *opnd;
1132 register char *loceol = regeol;
1135 if (max != 32767 && max < loceol - scan)
1136 loceol = scan + max;
1140 while (scan < loceol && *scan != '\n')
1146 case EXACT: /* length of string is 1 */
1147 c = UCHARAT(++opnd);
1148 while (scan < loceol && UCHARAT(scan) == c)
1151 case EXACTF: /* length of string is 1 */
1152 c = UCHARAT(++opnd);
1153 while (scan < loceol &&
1154 (UCHARAT(scan) == c || UCHARAT(scan) == fold[c]))
1157 case EXACTFL: /* length of string is 1 */
1159 c = UCHARAT(++opnd);
1160 while (scan < loceol &&
1161 (UCHARAT(scan) == c || UCHARAT(scan) == fold_locale[c]))
1165 while (scan < loceol && reginclass(opnd, *scan))
1169 while (scan < loceol && isALNUM(*scan))
1174 while (scan < loceol && isALNUM_LC(*scan))
1178 while (scan < loceol && !isALNUM(*scan))
1183 while (scan < loceol && !isALNUM_LC(*scan))
1187 while (scan < loceol && isSPACE(*scan))
1192 while (scan < loceol && isSPACE_LC(*scan))
1196 while (scan < loceol && !isSPACE(*scan))
1201 while (scan < loceol && !isSPACE_LC(*scan))
1205 while (scan < loceol && isDIGIT(*scan))
1209 while (scan < loceol && !isDIGIT(*scan))
1212 default: /* Called on something of 0 width. */
1213 break; /* So match right here or not at all. */
1216 c = scan - reginput;
1223 - regclass - determine if a character falls into a character class
1235 if (p[1 + (c >> 3)] & (1 << (c & 7)))
1237 else if (flags & ANYOF_FOLD) {
1239 if (flags & ANYOF_LOCALE) {
1241 cf = fold_locale[c];
1245 if (p[1 + (cf >> 3)] & (1 << (cf & 7)))
1249 if (!match && (flags & ANYOF_ISA)) {
1252 if (((flags & ANYOF_ALNUML) && isALNUM_LC(c)) ||
1253 ((flags & ANYOF_NALNUML) && !isALNUM_LC(c)) ||
1254 ((flags & ANYOF_SPACEL) && isSPACE_LC(c)) ||
1255 ((flags & ANYOF_NSPACEL) && !isSPACE_LC(c)))
1261 return match ^ ((flags & ANYOF_INVERT) != 0);
1265 - regnext - dig the "next" pointer out of a node
1267 * [Note, when REGALIGN is defined there are two places in regmatch()
1268 * that bypass this code for speed.]
1274 register I32 offset;