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 /* $RCSfile: regexec.c,v $$Revision: 4.1 $$Date: 92/08/07 18:26:32 $
13 * Revision 4.1 92/08/07 18:26:32 lwall
15 * Revision 4.0.1.4 92/06/08 15:25:50 lwall
16 * patch20: pattern modifiers i and g didn't interact right
17 * patch20: in some cases $` and $' didn't get set by match
18 * patch20: /x{0}/ was wrongly interpreted as /x{0,}/
20 * Revision 4.0.1.3 91/11/05 18:23:55 lwall
21 * patch11: prepared for ctype implementations that don't define isascii()
22 * patch11: initial .* in pattern had dependency on value of $*
24 * Revision 4.0.1.2 91/06/07 11:50:33 lwall
25 * patch4: new copyright notice
26 * patch4: // wouldn't use previous pattern if it started with a null character
28 * Revision 4.0.1.1 91/04/12 09:07:39 lwall
29 * patch1: regexec only allocated space for 9 subexpresssions
31 * Revision 4.0 91/03/20 01:39:16 lwall
37 * regcomp and regexec -- regsub and regerror are not used in perl
39 * Copyright (c) 1986 by University of Toronto.
40 * Written by Henry Spencer. Not derived from licensed software.
42 * Permission is granted to anyone to use this software for any
43 * purpose on any computer system, and to redistribute it freely,
44 * subject to the following restrictions:
46 * 1. The author is not responsible for the consequences of use of
47 * this software, no matter how awful, even if they arise
50 * 2. The origin of this software must not be misrepresented, either
51 * by explicit claim or by omission.
53 * 3. Altered versions must be plainly marked as such, and must not
54 * be misrepresented as being the original software.
56 **** Alterations to Henry's code are...
58 **** Copyright (c) 1991, Larry Wall
60 **** You may distribute under the terms of either the GNU General Public
61 **** License or the Artistic License, as specified in the README file.
63 * Beware that some of this code is subtly aware of the way operator
64 * precedence is structured in regular expressions. Serious changes in
65 * regular-expression syntax might require a total rethink.
87 STATIC I32 regmatch();
88 STATIC I32 regrepeat();
91 - regexec - match a regexp against a string
94 regexec(prog, stringarg, strend, strbeg, minend, screamer, safebase)
95 register regexp *prog;
97 register char *strend; /* pointer to null at end of string */
98 char *strbeg; /* real beginning of string */
99 I32 minend; /* end of match must be at least minend after stringarg */
101 I32 safebase; /* no need to remember string in subbase */
106 register char *string = stringarg;
108 I32 minlen = 0; /* must match at least this many chars */
109 I32 dontbother = 0; /* how many characters not to try at end */
112 if (prog == NULL || string == NULL) {
113 croak("NULL regexp parameter");
117 if (string == strbeg) /* is ^ valid at stringarg? */
120 regprev = stringarg[-1];
121 if (!multiline && regprev == '\n')
122 regprev = '\0'; /* force ^ to NOT match */
124 regprecomp = prog->precomp;
125 /* Check validity of program. */
126 if (UCHARAT(prog->program) != MAGIC) {
127 FAIL("corrupted regexp program");
130 if (prog->do_folding) {
132 New(1101,c,i+1,char);
133 Copy(string, c, i+1, char);
136 for (s = string; s < strend; s++)
141 /* If there is a "must appear" string, look for it. */
143 if (prog->regmust != Nullsv &&
144 (!(prog->reganch & ROPT_ANCH)
145 || (multiline && prog->regback >= 0)) ) {
146 if (stringarg == strbeg && screamer) {
147 if (screamfirst[BmRARE(prog->regmust)] >= 0)
148 s = screaminstr(screamer,prog->regmust);
154 s = fbm_instr((unsigned char*)s, (unsigned char*)strend,
158 ++BmUSEFUL(prog->regmust); /* hooray */
159 goto phooey; /* not present */
161 else if (prog->regback >= 0) {
165 minlen = prog->regback + SvCUR(prog->regmust);
167 else if (--BmUSEFUL(prog->regmust) < 0) { /* boo */
168 SvREFCNT_dec(prog->regmust);
169 prog->regmust = Nullsv; /* disable regmust */
174 minlen = SvCUR(prog->regmust);
178 /* Mark beginning of line for ^ . */
181 /* Mark end of line for $ (and such) */
184 /* see how far we have to get to not match where we matched before */
185 regtill = string+minend;
187 /* Allocate our backreference arrays */
188 if ( regmyp_size < prog->nparens + 1 ) {
189 /* Allocate or enlarge the arrays */
190 regmyp_size = prog->nparens + 1;
191 if ( regmyp_size < 10 ) regmyp_size = 10; /* minimum */
193 /* reallocate larger */
194 Renew(regmystartp,regmyp_size,char*);
195 Renew(regmyendp, regmyp_size,char*);
198 /* Initial allocation */
199 New(1102,regmystartp,regmyp_size,char*);
200 New(1102,regmyendp, regmyp_size,char*);
205 /* Simplest case: anchored match need be tried only once. */
206 /* [unless multiline is set] */
207 if (prog->reganch & ROPT_ANCH) {
208 if (regtry(prog, string))
210 else if (multiline || (prog->reganch & ROPT_IMPLICIT)) {
212 dontbother = minlen - 1;
213 strend -= dontbother;
214 /* for multiline we only have to try after newlines */
219 if (s < strend && regtry(prog, s))
227 /* Messy cases: unanchored match. */
228 if (prog->regstart) {
229 if (prog->reganch & ROPT_SKIP) { /* we have /x+whatever/ */
230 /* it must be a one character string */
231 i = SvPVX(prog->regstart)[0];
237 while (s < strend && *s == i)
243 else if (SvPOK(prog->regstart) == 3) {
244 /* We know what string it must start with. */
246 while ((s = fbm_instr((unsigned char*)s,
247 (unsigned char*)strend, prog->regstart)) != NULL)
258 c = SvPVX(prog->regstart);
259 while ((s = ninstr(s, strend,
260 c, c + SvCUR(prog->regstart) )) != NULL) {
269 if (c = prog->regstclass) {
270 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
273 dontbother = minlen - 1;
274 strend -= dontbother; /* don't bother with what can't match */
276 /* We know what class it must start with. */
282 if (!(c[i >> 3] & (1 << (i&7)))) {
283 if (tmp && regtry(prog, s))
295 dontbother++,strend--;
301 tmp = isALNUM(regprev); /* assume not alphanumeric */
304 if (tmp != isALNUM(i)) {
311 if ((minlen || tmp) && regtry(prog,s))
316 dontbother++,strend--;
322 tmp = isALNUM(regprev); /* assume not alphanumeric */
325 if (tmp != isALNUM(i))
327 else if (regtry(prog, s))
331 if ((minlen || !tmp) && regtry(prog,s))
338 if (tmp && regtry(prog, s))
352 if (tmp && regtry(prog, s))
365 if (tmp && regtry(prog, s))
378 if (tmp && regtry(prog, s))
391 if (tmp && regtry(prog, s))
404 if (tmp && regtry(prog, s))
418 dontbother = minlen - 1;
419 strend -= dontbother;
420 /* We don't know much -- general case. */
424 } while (s++ < strend);
431 prog->subbeg = strbeg;
432 prog->subend = strend;
433 if ((!safebase && (prog->nparens || sawampersand)) || prog->do_folding){
434 strend += dontbother; /* uncheat */
435 i = strend - string + (stringarg - strbeg);
436 if (safebase) { /* no need for $digit later */
440 else if (strbeg != prog->subbase) {
441 s = nsavestr(strbeg,i); /* so $digit will work later */
443 Safefree(prog->subbase);
444 prog->subbeg = prog->subbase = s;
448 prog->subbeg = s = prog->subbase;
451 s += (stringarg - strbeg);
452 for (i = 0; i <= prog->nparens; i++) {
454 prog->startp[i] = s + (prog->startp[i] - string);
455 prog->endp[i] = s + (prog->endp[i] - string);
458 if (prog->do_folding)
464 if (prog->do_folding)
470 - regtry - try match at specific point
472 static I32 /* 0 failure, 1 success */
482 regstartp = prog->startp;
483 regendp = prog->endp;
484 reglastparen = &prog->lastparen;
490 for (i = prog->nparens; i >= 0; i--) {
495 if (regmatch(prog->program + 1) && reginput >= regtill) {
496 prog->startp[0] = string;
497 prog->endp[0] = reginput;
504 - regmatch - main matching routine
506 * Conceptually the strategy is simple: check to see whether the current
507 * node matches, call self recursively to see whether the rest matches,
508 * and then act accordingly. In practice we make some effort to avoid
509 * recursion, in particular by going through "ordinary" nodes (that don't
510 * need to know whether the rest of the match failed) by a loop instead of
513 /* [lwall] I've hoisted the register declarations to the outer block in order to
514 * maybe save a little bit of pushing and popping on the stack. It also takes
515 * advantage of machines that use a register save mask on subroutine entry.
517 static I32 /* 0 failure, 1 success */
521 register char *scan; /* Current node. */
522 char *next; /* Next node. */
523 register I32 nextchar;
524 register I32 n; /* no or next */
525 register I32 ln; /* len or last */
526 register char *s; /* operand or save */
527 register char *locinput = reginput;
529 nextchar = *locinput;
532 if (scan != NULL && regnarrate)
533 fprintf(stderr, "%s(\n", regprop(scan));
535 while (scan != NULL) {
538 fprintf(stderr, "%s...\n", regprop(scan));
542 next = scan + NEXT(scan);
546 next = regnext(scan);
551 if (locinput == regbol ? regprev == '\n' :
552 ((nextchar || locinput < regeol) &&
553 locinput[-1] == '\n') )
555 /* regtill = regbol; */
560 if ((nextchar || locinput < regeol) && nextchar != '\n')
562 if (!multiline && regeol - locinput > 1)
564 /* regtill = regbol; */
567 if ((nextchar == '\0' && locinput >= regeol) ||
570 nextchar = *++locinput;
575 /* Inline the first character, for speed. */
578 if (regeol - locinput < ln)
580 if (ln > 1 && bcmp(s, locinput, ln) != 0)
583 nextchar = *locinput;
588 nextchar = UCHARAT(locinput);
589 if (s[nextchar >> 3] & (1 << (nextchar&7)))
591 if (!nextchar && locinput >= regeol)
593 nextchar = *++locinput;
598 if (!isALNUM(nextchar))
600 nextchar = *++locinput;
603 if (!nextchar && locinput >= regeol)
605 if (isALNUM(nextchar))
607 nextchar = *++locinput;
611 if (locinput == regbol) /* was last char in word? */
612 ln = isALNUM(regprev);
614 ln = isALNUM(locinput[-1]);
615 n = isALNUM(nextchar); /* is next char in word? */
616 if ((ln == n) == (OP(scan) == BOUND))
620 if (!nextchar && locinput >= regeol)
622 if (!isSPACE(nextchar))
624 nextchar = *++locinput;
629 if (isSPACE(nextchar))
631 nextchar = *++locinput;
634 if (!isDIGIT(nextchar))
636 nextchar = *++locinput;
639 if (!nextchar && locinput >= regeol)
641 if (isDIGIT(nextchar))
643 nextchar = *++locinput;
646 n = ARG1(scan); /* which paren pair */
652 if (s == regmyendp[n])
654 /* Inline the first character, for speed. */
657 ln = regmyendp[n] - s;
658 if (locinput + ln > regeol)
660 if (ln > 1 && bcmp(s, locinput, ln) != 0)
663 nextchar = *locinput;
671 n = ARG1(scan); /* which paren pair */
674 regmystartp[n] = locinput; /* for REF */
675 if (regmatch(next)) {
677 * Don't set startp if some later
678 * invocation of the same parentheses
681 if (regstartp[n] == NULL)
682 regstartp[n] = locinput;
688 n = ARG1(scan); /* which paren pair */
691 regmyendp[n] = locinput; /* for REF */
692 if (regmatch(next)) {
694 * Don't set endp if some later
695 * invocation of the same parentheses
698 if (regendp[n] == NULL) {
699 regendp[n] = locinput;
700 if (n > *reglastparen)
709 if (OP(next) != BRANCH) /* No choice. */
710 next = NEXTOPER(scan); /* Avoid recursion. */
714 if (regmatch(NEXTOPER(scan)))
723 scan = regnext(scan);
725 } while (scan != NULL && OP(scan) == BRANCH);
733 ln = ARG1(scan); /* min to match */
734 n = -ARG2(scan); /* max to match */
735 scan = NEXTOPER(scan) + 4;
739 ln = ARG1(scan); /* min to match */
740 n = ARG2(scan); /* max to match */
741 scan = NEXTOPER(scan) + 4;
746 scan = NEXTOPER(scan);
750 * Lookahead to avoid useless match attempts
751 * when we know what character comes next.
755 scan = NEXTOPER(scan);
757 if (OP(next) == EXACTLY)
758 nextchar = *(OPERAND(next)+1);
765 /* If it could work, try it. */
766 if (nextchar == -1000 ||
767 *reginput == nextchar)
770 /* Couldn't or didn't -- back up. */
772 reginput = locinput + ln;
776 n = regrepeat(scan, n);
777 if (!multiline && OP(next) == EOL && ln < n)
778 ln = n; /* why back off? */
780 /* If it could work, try it. */
781 if (nextchar == -1000 ||
782 *reginput == nextchar)
785 /* Couldn't or didn't -- back up. */
787 reginput = locinput + n;
792 reginput = locinput; /* put where regtry can find it */
793 return(1); /* Success! */
795 printf("%x %d\n",scan,scan[1]);
796 FAIL("regexp memory corruption");
803 * We get here only if there's trouble -- normally "case END" is
804 * the terminating point.
806 FAIL("corrupted regexp pointers");
814 - regrepeat - repeatedly match something simple, report how many
817 * [This routine now assumes that it will only match on things of length 1.
818 * That was true before, but now we assume scan - reginput is the count,
819 * rather than incrementing count on every character.]
829 register char *loceol = regeol;
832 if (max != 32767 && max < loceol - scan)
837 while (scan < loceol && *scan != '\n')
840 case EXACTLY: /* length of string is 1 */
842 while (scan < loceol && *opnd == *scan)
847 while (scan < loceol && !(opnd[c >> 3] & (1 << (c & 7)))) {
853 while (scan < loceol && isALNUM(*scan))
857 while (scan < loceol && !isALNUM(*scan))
861 while (scan < loceol && isSPACE(*scan))
865 while (scan < loceol && !isSPACE(*scan))
869 while (scan < loceol && isDIGIT(*scan))
873 while (scan < loceol && !isDIGIT(*scan))
876 default: /* Oh dear. Called inappropriately. */
877 FAIL("internal regexp foulup");
888 - regnext - dig the "next" pointer out of a node
890 * [Note, when REGALIGN is defined there are two places in regmatch()
891 * that bypass this code for speed.]