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 89/10/18 15:22:53 lwall Locked $
13 * Revision 3.0 89/10/18 15:22:53 lwall
19 * regcomp and regexec -- regsub and regerror are not used in perl
21 * Copyright (c) 1986 by University of Toronto.
22 * Written by Henry Spencer. Not derived from licensed software.
24 * Permission is granted to anyone to use this software for any
25 * purpose on any computer system, and to redistribute it freely,
26 * subject to the following restrictions:
28 * 1. The author is not responsible for the consequences of use of
29 * this software, no matter how awful, even if they arise
32 * 2. The origin of this software must not be misrepresented, either
33 * by explicit claim or by omission.
35 * 3. Altered versions must be plainly marked as such, and must not
36 * be misrepresented as being the original software.
38 **** Alterations to Henry's code are...
40 **** Copyright (c) 1989, Larry Wall
42 **** You may distribute under the terms of the GNU General Public License
43 **** as specified in the README file that comes with the perl 3.0 kit.
45 * Beware that some of this code is subtly aware of the way operator
46 * precedence is structured in regular expressions. Serious changes in
47 * regular-expression syntax might require a total rethink.
66 * Global work variables for regexec().
68 static char *regprecomp;
69 static char *reginput; /* String-input pointer. */
70 static char *regbol; /* Beginning of input, for ^ check. */
71 static char *regeol; /* End of input, for $ check. */
72 static char **regstartp; /* Pointer to startp array. */
73 static char **regendp; /* Ditto for endp. */
74 static char *reglastparen; /* Similarly for lastparen. */
77 static char *regmystartp[10]; /* For remembering backreferences. */
78 static char *regmyendp[10];
84 STATIC int regmatch();
85 STATIC int regrepeat();
90 - regexec - match a regexp against a string
93 regexec(prog, stringarg, strend, strbeg, minend, screamer, safebase)
94 register regexp *prog;
96 register char *strend; /* pointer to null at end of string */
97 char *strbeg; /* real beginning of string */
98 int minend; /* end of match must be at least minend after stringarg */
100 int safebase; /* no need to remember string in subbase */
105 register char *string = stringarg;
107 int minlen = 0; /* must match at least this many chars */
108 int dontbother = 0; /* how many characters not to try at end */
109 int beginning = (string == strbeg); /* is ^ valid at stringarg? */
112 if (prog == NULL || string == NULL) {
113 fatal("NULL regexp parameter");
117 regprecomp = prog->precomp;
118 /* Check validity of program. */
119 if (UCHARAT(prog->program) != MAGIC) {
120 FAIL("corrupted regexp program");
123 if (prog->do_folding) {
126 New(1101,c,i+1,char);
127 (void)bcopy(string, c, i+1);
130 for (s = string; s < strend; s++)
135 /* If there is a "must appear" string, look for it. */
137 if (prog->regmust != Nullstr) {
138 if (beginning && screamer) {
139 if (screamfirst[prog->regmust->str_rare] >= 0)
140 s = screaminstr(screamer,prog->regmust);
146 s = fbminstr((unsigned char*)s, (unsigned char*)strend,
150 ++prog->regmust->str_u.str_useful; /* hooray */
151 goto phooey; /* not present */
153 else if (prog->regback >= 0) {
157 minlen = prog->regback + prog->regmust->str_cur;
159 else if (--prog->regmust->str_u.str_useful < 0) { /* boo */
160 str_free(prog->regmust);
161 prog->regmust = Nullstr; /* disable regmust */
166 minlen = prog->regmust->str_cur;
170 /* Mark beginning of line for ^ . */
176 /* Mark end of line for $ (and such) */
179 /* see how far we have to get to not match where we matched before */
180 regtill = string+minend;
182 /* Simplest case: anchored match need be tried only once. */
183 /* [unless multiline is set] */
185 if (regtry(prog, string))
187 else if (multiline) {
189 dontbother = minlen - 1;
190 strend -= dontbother;
191 /* for multiline we only have to try after newlines */
194 for (; s < strend; s++) {
196 if (++s < strend && regtry(prog, s))
204 /* Messy cases: unanchored match. */
205 if (prog->regstart) {
206 /* We know what string it must start with. */
207 if (prog->regstart->str_pok == 3) {
209 while ((s = fbminstr((unsigned char*)s,
210 (unsigned char*)strend, prog->regstart)) != NULL)
221 c = prog->regstart->str_ptr;
222 while ((s = ninstr(s, strend,
223 c, c + prog->regstart->str_cur )) != NULL) {
231 if (c = prog->regstclass) {
233 dontbother = minlen - 1;
234 strend -= dontbother; /* don't bother with what can't match */
235 /* We know what class it must start with. */
237 case ANYOF: case ANYBUT:
241 if (!(c[i >> 3] & (1 << (i&7))))
249 dontbother++,strend--;
252 tmp = (isalpha(i) || isdigit(i) || i == '_');
255 tmp = 0; /* assume not alphanumeric */
258 if (tmp != (isalpha(i) || isdigit(i) || i == '_')) {
265 if (tmp && regtry(prog,s))
270 dontbother++,strend--;
273 tmp = (isalpha(i) || isdigit(i) || i == '_');
276 tmp = 0; /* assume not alphanumeric */
279 if (tmp != (isalpha(i) || isdigit(i) || i == '_'))
281 else if (regtry(prog, s))
285 if (!tmp && regtry(prog,s))
291 if (isalpha(i) || isdigit(i) || i == '_')
300 if (!isalpha(i) && !isdigit(i) && i != '_')
342 strend -= dontbother;
343 /* We don't know much -- general case. */
347 } while (s++ < strend);
354 if ((!safebase && (prog->nparens || sawampersand)) || prog->do_folding){
355 strend += dontbother; /* uncheat */
356 if (safebase) /* no need for $digit later */
358 else if (strbeg != prog->subbase) {
359 i = strend - string + (stringarg - strbeg);
360 s = nsavestr(strbeg,i); /* so $digit will work later */
362 Safefree(prog->subbase);
367 s += (stringarg - strbeg);
368 for (i = 0; i <= prog->nparens; i++) {
370 prog->startp[i] = s + (prog->startp[i] - string);
371 prog->endp[i] = s + (prog->endp[i] - string);
374 if (prog->do_folding)
380 if (prog->do_folding)
386 - regtry - try match at specific point
388 static int /* 0 failure, 1 success */
398 regstartp = prog->startp;
399 regendp = prog->endp;
400 reglastparen = &prog->lastparen;
406 for (i = NSUBEXP; i > 0; i--) {
411 if (regmatch(prog->program + 1) && reginput >= regtill) {
412 prog->startp[0] = string;
413 prog->endp[0] = reginput;
420 - regmatch - main matching routine
422 * Conceptually the strategy is simple: check to see whether the current
423 * node matches, call self recursively to see whether the rest matches,
424 * and then act accordingly. In practice we make some effort to avoid
425 * recursion, in particular by going through "ordinary" nodes (that don't
426 * need to know whether the rest of the match failed) by a loop instead of
429 /* [lwall] I've hoisted the register declarations to the outer block in order to
430 * maybe save a little bit of pushing and popping on the stack. It also takes
431 * advantage of machines that use a register save mask on subroutine entry.
433 static int /* 0 failure, 1 success */
437 register char *scan; /* Current node. */
438 char *next; /* Next node. */
439 register int nextchar;
440 register int n; /* no or next */
441 register int ln; /* len or last */
442 register char *s; /* operand or save */
443 register char *locinput = reginput;
445 nextchar = *locinput;
448 if (scan != NULL && regnarrate)
449 fprintf(stderr, "%s(\n", regprop(scan));
451 while (scan != NULL) {
454 fprintf(stderr, "%s...\n", regprop(scan));
458 next = scan + NEXT(scan);
462 next = regnext(scan);
467 if (locinput == regbol ||
468 ((nextchar || locinput < regeol) &&
469 locinput[-1] == '\n') )
476 if ((nextchar || locinput < regeol) && nextchar != '\n')
481 if ((nextchar == '\0' && locinput >= regeol) ||
484 nextchar = *++locinput;
489 /* Inline the first character, for speed. */
492 if (locinput + ln > regeol)
494 if (ln > 1 && bcmp(s, locinput, ln) != 0)
497 nextchar = *locinput;
503 nextchar = UCHARAT(locinput);
504 if (s[nextchar >> 3] & (1 << (nextchar&7)))
506 nextchar = *++locinput;
507 if (!nextchar && locinput > regeol)
513 if (!isalpha(nextchar) && !isdigit(nextchar) &&
516 nextchar = *++locinput;
519 if (!nextchar && locinput >= regeol)
521 if (isalpha(nextchar) || isdigit(nextchar) ||
524 nextchar = *++locinput;
528 if (locinput == regbol) /* was last char in word? */
531 ln = (isalpha(locinput[-1]) ||
532 isdigit(locinput[-1]) ||
533 locinput[-1] == '_' );
534 n = (isalpha(nextchar) || isdigit(nextchar) ||
535 nextchar == '_' ); /* is next char in word? */
536 if ((ln == n) == (OP(scan) == BOUND))
540 if (!nextchar && locinput >= regeol)
542 if (!isspace(nextchar))
544 nextchar = *++locinput;
549 if (isspace(nextchar))
551 nextchar = *++locinput;
554 if (!isdigit(nextchar))
556 nextchar = *++locinput;
559 if (!nextchar && locinput >= regeol)
561 if (isdigit(nextchar))
563 nextchar = *++locinput;
581 if (s == regmyendp[n])
583 /* Inline the first character, for speed. */
586 ln = regmyendp[n] - s;
587 if (locinput + ln > regeol)
589 if (ln > 1 && bcmp(s, locinput, ln) != 0)
592 nextchar = *locinput;
611 regmystartp[n] = locinput; /* for REF */
612 if (regmatch(next)) {
614 * Don't set startp if some later
615 * invocation of the same parentheses
618 if (regstartp[n] == NULL)
619 regstartp[n] = locinput;
633 n = OP(scan) - CLOSE;
636 regmyendp[n] = locinput; /* for REF */
637 if (regmatch(next)) {
639 * Don't set endp if some later
640 * invocation of the same parentheses
643 if (regendp[n] == NULL) {
644 regendp[n] = locinput;
645 if (n > *reglastparen)
654 if (OP(next) != BRANCH) /* No choice. */
655 next = NEXTOPER(scan); /* Avoid recursion. */
659 if (regmatch(NEXTOPER(scan)))
667 scan = regnext(scan);
669 } while (scan != NULL && OP(scan) == BRANCH);
678 * Lookahead to avoid useless match attempts
679 * when we know what character comes next.
681 if (OP(next) == EXACTLY)
682 nextchar = *(OPERAND(next)+1);
685 ln = (OP(scan) == STAR) ? 0 : 1;
687 n = regrepeat(NEXTOPER(scan));
689 /* If it could work, try it. */
690 if (nextchar == -1000 || *reginput == nextchar)
693 /* Couldn't or didn't -- back up. */
695 reginput = locinput + n;
699 reginput = locinput; /* put where regtry can find it */
700 return(1); /* Success! */
702 printf("%x %d\n",scan,scan[1]);
703 FAIL("regexp memory corruption");
710 * We get here only if there's trouble -- normally "case END" is
711 * the terminating point.
713 FAIL("corrupted regexp pointers");
721 - regrepeat - repeatedly match something simple, report how many
724 * [This routine now assumes that it will only match on things of length 1.
725 * That was true before, but now we assume scan - reginput is the count,
726 * rather than incrementing count on every character.]
735 register char *loceol = regeol;
741 while (scan < loceol && *scan != '\n')
744 case EXACTLY: /* length of string is 1 */
746 while (scan < loceol && *opnd == *scan)
752 while (scan < loceol && !(opnd[c >> 3] & (1 << (c & 7)))) {
758 while (isalpha(*scan) || isdigit(*scan) || *scan == '_')
762 while (scan < loceol && (!isalpha(*scan) && !isdigit(*scan) &&
767 while (scan < loceol && isspace(*scan))
771 while (scan < loceol && !isspace(*scan))
775 while (isdigit(*scan))
779 while (scan < loceol && !isdigit(*scan))
782 default: /* Oh dear. Called inappropriately. */
783 FAIL("internal regexp foulup");
794 - regnext - dig the "next" pointer out of a node
796 * [Note, when REGALIGN is defined there are two places in regmatch()
797 * that bypass this code for speed.]