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.2 89/12/21 20:16:27 lwall Locked $
13 * Revision 3.0.1.2 89/12/21 20:16:27 lwall
14 * patch7: certain patterns didn't match correctly at end of string
16 * Revision 3.0.1.1 89/11/11 04:52:04 lwall
17 * patch2: /\b$foo/ didn't work
19 * Revision 3.0 89/10/18 15:22:53 lwall
25 * regcomp and regexec -- regsub and regerror are not used in perl
27 * Copyright (c) 1986 by University of Toronto.
28 * Written by Henry Spencer. Not derived from licensed software.
30 * Permission is granted to anyone to use this software for any
31 * purpose on any computer system, and to redistribute it freely,
32 * subject to the following restrictions:
34 * 1. The author is not responsible for the consequences of use of
35 * this software, no matter how awful, even if they arise
38 * 2. The origin of this software must not be misrepresented, either
39 * by explicit claim or by omission.
41 * 3. Altered versions must be plainly marked as such, and must not
42 * be misrepresented as being the original software.
44 **** Alterations to Henry's code are...
46 **** Copyright (c) 1989, Larry Wall
48 **** You may distribute under the terms of the GNU General Public License
49 **** as specified in the README file that comes with the perl 3.0 kit.
51 * Beware that some of this code is subtly aware of the way operator
52 * precedence is structured in regular expressions. Serious changes in
53 * regular-expression syntax might require a total rethink.
72 * Global work variables for regexec().
74 static char *regprecomp;
75 static char *reginput; /* String-input pointer. */
76 static char *regbol; /* Beginning of input, for ^ check. */
77 static char *regeol; /* End of input, for $ check. */
78 static char **regstartp; /* Pointer to startp array. */
79 static char **regendp; /* Ditto for endp. */
80 static char *reglastparen; /* Similarly for lastparen. */
83 static char *regmystartp[10]; /* For remembering backreferences. */
84 static char *regmyendp[10];
90 STATIC int regmatch();
91 STATIC int regrepeat();
96 - regexec - match a regexp against a string
99 regexec(prog, stringarg, strend, strbeg, minend, screamer, safebase)
100 register regexp *prog;
102 register char *strend; /* pointer to null at end of string */
103 char *strbeg; /* real beginning of string */
104 int minend; /* end of match must be at least minend after stringarg */
106 int safebase; /* no need to remember string in subbase */
111 register char *string = stringarg;
113 int minlen = 0; /* must match at least this many chars */
114 int dontbother = 0; /* how many characters not to try at end */
115 int beginning = (string == strbeg); /* is ^ valid at stringarg? */
118 if (prog == NULL || string == NULL) {
119 fatal("NULL regexp parameter");
123 regprecomp = prog->precomp;
124 /* Check validity of program. */
125 if (UCHARAT(prog->program) != MAGIC) {
126 FAIL("corrupted regexp program");
129 if (prog->do_folding) {
132 New(1101,c,i+1,char);
133 (void)bcopy(string, c, i+1);
136 for (s = string; s < strend; s++)
141 /* If there is a "must appear" string, look for it. */
143 if (prog->regmust != Nullstr) {
144 if (beginning && screamer) {
145 if (screamfirst[prog->regmust->str_rare] >= 0)
146 s = screaminstr(screamer,prog->regmust);
152 s = fbminstr((unsigned char*)s, (unsigned char*)strend,
156 ++prog->regmust->str_u.str_useful; /* hooray */
157 goto phooey; /* not present */
159 else if (prog->regback >= 0) {
163 minlen = prog->regback + prog->regmust->str_cur;
165 else if (--prog->regmust->str_u.str_useful < 0) { /* boo */
166 str_free(prog->regmust);
167 prog->regmust = Nullstr; /* disable regmust */
172 minlen = prog->regmust->str_cur;
176 /* Mark beginning of line for ^ . */
182 /* Mark end of line for $ (and such) */
185 /* see how far we have to get to not match where we matched before */
186 regtill = string+minend;
188 /* Simplest case: anchored match need be tried only once. */
189 /* [unless multiline is set] */
191 if (regtry(prog, string))
193 else if (multiline) {
195 dontbother = minlen - 1;
196 strend -= dontbother;
197 /* for multiline we only have to try after newlines */
200 for (; s < strend; s++) {
202 if (++s < strend && regtry(prog, s))
210 /* Messy cases: unanchored match. */
211 if (prog->regstart) {
212 /* We know what string it must start with. */
213 if (prog->regstart->str_pok == 3) {
215 while ((s = fbminstr((unsigned char*)s,
216 (unsigned char*)strend, prog->regstart)) != NULL)
227 c = prog->regstart->str_ptr;
228 while ((s = ninstr(s, strend,
229 c, c + prog->regstart->str_cur )) != NULL) {
237 if (c = prog->regstclass) {
239 dontbother = minlen - 1;
240 strend -= dontbother; /* don't bother with what can't match */
241 /* We know what class it must start with. */
243 case ANYOF: case ANYBUT:
247 if (!(c[i >> 3] & (1 << (i&7))))
255 dontbother++,strend--;
258 tmp = (isalpha(i) || isdigit(i) || i == '_');
261 tmp = 0; /* assume not alphanumeric */
264 if (tmp != (isalpha(i) || isdigit(i) || i == '_')) {
271 if ((minlen || tmp) && regtry(prog,s))
276 dontbother++,strend--;
279 tmp = (isalpha(i) || isdigit(i) || i == '_');
282 tmp = 0; /* assume not alphanumeric */
285 if (tmp != (isalpha(i) || isdigit(i) || i == '_'))
287 else if (regtry(prog, s))
291 if ((minlen || !tmp) && regtry(prog,s))
297 if (isalpha(i) || isdigit(i) || i == '_')
306 if (!isalpha(i) && !isdigit(i) && i != '_')
348 dontbother = minlen - 1;
349 strend -= dontbother;
350 /* We don't know much -- general case. */
354 } while (s++ < strend);
361 if ((!safebase && (prog->nparens || sawampersand)) || prog->do_folding){
362 strend += dontbother; /* uncheat */
363 if (safebase) /* no need for $digit later */
365 else if (strbeg != prog->subbase) {
366 i = strend - string + (stringarg - strbeg);
367 s = nsavestr(strbeg,i); /* so $digit will work later */
369 Safefree(prog->subbase);
374 s += (stringarg - strbeg);
375 for (i = 0; i <= prog->nparens; i++) {
377 prog->startp[i] = s + (prog->startp[i] - string);
378 prog->endp[i] = s + (prog->endp[i] - string);
381 if (prog->do_folding)
387 if (prog->do_folding)
393 - regtry - try match at specific point
395 static int /* 0 failure, 1 success */
405 regstartp = prog->startp;
406 regendp = prog->endp;
407 reglastparen = &prog->lastparen;
413 for (i = NSUBEXP; i > 0; i--) {
418 if (regmatch(prog->program + 1) && reginput >= regtill) {
419 prog->startp[0] = string;
420 prog->endp[0] = reginput;
427 - regmatch - main matching routine
429 * Conceptually the strategy is simple: check to see whether the current
430 * node matches, call self recursively to see whether the rest matches,
431 * and then act accordingly. In practice we make some effort to avoid
432 * recursion, in particular by going through "ordinary" nodes (that don't
433 * need to know whether the rest of the match failed) by a loop instead of
436 /* [lwall] I've hoisted the register declarations to the outer block in order to
437 * maybe save a little bit of pushing and popping on the stack. It also takes
438 * advantage of machines that use a register save mask on subroutine entry.
440 static int /* 0 failure, 1 success */
444 register char *scan; /* Current node. */
445 char *next; /* Next node. */
446 register int nextchar;
447 register int n; /* no or next */
448 register int ln; /* len or last */
449 register char *s; /* operand or save */
450 register char *locinput = reginput;
452 nextchar = *locinput;
455 if (scan != NULL && regnarrate)
456 fprintf(stderr, "%s(\n", regprop(scan));
458 while (scan != NULL) {
461 fprintf(stderr, "%s...\n", regprop(scan));
465 next = scan + NEXT(scan);
469 next = regnext(scan);
474 if (locinput == regbol ||
475 ((nextchar || locinput < regeol) &&
476 locinput[-1] == '\n') )
483 if ((nextchar || locinput < regeol) && nextchar != '\n')
488 if ((nextchar == '\0' && locinput >= regeol) ||
491 nextchar = *++locinput;
496 /* Inline the first character, for speed. */
499 if (locinput + ln > regeol)
501 if (ln > 1 && bcmp(s, locinput, ln) != 0)
504 nextchar = *locinput;
510 nextchar = UCHARAT(locinput);
511 if (s[nextchar >> 3] & (1 << (nextchar&7)))
513 nextchar = *++locinput;
514 if (!nextchar && locinput > regeol)
520 if (!isalpha(nextchar) && !isdigit(nextchar) &&
523 nextchar = *++locinput;
526 if (!nextchar && locinput >= regeol)
528 if (isalpha(nextchar) || isdigit(nextchar) ||
531 nextchar = *++locinput;
535 if (locinput == regbol) /* was last char in word? */
538 ln = (isalpha(locinput[-1]) ||
539 isdigit(locinput[-1]) ||
540 locinput[-1] == '_' );
541 n = (isalpha(nextchar) || isdigit(nextchar) ||
542 nextchar == '_' ); /* is next char in word? */
543 if ((ln == n) == (OP(scan) == BOUND))
547 if (!nextchar && locinput >= regeol)
549 if (!isspace(nextchar))
551 nextchar = *++locinput;
556 if (isspace(nextchar))
558 nextchar = *++locinput;
561 if (!isdigit(nextchar))
563 nextchar = *++locinput;
566 if (!nextchar && locinput >= regeol)
568 if (isdigit(nextchar))
570 nextchar = *++locinput;
588 if (s == regmyendp[n])
590 /* Inline the first character, for speed. */
593 ln = regmyendp[n] - s;
594 if (locinput + ln > regeol)
596 if (ln > 1 && bcmp(s, locinput, ln) != 0)
599 nextchar = *locinput;
618 regmystartp[n] = locinput; /* for REF */
619 if (regmatch(next)) {
621 * Don't set startp if some later
622 * invocation of the same parentheses
625 if (regstartp[n] == NULL)
626 regstartp[n] = locinput;
640 n = OP(scan) - CLOSE;
643 regmyendp[n] = locinput; /* for REF */
644 if (regmatch(next)) {
646 * Don't set endp if some later
647 * invocation of the same parentheses
650 if (regendp[n] == NULL) {
651 regendp[n] = locinput;
652 if (n > *reglastparen)
661 if (OP(next) != BRANCH) /* No choice. */
662 next = NEXTOPER(scan); /* Avoid recursion. */
666 if (regmatch(NEXTOPER(scan)))
674 scan = regnext(scan);
676 } while (scan != NULL && OP(scan) == BRANCH);
685 * Lookahead to avoid useless match attempts
686 * when we know what character comes next.
688 if (OP(next) == EXACTLY)
689 nextchar = *(OPERAND(next)+1);
692 ln = (OP(scan) == STAR) ? 0 : 1;
694 n = regrepeat(NEXTOPER(scan));
696 /* If it could work, try it. */
697 if (nextchar == -1000 || *reginput == nextchar)
700 /* Couldn't or didn't -- back up. */
702 reginput = locinput + n;
706 reginput = locinput; /* put where regtry can find it */
707 return(1); /* Success! */
709 printf("%x %d\n",scan,scan[1]);
710 FAIL("regexp memory corruption");
717 * We get here only if there's trouble -- normally "case END" is
718 * the terminating point.
720 FAIL("corrupted regexp pointers");
728 - regrepeat - repeatedly match something simple, report how many
731 * [This routine now assumes that it will only match on things of length 1.
732 * That was true before, but now we assume scan - reginput is the count,
733 * rather than incrementing count on every character.]
742 register char *loceol = regeol;
748 while (scan < loceol && *scan != '\n')
751 case EXACTLY: /* length of string is 1 */
753 while (scan < loceol && *opnd == *scan)
759 while (scan < loceol && !(opnd[c >> 3] & (1 << (c & 7)))) {
765 while (isalpha(*scan) || isdigit(*scan) || *scan == '_')
769 while (scan < loceol && (!isalpha(*scan) && !isdigit(*scan) &&
774 while (scan < loceol && isspace(*scan))
778 while (scan < loceol && !isspace(*scan))
782 while (isdigit(*scan))
786 while (scan < loceol && !isdigit(*scan))
789 default: /* Oh dear. Called inappropriately. */
790 FAIL("internal regexp foulup");
801 - regnext - dig the "next" pointer out of a node
803 * [Note, when REGALIGN is defined there are two places in regmatch()
804 * that bypass this code for speed.]