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