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