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-1997, 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));
89 regcppush(I32 parenfloor)
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);
118 assert(i == SAVEt_REGCONTEXT);
120 input = (char *) SSPOPPTR;
121 *reglastparen = SSPOPINT;
123 for (i -= 3; i > 0; i -= 3) {
124 paren = (U32)SSPOPINT;
125 regstartp[paren] = (char *) SSPOPPTR;
126 tmps = (char*)SSPOPPTR;
127 if (paren <= *reglastparen)
128 regendp[paren] = tmps;
130 for (paren = *reglastparen + 1; paren <= regnpar; paren++) {
132 regstartp[paren] = Nullch;
133 regendp[paren] = Nullch;
138 /* After a successful match in WHILEM, we want to restore paren matches
139 * that have been overwritten by a failed match attempt in the process
140 * of reaching this success. We do this by restoring regstartp[i]
141 * wherever regendp[i] has not changed; if OPEN is changed to modify
142 * regendp[], the '== endp' test below should be changed to match.
143 * This corrects the error of:
144 * 0 > length [ "foobar" =~ / ( (foo) | (bar) )* /x ]->[1]
147 regcppartblow(I32 base)
154 assert(i == SAVEt_REGCONTEXT);
156 /* input, lastparen, size */
157 SSPOPPTR; SSPOPINT; SSPOPINT;
158 for (i -= 3; i > 0; i -= 3) {
159 paren = (U32)SSPOPINT;
160 startp = (char *) SSPOPPTR;
161 endp = (char *) SSPOPPTR;
162 if (paren <= *reglastparen && regendp[paren] == endp)
163 regstartp[paren] = startp;
165 assert(savestack_ix == base);
168 #define regcpblow(cp) leave_scope(cp)
171 * pregexec and friends
178 static I32 regmatch _((char *prog));
179 static I32 regrepeat _((char *p, I32 max));
180 static I32 regtry _((regexp *prog, char *startpos));
181 static bool reginclass _((char *p, I32 c));
183 static bool regtainted; /* tainted information used? */
186 - pregexec - match a regexp against a string
189 pregexec(register regexp *prog, char *stringarg, register char *strend, char *strbeg, I32 minend, SV *screamer, I32 safebase)
192 /* pointer to null at end of string */
193 /* real beginning of string */
194 /* end of match must be at least minend after stringarg */
196 /* no need to remember string in subbase */
200 register char *startpos = stringarg;
202 I32 minlen = 0; /* must match at least this many chars */
203 I32 dontbother = 0; /* how many characters not to try at end */
211 regnarrate = debug & 512;
212 regprogram = prog->program;
216 if (prog == NULL || startpos == NULL) {
217 croak("NULL regexp parameter");
221 if (startpos == strbeg) /* is ^ valid at stringarg? */
224 regprev = stringarg[-1];
225 if (!multiline && regprev == '\n')
226 regprev = '\0'; /* force ^ to NOT match */
229 regprecomp = prog->precomp;
230 /* Check validity of program. */
231 if (UCHARAT(prog->program) != MAGIC) {
232 FAIL("corrupted regexp program");
235 regnpar = prog->nparens;
238 /* If there is a "must appear" string, look for it. */
240 if (prog->regmust != Nullsv &&
241 !(prog->reganch & ROPT_ANCH_GPOS) &&
242 (!(prog->reganch & ROPT_ANCH_BOL)
243 || (multiline && prog->regback >= 0)) )
245 if (stringarg == strbeg && screamer) {
246 if (screamfirst[BmRARE(prog->regmust)] >= 0)
247 s = screaminstr(screamer,prog->regmust);
252 s = fbm_instr((unsigned char*)s, (unsigned char*)strend,
255 ++BmUSEFUL(prog->regmust); /* hooray */
256 goto phooey; /* not present */
258 else if (prog->regback >= 0) {
262 minlen = prog->regback + SvCUR(prog->regmust);
264 else if (!prog->naughty && --BmUSEFUL(prog->regmust) < 0) { /* boo */
265 SvREFCNT_dec(prog->regmust);
266 prog->regmust = Nullsv; /* disable regmust */
271 minlen = SvCUR(prog->regmust);
275 /* Mark beginning of line for ^ . */
278 /* Mark end of line for $ (and such) */
281 /* see how far we have to get to not match where we matched before */
282 regtill = startpos+minend;
284 /* Simplest case: anchored match need be tried only once. */
285 /* [unless only anchor is BOL and multiline is set] */
286 if (prog->reganch & ROPT_ANCH) {
287 if (regtry(prog, startpos))
289 else if (!(prog->reganch & ROPT_ANCH_GPOS) &&
290 (multiline || (prog->reganch & ROPT_IMPLICIT)))
293 dontbother = minlen - 1;
294 strend -= dontbother;
295 /* for multiline we only have to try after newlines */
300 if (s < strend && regtry(prog, s))
308 /* Messy cases: unanchored match. */
309 if (prog->regstart) {
310 if (prog->reganch & ROPT_SKIP) { /* we have /x+whatever/ */
311 /* it must be a one character string */
312 char ch = SvPVX(prog->regstart)[0];
318 while (s < strend && *s == ch)
324 else if (SvTYPE(prog->regstart) == SVt_PVBM) {
325 /* We know what string it must start with. */
326 while ((s = fbm_instr((unsigned char*)s,
327 (unsigned char*)strend, prog->regstart)) != NULL)
334 else { /* Optimized fbm_instr: */
335 c = SvPVX(prog->regstart);
336 while ((s = ninstr(s, strend, c, c + SvCUR(prog->regstart))) != NULL)
346 if (c = prog->regstclass) {
347 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
350 dontbother = minlen - 1;
351 strend -= dontbother; /* don't bother with what can't match */
353 /* We know what class it must start with. */
358 if (reginclass(c, *s)) {
359 if (tmp && regtry(prog, s))
374 dontbother++,strend--;
375 tmp = (s != startpos) ? UCHARAT(s - 1) : regprev;
376 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
378 if (tmp == !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
385 if ((minlen || tmp) && regtry(prog,s))
393 dontbother++,strend--;
394 tmp = (s != startpos) ? UCHARAT(s - 1) : regprev;
395 tmp = ((OP(c) == NBOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
397 if (tmp == !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
399 else if (regtry(prog, s))
403 if ((minlen || !tmp) && regtry(prog,s))
409 if (tmp && regtry(prog, s))
422 if (isALNUM_LC(*s)) {
423 if (tmp && regtry(prog, s))
436 if (tmp && regtry(prog, s))
449 if (!isALNUM_LC(*s)) {
450 if (tmp && regtry(prog, s))
463 if (tmp && regtry(prog, s))
476 if (isSPACE_LC(*s)) {
477 if (tmp && regtry(prog, s))
490 if (tmp && regtry(prog, s))
503 if (!isSPACE_LC(*s)) {
504 if (tmp && regtry(prog, s))
517 if (tmp && regtry(prog, s))
530 if (tmp && regtry(prog, s))
544 dontbother = minlen - 1;
545 strend -= dontbother;
546 /* We don't know much -- general case. */
550 } while (s++ < strend);
557 strend += dontbother; /* uncheat */
558 prog->subbeg = strbeg;
559 prog->subend = strend;
560 prog->exec_tainted = regtainted;
562 /* make sure $`, $&, $', and $digit will work later */
563 if (strbeg != prog->subbase) {
566 Safefree(prog->subbase);
567 prog->subbase = Nullch;
571 I32 i = strend - startpos + (stringarg - strbeg);
572 s = savepvn(strbeg, i);
573 Safefree(prog->subbase);
575 prog->subbeg = prog->subbase;
576 prog->subend = prog->subbase + i;
577 s = prog->subbase + (stringarg - strbeg);
578 for (i = 0; i <= prog->nparens; i++) {
580 prog->startp[i] = s + (prog->startp[i] - startpos);
581 prog->endp[i] = s + (prog->endp[i] - startpos);
593 - regtry - try match at specific point
595 static I32 /* 0 failure, 1 success */
596 regtry(regexp *prog, char *startpos)
603 regstartp = prog->startp;
604 regendp = prog->endp;
605 reglastparen = &prog->lastparen;
612 for (i = prog->nparens; i >= 0; i--) {
617 if (regmatch(prog->program + 1) && reginput >= regtill) {
618 prog->startp[0] = startpos;
619 prog->endp[0] = reginput;
627 - regmatch - main matching routine
629 * Conceptually the strategy is simple: check to see whether the current
630 * node matches, call self recursively to see whether the rest matches,
631 * and then act accordingly. In practice we make some effort to avoid
632 * recursion, in particular by going through "ordinary" nodes (that don't
633 * need to know whether the rest of the match failed) by a loop instead of
636 /* [lwall] I've hoisted the register declarations to the outer block in order to
637 * maybe save a little bit of pushing and popping on the stack. It also takes
638 * advantage of machines that use a register save mask on subroutine entry.
640 static I32 /* 0 failure, 1 success */
643 register char *scan; /* Current node. */
644 char *next; /* Next node. */
645 register I32 nextchar;
646 register I32 n; /* no or next */
647 register I32 ln; /* len or last */
648 register char *s; /* operand or save */
649 register char *locinput = reginput;
650 register I32 c1, c2; /* case fold search */
653 static int regindent = 0;
657 nextchar = UCHARAT(locinput);
659 while (scan != NULL) {
661 #define sayYES goto yes
662 #define sayNO goto no
663 #define saySAME(x) if (x) goto yes; else goto no
665 SV *prop = sv_newmortal();
667 PerlIO_printf(Perl_debug_log, "%*s%2ld%-8.8s\t<%.10s>\n",
668 regindent*2, "", (long)(scan - regprogram),
669 SvPVX(prop), locinput);
672 #define sayYES return 1
673 #define sayNO return 0
674 #define saySAME(x) return x
678 next = scan + NEXT(scan);
682 next = regnext(scan);
687 if (locinput == regbol
689 : ((nextchar || locinput < regeol) && locinput[-1] == '\n') )
691 /* regtill = regbol; */
696 if (locinput == regbol
698 : ((nextchar || locinput < regeol) && locinput[-1] == '\n') )
704 if (locinput == regbol && regprev == '\n')
708 if (locinput == regbol)
718 if ((nextchar || locinput < regeol) && nextchar != '\n')
723 if ((nextchar || locinput < regeol) && nextchar != '\n')
725 if (regeol - locinput > 1)
729 if (!nextchar && locinput >= regeol)
731 nextchar = UCHARAT(++locinput);
734 if (!nextchar && locinput >= regeol || nextchar == '\n')
736 nextchar = UCHARAT(++locinput);
741 /* Inline the first character, for speed. */
742 if (UCHARAT(s) != nextchar)
744 if (regeol - locinput < ln)
746 if (ln > 1 && memNE(s, locinput, ln))
749 nextchar = UCHARAT(locinput);
757 /* Inline the first character, for speed. */
758 if (UCHARAT(s) != nextchar &&
759 UCHARAT(s) != ((OP(scan) == EXACTF)
760 ? fold : fold_locale)[nextchar])
762 if (regeol - locinput < ln)
764 if (ln > 1 && (OP(scan) == EXACTF
765 ? ibcmp(s, locinput, ln)
766 : ibcmp_locale(s, locinput, ln)))
769 nextchar = UCHARAT(locinput);
774 nextchar = UCHARAT(locinput);
775 if (!reginclass(s, nextchar))
777 if (!nextchar && locinput >= regeol)
779 nextchar = UCHARAT(++locinput);
787 if (!(OP(scan) == ALNUM
788 ? isALNUM(nextchar) : isALNUM_LC(nextchar)))
790 nextchar = UCHARAT(++locinput);
796 if (!nextchar && locinput >= regeol)
798 if (OP(scan) == NALNUM
799 ? isALNUM(nextchar) : isALNUM_LC(nextchar))
801 nextchar = UCHARAT(++locinput);
809 /* was last char in word? */
810 ln = (locinput != regbol) ? UCHARAT(locinput - 1) : regprev;
811 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
813 n = isALNUM(nextchar);
817 n = isALNUM_LC(nextchar);
819 if (((!ln) == (!n)) == (OP(scan) == BOUND || OP(scan) == BOUNDL))
826 if (!nextchar && locinput >= regeol)
828 if (!(OP(scan) == SPACE
829 ? isSPACE(nextchar) : isSPACE_LC(nextchar)))
831 nextchar = UCHARAT(++locinput);
839 if (OP(scan) == SPACE
840 ? isSPACE(nextchar) : isSPACE_LC(nextchar))
842 nextchar = UCHARAT(++locinput);
845 if (!isDIGIT(nextchar))
847 nextchar = UCHARAT(++locinput);
850 if (!nextchar && locinput >= regeol)
852 if (isDIGIT(nextchar))
854 nextchar = UCHARAT(++locinput);
861 n = ARG1(scan); /* which paren pair */
869 /* Inline the first character, for speed. */
870 if (UCHARAT(s) != nextchar &&
872 (UCHARAT(s) != ((OP(scan) == REFF
873 ? fold : fold_locale)[nextchar]))))
876 if (locinput + ln > regeol)
878 if (ln > 1 && (OP(scan) == REF
879 ? memNE(s, locinput, ln)
881 ? ibcmp(s, locinput, ln)
882 : ibcmp_locale(s, locinput, ln))))
885 nextchar = UCHARAT(locinput);
893 n = ARG1(scan); /* which paren pair */
894 regstartp[n] = locinput;
899 n = ARG1(scan); /* which paren pair */
900 regendp[n] = locinput;
901 if (n > *reglastparen)
907 CHECKPOINT cp = savestack_ix;
910 cc.parenfloor = *reglastparen;
914 cc.scan = NEXTOPER(scan) + 4;
919 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
927 * This is really hard to understand, because after we match
928 * what we're trying to match, we must make sure the rest of
929 * the RE is going to match for sure, and to do that we have
930 * to go back UP the parse tree by recursing ever deeper. And
931 * if it fails, we have to reset our parent's current state
932 * that we can try again after backing off.
937 n = cc->cur + 1; /* how many we know we matched */
942 PerlIO_printf(Perl_debug_log, "%*s %ld %lx\n", regindent*2, "",
946 /* If degenerate scan matches "", assume scan done. */
948 if (locinput == cc->lastloc && n >= cc->min) {
951 if (regmatch(cc->next))
958 /* First just match a string of min scans. */
962 cc->lastloc = locinput;
963 if (regmatch(cc->scan))
969 /* Prefer next over scan for minimal matching. */
974 cp = regcppush(cc->parenfloor);
975 if (regmatch(cc->next)) {
977 sayYES; /* All done. */
983 if (n >= cc->max) /* Maximum greed exceeded? */
986 /* Try scanning more and see if it helps. */
989 cc->lastloc = locinput;
990 cp = regcppush(cc->parenfloor);
991 if (regmatch(cc->scan)) {
1000 /* Prefer scan over next for maximal matching. */
1002 if (n < cc->max) { /* More greed allowed? */
1003 cp = regcppush(cc->parenfloor);
1005 cc->lastloc = locinput;
1006 if (regmatch(cc->scan)) {
1010 regcppop(); /* Restore some previous $<digit>s? */
1011 reginput = locinput;
1014 /* Failed deeper matches of scan, so see if this one works. */
1017 if (regmatch(cc->next))
1026 if (OP(next) != BRANCH) /* No choice. */
1027 next = NEXTOPER(scan);/* Avoid recursion. */
1029 int lastparen = *reglastparen;
1031 reginput = locinput;
1032 if (regmatch(NEXTOPER(scan)))
1034 for (n = *reglastparen; n > lastparen; n--)
1045 scan = regnext(scan);
1047 } while (scan != NULL && OP(scan) == BRANCH);
1057 ln = ARG1(scan); /* min to match */
1058 n = ARG2(scan); /* max to match */
1059 scan = NEXTOPER(scan) + 4;
1064 scan = NEXTOPER(scan);
1068 * Lookahead to avoid useless match attempts
1069 * when we know what character comes next.
1073 scan = NEXTOPER(scan);
1075 if (regkind[(U8)OP(next)] == EXACT) {
1076 c1 = UCHARAT(OPERAND(next) + 1);
1077 if (OP(next) == EXACTF)
1079 else if (OP(next) == EXACTFL)
1080 c2 = fold_locale[c1];
1086 reginput = locinput;
1089 if (ln && regrepeat(scan, ln) < ln)
1091 while (n >= ln || (n == 32767 && ln > 0)) { /* ln overflow ? */
1092 /* If it could work, try it. */
1094 UCHARAT(reginput) == c1 ||
1095 UCHARAT(reginput) == c2)
1100 /* Couldn't or didn't -- back up. */
1101 reginput = locinput + ln;
1102 if (regrepeat(scan, 1)) {
1104 reginput = locinput + ln;
1111 n = regrepeat(scan, n);
1112 if (ln < n && regkind[(U8)OP(next)] == EOL &&
1113 (!multiline || OP(next) == SEOL))
1114 ln = n; /* why back off? */
1116 /* If it could work, try it. */
1118 UCHARAT(reginput) == c1 ||
1119 UCHARAT(reginput) == c2)
1124 /* Couldn't or didn't -- back up. */
1126 reginput = locinput + n;
1132 reginput = locinput; /* put where regtry can find it */
1133 sayYES; /* Success! */
1135 reginput = locinput;
1136 scan = NEXTOPER(scan);
1137 if (!regmatch(scan))
1141 reginput = locinput;
1142 scan = NEXTOPER(scan);
1147 PerlIO_printf(PerlIO_stderr(), "%lx %d\n",
1148 (unsigned long)scan, scan[1]);
1149 FAIL("regexp memory corruption");
1155 * We get here only if there's trouble -- normally "case END" is
1156 * the terminating point.
1158 FAIL("corrupted regexp pointers");
1176 - regrepeat - repeatedly match something simple, report how many
1179 * [This routine now assumes that it will only match on things of length 1.
1180 * That was true before, but now we assume scan - reginput is the count,
1181 * rather than incrementing count on every character.]
1184 regrepeat(char *p, I32 max)
1186 register char *scan;
1187 register char *opnd;
1189 register char *loceol = regeol;
1192 if (max != 32767 && max < loceol - scan)
1193 loceol = scan + max;
1197 while (scan < loceol && *scan != '\n')
1203 case EXACT: /* length of string is 1 */
1204 c = UCHARAT(++opnd);
1205 while (scan < loceol && UCHARAT(scan) == c)
1208 case EXACTF: /* length of string is 1 */
1209 c = UCHARAT(++opnd);
1210 while (scan < loceol &&
1211 (UCHARAT(scan) == c || UCHARAT(scan) == fold[c]))
1214 case EXACTFL: /* length of string is 1 */
1216 c = UCHARAT(++opnd);
1217 while (scan < loceol &&
1218 (UCHARAT(scan) == c || UCHARAT(scan) == fold_locale[c]))
1222 while (scan < loceol && reginclass(opnd, *scan))
1226 while (scan < loceol && isALNUM(*scan))
1231 while (scan < loceol && isALNUM_LC(*scan))
1235 while (scan < loceol && !isALNUM(*scan))
1240 while (scan < loceol && !isALNUM_LC(*scan))
1244 while (scan < loceol && isSPACE(*scan))
1249 while (scan < loceol && isSPACE_LC(*scan))
1253 while (scan < loceol && !isSPACE(*scan))
1258 while (scan < loceol && !isSPACE_LC(*scan))
1262 while (scan < loceol && isDIGIT(*scan))
1266 while (scan < loceol && !isDIGIT(*scan))
1269 default: /* Called on something of 0 width. */
1270 break; /* So match right here or not at all. */
1273 c = scan - reginput;
1280 - regclass - determine if a character falls into a character class
1284 reginclass(register char *p, register I32 c)
1290 if (p[1 + (c >> 3)] & (1 << (c & 7)))
1292 else if (flags & ANYOF_FOLD) {
1294 if (flags & ANYOF_LOCALE) {
1296 cf = fold_locale[c];
1300 if (p[1 + (cf >> 3)] & (1 << (cf & 7)))
1304 if (!match && (flags & ANYOF_ISA)) {
1307 if (((flags & ANYOF_ALNUML) && isALNUM_LC(c)) ||
1308 ((flags & ANYOF_NALNUML) && !isALNUM_LC(c)) ||
1309 ((flags & ANYOF_SPACEL) && isSPACE_LC(c)) ||
1310 ((flags & ANYOF_NSPACEL) && !isSPACE_LC(c)))
1316 return match ^ ((flags & ANYOF_INVERT) != 0);
1320 - regnext - dig the "next" pointer out of a node
1322 * [Note, when REGALIGN is defined there are two places in regmatch()
1323 * that bypass this code for speed.]
1326 regnext(register char *p)
1328 register I32 offset;