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.5 90/10/16 10:25:36 lwall Locked $
13 * Revision 3.0.1.5 90/10/16 10:25:36 lwall
14 * patch29: /^pat/ occasionally matched in middle of string when $* = 0
15 * patch29: /.{n,m}$/ could match with fewer than n characters remaining
16 * patch29: /\d{9}/ could match more than 9 characters
18 * Revision 3.0.1.4 90/08/09 05:12:03 lwall
19 * patch19: sped up /x+y/ patterns greatly by not retrying on every x
20 * patch19: inhibited backoff on patterns anchored to the end like /\s+$/
21 * patch19: sped up {m,n} on simple items
22 * patch19: $' broke on embedded nulls
23 * patch19: $ will now only match at end of string if $* == 0
25 * Revision 3.0.1.3 90/02/28 18:14:39 lwall
26 * patch9: /[\200-\377]/ didn't work on machines with signed chars
27 * patch9: \d, \w, and \s could misfire on characters with high bit set
28 * patch9: /\bfoo/i didn't work
30 * Revision 3.0.1.2 89/12/21 20:16:27 lwall
31 * patch7: certain patterns didn't match correctly at end of string
33 * Revision 3.0.1.1 89/11/11 04:52:04 lwall
34 * patch2: /\b$foo/ didn't work
36 * Revision 3.0 89/10/18 15:22:53 lwall
42 * regcomp and regexec -- regsub and regerror are not used in perl
44 * Copyright (c) 1986 by University of Toronto.
45 * Written by Henry Spencer. Not derived from licensed software.
47 * Permission is granted to anyone to use this software for any
48 * purpose on any computer system, and to redistribute it freely,
49 * subject to the following restrictions:
51 * 1. The author is not responsible for the consequences of use of
52 * this software, no matter how awful, even if they arise
55 * 2. The origin of this software must not be misrepresented, either
56 * by explicit claim or by omission.
58 * 3. Altered versions must be plainly marked as such, and must not
59 * be misrepresented as being the original software.
61 **** Alterations to Henry's code are...
63 **** Copyright (c) 1989, Larry Wall
65 **** You may distribute under the terms of the GNU General Public License
66 **** as specified in the README file that comes with the perl 3.0 kit.
68 * Beware that some of this code is subtly aware of the way operator
69 * precedence is structured in regular expressions. Serious changes in
70 * regular-expression syntax might require a total rethink.
84 #define isALNUM(c) (isascii(c) && (isalpha(c) || isdigit(c) || c == '_'))
85 #define isSPACE(c) (isascii(c) && isspace(c))
86 #define isDIGIT(c) (isascii(c) && isdigit(c))
87 #define isUPPER(c) (isascii(c) && isupper(c))
94 * Global work variables for regexec().
96 static char *regprecomp;
97 static char *reginput; /* String-input pointer. */
98 static char regprev; /* char before regbol, \n if none */
99 static char *regbol; /* Beginning of input, for ^ check. */
100 static char *regeol; /* End of input, for $ check. */
101 static char **regstartp; /* Pointer to startp array. */
102 static char **regendp; /* Ditto for endp. */
103 static char *reglastparen; /* Similarly for lastparen. */
104 static char *regtill;
106 static char *regmystartp[10]; /* For remembering backreferences. */
107 static char *regmyendp[10];
113 STATIC int regmatch();
114 STATIC int regrepeat();
116 extern int multiline;
119 - regexec - match a regexp against a string
122 regexec(prog, stringarg, strend, strbeg, minend, screamer, safebase)
123 register regexp *prog;
125 register char *strend; /* pointer to null at end of string */
126 char *strbeg; /* real beginning of string */
127 int minend; /* end of match must be at least minend after stringarg */
129 int safebase; /* no need to remember string in subbase */
134 register char *string = stringarg;
136 int minlen = 0; /* must match at least this many chars */
137 int dontbother = 0; /* how many characters not to try at end */
140 if (prog == NULL || string == NULL) {
141 fatal("NULL regexp parameter");
145 if (string == strbeg) /* is ^ valid at stringarg? */
148 regprev = stringarg[-1];
149 if (!multiline && regprev == '\n')
150 regprev = '\0'; /* force ^ to NOT match */
152 regprecomp = prog->precomp;
153 /* Check validity of program. */
154 if (UCHARAT(prog->program) != MAGIC) {
155 FAIL("corrupted regexp program");
158 if (prog->do_folding) {
161 New(1101,c,i+1,char);
162 (void)bcopy(string, c, i+1);
165 for (s = string; s < strend; s++)
170 /* If there is a "must appear" string, look for it. */
172 if (prog->regmust != Nullstr) {
173 if (stringarg == strbeg && screamer) {
174 if (screamfirst[prog->regmust->str_rare] >= 0)
175 s = screaminstr(screamer,prog->regmust);
181 s = fbminstr((unsigned char*)s, (unsigned char*)strend,
185 ++prog->regmust->str_u.str_useful; /* hooray */
186 goto phooey; /* not present */
188 else if (prog->regback >= 0) {
192 minlen = prog->regback + prog->regmust->str_cur;
194 else if (--prog->regmust->str_u.str_useful < 0) { /* boo */
195 str_free(prog->regmust);
196 prog->regmust = Nullstr; /* disable regmust */
201 minlen = prog->regmust->str_cur;
205 /* Mark beginning of line for ^ . */
208 /* Mark end of line for $ (and such) */
211 /* see how far we have to get to not match where we matched before */
212 regtill = string+minend;
214 /* Simplest case: anchored match need be tried only once. */
215 /* [unless multiline is set] */
216 if (prog->reganch & 1) {
217 if (regtry(prog, string))
219 else if (multiline) {
221 dontbother = minlen - 1;
222 strend -= dontbother;
223 /* for multiline we only have to try after newlines */
228 if (s < strend && regtry(prog, s))
236 /* Messy cases: unanchored match. */
237 if (prog->regstart) {
238 if (prog->reganch & 2) { /* we have /x+whatever/ */
239 /* it must be a one character string */
240 i = prog->regstart->str_ptr[0];
246 while (s < strend && *s == i)
252 else if (prog->regstart->str_pok == 3) {
253 /* We know what string it must start with. */
255 while ((s = fbminstr((unsigned char*)s,
256 (unsigned char*)strend, prog->regstart)) != NULL)
267 c = prog->regstart->str_ptr;
268 while ((s = ninstr(s, strend,
269 c, c + prog->regstart->str_cur )) != NULL) {
277 if (c = prog->regstclass) {
278 int doevery = (prog->reganch & 2) == 0;
281 dontbother = minlen - 1;
282 strend -= dontbother; /* don't bother with what can't match */
284 /* We know what class it must start with. */
290 if (!(c[i >> 3] & (1 << (i&7)))) {
291 if (tmp && regtry(prog, s))
303 dontbother++,strend--;
309 tmp = isALNUM(regprev); /* assume not alphanumeric */
312 if (tmp != isALNUM(i)) {
319 if ((minlen || tmp) && regtry(prog,s))
324 dontbother++,strend--;
330 tmp = isALNUM(regprev); /* assume not alphanumeric */
333 if (tmp != isALNUM(i))
335 else if (regtry(prog, s))
339 if ((minlen || !tmp) && regtry(prog,s))
346 if (tmp && regtry(prog, s))
360 if (tmp && regtry(prog, s))
373 if (tmp && regtry(prog, s))
386 if (tmp && regtry(prog, s))
399 if (tmp && regtry(prog, s))
412 if (tmp && regtry(prog, s))
426 dontbother = minlen - 1;
427 strend -= dontbother;
428 /* We don't know much -- general case. */
432 } while (s++ < strend);
439 if ((!safebase && (prog->nparens || sawampersand)) || prog->do_folding){
440 strend += dontbother; /* uncheat */
441 if (safebase) /* no need for $digit later */
443 else if (strbeg != prog->subbase) {
444 i = strend - string + (stringarg - strbeg);
445 s = nsavestr(strbeg,i); /* so $digit will work later */
447 Safefree(prog->subbase);
453 s += (stringarg - strbeg);
454 for (i = 0; i <= prog->nparens; i++) {
456 prog->startp[i] = s + (prog->startp[i] - string);
457 prog->endp[i] = s + (prog->endp[i] - string);
460 if (prog->do_folding)
466 if (prog->do_folding)
472 - regtry - try match at specific point
474 static int /* 0 failure, 1 success */
484 regstartp = prog->startp;
485 regendp = prog->endp;
486 reglastparen = &prog->lastparen;
492 for (i = NSUBEXP; i > 0; i--) {
497 if (regmatch(prog->program + 1) && reginput >= regtill) {
498 prog->startp[0] = string;
499 prog->endp[0] = reginput;
506 - regmatch - main matching routine
508 * Conceptually the strategy is simple: check to see whether the current
509 * node matches, call self recursively to see whether the rest matches,
510 * and then act accordingly. In practice we make some effort to avoid
511 * recursion, in particular by going through "ordinary" nodes (that don't
512 * need to know whether the rest of the match failed) by a loop instead of
515 /* [lwall] I've hoisted the register declarations to the outer block in order to
516 * maybe save a little bit of pushing and popping on the stack. It also takes
517 * advantage of machines that use a register save mask on subroutine entry.
519 static int /* 0 failure, 1 success */
523 register char *scan; /* Current node. */
524 char *next; /* Next node. */
525 register int nextchar;
526 register int n; /* no or next */
527 register int ln; /* len or last */
528 register char *s; /* operand or save */
529 register char *locinput = reginput;
531 nextchar = *locinput;
534 if (scan != NULL && regnarrate)
535 fprintf(stderr, "%s(\n", regprop(scan));
537 while (scan != NULL) {
540 fprintf(stderr, "%s...\n", regprop(scan));
544 next = scan + NEXT(scan);
548 next = regnext(scan);
553 if (locinput == regbol ? regprev == '\n' :
554 ((nextchar || locinput < regeol) &&
555 locinput[-1] == '\n') )
562 if ((nextchar || locinput < regeol) && nextchar != '\n')
564 if (!multiline && regeol - locinput > 1)
569 if ((nextchar == '\0' && locinput >= regeol) ||
572 nextchar = *++locinput;
577 /* Inline the first character, for speed. */
580 if (regeol - locinput < ln)
582 if (ln > 1 && bcmp(s, locinput, ln) != 0)
585 nextchar = *locinput;
590 nextchar = UCHARAT(locinput);
591 if (s[nextchar >> 3] & (1 << (nextchar&7)))
593 nextchar = *++locinput;
594 if (!nextchar && locinput > regeol)
600 if (!isALNUM(nextchar))
602 nextchar = *++locinput;
605 if (!nextchar && locinput >= regeol)
607 if (isALNUM(nextchar))
609 nextchar = *++locinput;
613 if (locinput == regbol) /* was last char in word? */
614 ln = isALNUM(regprev);
616 ln = isALNUM(locinput[-1]);
617 n = isALNUM(nextchar); /* is next char in word? */
618 if ((ln == n) == (OP(scan) == BOUND))
622 if (!nextchar && locinput >= regeol)
624 if (!isSPACE(nextchar))
626 nextchar = *++locinput;
631 if (isSPACE(nextchar))
633 nextchar = *++locinput;
636 if (!isDIGIT(nextchar))
638 nextchar = *++locinput;
641 if (!nextchar && locinput >= regeol)
643 if (isDIGIT(nextchar))
645 nextchar = *++locinput;
663 if (s == regmyendp[n])
665 /* Inline the first character, for speed. */
668 ln = regmyendp[n] - s;
669 if (locinput + ln > regeol)
671 if (ln > 1 && bcmp(s, locinput, ln) != 0)
674 nextchar = *locinput;
693 regmystartp[n] = locinput; /* for REF */
694 if (regmatch(next)) {
696 * Don't set startp if some later
697 * invocation of the same parentheses
700 if (regstartp[n] == NULL)
701 regstartp[n] = locinput;
715 n = OP(scan) - CLOSE;
718 regmyendp[n] = locinput; /* for REF */
719 if (regmatch(next)) {
721 * Don't set endp if some later
722 * invocation of the same parentheses
725 if (regendp[n] == NULL) {
726 regendp[n] = locinput;
727 if (n > *reglastparen)
736 if (OP(next) != BRANCH) /* No choice. */
737 next = NEXTOPER(scan); /* Avoid recursion. */
741 if (regmatch(NEXTOPER(scan)))
749 scan = regnext(scan);
751 } while (scan != NULL && OP(scan) == BRANCH);
758 ln = ARG1(scan); /* min to match */
759 n = ARG2(scan); /* max to match */
760 scan = NEXTOPER(scan) + 4;
765 scan = NEXTOPER(scan);
769 * Lookahead to avoid useless match attempts
770 * when we know what character comes next.
774 scan = NEXTOPER(scan);
776 if (OP(next) == EXACTLY)
777 nextchar = *(OPERAND(next)+1);
781 n = regrepeat(scan, n);
782 if (!multiline && OP(next) == EOL && ln < n)
783 ln = n; /* why back off? */
785 /* If it could work, try it. */
786 if (nextchar == -1000 || *reginput == nextchar)
789 /* Couldn't or didn't -- back up. */
791 reginput = locinput + n;
795 reginput = locinput; /* put where regtry can find it */
796 return(1); /* Success! */
798 printf("%x %d\n",scan,scan[1]);
799 FAIL("regexp memory corruption");
806 * We get here only if there's trouble -- normally "case END" is
807 * the terminating point.
809 FAIL("corrupted regexp pointers");
817 - regrepeat - repeatedly match something simple, report how many
820 * [This routine now assumes that it will only match on things of length 1.
821 * That was true before, but now we assume scan - reginput is the count,
822 * rather than incrementing count on every character.]
832 register char *loceol = regeol;
835 if (max && max < loceol - scan)
840 while (scan < loceol && *scan != '\n')
843 case EXACTLY: /* length of string is 1 */
845 while (scan < loceol && *opnd == *scan)
850 while (scan < loceol && !(opnd[c >> 3] & (1 << (c & 7)))) {
856 while (scan < loceol && isALNUM(*scan))
860 while (scan < loceol && !isALNUM(*scan))
864 while (scan < loceol && isSPACE(*scan))
868 while (scan < loceol && !isSPACE(*scan))
872 while (scan < loceol && isDIGIT(*scan))
876 while (scan < loceol && !isDIGIT(*scan))
879 default: /* Oh dear. Called inappropriately. */
880 FAIL("internal regexp foulup");
891 - regnext - dig the "next" pointer out of a node
893 * [Note, when REGALIGN is defined there are two places in regmatch()
894 * that bypass this code for speed.]