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.1 89/11/11 04:52:04 lwall Locked $
13 * Revision 3.0.1.1 89/11/11 04:52:04 lwall
14 * patch2: /\b$foo/ didn't work
16 * Revision 3.0 89/10/18 15:22:53 lwall
22 * regcomp and regexec -- regsub and regerror are not used in perl
24 * Copyright (c) 1986 by University of Toronto.
25 * Written by Henry Spencer. Not derived from licensed software.
27 * Permission is granted to anyone to use this software for any
28 * purpose on any computer system, and to redistribute it freely,
29 * subject to the following restrictions:
31 * 1. The author is not responsible for the consequences of use of
32 * this software, no matter how awful, even if they arise
35 * 2. The origin of this software must not be misrepresented, either
36 * by explicit claim or by omission.
38 * 3. Altered versions must be plainly marked as such, and must not
39 * be misrepresented as being the original software.
41 **** Alterations to Henry's code are...
43 **** Copyright (c) 1989, Larry Wall
45 **** You may distribute under the terms of the GNU General Public License
46 **** as specified in the README file that comes with the perl 3.0 kit.
48 * Beware that some of this code is subtly aware of the way operator
49 * precedence is structured in regular expressions. Serious changes in
50 * regular-expression syntax might require a total rethink.
69 * Global work variables for regexec().
71 static char *regprecomp;
72 static char *reginput; /* String-input pointer. */
73 static char *regbol; /* Beginning of input, for ^ check. */
74 static char *regeol; /* End of input, for $ check. */
75 static char **regstartp; /* Pointer to startp array. */
76 static char **regendp; /* Ditto for endp. */
77 static char *reglastparen; /* Similarly for lastparen. */
80 static char *regmystartp[10]; /* For remembering backreferences. */
81 static char *regmyendp[10];
87 STATIC int regmatch();
88 STATIC int regrepeat();
93 - regexec - match a regexp against a string
96 regexec(prog, stringarg, strend, strbeg, minend, screamer, safebase)
97 register regexp *prog;
99 register char *strend; /* pointer to null at end of string */
100 char *strbeg; /* real beginning of string */
101 int minend; /* end of match must be at least minend after stringarg */
103 int safebase; /* no need to remember string in subbase */
108 register char *string = stringarg;
110 int minlen = 0; /* must match at least this many chars */
111 int dontbother = 0; /* how many characters not to try at end */
112 int beginning = (string == strbeg); /* is ^ valid at stringarg? */
115 if (prog == NULL || string == NULL) {
116 fatal("NULL regexp parameter");
120 regprecomp = prog->precomp;
121 /* Check validity of program. */
122 if (UCHARAT(prog->program) != MAGIC) {
123 FAIL("corrupted regexp program");
126 if (prog->do_folding) {
129 New(1101,c,i+1,char);
130 (void)bcopy(string, c, i+1);
133 for (s = string; s < strend; s++)
138 /* If there is a "must appear" string, look for it. */
140 if (prog->regmust != Nullstr) {
141 if (beginning && screamer) {
142 if (screamfirst[prog->regmust->str_rare] >= 0)
143 s = screaminstr(screamer,prog->regmust);
149 s = fbminstr((unsigned char*)s, (unsigned char*)strend,
153 ++prog->regmust->str_u.str_useful; /* hooray */
154 goto phooey; /* not present */
156 else if (prog->regback >= 0) {
160 minlen = prog->regback + prog->regmust->str_cur;
162 else if (--prog->regmust->str_u.str_useful < 0) { /* boo */
163 str_free(prog->regmust);
164 prog->regmust = Nullstr; /* disable regmust */
169 minlen = prog->regmust->str_cur;
173 /* Mark beginning of line for ^ . */
179 /* Mark end of line for $ (and such) */
182 /* see how far we have to get to not match where we matched before */
183 regtill = string+minend;
185 /* Simplest case: anchored match need be tried only once. */
186 /* [unless multiline is set] */
188 if (regtry(prog, string))
190 else if (multiline) {
192 dontbother = minlen - 1;
193 strend -= dontbother;
194 /* for multiline we only have to try after newlines */
197 for (; s < strend; s++) {
199 if (++s < strend && regtry(prog, s))
207 /* Messy cases: unanchored match. */
208 if (prog->regstart) {
209 /* We know what string it must start with. */
210 if (prog->regstart->str_pok == 3) {
212 while ((s = fbminstr((unsigned char*)s,
213 (unsigned char*)strend, prog->regstart)) != NULL)
224 c = prog->regstart->str_ptr;
225 while ((s = ninstr(s, strend,
226 c, c + prog->regstart->str_cur )) != NULL) {
234 if (c = prog->regstclass) {
236 dontbother = minlen - 1;
237 strend -= dontbother; /* don't bother with what can't match */
238 /* We know what class it must start with. */
240 case ANYOF: case ANYBUT:
244 if (!(c[i >> 3] & (1 << (i&7))))
252 dontbother++,strend--;
255 tmp = (isalpha(i) || isdigit(i) || i == '_');
258 tmp = 0; /* assume not alphanumeric */
261 if (tmp != (isalpha(i) || isdigit(i) || i == '_')) {
268 if ((minlen || tmp) && regtry(prog,s))
273 dontbother++,strend--;
276 tmp = (isalpha(i) || isdigit(i) || i == '_');
279 tmp = 0; /* assume not alphanumeric */
282 if (tmp != (isalpha(i) || isdigit(i) || i == '_'))
284 else if (regtry(prog, s))
288 if ((minlen || !tmp) && regtry(prog,s))
294 if (isalpha(i) || isdigit(i) || i == '_')
303 if (!isalpha(i) && !isdigit(i) && i != '_')
345 strend -= dontbother;
346 /* We don't know much -- general case. */
350 } while (s++ < strend);
357 if ((!safebase && (prog->nparens || sawampersand)) || prog->do_folding){
358 strend += dontbother; /* uncheat */
359 if (safebase) /* no need for $digit later */
361 else if (strbeg != prog->subbase) {
362 i = strend - string + (stringarg - strbeg);
363 s = nsavestr(strbeg,i); /* so $digit will work later */
365 Safefree(prog->subbase);
370 s += (stringarg - strbeg);
371 for (i = 0; i <= prog->nparens; i++) {
373 prog->startp[i] = s + (prog->startp[i] - string);
374 prog->endp[i] = s + (prog->endp[i] - string);
377 if (prog->do_folding)
383 if (prog->do_folding)
389 - regtry - try match at specific point
391 static int /* 0 failure, 1 success */
401 regstartp = prog->startp;
402 regendp = prog->endp;
403 reglastparen = &prog->lastparen;
409 for (i = NSUBEXP; i > 0; i--) {
414 if (regmatch(prog->program + 1) && reginput >= regtill) {
415 prog->startp[0] = string;
416 prog->endp[0] = reginput;
423 - regmatch - main matching routine
425 * Conceptually the strategy is simple: check to see whether the current
426 * node matches, call self recursively to see whether the rest matches,
427 * and then act accordingly. In practice we make some effort to avoid
428 * recursion, in particular by going through "ordinary" nodes (that don't
429 * need to know whether the rest of the match failed) by a loop instead of
432 /* [lwall] I've hoisted the register declarations to the outer block in order to
433 * maybe save a little bit of pushing and popping on the stack. It also takes
434 * advantage of machines that use a register save mask on subroutine entry.
436 static int /* 0 failure, 1 success */
440 register char *scan; /* Current node. */
441 char *next; /* Next node. */
442 register int nextchar;
443 register int n; /* no or next */
444 register int ln; /* len or last */
445 register char *s; /* operand or save */
446 register char *locinput = reginput;
448 nextchar = *locinput;
451 if (scan != NULL && regnarrate)
452 fprintf(stderr, "%s(\n", regprop(scan));
454 while (scan != NULL) {
457 fprintf(stderr, "%s...\n", regprop(scan));
461 next = scan + NEXT(scan);
465 next = regnext(scan);
470 if (locinput == regbol ||
471 ((nextchar || locinput < regeol) &&
472 locinput[-1] == '\n') )
479 if ((nextchar || locinput < regeol) && nextchar != '\n')
484 if ((nextchar == '\0' && locinput >= regeol) ||
487 nextchar = *++locinput;
492 /* Inline the first character, for speed. */
495 if (locinput + ln > regeol)
497 if (ln > 1 && bcmp(s, locinput, ln) != 0)
500 nextchar = *locinput;
506 nextchar = UCHARAT(locinput);
507 if (s[nextchar >> 3] & (1 << (nextchar&7)))
509 nextchar = *++locinput;
510 if (!nextchar && locinput > regeol)
516 if (!isalpha(nextchar) && !isdigit(nextchar) &&
519 nextchar = *++locinput;
522 if (!nextchar && locinput >= regeol)
524 if (isalpha(nextchar) || isdigit(nextchar) ||
527 nextchar = *++locinput;
531 if (locinput == regbol) /* was last char in word? */
534 ln = (isalpha(locinput[-1]) ||
535 isdigit(locinput[-1]) ||
536 locinput[-1] == '_' );
537 n = (isalpha(nextchar) || isdigit(nextchar) ||
538 nextchar == '_' ); /* is next char in word? */
539 if ((ln == n) == (OP(scan) == BOUND))
543 if (!nextchar && locinput >= regeol)
545 if (!isspace(nextchar))
547 nextchar = *++locinput;
552 if (isspace(nextchar))
554 nextchar = *++locinput;
557 if (!isdigit(nextchar))
559 nextchar = *++locinput;
562 if (!nextchar && locinput >= regeol)
564 if (isdigit(nextchar))
566 nextchar = *++locinput;
584 if (s == regmyendp[n])
586 /* Inline the first character, for speed. */
589 ln = regmyendp[n] - s;
590 if (locinput + ln > regeol)
592 if (ln > 1 && bcmp(s, locinput, ln) != 0)
595 nextchar = *locinput;
614 regmystartp[n] = locinput; /* for REF */
615 if (regmatch(next)) {
617 * Don't set startp if some later
618 * invocation of the same parentheses
621 if (regstartp[n] == NULL)
622 regstartp[n] = locinput;
636 n = OP(scan) - CLOSE;
639 regmyendp[n] = locinput; /* for REF */
640 if (regmatch(next)) {
642 * Don't set endp if some later
643 * invocation of the same parentheses
646 if (regendp[n] == NULL) {
647 regendp[n] = locinput;
648 if (n > *reglastparen)
657 if (OP(next) != BRANCH) /* No choice. */
658 next = NEXTOPER(scan); /* Avoid recursion. */
662 if (regmatch(NEXTOPER(scan)))
670 scan = regnext(scan);
672 } while (scan != NULL && OP(scan) == BRANCH);
681 * Lookahead to avoid useless match attempts
682 * when we know what character comes next.
684 if (OP(next) == EXACTLY)
685 nextchar = *(OPERAND(next)+1);
688 ln = (OP(scan) == STAR) ? 0 : 1;
690 n = regrepeat(NEXTOPER(scan));
692 /* If it could work, try it. */
693 if (nextchar == -1000 || *reginput == nextchar)
696 /* Couldn't or didn't -- back up. */
698 reginput = locinput + n;
702 reginput = locinput; /* put where regtry can find it */
703 return(1); /* Success! */
705 printf("%x %d\n",scan,scan[1]);
706 FAIL("regexp memory corruption");
713 * We get here only if there's trouble -- normally "case END" is
714 * the terminating point.
716 FAIL("corrupted regexp pointers");
724 - regrepeat - repeatedly match something simple, report how many
727 * [This routine now assumes that it will only match on things of length 1.
728 * That was true before, but now we assume scan - reginput is the count,
729 * rather than incrementing count on every character.]
738 register char *loceol = regeol;
744 while (scan < loceol && *scan != '\n')
747 case EXACTLY: /* length of string is 1 */
749 while (scan < loceol && *opnd == *scan)
755 while (scan < loceol && !(opnd[c >> 3] & (1 << (c & 7)))) {
761 while (isalpha(*scan) || isdigit(*scan) || *scan == '_')
765 while (scan < loceol && (!isalpha(*scan) && !isdigit(*scan) &&
770 while (scan < loceol && isspace(*scan))
774 while (scan < loceol && !isspace(*scan))
778 while (isdigit(*scan))
782 while (scan < loceol && !isdigit(*scan))
785 default: /* Oh dear. Called inappropriately. */
786 FAIL("internal regexp foulup");
797 - regnext - dig the "next" pointer out of a node
799 * [Note, when REGALIGN is defined there are two places in regmatch()
800 * that bypass this code for speed.]