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