This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Fix spurious "uninitialized value" warning in regex match
[perl5.git] / regexec.c
CommitLineData
a0d0e21e
LW
1/* regexec.c
2 */
3
4/*
4ac71550
TC
5 * One Ring to rule them all, One Ring to find them
6 &
7 * [p.v of _The Lord of the Rings_, opening poem]
8 * [p.50 of _The Lord of the Rings_, I/iii: "The Shadow of the Past"]
9 * [p.254 of _The Lord of the Rings_, II/ii: "The Council of Elrond"]
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
81e983c1 83#include "inline_invlist.c"
1b0f46bf 84#include "unicode_constants.h"
81e983c1 85
ef07e810 86#define RF_tainted 1 /* tainted information used? e.g. locale */
c277df42 87#define RF_warned 2 /* warned about big count? */
faec1544 88
ab3bbdeb 89#define RF_utf8 8 /* Pattern contains multibyte chars? */
a0ed51b3 90
f2ed9b32 91#define UTF_PATTERN ((PL_reg_flags & RF_utf8) != 0)
ce862d02 92
a687059c
LW
93#ifndef STATIC
94#define STATIC static
95#endif
96
7e2509c1
KW
97/* Valid for non-utf8 strings, non-ANYOFV nodes only: avoids the reginclass
98 * call if there are no complications: i.e., if everything matchable is
99 * straight forward in the bitmap */
af364d03
KW
100#define REGINCLASS(prog,p,c) (ANYOF_FLAGS(p) ? reginclass(prog,p,c,0,0) \
101 : ANYOF_BITMAP_TEST(p,*(c)))
7d3e948e 102
c277df42
IZ
103/*
104 * Forwards.
105 */
106
f2ed9b32 107#define CHR_SVLEN(sv) (utf8_target ? sv_len_utf8(sv) : SvCUR(sv))
53c4c00c 108#define CHR_DIST(a,b) (PL_reg_match_utf8 ? utf8_distance(a,b) : a - b)
a0ed51b3 109
3dab1dad
YO
110#define HOPc(pos,off) \
111 (char *)(PL_reg_match_utf8 \
52657f30 112 ? reghop3((U8*)pos, off, (U8*)(off >= 0 ? PL_regeol : PL_bostr)) \
3dab1dad
YO
113 : (U8*)(pos + off))
114#define HOPBACKc(pos, off) \
07be1b83
YO
115 (char*)(PL_reg_match_utf8\
116 ? reghopmaybe3((U8*)pos, -off, (U8*)PL_bostr) \
117 : (pos - off >= PL_bostr) \
8e11feef 118 ? (U8*)pos - off \
3dab1dad 119 : NULL)
efb30f32 120
e7409c1b 121#define HOP3(pos,off,lim) (PL_reg_match_utf8 ? reghop3((U8*)(pos), off, (U8*)(lim)) : (U8*)(pos + off))
1aa99e6b 122#define HOP3c(pos,off,lim) ((char*)HOP3(pos,off,lim))
1aa99e6b 123
7016d6eb
DM
124
125#define NEXTCHR_EOS -10 /* nextchr has fallen off the end */
126#define NEXTCHR_IS_EOS (nextchr < 0)
127
128#define SET_nextchr \
129 nextchr = ((locinput < PL_regeol) ? UCHARAT(locinput) : NEXTCHR_EOS)
130
131#define SET_locinput(p) \
132 locinput = (p); \
133 SET_nextchr
134
135
20d0b1e9 136/* these are unrolled below in the CCC_TRY_XXX defined */
61dad979 137#define LOAD_UTF8_CHARCLASS(class,str) STMT_START { \
9c4fdda1 138 if (!CAT2(PL_utf8_,class)) { \
cf54f63a 139 bool ok; \
9c4fdda1 140 ENTER; save_re_context(); \
cf54f63a
JL
141 ok=CAT2(is_utf8_,class)((const U8*)str); \
142 PERL_UNUSED_VAR(ok); \
143 assert(ok); assert(CAT2(PL_utf8_,class)); LEAVE; } } STMT_END
37e2e78e
KW
144/* Doesn't do an assert to verify that is correct */
145#define LOAD_UTF8_CHARCLASS_NO_CHECK(class) STMT_START { \
9c4fdda1 146 if (!CAT2(PL_utf8_,class)) { \
9d63fa07 147 bool throw_away PERL_UNUSED_DECL; \
9c4fdda1
RB
148 ENTER; save_re_context(); \
149 throw_away = CAT2(is_utf8_,class)((const U8*)" "); \
150 LEAVE; } } STMT_END
37e2e78e 151
1a4fad37
AL
152#define LOAD_UTF8_CHARCLASS_ALNUM() LOAD_UTF8_CHARCLASS(alnum,"a")
153#define LOAD_UTF8_CHARCLASS_DIGIT() LOAD_UTF8_CHARCLASS(digit,"0")
154#define LOAD_UTF8_CHARCLASS_SPACE() LOAD_UTF8_CHARCLASS(space," ")
51371543 155
37e2e78e 156#define LOAD_UTF8_CHARCLASS_GCB() /* Grapheme cluster boundaries */ \
61dad979
KW
157 /* No asserts are done for some of these, in case called on a */ \
158 /* Unicode version in which they map to nothing */ \
27d4fc33 159 LOAD_UTF8_CHARCLASS(X_regular_begin, HYPHEN_UTF8); \
61dad979 160 LOAD_UTF8_CHARCLASS(X_extend, COMBINING_GRAVE_ACCENT_UTF8); \
20d0b1e9 161
1dcf4a1b 162#define PLACEHOLDER /* Something for the preprocessor to grab onto */
d1eb3177 163
ee9a90b8
KW
164/* The actual code for CCC_TRY, which uses several variables from the routine
165 * it's callable from. It is designed to be the bulk of a case statement.
166 * FUNC is the macro or function to call on non-utf8 targets that indicate if
167 * nextchr matches the class.
168 * UTF8_TEST is the whole test string to use for utf8 targets
169 * LOAD is what to use to test, and if not present to load in the swash for the
170 * class
171 * POS_OR_NEG is either empty or ! to complement the results of FUNC or
172 * UTF8_TEST test.
173 * The logic is: Fail if we're at the end-of-string; otherwise if the target is
174 * utf8 and a variant, load the swash if necessary and test using the utf8
175 * test. Advance to the next character if test is ok, otherwise fail; If not
176 * utf8 or an invariant under utf8, use the non-utf8 test, and fail if it
177 * fails, or advance to the next character */
178
179#define _CCC_TRY_CODE(POS_OR_NEG, FUNC, UTF8_TEST, CLASS, STR) \
7016d6eb 180 if (NEXTCHR_IS_EOS) { \
ee9a90b8
KW
181 sayNO; \
182 } \
183 if (utf8_target && UTF8_IS_CONTINUED(nextchr)) { \
184 LOAD_UTF8_CHARCLASS(CLASS, STR); \
185 if (POS_OR_NEG (UTF8_TEST)) { \
186 sayNO; \
187 } \
ee9a90b8 188 } \
28b98f76
DM
189 else if (POS_OR_NEG (FUNC(nextchr))) { \
190 sayNO; \
ee9a90b8 191 } \
28b98f76 192 goto increment_locinput;
980866de 193
ee9a90b8
KW
194/* Handle the non-locale cases for a character class and its complement. It
195 * calls _CCC_TRY_CODE with a ! to complement the test for the character class.
196 * This is because that code fails when the test succeeds, so we want to have
197 * the test fail so that the code succeeds. The swash is stored in a
198 * predictable PL_ place */
cfaf538b
KW
199#define _CCC_TRY_NONLOCALE(NAME, NNAME, FUNC, \
200 CLASS, STR) \
ee9a90b8
KW
201 case NAME: \
202 _CCC_TRY_CODE( !, FUNC, \
203 cBOOL(swash_fetch(CAT2(PL_utf8_,CLASS), \
204 (U8*)locinput, TRUE)), \
205 CLASS, STR) \
206 case NNAME: \
1dcf4a1b 207 _CCC_TRY_CODE( PLACEHOLDER , FUNC, \
ee9a90b8
KW
208 cBOOL(swash_fetch(CAT2(PL_utf8_,CLASS), \
209 (U8*)locinput, TRUE)), \
210 CLASS, STR) \
211
212/* Generate the case statements for both locale and non-locale character
213 * classes in regmatch for classes that don't have special unicode semantics.
214 * Locales don't use an immediate swash, but an intermediary special locale
215 * function that is called on the pointer to the current place in the input
216 * string. That function will resolve to needing the same swash. One might
217 * think that because we don't know what the locale will match, we shouldn't
218 * check with the swash loading function that it loaded properly; ie, that we
219 * should use LOAD_UTF8_CHARCLASS_NO_CHECK for those, but what is passed to the
220 * regular LOAD_UTF8_CHARCLASS is in non-locale terms, and so locale is
221 * irrelevant here */
222#define CCC_TRY(NAME, NNAME, FUNC, \
223 NAMEL, NNAMEL, LCFUNC, LCFUNC_utf8, \
cfaf538b 224 NAMEA, NNAMEA, FUNCA, \
ee9a90b8
KW
225 CLASS, STR) \
226 case NAMEL: \
227 PL_reg_flags |= RF_tainted; \
228 _CCC_TRY_CODE( !, LCFUNC, LCFUNC_utf8((U8*)locinput), CLASS, STR) \
229 case NNAMEL: \
230 PL_reg_flags |= RF_tainted; \
1dcf4a1b
KW
231 _CCC_TRY_CODE( PLACEHOLDER, LCFUNC, LCFUNC_utf8((U8*)locinput), \
232 CLASS, STR) \
cfaf538b 233 case NAMEA: \
7016d6eb 234 if (NEXTCHR_IS_EOS || ! FUNCA(nextchr)) { \
cfaf538b
KW
235 sayNO; \
236 } \
237 /* Matched a utf8-invariant, so don't have to worry about utf8 */ \
3640db6b 238 locinput++; \
cfaf538b
KW
239 break; \
240 case NNAMEA: \
7016d6eb 241 if (NEXTCHR_IS_EOS || FUNCA(nextchr)) { \
cfaf538b
KW
242 sayNO; \
243 } \
28b98f76 244 goto increment_locinput; \
ee9a90b8
KW
245 /* Generate the non-locale cases */ \
246 _CCC_TRY_NONLOCALE(NAME, NNAME, FUNC, CLASS, STR)
247
248/* This is like CCC_TRY, but has an extra set of parameters for generating case
249 * statements to handle separate Unicode semantics nodes */
250#define CCC_TRY_U(NAME, NNAME, FUNC, \
251 NAMEL, NNAMEL, LCFUNC, LCFUNC_utf8, \
252 NAMEU, NNAMEU, FUNCU, \
cfaf538b 253 NAMEA, NNAMEA, FUNCA, \
ee9a90b8 254 CLASS, STR) \
cfaf538b
KW
255 CCC_TRY(NAME, NNAME, FUNC, \
256 NAMEL, NNAMEL, LCFUNC, LCFUNC_utf8, \
257 NAMEA, NNAMEA, FUNCA, \
258 CLASS, STR) \
ee9a90b8 259 _CCC_TRY_NONLOCALE(NAMEU, NNAMEU, FUNCU, CLASS, STR)
d1eb3177 260
3dab1dad
YO
261/* TODO: Combine JUMPABLE and HAS_TEXT to cache OP(rn) */
262
5f80c4cf 263/* for use after a quantifier and before an EXACT-like node -- japhy */
c35dcbe2
YO
264/* it would be nice to rework regcomp.sym to generate this stuff. sigh
265 *
266 * NOTE that *nothing* that affects backtracking should be in here, specifically
267 * VERBS must NOT be included. JUMPABLE is used to determine if we can ignore a
268 * node that is in between two EXACT like nodes when ascertaining what the required
269 * "follow" character is. This should probably be moved to regex compile time
270 * although it may be done at run time beause of the REF possibility - more
271 * investigation required. -- demerphq
272*/
3e901dc0
YO
273#define JUMPABLE(rn) ( \
274 OP(rn) == OPEN || \
275 (OP(rn) == CLOSE && (!cur_eval || cur_eval->u.eval.close_paren != ARG(rn))) || \
276 OP(rn) == EVAL || \
cca55fe3
JP
277 OP(rn) == SUSPEND || OP(rn) == IFMATCH || \
278 OP(rn) == PLUS || OP(rn) == MINMOD || \
d1c771f5 279 OP(rn) == KEEPS || \
3dab1dad 280 (PL_regkind[OP(rn)] == CURLY && ARG1(rn) > 0) \
e2d8ce26 281)
ee9b8eae 282#define IS_EXACT(rn) (PL_regkind[OP(rn)] == EXACT)
e2d8ce26 283
ee9b8eae
YO
284#define HAS_TEXT(rn) ( IS_EXACT(rn) || PL_regkind[OP(rn)] == REF )
285
286#if 0
287/* Currently these are only used when PL_regkind[OP(rn)] == EXACT so
288 we don't need this definition. */
289#define IS_TEXT(rn) ( OP(rn)==EXACT || OP(rn)==REF || OP(rn)==NREF )
fab2782b 290#define IS_TEXTF(rn) ( OP(rn)==EXACTFU || OP(rn)==EXACTFU_SS || OP(rn)==EXACTFU_TRICKYFOLD || OP(rn)==EXACTFA || OP(rn)==EXACTF || OP(rn)==REFF || OP(rn)==NREFF )
ee9b8eae
YO
291#define IS_TEXTFL(rn) ( OP(rn)==EXACTFL || OP(rn)==REFFL || OP(rn)==NREFFL )
292
293#else
294/* ... so we use this as its faster. */
295#define IS_TEXT(rn) ( OP(rn)==EXACT )
fab2782b 296#define IS_TEXTFU(rn) ( OP(rn)==EXACTFU || OP(rn)==EXACTFU_SS || OP(rn)==EXACTFU_TRICKYFOLD || OP(rn) == EXACTFA)
ee9b8eae
YO
297#define IS_TEXTF(rn) ( OP(rn)==EXACTF )
298#define IS_TEXTFL(rn) ( OP(rn)==EXACTFL )
299
300#endif
e2d8ce26 301
a84d97b6
HS
302/*
303 Search for mandatory following text node; for lookahead, the text must
304 follow but for lookbehind (rn->flags != 0) we skip to the next step.
305*/
cca55fe3 306#define FIND_NEXT_IMPT(rn) STMT_START { \
3dab1dad
YO
307 while (JUMPABLE(rn)) { \
308 const OPCODE type = OP(rn); \
309 if (type == SUSPEND || PL_regkind[type] == CURLY) \
e2d8ce26 310 rn = NEXTOPER(NEXTOPER(rn)); \
3dab1dad 311 else if (type == PLUS) \
cca55fe3 312 rn = NEXTOPER(rn); \
3dab1dad 313 else if (type == IFMATCH) \
a84d97b6 314 rn = (rn->flags == 0) ? NEXTOPER(NEXTOPER(rn)) : rn + ARG(rn); \
e2d8ce26 315 else rn += NEXT_OFF(rn); \
3dab1dad 316 } \
5f80c4cf 317} STMT_END
74750237 318
c476f425 319
acfe0abc 320static void restore_pos(pTHX_ void *arg);
51371543 321
87c0511b 322#define REGCP_PAREN_ELEMS 3
f067efbf 323#define REGCP_OTHER_ELEMS 3
e0fa7e2b 324#define REGCP_FRAME_ELEMS 1
620d5b66
NC
325/* REGCP_FRAME_ELEMS are not part of the REGCP_OTHER_ELEMS and
326 * are needed for the regexp context stack bookkeeping. */
327
76e3520e 328STATIC CHECKPOINT
b93070ed 329S_regcppush(pTHX_ const regexp *rex, I32 parenfloor)
a0d0e21e 330{
97aff369 331 dVAR;
a3b680e6 332 const int retval = PL_savestack_ix;
a3b680e6 333 const int paren_elems_to_push = (PL_regsize - parenfloor) * REGCP_PAREN_ELEMS;
e0fa7e2b
NC
334 const UV total_elems = paren_elems_to_push + REGCP_OTHER_ELEMS;
335 const UV elems_shifted = total_elems << SAVE_TIGHT_SHIFT;
87c0511b 336 I32 p;
40a82448 337 GET_RE_DEBUG_FLAGS_DECL;
a0d0e21e 338
b93070ed
DM
339 PERL_ARGS_ASSERT_REGCPPUSH;
340
e49a9654 341 if (paren_elems_to_push < 0)
5637ef5b
NC
342 Perl_croak(aTHX_ "panic: paren_elems_to_push, %i < 0",
343 paren_elems_to_push);
e49a9654 344
e0fa7e2b
NC
345 if ((elems_shifted >> SAVE_TIGHT_SHIFT) != total_elems)
346 Perl_croak(aTHX_ "panic: paren_elems_to_push offset %"UVuf
5df417d0
JH
347 " out of range (%lu-%ld)",
348 total_elems, (unsigned long)PL_regsize, (long)parenfloor);
e0fa7e2b 349
620d5b66 350 SSGROW(total_elems + REGCP_FRAME_ELEMS);
7f69552c 351
495f47a5
DM
352 DEBUG_BUFFERS_r(
353 if ((int)PL_regsize > (int)parenfloor)
354 PerlIO_printf(Perl_debug_log,
355 "rex=0x%"UVxf" offs=0x%"UVxf": saving capture indices:\n",
356 PTR2UV(rex),
357 PTR2UV(rex->offs)
358 );
359 );
87c0511b 360 for (p = parenfloor+1; p <= (I32)PL_regsize; p++) {
b1ce53c5 361/* REGCP_PARENS_ELEMS are pushed per pairs of parentheses. */
b93070ed
DM
362 SSPUSHINT(rex->offs[p].end);
363 SSPUSHINT(rex->offs[p].start);
1ca2007e 364 SSPUSHINT(rex->offs[p].start_tmp);
e7707071 365 DEBUG_BUFFERS_r(PerlIO_printf(Perl_debug_log,
495f47a5
DM
366 " \\%"UVuf": %"IVdf"(%"IVdf")..%"IVdf"\n",
367 (UV)p,
368 (IV)rex->offs[p].start,
369 (IV)rex->offs[p].start_tmp,
370 (IV)rex->offs[p].end
40a82448 371 ));
a0d0e21e 372 }
b1ce53c5 373/* REGCP_OTHER_ELEMS are pushed in any case, parentheses or no. */
3280af22 374 SSPUSHINT(PL_regsize);
b93070ed
DM
375 SSPUSHINT(rex->lastparen);
376 SSPUSHINT(rex->lastcloseparen);
e0fa7e2b 377 SSPUSHUV(SAVEt_REGCONTEXT | elems_shifted); /* Magic cookie. */
41123dfd 378
a0d0e21e
LW
379 return retval;
380}
381
c277df42 382/* These are needed since we do not localize EVAL nodes: */
ab3bbdeb
YO
383#define REGCP_SET(cp) \
384 DEBUG_STATE_r( \
ab3bbdeb 385 PerlIO_printf(Perl_debug_log, \
e4f74956 386 " Setting an EVAL scope, savestack=%"IVdf"\n", \
ab3bbdeb
YO
387 (IV)PL_savestack_ix)); \
388 cp = PL_savestack_ix
c3464db5 389
ab3bbdeb 390#define REGCP_UNWIND(cp) \
e4f74956 391 DEBUG_STATE_r( \
ab3bbdeb 392 if (cp != PL_savestack_ix) \
e4f74956
YO
393 PerlIO_printf(Perl_debug_log, \
394 " Clearing an EVAL scope, savestack=%"IVdf"..%"IVdf"\n", \
ab3bbdeb
YO
395 (IV)(cp), (IV)PL_savestack_ix)); \
396 regcpblow(cp)
c277df42 397
a8d1f4b4
DM
398#define UNWIND_PAREN(lp, lcp) \
399 for (n = rex->lastparen; n > lp; n--) \
400 rex->offs[n].end = -1; \
401 rex->lastparen = n; \
402 rex->lastcloseparen = lcp;
403
404
f067efbf 405STATIC void
b93070ed 406S_regcppop(pTHX_ regexp *rex)
a0d0e21e 407{
97aff369 408 dVAR;
e0fa7e2b 409 UV i;
87c0511b 410 U32 paren;
a3621e74
YO
411 GET_RE_DEBUG_FLAGS_DECL;
412
7918f24d
NC
413 PERL_ARGS_ASSERT_REGCPPOP;
414
b1ce53c5 415 /* Pop REGCP_OTHER_ELEMS before the parentheses loop starts. */
c6bf6a65 416 i = SSPOPUV;
e0fa7e2b
NC
417 assert((i & SAVE_MASK) == SAVEt_REGCONTEXT); /* Check that the magic cookie is there. */
418 i >>= SAVE_TIGHT_SHIFT; /* Parentheses elements to pop. */
b93070ed
DM
419 rex->lastcloseparen = SSPOPINT;
420 rex->lastparen = SSPOPINT;
3280af22 421 PL_regsize = SSPOPINT;
b1ce53c5 422
620d5b66 423 i -= REGCP_OTHER_ELEMS;
b1ce53c5 424 /* Now restore the parentheses context. */
495f47a5
DM
425 DEBUG_BUFFERS_r(
426 if (i || rex->lastparen + 1 <= rex->nparens)
427 PerlIO_printf(Perl_debug_log,
428 "rex=0x%"UVxf" offs=0x%"UVxf": restoring capture indices to:\n",
429 PTR2UV(rex),
430 PTR2UV(rex->offs)
431 );
432 );
87c0511b 433 paren = PL_regsize;
620d5b66 434 for ( ; i > 0; i -= REGCP_PAREN_ELEMS) {
1df70142 435 I32 tmps;
1ca2007e 436 rex->offs[paren].start_tmp = SSPOPINT;
b93070ed 437 rex->offs[paren].start = SSPOPINT;
cf93c79d 438 tmps = SSPOPINT;
b93070ed
DM
439 if (paren <= rex->lastparen)
440 rex->offs[paren].end = tmps;
495f47a5
DM
441 DEBUG_BUFFERS_r( PerlIO_printf(Perl_debug_log,
442 " \\%"UVuf": %"IVdf"(%"IVdf")..%"IVdf"%s\n",
443 (UV)paren,
444 (IV)rex->offs[paren].start,
445 (IV)rex->offs[paren].start_tmp,
446 (IV)rex->offs[paren].end,
447 (paren > rex->lastparen ? "(skipped)" : ""));
c277df42 448 );
87c0511b 449 paren--;
a0d0e21e 450 }
daf18116 451#if 1
dafc8851
JH
452 /* It would seem that the similar code in regtry()
453 * already takes care of this, and in fact it is in
454 * a better location to since this code can #if 0-ed out
455 * but the code in regtry() is needed or otherwise tests
456 * requiring null fields (pat.t#187 and split.t#{13,14}
daf18116
JH
457 * (as of patchlevel 7877) will fail. Then again,
458 * this code seems to be necessary or otherwise
225593e1
DM
459 * this erroneously leaves $1 defined: "1" =~ /^(?:(\d)x)?\d$/
460 * --jhi updated by dapm */
b93070ed 461 for (i = rex->lastparen + 1; i <= rex->nparens; i++) {
097eb12c 462 if (i > PL_regsize)
b93070ed
DM
463 rex->offs[i].start = -1;
464 rex->offs[i].end = -1;
495f47a5
DM
465 DEBUG_BUFFERS_r( PerlIO_printf(Perl_debug_log,
466 " \\%"UVuf": %s ..-1 undeffing\n",
467 (UV)i,
468 (i > PL_regsize) ? "-1" : " "
469 ));
a0d0e21e 470 }
dafc8851 471#endif
a0d0e21e
LW
472}
473
74088413
DM
474/* restore the parens and associated vars at savestack position ix,
475 * but without popping the stack */
476
477STATIC void
478S_regcp_restore(pTHX_ regexp *rex, I32 ix)
479{
480 I32 tmpix = PL_savestack_ix;
481 PL_savestack_ix = ix;
482 regcppop(rex);
483 PL_savestack_ix = tmpix;
484}
485
02db2b7b 486#define regcpblow(cp) LEAVE_SCOPE(cp) /* Ignores regcppush()ed data. */
a0d0e21e 487
a687059c 488/*
e50aee73 489 * pregexec and friends
a687059c
LW
490 */
491
76234dfb 492#ifndef PERL_IN_XSUB_RE
a687059c 493/*
c277df42 494 - pregexec - match a regexp against a string
a687059c 495 */
c277df42 496I32
49d7dfbc 497Perl_pregexec(pTHX_ REGEXP * const prog, char* stringarg, register char *strend,
c3464db5 498 char *strbeg, I32 minend, SV *screamer, U32 nosave)
8fd1a950
DM
499/* stringarg: the point in the string at which to begin matching */
500/* strend: pointer to null at end of string */
501/* strbeg: real beginning of string */
502/* minend: end of match must be >= minend bytes after stringarg. */
503/* screamer: SV being matched: only used for utf8 flag, pos() etc; string
504 * itself is accessed via the pointers above */
505/* nosave: For optimizations. */
c277df42 506{
7918f24d
NC
507 PERL_ARGS_ASSERT_PREGEXEC;
508
c277df42 509 return
9041c2e3 510 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
c277df42
IZ
511 nosave ? 0 : REXEC_COPY_STR);
512}
76234dfb 513#endif
22e551b9 514
9041c2e3 515/*
cad2e5aa
JH
516 * Need to implement the following flags for reg_anch:
517 *
518 * USE_INTUIT_NOML - Useful to call re_intuit_start() first
519 * USE_INTUIT_ML
520 * INTUIT_AUTORITATIVE_NOML - Can trust a positive answer
521 * INTUIT_AUTORITATIVE_ML
522 * INTUIT_ONCE_NOML - Intuit can match in one location only.
523 * INTUIT_ONCE_ML
524 *
525 * Another flag for this function: SECOND_TIME (so that float substrs
526 * with giant delta may be not rechecked).
527 */
528
529/* Assumptions: if ANCH_GPOS, then strpos is anchored. XXXX Check GPOS logic */
530
3f7c398e 531/* If SCREAM, then SvPVX_const(sv) should be compatible with strpos and strend.
cad2e5aa
JH
532 Otherwise, only SvCUR(sv) is used to get strbeg. */
533
534/* XXXX We assume that strpos is strbeg unless sv. */
535
6eb5f6b9
JH
536/* XXXX Some places assume that there is a fixed substring.
537 An update may be needed if optimizer marks as "INTUITable"
538 RExen without fixed substrings. Similarly, it is assumed that
539 lengths of all the strings are no more than minlen, thus they
540 cannot come from lookahead.
40d049e4
YO
541 (Or minlen should take into account lookahead.)
542 NOTE: Some of this comment is not correct. minlen does now take account
543 of lookahead/behind. Further research is required. -- demerphq
544
545*/
6eb5f6b9 546
2c2d71f5
JH
547/* A failure to find a constant substring means that there is no need to make
548 an expensive call to REx engine, thus we celebrate a failure. Similarly,
549 finding a substring too deep into the string means that less calls to
30944b6d
IZ
550 regtry() should be needed.
551
552 REx compiler's optimizer found 4 possible hints:
553 a) Anchored substring;
554 b) Fixed substring;
555 c) Whether we are anchored (beginning-of-line or \G);
486ec47a 556 d) First node (of those at offset 0) which may distinguish positions;
6eb5f6b9 557 We use a)b)d) and multiline-part of c), and try to find a position in the
30944b6d
IZ
558 string which does not contradict any of them.
559 */
2c2d71f5 560
6eb5f6b9
JH
561/* Most of decisions we do here should have been done at compile time.
562 The nodes of the REx which we used for the search should have been
563 deleted from the finite automaton. */
564
cad2e5aa 565char *
288b8c02 566Perl_re_intuit_start(pTHX_ REGEXP * const rx, SV *sv, char *strpos,
9f61653a 567 char *strend, const U32 flags, re_scream_pos_data *data)
cad2e5aa 568{
97aff369 569 dVAR;
288b8c02 570 struct regexp *const prog = (struct regexp *)SvANY(rx);
eb578fdb 571 I32 start_shift = 0;
cad2e5aa 572 /* Should be nonnegative! */
eb578fdb
KW
573 I32 end_shift = 0;
574 char *s;
575 SV *check;
a1933d95 576 char *strbeg;
cad2e5aa 577 char *t;
f2ed9b32 578 const bool utf8_target = (sv && SvUTF8(sv)) ? 1 : 0; /* if no sv we have to assume bytes */
cad2e5aa 579 I32 ml_anch;
eb578fdb 580 char *other_last = NULL; /* other substr checked before this */
bd61b366 581 char *check_at = NULL; /* check substr found at this pos */
d8080198 582 char *checked_upto = NULL; /* how far into the string we have already checked using find_byclass*/
bbe252da 583 const I32 multiline = prog->extflags & RXf_PMf_MULTILINE;
f8fc2ecf 584 RXi_GET_DECL(prog,progi);
30944b6d 585#ifdef DEBUGGING
890ce7af 586 const char * const i_strpos = strpos;
30944b6d 587#endif
a3621e74
YO
588 GET_RE_DEBUG_FLAGS_DECL;
589
7918f24d 590 PERL_ARGS_ASSERT_RE_INTUIT_START;
c33e64f0
FC
591 PERL_UNUSED_ARG(flags);
592 PERL_UNUSED_ARG(data);
7918f24d 593
f2ed9b32 594 RX_MATCH_UTF8_set(rx,utf8_target);
cad2e5aa 595
3c8556c3 596 if (RX_UTF8(rx)) {
b8d68ded
JH
597 PL_reg_flags |= RF_utf8;
598 }
ab3bbdeb 599 DEBUG_EXECUTE_r(
f2ed9b32 600 debug_start_match(rx, utf8_target, strpos, strend,
1de06328
YO
601 sv ? "Guessing start of match in sv for"
602 : "Guessing start of match in string for");
2a782b5b 603 );
cad2e5aa 604
c344f387
JH
605 /* CHR_DIST() would be more correct here but it makes things slow. */
606 if (prog->minlen > strend - strpos) {
a3621e74 607 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
a72c7584 608 "String too short... [re_intuit_start]\n"));
cad2e5aa 609 goto fail;
2c2d71f5 610 }
1de06328 611
7016d6eb
DM
612 /* XXX we need to pass strbeg as a separate arg: the following is
613 * guesswork and can be wrong... */
614 if (sv && SvPOK(sv)) {
615 char * p = SvPVX(sv);
616 STRLEN cur = SvCUR(sv);
617 if (p <= strpos && strpos < p + cur) {
618 strbeg = p;
619 assert(p <= strend && strend <= p + cur);
620 }
621 else
622 strbeg = strend - cur;
623 }
624 else
625 strbeg = strpos;
626
1aa99e6b 627 PL_regeol = strend;
f2ed9b32 628 if (utf8_target) {
33b8afdf
JH
629 if (!prog->check_utf8 && prog->check_substr)
630 to_utf8_substr(prog);
631 check = prog->check_utf8;
632 } else {
633 if (!prog->check_substr && prog->check_utf8)
634 to_byte_substr(prog);
635 check = prog->check_substr;
636 }
1de06328 637 if (check == &PL_sv_undef) {
a3621e74 638 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1de06328 639 "Non-utf8 string cannot match utf8 check string\n"));
33b8afdf
JH
640 goto fail;
641 }
bbe252da
YO
642 if (prog->extflags & RXf_ANCH) { /* Match at beg-of-str or after \n */
643 ml_anch = !( (prog->extflags & RXf_ANCH_SINGLE)
644 || ( (prog->extflags & RXf_ANCH_BOL)
7fba1cd6 645 && !multiline ) ); /* Check after \n? */
cad2e5aa 646
7e25d62c 647 if (!ml_anch) {
bbe252da
YO
648 if ( !(prog->extflags & RXf_ANCH_GPOS) /* Checked by the caller */
649 && !(prog->intflags & PREGf_IMPLICIT) /* not a real BOL */
3f7c398e 650 /* SvCUR is not set on references: SvRV and SvPVX_const overlap */
7e25d62c
JH
651 && sv && !SvROK(sv)
652 && (strpos != strbeg)) {
a3621e74 653 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Not at start...\n"));
7e25d62c
JH
654 goto fail;
655 }
656 if (prog->check_offset_min == prog->check_offset_max &&
bbe252da 657 !(prog->extflags & RXf_CANY_SEEN)) {
2c2d71f5 658 /* Substring at constant offset from beg-of-str... */
cad2e5aa
JH
659 I32 slen;
660
1aa99e6b 661 s = HOP3c(strpos, prog->check_offset_min, strend);
1de06328 662
653099ff
GS
663 if (SvTAIL(check)) {
664 slen = SvCUR(check); /* >= 1 */
cad2e5aa 665
9041c2e3 666 if ( strend - s > slen || strend - s < slen - 1
2c2d71f5 667 || (strend - s == slen && strend[-1] != '\n')) {
a3621e74 668 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "String too long...\n"));
2c2d71f5 669 goto fail_finish;
cad2e5aa
JH
670 }
671 /* Now should match s[0..slen-2] */
672 slen--;
3f7c398e 673 if (slen && (*SvPVX_const(check) != *s
cad2e5aa 674 || (slen > 1
3f7c398e 675 && memNE(SvPVX_const(check), s, slen)))) {
2c2d71f5 676 report_neq:
a3621e74 677 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "String not equal...\n"));
2c2d71f5
JH
678 goto fail_finish;
679 }
cad2e5aa 680 }
3f7c398e 681 else if (*SvPVX_const(check) != *s
653099ff 682 || ((slen = SvCUR(check)) > 1
3f7c398e 683 && memNE(SvPVX_const(check), s, slen)))
2c2d71f5 684 goto report_neq;
c315bfe8 685 check_at = s;
2c2d71f5 686 goto success_at_start;
7e25d62c 687 }
cad2e5aa 688 }
2c2d71f5 689 /* Match is anchored, but substr is not anchored wrt beg-of-str. */
cad2e5aa 690 s = strpos;
2c2d71f5 691 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
1de06328
YO
692 end_shift = prog->check_end_shift;
693
2c2d71f5 694 if (!ml_anch) {
a3b680e6 695 const I32 end = prog->check_offset_max + CHR_SVLEN(check)
653099ff 696 - (SvTAIL(check) != 0);
a3b680e6 697 const I32 eshift = CHR_DIST((U8*)strend, (U8*)s) - end;
2c2d71f5
JH
698
699 if (end_shift < eshift)
700 end_shift = eshift;
701 }
cad2e5aa 702 }
2c2d71f5 703 else { /* Can match at random position */
cad2e5aa
JH
704 ml_anch = 0;
705 s = strpos;
1de06328
YO
706 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
707 end_shift = prog->check_end_shift;
708
709 /* end shift should be non negative here */
cad2e5aa
JH
710 }
711
bcdf7404 712#ifdef QDEBUGGING /* 7/99: reports of failure (with the older version) */
0033605d 713 if (end_shift < 0)
1de06328 714 Perl_croak(aTHX_ "panic: end_shift: %"IVdf" pattern:\n%s\n ",
220fc49f 715 (IV)end_shift, RX_PRECOMP(prog));
2c2d71f5
JH
716#endif
717
2c2d71f5
JH
718 restart:
719 /* Find a possible match in the region s..strend by looking for
720 the "check" substring in the region corrected by start/end_shift. */
1de06328
YO
721
722 {
723 I32 srch_start_shift = start_shift;
724 I32 srch_end_shift = end_shift;
c33e64f0
FC
725 U8* start_point;
726 U8* end_point;
1de06328
YO
727 if (srch_start_shift < 0 && strbeg - s > srch_start_shift) {
728 srch_end_shift -= ((strbeg - s) - srch_start_shift);
729 srch_start_shift = strbeg - s;
730 }
6bda09f9 731 DEBUG_OPTIMISE_MORE_r({
1de06328
YO
732 PerlIO_printf(Perl_debug_log, "Check offset min: %"IVdf" Start shift: %"IVdf" End shift %"IVdf" Real End Shift: %"IVdf"\n",
733 (IV)prog->check_offset_min,
734 (IV)srch_start_shift,
735 (IV)srch_end_shift,
736 (IV)prog->check_end_shift);
737 });
738
bbe252da 739 if (prog->extflags & RXf_CANY_SEEN) {
1de06328
YO
740 start_point= (U8*)(s + srch_start_shift);
741 end_point= (U8*)(strend - srch_end_shift);
742 } else {
743 start_point= HOP3(s, srch_start_shift, srch_start_shift < 0 ? strbeg : strend);
744 end_point= HOP3(strend, -srch_end_shift, strbeg);
745 }
6bda09f9 746 DEBUG_OPTIMISE_MORE_r({
56570a2c 747 PerlIO_printf(Perl_debug_log, "fbm_instr len=%d str=<%.*s>\n",
1de06328 748 (int)(end_point - start_point),
fc8cd66c 749 (int)(end_point - start_point) > 20 ? 20 : (int)(end_point - start_point),
1de06328
YO
750 start_point);
751 });
752
753 s = fbm_instr( start_point, end_point,
7fba1cd6 754 check, multiline ? FBMrf_MULTILINE : 0);
1de06328 755 }
cad2e5aa
JH
756 /* Update the count-of-usability, remove useless subpatterns,
757 unshift s. */
2c2d71f5 758
ab3bbdeb 759 DEBUG_EXECUTE_r({
f2ed9b32 760 RE_PV_QUOTED_DECL(quoted, utf8_target, PERL_DEBUG_PAD_ZERO(0),
ab3bbdeb
YO
761 SvPVX_const(check), RE_SV_DUMPLEN(check), 30);
762 PerlIO_printf(Perl_debug_log, "%s %s substr %s%s%s",
2c2d71f5 763 (s ? "Found" : "Did not find"),
f2ed9b32 764 (check == (utf8_target ? prog->anchored_utf8 : prog->anchored_substr)
ab3bbdeb
YO
765 ? "anchored" : "floating"),
766 quoted,
767 RE_SV_TAIL(check),
768 (s ? " at offset " : "...\n") );
769 });
2c2d71f5
JH
770
771 if (!s)
772 goto fail_finish;
2c2d71f5 773 /* Finish the diagnostic message */
a3621e74 774 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%ld...\n", (long)(s - i_strpos)) );
2c2d71f5 775
1de06328
YO
776 /* XXX dmq: first branch is for positive lookbehind...
777 Our check string is offset from the beginning of the pattern.
778 So we need to do any stclass tests offset forward from that
779 point. I think. :-(
780 */
781
782
783
784 check_at=s;
785
786
2c2d71f5
JH
787 /* Got a candidate. Check MBOL anchoring, and the *other* substr.
788 Start with the other substr.
789 XXXX no SCREAM optimization yet - and a very coarse implementation
a0288114 790 XXXX /ttx+/ results in anchored="ttx", floating="x". floating will
2c2d71f5
JH
791 *always* match. Probably should be marked during compile...
792 Probably it is right to do no SCREAM here...
793 */
794
f2ed9b32 795 if (utf8_target ? (prog->float_utf8 && prog->anchored_utf8)
1de06328
YO
796 : (prog->float_substr && prog->anchored_substr))
797 {
30944b6d 798 /* Take into account the "other" substring. */
2c2d71f5
JH
799 /* XXXX May be hopelessly wrong for UTF... */
800 if (!other_last)
6eb5f6b9 801 other_last = strpos;
f2ed9b32 802 if (check == (utf8_target ? prog->float_utf8 : prog->float_substr)) {
30944b6d
IZ
803 do_other_anchored:
804 {
890ce7af
AL
805 char * const last = HOP3c(s, -start_shift, strbeg);
806 char *last1, *last2;
be8e71aa 807 char * const saved_s = s;
33b8afdf 808 SV* must;
2c2d71f5 809
2c2d71f5
JH
810 t = s - prog->check_offset_max;
811 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
f2ed9b32 812 && (!utf8_target
0ce71af7 813 || ((t = (char*)reghopmaybe3((U8*)s, -(prog->check_offset_max), (U8*)strpos))
2c2d71f5 814 && t > strpos)))
6f207bd3 815 NOOP;
2c2d71f5
JH
816 else
817 t = strpos;
1aa99e6b 818 t = HOP3c(t, prog->anchored_offset, strend);
6eb5f6b9
JH
819 if (t < other_last) /* These positions already checked */
820 t = other_last;
1aa99e6b 821 last2 = last1 = HOP3c(strend, -prog->minlen, strbeg);
2c2d71f5
JH
822 if (last < last1)
823 last1 = last;
1de06328
YO
824 /* XXXX It is not documented what units *_offsets are in.
825 We assume bytes, but this is clearly wrong.
826 Meaning this code needs to be carefully reviewed for errors.
827 dmq.
828 */
829
2c2d71f5 830 /* On end-of-str: see comment below. */
f2ed9b32 831 must = utf8_target ? prog->anchored_utf8 : prog->anchored_substr;
33b8afdf
JH
832 if (must == &PL_sv_undef) {
833 s = (char*)NULL;
1de06328 834 DEBUG_r(must = prog->anchored_utf8); /* for debug */
33b8afdf
JH
835 }
836 else
837 s = fbm_instr(
838 (unsigned char*)t,
839 HOP3(HOP3(last1, prog->anchored_offset, strend)
840 + SvCUR(must), -(SvTAIL(must)!=0), strbeg),
841 must,
7fba1cd6 842 multiline ? FBMrf_MULTILINE : 0
33b8afdf 843 );
ab3bbdeb 844 DEBUG_EXECUTE_r({
f2ed9b32 845 RE_PV_QUOTED_DECL(quoted, utf8_target, PERL_DEBUG_PAD_ZERO(0),
ab3bbdeb
YO
846 SvPVX_const(must), RE_SV_DUMPLEN(must), 30);
847 PerlIO_printf(Perl_debug_log, "%s anchored substr %s%s",
2c2d71f5 848 (s ? "Found" : "Contradicts"),
ab3bbdeb
YO
849 quoted, RE_SV_TAIL(must));
850 });
851
852
2c2d71f5
JH
853 if (!s) {
854 if (last1 >= last2) {
a3621e74 855 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
2c2d71f5
JH
856 ", giving up...\n"));
857 goto fail_finish;
858 }
a3621e74 859 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
2c2d71f5 860 ", trying floating at offset %ld...\n",
be8e71aa 861 (long)(HOP3c(saved_s, 1, strend) - i_strpos)));
1aa99e6b
IH
862 other_last = HOP3c(last1, prog->anchored_offset+1, strend);
863 s = HOP3c(last, 1, strend);
2c2d71f5
JH
864 goto restart;
865 }
866 else {
a3621e74 867 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
30944b6d 868 (long)(s - i_strpos)));
1aa99e6b
IH
869 t = HOP3c(s, -prog->anchored_offset, strbeg);
870 other_last = HOP3c(s, 1, strend);
be8e71aa 871 s = saved_s;
2c2d71f5
JH
872 if (t == strpos)
873 goto try_at_start;
2c2d71f5
JH
874 goto try_at_offset;
875 }
30944b6d 876 }
2c2d71f5
JH
877 }
878 else { /* Take into account the floating substring. */
33b8afdf 879 char *last, *last1;
be8e71aa 880 char * const saved_s = s;
33b8afdf
JH
881 SV* must;
882
883 t = HOP3c(s, -start_shift, strbeg);
884 last1 = last =
885 HOP3c(strend, -prog->minlen + prog->float_min_offset, strbeg);
886 if (CHR_DIST((U8*)last, (U8*)t) > prog->float_max_offset)
887 last = HOP3c(t, prog->float_max_offset, strend);
888 s = HOP3c(t, prog->float_min_offset, strend);
889 if (s < other_last)
890 s = other_last;
2c2d71f5 891 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
f2ed9b32 892 must = utf8_target ? prog->float_utf8 : prog->float_substr;
33b8afdf
JH
893 /* fbm_instr() takes into account exact value of end-of-str
894 if the check is SvTAIL(ed). Since false positives are OK,
895 and end-of-str is not later than strend we are OK. */
896 if (must == &PL_sv_undef) {
897 s = (char*)NULL;
1de06328 898 DEBUG_r(must = prog->float_utf8); /* for debug message */
33b8afdf
JH
899 }
900 else
2c2d71f5 901 s = fbm_instr((unsigned char*)s,
33b8afdf
JH
902 (unsigned char*)last + SvCUR(must)
903 - (SvTAIL(must)!=0),
7fba1cd6 904 must, multiline ? FBMrf_MULTILINE : 0);
ab3bbdeb 905 DEBUG_EXECUTE_r({
f2ed9b32 906 RE_PV_QUOTED_DECL(quoted, utf8_target, PERL_DEBUG_PAD_ZERO(0),
ab3bbdeb
YO
907 SvPVX_const(must), RE_SV_DUMPLEN(must), 30);
908 PerlIO_printf(Perl_debug_log, "%s floating substr %s%s",
33b8afdf 909 (s ? "Found" : "Contradicts"),
ab3bbdeb
YO
910 quoted, RE_SV_TAIL(must));
911 });
33b8afdf
JH
912 if (!s) {
913 if (last1 == last) {
a3621e74 914 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
33b8afdf
JH
915 ", giving up...\n"));
916 goto fail_finish;
2c2d71f5 917 }
a3621e74 918 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
33b8afdf 919 ", trying anchored starting at offset %ld...\n",
be8e71aa 920 (long)(saved_s + 1 - i_strpos)));
33b8afdf
JH
921 other_last = last;
922 s = HOP3c(t, 1, strend);
923 goto restart;
924 }
925 else {
a3621e74 926 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
33b8afdf
JH
927 (long)(s - i_strpos)));
928 other_last = s; /* Fix this later. --Hugo */
be8e71aa 929 s = saved_s;
33b8afdf
JH
930 if (t == strpos)
931 goto try_at_start;
932 goto try_at_offset;
933 }
2c2d71f5 934 }
cad2e5aa 935 }
2c2d71f5 936
1de06328 937
9ef43ace 938 t= (char*)HOP3( s, -prog->check_offset_max, (prog->check_offset_max<0) ? strend : strpos);
1de06328 939
6bda09f9 940 DEBUG_OPTIMISE_MORE_r(
1de06328
YO
941 PerlIO_printf(Perl_debug_log,
942 "Check offset min:%"IVdf" max:%"IVdf" S:%"IVdf" t:%"IVdf" D:%"IVdf" end:%"IVdf"\n",
943 (IV)prog->check_offset_min,
944 (IV)prog->check_offset_max,
945 (IV)(s-strpos),
946 (IV)(t-strpos),
947 (IV)(t-s),
948 (IV)(strend-strpos)
949 )
950 );
951
2c2d71f5 952 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
f2ed9b32 953 && (!utf8_target
9ef43ace 954 || ((t = (char*)reghopmaybe3((U8*)s, -prog->check_offset_max, (U8*) ((prog->check_offset_max<0) ? strend : strpos)))
1de06328
YO
955 && t > strpos)))
956 {
2c2d71f5
JH
957 /* Fixed substring is found far enough so that the match
958 cannot start at strpos. */
959 try_at_offset:
cad2e5aa 960 if (ml_anch && t[-1] != '\n') {
30944b6d
IZ
961 /* Eventually fbm_*() should handle this, but often
962 anchored_offset is not 0, so this check will not be wasted. */
963 /* XXXX In the code below we prefer to look for "^" even in
964 presence of anchored substrings. And we search even
965 beyond the found float position. These pessimizations
966 are historical artefacts only. */
967 find_anchor:
2c2d71f5 968 while (t < strend - prog->minlen) {
cad2e5aa 969 if (*t == '\n') {
4ee3650e 970 if (t < check_at - prog->check_offset_min) {
f2ed9b32 971 if (utf8_target ? prog->anchored_utf8 : prog->anchored_substr) {
4ee3650e
GS
972 /* Since we moved from the found position,
973 we definitely contradict the found anchored
30944b6d
IZ
974 substr. Due to the above check we do not
975 contradict "check" substr.
976 Thus we can arrive here only if check substr
977 is float. Redo checking for "other"=="fixed".
978 */
9041c2e3 979 strpos = t + 1;
a3621e74 980 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld, rescanning for anchored from offset %ld...\n",
e4584336 981 PL_colors[0], PL_colors[1], (long)(strpos - i_strpos), (long)(strpos - i_strpos + prog->anchored_offset)));
30944b6d
IZ
982 goto do_other_anchored;
983 }
4ee3650e
GS
984 /* We don't contradict the found floating substring. */
985 /* XXXX Why not check for STCLASS? */
cad2e5aa 986 s = t + 1;
a3621e74 987 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld...\n",
e4584336 988 PL_colors[0], PL_colors[1], (long)(s - i_strpos)));
cad2e5aa
JH
989 goto set_useful;
990 }
4ee3650e
GS
991 /* Position contradicts check-string */
992 /* XXXX probably better to look for check-string
993 than for "\n", so one should lower the limit for t? */
a3621e74 994 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m, restarting lookup for check-string at offset %ld...\n",
e4584336 995 PL_colors[0], PL_colors[1], (long)(t + 1 - i_strpos)));
0e41cd87 996 other_last = strpos = s = t + 1;
cad2e5aa
JH
997 goto restart;
998 }
999 t++;
1000 }
a3621e74 1001 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Did not find /%s^%s/m...\n",
e4584336 1002 PL_colors[0], PL_colors[1]));
2c2d71f5 1003 goto fail_finish;
cad2e5aa 1004 }
f5952150 1005 else {
a3621e74 1006 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Starting position does not contradict /%s^%s/m...\n",
e4584336 1007 PL_colors[0], PL_colors[1]));
f5952150 1008 }
cad2e5aa
JH
1009 s = t;
1010 set_useful:
f2ed9b32 1011 ++BmUSEFUL(utf8_target ? prog->check_utf8 : prog->check_substr); /* hooray/5 */
cad2e5aa
JH
1012 }
1013 else {
f5952150 1014 /* The found string does not prohibit matching at strpos,
2c2d71f5 1015 - no optimization of calling REx engine can be performed,
f5952150
GS
1016 unless it was an MBOL and we are not after MBOL,
1017 or a future STCLASS check will fail this. */
2c2d71f5
JH
1018 try_at_start:
1019 /* Even in this situation we may use MBOL flag if strpos is offset
1020 wrt the start of the string. */
05b4157f 1021 if (ml_anch && sv && !SvROK(sv) /* See prev comment on SvROK */
a1933d95 1022 && (strpos != strbeg) && strpos[-1] != '\n'
d506a20d 1023 /* May be due to an implicit anchor of m{.*foo} */
bbe252da 1024 && !(prog->intflags & PREGf_IMPLICIT))
d506a20d 1025 {
cad2e5aa
JH
1026 t = strpos;
1027 goto find_anchor;
1028 }
a3621e74 1029 DEBUG_EXECUTE_r( if (ml_anch)
f5952150 1030 PerlIO_printf(Perl_debug_log, "Position at offset %ld does not contradict /%s^%s/m...\n",
70685ca0 1031 (long)(strpos - i_strpos), PL_colors[0], PL_colors[1]);
30944b6d 1032 );
2c2d71f5 1033 success_at_start:
bbe252da 1034 if (!(prog->intflags & PREGf_NAUGHTY) /* XXXX If strpos moved? */
f2ed9b32 1035 && (utf8_target ? (
33b8afdf
JH
1036 prog->check_utf8 /* Could be deleted already */
1037 && --BmUSEFUL(prog->check_utf8) < 0
1038 && (prog->check_utf8 == prog->float_utf8)
1039 ) : (
1040 prog->check_substr /* Could be deleted already */
1041 && --BmUSEFUL(prog->check_substr) < 0
1042 && (prog->check_substr == prog->float_substr)
1043 )))
66e933ab 1044 {
cad2e5aa 1045 /* If flags & SOMETHING - do not do it many times on the same match */
a3621e74 1046 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "... Disabling check substring...\n"));
f2ed9b32
KW
1047 /* XXX Does the destruction order has to change with utf8_target? */
1048 SvREFCNT_dec(utf8_target ? prog->check_utf8 : prog->check_substr);
1049 SvREFCNT_dec(utf8_target ? prog->check_substr : prog->check_utf8);
a0714e2c
SS
1050 prog->check_substr = prog->check_utf8 = NULL; /* disable */
1051 prog->float_substr = prog->float_utf8 = NULL; /* clear */
1052 check = NULL; /* abort */
cad2e5aa 1053 s = strpos;
486ec47a 1054 /* XXXX If the check string was an implicit check MBOL, then we need to unset the relevant flag
c9415951
YO
1055 see http://bugs.activestate.com/show_bug.cgi?id=87173 */
1056 if (prog->intflags & PREGf_IMPLICIT)
1057 prog->extflags &= ~RXf_ANCH_MBOL;
3cf5c195
IZ
1058 /* XXXX This is a remnant of the old implementation. It
1059 looks wasteful, since now INTUIT can use many
6eb5f6b9 1060 other heuristics. */
bbe252da 1061 prog->extflags &= ~RXf_USE_INTUIT;
c9415951 1062 /* XXXX What other flags might need to be cleared in this branch? */
cad2e5aa
JH
1063 }
1064 else
1065 s = strpos;
1066 }
1067
6eb5f6b9
JH
1068 /* Last resort... */
1069 /* XXXX BmUSEFUL already changed, maybe multiple change is meaningful... */
1de06328
YO
1070 /* trie stclasses are too expensive to use here, we are better off to
1071 leave it to regmatch itself */
f8fc2ecf 1072 if (progi->regstclass && PL_regkind[OP(progi->regstclass)]!=TRIE) {
6eb5f6b9
JH
1073 /* minlen == 0 is possible if regstclass is \b or \B,
1074 and the fixed substr is ''$.
1075 Since minlen is already taken into account, s+1 is before strend;
1076 accidentally, minlen >= 1 guaranties no false positives at s + 1
1077 even for \b or \B. But (minlen? 1 : 0) below assumes that
1078 regstclass does not come from lookahead... */
1079 /* If regstclass takes bytelength more than 1: If charlength==1, OK.
af944926 1080 This leaves EXACTF-ish only, which are dealt with in find_byclass(). */
f8fc2ecf
YO
1081 const U8* const str = (U8*)STRING(progi->regstclass);
1082 const int cl_l = (PL_regkind[OP(progi->regstclass)] == EXACT
1083 ? CHR_DIST(str+STR_LEN(progi->regstclass), str)
66e933ab 1084 : 1);
1de06328
YO
1085 char * endpos;
1086 if (prog->anchored_substr || prog->anchored_utf8 || ml_anch)
1087 endpos= HOP3c(s, (prog->minlen ? cl_l : 0), strend);
1088 else if (prog->float_substr || prog->float_utf8)
1089 endpos= HOP3c(HOP3c(check_at, -start_shift, strbeg), cl_l, strend);
1090 else
1091 endpos= strend;
1092
d8080198
YO
1093 if (checked_upto < s)
1094 checked_upto = s;
1095 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "start_shift: %"IVdf" check_at: %"IVdf" s: %"IVdf" endpos: %"IVdf" checked_upto: %"IVdf"\n",
1096 (IV)start_shift, (IV)(check_at - strbeg), (IV)(s - strbeg), (IV)(endpos - strbeg), (IV)(checked_upto- strbeg)));
1097
6eb5f6b9 1098 t = s;
d8080198
YO
1099 s = find_byclass(prog, progi->regstclass, checked_upto, endpos, NULL);
1100 if (s) {
1101 checked_upto = s;
1102 } else {
6eb5f6b9 1103#ifdef DEBUGGING
cbbf8932 1104 const char *what = NULL;
6eb5f6b9
JH
1105#endif
1106 if (endpos == strend) {
a3621e74 1107 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
6eb5f6b9
JH
1108 "Could not match STCLASS...\n") );
1109 goto fail;
1110 }
a3621e74 1111 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
66e933ab 1112 "This position contradicts STCLASS...\n") );
bbe252da 1113 if ((prog->extflags & RXf_ANCH) && !ml_anch)
653099ff 1114 goto fail;
d8080198
YO
1115 checked_upto = HOPBACKc(endpos, start_shift);
1116 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "start_shift: %"IVdf" check_at: %"IVdf" endpos: %"IVdf" checked_upto: %"IVdf"\n",
1117 (IV)start_shift, (IV)(check_at - strbeg), (IV)(endpos - strbeg), (IV)(checked_upto- strbeg)));
6eb5f6b9 1118 /* Contradict one of substrings */
33b8afdf 1119 if (prog->anchored_substr || prog->anchored_utf8) {
f2ed9b32 1120 if ((utf8_target ? prog->anchored_utf8 : prog->anchored_substr) == check) {
a3621e74 1121 DEBUG_EXECUTE_r( what = "anchored" );
6eb5f6b9 1122 hop_and_restart:
1aa99e6b 1123 s = HOP3c(t, 1, strend);
66e933ab
GS
1124 if (s + start_shift + end_shift > strend) {
1125 /* XXXX Should be taken into account earlier? */
a3621e74 1126 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
66e933ab
GS
1127 "Could not match STCLASS...\n") );
1128 goto fail;
1129 }
5e39e1e5
HS
1130 if (!check)
1131 goto giveup;
a3621e74 1132 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
f5952150 1133 "Looking for %s substr starting at offset %ld...\n",
6eb5f6b9
JH
1134 what, (long)(s + start_shift - i_strpos)) );
1135 goto restart;
1136 }
66e933ab 1137 /* Have both, check_string is floating */
6eb5f6b9
JH
1138 if (t + start_shift >= check_at) /* Contradicts floating=check */
1139 goto retry_floating_check;
1140 /* Recheck anchored substring, but not floating... */
9041c2e3 1141 s = check_at;
5e39e1e5
HS
1142 if (!check)
1143 goto giveup;
a3621e74 1144 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
f5952150 1145 "Looking for anchored substr starting at offset %ld...\n",
6eb5f6b9
JH
1146 (long)(other_last - i_strpos)) );
1147 goto do_other_anchored;
1148 }
60e71179
GS
1149 /* Another way we could have checked stclass at the
1150 current position only: */
1151 if (ml_anch) {
1152 s = t = t + 1;
5e39e1e5
HS
1153 if (!check)
1154 goto giveup;
a3621e74 1155 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
f5952150 1156 "Looking for /%s^%s/m starting at offset %ld...\n",
e4584336 1157 PL_colors[0], PL_colors[1], (long)(t - i_strpos)) );
60e71179 1158 goto try_at_offset;
66e933ab 1159 }
f2ed9b32 1160 if (!(utf8_target ? prog->float_utf8 : prog->float_substr)) /* Could have been deleted */
60e71179 1161 goto fail;
486ec47a 1162 /* Check is floating substring. */
6eb5f6b9
JH
1163 retry_floating_check:
1164 t = check_at - start_shift;
a3621e74 1165 DEBUG_EXECUTE_r( what = "floating" );
6eb5f6b9
JH
1166 goto hop_and_restart;
1167 }
b7953727 1168 if (t != s) {
a3621e74 1169 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
6eb5f6b9 1170 "By STCLASS: moving %ld --> %ld\n",
b7953727
JH
1171 (long)(t - i_strpos), (long)(s - i_strpos))
1172 );
1173 }
1174 else {
a3621e74 1175 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
b7953727
JH
1176 "Does not contradict STCLASS...\n");
1177 );
1178 }
6eb5f6b9 1179 }
5e39e1e5 1180 giveup:
a3621e74 1181 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%s%s:%s match at offset %ld\n",
5e39e1e5
HS
1182 PL_colors[4], (check ? "Guessed" : "Giving up"),
1183 PL_colors[5], (long)(s - i_strpos)) );
cad2e5aa 1184 return s;
2c2d71f5
JH
1185
1186 fail_finish: /* Substring not found */
33b8afdf 1187 if (prog->check_substr || prog->check_utf8) /* could be removed already */
f2ed9b32 1188 BmUSEFUL(utf8_target ? prog->check_utf8 : prog->check_substr) += 5; /* hooray */
cad2e5aa 1189 fail:
a3621e74 1190 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch rejected by optimizer%s\n",
e4584336 1191 PL_colors[4], PL_colors[5]));
bd61b366 1192 return NULL;
cad2e5aa 1193}
9661b544 1194
a0a388a1
YO
1195#define DECL_TRIE_TYPE(scan) \
1196 const enum { trie_plain, trie_utf8, trie_utf8_fold, trie_latin_utf8_fold } \
fab2782b
YO
1197 trie_type = ((scan->flags == EXACT) \
1198 ? (utf8_target ? trie_utf8 : trie_plain) \
1199 : (utf8_target ? trie_utf8_fold : trie_latin_utf8_fold))
1200
1201#define REXEC_TRIE_READ_CHAR(trie_type, trie, widecharmap, uc, uscan, len, \
1202uvc, charid, foldlen, foldbuf, uniflags) STMT_START { \
1203 STRLEN skiplen; \
1204 switch (trie_type) { \
1205 case trie_utf8_fold: \
1206 if ( foldlen>0 ) { \
1207 uvc = utf8n_to_uvuni( (const U8*) uscan, UTF8_MAXLEN, &len, uniflags ); \
1208 foldlen -= len; \
1209 uscan += len; \
1210 len=0; \
1211 } else { \
1212 uvc = to_utf8_fold( (const U8*) uc, foldbuf, &foldlen ); \
1213 len = UTF8SKIP(uc); \
1214 skiplen = UNISKIP( uvc ); \
1215 foldlen -= skiplen; \
1216 uscan = foldbuf + skiplen; \
1217 } \
1218 break; \
1219 case trie_latin_utf8_fold: \
1220 if ( foldlen>0 ) { \
1221 uvc = utf8n_to_uvuni( (const U8*) uscan, UTF8_MAXLEN, &len, uniflags ); \
1222 foldlen -= len; \
1223 uscan += len; \
1224 len=0; \
1225 } else { \
1226 len = 1; \
1227 uvc = _to_fold_latin1( (U8) *uc, foldbuf, &foldlen, 1); \
1228 skiplen = UNISKIP( uvc ); \
1229 foldlen -= skiplen; \
1230 uscan = foldbuf + skiplen; \
1231 } \
1232 break; \
1233 case trie_utf8: \
1234 uvc = utf8n_to_uvuni( (const U8*) uc, UTF8_MAXLEN, &len, uniflags ); \
1235 break; \
1236 case trie_plain: \
1237 uvc = (UV)*uc; \
1238 len = 1; \
1239 } \
1240 if (uvc < 256) { \
1241 charid = trie->charmap[ uvc ]; \
1242 } \
1243 else { \
1244 charid = 0; \
1245 if (widecharmap) { \
1246 SV** const svpp = hv_fetch(widecharmap, \
1247 (char*)&uvc, sizeof(UV), 0); \
1248 if (svpp) \
1249 charid = (U16)SvIV(*svpp); \
1250 } \
1251 } \
4cadc6a9
YO
1252} STMT_END
1253
4cadc6a9
YO
1254#define REXEC_FBC_EXACTISH_SCAN(CoNd) \
1255STMT_START { \
1256 while (s <= e) { \
1257 if ( (CoNd) \
fac1af77 1258 && (ln == 1 || folder(s, pat_string, ln)) \
9a5a5549 1259 && (!reginfo || regtry(reginfo, &s)) ) \
4cadc6a9
YO
1260 goto got_it; \
1261 s++; \
1262 } \
1263} STMT_END
1264
1265#define REXEC_FBC_UTF8_SCAN(CoDe) \
1266STMT_START { \
7016d6eb 1267 while (s < strend && s + (uskip = UTF8SKIP(s)) <= strend) { \
4cadc6a9
YO
1268 CoDe \
1269 s += uskip; \
1270 } \
1271} STMT_END
1272
1273#define REXEC_FBC_SCAN(CoDe) \
1274STMT_START { \
1275 while (s < strend) { \
1276 CoDe \
1277 s++; \
1278 } \
1279} STMT_END
1280
1281#define REXEC_FBC_UTF8_CLASS_SCAN(CoNd) \
1282REXEC_FBC_UTF8_SCAN( \
1283 if (CoNd) { \
24b23f37 1284 if (tmp && (!reginfo || regtry(reginfo, &s))) \
4cadc6a9
YO
1285 goto got_it; \
1286 else \
1287 tmp = doevery; \
1288 } \
1289 else \
1290 tmp = 1; \
1291)
1292
1293#define REXEC_FBC_CLASS_SCAN(CoNd) \
1294REXEC_FBC_SCAN( \
1295 if (CoNd) { \
24b23f37 1296 if (tmp && (!reginfo || regtry(reginfo, &s))) \
4cadc6a9
YO
1297 goto got_it; \
1298 else \
1299 tmp = doevery; \
1300 } \
1301 else \
1302 tmp = 1; \
1303)
1304
1305#define REXEC_FBC_TRYIT \
24b23f37 1306if ((!reginfo || regtry(reginfo, &s))) \
4cadc6a9
YO
1307 goto got_it
1308
e1d1eefb 1309#define REXEC_FBC_CSCAN(CoNdUtF8,CoNd) \
f2ed9b32 1310 if (utf8_target) { \
e1d1eefb
YO
1311 REXEC_FBC_UTF8_CLASS_SCAN(CoNdUtF8); \
1312 } \
1313 else { \
1314 REXEC_FBC_CLASS_SCAN(CoNd); \
d981ef24 1315 }
e1d1eefb 1316
4cadc6a9 1317#define REXEC_FBC_CSCAN_PRELOAD(UtFpReLoAd,CoNdUtF8,CoNd) \
f2ed9b32 1318 if (utf8_target) { \
4cadc6a9
YO
1319 UtFpReLoAd; \
1320 REXEC_FBC_UTF8_CLASS_SCAN(CoNdUtF8); \
1321 } \
1322 else { \
1323 REXEC_FBC_CLASS_SCAN(CoNd); \
d981ef24 1324 }
4cadc6a9
YO
1325
1326#define REXEC_FBC_CSCAN_TAINT(CoNdUtF8,CoNd) \
1327 PL_reg_flags |= RF_tainted; \
f2ed9b32 1328 if (utf8_target) { \
4cadc6a9
YO
1329 REXEC_FBC_UTF8_CLASS_SCAN(CoNdUtF8); \
1330 } \
1331 else { \
1332 REXEC_FBC_CLASS_SCAN(CoNd); \
d981ef24 1333 }
4cadc6a9 1334
786e8c11
YO
1335#define DUMP_EXEC_POS(li,s,doutf8) \
1336 dump_exec_pos(li,s,(PL_regeol),(PL_bostr),(PL_reg_starttry),doutf8)
1337
cfaf538b
KW
1338
1339#define UTF8_NOLOAD(TEST_NON_UTF8, IF_SUCCESS, IF_FAIL) \
1340 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n'; \
1341 tmp = TEST_NON_UTF8(tmp); \
1342 REXEC_FBC_UTF8_SCAN( \
1343 if (tmp == ! TEST_NON_UTF8((U8) *s)) { \
1344 tmp = !tmp; \
1345 IF_SUCCESS; \
1346 } \
1347 else { \
1348 IF_FAIL; \
1349 } \
1350 ); \
1351
1352#define UTF8_LOAD(TeSt1_UtF8, TeSt2_UtF8, IF_SUCCESS, IF_FAIL) \
1353 if (s == PL_bostr) { \
1354 tmp = '\n'; \
1355 } \
1356 else { \
1357 U8 * const r = reghop3((U8*)s, -1, (U8*)PL_bostr); \
1358 tmp = utf8n_to_uvchr(r, UTF8SKIP(r), 0, UTF8_ALLOW_DEFAULT); \
1359 } \
1360 tmp = TeSt1_UtF8; \
1361 LOAD_UTF8_CHARCLASS_ALNUM(); \
1362 REXEC_FBC_UTF8_SCAN( \
1363 if (tmp == ! (TeSt2_UtF8)) { \
1364 tmp = !tmp; \
1365 IF_SUCCESS; \
1366 } \
1367 else { \
1368 IF_FAIL; \
1369 } \
1370 ); \
1371
63ac0dad
KW
1372/* The only difference between the BOUND and NBOUND cases is that
1373 * REXEC_FBC_TRYIT is called when matched in BOUND, and when non-matched in
1374 * NBOUND. This is accomplished by passing it in either the if or else clause,
1375 * with the other one being empty */
1376#define FBC_BOUND(TEST_NON_UTF8, TEST1_UTF8, TEST2_UTF8) \
1dcf4a1b 1377 FBC_BOUND_COMMON(UTF8_LOAD(TEST1_UTF8, TEST2_UTF8, REXEC_FBC_TRYIT, PLACEHOLDER), TEST_NON_UTF8, REXEC_FBC_TRYIT, PLACEHOLDER)
cfaf538b
KW
1378
1379#define FBC_BOUND_NOLOAD(TEST_NON_UTF8, TEST1_UTF8, TEST2_UTF8) \
1dcf4a1b 1380 FBC_BOUND_COMMON(UTF8_NOLOAD(TEST_NON_UTF8, REXEC_FBC_TRYIT, PLACEHOLDER), TEST_NON_UTF8, REXEC_FBC_TRYIT, PLACEHOLDER)
63ac0dad
KW
1381
1382#define FBC_NBOUND(TEST_NON_UTF8, TEST1_UTF8, TEST2_UTF8) \
1dcf4a1b 1383 FBC_BOUND_COMMON(UTF8_LOAD(TEST1_UTF8, TEST2_UTF8, PLACEHOLDER, REXEC_FBC_TRYIT), TEST_NON_UTF8, PLACEHOLDER, REXEC_FBC_TRYIT)
cfaf538b
KW
1384
1385#define FBC_NBOUND_NOLOAD(TEST_NON_UTF8, TEST1_UTF8, TEST2_UTF8) \
1dcf4a1b 1386 FBC_BOUND_COMMON(UTF8_NOLOAD(TEST_NON_UTF8, PLACEHOLDER, REXEC_FBC_TRYIT), TEST_NON_UTF8, PLACEHOLDER, REXEC_FBC_TRYIT)
cfaf538b 1387
63ac0dad
KW
1388
1389/* Common to the BOUND and NBOUND cases. Unfortunately the UTF8 tests need to
1390 * be passed in completely with the variable name being tested, which isn't
1391 * such a clean interface, but this is easier to read than it was before. We
1392 * are looking for the boundary (or non-boundary between a word and non-word
1393 * character. The utf8 and non-utf8 cases have the same logic, but the details
1394 * must be different. Find the "wordness" of the character just prior to this
1395 * one, and compare it with the wordness of this one. If they differ, we have
1396 * a boundary. At the beginning of the string, pretend that the previous
1397 * character was a new-line */
cfaf538b 1398#define FBC_BOUND_COMMON(UTF8_CODE, TEST_NON_UTF8, IF_SUCCESS, IF_FAIL) \
63ac0dad 1399 if (utf8_target) { \
cfaf538b 1400 UTF8_CODE \
63ac0dad
KW
1401 } \
1402 else { /* Not utf8 */ \
1403 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n'; \
1404 tmp = TEST_NON_UTF8(tmp); \
1405 REXEC_FBC_SCAN( \
1406 if (tmp == ! TEST_NON_UTF8((U8) *s)) { \
1407 tmp = !tmp; \
1408 IF_SUCCESS; \
1409 } \
1410 else { \
1411 IF_FAIL; \
1412 } \
1413 ); \
1414 } \
1415 if ((!prog->minlen && tmp) && (!reginfo || regtry(reginfo, &s))) \
1416 goto got_it;
1417
786e8c11
YO
1418/* We know what class REx starts with. Try to find this position... */
1419/* if reginfo is NULL, its a dryrun */
1420/* annoyingly all the vars in this routine have different names from their counterparts
1421 in regmatch. /grrr */
1422
3c3eec57 1423STATIC char *
07be1b83 1424S_find_byclass(pTHX_ regexp * prog, const regnode *c, char *s,
24b23f37 1425 const char *strend, regmatch_info *reginfo)
a687059c 1426{
27da23d5 1427 dVAR;
bbe252da 1428 const I32 doevery = (prog->intflags & PREGf_SKIP) == 0;
fac1af77
KW
1429 char *pat_string; /* The pattern's exactish string */
1430 char *pat_end; /* ptr to end char of pat_string */
1431 re_fold_t folder; /* Function for computing non-utf8 folds */
1432 const U8 *fold_array; /* array for folding ords < 256 */
d8093b23 1433 STRLEN ln;
5dab1207 1434 STRLEN lnc;
eb578fdb 1435 STRLEN uskip;
fac1af77
KW
1436 U8 c1;
1437 U8 c2;
6eb5f6b9 1438 char *e;
eb578fdb
KW
1439 I32 tmp = 1; /* Scratch variable? */
1440 const bool utf8_target = PL_reg_match_utf8;
453bfd44 1441 UV utf8_fold_flags = 0;
f8fc2ecf 1442 RXi_GET_DECL(prog,progi);
7918f24d
NC
1443
1444 PERL_ARGS_ASSERT_FIND_BYCLASS;
f8fc2ecf 1445
6eb5f6b9
JH
1446 /* We know what class it must start with. */
1447 switch (OP(c)) {
f56b6394 1448 case ANYOFV:
6eb5f6b9 1449 case ANYOF:
f56b6394 1450 if (utf8_target || OP(c) == ANYOFV) {
b1e3e569
KW
1451 STRLEN inclasslen = strend - s;
1452 REXEC_FBC_UTF8_CLASS_SCAN(
1453 reginclass(prog, c, (U8*)s, &inclasslen, utf8_target));
388cc4de
HS
1454 }
1455 else {
6ef69d56 1456 REXEC_FBC_CLASS_SCAN(REGINCLASS(prog, c, (U8*)s));
a0d0e21e 1457 }
6eb5f6b9 1458 break;
f33976b4 1459 case CANY:
4cadc6a9 1460 REXEC_FBC_SCAN(
24b23f37 1461 if (tmp && (!reginfo || regtry(reginfo, &s)))
f33976b4
DB
1462 goto got_it;
1463 else
1464 tmp = doevery;
4cadc6a9 1465 );
f33976b4 1466 break;
2f7f8cb1
KW
1467
1468 case EXACTFA:
1469 if (UTF_PATTERN || utf8_target) {
1470 utf8_fold_flags = FOLDEQ_UTF8_NOMIX_ASCII;
1471 goto do_exactf_utf8;
1472 }
1473 fold_array = PL_fold_latin1; /* Latin1 folds are not affected by */
1474 folder = foldEQ_latin1; /* /a, except the sharp s one which */
1475 goto do_exactf_non_utf8; /* isn't dealt with by these */
1476
6eb5f6b9 1477 case EXACTF:
62bf7766 1478 if (utf8_target) {
77a6d856
KW
1479
1480 /* regcomp.c already folded this if pattern is in UTF-8 */
62bf7766 1481 utf8_fold_flags = 0;
fac1af77
KW
1482 goto do_exactf_utf8;
1483 }
1484 fold_array = PL_fold;
1485 folder = foldEQ;
1486 goto do_exactf_non_utf8;
1487
1488 case EXACTFL:
1489 if (UTF_PATTERN || utf8_target) {
17580e7a 1490 utf8_fold_flags = FOLDEQ_UTF8_LOCALE;
fac1af77
KW
1491 goto do_exactf_utf8;
1492 }
1493 fold_array = PL_fold_locale;
1494 folder = foldEQ_locale;
16d951b7
KW
1495 goto do_exactf_non_utf8;
1496
3c760661
KW
1497 case EXACTFU_SS:
1498 if (UTF_PATTERN) {
1499 utf8_fold_flags = FOLDEQ_S2_ALREADY_FOLDED;
1500 }
1501 goto do_exactf_utf8;
1502
fab2782b 1503 case EXACTFU_TRICKYFOLD:
16d951b7
KW
1504 case EXACTFU:
1505 if (UTF_PATTERN || utf8_target) {
77a6d856 1506 utf8_fold_flags = (UTF_PATTERN) ? FOLDEQ_S2_ALREADY_FOLDED : 0;
16d951b7
KW
1507 goto do_exactf_utf8;
1508 }
1509
1510 /* Any 'ss' in the pattern should have been replaced by regcomp,
1511 * so we don't have to worry here about this single special case
1512 * in the Latin1 range */
1513 fold_array = PL_fold_latin1;
1514 folder = foldEQ_latin1;
fac1af77
KW
1515
1516 /* FALL THROUGH */
1517
62bf7766
KW
1518 do_exactf_non_utf8: /* Neither pattern nor string are UTF8, and there
1519 are no glitches with fold-length differences
1520 between the target string and pattern */
fac1af77
KW
1521
1522 /* The idea in the non-utf8 EXACTF* cases is to first find the
1523 * first character of the EXACTF* node and then, if necessary,
1524 * case-insensitively compare the full text of the node. c1 is the
1525 * first character. c2 is its fold. This logic will not work for
1526 * Unicode semantics and the german sharp ss, which hence should
1527 * not be compiled into a node that gets here. */
1528 pat_string = STRING(c);
1529 ln = STR_LEN(c); /* length to match in octets/bytes */
1530
8a90a8fe
KW
1531 /* We know that we have to match at least 'ln' bytes (which is the
1532 * same as characters, since not utf8). If we have to match 3
1533 * characters, and there are only 2 availabe, we know without
1534 * trying that it will fail; so don't start a match past the
1535 * required minimum number from the far end */
fac1af77
KW
1536 e = HOP3c(strend, -((I32)ln), s);
1537
1538 if (!reginfo && e < s) {
1539 e = s; /* Due to minlen logic of intuit() */
1540 }
1541
1542 c1 = *pat_string;
1543 c2 = fold_array[c1];
1544 if (c1 == c2) { /* If char and fold are the same */
1545 REXEC_FBC_EXACTISH_SCAN(*(U8*)s == c1);
1546 }
1547 else {
1548 REXEC_FBC_EXACTISH_SCAN(*(U8*)s == c1 || *(U8*)s == c2);
1549 }
1550 break;
1551
1552 do_exactf_utf8:
e067297c
KW
1553 {
1554 unsigned expansion;
1555
fac1af77
KW
1556
1557 /* If one of the operands is in utf8, we can't use the simpler
1558 * folding above, due to the fact that many different characters
1559 * can have the same fold, or portion of a fold, or different-
1560 * length fold */
1561 pat_string = STRING(c);
1562 ln = STR_LEN(c); /* length to match in octets/bytes */
1563 pat_end = pat_string + ln;
1564 lnc = (UTF_PATTERN) /* length to match in characters */
1565 ? utf8_length((U8 *) pat_string, (U8 *) pat_end)
1566 : ln;
1567
e067297c
KW
1568 /* We have 'lnc' characters to match in the pattern, but because of
1569 * multi-character folding, each character in the target can match
1570 * up to 3 characters (Unicode guarantees it will never exceed
1571 * this) if it is utf8-encoded; and up to 2 if not (based on the
1572 * fact that the Latin 1 folds are already determined, and the
1573 * only multi-char fold in that range is the sharp-s folding to
1574 * 'ss'. Thus, a pattern character can match as little as 1/3 of a
89378d8a
KW
1575 * string character. Adjust lnc accordingly, rounding up, so that
1576 * if we need to match at least 4+1/3 chars, that really is 5. */
e067297c 1577 expansion = (utf8_target) ? UTF8_MAX_FOLD_CHAR_EXPAND : 2;
89378d8a 1578 lnc = (lnc + expansion - 1) / expansion;
e067297c
KW
1579
1580 /* As in the non-UTF8 case, if we have to match 3 characters, and
1581 * only 2 are left, it's guaranteed to fail, so don't start a
1582 * match that would require us to go beyond the end of the string
1583 */
1584 e = HOP3c(strend, -((I32)lnc), s);
fac1af77
KW
1585
1586 if (!reginfo && e < s) {
1587 e = s; /* Due to minlen logic of intuit() */
1588 }
1589
b33105db
KW
1590 /* XXX Note that we could recalculate e to stop the loop earlier,
1591 * as the worst case expansion above will rarely be met, and as we
1592 * go along we would usually find that e moves further to the left.
1593 * This would happen only after we reached the point in the loop
1594 * where if there were no expansion we should fail. Unclear if
1595 * worth the expense */
e067297c 1596
fac1af77
KW
1597 while (s <= e) {
1598 char *my_strend= (char *)strend;
1599 if (foldEQ_utf8_flags(s, &my_strend, 0, utf8_target,
1600 pat_string, NULL, ln, cBOOL(UTF_PATTERN), utf8_fold_flags)
1601 && (!reginfo || regtry(reginfo, &s)) )
1602 {
1603 goto got_it;
1604 }
bbdd8bad 1605 s += (utf8_target) ? UTF8SKIP(s) : 1;
fac1af77
KW
1606 }
1607 break;
e067297c 1608 }
bbce6d69 1609 case BOUNDL:
3280af22 1610 PL_reg_flags |= RF_tainted;
63ac0dad
KW
1611 FBC_BOUND(isALNUM_LC,
1612 isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp)),
1613 isALNUM_LC_utf8((U8*)s));
a0ed51b3 1614 break;
bbce6d69 1615 case NBOUNDL:
3280af22 1616 PL_reg_flags |= RF_tainted;
63ac0dad
KW
1617 FBC_NBOUND(isALNUM_LC,
1618 isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp)),
1619 isALNUM_LC_utf8((U8*)s));
1620 break;
1621 case BOUND:
1622 FBC_BOUND(isWORDCHAR,
1623 isALNUM_uni(tmp),
1624 cBOOL(swash_fetch(PL_utf8_alnum, (U8*)s, utf8_target)));
1625 break;
cfaf538b
KW
1626 case BOUNDA:
1627 FBC_BOUND_NOLOAD(isWORDCHAR_A,
1628 isWORDCHAR_A(tmp),
1629 isWORDCHAR_A((U8*)s));
1630 break;
a0d0e21e 1631 case NBOUND:
63ac0dad
KW
1632 FBC_NBOUND(isWORDCHAR,
1633 isALNUM_uni(tmp),
1634 cBOOL(swash_fetch(PL_utf8_alnum, (U8*)s, utf8_target)));
1635 break;
cfaf538b
KW
1636 case NBOUNDA:
1637 FBC_NBOUND_NOLOAD(isWORDCHAR_A,
1638 isWORDCHAR_A(tmp),
1639 isWORDCHAR_A((U8*)s));
1640 break;
63ac0dad
KW
1641 case BOUNDU:
1642 FBC_BOUND(isWORDCHAR_L1,
1643 isALNUM_uni(tmp),
1644 cBOOL(swash_fetch(PL_utf8_alnum, (U8*)s, utf8_target)));
1645 break;
1646 case NBOUNDU:
1647 FBC_NBOUND(isWORDCHAR_L1,
1648 isALNUM_uni(tmp),
1649 cBOOL(swash_fetch(PL_utf8_alnum, (U8*)s, utf8_target)));
a0ed51b3 1650 break;
bbce6d69 1651 case ALNUML:
4cadc6a9
YO
1652 REXEC_FBC_CSCAN_TAINT(
1653 isALNUM_LC_utf8((U8*)s),
1654 isALNUM_LC(*s)
1655 );
6895a8aa 1656 break;
980866de
KW
1657 case ALNUMU:
1658 REXEC_FBC_CSCAN_PRELOAD(
779d7b58
KW
1659 LOAD_UTF8_CHARCLASS_ALNUM(),
1660 swash_fetch(PL_utf8_alnum,(U8*)s, utf8_target),
980866de
KW
1661 isWORDCHAR_L1((U8) *s)
1662 );
6895a8aa 1663 break;
980866de
KW
1664 case ALNUM:
1665 REXEC_FBC_CSCAN_PRELOAD(
779d7b58
KW
1666 LOAD_UTF8_CHARCLASS_ALNUM(),
1667 swash_fetch(PL_utf8_alnum,(U8*)s, utf8_target),
980866de
KW
1668 isWORDCHAR((U8) *s)
1669 );
6895a8aa 1670 break;
cfaf538b 1671 case ALNUMA:
8e9da4d4
KW
1672 /* Don't need to worry about utf8, as it can match only a single
1673 * byte invariant character */
cfaf538b 1674 REXEC_FBC_CLASS_SCAN( isWORDCHAR_A(*s));
6895a8aa 1675 break;
980866de
KW
1676 case NALNUMU:
1677 REXEC_FBC_CSCAN_PRELOAD(
779d7b58 1678 LOAD_UTF8_CHARCLASS_ALNUM(),
359960d4 1679 !swash_fetch(PL_utf8_alnum,(U8*)s, utf8_target),
980866de
KW
1680 ! isWORDCHAR_L1((U8) *s)
1681 );
6895a8aa 1682 break;
a0d0e21e 1683 case NALNUM:
4cadc6a9 1684 REXEC_FBC_CSCAN_PRELOAD(
779d7b58
KW
1685 LOAD_UTF8_CHARCLASS_ALNUM(),
1686 !swash_fetch(PL_utf8_alnum, (U8*)s, utf8_target),
980866de 1687 ! isALNUM(*s)
4cadc6a9 1688 );
6895a8aa 1689 break;
cfaf538b 1690 case NALNUMA:
8e9da4d4
KW
1691 REXEC_FBC_CSCAN(
1692 !isWORDCHAR_A(*s),
1693 !isWORDCHAR_A(*s)
1694 );
1695 break;
bbce6d69 1696 case NALNUML:
4cadc6a9
YO
1697 REXEC_FBC_CSCAN_TAINT(
1698 !isALNUM_LC_utf8((U8*)s),
1699 !isALNUM_LC(*s)
1700 );
6895a8aa 1701 break;
980866de
KW
1702 case SPACEU:
1703 REXEC_FBC_CSCAN_PRELOAD(
779d7b58
KW
1704 LOAD_UTF8_CHARCLASS_SPACE(),
1705 *s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, utf8_target),
980866de
KW
1706 isSPACE_L1((U8) *s)
1707 );
6895a8aa 1708 break;
a0d0e21e 1709 case SPACE:
4cadc6a9 1710 REXEC_FBC_CSCAN_PRELOAD(
779d7b58
KW
1711 LOAD_UTF8_CHARCLASS_SPACE(),
1712 *s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, utf8_target),
980866de 1713 isSPACE((U8) *s)
4cadc6a9 1714 );
6895a8aa 1715 break;
cfaf538b 1716 case SPACEA:
8e9da4d4
KW
1717 /* Don't need to worry about utf8, as it can match only a single
1718 * byte invariant character */
cfaf538b 1719 REXEC_FBC_CLASS_SCAN( isSPACE_A(*s));
6895a8aa 1720 break;
bbce6d69 1721 case SPACEL:
4cadc6a9 1722 REXEC_FBC_CSCAN_TAINT(
6bbba904 1723 isSPACE_LC_utf8((U8*)s),
4cadc6a9
YO
1724 isSPACE_LC(*s)
1725 );
6895a8aa 1726 break;
980866de
KW
1727 case NSPACEU:
1728 REXEC_FBC_CSCAN_PRELOAD(
779d7b58
KW
1729 LOAD_UTF8_CHARCLASS_SPACE(),
1730 !( *s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, utf8_target)),
980866de
KW
1731 ! isSPACE_L1((U8) *s)
1732 );
6895a8aa 1733 break;
a0d0e21e 1734 case NSPACE:
4cadc6a9 1735 REXEC_FBC_CSCAN_PRELOAD(
779d7b58
KW
1736 LOAD_UTF8_CHARCLASS_SPACE(),
1737 !(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, utf8_target)),
980866de 1738 ! isSPACE((U8) *s)
4cadc6a9 1739 );
6895a8aa 1740 break;
cfaf538b 1741 case NSPACEA:
8e9da4d4
KW
1742 REXEC_FBC_CSCAN(
1743 !isSPACE_A(*s),
1744 !isSPACE_A(*s)
1745 );
1746 break;
bbce6d69 1747 case NSPACEL:
4cadc6a9 1748 REXEC_FBC_CSCAN_TAINT(
6bbba904 1749 !isSPACE_LC_utf8((U8*)s),
4cadc6a9
YO
1750 !isSPACE_LC(*s)
1751 );
6895a8aa 1752 break;
a0d0e21e 1753 case DIGIT:
4cadc6a9 1754 REXEC_FBC_CSCAN_PRELOAD(
779d7b58
KW
1755 LOAD_UTF8_CHARCLASS_DIGIT(),
1756 swash_fetch(PL_utf8_digit,(U8*)s, utf8_target),
4cadc6a9
YO
1757 isDIGIT(*s)
1758 );
6895a8aa 1759 break;
cfaf538b 1760 case DIGITA:
8e9da4d4
KW
1761 /* Don't need to worry about utf8, as it can match only a single
1762 * byte invariant character */
cfaf538b 1763 REXEC_FBC_CLASS_SCAN( isDIGIT_A(*s));
6895a8aa 1764 break;
b8c5462f 1765 case DIGITL:
4cadc6a9
YO
1766 REXEC_FBC_CSCAN_TAINT(
1767 isDIGIT_LC_utf8((U8*)s),
1768 isDIGIT_LC(*s)
1769 );
6895a8aa 1770 break;
a0d0e21e 1771 case NDIGIT:
4cadc6a9 1772 REXEC_FBC_CSCAN_PRELOAD(
779d7b58
KW
1773 LOAD_UTF8_CHARCLASS_DIGIT(),
1774 !swash_fetch(PL_utf8_digit,(U8*)s, utf8_target),
4cadc6a9
YO
1775 !isDIGIT(*s)
1776 );
6895a8aa 1777 break;
cfaf538b 1778 case NDIGITA:
8e9da4d4
KW
1779 REXEC_FBC_CSCAN(
1780 !isDIGIT_A(*s),
1781 !isDIGIT_A(*s)
1782 );
1783 break;
b8c5462f 1784 case NDIGITL:
4cadc6a9
YO
1785 REXEC_FBC_CSCAN_TAINT(
1786 !isDIGIT_LC_utf8((U8*)s),
1787 !isDIGIT_LC(*s)
1788 );
6895a8aa 1789 break;
e1d1eefb
YO
1790 case LNBREAK:
1791 REXEC_FBC_CSCAN(
7016d6eb
DM
1792 is_LNBREAK_utf8_safe(s, strend),
1793 is_LNBREAK_latin1_safe(s, strend)
e1d1eefb 1794 );
6895a8aa 1795 break;
e1d1eefb
YO
1796 case VERTWS:
1797 REXEC_FBC_CSCAN(
7016d6eb
DM
1798 is_VERTWS_utf8_safe(s, strend),
1799 is_VERTWS_latin1_safe(s, strend)
e1d1eefb 1800 );
6895a8aa 1801 break;
e1d1eefb
YO
1802 case NVERTWS:
1803 REXEC_FBC_CSCAN(
7016d6eb
DM
1804 !is_VERTWS_utf8_safe(s, strend),
1805 !is_VERTWS_latin1_safe(s, strend)
e1d1eefb 1806 );
6895a8aa 1807 break;
e1d1eefb
YO
1808 case HORIZWS:
1809 REXEC_FBC_CSCAN(
7016d6eb
DM
1810 is_HORIZWS_utf8_safe(s, strend),
1811 is_HORIZWS_latin1_safe(s, strend)
e1d1eefb 1812 );
6895a8aa 1813 break;
e1d1eefb
YO
1814 case NHORIZWS:
1815 REXEC_FBC_CSCAN(
7016d6eb
DM
1816 !is_HORIZWS_utf8_safe(s, strend),
1817 !is_HORIZWS_latin1_safe(s, strend)
e1d1eefb 1818 );
6895a8aa 1819 break;
0658cdde
KW
1820 case POSIXA:
1821 /* Don't need to worry about utf8, as it can match only a single
1822 * byte invariant character. The flag in this node type is the
1823 * class number to pass to _generic_isCC() to build a mask for
1824 * searching in PL_charclass[] */
1825 REXEC_FBC_CLASS_SCAN( _generic_isCC_A(*s, FLAGS(c)));
1826 break;
1827 case NPOSIXA:
1828 REXEC_FBC_CSCAN(
1829 !_generic_isCC_A(*s, FLAGS(c)),
1830 !_generic_isCC_A(*s, FLAGS(c))
1831 );
1832 break;
1833
1de06328
YO
1834 case AHOCORASICKC:
1835 case AHOCORASICK:
07be1b83 1836 {
a0a388a1 1837 DECL_TRIE_TYPE(c);
07be1b83
YO
1838 /* what trie are we using right now */
1839 reg_ac_data *aho
f8fc2ecf 1840 = (reg_ac_data*)progi->data->data[ ARG( c ) ];
3251b653
NC
1841 reg_trie_data *trie
1842 = (reg_trie_data*)progi->data->data[ aho->trie ];
85fbaab2 1843 HV *widecharmap = MUTABLE_HV(progi->data->data[ aho->trie + 1 ]);
07be1b83
YO
1844
1845 const char *last_start = strend - trie->minlen;
6148ee25 1846#ifdef DEBUGGING
07be1b83 1847 const char *real_start = s;
6148ee25 1848#endif
07be1b83 1849 STRLEN maxlen = trie->maxlen;
be8e71aa
YO
1850 SV *sv_points;
1851 U8 **points; /* map of where we were in the input string
786e8c11 1852 when reading a given char. For ASCII this
be8e71aa 1853 is unnecessary overhead as the relationship
38a44b82
NC
1854 is always 1:1, but for Unicode, especially
1855 case folded Unicode this is not true. */
f9e705e8 1856 U8 foldbuf[ UTF8_MAXBYTES_CASE + 1 ];
786e8c11
YO
1857 U8 *bitmap=NULL;
1858
07be1b83
YO
1859
1860 GET_RE_DEBUG_FLAGS_DECL;
1861
be8e71aa
YO
1862 /* We can't just allocate points here. We need to wrap it in
1863 * an SV so it gets freed properly if there is a croak while
1864 * running the match */
1865 ENTER;
1866 SAVETMPS;
1867 sv_points=newSV(maxlen * sizeof(U8 *));
1868 SvCUR_set(sv_points,
1869 maxlen * sizeof(U8 *));
1870 SvPOK_on(sv_points);
1871 sv_2mortal(sv_points);
1872 points=(U8**)SvPV_nolen(sv_points );
1de06328
YO
1873 if ( trie_type != trie_utf8_fold
1874 && (trie->bitmap || OP(c)==AHOCORASICKC) )
1875 {
786e8c11
YO
1876 if (trie->bitmap)
1877 bitmap=(U8*)trie->bitmap;
1878 else
1879 bitmap=(U8*)ANYOF_BITMAP(c);
07be1b83 1880 }
786e8c11
YO
1881 /* this is the Aho-Corasick algorithm modified a touch
1882 to include special handling for long "unknown char"
1883 sequences. The basic idea being that we use AC as long
1884 as we are dealing with a possible matching char, when
1885 we encounter an unknown char (and we have not encountered
1886 an accepting state) we scan forward until we find a legal
1887 starting char.
1888 AC matching is basically that of trie matching, except
1889 that when we encounter a failing transition, we fall back
1890 to the current states "fail state", and try the current char
1891 again, a process we repeat until we reach the root state,
1892 state 1, or a legal transition. If we fail on the root state
1893 then we can either terminate if we have reached an accepting
1894 state previously, or restart the entire process from the beginning
1895 if we have not.
1896
1897 */
07be1b83
YO
1898 while (s <= last_start) {
1899 const U32 uniflags = UTF8_ALLOW_DEFAULT;
1900 U8 *uc = (U8*)s;
1901 U16 charid = 0;
1902 U32 base = 1;
1903 U32 state = 1;
1904 UV uvc = 0;
1905 STRLEN len = 0;
1906 STRLEN foldlen = 0;
1907 U8 *uscan = (U8*)NULL;
1908 U8 *leftmost = NULL;
786e8c11
YO
1909#ifdef DEBUGGING
1910 U32 accepted_word= 0;
1911#endif
07be1b83
YO
1912 U32 pointpos = 0;
1913
1914 while ( state && uc <= (U8*)strend ) {
1915 int failed=0;
786e8c11
YO
1916 U32 word = aho->states[ state ].wordnum;
1917
1de06328
YO
1918 if( state==1 ) {
1919 if ( bitmap ) {
1920 DEBUG_TRIE_EXECUTE_r(
1921 if ( uc <= (U8*)last_start && !BITMAP_TEST(bitmap,*uc) ) {
1922 dump_exec_pos( (char *)uc, c, strend, real_start,
f2ed9b32 1923 (char *)uc, utf8_target );
1de06328
YO
1924 PerlIO_printf( Perl_debug_log,
1925 " Scanning for legal start char...\n");
1926 }
d085b490
YO
1927 );
1928 if (utf8_target) {
1929 while ( uc <= (U8*)last_start && !BITMAP_TEST(bitmap,*uc) ) {
1930 uc += UTF8SKIP(uc);
1931 }
1932 } else {
1933 while ( uc <= (U8*)last_start && !BITMAP_TEST(bitmap,*uc) ) {
1934 uc++;
1935 }
1936 }
1de06328 1937 s= (char *)uc;
786e8c11 1938 }
786e8c11
YO
1939 if (uc >(U8*)last_start) break;
1940 }
1941
1942 if ( word ) {
2e64971a 1943 U8 *lpos= points[ (pointpos - trie->wordinfo[word].len) % maxlen ];
786e8c11
YO
1944 if (!leftmost || lpos < leftmost) {
1945 DEBUG_r(accepted_word=word);
07be1b83 1946 leftmost= lpos;
786e8c11 1947 }
07be1b83 1948 if (base==0) break;
786e8c11 1949
07be1b83
YO
1950 }
1951 points[pointpos++ % maxlen]= uc;
7016d6eb
DM
1952 if (foldlen || uc < (U8*)strend) {
1953 REXEC_TRIE_READ_CHAR(trie_type, trie,
1954 widecharmap, uc,
55eed653
NC
1955 uscan, len, uvc, charid, foldlen,
1956 foldbuf, uniflags);
7016d6eb
DM
1957 DEBUG_TRIE_EXECUTE_r({
1958 dump_exec_pos( (char *)uc, c, strend,
1959 real_start, s, utf8_target);
1960 PerlIO_printf(Perl_debug_log,
1961 " Charid:%3u CP:%4"UVxf" ",
1962 charid, uvc);
1963 });
1964 }
1965 else {
1966 len = 0;
1967 charid = 0;
1968 }
1969
07be1b83
YO
1970
1971 do {
6148ee25 1972#ifdef DEBUGGING
786e8c11 1973 word = aho->states[ state ].wordnum;
6148ee25 1974#endif
07be1b83
YO
1975 base = aho->states[ state ].trans.base;
1976
786e8c11
YO
1977 DEBUG_TRIE_EXECUTE_r({
1978 if (failed)
1979 dump_exec_pos( (char *)uc, c, strend, real_start,
f2ed9b32 1980 s, utf8_target );
07be1b83 1981 PerlIO_printf( Perl_debug_log,
786e8c11
YO
1982 "%sState: %4"UVxf", word=%"UVxf,
1983 failed ? " Fail transition to " : "",
1984 (UV)state, (UV)word);
1985 });
07be1b83
YO
1986 if ( base ) {
1987 U32 tmp;
6dd2be57 1988 I32 offset;
07be1b83 1989 if (charid &&
6dd2be57
DM
1990 ( ((offset = base + charid
1991 - 1 - trie->uniquecharcount)) >= 0)
1992 && ((U32)offset < trie->lasttrans)
1993 && trie->trans[offset].check == state
1994 && (tmp=trie->trans[offset].next))
07be1b83 1995 {
786e8c11
YO
1996 DEBUG_TRIE_EXECUTE_r(
1997 PerlIO_printf( Perl_debug_log," - legal\n"));
07be1b83
YO
1998 state = tmp;
1999 break;
2000 }
2001 else {
786e8c11
YO
2002 DEBUG_TRIE_EXECUTE_r(
2003 PerlIO_printf( Perl_debug_log," - fail\n"));
2004 failed = 1;
2005 state = aho->fail[state];
07be1b83
YO
2006 }
2007 }
2008 else {
2009 /* we must be accepting here */
786e8c11
YO
2010 DEBUG_TRIE_EXECUTE_r(
2011 PerlIO_printf( Perl_debug_log," - accepting\n"));
2012 failed = 1;
07be1b83
YO
2013 break;
2014 }
2015 } while(state);
786e8c11 2016 uc += len;
07be1b83
YO
2017 if (failed) {
2018 if (leftmost)
2019 break;
786e8c11 2020 if (!state) state = 1;
07be1b83
YO
2021 }
2022 }
2023 if ( aho->states[ state ].wordnum ) {
2e64971a 2024 U8 *lpos = points[ (pointpos - trie->wordinfo[aho->states[ state ].wordnum].len) % maxlen ];
786e8c11
YO
2025 if (!leftmost || lpos < leftmost) {
2026 DEBUG_r(accepted_word=aho->states[ state ].wordnum);
07be1b83 2027 leftmost = lpos;
786e8c11 2028 }
07be1b83 2029 }
07be1b83
YO
2030 if (leftmost) {
2031 s = (char*)leftmost;
786e8c11
YO
2032 DEBUG_TRIE_EXECUTE_r({
2033 PerlIO_printf(
70685ca0
JH
2034 Perl_debug_log,"Matches word #%"UVxf" at position %"IVdf". Trying full pattern...\n",
2035 (UV)accepted_word, (IV)(s - real_start)
786e8c11
YO
2036 );
2037 });
24b23f37 2038 if (!reginfo || regtry(reginfo, &s)) {
be8e71aa
YO
2039 FREETMPS;
2040 LEAVE;
07be1b83 2041 goto got_it;
be8e71aa 2042 }
07be1b83 2043 s = HOPc(s,1);
786e8c11
YO
2044 DEBUG_TRIE_EXECUTE_r({
2045 PerlIO_printf( Perl_debug_log,"Pattern failed. Looking for new start point...\n");
2046 });
07be1b83 2047 } else {
786e8c11
YO
2048 DEBUG_TRIE_EXECUTE_r(
2049 PerlIO_printf( Perl_debug_log,"No match.\n"));
07be1b83
YO
2050 break;
2051 }
2052 }
be8e71aa
YO
2053 FREETMPS;
2054 LEAVE;
07be1b83
YO
2055 }
2056 break;
b3c9acc1 2057 default:
3c3eec57
GS
2058 Perl_croak(aTHX_ "panic: unknown regstclass %d", (int)OP(c));
2059 break;
d6a28714 2060 }
6eb5f6b9
JH
2061 return 0;
2062 got_it:
2063 return s;
2064}
2065
fae667d5 2066
6eb5f6b9
JH
2067/*
2068 - regexec_flags - match a regexp against a string
2069 */
2070I32
288b8c02 2071Perl_regexec_flags(pTHX_ REGEXP * const rx, char *stringarg, register char *strend,
6eb5f6b9 2072 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
8fd1a950
DM
2073/* stringarg: the point in the string at which to begin matching */
2074/* strend: pointer to null at end of string */
2075/* strbeg: real beginning of string */
2076/* minend: end of match must be >= minend bytes after stringarg. */
2077/* sv: SV being matched: only used for utf8 flag, pos() etc; string
2078 * itself is accessed via the pointers above */
2079/* data: May be used for some additional optimizations.
2080 Currently its only used, with a U32 cast, for transmitting
2081 the ganch offset when doing a /g match. This will change */
2082/* nosave: For optimizations. */
2083
6eb5f6b9 2084{
97aff369 2085 dVAR;
288b8c02 2086 struct regexp *const prog = (struct regexp *)SvANY(rx);
24b23f37 2087 /*register*/ char *s;
eb578fdb 2088 regnode *c;
24b23f37 2089 /*register*/ char *startpos = stringarg;
6eb5f6b9
JH
2090 I32 minlen; /* must match at least this many chars */
2091 I32 dontbother = 0; /* how many characters not to try at end */
6eb5f6b9
JH
2092 I32 end_shift = 0; /* Same for the end. */ /* CC */
2093 I32 scream_pos = -1; /* Internal iterator of scream. */
ccac19ea 2094 char *scream_olds = NULL;
f2ed9b32 2095 const bool utf8_target = cBOOL(DO_UTF8(sv));
2757e526 2096 I32 multiline;
f8fc2ecf 2097 RXi_GET_DECL(prog,progi);
3b0527fe 2098 regmatch_info reginfo; /* create some info to pass to regtry etc */
e9105d30 2099 regexp_paren_pair *swap = NULL;
a3621e74
YO
2100 GET_RE_DEBUG_FLAGS_DECL;
2101
7918f24d 2102 PERL_ARGS_ASSERT_REGEXEC_FLAGS;
9d4ba2ae 2103 PERL_UNUSED_ARG(data);
6eb5f6b9
JH
2104
2105 /* Be paranoid... */
2106 if (prog == NULL || startpos == NULL) {
2107 Perl_croak(aTHX_ "NULL regexp parameter");
2108 return 0;
2109 }
2110
bbe252da 2111 multiline = prog->extflags & RXf_PMf_MULTILINE;
288b8c02 2112 reginfo.prog = rx; /* Yes, sorry that this is confusing. */
2757e526 2113
f2ed9b32 2114 RX_MATCH_UTF8_set(rx, utf8_target);
1de06328 2115 DEBUG_EXECUTE_r(
f2ed9b32 2116 debug_start_match(rx, utf8_target, startpos, strend,
1de06328
YO
2117 "Matching");
2118 );
bac06658 2119
6eb5f6b9 2120 minlen = prog->minlen;
1de06328
YO
2121
2122 if (strend - startpos < (minlen+(prog->check_offset_min<0?prog->check_offset_min:0))) {
a3621e74 2123 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
a72c7584
JH
2124 "String too short [regexec_flags]...\n"));
2125 goto phooey;
1aa99e6b 2126 }
6eb5f6b9 2127
1de06328 2128
6eb5f6b9 2129 /* Check validity of program. */
f8fc2ecf 2130 if (UCHARAT(progi->program) != REG_MAGIC) {
6eb5f6b9
JH
2131 Perl_croak(aTHX_ "corrupted regexp program");
2132 }
2133
2134 PL_reg_flags = 0;
ed301438 2135 PL_reg_state.re_state_eval_setup_done = FALSE;
6eb5f6b9
JH
2136 PL_reg_maxiter = 0;
2137
3c8556c3 2138 if (RX_UTF8(rx))
6eb5f6b9
JH
2139 PL_reg_flags |= RF_utf8;
2140
2141 /* Mark beginning of line for ^ and lookbehind. */
3b0527fe 2142 reginfo.bol = startpos; /* XXX not used ??? */
6eb5f6b9 2143 PL_bostr = strbeg;
3b0527fe 2144 reginfo.sv = sv;
6eb5f6b9
JH
2145
2146 /* Mark end of line for $ (and such) */
2147 PL_regeol = strend;
2148
2149 /* see how far we have to get to not match where we matched before */
3b0527fe 2150 reginfo.till = startpos+minend;
6eb5f6b9 2151
6eb5f6b9
JH
2152 /* If there is a "must appear" string, look for it. */
2153 s = startpos;
2154
bbe252da 2155 if (prog->extflags & RXf_GPOS_SEEN) { /* Need to set reginfo->ganch */
6eb5f6b9 2156 MAGIC *mg;
2c296965 2157 if (flags & REXEC_IGNOREPOS){ /* Means: check only at start */
58e23c8d 2158 reginfo.ganch = startpos + prog->gofs;
2c296965 2159 DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log,
ed549f2e 2160 "GPOS IGNOREPOS: reginfo.ganch = startpos + %"UVxf"\n",(UV)prog->gofs));
2c296965 2161 } else if (sv && SvTYPE(sv) >= SVt_PVMG
6eb5f6b9 2162 && SvMAGIC(sv)
14befaf4
DM
2163 && (mg = mg_find(sv, PERL_MAGIC_regex_global))
2164 && mg->mg_len >= 0) {
3b0527fe 2165 reginfo.ganch = strbeg + mg->mg_len; /* Defined pos() */
2c296965 2166 DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log,
ed549f2e 2167 "GPOS MAGIC: reginfo.ganch = strbeg + %"IVdf"\n",(IV)mg->mg_len));
2c296965 2168
bbe252da 2169 if (prog->extflags & RXf_ANCH_GPOS) {
3b0527fe 2170 if (s > reginfo.ganch)
6eb5f6b9 2171 goto phooey;
58e23c8d 2172 s = reginfo.ganch - prog->gofs;
2c296965 2173 DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log,
ed549f2e 2174 "GPOS ANCH_GPOS: s = ganch - %"UVxf"\n",(UV)prog->gofs));
c584a96e
YO
2175 if (s < strbeg)
2176 goto phooey;
6eb5f6b9
JH
2177 }
2178 }
58e23c8d 2179 else if (data) {
70685ca0 2180 reginfo.ganch = strbeg + PTR2UV(data);
2c296965
YO
2181 DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log,
2182 "GPOS DATA: reginfo.ganch= strbeg + %"UVxf"\n",PTR2UV(data)));
2183
2184 } else { /* pos() not defined */
3b0527fe 2185 reginfo.ganch = strbeg;
2c296965
YO
2186 DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log,
2187 "GPOS: reginfo.ganch = strbeg\n"));
2188 }
6eb5f6b9 2189 }
288b8c02 2190 if (PL_curpm && (PM_GETRE(PL_curpm) == rx)) {
e9105d30
GG
2191 /* We have to be careful. If the previous successful match
2192 was from this regex we don't want a subsequent partially
2193 successful match to clobber the old results.
2194 So when we detect this possibility we add a swap buffer
2195 to the re, and switch the buffer each match. If we fail
2196 we switch it back, otherwise we leave it swapped.
2197 */
2198 swap = prog->offs;
2199 /* do we need a save destructor here for eval dies? */
2200 Newxz(prog->offs, (prog->nparens + 1), regexp_paren_pair);
495f47a5
DM
2201 DEBUG_BUFFERS_r(PerlIO_printf(Perl_debug_log,
2202 "rex=0x%"UVxf" saving offs: orig=0x%"UVxf" new=0x%"UVxf"\n",
2203 PTR2UV(prog),
2204 PTR2UV(swap),
2205 PTR2UV(prog->offs)
2206 ));
c74340f9 2207 }
a0714e2c 2208 if (!(flags & REXEC_CHECKED) && (prog->check_substr != NULL || prog->check_utf8 != NULL)) {
6eb5f6b9
JH
2209 re_scream_pos_data d;
2210
2211 d.scream_olds = &scream_olds;
2212 d.scream_pos = &scream_pos;
288b8c02 2213 s = re_intuit_start(rx, sv, s, strend, flags, &d);
3fa9c3d7 2214 if (!s) {
a3621e74 2215 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Not present...\n"));
6eb5f6b9 2216 goto phooey; /* not present */
3fa9c3d7 2217 }
6eb5f6b9
JH
2218 }
2219
1de06328 2220
6eb5f6b9
JH
2221
2222 /* Simplest case: anchored match need be tried only once. */
2223 /* [unless only anchor is BOL and multiline is set] */
bbe252da 2224 if (prog->extflags & (RXf_ANCH & ~RXf_ANCH_GPOS)) {
24b23f37 2225 if (s == startpos && regtry(&reginfo, &startpos))
6eb5f6b9 2226 goto got_it;
bbe252da
YO
2227 else if (multiline || (prog->intflags & PREGf_IMPLICIT)
2228 || (prog->extflags & RXf_ANCH_MBOL)) /* XXXX SBOL? */
6eb5f6b9
JH
2229 {
2230 char *end;
2231
2232 if (minlen)
2233 dontbother = minlen - 1;
1aa99e6b 2234 end = HOP3c(strend, -dontbother, strbeg) - 1;
6eb5f6b9 2235 /* for multiline we only have to try after newlines */
33b8afdf 2236 if (prog->check_substr || prog->check_utf8) {
92f3d482
YO
2237 /* because of the goto we can not easily reuse the macros for bifurcating the
2238 unicode/non-unicode match modes here like we do elsewhere - demerphq */
2239 if (utf8_target) {
2240 if (s == startpos)
2241 goto after_try_utf8;
2242 while (1) {
2243 if (regtry(&reginfo, &s)) {
2244 goto got_it;
2245 }
2246 after_try_utf8:
2247 if (s > end) {
2248 goto phooey;
2249 }
2250 if (prog->extflags & RXf_USE_INTUIT) {
2251 s = re_intuit_start(rx, sv, s + UTF8SKIP(s), strend, flags, NULL);
2252 if (!s) {
2253 goto phooey;
2254 }
2255 }
2256 else {
2257 s += UTF8SKIP(s);
2258 }
2259 }
2260 } /* end search for check string in unicode */
2261 else {
2262 if (s == startpos) {
2263 goto after_try_latin;
2264 }
2265 while (1) {
2266 if (regtry(&reginfo, &s)) {
2267 goto got_it;
2268 }
2269 after_try_latin:
2270 if (s > end) {
2271 goto phooey;
2272 }
2273 if (prog->extflags & RXf_USE_INTUIT) {
2274 s = re_intuit_start(rx, sv, s + 1, strend, flags, NULL);
2275 if (!s) {
2276 goto phooey;
2277 }
2278 }
2279 else {
2280 s++;
2281 }
2282 }
2283 } /* end search for check string in latin*/
2284 } /* end search for check string */
2285 else { /* search for newline */
2286 if (s > startpos) {
2287 /*XXX: The s-- is almost definitely wrong here under unicode - demeprhq*/
6eb5f6b9 2288 s--;
92f3d482 2289 }
21eede78
YO
2290 /* We can use a more efficient search as newlines are the same in unicode as they are in latin */
2291 while (s <= end) { /* note it could be possible to match at the end of the string */
6eb5f6b9 2292 if (*s++ == '\n') { /* don't need PL_utf8skip here */
24b23f37 2293 if (regtry(&reginfo, &s))
6eb5f6b9
JH
2294 goto got_it;
2295 }
92f3d482
YO
2296 }
2297 } /* end search for newline */
2298 } /* end anchored/multiline check string search */
6eb5f6b9 2299 goto phooey;
bbe252da 2300 } else if (RXf_GPOS_CHECK == (prog->extflags & RXf_GPOS_CHECK))
f9f4320a 2301 {
486ec47a 2302 /* the warning about reginfo.ganch being used without initialization
bbe252da 2303 is bogus -- we set it above, when prog->extflags & RXf_GPOS_SEEN
f9f4320a 2304 and we only enter this block when the same bit is set. */
58e23c8d 2305 char *tmp_s = reginfo.ganch - prog->gofs;
c584a96e
YO
2306
2307 if (tmp_s >= strbeg && regtry(&reginfo, &tmp_s))
6eb5f6b9
JH
2308 goto got_it;
2309 goto phooey;
2310 }
2311
2312 /* Messy cases: unanchored match. */
bbe252da 2313 if ((prog->anchored_substr || prog->anchored_utf8) && prog->intflags & PREGf_SKIP) {
6eb5f6b9 2314 /* we have /x+whatever/ */
f2ed9b32 2315 /* it must be a one character string (XXXX Except UTF_PATTERN?) */
33b8afdf 2316 char ch;
bf93d4cc
GS
2317#ifdef DEBUGGING
2318 int did_match = 0;
2319#endif
f2ed9b32
KW
2320 if (!(utf8_target ? prog->anchored_utf8 : prog->anchored_substr))
2321 utf8_target ? to_utf8_substr(prog) : to_byte_substr(prog);
2322 ch = SvPVX_const(utf8_target ? prog->anchored_utf8 : prog->anchored_substr)[0];
bf93d4cc 2323
f2ed9b32 2324 if (utf8_target) {
4cadc6a9 2325 REXEC_FBC_SCAN(
6eb5f6b9 2326 if (*s == ch) {
a3621e74 2327 DEBUG_EXECUTE_r( did_match = 1 );
24b23f37 2328 if (regtry(&reginfo, &s)) goto got_it;
6eb5f6b9
JH
2329 s += UTF8SKIP(s);
2330 while (s < strend && *s == ch)
2331 s += UTF8SKIP(s);
2332 }
4cadc6a9 2333 );
6eb5f6b9
JH
2334 }
2335 else {
4cadc6a9 2336 REXEC_FBC_SCAN(
6eb5f6b9 2337 if (*s == ch) {
a3621e74 2338 DEBUG_EXECUTE_r( did_match = 1 );
24b23f37 2339 if (regtry(&reginfo, &s)) goto got_it;
6eb5f6b9
JH
2340 s++;
2341 while (s < strend && *s == ch)
2342 s++;
2343 }
4cadc6a9 2344 );
6eb5f6b9 2345 }
a3621e74 2346 DEBUG_EXECUTE_r(if (!did_match)
bf93d4cc 2347 PerlIO_printf(Perl_debug_log,
b7953727
JH
2348 "Did not find anchored character...\n")
2349 );
6eb5f6b9 2350 }
a0714e2c
SS
2351 else if (prog->anchored_substr != NULL
2352 || prog->anchored_utf8 != NULL
2353 || ((prog->float_substr != NULL || prog->float_utf8 != NULL)
33b8afdf
JH
2354 && prog->float_max_offset < strend - s)) {
2355 SV *must;
2356 I32 back_max;
2357 I32 back_min;
2358 char *last;
6eb5f6b9 2359 char *last1; /* Last position checked before */
bf93d4cc
GS
2360#ifdef DEBUGGING
2361 int did_match = 0;
2362#endif
33b8afdf 2363 if (prog->anchored_substr || prog->anchored_utf8) {
f2ed9b32
KW
2364 if (!(utf8_target ? prog->anchored_utf8 : prog->anchored_substr))
2365 utf8_target ? to_utf8_substr(prog) : to_byte_substr(prog);
2366 must = utf8_target ? prog->anchored_utf8 : prog->anchored_substr;
33b8afdf
JH
2367 back_max = back_min = prog->anchored_offset;
2368 } else {
f2ed9b32
KW
2369 if (!(utf8_target ? prog->float_utf8 : prog->float_substr))
2370 utf8_target ? to_utf8_substr(prog) : to_byte_substr(prog);
2371 must = utf8_target ? prog->float_utf8 : prog->float_substr;
33b8afdf
JH
2372 back_max = prog->float_max_offset;
2373 back_min = prog->float_min_offset;
2374 }
1de06328
YO
2375
2376
33b8afdf
JH
2377 if (must == &PL_sv_undef)
2378 /* could not downgrade utf8 check substring, so must fail */
2379 goto phooey;
2380
1de06328
YO
2381 if (back_min<0) {
2382 last = strend;
2383 } else {
2384 last = HOP3c(strend, /* Cannot start after this */
2385 -(I32)(CHR_SVLEN(must)
2386 - (SvTAIL(must) != 0) + back_min), strbeg);
2387 }
6eb5f6b9
JH
2388 if (s > PL_bostr)
2389 last1 = HOPc(s, -1);
2390 else
2391 last1 = s - 1; /* bogus */
2392
a0288114 2393 /* XXXX check_substr already used to find "s", can optimize if
6eb5f6b9
JH
2394 check_substr==must. */
2395 scream_pos = -1;
2396 dontbother = end_shift;
2397 strend = HOPc(strend, -dontbother);
2398 while ( (s <= last) &&
c33e64f0 2399 (s = fbm_instr((unsigned char*)HOP3(s, back_min, (back_min<0 ? strbeg : strend)),
9041c2e3 2400 (unsigned char*)strend, must,
c33e64f0 2401 multiline ? FBMrf_MULTILINE : 0)) ) {
a3621e74 2402 DEBUG_EXECUTE_r( did_match = 1 );
6eb5f6b9
JH
2403 if (HOPc(s, -back_max) > last1) {
2404 last1 = HOPc(s, -back_min);
2405 s = HOPc(s, -back_max);
2406 }
2407 else {
52657f30 2408 char * const t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
6eb5f6b9
JH
2409
2410 last1 = HOPc(s, -back_min);
52657f30 2411 s = t;
6eb5f6b9 2412 }
f2ed9b32 2413 if (utf8_target) {
6eb5f6b9 2414 while (s <= last1) {
24b23f37 2415 if (regtry(&reginfo, &s))
6eb5f6b9 2416 goto got_it;
7016d6eb
DM
2417 if (s >= last1) {
2418 s++; /* to break out of outer loop */
2419 break;
2420 }
2421 s += UTF8SKIP(s);
6eb5f6b9
JH
2422 }
2423 }
2424 else {
2425 while (s <= last1) {
24b23f37 2426 if (regtry(&reginfo, &s))
6eb5f6b9
JH
2427 goto got_it;
2428 s++;
2429 }
2430 }
2431 }
ab3bbdeb 2432 DEBUG_EXECUTE_r(if (!did_match) {
f2ed9b32 2433 RE_PV_QUOTED_DECL(quoted, utf8_target, PERL_DEBUG_PAD_ZERO(0),
ab3bbdeb
YO
2434 SvPVX_const(must), RE_SV_DUMPLEN(must), 30);
2435 PerlIO_printf(Perl_debug_log, "Did not find %s substr %s%s...\n",
33b8afdf 2436 ((must == prog->anchored_substr || must == prog->anchored_utf8)
bf93d4cc 2437 ? "anchored" : "floating"),
ab3bbdeb
YO
2438 quoted, RE_SV_TAIL(must));
2439 });
6eb5f6b9
JH
2440 goto phooey;
2441 }
f8fc2ecf 2442 else if ( (c = progi->regstclass) ) {
f14c76ed 2443 if (minlen) {
f8fc2ecf 2444 const OPCODE op = OP(progi->regstclass);
66e933ab 2445 /* don't bother with what can't match */
786e8c11 2446 if (PL_regkind[op] != EXACT && op != CANY && PL_regkind[op] != TRIE)
f14c76ed
RGS
2447 strend = HOPc(strend, -(minlen - 1));
2448 }
a3621e74 2449 DEBUG_EXECUTE_r({
be8e71aa 2450 SV * const prop = sv_newmortal();
32fc9b6a 2451 regprop(prog, prop, c);
0df25f3d 2452 {
f2ed9b32 2453 RE_PV_QUOTED_DECL(quoted,utf8_target,PERL_DEBUG_PAD_ZERO(1),
ab3bbdeb 2454 s,strend-s,60);
0df25f3d 2455 PerlIO_printf(Perl_debug_log,
1c8f8eb1 2456 "Matching stclass %.*s against %s (%d bytes)\n",
e4f74956 2457 (int)SvCUR(prop), SvPVX_const(prop),
ab3bbdeb 2458 quoted, (int)(strend - s));
0df25f3d 2459 }
ffc61ed2 2460 });
3b0527fe 2461 if (find_byclass(prog, c, s, strend, &reginfo))
6eb5f6b9 2462 goto got_it;
07be1b83 2463 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Contradicts stclass... [regexec_flags]\n"));
d6a28714
JH
2464 }
2465 else {
2466 dontbother = 0;
a0714e2c 2467 if (prog->float_substr != NULL || prog->float_utf8 != NULL) {
33b8afdf 2468 /* Trim the end. */
6af40bd7 2469 char *last= NULL;
33b8afdf 2470 SV* float_real;
c33e64f0
FC
2471 STRLEN len;
2472 const char *little;
33b8afdf 2473
c72077c4
AC
2474 if (utf8_target && !prog->float_utf8)
2475 to_utf8_substr(prog);
2476 else if (!utf8_target && !prog->float_substr) {
2477 to_byte_substr(prog);
2478 if (prog->float_substr == &PL_sv_undef)
2479 /* downgrading failed, but target is not utf8, so
2480 * matching must fail */
2481 goto phooey;
2482 }
f2ed9b32 2483 float_real = utf8_target ? prog->float_utf8 : prog->float_substr;
d6a28714 2484
c33e64f0
FC
2485 little = SvPV_const(float_real, len);
2486 if (SvTAIL(float_real)) {
1a13b075
YO
2487 /* This means that float_real contains an artificial \n on the end
2488 * due to the presence of something like this: /foo$/
2489 * where we can match both "foo" and "foo\n" at the end of the string.
2490 * So we have to compare the end of the string first against the float_real
2491 * without the \n and then against the full float_real with the string.
2492 * We have to watch out for cases where the string might be smaller
2493 * than the float_real or the float_real without the \n.
2494 */
2495 char *checkpos= strend - len;
2496 DEBUG_OPTIMISE_r(
2497 PerlIO_printf(Perl_debug_log,
2498 "%sChecking for float_real.%s\n",
2499 PL_colors[4], PL_colors[5]));
2500 if (checkpos + 1 < strbeg) {
2501 /* can't match, even if we remove the trailing \n string is too short to match */
2502 DEBUG_EXECUTE_r(
2503 PerlIO_printf(Perl_debug_log,
2504 "%sString shorter than required trailing substring, cannot match.%s\n",
2505 PL_colors[4], PL_colors[5]));
2506 goto phooey;
2507 } else if (memEQ(checkpos + 1, little, len - 1)) {
2508 /* can match, the end of the string matches without the "\n" */
2509 last = checkpos + 1;
2510 } else if (checkpos < strbeg) {
2511 /* cant match, string is too short when the "\n" is included */
2512 DEBUG_EXECUTE_r(
2513 PerlIO_printf(Perl_debug_log,
2514 "%sString does not contain required trailing substring, cannot match.%s\n",
2515 PL_colors[4], PL_colors[5]));
2516 goto phooey;
2517 } else if (!multiline) {
2518 /* non multiline match, so compare with the "\n" at the end of the string */
2519 if (memEQ(checkpos, little, len)) {
2520 last= checkpos;
2521 } else {
2522 DEBUG_EXECUTE_r(
2523 PerlIO_printf(Perl_debug_log,
2524 "%sString does not contain required trailing substring, cannot match.%s\n",
2525 PL_colors[4], PL_colors[5]));
2526 goto phooey;
2527 }
2528 } else {
2529 /* multiline match, so we have to search for a place where the full string is located */
d6a28714 2530 goto find_last;
1a13b075 2531 }
c33e64f0 2532 } else {
d6a28714 2533 find_last:
9041c2e3 2534 if (len)
d6a28714 2535 last = rninstr(s, strend, little, little + len);
b8c5462f 2536 else
a0288114 2537 last = strend; /* matching "$" */
b8c5462f 2538 }
6af40bd7
YO
2539 if (!last) {
2540 /* at one point this block contained a comment which was probably
2541 * incorrect, which said that this was a "should not happen" case.
2542 * Even if it was true when it was written I am pretty sure it is
2543 * not anymore, so I have removed the comment and replaced it with
2544 * this one. Yves */
6bda09f9
YO
2545 DEBUG_EXECUTE_r(
2546 PerlIO_printf(Perl_debug_log,
6af40bd7
YO
2547 "String does not contain required substring, cannot match.\n"
2548 ));
2549 goto phooey;
bf93d4cc 2550 }
d6a28714
JH
2551 dontbother = strend - last + prog->float_min_offset;
2552 }
2553 if (minlen && (dontbother < minlen))
2554 dontbother = minlen - 1;
2555 strend -= dontbother; /* this one's always in bytes! */
2556 /* We don't know much -- general case. */
f2ed9b32 2557 if (utf8_target) {
d6a28714 2558 for (;;) {
24b23f37 2559 if (regtry(&reginfo, &s))
d6a28714
JH
2560 goto got_it;
2561 if (s >= strend)
2562 break;
b8c5462f 2563 s += UTF8SKIP(s);
d6a28714
JH
2564 };
2565 }
2566 else {
2567 do {
24b23f37 2568 if (regtry(&reginfo, &s))
d6a28714
JH
2569 goto got_it;
2570 } while (s++ < strend);
2571 }
2572 }
2573
2574 /* Failure. */
2575 goto phooey;
2576
2577got_it:
495f47a5
DM
2578 DEBUG_BUFFERS_r(
2579 if (swap)
2580 PerlIO_printf(Perl_debug_log,
2581 "rex=0x%"UVxf" freeing offs: 0x%"UVxf"\n",
2582 PTR2UV(prog),
2583 PTR2UV(swap)
2584 );
2585 );
e9105d30 2586 Safefree(swap);
288b8c02 2587 RX_MATCH_TAINTED_set(rx, PL_reg_flags & RF_tainted);
d6a28714 2588
ed301438 2589 if (PL_reg_state.re_state_eval_setup_done)
4f639d21 2590 restore_pos(aTHX_ prog);
5daac39c
NC
2591 if (RXp_PAREN_NAMES(prog))
2592 (void)hv_iterinit(RXp_PAREN_NAMES(prog));
d6a28714
JH
2593
2594 /* make sure $`, $&, $', and $digit will work later */
2595 if ( !(flags & REXEC_NOT_FIRST) ) {
d6a28714 2596 if (flags & REXEC_COPY_STR) {
f8c7b90f 2597#ifdef PERL_OLD_COPY_ON_WRITE
ed252734
NC
2598 if ((SvIsCOW(sv)
2599 || (SvFLAGS(sv) & CAN_COW_MASK) == CAN_COW_FLAGS)) {
2600 if (DEBUG_C_TEST) {
2601 PerlIO_printf(Perl_debug_log,
2602 "Copy on write: regexp capture, type %d\n",
2603 (int) SvTYPE(sv));
2604 }
77f8f7c1 2605 RX_MATCH_COPY_FREE(rx);
ed252734 2606 prog->saved_copy = sv_setsv_cow(prog->saved_copy, sv);
d5263905 2607 prog->subbeg = (char *)SvPVX_const(prog->saved_copy);
ed252734 2608 assert (SvPOKp(prog->saved_copy));
6502e081
DM
2609 prog->sublen = PL_regeol - strbeg;
2610 prog->suboffset = 0;
2611 prog->subcoffset = 0;
ed252734
NC
2612 } else
2613#endif
2614 {
6502e081
DM
2615 I32 min = 0;
2616 I32 max = PL_regeol - strbeg;
2617 I32 sublen;
2618
2619 if ( (flags & REXEC_COPY_SKIP_POST)
2620 && !(RX_EXTFLAGS(rx) & RXf_PMf_KEEPCOPY) /* //p */
2621 && !(PL_sawampersand & SAWAMPERSAND_RIGHT)
2622 ) { /* don't copy $' part of string */
3de645a8 2623 U32 n = 0;
6502e081
DM
2624 max = -1;
2625 /* calculate the right-most part of the string covered
2626 * by a capture. Due to look-ahead, this may be to
2627 * the right of $&, so we have to scan all captures */
2628 while (n <= prog->lastparen) {
2629 if (prog->offs[n].end > max)
2630 max = prog->offs[n].end;
2631 n++;
2632 }
2633 if (max == -1)
2634 max = (PL_sawampersand & SAWAMPERSAND_LEFT)
2635 ? prog->offs[0].start
2636 : 0;
2637 assert(max >= 0 && max <= PL_regeol - strbeg);
2638 }
2639
2640 if ( (flags & REXEC_COPY_SKIP_PRE)
2641 && !(RX_EXTFLAGS(rx) & RXf_PMf_KEEPCOPY) /* //p */
2642 && !(PL_sawampersand & SAWAMPERSAND_LEFT)
2643 ) { /* don't copy $` part of string */
3de645a8 2644 U32 n = 0;
6502e081
DM
2645 min = max;
2646 /* calculate the left-most part of the string covered
2647 * by a capture. Due to look-behind, this may be to
2648 * the left of $&, so we have to scan all captures */
2649 while (min && n <= prog->lastparen) {
2650 if ( prog->offs[n].start != -1
2651 && prog->offs[n].start < min)
2652 {
2653 min = prog->offs[n].start;
2654 }
2655 n++;
2656 }
2657 if ((PL_sawampersand & SAWAMPERSAND_RIGHT)
2658 && min > prog->offs[0].end
2659 )
2660 min = prog->offs[0].end;
2661
2662 }
2663
2664 assert(min >= 0 && min <= max && min <= PL_regeol - strbeg);
2665 sublen = max - min;
2666
2667 if (RX_MATCH_COPIED(rx)) {
2668 if (sublen > prog->sublen)
2669 prog->subbeg =
2670 (char*)saferealloc(prog->subbeg, sublen+1);
2671 }
2672 else
2673 prog->subbeg = (char*)safemalloc(sublen+1);
2674 Copy(strbeg + min, prog->subbeg, sublen, char);
2675 prog->subbeg[sublen] = '\0';
2676 prog->suboffset = min;
2677 prog->sublen = sublen;
77f8f7c1 2678 RX_MATCH_COPIED_on(rx);
6502e081 2679 }
6502e081
DM
2680 prog->subcoffset = prog->suboffset;
2681 if (prog->suboffset && utf8_target) {
2682 /* Convert byte offset to chars.
2683 * XXX ideally should only compute this if @-/@+
2684 * has been seen, a la PL_sawampersand ??? */
2685
2686 /* If there's a direct correspondence between the
2687 * string which we're matching and the original SV,
2688 * then we can use the utf8 len cache associated with
2689 * the SV. In particular, it means that under //g,
2690 * sv_pos_b2u() will use the previously cached
2691 * position to speed up working out the new length of
2692 * subcoffset, rather than counting from the start of
2693 * the string each time. This stops
2694 * $x = "\x{100}" x 1E6; 1 while $x =~ /(.)/g;
2695 * from going quadratic */
2696 if (SvPOKp(sv) && SvPVX(sv) == strbeg)
2697 sv_pos_b2u(sv, &(prog->subcoffset));
2698 else
2699 prog->subcoffset = utf8_length((U8*)strbeg,
2700 (U8*)(strbeg+prog->suboffset));
2701 }
d6a28714
JH
2702 }
2703 else {
6502e081 2704 RX_MATCH_COPY_FREE(rx);
d6a28714 2705 prog->subbeg = strbeg;
6502e081
DM
2706 prog->suboffset = 0;
2707 prog->subcoffset = 0;
d6a28714
JH
2708 prog->sublen = PL_regeol - strbeg; /* strend may have been modified */
2709 }
2710 }
9041c2e3 2711
d6a28714
JH
2712 return 1;
2713
2714phooey:
a3621e74 2715 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch failed%s\n",
e4584336 2716 PL_colors[4], PL_colors[5]));
ed301438 2717 if (PL_reg_state.re_state_eval_setup_done)
4f639d21 2718 restore_pos(aTHX_ prog);
e9105d30 2719 if (swap) {
c74340f9 2720 /* we failed :-( roll it back */
495f47a5
DM
2721 DEBUG_BUFFERS_r(PerlIO_printf(Perl_debug_log,
2722 "rex=0x%"UVxf" rolling back offs: freeing=0x%"UVxf" restoring=0x%"UVxf"\n",
2723 PTR2UV(prog),
2724 PTR2UV(prog->offs),
2725 PTR2UV(swap)
2726 ));
e9105d30
GG
2727 Safefree(prog->offs);
2728 prog->offs = swap;
2729 }
d6a28714
JH
2730 return 0;
2731}
2732
6bda09f9 2733
ec43f78b
DM
2734/* Set which rex is pointed to by PL_reg_state, handling ref counting.
2735 * Do inc before dec, in case old and new rex are the same */
2736#define SET_reg_curpm(Re2) \
2737 if (PL_reg_state.re_state_eval_setup_done) { \
2738 (void)ReREFCNT_inc(Re2); \
2739 ReREFCNT_dec(PM_GETRE(PL_reg_curpm)); \
2740 PM_SETRE((PL_reg_curpm), (Re2)); \
2741 }
2742
2743
d6a28714
JH
2744/*
2745 - regtry - try match at specific point
2746 */
2747STATIC I32 /* 0 failure, 1 success */
f73aaa43 2748S_regtry(pTHX_ regmatch_info *reginfo, char **startposp)
d6a28714 2749{
97aff369 2750 dVAR;
d6a28714 2751 CHECKPOINT lastcp;
288b8c02
NC
2752 REGEXP *const rx = reginfo->prog;
2753 regexp *const prog = (struct regexp *)SvANY(rx);
f73aaa43 2754 I32 result;
f8fc2ecf 2755 RXi_GET_DECL(prog,progi);
a3621e74 2756 GET_RE_DEBUG_FLAGS_DECL;
7918f24d
NC
2757
2758 PERL_ARGS_ASSERT_REGTRY;
2759
24b23f37 2760 reginfo->cutpoint=NULL;
d6a28714 2761
ed301438
DM
2762 if ((prog->extflags & RXf_EVAL_SEEN)
2763 && !PL_reg_state.re_state_eval_setup_done)
2764 {
d6a28714
JH
2765 MAGIC *mg;
2766
ed301438 2767 PL_reg_state.re_state_eval_setup_done = TRUE;
3b0527fe 2768 if (reginfo->sv) {
d6a28714 2769 /* Make $_ available to executed code. */
3b0527fe 2770 if (reginfo->sv != DEFSV) {
59f00321 2771 SAVE_DEFSV;
414bf5ae 2772 DEFSV_set(reginfo->sv);
b8c5462f 2773 }
d6a28714 2774
3b0527fe
DM
2775 if (!(SvTYPE(reginfo->sv) >= SVt_PVMG && SvMAGIC(reginfo->sv)
2776 && (mg = mg_find(reginfo->sv, PERL_MAGIC_regex_global)))) {
d6a28714 2777 /* prepare for quick setting of pos */
d300d9fa 2778#ifdef PERL_OLD_COPY_ON_WRITE
51a9ea20
NC
2779 if (SvIsCOW(reginfo->sv))
2780 sv_force_normal_flags(reginfo->sv, 0);
d300d9fa 2781#endif
3dab1dad 2782 mg = sv_magicext(reginfo->sv, NULL, PERL_MAGIC_regex_global,
d300d9fa 2783 &PL_vtbl_mglob, NULL, 0);
d6a28714 2784 mg->mg_len = -1;
b8c5462f 2785 }
d6a28714
JH
2786 PL_reg_magic = mg;
2787 PL_reg_oldpos = mg->mg_len;
4f639d21 2788 SAVEDESTRUCTOR_X(restore_pos, prog);
d6a28714 2789 }
09687e5a 2790 if (!PL_reg_curpm) {
a02a5408 2791 Newxz(PL_reg_curpm, 1, PMOP);
09687e5a
AB
2792#ifdef USE_ITHREADS
2793 {
14a49a24 2794 SV* const repointer = &PL_sv_undef;
92313705
NC
2795 /* this regexp is also owned by the new PL_reg_curpm, which
2796 will try to free it. */
d2ece331 2797 av_push(PL_regex_padav, repointer);
09687e5a
AB
2798 PL_reg_curpm->op_pmoffset = av_len(PL_regex_padav);
2799 PL_regex_pad = AvARRAY(PL_regex_padav);
2800 }
2801#endif
2802 }
ec43f78b 2803 SET_reg_curpm(rx);
d6a28714
JH
2804 PL_reg_oldcurpm = PL_curpm;
2805 PL_curpm = PL_reg_curpm;
07bc277f 2806 if (RXp_MATCH_COPIED(prog)) {
d6a28714
JH
2807 /* Here is a serious problem: we cannot rewrite subbeg,
2808 since it may be needed if this match fails. Thus
2809 $` inside (?{}) could fail... */
2810 PL_reg_oldsaved = prog->subbeg;
2811 PL_reg_oldsavedlen = prog->sublen;
6502e081
DM
2812 PL_reg_oldsavedoffset = prog->suboffset;
2813 PL_reg_oldsavedcoffset = prog->suboffset;
f8c7b90f 2814#ifdef PERL_OLD_COPY_ON_WRITE
ed252734
NC
2815 PL_nrs = prog->saved_copy;
2816#endif
07bc277f 2817 RXp_MATCH_COPIED_off(prog);
d6a28714
JH
2818 }
2819 else
bd61b366 2820 PL_reg_oldsaved = NULL;
d6a28714 2821 prog->subbeg = PL_bostr;
6502e081
DM
2822 prog->suboffset = 0;
2823 prog->subcoffset = 0;
d6a28714
JH
2824 prog->sublen = PL_regeol - PL_bostr; /* strend may have been modified */
2825 }
97ca13b7 2826#ifdef DEBUGGING
f73aaa43 2827 PL_reg_starttry = *startposp;
97ca13b7 2828#endif
f73aaa43 2829 prog->offs[0].start = *startposp - PL_bostr;
d6a28714 2830 prog->lastparen = 0;
03994de8 2831 prog->lastcloseparen = 0;
d6a28714 2832 PL_regsize = 0;
d6a28714
JH
2833
2834 /* XXXX What this code is doing here?!!! There should be no need
b93070ed 2835 to do this again and again, prog->lastparen should take care of
3dd2943c 2836 this! --ilya*/
dafc8851
JH
2837
2838 /* Tests pat.t#187 and split.t#{13,14} seem to depend on this code.
2839 * Actually, the code in regcppop() (which Ilya may be meaning by
b93070ed 2840 * prog->lastparen), is not needed at all by the test suite
225593e1
DM
2841 * (op/regexp, op/pat, op/split), but that code is needed otherwise
2842 * this erroneously leaves $1 defined: "1" =~ /^(?:(\d)x)?\d$/
2843 * Meanwhile, this code *is* needed for the
daf18116
JH
2844 * above-mentioned test suite tests to succeed. The common theme
2845 * on those tests seems to be returning null fields from matches.
225593e1 2846 * --jhi updated by dapm */
dafc8851 2847#if 1
d6a28714 2848 if (prog->nparens) {
b93070ed 2849 regexp_paren_pair *pp = prog->offs;
eb578fdb 2850 I32 i;
b93070ed 2851 for (i = prog->nparens; i > (I32)prog->lastparen; i--) {
f0ab9afb
NC
2852 ++pp;
2853 pp->start = -1;
2854 pp->end = -1;
d6a28714
JH
2855 }
2856 }
dafc8851 2857#endif
02db2b7b 2858 REGCP_SET(lastcp);
f73aaa43
DM
2859 result = regmatch(reginfo, *startposp, progi->program + 1);
2860 if (result != -1) {
2861 prog->offs[0].end = result;
d6a28714
JH
2862 return 1;
2863 }
24b23f37 2864 if (reginfo->cutpoint)
f73aaa43 2865 *startposp= reginfo->cutpoint;
02db2b7b 2866 REGCP_UNWIND(lastcp);
d6a28714
JH
2867 return 0;
2868}
2869
02db2b7b 2870
8ba1375e
MJD
2871#define sayYES goto yes
2872#define sayNO goto no
262b90c4 2873#define sayNO_SILENT goto no_silent
8ba1375e 2874
f9f4320a
YO
2875/* we dont use STMT_START/END here because it leads to
2876 "unreachable code" warnings, which are bogus, but distracting. */
2877#define CACHEsayNO \
c476f425
DM
2878 if (ST.cache_mask) \
2879 PL_reg_poscache[ST.cache_offset] |= ST.cache_mask; \
f9f4320a 2880 sayNO
3298f257 2881
a3621e74 2882/* this is used to determine how far from the left messages like
265c4333
YO
2883 'failed...' are printed. It should be set such that messages
2884 are inline with the regop output that created them.
a3621e74 2885*/
265c4333 2886#define REPORT_CODE_OFF 32
a3621e74
YO
2887
2888
40a82448
DM
2889#define CHRTEST_UNINIT -1001 /* c1/c2 haven't been calculated yet */
2890#define CHRTEST_VOID -1000 /* the c1/c2 "next char" test should be skipped */
9e137952 2891
86545054
DM
2892#define SLAB_FIRST(s) (&(s)->states[0])
2893#define SLAB_LAST(s) (&(s)->states[PERL_REGMATCH_SLAB_SLOTS-1])
2894
5d9a96ca
DM
2895/* grab a new slab and return the first slot in it */
2896
2897STATIC regmatch_state *
2898S_push_slab(pTHX)
2899{
a35a87e7 2900#if PERL_VERSION < 9 && !defined(PERL_CORE)
54df2634
NC
2901 dMY_CXT;
2902#endif
5d9a96ca
DM
2903 regmatch_slab *s = PL_regmatch_slab->next;
2904 if (!s) {
2905 Newx(s, 1, regmatch_slab);
2906 s->prev = PL_regmatch_slab;
2907 s->next = NULL;
2908 PL_regmatch_slab->next = s;
2909 }
2910 PL_regmatch_slab = s;
86545054 2911 return SLAB_FIRST(s);
5d9a96ca 2912}
5b47454d 2913
95b24440 2914
40a82448
DM
2915/* push a new state then goto it */
2916
4d5016e5
DM
2917#define PUSH_STATE_GOTO(state, node, input) \
2918 pushinput = input; \
40a82448
DM
2919 scan = node; \
2920 st->resume_state = state; \
2921 goto push_state;
2922
2923/* push a new state with success backtracking, then goto it */
2924
4d5016e5
DM
2925#define PUSH_YES_STATE_GOTO(state, node, input) \
2926 pushinput = input; \
40a82448
DM
2927 scan = node; \
2928 st->resume_state = state; \
2929 goto push_yes_state;
2930
aa283a38 2931
aa283a38 2932
4d5016e5 2933
d6a28714 2934/*
95b24440 2935
bf1f174e
DM
2936regmatch() - main matching routine
2937
2938This is basically one big switch statement in a loop. We execute an op,
2939set 'next' to point the next op, and continue. If we come to a point which
2940we may need to backtrack to on failure such as (A|B|C), we push a
2941backtrack state onto the backtrack stack. On failure, we pop the top
2942state, and re-enter the loop at the state indicated. If there are no more
2943states to pop, we return failure.
2944
2945Sometimes we also need to backtrack on success; for example /A+/, where
2946after successfully matching one A, we need to go back and try to
2947match another one; similarly for lookahead assertions: if the assertion
2948completes successfully, we backtrack to the state just before the assertion
2949and then carry on. In these cases, the pushed state is marked as
2950'backtrack on success too'. This marking is in fact done by a chain of
2951pointers, each pointing to the previous 'yes' state. On success, we pop to
2952the nearest yes state, discarding any intermediate failure-only states.
2953Sometimes a yes state is pushed just to force some cleanup code to be
2954called at the end of a successful match or submatch; e.g. (??{$re}) uses
2955it to free the inner regex.
2956
2957Note that failure backtracking rewinds the cursor position, while
2958success backtracking leaves it alone.
2959
2960A pattern is complete when the END op is executed, while a subpattern
2961such as (?=foo) is complete when the SUCCESS op is executed. Both of these
2962ops trigger the "pop to last yes state if any, otherwise return true"
2963behaviour.
2964
2965A common convention in this function is to use A and B to refer to the two
2966subpatterns (or to the first nodes thereof) in patterns like /A*B/: so A is
2967the subpattern to be matched possibly multiple times, while B is the entire
2968rest of the pattern. Variable and state names reflect this convention.
2969
2970The states in the main switch are the union of ops and failure/success of
2971substates associated with with that op. For example, IFMATCH is the op
2972that does lookahead assertions /(?=A)B/ and so the IFMATCH state means
2973'execute IFMATCH'; while IFMATCH_A is a state saying that we have just
2974successfully matched A and IFMATCH_A_fail is a state saying that we have
2975just failed to match A. Resume states always come in pairs. The backtrack
2976state we push is marked as 'IFMATCH_A', but when that is popped, we resume
2977at IFMATCH_A or IFMATCH_A_fail, depending on whether we are backtracking
2978on success or failure.
2979
2980The struct that holds a backtracking state is actually a big union, with
2981one variant for each major type of op. The variable st points to the
2982top-most backtrack struct. To make the code clearer, within each
2983block of code we #define ST to alias the relevant union.
2984
2985Here's a concrete example of a (vastly oversimplified) IFMATCH
2986implementation:
2987
2988 switch (state) {
2989 ....
2990
2991#define ST st->u.ifmatch
2992
2993 case IFMATCH: // we are executing the IFMATCH op, (?=A)B
2994 ST.foo = ...; // some state we wish to save
95b24440 2995 ...
bf1f174e
DM
2996 // push a yes backtrack state with a resume value of
2997 // IFMATCH_A/IFMATCH_A_fail, then continue execution at the
2998 // first node of A:
4d5016e5 2999 PUSH_YES_STATE_GOTO(IFMATCH_A, A, newinput);
bf1f174e
DM
3000 // NOTREACHED
3001
3002 case IFMATCH_A: // we have successfully executed A; now continue with B
3003 next = B;
3004 bar = ST.foo; // do something with the preserved value
3005 break;
3006
3007 case IFMATCH_A_fail: // A failed, so the assertion failed
3008 ...; // do some housekeeping, then ...
3009 sayNO; // propagate the failure
3010
3011#undef ST
95b24440 3012
bf1f174e
DM
3013 ...
3014 }
95b24440 3015
bf1f174e
DM
3016For any old-timers reading this who are familiar with the old recursive
3017approach, the code above is equivalent to:
95b24440 3018
bf1f174e
DM
3019 case IFMATCH: // we are executing the IFMATCH op, (?=A)B
3020 {
3021 int foo = ...
95b24440 3022 ...
bf1f174e
DM
3023 if (regmatch(A)) {
3024 next = B;
3025 bar = foo;
3026 break;
95b24440 3027 }
bf1f174e
DM
3028 ...; // do some housekeeping, then ...
3029 sayNO; // propagate the failure
95b24440 3030 }
bf1f174e
DM
3031
3032The topmost backtrack state, pointed to by st, is usually free. If you
3033want to claim it, populate any ST.foo fields in it with values you wish to
3034save, then do one of
3035
4d5016e5
DM
3036 PUSH_STATE_GOTO(resume_state, node, newinput);
3037 PUSH_YES_STATE_GOTO(resume_state, node, newinput);
bf1f174e
DM
3038
3039which sets that backtrack state's resume value to 'resume_state', pushes a
3040new free entry to the top of the backtrack stack, then goes to 'node'.
3041On backtracking, the free slot is popped, and the saved state becomes the
3042new free state. An ST.foo field in this new top state can be temporarily
3043accessed to retrieve values, but once the main loop is re-entered, it
3044becomes available for reuse.
3045
3046Note that the depth of the backtrack stack constantly increases during the
3047left-to-right execution of the pattern, rather than going up and down with
3048the pattern nesting. For example the stack is at its maximum at Z at the
3049end of the pattern, rather than at X in the following:
3050
3051 /(((X)+)+)+....(Y)+....Z/
3052
3053The only exceptions to this are lookahead/behind assertions and the cut,
3054(?>A), which pop all the backtrack states associated with A before
3055continuing.
3056
486ec47a 3057Backtrack state structs are allocated in slabs of about 4K in size.
bf1f174e
DM
3058PL_regmatch_state and st always point to the currently active state,
3059and PL_regmatch_slab points to the slab currently containing
3060PL_regmatch_state. The first time regmatch() is called, the first slab is
3061allocated, and is never freed until interpreter destruction. When the slab
3062is full, a new one is allocated and chained to the end. At exit from
3063regmatch(), slabs allocated since entry are freed.
3064
3065*/
95b24440 3066
40a82448 3067
5bc10b2c 3068#define DEBUG_STATE_pp(pp) \
265c4333 3069 DEBUG_STATE_r({ \
f2ed9b32 3070 DUMP_EXEC_POS(locinput, scan, utf8_target); \
5bc10b2c 3071 PerlIO_printf(Perl_debug_log, \
5d458dd8 3072 " %*s"pp" %s%s%s%s%s\n", \
5bc10b2c 3073 depth*2, "", \
13d6edb4 3074 PL_reg_name[st->resume_state], \
5d458dd8
YO
3075 ((st==yes_state||st==mark_state) ? "[" : ""), \
3076 ((st==yes_state) ? "Y" : ""), \
3077 ((st==mark_state) ? "M" : ""), \
3078 ((st==yes_state||st==mark_state) ? "]" : "") \
3079 ); \
265c4333 3080 });
5bc10b2c 3081
40a82448 3082
3dab1dad 3083#define REG_NODE_NUM(x) ((x) ? (int)((x)-prog) : -1)
95b24440 3084
3df15adc 3085#ifdef DEBUGGING
5bc10b2c 3086
ab3bbdeb 3087STATIC void
f2ed9b32 3088S_debug_start_match(pTHX_ const REGEXP *prog, const bool utf8_target,
ab3bbdeb
YO
3089 const char *start, const char *end, const char *blurb)
3090{
efd26800 3091 const bool utf8_pat = RX_UTF8(prog) ? 1 : 0;
7918f24d
NC
3092
3093 PERL_ARGS_ASSERT_DEBUG_START_MATCH;
3094
ab3bbdeb
YO
3095 if (!PL_colorset)
3096 reginitcolors();
3097 {
3098 RE_PV_QUOTED_DECL(s0, utf8_pat, PERL_DEBUG_PAD_ZERO(0),
d2c6dc5e 3099 RX_PRECOMP_const(prog), RX_PRELEN(prog), 60);
ab3bbdeb 3100
f2ed9b32 3101 RE_PV_QUOTED_DECL(s1, utf8_target, PERL_DEBUG_PAD_ZERO(1),
ab3bbdeb
YO
3102 start, end - start, 60);
3103
3104 PerlIO_printf(Perl_debug_log,
3105 "%s%s REx%s %s against %s\n",
3106 PL_colors[4], blurb, PL_colors[5], s0, s1);
3107
f2ed9b32 3108 if (utf8_target||utf8_pat)
1de06328
YO
3109 PerlIO_printf(Perl_debug_log, "UTF-8 %s%s%s...\n",
3110 utf8_pat ? "pattern" : "",
f2ed9b32
KW
3111 utf8_pat && utf8_target ? " and " : "",
3112 utf8_target ? "string" : ""
ab3bbdeb
YO
3113 );
3114 }
3115}
3df15adc
YO
3116
3117STATIC void
786e8c11
YO
3118S_dump_exec_pos(pTHX_ const char *locinput,
3119 const regnode *scan,
3120 const char *loc_regeol,
3121 const char *loc_bostr,
3122 const char *loc_reg_starttry,
f2ed9b32 3123 const bool utf8_target)
07be1b83 3124{
786e8c11 3125 const int docolor = *PL_colors[0] || *PL_colors[2] || *PL_colors[4];
07be1b83 3126 const int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
786e8c11 3127 int l = (loc_regeol - locinput) > taill ? taill : (loc_regeol - locinput);
07be1b83
YO
3128 /* The part of the string before starttry has one color
3129 (pref0_len chars), between starttry and current
3130 position another one (pref_len - pref0_len chars),
3131 after the current position the third one.
3132 We assume that pref0_len <= pref_len, otherwise we
3133 decrease pref0_len. */
786e8c11
YO
3134 int pref_len = (locinput - loc_bostr) > (5 + taill) - l
3135 ? (5 + taill) - l : locinput - loc_bostr;
07be1b83
YO
3136 int pref0_len;
3137
7918f24d
NC
3138 PERL_ARGS_ASSERT_DUMP_EXEC_POS;
3139
f2ed9b32 3140 while (utf8_target && UTF8_IS_CONTINUATION(*(U8*)(locinput - pref_len)))
07be1b83 3141 pref_len++;
786e8c11
YO
3142 pref0_len = pref_len - (locinput - loc_reg_starttry);
3143 if (l + pref_len < (5 + taill) && l < loc_regeol - locinput)
3144 l = ( loc_regeol - locinput > (5 + taill) - pref_len
3145 ? (5 + taill) - pref_len : loc_regeol - locinput);
f2ed9b32 3146 while (utf8_target && UTF8_IS_CONTINUATION(*(U8*)(locinput + l)))
07be1b83
YO
3147 l--;
3148 if (pref0_len < 0)
3149 pref0_len = 0;
3150 if (pref0_len > pref_len)
3151 pref0_len = pref_len;
3152 {
f2ed9b32 3153 const int is_uni = (utf8_target && OP(scan) != CANY) ? 1 : 0;
0df25f3d 3154
ab3bbdeb 3155 RE_PV_COLOR_DECL(s0,len0,is_uni,PERL_DEBUG_PAD(0),
1de06328 3156 (locinput - pref_len),pref0_len, 60, 4, 5);
0df25f3d 3157
ab3bbdeb 3158 RE_PV_COLOR_DECL(s1,len1,is_uni,PERL_DEBUG_PAD(1),
3df15adc 3159 (locinput - pref_len + pref0_len),
1de06328 3160 pref_len - pref0_len, 60, 2, 3);
0df25f3d 3161
ab3bbdeb 3162 RE_PV_COLOR_DECL(s2,len2,is_uni,PERL_DEBUG_PAD(2),
1de06328 3163 locinput, loc_regeol - locinput, 10, 0, 1);
0df25f3d 3164
1de06328 3165 const STRLEN tlen=len0+len1+len2;
3df15adc 3166 PerlIO_printf(Perl_debug_log,
ab3bbdeb 3167 "%4"IVdf" <%.*s%.*s%s%.*s>%*s|",
786e8c11 3168 (IV)(locinput - loc_bostr),
07be1b83 3169 len0, s0,
07be1b83 3170 len1, s1,
07be1b83 3171 (docolor ? "" : "> <"),
07be1b83 3172 len2, s2,
f9f4320a 3173 (int)(tlen > 19 ? 0 : 19 - tlen),
07be1b83
YO
3174 "");
3175 }
3176}
3df15adc 3177
07be1b83
YO
3178#endif
3179
0a4db386
YO
3180/* reg_check_named_buff_matched()
3181 * Checks to see if a named buffer has matched. The data array of
3182 * buffer numbers corresponding to the buffer is expected to reside
3183 * in the regexp->data->data array in the slot stored in the ARG() of
3184 * node involved. Note that this routine doesn't actually care about the
3185 * name, that information is not preserved from compilation to execution.
3186 * Returns the index of the leftmost defined buffer with the given name
3187 * or 0 if non of the buffers matched.
3188 */
3189STATIC I32
7918f24d
NC
3190S_reg_check_named_buff_matched(pTHX_ const regexp *rex, const regnode *scan)
3191{
0a4db386 3192 I32 n;
f8fc2ecf 3193 RXi_GET_DECL(rex,rexi);
ad64d0ec 3194 SV *sv_dat= MUTABLE_SV(rexi->data->data[ ARG( scan ) ]);
0a4db386 3195 I32 *nums=(I32*)SvPVX(sv_dat);
7918f24d
NC
3196
3197 PERL_ARGS_ASSERT_REG_CHECK_NAMED_BUFF_MATCHED;
3198
0a4db386 3199 for ( n=0; n<SvIVX(sv_dat); n++ ) {
b93070ed
DM
3200 if ((I32)rex->lastparen >= nums[n] &&
3201 rex->offs[nums[n]].end != -1)
0a4db386
YO
3202 {
3203 return nums[n];
3204 }
3205 }
3206 return 0;
3207}
3208
2f554ef7
DM
3209
3210/* free all slabs above current one - called during LEAVE_SCOPE */
3211
3212STATIC void
3213S_clear_backtrack_stack(pTHX_ void *p)
3214{
3215 regmatch_slab *s = PL_regmatch_slab->next;
3216 PERL_UNUSED_ARG(p);
3217
3218 if (!s)
3219 return;
3220 PL_regmatch_slab->next = NULL;
3221 while (s) {
3222 regmatch_slab * const osl = s;
3223 s = s->next;
3224 Safefree(osl);
3225 }
3226}
3227
3228
f73aaa43
DM
3229/* returns -1 on failure, $+[0] on success */
3230STATIC I32
3231S_regmatch(pTHX_ regmatch_info *reginfo, char *startpos, regnode *prog)
d6a28714 3232{
a35a87e7 3233#if PERL_VERSION < 9 && !defined(PERL_CORE)
54df2634
NC
3234 dMY_CXT;
3235#endif
27da23d5 3236 dVAR;
eb578fdb 3237 const bool utf8_target = PL_reg_match_utf8;
4ad0818d 3238 const U32 uniflags = UTF8_ALLOW_DEFAULT;
288b8c02
NC
3239 REGEXP *rex_sv = reginfo->prog;
3240 regexp *rex = (struct regexp *)SvANY(rex_sv);
f8fc2ecf 3241 RXi_GET_DECL(rex,rexi);
2f554ef7 3242 I32 oldsave;
5d9a96ca 3243 /* the current state. This is a cached copy of PL_regmatch_state */
eb578fdb 3244 regmatch_state *st;
5d9a96ca 3245 /* cache heavy used fields of st in registers */
eb578fdb
KW
3246 regnode *scan;
3247 regnode *next;
3248 U32 n = 0; /* general value; init to avoid compiler warning */
3249 I32 ln = 0; /* len or last; init to avoid compiler warning */
d60de1d1 3250 char *locinput = startpos;
4d5016e5 3251 char *pushinput; /* where to continue after a PUSH */
eb578fdb 3252 I32 nextchr; /* is always set to UCHARAT(locinput) */
24d3c4a9 3253
b69b0499 3254 bool result = 0; /* return value of S_regmatch */
24d3c4a9 3255 int depth = 0; /* depth of backtrack stack */
4b196cd4
YO
3256 U32 nochange_depth = 0; /* depth of GOSUB recursion with nochange */
3257 const U32 max_nochange_depth =
3258 (3 * rex->nparens > MAX_RECURSE_EVAL_NOCHANGE_DEPTH) ?
3259 3 * rex->nparens : MAX_RECURSE_EVAL_NOCHANGE_DEPTH;
77cb431f
DM
3260 regmatch_state *yes_state = NULL; /* state to pop to on success of
3261 subpattern */
e2e6a0f1
YO
3262 /* mark_state piggy backs on the yes_state logic so that when we unwind
3263 the stack on success we can update the mark_state as we go */
3264 regmatch_state *mark_state = NULL; /* last mark state we have seen */
faec1544 3265 regmatch_state *cur_eval = NULL; /* most recent EVAL_AB state */
b8591aee 3266 struct regmatch_state *cur_curlyx = NULL; /* most recent curlyx */
40a82448 3267 U32 state_num;
5d458dd8
YO
3268 bool no_final = 0; /* prevent failure from backtracking? */
3269 bool do_cutgroup = 0; /* no_final only until next branch/trie entry */
d60de1d1 3270 char *startpoint = locinput;
5d458dd8
YO
3271 SV *popmark = NULL; /* are we looking for a mark? */
3272 SV *sv_commit = NULL; /* last mark name seen in failure */
3273 SV *sv_yes_mark = NULL; /* last mark name we have seen
486ec47a 3274 during a successful match */
5d458dd8
YO
3275 U32 lastopen = 0; /* last open we saw */
3276 bool has_cutgroup = RX_HAS_CUTGROUP(rex) ? 1 : 0;
19b95bf0 3277 SV* const oreplsv = GvSV(PL_replgv);
24d3c4a9
DM
3278 /* these three flags are set by various ops to signal information to
3279 * the very next op. They have a useful lifetime of exactly one loop
3280 * iteration, and are not preserved or restored by state pushes/pops
3281 */
3282 bool sw = 0; /* the condition value in (?(cond)a|b) */
3283 bool minmod = 0; /* the next "{n,m}" is a "{n,m}?" */
3284 int logical = 0; /* the following EVAL is:
3285 0: (?{...})
3286 1: (?(?{...})X|Y)
3287 2: (??{...})
3288 or the following IFMATCH/UNLESSM is:
3289 false: plain (?=foo)
3290 true: used as a condition: (?(?=foo))
3291 */
81ed78b2
DM
3292 PAD* last_pad = NULL;
3293 dMULTICALL;
3294 I32 gimme = G_SCALAR;
3295 CV *caller_cv = NULL; /* who called us */
3296 CV *last_pushed_cv = NULL; /* most recently called (?{}) CV */
74088413 3297 CHECKPOINT runops_cp; /* savestack position before executing EVAL */
81ed78b2 3298
95b24440 3299#ifdef DEBUGGING
e68ec53f 3300 GET_RE_DEBUG_FLAGS_DECL;
d6a28714
JH
3301#endif
3302
81ed78b2
DM
3303 /* shut up 'may be used uninitialized' compiler warnings for dMULTICALL */
3304 multicall_oldcatch = 0;
3305 multicall_cv = NULL;
3306 cx = NULL;
4f8dbb2d
JL
3307 PERL_UNUSED_VAR(multicall_cop);
3308 PERL_UNUSED_VAR(newsp);
81ed78b2
DM
3309
3310
7918f24d
NC
3311 PERL_ARGS_ASSERT_REGMATCH;
3312
3b57cd43 3313 DEBUG_OPTIMISE_r( DEBUG_EXECUTE_r({
24b23f37 3314 PerlIO_printf(Perl_debug_log,"regmatch start\n");
3b57cd43 3315 }));
5d9a96ca
DM
3316 /* on first ever call to regmatch, allocate first slab */
3317 if (!PL_regmatch_slab) {
3318 Newx(PL_regmatch_slab, 1, regmatch_slab);
3319 PL_regmatch_slab->prev = NULL;
3320 PL_regmatch_slab->next = NULL;
86545054 3321 PL_regmatch_state = SLAB_FIRST(PL_regmatch_slab);
5d9a96ca
DM
3322 }
3323
2f554ef7
DM
3324 oldsave = PL_savestack_ix;
3325 SAVEDESTRUCTOR_X(S_clear_backtrack_stack, NULL);
3326 SAVEVPTR(PL_regmatch_slab);
3327 SAVEVPTR(PL_regmatch_state);
5d9a96ca
DM
3328
3329 /* grab next free state slot */
3330 st = ++PL_regmatch_state;
86545054 3331 if (st > SLAB_LAST(PL_regmatch_slab))
5d9a96ca
DM
3332 st = PL_regmatch_state = S_push_slab(aTHX);
3333
d6a28714 3334 /* Note that nextchr is a byte even in UTF */
7016d6eb 3335 SET_nextchr;
d6a28714
JH
3336 scan = prog;
3337 while (scan != NULL) {
8ba1375e 3338
a3621e74 3339 DEBUG_EXECUTE_r( {
6136c704 3340 SV * const prop = sv_newmortal();
1de06328 3341 regnode *rnext=regnext(scan);
f2ed9b32 3342 DUMP_EXEC_POS( locinput, scan, utf8_target );
32fc9b6a 3343 regprop(rex, prop, scan);
07be1b83
YO
3344
3345 PerlIO_printf(Perl_debug_log,
3346 "%3"IVdf":%*s%s(%"IVdf")\n",
f8fc2ecf 3347 (IV)(scan - rexi->program), depth*2, "",
07be1b83 3348 SvPVX_const(prop),
1de06328 3349 (PL_regkind[OP(scan)] == END || !rnext) ?
f8fc2ecf 3350 0 : (IV)(rnext - rexi->program));
2a782b5b 3351 });
d6a28714
JH
3352
3353 next = scan + NEXT_OFF(scan);
3354 if (next == scan)
3355 next = NULL;
40a82448 3356 state_num = OP(scan);
d6a28714 3357
40a82448 3358 reenter_switch:
34a81e2b 3359
7016d6eb 3360 SET_nextchr;
bf798dc4 3361
40a82448 3362 switch (state_num) {
3c0563b9 3363 case BOL: /* /^../ */
7fba1cd6 3364 if (locinput == PL_bostr)
d6a28714 3365 {
3b0527fe 3366 /* reginfo->till = reginfo->bol; */
b8c5462f
JH
3367 break;
3368 }
d6a28714 3369 sayNO;
3c0563b9
DM
3370
3371 case MBOL: /* /^../m */
12d33761 3372 if (locinput == PL_bostr ||
7016d6eb 3373 (!NEXTCHR_IS_EOS && locinput[-1] == '\n'))
d6a28714 3374 {
b8c5462f
JH
3375 break;
3376 }
d6a28714 3377 sayNO;
3c0563b9
DM
3378
3379 case SBOL: /* /^../s */
c2a73568 3380 if (locinput == PL_bostr)
b8c5462f 3381 break;
d6a28714 3382 sayNO;
3c0563b9
DM
3383
3384 case GPOS: /* \G */
3b0527fe 3385 if (locinput == reginfo->ganch)
d6a28714
JH
3386 break;
3387 sayNO;
ee9b8eae 3388
3c0563b9 3389 case KEEPS: /* \K */
ee9b8eae 3390 /* update the startpoint */
b93070ed 3391 st->u.keeper.val = rex->offs[0].start;
b93070ed 3392 rex->offs[0].start = locinput - PL_bostr;
4d5016e5 3393 PUSH_STATE_GOTO(KEEPS_next, next, locinput);
ee9b8eae
YO
3394 /*NOT-REACHED*/
3395 case KEEPS_next_fail:
3396 /* rollback the start point change */
b93070ed 3397 rex->offs[0].start = st->u.keeper.val;
ee9b8eae
YO
3398 sayNO_SILENT;
3399 /*NOT-REACHED*/
3c0563b9
DM
3400
3401 case EOL: /* /..$/ */
d6a28714 3402 goto seol;
3c0563b9
DM
3403
3404 case MEOL: /* /..$/m */
7016d6eb 3405 if (!NEXTCHR_IS_EOS && nextchr != '\n')
b8c5462f 3406 sayNO;
b8c5462f 3407 break;
3c0563b9
DM
3408
3409 case SEOL: /* /..$/s */
d6a28714 3410 seol:
7016d6eb 3411 if (!NEXTCHR_IS_EOS && nextchr != '\n')
b8c5462f 3412 sayNO;
d6a28714 3413 if (PL_regeol - locinput > 1)
b8c5462f 3414 sayNO;
b8c5462f 3415 break;
3c0563b9
DM
3416