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