d3b678327908b71b23d30b4568c93d507bcbc794
[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 #define RF_tainted      1               /* tainted information used? */
59 #define RF_warned       2               /* warned about big count? */
60 #define RF_evaled       4               /* Did an EVAL? */
61
62 #ifndef STATIC
63 #define STATIC  static
64 #endif
65
66 #ifndef PERL_OBJECT
67 typedef I32 CHECKPOINT;
68
69 /*
70  * Forwards.
71  */
72
73 static I32 regmatch _((regnode *prog));
74 static I32 regrepeat _((regnode *p, I32 max));
75 static I32 regrepeat_hard _((regnode *p, I32 max, I32 *lp));
76 static I32 regtry _((regexp *prog, char *startpos));
77 static bool reginclass _((char *p, I32 c));
78 static CHECKPOINT regcppush _((I32 parenfloor));
79 static char * regcppop _((void));
80 #endif
81
82 STATIC CHECKPOINT
83 regcppush(I32 parenfloor)
84 {
85     dTHR;
86     int retval = savestack_ix;
87     int i = (regsize - parenfloor) * 4;
88     int p;
89
90     SSCHECK(i + 5);
91     for (p = regsize; p > parenfloor; p--) {
92         SSPUSHPTR(regendp[p]);
93         SSPUSHPTR(regstartp[p]);
94         SSPUSHPTR(reg_start_tmp[p]);
95         SSPUSHINT(p);
96     }
97     SSPUSHINT(regsize);
98     SSPUSHINT(*reglastparen);
99     SSPUSHPTR(reginput);
100     SSPUSHINT(i + 3);
101     SSPUSHINT(SAVEt_REGCONTEXT);
102     return retval;
103 }
104
105 /* These are needed since we do not localize EVAL nodes: */
106 #  define REGCP_SET  DEBUG_r(PerlIO_printf(Perl_debug_log, "  Setting an EVAL scope, savestack=%i\n", savestack_ix)); lastcp = savestack_ix
107 #  define REGCP_UNWIND  DEBUG_r(lastcp != savestack_ix ? PerlIO_printf(Perl_debug_log,"  Clearing an EVAL scope, savestack=%i..%i\n", lastcp, savestack_ix) : 0); regcpblow(lastcp)
108
109 STATIC char *
110 regcppop(void)
111 {
112     dTHR;
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 -= 4) {
123         paren = (U32)SSPOPINT;
124         reg_start_tmp[paren] = (char *) SSPOPPTR;
125         regstartp[paren] = (char *) SSPOPPTR;
126         tmps = (char*)SSPOPPTR;
127         if (paren <= *reglastparen)
128             regendp[paren] = tmps;
129         DEBUG_r(
130             PerlIO_printf(Perl_debug_log, "     restoring \\%d to %d(%d)..%d%s\n",
131                           paren, regstartp[paren] - regbol, 
132                           reg_start_tmp[paren] - regbol,
133                           regendp[paren] - regbol, 
134                           (paren > *reglastparen ? "(no)" : ""));
135         );
136     }
137     DEBUG_r(
138         if (*reglastparen + 1 <= regnpar) {
139             PerlIO_printf(Perl_debug_log, "     restoring \\%d..\\%d to undef\n",
140                           *reglastparen + 1, regnpar);
141         }
142     );
143     for (paren = *reglastparen + 1; paren <= regnpar; paren++) {
144         if (paren > regsize)
145             regstartp[paren] = Nullch;
146         regendp[paren] = Nullch;
147     }
148     return input;
149 }
150
151 #define regcpblow(cp) LEAVE_SCOPE(cp)
152
153 /*
154  * pregexec and friends
155  */
156
157 /*
158  - pregexec - match a regexp against a string
159  */
160 I32
161 pregexec(register regexp *prog, char *stringarg, register char *strend, char *strbeg, I32 minend, SV *screamer, U32 nosave)
162 /* strend: pointer to null at end of string */
163 /* strbeg: real beginning of string */
164 /* minend: end of match must be >=minend after stringarg. */
165 /* nosave: For optimizations. */
166 {
167     return
168         regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL, 
169                       nosave ? 0 : REXEC_COPY_STR);
170 }
171   
172 /*
173  - regexec_flags - match a regexp against a string
174  */
175 I32
176 regexec_flags(register regexp *prog, char *stringarg, register char *strend, char *strbeg, I32 minend, SV *screamer, void *data, U32 flags)
177 /* strend: pointer to null at end of string */
178 /* strbeg: real beginning of string */
179 /* minend: end of match must be >=minend after stringarg. */
180 /* data: May be used for some additional optimizations. */
181 /* nosave: For optimizations. */
182 {
183     register char *s;
184     register regnode *c;
185     register char *startpos = stringarg;
186     register I32 tmp;
187     I32 minlen;         /* must match at least this many chars */
188     I32 dontbother = 0; /* how many characters not to try at end */
189     CURCUR cc;
190     I32 start_shift = 0;                /* Offset of the start to find
191                                          constant substr. */
192     I32 end_shift = 0;                  /* Same for the end. */
193     I32 scream_pos = -1;                /* Internal iterator of scream. */
194     char *scream_olds;
195
196     cc.cur = 0;
197     cc.oldcc = 0;
198     regcc = &cc;
199
200     regprecomp = prog->precomp;         /* Needed for error messages. */
201 #ifdef DEBUGGING
202     regnarrate = debug & 512;
203     regprogram = prog->program;
204 #endif
205
206     /* Be paranoid... */
207     if (prog == NULL || startpos == NULL) {
208         croak("NULL regexp parameter");
209         return 0;
210     }
211
212     minlen = prog->minlen;
213     if (strend - startpos < minlen) goto phooey;
214
215     if (startpos == strbeg)     /* is ^ valid at stringarg? */
216         regprev = '\n';
217     else {
218         regprev = stringarg[-1];
219         if (!multiline && regprev == '\n')
220             regprev = '\0';             /* force ^ to NOT match */
221     }
222
223     /* Check validity of program. */
224     if (UCHARAT(prog->program) != MAGIC) {
225         FAIL("corrupted regexp program");
226     }
227
228     regnpar = prog->nparens;
229     reg_flags = 0;
230     reg_eval_set = 0;
231
232     /* If there is a "must appear" string, look for it. */
233     s = startpos;
234     if (!(flags & REXEC_CHECKED) 
235         && prog->check_substr != Nullsv &&
236         !(prog->reganch & ROPT_ANCH_GPOS) &&
237         (!(prog->reganch & (ROPT_ANCH_BOL | ROPT_ANCH_MBOL))
238          || (multiline && prog->check_substr == prog->anchored_substr)) )
239     {
240         start_shift = prog->check_offset_min;
241         /* Should be nonnegative! */
242         end_shift = minlen - start_shift - SvCUR(prog->check_substr);
243         if (screamer) {
244             if (screamfirst[BmRARE(prog->check_substr)] >= 0)
245                     s = screaminstr(screamer, prog->check_substr, 
246                                     start_shift + (stringarg - strbeg),
247                                     end_shift, &scream_pos, 0);
248             else
249                     s = Nullch;
250             scream_olds = s;
251         }
252         else
253             s = fbm_instr((unsigned char*)s + start_shift,
254                           (unsigned char*)strend - end_shift,
255                 prog->check_substr);
256         if (!s) {
257             ++BmUSEFUL(prog->check_substr);     /* hooray */
258             goto phooey;        /* not present */
259         } else if ((s - stringarg) > prog->check_offset_max) {
260             ++BmUSEFUL(prog->check_substr);     /* hooray/2 */
261             s -= prog->check_offset_max;
262         } else if (!prog->naughty 
263                    && --BmUSEFUL(prog->check_substr) < 0
264                    && prog->check_substr == prog->float_substr) { /* boo */
265             SvREFCNT_dec(prog->check_substr);
266             prog->check_substr = Nullsv;        /* disable */
267             prog->float_substr = Nullsv;        /* clear */
268             s = startpos;
269         } else s = startpos;
270     }
271
272     /* Mark beginning of line for ^ and lookbehind. */
273     regbol = startpos;
274     bostr  = strbeg;
275
276     /* Mark end of line for $ (and such) */
277     regeol = strend;
278
279     /* see how far we have to get to not match where we matched before */
280     regtill = startpos+minend;
281
282     DEBUG_r(
283         PerlIO_printf(Perl_debug_log, 
284                       "Matching `%.60s%s' against `%.*s%s'\n",
285                       prog->precomp, 
286                       (strlen(prog->precomp) > 60 ? "..." : ""),
287                       (strend - startpos > 60 ? 60 : strend - startpos),
288                       startpos, 
289                       (strend - startpos > 60 ? "..." : ""))
290         );
291
292     /* Simplest case:  anchored match need be tried only once. */
293     /*  [unless only anchor is BOL and multiline is set] */
294     if (prog->reganch & ROPT_ANCH) {
295         if (regtry(prog, startpos))
296             goto got_it;
297         else if (!(prog->reganch & ROPT_ANCH_GPOS) &&
298                  (multiline || (prog->reganch & ROPT_IMPLICIT)
299                   || (prog->reganch & ROPT_ANCH_MBOL)))
300         {
301             if (minlen)
302                 dontbother = minlen - 1;
303             strend -= dontbother;
304             /* for multiline we only have to try after newlines */
305             if (s > startpos)
306                 s--;
307             while (s < strend) {
308                 if (*s++ == '\n') {
309                     if (s < strend && regtry(prog, s))
310                         goto got_it;
311                 }
312             }
313         }
314         goto phooey;
315     }
316
317     /* Messy cases:  unanchored match. */
318     if (prog->anchored_substr && prog->reganch & ROPT_SKIP) { 
319         /* we have /x+whatever/ */
320         /* it must be a one character string */
321         char ch = SvPVX(prog->anchored_substr)[0];
322         while (s < strend) {
323             if (*s == ch) {
324                 if (regtry(prog, s)) goto got_it;
325                 s++;
326                 while (s < strend && *s == ch)
327                     s++;
328             }
329             s++;
330         }
331     }
332     /*SUPPRESS 560*/
333     else if (prog->anchored_substr != Nullsv
334              || (prog->float_substr != Nullsv 
335                  && prog->float_max_offset < strend - s)) {
336         SV *must = prog->anchored_substr 
337             ? prog->anchored_substr : prog->float_substr;
338         I32 back_max = 
339             prog->anchored_substr ? prog->anchored_offset : prog->float_max_offset;
340         I32 back_min = 
341             prog->anchored_substr ? prog->anchored_offset : prog->float_min_offset;
342         I32 delta = back_max - back_min;
343         char *last = strend - SvCUR(must) - back_min; /* Cannot start after this */
344         char *last1 = s - 1;            /* Last position checked before */
345
346         /* XXXX check_substr already used to find `s', can optimize if
347            check_substr==must. */
348         scream_pos = -1;
349         dontbother = end_shift;
350         strend -= dontbother;
351         while ( (s <= last) &&
352                 (screamer 
353                  ? (s = screaminstr(screamer, must, s + back_min - strbeg,
354                                     end_shift, &scream_pos, 0))
355                  : (s = fbm_instr((unsigned char*)s + back_min,
356                                   (unsigned char*)strend, must))) ) {
357             if (s - back_max > last1) {
358                 last1 = s - back_min;
359                 s = s - back_max;
360             } else {
361                 char *t = last1 + 1;            
362
363                 last1 = s - back_min;
364                 s = t;          
365             }
366             while (s <= last1) {
367                 if (regtry(prog, s))
368                     goto got_it;
369                 s++;
370             }
371         }
372         goto phooey;
373     } else if (c = prog->regstclass) {
374         I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
375         char *Class;
376
377         if (minlen)
378             dontbother = minlen - 1;
379         strend -= dontbother;   /* don't bother with what can't match */
380         tmp = 1;
381         /* We know what class it must start with. */
382         switch (OP(c)) {
383         case ANYOF:
384             Class = (char *) OPERAND(c);
385             while (s < strend) {
386                 if (reginclass(Class, *s)) {
387                     if (tmp && regtry(prog, s))
388                         goto got_it;
389                     else
390                         tmp = doevery;
391                 }
392                 else
393                     tmp = 1;
394                 s++;
395             }
396             break;
397         case BOUNDL:
398             reg_flags |= RF_tainted;
399             /* FALL THROUGH */
400         case BOUND:
401             if (minlen)
402                 dontbother++,strend--;
403             tmp = (s != startpos) ? UCHARAT(s - 1) : regprev;
404             tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
405             while (s < strend) {
406                 if (tmp == !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
407                     tmp = !tmp;
408                     if (regtry(prog, s))
409                         goto got_it;
410                 }
411                 s++;
412             }
413             if ((minlen || tmp) && regtry(prog,s))
414                 goto got_it;
415             break;
416         case NBOUNDL:
417             reg_flags |= RF_tainted;
418             /* FALL THROUGH */
419         case NBOUND:
420             if (minlen)
421                 dontbother++,strend--;
422             tmp = (s != startpos) ? UCHARAT(s - 1) : regprev;
423             tmp = ((OP(c) == NBOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
424             while (s < strend) {
425                 if (tmp == !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
426                     tmp = !tmp;
427                 else if (regtry(prog, s))
428                     goto got_it;
429                 s++;
430             }
431             if ((minlen || !tmp) && regtry(prog,s))
432                 goto got_it;
433             break;
434         case ALNUM:
435             while (s < strend) {
436                 if (isALNUM(*s)) {
437                     if (tmp && regtry(prog, s))
438                         goto got_it;
439                     else
440                         tmp = doevery;
441                 }
442                 else
443                     tmp = 1;
444                 s++;
445             }
446             break;
447         case ALNUML:
448             reg_flags |= RF_tainted;
449             while (s < strend) {
450                 if (isALNUM_LC(*s)) {
451                     if (tmp && regtry(prog, s))
452                         goto got_it;
453                     else
454                         tmp = doevery;
455                 }
456                 else
457                     tmp = 1;
458                 s++;
459             }
460             break;
461         case NALNUM:
462             while (s < strend) {
463                 if (!isALNUM(*s)) {
464                     if (tmp && regtry(prog, s))
465                         goto got_it;
466                     else
467                         tmp = doevery;
468                 }
469                 else
470                     tmp = 1;
471                 s++;
472             }
473             break;
474         case NALNUML:
475             reg_flags |= RF_tainted;
476             while (s < strend) {
477                 if (!isALNUM_LC(*s)) {
478                     if (tmp && regtry(prog, s))
479                         goto got_it;
480                     else
481                         tmp = doevery;
482                 }
483                 else
484                     tmp = 1;
485                 s++;
486             }
487             break;
488         case SPACE:
489             while (s < strend) {
490                 if (isSPACE(*s)) {
491                     if (tmp && regtry(prog, s))
492                         goto got_it;
493                     else
494                         tmp = doevery;
495                 }
496                 else
497                     tmp = 1;
498                 s++;
499             }
500             break;
501         case SPACEL:
502             reg_flags |= RF_tainted;
503             while (s < strend) {
504                 if (isSPACE_LC(*s)) {
505                     if (tmp && regtry(prog, s))
506                         goto got_it;
507                     else
508                         tmp = doevery;
509                 }
510                 else
511                     tmp = 1;
512                 s++;
513             }
514             break;
515         case NSPACE:
516             while (s < strend) {
517                 if (!isSPACE(*s)) {
518                     if (tmp && regtry(prog, s))
519                         goto got_it;
520                     else
521                         tmp = doevery;
522                 }
523                 else
524                     tmp = 1;
525                 s++;
526             }
527             break;
528         case NSPACEL:
529             reg_flags |= RF_tainted;
530             while (s < strend) {
531                 if (!isSPACE_LC(*s)) {
532                     if (tmp && regtry(prog, s))
533                         goto got_it;
534                     else
535                         tmp = doevery;
536                 }
537                 else
538                     tmp = 1;
539                 s++;
540             }
541             break;
542         case DIGIT:
543             while (s < strend) {
544                 if (isDIGIT(*s)) {
545                     if (tmp && regtry(prog, s))
546                         goto got_it;
547                     else
548                         tmp = doevery;
549                 }
550                 else
551                     tmp = 1;
552                 s++;
553             }
554             break;
555         case NDIGIT:
556             while (s < strend) {
557                 if (!isDIGIT(*s)) {
558                     if (tmp && regtry(prog, s))
559                         goto got_it;
560                     else
561                         tmp = doevery;
562                 }
563                 else
564                     tmp = 1;
565                 s++;
566             }
567             break;
568         }
569     }
570     else {
571         dontbother = 0;
572         if (prog->float_substr != Nullsv) {     /* Trim the end. */
573             char *last;
574             I32 oldpos = scream_pos;
575
576             if (screamer) {
577                 last = screaminstr(screamer, prog->float_substr, s - strbeg,
578                                    end_shift, &scream_pos, 1); /* last one */
579                 if (!last) {
580                     last = scream_olds; /* Only one occurence. */
581                 }
582             } else {
583                 STRLEN len;
584                 char *little = SvPV(prog->float_substr, len);
585                 last = rninstr(s, strend, little, little + len);
586             }
587             if (last == NULL) goto phooey; /* Should not happen! */
588             dontbother = strend - last - 1;
589         }
590         if (minlen && (dontbother < minlen))
591             dontbother = minlen - 1;
592         strend -= dontbother;
593         /* We don't know much -- general case. */
594         do {
595             if (regtry(prog, s))
596                 goto got_it;
597         } while (s++ < strend);
598     }
599
600     /* Failure. */
601     goto phooey;
602
603 got_it:
604     strend += dontbother;       /* uncheat */
605     prog->subbeg = strbeg;
606     prog->subend = strend;
607     RX_MATCH_TAINTED_SET(prog, reg_flags & RF_tainted);
608
609     /* make sure $`, $&, $', and $digit will work later */
610     if (strbeg != prog->subbase) {      /* second+ //g match.  */
611         if (!(flags & REXEC_COPY_STR)) {
612             if (prog->subbase) {
613                 Safefree(prog->subbase);
614                 prog->subbase = Nullch;
615             }
616         }
617         else {
618             I32 i = strend - startpos + (stringarg - strbeg);
619             s = savepvn(strbeg, i);
620             Safefree(prog->subbase);
621             prog->subbase = s;
622             prog->subbeg = prog->subbase;
623             prog->subend = prog->subbase + i;
624             s = prog->subbase + (stringarg - strbeg);
625             for (i = 0; i <= prog->nparens; i++) {
626                 if (prog->endp[i]) {
627                     prog->startp[i] = s + (prog->startp[i] - startpos);
628                     prog->endp[i] = s + (prog->endp[i] - startpos);
629                 }
630             }
631         }
632     }
633     return 1;
634
635 phooey:
636     return 0;
637 }
638
639 /*
640  - regtry - try match at specific point
641  */
642 STATIC I32                      /* 0 failure, 1 success */
643 regtry(regexp *prog, char *startpos)
644 {
645     dTHR;
646     register I32 i;
647     register char **sp;
648     register char **ep;
649     CHECKPOINT lastcp;
650
651     reginput = startpos;
652     regstartp = prog->startp;
653     regendp = prog->endp;
654     reglastparen = &prog->lastparen;
655     prog->lastparen = 0;
656     regsize = 0;
657     if (reg_start_tmpl <= prog->nparens) {
658         reg_start_tmpl = prog->nparens*3/2 + 3;
659         if(reg_start_tmp)
660             Renew(reg_start_tmp, reg_start_tmpl, char*);
661         else
662             New(22,reg_start_tmp, reg_start_tmpl, char*);
663     }
664
665     sp = prog->startp;
666     ep = prog->endp;
667     regdata = prog->data;
668     if (prog->nparens) {
669         for (i = prog->nparens; i >= 0; i--) {
670             *sp++ = NULL;
671             *ep++ = NULL;
672         }
673     }
674     REGCP_SET;
675     if (regmatch(prog->program + 1) && reginput >= regtill) {
676         prog->startp[0] = startpos;
677         prog->endp[0] = reginput;
678         return 1;
679     }
680     REGCP_UNWIND;
681     return 0;
682 }
683
684 /*
685  - regmatch - main matching routine
686  *
687  * Conceptually the strategy is simple:  check to see whether the current
688  * node matches, call self recursively to see whether the rest matches,
689  * and then act accordingly.  In practice we make some effort to avoid
690  * recursion, in particular by going through "ordinary" nodes (that don't
691  * need to know whether the rest of the match failed) by a loop instead of
692  * by recursion.
693  */
694 /* [lwall] I've hoisted the register declarations to the outer block in order to
695  * maybe save a little bit of pushing and popping on the stack.  It also takes
696  * advantage of machines that use a register save mask on subroutine entry.
697  */
698 STATIC I32                      /* 0 failure, 1 success */
699 regmatch(regnode *prog)
700 {
701     dTHR;
702     register regnode *scan;     /* Current node. */
703     regnode *next;              /* Next node. */
704     regnode *inner;             /* Next node in internal branch. */
705     register I32 nextchr; /* renamed nextchr - nextchar colides with function of same name */
706     register I32 n;             /* no or next */
707     register I32 ln;            /* len or last */
708     register char *s;           /* operand or save */
709     register char *locinput = reginput;
710     register I32 c1, c2, paren; /* case fold search, parenth */
711     int minmod = 0, sw = 0, logical = 0;
712 #ifdef DEBUGGING
713     regindent++;
714 #endif
715
716     nextchr = UCHARAT(locinput);
717     scan = prog;
718     while (scan != NULL) {
719 #define sayNO_L (logical ? (logical = 0, sw = 0, goto cont) : sayNO)
720 #ifdef DEBUGGING
721 #  define sayYES goto yes
722 #  define sayNO goto no
723 #  define saySAME(x) if (x) goto yes; else goto no
724 #  define REPORT_CODE_OFF 24
725 #else
726 #  define sayYES return 1
727 #  define sayNO return 0
728 #  define saySAME(x) return x
729 #endif
730         DEBUG_r( {
731             SV *prop = sv_newmortal();
732             int docolor = *colors[0];
733             int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
734             int l = (regeol - locinput > taill ? taill : regeol - locinput);
735             int pref_len = (locinput - bostr > (5 + taill) - l 
736                             ? (5 + taill) - l : locinput - bostr);
737
738             if (l + pref_len < (5 + taill) && l < regeol - locinput)
739                 l = ( regeol - locinput > (5 + taill) - pref_len 
740                       ? (5 + taill) - pref_len : regeol - locinput);
741             regprop(prop, scan);
742             PerlIO_printf(Perl_debug_log, 
743                           "%4i <%s%.*s%s%s%s%.*s%s>%*s|%*s%2d%s\n",
744                           locinput - bostr, 
745                           colors[2], pref_len, locinput - pref_len, colors[3],
746                           (docolor ? "" : "> <"),
747                           colors[0], l, locinput, colors[1],
748                           15 - l - pref_len + 1,
749                           "",
750                           regindent*2, "", scan - regprogram,
751                           SvPVX(prop));
752         } );
753
754 #ifdef REGALIGN
755         next = scan + NEXT_OFF(scan);
756         if (next == scan)
757             next = NULL;
758 #else
759         next = regnext(scan);
760 #endif
761
762         switch (OP(scan)) {
763         case BOL:
764             if (locinput == regbol
765                 ? regprev == '\n'
766                 : (multiline && 
767                    (nextchr || locinput < regeol) && locinput[-1] == '\n') )
768             {
769                 /* regtill = regbol; */
770                 break;
771             }
772             sayNO;
773         case MBOL:
774             if (locinput == regbol
775                 ? regprev == '\n'
776                 : ((nextchr || locinput < regeol) && locinput[-1] == '\n') )
777             {
778                 break;
779             }
780             sayNO;
781         case SBOL:
782             if (locinput == regbol && regprev == '\n')
783                 break;
784             sayNO;
785         case GPOS:
786             if (locinput == regbol)
787                 break;
788             sayNO;
789         case EOL:
790             if (multiline)
791                 goto meol;
792             else
793                 goto seol;
794         case MEOL:
795           meol:
796             if ((nextchr || locinput < regeol) && nextchr != '\n')
797                 sayNO;
798             break;
799         case SEOL:
800           seol:
801             if ((nextchr || locinput < regeol) && nextchr != '\n')
802                 sayNO;
803             if (regeol - locinput > 1)
804                 sayNO;
805             break;
806         case SANY:
807             if (!nextchr && locinput >= regeol)
808                 sayNO;
809             nextchr = UCHARAT(++locinput);
810             break;
811         case ANY:
812             if (!nextchr && locinput >= regeol || nextchr == '\n')
813                 sayNO;
814             nextchr = UCHARAT(++locinput);
815             break;
816         case EXACT:
817             s = (char *) OPERAND(scan);
818             ln = UCHARAT(s++);
819             /* Inline the first character, for speed. */
820             if (UCHARAT(s) != nextchr)
821                 sayNO;
822             if (regeol - locinput < ln)
823                 sayNO;
824             if (ln > 1 && memNE(s, locinput, ln))
825                 sayNO;
826             locinput += ln;
827             nextchr = UCHARAT(locinput);
828             break;
829         case EXACTFL:
830             reg_flags |= RF_tainted;
831             /* FALL THROUGH */
832         case EXACTF:
833             s = (char *) OPERAND(scan);
834             ln = UCHARAT(s++);
835             /* Inline the first character, for speed. */
836             if (UCHARAT(s) != nextchr &&
837                 UCHARAT(s) != ((OP(scan) == EXACTF)
838                                ? fold : fold_locale)[nextchr])
839                 sayNO;
840             if (regeol - locinput < ln)
841                 sayNO;
842             if (ln > 1 && (OP(scan) == EXACTF
843                            ? ibcmp(s, locinput, ln)
844                            : ibcmp_locale(s, locinput, ln)))
845                 sayNO;
846             locinput += ln;
847             nextchr = UCHARAT(locinput);
848             break;
849         case ANYOF:
850             s = (char *) OPERAND(scan);
851             if (nextchr < 0)
852                 nextchr = UCHARAT(locinput);
853             if (!reginclass(s, nextchr))
854                 sayNO;
855             if (!nextchr && locinput >= regeol)
856                 sayNO;
857             nextchr = UCHARAT(++locinput);
858             break;
859         case ALNUML:
860             reg_flags |= RF_tainted;
861             /* FALL THROUGH */
862         case ALNUM:
863             if (!nextchr)
864                 sayNO;
865             if (!(OP(scan) == ALNUM
866                   ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
867                 sayNO;
868             nextchr = UCHARAT(++locinput);
869             break;
870         case NALNUML:
871             reg_flags |= RF_tainted;
872             /* FALL THROUGH */
873         case NALNUM:
874             if (!nextchr && locinput >= regeol)
875                 sayNO;
876             if (OP(scan) == NALNUM
877                 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
878                 sayNO;
879             nextchr = UCHARAT(++locinput);
880             break;
881         case BOUNDL:
882         case NBOUNDL:
883             reg_flags |= RF_tainted;
884             /* FALL THROUGH */
885         case BOUND:
886         case NBOUND:
887             /* was last char in word? */
888             ln = (locinput != regbol) ? UCHARAT(locinput - 1) : regprev;
889             if (OP(scan) == BOUND || OP(scan) == NBOUND) {
890                 ln = isALNUM(ln);
891                 n = isALNUM(nextchr);
892             }
893             else {
894                 ln = isALNUM_LC(ln);
895                 n = isALNUM_LC(nextchr);
896             }
897             if (((!ln) == (!n)) == (OP(scan) == BOUND || OP(scan) == BOUNDL))
898                 sayNO;
899             break;
900         case SPACEL:
901             reg_flags |= RF_tainted;
902             /* FALL THROUGH */
903         case SPACE:
904             if (!nextchr && locinput >= regeol)
905                 sayNO;
906             if (!(OP(scan) == SPACE
907                   ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
908                 sayNO;
909             nextchr = UCHARAT(++locinput);
910             break;
911         case NSPACEL:
912             reg_flags |= RF_tainted;
913             /* FALL THROUGH */
914         case NSPACE:
915             if (!nextchr)
916                 sayNO;
917             if (OP(scan) == SPACE
918                 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
919                 sayNO;
920             nextchr = UCHARAT(++locinput);
921             break;
922         case DIGIT:
923             if (!isDIGIT(nextchr))
924                 sayNO;
925             nextchr = UCHARAT(++locinput);
926             break;
927         case NDIGIT:
928             if (!nextchr && locinput >= regeol)
929                 sayNO;
930             if (isDIGIT(nextchr))
931                 sayNO;
932             nextchr = UCHARAT(++locinput);
933             break;
934         case REFFL:
935             reg_flags |= RF_tainted;
936             /* FALL THROUGH */
937         case REF:
938         case REFF:
939             n = ARG(scan);  /* which paren pair */
940             s = regstartp[n];
941             if (*reglastparen < n || !s)
942                 break;                  /* Zero length always matches */
943             if (s == regendp[n])
944                 break;
945             /* Inline the first character, for speed. */
946             if (UCHARAT(s) != nextchr &&
947                 (OP(scan) == REF ||
948                  (UCHARAT(s) != ((OP(scan) == REFF
949                                   ? fold : fold_locale)[nextchr]))))
950                 sayNO;
951             ln = regendp[n] - s;
952             if (locinput + ln > regeol)
953                 sayNO;
954             if (ln > 1 && (OP(scan) == REF
955                            ? memNE(s, locinput, ln)
956                            : (OP(scan) == REFF
957                               ? ibcmp(s, locinput, ln)
958                               : ibcmp_locale(s, locinput, ln))))
959                 sayNO;
960             locinput += ln;
961             nextchr = UCHARAT(locinput);
962             break;
963
964         case NOTHING:
965         case TAIL:
966             break;
967         case BACK:
968             break;
969         case EVAL:
970         {
971             dSP;
972             OP_4tree *oop = op;
973             COP *ocurcop = curcop;
974             SV **ocurpad = curpad;
975             SV *ret;
976             
977             n = ARG(scan);
978             op = (OP_4tree*)regdata->data[n];
979             DEBUG_r( PerlIO_printf(Perl_debug_log, "  re_eval 0x%x\n", op) );
980             curpad = AvARRAY((AV*)regdata->data[n + 1]);
981             if (!reg_eval_set) {
982                 /* Preserve whatever is on stack now, otherwise
983                    OP_NEXTSTATE will overwrite it. */
984                 SAVEINT(reg_eval_set);  /* Protect against unwinding. */
985                 reg_eval_set = 1;
986                 DEBUG_r(DEBUG_s(
987                     PerlIO_printf(Perl_debug_log, "  setting stack tmpbase at %i\n", stack_sp - stack_base);
988                     ));
989                 SAVEINT(cxstack[cxstack_ix].blk_oldsp);
990                 cxstack[cxstack_ix].blk_oldsp = stack_sp - stack_base;
991                 /* Otherwise OP_NEXTSTATE will free whatever on stack now.  */
992                 SAVETMPS;
993                 /* Apparently this is not needed, judging by wantarray. */
994                 /* SAVEINT(cxstack[cxstack_ix].blk_gimme);
995                    cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
996             }
997
998             CALLRUNOPS();                       /* Scalar context. */
999             SPAGAIN;
1000             ret = POPs;
1001             PUTBACK;
1002             
1003             if (logical) {
1004                 logical = 0;
1005                 sw = SvTRUE(ret);
1006             }
1007             op = oop;
1008             curpad = ocurpad;
1009             curcop = ocurcop;
1010             break;
1011         }
1012         case OPEN:
1013             n = ARG(scan);  /* which paren pair */
1014             reg_start_tmp[n] = locinput;
1015             if (n > regsize)
1016                 regsize = n;
1017             break;
1018         case CLOSE:
1019             n = ARG(scan);  /* which paren pair */
1020             regstartp[n] = reg_start_tmp[n];
1021             regendp[n] = locinput;
1022             if (n > *reglastparen)
1023                 *reglastparen = n;
1024             break;
1025         case GROUPP:
1026             n = ARG(scan);  /* which paren pair */
1027             sw = (*reglastparen >= n && regendp[n] != NULL);
1028             break;
1029         case IFTHEN:
1030             if (sw)
1031                 next = NEXTOPER(NEXTOPER(scan));
1032             else {
1033                 next = scan + ARG(scan);
1034                 if (OP(next) == IFTHEN) /* Fake one. */
1035                     next = NEXTOPER(NEXTOPER(next));
1036             }
1037             break;
1038         case LOGICAL:
1039             logical = 1;
1040             break;
1041         case CURLYX: {
1042                 CURCUR cc;
1043                 CHECKPOINT cp = savestack_ix;
1044
1045                 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
1046                     next += ARG(next);
1047                 cc.oldcc = regcc;
1048                 regcc = &cc;
1049                 cc.parenfloor = *reglastparen;
1050                 cc.cur = -1;
1051                 cc.min = ARG1(scan);
1052                 cc.max  = ARG2(scan);
1053                 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
1054                 cc.next = next;
1055                 cc.minmod = minmod;
1056                 cc.lastloc = 0;
1057                 reginput = locinput;
1058                 n = regmatch(PREVOPER(next));   /* start on the WHILEM */
1059                 regcpblow(cp);
1060                 regcc = cc.oldcc;
1061                 saySAME(n);
1062             }
1063             /* NOT REACHED */
1064         case WHILEM: {
1065                 /*
1066                  * This is really hard to understand, because after we match
1067                  * what we're trying to match, we must make sure the rest of
1068                  * the RE is going to match for sure, and to do that we have
1069                  * to go back UP the parse tree by recursing ever deeper.  And
1070                  * if it fails, we have to reset our parent's current state
1071                  * that we can try again after backing off.
1072                  */
1073
1074                 CHECKPOINT cp, lastcp;
1075                 CURCUR* cc = regcc;
1076                 char *lastloc = cc->lastloc; /* Detection of 0-len. */
1077                 
1078                 n = cc->cur + 1;        /* how many we know we matched */
1079                 reginput = locinput;
1080
1081                 DEBUG_r(
1082                     PerlIO_printf(Perl_debug_log, 
1083                                   "%*s  %ld out of %ld..%ld  cc=%lx\n", 
1084                                   REPORT_CODE_OFF+regindent*2, "",
1085                                   (long)n, (long)cc->min, 
1086                                   (long)cc->max, (long)cc)
1087                     );
1088
1089                 /* If degenerate scan matches "", assume scan done. */
1090
1091                 if (locinput == cc->lastloc && n >= cc->min) {
1092                     regcc = cc->oldcc;
1093                     ln = regcc->cur;
1094                     DEBUG_r(
1095                         PerlIO_printf(Perl_debug_log, "%*s  empty match detected, try continuation...\n", REPORT_CODE_OFF+regindent*2, "")
1096                         );
1097                     if (regmatch(cc->next))
1098                         sayYES;
1099                     DEBUG_r(
1100                         PerlIO_printf(Perl_debug_log, "%*s  failed...\n", REPORT_CODE_OFF+regindent*2, "")
1101                         );
1102                     regcc->cur = ln;
1103                     regcc = cc;
1104                     sayNO;
1105                 }
1106
1107                 /* First just match a string of min scans. */
1108
1109                 if (n < cc->min) {
1110                     cc->cur = n;
1111                     cc->lastloc = locinput;
1112                     if (regmatch(cc->scan))
1113                         sayYES;
1114                     cc->cur = n - 1;
1115                     cc->lastloc = lastloc;
1116                     DEBUG_r(
1117                         PerlIO_printf(Perl_debug_log, "%*s  failed...\n", REPORT_CODE_OFF+regindent*2, "")
1118                         );
1119                     sayNO;
1120                 }
1121
1122                 /* Prefer next over scan for minimal matching. */
1123
1124                 if (cc->minmod) {
1125                     regcc = cc->oldcc;
1126                     ln = regcc->cur;
1127                     cp = regcppush(cc->parenfloor);
1128                     REGCP_SET;
1129                     if (regmatch(cc->next)) {
1130                         regcpblow(cp);
1131                         sayYES; /* All done. */
1132                     }
1133                     REGCP_UNWIND;
1134                     regcppop();
1135                     regcc->cur = ln;
1136                     regcc = cc;
1137
1138                     if (n >= cc->max) { /* Maximum greed exceeded? */
1139                         if (dowarn && n >= REG_INFTY 
1140                             && !(reg_flags & RF_warned)) {
1141                             reg_flags |= RF_warned;
1142                             warn("count exceeded %d", REG_INFTY - 1);
1143                         }
1144                         sayNO;
1145                     }
1146
1147                     DEBUG_r(
1148                         PerlIO_printf(Perl_debug_log, "%*s  trying longer...\n", REPORT_CODE_OFF+regindent*2, "")
1149                         );
1150                     /* Try scanning more and see if it helps. */
1151                     reginput = locinput;
1152                     cc->cur = n;
1153                     cc->lastloc = locinput;
1154                     cp = regcppush(cc->parenfloor);
1155                     REGCP_SET;
1156                     if (regmatch(cc->scan)) {
1157                         regcpblow(cp);
1158                         sayYES;
1159                     }
1160                     DEBUG_r(
1161                         PerlIO_printf(Perl_debug_log, "%*s  failed...\n", REPORT_CODE_OFF+regindent*2, "")
1162                         );
1163                     REGCP_UNWIND;
1164                     regcppop();
1165                     cc->cur = n - 1;
1166                     cc->lastloc = lastloc;
1167                     sayNO;
1168                 }
1169
1170                 /* Prefer scan over next for maximal matching. */
1171
1172                 if (n < cc->max) {      /* More greed allowed? */
1173                     cp = regcppush(cc->parenfloor);
1174                     cc->cur = n;
1175                     cc->lastloc = locinput;
1176                     REGCP_SET;
1177                     if (regmatch(cc->scan)) {
1178                         regcpblow(cp);
1179                         sayYES;
1180                     }
1181                     REGCP_UNWIND;
1182                     regcppop();         /* Restore some previous $<digit>s? */
1183                     reginput = locinput;
1184                     DEBUG_r(
1185                         PerlIO_printf(Perl_debug_log, "%*s  failed, try continuation...\n", REPORT_CODE_OFF+regindent*2, "")
1186                         );
1187                 }
1188                 if (dowarn && n >= REG_INFTY && !(reg_flags & RF_warned)) {
1189                     reg_flags |= RF_warned;
1190                     warn("count exceeded %d", REG_INFTY - 1);
1191                 }
1192
1193                 /* Failed deeper matches of scan, so see if this one works. */
1194                 regcc = cc->oldcc;
1195                 ln = regcc->cur;
1196                 if (regmatch(cc->next))
1197                     sayYES;
1198                 DEBUG_r(
1199                     PerlIO_printf(Perl_debug_log, "%*s  failed...\n", REPORT_CODE_OFF+regindent*2, "")
1200                     );
1201                 regcc->cur = ln;
1202                 regcc = cc;
1203                 cc->cur = n - 1;
1204                 cc->lastloc = lastloc;
1205                 sayNO;
1206             }
1207             /* NOT REACHED */
1208         case BRANCHJ: 
1209             next = scan + ARG(scan);
1210             if (next == scan)
1211                 next = NULL;
1212             inner = NEXTOPER(NEXTOPER(scan));
1213             goto do_branch;
1214         case BRANCH: 
1215             inner = NEXTOPER(scan);
1216           do_branch:
1217             {
1218                 CHECKPOINT lastcp;
1219                 c1 = OP(scan);
1220                 if (OP(next) != c1)     /* No choice. */
1221                     next = inner;       /* Avoid recursion. */
1222                 else {
1223                     int lastparen = *reglastparen;
1224
1225                     REGCP_SET;
1226                     do {
1227                         reginput = locinput;
1228                         if (regmatch(inner))
1229                             sayYES;
1230                         REGCP_UNWIND;
1231                         for (n = *reglastparen; n > lastparen; n--)
1232                             regendp[n] = 0;
1233                         *reglastparen = n;
1234                         scan = next;
1235 #ifdef REGALIGN
1236                         /*SUPPRESS 560*/
1237                         if (n = (c1 == BRANCH ? NEXT_OFF(next) : ARG(next)))
1238                             next += n;
1239                         else
1240                             next = NULL;
1241 #else
1242                         next = regnext(next);
1243 #endif
1244                         inner = NEXTOPER(scan);
1245                         if (c1 == BRANCHJ) {
1246                             inner = NEXTOPER(inner);
1247                         }
1248                     } while (scan != NULL && OP(scan) == c1);
1249                     sayNO;
1250                     /* NOTREACHED */
1251                 }
1252             }
1253             break;
1254         case MINMOD:
1255             minmod = 1;
1256             break;
1257         case CURLYM:
1258         {
1259             I32 l;
1260             CHECKPOINT lastcp;
1261             
1262             /* We suppose that the next guy does not need
1263                backtracking: in particular, it is of constant length,
1264                and has no parenths to influence future backrefs. */
1265             ln = ARG1(scan);  /* min to match */
1266             n  = ARG2(scan);  /* max to match */
1267 #ifdef REGALIGN_STRUCT
1268             paren = scan->flags;
1269             if (paren) {
1270                 if (paren > regsize)
1271                     regsize = paren;
1272                 if (paren > *reglastparen)
1273                     *reglastparen = paren;
1274             }
1275 #endif 
1276             scan = NEXTOPER(scan) + 4/sizeof(regnode);
1277             if (paren)
1278                 scan += NEXT_OFF(scan); /* Skip former OPEN. */
1279             reginput = locinput;
1280             if (minmod) {
1281                 minmod = 0;
1282                 if (ln && regrepeat_hard(scan, ln, &l) < ln)
1283                     sayNO;
1284                 if (l == 0 && n >= ln
1285                     /* In fact, this is tricky.  If paren, then the
1286                        fact that we did/didnot match may influence
1287                        future execution. */
1288                     && !(paren && ln == 0))
1289                     ln = n;
1290                 locinput = reginput;
1291                 if (regkind[(U8)OP(next)] == EXACT) {
1292                     c1 = UCHARAT(OPERAND(next) + 1);
1293                     if (OP(next) == EXACTF)
1294                         c2 = fold[c1];
1295                     else if (OP(next) == EXACTFL)
1296                         c2 = fold_locale[c1];
1297                     else
1298                         c2 = c1;
1299                 } else
1300                     c1 = c2 = -1000;
1301                 REGCP_SET;
1302                 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
1303                     /* If it could work, try it. */
1304                     if (c1 == -1000 ||
1305                         UCHARAT(reginput) == c1 ||
1306                         UCHARAT(reginput) == c2)
1307                     {
1308                         if (paren) {
1309                             if (n) {
1310                                 regstartp[paren] = reginput - l;
1311                                 regendp[paren] = reginput;
1312                             } else
1313                                 regendp[paren] = NULL;
1314                         }
1315                         if (regmatch(next))
1316                             sayYES;
1317                         REGCP_UNWIND;
1318                     }
1319                     /* Couldn't or didn't -- move forward. */
1320                     reginput = locinput;
1321                     if (regrepeat_hard(scan, 1, &l)) {
1322                         ln++;
1323                         locinput = reginput;
1324                     }
1325                     else
1326                         sayNO;
1327                 }
1328             } else {
1329                 n = regrepeat_hard(scan, n, &l);
1330                 if (n != 0 && l == 0
1331                     /* In fact, this is tricky.  If paren, then the
1332                        fact that we did/didnot match may influence
1333                        future execution. */
1334                     && !(paren && ln == 0))
1335                     ln = n;
1336                 locinput = reginput;
1337                 DEBUG_r(
1338                     PerlIO_printf(Perl_debug_log, "%*s  matched %ld times, len=%ld...\n", REPORT_CODE_OFF+regindent*2, "", n, l)
1339                     );
1340                 if (n >= ln) {
1341                     if (regkind[(U8)OP(next)] == EXACT) {
1342                         c1 = UCHARAT(OPERAND(next) + 1);
1343                         if (OP(next) == EXACTF)
1344                             c2 = fold[c1];
1345                         else if (OP(next) == EXACTFL)
1346                             c2 = fold_locale[c1];
1347                         else
1348                             c2 = c1;
1349                     } else
1350                         c1 = c2 = -1000;
1351                 }
1352                 REGCP_SET;
1353                 while (n >= ln) {
1354                     /* If it could work, try it. */
1355                     if (c1 == -1000 ||
1356                         UCHARAT(reginput) == c1 ||
1357                         UCHARAT(reginput) == c2)
1358                         {
1359                             DEBUG_r(
1360                                 PerlIO_printf(Perl_debug_log, "%*s  trying tail with n=%ld...\n", REPORT_CODE_OFF+regindent*2, "", n)
1361                                 );
1362                             if (paren) {
1363                                 if (n) {
1364                                     regstartp[paren] = reginput - l;
1365                                     regendp[paren] = reginput;
1366                                 } else
1367                                     regendp[paren] = NULL;
1368                             }
1369                             if (regmatch(next))
1370                                 sayYES;
1371                             REGCP_UNWIND;
1372                         }
1373                     /* Couldn't or didn't -- back up. */
1374                     n--;
1375                     locinput -= l;
1376                     reginput = locinput;
1377                 }
1378             }
1379             sayNO;
1380             break;
1381         }
1382         case CURLYN:
1383             paren = scan->flags;        /* Which paren to set */
1384             if (paren > regsize)
1385                 regsize = paren;
1386             if (paren > *reglastparen)
1387                 *reglastparen = paren;
1388             ln = ARG1(scan);  /* min to match */
1389             n  = ARG2(scan);  /* max to match */
1390             scan = regnext(NEXTOPER(scan) + 4/sizeof(regnode));
1391             goto repeat;
1392         case CURLY:
1393             paren = 0;
1394             ln = ARG1(scan);  /* min to match */
1395             n  = ARG2(scan);  /* max to match */
1396             scan = NEXTOPER(scan) + 4/sizeof(regnode);
1397             goto repeat;
1398         case STAR:
1399             ln = 0;
1400             n = REG_INFTY;
1401             scan = NEXTOPER(scan);
1402             paren = 0;
1403             goto repeat;
1404         case PLUS:
1405             ln = 1;
1406             n = REG_INFTY;
1407             scan = NEXTOPER(scan);
1408             paren = 0;
1409           repeat:
1410             /*
1411             * Lookahead to avoid useless match attempts
1412             * when we know what character comes next.
1413             */
1414             if (regkind[(U8)OP(next)] == EXACT) {
1415                 c1 = UCHARAT(OPERAND(next) + 1);
1416                 if (OP(next) == EXACTF)
1417                     c2 = fold[c1];
1418                 else if (OP(next) == EXACTFL)
1419                     c2 = fold_locale[c1];
1420                 else
1421                     c2 = c1;
1422             }
1423             else
1424                 c1 = c2 = -1000;
1425             reginput = locinput;
1426             if (minmod) {
1427                 CHECKPOINT lastcp;
1428                 minmod = 0;
1429                 if (ln && regrepeat(scan, ln) < ln)
1430                     sayNO;
1431                 REGCP_SET;
1432                 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
1433                     /* If it could work, try it. */
1434                     if (c1 == -1000 ||
1435                         UCHARAT(reginput) == c1 ||
1436                         UCHARAT(reginput) == c2)
1437                     {
1438                         if (paren) {
1439                             if (n) {
1440                                 regstartp[paren] = reginput - 1;
1441                                 regendp[paren] = reginput;
1442                             } else
1443                                 regendp[paren] = NULL;
1444                         }
1445                         if (regmatch(next))
1446                             sayYES;
1447                         REGCP_UNWIND;
1448                     }
1449                     /* Couldn't or didn't -- move forward. */
1450                     reginput = locinput + ln;
1451                     if (regrepeat(scan, 1)) {
1452                         ln++;
1453                         reginput = locinput + ln;
1454                     } else
1455                         sayNO;
1456                 }
1457             }
1458             else {
1459                 CHECKPOINT lastcp;
1460                 n = regrepeat(scan, n);
1461                 if (ln < n && regkind[(U8)OP(next)] == EOL &&
1462                     (!multiline  || OP(next) == SEOL))
1463                     ln = n;                     /* why back off? */
1464                 REGCP_SET;
1465                 if (paren) {
1466                     while (n >= ln) {
1467                         /* If it could work, try it. */
1468                         if (c1 == -1000 ||
1469                             UCHARAT(reginput) == c1 ||
1470                             UCHARAT(reginput) == c2)
1471                             {
1472                                 if (paren && n) {
1473                                     if (n) {
1474                                         regstartp[paren] = reginput - 1;
1475                                         regendp[paren] = reginput;
1476                                     } else
1477                                         regendp[paren] = NULL;
1478                                 }
1479                                 if (regmatch(next))
1480                                     sayYES;
1481                                 REGCP_UNWIND;
1482                             }
1483                         /* Couldn't or didn't -- back up. */
1484                         n--;
1485                         reginput = locinput + n;
1486                     }
1487                 } else {
1488                     while (n >= ln) {
1489                         /* If it could work, try it. */
1490                         if (c1 == -1000 ||
1491                             UCHARAT(reginput) == c1 ||
1492                             UCHARAT(reginput) == c2)
1493                             {
1494                                 if (regmatch(next))
1495                                     sayYES;
1496                                 REGCP_UNWIND;
1497                             }
1498                         /* Couldn't or didn't -- back up. */
1499                         n--;
1500                         reginput = locinput + n;
1501                     }
1502                 }
1503             }
1504             sayNO;
1505             break;
1506         case SUCCEED:
1507         case END:
1508             reginput = locinput;        /* put where regtry can find it */
1509             sayYES;                     /* Success! */
1510         case SUSPEND:
1511             n = 1;
1512             goto do_ifmatch;        
1513         case UNLESSM:
1514             n = 0;
1515             if (locinput < bostr + scan->flags) 
1516                 goto say_yes;
1517             goto do_ifmatch;
1518         case IFMATCH:
1519             n = 1;
1520             if (locinput < bostr + scan->flags) 
1521                 goto say_no;
1522           do_ifmatch:
1523             reginput = locinput - scan->flags;
1524             inner = NEXTOPER(NEXTOPER(scan));
1525             if (regmatch(inner) != n) {
1526               say_no:
1527                 if (logical) {
1528                     logical = 0;
1529                     sw = 0;
1530                     goto do_longjump;
1531                 } else
1532                     sayNO;
1533             }
1534           say_yes:
1535             if (logical) {
1536                 logical = 0;
1537                 sw = 1;
1538             }
1539             if (OP(scan) == SUSPEND) {
1540                 locinput = reginput;
1541                 nextchr = UCHARAT(locinput);
1542             }
1543             /* FALL THROUGH. */
1544         case LONGJMP:
1545           do_longjump:
1546             next = scan + ARG(scan);
1547             if (next == scan)
1548                 next = NULL;
1549             break;
1550         default:
1551             PerlIO_printf(PerlIO_stderr(), "%lx %d\n",
1552                           (unsigned long)scan, OP(scan));
1553             FAIL("regexp memory corruption");
1554         }
1555         scan = next;
1556     }
1557
1558     /*
1559     * We get here only if there's trouble -- normally "case END" is
1560     * the terminating point.
1561     */
1562     FAIL("corrupted regexp pointers");
1563     /*NOTREACHED*/
1564     sayNO;
1565
1566 yes:
1567 #ifdef DEBUGGING
1568     regindent--;
1569 #endif
1570     return 1;
1571
1572 no:
1573 #ifdef DEBUGGING
1574     regindent--;
1575 #endif
1576     return 0;
1577 }
1578
1579 /*
1580  - regrepeat - repeatedly match something simple, report how many
1581  */
1582 /*
1583  * [This routine now assumes that it will only match on things of length 1.
1584  * That was true before, but now we assume scan - reginput is the count,
1585  * rather than incrementing count on every character.]
1586  */
1587 STATIC I32
1588 regrepeat(regnode *p, I32 max)
1589 {
1590     register char *scan;
1591     register char *opnd;
1592     register I32 c;
1593     register char *loceol = regeol;
1594
1595     scan = reginput;
1596     if (max != REG_INFTY && max < loceol - scan)
1597       loceol = scan + max;
1598     opnd = (char *) OPERAND(p);
1599     switch (OP(p)) {
1600     case ANY:
1601         while (scan < loceol && *scan != '\n')
1602             scan++;
1603         break;
1604     case SANY:
1605         scan = loceol;
1606         break;
1607     case EXACT:         /* length of string is 1 */
1608         c = UCHARAT(++opnd);
1609         while (scan < loceol && UCHARAT(scan) == c)
1610             scan++;
1611         break;
1612     case EXACTF:        /* length of string is 1 */
1613         c = UCHARAT(++opnd);
1614         while (scan < loceol &&
1615                (UCHARAT(scan) == c || UCHARAT(scan) == fold[c]))
1616             scan++;
1617         break;
1618     case EXACTFL:       /* length of string is 1 */
1619         reg_flags |= RF_tainted;
1620         c = UCHARAT(++opnd);
1621         while (scan < loceol &&
1622                (UCHARAT(scan) == c || UCHARAT(scan) == fold_locale[c]))
1623             scan++;
1624         break;
1625     case ANYOF:
1626         while (scan < loceol && reginclass(opnd, *scan))
1627             scan++;
1628         break;
1629     case ALNUM:
1630         while (scan < loceol && isALNUM(*scan))
1631             scan++;
1632         break;
1633     case ALNUML:
1634         reg_flags |= RF_tainted;
1635         while (scan < loceol && isALNUM_LC(*scan))
1636             scan++;
1637         break;
1638     case NALNUM:
1639         while (scan < loceol && !isALNUM(*scan))
1640             scan++;
1641         break;
1642     case NALNUML:
1643         reg_flags |= RF_tainted;
1644         while (scan < loceol && !isALNUM_LC(*scan))
1645             scan++;
1646         break;
1647     case SPACE:
1648         while (scan < loceol && isSPACE(*scan))
1649             scan++;
1650         break;
1651     case SPACEL:
1652         reg_flags |= RF_tainted;
1653         while (scan < loceol && isSPACE_LC(*scan))
1654             scan++;
1655         break;
1656     case NSPACE:
1657         while (scan < loceol && !isSPACE(*scan))
1658             scan++;
1659         break;
1660     case NSPACEL:
1661         reg_flags |= RF_tainted;
1662         while (scan < loceol && !isSPACE_LC(*scan))
1663             scan++;
1664         break;
1665     case DIGIT:
1666         while (scan < loceol && isDIGIT(*scan))
1667             scan++;
1668         break;
1669     case NDIGIT:
1670         while (scan < loceol && !isDIGIT(*scan))
1671             scan++;
1672         break;
1673     default:            /* Called on something of 0 width. */
1674         break;          /* So match right here or not at all. */
1675     }
1676
1677     c = scan - reginput;
1678     reginput = scan;
1679
1680     DEBUG_r( 
1681         {
1682                 SV *prop = sv_newmortal();
1683
1684                 regprop(prop, p);
1685                 PerlIO_printf(Perl_debug_log, 
1686                               "%*s  %s can match %ld times out of %ld...\n", 
1687                               REPORT_CODE_OFF+1, "", SvPVX(prop),c,max);
1688         });
1689     
1690     return(c);
1691 }
1692
1693 /*
1694  - regrepeat_hard - repeatedly match something, report total lenth and length
1695  * 
1696  * The repeater is supposed to have constant length.
1697  */
1698
1699 STATIC I32
1700 regrepeat_hard(regnode *p, I32 max, I32 *lp)
1701 {
1702     register char *scan;
1703     register char *start;
1704     register char *loceol = regeol;
1705     I32 l = -1;
1706
1707     start = reginput;
1708     while (reginput < loceol && (scan = reginput, regmatch(p))) {
1709         if (l == -1) {
1710             *lp = l = reginput - start;
1711             if (max != REG_INFTY && l*max < loceol - scan)
1712                 loceol = scan + l*max;
1713             if (l == 0) {
1714                 return max;
1715             }
1716         }
1717     }
1718     if (reginput < loceol)
1719         reginput = scan;
1720     else
1721         scan = reginput;
1722     
1723     return (scan - start)/l;
1724 }
1725
1726 /*
1727  - regclass - determine if a character falls into a character class
1728  */
1729
1730 STATIC bool
1731 reginclass(register char *p, register I32 c)
1732 {
1733     char flags = *p;
1734     bool match = FALSE;
1735
1736     c &= 0xFF;
1737     if (p[1 + (c >> 3)] & (1 << (c & 7)))
1738         match = TRUE;
1739     else if (flags & ANYOF_FOLD) {
1740         I32 cf;
1741         if (flags & ANYOF_LOCALE) {
1742             reg_flags |= RF_tainted;
1743             cf = fold_locale[c];
1744         }
1745         else
1746             cf = fold[c];
1747         if (p[1 + (cf >> 3)] & (1 << (cf & 7)))
1748             match = TRUE;
1749     }
1750
1751     if (!match && (flags & ANYOF_ISA)) {
1752         reg_flags |= RF_tainted;
1753
1754         if (((flags & ANYOF_ALNUML)  && isALNUM_LC(c))  ||
1755             ((flags & ANYOF_NALNUML) && !isALNUM_LC(c)) ||
1756             ((flags & ANYOF_SPACEL)  && isSPACE_LC(c))  ||
1757             ((flags & ANYOF_NSPACEL) && !isSPACE_LC(c)))
1758         {
1759             match = TRUE;
1760         }
1761     }
1762
1763     return match ^ ((flags & ANYOF_INVERT) != 0);
1764 }
1765
1766
1767