const declaration fixup
[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 #  if defined(PERL_EXT_RE_DEBUG) && !defined(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 #  define Perl_re_intuit_start my_re_intuit_start
39 /* *These* symbols are masked to allow static link. */
40 #  define Perl_pregexec my_pregexec
41 #  define Perl_reginitcolors my_reginitcolors
42 #  define Perl_regclass_swash my_regclass_swash
43
44 #  define PERL_NO_GET_CONTEXT
45 #endif
46
47 /*SUPPRESS 112*/
48 /*
49  * pregcomp and pregexec -- regsub and regerror are not used in perl
50  *
51  *      Copyright (c) 1986 by University of Toronto.
52  *      Written by Henry Spencer.  Not derived from licensed software.
53  *
54  *      Permission is granted to anyone to use this software for any
55  *      purpose on any computer system, and to redistribute it freely,
56  *      subject to the following restrictions:
57  *
58  *      1. The author is not responsible for the consequences of use of
59  *              this software, no matter how awful, even if they arise
60  *              from defects in it.
61  *
62  *      2. The origin of this software must not be misrepresented, either
63  *              by explicit claim or by omission.
64  *
65  *      3. Altered versions must be plainly marked as such, and must not
66  *              be misrepresented as being the original software.
67  *
68  ****    Alterations to Henry's code are...
69  ****
70  ****    Copyright (c) 1991-2002, Larry Wall
71  ****
72  ****    You may distribute under the terms of either the GNU General Public
73  ****    License or the Artistic License, as specified in the README file.
74  *
75  * Beware that some of this code is subtly aware of the way operator
76  * precedence is structured in regular expressions.  Serious changes in
77  * regular-expression syntax might require a total rethink.
78  */
79 #include "EXTERN.h"
80 #define PERL_IN_REGEXEC_C
81 #include "perl.h"
82
83 #include "regcomp.h"
84
85 #define RF_tainted      1               /* tainted information used? */
86 #define RF_warned       2               /* warned about big count? */
87 #define RF_evaled       4               /* Did an EVAL with setting? */
88 #define RF_utf8         8               /* String contains multibyte chars? */
89
90 #define UTF ((PL_reg_flags & RF_utf8) != 0)
91
92 #define RS_init         1               /* eval environment created */
93 #define RS_set          2               /* replsv value is set */
94
95 #ifndef STATIC
96 #define STATIC  static
97 #endif
98
99 #define REGINCLASS(p,c)  (ANYOF_FLAGS(p) ? reginclass(p,c,0,0) : ANYOF_BITMAP_TEST(p,*(c)))
100
101 /*
102  * Forwards.
103  */
104
105 #define CHR_SVLEN(sv) (do_utf8 ? sv_len_utf8(sv) : SvCUR(sv))
106 #define CHR_DIST(a,b) (PL_reg_match_utf8 ? utf8_distance(a,b) : a - b)
107
108 #define reghop_c(pos,off) ((char*)reghop((U8*)pos, off))
109 #define reghopmaybe_c(pos,off) ((char*)reghopmaybe((U8*)pos, off))
110 #define HOP(pos,off) (PL_reg_match_utf8 ? reghop((U8*)pos, off) : (U8*)(pos + off))
111 #define HOPMAYBE(pos,off) (PL_reg_match_utf8 ? reghopmaybe((U8*)pos, off) : (U8*)(pos + off))
112 #define HOPc(pos,off) ((char*)HOP(pos,off))
113 #define HOPMAYBEc(pos,off) ((char*)HOPMAYBE(pos,off))
114
115 #define HOPBACK(pos, off) (             \
116     (PL_reg_match_utf8)                 \
117         ? reghopmaybe((U8*)pos, -off)   \
118     : (pos - off >= PL_bostr)           \
119         ? (U8*)(pos - off)              \
120     : (U8*)NULL                         \
121 )
122 #define HOPBACKc(pos, off) (char*)HOPBACK(pos, off)
123
124 #define reghop3_c(pos,off,lim) ((char*)reghop3((U8*)pos, off, (U8*)lim))
125 #define reghopmaybe3_c(pos,off,lim) ((char*)reghopmaybe3((U8*)pos, off, (U8*)lim))
126 #define HOP3(pos,off,lim) (PL_reg_match_utf8 ? reghop3((U8*)pos, off, (U8*)lim) : (U8*)(pos + off))
127 #define HOPMAYBE3(pos,off,lim) (PL_reg_match_utf8 ? reghopmaybe3((U8*)pos, off, (U8*)lim) : (U8*)(pos + off))
128 #define HOP3c(pos,off,lim) ((char*)HOP3(pos,off,lim))
129 #define HOPMAYBE3c(pos,off,lim) ((char*)HOPMAYBE3(pos,off,lim))
130
131 #define LOAD_UTF8_CHARCLASS(a,b) STMT_START { if (!CAT2(PL_utf8_,a)) { ENTER; save_re_context(); (void)CAT2(is_utf8_, a)((U8*)b); LEAVE; } } STMT_END
132
133 /* for use after a quantifier and before an EXACT-like node -- japhy */
134 #define JUMPABLE(rn) ( \
135     OP(rn) == OPEN || OP(rn) == CLOSE || OP(rn) == EVAL || \
136     OP(rn) == SUSPEND || OP(rn) == IFMATCH || \
137     OP(rn) == PLUS || OP(rn) == MINMOD || \
138     (PL_regkind[(U8)OP(rn)] == CURLY && ARG1(rn) > 0) \
139 )
140
141 #define HAS_TEXT(rn) ( \
142     PL_regkind[(U8)OP(rn)] == EXACT || PL_regkind[(U8)OP(rn)] == REF \
143 )
144
145 /*
146   Search for mandatory following text node; for lookahead, the text must
147   follow but for lookbehind (rn->flags != 0) we skip to the next step.
148 */
149 #define FIND_NEXT_IMPT(rn) STMT_START { \
150     while (JUMPABLE(rn)) \
151         if (OP(rn) == SUSPEND || PL_regkind[(U8)OP(rn)] == CURLY) \
152             rn = NEXTOPER(NEXTOPER(rn)); \
153         else if (OP(rn) == PLUS) \
154             rn = NEXTOPER(rn); \
155         else if (OP(rn) == IFMATCH) \
156             rn = (rn->flags == 0) ? NEXTOPER(NEXTOPER(rn)) : rn + ARG(rn); \
157         else rn += NEXT_OFF(rn); \
158 } STMT_END 
159
160 static void restore_pos(pTHX_ void *arg);
161
162 STATIC CHECKPOINT
163 S_regcppush(pTHX_ I32 parenfloor)
164 {
165     int retval = PL_savestack_ix;
166 #define REGCP_PAREN_ELEMS 4
167     int paren_elems_to_push = (PL_regsize - parenfloor) * REGCP_PAREN_ELEMS;
168     int p;
169
170     if (paren_elems_to_push < 0)
171         Perl_croak(aTHX_ "panic: paren_elems_to_push < 0");
172
173 #define REGCP_OTHER_ELEMS 6
174     SSCHECK(paren_elems_to_push + REGCP_OTHER_ELEMS);
175     for (p = PL_regsize; p > parenfloor; p--) {
176 /* REGCP_PARENS_ELEMS are pushed per pairs of parentheses. */
177         SSPUSHINT(PL_regendp[p]);
178         SSPUSHINT(PL_regstartp[p]);
179         SSPUSHPTR(PL_reg_start_tmp[p]);
180         SSPUSHINT(p);
181     }
182 /* REGCP_OTHER_ELEMS are pushed in any case, parentheses or no. */
183     SSPUSHINT(PL_regsize);
184     SSPUSHINT(*PL_reglastparen);
185     SSPUSHINT(*PL_reglastcloseparen);
186     SSPUSHPTR(PL_reginput);
187 #define REGCP_FRAME_ELEMS 2
188 /* REGCP_FRAME_ELEMS are part of the REGCP_OTHER_ELEMS and
189  * are needed for the regexp context stack bookkeeping. */
190     SSPUSHINT(paren_elems_to_push + REGCP_OTHER_ELEMS - REGCP_FRAME_ELEMS);
191     SSPUSHINT(SAVEt_REGCONTEXT); /* Magic cookie. */
192
193     return retval;
194 }
195
196 /* These are needed since we do not localize EVAL nodes: */
197 #  define REGCP_SET(cp)  DEBUG_r(PerlIO_printf(Perl_debug_log,          \
198                              "  Setting an EVAL scope, savestack=%"IVdf"\n",    \
199                              (IV)PL_savestack_ix)); cp = PL_savestack_ix
200
201 #  define REGCP_UNWIND(cp)  DEBUG_r(cp != PL_savestack_ix ?             \
202                                 PerlIO_printf(Perl_debug_log,           \
203                                 "  Clearing an EVAL scope, savestack=%"IVdf"..%"IVdf"\n", \
204                                 (IV)(cp), (IV)PL_savestack_ix) : 0); regcpblow(cp)
205
206 STATIC char *
207 S_regcppop(pTHX)
208 {
209     I32 i;
210     U32 paren = 0;
211     char *input;
212     I32 tmps;
213
214     /* Pop REGCP_OTHER_ELEMS before the parentheses loop starts. */
215     i = SSPOPINT;
216     assert(i == SAVEt_REGCONTEXT); /* Check that the magic cookie is there. */
217     i = SSPOPINT; /* Parentheses elements to pop. */
218     input = (char *) SSPOPPTR;
219     *PL_reglastcloseparen = SSPOPINT;
220     *PL_reglastparen = SSPOPINT;
221     PL_regsize = SSPOPINT;
222
223     /* Now restore the parentheses context. */
224     for (i -= (REGCP_OTHER_ELEMS - REGCP_FRAME_ELEMS);
225          i > 0; i -= REGCP_PAREN_ELEMS) {
226         paren = (U32)SSPOPINT;
227         PL_reg_start_tmp[paren] = (char *) SSPOPPTR;
228         PL_regstartp[paren] = SSPOPINT;
229         tmps = SSPOPINT;
230         if (paren <= *PL_reglastparen)
231             PL_regendp[paren] = tmps;
232         DEBUG_r(
233             PerlIO_printf(Perl_debug_log,
234                           "     restoring \\%"UVuf" to %"IVdf"(%"IVdf")..%"IVdf"%s\n",
235                           (UV)paren, (IV)PL_regstartp[paren],
236                           (IV)(PL_reg_start_tmp[paren] - PL_bostr),
237                           (IV)PL_regendp[paren],
238                           (paren > *PL_reglastparen ? "(no)" : ""));
239         );
240     }
241     DEBUG_r(
242         if ((I32)(*PL_reglastparen + 1) <= PL_regnpar) {
243             PerlIO_printf(Perl_debug_log,
244                           "     restoring \\%"IVdf"..\\%"IVdf" to undef\n",
245                           (IV)(*PL_reglastparen + 1), (IV)PL_regnpar);
246         }
247     );
248 #if 1
249     /* It would seem that the similar code in regtry()
250      * already takes care of this, and in fact it is in
251      * a better location to since this code can #if 0-ed out
252      * but the code in regtry() is needed or otherwise tests
253      * requiring null fields (pat.t#187 and split.t#{13,14}
254      * (as of patchlevel 7877)  will fail.  Then again,
255      * this code seems to be necessary or otherwise
256      * building DynaLoader will fail:
257      * "Error: '*' not in typemap in DynaLoader.xs, line 164"
258      * --jhi */
259     for (paren = *PL_reglastparen + 1; (I32)paren <= PL_regnpar; paren++) {
260         if ((I32)paren > PL_regsize)
261             PL_regstartp[paren] = -1;
262         PL_regendp[paren] = -1;
263     }
264 #endif
265     return input;
266 }
267
268 STATIC char *
269 S_regcp_set_to(pTHX_ I32 ss)
270 {
271     I32 tmp = PL_savestack_ix;
272
273     PL_savestack_ix = ss;
274     regcppop();
275     PL_savestack_ix = tmp;
276     return Nullch;
277 }
278
279 typedef struct re_cc_state
280 {
281     I32 ss;
282     regnode *node;
283     struct re_cc_state *prev;
284     CURCUR *cc;
285     regexp *re;
286 } re_cc_state;
287
288 #define regcpblow(cp) LEAVE_SCOPE(cp)   /* Ignores regcppush()ed data. */
289
290 #define TRYPAREN(paren, n, input) {                             \
291     if (paren) {                                                \
292         if (n) {                                                \
293             PL_regstartp[paren] = HOPc(input, -1) - PL_bostr;   \
294             PL_regendp[paren] = input - PL_bostr;               \
295         }                                                       \
296         else                                                    \
297             PL_regendp[paren] = -1;                             \
298     }                                                           \
299     if (regmatch(next))                                         \
300         sayYES;                                                 \
301     if (paren && n)                                             \
302         PL_regendp[paren] = -1;                                 \
303 }
304
305
306 /*
307  * pregexec and friends
308  */
309
310 /*
311  - pregexec - match a regexp against a string
312  */
313 I32
314 Perl_pregexec(pTHX_ register regexp *prog, char *stringarg, register char *strend,
315          char *strbeg, I32 minend, SV *screamer, U32 nosave)
316 /* strend: pointer to null at end of string */
317 /* strbeg: real beginning of string */
318 /* minend: end of match must be >=minend after stringarg. */
319 /* nosave: For optimizations. */
320 {
321     return
322         regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
323                       nosave ? 0 : REXEC_COPY_STR);
324 }
325
326 STATIC void
327 S_cache_re(pTHX_ regexp *prog)
328 {
329     PL_regprecomp = prog->precomp;              /* Needed for FAIL. */
330 #ifdef DEBUGGING
331     PL_regprogram = prog->program;
332 #endif
333     PL_regnpar = prog->nparens;
334     PL_regdata = prog->data;
335     PL_reg_re = prog;
336 }
337
338 /*
339  * Need to implement the following flags for reg_anch:
340  *
341  * USE_INTUIT_NOML              - Useful to call re_intuit_start() first
342  * USE_INTUIT_ML
343  * INTUIT_AUTORITATIVE_NOML     - Can trust a positive answer
344  * INTUIT_AUTORITATIVE_ML
345  * INTUIT_ONCE_NOML             - Intuit can match in one location only.
346  * INTUIT_ONCE_ML
347  *
348  * Another flag for this function: SECOND_TIME (so that float substrs
349  * with giant delta may be not rechecked).
350  */
351
352 /* Assumptions: if ANCH_GPOS, then strpos is anchored. XXXX Check GPOS logic */
353
354 /* If SCREAM, then SvPVX(sv) should be compatible with strpos and strend.
355    Otherwise, only SvCUR(sv) is used to get strbeg. */
356
357 /* XXXX We assume that strpos is strbeg unless sv. */
358
359 /* XXXX Some places assume that there is a fixed substring.
360         An update may be needed if optimizer marks as "INTUITable"
361         RExen without fixed substrings.  Similarly, it is assumed that
362         lengths of all the strings are no more than minlen, thus they
363         cannot come from lookahead.
364         (Or minlen should take into account lookahead.) */
365
366 /* A failure to find a constant substring means that there is no need to make
367    an expensive call to REx engine, thus we celebrate a failure.  Similarly,
368    finding a substring too deep into the string means that less calls to
369    regtry() should be needed.
370
371    REx compiler's optimizer found 4 possible hints:
372         a) Anchored substring;
373         b) Fixed substring;
374         c) Whether we are anchored (beginning-of-line or \G);
375         d) First node (of those at offset 0) which may distingush positions;
376    We use a)b)d) and multiline-part of c), and try to find a position in the
377    string which does not contradict any of them.
378  */
379
380 /* Most of decisions we do here should have been done at compile time.
381    The nodes of the REx which we used for the search should have been
382    deleted from the finite automaton. */
383
384 char *
385 Perl_re_intuit_start(pTHX_ regexp *prog, SV *sv, char *strpos,
386                      char *strend, U32 flags, re_scream_pos_data *data)
387 {
388     register I32 start_shift = 0;
389     /* Should be nonnegative! */
390     register I32 end_shift   = 0;
391     register char *s;
392     register SV *check;
393     char *strbeg;
394     char *t;
395     int do_utf8 = sv ? SvUTF8(sv) : 0;  /* if no sv we have to assume bytes */
396     I32 ml_anch;
397     register char *other_last = Nullch; /* other substr checked before this */
398     char *check_at = Nullch;            /* check substr found at this pos */
399 #ifdef DEBUGGING
400     char *i_strpos = strpos;
401     SV *dsv = PERL_DEBUG_PAD_ZERO(0);
402 #endif
403
404     if (prog->reganch & ROPT_UTF8) {
405         DEBUG_r(PerlIO_printf(Perl_debug_log,
406                               "UTF-8 regex...\n"));
407         PL_reg_flags |= RF_utf8;
408     }
409
410     DEBUG_r({
411          char *s   = PL_reg_match_utf8 ?
412                          sv_uni_display(dsv, sv, 60, UNI_DISPLAY_REGEX) :
413                          strpos;
414          int   len = PL_reg_match_utf8 ?
415                          strlen(s) : strend - strpos;
416          if (!PL_colorset)
417               reginitcolors();
418          if (PL_reg_match_utf8)
419              DEBUG_r(PerlIO_printf(Perl_debug_log,
420                                    "UTF-8 target...\n"));
421          PerlIO_printf(Perl_debug_log,
422                        "%sGuessing start of match, REx%s `%s%.60s%s%s' against `%s%.*s%s%s'...\n",
423                        PL_colors[4],PL_colors[5],PL_colors[0],
424                        prog->precomp,
425                        PL_colors[1],
426                        (strlen(prog->precomp) > 60 ? "..." : ""),
427                        PL_colors[0],
428                        (int)(len > 60 ? 60 : len),
429                        s, PL_colors[1],
430                        (len > 60 ? "..." : "")
431               );
432     });
433
434     /* CHR_DIST() would be more correct here but it makes things slow. */
435     if (prog->minlen > strend - strpos) {
436         DEBUG_r(PerlIO_printf(Perl_debug_log,
437                               "String too short... [re_intuit_start]\n"));
438         goto fail;
439     }
440     strbeg = (sv && SvPOK(sv)) ? strend - SvCUR(sv) : strpos;
441     PL_regeol = strend;
442     if (do_utf8) {
443         if (!prog->check_utf8 && prog->check_substr)
444             to_utf8_substr(prog);
445         check = prog->check_utf8;
446     } else {
447         if (!prog->check_substr && prog->check_utf8)
448             to_byte_substr(prog);
449         check = prog->check_substr;
450     }
451    if (check == &PL_sv_undef) {
452         DEBUG_r(PerlIO_printf(Perl_debug_log,
453                 "Non-utf string cannot match utf check string\n"));
454         goto fail;
455     }
456     if (prog->reganch & ROPT_ANCH) {    /* Match at beg-of-str or after \n */
457         ml_anch = !( (prog->reganch & ROPT_ANCH_SINGLE)
458                      || ( (prog->reganch & ROPT_ANCH_BOL)
459                           && !PL_multiline ) ); /* Check after \n? */
460
461         if (!ml_anch) {
462           if ( !(prog->reganch & (ROPT_ANCH_GPOS /* Checked by the caller */
463                                   | ROPT_IMPLICIT)) /* not a real BOL */
464                /* SvCUR is not set on references: SvRV and SvPVX overlap */
465                && sv && !SvROK(sv)
466                && (strpos != strbeg)) {
467               DEBUG_r(PerlIO_printf(Perl_debug_log, "Not at start...\n"));
468               goto fail;
469           }
470           if (prog->check_offset_min == prog->check_offset_max &&
471               !(prog->reganch & ROPT_CANY_SEEN)) {
472             /* Substring at constant offset from beg-of-str... */
473             I32 slen;
474
475             s = HOP3c(strpos, prog->check_offset_min, strend);
476             if (SvTAIL(check)) {
477                 slen = SvCUR(check);    /* >= 1 */
478
479                 if ( strend - s > slen || strend - s < slen - 1
480                      || (strend - s == slen && strend[-1] != '\n')) {
481                     DEBUG_r(PerlIO_printf(Perl_debug_log, "String too long...\n"));
482                     goto fail_finish;
483                 }
484                 /* Now should match s[0..slen-2] */
485                 slen--;
486                 if (slen && (*SvPVX(check) != *s
487                              || (slen > 1
488                                  && memNE(SvPVX(check), s, slen)))) {
489                   report_neq:
490                     DEBUG_r(PerlIO_printf(Perl_debug_log, "String not equal...\n"));
491                     goto fail_finish;
492                 }
493             }
494             else if (*SvPVX(check) != *s
495                      || ((slen = SvCUR(check)) > 1
496                          && memNE(SvPVX(check), s, slen)))
497                 goto report_neq;
498             goto success_at_start;
499           }
500         }
501         /* Match is anchored, but substr is not anchored wrt beg-of-str. */
502         s = strpos;
503         start_shift = prog->check_offset_min; /* okay to underestimate on CC */
504         end_shift = prog->minlen - start_shift -
505             CHR_SVLEN(check) + (SvTAIL(check) != 0);
506         if (!ml_anch) {
507             I32 end = prog->check_offset_max + CHR_SVLEN(check)
508                                          - (SvTAIL(check) != 0);
509             I32 eshift = CHR_DIST((U8*)strend, (U8*)s) - end;
510
511             if (end_shift < eshift)
512                 end_shift = eshift;
513         }
514     }
515     else {                              /* Can match at random position */
516         ml_anch = 0;
517         s = strpos;
518         start_shift = prog->check_offset_min; /* okay to underestimate on CC */
519         /* Should be nonnegative! */
520         end_shift = prog->minlen - start_shift -
521             CHR_SVLEN(check) + (SvTAIL(check) != 0);
522     }
523
524 #ifdef DEBUGGING        /* 7/99: reports of failure (with the older version) */
525     if (end_shift < 0)
526         Perl_croak(aTHX_ "panic: end_shift");
527 #endif
528
529   restart:
530     /* Find a possible match in the region s..strend by looking for
531        the "check" substring in the region corrected by start/end_shift. */
532     if (flags & REXEC_SCREAM) {
533         I32 p = -1;                     /* Internal iterator of scream. */
534         I32 *pp = data ? data->scream_pos : &p;
535
536         if (PL_screamfirst[BmRARE(check)] >= 0
537             || ( BmRARE(check) == '\n'
538                  && (BmPREVIOUS(check) == SvCUR(check) - 1)
539                  && SvTAIL(check) ))
540             s = screaminstr(sv, check,
541                             start_shift + (s - strbeg), end_shift, pp, 0);
542         else
543             goto fail_finish;
544         /* we may be pointing at the wrong string */
545         if (s && RX_MATCH_COPIED(prog))
546             s = prog->subbeg + (s - SvPVX(sv));
547         if (data)
548             *data->scream_olds = s;
549     }
550     else if (prog->reganch & ROPT_CANY_SEEN)
551         s = fbm_instr((U8*)(s + start_shift),
552                       (U8*)(strend - end_shift),
553                       check, PL_multiline ? FBMrf_MULTILINE : 0);
554     else
555         s = fbm_instr(HOP3(s, start_shift, strend),
556                       HOP3(strend, -end_shift, strbeg),
557                       check, PL_multiline ? FBMrf_MULTILINE : 0);
558
559     /* Update the count-of-usability, remove useless subpatterns,
560         unshift s.  */
561
562     DEBUG_r(PerlIO_printf(Perl_debug_log, "%s %s substr `%s%.*s%s'%s%s",
563                           (s ? "Found" : "Did not find"),
564                           (check == (do_utf8 ? prog->anchored_utf8 : prog->anchored_substr) ? "anchored" : "floating"),
565                           PL_colors[0],
566                           (int)(SvCUR(check) - (SvTAIL(check)!=0)),
567                           SvPVX(check),
568                           PL_colors[1], (SvTAIL(check) ? "$" : ""),
569                           (s ? " at offset " : "...\n") ) );
570
571     if (!s)
572         goto fail_finish;
573
574     check_at = s;
575
576     /* Finish the diagnostic message */
577     DEBUG_r(PerlIO_printf(Perl_debug_log, "%ld...\n", (long)(s - i_strpos)) );
578
579     /* Got a candidate.  Check MBOL anchoring, and the *other* substr.
580        Start with the other substr.
581        XXXX no SCREAM optimization yet - and a very coarse implementation
582        XXXX /ttx+/ results in anchored=`ttx', floating=`x'.  floating will
583                 *always* match.  Probably should be marked during compile...
584        Probably it is right to do no SCREAM here...
585      */
586
587     if (do_utf8 ? (prog->float_utf8 && prog->anchored_utf8) : (prog->float_substr && prog->anchored_substr)) {
588         /* Take into account the "other" substring. */
589         /* XXXX May be hopelessly wrong for UTF... */
590         if (!other_last)
591             other_last = strpos;
592         if (check == (do_utf8 ? prog->float_utf8 : prog->float_substr)) {
593           do_other_anchored:
594             {
595                 char *last = HOP3c(s, -start_shift, strbeg), *last1, *last2;
596                 char *s1 = s;
597                 SV* must;
598
599                 t = s - prog->check_offset_max;
600                 if (s - strpos > prog->check_offset_max  /* signed-corrected t > strpos */
601                     && (!do_utf8
602                         || ((t = reghopmaybe3_c(s, -(prog->check_offset_max), strpos))
603                             && t > strpos)))
604                     /* EMPTY */;
605                 else
606                     t = strpos;
607                 t = HOP3c(t, prog->anchored_offset, strend);
608                 if (t < other_last)     /* These positions already checked */
609                     t = other_last;
610                 last2 = last1 = HOP3c(strend, -prog->minlen, strbeg);
611                 if (last < last1)
612                     last1 = last;
613  /* XXXX It is not documented what units *_offsets are in.  Assume bytes.  */
614                 /* On end-of-str: see comment below. */
615                 must = do_utf8 ? prog->anchored_utf8 : prog->anchored_substr;
616                 if (must == &PL_sv_undef) {
617                     s = (char*)NULL;
618                     DEBUG_r(must = prog->anchored_utf8);        /* for debug */
619                 }
620                 else
621                     s = fbm_instr(
622                         (unsigned char*)t,
623                         HOP3(HOP3(last1, prog->anchored_offset, strend)
624                                 + SvCUR(must), -(SvTAIL(must)!=0), strbeg),
625                         must,
626                         PL_multiline ? FBMrf_MULTILINE : 0
627                     );
628                 DEBUG_r(PerlIO_printf(Perl_debug_log,
629                         "%s anchored substr `%s%.*s%s'%s",
630                         (s ? "Found" : "Contradicts"),
631                         PL_colors[0],
632                           (int)(SvCUR(must)
633                           - (SvTAIL(must)!=0)),
634                           SvPVX(must),
635                           PL_colors[1], (SvTAIL(must) ? "$" : "")));
636                 if (!s) {
637                     if (last1 >= last2) {
638                         DEBUG_r(PerlIO_printf(Perl_debug_log,
639                                                 ", giving up...\n"));
640                         goto fail_finish;
641                     }
642                     DEBUG_r(PerlIO_printf(Perl_debug_log,
643                         ", trying floating at offset %ld...\n",
644                         (long)(HOP3c(s1, 1, strend) - i_strpos)));
645                     other_last = HOP3c(last1, prog->anchored_offset+1, strend);
646                     s = HOP3c(last, 1, strend);
647                     goto restart;
648                 }
649                 else {
650                     DEBUG_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
651                           (long)(s - i_strpos)));
652                     t = HOP3c(s, -prog->anchored_offset, strbeg);
653                     other_last = HOP3c(s, 1, strend);
654                     s = s1;
655                     if (t == strpos)
656                         goto try_at_start;
657                     goto try_at_offset;
658                 }
659             }
660         }
661         else {          /* Take into account the floating substring. */
662             char *last, *last1;
663             char *s1 = s;
664             SV* must;
665
666             t = HOP3c(s, -start_shift, strbeg);
667             last1 = last =
668                 HOP3c(strend, -prog->minlen + prog->float_min_offset, strbeg);
669             if (CHR_DIST((U8*)last, (U8*)t) > prog->float_max_offset)
670                 last = HOP3c(t, prog->float_max_offset, strend);
671             s = HOP3c(t, prog->float_min_offset, strend);
672             if (s < other_last)
673                 s = other_last;
674  /* XXXX It is not documented what units *_offsets are in.  Assume bytes.  */
675             must = do_utf8 ? prog->float_utf8 : prog->float_substr;
676             /* fbm_instr() takes into account exact value of end-of-str
677                if the check is SvTAIL(ed).  Since false positives are OK,
678                and end-of-str is not later than strend we are OK. */
679             if (must == &PL_sv_undef) {
680                 s = (char*)NULL;
681                 DEBUG_r(must = prog->float_utf8);       /* for debug message */
682             }
683             else
684                 s = fbm_instr((unsigned char*)s,
685                               (unsigned char*)last + SvCUR(must)
686                                   - (SvTAIL(must)!=0),
687                               must, PL_multiline ? FBMrf_MULTILINE : 0);
688             DEBUG_r(PerlIO_printf(Perl_debug_log, "%s floating substr `%s%.*s%s'%s",
689                     (s ? "Found" : "Contradicts"),
690                     PL_colors[0],
691                       (int)(SvCUR(must) - (SvTAIL(must)!=0)),
692                       SvPVX(must),
693                       PL_colors[1], (SvTAIL(must) ? "$" : "")));
694             if (!s) {
695                 if (last1 == last) {
696                     DEBUG_r(PerlIO_printf(Perl_debug_log,
697                                             ", giving up...\n"));
698                     goto fail_finish;
699                 }
700                 DEBUG_r(PerlIO_printf(Perl_debug_log,
701                     ", trying anchored starting at offset %ld...\n",
702                     (long)(s1 + 1 - i_strpos)));
703                 other_last = last;
704                 s = HOP3c(t, 1, strend);
705                 goto restart;
706             }
707             else {
708                 DEBUG_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
709                       (long)(s - i_strpos)));
710                 other_last = s; /* Fix this later. --Hugo */
711                 s = s1;
712                 if (t == strpos)
713                     goto try_at_start;
714                 goto try_at_offset;
715             }
716         }
717     }
718
719     t = s - prog->check_offset_max;
720     if (s - strpos > prog->check_offset_max  /* signed-corrected t > strpos */
721         && (!do_utf8
722             || ((t = reghopmaybe3_c(s, -prog->check_offset_max, strpos))
723                  && t > strpos))) {
724         /* Fixed substring is found far enough so that the match
725            cannot start at strpos. */
726       try_at_offset:
727         if (ml_anch && t[-1] != '\n') {
728             /* Eventually fbm_*() should handle this, but often
729                anchored_offset is not 0, so this check will not be wasted. */
730             /* XXXX In the code below we prefer to look for "^" even in
731                presence of anchored substrings.  And we search even
732                beyond the found float position.  These pessimizations
733                are historical artefacts only.  */
734           find_anchor:
735             while (t < strend - prog->minlen) {
736                 if (*t == '\n') {
737                     if (t < check_at - prog->check_offset_min) {
738                         if (do_utf8 ? prog->anchored_utf8 : prog->anchored_substr) {
739                             /* Since we moved from the found position,
740                                we definitely contradict the found anchored
741                                substr.  Due to the above check we do not
742                                contradict "check" substr.
743                                Thus we can arrive here only if check substr
744                                is float.  Redo checking for "other"=="fixed".
745                              */
746                             strpos = t + 1;                     
747                             DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld, rescanning for anchored from offset %ld...\n",
748                                 PL_colors[0],PL_colors[1], (long)(strpos - i_strpos), (long)(strpos - i_strpos + prog->anchored_offset)));
749                             goto do_other_anchored;
750                         }
751                         /* We don't contradict the found floating substring. */
752                         /* XXXX Why not check for STCLASS? */
753                         s = t + 1;
754                         DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld...\n",
755                             PL_colors[0],PL_colors[1], (long)(s - i_strpos)));
756                         goto set_useful;
757                     }
758                     /* Position contradicts check-string */
759                     /* XXXX probably better to look for check-string
760                        than for "\n", so one should lower the limit for t? */
761                     DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m, restarting lookup for check-string at offset %ld...\n",
762                         PL_colors[0],PL_colors[1], (long)(t + 1 - i_strpos)));
763                     other_last = strpos = s = t + 1;
764                     goto restart;
765                 }
766                 t++;
767             }
768             DEBUG_r(PerlIO_printf(Perl_debug_log, "Did not find /%s^%s/m...\n",
769                         PL_colors[0],PL_colors[1]));
770             goto fail_finish;
771         }
772         else {
773             DEBUG_r(PerlIO_printf(Perl_debug_log, "Starting position does not contradict /%s^%s/m...\n",
774                         PL_colors[0],PL_colors[1]));
775         }
776         s = t;
777       set_useful:
778         ++BmUSEFUL(do_utf8 ? prog->check_utf8 : prog->check_substr);    /* hooray/5 */
779     }
780     else {
781         /* The found string does not prohibit matching at strpos,
782            - no optimization of calling REx engine can be performed,
783            unless it was an MBOL and we are not after MBOL,
784            or a future STCLASS check will fail this. */
785       try_at_start:
786         /* Even in this situation we may use MBOL flag if strpos is offset
787            wrt the start of the string. */
788         if (ml_anch && sv && !SvROK(sv) /* See prev comment on SvROK */
789             && (strpos != strbeg) && strpos[-1] != '\n'
790             /* May be due to an implicit anchor of m{.*foo}  */
791             && !(prog->reganch & ROPT_IMPLICIT))
792         {
793             t = strpos;
794             goto find_anchor;
795         }
796         DEBUG_r( if (ml_anch)
797             PerlIO_printf(Perl_debug_log, "Position at offset %ld does not contradict /%s^%s/m...\n",
798                         (long)(strpos - i_strpos), PL_colors[0],PL_colors[1]);
799         );
800       success_at_start:
801         if (!(prog->reganch & ROPT_NAUGHTY)     /* XXXX If strpos moved? */
802             && (do_utf8 ? (
803                 prog->check_utf8                /* Could be deleted already */
804                 && --BmUSEFUL(prog->check_utf8) < 0
805                 && (prog->check_utf8 == prog->float_utf8)
806             ) : (
807                 prog->check_substr              /* Could be deleted already */
808                 && --BmUSEFUL(prog->check_substr) < 0
809                 && (prog->check_substr == prog->float_substr)
810             )))
811         {
812             /* If flags & SOMETHING - do not do it many times on the same match */
813             DEBUG_r(PerlIO_printf(Perl_debug_log, "... Disabling check substring...\n"));
814             SvREFCNT_dec(do_utf8 ? prog->check_utf8 : prog->check_substr);
815             if (do_utf8 ? prog->check_substr : prog->check_utf8)
816                 SvREFCNT_dec(do_utf8 ? prog->check_substr : prog->check_utf8);
817             prog->check_substr = prog->check_utf8 = Nullsv;     /* disable */
818             prog->float_substr = prog->float_utf8 = Nullsv;     /* clear */
819             check = Nullsv;                     /* abort */
820             s = strpos;
821             /* XXXX This is a remnant of the old implementation.  It
822                     looks wasteful, since now INTUIT can use many
823                     other heuristics. */
824             prog->reganch &= ~RE_USE_INTUIT;
825         }
826         else
827             s = strpos;
828     }
829
830     /* Last resort... */
831     /* XXXX BmUSEFUL already changed, maybe multiple change is meaningful... */
832     if (prog->regstclass) {
833         /* minlen == 0 is possible if regstclass is \b or \B,
834            and the fixed substr is ''$.
835            Since minlen is already taken into account, s+1 is before strend;
836            accidentally, minlen >= 1 guaranties no false positives at s + 1
837            even for \b or \B.  But (minlen? 1 : 0) below assumes that
838            regstclass does not come from lookahead...  */
839         /* If regstclass takes bytelength more than 1: If charlength==1, OK.
840            This leaves EXACTF only, which is dealt with in find_byclass().  */
841         U8* str = (U8*)STRING(prog->regstclass);
842         int cl_l = (PL_regkind[(U8)OP(prog->regstclass)] == EXACT
843                     ? CHR_DIST(str+STR_LEN(prog->regstclass), str)
844                     : 1);
845         char *endpos = (prog->anchored_substr || prog->anchored_utf8 || ml_anch)
846                 ? HOP3c(s, (prog->minlen ? cl_l : 0), strend)
847                 : (prog->float_substr || prog->float_utf8
848                    ? HOP3c(HOP3c(check_at, -start_shift, strbeg),
849                            cl_l, strend)
850                    : strend);
851         char *startpos = strbeg;
852
853         t = s;
854         if (prog->reganch & ROPT_UTF8) {        
855             PL_regdata = prog->data;
856             PL_bostr = startpos;
857         }
858         s = find_byclass(prog, prog->regstclass, s, endpos, startpos, 1);
859         if (!s) {
860 #ifdef DEBUGGING
861             char *what = 0;
862 #endif
863             if (endpos == strend) {
864                 DEBUG_r( PerlIO_printf(Perl_debug_log,
865                                 "Could not match STCLASS...\n") );
866                 goto fail;
867             }
868             DEBUG_r( PerlIO_printf(Perl_debug_log,
869                                    "This position contradicts STCLASS...\n") );
870             if ((prog->reganch & ROPT_ANCH) && !ml_anch)
871                 goto fail;
872             /* Contradict one of substrings */
873             if (prog->anchored_substr || prog->anchored_utf8) {
874                 if ((do_utf8 ? prog->anchored_utf8 : prog->anchored_substr) == check) {
875                     DEBUG_r( what = "anchored" );
876                   hop_and_restart:
877                     s = HOP3c(t, 1, strend);
878                     if (s + start_shift + end_shift > strend) {
879                         /* XXXX Should be taken into account earlier? */
880                         DEBUG_r( PerlIO_printf(Perl_debug_log,
881                                                "Could not match STCLASS...\n") );
882                         goto fail;
883                     }
884                     if (!check)
885                         goto giveup;
886                     DEBUG_r( PerlIO_printf(Perl_debug_log,
887                                 "Looking for %s substr starting at offset %ld...\n",
888                                  what, (long)(s + start_shift - i_strpos)) );
889                     goto restart;
890                 }
891                 /* Have both, check_string is floating */
892                 if (t + start_shift >= check_at) /* Contradicts floating=check */
893                     goto retry_floating_check;
894                 /* Recheck anchored substring, but not floating... */
895                 s = check_at;
896                 if (!check)
897                     goto giveup;
898                 DEBUG_r( PerlIO_printf(Perl_debug_log,
899                           "Looking for anchored substr starting at offset %ld...\n",
900                           (long)(other_last - i_strpos)) );
901                 goto do_other_anchored;
902             }
903             /* Another way we could have checked stclass at the
904                current position only: */
905             if (ml_anch) {
906                 s = t = t + 1;
907                 if (!check)
908                     goto giveup;
909                 DEBUG_r( PerlIO_printf(Perl_debug_log,
910                           "Looking for /%s^%s/m starting at offset %ld...\n",
911                           PL_colors[0],PL_colors[1], (long)(t - i_strpos)) );
912                 goto try_at_offset;
913             }
914             if (!(do_utf8 ? prog->float_utf8 : prog->float_substr))     /* Could have been deleted */
915                 goto fail;
916             /* Check is floating subtring. */
917           retry_floating_check:
918             t = check_at - start_shift;
919             DEBUG_r( what = "floating" );
920             goto hop_and_restart;
921         }
922         if (t != s) {
923             DEBUG_r(PerlIO_printf(Perl_debug_log,
924                         "By STCLASS: moving %ld --> %ld\n",
925                                   (long)(t - i_strpos), (long)(s - i_strpos))
926                    );
927         }
928         else {
929             DEBUG_r(PerlIO_printf(Perl_debug_log,
930                                   "Does not contradict STCLASS...\n"); 
931                    );
932         }
933     }
934   giveup:
935     DEBUG_r(PerlIO_printf(Perl_debug_log, "%s%s:%s match at offset %ld\n",
936                           PL_colors[4], (check ? "Guessed" : "Giving up"),
937                           PL_colors[5], (long)(s - i_strpos)) );
938     return s;
939
940   fail_finish:                          /* Substring not found */
941     if (prog->check_substr || prog->check_utf8)         /* could be removed already */
942         BmUSEFUL(do_utf8 ? prog->check_utf8 : prog->check_substr) += 5; /* hooray */
943   fail:
944     DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch rejected by optimizer%s\n",
945                           PL_colors[4],PL_colors[5]));
946     return Nullch;
947 }
948
949 /* We know what class REx starts with.  Try to find this position... */
950 STATIC char *
951 S_find_byclass(pTHX_ regexp * prog, regnode *c, char *s, char *strend, char *startpos, I32 norun)
952 {
953         I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
954         char *m;
955         STRLEN ln;
956         unsigned int c1;
957         unsigned int c2;
958         char *e;
959         register I32 tmp = 1;   /* Scratch variable? */
960         register bool do_utf8 = PL_reg_match_utf8;
961
962         /* We know what class it must start with. */
963         switch (OP(c)) {
964         case ANYOF:
965             if (do_utf8) {
966                  while (s < strend) {
967                       if ((ANYOF_FLAGS(c) & ANYOF_UNICODE) ||
968                           !UTF8_IS_INVARIANT((U8)s[0]) ?
969                           reginclass(c, (U8*)s, 0, do_utf8) :
970                           REGINCLASS(c, (U8*)s)) {
971                            if (tmp && (norun || regtry(prog, s)))
972                                 goto got_it;
973                            else
974                                 tmp = doevery;
975                       }
976                       else 
977                            tmp = 1;
978                       s += UTF8SKIP(s);
979                  }
980             }
981             else {
982                  while (s < strend) {
983                       STRLEN skip = 1;
984
985                       if (REGINCLASS(c, (U8*)s) ||
986                           (ANYOF_FOLD_SHARP_S(c, s, strend) &&
987                            /* The assignment of 2 is intentional:
988                             * for the folded sharp s, the skip is 2. */
989                            (skip = SHARP_S_SKIP))) {
990                            if (tmp && (norun || regtry(prog, s)))
991                                 goto got_it;
992                            else
993                                 tmp = doevery;
994                       }
995                       else 
996                            tmp = 1;
997                       s += skip;
998                  }
999             }
1000             break;
1001         case CANY:
1002             while (s < strend) {
1003                 if (tmp && (norun || regtry(prog, s)))
1004                     goto got_it;
1005                 else
1006                     tmp = doevery;
1007                 s++;
1008             }
1009             break;
1010         case EXACTF:
1011             m = STRING(c);
1012             ln = STR_LEN(c);
1013             if (UTF) {
1014                 STRLEN ulen1, ulen2;
1015                 U8 tmpbuf1[UTF8_MAXLEN_UCLC+1];
1016                 U8 tmpbuf2[UTF8_MAXLEN_UCLC+1];
1017
1018                 to_utf8_lower((U8*)m, tmpbuf1, &ulen1);
1019                 to_utf8_upper((U8*)m, tmpbuf2, &ulen2);
1020
1021                 c1 = utf8n_to_uvchr(tmpbuf1, UTF8_MAXLEN_UCLC, 
1022                                     0, ckWARN(WARN_UTF8) ? 0 : UTF8_ALLOW_ANY);
1023                 c2 = utf8n_to_uvchr(tmpbuf2, UTF8_MAXLEN_UCLC,
1024                                     0, ckWARN(WARN_UTF8) ? 0 : UTF8_ALLOW_ANY);
1025             }
1026             else {
1027                 c1 = *(U8*)m;
1028                 c2 = PL_fold[c1];
1029             }
1030             goto do_exactf;
1031         case EXACTFL:
1032             m = STRING(c);
1033             ln = STR_LEN(c);
1034             c1 = *(U8*)m;
1035             c2 = PL_fold_locale[c1];
1036           do_exactf:
1037             e = HOP3c(strend, -(I32)ln, s);
1038
1039             if (norun && e < s)
1040                 e = s;                  /* Due to minlen logic of intuit() */
1041
1042             /* The idea in the EXACTF* cases is to first find the
1043              * first character of the EXACTF* node and then, if
1044              * necessary, case-insensitively compare the full
1045              * text of the node.  The c1 and c2 are the first
1046              * characters (though in Unicode it gets a bit
1047              * more complicated because there are more cases
1048              * than just upper and lower: one needs to use
1049              * the so-called folding case for case-insensitive
1050              * matching (called "loose matching" in Unicode).
1051              * ibcmp_utf8() will do just that. */
1052
1053             if (do_utf8) {
1054                 UV c, f;
1055                 U8 tmpbuf [UTF8_MAXLEN+1];
1056                 U8 foldbuf[UTF8_MAXLEN_FOLD+1];
1057                 STRLEN len, foldlen;
1058                 
1059                 if (c1 == c2) {
1060                     while (s <= e) {
1061                         c = utf8n_to_uvchr((U8*)s, UTF8_MAXLEN, &len,
1062                                            ckWARN(WARN_UTF8) ?
1063                                            0 : UTF8_ALLOW_ANY);
1064                         if ( c == c1
1065                              && (ln == len ||
1066                                  ibcmp_utf8(s, (char **)0, 0,  do_utf8,
1067                                             m, (char **)0, ln, (bool)UTF))
1068                              && (norun || regtry(prog, s)) )
1069                             goto got_it;
1070                         else {
1071                              uvchr_to_utf8(tmpbuf, c);
1072                              f = to_utf8_fold(tmpbuf, foldbuf, &foldlen);
1073                              if ( f != c
1074                                   && (f == c1 || f == c2)
1075                                   && (ln == foldlen ||
1076                                       !ibcmp_utf8((char *) foldbuf,
1077                                                   (char **)0, foldlen, do_utf8,
1078                                                   m,
1079                                                   (char **)0, ln, (bool)UTF))
1080                                   && (norun || regtry(prog, s)) )
1081                                   goto got_it;
1082                         }
1083                         s += len;
1084                     }
1085                 }
1086                 else {
1087                     while (s <= e) {
1088                       c = utf8n_to_uvchr((U8*)s, UTF8_MAXLEN, &len,
1089                                            ckWARN(WARN_UTF8) ?
1090                                            0 : UTF8_ALLOW_ANY);
1091
1092                         /* Handle some of the three Greek sigmas cases.
1093                          * Note that not all the possible combinations
1094                          * are handled here: some of them are handled
1095                          * by the standard folding rules, and some of
1096                          * them (the character class or ANYOF cases)
1097                          * are handled during compiletime in
1098                          * regexec.c:S_regclass(). */
1099                         if (c == (UV)UNICODE_GREEK_CAPITAL_LETTER_SIGMA ||
1100                             c == (UV)UNICODE_GREEK_SMALL_LETTER_FINAL_SIGMA)
1101                             c = (UV)UNICODE_GREEK_SMALL_LETTER_SIGMA;
1102
1103                         if ( (c == c1 || c == c2)
1104                              && (ln == len ||
1105                                  ibcmp_utf8(s, (char **)0, 0,  do_utf8,
1106                                             m, (char **)0, ln, (bool)UTF))
1107                              && (norun || regtry(prog, s)) )
1108                             goto got_it;
1109                         else {
1110                              uvchr_to_utf8(tmpbuf, c);
1111                              f = to_utf8_fold(tmpbuf, foldbuf, &foldlen);
1112                              if ( f != c
1113                                   && (f == c1 || f == c2)
1114                                   && (ln == foldlen ||
1115                                       !ibcmp_utf8((char *) foldbuf,
1116                                                   (char **)0, foldlen, do_utf8,
1117                                                   m,
1118                                                   (char **)0, ln, (bool)UTF))
1119                                   && (norun || regtry(prog, s)) )
1120                                   goto got_it;
1121                         }
1122                         s += len;
1123                     }
1124                 }
1125             }
1126             else {
1127                 if (c1 == c2)
1128                     while (s <= e) {
1129                         if ( *(U8*)s == c1
1130                              && (ln == 1 || !(OP(c) == EXACTF
1131                                               ? ibcmp(s, m, ln)
1132                                               : ibcmp_locale(s, m, ln)))
1133                              && (norun || regtry(prog, s)) )
1134                             goto got_it;
1135                         s++;
1136                     }
1137                 else
1138                     while (s <= e) {
1139                         if ( (*(U8*)s == c1 || *(U8*)s == c2)
1140                              && (ln == 1 || !(OP(c) == EXACTF
1141                                               ? ibcmp(s, m, ln)
1142                                               : ibcmp_locale(s, m, ln)))
1143                              && (norun || regtry(prog, s)) )
1144                             goto got_it;
1145                         s++;
1146                     }
1147             }
1148             break;
1149         case BOUNDL:
1150             PL_reg_flags |= RF_tainted;
1151             /* FALL THROUGH */
1152         case BOUND:
1153             if (do_utf8) {
1154                 if (s == PL_bostr)
1155                     tmp = '\n';
1156                 else {
1157                     U8 *r = reghop3((U8*)s, -1, (U8*)PL_bostr);
1158                 
1159                     tmp = utf8n_to_uvchr(r, UTF8SKIP(r), 0, 0);
1160                 }
1161                 tmp = ((OP(c) == BOUND ?
1162                         isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1163                 LOAD_UTF8_CHARCLASS(alnum,"a");
1164                 while (s < strend) {
1165                     if (tmp == !(OP(c) == BOUND ?
1166                                  swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) :
1167                                  isALNUM_LC_utf8((U8*)s)))
1168                     {
1169                         tmp = !tmp;
1170                         if ((norun || regtry(prog, s)))
1171                             goto got_it;
1172                     }
1173                     s += UTF8SKIP(s);
1174                 }
1175             }
1176             else {
1177                 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1178                 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1179                 while (s < strend) {
1180                     if (tmp ==
1181                         !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
1182                         tmp = !tmp;
1183                         if ((norun || regtry(prog, s)))
1184                             goto got_it;
1185                     }
1186                     s++;
1187                 }
1188             }
1189             if ((!prog->minlen && tmp) && (norun || regtry(prog, s)))
1190                 goto got_it;
1191             break;
1192         case NBOUNDL:
1193             PL_reg_flags |= RF_tainted;
1194             /* FALL THROUGH */
1195         case NBOUND:
1196             if (do_utf8) {
1197                 if (s == PL_bostr)
1198                     tmp = '\n';
1199                 else {
1200                     U8 *r = reghop3((U8*)s, -1, (U8*)PL_bostr);
1201                 
1202                     tmp = utf8n_to_uvchr(r, UTF8SKIP(r), 0, 0);
1203                 }
1204                 tmp = ((OP(c) == NBOUND ?
1205                         isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1206                 LOAD_UTF8_CHARCLASS(alnum,"a");
1207                 while (s < strend) {
1208                     if (tmp == !(OP(c) == NBOUND ?
1209                                  swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) :
1210                                  isALNUM_LC_utf8((U8*)s)))
1211                         tmp = !tmp;
1212                     else if ((norun || regtry(prog, s)))
1213                         goto got_it;
1214                     s += UTF8SKIP(s);
1215                 }
1216             }
1217             else {
1218                 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
1219                 tmp = ((OP(c) == NBOUND ?
1220                         isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1221                 while (s < strend) {
1222                     if (tmp ==
1223                         !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
1224                         tmp = !tmp;
1225                     else if ((norun || regtry(prog, s)))
1226                         goto got_it;
1227                     s++;
1228                 }
1229             }
1230             if ((!prog->minlen && !tmp) && (norun || regtry(prog, s)))
1231                 goto got_it;
1232             break;
1233         case ALNUM:
1234             if (do_utf8) {
1235                 LOAD_UTF8_CHARCLASS(alnum,"a");
1236                 while (s < strend) {
1237                     if (swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) {
1238                         if (tmp && (norun || regtry(prog, s)))
1239                             goto got_it;
1240                         else
1241                             tmp = doevery;
1242                     }
1243                     else
1244                         tmp = 1;
1245                     s += UTF8SKIP(s);
1246                 }
1247             }
1248             else {
1249                 while (s < strend) {
1250                     if (isALNUM(*s)) {
1251                         if (tmp && (norun || regtry(prog, s)))
1252                             goto got_it;
1253                         else
1254                             tmp = doevery;
1255                     }
1256                     else
1257                         tmp = 1;
1258                     s++;
1259                 }
1260             }
1261             break;
1262         case ALNUML:
1263             PL_reg_flags |= RF_tainted;
1264             if (do_utf8) {
1265                 while (s < strend) {
1266                     if (isALNUM_LC_utf8((U8*)s)) {
1267                         if (tmp && (norun || regtry(prog, s)))
1268                             goto got_it;
1269                         else
1270                             tmp = doevery;
1271                     }
1272                     else
1273                         tmp = 1;
1274                     s += UTF8SKIP(s);
1275                 }
1276             }
1277             else {
1278                 while (s < strend) {
1279                     if (isALNUM_LC(*s)) {
1280                         if (tmp && (norun || regtry(prog, s)))
1281                             goto got_it;
1282                         else
1283                             tmp = doevery;
1284                     }
1285                     else
1286                         tmp = 1;
1287                     s++;
1288                 }
1289             }
1290             break;
1291         case NALNUM:
1292             if (do_utf8) {
1293                 LOAD_UTF8_CHARCLASS(alnum,"a");
1294                 while (s < strend) {
1295                     if (!swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) {
1296                         if (tmp && (norun || regtry(prog, s)))
1297                             goto got_it;
1298                         else
1299                             tmp = doevery;
1300                     }
1301                     else
1302                         tmp = 1;
1303                     s += UTF8SKIP(s);
1304                 }
1305             }
1306             else {
1307                 while (s < strend) {
1308                     if (!isALNUM(*s)) {
1309                         if (tmp && (norun || regtry(prog, s)))
1310                             goto got_it;
1311                         else
1312                             tmp = doevery;
1313                     }
1314                     else
1315                         tmp = 1;
1316                     s++;
1317                 }
1318             }
1319             break;
1320         case NALNUML:
1321             PL_reg_flags |= RF_tainted;
1322             if (do_utf8) {
1323                 while (s < strend) {
1324                     if (!isALNUM_LC_utf8((U8*)s)) {
1325                         if (tmp && (norun || regtry(prog, s)))
1326                             goto got_it;
1327                         else
1328                             tmp = doevery;
1329                     }
1330                     else
1331                         tmp = 1;
1332                     s += UTF8SKIP(s);
1333                 }
1334             }
1335             else {
1336                 while (s < strend) {
1337                     if (!isALNUM_LC(*s)) {
1338                         if (tmp && (norun || regtry(prog, s)))
1339                             goto got_it;
1340                         else
1341                             tmp = doevery;
1342                     }
1343                     else
1344                         tmp = 1;
1345                     s++;
1346                 }
1347             }
1348             break;
1349         case SPACE:
1350             if (do_utf8) {
1351                 LOAD_UTF8_CHARCLASS(space," ");
1352                 while (s < strend) {
1353                     if (*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, do_utf8)) {
1354                         if (tmp && (norun || regtry(prog, s)))
1355                             goto got_it;
1356                         else
1357                             tmp = doevery;
1358                     }
1359                     else
1360                         tmp = 1;
1361                     s += UTF8SKIP(s);
1362                 }
1363             }
1364             else {
1365                 while (s < strend) {
1366                     if (isSPACE(*s)) {
1367                         if (tmp && (norun || regtry(prog, s)))
1368                             goto got_it;
1369                         else
1370                             tmp = doevery;
1371                     }
1372                     else
1373                         tmp = 1;
1374                     s++;
1375                 }
1376             }
1377             break;
1378         case SPACEL:
1379             PL_reg_flags |= RF_tainted;
1380             if (do_utf8) {
1381                 while (s < strend) {
1382                     if (*s == ' ' || isSPACE_LC_utf8((U8*)s)) {
1383                         if (tmp && (norun || regtry(prog, s)))
1384                             goto got_it;
1385                         else
1386                             tmp = doevery;
1387                     }
1388                     else
1389                         tmp = 1;
1390                     s += UTF8SKIP(s);
1391                 }
1392             }
1393             else {
1394                 while (s < strend) {
1395                     if (isSPACE_LC(*s)) {
1396                         if (tmp && (norun || regtry(prog, s)))
1397                             goto got_it;
1398                         else
1399                             tmp = doevery;
1400                     }
1401                     else
1402                         tmp = 1;
1403                     s++;
1404                 }
1405             }
1406             break;
1407         case NSPACE:
1408             if (do_utf8) {
1409                 LOAD_UTF8_CHARCLASS(space," ");
1410                 while (s < strend) {
1411                     if (!(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, do_utf8))) {
1412                         if (tmp && (norun || regtry(prog, s)))
1413                             goto got_it;
1414                         else
1415                             tmp = doevery;
1416                     }
1417                     else
1418                         tmp = 1;
1419                     s += UTF8SKIP(s);
1420                 }
1421             }
1422             else {
1423                 while (s < strend) {
1424                     if (!isSPACE(*s)) {
1425                         if (tmp && (norun || regtry(prog, s)))
1426                             goto got_it;
1427                         else
1428                             tmp = doevery;
1429                     }
1430                     else
1431                         tmp = 1;
1432                     s++;
1433                 }
1434             }
1435             break;
1436         case NSPACEL:
1437             PL_reg_flags |= RF_tainted;
1438             if (do_utf8) {
1439                 while (s < strend) {
1440                     if (!(*s == ' ' || isSPACE_LC_utf8((U8*)s))) {
1441                         if (tmp && (norun || regtry(prog, s)))
1442                             goto got_it;
1443                         else
1444                             tmp = doevery;
1445                     }
1446                     else
1447                         tmp = 1;
1448                     s += UTF8SKIP(s);
1449                 }
1450             }
1451             else {
1452                 while (s < strend) {
1453                     if (!isSPACE_LC(*s)) {
1454                         if (tmp && (norun || regtry(prog, s)))
1455                             goto got_it;
1456                         else
1457                             tmp = doevery;
1458                     }
1459                     else
1460                         tmp = 1;
1461                     s++;
1462                 }
1463             }
1464             break;
1465         case DIGIT:
1466             if (do_utf8) {
1467                 LOAD_UTF8_CHARCLASS(digit,"0");
1468                 while (s < strend) {
1469                     if (swash_fetch(PL_utf8_digit,(U8*)s, do_utf8)) {
1470                         if (tmp && (norun || regtry(prog, s)))
1471                             goto got_it;
1472                         else
1473                             tmp = doevery;
1474                     }
1475                     else
1476                         tmp = 1;
1477                     s += UTF8SKIP(s);
1478                 }
1479             }
1480             else {
1481                 while (s < strend) {
1482                     if (isDIGIT(*s)) {
1483                         if (tmp && (norun || regtry(prog, s)))
1484                             goto got_it;
1485                         else
1486                             tmp = doevery;
1487                     }
1488                     else
1489                         tmp = 1;
1490                     s++;
1491                 }
1492             }
1493             break;
1494         case DIGITL:
1495             PL_reg_flags |= RF_tainted;
1496             if (do_utf8) {
1497                 while (s < strend) {
1498                     if (isDIGIT_LC_utf8((U8*)s)) {
1499                         if (tmp && (norun || regtry(prog, s)))
1500                             goto got_it;
1501                         else
1502                             tmp = doevery;
1503                     }
1504                     else
1505                         tmp = 1;
1506                     s += UTF8SKIP(s);
1507                 }
1508             }
1509             else {
1510                 while (s < strend) {
1511                     if (isDIGIT_LC(*s)) {
1512                         if (tmp && (norun || regtry(prog, s)))
1513                             goto got_it;
1514                         else
1515                             tmp = doevery;
1516                     }
1517                     else
1518                         tmp = 1;
1519                     s++;
1520                 }
1521             }
1522             break;
1523         case NDIGIT:
1524             if (do_utf8) {
1525                 LOAD_UTF8_CHARCLASS(digit,"0");
1526                 while (s < strend) {
1527                     if (!swash_fetch(PL_utf8_digit,(U8*)s, do_utf8)) {
1528                         if (tmp && (norun || regtry(prog, s)))
1529                             goto got_it;
1530                         else
1531                             tmp = doevery;
1532                     }
1533                     else
1534                         tmp = 1;
1535                     s += UTF8SKIP(s);
1536                 }
1537             }
1538             else {
1539                 while (s < strend) {
1540                     if (!isDIGIT(*s)) {
1541                         if (tmp && (norun || regtry(prog, s)))
1542                             goto got_it;
1543                         else
1544                             tmp = doevery;
1545                     }
1546                     else
1547                         tmp = 1;
1548                     s++;
1549                 }
1550             }
1551             break;
1552         case NDIGITL:
1553             PL_reg_flags |= RF_tainted;
1554             if (do_utf8) {
1555                 while (s < strend) {
1556                     if (!isDIGIT_LC_utf8((U8*)s)) {
1557                         if (tmp && (norun || regtry(prog, s)))
1558                             goto got_it;
1559                         else
1560                             tmp = doevery;
1561                     }
1562                     else
1563                         tmp = 1;
1564                     s += UTF8SKIP(s);
1565                 }
1566             }
1567             else {
1568                 while (s < strend) {
1569                     if (!isDIGIT_LC(*s)) {
1570                         if (tmp && (norun || regtry(prog, s)))
1571                             goto got_it;
1572                         else
1573                             tmp = doevery;
1574                     }
1575                     else
1576                         tmp = 1;
1577                     s++;
1578                 }
1579             }
1580             break;
1581         default:
1582             Perl_croak(aTHX_ "panic: unknown regstclass %d", (int)OP(c));
1583             break;
1584         }
1585         return 0;
1586       got_it:
1587         return s;
1588 }
1589
1590 /*
1591  - regexec_flags - match a regexp against a string
1592  */
1593 I32
1594 Perl_regexec_flags(pTHX_ register regexp *prog, char *stringarg, register char *strend,
1595               char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
1596 /* strend: pointer to null at end of string */
1597 /* strbeg: real beginning of string */
1598 /* minend: end of match must be >=minend after stringarg. */
1599 /* data: May be used for some additional optimizations. */
1600 /* nosave: For optimizations. */
1601 {
1602     register char *s;
1603     register regnode *c;
1604     register char *startpos = stringarg;
1605     I32 minlen;         /* must match at least this many chars */
1606     I32 dontbother = 0; /* how many characters not to try at end */
1607     /* I32 start_shift = 0; */          /* Offset of the start to find
1608                                          constant substr. */            /* CC */
1609     I32 end_shift = 0;                  /* Same for the end. */         /* CC */
1610     I32 scream_pos = -1;                /* Internal iterator of scream. */
1611     char *scream_olds;
1612     SV* oreplsv = GvSV(PL_replgv);
1613     bool do_utf8 = DO_UTF8(sv);
1614 #ifdef DEBUGGING
1615     SV *dsv0 = PERL_DEBUG_PAD_ZERO(0);
1616     SV *dsv1 = PERL_DEBUG_PAD_ZERO(1);
1617 #endif
1618
1619     PL_regcc = 0;
1620
1621     cache_re(prog);
1622 #ifdef DEBUGGING
1623     PL_regnarrate = DEBUG_r_TEST;
1624 #endif
1625
1626     /* Be paranoid... */
1627     if (prog == NULL || startpos == NULL) {
1628         Perl_croak(aTHX_ "NULL regexp parameter");
1629         return 0;
1630     }
1631
1632     minlen = prog->minlen;
1633     if (strend - startpos < minlen) {
1634         DEBUG_r(PerlIO_printf(Perl_debug_log,
1635                               "String too short [regexec_flags]...\n"));
1636         goto phooey;
1637     }
1638
1639     /* Check validity of program. */
1640     if (UCHARAT(prog->program) != REG_MAGIC) {
1641         Perl_croak(aTHX_ "corrupted regexp program");
1642     }
1643
1644     PL_reg_flags = 0;
1645     PL_reg_eval_set = 0;
1646     PL_reg_maxiter = 0;
1647
1648     if (prog->reganch & ROPT_UTF8)
1649         PL_reg_flags |= RF_utf8;
1650
1651     /* Mark beginning of line for ^ and lookbehind. */
1652     PL_regbol = startpos;
1653     PL_bostr  = strbeg;
1654     PL_reg_sv = sv;
1655
1656     /* Mark end of line for $ (and such) */
1657     PL_regeol = strend;
1658
1659     /* see how far we have to get to not match where we matched before */
1660     PL_regtill = startpos+minend;
1661
1662     /* We start without call_cc context.  */
1663     PL_reg_call_cc = 0;
1664
1665     /* If there is a "must appear" string, look for it. */
1666     s = startpos;
1667
1668     if (prog->reganch & ROPT_GPOS_SEEN) { /* Need to have PL_reg_ganch */
1669         MAGIC *mg;
1670
1671         if (flags & REXEC_IGNOREPOS)    /* Means: check only at start */
1672             PL_reg_ganch = startpos;
1673         else if (sv && SvTYPE(sv) >= SVt_PVMG
1674                   && SvMAGIC(sv)
1675                   && (mg = mg_find(sv, PERL_MAGIC_regex_global))
1676                   && mg->mg_len >= 0) {
1677             PL_reg_ganch = strbeg + mg->mg_len; /* Defined pos() */
1678             if (prog->reganch & ROPT_ANCH_GPOS) {
1679                 if (s > PL_reg_ganch)
1680                     goto phooey;
1681                 s = PL_reg_ganch;
1682             }
1683         }
1684         else                            /* pos() not defined */
1685             PL_reg_ganch = strbeg;
1686     }
1687
1688     if (!(flags & REXEC_CHECKED) && (prog->check_substr != Nullsv || prog->check_utf8 != Nullsv)) {
1689         re_scream_pos_data d;
1690
1691         d.scream_olds = &scream_olds;
1692         d.scream_pos = &scream_pos;
1693         s = re_intuit_start(prog, sv, s, strend, flags, &d);
1694         if (!s) {
1695             DEBUG_r(PerlIO_printf(Perl_debug_log, "Not present...\n"));
1696             goto phooey;        /* not present */
1697         }
1698     }
1699
1700     DEBUG_r({
1701          char *s0   = UTF ?
1702            pv_uni_display(dsv0, (U8*)prog->precomp, prog->prelen, 60,
1703                           UNI_DISPLAY_REGEX) :
1704            prog->precomp;
1705          int   len0 = UTF ? SvCUR(dsv0) : prog->prelen;
1706          char *s1   = do_utf8 ? sv_uni_display(dsv1, sv, 60,
1707                                                UNI_DISPLAY_REGEX) : startpos;
1708          int   len1 = do_utf8 ? SvCUR(dsv1) : strend - startpos;
1709          if (!PL_colorset)
1710              reginitcolors();
1711          PerlIO_printf(Perl_debug_log,
1712                        "%sMatching REx%s `%s%*.*s%s%s' against `%s%.*s%s%s'\n",
1713                        PL_colors[4],PL_colors[5],PL_colors[0],
1714                        len0, len0, s0,
1715                        PL_colors[1],
1716                        len0 > 60 ? "..." : "",
1717                        PL_colors[0],
1718                        (int)(len1 > 60 ? 60 : len1),
1719                        s1, PL_colors[1],
1720                        (len1 > 60 ? "..." : "")
1721               );
1722     });
1723
1724     /* Simplest case:  anchored match need be tried only once. */
1725     /*  [unless only anchor is BOL and multiline is set] */
1726     if (prog->reganch & (ROPT_ANCH & ~ROPT_ANCH_GPOS)) {
1727         if (s == startpos && regtry(prog, startpos))
1728             goto got_it;
1729         else if (PL_multiline || (prog->reganch & ROPT_IMPLICIT)
1730                  || (prog->reganch & ROPT_ANCH_MBOL)) /* XXXX SBOL? */
1731         {
1732             char *end;
1733
1734             if (minlen)
1735                 dontbother = minlen - 1;
1736             end = HOP3c(strend, -dontbother, strbeg) - 1;
1737             /* for multiline we only have to try after newlines */
1738             if (prog->check_substr || prog->check_utf8) {
1739                 if (s == startpos)
1740                     goto after_try;
1741                 while (1) {
1742                     if (regtry(prog, s))
1743                         goto got_it;
1744                   after_try:
1745                     if (s >= end)
1746                         goto phooey;
1747                     if (prog->reganch & RE_USE_INTUIT) {
1748                         s = re_intuit_start(prog, sv, s + 1, strend, flags, NULL);
1749                         if (!s)
1750                             goto phooey;
1751                     }
1752                     else
1753                         s++;
1754                 }               
1755             } else {
1756                 if (s > startpos)
1757                     s--;
1758                 while (s < end) {
1759                     if (*s++ == '\n') { /* don't need PL_utf8skip here */
1760                         if (regtry(prog, s))
1761                             goto got_it;
1762                     }
1763                 }               
1764             }
1765         }
1766         goto phooey;
1767     } else if (prog->reganch & ROPT_ANCH_GPOS) {
1768         if (regtry(prog, PL_reg_ganch))
1769             goto got_it;
1770         goto phooey;
1771     }
1772
1773     /* Messy cases:  unanchored match. */
1774     if ((prog->anchored_substr || prog->anchored_utf8) && prog->reganch & ROPT_SKIP) {
1775         /* we have /x+whatever/ */
1776         /* it must be a one character string (XXXX Except UTF?) */
1777         char ch;
1778 #ifdef DEBUGGING
1779         int did_match = 0;
1780 #endif
1781         if (!(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr))
1782             do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1783         ch = SvPVX(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr)[0];
1784
1785         if (do_utf8) {
1786             while (s < strend) {
1787                 if (*s == ch) {
1788                     DEBUG_r( did_match = 1 );
1789                     if (regtry(prog, s)) goto got_it;
1790                     s += UTF8SKIP(s);
1791                     while (s < strend && *s == ch)
1792                         s += UTF8SKIP(s);
1793                 }
1794                 s += UTF8SKIP(s);
1795             }
1796         }
1797         else {
1798             while (s < strend) {
1799                 if (*s == ch) {
1800                     DEBUG_r( did_match = 1 );
1801                     if (regtry(prog, s)) goto got_it;
1802                     s++;
1803                     while (s < strend && *s == ch)
1804                         s++;
1805                 }
1806                 s++;
1807             }
1808         }
1809         DEBUG_r(if (!did_match)
1810                 PerlIO_printf(Perl_debug_log,
1811                                   "Did not find anchored character...\n")
1812                );
1813     }
1814     /*SUPPRESS 560*/
1815     else if (prog->anchored_substr != Nullsv
1816               || prog->anchored_utf8 != Nullsv
1817               || ((prog->float_substr != Nullsv || prog->float_utf8 != Nullsv)
1818                   && prog->float_max_offset < strend - s)) {
1819         SV *must;
1820         I32 back_max;
1821         I32 back_min;
1822         char *last;
1823         char *last1;            /* Last position checked before */
1824 #ifdef DEBUGGING
1825         int did_match = 0;
1826 #endif
1827         if (prog->anchored_substr || prog->anchored_utf8) {
1828             if (!(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr))
1829                 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1830             must = do_utf8 ? prog->anchored_utf8 : prog->anchored_substr;
1831             back_max = back_min = prog->anchored_offset;
1832         } else {
1833             if (!(do_utf8 ? prog->float_utf8 : prog->float_substr))
1834                 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1835             must = do_utf8 ? prog->float_utf8 : prog->float_substr;
1836             back_max = prog->float_max_offset;
1837             back_min = prog->float_min_offset;
1838         }
1839         if (must == &PL_sv_undef)
1840             /* could not downgrade utf8 check substring, so must fail */
1841             goto phooey;
1842
1843         last = HOP3c(strend,    /* Cannot start after this */
1844                           -(I32)(CHR_SVLEN(must)
1845                                  - (SvTAIL(must) != 0) + back_min), strbeg);
1846
1847         if (s > PL_bostr)
1848             last1 = HOPc(s, -1);
1849         else
1850             last1 = s - 1;      /* bogus */
1851
1852         /* XXXX check_substr already used to find `s', can optimize if
1853            check_substr==must. */
1854         scream_pos = -1;
1855         dontbother = end_shift;
1856         strend = HOPc(strend, -dontbother);
1857         while ( (s <= last) &&
1858                 ((flags & REXEC_SCREAM)
1859                  ? (s = screaminstr(sv, must, HOP3c(s, back_min, strend) - strbeg,
1860                                     end_shift, &scream_pos, 0))
1861                  : (s = fbm_instr((unsigned char*)HOP3(s, back_min, strend),
1862                                   (unsigned char*)strend, must,
1863                                   PL_multiline ? FBMrf_MULTILINE : 0))) ) {
1864             /* we may be pointing at the wrong string */
1865             if ((flags & REXEC_SCREAM) && RX_MATCH_COPIED(prog))
1866                 s = prog->subbeg + (s - SvPVX(sv));
1867             DEBUG_r( did_match = 1 );
1868             if (HOPc(s, -back_max) > last1) {
1869                 last1 = HOPc(s, -back_min);
1870                 s = HOPc(s, -back_max);
1871             }
1872             else {
1873                 char *t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
1874
1875                 last1 = HOPc(s, -back_min);
1876                 s = t;          
1877             }
1878             if (do_utf8) {
1879                 while (s <= last1) {
1880                     if (regtry(prog, s))
1881                         goto got_it;
1882                     s += UTF8SKIP(s);
1883                 }
1884             }
1885             else {
1886                 while (s <= last1) {
1887                     if (regtry(prog, s))
1888                         goto got_it;
1889                     s++;
1890                 }
1891             }
1892         }
1893         DEBUG_r(if (!did_match)
1894                     PerlIO_printf(Perl_debug_log, 
1895                                   "Did not find %s substr `%s%.*s%s'%s...\n",
1896                               ((must == prog->anchored_substr || must == prog->anchored_utf8)
1897                                ? "anchored" : "floating"),
1898                               PL_colors[0],
1899                               (int)(SvCUR(must) - (SvTAIL(must)!=0)),
1900                               SvPVX(must),
1901                                   PL_colors[1], (SvTAIL(must) ? "$" : ""))
1902                );
1903         goto phooey;
1904     }
1905     else if ((c = prog->regstclass)) {
1906         if (minlen) {
1907             I32 op = (U8)OP(prog->regstclass);
1908             /* don't bother with what can't match */
1909             if (PL_regkind[op] != EXACT && op != CANY)
1910                 strend = HOPc(strend, -(minlen - 1));
1911         }
1912         DEBUG_r({
1913             SV *prop = sv_newmortal();
1914             char *s0;
1915             char *s1;
1916             int len0;
1917             int len1;
1918
1919             regprop(prop, c);
1920             s0 = UTF ?
1921               pv_uni_display(dsv0, (U8*)SvPVX(prop), SvCUR(prop), 60,
1922                              UNI_DISPLAY_REGEX) :
1923               SvPVX(prop);
1924             len0 = UTF ? SvCUR(dsv0) : SvCUR(prop);
1925             s1 = UTF ?
1926               sv_uni_display(dsv1, sv, 60, UNI_DISPLAY_REGEX) : s;
1927             len1 = UTF ? SvCUR(dsv1) : strend - s;
1928             PerlIO_printf(Perl_debug_log,
1929                           "Matching stclass `%*.*s' against `%*.*s'\n",
1930                           len0, len0, s0,
1931                           len1, len1, s1);
1932         });
1933         if (find_byclass(prog, c, s, strend, startpos, 0))
1934             goto got_it;
1935         DEBUG_r(PerlIO_printf(Perl_debug_log, "Contradicts stclass...\n"));
1936     }
1937     else {
1938         dontbother = 0;
1939         if (prog->float_substr != Nullsv || prog->float_utf8 != Nullsv) {
1940             /* Trim the end. */
1941             char *last;
1942             SV* float_real;
1943
1944             if (!(do_utf8 ? prog->float_utf8 : prog->float_substr))
1945                 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1946             float_real = do_utf8 ? prog->float_utf8 : prog->float_substr;
1947
1948             if (flags & REXEC_SCREAM) {
1949                 last = screaminstr(sv, float_real, s - strbeg,
1950                                    end_shift, &scream_pos, 1); /* last one */
1951                 if (!last)
1952                     last = scream_olds; /* Only one occurrence. */
1953                 /* we may be pointing at the wrong string */
1954                 else if (RX_MATCH_COPIED(prog))
1955                     s = prog->subbeg + (s - SvPVX(sv));
1956             }
1957             else {
1958                 STRLEN len;
1959                 char *little = SvPV(float_real, len);
1960
1961                 if (SvTAIL(float_real)) {
1962                     if (memEQ(strend - len + 1, little, len - 1))
1963                         last = strend - len + 1;
1964                     else if (!PL_multiline)
1965                         last = memEQ(strend - len, little, len)
1966                             ? strend - len : Nullch;
1967                     else
1968                         goto find_last;
1969                 } else {
1970                   find_last:
1971                     if (len)
1972                         last = rninstr(s, strend, little, little + len);
1973                     else
1974                         last = strend;  /* matching `$' */
1975                 }
1976             }
1977             if (last == NULL) {
1978                 DEBUG_r(PerlIO_printf(Perl_debug_log,
1979                                       "%sCan't trim the tail, match fails (should not happen)%s\n",
1980                                       PL_colors[4],PL_colors[5]));
1981                 goto phooey; /* Should not happen! */
1982             }
1983             dontbother = strend - last + prog->float_min_offset;
1984         }
1985         if (minlen && (dontbother < minlen))
1986             dontbother = minlen - 1;
1987         strend -= dontbother;              /* this one's always in bytes! */
1988         /* We don't know much -- general case. */
1989         if (do_utf8) {
1990             for (;;) {
1991                 if (regtry(prog, s))
1992                     goto got_it;
1993                 if (s >= strend)
1994                     break;
1995                 s += UTF8SKIP(s);
1996             };
1997         }
1998         else {
1999             do {
2000                 if (regtry(prog, s))
2001                     goto got_it;
2002             } while (s++ < strend);
2003         }
2004     }
2005
2006     /* Failure. */
2007     goto phooey;
2008
2009 got_it:
2010     RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted);
2011
2012     if (PL_reg_eval_set) {
2013         /* Preserve the current value of $^R */
2014         if (oreplsv != GvSV(PL_replgv))
2015             sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is
2016                                                   restored, the value remains
2017                                                   the same. */
2018         restore_pos(aTHX_ 0);
2019     }
2020
2021     /* make sure $`, $&, $', and $digit will work later */
2022     if ( !(flags & REXEC_NOT_FIRST) ) {
2023         if (RX_MATCH_COPIED(prog)) {
2024             Safefree(prog->subbeg);
2025             RX_MATCH_COPIED_off(prog);
2026         }
2027         if (flags & REXEC_COPY_STR) {
2028             I32 i = PL_regeol - startpos + (stringarg - strbeg);
2029
2030             s = savepvn(strbeg, i);
2031             prog->subbeg = s;
2032             prog->sublen = i;
2033             RX_MATCH_COPIED_on(prog);
2034         }
2035         else {
2036             prog->subbeg = strbeg;
2037             prog->sublen = PL_regeol - strbeg;  /* strend may have been modified */
2038         }
2039     }
2040
2041     return 1;
2042
2043 phooey:
2044     DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch failed%s\n",
2045                           PL_colors[4],PL_colors[5]));
2046     if (PL_reg_eval_set)
2047         restore_pos(aTHX_ 0);
2048     return 0;
2049 }
2050
2051 /*
2052  - regtry - try match at specific point
2053  */
2054 STATIC I32                      /* 0 failure, 1 success */
2055 S_regtry(pTHX_ regexp *prog, char *startpos)
2056 {
2057     register I32 i;
2058     register I32 *sp;
2059     register I32 *ep;
2060     CHECKPOINT lastcp;
2061
2062 #ifdef DEBUGGING
2063     PL_regindent = 0;   /* XXXX Not good when matches are reenterable... */
2064 #endif
2065     if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) {
2066         MAGIC *mg;
2067
2068         PL_reg_eval_set = RS_init;
2069         DEBUG_r(DEBUG_s(
2070             PerlIO_printf(Perl_debug_log, "  setting stack tmpbase at %"IVdf"\n",
2071                           (IV)(PL_stack_sp - PL_stack_base));
2072             ));
2073         SAVEI32(cxstack[cxstack_ix].blk_oldsp);
2074         cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
2075         /* Otherwise OP_NEXTSTATE will free whatever on stack now.  */
2076         SAVETMPS;
2077         /* Apparently this is not needed, judging by wantarray. */
2078         /* SAVEI8(cxstack[cxstack_ix].blk_gimme);
2079            cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
2080
2081         if (PL_reg_sv) {
2082             /* Make $_ available to executed code. */
2083             if (PL_reg_sv != DEFSV) {
2084                 /* SAVE_DEFSV does *not* suffice here for USE_5005THREADS */
2085                 SAVESPTR(DEFSV);
2086                 DEFSV = PL_reg_sv;
2087             }
2088         
2089             if (!(SvTYPE(PL_reg_sv) >= SVt_PVMG && SvMAGIC(PL_reg_sv)
2090                   && (mg = mg_find(PL_reg_sv, PERL_MAGIC_regex_global)))) {
2091                 /* prepare for quick setting of pos */
2092                 sv_magic(PL_reg_sv, (SV*)0,
2093                         PERL_MAGIC_regex_global, Nullch, 0);
2094                 mg = mg_find(PL_reg_sv, PERL_MAGIC_regex_global);
2095                 mg->mg_len = -1;
2096             }
2097             PL_reg_magic    = mg;
2098             PL_reg_oldpos   = mg->mg_len;
2099             SAVEDESTRUCTOR_X(restore_pos, 0);
2100         }
2101         if (!PL_reg_curpm) {
2102             Newz(22,PL_reg_curpm, 1, PMOP);
2103 #ifdef USE_ITHREADS
2104             {
2105                 SV* repointer = newSViv(0);
2106                 /* so we know which PL_regex_padav element is PL_reg_curpm */
2107                 SvFLAGS(repointer) |= SVf_BREAK;
2108                 av_push(PL_regex_padav,repointer);
2109                 PL_reg_curpm->op_pmoffset = av_len(PL_regex_padav);
2110                 PL_regex_pad = AvARRAY(PL_regex_padav);
2111             }
2112 #endif      
2113         }
2114         PM_SETRE(PL_reg_curpm, prog);
2115         PL_reg_oldcurpm = PL_curpm;
2116         PL_curpm = PL_reg_curpm;
2117         if (RX_MATCH_COPIED(prog)) {
2118             /*  Here is a serious problem: we cannot rewrite subbeg,
2119                 since it may be needed if this match fails.  Thus
2120                 $` inside (?{}) could fail... */
2121             PL_reg_oldsaved = prog->subbeg;
2122             PL_reg_oldsavedlen = prog->sublen;
2123             RX_MATCH_COPIED_off(prog);
2124         }
2125         else
2126             PL_reg_oldsaved = Nullch;
2127         prog->subbeg = PL_bostr;
2128         prog->sublen = PL_regeol - PL_bostr; /* strend may have been modified */
2129     }
2130     prog->startp[0] = startpos - PL_bostr;
2131     PL_reginput = startpos;
2132     PL_regstartp = prog->startp;
2133     PL_regendp = prog->endp;
2134     PL_reglastparen = &prog->lastparen;
2135     PL_reglastcloseparen = &prog->lastcloseparen;
2136     prog->lastparen = 0;
2137     prog->lastcloseparen = 0;
2138     PL_regsize = 0;
2139     DEBUG_r(PL_reg_starttry = startpos);
2140     if (PL_reg_start_tmpl <= prog->nparens) {
2141         PL_reg_start_tmpl = prog->nparens*3/2 + 3;
2142         if(PL_reg_start_tmp)
2143             Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2144         else
2145             New(22,PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2146     }
2147
2148     /* XXXX What this code is doing here?!!!  There should be no need
2149        to do this again and again, PL_reglastparen should take care of
2150        this!  --ilya*/
2151
2152     /* Tests pat.t#187 and split.t#{13,14} seem to depend on this code.
2153      * Actually, the code in regcppop() (which Ilya may be meaning by
2154      * PL_reglastparen), is not needed at all by the test suite
2155      * (op/regexp, op/pat, op/split), but that code is needed, oddly
2156      * enough, for building DynaLoader, or otherwise this
2157      * "Error: '*' not in typemap in DynaLoader.xs, line 164"
2158      * will happen.  Meanwhile, this code *is* needed for the
2159      * above-mentioned test suite tests to succeed.  The common theme
2160      * on those tests seems to be returning null fields from matches.
2161      * --jhi */
2162 #if 1
2163     sp = prog->startp;
2164     ep = prog->endp;
2165     if (prog->nparens) {
2166         for (i = prog->nparens; i > (I32)*PL_reglastparen; i--) {
2167             *++sp = -1;
2168             *++ep = -1;
2169         }
2170     }
2171 #endif
2172     REGCP_SET(lastcp);
2173     if (regmatch(prog->program + 1)) {
2174         prog->endp[0] = PL_reginput - PL_bostr;
2175         return 1;
2176     }
2177     REGCP_UNWIND(lastcp);
2178     return 0;
2179 }
2180
2181 #define RE_UNWIND_BRANCH        1
2182 #define RE_UNWIND_BRANCHJ       2
2183
2184 union re_unwind_t;
2185
2186 typedef struct {                /* XX: makes sense to enlarge it... */
2187     I32 type;
2188     I32 prev;
2189     CHECKPOINT lastcp;
2190 } re_unwind_generic_t;
2191
2192 typedef struct {
2193     I32 type;
2194     I32 prev;
2195     CHECKPOINT lastcp;
2196     I32 lastparen;
2197     regnode *next;
2198     char *locinput;
2199     I32 nextchr;
2200 #ifdef DEBUGGING
2201     int regindent;
2202 #endif
2203 } re_unwind_branch_t;
2204
2205 typedef union re_unwind_t {
2206     I32 type;
2207     re_unwind_generic_t generic;
2208     re_unwind_branch_t branch;
2209 } re_unwind_t;
2210
2211 #define sayYES goto yes
2212 #define sayNO goto no
2213 #define sayNO_ANYOF goto no_anyof
2214 #define sayYES_FINAL goto yes_final
2215 #define sayYES_LOUD  goto yes_loud
2216 #define sayNO_FINAL  goto no_final
2217 #define sayNO_SILENT goto do_no
2218 #define saySAME(x) if (x) goto yes; else goto no
2219
2220 #define REPORT_CODE_OFF 24
2221
2222 /*
2223  - regmatch - main matching routine
2224  *
2225  * Conceptually the strategy is simple:  check to see whether the current
2226  * node matches, call self recursively to see whether the rest matches,
2227  * and then act accordingly.  In practice we make some effort to avoid
2228  * recursion, in particular by going through "ordinary" nodes (that don't
2229  * need to know whether the rest of the match failed) by a loop instead of
2230  * by recursion.
2231  */
2232 /* [lwall] I've hoisted the register declarations to the outer block in order to
2233  * maybe save a little bit of pushing and popping on the stack.  It also takes
2234  * advantage of machines that use a register save mask on subroutine entry.
2235  */
2236 STATIC I32                      /* 0 failure, 1 success */
2237 S_regmatch(pTHX_ regnode *prog)
2238 {
2239     register regnode *scan;     /* Current node. */
2240     regnode *next;              /* Next node. */
2241     regnode *inner;             /* Next node in internal branch. */
2242     register I32 nextchr;       /* renamed nextchr - nextchar colides with
2243                                    function of same name */
2244     register I32 n;             /* no or next */
2245     register I32 ln = 0;        /* len or last */
2246     register char *s = Nullch;  /* operand or save */
2247     register char *locinput = PL_reginput;
2248     register I32 c1 = 0, c2 = 0, paren; /* case fold search, parenth */
2249     int minmod = 0, sw = 0, logical = 0;
2250     I32 unwind = 0;
2251 #if 0
2252     I32 firstcp = PL_savestack_ix;
2253 #endif
2254     register bool do_utf8 = PL_reg_match_utf8;
2255 #ifdef DEBUGGING
2256     SV *dsv0 = PERL_DEBUG_PAD_ZERO(0);
2257     SV *dsv1 = PERL_DEBUG_PAD_ZERO(1);
2258     SV *dsv2 = PERL_DEBUG_PAD_ZERO(2);
2259 #endif
2260
2261 #ifdef DEBUGGING
2262     PL_regindent++;
2263 #endif
2264
2265     /* Note that nextchr is a byte even in UTF */
2266     nextchr = UCHARAT(locinput);
2267     scan = prog;
2268     while (scan != NULL) {
2269
2270         DEBUG_r( {
2271             SV *prop = sv_newmortal();
2272             int docolor = *PL_colors[0];
2273             int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
2274             int l = (PL_regeol - locinput) > taill ? taill : (PL_regeol - locinput);
2275             /* The part of the string before starttry has one color
2276                (pref0_len chars), between starttry and current
2277                position another one (pref_len - pref0_len chars),
2278                after the current position the third one.
2279                We assume that pref0_len <= pref_len, otherwise we
2280                decrease pref0_len.  */
2281             int pref_len = (locinput - PL_bostr) > (5 + taill) - l
2282                 ? (5 + taill) - l : locinput - PL_bostr;
2283             int pref0_len;
2284
2285             while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput - pref_len)))
2286                 pref_len++;
2287             pref0_len = pref_len  - (locinput - PL_reg_starttry);
2288             if (l + pref_len < (5 + taill) && l < PL_regeol - locinput)
2289                 l = ( PL_regeol - locinput > (5 + taill) - pref_len
2290                       ? (5 + taill) - pref_len : PL_regeol - locinput);
2291             while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput + l)))
2292                 l--;
2293             if (pref0_len < 0)
2294                 pref0_len = 0;
2295             if (pref0_len > pref_len)
2296                 pref0_len = pref_len;
2297             regprop(prop, scan);
2298             {
2299               char *s0 =
2300                 do_utf8 && OP(scan) != CANY ?
2301                 pv_uni_display(dsv0, (U8*)(locinput - pref_len),
2302                                pref0_len, 60, UNI_DISPLAY_REGEX) :
2303                 locinput - pref_len;
2304               int len0 = do_utf8 ? strlen(s0) : pref0_len;
2305               char *s1 = do_utf8 && OP(scan) != CANY ?
2306                 pv_uni_display(dsv1, (U8*)(locinput - pref_len + pref0_len),
2307                                pref_len - pref0_len, 60, UNI_DISPLAY_REGEX) :
2308                 locinput - pref_len + pref0_len;
2309               int len1 = do_utf8 ? strlen(s1) : pref_len - pref0_len;
2310               char *s2 = do_utf8 && OP(scan) != CANY ?
2311                 pv_uni_display(dsv2, (U8*)locinput,
2312                                PL_regeol - locinput, 60, UNI_DISPLAY_REGEX) :
2313                 locinput;
2314               int len2 = do_utf8 ? strlen(s2) : l;
2315               PerlIO_printf(Perl_debug_log,
2316                             "%4"IVdf" <%s%.*s%s%s%.*s%s%s%s%.*s%s>%*s|%3"IVdf":%*s%s\n",
2317                             (IV)(locinput - PL_bostr),
2318                             PL_colors[4],
2319                             len0, s0,
2320                             PL_colors[5],
2321                             PL_colors[2],
2322                             len1, s1,
2323                             PL_colors[3],
2324                             (docolor ? "" : "> <"),
2325                             PL_colors[0],
2326                             len2, s2,
2327                             PL_colors[1],
2328                             15 - l - pref_len + 1,
2329                             "",
2330                             (IV)(scan - PL_regprogram), PL_regindent*2, "",
2331                             SvPVX(prop));
2332             }
2333         });
2334
2335         next = scan + NEXT_OFF(scan);
2336         if (next == scan)
2337             next = NULL;
2338
2339         switch (OP(scan)) {
2340         case BOL:
2341             if (locinput == PL_bostr || (PL_multiline &&
2342                 (nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
2343             {
2344                 /* regtill = regbol; */
2345                 break;
2346             }
2347             sayNO;
2348         case MBOL:
2349             if (locinput == PL_bostr ||
2350                 ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n'))
2351             {
2352                 break;
2353             }
2354             sayNO;
2355         case SBOL:
2356             if (locinput == PL_bostr)
2357                 break;
2358             sayNO;
2359         case GPOS:
2360             if (locinput == PL_reg_ganch)
2361                 break;
2362             sayNO;
2363         case EOL:
2364             if (PL_multiline)
2365                 goto meol;
2366             else
2367                 goto seol;
2368         case MEOL:
2369           meol:
2370             if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2371                 sayNO;
2372             break;
2373         case SEOL:
2374           seol:
2375             if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2376                 sayNO;
2377             if (PL_regeol - locinput > 1)
2378                 sayNO;
2379             break;
2380         case EOS:
2381             if (PL_regeol != locinput)
2382                 sayNO;
2383             break;
2384         case SANY:
2385             if (!nextchr && locinput >= PL_regeol)
2386                 sayNO;
2387             if (do_utf8) {
2388                 locinput += PL_utf8skip[nextchr];
2389                 if (locinput > PL_regeol)
2390                     sayNO;
2391                 nextchr = UCHARAT(locinput);
2392             }
2393             else
2394                 nextchr = UCHARAT(++locinput);
2395             break;
2396         case CANY:
2397             if (!nextchr && locinput >= PL_regeol)
2398                 sayNO;
2399             nextchr = UCHARAT(++locinput);
2400             break;
2401         case REG_ANY:
2402             if ((!nextchr && locinput >= PL_regeol) || nextchr == '\n')
2403                 sayNO;
2404             if (do_utf8) {
2405                 locinput += PL_utf8skip[nextchr];
2406                 if (locinput > PL_regeol)
2407                     sayNO;
2408                 nextchr = UCHARAT(locinput);
2409             }
2410             else
2411                 nextchr = UCHARAT(++locinput);
2412             break;
2413         case EXACT:
2414             s = STRING(scan);
2415             ln = STR_LEN(scan);
2416             if (do_utf8 != UTF) {
2417                 /* The target and the pattern have differing utf8ness. */
2418                 char *l = locinput;
2419                 char *e = s + ln;
2420                 STRLEN ulen;
2421
2422                 if (do_utf8) {
2423                     /* The target is utf8, the pattern is not utf8. */
2424                     while (s < e) {
2425                         if (l >= PL_regeol)
2426                              sayNO;
2427                         if (NATIVE_TO_UNI(*(U8*)s) !=
2428                             utf8n_to_uvuni((U8*)l, UTF8_MAXLEN, &ulen,
2429                                            ckWARN(WARN_UTF8) ?
2430                                            0 : UTF8_ALLOW_ANY))
2431                              sayNO;
2432                         l += ulen;
2433                         s ++;
2434                     }
2435                 }
2436                 else {
2437                     /* The target is not utf8, the pattern is utf8. */
2438                     while (s < e) {
2439                         if (l >= PL_regeol)
2440                             sayNO;
2441                         if (NATIVE_TO_UNI(*((U8*)l)) !=
2442                             utf8n_to_uvuni((U8*)s, UTF8_MAXLEN, &ulen,
2443                                            ckWARN(WARN_UTF8) ?
2444                                            0 : UTF8_ALLOW_ANY))
2445                             sayNO;
2446                         s += ulen;
2447                         l ++;
2448                     }
2449                 }
2450                 locinput = l;
2451                 nextchr = UCHARAT(locinput);
2452                 break;
2453             }
2454             /* The target and the pattern have the same utf8ness. */
2455             /* Inline the first character, for speed. */
2456             if (UCHARAT(s) != nextchr)
2457                 sayNO;
2458             if (PL_regeol - locinput < ln)
2459                 sayNO;
2460             if (ln > 1 && memNE(s, locinput, ln))
2461                 sayNO;
2462             locinput += ln;
2463             nextchr = UCHARAT(locinput);
2464             break;
2465         case EXACTFL:
2466             PL_reg_flags |= RF_tainted;
2467             /* FALL THROUGH */
2468         case EXACTF:
2469             s = STRING(scan);
2470             ln = STR_LEN(scan);
2471
2472             if (do_utf8 || UTF) {
2473               /* Either target or the pattern are utf8. */
2474                 char *l = locinput;
2475                 char *e = PL_regeol;
2476
2477                 if (ibcmp_utf8(s, 0,  ln, (bool)UTF,
2478                                l, &e, 0,  do_utf8)) {
2479                      /* One more case for the sharp s:
2480                       * pack("U0U*", 0xDF) =~ /ss/i,
2481                       * the 0xC3 0x9F are the UTF-8
2482                       * byte sequence for the U+00DF. */
2483                      if (!(do_utf8 &&
2484                            toLOWER(s[0]) == 's' &&
2485                            ln >= 2 &&
2486                            toLOWER(s[1]) == 's' &&
2487                            (U8)l[0] == 0xC3 &&
2488                            e - l >= 2 &&
2489                            (U8)l[1] == 0x9F))
2490                           sayNO;
2491                 }
2492                 locinput = e;
2493                 nextchr = UCHARAT(locinput);
2494                 break;
2495             }
2496
2497             /* Neither the target and the pattern are utf8. */
2498
2499             /* Inline the first character, for speed. */
2500             if (UCHARAT(s) != nextchr &&
2501                 UCHARAT(s) != ((OP(scan) == EXACTF)
2502                                ? PL_fold : PL_fold_locale)[nextchr])
2503                 sayNO;
2504             if (PL_regeol - locinput < ln)
2505                 sayNO;
2506             if (ln > 1 && (OP(scan) == EXACTF
2507                            ? ibcmp(s, locinput, ln)
2508                            : ibcmp_locale(s, locinput, ln)))
2509                 sayNO;
2510             locinput += ln;
2511             nextchr = UCHARAT(locinput);
2512             break;
2513         case ANYOF:
2514             if (do_utf8) {
2515                 STRLEN inclasslen = PL_regeol - locinput;
2516
2517                 if (!reginclass(scan, (U8*)locinput, &inclasslen, do_utf8))
2518                     sayNO_ANYOF;
2519                 if (locinput >= PL_regeol)
2520                     sayNO;
2521                 locinput += inclasslen ? inclasslen : UTF8SKIP(locinput);
2522                 nextchr = UCHARAT(locinput);
2523                 break;
2524             }
2525             else {
2526                 if (nextchr < 0)
2527                     nextchr = UCHARAT(locinput);
2528                 if (!REGINCLASS(scan, (U8*)locinput))
2529                     sayNO_ANYOF;
2530                 if (!nextchr && locinput >= PL_regeol)
2531                     sayNO;
2532                 nextchr = UCHARAT(++locinput);
2533                 break;
2534             }
2535         no_anyof:
2536             /* If we might have the case of the German sharp s
2537              * in a casefolding Unicode character class. */
2538
2539             if (ANYOF_FOLD_SHARP_S(scan, locinput, PL_regeol)) {
2540                  locinput += SHARP_S_SKIP;
2541                  nextchr = UCHARAT(locinput);
2542             }
2543             else
2544                  sayNO;
2545             break;
2546         case ALNUML:
2547             PL_reg_flags |= RF_tainted;
2548             /* FALL THROUGH */
2549         case ALNUM:
2550             if (!nextchr)
2551                 sayNO;
2552             if (do_utf8) {
2553                 LOAD_UTF8_CHARCLASS(alnum,"a");
2554                 if (!(OP(scan) == ALNUM
2555                       ? swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8)
2556                       : isALNUM_LC_utf8((U8*)locinput)))
2557                 {
2558                     sayNO;
2559                 }
2560                 locinput += PL_utf8skip[nextchr];
2561                 nextchr = UCHARAT(locinput);
2562                 break;
2563             }
2564             if (!(OP(scan) == ALNUM
2565                   ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
2566                 sayNO;
2567             nextchr = UCHARAT(++locinput);
2568             break;
2569         case NALNUML:
2570             PL_reg_flags |= RF_tainted;
2571             /* FALL THROUGH */
2572         case NALNUM:
2573             if (!nextchr && locinput >= PL_regeol)
2574                 sayNO;
2575             if (do_utf8) {
2576                 LOAD_UTF8_CHARCLASS(alnum,"a");
2577                 if (OP(scan) == NALNUM
2578                     ? swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8)
2579                     : isALNUM_LC_utf8((U8*)locinput))
2580                 {
2581                     sayNO;
2582                 }
2583                 locinput += PL_utf8skip[nextchr];
2584                 nextchr = UCHARAT(locinput);
2585                 break;
2586             }
2587             if (OP(scan) == NALNUM
2588                 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
2589                 sayNO;
2590             nextchr = UCHARAT(++locinput);
2591             break;
2592         case BOUNDL:
2593         case NBOUNDL:
2594             PL_reg_flags |= RF_tainted;
2595             /* FALL THROUGH */
2596         case BOUND:
2597         case NBOUND:
2598             /* was last char in word? */
2599             if (do_utf8) {
2600                 if (locinput == PL_bostr)
2601                     ln = '\n';
2602                 else {
2603                     U8 *r = reghop3((U8*)locinput, -1, (U8*)PL_bostr);
2604                 
2605                     ln = utf8n_to_uvchr(r, UTF8SKIP(r), 0, 0);
2606                 }
2607                 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
2608                     ln = isALNUM_uni(ln);
2609                     LOAD_UTF8_CHARCLASS(alnum,"a");
2610                     n = swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8);
2611                 }
2612                 else {
2613                     ln = isALNUM_LC_uvchr(UNI_TO_NATIVE(ln));
2614                     n = isALNUM_LC_utf8((U8*)locinput);
2615                 }
2616             }
2617             else {
2618                 ln = (locinput != PL_bostr) ?
2619                     UCHARAT(locinput - 1) : '\n';
2620                 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
2621                     ln = isALNUM(ln);
2622                     n = isALNUM(nextchr);
2623                 }
2624                 else {
2625                     ln = isALNUM_LC(ln);
2626                     n = isALNUM_LC(nextchr);
2627                 }
2628             }
2629             if (((!ln) == (!n)) == (OP(scan) == BOUND ||
2630                                     OP(scan) == BOUNDL))
2631                     sayNO;
2632             break;
2633         case SPACEL:
2634             PL_reg_flags |= RF_tainted;
2635             /* FALL THROUGH */
2636         case SPACE:
2637             if (!nextchr)
2638                 sayNO;
2639             if (do_utf8) {
2640                 if (UTF8_IS_CONTINUED(nextchr)) {
2641                     LOAD_UTF8_CHARCLASS(space," ");
2642                     if (!(OP(scan) == SPACE
2643                           ? swash_fetch(PL_utf8_space, (U8*)locinput, do_utf8)
2644                           : isSPACE_LC_utf8((U8*)locinput)))
2645                     {
2646                         sayNO;
2647                     }
2648                     locinput += PL_utf8skip[nextchr];
2649                     nextchr = UCHARAT(locinput);
2650                     break;
2651                 }
2652                 if (!(OP(scan) == SPACE
2653                       ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2654                     sayNO;
2655                 nextchr = UCHARAT(++locinput);
2656             }
2657             else {
2658                 if (!(OP(scan) == SPACE
2659                       ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2660                     sayNO;
2661                 nextchr = UCHARAT(++locinput);
2662             }
2663             break;
2664         case NSPACEL:
2665             PL_reg_flags |= RF_tainted;
2666             /* FALL THROUGH */
2667         case NSPACE:
2668             if (!nextchr && locinput >= PL_regeol)
2669                 sayNO;
2670             if (do_utf8) {
2671                 LOAD_UTF8_CHARCLASS(space," ");
2672                 if (OP(scan) == NSPACE
2673                     ? swash_fetch(PL_utf8_space, (U8*)locinput, do_utf8)
2674                     : isSPACE_LC_utf8((U8*)locinput))
2675                 {
2676                     sayNO;
2677                 }
2678                 locinput += PL_utf8skip[nextchr];
2679                 nextchr = UCHARAT(locinput);
2680                 break;
2681             }
2682             if (OP(scan) == NSPACE
2683                 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
2684                 sayNO;
2685             nextchr = UCHARAT(++locinput);
2686             break;
2687         case DIGITL:
2688             PL_reg_flags |= RF_tainted;
2689             /* FALL THROUGH */
2690         case DIGIT:
2691             if (!nextchr)
2692                 sayNO;
2693             if (do_utf8) {
2694                 LOAD_UTF8_CHARCLASS(digit,"0");
2695                 if (!(OP(scan) == DIGIT
2696                       ? swash_fetch(PL_utf8_digit, (U8*)locinput, do_utf8)
2697                       : isDIGIT_LC_utf8((U8*)locinput)))
2698                 {
2699                     sayNO;
2700                 }
2701                 locinput += PL_utf8skip[nextchr];
2702                 nextchr = UCHARAT(locinput);
2703                 break;
2704             }
2705             if (!(OP(scan) == DIGIT
2706                   ? isDIGIT(nextchr) : isDIGIT_LC(nextchr)))
2707                 sayNO;
2708             nextchr = UCHARAT(++locinput);
2709             break;
2710         case NDIGITL:
2711             PL_reg_flags |= RF_tainted;
2712             /* FALL THROUGH */
2713         case NDIGIT:
2714             if (!nextchr && locinput >= PL_regeol)
2715                 sayNO;
2716             if (do_utf8) {
2717                 LOAD_UTF8_CHARCLASS(digit,"0");
2718                 if (OP(scan) == NDIGIT
2719                     ? swash_fetch(PL_utf8_digit, (U8*)locinput, do_utf8)
2720                     : isDIGIT_LC_utf8((U8*)locinput))
2721                 {
2722                     sayNO;
2723                 }
2724                 locinput += PL_utf8skip[nextchr];
2725                 nextchr = UCHARAT(locinput);
2726                 break;
2727             }
2728             if (OP(scan) == NDIGIT
2729                 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr))
2730                 sayNO;
2731             nextchr = UCHARAT(++locinput);
2732             break;
2733         case CLUMP:
2734             if (locinput >= PL_regeol)
2735                 sayNO;
2736             if  (do_utf8) {
2737                 LOAD_UTF8_CHARCLASS(mark,"~");
2738                 if (swash_fetch(PL_utf8_mark,(U8*)locinput, do_utf8))
2739                     sayNO;
2740                 locinput += PL_utf8skip[nextchr];
2741                 while (locinput < PL_regeol &&
2742                        swash_fetch(PL_utf8_mark,(U8*)locinput, do_utf8))
2743                     locinput += UTF8SKIP(locinput);
2744                 if (locinput > PL_regeol)
2745                     sayNO;
2746             } 
2747             else
2748                locinput++;
2749             nextchr = UCHARAT(locinput);
2750             break;
2751         case REFFL:
2752             PL_reg_flags |= RF_tainted;
2753             /* FALL THROUGH */
2754         case REF:
2755         case REFF:
2756             n = ARG(scan);  /* which paren pair */
2757             ln = PL_regstartp[n];
2758             PL_reg_leftiter = PL_reg_maxiter;           /* Void cache */
2759             if ((I32)*PL_reglastparen < n || ln == -1)
2760                 sayNO;                  /* Do not match unless seen CLOSEn. */
2761             if (ln == PL_regendp[n])
2762                 break;
2763
2764             s = PL_bostr + ln;
2765             if (do_utf8 && OP(scan) != REF) {   /* REF can do byte comparison */
2766                 char *l = locinput;
2767                 char *e = PL_bostr + PL_regendp[n];
2768                 /*
2769                  * Note that we can't do the "other character" lookup trick as
2770                  * in the 8-bit case (no pun intended) because in Unicode we
2771                  * have to map both upper and title case to lower case.
2772                  */
2773                 if (OP(scan) == REFF) {
2774                     STRLEN ulen1, ulen2;
2775                     U8 tmpbuf1[UTF8_MAXLEN_UCLC+1];
2776                     U8 tmpbuf2[UTF8_MAXLEN_UCLC+1];
2777                     while (s < e) {
2778                         if (l >= PL_regeol)
2779                             sayNO;
2780                         toLOWER_utf8((U8*)s, tmpbuf1, &ulen1);
2781                         toLOWER_utf8((U8*)l, tmpbuf2, &ulen2);
2782                         if (ulen1 != ulen2 || memNE((char *)tmpbuf1, (char *)tmpbuf2, ulen1))
2783                             sayNO;
2784                         s += ulen1;
2785                         l += ulen2;
2786                     }
2787                 }
2788                 locinput = l;
2789                 nextchr = UCHARAT(locinput);
2790                 break;
2791             }
2792
2793             /* Inline the first character, for speed. */
2794             if (UCHARAT(s) != nextchr &&
2795                 (OP(scan) == REF ||
2796                  (UCHARAT(s) != ((OP(scan) == REFF
2797                                   ? PL_fold : PL_fold_locale)[nextchr]))))
2798                 sayNO;
2799             ln = PL_regendp[n] - ln;
2800             if (locinput + ln > PL_regeol)
2801                 sayNO;
2802             if (ln > 1 && (OP(scan) == REF
2803                            ? memNE(s, locinput, ln)
2804                            : (OP(scan) == REFF
2805                               ? ibcmp(s, locinput, ln)
2806                               : ibcmp_locale(s, locinput, ln))))
2807                 sayNO;
2808             locinput += ln;
2809             nextchr = UCHARAT(locinput);
2810             break;
2811
2812         case NOTHING:
2813         case TAIL:
2814             break;
2815         case BACK:
2816             break;
2817         case EVAL:
2818         {
2819             dSP;
2820             OP_4tree *oop = PL_op;
2821             COP *ocurcop = PL_curcop;
2822             PAD *old_comppad;
2823             SV *ret;
2824         
2825             n = ARG(scan);
2826             PL_op = (OP_4tree*)PL_regdata->data[n];
2827             DEBUG_r( PerlIO_printf(Perl_debug_log, "  re_eval 0x%"UVxf"\n", PTR2UV(PL_op)) );
2828             PAD_SAVE_LOCAL(old_comppad, (PAD*)PL_regdata->data[n + 2]);
2829             PL_regendp[0] = PL_reg_magic->mg_len = locinput - PL_bostr;
2830
2831             {
2832                 SV **before = SP;
2833                 CALLRUNOPS(aTHX);                       /* Scalar context. */
2834                 SPAGAIN;
2835                 if (SP == before)
2836                     ret = &PL_sv_undef;   /* protect against empty (?{}) blocks. */
2837                 else {
2838                     ret = POPs;
2839                     PUTBACK;
2840                 }
2841             }
2842
2843             PL_op = oop;
2844             PAD_RESTORE_LOCAL(old_comppad);
2845             PL_curcop = ocurcop;
2846             if (logical) {
2847                 if (logical == 2) {     /* Postponed subexpression. */
2848                     regexp *re;
2849                     MAGIC *mg = Null(MAGIC*);
2850                     re_cc_state state;
2851                     CHECKPOINT cp, lastcp;
2852                     int toggleutf;
2853
2854                     if(SvROK(ret) || SvRMAGICAL(ret)) {
2855                         SV *sv = SvROK(ret) ? SvRV(ret) : ret;
2856
2857                         if(SvMAGICAL(sv))
2858                             mg = mg_find(sv, PERL_MAGIC_qr);
2859                     }
2860                     if (mg) {
2861                         re = (regexp *)mg->mg_obj;
2862                         (void)ReREFCNT_inc(re);
2863                     }
2864                     else {
2865                         STRLEN len;
2866                         char *t = SvPV(ret, len);
2867                         PMOP pm;
2868                         char *oprecomp = PL_regprecomp;
2869                         I32 osize = PL_regsize;
2870                         I32 onpar = PL_regnpar;
2871
2872                         Zero(&pm, 1, PMOP);
2873                         if (DO_UTF8(ret)) pm.op_pmdynflags |= PMdf_DYN_UTF8;
2874                         re = CALLREGCOMP(aTHX_ t, t + len, &pm);
2875                         if (!(SvFLAGS(ret)
2876                               & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)))
2877                             sv_magic(ret,(SV*)ReREFCNT_inc(re),
2878                                         PERL_MAGIC_qr,0,0);
2879                         PL_regprecomp = oprecomp;
2880                         PL_regsize = osize;
2881                         PL_regnpar = onpar;
2882                     }
2883                     DEBUG_r(
2884                         PerlIO_printf(Perl_debug_log,
2885                                       "Entering embedded `%s%.60s%s%s'\n",
2886                                       PL_colors[0],
2887                                       re->precomp,
2888                                       PL_colors[1],
2889                                       (strlen(re->precomp) > 60 ? "..." : ""))
2890                         );
2891                     state.node = next;
2892                     state.prev = PL_reg_call_cc;
2893                     state.cc = PL_regcc;
2894                     state.re = PL_reg_re;
2895
2896                     PL_regcc = 0;
2897                 
2898                     cp = regcppush(0);  /* Save *all* the positions. */
2899                     REGCP_SET(lastcp);
2900                     cache_re(re);
2901                     state.ss = PL_savestack_ix;
2902                     *PL_reglastparen = 0;
2903                     *PL_reglastcloseparen = 0;
2904                     PL_reg_call_cc = &state;
2905                     PL_reginput = locinput;
2906                     toggleutf = ((PL_reg_flags & RF_utf8) != 0) ^
2907                                 ((re->reganch & ROPT_UTF8) != 0);
2908                     if (toggleutf) PL_reg_flags ^= RF_utf8;
2909
2910                     /* XXXX This is too dramatic a measure... */
2911                     PL_reg_maxiter = 0;
2912
2913                     if (regmatch(re->program + 1)) {
2914                         /* Even though we succeeded, we need to restore
2915                            global variables, since we may be wrapped inside
2916                            SUSPEND, thus the match may be not finished yet. */
2917
2918                         /* XXXX Do this only if SUSPENDed? */
2919                         PL_reg_call_cc = state.prev;
2920                         PL_regcc = state.cc;
2921                         PL_reg_re = state.re;
2922                         cache_re(PL_reg_re);
2923                         if (toggleutf) PL_reg_flags ^= RF_utf8;
2924
2925                         /* XXXX This is too dramatic a measure... */
2926                         PL_reg_maxiter = 0;
2927
2928                         /* These are needed even if not SUSPEND. */
2929                         ReREFCNT_dec(re);
2930                         regcpblow(cp);
2931                         sayYES;
2932                     }
2933                     ReREFCNT_dec(re);
2934                     REGCP_UNWIND(lastcp);
2935                     regcppop();
2936                     PL_reg_call_cc = state.prev;
2937                     PL_regcc = state.cc;
2938                     PL_reg_re = state.re;
2939                     cache_re(PL_reg_re);
2940                     if (toggleutf) PL_reg_flags ^= RF_utf8;
2941
2942                     /* XXXX This is too dramatic a measure... */
2943                     PL_reg_maxiter = 0;
2944
2945                     logical = 0;
2946                     sayNO;
2947                 }
2948                 sw = SvTRUE(ret);
2949                 logical = 0;
2950             }
2951             else
2952                 sv_setsv(save_scalar(PL_replgv), ret);
2953             break;
2954         }
2955         case OPEN:
2956             n = ARG(scan);  /* which paren pair */
2957             PL_reg_start_tmp[n] = locinput;
2958             if (n > PL_regsize)
2959                 PL_regsize = n;
2960             break;
2961         case CLOSE:
2962             n = ARG(scan);  /* which paren pair */
2963             PL_regstartp[n] = PL_reg_start_tmp[n] - PL_bostr;
2964             PL_regendp[n] = locinput - PL_bostr;
2965             if (n > (I32)*PL_reglastparen)
2966                 *PL_reglastparen = n;
2967             *PL_reglastcloseparen = n;
2968             break;
2969         case GROUPP:
2970             n = ARG(scan);  /* which paren pair */
2971             sw = ((I32)*PL_reglastparen >= n && PL_regendp[n] != -1);
2972             break;
2973         case IFTHEN:
2974             PL_reg_leftiter = PL_reg_maxiter;           /* Void cache */
2975             if (sw)
2976                 next = NEXTOPER(NEXTOPER(scan));
2977             else {
2978                 next = scan + ARG(scan);
2979                 if (OP(next) == IFTHEN) /* Fake one. */
2980                     next = NEXTOPER(NEXTOPER(next));
2981             }
2982             break;
2983         case LOGICAL:
2984             logical = scan->flags;
2985             break;
2986 /*******************************************************************
2987  PL_regcc contains infoblock about the innermost (...)* loop, and
2988  a pointer to the next outer infoblock.
2989
2990  Here is how Y(A)*Z is processed (if it is compiled into CURLYX/WHILEM):
2991
2992    1) After matching X, regnode for CURLYX is processed;
2993
2994    2) This regnode creates infoblock on the stack, and calls
2995       regmatch() recursively with the starting point at WHILEM node;
2996
2997    3) Each hit of WHILEM node tries to match A and Z (in the order
2998       depending on the current iteration, min/max of {min,max} and
2999       greediness).  The information about where are nodes for "A"
3000       and "Z" is read from the infoblock, as is info on how many times "A"
3001       was already matched, and greediness.
3002
3003    4) After A matches, the same WHILEM node is hit again.
3004
3005    5) Each time WHILEM is hit, PL_regcc is the infoblock created by CURLYX
3006       of the same pair.  Thus when WHILEM tries to match Z, it temporarily
3007       resets PL_regcc, since this Y(A)*Z can be a part of some other loop:
3008       as in (Y(A)*Z)*.  If Z matches, the automaton will hit the WHILEM node
3009       of the external loop.
3010
3011  Currently present infoblocks form a tree with a stem formed by PL_curcc
3012  and whatever it mentions via ->next, and additional attached trees
3013  corresponding to temporarily unset infoblocks as in "5" above.
3014
3015  In the following picture infoblocks for outer loop of
3016  (Y(A)*?Z)*?T are denoted O, for inner I.  NULL starting block
3017  is denoted by x.  The matched string is YAAZYAZT.  Temporarily postponed
3018  infoblocks are drawn below the "reset" infoblock.
3019
3020  In fact in the picture below we do not show failed matches for Z and T
3021  by WHILEM blocks.  [We illustrate minimal matches, since for them it is
3022  more obvious *why* one needs to *temporary* unset infoblocks.]
3023
3024   Matched       REx position    InfoBlocks      Comment
3025                 (Y(A)*?Z)*?T    x
3026                 Y(A)*?Z)*?T     x <- O
3027   Y             (A)*?Z)*?T      x <- O
3028   Y             A)*?Z)*?T       x <- O <- I
3029   YA            )*?Z)*?T        x <- O <- I
3030   YA            A)*?Z)*?T       x <- O <- I
3031   YAA           )*?Z)*?T        x <- O <- I
3032   YAA           Z)*?T           x <- O          # Temporary unset I
3033                                      I
3034
3035   YAAZ          Y(A)*?Z)*?T     x <- O
3036                                      I
3037
3038   YAAZY         (A)*?Z)*?T      x <- O
3039                                      I
3040
3041   YAAZY         A)*?Z)*?T       x <- O <- I
3042                                      I
3043
3044   YAAZYA        )*?Z)*?T        x <- O <- I     
3045                                      I
3046
3047   YAAZYA        Z)*?T           x <- O          # Temporary unset I
3048                                      I,I
3049
3050   YAAZYAZ       )*?T            x <- O
3051                                      I,I
3052
3053   YAAZYAZ       T               x               # Temporary unset O
3054                                 O
3055                                 I,I
3056
3057   YAAZYAZT                      x
3058                                 O
3059                                 I,I
3060  *******************************************************************/
3061         case CURLYX: {
3062                 CURCUR cc;
3063                 CHECKPOINT cp = PL_savestack_ix;
3064                 /* No need to save/restore up to this paren */
3065                 I32 parenfloor = scan->flags;
3066
3067                 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
3068                     next += ARG(next);
3069                 cc.oldcc = PL_regcc;
3070                 PL_regcc = &cc;
3071                 /* XXXX Probably it is better to teach regpush to support
3072                    parenfloor > PL_regsize... */
3073                 if (parenfloor > (I32)*PL_reglastparen)
3074                     parenfloor = *PL_reglastparen; /* Pessimization... */
3075                 cc.parenfloor = parenfloor;
3076                 cc.cur = -1;
3077                 cc.min = ARG1(scan);
3078                 cc.max  = ARG2(scan);
3079                 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
3080                 cc.next = next;
3081                 cc.minmod = minmod;
3082                 cc.lastloc = 0;
3083                 PL_reginput = locinput;
3084                 n = regmatch(PREVOPER(next));   /* start on the WHILEM */
3085                 regcpblow(cp);
3086                 PL_regcc = cc.oldcc;
3087                 saySAME(n);
3088             }
3089             /* NOT REACHED */
3090         case WHILEM: {
3091                 /*
3092                  * This is really hard to understand, because after we match
3093                  * what we're trying to match, we must make sure the rest of
3094                  * the REx is going to match for sure, and to do that we have
3095                  * to go back UP the parse tree by recursing ever deeper.  And
3096                  * if it fails, we have to reset our parent's current state
3097                  * that we can try again after backing off.
3098                  */
3099
3100                 CHECKPOINT cp, lastcp;
3101                 CURCUR* cc = PL_regcc;
3102                 char *lastloc = cc->lastloc; /* Detection of 0-len. */
3103                 
3104                 n = cc->cur + 1;        /* how many we know we matched */
3105                 PL_reginput = locinput;
3106
3107                 DEBUG_r(
3108                     PerlIO_printf(Perl_debug_log,
3109                                   "%*s  %ld out of %ld..%ld  cc=%"UVxf"\n",
3110                                   REPORT_CODE_OFF+PL_regindent*2, "",
3111                                   (long)n, (long)cc->min,
3112                                   (long)cc->max, PTR2UV(cc))
3113                     );
3114
3115                 /* If degenerate scan matches "", assume scan done. */
3116
3117                 if (locinput == cc->lastloc && n >= cc->min) {
3118                     PL_regcc = cc->oldcc;
3119                     if (PL_regcc)
3120                         ln = PL_regcc->cur;
3121                     DEBUG_r(
3122                         PerlIO_printf(Perl_debug_log,
3123                            "%*s  empty match detected, try continuation...\n",
3124                            REPORT_CODE_OFF+PL_regindent*2, "")
3125                         );
3126                     if (regmatch(cc->next))
3127                         sayYES;
3128                     if (PL_regcc)
3129                         PL_regcc->cur = ln;
3130                     PL_regcc = cc;
3131                     sayNO;
3132                 }
3133
3134                 /* First just match a string of min scans. */
3135
3136                 if (n < cc->min) {
3137                     cc->cur = n;
3138                     cc->lastloc = locinput;
3139                     if (regmatch(cc->scan))
3140                         sayYES;
3141                     cc->cur = n - 1;
3142                     cc->lastloc = lastloc;
3143                     sayNO;
3144                 }
3145
3146                 if (scan->flags) {
3147                     /* Check whether we already were at this position.
3148                         Postpone detection until we know the match is not
3149                         *that* much linear. */
3150                 if (!PL_reg_maxiter) {
3151                     PL_reg_maxiter = (PL_regeol - PL_bostr + 1) * (scan->flags>>4);
3152                     PL_reg_leftiter = PL_reg_maxiter;
3153                 }
3154                 if (PL_reg_leftiter-- == 0) {
3155                     I32 size = (PL_reg_maxiter + 7)/8;
3156                     if (PL_reg_poscache) {
3157                         if ((I32)PL_reg_poscache_size < size) {
3158                             Renew(PL_reg_poscache, size, char);
3159                             PL_reg_poscache_size = size;
3160                         }
3161                         Zero(PL_reg_poscache, size, char);
3162                     }
3163                     else {
3164                         PL_reg_poscache_size = size;
3165                         Newz(29, PL_reg_poscache, size, char);
3166                     }
3167                     DEBUG_r(
3168                         PerlIO_printf(Perl_debug_log,
3169               "%sDetected a super-linear match, switching on caching%s...\n",
3170                                       PL_colors[4], PL_colors[5])
3171                         );
3172                 }
3173                 if (PL_reg_leftiter < 0) {
3174                     I32 o = locinput - PL_bostr, b;
3175
3176                     o = (scan->flags & 0xf) - 1 + o * (scan->flags>>4);
3177                     b = o % 8;
3178                     o /= 8;
3179                     if (PL_reg_poscache[o] & (1<<b)) {
3180                     DEBUG_r(
3181                         PerlIO_printf(Perl_debug_log,
3182                                       "%*s  already tried at this position...\n",
3183                                       REPORT_CODE_OFF+PL_regindent*2, "")
3184                         );
3185                         sayNO_SILENT;
3186                     }
3187                     PL_reg_poscache[o] |= (1<<b);
3188                 }
3189                 }
3190
3191                 /* Prefer next over scan for minimal matching. */
3192
3193                 if (cc->minmod) {
3194                     PL_regcc = cc->oldcc;
3195                     if (PL_regcc)
3196                         ln = PL_regcc->cur;
3197                     cp = regcppush(cc->parenfloor);
3198                     REGCP_SET(lastcp);
3199                     if (regmatch(cc->next)) {
3200                         regcpblow(cp);
3201                         sayYES; /* All done. */
3202                     }
3203                     REGCP_UNWIND(lastcp);
3204                     regcppop();
3205                     if (PL_regcc)
3206                         PL_regcc->cur = ln;
3207                     PL_regcc = cc;
3208
3209                     if (n >= cc->max) { /* Maximum greed exceeded? */
3210                         if (ckWARN(WARN_REGEXP) && n >= REG_INFTY
3211                             && !(PL_reg_flags & RF_warned)) {
3212                             PL_reg_flags |= RF_warned;
3213                             Perl_warner(aTHX_ packWARN(WARN_REGEXP), "%s limit (%d) exceeded",
3214                                  "Complex regular subexpression recursion",
3215                                  REG_INFTY - 1);
3216                         }
3217                         sayNO;
3218                     }
3219
3220                     DEBUG_r(
3221                         PerlIO_printf(Perl_debug_log,
3222                                       "%*s  trying longer...\n",
3223                                       REPORT_CODE_OFF+PL_regindent*2, "")
3224                         );
3225                     /* Try scanning more and see if it helps. */
3226                     PL_reginput = locinput;
3227                     cc->cur = n;
3228                     cc->lastloc = locinput;
3229                     cp = regcppush(cc->parenfloor);
3230                     REGCP_SET(lastcp);
3231                     if (regmatch(cc->scan)) {
3232                         regcpblow(cp);
3233                         sayYES;
3234                     }
3235                     REGCP_UNWIND(lastcp);
3236                     regcppop();
3237                     cc->cur = n - 1;
3238                     cc->lastloc = lastloc;
3239                     sayNO;
3240                 }
3241
3242                 /* Prefer scan over next for maximal matching. */
3243
3244                 if (n < cc->max) {      /* More greed allowed? */
3245                     cp = regcppush(cc->parenfloor);
3246                     cc->cur = n;
3247                     cc->lastloc = locinput;
3248                     REGCP_SET(lastcp);
3249                     if (regmatch(cc->scan)) {
3250                         regcpblow(cp);
3251                         sayYES;
3252                     }
3253                     REGCP_UNWIND(lastcp);
3254                     regcppop();         /* Restore some previous $<digit>s? */
3255                     PL_reginput = locinput;
3256                     DEBUG_r(
3257                         PerlIO_printf(Perl_debug_log,
3258                                       "%*s  failed, try continuation...\n",
3259                                       REPORT_CODE_OFF+PL_regindent*2, "")
3260                         );
3261                 }
3262                 if (ckWARN(WARN_REGEXP) && n >= REG_INFTY
3263                         && !(PL_reg_flags & RF_warned)) {
3264                     PL_reg_flags |= RF_warned;
3265                     Perl_warner(aTHX_ packWARN(WARN_REGEXP), "%s limit (%d) exceeded",
3266                          "Complex regular subexpression recursion",
3267                          REG_INFTY - 1);
3268                 }
3269
3270                 /* Failed deeper matches of scan, so see if this one works. */
3271                 PL_regcc = cc->oldcc;
3272                 if (PL_regcc)
3273                     ln = PL_regcc->cur;
3274                 if (regmatch(cc->next))
3275                     sayYES;
3276                 if (PL_regcc)
3277                     PL_regcc->cur = ln;
3278                 PL_regcc = cc;
3279                 cc->cur = n - 1;
3280                 cc->lastloc = lastloc;
3281                 sayNO;
3282             }
3283             /* NOT REACHED */
3284         case BRANCHJ:
3285             next = scan + ARG(scan);
3286             if (next == scan)
3287                 next = NULL;
3288             inner = NEXTOPER(NEXTOPER(scan));
3289             goto do_branch;
3290         case BRANCH:
3291             inner = NEXTOPER(scan);
3292           do_branch:
3293             {
3294                 c1 = OP(scan);
3295                 if (OP(next) != c1)     /* No choice. */
3296                     next = inner;       /* Avoid recursion. */
3297                 else {
3298                     I32 lastparen = *PL_reglastparen;
3299                     I32 unwind1;
3300                     re_unwind_branch_t *uw;
3301
3302                     /* Put unwinding data on stack */
3303                     unwind1 = SSNEWt(1,re_unwind_branch_t);
3304                     uw = SSPTRt(unwind1,re_unwind_branch_t);
3305                     uw->prev = unwind;
3306                     unwind = unwind1;
3307                     uw->type = ((c1 == BRANCH)
3308                                 ? RE_UNWIND_BRANCH
3309                                 : RE_UNWIND_BRANCHJ);
3310                     uw->lastparen = lastparen;
3311                     uw->next = next;
3312                     uw->locinput = locinput;
3313                     uw->nextchr = nextchr;
3314 #ifdef DEBUGGING
3315                     uw->regindent = ++PL_regindent;
3316 #endif
3317
3318                     REGCP_SET(uw->lastcp);
3319
3320                     /* Now go into the first branch */
3321                     next = inner;
3322                 }
3323             }
3324             break;
3325         case MINMOD:
3326             minmod = 1;
3327             break;
3328         case CURLYM:
3329         {
3330             I32 l = 0;
3331             CHECKPOINT lastcp;
3332         
3333             /* We suppose that the next guy does not need
3334                backtracking: in particular, it is of constant length,
3335                and has no parenths to influence future backrefs. */
3336             ln = ARG1(scan);  /* min to match */
3337             n  = ARG2(scan);  /* max to match */
3338             paren = scan->flags;
3339             if (paren) {
3340                 if (paren > PL_regsize)
3341                     PL_regsize = paren;
3342                 if (paren > (I32)*PL_reglastparen)
3343                     *PL_reglastparen = paren;
3344             }
3345             scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
3346             if (paren)
3347                 scan += NEXT_OFF(scan); /* Skip former OPEN. */
3348             PL_reginput = locinput;
3349             if (minmod) {
3350                 minmod = 0;
3351                 if (ln && regrepeat_hard(scan, ln, &l) < ln)
3352                     sayNO;
3353                 /* if we matched something zero-length we don't need to
3354                    backtrack - capturing parens are already defined, so
3355                    the caveat in the maximal case doesn't apply
3356
3357                    XXXX if ln == 0, we can redo this check first time
3358                    through the following loop
3359                 */
3360                 if (ln && l == 0)
3361                     n = ln;     /* don't backtrack */
3362                 locinput = PL_reginput;
3363                 if (HAS_TEXT(next) || JUMPABLE(next)) {
3364                     regnode *text_node = next;
3365
3366                     if (! HAS_TEXT(text_node)) FIND_NEXT_IMPT(text_node);
3367
3368                     if (! HAS_TEXT(text_node)) c1 = c2 = -1000;
3369                     else {
3370                         if (PL_regkind[(U8)OP(text_node)] == REF) {
3371                             I32 n, ln;
3372                             n = ARG(text_node);  /* which paren pair */
3373                             ln = PL_regstartp[n];
3374                             /* assume yes if we haven't seen CLOSEn */
3375                             if (
3376                                 (I32)*PL_reglastparen < n ||
3377                                 ln == -1 ||
3378                                 ln == PL_regendp[n]
3379                             ) {
3380                                 c1 = c2 = -1000;
3381                                 goto assume_ok_MM;
3382                             }
3383                             c1 = *(PL_bostr + ln);
3384                         }
3385                         else { c1 = (U8)*STRING(text_node); }
3386                         if (OP(text_node) == EXACTF || OP(text_node) == REFF)
3387                             c2 = PL_fold[c1];
3388                         else if (OP(text_node) == EXACTFL || OP(text_node) == REFFL)
3389                             c2 = PL_fold_locale[c1];
3390                         else
3391                             c2 = c1;
3392                     }
3393                 }
3394                 else
3395                     c1 = c2 = -1000;
3396             assume_ok_MM:
3397                 REGCP_SET(lastcp);
3398                 /* This may be improved if l == 0.  */
3399                 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
3400                     /* If it could work, try it. */
3401                     if (c1 == -1000 ||
3402                         UCHARAT(PL_reginput) == c1 ||
3403                         UCHARAT(PL_reginput) == c2)
3404                     {
3405                         if (paren) {
3406                             if (ln) {
3407                                 PL_regstartp[paren] =
3408                                     HOPc(PL_reginput, -l) - PL_bostr;
3409                                 PL_regendp[paren] = PL_reginput - PL_bostr;
3410                             }
3411                             else
3412                                 PL_regendp[paren] = -1;
3413                         }
3414                         if (regmatch(next))
3415                             sayYES;
3416                         REGCP_UNWIND(lastcp);
3417                     }
3418                     /* Couldn't or didn't -- move forward. */
3419                     PL_reginput = locinput;
3420                     if (regrepeat_hard(scan, 1, &l)) {
3421                         ln++;
3422                         locinput = PL_reginput;
3423                     }
3424                     else
3425                         sayNO;
3426                 }
3427             }
3428             else {
3429                 n = regrepeat_hard(scan, n, &l);
3430                 /* if we matched something zero-length we don't need to
3431                    backtrack, unless the minimum count is zero and we
3432                    are capturing the result - in that case the capture
3433                    being defined or not may affect later execution
3434                 */
3435                 if (n != 0 && l == 0 && !(paren && ln == 0))
3436                     ln = n;     /* don't backtrack */
3437                 locinput = PL_reginput;
3438                 DEBUG_r(
3439                     PerlIO_printf(Perl_debug_log,
3440                                   "%*s  matched %"IVdf" times, len=%"IVdf"...\n",
3441                                   (int)(REPORT_CODE_OFF+PL_regindent*2), "",
3442                                   (IV) n, (IV)l)
3443                     );
3444                 if (n >= ln) {
3445                     if (HAS_TEXT(next) || JUMPABLE(next)) {
3446                         regnode *text_node = next;
3447
3448                         if (! HAS_TEXT(text_node)) FIND_NEXT_IMPT(text_node);
3449
3450                         if (! HAS_TEXT(text_node)) c1 = c2 = -1000;
3451                         else {
3452                             if (PL_regkind[(U8)OP(text_node)] == REF) {
3453                                 I32 n, ln;
3454                                 n = ARG(text_node);  /* which paren pair */
3455                                 ln = PL_regstartp[n];
3456                                 /* assume yes if we haven't seen CLOSEn */
3457                                 if (
3458                                     (I32)*PL_reglastparen < n ||
3459                                     ln == -1 ||
3460                                     ln == PL_regendp[n]
3461                                 ) {
3462                                     c1 = c2 = -1000;
3463                                     goto assume_ok_REG;
3464                                 }
3465                                 c1 = *(PL_bostr + ln);
3466                             }
3467                             else { c1 = (U8)*STRING(text_node); }
3468
3469                             if (OP(text_node) == EXACTF || OP(text_node) == REFF)
3470                                 c2 = PL_fold[c1];
3471                             else if (OP(text_node) == EXACTFL || OP(text_node) == REFFL)
3472                                 c2 = PL_fold_locale[c1];
3473                             else
3474                                 c2 = c1;
3475                         }
3476                     }
3477                     else
3478                         c1 = c2 = -1000;
3479                 }
3480             assume_ok_REG:
3481                 REGCP_SET(lastcp);
3482                 while (n >= ln) {
3483                     /* If it could work, try it. */
3484                     if (c1 == -1000 ||
3485                         UCHARAT(PL_reginput) == c1 ||
3486                         UCHARAT(PL_reginput) == c2)
3487                     {
3488                         DEBUG_r(
3489                                 PerlIO_printf(Perl_debug_log,
3490                                               "%*s  trying tail with n=%"IVdf"...\n",
3491                                               (int)(REPORT_CODE_OFF+PL_regindent*2), "", (IV)n)
3492                             );
3493                         if (paren) {
3494                             if (n) {
3495                                 PL_regstartp[paren] = HOPc(PL_reginput, -l) - PL_bostr;
3496                                 PL_regendp[paren] = PL_reginput - PL_bostr;
3497                             }
3498                             else
3499                                 PL_regendp[paren] = -1;
3500                         }
3501                         if (regmatch(next))
3502                             sayYES;
3503                         REGCP_UNWIND(lastcp);
3504                     }
3505                     /* Couldn't or didn't -- back up. */
3506                     n--;
3507                     locinput = HOPc(locinput, -l);
3508                     PL_reginput = locinput;
3509                 }
3510             }
3511             sayNO;
3512             break;
3513         }
3514         case CURLYN:
3515             paren = scan->flags;        /* Which paren to set */
3516             if (paren > PL_regsize)
3517                 PL_regsize = paren;
3518             if (paren > (I32)*PL_reglastparen)
3519                 *PL_reglastparen = paren;
3520             ln = ARG1(scan);  /* min to match */
3521             n  = ARG2(scan);  /* max to match */
3522             scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
3523             goto repeat;
3524         case CURLY:
3525             paren = 0;
3526             ln = ARG1(scan);  /* min to match */
3527             n  = ARG2(scan);  /* max to match */
3528             scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
3529             goto repeat;
3530         case STAR:
3531             ln = 0;
3532             n = REG_INFTY;
3533             scan = NEXTOPER(scan);
3534             paren = 0;
3535             goto repeat;
3536         case PLUS:
3537             ln = 1;
3538             n = REG_INFTY;
3539             scan = NEXTOPER(scan);
3540             paren = 0;
3541           repeat:
3542             /*
3543             * Lookahead to avoid useless match attempts
3544             * when we know what character comes next.
3545             */
3546
3547             /*
3548             * Used to only do .*x and .*?x, but now it allows
3549             * for )'s, ('s and (?{ ... })'s to be in the way
3550             * of the quantifier and the EXACT-like node.  -- japhy
3551             */
3552
3553             if (HAS_TEXT(next) || JUMPABLE(next)) {
3554                 U8 *s;
3555                 regnode *text_node = next;
3556
3557                 if (! HAS_TEXT(text_node)) FIND_NEXT_IMPT(text_node);
3558
3559                 if (! HAS_TEXT(text_node)) c1 = c2 = -1000;
3560                 else {
3561                     if (PL_regkind[(U8)OP(text_node)] == REF) {
3562                         I32 n, ln;
3563                         n = ARG(text_node);  /* which paren pair */
3564                         ln = PL_regstartp[n];
3565                         /* assume yes if we haven't seen CLOSEn */
3566                         if (
3567                             (I32)*PL_reglastparen < n ||
3568                             ln == -1 ||
3569                             ln == PL_regendp[n]
3570                         ) {
3571                             c1 = c2 = -1000;
3572                             goto assume_ok_easy;
3573                         }
3574                         s = (U8*)PL_bostr + ln;
3575                     }
3576                     else { s = (U8*)STRING(text_node); }
3577
3578                     if (!UTF) {
3579                         c2 = c1 = *s;
3580                         if (OP(text_node) == EXACTF || OP(text_node) == REFF)
3581                             c2 = PL_fold[c1];
3582                         else if (OP(text_node) == EXACTFL || OP(text_node) == REFFL)
3583                             c2 = PL_fold_locale[c1];
3584                     }
3585                     else { /* UTF */
3586                         if (OP(text_node) == EXACTF || OP(text_node) == REFF) {
3587                              STRLEN ulen1, ulen2;
3588                              U8 tmpbuf1[UTF8_MAXLEN_UCLC+1];
3589                              U8 tmpbuf2[UTF8_MAXLEN_UCLC+1];
3590
3591                              to_utf8_lower((U8*)s, tmpbuf1, &ulen1);
3592                              to_utf8_upper((U8*)s, tmpbuf2, &ulen2);
3593
3594                              c1 = utf8n_to_uvuni(tmpbuf1, UTF8_MAXLEN, 0,
3595                                                  ckWARN(WARN_UTF8) ?
3596                                                  0 : UTF8_ALLOW_ANY);
3597                              c2 = utf8n_to_uvuni(tmpbuf2, UTF8_MAXLEN, 0,
3598                                                  ckWARN(WARN_UTF8) ?
3599                                                  0 : UTF8_ALLOW_ANY);
3600                         }
3601                         else {
3602                             c2 = c1 = utf8n_to_uvchr(s, UTF8_MAXLEN, 0,
3603                                                      ckWARN(WARN_UTF8) ?
3604                                                      0 : UTF8_ALLOW_ANY);
3605                         }
3606                     }
3607                 }
3608             }
3609             else
3610                 c1 = c2 = -1000;
3611         assume_ok_easy:
3612             PL_reginput = locinput;
3613             if (minmod) {
3614                 CHECKPOINT lastcp;
3615                 minmod = 0;
3616                 if (ln && regrepeat(scan, ln) < ln)
3617                     sayNO;
3618                 locinput = PL_reginput;
3619                 REGCP_SET(lastcp);
3620                 if (c1 != -1000) {
3621                     char *e; /* Should not check after this */
3622                     char *old = locinput;
3623                     int count = 0;
3624
3625                     if  (n == REG_INFTY) {
3626                         e = PL_regeol - 1;
3627                         if (do_utf8)
3628                             while (UTF8_IS_CONTINUATION(*(U8*)e))
3629                                 e--;
3630                     }
3631                     else if (do_utf8) {
3632                         int m = n - ln;
3633                         for (e = locinput;
3634                              m >0 && e + UTF8SKIP(e) <= PL_regeol; m--)
3635                             e += UTF8SKIP(e);
3636                     }
3637                     else {
3638                         e = locinput + n - ln;
3639                         if (e >= PL_regeol)
3640                             e = PL_regeol - 1;
3641                     }
3642                     while (1) {
3643                         /* Find place 'next' could work */
3644                         if (!do_utf8) {
3645                             if (c1 == c2) {
3646                                 while (locinput <= e &&
3647                                        UCHARAT(locinput) != c1)
3648                                     locinput++;
3649                             } else {
3650                                 while (locinput <= e
3651                                        && UCHARAT(locinput) != c1
3652                                        && UCHARAT(locinput) != c2)
3653                                     locinput++;
3654                             }
3655                             count = locinput - old;
3656                         }
3657                         else {
3658                             STRLEN len;
3659                             if (c1 == c2) {
3660                                 /* count initialised to
3661                                  * utf8_distance(old, locinput) */
3662                                 while (locinput <= e &&
3663                                        utf8n_to_uvchr((U8*)locinput,
3664                                                       UTF8_MAXLEN, &len,
3665                                                       ckWARN(WARN_UTF8) ?
3666                                                       0 : UTF8_ALLOW_ANY) != (UV)c1) {
3667                                     locinput += len;
3668                                     count++;
3669                                 }
3670                             } else {
3671                                 /* count initialised to
3672                                  * utf8_distance(old, locinput) */
3673                                 while (locinput <= e) {
3674                                     UV c = utf8n_to_uvchr((U8*)locinput,
3675                                                           UTF8_MAXLEN, &len,
3676                                                           ckWARN(WARN_UTF8) ?
3677                                                           0 : UTF8_ALLOW_ANY);
3678                                     if (c == (UV)c1 || c == (UV)c2)
3679                                         break;
3680                                     locinput += len;
3681                                     count++;
3682                                 }
3683                             }
3684                         }
3685                         if (locinput > e)
3686                             sayNO;
3687                         /* PL_reginput == old now */
3688                         if (locinput != old) {
3689                             ln = 1;     /* Did some */
3690                             if (regrepeat(scan, count) < count)
3691                                 sayNO;
3692                         }
3693                         /* PL_reginput == locinput now */
3694                         TRYPAREN(paren, ln, locinput);
3695                         PL_reginput = locinput; /* Could be reset... */
3696                         REGCP_UNWIND(lastcp);
3697                         /* Couldn't or didn't -- move forward. */
3698                         old = locinput;
3699                         if (do_utf8)
3700                             locinput += UTF8SKIP(locinput);
3701                         else
3702                             locinput++;
3703                         count = 1;
3704                     }
3705                 }
3706                 else
3707                 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
3708                     UV c;
3709                     if (c1 != -1000) {
3710                         if (do_utf8)
3711                             c = utf8n_to_uvchr((U8*)PL_reginput,
3712                                                UTF8_MAXLEN, 0,
3713                                                ckWARN(WARN_UTF8) ?
3714                                                0 : UTF8_ALLOW_ANY);
3715                         else
3716                             c = UCHARAT(PL_reginput);
3717                         /* If it could work, try it. */
3718                         if (c == (UV)c1 || c == (UV)c2)
3719                         {
3720                             TRYPAREN(paren, ln, PL_reginput);
3721                             REGCP_UNWIND(lastcp);
3722                         }
3723                     }
3724                     /* If it could work, try it. */
3725                     else if (c1 == -1000)
3726                     {
3727                         TRYPAREN(paren, ln, PL_reginput);
3728                         REGCP_UNWIND(lastcp);
3729                     }
3730                     /* Couldn't or didn't -- move forward. */
3731                     PL_reginput = locinput;
3732                     if (regrepeat(scan, 1)) {
3733                         ln++;
3734                         locinput = PL_reginput;
3735                     }
3736                     else
3737                         sayNO;
3738                 }
3739             }
3740             else {
3741                 CHECKPOINT lastcp;
3742                 n = regrepeat(scan, n);
3743                 locinput = PL_reginput;
3744                 if (ln < n && PL_regkind[(U8)OP(next)] == EOL &&
3745                     ((!PL_multiline && OP(next) != MEOL) ||
3746                         OP(next) == SEOL || OP(next) == EOS))
3747                 {
3748                     ln = n;                     /* why back off? */
3749                     /* ...because $ and \Z can match before *and* after
3750                        newline at the end.  Consider "\n\n" =~ /\n+\Z\n/.
3751                        We should back off by one in this case. */
3752                     if (UCHARAT(PL_reginput - 1) == '\n' && OP(next) != EOS)
3753                         ln--;
3754                 }
3755                 REGCP_SET(lastcp);
3756                 if (paren) {
3757                     UV c = 0;
3758                     while (n >= ln) {
3759                         if (c1 != -1000) {
3760                             if (do_utf8)
3761                                 c = utf8n_to_uvchr((U8*)PL_reginput,
3762                                                    UTF8_MAXLEN, 0,
3763                                                    ckWARN(WARN_UTF8) ?
3764                                                    0 : UTF8_ALLOW_ANY);
3765                             else
3766                                 c = UCHARAT(PL_reginput);
3767                         }
3768                         /* If it could work, try it. */
3769                         if (c1 == -1000 || c == (UV)c1 || c == (UV)c2)
3770                             {
3771                                 TRYPAREN(paren, n, PL_reginput);
3772                                 REGCP_UNWIND(lastcp);
3773                             }
3774                         /* Couldn't or didn't -- back up. */
3775                         n--;
3776                         PL_reginput = locinput = HOPc(locinput, -1);
3777                     }
3778                 }
3779                 else {
3780                     UV c = 0;
3781                     while (n >= ln) {
3782                         if (c1 != -1000) {
3783                             if (do_utf8)
3784                                 c = utf8n_to_uvchr((U8*)PL_reginput,
3785                                                    UTF8_MAXLEN, 0,
3786                                                    ckWARN(WARN_UTF8) ?
3787                                                    0 : UTF8_ALLOW_ANY);
3788                             else
3789                                 c = UCHARAT(PL_reginput);
3790                         }
3791                         /* If it could work, try it. */
3792                         if (c1 == -1000 || c == (UV)c1 || c == (UV)c2)
3793                             {
3794                                 TRYPAREN(paren, n, PL_reginput);
3795                                 REGCP_UNWIND(lastcp);
3796                             }
3797                         /* Couldn't or didn't -- back up. */
3798                         n--;
3799                         PL_reginput = locinput = HOPc(locinput, -1);
3800                     }
3801                 }
3802             }
3803             sayNO;
3804             break;
3805         case END:
3806             if (PL_reg_call_cc) {
3807                 re_cc_state *cur_call_cc = PL_reg_call_cc;
3808                 CURCUR *cctmp = PL_regcc;
3809                 regexp *re = PL_reg_re;
3810                 CHECKPOINT cp, lastcp;
3811                 
3812                 cp = regcppush(0);      /* Save *all* the positions. */
3813                 REGCP_SET(lastcp);
3814                 regcp_set_to(PL_reg_call_cc->ss); /* Restore parens of
3815                                                     the caller. */
3816                 PL_reginput = locinput; /* Make position available to
3817                                            the callcc. */
3818                 cache_re(PL_reg_call_cc->re);
3819                 PL_regcc = PL_reg_call_cc->cc;
3820                 PL_reg_call_cc = PL_reg_call_cc->prev;
3821                 if (regmatch(cur_call_cc->node)) {
3822                     PL_reg_call_cc = cur_call_cc;
3823                     regcpblow(cp);
3824                     sayYES;
3825                 }
3826                 REGCP_UNWIND(lastcp);
3827                 regcppop();
3828                 PL_reg_call_cc = cur_call_cc;
3829                 PL_regcc = cctmp;
3830                 PL_reg_re = re;
3831                 cache_re(re);
3832
3833                 DEBUG_r(
3834                     PerlIO_printf(Perl_debug_log,
3835                                   "%*s  continuation failed...\n",
3836                                   REPORT_CODE_OFF+PL_regindent*2, "")
3837                     );
3838                 sayNO_SILENT;
3839             }
3840             if (locinput < PL_regtill) {
3841                 DEBUG_r(PerlIO_printf(Perl_debug_log,
3842                                       "%sMatch possible, but length=%ld is smaller than requested=%ld, failing!%s\n",
3843                                       PL_colors[4],
3844                                       (long)(locinput - PL_reg_starttry),
3845                                       (long)(PL_regtill - PL_reg_starttry),
3846                                       PL_colors[5]));
3847                 sayNO_FINAL;            /* Cannot match: too short. */
3848             }
3849             PL_reginput = locinput;     /* put where regtry can find it */
3850             sayYES_FINAL;               /* Success! */
3851         case SUCCEED:
3852             PL_reginput = locinput;     /* put where regtry can find it */
3853             sayYES_LOUD;                /* Success! */
3854         case SUSPEND:
3855             n = 1;
3856             PL_reginput = locinput;
3857             goto do_ifmatch;    
3858         case UNLESSM:
3859             n = 0;
3860             if (scan->flags) {
3861                 s = HOPBACKc(locinput, scan->flags);
3862                 if (!s)
3863                     goto say_yes;
3864                 PL_reginput = s;
3865             }
3866             else
3867                 PL_reginput = locinput;
3868             goto do_ifmatch;
3869         case IFMATCH:
3870             n = 1;
3871             if (scan->flags) {
3872                 s = HOPBACKc(locinput, scan->flags);
3873                 if (!s)
3874                     goto say_no;
3875                 PL_reginput = s;
3876             }
3877             else
3878                 PL_reginput = locinput;
3879
3880           do_ifmatch:
3881             inner = NEXTOPER(NEXTOPER(scan));
3882             if (regmatch(inner) != n) {
3883               say_no:
3884                 if (logical) {
3885                     logical = 0;
3886                     sw = 0;
3887                     goto do_longjump;
3888                 }
3889                 else
3890                     sayNO;
3891             }
3892           say_yes:
3893             if (logical) {
3894                 logical = 0;
3895                 sw = 1;
3896             }
3897             if (OP(scan) == SUSPEND) {
3898                 locinput = PL_reginput;
3899                 nextchr = UCHARAT(locinput);
3900             }
3901             /* FALL THROUGH. */
3902         case LONGJMP:
3903           do_longjump:
3904             next = scan + ARG(scan);
3905             if (next == scan)
3906                 next = NULL;
3907             break;
3908         default:
3909             PerlIO_printf(Perl_error_log, "%"UVxf" %d\n",
3910                           PTR2UV(scan), OP(scan));
3911             Perl_croak(aTHX_ "regexp memory corruption");
3912         }
3913       reenter:
3914         scan = next;
3915     }
3916
3917     /*
3918     * We get here only if there's trouble -- normally "case END" is
3919     * the terminating point.
3920     */
3921     Perl_croak(aTHX_ "corrupted regexp pointers");
3922     /*NOTREACHED*/
3923     sayNO;
3924
3925 yes_loud:
3926     DEBUG_r(
3927         PerlIO_printf(Perl_debug_log,
3928                       "%*s  %scould match...%s\n",
3929                       REPORT_CODE_OFF+PL_regindent*2, "", PL_colors[4],PL_colors[5])
3930         );
3931     goto yes;
3932 yes_final:
3933     DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch successful!%s\n",
3934                           PL_colors[4],PL_colors[5]));
3935 yes:
3936 #ifdef DEBUGGING
3937     PL_regindent--;
3938 #endif
3939
3940 #if 0                                   /* Breaks $^R */
3941     if (unwind)
3942         regcpblow(firstcp);
3943 #endif
3944     return 1;
3945
3946 no:
3947     DEBUG_r(
3948         PerlIO_printf(Perl_debug_log,
3949                       "%*s  %sfailed...%s\n",
3950                       REPORT_CODE_OFF+PL_regindent*2, "",PL_colors[4],PL_colors[5])
3951         );
3952     goto do_no;
3953 no_final:
3954 do_no:
3955     if (unwind) {
3956         re_unwind_t *uw = SSPTRt(unwind,re_unwind_t);
3957
3958         switch (uw->type) {
3959         case RE_UNWIND_BRANCH:
3960         case RE_UNWIND_BRANCHJ:
3961         {
3962             re_unwind_branch_t *uwb = &(uw->branch);
3963             I32 lastparen = uwb->lastparen;
3964         
3965             REGCP_UNWIND(uwb->lastcp);
3966             for (n = *PL_reglastparen; n > lastparen; n--)
3967                 PL_regendp[n] = -1;
3968             *PL_reglastparen = n;
3969             scan = next = uwb->next;
3970             if ( !scan ||
3971                  OP(scan) != (uwb->type == RE_UNWIND_BRANCH
3972                               ? BRANCH : BRANCHJ) ) {           /* Failure */
3973                 unwind = uwb->prev;
3974 #ifdef DEBUGGING
3975                 PL_regindent--;
3976 #endif
3977                 goto do_no;
3978             }
3979             /* Have more choice yet.  Reuse the same uwb.  */
3980             /*SUPPRESS 560*/
3981             if ((n = (uwb->type == RE_UNWIND_BRANCH
3982                       ? NEXT_OFF(next) : ARG(next))))
3983                 next += n;
3984             else
3985                 next = NULL;    /* XXXX Needn't unwinding in this case... */
3986             uwb->next = next;
3987             next = NEXTOPER(scan);
3988             if (uwb->type == RE_UNWIND_BRANCHJ)
3989                 next = NEXTOPER(next);
3990             locinput = uwb->locinput;
3991             nextchr = uwb->nextchr;
3992 #ifdef DEBUGGING
3993             PL_regindent = uwb->regindent;
3994 #endif
3995
3996             goto reenter;
3997         }
3998         /* NOT REACHED */
3999         default:
4000             Perl_croak(aTHX_ "regexp unwind memory corruption");
4001         }
4002         /* NOT REACHED */
4003     }
4004 #ifdef DEBUGGING
4005     PL_regindent--;
4006 #endif
4007     return 0;
4008 }
4009
4010 /*
4011  - regrepeat - repeatedly match something simple, report how many
4012  */
4013 /*
4014  * [This routine now assumes that it will only match on things of length 1.
4015  * That was true before, but now we assume scan - reginput is the count,
4016  * rather than incrementing count on every character.  [Er, except utf8.]]
4017  */
4018 STATIC I32
4019 S_regrepeat(pTHX_ regnode *p, I32 max)
4020 {
4021     register char *scan;
4022     register I32 c;
4023     register char *loceol = PL_regeol;
4024     register I32 hardcount = 0;
4025     register bool do_utf8 = PL_reg_match_utf8;
4026
4027     scan = PL_reginput;
4028     if (max == REG_INFTY)
4029         max = I32_MAX;
4030     else if (max < loceol - scan)
4031       loceol = scan + max;
4032     switch (OP(p)) {
4033     case REG_ANY:
4034         if (do_utf8) {
4035             loceol = PL_regeol;
4036             while (scan < loceol && hardcount < max && *scan != '\n') {
4037                 scan += UTF8SKIP(scan);
4038                 hardcount++;
4039             }
4040         } else {
4041             while (scan < loceol && *scan != '\n')
4042                 scan++;
4043         }
4044         break;
4045     case SANY:
4046         if (do_utf8) {
4047             loceol = PL_regeol;
4048             while (scan < loceol && hardcount < max) {
4049                 scan += UTF8SKIP(scan);
4050                 hardcount++;
4051             }
4052         }
4053         else
4054             scan = loceol;
4055         break;
4056     case CANY:
4057         scan = loceol;
4058         break;
4059     case EXACT:         /* length of string is 1 */
4060         c = (U8)*STRING(p);
4061         while (scan < loceol && UCHARAT(scan) == c)
4062             scan++;
4063         break;
4064     case EXACTF:        /* length of string is 1 */
4065         c = (U8)*STRING(p);
4066         while (scan < loceol &&
4067                (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold[c]))
4068             scan++;
4069         break;
4070     case EXACTFL:       /* length of string is 1 */
4071         PL_reg_flags |= RF_tainted;
4072         c = (U8)*STRING(p);
4073         while (scan < loceol &&
4074                (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold_locale[c]))
4075             scan++;
4076         break;
4077     case ANYOF:
4078         if (do_utf8) {
4079             loceol = PL_regeol;
4080             while (hardcount < max && scan < loceol) {
4081                  bool cont = FALSE;
4082                  if (ANYOF_FLAGS(p) & ANYOF_UNICODE) {
4083                       if (reginclass(p, (U8*)scan, 0, do_utf8))
4084                            cont = TRUE;
4085                  }
4086                  else {
4087                       U8 c = (U8)scan[0];
4088
4089                       if (UTF8_IS_INVARIANT(c)) {
4090                            if (ANYOF_BITMAP_TEST(p, c))
4091                                 cont = TRUE;
4092                       }
4093                       else {
4094                            if (reginclass(p, (U8*)scan, 0, do_utf8))
4095                                 cont = TRUE;
4096                       }
4097                 }
4098                 if (!cont)
4099                      break;
4100                 scan += UTF8SKIP(scan);
4101                 hardcount++;
4102             }
4103         } else {
4104             while (scan < loceol && REGINCLASS(p, (U8*)scan))
4105                 scan++;
4106         }
4107         break;
4108     case ALNUM:
4109         if (do_utf8) {
4110             loceol = PL_regeol;
4111             LOAD_UTF8_CHARCLASS(alnum,"a");
4112             while (hardcount < max && scan < loceol &&
4113                    swash_fetch(PL_utf8_alnum, (U8*)scan, do_utf8)) {
4114                 scan += UTF8SKIP(scan);
4115                 hardcount++;
4116             }
4117         } else {
4118             while (scan < loceol && isALNUM(*scan))
4119                 scan++;
4120         }
4121         break;
4122     case ALNUML:
4123         PL_reg_flags |= RF_tainted;
4124         if (do_utf8) {
4125             loceol = PL_regeol;
4126             while (hardcount < max && scan < loceol &&
4127                    isALNUM_LC_utf8((U8*)scan)) {
4128                 scan += UTF8SKIP(scan);
4129                 hardcount++;
4130             }
4131         } else {
4132             while (scan < loceol && isALNUM_LC(*scan))
4133                 scan++;
4134         }
4135         break;
4136     case NALNUM:
4137         if (do_utf8) {
4138             loceol = PL_regeol;
4139             LOAD_UTF8_CHARCLASS(alnum,"a");
4140             while (hardcount < max && scan < loceol &&
4141                    !swash_fetch(PL_utf8_alnum, (U8*)scan, do_utf8)) {
4142                 scan += UTF8SKIP(scan);
4143                 hardcount++;
4144             }
4145         } else {
4146             while (scan < loceol && !isALNUM(*scan))
4147                 scan++;
4148         }
4149         break;
4150     case NALNUML:
4151         PL_reg_flags |= RF_tainted;
4152         if (do_utf8) {
4153             loceol = PL_regeol;
4154             while (hardcount < max && scan < loceol &&
4155                    !isALNUM_LC_utf8((U8*)scan)) {
4156                 scan += UTF8SKIP(scan);
4157                 hardcount++;
4158             }
4159         } else {
4160             while (scan < loceol && !isALNUM_LC(*scan))
4161                 scan++;
4162         }
4163         break;
4164     case SPACE:
4165         if (do_utf8) {
4166             loceol = PL_regeol;
4167             LOAD_UTF8_CHARCLASS(space," ");
4168             while (hardcount < max && scan < loceol &&
4169                    (*scan == ' ' ||
4170                     swash_fetch(PL_utf8_space,(U8*)scan, do_utf8))) {
4171                 scan += UTF8SKIP(scan);
4172                 hardcount++;
4173             }
4174         } else {
4175             while (scan < loceol && isSPACE(*scan))
4176                 scan++;
4177         }
4178         break;
4179     case SPACEL:
4180         PL_reg_flags |= RF_tainted;
4181         if (do_utf8) {
4182             loceol = PL_regeol;
4183             while (hardcount < max && scan < loceol &&
4184                    (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
4185                 scan += UTF8SKIP(scan);
4186                 hardcount++;
4187             }
4188         } else {
4189             while (scan < loceol && isSPACE_LC(*scan))
4190                 scan++;
4191         }
4192         break;
4193     case NSPACE:
4194         if (do_utf8) {
4195             loceol = PL_regeol;
4196             LOAD_UTF8_CHARCLASS(space," ");
4197             while (hardcount < max && scan < loceol &&
4198                    !(*scan == ' ' ||
4199                      swash_fetch(PL_utf8_space,(U8*)scan, do_utf8))) {
4200                 scan += UTF8SKIP(scan);
4201                 hardcount++;
4202             }
4203         } else {
4204             while (scan < loceol && !isSPACE(*scan))
4205                 scan++;
4206             break;
4207         }
4208     case NSPACEL:
4209         PL_reg_flags |= RF_tainted;
4210         if (do_utf8) {
4211             loceol = PL_regeol;
4212             while (hardcount < max && scan < loceol &&
4213                    !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
4214                 scan += UTF8SKIP(scan);
4215                 hardcount++;
4216             }
4217         } else {
4218             while (scan < loceol && !isSPACE_LC(*scan))
4219                 scan++;
4220         }
4221         break;
4222     case DIGIT:
4223         if (do_utf8) {
4224             loceol = PL_regeol;
4225             LOAD_UTF8_CHARCLASS(digit,"0");
4226             while (hardcount < max && scan < loceol &&
4227                    swash_fetch(PL_utf8_digit, (U8*)scan, do_utf8)) {
4228                 scan += UTF8SKIP(scan);
4229                 hardcount++;
4230             }
4231         } else {
4232             while (scan < loceol && isDIGIT(*scan))
4233                 scan++;
4234         }
4235         break;
4236     case NDIGIT:
4237         if (do_utf8) {
4238             loceol = PL_regeol;
4239             LOAD_UTF8_CHARCLASS(digit,"0");
4240             while (hardcount < max && scan < loceol &&
4241                    !swash_fetch(PL_utf8_digit, (U8*)scan, do_utf8)) {
4242                 scan += UTF8SKIP(scan);
4243                 hardcount++;
4244             }
4245         } else {
4246             while (scan < loceol && !isDIGIT(*scan))
4247                 scan++;
4248         }
4249         break;
4250     default:            /* Called on something of 0 width. */
4251         break;          /* So match right here or not at all. */
4252     }
4253
4254     if (hardcount)
4255         c = hardcount;
4256     else
4257         c = scan - PL_reginput;
4258     PL_reginput = scan;
4259
4260     DEBUG_r(
4261         {
4262                 SV *prop = sv_newmortal();
4263
4264                 regprop(prop, p);
4265                 PerlIO_printf(Perl_debug_log,
4266                               "%*s  %s can match %"IVdf" times out of %"IVdf"...\n",
4267                               REPORT_CODE_OFF+1, "", SvPVX(prop),(IV)c,(IV)max);
4268         });
4269
4270     return(c);
4271 }
4272
4273 /*
4274  - regrepeat_hard - repeatedly match something, report total lenth and length
4275  *
4276  * The repeater is supposed to have constant length.
4277  */
4278
4279 STATIC I32
4280 S_regrepeat_hard(pTHX_ regnode *p, I32 max, I32 *lp)
4281 {
4282     register char *scan = Nullch;
4283     register char *start;
4284     register char *loceol = PL_regeol;
4285     I32 l = 0;
4286     I32 count = 0, res = 1;
4287
4288     if (!max)
4289         return 0;
4290
4291     start = PL_reginput;
4292     if (PL_reg_match_utf8) {
4293         while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
4294             if (!count++) {
4295                 l = 0;
4296                 while (start < PL_reginput) {
4297                     l++;
4298                     start += UTF8SKIP(start);
4299                 }
4300                 *lp = l;
4301                 if (l == 0)
4302                     return max;
4303             }
4304             if (count == max)
4305                 return count;
4306         }
4307     }
4308     else {
4309         while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
4310             if (!count++) {
4311                 *lp = l = PL_reginput - start;
4312                 if (max != REG_INFTY && l*max < loceol - scan)
4313                     loceol = scan + l*max;
4314                 if (l == 0)
4315                     return max;
4316             }
4317         }
4318     }
4319     if (!res)
4320         PL_reginput = scan;
4321
4322     return count;
4323 }
4324
4325 /*
4326 - regclass_swash - prepare the utf8 swash
4327 */
4328
4329 SV *
4330 Perl_regclass_swash(pTHX_ register regnode* node, bool doinit, SV** listsvp, SV **altsvp)
4331 {
4332     SV *sw  = NULL;
4333     SV *si  = NULL;
4334     SV *alt = NULL;
4335
4336     if (PL_regdata && PL_regdata->count) {
4337         U32 n = ARG(node);
4338
4339         if (PL_regdata->what[n] == 's') {
4340             SV *rv = (SV*)PL_regdata->data[n];
4341             AV *av = (AV*)SvRV((SV*)rv);
4342             SV **ary = AvARRAY(av);
4343             SV **a, **b;
4344         
4345             /* See the end of regcomp.c:S_reglass() for
4346              * documentation of these array elements. */
4347
4348             si = *ary;
4349             a  = SvTYPE(ary[1]) == SVt_RV   ? &ary[1] : 0;
4350             b  = SvTYPE(ary[2]) == SVt_PVAV ? &ary[2] : 0;
4351
4352             if (a)
4353                 sw = *a;
4354             else if (si && doinit) {
4355                 sw = swash_init("utf8", "", si, 1, 0);
4356                 (void)av_store(av, 1, sw);
4357             }
4358             if (b)
4359                 alt = *b;
4360         }
4361     }
4362         
4363     if (listsvp)
4364         *listsvp = si;
4365     if (altsvp)
4366         *altsvp  = alt;
4367
4368     return sw;
4369 }
4370
4371 /*
4372  - reginclass - determine if a character falls into a character class
4373  
4374   The n is the ANYOF regnode, the p is the target string, lenp
4375   is pointer to the maximum length of how far to go in the p
4376   (if the lenp is zero, UTF8SKIP(p) is used),
4377   do_utf8 tells whether the target string is in UTF-8.
4378
4379  */
4380
4381 STATIC bool
4382 S_reginclass(pTHX_ register regnode *n, register U8* p, STRLEN* lenp, register bool do_utf8)
4383 {
4384     char flags = ANYOF_FLAGS(n);
4385     bool match = FALSE;
4386     UV c = *p;
4387     STRLEN len = 0;
4388     STRLEN plen;
4389
4390     if (do_utf8 && !UTF8_IS_INVARIANT(c))
4391          c = utf8n_to_uvchr(p, UTF8_MAXLEN, &len,
4392                             ckWARN(WARN_UTF8) ? 0 : UTF8_ALLOW_ANY);
4393
4394     plen = lenp ? *lenp : UNISKIP(NATIVE_TO_UNI(c));
4395     if (do_utf8 || (flags & ANYOF_UNICODE)) {
4396         if (lenp)
4397             *lenp = 0;
4398         if (do_utf8 && !ANYOF_RUNTIME(n)) {
4399             if (len != (STRLEN)-1 && c < 256 && ANYOF_BITMAP_TEST(n, c))
4400                 match = TRUE;
4401         }
4402         if (!match && do_utf8 && (flags & ANYOF_UNICODE_ALL) && c >= 256)
4403             match = TRUE;
4404         if (!match) {
4405             AV *av;
4406             SV *sw = regclass_swash(n, TRUE, 0, (SV**)&av);
4407         
4408             if (sw) {
4409                 if (swash_fetch(sw, p, do_utf8))
4410                     match = TRUE;
4411                 else if (flags & ANYOF_FOLD) {
4412                     if (!match && lenp && av) {
4413                         I32 i;
4414                       
4415                         for (i = 0; i <= av_len(av); i++) {
4416                             SV* sv = *av_fetch(av, i, FALSE);
4417                             STRLEN len;
4418                             char *s = SvPV(sv, len);
4419                         
4420                             if (len <= plen && memEQ(s, (char*)p, len)) {
4421                                 *lenp = len;
4422                                 match = TRUE;
4423                                 break;
4424                             }
4425                         }
4426                     }
4427                     if (!match) {
4428                         U8 tmpbuf[UTF8_MAXLEN_FOLD+1];
4429                         STRLEN tmplen;
4430
4431                         to_utf8_fold(p, tmpbuf, &tmplen);
4432                         if (swash_fetch(sw, tmpbuf, do_utf8))
4433                             match = TRUE;
4434                     }
4435                 }
4436             }
4437         }
4438         if (match && lenp && *lenp == 0)
4439             *lenp = UNISKIP(NATIVE_TO_UNI(c));
4440     }
4441     if (!match && c < 256) {
4442         if (ANYOF_BITMAP_TEST(n, c))
4443             match = TRUE;
4444         else if (flags & ANYOF_FOLD) {
4445             U8 f;
4446
4447             if (flags & ANYOF_LOCALE) {
4448                 PL_reg_flags |= RF_tainted;
4449                 f = PL_fold_locale[c];
4450             }
4451             else
4452                 f = PL_fold[c];
4453             if (f != c && ANYOF_BITMAP_TEST(n, f))
4454                 match = TRUE;
4455         }
4456         
4457         if (!match && (flags & ANYOF_CLASS)) {
4458             PL_reg_flags |= RF_tainted;
4459             if (
4460                 (ANYOF_CLASS_TEST(n, ANYOF_ALNUM)   &&  isALNUM_LC(c))  ||
4461                 (ANYOF_CLASS_TEST(n, ANYOF_NALNUM)  && !isALNUM_LC(c))  ||
4462                 (ANYOF_CLASS_TEST(n, ANYOF_SPACE)   &&  isSPACE_LC(c))  ||
4463                 (ANYOF_CLASS_TEST(n, ANYOF_NSPACE)  && !isSPACE_LC(c))  ||
4464                 (ANYOF_CLASS_TEST(n, ANYOF_DIGIT)   &&  isDIGIT_LC(c))  ||
4465                 (ANYOF_CLASS_TEST(n, ANYOF_NDIGIT)  && !isDIGIT_LC(c))  ||
4466                 (ANYOF_CLASS_TEST(n, ANYOF_ALNUMC)  &&  isALNUMC_LC(c)) ||
4467                 (ANYOF_CLASS_TEST(n, ANYOF_NALNUMC) && !isALNUMC_LC(c)) ||
4468                 (ANYOF_CLASS_TEST(n, ANYOF_ALPHA)   &&  isALPHA_LC(c))  ||
4469                 (ANYOF_CLASS_TEST(n, ANYOF_NALPHA)  && !isALPHA_LC(c))  ||
4470                 (ANYOF_CLASS_TEST(n, ANYOF_ASCII)   &&  isASCII(c))     ||
4471                 (ANYOF_CLASS_TEST(n, ANYOF_NASCII)  && !isASCII(c))     ||
4472                 (ANYOF_CLASS_TEST(n, ANYOF_CNTRL)   &&  isCNTRL_LC(c))  ||
4473                 (ANYOF_CLASS_TEST(n, ANYOF_NCNTRL)  && !isCNTRL_LC(c))  ||
4474                 (ANYOF_CLASS_TEST(n, ANYOF_GRAPH)   &&  isGRAPH_LC(c))  ||
4475                 (ANYOF_CLASS_TEST(n, ANYOF_NGRAPH)  && !isGRAPH_LC(c))  ||
4476                 (ANYOF_CLASS_TEST(n, ANYOF_LOWER)   &&  isLOWER_LC(c))  ||
4477                 (ANYOF_CLASS_TEST(n, ANYOF_NLOWER)  && !isLOWER_LC(c))  ||
4478                 (ANYOF_CLASS_TEST(n, ANYOF_PRINT)   &&  isPRINT_LC(c))  ||
4479                 (ANYOF_CLASS_TEST(n, ANYOF_NPRINT)  && !isPRINT_LC(c))  ||
4480                 (ANYOF_CLASS_TEST(n, ANYOF_PUNCT)   &&  isPUNCT_LC(c))  ||
4481                 (ANYOF_CLASS_TEST(n, ANYOF_NPUNCT)  && !isPUNCT_LC(c))  ||
4482                 (ANYOF_CLASS_TEST(n, ANYOF_UPPER)   &&  isUPPER_LC(c))  ||
4483                 (ANYOF_CLASS_TEST(n, ANYOF_NUPPER)  && !isUPPER_LC(c))  ||
4484                 (ANYOF_CLASS_TEST(n, ANYOF_XDIGIT)  &&  isXDIGIT(c))    ||
4485                 (ANYOF_CLASS_TEST(n, ANYOF_NXDIGIT) && !isXDIGIT(c))    ||
4486                 (ANYOF_CLASS_TEST(n, ANYOF_PSXSPC)  &&  isPSXSPC(c))    ||
4487                 (ANYOF_CLASS_TEST(n, ANYOF_NPSXSPC) && !isPSXSPC(c))    ||
4488                 (ANYOF_CLASS_TEST(n, ANYOF_BLANK)   &&  isBLANK(c))     ||
4489                 (ANYOF_CLASS_TEST(n, ANYOF_NBLANK)  && !isBLANK(c))
4490                 ) /* How's that for a conditional? */
4491             {
4492                 match = TRUE;
4493             }
4494         }
4495     }
4496
4497     return (flags & ANYOF_INVERT) ? !match : match;
4498 }
4499
4500 STATIC U8 *
4501 S_reghop(pTHX_ U8 *s, I32 off)
4502 {
4503     return S_reghop3(aTHX_ s, off, (U8*)(off >= 0 ? PL_regeol : PL_bostr));
4504 }
4505
4506 STATIC U8 *
4507 S_reghop3(pTHX_ U8 *s, I32 off, U8* lim)
4508 {
4509     if (off >= 0) {
4510         while (off-- && s < lim) {
4511             /* XXX could check well-formedness here */
4512             s += UTF8SKIP(s);
4513         }
4514     }
4515     else {
4516         while (off++) {
4517             if (s > lim) {
4518                 s--;
4519                 if (UTF8_IS_CONTINUED(*s)) {
4520                     while (s > (U8*)lim && UTF8_IS_CONTINUATION(*s))
4521                         s--;
4522                 }
4523                 /* XXX could check well-formedness here */
4524             }
4525         }
4526     }
4527     return s;
4528 }
4529
4530 STATIC U8 *
4531 S_reghopmaybe(pTHX_ U8 *s, I32 off)
4532 {
4533     return S_reghopmaybe3(aTHX_ s, off, (U8*)(off >= 0 ? PL_regeol : PL_bostr));
4534 }
4535
4536 STATIC U8 *
4537 S_reghopmaybe3(pTHX_ U8* s, I32 off, U8* lim)
4538 {
4539     if (off >= 0) {
4540         while (off-- && s < lim) {
4541             /* XXX could check well-formedness here */
4542             s += UTF8SKIP(s);
4543         }
4544         if (off >= 0)
4545             return 0;
4546     }
4547     else {
4548         while (off++) {
4549             if (s > lim) {
4550                 s--;
4551                 if (UTF8_IS_CONTINUED(*s)) {
4552                     while (s > (U8*)lim && UTF8_IS_CONTINUATION(*s))
4553                         s--;
4554                 }
4555                 /* XXX could check well-formedness here */
4556             }
4557             else
4558                 break;
4559         }
4560         if (off <= 0)
4561             return 0;
4562     }
4563     return s;
4564 }
4565
4566 static void
4567 restore_pos(pTHX_ void *arg)
4568 {
4569     if (PL_reg_eval_set) {
4570         if (PL_reg_oldsaved) {
4571             PL_reg_re->subbeg = PL_reg_oldsaved;
4572             PL_reg_re->sublen = PL_reg_oldsavedlen;
4573             RX_MATCH_COPIED_on(PL_reg_re);
4574         }
4575         PL_reg_magic->mg_len = PL_reg_oldpos;
4576         PL_reg_eval_set = 0;
4577         PL_curpm = PL_reg_oldcurpm;
4578     }   
4579 }
4580
4581 STATIC void
4582 S_to_utf8_substr(pTHX_ register regexp *prog)
4583 {
4584     SV* sv;
4585     if (prog->float_substr && !prog->float_utf8) {
4586         prog->float_utf8 = sv = NEWSV(117, 0);
4587         SvSetSV(sv, prog->float_substr);
4588         sv_utf8_upgrade(sv);
4589         if (SvTAIL(prog->float_substr))
4590             SvTAIL_on(sv);
4591         if (prog->float_substr == prog->check_substr)
4592             prog->check_utf8 = sv;
4593     }
4594     if (prog->anchored_substr && !prog->anchored_utf8) {
4595         prog->anchored_utf8 = sv = NEWSV(118, 0);
4596         SvSetSV(sv, prog->anchored_substr);
4597         sv_utf8_upgrade(sv);
4598         if (SvTAIL(prog->anchored_substr))
4599             SvTAIL_on(sv);
4600         if (prog->anchored_substr == prog->check_substr)
4601             prog->check_utf8 = sv;
4602     }
4603 }
4604
4605 STATIC void
4606 S_to_byte_substr(pTHX_ register regexp *prog)
4607 {
4608     SV* sv;
4609     if (prog->float_utf8 && !prog->float_substr) {
4610         prog->float_substr = sv = NEWSV(117, 0);
4611         SvSetSV(sv, prog->float_utf8);
4612         if (sv_utf8_downgrade(sv, TRUE)) {
4613             if (SvTAIL(prog->float_utf8))
4614                 SvTAIL_on(sv);
4615         } else {
4616             SvREFCNT_dec(sv);
4617             prog->float_substr = sv = &PL_sv_undef;
4618         }
4619         if (prog->float_utf8 == prog->check_utf8)
4620             prog->check_substr = sv;
4621     }
4622     if (prog->anchored_utf8 && !prog->anchored_substr) {
4623         prog->anchored_substr = sv = NEWSV(118, 0);
4624         SvSetSV(sv, prog->anchored_utf8);
4625         if (sv_utf8_downgrade(sv, TRUE)) {
4626             if (SvTAIL(prog->anchored_utf8))
4627                 SvTAIL_on(sv);
4628         } else {
4629             SvREFCNT_dec(sv);
4630             prog->anchored_substr = sv = &PL_sv_undef;
4631         }
4632         if (prog->anchored_utf8 == prog->check_utf8)
4633             prog->check_substr = sv;
4634     }
4635 }