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