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.
19 * regcomp and regexec -- regsub and regerror are not used in perl
21 * Copyright (c) 1986 by University of Toronto.
22 * Written by Henry Spencer. Not derived from licensed software.
24 * Permission is granted to anyone to use this software for any
25 * purpose on any computer system, and to redistribute it freely,
26 * subject to the following restrictions:
28 * 1. The author is not responsible for the consequences of use of
29 * this software, no matter how awful, even if they arise
32 * 2. The origin of this software must not be misrepresented, either
33 * by explicit claim or by omission.
35 * 3. Altered versions must be plainly marked as such, and must not
36 * be misrepresented as being the original software.
38 **** Alterations to Henry's code are...
40 **** Copyright (c) 1991-1994, Larry Wall
42 **** You may distribute under the terms of either the GNU General Public
43 **** License or the Artistic License, as specified in the README file.
45 * Beware that some of this code is subtly aware of the way operator
46 * precedence is structured in regular expressions. Serious changes in
47 * regular-expression syntax might require a total rethink.
58 static I32 regnarrate = 0;
59 static char* regprogram = 0;
62 /* Current curly descriptor */
63 typedef struct curcur CURCUR;
65 int parenfloor; /* how far back to strip paren data */
66 int cur; /* how many instances of scan we've matched */
67 int min; /* the minimal number of scans to match */
68 int max; /* the maximal number of scans to match */
69 int minmod; /* whether to work our way up or down */
70 char * scan; /* the thing to match */
71 char * next; /* what has to match after it */
72 char * lastloc; /* where we started matching this scan */
73 CURCUR * oldcc; /* current curly before we started this one */
78 typedef I32 CHECKPOINT;
80 CHECKPOINT regcppush _((I32 parenfloor));
81 char * regcppop _((void));
87 int retval = savestack_ix;
88 int i = (regsize - parenfloor) * 3;
92 for (p = regsize; p > parenfloor; p--) {
93 SSPUSHPTR(regendp[p]);
94 SSPUSHPTR(regstartp[p]);
98 SSPUSHINT(*reglastparen);
101 SSPUSHINT(SAVEt_REGCONTEXT);
112 assert(i == SAVEt_REGCONTEXT);
114 input = (char *) SSPOPPTR;
115 *reglastparen = SSPOPINT;
117 for (i -= 3; i > 0; i -= 3) {
118 paren = (U32)SSPOPINT;
119 regstartp[paren] = (char *) SSPOPPTR;
120 tmps = (char*)SSPOPPTR;
121 if (paren <= *reglastparen)
122 regendp[paren] = tmps;
124 for (paren = *reglastparen + 1; paren <= regnpar; paren++) {
126 regstartp[paren] = Nullch;
127 regendp[paren] = Nullch;
132 #define regcpblow(cp) leave_scope(cp)
135 * regexec and friends
142 static I32 regmatch _((char *prog));
143 static I32 regrepeat _((char *p, I32 max));
144 static I32 regtry _((regexp *prog, char *startpos));
147 - regexec - match a regexp against a string
150 regexec(prog, stringarg, strend, strbeg, minend, screamer, safebase)
151 register regexp *prog;
153 register char *strend; /* pointer to null at end of string */
154 char *strbeg; /* real beginning of string */
155 I32 minend; /* end of match must be at least minend after stringarg */
157 I32 safebase; /* no need to remember string in subbase */
162 register char *startpos = stringarg;
164 I32 minlen = 0; /* must match at least this many chars */
165 I32 dontbother = 0; /* how many characters not to try at end */
172 regnarrate = debug & 512;
173 regprogram = prog->program;
177 if (prog == NULL || startpos == NULL) {
178 croak("NULL regexp parameter");
182 if (startpos == strbeg) /* is ^ valid at stringarg? */
185 regprev = stringarg[-1];
186 if (!multiline && regprev == '\n')
187 regprev = '\0'; /* force ^ to NOT match */
189 regprecomp = prog->precomp;
190 regnpar = prog->nparens;
191 /* Check validity of program. */
192 if (UCHARAT(prog->program) != MAGIC) {
193 FAIL("corrupted regexp program");
196 if (prog->do_folding) {
197 i = strend - startpos;
198 New(1101,c,i+1,char);
199 Copy(startpos, c, i+1, char);
201 strend = startpos + i;
202 for (s = startpos; s < strend; s++)
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 i = SvPVX(prog->regstart)[0];
284 while (s < strend && *s == i)
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. */
325 if (!(c[i >> 3] & (1 << (i&7)))) {
326 if (tmp && regtry(prog, s))
338 dontbother++,strend--;
344 tmp = isALNUM(regprev); /* assume not alphanumeric */
347 if (tmp != isALNUM(i)) {
354 if ((minlen || tmp) && regtry(prog,s))
359 dontbother++,strend--;
365 tmp = isALNUM(regprev); /* assume not alphanumeric */
368 if (tmp != isALNUM(i))
370 else if (regtry(prog, s))
374 if ((minlen || !tmp) && regtry(prog,s))
381 if (tmp && regtry(prog, s))
395 if (tmp && regtry(prog, s))
408 if (tmp && regtry(prog, s))
421 if (tmp && regtry(prog, s))
434 if (tmp && regtry(prog, s))
447 if (tmp && regtry(prog, s))
461 dontbother = minlen - 1;
462 strend -= dontbother;
463 /* We don't know much -- general case. */
467 } while (s++ < strend);
474 prog->subbeg = strbeg;
475 prog->subend = strend;
476 if ((!safebase && (prog->nparens || sawampersand)) || prog->do_folding) {
477 strend += dontbother; /* uncheat */
478 i = strend - startpos + (stringarg - strbeg);
479 if (safebase) { /* no need for $digit later */
483 else if (strbeg != prog->subbase) {
484 s = savepvn(strbeg,i); /* so $digit will work later */
486 Safefree(prog->subbase);
487 prog->subbeg = prog->subbase = s;
491 prog->subbeg = s = prog->subbase;
494 s += (stringarg - strbeg);
495 for (i = 0; i <= prog->nparens; i++) {
497 prog->startp[i] = s + (prog->startp[i] - startpos);
498 prog->endp[i] = s + (prog->endp[i] - startpos);
501 if (prog->do_folding)
507 if (prog->do_folding)
513 - regtry - try match at specific point
515 static I32 /* 0 failure, 1 success */
516 regtry(prog, startpos)
525 regstartp = prog->startp;
526 regendp = prog->endp;
527 reglastparen = &prog->lastparen;
534 for (i = prog->nparens; i >= 0; i--) {
539 if (regmatch(prog->program + 1) && reginput >= regtill) {
540 prog->startp[0] = startpos;
541 prog->endp[0] = reginput;
549 - regmatch - main matching routine
551 * Conceptually the strategy is simple: check to see whether the current
552 * node matches, call self recursively to see whether the rest matches,
553 * and then act accordingly. In practice we make some effort to avoid
554 * recursion, in particular by going through "ordinary" nodes (that don't
555 * need to know whether the rest of the match failed) by a loop instead of
558 /* [lwall] I've hoisted the register declarations to the outer block in order to
559 * maybe save a little bit of pushing and popping on the stack. It also takes
560 * advantage of machines that use a register save mask on subroutine entry.
562 static I32 /* 0 failure, 1 success */
566 register char *scan; /* Current node. */
567 char *next; /* Next node. */
568 register I32 nextchar;
569 register I32 n; /* no or next */
570 register I32 ln; /* len or last */
571 register char *s; /* operand or save */
572 register char *locinput = reginput;
575 nextchar = *locinput;
577 while (scan != NULL) {
580 fprintf(stderr, "%2d%-8.8s\t<%.10s>\n",
581 scan - regprogram, regprop(scan), locinput);
585 next = scan + NEXT(scan);
589 next = regnext(scan);
594 if (locinput == regbol
596 : ((nextchar || locinput < regeol) && locinput[-1] == '\n') )
598 /* regtill = regbol; */
603 if (locinput == regbol
605 : ((nextchar || locinput < regeol) && locinput[-1] == '\n') )
611 if (locinput == regbol && regprev == '\n')
615 if (locinput == regbol)
625 if ((nextchar || locinput < regeol) && nextchar != '\n')
630 if ((nextchar || locinput < regeol) && nextchar != '\n')
632 if (regeol - locinput > 1)
636 if (!nextchar && locinput >= regeol)
638 nextchar = *++locinput;
641 if (!nextchar && locinput >= regeol || nextchar == '\n')
643 nextchar = *++locinput;
648 /* Inline the first character, for speed. */
651 if (regeol - locinput < ln)
653 if (ln > 1 && bcmp(s, locinput, ln) != 0)
656 nextchar = *locinput;
661 nextchar = UCHARAT(locinput);
662 if (s[nextchar >> 3] & (1 << (nextchar&7)))
664 if (!nextchar && locinput >= regeol)
666 nextchar = *++locinput;
671 if (!isALNUM(nextchar))
673 nextchar = *++locinput;
676 if (!nextchar && locinput >= regeol)
678 if (isALNUM(nextchar))
680 nextchar = *++locinput;
684 if (locinput == regbol) /* was last char in word? */
685 ln = isALNUM(regprev);
687 ln = isALNUM(locinput[-1]);
688 n = isALNUM(nextchar); /* is next char in word? */
689 if ((ln == n) == (OP(scan) == BOUND))
693 if (!nextchar && locinput >= regeol)
695 if (!isSPACE(nextchar))
697 nextchar = *++locinput;
702 if (isSPACE(nextchar))
704 nextchar = *++locinput;
707 if (!isDIGIT(nextchar))
709 nextchar = *++locinput;
712 if (!nextchar && locinput >= regeol)
714 if (isDIGIT(nextchar))
716 nextchar = *++locinput;
719 n = ARG1(scan); /* which paren pair */
727 /* Inline the first character, for speed. */
731 if (locinput + ln > regeol)
733 if (ln > 1 && bcmp(s, locinput, ln) != 0)
736 nextchar = *locinput;
744 n = ARG1(scan); /* which paren pair */
745 regstartp[n] = locinput;
750 n = ARG1(scan); /* which paren pair */
751 regendp[n] = locinput;
752 if (n > *reglastparen)
757 CHECKPOINT cp = savestack_ix;
760 cc.parenfloor = *reglastparen;
764 cc.scan = NEXTOPER(scan) + 4;
769 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
777 * This is really hard to understand, because after we match
778 * what we're trying to match, we must make sure the rest of
779 * the RE is going to match for sure, and to do that we have
780 * to go back UP the parse tree by recursing ever deeper. And
781 * if it fails, we have to reset our parent's current state
782 * that we can try again after backing off.
789 /* If degenerate scan matches "", assume scan done. */
791 if (locinput == cc->lastloc) {
794 if (regmatch(cc->next))
801 /* First just match a string of min scans. */
805 cc->lastloc = locinput;
806 return regmatch(cc->scan);
809 /* Prefer next over scan for minimal matching. */
814 if (regmatch(cc->next))
815 return TRUE; /* All done. */
819 if (n >= cc->max) /* Maximum greed exceeded? */
822 /* Try scanning more and see if it helps. */
825 cc->lastloc = locinput;
826 return regmatch(cc->scan);
829 /* Prefer scan over next for maximal matching. */
831 if (n < cc->max) { /* More greed allowed? */
832 regcppush(cc->parenfloor);
834 cc->lastloc = locinput;
835 if (regmatch(cc->scan))
837 regcppop(); /* Restore some previous $<digit>s? */
841 /* Failed deeper matches of scan, so see if this one works. */
844 if (regmatch(cc->next))
852 if (OP(next) != BRANCH) /* No choice. */
853 next = NEXTOPER(scan);/* Avoid recursion. */
857 if (regmatch(NEXTOPER(scan)))
866 scan = regnext(scan);
868 } while (scan != NULL && OP(scan) == BRANCH);
878 ln = ARG1(scan); /* min to match */
879 n = ARG2(scan); /* max to match */
880 scan = NEXTOPER(scan) + 4;
885 scan = NEXTOPER(scan);
889 * Lookahead to avoid useless match attempts
890 * when we know what character comes next.
894 scan = NEXTOPER(scan);
896 if (OP(next) == EXACTLY)
897 nextchar = *(OPERAND(next)+1);
903 if (ln && regrepeat(scan, ln) < ln)
906 /* If it could work, try it. */
907 if (nextchar == -1000 || *reginput == nextchar)
910 /* Couldn't or didn't -- back up. */
911 if (regrepeat(scan, 1)) {
913 reginput = locinput + ln;
920 n = regrepeat(scan, n);
921 if (ln < n && regkind[(U8)OP(next)] == EOL &&
922 (!multiline || OP(next) == SEOL))
923 ln = n; /* why back off? */
925 /* If it could work, try it. */
926 if (nextchar == -1000 || *reginput == nextchar)
929 /* Couldn't or didn't -- back up. */
931 reginput = locinput + n;
937 reginput = locinput; /* put where regtry can find it */
938 return 1; /* Success! */
941 scan = NEXTOPER(scan);
947 scan = NEXTOPER(scan);
952 fprintf(stderr, "%x %d\n",(unsigned)scan,scan[1]);
953 FAIL("regexp memory corruption");
959 * We get here only if there's trouble -- normally "case END" is
960 * the terminating point.
962 FAIL("corrupted regexp pointers");
968 - regrepeat - repeatedly match something simple, report how many
971 * [This routine now assumes that it will only match on things of length 1.
972 * That was true before, but now we assume scan - reginput is the count,
973 * rather than incrementing count on every character.]
983 register char *loceol = regeol;
986 if (max != 32767 && max < loceol - scan)
991 while (scan < loceol && *scan != '\n')
997 case EXACTLY: /* length of string is 1 */
999 while (scan < loceol && *opnd == *scan)
1004 while (scan < loceol && !(opnd[c >> 3] & (1 << (c & 7)))) {
1010 while (scan < loceol && isALNUM(*scan))
1014 while (scan < loceol && !isALNUM(*scan))
1018 while (scan < loceol && isSPACE(*scan))
1022 while (scan < loceol && !isSPACE(*scan))
1026 while (scan < loceol && isDIGIT(*scan))
1030 while (scan < loceol && !isDIGIT(*scan))
1033 default: /* Called on something of 0 width. */
1034 break; /* So match right here or not at all. */
1037 c = scan - reginput;
1044 - regnext - dig the "next" pointer out of a node
1046 * [Note, when REGALIGN is defined there are two places in regmatch()
1047 * that bypass this code for speed.]
1053 register I32 offset;