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