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 3.0.1.4 90/08/09 05:12:03 lwall Locked $
13 * Revision 3.0.1.4 90/08/09 05:12:03 lwall
14 * patch19: sped up /x+y/ patterns greatly by not retrying on every x
15 * patch19: inhibited backoff on patterns anchored to the end like /\s+$/
16 * patch19: sped up {m,n} on simple items
17 * patch19: $' broke on embedded nulls
18 * patch19: $ will now only match at end of string if $* == 0
20 * Revision 3.0.1.3 90/02/28 18:14:39 lwall
21 * patch9: /[\200-\377]/ didn't work on machines with signed chars
22 * patch9: \d, \w, and \s could misfire on characters with high bit set
23 * patch9: /\bfoo/i didn't work
25 * Revision 3.0.1.2 89/12/21 20:16:27 lwall
26 * patch7: certain patterns didn't match correctly at end of string
28 * Revision 3.0.1.1 89/11/11 04:52:04 lwall
29 * patch2: /\b$foo/ didn't work
31 * Revision 3.0 89/10/18 15:22:53 lwall
37 * regcomp and regexec -- regsub and regerror are not used in perl
39 * Copyright (c) 1986 by University of Toronto.
40 * Written by Henry Spencer. Not derived from licensed software.
42 * Permission is granted to anyone to use this software for any
43 * purpose on any computer system, and to redistribute it freely,
44 * subject to the following restrictions:
46 * 1. The author is not responsible for the consequences of use of
47 * this software, no matter how awful, even if they arise
50 * 2. The origin of this software must not be misrepresented, either
51 * by explicit claim or by omission.
53 * 3. Altered versions must be plainly marked as such, and must not
54 * be misrepresented as being the original software.
56 **** Alterations to Henry's code are...
58 **** Copyright (c) 1989, Larry Wall
60 **** You may distribute under the terms of the GNU General Public License
61 **** as specified in the README file that comes with the perl 3.0 kit.
63 * Beware that some of this code is subtly aware of the way operator
64 * precedence is structured in regular expressions. Serious changes in
65 * regular-expression syntax might require a total rethink.
79 #define isALNUM(c) (isascii(c) && (isalpha(c) || isdigit(c) || c == '_'))
80 #define isSPACE(c) (isascii(c) && isspace(c))
81 #define isDIGIT(c) (isascii(c) && isdigit(c))
82 #define isUPPER(c) (isascii(c) && isupper(c))
89 * Global work variables for regexec().
91 static char *regprecomp;
92 static char *reginput; /* String-input pointer. */
93 static char regprev; /* char before regbol, \n if none */
94 static char *regbol; /* Beginning of input, for ^ check. */
95 static char *regeol; /* End of input, for $ check. */
96 static char **regstartp; /* Pointer to startp array. */
97 static char **regendp; /* Ditto for endp. */
98 static char *reglastparen; /* Similarly for lastparen. */
101 static char *regmystartp[10]; /* For remembering backreferences. */
102 static char *regmyendp[10];
108 STATIC int regmatch();
109 STATIC int regrepeat();
111 extern int multiline;
114 - regexec - match a regexp against a string
117 regexec(prog, stringarg, strend, strbeg, minend, screamer, safebase)
118 register regexp *prog;
120 register char *strend; /* pointer to null at end of string */
121 char *strbeg; /* real beginning of string */
122 int minend; /* end of match must be at least minend after stringarg */
124 int safebase; /* no need to remember string in subbase */
129 register char *string = stringarg;
131 int minlen = 0; /* must match at least this many chars */
132 int dontbother = 0; /* how many characters not to try at end */
135 if (prog == NULL || string == NULL) {
136 fatal("NULL regexp parameter");
140 if (string == strbeg) /* is ^ valid at stringarg? */
143 regprev = stringarg[-1];
144 regprecomp = prog->precomp;
145 /* Check validity of program. */
146 if (UCHARAT(prog->program) != MAGIC) {
147 FAIL("corrupted regexp program");
150 if (prog->do_folding) {
153 New(1101,c,i+1,char);
154 (void)bcopy(string, c, i+1);
157 for (s = string; s < strend; s++)
162 /* If there is a "must appear" string, look for it. */
164 if (prog->regmust != Nullstr) {
165 if (stringarg == strbeg && screamer) {
166 if (screamfirst[prog->regmust->str_rare] >= 0)
167 s = screaminstr(screamer,prog->regmust);
173 s = fbminstr((unsigned char*)s, (unsigned char*)strend,
177 ++prog->regmust->str_u.str_useful; /* hooray */
178 goto phooey; /* not present */
180 else if (prog->regback >= 0) {
184 minlen = prog->regback + prog->regmust->str_cur;
186 else if (--prog->regmust->str_u.str_useful < 0) { /* boo */
187 str_free(prog->regmust);
188 prog->regmust = Nullstr; /* disable regmust */
193 minlen = prog->regmust->str_cur;
197 /* Mark beginning of line for ^ . */
200 /* Mark end of line for $ (and such) */
203 /* see how far we have to get to not match where we matched before */
204 regtill = string+minend;
206 /* Simplest case: anchored match need be tried only once. */
207 /* [unless multiline is set] */
208 if (prog->reganch & 1) {
209 if (regtry(prog, string))
211 else if (multiline) {
213 dontbother = minlen - 1;
214 strend -= dontbother;
215 /* for multiline we only have to try after newlines */
220 if (s < strend && regtry(prog, s))
228 /* Messy cases: unanchored match. */
229 if (prog->regstart) {
230 if (prog->reganch & 2) { /* we have /x+whatever/ */
231 /* it must be a one character string */
232 i = prog->regstart->str_ptr[0];
238 while (s < strend && *s == i)
244 else if (prog->regstart->str_pok == 3) {
245 /* We know what string it must start with. */
247 while ((s = fbminstr((unsigned char*)s,
248 (unsigned char*)strend, prog->regstart)) != NULL)
259 c = prog->regstart->str_ptr;
260 while ((s = ninstr(s, strend,
261 c, c + prog->regstart->str_cur )) != NULL) {
269 if (c = prog->regstclass) {
270 int doevery = (prog->reganch & 2) == 0;
273 dontbother = minlen - 1;
274 strend -= dontbother; /* don't bother with what can't match */
276 /* We know what class it must start with. */
282 if (!(c[i >> 3] & (1 << (i&7)))) {
283 if (tmp && regtry(prog, s))
295 dontbother++,strend--;
301 tmp = isALNUM(regprev); /* assume not alphanumeric */
304 if (tmp != isALNUM(i)) {
311 if ((minlen || tmp) && regtry(prog,s))
316 dontbother++,strend--;
322 tmp = isALNUM(regprev); /* assume not alphanumeric */
325 if (tmp != isALNUM(i))
327 else if (regtry(prog, s))
331 if ((minlen || !tmp) && regtry(prog,s))
338 if (tmp && regtry(prog, s))
352 if (tmp && regtry(prog, s))
365 if (tmp && regtry(prog, s))
378 if (tmp && regtry(prog, s))
391 if (tmp && regtry(prog, s))
404 if (tmp && regtry(prog, s))
418 dontbother = minlen - 1;
419 strend -= dontbother;
420 /* We don't know much -- general case. */
424 } while (s++ < strend);
431 if ((!safebase && (prog->nparens || sawampersand)) || prog->do_folding){
432 strend += dontbother; /* uncheat */
433 if (safebase) /* no need for $digit later */
435 else if (strbeg != prog->subbase) {
436 i = strend - string + (stringarg - strbeg);
437 s = nsavestr(strbeg,i); /* so $digit will work later */
439 Safefree(prog->subbase);
445 s += (stringarg - strbeg);
446 for (i = 0; i <= prog->nparens; i++) {
448 prog->startp[i] = s + (prog->startp[i] - string);
449 prog->endp[i] = s + (prog->endp[i] - string);
452 if (prog->do_folding)
458 if (prog->do_folding)
464 - regtry - try match at specific point
466 static int /* 0 failure, 1 success */
476 regstartp = prog->startp;
477 regendp = prog->endp;
478 reglastparen = &prog->lastparen;
484 for (i = NSUBEXP; i > 0; i--) {
489 if (regmatch(prog->program + 1) && reginput >= regtill) {
490 prog->startp[0] = string;
491 prog->endp[0] = reginput;
498 - regmatch - main matching routine
500 * Conceptually the strategy is simple: check to see whether the current
501 * node matches, call self recursively to see whether the rest matches,
502 * and then act accordingly. In practice we make some effort to avoid
503 * recursion, in particular by going through "ordinary" nodes (that don't
504 * need to know whether the rest of the match failed) by a loop instead of
507 /* [lwall] I've hoisted the register declarations to the outer block in order to
508 * maybe save a little bit of pushing and popping on the stack. It also takes
509 * advantage of machines that use a register save mask on subroutine entry.
511 static int /* 0 failure, 1 success */
515 register char *scan; /* Current node. */
516 char *next; /* Next node. */
517 register int nextchar;
518 register int n; /* no or next */
519 register int ln; /* len or last */
520 register char *s; /* operand or save */
521 register char *locinput = reginput;
523 nextchar = *locinput;
526 if (scan != NULL && regnarrate)
527 fprintf(stderr, "%s(\n", regprop(scan));
529 while (scan != NULL) {
532 fprintf(stderr, "%s...\n", regprop(scan));
536 next = scan + NEXT(scan);
540 next = regnext(scan);
545 if (locinput == regbol ? regprev == '\n' :
546 ((nextchar || locinput < regeol) &&
547 locinput[-1] == '\n') )
554 if ((nextchar || locinput < regeol) && nextchar != '\n')
556 if (!multiline && regeol - locinput > 1)
561 if ((nextchar == '\0' && locinput >= regeol) ||
564 nextchar = *++locinput;
569 /* Inline the first character, for speed. */
572 if (regeol - locinput < ln)
574 if (ln > 1 && bcmp(s, locinput, ln) != 0)
577 nextchar = *locinput;
582 nextchar = UCHARAT(locinput);
583 if (s[nextchar >> 3] & (1 << (nextchar&7)))
585 nextchar = *++locinput;
586 if (!nextchar && locinput > regeol)
592 if (!isALNUM(nextchar))
594 nextchar = *++locinput;
597 if (!nextchar && locinput >= regeol)
599 if (isALNUM(nextchar))
601 nextchar = *++locinput;
605 if (locinput == regbol) /* was last char in word? */
606 ln = isALNUM(regprev);
608 ln = isALNUM(locinput[-1]);
609 n = isALNUM(nextchar); /* is next char in word? */
610 if ((ln == n) == (OP(scan) == BOUND))
614 if (!nextchar && locinput >= regeol)
616 if (!isSPACE(nextchar))
618 nextchar = *++locinput;
623 if (isSPACE(nextchar))
625 nextchar = *++locinput;
628 if (!isDIGIT(nextchar))
630 nextchar = *++locinput;
633 if (!nextchar && locinput >= regeol)
635 if (isDIGIT(nextchar))
637 nextchar = *++locinput;
655 if (s == regmyendp[n])
657 /* Inline the first character, for speed. */
660 ln = regmyendp[n] - s;
661 if (locinput + ln > regeol)
663 if (ln > 1 && bcmp(s, locinput, ln) != 0)
666 nextchar = *locinput;
685 regmystartp[n] = locinput; /* for REF */
686 if (regmatch(next)) {
688 * Don't set startp if some later
689 * invocation of the same parentheses
692 if (regstartp[n] == NULL)
693 regstartp[n] = locinput;
707 n = OP(scan) - CLOSE;
710 regmyendp[n] = locinput; /* for REF */
711 if (regmatch(next)) {
713 * Don't set endp if some later
714 * invocation of the same parentheses
717 if (regendp[n] == NULL) {
718 regendp[n] = locinput;
719 if (n > *reglastparen)
728 if (OP(next) != BRANCH) /* No choice. */
729 next = NEXTOPER(scan); /* Avoid recursion. */
733 if (regmatch(NEXTOPER(scan)))
741 scan = regnext(scan);
743 } while (scan != NULL && OP(scan) == BRANCH);
750 ln = ARG1(scan); /* min to match */
751 n = ARG2(scan); /* max to match */
752 scan = NEXTOPER(scan) + 4;
757 scan = NEXTOPER(scan);
761 * Lookahead to avoid useless match attempts
762 * when we know what character comes next.
766 scan = NEXTOPER(scan);
768 if (OP(next) == EXACTLY)
769 nextchar = *(OPERAND(next)+1);
773 n = regrepeat(scan, n);
774 if (!multiline && OP(next) == EOL)
775 ln = n; /* why back off? */
777 /* If it could work, try it. */
778 if (nextchar == -1000 || *reginput == nextchar)
781 /* Couldn't or didn't -- back up. */
783 reginput = locinput + n;
787 reginput = locinput; /* put where regtry can find it */
788 return(1); /* Success! */
790 printf("%x %d\n",scan,scan[1]);
791 FAIL("regexp memory corruption");
798 * We get here only if there's trouble -- normally "case END" is
799 * the terminating point.
801 FAIL("corrupted regexp pointers");
809 - regrepeat - repeatedly match something simple, report how many
812 * [This routine now assumes that it will only match on things of length 1.
813 * That was true before, but now we assume scan - reginput is the count,
814 * rather than incrementing count on every character.]
824 register char *loceol = regeol;
827 if (max && max < loceol - scan)
832 while (scan < loceol && *scan != '\n')
835 case EXACTLY: /* length of string is 1 */
837 while (scan < loceol && *opnd == *scan)
842 while (scan < loceol && !(opnd[c >> 3] & (1 << (c & 7)))) {
848 while (isALNUM(*scan))
852 while (scan < loceol && !isALNUM(*scan))
856 while (scan < loceol && isSPACE(*scan))
860 while (scan < loceol && !isSPACE(*scan))
864 while (isDIGIT(*scan))
868 while (scan < loceol && !isDIGIT(*scan))
871 default: /* Oh dear. Called inappropriately. */
872 FAIL("internal regexp foulup");
883 - regnext - dig the "next" pointer out of a node
885 * [Note, when REGALIGN is defined there are two places in regmatch()
886 * that bypass this code for speed.]