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.6 90/11/10 02:00:57 lwall Locked $
13 * Revision 3.0.1.6 90/11/10 02:00:57 lwall
14 * patch38: patterns like /^foo.*bar/ sped up some
15 * patch38: /[^whatever]+/ could scan past end of string
17 * Revision 3.0.1.5 90/10/16 10:25:36 lwall
18 * patch29: /^pat/ occasionally matched in middle of string when $* = 0
19 * patch29: /.{n,m}$/ could match with fewer than n characters remaining
20 * patch29: /\d{9}/ could match more than 9 characters
22 * Revision 3.0.1.4 90/08/09 05:12:03 lwall
23 * patch19: sped up /x+y/ patterns greatly by not retrying on every x
24 * patch19: inhibited backoff on patterns anchored to the end like /\s+$/
25 * patch19: sped up {m,n} on simple items
26 * patch19: $' broke on embedded nulls
27 * patch19: $ will now only match at end of string if $* == 0
29 * Revision 3.0.1.3 90/02/28 18:14:39 lwall
30 * patch9: /[\200-\377]/ didn't work on machines with signed chars
31 * patch9: \d, \w, and \s could misfire on characters with high bit set
32 * patch9: /\bfoo/i didn't work
34 * Revision 3.0.1.2 89/12/21 20:16:27 lwall
35 * patch7: certain patterns didn't match correctly at end of string
37 * Revision 3.0.1.1 89/11/11 04:52:04 lwall
38 * patch2: /\b$foo/ didn't work
40 * Revision 3.0 89/10/18 15:22:53 lwall
46 * regcomp and regexec -- regsub and regerror are not used in perl
48 * Copyright (c) 1986 by University of Toronto.
49 * Written by Henry Spencer. Not derived from licensed software.
51 * Permission is granted to anyone to use this software for any
52 * purpose on any computer system, and to redistribute it freely,
53 * subject to the following restrictions:
55 * 1. The author is not responsible for the consequences of use of
56 * this software, no matter how awful, even if they arise
59 * 2. The origin of this software must not be misrepresented, either
60 * by explicit claim or by omission.
62 * 3. Altered versions must be plainly marked as such, and must not
63 * be misrepresented as being the original software.
65 **** Alterations to Henry's code are...
67 **** Copyright (c) 1989, Larry Wall
69 **** You may distribute under the terms of the GNU General Public License
70 **** as specified in the README file that comes with the perl 3.0 kit.
72 * Beware that some of this code is subtly aware of the way operator
73 * precedence is structured in regular expressions. Serious changes in
74 * regular-expression syntax might require a total rethink.
88 #define isALNUM(c) (isascii(c) && (isalpha(c) || isdigit(c) || c == '_'))
89 #define isSPACE(c) (isascii(c) && isspace(c))
90 #define isDIGIT(c) (isascii(c) && isdigit(c))
91 #define isUPPER(c) (isascii(c) && isupper(c))
98 * Global work variables for regexec().
100 static char *regprecomp;
101 static char *reginput; /* String-input pointer. */
102 static char regprev; /* char before regbol, \n if none */
103 static char *regbol; /* Beginning of input, for ^ check. */
104 static char *regeol; /* End of input, for $ check. */
105 static char **regstartp; /* Pointer to startp array. */
106 static char **regendp; /* Ditto for endp. */
107 static char *reglastparen; /* Similarly for lastparen. */
108 static char *regtill;
110 static char *regmystartp[10]; /* For remembering backreferences. */
111 static char *regmyendp[10];
117 STATIC int regmatch();
118 STATIC int regrepeat();
120 extern int multiline;
123 - regexec - match a regexp against a string
126 regexec(prog, stringarg, strend, strbeg, minend, screamer, safebase)
127 register regexp *prog;
129 register char *strend; /* pointer to null at end of string */
130 char *strbeg; /* real beginning of string */
131 int minend; /* end of match must be at least minend after stringarg */
133 int safebase; /* no need to remember string in subbase */
138 register char *string = stringarg;
140 int minlen = 0; /* must match at least this many chars */
141 int dontbother = 0; /* how many characters not to try at end */
144 if (prog == NULL || string == NULL) {
145 fatal("NULL regexp parameter");
149 if (string == strbeg) /* is ^ valid at stringarg? */
152 regprev = stringarg[-1];
153 if (!multiline && regprev == '\n')
154 regprev = '\0'; /* force ^ to NOT match */
156 regprecomp = prog->precomp;
157 /* Check validity of program. */
158 if (UCHARAT(prog->program) != MAGIC) {
159 FAIL("corrupted regexp program");
162 if (prog->do_folding) {
165 New(1101,c,i+1,char);
166 (void)bcopy(string, c, i+1);
169 for (s = string; s < strend; s++)
174 /* If there is a "must appear" string, look for it. */
176 if (prog->regmust != Nullstr &&
177 (!(prog->reganch & 1) || (multiline && prog->regback >= 0)) ) {
178 if (stringarg == strbeg && screamer) {
179 if (screamfirst[prog->regmust->str_rare] >= 0)
180 s = screaminstr(screamer,prog->regmust);
186 s = fbminstr((unsigned char*)s, (unsigned char*)strend,
190 ++prog->regmust->str_u.str_useful; /* hooray */
191 goto phooey; /* not present */
193 else if (prog->regback >= 0) {
197 minlen = prog->regback + prog->regmust->str_cur;
199 else if (--prog->regmust->str_u.str_useful < 0) { /* boo */
200 str_free(prog->regmust);
201 prog->regmust = Nullstr; /* disable regmust */
206 minlen = prog->regmust->str_cur;
210 /* Mark beginning of line for ^ . */
213 /* Mark end of line for $ (and such) */
216 /* see how far we have to get to not match where we matched before */
217 regtill = string+minend;
219 /* Simplest case: anchored match need be tried only once. */
220 /* [unless multiline is set] */
221 if (prog->reganch & 1) {
222 if (regtry(prog, string))
224 else if (multiline) {
226 dontbother = minlen - 1;
227 strend -= dontbother;
228 /* for multiline we only have to try after newlines */
233 if (s < strend && regtry(prog, s))
241 /* Messy cases: unanchored match. */
242 if (prog->regstart) {
243 if (prog->reganch & 2) { /* we have /x+whatever/ */
244 /* it must be a one character string */
245 i = prog->regstart->str_ptr[0];
251 while (s < strend && *s == i)
257 else if (prog->regstart->str_pok == 3) {
258 /* We know what string it must start with. */
260 while ((s = fbminstr((unsigned char*)s,
261 (unsigned char*)strend, prog->regstart)) != NULL)
272 c = prog->regstart->str_ptr;
273 while ((s = ninstr(s, strend,
274 c, c + prog->regstart->str_cur )) != NULL) {
282 if (c = prog->regstclass) {
283 int doevery = (prog->reganch & 2) == 0;
286 dontbother = minlen - 1;
287 strend -= dontbother; /* don't bother with what can't match */
289 /* We know what class it must start with. */
295 if (!(c[i >> 3] & (1 << (i&7)))) {
296 if (tmp && regtry(prog, s))
308 dontbother++,strend--;
314 tmp = isALNUM(regprev); /* assume not alphanumeric */
317 if (tmp != isALNUM(i)) {
324 if ((minlen || tmp) && regtry(prog,s))
329 dontbother++,strend--;
335 tmp = isALNUM(regprev); /* assume not alphanumeric */
338 if (tmp != isALNUM(i))
340 else if (regtry(prog, s))
344 if ((minlen || !tmp) && regtry(prog,s))
351 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))
417 if (tmp && regtry(prog, s))
431 dontbother = minlen - 1;
432 strend -= dontbother;
433 /* We don't know much -- general case. */
437 } while (s++ < strend);
444 if ((!safebase && (prog->nparens || sawampersand)) || prog->do_folding){
445 strend += dontbother; /* uncheat */
446 if (safebase) /* no need for $digit later */
448 else if (strbeg != prog->subbase) {
449 i = strend - string + (stringarg - strbeg);
450 s = nsavestr(strbeg,i); /* so $digit will work later */
452 Safefree(prog->subbase);
458 s += (stringarg - strbeg);
459 for (i = 0; i <= prog->nparens; i++) {
461 prog->startp[i] = s + (prog->startp[i] - string);
462 prog->endp[i] = s + (prog->endp[i] - string);
465 if (prog->do_folding)
471 if (prog->do_folding)
477 - regtry - try match at specific point
479 static int /* 0 failure, 1 success */
489 regstartp = prog->startp;
490 regendp = prog->endp;
491 reglastparen = &prog->lastparen;
497 for (i = NSUBEXP; i > 0; i--) {
502 if (regmatch(prog->program + 1) && reginput >= regtill) {
503 prog->startp[0] = string;
504 prog->endp[0] = reginput;
511 - regmatch - main matching routine
513 * Conceptually the strategy is simple: check to see whether the current
514 * node matches, call self recursively to see whether the rest matches,
515 * and then act accordingly. In practice we make some effort to avoid
516 * recursion, in particular by going through "ordinary" nodes (that don't
517 * need to know whether the rest of the match failed) by a loop instead of
520 /* [lwall] I've hoisted the register declarations to the outer block in order to
521 * maybe save a little bit of pushing and popping on the stack. It also takes
522 * advantage of machines that use a register save mask on subroutine entry.
524 static int /* 0 failure, 1 success */
528 register char *scan; /* Current node. */
529 char *next; /* Next node. */
530 register int nextchar;
531 register int n; /* no or next */
532 register int ln; /* len or last */
533 register char *s; /* operand or save */
534 register char *locinput = reginput;
536 nextchar = *locinput;
539 if (scan != NULL && regnarrate)
540 fprintf(stderr, "%s(\n", regprop(scan));
542 while (scan != NULL) {
545 fprintf(stderr, "%s...\n", regprop(scan));
549 next = scan + NEXT(scan);
553 next = regnext(scan);
558 if (locinput == regbol ? regprev == '\n' :
559 ((nextchar || locinput < regeol) &&
560 locinput[-1] == '\n') )
567 if ((nextchar || locinput < regeol) && nextchar != '\n')
569 if (!multiline && regeol - locinput > 1)
574 if ((nextchar == '\0' && locinput >= regeol) ||
577 nextchar = *++locinput;
582 /* Inline the first character, for speed. */
585 if (regeol - locinput < ln)
587 if (ln > 1 && bcmp(s, locinput, ln) != 0)
590 nextchar = *locinput;
595 nextchar = UCHARAT(locinput);
596 if (s[nextchar >> 3] & (1 << (nextchar&7)))
598 if (!nextchar && locinput >= regeol)
600 nextchar = *++locinput;
605 if (!isALNUM(nextchar))
607 nextchar = *++locinput;
610 if (!nextchar && locinput >= regeol)
612 if (isALNUM(nextchar))
614 nextchar = *++locinput;
618 if (locinput == regbol) /* was last char in word? */
619 ln = isALNUM(regprev);
621 ln = isALNUM(locinput[-1]);
622 n = isALNUM(nextchar); /* is next char in word? */
623 if ((ln == n) == (OP(scan) == BOUND))
627 if (!nextchar && locinput >= regeol)
629 if (!isSPACE(nextchar))
631 nextchar = *++locinput;
636 if (isSPACE(nextchar))
638 nextchar = *++locinput;
641 if (!isDIGIT(nextchar))
643 nextchar = *++locinput;
646 if (!nextchar && locinput >= regeol)
648 if (isDIGIT(nextchar))
650 nextchar = *++locinput;
668 if (s == regmyendp[n])
670 /* Inline the first character, for speed. */
673 ln = regmyendp[n] - s;
674 if (locinput + ln > regeol)
676 if (ln > 1 && bcmp(s, locinput, ln) != 0)
679 nextchar = *locinput;
698 regmystartp[n] = locinput; /* for REF */
699 if (regmatch(next)) {
701 * Don't set startp if some later
702 * invocation of the same parentheses
705 if (regstartp[n] == NULL)
706 regstartp[n] = locinput;
720 n = OP(scan) - CLOSE;
723 regmyendp[n] = locinput; /* for REF */
724 if (regmatch(next)) {
726 * Don't set endp if some later
727 * invocation of the same parentheses
730 if (regendp[n] == NULL) {
731 regendp[n] = locinput;
732 if (n > *reglastparen)
741 if (OP(next) != BRANCH) /* No choice. */
742 next = NEXTOPER(scan); /* Avoid recursion. */
746 if (regmatch(NEXTOPER(scan)))
754 scan = regnext(scan);
756 } while (scan != NULL && OP(scan) == BRANCH);
763 ln = ARG1(scan); /* min to match */
764 n = ARG2(scan); /* max to match */
765 scan = NEXTOPER(scan) + 4;
770 scan = NEXTOPER(scan);
774 * Lookahead to avoid useless match attempts
775 * when we know what character comes next.
779 scan = NEXTOPER(scan);
781 if (OP(next) == EXACTLY)
782 nextchar = *(OPERAND(next)+1);
786 n = regrepeat(scan, n);
787 if (!multiline && OP(next) == EOL && ln < n)
788 ln = n; /* why back off? */
790 /* If it could work, try it. */
791 if (nextchar == -1000 || *reginput == nextchar)
794 /* Couldn't or didn't -- back up. */
796 reginput = locinput + n;
800 reginput = locinput; /* put where regtry can find it */
801 return(1); /* Success! */
803 printf("%x %d\n",scan,scan[1]);
804 FAIL("regexp memory corruption");
811 * We get here only if there's trouble -- normally "case END" is
812 * the terminating point.
814 FAIL("corrupted regexp pointers");
822 - regrepeat - repeatedly match something simple, report how many
825 * [This routine now assumes that it will only match on things of length 1.
826 * That was true before, but now we assume scan - reginput is the count,
827 * rather than incrementing count on every character.]
837 register char *loceol = regeol;
840 if (max && max < loceol - scan)
845 while (scan < loceol && *scan != '\n')
848 case EXACTLY: /* length of string is 1 */
850 while (scan < loceol && *opnd == *scan)
855 while (scan < loceol && !(opnd[c >> 3] & (1 << (c & 7)))) {
861 while (scan < loceol && isALNUM(*scan))
865 while (scan < loceol && !isALNUM(*scan))
869 while (scan < loceol && isSPACE(*scan))
873 while (scan < loceol && !isSPACE(*scan))
877 while (scan < loceol && isDIGIT(*scan))
881 while (scan < loceol && !isDIGIT(*scan))
884 default: /* Oh dear. Called inappropriately. */
885 FAIL("internal regexp foulup");
896 - regnext - dig the "next" pointer out of a node
898 * [Note, when REGALIGN is defined there are two places in regmatch()
899 * that bypass this code for speed.]