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