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