This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[perl #130522] don't try to find_by_class outside the string
[perl5.git] / regexec.c
CommitLineData
a0d0e21e
LW
1/* regexec.c
2 */
3
4/*
f65819ce
CO
5 * One Ring to rule them all, One Ring to find them
6 *
4ac71550
TC
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"]
a0d0e21e
LW
10 */
11
61296642
DM
12/* This file contains functions for executing a regular expression. See
13 * also regcomp.c which funnily enough, contains functions for compiling
166f8a29 14 * a regular expression.
e4a054ea
DM
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.
166f8a29
DM
20 */
21
a687059c
LW
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
e50aee73
AD
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
b9d5759e 36#ifdef PERL_EXT_RE_BUILD
54df2634 37#include "re_top.h"
9041c2e3 38#endif
56953603 39
a687059c 40/*
e50aee73 41 * pregcomp and pregexec -- regsub and regerror are not used in perl
a687059c
LW
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 ****
4bb101f2 62 **** Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
1129b882
NC
63 **** 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
64 **** by Larry Wall and others
a687059c 65 ****
9ef589d8
LW
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.
a687059c
LW
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"
864dbfa3 74#define PERL_IN_REGEXEC_C
a687059c 75#include "perl.h"
0f5d15d6 76
54df2634
NC
77#ifdef PERL_IN_XSUB_RE
78# include "re_comp.h"
79#else
80# include "regcomp.h"
81#endif
a687059c 82
b992490d 83#include "invlist_inline.h"
1b0f46bf 84#include "unicode_constants.h"
81e983c1 85
bbac6b20
KW
86#define B_ON_NON_UTF8_LOCALE_IS_WRONG \
87 "Use of \\b{} or \\B{} for non-UTF-8 locale is wrong. Assuming a UTF-8 locale"
88
a0bd1a30
KW
89static const char utf8_locale_required[] =
90 "Use of (?[ ]) for non-UTF-8 locale is wrong. Assuming a UTF-8 locale";
91
e1cf74e3
CB
92#ifdef DEBUGGING
93/* At least one required character in the target string is expressible only in
94 * UTF-8. */
95static const char* const non_utf8_target_but_utf8_required
96 = "Can't match, because target string needs to be in UTF-8\n";
97#endif
98
7b031478 99#define NON_UTF8_TARGET_BUT_UTF8_REQUIRED(target) STMT_START { \
6ad9a8ab 100 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_ "%s", non_utf8_target_but_utf8_required));\
7b031478 101 goto target; \
e1cf74e3
CB
102} STMT_END
103
c74f6de9
KW
104#define HAS_NONLATIN1_FOLD_CLOSURE(i) _HAS_NONLATIN1_FOLD_CLOSURE_ONLY_FOR_USE_BY_REGCOMP_DOT_C_AND_REGEXEC_DOT_C(i)
105
a687059c
LW
106#ifndef STATIC
107#define STATIC static
108#endif
109
451c6e0b
KW
110/* Valid only if 'c', the character being looke-up, is an invariant under
111 * UTF-8: it avoids the reginclass call if there are no complications: i.e., if
112 * everything matchable is straight forward in the bitmap */
113#define REGINCLASS(prog,p,c,u) (ANYOF_FLAGS(p) \
114 ? reginclass(prog,p,c,c+1,u) \
115 : ANYOF_BITMAP_TEST(p,*(c)))
7d3e948e 116
c277df42
IZ
117/*
118 * Forwards.
119 */
120
f2ed9b32 121#define CHR_SVLEN(sv) (utf8_target ? sv_len_utf8(sv) : SvCUR(sv))
a0ed51b3 122
3dab1dad 123#define HOPc(pos,off) \
ba44c216 124 (char *)(reginfo->is_utf8_target \
220db18a 125 ? reghop3((U8*)pos, off, \
9d9163fb 126 (U8*)(off >= 0 ? reginfo->strend : reginfo->strbeg)) \
3dab1dad 127 : (U8*)(pos + off))
557f47af 128
3dab1dad 129#define HOPBACKc(pos, off) \
ba44c216 130 (char*)(reginfo->is_utf8_target \
c708944d 131 ? reghopmaybe3((U8*)pos, (SSize_t)0-off, (U8*)(reginfo->strbeg)) \
9d9163fb 132 : (pos - off >= reginfo->strbeg) \
8e11feef 133 ? (U8*)pos - off \
3dab1dad 134 : NULL)
efb30f32 135
ba44c216 136#define HOP3(pos,off,lim) (reginfo->is_utf8_target ? reghop3((U8*)(pos), off, (U8*)(lim)) : (U8*)(pos + off))
1aa99e6b 137#define HOP3c(pos,off,lim) ((char*)HOP3(pos,off,lim))
1aa99e6b 138
557f47af
DM
139/* lim must be +ve. Returns NULL on overshoot */
140#define HOPMAYBE3(pos,off,lim) \
141 (reginfo->is_utf8_target \
142 ? reghopmaybe3((U8*)pos, off, (U8*)(lim)) \
143 : ((U8*)pos + off <= lim) \
144 ? (U8*)pos + off \
145 : NULL)
146
8e9f2289
DM
147/* like HOP3, but limits the result to <= lim even for the non-utf8 case.
148 * off must be >=0; args should be vars rather than expressions */
149#define HOP3lim(pos,off,lim) (reginfo->is_utf8_target \
150 ? reghop3((U8*)(pos), off, (U8*)(lim)) \
151 : (U8*)((pos + off) > lim ? lim : (pos + off)))
67853908 152#define HOP3clim(pos,off,lim) ((char*)HOP3lim(pos,off,lim))
8e9f2289 153
2974eaec
DM
154#define HOP4(pos,off,llim, rlim) (reginfo->is_utf8_target \
155 ? reghop4((U8*)(pos), off, (U8*)(llim), (U8*)(rlim)) \
156 : (U8*)(pos + off))
157#define HOP4c(pos,off,llim, rlim) ((char*)HOP4(pos,off,llim, rlim))
7016d6eb
DM
158
159#define NEXTCHR_EOS -10 /* nextchr has fallen off the end */
160#define NEXTCHR_IS_EOS (nextchr < 0)
161
162#define SET_nextchr \
220db18a 163 nextchr = ((locinput < reginfo->strend) ? UCHARAT(locinput) : NEXTCHR_EOS)
7016d6eb
DM
164
165#define SET_locinput(p) \
166 locinput = (p); \
167 SET_nextchr
168
169
2a16ac92 170#define LOAD_UTF8_CHARCLASS(swash_ptr, property_name, invlist) STMT_START { \
c7304fe2
KW
171 if (!swash_ptr) { \
172 U8 flags = _CORE_SWASH_INIT_ACCEPT_INVLIST; \
c7304fe2 173 swash_ptr = _core_swash_init("utf8", property_name, &PL_sv_undef, \
2a16ac92 174 1, 0, invlist, &flags); \
c7304fe2
KW
175 assert(swash_ptr); \
176 } \
177 } STMT_END
178
179/* If in debug mode, we test that a known character properly matches */
180#ifdef DEBUGGING
181# define LOAD_UTF8_CHARCLASS_DEBUG_TEST(swash_ptr, \
182 property_name, \
2a16ac92 183 invlist, \
c7304fe2 184 utf8_char_in_property) \
2a16ac92 185 LOAD_UTF8_CHARCLASS(swash_ptr, property_name, invlist); \
c7304fe2
KW
186 assert(swash_fetch(swash_ptr, (U8 *) utf8_char_in_property, TRUE));
187#else
188# define LOAD_UTF8_CHARCLASS_DEBUG_TEST(swash_ptr, \
189 property_name, \
2a16ac92 190 invlist, \
c7304fe2 191 utf8_char_in_property) \
2a16ac92 192 LOAD_UTF8_CHARCLASS(swash_ptr, property_name, invlist)
c7304fe2 193#endif
d1eb3177 194
c7304fe2
KW
195#define LOAD_UTF8_CHARCLASS_ALNUM() LOAD_UTF8_CHARCLASS_DEBUG_TEST( \
196 PL_utf8_swash_ptrs[_CC_WORDCHAR], \
2a16ac92
KW
197 "", \
198 PL_XPosix_ptrs[_CC_WORDCHAR], \
0766489e 199 LATIN_SMALL_LIGATURE_LONG_S_T_UTF8);
c7304fe2 200
c7304fe2 201#define PLACEHOLDER /* Something for the preprocessor to grab onto */
3dab1dad
YO
202/* TODO: Combine JUMPABLE and HAS_TEXT to cache OP(rn) */
203
5f80c4cf 204/* for use after a quantifier and before an EXACT-like node -- japhy */
c35dcbe2
YO
205/* it would be nice to rework regcomp.sym to generate this stuff. sigh
206 *
207 * NOTE that *nothing* that affects backtracking should be in here, specifically
208 * VERBS must NOT be included. JUMPABLE is used to determine if we can ignore a
209 * node that is in between two EXACT like nodes when ascertaining what the required
210 * "follow" character is. This should probably be moved to regex compile time
211 * although it may be done at run time beause of the REF possibility - more
212 * investigation required. -- demerphq
213*/
baa60164
KW
214#define JUMPABLE(rn) ( \
215 OP(rn) == OPEN || \
24be3102
YO
216 (OP(rn) == CLOSE && \
217 !EVAL_CLOSE_PAREN_IS(cur_eval,ARG(rn)) ) || \
baa60164
KW
218 OP(rn) == EVAL || \
219 OP(rn) == SUSPEND || OP(rn) == IFMATCH || \
220 OP(rn) == PLUS || OP(rn) == MINMOD || \
221 OP(rn) == KEEPS || \
222 (PL_regkind[OP(rn)] == CURLY && ARG1(rn) > 0) \
e2d8ce26 223)
ee9b8eae 224#define IS_EXACT(rn) (PL_regkind[OP(rn)] == EXACT)
e2d8ce26 225
ee9b8eae
YO
226#define HAS_TEXT(rn) ( IS_EXACT(rn) || PL_regkind[OP(rn)] == REF )
227
228#if 0
229/* Currently these are only used when PL_regkind[OP(rn)] == EXACT so
a4525e78 230 we don't need this definition. XXX These are now out-of-sync*/
ee9b8eae 231#define IS_TEXT(rn) ( OP(rn)==EXACT || OP(rn)==REF || OP(rn)==NREF )
098b07d5 232#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 )
ee9b8eae
YO
233#define IS_TEXTFL(rn) ( OP(rn)==EXACTFL || OP(rn)==REFFL || OP(rn)==NREFFL )
234
235#else
236/* ... so we use this as its faster. */
a4525e78
KW
237#define IS_TEXT(rn) ( OP(rn)==EXACT || OP(rn)==EXACTL )
238#define IS_TEXTFU(rn) ( OP(rn)==EXACTFU || OP(rn)==EXACTFLU8 || OP(rn)==EXACTFU_SS || OP(rn) == EXACTFA || OP(rn) == EXACTFA_NO_TRIE)
ee9b8eae
YO
239#define IS_TEXTF(rn) ( OP(rn)==EXACTF )
240#define IS_TEXTFL(rn) ( OP(rn)==EXACTFL )
241
242#endif
e2d8ce26 243
a84d97b6
HS
244/*
245 Search for mandatory following text node; for lookahead, the text must
246 follow but for lookbehind (rn->flags != 0) we skip to the next step.
247*/
baa60164 248#define FIND_NEXT_IMPT(rn) STMT_START { \
3dab1dad
YO
249 while (JUMPABLE(rn)) { \
250 const OPCODE type = OP(rn); \
251 if (type == SUSPEND || PL_regkind[type] == CURLY) \
e2d8ce26 252 rn = NEXTOPER(NEXTOPER(rn)); \
3dab1dad 253 else if (type == PLUS) \
cca55fe3 254 rn = NEXTOPER(rn); \
3dab1dad 255 else if (type == IFMATCH) \
a84d97b6 256 rn = (rn->flags == 0) ? NEXTOPER(NEXTOPER(rn)) : rn + ARG(rn); \
e2d8ce26 257 else rn += NEXT_OFF(rn); \
3dab1dad 258 } \
5f80c4cf 259} STMT_END
74750237 260
006f26b2
DM
261#define SLAB_FIRST(s) (&(s)->states[0])
262#define SLAB_LAST(s) (&(s)->states[PERL_REGMATCH_SLAB_SLOTS-1])
263
a75351a1 264static void S_setup_eval_state(pTHX_ regmatch_info *const reginfo);
bf2039a9 265static void S_cleanup_regmatch_info_aux(pTHX_ void *arg);
bf2039a9 266static regmatch_state * S_push_slab(pTHX);
51371543 267
87c0511b 268#define REGCP_PAREN_ELEMS 3
f067efbf 269#define REGCP_OTHER_ELEMS 3
e0fa7e2b 270#define REGCP_FRAME_ELEMS 1
620d5b66
NC
271/* REGCP_FRAME_ELEMS are not part of the REGCP_OTHER_ELEMS and
272 * are needed for the regexp context stack bookkeeping. */
273
76e3520e 274STATIC CHECKPOINT
21553840 275S_regcppush(pTHX_ const regexp *rex, I32 parenfloor, U32 maxopenparen _pDEPTH)
a0d0e21e 276{
a3b680e6 277 const int retval = PL_savestack_ix;
92da3157
DM
278 const int paren_elems_to_push =
279 (maxopenparen - parenfloor) * REGCP_PAREN_ELEMS;
e0fa7e2b
NC
280 const UV total_elems = paren_elems_to_push + REGCP_OTHER_ELEMS;
281 const UV elems_shifted = total_elems << SAVE_TIGHT_SHIFT;
87c0511b 282 I32 p;
40a82448 283 GET_RE_DEBUG_FLAGS_DECL;
a0d0e21e 284
b93070ed
DM
285 PERL_ARGS_ASSERT_REGCPPUSH;
286
e49a9654 287 if (paren_elems_to_push < 0)
e8a85d26
JH
288 Perl_croak(aTHX_ "panic: paren_elems_to_push, %i < 0, maxopenparen: %i parenfloor: %i REGCP_PAREN_ELEMS: %u",
289 (int)paren_elems_to_push, (int)maxopenparen,
290 (int)parenfloor, (unsigned)REGCP_PAREN_ELEMS);
e49a9654 291
e0fa7e2b 292 if ((elems_shifted >> SAVE_TIGHT_SHIFT) != total_elems)
147e3846 293 Perl_croak(aTHX_ "panic: paren_elems_to_push offset %" UVuf
5df417d0 294 " out of range (%lu-%ld)",
92da3157
DM
295 total_elems,
296 (unsigned long)maxopenparen,
297 (long)parenfloor);
e0fa7e2b 298
620d5b66 299 SSGROW(total_elems + REGCP_FRAME_ELEMS);
7f69552c 300
495f47a5 301 DEBUG_BUFFERS_r(
92da3157 302 if ((int)maxopenparen > (int)parenfloor)
2b1a3689 303 Perl_re_exec_indentf( aTHX_
147e3846 304 "rex=0x%" UVxf " offs=0x%" UVxf ": saving capture indices:\n",
2b1a3689
YO
305 depth,
306 PTR2UV(rex),
495f47a5
DM
307 PTR2UV(rex->offs)
308 );
309 );
92da3157 310 for (p = parenfloor+1; p <= (I32)maxopenparen; p++) {
b1ce53c5 311/* REGCP_PARENS_ELEMS are pushed per pairs of parentheses. */
99a90e59
FC
312 SSPUSHIV(rex->offs[p].end);
313 SSPUSHIV(rex->offs[p].start);
1ca2007e 314 SSPUSHINT(rex->offs[p].start_tmp);
2b1a3689 315 DEBUG_BUFFERS_r(Perl_re_exec_indentf( aTHX_
147e3846 316 " \\%" UVuf ": %" IVdf "(%" IVdf ")..%" IVdf "\n",
2b1a3689
YO
317 depth,
318 (UV)p,
495f47a5
DM
319 (IV)rex->offs[p].start,
320 (IV)rex->offs[p].start_tmp,
321 (IV)rex->offs[p].end
40a82448 322 ));
a0d0e21e 323 }
b1ce53c5 324/* REGCP_OTHER_ELEMS are pushed in any case, parentheses or no. */
92da3157 325 SSPUSHINT(maxopenparen);
b93070ed
DM
326 SSPUSHINT(rex->lastparen);
327 SSPUSHINT(rex->lastcloseparen);
e0fa7e2b 328 SSPUSHUV(SAVEt_REGCONTEXT | elems_shifted); /* Magic cookie. */
41123dfd 329
a0d0e21e
LW
330 return retval;
331}
332
c277df42 333/* These are needed since we do not localize EVAL nodes: */
ab3bbdeb
YO
334#define REGCP_SET(cp) \
335 DEBUG_STATE_r( \
147e3846
KW
336 Perl_re_exec_indentf( aTHX_ \
337 "Setting an EVAL scope, savestack=%" IVdf ",\n", \
cb41e5d6
YO
338 depth, (IV)PL_savestack_ix \
339 ) \
340 ); \
ab3bbdeb 341 cp = PL_savestack_ix
c3464db5 342
ab3bbdeb 343#define REGCP_UNWIND(cp) \
e4f74956 344 DEBUG_STATE_r( \
cb41e5d6 345 if (cp != PL_savestack_ix) \
147e3846
KW
346 Perl_re_exec_indentf( aTHX_ \
347 "Clearing an EVAL scope, savestack=%" \
348 IVdf "..%" IVdf "\n", \
cb41e5d6
YO
349 depth, (IV)(cp), (IV)PL_savestack_ix \
350 ) \
351 ); \
ab3bbdeb 352 regcpblow(cp)
c277df42 353
a8d1f4b4
DM
354#define UNWIND_PAREN(lp, lcp) \
355 for (n = rex->lastparen; n > lp; n--) \
356 rex->offs[n].end = -1; \
357 rex->lastparen = n; \
358 rex->lastcloseparen = lcp;
359
360
f067efbf 361STATIC void
21553840 362S_regcppop(pTHX_ regexp *rex, U32 *maxopenparen_p _pDEPTH)
a0d0e21e 363{
e0fa7e2b 364 UV i;
87c0511b 365 U32 paren;
a3621e74
YO
366 GET_RE_DEBUG_FLAGS_DECL;
367
7918f24d
NC
368 PERL_ARGS_ASSERT_REGCPPOP;
369
b1ce53c5 370 /* Pop REGCP_OTHER_ELEMS before the parentheses loop starts. */
c6bf6a65 371 i = SSPOPUV;
e0fa7e2b
NC
372 assert((i & SAVE_MASK) == SAVEt_REGCONTEXT); /* Check that the magic cookie is there. */
373 i >>= SAVE_TIGHT_SHIFT; /* Parentheses elements to pop. */
b93070ed
DM
374 rex->lastcloseparen = SSPOPINT;
375 rex->lastparen = SSPOPINT;
92da3157 376 *maxopenparen_p = SSPOPINT;
b1ce53c5 377
620d5b66 378 i -= REGCP_OTHER_ELEMS;
b1ce53c5 379 /* Now restore the parentheses context. */
495f47a5
DM
380 DEBUG_BUFFERS_r(
381 if (i || rex->lastparen + 1 <= rex->nparens)
2b1a3689 382 Perl_re_exec_indentf( aTHX_
147e3846 383 "rex=0x%" UVxf " offs=0x%" UVxf ": restoring capture indices to:\n",
2b1a3689
YO
384 depth,
385 PTR2UV(rex),
495f47a5
DM
386 PTR2UV(rex->offs)
387 );
388 );
92da3157 389 paren = *maxopenparen_p;
620d5b66 390 for ( ; i > 0; i -= REGCP_PAREN_ELEMS) {
ea3daa5d 391 SSize_t tmps;
1ca2007e 392 rex->offs[paren].start_tmp = SSPOPINT;
99a90e59
FC
393 rex->offs[paren].start = SSPOPIV;
394 tmps = SSPOPIV;
b93070ed
DM
395 if (paren <= rex->lastparen)
396 rex->offs[paren].end = tmps;
2b1a3689 397 DEBUG_BUFFERS_r( Perl_re_exec_indentf( aTHX_
147e3846 398 " \\%" UVuf ": %" IVdf "(%" IVdf ")..%" IVdf "%s\n",
2b1a3689
YO
399 depth,
400 (UV)paren,
495f47a5
DM
401 (IV)rex->offs[paren].start,
402 (IV)rex->offs[paren].start_tmp,
403 (IV)rex->offs[paren].end,
404 (paren > rex->lastparen ? "(skipped)" : ""));
c277df42 405 );
87c0511b 406 paren--;
a0d0e21e 407 }
daf18116 408#if 1
dafc8851
JH
409 /* It would seem that the similar code in regtry()
410 * already takes care of this, and in fact it is in
411 * a better location to since this code can #if 0-ed out
412 * but the code in regtry() is needed or otherwise tests
413 * requiring null fields (pat.t#187 and split.t#{13,14}
daf18116
JH
414 * (as of patchlevel 7877) will fail. Then again,
415 * this code seems to be necessary or otherwise
225593e1
DM
416 * this erroneously leaves $1 defined: "1" =~ /^(?:(\d)x)?\d$/
417 * --jhi updated by dapm */
b93070ed 418 for (i = rex->lastparen + 1; i <= rex->nparens; i++) {
92da3157 419 if (i > *maxopenparen_p)
b93070ed
DM
420 rex->offs[i].start = -1;
421 rex->offs[i].end = -1;
2b1a3689 422 DEBUG_BUFFERS_r( Perl_re_exec_indentf( aTHX_
147e3846 423 " \\%" UVuf ": %s ..-1 undeffing\n",
2b1a3689
YO
424 depth,
425 (UV)i,
92da3157 426 (i > *maxopenparen_p) ? "-1" : " "
495f47a5 427 ));
a0d0e21e 428 }
dafc8851 429#endif
a0d0e21e
LW
430}
431
74088413
DM
432/* restore the parens and associated vars at savestack position ix,
433 * but without popping the stack */
434
435STATIC void
21553840 436S_regcp_restore(pTHX_ regexp *rex, I32 ix, U32 *maxopenparen_p _pDEPTH)
74088413
DM
437{
438 I32 tmpix = PL_savestack_ix;
85882954
YO
439 PERL_ARGS_ASSERT_REGCP_RESTORE;
440
74088413 441 PL_savestack_ix = ix;
21553840 442 regcppop(rex, maxopenparen_p);
74088413
DM
443 PL_savestack_ix = tmpix;
444}
445
02db2b7b 446#define regcpblow(cp) LEAVE_SCOPE(cp) /* Ignores regcppush()ed data. */
a0d0e21e 447
9637d2a5
CB
448#ifndef PERL_IN_XSUB_RE
449
24e16d7b
KW
450bool
451Perl_isFOO_lc(pTHX_ const U8 classnum, const U8 character)
31c7f561
KW
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,
7aee35ff
KW
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. */
31c7f561
KW
465
466 switch ((_char_class_number) classnum) {
15861f94 467 case _CC_ENUM_ALPHANUMERIC: return isALPHANUMERIC_LC(character);
31c7f561 468 case _CC_ENUM_ALPHA: return isALPHA_LC(character);
e8d596e0
KW
469 case _CC_ENUM_ASCII: return isASCII_LC(character);
470 case _CC_ENUM_BLANK: return isBLANK_LC(character);
cee69f79 471 case _CC_ENUM_CASED: return isLOWER_LC(character)
b0d691b2 472 || isUPPER_LC(character);
e8d596e0 473 case _CC_ENUM_CNTRL: return isCNTRL_LC(character);
31c7f561
KW
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_PUNCT: return isPUNCT_LC(character);
e8d596e0 479 case _CC_ENUM_SPACE: return isSPACE_LC(character);
31c7f561
KW
480 case _CC_ENUM_UPPER: return isUPPER_LC(character);
481 case _CC_ENUM_WORDCHAR: return isWORDCHAR_LC(character);
31c7f561 482 case _CC_ENUM_XDIGIT: return isXDIGIT_LC(character);
31c7f561
KW
483 default: /* VERTSPACE should never occur in locales */
484 Perl_croak(aTHX_ "panic: isFOO_lc() has an unexpected character class '%d'", classnum);
485 }
486
e5964223 487 NOT_REACHED; /* NOTREACHED */
31c7f561
KW
488 return FALSE;
489}
490
9637d2a5
CB
491#endif
492
3018b823
KW
493STATIC bool
494S_isFOO_utf8_lc(pTHX_ const U8 classnum, const U8* character)
495{
496 /* Returns a boolean as to whether or not the (well-formed) UTF-8-encoded
497 * 'character' is a member of the Posix character class given by 'classnum'
498 * that should be equivalent to a value in the typedef
499 * '_char_class_number'.
500 *
501 * This just calls isFOO_lc on the code point for the character if it is in
2f306ab9 502 * the range 0-255. Outside that range, all characters use Unicode
3018b823
KW
503 * rules, ignoring any locale. So use the Unicode function if this class
504 * requires a swash, and use the Unicode macro otherwise. */
505
506 PERL_ARGS_ASSERT_ISFOO_UTF8_LC;
507
508 if (UTF8_IS_INVARIANT(*character)) {
509 return isFOO_lc(classnum, *character);
510 }
511 else if (UTF8_IS_DOWNGRADEABLE_START(*character)) {
512 return isFOO_lc(classnum,
a62b247b 513 EIGHT_BIT_UTF8_TO_NATIVE(*character, *(character + 1)));
3018b823
KW
514 }
515
613abc6d
KW
516 _CHECK_AND_OUTPUT_WIDE_LOCALE_UTF8_MSG(character, character + UTF8SKIP(character));
517
3018b823
KW
518 if (classnum < _FIRST_NON_SWASH_CC) {
519
520 /* Initialize the swash unless done already */
521 if (! PL_utf8_swash_ptrs[classnum]) {
522 U8 flags = _CORE_SWASH_INIT_ACCEPT_INVLIST;
2a16ac92
KW
523 PL_utf8_swash_ptrs[classnum] =
524 _core_swash_init("utf8",
525 "",
526 &PL_sv_undef, 1, 0,
527 PL_XPosix_ptrs[classnum], &flags);
3018b823
KW
528 }
529
92a2046b
KW
530 return cBOOL(swash_fetch(PL_utf8_swash_ptrs[classnum], (U8 *)
531 character,
532 TRUE /* is UTF */ ));
3018b823
KW
533 }
534
535 switch ((_char_class_number) classnum) {
779cf272 536 case _CC_ENUM_SPACE: return is_XPERLSPACE_high(character);
3018b823
KW
537 case _CC_ENUM_BLANK: return is_HORIZWS_high(character);
538 case _CC_ENUM_XDIGIT: return is_XDIGIT_high(character);
539 case _CC_ENUM_VERTSPACE: return is_VERTWS_high(character);
e1ee3960 540 default: break;
3018b823
KW
541 }
542
e1ee3960 543 return FALSE; /* Things like CNTRL are always below 256 */
3018b823
KW
544}
545
a687059c 546/*
e50aee73 547 * pregexec and friends
a687059c
LW
548 */
549
76234dfb 550#ifndef PERL_IN_XSUB_RE
a687059c 551/*
c277df42 552 - pregexec - match a regexp against a string
a687059c 553 */
c277df42 554I32
5aaab254 555Perl_pregexec(pTHX_ REGEXP * const prog, char* stringarg, char *strend,
ea3daa5d 556 char *strbeg, SSize_t minend, SV *screamer, U32 nosave)
8fd1a950
DM
557/* stringarg: the point in the string at which to begin matching */
558/* strend: pointer to null at end of string */
559/* strbeg: real beginning of string */
560/* minend: end of match must be >= minend bytes after stringarg. */
561/* screamer: SV being matched: only used for utf8 flag, pos() etc; string
562 * itself is accessed via the pointers above */
563/* nosave: For optimizations. */
c277df42 564{
7918f24d
NC
565 PERL_ARGS_ASSERT_PREGEXEC;
566
c277df42 567 return
9041c2e3 568 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
c277df42
IZ
569 nosave ? 0 : REXEC_COPY_STR);
570}
76234dfb 571#endif
22e551b9 572
cad2e5aa 573
6eb5f6b9 574
1a4edc3c
DM
575/* re_intuit_start():
576 *
577 * Based on some optimiser hints, try to find the earliest position in the
578 * string where the regex could match.
579 *
580 * rx: the regex to match against
581 * sv: the SV being matched: only used for utf8 flag; the string
582 * itself is accessed via the pointers below. Note that on
583 * something like an overloaded SV, SvPOK(sv) may be false
584 * and the string pointers may point to something unrelated to
585 * the SV itself.
586 * strbeg: real beginning of string
587 * strpos: the point in the string at which to begin matching
588 * strend: pointer to the byte following the last char of the string
589 * flags currently unused; set to 0
590 * data: currently unused; set to NULL
591 *
592 * The basic idea of re_intuit_start() is to use some known information
593 * about the pattern, namely:
594 *
595 * a) the longest known anchored substring (i.e. one that's at a
596 * constant offset from the beginning of the pattern; but not
597 * necessarily at a fixed offset from the beginning of the
598 * string);
599 * b) the longest floating substring (i.e. one that's not at a constant
600 * offset from the beginning of the pattern);
601 * c) Whether the pattern is anchored to the string; either
602 * an absolute anchor: /^../, or anchored to \n: /^.../m,
603 * or anchored to pos(): /\G/;
604 * d) A start class: a real or synthetic character class which
605 * represents which characters are legal at the start of the pattern;
606 *
607 * to either quickly reject the match, or to find the earliest position
608 * within the string at which the pattern might match, thus avoiding
609 * running the full NFA engine at those earlier locations, only to
610 * eventually fail and retry further along.
611 *
612 * Returns NULL if the pattern can't match, or returns the address within
613 * the string which is the earliest place the match could occur.
614 *
615 * The longest of the anchored and floating substrings is called 'check'
616 * and is checked first. The other is called 'other' and is checked
617 * second. The 'other' substring may not be present. For example,
618 *
619 * /(abc|xyz)ABC\d{0,3}DEFG/
620 *
621 * will have
622 *
623 * check substr (float) = "DEFG", offset 6..9 chars
624 * other substr (anchored) = "ABC", offset 3..3 chars
625 * stclass = [ax]
626 *
627 * Be aware that during the course of this function, sometimes 'anchored'
628 * refers to a substring being anchored relative to the start of the
629 * pattern, and sometimes to the pattern itself being anchored relative to
630 * the string. For example:
631 *
632 * /\dabc/: "abc" is anchored to the pattern;
633 * /^\dabc/: "abc" is anchored to the pattern and the string;
634 * /\d+abc/: "abc" is anchored to neither the pattern nor the string;
635 * /^\d+abc/: "abc" is anchored to neither the pattern nor the string,
636 * but the pattern is anchored to the string.
52a21eb3
DM
637 */
638
cad2e5aa 639char *
52a21eb3
DM
640Perl_re_intuit_start(pTHX_
641 REGEXP * const rx,
642 SV *sv,
643 const char * const strbeg,
644 char *strpos,
645 char *strend,
646 const U32 flags,
647 re_scream_pos_data *data)
cad2e5aa 648{
8d919b0a 649 struct regexp *const prog = ReANY(rx);
6b071d16 650 SSize_t start_shift = prog->check_offset_min;
cad2e5aa 651 /* Should be nonnegative! */
ea3daa5d 652 SSize_t end_shift = 0;
0fc004dd
DM
653 /* current lowest pos in string where the regex can start matching */
654 char *rx_origin = strpos;
eb578fdb 655 SV *check;
f2ed9b32 656 const bool utf8_target = (sv && SvUTF8(sv)) ? 1 : 0; /* if no sv we have to assume bytes */
6480a6c4 657 U8 other_ix = 1 - prog->substrs->check_ix;
6ad5ffb3 658 bool ml_anch = 0;
8f4bf5fc 659 char *other_last = strpos;/* latest pos 'other' substr already checked to */
bd61b366 660 char *check_at = NULL; /* check substr found at this pos */
bbe252da 661 const I32 multiline = prog->extflags & RXf_PMf_MULTILINE;
f8fc2ecf 662 RXi_GET_DECL(prog,progi);
02d5137b
DM
663 regmatch_info reginfo_buf; /* create some info to pass to find_byclass */
664 regmatch_info *const reginfo = &reginfo_buf;
a3621e74
YO
665 GET_RE_DEBUG_FLAGS_DECL;
666
7918f24d 667 PERL_ARGS_ASSERT_RE_INTUIT_START;
c33e64f0
FC
668 PERL_UNUSED_ARG(flags);
669 PERL_UNUSED_ARG(data);
7918f24d 670
6ad9a8ab 671 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
1dc475d0
DM
672 "Intuit: trying to determine minimum start position...\n"));
673
fb9bbddb 674 /* for now, assume that all substr offsets are positive. If at some point
f67a5002 675 * in the future someone wants to do clever things with lookbehind and
fb9bbddb
DM
676 * -ve offsets, they'll need to fix up any code in this function
677 * which uses these offsets. See the thread beginning
678 * <20140113145929.GF27210@iabyn.com>
679 */
680 assert(prog->substrs->data[0].min_offset >= 0);
681 assert(prog->substrs->data[0].max_offset >= 0);
682 assert(prog->substrs->data[1].min_offset >= 0);
683 assert(prog->substrs->data[1].max_offset >= 0);
684 assert(prog->substrs->data[2].min_offset >= 0);
685 assert(prog->substrs->data[2].max_offset >= 0);
686
f7022b5a 687 /* for now, assume that if both present, that the floating substring
83f2232d 688 * doesn't start before the anchored substring.
f7022b5a
DM
689 * If you break this assumption (e.g. doing better optimisations
690 * with lookahead/behind), then you'll need to audit the code in this
691 * function carefully first
692 */
693 assert(
694 ! ( (prog->anchored_utf8 || prog->anchored_substr)
695 && (prog->float_utf8 || prog->float_substr))
696 || (prog->float_min_offset >= prog->anchored_offset));
697
1a4edc3c
DM
698 /* byte rather than char calculation for efficiency. It fails
699 * to quickly reject some cases that can't match, but will reject
700 * them later after doing full char arithmetic */
c344f387 701 if (prog->minlen > strend - strpos) {
6ad9a8ab 702 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
1dc475d0 703 " String too short...\n"));
cad2e5aa 704 goto fail;
2c2d71f5 705 }
d8da0584 706
ab4e48c1 707 RX_MATCH_UTF8_set(rx,utf8_target);
6c3fea77 708 reginfo->is_utf8_target = cBOOL(utf8_target);
bf2039a9 709 reginfo->info_aux = NULL;
9d9163fb 710 reginfo->strbeg = strbeg;
220db18a 711 reginfo->strend = strend;
aed7b151 712 reginfo->is_utf8_pat = cBOOL(RX_UTF8(rx));
02d5137b 713 reginfo->intuit = 1;
1cb48e53
DM
714 /* not actually used within intuit, but zero for safety anyway */
715 reginfo->poscache_maxiter = 0;
02d5137b 716
f2ed9b32 717 if (utf8_target) {
2814f4b3
HS
718 if ((!prog->anchored_utf8 && prog->anchored_substr)
719 || (!prog->float_utf8 && prog->float_substr))
33b8afdf
JH
720 to_utf8_substr(prog);
721 check = prog->check_utf8;
722 } else {
7e0d5ad7
KW
723 if (!prog->check_substr && prog->check_utf8) {
724 if (! to_byte_substr(prog)) {
6b54ddc5 725 NON_UTF8_TARGET_BUT_UTF8_REQUIRED(fail);
7e0d5ad7
KW
726 }
727 }
33b8afdf
JH
728 check = prog->check_substr;
729 }
274cd312 730
1dc475d0
DM
731 /* dump the various substring data */
732 DEBUG_OPTIMISE_MORE_r({
733 int i;
734 for (i=0; i<=2; i++) {
735 SV *sv = (utf8_target ? prog->substrs->data[i].utf8_substr
736 : prog->substrs->data[i].substr);
737 if (!sv)
738 continue;
739
6ad9a8ab 740 Perl_re_printf( aTHX_
147e3846
KW
741 " substrs[%d]: min=%" IVdf " max=%" IVdf " end shift=%" IVdf
742 " useful=%" IVdf " utf8=%d [%s]\n",
1dc475d0
DM
743 i,
744 (IV)prog->substrs->data[i].min_offset,
745 (IV)prog->substrs->data[i].max_offset,
746 (IV)prog->substrs->data[i].end_shift,
747 BmUSEFUL(sv),
748 utf8_target ? 1 : 0,
749 SvPEEK(sv));
750 }
751 });
752
8e1490ee 753 if (prog->intflags & PREGf_ANCH) { /* Match at \G, beg-of-str or after \n */
9fc7410e
DM
754
755 /* ml_anch: check after \n?
756 *
0fa70a06 757 * A note about PREGf_IMPLICIT: on an un-anchored pattern beginning
9fc7410e
DM
758 * with /.*.../, these flags will have been added by the
759 * compiler:
760 * /.*abc/, /.*abc/m: PREGf_IMPLICIT | PREGf_ANCH_MBOL
761 * /.*abc/s: PREGf_IMPLICIT | PREGf_ANCH_SBOL
762 */
7d2d37f5
DM
763 ml_anch = (prog->intflags & PREGf_ANCH_MBOL)
764 && !(prog->intflags & PREGf_IMPLICIT);
cad2e5aa 765
343c8a29 766 if (!ml_anch && !(prog->intflags & PREGf_IMPLICIT)) {
c889ccc8
DM
767 /* we are only allowed to match at BOS or \G */
768
57fcbfa7 769 /* trivially reject if there's a BOS anchor and we're not at BOS.
7bb3b9eb
DM
770 *
771 * Note that we don't try to do a similar quick reject for
772 * \G, since generally the caller will have calculated strpos
773 * based on pos() and gofs, so the string is already correctly
774 * anchored by definition; and handling the exceptions would
775 * be too fiddly (e.g. REXEC_IGNOREPOS).
57fcbfa7 776 */
7bb3b9eb 777 if ( strpos != strbeg
d3d47aac 778 && (prog->intflags & PREGf_ANCH_SBOL))
c889ccc8 779 {
6ad9a8ab 780 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
1dc475d0 781 " Not at start...\n"));
c889ccc8
DM
782 goto fail;
783 }
784
a5d12a4b
DM
785 /* in the presence of an anchor, the anchored (relative to the
786 * start of the regex) substr must also be anchored relative
66b7ec5c
DM
787 * to strpos. So quickly reject if substr isn't found there.
788 * This works for \G too, because the caller will already have
789 * subtracted gofs from pos, and gofs is the offset from the
790 * \G to the start of the regex. For example, in /.abc\Gdef/,
791 * where substr="abcdef", pos()=3, gofs=4, offset_min=1:
792 * caller will have set strpos=pos()-4; we look for the substr
793 * at position pos()-4+1, which lines up with the "a" */
a5d12a4b 794
33c28ab2 795 if (prog->check_offset_min == prog->check_offset_max) {
c889ccc8 796 /* Substring at constant offset from beg-of-str... */
d307bf57 797 SSize_t slen = SvCUR(check);
343c8a29 798 char *s = HOP3c(strpos, prog->check_offset_min, strend);
1de06328 799
6ad9a8ab 800 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
147e3846 801 " Looking for check substr at fixed offset %" IVdf "...\n",
1dc475d0
DM
802 (IV)prog->check_offset_min));
803
7742aa66
DM
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')))
c889ccc8 813 {
6ad9a8ab 814 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
1dc475d0 815 " String too long...\n"));
c889ccc8
DM
816 goto fail_finish;
817 }
818 /* Now should match s[0..slen-2] */
819 slen--;
c889ccc8 820 }
26fb2318
TC
821 if (slen && (strend - s < slen
822 || *SvPVX_const(check) != *s
823 || (slen > 1 && (memNE(SvPVX_const(check), s, slen)))))
d307bf57 824 {
6ad9a8ab 825 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
1dc475d0 826 " String not equal...\n"));
d307bf57
DM
827 goto fail_finish;
828 }
c889ccc8
DM
829
830 check_at = s;
831 goto success_at_start;
cad2e5aa 832 }
cad2e5aa 833 }
cad2e5aa 834 }
0fc004dd 835
c0e0ec46 836 end_shift = prog->check_end_shift;
cad2e5aa 837
19188028 838#ifdef DEBUGGING /* 7/99: reports of failure (with the older version) */
0033605d 839 if (end_shift < 0)
147e3846 840 Perl_croak(aTHX_ "panic: end_shift: %" IVdf " pattern:\n%s\n ",
220fc49f 841 (IV)end_shift, RX_PRECOMP(prog));
2c2d71f5
JH
842#endif
843
2c2d71f5 844 restart:
1de06328 845
66b7ec5c
DM
846 /* This is the (re)entry point of the main loop in this function.
847 * The goal of this loop is to:
848 * 1) find the "check" substring in the region rx_origin..strend
849 * (adjusted by start_shift / end_shift). If not found, reject
850 * immediately.
851 * 2) If it exists, look for the "other" substr too if defined; for
852 * example, if the check substr maps to the anchored substr, then
853 * check the floating substr, and vice-versa. If not found, go
854 * back to (1) with rx_origin suitably incremented.
855 * 3) If we find an rx_origin position that doesn't contradict
856 * either of the substrings, then check the possible additional
857 * constraints on rx_origin of /^.../m or a known start class.
858 * If these fail, then depending on which constraints fail, jump
859 * back to here, or to various other re-entry points further along
860 * that skip some of the first steps.
861 * 4) If we pass all those tests, update the BmUSEFUL() count on the
862 * substring. If the start position was determined to be at the
863 * beginning of the string - so, not rejected, but not optimised,
864 * since we have to run regmatch from position 0 - decrement the
865 * BmUSEFUL() count. Otherwise increment it.
866 */
867
1a4edc3c
DM
868
869 /* first, look for the 'check' substring */
870
1de06328 871 {
c33e64f0
FC
872 U8* start_point;
873 U8* end_point;
c889ccc8 874
c889ccc8 875 DEBUG_OPTIMISE_MORE_r({
6ad9a8ab 876 Perl_re_printf( aTHX_
147e3846
KW
877 " At restart: rx_origin=%" IVdf " Check offset min: %" IVdf
878 " Start shift: %" IVdf " End shift %" IVdf
879 " Real end Shift: %" IVdf "\n",
675e93ee 880 (IV)(rx_origin - strbeg),
c889ccc8 881 (IV)prog->check_offset_min,
12fbc530
DM
882 (IV)start_shift,
883 (IV)end_shift,
c889ccc8
DM
884 (IV)prog->check_end_shift);
885 });
1de06328 886
33c28ab2
DM
887 end_point = HOP3(strend, -end_shift, strbeg);
888 start_point = HOPMAYBE3(rx_origin, start_shift, end_point);
889 if (!start_point)
890 goto fail_finish;
c889ccc8 891
557f47af 892
e0362b86 893 /* If the regex is absolutely anchored to either the start of the
d3d47aac 894 * string (SBOL) or to pos() (ANCH_GPOS), then
e0362b86
DM
895 * check_offset_max represents an upper bound on the string where
896 * the substr could start. For the ANCH_GPOS case, we assume that
897 * the caller of intuit will have already set strpos to
898 * pos()-gofs, so in this case strpos + offset_max will still be
899 * an upper bound on the substr.
900 */
c19c836a
DM
901 if (!ml_anch
902 && prog->intflags & PREGf_ANCH
e0362b86 903 && prog->check_offset_max != SSize_t_MAX)
c19c836a 904 {
1a08ba3a 905 SSize_t len = SvCUR(check) - !!SvTAIL(check);
e0362b86
DM
906 const char * const anchor =
907 (prog->intflags & PREGf_ANCH_GPOS ? strpos : strbeg);
908
909 /* do a bytes rather than chars comparison. It's conservative;
910 * so it skips doing the HOP if the result can't possibly end
911 * up earlier than the old value of end_point.
912 */
913 if ((char*)end_point - anchor > prog->check_offset_max) {
914 end_point = HOP3lim((U8*)anchor,
915 prog->check_offset_max,
916 end_point -len)
917 + len;
918 }
d6ef1678
DM
919 }
920
ae5d4331 921 check_at = fbm_instr( start_point, end_point,
7fba1cd6 922 check, multiline ? FBMrf_MULTILINE : 0);
c889ccc8 923
6ad9a8ab 924 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
147e3846 925 " doing 'check' fbm scan, [%" IVdf "..%" IVdf "] gave %" IVdf "\n",
675e93ee
DM
926 (IV)((char*)start_point - strbeg),
927 (IV)((char*)end_point - strbeg),
928 (IV)(check_at ? check_at - strbeg : -1)
929 ));
930
8fd34720
DM
931 /* Update the count-of-usability, remove useless subpatterns,
932 unshift s. */
933
934 DEBUG_EXECUTE_r({
935 RE_PV_QUOTED_DECL(quoted, utf8_target, PERL_DEBUG_PAD_ZERO(0),
936 SvPVX_const(check), RE_SV_DUMPLEN(check), 30);
6ad9a8ab 937 Perl_re_printf( aTHX_ " %s %s substr %s%s%s",
8fd34720
DM
938 (check_at ? "Found" : "Did not find"),
939 (check == (utf8_target ? prog->anchored_utf8 : prog->anchored_substr)
940 ? "anchored" : "floating"),
941 quoted,
942 RE_SV_TAIL(check),
943 (check_at ? " at offset " : "...\n") );
944 });
2c2d71f5 945
8fd34720
DM
946 if (!check_at)
947 goto fail_finish;
8fd34720
DM
948 /* set rx_origin to the minimum position where the regex could start
949 * matching, given the constraint of the just-matched check substring.
950 * But don't set it lower than previously.
951 */
fdc003fd 952
8fd34720
DM
953 if (check_at - rx_origin > prog->check_offset_max)
954 rx_origin = HOP3c(check_at, -prog->check_offset_max, rx_origin);
675e93ee 955 /* Finish the diagnostic message */
6ad9a8ab 956 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
147e3846 957 "%ld (rx_origin now %" IVdf ")...\n",
675e93ee
DM
958 (long)(check_at - strbeg),
959 (IV)(rx_origin - strbeg)
960 ));
8fd34720 961 }
fdc003fd
DM
962
963
1a4edc3c 964 /* now look for the 'other' substring if defined */
2c2d71f5 965
6480a6c4
DM
966 if (utf8_target ? prog->substrs->data[other_ix].utf8_substr
967 : prog->substrs->data[other_ix].substr)
1de06328 968 {
30944b6d 969 /* Take into account the "other" substring. */
6c3343a6
DM
970 char *last, *last1;
971 char *s;
972 SV* must;
973 struct reg_substr_datum *other;
974
975 do_other_substr:
976 other = &prog->substrs->data[other_ix];
977
978 /* if "other" is anchored:
979 * we've previously found a floating substr starting at check_at.
980 * This means that the regex origin must lie somewhere
981 * between min (rx_origin): HOP3(check_at, -check_offset_max)
982 * and max: HOP3(check_at, -check_offset_min)
983 * (except that min will be >= strpos)
984 * So the fixed substr must lie somewhere between
985 * HOP3(min, anchored_offset)
986 * HOP3(max, anchored_offset) + SvCUR(substr)
987 */
988
989 /* if "other" is floating
990 * Calculate last1, the absolute latest point where the
991 * floating substr could start in the string, ignoring any
992 * constraints from the earlier fixed match. It is calculated
993 * as follows:
994 *
995 * strend - prog->minlen (in chars) is the absolute latest
996 * position within the string where the origin of the regex
997 * could appear. The latest start point for the floating
998 * substr is float_min_offset(*) on from the start of the
999 * regex. last1 simply combines thee two offsets.
1000 *
1001 * (*) You might think the latest start point should be
1002 * float_max_offset from the regex origin, and technically
1003 * you'd be correct. However, consider
1004 * /a\d{2,4}bcd\w/
1005 * Here, float min, max are 3,5 and minlen is 7.
1006 * This can match either
1007 * /a\d\dbcd\w/
1008 * /a\d\d\dbcd\w/
1009 * /a\d\d\d\dbcd\w/
1010 * In the first case, the regex matches minlen chars; in the
1011 * second, minlen+1, in the third, minlen+2.
1012 * In the first case, the floating offset is 3 (which equals
1013 * float_min), in the second, 4, and in the third, 5 (which
1014 * equals float_max). In all cases, the floating string bcd
1015 * can never start more than 4 chars from the end of the
1016 * string, which equals minlen - float_min. As the substring
1017 * starts to match more than float_min from the start of the
1018 * regex, it makes the regex match more than minlen chars,
1019 * and the two cancel each other out. So we can always use
1020 * float_min - minlen, rather than float_max - minlen for the
1021 * latest position in the string.
1022 *
1023 * Note that -minlen + float_min_offset is equivalent (AFAIKT)
1024 * to CHR_SVLEN(must) - !!SvTAIL(must) + prog->float_end_shift
1025 */
1026
e7a14a9c 1027 assert(prog->minlen >= other->min_offset);
6c3343a6
DM
1028 last1 = HOP3c(strend,
1029 other->min_offset - prog->minlen, strbeg);
1030
4d006249 1031 if (other_ix) {/* i.e. if (other-is-float) */
6c3343a6
DM
1032 /* last is the latest point where the floating substr could
1033 * start, *given* any constraints from the earlier fixed
1034 * match. This constraint is that the floating string starts
1035 * <= float_max_offset chars from the regex origin (rx_origin).
1036 * If this value is less than last1, use it instead.
eb3831ce 1037 */
6c3343a6
DM
1038 assert(rx_origin <= last1);
1039 last =
1040 /* this condition handles the offset==infinity case, and
1041 * is a short-cut otherwise. Although it's comparing a
1042 * byte offset to a char length, it does so in a safe way,
1043 * since 1 char always occupies 1 or more bytes,
1044 * so if a string range is (last1 - rx_origin) bytes,
1045 * it will be less than or equal to (last1 - rx_origin)
1046 * chars; meaning it errs towards doing the accurate HOP3
1047 * rather than just using last1 as a short-cut */
1048 (last1 - rx_origin) < other->max_offset
1049 ? last1
1050 : (char*)HOP3lim(rx_origin, other->max_offset, last1);
1051 }
1052 else {
1053 assert(strpos + start_shift <= check_at);
1054 last = HOP4c(check_at, other->min_offset - start_shift,
1055 strbeg, strend);
1056 }
ead917d0 1057
6c3343a6
DM
1058 s = HOP3c(rx_origin, other->min_offset, strend);
1059 if (s < other_last) /* These positions already checked */
1060 s = other_last;
1061
1062 must = utf8_target ? other->utf8_substr : other->substr;
1063 assert(SvPOK(must));
675e93ee
DM
1064 {
1065 char *from = s;
1066 char *to = last + SvCUR(must) - (SvTAIL(must)!=0);
1067
71a9d105
DM
1068 if (to > strend)
1069 to = strend;
88203927
DM
1070 if (from > to) {
1071 s = NULL;
6ad9a8ab 1072 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
147e3846 1073 " skipping 'other' fbm scan: %" IVdf " > %" IVdf "\n",
88203927
DM
1074 (IV)(from - strbeg),
1075 (IV)(to - strbeg)
1076 ));
1077 }
1078 else {
1079 s = fbm_instr(
1080 (unsigned char*)from,
1081 (unsigned char*)to,
1082 must,
1083 multiline ? FBMrf_MULTILINE : 0
1084 );
6ad9a8ab 1085 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
147e3846 1086 " doing 'other' fbm scan, [%" IVdf "..%" IVdf "] gave %" IVdf "\n",
88203927
DM
1087 (IV)(from - strbeg),
1088 (IV)(to - strbeg),
1089 (IV)(s ? s - strbeg : -1)
1090 ));
1091 }
675e93ee
DM
1092 }
1093
6c3343a6
DM
1094 DEBUG_EXECUTE_r({
1095 RE_PV_QUOTED_DECL(quoted, utf8_target, PERL_DEBUG_PAD_ZERO(0),
1096 SvPVX_const(must), RE_SV_DUMPLEN(must), 30);
6ad9a8ab 1097 Perl_re_printf( aTHX_ " %s %s substr %s%s",
6c3343a6
DM
1098 s ? "Found" : "Contradicts",
1099 other_ix ? "floating" : "anchored",
1100 quoted, RE_SV_TAIL(must));
1101 });
ead917d0 1102
ead917d0 1103
6c3343a6
DM
1104 if (!s) {
1105 /* last1 is latest possible substr location. If we didn't
1106 * find it before there, we never will */
1107 if (last >= last1) {
6ad9a8ab 1108 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
675e93ee 1109 "; giving up...\n"));
6c3343a6 1110 goto fail_finish;
ead917d0
DM
1111 }
1112
6c3343a6
DM
1113 /* try to find the check substr again at a later
1114 * position. Maybe next time we'll find the "other" substr
1115 * in range too */
6c3343a6
DM
1116 other_last = HOP3c(last, 1, strend) /* highest failure */;
1117 rx_origin =
4d006249 1118 other_ix /* i.e. if other-is-float */
6c3343a6
DM
1119 ? HOP3c(rx_origin, 1, strend)
1120 : HOP4c(last, 1 - other->min_offset, strbeg, strend);
6ad9a8ab 1121 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
147e3846 1122 "; about to retry %s at offset %ld (rx_origin now %" IVdf ")...\n",
675e93ee
DM
1123 (other_ix ? "floating" : "anchored"),
1124 (long)(HOP3c(check_at, 1, strend) - strbeg),
1125 (IV)(rx_origin - strbeg)
1126 ));
6c3343a6
DM
1127 goto restart;
1128 }
1129 else {
4d006249 1130 if (other_ix) { /* if (other-is-float) */
6c3343a6
DM
1131 /* other_last is set to s, not s+1, since its possible for
1132 * a floating substr to fail first time, then succeed
1133 * second time at the same floating position; e.g.:
1134 * "-AB--AABZ" =~ /\wAB\d*Z/
1135 * The first time round, anchored and float match at
1136 * "-(AB)--AAB(Z)" then fail on the initial \w character
1137 * class. Second time round, they match at "-AB--A(AB)(Z)".
1138 */
1139 other_last = s;
ead917d0
DM
1140 }
1141 else {
6c3343a6
DM
1142 rx_origin = HOP3c(s, -other->min_offset, strbeg);
1143 other_last = HOP3c(s, 1, strend);
ead917d0 1144 }
6ad9a8ab 1145 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
147e3846 1146 " at offset %ld (rx_origin now %" IVdf ")...\n",
675e93ee
DM
1147 (long)(s - strbeg),
1148 (IV)(rx_origin - strbeg)
1149 ));
1150
6c3343a6 1151 }
cad2e5aa 1152 }
acba93e8
DM
1153 else {
1154 DEBUG_OPTIMISE_MORE_r(
6ad9a8ab 1155 Perl_re_printf( aTHX_
147e3846
KW
1156 " Check-only match: offset min:%" IVdf " max:%" IVdf
1157 " check_at:%" IVdf " rx_origin:%" IVdf " rx_origin-check_at:%" IVdf
1158 " strend:%" IVdf "\n",
acba93e8
DM
1159 (IV)prog->check_offset_min,
1160 (IV)prog->check_offset_max,
675e93ee
DM
1161 (IV)(check_at-strbeg),
1162 (IV)(rx_origin-strbeg),
1c1c599d 1163 (IV)(rx_origin-check_at),
675e93ee 1164 (IV)(strend-strbeg)
acba93e8
DM
1165 )
1166 );
1167 }
2c2d71f5 1168
acba93e8 1169 postprocess_substr_matches:
0991020e 1170
1a4edc3c 1171 /* handle the extra constraint of /^.../m if present */
e3c6feb0 1172
7d2d37f5 1173 if (ml_anch && rx_origin != strbeg && rx_origin[-1] != '\n') {
4620cb61
DM
1174 char *s;
1175
6ad9a8ab 1176 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
a62659bd 1177 " looking for /^/m anchor"));
d0880ea7
DM
1178
1179 /* we have failed the constraint of a \n before rx_origin.
2e759faa
DM
1180 * Find the next \n, if any, even if it's beyond the current
1181 * anchored and/or floating substrings. Whether we should be
1182 * scanning ahead for the next \n or the next substr is debatable.
1183 * On the one hand you'd expect rare substrings to appear less
1184 * often than \n's. On the other hand, searching for \n means
675e93ee 1185 * we're effectively flipping between check_substr and "\n" on each
2e759faa
DM
1186 * iteration as the current "rarest" string candidate, which
1187 * means for example that we'll quickly reject the whole string if
1188 * hasn't got a \n, rather than trying every substr position
1189 * first
1190 */
d0880ea7 1191
4620cb61
DM
1192 s = HOP3c(strend, - prog->minlen, strpos);
1193 if (s <= rx_origin ||
1194 ! ( rx_origin = (char *)memchr(rx_origin, '\n', s - rx_origin)))
1195 {
6ad9a8ab 1196 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
d0880ea7
DM
1197 " Did not find /%s^%s/m...\n",
1198 PL_colors[0], PL_colors[1]));
a62659bd
DM
1199 goto fail_finish;
1200 }
d0880ea7 1201
4ada1233
DM
1202 /* earliest possible origin is 1 char after the \n.
1203 * (since *rx_origin == '\n', it's safe to ++ here rather than
1204 * HOP(rx_origin, 1)) */
1205 rx_origin++;
d0880ea7 1206
f4f115de 1207 if (prog->substrs->check_ix == 0 /* check is anchored */
4ada1233 1208 || rx_origin >= HOP3c(check_at, - prog->check_offset_min, strpos))
f4f115de 1209 {
d0880ea7
DM
1210 /* Position contradicts check-string; either because
1211 * check was anchored (and thus has no wiggle room),
4ada1233 1212 * or check was float and rx_origin is above the float range */
6ad9a8ab 1213 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
675e93ee
DM
1214 " Found /%s^%s/m, about to restart lookup for check-string with rx_origin %ld...\n",
1215 PL_colors[0], PL_colors[1], (long)(rx_origin - strbeg)));
d0880ea7
DM
1216 goto restart;
1217 }
1218
1219 /* if we get here, the check substr must have been float,
2e759faa 1220 * is in range, and we may or may not have had an anchored
d0880ea7
DM
1221 * "other" substr which still contradicts */
1222 assert(prog->substrs->check_ix); /* check is float */
1223
1224 if (utf8_target ? prog->anchored_utf8 : prog->anchored_substr) {
1225 /* whoops, the anchored "other" substr exists, so we still
1226 * contradict. On the other hand, the float "check" substr
1227 * didn't contradict, so just retry the anchored "other"
1228 * substr */
6ad9a8ab 1229 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
147e3846 1230 " Found /%s^%s/m, rescanning for anchored from offset %" IVdf " (rx_origin now %" IVdf ")...\n",
d0880ea7 1231 PL_colors[0], PL_colors[1],
73e8ff00
DM
1232 (IV)(rx_origin - strbeg + prog->anchored_offset),
1233 (IV)(rx_origin - strbeg)
675e93ee 1234 ));
d0880ea7
DM
1235 goto do_other_substr;
1236 }
1237
1238 /* success: we don't contradict the found floating substring
1239 * (and there's no anchored substr). */
6ad9a8ab 1240 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
675e93ee
DM
1241 " Found /%s^%s/m with rx_origin %ld...\n",
1242 PL_colors[0], PL_colors[1], (long)(rx_origin - strbeg)));
e3c6feb0
DM
1243 }
1244 else {
6ad9a8ab 1245 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
fe4f3442 1246 " (multiline anchor test skipped)\n"));
e3c6feb0
DM
1247 }
1248
ffad1e6a 1249 success_at_start:
e3c6feb0 1250
cad2e5aa 1251
dd170ff5
DM
1252 /* if we have a starting character class, then test that extra constraint.
1253 * (trie stclasses are too expensive to use here, we are better off to
1254 * leave it to regmatch itself) */
1255
f8fc2ecf 1256 if (progi->regstclass && PL_regkind[OP(progi->regstclass)]!=TRIE) {
f8fc2ecf 1257 const U8* const str = (U8*)STRING(progi->regstclass);
0991020e 1258
2c75e362 1259 /* XXX this value could be pre-computed */
f8fc2ecf 1260 const int cl_l = (PL_regkind[OP(progi->regstclass)] == EXACT
2c75e362
DM
1261 ? (reginfo->is_utf8_pat
1262 ? utf8_distance(str + STR_LEN(progi->regstclass), str)
1263 : STR_LEN(progi->regstclass))
66e933ab 1264 : 1);
1de06328 1265 char * endpos;
fa3bb21d 1266 char *s;
000dfd2d
DM
1267 /* latest pos that a matching float substr constrains rx start to */
1268 char *rx_max_float = NULL;
1269
c75a3985
DM
1270 /* if the current rx_origin is anchored, either by satisfying an
1271 * anchored substring constraint, or a /^.../m constraint, then we
1272 * can reject the current origin if the start class isn't found
1273 * at the current position. If we have a float-only match, then
1274 * rx_origin is constrained to a range; so look for the start class
1275 * in that range. if neither, then look for the start class in the
1276 * whole rest of the string */
1277
dd170ff5
DM
1278 /* XXX DAPM it's not clear what the minlen test is for, and why
1279 * it's not used in the floating case. Nothing in the test suite
1280 * causes minlen == 0 here. See <20140313134639.GS12844@iabyn.com>.
1281 * Here are some old comments, which may or may not be correct:
1282 *
1283 * minlen == 0 is possible if regstclass is \b or \B,
1284 * and the fixed substr is ''$.
1285 * Since minlen is already taken into account, rx_origin+1 is
1286 * before strend; accidentally, minlen >= 1 guaranties no false
1287 * positives at rx_origin + 1 even for \b or \B. But (minlen? 1 :
1288 * 0) below assumes that regstclass does not come from lookahead...
1289 * If regstclass takes bytelength more than 1: If charlength==1, OK.
1290 * This leaves EXACTF-ish only, which are dealt with in
1291 * find_byclass().
1292 */
1293
7d2d37f5 1294 if (prog->anchored_substr || prog->anchored_utf8 || ml_anch)
67853908 1295 endpos = HOP3clim(rx_origin, (prog->minlen ? cl_l : 0), strend);
000dfd2d
DM
1296 else if (prog->float_substr || prog->float_utf8) {
1297 rx_max_float = HOP3c(check_at, -start_shift, strbeg);
67853908 1298 endpos = HOP3clim(rx_max_float, cl_l, strend);
000dfd2d 1299 }
1de06328
YO
1300 else
1301 endpos= strend;
1302
6ad9a8ab 1303 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
147e3846
KW
1304 " looking for class: start_shift: %" IVdf " check_at: %" IVdf
1305 " rx_origin: %" IVdf " endpos: %" IVdf "\n",
1dc475d0 1306 (IV)start_shift, (IV)(check_at - strbeg),
c43b5520 1307 (IV)(rx_origin - strbeg), (IV)(endpos - strbeg)));
d8080198 1308
c43b5520 1309 s = find_byclass(prog, progi->regstclass, rx_origin, endpos,
f9176b44 1310 reginfo);
be778b1a 1311 if (!s) {
6eb5f6b9 1312 if (endpos == strend) {
6ad9a8ab 1313 DEBUG_EXECUTE_r( Perl_re_printf( aTHX_
1dc475d0 1314 " Could not match STCLASS...\n") );
6eb5f6b9
JH
1315 goto fail;
1316 }
6ad9a8ab 1317 DEBUG_EXECUTE_r( Perl_re_printf( aTHX_
1dc475d0 1318 " This position contradicts STCLASS...\n") );
e0eb31e7
DM
1319 if ((prog->intflags & PREGf_ANCH) && !ml_anch
1320 && !(prog->intflags & PREGf_IMPLICIT))
653099ff 1321 goto fail;
9fed8d02 1322
6eb5f6b9 1323 /* Contradict one of substrings */
97136c8a
DM
1324 if (prog->anchored_substr || prog->anchored_utf8) {
1325 if (prog->substrs->check_ix == 1) { /* check is float */
1326 /* Have both, check_string is floating */
1327 assert(rx_origin + start_shift <= check_at);
1328 if (rx_origin + start_shift != check_at) {
1329 /* not at latest position float substr could match:
c75a3985
DM
1330 * Recheck anchored substring, but not floating.
1331 * The condition above is in bytes rather than
1332 * chars for efficiency. It's conservative, in
1333 * that it errs on the side of doing 'goto
88203927
DM
1334 * do_other_substr'. In this case, at worst,
1335 * an extra anchored search may get done, but in
1336 * practice the extra fbm_instr() is likely to
1337 * get skipped anyway. */
6ad9a8ab 1338 DEBUG_EXECUTE_r( Perl_re_printf( aTHX_
147e3846 1339 " about to retry anchored at offset %ld (rx_origin now %" IVdf ")...\n",
675e93ee
DM
1340 (long)(other_last - strbeg),
1341 (IV)(rx_origin - strbeg)
1342 ));
97136c8a 1343 goto do_other_substr;
3369914b 1344 }
3369914b
DM
1345 }
1346 }
97136c8a 1347 else {
9fed8d02
DM
1348 /* float-only */
1349
7d2d37f5 1350 if (ml_anch) {
c75a3985
DM
1351 /* In the presence of ml_anch, we might be able to
1352 * find another \n without breaking the current float
1353 * constraint. */
1354
1355 /* strictly speaking this should be HOP3c(..., 1, ...),
1356 * but since we goto a block of code that's going to
1357 * search for the next \n if any, its safe here */
9fed8d02 1358 rx_origin++;
6ad9a8ab 1359 DEBUG_EXECUTE_r( Perl_re_printf( aTHX_
675e93ee 1360 " about to look for /%s^%s/m starting at rx_origin %ld...\n",
9fed8d02 1361 PL_colors[0], PL_colors[1],
675e93ee 1362 (long)(rx_origin - strbeg)) );
9fed8d02 1363 goto postprocess_substr_matches;
ab60c45a 1364 }
c75a3985
DM
1365
1366 /* strictly speaking this can never be true; but might
1367 * be if we ever allow intuit without substrings */
1368 if (!(utf8_target ? prog->float_utf8 : prog->float_substr))
9fed8d02 1369 goto fail;
c75a3985 1370
000dfd2d 1371 rx_origin = rx_max_float;
9fed8d02
DM
1372 }
1373
c75a3985
DM
1374 /* at this point, any matching substrings have been
1375 * contradicted. Start again... */
1376
9fed8d02 1377 rx_origin = HOP3c(rx_origin, 1, strend);
557f47af
DM
1378
1379 /* uses bytes rather than char calculations for efficiency.
1380 * It's conservative: it errs on the side of doing 'goto restart',
1381 * where there is code that does a proper char-based test */
9fed8d02 1382 if (rx_origin + start_shift + end_shift > strend) {
6ad9a8ab 1383 DEBUG_EXECUTE_r( Perl_re_printf( aTHX_
9fed8d02
DM
1384 " Could not match STCLASS...\n") );
1385 goto fail;
1386 }
6ad9a8ab 1387 DEBUG_EXECUTE_r( Perl_re_printf( aTHX_
147e3846 1388 " about to look for %s substr starting at offset %ld (rx_origin now %" IVdf ")...\n",
9fed8d02 1389 (prog->substrs->check_ix ? "floating" : "anchored"),
675e93ee
DM
1390 (long)(rx_origin + start_shift - strbeg),
1391 (IV)(rx_origin - strbeg)
1392 ));
9fed8d02 1393 goto restart;
6eb5f6b9 1394 }
9fed8d02 1395
c75a3985
DM
1396 /* Success !!! */
1397
5f9c6575 1398 if (rx_origin != s) {
6ad9a8ab 1399 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
1dc475d0 1400 " By STCLASS: moving %ld --> %ld\n",
675e93ee 1401 (long)(rx_origin - strbeg), (long)(s - strbeg))
b7953727
JH
1402 );
1403 }
1404 else {
6ad9a8ab 1405 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
1dc475d0 1406 " Does not contradict STCLASS...\n");
b7953727
JH
1407 );
1408 }
6eb5f6b9 1409 }
ffad1e6a
DM
1410
1411 /* Decide whether using the substrings helped */
1412
1413 if (rx_origin != strpos) {
1414 /* Fixed substring is found far enough so that the match
1415 cannot start at strpos. */
1416
6ad9a8ab 1417 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_ " try at offset...\n"));
ffad1e6a
DM
1418 ++BmUSEFUL(utf8_target ? prog->check_utf8 : prog->check_substr); /* hooray/5 */
1419 }
1420 else {
70563e16
DM
1421 /* The found rx_origin position does not prohibit matching at
1422 * strpos, so calling intuit didn't gain us anything. Decrement
1423 * the BmUSEFUL() count on the check substring, and if we reach
1424 * zero, free it. */
1425 if (!(prog->intflags & PREGf_NAUGHTY)
ffad1e6a
DM
1426 && (utf8_target ? (
1427 prog->check_utf8 /* Could be deleted already */
1428 && --BmUSEFUL(prog->check_utf8) < 0
1429 && (prog->check_utf8 == prog->float_utf8)
1430 ) : (
1431 prog->check_substr /* Could be deleted already */
1432 && --BmUSEFUL(prog->check_substr) < 0
1433 && (prog->check_substr == prog->float_substr)
1434 )))
1435 {
1436 /* If flags & SOMETHING - do not do it many times on the same match */
6ad9a8ab 1437 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_ " ... Disabling check substring...\n"));
ffad1e6a
DM
1438 /* XXX Does the destruction order has to change with utf8_target? */
1439 SvREFCNT_dec(utf8_target ? prog->check_utf8 : prog->check_substr);
1440 SvREFCNT_dec(utf8_target ? prog->check_substr : prog->check_utf8);
1441 prog->check_substr = prog->check_utf8 = NULL; /* disable */
1442 prog->float_substr = prog->float_utf8 = NULL; /* clear */
1443 check = NULL; /* abort */
ffad1e6a
DM
1444 /* XXXX This is a remnant of the old implementation. It
1445 looks wasteful, since now INTUIT can use many
1446 other heuristics. */
1447 prog->extflags &= ~RXf_USE_INTUIT;
ffad1e6a
DM
1448 }
1449 }
1450
6ad9a8ab 1451 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
ffad1e6a 1452 "Intuit: %sSuccessfully guessed:%s match at offset %ld\n",
675e93ee 1453 PL_colors[4], PL_colors[5], (long)(rx_origin - strbeg)) );
ffad1e6a 1454
c765d6e0 1455 return rx_origin;
2c2d71f5
JH
1456
1457 fail_finish: /* Substring not found */
33b8afdf 1458 if (prog->check_substr || prog->check_utf8) /* could be removed already */
f2ed9b32 1459 BmUSEFUL(utf8_target ? prog->check_utf8 : prog->check_substr) += 5; /* hooray */
cad2e5aa 1460 fail:
6ad9a8ab 1461 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_ "%sMatch rejected by optimizer%s\n",
e4584336 1462 PL_colors[4], PL_colors[5]));
bd61b366 1463 return NULL;
cad2e5aa 1464}
9661b544 1465
70563e16 1466
a0a388a1 1467#define DECL_TRIE_TYPE(scan) \
e7fd4aa1 1468 const enum { trie_plain, trie_utf8, trie_utf8_fold, trie_latin_utf8_fold, \
a4525e78
KW
1469 trie_utf8_exactfa_fold, trie_latin_utf8_exactfa_fold, \
1470 trie_utf8l, trie_flu8 } \
e7fd4aa1
KW
1471 trie_type = ((scan->flags == EXACT) \
1472 ? (utf8_target ? trie_utf8 : trie_plain) \
a4525e78
KW
1473 : (scan->flags == EXACTL) \
1474 ? (utf8_target ? trie_utf8l : trie_plain) \
1475 : (scan->flags == EXACTFA) \
1476 ? (utf8_target \
1477 ? trie_utf8_exactfa_fold \
1478 : trie_latin_utf8_exactfa_fold) \
1479 : (scan->flags == EXACTFLU8 \
1480 ? trie_flu8 \
1481 : (utf8_target \
1482 ? trie_utf8_fold \
1483 : trie_latin_utf8_fold)))
fab2782b 1484
fd3249ee 1485#define REXEC_TRIE_READ_CHAR(trie_type, trie, widecharmap, uc, uscan, len, uvc, charid, foldlen, foldbuf, uniflags) \
baa60164 1486STMT_START { \
fab2782b 1487 STRLEN skiplen; \
baa60164 1488 U8 flags = FOLD_FLAGS_FULL; \
fab2782b 1489 switch (trie_type) { \
a4525e78 1490 case trie_flu8: \
780fcc9f 1491 _CHECK_AND_WARN_PROBLEMATIC_LOCALE; \
613abc6d
KW
1492 if (utf8_target && UTF8_IS_ABOVE_LATIN1(*uc)) { \
1493 _CHECK_AND_OUTPUT_WIDE_LOCALE_UTF8_MSG(uc, uc + UTF8SKIP(uc)); \
1494 } \
a4525e78 1495 goto do_trie_utf8_fold; \
31f05a37 1496 case trie_utf8_exactfa_fold: \
baa60164 1497 flags |= FOLD_FLAGS_NOMIX_ASCII; \
8e57b935 1498 /* FALLTHROUGH */ \
fab2782b 1499 case trie_utf8_fold: \
a4525e78 1500 do_trie_utf8_fold: \
fab2782b 1501 if ( foldlen>0 ) { \
c80e42f3 1502 uvc = utf8n_to_uvchr( (const U8*) uscan, UTF8_MAXLEN, &len, uniflags ); \
fab2782b
YO
1503 foldlen -= len; \
1504 uscan += len; \
1505 len=0; \
1506 } else { \
fab2782b 1507 len = UTF8SKIP(uc); \
a1a5ec35
KW
1508 uvc = _toFOLD_utf8_flags( (const U8*) uc, uc + len, foldbuf, &foldlen, \
1509 flags); \
5f560d8a 1510 skiplen = UVCHR_SKIP( uvc ); \
fab2782b
YO
1511 foldlen -= skiplen; \
1512 uscan = foldbuf + skiplen; \
1513 } \
1514 break; \
baa60164
KW
1515 case trie_latin_utf8_exactfa_fold: \
1516 flags |= FOLD_FLAGS_NOMIX_ASCII; \
8e57b935 1517 /* FALLTHROUGH */ \
fab2782b
YO
1518 case trie_latin_utf8_fold: \
1519 if ( foldlen>0 ) { \
c80e42f3 1520 uvc = utf8n_to_uvchr( (const U8*) uscan, UTF8_MAXLEN, &len, uniflags ); \
fab2782b
YO
1521 foldlen -= len; \
1522 uscan += len; \
1523 len=0; \
1524 } else { \
1525 len = 1; \
31f05a37 1526 uvc = _to_fold_latin1( (U8) *uc, foldbuf, &foldlen, flags); \
5f560d8a 1527 skiplen = UVCHR_SKIP( uvc ); \
fab2782b
YO
1528 foldlen -= skiplen; \
1529 uscan = foldbuf + skiplen; \
1530 } \
1531 break; \
a4525e78 1532 case trie_utf8l: \
780fcc9f 1533 _CHECK_AND_WARN_PROBLEMATIC_LOCALE; \
613abc6d
KW
1534 if (utf8_target && UTF8_IS_ABOVE_LATIN1(*uc)) { \
1535 _CHECK_AND_OUTPUT_WIDE_LOCALE_UTF8_MSG(uc, uc + UTF8SKIP(uc)); \
1536 } \
780fcc9f 1537 /* FALLTHROUGH */ \
fab2782b 1538 case trie_utf8: \
c80e42f3 1539 uvc = utf8n_to_uvchr( (const U8*) uc, UTF8_MAXLEN, &len, uniflags ); \
fab2782b
YO
1540 break; \
1541 case trie_plain: \
1542 uvc = (UV)*uc; \
1543 len = 1; \
1544 } \
1545 if (uvc < 256) { \
1546 charid = trie->charmap[ uvc ]; \
1547 } \
1548 else { \
1549 charid = 0; \
1550 if (widecharmap) { \
1551 SV** const svpp = hv_fetch(widecharmap, \
1552 (char*)&uvc, sizeof(UV), 0); \
1553 if (svpp) \
1554 charid = (U16)SvIV(*svpp); \
1555 } \
1556 } \
4cadc6a9
YO
1557} STMT_END
1558
cb41e5d6 1559#define DUMP_EXEC_POS(li,s,doutf8,depth) \
ae7c5b9b 1560 dump_exec_pos(li,s,(reginfo->strend),(reginfo->strbeg), \
cb41e5d6 1561 startpos, doutf8, depth)
ae7c5b9b 1562
c84a03c5 1563#define REXEC_FBC_EXACTISH_SCAN(COND) \
4cadc6a9
YO
1564STMT_START { \
1565 while (s <= e) { \
c84a03c5 1566 if ( (COND) \
fac1af77 1567 && (ln == 1 || folder(s, pat_string, ln)) \
02d5137b 1568 && (reginfo->intuit || regtry(reginfo, &s)) )\
4cadc6a9
YO
1569 goto got_it; \
1570 s++; \
1571 } \
1572} STMT_END
1573
c84a03c5 1574#define REXEC_FBC_UTF8_SCAN(CODE) \
4cadc6a9 1575STMT_START { \
9a902117 1576 while (s < strend) { \
c84a03c5 1577 CODE \
9a902117 1578 s += UTF8SKIP(s); \
4cadc6a9
YO
1579 } \
1580} STMT_END
1581
c84a03c5 1582#define REXEC_FBC_SCAN(CODE) \
4cadc6a9
YO
1583STMT_START { \
1584 while (s < strend) { \
c84a03c5 1585 CODE \
4cadc6a9
YO
1586 s++; \
1587 } \
1588} STMT_END
1589
05bd126c
KW
1590#define REXEC_FBC_UTF8_CLASS_SCAN(COND) \
1591REXEC_FBC_UTF8_SCAN( /* Loops while (s < strend) */ \
1592 if (COND) { \
1593 if (tmp && (reginfo->intuit || regtry(reginfo, &s))) \
1594 goto got_it; \
1595 else \
1596 tmp = doevery; \
1597 } \
1598 else \
1599 tmp = 1; \
4cadc6a9
YO
1600)
1601
05bd126c
KW
1602#define REXEC_FBC_CLASS_SCAN(COND) \
1603REXEC_FBC_SCAN( /* Loops while (s < strend) */ \
1604 if (COND) { \
1605 if (tmp && (reginfo->intuit || regtry(reginfo, &s))) \
1606 goto got_it; \
1607 else \
1608 tmp = doevery; \
1609 } \
1610 else \
1611 tmp = 1; \
4cadc6a9
YO
1612)
1613
c84a03c5 1614#define REXEC_FBC_CSCAN(CONDUTF8,COND) \
baa60164 1615 if (utf8_target) { \
c84a03c5 1616 REXEC_FBC_UTF8_CLASS_SCAN(CONDUTF8); \
e1d1eefb
YO
1617 } \
1618 else { \
c84a03c5 1619 REXEC_FBC_CLASS_SCAN(COND); \
d981ef24 1620 }
05bd126c 1621
05bd126c
KW
1622/* The three macros below are slightly different versions of the same logic.
1623 *
1624 * The first is for /a and /aa when the target string is UTF-8. This can only
1625 * match ascii, but it must advance based on UTF-8. The other two handle the
1626 * non-UTF-8 and the more generic UTF-8 cases. In all three, we are looking
1627 * for the boundary (or non-boundary) between a word and non-word character.
1628 * The utf8 and non-utf8 cases have the same logic, but the details must be
1629 * different. Find the "wordness" of the character just prior to this one, and
1630 * compare it with the wordness of this one. If they differ, we have a
1631 * boundary. At the beginning of the string, pretend that the previous
1632 * character was a new-line.
1633 *
1634 * All these macros uncleanly have side-effects with each other and outside
1635 * variables. So far it's been too much trouble to clean-up
1636 *
1637 * TEST_NON_UTF8 is the macro or function to call to test if its byte input is
1638 * a word character or not.
1639 * IF_SUCCESS is code to do if it finds that we are at a boundary between
1640 * word/non-word
1641 * IF_FAIL is code to do if we aren't at a boundary between word/non-word
1642 *
1643 * Exactly one of the two IF_FOO parameters is a no-op, depending on whether we
1644 * are looking for a boundary or for a non-boundary. If we are looking for a
1645 * boundary, we want IF_FAIL to be the no-op, and for IF_SUCCESS to go out and
1646 * see if this tentative match actually works, and if so, to quit the loop
1647 * here. And vice-versa if we are looking for a non-boundary.
1648 *
1649 * 'tmp' below in the next three macros in the REXEC_FBC_SCAN and
1650 * REXEC_FBC_UTF8_SCAN loops is a loop invariant, a bool giving the return of
1651 * TEST_NON_UTF8(s-1). To see this, note that that's what it is defined to be
1652 * at entry to the loop, and to get to the IF_FAIL branch, tmp must equal
1653 * TEST_NON_UTF8(s), and in the opposite branch, IF_SUCCESS, tmp is that
1654 * complement. But in that branch we complement tmp, meaning that at the
1655 * bottom of the loop tmp is always going to be equal to TEST_NON_UTF8(s),
1656 * which means at the top of the loop in the next iteration, it is
1657 * TEST_NON_UTF8(s-1) */
b2f4e957 1658#define FBC_UTF8_A(TEST_NON_UTF8, IF_SUCCESS, IF_FAIL) \
05bd126c
KW
1659 tmp = (s != reginfo->strbeg) ? UCHARAT(s - 1) : '\n'; \
1660 tmp = TEST_NON_UTF8(tmp); \
1661 REXEC_FBC_UTF8_SCAN( /* advances s while s < strend */ \
1662 if (tmp == ! TEST_NON_UTF8((U8) *s)) { \
1663 tmp = !tmp; \
1664 IF_SUCCESS; /* Is a boundary if values for s-1 and s differ */ \
1665 } \
1666 else { \
1667 IF_FAIL; \
1668 } \
1669 ); \
1670
1671/* Like FBC_UTF8_A, but TEST_UV is a macro which takes a UV as its input, and
1672 * TEST_UTF8 is a macro that for the same input code points returns identically
1673 * to TEST_UV, but takes a pointer to a UTF-8 encoded string instead */
236d82fd 1674#define FBC_UTF8(TEST_UV, TEST_UTF8, IF_SUCCESS, IF_FAIL) \
05bd126c
KW
1675 if (s == reginfo->strbeg) { \
1676 tmp = '\n'; \
1677 } \
1678 else { /* Back-up to the start of the previous character */ \
1679 U8 * const r = reghop3((U8*)s, -1, (U8*)reginfo->strbeg); \
1680 tmp = utf8n_to_uvchr(r, (U8*) reginfo->strend - r, \
3db24e1e 1681 0, UTF8_ALLOW_DEFAULT); \
05bd126c
KW
1682 } \
1683 tmp = TEST_UV(tmp); \
1684 LOAD_UTF8_CHARCLASS_ALNUM(); \
1685 REXEC_FBC_UTF8_SCAN( /* advances s while s < strend */ \
7a207065 1686 if (tmp == ! (TEST_UTF8((U8 *) s, (U8 *) reginfo->strend))) { \
05bd126c
KW
1687 tmp = !tmp; \
1688 IF_SUCCESS; \
1689 } \
1690 else { \
1691 IF_FAIL; \
1692 } \
1693 );
cfaf538b 1694
05bd126c
KW
1695/* Like the above two macros. UTF8_CODE is the complete code for handling
1696 * UTF-8. Common to the BOUND and NBOUND cases, set-up by the FBC_BOUND, etc
1697 * macros below */
baa60164 1698#define FBC_BOUND_COMMON(UTF8_CODE, TEST_NON_UTF8, IF_SUCCESS, IF_FAIL) \
63ac0dad 1699 if (utf8_target) { \
05bd126c 1700 UTF8_CODE \
63ac0dad
KW
1701 } \
1702 else { /* Not utf8 */ \
9d9163fb 1703 tmp = (s != reginfo->strbeg) ? UCHARAT(s - 1) : '\n'; \
63ac0dad 1704 tmp = TEST_NON_UTF8(tmp); \
05bd126c 1705 REXEC_FBC_SCAN( /* advances s while s < strend */ \
63ac0dad 1706 if (tmp == ! TEST_NON_UTF8((U8) *s)) { \
63ac0dad 1707 IF_SUCCESS; \
760cfa8e 1708 tmp = !tmp; \
63ac0dad
KW
1709 } \
1710 else { \
1711 IF_FAIL; \
1712 } \
1713 ); \
1714 } \
c8519dc7
KW
1715 /* Here, things have been set up by the previous code so that tmp is the \
1716 * return of TEST_NON_UTF(s-1) or TEST_UTF8(s-1) (depending on the \
1717 * utf8ness of the target). We also have to check if this matches against \
1718 * the EOS, which we treat as a \n (which is the same value in both UTF-8 \
1719 * or non-UTF8, so can use the non-utf8 test condition even for a UTF-8 \
1720 * string */ \
1721 if (tmp == ! TEST_NON_UTF8('\n')) { \
1722 IF_SUCCESS; \
1723 } \
1724 else { \
1725 IF_FAIL; \
1726 }
63ac0dad 1727
ae7c5b9b
KW
1728/* This is the macro to use when we want to see if something that looks like it
1729 * could match, actually does, and if so exits the loop */
1730#define REXEC_FBC_TRYIT \
1731 if ((reginfo->intuit || regtry(reginfo, &s))) \
1732 goto got_it
1733
1734/* The only difference between the BOUND and NBOUND cases is that
1735 * REXEC_FBC_TRYIT is called when matched in BOUND, and when non-matched in
1736 * NBOUND. This is accomplished by passing it as either the if or else clause,
1737 * with the other one being empty (PLACEHOLDER is defined as empty).
1738 *
1739 * The TEST_FOO parameters are for operating on different forms of input, but
1740 * all should be ones that return identically for the same underlying code
1741 * points */
1742#define FBC_BOUND(TEST_NON_UTF8, TEST_UV, TEST_UTF8) \
1743 FBC_BOUND_COMMON( \
1744 FBC_UTF8(TEST_UV, TEST_UTF8, REXEC_FBC_TRYIT, PLACEHOLDER), \
1745 TEST_NON_UTF8, REXEC_FBC_TRYIT, PLACEHOLDER)
1746
44129e46 1747#define FBC_BOUND_A(TEST_NON_UTF8) \
ae7c5b9b
KW
1748 FBC_BOUND_COMMON( \
1749 FBC_UTF8_A(TEST_NON_UTF8, REXEC_FBC_TRYIT, PLACEHOLDER), \
1750 TEST_NON_UTF8, REXEC_FBC_TRYIT, PLACEHOLDER)
1751
1752#define FBC_NBOUND(TEST_NON_UTF8, TEST_UV, TEST_UTF8) \
1753 FBC_BOUND_COMMON( \
1754 FBC_UTF8(TEST_UV, TEST_UTF8, PLACEHOLDER, REXEC_FBC_TRYIT), \
1755 TEST_NON_UTF8, PLACEHOLDER, REXEC_FBC_TRYIT)
1756
44129e46 1757#define FBC_NBOUND_A(TEST_NON_UTF8) \
ae7c5b9b
KW
1758 FBC_BOUND_COMMON( \
1759 FBC_UTF8_A(TEST_NON_UTF8, PLACEHOLDER, REXEC_FBC_TRYIT), \
1760 TEST_NON_UTF8, PLACEHOLDER, REXEC_FBC_TRYIT)
1761
8bde5eaf
JH
1762#ifdef DEBUGGING
1763static IV
1764S_get_break_val_cp_checked(SV* const invlist, const UV cp_in) {
1765 IV cp_out = Perl__invlist_search(invlist, cp_in);
1766 assert(cp_out >= 0);
1767 return cp_out;
1768}
1769# define _generic_GET_BREAK_VAL_CP_CHECKED(invlist, invmap, cp) \
1770 invmap[S_get_break_val_cp_checked(invlist, cp)]
1771#else
1772# define _generic_GET_BREAK_VAL_CP_CHECKED(invlist, invmap, cp) \
1773 invmap[_invlist_search(invlist, cp)]
1774#endif
1775
64935bc6
KW
1776/* Takes a pointer to an inversion list, a pointer to its corresponding
1777 * inversion map, and a code point, and returns the code point's value
1778 * according to the two arrays. It assumes that all code points have a value.
1779 * This is used as the base macro for macros for particular properties */
1780#define _generic_GET_BREAK_VAL_CP(invlist, invmap, cp) \
8bde5eaf 1781 _generic_GET_BREAK_VAL_CP_CHECKED(invlist, invmap, cp)
64935bc6
KW
1782
1783/* Same as above, but takes begin, end ptrs to a UTF-8 encoded string instead
1784 * of a code point, returning the value for the first code point in the string.
1785 * And it takes the particular macro name that finds the desired value given a
1786 * code point. Merely convert the UTF-8 to code point and call the cp macro */
1787#define _generic_GET_BREAK_VAL_UTF8(cp_macro, pos, strend) \
1788 (__ASSERT_(pos < strend) \
1789 /* Note assumes is valid UTF-8 */ \
1790 (cp_macro(utf8_to_uvchr_buf((pos), (strend), NULL))))
1791
1792/* Returns the GCB value for the input code point */
1793#define getGCB_VAL_CP(cp) \
1794 _generic_GET_BREAK_VAL_CP( \
1795 PL_GCB_invlist, \
02f811dd 1796 _Perl_GCB_invmap, \
64935bc6
KW
1797 (cp))
1798
1799/* Returns the GCB value for the first code point in the UTF-8 encoded string
1800 * bounded by pos and strend */
1801#define getGCB_VAL_UTF8(pos, strend) \
1802 _generic_GET_BREAK_VAL_UTF8(getGCB_VAL_CP, pos, strend)
05bd126c 1803
6b659339
KW
1804/* Returns the LB value for the input code point */
1805#define getLB_VAL_CP(cp) \
1806 _generic_GET_BREAK_VAL_CP( \
1807 PL_LB_invlist, \
1808 _Perl_LB_invmap, \
1809 (cp))
1810
1811/* Returns the LB value for the first code point in the UTF-8 encoded string
1812 * bounded by pos and strend */
1813#define getLB_VAL_UTF8(pos, strend) \
1814 _generic_GET_BREAK_VAL_UTF8(getLB_VAL_CP, pos, strend)
1815
06ae2722
KW
1816
1817/* Returns the SB value for the input code point */
1818#define getSB_VAL_CP(cp) \
1819 _generic_GET_BREAK_VAL_CP( \
1820 PL_SB_invlist, \
bf4268fa 1821 _Perl_SB_invmap, \
06ae2722
KW
1822 (cp))
1823
1824/* Returns the SB value for the first code point in the UTF-8 encoded string
1825 * bounded by pos and strend */
1826#define getSB_VAL_UTF8(pos, strend) \
1827 _generic_GET_BREAK_VAL_UTF8(getSB_VAL_CP, pos, strend)
1828
ae3bb8ea
KW
1829/* Returns the WB value for the input code point */
1830#define getWB_VAL_CP(cp) \
1831 _generic_GET_BREAK_VAL_CP( \
1832 PL_WB_invlist, \
bf4268fa 1833 _Perl_WB_invmap, \
ae3bb8ea
KW
1834 (cp))
1835
1836/* Returns the WB value for the first code point in the UTF-8 encoded string
1837 * bounded by pos and strend */
1838#define getWB_VAL_UTF8(pos, strend) \
1839 _generic_GET_BREAK_VAL_UTF8(getWB_VAL_CP, pos, strend)
1840
786e8c11 1841/* We know what class REx starts with. Try to find this position... */
02d5137b 1842/* if reginfo->intuit, its a dryrun */
786e8c11
YO
1843/* annoyingly all the vars in this routine have different names from their counterparts
1844 in regmatch. /grrr */
3c3eec57 1845STATIC char *
07be1b83 1846S_find_byclass(pTHX_ regexp * prog, const regnode *c, char *s,
f9176b44 1847 const char *strend, regmatch_info *reginfo)
a687059c 1848{
73104a1b
KW
1849 dVAR;
1850 const I32 doevery = (prog->intflags & PREGf_SKIP) == 0;
1851 char *pat_string; /* The pattern's exactish string */
1852 char *pat_end; /* ptr to end char of pat_string */
1853 re_fold_t folder; /* Function for computing non-utf8 folds */
1854 const U8 *fold_array; /* array for folding ords < 256 */
1855 STRLEN ln;
1856 STRLEN lnc;
73104a1b
KW
1857 U8 c1;
1858 U8 c2;
1859 char *e;
1860 I32 tmp = 1; /* Scratch variable? */
ba44c216 1861 const bool utf8_target = reginfo->is_utf8_target;
73104a1b 1862 UV utf8_fold_flags = 0;
f9176b44 1863 const bool is_utf8_pat = reginfo->is_utf8_pat;
3018b823
KW
1864 bool to_complement = FALSE; /* Invert the result? Taking the xor of this
1865 with a result inverts that result, as 0^1 =
1866 1 and 1^1 = 0 */
1867 _char_class_number classnum;
1868
73104a1b 1869 RXi_GET_DECL(prog,progi);
2f7f8cb1 1870
73104a1b 1871 PERL_ARGS_ASSERT_FIND_BYCLASS;
2f7f8cb1 1872
73104a1b
KW
1873 /* We know what class it must start with. */
1874 switch (OP(c)) {
a4525e78 1875 case ANYOFL:
780fcc9f 1876 _CHECK_AND_WARN_PROBLEMATIC_LOCALE;
a0bd1a30 1877
d1c40ef5 1878 if (ANYOFL_UTF8_LOCALE_REQD(FLAGS(c)) && ! IN_UTF8_CTYPE_LOCALE) {
a0bd1a30
KW
1879 Perl_ck_warner(aTHX_ packWARN(WARN_LOCALE), utf8_locale_required);
1880 }
1881
780fcc9f 1882 /* FALLTHROUGH */
ac44c12e 1883 case ANYOFD:
73104a1b
KW
1884 case ANYOF:
1885 if (utf8_target) {
1886 REXEC_FBC_UTF8_CLASS_SCAN(
3db24e1e 1887 reginclass(prog, c, (U8*)s, (U8*) strend, utf8_target));
73104a1b 1888 }
1451f692
DM
1889 else if (ANYOF_FLAGS(c)) {
1890 REXEC_FBC_CLASS_SCAN(reginclass(prog,c, (U8*)s, (U8*)s+1, 0));
1891 }
73104a1b 1892 else {
1451f692 1893 REXEC_FBC_CLASS_SCAN(ANYOF_BITMAP_TEST(c, *((U8*)s)));
73104a1b
KW
1894 }
1895 break;
73104a1b 1896
098b07d5
KW
1897 case EXACTFA_NO_TRIE: /* This node only generated for non-utf8 patterns */
1898 assert(! is_utf8_pat);
924ba076 1899 /* FALLTHROUGH */
73104a1b 1900 case EXACTFA:
984e6dd1 1901 if (is_utf8_pat || utf8_target) {
73104a1b
KW
1902 utf8_fold_flags = FOLDEQ_UTF8_NOMIX_ASCII;
1903 goto do_exactf_utf8;
1904 }
1905 fold_array = PL_fold_latin1; /* Latin1 folds are not affected by */
1906 folder = foldEQ_latin1; /* /a, except the sharp s one which */
1907 goto do_exactf_non_utf8; /* isn't dealt with by these */
77a6d856 1908
2fdb7295
KW
1909 case EXACTF: /* This node only generated for non-utf8 patterns */
1910 assert(! is_utf8_pat);
73104a1b 1911 if (utf8_target) {
73104a1b
KW
1912 utf8_fold_flags = 0;
1913 goto do_exactf_utf8;
1914 }
1915 fold_array = PL_fold;
1916 folder = foldEQ;
1917 goto do_exactf_non_utf8;
1918
1919 case EXACTFL:
780fcc9f 1920 _CHECK_AND_WARN_PROBLEMATIC_LOCALE;
31f05a37 1921 if (is_utf8_pat || utf8_target || IN_UTF8_CTYPE_LOCALE) {
cea315b6 1922 utf8_fold_flags = FOLDEQ_LOCALE;
73104a1b
KW
1923 goto do_exactf_utf8;
1924 }
1925 fold_array = PL_fold_locale;
1926 folder = foldEQ_locale;
1927 goto do_exactf_non_utf8;
3c760661 1928
73104a1b 1929 case EXACTFU_SS:
984e6dd1 1930 if (is_utf8_pat) {
73104a1b
KW
1931 utf8_fold_flags = FOLDEQ_S2_ALREADY_FOLDED;
1932 }
1933 goto do_exactf_utf8;
16d951b7 1934
a4525e78
KW
1935 case EXACTFLU8:
1936 if (! utf8_target) { /* All code points in this node require
1937 UTF-8 to express. */
1938 break;
1939 }
613abc6d
KW
1940 utf8_fold_flags = FOLDEQ_LOCALE | FOLDEQ_S2_ALREADY_FOLDED
1941 | FOLDEQ_S2_FOLDS_SANE;
a4525e78
KW
1942 goto do_exactf_utf8;
1943
73104a1b 1944 case EXACTFU:
984e6dd1
DM
1945 if (is_utf8_pat || utf8_target) {
1946 utf8_fold_flags = is_utf8_pat ? FOLDEQ_S2_ALREADY_FOLDED : 0;
73104a1b
KW
1947 goto do_exactf_utf8;
1948 }
fac1af77 1949
73104a1b
KW
1950 /* Any 'ss' in the pattern should have been replaced by regcomp,
1951 * so we don't have to worry here about this single special case
1952 * in the Latin1 range */
1953 fold_array = PL_fold_latin1;
1954 folder = foldEQ_latin1;
1955
924ba076 1956 /* FALLTHROUGH */
73104a1b 1957
c52b8b12 1958 do_exactf_non_utf8: /* Neither pattern nor string are UTF8, and there
73104a1b
KW
1959 are no glitches with fold-length differences
1960 between the target string and pattern */
1961
1962 /* The idea in the non-utf8 EXACTF* cases is to first find the
1963 * first character of the EXACTF* node and then, if necessary,
1964 * case-insensitively compare the full text of the node. c1 is the
1965 * first character. c2 is its fold. This logic will not work for
1966 * Unicode semantics and the german sharp ss, which hence should
1967 * not be compiled into a node that gets here. */
1968 pat_string = STRING(c);
1969 ln = STR_LEN(c); /* length to match in octets/bytes */
1970
1971 /* We know that we have to match at least 'ln' bytes (which is the
1972 * same as characters, since not utf8). If we have to match 3
1973 * characters, and there are only 2 availabe, we know without
1974 * trying that it will fail; so don't start a match past the
1975 * required minimum number from the far end */
ea3daa5d 1976 e = HOP3c(strend, -((SSize_t)ln), s);
dda01918
HS
1977 if (e < s)
1978 break;
fac1af77 1979
73104a1b
KW
1980 c1 = *pat_string;
1981 c2 = fold_array[c1];
1982 if (c1 == c2) { /* If char and fold are the same */
1983 REXEC_FBC_EXACTISH_SCAN(*(U8*)s == c1);
1984 }
1985 else {
1986 REXEC_FBC_EXACTISH_SCAN(*(U8*)s == c1 || *(U8*)s == c2);
1987 }
1988 break;
fac1af77 1989
c52b8b12
KW
1990 do_exactf_utf8:
1991 {
73104a1b
KW
1992 unsigned expansion;
1993
1994 /* If one of the operands is in utf8, we can't use the simpler folding
1995 * above, due to the fact that many different characters can have the
1996 * same fold, or portion of a fold, or different- length fold */
1997 pat_string = STRING(c);
1998 ln = STR_LEN(c); /* length to match in octets/bytes */
1999 pat_end = pat_string + ln;
984e6dd1 2000 lnc = is_utf8_pat /* length to match in characters */
73104a1b
KW
2001 ? utf8_length((U8 *) pat_string, (U8 *) pat_end)
2002 : ln;
2003
2004 /* We have 'lnc' characters to match in the pattern, but because of
2005 * multi-character folding, each character in the target can match
2006 * up to 3 characters (Unicode guarantees it will never exceed
2007 * this) if it is utf8-encoded; and up to 2 if not (based on the
2008 * fact that the Latin 1 folds are already determined, and the
2009 * only multi-char fold in that range is the sharp-s folding to
2010 * 'ss'. Thus, a pattern character can match as little as 1/3 of a
2011 * string character. Adjust lnc accordingly, rounding up, so that
2012 * if we need to match at least 4+1/3 chars, that really is 5. */
2013 expansion = (utf8_target) ? UTF8_MAX_FOLD_CHAR_EXPAND : 2;
2014 lnc = (lnc + expansion - 1) / expansion;
2015
2016 /* As in the non-UTF8 case, if we have to match 3 characters, and
2017 * only 2 are left, it's guaranteed to fail, so don't start a
2018 * match that would require us to go beyond the end of the string
2019 */
ea3daa5d 2020 e = HOP3c(strend, -((SSize_t)lnc), s);
73104a1b 2021
73104a1b
KW
2022 /* XXX Note that we could recalculate e to stop the loop earlier,
2023 * as the worst case expansion above will rarely be met, and as we
2024 * go along we would usually find that e moves further to the left.
2025 * This would happen only after we reached the point in the loop
2026 * where if there were no expansion we should fail. Unclear if
2027 * worth the expense */
2028
2029 while (s <= e) {
2030 char *my_strend= (char *)strend;
2031 if (foldEQ_utf8_flags(s, &my_strend, 0, utf8_target,
984e6dd1 2032 pat_string, NULL, ln, is_utf8_pat, utf8_fold_flags)
02d5137b 2033 && (reginfo->intuit || regtry(reginfo, &s)) )
73104a1b
KW
2034 {
2035 goto got_it;
2036 }
2037 s += (utf8_target) ? UTF8SKIP(s) : 1;
2038 }
2039 break;
2040 }
236d82fd 2041
73104a1b 2042 case BOUNDL:
780fcc9f 2043 _CHECK_AND_WARN_PROBLEMATIC_LOCALE;
64935bc6 2044 if (FLAGS(c) != TRADITIONAL_BOUND) {
89ad707a
KW
2045 if (! IN_UTF8_CTYPE_LOCALE) {
2046 Perl_ck_warner(aTHX_ packWARN(WARN_LOCALE),
64935bc6 2047 B_ON_NON_UTF8_LOCALE_IS_WRONG);
89ad707a 2048 }
64935bc6
KW
2049 goto do_boundu;
2050 }
2051
7a207065 2052 FBC_BOUND(isWORDCHAR_LC, isWORDCHAR_LC_uvchr, isWORDCHAR_LC_utf8_safe);
73104a1b 2053 break;
64935bc6 2054
73104a1b 2055 case NBOUNDL:
780fcc9f 2056 _CHECK_AND_WARN_PROBLEMATIC_LOCALE;
64935bc6 2057 if (FLAGS(c) != TRADITIONAL_BOUND) {
89ad707a
KW
2058 if (! IN_UTF8_CTYPE_LOCALE) {
2059 Perl_ck_warner(aTHX_ packWARN(WARN_LOCALE),
64935bc6 2060 B_ON_NON_UTF8_LOCALE_IS_WRONG);
89ad707a 2061 }
64935bc6
KW
2062 goto do_nboundu;
2063 }
2064
7a207065 2065 FBC_NBOUND(isWORDCHAR_LC, isWORDCHAR_LC_uvchr, isWORDCHAR_LC_utf8_safe);
73104a1b 2066 break;
64935bc6
KW
2067
2068 case BOUND: /* regcomp.c makes sure that this only has the traditional \b
2069 meaning */
2070 assert(FLAGS(c) == TRADITIONAL_BOUND);
2071
7a207065 2072 FBC_BOUND(isWORDCHAR, isWORDCHAR_uni, isWORDCHAR_utf8_safe);
73104a1b 2073 break;
64935bc6
KW
2074
2075 case BOUNDA: /* regcomp.c makes sure that this only has the traditional \b
2076 meaning */
2077 assert(FLAGS(c) == TRADITIONAL_BOUND);
2078
44129e46 2079 FBC_BOUND_A(isWORDCHAR_A);
73104a1b 2080 break;
64935bc6
KW
2081
2082 case NBOUND: /* regcomp.c makes sure that this only has the traditional \b
2083 meaning */
2084 assert(FLAGS(c) == TRADITIONAL_BOUND);
2085
7a207065 2086 FBC_NBOUND(isWORDCHAR, isWORDCHAR_uni, isWORDCHAR_utf8_safe);
73104a1b 2087 break;
64935bc6
KW
2088
2089 case NBOUNDA: /* regcomp.c makes sure that this only has the traditional \b
2090 meaning */
2091 assert(FLAGS(c) == TRADITIONAL_BOUND);
2092
44129e46 2093 FBC_NBOUND_A(isWORDCHAR_A);
73104a1b 2094 break;
64935bc6 2095
73104a1b 2096 case NBOUNDU:
64935bc6 2097 if ((bound_type) FLAGS(c) == TRADITIONAL_BOUND) {
7a207065 2098 FBC_NBOUND(isWORDCHAR_L1, isWORDCHAR_uni, isWORDCHAR_utf8_safe);
64935bc6
KW
2099 break;
2100 }
2101
2102 do_nboundu:
2103
2104 to_complement = 1;
2105 /* FALLTHROUGH */
2106
2107 case BOUNDU:
2108 do_boundu:
2109 switch((bound_type) FLAGS(c)) {
2110 case TRADITIONAL_BOUND:
7a207065 2111 FBC_BOUND(isWORDCHAR_L1, isWORDCHAR_uni, isWORDCHAR_utf8_safe);
64935bc6
KW
2112 break;
2113 case GCB_BOUND:
a7a8bd1e 2114 if (s == reginfo->strbeg) {
67481c39 2115 if (reginfo->intuit || regtry(reginfo, &s))
64935bc6
KW
2116 {
2117 goto got_it;
2118 }
a7a8bd1e
KW
2119
2120 /* Didn't match. Try at the next position (if there is one) */
64935bc6 2121 s += (utf8_target) ? UTF8SKIP(s) : 1;
a7a8bd1e
KW
2122 if (UNLIKELY(s >= reginfo->strend)) {
2123 break;
2124 }
64935bc6
KW
2125 }
2126
2127 if (utf8_target) {
85e5f08b 2128 GCB_enum before = getGCB_VAL_UTF8(
64935bc6
KW
2129 reghop3((U8*)s, -1,
2130 (U8*)(reginfo->strbeg)),
2131 (U8*) reginfo->strend);
2132 while (s < strend) {
85e5f08b 2133 GCB_enum after = getGCB_VAL_UTF8((U8*) s,
64935bc6 2134 (U8*) reginfo->strend);
b0e24409
KW
2135 if ( (to_complement ^ isGCB(before,
2136 after,
2137 (U8*) reginfo->strbeg,
2138 (U8*) s,
2139 utf8_target))
00e3344b
KW
2140 && (reginfo->intuit || regtry(reginfo, &s)))
2141 {
2142 goto got_it;
64935bc6 2143 }
43a7bd62 2144 before = after;
64935bc6
KW
2145 s += UTF8SKIP(s);
2146 }
2147 }
2148 else { /* Not utf8. Everything is a GCB except between CR and
2149 LF */
2150 while (s < strend) {
00e3344b
KW
2151 if ((to_complement ^ ( UCHARAT(s - 1) != '\r'
2152 || UCHARAT(s) != '\n'))
2153 && (reginfo->intuit || regtry(reginfo, &s)))
64935bc6 2154 {
00e3344b 2155 goto got_it;
64935bc6 2156 }
43a7bd62 2157 s++;
64935bc6
KW
2158 }
2159 }
2160
6de80efc
KW
2161 /* And, since this is a bound, it can match after the final
2162 * character in the string */
67481c39 2163 if ((reginfo->intuit || regtry(reginfo, &s))) {
64935bc6
KW
2164 goto got_it;
2165 }
2166 break;
ae3bb8ea 2167
6b659339
KW
2168 case LB_BOUND:
2169 if (s == reginfo->strbeg) {
2170 if (reginfo->intuit || regtry(reginfo, &s)) {
2171 goto got_it;
2172 }
2173 s += (utf8_target) ? UTF8SKIP(s) : 1;
2174 if (UNLIKELY(s >= reginfo->strend)) {
2175 break;
2176 }
2177 }
2178
2179 if (utf8_target) {
2180 LB_enum before = getLB_VAL_UTF8(reghop3((U8*)s,
2181 -1,
2182 (U8*)(reginfo->strbeg)),
2183 (U8*) reginfo->strend);
2184 while (s < strend) {
2185 LB_enum after = getLB_VAL_UTF8((U8*) s, (U8*) reginfo->strend);
2186 if (to_complement ^ isLB(before,
2187 after,
2188 (U8*) reginfo->strbeg,
2189 (U8*) s,
2190 (U8*) reginfo->strend,
2191 utf8_target)
2192 && (reginfo->intuit || regtry(reginfo, &s)))
2193 {
2194 goto got_it;
2195 }
2196 before = after;
2197 s += UTF8SKIP(s);
2198 }
2199 }
2200 else { /* Not utf8. */
2201 LB_enum before = getLB_VAL_CP((U8) *(s -1));
2202 while (s < strend) {
2203 LB_enum after = getLB_VAL_CP((U8) *s);
2204 if (to_complement ^ isLB(before,
2205 after,
2206 (U8*) reginfo->strbeg,
2207 (U8*) s,
2208 (U8*) reginfo->strend,
2209 utf8_target)
2210 && (reginfo->intuit || regtry(reginfo, &s)))
2211 {
2212 goto got_it;
2213 }
2214 before = after;
2215 s++;
2216 }
2217 }
2218
2219 if (reginfo->intuit || regtry(reginfo, &s)) {
2220 goto got_it;
2221 }
2222
2223 break;
2224
06ae2722 2225 case SB_BOUND:
a7a8bd1e 2226 if (s == reginfo->strbeg) {
67481c39 2227 if (reginfo->intuit || regtry(reginfo, &s)) {
06ae2722
KW
2228 goto got_it;
2229 }
06ae2722 2230 s += (utf8_target) ? UTF8SKIP(s) : 1;
a7a8bd1e
KW
2231 if (UNLIKELY(s >= reginfo->strend)) {
2232 break;
2233 }
06ae2722
KW
2234 }
2235
2236 if (utf8_target) {
85e5f08b 2237 SB_enum before = getSB_VAL_UTF8(reghop3((U8*)s,
06ae2722
KW
2238 -1,
2239 (U8*)(reginfo->strbeg)),
2240 (U8*) reginfo->strend);
2241 while (s < strend) {
85e5f08b 2242 SB_enum after = getSB_VAL_UTF8((U8*) s,
06ae2722 2243 (U8*) reginfo->strend);
00e3344b
KW
2244 if ((to_complement ^ isSB(before,
2245 after,
2246 (U8*) reginfo->strbeg,
2247 (U8*) s,
2248 (U8*) reginfo->strend,
2249 utf8_target))
2250 && (reginfo->intuit || regtry(reginfo, &s)))
06ae2722 2251 {
00e3344b 2252 goto got_it;
06ae2722 2253 }
43a7bd62 2254 before = after;
06ae2722
KW
2255 s += UTF8SKIP(s);
2256 }
2257 }
2258 else { /* Not utf8. */
85e5f08b 2259 SB_enum before = getSB_VAL_CP((U8) *(s -1));
06ae2722 2260 while (s < strend) {
85e5f08b 2261 SB_enum after = getSB_VAL_CP((U8) *s);
00e3344b
KW
2262 if ((to_complement ^ isSB(before,
2263 after,
2264 (U8*) reginfo->strbeg,
2265 (U8*) s,
2266 (U8*) reginfo->strend,
2267 utf8_target))
2268 && (reginfo->intuit || regtry(reginfo, &s)))
06ae2722 2269 {
00e3344b 2270 goto got_it;
06ae2722 2271 }
43a7bd62 2272 before = after;
06ae2722
KW
2273 s++;
2274 }
2275 }
2276
2277 /* Here are at the final position in the target string. The SB
2278 * value is always true here, so matches, depending on other
2279 * constraints */
67481c39 2280 if (reginfo->intuit || regtry(reginfo, &s)) {
06ae2722
KW
2281 goto got_it;
2282 }
2283
2284 break;
2285
ae3bb8ea
KW
2286 case WB_BOUND:
2287 if (s == reginfo->strbeg) {
67481c39 2288 if (reginfo->intuit || regtry(reginfo, &s)) {
ae3bb8ea
KW
2289 goto got_it;
2290 }
2291 s += (utf8_target) ? UTF8SKIP(s) : 1;
a7a8bd1e
KW
2292 if (UNLIKELY(s >= reginfo->strend)) {
2293 break;
2294 }
ae3bb8ea
KW
2295 }
2296
2297 if (utf8_target) {
2298 /* We are at a boundary between char_sub_0 and char_sub_1.
2299 * We also keep track of the value for char_sub_-1 as we
2300 * loop through the line. Context may be needed to make a
2301 * determination, and if so, this can save having to
2302 * recalculate it */
85e5f08b
KW
2303 WB_enum previous = WB_UNKNOWN;
2304 WB_enum before = getWB_VAL_UTF8(
ae3bb8ea
KW
2305 reghop3((U8*)s,
2306 -1,
2307 (U8*)(reginfo->strbeg)),
2308 (U8*) reginfo->strend);
2309 while (s < strend) {
85e5f08b 2310 WB_enum after = getWB_VAL_UTF8((U8*) s,
ae3bb8ea 2311 (U8*) reginfo->strend);
00e3344b
KW
2312 if ((to_complement ^ isWB(previous,
2313 before,
2314 after,
2315 (U8*) reginfo->strbeg,
2316 (U8*) s,
2317 (U8*) reginfo->strend,
2318 utf8_target))
2319 && (reginfo->intuit || regtry(reginfo, &s)))
ae3bb8ea 2320 {
00e3344b 2321 goto got_it;
ae3bb8ea 2322 }
43a7bd62
KW
2323 previous = before;
2324 before = after;
ae3bb8ea
KW
2325 s += UTF8SKIP(s);
2326 }
2327 }
2328 else { /* Not utf8. */
85e5f08b
KW
2329 WB_enum previous = WB_UNKNOWN;
2330 WB_enum before = getWB_VAL_CP((U8) *(s -1));
ae3bb8ea 2331 while (s < strend) {
85e5f08b 2332 WB_enum after = getWB_VAL_CP((U8) *s);
00e3344b
KW
2333 if ((to_complement ^ isWB(previous,
2334 before,
2335 after,
2336 (U8*) reginfo->strbeg,
2337 (U8*) s,
2338 (U8*) reginfo->strend,
2339 utf8_target))
2340 && (reginfo->intuit || regtry(reginfo, &s)))
ae3bb8ea 2341 {
00e3344b 2342 goto got_it;
ae3bb8ea 2343 }
43a7bd62
KW
2344 previous = before;
2345 before = after;
ae3bb8ea
KW
2346 s++;
2347 }
2348 }
2349
67481c39 2350 if (reginfo->intuit || regtry(reginfo, &s)) {
ae3bb8ea
KW
2351 goto got_it;
2352 }
64935bc6 2353 }
73104a1b 2354 break;
64935bc6 2355
73104a1b
KW
2356 case LNBREAK:
2357 REXEC_FBC_CSCAN(is_LNBREAK_utf8_safe(s, strend),
2358 is_LNBREAK_latin1_safe(s, strend)
2359 );
2360 break;
3018b823
KW
2361
2362 /* The argument to all the POSIX node types is the class number to pass to
2363 * _generic_isCC() to build a mask for searching in PL_charclass[] */
2364
2365 case NPOSIXL:
2366 to_complement = 1;
2367 /* FALLTHROUGH */
2368
2369 case POSIXL:
780fcc9f 2370 _CHECK_AND_WARN_PROBLEMATIC_LOCALE;
3018b823
KW
2371 REXEC_FBC_CSCAN(to_complement ^ cBOOL(isFOO_utf8_lc(FLAGS(c), (U8 *) s)),
2372 to_complement ^ cBOOL(isFOO_lc(FLAGS(c), *s)));
73104a1b 2373 break;
3018b823
KW
2374
2375 case NPOSIXD:
2376 to_complement = 1;
2377 /* FALLTHROUGH */
2378
2379 case POSIXD:
2380 if (utf8_target) {
2381 goto posix_utf8;
2382 }
2383 goto posixa;
2384
2385 case NPOSIXA:
2386 if (utf8_target) {
2387 /* The complement of something that matches only ASCII matches all
837226c8 2388 * non-ASCII, plus everything in ASCII that isn't in the class. */
7a207065 2389 REXEC_FBC_UTF8_CLASS_SCAN( ! isASCII_utf8_safe(s, strend)
3018b823
KW
2390 || ! _generic_isCC_A(*s, FLAGS(c)));
2391 break;
2392 }
2393
2394 to_complement = 1;
2395 /* FALLTHROUGH */
2396
73104a1b 2397 case POSIXA:
3018b823 2398 posixa:
73104a1b 2399 /* Don't need to worry about utf8, as it can match only a single
3018b823
KW
2400 * byte invariant character. */
2401 REXEC_FBC_CLASS_SCAN(
2402 to_complement ^ cBOOL(_generic_isCC_A(*s, FLAGS(c))));
73104a1b 2403 break;
3018b823
KW
2404
2405 case NPOSIXU:
2406 to_complement = 1;
2407 /* FALLTHROUGH */
2408
2409 case POSIXU:
2410 if (! utf8_target) {
2411 REXEC_FBC_CLASS_SCAN(to_complement ^ cBOOL(_generic_isCC(*s,
2412 FLAGS(c))));
2413 }
2414 else {
2415
c52b8b12 2416 posix_utf8:
3018b823
KW
2417 classnum = (_char_class_number) FLAGS(c);
2418 if (classnum < _FIRST_NON_SWASH_CC) {
2419 while (s < strend) {
2420
2421 /* We avoid loading in the swash as long as possible, but
2422 * should we have to, we jump to a separate loop. This
2423 * extra 'if' statement is what keeps this code from being
2424 * just a call to REXEC_FBC_UTF8_CLASS_SCAN() */
2425 if (UTF8_IS_ABOVE_LATIN1(*s)) {
2426 goto found_above_latin1;
2427 }
2428 if ((UTF8_IS_INVARIANT(*s)
2429 && to_complement ^ cBOOL(_generic_isCC((U8) *s,
2430 classnum)))
042d9e50 2431 || ( UTF8_IS_NEXT_CHAR_DOWNGRADEABLE(s, strend)
3018b823 2432 && to_complement ^ cBOOL(
a62b247b 2433 _generic_isCC(EIGHT_BIT_UTF8_TO_NATIVE(*s,
94bb8c36 2434 *(s + 1)),
3018b823
KW
2435 classnum))))
2436 {
02d5137b 2437 if (tmp && (reginfo->intuit || regtry(reginfo, &s)))
3018b823
KW
2438 goto got_it;
2439 else {
2440 tmp = doevery;
2441 }
2442 }
2443 else {
2444 tmp = 1;
2445 }
2446 s += UTF8SKIP(s);
2447 }
2448 }
2449 else switch (classnum) { /* These classes are implemented as
2450 macros */
779cf272 2451 case _CC_ENUM_SPACE:
3018b823 2452 REXEC_FBC_UTF8_CLASS_SCAN(
7a207065 2453 to_complement ^ cBOOL(isSPACE_utf8_safe(s, strend)));
3018b823
KW
2454 break;
2455
2456 case _CC_ENUM_BLANK:
2457 REXEC_FBC_UTF8_CLASS_SCAN(
7a207065 2458 to_complement ^ cBOOL(isBLANK_utf8_safe(s, strend)));
3018b823
KW
2459 break;
2460
2461 case _CC_ENUM_XDIGIT:
2462 REXEC_FBC_UTF8_CLASS_SCAN(
7a207065 2463 to_complement ^ cBOOL(isXDIGIT_utf8_safe(s, strend)));
3018b823
KW
2464 break;
2465
2466 case _CC_ENUM_VERTSPACE:
2467 REXEC_FBC_UTF8_CLASS_SCAN(
7a207065 2468 to_complement ^ cBOOL(isVERTWS_utf8_safe(s, strend)));
3018b823
KW
2469 break;
2470
2471 case _CC_ENUM_CNTRL:
2472 REXEC_FBC_UTF8_CLASS_SCAN(
7a207065 2473 to_complement ^ cBOOL(isCNTRL_utf8_safe(s, strend)));
3018b823
KW
2474 break;
2475
2476 default:
2477 Perl_croak(aTHX_ "panic: find_byclass() node %d='%s' has an unexpected character class '%d'", OP(c), PL_reg_name[OP(c)], classnum);
e5964223 2478 NOT_REACHED; /* NOTREACHED */
3018b823
KW
2479 }
2480 }
2481 break;
2482
2483 found_above_latin1: /* Here we have to load a swash to get the result
2484 for the current code point */
2485 if (! PL_utf8_swash_ptrs[classnum]) {
2486 U8 flags = _CORE_SWASH_INIT_ACCEPT_INVLIST;
2487 PL_utf8_swash_ptrs[classnum] =
2a16ac92
KW
2488 _core_swash_init("utf8",
2489 "",
2490 &PL_sv_undef, 1, 0,
2491 PL_XPosix_ptrs[classnum], &flags);
3018b823
KW
2492 }
2493
2494 /* This is a copy of the loop above for swash classes, though using the
2495 * FBC macro instead of being expanded out. Since we've loaded the
2496 * swash, we don't have to check for that each time through the loop */
2497 REXEC_FBC_UTF8_CLASS_SCAN(
7a207065 2498 to_complement ^ cBOOL(_generic_utf8_safe(
3018b823
KW
2499 classnum,
2500 s,
7a207065 2501 strend,
3018b823
KW
2502 swash_fetch(PL_utf8_swash_ptrs[classnum],
2503 (U8 *) s, TRUE))));
73104a1b
KW
2504 break;
2505
2506 case AHOCORASICKC:
2507 case AHOCORASICK:
2508 {
2509 DECL_TRIE_TYPE(c);
2510 /* what trie are we using right now */
2511 reg_ac_data *aho = (reg_ac_data*)progi->data->data[ ARG( c ) ];
2512 reg_trie_data *trie = (reg_trie_data*)progi->data->data[ aho->trie ];
2513 HV *widecharmap = MUTABLE_HV(progi->data->data[ aho->trie + 1 ]);
2514
2515 const char *last_start = strend - trie->minlen;
6148ee25 2516#ifdef DEBUGGING
73104a1b 2517 const char *real_start = s;
6148ee25 2518#endif
73104a1b
KW
2519 STRLEN maxlen = trie->maxlen;
2520 SV *sv_points;
2521 U8 **points; /* map of where we were in the input string
2522 when reading a given char. For ASCII this
2523 is unnecessary overhead as the relationship
2524 is always 1:1, but for Unicode, especially
2525 case folded Unicode this is not true. */
2526 U8 foldbuf[ UTF8_MAXBYTES_CASE + 1 ];
2527 U8 *bitmap=NULL;
2528
2529
2530 GET_RE_DEBUG_FLAGS_DECL;
2531
2532 /* We can't just allocate points here. We need to wrap it in
2533 * an SV so it gets freed properly if there is a croak while
2534 * running the match */
2535 ENTER;
2536 SAVETMPS;
2537 sv_points=newSV(maxlen * sizeof(U8 *));
2538 SvCUR_set(sv_points,
2539 maxlen * sizeof(U8 *));
2540 SvPOK_on(sv_points);
2541 sv_2mortal(sv_points);
2542 points=(U8**)SvPV_nolen(sv_points );
2543 if ( trie_type != trie_utf8_fold
2544 && (trie->bitmap || OP(c)==AHOCORASICKC) )
2545 {
2546 if (trie->bitmap)
2547 bitmap=(U8*)trie->bitmap;
2548 else
2549 bitmap=(U8*)ANYOF_BITMAP(c);
2550 }
2551 /* this is the Aho-Corasick algorithm modified a touch
2552 to include special handling for long "unknown char" sequences.
2553 The basic idea being that we use AC as long as we are dealing
2554 with a possible matching char, when we encounter an unknown char
2555 (and we have not encountered an accepting state) we scan forward
2556 until we find a legal starting char.
2557 AC matching is basically that of trie matching, except that when
2558 we encounter a failing transition, we fall back to the current
2559 states "fail state", and try the current char again, a process
2560 we repeat until we reach the root state, state 1, or a legal
2561 transition. If we fail on the root state then we can either
2562 terminate if we have reached an accepting state previously, or
2563 restart the entire process from the beginning if we have not.
2564
2565 */
2566 while (s <= last_start) {
2567 const U32 uniflags = UTF8_ALLOW_DEFAULT;
2568 U8 *uc = (U8*)s;
2569 U16 charid = 0;
2570 U32 base = 1;
2571 U32 state = 1;
2572 UV uvc = 0;
2573 STRLEN len = 0;
2574 STRLEN foldlen = 0;
2575 U8 *uscan = (U8*)NULL;
2576 U8 *leftmost = NULL;
2577#ifdef DEBUGGING
2578 U32 accepted_word= 0;
786e8c11 2579#endif
73104a1b
KW
2580 U32 pointpos = 0;
2581
2582 while ( state && uc <= (U8*)strend ) {
2583 int failed=0;
2584 U32 word = aho->states[ state ].wordnum;
2585
2586 if( state==1 ) {
2587 if ( bitmap ) {
2588 DEBUG_TRIE_EXECUTE_r(
2589 if ( uc <= (U8*)last_start && !BITMAP_TEST(bitmap,*uc) ) {
2590 dump_exec_pos( (char *)uc, c, strend, real_start,
cb41e5d6 2591 (char *)uc, utf8_target, 0 );
6ad9a8ab 2592 Perl_re_printf( aTHX_
73104a1b
KW
2593 " Scanning for legal start char...\n");
2594 }
2595 );
2596 if (utf8_target) {
2597 while ( uc <= (U8*)last_start && !BITMAP_TEST(bitmap,*uc) ) {
2598 uc += UTF8SKIP(uc);
2599 }
2600 } else {
2601 while ( uc <= (U8*)last_start && !BITMAP_TEST(bitmap,*uc) ) {
2602 uc++;
2603 }
786e8c11 2604 }
73104a1b 2605 s= (char *)uc;
07be1b83 2606 }
73104a1b
KW
2607 if (uc >(U8*)last_start) break;
2608 }
2609
2610 if ( word ) {
2611 U8 *lpos= points[ (pointpos - trie->wordinfo[word].len) % maxlen ];
2612 if (!leftmost || lpos < leftmost) {
2613 DEBUG_r(accepted_word=word);
2614 leftmost= lpos;
7016d6eb 2615 }
73104a1b 2616 if (base==0) break;
7016d6eb 2617
73104a1b
KW
2618 }
2619 points[pointpos++ % maxlen]= uc;
2620 if (foldlen || uc < (U8*)strend) {
2621 REXEC_TRIE_READ_CHAR(trie_type, trie,
2622 widecharmap, uc,
2623 uscan, len, uvc, charid, foldlen,
2624 foldbuf, uniflags);
2625 DEBUG_TRIE_EXECUTE_r({
2626 dump_exec_pos( (char *)uc, c, strend,
cb41e5d6 2627 real_start, s, utf8_target, 0);
6ad9a8ab 2628 Perl_re_printf( aTHX_
147e3846 2629 " Charid:%3u CP:%4" UVxf " ",
73104a1b
KW
2630 charid, uvc);
2631 });
2632 }
2633 else {
2634 len = 0;
2635 charid = 0;
2636 }
07be1b83 2637
73104a1b
KW
2638
2639 do {
6148ee25 2640#ifdef DEBUGGING
73104a1b 2641 word = aho->states[ state ].wordnum;
6148ee25 2642#endif
73104a1b
KW
2643 base = aho->states[ state ].trans.base;
2644
2645 DEBUG_TRIE_EXECUTE_r({
2646 if (failed)
2647 dump_exec_pos( (char *)uc, c, strend, real_start,
cb41e5d6 2648 s, utf8_target, 0 );
6ad9a8ab 2649 Perl_re_printf( aTHX_
147e3846 2650 "%sState: %4" UVxf ", word=%" UVxf,
73104a1b
KW
2651 failed ? " Fail transition to " : "",
2652 (UV)state, (UV)word);
2653 });
2654 if ( base ) {
2655 U32 tmp;
2656 I32 offset;
2657 if (charid &&
2658 ( ((offset = base + charid
2659 - 1 - trie->uniquecharcount)) >= 0)
2660 && ((U32)offset < trie->lasttrans)
2661 && trie->trans[offset].check == state
2662 && (tmp=trie->trans[offset].next))
2663 {
2664 DEBUG_TRIE_EXECUTE_r(
6ad9a8ab 2665 Perl_re_printf( aTHX_ " - legal\n"));
73104a1b
KW
2666 state = tmp;
2667 break;
07be1b83
YO
2668 }
2669 else {
786e8c11 2670 DEBUG_TRIE_EXECUTE_r(
6ad9a8ab 2671 Perl_re_printf( aTHX_ " - fail\n"));
786e8c11 2672 failed = 1;
73104a1b 2673 state = aho->fail[state];
07be1b83 2674 }
07be1b83 2675 }
73104a1b
KW
2676 else {
2677 /* we must be accepting here */
2678 DEBUG_TRIE_EXECUTE_r(
6ad9a8ab 2679 Perl_re_printf( aTHX_ " - accepting\n"));
73104a1b
KW
2680 failed = 1;
2681 break;
786e8c11 2682 }
73104a1b
KW
2683 } while(state);
2684 uc += len;
2685 if (failed) {
2686 if (leftmost)
2687 break;
2688 if (!state) state = 1;
07be1b83 2689 }
73104a1b
KW
2690 }
2691 if ( aho->states[ state ].wordnum ) {
2692 U8 *lpos = points[ (pointpos - trie->wordinfo[aho->states[ state ].wordnum].len) % maxlen ];
2693 if (!leftmost || lpos < leftmost) {
2694 DEBUG_r(accepted_word=aho->states[ state ].wordnum);
2695 leftmost = lpos;
07be1b83
YO
2696 }
2697 }
73104a1b
KW
2698 if (leftmost) {
2699 s = (char*)leftmost;
2700 DEBUG_TRIE_EXECUTE_r({
147e3846 2701 Perl_re_printf( aTHX_ "Matches word #%" UVxf " at position %" IVdf ". Trying full pattern...\n",
73104a1b
KW
2702 (UV)accepted_word, (IV)(s - real_start)
2703 );
2704 });
02d5137b 2705 if (reginfo->intuit || regtry(reginfo, &s)) {
73104a1b
KW
2706 FREETMPS;
2707 LEAVE;
2708 goto got_it;
2709 }
2710 s = HOPc(s,1);
2711 DEBUG_TRIE_EXECUTE_r({
6ad9a8ab 2712 Perl_re_printf( aTHX_ "Pattern failed. Looking for new start point...\n");
73104a1b
KW
2713 });
2714 } else {
2715 DEBUG_TRIE_EXECUTE_r(
6ad9a8ab 2716 Perl_re_printf( aTHX_ "No match.\n"));
73104a1b
KW
2717 break;
2718 }
2719 }
2720 FREETMPS;
2721 LEAVE;
2722 }
2723 break;
2724 default:
2725 Perl_croak(aTHX_ "panic: unknown regstclass %d", (int)OP(c));
73104a1b
KW
2726 }
2727 return 0;
2728 got_it:
2729 return s;
6eb5f6b9
JH
2730}
2731
60165aa4
DM
2732/* set RX_SAVED_COPY, RX_SUBBEG etc.
2733 * flags have same meanings as with regexec_flags() */
2734
749f4950
DM
2735static void
2736S_reg_set_capture_string(pTHX_ REGEXP * const rx,
60165aa4
DM
2737 char *strbeg,
2738 char *strend,
2739 SV *sv,
2740 U32 flags,
2741 bool utf8_target)
2742{
2743 struct regexp *const prog = ReANY(rx);
2744
60165aa4
DM
2745 if (flags & REXEC_COPY_STR) {
2746#ifdef PERL_ANY_COW
2747 if (SvCANCOW(sv)) {
eb8fc9fe 2748 DEBUG_C(Perl_re_printf( aTHX_
60165aa4 2749 "Copy on write: regexp capture, type %d\n",
eb8fc9fe 2750 (int) SvTYPE(sv)));
5411a0e5
DM
2751 /* Create a new COW SV to share the match string and store
2752 * in saved_copy, unless the current COW SV in saved_copy
2753 * is valid and suitable for our purpose */
2754 if (( prog->saved_copy
2755 && SvIsCOW(prog->saved_copy)
2756 && SvPOKp(prog->saved_copy)
2757 && SvIsCOW(sv)
2758 && SvPOKp(sv)
2759 && SvPVX(sv) == SvPVX(prog->saved_copy)))
a76b0e90 2760 {
5411a0e5
DM
2761 /* just reuse saved_copy SV */
2762 if (RXp_MATCH_COPIED(prog)) {
2763 Safefree(prog->subbeg);
2764 RXp_MATCH_COPIED_off(prog);
2765 }
2766 }
2767 else {
2768 /* create new COW SV to share string */
a76b0e90
DM
2769 RX_MATCH_COPY_FREE(rx);
2770 prog->saved_copy = sv_setsv_cow(prog->saved_copy, sv);
a76b0e90 2771 }
5411a0e5
DM
2772 prog->subbeg = (char *)SvPVX_const(prog->saved_copy);
2773 assert (SvPOKp(prog->saved_copy));
60165aa4
DM
2774 prog->sublen = strend - strbeg;
2775 prog->suboffset = 0;
2776 prog->subcoffset = 0;
2777 } else
2778#endif
2779 {
99a90e59
FC
2780 SSize_t min = 0;
2781 SSize_t max = strend - strbeg;
ea3daa5d 2782 SSize_t sublen;
60165aa4
DM
2783
2784 if ( (flags & REXEC_COPY_SKIP_POST)
e322109a 2785 && !(prog->extflags & RXf_PMf_KEEPCOPY) /* //p */
60165aa4
DM
2786 && !(PL_sawampersand & SAWAMPERSAND_RIGHT)
2787 ) { /* don't copy $' part of string */
2788 U32 n = 0;
2789 max = -1;
2790 /* calculate the right-most part of the string covered
f67a5002 2791 * by a capture. Due to lookahead, this may be to
60165aa4
DM
2792 * the right of $&, so we have to scan all captures */
2793 while (n <= prog->lastparen) {
2794 if (prog->offs[n].end > max)
2795 max = prog->offs[n].end;
2796 n++;
2797 }
2798 if (max == -1)
2799 max = (PL_sawampersand & SAWAMPERSAND_LEFT)
2800 ? prog->offs[0].start
2801 : 0;
2802 assert(max >= 0 && max <= strend - strbeg);
2803 }
2804
2805 if ( (flags & REXEC_COPY_SKIP_PRE)
e322109a 2806 && !(prog->extflags & RXf_PMf_KEEPCOPY) /* //p */
60165aa4
DM
2807 && !(PL_sawampersand & SAWAMPERSAND_LEFT)
2808 ) { /* don't copy $` part of string */
2809 U32 n = 0;
2810 min = max;
2811 /* calculate the left-most part of the string covered
f67a5002 2812 * by a capture. Due to lookbehind, this may be to
60165aa4
DM
2813 * the left of $&, so we have to scan all captures */
2814 while (min && n <= prog->lastparen) {
2815 if ( prog->offs[n].start != -1
2816 && prog->offs[n].start < min)
2817 {
2818 min = prog->offs[n].start;
2819 }
2820 n++;
2821 }
2822 if ((PL_sawampersand & SAWAMPERSAND_RIGHT)
2823 && min > prog->offs[0].end
2824 )
2825 min = prog->offs[0].end;
2826
2827 }
2828
2829 assert(min >= 0 && min <= max && min <= strend - strbeg);
2830 sublen = max - min;
2831
2832 if (RX_MATCH_COPIED(rx)) {
2833 if (sublen > prog->sublen)
2834 prog->subbeg =
2835 (char*)saferealloc(prog->subbeg, sublen+1);
2836 }
2837 else
2838 prog->subbeg = (char*)safemalloc(sublen+1);
2839 Copy(strbeg + min, prog->subbeg, sublen, char);
2840 prog->subbeg[sublen] = '\0';
2841 prog->suboffset = min;
2842 prog->sublen = sublen;
2843 RX_MATCH_COPIED_on(rx);
2844 }
2845 prog->subcoffset = prog->suboffset;
2846 if (prog->suboffset && utf8_target) {
2847 /* Convert byte offset to chars.
2848 * XXX ideally should only compute this if @-/@+
2849 * has been seen, a la PL_sawampersand ??? */
2850
2851 /* If there's a direct correspondence between the
2852 * string which we're matching and the original SV,
2853 * then we can use the utf8 len cache associated with
2854 * the SV. In particular, it means that under //g,
2855 * sv_pos_b2u() will use the previously cached
2856 * position to speed up working out the new length of
2857 * subcoffset, rather than counting from the start of
2858 * the string each time. This stops
2859 * $x = "\x{100}" x 1E6; 1 while $x =~ /(.)/g;
2860 * from going quadratic */
2861 if (SvPOKp(sv) && SvPVX(sv) == strbeg)
ea3daa5d
FC
2862 prog->subcoffset = sv_pos_b2u_flags(sv, prog->subcoffset,
2863 SV_GMAGIC|SV_CONST_RETURN);
60165aa4
DM
2864 else
2865 prog->subcoffset = utf8_length((U8*)strbeg,
2866 (U8*)(strbeg+prog->suboffset));
2867 }
2868 }
2869 else {
2870 RX_MATCH_COPY_FREE(rx);
2871 prog->subbeg = strbeg;
2872 prog->suboffset = 0;
2873 prog->subcoffset = 0;
2874 prog->sublen = strend - strbeg;
2875 }
2876}
2877
2878
2879
fae667d5 2880
6eb5f6b9
JH
2881/*
2882 - regexec_flags - match a regexp against a string
2883 */
2884I32
5aaab254 2885Perl_regexec_flags(pTHX_ REGEXP * const rx, char *stringarg, char *strend,
ea3daa5d 2886 char *strbeg, SSize_t minend, SV *sv, void *data, U32 flags)
8fd1a950
DM
2887/* stringarg: the point in the string at which to begin matching */
2888/* strend: pointer to null at end of string */
2889/* strbeg: real beginning of string */
2890/* minend: end of match must be >= minend bytes after stringarg. */
2891/* sv: SV being matched: only used for utf8 flag, pos() etc; string
2892 * itself is accessed via the pointers above */
2893/* data: May be used for some additional optimizations.
d058ec57 2894 Currently unused. */
a340edde 2895/* flags: For optimizations. See REXEC_* in regexp.h */
8fd1a950 2896
6eb5f6b9 2897{
8d919b0a 2898 struct regexp *const prog = ReANY(rx);
5aaab254 2899 char *s;
eb578fdb 2900 regnode *c;
03c83e26 2901 char *startpos;
ea3daa5d
FC
2902 SSize_t minlen; /* must match at least this many chars */
2903 SSize_t dontbother = 0; /* how many characters not to try at end */
f2ed9b32 2904 const bool utf8_target = cBOOL(DO_UTF8(sv));
2757e526 2905 I32 multiline;
f8fc2ecf 2906 RXi_GET_DECL(prog,progi);
02d5137b
DM
2907 regmatch_info reginfo_buf; /* create some info to pass to regtry etc */
2908 regmatch_info *const reginfo = &reginfo_buf;
e9105d30 2909 regexp_paren_pair *swap = NULL;
006f26b2 2910 I32 oldsave;
a3621e74
YO
2911 GET_RE_DEBUG_FLAGS_DECL;
2912
7918f24d 2913 PERL_ARGS_ASSERT_REGEXEC_FLAGS;
9d4ba2ae 2914 PERL_UNUSED_ARG(data);
6eb5f6b9
JH
2915
2916 /* Be paranoid... */
3dc78631 2917 if (prog == NULL) {
6eb5f6b9 2918 Perl_croak(aTHX_ "NULL regexp parameter");
6eb5f6b9
JH
2919 }
2920
6c3fea77 2921 DEBUG_EXECUTE_r(
03c83e26 2922 debug_start_match(rx, utf8_target, stringarg, strend,
6c3fea77
DM
2923 "Matching");
2924 );
8adc0f72 2925
b342a604
DM
2926 startpos = stringarg;
2927
4cf1a867
DM
2928 /* set these early as they may be used by the HOP macros below */
2929 reginfo->strbeg = strbeg;
2930 reginfo->strend = strend;
2931 reginfo->is_utf8_target = cBOOL(utf8_target);
2932
58430ea8 2933 if (prog->intflags & PREGf_GPOS_SEEN) {
d307c076
DM
2934 MAGIC *mg;
2935
fef7148b
DM
2936 /* set reginfo->ganch, the position where \G can match */
2937
2938 reginfo->ganch =
2939 (flags & REXEC_IGNOREPOS)
2940 ? stringarg /* use start pos rather than pos() */
3dc78631 2941 : ((mg = mg_find_mglob(sv)) && mg->mg_len >= 0)
25fdce4a
FC
2942 /* Defined pos(): */
2943 ? strbeg + MgBYTEPOS(mg, sv, strbeg, strend-strbeg)
fef7148b
DM
2944 : strbeg; /* pos() not defined; use start of string */
2945
6ad9a8ab 2946 DEBUG_GPOS_r(Perl_re_printf( aTHX_
147e3846 2947 "GPOS ganch set to strbeg[%" IVdf "]\n", (IV)(reginfo->ganch - strbeg)));
fef7148b 2948
03c83e26
DM
2949 /* in the presence of \G, we may need to start looking earlier in
2950 * the string than the suggested start point of stringarg:
0b2c2a84 2951 * if prog->gofs is set, then that's a known, fixed minimum
03c83e26
DM
2952 * offset, such as
2953 * /..\G/: gofs = 2
2954 * /ab|c\G/: gofs = 1
2955 * or if the minimum offset isn't known, then we have to go back
2956 * to the start of the string, e.g. /w+\G/
2957 */
2bfbe302 2958
8e1490ee 2959 if (prog->intflags & PREGf_ANCH_GPOS) {
4cf1a867
DM
2960 if (prog->gofs) {
2961 startpos = HOPBACKc(reginfo->ganch, prog->gofs);
2962 if (!startpos ||
2963 ((flags & REXEC_FAIL_ON_UNDERFLOW) && startpos < stringarg))
2964 {
6ad9a8ab 2965 DEBUG_r(Perl_re_printf( aTHX_
4cf1a867
DM
2966 "fail: ganch-gofs before earliest possible start\n"));
2967 return 0;
2968 }
2bfbe302 2969 }
4cf1a867
DM
2970 else
2971 startpos = reginfo->ganch;
2bfbe302
DM
2972 }
2973 else if (prog->gofs) {
4cf1a867
DM
2974 startpos = HOPBACKc(startpos, prog->gofs);
2975 if (!startpos)
b342a604 2976 startpos = strbeg;
03c83e26 2977 }
58430ea8 2978 else if (prog->intflags & PREGf_GPOS_FLOAT)
b342a604 2979 startpos = strbeg;
03c83e26
DM
2980 }
2981
2982 minlen = prog->minlen;
b342a604 2983 if ((startpos + minlen) > strend || startpos < strbeg) {
6ad9a8ab 2984 DEBUG_r(Perl_re_printf( aTHX_
03c83e26
DM
2985 "Regex match can't succeed, so not even tried\n"));
2986 return 0;
2987 }
2988
63a3746a
DM
2989 /* at the end of this function, we'll do a LEAVE_SCOPE(oldsave),
2990 * which will call destuctors to reset PL_regmatch_state, free higher
2991 * PL_regmatch_slabs, and clean up regmatch_info_aux and
2992 * regmatch_info_aux_eval */
2993
2994 oldsave = PL_savestack_ix;
2995
dfa77d06
DM
2996 s = startpos;
2997
e322109a 2998 if ((prog->extflags & RXf_USE_INTUIT)
7fadf4a7
DM
2999 && !(flags & REXEC_CHECKED))
3000 {
dfa77d06 3001 s = re_intuit_start(rx, sv, strbeg, startpos, strend,
7fadf4a7 3002 flags, NULL);
dfa77d06 3003 if (!s)
7fadf4a7
DM
3004 return 0;
3005
e322109a 3006 if (prog->extflags & RXf_CHECK_ALL) {
7fadf4a7
DM
3007 /* we can match based purely on the result of INTUIT.
3008 * Set up captures etc just for $& and $-[0]
3009 * (an intuit-only match wont have $1,$2,..) */
3010 assert(!prog->nparens);
d5e7783a
DM
3011
3012 /* s/// doesn't like it if $& is earlier than where we asked it to
3013 * start searching (which can happen on something like /.\G/) */
3014 if ( (flags & REXEC_FAIL_ON_UNDERFLOW)
3015 && (s < stringarg))
3016 {
3017 /* this should only be possible under \G */
58430ea8 3018 assert(prog->intflags & PREGf_GPOS_SEEN);
6ad9a8ab 3019 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
d5e7783a
DM
3020 "matched, but failing for REXEC_FAIL_ON_UNDERFLOW\n"));
3021 goto phooey;
3022 }
3023
7fadf4a7
DM
3024 /* match via INTUIT shouldn't have any captures.
3025 * Let @-, @+, $^N know */
3026 prog->lastparen = prog->lastcloseparen = 0;
3027 RX_MATCH_UTF8_set(rx, utf8_target);
3ff69bd6
DM
3028 prog->offs[0].start = s - strbeg;
3029 prog->offs[0].end = utf8_target
3030 ? (char*)utf8_hop((U8*)s, prog->minlenret) - strbeg
3031 : s - strbeg + prog->minlenret;
7fadf4a7 3032 if ( !(flags & REXEC_NOT_FIRST) )
749f4950 3033 S_reg_set_capture_string(aTHX_ rx,
7fadf4a7
DM
3034 strbeg, strend,
3035 sv, flags, utf8_target);
3036
7fadf4a7
DM
3037 return 1;
3038 }
3039 }
3040
6c3fea77 3041 multiline = prog->extflags & RXf_PMf_MULTILINE;
1de06328 3042
dfa77d06 3043 if (strend - s < (minlen+(prog->check_offset_min<0?prog->check_offset_min:0))) {
6ad9a8ab 3044 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
a72c7584
JH
3045 "String too short [regexec_flags]...\n"));
3046 goto phooey;
1aa99e6b 3047 }
1de06328 3048
6eb5f6b9 3049 /* Check validity of program. */
f8fc2ecf 3050 if (UCHARAT(progi->program) != REG_MAGIC) {
6eb5f6b9
JH
3051 Perl_croak(aTHX_ "corrupted regexp program");
3052 }
3053
1738e041 3054 RX_MATCH_TAINTED_off(rx);
ab4e48c1 3055 RX_MATCH_UTF8_set(rx, utf8_target);
1738e041 3056
6c3fea77
DM
3057 reginfo->prog = rx; /* Yes, sorry that this is confusing. */
3058 reginfo->intuit = 0;
02d5137b
DM
3059 reginfo->is_utf8_pat = cBOOL(RX_UTF8(rx));
3060 reginfo->warned = FALSE;
02d5137b 3061 reginfo->sv = sv;
1cb48e53 3062 reginfo->poscache_maxiter = 0; /* not yet started a countdown */
6eb5f6b9 3063 /* see how far we have to get to not match where we matched before */
fe3974be 3064 reginfo->till = stringarg + minend;
6eb5f6b9 3065
60779a30 3066 if (prog->extflags & RXf_EVAL_SEEN && SvPADTMP(sv)) {
82c23608
FC
3067 /* SAVEFREESV, not sv_mortalcopy, as this SV must last until after
3068 S_cleanup_regmatch_info_aux has executed (registered by
3069 SAVEDESTRUCTOR_X below). S_cleanup_regmatch_info_aux modifies
3070 magic belonging to this SV.
3071 Not newSVsv, either, as it does not COW.
3072 */
3073 reginfo->sv = newSV(0);
4cba5ac0 3074 SvSetSV_nosteal(reginfo->sv, sv);
82c23608
FC
3075 SAVEFREESV(reginfo->sv);
3076 }
3077
331b2dcc
DM
3078 /* reserve next 2 or 3 slots in PL_regmatch_state:
3079 * slot N+0: may currently be in use: skip it
3080 * slot N+1: use for regmatch_info_aux struct
3081 * slot N+2: use for regmatch_info_aux_eval struct if we have (?{})'s
3082 * slot N+3: ready for use by regmatch()
3083 */
bf2039a9 3084
331b2dcc
DM
3085 {
3086 regmatch_state *old_regmatch_state;
3087 regmatch_slab *old_regmatch_slab;
3088 int i, max = (prog->extflags & RXf_EVAL_SEEN) ? 2 : 1;
3089
3090 /* on first ever match, allocate first slab */
3091 if (!PL_regmatch_slab) {
3092 Newx(PL_regmatch_slab, 1, regmatch_slab);
3093 PL_regmatch_slab->prev = NULL;
3094 PL_regmatch_slab->next = NULL;
3095 PL_regmatch_state = SLAB_FIRST(PL_regmatch_slab);
3096 }
bf2039a9 3097
331b2dcc
DM
3098 old_regmatch_state = PL_regmatch_state;
3099 old_regmatch_slab = PL_regmatch_slab;
bf2039a9 3100
331b2dcc
DM
3101 for (i=0; i <= max; i++) {
3102 if (i == 1)
3103 reginfo->info_aux = &(PL_regmatch_state->u.info_aux);
3104 else if (i ==2)
3105 reginfo->info_aux_eval =
3106 reginfo->info_aux->info_aux_eval =
3107 &(PL_regmatch_state->u.info_aux_eval);
bf2039a9 3108
331b2dcc
DM
3109 if (++PL_regmatch_state > SLAB_LAST(PL_regmatch_slab))
3110 PL_regmatch_state = S_push_slab(aTHX);
3111 }
bf2039a9 3112
331b2dcc
DM
3113 /* note initial PL_regmatch_state position; at end of match we'll
3114 * pop back to there and free any higher slabs */
bf2039a9 3115
331b2dcc
DM
3116 reginfo->info_aux->old_regmatch_state = old_regmatch_state;
3117 reginfo->info_aux->old_regmatch_slab = old_regmatch_slab;
2ac8ff4b 3118 reginfo->info_aux->poscache = NULL;
bf2039a9 3119
331b2dcc 3120 SAVEDESTRUCTOR_X(S_cleanup_regmatch_info_aux, reginfo->info_aux);
bf2039a9 3121
331b2dcc
DM
3122 if ((prog->extflags & RXf_EVAL_SEEN))
3123 S_setup_eval_state(aTHX_ reginfo);
3124 else
3125 reginfo->info_aux_eval = reginfo->info_aux->info_aux_eval = NULL;
bf2039a9 3126 }
d3aa529c 3127
6eb5f6b9 3128 /* If there is a "must appear" string, look for it. */
6eb5f6b9 3129
288b8c02 3130 if (PL_curpm && (PM_GETRE(PL_curpm) == rx)) {
e9105d30
GG
3131 /* We have to be careful. If the previous successful match
3132 was from this regex we don't want a subsequent partially
3133 successful match to clobber the old results.
3134 So when we detect this possibility we add a swap buffer
d8da0584
KW
3135 to the re, and switch the buffer each match. If we fail,
3136 we switch it back; otherwise we leave it swapped.
e9105d30
GG
3137 */
3138 swap = prog->offs;
3139 /* do we need a save destructor here for eval dies? */
3140 Newxz(prog->offs, (prog->nparens + 1), regexp_paren_pair);
2b1a3689 3141 DEBUG_BUFFERS_r(Perl_re_exec_indentf( aTHX_
147e3846 3142 "rex=0x%" UVxf " saving offs: orig=0x%" UVxf " new=0x%" UVxf "\n",
2b1a3689
YO
3143 0,
3144 PTR2UV(prog),
495f47a5
DM
3145 PTR2UV(swap),
3146 PTR2UV(prog->offs)
3147 ));
c74340f9 3148 }
6eb5f6b9 3149
ba6840fb
YO
3150 if (prog->recurse_locinput)
3151 Zero(prog->recurse_locinput,prog->nparens + 1, char *);
3152
0fa70a06
DM
3153 /* Simplest case: anchored match need be tried only once, or with
3154 * MBOL, only at the beginning of each line.
3155 *
3156 * Note that /.*.../ sets PREGf_IMPLICIT|MBOL, while /.*.../s sets
3157 * PREGf_IMPLICIT|SBOL. The idea is that with /.*.../s, if it doesn't
3158 * match at the start of the string then it won't match anywhere else
3159 * either; while with /.*.../, if it doesn't match at the beginning,
3160 * the earliest it could match is at the start of the next line */
3161
8e1490ee 3162 if (prog->intflags & (PREGf_ANCH & ~PREGf_ANCH_GPOS)) {
0fa70a06
DM
3163 char *end;
3164
3165 if (regtry(reginfo, &s))
6eb5f6b9 3166 goto got_it;
0fa70a06
DM
3167
3168 if (!(prog->intflags & PREGf_ANCH_MBOL))
3169 goto phooey;
3170
3171 /* didn't match at start, try at other newline positions */
3172
3173 if (minlen)
3174 dontbother = minlen - 1;
3175 end = HOP3c(strend, -dontbother, strbeg) - 1;
3176
3177 /* skip to next newline */
3178
3179 while (s <= end) { /* note it could be possible to match at the end of the string */
3180 /* NB: newlines are the same in unicode as they are in latin */
3181 if (*s++ != '\n')
3182 continue;
3183 if (prog->check_substr || prog->check_utf8) {
3184 /* note that with PREGf_IMPLICIT, intuit can only fail
3185 * or return the start position, so it's of limited utility.
3186 * Nevertheless, I made the decision that the potential for
3187 * quick fail was still worth it - DAPM */
3188 s = re_intuit_start(rx, sv, strbeg, s, strend, flags, NULL);
3189 if (!s)
3190 goto phooey;
3191 }
3192 if (regtry(reginfo, &s))
3193 goto got_it;
3194 }
3195 goto phooey;
3196 } /* end anchored search */
3197
3198 if (prog->intflags & PREGf_ANCH_GPOS)
f9f4320a 3199 {
a8430a8b
YO
3200 /* PREGf_ANCH_GPOS should never be true if PREGf_GPOS_SEEN is not true */
3201 assert(prog->intflags & PREGf_GPOS_SEEN);
2bfbe302
DM
3202 /* For anchored \G, the only position it can match from is
3203 * (ganch-gofs); we already set startpos to this above; if intuit
3204 * moved us on from there, we can't possibly succeed */
4cf1a867 3205 assert(startpos == HOPBACKc(reginfo->ganch, prog->gofs));
2bfbe302 3206 if (s == startpos && regtry(reginfo, &s))
6eb5f6b9
JH
3207 goto got_it;
3208 goto phooey;
3209 }
3210
3211 /* Messy cases: unanchored match. */
bbe252da 3212 if ((prog->anchored_substr || prog->anchored_utf8) && prog->intflags & PREGf_SKIP) {
6eb5f6b9 3213 /* we have /x+whatever/ */
984e6dd1 3214 /* it must be a one character string (XXXX Except is_utf8_pat?) */
33b8afdf 3215 char ch;
bf93d4cc
GS
3216#ifdef DEBUGGING
3217 int did_match = 0;
3218#endif
f2ed9b32 3219 if (utf8_target) {
7e0d5ad7
KW
3220 if (! prog->anchored_utf8) {
3221 to_utf8_substr(prog);
3222 }
3223 ch = SvPVX_const(prog->anchored_utf8)[0];
4cadc6a9 3224 REXEC_FBC_SCAN(
6eb5f6b9 3225 if (*s == ch) {
a3621e74 3226 DEBUG_EXECUTE_r( did_match = 1 );
02d5137b 3227 if (regtry(reginfo, &s)) goto got_it;
6eb5f6b9
JH
3228 s += UTF8SKIP(s);
3229 while (s < strend && *s == ch)
3230 s += UTF8SKIP(s);
3231 }
4cadc6a9 3232 );
7e0d5ad7 3233
6eb5f6b9
JH
3234 }
3235 else {
7e0d5ad7
KW
3236 if (! prog->anchored_substr) {
3237 if (! to_byte_substr(prog)) {
6b54ddc5 3238 NON_UTF8_TARGET_BUT_UTF8_REQUIRED(phooey);
7e0d5ad7
KW
3239 }
3240 }
3241 ch = SvPVX_const(prog->anchored_substr)[0];
4cadc6a9 3242 REXEC_FBC_SCAN(
6eb5f6b9 3243 if (*s == ch) {
a3621e74 3244 DEBUG_EXECUTE_r( did_match = 1 );
02d5137b 3245 if (regtry(reginfo, &s)) goto got_it;
6eb5f6b9
JH
3246 s++;
3247 while (s < strend && *s == ch)
3248 s++;
3249 }
4cadc6a9 3250 );
6eb5f6b9 3251 }
a3621e74 3252 DEBUG_EXECUTE_r(if (!did_match)
6ad9a8ab 3253 Perl_re_printf( aTHX_
b7953727
JH
3254 "Did not find anchored character...\n")
3255 );
6eb5f6b9 3256 }
a0714e2c
SS
3257 else if (prog->anchored_substr != NULL
3258 || prog->anchored_utf8 != NULL
3259 || ((prog->float_substr != NULL || prog->float_utf8 != NULL)
33b8afdf
JH
3260 && prog->float_max_offset < strend - s)) {
3261 SV *must;
ea3daa5d
FC
3262 SSize_t back_max;
3263 SSize_t back_min;
33b8afdf 3264 char *last;
6eb5f6b9 3265 char *last1; /* Last position checked before */
bf93d4cc
GS
3266#ifdef DEBUGGING
3267 int did_match = 0;
3268#endif
33b8afdf 3269 if (prog->anchored_substr || prog->anchored_utf8) {
7e0d5ad7
KW
3270 if (utf8_target) {
3271 if (! prog->anchored_utf8) {
3272 to_utf8_substr(prog);
3273 }
3274 must = prog->anchored_utf8;
3275 }
3276 else {
3277 if (! prog->anchored_substr) {
3278 if (! to_byte_substr(prog)) {
6b54ddc5 3279 NON_UTF8_TARGET_BUT_UTF8_REQUIRED(phooey);
7e0d5ad7
KW
3280 }
3281 }
3282 must = prog->anchored_substr;
3283 }
33b8afdf
JH
3284 back_max = back_min = prog->anchored_offset;
3285 } else {
7e0d5ad7
KW
3286 if (utf8_target) {
3287 if (! prog->float_utf8) {
3288 to_utf8_substr(prog);
3289 }
3290 must = prog->float_utf8;
3291 }
3292 else {
3293 if (! prog->float_substr) {
3294 if (! to_byte_substr(prog)) {
6b54ddc5 3295 NON_UTF8_TARGET_BUT_UTF8_REQUIRED(phooey);
7e0d5ad7
KW
3296 }
3297 }
3298 must = prog->float_substr;
3299 }
33b8afdf
JH
3300 back_max = prog->float_max_offset;
3301 back_min = prog->float_min_offset;
3302 }
1de06328 3303
1de06328
YO
3304 if (back_min<0) {
3305 last = strend;
3306 } else {
3307 last = HOP3c(strend, /* Cannot start after this */
ea3daa5d 3308 -(SSize_t)(CHR_SVLEN(must)
1de06328
YO
3309 - (SvTAIL(must) != 0) + back_min), strbeg);
3310 }
9d9163fb 3311 if (s > reginfo->strbeg)
6eb5f6b9
JH
3312 last1 = HOPc(s, -1);
3313 else
3314 last1 = s - 1; /* bogus */
3315
a0288114 3316 /* XXXX check_substr already used to find "s", can optimize if
6eb5f6b9 3317 check_substr==must. */
bf05793b 3318 dontbother = 0;
6eb5f6b9
JH
3319 strend = HOPc(strend, -dontbother);
3320 while ( (s <= last) &&
e50d57d4 3321 (s = fbm_instr((unsigned char*)HOP4c(s, back_min, strbeg, strend),
9041c2e3 3322 (unsigned char*)strend, must,
c33e64f0 3323 multiline ? FBMrf_MULTILINE : 0)) ) {
a3621e74 3324 DEBUG_EXECUTE_r( did_match = 1 );
6eb5f6b9
JH
3325 if (HOPc(s, -back_max) > last1) {
3326 last1 = HOPc(s, -back_min);
3327 s = HOPc(s, -back_max);
3328 }
3329 else {
9d9163fb
DM
3330 char * const t = (last1 >= reginfo->strbeg)
3331 ? HOPc(last1, 1) : last1 + 1;
6eb5f6b9
JH
3332
3333 last1 = HOPc(s, -back_min);
52657f30 3334 s = t;
6eb5f6b9 3335 }
f2ed9b32 3336 if (utf8_target) {
6eb5f6b9 3337 while (s <= last1) {
02d5137b 3338 if (regtry(reginfo, &s))
6eb5f6b9 3339 goto got_it;
7016d6eb
DM
3340 if (s >= last1) {
3341 s++; /* to break out of outer loop */
3342 break;
3343 }
3344 s += UTF8SKIP(s);
6eb5f6b9
JH
3345 }
3346 }
3347 else {
3348 while (s <= last1) {
02d5137b 3349 if (regtry(reginfo, &s))
6eb5f6b9
JH
3350 goto got_it;
3351 s++;
3352 }
3353 }
3354 }
ab3bbdeb 3355 DEBUG_EXECUTE_r(if (!did_match) {
f2ed9b32 3356 RE_PV_QUOTED_DECL(quoted, utf8_target, PERL_DEBUG_PAD_ZERO(0),
ab3bbdeb 3357 SvPVX_const(must), RE_SV_DUMPLEN(must), 30);
6ad9a8ab 3358 Perl_re_printf( aTHX_ "Did not find %s substr %s%s...\n",
33b8afdf 3359 ((must == prog->anchored_substr || must == prog->anchored_utf8)
bf93d4cc 3360 ? "anchored" : "floating"),
ab3bbdeb
YO
3361 quoted, RE_SV_TAIL(must));
3362 });
6eb5f6b9
JH
3363 goto phooey;
3364 }
f8fc2ecf 3365 else if ( (c = progi->regstclass) ) {
f14c76ed 3366 if (minlen) {
f8fc2ecf 3367 const OPCODE op = OP(progi->regstclass);
66e933ab 3368 /* don't bother with what can't match */
33c28ab2 3369 if (PL_regkind[op] != EXACT && PL_regkind[op] != TRIE)
f14c76ed
RGS
3370 strend = HOPc(strend, -(minlen - 1));
3371 }
a3621e74 3372 DEBUG_EXECUTE_r({
be8e71aa 3373 SV * const prop = sv_newmortal();
8b9781c9 3374 regprop(prog, prop, c, reginfo, NULL);
0df25f3d 3375 {
f2ed9b32 3376 RE_PV_QUOTED_DECL(quoted,utf8_target,PERL_DEBUG_PAD_ZERO(1),
ab3bbdeb 3377 s,strend-s,60);
6ad9a8ab 3378 Perl_re_printf( aTHX_
1c8f8eb1 3379 "Matching stclass %.*s against %s (%d bytes)\n",
e4f74956 3380 (int)SvCUR(prop), SvPVX_const(prop),
ab3bbdeb 3381 quoted, (int)(strend - s));
0df25f3d 3382 }
ffc61ed2 3383 });
f9176b44 3384 if (find_byclass(prog, c, s, strend, reginfo))
6eb5f6b9 3385 goto got_it;
6ad9a8ab 3386 DEBUG_EXECUTE_r(Perl_re_printf( aTHX_ "Contradicts stclass... [regexec_flags]\n"));
d6a28714
JH
3387 }
3388 else {
3389 dontbother = 0;
a0714e2c 3390 if (prog->float_substr != NULL || prog->float_utf8 != NULL) {
33b8afdf 3391 /* Trim the end. */
6af40bd7 3392 char *last= NULL;
33b8afdf 3393 SV* float_real;
c33e64f0
FC
3394 STRLEN len;
3395 const char *little;
33b8afdf 3396
7e0d5ad7
KW
3397 if (utf8_target) {
3398 if (! prog->float_utf8) {
3399 to_utf8_substr(prog);
3400 }
3401 float_real = prog->float_utf8;
3402 }
3403 else {
3404 if (! prog->float_substr) {
3405 if (! to_byte_substr(prog)) {
6b54ddc5 3406 NON_UTF8_TARGET_BUT_UTF8_REQUIRED(phooey);
7e0d5ad7
KW
3407 }
3408 }
3409 float_real = prog->float_substr;
3410 }
d6a28714 3411
c33e64f0
FC
3412 little = SvPV_const(float_real, len);
3413 if (SvTAIL(float_real)) {
7f18ad16
KW
3414 /* This means that float_real contains an artificial \n on
3415 * the end due to the presence of something like this:
3416 * /foo$/ where we can match both "foo" and "foo\n" at the
3417 * end of the string. So we have to compare the end of the
3418 * string first against the float_real without the \n and
3419 * then against the full float_real with the string. We
3420 * have to watch out for cases where the string might be
3421 * smaller than the float_real or the float_real without
3422 * the \n. */
1a13b075
YO
3423 char *checkpos= strend - len;
3424 DEBUG_OPTIMISE_r(
6ad9a8ab 3425 Perl_re_printf( aTHX_
1a13b075
YO
3426 "%sChecking for float_real.%s\n",
3427 PL_colors[4], PL_colors[5]));
3428 if (checkpos + 1 < strbeg) {
7f18ad16
KW
3429 /* can't match, even if we remove the trailing \n
3430 * string is too short to match */
1a13b075 3431 DEBUG_EXECUTE_r(
6ad9a8ab 3432 Perl_re_printf( aTHX_
1a13b075
YO
3433 "%sString shorter than required trailing substring, cannot match.%s\n",
3434 PL_colors[4], PL_colors[5]));
3435 goto phooey;
3436 } else if (memEQ(checkpos + 1, little, len - 1)) {
7f18ad16
KW
3437 /* can match, the end of the string matches without the
3438 * "\n" */
1a13b075
YO
3439 last = checkpos + 1;
3440 } else if (checkpos < strbeg) {
7f18ad16
KW
3441 /* cant match, string is too short when the "\n" is
3442 * included */
1a13b075 3443 DEBUG_EXECUTE_r(
6ad9a8ab 3444 Perl_re_printf( aTHX_
1a13b075
YO
3445 "%sString does not contain required trailing substring, cannot match.%s\n",
3446 PL_colors[4], PL_colors[5]));
3447 goto phooey;
3448 } else if (!multiline) {
7f18ad16
KW
3449 /* non multiline match, so compare with the "\n" at the
3450 * end of the string */
1a13b075
YO
3451 if (memEQ(checkpos, little, len)) {
3452 last= checkpos;
3453 } else {
3454 DEBUG_EXECUTE_r(
6ad9a8ab 3455 Perl_re_printf( aTHX_
1a13b075
YO
3456 "%sString does not contain required trailing substring, cannot match.%s\n",
3457 PL_colors[4], PL_colors[5]));
3458 goto phooey;
3459 }
3460 } else {
7f18ad16
KW
3461 /* multiline match, so we have to search for a place
3462 * where the full str