This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
regen/regcharclass.pl: Improve the generated code
[perl5.git] / regexec.c
1 /*    regexec.c
2  */
3
4 /*
5  *      One Ring to rule them all, One Ring to find them
6  &
7  *     [p.v of _The Lord of the Rings_, opening poem]
8  *     [p.50 of _The Lord of the Rings_, I/iii: "The Shadow of the Past"]
9  *     [p.254 of _The Lord of the Rings_, II/ii: "The Council of Elrond"]
10  */
11
12 /* This file contains functions for executing a regular expression.  See
13  * also regcomp.c which funnily enough, contains functions for compiling
14  * a regular expression.
15  *
16  * This file is also copied at build time to ext/re/re_exec.c, where
17  * it's built with -DPERL_EXT_RE_BUILD -DPERL_EXT_RE_DEBUG -DPERL_EXT.
18  * This causes the main functions to be compiled under new names and with
19  * debugging support added, which makes "use re 'debug'" work.
20  */
21
22 /* NOTE: this is derived from Henry Spencer's regexp code, and should not
23  * confused with the original package (see point 3 below).  Thanks, Henry!
24  */
25
26 /* Additional note: this code is very heavily munged from Henry's version
27  * in places.  In some spots I've traded clarity for efficiency, so don't
28  * blame Henry for some of the lack of readability.
29  */
30
31 /* The names of the functions have been changed from regcomp and
32  * regexec to  pregcomp and pregexec in order to avoid conflicts
33  * with the POSIX routines of the same names.
34 */
35
36 #ifdef PERL_EXT_RE_BUILD
37 #include "re_top.h"
38 #endif
39
40 /*
41  * pregcomp and pregexec -- regsub and regerror are not used in perl
42  *
43  *      Copyright (c) 1986 by University of Toronto.
44  *      Written by Henry Spencer.  Not derived from licensed software.
45  *
46  *      Permission is granted to anyone to use this software for any
47  *      purpose on any computer system, and to redistribute it freely,
48  *      subject to the following restrictions:
49  *
50  *      1. The author is not responsible for the consequences of use of
51  *              this software, no matter how awful, even if they arise
52  *              from defects in it.
53  *
54  *      2. The origin of this software must not be misrepresented, either
55  *              by explicit claim or by omission.
56  *
57  *      3. Altered versions must be plainly marked as such, and must not
58  *              be misrepresented as being the original software.
59  *
60  ****    Alterations to Henry's code are...
61  ****
62  ****    Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
63  ****    2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
64  ****    by Larry Wall and others
65  ****
66  ****    You may distribute under the terms of either the GNU General Public
67  ****    License or the Artistic License, as specified in the README file.
68  *
69  * Beware that some of this code is subtly aware of the way operator
70  * precedence is structured in regular expressions.  Serious changes in
71  * regular-expression syntax might require a total rethink.
72  */
73 #include "EXTERN.h"
74 #define PERL_IN_REGEXEC_C
75 #include "perl.h"
76
77 #ifdef PERL_IN_XSUB_RE
78 #  include "re_comp.h"
79 #else
80 #  include "regcomp.h"
81 #endif
82
83 #include "inline_invlist.c"
84 #include "unicode_constants.h"
85
86 #ifdef DEBUGGING
87 /* At least one required character in the target string is expressible only in
88  * UTF-8. */
89 static const char* const non_utf8_target_but_utf8_required
90                 = "Can't match, because target string needs to be in UTF-8\n";
91 #endif
92
93 #define NON_UTF8_TARGET_BUT_UTF8_REQUIRED(target) STMT_START { \
94     DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%s", non_utf8_target_but_utf8_required));\
95     goto target; \
96 } STMT_END
97
98 #define HAS_NONLATIN1_FOLD_CLOSURE(i) _HAS_NONLATIN1_FOLD_CLOSURE_ONLY_FOR_USE_BY_REGCOMP_DOT_C_AND_REGEXEC_DOT_C(i)
99
100 #ifndef STATIC
101 #define STATIC  static
102 #endif
103
104 /* Valid only for non-utf8 strings: avoids the reginclass
105  * call if there are no complications: i.e., if everything matchable is
106  * straight forward in the bitmap */
107 #define REGINCLASS(prog,p,c)  (ANYOF_FLAGS(p) ? reginclass(prog,p,c,c+1,0)   \
108                                               : ANYOF_BITMAP_TEST(p,*(c)))
109
110 /*
111  * Forwards.
112  */
113
114 #define CHR_SVLEN(sv) (utf8_target ? sv_len_utf8(sv) : SvCUR(sv))
115 #define CHR_DIST(a,b) (reginfo->is_utf8_target ? utf8_distance(a,b) : a - b)
116
117 #define HOPc(pos,off) \
118         (char *)(reginfo->is_utf8_target \
119             ? reghop3((U8*)pos, off, \
120                     (U8*)(off >= 0 ? reginfo->strend : reginfo->strbeg)) \
121             : (U8*)(pos + off))
122
123 #define HOPBACKc(pos, off) \
124         (char*)(reginfo->is_utf8_target \
125             ? reghopmaybe3((U8*)pos, -off, (U8*)(reginfo->strbeg)) \
126             : (pos - off >= reginfo->strbeg)    \
127                 ? (U8*)pos - off                \
128                 : NULL)
129
130 #define HOP3(pos,off,lim) (reginfo->is_utf8_target  ? reghop3((U8*)(pos), off, (U8*)(lim)) : (U8*)(pos + off))
131 #define HOP3c(pos,off,lim) ((char*)HOP3(pos,off,lim))
132
133 /* lim must be +ve. Returns NULL on overshoot */
134 #define HOPMAYBE3(pos,off,lim) \
135         (reginfo->is_utf8_target                        \
136             ? reghopmaybe3((U8*)pos, off, (U8*)(lim))   \
137             : ((U8*)pos + off <= lim)                   \
138                 ? (U8*)pos + off                        \
139                 : NULL)
140
141 /* like HOP3, but limits the result to <= lim even for the non-utf8 case.
142  * off must be >=0; args should be vars rather than expressions */
143 #define HOP3lim(pos,off,lim) (reginfo->is_utf8_target \
144     ? reghop3((U8*)(pos), off, (U8*)(lim)) \
145     : (U8*)((pos + off) > lim ? lim : (pos + off)))
146
147 #define HOP4(pos,off,llim, rlim) (reginfo->is_utf8_target \
148     ? reghop4((U8*)(pos), off, (U8*)(llim), (U8*)(rlim)) \
149     : (U8*)(pos + off))
150 #define HOP4c(pos,off,llim, rlim) ((char*)HOP4(pos,off,llim, rlim))
151
152 #define NEXTCHR_EOS -10 /* nextchr has fallen off the end */
153 #define NEXTCHR_IS_EOS (nextchr < 0)
154
155 #define SET_nextchr \
156     nextchr = ((locinput < reginfo->strend) ? UCHARAT(locinput) : NEXTCHR_EOS)
157
158 #define SET_locinput(p) \
159     locinput = (p);  \
160     SET_nextchr
161
162
163 #define LOAD_UTF8_CHARCLASS(swash_ptr, property_name, invlist) STMT_START {   \
164         if (!swash_ptr) {                                                     \
165             U8 flags = _CORE_SWASH_INIT_ACCEPT_INVLIST;                       \
166             swash_ptr = _core_swash_init("utf8", property_name, &PL_sv_undef, \
167                                          1, 0, invlist, &flags);              \
168             assert(swash_ptr);                                                \
169         }                                                                     \
170     } STMT_END
171
172 /* If in debug mode, we test that a known character properly matches */
173 #ifdef DEBUGGING
174 #   define LOAD_UTF8_CHARCLASS_DEBUG_TEST(swash_ptr,                          \
175                                           property_name,                      \
176                                           invlist,                            \
177                                           utf8_char_in_property)              \
178         LOAD_UTF8_CHARCLASS(swash_ptr, property_name, invlist);               \
179         assert(swash_fetch(swash_ptr, (U8 *) utf8_char_in_property, TRUE));
180 #else
181 #   define LOAD_UTF8_CHARCLASS_DEBUG_TEST(swash_ptr,                          \
182                                           property_name,                      \
183                                           invlist,                            \
184                                           utf8_char_in_property)              \
185         LOAD_UTF8_CHARCLASS(swash_ptr, property_name, invlist)
186 #endif
187
188 #define LOAD_UTF8_CHARCLASS_ALNUM() LOAD_UTF8_CHARCLASS_DEBUG_TEST(           \
189                                         PL_utf8_swash_ptrs[_CC_WORDCHAR],     \
190                                         "",                                   \
191                                         PL_XPosix_ptrs[_CC_WORDCHAR],         \
192                                         LATIN_CAPITAL_LETTER_SHARP_S_UTF8);
193
194 #define LOAD_UTF8_CHARCLASS_GCB()  /* Grapheme cluster boundaries */          \
195     STMT_START {                                                              \
196         LOAD_UTF8_CHARCLASS_DEBUG_TEST(PL_utf8_X_regular_begin,               \
197                                        "_X_regular_begin",                    \
198                                        NULL,                                  \
199                                        LATIN_CAPITAL_LETTER_SHARP_S_UTF8);    \
200         LOAD_UTF8_CHARCLASS_DEBUG_TEST(PL_utf8_X_extend,                      \
201                                        "_X_extend",                           \
202                                        NULL,                                  \
203                                        COMBINING_GRAVE_ACCENT_UTF8);          \
204     } STMT_END
205
206 #define PLACEHOLDER     /* Something for the preprocessor to grab onto */
207 /* TODO: Combine JUMPABLE and HAS_TEXT to cache OP(rn) */
208
209 /* for use after a quantifier and before an EXACT-like node -- japhy */
210 /* it would be nice to rework regcomp.sym to generate this stuff. sigh
211  *
212  * NOTE that *nothing* that affects backtracking should be in here, specifically
213  * VERBS must NOT be included. JUMPABLE is used to determine  if we can ignore a
214  * node that is in between two EXACT like nodes when ascertaining what the required
215  * "follow" character is. This should probably be moved to regex compile time
216  * although it may be done at run time beause of the REF possibility - more
217  * investigation required. -- demerphq
218 */
219 #define JUMPABLE(rn) (                                                             \
220     OP(rn) == OPEN ||                                                              \
221     (OP(rn) == CLOSE && (!cur_eval || cur_eval->u.eval.close_paren != ARG(rn))) || \
222     OP(rn) == EVAL ||                                                              \
223     OP(rn) == SUSPEND || OP(rn) == IFMATCH ||                                      \
224     OP(rn) == PLUS || OP(rn) == MINMOD ||                                          \
225     OP(rn) == KEEPS ||                                                             \
226     (PL_regkind[OP(rn)] == CURLY && ARG1(rn) > 0)                                  \
227 )
228 #define IS_EXACT(rn) (PL_regkind[OP(rn)] == EXACT)
229
230 #define HAS_TEXT(rn) ( IS_EXACT(rn) || PL_regkind[OP(rn)] == REF )
231
232 #if 0 
233 /* Currently these are only used when PL_regkind[OP(rn)] == EXACT so
234    we don't need this definition. */
235 #define IS_TEXT(rn)   ( OP(rn)==EXACT   || OP(rn)==REF   || OP(rn)==NREF   )
236 #define IS_TEXTF(rn)  ( OP(rn)==EXACTFU || OP(rn)==EXACTFU_SS || OP(rn)==EXACTFA || OP(rn)==EXACTFA_NO_TRIE || OP(rn)==EXACTF || OP(rn)==REFF  || OP(rn)==NREFF )
237 #define IS_TEXTFL(rn) ( OP(rn)==EXACTFL || OP(rn)==REFFL || OP(rn)==NREFFL )
238
239 #else
240 /* ... so we use this as its faster. */
241 #define IS_TEXT(rn)   ( OP(rn)==EXACT   )
242 #define IS_TEXTFU(rn)  ( OP(rn)==EXACTFU || OP(rn)==EXACTFU_SS || OP(rn) == EXACTFA || OP(rn) == EXACTFA_NO_TRIE)
243 #define IS_TEXTF(rn)  ( OP(rn)==EXACTF  )
244 #define IS_TEXTFL(rn) ( OP(rn)==EXACTFL )
245
246 #endif
247
248 /*
249   Search for mandatory following text node; for lookahead, the text must
250   follow but for lookbehind (rn->flags != 0) we skip to the next step.
251 */
252 #define FIND_NEXT_IMPT(rn) STMT_START {                                   \
253     while (JUMPABLE(rn)) { \
254         const OPCODE type = OP(rn); \
255         if (type == SUSPEND || PL_regkind[type] == CURLY) \
256             rn = NEXTOPER(NEXTOPER(rn)); \
257         else if (type == PLUS) \
258             rn = NEXTOPER(rn); \
259         else if (type == IFMATCH) \
260             rn = (rn->flags == 0) ? NEXTOPER(NEXTOPER(rn)) : rn + ARG(rn); \
261         else rn += NEXT_OFF(rn); \
262     } \
263 } STMT_END 
264
265 /* These constants are for finding GCB=LV and GCB=LVT in the CLUMP regnode.
266  * These are for the pre-composed Hangul syllables, which are all in a
267  * contiguous block and arranged there in such a way so as to facilitate
268  * alorithmic determination of their characteristics.  As such, they don't need
269  * a swash, but can be determined by simple arithmetic.  Almost all are
270  * GCB=LVT, but every 28th one is a GCB=LV */
271 #define SBASE 0xAC00    /* Start of block */
272 #define SCount 11172    /* Length of block */
273 #define TCount 28
274
275 #define SLAB_FIRST(s) (&(s)->states[0])
276 #define SLAB_LAST(s)  (&(s)->states[PERL_REGMATCH_SLAB_SLOTS-1])
277
278 static void S_setup_eval_state(pTHX_ regmatch_info *const reginfo);
279 static void S_cleanup_regmatch_info_aux(pTHX_ void *arg);
280 static regmatch_state * S_push_slab(pTHX);
281
282 #define REGCP_PAREN_ELEMS 3
283 #define REGCP_OTHER_ELEMS 3
284 #define REGCP_FRAME_ELEMS 1
285 /* REGCP_FRAME_ELEMS are not part of the REGCP_OTHER_ELEMS and
286  * are needed for the regexp context stack bookkeeping. */
287
288 STATIC CHECKPOINT
289 S_regcppush(pTHX_ const regexp *rex, I32 parenfloor, U32 maxopenparen)
290 {
291     dVAR;
292     const int retval = PL_savestack_ix;
293     const int paren_elems_to_push =
294                 (maxopenparen - parenfloor) * REGCP_PAREN_ELEMS;
295     const UV total_elems = paren_elems_to_push + REGCP_OTHER_ELEMS;
296     const UV elems_shifted = total_elems << SAVE_TIGHT_SHIFT;
297     I32 p;
298     GET_RE_DEBUG_FLAGS_DECL;
299
300     PERL_ARGS_ASSERT_REGCPPUSH;
301
302     if (paren_elems_to_push < 0)
303         Perl_croak(aTHX_ "panic: paren_elems_to_push, %i < 0, maxopenparen: %i parenfloor: %i REGCP_PAREN_ELEMS: %i",
304                    paren_elems_to_push, maxopenparen, parenfloor, REGCP_PAREN_ELEMS);
305
306     if ((elems_shifted >> SAVE_TIGHT_SHIFT) != total_elems)
307         Perl_croak(aTHX_ "panic: paren_elems_to_push offset %"UVuf
308                    " out of range (%lu-%ld)",
309                    total_elems,
310                    (unsigned long)maxopenparen,
311                    (long)parenfloor);
312
313     SSGROW(total_elems + REGCP_FRAME_ELEMS);
314     
315     DEBUG_BUFFERS_r(
316         if ((int)maxopenparen > (int)parenfloor)
317             PerlIO_printf(Perl_debug_log,
318                 "rex=0x%"UVxf" offs=0x%"UVxf": saving capture indices:\n",
319                 PTR2UV(rex),
320                 PTR2UV(rex->offs)
321             );
322     );
323     for (p = parenfloor+1; p <= (I32)maxopenparen;  p++) {
324 /* REGCP_PARENS_ELEMS are pushed per pairs of parentheses. */
325         SSPUSHIV(rex->offs[p].end);
326         SSPUSHIV(rex->offs[p].start);
327         SSPUSHINT(rex->offs[p].start_tmp);
328         DEBUG_BUFFERS_r(PerlIO_printf(Perl_debug_log,
329             "    \\%"UVuf": %"IVdf"(%"IVdf")..%"IVdf"\n",
330             (UV)p,
331             (IV)rex->offs[p].start,
332             (IV)rex->offs[p].start_tmp,
333             (IV)rex->offs[p].end
334         ));
335     }
336 /* REGCP_OTHER_ELEMS are pushed in any case, parentheses or no. */
337     SSPUSHINT(maxopenparen);
338     SSPUSHINT(rex->lastparen);
339     SSPUSHINT(rex->lastcloseparen);
340     SSPUSHUV(SAVEt_REGCONTEXT | elems_shifted); /* Magic cookie. */
341
342     return retval;
343 }
344
345 /* These are needed since we do not localize EVAL nodes: */
346 #define REGCP_SET(cp)                                           \
347     DEBUG_STATE_r(                                              \
348             PerlIO_printf(Perl_debug_log,                       \
349                 "  Setting an EVAL scope, savestack=%"IVdf"\n", \
350                 (IV)PL_savestack_ix));                          \
351     cp = PL_savestack_ix
352
353 #define REGCP_UNWIND(cp)                                        \
354     DEBUG_STATE_r(                                              \
355         if (cp != PL_savestack_ix)                              \
356             PerlIO_printf(Perl_debug_log,                       \
357                 "  Clearing an EVAL scope, savestack=%"IVdf"..%"IVdf"\n", \
358                 (IV)(cp), (IV)PL_savestack_ix));                \
359     regcpblow(cp)
360
361 #define UNWIND_PAREN(lp, lcp)               \
362     for (n = rex->lastparen; n > lp; n--)   \
363         rex->offs[n].end = -1;              \
364     rex->lastparen = n;                     \
365     rex->lastcloseparen = lcp;
366
367
368 STATIC void
369 S_regcppop(pTHX_ regexp *rex, U32 *maxopenparen_p)
370 {
371     dVAR;
372     UV i;
373     U32 paren;
374     GET_RE_DEBUG_FLAGS_DECL;
375
376     PERL_ARGS_ASSERT_REGCPPOP;
377
378     /* Pop REGCP_OTHER_ELEMS before the parentheses loop starts. */
379     i = SSPOPUV;
380     assert((i & SAVE_MASK) == SAVEt_REGCONTEXT); /* Check that the magic cookie is there. */
381     i >>= SAVE_TIGHT_SHIFT; /* Parentheses elements to pop. */
382     rex->lastcloseparen = SSPOPINT;
383     rex->lastparen = SSPOPINT;
384     *maxopenparen_p = SSPOPINT;
385
386     i -= REGCP_OTHER_ELEMS;
387     /* Now restore the parentheses context. */
388     DEBUG_BUFFERS_r(
389         if (i || rex->lastparen + 1 <= rex->nparens)
390             PerlIO_printf(Perl_debug_log,
391                 "rex=0x%"UVxf" offs=0x%"UVxf": restoring capture indices to:\n",
392                 PTR2UV(rex),
393                 PTR2UV(rex->offs)
394             );
395     );
396     paren = *maxopenparen_p;
397     for ( ; i > 0; i -= REGCP_PAREN_ELEMS) {
398         SSize_t tmps;
399         rex->offs[paren].start_tmp = SSPOPINT;
400         rex->offs[paren].start = SSPOPIV;
401         tmps = SSPOPIV;
402         if (paren <= rex->lastparen)
403             rex->offs[paren].end = tmps;
404         DEBUG_BUFFERS_r( PerlIO_printf(Perl_debug_log,
405             "    \\%"UVuf": %"IVdf"(%"IVdf")..%"IVdf"%s\n",
406             (UV)paren,
407             (IV)rex->offs[paren].start,
408             (IV)rex->offs[paren].start_tmp,
409             (IV)rex->offs[paren].end,
410             (paren > rex->lastparen ? "(skipped)" : ""));
411         );
412         paren--;
413     }
414 #if 1
415     /* It would seem that the similar code in regtry()
416      * already takes care of this, and in fact it is in
417      * a better location to since this code can #if 0-ed out
418      * but the code in regtry() is needed or otherwise tests
419      * requiring null fields (pat.t#187 and split.t#{13,14}
420      * (as of patchlevel 7877)  will fail.  Then again,
421      * this code seems to be necessary or otherwise
422      * this erroneously leaves $1 defined: "1" =~ /^(?:(\d)x)?\d$/
423      * --jhi updated by dapm */
424     for (i = rex->lastparen + 1; i <= rex->nparens; i++) {
425         if (i > *maxopenparen_p)
426             rex->offs[i].start = -1;
427         rex->offs[i].end = -1;
428         DEBUG_BUFFERS_r( PerlIO_printf(Perl_debug_log,
429             "    \\%"UVuf": %s   ..-1 undeffing\n",
430             (UV)i,
431             (i > *maxopenparen_p) ? "-1" : "  "
432         ));
433     }
434 #endif
435 }
436
437 /* restore the parens and associated vars at savestack position ix,
438  * but without popping the stack */
439
440 STATIC void
441 S_regcp_restore(pTHX_ regexp *rex, I32 ix, U32 *maxopenparen_p)
442 {
443     I32 tmpix = PL_savestack_ix;
444     PL_savestack_ix = ix;
445     regcppop(rex, maxopenparen_p);
446     PL_savestack_ix = tmpix;
447 }
448
449 #define regcpblow(cp) LEAVE_SCOPE(cp)   /* Ignores regcppush()ed data. */
450
451 STATIC bool
452 S_isFOO_lc(pTHX_ const U8 classnum, const U8 character)
453 {
454     /* Returns a boolean as to whether or not 'character' is a member of the
455      * Posix character class given by 'classnum' that should be equivalent to a
456      * value in the typedef '_char_class_number'.
457      *
458      * Ideally this could be replaced by a just an array of function pointers
459      * to the C library functions that implement the macros this calls.
460      * However, to compile, the precise function signatures are required, and
461      * these may vary from platform to to platform.  To avoid having to figure
462      * out what those all are on each platform, I (khw) am using this method,
463      * which adds an extra layer of function call overhead (unless the C
464      * optimizer strips it away).  But we don't particularly care about
465      * performance with locales anyway. */
466
467     switch ((_char_class_number) classnum) {
468         case _CC_ENUM_ALPHANUMERIC: return isALPHANUMERIC_LC(character);
469         case _CC_ENUM_ALPHA:     return isALPHA_LC(character);
470         case _CC_ENUM_ASCII:     return isASCII_LC(character);
471         case _CC_ENUM_BLANK:     return isBLANK_LC(character);
472         case _CC_ENUM_CASED:     return isLOWER_LC(character)
473                                         || isUPPER_LC(character);
474         case _CC_ENUM_CNTRL:     return isCNTRL_LC(character);
475         case _CC_ENUM_DIGIT:     return isDIGIT_LC(character);
476         case _CC_ENUM_GRAPH:     return isGRAPH_LC(character);
477         case _CC_ENUM_LOWER:     return isLOWER_LC(character);
478         case _CC_ENUM_PRINT:     return isPRINT_LC(character);
479         case _CC_ENUM_PSXSPC:    return isPSXSPC_LC(character);
480         case _CC_ENUM_PUNCT:     return isPUNCT_LC(character);
481         case _CC_ENUM_SPACE:     return isSPACE_LC(character);
482         case _CC_ENUM_UPPER:     return isUPPER_LC(character);
483         case _CC_ENUM_WORDCHAR:  return isWORDCHAR_LC(character);
484         case _CC_ENUM_XDIGIT:    return isXDIGIT_LC(character);
485         default:    /* VERTSPACE should never occur in locales */
486             Perl_croak(aTHX_ "panic: isFOO_lc() has an unexpected character class '%d'", classnum);
487     }
488
489     assert(0); /* NOTREACHED */
490     return FALSE;
491 }
492
493 STATIC bool
494 S_isFOO_utf8_lc(pTHX_ const U8 classnum, const U8* character)
495 {
496     /* Returns a boolean as to whether or not the (well-formed) UTF-8-encoded
497      * 'character' is a member of the Posix character class given by 'classnum'
498      * that should be equivalent to a value in the typedef
499      * '_char_class_number'.
500      *
501      * This just calls isFOO_lc on the code point for the character if it is in
502      * the range 0-255.  Outside that range, all characters avoid Unicode
503      * rules, ignoring any locale.  So use the Unicode function if this class
504      * requires a swash, and use the Unicode macro otherwise. */
505
506     PERL_ARGS_ASSERT_ISFOO_UTF8_LC;
507
508     if (UTF8_IS_INVARIANT(*character)) {
509         return isFOO_lc(classnum, *character);
510     }
511     else if (UTF8_IS_DOWNGRADEABLE_START(*character)) {
512         return isFOO_lc(classnum,
513                         TWO_BYTE_UTF8_TO_NATIVE(*character, *(character + 1)));
514     }
515
516     if (classnum < _FIRST_NON_SWASH_CC) {
517
518         /* Initialize the swash unless done already */
519         if (! PL_utf8_swash_ptrs[classnum]) {
520             U8 flags = _CORE_SWASH_INIT_ACCEPT_INVLIST;
521             PL_utf8_swash_ptrs[classnum] =
522                     _core_swash_init("utf8",
523                                      "",
524                                      &PL_sv_undef, 1, 0,
525                                      PL_XPosix_ptrs[classnum], &flags);
526         }
527
528         return cBOOL(swash_fetch(PL_utf8_swash_ptrs[classnum], (U8 *)
529                                  character,
530                                  TRUE /* is UTF */ ));
531     }
532
533     switch ((_char_class_number) classnum) {
534         case _CC_ENUM_SPACE:
535         case _CC_ENUM_PSXSPC:    return is_XPERLSPACE_high(character);
536
537         case _CC_ENUM_BLANK:     return is_HORIZWS_high(character);
538         case _CC_ENUM_XDIGIT:    return is_XDIGIT_high(character);
539         case _CC_ENUM_VERTSPACE: return is_VERTWS_high(character);
540         default:                 return 0;  /* Things like CNTRL are always
541                                                below 256 */
542     }
543
544     assert(0); /* NOTREACHED */
545     return FALSE;
546 }
547
548 /*
549  * pregexec and friends
550  */
551
552 #ifndef PERL_IN_XSUB_RE
553 /*
554  - pregexec - match a regexp against a string
555  */
556 I32
557 Perl_pregexec(pTHX_ REGEXP * const prog, char* stringarg, char *strend,
558          char *strbeg, SSize_t minend, SV *screamer, U32 nosave)
559 /* stringarg: the point in the string at which to begin matching */
560 /* strend:    pointer to null at end of string */
561 /* strbeg:    real beginning of string */
562 /* minend:    end of match must be >= minend bytes after stringarg. */
563 /* screamer:  SV being matched: only used for utf8 flag, pos() etc; string
564  *            itself is accessed via the pointers above */
565 /* nosave:    For optimizations. */
566 {
567     PERL_ARGS_ASSERT_PREGEXEC;
568
569     return
570         regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
571                       nosave ? 0 : REXEC_COPY_STR);
572 }
573 #endif
574
575
576
577 /* re_intuit_start():
578  *
579  * Based on some optimiser hints, try to find the earliest position in the
580  * string where the regex could match.
581  *
582  *   rx:     the regex to match against
583  *   sv:     the SV being matched: only used for utf8 flag; the string
584  *           itself is accessed via the pointers below. Note that on
585  *           something like an overloaded SV, SvPOK(sv) may be false
586  *           and the string pointers may point to something unrelated to
587  *           the SV itself.
588  *   strbeg: real beginning of string
589  *   strpos: the point in the string at which to begin matching
590  *   strend: pointer to the byte following the last char of the string
591  *   flags   currently unused; set to 0
592  *   data:   currently unused; set to NULL
593  *
594  * The basic idea of re_intuit_start() is to use some known information
595  * about the pattern, namely:
596  *
597  *   a) the longest known anchored substring (i.e. one that's at a
598  *      constant offset from the beginning of the pattern; but not
599  *      necessarily at a fixed offset from the beginning of the
600  *      string);
601  *   b) the longest floating substring (i.e. one that's not at a constant
602  *      offset from the beginning of the pattern);
603  *   c) Whether the pattern is anchored to the string; either
604  *      an absolute anchor: /^../, or anchored to \n: /^.../m,
605  *      or anchored to pos(): /\G/;
606  *   d) A start class: a real or synthetic character class which
607  *      represents which characters are legal at the start of the pattern;
608  *
609  * to either quickly reject the match, or to find the earliest position
610  * within the string at which the pattern might match, thus avoiding
611  * running the full NFA engine at those earlier locations, only to
612  * eventually fail and retry further along.
613  *
614  * Returns NULL if the pattern can't match, or returns the address within
615  * the string which is the earliest place the match could occur.
616  *
617  * The longest of the anchored and floating substrings is called 'check'
618  * and is checked first. The other is called 'other' and is checked
619  * second. The 'other' substring may not be present.  For example,
620  *
621  *    /(abc|xyz)ABC\d{0,3}DEFG/
622  *
623  * will have
624  *
625  *   check substr (float)    = "DEFG", offset 6..9 chars
626  *   other substr (anchored) = "ABC",  offset 3..3 chars
627  *   stclass = [ax]
628  *
629  * Be aware that during the course of this function, sometimes 'anchored'
630  * refers to a substring being anchored relative to the start of the
631  * pattern, and sometimes to the pattern itself being anchored relative to
632  * the string. For example:
633  *
634  *   /\dabc/:   "abc" is anchored to the pattern;
635  *   /^\dabc/:  "abc" is anchored to the pattern and the string;
636  *   /\d+abc/:  "abc" is anchored to neither the pattern nor the string;
637  *   /^\d+abc/: "abc" is anchored to neither the pattern nor the string,
638  *                    but the pattern is anchored to the string.
639  */
640
641 char *
642 Perl_re_intuit_start(pTHX_
643                     REGEXP * const rx,
644                     SV *sv,
645                     const char * const strbeg,
646                     char *strpos,
647                     char *strend,
648                     const U32 flags,
649                     re_scream_pos_data *data)
650 {
651     dVAR;
652     struct regexp *const prog = ReANY(rx);
653     SSize_t start_shift = prog->check_offset_min;
654     /* Should be nonnegative! */
655     SSize_t end_shift   = 0;
656     /* current lowest pos in string where the regex can start matching */
657     char *rx_origin = strpos;
658     SV *check;
659     const bool utf8_target = (sv && SvUTF8(sv)) ? 1 : 0; /* if no sv we have to assume bytes */
660     U8   other_ix = 1 - prog->substrs->check_ix;
661     bool ml_anch = 0;
662     char *other_last = strpos;/* latest pos 'other' substr already checked to */
663     char *check_at = NULL;              /* check substr found at this pos */
664     const I32 multiline = prog->extflags & RXf_PMf_MULTILINE;
665     RXi_GET_DECL(prog,progi);
666     regmatch_info reginfo_buf;  /* create some info to pass to find_byclass */
667     regmatch_info *const reginfo = &reginfo_buf;
668     GET_RE_DEBUG_FLAGS_DECL;
669
670     PERL_ARGS_ASSERT_RE_INTUIT_START;
671     PERL_UNUSED_ARG(flags);
672     PERL_UNUSED_ARG(data);
673
674     DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
675                 "Intuit: trying to determine minimum start position...\n"));
676
677     /* for now, assume that all substr offsets are positive. If at some point
678      * in the future someone wants to do clever things with look-behind and
679      * -ve offsets, they'll need to fix up any code in this function
680      * which uses these offsets. See the thread beginning
681      * <20140113145929.GF27210@iabyn.com>
682      */
683     assert(prog->substrs->data[0].min_offset >= 0);
684     assert(prog->substrs->data[0].max_offset >= 0);
685     assert(prog->substrs->data[1].min_offset >= 0);
686     assert(prog->substrs->data[1].max_offset >= 0);
687     assert(prog->substrs->data[2].min_offset >= 0);
688     assert(prog->substrs->data[2].max_offset >= 0);
689
690     /* for now, assume that if both present, that the floating substring
691      * doesn't start before the anchored substring.
692      * If you break this assumption (e.g. doing better optimisations
693      * with lookahead/behind), then you'll need to audit the code in this
694      * function carefully first
695      */
696     assert(
697             ! (  (prog->anchored_utf8 || prog->anchored_substr)
698               && (prog->float_utf8    || prog->float_substr))
699            || (prog->float_min_offset >= prog->anchored_offset));
700
701     /* byte rather than char calculation for efficiency. It fails
702      * to quickly reject some cases that can't match, but will reject
703      * them later after doing full char arithmetic */
704     if (prog->minlen > strend - strpos) {
705         DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
706                               "  String too short...\n"));
707         goto fail;
708     }
709
710     reginfo->is_utf8_target = cBOOL(utf8_target);
711     reginfo->info_aux = NULL;
712     reginfo->strbeg = strbeg;
713     reginfo->strend = strend;
714     reginfo->is_utf8_pat = cBOOL(RX_UTF8(rx));
715     reginfo->intuit = 1;
716     /* not actually used within intuit, but zero for safety anyway */
717     reginfo->poscache_maxiter = 0;
718
719     if (utf8_target) {
720         if (!prog->check_utf8 && prog->check_substr)
721             to_utf8_substr(prog);
722         check = prog->check_utf8;
723     } else {
724         if (!prog->check_substr && prog->check_utf8) {
725             if (! to_byte_substr(prog)) {
726                 NON_UTF8_TARGET_BUT_UTF8_REQUIRED(fail);
727             }
728         }
729         check = prog->check_substr;
730     }
731
732     /* dump the various substring data */
733     DEBUG_OPTIMISE_MORE_r({
734         int i;
735         for (i=0; i<=2; i++) {
736             SV *sv = (utf8_target ? prog->substrs->data[i].utf8_substr
737                                   : prog->substrs->data[i].substr);
738             if (!sv)
739                 continue;
740
741             PerlIO_printf(Perl_debug_log,
742                 "  substrs[%d]: min=%"IVdf" max=%"IVdf" end shift=%"IVdf
743                 " useful=%"IVdf" utf8=%d [%s]\n",
744                 i,
745                 (IV)prog->substrs->data[i].min_offset,
746                 (IV)prog->substrs->data[i].max_offset,
747                 (IV)prog->substrs->data[i].end_shift,
748                 BmUSEFUL(sv),
749                 utf8_target ? 1 : 0,
750                 SvPEEK(sv));
751         }
752     });
753
754     if (prog->intflags & PREGf_ANCH) { /* Match at \G, beg-of-str or after \n */
755
756         /* ml_anch: check after \n?
757          *
758          * A note about IMPLICIT: on an un-anchored pattern beginning
759          * with /.*.../, these flags will have been added by the
760          * compiler:
761          *   /.*abc/, /.*abc/m:  PREGf_IMPLICIT | PREGf_ANCH_MBOL
762          *   /.*abc/s:           PREGf_IMPLICIT | PREGf_ANCH_SBOL
763          */
764         ml_anch =      (prog->intflags & PREGf_ANCH_MBOL)
765                    && !(prog->intflags & PREGf_IMPLICIT);
766
767         if (!ml_anch && !(prog->intflags & PREGf_IMPLICIT)) {
768             /* we are only allowed to match at BOS or \G */
769
770             /* trivially reject if there's a BOS anchor and we're not at BOS.
771              *
772              * Note that we don't try to do a similar quick reject for
773              * \G, since generally the caller will have calculated strpos
774              * based on pos() and gofs, so the string is already correctly
775              * anchored by definition; and handling the exceptions would
776              * be too fiddly (e.g. REXEC_IGNOREPOS).
777              */
778             if (   strpos != strbeg
779                 && (prog->intflags & (PREGf_ANCH_BOL|PREGf_ANCH_SBOL)))
780             {
781                 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
782                                 "  Not at start...\n"));
783                 goto fail;
784             }
785
786             /* in the presence of an anchor, the anchored (relative to the
787              * start of the regex) substr must also be anchored relative
788              * to strpos. So quickly reject if substr isn't found there.
789              * This works for \G too, because the caller will already have
790              * subtracted gofs from pos, and gofs is the offset from the
791              * \G to the start of the regex. For example, in /.abc\Gdef/,
792              * where substr="abcdef", pos()=3, gofs=4, offset_min=1:
793              * caller will have set strpos=pos()-4; we look for the substr
794              * at position pos()-4+1, which lines up with the "a" */
795
796             if (prog->check_offset_min == prog->check_offset_max
797                 && !(prog->intflags & PREGf_CANY_SEEN))
798             {
799                 /* Substring at constant offset from beg-of-str... */
800                 SSize_t slen = SvCUR(check);
801                 char *s = HOP3c(strpos, prog->check_offset_min, strend);
802             
803                 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
804                     "  Looking for check substr at fixed offset %"IVdf"...\n",
805                     (IV)prog->check_offset_min));
806
807                 if (SvTAIL(check)) {
808                     /* In this case, the regex is anchored at the end too.
809                      * Unless it's a multiline match, the lengths must match
810                      * exactly, give or take a \n.  NB: slen >= 1 since
811                      * the last char of check is \n */
812                     if (!multiline
813                         && (   strend - s > slen
814                             || strend - s < slen - 1
815                             || (strend - s == slen && strend[-1] != '\n')))
816                     {
817                         DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
818                                             "  String too long...\n"));
819                         goto fail_finish;
820                     }
821                     /* Now should match s[0..slen-2] */
822                     slen--;
823                 }
824                 if (slen && (*SvPVX_const(check) != *s
825                     || (slen > 1 && memNE(SvPVX_const(check), s, slen))))
826                 {
827                     DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
828                                     "  String not equal...\n"));
829                     goto fail_finish;
830                 }
831
832                 check_at = s;
833                 goto success_at_start;
834             }
835         }
836     }
837
838     end_shift = prog->check_end_shift;
839
840 #ifdef DEBUGGING        /* 7/99: reports of failure (with the older version) */
841     if (end_shift < 0)
842         Perl_croak(aTHX_ "panic: end_shift: %"IVdf" pattern:\n%s\n ",
843                    (IV)end_shift, RX_PRECOMP(prog));
844 #endif
845
846   restart:
847     
848     /* This is the (re)entry point of the main loop in this function.
849      * The goal of this loop is to:
850      * 1) find the "check" substring in the region rx_origin..strend
851      *    (adjusted by start_shift / end_shift). If not found, reject
852      *    immediately.
853      * 2) If it exists, look for the "other" substr too if defined; for
854      *    example, if the check substr maps to the anchored substr, then
855      *    check the floating substr, and vice-versa. If not found, go
856      *    back to (1) with rx_origin suitably incremented.
857      * 3) If we find an rx_origin position that doesn't contradict
858      *    either of the substrings, then check the possible additional
859      *    constraints on rx_origin of /^.../m or a known start class.
860      *    If these fail, then depending on which constraints fail, jump
861      *    back to here, or to various other re-entry points further along
862      *    that skip some of the first steps.
863      * 4) If we pass all those tests, update the BmUSEFUL() count on the
864      *    substring. If the start position was determined to be at the
865      *    beginning of the string  - so, not rejected, but not optimised,
866      *    since we have to run regmatch from position 0 - decrement the
867      *    BmUSEFUL() count. Otherwise increment it.
868      */
869
870
871     /* first, look for the 'check' substring */
872
873     {
874         U8* start_point;
875         U8* end_point;
876
877         DEBUG_OPTIMISE_MORE_r({
878             PerlIO_printf(Perl_debug_log,
879                 "  At restart: rx_origin=%"IVdf" Check offset min: %"IVdf
880                 " Start shift: %"IVdf" End shift %"IVdf
881                 " Real end Shift: %"IVdf"\n",
882                 (IV)(rx_origin - strpos),
883                 (IV)prog->check_offset_min,
884                 (IV)start_shift,
885                 (IV)end_shift,
886                 (IV)prog->check_end_shift);
887         });
888         
889         if (prog->intflags & PREGf_CANY_SEEN) {
890             start_point= (U8*)(rx_origin + start_shift);
891             end_point= (U8*)(strend - end_shift);
892             if (start_point > end_point)
893                 goto fail_finish;
894         } else {
895             end_point = HOP3(strend, -end_shift, strbeg);
896             start_point = HOPMAYBE3(rx_origin, start_shift, end_point);
897             if (!start_point)
898                 goto fail_finish;
899         }
900
901
902         /* If the regex is absolutely anchored to either the start of the
903          * string (BOL,SBOL) or to pos() (ANCH_GPOS), then
904          * check_offset_max represents an upper bound on the string where
905          * the substr could start. For the ANCH_GPOS case, we assume that
906          * the caller of intuit will have already set strpos to
907          * pos()-gofs, so in this case strpos + offset_max will still be
908          * an upper bound on the substr.
909          */
910         if (!ml_anch
911             && prog->intflags & PREGf_ANCH
912             && prog->check_offset_max != SSize_t_MAX)
913         {
914             SSize_t len = SvCUR(check) - !!SvTAIL(check);
915             const char * const anchor =
916                         (prog->intflags & PREGf_ANCH_GPOS ? strpos : strbeg);
917
918             /* do a bytes rather than chars comparison. It's conservative;
919              * so it skips doing the HOP if the result can't possibly end
920              * up earlier than the old value of end_point.
921              */
922             if ((char*)end_point - anchor > prog->check_offset_max) {
923                 end_point = HOP3lim((U8*)anchor,
924                                 prog->check_offset_max,
925                                 end_point -len)
926                             + len;
927             }
928         }
929
930         DEBUG_OPTIMISE_MORE_r({
931             PerlIO_printf(Perl_debug_log, "  fbm_instr len=%d str=<%.*s>\n",
932                 (int)(end_point - start_point),
933                 (int)(end_point - start_point) > 20 ? 20 : (int)(end_point - start_point), 
934                 start_point);
935         });
936
937         check_at = fbm_instr( start_point, end_point,
938                       check, multiline ? FBMrf_MULTILINE : 0);
939
940         /* Update the count-of-usability, remove useless subpatterns,
941             unshift s.  */
942
943         DEBUG_EXECUTE_r({
944             RE_PV_QUOTED_DECL(quoted, utf8_target, PERL_DEBUG_PAD_ZERO(0),
945                 SvPVX_const(check), RE_SV_DUMPLEN(check), 30);
946             PerlIO_printf(Perl_debug_log, "  %s %s substr %s%s%s",
947                               (check_at ? "Found" : "Did not find"),
948                 (check == (utf8_target ? prog->anchored_utf8 : prog->anchored_substr)
949                     ? "anchored" : "floating"),
950                 quoted,
951                 RE_SV_TAIL(check),
952                 (check_at ? " at offset " : "...\n") );
953         });
954
955         if (!check_at)
956             goto fail_finish;
957         /* Finish the diagnostic message */
958         DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%ld...\n", (long)(check_at - strpos)) );
959
960         /* set rx_origin to the minimum position where the regex could start
961          * matching, given the constraint of the just-matched check substring.
962          * But don't set it lower than previously.
963          */
964
965         if (check_at - rx_origin > prog->check_offset_max)
966             rx_origin = HOP3c(check_at, -prog->check_offset_max, rx_origin);
967     }
968
969
970     /* now look for the 'other' substring if defined */
971
972     if (utf8_target ? prog->substrs->data[other_ix].utf8_substr
973                     : prog->substrs->data[other_ix].substr)
974     {
975         /* Take into account the "other" substring. */
976         char *last, *last1;
977         char *s;
978         SV* must;
979         struct reg_substr_datum *other;
980
981       do_other_substr:
982         other = &prog->substrs->data[other_ix];
983
984         /* if "other" is anchored:
985          * we've previously found a floating substr starting at check_at.
986          * This means that the regex origin must lie somewhere
987          * between min (rx_origin): HOP3(check_at, -check_offset_max)
988          * and max:                 HOP3(check_at, -check_offset_min)
989          * (except that min will be >= strpos)
990          * So the fixed  substr must lie somewhere between
991          *  HOP3(min, anchored_offset)
992          *  HOP3(max, anchored_offset) + SvCUR(substr)
993          */
994
995         /* if "other" is floating
996          * Calculate last1, the absolute latest point where the
997          * floating substr could start in the string, ignoring any
998          * constraints from the earlier fixed match. It is calculated
999          * as follows:
1000          *
1001          * strend - prog->minlen (in chars) is the absolute latest
1002          * position within the string where the origin of the regex
1003          * could appear. The latest start point for the floating
1004          * substr is float_min_offset(*) on from the start of the
1005          * regex.  last1 simply combines thee two offsets.
1006          *
1007          * (*) You might think the latest start point should be
1008          * float_max_offset from the regex origin, and technically
1009          * you'd be correct. However, consider
1010          *    /a\d{2,4}bcd\w/
1011          * Here, float min, max are 3,5 and minlen is 7.
1012          * This can match either
1013          *    /a\d\dbcd\w/
1014          *    /a\d\d\dbcd\w/
1015          *    /a\d\d\d\dbcd\w/
1016          * In the first case, the regex matches minlen chars; in the
1017          * second, minlen+1, in the third, minlen+2.
1018          * In the first case, the floating offset is 3 (which equals
1019          * float_min), in the second, 4, and in the third, 5 (which
1020          * equals float_max). In all cases, the floating string bcd
1021          * can never start more than 4 chars from the end of the
1022          * string, which equals minlen - float_min. As the substring
1023          * starts to match more than float_min from the start of the
1024          * regex, it makes the regex match more than minlen chars,
1025          * and the two cancel each other out. So we can always use
1026          * float_min - minlen, rather than float_max - minlen for the
1027          * latest position in the string.
1028          *
1029          * Note that -minlen + float_min_offset is equivalent (AFAIKT)
1030          * to CHR_SVLEN(must) - !!SvTAIL(must) + prog->float_end_shift
1031          */
1032
1033         assert(prog->minlen >= other->min_offset);
1034         last1 = HOP3c(strend,
1035                         other->min_offset - prog->minlen, strbeg);
1036
1037         if (other_ix) {/* i.e. if (other-is-float) */
1038             /* last is the latest point where the floating substr could
1039              * start, *given* any constraints from the earlier fixed
1040              * match. This constraint is that the floating string starts
1041              * <= float_max_offset chars from the regex origin (rx_origin).
1042              * If this value is less than last1, use it instead.
1043              */
1044             assert(rx_origin <= last1);
1045             last =
1046                 /* this condition handles the offset==infinity case, and
1047                  * is a short-cut otherwise. Although it's comparing a
1048                  * byte offset to a char length, it does so in a safe way,
1049                  * since 1 char always occupies 1 or more bytes,
1050                  * so if a string range is  (last1 - rx_origin) bytes,
1051                  * it will be less than or equal to  (last1 - rx_origin)
1052                  * chars; meaning it errs towards doing the accurate HOP3
1053                  * rather than just using last1 as a short-cut */
1054                 (last1 - rx_origin) < other->max_offset
1055                     ? last1
1056                     : (char*)HOP3lim(rx_origin, other->max_offset, last1);
1057         }
1058         else {
1059             assert(strpos + start_shift <= check_at);
1060             last = HOP4c(check_at, other->min_offset - start_shift,
1061                         strbeg, strend);
1062         }
1063
1064         s = HOP3c(rx_origin, other->min_offset, strend);
1065         if (s < other_last)     /* These positions already checked */
1066             s = other_last;
1067
1068         must = utf8_target ? other->utf8_substr : other->substr;
1069         assert(SvPOK(must));
1070         s = fbm_instr(
1071             (unsigned char*)s,
1072             (unsigned char*)last + SvCUR(must) - (SvTAIL(must)!=0),
1073             must,
1074             multiline ? FBMrf_MULTILINE : 0
1075         );
1076         DEBUG_EXECUTE_r({
1077             RE_PV_QUOTED_DECL(quoted, utf8_target, PERL_DEBUG_PAD_ZERO(0),
1078                 SvPVX_const(must), RE_SV_DUMPLEN(must), 30);
1079             PerlIO_printf(Perl_debug_log, "  %s %s substr %s%s",
1080                 s ? "Found" : "Contradicts",
1081                 other_ix ? "floating" : "anchored",
1082                 quoted, RE_SV_TAIL(must));
1083         });
1084
1085
1086         if (!s) {
1087             /* last1 is latest possible substr location. If we didn't
1088              * find it before there, we never will */
1089             if (last >= last1) {
1090                 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1091                                         ", giving up...\n"));
1092                 goto fail_finish;
1093             }
1094
1095             /* try to find the check substr again at a later
1096              * position. Maybe next time we'll find the "other" substr
1097              * in range too */
1098             DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1099                 ", trying %s at offset %ld...\n",
1100                 (other_ix ? "floating" : "anchored"),
1101                 (long)(HOP3c(check_at, 1, strend) - strpos)));
1102
1103             other_last = HOP3c(last, 1, strend) /* highest failure */;
1104             rx_origin =
1105                 other_ix /* i.e. if other-is-float */
1106                     ? HOP3c(rx_origin, 1, strend)
1107                     : HOP4c(last, 1 - other->min_offset, strbeg, strend);
1108             goto restart;
1109         }
1110         else {
1111             DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
1112                   (long)(s - strpos)));
1113
1114             if (other_ix) { /* if (other-is-float) */
1115                 /* other_last is set to s, not s+1, since its possible for
1116                  * a floating substr to fail first time, then succeed
1117                  * second time at the same floating position; e.g.:
1118                  *     "-AB--AABZ" =~ /\wAB\d*Z/
1119                  * The first time round, anchored and float match at
1120                  * "-(AB)--AAB(Z)" then fail on the initial \w character
1121                  * class. Second time round, they match at "-AB--A(AB)(Z)".
1122                  */
1123                 other_last = s;
1124             }
1125             else {
1126                 rx_origin = HOP3c(s, -other->min_offset, strbeg);
1127                 other_last = HOP3c(s, 1, strend);
1128             }
1129         }
1130     }
1131     else {
1132         DEBUG_OPTIMISE_MORE_r(
1133             PerlIO_printf(Perl_debug_log,
1134                 "  Check-only match: offset min:%"IVdf" max:%"IVdf
1135                 " check_at:%"IVdf" rx_origin:%"IVdf" rx_origin-check_at:%"IVdf
1136                 " strend-strpos:%"IVdf"\n",
1137                 (IV)prog->check_offset_min,
1138                 (IV)prog->check_offset_max,
1139                 (IV)(check_at-strpos),
1140                 (IV)(rx_origin-strpos),
1141                 (IV)(rx_origin-check_at),
1142                 (IV)(strend-strpos)
1143             )
1144         );
1145     }
1146
1147   postprocess_substr_matches:
1148
1149     /* handle the extra constraint of /^.../m if present */
1150
1151     if (ml_anch && rx_origin != strbeg && rx_origin[-1] != '\n') {
1152         char *s;
1153
1154         DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1155                         "  looking for /^/m anchor"));
1156
1157         /* we have failed the constraint of a \n before rx_origin.
1158          * Find the next \n, if any, even if it's beyond the current
1159          * anchored and/or floating substrings. Whether we should be
1160          * scanning ahead for the next \n or the next substr is debatable.
1161          * On the one hand you'd expect rare substrings to appear less
1162          * often than \n's. On the other hand, searching for \n means
1163          * we're effectively flipping been check_substr and "\n" on each
1164          * iteration as the current "rarest" string candidate, which
1165          * means for example that we'll quickly reject the whole string if
1166          * hasn't got a \n, rather than trying every substr position
1167          * first
1168          */
1169
1170         s = HOP3c(strend, - prog->minlen, strpos);
1171         if (s <= rx_origin ||
1172             ! ( rx_origin = (char *)memchr(rx_origin, '\n', s - rx_origin)))
1173         {
1174             DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1175                             "  Did not find /%s^%s/m...\n",
1176                             PL_colors[0], PL_colors[1]));
1177             goto fail_finish;
1178         }
1179
1180         /* earliest possible origin is 1 char after the \n.
1181          * (since *rx_origin == '\n', it's safe to ++ here rather than
1182          * HOP(rx_origin, 1)) */
1183         rx_origin++;
1184
1185         if (prog->substrs->check_ix == 0  /* check is anchored */
1186             || rx_origin >= HOP3c(check_at,  - prog->check_offset_min, strpos))
1187         {
1188             /* Position contradicts check-string; either because
1189              * check was anchored (and thus has no wiggle room),
1190              * or check was float and rx_origin is above the float range */
1191             DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1192                 "  Found /%s^%s/m, restarting lookup for check-string at offset %ld...\n",
1193                 PL_colors[0], PL_colors[1], (long)(rx_origin - strpos)));
1194             goto restart;
1195         }
1196
1197         /* if we get here, the check substr must have been float,
1198          * is in range, and we may or may not have had an anchored
1199          * "other" substr which still contradicts */
1200         assert(prog->substrs->check_ix); /* check is float */
1201
1202         if (utf8_target ? prog->anchored_utf8 : prog->anchored_substr) {
1203             /* whoops, the anchored "other" substr exists, so we still
1204              * contradict. On the other hand, the float "check" substr
1205              * didn't contradict, so just retry the anchored "other"
1206              * substr */
1207             DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1208                 "  Found /%s^%s/m at offset %ld, rescanning for anchored from offset %ld...\n",
1209                 PL_colors[0], PL_colors[1],
1210                 (long)(rx_origin - strpos),
1211                 (long)(rx_origin - strpos + prog->anchored_offset)));
1212             goto do_other_substr;
1213         }
1214
1215         /* success: we don't contradict the found floating substring
1216          * (and there's no anchored substr). */
1217         DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1218             "  Found /%s^%s/m at offset %ld...\n",
1219             PL_colors[0], PL_colors[1], (long)(rx_origin - strpos)));
1220     }
1221     else {
1222         DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1223             "  (multiline anchor test skipped)\n"));
1224     }
1225
1226   success_at_start:
1227
1228
1229     /* if we have a starting character class, then test that extra constraint.
1230      * (trie stclasses are too expensive to use here, we are better off to
1231      * leave it to regmatch itself) */
1232
1233     if (progi->regstclass && PL_regkind[OP(progi->regstclass)]!=TRIE) {
1234         const U8* const str = (U8*)STRING(progi->regstclass);
1235
1236         /* XXX this value could be pre-computed */
1237         const int cl_l = (PL_regkind[OP(progi->regstclass)] == EXACT
1238                     ?  (reginfo->is_utf8_pat
1239                         ? utf8_distance(str + STR_LEN(progi->regstclass), str)
1240                         : STR_LEN(progi->regstclass))
1241                     : 1);
1242         char * endpos;
1243         char *s;
1244         /* latest pos that a matching float substr constrains rx start to */
1245         char *rx_max_float = NULL;
1246
1247         /* if the current rx_origin is anchored, either by satisfying an
1248          * anchored substring constraint, or a /^.../m constraint, then we
1249          * can reject the current origin if the start class isn't found
1250          * at the current position. If we have a float-only match, then
1251          * rx_origin is constrained to a range; so look for the start class
1252          * in that range. if neither, then look for the start class in the
1253          * whole rest of the string */
1254
1255         /* XXX DAPM it's not clear what the minlen test is for, and why
1256          * it's not used in the floating case. Nothing in the test suite
1257          * causes minlen == 0 here. See <20140313134639.GS12844@iabyn.com>.
1258          * Here are some old comments, which may or may not be correct:
1259          *
1260          *   minlen == 0 is possible if regstclass is \b or \B,
1261          *   and the fixed substr is ''$.
1262          *   Since minlen is already taken into account, rx_origin+1 is
1263          *   before strend; accidentally, minlen >= 1 guaranties no false
1264          *   positives at rx_origin + 1 even for \b or \B.  But (minlen? 1 :
1265          *   0) below assumes that regstclass does not come from lookahead...
1266          *   If regstclass takes bytelength more than 1: If charlength==1, OK.
1267          *   This leaves EXACTF-ish only, which are dealt with in
1268          *   find_byclass().
1269          */
1270
1271         if (prog->anchored_substr || prog->anchored_utf8 || ml_anch)
1272             endpos= HOP3c(rx_origin, (prog->minlen ? cl_l : 0), strend);
1273         else if (prog->float_substr || prog->float_utf8) {
1274             rx_max_float = HOP3c(check_at, -start_shift, strbeg);
1275             endpos= HOP3c(rx_max_float, cl_l, strend);
1276         }
1277         else 
1278             endpos= strend;
1279                     
1280         DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1281             "  looking for class: start_shift: %"IVdf" check_at: %"IVdf
1282             " rx_origin: %"IVdf" endpos: %"IVdf"\n",
1283               (IV)start_shift, (IV)(check_at - strbeg),
1284               (IV)(rx_origin - strbeg), (IV)(endpos - strbeg)));
1285
1286         s = find_byclass(prog, progi->regstclass, rx_origin, endpos,
1287                             reginfo);
1288         if (!s) {
1289             if (endpos == strend) {
1290                 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
1291                                 "  Could not match STCLASS...\n") );
1292                 goto fail;
1293             }
1294             DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
1295                                "  This position contradicts STCLASS...\n") );
1296             if ((prog->intflags & PREGf_ANCH) && !ml_anch
1297                         && !(prog->intflags & PREGf_IMPLICIT))
1298                 goto fail;
1299
1300             /* Contradict one of substrings */
1301             if (prog->anchored_substr || prog->anchored_utf8) {
1302                 if (prog->substrs->check_ix == 1) { /* check is float */
1303                     /* Have both, check_string is floating */
1304                     assert(rx_origin + start_shift <= check_at);
1305                     if (rx_origin + start_shift != check_at) {
1306                         /* not at latest position float substr could match:
1307                          * Recheck anchored substring, but not floating.
1308                          * The condition above is in bytes rather than
1309                          * chars for efficiency. It's conservative, in
1310                          * that it errs on the side of doing 'goto
1311                          * do_other_substr', where a more accurate
1312                          * char-based calculation will be done */
1313                         DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
1314                                   "  Looking for anchored substr starting at offset %ld...\n",
1315                                   (long)(other_last - strpos)) );
1316                         goto do_other_substr;
1317                     }
1318                 }
1319             }
1320             else {
1321                 /* float-only */
1322
1323                 if (ml_anch) {
1324                     /* In the presence of ml_anch, we might be able to
1325                      * find another \n without breaking the current float
1326                      * constraint. */
1327
1328                     /* strictly speaking this should be HOP3c(..., 1, ...),
1329                      * but since we goto a block of code that's going to
1330                      * search for the next \n if any, its safe here */
1331                     rx_origin++;
1332                     DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
1333                               "  Looking for /%s^%s/m starting at offset %ld...\n",
1334                               PL_colors[0], PL_colors[1],
1335                               (long)(rx_origin - strpos)) );
1336                     goto postprocess_substr_matches;
1337                 }
1338
1339                 /* strictly speaking this can never be true; but might
1340                  * be if we ever allow intuit without substrings */
1341                 if (!(utf8_target ? prog->float_utf8 : prog->float_substr))
1342                     goto fail;
1343
1344                 rx_origin = rx_max_float;
1345             }
1346
1347             /* at this point, any matching substrings have been
1348              * contradicted. Start again... */
1349
1350             rx_origin = HOP3c(rx_origin, 1, strend);
1351
1352             /* uses bytes rather than char calculations for efficiency.
1353              * It's conservative: it errs on the side of doing 'goto restart',
1354              * where there is code that does a proper char-based test */
1355             if (rx_origin + start_shift + end_shift > strend) {
1356                 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
1357                                        "  Could not match STCLASS...\n") );
1358                 goto fail;
1359             }
1360             DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
1361                 "  Looking for %s substr starting at offset %ld...\n",
1362                 (prog->substrs->check_ix ? "floating" : "anchored"),
1363                 (long)(rx_origin + start_shift - strpos)) );
1364             goto restart;
1365         }
1366
1367         /* Success !!! */
1368
1369         if (rx_origin != s) {
1370             DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1371                         "  By STCLASS: moving %ld --> %ld\n",
1372                                   (long)(rx_origin - strpos), (long)(s - strpos))
1373                    );
1374         }
1375         else {
1376             DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1377                                   "  Does not contradict STCLASS...\n");
1378                    );
1379         }
1380     }
1381
1382     /* Decide whether using the substrings helped */
1383
1384     if (rx_origin != strpos) {
1385         /* Fixed substring is found far enough so that the match
1386            cannot start at strpos. */
1387
1388         DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "  try at offset...\n"));
1389         ++BmUSEFUL(utf8_target ? prog->check_utf8 : prog->check_substr);        /* hooray/5 */
1390     }
1391     else {
1392         /* The found rx_origin position does not prohibit matching at
1393          * strpos, so calling intuit didn't gain us anything. Decrement
1394          * the BmUSEFUL() count on the check substring, and if we reach
1395          * zero, free it.  */
1396         if (!(prog->intflags & PREGf_NAUGHTY)
1397             && (utf8_target ? (
1398                 prog->check_utf8                /* Could be deleted already */
1399                 && --BmUSEFUL(prog->check_utf8) < 0
1400                 && (prog->check_utf8 == prog->float_utf8)
1401             ) : (
1402                 prog->check_substr              /* Could be deleted already */
1403                 && --BmUSEFUL(prog->check_substr) < 0
1404                 && (prog->check_substr == prog->float_substr)
1405             )))
1406         {
1407             /* If flags & SOMETHING - do not do it many times on the same match */
1408             DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "  ... Disabling check substring...\n"));
1409             /* XXX Does the destruction order has to change with utf8_target? */
1410             SvREFCNT_dec(utf8_target ? prog->check_utf8 : prog->check_substr);
1411             SvREFCNT_dec(utf8_target ? prog->check_substr : prog->check_utf8);
1412             prog->check_substr = prog->check_utf8 = NULL;       /* disable */
1413             prog->float_substr = prog->float_utf8 = NULL;       /* clear */
1414             check = NULL;                       /* abort */
1415             /* XXXX This is a remnant of the old implementation.  It
1416                     looks wasteful, since now INTUIT can use many
1417                     other heuristics. */
1418             prog->extflags &= ~RXf_USE_INTUIT;
1419         }
1420     }
1421
1422     DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1423             "Intuit: %sSuccessfully guessed:%s match at offset %ld\n",
1424              PL_colors[4], PL_colors[5], (long)(rx_origin - strpos)) );
1425
1426     return rx_origin;
1427
1428   fail_finish:                          /* Substring not found */
1429     if (prog->check_substr || prog->check_utf8)         /* could be removed already */
1430         BmUSEFUL(utf8_target ? prog->check_utf8 : prog->check_substr) += 5; /* hooray */
1431   fail:
1432     DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch rejected by optimizer%s\n",
1433                           PL_colors[4], PL_colors[5]));
1434     return NULL;
1435 }
1436
1437
1438 #define DECL_TRIE_TYPE(scan) \
1439     const enum { trie_plain, trie_utf8, trie_utf8_fold, trie_latin_utf8_fold, \
1440                  trie_utf8_exactfa_fold, trie_latin_utf8_exactfa_fold } \
1441                     trie_type = ((scan->flags == EXACT) \
1442                               ? (utf8_target ? trie_utf8 : trie_plain) \
1443                               : (scan->flags == EXACTFA) \
1444                                 ? (utf8_target ? trie_utf8_exactfa_fold : trie_latin_utf8_exactfa_fold) \
1445                                 : (utf8_target ? trie_utf8_fold : trie_latin_utf8_fold))
1446
1447 #define REXEC_TRIE_READ_CHAR(trie_type, trie, widecharmap, uc, uscan, len, uvc, charid, foldlen, foldbuf, uniflags) \
1448 STMT_START {                                                                        \
1449     STRLEN skiplen;                                                                 \
1450     U8 flags = FOLD_FLAGS_FULL;                                                     \
1451     switch (trie_type) {                                                            \
1452     case trie_utf8_exactfa_fold:                                                    \
1453         flags |= FOLD_FLAGS_NOMIX_ASCII;                                            \
1454         /* FALLTHROUGH */                                                          \
1455     case trie_utf8_fold:                                                            \
1456         if ( foldlen>0 ) {                                                          \
1457             uvc = utf8n_to_uvchr( (const U8*) uscan, UTF8_MAXLEN, &len, uniflags ); \
1458             foldlen -= len;                                                         \
1459             uscan += len;                                                           \
1460             len=0;                                                                  \
1461         } else {                                                                    \
1462             uvc = _to_utf8_fold_flags( (const U8*) uc, foldbuf, &foldlen, flags);   \
1463             len = UTF8SKIP(uc);                                                     \
1464             skiplen = UNISKIP( uvc );                                               \
1465             foldlen -= skiplen;                                                     \
1466             uscan = foldbuf + skiplen;                                              \
1467         }                                                                           \
1468         break;                                                                      \
1469     case trie_latin_utf8_exactfa_fold:                                              \
1470         flags |= FOLD_FLAGS_NOMIX_ASCII;                                            \
1471         /* FALLTHROUGH */                                                          \
1472     case trie_latin_utf8_fold:                                                      \
1473         if ( foldlen>0 ) {                                                          \
1474             uvc = utf8n_to_uvchr( (const U8*) uscan, UTF8_MAXLEN, &len, uniflags ); \
1475             foldlen -= len;                                                         \
1476             uscan += len;                                                           \
1477             len=0;                                                                  \
1478         } else {                                                                    \
1479             len = 1;                                                                \
1480             uvc = _to_fold_latin1( (U8) *uc, foldbuf, &foldlen, flags);             \
1481             skiplen = UNISKIP( uvc );                                               \
1482             foldlen -= skiplen;                                                     \
1483             uscan = foldbuf + skiplen;                                              \
1484         }                                                                           \
1485         break;                                                                      \
1486     case trie_utf8:                                                                 \
1487         uvc = utf8n_to_uvchr( (const U8*) uc, UTF8_MAXLEN, &len, uniflags );        \
1488         break;                                                                      \
1489     case trie_plain:                                                                \
1490         uvc = (UV)*uc;                                                              \
1491         len = 1;                                                                    \
1492     }                                                                               \
1493     if (uvc < 256) {                                                                \
1494         charid = trie->charmap[ uvc ];                                              \
1495     }                                                                               \
1496     else {                                                                          \
1497         charid = 0;                                                                 \
1498         if (widecharmap) {                                                          \
1499             SV** const svpp = hv_fetch(widecharmap,                                 \
1500                         (char*)&uvc, sizeof(UV), 0);                                \
1501             if (svpp)                                                               \
1502                 charid = (U16)SvIV(*svpp);                                          \
1503         }                                                                           \
1504     }                                                                               \
1505 } STMT_END
1506
1507 #define REXEC_FBC_EXACTISH_SCAN(CoNd)                     \
1508 STMT_START {                                              \
1509     while (s <= e) {                                      \
1510         if ( (CoNd)                                       \
1511              && (ln == 1 || folder(s, pat_string, ln))    \
1512              && (reginfo->intuit || regtry(reginfo, &s)) )\
1513             goto got_it;                                  \
1514         s++;                                              \
1515     }                                                     \
1516 } STMT_END
1517
1518 #define REXEC_FBC_UTF8_SCAN(CoDe)                     \
1519 STMT_START {                                          \
1520     while (s < strend) {                              \
1521         CoDe                                          \
1522         s += UTF8SKIP(s);                             \
1523     }                                                 \
1524 } STMT_END
1525
1526 #define REXEC_FBC_SCAN(CoDe)                          \
1527 STMT_START {                                          \
1528     while (s < strend) {                              \
1529         CoDe                                          \
1530         s++;                                          \
1531     }                                                 \
1532 } STMT_END
1533
1534 #define REXEC_FBC_UTF8_CLASS_SCAN(CoNd)               \
1535 REXEC_FBC_UTF8_SCAN(                                  \
1536     if (CoNd) {                                       \
1537         if (tmp && (reginfo->intuit || regtry(reginfo, &s))) \
1538             goto got_it;                              \
1539         else                                          \
1540             tmp = doevery;                            \
1541     }                                                 \
1542     else                                              \
1543         tmp = 1;                                      \
1544 )
1545
1546 #define REXEC_FBC_CLASS_SCAN(CoNd)                    \
1547 REXEC_FBC_SCAN(                                       \
1548     if (CoNd) {                                       \
1549         if (tmp && (reginfo->intuit || regtry(reginfo, &s)))  \
1550             goto got_it;                              \
1551         else                                          \
1552             tmp = doevery;                            \
1553     }                                                 \
1554     else                                              \
1555         tmp = 1;                                      \
1556 )
1557
1558 #define REXEC_FBC_TRYIT                       \
1559 if ((reginfo->intuit || regtry(reginfo, &s))) \
1560     goto got_it
1561
1562 #define REXEC_FBC_CSCAN(CoNdUtF8,CoNd)                         \
1563     if (utf8_target) {                                         \
1564         REXEC_FBC_UTF8_CLASS_SCAN(CoNdUtF8);                   \
1565     }                                                          \
1566     else {                                                     \
1567         REXEC_FBC_CLASS_SCAN(CoNd);                            \
1568     }
1569     
1570 #define DUMP_EXEC_POS(li,s,doutf8)                          \
1571     dump_exec_pos(li,s,(reginfo->strend),(reginfo->strbeg), \
1572                 startpos, doutf8)
1573
1574
1575 #define UTF8_NOLOAD(TEST_NON_UTF8, IF_SUCCESS, IF_FAIL)                        \
1576         tmp = (s != reginfo->strbeg) ? UCHARAT(s - 1) : '\n';                  \
1577         tmp = TEST_NON_UTF8(tmp);                                              \
1578         REXEC_FBC_UTF8_SCAN(                                                   \
1579             if (tmp == ! TEST_NON_UTF8((U8) *s)) {                             \
1580                 tmp = !tmp;                                                    \
1581                 IF_SUCCESS;                                                    \
1582             }                                                                  \
1583             else {                                                             \
1584                 IF_FAIL;                                                       \
1585             }                                                                  \
1586         );                                                                     \
1587
1588 #define UTF8_LOAD(TeSt1_UtF8, TeSt2_UtF8, IF_SUCCESS, IF_FAIL)                 \
1589         if (s == reginfo->strbeg) {                                            \
1590             tmp = '\n';                                                        \
1591         }                                                                      \
1592         else {                                                                 \
1593             U8 * const r = reghop3((U8*)s, -1, (U8*)reginfo->strbeg);          \
1594             tmp = utf8n_to_uvchr(r, (U8*) reginfo->strend - r,                 \
1595                                                        0, UTF8_ALLOW_DEFAULT); \
1596         }                                                                      \
1597         tmp = TeSt1_UtF8;                                                      \
1598         LOAD_UTF8_CHARCLASS_ALNUM();                                           \
1599         REXEC_FBC_UTF8_SCAN(                                                   \
1600             if (tmp == ! (TeSt2_UtF8)) {                                       \
1601                 tmp = !tmp;                                                    \
1602                 IF_SUCCESS;                                                    \
1603             }                                                                  \
1604             else {                                                             \
1605                 IF_FAIL;                                                       \
1606             }                                                                  \
1607         );                                                                     \
1608
1609 /* The only difference between the BOUND and NBOUND cases is that
1610  * REXEC_FBC_TRYIT is called when matched in BOUND, and when non-matched in
1611  * NBOUND.  This is accomplished by passing it in either the if or else clause,
1612  * with the other one being empty */
1613 #define FBC_BOUND(TEST_NON_UTF8, TEST1_UTF8, TEST2_UTF8) \
1614     FBC_BOUND_COMMON(UTF8_LOAD(TEST1_UTF8, TEST2_UTF8, REXEC_FBC_TRYIT, PLACEHOLDER), TEST_NON_UTF8, REXEC_FBC_TRYIT, PLACEHOLDER)
1615
1616 #define FBC_BOUND_NOLOAD(TEST_NON_UTF8, TEST1_UTF8, TEST2_UTF8) \
1617     FBC_BOUND_COMMON(UTF8_NOLOAD(TEST_NON_UTF8, REXEC_FBC_TRYIT, PLACEHOLDER), TEST_NON_UTF8, REXEC_FBC_TRYIT, PLACEHOLDER)
1618
1619 #define FBC_NBOUND(TEST_NON_UTF8, TEST1_UTF8, TEST2_UTF8) \
1620     FBC_BOUND_COMMON(UTF8_LOAD(TEST1_UTF8, TEST2_UTF8, PLACEHOLDER, REXEC_FBC_TRYIT), TEST_NON_UTF8, PLACEHOLDER, REXEC_FBC_TRYIT)
1621
1622 #define FBC_NBOUND_NOLOAD(TEST_NON_UTF8, TEST1_UTF8, TEST2_UTF8) \
1623     FBC_BOUND_COMMON(UTF8_NOLOAD(TEST_NON_UTF8, PLACEHOLDER, REXEC_FBC_TRYIT), TEST_NON_UTF8, PLACEHOLDER, REXEC_FBC_TRYIT)
1624
1625
1626 /* Common to the BOUND and NBOUND cases.  Unfortunately the UTF8 tests need to
1627  * be passed in completely with the variable name being tested, which isn't
1628  * such a clean interface, but this is easier to read than it was before.  We
1629  * are looking for the boundary (or non-boundary between a word and non-word
1630  * character.  The utf8 and non-utf8 cases have the same logic, but the details
1631  * must be different.  Find the "wordness" of the character just prior to this
1632  * one, and compare it with the wordness of this one.  If they differ, we have
1633  * a boundary.  At the beginning of the string, pretend that the previous
1634  * character was a new-line */
1635 #define FBC_BOUND_COMMON(UTF8_CODE, TEST_NON_UTF8, IF_SUCCESS, IF_FAIL)        \
1636     if (utf8_target) {                                                         \
1637                 UTF8_CODE                                                      \
1638     }                                                                          \
1639     else {  /* Not utf8 */                                                     \
1640         tmp = (s != reginfo->strbeg) ? UCHARAT(s - 1) : '\n';                  \
1641         tmp = TEST_NON_UTF8(tmp);                                              \
1642         REXEC_FBC_SCAN(                                                        \
1643             if (tmp == ! TEST_NON_UTF8((U8) *s)) {                             \
1644                 tmp = !tmp;                                                    \
1645                 IF_SUCCESS;                                                    \
1646             }                                                                  \
1647             else {                                                             \
1648                 IF_FAIL;                                                       \
1649             }                                                                  \
1650         );                                                                     \
1651     }                                                                          \
1652     if ((!prog->minlen && tmp) && (reginfo->intuit || regtry(reginfo, &s)))    \
1653         goto got_it;
1654
1655 /* We know what class REx starts with.  Try to find this position... */
1656 /* if reginfo->intuit, its a dryrun */
1657 /* annoyingly all the vars in this routine have different names from their counterparts
1658    in regmatch. /grrr */
1659
1660 STATIC char *
1661 S_find_byclass(pTHX_ regexp * prog, const regnode *c, char *s, 
1662     const char *strend, regmatch_info *reginfo)
1663 {
1664     dVAR;
1665     const I32 doevery = (prog->intflags & PREGf_SKIP) == 0;
1666     char *pat_string;   /* The pattern's exactish string */
1667     char *pat_end;          /* ptr to end char of pat_string */
1668     re_fold_t folder;   /* Function for computing non-utf8 folds */
1669     const U8 *fold_array;   /* array for folding ords < 256 */
1670     STRLEN ln;
1671     STRLEN lnc;
1672     U8 c1;
1673     U8 c2;
1674     char *e;
1675     I32 tmp = 1;        /* Scratch variable? */
1676     const bool utf8_target = reginfo->is_utf8_target;
1677     UV utf8_fold_flags = 0;
1678     const bool is_utf8_pat = reginfo->is_utf8_pat;
1679     bool to_complement = FALSE; /* Invert the result?  Taking the xor of this
1680                                    with a result inverts that result, as 0^1 =
1681                                    1 and 1^1 = 0 */
1682     _char_class_number classnum;
1683
1684     RXi_GET_DECL(prog,progi);
1685
1686     PERL_ARGS_ASSERT_FIND_BYCLASS;
1687
1688     /* We know what class it must start with. */
1689     switch (OP(c)) {
1690     case ANYOF:
1691         if (utf8_target) {
1692             REXEC_FBC_UTF8_CLASS_SCAN(
1693                       reginclass(prog, c, (U8*)s, (U8*) strend, utf8_target));
1694         }
1695         else {
1696             REXEC_FBC_CLASS_SCAN(REGINCLASS(prog, c, (U8*)s));
1697         }
1698         break;
1699     case CANY:
1700         REXEC_FBC_SCAN(
1701             if (tmp && (reginfo->intuit || regtry(reginfo, &s)))
1702                 goto got_it;
1703             else
1704                 tmp = doevery;
1705         );
1706         break;
1707
1708     case EXACTFA_NO_TRIE:   /* This node only generated for non-utf8 patterns */
1709         assert(! is_utf8_pat);
1710         /* FALLTHROUGH */
1711     case EXACTFA:
1712         if (is_utf8_pat || utf8_target) {
1713             utf8_fold_flags = FOLDEQ_UTF8_NOMIX_ASCII;
1714             goto do_exactf_utf8;
1715         }
1716         fold_array = PL_fold_latin1;    /* Latin1 folds are not affected by */
1717         folder = foldEQ_latin1;         /* /a, except the sharp s one which */
1718         goto do_exactf_non_utf8;        /* isn't dealt with by these */
1719
1720     case EXACTF:   /* This node only generated for non-utf8 patterns */
1721         assert(! is_utf8_pat);
1722         if (utf8_target) {
1723             utf8_fold_flags = 0;
1724             goto do_exactf_utf8;
1725         }
1726         fold_array = PL_fold;
1727         folder = foldEQ;
1728         goto do_exactf_non_utf8;
1729
1730     case EXACTFL:
1731         if (is_utf8_pat || utf8_target || IN_UTF8_CTYPE_LOCALE) {
1732             utf8_fold_flags = FOLDEQ_LOCALE;
1733             goto do_exactf_utf8;
1734         }
1735         fold_array = PL_fold_locale;
1736         folder = foldEQ_locale;
1737         goto do_exactf_non_utf8;
1738
1739     case EXACTFU_SS:
1740         if (is_utf8_pat) {
1741             utf8_fold_flags = FOLDEQ_S2_ALREADY_FOLDED;
1742         }
1743         goto do_exactf_utf8;
1744
1745     case EXACTFU:
1746         if (is_utf8_pat || utf8_target) {
1747             utf8_fold_flags = is_utf8_pat ? FOLDEQ_S2_ALREADY_FOLDED : 0;
1748             goto do_exactf_utf8;
1749         }
1750
1751         /* Any 'ss' in the pattern should have been replaced by regcomp,
1752          * so we don't have to worry here about this single special case
1753          * in the Latin1 range */
1754         fold_array = PL_fold_latin1;
1755         folder = foldEQ_latin1;
1756
1757         /* FALLTHROUGH */
1758
1759     do_exactf_non_utf8: /* Neither pattern nor string are UTF8, and there
1760                            are no glitches with fold-length differences
1761                            between the target string and pattern */
1762
1763         /* The idea in the non-utf8 EXACTF* cases is to first find the
1764          * first character of the EXACTF* node and then, if necessary,
1765          * case-insensitively compare the full text of the node.  c1 is the
1766          * first character.  c2 is its fold.  This logic will not work for
1767          * Unicode semantics and the german sharp ss, which hence should
1768          * not be compiled into a node that gets here. */
1769         pat_string = STRING(c);
1770         ln  = STR_LEN(c);       /* length to match in octets/bytes */
1771
1772         /* We know that we have to match at least 'ln' bytes (which is the
1773          * same as characters, since not utf8).  If we have to match 3
1774          * characters, and there are only 2 availabe, we know without
1775          * trying that it will fail; so don't start a match past the
1776          * required minimum number from the far end */
1777         e = HOP3c(strend, -((SSize_t)ln), s);
1778
1779         if (reginfo->intuit && e < s) {
1780             e = s;                      /* Due to minlen logic of intuit() */
1781         }
1782
1783         c1 = *pat_string;
1784         c2 = fold_array[c1];
1785         if (c1 == c2) { /* If char and fold are the same */
1786             REXEC_FBC_EXACTISH_SCAN(*(U8*)s == c1);
1787         }
1788         else {
1789             REXEC_FBC_EXACTISH_SCAN(*(U8*)s == c1 || *(U8*)s == c2);
1790         }
1791         break;
1792
1793     do_exactf_utf8:
1794     {
1795         unsigned expansion;
1796
1797         /* If one of the operands is in utf8, we can't use the simpler folding
1798          * above, due to the fact that many different characters can have the
1799          * same fold, or portion of a fold, or different- length fold */
1800         pat_string = STRING(c);
1801         ln  = STR_LEN(c);       /* length to match in octets/bytes */
1802         pat_end = pat_string + ln;
1803         lnc = is_utf8_pat       /* length to match in characters */
1804                 ? utf8_length((U8 *) pat_string, (U8 *) pat_end)
1805                 : ln;
1806
1807         /* We have 'lnc' characters to match in the pattern, but because of
1808          * multi-character folding, each character in the target can match
1809          * up to 3 characters (Unicode guarantees it will never exceed
1810          * this) if it is utf8-encoded; and up to 2 if not (based on the
1811          * fact that the Latin 1 folds are already determined, and the
1812          * only multi-char fold in that range is the sharp-s folding to
1813          * 'ss'.  Thus, a pattern character can match as little as 1/3 of a
1814          * string character.  Adjust lnc accordingly, rounding up, so that
1815          * if we need to match at least 4+1/3 chars, that really is 5. */
1816         expansion = (utf8_target) ? UTF8_MAX_FOLD_CHAR_EXPAND : 2;
1817         lnc = (lnc + expansion - 1) / expansion;
1818
1819         /* As in the non-UTF8 case, if we have to match 3 characters, and
1820          * only 2 are left, it's guaranteed to fail, so don't start a
1821          * match that would require us to go beyond the end of the string
1822          */
1823         e = HOP3c(strend, -((SSize_t)lnc), s);
1824
1825         if (reginfo->intuit && e < s) {
1826             e = s;                      /* Due to minlen logic of intuit() */
1827         }
1828
1829         /* XXX Note that we could recalculate e to stop the loop earlier,
1830          * as the worst case expansion above will rarely be met, and as we
1831          * go along we would usually find that e moves further to the left.
1832          * This would happen only after we reached the point in the loop
1833          * where if there were no expansion we should fail.  Unclear if
1834          * worth the expense */
1835
1836         while (s <= e) {
1837             char *my_strend= (char *)strend;
1838             if (foldEQ_utf8_flags(s, &my_strend, 0,  utf8_target,
1839                   pat_string, NULL, ln, is_utf8_pat, utf8_fold_flags)
1840                 && (reginfo->intuit || regtry(reginfo, &s)) )
1841             {
1842                 goto got_it;
1843             }
1844             s += (utf8_target) ? UTF8SKIP(s) : 1;
1845         }
1846         break;
1847     }
1848     case BOUNDL:
1849         FBC_BOUND(isWORDCHAR_LC,
1850                   isWORDCHAR_LC_uvchr(tmp),
1851                   isWORDCHAR_LC_utf8((U8*)s));
1852         break;
1853     case NBOUNDL:
1854         FBC_NBOUND(isWORDCHAR_LC,
1855                    isWORDCHAR_LC_uvchr(tmp),
1856                    isWORDCHAR_LC_utf8((U8*)s));
1857         break;
1858     case BOUND:
1859         FBC_BOUND(isWORDCHAR,
1860                   isWORDCHAR_uni(tmp),
1861                   cBOOL(swash_fetch(PL_utf8_swash_ptrs[_CC_WORDCHAR], (U8*)s, utf8_target)));
1862         break;
1863     case BOUNDA:
1864         FBC_BOUND_NOLOAD(isWORDCHAR_A,
1865                          isWORDCHAR_A(tmp),
1866                          isWORDCHAR_A((U8*)s));
1867         break;
1868     case NBOUND:
1869         FBC_NBOUND(isWORDCHAR,
1870                    isWORDCHAR_uni(tmp),
1871                    cBOOL(swash_fetch(PL_utf8_swash_ptrs[_CC_WORDCHAR], (U8*)s, utf8_target)));
1872         break;
1873     case NBOUNDA:
1874         FBC_NBOUND_NOLOAD(isWORDCHAR_A,
1875                           isWORDCHAR_A(tmp),
1876                           isWORDCHAR_A((U8*)s));
1877         break;
1878     case BOUNDU:
1879         FBC_BOUND(isWORDCHAR_L1,
1880                   isWORDCHAR_uni(tmp),
1881                   cBOOL(swash_fetch(PL_utf8_swash_ptrs[_CC_WORDCHAR], (U8*)s, utf8_target)));
1882         break;
1883     case NBOUNDU:
1884         FBC_NBOUND(isWORDCHAR_L1,
1885                    isWORDCHAR_uni(tmp),
1886                    cBOOL(swash_fetch(PL_utf8_swash_ptrs[_CC_WORDCHAR], (U8*)s, utf8_target)));
1887         break;
1888     case LNBREAK:
1889         REXEC_FBC_CSCAN(is_LNBREAK_utf8_safe(s, strend),
1890                         is_LNBREAK_latin1_safe(s, strend)
1891         );
1892         break;
1893
1894     /* The argument to all the POSIX node types is the class number to pass to
1895      * _generic_isCC() to build a mask for searching in PL_charclass[] */
1896
1897     case NPOSIXL:
1898         to_complement = 1;
1899         /* FALLTHROUGH */
1900
1901     case POSIXL:
1902         REXEC_FBC_CSCAN(to_complement ^ cBOOL(isFOO_utf8_lc(FLAGS(c), (U8 *) s)),
1903                         to_complement ^ cBOOL(isFOO_lc(FLAGS(c), *s)));
1904         break;
1905
1906     case NPOSIXD:
1907         to_complement = 1;
1908         /* FALLTHROUGH */
1909
1910     case POSIXD:
1911         if (utf8_target) {
1912             goto posix_utf8;
1913         }
1914         goto posixa;
1915
1916     case NPOSIXA:
1917         if (utf8_target) {
1918             /* The complement of something that matches only ASCII matches all
1919              * non-ASCII, plus everything in ASCII that isn't in the class. */
1920             REXEC_FBC_UTF8_CLASS_SCAN(! isASCII_utf8(s)
1921                                       || ! _generic_isCC_A(*s, FLAGS(c)));
1922             break;
1923         }
1924
1925         to_complement = 1;
1926         /* FALLTHROUGH */
1927
1928     case POSIXA:
1929       posixa:
1930         /* Don't need to worry about utf8, as it can match only a single
1931          * byte invariant character. */
1932         REXEC_FBC_CLASS_SCAN(
1933                         to_complement ^ cBOOL(_generic_isCC_A(*s, FLAGS(c))));
1934         break;
1935
1936     case NPOSIXU:
1937         to_complement = 1;
1938         /* FALLTHROUGH */
1939
1940     case POSIXU:
1941         if (! utf8_target) {
1942             REXEC_FBC_CLASS_SCAN(to_complement ^ cBOOL(_generic_isCC(*s,
1943                                                                     FLAGS(c))));
1944         }
1945         else {
1946
1947       posix_utf8:
1948             classnum = (_char_class_number) FLAGS(c);
1949             if (classnum < _FIRST_NON_SWASH_CC) {
1950                 while (s < strend) {
1951
1952                     /* We avoid loading in the swash as long as possible, but
1953                      * should we have to, we jump to a separate loop.  This
1954                      * extra 'if' statement is what keeps this code from being
1955                      * just a call to REXEC_FBC_UTF8_CLASS_SCAN() */
1956                     if (UTF8_IS_ABOVE_LATIN1(*s)) {
1957                         goto found_above_latin1;
1958                     }
1959                     if ((UTF8_IS_INVARIANT(*s)
1960                          && to_complement ^ cBOOL(_generic_isCC((U8) *s,
1961                                                                 classnum)))
1962                         || (UTF8_IS_DOWNGRADEABLE_START(*s)
1963                             && to_complement ^ cBOOL(
1964                                 _generic_isCC(TWO_BYTE_UTF8_TO_NATIVE(*s,
1965                                                                       *(s + 1)),
1966                                               classnum))))
1967                     {
1968                         if (tmp && (reginfo->intuit || regtry(reginfo, &s)))
1969                             goto got_it;
1970                         else {
1971                             tmp = doevery;
1972                         }
1973                     }
1974                     else {
1975                         tmp = 1;
1976                     }
1977                     s += UTF8SKIP(s);
1978                 }
1979             }
1980             else switch (classnum) {    /* These classes are implemented as
1981                                            macros */
1982                 case _CC_ENUM_SPACE: /* XXX would require separate code if we
1983                                         revert the change of \v matching this */
1984                     /* FALLTHROUGH */
1985
1986                 case _CC_ENUM_PSXSPC:
1987                     REXEC_FBC_UTF8_CLASS_SCAN(
1988                                         to_complement ^ cBOOL(isSPACE_utf8(s)));
1989                     break;
1990
1991                 case _CC_ENUM_BLANK:
1992                     REXEC_FBC_UTF8_CLASS_SCAN(
1993                                         to_complement ^ cBOOL(isBLANK_utf8(s)));
1994                     break;
1995
1996                 case _CC_ENUM_XDIGIT:
1997                     REXEC_FBC_UTF8_CLASS_SCAN(
1998                                        to_complement ^ cBOOL(isXDIGIT_utf8(s)));
1999                     break;
2000
2001                 case _CC_ENUM_VERTSPACE:
2002                     REXEC_FBC_UTF8_CLASS_SCAN(
2003                                        to_complement ^ cBOOL(isVERTWS_utf8(s)));
2004                     break;
2005
2006                 case _CC_ENUM_CNTRL:
2007                     REXEC_FBC_UTF8_CLASS_SCAN(
2008                                         to_complement ^ cBOOL(isCNTRL_utf8(s)));
2009                     break;
2010
2011                 default:
2012                     Perl_croak(aTHX_ "panic: find_byclass() node %d='%s' has an unexpected character class '%d'", OP(c), PL_reg_name[OP(c)], classnum);
2013                     assert(0); /* NOTREACHED */
2014             }
2015         }
2016         break;
2017
2018       found_above_latin1:   /* Here we have to load a swash to get the result
2019                                for the current code point */
2020         if (! PL_utf8_swash_ptrs[classnum]) {
2021             U8 flags = _CORE_SWASH_INIT_ACCEPT_INVLIST;
2022             PL_utf8_swash_ptrs[classnum] =
2023                     _core_swash_init("utf8",
2024                                      "",
2025                                      &PL_sv_undef, 1, 0,
2026                                      PL_XPosix_ptrs[classnum], &flags);
2027         }
2028
2029         /* This is a copy of the loop above for swash classes, though using the
2030          * FBC macro instead of being expanded out.  Since we've loaded the
2031          * swash, we don't have to check for that each time through the loop */
2032         REXEC_FBC_UTF8_CLASS_SCAN(
2033                 to_complement ^ cBOOL(_generic_utf8(
2034                                       classnum,
2035                                       s,
2036                                       swash_fetch(PL_utf8_swash_ptrs[classnum],
2037                                                   (U8 *) s, TRUE))));
2038         break;
2039
2040     case AHOCORASICKC:
2041     case AHOCORASICK:
2042         {
2043             DECL_TRIE_TYPE(c);
2044             /* what trie are we using right now */
2045             reg_ac_data *aho = (reg_ac_data*)progi->data->data[ ARG( c ) ];
2046             reg_trie_data *trie = (reg_trie_data*)progi->data->data[ aho->trie ];
2047             HV *widecharmap = MUTABLE_HV(progi->data->data[ aho->trie + 1 ]);
2048
2049             const char *last_start = strend - trie->minlen;
2050 #ifdef DEBUGGING
2051             const char *real_start = s;
2052 #endif
2053             STRLEN maxlen = trie->maxlen;
2054             SV *sv_points;
2055             U8 **points; /* map of where we were in the input string
2056                             when reading a given char. For ASCII this
2057                             is unnecessary overhead as the relationship
2058                             is always 1:1, but for Unicode, especially
2059                             case folded Unicode this is not true. */
2060             U8 foldbuf[ UTF8_MAXBYTES_CASE + 1 ];
2061             U8 *bitmap=NULL;
2062
2063
2064             GET_RE_DEBUG_FLAGS_DECL;
2065
2066             /* We can't just allocate points here. We need to wrap it in
2067              * an SV so it gets freed properly if there is a croak while
2068              * running the match */
2069             ENTER;
2070             SAVETMPS;
2071             sv_points=newSV(maxlen * sizeof(U8 *));
2072             SvCUR_set(sv_points,
2073                 maxlen * sizeof(U8 *));
2074             SvPOK_on(sv_points);
2075             sv_2mortal(sv_points);
2076             points=(U8**)SvPV_nolen(sv_points );
2077             if ( trie_type != trie_utf8_fold
2078                  && (trie->bitmap || OP(c)==AHOCORASICKC) )
2079             {
2080                 if (trie->bitmap)
2081                     bitmap=(U8*)trie->bitmap;
2082                 else
2083                     bitmap=(U8*)ANYOF_BITMAP(c);
2084             }
2085             /* this is the Aho-Corasick algorithm modified a touch
2086                to include special handling for long "unknown char" sequences.
2087                The basic idea being that we use AC as long as we are dealing
2088                with a possible matching char, when we encounter an unknown char
2089                (and we have not encountered an accepting state) we scan forward
2090                until we find a legal starting char.
2091                AC matching is basically that of trie matching, except that when
2092                we encounter a failing transition, we fall back to the current
2093                states "fail state", and try the current char again, a process
2094                we repeat until we reach the root state, state 1, or a legal
2095                transition. If we fail on the root state then we can either
2096                terminate if we have reached an accepting state previously, or
2097                restart the entire process from the beginning if we have not.
2098
2099              */
2100             while (s <= last_start) {
2101                 const U32 uniflags = UTF8_ALLOW_DEFAULT;
2102                 U8 *uc = (U8*)s;
2103                 U16 charid = 0;
2104                 U32 base = 1;
2105                 U32 state = 1;
2106                 UV uvc = 0;
2107                 STRLEN len = 0;
2108                 STRLEN foldlen = 0;
2109                 U8 *uscan = (U8*)NULL;
2110                 U8 *leftmost = NULL;
2111 #ifdef DEBUGGING
2112                 U32 accepted_word= 0;
2113 #endif
2114                 U32 pointpos = 0;
2115
2116                 while ( state && uc <= (U8*)strend ) {
2117                     int failed=0;
2118                     U32 word = aho->states[ state ].wordnum;
2119
2120                     if( state==1 ) {
2121                         if ( bitmap ) {
2122                             DEBUG_TRIE_EXECUTE_r(
2123                                 if ( uc <= (U8*)last_start && !BITMAP_TEST(bitmap,*uc) ) {
2124                                     dump_exec_pos( (char *)uc, c, strend, real_start,
2125                                         (char *)uc, utf8_target );
2126                                     PerlIO_printf( Perl_debug_log,
2127                                         " Scanning for legal start char...\n");
2128                                 }
2129                             );
2130                             if (utf8_target) {
2131                                 while ( uc <= (U8*)last_start && !BITMAP_TEST(bitmap,*uc) ) {
2132                                     uc += UTF8SKIP(uc);
2133                                 }
2134                             } else {
2135                                 while ( uc <= (U8*)last_start  && !BITMAP_TEST(bitmap,*uc) ) {
2136                                     uc++;
2137                                 }
2138                             }
2139                             s= (char *)uc;
2140                         }
2141                         if (uc >(U8*)last_start) break;
2142                     }
2143
2144                     if ( word ) {
2145                         U8 *lpos= points[ (pointpos - trie->wordinfo[word].len) % maxlen ];
2146                         if (!leftmost || lpos < leftmost) {
2147                             DEBUG_r(accepted_word=word);
2148                             leftmost= lpos;
2149                         }
2150                         if (base==0) break;
2151
2152                     }
2153                     points[pointpos++ % maxlen]= uc;
2154                     if (foldlen || uc < (U8*)strend) {
2155                         REXEC_TRIE_READ_CHAR(trie_type, trie,
2156                                          widecharmap, uc,
2157                                          uscan, len, uvc, charid, foldlen,
2158                                          foldbuf, uniflags);
2159                         DEBUG_TRIE_EXECUTE_r({
2160                             dump_exec_pos( (char *)uc, c, strend,
2161                                         real_start, s, utf8_target);
2162                             PerlIO_printf(Perl_debug_log,
2163                                 " Charid:%3u CP:%4"UVxf" ",
2164                                  charid, uvc);
2165                         });
2166                     }
2167                     else {
2168                         len = 0;
2169                         charid = 0;
2170                     }
2171
2172
2173                     do {
2174 #ifdef DEBUGGING
2175                         word = aho->states[ state ].wordnum;
2176 #endif
2177                         base = aho->states[ state ].trans.base;
2178
2179                         DEBUG_TRIE_EXECUTE_r({
2180                             if (failed)
2181                                 dump_exec_pos( (char *)uc, c, strend, real_start,
2182                                     s,   utf8_target );
2183                             PerlIO_printf( Perl_debug_log,
2184                                 "%sState: %4"UVxf", word=%"UVxf,
2185                                 failed ? " Fail transition to " : "",
2186                                 (UV)state, (UV)word);
2187                         });
2188                         if ( base ) {
2189                             U32 tmp;
2190                             I32 offset;
2191                             if (charid &&
2192                                  ( ((offset = base + charid
2193                                     - 1 - trie->uniquecharcount)) >= 0)
2194                                  && ((U32)offset < trie->lasttrans)
2195                                  && trie->trans[offset].check == state
2196                                  && (tmp=trie->trans[offset].next))
2197                             {
2198                                 DEBUG_TRIE_EXECUTE_r(
2199                                     PerlIO_printf( Perl_debug_log," - legal\n"));
2200                                 state = tmp;
2201                                 break;
2202                             }
2203                             else {
2204                                 DEBUG_TRIE_EXECUTE_r(
2205                                     PerlIO_printf( Perl_debug_log," - fail\n"));
2206                                 failed = 1;
2207                                 state = aho->fail[state];
2208                             }
2209                         }
2210                         else {
2211                             /* we must be accepting here */
2212                             DEBUG_TRIE_EXECUTE_r(
2213                                     PerlIO_printf( Perl_debug_log," - accepting\n"));
2214                             failed = 1;
2215                             break;
2216                         }
2217                     } while(state);
2218                     uc += len;
2219                     if (failed) {
2220                         if (leftmost)
2221                             break;
2222                         if (!state) state = 1;
2223                     }
2224                 }
2225                 if ( aho->states[ state ].wordnum ) {
2226                     U8 *lpos = points[ (pointpos - trie->wordinfo[aho->states[ state ].wordnum].len) % maxlen ];
2227                     if (!leftmost || lpos < leftmost) {
2228                         DEBUG_r(accepted_word=aho->states[ state ].wordnum);
2229                         leftmost = lpos;
2230                     }
2231                 }
2232                 if (leftmost) {
2233                     s = (char*)leftmost;
2234                     DEBUG_TRIE_EXECUTE_r({
2235                         PerlIO_printf(
2236                             Perl_debug_log,"Matches word #%"UVxf" at position %"IVdf". Trying full pattern...\n",
2237                             (UV)accepted_word, (IV)(s - real_start)
2238                         );
2239                     });
2240                     if (reginfo->intuit || regtry(reginfo, &s)) {
2241                         FREETMPS;
2242                         LEAVE;
2243                         goto got_it;
2244                     }
2245                     s = HOPc(s,1);
2246                     DEBUG_TRIE_EXECUTE_r({
2247                         PerlIO_printf( Perl_debug_log,"Pattern failed. Looking for new start point...\n");
2248                     });
2249                 } else {
2250                     DEBUG_TRIE_EXECUTE_r(
2251                         PerlIO_printf( Perl_debug_log,"No match.\n"));
2252                     break;
2253                 }
2254             }
2255             FREETMPS;
2256             LEAVE;
2257         }
2258         break;
2259     default:
2260         Perl_croak(aTHX_ "panic: unknown regstclass %d", (int)OP(c));
2261         break;
2262     }
2263     return 0;
2264   got_it:
2265     return s;
2266 }
2267
2268 /* set RX_SAVED_COPY, RX_SUBBEG etc.
2269  * flags have same meanings as with regexec_flags() */
2270
2271 static void
2272 S_reg_set_capture_string(pTHX_ REGEXP * const rx,
2273                             char *strbeg,
2274                             char *strend,
2275                             SV *sv,
2276                             U32 flags,
2277                             bool utf8_target)
2278 {
2279     struct regexp *const prog = ReANY(rx);
2280
2281     if (flags & REXEC_COPY_STR) {
2282 #ifdef PERL_ANY_COW
2283         if (SvCANCOW(sv)) {
2284             if (DEBUG_C_TEST) {
2285                 PerlIO_printf(Perl_debug_log,
2286                               "Copy on write: regexp capture, type %d\n",
2287                               (int) SvTYPE(sv));
2288             }
2289             /* Create a new COW SV to share the match string and store
2290              * in saved_copy, unless the current COW SV in saved_copy
2291              * is valid and suitable for our purpose */
2292             if ((   prog->saved_copy
2293                  && SvIsCOW(prog->saved_copy)
2294                  && SvPOKp(prog->saved_copy)
2295                  && SvIsCOW(sv)
2296                  && SvPOKp(sv)
2297                  && SvPVX(sv) == SvPVX(prog->saved_copy)))
2298             {
2299                 /* just reuse saved_copy SV */
2300                 if (RXp_MATCH_COPIED(prog)) {
2301                     Safefree(prog->subbeg);
2302                     RXp_MATCH_COPIED_off(prog);
2303                 }
2304             }
2305             else {
2306                 /* create new COW SV to share string */
2307                 RX_MATCH_COPY_FREE(rx);
2308                 prog->saved_copy = sv_setsv_cow(prog->saved_copy, sv);
2309             }
2310             prog->subbeg = (char *)SvPVX_const(prog->saved_copy);
2311             assert (SvPOKp(prog->saved_copy));
2312             prog->sublen  = strend - strbeg;
2313             prog->suboffset = 0;
2314             prog->subcoffset = 0;
2315         } else
2316 #endif
2317         {
2318             SSize_t min = 0;
2319             SSize_t max = strend - strbeg;
2320             SSize_t sublen;
2321
2322             if (    (flags & REXEC_COPY_SKIP_POST)
2323                 && !(prog->extflags & RXf_PMf_KEEPCOPY) /* //p */
2324                 && !(PL_sawampersand & SAWAMPERSAND_RIGHT)
2325             ) { /* don't copy $' part of string */
2326                 U32 n = 0;
2327                 max = -1;
2328                 /* calculate the right-most part of the string covered
2329                  * by a capture. Due to look-ahead, this may be to
2330                  * the right of $&, so we have to scan all captures */
2331                 while (n <= prog->lastparen) {
2332                     if (prog->offs[n].end > max)
2333                         max = prog->offs[n].end;
2334                     n++;
2335                 }
2336                 if (max == -1)
2337                     max = (PL_sawampersand & SAWAMPERSAND_LEFT)
2338                             ? prog->offs[0].start
2339                             : 0;
2340                 assert(max >= 0 && max <= strend - strbeg);
2341             }
2342
2343             if (    (flags & REXEC_COPY_SKIP_PRE)
2344                 && !(prog->extflags & RXf_PMf_KEEPCOPY) /* //p */
2345                 && !(PL_sawampersand & SAWAMPERSAND_LEFT)
2346             ) { /* don't copy $` part of string */
2347                 U32 n = 0;
2348                 min = max;
2349                 /* calculate the left-most part of the string covered
2350                  * by a capture. Due to look-behind, this may be to
2351                  * the left of $&, so we have to scan all captures */
2352                 while (min && n <= prog->lastparen) {
2353                     if (   prog->offs[n].start != -1
2354                         && prog->offs[n].start < min)
2355                     {
2356                         min = prog->offs[n].start;
2357                     }
2358                     n++;
2359                 }
2360                 if ((PL_sawampersand & SAWAMPERSAND_RIGHT)
2361                     && min >  prog->offs[0].end
2362                 )
2363                     min = prog->offs[0].end;
2364
2365             }
2366
2367             assert(min >= 0 && min <= max && min <= strend - strbeg);
2368             sublen = max - min;
2369
2370             if (RX_MATCH_COPIED(rx)) {
2371                 if (sublen > prog->sublen)
2372                     prog->subbeg =
2373                             (char*)saferealloc(prog->subbeg, sublen+1);
2374             }
2375             else
2376                 prog->subbeg = (char*)safemalloc(sublen+1);
2377             Copy(strbeg + min, prog->subbeg, sublen, char);
2378             prog->subbeg[sublen] = '\0';
2379             prog->suboffset = min;
2380             prog->sublen = sublen;
2381             RX_MATCH_COPIED_on(rx);
2382         }
2383         prog->subcoffset = prog->suboffset;
2384         if (prog->suboffset && utf8_target) {
2385             /* Convert byte offset to chars.
2386              * XXX ideally should only compute this if @-/@+
2387              * has been seen, a la PL_sawampersand ??? */
2388
2389             /* If there's a direct correspondence between the
2390              * string which we're matching and the original SV,
2391              * then we can use the utf8 len cache associated with
2392              * the SV. In particular, it means that under //g,
2393              * sv_pos_b2u() will use the previously cached
2394              * position to speed up working out the new length of
2395              * subcoffset, rather than counting from the start of
2396              * the string each time. This stops
2397              *   $x = "\x{100}" x 1E6; 1 while $x =~ /(.)/g;
2398              * from going quadratic */
2399             if (SvPOKp(sv) && SvPVX(sv) == strbeg)
2400                 prog->subcoffset = sv_pos_b2u_flags(sv, prog->subcoffset,
2401                                                 SV_GMAGIC|SV_CONST_RETURN);
2402             else
2403                 prog->subcoffset = utf8_length((U8*)strbeg,
2404                                     (U8*)(strbeg+prog->suboffset));
2405         }
2406     }
2407     else {
2408         RX_MATCH_COPY_FREE(rx);
2409         prog->subbeg = strbeg;
2410         prog->suboffset = 0;
2411         prog->subcoffset = 0;
2412         prog->sublen = strend - strbeg;
2413     }
2414 }
2415
2416
2417
2418
2419 /*
2420  - regexec_flags - match a regexp against a string
2421  */
2422 I32
2423 Perl_regexec_flags(pTHX_ REGEXP * const rx, char *stringarg, char *strend,
2424               char *strbeg, SSize_t minend, SV *sv, void *data, U32 flags)
2425 /* stringarg: the point in the string at which to begin matching */
2426 /* strend:    pointer to null at end of string */
2427 /* strbeg:    real beginning of string */
2428 /* minend:    end of match must be >= minend bytes after stringarg. */
2429 /* sv:        SV being matched: only used for utf8 flag, pos() etc; string
2430  *            itself is accessed via the pointers above */
2431 /* data:      May be used for some additional optimizations.
2432               Currently unused. */
2433 /* flags:     For optimizations. See REXEC_* in regexp.h */
2434
2435 {
2436     dVAR;
2437     struct regexp *const prog = ReANY(rx);
2438     char *s;
2439     regnode *c;
2440     char *startpos;
2441     SSize_t minlen;             /* must match at least this many chars */
2442     SSize_t dontbother = 0;     /* how many characters not to try at end */
2443     const bool utf8_target = cBOOL(DO_UTF8(sv));
2444     I32 multiline;
2445     RXi_GET_DECL(prog,progi);
2446     regmatch_info reginfo_buf;  /* create some info to pass to regtry etc */
2447     regmatch_info *const reginfo = &reginfo_buf;
2448     regexp_paren_pair *swap = NULL;
2449     I32 oldsave;
2450     GET_RE_DEBUG_FLAGS_DECL;
2451
2452     PERL_ARGS_ASSERT_REGEXEC_FLAGS;
2453     PERL_UNUSED_ARG(data);
2454
2455     /* Be paranoid... */
2456     if (prog == NULL || stringarg == NULL) {
2457         Perl_croak(aTHX_ "NULL regexp parameter");
2458         return 0;
2459     }
2460
2461     DEBUG_EXECUTE_r(
2462         debug_start_match(rx, utf8_target, stringarg, strend,
2463         "Matching");
2464     );
2465
2466     startpos = stringarg;
2467
2468     if (prog->intflags & PREGf_GPOS_SEEN) {
2469         MAGIC *mg;
2470
2471         /* set reginfo->ganch, the position where \G can match */
2472
2473         reginfo->ganch =
2474             (flags & REXEC_IGNOREPOS)
2475             ? stringarg /* use start pos rather than pos() */
2476             : (sv && (mg = mg_find_mglob(sv)) && mg->mg_len >= 0)
2477               /* Defined pos(): */
2478             ? strbeg + MgBYTEPOS(mg, sv, strbeg, strend-strbeg)
2479             : strbeg; /* pos() not defined; use start of string */
2480
2481         DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log,
2482             "GPOS ganch set to strbeg[%"IVdf"]\n", (IV)(reginfo->ganch - strbeg)));
2483
2484         /* in the presence of \G, we may need to start looking earlier in
2485          * the string than the suggested start point of stringarg:
2486          * if prog->gofs is set, then that's a known, fixed minimum
2487          * offset, such as
2488          * /..\G/:   gofs = 2
2489          * /ab|c\G/: gofs = 1
2490          * or if the minimum offset isn't known, then we have to go back
2491          * to the start of the string, e.g. /w+\G/
2492          */
2493
2494         if (prog->intflags & PREGf_ANCH_GPOS) {
2495             startpos  = reginfo->ganch - prog->gofs;
2496             if (startpos <
2497                 ((flags & REXEC_FAIL_ON_UNDERFLOW) ? stringarg : strbeg))
2498             {
2499                 DEBUG_r(PerlIO_printf(Perl_debug_log,
2500                         "fail: ganch-gofs before earliest possible start\n"));
2501                 return 0;
2502             }
2503         }
2504         else if (prog->gofs) {
2505             if (startpos - prog->gofs < strbeg)
2506                 startpos = strbeg;
2507             else
2508                 startpos -= prog->gofs;
2509         }
2510         else if (prog->intflags & PREGf_GPOS_FLOAT)
2511             startpos = strbeg;
2512     }
2513
2514     minlen = prog->minlen;
2515     if ((startpos + minlen) > strend || startpos < strbeg) {
2516         DEBUG_r(PerlIO_printf(Perl_debug_log,
2517                     "Regex match can't succeed, so not even tried\n"));
2518         return 0;
2519     }
2520
2521     /* at the end of this function, we'll do a LEAVE_SCOPE(oldsave),
2522      * which will call destuctors to reset PL_regmatch_state, free higher
2523      * PL_regmatch_slabs, and clean up regmatch_info_aux and
2524      * regmatch_info_aux_eval */
2525
2526     oldsave = PL_savestack_ix;
2527
2528     s = startpos;
2529
2530     if ((prog->extflags & RXf_USE_INTUIT)
2531         && !(flags & REXEC_CHECKED))
2532     {
2533         s = re_intuit_start(rx, sv, strbeg, startpos, strend,
2534                                     flags, NULL);
2535         if (!s)
2536             return 0;
2537
2538         if (prog->extflags & RXf_CHECK_ALL) {
2539             /* we can match based purely on the result of INTUIT.
2540              * Set up captures etc just for $& and $-[0]
2541              * (an intuit-only match wont have $1,$2,..) */
2542             assert(!prog->nparens);
2543
2544             /* s/// doesn't like it if $& is earlier than where we asked it to
2545              * start searching (which can happen on something like /.\G/) */
2546             if (       (flags & REXEC_FAIL_ON_UNDERFLOW)
2547                     && (s < stringarg))
2548             {
2549                 /* this should only be possible under \G */
2550                 assert(prog->intflags & PREGf_GPOS_SEEN);
2551                 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
2552                     "matched, but failing for REXEC_FAIL_ON_UNDERFLOW\n"));
2553                 goto phooey;
2554             }
2555
2556             /* match via INTUIT shouldn't have any captures.
2557              * Let @-, @+, $^N know */
2558             prog->lastparen = prog->lastcloseparen = 0;
2559             RX_MATCH_UTF8_set(rx, utf8_target);
2560             prog->offs[0].start = s - strbeg;
2561             prog->offs[0].end = utf8_target
2562                 ? (char*)utf8_hop((U8*)s, prog->minlenret) - strbeg
2563                 : s - strbeg + prog->minlenret;
2564             if ( !(flags & REXEC_NOT_FIRST) )
2565                 S_reg_set_capture_string(aTHX_ rx,
2566                                         strbeg, strend,
2567                                         sv, flags, utf8_target);
2568
2569             return 1;
2570         }
2571     }
2572
2573     multiline = prog->extflags & RXf_PMf_MULTILINE;
2574     
2575     if (strend - s < (minlen+(prog->check_offset_min<0?prog->check_offset_min:0))) {
2576         DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
2577                               "String too short [regexec_flags]...\n"));
2578         goto phooey;
2579     }
2580     
2581     /* Check validity of program. */
2582     if (UCHARAT(progi->program) != REG_MAGIC) {
2583         Perl_croak(aTHX_ "corrupted regexp program");
2584     }
2585
2586     RX_MATCH_TAINTED_off(rx);
2587
2588     reginfo->prog = rx;  /* Yes, sorry that this is confusing.  */
2589     reginfo->intuit = 0;
2590     reginfo->is_utf8_target = cBOOL(utf8_target);
2591     reginfo->is_utf8_pat = cBOOL(RX_UTF8(rx));
2592     reginfo->warned = FALSE;
2593     reginfo->strbeg  = strbeg;
2594     reginfo->sv = sv;
2595     reginfo->poscache_maxiter = 0; /* not yet started a countdown */
2596     reginfo->strend = strend;
2597     /* see how far we have to get to not match where we matched before */
2598     reginfo->till = stringarg + minend;
2599
2600     if (prog->extflags & RXf_EVAL_SEEN && SvPADTMP(sv)) {
2601         /* SAVEFREESV, not sv_mortalcopy, as this SV must last until after
2602            S_cleanup_regmatch_info_aux has executed (registered by
2603            SAVEDESTRUCTOR_X below).  S_cleanup_regmatch_info_aux modifies
2604            magic belonging to this SV.
2605            Not newSVsv, either, as it does not COW.
2606         */
2607         assert(!IS_PADGV(sv));
2608         reginfo->sv = newSV(0);
2609         SvSetSV_nosteal(reginfo->sv, sv);
2610         SAVEFREESV(reginfo->sv);
2611     }
2612
2613     /* reserve next 2 or 3 slots in PL_regmatch_state:
2614      * slot N+0: may currently be in use: skip it
2615      * slot N+1: use for regmatch_info_aux struct
2616      * slot N+2: use for regmatch_info_aux_eval struct if we have (?{})'s
2617      * slot N+3: ready for use by regmatch()
2618      */
2619
2620     {
2621         regmatch_state *old_regmatch_state;
2622         regmatch_slab  *old_regmatch_slab;
2623         int i, max = (prog->extflags & RXf_EVAL_SEEN) ? 2 : 1;
2624
2625         /* on first ever match, allocate first slab */
2626         if (!PL_regmatch_slab) {
2627             Newx(PL_regmatch_slab, 1, regmatch_slab);
2628             PL_regmatch_slab->prev = NULL;
2629             PL_regmatch_slab->next = NULL;
2630             PL_regmatch_state = SLAB_FIRST(PL_regmatch_slab);
2631         }
2632
2633         old_regmatch_state = PL_regmatch_state;
2634         old_regmatch_slab  = PL_regmatch_slab;
2635
2636         for (i=0; i <= max; i++) {
2637             if (i == 1)
2638                 reginfo->info_aux = &(PL_regmatch_state->u.info_aux);
2639             else if (i ==2)
2640                 reginfo->info_aux_eval =
2641                 reginfo->info_aux->info_aux_eval =
2642                             &(PL_regmatch_state->u.info_aux_eval);
2643
2644             if (++PL_regmatch_state >  SLAB_LAST(PL_regmatch_slab))
2645                 PL_regmatch_state = S_push_slab(aTHX);
2646         }
2647
2648         /* note initial PL_regmatch_state position; at end of match we'll
2649          * pop back to there and free any higher slabs */
2650
2651         reginfo->info_aux->old_regmatch_state = old_regmatch_state;
2652         reginfo->info_aux->old_regmatch_slab  = old_regmatch_slab;
2653         reginfo->info_aux->poscache = NULL;
2654
2655         SAVEDESTRUCTOR_X(S_cleanup_regmatch_info_aux, reginfo->info_aux);
2656
2657         if ((prog->extflags & RXf_EVAL_SEEN))
2658             S_setup_eval_state(aTHX_ reginfo);
2659         else
2660             reginfo->info_aux_eval = reginfo->info_aux->info_aux_eval = NULL;
2661     }
2662
2663     /* If there is a "must appear" string, look for it. */
2664
2665     if (PL_curpm && (PM_GETRE(PL_curpm) == rx)) {
2666         /* We have to be careful. If the previous successful match
2667            was from this regex we don't want a subsequent partially
2668            successful match to clobber the old results.
2669            So when we detect this possibility we add a swap buffer
2670            to the re, and switch the buffer each match. If we fail,
2671            we switch it back; otherwise we leave it swapped.
2672         */
2673         swap = prog->offs;
2674         /* do we need a save destructor here for eval dies? */
2675         Newxz(prog->offs, (prog->nparens + 1), regexp_paren_pair);
2676         DEBUG_BUFFERS_r(PerlIO_printf(Perl_debug_log,
2677             "rex=0x%"UVxf" saving  offs: orig=0x%"UVxf" new=0x%"UVxf"\n",
2678             PTR2UV(prog),
2679             PTR2UV(swap),
2680             PTR2UV(prog->offs)
2681         ));
2682     }
2683
2684     /* Simplest case:  anchored match need be tried only once. */
2685     /*  [unless only anchor is BOL and multiline is set] */
2686     if (prog->intflags & (PREGf_ANCH & ~PREGf_ANCH_GPOS)) {
2687         if (s == startpos && regtry(reginfo, &s))
2688             goto got_it;
2689         else if (multiline || (prog->intflags & (PREGf_IMPLICIT | PREGf_ANCH_MBOL))) /* XXXX SBOL? */
2690         {
2691             char *end;
2692
2693             if (minlen)
2694                 dontbother = minlen - 1;
2695             end = HOP3c(strend, -dontbother, strbeg) - 1;
2696             /* for multiline we only have to try after newlines */
2697             if (prog->check_substr || prog->check_utf8) {
2698                 /* because of the goto we can not easily reuse the macros for bifurcating the
2699                    unicode/non-unicode match modes here like we do elsewhere - demerphq */
2700                 if (utf8_target) {
2701                     if (s == startpos)
2702                         goto after_try_utf8;
2703                     while (1) {
2704                         if (regtry(reginfo, &s)) {
2705                             goto got_it;
2706                         }
2707                       after_try_utf8:
2708                         if (s > end) {
2709                             goto phooey;
2710                         }
2711                         if (prog->extflags & RXf_USE_INTUIT) {
2712                             s = re_intuit_start(rx, sv, strbeg,
2713                                     s + UTF8SKIP(s), strend, flags, NULL);
2714                             if (!s) {
2715                                 goto phooey;
2716                             }
2717                         }
2718                         else {
2719                             s += UTF8SKIP(s);
2720                         }
2721                     }
2722                 } /* end search for check string in unicode */
2723                 else {
2724                     if (s == startpos) {
2725                         goto after_try_latin;
2726                     }
2727                     while (1) {
2728                         if (regtry(reginfo, &s)) {
2729                             goto got_it;
2730                         }
2731                       after_try_latin:
2732                         if (s > end) {
2733                             goto phooey;
2734                         }
2735                         if (prog->extflags & RXf_USE_INTUIT) {
2736                             s = re_intuit_start(rx, sv, strbeg,
2737                                         s + 1, strend, flags, NULL);
2738                             if (!s) {
2739                                 goto phooey;
2740                             }
2741                         }
2742                         else {
2743                             s++;
2744                         }
2745                     }
2746                 } /* end search for check string in latin*/
2747             } /* end search for check string */
2748             else { /* search for newline */
2749                 if (s > startpos) {
2750                     /*XXX: The s-- is almost definitely wrong here under unicode - demeprhq*/
2751                     s--;
2752                 }
2753                 /* We can use a more efficient search as newlines are the same in unicode as they are in latin */
2754                 while (s <= end) { /* note it could be possible to match at the end of the string */
2755                     if (*s++ == '\n') { /* don't need PL_utf8skip here */
2756                         if (regtry(reginfo, &s))
2757                             goto got_it;
2758                     }
2759                 }
2760             } /* end search for newline */
2761         } /* end anchored/multiline check string search */
2762         goto phooey;
2763     } else if (prog->intflags & PREGf_ANCH_GPOS)
2764     {
2765         /* PREGf_ANCH_GPOS should never be true if PREGf_GPOS_SEEN is not true */
2766         assert(prog->intflags & PREGf_GPOS_SEEN);
2767         /* For anchored \G, the only position it can match from is
2768          * (ganch-gofs); we already set startpos to this above; if intuit
2769          * moved us on from there, we can't possibly succeed */
2770         assert(startpos == reginfo->ganch - prog->gofs);
2771         if (s == startpos && regtry(reginfo, &s))
2772             goto got_it;
2773         goto phooey;
2774     }
2775
2776     /* Messy cases:  unanchored match. */
2777     if ((prog->anchored_substr || prog->anchored_utf8) && prog->intflags & PREGf_SKIP) {
2778         /* we have /x+whatever/ */
2779         /* it must be a one character string (XXXX Except is_utf8_pat?) */
2780         char ch;
2781 #ifdef DEBUGGING
2782         int did_match = 0;
2783 #endif
2784         if (utf8_target) {
2785             if (! prog->anchored_utf8) {
2786                 to_utf8_substr(prog);
2787             }
2788             ch = SvPVX_const(prog->anchored_utf8)[0];
2789             REXEC_FBC_SCAN(
2790                 if (*s == ch) {
2791                     DEBUG_EXECUTE_r( did_match = 1 );
2792                     if (regtry(reginfo, &s)) goto got_it;
2793                     s += UTF8SKIP(s);
2794                     while (s < strend && *s == ch)
2795                         s += UTF8SKIP(s);
2796                 }
2797             );
2798
2799         }
2800         else {
2801             if (! prog->anchored_substr) {
2802                 if (! to_byte_substr(prog)) {
2803                     NON_UTF8_TARGET_BUT_UTF8_REQUIRED(phooey);
2804                 }
2805             }
2806             ch = SvPVX_const(prog->anchored_substr)[0];
2807             REXEC_FBC_SCAN(
2808                 if (*s == ch) {
2809                     DEBUG_EXECUTE_r( did_match = 1 );
2810                     if (regtry(reginfo, &s)) goto got_it;
2811                     s++;
2812                     while (s < strend && *s == ch)
2813                         s++;
2814                 }
2815             );
2816         }
2817         DEBUG_EXECUTE_r(if (!did_match)
2818                 PerlIO_printf(Perl_debug_log,
2819                                   "Did not find anchored character...\n")
2820                );
2821     }
2822     else if (prog->anchored_substr != NULL
2823               || prog->anchored_utf8 != NULL
2824               || ((prog->float_substr != NULL || prog->float_utf8 != NULL)
2825                   && prog->float_max_offset < strend - s)) {
2826         SV *must;
2827         SSize_t back_max;
2828         SSize_t back_min;
2829         char *last;
2830         char *last1;            /* Last position checked before */
2831 #ifdef DEBUGGING
2832         int did_match = 0;
2833 #endif
2834         if (prog->anchored_substr || prog->anchored_utf8) {
2835             if (utf8_target) {
2836                 if (! prog->anchored_utf8) {
2837                     to_utf8_substr(prog);
2838                 }
2839                 must = prog->anchored_utf8;
2840             }
2841             else {
2842                 if (! prog->anchored_substr) {
2843                     if (! to_byte_substr(prog)) {
2844                         NON_UTF8_TARGET_BUT_UTF8_REQUIRED(phooey);
2845                     }
2846                 }
2847                 must = prog->anchored_substr;
2848             }
2849             back_max = back_min = prog->anchored_offset;
2850         } else {
2851             if (utf8_target) {
2852                 if (! prog->float_utf8) {
2853                     to_utf8_substr(prog);
2854                 }
2855                 must = prog->float_utf8;
2856             }
2857             else {
2858                 if (! prog->float_substr) {
2859                     if (! to_byte_substr(prog)) {
2860                         NON_UTF8_TARGET_BUT_UTF8_REQUIRED(phooey);
2861                     }
2862                 }
2863                 must = prog->float_substr;
2864             }
2865             back_max = prog->float_max_offset;
2866             back_min = prog->float_min_offset;
2867         }
2868             
2869         if (back_min<0) {
2870             last = strend;
2871         } else {
2872             last = HOP3c(strend,        /* Cannot start after this */
2873                   -(SSize_t)(CHR_SVLEN(must)
2874                          - (SvTAIL(must) != 0) + back_min), strbeg);
2875         }
2876         if (s > reginfo->strbeg)
2877             last1 = HOPc(s, -1);
2878         else
2879             last1 = s - 1;      /* bogus */
2880
2881         /* XXXX check_substr already used to find "s", can optimize if
2882            check_substr==must. */
2883         dontbother = 0;
2884         strend = HOPc(strend, -dontbother);
2885         while ( (s <= last) &&
2886                 (s = fbm_instr((unsigned char*)HOP4c(s, back_min, strbeg,  strend),
2887                                   (unsigned char*)strend, must,
2888                                   multiline ? FBMrf_MULTILINE : 0)) ) {
2889             DEBUG_EXECUTE_r( did_match = 1 );
2890             if (HOPc(s, -back_max) > last1) {
2891                 last1 = HOPc(s, -back_min);
2892                 s = HOPc(s, -back_max);
2893             }
2894             else {
2895                 char * const t = (last1 >= reginfo->strbeg)
2896                                     ? HOPc(last1, 1) : last1 + 1;
2897
2898                 last1 = HOPc(s, -back_min);
2899                 s = t;
2900             }
2901             if (utf8_target) {
2902                 while (s <= last1) {
2903                     if (regtry(reginfo, &s))
2904                         goto got_it;
2905                     if (s >= last1) {
2906                         s++; /* to break out of outer loop */
2907                         break;
2908                     }
2909                     s += UTF8SKIP(s);
2910                 }
2911             }
2912             else {
2913                 while (s <= last1) {
2914                     if (regtry(reginfo, &s))
2915                         goto got_it;
2916                     s++;
2917                 }
2918             }
2919         }
2920         DEBUG_EXECUTE_r(if (!did_match) {
2921             RE_PV_QUOTED_DECL(quoted, utf8_target, PERL_DEBUG_PAD_ZERO(0),
2922                 SvPVX_const(must), RE_SV_DUMPLEN(must), 30);
2923             PerlIO_printf(Perl_debug_log, "Did not find %s substr %s%s...\n",
2924                               ((must == prog->anchored_substr || must == prog->anchored_utf8)
2925                                ? "anchored" : "floating"),
2926                 quoted, RE_SV_TAIL(must));
2927         });                 
2928         goto phooey;
2929     }
2930     else if ( (c = progi->regstclass) ) {
2931         if (minlen) {
2932             const OPCODE op = OP(progi->regstclass);
2933             /* don't bother with what can't match */
2934             if (PL_regkind[op] != EXACT && op != CANY && PL_regkind[op] != TRIE)
2935                 strend = HOPc(strend, -(minlen - 1));
2936         }
2937         DEBUG_EXECUTE_r({
2938             SV * const prop = sv_newmortal();
2939             regprop(prog, prop, c, reginfo);
2940             {
2941                 RE_PV_QUOTED_DECL(quoted,utf8_target,PERL_DEBUG_PAD_ZERO(1),
2942                     s,strend-s,60);
2943                 PerlIO_printf(Perl_debug_log,
2944                     "Matching stclass %.*s against %s (%d bytes)\n",
2945                     (int)SvCUR(prop), SvPVX_const(prop),
2946                      quoted, (int)(strend - s));
2947             }
2948         });
2949         if (find_byclass(prog, c, s, strend, reginfo))
2950             goto got_it;
2951         DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Contradicts stclass... [regexec_flags]\n"));
2952     }
2953     else {
2954         dontbother = 0;
2955         if (prog->float_substr != NULL || prog->float_utf8 != NULL) {
2956             /* Trim the end. */
2957             char *last= NULL;
2958             SV* float_real;
2959             STRLEN len;
2960             const char *little;
2961
2962             if (utf8_target) {
2963                 if (! prog->float_utf8) {
2964                     to_utf8_substr(prog);
2965                 }
2966                 float_real = prog->float_utf8;
2967             }
2968             else {
2969                 if (! prog->float_substr) {
2970                     if (! to_byte_substr(prog)) {
2971                         NON_UTF8_TARGET_BUT_UTF8_REQUIRED(phooey);
2972                     }
2973                 }
2974                 float_real = prog->float_substr;
2975             }
2976
2977             little = SvPV_const(float_real, len);
2978             if (SvTAIL(float_real)) {
2979                     /* This means that float_real contains an artificial \n on
2980                      * the end due to the presence of something like this:
2981                      * /foo$/ where we can match both "foo" and "foo\n" at the
2982                      * end of the string.  So we have to compare the end of the
2983                      * string first against the float_real without the \n and
2984                      * then against the full float_real with the string.  We
2985                      * have to watch out for cases where the string might be
2986                      * smaller than the float_real or the float_real without
2987                      * the \n. */
2988                     char *checkpos= strend - len;
2989                     DEBUG_OPTIMISE_r(
2990                         PerlIO_printf(Perl_debug_log,
2991                             "%sChecking for float_real.%s\n",
2992                             PL_colors[4], PL_colors[5]));
2993                     if (checkpos + 1 < strbeg) {
2994                         /* can't match, even if we remove the trailing \n
2995                          * string is too short to match */
2996                         DEBUG_EXECUTE_r(
2997                             PerlIO_printf(Perl_debug_log,
2998                                 "%sString shorter than required trailing substring, cannot match.%s\n",
2999                                 PL_colors[4], PL_colors[5]));
3000                         goto phooey;
3001                     } else if (memEQ(checkpos + 1, little, len - 1)) {
3002                         /* can match, the end of the string matches without the
3003                          * "\n" */
3004                         last = checkpos + 1;
3005                     } else if (checkpos < strbeg) {
3006                         /* cant match, string is too short when the "\n" is
3007                          * included */
3008                         DEBUG_EXECUTE_r(
3009                             PerlIO_printf(Perl_debug_log,
3010                                 "%sString does not contain required trailing substring, cannot match.%s\n",
3011                                 PL_colors[4], PL_colors[5]));
3012                         goto phooey;
3013                     } else if (!multiline) {
3014                         /* non multiline match, so compare with the "\n" at the
3015                          * end of the string */
3016                         if (memEQ(checkpos, little, len)) {
3017                             last= checkpos;
3018                         } else {
3019                             DEBUG_EXECUTE_r(
3020                                 PerlIO_printf(Perl_debug_log,
3021                                     "%sString does not contain required trailing substring, cannot match.%s\n",
3022                                     PL_colors[4], PL_colors[5]));
3023                             goto phooey;
3024                         }
3025                     } else {
3026                         /* multiline match, so we have to search for a place
3027                          * where the full string is located */
3028                         goto find_last;
3029                     }
3030             } else {
3031                   find_last:
3032                     if (len)
3033                         last = rninstr(s, strend, little, little + len);
3034                     else
3035                         last = strend;  /* matching "$" */
3036             }
3037             if (!last) {
3038                 /* at one point this block contained a comment which was
3039                  * probably incorrect, which said that this was a "should not
3040                  * happen" case.  Even if it was true when it was written I am
3041                  * pretty sure it is not anymore, so I have removed the comment
3042                  * and replaced it with this one. Yves */
3043                 DEBUG_EXECUTE_r(
3044                     PerlIO_printf(Perl_debug_log,
3045                         "String does not contain required substring, cannot match.\n"
3046                     ));
3047                 goto phooey;
3048             }
3049             dontbother = strend - last + prog->float_min_offset;
3050         }
3051         if (minlen && (dontbother < minlen))
3052             dontbother = minlen - 1;
3053         strend -= dontbother;              /* this one's always in bytes! */
3054         /* We don't know much -- general case. */
3055         if (utf8_target) {
3056             for (;;) {
3057                 if (regtry(reginfo, &s))
3058                     goto got_it;
3059                 if (s >= strend)
3060                     break;
3061                 s += UTF8SKIP(s);
3062             };
3063         }
3064         else {
3065             do {
3066                 if (regtry(reginfo, &s))
3067                     goto got_it;
3068             } while (s++ < strend);
3069         }
3070     }
3071
3072     /* Failure. */
3073     goto phooey;
3074
3075 got_it:
3076     /* s/// doesn't like it if $& is earlier than where we asked it to
3077      * start searching (which can happen on something like /.\G/) */
3078     if (       (flags & REXEC_FAIL_ON_UNDERFLOW)
3079             && (prog->offs[0].start < stringarg - strbeg))
3080     {
3081         /* this should only be possible under \G */
3082         assert(prog->intflags & PREGf_GPOS_SEEN);
3083         DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
3084             "matched, but failing for REXEC_FAIL_ON_UNDERFLOW\n"));
3085         goto phooey;
3086     }
3087
3088     DEBUG_BUFFERS_r(
3089         if (swap)
3090             PerlIO_printf(Perl_debug_log,
3091                 "rex=0x%"UVxf" freeing offs: 0x%"UVxf"\n",
3092                 PTR2UV(prog),
3093                 PTR2UV(swap)
3094             );
3095     );
3096     Safefree(swap);
3097
3098     /* clean up; this will trigger destructors that will free all slabs
3099      * above the current one, and cleanup the regmatch_info_aux
3100      * and regmatch_info_aux_eval sructs */
3101
3102     LEAVE_SCOPE(oldsave);
3103
3104     if (RXp_PAREN_NAMES(prog)) 
3105         (void)hv_iterinit(RXp_PAREN_NAMES(prog));
3106
3107     RX_MATCH_UTF8_set(rx, utf8_target);
3108
3109     /* make sure $`, $&, $', and $digit will work later */
3110     if ( !(flags & REXEC_NOT_FIRST) )
3111         S_reg_set_capture_string(aTHX_ rx,
3112                                     strbeg, reginfo->strend,
3113                                     sv, flags, utf8_target);
3114
3115     return 1;
3116
3117 phooey:
3118     DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch failed%s\n",
3119                           PL_colors[4], PL_colors[5]));
3120
3121     /* clean up; this will trigger destructors that will free all slabs
3122      * above the current one, and cleanup the regmatch_info_aux
3123      * and regmatch_info_aux_eval sructs */
3124
3125     LEAVE_SCOPE(oldsave);
3126
3127     if (swap) {
3128         /* we failed :-( roll it back */
3129         DEBUG_BUFFERS_r(PerlIO_printf(Perl_debug_log,
3130             "rex=0x%"UVxf" rolling back offs: freeing=0x%"UVxf" restoring=0x%"UVxf"\n",
3131             PTR2UV(prog),
3132             PTR2UV(prog->offs),
3133             PTR2UV(swap)
3134         ));
3135         Safefree(prog->offs);
3136         prog->offs = swap;
3137     }
3138     return 0;
3139 }
3140
3141
3142 /* Set which rex is pointed to by PL_reg_curpm, handling ref counting.
3143  * Do inc before dec, in case old and new rex are the same */
3144 #define SET_reg_curpm(Re2)                          \
3145     if (reginfo->info_aux_eval) {                   \
3146         (void)ReREFCNT_inc(Re2);                    \
3147         ReREFCNT_dec(PM_GETRE(PL_reg_curpm));       \
3148         PM_SETRE((PL_reg_curpm), (Re2));            \
3149     }
3150
3151
3152 /*
3153  - regtry - try match at specific point
3154  */
3155 STATIC I32                      /* 0 failure, 1 success */
3156 S_regtry(pTHX_ regmatch_info *reginfo, char **startposp)
3157 {
3158     dVAR;
3159     CHECKPOINT lastcp;
3160     REGEXP *const rx = reginfo->prog;
3161     regexp *const prog = ReANY(rx);
3162     SSize_t result;
3163     RXi_GET_DECL(prog,progi);
3164     GET_RE_DEBUG_FLAGS_DECL;
3165
3166     PERL_ARGS_ASSERT_REGTRY;
3167
3168     reginfo->cutpoint=NULL;
3169
3170     prog->offs[0].start = *startposp - reginfo->strbeg;
3171     prog->lastparen = 0;
3172     prog->lastcloseparen = 0;
3173
3174     /* XXXX What this code is doing here?!!!  There should be no need
3175        to do this again and again, prog->lastparen should take care of
3176        this!  --ilya*/
3177
3178     /* Tests pat.t#187 and split.t#{13,14} seem to depend on this code.
3179      * Actually, the code in regcppop() (which Ilya may be meaning by
3180      * prog->lastparen), is not needed at all by the test suite
3181      * (op/regexp, op/pat, op/split), but that code is needed otherwise
3182      * this erroneously leaves $1 defined: "1" =~ /^(?:(\d)x)?\d$/
3183      * Meanwhile, this code *is* needed for the
3184      * above-mentioned test suite tests to succeed.  The common theme
3185      * on those tests seems to be returning null fields from matches.
3186      * --jhi updated by dapm */
3187 #if 1
3188     if (prog->nparens) {
3189         regexp_paren_pair *pp = prog->offs;
3190         I32 i;
3191         for (i = prog->nparens; i > (I32)prog->lastparen; i--) {
3192             ++pp;
3193             pp->start = -1;
3194             pp->end = -1;
3195         }
3196     }
3197 #endif
3198     REGCP_SET(lastcp);
3199     result = regmatch(reginfo, *startposp, progi->program + 1);
3200     if (result != -1) {
3201         prog->offs[0].end = result;
3202         return 1;
3203     }
3204     if (reginfo->cutpoint)
3205         *startposp= reginfo->cutpoint;
3206     REGCP_UNWIND(lastcp);
3207     return 0;
3208 }
3209
3210
3211 #define sayYES goto yes
3212 #define sayNO goto no
3213 #define sayNO_SILENT goto no_silent
3214
3215 /* we dont use STMT_START/END here because it leads to 
3216    "unreachable code" warnings, which are bogus, but distracting. */
3217 #define CACHEsayNO \
3218     if (ST.cache_mask) \
3219        reginfo->info_aux->poscache[ST.cache_offset] |= ST.cache_mask; \
3220     sayNO
3221
3222 /* this is used to determine how far from the left messages like
3223    'failed...' are printed. It should be set such that messages 
3224    are inline with the regop output that created them.
3225 */
3226 #define REPORT_CODE_OFF 32
3227
3228
3229 #define CHRTEST_UNINIT -1001 /* c1/c2 haven't been calculated yet */
3230 #define CHRTEST_VOID   -1000 /* the c1/c2 "next char" test should be skipped */
3231 #define CHRTEST_NOT_A_CP_1 -999
3232 #define CHRTEST_NOT_A_CP_2 -998
3233
3234 /* grab a new slab and return the first slot in it */
3235
3236 STATIC regmatch_state *
3237 S_push_slab(pTHX)
3238 {
3239 #if PERL_VERSION < 9 && !defined(PERL_CORE)
3240     dMY_CXT;
3241 #endif
3242     regmatch_slab *s = PL_regmatch_slab->next;
3243     if (!s) {
3244         Newx(s, 1, regmatch_slab);
3245         s->prev = PL_regmatch_slab;
3246         s->next = NULL;
3247         PL_regmatch_slab->next = s;
3248     }
3249     PL_regmatch_slab = s;
3250     return SLAB_FIRST(s);
3251 }
3252
3253
3254 /* push a new state then goto it */
3255
3256 #define PUSH_STATE_GOTO(state, node, input) \
3257     pushinput = input; \
3258     scan = node; \
3259     st->resume_state = state; \
3260     goto push_state;
3261
3262 /* push a new state with success backtracking, then goto it */
3263
3264 #define PUSH_YES_STATE_GOTO(state, node, input) \
3265     pushinput = input; \
3266     scan = node; \
3267     st->resume_state = state; \
3268     goto push_yes_state;
3269
3270
3271
3272
3273 /*
3274
3275 regmatch() - main matching routine
3276
3277 This is basically one big switch statement in a loop. We execute an op,
3278 set 'next' to point the next op, and continue. If we come to a point which
3279 we may need to backtrack to on failure such as (A|B|C), we push a
3280 backtrack state onto the backtrack stack. On failure, we pop the top
3281 state, and re-enter the loop at the state indicated. If there are no more
3282 states to pop, we return failure.
3283
3284 Sometimes we also need to backtrack on success; for example /A+/, where
3285 after successfully matching one A, we need to go back and try to
3286 match another one; similarly for lookahead assertions: if the assertion
3287 completes successfully, we backtrack to the state just before the assertion
3288 and then carry on.  In these cases, the pushed state is marked as
3289 'backtrack on success too'. This marking is in fact done by a chain of
3290 pointers, each pointing to the previous 'yes' state. On success, we pop to
3291 the nearest yes state, discarding any intermediate failure-only states.
3292 Sometimes a yes state is pushed just to force some cleanup code to be
3293 called at the end of a successful match or submatch; e.g. (??{$re}) uses
3294 it to free the inner regex.
3295
3296 Note that failure backtracking rewinds the cursor position, while
3297 success backtracking leaves it alone.
3298
3299 A pattern is complete when the END op is executed, while a subpattern
3300 such as (?=foo) is complete when the SUCCESS op is executed. Both of these
3301 ops trigger the "pop to last yes state if any, otherwise return true"
3302 behaviour.
3303
3304 A common convention in this function is to use A and B to refer to the two
3305 subpatterns (or to the first nodes thereof) in patterns like /A*B/: so A is
3306 the subpattern to be matched possibly multiple times, while B is the entire
3307 rest of the pattern. Variable and state names reflect this convention.
3308
3309 The states in the main switch are the union of ops and failure/success of
3310 substates associated with with that op.  For example, IFMATCH is the op
3311 that does lookahead assertions /(?=A)B/ and so the IFMATCH state means
3312 'execute IFMATCH'; while IFMATCH_A is a state saying that we have just
3313 successfully matched A and IFMATCH_A_fail is a state saying that we have
3314 just failed to match A. Resume states always come in pairs. The backtrack
3315 state we push is marked as 'IFMATCH_A', but when that is popped, we resume
3316 at IFMATCH_A or IFMATCH_A_fail, depending on whether we are backtracking
3317 on success or failure.
3318
3319 The struct that holds a backtracking state is actually a big union, with
3320 one variant for each major type of op. The variable st points to the
3321 top-most backtrack struct. To make the code clearer, within each
3322 block of code we #define ST to alias the relevant union.
3323
3324 Here's a concrete example of a (vastly oversimplified) IFMATCH
3325 implementation:
3326
3327     switch (state) {
3328     ....
3329
3330 #define ST st->u.ifmatch
3331
3332     case IFMATCH: // we are executing the IFMATCH op, (?=A)B
3333         ST.foo = ...; // some state we wish to save
3334         ...
3335         // push a yes backtrack state with a resume value of
3336         // IFMATCH_A/IFMATCH_A_fail, then continue execution at the
3337         // first node of A:
3338         PUSH_YES_STATE_GOTO(IFMATCH_A, A, newinput);
3339         // NOTREACHED
3340
3341     case IFMATCH_A: // we have successfully executed A; now continue with B
3342         next = B;
3343         bar = ST.foo; // do something with the preserved value
3344         break;
3345
3346     case IFMATCH_A_fail: // A failed, so the assertion failed
3347         ...;   // do some housekeeping, then ...
3348         sayNO; // propagate the failure
3349
3350 #undef ST
3351
3352     ...
3353     }
3354
3355 For any old-timers reading this who are familiar with the old recursive
3356 approach, the code above is equivalent to:
3357
3358     case IFMATCH: // we are executing the IFMATCH op, (?=A)B
3359     {
3360         int foo = ...
3361         ...
3362         if (regmatch(A)) {
3363             next = B;
3364             bar = foo;
3365             break;
3366         }
3367         ...;   // do some housekeeping, then ...
3368         sayNO; // propagate the failure
3369     }
3370
3371 The topmost backtrack state, pointed to by st, is usually free. If you
3372 want to claim it, populate any ST.foo fields in it with values you wish to
3373 save, then do one of
3374
3375         PUSH_STATE_GOTO(resume_state, node, newinput);
3376         PUSH_YES_STATE_GOTO(resume_state, node, newinput);
3377
3378 which sets that backtrack state's resume value to 'resume_state', pushes a
3379 new free entry to the top of the backtrack stack, then goes to 'node'.
3380 On backtracking, the free slot is popped, and the saved state becomes the
3381 new free state. An ST.foo field in this new top state can be temporarily
3382 accessed to retrieve values, but once the main loop is re-entered, it
3383 becomes available for reuse.
3384
3385 Note that the depth of the backtrack stack constantly increases during the
3386 left-to-right execution of the pattern, rather than going up and down with
3387 the pattern nesting. For example the stack is at its maximum at Z at the
3388 end of the pattern, rather than at X in the following:
3389
3390     /(((X)+)+)+....(Y)+....Z/
3391
3392 The only exceptions to this are lookahead/behind assertions and the cut,
3393 (?>A), which pop all the backtrack states associated with A before
3394 continuing.
3395  
3396 Backtrack state structs are allocated in slabs of about 4K in size.
3397 PL_regmatch_state and st always point to the currently active state,
3398 and PL_regmatch_slab points to the slab currently containing
3399 PL_regmatch_state.  The first time regmatch() is called, the first slab is
3400 allocated, and is never freed until interpreter destruction. When the slab
3401 is full, a new one is allocated and chained to the end. At exit from
3402 regmatch(), slabs allocated since entry are freed.
3403
3404 */
3405  
3406
3407 #define DEBUG_STATE_pp(pp)                                  \
3408     DEBUG_STATE_r({                                         \
3409         DUMP_EXEC_POS(locinput, scan, utf8_target);         \
3410         PerlIO_printf(Perl_debug_log,                       \
3411             "    %*s"pp" %s%s%s%s%s\n",                     \
3412             depth*2, "",                                    \
3413             PL_reg_name[st->resume_state],                  \
3414             ((st==yes_state||st==mark_state) ? "[" : ""),   \
3415             ((st==yes_state) ? "Y" : ""),                   \
3416             ((st==mark_state) ? "M" : ""),                  \
3417             ((st==yes_state||st==mark_state) ? "]" : "")    \
3418         );                                                  \
3419     });
3420
3421
3422 #define REG_NODE_NUM(x) ((x) ? (int)((x)-prog) : -1)
3423
3424 #ifdef DEBUGGING
3425
3426 STATIC void
3427 S_debug_start_match(pTHX_ const REGEXP *prog, const bool utf8_target,
3428     const char *start, const char *end, const char *blurb)
3429 {
3430     const bool utf8_pat = RX_UTF8(prog) ? 1 : 0;
3431
3432     PERL_ARGS_ASSERT_DEBUG_START_MATCH;
3433
3434     if (!PL_colorset)   
3435             reginitcolors();    
3436     {
3437         RE_PV_QUOTED_DECL(s0, utf8_pat, PERL_DEBUG_PAD_ZERO(0), 
3438             RX_PRECOMP_const(prog), RX_PRELEN(prog), 60);   
3439         
3440         RE_PV_QUOTED_DECL(s1, utf8_target, PERL_DEBUG_PAD_ZERO(1),
3441             start, end - start, 60); 
3442         
3443         PerlIO_printf(Perl_debug_log, 
3444             "%s%s REx%s %s against %s\n", 
3445                        PL_colors[4], blurb, PL_colors[5], s0, s1); 
3446         
3447         if (utf8_target||utf8_pat)
3448             PerlIO_printf(Perl_debug_log, "UTF-8 %s%s%s...\n",
3449                 utf8_pat ? "pattern" : "",
3450                 utf8_pat && utf8_target ? " and " : "",
3451                 utf8_target ? "string" : ""
3452             ); 
3453     }
3454 }
3455
3456 STATIC void
3457 S_dump_exec_pos(pTHX_ const char *locinput, 
3458                       const regnode *scan, 
3459                       const char *loc_regeol, 
3460                       const char *loc_bostr, 
3461                       const char *loc_reg_starttry,
3462                       const bool utf8_target)
3463 {
3464     const int docolor = *PL_colors[0] || *PL_colors[2] || *PL_colors[4];
3465     const int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
3466     int l = (loc_regeol - locinput) > taill ? taill : (loc_regeol - locinput);
3467     /* The part of the string before starttry has one color
3468        (pref0_len chars), between starttry and current
3469        position another one (pref_len - pref0_len chars),
3470        after the current position the third one.
3471        We assume that pref0_len <= pref_len, otherwise we
3472        decrease pref0_len.  */
3473     int pref_len = (locinput - loc_bostr) > (5 + taill) - l
3474         ? (5 + taill) - l : locinput - loc_bostr;
3475     int pref0_len;
3476
3477     PERL_ARGS_ASSERT_DUMP_EXEC_POS;
3478
3479     while (utf8_target && UTF8_IS_CONTINUATION(*(U8*)(locinput - pref_len)))
3480         pref_len++;
3481     pref0_len = pref_len  - (locinput - loc_reg_starttry);
3482     if (l + pref_len < (5 + taill) && l < loc_regeol - locinput)
3483         l = ( loc_regeol - locinput > (5 + taill) - pref_len
3484               ? (5 + taill) - pref_len : loc_regeol - locinput);
3485     while (utf8_target && UTF8_IS_CONTINUATION(*(U8*)(locinput + l)))
3486         l--;
3487     if (pref0_len < 0)
3488         pref0_len = 0;
3489     if (pref0_len > pref_len)
3490         pref0_len = pref_len;
3491     {
3492         const int is_uni = (utf8_target && OP(scan) != CANY) ? 1 : 0;
3493
3494         RE_PV_COLOR_DECL(s0,len0,is_uni,PERL_DEBUG_PAD(0),
3495             (locinput - pref_len),pref0_len, 60, 4, 5);
3496         
3497         RE_PV_COLOR_DECL(s1,len1,is_uni,PERL_DEBUG_PAD(1),
3498                     (locinput - pref_len + pref0_len),
3499                     pref_len - pref0_len, 60, 2, 3);
3500         
3501         RE_PV_COLOR_DECL(s2,len2,is_uni,PERL_DEBUG_PAD(2),
3502                     locinput, loc_regeol - locinput, 10, 0, 1);
3503
3504         const STRLEN tlen=len0+len1+len2;
3505         PerlIO_printf(Perl_debug_log,
3506                     "%4"IVdf" <%.*s%.*s%s%.*s>%*s|",
3507                     (IV)(locinput - loc_bostr),
3508                     len0, s0,
3509                     len1, s1,
3510                     (docolor ? "" : "> <"),
3511                     len2, s2,
3512                     (int)(tlen > 19 ? 0 :  19 - tlen),
3513                     "");
3514     }
3515 }
3516
3517 #endif
3518
3519 /* reg_check_named_buff_matched()
3520  * Checks to see if a named buffer has matched. The data array of 
3521  * buffer numbers corresponding to the buffer is expected to reside
3522  * in the regexp->data->data array in the slot stored in the ARG() of
3523  * node involved. Note that this routine doesn't actually care about the
3524  * name, that information is not preserved from compilation to execution.
3525  * Returns the index of the leftmost defined buffer with the given name
3526  * or 0 if non of the buffers matched.
3527  */
3528 STATIC I32
3529 S_reg_check_named_buff_matched(pTHX_ const regexp *rex, const regnode *scan)
3530 {
3531     I32 n;
3532     RXi_GET_DECL(rex,rexi);
3533     SV *sv_dat= MUTABLE_SV(rexi->data->data[ ARG( scan ) ]);
3534     I32 *nums=(I32*)SvPVX(sv_dat);
3535
3536     PERL_ARGS_ASSERT_REG_CHECK_NAMED_BUFF_MATCHED;
3537
3538     for ( n=0; n<SvIVX(sv_dat); n++ ) {
3539         if ((I32)rex->lastparen >= nums[n] &&
3540             rex->offs[nums[n]].end != -1)
3541         {
3542             return nums[n];
3543         }
3544     }
3545     return 0;
3546 }
3547
3548
3549 static bool
3550 S_setup_EXACTISH_ST_c1_c2(pTHX_ const regnode * const text_node, int *c1p,
3551         U8* c1_utf8, int *c2p, U8* c2_utf8, regmatch_info *reginfo)
3552 {
3553     /* This function determines if there are one or two characters that match
3554      * the first character of the passed-in EXACTish node <text_node>, and if
3555      * so, returns them in the passed-in pointers.
3556      *
3557      * If it determines that no possible character in the target string can
3558      * match, it returns FALSE; otherwise TRUE.  (The FALSE situation occurs if
3559      * the first character in <text_node> requires UTF-8 to represent, and the
3560      * target string isn't in UTF-8.)
3561      *
3562      * If there are more than two characters that could match the beginning of
3563      * <text_node>, or if more context is required to determine a match or not,
3564      * it sets both *<c1p> and *<c2p> to CHRTEST_VOID.
3565      *
3566      * The motiviation behind this function is to allow the caller to set up
3567      * tight loops for matching.  If <text_node> is of type EXACT, there is
3568      * only one possible character that can match its first character, and so
3569      * the situation is quite simple.  But things get much more complicated if
3570      * folding is involved.  It may be that the first character of an EXACTFish
3571      * node doesn't participate in any possible fold, e.g., punctuation, so it
3572      * can be matched only by itself.  The vast majority of characters that are
3573      * in folds match just two things, their lower and upper-case equivalents.
3574      * But not all are like that; some have multiple possible matches, or match
3575      * sequences of more than one character.  This function sorts all that out.
3576      *
3577      * Consider the patterns A*B or A*?B where A and B are arbitrary.  In a
3578      * loop of trying to match A*, we know we can't exit where the thing
3579      * following it isn't a B.  And something can't be a B unless it is the
3580      * beginning of B.  By putting a quick test for that beginning in a tight
3581      * loop, we can rule out things that can't possibly be B without having to
3582      * break out of the loop, thus avoiding work.  Similarly, if A is a single
3583      * character, we can make a tight loop matching A*, using the outputs of
3584      * this function.
3585      *
3586      * If the target string to match isn't in UTF-8, and there aren't
3587      * complications which require CHRTEST_VOID, *<c1p> and *<c2p> are set to
3588      * the one or two possible octets (which are characters in this situation)
3589      * that can match.  In all cases, if there is only one character that can
3590      * match, *<c1p> and *<c2p> will be identical.
3591      *
3592      * If the target string is in UTF-8, the buffers pointed to by <c1_utf8>
3593      * and <c2_utf8> will contain the one or two UTF-8 sequences of bytes that
3594      * can match the beginning of <text_node>.  They should be declared with at
3595      * least length UTF8_MAXBYTES+1.  (If the target string isn't in UTF-8, it is
3596      * undefined what these contain.)  If one or both of the buffers are
3597      * invariant under UTF-8, *<c1p>, and *<c2p> will also be set to the
3598      * corresponding invariant.  If variant, the corresponding *<c1p> and/or
3599      * *<c2p> will be set to a negative number(s) that shouldn't match any code
3600      * point (unless inappropriately coerced to unsigned).   *<c1p> will equal
3601      * *<c2p> if and only if <c1_utf8> and <c2_utf8> are the same. */
3602
3603     const bool utf8_target = reginfo->is_utf8_target;
3604
3605     UV c1 = CHRTEST_NOT_A_CP_1;
3606     UV c2 = CHRTEST_NOT_A_CP_2;
3607     bool use_chrtest_void = FALSE;
3608     const bool is_utf8_pat = reginfo->is_utf8_pat;
3609
3610     /* Used when we have both utf8 input and utf8 output, to avoid converting
3611      * to/from code points */
3612     bool utf8_has_been_setup = FALSE;
3613
3614     dVAR;
3615
3616     U8 *pat = (U8*)STRING(text_node);
3617     U8 folded[UTF8_MAX_FOLD_CHAR_EXPAND * UTF8_MAXBYTES_CASE + 1] = { '\0' };
3618
3619     if (OP(text_node) == EXACT) {
3620
3621         /* In an exact node, only one thing can be matched, that first
3622          * character.  If both the pat and the target are UTF-8, we can just
3623          * copy the input to the output, avoiding finding the code point of
3624          * that character */
3625         if (!is_utf8_pat) {
3626             c2 = c1 = *pat;
3627         }
3628         else if (utf8_target) {
3629             Copy(pat, c1_utf8, UTF8SKIP(pat), U8);
3630             Copy(pat, c2_utf8, UTF8SKIP(pat), U8);
3631             utf8_has_been_setup = TRUE;
3632         }
3633         else {
3634             c2 = c1 = valid_utf8_to_uvchr(pat, NULL);
3635         }
3636     }
3637     else { /* an EXACTFish node */
3638         U8 *pat_end = pat + STR_LEN(text_node);
3639
3640         /* An EXACTFL node has at least some characters unfolded, because what
3641          * they match is not known until now.  So, now is the time to fold
3642          * the first few of them, as many as are needed to determine 'c1' and
3643          * 'c2' later in the routine.  If the pattern isn't UTF-8, we only need
3644          * to fold if in a UTF-8 locale, and then only the Sharp S; everything
3645          * else is 1-1 and isn't assumed to be folded.  In a UTF-8 pattern, we
3646          * need to fold as many characters as a single character can fold to,
3647          * so that later we can check if the first ones are such a multi-char
3648          * fold.  But, in such a pattern only locale-problematic characters
3649          * aren't folded, so we can skip this completely if the first character
3650          * in the node isn't one of the tricky ones */
3651         if (OP(text_node) == EXACTFL) {
3652
3653             if (! is_utf8_pat) {
3654                 if (IN_UTF8_CTYPE_LOCALE && *pat == LATIN_SMALL_LETTER_SHARP_S)
3655                 {
3656                     folded[0] = folded[1] = 's';
3657                     pat = folded;
3658                     pat_end = folded + 2;
3659                 }
3660             }
3661             else if (is_PROBLEMATIC_LOCALE_FOLDEDS_START_utf8(pat)) {
3662                 U8 *s = pat;
3663                 U8 *d = folded;
3664                 int i;
3665
3666                 for (i = 0; i < UTF8_MAX_FOLD_CHAR_EXPAND && s < pat_end; i++) {
3667                     if (isASCII(*s)) {
3668                         *(d++) = (U8) toFOLD_LC(*s);
3669                         s++;
3670                     }
3671                     else {
3672                         STRLEN len;
3673                         _to_utf8_fold_flags(s,
3674                                             d,
3675                                             &len,
3676                                             FOLD_FLAGS_FULL | FOLD_FLAGS_LOCALE);
3677                         d += len;
3678                         s += UTF8SKIP(s);
3679                     }
3680                 }
3681
3682                 pat = folded;
3683                 pat_end = d;
3684             }
3685         }
3686
3687         if ((is_utf8_pat && is_MULTI_CHAR_FOLD_utf8_safe(pat, pat_end))
3688              || (!is_utf8_pat && is_MULTI_CHAR_FOLD_latin1_safe(pat, pat_end)))
3689         {
3690             /* Multi-character folds require more context to sort out.  Also
3691              * PL_utf8_foldclosures used below doesn't handle them, so have to
3692              * be handled outside this routine */
3693             use_chrtest_void = TRUE;
3694         }
3695         else { /* an EXACTFish node which doesn't begin with a multi-char fold */
3696             c1 = is_utf8_pat ? valid_utf8_to_uvchr(pat, NULL) : *pat;
3697             if (c1 > 255) {
3698                 /* Load the folds hash, if not already done */
3699                 SV** listp;
3700                 if (! PL_utf8_foldclosures) {
3701                     _load_PL_utf8_foldclosures();
3702                 }
3703
3704                 /* The fold closures data structure is a hash with the keys
3705                  * being the UTF-8 of every character that is folded to, like
3706                  * 'k', and the values each an array of all code points that
3707                  * fold to its key.  e.g. [ 'k', 'K', KELVIN_SIGN ].
3708                  * Multi-character folds are not included */
3709                 if ((! (listp = hv_fetch(PL_utf8_foldclosures,
3710                                         (char *) pat,
3711                                         UTF8SKIP(pat),
3712                                         FALSE))))
3713                 {
3714                     /* Not found in the hash, therefore there are no folds
3715                     * containing it, so there is only a single character that
3716                     * could match */
3717                     c2 = c1;
3718                 }
3719                 else {  /* Does participate in folds */
3720                     AV* list = (AV*) *listp;
3721                     if (av_tindex(list) != 1) {
3722
3723                         /* If there aren't exactly two folds to this, it is
3724                          * outside the scope of this function */
3725                         use_chrtest_void = TRUE;
3726                     }
3727                     else {  /* There are two.  Get them */
3728                         SV** c_p = av_fetch(list, 0, FALSE);
3729                         if (c_p == NULL) {
3730                             Perl_croak(aTHX_ "panic: invalid PL_utf8_foldclosures structure");
3731                         }
3732                         c1 = SvUV(*c_p);
3733
3734                         c_p = av_fetch(list, 1, FALSE);
3735                         if (c_p == NULL) {
3736                             Perl_croak(aTHX_ "panic: invalid PL_utf8_foldclosures structure");
3737                         }
3738                         c2 = SvUV(*c_p);
3739
3740                         /* Folds that cross the 255/256 boundary are forbidden
3741                          * if EXACTFL (and isnt a UTF8 locale), or EXACTFA and
3742                          * one is ASCIII.  Since the pattern character is above
3743                          * 255, and its only other match is below 256, the only
3744                          * legal match will be to itself.  We have thrown away
3745                          * the original, so have to compute which is the one
3746                          * above 255. */
3747                         if ((c1 < 256) != (c2 < 256)) {
3748                             if ((OP(text_node) == EXACTFL
3749                                  && ! IN_UTF8_CTYPE_LOCALE)
3750                                 || ((OP(text_node) == EXACTFA
3751                                     || OP(text_node) == EXACTFA_NO_TRIE)
3752                                     && (isASCII(c1) || isASCII(c2))))
3753                             {
3754                                 if (c1 < 256) {
3755                                     c1 = c2;
3756                                 }
3757                                 else {
3758                                     c2 = c1;
3759                                 }
3760                             }
3761                         }
3762                     }
3763                 }
3764             }
3765             else /* Here, c1 is <= 255 */
3766                 if (utf8_target
3767                     && HAS_NONLATIN1_FOLD_CLOSURE(c1)
3768                     && ( ! (OP(text_node) == EXACTFL && ! IN_UTF8_CTYPE_LOCALE))
3769                     && ((OP(text_node) != EXACTFA
3770                         && OP(text_node) != EXACTFA_NO_TRIE)
3771                         || ! isASCII(c1)))
3772             {
3773                 /* Here, there could be something above Latin1 in the target
3774                  * which folds to this character in the pattern.  All such
3775                  * cases except LATIN SMALL LETTER Y WITH DIAERESIS have more
3776                  * than two characters involved in their folds, so are outside
3777                  * the scope of this function */
3778                 if (UNLIKELY(c1 == LATIN_SMALL_LETTER_Y_WITH_DIAERESIS)) {
3779                     c2 = LATIN_CAPITAL_LETTER_Y_WITH_DIAERESIS;
3780                 }
3781                 else {
3782                     use_chrtest_void = TRUE;
3783                 }
3784             }
3785             else { /* Here nothing above Latin1 can fold to the pattern
3786                       character */
3787                 switch (OP(text_node)) {
3788
3789                     case EXACTFL:   /* /l rules */
3790                         c2 = PL_fold_locale[c1];
3791                         break;
3792
3793                     case EXACTF:   /* This node only generated for non-utf8
3794                                     patterns */
3795                         assert(! is_utf8_pat);
3796                         if (! utf8_target) {    /* /d rules */
3797                             c2 = PL_fold[c1];
3798                             break;
3799                         }
3800                         /* FALLTHROUGH */
3801                         /* /u rules for all these.  This happens to work for
3802                         * EXACTFA as nothing in Latin1 folds to ASCII */
3803                     case EXACTFA_NO_TRIE:   /* This node only generated for
3804                                             non-utf8 patterns */
3805                         assert(! is_utf8_pat);
3806                         /* FALLTHROUGH */
3807                     case EXACTFA:
3808                     case EXACTFU_SS:
3809                     case EXACTFU:
3810                         c2 = PL_fold_latin1[c1];
3811                         break;
3812
3813                     default:
3814                         Perl_croak(aTHX_ "panic: Unexpected op %u", OP(text_node));
3815                         assert(0); /* NOTREACHED */
3816                 }
3817             }
3818         }
3819     }
3820
3821     /* Here have figured things out.  Set up the returns */
3822     if (use_chrtest_void) {
3823         *c2p = *c1p = CHRTEST_VOID;
3824     }
3825     else if (utf8_target) {
3826         if (! utf8_has_been_setup) {    /* Don't have the utf8; must get it */
3827             uvchr_to_utf8(c1_utf8, c1);
3828             uvchr_to_utf8(c2_utf8, c2);
3829         }
3830
3831         /* Invariants are stored in both the utf8 and byte outputs; Use
3832          * negative numbers otherwise for the byte ones.  Make sure that the
3833          * byte ones are the same iff the utf8 ones are the same */
3834         *c1p = (UTF8_IS_INVARIANT(*c1_utf8)) ? *c1_utf8 : CHRTEST_NOT_A_CP_1;
3835         *c2p = (UTF8_IS_INVARIANT(*c2_utf8))
3836                 ? *c2_utf8
3837                 : (c1 == c2)
3838                   ? CHRTEST_NOT_A_CP_1
3839                   : CHRTEST_NOT_A_CP_2;
3840     }
3841     else if (c1 > 255) {
3842        if (c2 > 255) {  /* both possibilities are above what a non-utf8 string
3843                            can represent */
3844            return FALSE;
3845        }
3846
3847        *c1p = *c2p = c2;    /* c2 is the only representable value */
3848     }
3849     else {  /* c1 is representable; see about c2 */
3850        *c1p = c1;
3851        *c2p = (c2 < 256) ? c2 : c1;
3852     }
3853
3854     return TRUE;
3855 }
3856
3857 /* returns -1 on failure, $+[0] on success */
3858 STATIC SSize_t
3859 S_regmatch(pTHX_ regmatch_info *reginfo, char *startpos, regnode *prog)
3860 {
3861 #if PERL_VERSION < 9 && !defined(PERL_CORE)
3862     dMY_CXT;
3863 #endif
3864     dVAR;
3865     const bool utf8_target = reginfo->is_utf8_target;
3866     const U32 uniflags = UTF8_ALLOW_DEFAULT;
3867     REGEXP *rex_sv = reginfo->prog;
3868     regexp *rex = ReANY(rex_sv);
3869     RXi_GET_DECL(rex,rexi);
3870     /* the current state. This is a cached copy of PL_regmatch_state */
3871     regmatch_state *st;
3872     /* cache heavy used fields of st in registers */
3873     regnode *scan;
3874     regnode *next;
3875     U32 n = 0;  /* general value; init to avoid compiler warning */
3876     SSize_t ln = 0; /* len or last;  init to avoid compiler warning */
3877     char *locinput = startpos;
3878     char *pushinput; /* where to continue after a PUSH */
3879     I32 nextchr;   /* is always set to UCHARAT(locinput) */
3880
3881     bool result = 0;        /* return value of S_regmatch */
3882     int depth = 0;          /* depth of backtrack stack */
3883     U32 nochange_depth = 0; /* depth of GOSUB recursion with nochange */
3884     const U32 max_nochange_depth =
3885         (3 * rex->nparens > MAX_RECURSE_EVAL_NOCHANGE_DEPTH) ?
3886         3 * rex->nparens : MAX_RECURSE_EVAL_NOCHANGE_DEPTH;
3887     regmatch_state *yes_state = NULL; /* state to pop to on success of
3888                                                             subpattern */
3889     /* mark_state piggy backs on the yes_state logic so that when we unwind 
3890        the stack on success we can update the mark_state as we go */
3891     regmatch_state *mark_state = NULL; /* last mark state we have seen */
3892     regmatch_state *cur_eval = NULL; /* most recent EVAL_AB state */
3893     struct regmatch_state  *cur_curlyx = NULL; /* most recent curlyx */
3894     U32 state_num;
3895     bool no_final = 0;      /* prevent failure from backtracking? */
3896     bool do_cutgroup = 0;   /* no_final only until next branch/trie entry */
3897     char *startpoint = locinput;
3898     SV *popmark = NULL;     /* are we looking for a mark? */
3899     SV *sv_commit = NULL;   /* last mark name seen in failure */
3900     SV *sv_yes_mark = NULL; /* last mark name we have seen 
3901                                during a successful match */
3902     U32 lastopen = 0;       /* last open we saw */
3903     bool has_cutgroup = RX_HAS_CUTGROUP(rex) ? 1 : 0;   
3904     SV* const oreplsv = GvSVn(PL_replgv);
3905     /* these three flags are set by various ops to signal information to
3906      * the very next op. They have a useful lifetime of exactly one loop
3907      * iteration, and are not preserved or restored by state pushes/pops
3908      */
3909     bool sw = 0;            /* the condition value in (?(cond)a|b) */
3910     bool minmod = 0;        /* the next "{n,m}" is a "{n,m}?" */
3911     int logical = 0;        /* the following EVAL is:
3912                                 0: (?{...})
3913                                 1: (?(?{...})X|Y)
3914                                 2: (??{...})
3915                                or the following IFMATCH/UNLESSM is:
3916                                 false: plain (?=foo)
3917                                 true:  used as a condition: (?(?=foo))
3918                             */
3919     PAD* last_pad = NULL;
3920     dMULTICALL;
3921     I32 gimme = G_SCALAR;
3922     CV *caller_cv = NULL;       /* who called us */
3923     CV *last_pushed_cv = NULL;  /* most recently called (?{}) CV */
3924     CHECKPOINT runops_cp;       /* savestack position before executing EVAL */
3925     U32 maxopenparen = 0;       /* max '(' index seen so far */
3926     int to_complement;  /* Invert the result? */
3927     _char_class_number classnum;
3928     bool is_utf8_pat = reginfo->is_utf8_pat;
3929
3930 #ifdef DEBUGGING
3931     GET_RE_DEBUG_FLAGS_DECL;
3932 #endif
3933
3934     /* protect against undef(*^R) */
3935     SAVEFREESV(SvREFCNT_inc_simple_NN(oreplsv));
3936
3937     /* shut up 'may be used uninitialized' compiler warnings for dMULTICALL */
3938     multicall_oldcatch = 0;
3939     multicall_cv = NULL;
3940     cx = NULL;
3941     PERL_UNUSED_VAR(multicall_cop);
3942     PERL_UNUSED_VAR(newsp);
3943
3944
3945     PERL_ARGS_ASSERT_REGMATCH;
3946
3947     DEBUG_OPTIMISE_r( DEBUG_EXECUTE_r({
3948             PerlIO_printf(Perl_debug_log,"regmatch start\n");
3949     }));
3950
3951     st = PL_regmatch_state;
3952
3953     /* Note that nextchr is a byte even in UTF */
3954     SET_nextchr;
3955     scan = prog;
3956     while (scan != NULL) {
3957
3958         DEBUG_EXECUTE_r( {
3959             SV * const prop = sv_newmortal();
3960             regnode *rnext=regnext(scan);
3961             DUMP_EXEC_POS( locinput, scan, utf8_target );
3962             regprop(rex, prop, scan, reginfo);
3963             
3964             PerlIO_printf(Perl_debug_log,
3965                     "%3"IVdf":%*s%s(%"IVdf")\n",
3966                     (IV)(scan - rexi->program), depth*2, "",
3967                     SvPVX_const(prop),
3968                     (PL_regkind[OP(scan)] == END || !rnext) ? 
3969                         0 : (IV)(rnext - rexi->program));
3970         });
3971
3972         next = scan + NEXT_OFF(scan);
3973         if (next == scan)
3974             next = NULL;
3975         state_num = OP(scan);
3976
3977       reenter_switch:
3978         to_complement = 0;
3979
3980         SET_nextchr;
3981         assert(nextchr < 256 && (nextchr >= 0 || nextchr == NEXTCHR_EOS));
3982
3983         switch (state_num) {
3984         case BOL:  /*  /^../   */
3985         case SBOL: /*  /^../s  */
3986             if (locinput == reginfo->strbeg)
3987                 break;
3988             sayNO;
3989
3990         case MBOL: /*  /^../m  */
3991             if (locinput == reginfo->strbeg ||
3992                 (!NEXTCHR_IS_EOS && locinput[-1] == '\n'))
3993             {
3994                 break;
3995             }
3996             sayNO;
3997
3998         case GPOS: /*  \G  */
3999             if (locinput == reginfo->ganch)
4000                 break;
4001             sayNO;
4002
4003         case KEEPS: /*   \K  */
4004             /* update the startpoint */
4005             st->u.keeper.val = rex->offs[0].start;
4006             rex->offs[0].start = locinput - reginfo->strbeg;
4007             PUSH_STATE_GOTO(KEEPS_next, next, locinput);
4008             assert(0); /*NOTREACHED*/
4009         case KEEPS_next_fail:
4010             /* rollback the start point change */
4011             rex->offs[0].start = st->u.keeper.val;
4012             sayNO_SILENT;
4013             assert(0); /*NOTREACHED*/
4014
4015         case MEOL: /* /..$/m  */
4016             if (!NEXTCHR_IS_EOS && nextchr != '\n')
4017                 sayNO;
4018             break;
4019
4020         case EOL: /* /..$/  */
4021             /* FALLTHROUGH */
4022         case SEOL: /* /..$/s  */
4023             if (!NEXTCHR_IS_EOS && nextchr != '\n')
4024                 sayNO;
4025             if (reginfo->strend - locinput > 1)
4026                 sayNO;
4027             break;
4028
4029         case EOS: /*  \z  */
4030             if (!NEXTCHR_IS_EOS)
4031                 sayNO;
4032             break;
4033
4034         case SANY: /*  /./s  */
4035             if (NEXTCHR_IS_EOS)
4036                 sayNO;
4037             goto increment_locinput;
4038
4039         case CANY: /*  \C  */
4040             if (NEXTCHR_IS_EOS)
4041                 sayNO;
4042             locinput++;
4043             break;
4044
4045         case REG_ANY: /*  /./  */
4046             if ((NEXTCHR_IS_EOS) || nextchr == '\n')
4047                 sayNO;
4048             goto increment_locinput;
4049
4050
4051 #undef  ST
4052 #define ST st->u.trie
4053         case TRIEC: /* (ab|cd) with known charclass */
4054             /* In this case the charclass data is available inline so
4055                we can fail fast without a lot of extra overhead. 
4056              */
4057             if(!NEXTCHR_IS_EOS && !ANYOF_BITMAP_TEST(scan, nextchr)) {
4058                 DEBUG_EXECUTE_r(
4059                     PerlIO_printf(Perl_debug_log,
4060                               "%*s  %sfailed to match trie start class...%s\n",
4061                               REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5])
4062                 );
4063                 sayNO_SILENT;
4064                 assert(0); /* NOTREACHED */
4065             }
4066             /* FALLTHROUGH */
4067         case TRIE:  /* (ab|cd)  */
4068             /* the basic plan of execution of the trie is:
4069              * At the beginning, run though all the states, and
4070              * find the longest-matching word. Also remember the position
4071              * of the shortest matching word. For example, this pattern:
4072              *    1  2 3 4    5
4073              *    ab|a|x|abcd|abc
4074              * when matched against the string "abcde", will generate
4075              * accept states for all words except 3, with the longest
4076              * matching word being 4, and the shortest being 2 (with
4077              * the position being after char 1 of the string).
4078              *
4079              * Then for each matching word, in word order (i.e. 1,2,4,5),
4080              * we run the remainder of the pattern; on each try setting
4081              * the current position to the character following the word,
4082              * returning to try the next word on failure.
4083              *
4084              * We avoid having to build a list of words at runtime by
4085              * using a compile-time structure, wordinfo[].prev, which
4086              * gives, for each word, the previous accepting word (if any).
4087              * In the case above it would contain the mappings 1->2, 2->0,
4088              * 3->0, 4->5, 5->1.  We can use this table to generate, from
4089              * the longest word (4 above), a list of all words, by
4090              * following the list of prev pointers; this gives us the
4091              * unordered list 4,5,1,2. Then given the current word we have
4092              * just tried, we can go through the list and find the
4093              * next-biggest word to try (so if we just failed on word 2,
4094              * the next in the list is 4).
4095              *
4096              * Since at runtime we don't record the matching position in
4097              * the string for each word, we have to work that out for
4098              * each word we're about to process. The wordinfo table holds
4099              * the character length of each word; given that we recorded
4100              * at the start: the position of the shortest word and its
4101              * length in chars, we just need to move the pointer the
4102              * difference between the two char lengths. Depending on
4103              * Unicode status and folding, that's cheap or expensive.
4104              *
4105              * This algorithm is optimised for the case where are only a
4106              * small number of accept states, i.e. 0,1, or maybe 2.
4107              * With lots of accepts states, and having to try all of them,
4108              * it becomes quadratic on number of accept states to find all
4109              * the next words.
4110              */
4111
4112             {
4113                 /* what type of TRIE am I? (utf8 makes this contextual) */
4114                 DECL_TRIE_TYPE(scan);
4115
4116                 /* what trie are we using right now */
4117                 reg_trie_data * const trie
4118                     = (reg_trie_data*)rexi->data->data[ ARG( scan ) ];
4119                 HV * widecharmap = MUTABLE_HV(rexi->data->data[ ARG( scan ) + 1 ]);
4120                 U32 state = trie->startstate;
4121
4122                 if (   trie->bitmap
4123                     && (NEXTCHR_IS_EOS || !TRIE_BITMAP_TEST(trie, nextchr)))
4124                 {
4125                     if (trie->states[ state ].wordnum) {
4126                          DEBUG_EXECUTE_r(
4127                             PerlIO_printf(Perl_debug_log,
4128                                           "%*s  %smatched empty string...%s\n",
4129                                           REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5])
4130                         );
4131                         if (!trie->jump)
4132                             break;
4133                     } else {
4134                         DEBUG_EXECUTE_r(
4135                             PerlIO_printf(Perl_debug_log,
4136                                           "%*s  %sfailed to match trie start class...%s\n",
4137                                           REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5])
4138                         );
4139                         sayNO_SILENT;
4140                    }
4141                 }
4142
4143             { 
4144                 U8 *uc = ( U8* )locinput;
4145
4146                 STRLEN len = 0;
4147                 STRLEN foldlen = 0;
4148                 U8 *uscan = (U8*)NULL;
4149                 U8 foldbuf[ UTF8_MAXBYTES_CASE + 1 ];
4150                 U32 charcount = 0; /* how many input chars we have matched */
4151                 U32 accepted = 0; /* have we seen any accepting states? */
4152
4153                 ST.jump = trie->jump;
4154                 ST.me = scan;
4155                 ST.firstpos = NULL;
4156                 ST.longfold = FALSE; /* char longer if folded => it's harder */
4157                 ST.nextword = 0;
4158
4159                 /* fully traverse the TRIE; note the position of the
4160                    shortest accept state and the wordnum of the longest
4161                    accept state */
4162
4163                 while ( state && uc <= (U8*)(reginfo->strend) ) {
4164                     U32 base = trie->states[ state ].trans.base;
4165                     UV uvc = 0;
4166                     U16 charid = 0;
4167                     U16 wordnum;
4168                     wordnum = trie->states[ state ].wordnum;
4169
4170                     if (wordnum) { /* it's an accept state */
4171                         if (!accepted) {
4172                             accepted = 1;
4173                             /* record first match position */
4174                             if (ST.longfold) {
4175                                 ST.firstpos = (U8*)locinput;
4176                                 ST.firstchars = 0;
4177                             }
4178                             else {
4179                                 ST.firstpos = uc;
4180                                 ST.firstchars = charcount;
4181                             }
4182                         }
4183                         if (!ST.nextword || wordnum < ST.nextword)
4184                             ST.nextword = wordnum;
4185                         ST.topword = wordnum;
4186                     }
4187
4188                     DEBUG_TRIE_EXECUTE_r({
4189                                 DUMP_EXEC_POS( (char *)uc, scan, utf8_target );
4190                                 PerlIO_printf( Perl_debug_log,
4191                                     "%*s  %sState: %4"UVxf" Accepted: %c ",
4192                                     2+depth * 2, "", PL_colors[4],
4193                                     (UV)state, (accepted ? 'Y' : 'N'));
4194                     });
4195
4196                     /* read a char and goto next state */
4197                     if ( base && (foldlen || uc < (U8*)(reginfo->strend))) {
4198                         I32 offset;
4199                         REXEC_TRIE_READ_CHAR(trie_type, trie, widecharmap, uc,
4200                                              uscan, len, uvc, charid, foldlen,
4201                                              foldbuf, uniflags);
4202                         charcount++;
4203                         if (foldlen>0)
4204                             ST.longfold = TRUE;
4205                         if (charid &&
4206                              ( ((offset =
4207                               base + charid - 1 - trie->uniquecharcount)) >= 0)
4208
4209                              && ((U32)offset < trie->lasttrans)
4210                              && trie->trans[offset].check == state)
4211                         {
4212                             state = trie->trans[offset].next;
4213                         }
4214                         else {
4215                             state = 0;
4216                         }
4217                         uc += len;
4218
4219                     }
4220                     else {
4221                         state = 0;
4222                     }
4223                     DEBUG_TRIE_EXECUTE_r(
4224                         PerlIO_printf( Perl_debug_log,
4225                             "Charid:%3x CP:%4"UVxf" After State: %4"UVxf"%s\n",
4226                             charid, uvc, (UV)state, PL_colors[5] );
4227                     );
4228                 }
4229                 if (!accepted)
4230                    sayNO;
4231
4232                 /* calculate total number of accept states */
4233                 {
4234                     U16 w = ST.topword;
4235                     accepted = 0;
4236                     while (w) {
4237                         w = trie->wordinfo[w].prev;
4238                         accepted++;
4239                     }
4240                     ST.accepted = accepted;
4241                 }
4242
4243                 DEBUG_EXECUTE_r(
4244                     PerlIO_printf( Perl_debug_log,
4245                         "%*s  %sgot %"IVdf" possible matches%s\n",
4246                         REPORT_CODE_OFF + depth * 2, "",
4247                         PL_colors[4], (IV)ST.accepted, PL_colors[5] );
4248                 );
4249                 goto trie_first_try; /* jump into the fail handler */
4250             }}
4251             assert(0); /* NOTREACHED */
4252
4253         case TRIE_next_fail: /* we failed - try next alternative */
4254         {
4255             U8 *uc;
4256             if ( ST.jump) {
4257                 REGCP_UNWIND(ST.cp);
4258                 UNWIND_PAREN(ST.lastparen, ST.lastcloseparen);
4259             }
4260             if (!--ST.accepted) {
4261                 DEBUG_EXECUTE_r({
4262                     PerlIO_printf( Perl_debug_log,
4263                         "%*s  %sTRIE failed...%s\n",
4264                         REPORT_CODE_OFF+depth*2, "", 
4265                         PL_colors[4],
4266                         PL_colors[5] );
4267                 });
4268                 sayNO_SILENT;
4269             }
4270             {
4271                 /* Find next-highest word to process.  Note that this code
4272                  * is O(N^2) per trie run (O(N) per branch), so keep tight */
4273                 U16 min = 0;
4274                 U16 word;
4275                 U16 const nextword = ST.nextword;
4276                 reg_trie_wordinfo * const wordinfo
4277                     = ((reg_trie_data*)rexi->data->data[ARG(ST.me)])->wordinfo;
4278                 for (word=ST.topword; word; word=wordinfo[word].prev) {
4279                     if (word > nextword && (!min || word < min))
4280                         min = word;
4281                 }
4282                 ST.nextword = min;
4283             }
4284
4285           trie_first_try:
4286             if (do_cutgroup) {
4287                 do_cutgroup = 0;
4288                 no_final = 0;
4289             }
4290
4291             if ( ST.jump) {
4292                 ST.lastparen = rex->lastparen;
4293                 ST.lastcloseparen = rex->lastcloseparen;
4294                 REGCP_SET(ST.cp);
4295             }
4296
4297             /* find start char of end of current word */
4298             {
4299                 U32 chars; /* how many chars to skip */
4300                 reg_trie_data * const trie
4301                     = (reg_trie_data*)rexi->data->data[ARG(ST.me)];
4302
4303                 assert((trie->wordinfo[ST.nextword].len - trie->prefixlen)
4304                             >=  ST.firstchars);
4305                 chars = (trie->wordinfo[ST.nextword].len - trie->prefixlen)
4306                             - ST.firstchars;
4307                 uc = ST.firstpos;
4308
4309                 if (ST.longfold) {
4310                     /* the hard option - fold each char in turn and find
4311                      * its folded length (which may be different */
4312                     U8 foldbuf[UTF8_MAXBYTES_CASE + 1];
4313                     STRLEN foldlen;
4314                     STRLEN len;
4315                     UV uvc;
4316                     U8 *uscan;
4317
4318                     while (chars) {
4319                         if (utf8_target) {
4320                             uvc = utf8n_to_uvchr((U8*)uc, UTF8_MAXLEN, &len,
4321                                                     uniflags);
4322                             uc += len;
4323                         }
4324                         else {
4325                             uvc = *uc;
4326                             uc++;
4327                         }
4328                         uvc = to_uni_fold(uvc, foldbuf, &foldlen);
4329                         uscan = foldbuf;
4330                         while (foldlen) {
4331                             if (!--chars)
4332                                 break;
4333                             uvc = utf8n_to_uvchr(uscan, UTF8_MAXLEN, &len,
4334                                             uniflags);
4335                             uscan += len;
4336                             foldlen -= len;
4337                         }
4338                     }
4339                 }
4340                 else {
4341                     if (utf8_target)
4342                         while (chars--)
4343                             uc += UTF8SKIP(uc);
4344                     else
4345                         uc += chars;
4346                 }
4347             }
4348
4349             scan = ST.me + ((ST.jump && ST.jump[ST.nextword])
4350                             ? ST.jump[ST.nextword]
4351                             : NEXT_OFF(ST.me));
4352
4353             DEBUG_EXECUTE_r({
4354                 PerlIO_printf( Perl_debug_log,
4355                     "%*s  %sTRIE matched word #%d, continuing%s\n",
4356                     REPORT_CODE_OFF+depth*2, "", 
4357                     PL_colors[4],
4358                     ST.nextword,
4359                     PL_colors[5]
4360                     );
4361             });
4362
4363             if (ST.accepted > 1 || has_cutgroup) {
4364                 PUSH_STATE_GOTO(TRIE_next, scan, (char*)uc);
4365                 assert(0); /* NOTREACHED */
4366             }
4367             /* only one choice left - just continue */
4368             DEBUG_EXECUTE_r({
4369                 AV *const trie_words
4370                     = MUTABLE_AV(rexi->data->data[ARG(ST.me)+TRIE_WORDS_OFFSET]);
4371                 SV ** const tmp = av_fetch( trie_words,
4372                     ST.nextword-1, 0 );
4373                 SV *sv= tmp ? sv_newmortal() : NULL;
4374
4375                 PerlIO_printf( Perl_debug_log,
4376                     "%*s  %sonly one match left, short-circuiting: #%d <%s>%s\n",
4377                     REPORT_CODE_OFF+depth*2, "", PL_colors[4],
4378                     ST.nextword,
4379                     tmp ? pv_pretty(sv, SvPV_nolen_const(*tmp), SvCUR(*tmp), 0,
4380                             PL_colors[0], PL_colors[1],
4381                             (SvUTF8(*tmp) ? PERL_PV_ESCAPE_UNI : 0)|PERL_PV_ESCAPE_NONASCII
4382                         ) 
4383                     : "not compiled under -Dr",
4384                     PL_colors[5] );
4385             });
4386
4387             locinput = (char*)uc;
4388             continue; /* execute rest of RE */
4389             assert(0); /* NOTREACHED */
4390         }
4391 #undef  ST
4392
4393         case EXACT: {            /*  /abc/        */
4394             char *s = STRING(scan);
4395             ln = STR_LEN(scan);
4396             if (utf8_target != is_utf8_pat) {
4397                 /* The target and the pattern have differing utf8ness. */
4398                 char *l = locinput;
4399                 const char * const e = s + ln;
4400
4401                 if (utf8_target) {
4402                     /* The target is utf8, the pattern is not utf8.
4403                      * Above-Latin1 code points can't match the pattern;
4404                      * invariants match exactly, and the other Latin1 ones need
4405                      * to be downgraded to a single byte in order to do the
4406                      * comparison.  (If we could be confident that the target
4407                      * is not malformed, this could be refactored to have fewer
4408                      * tests by just assuming that if the first bytes match, it
4409                      * is an invariant, but there are tests in the test suite
4410                      * dealing with (??{...}) which violate this) */
4411                     while (s < e) {
4412                         if (l >= reginfo->strend
4413                             || UTF8_IS_ABOVE_LATIN1(* (U8*) l))
4414                         {
4415                             sayNO;
4416                         }
4417                         if (UTF8_IS_INVARIANT(*(U8*)l)) {
4418                             if (*l != *s) {
4419                                 sayNO;
4420                             }
4421                             l++;
4422                         }
4423                         else {
4424                             if (TWO_BYTE_UTF8_TO_NATIVE(*l, *(l+1)) != * (U8*) s)
4425                             {
4426                                 sayNO;
4427                             }
4428                             l += 2;
4429                         }
4430                         s++;
4431                     }
4432                 }
4433                 else {
4434                     /* The target is not utf8, the pattern is utf8. */
4435                     while (s < e) {
4436                         if (l >= reginfo->strend
4437                             || UTF8_IS_ABOVE_LATIN1(* (U8*) s))
4438                         {
4439                             sayNO;
4440                         }
4441                         if (UTF8_IS_INVARIANT(*(U8*)s)) {
4442                             if (*s != *l) {
4443                                 sayNO;
4444                             }
4445                             s++;
4446                         }
4447                         else {
4448                             if (TWO_BYTE_UTF8_TO_NATIVE(*s, *(s+1)) != * (U8*) l)
4449                             {
4450                                 sayNO;
4451                             }
4452                             s += 2;
4453                         }
4454                         l++;
4455                     }
4456                 }
4457                 locinput = l;
4458             }
4459             else {
4460                 /* The target and the pattern have the same utf8ness. */
4461                 /* Inline the first character, for speed. */
4462                 if (reginfo->strend - locinput < ln
4463                     || UCHARAT(s) != nextchr
4464                     || (ln > 1 && memNE(s, locinput, ln)))
4465                 {
4466                     sayNO;
4467                 }
4468                 locinput += ln;
4469             }
4470             break;
4471             }
4472
4473         case EXACTFL: {          /*  /abc/il      */
4474             re_fold_t folder;
4475             const U8 * fold_array;
4476             const char * s;
4477             U32 fold_utf8_flags;
4478
4479             folder = foldEQ_locale;
4480             fold_array = PL_fold_locale;
4481             fold_utf8_flags = FOLDEQ_LOCALE;
4482             goto do_exactf;
4483
4484         case EXACTFU_SS:         /*  /\x{df}/iu   */
4485         case EXACTFU:            /*  /abc/iu      */
4486             folder = foldEQ_latin1;
4487             fold_array = PL_fold_latin1;
4488             fold_utf8_flags = is_utf8_pat ? FOLDEQ_S1_ALREADY_FOLDED : 0;
4489             goto do_exactf;
4490
4491         case EXACTFA_NO_TRIE:   /* This node only generated for non-utf8
4492                                    patterns */
4493             assert(! is_utf8_pat);
4494             /* FALLTHROUGH */
4495         case EXACTFA:            /*  /abc/iaa     */
4496             folder = foldEQ_latin1;
4497             fold_array = PL_fold_latin1;
4498             fold_utf8_flags = FOLDEQ_UTF8_NOMIX_ASCII;
4499             goto do_exactf;
4500
4501         case EXACTF:             /*  /abc/i    This node only generated for
4502                                                non-utf8 patterns */
4503             assert(! is_utf8_pat);
4504             folder = foldEQ;
4505             fold_array = PL_fold;
4506             fold_utf8_flags = 0;
4507
4508           do_exactf:
4509             s = STRING(scan);
4510             ln = STR_LEN(scan);
4511
4512             if (utf8_target
4513                 || is_utf8_pat
4514                 || state_num == EXACTFU_SS
4515                 || (state_num == EXACTFL && IN_UTF8_CTYPE_LOCALE))
4516             {
4517               /* Either target or the pattern are utf8, or has the issue where
4518                * the fold lengths may differ. */
4519                 const char * const l = locinput;
4520                 char *e = reginfo->strend;
4521
4522                 if (! foldEQ_utf8_flags(s, 0,  ln, is_utf8_pat,
4523                                         l, &e, 0,  utf8_target, fold_utf8_flags))
4524                 {
4525                     sayNO;
4526                 }
4527                 locinput = e;
4528                 break;
4529             }
4530
4531             /* Neither the target nor the pattern are utf8 */
4532             if (UCHARAT(s) != nextchr
4533                 && !NEXTCHR_IS_EOS
4534                 && UCHARAT(s) != fold_array[nextchr])
4535             {
4536                 sayNO;
4537             }
4538             if (reginfo->strend - locinput < ln)
4539                 sayNO;
4540             if (ln > 1 && ! folder(s, locinput, ln))
4541                 sayNO;
4542             locinput += ln;
4543             break;
4544         }
4545
4546         /* XXX Could improve efficiency by separating these all out using a
4547          * macro or in-line function.  At that point regcomp.c would no longer
4548          * have to set the FLAGS fields of these */
4549         case BOUNDL:  /*  /\b/l  */
4550         case NBOUNDL: /*  /\B/l  */
4551         case BOUND:   /*  /\b/   */
4552         case BOUNDU:  /*  /\b/u  */
4553         case BOUNDA:  /*  /\b/a  */
4554         case NBOUND:  /*  /\B/   */
4555         case NBOUNDU: /*  /\B/u  */
4556         case NBOUNDA: /*  /\B/a  */
4557             /* was last char in word? */
4558             if (utf8_target
4559                 && FLAGS(scan) != REGEX_ASCII_RESTRICTED_CHARSET
4560                 && FLAGS(scan) != REGEX_ASCII_MORE_RESTRICTED_CHARSET)
4561             {
4562                 if (locinput == reginfo->strbeg)
4563                     ln = '\n';
4564                 else {
4565                     const U8 * const r =
4566                             reghop3((U8*)locinput, -1, (U8*)(reginfo->strbeg));
4567
4568                     ln = utf8n_to_uvchr(r, (U8*) reginfo->strend - r,
4569                                                                    0, uniflags);
4570                 }
4571                 if (FLAGS(scan) != REGEX_LOCALE_CHARSET) {
4572                     ln = isWORDCHAR_uni(ln);
4573                     if (NEXTCHR_IS_EOS)
4574                         n = 0;
4575                     else {
4576                         LOAD_UTF8_CHARCLASS_ALNUM();
4577                         n = swash_fetch(PL_utf8_swash_ptrs[_CC_WORDCHAR], (U8*)locinput,
4578                                                                 utf8_target);
4579                     }
4580                 }
4581                 else {
4582                     ln = isWORDCHAR_LC_uvchr(ln);
4583                     n = NEXTCHR_IS_EOS ? 0 : isWORDCHAR_LC_utf8((U8*)locinput);
4584                 }
4585             }
4586             else {
4587
4588                 /* Here the string isn't utf8, or is utf8 and only ascii
4589                  * characters are to match \w.  In the latter case looking at
4590                  * the byte just prior to the current one may be just the final
4591                  * byte of a multi-byte character.  This is ok.  There are two
4592                  * cases:
4593                  * 1) it is a single byte character, and then the test is doing
4594                  *      just what it's supposed to.
4595                  * 2) it is a multi-byte character, in which case the final
4596                  *      byte is never mistakable for ASCII, and so the test
4597                  *      will say it is not a word character, which is the
4598                  *      correct answer. */
4599                 ln = (locinput != reginfo->strbeg) ?
4600                     UCHARAT(locinput - 1) : '\n';
4601                 switch (FLAGS(scan)) {
4602                     case REGEX_UNICODE_CHARSET:
4603                         ln = isWORDCHAR_L1(ln);
4604                         n = NEXTCHR_IS_EOS ? 0 : isWORDCHAR_L1(nextchr);
4605                         break;
4606                     case REGEX_LOCALE_CHARSET:
4607                         ln = isWORDCHAR_LC(ln);
4608                         n = NEXTCHR_IS_EOS ? 0 : isWORDCHAR_LC(nextchr);
4609                         break;
4610                     case REGEX_DEPENDS_CHARSET:
4611                         ln = isWORDCHAR(ln);
4612                         n = NEXTCHR_IS_EOS ? 0 : isWORDCHAR(nextchr);
4613                         break;
4614                     case REGEX_ASCII_RESTRICTED_CHARSET:
4615                     case REGEX_ASCII_MORE_RESTRICTED_CHARSET:
4616                         ln = isWORDCHAR_A(ln);
4617                         n = NEXTCHR_IS_EOS ? 0 : isWORDCHAR_A(nextchr);
4618                         break;
4619                     default:
4620                         Perl_croak(aTHX_ "panic: Unexpected FLAGS %u in op %u", FLAGS(scan), OP(scan));
4621                         break;
4622                 }
4623             }
4624             /* Note requires that all BOUNDs be lower than all NBOUNDs in
4625              * regcomp.sym */
4626             if (((!ln) == (!n)) == (OP(scan) < NBOUND))
4627                     sayNO;
4628             break;
4629
4630         case ANYOF:  /*  /[abc]/       */
4631             if (NEXTCHR_IS_EOS)
4632                 sayNO;
4633             if (utf8_target) {
4634                 if (!reginclass(rex, scan, (U8*)locinput, (U8*)reginfo->strend,
4635                                                                    utf8_target))
4636                     sayNO;
4637                 locinput += UTF8SKIP(locinput);
4638             }
4639             else {
4640                 if (!REGINCLASS(rex, scan, (U8*)locinput))
4641                     sayNO;
4642                 locinput++;
4643             }
4644             break;
4645
4646         /* The argument (FLAGS) to all the POSIX node types is the class number
4647          * */
4648
4649         case NPOSIXL:   /* \W or [:^punct:] etc. under /l */
4650             to_complement = 1;
4651             /* FALLTHROUGH */
4652
4653         case POSIXL:    /* \w or [:punct:] etc. under /l */
4654             if (NEXTCHR_IS_EOS)
4655                 sayNO;
4656
4657             /* Use isFOO_lc() for characters within Latin1.  (Note that
4658              * UTF8_IS_INVARIANT works even on non-UTF-8 strings, or else
4659              * wouldn't be invariant) */
4660             if (UTF8_IS_INVARIANT(nextchr) || ! utf8_target) {
4661                 if (! (to_complement ^ cBOOL(isFOO_lc(FLAGS(scan), (U8) nextchr)))) {
4662                     sayNO;
4663                 }
4664             }
4665             else if (UTF8_IS_DOWNGRADEABLE_START(nextchr)) {
4666                 if (! (to_complement ^ cBOOL(isFOO_lc(FLAGS(scan),
4667                                            (U8) TWO_BYTE_UTF8_TO_NATIVE(nextchr,
4668                                                             *(locinput + 1))))))
4669                 {
4670                     sayNO;
4671                 }
4672             }
4673             else { /* Here, must be an above Latin-1 code point */
4674                 goto utf8_posix_not_eos;
4675             }
4676
4677             /* Here, must be utf8 */
4678             locinput += UTF8SKIP(locinput);
4679             break;
4680
4681         case NPOSIXD:   /* \W or [:^punct:] etc. under /d */
4682             to_complement = 1;
4683             /* FALLTHROUGH */
4684
4685         case POSIXD:    /* \w or [:punct:] etc. under /d */
4686             if (utf8_target) {
4687                 goto utf8_posix;
4688             }
4689             goto posixa;
4690
4691         case NPOSIXA:   /* \W or [:^punct:] etc. under /a */
4692
4693             if (NEXTCHR_IS_EOS) {
4694                 sayNO;
4695             }
4696
4697             /* All UTF-8 variants match */
4698             if (! UTF8_IS_INVARIANT(nextchr)) {
4699                 goto increment_locinput;
4700             }
4701
4702             to_complement = 1;
4703             /* FALLTHROUGH */
4704
4705         case POSIXA:    /* \w or [:punct:] etc. under /a */
4706
4707           posixa:
4708             /* We get here through POSIXD, NPOSIXD, and NPOSIXA when not in
4709              * UTF-8, and also from NPOSIXA even in UTF-8 when the current
4710              * character is a single byte */
4711
4712             if (NEXTCHR_IS_EOS
4713                 || ! (to_complement ^ cBOOL(_generic_isCC_A(nextchr,
4714                                                             FLAGS(scan)))))
4715             {
4716                 sayNO;
4717             }
4718
4719             /* Here we are either not in utf8, or we matched a utf8-invariant,
4720              * so the next char is the next byte */
4721             locinput++;
4722             break;
4723
4724         case NPOSIXU:   /* \W or [:^punct:] etc. under /u */
4725             to_complement = 1;
4726             /* FALLTHROUGH */
4727
4728         case POSIXU:    /* \w or [:punct:] etc. under /u */
4729           utf8_posix:
4730             if (NEXTCHR_IS_EOS) {
4731                 sayNO;
4732             }
4733           utf8_posix_not_eos:
4734
4735             /* Use _generic_isCC() for characters within Latin1.  (Note that
4736              * UTF8_IS_INVARIANT works even on non-UTF-8 strings, or else
4737              * wouldn't be invariant) */
4738             if (UTF8_IS_INVARIANT(nextchr) || ! utf8_target) {
4739                 if (! (to_complement ^ cBOOL(_generic_isCC(nextchr,
4740                                                            FLAGS(scan)))))
4741                 {
4742                     sayNO;
4743                 }
4744                 locinput++;
4745             }
4746             else if (UTF8_IS_DOWNGRADEABLE_START(nextchr)) {
4747                 if (! (to_complement
4748                        ^ cBOOL(_generic_isCC(TWO_BYTE_UTF8_TO_NATIVE(nextchr,
4749                                                                *(locinput + 1)),
4750                                              FLAGS(scan)))))
4751                 {
4752                     sayNO;
4753                 }
4754                 locinput += 2;
4755             }
4756             else {  /* Handle above Latin-1 code points */
4757                 classnum = (_char_class_number) FLAGS(scan);
4758                 if (classnum < _FIRST_NON_SWASH_CC) {
4759
4760                     /* Here, uses a swash to find such code points.  Load if if
4761                      * not done already */
4762                     if (! PL_utf8_swash_ptrs[classnum]) {
4763                         U8 flags = _CORE_SWASH_INIT_ACCEPT_INVLIST;
4764                         PL_utf8_swash_ptrs[classnum]
4765                                 = _core_swash_init("utf8",
4766                                         "",
4767                                         &PL_sv_undef, 1, 0,
4768                                         PL_XPosix_ptrs[classnum], &flags);
4769                     }
4770                     if (! (to_complement
4771                            ^ cBOOL(swash_fetch(PL_utf8_swash_ptrs[classnum],
4772                                                (U8 *) locinput, TRUE))))
4773                     {
4774                         sayNO;
4775                     }
4776                 }
4777                 else {  /* Here, uses macros to find above Latin-1 code points */
4778                     switch (classnum) {
4779                         case _CC_ENUM_SPACE:    /* XXX would require separate
4780                                                    code if we revert the change
4781                                                    of \v matching this */
4782                         case _CC_ENUM_PSXSPC:
4783                             if (! (to_complement
4784                                         ^ cBOOL(is_XPERLSPACE_high(locinput))))
4785                             {
4786                                 sayNO;
4787                             }
4788                             break;
4789                         case _CC_ENUM_BLANK:
4790                             if (! (to_complement
4791                                             ^ cBOOL(is_HORIZWS_high(locinput))))
4792                             {
4793                                 sayNO;
4794                             }
4795                             break;
4796                         case _CC_ENUM_XDIGIT:
4797                             if (! (to_complement
4798                                             ^ cBOOL(is_XDIGIT_high(locinput))))
4799                             {
4800                                 sayNO;
4801                             }
4802                             break;
4803                         case _CC_ENUM_VERTSPACE:
4804                             if (! (to_complement
4805                                             ^ cBOOL(is_VERTWS_high(locinput))))
4806                             {
4807                                 sayNO;
4808                             }
4809                             break;
4810                         default:    /* The rest, e.g. [:cntrl:], can't match
4811                                        above Latin1 */
4812                             if (! to_complement) {
4813                                 sayNO;
4814                             }
4815                             break;
4816                     }
4817                 }
4818                 locinput += UTF8SKIP(locinput);
4819             }
4820             break;
4821
4822         case CLUMP: /* Match \X: logical Unicode character.  This is defined as
4823                        a Unicode extended Grapheme Cluster */
4824             /* From http://www.unicode.org/reports/tr29 (5.2 version).  An
4825               extended Grapheme Cluster is:
4826
4827             CR LF
4828             | Prepend* Begin Extend*
4829             | .
4830
4831             Begin is:           ( Special_Begin | ! Control )
4832             Special_Begin is:   ( Regional-Indicator+ | Hangul-syllable )
4833             Extend is:          ( Grapheme_Extend | Spacing_Mark )
4834             Control is:         [ GCB_Control | CR | LF ]
4835             Hangul-syllable is: ( T+ | ( L* ( L | ( LVT | ( V | LV ) V* ) T* ) ))
4836
4837                If we create a 'Regular_Begin' = Begin - Special_Begin, then
4838                we can rewrite
4839
4840                    Begin is ( Regular_Begin + Special Begin )
4841
4842                It turns out that 98.4% of all Unicode code points match
4843                Regular_Begin.  Doing it this way eliminates a table match in
4844                the previous implementation for almost all Unicode code points.
4845
4846                There is a subtlety with Prepend* which showed up in testing.
4847                Note that the Begin, and only the Begin is required in:
4848                 | Prepend* Begin Extend*
4849                Also, Begin contains '! Control'.  A Prepend must be a
4850                '!  Control', which means it must also be a Begin.  What it
4851                comes down to is that if we match Prepend* and then find no
4852                suitable Begin afterwards, that if we backtrack the last
4853                Prepend, that one will be a suitable Begin.
4854             */
4855
4856             if (NEXTCHR_IS_EOS)
4857                 sayNO;
4858             if  (! utf8_target) {
4859
4860                 /* Match either CR LF  or '.', as all the other possibilities
4861                  * require utf8 */
4862                 locinput++;         /* Match the . or CR */
4863                 if (nextchr == '\r' /* And if it was CR, and the next is LF,
4864                                        match the LF */
4865                     && locinput < reginfo->strend
4866                     && UCHARAT(locinput) == '\n')
4867                 {
4868                     locinput++;
4869                 }
4870             }
4871             else {
4872
4873                 /* Utf8: See if is ( CR LF ); already know that locinput <
4874                  * reginfo->strend, so locinput+1 is in bounds */
4875                 if ( nextchr == '\r' && locinput+1 < reginfo->strend
4876                      && UCHARAT(locinput + 1) == '\n')
4877                 {
4878                     locinput += 2;
4879                 }
4880                 else {
4881                     STRLEN len;
4882
4883                     /* In case have to backtrack to beginning, then match '.' */
4884                     char *starting = locinput;
4885
4886                     /* In case have to backtrack the last prepend */
4887                     char *previous_prepend = NULL;
4888
4889                     LOAD_UTF8_CHARCLASS_GCB();
4890
4891                     /* Match (prepend)*   */
4892                     while (locinput < reginfo->strend
4893                            && (len = is_GCB_Prepend_utf8(locinput)))
4894                     {
4895                         previous_prepend = locinput;
4896                         locinput += len;
4897                     }
4898
4899                     /* As noted above, if we matched a prepend character, but
4900                      * the next thing won't match, back off the last prepend we
4901                      * matched, as it is guaranteed to match the begin */
4902                     if (previous_prepend
4903                         && (locinput >=  reginfo->strend
4904                             || (! swash_fetch(PL_utf8_X_regular_begin,
4905                                              (U8*)locinput, utf8_target)
4906                                  && ! is_GCB_SPECIAL_BEGIN_START_utf8(locinput)))
4907                         )
4908                     {
4909                         locinput = previous_prepend;
4910                     }
4911
4912                     /* Note that here we know reginfo->strend > locinput, as we
4913                      * tested that upon input to this switch case, and if we
4914                      * moved locinput forward, we tested the result just above
4915                      * and it either passed, or we backed off so that it will
4916                      * now pass */
4917                     if (swash_fetch(PL_utf8_X_regular_begin,
4918                                     (U8*)locinput, utf8_target)) {
4919                         locinput += UTF8SKIP(locinput);
4920                     }
4921                     else if (! is_GCB_SPECIAL_BEGIN_START_utf8(locinput)) {
4922
4923                         /* Here did not match the required 'Begin' in the
4924                          * second term.  So just match the very first
4925                          * character, the '.' of the final term of the regex */
4926                         locinput = starting + UTF8SKIP(starting);
4927                         goto exit_utf8;
4928                     } else {
4929
4930                         /* Here is a special begin.  It can be composed of
4931                          * several individual characters.  One possibility is
4932                          * RI+ */
4933                         if ((len = is_GCB_RI_utf8(locinput))) {
4934                             locinput += len;
4935                             while (locinput < reginfo->strend
4936                                    && (len = is_GCB_RI_utf8(locinput)))
4937                             {
4938                                 locinput += len;
4939                             }
4940                         } else if ((len = is_GCB_T_utf8(locinput))) {
4941                             /* Another possibility is T+ */
4942                             locinput += len;
4943                             while (locinput < reginfo->strend
4944                                 && (len = is_GCB_T_utf8(locinput)))
4945                             {
4946                                 locinput += len;
4947                             }
4948                         } else {
4949
4950                             /* Here, neither RI+ nor T+; must be some other
4951                              * Hangul.  That means it is one of the others: L,
4952                              * LV, LVT or V, and matches:
4953                              * L* (L | LVT T* | V * V* T* | LV  V* T*) */
4954
4955                             /* Match L*           */
4956                             while (locinput < reginfo->strend
4957                                    && (len = is_GCB_L_utf8(locinput)))
4958                             {
4959                                 locinput += len;
4960                             }
4961
4962                             /* Here, have exhausted L*.  If the next character
4963                              * is not an LV, LVT nor V, it means we had to have
4964                              * at least one L, so matches L+ in the original
4965                              * equation, we have a complete hangul syllable.
4966                              * Are done. */
4967
4968                             if (locinput < reginfo->strend
4969                                 && is_GCB_LV_LVT_V_utf8(locinput))
4970                             {
4971                                 /* Otherwise keep going.  Must be LV, LVT or V.
4972                                  * See if LVT, by first ruling out V, then LV */
4973                                 if (! is_GCB_V_utf8(locinput)
4974                                         /* All but every TCount one is LV */
4975                                     && (valid_utf8_to_uvchr((U8 *) locinput,
4976                                                                          NULL)
4977                                                                         - SBASE)
4978                                         % TCount != 0)
4979                                 {
4980                                     locinput += UTF8SKIP(locinput);
4981                                 } else {
4982
4983                                     /* Must be  V or LV.  Take it, then match
4984                                      * V*     */
4985                                     locinput += UTF8SKIP(locinput);
4986                                     while (locinput < reginfo->strend
4987                                            && (len = is_GCB_V_utf8(locinput)))
4988                                     {
4989                                         locinput += len;
4990                                     }
4991                                 }
4992
4993                                 /* And any of LV, LVT, or V can be followed
4994                                  * by T*            */
4995                                 while (locinput < reginfo->strend
4996                                        && (len = is_GCB_T_utf8(locinput)))
4997                                 {
4998                                     locinput += len;
4999                                 }
5000                             }
5001                         }
5002                     }
5003
5004                     /* Match any extender */
5005                     while (locinput < reginfo->strend
5006                             && swash_fetch(PL_utf8_X_extend,
5007                                             (U8*)locinput, utf8_target))
5008                     {
5009                         locinput += UTF8SKIP(locinput);
5010                     }
5011                 }
5012             exit_utf8:
5013                 if (locinput > reginfo->strend) sayNO;
5014             }
5015             break;
5016             
5017         case NREFFL:  /*  /\g{name}/il  */
5018         {   /* The capture buffer cases.  The ones beginning with N for the
5019                named buffers just convert to the equivalent numbered and
5020                pretend they were called as the corresponding numbered buffer
5021                op.  */
5022             /* don't initialize these in the declaration, it makes C++
5023                unhappy */
5024             const char *s;
5025             char type;
5026             re_fold_t folder;
5027             const U8 *fold_array;
5028             UV utf8_fold_flags;
5029
5030             folder = foldEQ_locale;
5031             fold_array = PL_fold_locale;
5032             type = REFFL;
5033             utf8_fold_flags = FOLDEQ_LOCALE;
5034             goto do_nref;
5035
5036         case NREFFA:  /*  /\g{name}/iaa  */
5037             folder = foldEQ_latin1;
5038             fold_array = PL_fold_latin1;
5039             type = REFFA;
5040             utf8_fold_flags = FOLDEQ_UTF8_NOMIX_ASCII;
5041             goto do_nref;
5042
5043         case NREFFU:  /*  /\g{name}/iu  */
5044             folder = foldEQ_latin1;
5045             fold_array = PL_fold_latin1;
5046             type = REFFU;
5047             utf8_fold_flags = 0;
5048             goto do_nref;
5049
5050         case NREFF:  /*  /\g{name}/i  */
5051             folder = foldEQ;
5052             fold_array = PL_fold;
5053             type = REFF;
5054             utf8_fold_flags = 0;
5055             goto do_nref;
5056
5057         case NREF:  /*  /\g{name}/   */
5058             type = REF;
5059             folder = NULL;
5060             fold_array = NULL;
5061             utf8_fold_flags = 0;
5062           do_nref:
5063
5064             /* For the named back references, find the corresponding buffer
5065              * number */
5066             n = reg_check_named_buff_matched(rex,scan);
5067
5068             if ( ! n ) {
5069                 sayNO;
5070             }
5071             goto do_nref_ref_common;
5072
5073         case REFFL:  /*  /\1/il  */
5074             folder = foldEQ_locale;
5075             fold_array = PL_fold_locale;
5076             utf8_fold_flags = FOLDEQ_LOCALE;
5077             goto do_ref;
5078
5079         case REFFA:  /*  /\1/iaa  */
5080             folder = foldEQ_latin1;
5081             fold_array = PL_fold_latin1;
5082             utf8_fold_flags = FOLDEQ_UTF8_NOMIX_ASCII;
5083             goto do_ref;
5084
5085         case REFFU:  /*  /\1/iu  */
5086             folder = foldEQ_latin1;
5087             fold_array = PL_fold_latin1;
5088             utf8_fold_flags = 0;
5089             goto do_ref;
5090
5091         case REFF:  /*  /\1/i  */
5092             folder = foldEQ;
5093             fold_array = PL_fold;
5094             utf8_fold_flags = 0;
5095             goto do_ref;
5096
5097         case REF:  /*  /\1/    */
5098             folder = NULL;
5099             fold_array = NULL;
5100             utf8_fold_flags = 0;
5101
5102           do_ref:
5103             type = OP(scan);
5104             n = ARG(scan);  /* which paren pair */
5105
5106           do_nref_ref_common:
5107             ln = rex->offs[n].start;
5108             reginfo->poscache_iter = reginfo->poscache_maxiter; /* Void cache */
5109             if (rex->lastparen < n || ln == -1)
5110                 sayNO;                  /* Do not match unless seen CLOSEn. */
5111             if (ln == rex->offs[n].end)
5112                 break;
5113
5114             s = reginfo->strbeg + ln;
5115             if (type != REF     /* REF can do byte comparison */
5116                 && (utf8_target || type == REFFU || type == REFFL))
5117             {
5118                 char * limit = reginfo->strend;
5119
5120                 /* This call case insensitively compares the entire buffer
5121                     * at s, with the current input starting at locinput, but
5122                     * not going off the end given by reginfo->strend, and
5123                     * returns in <limit> upon success, how much of the
5124                     * current input was matched */
5125                 if (! foldEQ_utf8_flags(s, NULL, rex->offs[n].end - ln, utf8_target,
5126                                     locinput, &limit, 0, utf8_target, utf8_fold_flags))
5127                 {
5128                     sayNO;
5129                 }
5130                 locinput = limit;
5131                 break;
5132             }
5133
5134             /* Not utf8:  Inline the first character, for speed. */
5135             if (!NEXTCHR_IS_EOS &&
5136                 UCHARAT(s) != nextchr &&
5137                 (type == REF ||
5138                  UCHARAT(s) != fold_array[nextchr]))
5139                 sayNO;
5140             ln = rex->offs[n].end - ln;
5141             if (locinput + ln > reginfo->strend)
5142                 sayNO;
5143             if (ln > 1 && (type == REF
5144                            ? memNE(s, locinput, ln)
5145                            : ! folder(s, locinput, ln)))
5146                 sayNO;
5147             locinput += ln;
5148             break;
5149         }
5150
5151         case NOTHING: /* null op; e.g. the 'nothing' following
5152                        * the '*' in m{(a+|b)*}' */
5153             break;
5154         case TAIL: /* placeholder while compiling (A|B|C) */
5155             break;
5156
5157         case BACK: /* ??? doesn't appear to be used ??? */
5158             break;
5159
5160 #undef  ST
5161 #define ST st->u.eval
5162         {
5163             SV *ret;
5164             REGEXP *re_sv;
5165             regexp *re;
5166             regexp_internal *rei;
5167             regnode *startpoint;
5168
5169         case GOSTART: /*  (?R)  */
5170         case GOSUB: /*    /(...(?1))/   /(...(?&foo))/   */
5171             if (cur_eval && cur_eval->locinput==locinput) {
5172                 if (cur_eval->u.eval.close_paren == (U32)ARG(scan)) 
5173                     Perl_croak(aTHX_ "Infinite recursion in regex");
5174                 if ( ++nochange_depth > max_nochange_depth )
5175                     Perl_croak(aTHX_ 
5176                         "Pattern subroutine nesting without pos change"
5177                         " exceeded limit in regex");
5178             } else {
5179                 nochange_depth = 0;
5180             }
5181             re_sv = rex_sv;
5182             re = rex;
5183             rei = rexi;
5184             if (OP(scan)==GOSUB) {
5185                 startpoint = scan + ARG2L(scan);
5186                 ST.close_paren = ARG(scan);
5187             } else {
5188                 startpoint = rei->program+1;
5189                 ST.close_paren = 0;
5190             }
5191
5192             /* Save all the positions seen so far. */
5193             ST.cp = regcppush(rex, 0, maxopenparen);
5194             REGCP_SET(ST.lastcp);
5195
5196             /* and then jump to the code we share with EVAL */
5197             goto eval_recurse_doit;
5198
5199             assert(0); /* NOTREACHED */
5200
5201         case EVAL:  /*   /(?{A})B/   /(??{A})B/  and /(?(?{A})X|Y)B/   */        
5202             if (cur_eval && cur_eval->locinput==locinput) {
5203                 if ( ++nochange_depth > max_nochange_depth )
5204                     Perl_croak(aTHX_ "EVAL without pos change exceeded limit in regex");
5205             } else {
5206                 nochange_depth = 0;
5207             }    
5208             {
5209                 /* execute the code in the {...} */
5210
5211                 dSP;
5212                 IV before;
5213                 OP * const oop = PL_op;
5214                 COP * const ocurcop = PL_curcop;
5215                 OP *nop;
5216                 CV *newcv;
5217
5218                 /* save *all* paren positions */
5219                 regcppush(rex, 0, maxopenparen);
5220                 REGCP_SET(runops_cp);
5221
5222                 if (!caller_cv)
5223                     caller_cv = find_runcv(NULL);
5224
5225                 n = ARG(scan);
5226
5227                 if (rexi->data->what[n] == 'r') { /* code from an external qr */
5228                     newcv = (ReANY(
5229                                                 (REGEXP*)(rexi->data->data[n])
5230                                             ))->qr_anoncv
5231                                         ;
5232                     nop = (OP*)rexi->data->data[n+1];
5233                 }
5234                 else if (rexi->data->what[n] == 'l') { /* literal code */
5235                     newcv = caller_cv;
5236                     nop = (OP*)rexi->data->data[n];
5237                     assert(CvDEPTH(newcv));
5238                 }
5239                 else {
5240                     /* literal with own CV */
5241                     assert(rexi->data->what[n] == 'L');
5242                     newcv = rex->qr_anoncv;
5243                     nop = (OP*)rexi->data->data[n];
5244                 }
5245
5246                 /* normally if we're about to execute code from the same
5247                  * CV that we used previously, we just use the existing
5248                  * CX stack entry. However, its possible that in the
5249                  * meantime we may have backtracked, popped from the save
5250                  * stack, and undone the SAVECOMPPAD(s) associated with
5251                  * PUSH_MULTICALL; in which case PL_comppad no longer
5252                  * points to newcv's pad. */
5253                 if (newcv != last_pushed_cv || PL_comppad != last_pad)
5254                 {
5255                     U8 flags = (CXp_SUB_RE |
5256                                 ((newcv == caller_cv) ? CXp_SUB_RE_FAKE : 0));
5257                     if (last_pushed_cv) {
5258                         CHANGE_MULTICALL_FLAGS(newcv, flags);
5259                     }
5260                     else {
5261                         PUSH_MULTICALL_FLAGS(newcv, flags);
5262                     }
5263                     last_pushed_cv = newcv;
5264                 }
5265                 else {
5266                     /* these assignments are just to silence compiler
5267                      * warnings */
5268                     multicall_cop = NULL;
5269                     newsp = NULL;
5270                 }
5271                 last_pad = PL_comppad;
5272
5273                 /* the initial nextstate you would normally execute
5274                  * at the start of an eval (which would cause error
5275                  * messages to come from the eval), may be optimised
5276                  * away from the execution path in the regex code blocks;
5277                  * so manually set PL_curcop to it initially */
5278                 {
5279                     OP *o = cUNOPx(nop)->op_first;
5280                     assert(o->op_type == OP_NULL);
5281                     if (o->op_targ == OP_SCOPE) {
5282                         o = cUNOPo->op_first;
5283                     }
5284                     else {
5285                         assert(o->op_targ == OP_LEAVE);
5286                         o = cUNOPo->op_first;
5287                         assert(o->op_type == OP_ENTER);
5288                         o = o->op_sibling;
5289                     }
5290
5291                     if (o->op_type != OP_STUB) {
5292                         assert(    o->op_type == OP_NEXTSTATE
5293                                 || o->op_type == OP_DBSTATE
5294                                 || (o->op_type == OP_NULL
5295                                     &&  (  o->op_targ == OP_NEXTSTATE
5296                                         || o->op_targ == OP_DBSTATE
5297                                         )
5298                                     )
5299                         );
5300                         PL_curcop = (COP*)o;
5301                     }
5302                 }
5303                 nop = nop->op_next;
5304
5305                 DEBUG_STATE_r( PerlIO_printf(Perl_debug_log, 
5306                     "  re EVAL PL_op=0x%"UVxf"\n", PTR2UV(nop)) );
5307
5308                 rex->offs[0].end = locinput - reginfo->strbeg;
5309                 if (reginfo->info_aux_eval->pos_magic)
5310                     MgBYTEPOS_set(reginfo->info_aux_eval->pos_magic,
5311                                   reginfo->sv, reginfo->strbeg,
5312                                   locinput - reginfo->strbeg);
5313
5314                 if (sv_yes_mark) {
5315                     SV *sv_mrk = get_sv("REGMARK", 1);
5316                     sv_setsv(sv_mrk, sv_yes_mark);
5317                 }
5318
5319                 /* we don't use MULTICALL here as we want to call the
5320                  * first op of the block of interest, rather than the
5321                  * first op of the sub */
5322                 before = (IV)(SP-PL_stack_base);
5323                 PL_op = nop;
5324                 CALLRUNOPS(aTHX);                       /* Scalar context. */
5325                 SPAGAIN;
5326                 if ((IV)(SP-PL_stack_base) == before)
5327                     ret = &PL_sv_undef;   /* protect against empty (?{}) blocks. */
5328                 else {
5329                     ret = POPs;
5330                     PUTBACK;
5331                 }
5332
5333                 /* before restoring everything, evaluate the returned
5334                  * value, so that 'uninit' warnings don't use the wrong
5335                  * PL_op or pad. Also need to process any magic vars
5336                  * (e.g. $1) *before* parentheses are restored */
5337
5338                 PL_op = NULL;
5339
5340                 re_sv = NULL;
5341                 if (logical == 0)        /*   (?{})/   */
5342                     sv_setsv(save_scalar(PL_replgv), ret); /* $^R */
5343                 else if (logical == 1) { /*   /(?(?{...})X|Y)/    */
5344                     sw = cBOOL(SvTRUE(ret));
5345                     logical = 0;
5346                 }
5347                 else {                   /*  /(??{})  */
5348                     /*  if its overloaded, let the regex compiler handle
5349                      *  it; otherwise extract regex, or stringify  */
5350                     if (SvGMAGICAL(ret))
5351                         ret = sv_mortalcopy(ret);
5352                     if (!SvAMAGIC(ret)) {
5353                         SV *sv = ret;
5354                         if (SvROK(sv))
5355                             sv = SvRV(sv);
5356                         if (SvTYPE(sv) == SVt_REGEXP)
5357                             re_sv = (REGEXP*) sv;
5358                         else if (SvSMAGICAL(ret)) {
5359                             MAGIC *mg = mg_find(ret, PERL_MAGIC_qr);
5360                             if (mg)
5361                                 re_sv = (REGEXP *) mg->mg_obj;
5362                         }
5363
5364                         /* force any undef warnings here */
5365                         if (!re_sv && !SvPOK(ret) && !SvNIOK(ret)) {
5366                             ret = sv_mortalcopy(ret);
5367                             (void) SvPV_force_nolen(ret);
5368                         }
5369                     }
5370
5371                 }
5372
5373                 /* *** Note that at this point we don't restore
5374                  * PL_comppad, (or pop the CxSUB) on the assumption it may
5375                  * be used again soon. This is safe as long as nothing
5376                  * in the regexp code uses the pad ! */
5377                 PL_op = oop;
5378                 PL_curcop = ocurcop;
5379                 S_regcp_restore(aTHX_ rex, runops_cp, &maxopenparen);
5380                 PL_curpm = PL_reg_curpm;
5381
5382                 if (logical != 2)
5383                     break;
5384             }
5385
5386                 /* only /(??{})/  from now on */
5387                 logical = 0;
5388                 {
5389                     /* extract RE object from returned value; compiling if
5390                      * necessary */
5391
5392                     if (re_sv) {
5393                         re_sv = reg_temp_copy(NULL, re_sv);
5394                     }
5395                     else {
5396                         U32 pm_flags = 0;
5397
5398                         if (SvUTF8(ret) && IN_BYTES) {
5399                             /* In use 'bytes': make a copy of the octet
5400                              * sequence, but without the flag on */
5401                             STRLEN len;
5402                             const char *const p = SvPV(ret, len);
5403                             ret = newSVpvn_flags(p, len, SVs_TEMP);
5404                         }
5405                         if (rex->intflags & PREGf_USE_RE_EVAL)
5406                             pm_flags |= PMf_USE_RE_EVAL;
5407
5408                         /* if we got here, it should be an engine which
5409                          * supports compiling code blocks and stuff */
5410                         assert(rex->engine && rex->engine->op_comp);
5411                         assert(!(scan->flags & ~RXf_PMf_COMPILETIME));
5412                         re_sv = rex->engine->op_comp(aTHX_ &ret, 1, NULL,
5413                                     rex->engine, NULL, NULL,
5414                                     /* copy /msix etc to inner pattern */
5415                                     scan->flags,
5416                                     pm_flags);
5417
5418                         if (!(SvFLAGS(ret)
5419                               & (SVs_TEMP | SVs_GMG | SVf_ROK))
5420                          && (!SvPADTMP(ret) || SvREADONLY(ret))) {
5421                             /* This isn't a first class regexp. Instead, it's
5422                                caching a regexp onto an existing, Perl visible
5423                                scalar.  */
5424                             sv_magic(ret, MUTABLE_SV(re_sv), PERL_MAGIC_qr, 0, 0);
5425                         }
5426                     }
5427                     SAVEFREESV(re_sv);
5428                     re = ReANY(re_sv);
5429                 }
5430                 RXp_MATCH_COPIED_off(re);
5431                 re->subbeg = rex->subbeg;
5432                 re->sublen = rex->sublen;
5433                 re->suboffset = rex->suboffset;
5434                 re->subcoffset = rex->subcoffset;
5435                 re->lastparen = 0;
5436                 re->lastcloseparen = 0;
5437                 rei = RXi_GET(re);
5438                 DEBUG_EXECUTE_r(
5439                     debug_start_match(re_sv, utf8_target, locinput,
5440                                     reginfo->strend, "Matching embedded");
5441                 );              
5442                 startpoint = rei->program + 1;
5443                 ST.close_paren = 0; /* only used for GOSUB */
5444                 /* Save all the seen positions so far. */
5445                 ST.cp = regcppush(rex, 0, maxopenparen);
5446                 REGCP_SET(ST.lastcp);
5447                 /* and set maxopenparen to 0, since we are starting a "fresh" match */
5448                 maxopenparen = 0;
5449                 /* run the pattern returned from (??{...}) */
5450
5451         eval_recurse_doit: /* Share code with GOSUB below this line
5452                             * At this point we expect the stack context to be
5453                             * set up correctly */
5454
5455                 /* invalidate the S-L poscache. We're now executing a
5456                  * different set of WHILEM ops (and their associated
5457                  * indexes) against the same string, so the bits in the
5458                  * cache are meaningless. Setting maxiter to zero forces
5459                  * the cache to be invalidated and zeroed before reuse.
5460                  * XXX This is too dramatic a measure. Ideally we should
5461                  * save the old cache and restore when running the outer
5462                  * pattern again */
5463                 reginfo->poscache_maxiter = 0;
5464
5465                 /* the new regexp might have a different is_utf8_pat than we do */
5466                 is_utf8_pat = reginfo->is_utf8_pat = cBOOL(RX_UTF8(re_sv));
5467
5468                 ST.prev_rex = rex_sv;
5469                 ST.prev_curlyx = cur_curlyx;
5470                 rex_sv = re_sv;
5471                 SET_reg_curpm(rex_sv);
5472                 rex = re;
5473                 rexi = rei;
5474                 cur_curlyx = NULL;
5475                 ST.B = next;
5476                 ST.prev_eval = cur_eval;
5477                 cur_eval = st;
5478                 /* now continue from first node in postoned RE */
5479                 PUSH_YES_STATE_GOTO(EVAL_AB, startpoint, locinput);
5480                 assert(0); /* NOTREACHED */
5481         }
5482
5483         case EVAL_AB: /* cleanup after a successful (??{A})B */
5484             /* note: this is called twice; first after popping B, then A */
5485             rex_sv = ST.prev_rex;
5486             is_utf8_pat = reginfo->is_utf8_pat = cBOOL(RX_UTF8(rex_sv));
5487             SET_reg_curpm(rex_sv);
5488             rex = ReANY(rex_sv);
5489             rexi = RXi_GET(rex);
5490             {
5491                 /* preserve $^R across LEAVE's. See Bug 121070. */
5492                 SV *save_sv= GvSV(PL_replgv);
5493                 SvREFCNT_inc(save_sv);
5494                 regcpblow(ST.cp); /* LEAVE in disguise */
5495                 sv_setsv(GvSV(PL_replgv), save_sv);
5496                 SvREFCNT_dec(save_sv);
5497             }
5498             cur_eval = ST.prev_eval;
5499             cur_curlyx = ST.prev_curlyx;
5500
5501             /* Invalidate cache. See "invalidate" comment above. */
5502             reginfo->poscache_maxiter = 0;
5503             if ( nochange_depth )
5504                 nochange_depth--;
5505             sayYES;
5506
5507
5508         case EVAL_AB_fail: /* unsuccessfully ran A or B in (??{A})B */
5509             /* note: this is called twice; first after popping B, then A */
5510             rex_sv = ST.prev_rex;
5511             is_utf8_pat = reginfo->is_utf8_pat = cBOOL(RX_UTF8(rex_sv));
5512             SET_reg_curpm(rex_sv);
5513             rex = ReANY(rex_sv);
5514             rexi = RXi_GET(rex); 
5515
5516             REGCP_UNWIND(ST.lastcp);
5517             regcppop(rex, &maxopenparen);
5518             cur_eval = ST.prev_eval;
5519             cur_curlyx = ST.prev_curlyx;
5520             /* Invalidate cache. See "invalidate" comment above. */
5521             reginfo->poscache_maxiter = 0;
5522             if ( nochange_depth )
5523                 nochange_depth--;
5524             sayNO_SILENT;
5525 #undef ST
5526
5527         case OPEN: /*  (  */
5528             n = ARG(scan);  /* which paren pair */
5529             rex->offs[n].start_tmp = locinput - reginfo->strbeg;
5530             if (n > maxopenparen)
5531                 maxopenparen = n;
5532             DEBUG_BUFFERS_r(PerlIO_printf(Perl_debug_log,
5533                 "rex=0x%"UVxf" offs=0x%"UVxf": \\%"UVuf": set %"IVdf" tmp; maxopenparen=%"UVuf"\n",
5534                 PTR2UV(rex),
5535                 PTR2UV(rex->offs),
5536                 (UV)n,
5537                 (IV)rex->offs[n].start_tmp,
5538                 (UV)maxopenparen
5539             ));
5540             lastopen = n;
5541             break;
5542
5543 /* XXX really need to log other places start/end are set too */
5544 #define CLOSE_CAPTURE \
5545     rex->offs[n].start = rex->offs[n].start_tmp; \
5546     rex->offs[n].end = locinput - reginfo->strbeg; \
5547     DEBUG_BUFFERS_r(PerlIO_printf(Perl_debug_log, \
5548         "rex=0x%"UVxf" offs=0x%"UVxf": \\%"UVuf": set %"IVdf"..%"IVdf"\n", \
5549         PTR2UV(rex), \
5550         PTR2UV(rex->offs), \
5551         (UV)n, \
5552         (IV)rex->offs[n].start, \
5553         (IV)rex->offs[n].end \
5554     ))
5555
5556         case CLOSE:  /*  )  */
5557             n = ARG(scan);  /* which paren pair */
5558             CLOSE_CAPTURE;
5559             if (n > rex->lastparen)
5560                 rex->lastparen = n;
5561             rex->lastcloseparen = n;
5562             if (cur_eval && cur_eval->u.eval.close_paren == n) {
5563                 goto fake_end;
5564             }    
5565             break;
5566
5567         case ACCEPT:  /*  (*ACCEPT)  */
5568             if (ARG(scan)){
5569                 regnode *cursor;
5570                 for (cursor=scan;
5571                      cursor && OP(cursor)!=END; 
5572                      cursor=regnext(cursor)) 
5573                 {
5574                     if ( OP(cursor)==CLOSE ){
5575                         n = ARG(cursor);
5576                         if ( n <= lastopen ) {
5577                             CLOSE_CAPTURE;
5578                             if (n > rex->lastparen)
5579                                 rex->lastparen = n;
5580                             rex->lastcloseparen = n;
5581                             if ( n == ARG(scan) || (cur_eval &&
5582                                 cur_eval->u.eval.close_paren == n))
5583                                 break;
5584                         }
5585                     }
5586                 }
5587             }
5588             goto fake_end;
5589             /*NOTREACHED*/          
5590
5591         case GROUPP:  /*  (?(1))  */
5592             n = ARG(scan);  /* which paren pair */
5593             sw = cBOOL(rex->lastparen >= n && rex->offs[n].end != -1);
5594             break;
5595
5596         case NGROUPP:  /*  (?(<name>))  */
5597             /* reg_check_named_buff_matched returns 0 for no match */
5598             sw = cBOOL(0 < reg_check_named_buff_matched(rex,scan));
5599             break;
5600
5601         case INSUBP:   /*  (?(R))  */
5602             n = ARG(scan);
5603             sw = (cur_eval && (!n || cur_eval->u.eval.close_paren == n));
5604             break;
5605
5606         case DEFINEP:  /*  (?(DEFINE))  */
5607             sw = 0;
5608             break;
5609
5610         case IFTHEN:   /*  (?(cond)A|B)  */
5611             reginfo->poscache_iter = reginfo->poscache_maxiter; /* Void cache */
5612             if (sw)
5613                 next = NEXTOPER(NEXTOPER(scan));
5614             else {
5615                 next = scan + ARG(scan);
5616                 if (OP(next) == IFTHEN) /* Fake one. */
5617                     next = NEXTOPER(NEXTOPER(next));
5618             }
5619             break;
5620
5621         case LOGICAL:  /* modifier for EVAL and IFMATCH */
5622             logical = scan->flags;
5623             break;
5624
5625 /*******************************************************************
5626
5627 The CURLYX/WHILEM pair of ops handle the most generic case of the /A*B/
5628 pattern, where A and B are subpatterns. (For simple A, CURLYM or
5629 STAR/PLUS/CURLY/CURLYN are used instead.)
5630
5631 A*B is compiled as <CURLYX><A><WHILEM><B>
5632
5633 On entry to the subpattern, CURLYX is called. This pushes a CURLYX
5634 state, which contains the current count, initialised to -1. It also sets
5635 cur_curlyx to point to this state, with any previous value saved in the
5636 state block.
5637
5638 CURLYX then jumps straight to the WHILEM op, rather than executing A,
5639 since the pattern may possibly match zero times (i.e. it's a while {} loop
5640 rather than a do {} while loop).
5641
5642 Each entry to WHILEM represents a successful match of A. The count in the
5643 CURLYX block is incremented, another WHILEM state is pushed, and execution
5644 passes to A or B depending on greediness and the current count.
5645
5646 For example, if matching against the string a1a2a3b (where the aN are
5647 substrings that match /A/), then the match progresses as follows: (the
5648 pushed states are interspersed with the bits of strings matched so far):
5649
5650     <CURLYX cnt=-1>
5651     <CURLYX cnt=0><WHILEM>
5652     <CURLYX cnt=1><WHILEM> a1 <WHILEM>
5653     <CURLYX cnt=2><WHILEM> a1 <WHILEM> a2 <WHILEM>
5654     <CURLYX cnt=3><WHILEM> a1 <WHILEM> a2 <WHILEM> a3 <WHILEM>
5655     <CURLYX cnt=3><WHILEM> a1 <WHILEM> a2 <WHILEM> a3 <WHILEM> b
5656
5657 (Contrast this with something like CURLYM, which maintains only a single
5658 backtrack state:
5659
5660     <CURLYM cnt=0> a1
5661     a1 <CURLYM cnt=1> a2
5662     a1 a2 <CURLYM cnt=2> a3
5663     a1 a2 a3 <CURLYM cnt=3> b
5664 )
5665
5666 Each WHILEM state block marks a point to backtrack to upon partial failure
5667 of A or B, and also contains some minor state data related to that
5668 iteration.  The CURLYX block, pointed to by cur_curlyx, contains the
5669 overall state, such as the count, and pointers to the A and B ops.
5670
5671 This is complicated slightly by nested CURLYX/WHILEM's. Since cur_curlyx
5672 must always point to the *current* CURLYX block, the rules are:
5673
5674 When executing CURLYX, save the old cur_curlyx in the CURLYX state block,
5675 and set cur_curlyx to point the new block.
5676
5677 When popping the CURLYX block after a successful or unsuccessful match,
5678 restore the previous cur_curlyx.
5679
5680 When WHILEM is about to execute B, save the current cur_curlyx, and set it
5681 to the outer one saved in the CURLYX block.
5682
5683 When popping the WHILEM block after a successful or unsuccessful B match,
5684 restore the previous cur_curlyx.
5685
5686 Here's an example for the pattern (AI* BI)*BO
5687 I and O refer to inner and outer, C and W refer to CURLYX and WHILEM:
5688
5689 cur_
5690 curlyx backtrack stack
5691 ------ ---------------
5692 NULL   
5693 CO     <CO prev=NULL> <WO>
5694 CI     <CO prev=NULL> <WO> <CI prev=CO> <WI> ai 
5695 CO     <CO prev=NULL> <WO> <CI prev=CO> <WI> ai <WI prev=CI> bi 
5696 NULL   <CO prev=NULL> <WO> <CI prev=CO> <WI> ai <WI prev=CI> bi <WO prev=CO> bo
5697
5698 At this point the pattern succeeds, and we work back down the stack to
5699 clean up, restoring as we go:
5700
5701 CO     <CO prev=NULL> <WO> <CI prev=CO> <WI> ai <WI prev=CI> bi 
5702 CI     <CO prev=NULL> <WO> <CI prev=CO> <WI> ai 
5703 CO     <CO prev=NULL> <WO>
5704 NULL   
5705
5706 *******************************************************************/
5707
5708 #define ST st->u.curlyx
5709
5710         case CURLYX:    /* start of /A*B/  (for complex A) */
5711         {
5712             /* No need to save/restore up to this paren */
5713             I32 parenfloor = scan->flags;
5714             
5715             assert(next); /* keep Coverity happy */
5716             if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
5717                 next += ARG(next);
5718
5719             /* XXXX Probably it is better to teach regpush to support
5720                parenfloor > maxopenparen ... */
5721             if (parenfloor > (I32)rex->lastparen)
5722                 parenfloor = rex->lastparen; /* Pessimization... */
5723
5724             ST.prev_curlyx= cur_curlyx;
5725             cur_curlyx = st;
5726             ST.cp = PL_savestack_ix;
5727
5728             /* these fields contain the state of the current curly.
5729              * they are accessed by subsequent WHILEMs */
5730             ST.parenfloor = parenfloor;
5731             ST.me = scan;
5732             ST.B = next;
5733             ST.minmod = minmod;
5734             minmod = 0;
5735             ST.count = -1;      /* this will be updated by WHILEM */
5736             ST.lastloc = NULL;  /* this will be updated by WHILEM */
5737
5738             PUSH_YES_STATE_GOTO(CURLYX_end, PREVOPER(next), locinput);
5739             assert(0); /* NOTREACHED */
5740         }
5741
5742         case CURLYX_end: /* just finished matching all of A*B */
5743             cur_curlyx = ST.prev_curlyx;
5744             sayYES;
5745             assert(0); /* NOTREACHED */
5746
5747         case CURLYX_end_fail: /* just failed to match all of A*B */
5748             regcpblow(ST.cp);
5749             cur_curlyx = ST.prev_curlyx;
5750             sayNO;
5751             assert(0); /* NOTREACHED */
5752
5753
5754 #undef ST
5755 #define ST st->u.whilem
5756
5757         case WHILEM:     /* just matched an A in /A*B/  (for complex A) */
5758         {
5759             /* see the discussion above about CURLYX/WHILEM */
5760             I32 n;
5761             int min = ARG1(cur_curlyx->u.curlyx.me);
5762             int max = ARG2(cur_curlyx->u.curlyx.me);
5763             regnode *A = NEXTOPER(cur_curlyx->u.curlyx.me) + EXTRA_STEP_2ARGS;
5764
5765             assert(cur_curlyx); /* keep Coverity happy */
5766             n = ++cur_curlyx->u.curlyx.count; /* how many A's matched */
5767             ST.save_lastloc = cur_curlyx->u.curlyx.lastloc;
5768             ST.cache_offset = 0;
5769             ST.cache_mask = 0;
5770             
5771
5772             DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
5773                   "%*s  whilem: matched %ld out of %d..%d\n",
5774                   REPORT_CODE_OFF+depth*2, "", (long)n, min, max)
5775             );
5776
5777             /* First just match a string of min A's. */
5778
5779             if (n < min) {
5780                 ST.cp = regcppush(rex, cur_curlyx->u.curlyx.parenfloor,
5781                                     maxopenparen);
5782                 cur_curlyx->u.curlyx.lastloc = locinput;
5783                 REGCP_SET(ST.lastcp);
5784
5785                 PUSH_STATE_GOTO(WHILEM_A_pre, A, locinput);
5786                 assert(0); /* NOTREACHED */
5787             }
5788
5789             /* If degenerate A matches "", assume A done. */
5790
5791             if (locinput == cur_curlyx->u.curlyx.lastloc) {
5792                 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
5793                    "%*s  whilem: empty match detected, trying continuation...\n",
5794                    REPORT_CODE_OFF+depth*2, "")
5795                 );
5796                 goto do_whilem_B_max;
5797             }
5798
5799             /* super-linear cache processing.
5800              *
5801              * The idea here is that for certain types of CURLYX/WHILEM -
5802              * principally those whose upper bound is infinity (and
5803              * excluding regexes that have things like \1 and other very
5804              * non-regular expresssiony things), then if a pattern like
5805              * /....A*.../ fails and we backtrack to the WHILEM, then we
5806              * make a note that this particular WHILEM op was at string
5807              * position 47 (say) when the rest of pattern failed. Then, if
5808              * we ever find ourselves back at that WHILEM, and at string
5809              * position 47 again, we can just fail immediately rather than
5810              * running the rest of the pattern again.
5811              *
5812              * This is very handy when patterns start to go
5813              * 'super-linear', like in (a+)*(a+)*(a+)*, where you end up
5814              * with a combinatorial explosion of backtracking.
5815              *
5816              * The cache is implemented as a bit array, with one bit per
5817              * string byte position per WHILEM op (up to 16) - so its
5818              * between 0.25 and 2x the string size.
5819              *
5820              * To avoid allocating a poscache buffer every time, we do an
5821              * initially countdown; only after we have  executed a WHILEM
5822              * op (string-length x #WHILEMs) times do we allocate the
5823              * cache.
5824              *
5825              * The top 4 bits of scan->flags byte say how many different
5826              * relevant CURLLYX/WHILEM op pairs there are, while the
5827              * bottom 4-bits is the identifying index number of this
5828              * WHILEM.
5829              */
5830
5831             if (scan->flags) {
5832
5833                 if (!reginfo->poscache_maxiter) {
5834                     /* start the countdown: Postpone detection until we
5835                      * know the match is not *that* much linear. */
5836                     reginfo->poscache_maxiter
5837                         =    (reginfo->strend - reginfo->strbeg + 1)
5838                            * (scan->flags>>4);
5839                     /* possible overflow for long strings and many CURLYX's */
5840                     if (reginfo->poscache_maxiter < 0)
5841                         reginfo->poscache_maxiter = I32_MAX;
5842                     reginfo->poscache_iter = reginfo->poscache_maxiter;
5843                 }
5844
5845                 if (reginfo->poscache_iter-- == 0) {
5846                     /* initialise cache */
5847                     const SSize_t size = (reginfo->poscache_maxiter + 7)/8;
5848                     regmatch_info_aux *const aux = reginfo->info_aux;
5849                     if (aux->poscache) {
5850                         if ((SSize_t)reginfo->poscache_size < size) {
5851                             Renew(aux->poscache, size, char);
5852                             reginfo->poscache_size = size;
5853                         }
5854                         Zero(aux->poscache, size, char);
5855                     }
5856                     else {
5857                         reginfo->poscache_size = size;
5858                         Newxz(aux->poscache, size, char);
5859                     }
5860                     DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
5861       "%swhilem: Detected a super-linear match, switching on caching%s...\n",
5862                               PL_colors[4], PL_colors[5])
5863                     );
5864                 }
5865
5866                 if (reginfo->poscache_iter < 0) {
5867                     /* have we already failed at this position? */
5868                     SSize_t offset, mask;
5869
5870                     reginfo->poscache_iter = -1; /* stop eventual underflow */
5871                     offset  = (scan->flags & 0xf) - 1
5872                                 +   (locinput - reginfo->strbeg)
5873                                   * (scan->flags>>4);
5874                     mask    = 1 << (offset % 8);
5875                     offset /= 8;
5876                     if (reginfo->info_aux->poscache[offset] & mask) {
5877                         DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
5878                             "%*s  whilem: (cache) already tried at this position...\n",
5879                             REPORT_CODE_OFF+depth*2, "")
5880                         );
5881                         sayNO; /* cache records failure */
5882                     }
5883                     ST.cache_offset = offset;
5884                     ST.cache_mask   = mask;
5885                 }
5886             }
5887
5888             /* Prefer B over A for minimal matching. */
5889
5890             if (cur_curlyx->u.curlyx.minmod) {
5891                 ST.save_curlyx = cur_curlyx;
5892                 cur_curlyx = cur_curlyx->u.curlyx.prev_curlyx;
5893                 ST.cp = regcppush(rex, ST.save_curlyx->u.curlyx.parenfloor,
5894                             maxopenparen);
5895                 REGCP_SET(ST.lastcp);
5896                 PUSH_YES_STATE_GOTO(WHILEM_B_min, ST.save_curlyx->u.curlyx.B,
5897                                     locinput);
5898                 assert(0); /* NOTREACHED */
5899             }
5900
5901             /* Prefer A over B for maximal matching. */
5902
5903             if (n < max) { /* More greed allowed? */
5904                 ST.cp = regcppush(rex, cur_curlyx->u.curlyx.parenfloor,
5905                             maxopenparen);
5906                 cur_curlyx->u.curlyx.lastloc = locinput;
5907                 REGCP_SET(ST.lastcp);
5908                 PUSH_STATE_GOTO(WHILEM_A_max, A, locinput);
5909                 assert(0); /* NOTREACHED */
5910             }
5911             goto do_whilem_B_max;
5912         }
5913         assert(0); /* NOTREACHED */
5914
5915         case WHILEM_B_min: /* just matched B in a minimal match */
5916         case WHILEM_B_max: /* just matched B in a maximal match */
5917             cur_curlyx = ST.save_curlyx;
5918             sayYES;
5919             assert(0); /* NOTREACHED */
5920
5921         case WHILEM_B_max_fail: /* just failed to match B in a maximal match */
5922             cur_curlyx = ST.save_curlyx;
5923             cur_curlyx->u.curlyx.lastloc = ST.save_lastloc;
5924             cur_curlyx->u.curlyx.count--;
5925             CACHEsayNO;
5926             assert(0); /* NOTREACHED */
5927
5928         case WHILEM_A_min_fail: /* just failed to match A in a minimal match */
5929             /* FALLTHROUGH */
5930         case WHILEM_A_pre_fail: /* just failed to match even minimal A */
5931             REGCP_UNWIND(ST.lastcp);
5932             regcppop(rex, &maxopenparen);
5933             cur_curlyx->u.curlyx.lastloc = ST.save_lastloc;
5934             cur_curlyx->u.curlyx.count--;
5935             CACHEsayNO;
5936             assert(0); /* NOTREACHED */
5937
5938         case WHILEM_A_max_fail: /* just failed to match A in a maximal match */
5939             REGCP_UNWIND(ST.lastcp);
5940             regcppop(rex, &maxopenparen); /* Restore some previous $<digit>s? */
5941             DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
5942                 "%*s  whilem: failed, trying continuation...\n",
5943                 REPORT_CODE_OFF+depth*2, "")
5944             );
5945           do_whilem_B_max:
5946             if (cur_curlyx->u.curlyx.count >= REG_INFTY
5947                 && ckWARN(WARN_REGEXP)
5948                 && !reginfo->warned)
5949             {
5950                 reginfo->warned = TRUE;
5951                 Perl_warner(aTHX_ packWARN(WARN_REGEXP),
5952                      "Complex regular subexpression recursion limit (%d) "
5953                      "exceeded",
5954                      REG_INFTY - 1);
5955             }
5956
5957             /* now try B */
5958             ST.save_curlyx = cur_curlyx;
5959             cur_curlyx = cur_curlyx->u.curlyx.prev_curlyx;
5960             PUSH_YES_STATE_GOTO(WHILEM_B_max, ST.save_curlyx->u.curlyx.B,
5961                                 locinput);
5962             assert(0); /* NOTREACHED */
5963
5964         case WHILEM_B_min_fail: /* just failed to match B in a minimal match */
5965             cur_curlyx = ST.save_curlyx;
5966             REGCP_UNWIND(ST.lastcp);
5967             regcppop(rex, &maxopenparen);
5968
5969             if (cur_curlyx->u.curlyx.count >= /*max*/ARG2(cur_curlyx->u.curlyx.me)) {
5970                 /* Maximum greed exceeded */
5971                 if (cur_curlyx->u.curlyx.count >= REG_INFTY
5972                     && ckWARN(WARN_REGEXP)
5973                     && !reginfo->warned)
5974                 {
5975                     reginfo->warned     = TRUE;
5976                     Perl_warner(aTHX_ packWARN(WARN_REGEXP),
5977                         "Complex regular subexpression recursion "
5978                         "limit (%d) exceeded",
5979                         REG_INFTY - 1);
5980                 }
5981                 cur_curlyx->u.curlyx.count--;
5982                 CACHEsayNO;
5983             }
5984
5985             DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
5986                 "%*s  trying longer...\n", REPORT_CODE_OFF+depth*2, "")
5987             );
5988             /* Try grabbing another A and see if it helps. */
5989             cur_curlyx->u.curlyx.lastloc = locinput;
5990             ST.cp = regcppush(rex, cur_curlyx->u.curlyx.parenfloor,
5991                             maxopenparen);
5992             REGCP_SET(ST.lastcp);
5993             PUSH_STATE_GOTO(WHILEM_A_min,
5994                 /*A*/ NEXTOPER(ST.save_curlyx->u.curlyx.me) + EXTRA_STEP_2ARGS,
5995                 locinput);
5996             assert(0); /* NOTREACHED */
5997
5998 #undef  ST
5999 #define ST st->u.branch
6000
6001         case BRANCHJ:       /*  /(...|A|...)/ with long next pointer */
6002             next = scan + ARG(scan);
6003             if (next == scan)
6004                 next = NULL;
6005             scan = NEXTOPER(scan);
6006             /* FALLTHROUGH */
6007
6008         case BRANCH:        /*  /(...|A|...)/ */
6009             scan = NEXTOPER(scan); /* scan now points to inner node */
6010             ST.lastparen = rex->lastparen;
6011             ST.lastcloseparen = rex->lastcloseparen;
6012             ST.next_branch = next;
6013             REGCP_SET(ST.cp);
6014
6015             /* Now go into the branch */
6016             if (has_cutgroup) {
6017                 PUSH_YES_STATE_GOTO(BRANCH_next, scan, locinput);
6018             } else {
6019                 PUSH_STATE_GOTO(BRANCH_next, scan, locinput);
6020             }
6021             assert(0); /* NOTREACHED */
6022
6023         case CUTGROUP:  /*  /(*THEN)/  */
6024             sv_yes_mark = st->u.mark.mark_name = scan->flags ? NULL :
6025                 MUTABLE_SV(rexi->data->data[ ARG( scan ) ]);
6026             PUSH_STATE_GOTO(CUTGROUP_next, next, locinput);
6027             assert(0); /* NOTREACHED */
6028
6029         case CUTGROUP_next_fail:
6030             do_cutgroup = 1;
6031             no_final = 1;
6032             if (st->u.mark.mark_name)
6033                 sv_commit = st->u.mark.mark_name;
6034             sayNO;          
6035             assert(0); /* NOTREACHED */
6036
6037         case BRANCH_next:
6038             sayYES;
6039             assert(0); /* NOTREACHED */
6040
6041         case BRANCH_next_fail: /* that branch failed; try the next, if any */
6042             if (do_cutgroup) {
6043                 do_cutgroup = 0;
6044                 no_final = 0;
6045             }
6046             REGCP_UNWIND(ST.cp);
6047             UNWIND_PAREN(ST.lastparen, ST.lastcloseparen);
6048             scan = ST.next_branch;
6049             /* no more branches? */
6050             if (!scan || (OP(scan) != BRANCH && OP(scan) != BRANCHJ)) {
6051                 DEBUG_EXECUTE_r({
6052                     PerlIO_printf( Perl_debug_log,
6053                         "%*s  %sBRANCH failed...%s\n",
6054                         REPORT_CODE_OFF+depth*2, "", 
6055                         PL_colors[4],
6056                         PL_colors[5] );
6057                 });
6058                 sayNO_SILENT;
6059             }
6060             continue; /* execute next BRANCH[J] op */
6061             assert(0); /* NOTREACHED */
6062     
6063         case MINMOD: /* next op will be non-greedy, e.g. A*?  */
6064             minmod = 1;
6065             break;
6066
6067 #undef  ST
6068 #define ST st->u.curlym
6069
6070         case CURLYM:    /* /A{m,n}B/ where A is fixed-length */
6071
6072             /* This is an optimisation of CURLYX that enables us to push
6073              * only a single backtracking state, no matter how many matches
6074              * there are in {m,n}. It relies on the pattern being constant
6075              * length, with no parens to influence future backrefs
6076              */
6077
6078             ST.me = scan;
6079             scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
6080
6081             ST.lastparen      = rex->lastparen;
6082             ST.lastcloseparen = rex->lastcloseparen;
6083
6084             /* if paren positive, emulate an OPEN/CLOSE around A */
6085             if (ST.me->flags) {
6086                 U32 paren = ST.me->flags;
6087                 if (paren > maxopenparen)
6088                     maxopenparen = paren;
6089                 scan += NEXT_OFF(scan); /* Skip former OPEN. */
6090             }
6091             ST.A = scan;
6092             ST.B = next;
6093             ST.alen = 0;
6094             ST.count = 0;
6095             ST.minmod = minmod;
6096             minmod = 0;
6097             ST.c1 = CHRTEST_UNINIT;
6098             REGCP_SET(ST.cp);
6099
6100             if (!(ST.minmod ? ARG1(ST.me) : ARG2(ST.me))) /* min/max */
6101                 goto curlym_do_B;
6102
6103           curlym_do_A: /* execute the A in /A{m,n}B/  */
6104             PUSH_YES_STATE_GOTO(CURLYM_A, ST.A, locinput); /* match A */
6105             assert(0); /* NOTREACHED */
6106
6107         case CURLYM_A: /* we've just matched an A */
6108             ST.count++;
6109             /* after first match, determine A's length: u.curlym.alen */
6110             if (ST.count == 1) {
6111                 if (reginfo->is_utf8_target) {
6112                     char *s = st->locinput;
6113                     while (s < locinput) {
6114                         ST.alen++;
6115                         s += UTF8SKIP(s);
6116                     }
6117                 }
6118                 else {
6119                     ST.alen = locinput - st->locinput;
6120                 }
6121                 if (ST.alen == 0)
6122                     ST.count = ST.minmod ? ARG1(ST.me) : ARG2(ST.me);
6123             }
6124             DEBUG_EXECUTE_r(
6125                 PerlIO_printf(Perl_debug_log,
6126                           "%*s  CURLYM now matched %"IVdf" times, len=%"IVdf"...\n",
6127                           (int)(REPORT_CODE_OFF+(depth*2)), "",
6128                           (IV) ST.count, (IV)ST.alen)
6129             );
6130
6131             if (cur_eval && cur_eval->u.eval.close_paren && 
6132                 cur_eval->u.eval.close_paren == (U32)ST.me->flags) 
6133                 goto fake_end;
6134                 
6135             {
6136                 I32 max = (ST.minmod ? ARG1(ST.me) : ARG2(ST.me));
6137                 if ( max == REG_INFTY || ST.count < max )
6138                     goto curlym_do_A; /* try to match another A */
6139             }
6140             goto curlym_do_B; /* try to match B */
6141
6142         case CURLYM_A_fail: /* just failed to match an A */
6143             REGCP_UNWIND(ST.cp);
6144
6145             if (ST.minmod || ST.count < ARG1(ST.me) /* min*/ 
6146                 || (cur_eval && cur_eval->u.eval.close_paren &&
6147                     cur_eval->u.eval.close_paren == (U32)ST.me->flags))
6148                 sayNO;
6149
6150           curlym_do_B: /* execute the B in /A{m,n}B/  */
6151             if (ST.c1 == CHRTEST_UNINIT) {
6152                 /* calculate c1 and c2 for possible match of 1st char
6153                  * following curly */
6154                 ST.c1 = ST.c2 = CHRTEST_VOID;
6155                 if (HAS_TEXT(ST.B) || JUMPABLE(ST.B)) {
6156                     regnode *text_node = ST.B;
6157                     if (! HAS_TEXT(text_node))
6158                         FIND_NEXT_IMPT(text_node);
6159                     /* this used to be 
6160                         
6161                         (HAS_TEXT(text_node) && PL_regkind[OP(text_node)] == EXACT)
6162                         
6163                         But the former is redundant in light of the latter.
6164                         
6165                         if this changes back then the macro for 
6166                         IS_TEXT and friends need to change.
6167                      */
6168                     if (PL_regkind[OP(text_node)] == EXACT) {
6169                         if (! S_setup_EXACTISH_ST_c1_c2(aTHX_
6170                            text_node, &ST.c1, ST.c1_utf8, &ST.c2, ST.c2_utf8,
6171                            reginfo))
6172                         {
6173                             sayNO;
6174                         }
6175                     }
6176                 }
6177             }
6178
6179             DEBUG_EXECUTE_r(
6180                 PerlIO_printf(Perl_debug_log,
6181                     "%*s  CURLYM trying tail with matches=%"IVdf"...\n",
6182                     (int)(REPORT_CODE_OFF+(depth*2)),
6183                     "", (IV)ST.count)
6184                 );
6185             if (! NEXTCHR_IS_EOS && ST.c1 != CHRTEST_VOID) {
6186                 if (! UTF8_IS_INVARIANT(nextchr) && utf8_target) {
6187                     if (memNE(locinput, ST.c1_utf8, UTF8SKIP(locinput))
6188                         && memNE(locinput, ST.c2_utf8, UTF8SKIP(locinput)))
6189                     {
6190                         /* simulate B failing */
6191                         DEBUG_OPTIMISE_r(
6192                             PerlIO_printf(Perl_debug_log,
6193                                 "%*s  CURLYM Fast bail next target=0x%"UVXf" c1=0x%"UVXf" c2=0x%"UVXf"\n",
6194                                 (int)(REPORT_CODE_OFF+(depth*2)),"",
6195                                 valid_utf8_to_uvchr((U8 *) locinput, NULL),
6196                                 valid_utf8_to_uvchr(ST.c1_utf8, NULL),
6197                                 valid_utf8_to_uvchr(ST.c2_utf8, NULL))
6198                         );
6199                         state_num = CURLYM_B_fail;
6200                         goto reenter_switch;
6201                     }
6202                 }
6203                 else if (nextchr != ST.c1 && nextchr != ST.c2) {
6204                     /* simulate B failing */
6205                     DEBUG_OPTIMISE_r(
6206                         PerlIO_printf(Perl_debug_log,
6207                             "%*s  CURLYM Fast bail next target=0x%X c1=0x%X c2=0x%X\n",
6208                             (int)(REPORT_CODE_OFF+(depth*2)),"",
6209                             (int) nextchr, ST.c1, ST.c2)
6210                     );
6211                     state_num = CURLYM_B_fail;
6212                     goto reenter_switch;
6213                 }
6214             }
6215
6216             if (ST.me->flags) {
6217                 /* emulate CLOSE: mark current A as captured */
6218                 I32 paren = ST.me->flags;
6219                 if (ST.count) {
6220                     rex->offs[paren].start
6221                         = HOPc(locinput, -ST.alen) - reginfo->strbeg;
6222                     rex->offs[paren].end = locinput - reginfo->strbeg;
6223                     if ((U32)paren > rex->lastparen)
6224                         rex->lastparen = paren;
6225                     rex->lastcloseparen = paren;
6226                 }
6227                 else
6228                     rex->offs[paren].end = -1;
6229                 if (cur_eval && cur_eval->u.eval.close_paren &&
6230                     cur_eval->u.eval.close_paren == (U32)ST.me->flags) 
6231                 {
6232                     if (ST.count) 
6233                         goto fake_end;
6234                     else
6235                         sayNO;
6236                 }
6237             }
6238             
6239             PUSH_STATE_GOTO(CURLYM_B, ST.B, locinput); /* match B */
6240             assert(0); /* NOTREACHED */
6241
6242         case CURLYM_B_fail: /* just failed to match a B */
6243             REGCP_UNWIND(ST.cp);
6244             UNWIND_PAREN(ST.lastparen, ST.lastcloseparen);
6245             if (ST.minmod) {
6246                 I32 max = ARG2(ST.me);
6247                 if (max != REG_INFTY && ST.count == max)
6248                     sayNO;
6249                 goto curlym_do_A; /* try to match a further A */
6250             }
6251             /* backtrack one A */
6252             if (ST.count == ARG1(ST.me) /* min */)
6253                 sayNO;
6254             ST.count--;
6255             SET_locinput(HOPc(locinput, -ST.alen));
6256             goto curlym_do_B; /* try to match B */
6257
6258 #undef ST
6259 #define ST st->u.curly
6260
6261 #define CURLY_SETPAREN(paren, success) \
6262     if (paren) { \
6263         if (success) { \
6264             rex->offs[paren].start = HOPc(locinput, -1) - reginfo->strbeg; \
6265             rex->offs[paren].end = locinput - reginfo->strbeg; \
6266             if (paren > rex->lastparen) \
6267                 rex->lastparen = paren; \
6268             rex->lastcloseparen = paren; \
6269         } \
6270         else { \
6271             rex->offs[paren].end = -1; \
6272             rex->lastparen      = ST.lastparen; \
6273             rex->lastcloseparen = ST.lastcloseparen; \
6274         } \
6275     }
6276
6277         case STAR:              /*  /A*B/ where A is width 1 char */
6278             ST.paren = 0;
6279             ST.min = 0;
6280             ST.max = REG_INFTY;
6281             scan = NEXTOPER(scan);
6282             goto repeat;
6283
6284         case PLUS:              /*  /A+B/ where A is width 1 char */
6285             ST.paren = 0;
6286             ST.min = 1;
6287             ST.max = REG_INFTY;
6288             scan = NEXTOPER(scan);
6289             goto repeat;
6290
6291         case CURLYN:            /*  /(A){m,n}B/ where A is width 1 char */
6292             ST.paren = scan->flags;     /* Which paren to set */
6293             ST.lastparen      = rex->lastparen;
6294             ST.lastcloseparen = rex->lastcloseparen;
6295             if (ST.paren > maxopenparen)
6296                 maxopenparen = ST.paren;
6297             ST.min = ARG1(scan);  /* min to match */
6298             ST.max = ARG2(scan);  /* max to match */
6299             if (cur_eval && cur_eval->u.eval.close_paren &&
6300                 cur_eval->u.eval.close_paren == (U32)ST.paren) {
6301                 ST.min=1;
6302                 ST.max=1;
6303             }
6304             scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
6305             goto repeat;
6306
6307         case CURLY:             /*  /A{m,n}B/ where A is width 1 char */
6308             ST.paren = 0;
6309             ST.min = ARG1(scan);  /* min to match */
6310             ST.max = ARG2(scan);  /* max to match */
6311             scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
6312           repeat:
6313             /*
6314             * Lookahead to avoid useless match attempts
6315             * when we know what character comes next.
6316             *
6317             * Used to only do .*x and .*?x, but now it allows
6318             * for )'s, ('s and (?{ ... })'s to be in the way
6319             * of the quantifier and the EXACT-like node.  -- japhy
6320             */
6321
6322             assert(ST.min <= ST.max);
6323             if (! HAS_TEXT(next) && ! JUMPABLE(next)) {
6324                 ST.c1 = ST.c2 = CHRTEST_VOID;
6325             }
6326             else {
6327                 regnode *text_node = next;
6328
6329                 if (! HAS_TEXT(text_node)) 
6330                     FIND_NEXT_IMPT(text_node);
6331
6332                 if (! HAS_TEXT(text_node))
6333                     ST.c1 = ST.c2 = CHRTEST_VOID;
6334                 else {
6335                     if ( PL_regkind[OP(text_node)] != EXACT ) {
6336                         ST.c1 = ST.c2 = CHRTEST_VOID;
6337                     }
6338                     else {
6339                     
6340                     /*  Currently we only get here when 
6341                         
6342                         PL_rekind[OP(text_node)] == EXACT
6343                     
6344                         if this changes back then the macro for IS_TEXT and 
6345                         friends need to change. */
6346                         if (! S_setup_EXACTISH_ST_c1_c2(aTHX_
6347                            text_node, &ST.c1, ST.c1_utf8, &ST.c2, ST.c2_utf8,
6348                            reginfo))
6349                         {
6350                             sayNO;
6351                         }
6352                     }
6353                 }
6354             }
6355
6356             ST.A = scan;
6357             ST.B = next;
6358             if (minmod) {
6359                 char *li = locinput;
6360                 minmod = 0;
6361                 if (ST.min &&
6362                         regrepeat(rex, &li, ST.A, reginfo, ST.min, depth)
6363                             < ST.min)
6364                     sayNO;
6365                 SET_locinput(li);
6366                 ST.count = ST.min;
6367                 REGCP_SET(ST.cp);
6368                 if (ST.c1 == CHRTEST_VOID)
6369                     goto curly_try_B_min;
6370
6371                 ST.oldloc = locinput;
6372
6373                 /* set ST.maxpos to the furthest point along the
6374                  * string that could possibly match */
6375                 if  (ST.max == REG_INFTY) {
6376                     ST.maxpos = reginfo->strend - 1;
6377                     if (utf8_target)
6378                         while (UTF8_IS_CONTINUATION(*(U8*)ST.maxpos))
6379                             ST.maxpos--;
6380                 }
6381                 else if (utf8_target) {
6382                     int m = ST.max - ST.min;
6383                     for (ST.maxpos = locinput;
6384                          m >0 && ST.maxpos < reginfo->strend; m--)
6385                         ST.maxpos += UTF8SKIP(ST.maxpos);
6386                 }
6387                 else {
6388                     ST.maxpos = locinput + ST.max - ST.min;
6389                     if (ST.maxpos >= reginfo->strend)
6390                         ST.maxpos = reginfo->strend - 1;
6391                 }
6392                 goto curly_try_B_min_known;
6393
6394             }
6395             else {
6396                 /* avoid taking address of locinput, so it can remain
6397                  * a register var */
6398                 char *li = locinput;
6399                 ST.count = regrepeat(rex, &li, ST.A, reginfo, ST.max, depth);
6400                 if (ST.count < ST.min)
6401                     sayNO;
6402                 SET_locinput(li);
6403                 if ((ST.count > ST.min)
6404                     && (PL_regkind[OP(ST.B)] == EOL) && (OP(ST.B) != MEOL))
6405                 {
6406                     /* A{m,n} must come at the end of the string, there's
6407                      * no point in backing off ... */
6408                     ST.min = ST.count;
6409                     /* ...except that $ and \Z can match before *and* after
6410                        newline at the end.  Consider "\n\n" =~ /\n+\Z\n/.
6411                        We may back off by one in this case. */
6412                     if (UCHARAT(locinput - 1) == '\n' && OP(ST.B) != EOS)
6413                         ST.min--;
6414                 }
6415                 REGCP_SET(ST.cp);
6416                 goto curly_try_B_max;
6417             }
6418             assert(0); /* NOTREACHED */
6419
6420
6421         case CURLY_B_min_known_fail:
6422             /* failed to find B in a non-greedy match where c1,c2 valid */
6423
6424             REGCP_UNWIND(ST.cp);
6425             if (ST.paren) {
6426                 UNWIND_PAREN(ST.lastparen, ST.lastcloseparen);
6427             }
6428             /* Couldn't or didn't -- move forward. */
6429             ST.oldloc = locinput;
6430             if (utf8_target)
6431                 locinput += UTF8SKIP(locinput);
6432             else
6433                 locinput++;
6434             ST.count++;
6435           curly_try_B_min_known:
6436              /* find the next place where 'B' could work, then call B */
6437             {
6438                 int n;
6439                 if (utf8_target) {
6440                     n = (ST.oldloc == locinput) ? 0 : 1;
6441                     if (ST.c1 == ST.c2) {
6442                         /* set n to utf8_distance(oldloc, locinput) */
6443                         while (locinput <= ST.maxpos
6444                               && memNE(locinput, ST.c1_utf8, UTF8SKIP(locinput)))
6445                         {
6446                             locinput += UTF8SKIP(locinput);
6447                             n++;
6448                         }
6449                     }
6450                     else {
6451                         /* set n to utf8_distance(oldloc, locinput) */
6452                         while (locinput <= ST.maxpos
6453                               && memNE(locinput, ST.c1_utf8, UTF8SKIP(locinput))
6454                               && memNE(locinput, ST.c2_utf8, UTF8SKIP(locinput)))
6455                         {
6456                             locinput += UTF8SKIP(locinput);
6457                             n++;
6458                         }
6459                     }
6460                 }
6461                 else {  /* Not utf8_target */
6462                     if (ST.c1 == ST.c2) {
6463                         while (locinput <= ST.maxpos &&
6464                                UCHARAT(locinput) != ST.c1)
6465                             locinput++;
6466                     }
6467                     else {
6468                         while (locinput <= ST.maxpos
6469                                && UCHARAT(locinput) != ST.c1
6470                                && UCHARAT(locinput) != ST.c2)
6471                             locinput++;
6472                     }
6473                     n = locinput - ST.oldloc;
6474                 }
6475                 if (locinput > ST.maxpos)
6476                     sayNO;
6477                 if (n) {
6478                     /* In /a{m,n}b/, ST.oldloc is at "a" x m, locinput is
6479                      * at b; check that everything between oldloc and
6480                      * locinput matches */
6481                     char *li = ST.oldloc;
6482                     ST.count += n;
6483                     if (regrepeat(rex, &li, ST.A, reginfo, n, depth) < n)
6484                         sayNO;
6485                     assert(n == REG_INFTY || locinput == li);
6486                 }
6487                 CURLY_SETPAREN(ST.paren, ST.count);
6488                 if (cur_eval && cur_eval->u.eval.close_paren && 
6489                     cur_eval->u.eval.close_paren == (U32)ST.paren) {
6490                     goto fake_end;
6491                 }
6492                 PUSH_STATE_GOTO(CURLY_B_min_known, ST.B, locinput);
6493             }
6494             assert(0); /* NOTREACHED */
6495
6496
6497         case CURLY_B_min_fail:
6498             /* failed to find B in a non-greedy match where c1,c2 invalid */
6499
6500             REGCP_UNWIND(ST.cp);
6501             if (ST.paren) {
6502                 UNWIND_PAREN(ST.lastparen, ST.lastcloseparen);
6503             }
6504             /* failed -- move forward one */
6505             {
6506                 char *li = locinput;
6507                 if (!regrepeat(rex, &li, ST.A, reginfo, 1, depth)) {
6508                     sayNO;
6509                 }
6510                 locinput = li;
6511             }
6512             {
6513                 ST.count++;
6514                 if (ST.count <= ST.max || (ST.max == REG_INFTY &&
6515                         ST.count > 0)) /* count overflow ? */
6516                 {
6517                   curly_try_B_min:
6518                     CURLY_SETPAREN(ST.paren, ST.count);
6519                     if (cur_eval && cur_eval->u.eval.close_paren &&
6520                         cur_eval->u.eval.close_paren == (U32)ST.paren) {
6521                         goto fake_end;
6522                     }
6523                     PUSH_STATE_GOTO(CURLY_B_min, ST.B, locinput);
6524                 }
6525             }
6526             sayNO;
6527             assert(0); /* NOTREACHED */
6528
6529
6530         curly_try_B_max:
6531             /* a successful greedy match: now try to match B */
6532             if (cur_eval && cur_eval->u.eval.close_paren &&
6533                 cur_eval->u.eval.close_paren == (U32)ST.paren) {
6534                 goto fake_end;
6535             }
6536             {
6537                 bool could_match = locinput < reginfo->strend;
6538
6539                 /* If it could work, try it. */
6540                 if (ST.c1 != CHRTEST_VOID && could_match) {
6541                     if (! UTF8_IS_INVARIANT(UCHARAT(locinput)) && utf8_target)
6542                     {
6543                         could_match = memEQ(locinput,
6544                                             ST.c1_utf8,
6545                                             UTF8SKIP(locinput))
6546                                     || memEQ(locinput,
6547                                              ST.c2_utf8,
6548                                              UTF8SKIP(locinput));
6549                     }
6550                     else {
6551                         could_match = UCHARAT(locinput) == ST.c1
6552                                       || UCHARAT(locinput) == ST.c2;
6553                     }
6554                 }
6555                 if (ST.c1 == CHRTEST_VOID || could_match) {
6556                     CURLY_SETPAREN(ST.paren, ST.count);
6557                     PUSH_STATE_GOTO(CURLY_B_max, ST.B, locinput);
6558                     assert(0); /* NOTREACHED */
6559                 }
6560             }
6561             /* FALLTHROUGH */
6562
6563         case CURLY_B_max_fail:
6564             /* failed to find B in a greedy match */
6565
6566             REGCP_UNWIND(ST.cp);
6567             if (ST.paren) {
6568                 UNWIND_PAREN(ST.lastparen, ST.lastcloseparen);
6569             }
6570             /*  back up. */
6571             if (--ST.count < ST.min)
6572                 sayNO;
6573             locinput = HOPc(locinput, -1);
6574             goto curly_try_B_max;
6575
6576 #undef ST
6577
6578         case END: /*  last op of main pattern  */
6579             fake_end:
6580             if (cur_eval) {
6581                 /* we've just finished A in /(??{A})B/; now continue with B */
6582
6583                 st->u.eval.prev_rex = rex_sv;           /* inner */
6584
6585                 /* Save *all* the positions. */
6586                 st->u.eval.cp = regcppush(rex, 0, maxopenparen);
6587                 rex_sv = cur_eval->u.eval.prev_rex;
6588                 is_utf8_pat = reginfo->is_utf8_pat = cBOOL(RX_UTF8(rex_sv));
6589                 SET_reg_curpm(rex_sv);
6590                 rex = ReANY(rex_sv);
6591                 rexi = RXi_GET(rex);
6592                 cur_curlyx = cur_eval->u.eval.prev_curlyx;
6593
6594                 REGCP_SET(st->u.eval.lastcp);
6595
6596                 /* Restore parens of the outer rex without popping the
6597                  * savestack */
6598                 S_regcp_restore(aTHX_ rex, cur_eval->u.eval.lastcp,
6599                                         &maxopenparen);
6600
6601                 st->u.eval.prev_eval = cur_eval;
6602                 cur_eval = cur_eval->u.eval.prev_eval;
6603                 DEBUG_EXECUTE_r(
6604                     PerlIO_printf(Perl_debug_log, "%*s  EVAL trying tail ... %"UVxf"\n",
6605                                       REPORT_CODE_OFF+depth*2, "",PTR2UV(cur_eval)););
6606                 if ( nochange_depth )
6607                     nochange_depth--;
6608
6609                 PUSH_YES_STATE_GOTO(EVAL_AB, st->u.eval.prev_eval->u.eval.B,
6610                                     locinput); /* match B */
6611             }
6612
6613             if (locinput < reginfo->till) {
6614                 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
6615                                       "%sMatch possible, but length=%ld is smaller than requested=%ld, failing!%s\n",
6616                                       PL_colors[4],
6617                                       (long)(locinput - startpos),
6618                                       (long)(reginfo->till - startpos),
6619                                       PL_colors[5]));
6620                                               
6621                 sayNO_SILENT;           /* Cannot match: too short. */
6622             }
6623             sayYES;                     /* Success! */
6624
6625         case SUCCEED: /* successful SUSPEND/UNLESSM/IFMATCH/CURLYM */
6626             DEBUG_EXECUTE_r(
6627             PerlIO_printf(Perl_debug_log,
6628                 "%*s  %ssubpattern success...%s\n",
6629                 REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5]));
6630             sayYES;                     /* Success! */
6631
6632 #undef  ST
6633 #define ST st->u.ifmatch
6634
6635         {
6636             char *newstart;
6637
6638         case SUSPEND:   /* (?>A) */
6639             ST.wanted = 1;
6640             newstart = locinput;
6641             goto do_ifmatch;    
6642
6643         case UNLESSM:   /* -ve lookaround: (?!A), or with flags, (?<!A) */
6644             ST.wanted = 0;
6645             goto ifmatch_trivial_fail_test;
6646
6647         case IFMATCH:   /* +ve lookaround: (?=A), or with flags, (?<=A) */
6648             ST.wanted = 1;
6649           ifmatch_trivial_fail_test:
6650             if (scan->flags) {
6651                 char * const s = HOPBACKc(locinput, scan->flags);
6652                 if (!s) {
6653                     /* trivial fail */
6654                     if (logical) {
6655                         logical = 0;
6656                         sw = 1 - cBOOL(ST.wanted);
6657                     }
6658                     else if (ST.wanted)
6659                         sayNO;
6660                     next = scan + ARG(scan);
6661                     if (next == scan)
6662                         next = NULL;
6663                     break;
6664                 }
6665                 newstart = s;
6666             }
6667             else
6668                 newstart = locinput;
6669
6670           do_ifmatch:
6671             ST.me = scan;
6672             ST.logical = logical;
6673             logical = 0; /* XXX: reset state of logical once it has been saved into ST */
6674             
6675             /* execute body of (?...A) */
6676             PUSH_YES_STATE_GOTO(IFMATCH_A, NEXTOPER(NEXTOPER(scan)), newstart);
6677             assert(0); /* NOTREACHED */
6678         }
6679
6680         case IFMATCH_A_fail: /* body of (?...A) failed */
6681             ST.wanted = !ST.wanted;
6682             /* FALLTHROUGH */
6683
6684         case IFMATCH_A: /* body of (?...A) succeeded */
6685             if (ST.logical) {
6686                 sw = cBOOL(ST.wanted);
6687             }
6688             else if (!ST.wanted)
6689                 sayNO;
6690
6691             if (OP(ST.me) != SUSPEND) {
6692                 /* restore old position except for (?>...) */
6693                 locinput = st->locinput;
6694             }
6695             scan = ST.me + ARG(ST.me);
6696             if (scan == ST.me)
6697                 scan = NULL;
6698             continue; /* execute B */
6699
6700 #undef ST
6701
6702         case LONGJMP: /*  alternative with many branches compiles to
6703                        * (BRANCHJ; EXACT ...; LONGJMP ) x N */
6704             next = scan + ARG(scan);
6705             if (next == scan)
6706                 next = NULL;
6707             break;
6708
6709         case COMMIT:  /*  (*COMMIT)  */
6710             reginfo->cutpoint = reginfo->strend;
6711             /* FALLTHROUGH */
6712
6713         case PRUNE:   /*  (*PRUNE)   */
6714             if (!scan->flags)
6715                 sv_yes_mark = sv_commit = MUTABLE_SV(rexi->data->data[ ARG( scan ) ]);
6716             PUSH_STATE_GOTO(COMMIT_next, next, locinput);
6717             assert(0); /* NOTREACHED */
6718
6719         case COMMIT_next_fail:
6720             no_final = 1;    
6721             /* FALLTHROUGH */       
6722
6723         case OPFAIL:   /* (*FAIL)  */
6724             sayNO;
6725             assert(0); /* NOTREACHED */
6726
6727 #define ST st->u.mark
6728         case MARKPOINT: /*  (*MARK:foo)  */
6729             ST.prev_mark = mark_state;
6730             ST.mark_name = sv_commit = sv_yes_mark 
6731                 = MUTABLE_SV(rexi->data->data[ ARG( scan ) ]);
6732             mark_state = st;
6733             ST.mark_loc = locinput;
6734             PUSH_YES_STATE_GOTO(MARKPOINT_next, next, locinput);
6735             assert(0); /* NOTREACHED */
6736
6737         case MARKPOINT_next:
6738             mark_state = ST.prev_mark;
6739             sayYES;
6740             assert(0); /* NOTREACHED */
6741
6742         case MARKPOINT_next_fail:
6743             if (popmark && sv_eq(ST.mark_name,popmark)) 
6744             {
6745                 if (ST.mark_loc > startpoint)
6746                     reginfo->cutpoint = HOPBACKc(ST.mark_loc, 1);
6747                 popmark = NULL; /* we found our mark */
6748                 sv_commit = ST.mark_name;
6749
6750                 DEBUG_EXECUTE_r({
6751                         PerlIO_printf(Perl_debug_log,
6752                             "%*s  %ssetting cutpoint to mark:%"SVf"...%s\n",
6753                             REPORT_CODE_OFF+depth*2, "", 
6754                             PL_colors[4], SVfARG(sv_commit), PL_colors[5]);
6755                 });
6756             }
6757             mark_state = ST.prev_mark;
6758             sv_yes_mark = mark_state ? 
6759                 mark_state->u.mark.mark_name : NULL;
6760             sayNO;
6761             assert(0); /* NOTREACHED */
6762
6763         case SKIP:  /*  (*SKIP)  */
6764             if (scan->flags) {
6765                 /* (*SKIP) : if we fail we cut here*/
6766                 ST.mark_name = NULL;
6767                 ST.mark_loc = locinput;
6768                 PUSH_STATE_GOTO(SKIP_next,next, locinput);
6769             } else {
6770                 /* (*SKIP:NAME) : if there is a (*MARK:NAME) fail where it was, 
6771                    otherwise do nothing.  Meaning we need to scan 
6772                  */
6773                 regmatch_state *cur = mark_state;
6774                 SV *find = MUTABLE_SV(rexi->data->data[ ARG( scan ) ]);
6775                 
6776                 while (cur) {
6777                     if ( sv_eq( cur->u.mark.mark_name, 
6778                                 find ) ) 
6779                     {
6780                         ST.mark_name = find;
6781                         PUSH_STATE_GOTO( SKIP_next, next, locinput);
6782                     }
6783                     cur = cur->u.mark.prev_mark;
6784                 }
6785             }    
6786             /* Didn't find our (*MARK:NAME) so ignore this (*SKIP:NAME) */
6787             break;    
6788
6789         case SKIP_next_fail:
6790             if (ST.mark_name) {
6791                 /* (*CUT:NAME) - Set up to search for the name as we 
6792                    collapse the stack*/
6793                 popmark = ST.mark_name;    
6794             } else {
6795                 /* (*CUT) - No name, we cut here.*/
6796                 if (ST.mark_loc > startpoint)
6797                     reginfo->cutpoint = HOPBACKc(ST.mark_loc, 1);
6798                 /* but we set sv_commit to latest mark_name if there
6799                    is one so they can test to see how things lead to this
6800                    cut */    
6801                 if (mark_state) 
6802                     sv_commit=mark_state->u.mark.mark_name;                 
6803             } 
6804             no_final = 1; 
6805             sayNO;
6806             assert(0); /* NOTREACHED */
6807 #undef ST
6808
6809         case LNBREAK: /* \R */
6810             if ((n=is_LNBREAK_safe(locinput, reginfo->strend, utf8_target))) {
6811                 locinput += n;
6812             } else
6813                 sayNO;
6814             break;
6815
6816         default:
6817             PerlIO_printf(Perl_error_log, "%"UVxf" %d\n",
6818                           PTR2UV(scan), OP(scan));
6819             Perl_croak(aTHX_ "regexp memory corruption");
6820
6821         /* this is a point to jump to in order to increment
6822          * locinput by one character */
6823         increment_locinput:
6824             assert(!NEXTCHR_IS_EOS);
6825             if (utf8_target) {
6826                 locinput += PL_utf8skip[nextchr];
6827                 /* locinput is allowed to go 1 char off the end, but not 2+ */
6828                 if (locinput > reginfo->strend)
6829                     sayNO;
6830             }
6831             else
6832                 locinput++;
6833             break;
6834             
6835         } /* end switch */ 
6836
6837         /* switch break jumps here */
6838         scan = next; /* prepare to execute the next op and ... */
6839         continue;    /* ... jump back to the top, reusing st */
6840         assert(0); /* NOTREACHED */
6841
6842       push_yes_state:
6843         /* push a state that backtracks on success */
6844         st->u.yes.prev_yes_state = yes_state;
6845         yes_state = st;
6846         /* FALLTHROUGH */
6847       push_state:
6848         /* push a new regex state, then continue at scan  */
6849         {
6850             regmatch_state *newst;
6851
6852             DEBUG_STACK_r({
6853                 regmatch_state *cur = st;
6854                 regmatch_state *curyes = yes_state;
6855                 int curd = depth;
6856                 regmatch_slab *slab = PL_regmatch_slab;
6857                 for (;curd > -1;cur--,curd--) {
6858                     if (cur < SLAB_FIRST(slab)) {
6859                         slab = slab->prev;
6860                         cur = SLAB_LAST(slab);
6861                     }
6862                     PerlIO_printf(Perl_error_log, "%*s#%-3d %-10s %s\n",
6863                         REPORT_CODE_OFF + 2 + depth * 2,"",
6864                         curd, PL_reg_name[cur->resume_state],
6865                         (curyes == cur) ? "yes" : ""
6866                     );
6867                     if (curyes == cur)
6868                         curyes = cur->u.yes.prev_yes_state;
6869                 }
6870             } else 
6871                 DEBUG_STATE_pp("push")
6872             );
6873             depth++;
6874             st->locinput = locinput;
6875             newst = st+1; 
6876             if (newst >  SLAB_LAST(PL_regmatch_slab))
6877                 newst = S_push_slab(aTHX);
6878             PL_regmatch_state = newst;
6879
6880             locinput = pushinput;
6881             st = newst;
6882             continue;
6883             assert(0); /* NOTREACHED */
6884         }
6885     }
6886
6887     /*
6888     * We get here only if there's trouble -- normally "case END" is
6889     * the terminating point.
6890     */
6891     Perl_croak(aTHX_ "corrupted regexp pointers");
6892     /*NOTREACHED*/
6893     sayNO;
6894
6895 yes:
6896     if (yes_state) {
6897         /* we have successfully completed a subexpression, but we must now
6898          * pop to the state marked by yes_state and continue from there */
6899         assert(st != yes_state);
6900 #ifdef DEBUGGING
6901         while (st != yes_state) {
6902             st--;
6903             if (st < SLAB_FIRST(PL_regmatch_slab)) {
6904                 PL_regmatch_slab = PL_regmatch_slab->prev;
6905                 st = SLAB_LAST(PL_regmatch_slab);
6906             }
6907             DEBUG_STATE_r({
6908                 if (no_final) {
6909                     DEBUG_STATE_pp("pop (no final)");        
6910                 } else {
6911                     DEBUG_STATE_pp("pop (yes)");
6912                 }
6913             });
6914             depth--;
6915         }
6916 #else
6917         while (yes_state < SLAB_FIRST(PL_regmatch_slab)
6918             || yes_state > SLAB_LAST(PL_regmatch_slab))
6919         {
6920             /* not in this slab, pop slab */
6921             depth -= (st - SLAB_FIRST(PL_regmatch_slab) + 1);
6922             PL_regmatch_slab = PL_regmatch_slab->prev;
6923             st = SLAB_LAST(PL_regmatch_slab);
6924         }
6925         depth -= (st - yes_state);
6926 #endif
6927         st = yes_state;
6928         yes_state = st->u.yes.prev_yes_state;
6929         PL_regmatch_state = st;
6930         
6931         if (no_final)
6932             locinput= st->locinput;
6933         state_num = st->resume_state + no_final;
6934         goto reenter_switch;
6935     }
6936
6937     DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch successful!%s\n",
6938                           PL_colors[4], PL_colors[5]));
6939
6940     if (reginfo->info_aux_eval) {
6941         /* each successfully executed (?{...}) block does the equivalent of
6942          *   local $^R = do {...}
6943          * When popping the save stack, all these locals would be undone;
6944          * bypass this by setting the outermost saved $^R to the latest
6945          * value */
6946         /* I dont know if this is needed or works properly now.
6947          * see code related to PL_replgv elsewhere in this file.
6948          * Yves
6949          */
6950         if (oreplsv != GvSV(PL_replgv))
6951             sv_setsv(oreplsv, GvSV(PL_replgv));
6952     }
6953     result = 1;
6954     goto final_exit;
6955
6956 no:
6957     DEBUG_EXECUTE_r(
6958         PerlIO_printf(Perl_debug_log,
6959             "%*s  %sfailed...%s\n",
6960             REPORT_CODE_OFF+depth*2, "", 
6961             PL_colors[4], PL_colors[5])
6962         );
6963
6964 no_silent:
6965     if (no_final) {
6966         if (yes_state) {
6967             goto yes;
6968         } else {
6969             goto final_exit;
6970         }
6971     }    
6972     if (depth) {
6973         /* there's a previous state to backtrack to */
6974         st--;
6975         if (st < SLAB_FIRST(PL_regmatch_slab)) {
6976             PL_regmatch_slab = PL_regmatch_slab->prev;
6977             st = SLAB_LAST(PL_regmatch_slab);
6978         }
6979         PL_regmatch_state = st;
6980         locinput= st->locinput;
6981
6982         DEBUG_STATE_pp("pop");
6983         depth--;
6984         if (yes_state == st)
6985             yes_state = st->u.yes.prev_yes_state;
6986
6987         state_num = st->resume_state + 1; /* failure = success + 1 */
6988         goto reenter_switch;
6989     }
6990     result = 0;
6991
6992   final_exit:
6993     if (rex->intflags & PREGf_VERBARG_SEEN) {
6994         SV *sv_err = get_sv("REGERROR", 1);
6995         SV *sv_mrk = get_sv("REGMARK", 1);
6996         if (result) {
6997             sv_commit = &PL_sv_no;
6998             if (!sv_yes_mark) 
6999                 sv_yes_mark = &PL_sv_yes;
7000         } else {
7001             if (!sv_commit) 
7002                 sv_commit = &PL_sv_yes;
7003             sv_yes_mark = &PL_sv_no;
7004         }
7005         assert(sv_err);
7006         assert(sv_mrk);
7007         sv_setsv(sv_err, sv_commit);
7008         sv_setsv(sv_mrk, sv_yes_mark);
7009     }
7010
7011
7012     if (last_pushed_cv) {
7013         dSP;
7014         POP_MULTICALL;
7015         PERL_UNUSED_VAR(SP);
7016     }
7017
7018     assert(!result ||  locinput - reginfo->strbeg >= 0);
7019     return result ?  locinput - reginfo->strbeg : -1;
7020 }
7021
7022 /*
7023  - regrepeat - repeatedly match something simple, report how many
7024  *
7025  * What 'simple' means is a node which can be the operand of a quantifier like
7026  * '+', or {1,3}
7027  *
7028  * startposp - pointer a pointer to the start position.  This is updated
7029  *             to point to the byte following the highest successful
7030  *             match.
7031  * p         - the regnode to be repeatedly matched against.
7032  * reginfo   - struct holding match state, such as strend
7033  * max       - maximum number of things to match.
7034  * depth     - (for debugging) backtracking depth.
7035  */
7036 STATIC I32
7037 S_regrepeat(pTHX_ regexp *prog, char **startposp, const regnode *p,
7038             regmatch_info *const reginfo, I32 max, int depth)
7039 {
7040     dVAR;
7041     char *scan;     /* Pointer to current position in target string */
7042     I32 c;
7043     char *loceol = reginfo->strend;   /* local version */
7044     I32 hardcount = 0;  /* How many matches so far */
7045     bool utf8_target = reginfo->is_utf8_target;
7046     int to_complement = 0;  /* Invert the result? */
7047     UV utf8_flags;
7048     _char_class_number classnum;
7049 #ifndef DEBUGGING
7050     PERL_UNUSED_ARG(depth);
7051 #endif
7052
7053     PERL_ARGS_ASSERT_REGREPEAT;
7054
7055     scan = *startposp;
7056     if (max == REG_INFTY)
7057         max = I32_MAX;
7058     else if (! utf8_target && loceol - scan > max)
7059         loceol = scan + max;
7060
7061     /* Here, for the case of a non-UTF-8 target we have adjusted <loceol> down
7062      * to the maximum of how far we should go in it (leaving it set to the real
7063      * end, if the maximum permissible would take us beyond that).  This allows
7064      * us to make the loop exit condition that we haven't gone past <loceol> to
7065      * also mean that we haven't exceeded the max permissible count, saving a
7066      * test each time through the loop.  But it assumes that the OP matches a
7067      * single byte, which is true for most of the OPs below when applied to a
7068      * non-UTF-8 target.  Those relatively few OPs that don't have this
7069      * characteristic will have to compensate.
7070      *
7071      * There is no adjustment for UTF-8 targets, as the number of bytes per
7072      * character varies.  OPs will have to test both that the count is less
7073      * than the max permissible (using <hardcount> to keep track), and that we
7074      * are still within the bounds of the string (using <loceol>.  A few OPs
7075      * match a single byte no matter what the encoding.  They can omit the max
7076      * test if, for the UTF-8 case, they do the adjustment that was skipped
7077      * above.
7078      *
7079      * Thus, the code above sets things up for the common case; and exceptional
7080      * cases need extra work; the common case is to make sure <scan> doesn't
7081      * go past <loceol>, and for UTF-8 to also use <hardcount> to make sure the
7082      * count doesn't exceed the maximum permissible */
7083
7084     switch (OP(p)) {
7085     case REG_ANY:
7086         if (utf8_target) {
7087             while (scan < loceol && hardcount < max && *scan != '\n') {
7088                 scan += UTF8SKIP(scan);
7089                 hardcount++;
7090             }
7091         } else {
7092             while (scan < loceol && *scan != '\n')
7093                 scan++;
7094         }
7095         break;
7096     case SANY:
7097         if (utf8_target) {
7098             while (scan < loceol && hardcount < max) {
7099                 scan += UTF8SKIP(scan);
7100                 hardcount++;
7101             }
7102         }
7103         else
7104             scan = loceol;
7105         break;
7106     case CANY:  /* Move <scan> forward <max> bytes, unless goes off end */
7107         if (utf8_target && loceol - scan > max) {
7108
7109             /* <loceol> hadn't been adjusted in the UTF-8 case */
7110             scan +=  max;
7111         }
7112         else {
7113             scan = loceol;
7114         }
7115         break;
7116     case EXACT:
7117         assert(STR_LEN(p) == reginfo->is_utf8_pat ? UTF8SKIP(STRING(p)) : 1);
7118
7119         c = (U8)*STRING(p);
7120
7121         /* Can use a simple loop if the pattern char to match on is invariant
7122          * under UTF-8, or both target and pattern aren't UTF-8.  Note that we
7123          * can use UTF8_IS_INVARIANT() even if the pattern isn't UTF-8, as it's
7124          * true iff it doesn't matter if the argument is in UTF-8 or not */
7125         if (UTF8_IS_INVARIANT(c) || (! utf8_target && ! reginfo->is_utf8_pat)) {
7126             if (utf8_target && loceol - scan > max) {
7127                 /* We didn't adjust <loceol> because is UTF-8, but ok to do so,
7128                  * since here, to match at all, 1 char == 1 byte */
7129                 loceol = scan + max;
7130             }
7131             while (scan < loceol && UCHARAT(scan) == c) {
7132                 scan++;
7133             }
7134         }
7135         else if (reginfo->is_utf8_pat) {
7136             if (utf8_target) {
7137                 STRLEN scan_char_len;
7138
7139                 /* When both target and pattern are UTF-8, we have to do
7140                  * string EQ */
7141                 while (hardcount < max
7142                        && scan < loceol
7143                        && (scan_char_len = UTF8SKIP(scan)) <= STR_LEN(p)
7144                        && memEQ(scan, STRING(p), scan_char_len))
7145                 {
7146                     scan += scan_char_len;
7147                     hardcount++;
7148                 }
7149             }
7150             else if (! UTF8_IS_ABOVE_LATIN1(c)) {
7151
7152                 /* Target isn't utf8; convert the character in the UTF-8
7153                  * pattern to non-UTF8, and do a simple loop */
7154                 c = TWO_BYTE_UTF8_TO_NATIVE(c, *(STRING(p) + 1));
7155                 while (scan < loceol && UCHARAT(scan) == c) {
7156                     scan++;
7157                 }
7158             } /* else pattern char is above Latin1, can't possibly match the
7159                  non-UTF-8 target */
7160         }
7161         else {
7162
7163             /* Here, the string must be utf8; pattern isn't, and <c> is
7164              * different in utf8 than not, so can't compare them directly.
7165              * Outside the loop, find the two utf8 bytes that represent c, and
7166              * then look for those in sequence in the utf8 string */
7167             U8 high = UTF8_TWO_BYTE_HI(c);
7168             U8 low = UTF8_TWO_BYTE_LO(c);
7169
7170             while (hardcount < max
7171                     && scan + 1 < loceol
7172                     && UCHARAT(scan) == high
7173                     && UCHARAT(scan + 1) == low)
7174             {
7175                 scan += 2;
7176                 hardcount++;
7177             }
7178         }
7179         break;
7180
7181     case EXACTFA_NO_TRIE:   /* This node only generated for non-utf8 patterns */
7182         assert(! reginfo->is_utf8_pat);
7183         /* FALLTHROUGH */
7184     case EXACTFA:
7185         utf8_flags = FOLDEQ_UTF8_NOMIX_ASCII;
7186         goto do_exactf;
7187
7188     case EXACTFL:
7189         utf8_flags = FOLDEQ_LOCALE;
7190         goto do_exactf;
7191
7192     case EXACTF:   /* This node only generated for non-utf8 patterns */
7193         assert(! reginfo->is_utf8_pat);
7194         utf8_flags = 0;
7195         goto do_exactf;
7196
7197     case EXACTFU_SS:
7198     case EXACTFU:
7199         utf8_flags = reginfo->is_utf8_pat ? FOLDEQ_S2_ALREADY_FOLDED : 0;
7200
7201     do_exactf: {
7202         int c1, c2;
7203         U8 c1_utf8[UTF8_MAXBYTES+1], c2_utf8[UTF8_MAXBYTES+1];
7204
7205         assert(STR_LEN(p) == reginfo->is_utf8_pat ? UTF8SKIP(STRING(p)) : 1);
7206
7207         if (S_setup_EXACTISH_ST_c1_c2(aTHX_ p, &c1, c1_utf8, &c2, c2_utf8,
7208                                         reginfo))
7209         {
7210             if (c1 == CHRTEST_VOID) {
7211                 /* Use full Unicode fold matching */
7212                 char *tmpeol = reginfo->strend;
7213                 STRLEN pat_len = reginfo->is_utf8_pat ? UTF8SKIP(STRING(p)) : 1;
7214                 while (hardcount < max
7215                         && foldEQ_utf8_flags(scan, &tmpeol, 0, utf8_target,
7216                                              STRING(p), NULL, pat_len,
7217                                              reginfo->is_utf8_pat, utf8_flags))
7218                 {
7219                     scan = tmpeol;
7220                     tmpeol = reginfo->strend;
7221                     hardcount++;
7222                 }
7223             }
7224             else if (utf8_target) {
7225                 if (c1 == c2) {
7226                     while (scan < loceol
7227                            && hardcount < max
7228                            && memEQ(scan, c1_utf8, UTF8SKIP(scan)))
7229                     {
7230                         scan += UTF8SKIP(scan);
7231                         hardcount++;
7232                     }
7233                 }
7234                 else {
7235                     while (scan < loceol
7236                            && hardcount < max
7237                            && (memEQ(scan, c1_utf8, UTF8SKIP(scan))
7238                                || memEQ(scan, c2_utf8, UTF8SKIP(scan))))
7239                     {
7240                         scan += UTF8SKIP(scan);
7241                         hardcount++;
7242                     }
7243                 }
7244             }
7245             else if (c1 == c2) {
7246                 while (scan < loceol && UCHARAT(scan) == c1) {
7247                     scan++;
7248                 }
7249             }
7250             else {
7251                 while (scan < loceol &&
7252                     (UCHARAT(scan) == c1 || UCHARAT(scan) == c2))
7253                 {
7254                     scan++;
7255                 }
7256             }
7257         }
7258         break;
7259     }
7260     case ANYOF:
7261         if (utf8_target) {
7262             while (hardcount < max
7263                    && scan < loceol
7264                    && reginclass(prog, p, (U8*)scan, (U8*) loceol, utf8_target))
7265             {
7266                 scan += UTF8SKIP(scan);
7267                 hardcount++;
7268             }
7269         } else {
7270             while (scan < loceol && REGINCLASS(prog, p, (U8*)scan))
7271                 scan++;
7272         }
7273         break;
7274
7275     /* The argument (FLAGS) to all the POSIX node types is the class number */
7276
7277     case NPOSIXL:
7278         to_complement = 1;
7279         /* FALLTHROUGH */
7280
7281     case POSIXL:
7282         if (! utf8_target) {
7283             while (scan < loceol && to_complement ^ cBOOL(isFOO_lc(FLAGS(p),
7284                                                                    *scan)))
7285             {
7286                 scan++;
7287             }
7288         } else {
7289             while (hardcount < max && scan < loceol
7290                    && to_complement ^ cBOOL(isFOO_utf8_lc(FLAGS(p),
7291                                                                   (U8 *) scan)))
7292             {
7293                 scan += UTF8SKIP(scan);
7294                 hardcount++;
7295             }
7296         }
7297         break;
7298
7299     case POSIXD:
7300         if (utf8_target) {
7301             goto utf8_posix;
7302         }
7303         /* FALLTHROUGH */
7304
7305     case POSIXA:
7306         if (utf8_target && loceol - scan > max) {
7307
7308             /* We didn't adjust <loceol> at the beginning of this routine
7309              * because is UTF-8, but it is actually ok to do so, since here, to
7310              * match, 1 char == 1 byte. */
7311             loceol = scan + max;
7312         }
7313         while (scan < loceol && _generic_isCC_A((U8) *scan, FLAGS(p))) {
7314             scan++;
7315         }
7316         break;
7317
7318     case NPOSIXD:
7319         if (utf8_target) {
7320             to_complement = 1;
7321             goto utf8_posix;
7322         }
7323         /* FALLTHROUGH */
7324
7325     case NPOSIXA:
7326         if (! utf8_target) {
7327             while (scan < loceol && ! _generic_isCC_A((U8) *scan, FLAGS(p))) {
7328                 scan++;
7329             }
7330         }
7331         else {
7332
7333             /* The complement of something that matches only ASCII matches all
7334              * non-ASCII, plus everything in ASCII that isn't in the class. */
7335             while (hardcount < max && scan < loceol
7336                    && (! isASCII_utf8(scan)
7337                        || ! _generic_isCC_A((U8) *scan, FLAGS(p))))
7338             {
7339                 scan += UTF8SKIP(scan);
7340                 hardcount++;
7341             }
7342         }
7343         break;
7344
7345     case NPOSIXU:
7346         to_complement = 1;
7347         /* FALLTHROUGH */
7348
7349     case POSIXU:
7350         if (! utf8_target) {
7351             while (scan < loceol && to_complement
7352                                 ^ cBOOL(_generic_isCC((U8) *scan, FLAGS(p))))
7353             {
7354                 scan++;
7355             }
7356         }
7357         else {
7358       utf8_posix:
7359             classnum = (_char_class_number) FLAGS(p);
7360             if (classnum < _FIRST_NON_SWASH_CC) {
7361
7362                 /* Here, a swash is needed for above-Latin1 code points.
7363                  * Process as many Latin1 code points using the built-in rules.
7364                  * Go to another loop to finish processing upon encountering
7365                  * the first Latin1 code point.  We could do that in this loop
7366                  * as well, but the other way saves having to test if the swash
7367                  * has been loaded every time through the loop: extra space to
7368                  * save a test. */
7369                 while (hardcount < max && scan < loceol) {
7370                     if (UTF8_IS_INVARIANT(*scan)) {
7371                         if (! (to_complement ^ cBOOL(_generic_isCC((U8) *scan,
7372                                                                    classnum))))
7373                         {
7374                             break;
7375                         }
7376                         scan++;
7377                     }
7378                     else if (UTF8_IS_DOWNGRADEABLE_START(*scan)) {
7379                         if (! (to_complement
7380                               ^ cBOOL(_generic_isCC(TWO_BYTE_UTF8_TO_NATIVE(*scan,
7381                                                                      *(scan + 1)),
7382                                                     classnum))))
7383                         {
7384                             break;
7385                         }
7386                         scan += 2;
7387                     }
7388                     else {
7389                         goto found_above_latin1;
7390                     }
7391
7392                     hardcount++;
7393                 }
7394             }
7395             else {
7396                 /* For these character classes, the knowledge of how to handle
7397                  * every code point is compiled in to Perl via a macro.  This
7398                  * code is written for making the loops as tight as possible.
7399                  * It could be refactored to save space instead */
7400                 switch (classnum) {
7401                     case _CC_ENUM_SPACE:    /* XXX would require separate code
7402                                                if we revert the change of \v
7403                                                matching this */
7404                         /* FALLTHROUGH */
7405                     case _CC_ENUM_PSXSPC:
7406                         while (hardcount < max
7407                                && scan < loceol
7408                                && (to_complement ^ cBOOL(isSPACE_utf8(scan))))
7409                         {
7410                             scan += UTF8SKIP(scan);
7411                             hardcount++;
7412                         }
7413                         break;
7414                     case _CC_ENUM_BLANK:
7415                         while (hardcount < max
7416                                && scan < loceol
7417                                && (to_complement ^ cBOOL(isBLANK_utf8(scan))))
7418                         {
7419                             scan += UTF8SKIP(scan);
7420                             hardcount++;
7421                         }
7422                         break;
7423                     case _CC_ENUM_XDIGIT:
7424                         while (hardcount < max
7425                                && scan < loceol
7426                                && (to_complement ^ cBOOL(isXDIGIT_utf8(scan))))
7427                         {
7428                             scan += UTF8SKIP(scan);
7429                             hardcount++;
7430                         }
7431                         break;
7432                     case _CC_ENUM_VERTSPACE:
7433                         while (hardcount < max
7434                                && scan < loceol
7435                                && (to_complement ^ cBOOL(isVERTWS_utf8(scan))))
7436                         {
7437                             scan += UTF8SKIP(scan);
7438                             hardcount++;
7439                         }
7440                         break;
7441                     case _CC_ENUM_CNTRL:
7442                         while (hardcount < max
7443                                && scan < loceol
7444                                && (to_complement ^ cBOOL(isCNTRL_utf8(scan))))
7445                         {
7446                             scan += UTF8SKIP(scan);
7447                             hardcount++;
7448                         }
7449                         break;
7450                     default:
7451                         Perl_croak(aTHX_ "panic: regrepeat() node %d='%s' has an unexpected character class '%d'", OP(p), PL_reg_name[OP(p)], classnum);
7452                 }
7453             }
7454         }
7455         break;
7456
7457       found_above_latin1:   /* Continuation of POSIXU and NPOSIXU */
7458
7459         /* Load the swash if not already present */
7460         if (! PL_utf8_swash_ptrs[classnum]) {
7461             U8 flags = _CORE_SWASH_INIT_ACCEPT_INVLIST;
7462             PL_utf8_swash_ptrs[classnum] = _core_swash_init(
7463                                         "utf8",
7464                                         "",
7465                                         &PL_sv_undef, 1, 0,
7466                                         PL_XPosix_ptrs[classnum], &flags);
7467         }
7468
7469         while (hardcount < max && scan < loceol
7470                && to_complement ^ cBOOL(_generic_utf8(
7471                                        classnum,
7472                                        scan,
7473                                        swash_fetch(PL_utf8_swash_ptrs[classnum],
7474                                                    (U8 *) scan,
7475                                                    TRUE))))
7476         {
7477             scan += UTF8SKIP(scan);
7478             hardcount++;
7479         }
7480         break;
7481
7482     case LNBREAK:
7483         if (utf8_target) {
7484             while (hardcount < max && scan < loceol &&
7485                     (c=is_LNBREAK_utf8_safe(scan, loceol))) {
7486                 scan += c;
7487                 hardcount++;
7488             }
7489         } else {
7490             /* LNBREAK can match one or two latin chars, which is ok, but we
7491              * have to use hardcount in this situation, and throw away the
7492              * adjustment to <loceol> done before the switch statement */
7493             loceol = reginfo->strend;
7494             while (scan < loceol && (c=is_LNBREAK_latin1_safe(scan, loceol))) {
7495                 scan+=c;
7496                 hardcount++;
7497             }
7498         }
7499         break;
7500
7501     case BOUND:
7502     case BOUNDA:
7503     case BOUNDL:
7504     case BOUNDU:
7505     case EOS:
7506     case GPOS:
7507     case KEEPS:
7508     case NBOUND:
7509     case NBOUNDA:
7510     case NBOUNDL:
7511     case NBOUNDU:
7512     case OPFAIL:
7513     case SBOL:
7514     case SEOL:
7515         /* These are all 0 width, so match right here or not at all. */
7516         break;
7517
7518     default:
7519         Perl_croak(aTHX_ "panic: regrepeat() called with unrecognized node type %d='%s'", OP(p), PL_reg_name[OP(p)]);
7520         assert(0); /* NOTREACHED */
7521
7522     }
7523
7524     if (hardcount)
7525         c = hardcount;
7526     else
7527         c = scan - *startposp;
7528     *startposp = scan;
7529
7530     DEBUG_r({
7531         GET_RE_DEBUG_FLAGS_DECL;
7532         DEBUG_EXECUTE_r({
7533             SV * const prop = sv_newmortal();
7534             regprop(prog, prop, p, reginfo);
7535             PerlIO_printf(Perl_debug_log,
7536                         "%*s  %s can match %"IVdf" times out of %"IVdf"...\n",
7537                         REPORT_CODE_OFF + depth*2, "", SvPVX_const(prop),(IV)c,(IV)max);
7538         });
7539     });
7540
7541     return(c);
7542 }
7543
7544
7545 #if !defined(PERL_IN_XSUB_RE) || defined(PLUGGABLE_RE_EXTENSION)
7546 /*
7547 - regclass_swash - prepare the utf8 swash.  Wraps the shared core version to
7548 create a copy so that changes the caller makes won't change the shared one.
7549 If <altsvp> is non-null, will return NULL in it, for back-compat.
7550  */
7551 SV *
7552 Perl_regclass_swash(pTHX_ const regexp *prog, const regnode* node, bool doinit, SV** listsvp, SV **altsvp)
7553 {
7554     PERL_ARGS_ASSERT_REGCLASS_SWASH;
7555
7556     if (altsvp) {
7557         *altsvp = NULL;
7558     }
7559
7560     return newSVsv(_get_regclass_nonbitmap_data(prog, node, doinit, listsvp, NULL));
7561 }
7562
7563 SV *
7564 Perl__get_regclass_nonbitmap_data(pTHX_ const regexp *prog,
7565                                         const regnode* node,
7566                                         bool doinit,
7567                                         SV** listsvp,
7568                                         SV** only_utf8_locale_ptr)
7569 {
7570     /* For internal core use only.
7571      * Returns the swash for the input 'node' in the regex 'prog'.
7572      * If <doinit> is 'true', will attempt to create the swash if not already
7573      *    done.
7574      * If <listsvp> is non-null, will return the printable contents of the
7575      *    swash.  This can be used to get debugging information even before the
7576      *    swash exists, by calling this function with 'doinit' set to false, in
7577      *    which case the components that will be used to eventually create the
7578      *    swash are returned  (in a printable form).
7579      * Tied intimately to how regcomp.c sets up the data structure */
7580
7581     dVAR;
7582     SV *sw  = NULL;
7583     SV *si  = NULL;         /* Input swash initialization string */
7584     SV*  invlist = NULL;
7585
7586     RXi_GET_DECL(prog,progi);
7587     const struct reg_data * const data = prog ? progi->data : NULL;
7588
7589     PERL_ARGS_ASSERT__GET_REGCLASS_NONBITMAP_DATA;
7590
7591     assert(ANYOF_FLAGS(node)
7592                         & (ANYOF_UTF8|ANYOF_NONBITMAP_NON_UTF8|ANYOF_LOC_FOLD));
7593
7594     if (data && data->count) {
7595         const U32 n = ARG(node);
7596
7597         if (data->what[n] == 's') {
7598             SV * const rv = MUTABLE_SV(data->data[n]);
7599             AV * const av = MUTABLE_AV(SvRV(rv));
7600             SV **const ary = AvARRAY(av);
7601             U8 swash_init_flags = _CORE_SWASH_INIT_ACCEPT_INVLIST;
7602         
7603             si = *ary;  /* ary[0] = the string to initialize the swash with */
7604
7605             /* Elements 3 and 4 are either both present or both absent. [3] is
7606              * any inversion list generated at compile time; [4] indicates if
7607              * that inversion list has any user-defined properties in it. */
7608             if (av_tindex(av) >= 2) {
7609                 if (only_utf8_locale_ptr
7610                     && ary[2]
7611                     && ary[2] != &PL_sv_undef)
7612                 {
7613                     *only_utf8_locale_ptr = ary[2];
7614                 }
7615                 else {
7616                     assert(only_utf8_locale_ptr);
7617                     *only_utf8_locale_ptr = NULL;
7618                 }
7619
7620                 if (av_tindex(av) >= 3) {
7621                     invlist = ary[3];
7622                     if (SvUV(ary[4])) {
7623                         swash_init_flags |= _CORE_SWASH_INIT_USER_DEFINED_PROPERTY;
7624                     }
7625                 }
7626                 else {
7627                     invlist = NULL;
7628                 }
7629             }
7630
7631             /* Element [1] is reserved for the set-up swash.  If already there,
7632              * return it; if not, create it and store it there */
7633             if (ary[1] && SvROK(ary[1])) {
7634                 sw = ary[1];
7635             }
7636             else if (doinit && ((si && si != &PL_sv_undef)
7637                                  || (invlist && invlist != &PL_sv_undef))) {
7638                 assert(si);
7639                 sw = _core_swash_init("utf8", /* the utf8 package */
7640                                       "", /* nameless */
7641                                       si,
7642                                       1, /* binary */
7643                                       0, /* not from tr/// */
7644                                       invlist,
7645                                       &swash_init_flags);
7646                 (void)av_store(av, 1, sw);
7647             }
7648         }
7649     }
7650         
7651     /* If requested, return a printable version of what this swash matches */
7652     if (listsvp) {
7653         SV* matches_string = newSVpvn("", 0);
7654
7655         /* The swash should be used, if possible, to get the data, as it
7656          * contains the resolved data.  But this function can be called at
7657          * compile-time, before everything gets resolved, in which case we
7658          * return the currently best available information, which is the string
7659          * that will eventually be used to do that resolving, 'si' */
7660         if ((! sw || (invlist = _get_swash_invlist(sw)) == NULL)
7661             && (si && si != &PL_sv_undef))
7662         {
7663             sv_catsv(matches_string, si);
7664         }
7665
7666         /* Add the inversion list to whatever we have.  This may have come from
7667          * the swash, or from an input parameter */
7668         if (invlist) {
7669             sv_catsv(matches_string, _invlist_contents(invlist));
7670         }
7671         *listsvp = matches_string;
7672     }
7673
7674     return sw;
7675 }
7676 #endif /* !defined(PERL_IN_XSUB_RE) || defined(PLUGGABLE_RE_EXTENSION) */
7677
7678 /*
7679  - reginclass - determine if a character falls into a character class
7680  
7681   n is the ANYOF regnode
7682   p is the target string
7683   p_end points to one byte beyond the end of the target string
7684   utf8_target tells whether p is in UTF-8.
7685
7686   Returns true if matched; false otherwise.
7687
7688   Note that this can be a synthetic start class, a combination of various
7689   nodes, so things you think might be mutually exclusive, such as locale,
7690   aren't.  It can match both locale and non-locale
7691
7692  */
7693
7694 STATIC bool
7695 S_reginclass(pTHX_ regexp * const prog, const regnode * const n, const U8* const p, const U8* const p_end, const bool utf8_target)
7696 {
7697     dVAR;
7698     const char flags = ANYOF_FLAGS(n);
7699     bool match = FALSE;
7700     UV c = *p;
7701
7702     PERL_ARGS_ASSERT_REGINCLASS;
7703
7704     /* If c is not already the code point, get it.  Note that
7705      * UTF8_IS_INVARIANT() works even if not in UTF-8 */
7706     if (! UTF8_IS_INVARIANT(c) && utf8_target) {
7707         STRLEN c_len = 0;
7708         c = utf8n_to_uvchr(p, p_end - p, &c_len,
7709                 (UTF8_ALLOW_DEFAULT & UTF8_ALLOW_ANYUV)
7710                 | UTF8_ALLOW_FFFF | UTF8_CHECK_ONLY);
7711                 /* see [perl #37836] for UTF8_ALLOW_ANYUV; [perl #38293] for
7712                  * UTF8_ALLOW_FFFF */
7713         if (c_len == (STRLEN)-1)
7714             Perl_croak(aTHX_ "Malformed UTF-8 character (fatal)");
7715     }
7716
7717     /* If this character is potentially in the bitmap, check it */
7718     if (c < 256) {
7719         if (ANYOF_BITMAP_TEST(n, c))
7720             match = TRUE;
7721         else if (flags & ANYOF_NON_UTF8_NON_ASCII_ALL
7722                 && ! utf8_target
7723                 && ! isASCII(c))
7724         {
7725             match = TRUE;
7726         }
7727         else if (flags & ANYOF_LOCALE_FLAGS) {
7728             if (flags & ANYOF_LOC_FOLD) {
7729                  if (ANYOF_BITMAP_TEST(n, PL_fold_locale[c])) {
7730                     match = TRUE;
7731                 }
7732             }
7733             if (! match && ANYOF_POSIXL_TEST_ANY_SET(n)) {
7734
7735                 /* The data structure is arranged so bits 0, 2, 4, ... are set
7736                  * if the class includes the Posix character class given by
7737                  * bit/2; and 1, 3, 5, ... are set if the class includes the
7738                  * complemented Posix class given by int(bit/2).  So we loop
7739                  * through the bits, each time changing whether we complement
7740                  * the result or not.  Suppose for the sake of illustration
7741                  * that bits 0-3 mean respectively, \w, \W, \s, \S.  If bit 0
7742                  * is set, it means there is a match for this ANYOF node if the
7743                  * character is in the class given by the expression (0 / 2 = 0
7744                  * = \w).  If it is in that class, isFOO_lc() will return 1,
7745                  * and since 'to_complement' is 0, the result will stay TRUE,
7746                  * and we exit the loop.  Suppose instead that bit 0 is 0, but
7747                  * bit 1 is 1.  That means there is a match if the character
7748                  * matches \W.  We won't bother to call isFOO_lc() on bit 0,
7749                  * but will on bit 1.  On the second iteration 'to_complement'
7750                  * will be 1, so the exclusive or will reverse things, so we
7751                  * are testing for \W.  On the third iteration, 'to_complement'
7752                  * will be 0, and we would be testing for \s; the fourth
7753                  * iteration would test for \S, etc.
7754                  *
7755                  * Note that this code assumes that all the classes are closed
7756                  * under folding.  For example, if a character matches \w, then
7757                  * its fold does too; and vice versa.  This should be true for
7758                  * any well-behaved locale for all the currently defined Posix
7759                  * classes, except for :lower: and :upper:, which are handled
7760                  * by the pseudo-class :cased: which matches if either of the
7761                  * other two does.  To get rid of this assumption, an outer
7762                  * loop could be used below to iterate over both the source
7763                  * character, and its fold (if different) */
7764
7765                 int count = 0;
7766                 int to_complement = 0;
7767
7768                 while (count < ANYOF_MAX) {
7769                     if (ANYOF_POSIXL_TEST(n, count)
7770                         && to_complement ^ cBOOL(isFOO_lc(count/2, (U8) c)))
7771                     {
7772                         match = TRUE;
7773                         break;
7774                     }
7775                     count++;
7776                     to_complement ^= 1;
7777                 }
7778             }
7779         }
7780     }
7781
7782
7783     /* If the bitmap didn't (or couldn't) match, and something outside the
7784      * bitmap could match, try that. */
7785     if (!match) {
7786         if (c >= 256 && (flags & ANYOF_ABOVE_LATIN1_ALL)) {
7787             match = TRUE;       /* Everything above 255 matches */
7788         }
7789         else if ((flags & ANYOF_NONBITMAP_NON_UTF8)
7790                   || (utf8_target && (flags & ANYOF_UTF8))
7791                   || ((flags & ANYOF_LOC_FOLD)
7792                        && IN_UTF8_CTYPE_LOCALE
7793                        && ARG(n) != ANYOF_NONBITMAP_EMPTY))
7794         {
7795             SV* only_utf8_locale = NULL;
7796             SV * const sw = _get_regclass_nonbitmap_data(prog, n, TRUE, 0,
7797                                                             &only_utf8_locale);
7798             if (sw) {
7799                 U8 * utf8_p;
7800                 if (utf8_target) {
7801                     utf8_p = (U8 *) p;
7802                 } else { /* Convert to utf8 */
7803                     STRLEN len = 1;
7804                     utf8_p = bytes_to_utf8(p, &len);
7805                 }
7806
7807                 if (swash_fetch(sw, utf8_p, TRUE)) {
7808                     match = TRUE;
7809                 }
7810
7811                 /* If we allocated a string above, free it */
7812                 if (! utf8_target) Safefree(utf8_p);
7813             }
7814             if (! match && only_utf8_locale && IN_UTF8_CTYPE_LOCALE) {
7815                 match = _invlist_contains_cp(only_utf8_locale, c);
7816             }
7817         }
7818
7819         if (UNICODE_IS_SUPER(c)
7820             && (flags & ANYOF_WARN_SUPER)
7821             && ckWARN_d(WARN_NON_UNICODE))
7822         {
7823             Perl_warner(aTHX_ packWARN(WARN_NON_UNICODE),
7824                 "Matched non-Unicode code point 0x%04"UVXf" against Unicode property; may not be portable", c);
7825         }
7826     }
7827
7828 #if ANYOF_INVERT != 1
7829     /* Depending on compiler optimization cBOOL takes time, so if don't have to
7830      * use it, don't */
7831 #   error ANYOF_INVERT needs to be set to 1, or guarded with cBOOL below,
7832 #endif
7833
7834     /* The xor complements the return if to invert: 1^1 = 0, 1^0 = 1 */
7835     return (flags & ANYOF_INVERT) ^ match;
7836 }
7837
7838 STATIC U8 *
7839 S_reghop3(U8 *s, SSize_t off, const U8* lim)
7840 {
7841     /* return the position 'off' UTF-8 characters away from 's', forward if
7842      * 'off' >= 0, backwards if negative.  But don't go outside of position
7843      * 'lim', which better be < s  if off < 0 */
7844
7845     dVAR;
7846
7847     PERL_ARGS_ASSERT_REGHOP3;
7848
7849     if (off >= 0) {
7850         while (off-- && s < lim) {
7851             /* XXX could check well-formedness here */
7852             s += UTF8SKIP(s);
7853         }
7854     }
7855     else {
7856         while (off++ && s > lim) {
7857             s--;
7858             if (UTF8_IS_CONTINUED(*s)) {
7859                 while (s > lim && UTF8_IS_CONTINUATION(*s))
7860                     s--;
7861             }
7862             /* XXX could check well-formedness here */
7863         }
7864     }
7865     return s;
7866 }
7867
7868 STATIC U8 *
7869 S_reghop4(U8 *s, SSize_t off, const U8* llim, const U8* rlim)
7870 {
7871     dVAR;
7872
7873     PERL_ARGS_ASSERT_REGHOP4;
7874
7875     if (off >= 0) {
7876         while (off-- && s < rlim) {
7877             /* XXX could check well-formedness here */
7878             s += UTF8SKIP(s);
7879         }
7880     }
7881     else {
7882         while (off++ && s > llim) {
7883             s--;
7884             if (UTF8_IS_CONTINUED(*s)) {
7885                 while (s > llim && UTF8_IS_CONTINUATION(*s))
7886                     s--;
7887             }
7888             /* XXX could check well-formedness here */
7889         }
7890     }
7891     return s;
7892 }
7893
7894 /* like reghop3, but returns NULL on overrun, rather than returning last
7895  * char pos */
7896
7897 STATIC U8 *
7898 S_reghopmaybe3(U8* s, SSize_t off, const U8* lim)
7899 {
7900     dVAR;
7901
7902     PERL_ARGS_ASSERT_REGHOPMAYBE3;
7903
7904     if (off >= 0) {
7905         while (off-- && s < lim) {
7906             /* XXX could check well-formedness here */
7907             s += UTF8SKIP(s);
7908         }
7909         if (off >= 0)
7910             return NULL;
7911     }
7912     else {
7913         while (off++ && s > lim) {
7914             s--;
7915             if (UTF8_IS_CONTINUED(*s)) {
7916                 while (s > lim && UTF8_IS_CONTINUATION(*s))
7917                     s--;
7918             }
7919             /* XXX could check well-formedness here */
7920         }
7921         if (off <= 0)
7922             return NULL;
7923     }
7924     return s;
7925 }
7926
7927
7928 /* when executing a regex that may have (?{}), extra stuff needs setting
7929    up that will be visible to the called code, even before the current
7930    match has finished. In particular:
7931
7932    * $_ is localised to the SV currently being matched;
7933    * pos($_) is created if necessary, ready to be updated on each call-out
7934      to code;
7935    * a fake PMOP is created that can be set to PL_curpm (normally PL_curpm
7936      isn't set until the current pattern is successfully finished), so that
7937      $1 etc of the match-so-far can be seen;
7938    * save the old values of subbeg etc of the current regex, and  set then
7939      to the current string (again, this is normally only done at the end
7940      of execution)
7941 */
7942
7943 static void
7944 S_setup_eval_state(pTHX_ regmatch_info *const reginfo)
7945 {
7946     MAGIC *mg;
7947     regexp *const rex = ReANY(reginfo->prog);
7948     regmatch_info_aux_eval *eval_state = reginfo->info_aux_eval;
7949
7950     eval_state->rex = rex;
7951
7952     if (reginfo->sv) {
7953         /* Make $_ available to executed code. */
7954         if (reginfo->sv != DEFSV) {
7955             SAVE_DEFSV;
7956             DEFSV_set(reginfo->sv);
7957         }
7958
7959         if (!(mg = mg_find_mglob(reginfo->sv))) {
7960             /* prepare for quick setting of pos */
7961             mg = sv_magicext_mglob(reginfo->sv);
7962             mg->mg_len = -1;
7963         }
7964         eval_state->pos_magic = mg;
7965         eval_state->pos       = mg->mg_len;
7966         eval_state->pos_flags = mg->mg_flags;
7967     }
7968     else
7969         eval_state->pos_magic = NULL;
7970
7971     if (!PL_reg_curpm) {
7972         /* PL_reg_curpm is a fake PMOP that we can attach the current
7973          * regex to and point PL_curpm at, so that $1 et al are visible
7974          * within a /(?{})/. It's just allocated once per interpreter the
7975          * first time its needed */
7976         Newxz(PL_reg_curpm, 1, PMOP);
7977 #ifdef USE_ITHREADS
7978         {
7979             SV* const repointer = &PL_sv_undef;
7980             /* this regexp is also owned by the new PL_reg_curpm, which
7981                will try to free it.  */
7982             av_push(PL_regex_padav, repointer);
7983             PL_reg_curpm->op_pmoffset = av_tindex(PL_regex_padav);
7984             PL_regex_pad = AvARRAY(PL_regex_padav);
7985         }
7986 #endif
7987     }
7988     SET_reg_curpm(reginfo->prog);
7989     eval_state->curpm = PL_curpm;
7990     PL_curpm = PL_reg_curpm;
7991     if (RXp_MATCH_COPIED(rex)) {
7992         /*  Here is a serious problem: we cannot rewrite subbeg,
7993             since it may be needed if this match fails.  Thus
7994             $` inside (?{}) could fail... */
7995         eval_state->subbeg     = rex->subbeg;
7996         eval_state->sublen     = rex->sublen;
7997         eval_state->suboffset  = rex->suboffset;
7998         eval_state->subcoffset = rex->subcoffset;
7999 #ifdef PERL_ANY_COW
8000         eval_state->saved_copy = rex->saved_copy;
8001 #endif
8002         RXp_MATCH_COPIED_off(rex);
8003     }
8004     else
8005         eval_state->subbeg = NULL;
8006     rex->subbeg = (char *)reginfo->strbeg;
8007     rex->suboffset = 0;
8008     rex->subcoffset = 0;
8009     rex->sublen = reginfo->strend - reginfo->strbeg;
8010 }
8011
8012
8013 /* destructor to clear up regmatch_info_aux and regmatch_info_aux_eval */
8014
8015 static void
8016 S_cleanup_regmatch_info_aux(pTHX_ void *arg)
8017 {
8018     dVAR;
8019     regmatch_info_aux *aux = (regmatch_info_aux *) arg;
8020     regmatch_info_aux_eval *eval_state =  aux->info_aux_eval;
8021     regmatch_slab *s;
8022
8023     Safefree(aux->poscache);
8024
8025     if (eval_state) {
8026
8027         /* undo the effects of S_setup_eval_state() */
8028
8029         if (eval_state->subbeg) {
8030             regexp * const rex = eval_state->rex;
8031             rex->subbeg     = eval_state->subbeg;
8032             rex->sublen     = eval_state->sublen;
8033             rex->suboffset  = eval_state->suboffset;
8034             rex->subcoffset = eval_state->subcoffset;
8035 #ifdef PERL_ANY_COW
8036             rex->saved_copy = eval_state->saved_copy;
8037 #endif
8038             RXp_MATCH_COPIED_on(rex);
8039         }
8040         if (eval_state->pos_magic)
8041         {
8042             eval_state->pos_magic->mg_len = eval_state->pos;
8043             eval_state->pos_magic->mg_flags =
8044                  (eval_state->pos_magic->mg_flags & ~MGf_BYTES)
8045                | (eval_state->pos_flags & MGf_BYTES);
8046         }
8047
8048         PL_curpm = eval_state->curpm;
8049     }
8050
8051     PL_regmatch_state = aux->old_regmatch_state;
8052     PL_regmatch_slab  = aux->old_regmatch_slab;
8053
8054     /* free all slabs above current one - this must be the last action
8055      * of this function, as aux and eval_state are allocated within
8056      * slabs and may be freed here */
8057
8058     s = PL_regmatch_slab->next;
8059     if (s) {
8060         PL_regmatch_slab->next = NULL;
8061         while (s) {
8062             regmatch_slab * const osl = s;
8063             s = s->next;
8064             Safefree(osl);
8065         }
8066     }
8067 }
8068
8069
8070 STATIC void
8071 S_to_utf8_substr(pTHX_ regexp *prog)
8072 {
8073     /* Converts substr fields in prog from bytes to UTF-8, calling fbm_compile
8074      * on the converted value */
8075
8076     int i = 1;
8077
8078     PERL_ARGS_ASSERT_TO_UTF8_SUBSTR;
8079
8080     do {
8081         if (prog->substrs->data[i].substr
8082             && !prog->substrs->data[i].utf8_substr) {
8083             SV* const sv = newSVsv(prog->substrs->data[i].substr);
8084             prog->substrs->data[i].utf8_substr = sv;
8085             sv_utf8_upgrade(sv);
8086             if (SvVALID(prog->substrs->data[i].substr)) {
8087                 if (SvTAIL(prog->substrs->data[i].substr)) {
8088                     /* Trim the trailing \n that fbm_compile added last
8089                        time.  */
8090                     SvCUR_set(sv, SvCUR(sv) - 1);
8091                     /* Whilst this makes the SV technically "invalid" (as its
8092                        buffer is no longer followed by "\0") when fbm_compile()
8093                        adds the "\n" back, a "\0" is restored.  */
8094                     fbm_compile(sv, FBMcf_TAIL);
8095                 } else
8096                     fbm_compile(sv, 0);
8097             }
8098             if (prog->substrs->data[i].substr == prog->check_substr)
8099                 prog->check_utf8 = sv;
8100         }
8101     } while (i--);
8102 }
8103
8104 STATIC bool
8105 S_to_byte_substr(pTHX_ regexp *prog)
8106 {
8107     /* Converts substr fields in prog from UTF-8 to bytes, calling fbm_compile
8108      * on the converted value; returns FALSE if can't be converted. */
8109
8110     dVAR;
8111     int i = 1;
8112
8113     PERL_ARGS_ASSERT_TO_BYTE_SUBSTR;
8114
8115     do {
8116         if (prog->substrs->data[i].utf8_substr
8117             && !prog->substrs->data[i].substr) {
8118             SV* sv = newSVsv(prog->substrs->data[i].utf8_substr);
8119             if (! sv_utf8_downgrade(sv, TRUE)) {
8120                 return FALSE;
8121             }
8122             if (SvVALID(prog->substrs->data[i].utf8_substr)) {
8123                 if (SvTAIL(prog->substrs->data[i].utf8_substr)) {
8124                     /* Trim the trailing \n that fbm_compile added last
8125                         time.  */
8126                     SvCUR_set(sv, SvCUR(sv) - 1);
8127                     fbm_compile(sv, FBMcf_TAIL);
8128                 } else
8129                     fbm_compile(sv, 0);
8130             }
8131             prog->substrs->data[i].substr = sv;
8132             if (prog->substrs->data[i].utf8_substr == prog->check_utf8)
8133                 prog->check_substr = sv;
8134         }
8135     } while (i--);
8136
8137     return TRUE;
8138 }
8139
8140 /*
8141  * Local variables:
8142  * c-indentation-style: bsd
8143  * c-basic-offset: 4
8144  * indent-tabs-mode: nil
8145  * End:
8146  *
8147  * ex: set ts=8 sts=4 sw=4 et:
8148  */