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