This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
4526d23e14f76bcd3f0d015d231e3a431261e481
[perl5.git] / regexec.c
1 /*    regexec.c
2  */
3
4 /*
5  *      One Ring to rule them all, One Ring to find them
6  &
7  *     [p.v of _The Lord of the Rings_, opening poem]
8  *     [p.50 of _The Lord of the Rings_, I/iii: "The Shadow of the Past"]
9  *     [p.254 of _The Lord of the Rings_, II/ii: "The Council of Elrond"]
10  */
11
12 /* This file contains functions for executing a regular expression.  See
13  * also regcomp.c which funnily enough, contains functions for compiling
14  * a regular expression.
15  *
16  * This file is also copied at build time to ext/re/re_exec.c, where
17  * it's built with -DPERL_EXT_RE_BUILD -DPERL_EXT_RE_DEBUG -DPERL_EXT.
18  * This causes the main functions to be compiled under new names and with
19  * debugging support added, which makes "use re 'debug'" work.
20  */
21
22 /* NOTE: this is derived from Henry Spencer's regexp code, and should not
23  * confused with the original package (see point 3 below).  Thanks, Henry!
24  */
25
26 /* Additional note: this code is very heavily munged from Henry's version
27  * in places.  In some spots I've traded clarity for efficiency, so don't
28  * blame Henry for some of the lack of readability.
29  */
30
31 /* The names of the functions have been changed from regcomp and
32  * regexec to  pregcomp and pregexec in order to avoid conflicts
33  * with the POSIX routines of the same names.
34 */
35
36 #ifdef PERL_EXT_RE_BUILD
37 #include "re_top.h"
38 #endif
39
40 /*
41  * pregcomp and pregexec -- regsub and regerror are not used in perl
42  *
43  *      Copyright (c) 1986 by University of Toronto.
44  *      Written by Henry Spencer.  Not derived from licensed software.
45  *
46  *      Permission is granted to anyone to use this software for any
47  *      purpose on any computer system, and to redistribute it freely,
48  *      subject to the following restrictions:
49  *
50  *      1. The author is not responsible for the consequences of use of
51  *              this software, no matter how awful, even if they arise
52  *              from defects in it.
53  *
54  *      2. The origin of this software must not be misrepresented, either
55  *              by explicit claim or by omission.
56  *
57  *      3. Altered versions must be plainly marked as such, and must not
58  *              be misrepresented as being the original software.
59  *
60  ****    Alterations to Henry's code are...
61  ****
62  ****    Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
63  ****    2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
64  ****    by Larry Wall and others
65  ****
66  ****    You may distribute under the terms of either the GNU General Public
67  ****    License or the Artistic License, as specified in the README file.
68  *
69  * Beware that some of this code is subtly aware of the way operator
70  * precedence is structured in regular expressions.  Serious changes in
71  * regular-expression syntax might require a total rethink.
72  */
73 #include "EXTERN.h"
74 #define PERL_IN_REGEXEC_C
75 #include "perl.h"
76
77 #ifdef PERL_IN_XSUB_RE
78 #  include "re_comp.h"
79 #else
80 #  include "regcomp.h"
81 #endif
82
83 #include "inline_invlist.c"
84 #include "unicode_constants.h"
85
86 #ifdef DEBUGGING
87 /* At least one required character in the target string is expressible only in
88  * UTF-8. */
89 static const char* const non_utf8_target_but_utf8_required
90                 = "Can't match, because target string needs to be in UTF-8\n";
91 #endif
92
93 #define NON_UTF8_TARGET_BUT_UTF8_REQUIRED(target) STMT_START { \
94     DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%s", non_utf8_target_but_utf8_required));\
95     goto target; \
96 } STMT_END
97
98 #define HAS_NONLATIN1_FOLD_CLOSURE(i) _HAS_NONLATIN1_FOLD_CLOSURE_ONLY_FOR_USE_BY_REGCOMP_DOT_C_AND_REGEXEC_DOT_C(i)
99
100 #ifndef STATIC
101 #define STATIC  static
102 #endif
103
104 /* Valid only for non-utf8 strings: avoids the reginclass
105  * call if there are no complications: i.e., if everything matchable is
106  * straight forward in the bitmap */
107 #define REGINCLASS(prog,p,c)  (ANYOF_FLAGS(p) ? reginclass(prog,p,c,c+1,0)   \
108                                               : ANYOF_BITMAP_TEST(p,*(c)))
109
110 /*
111  * Forwards.
112  */
113
114 #define CHR_SVLEN(sv) (utf8_target ? sv_len_utf8(sv) : SvCUR(sv))
115 #define CHR_DIST(a,b) (reginfo->is_utf8_target ? utf8_distance(a,b) : a - b)
116
117 #define HOPc(pos,off) \
118         (char *)(reginfo->is_utf8_target \
119             ? reghop3((U8*)pos, off, \
120                     (U8*)(off >= 0 ? reginfo->strend : reginfo->strbeg)) \
121             : (U8*)(pos + off))
122
123 #define HOPBACKc(pos, off) \
124         (char*)(reginfo->is_utf8_target \
125             ? reghopmaybe3((U8*)pos, -off, (U8*)(reginfo->strbeg)) \
126             : (pos - off >= reginfo->strbeg)    \
127                 ? (U8*)pos - off                \
128                 : NULL)
129
130 #define HOP3(pos,off,lim) (reginfo->is_utf8_target  ? reghop3((U8*)(pos), off, (U8*)(lim)) : (U8*)(pos + off))
131 #define HOP3c(pos,off,lim) ((char*)HOP3(pos,off,lim))
132
133 /* lim must be +ve. Returns NULL on overshoot */
134 #define HOPMAYBE3(pos,off,lim) \
135         (reginfo->is_utf8_target                        \
136             ? reghopmaybe3((U8*)pos, off, (U8*)(lim))   \
137             : ((U8*)pos + off <= lim)                   \
138                 ? (U8*)pos + off                        \
139                 : NULL)
140
141 /* like HOP3, but limits the result to <= lim even for the non-utf8 case.
142  * off must be >=0; args should be vars rather than expressions */
143 #define HOP3lim(pos,off,lim) (reginfo->is_utf8_target \
144     ? reghop3((U8*)(pos), off, (U8*)(lim)) \
145     : (U8*)((pos + off) > lim ? lim : (pos + off)))
146
147 #define HOP4(pos,off,llim, rlim) (reginfo->is_utf8_target \
148     ? reghop4((U8*)(pos), off, (U8*)(llim), (U8*)(rlim)) \
149     : (U8*)(pos + off))
150 #define HOP4c(pos,off,llim, rlim) ((char*)HOP4(pos,off,llim, rlim))
151
152 #define NEXTCHR_EOS -10 /* nextchr has fallen off the end */
153 #define NEXTCHR_IS_EOS (nextchr < 0)
154
155 #define SET_nextchr \
156     nextchr = ((locinput < reginfo->strend) ? UCHARAT(locinput) : NEXTCHR_EOS)
157
158 #define SET_locinput(p) \
159     locinput = (p);  \
160     SET_nextchr
161
162
163 #define LOAD_UTF8_CHARCLASS(swash_ptr, property_name, invlist) STMT_START {   \
164         if (!swash_ptr) {                                                     \
165             U8 flags = _CORE_SWASH_INIT_ACCEPT_INVLIST;                       \
166             swash_ptr = _core_swash_init("utf8", property_name, &PL_sv_undef, \
167                                          1, 0, invlist, &flags);              \
168             assert(swash_ptr);                                                \
169         }                                                                     \
170     } STMT_END
171
172 /* If in debug mode, we test that a known character properly matches */
173 #ifdef DEBUGGING
174 #   define LOAD_UTF8_CHARCLASS_DEBUG_TEST(swash_ptr,                          \
175                                           property_name,                      \
176                                           invlist,                            \
177                                           utf8_char_in_property)              \
178         LOAD_UTF8_CHARCLASS(swash_ptr, property_name, invlist);               \
179         assert(swash_fetch(swash_ptr, (U8 *) utf8_char_in_property, TRUE));
180 #else
181 #   define LOAD_UTF8_CHARCLASS_DEBUG_TEST(swash_ptr,                          \
182                                           property_name,                      \
183                                           invlist,                            \
184                                           utf8_char_in_property)              \
185         LOAD_UTF8_CHARCLASS(swash_ptr, property_name, invlist)
186 #endif
187
188 #define LOAD_UTF8_CHARCLASS_ALNUM() LOAD_UTF8_CHARCLASS_DEBUG_TEST(           \
189                                         PL_utf8_swash_ptrs[_CC_WORDCHAR],     \
190                                         "",                                   \
191                                         PL_XPosix_ptrs[_CC_WORDCHAR],         \
192                                         LATIN_CAPITAL_LETTER_SHARP_S_UTF8);
193
194 #define LOAD_UTF8_CHARCLASS_GCB()  /* Grapheme cluster boundaries */          \
195     STMT_START {                                                              \
196         LOAD_UTF8_CHARCLASS_DEBUG_TEST(PL_utf8_X_regular_begin,               \
197                                        "_X_regular_begin",                    \
198                                        NULL,                                  \
199                                        LATIN_CAPITAL_LETTER_SHARP_S_UTF8);    \
200         LOAD_UTF8_CHARCLASS_DEBUG_TEST(PL_utf8_X_extend,                      \
201                                        "_X_extend",                           \
202                                        NULL,                                  \
203                                        COMBINING_GRAVE_ACCENT_UTF8);          \
204     } STMT_END
205
206 #define PLACEHOLDER     /* Something for the preprocessor to grab onto */
207 /* TODO: Combine JUMPABLE and HAS_TEXT to cache OP(rn) */
208
209 /* for use after a quantifier and before an EXACT-like node -- japhy */
210 /* it would be nice to rework regcomp.sym to generate this stuff. sigh
211  *
212  * NOTE that *nothing* that affects backtracking should be in here, specifically
213  * VERBS must NOT be included. JUMPABLE is used to determine  if we can ignore a
214  * node that is in between two EXACT like nodes when ascertaining what the required
215  * "follow" character is. This should probably be moved to regex compile time
216  * although it may be done at run time beause of the REF possibility - more
217  * investigation required. -- demerphq
218 */
219 #define JUMPABLE(rn) (                                                             \
220     OP(rn) == OPEN ||                                                              \
221     (OP(rn) == CLOSE && (!cur_eval || cur_eval->u.eval.close_paren != ARG(rn))) || \
222     OP(rn) == EVAL ||                                                              \
223     OP(rn) == SUSPEND || OP(rn) == IFMATCH ||                                      \
224     OP(rn) == PLUS || OP(rn) == MINMOD ||                                          \
225     OP(rn) == KEEPS ||                                                             \
226     (PL_regkind[OP(rn)] == CURLY && ARG1(rn) > 0)                                  \
227 )
228 #define IS_EXACT(rn) (PL_regkind[OP(rn)] == EXACT)
229
230 #define HAS_TEXT(rn) ( IS_EXACT(rn) || PL_regkind[OP(rn)] == REF )
231
232 #if 0 
233 /* Currently these are only used when PL_regkind[OP(rn)] == EXACT so
234    we don't need this definition.  XXX These are now out-of-sync*/
235 #define IS_TEXT(rn)   ( OP(rn)==EXACT   || OP(rn)==REF   || OP(rn)==NREF   )
236 #define IS_TEXTF(rn)  ( OP(rn)==EXACTFU || OP(rn)==EXACTFU_SS || OP(rn)==EXACTFA || OP(rn)==EXACTFA_NO_TRIE || OP(rn)==EXACTF || OP(rn)==REFF  || OP(rn)==NREFF )
237 #define IS_TEXTFL(rn) ( OP(rn)==EXACTFL || OP(rn)==REFFL || OP(rn)==NREFFL )
238
239 #else
240 /* ... so we use this as its faster. */
241 #define IS_TEXT(rn)   ( OP(rn)==EXACT || OP(rn)==EXACTL )
242 #define IS_TEXTFU(rn)  ( OP(rn)==EXACTFU || OP(rn)==EXACTFLU8 || OP(rn)==EXACTFU_SS || OP(rn) == EXACTFA || OP(rn) == EXACTFA_NO_TRIE)
243 #define IS_TEXTF(rn)  ( OP(rn)==EXACTF  )
244 #define IS_TEXTFL(rn) ( OP(rn)==EXACTFL )
245
246 #endif
247
248 /*
249   Search for mandatory following text node; for lookahead, the text must
250   follow but for lookbehind (rn->flags != 0) we skip to the next step.
251 */
252 #define FIND_NEXT_IMPT(rn) STMT_START {                                   \
253     while (JUMPABLE(rn)) { \
254         const OPCODE type = OP(rn); \
255         if (type == SUSPEND || PL_regkind[type] == CURLY) \
256             rn = NEXTOPER(NEXTOPER(rn)); \
257         else if (type == PLUS) \
258             rn = NEXTOPER(rn); \
259         else if (type == IFMATCH) \
260             rn = (rn->flags == 0) ? NEXTOPER(NEXTOPER(rn)) : rn + ARG(rn); \
261         else rn += NEXT_OFF(rn); \
262     } \
263 } STMT_END 
264
265 /* These constants are for finding GCB=LV and GCB=LVT in the CLUMP regnode.
266  * These are for the pre-composed Hangul syllables, which are all in a
267  * contiguous block and arranged there in such a way so as to facilitate
268  * alorithmic determination of their characteristics.  As such, they don't need
269  * a swash, but can be determined by simple arithmetic.  Almost all are
270  * GCB=LVT, but every 28th one is a GCB=LV */
271 #define SBASE 0xAC00    /* Start of block */
272 #define SCount 11172    /* Length of block */
273 #define TCount 28
274
275 #define SLAB_FIRST(s) (&(s)->states[0])
276 #define SLAB_LAST(s)  (&(s)->states[PERL_REGMATCH_SLAB_SLOTS-1])
277
278 static void S_setup_eval_state(pTHX_ regmatch_info *const reginfo);
279 static void S_cleanup_regmatch_info_aux(pTHX_ void *arg);
280 static regmatch_state * S_push_slab(pTHX);
281
282 #define REGCP_PAREN_ELEMS 3
283 #define REGCP_OTHER_ELEMS 3
284 #define REGCP_FRAME_ELEMS 1
285 /* REGCP_FRAME_ELEMS are not part of the REGCP_OTHER_ELEMS and
286  * are needed for the regexp context stack bookkeeping. */
287
288 STATIC CHECKPOINT
289 S_regcppush(pTHX_ const regexp *rex, I32 parenfloor, U32 maxopenparen)
290 {
291     const int retval = PL_savestack_ix;
292     const int paren_elems_to_push =
293                 (maxopenparen - parenfloor) * REGCP_PAREN_ELEMS;
294     const UV total_elems = paren_elems_to_push + REGCP_OTHER_ELEMS;
295     const UV elems_shifted = total_elems << SAVE_TIGHT_SHIFT;
296     I32 p;
297     GET_RE_DEBUG_FLAGS_DECL;
298
299     PERL_ARGS_ASSERT_REGCPPUSH;
300
301     if (paren_elems_to_push < 0)
302         Perl_croak(aTHX_ "panic: paren_elems_to_push, %i < 0, maxopenparen: %i parenfloor: %i REGCP_PAREN_ELEMS: %u",
303                    (int)paren_elems_to_push, (int)maxopenparen,
304                    (int)parenfloor, (unsigned)REGCP_PAREN_ELEMS);
305
306     if ((elems_shifted >> SAVE_TIGHT_SHIFT) != total_elems)
307         Perl_croak(aTHX_ "panic: paren_elems_to_push offset %"UVuf
308                    " out of range (%lu-%ld)",
309                    total_elems,
310                    (unsigned long)maxopenparen,
311                    (long)parenfloor);
312
313     SSGROW(total_elems + REGCP_FRAME_ELEMS);
314     
315     DEBUG_BUFFERS_r(
316         if ((int)maxopenparen > (int)parenfloor)
317             PerlIO_printf(Perl_debug_log,
318                 "rex=0x%"UVxf" offs=0x%"UVxf": saving capture indices:\n",
319                 PTR2UV(rex),
320                 PTR2UV(rex->offs)
321             );
322     );
323     for (p = parenfloor+1; p <= (I32)maxopenparen;  p++) {
324 /* REGCP_PARENS_ELEMS are pushed per pairs of parentheses. */
325         SSPUSHIV(rex->offs[p].end);
326         SSPUSHIV(rex->offs[p].start);
327         SSPUSHINT(rex->offs[p].start_tmp);
328         DEBUG_BUFFERS_r(PerlIO_printf(Perl_debug_log,
329             "    \\%"UVuf": %"IVdf"(%"IVdf")..%"IVdf"\n",
330             (UV)p,
331             (IV)rex->offs[p].start,
332             (IV)rex->offs[p].start_tmp,
333             (IV)rex->offs[p].end
334         ));
335     }
336 /* REGCP_OTHER_ELEMS are pushed in any case, parentheses or no. */
337     SSPUSHINT(maxopenparen);
338     SSPUSHINT(rex->lastparen);
339     SSPUSHINT(rex->lastcloseparen);
340     SSPUSHUV(SAVEt_REGCONTEXT | elems_shifted); /* Magic cookie. */
341
342     return retval;
343 }
344
345 /* These are needed since we do not localize EVAL nodes: */
346 #define REGCP_SET(cp)                                           \
347     DEBUG_STATE_r(                                              \
348             PerlIO_printf(Perl_debug_log,                       \
349                 "  Setting an EVAL scope, savestack=%"IVdf"\n", \
350                 (IV)PL_savestack_ix));                          \
351     cp = PL_savestack_ix
352
353 #define REGCP_UNWIND(cp)                                        \
354     DEBUG_STATE_r(                                              \
355         if (cp != PL_savestack_ix)                              \
356             PerlIO_printf(Perl_debug_log,                       \
357                 "  Clearing an EVAL scope, savestack=%"IVdf"..%"IVdf"\n", \
358                 (IV)(cp), (IV)PL_savestack_ix));                \
359     regcpblow(cp)
360
361 #define UNWIND_PAREN(lp, lcp)               \
362     for (n = rex->lastparen; n > lp; n--)   \
363         rex->offs[n].end = -1;              \
364     rex->lastparen = n;                     \
365     rex->lastcloseparen = lcp;
366
367
368 STATIC void
369 S_regcppop(pTHX_ regexp *rex, U32 *maxopenparen_p)
370 {
371     UV i;
372     U32 paren;
373     GET_RE_DEBUG_FLAGS_DECL;
374
375     PERL_ARGS_ASSERT_REGCPPOP;
376
377     /* Pop REGCP_OTHER_ELEMS before the parentheses loop starts. */
378     i = SSPOPUV;
379     assert((i & SAVE_MASK) == SAVEt_REGCONTEXT); /* Check that the magic cookie is there. */
380     i >>= SAVE_TIGHT_SHIFT; /* Parentheses elements to pop. */
381     rex->lastcloseparen = SSPOPINT;
382     rex->lastparen = SSPOPINT;
383     *maxopenparen_p = SSPOPINT;
384
385     i -= REGCP_OTHER_ELEMS;
386     /* Now restore the parentheses context. */
387     DEBUG_BUFFERS_r(
388         if (i || rex->lastparen + 1 <= rex->nparens)
389             PerlIO_printf(Perl_debug_log,
390                 "rex=0x%"UVxf" offs=0x%"UVxf": restoring capture indices to:\n",
391                 PTR2UV(rex),
392                 PTR2UV(rex->offs)
393             );
394     );
395     paren = *maxopenparen_p;
396     for ( ; i > 0; i -= REGCP_PAREN_ELEMS) {
397         SSize_t tmps;
398         rex->offs[paren].start_tmp = SSPOPINT;
399         rex->offs[paren].start = SSPOPIV;
400         tmps = SSPOPIV;
401         if (paren <= rex->lastparen)
402             rex->offs[paren].end = tmps;
403         DEBUG_BUFFERS_r( PerlIO_printf(Perl_debug_log,
404             "    \\%"UVuf": %"IVdf"(%"IVdf")..%"IVdf"%s\n",
405             (UV)paren,
406             (IV)rex->offs[paren].start,
407             (IV)rex->offs[paren].start_tmp,
408             (IV)rex->offs[paren].end,
409             (paren > rex->lastparen ? "(skipped)" : ""));
410         );
411         paren--;
412     }
413 #if 1
414     /* It would seem that the similar code in regtry()
415      * already takes care of this, and in fact it is in
416      * a better location to since this code can #if 0-ed out
417      * but the code in regtry() is needed or otherwise tests
418      * requiring null fields (pat.t#187 and split.t#{13,14}
419      * (as of patchlevel 7877)  will fail.  Then again,
420      * this code seems to be necessary or otherwise
421      * this erroneously leaves $1 defined: "1" =~ /^(?:(\d)x)?\d$/
422      * --jhi updated by dapm */
423     for (i = rex->lastparen + 1; i <= rex->nparens; i++) {
424         if (i > *maxopenparen_p)
425             rex->offs[i].start = -1;
426         rex->offs[i].end = -1;
427         DEBUG_BUFFERS_r( PerlIO_printf(Perl_debug_log,
428             "    \\%"UVuf": %s   ..-1 undeffing\n",
429             (UV)i,
430             (i > *maxopenparen_p) ? "-1" : "  "
431         ));
432     }
433 #endif
434 }
435
436 /* restore the parens and associated vars at savestack position ix,
437  * but without popping the stack */
438
439 STATIC void
440 S_regcp_restore(pTHX_ regexp *rex, I32 ix, U32 *maxopenparen_p)
441 {
442     I32 tmpix = PL_savestack_ix;
443     PL_savestack_ix = ix;
444     regcppop(rex, maxopenparen_p);
445     PL_savestack_ix = tmpix;
446 }
447
448 #define regcpblow(cp) LEAVE_SCOPE(cp)   /* Ignores regcppush()ed data. */
449
450 STATIC bool
451 S_isFOO_lc(pTHX_ const U8 classnum, const U8 character)
452 {
453     /* Returns a boolean as to whether or not 'character' is a member of the
454      * Posix character class given by 'classnum' that should be equivalent to a
455      * value in the typedef '_char_class_number'.
456      *
457      * Ideally this could be replaced by a just an array of function pointers
458      * to the C library functions that implement the macros this calls.
459      * However, to compile, the precise function signatures are required, and
460      * these may vary from platform to to platform.  To avoid having to figure
461      * out what those all are on each platform, I (khw) am using this method,
462      * which adds an extra layer of function call overhead (unless the C
463      * optimizer strips it away).  But we don't particularly care about
464      * performance with locales anyway. */
465
466     switch ((_char_class_number) classnum) {
467         case _CC_ENUM_ALPHANUMERIC: return isALPHANUMERIC_LC(character);
468         case _CC_ENUM_ALPHA:     return isALPHA_LC(character);
469         case _CC_ENUM_ASCII:     return isASCII_LC(character);
470         case _CC_ENUM_BLANK:     return isBLANK_LC(character);
471         case _CC_ENUM_CASED:     return isLOWER_LC(character)
472                                         || isUPPER_LC(character);
473         case _CC_ENUM_CNTRL:     return isCNTRL_LC(character);
474         case _CC_ENUM_DIGIT:     return isDIGIT_LC(character);
475         case _CC_ENUM_GRAPH:     return isGRAPH_LC(character);
476         case _CC_ENUM_LOWER:     return isLOWER_LC(character);
477         case _CC_ENUM_PRINT:     return isPRINT_LC(character);
478         case _CC_ENUM_PSXSPC:    return isPSXSPC_LC(character);
479         case _CC_ENUM_PUNCT:     return isPUNCT_LC(character);
480         case _CC_ENUM_SPACE:     return isSPACE_LC(character);
481         case _CC_ENUM_UPPER:     return isUPPER_LC(character);
482         case _CC_ENUM_WORDCHAR:  return isWORDCHAR_LC(character);
483         case _CC_ENUM_XDIGIT:    return isXDIGIT_LC(character);
484         default:    /* VERTSPACE should never occur in locales */
485             Perl_croak(aTHX_ "panic: isFOO_lc() has an unexpected character class '%d'", classnum);
486     }
487
488     NOT_REACHED; /* NOTREACHED */
489     return FALSE;
490 }
491
492 STATIC bool
493 S_isFOO_utf8_lc(pTHX_ const U8 classnum, const U8* character)
494 {
495     /* Returns a boolean as to whether or not the (well-formed) UTF-8-encoded
496      * 'character' is a member of the Posix character class given by 'classnum'
497      * that should be equivalent to a value in the typedef
498      * '_char_class_number'.
499      *
500      * This just calls isFOO_lc on the code point for the character if it is in
501      * the range 0-255.  Outside that range, all characters use Unicode
502      * rules, ignoring any locale.  So use the Unicode function if this class
503      * requires a swash, and use the Unicode macro otherwise. */
504
505     PERL_ARGS_ASSERT_ISFOO_UTF8_LC;
506
507     if (UTF8_IS_INVARIANT(*character)) {
508         return isFOO_lc(classnum, *character);
509     }
510     else if (UTF8_IS_DOWNGRADEABLE_START(*character)) {
511         return isFOO_lc(classnum,
512                         TWO_BYTE_UTF8_TO_NATIVE(*character, *(character + 1)));
513     }
514
515     if (classnum < _FIRST_NON_SWASH_CC) {
516
517         /* Initialize the swash unless done already */
518         if (! PL_utf8_swash_ptrs[classnum]) {
519             U8 flags = _CORE_SWASH_INIT_ACCEPT_INVLIST;
520             PL_utf8_swash_ptrs[classnum] =
521                     _core_swash_init("utf8",
522                                      "",
523                                      &PL_sv_undef, 1, 0,
524                                      PL_XPosix_ptrs[classnum], &flags);
525         }
526
527         return cBOOL(swash_fetch(PL_utf8_swash_ptrs[classnum], (U8 *)
528                                  character,
529                                  TRUE /* is UTF */ ));
530     }
531
532     switch ((_char_class_number) classnum) {
533         case _CC_ENUM_SPACE:
534         case _CC_ENUM_PSXSPC:    return is_XPERLSPACE_high(character);
535
536         case _CC_ENUM_BLANK:     return is_HORIZWS_high(character);
537         case _CC_ENUM_XDIGIT:    return is_XDIGIT_high(character);
538         case _CC_ENUM_VERTSPACE: return is_VERTWS_high(character);
539         default:                 break;
540     }
541
542     return FALSE; /* Things like CNTRL are always below 256 */
543 }
544
545 /*
546  * pregexec and friends
547  */
548
549 #ifndef PERL_IN_XSUB_RE
550 /*
551  - pregexec - match a regexp against a string
552  */
553 I32
554 Perl_pregexec(pTHX_ REGEXP * const prog, char* stringarg, char *strend,
555          char *strbeg, SSize_t minend, SV *screamer, U32 nosave)
556 /* stringarg: the point in the string at which to begin matching */
557 /* strend:    pointer to null at end of string */
558 /* strbeg:    real beginning of string */
559 /* minend:    end of match must be >= minend bytes after stringarg. */
560 /* screamer:  SV being matched: only used for utf8 flag, pos() etc; string
561  *            itself is accessed via the pointers above */
562 /* nosave:    For optimizations. */
563 {
564     PERL_ARGS_ASSERT_PREGEXEC;
565
566     return
567         regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
568                       nosave ? 0 : REXEC_COPY_STR);
569 }
570 #endif
571
572
573
574 /* re_intuit_start():
575  *
576  * Based on some optimiser hints, try to find the earliest position in the
577  * string where the regex could match.
578  *
579  *   rx:     the regex to match against
580  *   sv:     the SV being matched: only used for utf8 flag; the string
581  *           itself is accessed via the pointers below. Note that on
582  *           something like an overloaded SV, SvPOK(sv) may be false
583  *           and the string pointers may point to something unrelated to
584  *           the SV itself.
585  *   strbeg: real beginning of string
586  *   strpos: the point in the string at which to begin matching
587  *   strend: pointer to the byte following the last char of the string
588  *   flags   currently unused; set to 0
589  *   data:   currently unused; set to NULL
590  *
591  * The basic idea of re_intuit_start() is to use some known information
592  * about the pattern, namely:
593  *
594  *   a) the longest known anchored substring (i.e. one that's at a
595  *      constant offset from the beginning of the pattern; but not
596  *      necessarily at a fixed offset from the beginning of the
597  *      string);
598  *   b) the longest floating substring (i.e. one that's not at a constant
599  *      offset from the beginning of the pattern);
600  *   c) Whether the pattern is anchored to the string; either
601  *      an absolute anchor: /^../, or anchored to \n: /^.../m,
602  *      or anchored to pos(): /\G/;
603  *   d) A start class: a real or synthetic character class which
604  *      represents which characters are legal at the start of the pattern;
605  *
606  * to either quickly reject the match, or to find the earliest position
607  * within the string at which the pattern might match, thus avoiding
608  * running the full NFA engine at those earlier locations, only to
609  * eventually fail and retry further along.
610  *
611  * Returns NULL if the pattern can't match, or returns the address within
612  * the string which is the earliest place the match could occur.
613  *
614  * The longest of the anchored and floating substrings is called 'check'
615  * and is checked first. The other is called 'other' and is checked
616  * second. The 'other' substring may not be present.  For example,
617  *
618  *    /(abc|xyz)ABC\d{0,3}DEFG/
619  *
620  * will have
621  *
622  *   check substr (float)    = "DEFG", offset 6..9 chars
623  *   other substr (anchored) = "ABC",  offset 3..3 chars
624  *   stclass = [ax]
625  *
626  * Be aware that during the course of this function, sometimes 'anchored'
627  * refers to a substring being anchored relative to the start of the
628  * pattern, and sometimes to the pattern itself being anchored relative to
629  * the string. For example:
630  *
631  *   /\dabc/:   "abc" is anchored to the pattern;
632  *   /^\dabc/:  "abc" is anchored to the pattern and the string;
633  *   /\d+abc/:  "abc" is anchored to neither the pattern nor the string;
634  *   /^\d+abc/: "abc" is anchored to neither the pattern nor the string,
635  *                    but the pattern is anchored to the string.
636  */
637
638 char *
639 Perl_re_intuit_start(pTHX_
640                     REGEXP * const rx,
641                     SV *sv,
642                     const char * const strbeg,
643                     char *strpos,
644                     char *strend,
645                     const U32 flags,
646                     re_scream_pos_data *data)
647 {
648     struct regexp *const prog = ReANY(rx);
649     SSize_t start_shift = prog->check_offset_min;
650     /* Should be nonnegative! */
651     SSize_t end_shift   = 0;
652     /* current lowest pos in string where the regex can start matching */
653     char *rx_origin = strpos;
654     SV *check;
655     const bool utf8_target = (sv && SvUTF8(sv)) ? 1 : 0; /* if no sv we have to assume bytes */
656     U8   other_ix = 1 - prog->substrs->check_ix;
657     bool ml_anch = 0;
658     char *other_last = strpos;/* latest pos 'other' substr already checked to */
659     char *check_at = NULL;              /* check substr found at this pos */
660     const I32 multiline = prog->extflags & RXf_PMf_MULTILINE;
661     RXi_GET_DECL(prog,progi);
662     regmatch_info reginfo_buf;  /* create some info to pass to find_byclass */
663     regmatch_info *const reginfo = &reginfo_buf;
664     GET_RE_DEBUG_FLAGS_DECL;
665
666     PERL_ARGS_ASSERT_RE_INTUIT_START;
667     PERL_UNUSED_ARG(flags);
668     PERL_UNUSED_ARG(data);
669
670     DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
671                 "Intuit: trying to determine minimum start position...\n"));
672
673     /* for now, assume that all substr offsets are positive. If at some point
674      * in the future someone wants to do clever things with look-behind and
675      * -ve offsets, they'll need to fix up any code in this function
676      * which uses these offsets. See the thread beginning
677      * <20140113145929.GF27210@iabyn.com>
678      */
679     assert(prog->substrs->data[0].min_offset >= 0);
680     assert(prog->substrs->data[0].max_offset >= 0);
681     assert(prog->substrs->data[1].min_offset >= 0);
682     assert(prog->substrs->data[1].max_offset >= 0);
683     assert(prog->substrs->data[2].min_offset >= 0);
684     assert(prog->substrs->data[2].max_offset >= 0);
685
686     /* for now, assume that if both present, that the floating substring
687      * doesn't start before the anchored substring.
688      * If you break this assumption (e.g. doing better optimisations
689      * with lookahead/behind), then you'll need to audit the code in this
690      * function carefully first
691      */
692     assert(
693             ! (  (prog->anchored_utf8 || prog->anchored_substr)
694               && (prog->float_utf8    || prog->float_substr))
695            || (prog->float_min_offset >= prog->anchored_offset));
696
697     /* byte rather than char calculation for efficiency. It fails
698      * to quickly reject some cases that can't match, but will reject
699      * them later after doing full char arithmetic */
700     if (prog->minlen > strend - strpos) {
701         DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
702                               "  String too short...\n"));
703         goto fail;
704     }
705
706     RX_MATCH_UTF8_set(rx,utf8_target);
707     reginfo->is_utf8_target = cBOOL(utf8_target);
708     reginfo->info_aux = NULL;
709     reginfo->strbeg = strbeg;
710     reginfo->strend = strend;
711     reginfo->is_utf8_pat = cBOOL(RX_UTF8(rx));
712     reginfo->intuit = 1;
713     /* not actually used within intuit, but zero for safety anyway */
714     reginfo->poscache_maxiter = 0;
715
716     if (utf8_target) {
717         if (!prog->check_utf8 && prog->check_substr)
718             to_utf8_substr(prog);
719         check = prog->check_utf8;
720     } else {
721         if (!prog->check_substr && prog->check_utf8) {
722             if (! to_byte_substr(prog)) {
723                 NON_UTF8_TARGET_BUT_UTF8_REQUIRED(fail);
724             }
725         }
726         check = prog->check_substr;
727     }
728
729     /* dump the various substring data */
730     DEBUG_OPTIMISE_MORE_r({
731         int i;
732         for (i=0; i<=2; i++) {
733             SV *sv = (utf8_target ? prog->substrs->data[i].utf8_substr
734                                   : prog->substrs->data[i].substr);
735             if (!sv)
736                 continue;
737
738             PerlIO_printf(Perl_debug_log,
739                 "  substrs[%d]: min=%"IVdf" max=%"IVdf" end shift=%"IVdf
740                 " useful=%"IVdf" utf8=%d [%s]\n",
741                 i,
742                 (IV)prog->substrs->data[i].min_offset,
743                 (IV)prog->substrs->data[i].max_offset,
744                 (IV)prog->substrs->data[i].end_shift,
745                 BmUSEFUL(sv),
746                 utf8_target ? 1 : 0,
747                 SvPEEK(sv));
748         }
749     });
750
751     if (prog->intflags & PREGf_ANCH) { /* Match at \G, beg-of-str or after \n */
752
753         /* ml_anch: check after \n?
754          *
755          * A note about IMPLICIT: on an un-anchored pattern beginning
756          * with /.*.../, these flags will have been added by the
757          * compiler:
758          *   /.*abc/, /.*abc/m:  PREGf_IMPLICIT | PREGf_ANCH_MBOL
759          *   /.*abc/s:           PREGf_IMPLICIT | PREGf_ANCH_SBOL
760          */
761         ml_anch =      (prog->intflags & PREGf_ANCH_MBOL)
762                    && !(prog->intflags & PREGf_IMPLICIT);
763
764         if (!ml_anch && !(prog->intflags & PREGf_IMPLICIT)) {
765             /* we are only allowed to match at BOS or \G */
766
767             /* trivially reject if there's a BOS anchor and we're not at BOS.
768              *
769              * Note that we don't try to do a similar quick reject for
770              * \G, since generally the caller will have calculated strpos
771              * based on pos() and gofs, so the string is already correctly
772              * anchored by definition; and handling the exceptions would
773              * be too fiddly (e.g. REXEC_IGNOREPOS).
774              */
775             if (   strpos != strbeg
776                 && (prog->intflags & PREGf_ANCH_SBOL))
777             {
778                 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
779                                 "  Not at start...\n"));
780                 goto fail;
781             }
782
783             /* in the presence of an anchor, the anchored (relative to the
784              * start of the regex) substr must also be anchored relative
785              * to strpos. So quickly reject if substr isn't found there.
786              * This works for \G too, because the caller will already have
787              * subtracted gofs from pos, and gofs is the offset from the
788              * \G to the start of the regex. For example, in /.abc\Gdef/,
789              * where substr="abcdef", pos()=3, gofs=4, offset_min=1:
790              * caller will have set strpos=pos()-4; we look for the substr
791              * at position pos()-4+1, which lines up with the "a" */
792
793             if (prog->check_offset_min == prog->check_offset_max
794                 && !(prog->intflags & PREGf_CANY_SEEN))
795             {
796                 /* Substring at constant offset from beg-of-str... */
797                 SSize_t slen = SvCUR(check);
798                 char *s = HOP3c(strpos, prog->check_offset_min, strend);
799             
800                 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
801                     "  Looking for check substr at fixed offset %"IVdf"...\n",
802                     (IV)prog->check_offset_min));
803
804                 if (SvTAIL(check)) {
805                     /* In this case, the regex is anchored at the end too.
806                      * Unless it's a multiline match, the lengths must match
807                      * exactly, give or take a \n.  NB: slen >= 1 since
808                      * the last char of check is \n */
809                     if (!multiline
810                         && (   strend - s > slen
811                             || strend - s < slen - 1
812                             || (strend - s == slen && strend[-1] != '\n')))
813                     {
814                         DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
815                                             "  String too long...\n"));
816                         goto fail_finish;
817                     }
818                     /* Now should match s[0..slen-2] */
819                     slen--;
820                 }
821                 if (slen && (*SvPVX_const(check) != *s
822                     || (slen > 1 && memNE(SvPVX_const(check), s, slen))))
823                 {
824                     DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
825                                     "  String not equal...\n"));
826                     goto fail_finish;
827                 }
828
829                 check_at = s;
830                 goto success_at_start;
831             }
832         }
833     }
834
835     end_shift = prog->check_end_shift;
836
837 #ifdef DEBUGGING        /* 7/99: reports of failure (with the older version) */
838     if (end_shift < 0)
839         Perl_croak(aTHX_ "panic: end_shift: %"IVdf" pattern:\n%s\n ",
840                    (IV)end_shift, RX_PRECOMP(prog));
841 #endif
842
843   restart:
844     
845     /* This is the (re)entry point of the main loop in this function.
846      * The goal of this loop is to:
847      * 1) find the "check" substring in the region rx_origin..strend
848      *    (adjusted by start_shift / end_shift). If not found, reject
849      *    immediately.
850      * 2) If it exists, look for the "other" substr too if defined; for
851      *    example, if the check substr maps to the anchored substr, then
852      *    check the floating substr, and vice-versa. If not found, go
853      *    back to (1) with rx_origin suitably incremented.
854      * 3) If we find an rx_origin position that doesn't contradict
855      *    either of the substrings, then check the possible additional
856      *    constraints on rx_origin of /^.../m or a known start class.
857      *    If these fail, then depending on which constraints fail, jump
858      *    back to here, or to various other re-entry points further along
859      *    that skip some of the first steps.
860      * 4) If we pass all those tests, update the BmUSEFUL() count on the
861      *    substring. If the start position was determined to be at the
862      *    beginning of the string  - so, not rejected, but not optimised,
863      *    since we have to run regmatch from position 0 - decrement the
864      *    BmUSEFUL() count. Otherwise increment it.
865      */
866
867
868     /* first, look for the 'check' substring */
869
870     {
871         U8* start_point;
872         U8* end_point;
873
874         DEBUG_OPTIMISE_MORE_r({
875             PerlIO_printf(Perl_debug_log,
876                 "  At restart: rx_origin=%"IVdf" Check offset min: %"IVdf
877                 " Start shift: %"IVdf" End shift %"IVdf
878                 " Real end Shift: %"IVdf"\n",
879                 (IV)(rx_origin - strpos),
880                 (IV)prog->check_offset_min,
881                 (IV)start_shift,
882                 (IV)end_shift,
883                 (IV)prog->check_end_shift);
884         });
885         
886         if (prog->intflags & PREGf_CANY_SEEN) {
887             start_point= (U8*)(rx_origin + start_shift);
888             end_point= (U8*)(strend - end_shift);
889             if (start_point > end_point)
890                 goto fail_finish;
891         } else {
892             end_point = HOP3(strend, -end_shift, strbeg);
893             start_point = HOPMAYBE3(rx_origin, start_shift, end_point);
894             if (!start_point)
895                 goto fail_finish;
896         }
897
898
899         /* If the regex is absolutely anchored to either the start of the
900          * string (SBOL) or to pos() (ANCH_GPOS), then
901          * check_offset_max represents an upper bound on the string where
902          * the substr could start. For the ANCH_GPOS case, we assume that
903          * the caller of intuit will have already set strpos to
904          * pos()-gofs, so in this case strpos + offset_max will still be
905          * an upper bound on the substr.
906          */
907         if (!ml_anch
908             && prog->intflags & PREGf_ANCH
909             && prog->check_offset_max != SSize_t_MAX)
910         {
911             SSize_t len = SvCUR(check) - !!SvTAIL(check);
912             const char * const anchor =
913                         (prog->intflags & PREGf_ANCH_GPOS ? strpos : strbeg);
914
915             /* do a bytes rather than chars comparison. It's conservative;
916              * so it skips doing the HOP if the result can't possibly end
917              * up earlier than the old value of end_point.
918              */
919             if ((char*)end_point - anchor > prog->check_offset_max) {
920                 end_point = HOP3lim((U8*)anchor,
921                                 prog->check_offset_max,
922                                 end_point -len)
923                             + len;
924             }
925         }
926
927         DEBUG_OPTIMISE_MORE_r({
928             PerlIO_printf(Perl_debug_log, "  fbm_instr len=%d str=<%.*s>\n",
929                 (int)(end_point - start_point),
930                 (int)(end_point - start_point) > 20 ? 20 : (int)(end_point - start_point), 
931                 start_point);
932         });
933
934         check_at = fbm_instr( start_point, end_point,
935                       check, multiline ? FBMrf_MULTILINE : 0);
936
937         /* Update the count-of-usability, remove useless subpatterns,
938             unshift s.  */
939
940         DEBUG_EXECUTE_r({
941             RE_PV_QUOTED_DECL(quoted, utf8_target, PERL_DEBUG_PAD_ZERO(0),
942                 SvPVX_const(check), RE_SV_DUMPLEN(check), 30);
943             PerlIO_printf(Perl_debug_log, "  %s %s substr %s%s%s",
944                               (check_at ? "Found" : "Did not find"),
945                 (check == (utf8_target ? prog->anchored_utf8 : prog->anchored_substr)
946                     ? "anchored" : "floating"),
947                 quoted,
948                 RE_SV_TAIL(check),
949                 (check_at ? " at offset " : "...\n") );
950         });
951
952         if (!check_at)
953             goto fail_finish;
954         /* Finish the diagnostic message */
955         DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%ld...\n", (long)(check_at - strpos)) );
956
957         /* set rx_origin to the minimum position where the regex could start
958          * matching, given the constraint of the just-matched check substring.
959          * But don't set it lower than previously.
960          */
961
962         if (check_at - rx_origin > prog->check_offset_max)
963             rx_origin = HOP3c(check_at, -prog->check_offset_max, rx_origin);
964     }
965
966
967     /* now look for the 'other' substring if defined */
968
969     if (utf8_target ? prog->substrs->data[other_ix].utf8_substr
970                     : prog->substrs->data[other_ix].substr)
971     {
972         /* Take into account the "other" substring. */
973         char *last, *last1;
974         char *s;
975         SV* must;
976         struct reg_substr_datum *other;
977
978       do_other_substr:
979         other = &prog->substrs->data[other_ix];
980
981         /* if "other" is anchored:
982          * we've previously found a floating substr starting at check_at.
983          * This means that the regex origin must lie somewhere
984          * between min (rx_origin): HOP3(check_at, -check_offset_max)
985          * and max:                 HOP3(check_at, -check_offset_min)
986          * (except that min will be >= strpos)
987          * So the fixed  substr must lie somewhere between
988          *  HOP3(min, anchored_offset)
989          *  HOP3(max, anchored_offset) + SvCUR(substr)
990          */
991
992         /* if "other" is floating
993          * Calculate last1, the absolute latest point where the
994          * floating substr could start in the string, ignoring any
995          * constraints from the earlier fixed match. It is calculated
996          * as follows:
997          *
998          * strend - prog->minlen (in chars) is the absolute latest
999          * position within the string where the origin of the regex
1000          * could appear. The latest start point for the floating
1001          * substr is float_min_offset(*) on from the start of the
1002          * regex.  last1 simply combines thee two offsets.
1003          *
1004          * (*) You might think the latest start point should be
1005          * float_max_offset from the regex origin, and technically
1006          * you'd be correct. However, consider
1007          *    /a\d{2,4}bcd\w/
1008          * Here, float min, max are 3,5 and minlen is 7.
1009          * This can match either
1010          *    /a\d\dbcd\w/
1011          *    /a\d\d\dbcd\w/
1012          *    /a\d\d\d\dbcd\w/
1013          * In the first case, the regex matches minlen chars; in the
1014          * second, minlen+1, in the third, minlen+2.
1015          * In the first case, the floating offset is 3 (which equals
1016          * float_min), in the second, 4, and in the third, 5 (which
1017          * equals float_max). In all cases, the floating string bcd
1018          * can never start more than 4 chars from the end of the
1019          * string, which equals minlen - float_min. As the substring
1020          * starts to match more than float_min from the start of the
1021          * regex, it makes the regex match more than minlen chars,
1022          * and the two cancel each other out. So we can always use
1023          * float_min - minlen, rather than float_max - minlen for the
1024          * latest position in the string.
1025          *
1026          * Note that -minlen + float_min_offset is equivalent (AFAIKT)
1027          * to CHR_SVLEN(must) - !!SvTAIL(must) + prog->float_end_shift
1028          */
1029
1030         assert(prog->minlen >= other->min_offset);
1031         last1 = HOP3c(strend,
1032                         other->min_offset - prog->minlen, strbeg);
1033
1034         if (other_ix) {/* i.e. if (other-is-float) */
1035             /* last is the latest point where the floating substr could
1036              * start, *given* any constraints from the earlier fixed
1037              * match. This constraint is that the floating string starts
1038              * <= float_max_offset chars from the regex origin (rx_origin).
1039              * If this value is less than last1, use it instead.
1040              */
1041             assert(rx_origin <= last1);
1042             last =
1043                 /* this condition handles the offset==infinity case, and
1044                  * is a short-cut otherwise. Although it's comparing a
1045                  * byte offset to a char length, it does so in a safe way,
1046                  * since 1 char always occupies 1 or more bytes,
1047                  * so if a string range is  (last1 - rx_origin) bytes,
1048                  * it will be less than or equal to  (last1 - rx_origin)
1049                  * chars; meaning it errs towards doing the accurate HOP3
1050                  * rather than just using last1 as a short-cut */
1051                 (last1 - rx_origin) < other->max_offset
1052                     ? last1
1053                     : (char*)HOP3lim(rx_origin, other->max_offset, last1);
1054         }
1055         else {
1056             assert(strpos + start_shift <= check_at);
1057             last = HOP4c(check_at, other->min_offset - start_shift,
1058                         strbeg, strend);
1059         }
1060
1061         s = HOP3c(rx_origin, other->min_offset, strend);
1062         if (s < other_last)     /* These positions already checked */
1063             s = other_last;
1064
1065         must = utf8_target ? other->utf8_substr : other->substr;
1066         assert(SvPOK(must));
1067         s = fbm_instr(
1068             (unsigned char*)s,
1069             (unsigned char*)last + SvCUR(must) - (SvTAIL(must)!=0),
1070             must,
1071             multiline ? FBMrf_MULTILINE : 0
1072         );
1073         DEBUG_EXECUTE_r({
1074             RE_PV_QUOTED_DECL(quoted, utf8_target, PERL_DEBUG_PAD_ZERO(0),
1075                 SvPVX_const(must), RE_SV_DUMPLEN(must), 30);
1076             PerlIO_printf(Perl_debug_log, "  %s %s substr %s%s",
1077                 s ? "Found" : "Contradicts",
1078                 other_ix ? "floating" : "anchored",
1079                 quoted, RE_SV_TAIL(must));
1080         });
1081
1082
1083         if (!s) {
1084             /* last1 is latest possible substr location. If we didn't
1085              * find it before there, we never will */
1086             if (last >= last1) {
1087                 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1088                                         ", giving up...\n"));
1089                 goto fail_finish;
1090             }
1091
1092             /* try to find the check substr again at a later
1093              * position. Maybe next time we'll find the "other" substr
1094              * in range too */
1095             DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1096                 ", trying %s at offset %ld...\n",
1097                 (other_ix ? "floating" : "anchored"),
1098                 (long)(HOP3c(check_at, 1, strend) - strpos)));
1099
1100             other_last = HOP3c(last, 1, strend) /* highest failure */;
1101             rx_origin =
1102                 other_ix /* i.e. if other-is-float */
1103                     ? HOP3c(rx_origin, 1, strend)
1104                     : HOP4c(last, 1 - other->min_offset, strbeg, strend);
1105             goto restart;
1106         }
1107         else {
1108             DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
1109                   (long)(s - strpos)));
1110
1111             if (other_ix) { /* if (other-is-float) */
1112                 /* other_last is set to s, not s+1, since its possible for
1113                  * a floating substr to fail first time, then succeed
1114                  * second time at the same floating position; e.g.:
1115                  *     "-AB--AABZ" =~ /\wAB\d*Z/
1116                  * The first time round, anchored and float match at
1117                  * "-(AB)--AAB(Z)" then fail on the initial \w character
1118                  * class. Second time round, they match at "-AB--A(AB)(Z)".
1119                  */
1120                 other_last = s;
1121             }
1122             else {
1123                 rx_origin = HOP3c(s, -other->min_offset, strbeg);
1124                 other_last = HOP3c(s, 1, strend);
1125             }
1126         }
1127     }
1128     else {
1129         DEBUG_OPTIMISE_MORE_r(
1130             PerlIO_printf(Perl_debug_log,
1131                 "  Check-only match: offset min:%"IVdf" max:%"IVdf
1132                 " check_at:%"IVdf" rx_origin:%"IVdf" rx_origin-check_at:%"IVdf
1133                 " strend-strpos:%"IVdf"\n",
1134                 (IV)prog->check_offset_min,
1135                 (IV)prog->check_offset_max,
1136                 (IV)(check_at-strpos),
1137                 (IV)(rx_origin-strpos),
1138                 (IV)(rx_origin-check_at),
1139                 (IV)(strend-strpos)
1140             )
1141         );
1142     }
1143
1144   postprocess_substr_matches:
1145
1146     /* handle the extra constraint of /^.../m if present */
1147
1148     if (ml_anch && rx_origin != strbeg && rx_origin[-1] != '\n') {
1149         char *s;
1150
1151         DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1152                         "  looking for /^/m anchor"));
1153
1154         /* we have failed the constraint of a \n before rx_origin.
1155          * Find the next \n, if any, even if it's beyond the current
1156          * anchored and/or floating substrings. Whether we should be
1157          * scanning ahead for the next \n or the next substr is debatable.
1158          * On the one hand you'd expect rare substrings to appear less
1159          * often than \n's. On the other hand, searching for \n means
1160          * we're effectively flipping been check_substr and "\n" on each
1161          * iteration as the current "rarest" string candidate, which
1162          * means for example that we'll quickly reject the whole string if
1163          * hasn't got a \n, rather than trying every substr position
1164          * first
1165          */
1166
1167         s = HOP3c(strend, - prog->minlen, strpos);
1168         if (s <= rx_origin ||
1169             ! ( rx_origin = (char *)memchr(rx_origin, '\n', s - rx_origin)))
1170         {
1171             DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1172                             "  Did not find /%s^%s/m...\n",
1173                             PL_colors[0], PL_colors[1]));
1174             goto fail_finish;
1175         }
1176
1177         /* earliest possible origin is 1 char after the \n.
1178          * (since *rx_origin == '\n', it's safe to ++ here rather than
1179          * HOP(rx_origin, 1)) */
1180         rx_origin++;
1181
1182         if (prog->substrs->check_ix == 0  /* check is anchored */
1183             || rx_origin >= HOP3c(check_at,  - prog->check_offset_min, strpos))
1184         {
1185             /* Position contradicts check-string; either because
1186              * check was anchored (and thus has no wiggle room),
1187              * or check was float and rx_origin is above the float range */
1188             DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1189                 "  Found /%s^%s/m, restarting lookup for check-string at offset %ld...\n",
1190                 PL_colors[0], PL_colors[1], (long)(rx_origin - strpos)));
1191             goto restart;
1192         }
1193
1194         /* if we get here, the check substr must have been float,
1195          * is in range, and we may or may not have had an anchored
1196          * "other" substr which still contradicts */
1197         assert(prog->substrs->check_ix); /* check is float */
1198
1199         if (utf8_target ? prog->anchored_utf8 : prog->anchored_substr) {
1200             /* whoops, the anchored "other" substr exists, so we still
1201              * contradict. On the other hand, the float "check" substr
1202              * didn't contradict, so just retry the anchored "other"
1203              * substr */
1204             DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1205                 "  Found /%s^%s/m at offset %ld, rescanning for anchored from offset %ld...\n",
1206                 PL_colors[0], PL_colors[1],
1207                 (long)(rx_origin - strpos),
1208                 (long)(rx_origin - strpos + prog->anchored_offset)));
1209             goto do_other_substr;
1210         }
1211
1212         /* success: we don't contradict the found floating substring
1213          * (and there's no anchored substr). */
1214         DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1215             "  Found /%s^%s/m at offset %ld...\n",
1216             PL_colors[0], PL_colors[1], (long)(rx_origin - strpos)));
1217     }
1218     else {
1219         DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1220             "  (multiline anchor test skipped)\n"));
1221     }
1222
1223   success_at_start:
1224
1225
1226     /* if we have a starting character class, then test that extra constraint.
1227      * (trie stclasses are too expensive to use here, we are better off to
1228      * leave it to regmatch itself) */
1229
1230     if (progi->regstclass && PL_regkind[OP(progi->regstclass)]!=TRIE) {
1231         const U8* const str = (U8*)STRING(progi->regstclass);
1232
1233         /* XXX this value could be pre-computed */
1234         const int cl_l = (PL_regkind[OP(progi->regstclass)] == EXACT
1235                     ?  (reginfo->is_utf8_pat
1236                         ? utf8_distance(str + STR_LEN(progi->regstclass), str)
1237                         : STR_LEN(progi->regstclass))
1238                     : 1);
1239         char * endpos;
1240         char *s;
1241         /* latest pos that a matching float substr constrains rx start to */
1242         char *rx_max_float = NULL;
1243
1244         /* if the current rx_origin is anchored, either by satisfying an
1245          * anchored substring constraint, or a /^.../m constraint, then we
1246          * can reject the current origin if the start class isn't found
1247          * at the current position. If we have a float-only match, then
1248          * rx_origin is constrained to a range; so look for the start class
1249          * in that range. if neither, then look for the start class in the
1250          * whole rest of the string */
1251
1252         /* XXX DAPM it's not clear what the minlen test is for, and why
1253          * it's not used in the floating case. Nothing in the test suite
1254          * causes minlen == 0 here. See <20140313134639.GS12844@iabyn.com>.
1255          * Here are some old comments, which may or may not be correct:
1256          *
1257          *   minlen == 0 is possible if regstclass is \b or \B,
1258          *   and the fixed substr is ''$.
1259          *   Since minlen is already taken into account, rx_origin+1 is
1260          *   before strend; accidentally, minlen >= 1 guaranties no false
1261          *   positives at rx_origin + 1 even for \b or \B.  But (minlen? 1 :
1262          *   0) below assumes that regstclass does not come from lookahead...
1263          *   If regstclass takes bytelength more than 1: If charlength==1, OK.
1264          *   This leaves EXACTF-ish only, which are dealt with in
1265          *   find_byclass().
1266          */
1267
1268         if (prog->anchored_substr || prog->anchored_utf8 || ml_anch)
1269             endpos= HOP3c(rx_origin, (prog->minlen ? cl_l : 0), strend);
1270         else if (prog->float_substr || prog->float_utf8) {
1271             rx_max_float = HOP3c(check_at, -start_shift, strbeg);
1272             endpos= HOP3c(rx_max_float, cl_l, strend);
1273         }
1274         else 
1275             endpos= strend;
1276                     
1277         DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1278             "  looking for class: start_shift: %"IVdf" check_at: %"IVdf
1279             " rx_origin: %"IVdf" endpos: %"IVdf"\n",
1280               (IV)start_shift, (IV)(check_at - strbeg),
1281               (IV)(rx_origin - strbeg), (IV)(endpos - strbeg)));
1282
1283         s = find_byclass(prog, progi->regstclass, rx_origin, endpos,
1284                             reginfo);
1285         if (!s) {
1286             if (endpos == strend) {
1287                 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
1288                                 "  Could not match STCLASS...\n") );
1289                 goto fail;
1290             }
1291             DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
1292                                "  This position contradicts STCLASS...\n") );
1293             if ((prog->intflags & PREGf_ANCH) && !ml_anch
1294                         && !(prog->intflags & PREGf_IMPLICIT))
1295                 goto fail;
1296
1297             /* Contradict one of substrings */
1298             if (prog->anchored_substr || prog->anchored_utf8) {
1299                 if (prog->substrs->check_ix == 1) { /* check is float */
1300                     /* Have both, check_string is floating */
1301                     assert(rx_origin + start_shift <= check_at);
1302                     if (rx_origin + start_shift != check_at) {
1303                         /* not at latest position float substr could match:
1304                          * Recheck anchored substring, but not floating.
1305                          * The condition above is in bytes rather than
1306                          * chars for efficiency. It's conservative, in
1307                          * that it errs on the side of doing 'goto
1308                          * do_other_substr', where a more accurate
1309                          * char-based calculation will be done */
1310                         DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
1311                                   "  Looking for anchored substr starting at offset %ld...\n",
1312                                   (long)(other_last - strpos)) );
1313                         goto do_other_substr;
1314                     }
1315                 }
1316             }
1317             else {
1318                 /* float-only */
1319
1320                 if (ml_anch) {
1321                     /* In the presence of ml_anch, we might be able to
1322                      * find another \n without breaking the current float
1323                      * constraint. */
1324
1325                     /* strictly speaking this should be HOP3c(..., 1, ...),
1326                      * but since we goto a block of code that's going to
1327                      * search for the next \n if any, its safe here */
1328                     rx_origin++;
1329                     DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
1330                               "  Looking for /%s^%s/m starting at offset %ld...\n",
1331                               PL_colors[0], PL_colors[1],
1332                               (long)(rx_origin - strpos)) );
1333                     goto postprocess_substr_matches;
1334                 }
1335
1336                 /* strictly speaking this can never be true; but might
1337                  * be if we ever allow intuit without substrings */
1338                 if (!(utf8_target ? prog->float_utf8 : prog->float_substr))
1339                     goto fail;
1340
1341                 rx_origin = rx_max_float;
1342             }
1343
1344             /* at this point, any matching substrings have been
1345              * contradicted. Start again... */
1346
1347             rx_origin = HOP3c(rx_origin, 1, strend);
1348
1349             /* uses bytes rather than char calculations for efficiency.
1350              * It's conservative: it errs on the side of doing 'goto restart',
1351              * where there is code that does a proper char-based test */
1352             if (rx_origin + start_shift + end_shift > strend) {
1353                 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
1354                                        "  Could not match STCLASS...\n") );
1355                 goto fail;
1356             }
1357             DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
1358                 "  Looking for %s substr starting at offset %ld...\n",
1359                 (prog->substrs->check_ix ? "floating" : "anchored"),
1360                 (long)(rx_origin + start_shift - strpos)) );
1361             goto restart;
1362         }
1363
1364         /* Success !!! */
1365
1366         if (rx_origin != s) {
1367             DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1368                         "  By STCLASS: moving %ld --> %ld\n",
1369                                   (long)(rx_origin - strpos), (long)(s - strpos))
1370                    );
1371         }
1372         else {
1373             DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1374                                   "  Does not contradict STCLASS...\n");
1375                    );
1376         }
1377     }
1378
1379     /* Decide whether using the substrings helped */
1380
1381     if (rx_origin != strpos) {
1382         /* Fixed substring is found far enough so that the match
1383            cannot start at strpos. */
1384
1385         DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "  try at offset...\n"));
1386         ++BmUSEFUL(utf8_target ? prog->check_utf8 : prog->check_substr);        /* hooray/5 */
1387     }
1388     else {
1389         /* The found rx_origin position does not prohibit matching at
1390          * strpos, so calling intuit didn't gain us anything. Decrement
1391          * the BmUSEFUL() count on the check substring, and if we reach
1392          * zero, free it.  */
1393         if (!(prog->intflags & PREGf_NAUGHTY)
1394             && (utf8_target ? (
1395                 prog->check_utf8                /* Could be deleted already */
1396                 && --BmUSEFUL(prog->check_utf8) < 0
1397                 && (prog->check_utf8 == prog->float_utf8)
1398             ) : (
1399                 prog->check_substr              /* Could be deleted already */
1400                 && --BmUSEFUL(prog->check_substr) < 0
1401                 && (prog->check_substr == prog->float_substr)
1402             )))
1403         {
1404             /* If flags & SOMETHING - do not do it many times on the same match */
1405             DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "  ... Disabling check substring...\n"));
1406             /* XXX Does the destruction order has to change with utf8_target? */
1407             SvREFCNT_dec(utf8_target ? prog->check_utf8 : prog->check_substr);
1408             SvREFCNT_dec(utf8_target ? prog->check_substr : prog->check_utf8);
1409             prog->check_substr = prog->check_utf8 = NULL;       /* disable */
1410             prog->float_substr = prog->float_utf8 = NULL;       /* clear */
1411             check = NULL;                       /* abort */
1412             /* XXXX This is a remnant of the old implementation.  It
1413                     looks wasteful, since now INTUIT can use many
1414                     other heuristics. */
1415             prog->extflags &= ~RXf_USE_INTUIT;
1416         }
1417     }
1418
1419     DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1420             "Intuit: %sSuccessfully guessed:%s match at offset %ld\n",
1421              PL_colors[4], PL_colors[5], (long)(rx_origin - strpos)) );
1422
1423     return rx_origin;
1424
1425   fail_finish:                          /* Substring not found */
1426     if (prog->check_substr || prog->check_utf8)         /* could be removed already */
1427         BmUSEFUL(utf8_target ? prog->check_utf8 : prog->check_substr) += 5; /* hooray */
1428   fail:
1429     DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch rejected by optimizer%s\n",
1430                           PL_colors[4], PL_colors[5]));
1431     return NULL;
1432 }
1433
1434
1435 #define DECL_TRIE_TYPE(scan) \
1436     const enum { trie_plain, trie_utf8, trie_utf8_fold, trie_latin_utf8_fold,       \
1437                  trie_utf8_exactfa_fold, trie_latin_utf8_exactfa_fold,              \
1438                  trie_utf8l, trie_flu8 }                                            \
1439                     trie_type = ((scan->flags == EXACT)                             \
1440                                  ? (utf8_target ? trie_utf8 : trie_plain)           \
1441                                  : (scan->flags == EXACTL)                          \
1442                                     ? (utf8_target ? trie_utf8l : trie_plain)       \
1443                                     : (scan->flags == EXACTFA)                      \
1444                                       ? (utf8_target                                \
1445                                          ? trie_utf8_exactfa_fold                   \
1446                                          : trie_latin_utf8_exactfa_fold)            \
1447                                       : (scan->flags == EXACTFLU8                   \
1448                                          ? trie_flu8                                \
1449                                          : (utf8_target                             \
1450                                            ? trie_utf8_fold                         \
1451                                            :   trie_latin_utf8_fold)))
1452
1453 #define REXEC_TRIE_READ_CHAR(trie_type, trie, widecharmap, uc, uscan, len, uvc, charid, foldlen, foldbuf, uniflags) \
1454 STMT_START {                                                                        \
1455     STRLEN skiplen;                                                                 \
1456     U8 flags = FOLD_FLAGS_FULL;                                                     \
1457     switch (trie_type) {                                                            \
1458     case trie_flu8:                                                                 \
1459         goto do_trie_utf8_fold;                                                     \
1460     case trie_utf8_exactfa_fold:                                                    \
1461         flags |= FOLD_FLAGS_NOMIX_ASCII;                                            \
1462         /* FALLTHROUGH */                                                          \
1463     case trie_utf8_fold:                                                            \
1464       do_trie_utf8_fold:                                                            \
1465         if ( foldlen>0 ) {                                                          \
1466             uvc = utf8n_to_uvchr( (const U8*) uscan, UTF8_MAXLEN, &len, uniflags ); \
1467             foldlen -= len;                                                         \
1468             uscan += len;                                                           \
1469             len=0;                                                                  \
1470         } else {                                                                    \
1471             uvc = _to_utf8_fold_flags( (const U8*) uc, foldbuf, &foldlen, flags);   \
1472             len = UTF8SKIP(uc);                                                     \
1473             skiplen = UNISKIP( uvc );                                               \
1474             foldlen -= skiplen;                                                     \
1475             uscan = foldbuf + skiplen;                                              \
1476         }                                                                           \
1477         break;                                                                      \
1478     case trie_latin_utf8_exactfa_fold:                                              \
1479         flags |= FOLD_FLAGS_NOMIX_ASCII;                                            \
1480         /* FALLTHROUGH */                                                          \
1481     case trie_latin_utf8_fold:                                                      \
1482         if ( foldlen>0 ) {                                                          \
1483             uvc = utf8n_to_uvchr( (const U8*) uscan, UTF8_MAXLEN, &len, uniflags ); \
1484             foldlen -= len;                                                         \
1485             uscan += len;                                                           \
1486             len=0;                                                                  \
1487         } else {                                                                    \
1488             len = 1;                                                                \
1489             uvc = _to_fold_latin1( (U8) *uc, foldbuf, &foldlen, flags);             \
1490             skiplen = UNISKIP( uvc );                                               \
1491             foldlen -= skiplen;                                                     \
1492             uscan = foldbuf + skiplen;                                              \
1493         }                                                                           \
1494         break;                                                                      \
1495     case trie_utf8l:                                                                \
1496     case trie_utf8:                                                                 \
1497         uvc = utf8n_to_uvchr( (const U8*) uc, UTF8_MAXLEN, &len, uniflags );        \
1498         break;                                                                      \
1499     case trie_plain:                                                                \
1500         uvc = (UV)*uc;                                                              \
1501         len = 1;                                                                    \
1502     }                                                                               \
1503     if (uvc < 256) {                                                                \
1504         charid = trie->charmap[ uvc ];                                              \
1505     }                                                                               \
1506     else {                                                                          \
1507         charid = 0;                                                                 \
1508         if (widecharmap) {                                                          \
1509             SV** const svpp = hv_fetch(widecharmap,                                 \
1510                         (char*)&uvc, sizeof(UV), 0);                                \
1511             if (svpp)                                                               \
1512                 charid = (U16)SvIV(*svpp);                                          \
1513         }                                                                           \
1514     }                                                                               \
1515 } STMT_END
1516
1517 #define DUMP_EXEC_POS(li,s,doutf8)                          \
1518     dump_exec_pos(li,s,(reginfo->strend),(reginfo->strbeg), \
1519                 startpos, doutf8)
1520
1521 #define REXEC_FBC_EXACTISH_SCAN(COND)                     \
1522 STMT_START {                                              \
1523     while (s <= e) {                                      \
1524         if ( (COND)                                       \
1525              && (ln == 1 || folder(s, pat_string, ln))    \
1526              && (reginfo->intuit || regtry(reginfo, &s)) )\
1527             goto got_it;                                  \
1528         s++;                                              \
1529     }                                                     \
1530 } STMT_END
1531
1532 #define REXEC_FBC_UTF8_SCAN(CODE)                     \
1533 STMT_START {                                          \
1534     while (s < strend) {                              \
1535         CODE                                          \
1536         s += UTF8SKIP(s);                             \
1537     }                                                 \
1538 } STMT_END
1539
1540 #define REXEC_FBC_SCAN(CODE)                          \
1541 STMT_START {                                          \
1542     while (s < strend) {                              \
1543         CODE                                          \
1544         s++;                                          \
1545     }                                                 \
1546 } STMT_END
1547
1548 #define REXEC_FBC_UTF8_CLASS_SCAN(COND)                        \
1549 REXEC_FBC_UTF8_SCAN( /* Loops while (s < strend) */            \
1550     if (COND) {                                                \
1551         if (tmp && (reginfo->intuit || regtry(reginfo, &s)))   \
1552             goto got_it;                                       \
1553         else                                                   \
1554             tmp = doevery;                                     \
1555     }                                                          \
1556     else                                                       \
1557         tmp = 1;                                               \
1558 )
1559
1560 #define REXEC_FBC_CLASS_SCAN(COND)                             \
1561 REXEC_FBC_SCAN( /* Loops while (s < strend) */                 \
1562     if (COND) {                                                \
1563         if (tmp && (reginfo->intuit || regtry(reginfo, &s)))   \
1564             goto got_it;                                       \
1565         else                                                   \
1566             tmp = doevery;                                     \
1567     }                                                          \
1568     else                                                       \
1569         tmp = 1;                                               \
1570 )
1571
1572 #define REXEC_FBC_CSCAN(CONDUTF8,COND)                         \
1573     if (utf8_target) {                                         \
1574         REXEC_FBC_UTF8_CLASS_SCAN(CONDUTF8);                   \
1575     }                                                          \
1576     else {                                                     \
1577         REXEC_FBC_CLASS_SCAN(COND);                            \
1578     }
1579
1580 /* The three macros below are slightly different versions of the same logic.
1581  *
1582  * The first is for /a and /aa when the target string is UTF-8.  This can only
1583  * match ascii, but it must advance based on UTF-8.   The other two handle the
1584  * non-UTF-8 and the more generic UTF-8 cases.   In all three, we are looking
1585  * for the boundary (or non-boundary) between a word and non-word character.
1586  * The utf8 and non-utf8 cases have the same logic, but the details must be
1587  * different.  Find the "wordness" of the character just prior to this one, and
1588  * compare it with the wordness of this one.  If they differ, we have a
1589  * boundary.  At the beginning of the string, pretend that the previous
1590  * character was a new-line.
1591  *
1592  * All these macros uncleanly have side-effects with each other and outside
1593  * variables.  So far it's been too much trouble to clean-up
1594  *
1595  * TEST_NON_UTF8 is the macro or function to call to test if its byte input is
1596  *               a word character or not.
1597  * IF_SUCCESS    is code to do if it finds that we are at a boundary between
1598  *               word/non-word
1599  * IF_FAIL       is code to do if we aren't at a boundary between word/non-word
1600  *
1601  * Exactly one of the two IF_FOO parameters is a no-op, depending on whether we
1602  * are looking for a boundary or for a non-boundary.  If we are looking for a
1603  * boundary, we want IF_FAIL to be the no-op, and for IF_SUCCESS to go out and
1604  * see if this tentative match actually works, and if so, to quit the loop
1605  * here.  And vice-versa if we are looking for a non-boundary.
1606  *
1607  * 'tmp' below in the next three macros in the REXEC_FBC_SCAN and
1608  * REXEC_FBC_UTF8_SCAN loops is a loop invariant, a bool giving the return of
1609  * TEST_NON_UTF8(s-1).  To see this, note that that's what it is defined to be
1610  * at entry to the loop, and to get to the IF_FAIL branch, tmp must equal
1611  * TEST_NON_UTF8(s), and in the opposite branch, IF_SUCCESS, tmp is that
1612  * complement.  But in that branch we complement tmp, meaning that at the
1613  * bottom of the loop tmp is always going to be equal to TEST_NON_UTF8(s),
1614  * which means at the top of the loop in the next iteration, it is
1615  * TEST_NON_UTF8(s-1) */
1616 #define FBC_UTF8_A(TEST_NON_UTF8, IF_SUCCESS, IF_FAIL)                         \
1617     tmp = (s != reginfo->strbeg) ? UCHARAT(s - 1) : '\n';                      \
1618     tmp = TEST_NON_UTF8(tmp);                                                  \
1619     REXEC_FBC_UTF8_SCAN( /* advances s while s < strend */                     \
1620         if (tmp == ! TEST_NON_UTF8((U8) *s)) {                                 \
1621             tmp = !tmp;                                                        \
1622             IF_SUCCESS; /* Is a boundary if values for s-1 and s differ */     \
1623         }                                                                      \
1624         else {                                                                 \
1625             IF_FAIL;                                                           \
1626         }                                                                      \
1627     );                                                                         \
1628
1629 /* Like FBC_UTF8_A, but TEST_UV is a macro which takes a UV as its input, and
1630  * TEST_UTF8 is a macro that for the same input code points returns identically
1631  * to TEST_UV, but takes a pointer to a UTF-8 encoded string instead */
1632 #define FBC_UTF8(TEST_UV, TEST_UTF8, IF_SUCCESS, IF_FAIL)                      \
1633     if (s == reginfo->strbeg) {                                                \
1634         tmp = '\n';                                                            \
1635     }                                                                          \
1636     else { /* Back-up to the start of the previous character */                \
1637         U8 * const r = reghop3((U8*)s, -1, (U8*)reginfo->strbeg);              \
1638         tmp = utf8n_to_uvchr(r, (U8*) reginfo->strend - r,                     \
1639                                                        0, UTF8_ALLOW_DEFAULT); \
1640     }                                                                          \
1641     tmp = TEST_UV(tmp);                                                        \
1642     LOAD_UTF8_CHARCLASS_ALNUM();                                               \
1643     REXEC_FBC_UTF8_SCAN( /* advances s while s < strend */                     \
1644         if (tmp == ! (TEST_UTF8((U8 *) s))) {                                  \
1645             tmp = !tmp;                                                        \
1646             IF_SUCCESS;                                                        \
1647         }                                                                      \
1648         else {                                                                 \
1649             IF_FAIL;                                                           \
1650         }                                                                      \
1651     );
1652
1653 /* Like the above two macros.  UTF8_CODE is the complete code for handling
1654  * UTF-8.  Common to the BOUND and NBOUND cases, set-up by the FBC_BOUND, etc
1655  * macros below */
1656 #define FBC_BOUND_COMMON(UTF8_CODE, TEST_NON_UTF8, IF_SUCCESS, IF_FAIL)        \
1657     if (utf8_target) {                                                         \
1658         UTF8_CODE                                                              \
1659     }                                                                          \
1660     else {  /* Not utf8 */                                                     \
1661         tmp = (s != reginfo->strbeg) ? UCHARAT(s - 1) : '\n';                  \
1662         tmp = TEST_NON_UTF8(tmp);                                              \
1663         REXEC_FBC_SCAN( /* advances s while s < strend */                      \
1664             if (tmp == ! TEST_NON_UTF8((U8) *s)) {                             \
1665                 IF_SUCCESS;                                                    \
1666                 tmp = !tmp;                                                    \
1667             }                                                                  \
1668             else {                                                             \
1669                 IF_FAIL;                                                       \
1670             }                                                                  \
1671         );                                                                     \
1672     }                                                                          \
1673     /* Here, things have been set up by the previous code so that tmp is the   \
1674      * return of TEST_NON_UTF(s-1) or TEST_UTF8(s-1) (depending on the         \
1675      * utf8ness of the target).  We also have to check if this matches against \
1676      * the EOS, which we treat as a \n (which is the same value in both UTF-8  \
1677      * or non-UTF8, so can use the non-utf8 test condition even for a UTF-8    \
1678      * string */                                                               \
1679     if (tmp == ! TEST_NON_UTF8('\n')) {                                        \
1680         IF_SUCCESS;                                                            \
1681     }                                                                          \
1682     else {                                                                     \
1683         IF_FAIL;                                                               \
1684     }
1685
1686 /* This is the macro to use when we want to see if something that looks like it
1687  * could match, actually does, and if so exits the loop */
1688 #define REXEC_FBC_TRYIT                            \
1689     if ((reginfo->intuit || regtry(reginfo, &s)))  \
1690         goto got_it
1691
1692 /* The only difference between the BOUND and NBOUND cases is that
1693  * REXEC_FBC_TRYIT is called when matched in BOUND, and when non-matched in
1694  * NBOUND.  This is accomplished by passing it as either the if or else clause,
1695  * with the other one being empty (PLACEHOLDER is defined as empty).
1696  *
1697  * The TEST_FOO parameters are for operating on different forms of input, but
1698  * all should be ones that return identically for the same underlying code
1699  * points */
1700 #define FBC_BOUND(TEST_NON_UTF8, TEST_UV, TEST_UTF8)                           \
1701     FBC_BOUND_COMMON(                                                          \
1702           FBC_UTF8(TEST_UV, TEST_UTF8, REXEC_FBC_TRYIT, PLACEHOLDER),          \
1703           TEST_NON_UTF8, REXEC_FBC_TRYIT, PLACEHOLDER)
1704
1705 #define FBC_BOUND_A(TEST_NON_UTF8, TEST_UV, TEST_UTF8)                         \
1706     FBC_BOUND_COMMON(                                                          \
1707             FBC_UTF8_A(TEST_NON_UTF8, REXEC_FBC_TRYIT, PLACEHOLDER),           \
1708             TEST_NON_UTF8, REXEC_FBC_TRYIT, PLACEHOLDER)
1709
1710 #define FBC_NBOUND(TEST_NON_UTF8, TEST_UV, TEST_UTF8)                          \
1711     FBC_BOUND_COMMON(                                                          \
1712           FBC_UTF8(TEST_UV, TEST_UTF8, PLACEHOLDER, REXEC_FBC_TRYIT),          \
1713           TEST_NON_UTF8, PLACEHOLDER, REXEC_FBC_TRYIT)
1714
1715 #define FBC_NBOUND_A(TEST_NON_UTF8, TEST_UV, TEST_UTF8)                        \
1716     FBC_BOUND_COMMON(                                                          \
1717             FBC_UTF8_A(TEST_NON_UTF8, PLACEHOLDER, REXEC_FBC_TRYIT),           \
1718             TEST_NON_UTF8, PLACEHOLDER, REXEC_FBC_TRYIT)
1719
1720
1721 /* We know what class REx starts with.  Try to find this position... */
1722 /* if reginfo->intuit, its a dryrun */
1723 /* annoyingly all the vars in this routine have different names from their counterparts
1724    in regmatch. /grrr */
1725 STATIC char *
1726 S_find_byclass(pTHX_ regexp * prog, const regnode *c, char *s, 
1727     const char *strend, regmatch_info *reginfo)
1728 {
1729     dVAR;
1730     const I32 doevery = (prog->intflags & PREGf_SKIP) == 0;
1731     char *pat_string;   /* The pattern's exactish string */
1732     char *pat_end;          /* ptr to end char of pat_string */
1733     re_fold_t folder;   /* Function for computing non-utf8 folds */
1734     const U8 *fold_array;   /* array for folding ords < 256 */
1735     STRLEN ln;
1736     STRLEN lnc;
1737     U8 c1;
1738     U8 c2;
1739     char *e;
1740     I32 tmp = 1;        /* Scratch variable? */
1741     const bool utf8_target = reginfo->is_utf8_target;
1742     UV utf8_fold_flags = 0;
1743     const bool is_utf8_pat = reginfo->is_utf8_pat;
1744     bool to_complement = FALSE; /* Invert the result?  Taking the xor of this
1745                                    with a result inverts that result, as 0^1 =
1746                                    1 and 1^1 = 0 */
1747     _char_class_number classnum;
1748
1749     RXi_GET_DECL(prog,progi);
1750
1751     PERL_ARGS_ASSERT_FIND_BYCLASS;
1752
1753     /* We know what class it must start with. */
1754     switch (OP(c)) {
1755     case ANYOFL:
1756     case ANYOF:
1757         if (utf8_target) {
1758             REXEC_FBC_UTF8_CLASS_SCAN(
1759                       reginclass(prog, c, (U8*)s, (U8*) strend, utf8_target));
1760         }
1761         else {
1762             REXEC_FBC_CLASS_SCAN(REGINCLASS(prog, c, (U8*)s));
1763         }
1764         break;
1765     case CANY:
1766         REXEC_FBC_SCAN(
1767             if (tmp && (reginfo->intuit || regtry(reginfo, &s)))
1768                 goto got_it;
1769             else
1770                 tmp = doevery;
1771         );
1772         break;
1773
1774     case EXACTFA_NO_TRIE:   /* This node only generated for non-utf8 patterns */
1775         assert(! is_utf8_pat);
1776         /* FALLTHROUGH */
1777     case EXACTFA:
1778         if (is_utf8_pat || utf8_target) {
1779             utf8_fold_flags = FOLDEQ_UTF8_NOMIX_ASCII;
1780             goto do_exactf_utf8;
1781         }
1782         fold_array = PL_fold_latin1;    /* Latin1 folds are not affected by */
1783         folder = foldEQ_latin1;         /* /a, except the sharp s one which */
1784         goto do_exactf_non_utf8;        /* isn't dealt with by these */
1785
1786     case EXACTF:   /* This node only generated for non-utf8 patterns */
1787         assert(! is_utf8_pat);
1788         if (utf8_target) {
1789             utf8_fold_flags = 0;
1790             goto do_exactf_utf8;
1791         }
1792         fold_array = PL_fold;
1793         folder = foldEQ;
1794         goto do_exactf_non_utf8;
1795
1796     case EXACTFL:
1797         if (is_utf8_pat || utf8_target || IN_UTF8_CTYPE_LOCALE) {
1798             utf8_fold_flags = FOLDEQ_LOCALE;
1799             goto do_exactf_utf8;
1800         }
1801         fold_array = PL_fold_locale;
1802         folder = foldEQ_locale;
1803         goto do_exactf_non_utf8;
1804
1805     case EXACTFU_SS:
1806         if (is_utf8_pat) {
1807             utf8_fold_flags = FOLDEQ_S2_ALREADY_FOLDED;
1808         }
1809         goto do_exactf_utf8;
1810
1811     case EXACTFLU8:
1812             if (! utf8_target) {    /* All code points in this node require
1813                                        UTF-8 to express.  */
1814                 break;
1815             }
1816             utf8_fold_flags = FOLDEQ_S2_ALREADY_FOLDED;
1817             goto do_exactf_utf8;
1818
1819     case EXACTFU:
1820         if (is_utf8_pat || utf8_target) {
1821             utf8_fold_flags = is_utf8_pat ? FOLDEQ_S2_ALREADY_FOLDED : 0;
1822             goto do_exactf_utf8;
1823         }
1824
1825         /* Any 'ss' in the pattern should have been replaced by regcomp,
1826          * so we don't have to worry here about this single special case
1827          * in the Latin1 range */
1828         fold_array = PL_fold_latin1;
1829         folder = foldEQ_latin1;
1830
1831         /* FALLTHROUGH */
1832
1833     do_exactf_non_utf8: /* Neither pattern nor string are UTF8, and there
1834                            are no glitches with fold-length differences
1835                            between the target string and pattern */
1836
1837         /* The idea in the non-utf8 EXACTF* cases is to first find the
1838          * first character of the EXACTF* node and then, if necessary,
1839          * case-insensitively compare the full text of the node.  c1 is the
1840          * first character.  c2 is its fold.  This logic will not work for
1841          * Unicode semantics and the german sharp ss, which hence should
1842          * not be compiled into a node that gets here. */
1843         pat_string = STRING(c);
1844         ln  = STR_LEN(c);       /* length to match in octets/bytes */
1845
1846         /* We know that we have to match at least 'ln' bytes (which is the
1847          * same as characters, since not utf8).  If we have to match 3
1848          * characters, and there are only 2 availabe, we know without
1849          * trying that it will fail; so don't start a match past the
1850          * required minimum number from the far end */
1851         e = HOP3c(strend, -((SSize_t)ln), s);
1852
1853         if (reginfo->intuit && e < s) {
1854             e = s;                      /* Due to minlen logic of intuit() */
1855         }
1856
1857         c1 = *pat_string;
1858         c2 = fold_array[c1];
1859         if (c1 == c2) { /* If char and fold are the same */
1860             REXEC_FBC_EXACTISH_SCAN(*(U8*)s == c1);
1861         }
1862         else {
1863             REXEC_FBC_EXACTISH_SCAN(*(U8*)s == c1 || *(U8*)s == c2);
1864         }
1865         break;
1866
1867     do_exactf_utf8:
1868     {
1869         unsigned expansion;
1870
1871         /* If one of the operands is in utf8, we can't use the simpler folding
1872          * above, due to the fact that many different characters can have the
1873          * same fold, or portion of a fold, or different- length fold */
1874         pat_string = STRING(c);
1875         ln  = STR_LEN(c);       /* length to match in octets/bytes */
1876         pat_end = pat_string + ln;
1877         lnc = is_utf8_pat       /* length to match in characters */
1878                 ? utf8_length((U8 *) pat_string, (U8 *) pat_end)
1879                 : ln;
1880
1881         /* We have 'lnc' characters to match in the pattern, but because of
1882          * multi-character folding, each character in the target can match
1883          * up to 3 characters (Unicode guarantees it will never exceed
1884          * this) if it is utf8-encoded; and up to 2 if not (based on the
1885          * fact that the Latin 1 folds are already determined, and the
1886          * only multi-char fold in that range is the sharp-s folding to
1887          * 'ss'.  Thus, a pattern character can match as little as 1/3 of a
1888          * string character.  Adjust lnc accordingly, rounding up, so that
1889          * if we need to match at least 4+1/3 chars, that really is 5. */
1890         expansion = (utf8_target) ? UTF8_MAX_FOLD_CHAR_EXPAND : 2;
1891         lnc = (lnc + expansion - 1) / expansion;
1892
1893         /* As in the non-UTF8 case, if we have to match 3 characters, and
1894          * only 2 are left, it's guaranteed to fail, so don't start a
1895          * match that would require us to go beyond the end of the string
1896          */
1897         e = HOP3c(strend, -((SSize_t)lnc), s);
1898
1899         if (reginfo->intuit && e < s) {
1900             e = s;                      /* Due to minlen logic of intuit() */
1901         }
1902
1903         /* XXX Note that we could recalculate e to stop the loop earlier,
1904          * as the worst case expansion above will rarely be met, and as we
1905          * go along we would usually find that e moves further to the left.
1906          * This would happen only after we reached the point in the loop
1907          * where if there were no expansion we should fail.  Unclear if
1908          * worth the expense */
1909
1910         while (s <= e) {
1911             char *my_strend= (char *)strend;
1912             if (foldEQ_utf8_flags(s, &my_strend, 0,  utf8_target,
1913                   pat_string, NULL, ln, is_utf8_pat, utf8_fold_flags)
1914                 && (reginfo->intuit || regtry(reginfo, &s)) )
1915             {
1916                 goto got_it;
1917             }
1918             s += (utf8_target) ? UTF8SKIP(s) : 1;
1919         }
1920         break;
1921     }
1922
1923     case BOUNDL:
1924         FBC_BOUND(isWORDCHAR_LC, isWORDCHAR_LC_uvchr, isWORDCHAR_LC_utf8);
1925         break;
1926     case NBOUNDL:
1927         FBC_NBOUND(isWORDCHAR_LC, isWORDCHAR_LC_uvchr, isWORDCHAR_LC_utf8);
1928         break;
1929     case BOUND:
1930         FBC_BOUND(isWORDCHAR, isWORDCHAR_uni, isWORDCHAR_utf8);
1931         break;
1932     case BOUNDA:
1933         FBC_BOUND_A(isWORDCHAR_A, isWORDCHAR_A, isWORDCHAR_A);
1934         break;
1935     case NBOUND:
1936         FBC_NBOUND(isWORDCHAR, isWORDCHAR_uni, isWORDCHAR_utf8);
1937         break;
1938     case NBOUNDA:
1939         FBC_NBOUND_A(isWORDCHAR_A, isWORDCHAR_A, isWORDCHAR_A);
1940         break;
1941     case BOUNDU:
1942         FBC_BOUND(isWORDCHAR_L1, isWORDCHAR_uni, isWORDCHAR_utf8);
1943         break;
1944     case NBOUNDU:
1945         FBC_NBOUND(isWORDCHAR_L1, isWORDCHAR_uni, isWORDCHAR_utf8);
1946         break;
1947     case LNBREAK:
1948         REXEC_FBC_CSCAN(is_LNBREAK_utf8_safe(s, strend),
1949                         is_LNBREAK_latin1_safe(s, strend)
1950         );
1951         break;
1952
1953     /* The argument to all the POSIX node types is the class number to pass to
1954      * _generic_isCC() to build a mask for searching in PL_charclass[] */
1955
1956     case NPOSIXL:
1957         to_complement = 1;
1958         /* FALLTHROUGH */
1959
1960     case POSIXL:
1961         REXEC_FBC_CSCAN(to_complement ^ cBOOL(isFOO_utf8_lc(FLAGS(c), (U8 *) s)),
1962                         to_complement ^ cBOOL(isFOO_lc(FLAGS(c), *s)));
1963         break;
1964
1965     case NPOSIXD:
1966         to_complement = 1;
1967         /* FALLTHROUGH */
1968
1969     case POSIXD:
1970         if (utf8_target) {
1971             goto posix_utf8;
1972         }
1973         goto posixa;
1974
1975     case NPOSIXA:
1976         if (utf8_target) {
1977             /* The complement of something that matches only ASCII matches all
1978              * non-ASCII, plus everything in ASCII that isn't in the class. */
1979             REXEC_FBC_UTF8_CLASS_SCAN(! isASCII_utf8(s)
1980                                       || ! _generic_isCC_A(*s, FLAGS(c)));
1981             break;
1982         }
1983
1984         to_complement = 1;
1985         /* FALLTHROUGH */
1986
1987     case POSIXA:
1988       posixa:
1989         /* Don't need to worry about utf8, as it can match only a single
1990          * byte invariant character. */
1991         REXEC_FBC_CLASS_SCAN(
1992                         to_complement ^ cBOOL(_generic_isCC_A(*s, FLAGS(c))));
1993         break;
1994
1995     case NPOSIXU:
1996         to_complement = 1;
1997         /* FALLTHROUGH */
1998
1999     case POSIXU:
2000         if (! utf8_target) {
2001             REXEC_FBC_CLASS_SCAN(to_complement ^ cBOOL(_generic_isCC(*s,
2002                                                                     FLAGS(c))));
2003         }
2004         else {
2005
2006       posix_utf8:
2007             classnum = (_char_class_number) FLAGS(c);
2008             if (classnum < _FIRST_NON_SWASH_CC) {
2009                 while (s < strend) {
2010
2011                     /* We avoid loading in the swash as long as possible, but
2012                      * should we have to, we jump to a separate loop.  This
2013                      * extra 'if' statement is what keeps this code from being
2014                      * just a call to REXEC_FBC_UTF8_CLASS_SCAN() */
2015                     if (UTF8_IS_ABOVE_LATIN1(*s)) {
2016                         goto found_above_latin1;
2017                     }
2018                     if ((UTF8_IS_INVARIANT(*s)
2019                          && to_complement ^ cBOOL(_generic_isCC((U8) *s,
2020                                                                 classnum)))
2021                         || (UTF8_IS_DOWNGRADEABLE_START(*s)
2022                             && to_complement ^ cBOOL(
2023                                 _generic_isCC(TWO_BYTE_UTF8_TO_NATIVE(*s,
2024                                                                       *(s + 1)),
2025                                               classnum))))
2026                     {
2027                         if (tmp && (reginfo->intuit || regtry(reginfo, &s)))
2028                             goto got_it;
2029                         else {
2030                             tmp = doevery;
2031                         }
2032                     }
2033                     else {
2034                         tmp = 1;
2035                     }
2036                     s += UTF8SKIP(s);
2037                 }
2038             }
2039             else switch (classnum) {    /* These classes are implemented as
2040                                            macros */
2041                 case _CC_ENUM_SPACE: /* XXX would require separate code if we
2042                                         revert the change of \v matching this */
2043                     /* FALLTHROUGH */
2044
2045                 case _CC_ENUM_PSXSPC:
2046                     REXEC_FBC_UTF8_CLASS_SCAN(
2047                                         to_complement ^ cBOOL(isSPACE_utf8(s)));
2048                     break;
2049
2050                 case _CC_ENUM_BLANK:
2051                     REXEC_FBC_UTF8_CLASS_SCAN(
2052                                         to_complement ^ cBOOL(isBLANK_utf8(s)));
2053                     break;
2054
2055                 case _CC_ENUM_XDIGIT:
2056                     REXEC_FBC_UTF8_CLASS_SCAN(
2057                                        to_complement ^ cBOOL(isXDIGIT_utf8(s)));
2058                     break;
2059
2060                 case _CC_ENUM_VERTSPACE:
2061                     REXEC_FBC_UTF8_CLASS_SCAN(
2062                                        to_complement ^ cBOOL(isVERTWS_utf8(s)));
2063                     break;
2064
2065                 case _CC_ENUM_CNTRL:
2066                     REXEC_FBC_UTF8_CLASS_SCAN(
2067                                         to_complement ^ cBOOL(isCNTRL_utf8(s)));
2068                     break;
2069
2070                 default:
2071                     Perl_croak(aTHX_ "panic: find_byclass() node %d='%s' has an unexpected character class '%d'", OP(c), PL_reg_name[OP(c)], classnum);
2072                     NOT_REACHED; /* NOTREACHED */
2073             }
2074         }
2075         break;
2076
2077       found_above_latin1:   /* Here we have to load a swash to get the result
2078                                for the current code point */
2079         if (! PL_utf8_swash_ptrs[classnum]) {
2080             U8 flags = _CORE_SWASH_INIT_ACCEPT_INVLIST;
2081             PL_utf8_swash_ptrs[classnum] =
2082                     _core_swash_init("utf8",
2083                                      "",
2084                                      &PL_sv_undef, 1, 0,
2085                                      PL_XPosix_ptrs[classnum], &flags);
2086         }
2087
2088         /* This is a copy of the loop above for swash classes, though using the
2089          * FBC macro instead of being expanded out.  Since we've loaded the
2090          * swash, we don't have to check for that each time through the loop */
2091         REXEC_FBC_UTF8_CLASS_SCAN(
2092                 to_complement ^ cBOOL(_generic_utf8(
2093                                       classnum,
2094                                       s,
2095                                       swash_fetch(PL_utf8_swash_ptrs[classnum],
2096                                                   (U8 *) s, TRUE))));
2097         break;
2098
2099     case AHOCORASICKC:
2100     case AHOCORASICK:
2101         {
2102             DECL_TRIE_TYPE(c);
2103             /* what trie are we using right now */
2104             reg_ac_data *aho = (reg_ac_data*)progi->data->data[ ARG( c ) ];
2105             reg_trie_data *trie = (reg_trie_data*)progi->data->data[ aho->trie ];
2106             HV *widecharmap = MUTABLE_HV(progi->data->data[ aho->trie + 1 ]);
2107
2108             const char *last_start = strend - trie->minlen;
2109 #ifdef DEBUGGING
2110             const char *real_start = s;
2111 #endif
2112             STRLEN maxlen = trie->maxlen;
2113             SV *sv_points;
2114             U8 **points; /* map of where we were in the input string
2115                             when reading a given char. For ASCII this
2116                             is unnecessary overhead as the relationship
2117                             is always 1:1, but for Unicode, especially
2118                             case folded Unicode this is not true. */
2119             U8 foldbuf[ UTF8_MAXBYTES_CASE + 1 ];
2120             U8 *bitmap=NULL;
2121
2122
2123             GET_RE_DEBUG_FLAGS_DECL;
2124
2125             /* We can't just allocate points here. We need to wrap it in
2126              * an SV so it gets freed properly if there is a croak while
2127              * running the match */
2128             ENTER;
2129             SAVETMPS;
2130             sv_points=newSV(maxlen * sizeof(U8 *));
2131             SvCUR_set(sv_points,
2132                 maxlen * sizeof(U8 *));
2133             SvPOK_on(sv_points);
2134             sv_2mortal(sv_points);
2135             points=(U8**)SvPV_nolen(sv_points );
2136             if ( trie_type != trie_utf8_fold
2137                  && (trie->bitmap || OP(c)==AHOCORASICKC) )
2138             {
2139                 if (trie->bitmap)
2140                     bitmap=(U8*)trie->bitmap;
2141                 else
2142                     bitmap=(U8*)ANYOF_BITMAP(c);
2143             }
2144             /* this is the Aho-Corasick algorithm modified a touch
2145                to include special handling for long "unknown char" sequences.
2146                The basic idea being that we use AC as long as we are dealing
2147                with a possible matching char, when we encounter an unknown char
2148                (and we have not encountered an accepting state) we scan forward
2149                until we find a legal starting char.
2150                AC matching is basically that of trie matching, except that when
2151                we encounter a failing transition, we fall back to the current
2152                states "fail state", and try the current char again, a process
2153                we repeat until we reach the root state, state 1, or a legal
2154                transition. If we fail on the root state then we can either
2155                terminate if we have reached an accepting state previously, or
2156                restart the entire process from the beginning if we have not.
2157
2158              */
2159             while (s <= last_start) {
2160                 const U32 uniflags = UTF8_ALLOW_DEFAULT;
2161                 U8 *uc = (U8*)s;
2162                 U16 charid = 0;
2163                 U32 base = 1;
2164                 U32 state = 1;
2165                 UV uvc = 0;
2166                 STRLEN len = 0;
2167                 STRLEN foldlen = 0;
2168                 U8 *uscan = (U8*)NULL;
2169                 U8 *leftmost = NULL;
2170 #ifdef DEBUGGING
2171                 U32 accepted_word= 0;
2172 #endif
2173                 U32 pointpos = 0;
2174
2175                 while ( state && uc <= (U8*)strend ) {
2176                     int failed=0;
2177                     U32 word = aho->states[ state ].wordnum;
2178
2179                     if( state==1 ) {
2180                         if ( bitmap ) {
2181                             DEBUG_TRIE_EXECUTE_r(
2182                                 if ( uc <= (U8*)last_start && !BITMAP_TEST(bitmap,*uc) ) {
2183                                     dump_exec_pos( (char *)uc, c, strend, real_start,
2184                                         (char *)uc, utf8_target );
2185                                     PerlIO_printf( Perl_debug_log,
2186                                         " Scanning for legal start char...\n");
2187                                 }
2188                             );
2189                             if (utf8_target) {
2190                                 while ( uc <= (U8*)last_start && !BITMAP_TEST(bitmap,*uc) ) {
2191                                     uc += UTF8SKIP(uc);
2192                                 }
2193                             } else {
2194                                 while ( uc <= (U8*)last_start  && !BITMAP_TEST(bitmap,*uc) ) {
2195                                     uc++;
2196                                 }
2197                             }
2198                             s= (char *)uc;
2199                         }
2200                         if (uc >(U8*)last_start) break;
2201                     }
2202
2203                     if ( word ) {
2204                         U8 *lpos= points[ (pointpos - trie->wordinfo[word].len) % maxlen ];
2205                         if (!leftmost || lpos < leftmost) {
2206                             DEBUG_r(accepted_word=word);
2207                             leftmost= lpos;
2208                         }
2209                         if (base==0) break;
2210
2211                     }
2212                     points[pointpos++ % maxlen]= uc;
2213                     if (foldlen || uc < (U8*)strend) {
2214                         REXEC_TRIE_READ_CHAR(trie_type, trie,
2215                                          widecharmap, uc,
2216                                          uscan, len, uvc, charid, foldlen,
2217                                          foldbuf, uniflags);
2218                         DEBUG_TRIE_EXECUTE_r({
2219                             dump_exec_pos( (char *)uc, c, strend,
2220                                         real_start, s, utf8_target);
2221                             PerlIO_printf(Perl_debug_log,
2222                                 " Charid:%3u CP:%4"UVxf" ",
2223                                  charid, uvc);
2224                         });
2225                     }
2226                     else {
2227                         len = 0;
2228                         charid = 0;
2229                     }
2230
2231
2232                     do {
2233 #ifdef DEBUGGING
2234                         word = aho->states[ state ].wordnum;
2235 #endif
2236                         base = aho->states[ state ].trans.base;
2237
2238                         DEBUG_TRIE_EXECUTE_r({
2239                             if (failed)
2240                                 dump_exec_pos( (char *)uc, c, strend, real_start,
2241                                     s,   utf8_target );
2242                             PerlIO_printf( Perl_debug_log,
2243                                 "%sState: %4"UVxf", word=%"UVxf,
2244                                 failed ? " Fail transition to " : "",
2245                                 (UV)state, (UV)word);
2246                         });
2247                         if ( base ) {
2248                             U32 tmp;
2249                             I32 offset;
2250                             if (charid &&
2251                                  ( ((offset = base + charid
2252                                     - 1 - trie->uniquecharcount)) >= 0)
2253                                  && ((U32)offset < trie->lasttrans)
2254                                  && trie->trans[offset].check == state
2255                                  && (tmp=trie->trans[offset].next))
2256                             {
2257                                 DEBUG_TRIE_EXECUTE_r(
2258                                     PerlIO_printf( Perl_debug_log," - legal\n"));
2259                                 state = tmp;
2260                                 break;
2261                             }
2262                             else {
2263                                 DEBUG_TRIE_EXECUTE_r(
2264                                     PerlIO_printf( Perl_debug_log," - fail\n"));
2265                                 failed = 1;
2266                                 state = aho->fail[state];
2267                             }
2268                         }
2269                         else {
2270                             /* we must be accepting here */
2271                             DEBUG_TRIE_EXECUTE_r(
2272                                     PerlIO_printf( Perl_debug_log," - accepting\n"));
2273                             failed = 1;
2274                             break;
2275                         }
2276                     } while(state);
2277                     uc += len;
2278                     if (failed) {
2279                         if (leftmost)
2280                             break;
2281                         if (!state) state = 1;
2282                     }
2283                 }
2284                 if ( aho->states[ state ].wordnum ) {
2285                     U8 *lpos = points[ (pointpos - trie->wordinfo[aho->states[ state ].wordnum].len) % maxlen ];
2286                     if (!leftmost || lpos < leftmost) {
2287                         DEBUG_r(accepted_word=aho->states[ state ].wordnum);
2288                         leftmost = lpos;
2289                     }
2290                 }
2291                 if (leftmost) {
2292                     s = (char*)leftmost;
2293                     DEBUG_TRIE_EXECUTE_r({
2294                         PerlIO_printf(
2295                             Perl_debug_log,"Matches word #%"UVxf" at position %"IVdf". Trying full pattern...\n",
2296                             (UV)accepted_word, (IV)(s - real_start)
2297                         );
2298                     });
2299                     if (reginfo->intuit || regtry(reginfo, &s)) {
2300                         FREETMPS;
2301                         LEAVE;
2302                         goto got_it;
2303                     }
2304                     s = HOPc(s,1);
2305                     DEBUG_TRIE_EXECUTE_r({
2306                         PerlIO_printf( Perl_debug_log,"Pattern failed. Looking for new start point...\n");
2307                     });
2308                 } else {
2309                     DEBUG_TRIE_EXECUTE_r(
2310                         PerlIO_printf( Perl_debug_log,"No match.\n"));
2311                     break;
2312                 }
2313             }
2314             FREETMPS;
2315             LEAVE;
2316         }
2317         break;
2318     default:
2319         Perl_croak(aTHX_ "panic: unknown regstclass %d", (int)OP(c));
2320     }
2321     return 0;
2322   got_it:
2323     return s;
2324 }
2325
2326 /* set RX_SAVED_COPY, RX_SUBBEG etc.
2327  * flags have same meanings as with regexec_flags() */
2328
2329 static void
2330 S_reg_set_capture_string(pTHX_ REGEXP * const rx,
2331                             char *strbeg,
2332                             char *strend,
2333                             SV *sv,
2334                             U32 flags,
2335                             bool utf8_target)
2336 {
2337     struct regexp *const prog = ReANY(rx);
2338
2339     if (flags & REXEC_COPY_STR) {
2340 #ifdef PERL_ANY_COW
2341         if (SvCANCOW(sv)) {
2342             if (DEBUG_C_TEST) {
2343                 PerlIO_printf(Perl_debug_log,
2344                               "Copy on write: regexp capture, type %d\n",
2345                               (int) SvTYPE(sv));
2346             }
2347             /* Create a new COW SV to share the match string and store
2348              * in saved_copy, unless the current COW SV in saved_copy
2349              * is valid and suitable for our purpose */
2350             if ((   prog->saved_copy
2351                  && SvIsCOW(prog->saved_copy)
2352                  && SvPOKp(prog->saved_copy)
2353                  && SvIsCOW(sv)
2354                  && SvPOKp(sv)
2355                  && SvPVX(sv) == SvPVX(prog->saved_copy)))
2356             {
2357                 /* just reuse saved_copy SV */
2358                 if (RXp_MATCH_COPIED(prog)) {
2359                     Safefree(prog->subbeg);
2360                     RXp_MATCH_COPIED_off(prog);
2361                 }
2362             }
2363             else {
2364                 /* create new COW SV to share string */
2365                 RX_MATCH_COPY_FREE(rx);
2366                 prog->saved_copy = sv_setsv_cow(prog->saved_copy, sv);
2367             }
2368             prog->subbeg = (char *)SvPVX_const(prog->saved_copy);
2369             assert (SvPOKp(prog->saved_copy));
2370             prog->sublen  = strend - strbeg;
2371             prog->suboffset = 0;
2372             prog->subcoffset = 0;
2373         } else
2374 #endif
2375         {
2376             SSize_t min = 0;
2377             SSize_t max = strend - strbeg;
2378             SSize_t sublen;
2379
2380             if (    (flags & REXEC_COPY_SKIP_POST)
2381                 && !(prog->extflags & RXf_PMf_KEEPCOPY) /* //p */
2382                 && !(PL_sawampersand & SAWAMPERSAND_RIGHT)
2383             ) { /* don't copy $' part of string */
2384                 U32 n = 0;
2385                 max = -1;
2386                 /* calculate the right-most part of the string covered
2387                  * by a capture. Due to look-ahead, this may be to
2388                  * the right of $&, so we have to scan all captures */
2389                 while (n <= prog->lastparen) {
2390                     if (prog->offs[n].end > max)
2391                         max = prog->offs[n].end;
2392                     n++;
2393                 }
2394                 if (max == -1)
2395                     max = (PL_sawampersand & SAWAMPERSAND_LEFT)
2396                             ? prog->offs[0].start
2397                             : 0;
2398                 assert(max >= 0 && max <= strend - strbeg);
2399             }
2400
2401             if (    (flags & REXEC_COPY_SKIP_PRE)
2402                 && !(prog->extflags & RXf_PMf_KEEPCOPY) /* //p */
2403                 && !(PL_sawampersand & SAWAMPERSAND_LEFT)
2404             ) { /* don't copy $` part of string */
2405                 U32 n = 0;
2406                 min = max;
2407                 /* calculate the left-most part of the string covered
2408                  * by a capture. Due to look-behind, this may be to
2409                  * the left of $&, so we have to scan all captures */
2410                 while (min && n <= prog->lastparen) {
2411                     if (   prog->offs[n].start != -1
2412                         && prog->offs[n].start < min)
2413                     {
2414                         min = prog->offs[n].start;
2415                     }
2416                     n++;
2417                 }
2418                 if ((PL_sawampersand & SAWAMPERSAND_RIGHT)
2419                     && min >  prog->offs[0].end
2420                 )
2421                     min = prog->offs[0].end;
2422
2423             }
2424
2425             assert(min >= 0 && min <= max && min <= strend - strbeg);
2426             sublen = max - min;
2427
2428             if (RX_MATCH_COPIED(rx)) {
2429                 if (sublen > prog->sublen)
2430                     prog->subbeg =
2431                             (char*)saferealloc(prog->subbeg, sublen+1);
2432             }
2433             else
2434                 prog->subbeg = (char*)safemalloc(sublen+1);
2435             Copy(strbeg + min, prog->subbeg, sublen, char);
2436             prog->subbeg[sublen] = '\0';
2437             prog->suboffset = min;
2438             prog->sublen = sublen;
2439             RX_MATCH_COPIED_on(rx);
2440         }
2441         prog->subcoffset = prog->suboffset;
2442         if (prog->suboffset && utf8_target) {
2443             /* Convert byte offset to chars.
2444              * XXX ideally should only compute this if @-/@+
2445              * has been seen, a la PL_sawampersand ??? */
2446
2447             /* If there's a direct correspondence between the
2448              * string which we're matching and the original SV,
2449              * then we can use the utf8 len cache associated with
2450              * the SV. In particular, it means that under //g,
2451              * sv_pos_b2u() will use the previously cached
2452              * position to speed up working out the new length of
2453              * subcoffset, rather than counting from the start of
2454              * the string each time. This stops
2455              *   $x = "\x{100}" x 1E6; 1 while $x =~ /(.)/g;
2456              * from going quadratic */
2457             if (SvPOKp(sv) && SvPVX(sv) == strbeg)
2458                 prog->subcoffset = sv_pos_b2u_flags(sv, prog->subcoffset,
2459                                                 SV_GMAGIC|SV_CONST_RETURN);
2460             else
2461                 prog->subcoffset = utf8_length((U8*)strbeg,
2462                                     (U8*)(strbeg+prog->suboffset));
2463         }
2464     }
2465     else {
2466         RX_MATCH_COPY_FREE(rx);
2467         prog->subbeg = strbeg;
2468         prog->suboffset = 0;
2469         prog->subcoffset = 0;
2470         prog->sublen = strend - strbeg;
2471     }
2472 }
2473
2474
2475
2476
2477 /*
2478  - regexec_flags - match a regexp against a string
2479  */
2480 I32
2481 Perl_regexec_flags(pTHX_ REGEXP * const rx, char *stringarg, char *strend,
2482               char *strbeg, SSize_t minend, SV *sv, void *data, U32 flags)
2483 /* stringarg: the point in the string at which to begin matching */
2484 /* strend:    pointer to null at end of string */
2485 /* strbeg:    real beginning of string */
2486 /* minend:    end of match must be >= minend bytes after stringarg. */
2487 /* sv:        SV being matched: only used for utf8 flag, pos() etc; string
2488  *            itself is accessed via the pointers above */
2489 /* data:      May be used for some additional optimizations.
2490               Currently unused. */
2491 /* flags:     For optimizations. See REXEC_* in regexp.h */
2492
2493 {
2494     struct regexp *const prog = ReANY(rx);
2495     char *s;
2496     regnode *c;
2497     char *startpos;
2498     SSize_t minlen;             /* must match at least this many chars */
2499     SSize_t dontbother = 0;     /* how many characters not to try at end */
2500     const bool utf8_target = cBOOL(DO_UTF8(sv));
2501     I32 multiline;
2502     RXi_GET_DECL(prog,progi);
2503     regmatch_info reginfo_buf;  /* create some info to pass to regtry etc */
2504     regmatch_info *const reginfo = &reginfo_buf;
2505     regexp_paren_pair *swap = NULL;
2506     I32 oldsave;
2507     GET_RE_DEBUG_FLAGS_DECL;
2508
2509     PERL_ARGS_ASSERT_REGEXEC_FLAGS;
2510     PERL_UNUSED_ARG(data);
2511
2512     /* Be paranoid... */
2513     if (prog == NULL || stringarg == NULL) {
2514         Perl_croak(aTHX_ "NULL regexp parameter");
2515     }
2516
2517     DEBUG_EXECUTE_r(
2518         debug_start_match(rx, utf8_target, stringarg, strend,
2519         "Matching");
2520     );
2521
2522     startpos = stringarg;
2523
2524     if (prog->intflags & PREGf_GPOS_SEEN) {
2525         MAGIC *mg;
2526
2527         /* set reginfo->ganch, the position where \G can match */
2528
2529         reginfo->ganch =
2530             (flags & REXEC_IGNOREPOS)
2531             ? stringarg /* use start pos rather than pos() */
2532             : (sv && (mg = mg_find_mglob(sv)) && mg->mg_len >= 0)
2533               /* Defined pos(): */
2534             ? strbeg + MgBYTEPOS(mg, sv, strbeg, strend-strbeg)
2535             : strbeg; /* pos() not defined; use start of string */
2536
2537         DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log,
2538             "GPOS ganch set to strbeg[%"IVdf"]\n", (IV)(reginfo->ganch - strbeg)));
2539
2540         /* in the presence of \G, we may need to start looking earlier in
2541          * the string than the suggested start point of stringarg:
2542          * if prog->gofs is set, then that's a known, fixed minimum
2543          * offset, such as
2544          * /..\G/:   gofs = 2
2545          * /ab|c\G/: gofs = 1
2546          * or if the minimum offset isn't known, then we have to go back
2547          * to the start of the string, e.g. /w+\G/
2548          */
2549
2550         if (prog->intflags & PREGf_ANCH_GPOS) {
2551             startpos  = reginfo->ganch - prog->gofs;
2552             if (startpos <
2553                 ((flags & REXEC_FAIL_ON_UNDERFLOW) ? stringarg : strbeg))
2554             {
2555                 DEBUG_r(PerlIO_printf(Perl_debug_log,
2556                         "fail: ganch-gofs before earliest possible start\n"));
2557                 return 0;
2558             }
2559         }
2560         else if (prog->gofs) {
2561             if (startpos - prog->gofs < strbeg)
2562                 startpos = strbeg;
2563             else
2564                 startpos -= prog->gofs;
2565         }
2566         else if (prog->intflags & PREGf_GPOS_FLOAT)
2567             startpos = strbeg;
2568     }
2569
2570     minlen = prog->minlen;
2571     if ((startpos + minlen) > strend || startpos < strbeg) {
2572         DEBUG_r(PerlIO_printf(Perl_debug_log,
2573                     "Regex match can't succeed, so not even tried\n"));
2574         return 0;
2575     }
2576
2577     /* at the end of this function, we'll do a LEAVE_SCOPE(oldsave),
2578      * which will call destuctors to reset PL_regmatch_state, free higher
2579      * PL_regmatch_slabs, and clean up regmatch_info_aux and
2580      * regmatch_info_aux_eval */
2581
2582     oldsave = PL_savestack_ix;
2583
2584     s = startpos;
2585
2586     if ((prog->extflags & RXf_USE_INTUIT)
2587         && !(flags & REXEC_CHECKED))
2588     {
2589         s = re_intuit_start(rx, sv, strbeg, startpos, strend,
2590                                     flags, NULL);
2591         if (!s)
2592             return 0;
2593
2594         if (prog->extflags & RXf_CHECK_ALL) {
2595             /* we can match based purely on the result of INTUIT.
2596              * Set up captures etc just for $& and $-[0]
2597              * (an intuit-only match wont have $1,$2,..) */
2598             assert(!prog->nparens);
2599
2600             /* s/// doesn't like it if $& is earlier than where we asked it to
2601              * start searching (which can happen on something like /.\G/) */
2602             if (       (flags & REXEC_FAIL_ON_UNDERFLOW)
2603                     && (s < stringarg))
2604             {
2605                 /* this should only be possible under \G */
2606                 assert(prog->intflags & PREGf_GPOS_SEEN);
2607                 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
2608                     "matched, but failing for REXEC_FAIL_ON_UNDERFLOW\n"));
2609                 goto phooey;
2610             }
2611
2612             /* match via INTUIT shouldn't have any captures.
2613              * Let @-, @+, $^N know */
2614             prog->lastparen = prog->lastcloseparen = 0;
2615             RX_MATCH_UTF8_set(rx, utf8_target);
2616             prog->offs[0].start = s - strbeg;
2617             prog->offs[0].end = utf8_target
2618                 ? (char*)utf8_hop((U8*)s, prog->minlenret) - strbeg
2619                 : s - strbeg + prog->minlenret;
2620             if ( !(flags & REXEC_NOT_FIRST) )
2621                 S_reg_set_capture_string(aTHX_ rx,
2622                                         strbeg, strend,
2623                                         sv, flags, utf8_target);
2624
2625             return 1;
2626         }
2627     }
2628
2629     multiline = prog->extflags & RXf_PMf_MULTILINE;
2630     
2631     if (strend - s < (minlen+(prog->check_offset_min<0?prog->check_offset_min:0))) {
2632         DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
2633                               "String too short [regexec_flags]...\n"));
2634         goto phooey;
2635     }
2636     
2637     /* Check validity of program. */
2638     if (UCHARAT(progi->program) != REG_MAGIC) {
2639         Perl_croak(aTHX_ "corrupted regexp program");
2640     }
2641
2642     RX_MATCH_TAINTED_off(rx);
2643     RX_MATCH_UTF8_set(rx, utf8_target);
2644
2645     reginfo->prog = rx;  /* Yes, sorry that this is confusing.  */
2646     reginfo->intuit = 0;
2647     reginfo->is_utf8_target = cBOOL(utf8_target);
2648     reginfo->is_utf8_pat = cBOOL(RX_UTF8(rx));
2649     reginfo->warned = FALSE;
2650     reginfo->strbeg  = strbeg;
2651     reginfo->sv = sv;
2652     reginfo->poscache_maxiter = 0; /* not yet started a countdown */
2653     reginfo->strend = strend;
2654     /* see how far we have to get to not match where we matched before */
2655     reginfo->till = stringarg + minend;
2656
2657     if (prog->extflags & RXf_EVAL_SEEN && SvPADTMP(sv)) {
2658         /* SAVEFREESV, not sv_mortalcopy, as this SV must last until after
2659            S_cleanup_regmatch_info_aux has executed (registered by
2660            SAVEDESTRUCTOR_X below).  S_cleanup_regmatch_info_aux modifies
2661            magic belonging to this SV.
2662            Not newSVsv, either, as it does not COW.
2663         */
2664         reginfo->sv = newSV(0);
2665         SvSetSV_nosteal(reginfo->sv, sv);
2666         SAVEFREESV(reginfo->sv);
2667     }
2668
2669     /* reserve next 2 or 3 slots in PL_regmatch_state:
2670      * slot N+0: may currently be in use: skip it
2671      * slot N+1: use for regmatch_info_aux struct
2672      * slot N+2: use for regmatch_info_aux_eval struct if we have (?{})'s
2673      * slot N+3: ready for use by regmatch()
2674      */
2675
2676     {
2677         regmatch_state *old_regmatch_state;
2678         regmatch_slab  *old_regmatch_slab;
2679         int i, max = (prog->extflags & RXf_EVAL_SEEN) ? 2 : 1;
2680
2681         /* on first ever match, allocate first slab */
2682         if (!PL_regmatch_slab) {
2683             Newx(PL_regmatch_slab, 1, regmatch_slab);
2684             PL_regmatch_slab->prev = NULL;
2685             PL_regmatch_slab->next = NULL;
2686             PL_regmatch_state = SLAB_FIRST(PL_regmatch_slab);
2687         }
2688
2689         old_regmatch_state = PL_regmatch_state;
2690         old_regmatch_slab  = PL_regmatch_slab;
2691
2692         for (i=0; i <= max; i++) {
2693             if (i == 1)
2694                 reginfo->info_aux = &(PL_regmatch_state->u.info_aux);
2695             else if (i ==2)
2696                 reginfo->info_aux_eval =
2697                 reginfo->info_aux->info_aux_eval =
2698                             &(PL_regmatch_state->u.info_aux_eval);
2699
2700             if (++PL_regmatch_state >  SLAB_LAST(PL_regmatch_slab))
2701                 PL_regmatch_state = S_push_slab(aTHX);
2702         }
2703
2704         /* note initial PL_regmatch_state position; at end of match we'll
2705          * pop back to there and free any higher slabs */
2706
2707         reginfo->info_aux->old_regmatch_state = old_regmatch_state;
2708         reginfo->info_aux->old_regmatch_slab  = old_regmatch_slab;
2709         reginfo->info_aux->poscache = NULL;
2710
2711         SAVEDESTRUCTOR_X(S_cleanup_regmatch_info_aux, reginfo->info_aux);
2712
2713         if ((prog->extflags & RXf_EVAL_SEEN))
2714             S_setup_eval_state(aTHX_ reginfo);
2715         else
2716             reginfo->info_aux_eval = reginfo->info_aux->info_aux_eval = NULL;
2717     }
2718
2719     /* If there is a "must appear" string, look for it. */
2720
2721     if (PL_curpm && (PM_GETRE(PL_curpm) == rx)) {
2722         /* We have to be careful. If the previous successful match
2723            was from this regex we don't want a subsequent partially
2724            successful match to clobber the old results.
2725            So when we detect this possibility we add a swap buffer
2726            to the re, and switch the buffer each match. If we fail,
2727            we switch it back; otherwise we leave it swapped.
2728         */
2729         swap = prog->offs;
2730         /* do we need a save destructor here for eval dies? */
2731         Newxz(prog->offs, (prog->nparens + 1), regexp_paren_pair);
2732         DEBUG_BUFFERS_r(PerlIO_printf(Perl_debug_log,
2733             "rex=0x%"UVxf" saving  offs: orig=0x%"UVxf" new=0x%"UVxf"\n",
2734             PTR2UV(prog),
2735             PTR2UV(swap),
2736             PTR2UV(prog->offs)
2737         ));
2738     }
2739
2740     /* Simplest case:  anchored match need be tried only once. */
2741     /*  [unless only anchor is MBOL - implying multiline is set] */
2742     if (prog->intflags & (PREGf_ANCH & ~PREGf_ANCH_GPOS)) {
2743         if (s == startpos && regtry(reginfo, &s))
2744             goto got_it;
2745         else if (multiline || (prog->intflags & (PREGf_IMPLICIT | PREGf_ANCH_MBOL))) /* XXXX SBOL? */
2746         {
2747             char *end;
2748
2749             if (minlen)
2750                 dontbother = minlen - 1;
2751             end = HOP3c(strend, -dontbother, strbeg) - 1;
2752             /* for multiline we only have to try after newlines */
2753             if (prog->check_substr || prog->check_utf8) {
2754                 /* because of the goto we can not easily reuse the macros for bifurcating the
2755                    unicode/non-unicode match modes here like we do elsewhere - demerphq */
2756                 if (utf8_target) {
2757                     if (s == startpos)
2758                         goto after_try_utf8;
2759                     while (1) {
2760                         if (regtry(reginfo, &s)) {
2761                             goto got_it;
2762                         }
2763                       after_try_utf8:
2764                         if (s > end) {
2765                             goto phooey;
2766                         }
2767                         if (prog->extflags & RXf_USE_INTUIT) {
2768                             s = re_intuit_start(rx, sv, strbeg,
2769                                     s + UTF8SKIP(s), strend, flags, NULL);
2770                             if (!s) {
2771                                 goto phooey;
2772                             }
2773                         }
2774                         else {
2775                             s += UTF8SKIP(s);
2776                         }
2777                     }
2778                 } /* end search for check string in unicode */
2779                 else {
2780                     if (s == startpos) {
2781                         goto after_try_latin;
2782                     }
2783                     while (1) {
2784                         if (regtry(reginfo, &s)) {
2785                             goto got_it;
2786                         }
2787                       after_try_latin:
2788                         if (s > end) {
2789                             goto phooey;
2790                         }
2791                         if (prog->extflags & RXf_USE_INTUIT) {
2792                             s = re_intuit_start(rx, sv, strbeg,
2793                                         s + 1, strend, flags, NULL);
2794                             if (!s) {
2795                                 goto phooey;
2796                             }
2797                         }
2798                         else {
2799                             s++;
2800                         }
2801                     }
2802                 } /* end search for check string in latin*/
2803             } /* end search for check string */
2804             else { /* search for newline */
2805                 if (s > startpos) {
2806                     /*XXX: The s-- is almost definitely wrong here under unicode - demeprhq*/
2807                     s--;
2808                 }
2809                 /* We can use a more efficient search as newlines are the same in unicode as they are in latin */
2810                 while (s <= end) { /* note it could be possible to match at the end of the string */
2811                     if (*s++ == '\n') { /* don't need PL_utf8skip here */
2812                         if (regtry(reginfo, &s))
2813                             goto got_it;
2814                     }
2815                 }
2816             } /* end search for newline */
2817         } /* end anchored/multiline check string search */
2818         goto phooey;
2819     } else if (prog->intflags & PREGf_ANCH_GPOS)
2820     {
2821         /* PREGf_ANCH_GPOS should never be true if PREGf_GPOS_SEEN is not true */
2822         assert(prog->intflags & PREGf_GPOS_SEEN);
2823         /* For anchored \G, the only position it can match from is
2824          * (ganch-gofs); we already set startpos to this above; if intuit
2825          * moved us on from there, we can't possibly succeed */
2826         assert(startpos == reginfo->ganch - prog->gofs);
2827         if (s == startpos && regtry(reginfo, &s))
2828             goto got_it;
2829         goto phooey;
2830     }
2831
2832     /* Messy cases:  unanchored match. */
2833     if ((prog->anchored_substr || prog->anchored_utf8) && prog->intflags & PREGf_SKIP) {
2834         /* we have /x+whatever/ */
2835         /* it must be a one character string (XXXX Except is_utf8_pat?) */
2836         char ch;
2837 #ifdef DEBUGGING
2838         int did_match = 0;
2839 #endif
2840         if (utf8_target) {
2841             if (! prog->anchored_utf8) {
2842                 to_utf8_substr(prog);
2843             }
2844             ch = SvPVX_const(prog->anchored_utf8)[0];
2845             REXEC_FBC_SCAN(
2846                 if (*s == ch) {
2847                     DEBUG_EXECUTE_r( did_match = 1 );
2848                     if (regtry(reginfo, &s)) goto got_it;
2849                     s += UTF8SKIP(s);
2850                     while (s < strend && *s == ch)
2851                         s += UTF8SKIP(s);
2852                 }
2853             );
2854
2855         }
2856         else {
2857             if (! prog->anchored_substr) {
2858                 if (! to_byte_substr(prog)) {
2859                     NON_UTF8_TARGET_BUT_UTF8_REQUIRED(phooey);
2860                 }
2861             }
2862             ch = SvPVX_const(prog->anchored_substr)[0];
2863             REXEC_FBC_SCAN(
2864                 if (*s == ch) {
2865                     DEBUG_EXECUTE_r( did_match = 1 );
2866                     if (regtry(reginfo, &s)) goto got_it;
2867                     s++;
2868                     while (s < strend && *s == ch)
2869                         s++;
2870                 }
2871             );
2872         }
2873         DEBUG_EXECUTE_r(if (!did_match)
2874                 PerlIO_printf(Perl_debug_log,
2875                                   "Did not find anchored character...\n")
2876                );
2877     }
2878     else if (prog->anchored_substr != NULL
2879               || prog->anchored_utf8 != NULL
2880               || ((prog->float_substr != NULL || prog->float_utf8 != NULL)
2881                   && prog->float_max_offset < strend - s)) {
2882         SV *must;
2883         SSize_t back_max;
2884         SSize_t back_min;
2885         char *last;
2886         char *last1;            /* Last position checked before */
2887 #ifdef DEBUGGING
2888         int did_match = 0;
2889 #endif
2890         if (prog->anchored_substr || prog->anchored_utf8) {
2891             if (utf8_target) {
2892                 if (! prog->anchored_utf8) {
2893                     to_utf8_substr(prog);
2894                 }
2895                 must = prog->anchored_utf8;
2896             }
2897             else {
2898                 if (! prog->anchored_substr) {
2899                     if (! to_byte_substr(prog)) {
2900                         NON_UTF8_TARGET_BUT_UTF8_REQUIRED(phooey);
2901                     }
2902                 }
2903                 must = prog->anchored_substr;
2904             }
2905             back_max = back_min = prog->anchored_offset;
2906         } else {
2907             if (utf8_target) {
2908                 if (! prog->float_utf8) {
2909                     to_utf8_substr(prog);
2910                 }
2911                 must = prog->float_utf8;
2912             }
2913             else {
2914                 if (! prog->float_substr) {
2915                     if (! to_byte_substr(prog)) {
2916                         NON_UTF8_TARGET_BUT_UTF8_REQUIRED(phooey);
2917                     }
2918                 }
2919                 must = prog->float_substr;
2920             }
2921             back_max = prog->float_max_offset;
2922             back_min = prog->float_min_offset;
2923         }
2924             
2925         if (back_min<0) {
2926             last = strend;
2927         } else {
2928             last = HOP3c(strend,        /* Cannot start after this */
2929                   -(SSize_t)(CHR_SVLEN(must)
2930                          - (SvTAIL(must) != 0) + back_min), strbeg);
2931         }
2932         if (s > reginfo->strbeg)
2933             last1 = HOPc(s, -1);
2934         else
2935             last1 = s - 1;      /* bogus */
2936
2937         /* XXXX check_substr already used to find "s", can optimize if
2938            check_substr==must. */
2939         dontbother = 0;
2940         strend = HOPc(strend, -dontbother);
2941         while ( (s <= last) &&
2942                 (s = fbm_instr((unsigned char*)HOP4c(s, back_min, strbeg,  strend),
2943                                   (unsigned char*)strend, must,
2944                                   multiline ? FBMrf_MULTILINE : 0)) ) {
2945             DEBUG_EXECUTE_r( did_match = 1 );
2946             if (HOPc(s, -back_max) > last1) {
2947                 last1 = HOPc(s, -back_min);
2948                 s = HOPc(s, -back_max);
2949             }
2950             else {
2951                 char * const t = (last1 >= reginfo->strbeg)
2952                                     ? HOPc(last1, 1) : last1 + 1;
2953
2954                 last1 = HOPc(s, -back_min);
2955                 s = t;
2956             }
2957             if (utf8_target) {
2958                 while (s <= last1) {
2959                     if (regtry(reginfo, &s))
2960                         goto got_it;
2961                     if (s >= last1) {
2962                         s++; /* to break out of outer loop */
2963                         break;
2964                     }
2965                     s += UTF8SKIP(s);
2966                 }
2967             }
2968             else {
2969                 while (s <= last1) {
2970                     if (regtry(reginfo, &s))
2971                         goto got_it;
2972                     s++;
2973                 }
2974             }
2975         }
2976         DEBUG_EXECUTE_r(if (!did_match) {
2977             RE_PV_QUOTED_DECL(quoted, utf8_target, PERL_DEBUG_PAD_ZERO(0),
2978                 SvPVX_const(must), RE_SV_DUMPLEN(must), 30);
2979             PerlIO_printf(Perl_debug_log, "Did not find %s substr %s%s...\n",
2980                               ((must == prog->anchored_substr || must == prog->anchored_utf8)
2981                                ? "anchored" : "floating"),
2982                 quoted, RE_SV_TAIL(must));
2983         });                 
2984         goto phooey;
2985     }
2986     else if ( (c = progi->regstclass) ) {
2987         if (minlen) {
2988             const OPCODE op = OP(progi->regstclass);
2989             /* don't bother with what can't match */
2990             if (PL_regkind[op] != EXACT && op != CANY && PL_regkind[op] != TRIE)
2991                 strend = HOPc(strend, -(minlen - 1));
2992         }
2993         DEBUG_EXECUTE_r({
2994             SV * const prop = sv_newmortal();
2995             regprop(prog, prop, c, reginfo, NULL);
2996             {
2997                 RE_PV_QUOTED_DECL(quoted,utf8_target,PERL_DEBUG_PAD_ZERO(1),
2998                     s,strend-s,60);
2999                 PerlIO_printf(Perl_debug_log,
3000                     "Matching stclass %.*s against %s (%d bytes)\n",
3001                     (int)SvCUR(prop), SvPVX_const(prop),
3002                      quoted, (int)(strend - s));
3003             }
3004         });
3005         if (find_byclass(prog, c, s, strend, reginfo))
3006             goto got_it;
3007         DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Contradicts stclass... [regexec_flags]\n"));
3008     }
3009     else {
3010         dontbother = 0;
3011         if (prog->float_substr != NULL || prog->float_utf8 != NULL) {
3012             /* Trim the end. */
3013             char *last= NULL;
3014             SV* float_real;
3015             STRLEN len;
3016             const char *little;
3017
3018             if (utf8_target) {
3019                 if (! prog->float_utf8) {
3020                     to_utf8_substr(prog);
3021                 }
3022                 float_real = prog->float_utf8;
3023             }
3024             else {
3025                 if (! prog->float_substr) {
3026                     if (! to_byte_substr(prog)) {
3027                         NON_UTF8_TARGET_BUT_UTF8_REQUIRED(phooey);
3028                     }
3029                 }
3030                 float_real = prog->float_substr;
3031             }
3032
3033             little = SvPV_const(float_real, len);
3034             if (SvTAIL(float_real)) {
3035                     /* This means that float_real contains an artificial \n on
3036                      * the end due to the presence of something like this:
3037                      * /foo$/ where we can match both "foo" and "foo\n" at the
3038                      * end of the string.  So we have to compare the end of the
3039                      * string first against the float_real without the \n and
3040                      * then against the full float_real with the string.  We
3041                      * have to watch out for cases where the string might be
3042                      * smaller than the float_real or the float_real without
3043                      * the \n. */
3044                     char *checkpos= strend - len;
3045                     DEBUG_OPTIMISE_r(
3046                         PerlIO_printf(Perl_debug_log,
3047                             "%sChecking for float_real.%s\n",
3048                             PL_colors[4], PL_colors[5]));
3049                     if (checkpos + 1 < strbeg) {
3050                         /* can't match, even if we remove the trailing \n
3051                          * string is too short to match */
3052                         DEBUG_EXECUTE_r(
3053                             PerlIO_printf(Perl_debug_log,
3054                                 "%sString shorter than required trailing substring, cannot match.%s\n",
3055                                 PL_colors[4], PL_colors[5]));
3056                         goto phooey;
3057                     } else if (memEQ(checkpos + 1, little, len - 1)) {
3058                         /* can match, the end of the string matches without the
3059                          * "\n" */
3060                         last = checkpos + 1;
3061                     } else if (checkpos < strbeg) {
3062                         /* cant match, string is too short when the "\n" is
3063                          * included */
3064                         DEBUG_EXECUTE_r(
3065                             PerlIO_printf(Perl_debug_log,
3066                                 "%sString does not contain required trailing substring, cannot match.%s\n",
3067                                 PL_colors[4], PL_colors[5]));
3068                         goto phooey;
3069                     } else if (!multiline) {
3070                         /* non multiline match, so compare with the "\n" at the
3071                          * end of the string */
3072                         if (memEQ(checkpos, little, len)) {
3073                             last= checkpos;
3074                         } else {
3075                             DEBUG_EXECUTE_r(
3076                                 PerlIO_printf(Perl_debug_log,
3077                                     "%sString does not contain required trailing substring, cannot match.%s\n",
3078                                     PL_colors[4], PL_colors[5]));
3079                             goto phooey;
3080                         }
3081                     } else {
3082                         /* multiline match, so we have to search for a place
3083                          * where the full string is located */
3084                         goto find_last;
3085                     }
3086             } else {
3087                   find_last:
3088                     if (len)
3089                         last = rninstr(s, strend, little, little + len);
3090                     else
3091                         last = strend;  /* matching "$" */
3092             }
3093             if (!last) {
3094                 /* at one point this block contained a comment which was
3095                  * probably incorrect, which said that this was a "should not
3096                  * happen" case.  Even if it was true when it was written I am
3097                  * pretty sure it is not anymore, so I have removed the comment
3098                  * and replaced it with this one. Yves */
3099                 DEBUG_EXECUTE_r(
3100                     PerlIO_printf(Perl_debug_log,
3101                         "%sString does not contain required substring, cannot match.%s\n",
3102                         PL_colors[4], PL_colors[5]
3103                     ));
3104                 goto phooey;
3105             }
3106             dontbother = strend - last + prog->float_min_offset;
3107         }
3108         if (minlen && (dontbother < minlen))
3109             dontbother = minlen - 1;
3110         strend -= dontbother;              /* this one's always in bytes! */
3111         /* We don't know much -- general case. */
3112         if (utf8_target) {
3113             for (;;) {
3114                 if (regtry(reginfo, &s))
3115                     goto got_it;
3116                 if (s >= strend)
3117                     break;
3118                 s += UTF8SKIP(s);
3119             };
3120         }
3121         else {
3122             do {
3123                 if (regtry(reginfo, &s))
3124                     goto got_it;
3125             } while (s++ < strend);
3126         }
3127     }
3128
3129     /* Failure. */
3130     goto phooey;
3131
3132 got_it:
3133     /* s/// doesn't like it if $& is earlier than where we asked it to
3134      * start searching (which can happen on something like /.\G/) */
3135     if (       (flags & REXEC_FAIL_ON_UNDERFLOW)
3136             && (prog->offs[0].start < stringarg - strbeg))
3137     {
3138         /* this should only be possible under \G */
3139         assert(prog->intflags & PREGf_GPOS_SEEN);
3140         DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
3141             "matched, but failing for REXEC_FAIL_ON_UNDERFLOW\n"));
3142         goto phooey;
3143     }
3144
3145     DEBUG_BUFFERS_r(
3146         if (swap)
3147             PerlIO_printf(Perl_debug_log,
3148                 "rex=0x%"UVxf" freeing offs: 0x%"UVxf"\n",
3149                 PTR2UV(prog),
3150                 PTR2UV(swap)
3151             );
3152     );
3153     Safefree(swap);
3154
3155     /* clean up; this will trigger destructors that will free all slabs
3156      * above the current one, and cleanup the regmatch_info_aux
3157      * and regmatch_info_aux_eval sructs */
3158
3159     LEAVE_SCOPE(oldsave);
3160
3161     if (RXp_PAREN_NAMES(prog)) 
3162         (void)hv_iterinit(RXp_PAREN_NAMES(prog));
3163
3164     /* make sure $`, $&, $', and $digit will work later */
3165     if ( !(flags & REXEC_NOT_FIRST) )
3166         S_reg_set_capture_string(aTHX_ rx,
3167                                     strbeg, reginfo->strend,
3168                                     sv, flags, utf8_target);
3169
3170     return 1;
3171
3172 phooey:
3173     DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch failed%s\n",
3174                           PL_colors[4], PL_colors[5]));
3175
3176     /* clean up; this will trigger destructors that will free all slabs
3177      * above the current one, and cleanup the regmatch_info_aux
3178      * and regmatch_info_aux_eval sructs */
3179
3180     LEAVE_SCOPE(oldsave);
3181
3182     if (swap) {
3183         /* we failed :-( roll it back */
3184         DEBUG_BUFFERS_r(PerlIO_printf(Perl_debug_log,
3185             "rex=0x%"UVxf" rolling back offs: freeing=0x%"UVxf" restoring=0x%"UVxf"\n",
3186             PTR2UV(prog),
3187             PTR2UV(prog->offs),
3188             PTR2UV(swap)
3189         ));
3190         Safefree(prog->offs);
3191         prog->offs = swap;
3192     }
3193     return 0;
3194 }
3195
3196
3197 /* Set which rex is pointed to by PL_reg_curpm, handling ref counting.
3198  * Do inc before dec, in case old and new rex are the same */
3199 #define SET_reg_curpm(Re2)                          \
3200     if (reginfo->info_aux_eval) {                   \
3201         (void)ReREFCNT_inc(Re2);                    \
3202         ReREFCNT_dec(PM_GETRE(PL_reg_curpm));       \
3203         PM_SETRE((PL_reg_curpm), (Re2));            \
3204     }
3205
3206
3207 /*
3208  - regtry - try match at specific point
3209  */
3210 STATIC I32                      /* 0 failure, 1 success */
3211 S_regtry(pTHX_ regmatch_info *reginfo, char **startposp)
3212 {
3213     CHECKPOINT lastcp;
3214     REGEXP *const rx = reginfo->prog;
3215     regexp *const prog = ReANY(rx);
3216     SSize_t result;
3217     RXi_GET_DECL(prog,progi);
3218     GET_RE_DEBUG_FLAGS_DECL;
3219
3220     PERL_ARGS_ASSERT_REGTRY;
3221
3222     reginfo->cutpoint=NULL;
3223
3224     prog->offs[0].start = *startposp - reginfo->strbeg;
3225     prog->lastparen = 0;
3226     prog->lastcloseparen = 0;
3227
3228     /* XXXX What this code is doing here?!!!  There should be no need
3229        to do this again and again, prog->lastparen should take care of
3230        this!  --ilya*/
3231
3232     /* Tests pat.t#187 and split.t#{13,14} seem to depend on this code.
3233      * Actually, the code in regcppop() (which Ilya may be meaning by
3234      * prog->lastparen), is not needed at all by the test suite
3235      * (op/regexp, op/pat, op/split), but that code is needed otherwise
3236      * this erroneously leaves $1 defined: "1" =~ /^(?:(\d)x)?\d$/
3237      * Meanwhile, this code *is* needed for the
3238      * above-mentioned test suite tests to succeed.  The common theme
3239      * on those tests seems to be returning null fields from matches.
3240      * --jhi updated by dapm */
3241 #if 1
3242     if (prog->nparens) {
3243         regexp_paren_pair *pp = prog->offs;
3244         I32 i;
3245         for (i = prog->nparens; i > (I32)prog->lastparen; i--) {
3246             ++pp;
3247             pp->start = -1;
3248             pp->end = -1;
3249         }
3250     }
3251 #endif
3252     REGCP_SET(lastcp);
3253     result = regmatch(reginfo, *startposp, progi->program + 1);
3254     if (result != -1) {
3255         prog->offs[0].end = result;
3256         return 1;
3257     }
3258     if (reginfo->cutpoint)
3259         *startposp= reginfo->cutpoint;
3260     REGCP_UNWIND(lastcp);
3261     return 0;
3262 }
3263
3264
3265 #define sayYES goto yes
3266 #define sayNO goto no
3267 #define sayNO_SILENT goto no_silent
3268
3269 /* we dont use STMT_START/END here because it leads to 
3270    "unreachable code" warnings, which are bogus, but distracting. */
3271 #define CACHEsayNO \
3272     if (ST.cache_mask) \
3273        reginfo->info_aux->poscache[ST.cache_offset] |= ST.cache_mask; \
3274     sayNO
3275
3276 /* this is used to determine how far from the left messages like
3277    'failed...' are printed. It should be set such that messages 
3278    are inline with the regop output that created them.
3279 */
3280 #define REPORT_CODE_OFF 32
3281
3282
3283 #define CHRTEST_UNINIT -1001 /* c1/c2 haven't been calculated yet */
3284 #define CHRTEST_VOID   -1000 /* the c1/c2 "next char" test should be skipped */
3285 #define CHRTEST_NOT_A_CP_1 -999
3286 #define CHRTEST_NOT_A_CP_2 -998
3287
3288 /* grab a new slab and return the first slot in it */
3289
3290 STATIC regmatch_state *
3291 S_push_slab(pTHX)
3292 {
3293 #if PERL_VERSION < 9 && !defined(PERL_CORE)
3294     dMY_CXT;
3295 #endif
3296     regmatch_slab *s = PL_regmatch_slab->next;
3297     if (!s) {
3298         Newx(s, 1, regmatch_slab);
3299         s->prev = PL_regmatch_slab;
3300         s->next = NULL;
3301         PL_regmatch_slab->next = s;
3302     }
3303     PL_regmatch_slab = s;
3304     return SLAB_FIRST(s);
3305 }
3306
3307
3308 /* push a new state then goto it */
3309
3310 #define PUSH_STATE_GOTO(state, node, input) \
3311     pushinput = input; \
3312     scan = node; \
3313     st->resume_state = state; \
3314     goto push_state;
3315
3316 /* push a new state with success backtracking, then goto it */
3317
3318 #define PUSH_YES_STATE_GOTO(state, node, input) \
3319     pushinput = input; \
3320     scan = node; \
3321     st->resume_state = state; \
3322     goto push_yes_state;
3323
3324
3325
3326
3327 /*
3328
3329 regmatch() - main matching routine
3330
3331 This is basically one big switch statement in a loop. We execute an op,
3332 set 'next' to point the next op, and continue. If we come to a point which
3333 we may need to backtrack to on failure such as (A|B|C), we push a
3334 backtrack state onto the backtrack stack. On failure, we pop the top
3335 state, and re-enter the loop at the state indicated. If there are no more
3336 states to pop, we return failure.
3337
3338 Sometimes we also need to backtrack on success; for example /A+/, where
3339 after successfully matching one A, we need to go back and try to
3340 match another one; similarly for lookahead assertions: if the assertion
3341 completes successfully, we backtrack to the state just before the assertion
3342 and then carry on.  In these cases, the pushed state is marked as
3343 'backtrack on success too'. This marking is in fact done by a chain of
3344 pointers, each pointing to the previous 'yes' state. On success, we pop to
3345 the nearest yes state, discarding any intermediate failure-only states.
3346 Sometimes a yes state is pushed just to force some cleanup code to be
3347 called at the end of a successful match or submatch; e.g. (??{$re}) uses
3348 it to free the inner regex.
3349
3350 Note that failure backtracking rewinds the cursor position, while
3351 success backtracking leaves it alone.
3352
3353 A pattern is complete when the END op is executed, while a subpattern
3354 such as (?=foo) is complete when the SUCCESS op is executed. Both of these
3355 ops trigger the "pop to last yes state if any, otherwise return true"
3356 behaviour.
3357
3358 A common convention in this function is to use A and B to refer to the two
3359 subpatterns (or to the first nodes thereof) in patterns like /A*B/: so A is
3360 the subpattern to be matched possibly multiple times, while B is the entire
3361 rest of the pattern. Variable and state names reflect this convention.
3362
3363 The states in the main switch are the union of ops and failure/success of
3364 substates associated with with that op.  For example, IFMATCH is the op
3365 that does lookahead assertions /(?=A)B/ and so the IFMATCH state means
3366 'execute IFMATCH'; while IFMATCH_A is a state saying that we have just
3367 successfully matched A and IFMATCH_A_fail is a state saying that we have
3368 just failed to match A. Resume states always come in pairs. The backtrack
3369 state we push is marked as 'IFMATCH_A', but when that is popped, we resume
3370 at IFMATCH_A or IFMATCH_A_fail, depending on whether we are backtracking
3371 on success or failure.
3372
3373 The struct that holds a backtracking state is actually a big union, with
3374 one variant for each major type of op. The variable st points to the
3375 top-most backtrack struct. To make the code clearer, within each
3376 block of code we #define ST to alias the relevant union.
3377
3378 Here's a concrete example of a (vastly oversimplified) IFMATCH
3379 implementation:
3380
3381     switch (state) {
3382     ....
3383
3384 #define ST st->u.ifmatch
3385
3386     case IFMATCH: // we are executing the IFMATCH op, (?=A)B
3387         ST.foo = ...; // some state we wish to save
3388         ...
3389         // push a yes backtrack state with a resume value of
3390         // IFMATCH_A/IFMATCH_A_fail, then continue execution at the
3391         // first node of A:
3392         PUSH_YES_STATE_GOTO(IFMATCH_A, A, newinput);
3393         // NOTREACHED
3394
3395     case IFMATCH_A: // we have successfully executed A; now continue with B
3396         next = B;
3397         bar = ST.foo; // do something with the preserved value
3398         break;
3399
3400     case IFMATCH_A_fail: // A failed, so the assertion failed
3401         ...;   // do some housekeeping, then ...
3402         sayNO; // propagate the failure
3403
3404 #undef ST
3405
3406     ...
3407     }
3408
3409 For any old-timers reading this who are familiar with the old recursive
3410 approach, the code above is equivalent to:
3411
3412     case IFMATCH: // we are executing the IFMATCH op, (?=A)B
3413     {
3414         int foo = ...
3415         ...
3416         if (regmatch(A)) {
3417             next = B;
3418             bar = foo;
3419             break;
3420         }
3421         ...;   // do some housekeeping, then ...
3422         sayNO; // propagate the failure
3423     }
3424
3425 The topmost backtrack state, pointed to by st, is usually free. If you
3426 want to claim it, populate any ST.foo fields in it with values you wish to
3427 save, then do one of
3428
3429         PUSH_STATE_GOTO(resume_state, node, newinput);
3430         PUSH_YES_STATE_GOTO(resume_state, node, newinput);
3431
3432 which sets that backtrack state's resume value to 'resume_state', pushes a
3433 new free entry to the top of the backtrack stack, then goes to 'node'.
3434 On backtracking, the free slot is popped, and the saved state becomes the
3435 new free state. An ST.foo field in this new top state can be temporarily
3436 accessed to retrieve values, but once the main loop is re-entered, it
3437 becomes available for reuse.
3438
3439 Note that the depth of the backtrack stack constantly increases during the
3440 left-to-right execution of the pattern, rather than going up and down with
3441 the pattern nesting. For example the stack is at its maximum at Z at the
3442 end of the pattern, rather than at X in the following:
3443
3444     /(((X)+)+)+....(Y)+....Z/
3445
3446 The only exceptions to this are lookahead/behind assertions and the cut,
3447 (?>A), which pop all the backtrack states associated with A before
3448 continuing.
3449  
3450 Backtrack state structs are allocated in slabs of about 4K in size.
3451 PL_regmatch_state and st always point to the currently active state,
3452 and PL_regmatch_slab points to the slab currently containing
3453 PL_regmatch_state.  The first time regmatch() is called, the first slab is
3454 allocated, and is never freed until interpreter destruction. When the slab
3455 is full, a new one is allocated and chained to the end. At exit from
3456 regmatch(), slabs allocated since entry are freed.
3457
3458 */
3459  
3460
3461 #define DEBUG_STATE_pp(pp)                                  \
3462     DEBUG_STATE_r({                                         \
3463         DUMP_EXEC_POS(locinput, scan, utf8_target);         \
3464         PerlIO_printf(Perl_debug_log,                       \
3465             "    %*s"pp" %s%s%s%s%s\n",                     \
3466             depth*2, "",                                    \
3467             PL_reg_name[st->resume_state],                  \
3468             ((st==yes_state||st==mark_state) ? "[" : ""),   \
3469             ((st==yes_state) ? "Y" : ""),                   \
3470             ((st==mark_state) ? "M" : ""),                  \
3471             ((st==yes_state||st==mark_state) ? "]" : "")    \
3472         );                                                  \
3473     });
3474
3475
3476 #define REG_NODE_NUM(x) ((x) ? (int)((x)-prog) : -1)
3477
3478 #ifdef DEBUGGING
3479
3480 STATIC void
3481 S_debug_start_match(pTHX_ const REGEXP *prog, const bool utf8_target,
3482     const char *start, const char *end, const char *blurb)
3483 {
3484     const bool utf8_pat = RX_UTF8(prog) ? 1 : 0;
3485
3486     PERL_ARGS_ASSERT_DEBUG_START_MATCH;
3487
3488     if (!PL_colorset)   
3489             reginitcolors();    
3490     {
3491         RE_PV_QUOTED_DECL(s0, utf8_pat, PERL_DEBUG_PAD_ZERO(0), 
3492             RX_PRECOMP_const(prog), RX_PRELEN(prog), 60);   
3493         
3494         RE_PV_QUOTED_DECL(s1, utf8_target, PERL_DEBUG_PAD_ZERO(1),
3495             start, end - start, 60); 
3496         
3497         PerlIO_printf(Perl_debug_log, 
3498             "%s%s REx%s %s against %s\n", 
3499                        PL_colors[4], blurb, PL_colors[5], s0, s1); 
3500         
3501         if (utf8_target||utf8_pat)
3502             PerlIO_printf(Perl_debug_log, "UTF-8 %s%s%s...\n",
3503                 utf8_pat ? "pattern" : "",
3504                 utf8_pat && utf8_target ? " and " : "",
3505                 utf8_target ? "string" : ""
3506             ); 
3507     }
3508 }
3509
3510 STATIC void
3511 S_dump_exec_pos(pTHX_ const char *locinput, 
3512                       const regnode *scan, 
3513                       const char *loc_regeol, 
3514                       const char *loc_bostr, 
3515                       const char *loc_reg_starttry,
3516                       const bool utf8_target)
3517 {
3518     const int docolor = *PL_colors[0] || *PL_colors[2] || *PL_colors[4];
3519     const int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
3520     int l = (loc_regeol - locinput) > taill ? taill : (loc_regeol - locinput);
3521     /* The part of the string before starttry has one color
3522        (pref0_len chars), between starttry and current
3523        position another one (pref_len - pref0_len chars),
3524        after the current position the third one.
3525        We assume that pref0_len <= pref_len, otherwise we
3526        decrease pref0_len.  */
3527     int pref_len = (locinput - loc_bostr) > (5 + taill) - l
3528         ? (5 + taill) - l : locinput - loc_bostr;
3529     int pref0_len;
3530
3531     PERL_ARGS_ASSERT_DUMP_EXEC_POS;
3532
3533     while (utf8_target && UTF8_IS_CONTINUATION(*(U8*)(locinput - pref_len)))
3534         pref_len++;
3535     pref0_len = pref_len  - (locinput - loc_reg_starttry);
3536     if (l + pref_len < (5 + taill) && l < loc_regeol - locinput)
3537         l = ( loc_regeol - locinput > (5 + taill) - pref_len
3538               ? (5 + taill) - pref_len : loc_regeol - locinput);
3539     while (utf8_target && UTF8_IS_CONTINUATION(*(U8*)(locinput + l)))
3540         l--;
3541     if (pref0_len < 0)
3542         pref0_len = 0;
3543     if (pref0_len > pref_len)
3544         pref0_len = pref_len;
3545     {
3546         const int is_uni = (utf8_target && OP(scan) != CANY) ? 1 : 0;
3547
3548         RE_PV_COLOR_DECL(s0,len0,is_uni,PERL_DEBUG_PAD(0),
3549             (locinput - pref_len),pref0_len, 60, 4, 5);
3550         
3551         RE_PV_COLOR_DECL(s1,len1,is_uni,PERL_DEBUG_PAD(1),
3552                     (locinput - pref_len + pref0_len),
3553                     pref_len - pref0_len, 60, 2, 3);
3554         
3555         RE_PV_COLOR_DECL(s2,len2,is_uni,PERL_DEBUG_PAD(2),
3556                     locinput, loc_regeol - locinput, 10, 0, 1);
3557
3558         const STRLEN tlen=len0+len1+len2;
3559         PerlIO_printf(Perl_debug_log,
3560                     "%4"IVdf" <%.*s%.*s%s%.*s>%*s|",
3561                     (IV)(locinput - loc_bostr),
3562                     len0, s0,
3563                     len1, s1,
3564                     (docolor ? "" : "> <"),
3565                     len2, s2,
3566                     (int)(tlen > 19 ? 0 :  19 - tlen),
3567                     "");
3568     }
3569 }
3570
3571 #endif
3572
3573 /* reg_check_named_buff_matched()
3574  * Checks to see if a named buffer has matched. The data array of 
3575  * buffer numbers corresponding to the buffer is expected to reside
3576  * in the regexp->data->data array in the slot stored in the ARG() of
3577  * node involved. Note that this routine doesn't actually care about the
3578  * name, that information is not preserved from compilation to execution.
3579  * Returns the index of the leftmost defined buffer with the given name
3580  * or 0 if non of the buffers matched.
3581  */
3582 STATIC I32
3583 S_reg_check_named_buff_matched(const regexp *rex, const regnode *scan)
3584 {
3585     I32 n;
3586     RXi_GET_DECL(rex,rexi);
3587     SV *sv_dat= MUTABLE_SV(rexi->data->data[ ARG( scan ) ]);
3588     I32 *nums=(I32*)SvPVX(sv_dat);
3589
3590     PERL_ARGS_ASSERT_REG_CHECK_NAMED_BUFF_MATCHED;
3591
3592     for ( n=0; n<SvIVX(sv_dat); n++ ) {
3593         if ((I32)rex->lastparen >= nums[n] &&
3594             rex->offs[nums[n]].end != -1)
3595         {
3596             return nums[n];
3597         }
3598     }
3599     return 0;
3600 }
3601
3602
3603 static bool
3604 S_setup_EXACTISH_ST_c1_c2(pTHX_ const regnode * const text_node, int *c1p,
3605         U8* c1_utf8, int *c2p, U8* c2_utf8, regmatch_info *reginfo)
3606 {
3607     /* This function determines if there are one or two characters that match
3608      * the first character of the passed-in EXACTish node <text_node>, and if
3609      * so, returns them in the passed-in pointers.
3610      *
3611      * If it determines that no possible character in the target string can
3612      * match, it returns FALSE; otherwise TRUE.  (The FALSE situation occurs if
3613      * the first character in <text_node> requires UTF-8 to represent, and the
3614      * target string isn't in UTF-8.)
3615      *
3616      * If there are more than two characters that could match the beginning of
3617      * <text_node>, or if more context is required to determine a match or not,
3618      * it sets both *<c1p> and *<c2p> to CHRTEST_VOID.
3619      *
3620      * The motiviation behind this function is to allow the caller to set up
3621      * tight loops for matching.  If <text_node> is of type EXACT, there is
3622      * only one possible character that can match its first character, and so
3623      * the situation is quite simple.  But things get much more complicated if
3624      * folding is involved.  It may be that the first character of an EXACTFish
3625      * node doesn't participate in any possible fold, e.g., punctuation, so it
3626      * can be matched only by itself.  The vast majority of characters that are
3627      * in folds match just two things, their lower and upper-case equivalents.
3628      * But not all are like that; some have multiple possible matches, or match
3629      * sequences of more than one character.  This function sorts all that out.
3630      *
3631      * Consider the patterns A*B or A*?B where A and B are arbitrary.  In a
3632      * loop of trying to match A*, we know we can't exit where the thing
3633      * following it isn't a B.  And something can't be a B unless it is the
3634      * beginning of B.  By putting a quick test for that beginning in a tight
3635      * loop, we can rule out things that can't possibly be B without having to
3636      * break out of the loop, thus avoiding work.  Similarly, if A is a single
3637      * character, we can make a tight loop matching A*, using the outputs of
3638      * this function.
3639      *
3640      * If the target string to match isn't in UTF-8, and there aren't
3641      * complications which require CHRTEST_VOID, *<c1p> and *<c2p> are set to
3642      * the one or two possible octets (which are characters in this situation)
3643      * that can match.  In all cases, if there is only one character that can
3644      * match, *<c1p> and *<c2p> will be identical.
3645      *
3646      * If the target string is in UTF-8, the buffers pointed to by <c1_utf8>
3647      * and <c2_utf8> will contain the one or two UTF-8 sequences of bytes that
3648      * can match the beginning of <text_node>.  They should be declared with at
3649      * least length UTF8_MAXBYTES+1.  (If the target string isn't in UTF-8, it is
3650      * undefined what these contain.)  If one or both of the buffers are
3651      * invariant under UTF-8, *<c1p>, and *<c2p> will also be set to the
3652      * corresponding invariant.  If variant, the corresponding *<c1p> and/or
3653      * *<c2p> will be set to a negative number(s) that shouldn't match any code
3654      * point (unless inappropriately coerced to unsigned).   *<c1p> will equal
3655      * *<c2p> if and only if <c1_utf8> and <c2_utf8> are the same. */
3656
3657     const bool utf8_target = reginfo->is_utf8_target;
3658
3659     UV c1 = (UV)CHRTEST_NOT_A_CP_1;
3660     UV c2 = (UV)CHRTEST_NOT_A_CP_2;
3661     bool use_chrtest_void = FALSE;
3662     const bool is_utf8_pat = reginfo->is_utf8_pat;
3663
3664     /* Used when we have both utf8 input and utf8 output, to avoid converting
3665      * to/from code points */
3666     bool utf8_has_been_setup = FALSE;
3667
3668     dVAR;
3669
3670     U8 *pat = (U8*)STRING(text_node);
3671     U8 folded[UTF8_MAX_FOLD_CHAR_EXPAND * UTF8_MAXBYTES_CASE + 1] = { '\0' };
3672
3673     if (OP(text_node) == EXACT || OP(text_node) == EXACTL) {
3674
3675         /* In an exact node, only one thing can be matched, that first
3676          * character.  If both the pat and the target are UTF-8, we can just
3677          * copy the input to the output, avoiding finding the code point of
3678          * that character */
3679         if (!is_utf8_pat) {
3680             c2 = c1 = *pat;
3681         }
3682         else if (utf8_target) {
3683             Copy(pat, c1_utf8, UTF8SKIP(pat), U8);
3684             Copy(pat, c2_utf8, UTF8SKIP(pat), U8);
3685             utf8_has_been_setup = TRUE;
3686         }
3687         else {
3688             c2 = c1 = valid_utf8_to_uvchr(pat, NULL);
3689         }
3690     }
3691     else { /* an EXACTFish node */
3692         U8 *pat_end = pat + STR_LEN(text_node);
3693
3694         /* An EXACTFL node has at least some characters unfolded, because what
3695          * they match is not known until now.  So, now is the time to fold
3696          * the first few of them, as many as are needed to determine 'c1' and
3697          * 'c2' later in the routine.  If the pattern isn't UTF-8, we only need
3698          * to fold if in a UTF-8 locale, and then only the Sharp S; everything
3699          * else is 1-1 and isn't assumed to be folded.  In a UTF-8 pattern, we
3700          * need to fold as many characters as a single character can fold to,
3701          * so that later we can check if the first ones are such a multi-char
3702          * fold.  But, in such a pattern only locale-problematic characters
3703          * aren't folded, so we can skip this completely if the first character
3704          * in the node isn't one of the tricky ones */
3705         if (OP(text_node) == EXACTFL) {
3706
3707             if (! is_utf8_pat) {
3708                 if (IN_UTF8_CTYPE_LOCALE && *pat == LATIN_SMALL_LETTER_SHARP_S)
3709                 {
3710                     folded[0] = folded[1] = 's';
3711                     pat = folded;
3712                     pat_end = folded + 2;
3713                 }
3714             }
3715             else if (is_PROBLEMATIC_LOCALE_FOLDEDS_START_utf8(pat)) {
3716                 U8 *s = pat;
3717                 U8 *d = folded;
3718                 int i;
3719
3720                 for (i = 0; i < UTF8_MAX_FOLD_CHAR_EXPAND && s < pat_end; i++) {
3721                     if (isASCII(*s)) {
3722                         *(d++) = (U8) toFOLD_LC(*s);
3723                         s++;
3724                     }
3725                     else {
3726                         STRLEN len;
3727                         _to_utf8_fold_flags(s,
3728                                             d,
3729                                             &len,
3730                                             FOLD_FLAGS_FULL | FOLD_FLAGS_LOCALE);
3731                         d += len;
3732                         s += UTF8SKIP(s);
3733                     }
3734                 }
3735
3736                 pat = folded;
3737                 pat_end = d;
3738             }
3739         }
3740
3741         if ((is_utf8_pat && is_MULTI_CHAR_FOLD_utf8_safe(pat, pat_end))
3742              || (!is_utf8_pat && is_MULTI_CHAR_FOLD_latin1_safe(pat, pat_end)))
3743         {
3744             /* Multi-character folds require more context to sort out.  Also
3745              * PL_utf8_foldclosures used below doesn't handle them, so have to
3746              * be handled outside this routine */
3747             use_chrtest_void = TRUE;
3748         }
3749         else { /* an EXACTFish node which doesn't begin with a multi-char fold */
3750             c1 = is_utf8_pat ? valid_utf8_to_uvchr(pat, NULL) : *pat;
3751             if (c1 > 255) {
3752                 /* Load the folds hash, if not already done */
3753                 SV** listp;
3754                 if (! PL_utf8_foldclosures) {
3755                     _load_PL_utf8_foldclosures();
3756                 }
3757
3758                 /* The fold closures data structure is a hash with the keys
3759                  * being the UTF-8 of every character that is folded to, like
3760                  * 'k', and the values each an array of all code points that
3761                  * fold to its key.  e.g. [ 'k', 'K', KELVIN_SIGN ].
3762                  * Multi-character folds are not included */
3763                 if ((! (listp = hv_fetch(PL_utf8_foldclosures,
3764                                         (char *) pat,
3765                                         UTF8SKIP(pat),
3766                                         FALSE))))
3767                 {
3768                     /* Not found in the hash, therefore there are no folds
3769                     * containing it, so there is only a single character that
3770                     * could match */
3771                     c2 = c1;
3772                 }
3773                 else {  /* Does participate in folds */
3774                     AV* list = (AV*) *listp;
3775                     if (av_tindex(list) != 1) {
3776
3777                         /* If there aren't exactly two folds to this, it is
3778                          * outside the scope of this function */
3779                         use_chrtest_void = TRUE;
3780                     }
3781                     else {  /* There are two.  Get them */
3782                         SV** c_p = av_fetch(list, 0, FALSE);
3783                         if (c_p == NULL) {
3784                             Perl_croak(aTHX_ "panic: invalid PL_utf8_foldclosures structure");
3785                         }
3786                         c1 = SvUV(*c_p);
3787
3788                         c_p = av_fetch(list, 1, FALSE);
3789                         if (c_p == NULL) {
3790                             Perl_croak(aTHX_ "panic: invalid PL_utf8_foldclosures structure");
3791                         }
3792                         c2 = SvUV(*c_p);
3793
3794                         /* Folds that cross the 255/256 boundary are forbidden
3795                          * if EXACTFL (and isnt a UTF8 locale), or EXACTFA and
3796                          * one is ASCIII.  Since the pattern character is above
3797                          * 255, and its only other match is below 256, the only
3798                          * legal match will be to itself.  We have thrown away
3799                          * the original, so have to compute which is the one
3800                          * above 255. */
3801                         if ((c1 < 256) != (c2 < 256)) {
3802                             if ((OP(text_node) == EXACTFL
3803                                  && ! IN_UTF8_CTYPE_LOCALE)
3804                                 || ((OP(text_node) == EXACTFA
3805                                     || OP(text_node) == EXACTFA_NO_TRIE)
3806                                     && (isASCII(c1) || isASCII(c2))))
3807                             {
3808                                 if (c1 < 256) {
3809                                     c1 = c2;
3810                                 }
3811                                 else {
3812                                     c2 = c1;
3813                                 }
3814                             }
3815                         }
3816                     }
3817                 }
3818             }
3819             else /* Here, c1 is <= 255 */
3820                 if (utf8_target
3821                     && HAS_NONLATIN1_FOLD_CLOSURE(c1)
3822                     && ( ! (OP(text_node) == EXACTFL && ! IN_UTF8_CTYPE_LOCALE))
3823                     && ((OP(text_node) != EXACTFA
3824                         && OP(text_node) != EXACTFA_NO_TRIE)
3825                         || ! isASCII(c1)))
3826             {
3827                 /* Here, there could be something above Latin1 in the target
3828                  * which folds to this character in the pattern.  All such
3829                  * cases except LATIN SMALL LETTER Y WITH DIAERESIS have more
3830                  * than two characters involved in their folds, so are outside
3831                  * the scope of this function */
3832                 if (UNLIKELY(c1 == LATIN_SMALL_LETTER_Y_WITH_DIAERESIS)) {
3833                     c2 = LATIN_CAPITAL_LETTER_Y_WITH_DIAERESIS;
3834                 }
3835                 else {
3836                     use_chrtest_void = TRUE;
3837                 }
3838             }
3839             else { /* Here nothing above Latin1 can fold to the pattern
3840                       character */
3841                 switch (OP(text_node)) {
3842
3843                     case EXACTFL:   /* /l rules */
3844                         c2 = PL_fold_locale[c1];
3845                         break;
3846
3847                     case EXACTF:   /* This node only generated for non-utf8
3848                                     patterns */
3849                         assert(! is_utf8_pat);
3850                         if (! utf8_target) {    /* /d rules */
3851                             c2 = PL_fold[c1];
3852                             break;
3853                         }
3854                         /* FALLTHROUGH */
3855                         /* /u rules for all these.  This happens to work for
3856                         * EXACTFA as nothing in Latin1 folds to ASCII */
3857                     case EXACTFA_NO_TRIE:   /* This node only generated for
3858                                             non-utf8 patterns */
3859                         assert(! is_utf8_pat);
3860                         /* FALLTHROUGH */
3861                     case EXACTFA:
3862                     case EXACTFU_SS:
3863                     case EXACTFU:
3864                         c2 = PL_fold_latin1[c1];
3865                         break;
3866
3867                     default:
3868                         Perl_croak(aTHX_ "panic: Unexpected op %u", OP(text_node));
3869                         NOT_REACHED; /* NOTREACHED */
3870                 }
3871             }
3872         }
3873     }
3874
3875     /* Here have figured things out.  Set up the returns */
3876     if (use_chrtest_void) {
3877         *c2p = *c1p = CHRTEST_VOID;
3878     }
3879     else if (utf8_target) {
3880         if (! utf8_has_been_setup) {    /* Don't have the utf8; must get it */
3881             uvchr_to_utf8(c1_utf8, c1);
3882             uvchr_to_utf8(c2_utf8, c2);
3883         }
3884
3885         /* Invariants are stored in both the utf8 and byte outputs; Use
3886          * negative numbers otherwise for the byte ones.  Make sure that the
3887       &