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 CHECKPOINT regcppush _((I32 parenfloor));
86 char * regcppop _((void));
93 int retval = savestack_ix;
94 int i = (regsize - parenfloor) * 3;
98 for (p = regsize; p > parenfloor; p--) {
99 SSPUSHPTR(regendp[p]);
100 SSPUSHPTR(regstartp[p]);
104 SSPUSHINT(*reglastparen);
107 SSPUSHINT(SAVEt_REGCONTEXT);
119 assert(i == SAVEt_REGCONTEXT);
121 input = (char *) SSPOPPTR;
122 *reglastparen = SSPOPINT;
124 for (i -= 3; i > 0; i -= 3) {
125 paren = (U32)SSPOPINT;
126 regstartp[paren] = (char *) SSPOPPTR;
127 tmps = (char*)SSPOPPTR;
128 if (paren <= *reglastparen)
129 regendp[paren] = tmps;
131 for (paren = *reglastparen + 1; paren <= regnpar; paren++) {
133 regstartp[paren] = Nullch;
134 regendp[paren] = Nullch;
139 #define regcpblow(cp) leave_scope(cp)
142 * pregexec and friends
149 static I32 regmatch _((char *prog));
150 static I32 regrepeat _((char *p, I32 max));
151 static I32 regtry _((regexp *prog, char *startpos));
154 - pregexec - match a regexp against a string
157 pregexec(prog, stringarg, strend, strbeg, minend, screamer, safebase)
158 register regexp *prog;
160 register char *strend; /* pointer to null at end of string */
161 char *strbeg; /* real beginning of string */
162 I32 minend; /* end of match must be at least minend after stringarg */
164 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 */
197 regprecomp = prog->precomp;
198 regnpar = prog->nparens;
199 /* Check validity of program. */
200 if (UCHARAT(prog->program) != MAGIC) {
201 FAIL("corrupted regexp program");
204 if (prog->do_folding) {
205 i = strend - startpos;
206 New(1101,c,i+1,char);
207 Copy(startpos, c, i+1, char);
209 strend = startpos + i;
210 for (s = startpos; s < strend; s++)
215 /* If there is a "must appear" string, look for it. */
217 if (prog->regmust != Nullsv &&
218 (!(prog->reganch & ROPT_ANCH)
219 || (multiline && prog->regback >= 0)) )
221 if (stringarg == strbeg && screamer) {
222 if (screamfirst[BmRARE(prog->regmust)] >= 0)
223 s = screaminstr(screamer,prog->regmust);
228 s = fbm_instr((unsigned char*)s, (unsigned char*)strend,
231 ++BmUSEFUL(prog->regmust); /* hooray */
232 goto phooey; /* not present */
234 else if (prog->regback >= 0) {
238 minlen = prog->regback + SvCUR(prog->regmust);
240 else if (!prog->naughty && --BmUSEFUL(prog->regmust) < 0) { /* boo */
241 SvREFCNT_dec(prog->regmust);
242 prog->regmust = Nullsv; /* disable regmust */
247 minlen = SvCUR(prog->regmust);
251 /* Mark beginning of line for ^ . */
254 /* Mark end of line for $ (and such) */
257 /* see how far we have to get to not match where we matched before */
258 regtill = startpos+minend;
260 /* Simplest case: anchored match need be tried only once. */
261 /* [unless multiline is set] */
262 if (prog->reganch & ROPT_ANCH) {
263 if (regtry(prog, startpos))
265 else if (multiline || (prog->reganch & ROPT_IMPLICIT)) {
267 dontbother = minlen - 1;
268 strend -= dontbother;
269 /* for multiline we only have to try after newlines */
274 if (s < strend && regtry(prog, s))
282 /* Messy cases: unanchored match. */
283 if (prog->regstart) {
284 if (prog->reganch & ROPT_SKIP) { /* we have /x+whatever/ */
285 /* it must be a one character string */
286 i = SvPVX(prog->regstart)[0];
292 while (s < strend && *s == i)
298 else if (SvPOK(prog->regstart) == 3) {
299 /* We know what string it must start with. */
300 while ((s = fbm_instr((unsigned char*)s,
301 (unsigned char*)strend, prog->regstart)) != NULL)
309 c = SvPVX(prog->regstart);
310 while ((s = ninstr(s, strend, c, c + SvCUR(prog->regstart))) != NULL)
320 if (c = prog->regstclass) {
321 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
324 dontbother = minlen - 1;
325 strend -= dontbother; /* don't bother with what can't match */
327 /* We know what class it must start with. */
333 if (!(c[i >> 3] & (1 << (i&7)))) {
334 if (tmp && regtry(prog, s))
346 dontbother++,strend--;
352 tmp = isALNUM(regprev); /* assume not alphanumeric */
355 if (tmp != isALNUM(i)) {
362 if ((minlen || tmp) && regtry(prog,s))
367 dontbother++,strend--;
373 tmp = isALNUM(regprev); /* assume not alphanumeric */
376 if (tmp != isALNUM(i))
378 else if (regtry(prog, s))
382 if ((minlen || !tmp) && regtry(prog,s))
389 if (tmp && regtry(prog, s))
403 if (tmp && regtry(prog, s))
416 if (tmp && regtry(prog, s))
429 if (tmp && regtry(prog, s))
442 if (tmp && regtry(prog, s))
455 if (tmp && regtry(prog, s))
469 dontbother = minlen - 1;
470 strend -= dontbother;
471 /* We don't know much -- general case. */
475 } while (s++ < strend);
482 strend += dontbother; /* uncheat */
483 prog->subbeg = strbeg;
484 prog->subend = strend;
485 if ((!safebase && (prog->nparens || sawampersand)) || prog->do_folding) {
486 i = strend - startpos + (stringarg - strbeg);
487 if (safebase) { /* no need for $digit later */
491 else if (strbeg != prog->subbase) {
492 s = savepvn(strbeg,i); /* so $digit will work later */
494 Safefree(prog->subbase);
495 prog->subbeg = prog->subbase = s;
499 prog->subbeg = s = prog->subbase;
502 s += (stringarg - strbeg);
503 for (i = 0; i <= prog->nparens; i++) {
505 prog->startp[i] = s + (prog->startp[i] - startpos);
506 prog->endp[i] = s + (prog->endp[i] - startpos);
509 if (prog->do_folding)
515 if (prog->do_folding)
521 - regtry - try match at specific point
523 static I32 /* 0 failure, 1 success */
524 regtry(prog, startpos)
533 regstartp = prog->startp;
534 regendp = prog->endp;
535 reglastparen = &prog->lastparen;
542 for (i = prog->nparens; i >= 0; i--) {
547 if (regmatch(prog->program + 1) && reginput >= regtill) {
548 prog->startp[0] = startpos;
549 prog->endp[0] = reginput;
557 - regmatch - main matching routine
559 * Conceptually the strategy is simple: check to see whether the current
560 * node matches, call self recursively to see whether the rest matches,
561 * and then act accordingly. In practice we make some effort to avoid
562 * recursion, in particular by going through "ordinary" nodes (that don't
563 * need to know whether the rest of the match failed) by a loop instead of
566 /* [lwall] I've hoisted the register declarations to the outer block in order to
567 * maybe save a little bit of pushing and popping on the stack. It also takes
568 * advantage of machines that use a register save mask on subroutine entry.
570 static I32 /* 0 failure, 1 success */
574 register char *scan; /* Current node. */
575 char *next; /* Next node. */
576 register I32 nextchar;
577 register I32 n; /* no or next */
578 register I32 ln; /* len or last */
579 register char *s; /* operand or save */
580 register char *locinput = reginput;
583 static int regindent = 0;
587 nextchar = *locinput;
589 while (scan != NULL) {
591 #define sayYES goto yes
592 #define sayNO goto no
593 #define saySAME(x) if (x) goto yes; else goto no
595 fprintf(stderr, "%*s%2d%-8.8s\t<%.10s>\n", regindent*2, "",
596 scan - regprogram, regprop(scan), locinput);
599 #define sayYES return 1
600 #define sayNO return 0
601 #define saySAME(x) return x
605 next = scan + NEXT(scan);
609 next = regnext(scan);
614 if (locinput == regbol
616 : ((nextchar || locinput < regeol) && locinput[-1] == '\n') )
618 /* regtill = regbol; */
623 if (locinput == regbol
625 : ((nextchar || locinput < regeol) && locinput[-1] == '\n') )
631 if (locinput == regbol && regprev == '\n')
635 if (locinput == regbol)
645 if ((nextchar || locinput < regeol) && nextchar != '\n')
650 if ((nextchar || locinput < regeol) && nextchar != '\n')
652 if (regeol - locinput > 1)
656 if (!nextchar && locinput >= regeol)
658 nextchar = *++locinput;
661 if (!nextchar && locinput >= regeol || nextchar == '\n')
663 nextchar = *++locinput;
668 /* Inline the first character, for speed. */
671 if (regeol - locinput < ln)
673 if (ln > 1 && bcmp(s, locinput, ln) != 0)
676 nextchar = *locinput;
681 nextchar = UCHARAT(locinput);
682 if (s[nextchar >> 3] & (1 << (nextchar&7)))
684 if (!nextchar && locinput >= regeol)
686 nextchar = *++locinput;
691 if (!isALNUM(nextchar))
693 nextchar = *++locinput;
696 if (!nextchar && locinput >= regeol)
698 if (isALNUM(nextchar))
700 nextchar = *++locinput;
704 if (locinput == regbol) /* was last char in word? */
705 ln = isALNUM(regprev);
707 ln = isALNUM(locinput[-1]);
708 n = isALNUM(nextchar); /* is next char in word? */
709 if ((ln == n) == (OP(scan) == BOUND))
713 if (!nextchar && locinput >= regeol)
715 if (!isSPACE(nextchar))
717 nextchar = *++locinput;
722 if (isSPACE(nextchar))
724 nextchar = *++locinput;
727 if (!isDIGIT(nextchar))
729 nextchar = *++locinput;
732 if (!nextchar && locinput >= regeol)
734 if (isDIGIT(nextchar))
736 nextchar = *++locinput;
739 n = ARG1(scan); /* which paren pair */
747 /* Inline the first character, for speed. */
751 if (locinput + ln > regeol)
753 if (ln > 1 && bcmp(s, locinput, ln) != 0)
756 nextchar = *locinput;
764 n = ARG1(scan); /* which paren pair */
765 regstartp[n] = locinput;
770 n = ARG1(scan); /* which paren pair */
771 regendp[n] = locinput;
772 if (n > *reglastparen)
778 CHECKPOINT cp = savestack_ix;
781 cc.parenfloor = *reglastparen;
785 cc.scan = NEXTOPER(scan) + 4;
790 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
798 * This is really hard to understand, because after we match
799 * what we're trying to match, we must make sure the rest of
800 * the RE is going to match for sure, and to do that we have
801 * to go back UP the parse tree by recursing ever deeper. And
802 * if it fails, we have to reset our parent's current state
803 * that we can try again after backing off.
807 n = cc->cur + 1; /* how many we know we matched */
812 fprintf(stderr, "%*s %d %lx\n", regindent*2, "",
816 /* If degenerate scan matches "", assume scan done. */
818 if (locinput == cc->lastloc) {
821 if (regmatch(cc->next))
828 /* First just match a string of min scans. */
832 cc->lastloc = locinput;
833 if (regmatch(cc->scan))
839 /* Prefer next over scan for minimal matching. */
844 if (regmatch(cc->next))
845 sayYES; /* All done. */
849 if (n >= cc->max) /* Maximum greed exceeded? */
852 /* Try scanning more and see if it helps. */
855 cc->lastloc = locinput;
856 if (regmatch(cc->scan))
862 /* Prefer scan over next for maximal matching. */
864 if (n < cc->max) { /* More greed allowed? */
865 regcppush(cc->parenfloor);
867 cc->lastloc = locinput;
868 if (regmatch(cc->scan))
870 regcppop(); /* Restore some previous $<digit>s? */
874 /* Failed deeper matches of scan, so see if this one works. */
877 if (regmatch(cc->next))
886 if (OP(next) != BRANCH) /* No choice. */
887 next = NEXTOPER(scan);/* Avoid recursion. */
889 int lastparen = *reglastparen;
892 if (regmatch(NEXTOPER(scan)))
894 for (n = *reglastparen; n > lastparen; n--)
905 scan = regnext(scan);
907 } while (scan != NULL && OP(scan) == BRANCH);
917 ln = ARG1(scan); /* min to match */
918 n = ARG2(scan); /* max to match */
919 scan = NEXTOPER(scan) + 4;
924 scan = NEXTOPER(scan);
928 * Lookahead to avoid useless match attempts
929 * when we know what character comes next.
933 scan = NEXTOPER(scan);
935 if (OP(next) == EXACTLY)
936 nextchar = *(OPERAND(next)+1);
942 if (ln && regrepeat(scan, ln) < ln)
944 while (n >= ln || (n == 32767 && ln > 0)) { /* ln overflow ? */
945 /* If it could work, try it. */
946 if (nextchar == -1000 || *reginput == nextchar)
949 /* Couldn't or didn't -- back up. */
950 reginput = locinput + ln;
951 if (regrepeat(scan, 1)) {
953 reginput = locinput + ln;
960 n = regrepeat(scan, n);
961 if (ln < n && regkind[(U8)OP(next)] == EOL &&
962 (!multiline || OP(next) == SEOL))
963 ln = n; /* why back off? */
965 /* If it could work, try it. */
966 if (nextchar == -1000 || *reginput == nextchar)
969 /* Couldn't or didn't -- back up. */
971 reginput = locinput + n;
977 reginput = locinput; /* put where regtry can find it */
978 sayYES; /* Success! */
981 scan = NEXTOPER(scan);
987 scan = NEXTOPER(scan);
992 fprintf(stderr, "%x %d\n",(unsigned)scan,scan[1]);
993 FAIL("regexp memory corruption");
999 * We get here only if there's trouble -- normally "case END" is
1000 * the terminating point.
1002 FAIL("corrupted regexp pointers");
1020 - regrepeat - repeatedly match something simple, report how many
1023 * [This routine now assumes that it will only match on things of length 1.
1024 * That was true before, but now we assume scan - reginput is the count,
1025 * rather than incrementing count on every character.]
1032 register char *scan;
1033 register char *opnd;
1035 register char *loceol = regeol;
1038 if (max != 32767 && max < loceol - scan)
1039 loceol = scan + max;
1043 while (scan < loceol && *scan != '\n')
1049 case EXACTLY: /* length of string is 1 */
1051 while (scan < loceol && *opnd == *scan)
1056 while (scan < loceol && !(opnd[c >> 3] & (1 << (c & 7)))) {
1062 while (scan < loceol && isALNUM(*scan))
1066 while (scan < loceol && !isALNUM(*scan))
1070 while (scan < loceol && isSPACE(*scan))
1074 while (scan < loceol && !isSPACE(*scan))
1078 while (scan < loceol && isDIGIT(*scan))
1082 while (scan < loceol && !isDIGIT(*scan))
1085 default: /* Called on something of 0 width. */
1086 break; /* So match right here or not at all. */
1089 c = scan - reginput;
1096 - regnext - dig the "next" pointer out of a node
1098 * [Note, when REGALIGN is defined there are two places in regmatch()
1099 * that bypass this code for speed.]
1105 register I32 offset;