1 /* NOTE: this is derived from Henry Spencer's regexp code, and should not
2 * confused with the original package (see point 3 below). Thanks, Henry!
5 /* Additional note: this code is very heavily munged from Henry's version
6 * in places. In some spots I've traded clarity for efficiency, so don't
7 * blame Henry for some of the lack of readability.
10 /* $Header: regexec.c,v 4.0 91/03/20 01:39:16 lwall Locked $
13 * Revision 4.0 91/03/20 01:39:16 lwall
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) 1989, Larry Wall
42 **** You may distribute under the terms of the GNU General Public License
43 **** as specified in the README file that comes with the perl 3.0 kit.
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.
61 #define isALNUM(c) (isascii(c) && (isalpha(c) || isdigit(c) || c == '_'))
62 #define isSPACE(c) (isascii(c) && isspace(c))
63 #define isDIGIT(c) (isascii(c) && isdigit(c))
64 #define isUPPER(c) (isascii(c) && isupper(c))
71 * Global work variables for regexec().
73 static char *regprecomp;
74 static char *reginput; /* String-input pointer. */
75 static char regprev; /* char before regbol, \n if none */
76 static char *regbol; /* Beginning of input, for ^ check. */
77 static char *regeol; /* End of input, for $ check. */
78 static char **regstartp; /* Pointer to startp array. */
79 static char **regendp; /* Ditto for endp. */
80 static char *reglastparen; /* Similarly for lastparen. */
83 static char *regmystartp[10]; /* For remembering backreferences. */
84 static char *regmyendp[10];
90 STATIC int regmatch();
91 STATIC int regrepeat();
96 - regexec - match a regexp against a string
99 regexec(prog, stringarg, strend, strbeg, minend, screamer, safebase)
100 register regexp *prog;
102 register char *strend; /* pointer to null at end of string */
103 char *strbeg; /* real beginning of string */
104 int minend; /* end of match must be at least minend after stringarg */
106 int safebase; /* no need to remember string in subbase */
111 register char *string = stringarg;
113 int minlen = 0; /* must match at least this many chars */
114 int dontbother = 0; /* how many characters not to try at end */
117 if (prog == NULL || string == NULL) {
118 fatal("NULL regexp parameter");
122 if (string == strbeg) /* is ^ valid at stringarg? */
125 regprev = stringarg[-1];
126 if (!multiline && regprev == '\n')
127 regprev = '\0'; /* force ^ to NOT match */
129 regprecomp = prog->precomp;
130 /* Check validity of program. */
131 if (UCHARAT(prog->program) != MAGIC) {
132 FAIL("corrupted regexp program");
135 if (prog->do_folding) {
138 New(1101,c,i+1,char);
139 (void)bcopy(string, c, i+1);
142 for (s = string; s < strend; s++)
147 /* If there is a "must appear" string, look for it. */
149 if (prog->regmust != Nullstr &&
150 (!(prog->reganch & 1) || (multiline && prog->regback >= 0)) ) {
151 if (stringarg == strbeg && screamer) {
152 if (screamfirst[prog->regmust->str_rare] >= 0)
153 s = screaminstr(screamer,prog->regmust);
159 s = fbminstr((unsigned char*)s, (unsigned char*)strend,
163 ++prog->regmust->str_u.str_useful; /* hooray */
164 goto phooey; /* not present */
166 else if (prog->regback >= 0) {
170 minlen = prog->regback + prog->regmust->str_cur;
172 else if (--prog->regmust->str_u.str_useful < 0) { /* boo */
173 str_free(prog->regmust);
174 prog->regmust = Nullstr; /* disable regmust */
179 minlen = prog->regmust->str_cur;
183 /* Mark beginning of line for ^ . */
186 /* Mark end of line for $ (and such) */
189 /* see how far we have to get to not match where we matched before */
190 regtill = string+minend;
192 /* Simplest case: anchored match need be tried only once. */
193 /* [unless multiline is set] */
194 if (prog->reganch & 1) {
195 if (regtry(prog, string))
197 else if (multiline) {
199 dontbother = minlen - 1;
200 strend -= dontbother;
201 /* for multiline we only have to try after newlines */
206 if (s < strend && regtry(prog, s))
214 /* Messy cases: unanchored match. */
215 if (prog->regstart) {
216 if (prog->reganch & 2) { /* we have /x+whatever/ */
217 /* it must be a one character string */
218 i = prog->regstart->str_ptr[0];
224 while (s < strend && *s == i)
230 else if (prog->regstart->str_pok == 3) {
231 /* We know what string it must start with. */
233 while ((s = fbminstr((unsigned char*)s,
234 (unsigned char*)strend, prog->regstart)) != NULL)
245 c = prog->regstart->str_ptr;
246 while ((s = ninstr(s, strend,
247 c, c + prog->regstart->str_cur )) != NULL) {
255 if (c = prog->regstclass) {
256 int doevery = (prog->reganch & 2) == 0;
259 dontbother = minlen - 1;
260 strend -= dontbother; /* don't bother with what can't match */
262 /* We know what class it must start with. */
268 if (!(c[i >> 3] & (1 << (i&7)))) {
269 if (tmp && regtry(prog, s))
281 dontbother++,strend--;
287 tmp = isALNUM(regprev); /* assume not alphanumeric */
290 if (tmp != isALNUM(i)) {
297 if ((minlen || tmp) && regtry(prog,s))
302 dontbother++,strend--;
308 tmp = isALNUM(regprev); /* assume not alphanumeric */
311 if (tmp != isALNUM(i))
313 else if (regtry(prog, s))
317 if ((minlen || !tmp) && regtry(prog,s))
324 if (tmp && regtry(prog, s))
338 if (tmp && regtry(prog, s))
351 if (tmp && regtry(prog, s))
364 if (tmp && regtry(prog, s))
377 if (tmp && regtry(prog, s))
390 if (tmp && regtry(prog, s))
404 dontbother = minlen - 1;
405 strend -= dontbother;
406 /* We don't know much -- general case. */
410 } while (s++ < strend);
417 if ((!safebase && (prog->nparens || sawampersand)) || prog->do_folding){
418 strend += dontbother; /* uncheat */
419 if (safebase) /* no need for $digit later */
421 else if (strbeg != prog->subbase) {
422 i = strend - string + (stringarg - strbeg);
423 s = nsavestr(strbeg,i); /* so $digit will work later */
425 Safefree(prog->subbase);
431 s += (stringarg - strbeg);
432 for (i = 0; i <= prog->nparens; i++) {
434 prog->startp[i] = s + (prog->startp[i] - string);
435 prog->endp[i] = s + (prog->endp[i] - string);
438 if (prog->do_folding)
444 if (prog->do_folding)
450 - regtry - try match at specific point
452 static int /* 0 failure, 1 success */
462 regstartp = prog->startp;
463 regendp = prog->endp;
464 reglastparen = &prog->lastparen;
470 for (i = prog->nparens; i >= 0; i--) {
475 if (regmatch(prog->program + 1) && reginput >= regtill) {
476 prog->startp[0] = string;
477 prog->endp[0] = reginput;
484 - regmatch - main matching routine
486 * Conceptually the strategy is simple: check to see whether the current
487 * node matches, call self recursively to see whether the rest matches,
488 * and then act accordingly. In practice we make some effort to avoid
489 * recursion, in particular by going through "ordinary" nodes (that don't
490 * need to know whether the rest of the match failed) by a loop instead of
493 /* [lwall] I've hoisted the register declarations to the outer block in order to
494 * maybe save a little bit of pushing and popping on the stack. It also takes
495 * advantage of machines that use a register save mask on subroutine entry.
497 static int /* 0 failure, 1 success */
501 register char *scan; /* Current node. */
502 char *next; /* Next node. */
503 register int nextchar;
504 register int n; /* no or next */
505 register int ln; /* len or last */
506 register char *s; /* operand or save */
507 register char *locinput = reginput;
509 nextchar = *locinput;
512 if (scan != NULL && regnarrate)
513 fprintf(stderr, "%s(\n", regprop(scan));
515 while (scan != NULL) {
518 fprintf(stderr, "%s...\n", regprop(scan));
522 next = scan + NEXT(scan);
526 next = regnext(scan);
531 if (locinput == regbol ? regprev == '\n' :
532 ((nextchar || locinput < regeol) &&
533 locinput[-1] == '\n') )
535 /* regtill = regbol; */
540 if ((nextchar || locinput < regeol) && nextchar != '\n')
542 if (!multiline && regeol - locinput > 1)
544 /* regtill = regbol; */
547 if ((nextchar == '\0' && locinput >= regeol) ||
550 nextchar = *++locinput;
555 /* Inline the first character, for speed. */
558 if (regeol - locinput < ln)
560 if (ln > 1 && bcmp(s, locinput, ln) != 0)
563 nextchar = *locinput;
568 nextchar = UCHARAT(locinput);
569 if (s[nextchar >> 3] & (1 << (nextchar&7)))
571 if (!nextchar && locinput >= regeol)
573 nextchar = *++locinput;
578 if (!isALNUM(nextchar))
580 nextchar = *++locinput;
583 if (!nextchar && locinput >= regeol)
585 if (isALNUM(nextchar))
587 nextchar = *++locinput;
591 if (locinput == regbol) /* was last char in word? */
592 ln = isALNUM(regprev);
594 ln = isALNUM(locinput[-1]);
595 n = isALNUM(nextchar); /* is next char in word? */
596 if ((ln == n) == (OP(scan) == BOUND))
600 if (!nextchar && locinput >= regeol)
602 if (!isSPACE(nextchar))
604 nextchar = *++locinput;
609 if (isSPACE(nextchar))
611 nextchar = *++locinput;
614 if (!isDIGIT(nextchar))
616 nextchar = *++locinput;
619 if (!nextchar && locinput >= regeol)
621 if (isDIGIT(nextchar))
623 nextchar = *++locinput;
626 n = ARG1(scan); /* which paren pair */
632 if (s == regmyendp[n])
634 /* Inline the first character, for speed. */
637 ln = regmyendp[n] - s;
638 if (locinput + ln > regeol)
640 if (ln > 1 && bcmp(s, locinput, ln) != 0)
643 nextchar = *locinput;
651 n = ARG1(scan); /* which paren pair */
654 regmystartp[n] = locinput; /* for REF */
655 if (regmatch(next)) {
657 * Don't set startp if some later
658 * invocation of the same parentheses
661 if (regstartp[n] == NULL)
662 regstartp[n] = locinput;
668 n = ARG1(scan); /* which paren pair */
671 regmyendp[n] = locinput; /* for REF */
672 if (regmatch(next)) {
674 * Don't set endp if some later
675 * invocation of the same parentheses
678 if (regendp[n] == NULL) {
679 regendp[n] = locinput;
680 if (n > *reglastparen)
689 if (OP(next) != BRANCH) /* No choice. */
690 next = NEXTOPER(scan); /* Avoid recursion. */
694 if (regmatch(NEXTOPER(scan)))
702 scan = regnext(scan);
704 } while (scan != NULL && OP(scan) == BRANCH);
711 ln = ARG1(scan); /* min to match */
712 n = ARG2(scan); /* max to match */
713 scan = NEXTOPER(scan) + 4;
718 scan = NEXTOPER(scan);
722 * Lookahead to avoid useless match attempts
723 * when we know what character comes next.
727 scan = NEXTOPER(scan);
729 if (OP(next) == EXACTLY)
730 nextchar = *(OPERAND(next)+1);
734 n = regrepeat(scan, n);
735 if (!multiline && OP(next) == EOL && ln < n)
736 ln = n; /* why back off? */
738 /* If it could work, try it. */
739 if (nextchar == -1000 || *reginput == nextchar)
742 /* Couldn't or didn't -- back up. */
744 reginput = locinput + n;
748 reginput = locinput; /* put where regtry can find it */
749 return(1); /* Success! */
751 printf("%x %d\n",scan,scan[1]);
752 FAIL("regexp memory corruption");
759 * We get here only if there's trouble -- normally "case END" is
760 * the terminating point.
762 FAIL("corrupted regexp pointers");
770 - regrepeat - repeatedly match something simple, report how many
773 * [This routine now assumes that it will only match on things of length 1.
774 * That was true before, but now we assume scan - reginput is the count,
775 * rather than incrementing count on every character.]
785 register char *loceol = regeol;
788 if (max && max < loceol - scan)
793 while (scan < loceol && *scan != '\n')
796 case EXACTLY: /* length of string is 1 */
798 while (scan < loceol && *opnd == *scan)
803 while (scan < loceol && !(opnd[c >> 3] & (1 << (c & 7)))) {
809 while (scan < loceol && isALNUM(*scan))
813 while (scan < loceol && !isALNUM(*scan))
817 while (scan < loceol && isSPACE(*scan))
821 while (scan < loceol && !isSPACE(*scan))
825 while (scan < loceol && isDIGIT(*scan))
829 while (scan < loceol && !isDIGIT(*scan))
832 default: /* Oh dear. Called inappropriately. */
833 FAIL("internal regexp foulup");
844 - regnext - dig the "next" pointer out of a node
846 * [Note, when REGALIGN is defined there are two places in regmatch()
847 * that bypass this code for speed.]