[inseparable changes from patch from perl-5.003_97d to perl-5.003_97e]
[p5sagit/p5-mst-13.2.git] / regexec.c
1 /*    regexec.c
2  */
3
4 /*
5  * "One Ring to rule them all, One Ring to find them..."
6  */
7
8 /* NOTE: this is derived from Henry Spencer's regexp code, and should not
9  * confused with the original package (see point 3 below).  Thanks, Henry!
10  */
11
12 /* Additional note: this code is very heavily munged from Henry's version
13  * in places.  In some spots I've traded clarity for efficiency, so don't
14  * blame Henry for some of the lack of readability.
15  */
16
17 /* The names of the functions have been changed from regcomp and
18  * regexec to  pregcomp and pregexec in order to avoid conflicts
19  * with the POSIX routines of the same names.
20 */
21
22 /*SUPPRESS 112*/
23 /*
24  * pregcomp and pregexec -- regsub and regerror are not used in perl
25  *
26  *      Copyright (c) 1986 by University of Toronto.
27  *      Written by Henry Spencer.  Not derived from licensed software.
28  *
29  *      Permission is granted to anyone to use this software for any
30  *      purpose on any computer system, and to redistribute it freely,
31  *      subject to the following restrictions:
32  *
33  *      1. The author is not responsible for the consequences of use of
34  *              this software, no matter how awful, even if they arise
35  *              from defects in it.
36  *
37  *      2. The origin of this software must not be misrepresented, either
38  *              by explicit claim or by omission.
39  *
40  *      3. Altered versions must be plainly marked as such, and must not
41  *              be misrepresented as being the original software.
42  *
43  ****    Alterations to Henry's code are...
44  ****
45  ****    Copyright (c) 1991-1997, Larry Wall
46  ****
47  ****    You may distribute under the terms of either the GNU General Public
48  ****    License or the Artistic License, as specified in the README file.
49  *
50  * Beware that some of this code is subtly aware of the way operator
51  * precedence is structured in regular expressions.  Serious changes in
52  * regular-expression syntax might require a total rethink.
53  */
54 #include "EXTERN.h"
55 #include "perl.h"
56 #include "regcomp.h"
57
58 #ifndef STATIC
59 #define STATIC  static
60 #endif
61
62 #ifdef DEBUGGING
63 static I32 regnarrate = 0;
64 static char* regprogram = 0;
65 #endif
66
67 /* Current curly descriptor */
68 typedef struct curcur CURCUR;
69 struct curcur {
70     int         parenfloor;     /* how far back to strip paren data */
71     int         cur;            /* how many instances of scan we've matched */
72     int         min;            /* the minimal number of scans to match */
73     int         max;            /* the maximal number of scans to match */
74     int         minmod;         /* whether to work our way up or down */
75     char *      scan;           /* the thing to match */
76     char *      next;           /* what has to match after it */
77     char *      lastloc;        /* where we started matching this scan */
78     CURCUR *    oldcc;          /* current curly before we started this one */
79 };
80
81 static CURCUR* regcc;
82
83 typedef I32 CHECKPOINT;
84
85 static CHECKPOINT regcppush _((I32 parenfloor));
86 static char * regcppop _((void));
87
88 static CHECKPOINT
89 regcppush(parenfloor)
90 I32 parenfloor;
91 {
92     int retval = savestack_ix;
93     int i = (regsize - parenfloor) * 3;
94     int p;
95
96     SSCHECK(i + 5);
97     for (p = regsize; p > parenfloor; p--) {
98         SSPUSHPTR(regendp[p]);
99         SSPUSHPTR(regstartp[p]);
100         SSPUSHINT(p);
101     }
102     SSPUSHINT(regsize);
103     SSPUSHINT(*reglastparen);
104     SSPUSHPTR(reginput);
105     SSPUSHINT(i + 3);
106     SSPUSHINT(SAVEt_REGCONTEXT);
107     return retval;
108 }
109
110 static char *
111 regcppop()
112 {
113     I32 i = SSPOPINT;
114     U32 paren = 0;
115     char *input;
116     char *tmps;
117     assert(i == SAVEt_REGCONTEXT);
118     i = SSPOPINT;
119     input = (char *) SSPOPPTR;
120     *reglastparen = SSPOPINT;
121     regsize = SSPOPINT;
122     for (i -= 3; i > 0; i -= 3) {
123         paren = (U32)SSPOPINT;
124         regstartp[paren] = (char *) SSPOPPTR;
125         tmps = (char*)SSPOPPTR;
126         if (paren <= *reglastparen)
127             regendp[paren] = tmps;
128     }
129     for (paren = *reglastparen + 1; paren <= regnpar; paren++) {
130         if (paren > regsize)
131             regstartp[paren] = Nullch;
132         regendp[paren] = Nullch;
133     }
134     return input;
135 }
136
137 #define regcpblow(cp) leave_scope(cp)
138
139 /*
140  * pregexec and friends
141  */
142
143 /*
144  * Forwards.
145  */
146
147 static I32 regmatch _((char *prog));
148 static I32 regrepeat _((char *p, I32 max));
149 static I32 regtry _((regexp *prog, char *startpos));
150 static bool reginclass _((char *p, I32 c));
151
152 static bool regtainted;         /* tainted information used? */
153
154 /*
155  - pregexec - match a regexp against a string
156  */
157 I32
158 pregexec(prog, stringarg, strend, strbeg, minend, screamer, safebase)
159 register regexp *prog;
160 char *stringarg;
161 register char *strend;  /* pointer to null at end of string */
162 char *strbeg;   /* real beginning of string */
163 I32 minend;     /* end of match must be at least minend after stringarg */
164 SV *screamer;
165 I32 safebase;   /* no need to remember string in subbase */
166 {
167     register char *s;
168     register char *c;
169     register char *startpos = stringarg;
170     register I32 tmp;
171     I32 minlen = 0;             /* must match at least this many chars */
172     I32 dontbother = 0; /* how many characters not to try at end */
173     CURCUR cc;
174
175     cc.cur = 0;
176     cc.oldcc = 0;
177     regcc = &cc;
178
179 #ifdef DEBUGGING
180     regnarrate = debug & 512;
181     regprogram = prog->program;
182 #endif
183
184     /* Be paranoid... */
185     if (prog == NULL || startpos == NULL) {
186         croak("NULL regexp parameter");
187         return 0;
188     }
189
190     if (startpos == strbeg)     /* is ^ valid at stringarg? */
191         regprev = '\n';
192     else {
193         regprev = stringarg[-1];
194         if (!multiline && regprev == '\n')
195             regprev = '\0';             /* force ^ to NOT match */
196     }
197
198     regprecomp = prog->precomp;
199     /* Check validity of program. */
200     if (UCHARAT(prog->program) != MAGIC) {
201         FAIL("corrupted regexp program");
202     }
203
204     regnpar = prog->nparens;
205     regtainted = FALSE;
206
207     /* If there is a "must appear" string, look for it. */
208     s = startpos;
209     if (prog->regmust != Nullsv &&
210         !(prog->reganch & ROPT_ANCH_GPOS) &&
211         (!(prog->reganch & ROPT_ANCH_BOL)
212          || (multiline && prog->regback >= 0)) )
213     {
214         if (stringarg == strbeg && screamer) {
215             if (screamfirst[BmRARE(prog->regmust)] >= 0)
216                     s = screaminstr(screamer,prog->regmust);
217             else
218                     s = Nullch;
219         }
220         else
221             s = fbm_instr((unsigned char*)s, (unsigned char*)strend,
222                 prog->regmust);
223         if (!s) {
224             ++BmUSEFUL(prog->regmust);  /* hooray */
225             goto phooey;        /* not present */
226         }
227         else if (prog->regback >= 0) {
228             s -= prog->regback;
229             if (s < startpos)
230                 s = startpos;
231             minlen = prog->regback + SvCUR(prog->regmust);
232         }
233         else if (!prog->naughty && --BmUSEFUL(prog->regmust) < 0) { /* boo */
234             SvREFCNT_dec(prog->regmust);
235             prog->regmust = Nullsv;     /* disable regmust */
236             s = startpos;
237         }
238         else {
239             s = startpos;
240             minlen = SvCUR(prog->regmust);
241         }
242     }
243
244     /* Mark beginning of line for ^ . */
245     regbol = startpos;
246
247     /* Mark end of line for $ (and such) */
248     regeol = strend;
249
250     /* see how far we have to get to not match where we matched before */
251     regtill = startpos+minend;
252
253     /* Simplest case:  anchored match need be tried only once. */
254     /*  [unless only anchor is BOL and multiline is set] */
255     if (prog->reganch & ROPT_ANCH) {
256         if (regtry(prog, startpos))
257             goto got_it;
258         else if (!(prog->reganch & ROPT_ANCH_GPOS) &&
259                  (multiline || (prog->reganch & ROPT_IMPLICIT)))
260         {
261             if (minlen)
262                 dontbother = minlen - 1;
263             strend -= dontbother;
264             /* for multiline we only have to try after newlines */
265             if (s > startpos)
266                 s--;
267             while (s < strend) {
268                 if (*s++ == '\n') {
269                     if (s < strend && regtry(prog, s))
270                         goto got_it;
271                 }
272             }
273         }
274         goto phooey;
275     }
276
277     /* Messy cases:  unanchored match. */
278     if (prog->regstart) {
279         if (prog->reganch & ROPT_SKIP) {  /* we have /x+whatever/ */
280             /* it must be a one character string */
281             char ch = SvPVX(prog->regstart)[0];
282             while (s < strend) {
283                 if (*s == ch) {
284                     if (regtry(prog, s))
285                         goto got_it;
286                     s++;
287                     while (s < strend && *s == ch)
288                         s++;
289                 }
290                 s++;
291             }
292         }
293         else if (SvTYPE(prog->regstart) == SVt_PVBM) {
294             /* We know what string it must start with. */
295             while ((s = fbm_instr((unsigned char*)s,
296               (unsigned char*)strend, prog->regstart)) != NULL)
297             {
298                 if (regtry(prog, s))
299                     goto got_it;
300                 s++;
301             }
302         }
303         else {                          /* Optimized fbm_instr: */
304             c = SvPVX(prog->regstart);
305             while ((s = ninstr(s, strend, c, c + SvCUR(prog->regstart))) != NULL)
306             {
307                 if (regtry(prog, s))
308                     goto got_it;
309                 s++;
310             }
311         }
312         goto phooey;
313     }
314     /*SUPPRESS 560*/
315     if (c = prog->regstclass) {
316         I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
317
318         if (minlen)
319             dontbother = minlen - 1;
320         strend -= dontbother;   /* don't bother with what can't match */
321         tmp = 1;
322         /* We know what class it must start with. */
323         switch (OP(c)) {
324         case ANYOF:
325             c = OPERAND(c);
326             while (s < strend) {
327                 if (reginclass(c, *s)) {
328                     if (tmp && regtry(prog, s))
329                         goto got_it;
330                     else
331                         tmp = doevery;
332                 }
333                 else
334                     tmp = 1;
335                 s++;
336             }
337             break;
338         case BOUNDL:
339             regtainted = TRUE;
340             /* FALL THROUGH */
341         case BOUND:
342             if (minlen)
343                 dontbother++,strend--;
344             tmp = (s != startpos) ? UCHARAT(s - 1) : regprev;
345             tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
346             while (s < strend) {
347                 if (tmp == !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
348                     tmp = !tmp;
349                     if (regtry(prog, s))
350                         goto got_it;
351                 }
352                 s++;
353             }
354             if ((minlen || tmp) && regtry(prog,s))
355                 goto got_it;
356             break;
357         case NBOUNDL:
358             regtainted = TRUE;
359             /* FALL THROUGH */
360         case NBOUND:
361             if (minlen)
362                 dontbother++,strend--;
363             tmp = (s != startpos) ? UCHARAT(s - 1) : regprev;
364             tmp = ((OP(c) == NBOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
365             while (s < strend) {
366                 if (tmp == !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
367                     tmp = !tmp;
368                 else if (regtry(prog, s))
369                     goto got_it;
370                 s++;
371             }
372             if ((minlen || !tmp) && regtry(prog,s))
373                 goto got_it;
374             break;
375         case ALNUM:
376             while (s < strend) {
377                 if (isALNUM(*s)) {
378                     if (tmp && regtry(prog, s))
379                         goto got_it;
380                     else
381                         tmp = doevery;
382                 }
383                 else
384                     tmp = 1;
385                 s++;
386             }
387             break;
388         case ALNUML:
389             regtainted = TRUE;
390             while (s < strend) {
391                 if (isALNUM_LC(*s)) {
392                     if (tmp && regtry(prog, s))
393                         goto got_it;
394                     else
395                         tmp = doevery;
396                 }
397                 else
398                     tmp = 1;
399                 s++;
400             }
401             break;
402         case NALNUM:
403             while (s < strend) {
404                 if (!isALNUM(*s)) {
405                     if (tmp && regtry(prog, s))
406                         goto got_it;
407                     else
408                         tmp = doevery;
409                 }
410                 else
411                     tmp = 1;
412                 s++;
413             }
414             break;
415         case NALNUML:
416             regtainted = TRUE;
417             while (s < strend) {
418                 if (!isALNUM_LC(*s)) {
419                     if (tmp && regtry(prog, s))
420                         goto got_it;
421                     else
422                         tmp = doevery;
423                 }
424                 else
425                     tmp = 1;
426                 s++;
427             }
428             break;
429         case SPACE:
430             while (s < strend) {
431                 if (isSPACE(*s)) {
432                     if (tmp && regtry(prog, s))
433                         goto got_it;
434                     else
435                         tmp = doevery;
436                 }
437                 else
438                     tmp = 1;
439                 s++;
440             }
441             break;
442         case SPACEL:
443             regtainted = TRUE;
444             while (s < strend) {
445                 if (isSPACE_LC(*s)) {
446                     if (tmp && regtry(prog, s))
447                         goto got_it;
448                     else
449                         tmp = doevery;
450                 }
451                 else
452                     tmp = 1;
453                 s++;
454             }
455             break;
456         case NSPACE:
457             while (s < strend) {
458                 if (!isSPACE(*s)) {
459                     if (tmp && regtry(prog, s))
460                         goto got_it;
461                     else
462                         tmp = doevery;
463                 }
464                 else
465                     tmp = 1;
466                 s++;
467             }
468             break;
469         case NSPACEL:
470             regtainted = TRUE;
471             while (s < strend) {
472                 if (!isSPACE_LC(*s)) {
473                     if (tmp && regtry(prog, s))
474                         goto got_it;
475                     else
476                         tmp = doevery;
477                 }
478                 else
479                     tmp = 1;
480                 s++;
481             }
482             break;
483         case DIGIT:
484             while (s < strend) {
485                 if (isDIGIT(*s)) {
486                     if (tmp && regtry(prog, s))
487                         goto got_it;
488                     else
489                         tmp = doevery;
490                 }
491                 else
492                     tmp = 1;
493                 s++;
494             }
495             break;
496         case NDIGIT:
497             while (s < strend) {
498                 if (!isDIGIT(*s)) {
499                     if (tmp && regtry(prog, s))
500                         goto got_it;
501                     else
502                         tmp = doevery;
503                 }
504                 else
505                     tmp = 1;
506                 s++;
507             }
508             break;
509         }
510     }
511     else {
512         if (minlen)
513             dontbother = minlen - 1;
514         strend -= dontbother;
515         /* We don't know much -- general case. */
516         do {
517             if (regtry(prog, s))
518                 goto got_it;
519         } while (s++ < strend);
520     }
521
522     /* Failure. */
523     goto phooey;
524
525 got_it:
526     strend += dontbother;       /* uncheat */
527     prog->subbeg = strbeg;
528     prog->subend = strend;
529     prog->exec_tainted = regtainted;
530
531     /* make sure $`, $&, $', and $digit will work later */
532     if (strbeg != prog->subbase) {
533         if (safebase) {
534             if (prog->subbase) {
535                 Safefree(prog->subbase);
536                 prog->subbase = Nullch;
537             }
538         }
539         else {
540             I32 i = strend - startpos + (stringarg - strbeg);
541             s = savepvn(strbeg, i);
542             Safefree(prog->subbase);
543             prog->subbase = s;
544             prog->subbeg = prog->subbase;
545             prog->subend = prog->subbase + i;
546             s = prog->subbase + (stringarg - strbeg);
547             for (i = 0; i <= prog->nparens; i++) {
548                 if (prog->endp[i]) {
549                     prog->startp[i] = s + (prog->startp[i] - startpos);
550                     prog->endp[i] = s + (prog->endp[i] - startpos);
551                 }
552             }
553         }
554     }
555     return 1;
556
557 phooey:
558     return 0;
559 }
560
561 /*
562  - regtry - try match at specific point
563  */
564 static I32                      /* 0 failure, 1 success */
565 regtry(prog, startpos)
566 regexp *prog;
567 char *startpos;
568 {
569     register I32 i;
570     register char **sp;
571     register char **ep;
572
573     reginput = startpos;
574     regstartp = prog->startp;
575     regendp = prog->endp;
576     reglastparen = &prog->lastparen;
577     prog->lastparen = 0;
578     regsize = 0;
579
580     sp = prog->startp;
581     ep = prog->endp;
582     if (prog->nparens) {
583         for (i = prog->nparens; i >= 0; i--) {
584             *sp++ = NULL;
585             *ep++ = NULL;
586         }
587     }
588     if (regmatch(prog->program + 1) && reginput >= regtill) {
589         prog->startp[0] = startpos;
590         prog->endp[0] = reginput;
591         return 1;
592     }
593     else
594         return 0;
595 }
596
597 /*
598  - regmatch - main matching routine
599  *
600  * Conceptually the strategy is simple:  check to see whether the current
601  * node matches, call self recursively to see whether the rest matches,
602  * and then act accordingly.  In practice we make some effort to avoid
603  * recursion, in particular by going through "ordinary" nodes (that don't
604  * need to know whether the rest of the match failed) by a loop instead of
605  * by recursion.
606  */
607 /* [lwall] I've hoisted the register declarations to the outer block in order to
608  * maybe save a little bit of pushing and popping on the stack.  It also takes
609  * advantage of machines that use a register save mask on subroutine entry.
610  */
611 static I32                      /* 0 failure, 1 success */
612 regmatch(prog)
613 char *prog;
614 {
615     register char *scan;        /* Current node. */
616     char *next;                 /* Next node. */
617     register I32 nextchar;
618     register I32 n;             /* no or next */
619     register I32 ln;            /* len or last */
620     register char *s;           /* operand or save */
621     register char *locinput = reginput;
622     register I32 c1, c2;        /* case fold search */
623     int minmod = 0;
624 #ifdef DEBUGGING
625     static int regindent = 0;
626     regindent++;
627 #endif
628
629     nextchar = UCHARAT(locinput);
630     scan = prog;
631     while (scan != NULL) {
632 #ifdef DEBUGGING
633 #define sayYES goto yes
634 #define sayNO goto no
635 #define saySAME(x) if (x) goto yes; else goto no
636         if (regnarrate) {
637             PerlIO_printf(Perl_debug_log, "%*s%2d%-8.8s\t<%.10s>\n", regindent*2, "",
638                 scan - regprogram, regprop(scan), locinput);
639         }
640 #else
641 #define sayYES return 1
642 #define sayNO return 0
643 #define saySAME(x) return x
644 #endif
645
646 #ifdef REGALIGN
647         next = scan + NEXT(scan);
648         if (next == scan)
649             next = NULL;
650 #else
651         next = regnext(scan);
652 #endif
653
654         switch (OP(scan)) {
655         case BOL:
656             if (locinput == regbol
657                 ? regprev == '\n'
658                 : ((nextchar || locinput < regeol) && locinput[-1] == '\n') )
659             {
660                 /* regtill = regbol; */
661                 break;
662             }
663             sayNO;
664         case MBOL:
665             if (locinput == regbol
666                 ? regprev == '\n'
667                 : ((nextchar || locinput < regeol) && locinput[-1] == '\n') )
668             {
669                 break;
670             }
671             sayNO;
672         case SBOL:
673             if (locinput == regbol && regprev == '\n')
674                 break;
675             sayNO;
676         case GPOS:
677             if (locinput == regbol)
678                 break;
679             sayNO;
680         case EOL:
681             if (multiline)
682                 goto meol;
683             else
684                 goto seol;
685         case MEOL:
686           meol:
687             if ((nextchar || locinput < regeol) && nextchar != '\n')
688                 sayNO;
689             break;
690         case SEOL:
691           seol:
692             if ((nextchar || locinput < regeol) && nextchar != '\n')
693                 sayNO;
694             if (regeol - locinput > 1)
695                 sayNO;
696             break;
697         case SANY:
698             if (!nextchar && locinput >= regeol)
699                 sayNO;
700             nextchar = UCHARAT(++locinput);
701             break;
702         case ANY:
703             if (!nextchar && locinput >= regeol || nextchar == '\n')
704                 sayNO;
705             nextchar = UCHARAT(++locinput);
706             break;
707         case EXACT:
708             s = OPERAND(scan);
709             ln = *s++;
710             /* Inline the first character, for speed. */
711             if (UCHARAT(s) != nextchar)
712                 sayNO;
713             if (regeol - locinput < ln)
714                 sayNO;
715             if (ln > 1 && memNE(s, locinput, ln))
716                 sayNO;
717             locinput += ln;
718             nextchar = UCHARAT(locinput);
719             break;
720         case EXACTFL:
721             regtainted = TRUE;
722             /* FALL THROUGH */
723         case EXACTF:
724             s = OPERAND(scan);
725             ln = *s++;
726             /* Inline the first character, for speed. */
727             if (UCHARAT(s) != nextchar &&
728                 UCHARAT(s) != ((OP(scan) == EXACTF)
729                                ? fold : fold_locale)[nextchar])
730                 sayNO;
731             if (regeol - locinput < ln)
732                 sayNO;
733             if (ln > 1 && (OP(scan) == EXACTF
734                            ? ibcmp(s, locinput, ln)
735                            : ibcmp_locale(s, locinput, ln)))
736                 sayNO;
737             locinput += ln;
738             nextchar = UCHARAT(locinput);
739             break;
740         case ANYOF:
741             s = OPERAND(scan);
742             if (nextchar < 0)
743                 nextchar = UCHARAT(locinput);
744             if (!reginclass(s, nextchar))
745                 sayNO;
746             if (!nextchar && locinput >= regeol)
747                 sayNO;
748             nextchar = UCHARAT(++locinput);
749             break;
750         case ALNUML:
751             regtainted = TRUE;
752             /* FALL THROUGH */
753         case ALNUM:
754             if (!nextchar)
755                 sayNO;
756             if (!(OP(scan) == ALNUM
757                   ? isALNUM(nextchar) : isALNUM_LC(nextchar)))
758                 sayNO;
759             nextchar = UCHARAT(++locinput);
760             break;
761         case NALNUML:
762             regtainted = TRUE;
763             /* FALL THROUGH */
764         case NALNUM:
765             if (!nextchar && locinput >= regeol)
766                 sayNO;
767             if (OP(scan) == NALNUM
768                 ? isALNUM(nextchar) : isALNUM_LC(nextchar))
769                 sayNO;
770             nextchar = UCHARAT(++locinput);
771             break;
772         case BOUNDL:
773         case NBOUNDL:
774             regtainted = TRUE;
775             /* FALL THROUGH */
776         case BOUND:
777         case NBOUND:
778             /* was last char in word? */
779             ln = (locinput != regbol) ? UCHARAT(locinput - 1) : regprev;
780             if (OP(scan) == BOUND || OP(scan) == NBOUND) {
781                 ln = isALNUM(ln);
782                 n = isALNUM(nextchar);
783             }
784             else {
785                 ln = isALNUM_LC(ln);
786                 n = isALNUM_LC(nextchar);
787             }
788             if (((!ln) == (!n)) == (OP(scan) == BOUND || OP(scan) == BOUNDL))
789                 sayNO;
790             break;
791         case SPACEL:
792             regtainted = TRUE;
793             /* FALL THROUGH */
794         case SPACE:
795             if (!nextchar && locinput >= regeol)
796                 sayNO;
797             if (!(OP(scan) == SPACE
798                   ? isSPACE(nextchar) : isSPACE_LC(nextchar)))
799                 sayNO;
800             nextchar = UCHARAT(++locinput);
801             break;
802         case NSPACEL:
803             regtainted = TRUE;
804             /* FALL THROUGH */
805         case NSPACE:
806             if (!nextchar)
807                 sayNO;
808             if (OP(scan) == SPACE
809                 ? isSPACE(nextchar) : isSPACE_LC(nextchar))
810                 sayNO;
811             nextchar = UCHARAT(++locinput);
812             break;
813         case DIGIT:
814             if (!isDIGIT(nextchar))
815                 sayNO;
816             nextchar = UCHARAT(++locinput);
817             break;
818         case NDIGIT:
819             if (!nextchar && locinput >= regeol)
820                 sayNO;
821             if (isDIGIT(nextchar))
822                 sayNO;
823             nextchar = UCHARAT(++locinput);
824             break;
825         case REF:
826             n = ARG1(scan);  /* which paren pair */
827             s = regstartp[n];
828             if (!s)
829                 sayNO;
830             if (!regendp[n])
831                 sayNO;
832             if (s == regendp[n])
833                 break;
834             /* Inline the first character, for speed. */
835             if (UCHARAT(s) != nextchar)
836                 sayNO;
837             ln = regendp[n] - s;
838             if (locinput + ln > regeol)
839                 sayNO;
840             if (ln > 1 && memNE(s, locinput, ln))
841                 sayNO;
842             locinput += ln;
843             nextchar = UCHARAT(locinput);
844             break;
845
846         case NOTHING:
847             break;
848         case BACK:
849             break;
850         case OPEN:
851             n = ARG1(scan);  /* which paren pair */
852             regstartp[n] = locinput;
853             if (n > regsize)
854                 regsize = n;
855             break;
856         case CLOSE:
857             n = ARG1(scan);  /* which paren pair */
858             regendp[n] = locinput;
859             if (n > *reglastparen)
860                 *reglastparen = n;
861             break;
862         case CURLYX: {
863                 CURCUR cc;
864                 CHECKPOINT cp = savestack_ix;
865                 cc.oldcc = regcc;
866                 regcc = &cc;
867                 cc.parenfloor = *reglastparen;
868                 cc.cur = -1;
869                 cc.min = ARG1(scan);
870                 cc.max  = ARG2(scan);
871                 cc.scan = NEXTOPER(scan) + 4;
872                 cc.next = next;
873                 cc.minmod = minmod;
874                 cc.lastloc = 0;
875                 reginput = locinput;
876                 n = regmatch(PREVOPER(next));   /* start on the WHILEM */
877                 regcpblow(cp);
878                 regcc = cc.oldcc;
879                 saySAME(n);
880             }
881             /* NOT REACHED */
882         case WHILEM: {
883                 /*
884                  * This is really hard to understand, because after we match
885                  * what we're trying to match, we must make sure the rest of
886                  * the RE is going to match for sure, and to do that we have
887                  * to go back UP the parse tree by recursing ever deeper.  And
888                  * if it fails, we have to reset our parent's current state
889                  * that we can try again after backing off.
890                  */
891
892                 CHECKPOINT cp;
893                 CURCUR* cc = regcc;
894                 n = cc->cur + 1;        /* how many we know we matched */
895                 reginput = locinput;
896
897 #ifdef DEBUGGING
898                 if (regnarrate)
899                     PerlIO_printf(Perl_debug_log, "%*s  %ld  %lx\n", regindent*2, "",
900                         (long)n, (long)cc);
901 #endif
902
903                 /* If degenerate scan matches "", assume scan done. */
904
905                 if (locinput == cc->lastloc && n >= cc->min) {
906                     regcc = cc->oldcc;
907                     ln = regcc->cur;
908                     if (regmatch(cc->next))
909                         sayYES;
910                     regcc->cur = ln;
911                     regcc = cc;
912                     sayNO;
913                 }
914
915                 /* First just match a string of min scans. */
916
917                 if (n < cc->min) {
918                     cc->cur = n;
919                     cc->lastloc = locinput;
920                     if (regmatch(cc->scan))
921                         sayYES;
922                     cc->cur = n - 1;
923                     sayNO;
924                 }
925
926                 /* Prefer next over scan for minimal matching. */
927
928                 if (cc->minmod) {
929                     regcc = cc->oldcc;
930                     ln = regcc->cur;
931                     cp = regcppush(cc->parenfloor);
932                     if (regmatch(cc->next)) {
933                         regcpblow(cp);
934                         sayYES; /* All done. */
935                     }
936                     regcppop();
937                     regcc->cur = ln;
938                     regcc = cc;
939
940                     if (n >= cc->max)   /* Maximum greed exceeded? */
941                         sayNO;
942
943                     /* Try scanning more and see if it helps. */
944                     reginput = locinput;
945                     cc->cur = n;
946                     cc->lastloc = locinput;
947                     cp = regcppush(cc->parenfloor);
948                     if (regmatch(cc->scan)) {
949                         regcpblow(cp);
950                         sayYES;
951                     }
952                     regcppop();
953                     cc->cur = n - 1;
954                     sayNO;
955                 }
956
957                 /* Prefer scan over next for maximal matching. */
958
959                 if (n < cc->max) {      /* More greed allowed? */
960                     cp = regcppush(cc->parenfloor);
961                     cc->cur = n;
962                     cc->lastloc = locinput;
963                     if (regmatch(cc->scan)) {
964                         regcpblow(cp);
965                         sayYES;
966                     }
967                     regcppop();         /* Restore some previous $<digit>s? */
968                     reginput = locinput;
969                 }
970
971                 /* Failed deeper matches of scan, so see if this one works. */
972                 regcc = cc->oldcc;
973                 ln = regcc->cur;
974                 if (regmatch(cc->next))
975                     sayYES;
976                 regcc->cur = ln;
977                 regcc = cc;
978                 cc->cur = n - 1;
979                 sayNO;
980             }
981             /* NOT REACHED */
982         case BRANCH: {
983                 if (OP(next) != BRANCH)   /* No choice. */
984                     next = NEXTOPER(scan);/* Avoid recursion. */
985                 else {
986                     int lastparen = *reglastparen;
987                     do {
988                         reginput = locinput;
989                         if (regmatch(NEXTOPER(scan)))
990                             sayYES;
991                         for (n = *reglastparen; n > lastparen; n--)
992                             regendp[n] = 0;
993                         *reglastparen = n;
994                             
995 #ifdef REGALIGN
996                         /*SUPPRESS 560*/
997                         if (n = NEXT(scan))
998                             scan += n;
999                         else
1000                             scan = NULL;
1001 #else
1002                         scan = regnext(scan);
1003 #endif
1004                     } while (scan != NULL && OP(scan) == BRANCH);
1005                     sayNO;
1006                     /* NOTREACHED */
1007                 }
1008             }
1009             break;
1010         case MINMOD:
1011             minmod = 1;
1012             break;
1013         case CURLY:
1014             ln = ARG1(scan);  /* min to match */
1015             n  = ARG2(scan);  /* max to match */
1016             scan = NEXTOPER(scan) + 4;
1017             goto repeat;
1018         case STAR:
1019             ln = 0;
1020             n = 32767;
1021             scan = NEXTOPER(scan);
1022             goto repeat;
1023         case PLUS:
1024             /*
1025             * Lookahead to avoid useless match attempts
1026             * when we know what character comes next.
1027             */
1028             ln = 1;
1029             n = 32767;
1030             scan = NEXTOPER(scan);
1031           repeat:
1032             if (regkind[(U8)OP(next)] == EXACT) {
1033                 c1 = UCHARAT(OPERAND(next) + 1);
1034                 if (OP(next) == EXACTF)
1035                     c2 = fold[c1];
1036                 else if (OP(next) == EXACTFL)
1037                     c2 = fold_locale[c1];
1038                 else
1039                     c2 = c1;
1040             }
1041             else
1042                 c1 = c2 = -1000;
1043             reginput = locinput;
1044             if (minmod) {
1045                 minmod = 0;
1046                 if (ln && regrepeat(scan, ln) < ln)
1047                     sayNO;
1048                 while (n >= ln || (n == 32767 && ln > 0)) { /* ln overflow ? */
1049                     /* If it could work, try it. */
1050                     if (c1 == -1000 ||
1051                         UCHARAT(reginput) == c1 ||
1052                         UCHARAT(reginput) == c2)
1053                     {
1054                         if (regmatch(next))
1055                             sayYES;
1056                     }
1057                     /* Couldn't or didn't -- back up. */
1058                     reginput = locinput + ln;
1059                     if (regrepeat(scan, 1)) {
1060                         ln++;
1061                         reginput = locinput + ln;
1062                     }
1063                     else
1064                         sayNO;
1065                 }
1066             }
1067             else {
1068                 n = regrepeat(scan, n);
1069                 if (ln < n && regkind[(U8)OP(next)] == EOL &&
1070                     (!multiline || OP(next) == SEOL))
1071                     ln = n;                     /* why back off? */
1072                 while (n >= ln) {
1073                     /* If it could work, try it. */
1074                     if (c1 == -1000 ||
1075                         UCHARAT(reginput) == c1 ||
1076                         UCHARAT(reginput) == c2)
1077                     {
1078                         if (regmatch(next))
1079                             sayYES;
1080                     }
1081                     /* Couldn't or didn't -- back up. */
1082                     n--;
1083                     reginput = locinput + n;
1084                 }
1085             }
1086             sayNO;
1087         case SUCCEED:
1088         case END:
1089             reginput = locinput;        /* put where regtry can find it */
1090             sayYES;                     /* Success! */
1091         case IFMATCH:
1092             reginput = locinput;
1093             scan = NEXTOPER(scan);
1094             if (!regmatch(scan))
1095                 sayNO;
1096             break;
1097         case UNLESSM:
1098             reginput = locinput;
1099             scan = NEXTOPER(scan);
1100             if (regmatch(scan))
1101                 sayNO;
1102             break;
1103         default:
1104             PerlIO_printf(PerlIO_stderr(), "%lx %d\n",
1105                           (unsigned long)scan, scan[1]);
1106             FAIL("regexp memory corruption");
1107         }
1108         scan = next;
1109     }
1110
1111     /*
1112     * We get here only if there's trouble -- normally "case END" is
1113     * the terminating point.
1114     */
1115     FAIL("corrupted regexp pointers");
1116     /*NOTREACHED*/
1117     sayNO;
1118
1119 yes:
1120 #ifdef DEBUGGING
1121     regindent--;
1122 #endif
1123     return 1;
1124
1125 no:
1126 #ifdef DEBUGGING
1127     regindent--;
1128 #endif
1129     return 0;
1130 }
1131
1132 /*
1133  - regrepeat - repeatedly match something simple, report how many
1134  */
1135 /*
1136  * [This routine now assumes that it will only match on things of length 1.
1137  * That was true before, but now we assume scan - reginput is the count,
1138  * rather than incrementing count on every character.]
1139  */
1140 static I32
1141 regrepeat(p, max)
1142 char *p;
1143 I32 max;
1144 {
1145     register char *scan;
1146     register char *opnd;
1147     register I32 c;
1148     register char *loceol = regeol;
1149
1150     scan = reginput;
1151     if (max != 32767 && max < loceol - scan)
1152       loceol = scan + max;
1153     opnd = OPERAND(p);
1154     switch (OP(p)) {
1155     case ANY:
1156         while (scan < loceol && *scan != '\n')
1157             scan++;
1158         break;
1159     case SANY:
1160         scan = loceol;
1161         break;
1162     case EXACT:         /* length of string is 1 */
1163         c = UCHARAT(++opnd);
1164         while (scan < loceol && UCHARAT(scan) == c)
1165             scan++;
1166         break;
1167     case EXACTF:        /* length of string is 1 */
1168         c = UCHARAT(++opnd);
1169         while (scan < loceol &&
1170                (UCHARAT(scan) == c || UCHARAT(scan) == fold[c]))
1171             scan++;
1172         break;
1173     case EXACTFL:       /* length of string is 1 */
1174         regtainted = TRUE;
1175         c = UCHARAT(++opnd);
1176         while (scan < loceol &&
1177                (UCHARAT(scan) == c || UCHARAT(scan) == fold_locale[c]))
1178             scan++;
1179         break;
1180     case ANYOF:
1181         while (scan < loceol && reginclass(opnd, *scan))
1182             scan++;
1183         break;
1184     case ALNUM:
1185         while (scan < loceol && isALNUM(*scan))
1186             scan++;
1187         break;
1188     case ALNUML:
1189         regtainted = TRUE;
1190         while (scan < loceol && isALNUM_LC(*scan))
1191             scan++;
1192         break;
1193     case NALNUM:
1194         while (scan < loceol && !isALNUM(*scan))
1195             scan++;
1196         break;
1197     case NALNUML:
1198         regtainted = TRUE;
1199         while (scan < loceol && !isALNUM_LC(*scan))
1200             scan++;
1201         break;
1202     case SPACE:
1203         while (scan < loceol && isSPACE(*scan))
1204             scan++;
1205         break;
1206     case SPACEL:
1207         regtainted = TRUE;
1208         while (scan < loceol && isSPACE_LC(*scan))
1209             scan++;
1210         break;
1211     case NSPACE:
1212         while (scan < loceol && !isSPACE(*scan))
1213             scan++;
1214         break;
1215     case NSPACEL:
1216         regtainted = TRUE;
1217         while (scan < loceol && !isSPACE_LC(*scan))
1218             scan++;
1219         break;
1220     case DIGIT:
1221         while (scan < loceol && isDIGIT(*scan))
1222             scan++;
1223         break;
1224     case NDIGIT:
1225         while (scan < loceol && !isDIGIT(*scan))
1226             scan++;
1227         break;
1228     default:            /* Called on something of 0 width. */
1229         break;          /* So match right here or not at all. */
1230     }
1231
1232     c = scan - reginput;
1233     reginput = scan;
1234
1235     return(c);
1236 }
1237
1238 /*
1239  - regclass - determine if a character falls into a character class
1240  */
1241
1242 static bool
1243 reginclass(p, c)
1244 register char *p;
1245 register I32 c;
1246 {
1247     char flags = *p;
1248     bool match = FALSE;
1249
1250     c &= 0xFF;
1251     if (p[1 + (c >> 3)] & (1 << (c & 7)))
1252         match = TRUE;
1253     else if (flags & ANYOF_FOLD) {
1254         I32 cf;
1255         if (flags & ANYOF_LOCALE) {
1256             regtainted = TRUE;
1257             cf = fold_locale[c];
1258         }
1259         else
1260             cf = fold[c];
1261         if (p[1 + (cf >> 3)] & (1 << (cf & 7)))
1262             match = TRUE;
1263     }
1264
1265     if (!match && (flags & ANYOF_ISA)) {
1266         regtainted = TRUE;
1267
1268         if (((flags & ANYOF_ALNUML)  && isALNUM_LC(c))  ||
1269             ((flags & ANYOF_NALNUML) && !isALNUM_LC(c)) ||
1270             ((flags & ANYOF_SPACEL)  && isSPACE_LC(c))  ||
1271             ((flags & ANYOF_NSPACEL) && !isSPACE_LC(c)))
1272         {
1273             match = TRUE;
1274         }
1275     }
1276
1277     return match ^ ((flags & ANYOF_INVERT) != 0);
1278 }
1279
1280 /*
1281  - regnext - dig the "next" pointer out of a node
1282  *
1283  * [Note, when REGALIGN is defined there are two places in regmatch()
1284  * that bypass this code for speed.]
1285  */
1286 char *
1287 regnext(p)
1288 register char *p;
1289 {
1290     register I32 offset;
1291
1292     if (p == &regdummy)
1293         return(NULL);
1294
1295     offset = NEXT(p);
1296     if (offset == 0)
1297         return(NULL);
1298
1299 #ifdef REGALIGN
1300     return(p+offset);
1301 #else
1302     if (OP(p) == BACK)
1303         return(p-offset);
1304     else
1305         return(p+offset);
1306 #endif
1307 }