46833c2f557fc1121f31b97f1ea13be4caadb007
[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 #ifdef PERL_EXT_RE_BUILD
23 /* need to replace pregcomp et al, so enable that */
24 #  ifndef PERL_IN_XSUB_RE
25 #    define PERL_IN_XSUB_RE
26 #  endif
27 /* need access to debugger hooks */
28 #  ifndef DEBUGGING
29 #    define DEBUGGING
30 #  endif
31 #endif
32
33 #ifdef PERL_IN_XSUB_RE
34 /* We *really* need to overwrite these symbols: */
35 #  define Perl_regexec_flags my_regexec
36 #  define Perl_regdump my_regdump
37 #  define Perl_regprop my_regprop
38 /* *These* symbols are masked to allow static link. */
39 #  define Perl_pregexec my_pregexec
40 #endif 
41
42 /*SUPPRESS 112*/
43 /*
44  * pregcomp and pregexec -- regsub and regerror are not used in perl
45  *
46  *      Copyright (c) 1986 by University of Toronto.
47  *      Written by Henry Spencer.  Not derived from licensed software.
48  *
49  *      Permission is granted to anyone to use this software for any
50  *      purpose on any computer system, and to redistribute it freely,
51  *      subject to the following restrictions:
52  *
53  *      1. The author is not responsible for the consequences of use of
54  *              this software, no matter how awful, even if they arise
55  *              from defects in it.
56  *
57  *      2. The origin of this software must not be misrepresented, either
58  *              by explicit claim or by omission.
59  *
60  *      3. Altered versions must be plainly marked as such, and must not
61  *              be misrepresented as being the original software.
62  *
63  ****    Alterations to Henry's code are...
64  ****
65  ****    Copyright (c) 1991-1998, Larry Wall
66  ****
67  ****    You may distribute under the terms of either the GNU General Public
68  ****    License or the Artistic License, as specified in the README file.
69  *
70  * Beware that some of this code is subtly aware of the way operator
71  * precedence is structured in regular expressions.  Serious changes in
72  * regular-expression syntax might require a total rethink.
73  */
74 #include "EXTERN.h"
75 #include "perl.h"
76
77 #include "regcomp.h"
78
79 #define RF_tainted      1               /* tainted information used? */
80 #define RF_warned       2               /* warned about big count? */
81 #define RF_evaled       4               /* Did an EVAL with setting? */
82 #define RF_utf8         8               /* String contains multibyte chars? */
83
84 #define UTF (PL_reg_flags & RF_utf8)
85
86 #define RS_init         1               /* eval environment created */
87 #define RS_set          2               /* replsv value is set */
88
89 #ifndef STATIC
90 #define STATIC  static
91 #endif
92
93 #ifndef PERL_OBJECT
94 typedef I32 CHECKPOINT;
95
96 /*
97  * Forwards.
98  */
99
100 static I32 regmatch _((regnode *prog));
101 static I32 regrepeat _((regnode *p, I32 max));
102 static I32 regrepeat_hard _((regnode *p, I32 max, I32 *lp));
103 static I32 regtry _((regexp *prog, char *startpos));
104
105 static bool reginclass _((char *p, I32 c));
106 static bool reginclassutf8 _((regnode *f, U8* p));
107 static CHECKPOINT regcppush _((I32 parenfloor));
108 static char * regcppop _((void));
109 static char * regcp_set_to _((I32 ss));
110 static void cache_re _((regexp *prog));
111 #endif
112
113 #define REGINCLASS(p,c)  (*(p) ? reginclass(p,c) : ANYOF_TEST(p,c))
114 #define REGINCLASSUTF8(f,p)  (ARG1(f) ? reginclassutf8(f,p) : swash_fetch((SV*)PL_regdata->data[ARG2(f)],p))
115
116 #define CHR_SVLEN(sv) (UTF ? sv_len_utf8(sv) : SvCUR(sv))
117 #define CHR_DIST(a,b) (UTF ? utf8_distance(a,b) : a - b)
118
119 #ifndef PERL_OBJECT
120 static U8 * reghop _((U8 *pos, I32 off));
121 static U8 * reghopmaybe _((U8 *pos, I32 off));
122 #endif
123 #define reghop_c(pos,off) ((char*)reghop((U8*)pos, off))
124 #define reghopmaybe_c(pos,off) ((char*)reghopmaybe((U8*)pos, off))
125 #define HOP(pos,off) (UTF ? reghop((U8*)pos, off) : (U8*)(pos + off))
126 #define HOPMAYBE(pos,off) (UTF ? reghopmaybe((U8*)pos, off) : (U8*)(pos + off))
127 #define HOPc(pos,off) ((char*)HOP(pos,off))
128 #define HOPMAYBEc(pos,off) ((char*)HOPMAYBE(pos,off))
129
130 STATIC CHECKPOINT
131 regcppush(I32 parenfloor)
132 {
133     dTHR;
134     int retval = PL_savestack_ix;
135     int i = (PL_regsize - parenfloor) * 4;
136     int p;
137
138     SSCHECK(i + 5);
139     for (p = PL_regsize; p > parenfloor; p--) {
140         SSPUSHPTR(PL_regendp[p]);
141         SSPUSHPTR(PL_regstartp[p]);
142         SSPUSHPTR(PL_reg_start_tmp[p]);
143         SSPUSHINT(p);
144     }
145     SSPUSHINT(PL_regsize);
146     SSPUSHINT(*PL_reglastparen);
147     SSPUSHPTR(PL_reginput);
148     SSPUSHINT(i + 3);
149     SSPUSHINT(SAVEt_REGCONTEXT);
150     return retval;
151 }
152
153 /* These are needed since we do not localize EVAL nodes: */
154 #  define REGCP_SET  DEBUG_r(PerlIO_printf(Perl_debug_log,              \
155                              "  Setting an EVAL scope, savestack=%i\n", \
156                              PL_savestack_ix)); lastcp = PL_savestack_ix
157
158 #  define REGCP_UNWIND  DEBUG_r(lastcp != PL_savestack_ix ?             \
159                                 PerlIO_printf(Perl_debug_log,           \
160                                 "  Clearing an EVAL scope, savestack=%i..%i\n", \
161                                 lastcp, PL_savestack_ix) : 0); regcpblow(lastcp)
162
163 STATIC char *
164 regcppop(void)
165 {
166     dTHR;
167     I32 i = SSPOPINT;
168     U32 paren = 0;
169     char *input;
170     char *tmps;
171     assert(i == SAVEt_REGCONTEXT);
172     i = SSPOPINT;
173     input = (char *) SSPOPPTR;
174     *PL_reglastparen = SSPOPINT;
175     PL_regsize = SSPOPINT;
176     for (i -= 3; i > 0; i -= 4) {
177         paren = (U32)SSPOPINT;
178         PL_reg_start_tmp[paren] = (char *) SSPOPPTR;
179         PL_regstartp[paren] = (char *) SSPOPPTR;
180         tmps = (char*)SSPOPPTR;
181         if (paren <= *PL_reglastparen)
182             PL_regendp[paren] = tmps;
183         DEBUG_r(
184             PerlIO_printf(Perl_debug_log,
185                           "     restoring \\%d to %d(%d)..%d%s\n",
186                           paren, PL_regstartp[paren] - PL_regbol, 
187                           PL_reg_start_tmp[paren] - PL_regbol,
188                           PL_regendp[paren] - PL_regbol, 
189                           (paren > *PL_reglastparen ? "(no)" : ""));
190         );
191     }
192     DEBUG_r(
193         if (*PL_reglastparen + 1 <= PL_regnpar) {
194             PerlIO_printf(Perl_debug_log,
195                           "     restoring \\%d..\\%d to undef\n",
196                           *PL_reglastparen + 1, PL_regnpar);
197         }
198     );
199     for (paren = *PL_reglastparen + 1; paren <= PL_regnpar; paren++) {
200         if (paren > PL_regsize)
201             PL_regstartp[paren] = Nullch;
202         PL_regendp[paren] = Nullch;
203     }
204     return input;
205 }
206
207 STATIC char *
208 regcp_set_to(I32 ss)
209 {
210     dTHR;
211     I32 tmp = PL_savestack_ix;
212
213     PL_savestack_ix = ss;
214     regcppop();
215     PL_savestack_ix = tmp;
216     return Nullch;
217 }
218
219 typedef struct re_cc_state
220 {
221     I32 ss;
222     regnode *node;
223     struct re_cc_state *prev;
224     CURCUR *cc;
225     regexp *re;
226 } re_cc_state;
227
228 #define regcpblow(cp) LEAVE_SCOPE(cp)
229
230 /*
231  * pregexec and friends
232  */
233
234 /*
235  - pregexec - match a regexp against a string
236  */
237 I32
238 pregexec(register regexp *prog, char *stringarg, register char *strend,
239          char *strbeg, I32 minend, SV *screamer, U32 nosave)
240 /* strend: pointer to null at end of string */
241 /* strbeg: real beginning of string */
242 /* minend: end of match must be >=minend after stringarg. */
243 /* nosave: For optimizations. */
244 {
245     return
246         regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL, 
247                       nosave ? 0 : REXEC_COPY_STR);
248 }
249
250 STATIC void
251 cache_re(regexp *prog)
252 {
253     dTHR;
254     PL_regprecomp = prog->precomp;              /* Needed for FAIL. */
255 #ifdef DEBUGGING
256     PL_regprogram = prog->program;
257 #endif
258     PL_regnpar = prog->nparens;
259     PL_regdata = prog->data;    
260     PL_reg_re = prog;    
261 }
262
263 /*
264  - regexec_flags - match a regexp against a string
265  */
266 I32
267 regexec_flags(register regexp *prog, char *stringarg, register char *strend,
268               char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
269 /* strend: pointer to null at end of string */
270 /* strbeg: real beginning of string */
271 /* minend: end of match must be >=minend after stringarg. */
272 /* data: May be used for some additional optimizations. */
273 /* nosave: For optimizations. */
274 {
275     dTHR;
276     register char *s;
277     register regnode *c;
278     register char *startpos = stringarg;
279     register I32 tmp;
280     I32 minlen;         /* must match at least this many chars */
281     I32 dontbother = 0; /* how many characters not to try at end */
282     CURCUR cc;
283     I32 start_shift = 0;                /* Offset of the start to find
284                                          constant substr. */            /* CC */
285     I32 end_shift = 0;                  /* Same for the end. */         /* CC */
286     I32 scream_pos = -1;                /* Internal iterator of scream. */
287     char *scream_olds;
288     SV* oreplsv = GvSV(PL_replgv);
289
290     cc.cur = 0;
291     cc.oldcc = 0;
292     PL_regcc = &cc;
293
294     cache_re(prog);
295 #ifdef DEBUGGING
296     PL_regnarrate = PL_debug & 512;
297 #endif
298
299     /* Be paranoid... */
300     if (prog == NULL || startpos == NULL) {
301         croak("NULL regexp parameter");
302         return 0;
303     }
304
305     minlen = prog->minlen;
306     if (strend - startpos < minlen) goto phooey;
307
308     if (startpos == strbeg)     /* is ^ valid at stringarg? */
309         PL_regprev = '\n';
310     else {
311         PL_regprev = (U32)stringarg[-1];
312         if (!PL_multiline && PL_regprev == '\n')
313             PL_regprev = '\0';          /* force ^ to NOT match */
314     }
315
316     /* Check validity of program. */
317     if (UCHARAT(prog->program) != REG_MAGIC) {
318         FAIL("corrupted regexp program");
319     }
320
321     PL_reg_flags = 0;
322     PL_reg_eval_set = 0;
323
324     if (prog->reganch & ROPT_UTF8)
325         PL_reg_flags |= RF_utf8;
326
327     /* Mark beginning of line for ^ and lookbehind. */
328     PL_regbol = startpos;
329     PL_bostr  = strbeg;
330
331     /* Mark end of line for $ (and such) */
332     PL_regeol = strend;
333
334     /* see how far we have to get to not match where we matched before */
335     PL_regtill = startpos+minend;
336
337     /* We start without call_cc context.  */
338     PL_reg_call_cc = 0;
339
340     /* If there is a "must appear" string, look for it. */
341     s = startpos;
342     if (!(flags & REXEC_CHECKED) 
343         && prog->check_substr != Nullsv &&
344         !(prog->reganch & ROPT_ANCH_GPOS) &&
345         (!(prog->reganch & (ROPT_ANCH_BOL | ROPT_ANCH_MBOL))
346          || (PL_multiline && prog->check_substr == prog->anchored_substr)) )
347     {
348         char *t;
349         start_shift = prog->check_offset_min;   /* okay to underestimate on CC */
350         /* Should be nonnegative! */
351         end_shift = minlen - start_shift - CHR_SVLEN(prog->check_substr);
352         if (flags & REXEC_SCREAM) {
353             if (PL_screamfirst[BmRARE(prog->check_substr)] >= 0)
354                     s = screaminstr(sv, prog->check_substr, 
355                                     start_shift + (stringarg - strbeg),
356                                     end_shift, &scream_pos, 0);
357             else
358                     s = Nullch;
359             scream_olds = s;
360         }
361         else
362             s = fbm_instr((unsigned char*)s + start_shift,
363                           (unsigned char*)strend - end_shift,
364                 prog->check_substr, 0);
365         if (!s) {
366             ++BmUSEFUL(prog->check_substr);     /* hooray */
367             goto phooey;        /* not present */
368         }
369         else if (s - stringarg > prog->check_offset_max &&
370                  (UTF 
371                     ? ((t = reghopmaybe_c(s, -(prog->check_offset_max))) && t >= stringarg)
372                     : (t = s - prog->check_offset_max) != 0
373                  )
374                 )
375         {
376             ++BmUSEFUL(prog->check_substr);     /* hooray/2 */
377             s = t;
378         }
379         else if (!(prog->reganch & ROPT_NAUGHTY)
380                    && --BmUSEFUL(prog->check_substr) < 0
381                    && prog->check_substr == prog->float_substr) { /* boo */
382             SvREFCNT_dec(prog->check_substr);
383             prog->check_substr = Nullsv;        /* disable */
384             prog->float_substr = Nullsv;        /* clear */
385             s = startpos;
386         }
387         else
388             s = startpos;
389     }
390
391     DEBUG_r(
392         PerlIO_printf(Perl_debug_log, 
393                       "%sMatching%s `%s%.60s%s%s' against `%s%.*s%s%s'\n",
394                       PL_colors[4],PL_colors[5],PL_colors[0],
395                       prog->precomp,
396                       PL_colors[1],
397                       (strlen(prog->precomp) > 60 ? "..." : ""),
398                       PL_colors[0], 
399                       (strend - startpos > 60 ? 60 : strend - startpos),
400                       startpos, PL_colors[1],
401                       (strend - startpos > 60 ? "..." : ""))
402         );
403
404     if (prog->reganch & ROPT_GPOS_SEEN) {
405         MAGIC *mg;
406         int pos = 0;
407
408         if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv) 
409             && (mg = mg_find(sv, 'g')) && mg->mg_len >= 0)
410             pos = mg->mg_len;
411         PL_reg_ganch = startpos + pos;
412     }
413
414     /* Simplest case:  anchored match need be tried only once. */
415     /*  [unless only anchor is BOL and multiline is set] */
416     if (prog->reganch & (ROPT_ANCH & ~ROPT_ANCH_GPOS)) {
417         if (regtry(prog, startpos))
418             goto got_it;
419         else if (PL_multiline || (prog->reganch & ROPT_IMPLICIT)
420                  || (prog->reganch & ROPT_ANCH_MBOL)) /* XXXX SBOL? */
421         {
422             if (minlen)
423                 dontbother = minlen - 1;
424             strend = HOPc(strend, -dontbother);
425             /* for multiline we only have to try after newlines */
426             if (s > startpos)
427                 s--;
428             while (s < strend) {
429                 if (*s++ == '\n') {     /* don't need PL_utf8skip here */
430                     if (s < strend && regtry(prog, s))
431                         goto got_it;
432                 }
433             }
434         }
435         goto phooey;
436     } else if (prog->reganch & ROPT_ANCH_GPOS) {
437         if (regtry(prog, PL_reg_ganch))
438             goto got_it;
439         goto phooey;
440     }
441
442     /* Messy cases:  unanchored match. */
443     if (prog->anchored_substr && prog->reganch & ROPT_SKIP) { 
444         /* we have /x+whatever/ */
445         /* it must be a one character string */
446         char ch = SvPVX(prog->anchored_substr)[0];
447         if (UTF) {
448             while (s < strend) {
449                 if (*s == ch) {
450                     if (regtry(prog, s)) goto got_it;
451                     s += UTF8SKIP(s);
452                     while (s < strend && *s == ch)
453                         s += UTF8SKIP(s);
454                 }
455                 s += UTF8SKIP(s);
456             }
457         }
458         else {
459             while (s < strend) {
460                 if (*s == ch) {
461                     if (regtry(prog, s)) goto got_it;
462                     s++;
463                     while (s < strend && *s == ch)
464                         s++;
465                 }
466                 s++;
467             }
468         }
469     }
470     /*SUPPRESS 560*/
471     else if (prog->anchored_substr != Nullsv
472              || (prog->float_substr != Nullsv 
473                  && prog->float_max_offset < strend - s)) {
474         SV *must = prog->anchored_substr 
475             ? prog->anchored_substr : prog->float_substr;
476         I32 back_max = 
477             prog->anchored_substr ? prog->anchored_offset : prog->float_max_offset;
478         I32 back_min = 
479             prog->anchored_substr ? prog->anchored_offset : prog->float_min_offset;
480         I32 delta = back_max - back_min;
481         char *last = HOPc(strend, 0-(CHR_SVLEN(must) + back_min)); /* Cannot start after this */
482         char *last1;            /* Last position checked before */
483
484         if (s > PL_bostr)
485             last1 = HOPc(s, -1);
486         else
487             last1 = s - 1;      /* bogus */
488
489         /* XXXX check_substr already used to find `s', can optimize if
490            check_substr==must. */
491         scream_pos = -1;
492         dontbother = end_shift;
493         strend = HOPc(strend, -dontbother);
494         while ( (s <= last) &&
495                 ((flags & REXEC_SCREAM) 
496                  ? (s = screaminstr(sv, must, HOPc(s, back_min) - strbeg,
497                                     end_shift, &scream_pos, 0))
498                  : (s = fbm_instr((unsigned char*)HOP(s, back_min),
499                                   (unsigned char*)strend, must, 0))) ) {
500             if (HOPc(s, -back_max) > last1) {
501                 last1 = HOPc(s, -back_min);
502                 s = HOPc(s, -back_max);
503             }
504             else {
505                 char *t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
506
507                 last1 = HOPc(s, -back_min);
508                 s = t;          
509             }
510             if (UTF) {
511                 while (s <= last1) {
512                     if (regtry(prog, s))
513                         goto got_it;
514                     s += UTF8SKIP(s);
515                 }
516             }
517             else {
518                 while (s <= last1) {
519                     if (regtry(prog, s))
520                         goto got_it;
521                     s++;
522                 }
523             }
524         }
525         goto phooey;
526     }
527     else if (c = prog->regstclass) {
528         I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
529         char *cc;
530
531         if (minlen)
532             dontbother = minlen - 1;
533         strend = HOPc(strend, -dontbother);     /* don't bother with what can't match */
534         tmp = 1;
535         /* We know what class it must start with. */
536         switch (OP(c)) {
537         case ANYOFUTF8:
538             cc = (char *) OPERAND(c);
539             while (s < strend) {
540                 if (REGINCLASSUTF8(c, (U8*)s)) {
541                     if (tmp && regtry(prog, s))
542                         goto got_it;
543                     else
544                         tmp = doevery;
545                 }
546                 else
547                     tmp = 1;
548                 s += UTF8SKIP(s);
549             }
550             break;
551         case ANYOF:
552             cc = (char *) OPERAND(c);
553             while (s < strend) {
554                 if (REGINCLASS(cc, *s)) {
555                     if (tmp && regtry(prog, s))
556                         goto got_it;
557                     else
558                         tmp = doevery;
559                 }
560                 else
561                     tmp = 1;
562                 s++;
563             }
564             break;
565         case BOUNDL:
566             PL_reg_flags |= RF_tainted;
567             /* FALL THROUGH */
568         case BOUND:
569             if (minlen) {
570                 dontbother++;
571                 strend -= 1;
572             }
573             tmp = (s != startpos) ? UCHARAT(s - 1) : PL_regprev;
574             tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
575             while (s < strend) {
576                 if (tmp == !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
577                     tmp = !tmp;
578                     if (regtry(prog, s))
579                         goto got_it;
580                 }
581                 s++;
582             }
583             if ((minlen || tmp) && regtry(prog,s))
584                 goto got_it;
585             break;
586         case BOUNDLUTF8:
587             PL_reg_flags |= RF_tainted;
588             /* FALL THROUGH */
589         case BOUNDUTF8:
590             if (minlen) {
591                 dontbother++;
592                 strend = reghop_c(strend, -1);
593             }
594             tmp = (I32)(s != startpos) ? utf8_to_uv(reghop((U8*)s, -1), 0) : PL_regprev;
595             tmp = ((OP(c) == BOUND ? isALNUM_uni(tmp) : isALNUM_LC_uni(tmp)) != 0);
596             while (s < strend) {
597                 if (tmp == !(OP(c) == BOUND ?
598                              swash_fetch(PL_utf8_alnum, (U8*)s) :
599                              isALNUM_LC_utf8((U8*)s)))
600                 {
601                     tmp = !tmp;
602                     if (regtry(prog, s))
603                         goto got_it;
604                 }
605                 s += UTF8SKIP(s);
606             }
607             if ((minlen || tmp) && regtry(prog,s))
608                 goto got_it;
609             break;
610         case NBOUNDL:
611             PL_reg_flags |= RF_tainted;
612             /* FALL THROUGH */
613         case NBOUND:
614             if (minlen) {
615                 dontbother++;
616                 strend -= 1;
617             }
618             tmp = (s != startpos) ? UCHARAT(s - 1) : PL_regprev;
619             tmp = ((OP(c) == NBOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
620             while (s < strend) {
621                 if (tmp == !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
622                     tmp = !tmp;
623                 else if (regtry(prog, s))
624                     goto got_it;
625                 s++;
626             }
627             if ((minlen || !tmp) && regtry(prog,s))
628                 goto got_it;
629             break;
630         case NBOUNDLUTF8:
631             PL_reg_flags |= RF_tainted;
632             /* FALL THROUGH */
633         case NBOUNDUTF8:
634             if (minlen) {
635                 dontbother++;
636                 strend = reghop_c(strend, -1);
637             }
638             tmp = (I32)(s != startpos) ? utf8_to_uv(reghop((U8*)s, -1), 0) : PL_regprev;
639             tmp = ((OP(c) == NBOUND ? isALNUM_uni(tmp) : isALNUM_LC_uni(tmp)) != 0);
640             while (s < strend) {
641                 if (tmp == !(OP(c) == NBOUND ?
642                              swash_fetch(PL_utf8_alnum, (U8*)s) :
643                              isALNUM_LC_utf8((U8*)s)))
644                     tmp = !tmp;
645                 else if (regtry(prog, s))
646                     goto got_it;
647                 s += UTF8SKIP(s);
648             }
649             if ((minlen || !tmp) && regtry(prog,s))
650                 goto got_it;
651             break;
652         case ALNUM:
653             while (s < strend) {
654                 if (isALNUM(*s)) {
655                     if (tmp && regtry(prog, s))
656                         goto got_it;
657                     else
658                         tmp = doevery;
659                 }
660                 else
661                     tmp = 1;
662                 s++;
663             }
664             break;
665         case ALNUMUTF8:
666             while (s < strend) {
667                 if (swash_fetch(PL_utf8_alnum, (U8*)s)) {
668                     if (tmp && regtry(prog, s))
669                         goto got_it;
670                     else
671                         tmp = doevery;
672                 }
673                 else
674                     tmp = 1;
675                 s += UTF8SKIP(s);
676             }
677             break;
678         case ALNUML:
679             PL_reg_flags |= RF_tainted;
680             while (s < strend) {
681                 if (isALNUM_LC(*s)) {
682                     if (tmp && regtry(prog, s))
683                         goto got_it;
684                     else
685                         tmp = doevery;
686                 }
687                 else
688                     tmp = 1;
689                 s++;
690             }
691             break;
692         case ALNUMLUTF8:
693             PL_reg_flags |= RF_tainted;
694             while (s < strend) {
695                 if (isALNUM_LC_utf8((U8*)s)) {
696                     if (tmp && regtry(prog, s))
697                         goto got_it;
698                     else
699                         tmp = doevery;
700                 }
701                 else
702                     tmp = 1;
703                 s += UTF8SKIP(s);
704             }
705             break;
706         case NALNUM:
707             while (s < strend) {
708                 if (!isALNUM(*s)) {
709                     if (tmp && regtry(prog, s))
710                         goto got_it;
711                     else
712                         tmp = doevery;
713                 }
714                 else
715                     tmp = 1;
716                 s++;
717             }
718             break;
719         case NALNUMUTF8:
720             while (s < strend) {
721                 if (!swash_fetch(PL_utf8_alnum, (U8*)s)) {
722                     if (tmp && regtry(prog, s))
723                         goto got_it;
724                     else
725                         tmp = doevery;
726                 }
727                 else
728                     tmp = 1;
729                 s += UTF8SKIP(s);
730             }
731             break;
732         case NALNUML:
733             PL_reg_flags |= RF_tainted;
734             while (s < strend) {
735                 if (!isALNUM_LC(*s)) {
736                     if (tmp && regtry(prog, s))
737                         goto got_it;
738                     else
739                         tmp = doevery;
740                 }
741                 else
742                     tmp = 1;
743                 s++;
744             }
745             break;
746         case NALNUMLUTF8:
747             PL_reg_flags |= RF_tainted;
748             while (s < strend) {
749                 if (!isALNUM_LC_utf8((U8*)s)) {
750                     if (tmp && regtry(prog, s))
751                         goto got_it;
752                     else
753                         tmp = doevery;
754                 }
755                 else
756                     tmp = 1;
757                 s += UTF8SKIP(s);
758             }
759             break;
760         case SPACE:
761             while (s < strend) {
762                 if (isSPACE(*s)) {
763                     if (tmp && regtry(prog, s))
764                         goto got_it;
765                     else
766                         tmp = doevery;
767                 }
768                 else
769                     tmp = 1;
770                 s++;
771             }
772             break;
773         case SPACEUTF8:
774             while (s < strend) {
775                 if (*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s)) {
776                     if (tmp && regtry(prog, s))
777                         goto got_it;
778                     else
779                         tmp = doevery;
780                 }
781                 else
782                     tmp = 1;
783                 s += UTF8SKIP(s);
784             }
785             break;
786         case SPACEL:
787             PL_reg_flags |= RF_tainted;
788             while (s < strend) {
789                 if (isSPACE_LC(*s)) {
790                     if (tmp && regtry(prog, s))
791                         goto got_it;
792                     else
793                         tmp = doevery;
794                 }
795                 else
796                     tmp = 1;
797                 s++;
798             }
799             break;
800         case SPACELUTF8:
801             PL_reg_flags |= RF_tainted;
802             while (s < strend) {
803                 if (*s == ' ' || isSPACE_LC_utf8((U8*)s)) {
804                     if (tmp && regtry(prog, s))
805                         goto got_it;
806                     else
807                         tmp = doevery;
808                 }
809                 else
810                     tmp = 1;
811                 s += UTF8SKIP(s);
812             }
813             break;
814         case NSPACE:
815             while (s < strend) {
816                 if (!isSPACE(*s)) {
817                     if (tmp && regtry(prog, s))
818                         goto got_it;
819                     else
820                         tmp = doevery;
821                 }
822                 else
823                     tmp = 1;
824                 s++;
825             }
826             break;
827         case NSPACEUTF8:
828             while (s < strend) {
829                 if (!(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s))) {
830                     if (tmp && regtry(prog, s))
831                         goto got_it;
832                     else
833                         tmp = doevery;
834                 }
835                 else
836                     tmp = 1;
837                 s += UTF8SKIP(s);
838             }
839             break;
840         case NSPACEL:
841             PL_reg_flags |= RF_tainted;
842             while (s < strend) {
843                 if (!isSPACE_LC(*s)) {
844                     if (tmp && regtry(prog, s))
845                         goto got_it;
846                     else
847                         tmp = doevery;
848                 }
849                 else
850                     tmp = 1;
851                 s++;
852             }
853             break;
854         case NSPACELUTF8:
855             PL_reg_flags |= RF_tainted;
856             while (s < strend) {
857                 if (!(*s == ' ' || isSPACE_LC_utf8((U8*)s))) {
858                     if (tmp && regtry(prog, s))
859                         goto got_it;
860                     else
861                         tmp = doevery;
862                 }
863                 else
864                     tmp = 1;
865                 s += UTF8SKIP(s);
866             }
867             break;
868         case DIGIT:
869             while (s < strend) {
870                 if (isDIGIT(*s)) {
871                     if (tmp && regtry(prog, s))
872                         goto got_it;
873                     else
874                         tmp = doevery;
875                 }
876                 else
877                     tmp = 1;
878                 s++;
879             }
880             break;
881         case DIGITUTF8:
882             while (s < strend) {
883                 if (swash_fetch(PL_utf8_digit,(U8*)s)) {
884                     if (tmp && regtry(prog, s))
885                         goto got_it;
886                     else
887                         tmp = doevery;
888                 }
889                 else
890                     tmp = 1;
891                 s += UTF8SKIP(s);
892             }
893             break;
894         case NDIGIT:
895             while (s < strend) {
896                 if (!isDIGIT(*s)) {
897                     if (tmp && regtry(prog, s))
898                         goto got_it;
899                     else
900                         tmp = doevery;
901                 }
902                 else
903                     tmp = 1;
904                 s++;
905             }
906             break;
907         case NDIGITUTF8:
908             while (s < strend) {
909                 if (!swash_fetch(PL_utf8_digit,(U8*)s)) {
910                     if (tmp && regtry(prog, s))
911                         goto got_it;
912                     else
913                         tmp = doevery;
914                 }
915                 else
916                     tmp = 1;
917                 s += UTF8SKIP(s);
918             }
919             break;
920         }
921     }
922     else {
923         dontbother = 0;
924         if (prog->float_substr != Nullsv) {     /* Trim the end. */
925             char *last;
926             I32 oldpos = scream_pos;
927
928             if (flags & REXEC_SCREAM) {
929                 last = screaminstr(sv, prog->float_substr, s - strbeg,
930                                    end_shift, &scream_pos, 1); /* last one */
931                 if (!last) {
932                     last = scream_olds; /* Only one occurence. */
933                 }
934             }
935             else {
936                 STRLEN len;
937                 char *little = SvPV(prog->float_substr, len);
938                 if (len) 
939                     last = rninstr(s, strend, little, little + len);
940                 else
941                     last = strend;      /* matching `$' */
942             }
943             if (last == NULL) goto phooey; /* Should not happen! */
944             dontbother = strend - last + prog->float_min_offset;
945         }
946         if (minlen && (dontbother < minlen))
947             dontbother = minlen - 1;
948         strend -= dontbother;              /* this one's always in bytes! */
949         /* We don't know much -- general case. */
950         if (UTF) {
951             for (;;) {
952                 if (regtry(prog, s))
953                     goto got_it;
954                 if (s >= strend)
955                     break;
956                 s += UTF8SKIP(s);
957             };
958         }
959         else {
960             do {
961                 if (regtry(prog, s))
962                     goto got_it;
963             } while (s++ < strend);
964         }
965     }
966
967     /* Failure. */
968     goto phooey;
969
970 got_it:
971     prog->subbeg = strbeg;
972     prog->subend = PL_regeol;   /* strend may have been modified */
973     RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted);
974
975     /* make sure $`, $&, $', and $digit will work later */
976     if (strbeg != prog->subbase) {      /* second+ //g match.  */
977         if (!(flags & REXEC_COPY_STR)) {
978             if (prog->subbase) {
979                 Safefree(prog->subbase);
980                 prog->subbase = Nullch;
981             }
982         }
983         else {
984             I32 i = PL_regeol - startpos + (stringarg - strbeg);
985             s = savepvn(strbeg, i);
986             Safefree(prog->subbase);
987             prog->subbase = s;
988             prog->subbeg = prog->subbase;
989             prog->subend = prog->subbase + i;
990             s = prog->subbase + (stringarg - strbeg);
991             for (i = 0; i <= prog->nparens; i++) {
992                 if (prog->endp[i]) {
993                     prog->startp[i] = s + (prog->startp[i] - startpos);
994                     prog->endp[i] = s + (prog->endp[i] - startpos);
995                 }
996             }
997         }
998     }
999     /* Preserve the current value of $^R */
1000     if (oreplsv != GvSV(PL_replgv)) {
1001         sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is
1002                                            restored, the value remains
1003                                            the same. */
1004     }
1005     return 1;
1006
1007 phooey:
1008     return 0;
1009 }
1010
1011 /*
1012  - regtry - try match at specific point
1013  */
1014 STATIC I32                      /* 0 failure, 1 success */
1015 regtry(regexp *prog, char *startpos)
1016 {
1017     dTHR;
1018     register I32 i;
1019     register char **sp;
1020     register char **ep;
1021     CHECKPOINT lastcp;
1022
1023     if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) {
1024         PL_reg_eval_set = RS_init;
1025         DEBUG_r(DEBUG_s(
1026             PerlIO_printf(Perl_debug_log, "  setting stack tmpbase at %i\n",
1027                           PL_stack_sp - PL_stack_base);
1028             ));
1029         SAVEINT(cxstack[cxstack_ix].blk_oldsp);
1030         cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
1031         /* Otherwise OP_NEXTSTATE will free whatever on stack now.  */
1032         SAVETMPS;
1033         /* Apparently this is not needed, judging by wantarray. */
1034         /* SAVEINT(cxstack[cxstack_ix].blk_gimme);
1035            cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
1036     }
1037     PL_reginput = startpos;
1038     PL_regstartp = prog->startp;
1039     PL_regendp = prog->endp;
1040     PL_reglastparen = &prog->lastparen;
1041     prog->lastparen = 0;
1042     PL_regsize = 0;
1043     if (PL_reg_start_tmpl <= prog->nparens) {
1044         PL_reg_start_tmpl = prog->nparens*3/2 + 3;
1045         if(PL_reg_start_tmp)
1046             Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
1047         else
1048             New(22,PL_reg_start_tmp, PL_reg_start_tmpl, char*);
1049     }
1050
1051     sp = prog->startp;
1052     ep = prog->endp;
1053     if (prog->nparens) {
1054         for (i = prog->nparens; i >= 0; i--) {
1055             *sp++ = NULL;
1056             *ep++ = NULL;
1057         }
1058     }
1059     REGCP_SET;
1060     if (regmatch(prog->program + 1)) {
1061         prog->startp[0] = startpos;
1062         prog->endp[0] = PL_reginput;
1063         return 1;
1064     }
1065     REGCP_UNWIND;
1066     return 0;
1067 }
1068
1069 /*
1070  - regmatch - main matching routine
1071  *
1072  * Conceptually the strategy is simple:  check to see whether the current
1073  * node matches, call self recursively to see whether the rest matches,
1074  * and then act accordingly.  In practice we make some effort to avoid
1075  * recursion, in particular by going through "ordinary" nodes (that don't
1076  * need to know whether the rest of the match failed) by a loop instead of
1077  * by recursion.
1078  */
1079 /* [lwall] I've hoisted the register declarations to the outer block in order to
1080  * maybe save a little bit of pushing and popping on the stack.  It also takes
1081  * advantage of machines that use a register save mask on subroutine entry.
1082  */
1083 STATIC I32                      /* 0 failure, 1 success */
1084 regmatch(regnode *prog)
1085 {
1086     dTHR;
1087     register regnode *scan;     /* Current node. */
1088     regnode *next;              /* Next node. */
1089     regnode *inner;             /* Next node in internal branch. */
1090     register I32 nextchr;       /* renamed nextchr - nextchar colides with
1091                                    function of same name */
1092     register I32 n;             /* no or next */
1093     register I32 ln;            /* len or last */
1094     register char *s;           /* operand or save */
1095     register char *locinput = PL_reginput;
1096     register I32 c1, c2, paren; /* case fold search, parenth */
1097     int minmod = 0, sw = 0, logical = 0;
1098 #ifdef DEBUGGING
1099     PL_regindent++;
1100 #endif
1101
1102     /* Note that nextchr is a byte even in UTF */
1103     nextchr = UCHARAT(locinput);
1104     scan = prog;
1105     while (scan != NULL) {
1106 #define sayNO_L (logical ? (logical = 0, sw = 0, goto cont) : sayNO)
1107 #ifdef DEBUGGING
1108 #  define sayYES goto yes
1109 #  define sayNO goto no
1110 #  define saySAME(x) if (x) goto yes; else goto no
1111 #  define REPORT_CODE_OFF 24
1112 #else
1113 #  define sayYES return 1
1114 #  define sayNO return 0
1115 #  define saySAME(x) return x
1116 #endif
1117         DEBUG_r( {
1118             SV *prop = sv_newmortal();
1119             int docolor = *PL_colors[0];
1120             int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
1121             int l = (PL_regeol - locinput > taill ? taill : PL_regeol - locinput);
1122             int pref_len = (locinput - PL_bostr > (5 + taill) - l 
1123                             ? (5 + taill) - l : locinput - PL_bostr);
1124             int pref0_len = pref_len  - (locinput - PL_reginput);
1125
1126             if (l + pref_len < (5 + taill) && l < PL_regeol - locinput)
1127                 l = ( PL_regeol - locinput > (5 + taill) - pref_len 
1128                       ? (5 + taill) - pref_len : PL_regeol - locinput);
1129             if (pref0_len < 0)
1130                 pref0_len = 0;
1131             regprop(prop, scan);
1132             PerlIO_printf(Perl_debug_log, 
1133                           "%4i <%s%.*s%s%s%.*s%s%s%s%.*s%s>%*s|%3d:%*s%s\n",
1134                           locinput - PL_bostr, 
1135                           PL_colors[4], pref0_len, 
1136                           locinput - pref_len, PL_colors[5],
1137                           PL_colors[2], pref_len - pref0_len, 
1138                           locinput - pref_len + pref0_len, PL_colors[3],
1139                           (docolor ? "" : "> <"),
1140                           PL_colors[0], l, locinput, PL_colors[1],
1141                           15 - l - pref_len + 1,
1142                           "",
1143                           scan - PL_regprogram, PL_regindent*2, "",
1144                           SvPVX(prop));
1145         } );
1146
1147         next = scan + NEXT_OFF(scan);
1148         if (next == scan)
1149             next = NULL;
1150
1151         switch (OP(scan)) {
1152         case BOL:
1153             if (locinput == PL_bostr
1154                 ? PL_regprev == '\n'
1155                 : (PL_multiline && 
1156                    (nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
1157             {
1158                 /* regtill = regbol; */
1159                 break;
1160             }
1161             sayNO;
1162         case MBOL:
1163             if (locinput == PL_bostr
1164                 ? PL_regprev == '\n'
1165                 : ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
1166             {
1167                 break;
1168             }
1169             sayNO;
1170         case SBOL:
1171             if (locinput == PL_regbol && PL_regprev == '\n')
1172                 break;
1173             sayNO;
1174         case GPOS:
1175             if (locinput == PL_reg_ganch)
1176                 break;
1177             sayNO;
1178         case EOL:
1179             if (PL_multiline)
1180                 goto meol;
1181             else
1182                 goto seol;
1183         case MEOL:
1184           meol:
1185             if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
1186                 sayNO;
1187             break;
1188         case SEOL:
1189           seol:
1190             if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
1191                 sayNO;
1192             if (PL_regeol - locinput > 1)
1193                 sayNO;
1194             break;
1195         case EOS:
1196             if (PL_regeol != locinput)
1197                 sayNO;
1198             break;
1199         case SANYUTF8:
1200             if (nextchr & 0x80) {
1201                 locinput += PL_utf8skip[nextchr];
1202                 if (locinput > PL_regeol)
1203                     sayNO;
1204                 nextchr = UCHARAT(locinput);
1205                 break;
1206             }
1207             if (!nextchr && locinput >= PL_regeol)
1208                 sayNO;
1209             nextchr = UCHARAT(++locinput);
1210             break;
1211         case SANY:
1212             if (!nextchr && locinput >= PL_regeol)
1213                 sayNO;
1214             nextchr = UCHARAT(++locinput);
1215             break;
1216         case ANYUTF8:
1217             if (nextchr & 0x80) {
1218                 locinput += PL_utf8skip[nextchr];
1219                 if (locinput > PL_regeol)
1220                     sayNO;
1221                 nextchr = UCHARAT(locinput);
1222                 break;
1223             }
1224             if (!nextchr && locinput >= PL_regeol || nextchr == '\n')
1225                 sayNO;
1226             nextchr = UCHARAT(++locinput);
1227             break;
1228         case REG_ANY:
1229             if (!nextchr && locinput >= PL_regeol || nextchr == '\n')
1230                 sayNO;
1231             nextchr = UCHARAT(++locinput);
1232             break;
1233         case EXACT:
1234             s = (char *) OPERAND(scan);
1235             ln = UCHARAT(s++);
1236             /* Inline the first character, for speed. */
1237             if (UCHARAT(s) != nextchr)
1238                 sayNO;
1239             if (PL_regeol - locinput < ln)
1240                 sayNO;
1241             if (ln > 1 && memNE(s, locinput, ln))
1242                 sayNO;
1243             locinput += ln;
1244             nextchr = UCHARAT(locinput);
1245             break;
1246         case EXACTFL:
1247             PL_reg_flags |= RF_tainted;
1248             /* FALL THROUGH */
1249         case EXACTF:
1250             s = (char *) OPERAND(scan);
1251             ln = UCHARAT(s++);
1252
1253             if (UTF) {
1254                 char *l = locinput;
1255                 char *e = s + ln;
1256                 c1 = OP(scan) == EXACTF;
1257                 while (s < e) {
1258                     if (l >= PL_regeol)
1259                         sayNO;
1260                     if (utf8_to_uv((U8*)s, 0) != (c1 ?
1261                                                   toLOWER_utf8((U8*)l) :
1262                                                   toLOWER_LC_utf8((U8*)l)))
1263                     {
1264                         sayNO;
1265                     }
1266                     s += UTF8SKIP(s);
1267                     l += UTF8SKIP(l);
1268                 }
1269                 locinput = l;
1270                 nextchr = UCHARAT(locinput);
1271                 break;
1272             }
1273
1274             /* Inline the first character, for speed. */
1275             if (UCHARAT(s) != nextchr &&
1276                 UCHARAT(s) != ((OP(scan) == EXACTF)
1277                                ? PL_fold : PL_fold_locale)[nextchr])
1278                 sayNO;
1279             if (PL_regeol - locinput < ln)
1280                 sayNO;
1281             if (ln > 1 && (OP(scan) == EXACTF
1282                            ? ibcmp(s, locinput, ln)
1283                            : ibcmp_locale(s, locinput, ln)))
1284                 sayNO;
1285             locinput += ln;
1286             nextchr = UCHARAT(locinput);
1287             break;
1288         case ANYOFUTF8:
1289             s = (char *) OPERAND(scan);
1290             if (!REGINCLASSUTF8(scan, (U8*)locinput))
1291                 sayNO;
1292             if (locinput >= PL_regeol)
1293                 sayNO;
1294             locinput += PL_utf8skip[nextchr];
1295             nextchr = UCHARAT(locinput);
1296             break;
1297         case ANYOF:
1298             s = (char *) OPERAND(scan);
1299             if (nextchr < 0)
1300                 nextchr = UCHARAT(locinput);
1301             if (!REGINCLASS(s, nextchr))
1302                 sayNO;
1303             if (!nextchr && locinput >= PL_regeol)
1304                 sayNO;
1305             nextchr = UCHARAT(++locinput);
1306             break;
1307         case ALNUML:
1308             PL_reg_flags |= RF_tainted;
1309             /* FALL THROUGH */
1310         case ALNUM:
1311             if (!nextchr)
1312                 sayNO;
1313             if (!(OP(scan) == ALNUM
1314                   ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
1315                 sayNO;
1316             nextchr = UCHARAT(++locinput);
1317             break;
1318         case ALNUMLUTF8:
1319             PL_reg_flags |= RF_tainted;
1320             /* FALL THROUGH */
1321         case ALNUMUTF8:
1322             if (!nextchr)
1323                 sayNO;
1324             if (nextchr & 0x80) {
1325                 if (!(OP(scan) == ALNUMUTF8
1326                       ? swash_fetch(PL_utf8_alnum, (U8*)locinput)
1327                       : isALNUM_LC_utf8((U8*)locinput)))
1328                 {
1329                     sayNO;
1330                 }
1331                 locinput += PL_utf8skip[nextchr];
1332                 nextchr = UCHARAT(locinput);
1333                 break;
1334             }
1335             if (!(OP(scan) == ALNUMUTF8
1336                   ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
1337                 sayNO;
1338             nextchr = UCHARAT(++locinput);
1339             break;
1340         case NALNUML:
1341             PL_reg_flags |= RF_tainted;
1342             /* FALL THROUGH */
1343         case NALNUM:
1344             if (!nextchr && locinput >= PL_regeol)
1345                 sayNO;
1346             if (OP(scan) == NALNUM
1347                 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
1348                 sayNO;
1349             nextchr = UCHARAT(++locinput);
1350             break;
1351         case NALNUMLUTF8:
1352             PL_reg_flags |= RF_tainted;
1353             /* FALL THROUGH */
1354         case NALNUMUTF8:
1355             if (!nextchr && locinput >= PL_regeol)
1356                 sayNO;
1357             if (nextchr & 0x80) {
1358                 if (OP(scan) == NALNUMUTF8
1359                     ? swash_fetch(PL_utf8_alnum, (U8*)locinput)
1360                     : isALNUM_LC_utf8((U8*)locinput))
1361                 {
1362                     sayNO;
1363                 }
1364                 locinput += PL_utf8skip[nextchr];
1365                 nextchr = UCHARAT(locinput);
1366                 break;
1367             }
1368             if (OP(scan) == NALNUMUTF8
1369                 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
1370                 sayNO;
1371             nextchr = UCHARAT(++locinput);
1372             break;
1373         case BOUNDL:
1374         case NBOUNDL:
1375             PL_reg_flags |= RF_tainted;
1376             /* FALL THROUGH */
1377         case BOUND:
1378         case NBOUND:
1379             /* was last char in word? */
1380             ln = (locinput != PL_regbol) ? UCHARAT(locinput - 1) : PL_regprev;
1381             if (OP(scan) == BOUND || OP(scan) == NBOUND) {
1382                 ln = isALNUM(ln);
1383                 n = isALNUM(nextchr);
1384             }
1385             else {
1386                 ln = isALNUM_LC(ln);
1387                 n = isALNUM_LC(nextchr);
1388             }
1389             if (((!ln) == (!n)) == (OP(scan) == BOUND || OP(scan) == BOUNDL))
1390                 sayNO;
1391             break;
1392         case BOUNDLUTF8:
1393         case NBOUNDLUTF8:
1394             PL_reg_flags |= RF_tainted;
1395             /* FALL THROUGH */
1396         case BOUNDUTF8:
1397         case NBOUNDUTF8:
1398             /* was last char in word? */
1399             ln = (locinput != PL_regbol)
1400                 ? utf8_to_uv(reghop((U8*)locinput, -1), 0) : PL_regprev;
1401             if (OP(scan) == BOUNDUTF8 || OP(scan) == NBOUNDUTF8) {
1402                 ln = isALNUM_uni(ln);
1403                 n = swash_fetch(PL_utf8_alnum, (U8*)locinput);
1404             }
1405             else {
1406                 ln = isALNUM_LC_uni(ln);
1407                 n = isALNUM_LC_utf8((U8*)locinput);
1408             }
1409             if (((!ln) == (!n)) == (OP(scan) == BOUNDUTF8 || OP(scan) == BOUNDLUTF8))
1410                 sayNO;
1411             break;
1412         case SPACEL:
1413             PL_reg_flags |= RF_tainted;
1414             /* FALL THROUGH */
1415         case SPACE:
1416             if (!nextchr && locinput >= PL_regeol)
1417                 sayNO;
1418             if (!(OP(scan) == SPACE
1419                   ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
1420                 sayNO;
1421             nextchr = UCHARAT(++locinput);
1422             break;
1423         case SPACELUTF8:
1424             PL_reg_flags |= RF_tainted;
1425             /* FALL THROUGH */
1426         case SPACEUTF8:
1427             if (!nextchr && locinput >= PL_regeol)
1428                 sayNO;
1429             if (nextchr & 0x80) {
1430                 if (!(OP(scan) == SPACEUTF8
1431                       ? swash_fetch(PL_utf8_space,(U8*)locinput)
1432                       : isSPACE_LC_utf8((U8*)locinput)))
1433                 {
1434                     sayNO;
1435                 }
1436                 locinput += PL_utf8skip[nextchr];
1437                 nextchr = UCHARAT(locinput);
1438                 break;
1439             }
1440             if (!(OP(scan) == SPACEUTF8
1441                   ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
1442                 sayNO;
1443             nextchr = UCHARAT(++locinput);
1444             break;
1445         case NSPACEL:
1446             PL_reg_flags |= RF_tainted;
1447             /* FALL THROUGH */
1448         case NSPACE:
1449             if (!nextchr)
1450                 sayNO;
1451             if (OP(scan) == SPACE
1452                 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
1453                 sayNO;
1454             nextchr = UCHARAT(++locinput);
1455             break;
1456         case NSPACELUTF8:
1457             PL_reg_flags |= RF_tainted;
1458             /* FALL THROUGH */
1459         case NSPACEUTF8:
1460             if (!nextchr)
1461                 sayNO;
1462             if (nextchr & 0x80) {
1463                 if (OP(scan) == NSPACEUTF8
1464                     ? swash_fetch(PL_utf8_space,(U8*)locinput)
1465                     : isSPACE_LC_utf8((U8*)locinput))
1466                 {
1467                     sayNO;
1468                 }
1469                 locinput += PL_utf8skip[nextchr];
1470                 nextchr = UCHARAT(locinput);
1471                 break;
1472             }
1473             if (OP(scan) == NSPACEUTF8
1474                 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
1475                 sayNO;
1476             nextchr = UCHARAT(++locinput);
1477             break;
1478         case DIGIT:
1479             if (!isDIGIT(nextchr))
1480                 sayNO;
1481             nextchr = UCHARAT(++locinput);
1482             break;
1483         case DIGITUTF8:
1484             if (nextchr & 0x80) {
1485                 if (!(swash_fetch(PL_utf8_digit,(U8*)locinput)))
1486                     sayNO;
1487                 locinput += PL_utf8skip[nextchr];
1488                 nextchr = UCHARAT(locinput);
1489                 break;
1490             }
1491             if (!isDIGIT(nextchr))
1492                 sayNO;
1493             nextchr = UCHARAT(++locinput);
1494             break;
1495         case NDIGIT:
1496             if (!nextchr && locinput >= PL_regeol)
1497                 sayNO;
1498             if (isDIGIT(nextchr))
1499                 sayNO;
1500             nextchr = UCHARAT(++locinput);
1501             break;
1502         case NDIGITUTF8:
1503             if (!nextchr && locinput >= PL_regeol)
1504                 sayNO;
1505             if (nextchr & 0x80) {
1506                 if (swash_fetch(PL_utf8_digit,(U8*)locinput))
1507                     sayNO;
1508                 locinput += PL_utf8skip[nextchr];
1509                 nextchr = UCHARAT(locinput);
1510                 break;
1511             }
1512             if (isDIGIT(nextchr))
1513                 sayNO;
1514             nextchr = UCHARAT(++locinput);
1515             break;
1516         case CLUMP:
1517             if (locinput >= PL_regeol || swash_fetch(PL_utf8_mark,(U8*)locinput))
1518                 sayNO;
1519             locinput += PL_utf8skip[nextchr];
1520             while (locinput < PL_regeol && swash_fetch(PL_utf8_mark,(U8*)locinput))
1521                 locinput += UTF8SKIP(locinput);
1522             if (locinput > PL_regeol)
1523                 sayNO;
1524             nextchr = UCHARAT(locinput);
1525             break;
1526         case REFFL:
1527             PL_reg_flags |= RF_tainted;
1528             /* FALL THROUGH */
1529         case REF:
1530         case REFF:
1531             n = ARG(scan);  /* which paren pair */
1532             s = PL_regstartp[n];
1533             if (*PL_reglastparen < n || !s)
1534                 sayNO;                  /* Do not match unless seen CLOSEn. */
1535             if (s == PL_regendp[n])
1536                 break;
1537
1538             if (UTF && OP(scan) != REF) {       /* REF can do byte comparison */
1539                 char *l = locinput;
1540                 char *e = PL_regendp[n];
1541                 /*
1542                  * Note that we can't do the "other character" lookup trick as
1543                  * in the 8-bit case (no pun intended) because in Unicode we
1544                  * have to map both upper and title case to lower case.
1545                  */
1546                 if (OP(scan) == REFF) {
1547                     while (s < e) {
1548                         if (l >= PL_regeol)
1549                             sayNO;
1550                         if (toLOWER_utf8((U8*)s) != toLOWER_utf8((U8*)l))
1551                             sayNO;
1552                         s += UTF8SKIP(s);
1553                         l += UTF8SKIP(l);
1554                     }
1555                 }
1556                 else {
1557                     while (s < e) {
1558                         if (l >= PL_regeol)
1559                             sayNO;
1560                         if (toLOWER_LC_utf8((U8*)s) != toLOWER_LC_utf8((U8*)l))
1561                             sayNO;
1562                         s += UTF8SKIP(s);
1563                         l += UTF8SKIP(l);
1564                     }
1565                 }
1566                 locinput = l;
1567                 nextchr = UCHARAT(locinput);
1568                 break;
1569             }
1570
1571             /* Inline the first character, for speed. */
1572             if (UCHARAT(s) != nextchr &&
1573                 (OP(scan) == REF ||
1574                  (UCHARAT(s) != ((OP(scan) == REFF
1575                                   ? PL_fold : PL_fold_locale)[nextchr]))))
1576                 sayNO;
1577             ln = PL_regendp[n] - s;
1578             if (locinput + ln > PL_regeol)
1579                 sayNO;
1580             if (ln > 1 && (OP(scan) == REF
1581                            ? memNE(s, locinput, ln)
1582                            : (OP(scan) == REFF
1583                               ? ibcmp(s, locinput, ln)
1584                               : ibcmp_locale(s, locinput, ln))))
1585                 sayNO;
1586             locinput += ln;
1587             nextchr = UCHARAT(locinput);
1588             break;
1589
1590         case NOTHING:
1591         case TAIL:
1592             break;
1593         case BACK:
1594             break;
1595         case EVAL:
1596         {
1597             dSP;
1598             OP_4tree *oop = PL_op;
1599             COP *ocurcop = PL_curcop;
1600             SV **ocurpad = PL_curpad;
1601             SV *ret;
1602             
1603             n = ARG(scan);
1604             PL_op = (OP_4tree*)PL_regdata->data[n];
1605             DEBUG_r( PerlIO_printf(Perl_debug_log, "  re_eval 0x%x\n", PL_op) );
1606             PL_curpad = AvARRAY((AV*)PL_regdata->data[n + 1]);
1607
1608             CALLRUNOPS();                       /* Scalar context. */
1609             SPAGAIN;
1610             ret = POPs;
1611             PUTBACK;
1612             
1613             PL_op = oop;
1614             PL_curpad = ocurpad;
1615             PL_curcop = ocurcop;
1616             if (logical) {
1617                 if (logical == 2) {     /* Postponed subexpression. */
1618                     regexp *re;
1619                     MAGIC *mg = Null(MAGIC*);
1620                     re_cc_state state;
1621                     CURCUR cctmp;
1622                     CHECKPOINT cp, lastcp;
1623
1624                     if(SvROK(ret) || SvRMAGICAL(ret)) {
1625                         SV *sv = SvROK(ret) ? SvRV(ret) : ret;
1626
1627                         if(SvMAGICAL(sv))
1628                             mg = mg_find(sv, 'r');
1629                     }
1630                     if (mg) {
1631                         re = (regexp *)mg->mg_obj;
1632                         (void)ReREFCNT_inc(re);
1633                     }
1634                     else {
1635                         STRLEN len;
1636                         char *t = SvPV(ret, len);
1637                         PMOP pm;
1638                         char *oprecomp = PL_regprecomp;
1639                         I32 osize = PL_regsize;
1640                         I32 onpar = PL_regnpar;
1641
1642                         pm.op_pmflags = 0;
1643                         re = CALLREGCOMP(t, t + len, &pm);
1644                         if (!(SvFLAGS(ret) 
1645                               & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)))
1646                             sv_magic(ret,(SV*)ReREFCNT_inc(re),'r',0,0);
1647                         PL_regprecomp = oprecomp;
1648                         PL_regsize = osize;
1649                         PL_regnpar = onpar;
1650                     }
1651                     DEBUG_r(
1652                         PerlIO_printf(Perl_debug_log, 
1653                                       "Entering embedded `%s%.60s%s%s'\n",
1654                                       PL_colors[0],
1655                                       re->precomp,
1656                                       PL_colors[1],
1657                                       (strlen(re->precomp) > 60 ? "..." : ""))
1658                         );
1659                     state.node = next;
1660                     state.prev = PL_reg_call_cc;
1661                     state.cc = PL_regcc;
1662                     state.re = PL_reg_re;
1663
1664                     cctmp.cur = 0;
1665                     cctmp.oldcc = 0;
1666                     PL_regcc = &cctmp;
1667                     
1668                     cp = regcppush(0);  /* Save *all* the positions. */
1669                     REGCP_SET;
1670                     cache_re(re);
1671                     state.ss = PL_savestack_ix;
1672                     *PL_reglastparen = 0;
1673                     PL_reg_call_cc = &state;
1674                     PL_reginput = locinput;
1675                     if (regmatch(re->program + 1)) {
1676                         ReREFCNT_dec(re);
1677                         regcpblow(cp);
1678                         sayYES;
1679                     }
1680                     DEBUG_r(
1681                         PerlIO_printf(Perl_debug_log,
1682                                       "%*s  failed...\n",
1683                                       REPORT_CODE_OFF+PL_regindent*2, "")
1684                         );
1685                     ReREFCNT_dec(re);
1686                     REGCP_UNWIND;
1687                     regcppop();
1688                     PL_reg_call_cc = state.prev;
1689                     PL_regcc = state.cc;
1690                     PL_reg_re = state.re;
1691                     cache_re(PL_reg_re);
1692                     sayNO;
1693                 }
1694                 sw = SvTRUE(ret);
1695                 logical = 0;
1696             }
1697             else
1698                 sv_setsv(save_scalar(PL_replgv), ret);
1699             break;
1700         }
1701         case OPEN:
1702             n = ARG(scan);  /* which paren pair */
1703             PL_reg_start_tmp[n] = locinput;
1704             if (n > PL_regsize)
1705                 PL_regsize = n;
1706             break;
1707         case CLOSE:
1708             n = ARG(scan);  /* which paren pair */
1709             PL_regstartp[n] = PL_reg_start_tmp[n];
1710             PL_regendp[n] = locinput;
1711             if (n > *PL_reglastparen)
1712                 *PL_reglastparen = n;
1713             break;
1714         case GROUPP:
1715             n = ARG(scan);  /* which paren pair */
1716             sw = (*PL_reglastparen >= n && PL_regendp[n] != NULL);
1717             break;
1718         case IFTHEN:
1719             if (sw)
1720                 next = NEXTOPER(NEXTOPER(scan));
1721             else {
1722                 next = scan + ARG(scan);
1723                 if (OP(next) == IFTHEN) /* Fake one. */
1724                     next = NEXTOPER(NEXTOPER(next));
1725             }
1726             break;
1727         case LOGICAL:
1728             logical = scan->flags;
1729             break;
1730         case CURLYX: {
1731                 CURCUR cc;
1732                 CHECKPOINT cp = PL_savestack_ix;
1733
1734                 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
1735                     next += ARG(next);
1736                 cc.oldcc = PL_regcc;
1737                 PL_regcc = &cc;
1738                 cc.parenfloor = *PL_reglastparen;
1739                 cc.cur = -1;
1740                 cc.min = ARG1(scan);
1741                 cc.max  = ARG2(scan);
1742                 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
1743                 cc.next = next;
1744                 cc.minmod = minmod;
1745                 cc.lastloc = 0;
1746                 PL_reginput = locinput;
1747                 n = regmatch(PREVOPER(next));   /* start on the WHILEM */
1748                 regcpblow(cp);
1749                 PL_regcc = cc.oldcc;
1750                 saySAME(n);
1751             }
1752             /* NOT REACHED */
1753         case WHILEM: {
1754                 /*
1755                  * This is really hard to understand, because after we match
1756                  * what we're trying to match, we must make sure the rest of
1757                  * the RE is going to match for sure, and to do that we have
1758                  * to go back UP the parse tree by recursing ever deeper.  And
1759                  * if it fails, we have to reset our parent's current state
1760                  * that we can try again after backing off.
1761                  */
1762
1763                 CHECKPOINT cp, lastcp;
1764                 CURCUR* cc = PL_regcc;
1765                 char *lastloc = cc->lastloc; /* Detection of 0-len. */
1766                 
1767                 n = cc->cur + 1;        /* how many we know we matched */
1768                 PL_reginput = locinput;
1769
1770                 DEBUG_r(
1771                     PerlIO_printf(Perl_debug_log, 
1772                                   "%*s  %ld out of %ld..%ld  cc=%lx\n", 
1773                                   REPORT_CODE_OFF+PL_regindent*2, "",
1774                                   (long)n, (long)cc->min, 
1775                                   (long)cc->max, (long)cc)
1776                     );
1777
1778                 /* If degenerate scan matches "", assume scan done. */
1779
1780                 if (locinput == cc->lastloc && n >= cc->min) {
1781                     PL_regcc = cc->oldcc;
1782                     ln = PL_regcc->cur;
1783                     DEBUG_r(
1784                         PerlIO_printf(Perl_debug_log,
1785                            "%*s  empty match detected, try continuation...\n",
1786                            REPORT_CODE_OFF+PL_regindent*2, "")
1787                         );
1788                     if (regmatch(cc->next))
1789                         sayYES;
1790                     DEBUG_r(
1791                         PerlIO_printf(Perl_debug_log,
1792                                       "%*s  failed...\n",
1793                                       REPORT_CODE_OFF+PL_regindent*2, "")
1794                         );
1795                     PL_regcc->cur = ln;
1796                     PL_regcc = cc;
1797                     sayNO;
1798                 }
1799
1800                 /* First just match a string of min scans. */
1801
1802                 if (n < cc->min) {
1803                     cc->cur = n;
1804                     cc->lastloc = locinput;
1805                     if (regmatch(cc->scan))
1806                         sayYES;
1807                     cc->cur = n - 1;
1808                     cc->lastloc = lastloc;
1809                     DEBUG_r(
1810                         PerlIO_printf(Perl_debug_log,
1811                                       "%*s  failed...\n",
1812                                       REPORT_CODE_OFF+PL_regindent*2, "")
1813                         );
1814                     sayNO;
1815                 }
1816
1817                 /* Prefer next over scan for minimal matching. */
1818
1819                 if (cc->minmod) {
1820                     PL_regcc = cc->oldcc;
1821                     ln = PL_regcc->cur;
1822                     cp = regcppush(cc->parenfloor);
1823                     REGCP_SET;
1824                     if (regmatch(cc->next)) {
1825                         regcpblow(cp);
1826                         sayYES; /* All done. */
1827                     }
1828                     REGCP_UNWIND;
1829                     regcppop();
1830                     PL_regcc->cur = ln;
1831                     PL_regcc = cc;
1832
1833                     if (n >= cc->max) { /* Maximum greed exceeded? */
1834                         if (ckWARN(WARN_UNSAFE) && n >= REG_INFTY 
1835                             && !(PL_reg_flags & RF_warned)) {
1836                             PL_reg_flags |= RF_warned;
1837                             warner(WARN_UNSAFE, "%s limit (%d) exceeded",
1838                                  "Complex regular subexpression recursion",
1839                                  REG_INFTY - 1);
1840                         }
1841                         sayNO;
1842                     }
1843
1844                     DEBUG_r(
1845                         PerlIO_printf(Perl_debug_log,
1846                                       "%*s  trying longer...\n",
1847                                       REPORT_CODE_OFF+PL_regindent*2, "")
1848                         );
1849                     /* Try scanning more and see if it helps. */
1850                     PL_reginput = locinput;
1851                     cc->cur = n;
1852                     cc->lastloc = locinput;
1853                     cp = regcppush(cc->parenfloor);
1854                     REGCP_SET;
1855                     if (regmatch(cc->scan)) {
1856                         regcpblow(cp);
1857                         sayYES;
1858                     }
1859                     DEBUG_r(
1860                         PerlIO_printf(Perl_debug_log,
1861                                       "%*s  failed...\n",
1862                                       REPORT_CODE_OFF+PL_regindent*2, "")
1863                         );
1864                     REGCP_UNWIND;
1865                     regcppop();
1866                     cc->cur = n - 1;
1867                     cc->lastloc = lastloc;
1868                     sayNO;
1869                 }
1870
1871                 /* Prefer scan over next for maximal matching. */
1872
1873                 if (n < cc->max) {      /* More greed allowed? */
1874                     cp = regcppush(cc->parenfloor);
1875                     cc->cur = n;
1876                     cc->lastloc = locinput;
1877                     REGCP_SET;
1878                     if (regmatch(cc->scan)) {
1879                         regcpblow(cp);
1880                         sayYES;
1881                     }
1882                     REGCP_UNWIND;
1883                     regcppop();         /* Restore some previous $<digit>s? */
1884                     PL_reginput = locinput;
1885                     DEBUG_r(
1886                         PerlIO_printf(Perl_debug_log,
1887                                       "%*s  failed, try continuation...\n",
1888                                       REPORT_CODE_OFF+PL_regindent*2, "")
1889                         );
1890                 }
1891                 if (ckWARN(WARN_UNSAFE) && n >= REG_INFTY 
1892                         && !(PL_reg_flags & RF_warned)) {
1893                     PL_reg_flags |= RF_warned;
1894                     warner(WARN_UNSAFE, "%s limit (%d) exceeded",
1895                          "Complex regular subexpression recursion",
1896                          REG_INFTY - 1);
1897                 }
1898
1899                 /* Failed deeper matches of scan, so see if this one works. */
1900                 PL_regcc = cc->oldcc;
1901                 ln = PL_regcc->cur;
1902                 if (regmatch(cc->next))
1903                     sayYES;
1904                 DEBUG_r(
1905                     PerlIO_printf(Perl_debug_log, "%*s  failed...\n",
1906                                   REPORT_CODE_OFF+PL_regindent*2, "")
1907                     );
1908                 PL_regcc->cur = ln;
1909                 PL_regcc = cc;
1910                 cc->cur = n - 1;
1911                 cc->lastloc = lastloc;
1912                 sayNO;
1913             }
1914             /* NOT REACHED */
1915         case BRANCHJ: 
1916             next = scan + ARG(scan);
1917             if (next == scan)
1918                 next = NULL;
1919             inner = NEXTOPER(NEXTOPER(scan));
1920             goto do_branch;
1921         case BRANCH: 
1922             inner = NEXTOPER(scan);
1923           do_branch:
1924             {
1925                 CHECKPOINT lastcp;
1926                 c1 = OP(scan);
1927                 if (OP(next) != c1)     /* No choice. */
1928                     next = inner;       /* Avoid recursion. */
1929                 else {
1930                     int lastparen = *PL_reglastparen;
1931
1932                     REGCP_SET;
1933                     do {
1934                         PL_reginput = locinput;
1935                         if (regmatch(inner))
1936                             sayYES;
1937                         REGCP_UNWIND;
1938                         for (n = *PL_reglastparen; n > lastparen; n--)
1939                             PL_regendp[n] = 0;
1940                         *PL_reglastparen = n;
1941                         scan = next;
1942                         /*SUPPRESS 560*/
1943                         if (n = (c1 == BRANCH ? NEXT_OFF(next) : ARG(next)))
1944                             next += n;
1945                         else
1946                             next = NULL;
1947                         inner = NEXTOPER(scan);
1948                         if (c1 == BRANCHJ) {
1949                             inner = NEXTOPER(inner);
1950                         }
1951                     } while (scan != NULL && OP(scan) == c1);
1952                     sayNO;
1953                     /* NOTREACHED */
1954                 }
1955             }
1956             break;
1957         case MINMOD:
1958             minmod = 1;
1959             break;
1960         case CURLYM:
1961         {
1962             I32 l = 0;
1963             CHECKPOINT lastcp;
1964             
1965             /* We suppose that the next guy does not need
1966                backtracking: in particular, it is of constant length,
1967                and has no parenths to influence future backrefs. */
1968             ln = ARG1(scan);  /* min to match */
1969             n  = ARG2(scan);  /* max to match */
1970             paren = scan->flags;
1971             if (paren) {
1972                 if (paren > PL_regsize)
1973                     PL_regsize = paren;
1974                 if (paren > *PL_reglastparen)
1975                     *PL_reglastparen = paren;
1976             }
1977             scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
1978             if (paren)
1979                 scan += NEXT_OFF(scan); /* Skip former OPEN. */
1980             PL_reginput = locinput;
1981             if (minmod) {
1982                 minmod = 0;
1983                 if (ln && regrepeat_hard(scan, ln, &l) < ln)
1984                     sayNO;
1985                 if (ln && l == 0 && n >= ln
1986                     /* In fact, this is tricky.  If paren, then the
1987                        fact that we did/didnot match may influence
1988                        future execution. */
1989                     && !(paren && ln == 0))
1990                     ln = n;
1991                 locinput = PL_reginput;
1992                 if (PL_regkind[(U8)OP(next)] == EXACT) {
1993                     c1 = UCHARAT(OPERAND(next) + 1);
1994                     if (OP(next) == EXACTF)
1995                         c2 = PL_fold[c1];
1996                     else if (OP(next) == EXACTFL)
1997                         c2 = PL_fold_locale[c1];
1998                     else
1999                         c2 = c1;
2000                 }
2001                 else
2002                     c1 = c2 = -1000;
2003                 REGCP_SET;
2004                 /* This may be improved if l == 0.  */
2005                 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
2006                     /* If it could work, try it. */
2007                     if (c1 == -1000 ||
2008                         UCHARAT(PL_reginput) == c1 ||
2009                         UCHARAT(PL_reginput) == c2)
2010                     {
2011                         if (paren) {
2012                             if (n) {
2013                                 PL_regstartp[paren] = HOPc(PL_reginput, -l);
2014                                 PL_regendp[paren] = PL_reginput;
2015                             }
2016                             else
2017                                 PL_regendp[paren] = NULL;
2018                         }
2019                         if (regmatch(next))
2020                             sayYES;
2021                         REGCP_UNWIND;
2022                     }
2023                     /* Couldn't or didn't -- move forward. */
2024                     PL_reginput = locinput;
2025                     if (regrepeat_hard(scan, 1, &l)) {
2026                         ln++;
2027                         locinput = PL_reginput;
2028                     }
2029                     else
2030                         sayNO;
2031                 }
2032             }
2033             else {
2034                 n = regrepeat_hard(scan, n, &l);
2035                 if (n != 0 && l == 0
2036                     /* In fact, this is tricky.  If paren, then the
2037                        fact that we did/didnot match may influence
2038                        future execution. */
2039                     && !(paren && ln == 0))
2040                     ln = n;
2041                 locinput = PL_reginput;
2042                 DEBUG_r(
2043                     PerlIO_printf(Perl_debug_log,
2044                                   "%*s  matched %ld times, len=%ld...\n",
2045                                   REPORT_CODE_OFF+PL_regindent*2, "", n, l)
2046                     );
2047                 if (n >= ln) {
2048                     if (PL_regkind[(U8)OP(next)] == EXACT) {
2049                         c1 = UCHARAT(OPERAND(next) + 1);
2050                         if (OP(next) == EXACTF)
2051                             c2 = PL_fold[c1];
2052                         else if (OP(next) == EXACTFL)
2053                             c2 = PL_fold_locale[c1];
2054                         else
2055                             c2 = c1;
2056                     }
2057                     else
2058                         c1 = c2 = -1000;
2059                 }
2060                 REGCP_SET;
2061                 while (n >= ln) {
2062                     /* If it could work, try it. */
2063                     if (c1 == -1000 ||
2064                         UCHARAT(PL_reginput) == c1 ||
2065                         UCHARAT(PL_reginput) == c2)
2066                     {
2067                         DEBUG_r(
2068                                 PerlIO_printf(Perl_debug_log,
2069                                               "%*s  trying tail with n=%ld...\n",
2070                                               REPORT_CODE_OFF+PL_regindent*2, "", n)
2071                             );
2072                         if (paren) {
2073                             if (n) {
2074                                 PL_regstartp[paren] = HOPc(PL_reginput, -l);
2075                                 PL_regendp[paren] = PL_reginput;
2076                             }
2077                             else
2078                                 PL_regendp[paren] = NULL;
2079                         }
2080                         if (regmatch(next))
2081                             sayYES;
2082                         REGCP_UNWIND;
2083                     }
2084                     /* Couldn't or didn't -- back up. */
2085                     n--;
2086                     locinput = HOPc(locinput, -l);
2087                     PL_reginput = locinput;
2088                 }
2089             }
2090             sayNO;
2091             break;
2092         }
2093         case CURLYN:
2094             paren = scan->flags;        /* Which paren to set */
2095             if (paren > PL_regsize)
2096                 PL_regsize = paren;
2097             if (paren > *PL_reglastparen)
2098                 *PL_reglastparen = paren;
2099             ln = ARG1(scan);  /* min to match */
2100             n  = ARG2(scan);  /* max to match */
2101             scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
2102             goto repeat;
2103         case CURLY:
2104             paren = 0;
2105             ln = ARG1(scan);  /* min to match */
2106             n  = ARG2(scan);  /* max to match */
2107             scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
2108             goto repeat;
2109         case STAR:
2110             ln = 0;
2111             n = REG_INFTY;
2112             scan = NEXTOPER(scan);
2113             paren = 0;
2114             goto repeat;
2115         case PLUS:
2116             ln = 1;
2117             n = REG_INFTY;
2118             scan = NEXTOPER(scan);
2119             paren = 0;
2120           repeat:
2121             /*
2122             * Lookahead to avoid useless match attempts
2123             * when we know what character comes next.
2124             */
2125             if (PL_regkind[(U8)OP(next)] == EXACT) {
2126                 c1 = UCHARAT(OPERAND(next) + 1);
2127                 if (OP(next) == EXACTF)
2128                     c2 = PL_fold[c1];
2129                 else if (OP(next) == EXACTFL)
2130                     c2 = PL_fold_locale[c1];
2131                 else
2132                     c2 = c1;
2133             }
2134             else
2135                 c1 = c2 = -1000;
2136             PL_reginput = locinput;
2137             if (minmod) {
2138                 CHECKPOINT lastcp;
2139                 minmod = 0;
2140                 if (ln && regrepeat(scan, ln) < ln)
2141                     sayNO;
2142                 locinput = PL_reginput;
2143                 REGCP_SET;
2144                 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
2145                     /* If it could work, try it. */
2146                     if (c1 == -1000 ||
2147                         UCHARAT(PL_reginput) == c1 ||
2148                         UCHARAT(PL_reginput) == c2)
2149                     {
2150                         if (paren) {
2151                             if (n) {
2152                                 PL_regstartp[paren] = HOPc(PL_reginput, -1);
2153                                 PL_regendp[paren] = PL_reginput;
2154                             }
2155                             else
2156                                 PL_regendp[paren] = NULL;
2157                         }
2158                         if (regmatch(next))
2159                             sayYES;
2160                         REGCP_UNWIND;
2161                     }
2162                     /* Couldn't or didn't -- move forward. */
2163                     PL_reginput = locinput;
2164                     if (regrepeat(scan, 1)) {
2165                         ln++;
2166                         locinput = PL_reginput;
2167                     }
2168                     else
2169                         sayNO;
2170                 }
2171             }
2172             else {
2173                 CHECKPOINT lastcp;
2174                 n = regrepeat(scan, n);
2175                 locinput = PL_reginput;
2176                 if (ln < n && PL_regkind[(U8)OP(next)] == EOL &&
2177                     (!PL_multiline  || OP(next) == SEOL))
2178                     ln = n;                     /* why back off? */
2179                 REGCP_SET;
2180                 if (paren) {
2181                     while (n >= ln) {
2182                         /* If it could work, try it. */
2183                         if (c1 == -1000 ||
2184                             UCHARAT(PL_reginput) == c1 ||
2185                             UCHARAT(PL_reginput) == c2)
2186                             {
2187                                 if (paren && n) {
2188                                     if (n) {
2189                                         PL_regstartp[paren] = HOPc(PL_reginput, -1);
2190                                         PL_regendp[paren] = PL_reginput;
2191                                     }
2192                                     else
2193                                         PL_regendp[paren] = NULL;
2194                                 }
2195                                 if (regmatch(next))
2196                                     sayYES;
2197                                 REGCP_UNWIND;
2198                             }
2199                         /* Couldn't or didn't -- back up. */
2200                         n--;
2201                         PL_reginput = locinput = HOPc(locinput, -1);
2202                     }
2203                 }
2204                 else {
2205                     while (n >= ln) {
2206                         /* If it could work, try it. */
2207                         if (c1 == -1000 ||
2208                             UCHARAT(PL_reginput) == c1 ||
2209                             UCHARAT(PL_reginput) == c2)
2210                             {
2211                                 if (regmatch(next))
2212                                     sayYES;
2213                                 REGCP_UNWIND;
2214                             }
2215                         /* Couldn't or didn't -- back up. */
2216                         n--;
2217                         PL_reginput = locinput = HOPc(locinput, -1);
2218                     }
2219                 }
2220             }
2221             sayNO;
2222             break;
2223         case END:
2224             if (PL_reg_call_cc) {
2225                 re_cc_state *cur_call_cc = PL_reg_call_cc;
2226                 CURCUR *cctmp = PL_regcc;
2227                 regexp *re = PL_reg_re;
2228                 CHECKPOINT cp, lastcp;
2229                 
2230                 cp = regcppush(0);      /* Save *all* the positions. */
2231                 REGCP_SET;
2232                 regcp_set_to(PL_reg_call_cc->ss); /* Restore parens of
2233                                                     the caller. */
2234                 PL_reginput = locinput; /* Make position available to
2235                                            the callcc. */
2236                 cache_re(PL_reg_call_cc->re);
2237                 PL_regcc = PL_reg_call_cc->cc;
2238                 PL_reg_call_cc = PL_reg_call_cc->prev;
2239                 if (regmatch(cur_call_cc->node)) {
2240                     PL_reg_call_cc = cur_call_cc;
2241                     regcpblow(cp);
2242                     sayYES;
2243                 }
2244                 REGCP_UNWIND;
2245                 regcppop();
2246                 PL_reg_call_cc = cur_call_cc;
2247                 PL_regcc = cctmp;
2248                 PL_reg_re = re;
2249                 cache_re(re);
2250
2251                 DEBUG_r(
2252                     PerlIO_printf(Perl_debug_log,
2253                                   "%*s  continuation failed...\n",
2254                                   REPORT_CODE_OFF+PL_regindent*2, "")
2255                     );
2256                 sayNO;
2257             }
2258             if (locinput < PL_regtill)
2259                 sayNO;                  /* Cannot match: too short. */
2260             /* Fall through */
2261         case SUCCEED:
2262             PL_reginput = locinput;     /* put where regtry can find it */
2263             sayYES;                     /* Success! */
2264         case SUSPEND:
2265             n = 1;
2266             PL_reginput = locinput;
2267             goto do_ifmatch;        
2268         case UNLESSM:
2269             n = 0;
2270             if (scan->flags) {
2271                 s = HOPMAYBEc(locinput, -scan->flags);
2272                 if (!s)
2273                     goto say_yes;
2274                 PL_reginput = s;
2275             }
2276             else
2277                 PL_reginput = locinput;
2278             goto do_ifmatch;
2279         case IFMATCH:
2280             n = 1;
2281             if (scan->flags) {
2282                 s = HOPMAYBEc(locinput, -scan->flags);
2283                 if (!s || s < PL_bostr)
2284                     goto say_no;
2285                 PL_reginput = s;
2286             }
2287             else
2288                 PL_reginput = locinput;
2289
2290           do_ifmatch:
2291             inner = NEXTOPER(NEXTOPER(scan));
2292             if (regmatch(inner) != n) {
2293               say_no:
2294                 if (logical) {
2295                     logical = 0;
2296                     sw = 0;
2297                     goto do_longjump;
2298                 }
2299                 else
2300                     sayNO;
2301             }
2302           say_yes:
2303             if (logical) {
2304                 logical = 0;
2305                 sw = 1;
2306             }
2307             if (OP(scan) == SUSPEND) {
2308                 locinput = PL_reginput;
2309                 nextchr = UCHARAT(locinput);
2310             }
2311             /* FALL THROUGH. */
2312         case LONGJMP:
2313           do_longjump:
2314             next = scan + ARG(scan);
2315             if (next == scan)
2316                 next = NULL;
2317             break;
2318         default:
2319             PerlIO_printf(PerlIO_stderr(), "%lx %d\n",
2320                           (unsigned long)scan, OP(scan));
2321             FAIL("regexp memory corruption");
2322         }
2323         scan = next;
2324     }
2325
2326     /*
2327     * We get here only if there's trouble -- normally "case END" is
2328     * the terminating point.
2329     */
2330     FAIL("corrupted regexp pointers");
2331     /*NOTREACHED*/
2332     sayNO;
2333
2334 yes:
2335 #ifdef DEBUGGING
2336     PL_regindent--;
2337 #endif
2338     return 1;
2339
2340 no:
2341 #ifdef DEBUGGING
2342     PL_regindent--;
2343 #endif
2344     return 0;
2345 }
2346
2347 /*
2348  - regrepeat - repeatedly match something simple, report how many
2349  */
2350 /*
2351  * [This routine now assumes that it will only match on things of length 1.
2352  * That was true before, but now we assume scan - reginput is the count,
2353  * rather than incrementing count on every character.  [Er, except utf8.]]
2354  */
2355 STATIC I32
2356 regrepeat(regnode *p, I32 max)
2357 {
2358     dTHR;
2359     register char *scan;
2360     register char *opnd;
2361     register I32 c;
2362     register char *loceol = PL_regeol;
2363     register I32 hardcount = 0;
2364
2365     scan = PL_reginput;
2366     if (max != REG_INFTY && max < loceol - scan)
2367       loceol = scan + max;
2368     opnd = (char *) OPERAND(p);
2369     switch (OP(p)) {
2370     case REG_ANY:
2371         while (scan < loceol && *scan != '\n')
2372             scan++;
2373         break;
2374     case SANY:
2375         scan = loceol;
2376         break;
2377     case ANYUTF8:
2378         loceol = PL_regeol;
2379         while (scan < loceol && *scan != '\n') {
2380             scan += UTF8SKIP(scan);
2381             hardcount++;
2382         }
2383         break;
2384     case SANYUTF8:
2385         loceol = PL_regeol;
2386         while (scan < loceol) {
2387             scan += UTF8SKIP(scan);
2388             hardcount++;
2389         }
2390         break;
2391     case EXACT:         /* length of string is 1 */
2392         c = UCHARAT(++opnd);
2393         while (scan < loceol && UCHARAT(scan) == c)
2394             scan++;
2395         break;
2396     case EXACTF:        /* length of string is 1 */
2397         c = UCHARAT(++opnd);
2398         while (scan < loceol &&
2399                (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold[c]))
2400             scan++;
2401         break;
2402     case EXACTFL:       /* length of string is 1 */
2403         PL_reg_flags |= RF_tainted;
2404         c = UCHARAT(++opnd);
2405         while (scan < loceol &&
2406                (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold_locale[c]))
2407             scan++;
2408         break;
2409     case ANYOFUTF8:
2410         loceol = PL_regeol;
2411         while (scan < loceol && REGINCLASSUTF8(p, (U8*)scan)) {
2412             scan += UTF8SKIP(scan);
2413             hardcount++;
2414         }
2415         break;
2416     case ANYOF:
2417         while (scan < loceol && REGINCLASS(opnd, *scan))
2418             scan++;
2419         break;
2420     case ALNUM:
2421         while (scan < loceol && isALNUM(*scan))
2422             scan++;
2423         break;
2424     case ALNUMUTF8:
2425         loceol = PL_regeol;
2426         while (scan < loceol && swash_fetch(PL_utf8_alnum, (U8*)scan)) {
2427             scan += UTF8SKIP(scan);
2428             hardcount++;
2429         }
2430         break;
2431     case ALNUML:
2432         PL_reg_flags |= RF_tainted;
2433         while (scan < loceol && isALNUM_LC(*scan))
2434             scan++;
2435         break;
2436     case ALNUMLUTF8:
2437         PL_reg_flags |= RF_tainted;
2438         loceol = PL_regeol;
2439         while (scan < loceol && isALNUM_LC_utf8((U8*)scan)) {
2440             scan += UTF8SKIP(scan);
2441             hardcount++;
2442         }
2443         break;
2444         break;
2445     case NALNUM:
2446         while (scan < loceol && !isALNUM(*scan))
2447             scan++;
2448         break;
2449     case NALNUMUTF8:
2450         loceol = PL_regeol;
2451         while (scan < loceol && !swash_fetch(PL_utf8_alnum, (U8*)scan)) {
2452             scan += UTF8SKIP(scan);
2453             hardcount++;
2454         }
2455         break;
2456     case NALNUML:
2457         PL_reg_flags |= RF_tainted;
2458         while (scan < loceol && !isALNUM_LC(*scan))
2459             scan++;
2460         break;
2461     case NALNUMLUTF8:
2462         PL_reg_flags |= RF_tainted;
2463         loceol = PL_regeol;
2464         while (scan < loceol && !isALNUM_LC_utf8((U8*)scan)) {
2465             scan += UTF8SKIP(scan);
2466             hardcount++;
2467         }
2468         break;
2469     case SPACE:
2470         while (scan < loceol && isSPACE(*scan))
2471             scan++;
2472         break;
2473     case SPACEUTF8:
2474         loceol = PL_regeol;
2475         while (scan < loceol && (*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
2476             scan += UTF8SKIP(scan);
2477             hardcount++;
2478         }
2479         break;
2480     case SPACEL:
2481         PL_reg_flags |= RF_tainted;
2482         while (scan < loceol && isSPACE_LC(*scan))
2483             scan++;
2484         break;
2485     case SPACELUTF8:
2486         PL_reg_flags |= RF_tainted;
2487         loceol = PL_regeol;
2488         while (scan < loceol && (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
2489             scan += UTF8SKIP(scan);
2490             hardcount++;
2491         }
2492         break;
2493     case NSPACE:
2494         while (scan < loceol && !isSPACE(*scan))
2495             scan++;
2496         break;
2497     case NSPACEUTF8:
2498         loceol = PL_regeol;
2499         while (scan < loceol && !(*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
2500             scan += UTF8SKIP(scan);
2501             hardcount++;
2502         }
2503         break;
2504     case NSPACEL:
2505         PL_reg_flags |= RF_tainted;
2506         while (scan < loceol && !isSPACE_LC(*scan))
2507             scan++;
2508         break;
2509     case NSPACELUTF8:
2510         PL_reg_flags |= RF_tainted;
2511         loceol = PL_regeol;
2512         while (scan < loceol && !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
2513             scan += UTF8SKIP(scan);
2514             hardcount++;
2515         }
2516         break;
2517     case DIGIT:
2518         while (scan < loceol && isDIGIT(*scan))
2519             scan++;
2520         break;
2521     case DIGITUTF8:
2522         loceol = PL_regeol;
2523         while (scan < loceol && swash_fetch(PL_utf8_digit,(U8*)scan)) {
2524             scan += UTF8SKIP(scan);
2525             hardcount++;
2526         }
2527         break;
2528         break;
2529     case NDIGIT:
2530         while (scan < loceol && !isDIGIT(*scan))
2531             scan++;
2532         break;
2533     case NDIGITUTF8:
2534         loceol = PL_regeol;
2535         while (scan < loceol && !swash_fetch(PL_utf8_digit,(U8*)scan)) {
2536             scan += UTF8SKIP(scan);
2537             hardcount++;
2538         }
2539         break;
2540     default:            /* Called on something of 0 width. */
2541         break;          /* So match right here or not at all. */
2542     }
2543
2544     if (hardcount)
2545         c = hardcount;
2546     else
2547         c = scan - PL_reginput;
2548     PL_reginput = scan;
2549
2550     DEBUG_r( 
2551         {
2552                 SV *prop = sv_newmortal();
2553
2554                 regprop(prop, p);
2555                 PerlIO_printf(Perl_debug_log, 
2556                               "%*s  %s can match %ld times out of %ld...\n", 
2557                               REPORT_CODE_OFF+1, "", SvPVX(prop),c,max);
2558         });
2559     
2560     return(c);
2561 }
2562
2563 /*
2564  - regrepeat_hard - repeatedly match something, report total lenth and length
2565  * 
2566  * The repeater is supposed to have constant length.
2567  */
2568
2569 STATIC I32
2570 regrepeat_hard(regnode *p, I32 max, I32 *lp)
2571 {
2572     dTHR;
2573     register char *scan;
2574     register char *start;
2575     register char *loceol = PL_regeol;
2576     I32 l = 0;
2577     I32 count = 0, res = 1;
2578
2579     if (!max)
2580         return 0;
2581
2582     start = PL_reginput;
2583     if (UTF) {
2584         while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
2585             if (!count++) {
2586                 l = 0;
2587                 while (start < PL_reginput) {
2588                     l++;
2589                     start += UTF8SKIP(start);
2590                 }
2591                 *lp = l;
2592                 if (l == 0)
2593                     return max;
2594             }
2595             if (count == max)
2596                 return count;
2597         }
2598     }
2599     else {
2600         while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
2601             if (!count++) {
2602                 *lp = l = PL_reginput - start;
2603                 if (max != REG_INFTY && l*max < loceol - scan)
2604                     loceol = scan + l*max;
2605                 if (l == 0)
2606                     return max;
2607             }
2608         }
2609     }
2610     if (!res)
2611         PL_reginput = scan;
2612     
2613     return count;
2614 }
2615
2616 /*
2617  - regclass - determine if a character falls into a character class
2618  */
2619
2620 STATIC bool
2621 reginclass(register char *p, register I32 c)
2622 {
2623     dTHR;
2624     char flags = *p;
2625     bool match = FALSE;
2626
2627     c &= 0xFF;
2628     if (ANYOF_TEST(p, c))
2629         match = TRUE;
2630     else if (flags & ANYOF_FOLD) {
2631         I32 cf;
2632         if (flags & ANYOF_LOCALE) {
2633             PL_reg_flags |= RF_tainted;
2634             cf = PL_fold_locale[c];
2635         }
2636         else
2637             cf = PL_fold[c];
2638         if (ANYOF_TEST(p, cf))
2639             match = TRUE;
2640     }
2641
2642     if (!match && (flags & ANYOF_ISA)) {
2643         PL_reg_flags |= RF_tainted;
2644
2645         if (((flags & ANYOF_ALNUML)  && isALNUM_LC(c))  ||
2646             ((flags & ANYOF_NALNUML) && !isALNUM_LC(c)) ||
2647             ((flags & ANYOF_SPACEL)  && isSPACE_LC(c))  ||
2648             ((flags & ANYOF_NSPACEL) && !isSPACE_LC(c)))
2649         {
2650             match = TRUE;
2651         }
2652     }
2653
2654     return (flags & ANYOF_INVERT) ? !match : match;
2655 }
2656
2657 STATIC bool
2658 reginclassutf8(regnode *f, U8 *p)
2659 {                                           
2660     dTHR;
2661     char flags = ARG1(f);
2662     bool match = FALSE;
2663     SV *sv = (SV*)PL_regdata->data[ARG2(f)];
2664
2665     if (swash_fetch(sv, p))
2666         match = TRUE;
2667     else if (flags & ANYOF_FOLD) {
2668         I32 cf;
2669         U8 tmpbuf[10];
2670         if (flags & ANYOF_LOCALE) {
2671             PL_reg_flags |= RF_tainted;
2672             uv_to_utf8(tmpbuf, toLOWER_LC_utf8(p));
2673         }
2674         else
2675             uv_to_utf8(tmpbuf, toLOWER_utf8(p));
2676         if (swash_fetch(sv, tmpbuf))
2677             match = TRUE;
2678     }
2679
2680     if (!match && (flags & ANYOF_ISA)) {
2681         PL_reg_flags |= RF_tainted;
2682
2683         if (((flags & ANYOF_ALNUML)  && isALNUM_LC_utf8(p))  ||
2684             ((flags & ANYOF_NALNUML) && !isALNUM_LC_utf8(p)) ||
2685             ((flags & ANYOF_SPACEL)  && isSPACE_LC_utf8(p))  ||
2686             ((flags & ANYOF_NSPACEL) && !isSPACE_LC_utf8(p)))
2687         {
2688             match = TRUE;
2689         }
2690     }
2691
2692     return (flags & ANYOF_INVERT) ? !match : match;
2693 }
2694
2695 STATIC U8 *
2696 reghop(U8 *s, I32 off)
2697 {                               
2698     dTHR;
2699     if (off >= 0) {
2700         while (off-- && s < (U8*)PL_regeol)
2701             s += UTF8SKIP(s);
2702     }
2703     else {
2704         while (off++) {
2705             if (s > (U8*)PL_bostr) {
2706                 s--;
2707                 if (*s & 0x80) {
2708                     while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80)
2709                         s--;
2710                 }               /* XXX could check well-formedness here */
2711             }
2712         }
2713     }
2714     return s;
2715 }
2716
2717 STATIC U8 *
2718 reghopmaybe(U8* s, I32 off)
2719 {
2720     dTHR;
2721     if (off >= 0) {
2722         while (off-- && s < (U8*)PL_regeol)
2723             s += UTF8SKIP(s);
2724         if (off >= 0)
2725             return 0;
2726     }
2727     else {
2728         while (off++) {
2729             if (s > (U8*)PL_bostr) {
2730                 s--;
2731                 if (*s & 0x80) {
2732                     while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80)
2733                         s--;
2734                 }               /* XXX could check well-formedness here */
2735             }
2736             else
2737                 break;
2738         }
2739         if (off <= 0)
2740             return 0;
2741     }
2742     return s;
2743 }