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.3 90/02/28 18:14:39 lwall Locked $
13 * Revision 3.0.1.3 90/02/28 18:14:39 lwall
14 * patch9: /[\200-\377]/ didn't work on machines with signed chars
15 * patch9: \d, \w, and \s could misfire on characters with high bit set
16 * patch9: /\bfoo/i didn't work
18 * Revision 3.0.1.2 89/12/21 20:16:27 lwall
19 * patch7: certain patterns didn't match correctly at end of string
21 * Revision 3.0.1.1 89/11/11 04:52:04 lwall
22 * patch2: /\b$foo/ didn't work
24 * Revision 3.0 89/10/18 15:22:53 lwall
30 * regcomp and regexec -- regsub and regerror are not used in perl
32 * Copyright (c) 1986 by University of Toronto.
33 * Written by Henry Spencer. Not derived from licensed software.
35 * Permission is granted to anyone to use this software for any
36 * purpose on any computer system, and to redistribute it freely,
37 * subject to the following restrictions:
39 * 1. The author is not responsible for the consequences of use of
40 * this software, no matter how awful, even if they arise
43 * 2. The origin of this software must not be misrepresented, either
44 * by explicit claim or by omission.
46 * 3. Altered versions must be plainly marked as such, and must not
47 * be misrepresented as being the original software.
49 **** Alterations to Henry's code are...
51 **** Copyright (c) 1989, Larry Wall
53 **** You may distribute under the terms of the GNU General Public License
54 **** as specified in the README file that comes with the perl 3.0 kit.
56 * Beware that some of this code is subtly aware of the way operator
57 * precedence is structured in regular expressions. Serious changes in
58 * regular-expression syntax might require a total rethink.
72 #define isALNUM(c) (isascii(c) && (isalpha(c) || isdigit(c) || c == '_'))
73 #define isSPACE(c) (isascii(c) && isspace(c))
74 #define isDIGIT(c) (isascii(c) && isdigit(c))
75 #define isUPPER(c) (isascii(c) && isupper(c))
82 * Global work variables for regexec().
84 static char *regprecomp;
85 static char *reginput; /* String-input pointer. */
86 static char regprev; /* char before regbol, \n if none */
87 static char *regbol; /* Beginning of input, for ^ check. */
88 static char *regeol; /* End of input, for $ check. */
89 static char **regstartp; /* Pointer to startp array. */
90 static char **regendp; /* Ditto for endp. */
91 static char *reglastparen; /* Similarly for lastparen. */
94 static char *regmystartp[10]; /* For remembering backreferences. */
95 static char *regmyendp[10];
101 STATIC int regmatch();
102 STATIC int regrepeat();
104 extern int multiline;
107 - regexec - match a regexp against a string
110 regexec(prog, stringarg, strend, strbeg, minend, screamer, safebase)
111 register regexp *prog;
113 register char *strend; /* pointer to null at end of string */
114 char *strbeg; /* real beginning of string */
115 int minend; /* end of match must be at least minend after stringarg */
117 int safebase; /* no need to remember string in subbase */
122 register char *string = stringarg;
124 int minlen = 0; /* must match at least this many chars */
125 int dontbother = 0; /* how many characters not to try at end */
128 if (prog == NULL || string == NULL) {
129 fatal("NULL regexp parameter");
133 if (string == strbeg) /* is ^ valid at stringarg? */
136 regprev = stringarg[-1];
137 regprecomp = prog->precomp;
138 /* Check validity of program. */
139 if (UCHARAT(prog->program) != MAGIC) {
140 FAIL("corrupted regexp program");
143 if (prog->do_folding) {
146 New(1101,c,i+1,char);
147 (void)bcopy(string, c, i+1);
150 for (s = string; s < strend; s++)
155 /* If there is a "must appear" string, look for it. */
157 if (prog->regmust != Nullstr) {
158 if (stringarg == strbeg && screamer) {
159 if (screamfirst[prog->regmust->str_rare] >= 0)
160 s = screaminstr(screamer,prog->regmust);
166 s = fbminstr((unsigned char*)s, (unsigned char*)strend,
170 ++prog->regmust->str_u.str_useful; /* hooray */
171 goto phooey; /* not present */
173 else if (prog->regback >= 0) {
177 minlen = prog->regback + prog->regmust->str_cur;
179 else if (--prog->regmust->str_u.str_useful < 0) { /* boo */
180 str_free(prog->regmust);
181 prog->regmust = Nullstr; /* disable regmust */
186 minlen = prog->regmust->str_cur;
190 /* Mark beginning of line for ^ . */
193 /* Mark end of line for $ (and such) */
196 /* see how far we have to get to not match where we matched before */
197 regtill = string+minend;
199 /* Simplest case: anchored match need be tried only once. */
200 /* [unless multiline is set] */
202 if (regtry(prog, string))
204 else if (multiline) {
206 dontbother = minlen - 1;
207 strend -= dontbother;
208 /* for multiline we only have to try after newlines */
211 for (; s < strend; s++) {
213 if (++s < strend && regtry(prog, s))
221 /* Messy cases: unanchored match. */
222 if (prog->regstart) {
223 /* We know what string it must start with. */
224 if (prog->regstart->str_pok == 3) {
226 while ((s = fbminstr((unsigned char*)s,
227 (unsigned char*)strend, prog->regstart)) != NULL)
238 c = prog->regstart->str_ptr;
239 while ((s = ninstr(s, strend,
240 c, c + prog->regstart->str_cur )) != NULL) {
248 if (c = prog->regstclass) {
250 dontbother = minlen - 1;
251 strend -= dontbother; /* don't bother with what can't match */
252 /* We know what class it must start with. */
254 case ANYOF: case ANYBUT:
258 if (!(c[i >> 3] & (1 << (i&7))))
266 dontbother++,strend--;
272 tmp = isALNUM(regprev); /* assume not alphanumeric */
275 if (tmp != isALNUM(i)) {
282 if ((minlen || tmp) && regtry(prog,s))
287 dontbother++,strend--;
293 tmp = isALNUM(regprev); /* assume not alphanumeric */
296 if (tmp != isALNUM(i))
298 else if (regtry(prog, s))
302 if ((minlen || !tmp) && regtry(prog,s))
359 dontbother = minlen - 1;
360 strend -= dontbother;
361 /* We don't know much -- general case. */
365 } while (s++ < strend);
372 if ((!safebase && (prog->nparens || sawampersand)) || prog->do_folding){
373 strend += dontbother; /* uncheat */
374 if (safebase) /* no need for $digit later */
376 else if (strbeg != prog->subbase) {
377 i = strend - string + (stringarg - strbeg);
378 s = nsavestr(strbeg,i); /* so $digit will work later */
380 Safefree(prog->subbase);
385 s += (stringarg - strbeg);
386 for (i = 0; i <= prog->nparens; i++) {
388 prog->startp[i] = s + (prog->startp[i] - string);
389 prog->endp[i] = s + (prog->endp[i] - string);
392 if (prog->do_folding)
398 if (prog->do_folding)
404 - regtry - try match at specific point
406 static int /* 0 failure, 1 success */
416 regstartp = prog->startp;
417 regendp = prog->endp;
418 reglastparen = &prog->lastparen;
424 for (i = NSUBEXP; i > 0; i--) {
429 if (regmatch(prog->program + 1) && reginput >= regtill) {
430 prog->startp[0] = string;
431 prog->endp[0] = reginput;
438 - regmatch - main matching routine
440 * Conceptually the strategy is simple: check to see whether the current
441 * node matches, call self recursively to see whether the rest matches,
442 * and then act accordingly. In practice we make some effort to avoid
443 * recursion, in particular by going through "ordinary" nodes (that don't
444 * need to know whether the rest of the match failed) by a loop instead of
447 /* [lwall] I've hoisted the register declarations to the outer block in order to
448 * maybe save a little bit of pushing and popping on the stack. It also takes
449 * advantage of machines that use a register save mask on subroutine entry.
451 static int /* 0 failure, 1 success */
455 register char *scan; /* Current node. */
456 char *next; /* Next node. */
457 register int nextchar;
458 register int n; /* no or next */
459 register int ln; /* len or last */
460 register char *s; /* operand or save */
461 register char *locinput = reginput;
463 nextchar = *locinput;
466 if (scan != NULL && regnarrate)
467 fprintf(stderr, "%s(\n", regprop(scan));
469 while (scan != NULL) {
472 fprintf(stderr, "%s...\n", regprop(scan));
476 next = scan + NEXT(scan);
480 next = regnext(scan);
485 if (locinput == regbol ? regprev == '\n' :
486 ((nextchar || locinput < regeol) &&
487 locinput[-1] == '\n') )
494 if ((nextchar || locinput < regeol) && nextchar != '\n')
499 if ((nextchar == '\0' && locinput >= regeol) ||
502 nextchar = *++locinput;
507 /* Inline the first character, for speed. */
510 if (locinput + ln > regeol)
512 if (ln > 1 && bcmp(s, locinput, ln) != 0)
515 nextchar = *locinput;
521 nextchar = UCHARAT(locinput);
522 if (s[nextchar >> 3] & (1 << (nextchar&7)))
524 nextchar = *++locinput;
525 if (!nextchar && locinput > regeol)
531 if (!isALNUM(nextchar))
533 nextchar = *++locinput;
536 if (!nextchar && locinput >= regeol)
538 if (isALNUM(nextchar))
540 nextchar = *++locinput;
544 if (locinput == regbol) /* was last char in word? */
545 ln = isALNUM(regprev);
547 ln = isALNUM(locinput[-1]);
548 n = isALNUM(nextchar); /* is next char in word? */
549 if ((ln == n) == (OP(scan) == BOUND))
553 if (!nextchar && locinput >= regeol)
555 if (!isSPACE(nextchar))
557 nextchar = *++locinput;
562 if (isSPACE(nextchar))
564 nextchar = *++locinput;
567 if (!isDIGIT(nextchar))
569 nextchar = *++locinput;
572 if (!nextchar && locinput >= regeol)
574 if (isDIGIT(nextchar))
576 nextchar = *++locinput;
594 if (s == regmyendp[n])
596 /* Inline the first character, for speed. */
599 ln = regmyendp[n] - s;
600 if (locinput + ln > regeol)
602 if (ln > 1 && bcmp(s, locinput, ln) != 0)
605 nextchar = *locinput;
624 regmystartp[n] = locinput; /* for REF */
625 if (regmatch(next)) {
627 * Don't set startp if some later
628 * invocation of the same parentheses
631 if (regstartp[n] == NULL)
632 regstartp[n] = locinput;
646 n = OP(scan) - CLOSE;
649 regmyendp[n] = locinput; /* for REF */
650 if (regmatch(next)) {
652 * Don't set endp if some later
653 * invocation of the same parentheses
656 if (regendp[n] == NULL) {
657 regendp[n] = locinput;
658 if (n > *reglastparen)
667 if (OP(next) != BRANCH) /* No choice. */
668 next = NEXTOPER(scan); /* Avoid recursion. */
672 if (regmatch(NEXTOPER(scan)))
680 scan = regnext(scan);
682 } while (scan != NULL && OP(scan) == BRANCH);
691 * Lookahead to avoid useless match attempts
692 * when we know what character comes next.
694 if (OP(next) == EXACTLY)
695 nextchar = *(OPERAND(next)+1);
698 ln = (OP(scan) == STAR) ? 0 : 1;
700 n = regrepeat(NEXTOPER(scan));
702 /* If it could work, try it. */
703 if (nextchar == -1000 || *reginput == nextchar)
706 /* Couldn't or didn't -- back up. */
708 reginput = locinput + n;
712 reginput = locinput; /* put where regtry can find it */
713 return(1); /* Success! */
715 printf("%x %d\n",scan,scan[1]);
716 FAIL("regexp memory corruption");
723 * We get here only if there's trouble -- normally "case END" is
724 * the terminating point.
726 FAIL("corrupted regexp pointers");
734 - regrepeat - repeatedly match something simple, report how many
737 * [This routine now assumes that it will only match on things of length 1.
738 * That was true before, but now we assume scan - reginput is the count,
739 * rather than incrementing count on every character.]
748 register char *loceol = regeol;
754 while (scan < loceol && *scan != '\n')
757 case EXACTLY: /* length of string is 1 */
759 while (scan < loceol && *opnd == *scan)
765 while (scan < loceol && !(opnd[c >> 3] & (1 << (c & 7)))) {
771 while (isALNUM(*scan))
775 while (scan < loceol && !isALNUM(*scan))
779 while (scan < loceol && isSPACE(*scan))
783 while (scan < loceol && !isSPACE(*scan))
787 while (isDIGIT(*scan))
791 while (scan < loceol && !isDIGIT(*scan))
794 default: /* Oh dear. Called inappropriately. */
795 FAIL("internal regexp foulup");
806 - regnext - dig the "next" pointer out of a node
808 * [Note, when REGALIGN is defined there are two places in regmatch()
809 * that bypass this code for speed.]