This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
regcomp.h: Clean up some comments
[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
c277df42
IZ
83#define RF_tainted 1 /* tainted information used? */
84#define RF_warned 2 /* warned about big count? */
faec1544 85
ab3bbdeb 86#define RF_utf8 8 /* Pattern contains multibyte chars? */
a0ed51b3 87
f2ed9b32 88#define UTF_PATTERN ((PL_reg_flags & RF_utf8) != 0)
ce862d02
IZ
89
90#define RS_init 1 /* eval environment created */
91#define RS_set 2 /* replsv value is set */
c277df42 92
a687059c
LW
93#ifndef STATIC
94#define STATIC static
95#endif
96
af364d03
KW
97/* Valid for non-utf8 strings only: avoids the reginclass call if there are no
98 * complications: i.e., if everything matchable is straight forward in the
99 * bitmap */
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
20d0b1e9 124/* these are unrolled below in the CCC_TRY_XXX defined */
1a4fad37
AL
125#define LOAD_UTF8_CHARCLASS(class,str) STMT_START { \
126 if (!CAT2(PL_utf8_,class)) { bool ok; ENTER; save_re_context(); ok=CAT2(is_utf8_,class)((const U8*)str); assert(ok); LEAVE; } } STMT_END
37e2e78e
KW
127
128/* Doesn't do an assert to verify that is correct */
129#define LOAD_UTF8_CHARCLASS_NO_CHECK(class) STMT_START { \
130 if (!CAT2(PL_utf8_,class)) { bool ok; ENTER; save_re_context(); ok=CAT2(is_utf8_,class)((const U8*)" "); LEAVE; } } STMT_END
131
1a4fad37
AL
132#define LOAD_UTF8_CHARCLASS_ALNUM() LOAD_UTF8_CHARCLASS(alnum,"a")
133#define LOAD_UTF8_CHARCLASS_DIGIT() LOAD_UTF8_CHARCLASS(digit,"0")
134#define LOAD_UTF8_CHARCLASS_SPACE() LOAD_UTF8_CHARCLASS(space," ")
51371543 135
37e2e78e 136#define LOAD_UTF8_CHARCLASS_GCB() /* Grapheme cluster boundaries */ \
d275fa5e
CB
137 LOAD_UTF8_CHARCLASS(X_begin, " "); \
138 LOAD_UTF8_CHARCLASS(X_non_hangul, "A"); \
37e2e78e
KW
139 /* These are utf8 constants, and not utf-ebcdic constants, so the \
140 * assert should likely and hopefully fail on an EBCDIC machine */ \
d275fa5e 141 LOAD_UTF8_CHARCLASS(X_extend, "\xcc\x80"); /* U+0300 */ \
37e2e78e
KW
142 \
143 /* No asserts are done for these, in case called on an early \
144 * Unicode version in which they map to nothing */ \
d275fa5e
CB
145 LOAD_UTF8_CHARCLASS_NO_CHECK(X_prepend);/* U+0E40 "\xe0\xb9\x80" */ \
146 LOAD_UTF8_CHARCLASS_NO_CHECK(X_L); /* U+1100 "\xe1\x84\x80" */ \
147 LOAD_UTF8_CHARCLASS_NO_CHECK(X_LV); /* U+AC00 "\xea\xb0\x80" */ \
148 LOAD_UTF8_CHARCLASS_NO_CHECK(X_LVT); /* U+AC01 "\xea\xb0\x81" */ \
149 LOAD_UTF8_CHARCLASS_NO_CHECK(X_LV_LVT_V);/* U+AC01 "\xea\xb0\x81" */\
150 LOAD_UTF8_CHARCLASS_NO_CHECK(X_T); /* U+11A8 "\xe1\x86\xa8" */ \
37e2e78e 151 LOAD_UTF8_CHARCLASS_NO_CHECK(X_V) /* U+1160 "\xe1\x85\xa0" */
20d0b1e9 152
d1eb3177
YO
153/*
154 We dont use PERL_LEGACY_UNICODE_CHARCLASS_MAPPINGS as the direct test
155 so that it is possible to override the option here without having to
156 rebuild the entire core. as we are required to do if we change regcomp.h
157 which is where PERL_LEGACY_UNICODE_CHARCLASS_MAPPINGS is defined.
158*/
159#if PERL_LEGACY_UNICODE_CHARCLASS_MAPPINGS
160#define BROKEN_UNICODE_CHARCLASS_MAPPINGS
161#endif
162
163#ifdef BROKEN_UNICODE_CHARCLASS_MAPPINGS
164#define LOAD_UTF8_CHARCLASS_PERL_WORD() LOAD_UTF8_CHARCLASS_ALNUM()
165#define LOAD_UTF8_CHARCLASS_PERL_SPACE() LOAD_UTF8_CHARCLASS_SPACE()
166#define LOAD_UTF8_CHARCLASS_POSIX_DIGIT() LOAD_UTF8_CHARCLASS_DIGIT()
167#define RE_utf8_perl_word PL_utf8_alnum
168#define RE_utf8_perl_space PL_utf8_space
169#define RE_utf8_posix_digit PL_utf8_digit
170#define perl_word alnum
171#define perl_space space
172#define posix_digit digit
173#else
174#define LOAD_UTF8_CHARCLASS_PERL_WORD() LOAD_UTF8_CHARCLASS(perl_word,"a")
175#define LOAD_UTF8_CHARCLASS_PERL_SPACE() LOAD_UTF8_CHARCLASS(perl_space," ")
176#define LOAD_UTF8_CHARCLASS_POSIX_DIGIT() LOAD_UTF8_CHARCLASS(posix_digit,"0")
177#define RE_utf8_perl_word PL_utf8_perl_word
178#define RE_utf8_perl_space PL_utf8_perl_space
179#define RE_utf8_posix_digit PL_utf8_posix_digit
180#endif
181
182
c02a0702
KW
183#define _CCC_TRY_AFF_COMMON(NAME,NAMEL,CLASS,STR,LCFUNC_utf8,FUNC) \
184 case NAMEL: \
185 PL_reg_flags |= RF_tainted; \
186 /* FALL THROUGH */ \
187 case NAME: \
188 if (!nextchr) \
189 sayNO; \
190 if (utf8_target && UTF8_IS_CONTINUED(nextchr)) { \
191 if (!CAT2(PL_utf8_,CLASS)) { \
192 bool ok; \
193 ENTER; \
194 save_re_context(); \
195 ok=CAT2(is_utf8_,CLASS)((const U8*)STR); \
196 assert(ok); \
197 LEAVE; \
198 } \
199 if (!(OP(scan) == NAME \
f2ed9b32 200 ? cBOOL(swash_fetch(CAT2(PL_utf8_,CLASS), (U8*)locinput, utf8_target)) \
c02a0702
KW
201 : LCFUNC_utf8((U8*)locinput))) \
202 { \
203 sayNO; \
204 } \
205 locinput += PL_utf8skip[nextchr]; \
206 nextchr = UCHARAT(locinput); \
207 break; \
208 } \
a12cf05f 209 /* Drops through to the macro that calls this one */
c02a0702
KW
210
211#define CCC_TRY_AFF(NAME,NAMEL,CLASS,STR,LCFUNC_utf8,FUNC,LCFUNC) \
212 _CCC_TRY_AFF_COMMON(NAME,NAMEL,CLASS,STR,LCFUNC_utf8,FUNC) \
213 if (!(OP(scan) == NAME ? FUNC(nextchr) : LCFUNC(nextchr))) \
214 sayNO; \
215 nextchr = UCHARAT(++locinput); \
20d0b1e9
YO
216 break
217
b10ac0d8
KW
218/* Almost identical to the above, but has a case for a node that matches chars
219 * between 128 and 255 using Unicode (latin1) semantics. */
220#define CCC_TRY_AFF_U(NAME,NAMEL,CLASS,STR,LCFUNC_utf8,FUNCU,LCFUNC) \
c02a0702 221 _CCC_TRY_AFF_COMMON(NAME,NAMEL,CLASS,STR,LCFUNC_utf8,FUNC) \
b10ac0d8 222 if (!(OP(scan) == NAMEL ? LCFUNC(nextchr) : (FUNCU(nextchr) && (isASCII(nextchr) || (FLAGS(scan) & USE_UNI))))) \
c02a0702
KW
223 sayNO; \
224 nextchr = UCHARAT(++locinput); \
b10ac0d8
KW
225 break
226
c02a0702
KW
227#define _CCC_TRY_NEG_COMMON(NAME,NAMEL,CLASS,STR,LCFUNC_utf8,FUNC) \
228 case NAMEL: \
229 PL_reg_flags |= RF_tainted; \
230 /* FALL THROUGH */ \
231 case NAME : \
232 if (!nextchr && locinput >= PL_regeol) \
233 sayNO; \
234 if (utf8_target && UTF8_IS_CONTINUED(nextchr)) { \
235 if (!CAT2(PL_utf8_,CLASS)) { \
236 bool ok; \
237 ENTER; \
238 save_re_context(); \
239 ok=CAT2(is_utf8_,CLASS)((const U8*)STR); \
240 assert(ok); \
241 LEAVE; \
242 } \
243 if ((OP(scan) == NAME \
f2ed9b32 244 ? cBOOL(swash_fetch(CAT2(PL_utf8_,CLASS), (U8*)locinput, utf8_target)) \
c02a0702
KW
245 : LCFUNC_utf8((U8*)locinput))) \
246 { \
247 sayNO; \
248 } \
249 locinput += PL_utf8skip[nextchr]; \
250 nextchr = UCHARAT(locinput); \
251 break; \
b10ac0d8
KW
252 }
253
c02a0702
KW
254#define CCC_TRY_NEG(NAME,NAMEL,CLASS,STR,LCFUNC_utf8,FUNC,LCFUNC) \
255 _CCC_TRY_NEG_COMMON(NAME,NAMEL,CLASS,STR,LCFUNC_utf8,FUNC) \
256 if ((OP(scan) == NAME ? FUNC(nextchr) : LCFUNC(nextchr))) \
257 sayNO; \
258 nextchr = UCHARAT(++locinput); \
20d0b1e9
YO
259 break
260
261
c02a0702
KW
262#define CCC_TRY_NEG_U(NAME,NAMEL,CLASS,STR,LCFUNC_utf8,FUNCU,LCFUNC) \
263 _CCC_TRY_NEG_COMMON(NAME,NAMEL,CLASS,STR,LCFUNC_utf8,FUNCU) \
b10ac0d8 264 if ((OP(scan) == NAMEL ? LCFUNC(nextchr) : (FUNCU(nextchr) && (isASCII(nextchr) || (FLAGS(scan) & USE_UNI))))) \
c02a0702
KW
265 sayNO; \
266 nextchr = UCHARAT(++locinput); \
b10ac0d8 267 break
d1eb3177
YO
268
269
270
3dab1dad
YO
271/* TODO: Combine JUMPABLE and HAS_TEXT to cache OP(rn) */
272
5f80c4cf 273/* for use after a quantifier and before an EXACT-like node -- japhy */
c35dcbe2
YO
274/* it would be nice to rework regcomp.sym to generate this stuff. sigh
275 *
276 * NOTE that *nothing* that affects backtracking should be in here, specifically
277 * VERBS must NOT be included. JUMPABLE is used to determine if we can ignore a
278 * node that is in between two EXACT like nodes when ascertaining what the required
279 * "follow" character is. This should probably be moved to regex compile time
280 * although it may be done at run time beause of the REF possibility - more
281 * investigation required. -- demerphq
282*/
3e901dc0
YO
283#define JUMPABLE(rn) ( \
284 OP(rn) == OPEN || \
285 (OP(rn) == CLOSE && (!cur_eval || cur_eval->u.eval.close_paren != ARG(rn))) || \
286 OP(rn) == EVAL || \
cca55fe3
JP
287 OP(rn) == SUSPEND || OP(rn) == IFMATCH || \
288 OP(rn) == PLUS || OP(rn) == MINMOD || \
d1c771f5 289 OP(rn) == KEEPS || \
3dab1dad 290 (PL_regkind[OP(rn)] == CURLY && ARG1(rn) > 0) \
e2d8ce26 291)
ee9b8eae 292#define IS_EXACT(rn) (PL_regkind[OP(rn)] == EXACT)
e2d8ce26 293
ee9b8eae
YO
294#define HAS_TEXT(rn) ( IS_EXACT(rn) || PL_regkind[OP(rn)] == REF )
295
296#if 0
297/* Currently these are only used when PL_regkind[OP(rn)] == EXACT so
298 we don't need this definition. */
299#define IS_TEXT(rn) ( OP(rn)==EXACT || OP(rn)==REF || OP(rn)==NREF )
300#define IS_TEXTF(rn) ( OP(rn)==EXACTF || OP(rn)==REFF || OP(rn)==NREFF )
301#define IS_TEXTFL(rn) ( OP(rn)==EXACTFL || OP(rn)==REFFL || OP(rn)==NREFFL )
302
303#else
304/* ... so we use this as its faster. */
305#define IS_TEXT(rn) ( OP(rn)==EXACT )
306#define IS_TEXTF(rn) ( OP(rn)==EXACTF )
307#define IS_TEXTFL(rn) ( OP(rn)==EXACTFL )
308
309#endif
e2d8ce26 310
a84d97b6
HS
311/*
312 Search for mandatory following text node; for lookahead, the text must
313 follow but for lookbehind (rn->flags != 0) we skip to the next step.
314*/
cca55fe3 315#define FIND_NEXT_IMPT(rn) STMT_START { \
3dab1dad
YO
316 while (JUMPABLE(rn)) { \
317 const OPCODE type = OP(rn); \
318 if (type == SUSPEND || PL_regkind[type] == CURLY) \
e2d8ce26 319 rn = NEXTOPER(NEXTOPER(rn)); \
3dab1dad 320 else if (type == PLUS) \
cca55fe3 321 rn = NEXTOPER(rn); \
3dab1dad 322 else if (type == IFMATCH) \
a84d97b6 323 rn = (rn->flags == 0) ? NEXTOPER(NEXTOPER(rn)) : rn + ARG(rn); \
e2d8ce26 324 else rn += NEXT_OFF(rn); \
3dab1dad 325 } \
5f80c4cf 326} STMT_END
74750237 327
c476f425 328
acfe0abc 329static void restore_pos(pTHX_ void *arg);
51371543 330
620d5b66
NC
331#define REGCP_PAREN_ELEMS 4
332#define REGCP_OTHER_ELEMS 5
e0fa7e2b 333#define REGCP_FRAME_ELEMS 1
620d5b66
NC
334/* REGCP_FRAME_ELEMS are not part of the REGCP_OTHER_ELEMS and
335 * are needed for the regexp context stack bookkeeping. */
336
76e3520e 337STATIC CHECKPOINT
cea2e8a9 338S_regcppush(pTHX_ I32 parenfloor)
a0d0e21e 339{
97aff369 340 dVAR;
a3b680e6 341 const int retval = PL_savestack_ix;
a3b680e6 342 const int paren_elems_to_push = (PL_regsize - parenfloor) * REGCP_PAREN_ELEMS;
e0fa7e2b
NC
343 const UV total_elems = paren_elems_to_push + REGCP_OTHER_ELEMS;
344 const UV elems_shifted = total_elems << SAVE_TIGHT_SHIFT;
a0d0e21e 345 int p;
40a82448 346 GET_RE_DEBUG_FLAGS_DECL;
a0d0e21e 347
e49a9654
IH
348 if (paren_elems_to_push < 0)
349 Perl_croak(aTHX_ "panic: paren_elems_to_push < 0");
350
e0fa7e2b
NC
351 if ((elems_shifted >> SAVE_TIGHT_SHIFT) != total_elems)
352 Perl_croak(aTHX_ "panic: paren_elems_to_push offset %"UVuf
5df417d0
JH
353 " out of range (%lu-%ld)",
354 total_elems, (unsigned long)PL_regsize, (long)parenfloor);
e0fa7e2b 355
620d5b66 356 SSGROW(total_elems + REGCP_FRAME_ELEMS);
7f69552c 357
3280af22 358 for (p = PL_regsize; p > parenfloor; p--) {
b1ce53c5 359/* REGCP_PARENS_ELEMS are pushed per pairs of parentheses. */
f0ab9afb
NC
360 SSPUSHINT(PL_regoffs[p].end);
361 SSPUSHINT(PL_regoffs[p].start);
3280af22 362 SSPUSHPTR(PL_reg_start_tmp[p]);
a0d0e21e 363 SSPUSHINT(p);
e7707071 364 DEBUG_BUFFERS_r(PerlIO_printf(Perl_debug_log,
40a82448 365 " saving \\%"UVuf" %"IVdf"(%"IVdf")..%"IVdf"\n",
f0ab9afb 366 (UV)p, (IV)PL_regoffs[p].start,
40a82448 367 (IV)(PL_reg_start_tmp[p] - PL_bostr),
f0ab9afb 368 (IV)PL_regoffs[p].end
40a82448 369 ));
a0d0e21e 370 }
b1ce53c5 371/* REGCP_OTHER_ELEMS are pushed in any case, parentheses or no. */
f0ab9afb 372 SSPUSHPTR(PL_regoffs);
3280af22
NIS
373 SSPUSHINT(PL_regsize);
374 SSPUSHINT(*PL_reglastparen);
a01268b5 375 SSPUSHINT(*PL_reglastcloseparen);
3280af22 376 SSPUSHPTR(PL_reginput);
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
76e3520e 398STATIC char *
097eb12c 399S_regcppop(pTHX_ const regexp *rex)
a0d0e21e 400{
97aff369 401 dVAR;
e0fa7e2b 402 UV i;
a0d0e21e 403 char *input;
a3621e74
YO
404 GET_RE_DEBUG_FLAGS_DECL;
405
7918f24d
NC
406 PERL_ARGS_ASSERT_REGCPPOP;
407
b1ce53c5 408 /* Pop REGCP_OTHER_ELEMS before the parentheses loop starts. */
c6bf6a65 409 i = SSPOPUV;
e0fa7e2b
NC
410 assert((i & SAVE_MASK) == SAVEt_REGCONTEXT); /* Check that the magic cookie is there. */
411 i >>= SAVE_TIGHT_SHIFT; /* Parentheses elements to pop. */
a0d0e21e 412 input = (char *) SSPOPPTR;
a01268b5 413 *PL_reglastcloseparen = SSPOPINT;
3280af22
NIS
414 *PL_reglastparen = SSPOPINT;
415 PL_regsize = SSPOPINT;
f0ab9afb 416 PL_regoffs=(regexp_paren_pair *) SSPOPPTR;
b1ce53c5 417
620d5b66 418 i -= REGCP_OTHER_ELEMS;
b1ce53c5 419 /* Now restore the parentheses context. */
620d5b66 420 for ( ; i > 0; i -= REGCP_PAREN_ELEMS) {
1df70142 421 I32 tmps;
097eb12c 422 U32 paren = (U32)SSPOPINT;
3280af22 423 PL_reg_start_tmp[paren] = (char *) SSPOPPTR;
f0ab9afb 424 PL_regoffs[paren].start = SSPOPINT;
cf93c79d 425 tmps = SSPOPINT;
3280af22 426 if (paren <= *PL_reglastparen)
f0ab9afb 427 PL_regoffs[paren].end = tmps;
e7707071 428 DEBUG_BUFFERS_r(
c3464db5 429 PerlIO_printf(Perl_debug_log,
b900a521 430 " restoring \\%"UVuf" to %"IVdf"(%"IVdf")..%"IVdf"%s\n",
f0ab9afb 431 (UV)paren, (IV)PL_regoffs[paren].start,
b900a521 432 (IV)(PL_reg_start_tmp[paren] - PL_bostr),
f0ab9afb 433 (IV)PL_regoffs[paren].end,
3280af22 434 (paren > *PL_reglastparen ? "(no)" : ""));
c277df42 435 );
a0d0e21e 436 }
e7707071 437 DEBUG_BUFFERS_r(
bb7a0f54 438 if (*PL_reglastparen + 1 <= rex->nparens) {
c3464db5 439 PerlIO_printf(Perl_debug_log,
faccc32b 440 " restoring \\%"IVdf"..\\%"IVdf" to undef\n",
4f639d21 441 (IV)(*PL_reglastparen + 1), (IV)rex->nparens);
c277df42
IZ
442 }
443 );
daf18116 444#if 1
dafc8851
JH
445 /* It would seem that the similar code in regtry()
446 * already takes care of this, and in fact it is in
447 * a better location to since this code can #if 0-ed out
448 * but the code in regtry() is needed or otherwise tests
449 * requiring null fields (pat.t#187 and split.t#{13,14}
daf18116
JH
450 * (as of patchlevel 7877) will fail. Then again,
451 * this code seems to be necessary or otherwise
225593e1
DM
452 * this erroneously leaves $1 defined: "1" =~ /^(?:(\d)x)?\d$/
453 * --jhi updated by dapm */
3b6647e0 454 for (i = *PL_reglastparen + 1; i <= rex->nparens; i++) {
097eb12c 455 if (i > PL_regsize)
f0ab9afb
NC
456 PL_regoffs[i].start = -1;
457 PL_regoffs[i].end = -1;
a0d0e21e 458 }
dafc8851 459#endif
a0d0e21e
LW
460 return input;
461}
462
02db2b7b 463#define regcpblow(cp) LEAVE_SCOPE(cp) /* Ignores regcppush()ed data. */
a0d0e21e 464
a687059c 465/*
e50aee73 466 * pregexec and friends
a687059c
LW
467 */
468
76234dfb 469#ifndef PERL_IN_XSUB_RE
a687059c 470/*
c277df42 471 - pregexec - match a regexp against a string
a687059c 472 */
c277df42 473I32
49d7dfbc 474Perl_pregexec(pTHX_ REGEXP * const prog, char* stringarg, register char *strend,
c3464db5 475 char *strbeg, I32 minend, SV *screamer, U32 nosave)
c277df42
IZ
476/* strend: pointer to null at end of string */
477/* strbeg: real beginning of string */
478/* minend: end of match must be >=minend after stringarg. */
479/* nosave: For optimizations. */
480{
7918f24d
NC
481 PERL_ARGS_ASSERT_PREGEXEC;
482
c277df42 483 return
9041c2e3 484 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
c277df42
IZ
485 nosave ? 0 : REXEC_COPY_STR);
486}
76234dfb 487#endif
22e551b9 488
9041c2e3 489/*
cad2e5aa
JH
490 * Need to implement the following flags for reg_anch:
491 *
492 * USE_INTUIT_NOML - Useful to call re_intuit_start() first
493 * USE_INTUIT_ML
494 * INTUIT_AUTORITATIVE_NOML - Can trust a positive answer
495 * INTUIT_AUTORITATIVE_ML
496 * INTUIT_ONCE_NOML - Intuit can match in one location only.
497 * INTUIT_ONCE_ML
498 *
499 * Another flag for this function: SECOND_TIME (so that float substrs
500 * with giant delta may be not rechecked).
501 */
502
503/* Assumptions: if ANCH_GPOS, then strpos is anchored. XXXX Check GPOS logic */
504
3f7c398e 505/* If SCREAM, then SvPVX_const(sv) should be compatible with strpos and strend.
cad2e5aa
JH
506 Otherwise, only SvCUR(sv) is used to get strbeg. */
507
508/* XXXX We assume that strpos is strbeg unless sv. */
509
6eb5f6b9
JH
510/* XXXX Some places assume that there is a fixed substring.
511 An update may be needed if optimizer marks as "INTUITable"
512 RExen without fixed substrings. Similarly, it is assumed that
513 lengths of all the strings are no more than minlen, thus they
514 cannot come from lookahead.
40d049e4
YO
515 (Or minlen should take into account lookahead.)
516 NOTE: Some of this comment is not correct. minlen does now take account
517 of lookahead/behind. Further research is required. -- demerphq
518
519*/
6eb5f6b9 520
2c2d71f5
JH
521/* A failure to find a constant substring means that there is no need to make
522 an expensive call to REx engine, thus we celebrate a failure. Similarly,
523 finding a substring too deep into the string means that less calls to
30944b6d
IZ
524 regtry() should be needed.
525
526 REx compiler's optimizer found 4 possible hints:
527 a) Anchored substring;
528 b) Fixed substring;
529 c) Whether we are anchored (beginning-of-line or \G);
530 d) First node (of those at offset 0) which may distingush positions;
6eb5f6b9 531 We use a)b)d) and multiline-part of c), and try to find a position in the
30944b6d
IZ
532 string which does not contradict any of them.
533 */
2c2d71f5 534
6eb5f6b9
JH
535/* Most of decisions we do here should have been done at compile time.
536 The nodes of the REx which we used for the search should have been
537 deleted from the finite automaton. */
538
cad2e5aa 539char *
288b8c02 540Perl_re_intuit_start(pTHX_ REGEXP * const rx, SV *sv, char *strpos,
9f61653a 541 char *strend, const U32 flags, re_scream_pos_data *data)
cad2e5aa 542{
97aff369 543 dVAR;
288b8c02 544 struct regexp *const prog = (struct regexp *)SvANY(rx);
b7953727 545 register I32 start_shift = 0;
cad2e5aa 546 /* Should be nonnegative! */
b7953727 547 register I32 end_shift = 0;
2c2d71f5
JH
548 register char *s;
549 register SV *check;
a1933d95 550 char *strbeg;
cad2e5aa 551 char *t;
f2ed9b32 552 const bool utf8_target = (sv && SvUTF8(sv)) ? 1 : 0; /* if no sv we have to assume bytes */
cad2e5aa 553 I32 ml_anch;
bd61b366
SS
554 register char *other_last = NULL; /* other substr checked before this */
555 char *check_at = NULL; /* check substr found at this pos */
bbe252da 556 const I32 multiline = prog->extflags & RXf_PMf_MULTILINE;
f8fc2ecf 557 RXi_GET_DECL(prog,progi);
30944b6d 558#ifdef DEBUGGING
890ce7af 559 const char * const i_strpos = strpos;
30944b6d 560#endif
a3621e74
YO
561 GET_RE_DEBUG_FLAGS_DECL;
562
7918f24d
NC
563 PERL_ARGS_ASSERT_RE_INTUIT_START;
564
f2ed9b32 565 RX_MATCH_UTF8_set(rx,utf8_target);
cad2e5aa 566
3c8556c3 567 if (RX_UTF8(rx)) {
b8d68ded
JH
568 PL_reg_flags |= RF_utf8;
569 }
ab3bbdeb 570 DEBUG_EXECUTE_r(
f2ed9b32 571 debug_start_match(rx, utf8_target, strpos, strend,
1de06328
YO
572 sv ? "Guessing start of match in sv for"
573 : "Guessing start of match in string for");
2a782b5b 574 );
cad2e5aa 575
c344f387
JH
576 /* CHR_DIST() would be more correct here but it makes things slow. */
577 if (prog->minlen > strend - strpos) {
a3621e74 578 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
a72c7584 579 "String too short... [re_intuit_start]\n"));
cad2e5aa 580 goto fail;
2c2d71f5 581 }
1de06328 582
a1933d95 583 strbeg = (sv && SvPOK(sv)) ? strend - SvCUR(sv) : strpos;
1aa99e6b 584 PL_regeol = strend;
f2ed9b32 585 if (utf8_target) {
33b8afdf
JH
586 if (!prog->check_utf8 && prog->check_substr)
587 to_utf8_substr(prog);
588 check = prog->check_utf8;
589 } else {
590 if (!prog->check_substr && prog->check_utf8)
591 to_byte_substr(prog);
592 check = prog->check_substr;
593 }
1de06328 594 if (check == &PL_sv_undef) {
a3621e74 595 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1de06328 596 "Non-utf8 string cannot match utf8 check string\n"));
33b8afdf
JH
597 goto fail;
598 }
bbe252da
YO
599 if (prog->extflags & RXf_ANCH) { /* Match at beg-of-str or after \n */
600 ml_anch = !( (prog->extflags & RXf_ANCH_SINGLE)
601 || ( (prog->extflags & RXf_ANCH_BOL)
7fba1cd6 602 && !multiline ) ); /* Check after \n? */
cad2e5aa 603
7e25d62c 604 if (!ml_anch) {
bbe252da
YO
605 if ( !(prog->extflags & RXf_ANCH_GPOS) /* Checked by the caller */
606 && !(prog->intflags & PREGf_IMPLICIT) /* not a real BOL */
3f7c398e 607 /* SvCUR is not set on references: SvRV and SvPVX_const overlap */
7e25d62c
JH
608 && sv && !SvROK(sv)
609 && (strpos != strbeg)) {
a3621e74 610 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Not at start...\n"));
7e25d62c
JH
611 goto fail;
612 }
613 if (prog->check_offset_min == prog->check_offset_max &&
bbe252da 614 !(prog->extflags & RXf_CANY_SEEN)) {
2c2d71f5 615 /* Substring at constant offset from beg-of-str... */
cad2e5aa
JH
616 I32 slen;
617
1aa99e6b 618 s = HOP3c(strpos, prog->check_offset_min, strend);
1de06328 619
653099ff
GS
620 if (SvTAIL(check)) {
621 slen = SvCUR(check); /* >= 1 */
cad2e5aa 622
9041c2e3 623 if ( strend - s > slen || strend - s < slen - 1
2c2d71f5 624 || (strend - s == slen && strend[-1] != '\n')) {
a3621e74 625 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "String too long...\n"));
2c2d71f5 626 goto fail_finish;
cad2e5aa
JH
627 }
628 /* Now should match s[0..slen-2] */
629 slen--;
3f7c398e 630 if (slen && (*SvPVX_const(check) != *s
cad2e5aa 631 || (slen > 1
3f7c398e 632 && memNE(SvPVX_const(check), s, slen)))) {
2c2d71f5 633 report_neq:
a3621e74 634 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "String not equal...\n"));
2c2d71f5
JH
635 goto fail_finish;
636 }
cad2e5aa 637 }
3f7c398e 638 else if (*SvPVX_const(check) != *s
653099ff 639 || ((slen = SvCUR(check)) > 1
3f7c398e 640 && memNE(SvPVX_const(check), s, slen)))
2c2d71f5 641 goto report_neq;
c315bfe8 642 check_at = s;
2c2d71f5 643 goto success_at_start;
7e25d62c 644 }
cad2e5aa 645 }
2c2d71f5 646 /* Match is anchored, but substr is not anchored wrt beg-of-str. */
cad2e5aa 647 s = strpos;
2c2d71f5 648 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
1de06328
YO
649 end_shift = prog->check_end_shift;
650
2c2d71f5 651 if (!ml_anch) {
a3b680e6 652 const I32 end = prog->check_offset_max + CHR_SVLEN(check)
653099ff 653 - (SvTAIL(check) != 0);
a3b680e6 654 const I32 eshift = CHR_DIST((U8*)strend, (U8*)s) - end;
2c2d71f5
JH
655
656 if (end_shift < eshift)
657 end_shift = eshift;
658 }
cad2e5aa 659 }
2c2d71f5 660 else { /* Can match at random position */
cad2e5aa
JH
661 ml_anch = 0;
662 s = strpos;
1de06328
YO
663 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
664 end_shift = prog->check_end_shift;
665
666 /* end shift should be non negative here */
cad2e5aa
JH
667 }
668
bcdf7404 669#ifdef QDEBUGGING /* 7/99: reports of failure (with the older version) */
0033605d 670 if (end_shift < 0)
1de06328 671 Perl_croak(aTHX_ "panic: end_shift: %"IVdf" pattern:\n%s\n ",
220fc49f 672 (IV)end_shift, RX_PRECOMP(prog));
2c2d71f5
JH
673#endif
674
2c2d71f5
JH
675 restart:
676 /* Find a possible match in the region s..strend by looking for
677 the "check" substring in the region corrected by start/end_shift. */
1de06328
YO
678
679 {
680 I32 srch_start_shift = start_shift;
681 I32 srch_end_shift = end_shift;
682 if (srch_start_shift < 0 && strbeg - s > srch_start_shift) {
683 srch_end_shift -= ((strbeg - s) - srch_start_shift);
684 srch_start_shift = strbeg - s;
685 }
6bda09f9 686 DEBUG_OPTIMISE_MORE_r({
1de06328
YO
687 PerlIO_printf(Perl_debug_log, "Check offset min: %"IVdf" Start shift: %"IVdf" End shift %"IVdf" Real End Shift: %"IVdf"\n",
688 (IV)prog->check_offset_min,
689 (IV)srch_start_shift,
690 (IV)srch_end_shift,
691 (IV)prog->check_end_shift);
692 });
693
cad2e5aa 694 if (flags & REXEC_SCREAM) {
cad2e5aa 695 I32 p = -1; /* Internal iterator of scream. */
a3b680e6 696 I32 * const pp = data ? data->scream_pos : &p;
cad2e5aa 697
2c2d71f5
JH
698 if (PL_screamfirst[BmRARE(check)] >= 0
699 || ( BmRARE(check) == '\n'
85c508c3 700 && (BmPREVIOUS(check) == SvCUR(check) - 1)
2c2d71f5 701 && SvTAIL(check) ))
9041c2e3 702 s = screaminstr(sv, check,
1de06328 703 srch_start_shift + (s - strbeg), srch_end_shift, pp, 0);
cad2e5aa 704 else
2c2d71f5 705 goto fail_finish;
4addbd3b 706 /* we may be pointing at the wrong string */
07bc277f 707 if (s && RXp_MATCH_COPIED(prog))
3f7c398e 708 s = strbeg + (s - SvPVX_const(sv));
cad2e5aa
JH
709 if (data)
710 *data->scream_olds = s;
711 }
1de06328
YO
712 else {
713 U8* start_point;
714 U8* end_point;
bbe252da 715 if (prog->extflags & RXf_CANY_SEEN) {
1de06328
YO
716 start_point= (U8*)(s + srch_start_shift);
717 end_point= (U8*)(strend - srch_end_shift);
718 } else {
719 start_point= HOP3(s, srch_start_shift, srch_start_shift < 0 ? strbeg : strend);
720 end_point= HOP3(strend, -srch_end_shift, strbeg);
721 }
6bda09f9 722 DEBUG_OPTIMISE_MORE_r({
56570a2c 723 PerlIO_printf(Perl_debug_log, "fbm_instr len=%d str=<%.*s>\n",
1de06328 724 (int)(end_point - start_point),
fc8cd66c 725 (int)(end_point - start_point) > 20 ? 20 : (int)(end_point - start_point),
1de06328
YO
726 start_point);
727 });
728
729 s = fbm_instr( start_point, end_point,
7fba1cd6 730 check, multiline ? FBMrf_MULTILINE : 0);
1de06328
YO
731 }
732 }
cad2e5aa
JH
733 /* Update the count-of-usability, remove useless subpatterns,
734 unshift s. */
2c2d71f5 735
ab3bbdeb 736 DEBUG_EXECUTE_r({
f2ed9b32 737 RE_PV_QUOTED_DECL(quoted, utf8_target, PERL_DEBUG_PAD_ZERO(0),
ab3bbdeb
YO
738 SvPVX_const(check), RE_SV_DUMPLEN(check), 30);
739 PerlIO_printf(Perl_debug_log, "%s %s substr %s%s%s",
2c2d71f5 740 (s ? "Found" : "Did not find"),
f2ed9b32 741 (check == (utf8_target ? prog->anchored_utf8 : prog->anchored_substr)
ab3bbdeb
YO
742 ? "anchored" : "floating"),
743 quoted,
744 RE_SV_TAIL(check),
745 (s ? " at offset " : "...\n") );
746 });
2c2d71f5
JH
747
748 if (!s)
749 goto fail_finish;
2c2d71f5 750 /* Finish the diagnostic message */
a3621e74 751 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%ld...\n", (long)(s - i_strpos)) );
2c2d71f5 752
1de06328
YO
753 /* XXX dmq: first branch is for positive lookbehind...
754 Our check string is offset from the beginning of the pattern.
755 So we need to do any stclass tests offset forward from that
756 point. I think. :-(
757 */
758
759
760
761 check_at=s;
762
763
2c2d71f5
JH
764 /* Got a candidate. Check MBOL anchoring, and the *other* substr.
765 Start with the other substr.
766 XXXX no SCREAM optimization yet - and a very coarse implementation
a0288114 767 XXXX /ttx+/ results in anchored="ttx", floating="x". floating will
2c2d71f5
JH
768 *always* match. Probably should be marked during compile...
769 Probably it is right to do no SCREAM here...
770 */
771
f2ed9b32 772 if (utf8_target ? (prog->float_utf8 && prog->anchored_utf8)
1de06328
YO
773 : (prog->float_substr && prog->anchored_substr))
774 {
30944b6d 775 /* Take into account the "other" substring. */
2c2d71f5
JH
776 /* XXXX May be hopelessly wrong for UTF... */
777 if (!other_last)
6eb5f6b9 778 other_last = strpos;
f2ed9b32 779 if (check == (utf8_target ? prog->float_utf8 : prog->float_substr)) {
30944b6d
IZ
780 do_other_anchored:
781 {
890ce7af
AL
782 char * const last = HOP3c(s, -start_shift, strbeg);
783 char *last1, *last2;
be8e71aa 784 char * const saved_s = s;
33b8afdf 785 SV* must;
2c2d71f5 786
2c2d71f5
JH
787 t = s - prog->check_offset_max;
788 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
f2ed9b32 789 && (!utf8_target
0ce71af7 790 || ((t = (char*)reghopmaybe3((U8*)s, -(prog->check_offset_max), (U8*)strpos))
2c2d71f5 791 && t > strpos)))
6f207bd3 792 NOOP;
2c2d71f5
JH
793 else
794 t = strpos;
1aa99e6b 795 t = HOP3c(t, prog->anchored_offset, strend);
6eb5f6b9
JH
796 if (t < other_last) /* These positions already checked */
797 t = other_last;
1aa99e6b 798 last2 = last1 = HOP3c(strend, -prog->minlen, strbeg);
2c2d71f5
JH
799 if (last < last1)
800 last1 = last;
1de06328
YO
801 /* XXXX It is not documented what units *_offsets are in.
802 We assume bytes, but this is clearly wrong.
803 Meaning this code needs to be carefully reviewed for errors.
804 dmq.
805 */
806
2c2d71f5 807 /* On end-of-str: see comment below. */
f2ed9b32 808 must = utf8_target ? prog->anchored_utf8 : prog->anchored_substr;
33b8afdf
JH
809 if (must == &PL_sv_undef) {
810 s = (char*)NULL;
1de06328 811 DEBUG_r(must = prog->anchored_utf8); /* for debug */
33b8afdf
JH
812 }
813 else
814 s = fbm_instr(
815 (unsigned char*)t,
816 HOP3(HOP3(last1, prog->anchored_offset, strend)
817 + SvCUR(must), -(SvTAIL(must)!=0), strbeg),
818 must,
7fba1cd6 819 multiline ? FBMrf_MULTILINE : 0
33b8afdf 820 );
ab3bbdeb 821 DEBUG_EXECUTE_r({
f2ed9b32 822 RE_PV_QUOTED_DECL(quoted, utf8_target, PERL_DEBUG_PAD_ZERO(0),
ab3bbdeb
YO
823 SvPVX_const(must), RE_SV_DUMPLEN(must), 30);
824 PerlIO_printf(Perl_debug_log, "%s anchored substr %s%s",
2c2d71f5 825 (s ? "Found" : "Contradicts"),
ab3bbdeb
YO
826 quoted, RE_SV_TAIL(must));
827 });
828
829
2c2d71f5
JH
830 if (!s) {
831 if (last1 >= last2) {
a3621e74 832 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
2c2d71f5
JH
833 ", giving up...\n"));
834 goto fail_finish;
835 }
a3621e74 836 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
2c2d71f5 837 ", trying floating at offset %ld...\n",
be8e71aa 838 (long)(HOP3c(saved_s, 1, strend) - i_strpos)));
1aa99e6b
IH
839 other_last = HOP3c(last1, prog->anchored_offset+1, strend);
840 s = HOP3c(last, 1, strend);
2c2d71f5
JH
841 goto restart;
842 }
843 else {
a3621e74 844 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
30944b6d 845 (long)(s - i_strpos)));
1aa99e6b
IH
846 t = HOP3c(s, -prog->anchored_offset, strbeg);
847 other_last = HOP3c(s, 1, strend);
be8e71aa 848 s = saved_s;
2c2d71f5
JH
849 if (t == strpos)
850 goto try_at_start;
2c2d71f5
JH
851 goto try_at_offset;
852 }
30944b6d 853 }
2c2d71f5
JH
854 }
855 else { /* Take into account the floating substring. */
33b8afdf 856 char *last, *last1;
be8e71aa 857 char * const saved_s = s;
33b8afdf
JH
858 SV* must;
859
860 t = HOP3c(s, -start_shift, strbeg);
861 last1 = last =
862 HOP3c(strend, -prog->minlen + prog->float_min_offset, strbeg);
863 if (CHR_DIST((U8*)last, (U8*)t) > prog->float_max_offset)
864 last = HOP3c(t, prog->float_max_offset, strend);
865 s = HOP3c(t, prog->float_min_offset, strend);
866 if (s < other_last)
867 s = other_last;
2c2d71f5 868 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
f2ed9b32 869 must = utf8_target ? prog->float_utf8 : prog->float_substr;
33b8afdf
JH
870 /* fbm_instr() takes into account exact value of end-of-str
871 if the check is SvTAIL(ed). Since false positives are OK,
872 and end-of-str is not later than strend we are OK. */
873 if (must == &PL_sv_undef) {
874 s = (char*)NULL;
1de06328 875 DEBUG_r(must = prog->float_utf8); /* for debug message */
33b8afdf
JH
876 }
877 else
2c2d71f5 878 s = fbm_instr((unsigned char*)s,
33b8afdf
JH
879 (unsigned char*)last + SvCUR(must)
880 - (SvTAIL(must)!=0),
7fba1cd6 881 must, multiline ? FBMrf_MULTILINE : 0);
ab3bbdeb 882 DEBUG_EXECUTE_r({
f2ed9b32 883 RE_PV_QUOTED_DECL(quoted, utf8_target, PERL_DEBUG_PAD_ZERO(0),
ab3bbdeb
YO
884 SvPVX_const(must), RE_SV_DUMPLEN(must), 30);
885 PerlIO_printf(Perl_debug_log, "%s floating substr %s%s",
33b8afdf 886 (s ? "Found" : "Contradicts"),
ab3bbdeb
YO
887 quoted, RE_SV_TAIL(must));
888 });
33b8afdf
JH
889 if (!s) {
890 if (last1 == last) {
a3621e74 891 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
33b8afdf
JH
892 ", giving up...\n"));
893 goto fail_finish;
2c2d71f5 894 }
a3621e74 895 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
33b8afdf 896 ", trying anchored starting at offset %ld...\n",
be8e71aa 897 (long)(saved_s + 1 - i_strpos)));
33b8afdf
JH
898 other_last = last;
899 s = HOP3c(t, 1, strend);
900 goto restart;
901 }
902 else {
a3621e74 903 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
33b8afdf
JH
904 (long)(s - i_strpos)));
905 other_last = s; /* Fix this later. --Hugo */
be8e71aa 906 s = saved_s;
33b8afdf
JH
907 if (t == strpos)
908 goto try_at_start;
909 goto try_at_offset;
910 }
2c2d71f5 911 }
cad2e5aa 912 }
2c2d71f5 913
1de06328 914
9ef43ace 915 t= (char*)HOP3( s, -prog->check_offset_max, (prog->check_offset_max<0) ? strend : strpos);
1de06328 916
6bda09f9 917 DEBUG_OPTIMISE_MORE_r(
1de06328
YO
918 PerlIO_printf(Perl_debug_log,
919 "Check offset min:%"IVdf" max:%"IVdf" S:%"IVdf" t:%"IVdf" D:%"IVdf" end:%"IVdf"\n",
920 (IV)prog->check_offset_min,
921 (IV)prog->check_offset_max,
922 (IV)(s-strpos),
923 (IV)(t-strpos),
924 (IV)(t-s),
925 (IV)(strend-strpos)
926 )
927 );
928
2c2d71f5 929 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
f2ed9b32 930 && (!utf8_target
9ef43ace 931 || ((t = (char*)reghopmaybe3((U8*)s, -prog->check_offset_max, (U8*) ((prog->check_offset_max<0) ? strend : strpos)))
1de06328
YO
932 && t > strpos)))
933 {
2c2d71f5
JH
934 /* Fixed substring is found far enough so that the match
935 cannot start at strpos. */
936 try_at_offset:
cad2e5aa 937 if (ml_anch && t[-1] != '\n') {
30944b6d
IZ
938 /* Eventually fbm_*() should handle this, but often
939 anchored_offset is not 0, so this check will not be wasted. */
940 /* XXXX In the code below we prefer to look for "^" even in
941 presence of anchored substrings. And we search even
942 beyond the found float position. These pessimizations
943 are historical artefacts only. */
944 find_anchor:
2c2d71f5 945 while (t < strend - prog->minlen) {
cad2e5aa 946 if (*t == '\n') {
4ee3650e 947 if (t < check_at - prog->check_offset_min) {
f2ed9b32 948 if (utf8_target ? prog->anchored_utf8 : prog->anchored_substr) {
4ee3650e
GS
949 /* Since we moved from the found position,
950 we definitely contradict the found anchored
30944b6d
IZ
951 substr. Due to the above check we do not
952 contradict "check" substr.
953 Thus we can arrive here only if check substr
954 is float. Redo checking for "other"=="fixed".
955 */
9041c2e3 956 strpos = t + 1;
a3621e74 957 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld, rescanning for anchored from offset %ld...\n",
e4584336 958 PL_colors[0], PL_colors[1], (long)(strpos - i_strpos), (long)(strpos - i_strpos + prog->anchored_offset)));
30944b6d
IZ
959 goto do_other_anchored;
960 }
4ee3650e
GS
961 /* We don't contradict the found floating substring. */
962 /* XXXX Why not check for STCLASS? */
cad2e5aa 963 s = t + 1;
a3621e74 964 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld...\n",
e4584336 965 PL_colors[0], PL_colors[1], (long)(s - i_strpos)));
cad2e5aa
JH
966 goto set_useful;
967 }
4ee3650e
GS
968 /* Position contradicts check-string */
969 /* XXXX probably better to look for check-string
970 than for "\n", so one should lower the limit for t? */
a3621e74 971 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m, restarting lookup for check-string at offset %ld...\n",
e4584336 972 PL_colors[0], PL_colors[1], (long)(t + 1 - i_strpos)));
0e41cd87 973 other_last = strpos = s = t + 1;
cad2e5aa
JH
974 goto restart;
975 }
976 t++;
977 }
a3621e74 978 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Did not find /%s^%s/m...\n",
e4584336 979 PL_colors[0], PL_colors[1]));
2c2d71f5 980 goto fail_finish;
cad2e5aa 981 }
f5952150 982 else {
a3621e74 983 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Starting position does not contradict /%s^%s/m...\n",
e4584336 984 PL_colors[0], PL_colors[1]));
f5952150 985 }
cad2e5aa
JH
986 s = t;
987 set_useful:
f2ed9b32 988 ++BmUSEFUL(utf8_target ? prog->check_utf8 : prog->check_substr); /* hooray/5 */
cad2e5aa
JH
989 }
990 else {
f5952150 991 /* The found string does not prohibit matching at strpos,
2c2d71f5 992 - no optimization of calling REx engine can be performed,
f5952150
GS
993 unless it was an MBOL and we are not after MBOL,
994 or a future STCLASS check will fail this. */
2c2d71f5
JH
995 try_at_start:
996 /* Even in this situation we may use MBOL flag if strpos is offset
997 wrt the start of the string. */
05b4157f 998 if (ml_anch && sv && !SvROK(sv) /* See prev comment on SvROK */
a1933d95 999 && (strpos != strbeg) && strpos[-1] != '\n'
d506a20d 1000 /* May be due to an implicit anchor of m{.*foo} */
bbe252da 1001 && !(prog->intflags & PREGf_IMPLICIT))
d506a20d 1002 {
cad2e5aa
JH
1003 t = strpos;
1004 goto find_anchor;
1005 }
a3621e74 1006 DEBUG_EXECUTE_r( if (ml_anch)
f5952150 1007 PerlIO_printf(Perl_debug_log, "Position at offset %ld does not contradict /%s^%s/m...\n",
70685ca0 1008 (long)(strpos - i_strpos), PL_colors[0], PL_colors[1]);
30944b6d 1009 );
2c2d71f5 1010 success_at_start:
bbe252da 1011 if (!(prog->intflags & PREGf_NAUGHTY) /* XXXX If strpos moved? */
f2ed9b32 1012 && (utf8_target ? (
33b8afdf
JH
1013 prog->check_utf8 /* Could be deleted already */
1014 && --BmUSEFUL(prog->check_utf8) < 0
1015 && (prog->check_utf8 == prog->float_utf8)
1016 ) : (
1017 prog->check_substr /* Could be deleted already */
1018 && --BmUSEFUL(prog->check_substr) < 0
1019 && (prog->check_substr == prog->float_substr)
1020 )))
66e933ab 1021 {
cad2e5aa 1022 /* If flags & SOMETHING - do not do it many times on the same match */
a3621e74 1023 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "... Disabling check substring...\n"));
f2ed9b32
KW
1024 /* XXX Does the destruction order has to change with utf8_target? */
1025 SvREFCNT_dec(utf8_target ? prog->check_utf8 : prog->check_substr);
1026 SvREFCNT_dec(utf8_target ? prog->check_substr : prog->check_utf8);
a0714e2c
SS
1027 prog->check_substr = prog->check_utf8 = NULL; /* disable */
1028 prog->float_substr = prog->float_utf8 = NULL; /* clear */
1029 check = NULL; /* abort */
cad2e5aa 1030 s = strpos;
c9415951
YO
1031 /* XXXX If the check string was an implicit check MBOL, then we need to unset the relevent flag
1032 see http://bugs.activestate.com/show_bug.cgi?id=87173 */
1033 if (prog->intflags & PREGf_IMPLICIT)
1034 prog->extflags &= ~RXf_ANCH_MBOL;
3cf5c195
IZ
1035 /* XXXX This is a remnant of the old implementation. It
1036 looks wasteful, since now INTUIT can use many
6eb5f6b9 1037 other heuristics. */
bbe252da 1038 prog->extflags &= ~RXf_USE_INTUIT;
c9415951 1039 /* XXXX What other flags might need to be cleared in this branch? */
cad2e5aa
JH
1040 }
1041 else
1042 s = strpos;
1043 }
1044
6eb5f6b9
JH
1045 /* Last resort... */
1046 /* XXXX BmUSEFUL already changed, maybe multiple change is meaningful... */
1de06328
YO
1047 /* trie stclasses are too expensive to use here, we are better off to
1048 leave it to regmatch itself */
f8fc2ecf 1049 if (progi->regstclass && PL_regkind[OP(progi->regstclass)]!=TRIE) {
6eb5f6b9
JH
1050 /* minlen == 0 is possible if regstclass is \b or \B,
1051 and the fixed substr is ''$.
1052 Since minlen is already taken into account, s+1 is before strend;
1053 accidentally, minlen >= 1 guaranties no false positives at s + 1
1054 even for \b or \B. But (minlen? 1 : 0) below assumes that
1055 regstclass does not come from lookahead... */
1056 /* If regstclass takes bytelength more than 1: If charlength==1, OK.
1057 This leaves EXACTF only, which is dealt with in find_byclass(). */
f8fc2ecf
YO
1058 const U8* const str = (U8*)STRING(progi->regstclass);
1059 const int cl_l = (PL_regkind[OP(progi->regstclass)] == EXACT
1060 ? CHR_DIST(str+STR_LEN(progi->regstclass), str)
66e933ab 1061 : 1);
1de06328
YO
1062 char * endpos;
1063 if (prog->anchored_substr || prog->anchored_utf8 || ml_anch)
1064 endpos= HOP3c(s, (prog->minlen ? cl_l : 0), strend);
1065 else if (prog->float_substr || prog->float_utf8)
1066 endpos= HOP3c(HOP3c(check_at, -start_shift, strbeg), cl_l, strend);
1067 else
1068 endpos= strend;
1069
70685ca0
JH
1070 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "start_shift: %"IVdf" check_at: %"IVdf" s: %"IVdf" endpos: %"IVdf"\n",
1071 (IV)start_shift, (IV)(check_at - strbeg), (IV)(s - strbeg), (IV)(endpos - strbeg)));
1de06328 1072
6eb5f6b9 1073 t = s;
f8fc2ecf 1074 s = find_byclass(prog, progi->regstclass, s, endpos, NULL);
6eb5f6b9
JH
1075 if (!s) {
1076#ifdef DEBUGGING
cbbf8932 1077 const char *what = NULL;
6eb5f6b9
JH
1078#endif
1079 if (endpos == strend) {
a3621e74 1080 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
6eb5f6b9
JH
1081 "Could not match STCLASS...\n") );
1082 goto fail;
1083 }
a3621e74 1084 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
66e933ab 1085 "This position contradicts STCLASS...\n") );
bbe252da 1086 if ((prog->extflags & RXf_ANCH) && !ml_anch)
653099ff 1087 goto fail;
6eb5f6b9 1088 /* Contradict one of substrings */
33b8afdf 1089 if (prog->anchored_substr || prog->anchored_utf8) {
f2ed9b32 1090 if ((utf8_target ? prog->anchored_utf8 : prog->anchored_substr) == check) {
a3621e74 1091 DEBUG_EXECUTE_r( what = "anchored" );
6eb5f6b9 1092 hop_and_restart:
1aa99e6b 1093 s = HOP3c(t, 1, strend);
66e933ab
GS
1094 if (s + start_shift + end_shift > strend) {
1095 /* XXXX Should be taken into account earlier? */
a3621e74 1096 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
66e933ab
GS
1097 "Could not match STCLASS...\n") );
1098 goto fail;
1099 }
5e39e1e5
HS
1100 if (!check)
1101 goto giveup;
a3621e74 1102 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
f5952150 1103 "Looking for %s substr starting at offset %ld...\n",
6eb5f6b9
JH
1104 what, (long)(s + start_shift - i_strpos)) );
1105 goto restart;
1106 }
66e933ab 1107 /* Have both, check_string is floating */
6eb5f6b9
JH
1108 if (t + start_shift >= check_at) /* Contradicts floating=check */
1109 goto retry_floating_check;
1110 /* Recheck anchored substring, but not floating... */
9041c2e3 1111 s = check_at;
5e39e1e5
HS
1112 if (!check)
1113 goto giveup;
a3621e74 1114 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
f5952150 1115 "Looking for anchored substr starting at offset %ld...\n",
6eb5f6b9
JH
1116 (long)(other_last - i_strpos)) );
1117 goto do_other_anchored;
1118 }
60e71179
GS
1119 /* Another way we could have checked stclass at the
1120 current position only: */
1121 if (ml_anch) {
1122 s = t = t + 1;
5e39e1e5
HS
1123 if (!check)
1124 goto giveup;
a3621e74 1125 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
f5952150 1126 "Looking for /%s^%s/m starting at offset %ld...\n",
e4584336 1127 PL_colors[0], PL_colors[1], (long)(t - i_strpos)) );
60e71179 1128 goto try_at_offset;
66e933ab 1129 }
f2ed9b32 1130 if (!(utf8_target ? prog->float_utf8 : prog->float_substr)) /* Could have been deleted */
60e71179 1131 goto fail;
6eb5f6b9
JH
1132 /* Check is floating subtring. */
1133 retry_floating_check:
1134 t = check_at - start_shift;
a3621e74 1135 DEBUG_EXECUTE_r( what = "floating" );
6eb5f6b9
JH
1136 goto hop_and_restart;
1137 }
b7953727 1138 if (t != s) {
a3621e74 1139 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
6eb5f6b9 1140 "By STCLASS: moving %ld --> %ld\n",
b7953727
JH
1141 (long)(t - i_strpos), (long)(s - i_strpos))
1142 );
1143 }
1144 else {
a3621e74 1145 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
b7953727
JH
1146 "Does not contradict STCLASS...\n");
1147 );
1148 }
6eb5f6b9 1149 }
5e39e1e5 1150 giveup:
a3621e74 1151 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%s%s:%s match at offset %ld\n",
5e39e1e5
HS
1152 PL_colors[4], (check ? "Guessed" : "Giving up"),
1153 PL_colors[5], (long)(s - i_strpos)) );
cad2e5aa 1154 return s;
2c2d71f5
JH
1155
1156 fail_finish: /* Substring not found */
33b8afdf 1157 if (prog->check_substr || prog->check_utf8) /* could be removed already */
f2ed9b32 1158 BmUSEFUL(utf8_target ? prog->check_utf8 : prog->check_substr) += 5; /* hooray */
cad2e5aa 1159 fail:
a3621e74 1160 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch rejected by optimizer%s\n",
e4584336 1161 PL_colors[4], PL_colors[5]));
bd61b366 1162 return NULL;
cad2e5aa 1163}
9661b544 1164
a0a388a1
YO
1165#define DECL_TRIE_TYPE(scan) \
1166 const enum { trie_plain, trie_utf8, trie_utf8_fold, trie_latin_utf8_fold } \
1167 trie_type = (scan->flags != EXACT) \
f2ed9b32
KW
1168 ? (utf8_target ? trie_utf8_fold : (UTF_PATTERN ? trie_latin_utf8_fold : trie_plain)) \
1169 : (utf8_target ? trie_utf8 : trie_plain)
3b0527fe 1170
55eed653
NC
1171#define REXEC_TRIE_READ_CHAR(trie_type, trie, widecharmap, uc, uscan, len, \
1172uvc, charid, foldlen, foldbuf, uniflags) STMT_START { \
4cadc6a9
YO
1173 switch (trie_type) { \
1174 case trie_utf8_fold: \
1175 if ( foldlen>0 ) { \
0abd0d78 1176 uvc = utf8n_to_uvuni( uscan, UTF8_MAXLEN, &len, uniflags ); \
4cadc6a9
YO
1177 foldlen -= len; \
1178 uscan += len; \
1179 len=0; \
1180 } else { \
0abd0d78 1181 uvc = utf8n_to_uvuni( (U8*)uc, UTF8_MAXLEN, &len, uniflags ); \
4cadc6a9
YO
1182 uvc = to_uni_fold( uvc, foldbuf, &foldlen ); \
1183 foldlen -= UNISKIP( uvc ); \
1184 uscan = foldbuf + UNISKIP( uvc ); \
1185 } \
1186 break; \
a0a388a1
YO
1187 case trie_latin_utf8_fold: \
1188 if ( foldlen>0 ) { \
1189 uvc = utf8n_to_uvuni( uscan, UTF8_MAXLEN, &len, uniflags ); \
1190 foldlen -= len; \
1191 uscan += len; \
1192 len=0; \
1193 } else { \
1194 len = 1; \
1195 uvc = to_uni_fold( *(U8*)uc, foldbuf, &foldlen ); \
1196 foldlen -= UNISKIP( uvc ); \
1197 uscan = foldbuf + UNISKIP( uvc ); \
1198 } \
1199 break; \
4cadc6a9
YO
1200 case trie_utf8: \
1201 uvc = utf8n_to_uvuni( (U8*)uc, UTF8_MAXLEN, &len, uniflags ); \
1202 break; \
1203 case trie_plain: \
1204 uvc = (UV)*uc; \
1205 len = 1; \
1206 } \
4cadc6a9
YO
1207 if (uvc < 256) { \
1208 charid = trie->charmap[ uvc ]; \
1209 } \
1210 else { \
1211 charid = 0; \
55eed653
NC
1212 if (widecharmap) { \
1213 SV** const svpp = hv_fetch(widecharmap, \
4cadc6a9
YO
1214 (char*)&uvc, sizeof(UV), 0); \
1215 if (svpp) \
1216 charid = (U16)SvIV(*svpp); \
1217 } \
1218 } \
1219} STMT_END
1220
a0a388a1
YO
1221#define REXEC_FBC_EXACTISH_CHECK(CoNd) \
1222{ \
1223 char *my_strend= (char *)strend; \
4cadc6a9
YO
1224 if ( (CoNd) \
1225 && (ln == len || \
f2ed9b32
KW
1226 foldEQ_utf8(s, &my_strend, 0, utf8_target, \
1227 m, NULL, ln, cBOOL(UTF_PATTERN))) \
a0a388a1 1228 && (!reginfo || regtry(reginfo, &s)) ) \
4cadc6a9
YO
1229 goto got_it; \
1230 else { \
1231 U8 foldbuf[UTF8_MAXBYTES_CASE+1]; \
1232 uvchr_to_utf8(tmpbuf, c); \
1233 f = to_utf8_fold(tmpbuf, foldbuf, &foldlen); \
1234 if ( f != c \
1235 && (f == c1 || f == c2) \
a0a388a1 1236 && (ln == len || \
f2ed9b32
KW
1237 foldEQ_utf8(s, &my_strend, 0, utf8_target,\
1238 m, NULL, ln, cBOOL(UTF_PATTERN)))\
a0a388a1 1239 && (!reginfo || regtry(reginfo, &s)) ) \
4cadc6a9
YO
1240 goto got_it; \
1241 } \
a0a388a1
YO
1242} \
1243s += len
4cadc6a9
YO
1244
1245#define REXEC_FBC_EXACTISH_SCAN(CoNd) \
1246STMT_START { \
1247 while (s <= e) { \
1248 if ( (CoNd) \
4c1b470c
KW
1249 && (ln == 1 || (OP(c) == EXACTF \
1250 ? foldEQ(s, m, ln) \
1251 : foldEQ_locale(s, m, ln))) \
24b23f37 1252 && (!reginfo || regtry(reginfo, &s)) ) \
4cadc6a9
YO
1253 goto got_it; \
1254 s++; \
1255 } \
1256} STMT_END
1257
1258#define REXEC_FBC_UTF8_SCAN(CoDe) \
1259STMT_START { \
1260 while (s + (uskip = UTF8SKIP(s)) <= strend) { \
1261 CoDe \
1262 s += uskip; \
1263 } \
1264} STMT_END
1265
1266#define REXEC_FBC_SCAN(CoDe) \
1267STMT_START { \
1268 while (s < strend) { \
1269 CoDe \
1270 s++; \
1271 } \
1272} STMT_END
1273
1274#define REXEC_FBC_UTF8_CLASS_SCAN(CoNd) \
1275REXEC_FBC_UTF8_SCAN( \
1276 if (CoNd) { \
24b23f37 1277 if (tmp && (!reginfo || regtry(reginfo, &s))) \
4cadc6a9
YO
1278 goto got_it; \
1279 else \
1280 tmp = doevery; \
1281 } \
1282 else \
1283 tmp = 1; \
1284)
1285
1286#define REXEC_FBC_CLASS_SCAN(CoNd) \
1287REXEC_FBC_SCAN( \
1288 if (CoNd) { \
24b23f37 1289 if (tmp && (!reginfo || regtry(reginfo, &s))) \
4cadc6a9
YO
1290 goto got_it; \
1291 else \
1292 tmp = doevery; \
1293 } \
1294 else \
1295 tmp = 1; \
1296)
1297
1298#define REXEC_FBC_TRYIT \
24b23f37 1299if ((!reginfo || regtry(reginfo, &s))) \
4cadc6a9
YO
1300 goto got_it
1301
e1d1eefb 1302#define REXEC_FBC_CSCAN(CoNdUtF8,CoNd) \
f2ed9b32 1303 if (utf8_target) { \
e1d1eefb
YO
1304 REXEC_FBC_UTF8_CLASS_SCAN(CoNdUtF8); \
1305 } \
1306 else { \
1307 REXEC_FBC_CLASS_SCAN(CoNd); \
1308 } \
1309 break
1310
4cadc6a9 1311#define REXEC_FBC_CSCAN_PRELOAD(UtFpReLoAd,CoNdUtF8,CoNd) \
f2ed9b32 1312 if (utf8_target) { \
4cadc6a9
YO
1313 UtFpReLoAd; \
1314 REXEC_FBC_UTF8_CLASS_SCAN(CoNdUtF8); \
1315 } \
1316 else { \
1317 REXEC_FBC_CLASS_SCAN(CoNd); \
1318 } \
1319 break
1320
1321#define REXEC_FBC_CSCAN_TAINT(CoNdUtF8,CoNd) \
1322 PL_reg_flags |= RF_tainted; \
f2ed9b32 1323 if (utf8_target) { \
4cadc6a9
YO
1324 REXEC_FBC_UTF8_CLASS_SCAN(CoNdUtF8); \
1325 } \
1326 else { \
1327 REXEC_FBC_CLASS_SCAN(CoNd); \
1328 } \
1329 break
1330
786e8c11
YO
1331#define DUMP_EXEC_POS(li,s,doutf8) \
1332 dump_exec_pos(li,s,(PL_regeol),(PL_bostr),(PL_reg_starttry),doutf8)
1333
1334/* We know what class REx starts with. Try to find this position... */
1335/* if reginfo is NULL, its a dryrun */
1336/* annoyingly all the vars in this routine have different names from their counterparts
1337 in regmatch. /grrr */
1338
3c3eec57 1339STATIC char *
07be1b83 1340S_find_byclass(pTHX_ regexp * prog, const regnode *c, char *s,
24b23f37 1341 const char *strend, regmatch_info *reginfo)
a687059c 1342{
27da23d5 1343 dVAR;
bbe252da 1344 const I32 doevery = (prog->intflags & PREGf_SKIP) == 0;
6eb5f6b9 1345 char *m;
d8093b23 1346 STRLEN ln;
5dab1207 1347 STRLEN lnc;
078c425b 1348 register STRLEN uskip;
d8093b23
G
1349 unsigned int c1;
1350 unsigned int c2;
6eb5f6b9
JH
1351 char *e;
1352 register I32 tmp = 1; /* Scratch variable? */
f2ed9b32 1353 register const bool utf8_target = PL_reg_match_utf8;
f8fc2ecf 1354 RXi_GET_DECL(prog,progi);
7918f24d
NC
1355
1356 PERL_ARGS_ASSERT_FIND_BYCLASS;
f8fc2ecf 1357
6eb5f6b9
JH
1358 /* We know what class it must start with. */
1359 switch (OP(c)) {
6eb5f6b9 1360 case ANYOF:
f2ed9b32 1361 if (utf8_target) {
4cadc6a9 1362 REXEC_FBC_UTF8_CLASS_SCAN((ANYOF_FLAGS(c) & ANYOF_UNICODE) ||
388cc4de 1363 !UTF8_IS_INVARIANT((U8)s[0]) ?
f2ed9b32 1364 reginclass(prog, c, (U8*)s, 0, utf8_target) :
4cadc6a9 1365 REGINCLASS(prog, c, (U8*)s));
388cc4de
HS
1366 }
1367 else {
1368 while (s < strend) {
1369 STRLEN skip = 1;
1370
32fc9b6a 1371 if (REGINCLASS(prog, c, (U8*)s) ||
388cc4de
HS
1372 (ANYOF_FOLD_SHARP_S(c, s, strend) &&
1373 /* The assignment of 2 is intentional:
1374 * for the folded sharp s, the skip is 2. */
1375 (skip = SHARP_S_SKIP))) {
24b23f37 1376 if (tmp && (!reginfo || regtry(reginfo, &s)))
388cc4de
HS
1377 goto got_it;
1378 else
1379 tmp = doevery;
1380 }
1381 else
1382 tmp = 1;
1383 s += skip;
1384 }
a0d0e21e 1385 }
6eb5f6b9 1386 break;
f33976b4 1387 case CANY:
4cadc6a9 1388 REXEC_FBC_SCAN(
24b23f37 1389 if (tmp && (!reginfo || regtry(reginfo, &s)))
f33976b4
DB
1390 goto got_it;
1391 else
1392 tmp = doevery;
4cadc6a9 1393 );
f33976b4 1394 break;
6eb5f6b9 1395 case EXACTF:
5dab1207
NIS
1396 m = STRING(c);
1397 ln = STR_LEN(c); /* length to match in octets/bytes */
1398 lnc = (I32) ln; /* length to match in characters */
f2ed9b32 1399 if (UTF_PATTERN) {
a2a2844f 1400 STRLEN ulen1, ulen2;
5dab1207 1401 U8 *sm = (U8 *) m;
89ebb4a3
JH
1402 U8 tmpbuf1[UTF8_MAXBYTES_CASE+1];
1403 U8 tmpbuf2[UTF8_MAXBYTES_CASE+1];
97dc7d3e
RGS
1404 /* used by commented-out code below */
1405 /*const U32 uniflags = UTF8_ALLOW_DEFAULT;*/
a0a388a1
YO
1406
1407 /* XXX: Since the node will be case folded at compile
1408 time this logic is a little odd, although im not
1409 sure that its actually wrong. --dmq */
1410
1411 c1 = to_utf8_lower((U8*)m, tmpbuf1, &ulen1);
1412 c2 = to_utf8_upper((U8*)m, tmpbuf2, &ulen2);
1413
1414 /* XXX: This is kinda strange. to_utf8_XYZ returns the
1415 codepoint of the first character in the converted
1416 form, yet originally we did the extra step.
1417 No tests fail by commenting this code out however
1418 so Ive left it out. -- dmq.
1419
89ebb4a3 1420 c1 = utf8n_to_uvchr(tmpbuf1, UTF8_MAXBYTES_CASE,
041457d9 1421 0, uniflags);
89ebb4a3 1422 c2 = utf8n_to_uvchr(tmpbuf2, UTF8_MAXBYTES_CASE,
041457d9 1423 0, uniflags);
a0a388a1
YO
1424 */
1425
5dab1207
NIS
1426 lnc = 0;
1427 while (sm < ((U8 *) m + ln)) {
1428 lnc++;
1429 sm += UTF8SKIP(sm);
1430 }
1aa99e6b
IH
1431 }
1432 else {
1433 c1 = *(U8*)m;
1434 c2 = PL_fold[c1];
1435 }
6eb5f6b9
JH
1436 goto do_exactf;
1437 case EXACTFL:
5dab1207
NIS
1438 m = STRING(c);
1439 ln = STR_LEN(c);
1440 lnc = (I32) ln;
d8093b23 1441 c1 = *(U8*)m;
6eb5f6b9
JH
1442 c2 = PL_fold_locale[c1];
1443 do_exactf:
db12adc6 1444 e = HOP3c(strend, -((I32)lnc), s);
b3c9acc1 1445
3b0527fe 1446 if (!reginfo && e < s)
6eb5f6b9 1447 e = s; /* Due to minlen logic of intuit() */
1aa99e6b 1448
60a8b682
JH
1449 /* The idea in the EXACTF* cases is to first find the
1450 * first character of the EXACTF* node and then, if
1451 * necessary, case-insensitively compare the full
1452 * text of the node. The c1 and c2 are the first
1453 * characters (though in Unicode it gets a bit
1454 * more complicated because there are more cases
7f16dd3d
JH
1455 * than just upper and lower: one needs to use
1456 * the so-called folding case for case-insensitive
1457 * matching (called "loose matching" in Unicode).
4c1b470c 1458 * foldEQ_utf8() will do just that. */
60a8b682 1459
f2ed9b32 1460 if (utf8_target || UTF_PATTERN) {
575cac57 1461 UV c, f;
89ebb4a3 1462 U8 tmpbuf [UTF8_MAXBYTES+1];
a0a388a1
YO
1463 STRLEN len = 1;
1464 STRLEN foldlen;
4ad0818d 1465 const U32 uniflags = UTF8_ALLOW_DEFAULT;
09091399 1466 if (c1 == c2) {
5dab1207
NIS
1467 /* Upper and lower of 1st char are equal -
1468 * probably not a "letter". */
1aa99e6b 1469 while (s <= e) {
f2ed9b32 1470 if (utf8_target) {
a0a388a1 1471 c = utf8n_to_uvchr((U8*)s, UTF8_MAXBYTES, &len,
041457d9 1472 uniflags);
a0a388a1
YO
1473 } else {
1474 c = *((U8*)s);
1475 }
4cadc6a9 1476 REXEC_FBC_EXACTISH_CHECK(c == c1);
1aa99e6b 1477 }
09091399
JH
1478 }
1479 else {
1aa99e6b 1480 while (s <= e) {
f2ed9b32 1481 if (utf8_target) {
a0a388a1 1482 c = utf8n_to_uvchr((U8*)s, UTF8_MAXBYTES, &len,
041457d9 1483 uniflags);
a0a388a1
YO
1484 } else {
1485 c = *((U8*)s);
1486 }
80aecb99 1487
60a8b682 1488 /* Handle some of the three Greek sigmas cases.
8c01da3c
JH
1489 * Note that not all the possible combinations
1490 * are handled here: some of them are handled
1491 * by the standard folding rules, and some of
1492 * them (the character class or ANYOF cases)
1493 * are handled during compiletime in
1494 * regexec.c:S_regclass(). */
880bd946
JH
1495 if (c == (UV)UNICODE_GREEK_CAPITAL_LETTER_SIGMA ||
1496 c == (UV)UNICODE_GREEK_SMALL_LETTER_FINAL_SIGMA)
1497 c = (UV)UNICODE_GREEK_SMALL_LETTER_SIGMA;
80aecb99 1498
4cadc6a9 1499 REXEC_FBC_EXACTISH_CHECK(c == c1 || c == c2);
1aa99e6b 1500 }
09091399 1501 }
1aa99e6b
IH
1502 }
1503 else {
a0a388a1 1504 /* Neither pattern nor string are UTF8 */
1aa99e6b 1505 if (c1 == c2)
4cadc6a9 1506 REXEC_FBC_EXACTISH_SCAN(*(U8*)s == c1);
1aa99e6b 1507 else
4cadc6a9 1508 REXEC_FBC_EXACTISH_SCAN(*(U8*)s == c1 || *(U8*)s == c2);
b3c9acc1
IZ
1509 }
1510 break;
bbce6d69 1511 case BOUNDL:
3280af22 1512 PL_reg_flags |= RF_tainted;
bbce6d69 1513 /* FALL THROUGH */
a0d0e21e 1514 case BOUND:
f2ed9b32 1515 if (utf8_target) {
12d33761 1516 if (s == PL_bostr)
ffc61ed2
JH
1517 tmp = '\n';
1518 else {
6136c704 1519 U8 * const r = reghop3((U8*)s, -1, (U8*)PL_bostr);
4ad0818d 1520 tmp = utf8n_to_uvchr(r, UTF8SKIP(r), 0, UTF8_ALLOW_DEFAULT);
ffc61ed2
JH
1521 }
1522 tmp = ((OP(c) == BOUND ?
9041c2e3 1523 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1a4fad37 1524 LOAD_UTF8_CHARCLASS_ALNUM();
4cadc6a9 1525 REXEC_FBC_UTF8_SCAN(
ffc61ed2 1526 if (tmp == !(OP(c) == BOUND ?
f2ed9b32 1527 cBOOL(swash_fetch(PL_utf8_alnum, (U8*)s, utf8_target)) :
ffc61ed2
JH
1528 isALNUM_LC_utf8((U8*)s)))
1529 {
1530 tmp = !tmp;
4cadc6a9 1531 REXEC_FBC_TRYIT;
a687059c 1532 }
4cadc6a9 1533 );
a0d0e21e 1534 }
a12cf05f 1535 else { /* Not utf8 */
12d33761 1536 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
a12cf05f
KW
1537 tmp = cBOOL((OP(c) == BOUNDL)
1538 ? isALNUM_LC(tmp)
1539 : (isWORDCHAR_L1(tmp)
1540 && (isASCII(tmp) || (FLAGS(c) & USE_UNI))));
4cadc6a9 1541 REXEC_FBC_SCAN(
ffc61ed2 1542 if (tmp ==
a12cf05f
KW
1543 !((OP(c) == BOUNDL)
1544 ? isALNUM_LC(*s)
1545 : (isWORDCHAR_L1((U8) *s)
1546 && (isASCII((U8) *s) || (FLAGS(c) & USE_UNI)))))
1547 {
ffc61ed2 1548 tmp = !tmp;
4cadc6a9 1549 REXEC_FBC_TRYIT;
a0ed51b3 1550 }
4cadc6a9 1551 );
a0ed51b3 1552 }
24b23f37 1553 if ((!prog->minlen && tmp) && (!reginfo || regtry(reginfo, &s)))
a0ed51b3
LW
1554 goto got_it;
1555 break;
bbce6d69 1556 case NBOUNDL:
3280af22 1557 PL_reg_flags |= RF_tainted;
bbce6d69 1558 /* FALL THROUGH */
a0d0e21e 1559 case NBOUND:
f2ed9b32 1560 if (utf8_target) {
12d33761 1561 if (s == PL_bostr)
ffc61ed2
JH
1562 tmp = '\n';
1563 else {
6136c704 1564 U8 * const r = reghop3((U8*)s, -1, (U8*)PL_bostr);
4ad0818d 1565 tmp = utf8n_to_uvchr(r, UTF8SKIP(r), 0, UTF8_ALLOW_DEFAULT);
ffc61ed2
JH
1566 }
1567 tmp = ((OP(c) == NBOUND ?
9041c2e3 1568 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1a4fad37 1569 LOAD_UTF8_CHARCLASS_ALNUM();
4cadc6a9 1570 REXEC_FBC_UTF8_SCAN(
ffc61ed2 1571 if (tmp == !(OP(c) == NBOUND ?
f2ed9b32 1572 cBOOL(swash_fetch(PL_utf8_alnum, (U8*)s, utf8_target)) :
ffc61ed2
JH
1573 isALNUM_LC_utf8((U8*)s)))
1574 tmp = !tmp;
4cadc6a9
YO
1575 else REXEC_FBC_TRYIT;
1576 );
a0d0e21e 1577 }
667bb95a 1578 else {
12d33761 1579 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
a12cf05f
KW
1580 tmp = cBOOL((OP(c) == NBOUNDL)
1581 ? isALNUM_LC(tmp)
1582 : (isWORDCHAR_L1(tmp)
1583 && (isASCII(tmp) || (FLAGS(c) & USE_UNI))));
4cadc6a9 1584 REXEC_FBC_SCAN(
a12cf05f
KW
1585 if (tmp == ! cBOOL(
1586 (OP(c) == NBOUNDL)
1587 ? isALNUM_LC(*s)
1588 : (isWORDCHAR_L1((U8) *s)
1589 && (isASCII((U8) *s) || (FLAGS(c) & USE_UNI)))))
1590 {
ffc61ed2 1591 tmp = !tmp;
a12cf05f 1592 }
4cadc6a9
YO
1593 else REXEC_FBC_TRYIT;
1594 );
a0ed51b3 1595 }
24b23f37 1596 if ((!prog->minlen && !tmp) && (!reginfo || regtry(reginfo, &s)))
a0ed51b3
LW
1597 goto got_it;
1598 break;
a0d0e21e 1599 case ALNUM:
4cadc6a9 1600 REXEC_FBC_CSCAN_PRELOAD(
d1eb3177 1601 LOAD_UTF8_CHARCLASS_PERL_WORD(),
f2ed9b32 1602 swash_fetch(RE_utf8_perl_word, (U8*)s, utf8_target),
a12cf05f 1603 (FLAGS(c) & USE_UNI) ? isWORDCHAR_L1((U8) *s) : isALNUM(*s)
4cadc6a9 1604 );
bbce6d69 1605 case ALNUML:
4cadc6a9
YO
1606 REXEC_FBC_CSCAN_TAINT(
1607 isALNUM_LC_utf8((U8*)s),
1608 isALNUM_LC(*s)
1609 );
a0d0e21e 1610 case NALNUM:
4cadc6a9 1611 REXEC_FBC_CSCAN_PRELOAD(
d1eb3177 1612 LOAD_UTF8_CHARCLASS_PERL_WORD(),
f2ed9b32 1613 !swash_fetch(RE_utf8_perl_word, (U8*)s, utf8_target),
a12cf05f 1614 ! ((FLAGS(c) & USE_UNI) ? isWORDCHAR_L1((U8) *s) : isALNUM(*s))
4cadc6a9 1615 );
bbce6d69 1616 case NALNUML:
4cadc6a9
YO
1617 REXEC_FBC_CSCAN_TAINT(
1618 !isALNUM_LC_utf8((U8*)s),
1619 !isALNUM_LC(*s)
1620 );
a0d0e21e 1621 case SPACE:
4cadc6a9 1622 REXEC_FBC_CSCAN_PRELOAD(
d1eb3177 1623 LOAD_UTF8_CHARCLASS_PERL_SPACE(),
f2ed9b32 1624 *s == ' ' || swash_fetch(RE_utf8_perl_space,(U8*)s, utf8_target),
a12cf05f 1625 isSPACE_L1((U8) *s) && (isASCII((U8) *s) || (FLAGS(c) & USE_UNI))
4cadc6a9 1626 );
bbce6d69 1627 case SPACEL:
4cadc6a9
YO
1628 REXEC_FBC_CSCAN_TAINT(
1629 *s == ' ' || isSPACE_LC_utf8((U8*)s),
1630 isSPACE_LC(*s)
1631 );
a0d0e21e 1632 case NSPACE:
4cadc6a9 1633 REXEC_FBC_CSCAN_PRELOAD(
d1eb3177 1634 LOAD_UTF8_CHARCLASS_PERL_SPACE(),
f2ed9b32 1635 !(*s == ' ' || swash_fetch(RE_utf8_perl_space,(U8*)s, utf8_target)),
a12cf05f 1636 !(isSPACE_L1((U8) *s) && (isASCII((U8) *s) || (FLAGS(c) & USE_UNI)))
4cadc6a9 1637 );
bbce6d69 1638 case NSPACEL:
4cadc6a9
YO
1639 REXEC_FBC_CSCAN_TAINT(
1640 !(*s == ' ' || isSPACE_LC_utf8((U8*)s)),
1641 !isSPACE_LC(*s)
1642 );
a0d0e21e 1643 case DIGIT:
4cadc6a9 1644 REXEC_FBC_CSCAN_PRELOAD(
d1eb3177 1645 LOAD_UTF8_CHARCLASS_POSIX_DIGIT(),
f2ed9b32 1646 swash_fetch(RE_utf8_posix_digit,(U8*)s, utf8_target),
4cadc6a9
YO
1647 isDIGIT(*s)
1648 );
b8c5462f 1649 case DIGITL:
4cadc6a9
YO
1650 REXEC_FBC_CSCAN_TAINT(
1651 isDIGIT_LC_utf8((U8*)s),
1652 isDIGIT_LC(*s)
1653 );
a0d0e21e 1654 case NDIGIT:
4cadc6a9 1655 REXEC_FBC_CSCAN_PRELOAD(
d1eb3177 1656 LOAD_UTF8_CHARCLASS_POSIX_DIGIT(),
f2ed9b32 1657 !swash_fetch(RE_utf8_posix_digit,(U8*)s, utf8_target),
4cadc6a9
YO
1658 !isDIGIT(*s)
1659 );
b8c5462f 1660 case NDIGITL:
4cadc6a9
YO
1661 REXEC_FBC_CSCAN_TAINT(
1662 !isDIGIT_LC_utf8((U8*)s),
1663 !isDIGIT_LC(*s)
1664 );
e1d1eefb
YO
1665 case LNBREAK:
1666 REXEC_FBC_CSCAN(
1667 is_LNBREAK_utf8(s),
1668 is_LNBREAK_latin1(s)
1669 );
1670 case VERTWS:
1671 REXEC_FBC_CSCAN(
1672 is_VERTWS_utf8(s),
1673 is_VERTWS_latin1(s)
1674 );
1675 case NVERTWS:
1676 REXEC_FBC_CSCAN(
1677 !is_VERTWS_utf8(s),
1678 !is_VERTWS_latin1(s)
1679 );
1680 case HORIZWS:
1681 REXEC_FBC_CSCAN(
1682 is_HORIZWS_utf8(s),
1683 is_HORIZWS_latin1(s)
1684 );
1685 case NHORIZWS:
1686 REXEC_FBC_CSCAN(
1687 !is_HORIZWS_utf8(s),
1688 !is_HORIZWS_latin1(s)
1689 );
1de06328
YO
1690 case AHOCORASICKC:
1691 case AHOCORASICK:
07be1b83 1692 {
a0a388a1 1693 DECL_TRIE_TYPE(c);
07be1b83
YO
1694 /* what trie are we using right now */
1695 reg_ac_data *aho
f8fc2ecf 1696 = (reg_ac_data*)progi->data->data[ ARG( c ) ];
3251b653
NC
1697 reg_trie_data *trie
1698 = (reg_trie_data*)progi->data->data[ aho->trie ];
85fbaab2 1699 HV *widecharmap = MUTABLE_HV(progi->data->data[ aho->trie + 1 ]);
07be1b83
YO
1700
1701 const char *last_start = strend - trie->minlen;
6148ee25 1702#ifdef DEBUGGING
07be1b83 1703 const char *real_start = s;
6148ee25 1704#endif
07be1b83 1705 STRLEN maxlen = trie->maxlen;
be8e71aa
YO
1706 SV *sv_points;
1707 U8 **points; /* map of where we were in the input string
786e8c11 1708 when reading a given char. For ASCII this
be8e71aa 1709 is unnecessary overhead as the relationship
38a44b82
NC
1710 is always 1:1, but for Unicode, especially
1711 case folded Unicode this is not true. */
f9e705e8 1712 U8 foldbuf[ UTF8_MAXBYTES_CASE + 1 ];
786e8c11
YO
1713 U8 *bitmap=NULL;
1714
07be1b83
YO
1715
1716 GET_RE_DEBUG_FLAGS_DECL;
1717
be8e71aa
YO
1718 /* We can't just allocate points here. We need to wrap it in
1719 * an SV so it gets freed properly if there is a croak while
1720 * running the match */
1721 ENTER;
1722 SAVETMPS;
1723 sv_points=newSV(maxlen * sizeof(U8 *));
1724 SvCUR_set(sv_points,
1725 maxlen * sizeof(U8 *));
1726 SvPOK_on(sv_points);
1727 sv_2mortal(sv_points);
1728 points=(U8**)SvPV_nolen(sv_points );
1de06328
YO
1729 if ( trie_type != trie_utf8_fold
1730 && (trie->bitmap || OP(c)==AHOCORASICKC) )
1731 {
786e8c11
YO
1732 if (trie->bitmap)
1733 bitmap=(U8*)trie->bitmap;
1734 else
1735 bitmap=(U8*)ANYOF_BITMAP(c);
07be1b83 1736 }
786e8c11
YO
1737 /* this is the Aho-Corasick algorithm modified a touch
1738 to include special handling for long "unknown char"
1739 sequences. The basic idea being that we use AC as long
1740 as we are dealing with a possible matching char, when
1741 we encounter an unknown char (and we have not encountered
1742 an accepting state) we scan forward until we find a legal
1743 starting char.
1744 AC matching is basically that of trie matching, except
1745 that when we encounter a failing transition, we fall back
1746 to the current states "fail state", and try the current char
1747 again, a process we repeat until we reach the root state,
1748 state 1, or a legal transition. If we fail on the root state
1749 then we can either terminate if we have reached an accepting
1750 state previously, or restart the entire process from the beginning
1751 if we have not.
1752
1753 */
07be1b83
YO
1754 while (s <= last_start) {
1755 const U32 uniflags = UTF8_ALLOW_DEFAULT;
1756 U8 *uc = (U8*)s;
1757 U16 charid = 0;
1758 U32 base = 1;
1759 U32 state = 1;
1760 UV uvc = 0;
1761 STRLEN len = 0;
1762 STRLEN foldlen = 0;
1763 U8 *uscan = (U8*)NULL;
1764 U8 *leftmost = NULL;
786e8c11
YO
1765#ifdef DEBUGGING
1766 U32 accepted_word= 0;
1767#endif
07be1b83
YO
1768 U32 pointpos = 0;
1769
1770 while ( state && uc <= (U8*)strend ) {
1771 int failed=0;
786e8c11
YO
1772 U32 word = aho->states[ state ].wordnum;
1773
1de06328
YO
1774 if( state==1 ) {
1775 if ( bitmap ) {
1776 DEBUG_TRIE_EXECUTE_r(
1777 if ( uc <= (U8*)last_start && !BITMAP_TEST(bitmap,*uc) ) {
1778 dump_exec_pos( (char *)uc, c, strend, real_start,
f2ed9b32 1779 (char *)uc, utf8_target );
1de06328
YO
1780 PerlIO_printf( Perl_debug_log,
1781 " Scanning for legal start char...\n");
1782 }
1783 );
1784 while ( uc <= (U8*)last_start && !BITMAP_TEST(bitmap,*uc) ) {
1785 uc++;
786e8c11 1786 }
1de06328 1787 s= (char *)uc;
786e8c11 1788 }
786e8c11
YO
1789 if (uc >(U8*)last_start) break;
1790 }
1791
1792 if ( word ) {
2e64971a 1793 U8 *lpos= points[ (pointpos - trie->wordinfo[word].len) % maxlen ];
786e8c11
YO
1794 if (!leftmost || lpos < leftmost) {
1795 DEBUG_r(accepted_word=word);
07be1b83 1796 leftmost= lpos;
786e8c11 1797 }
07be1b83 1798 if (base==0) break;
786e8c11 1799
07be1b83
YO
1800 }
1801 points[pointpos++ % maxlen]= uc;
55eed653
NC
1802 REXEC_TRIE_READ_CHAR(trie_type, trie, widecharmap, uc,
1803 uscan, len, uvc, charid, foldlen,
1804 foldbuf, uniflags);
786e8c11
YO
1805 DEBUG_TRIE_EXECUTE_r({
1806 dump_exec_pos( (char *)uc, c, strend, real_start,
f2ed9b32 1807 s, utf8_target );
07be1b83 1808 PerlIO_printf(Perl_debug_log,
786e8c11
YO
1809 " Charid:%3u CP:%4"UVxf" ",
1810 charid, uvc);
1811 });
07be1b83
YO
1812
1813 do {
6148ee25 1814#ifdef DEBUGGING
786e8c11 1815 word = aho->states[ state ].wordnum;
6148ee25 1816#endif
07be1b83
YO
1817 base = aho->states[ state ].trans.base;
1818
786e8c11
YO
1819 DEBUG_TRIE_EXECUTE_r({
1820 if (failed)
1821 dump_exec_pos( (char *)uc, c, strend, real_start,
f2ed9b32 1822 s, utf8_target );
07be1b83 1823 PerlIO_printf( Perl_debug_log,
786e8c11
YO
1824 "%sState: %4"UVxf", word=%"UVxf,
1825 failed ? " Fail transition to " : "",
1826 (UV)state, (UV)word);
1827 });
07be1b83
YO
1828 if ( base ) {
1829 U32 tmp;
6dd2be57 1830 I32 offset;
07be1b83 1831 if (charid &&
6dd2be57
DM
1832 ( ((offset = base + charid
1833 - 1 - trie->uniquecharcount)) >= 0)
1834 && ((U32)offset < trie->lasttrans)
1835 && trie->trans[offset].check == state
1836 && (tmp=trie->trans[offset].next))
07be1b83 1837 {
786e8c11
YO
1838 DEBUG_TRIE_EXECUTE_r(
1839 PerlIO_printf( Perl_debug_log," - legal\n"));
07be1b83
YO
1840 state = tmp;
1841 break;
1842 }
1843 else {
786e8c11
YO
1844 DEBUG_TRIE_EXECUTE_r(
1845 PerlIO_printf( Perl_debug_log," - fail\n"));
1846 failed = 1;
1847 state = aho->fail[state];
07be1b83
YO
1848 }
1849 }
1850 else {
1851 /* we must be accepting here */
786e8c11
YO
1852 DEBUG_TRIE_EXECUTE_r(
1853 PerlIO_printf( Perl_debug_log," - accepting\n"));
1854 failed = 1;
07be1b83
YO
1855 break;
1856 }
1857 } while(state);
786e8c11 1858 uc += len;
07be1b83
YO
1859 if (failed) {
1860 if (leftmost)
1861 break;
786e8c11 1862 if (!state) state = 1;
07be1b83
YO
1863 }
1864 }
1865 if ( aho->states[ state ].wordnum ) {
2e64971a 1866 U8 *lpos = points[ (pointpos - trie->wordinfo[aho->states[ state ].wordnum].len) % maxlen ];
786e8c11
YO
1867 if (!leftmost || lpos < leftmost) {
1868 DEBUG_r(accepted_word=aho->states[ state ].wordnum);
07be1b83 1869 leftmost = lpos;
786e8c11 1870 }
07be1b83 1871 }
07be1b83
YO
1872 if (leftmost) {
1873 s = (char*)leftmost;
786e8c11
YO
1874 DEBUG_TRIE_EXECUTE_r({
1875 PerlIO_printf(
70685ca0
JH
1876 Perl_debug_log,"Matches word #%"UVxf" at position %"IVdf". Trying full pattern...\n",
1877 (UV)accepted_word, (IV)(s - real_start)
786e8c11
YO
1878 );
1879 });
24b23f37 1880 if (!reginfo || regtry(reginfo, &s)) {
be8e71aa
YO
1881 FREETMPS;
1882 LEAVE;
07be1b83 1883 goto got_it;
be8e71aa 1884 }
07be1b83 1885 s = HOPc(s,1);
786e8c11
YO
1886 DEBUG_TRIE_EXECUTE_r({
1887 PerlIO_printf( Perl_debug_log,"Pattern failed. Looking for new start point...\n");
1888 });
07be1b83 1889 } else {
786e8c11
YO
1890 DEBUG_TRIE_EXECUTE_r(
1891 PerlIO_printf( Perl_debug_log,"No match.\n"));
07be1b83
YO
1892 break;
1893 }
1894 }
be8e71aa
YO
1895 FREETMPS;
1896 LEAVE;
07be1b83
YO
1897 }
1898 break;
b3c9acc1 1899 default:
3c3eec57
GS
1900 Perl_croak(aTHX_ "panic: unknown regstclass %d", (int)OP(c));
1901 break;
d6a28714 1902 }
6eb5f6b9
JH
1903 return 0;
1904 got_it:
1905 return s;
1906}
1907
fae667d5 1908
6eb5f6b9
JH
1909/*
1910 - regexec_flags - match a regexp against a string
1911 */
1912I32
288b8c02 1913Perl_regexec_flags(pTHX_ REGEXP * const rx, char *stringarg, register char *strend,
6eb5f6b9
JH
1914 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
1915/* strend: pointer to null at end of string */
1916/* strbeg: real beginning of string */
1917/* minend: end of match must be >=minend after stringarg. */
58e23c8d
YO
1918/* data: May be used for some additional optimizations.
1919 Currently its only used, with a U32 cast, for transmitting
1920 the ganch offset when doing a /g match. This will change */
6eb5f6b9
JH
1921/* nosave: For optimizations. */
1922{
97aff369 1923 dVAR;
288b8c02 1924 struct regexp *const prog = (struct regexp *)SvANY(rx);
24b23f37 1925 /*register*/ char *s;
6eb5f6b9 1926 register regnode *c;
24b23f37 1927 /*register*/ char *startpos = stringarg;
6eb5f6b9
JH
1928 I32 minlen; /* must match at least this many chars */
1929 I32 dontbother = 0; /* how many characters not to try at end */
6eb5f6b9
JH
1930 I32 end_shift = 0; /* Same for the end. */ /* CC */
1931 I32 scream_pos = -1; /* Internal iterator of scream. */
ccac19ea 1932 char *scream_olds = NULL;
f2ed9b32 1933 const bool utf8_target = cBOOL(DO_UTF8(sv));
2757e526 1934 I32 multiline;
f8fc2ecf 1935 RXi_GET_DECL(prog,progi);
3b0527fe 1936 regmatch_info reginfo; /* create some info to pass to regtry etc */
e9105d30 1937 regexp_paren_pair *swap = NULL;
a3621e74
YO
1938 GET_RE_DEBUG_FLAGS_DECL;
1939
7918f24d 1940 PERL_ARGS_ASSERT_REGEXEC_FLAGS;
9d4ba2ae 1941 PERL_UNUSED_ARG(data);
6eb5f6b9
JH
1942
1943 /* Be paranoid... */
1944 if (prog == NULL || startpos == NULL) {
1945 Perl_croak(aTHX_ "NULL regexp parameter");
1946 return 0;
1947 }
1948
bbe252da 1949 multiline = prog->extflags & RXf_PMf_MULTILINE;
288b8c02 1950 reginfo.prog = rx; /* Yes, sorry that this is confusing. */
2757e526 1951
f2ed9b32 1952 RX_MATCH_UTF8_set(rx, utf8_target);
1de06328 1953 DEBUG_EXECUTE_r(
f2ed9b32 1954 debug_start_match(rx, utf8_target, startpos, strend,
1de06328
YO
1955 "Matching");
1956 );
bac06658 1957
6eb5f6b9 1958 minlen = prog->minlen;
1de06328
YO
1959
1960 if (strend - startpos < (minlen+(prog->check_offset_min<0?prog->check_offset_min:0))) {
a3621e74 1961 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
a72c7584
JH
1962 "String too short [regexec_flags]...\n"));
1963 goto phooey;
1aa99e6b 1964 }
6eb5f6b9 1965
1de06328 1966
6eb5f6b9 1967 /* Check validity of program. */
f8fc2ecf 1968 if (UCHARAT(progi->program) != REG_MAGIC) {
6eb5f6b9
JH
1969 Perl_croak(aTHX_ "corrupted regexp program");
1970 }
1971
1972 PL_reg_flags = 0;
1973 PL_reg_eval_set = 0;
1974 PL_reg_maxiter = 0;
1975
3c8556c3 1976 if (RX_UTF8(rx))
6eb5f6b9
JH
1977 PL_reg_flags |= RF_utf8;
1978
1979 /* Mark beginning of line for ^ and lookbehind. */
3b0527fe 1980 reginfo.bol = startpos; /* XXX not used ??? */
6eb5f6b9 1981 PL_bostr = strbeg;
3b0527fe 1982 reginfo.sv = sv;
6eb5f6b9
JH
1983
1984 /* Mark end of line for $ (and such) */
1985 PL_regeol = strend;
1986
1987 /* see how far we have to get to not match where we matched before */
3b0527fe 1988 reginfo.till = startpos+minend;
6eb5f6b9 1989
6eb5f6b9
JH
1990 /* If there is a "must appear" string, look for it. */
1991 s = startpos;
1992
bbe252da 1993 if (prog->extflags & RXf_GPOS_SEEN) { /* Need to set reginfo->ganch */
6eb5f6b9 1994 MAGIC *mg;
2c296965 1995 if (flags & REXEC_IGNOREPOS){ /* Means: check only at start */
58e23c8d 1996 reginfo.ganch = startpos + prog->gofs;
2c296965 1997 DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log,
ed549f2e 1998 "GPOS IGNOREPOS: reginfo.ganch = startpos + %"UVxf"\n",(UV)prog->gofs));
2c296965 1999 } else if (sv && SvTYPE(sv) >= SVt_PVMG
6eb5f6b9 2000 && SvMAGIC(sv)
14befaf4
DM
2001 && (mg = mg_find(sv, PERL_MAGIC_regex_global))
2002 && mg->mg_len >= 0) {
3b0527fe 2003 reginfo.ganch = strbeg + mg->mg_len; /* Defined pos() */
2c296965 2004 DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log,
ed549f2e 2005 "GPOS MAGIC: reginfo.ganch = strbeg + %"IVdf"\n",(IV)mg->mg_len));
2c296965 2006
bbe252da 2007 if (prog->extflags & RXf_ANCH_GPOS) {
3b0527fe 2008 if (s > reginfo.ganch)
6eb5f6b9 2009 goto phooey;
58e23c8d 2010 s = reginfo.ganch - prog->gofs;
2c296965 2011 DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log,
ed549f2e 2012 "GPOS ANCH_GPOS: s = ganch - %"UVxf"\n",(UV)prog->gofs));
c584a96e
YO
2013 if (s < strbeg)
2014 goto phooey;
6eb5f6b9
JH
2015 }
2016 }
58e23c8d 2017 else if (data) {
70685ca0 2018 reginfo.ganch = strbeg + PTR2UV(data);
2c296965
YO
2019 DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log,
2020 "GPOS DATA: reginfo.ganch= strbeg + %"UVxf"\n",PTR2UV(data)));
2021
2022 } else { /* pos() not defined */
3b0527fe 2023 reginfo.ganch = strbeg;
2c296965
YO
2024 DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log,
2025 "GPOS: reginfo.ganch = strbeg\n"));
2026 }
6eb5f6b9 2027 }
288b8c02 2028 if (PL_curpm && (PM_GETRE(PL_curpm) == rx)) {
e9105d30
GG
2029 /* We have to be careful. If the previous successful match
2030 was from this regex we don't want a subsequent partially
2031 successful match to clobber the old results.
2032 So when we detect this possibility we add a swap buffer
2033 to the re, and switch the buffer each match. If we fail
2034 we switch it back, otherwise we leave it swapped.
2035 */
2036 swap = prog->offs;
2037 /* do we need a save destructor here for eval dies? */
2038 Newxz(prog->offs, (prog->nparens + 1), regexp_paren_pair);
c74340f9 2039 }
a0714e2c 2040 if (!(flags & REXEC_CHECKED) && (prog->check_substr != NULL || prog->check_utf8 != NULL)) {
6eb5f6b9
JH
2041 re_scream_pos_data d;
2042
2043 d.scream_olds = &scream_olds;
2044 d.scream_pos = &scream_pos;
288b8c02 2045 s = re_intuit_start(rx, sv, s, strend, flags, &d);
3fa9c3d7 2046 if (!s) {
a3621e74 2047 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Not present...\n"));
6eb5f6b9 2048 goto phooey; /* not present */
3fa9c3d7 2049 }
6eb5f6b9
JH
2050 }
2051
1de06328 2052
6eb5f6b9
JH
2053
2054 /* Simplest case: anchored match need be tried only once. */
2055 /* [unless only anchor is BOL and multiline is set] */
bbe252da 2056 if (prog->extflags & (RXf_ANCH & ~RXf_ANCH_GPOS)) {
24b23f37 2057 if (s == startpos && regtry(&reginfo, &startpos))
6eb5f6b9 2058 goto got_it;
bbe252da
YO
2059 else if (multiline || (prog->intflags & PREGf_IMPLICIT)
2060 || (prog->extflags & RXf_ANCH_MBOL)) /* XXXX SBOL? */
6eb5f6b9
JH
2061 {
2062 char *end;
2063
2064 if (minlen)
2065 dontbother = minlen - 1;
1aa99e6b 2066 end = HOP3c(strend, -dontbother, strbeg) - 1;
6eb5f6b9 2067 /* for multiline we only have to try after newlines */
33b8afdf 2068 if (prog->check_substr || prog->check_utf8) {
92f3d482
YO
2069 /* because of the goto we can not easily reuse the macros for bifurcating the
2070 unicode/non-unicode match modes here like we do elsewhere - demerphq */
2071 if (utf8_target) {
2072 if (s == startpos)
2073 goto after_try_utf8;
2074 while (1) {
2075 if (regtry(&reginfo, &s)) {
2076 goto got_it;
2077 }
2078 after_try_utf8:
2079 if (s > end) {
2080 goto phooey;
2081 }
2082 if (prog->extflags & RXf_USE_INTUIT) {
2083 s = re_intuit_start(rx, sv, s + UTF8SKIP(s), strend, flags, NULL);
2084 if (!s) {
2085 goto phooey;
2086 }
2087 }
2088 else {
2089 s += UTF8SKIP(s);
2090 }
2091 }
2092 } /* end search for check string in unicode */
2093 else {
2094 if (s == startpos) {
2095 goto after_try_latin;
2096 }
2097 while (1) {
2098 if (regtry(&reginfo, &s)) {
2099 goto got_it;
2100 }
2101 after_try_latin:
2102 if (s > end) {
2103 goto phooey;
2104 }
2105 if (prog->extflags & RXf_USE_INTUIT) {
2106 s = re_intuit_start(rx, sv, s + 1, strend, flags, NULL);
2107 if (!s) {
2108 goto phooey;
2109 }
2110 }
2111 else {
2112 s++;
2113 }
2114 }
2115 } /* end search for check string in latin*/
2116 } /* end search for check string */
2117 else { /* search for newline */
2118 if (s > startpos) {
2119 /*XXX: The s-- is almost definitely wrong here under unicode - demeprhq*/
6eb5f6b9 2120 s--;
92f3d482
YO
2121 }
2122 /* We can use a more efficient search as newlines are the same in unicode as they are in latin */
6eb5f6b9
JH
2123 while (s < end) {
2124 if (*s++ == '\n') { /* don't need PL_utf8skip here */
24b23f37 2125 if (regtry(&reginfo, &s))
6eb5f6b9
JH
2126 goto got_it;
2127 }
92f3d482
YO
2128 }
2129 } /* end search for newline */
2130 } /* end anchored/multiline check string search */
6eb5f6b9 2131 goto phooey;
bbe252da 2132 } else if (RXf_GPOS_CHECK == (prog->extflags & RXf_GPOS_CHECK))
f9f4320a
YO
2133 {
2134 /* the warning about reginfo.ganch being used without intialization
bbe252da 2135 is bogus -- we set it above, when prog->extflags & RXf_GPOS_SEEN
f9f4320a 2136 and we only enter this block when the same bit is set. */
58e23c8d 2137 char *tmp_s = reginfo.ganch - prog->gofs;
c584a96e
YO
2138
2139 if (tmp_s >= strbeg && regtry(&reginfo, &tmp_s))
6eb5f6b9
JH
2140 goto got_it;
2141 goto phooey;
2142 }
2143
2144 /* Messy cases: unanchored match. */
bbe252da 2145 if ((prog->anchored_substr || prog->anchored_utf8) && prog->intflags & PREGf_SKIP) {
6eb5f6b9 2146 /* we have /x+whatever/ */
f2ed9b32 2147 /* it must be a one character string (XXXX Except UTF_PATTERN?) */
33b8afdf 2148 char ch;
bf93d4cc
GS
2149#ifdef DEBUGGING
2150 int did_match = 0;
2151#endif
f2ed9b32
KW
2152 if (!(utf8_target ? prog->anchored_utf8 : prog->anchored_substr))
2153 utf8_target ? to_utf8_substr(prog) : to_byte_substr(prog);
2154 ch = SvPVX_const(utf8_target ? prog->anchored_utf8 : prog->anchored_substr)[0];
bf93d4cc 2155
f2ed9b32 2156 if (utf8_target) {
4cadc6a9 2157 REXEC_FBC_SCAN(
6eb5f6b9 2158 if (*s == ch) {
a3621e74 2159 DEBUG_EXECUTE_r( did_match = 1 );
24b23f37 2160 if (regtry(&reginfo, &s)) goto got_it;
6eb5f6b9
JH
2161 s += UTF8SKIP(s);
2162 while (s < strend && *s == ch)
2163 s += UTF8SKIP(s);
2164 }
4cadc6a9 2165 );
6eb5f6b9
JH
2166 }
2167 else {
4cadc6a9 2168 REXEC_FBC_SCAN(
6eb5f6b9 2169 if (*s == ch) {
a3621e74 2170 DEBUG_EXECUTE_r( did_match = 1 );
24b23f37 2171 if (regtry(&reginfo, &s)) goto got_it;
6eb5f6b9
JH
2172 s++;
2173 while (s < strend && *s == ch)
2174 s++;
2175 }
4cadc6a9 2176 );
6eb5f6b9 2177 }
a3621e74 2178 DEBUG_EXECUTE_r(if (!did_match)
bf93d4cc 2179 PerlIO_printf(Perl_debug_log,
b7953727
JH
2180 "Did not find anchored character...\n")
2181 );
6eb5f6b9 2182 }
a0714e2c
SS
2183 else if (prog->anchored_substr != NULL
2184 || prog->anchored_utf8 != NULL
2185 || ((prog->float_substr != NULL || prog->float_utf8 != NULL)
33b8afdf
JH
2186 && prog->float_max_offset < strend - s)) {
2187 SV *must;
2188 I32 back_max;
2189 I32 back_min;
2190 char *last;
6eb5f6b9 2191 char *last1; /* Last position checked before */
bf93d4cc
GS
2192#ifdef DEBUGGING
2193 int did_match = 0;
2194#endif
33b8afdf 2195 if (prog->anchored_substr || prog->anchored_utf8) {
f2ed9b32
KW
2196 if (!(utf8_target ? prog->anchored_utf8 : prog->anchored_substr))
2197 utf8_target ? to_utf8_substr(prog) : to_byte_substr(prog);
2198 must = utf8_target ? prog->anchored_utf8 : prog->anchored_substr;
33b8afdf
JH
2199 back_max = back_min = prog->anchored_offset;
2200 } else {
f2ed9b32
KW
2201 if (!(utf8_target ? prog->float_utf8 : prog->float_substr))
2202 utf8_target ? to_utf8_substr(prog) : to_byte_substr(prog);
2203 must = utf8_target ? prog->float_utf8 : prog->float_substr;
33b8afdf
JH
2204 back_max = prog->float_max_offset;
2205 back_min = prog->float_min_offset;
2206 }
1de06328
YO
2207
2208
33b8afdf
JH
2209 if (must == &PL_sv_undef)
2210 /* could not downgrade utf8 check substring, so must fail */
2211 goto phooey;
2212
1de06328
YO
2213 if (back_min<0) {
2214 last = strend;
2215 } else {
2216 last = HOP3c(strend, /* Cannot start after this */
2217 -(I32)(CHR_SVLEN(must)
2218 - (SvTAIL(must) != 0) + back_min), strbeg);
2219 }
6eb5f6b9
JH
2220 if (s > PL_bostr)
2221 last1 = HOPc(s, -1);
2222 else
2223 last1 = s - 1; /* bogus */
2224
a0288114 2225 /* XXXX check_substr already used to find "s", can optimize if
6eb5f6b9
JH
2226 check_substr==must. */
2227 scream_pos = -1;
2228 dontbother = end_shift;
2229 strend = HOPc(strend, -dontbother);
2230 while ( (s <= last) &&
9041c2e3 2231 ((flags & REXEC_SCREAM)
1de06328 2232 ? (s = screaminstr(sv, must, HOP3c(s, back_min, (back_min<0 ? strbeg : strend)) - strbeg,
6eb5f6b9 2233 end_shift, &scream_pos, 0))
1de06328 2234 : (s = fbm_instr((unsigned char*)HOP3(s, back_min, (back_min<0 ? strbeg : strend)),
9041c2e3 2235 (unsigned char*)strend, must,
7fba1cd6 2236 multiline ? FBMrf_MULTILINE : 0))) ) {
4addbd3b 2237 /* we may be pointing at the wrong string */
07bc277f 2238 if ((flags & REXEC_SCREAM) && RXp_MATCH_COPIED(prog))
3f7c398e 2239 s = strbeg + (s - SvPVX_const(sv));
a3621e74 2240 DEBUG_EXECUTE_r( did_match = 1 );
6eb5f6b9
JH
2241 if (HOPc(s, -back_max) > last1) {
2242 last1 = HOPc(s, -back_min);
2243 s = HOPc(s, -back_max);
2244 }
2245 else {
52657f30 2246 char * const t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
6eb5f6b9
JH
2247
2248 last1 = HOPc(s, -back_min);
52657f30 2249 s = t;
6eb5f6b9 2250 }
f2ed9b32 2251 if (utf8_target) {
6eb5f6b9 2252 while (s <= last1) {
24b23f37 2253 if (regtry(&reginfo, &s))
6eb5f6b9
JH
2254 goto got_it;
2255 s += UTF8SKIP(s);
2256 }
2257 }
2258 else {
2259 while (s <= last1) {
24b23f37 2260 if (regtry(&reginfo, &s))
6eb5f6b9
JH
2261 goto got_it;
2262 s++;
2263 }
2264 }
2265 }
ab3bbdeb 2266 DEBUG_EXECUTE_r(if (!did_match) {
f2ed9b32 2267 RE_PV_QUOTED_DECL(quoted, utf8_target, PERL_DEBUG_PAD_ZERO(0),
ab3bbdeb
YO
2268 SvPVX_const(must), RE_SV_DUMPLEN(must), 30);
2269 PerlIO_printf(Perl_debug_log, "Did not find %s substr %s%s...\n",
33b8afdf 2270 ((must == prog->anchored_substr || must == prog->anchored_utf8)
bf93d4cc 2271 ? "anchored" : "floating"),
ab3bbdeb
YO
2272 quoted, RE_SV_TAIL(must));
2273 });
6eb5f6b9
JH
2274 goto phooey;
2275 }
f8fc2ecf 2276 else if ( (c = progi->regstclass) ) {
f14c76ed 2277 if (minlen) {
f8fc2ecf 2278 const OPCODE op = OP(progi->regstclass);
66e933ab 2279 /* don't bother with what can't match */
786e8c11 2280 if (PL_regkind[op] != EXACT && op != CANY && PL_regkind[op] != TRIE)
f14c76ed
RGS
2281 strend = HOPc(strend, -(minlen - 1));
2282 }
a3621e74 2283 DEBUG_EXECUTE_r({
be8e71aa 2284 SV * const prop = sv_newmortal();
32fc9b6a 2285 regprop(prog, prop, c);
0df25f3d 2286 {
f2ed9b32 2287 RE_PV_QUOTED_DECL(quoted,utf8_target,PERL_DEBUG_PAD_ZERO(1),
ab3bbdeb 2288 s,strend-s,60);
0df25f3d 2289 PerlIO_printf(Perl_debug_log,
1c8f8eb1 2290 "Matching stclass %.*s against %s (%d bytes)\n",
e4f74956 2291 (int)SvCUR(prop), SvPVX_const(prop),
ab3bbdeb 2292 quoted, (int)(strend - s));
0df25f3d 2293 }
ffc61ed2 2294 });
3b0527fe 2295 if (find_byclass(prog, c, s, strend, &reginfo))
6eb5f6b9 2296 goto got_it;
07be1b83 2297 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Contradicts stclass... [regexec_flags]\n"));
d6a28714
JH
2298 }
2299 else {
2300 dontbother = 0;
a0714e2c 2301 if (prog->float_substr != NULL || prog->float_utf8 != NULL) {
33b8afdf 2302 /* Trim the end. */
d6a28714 2303 char *last;
33b8afdf
JH
2304 SV* float_real;
2305
f2ed9b32
KW
2306 if (!(utf8_target ? prog->float_utf8 : prog->float_substr))
2307 utf8_target ? to_utf8_substr(prog) : to_byte_substr(prog);
2308 float_real = utf8_target ? prog->float_utf8 : prog->float_substr;
d6a28714
JH
2309
2310 if (flags & REXEC_SCREAM) {
33b8afdf 2311 last = screaminstr(sv, float_real, s - strbeg,
d6a28714
JH
2312 end_shift, &scream_pos, 1); /* last one */
2313 if (!last)
ffc61ed2 2314 last = scream_olds; /* Only one occurrence. */
4addbd3b 2315 /* we may be pointing at the wrong string */
07bc277f 2316 else if (RXp_MATCH_COPIED(prog))
3f7c398e 2317 s = strbeg + (s - SvPVX_const(sv));
b8c5462f 2318 }
d6a28714
JH
2319 else {
2320 STRLEN len;
cfd0369c 2321 const char * const little = SvPV_const(float_real, len);
d6a28714 2322
33b8afdf 2323 if (SvTAIL(float_real)) {
d6a28714
JH
2324 if (memEQ(strend - len + 1, little, len - 1))
2325 last = strend - len + 1;
7fba1cd6 2326 else if (!multiline)
9041c2e3 2327 last = memEQ(strend - len, little, len)
bd61b366 2328 ? strend - len : NULL;
b8c5462f 2329 else
d6a28714
JH
2330 goto find_last;
2331 } else {
2332 find_last:
9041c2e3 2333 if (len)
d6a28714 2334 last = rninstr(s, strend, little, little + len);
b8c5462f 2335 else
a0288114 2336 last = strend; /* matching "$" */
b8c5462f 2337 }
b8c5462f 2338 }
bf93d4cc 2339 if (last == NULL) {
6bda09f9
YO
2340 DEBUG_EXECUTE_r(
2341 PerlIO_printf(Perl_debug_log,
2342 "%sCan't trim the tail, match fails (should not happen)%s\n",
2343 PL_colors[4], PL_colors[5]));
bf93d4cc
GS
2344 goto phooey; /* Should not happen! */
2345 }
d6a28714
JH
2346 dontbother = strend - last + prog->float_min_offset;
2347 }
2348 if (minlen && (dontbother < minlen))
2349 dontbother = minlen - 1;
2350 strend -= dontbother; /* this one's always in bytes! */
2351 /* We don't know much -- general case. */
f2ed9b32 2352 if (utf8_target) {
d6a28714 2353 for (;;) {
24b23f37 2354 if (regtry(&reginfo, &s))
d6a28714
JH
2355 goto got_it;
2356 if (s >= strend)
2357 break;
b8c5462f 2358 s += UTF8SKIP(s);
d6a28714
JH
2359 };
2360 }
2361 else {
2362 do {
24b23f37 2363 if (regtry(&reginfo, &s))
d6a28714
JH
2364 goto got_it;
2365 } while (s++ < strend);
2366 }
2367 }
2368
2369 /* Failure. */
2370 goto phooey;
2371
2372got_it:
e9105d30 2373 Safefree(swap);
288b8c02 2374 RX_MATCH_TAINTED_set(rx, PL_reg_flags & RF_tainted);
d6a28714 2375
19b95bf0 2376 if (PL_reg_eval_set)
4f639d21 2377 restore_pos(aTHX_ prog);
5daac39c
NC
2378 if (RXp_PAREN_NAMES(prog))
2379 (void)hv_iterinit(RXp_PAREN_NAMES(prog));
d6a28714
JH
2380
2381 /* make sure $`, $&, $', and $digit will work later */
2382 if ( !(flags & REXEC_NOT_FIRST) ) {
288b8c02 2383 RX_MATCH_COPY_FREE(rx);
d6a28714 2384 if (flags & REXEC_COPY_STR) {
be8e71aa 2385 const I32 i = PL_regeol - startpos + (stringarg - strbeg);
f8c7b90f 2386#ifdef PERL_OLD_COPY_ON_WRITE
ed252734
NC
2387 if ((SvIsCOW(sv)
2388 || (SvFLAGS(sv) & CAN_COW_MASK) == CAN_COW_FLAGS)) {
2389 if (DEBUG_C_TEST) {
2390 PerlIO_printf(Perl_debug_log,
2391 "Copy on write: regexp capture, type %d\n",
2392 (int) SvTYPE(sv));
2393 }
2394 prog->saved_copy = sv_setsv_cow(prog->saved_copy, sv);
d5263905 2395 prog->subbeg = (char *)SvPVX_const(prog->saved_copy);
ed252734
NC
2396 assert (SvPOKp(prog->saved_copy));
2397 } else
2398#endif
2399 {
288b8c02 2400 RX_MATCH_COPIED_on(rx);
ed252734
NC
2401 s = savepvn(strbeg, i);
2402 prog->subbeg = s;
2403 }
d6a28714 2404 prog->sublen = i;
d6a28714
JH
2405 }
2406 else {
2407 prog->subbeg = strbeg;
2408 prog->sublen = PL_regeol - strbeg; /* strend may have been modified */
2409 }
2410 }
9041c2e3 2411
d6a28714
JH
2412 return 1;
2413
2414phooey:
a3621e74 2415 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch failed%s\n",
e4584336 2416 PL_colors[4], PL_colors[5]));
d6a28714 2417 if (PL_reg_eval_set)
4f639d21 2418 restore_pos(aTHX_ prog);
e9105d30 2419 if (swap) {
c74340f9 2420 /* we failed :-( roll it back */
e9105d30
GG
2421 Safefree(prog->offs);
2422 prog->offs = swap;
2423 }
2424
d6a28714
JH
2425 return 0;
2426}
2427
6bda09f9 2428
d6a28714
JH
2429/*
2430 - regtry - try match at specific point
2431 */
2432STATIC I32 /* 0 failure, 1 success */
24b23f37 2433S_regtry(pTHX_ regmatch_info *reginfo, char **startpos)
d6a28714 2434{
97aff369 2435 dVAR;
d6a28714 2436 CHECKPOINT lastcp;
288b8c02
NC
2437 REGEXP *const rx = reginfo->prog;
2438 regexp *const prog = (struct regexp *)SvANY(rx);
f8fc2ecf 2439 RXi_GET_DECL(prog,progi);
a3621e74 2440 GET_RE_DEBUG_FLAGS_DECL;
7918f24d
NC
2441
2442 PERL_ARGS_ASSERT_REGTRY;
2443
24b23f37 2444 reginfo->cutpoint=NULL;
d6a28714 2445
bbe252da 2446 if ((prog->extflags & RXf_EVAL_SEEN) && !PL_reg_eval_set) {
d6a28714
JH
2447 MAGIC *mg;
2448
2449 PL_reg_eval_set = RS_init;
a3621e74 2450 DEBUG_EXECUTE_r(DEBUG_s(
b900a521
JH
2451 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %"IVdf"\n",
2452 (IV)(PL_stack_sp - PL_stack_base));
d6a28714 2453 ));
ea8d6ae1 2454 SAVESTACK_CXPOS();
d6a28714
JH
2455 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
2456 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
2457 SAVETMPS;
2458 /* Apparently this is not needed, judging by wantarray. */
e8347627 2459 /* SAVEI8(cxstack[cxstack_ix].blk_gimme);
d6a28714
JH
2460 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
2461
3b0527fe 2462 if (reginfo->sv) {
d6a28714 2463 /* Make $_ available to executed code. */
3b0527fe 2464 if (reginfo->sv != DEFSV) {
59f00321 2465 SAVE_DEFSV;
414bf5ae 2466 DEFSV_set(reginfo->sv);
b8c5462f 2467 }
d6a28714 2468
3b0527fe
DM
2469 if (!(SvTYPE(reginfo->sv) >= SVt_PVMG && SvMAGIC(reginfo->sv)
2470 && (mg = mg_find(reginfo->sv, PERL_MAGIC_regex_global)))) {
d6a28714 2471 /* prepare for quick setting of pos */
d300d9fa 2472#ifdef PERL_OLD_COPY_ON_WRITE
51a9ea20
NC
2473 if (SvIsCOW(reginfo->sv))
2474 sv_force_normal_flags(reginfo->sv, 0);
d300d9fa 2475#endif
3dab1dad 2476 mg = sv_magicext(reginfo->sv, NULL, PERL_MAGIC_regex_global,
d300d9fa 2477 &PL_vtbl_mglob, NULL, 0);
d6a28714 2478 mg->mg_len = -1;
b8c5462f 2479 }
d6a28714
JH
2480 PL_reg_magic = mg;
2481 PL_reg_oldpos = mg->mg_len;
4f639d21 2482 SAVEDESTRUCTOR_X(restore_pos, prog);
d6a28714 2483 }
09687e5a 2484 if (!PL_reg_curpm) {
a02a5408 2485 Newxz(PL_reg_curpm, 1, PMOP);
09687e5a
AB
2486#ifdef USE_ITHREADS
2487 {
14a49a24 2488 SV* const repointer = &PL_sv_undef;
92313705
NC
2489 /* this regexp is also owned by the new PL_reg_curpm, which
2490 will try to free it. */
d2ece331 2491 av_push(PL_regex_padav, repointer);
09687e5a
AB
2492 PL_reg_curpm->op_pmoffset = av_len(PL_regex_padav);
2493 PL_regex_pad = AvARRAY(PL_regex_padav);
2494 }
2495#endif
2496 }
86c29d75
NC
2497#ifdef USE_ITHREADS
2498 /* It seems that non-ithreads works both with and without this code.
2499 So for efficiency reasons it seems best not to have the code
2500 compiled when it is not needed. */
92313705
NC
2501 /* This is safe against NULLs: */
2502 ReREFCNT_dec(PM_GETRE(PL_reg_curpm));
2503 /* PM_reg_curpm owns a reference to this regexp. */
2504 ReREFCNT_inc(rx);
86c29d75 2505#endif
288b8c02 2506 PM_SETRE(PL_reg_curpm, rx);
d6a28714
JH
2507 PL_reg_oldcurpm = PL_curpm;
2508 PL_curpm = PL_reg_curpm;
07bc277f 2509 if (RXp_MATCH_COPIED(prog)) {
d6a28714
JH
2510 /* Here is a serious problem: we cannot rewrite subbeg,
2511 since it may be needed if this match fails. Thus
2512 $` inside (?{}) could fail... */
2513 PL_reg_oldsaved = prog->subbeg;
2514 PL_reg_oldsavedlen = prog->sublen;
f8c7b90f 2515#ifdef PERL_OLD_COPY_ON_WRITE
ed252734
NC
2516 PL_nrs = prog->saved_copy;
2517#endif
07bc277f 2518 RXp_MATCH_COPIED_off(prog);
d6a28714
JH
2519 }
2520 else
bd61b366 2521 PL_reg_oldsaved = NULL;
d6a28714
JH
2522 prog->subbeg = PL_bostr;
2523 prog->sublen = PL_regeol - PL_bostr; /* strend may have been modified */
2524 }
24b23f37 2525 DEBUG_EXECUTE_r(PL_reg_starttry = *startpos);
f0ab9afb 2526 prog->offs[0].start = *startpos - PL_bostr;
24b23f37 2527 PL_reginput = *startpos;
d6a28714 2528 PL_reglastparen = &prog->lastparen;
a01268b5 2529 PL_reglastcloseparen = &prog->lastcloseparen;
d6a28714 2530 prog->lastparen = 0;
03994de8 2531 prog->lastcloseparen = 0;
d6a28714 2532 PL_regsize = 0;
f0ab9afb 2533 PL_regoffs = prog->offs;
d6a28714
JH
2534 if (PL_reg_start_tmpl <= prog->nparens) {
2535 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
2536 if(PL_reg_start_tmp)
2537 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2538 else
a02a5408 2539 Newx(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
d6a28714
JH
2540 }
2541
2542 /* XXXX What this code is doing here?!!! There should be no need
2543 to do this again and again, PL_reglastparen should take care of
3dd2943c 2544 this! --ilya*/
dafc8851
JH
2545
2546 /* Tests pat.t#187 and split.t#{13,14} seem to depend on this code.
2547 * Actually, the code in regcppop() (which Ilya may be meaning by
daf18116 2548 * PL_reglastparen), is not needed at all by the test suite
225593e1
DM
2549 * (op/regexp, op/pat, op/split), but that code is needed otherwise
2550 * this erroneously leaves $1 defined: "1" =~ /^(?:(\d)x)?\d$/
2551 * Meanwhile, this code *is* needed for the
daf18116
JH
2552 * above-mentioned test suite tests to succeed. The common theme
2553 * on those tests seems to be returning null fields from matches.
225593e1 2554 * --jhi updated by dapm */
dafc8851 2555#if 1
d6a28714 2556 if (prog->nparens) {
f0ab9afb 2557 regexp_paren_pair *pp = PL_regoffs;
097eb12c 2558 register I32 i;
eb160463 2559 for (i = prog->nparens; i > (I32)*PL_reglastparen; i--) {
f0ab9afb
NC
2560 ++pp;
2561 pp->start = -1;
2562 pp->end = -1;
d6a28714
JH
2563 }
2564 }
dafc8851 2565#endif
02db2b7b 2566 REGCP_SET(lastcp);
f8fc2ecf 2567 if (regmatch(reginfo, progi->program + 1)) {
f0ab9afb 2568 PL_regoffs[0].end = PL_reginput - PL_bostr;
d6a28714
JH
2569 return 1;
2570 }
24b23f37
YO
2571 if (reginfo->cutpoint)
2572 *startpos= reginfo->cutpoint;
02db2b7b 2573 REGCP_UNWIND(lastcp);
d6a28714
JH
2574 return 0;
2575}
2576
02db2b7b 2577
8ba1375e
MJD
2578#define sayYES goto yes
2579#define sayNO goto no
262b90c4 2580#define sayNO_SILENT goto no_silent
8ba1375e 2581
f9f4320a
YO
2582/* we dont use STMT_START/END here because it leads to
2583 "unreachable code" warnings, which are bogus, but distracting. */
2584#define CACHEsayNO \
c476f425
DM
2585 if (ST.cache_mask) \
2586 PL_reg_poscache[ST.cache_offset] |= ST.cache_mask; \
f9f4320a 2587 sayNO
3298f257 2588
a3621e74 2589/* this is used to determine how far from the left messages like
265c4333
YO
2590 'failed...' are printed. It should be set such that messages
2591 are inline with the regop output that created them.
a3621e74 2592*/
265c4333 2593#define REPORT_CODE_OFF 32
a3621e74
YO
2594
2595
40a82448
DM
2596#define CHRTEST_UNINIT -1001 /* c1/c2 haven't been calculated yet */
2597#define CHRTEST_VOID -1000 /* the c1/c2 "next char" test should be skipped */
9e137952 2598
86545054
DM
2599#define SLAB_FIRST(s) (&(s)->states[0])
2600#define SLAB_LAST(s) (&(s)->states[PERL_REGMATCH_SLAB_SLOTS-1])
2601
5d9a96ca
DM
2602/* grab a new slab and return the first slot in it */
2603
2604STATIC regmatch_state *
2605S_push_slab(pTHX)
2606{
a35a87e7 2607#if PERL_VERSION < 9 && !defined(PERL_CORE)
54df2634
NC
2608 dMY_CXT;
2609#endif
5d9a96ca
DM
2610 regmatch_slab *s = PL_regmatch_slab->next;
2611 if (!s) {
2612 Newx(s, 1, regmatch_slab);
2613 s->prev = PL_regmatch_slab;
2614 s->next = NULL;
2615 PL_regmatch_slab->next = s;
2616 }
2617 PL_regmatch_slab = s;
86545054 2618 return SLAB_FIRST(s);
5d9a96ca 2619}
5b47454d 2620
95b24440 2621
40a82448
DM
2622/* push a new state then goto it */
2623
2624#define PUSH_STATE_GOTO(state, node) \
2625 scan = node; \
2626 st->resume_state = state; \
2627 goto push_state;
2628
2629/* push a new state with success backtracking, then goto it */
2630
2631#define PUSH_YES_STATE_GOTO(state, node) \
2632 scan = node; \
2633 st->resume_state = state; \
2634 goto push_yes_state;
2635
aa283a38 2636
aa283a38 2637
d6a28714 2638/*
95b24440 2639
bf1f174e
DM
2640regmatch() - main matching routine
2641
2642This is basically one big switch statement in a loop. We execute an op,
2643set 'next' to point the next op, and continue. If we come to a point which
2644we may need to backtrack to on failure such as (A|B|C), we push a
2645backtrack state onto the backtrack stack. On failure, we pop the top
2646state, and re-enter the loop at the state indicated. If there are no more
2647states to pop, we return failure.
2648
2649Sometimes we also need to backtrack on success; for example /A+/, where
2650after successfully matching one A, we need to go back and try to
2651match another one; similarly for lookahead assertions: if the assertion
2652completes successfully, we backtrack to the state just before the assertion
2653and then carry on. In these cases, the pushed state is marked as
2654'backtrack on success too'. This marking is in fact done by a chain of
2655pointers, each pointing to the previous 'yes' state. On success, we pop to
2656the nearest yes state, discarding any intermediate failure-only states.
2657Sometimes a yes state is pushed just to force some cleanup code to be
2658called at the end of a successful match or submatch; e.g. (??{$re}) uses
2659it to free the inner regex.
2660
2661Note that failure backtracking rewinds the cursor position, while
2662success backtracking leaves it alone.
2663
2664A pattern is complete when the END op is executed, while a subpattern
2665such as (?=foo) is complete when the SUCCESS op is executed. Both of these
2666ops trigger the "pop to last yes state if any, otherwise return true"
2667behaviour.
2668
2669A common convention in this function is to use A and B to refer to the two
2670subpatterns (or to the first nodes thereof) in patterns like /A*B/: so A is
2671the subpattern to be matched possibly multiple times, while B is the entire
2672rest of the pattern. Variable and state names reflect this convention.
2673
2674The states in the main switch are the union of ops and failure/success of
2675substates associated with with that op. For example, IFMATCH is the op
2676that does lookahead assertions /(?=A)B/ and so the IFMATCH state means
2677'execute IFMATCH'; while IFMATCH_A is a state saying that we have just
2678successfully matched A and IFMATCH_A_fail is a state saying that we have
2679just failed to match A. Resume states always come in pairs. The backtrack
2680state we push is marked as 'IFMATCH_A', but when that is popped, we resume
2681at IFMATCH_A or IFMATCH_A_fail, depending on whether we are backtracking
2682on success or failure.
2683
2684The struct that holds a backtracking state is actually a big union, with
2685one variant for each major type of op. The variable st points to the
2686top-most backtrack struct. To make the code clearer, within each
2687block of code we #define ST to alias the relevant union.
2688
2689Here's a concrete example of a (vastly oversimplified) IFMATCH
2690implementation:
2691
2692 switch (state) {
2693 ....
2694
2695#define ST st->u.ifmatch
2696
2697 case IFMATCH: // we are executing the IFMATCH op, (?=A)B
2698 ST.foo = ...; // some state we wish to save
95b24440 2699 ...
bf1f174e
DM
2700 // push a yes backtrack state with a resume value of
2701 // IFMATCH_A/IFMATCH_A_fail, then continue execution at the
2702 // first node of A:
2703 PUSH_YES_STATE_GOTO(IFMATCH_A, A);
2704 // NOTREACHED
2705
2706 case IFMATCH_A: // we have successfully executed A; now continue with B
2707 next = B;
2708 bar = ST.foo; // do something with the preserved value
2709 break;
2710
2711 case IFMATCH_A_fail: // A failed, so the assertion failed
2712 ...; // do some housekeeping, then ...
2713 sayNO; // propagate the failure
2714
2715#undef ST
95b24440 2716
bf1f174e
DM
2717 ...
2718 }
95b24440 2719
bf1f174e
DM
2720For any old-timers reading this who are familiar with the old recursive
2721approach, the code above is equivalent to:
95b24440 2722
bf1f174e
DM
2723 case IFMATCH: // we are executing the IFMATCH op, (?=A)B
2724 {
2725 int foo = ...
95b24440 2726 ...
bf1f174e
DM
2727 if (regmatch(A)) {
2728 next = B;
2729 bar = foo;
2730 break;
95b24440 2731 }
bf1f174e
DM
2732 ...; // do some housekeeping, then ...
2733 sayNO; // propagate the failure
95b24440 2734 }
bf1f174e
DM
2735
2736The topmost backtrack state, pointed to by st, is usually free. If you
2737want to claim it, populate any ST.foo fields in it with values you wish to
2738save, then do one of
2739
2740 PUSH_STATE_GOTO(resume_state, node);
2741 PUSH_YES_STATE_GOTO(resume_state, node);
2742
2743which sets that backtrack state's resume value to 'resume_state', pushes a
2744new free entry to the top of the backtrack stack, then goes to 'node'.
2745On backtracking, the free slot is popped, and the saved state becomes the
2746new free state. An ST.foo field in this new top state can be temporarily
2747accessed to retrieve values, but once the main loop is re-entered, it
2748becomes available for reuse.
2749
2750Note that the depth of the backtrack stack constantly increases during the
2751left-to-right execution of the pattern, rather than going up and down with
2752the pattern nesting. For example the stack is at its maximum at Z at the
2753end of the pattern, rather than at X in the following:
2754
2755 /(((X)+)+)+....(Y)+....Z/
2756
2757The only exceptions to this are lookahead/behind assertions and the cut,
2758(?>A), which pop all the backtrack states associated with A before
2759continuing.
2760
2761Bascktrack state structs are allocated in slabs of about 4K in size.
2762PL_regmatch_state and st always point to the currently active state,
2763and PL_regmatch_slab points to the slab currently containing
2764PL_regmatch_state. The first time regmatch() is called, the first slab is
2765allocated, and is never freed until interpreter destruction. When the slab
2766is full, a new one is allocated and chained to the end. At exit from
2767regmatch(), slabs allocated since entry are freed.
2768
2769*/
95b24440 2770
40a82448 2771
5bc10b2c 2772#define DEBUG_STATE_pp(pp) \
265c4333 2773 DEBUG_STATE_r({ \
f2ed9b32 2774 DUMP_EXEC_POS(locinput, scan, utf8_target); \
5bc10b2c 2775 PerlIO_printf(Perl_debug_log, \
5d458dd8 2776 " %*s"pp" %s%s%s%s%s\n", \
5bc10b2c 2777 depth*2, "", \
13d6edb4 2778 PL_reg_name[st->resume_state], \
5d458dd8
YO
2779 ((st==yes_state||st==mark_state) ? "[" : ""), \
2780 ((st==yes_state) ? "Y" : ""), \
2781 ((st==mark_state) ? "M" : ""), \
2782 ((st==yes_state||st==mark_state) ? "]" : "") \
2783 ); \
265c4333 2784 });
5bc10b2c 2785
40a82448 2786
3dab1dad 2787#define REG_NODE_NUM(x) ((x) ? (int)((x)-prog) : -1)
95b24440 2788
3df15adc 2789#ifdef DEBUGGING
5bc10b2c 2790
ab3bbdeb 2791STATIC void
f2ed9b32 2792S_debug_start_match(pTHX_ const REGEXP *prog, const bool utf8_target,
ab3bbdeb
YO
2793 const char *start, const char *end, const char *blurb)
2794{
efd26800 2795 const bool utf8_pat = RX_UTF8(prog) ? 1 : 0;
7918f24d
NC
2796
2797 PERL_ARGS_ASSERT_DEBUG_START_MATCH;
2798
ab3bbdeb
YO
2799 if (!PL_colorset)
2800 reginitcolors();
2801 {
2802 RE_PV_QUOTED_DECL(s0, utf8_pat, PERL_DEBUG_PAD_ZERO(0),
d2c6dc5e 2803 RX_PRECOMP_const(prog), RX_PRELEN(prog), 60);
ab3bbdeb 2804
f2ed9b32 2805 RE_PV_QUOTED_DECL(s1, utf8_target, PERL_DEBUG_PAD_ZERO(1),
ab3bbdeb
YO
2806 start, end - start, 60);
2807
2808 PerlIO_printf(Perl_debug_log,
2809 "%s%s REx%s %s against %s\n",
2810 PL_colors[4], blurb, PL_colors[5], s0, s1);
2811
f2ed9b32 2812 if (utf8_target||utf8_pat)
1de06328
YO
2813 PerlIO_printf(Perl_debug_log, "UTF-8 %s%s%s...\n",
2814 utf8_pat ? "pattern" : "",
f2ed9b32
KW
2815 utf8_pat && utf8_target ? " and " : "",
2816 utf8_target ? "string" : ""
ab3bbdeb
YO
2817 );
2818 }
2819}
3df15adc
YO
2820
2821STATIC void
786e8c11
YO
2822S_dump_exec_pos(pTHX_ const char *locinput,
2823 const regnode *scan,
2824 const char *loc_regeol,
2825 const char *loc_bostr,
2826 const char *loc_reg_starttry,
f2ed9b32 2827 const bool utf8_target)
07be1b83 2828{
786e8c11 2829 const int docolor = *PL_colors[0] || *PL_colors[2] || *PL_colors[4];
07be1b83 2830 const int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
786e8c11 2831 int l = (loc_regeol - locinput) > taill ? taill : (loc_regeol - locinput);
07be1b83
YO
2832 /* The part of the string before starttry has one color
2833 (pref0_len chars), between starttry and current
2834 position another one (pref_len - pref0_len chars),
2835 after the current position the third one.
2836 We assume that pref0_len <= pref_len, otherwise we
2837 decrease pref0_len. */
786e8c11
YO
2838 int pref_len = (locinput - loc_bostr) > (5 + taill) - l
2839 ? (5 + taill) - l : locinput - loc_bostr;
07be1b83
YO
2840 int pref0_len;
2841
7918f24d
NC
2842 PERL_ARGS_ASSERT_DUMP_EXEC_POS;
2843
f2ed9b32 2844 while (utf8_target && UTF8_IS_CONTINUATION(*(U8*)(locinput - pref_len)))
07be1b83 2845 pref_len++;
786e8c11
YO
2846 pref0_len = pref_len - (locinput - loc_reg_starttry);
2847 if (l + pref_len < (5 + taill) && l < loc_regeol - locinput)
2848 l = ( loc_regeol - locinput > (5 + taill) - pref_len
2849 ? (5 + taill) - pref_len : loc_regeol - locinput);
f2ed9b32 2850 while (utf8_target && UTF8_IS_CONTINUATION(*(U8*)(locinput + l)))
07be1b83
YO
2851 l--;
2852 if (pref0_len < 0)
2853 pref0_len = 0;
2854 if (pref0_len > pref_len)
2855 pref0_len = pref_len;
2856 {
f2ed9b32 2857 const int is_uni = (utf8_target && OP(scan) != CANY) ? 1 : 0;
0df25f3d 2858
ab3bbdeb 2859 RE_PV_COLOR_DECL(s0,len0,is_uni,PERL_DEBUG_PAD(0),
1de06328 2860 (locinput - pref_len),pref0_len, 60, 4, 5);
0df25f3d 2861
ab3bbdeb 2862 RE_PV_COLOR_DECL(s1,len1,is_uni,PERL_DEBUG_PAD(1),
3df15adc 2863 (locinput - pref_len + pref0_len),
1de06328 2864 pref_len - pref0_len, 60, 2, 3);
0df25f3d 2865
ab3bbdeb 2866 RE_PV_COLOR_DECL(s2,len2,is_uni,PERL_DEBUG_PAD(2),
1de06328 2867 locinput, loc_regeol - locinput, 10, 0, 1);
0df25f3d 2868
1de06328 2869 const STRLEN tlen=len0+len1+len2;
3df15adc 2870 PerlIO_printf(Perl_debug_log,
ab3bbdeb 2871 "%4"IVdf" <%.*s%.*s%s%.*s>%*s|",
786e8c11 2872 (IV)(locinput - loc_bostr),
07be1b83 2873 len0, s0,
07be1b83 2874 len1, s1,
07be1b83 2875 (docolor ? "" : "> <"),
07be1b83 2876 len2, s2,
f9f4320a 2877 (int)(tlen > 19 ? 0 : 19 - tlen),
07be1b83
YO
2878 "");
2879 }
2880}
3df15adc 2881
07be1b83
YO
2882#endif
2883
0a4db386
YO
2884/* reg_check_named_buff_matched()
2885 * Checks to see if a named buffer has matched. The data array of
2886 * buffer numbers corresponding to the buffer is expected to reside
2887 * in the regexp->data->data array in the slot stored in the ARG() of
2888 * node involved. Note that this routine doesn't actually care about the
2889 * name, that information is not preserved from compilation to execution.
2890 * Returns the index of the leftmost defined buffer with the given name
2891 * or 0 if non of the buffers matched.
2892 */
2893STATIC I32
7918f24d
NC
2894S_reg_check_named_buff_matched(pTHX_ const regexp *rex, const regnode *scan)
2895{
0a4db386 2896 I32 n;
f8fc2ecf 2897 RXi_GET_DECL(rex,rexi);
ad64d0ec 2898 SV *sv_dat= MUTABLE_SV(rexi->data->data[ ARG( scan ) ]);
0a4db386 2899 I32 *nums=(I32*)SvPVX(sv_dat);
7918f24d
NC
2900
2901 PERL_ARGS_ASSERT_REG_CHECK_NAMED_BUFF_MATCHED;
2902
0a4db386
YO
2903 for ( n=0; n<SvIVX(sv_dat); n++ ) {
2904 if ((I32)*PL_reglastparen >= nums[n] &&
f0ab9afb 2905 PL_regoffs[nums[n]].end != -1)
0a4db386
YO
2906 {
2907 return nums[n];
2908 }
2909 }
2910 return 0;
2911}
2912
2f554ef7
DM
2913
2914/* free all slabs above current one - called during LEAVE_SCOPE */
2915
2916STATIC void
2917S_clear_backtrack_stack(pTHX_ void *p)
2918{
2919 regmatch_slab *s = PL_regmatch_slab->next;
2920 PERL_UNUSED_ARG(p);
2921
2922 if (!s)
2923 return;
2924 PL_regmatch_slab->next = NULL;
2925 while (s) {
2926 regmatch_slab * const osl = s;
2927 s = s->next;
2928 Safefree(osl);
2929 }
2930}
2931
2932
28d8d7f4
YO
2933#define SETREX(Re1,Re2) \
2934 if (PL_reg_eval_set) PM_SETRE((PL_reg_curpm), (Re2)); \
2935 Re1 = (Re2)
2936
d6a28714 2937STATIC I32 /* 0 failure, 1 success */
24b23f37 2938S_regmatch(pTHX_ regmatch_info *reginfo, regnode *prog)
d6a28714 2939{
a35a87e7 2940#if PERL_VERSION < 9 && !defined(PERL_CORE)
54df2634
NC
2941 dMY_CXT;
2942#endif
27da23d5 2943 dVAR;
f2ed9b32 2944 register const bool utf8_target = PL_reg_match_utf8;
4ad0818d 2945 const U32 uniflags = UTF8_ALLOW_DEFAULT;
288b8c02
NC
2946 REGEXP *rex_sv = reginfo->prog;
2947 regexp *rex = (struct regexp *)SvANY(rex_sv);
f8fc2ecf 2948 RXi_GET_DECL(rex,rexi);
2f554ef7 2949 I32 oldsave;
5d9a96ca
DM
2950 /* the current state. This is a cached copy of PL_regmatch_state */
2951 register regmatch_state *st;
5d9a96ca
DM
2952 /* cache heavy used fields of st in registers */
2953 register regnode *scan;
2954 register regnode *next;
438e9bae 2955 register U32 n = 0; /* general value; init to avoid compiler warning */
24d3c4a9 2956 register I32 ln = 0; /* len or last; init to avoid compiler warning */
5d9a96ca 2957 register char *locinput = PL_reginput;
5d9a96ca 2958 register I32 nextchr; /* is always set to UCHARAT(locinput) */
24d3c4a9 2959
b69b0499 2960 bool result = 0; /* return value of S_regmatch */
24d3c4a9 2961 int depth = 0; /* depth of backtrack stack */
4b196cd4
YO
2962 U32 nochange_depth = 0; /* depth of GOSUB recursion with nochange */
2963 const U32 max_nochange_depth =
2964 (3 * rex->nparens > MAX_RECURSE_EVAL_NOCHANGE_DEPTH) ?
2965 3 * rex->nparens : MAX_RECURSE_EVAL_NOCHANGE_DEPTH;
77cb431f
DM
2966 regmatch_state *yes_state = NULL; /* state to pop to on success of
2967 subpattern */
e2e6a0f1
YO
2968 /* mark_state piggy backs on the yes_state logic so that when we unwind
2969 the stack on success we can update the mark_state as we go */
2970 regmatch_state *mark_state = NULL; /* last mark state we have seen */
faec1544 2971 regmatch_state *cur_eval = NULL; /* most recent EVAL_AB state */
b8591aee 2972 struct regmatch_state *cur_curlyx = NULL; /* most recent curlyx */
40a82448 2973 U32 state_num;
5d458dd8
YO
2974 bool no_final = 0; /* prevent failure from backtracking? */
2975 bool do_cutgroup = 0; /* no_final only until next branch/trie entry */
e2e6a0f1 2976 char *startpoint = PL_reginput;
5d458dd8
YO
2977 SV *popmark = NULL; /* are we looking for a mark? */
2978 SV *sv_commit = NULL; /* last mark name seen in failure */
2979 SV *sv_yes_mark = NULL; /* last mark name we have seen
2980 during a successfull match */
2981 U32 lastopen = 0; /* last open we saw */
2982 bool has_cutgroup = RX_HAS_CUTGROUP(rex) ? 1 : 0;
19b95bf0 2983 SV* const oreplsv = GvSV(PL_replgv);
24d3c4a9
DM
2984 /* these three flags are set by various ops to signal information to
2985 * the very next op. They have a useful lifetime of exactly one loop
2986 * iteration, and are not preserved or restored by state pushes/pops
2987 */
2988 bool sw = 0; /* the condition value in (?(cond)a|b) */
2989 bool minmod = 0; /* the next "{n,m}" is a "{n,m}?" */
2990 int logical = 0; /* the following EVAL is:
2991 0: (?{...})
2992 1: (?(?{...})X|Y)
2993 2: (??{...})
2994 or the following IFMATCH/UNLESSM is:
2995 false: plain (?=foo)
2996 true: used as a condition: (?(?=foo))
2997 */
95b24440 2998#ifdef DEBUGGING
e68ec53f 2999 GET_RE_DEBUG_FLAGS_DECL;
d6a28714
JH
3000#endif
3001
7918f24d
NC
3002 PERL_ARGS_ASSERT_REGMATCH;
3003
3b57cd43 3004 DEBUG_OPTIMISE_r( DEBUG_EXECUTE_r({
24b23f37 3005 PerlIO_printf(Perl_debug_log,"regmatch start\n");
3b57cd43 3006 }));
5d9a96ca
DM
3007 /* on first ever call to regmatch, allocate first slab */
3008 if (!PL_regmatch_slab) {
3009 Newx(PL_regmatch_slab, 1, regmatch_slab);
3010 PL_regmatch_slab->prev = NULL;
3011 PL_regmatch_slab->next = NULL;
86545054 3012 PL_regmatch_state = SLAB_FIRST(PL_regmatch_slab);
5d9a96ca
DM
3013 }
3014
2f554ef7
DM
3015 oldsave = PL_savestack_ix;
3016 SAVEDESTRUCTOR_X(S_clear_backtrack_stack, NULL);
3017 SAVEVPTR(PL_regmatch_slab);
3018 SAVEVPTR(PL_regmatch_state);
5d9a96ca
DM
3019
3020 /* grab next free state slot */
3021 st = ++PL_regmatch_state;
86545054 3022 if (st > SLAB_LAST(PL_regmatch_slab))
5d9a96ca
DM
3023 st = PL_regmatch_state = S_push_slab(aTHX);
3024
d6a28714
JH
3025 /* Note that nextchr is a byte even in UTF */
3026 nextchr = UCHARAT(locinput);
3027 scan = prog;
3028 while (scan != NULL) {
8ba1375e 3029
a3621e74 3030 DEBUG_EXECUTE_r( {
6136c704 3031 SV * const prop = sv_newmortal();
1de06328 3032 regnode *rnext=regnext(scan);
f2ed9b32 3033 DUMP_EXEC_POS( locinput, scan, utf8_target );
32fc9b6a 3034 regprop(rex, prop, scan);
07be1b83
YO
3035
3036 PerlIO_printf(Perl_debug_log,
3037 "%3"IVdf":%*s%s(%"IVdf")\n",
f8fc2ecf 3038 (IV)(scan - rexi->program), depth*2, "",
07be1b83 3039 SvPVX_const(prop),
1de06328 3040 (PL_regkind[OP(scan)] == END || !rnext) ?
f8fc2ecf 3041 0 : (IV)(rnext - rexi->program));
2a782b5b 3042 });
d6a28714
JH
3043
3044 next = scan + NEXT_OFF(scan);
3045 if (next == scan)
3046 next = NULL;
40a82448 3047 state_num = OP(scan);
d6a28714 3048
40a82448 3049 reenter_switch:
34a81e2b
B
3050
3051 assert(PL_reglastparen == &rex->lastparen);
3052 assert(PL_reglastcloseparen == &rex->lastcloseparen);
3053 assert(PL_regoffs == rex->offs);
3054
40a82448 3055 switch (state_num) {
d6a28714 3056 case BOL:
7fba1cd6 3057 if (locinput == PL_bostr)
d6a28714 3058 {
3b0527fe 3059 /* reginfo->till = reginfo->bol; */
b8c5462f
JH
3060 break;
3061 }
d6a28714
JH
3062 sayNO;
3063 case MBOL:
12d33761
HS
3064 if (locinput == PL_bostr ||
3065 ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n'))
d6a28714 3066 {
b8c5462f
JH
3067 break;
3068 }
d6a28714
JH
3069 sayNO;
3070 case SBOL:
c2a73568 3071 if (locinput == PL_bostr)
b8c5462f 3072 break;
d6a28714
JH
3073 sayNO;
3074 case GPOS:
3b0527fe 3075 if (locinput == reginfo->ganch)
d6a28714
JH
3076 break;
3077 sayNO;
ee9b8eae
YO
3078
3079 case KEEPS:
3080 /* update the startpoint */
f0ab9afb 3081 st->u.keeper.val = PL_regoffs[0].start;
ee9b8eae 3082 PL_reginput = locinput;
f0ab9afb 3083 PL_regoffs[0].start = locinput - PL_bostr;
ee9b8eae
YO
3084 PUSH_STATE_GOTO(KEEPS_next, next);
3085 /*NOT-REACHED*/
3086 case KEEPS_next_fail:
3087 /* rollback the start point change */
f0ab9afb 3088 PL_regoffs[0].start = st->u.keeper.val;
ee9b8eae
YO
3089 sayNO_SILENT;
3090 /*NOT-REACHED*/
d6a28714 3091 case EOL:
d6a28714
JH
3092 goto seol;
3093 case MEOL:
d6a28714 3094 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
b8c5462f 3095 sayNO;
b8c5462f 3096 break;
d6a28714
JH
3097 case SEOL:
3098 seol:
3099 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
b8c5462f 3100 sayNO;
d6a28714 3101 if (PL_regeol - locinput > 1)
b8c5462f 3102 sayNO;
b8c5462f 3103 break;
d6a28714
JH
3104 case EOS:
3105 if (PL_regeol != locinput)
b8c5462f 3106 sayNO;
d6a28714 3107 break;
ffc61ed2 3108 case SANY:
d6a28714 3109 if (!nextchr && locinput >= PL_regeol)
4633a7c4 3110 sayNO;
f2ed9b32 3111 if (utf8_target) {
f33976b4
DB
3112 locinput += PL_utf8skip[nextchr];
3113 if (locinput > PL_regeol)
3114 sayNO;
3115 nextchr = UCHARAT(locinput);
3116 }
3117 else
3118 nextchr = UCHARAT(++locinput);
3119 break;
3120 case CANY:
3121 if (!nextchr && locinput >= PL_regeol)
3122 sayNO;
b8c5462f 3123 nextchr = UCHARAT(++locinput);
a0d0e21e 3124 break;
ffc61ed2 3125 case REG_ANY:
1aa99e6b
IH
3126 if ((!nextchr && locinput >= PL_regeol) || nextchr == '\n')
3127 sayNO;
f2ed9b32 3128 if (utf8_target) {
b8c5462f 3129 locinput += PL_utf8skip[nextchr];
d6a28714
JH
3130 if (locinput > PL_regeol)
3131 sayNO;
a0ed51b3 3132 nextchr = UCHARAT(locinput);
a0ed51b3 3133 }
1aa99e6b
IH
3134 else
3135 nextchr = UCHARAT(++locinput);
a0ed51b3 3136 break;
166ba7cd
DM
3137
3138#undef ST
3139#define ST st->u.trie
786e8c11
YO
3140 case TRIEC:
3141 /* In this case the charclass data is available inline so
3142 we can fail fast without a lot of extra overhead.
3143 */
f2ed9b32 3144 if (scan->flags == EXACT || !utf8_target) {
786e8c11
YO
3145 if(!ANYOF_BITMAP_TEST(scan, *locinput)) {
3146 DEBUG_EXECUTE_r(
3147 PerlIO_printf(Perl_debug_log,
3148 "%*s %sfailed to match trie start class...%s\n",
5bc10b2c 3149 REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5])
786e8c11
YO
3150 );
3151 sayNO_SILENT;
3152 /* NOTREACHED */
3153 }
3154 }
3155 /* FALL THROUGH */
5b47454d 3156 case TRIE:
2e64971a
DM
3157 /* the basic plan of execution of the trie is:
3158 * At the beginning, run though all the states, and
3159 * find the longest-matching word. Also remember the position
3160 * of the shortest matching word. For example, this pattern:
3161 * 1 2 3 4 5
3162 * ab|a|x|abcd|abc
3163 * when matched against the string "abcde", will generate
3164 * accept states for all words except 3, with the longest
3165 * matching word being 4, and the shortest being 1 (with
3166 * the position being after char 1 of the string).
3167 *
3168 * Then for each matching word, in word order (i.e. 1,2,4,5),
3169 * we run the remainder of the pattern; on each try setting
3170 * the current position to the character following the word,
3171 * returning to try the next word on failure.
3172 *
3173 * We avoid having to build a list of words at runtime by
3174 * using a compile-time structure, wordinfo[].prev, which
3175 * gives, for each word, the previous accepting word (if any).
3176 * In the case above it would contain the mappings 1->2, 2->0,
3177 * 3->0, 4->5, 5->1. We can use this table to generate, from
3178 * the longest word (4 above), a list of all words, by
3179 * following the list of prev pointers; this gives us the
3180 * unordered list 4,5,1,2. Then given the current word we have
3181 * just tried, we can go through the list and find the
3182 * next-biggest word to try (so if we just failed on word 2,
3183 * the next in the list is 4).
3184 *
3185 * Since at runtime we don't record the matching position in
3186 * the string for each word, we have to work that out for
3187 * each word we're about to process. The wordinfo table holds
3188 * the character length of each word; given that we recorded
3189 * at the start: the position of the shortest word and its
3190 * length in chars, we just need to move the pointer the
3191 * difference between the two char lengths. Depending on
3192 * Unicode status and folding, that's cheap or expensive.
3193 *
3194 * This algorithm is optimised for the case where are only a
3195 * small number of accept states, i.e. 0,1, or maybe 2.
3196 * With lots of accepts states, and having to try all of them,
3197 * it becomes quadratic on number of accept states to find all
3198 * the next words.
3199 */
3200
3dab1dad 3201 {
07be1b83 3202 /* what type of TRIE am I? (utf8 makes this contextual) */
a0a388a1 3203 DECL_TRIE_TYPE(scan);
3dab1dad
YO
3204
3205 /* what trie are we using right now */
be8e71aa 3206 reg_trie_data * const trie
f8fc2ecf 3207 = (reg_trie_data*)rexi->data->data[ ARG( scan ) ];
85fbaab2 3208 HV * widecharmap = MUTABLE_HV(rexi->data->data[ ARG( scan ) + 1 ]);
3dab1dad 3209 U32 state = trie->startstate;
166ba7cd 3210
3dab1dad
YO
3211 if (trie->bitmap && trie_type != trie_utf8_fold &&
3212 !TRIE_BITMAP_TEST(trie,*locinput)
3213 ) {
3214 if (trie->states[ state ].wordnum) {
3215 DEBUG_EXECUTE_r(
3216 PerlIO_printf(Perl_debug_log,
3217 "%*s %smatched empty string...%s\n",
5bc10b2c 3218 REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5])
3dab1dad
YO
3219 );
3220 break;
3221 } else {
3222 DEBUG_EXECUTE_r(
3223 PerlIO_printf(Perl_debug_log,
786e8c11 3224 "%*s %sfailed to match trie start class...%s\n",
5bc10b2c 3225 REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5])
3dab1dad
YO
3226 );
3227 sayNO_SILENT;
3228 }
3229 }
166ba7cd 3230
786e8c11
YO
3231 {
3232 U8 *uc = ( U8* )locinput;
3233
3234 STRLEN len = 0;
3235 STRLEN foldlen = 0;
3236 U8 *uscan = (U8*)NULL;
786e8c11 3237 U8 foldbuf[ UTF8_MAXBYTES_CASE + 1 ];
2e64971a
DM
3238 U32 charcount = 0; /* how many input chars we have matched */
3239 U32 accepted = 0; /* have we seen any accepting states? */
786e8c11 3240
786e8c11
YO
3241 ST.B = next;
3242 ST.jump = trie->jump;
786e8c11 3243 ST.me = scan;
2e64971a
DM
3244 ST.firstpos = NULL;
3245 ST.longfold = FALSE; /* char longer if folded => it's harder */
3246 ST.nextword = 0;
3247
3248 /* fully traverse the TRIE; note the position of the
3249 shortest accept state and the wordnum of the longest
3250 accept state */
07be1b83 3251
a3621e74 3252 while ( state && uc <= (U8*)PL_regeol ) {
786e8c11 3253 U32 base = trie->states[ state ].trans.base;
f9f4320a 3254 UV uvc = 0;
acb909b4 3255 U16 charid = 0;
2e64971a
DM
3256 U16 wordnum;
3257 wordnum = trie->states[ state ].wordnum;
3258
3259 if (wordnum) { /* it's an accept state */
3260 if (!accepted) {
3261 accepted = 1;
3262 /* record first match position */
3263 if (ST.longfold) {
3264 ST.firstpos = (U8*)locinput;
3265 ST.firstchars = 0;
5b47454d 3266 }
2e64971a
DM
3267 else {
3268 ST.firstpos = uc;
3269 ST.firstchars = charcount;
3270 }
3271 }
3272 if (!ST.nextword || wordnum < ST.nextword)
3273 ST.nextword = wordnum;
3274 ST.topword = wordnum;
786e8c11 3275 }
a3621e74 3276
07be1b83 3277 DEBUG_TRIE_EXECUTE_r({
f2ed9b32 3278 DUMP_EXEC_POS( (char *)uc, scan, utf8_target );
a3621e74 3279 PerlIO_printf( Perl_debug_log,
2e64971a 3280 "%*s %sState: %4"UVxf" Accepted: %c ",
5bc10b2c 3281 2+depth * 2, "", PL_colors[4],
2e64971a 3282 (UV)state, (accepted ? 'Y' : 'N'));
07be1b83 3283 });
a3621e74 3284
2e64971a 3285 /* read a char and goto next state */
a3621e74 3286 if ( base ) {
6dd2be57 3287 I32 offset;
55eed653
NC
3288 REXEC_TRIE_READ_CHAR(trie_type, trie, widecharmap, uc,
3289 uscan, len, uvc, charid, foldlen,
3290 foldbuf, uniflags);
2e64971a
DM
3291 charcount++;
3292 if (foldlen>0)
3293 ST.longfold = TRUE;
5b47454d 3294 if (charid &&
6dd2be57
DM
3295 ( ((offset =
3296 base + charid - 1 - trie->uniquecharcount)) >= 0)
3297
3298 && ((U32)offset < trie->lasttrans)
3299 && trie->trans[offset].check == state)
5b47454d 3300 {
6dd2be57 3301 state = trie->trans[offset].next;
5b47454d
DM
3302 }
3303 else {
3304 state = 0;
3305 }
3306 uc += len;
3307
3308 }
3309 else {
a3621e74
YO
3310 state = 0;
3311 }
3312 DEBUG_TRIE_EXECUTE_r(
e4584336 3313 PerlIO_printf( Perl_debug_log,
786e8c11 3314 "Charid:%3x CP:%4"UVxf" After State: %4"UVxf"%s\n",
e4584336 3315 charid, uvc, (UV)state, PL_colors[5] );
a3621e74
YO
3316 );
3317 }
2e64971a 3318 if (!accepted)
a3621e74 3319 sayNO;
a3621e74 3320
2e64971a
DM
3321 /* calculate total number of accept states */
3322 {
3323 U16 w = ST.topword;
3324 accepted = 0;
3325 while (w) {
3326 w = trie->wordinfo[w].prev;
3327 accepted++;
3328 }
3329 ST.accepted = accepted;
3330 }
3331
166ba7cd
DM
3332 DEBUG_EXECUTE_r(
3333 PerlIO_printf( Perl_debug_log,
3334 "%*s %sgot %"IVdf" possible matches%s\n",
5bc10b2c 3335 REPORT_CODE_OFF + depth * 2, "",
166ba7cd
DM
3336 PL_colors[4], (IV)ST.accepted, PL_colors[5] );
3337 );
2e64971a 3338 goto trie_first_try; /* jump into the fail handler */
786e8c11 3339 }}
fae667d5 3340 /* NOTREACHED */
2e64971a
DM
3341
3342 case TRIE_next_fail: /* we failed - try next alternative */
fae667d5
YO
3343 if ( ST.jump) {
3344 REGCP_UNWIND(ST.cp);
3345 for (n = *PL_reglastparen; n > ST.lastparen; n--)
f0ab9afb 3346 PL_regoffs[n].end = -1;
fae667d5
YO
3347 *PL_reglastparen = n;
3348 }
2e64971a
DM
3349 if (!--ST.accepted) {
3350 DEBUG_EXECUTE_r({
3351 PerlIO_printf( Perl_debug_log,
3352 "%*s %sTRIE failed...%s\n",
3353 REPORT_CODE_OFF+depth*2, "",
3354 PL_colors[4],
3355 PL_colors[5] );
3356 });
3357 sayNO_SILENT;
3358 }
3359 {
3360 /* Find next-highest word to process. Note that this code
3361 * is O(N^2) per trie run (O(N) per branch), so keep tight */
d9a396a3
DM
3362 register U16 min = 0;
3363 register U16 word;
2e64971a
DM
3364 register U16 const nextword = ST.nextword;
3365 register reg_trie_wordinfo * const wordinfo
3366 = ((reg_trie_data*)rexi->data->data[ARG(ST.me)])->wordinfo;
3367 for (word=ST.topword; word; word=wordinfo[word].prev) {
3368 if (word > nextword && (!min || word < min))
3369 min = word;
3370 }
3371 ST.nextword = min;
3372 }
3373
fae667d5 3374 trie_first_try:
5d458dd8
YO
3375 if (do_cutgroup) {
3376 do_cutgroup = 0;
3377 no_final = 0;
3378 }
fae667d5
YO
3379
3380 if ( ST.jump) {
3381 ST.lastparen = *PL_reglastparen;
3382 REGCP_SET(ST.cp);
2e64971a 3383 }
a3621e74 3384
2e64971a 3385 /* find start char of end of current word */
166ba7cd 3386 {
2e64971a
DM
3387 U32 chars; /* how many chars to skip */
3388 U8 *uc = ST.firstpos;
3389 reg_trie_data * const trie
3390 = (reg_trie_data*)rexi->data->data[ARG(ST.me)];
3391
3392 assert((trie->wordinfo[ST.nextword].len - trie->prefixlen)
3393 >= ST.firstchars);
3394 chars = (trie->wordinfo[ST.nextword].len - trie->prefixlen)
3395 - ST.firstchars;
3396
3397 if (ST.longfold) {
3398 /* the hard option - fold each char in turn and find
3399 * its folded length (which may be different */
3400 U8 foldbuf[UTF8_MAXBYTES_CASE + 1];
3401 STRLEN foldlen;
3402 STRLEN len;
d9a396a3 3403 UV uvc;
2e64971a
DM
3404 U8 *uscan;
3405
3406 while (chars) {
f2ed9b32 3407 if (utf8_target) {
2e64971a
DM
3408 uvc = utf8n_to_uvuni((U8*)uc, UTF8_MAXLEN, &len,
3409 uniflags);
3410 uc += len;
3411 }
3412 else {
3413 uvc = *uc;
3414 uc++;
3415 }
3416 uvc = to_uni_fold(uvc, foldbuf, &foldlen);
3417 uscan = foldbuf;
3418 while (foldlen) {
3419 if (!--chars)
3420 break;
3421 uvc = utf8n_to_uvuni(uscan, UTF8_MAXLEN, &len,
3422 uniflags);
3423 uscan += len;
3424 foldlen -= len;
3425 }
3426 }
a3621e74 3427 }
2e64971a 3428 else {
f2ed9b32 3429 if (utf8_target)
2e64971a
DM
3430 while (chars--)
3431 uc += UTF8SKIP(uc);
3432 else
3433 uc += chars;
3434 }
3435 PL_reginput = (char *)uc;
3436 }
166ba7cd 3437
2e64971a
DM
3438 scan = (ST.jump && ST.jump[ST.nextword])
3439 ? ST.me + ST.jump[ST.nextword]
3440 : ST.B;
166ba7cd 3441
2e64971a
DM
3442 DEBUG_EXECUTE_r({
3443 PerlIO_printf( Perl_debug_log,
3444 "%*s %sTRIE matched word #%d, continuing%s\n",
3445 REPORT_CODE_OFF+depth*2, "",
3446 PL_colors[4],
3447 ST.nextword,
3448 PL_colors[5]
3449 );
3450 });
3451
3452 if (ST.accepted > 1 || has_cutgroup) {
3453 PUSH_STATE_GOTO(TRIE_next, scan);
3454 /* NOTREACHED */
166ba7cd 3455 }
2e64971a
DM
3456 /* only one choice left - just continue */
3457 DEBUG_EXECUTE_r({
3458 AV *const trie_words
3459 = MUTABLE_AV(rexi->data->data[ARG(ST.me)+TRIE_WORDS_OFFSET]);
3460 SV ** const tmp = av_fetch( trie_words,
3461 ST.nextword-1, 0 );
3462 SV *sv= tmp ? sv_newmortal() : NULL;
3463
3464 PerlIO_printf( Perl_debug_log,
3465 "%*s %sonly one match left, short-circuiting: #%d <%s>%s\n",
3466 REPORT_CODE_OFF+depth*2, "", PL_colors[4],
3467 ST.nextword,
3468 tmp ? pv_pretty(sv, SvPV_nolen_const(*tmp), SvCUR(*tmp), 0,
3469 PL_colors[0], PL_colors[1],
3470 (SvUTF8(*tmp) ? PERL_PV_ESCAPE_UNI : 0)
3471 )
3472 : "not compiled under -Dr",
3473 PL_colors[5] );
3474 });
3475
3476 locinput = PL_reginput;
3477 nextchr = UCHARAT(locinput);
3478 continue; /* execute rest of RE */
166ba7cd 3479 /* NOTREACHED */
166ba7cd
DM
3480#undef ST
3481
95b24440
DM
3482 case EXACT: {
3483 char *s = STRING(scan);
24d3c4a9 3484 ln = STR_LEN(scan);
f2ed9b32 3485 if (utf8_target != UTF_PATTERN) {
bc517b45 3486 /* The target and the pattern have differing utf8ness. */
1aa99e6b 3487 char *l = locinput;
24d3c4a9 3488 const char * const e = s + ln;
a72c7584 3489
f2ed9b32 3490 if (utf8_target) {
5ff6fc6d 3491 /* The target is utf8, the pattern is not utf8. */
1aa99e6b 3492 while (s < e) {
a3b680e6 3493 STRLEN ulen;
1aa99e6b 3494 if (l >= PL_regeol)
5ff6fc6d
JH
3495 sayNO;
3496 if (NATIVE_TO_UNI(*(U8*)s) !=
89ebb4a3 3497 utf8n_to_uvuni((U8*)l, UTF8_MAXBYTES, &ulen,
041457d9 3498 uniflags))
5ff6fc6d 3499 sayNO;
bc517b45 3500 l += ulen;
5ff6fc6d 3501 s ++;
1aa99e6b 3502 }
5ff6fc6d
JH
3503 }
3504 else {
3505 /* The target is not utf8, the pattern is utf8. */
1aa99e6b 3506 while (s < e) {
a3b680e6 3507 STRLEN ulen;
1aa99e6b
IH
3508 if (l >= PL_regeol)
3509 sayNO;
5ff6fc6d 3510 if (NATIVE_TO_UNI(*((U8*)l)) !=
89ebb4a3 3511 utf8n_to_uvuni((U8*)s, UTF8_MAXBYTES, &ulen,
041457d9 3512 uniflags))
1aa99e6b 3513 sayNO;
bc517b45 3514 s += ulen;
a72c7584 3515 l ++;
1aa99e6b 3516 }
5ff6fc6d 3517 }
1aa99e6b
IH
3518 locinput = l;
3519 nextchr = UCHARAT(locinput);
3520 break;
3521 }
bc517b45 3522 /* The target and the pattern have the same utf8ness. */
d6a28714
JH
3523 /* Inline the first character, for speed. */
3524 if (UCHARAT(s) != nextchr)
3525 sayNO;
24d3c4a9 3526 if (PL_regeol - locinput < ln)
d6a28714 3527 sayNO;
24d3c4a9 3528 if (ln > 1 && memNE(s, locinput, ln))
d6a28714 3529 sayNO;
24d3c4a9 3530 locinput += ln;
d6a28714
JH
3531 nextchr = UCHARAT(locinput);
3532 break;
95b24440 3533 }
d6a28714 3534 case EXACTFL:
b8c5462f
JH
3535 PL_reg_flags |= RF_tainted;
3536 /* FALL THROUGH */
95b24440 3537 case EXACTF: {
be8e71aa 3538 char * const s = STRING(scan);
24d3c4a9 3539 ln = STR_LEN(scan);
d6a28714 3540
f2ed9b32 3541 if (utf8_target || UTF_PATTERN) {
d07ddd77 3542 /* Either target or the pattern are utf8. */
be8e71aa 3543 const char * const l = locinput;
d07ddd77 3544 char *e = PL_regeol;
bc517b45 3545
f2ed9b32
KW
3546 if (! foldEQ_utf8(s, 0, ln, cBOOL(UTF_PATTERN),
3547 l, &e, 0, utf8_target)) {
5486206c
JH
3548 /* One more case for the sharp s:
3549 * pack("U0U*", 0xDF) =~ /ss/i,
3550 * the 0xC3 0x9F are the UTF-8
3551 * byte sequence for the U+00DF. */
e1d1eefb 3552
f2ed9b32 3553 if (!(utf8_target &&
e1d1eefb 3554 toLOWER(s[0]) == 's' &&
24d3c4a9 3555 ln >= 2 &&
5486206c
JH
3556 toLOWER(s[1]) == 's' &&
3557 (U8)l[0] == 0xC3 &&
3558 e - l >= 2 &&
3559 (U8)l[1] == 0x9F))
3560 sayNO;
3561 }
d07ddd77
JH
3562 locinput = e;
3563 nextchr = UCHARAT(locinput);
3564 break;
a0ed51b3 3565 }
d6a28714 3566
bc517b45
JH
3567 /* Neither the target and the pattern are utf8. */
3568
d6a28714
JH
3569 /* Inline the first character, for speed. */
3570 if (UCHARAT(s) != nextchr &&
3571 UCHARAT(s) != ((OP(scan) == EXACTF)
3572 ? PL_fold : PL_fold_locale)[nextchr])
a0ed51b3 3573 sayNO;
24d3c4a9 3574 if (PL_regeol - locinput < ln)
b8c5462f 3575 sayNO;
24d3c4a9 3576 if (ln > 1 && (OP(scan) == EXACTF
4c1b470c
KW
3577 ? ! foldEQ(s, locinput, ln)
3578 : ! foldEQ_locale(s, locinput, ln)))
4633a7c4 3579 sayNO;
24d3c4a9 3580 locinput += ln;
d6a28714 3581 nextchr = UCHARAT(locinput);
a0d0e21e 3582 break;
95b24440 3583 }
b2680017
YO
3584 case BOUNDL:
3585 case NBOUNDL:
3586 PL_reg_flags |= RF_tainted;
3587 /* FALL THROUGH */
3588 case BOUND:
3589 case NBOUND:
3590 /* was last char in word? */
f2ed9b32 3591 if (utf8_target) {
b2680017
YO
3592 if (locinput == PL_bostr)
3593 ln = '\n';
3594 else {
3595 const U8 * const r = reghop3((U8*)locinput, -1, (U8*)PL_bostr);
3596
3597 ln = utf8n_to_uvchr(r, UTF8SKIP(r), 0, uniflags);
3598 }
3599 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
3600 ln = isALNUM_uni(ln);
3601 LOAD_UTF8_CHARCLASS_ALNUM();
f2ed9b32 3602 n = swash_fetch(PL_utf8_alnum, (U8*)locinput, utf8_target);
b2680017
YO
3603 }
3604 else {
3605 ln = isALNUM_LC_uvchr(UNI_TO_NATIVE(ln));
3606 n = isALNUM_LC_utf8((U8*)locinput);
3607 }
3608 }
3609 else {
3610 ln = (locinput != PL_bostr) ?
3611 UCHARAT(locinput - 1) : '\n';
a12cf05f
KW
3612 if (FLAGS(scan) & USE_UNI) {
3613
3614 /* Here, can't be BOUNDL or NBOUNDL because they never set
3615 * the flags to USE_UNI */
3616 ln = isWORDCHAR_L1(ln);
3617 n = isWORDCHAR_L1(nextchr);
3618 }
3619 else if (OP(scan) == BOUND || OP(scan) == NBOUND) {
b2680017
YO
3620 ln = isALNUM(ln);
3621 n = isALNUM(nextchr);
3622 }
3623 else {
3624 ln = isALNUM_LC(ln);
3625 n = isALNUM_LC(nextchr);
3626 }
3627 }
3628 if (((!ln) == (!n)) == (OP(scan) == BOUND ||
3629 OP(scan) == BOUNDL))
3630 sayNO;
3631 break;
d6a28714 3632 case ANYOF:
f2ed9b32 3633 if (utf8_target) {
9e55ce06 3634 STRLEN inclasslen = PL_regeol - locinput;
20ed0b26
KW
3635 if (locinput >= PL_regeol)
3636 sayNO;
9e55ce06 3637
f2ed9b32 3638 if (!reginclass(rex, scan, (U8*)locinput, &inclasslen, utf8_target))
262b90c4 3639 goto anyof_fail;
b32d7d3e 3640 locinput += inclasslen;
b8c5462f 3641 nextchr = UCHARAT(locinput);
e0f9d4a8 3642 break;
ffc61ed2
JH
3643 }
3644 else {
3645 if (nextchr < 0)
3646 nextchr = UCHARAT(locinput);
ffc61ed2
JH
3647 if (!nextchr && locinput >= PL_regeol)
3648 sayNO;
20ed0b26
KW
3649 if (!REGINCLASS(rex, scan, (U8*)locinput))
3650 goto anyof_fail;
ffc61ed2 3651 nextchr = UCHARAT(++locinput);
e0f9d4a8
JH
3652 break;
3653 }
262b90c4 3654 anyof_fail:
e0f9d4a8
JH
3655 /* If we might have the case of the German sharp s
3656 * in a casefolding Unicode character class. */
3657
ebc501f0
JH
3658 if (ANYOF_FOLD_SHARP_S(scan, locinput, PL_regeol)) {
3659 locinput += SHARP_S_SKIP;
e0f9d4a8 3660 nextchr = UCHARAT(locinput);
ffc61ed2 3661 }
e0f9d4a8
JH
3662 else
3663 sayNO;
b8c5462f 3664 break;
20d0b1e9 3665 /* Special char classes - The defines start on line 129 or so */
a12cf05f
KW
3666 CCC_TRY_AFF_U( ALNUM, ALNUML, perl_word, "a", isALNUM_LC_utf8, isWORDCHAR_L1, isALNUM_LC);
3667 CCC_TRY_NEG_U(NALNUM, NALNUML, perl_word, "a", isALNUM_LC_utf8, isWORDCHAR_L1, isALNUM_LC);
20d0b1e9 3668
a12cf05f
KW
3669 CCC_TRY_AFF_U( SPACE, SPACEL, perl_space, " ", isSPACE_LC_utf8, isSPACE_L1, isSPACE_LC);
3670 CCC_TRY_NEG_U(NSPACE, NSPACEL, perl_space, " ", isSPACE_LC_utf8, isSPACE_L1, isSPACE_LC);
20d0b1e9 3671
d1eb3177
YO
3672 CCC_TRY_AFF( DIGIT, DIGITL, posix_digit, "0", isDIGIT_LC_utf8, isDIGIT, isDIGIT_LC);
3673 CCC_TRY_NEG(NDIGIT, NDIGITL, posix_digit, "0", isDIGIT_LC_utf8, isDIGIT, isDIGIT_LC);
20d0b1e9 3674
37e2e78e
KW
3675 case CLUMP: /* Match \X: logical Unicode character. This is defined as
3676 a Unicode extended Grapheme Cluster */
3677 /* From http://www.unicode.org/reports/tr29 (5.2 version). An
3678 extended Grapheme Cluster is:
3679
3680 CR LF
3681 | Prepend* Begin Extend*
3682 | .
3683
3684 Begin is (Hangul-syllable | ! Control)
3685 Extend is (Grapheme_Extend | Spacing_Mark)
3686 Control is [ GCB_Control CR LF ]
3687
3688 The discussion below shows how the code for CLUMP is derived
3689 from this regex. Note that most of these concepts are from
3690 property values of the Grapheme Cluster Boundary (GCB) property.
3691 No code point can have multiple property values for a given
3692 property. Thus a code point in Prepend can't be in Control, but
3693 it must be in !Control. This is why Control above includes
3694 GCB_Control plus CR plus LF. The latter two are used in the GCB
3695 property separately, and so can't be in GCB_Control, even though
3696 they logically are controls. Control is not the same as gc=cc,
3697 but includes format and other characters as well.
3698
3699 The Unicode definition of Hangul-syllable is:
3700 L+
3701 | (L* ( ( V | LV ) V* | LVT ) T*)
3702 | T+
3703 )
3704 Each of these is a value for the GCB property, and hence must be
3705 disjoint, so the order they are tested is immaterial, so the
3706 above can safely be changed to
3707 T+
3708 | L+
3709 | (L* ( LVT | ( V | LV ) V*) T*)
3710
3711 The last two terms can be combined like this:
3712 L* ( L
3713 | (( LVT | ( V | LV ) V*) T*))
3714
3715 And refactored into this:
3716 L* (L | LVT T* | V V* T* | LV V* T*)
3717
3718 That means that if we have seen any L's at all we can quit
3719 there, but if the next character is a LVT, a V or and LV we
3720 should keep going.
3721
3722 There is a subtlety with Prepend* which showed up in testing.
3723 Note that the Begin, and only the Begin is required in:
3724 | Prepend* Begin Extend*
3725 Also, Begin contains '! Control'. A Prepend must be a '!
3726 Control', which means it must be a Begin. What it comes down to
3727 is that if we match Prepend* and then find no suitable Begin
3728 afterwards, that if we backtrack the last Prepend, that one will
3729 be a suitable Begin.
3730 */
3731
b7c83a7e 3732 if (locinput >= PL_regeol)
a0ed51b3 3733 sayNO;
f2ed9b32 3734 if (! utf8_target) {
37e2e78e
KW
3735
3736 /* Match either CR LF or '.', as all the other possibilities
3737 * require utf8 */
3738 locinput++; /* Match the . or CR */
3739 if (nextchr == '\r'
3740 && locinput < PL_regeol
3741 && UCHARAT(locinput) == '\n') locinput++;
3742 }
3743 else {
3744
3745 /* Utf8: See if is ( CR LF ); already know that locinput <
3746 * PL_regeol, so locinput+1 is in bounds */
3747 if (nextchr == '\r' && UCHARAT(locinput + 1) == '\n') {
3748 locinput += 2;
3749 }
3750 else {
3751 /* In case have to backtrack to beginning, then match '.' */
3752 char *starting = locinput;
3753
3754 /* In case have to backtrack the last prepend */
3755 char *previous_prepend = 0;
3756
3757 LOAD_UTF8_CHARCLASS_GCB();
3758
3759 /* Match (prepend)* */
3760 while (locinput < PL_regeol
3761 && swash_fetch(PL_utf8_X_prepend,
f2ed9b32 3762 (U8*)locinput, utf8_target))
37e2e78e
KW
3763 {
3764 previous_prepend = locinput;
3765 locinput += UTF8SKIP(locinput);
3766 }
3767
3768 /* As noted above, if we matched a prepend character, but
3769 * the next thing won't match, back off the last prepend we
3770 * matched, as it is guaranteed to match the begin */
3771 if (previous_prepend
3772 && (locinput >= PL_regeol
3773 || ! swash_fetch(PL_utf8_X_begin,
f2ed9b32 3774 (U8*)locinput, utf8_target)))
37e2e78e
KW
3775 {
3776 locinput = previous_prepend;
3777 }
3778
3779 /* Note that here we know PL_regeol > locinput, as we
3780 * tested that upon input to this switch case, and if we
3781 * moved locinput forward, we tested the result just above
3782 * and it either passed, or we backed off so that it will
3783 * now pass */
f2ed9b32 3784 if (! swash_fetch(PL_utf8_X_begin, (U8*)locinput, utf8_target)) {
37e2e78e
KW
3785
3786 /* Here did not match the required 'Begin' in the
3787 * second term. So just match the very first
3788 * character, the '.' of the final term of the regex */
3789 locinput = starting + UTF8SKIP(starting);
3790 } else {
3791
3792 /* Here is the beginning of a character that can have
3793 * an extender. It is either a hangul syllable, or a
3794 * non-control */
3795 if (swash_fetch(PL_utf8_X_non_hangul,
f2ed9b32 3796 (U8*)locinput, utf8_target))
37e2e78e
KW
3797 {
3798
3799 /* Here not a Hangul syllable, must be a
3800 * ('! * Control') */
3801 locinput += UTF8SKIP(locinput);
3802 } else {
3803
3804 /* Here is a Hangul syllable. It can be composed
3805 * of several individual characters. One
3806 * possibility is T+ */
3807 if (swash_fetch(PL_utf8_X_T,
f2ed9b32 3808 (U8*)locinput, utf8_target))
37e2e78e
KW
3809 {
3810 while (locinput < PL_regeol
3811 && swash_fetch(PL_utf8_X_T,
f2ed9b32 3812 (U8*)locinput, utf8_target))
37e2e78e
KW
3813 {
3814 locinput += UTF8SKIP(locinput);
3815 }
3816 } else {
3817
3818 /* Here, not T+, but is a Hangul. That means
3819 * it is one of the others: L, LV, LVT or V,
3820 * and matches:
3821 * L* (L | LVT T* | V V* T* | LV V* T*) */
3822
3823 /* Match L* */
3824 while (locinput < PL_regeol
3825 && swash_fetch(PL_utf8_X_L,
f2ed9b32 3826 (U8*)locinput, utf8_target))
37e2e78e
KW
3827 {
3828 locinput += UTF8SKIP(locinput);
3829 }
3830
3831 /* Here, have exhausted L*. If the next
3832 * character is not an LV, LVT nor V, it means
3833 * we had to have at least one L, so matches L+
3834 * in the original equation, we have a complete
3835 * hangul syllable. Are done. */
3836
3837 if (locinput < PL_regeol
3838 && swash_fetch(PL_utf8_X_LV_LVT_V,
f2ed9b32 3839 (U8*)locinput, utf8_target))
37e2e78e
KW
3840 {
3841
3842 /* Otherwise keep going. Must be LV, LVT
3843 * or V. See if LVT */
3844 if (swash_fetch(PL_utf8_X_LVT,
f2ed9b32 3845 (U8*)locinput, utf8_target))
37e2e78e
KW
3846 {
3847 locinput += UTF8SKIP(locinput);
3848 } else {
3849
3850 /* Must be V or LV. Take it, then
3851 * match V* */
3852 locinput += UTF8SKIP(locinput);
3853 while (locinput < PL_regeol
3854 && swash_fetch(PL_utf8_X_V,
f2ed9b32 3855 (U8*)locinput, utf8_target))
37e2e78e
KW
3856 {
3857 locinput += UTF8SKIP(locinput);
3858 }
3859 }
3860
3861 /* And any of LV, LVT, or V can be followed
3862 * by T* */
3863 while (locinput < PL_regeol
3864 && swash_fetch(PL_utf8_X_T,
3865 (U8*)locinput,
f2ed9b32 3866 utf8_target))
37e2e78e
KW
3867 {
3868 locinput += UTF8SKIP(locinput);
3869 }
3870 }
3871 }
3872 }
3873
3874 /* Match any extender */
3875 while (locinput < PL_regeol
3876 && swash_fetch(PL_utf8_X_extend,
f2ed9b32 3877 (U8*)locinput, utf8_target))
37e2e78e
KW
3878 {
3879 locinput += UTF8SKIP(locinput);
3880 }
3881 }
3882 }
3883 if (locinput > PL_regeol) sayNO;
3884 }
a0ed51b3
LW
3885 nextchr = UCHARAT(locinput);
3886 break;
81714fb9
YO
3887
3888 case NREFFL:
3889 {
3890 char *s;
ff1157ca 3891 char type;
81714fb9
YO
3892 PL_reg_flags |= RF_tainted;
3893 /* FALL THROUGH */
3894 case NREF:
3895 case NREFF:
ff1157ca 3896 type = OP(scan);
0a4db386
YO
3897 n = reg_check_named_buff_matched(rex,scan);
3898
3899 if ( n ) {
3900 type = REF + ( type - NREF );
3901 goto do_ref;
3902 } else {
81714fb9 3903 sayNO;
0a4db386
YO
3904 }
3905 /* unreached */
c8756f30 3906 case REFFL:
3280af22 3907 PL_reg_flags |= RF_tainted;
c8756f30 3908 /* FALL THROUGH */
c277df42 3909 case REF:
81714fb9 3910 case REFF:
c277df42 3911 n = ARG(scan); /* which paren pair */
81714fb9
YO
3912 type = OP(scan);
3913 do_ref:
f0ab9afb 3914 ln = PL_regoffs[n].start;
2c2d71f5 3915 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
3b6647e0 3916 if (*PL_reglastparen < n || ln == -1)
af3f8c16 3917 sayNO; /* Do not match unless seen CLOSEn. */
f0ab9afb 3918 if (ln == PL_regoffs[n].end)
a0d0e21e 3919 break;
a0ed51b3 3920
24d3c4a9 3921 s = PL_bostr + ln;
f2ed9b32 3922 if (utf8_target && type != REF) { /* REF can do byte comparison */
a0ed51b3 3923 char *l = locinput;
f0ab9afb 3924 const char *e = PL_bostr + PL_regoffs[n].end;
a0ed51b3
LW
3925 /*
3926 * Note that we can't do the "other character" lookup trick as
3927 * in the 8-bit case (no pun intended) because in Unicode we
3928 * have to map both upper and title case to lower case.
3929 */
81714fb9 3930 if (type == REFF) {
a0ed51b3 3931 while (s < e) {
a3b680e6
AL
3932 STRLEN ulen1, ulen2;
3933 U8 tmpbuf1[UTF8_MAXBYTES_CASE+1];
3934 U8 tmpbuf2[UTF8_MAXBYTES_CASE+1];
3935
a0ed51b3
LW
3936 if (l >= PL_regeol)
3937 sayNO;
a2a2844f
JH
3938 toLOWER_utf8((U8*)s, tmpbuf1, &ulen1);
3939 toLOWER_utf8((U8*)l, tmpbuf2, &ulen2);
7114a2d2 3940 if (ulen1 != ulen2 || memNE((char *)tmpbuf1, (char *)tmpbuf2, ulen1))
a0ed51b3 3941 sayNO;
a2a2844f
JH
3942 s += ulen1;
3943 l += ulen2;
a0ed51b3
LW
3944 }
3945 }
3946 locinput = l;
3947 nextchr = UCHARAT(locinput);
3948 break;
3949 }
3950
a0d0e21e 3951 /* Inline the first character, for speed. */
76e3520e 3952 if (UCHARAT(s) != nextchr &&
81714fb9
YO
3953 (type == REF ||
3954 (UCHARAT(s) != (type == REFF
3955 ? PL_fold : PL_fold_locale)[nextchr])))
4633a7c4 3956 sayNO;
f0ab9afb 3957 ln = PL_regoffs[n].end - ln;
24d3c4a9 3958 if (locinput + ln > PL_regeol)
4633a7c4 3959 sayNO;
81714fb9 3960 if (ln > 1 && (type == REF
24d3c4a9 3961 ? memNE(s, locinput, ln)
81714fb9 3962 : (type == REFF
4c1b470c
KW
3963 ? ! foldEQ(s, locinput, ln)
3964 : ! foldEQ_locale(s, locinput, ln))))
4633a7c4 3965 sayNO;
24d3c4a9 3966 locinput += ln;
76e3520e 3967 nextchr = UCHARAT(locinput);
a0d0e21e 3968 break;
81714fb9 3969 }
a0d0e21e 3970 case NOTHING:
c277df42 3971 case TAIL:
a0d0e21e
LW
3972 break;
3973 case BACK:
3974 break;
40a82448
DM
3975
3976#undef ST
3977#define ST st->u.eval
c277df42 3978 {
c277df42 3979 SV *ret;
d2f13c59 3980 REGEXP *re_sv;
6bda09f9 3981 regexp *re;
f8fc2ecf 3982 regexp_internal *rei;
1a147d38
YO
3983 regnode *startpoint;
3984
3985 case GOSTART:
e7707071
YO
3986 case GOSUB: /* /(...(?1))/ /(...(?&foo))/ */
3987 if (cur_eval && cur_eval->locinput==locinput) {
24b23f37 3988 if (cur_eval->u.eval.close_paren == (U32)ARG(scan))
1a147d38 3989 Perl_croak(aTHX_ "Infinite recursion in regex");
4b196cd4 3990 if ( ++nochange_depth > max_nochange_depth )
1a147d38
YO
3991 Perl_croak(aTHX_
3992 "Pattern subroutine nesting without pos change"
3993 " exceeded limit in regex");
6bda09f9
YO
3994 } else {
3995 nochange_depth = 0;
1a147d38 3996 }
288b8c02 3997 re_sv = rex_sv;
6bda09f9 3998 re = rex;
f8fc2ecf 3999 rei = rexi;
288b8c02 4000 (void)ReREFCNT_inc(rex_sv);
1a147d38 4001 if (OP(scan)==GOSUB) {
6bda09f9
YO
4002 startpoint = scan + ARG2L(scan);
4003 ST.close_paren = ARG(scan);
4004 } else {
f8fc2ecf 4005 startpoint = rei->program+1;
6bda09f9
YO
4006 ST.close_paren = 0;
4007 }
4008 goto eval_recurse_doit;
4009 /* NOTREACHED */
4010 case EVAL: /* /(?{A})B/ /(??{A})B/ and /(?(?{A})X|Y)B/ */
4011 if (cur_eval && cur_eval->locinput==locinput) {
4b196cd4 4012 if ( ++nochange_depth > max_nochange_depth )
1a147d38 4013 Perl_croak(aTHX_ "EVAL without pos change exceeded limit in regex");
6bda09f9
YO
4014 } else {
4015 nochange_depth = 0;
4016 }
8e5e9ebe 4017 {
4aabdb9b
DM
4018 /* execute the code in the {...} */
4019 dSP;
6136c704 4020 SV ** const before = SP;
4aabdb9b
DM
4021 OP_4tree * const oop = PL_op;
4022 COP * const ocurcop = PL_curcop;
4023 PAD *old_comppad;
d80618d2 4024 char *saved_regeol = PL_regeol;
91332126
FR
4025 struct re_save_state saved_state;
4026
6562f1c4 4027 /* To not corrupt the existing regex state while executing the
b7f4cd04
FR
4028 * eval we would normally put it on the save stack, like with
4029 * save_re_context. However, re-evals have a weird scoping so we
4030 * can't just add ENTER/LEAVE here. With that, things like
4031 *
4032 * (?{$a=2})(a(?{local$a=$a+1}))*aak*c(?{$b=$a})
4033 *
4034 * would break, as they expect the localisation to be unwound
4035 * only when the re-engine backtracks through the bit that
4036 * localised it.
4037 *
4038 * What we do instead is just saving the state in a local c
4039 * variable.
4040 */
91332126
FR
4041 Copy(&PL_reg_state, &saved_state, 1, struct re_save_state);
4042
4aabdb9b 4043 n = ARG(scan);
f8fc2ecf 4044 PL_op = (OP_4tree*)rexi->data->data[n];
24b23f37
YO
4045 DEBUG_STATE_r( PerlIO_printf(Perl_debug_log,
4046 " re_eval 0x%"UVxf"\n", PTR2UV(PL_op)) );
f8fc2ecf 4047 PAD_SAVE_LOCAL(old_comppad, (PAD*)rexi->data->data[n + 2]);
f0ab9afb 4048 PL_regoffs[0].end = PL_reg_magic->mg_len = locinput - PL_bostr;
4aabdb9b 4049
2bf803e2
YO
4050 if (sv_yes_mark) {
4051 SV *sv_mrk = get_sv("REGMARK", 1);
4052 sv_setsv(sv_mrk, sv_yes_mark);
4053 }
4054
8e5e9ebe
RGS
4055 CALLRUNOPS(aTHX); /* Scalar context. */
4056 SPAGAIN;
4057 if (SP == before)
075aa684 4058 ret = &PL_sv_undef; /* protect against empty (?{}) blocks. */
8e5e9ebe
RGS
4059 else {
4060 ret = POPs;
4061 PUTBACK;
4062 }
4aabdb9b 4063
91332126
FR
4064 Copy(&saved_state, &PL_reg_state, 1, struct re_save_state);
4065
4aabdb9b
DM
4066 PL_op = oop;
4067 PAD_RESTORE_LOCAL(old_comppad);
4068 PL_curcop = ocurcop;
d80618d2 4069 PL_regeol = saved_regeol;
24d3c4a9 4070 if (!logical) {
4aabdb9b
DM
4071 /* /(?{...})/ */
4072 sv_setsv(save_scalar(PL_replgv), ret);
4aabdb9b
DM
4073 break;
4074 }
8e5e9ebe 4075 }
24d3c4a9
DM
4076 if (logical == 2) { /* Postponed subexpression: /(??{...})/ */
4077 logical = 0;
4aabdb9b 4078 {
4f639d21
DM
4079 /* extract RE object from returned value; compiling if
4080 * necessary */
6136c704 4081 MAGIC *mg = NULL;
288b8c02 4082 REGEXP *rx = NULL;
5c35adbb
NC
4083
4084 if (SvROK(ret)) {
288b8c02 4085 SV *const sv = SvRV(ret);
5c35adbb
NC
4086
4087 if (SvTYPE(sv) == SVt_REGEXP) {
d2f13c59 4088 rx = (REGEXP*) sv;
5c35adbb
NC
4089 } else if (SvSMAGICAL(sv)) {
4090 mg = mg_find(sv, PERL_MAGIC_qr);
4091 assert(mg);
4092 }
4093 } else if (SvTYPE(ret) == SVt_REGEXP) {
d2f13c59 4094 rx = (REGEXP*) ret;
5c35adbb 4095 } else if (SvSMAGICAL(ret)) {
124ee91a
NC
4096 if (SvGMAGICAL(ret)) {
4097 /* I don't believe that there is ever qr magic
4098 here. */
4099 assert(!mg_find(ret, PERL_MAGIC_qr));
faf82a0b 4100 sv_unmagic(ret, PERL_MAGIC_qr);
124ee91a
NC
4101 }
4102 else {
faf82a0b 4103 mg = mg_find(ret, PERL_MAGIC_qr);
124ee91a
NC
4104 /* testing suggests mg only ends up non-NULL for
4105 scalars who were upgraded and compiled in the
4106 else block below. In turn, this is only
4107 triggered in the "postponed utf8 string" tests
4108 in t/op/pat.t */
4109 }
0f5d15d6 4110 }
faf82a0b 4111
0f5d15d6 4112 if (mg) {
d2f13c59 4113 rx = (REGEXP *) mg->mg_obj; /*XXX:dmq*/
e2560c33 4114 assert(rx);
0f5d15d6 4115 }
288b8c02 4116 if (rx) {
f0826785 4117 rx = reg_temp_copy(NULL, rx);
288b8c02 4118 }
0f5d15d6 4119 else {
c737faaf 4120 U32 pm_flags = 0;
a3b680e6 4121 const I32 osize = PL_regsize;
0f5d15d6 4122
b9ad30b4
NC
4123 if (DO_UTF8(ret)) {
4124 assert (SvUTF8(ret));
4125 } else if (SvUTF8(ret)) {
4126 /* Not doing UTF-8, despite what the SV says. Is
4127 this only if we're trapped in use 'bytes'? */
4128 /* Make a copy of the octet sequence, but without
4129 the flag on, as the compiler now honours the
4130 SvUTF8 flag on ret. */
4131 STRLEN len;
4132 const char *const p = SvPV(ret, len);
4133 ret = newSVpvn_flags(p, len, SVs_TEMP);
4134 }
288b8c02 4135 rx = CALLREGCOMP(ret, pm_flags);
9041c2e3 4136 if (!(SvFLAGS(ret)
faf82a0b 4137 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY
3ce3ed55 4138 | SVs_GMG))) {
a2794585
NC
4139 /* This isn't a first class regexp. Instead, it's
4140 caching a regexp onto an existing, Perl visible
4141 scalar. */
ad64d0ec 4142 sv_magic(ret, MUTABLE_SV(rx), PERL_MAGIC_qr, 0, 0);
3ce3ed55 4143 }
0f5d15d6 4144 PL_regsize = osize;
0f5d15d6 4145 }
288b8c02
NC
4146 re_sv = rx;
4147 re = (struct regexp *)SvANY(rx);
4aabdb9b 4148 }
07bc277f 4149 RXp_MATCH_COPIED_off(re);
28d8d7f4
YO
4150 re->subbeg = rex->subbeg;
4151 re->sublen = rex->sublen;
f8fc2ecf 4152 rei = RXi_GET(re);
6bda09f9 4153 DEBUG_EXECUTE_r(
f2ed9b32 4154 debug_start_match(re_sv, utf8_target, locinput, PL_regeol,
6bda09f9
YO
4155 "Matching embedded");
4156 );
f8fc2ecf 4157 startpoint = rei->program + 1;
1a147d38 4158 ST.close_paren = 0; /* only used for GOSUB */
6bda09f9
YO
4159 /* borrowed from regtry */
4160 if (PL_reg_start_tmpl <= re->nparens) {
4161 PL_reg_start_tmpl = re->nparens*3/2 + 3;
4162 if(PL_reg_start_tmp)
4163 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
4164 else
4165 Newx(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
dd5def09 4166 }
aa283a38 4167
1a147d38 4168 eval_recurse_doit: /* Share code with GOSUB below this line */
aa283a38 4169 /* run the pattern returned from (??{...}) */
40a82448
DM
4170 ST.cp = regcppush(0); /* Save *all* the positions. */
4171 REGCP_SET(ST.lastcp);
6bda09f9 4172
f0ab9afb 4173 PL_regoffs = re->offs; /* essentially NOOP on GOSUB */
6bda09f9 4174
0357f1fd
ML
4175 /* see regtry, specifically PL_reglast(?:close)?paren is a pointer! (i dont know why) :dmq */
4176 PL_reglastparen = &re->lastparen;
4177 PL_reglastcloseparen = &re->lastcloseparen;
4178 re->lastparen = 0;
4179 re->lastcloseparen = 0;
4180
4aabdb9b 4181 PL_reginput = locinput;
ae0beba1 4182 PL_regsize = 0;
4aabdb9b
DM
4183
4184 /* XXXX This is too dramatic a measure... */
4185 PL_reg_maxiter = 0;
4186
faec1544 4187 ST.toggle_reg_flags = PL_reg_flags;
3c8556c3 4188 if (RX_UTF8(re_sv))
faec1544
DM
4189 PL_reg_flags |= RF_utf8;
4190 else
4191 PL_reg_flags &= ~RF_utf8;
4192 ST.toggle_reg_flags ^= PL_reg_flags; /* diff of old and new */
4193
288b8c02 4194 ST.prev_rex = rex_sv;
faec1544 4195 ST.prev_curlyx = cur_curlyx;
288b8c02
NC
4196 SETREX(rex_sv,re_sv);
4197 rex = re;
f8fc2ecf 4198 rexi = rei;
faec1544 4199 cur_curlyx = NULL;
40a82448 4200 ST.B = next;
faec1544
DM
4201 ST.prev_eval = cur_eval;
4202 cur_eval = st;
faec1544 4203 /* now continue from first node in postoned RE */
6bda09f9 4204 PUSH_YES_STATE_GOTO(EVAL_AB, startpoint);
4aabdb9b 4205 /* NOTREACHED */
a0ed51b3 4206 }
24d3c4a9 4207 /* logical is 1, /(?(?{...})X|Y)/ */
f2338a2e 4208 sw = cBOOL(SvTRUE(ret));
24d3c4a9 4209 logical = 0;
c277df42
IZ
4210 break;
4211 }
40a82448 4212
faec1544
DM
4213 case EVAL_AB: /* cleanup after a successful (??{A})B */
4214 /* note: this is called twice; first after popping B, then A */
4215 PL_reg_flags ^= ST.toggle_reg_flags;
288b8c02
NC
4216 ReREFCNT_dec(rex_sv);
4217 SETREX(rex_sv,ST.prev_rex);
4218 rex = (struct regexp *)SvANY(rex_sv);
f8fc2ecf 4219 rexi = RXi_GET(rex);
faec1544
DM
4220 regcpblow(ST.cp);
4221 cur_eval = ST.prev_eval;
4222 cur_curlyx = ST.prev_curlyx;
34a81e2b
B
4223
4224 /* rex was changed so update the pointer in PL_reglastparen and PL_reglastcloseparen */
0357f1fd
ML
4225 PL_reglastparen = &rex->lastparen;
4226 PL_reglastcloseparen = &rex->lastcloseparen;
34a81e2b
B
4227 /* also update PL_regoffs */
4228 PL_regoffs = rex->offs;
0357f1fd 4229
40a82448
DM
4230 /* XXXX This is too dramatic a measure... */
4231 PL_reg_maxiter = 0;
e7707071 4232 if ( nochange_depth )
4b196cd4 4233 nochange_depth--;
262b90c4 4234 sayYES;
40a82448 4235
40a82448 4236
faec1544
DM
4237 case EVAL_AB_fail: /* unsuccessfully ran A or B in (??{A})B */
4238 /* note: this is called twice; first after popping B, then A */
4239 PL_reg_flags ^= ST.toggle_reg_flags;
288b8c02
NC
4240 ReREFCNT_dec(rex_sv);
4241 SETREX(rex_sv,ST.prev_rex);
4242 rex = (struct regexp *)SvANY(rex_sv);
f8fc2ecf 4243 rexi = RXi_GET(rex);
34a81e2b 4244 /* rex was changed so update the pointer in PL_reglastparen and PL_reglastcloseparen */
0357f1fd
ML
4245 PL_reglastparen = &rex->lastparen;
4246 PL_reglastcloseparen = &rex->lastcloseparen;
4247
40a82448
DM
4248 PL_reginput = locinput;
4249 REGCP_UNWIND(ST.lastcp);
4250 regcppop(rex);
faec1544
DM
4251 cur_eval = ST.prev_eval;
4252 cur_curlyx = ST.prev_curlyx;
4253 /* XXXX This is too dramatic a measure... */
4254 PL_reg_maxiter = 0;
e7707071 4255 if ( nochange_depth )
4b196cd4 4256 nochange_depth--;
40a82448 4257 sayNO_SILENT;
40a82448
DM
4258#undef ST
4259
a0d0e21e 4260 case OPEN:
c277df42 4261 n = ARG(scan); /* which paren pair */
3280af22
NIS
4262 PL_reg_start_tmp[n] = locinput;
4263 if (n > PL_regsize)
4264 PL_regsize = n;
e2e6a0f1 4265 lastopen = n;
a0d0e21e
LW
4266 break;
4267 case CLOSE:
c277df42 4268 n = ARG(scan); /* which paren pair */
f0ab9afb
NC
4269 PL_regoffs[n].start = PL_reg_start_tmp[n] - PL_bostr;
4270 PL_regoffs[n].end = locinput - PL_bostr;
7f69552c
YO
4271 /*if (n > PL_regsize)
4272 PL_regsize = n;*/
3b6647e0 4273 if (n > *PL_reglastparen)
3280af22 4274 *PL_reglastparen = n;
a01268b5 4275 *PL_reglastcloseparen = n;
3b6647e0 4276 if (cur_eval && cur_eval->u.eval.close_paren == n) {
6bda09f9
YO
4277 goto fake_end;
4278 }
a0d0e21e 4279 break;
e2e6a0f1
YO
4280 case ACCEPT:
4281 if (ARG(scan)){
4282 regnode *cursor;
4283 for (cursor=scan;
4284 cursor && OP(cursor)!=END;
4285 cursor=regnext(cursor))
4286 {
4287 if ( OP(cursor)==CLOSE ){
4288 n = ARG(cursor);
4289 if ( n <= lastopen ) {
f0ab9afb
NC
4290 PL_regoffs[n].start
4291 = PL_reg_start_tmp[n] - PL_bostr;
4292 PL_regoffs[n].end = locinput - PL_bostr;
e2e6a0f1
YO
4293 /*if (n > PL_regsize)
4294 PL_regsize = n;*/
3b6647e0 4295 if (n > *PL_reglastparen)
e2e6a0f1
YO
4296 *PL_reglastparen = n;
4297 *PL_reglastcloseparen = n;
3b6647e0
RB
4298 if ( n == ARG(scan) || (cur_eval &&
4299 cur_eval->u.eval.close_paren == n))
e2e6a0f1
YO
4300 break;
4301 }
4302 }
4303 }
4304 }
4305 goto fake_end;
4306 /*NOTREACHED*/
c277df42
IZ
4307 case GROUPP:
4308 n = ARG(scan); /* which paren pair */
f2338a2e 4309 sw = cBOOL(*PL_reglastparen >= n && PL_regoffs[n].end != -1);
c277df42 4310 break;
0a4db386
YO
4311 case NGROUPP:
4312 /* reg_check_named_buff_matched returns 0 for no match */
f2338a2e 4313 sw = cBOOL(0 < reg_check_named_buff_matched(rex,scan));
0a4db386 4314 break;
1a147d38 4315 case INSUBP:
0a4db386 4316 n = ARG(scan);
3b6647e0 4317 sw = (cur_eval && (!n || cur_eval->u.eval.close_paren == n));
0a4db386
YO
4318 break;
4319 case DEFINEP:
4320 sw = 0;
4321 break;
c277df42 4322 case IFTHEN:
2c2d71f5 4323 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
24d3c4a9 4324 if (sw)
c277df42
IZ
4325 next = NEXTOPER(NEXTOPER(scan));
4326 else {
4327 next = scan + ARG(scan);
4328 if (OP(next) == IFTHEN) /* Fake one. */
4329 next = NEXTOPER(NEXTOPER(next));
4330 }
4331 break;
4332 case LOGICAL:
24d3c4a9 4333 logical = scan->flags;
c277df42 4334 break;
c476f425 4335
2ab05381 4336/*******************************************************************
2ab05381 4337
c476f425
DM
4338The CURLYX/WHILEM pair of ops handle the most generic case of the /A*B/
4339pattern, where A and B are subpatterns. (For simple A, CURLYM or
4340STAR/PLUS/CURLY/CURLYN are used instead.)
2ab05381 4341
c476f425 4342A*B is compiled as <CURLYX><A><WHILEM><B>
2ab05381 4343
c476f425
DM
4344On entry to the subpattern, CURLYX is called. This pushes a CURLYX
4345state, which contains the current count, initialised to -1. It also sets
4346cur_curlyx to point to this state, with any previous value saved in the
4347state block.
2ab05381 4348
c476f425
DM
4349CURLYX then jumps straight to the WHILEM op, rather than executing A,
4350since the pattern may possibly match zero times (i.e. it's a while {} loop
4351rather than a do {} while loop).
2ab05381 4352
c476f425
DM
4353Each entry to WHILEM represents a successful match of A. The count in the
4354CURLYX block is incremented, another WHILEM state is pushed, and execution
4355passes to A or B depending on greediness and the current count.
2ab05381 4356
c476f425
DM
4357For example, if matching against the string a1a2a3b (where the aN are
4358substrings that match /A/), then the match progresses as follows: (the
4359pushed states are interspersed with the bits of strings matched so far):
2ab05381 4360
c476f425
DM
4361 <CURLYX cnt=-1>
4362 <CURLYX cnt=0><WHILEM>
4363 <CURLYX cnt=1><WHILEM> a1 <WHILEM>
4364 <CURLYX cnt=2><WHILEM> a1 <WHILEM> a2 <WHILEM>
4365 <CURLYX cnt=3><WHILEM> a1 <WHILEM> a2 <WHILEM> a3 <WHILEM>
4366 <CURLYX cnt=3><WHILEM> a1 <WHILEM> a2 <WHILEM> a3 <WHILEM> b
2ab05381 4367
c476f425
DM
4368(Contrast this with something like CURLYM, which maintains only a single
4369backtrack state:
2ab05381 4370
c476f425
DM
4371 <CURLYM cnt=0> a1
4372 a1 <CURLYM cnt=1> a2
4373 a1 a2 <CURLYM cnt=2> a3
4374 a1 a2 a3 <CURLYM cnt=3> b
4375)
2ab05381 4376
c476f425
DM
4377Each WHILEM state block marks a point to backtrack to upon partial failure
4378of A or B, and also contains some minor state data related to that
4379iteration. The CURLYX block, pointed to by cur_curlyx, contains the
4380overall state, such as the count, and pointers to the A and B ops.
2ab05381 4381
c476f425
DM
4382This is complicated slightly by nested CURLYX/WHILEM's. Since cur_curlyx
4383must always point to the *current* CURLYX block, the rules are:
2ab05381 4384
c476f425
DM
4385When executing CURLYX, save the old cur_curlyx in the CURLYX state block,
4386and set cur_curlyx to point the new block.
2ab05381 4387
c476f425
DM
4388When popping the CURLYX block after a successful or unsuccessful match,
4389restore the previous cur_curlyx.
2ab05381 4390
c476f425
DM
4391When WHILEM is about to execute B, save the current cur_curlyx, and set it
4392to the outer one saved in the CURLYX block.
2ab05381 4393
c476f425
DM
4394When popping the WHILEM block after a successful or unsuccessful B match,
4395restore the previous cur_curlyx.
2ab05381 4396
c476f425
DM
4397Here's an example for the pattern (AI* BI)*BO
4398I and O refer to inner and outer, C and W refer to CURLYX and WHILEM:
2ab05381 4399
c476f425
DM
4400cur_
4401curlyx backtrack stack
4402------ ---------------
4403NULL
4404CO <CO prev=NULL> <WO>
4405CI <CO prev=NULL> <WO> <CI prev=CO> <WI> ai
4406CO <CO prev=NULL> <WO> <CI prev=CO> <WI> ai <WI prev=CI> bi
4407NULL <CO prev=NULL> <WO> <CI prev=CO> <WI> ai <WI prev=CI> bi <WO prev=CO> bo
2ab05381 4408
c476f425
DM
4409At this point the pattern succeeds, and we work back down the stack to
4410clean up, restoring as we go:
95b24440 4411
c476f425
DM
4412CO <CO prev=NULL> <WO> <CI prev=CO> <WI> ai <WI prev=CI> bi
4413CI <CO prev=NULL> <WO> <CI prev=CO> <WI> ai
4414CO <CO prev=NULL> <WO>
4415NULL
a0374537 4416
c476f425
DM
4417*******************************************************************/
4418
4419#define ST st->u.curlyx
4420
4421 case CURLYX: /* start of /A*B/ (for complex A) */
4422 {
4423 /* No need to save/restore up to this paren */
4424 I32 parenfloor = scan->flags;
4425
4426 assert(next); /* keep Coverity happy */
4427 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
4428 next += ARG(next);
4429
4430 /* XXXX Probably it is better to teach regpush to support
4431 parenfloor > PL_regsize... */
4432 if (parenfloor > (I32)*PL_reglastparen)
4433 parenfloor = *PL_reglastparen; /* Pessimization... */
4434
4435 ST.prev_curlyx= cur_curlyx;
4436 cur_curlyx = st;
4437 ST.cp = PL_savestack_ix;
4438
4439 /* these fields contain the state of the current curly.
4440 * they are accessed by subsequent WHILEMs */
4441 ST.parenfloor = parenfloor;
d02d6d97 4442 ST.me = scan;
c476f425 4443 ST.B = next;
24d3c4a9
DM
4444 ST.minmod = minmod;
4445 minmod = 0;
c476f425
DM
4446 ST.count = -1; /* this will be updated by WHILEM */
4447 ST.lastloc = NULL; /* this will be updated by WHILEM */
4448
4449 PL_reginput = locinput;
4450 PUSH_YES_STATE_GOTO(CURLYX_end, PREVOPER(next));
5f66b61c 4451 /* NOTREACHED */
c476f425 4452 }
a0d0e21e 4453
c476f425 4454 case CURLYX_end: /* just finished matching all of A*B */
c476f425
DM
4455 cur_curlyx = ST.prev_curlyx;
4456 sayYES;
4457 /* NOTREACHED */
a0d0e21e 4458
c476f425
DM
4459 case CURLYX_end_fail: /* just failed to match all of A*B */
4460 regcpblow(ST.cp);
4461 cur_curlyx = ST.prev_curlyx;
4462 sayNO;
4463 /* NOTREACHED */
4633a7c4 4464
a0d0e21e 4465
c476f425
DM
4466#undef ST
4467#define ST st->u.whilem
4468
4469 case WHILEM: /* just matched an A in /A*B/ (for complex A) */
4470 {
4471 /* see the discussion above about CURLYX/WHILEM */
c476f425 4472 I32 n;
d02d6d97
DM
4473 int min = ARG1(cur_curlyx->u.curlyx.me);
4474 int max = ARG2(cur_curlyx->u.curlyx.me);
4475 regnode *A = NEXTOPER(cur_curlyx->u.curlyx.me) + EXTRA_STEP_2ARGS;
4476
c476f425
DM
4477 assert(cur_curlyx); /* keep Coverity happy */
4478 n = ++cur_curlyx->u.curlyx.count; /* how many A's matched */
4479 ST.save_lastloc = cur_curlyx->u.curlyx.lastloc;
4480 ST.cache_offset = 0;
4481 ST.cache_mask = 0;
4482
4483 PL_reginput = locinput;
4484
4485 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
d02d6d97
DM
4486 "%*s whilem: matched %ld out of %d..%d\n",
4487 REPORT_CODE_OFF+depth*2, "", (long)n, min, max)
c476f425 4488 );
a0d0e21e 4489
c476f425 4490 /* First just match a string of min A's. */
a0d0e21e 4491
d02d6d97 4492 if (n < min) {
c476f425 4493 cur_curlyx->u.curlyx.lastloc = locinput;
d02d6d97 4494 PUSH_STATE_GOTO(WHILEM_A_pre, A);
c476f425
DM
4495 /* NOTREACHED */
4496 }
4497
4498 /* If degenerate A matches "", assume A done. */
4499
4500 if (locinput == cur_curlyx->u.curlyx.lastloc) {
4501 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
4502 "%*s whilem: empty match detected, trying continuation...\n",
4503 REPORT_CODE_OFF+depth*2, "")
4504 );
4505 goto do_whilem_B_max;
4506 }
4507
4508 /* super-linear cache processing */
4509
4510 if (scan->flags) {
a0d0e21e 4511
2c2d71f5 4512 if (!PL_reg_maxiter) {
c476f425
DM
4513 /* start the countdown: Postpone detection until we
4514 * know the match is not *that* much linear. */
2c2d71f5 4515 PL_reg_maxiter = (PL_regeol - PL_bostr + 1) * (scan->flags>>4);
66bf836d
DM
4516 /* possible overflow for long strings and many CURLYX's */
4517 if (PL_reg_maxiter < 0)
4518 PL_reg_maxiter = I32_MAX;
2c2d71f5
JH
4519 PL_reg_leftiter = PL_reg_maxiter;
4520 }
c476f425 4521
2c2d71f5 4522 if (PL_reg_leftiter-- == 0) {
c476f425 4523 /* initialise cache */
3298f257 4524 const I32 size = (PL_reg_maxiter + 7)/8;
2c2d71f5 4525 if (PL_reg_poscache) {
eb160463 4526 if ((I32)PL_reg_poscache_size < size) {
2c2d71f5
JH
4527 Renew(PL_reg_poscache, size, char);
4528 PL_reg_poscache_size = size;
4529 }
4530 Zero(PL_reg_poscache, size, char);
4531 }
4532 else {
4533 PL_reg_poscache_size = size;
a02a5408 4534 Newxz(PL_reg_poscache, size, char);
2c2d71f5 4535 }
c476f425
DM
4536 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
4537 "%swhilem: Detected a super-linear match, switching on caching%s...\n",
4538 PL_colors[4], PL_colors[5])
4539 );
2c2d71f5 4540 }
c476f425 4541
2c2d71f5 4542 if (PL_reg_leftiter < 0) {
c476f425
DM
4543 /* have we already failed at this position? */
4544 I32 offset, mask;
4545 offset = (scan->flags & 0xf) - 1
4546 + (locinput - PL_bostr) * (scan->flags>>4);
4547 mask = 1 << (offset % 8);
4548 offset /= 8;
4549 if (PL_reg_poscache[offset] & mask) {
4550 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
4551 "%*s whilem: (cache) already tried at this position...\n",
4552 REPORT_CODE_OFF+depth*2, "")
2c2d71f5 4553 );
3298f257 4554 sayNO; /* cache records failure */
2c2d71f5 4555 }
c476f425
DM
4556 ST.cache_offset = offset;
4557 ST.cache_mask = mask;
2c2d71f5 4558 }
c476f425 4559 }
2c2d71f5 4560
c476f425 4561 /* Prefer B over A for minimal matching. */
a687059c 4562
c476f425
DM
4563 if (cur_curlyx->u.curlyx.minmod) {
4564 ST.save_curlyx = cur_curlyx;
4565 cur_curlyx = cur_curlyx->u.curlyx.prev_curlyx;
4566 ST.cp = regcppush(ST.save_curlyx->u.curlyx.parenfloor);
4567 REGCP_SET(ST.lastcp);
4568 PUSH_YES_STATE_GOTO(WHILEM_B_min, ST.save_curlyx->u.curlyx.B);
4569 /* NOTREACHED */
4570 }
a0d0e21e 4571
c476f425
DM
4572 /* Prefer A over B for maximal matching. */
4573
d02d6d97 4574 if (n < max) { /* More greed allowed? */
c476f425
DM
4575 ST.cp = regcppush(cur_curlyx->u.curlyx.parenfloor);
4576 cur_curlyx->u.curlyx.lastloc = locinput;
4577 REGCP_SET(ST.lastcp);
d02d6d97 4578 PUSH_STATE_GOTO(WHILEM_A_max, A);
c476f425
DM
4579 /* NOTREACHED */
4580 }
4581 goto do_whilem_B_max;
4582 }
4583 /* NOTREACHED */
4584
4585 case WHILEM_B_min: /* just matched B in a minimal match */
4586 case WHILEM_B_max: /* just matched B in a maximal match */
4587 cur_curlyx = ST.save_curlyx;
4588 sayYES;
4589 /* NOTREACHED */
4590
4591 case WHILEM_B_max_fail: /* just failed to match B in a maximal match */
4592 cur_curlyx = ST.save_curlyx;
4593 cur_curlyx->u.curlyx.lastloc = ST.save_lastloc;
4594 cur_curlyx->u.curlyx.count--;
4595 CACHEsayNO;
4596 /* NOTREACHED */
4597
4598 case WHILEM_A_min_fail: /* just failed to match A in a minimal match */
4599 REGCP_UNWIND(ST.lastcp);
4600 regcppop(rex);
4601 /* FALL THROUGH */
4602 case WHILEM_A_pre_fail: /* just failed to match even minimal A */
4603 cur_curlyx->u.curlyx.lastloc = ST.save_lastloc;
4604 cur_curlyx->u.curlyx.count--;
4605 CACHEsayNO;
4606 /* NOTREACHED */
4607
4608 case WHILEM_A_max_fail: /* just failed to match A in a maximal match */
4609 REGCP_UNWIND(ST.lastcp);
4610 regcppop(rex); /* Restore some previous $<digit>s? */
4611 PL_reginput = locinput;
4612 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
4613 "%*s whilem: failed, trying continuation...\n",
4614 REPORT_CODE_OFF+depth*2, "")
4615 );
4616 do_whilem_B_max:
4617 if (cur_curlyx->u.curlyx.count >= REG_INFTY
4618 && ckWARN(WARN_REGEXP)
4619 && !(PL_reg_flags & RF_warned))
4620 {
4621 PL_reg_flags |= RF_warned;
4622 Perl_warner(aTHX_ packWARN(WARN_REGEXP), "%s limit (%d) exceeded",
4623 "Complex regular subexpression recursion",
4624 REG_INFTY - 1);
4625 }
4626
4627 /* now try B */
4628 ST.save_curlyx = cur_curlyx;
4629 cur_curlyx = cur_curlyx->u.curlyx.prev_curlyx;
4630 PUSH_YES_STATE_GOTO(WHILEM_B_max, ST.save_curlyx->u.curlyx.B);
4631 /* NOTREACHED */
4632
4633 case WHILEM_B_min_fail: /* just failed to match B in a minimal match */
4634 cur_curlyx = ST.save_curlyx;
4635 REGCP_UNWIND(ST.lastcp);
4636 regcppop(rex);
4637
d02d6d97 4638 if (cur_curlyx->u.curlyx.count >= /*max*/ARG2(cur_curlyx->u.curlyx.me)) {
c476f425
DM
4639 /* Maximum greed exceeded */
4640 if (cur_curlyx->u.curlyx.count >= REG_INFTY
4641 && ckWARN(WARN_REGEXP)
4642 && !(PL_reg_flags & RF_warned))
4643 {
3280af22 4644 PL_reg_flags |= RF_warned;
c476f425
DM
4645 Perl_warner(aTHX_ packWARN(WARN_REGEXP),
4646 "%s limit (%d) exceeded",
4647 "Complex regular subexpression recursion",
4648 REG_INFTY - 1);
a0d0e21e 4649 }
c476f425 4650 cur_curlyx->u.curlyx.count--;
3ab3c9b4 4651 CACHEsayNO;
a0d0e21e 4652 }
c476f425
DM
4653
4654 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
4655 "%*s trying longer...\n", REPORT_CODE_OFF+depth*2, "")
4656 );
4657 /* Try grabbing another A and see if it helps. */
4658 PL_reginput = locinput;
4659 cur_curlyx->u.curlyx.lastloc = locinput;
4660 ST.cp = regcppush(cur_curlyx->u.curlyx.parenfloor);
4661 REGCP_SET(ST.lastcp);
d02d6d97
DM
4662 PUSH_STATE_GOTO(WHILEM_A_min,
4663 /*A*/ NEXTOPER(ST.save_curlyx->u.curlyx.me) + EXTRA_STEP_2ARGS);
5f66b61c 4664 /* NOTREACHED */
40a82448
DM
4665
4666#undef ST
4667#define ST st->u.branch
4668
4669 case BRANCHJ: /* /(...|A|...)/ with long next pointer */
c277df42
IZ
4670 next = scan + ARG(scan);
4671 if (next == scan)
4672 next = NULL;
40a82448
DM
4673 scan = NEXTOPER(scan);
4674 /* FALL THROUGH */
c277df42 4675
40a82448
DM
4676 case BRANCH: /* /(...|A|...)/ */
4677 scan = NEXTOPER(scan); /* scan now points to inner node */
40a82448
DM
4678 ST.lastparen = *PL_reglastparen;
4679 ST.next_branch = next;
4680 REGCP_SET(ST.cp);
4681 PL_reginput = locinput;
02db2b7b 4682
40a82448 4683 /* Now go into the branch */
5d458dd8
YO
4684 if (has_cutgroup) {
4685 PUSH_YES_STATE_GOTO(BRANCH_next, scan);
4686 } else {
4687 PUSH_STATE_GOTO(BRANCH_next, scan);
4688 }
40a82448 4689 /* NOTREACHED */
5d458dd8
YO
4690 case CUTGROUP:
4691 PL_reginput = locinput;
4692 sv_yes_mark = st->u.mark.mark_name = scan->flags ? NULL :
ad64d0ec 4693 MUTABLE_SV(rexi->data->data[ ARG( scan ) ]);
5d458dd8
YO
4694 PUSH_STATE_GOTO(CUTGROUP_next,next);
4695 /* NOTREACHED */
4696 case CUTGROUP_next_fail:
4697 do_cutgroup = 1;
4698 no_final = 1;
4699 if (st->u.mark.mark_name)
4700 sv_commit = st->u.mark.mark_name;
4701 sayNO;
4702 /* NOTREACHED */
4703 case BRANCH_next:
4704 sayYES;
4705 /* NOTREACHED */
40a82448 4706 case BRANCH_next_fail: /* that branch failed; try the next, if any */
5d458dd8
YO
4707 if (do_cutgroup) {
4708 do_cutgroup = 0;
4709 no_final = 0;
4710 }
40a82448
DM
4711 REGCP_UNWIND(ST.cp);
4712 for (n = *PL_reglastparen; n > ST.lastparen; n--)
f0ab9afb 4713 PL_regoffs[n].end = -1;
40a82448 4714 *PL_reglastparen = n;
0a4db386 4715 /*dmq: *PL_reglastcloseparen = n; */
40a82448
DM
4716 scan = ST.next_branch;
4717 /* no more branches? */
5d458dd8
YO
4718 if (!scan || (OP(scan) != BRANCH && OP(scan) != BRANCHJ)) {
4719 DEBUG_EXECUTE_r({
4720 PerlIO_printf( Perl_debug_log,
4721 "%*s %sBRANCH failed...%s\n",
4722 REPORT_CODE_OFF+depth*2, "",
4723 PL_colors[4],
4724 PL_colors[5] );
4725 });
4726 sayNO_SILENT;
4727 }
40a82448
DM
4728 continue; /* execute next BRANCH[J] op */
4729 /* NOTREACHED */
4730
a0d0e21e 4731 case MINMOD:
24d3c4a9 4732 minmod = 1;
a0d0e21e 4733 break;
40a82448
DM
4734
4735#undef ST
4736#define ST st->u.curlym
4737
4738 case CURLYM: /* /A{m,n}B/ where A is fixed-length */
4739
4740 /* This is an optimisation of CURLYX that enables us to push
84d2fa14 4741 * only a single backtracking state, no matter how many matches
40a82448
DM
4742 * there are in {m,n}. It relies on the pattern being constant
4743 * length, with no parens to influence future backrefs
4744 */
4745
4746 ST.me = scan;
dc45a647 4747 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
40a82448
DM
4748
4749 /* if paren positive, emulate an OPEN/CLOSE around A */
4750 if (ST.me->flags) {
3b6647e0 4751 U32 paren = ST.me->flags;
40a82448
DM
4752 if (paren > PL_regsize)
4753 PL_regsize = paren;
3b6647e0 4754 if (paren > *PL_reglastparen)
40a82448 4755 *PL_reglastparen = paren;
c277df42 4756 scan += NEXT_OFF(scan); /* Skip former OPEN. */
6407bf3b 4757 }
40a82448
DM
4758 ST.A = scan;
4759 ST.B = next;
4760 ST.alen = 0;
4761 ST.count = 0;
24d3c4a9
DM
4762 ST.minmod = minmod;
4763 minmod = 0;
40a82448
DM
4764 ST.c1 = CHRTEST_UNINIT;
4765 REGCP_SET(ST.cp);
6407bf3b 4766
40a82448
DM
4767 if (!(ST.minmod ? ARG1(ST.me) : ARG2(ST.me))) /* min/max */
4768 goto curlym_do_B;
4769
4770 curlym_do_A: /* execute the A in /A{m,n}B/ */
6407bf3b 4771 PL_reginput = locinput;
40a82448
DM
4772 PUSH_YES_STATE_GOTO(CURLYM_A, ST.A); /* match A */
4773 /* NOTREACHED */
5f80c4cf 4774
40a82448
DM
4775 case CURLYM_A: /* we've just matched an A */
4776 locinput = st->locinput;
4777 nextchr = UCHARAT(locinput);
4778
4779 ST.count++;
4780 /* after first match, determine A's length: u.curlym.alen */
4781 if (ST.count == 1) {
4782 if (PL_reg_match_utf8) {
4783 char *s = locinput;
4784 while (s < PL_reginput) {
4785 ST.alen++;
4786 s += UTF8SKIP(s);
4787 }
4788 }
4789 else {
4790 ST.alen = PL_reginput - locinput;
4791 }
4792 if (ST.alen == 0)
4793 ST.count = ST.minmod ? ARG1(ST.me) : ARG2(ST.me);
4794 }
0cadcf80
DM
4795 DEBUG_EXECUTE_r(
4796 PerlIO_printf(Perl_debug_log,
40a82448 4797 "%*s CURLYM now matched %"IVdf" times, len=%"IVdf"...\n",
5bc10b2c 4798 (int)(REPORT_CODE_OFF+(depth*2)), "",
40a82448 4799 (IV) ST.count, (IV)ST.alen)
0cadcf80
DM
4800 );
4801
40a82448 4802 locinput = PL_reginput;
0a4db386
YO
4803
4804 if (cur_eval && cur_eval->u.eval.close_paren &&
24b23f37 4805 cur_eval->u.eval.close_paren == (U32)ST.me->flags)
0a4db386
YO
4806 goto fake_end;
4807
c966426a
DM
4808 {
4809 I32 max = (ST.minmod ? ARG1(ST.me) : ARG2(ST.me));
4810 if ( max == REG_INFTY || ST.count < max )
4811 goto curlym_do_A; /* try to match another A */
4812 }
40a82448 4813 goto curlym_do_B; /* try to match B */
5f80c4cf 4814
40a82448
DM
4815 case CURLYM_A_fail: /* just failed to match an A */
4816 REGCP_UNWIND(ST.cp);
0a4db386
YO
4817
4818 if (ST.minmod || ST.count < ARG1(ST.me) /* min*/
4819 || (cur_eval && cur_eval->u.eval.close_paren &&
24b23f37 4820 cur_eval->u.eval.close_paren == (U32)ST.me->flags))
40a82448 4821 sayNO;
0cadcf80 4822
40a82448
DM
4823 curlym_do_B: /* execute the B in /A{m,n}B/ */
4824 PL_reginput = locinput;
4825 if (ST.c1 == CHRTEST_UNINIT) {
4826 /* calculate c1 and c2 for possible match of 1st char
4827 * following curly */
4828 ST.c1 = ST.c2 = CHRTEST_VOID;
4829 if (HAS_TEXT(ST.B) || JUMPABLE(ST.B)) {
4830 regnode *text_node = ST.B;
4831 if (! HAS_TEXT(text_node))
4832 FIND_NEXT_IMPT(text_node);
ee9b8eae
YO
4833 /* this used to be
4834
4835 (HAS_TEXT(text_node) && PL_regkind[OP(text_node)] == EXACT)
4836
4837 But the former is redundant in light of the latter.
4838
4839 if this changes back then the macro for
4840 IS_TEXT and friends need to change.
4841 */
4842 if (PL_regkind[OP(text_node)] == EXACT)
40a82448 4843 {
ee9b8eae 4844
40a82448
DM
4845 ST.c1 = (U8)*STRING(text_node);
4846 ST.c2 =
ee9b8eae 4847 (IS_TEXTF(text_node))
40a82448 4848 ? PL_fold[ST.c1]
ee9b8eae 4849 : (IS_TEXTFL(text_node))
40a82448
DM
4850 ? PL_fold_locale[ST.c1]
4851 : ST.c1;
c277df42 4852 }
c277df42 4853 }
40a82448
DM
4854 }
4855
4856 DEBUG_EXECUTE_r(
4857 PerlIO_printf(Perl_debug_log,
4858 "%*s CURLYM trying tail with matches=%"IVdf"...\n",
5bc10b2c 4859 (int)(REPORT_CODE_OFF+(depth*2)),
40a82448
DM
4860 "", (IV)ST.count)
4861 );
4862 if (ST.c1 != CHRTEST_VOID
4863 && UCHARAT(PL_reginput) != ST.c1
4864 && UCHARAT(PL_reginput) != ST.c2)
4865 {
4866 /* simulate B failing */
3e901dc0
YO
4867 DEBUG_OPTIMISE_r(
4868 PerlIO_printf(Perl_debug_log,
4869 "%*s CURLYM Fast bail c1=%"IVdf" c2=%"IVdf"\n",
4870 (int)(REPORT_CODE_OFF+(depth*2)),"",
4871 (IV)ST.c1,(IV)ST.c2
4872 ));
40a82448
DM
4873 state_num = CURLYM_B_fail;
4874 goto reenter_switch;
4875 }
4876
4877 if (ST.me->flags) {
4878 /* mark current A as captured */
4879 I32 paren = ST.me->flags;
4880 if (ST.count) {
f0ab9afb 4881 PL_regoffs[paren].start
40a82448 4882 = HOPc(PL_reginput, -ST.alen) - PL_bostr;
f0ab9afb 4883 PL_regoffs[paren].end = PL_reginput - PL_bostr;
0a4db386 4884 /*dmq: *PL_reglastcloseparen = paren; */
c277df42 4885 }
40a82448 4886 else
f0ab9afb 4887 PL_regoffs[paren].end = -1;
0a4db386 4888 if (cur_eval && cur_eval->u.eval.close_paren &&
24b23f37 4889 cur_eval->u.eval.close_paren == (U32)ST.me->flags)
0a4db386
YO
4890 {
4891 if (ST.count)
4892 goto fake_end;
4893 else
4894 sayNO;
4895 }
c277df42 4896 }
0a4db386 4897
40a82448 4898 PUSH_STATE_GOTO(CURLYM_B, ST.B); /* match B */
5f66b61c 4899 /* NOTREACHED */
40a82448
DM
4900
4901 case CURLYM_B_fail: /* just failed to match a B */
4902 REGCP_UNWIND(ST.cp);
4903 if (ST.minmod) {
84d2fa14
HS
4904 I32 max = ARG2(ST.me);
4905 if (max != REG_INFTY && ST.count == max)
40a82448
DM
4906 sayNO;
4907 goto curlym_do_A; /* try to match a further A */
4908 }
4909 /* backtrack one A */
4910 if (ST.count == ARG1(ST.me) /* min */)
4911 sayNO;
4912 ST.count--;
4913 locinput = HOPc(locinput, -ST.alen);
4914 goto curlym_do_B; /* try to match B */
4915
c255a977
DM
4916#undef ST
4917#define ST st->u.curly
40a82448 4918
c255a977
DM
4919#define CURLY_SETPAREN(paren, success) \
4920 if (paren) { \
4921 if (success) { \
f0ab9afb
NC
4922 PL_regoffs[paren].start = HOPc(locinput, -1) - PL_bostr; \
4923 PL_regoffs[paren].end = locinput - PL_bostr; \
0a4db386 4924 *PL_reglastcloseparen = paren; \
c255a977
DM
4925 } \
4926 else \
f0ab9afb 4927 PL_regoffs[paren].end = -1; \
c255a977
DM
4928 }
4929
4930 case STAR: /* /A*B/ where A is width 1 */
4931 ST.paren = 0;
4932 ST.min = 0;
4933 ST.max = REG_INFTY;
a0d0e21e
LW
4934 scan = NEXTOPER(scan);
4935 goto repeat;
c255a977
DM
4936 case PLUS: /* /A+B/ where A is width 1 */
4937 ST.paren = 0;
4938 ST.min = 1;
4939 ST.max = REG_INFTY;
c277df42 4940 scan = NEXTOPER(scan);
c255a977
DM
4941 goto repeat;
4942 case CURLYN: /* /(A){m,n}B/ where A is width 1 */
4943 ST.paren = scan->flags; /* Which paren to set */
4944 if (ST.paren > PL_regsize)
4945 PL_regsize = ST.paren;
3b6647e0 4946 if (ST.paren > *PL_reglastparen)
c255a977
DM
4947 *PL_reglastparen = ST.paren;
4948 ST.min = ARG1(scan); /* min to match */
4949 ST.max = ARG2(scan); /* max to match */
0a4db386 4950 if (cur_eval && cur_eval->u.eval.close_paren &&
86413ec0 4951 cur_eval->u.eval.close_paren == (U32)ST.paren) {
0a4db386
YO
4952 ST.min=1;
4953 ST.max=1;
4954 }
c255a977
DM
4955 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
4956 goto repeat;
4957 case CURLY: /* /A{m,n}B/ where A is width 1 */
4958 ST.paren = 0;
4959 ST.min = ARG1(scan); /* min to match */
4960 ST.max = ARG2(scan); /* max to match */
4961 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
c277df42 4962 repeat:
a0d0e21e
LW
4963 /*
4964 * Lookahead to avoid useless match attempts
4965 * when we know what character comes next.
c255a977 4966 *
5f80c4cf
JP
4967 * Used to only do .*x and .*?x, but now it allows
4968 * for )'s, ('s and (?{ ... })'s to be in the way
4969 * of the quantifier and the EXACT-like node. -- japhy
4970 */
4971
c255a977
DM
4972 if (ST.min > ST.max) /* XXX make this a compile-time check? */
4973 sayNO;
cca55fe3 4974 if (HAS_TEXT(next) || JUMPABLE(next)) {
5f80c4cf
JP
4975 U8 *s;
4976 regnode *text_node = next;
4977
3dab1dad
YO
4978 if (! HAS_TEXT(text_node))
4979 FIND_NEXT_IMPT(text_node);
5f80c4cf 4980
9e137952 4981 if (! HAS_TEXT(text_node))
c255a977 4982 ST.c1 = ST.c2 = CHRTEST_VOID;
5f80c4cf 4983 else {
ee9b8eae 4984 if ( PL_regkind[OP(text_node)] != EXACT ) {
c255a977 4985 ST.c1 = ST.c2 = CHRTEST_VOID;
44a68960 4986 goto assume_ok_easy;
cca55fe3 4987 }
be8e71aa
YO
4988 else
4989 s = (U8*)STRING(text_node);
ee9b8eae
YO
4990
4991 /* Currently we only get here when
4992
4993 PL_rekind[OP(text_node)] == EXACT
4994
4995 if this changes back then the macro for IS_TEXT and
4996 friends need to change. */
f2ed9b32 4997 if (!UTF_PATTERN) {
c255a977 4998 ST.c2 = ST.c1 = *s;
ee9b8eae 4999 if (IS_TEXTF(text_node))
c255a977 5000 ST.c2 = PL_fold[ST.c1];
ee9b8eae 5001 else if (IS_TEXTFL(text_node))
c255a977 5002 ST.c2 = PL_fold_locale[ST.c1];
1aa99e6b 5003 }
f2ed9b32 5004 else { /* UTF_PATTERN */
ee9b8eae 5005 if (IS_TEXTF(text_node)) {
a2a2844f 5006 STRLEN ulen1, ulen2;
89ebb4a3
JH
5007 U8 tmpbuf1[UTF8_MAXBYTES_CASE+1];
5008 U8 tmpbuf2[UTF8_MAXBYTES_CASE+1];
a2a2844f
JH
5009
5010 to_utf8_lower((U8*)s, tmpbuf1, &ulen1);
5011 to_utf8_upper((U8*)s, tmpbuf2, &ulen2);
e294cc5d
JH
5012#ifdef EBCDIC
5013 ST.c1 = utf8n_to_uvchr(tmpbuf1, UTF8_MAXLEN, 0,
5014 ckWARN(WARN_UTF8) ?
5015 0 : UTF8_ALLOW_ANY);
5016 ST.c2 = utf8n_to_uvchr(tmpbuf2, UTF8_MAXLEN, 0,
5017 ckWARN(WARN_UTF8) ?
5018 0 : UTF8_ALLOW_ANY);
5019#else
c255a977 5020 ST.c1 = utf8n_to_uvuni(tmpbuf1, UTF8_MAXBYTES, 0,
e294cc5d 5021 uniflags);
c255a977 5022 ST.c2 = utf8n_to_uvuni(tmpbuf2, UTF8_MAXBYTES, 0,
e294cc5d
JH
5023 uniflags);
5024#endif
5f80c4cf
JP
5025 }
5026 else {
c255a977 5027 ST.c2 = ST.c1 = utf8n_to_uvchr(s, UTF8_MAXBYTES, 0,
041457d9 5028 uniflags);
5f80c4cf 5029 }
1aa99e6b
IH
5030 }
5031 }
bbce6d69 5032 }
a0d0e21e 5033 else
c255a977 5034 ST.c1 = ST.c2 = CHRTEST_VOID;
cca55fe3 5035 assume_ok_easy:
c255a977
DM
5036
5037 ST.A = scan;
5038 ST.B = next;
3280af22 5039 PL_reginput = locinput;
24d3c4a9
DM
5040 if (minmod) {
5041 minmod = 0;
e2e6a0f1 5042 if (ST.min && regrepeat(rex, ST.A, ST.min, depth) < ST.min)
4633a7c4 5043 sayNO;
c255a977 5044 ST.count = ST.min;
a0ed51b3 5045 locinput = PL_reginput;
c255a977
DM
5046 REGCP_SET(ST.cp);
5047 if (ST.c1 == CHRTEST_VOID)
5048 goto curly_try_B_min;
5049
5050 ST.oldloc = locinput;
5051
5052 /* set ST.maxpos to the furthest point along the
5053 * string that could possibly match */
5054 if (ST.max == REG_INFTY) {
5055 ST.maxpos = PL_regeol - 1;
f2ed9b32 5056 if (utf8_target)
c255a977
DM
5057 while (UTF8_IS_CONTINUATION(*(U8*)ST.maxpos))
5058 ST.maxpos--;
5059 }
f2ed9b32 5060 else if (utf8_target) {
c255a977
DM
5061 int m = ST.max - ST.min;
5062 for (ST.maxpos = locinput;
5063 m >0 && ST.maxpos + UTF8SKIP(ST.maxpos) <= PL_regeol; m--)
5064 ST.maxpos += UTF8SKIP(ST.maxpos);
5065 }
5066 else {
5067 ST.maxpos = locinput + ST.max - ST.min;
5068 if (ST.maxpos >= PL_regeol)
5069 ST.maxpos = PL_regeol - 1;
5070 }
5071 goto curly_try_B_min_known;
5072
5073 }
5074 else {
e2e6a0f1 5075 ST.count = regrepeat(rex, ST.A, ST.max, depth);
c255a977
DM
5076 locinput = PL_reginput;
5077 if (ST.count < ST.min)
5078 sayNO;
5079 if ((ST.count > ST.min)
5080 && (PL_regkind[OP(ST.B)] == EOL) && (OP(ST.B) != MEOL))
5081 {
5082 /* A{m,n} must come at the end of the string, there's
5083 * no point in backing off ... */
5084 ST.min = ST.count;
5085 /* ...except that $ and \Z can match before *and* after
5086 newline at the end. Consider "\n\n" =~ /\n+\Z\n/.
5087 We may back off by one in this case. */
5088 if (UCHARAT(PL_reginput - 1) == '\n' && OP(ST.B) != EOS)
5089 ST.min--;
5090 }
5091 REGCP_SET(ST.cp);
5092 goto curly_try_B_max;
5093 }
5094 /* NOTREACHED */
5095
5096
5097 case CURLY_B_min_known_fail:
5098 /* failed to find B in a non-greedy match where c1,c2 valid */
5099 if (ST.paren && ST.count)
f0ab9afb 5100 PL_regoffs[ST.paren].end = -1;
c255a977
DM
5101
5102 PL_reginput = locinput; /* Could be reset... */
5103 REGCP_UNWIND(ST.cp);
5104 /* Couldn't or didn't -- move forward. */
5105 ST.oldloc = locinput;
f2ed9b32 5106 if (utf8_target)
c255a977
DM
5107 locinput += UTF8SKIP(locinput);
5108 else
5109 locinput++;
5110 ST.count++;
5111 curly_try_B_min_known:
5112 /* find the next place where 'B' could work, then call B */
5113 {
5114 int n;
f2ed9b32 5115 if (utf8_target) {
c255a977
DM
5116 n = (ST.oldloc == locinput) ? 0 : 1;
5117 if (ST.c1 == ST.c2) {
5118 STRLEN len;
5119 /* set n to utf8_distance(oldloc, locinput) */
5120 while (locinput <= ST.maxpos &&
5121 utf8n_to_uvchr((U8*)locinput,
5122 UTF8_MAXBYTES, &len,
5123 uniflags) != (UV)ST.c1) {
5124 locinput += len;
5125 n++;
5126 }
1aa99e6b
IH
5127 }
5128 else {
c255a977
DM
5129 /* set n to utf8_distance(oldloc, locinput) */
5130 while (locinput <= ST.maxpos) {
5131 STRLEN len;
5132 const UV c = utf8n_to_uvchr((U8*)locinput,
5133 UTF8_MAXBYTES, &len,
5134 uniflags);
5135 if (c == (UV)ST.c1 || c == (UV)ST.c2)
5136 break;
5137 locinput += len;
5138 n++;
1aa99e6b 5139 }
0fe9bf95
IZ
5140 }
5141 }
c255a977
DM
5142 else {
5143 if (ST.c1 == ST.c2) {
5144 while (locinput <= ST.maxpos &&
5145 UCHARAT(locinput) != ST.c1)
5146 locinput++;
bbce6d69 5147 }
c255a977
DM
5148 else {
5149 while (locinput <= ST.maxpos
5150 && UCHARAT(locinput) != ST.c1
5151 && UCHARAT(locinput) != ST.c2)
5152 locinput++;
a0ed51b3 5153 }
c255a977
DM
5154 n = locinput - ST.oldloc;
5155 }
5156 if (locinput > ST.maxpos)
5157 sayNO;
5158 /* PL_reginput == oldloc now */
5159 if (n) {
5160 ST.count += n;
e2e6a0f1 5161 if (regrepeat(rex, ST.A, n, depth) < n)
4633a7c4 5162 sayNO;
a0d0e21e 5163 }
c255a977
DM
5164 PL_reginput = locinput;
5165 CURLY_SETPAREN(ST.paren, ST.count);
0a4db386 5166 if (cur_eval && cur_eval->u.eval.close_paren &&
86413ec0 5167 cur_eval->u.eval.close_paren == (U32)ST.paren) {
0a4db386
YO
5168 goto fake_end;
5169 }
c255a977 5170 PUSH_STATE_GOTO(CURLY_B_min_known, ST.B);
a0d0e21e 5171 }
c255a977
DM
5172 /* NOTREACHED */
5173
5174
5175 case CURLY_B_min_fail:
5176 /* failed to find B in a non-greedy match where c1,c2 invalid */
5177 if (ST.paren && ST.count)
f0ab9afb 5178 PL_regoffs[ST.paren].end = -1;
c255a977
DM
5179
5180 REGCP_UNWIND(ST.cp);
5181 /* failed -- move forward one */
5182 PL_reginput = locinput;
e2e6a0f1 5183 if (regrepeat(rex, ST.A, 1, depth)) {
c255a977 5184 ST.count++;
a0ed51b3 5185 locinput = PL_reginput;
c255a977
DM
5186 if (ST.count <= ST.max || (ST.max == REG_INFTY &&
5187 ST.count > 0)) /* count overflow ? */
15272685 5188 {
c255a977
DM
5189 curly_try_B_min:
5190 CURLY_SETPAREN(ST.paren, ST.count);
0a4db386 5191 if (cur_eval && cur_eval->u.eval.close_paren &&
86413ec0 5192 cur_eval->u.eval.close_paren == (U32)ST.paren) {
0a4db386
YO
5193 goto fake_end;
5194 }
c255a977 5195 PUSH_STATE_GOTO(CURLY_B_min, ST.B);
a0d0e21e
LW
5196 }
5197 }
4633a7c4 5198 sayNO;
c255a977
DM
5199 /* NOTREACHED */
5200
5201
5202 curly_try_B_max:
5203 /* a successful greedy match: now try to match B */
40d049e4 5204 if (cur_eval && cur_eval->u.eval.close_paren &&
86413ec0 5205 cur_eval->u.eval.close_paren == (U32)ST.paren) {
40d049e4
YO
5206 goto fake_end;
5207 }
c255a977
DM
5208 {
5209 UV c = 0;
5210 if (ST.c1 != CHRTEST_VOID)
f2ed9b32 5211 c = utf8_target ? utf8n_to_uvchr((U8*)PL_reginput,
c255a977 5212 UTF8_MAXBYTES, 0, uniflags)
466787eb 5213 : (UV) UCHARAT(PL_reginput);
c255a977
DM
5214 /* If it could work, try it. */
5215 if (ST.c1 == CHRTEST_VOID || c == (UV)ST.c1 || c == (UV)ST.c2) {
5216 CURLY_SETPAREN(ST.paren, ST.count);
5217 PUSH_STATE_GOTO(CURLY_B_max, ST.B);
5218 /* NOTREACHED */
5219 }
5220 }
5221 /* FALL THROUGH */
5222 case CURLY_B_max_fail:
5223 /* failed to find B in a greedy match */
5224 if (ST.paren && ST.count)
f0ab9afb 5225 PL_regoffs[ST.paren].end = -1;
c255a977
DM
5226
5227 REGCP_UNWIND(ST.cp);
5228 /* back up. */
5229 if (--ST.count < ST.min)
5230 sayNO;
5231 PL_reginput = locinput = HOPc(locinput, -1);
5232 goto curly_try_B_max;
5233
5234#undef ST
5235
a0d0e21e 5236 case END:
6bda09f9 5237 fake_end:
faec1544
DM
5238 if (cur_eval) {
5239 /* we've just finished A in /(??{A})B/; now continue with B */
5240 I32 tmpix;
faec1544
DM
5241 st->u.eval.toggle_reg_flags
5242 = cur_eval->u.eval.toggle_reg_flags;
5243 PL_reg_flags ^= st->u.eval.toggle_reg_flags;
5244
288b8c02
NC
5245 st->u.eval.prev_rex = rex_sv; /* inner */
5246 SETREX(rex_sv,cur_eval->u.eval.prev_rex);
5247 rex = (struct regexp *)SvANY(rex_sv);
f8fc2ecf 5248 rexi = RXi_GET(rex);
faec1544 5249 cur_curlyx = cur_eval->u.eval.prev_curlyx;
288b8c02 5250 ReREFCNT_inc(rex_sv);
faec1544 5251 st->u.eval.cp = regcppush(0); /* Save *all* the positions. */
34a81e2b
B
5252
5253 /* rex was changed so update the pointer in PL_reglastparen and PL_reglastcloseparen */
5254 PL_reglastparen = &rex->lastparen;
5255 PL_reglastcloseparen = &rex->lastcloseparen;
5256
faec1544
DM
5257 REGCP_SET(st->u.eval.lastcp);
5258 PL_reginput = locinput;
5259
5260 /* Restore parens of the outer rex without popping the
5261 * savestack */
5262 tmpix = PL_savestack_ix;
5263 PL_savestack_ix = cur_eval->u.eval.lastcp;
5264 regcppop(rex);
5265 PL_savestack_ix = tmpix;
5266
5267 st->u.eval.prev_eval = cur_eval;
5268 cur_eval = cur_eval->u.eval.prev_eval;
5269 DEBUG_EXECUTE_r(
2a49f0f5
JH
5270 PerlIO_printf(Perl_debug_log, "%*s EVAL trying tail ... %"UVxf"\n",
5271 REPORT_CODE_OFF+depth*2, "",PTR2UV(cur_eval)););
e7707071
YO
5272 if ( nochange_depth )
5273 nochange_depth--;
5274
5275 PUSH_YES_STATE_GOTO(EVAL_AB,
faec1544
DM
5276 st->u.eval.prev_eval->u.eval.B); /* match B */
5277 }
5278
3b0527fe 5279 if (locinput < reginfo->till) {
a3621e74 5280 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
7821416a
IZ
5281 "%sMatch possible, but length=%ld is smaller than requested=%ld, failing!%s\n",
5282 PL_colors[4],
5283 (long)(locinput - PL_reg_starttry),
3b0527fe 5284 (long)(reginfo->till - PL_reg_starttry),
7821416a 5285 PL_colors[5]));
58e23c8d 5286
262b90c4 5287 sayNO_SILENT; /* Cannot match: too short. */
7821416a
IZ
5288 }
5289 PL_reginput = locinput; /* put where regtry can find it */
262b90c4 5290 sayYES; /* Success! */
dad79028
DM
5291
5292 case SUCCEED: /* successful SUSPEND/UNLESSM/IFMATCH/CURLYM */
5293 DEBUG_EXECUTE_r(
5294 PerlIO_printf(Perl_debug_log,
5295 "%*s %ssubpattern success...%s\n",
5bc10b2c 5296 REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5]));
3280af22 5297 PL_reginput = locinput; /* put where regtry can find it */
262b90c4 5298 sayYES; /* Success! */
dad79028 5299
40a82448
DM
5300#undef ST
5301#define ST st->u.ifmatch
5302
5303 case SUSPEND: /* (?>A) */
5304 ST.wanted = 1;
9fe1d20c 5305 PL_reginput = locinput;
9041c2e3 5306 goto do_ifmatch;
dad79028 5307
40a82448
DM
5308 case UNLESSM: /* -ve lookaround: (?!A), or with flags, (?<!A) */
5309 ST.wanted = 0;
dad79028
DM
5310 goto ifmatch_trivial_fail_test;
5311
40a82448
DM
5312 case IFMATCH: /* +ve lookaround: (?=A), or with flags, (?<=A) */
5313 ST.wanted = 1;
dad79028 5314 ifmatch_trivial_fail_test:
a0ed51b3 5315 if (scan->flags) {
52657f30 5316 char * const s = HOPBACKc(locinput, scan->flags);
dad79028
DM
5317 if (!s) {
5318 /* trivial fail */
24d3c4a9
DM
5319 if (logical) {
5320 logical = 0;
f2338a2e 5321 sw = 1 - cBOOL(ST.wanted);
dad79028 5322 }
40a82448 5323 else if (ST.wanted)
dad79028
DM
5324 sayNO;
5325 next = scan + ARG(scan);
5326 if (next == scan)
5327 next = NULL;
5328 break;
5329 }
efb30f32 5330 PL_reginput = s;
a0ed51b3
LW
5331 }
5332 else
5333 PL_reginput = locinput;
5334
c277df42 5335 do_ifmatch:
40a82448 5336 ST.me = scan;
24d3c4a9 5337 ST.logical = logical;
24d786f4
YO
5338 logical = 0; /* XXX: reset state of logical once it has been saved into ST */
5339
40a82448
DM
5340 /* execute body of (?...A) */
5341 PUSH_YES_STATE_GOTO(IFMATCH_A, NEXTOPER(NEXTOPER(scan)));
5342 /* NOTREACHED */
5343
5344 case IFMATCH_A_fail: /* body of (?...A) failed */
5345 ST.wanted = !ST.wanted;
5346 /* FALL THROUGH */
5347
5348 case IFMATCH_A: /* body of (?...A) succeeded */
24d3c4a9 5349 if (ST.logical) {
f2338a2e 5350 sw = cBOOL(ST.wanted);
40a82448
DM
5351 }
5352 else if (!ST.wanted)
5353 sayNO;
5354
5355 if (OP(ST.me) == SUSPEND)
5356 locinput = PL_reginput;
5357 else {
5358 locinput = PL_reginput = st->locinput;
5359 nextchr = UCHARAT(locinput);
5360 }
5361 scan = ST.me + ARG(ST.me);
5362 if (scan == ST.me)
5363 scan = NULL;
5364 continue; /* execute B */
5365
5366#undef ST
dad79028 5367
c277df42 5368 case LONGJMP:
c277df42
IZ
5369 next = scan + ARG(scan);
5370 if (next == scan)
5371 next = NULL;
a0d0e21e 5372 break;
54612592 5373 case COMMIT:
e2e6a0f1
YO
5374 reginfo->cutpoint = PL_regeol;
5375 /* FALLTHROUGH */
5d458dd8 5376 case PRUNE:
24b23f37 5377 PL_reginput = locinput;
e2e6a0f1 5378 if (!scan->flags)
ad64d0ec 5379 sv_yes_mark = sv_commit = MUTABLE_SV(rexi->data->data[ ARG( scan ) ]);
54612592
YO
5380 PUSH_STATE_GOTO(COMMIT_next,next);
5381 /* NOTREACHED */
5382 case COMMIT_next_fail:
5383 no_final = 1;
5384 /* FALLTHROUGH */
7f69552c
YO
5385 case OPFAIL:
5386 sayNO;
e2e6a0f1
YO
5387 /* NOTREACHED */
5388
5389#define ST st->u.mark
5390 case MARKPOINT:
5391 ST.prev_mark = mark_state;
5d458dd8 5392 ST.mark_name = sv_commit = sv_yes_mark
ad64d0ec 5393 = MUTABLE_SV(rexi->data->data[ ARG( scan ) ]);
e2e6a0f1
YO
5394 mark_state = st;
5395 ST.mark_loc = PL_reginput = locinput;
5396 PUSH_YES_STATE_GOTO(MARKPOINT_next,next);
5397 /* NOTREACHED */
5398 case MARKPOINT_next:
5399 mark_state = ST.prev_mark;
5400 sayYES;
5401 /* NOTREACHED */
5402 case MARKPOINT_next_fail:
5d458dd8 5403 if (popmark && sv_eq(ST.mark_name,popmark))
e2e6a0f1
YO
5404 {
5405 if (ST.mark_loc > startpoint)
5406 reginfo->cutpoint = HOPBACKc(ST.mark_loc, 1);
5407 popmark = NULL; /* we found our mark */
5408 sv_commit = ST.mark_name;
5409
5410 DEBUG_EXECUTE_r({
5d458dd8 5411 PerlIO_printf(Perl_debug_log,
e2e6a0f1
YO
5412 "%*s %ssetting cutpoint to mark:%"SVf"...%s\n",
5413 REPORT_CODE_OFF+depth*2, "",
be2597df 5414 PL_colors[4], SVfARG(sv_commit), PL_colors[5]);
e2e6a0f1
YO
5415 });
5416 }
5417 mark_state = ST.prev_mark;
5d458dd8
YO
5418 sv_yes_mark = mark_state ?
5419 mark_state->u.mark.mark_name : NULL;
e2e6a0f1
YO
5420 sayNO;
5421 /* NOTREACHED */
5d458dd8
YO
5422 case SKIP:
5423 PL_reginput = locinput;
5424 if (scan->flags) {
2bf803e2 5425 /* (*SKIP) : if we fail we cut here*/
5d458dd8 5426 ST.mark_name = NULL;
e2e6a0f1 5427 ST.mark_loc = locinput;
5d458dd8
YO
5428 PUSH_STATE_GOTO(SKIP_next,next);
5429 } else {
2bf803e2 5430 /* (*SKIP:NAME) : if there is a (*MARK:NAME) fail where it was,
5d458dd8
YO
5431 otherwise do nothing. Meaning we need to scan
5432 */
5433 regmatch_state *cur = mark_state;
ad64d0ec 5434 SV *find = MUTABLE_SV(rexi->data->data[ ARG( scan ) ]);
5d458dd8
YO
5435
5436 while (cur) {
5437 if ( sv_eq( cur->u.mark.mark_name,
5438 find ) )
5439 {
5440 ST.mark_name = find;
5441 PUSH_STATE_GOTO( SKIP_next, next );
5442 }
5443 cur = cur->u.mark.prev_mark;
5444 }
e2e6a0f1 5445 }
2bf803e2 5446 /* Didn't find our (*MARK:NAME) so ignore this (*SKIP:NAME) */
5d458dd8
YO
5447 break;
5448 case SKIP_next_fail:
5449 if (ST.mark_name) {
5450 /* (*CUT:NAME) - Set up to search for the name as we
5451 collapse the stack*/
5452 popmark = ST.mark_name;
5453 } else {
5454 /* (*CUT) - No name, we cut here.*/
e2e6a0f1
YO
5455 if (ST.mark_loc > startpoint)
5456 reginfo->cutpoint = HOPBACKc(ST.mark_loc, 1);
5d458dd8
YO
5457 /* but we set sv_commit to latest mark_name if there
5458 is one so they can test to see how things lead to this
5459 cut */
5460 if (mark_state)
5461 sv_commit=mark_state->u.mark.mark_name;
5462 }
e2e6a0f1
YO
5463 no_final = 1;
5464 sayNO;
5465 /* NOTREACHED */
5466#undef ST
32e6a07c
YO
5467 case FOLDCHAR:
5468 n = ARG(scan);
f2ed9b32 5469 if ( n == (U32)what_len_TRICKYFOLD(locinput,utf8_target,ln) ) {
e64b1bd1 5470 locinput += ln;
f2ed9b32 5471 } else if ( 0xDF == n && !utf8_target && !UTF_PATTERN ) {
e64b1bd1
YO
5472 sayNO;
5473 } else {
5474 U8 folded[UTF8_MAXBYTES_CASE+1];
5475 STRLEN foldlen;
5476 const char * const l = locinput;
5477 char *e = PL_regeol;
5478 to_uni_fold(n, folded, &foldlen);
5479
4c1b470c 5480 if (! foldEQ_utf8((const char*) folded, 0, foldlen, 1,
f2ed9b32 5481 l, &e, 0, utf8_target)) {
32e6a07c 5482 sayNO;
e64b1bd1
YO
5483 }
5484 locinput = e;
32e6a07c
YO
5485 }
5486 nextchr = UCHARAT(locinput);
5487 break;
e1d1eefb 5488 case LNBREAK:
f2ed9b32 5489 if ((n=is_LNBREAK(locinput,utf8_target))) {
e1d1eefb
YO
5490 locinput += n;
5491 nextchr = UCHARAT(locinput);
5492 } else
5493 sayNO;
5494 break;
5495
5496#define CASE_CLASS(nAmE) \
5497 case nAmE: \
f2ed9b32 5498 if ((n=is_##nAmE(locinput,utf8_target))) { \
e1d1eefb
YO
5499 locinput += n; \
5500 nextchr = UCHARAT(locinput); \
5501 } else \
5502 sayNO; \
5503 break; \
5504 case N##nAmE: \
f2ed9b32 5505 if ((n=is_##nAmE(locinput,utf8_target))) { \
e1d1eefb
YO
5506 sayNO; \
5507 } else { \
5508 locinput += UTF8SKIP(locinput); \
5509 nextchr = UCHARAT(locinput); \
5510 } \
5511 break
5512
5513 CASE_CLASS(VERTWS);
5514 CASE_CLASS(HORIZWS);
5515#undef CASE_CLASS
5516
a0d0e21e 5517 default:
b900a521 5518 PerlIO_printf(Perl_error_log, "%"UVxf" %d\n",
d7d93a81 5519 PTR2UV(scan), OP(scan));
cea2e8a9 5520 Perl_croak(aTHX_ "regexp memory corruption");
5d458dd8
YO
5521
5522 } /* end switch */
95b24440 5523
5d458dd8
YO
5524 /* switch break jumps here */
5525 scan = next; /* prepare to execute the next op and ... */
5526 continue; /* ... jump back to the top, reusing st */
95b24440
DM
5527 /* NOTREACHED */
5528
40a82448
DM
5529 push_yes_state:
5530 /* push a state that backtracks on success */
5531 st->u.yes.prev_yes_state = yes_state;
5532 yes_state = st;
5533 /* FALL THROUGH */
5534 push_state:
5535 /* push a new regex state, then continue at scan */
5536 {
5537 regmatch_state *newst;
5538
24b23f37
YO
5539 DEBUG_STACK_r({
5540 regmatch_state *cur = st;
5541 regmatch_state *curyes = yes_state;
5542 int curd = depth;
5543 regmatch_slab *slab = PL_regmatch_slab;
5544 for (;curd > -1;cur--,curd--) {
5545 if (cur < SLAB_FIRST(slab)) {
5546 slab = slab->prev;
5547 cur = SLAB_LAST(slab);
5548 }
5549 PerlIO_printf(Perl_error_log, "%*s#%-3d %-10s %s\n",
5550 REPORT_CODE_OFF + 2 + depth * 2,"",
13d6edb4 5551 curd, PL_reg_name[cur->resume_state],
24b23f37
YO
5552 (curyes == cur) ? "yes" : ""
5553 );
5554 if (curyes == cur)
5555 curyes = cur->u.yes.prev_yes_state;
5556 }
5557 } else
5558 DEBUG_STATE_pp("push")
5559 );
40a82448 5560 depth++;
40a82448
DM
5561 st->locinput = locinput;
5562 newst = st+1;
5563 if (newst > SLAB_LAST(PL_regmatch_slab))
5564 newst = S_push_slab(aTHX);
5565 PL_regmatch_state = newst;
786e8c11 5566
40a82448
DM
5567 locinput = PL_reginput;
5568 nextchr = UCHARAT(locinput);
5569 st = newst;
5570 continue;
5571 /* NOTREACHED */
5572 }
a0d0e21e 5573 }
a687059c 5574
a0d0e21e
LW
5575 /*
5576 * We get here only if there's trouble -- normally "case END" is
5577 * the terminating point.
5578 */
cea2e8a9 5579 Perl_croak(aTHX_ "corrupted regexp pointers");
a0d0e21e 5580 /*NOTREACHED*/
4633a7c4
LW
5581 sayNO;
5582
262b90c4 5583yes:
77cb431f
DM
5584 if (yes_state) {
5585 /* we have successfully completed a subexpression, but we must now
5586 * pop to the state marked by yes_state and continue from there */
77cb431f 5587 assert(st != yes_state);
5bc10b2c
DM
5588#ifdef DEBUGGING
5589 while (st != yes_state) {
5590 st--;
5591 if (st < SLAB_FIRST(PL_regmatch_slab)) {
5592 PL_regmatch_slab = PL_regmatch_slab->prev;
5593 st = SLAB_LAST(PL_regmatch_slab);
5594 }
e2e6a0f1 5595 DEBUG_STATE_r({
54612592
YO
5596 if (no_final) {
5597 DEBUG_STATE_pp("pop (no final)");
5598 } else {
5599 DEBUG_STATE_pp("pop (yes)");
5600 }
e2e6a0f1 5601 });
5bc10b2c
DM
5602 depth--;
5603 }
5604#else
77cb431f
DM
5605 while (yes_state < SLAB_FIRST(PL_regmatch_slab)
5606 || yes_state > SLAB_LAST(PL_regmatch_slab))
5607 {
5608 /* not in this slab, pop slab */
5609 depth -= (st - SLAB_FIRST(PL_regmatch_slab) + 1);
5610 PL_regmatch_slab = PL_regmatch_slab->prev;
5611 st = SLAB_LAST(PL_regmatch_slab);
5612 }
5613 depth -= (st - yes_state);
5bc10b2c 5614#endif
77cb431f
DM
5615 st = yes_state;
5616 yes_state = st->u.yes.prev_yes_state;
5617 PL_regmatch_state = st;
24b23f37 5618
5d458dd8
YO
5619 if (no_final) {
5620 locinput= st->locinput;
5621 nextchr = UCHARAT(locinput);
5622 }
54612592 5623 state_num = st->resume_state + no_final;
24d3c4a9 5624 goto reenter_switch;
77cb431f
DM
5625 }
5626
a3621e74 5627 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch successful!%s\n",
e4584336 5628 PL_colors[4], PL_colors[5]));
02db2b7b 5629
19b95bf0
DM
5630 if (PL_reg_eval_set) {
5631 /* each successfully executed (?{...}) block does the equivalent of
5632 * local $^R = do {...}
5633 * When popping the save stack, all these locals would be undone;
5634 * bypass this by setting the outermost saved $^R to the latest
5635 * value */
5636 if (oreplsv != GvSV(PL_replgv))
5637 sv_setsv(oreplsv, GvSV(PL_replgv));
5638 }
95b24440 5639 result = 1;
aa283a38 5640 goto final_exit;
4633a7c4
LW
5641
5642no:
a3621e74 5643 DEBUG_EXECUTE_r(
7821416a 5644 PerlIO_printf(Perl_debug_log,
786e8c11 5645 "%*s %sfailed...%s\n",
5bc10b2c 5646 REPORT_CODE_OFF+depth*2, "",
786e8c11 5647 PL_colors[4], PL_colors[5])
7821416a 5648 );
aa283a38 5649
262b90c4 5650no_silent:
54612592
YO
5651 if (no_final) {
5652 if (yes_state) {
5653 goto yes;
5654 } else {
5655 goto final_exit;
5656 }
5657 }
aa283a38
DM
5658 if (depth) {
5659 /* there's a previous state to backtrack to */
40a82448
DM
5660 st--;
5661 if (st < SLAB_FIRST(PL_regmatch_slab)) {
5662 PL_regmatch_slab = PL_regmatch_slab->prev;
5663 st = SLAB_LAST(PL_regmatch_slab);
5664 }
5665 PL_regmatch_state = st;
40a82448
DM
5666 locinput= st->locinput;
5667 nextchr = UCHARAT(locinput);
5668
5bc10b2c
DM
5669 DEBUG_STATE_pp("pop");
5670 depth--;
262b90c4
DM
5671 if (yes_state == st)
5672 yes_state = st->u.yes.prev_yes_state;
5bc10b2c 5673
24d3c4a9
DM
5674 state_num = st->resume_state + 1; /* failure = success + 1 */
5675 goto reenter_switch;
95b24440 5676 }
24d3c4a9 5677 result = 0;
aa283a38 5678
262b90c4 5679 final_exit:
bbe252da 5680 if (rex->intflags & PREGf_VERBARG_SEEN) {
5d458dd8
YO
5681 SV *sv_err = get_sv("REGERROR", 1);
5682 SV *sv_mrk = get_sv("REGMARK", 1);
5683 if (result) {
e2e6a0f1 5684 sv_commit = &PL_sv_no;
5d458dd8
YO
5685 if (!sv_yes_mark)
5686 sv_yes_mark = &PL_sv_yes;
5687 } else {
5688 if (!sv_commit)
5689 sv_commit = &PL_sv_yes;
5690 sv_yes_mark = &PL_sv_no;
5691 }
5692 sv_setsv(sv_err, sv_commit);
5693 sv_setsv(sv_mrk, sv_yes_mark);
e2e6a0f1 5694 }
19b95bf0 5695
2f554ef7
DM
5696 /* clean up; in particular, free all slabs above current one */
5697 LEAVE_SCOPE(oldsave);
5d9a96ca 5698
95b24440 5699 return result;
a687059c
LW
5700}
5701
5702/*
5703 - regrepeat - repeatedly match something simple, report how many
5704 */
5705/*
5706 * [This routine now assumes that it will only match on things of length 1.
5707 * That was true before, but now we assume scan - reginput is the count,
a0ed51b3 5708 * rather than incrementing count on every character. [Er, except utf8.]]
a687059c 5709 */
76e3520e 5710STATIC I32
e2e6a0f1 5711S_regrepeat(pTHX_ const regexp *prog, const regnode *p, I32 max, int depth)
a687059c 5712{
27da23d5 5713 dVAR;
a0d0e21e 5714 register char *scan;
a0d0e21e 5715 register I32 c;
3280af22 5716 register char *loceol = PL_regeol;
a0ed51b3 5717 register I32 hardcount = 0;
f2ed9b32 5718 register bool utf8_target = PL_reg_match_utf8;
4f55667c
SP
5719#ifndef DEBUGGING
5720 PERL_UNUSED_ARG(depth);
5721#endif
a0d0e21e 5722
7918f24d
NC
5723 PERL_ARGS_ASSERT_REGREPEAT;
5724
3280af22 5725 scan = PL_reginput;
faf11cac
HS
5726 if (max == REG_INFTY)
5727 max = I32_MAX;
5728 else if (max < loceol - scan)
7f596f4c 5729 loceol = scan + max;
a0d0e21e 5730 switch (OP(p)) {
22c35a8c 5731 case REG_ANY:
f2ed9b32 5732 if (utf8_target) {
ffc61ed2 5733 loceol = PL_regeol;
1aa99e6b 5734 while (scan < loceol && hardcount < max && *scan != '\n') {
ffc61ed2
JH
5735 scan += UTF8SKIP(scan);
5736 hardcount++;
5737 }
5738 } else {
5739 while (scan < loceol && *scan != '\n')
5740 scan++;
a0ed51b3
LW
5741 }
5742 break;
ffc61ed2 5743 case SANY:
f2ed9b32 5744 if (utf8_target) {
def8e4ea 5745 loceol = PL_regeol;
a0804c9e 5746 while (scan < loceol && hardcount < max) {
def8e4ea
JH
5747 scan += UTF8SKIP(scan);
5748 hardcount++;
5749 }
5750 }
5751 else
5752 scan = loceol;
a0ed51b3 5753 break;
f33976b4
DB
5754 case CANY:
5755 scan = loceol;
5756 break;
d4e0b827
KW
5757 case EXACTFL:
5758 PL_reg_flags |= RF_tainted;
5759 /* FALL THROUGH */
634c83a2 5760 case EXACT:
d4e0b827
KW
5761 case EXACTF:
5762 /* To get here, EXACTish nodes must have *byte* length == 1. That means
634c83a2
KW
5763 * they match only characters in the string that can be expressed as a
5764 * single byte. For non-utf8 strings, that means a simple match. For
5765 * utf8 strings, the character matched must be an invariant, or
5766 * downgradable to a single byte. The pattern's utf8ness is
5767 * irrelevant, as it must be a single byte, so either it isn't utf8, or
5768 * if it is it's an invariant */
5769
090f7165 5770 c = (U8)*STRING(p);
634c83a2 5771 assert(! UTF_PATTERN || UNI_IS_INVARIANT(c));
d4e0b827 5772
634c83a2
KW
5773 if ((! utf8_target) || UNI_IS_INVARIANT(c)) {
5774
5775 /* Here, the string isn't utf8, or the character in the EXACT
5776 * node is the same in utf8 as not, so can just do equality.
5777 * Each matching char must be 1 byte long */
d4e0b827
KW
5778 switch (OP(p)) {
5779 case EXACT:
5780 while (scan < loceol && UCHARAT(scan) == c) {
5781 scan++;
5782 }
5783 break;
5784 case EXACTF:
5785 while (scan < loceol &&
5786 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold[c]))
5787 {
5788 scan++;
5789 }
5790 break;
5791 case EXACTFL:
5792 while (scan < loceol &&
5793 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold_locale[c]))
5794 {
5795 scan++;
5796 }
5797 break;
5798 default:
5799 Perl_croak(aTHX_ "panic: Unexpected op %u", OP(p));
634c83a2
KW
5800 }
5801 }
5802 else {
5803
d4e0b827
KW
5804 /* Here, the string is utf8, and the pattern char is different
5805 * in utf8 than not. */
5806
5807 switch (OP(p)) {
5808 case EXACT:
5809 {
5810 /* Fastest to find the two utf8 bytes that represent c, and
5811 * then look for those in sequence in the utf8 string */
5812 U8 high = UTF8_TWO_BYTE_HI(c);
5813 U8 low = UTF8_TWO_BYTE_LO(c);
5814 loceol = PL_regeol;
5815
5816 while (hardcount < max
5817 && scan + 1 < loceol
5818 && UCHARAT(scan) == high
5819 && UCHARAT(scan + 1) == low)
5820 {
5821 scan += 2;
5822 hardcount++;
5823 }
5824 }
5825 break;
5826 case EXACTFL: /* Doesn't really make sense, but is best we can
5827 do. The documents warn against mixing locale
5828 and utf8 */
5829 case EXACTF:
5830 { /* utf8 string, so use utf8 foldEQ */
5831 char *tmpeol = loceol;
5832 while (hardcount < max
5833 && foldEQ_utf8(scan, &tmpeol, 0, utf8_target,
5834 STRING(p), NULL, 1, UTF_PATTERN))
5835 {
5836 scan = tmpeol;
5837 tmpeol = loceol;
5838 hardcount++;
5839 }
5840
5841 /* XXX Note that the above handles properly the German
5842 * sharp ss in the pattern matching ss in the string. But
5843 * it doesn't handle properly cases where the string
5844 * contains say 'LIGATURE ff' and the pattern is 'f+'.
5845 * This would require, say, a new function or revised
5846 * interface to foldEQ_utf8(), in which the maximum number
5847 * of characters to match could be passed and it would
5848 * return how many actually did. This is just one of many
5849 * cases where multi-char folds don't work properly, and so
5850 * the fix is being deferred */
5851 }
5852 break;
5853 default:
5854 Perl_croak(aTHX_ "panic: Unexpected op %u", OP(p));
634c83a2
KW
5855 }
5856 }
bbce6d69 5857 break;
a0d0e21e 5858 case ANYOF:
f2ed9b32 5859 if (utf8_target) {
ffc61ed2 5860 loceol = PL_regeol;
cfc92286 5861 while (hardcount < max && scan < loceol &&
f2ed9b32 5862 reginclass(prog, p, (U8*)scan, 0, utf8_target)) {
ffc61ed2
JH
5863 scan += UTF8SKIP(scan);
5864 hardcount++;
5865 }
5866 } else {
32fc9b6a 5867 while (scan < loceol && REGINCLASS(prog, p, (U8*)scan))
ffc61ed2
JH
5868 scan++;
5869 }
a0d0e21e
LW
5870 break;
5871 case ALNUM:
f2ed9b32 5872 if (utf8_target) {
ffc61ed2 5873 loceol = PL_regeol;
1a4fad37 5874 LOAD_UTF8_CHARCLASS_ALNUM();
1aa99e6b 5875 while (hardcount < max && scan < loceol &&
a12cf05f
KW
5876 swash_fetch(PL_utf8_alnum, (U8*)scan, utf8_target))
5877 {
ffc61ed2
JH
5878 scan += UTF8SKIP(scan);
5879 hardcount++;
5880 }
a12cf05f
KW
5881 } else if (FLAGS(p) & USE_UNI) {
5882 while (scan < loceol && isWORDCHAR_L1((U8) *scan)) {
5883 scan++;
5884 }
ffc61ed2 5885 } else {
a12cf05f
KW
5886 while (scan < loceol && isALNUM((U8) *scan)) {
5887 scan++;
5888 }
a0ed51b3
LW
5889 }
5890 break;
bbce6d69 5891 case ALNUML:
3280af22 5892 PL_reg_flags |= RF_tainted;
f2ed9b32 5893 if (utf8_target) {
ffc61ed2 5894 loceol = PL_regeol;
1aa99e6b
IH
5895 while (hardcount < max && scan < loceol &&
5896 isALNUM_LC_utf8((U8*)scan)) {
ffc61ed2
JH
5897 scan += UTF8SKIP(scan);
5898 hardcount++;
5899 }
5900 } else {
5901 while (scan < loceol && isALNUM_LC(*scan))
5902 scan++;
a0ed51b3
LW
5903 }
5904 break;
a0d0e21e 5905 case NALNUM:
f2ed9b32 5906 if (utf8_target) {
ffc61ed2 5907 loceol = PL_regeol;
1a4fad37 5908 LOAD_UTF8_CHARCLASS_ALNUM();
1aa99e6b 5909 while (hardcount < max && scan < loceol &&
a12cf05f
KW
5910 !swash_fetch(PL_utf8_alnum, (U8*)scan, utf8_target))
5911 {
ffc61ed2
JH
5912 scan += UTF8SKIP(scan);
5913 hardcount++;
5914 }
a12cf05f
KW
5915 } else if (FLAGS(p) & USE_UNI) {
5916 while (scan < loceol && ! isWORDCHAR_L1((U8) *scan)) {
5917 scan++;
5918 }
ffc61ed2 5919 } else {
a12cf05f
KW
5920 while (scan < loceol && ! isALNUM((U8) *scan)) {
5921 scan++;
5922 }
a0ed51b3
LW
5923 }
5924 break;
bbce6d69 5925 case NALNUML:
3280af22 5926 PL_reg_flags |= RF_tainted;
f2ed9b32 5927 if (utf8_target) {
ffc61ed2 5928 loceol = PL_regeol;
1aa99e6b
IH
5929 while (hardcount < max && scan < loceol &&
5930 !isALNUM_LC_utf8((U8*)scan)) {
ffc61ed2
JH
5931 scan += UTF8SKIP(scan);
5932 hardcount++;
5933 }
5934 } else {
5935 while (scan < loceol && !isALNUM_LC(*scan))
5936 scan++;
a0ed51b3
LW
5937 }
5938 break;
a0d0e21e 5939 case SPACE:
f2ed9b32 5940 if (utf8_target) {
ffc61ed2 5941 loceol = PL_regeol;
1a4fad37 5942 LOAD_UTF8_CHARCLASS_SPACE();
1aa99e6b 5943 while (hardcount < max && scan < loceol &&
3568d838 5944 (*scan == ' ' ||
a12cf05f
KW
5945 swash_fetch(PL_utf8_space,(U8*)scan, utf8_target)))
5946 {
ffc61ed2
JH
5947 scan += UTF8SKIP(scan);
5948 hardcount++;
5949 }
a12cf05f
KW
5950 } else if (FLAGS(p) & USE_UNI) {
5951 while (scan < loceol && isSPACE_L1((U8) *scan)) {
5952 scan++;
5953 }
ffc61ed2 5954 } else {
a12cf05f
KW
5955 while (scan < loceol && isSPACE((U8) *scan))
5956 scan++;
a0ed51b3
LW
5957 }
5958 break;
bbce6d69 5959 case SPACEL:
3280af22 5960 PL_reg_flags |= RF_tainted;
f2ed9b32 5961 if (utf8_target) {
ffc61ed2 5962 loceol = PL_regeol;
1aa99e6b 5963 while (hardcount < max && scan < loceol &&
ffc61ed2
JH
5964 (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
5965 scan += UTF8SKIP(scan);
5966 hardcount++;
5967 }
5968 } else {
5969 while (scan < loceol && isSPACE_LC(*scan))
5970 scan++;
a0ed51b3
LW
5971 }
5972 break;
a0d0e21e 5973 case NSPACE:
f2ed9b32 5974 if (utf8_target) {
ffc61ed2 5975 loceol = PL_regeol;
1a4fad37 5976 LOAD_UTF8_CHARCLASS_SPACE();
1aa99e6b 5977 while (hardcount < max && scan < loceol &&
3568d838 5978 !(*scan == ' ' ||
a12cf05f
KW
5979 swash_fetch(PL_utf8_space,(U8*)scan, utf8_target)))
5980 {
ffc61ed2
JH
5981 scan += UTF8SKIP(scan);
5982 hardcount++;
5983 }
a12cf05f
KW
5984 } else if (FLAGS(p) & USE_UNI) {
5985 while (scan < loceol && ! isSPACE_L1((U8) *scan)) {
5986 scan++;
5987 }
ffc61ed2 5988 } else {
a12cf05f
KW
5989 while (scan < loceol && ! isSPACE((U8) *scan)) {
5990 scan++;
5991 }
a0ed51b3 5992 }
0008a298 5993 break;
bbce6d69 5994 case NSPACEL:
3280af22 5995 PL_reg_flags |= RF_tainted;
f2ed9b32 5996 if (utf8_target) {
ffc61ed2 5997 loceol = PL_regeol;
1aa99e6b 5998 while (hardcount < max && scan < loceol &&
ffc61ed2
JH
5999 !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
6000 scan += UTF8SKIP(scan);
6001 hardcount++;
6002 }
6003 } else {
6004 while (scan < loceol && !isSPACE_LC(*scan))
6005 scan++;
a0ed51b3
LW
6006 }
6007 break;
a0d0e21e 6008 case DIGIT:
f2ed9b32 6009 if (utf8_target) {
ffc61ed2 6010 loceol = PL_regeol;
1a4fad37 6011 LOAD_UTF8_CHARCLASS_DIGIT();
1aa99e6b 6012 while (hardcount < max && scan < loceol &&
f2ed9b32 6013 swash_fetch(PL_utf8_digit, (U8*)scan, utf8_target)) {
ffc61ed2
JH
6014 scan += UTF8SKIP(scan);
6015 hardcount++;
6016 }
6017 } else {
6018 while (scan < loceol && isDIGIT(*scan))
6019 scan++;
a0ed51b3
LW
6020 }
6021 break;
a0d0e21e 6022 case NDIGIT:
f2ed9b32 6023 if (utf8_target) {
ffc61ed2 6024 loceol = PL_regeol;
1a4fad37 6025 LOAD_UTF8_CHARCLASS_DIGIT();
1aa99e6b 6026 while (hardcount < max && scan < loceol &&
f2ed9b32 6027 !swash_fetch(PL_utf8_digit, (U8*)scan, utf8_target)) {
ffc61ed2
JH
6028 scan += UTF8SKIP(scan);
6029 hardcount++;
6030 }
6031 } else {
6032 while (scan < loceol && !isDIGIT(*scan))
6033 scan++;
a0ed51b3 6034 }
e1d1eefb 6035 case LNBREAK:
f2ed9b32 6036 if (utf8_target) {
e1d1eefb
YO
6037 loceol = PL_regeol;
6038 while (hardcount < max && scan < loceol && (c=is_LNBREAK_utf8(scan))) {
6039 scan += c;
6040 hardcount++;
6041 }
6042 } else {
6043 /*
6044 LNBREAK can match two latin chars, which is ok,
6045 because we have a null terminated string, but we
6046 have to use hardcount in this situation
6047 */
6048 while (scan < loceol && (c=is_LNBREAK_latin1(scan))) {
6049 scan+=c;
6050 hardcount++;
6051 }
6052 }
6053 break;
6054 case HORIZWS:
f2ed9b32 6055 if (utf8_target) {
e1d1eefb
YO
6056 loceol = PL_regeol;
6057 while (hardcount < max && scan < loceol && (c=is_HORIZWS_utf8(scan))) {
6058 scan += c;
6059 hardcount++;
6060 }
6061 } else {
6062 while (scan < loceol && is_HORIZWS_latin1(scan))
6063 scan++;
6064 }
a0ed51b3 6065 break;
e1d1eefb 6066 case NHORIZWS:
f2ed9b32 6067 if (utf8_target) {
e1d1eefb
YO
6068 loceol = PL_regeol;
6069 while (hardcount < max && scan < loceol && !is_HORIZWS_utf8(scan)) {
6070 scan += UTF8SKIP(scan);
6071 hardcount++;
6072 }
6073 } else {
6074 while (scan < loceol && !is_HORIZWS_latin1(scan))
6075 scan++;
6076
6077 }
6078 break;
6079 case VERTWS:
f2ed9b32 6080 if (utf8_target) {
e1d1eefb
YO
6081 loceol = PL_regeol;
6082 while (hardcount < max && scan < loceol && (c=is_VERTWS_utf8(scan))) {
6083 scan += c;
6084 hardcount++;
6085 }
6086 } else {
6087 while (scan < loceol && is_VERTWS_latin1(scan))
6088 scan++;
6089
6090 }
6091 break;
6092 case NVERTWS:
f2ed9b32 6093 if (utf8_target) {
e1d1eefb
YO
6094 loceol = PL_regeol;
6095 while (hardcount < max && scan < loceol && !is_VERTWS_utf8(scan)) {
6096 scan += UTF8SKIP(scan);
6097 hardcount++;
6098 }
6099 } else {
6100 while (scan < loceol && !is_VERTWS_latin1(scan))
6101 scan++;
6102
6103 }
6104 break;
6105
a0d0e21e
LW
6106 default: /* Called on something of 0 width. */
6107 break; /* So match right here or not at all. */
6108 }
a687059c 6109
a0ed51b3
LW
6110 if (hardcount)
6111 c = hardcount;
6112 else
6113 c = scan - PL_reginput;
3280af22 6114 PL_reginput = scan;
a687059c 6115
a3621e74 6116 DEBUG_r({
e68ec53f 6117 GET_RE_DEBUG_FLAGS_DECL;
be8e71aa 6118 DEBUG_EXECUTE_r({
e68ec53f
YO
6119 SV * const prop = sv_newmortal();
6120 regprop(prog, prop, p);
6121 PerlIO_printf(Perl_debug_log,
be8e71aa 6122 "%*s %s can match %"IVdf" times out of %"IVdf"...\n",
e2e6a0f1 6123 REPORT_CODE_OFF + depth*2, "", SvPVX_const(prop),(IV)c,(IV)max);
a3621e74 6124 });
be8e71aa 6125 });
9041c2e3 6126
a0d0e21e 6127 return(c);
a687059c
LW
6128}
6129
c277df42 6130
be8e71aa 6131#if !defined(PERL_IN_XSUB_RE) || defined(PLUGGABLE_RE_EXTENSION)
c277df42 6132/*
ffc61ed2
JH
6133- regclass_swash - prepare the utf8 swash
6134*/
6135
6136SV *
32fc9b6a 6137Perl_regclass_swash(pTHX_ const regexp *prog, register const regnode* node, bool doinit, SV** listsvp, SV **altsvp)
ffc61ed2 6138{
97aff369 6139 dVAR;
9e55ce06
JH
6140 SV *sw = NULL;
6141 SV *si = NULL;
6142 SV *alt = NULL;
f8fc2ecf
YO
6143 RXi_GET_DECL(prog,progi);
6144 const struct reg_data * const data = prog ? progi->data : NULL;
ffc61ed2 6145
7918f24d
NC
6146 PERL_ARGS_ASSERT_REGCLASS_SWASH;
6147
4f639d21 6148 if (data && data->count) {
a3b680e6 6149 const U32 n = ARG(node);
ffc61ed2 6150
4f639d21 6151 if (data->what[n] == 's') {
ad64d0ec
NC
6152 SV * const rv = MUTABLE_SV(data->data[n]);
6153 AV * const av = MUTABLE_AV(SvRV(rv));
2d03de9c 6154 SV **const ary = AvARRAY(av);
9e55ce06 6155 SV **a, **b;
9041c2e3 6156
711a919c 6157 /* See the end of regcomp.c:S_regclass() for
9e55ce06
JH
6158 * documentation of these array elements. */
6159
b11f357e 6160 si = *ary;
fe5bfecd
JH
6161 a = SvROK(ary[1]) ? &ary[1] : NULL;
6162 b = SvTYPE(ary[2]) == SVt_PVAV ? &ary[2] : NULL;
b11f357e 6163
ffc61ed2
JH
6164 if (a)
6165 sw = *a;
6166 else if (si && doinit) {
6167 sw = swash_init("utf8", "", si, 1, 0);
6168 (void)av_store(av, 1, sw);
6169 }
9e55ce06
JH
6170 if (b)
6171 alt = *b;
ffc61ed2
JH
6172 }
6173 }
6174
9e55ce06
JH
6175 if (listsvp)
6176 *listsvp = si;
6177 if (altsvp)
6178 *altsvp = alt;
ffc61ed2
JH
6179
6180 return sw;
6181}
76234dfb 6182#endif
ffc61ed2
JH
6183
6184/*
ba7b4546 6185 - reginclass - determine if a character falls into a character class
832705d4 6186
6698fab5
KW
6187 n is the ANYOF regnode
6188 p is the target string
6189 lenp is pointer to the maximum number of bytes of how far to go in p
6190 (This is assumed wthout checking to always be at least the current
6191 character's size)
6192 utf8_target tells whether p is in UTF-8.
832705d4 6193
4b3cda86
KW
6194 Returns true if matched; false otherwise. If lenp is not NULL, on return
6195 from a successful match, the value it points to will be updated to how many
6196 bytes in p were matched. If there was no match, the value is undefined,
6197 possibly changed from the input.
eba1359e 6198
bbce6d69 6199 */
6200
76e3520e 6201STATIC bool
f6ad78d8 6202S_reginclass(pTHX_ const regexp * const prog, register const regnode * const n, register const U8* const p, STRLEN* lenp, register const bool utf8_target)
bbce6d69 6203{
27da23d5 6204 dVAR;
a3b680e6 6205 const char flags = ANYOF_FLAGS(n);
bbce6d69 6206 bool match = FALSE;
cc07378b 6207 UV c = *p;
ae9ddab8 6208 STRLEN len = 0;
6698fab5 6209 STRLEN maxlen;
1aa99e6b 6210
7918f24d
NC
6211 PERL_ARGS_ASSERT_REGINCLASS;
6212
4b3cda86 6213 /* If c is not already the code point, get it */
f2ed9b32 6214 if (utf8_target && !UTF8_IS_INVARIANT(c)) {
19f67299 6215 c = utf8n_to_uvchr(p, UTF8_MAXBYTES, &len,
6182169b
KW
6216 (UTF8_ALLOW_DEFAULT & UTF8_ALLOW_ANYUV)
6217 | UTF8_ALLOW_FFFF | UTF8_CHECK_ONLY);
6218 /* see [perl #37836] for UTF8_ALLOW_ANYUV; [perl #38293] for
6219 * UTF8_ALLOW_FFFF */
e8a70c6f
SP
6220 if (len == (STRLEN)-1)
6221 Perl_croak(aTHX_ "Malformed UTF-8 character (fatal)");
19f67299 6222 }
bbce6d69 6223
4b3cda86
KW
6224 /* Use passed in max length, or one character if none passed in. And
6225 * assume will match just one character. This is overwritten later if
6226 * matched more. (Note that the code makes an implicit assumption that any
6227 * passed in max is at least one character) */
6228 if (lenp) {
6229 maxlen = *lenp;
6230 *lenp = UNISKIP(NATIVE_TO_UNI(c));
6231
6232 }
6233 else {
6234 maxlen = UNISKIP(NATIVE_TO_UNI(c));
6235 }
6236
f2ed9b32 6237 if (utf8_target || (flags & ANYOF_UNICODE)) {
f2ed9b32 6238 if (utf8_target && !ANYOF_RUNTIME(n)) {
ffc61ed2
JH
6239 if (len != (STRLEN)-1 && c < 256 && ANYOF_BITMAP_TEST(n, c))
6240 match = TRUE;
bbce6d69 6241 }
f2ed9b32 6242 if (!match && utf8_target && (flags & ANYOF_UNICODE_ALL) && c >= 256)
1aa99e6b 6243 match = TRUE;
ffc61ed2 6244 if (!match) {
9e55ce06 6245 AV *av;
32fc9b6a 6246 SV * const sw = regclass_swash(prog, n, TRUE, 0, (SV**)&av);
ffc61ed2
JH
6247
6248 if (sw) {
3f0c5693 6249 U8 * utf8_p;
f2ed9b32 6250 if (utf8_target) {
3f0c5693
KW
6251 utf8_p = (U8 *) p;
6252 } else {
6253 STRLEN len = 1;
6254 utf8_p = bytes_to_utf8(p, &len);
6255 }
6256 if (swash_fetch(sw, utf8_p, 1))
ffc61ed2
JH
6257 match = TRUE;
6258 else if (flags & ANYOF_FOLD) {
9e55ce06
JH
6259 if (!match && lenp && av) {
6260 I32 i;
9e55ce06 6261 for (i = 0; i <= av_len(av); i++) {
890ce7af 6262 SV* const sv = *av_fetch(av, i, FALSE);
9e55ce06 6263 STRLEN len;
890ce7af 6264 const char * const s = SvPV_const(sv, len);
6698fab5 6265 if (len <= maxlen && memEQ(s, (char*)utf8_p, len)) {
9e55ce06
JH
6266 *lenp = len;
6267 match = TRUE;
6268 break;
6269 }
6270 }
6271 }
6272 if (!match) {
89ebb4a3 6273 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
4a623e43 6274
3f0c5693
KW
6275 STRLEN tmplen;
6276 to_utf8_fold(utf8_p, tmpbuf, &tmplen);
6277 if (swash_fetch(sw, tmpbuf, 1))
9e55ce06
JH
6278 match = TRUE;
6279 }
ffc61ed2 6280 }
b3a04dd3
KW
6281
6282 /* If we allocated a string above, free it */
f2ed9b32 6283 if (! utf8_target) Safefree(utf8_p);
ffc61ed2 6284 }
bbce6d69 6285 }
6286 }
1aa99e6b 6287 if (!match && c < 256) {
ffc61ed2
JH
6288 if (ANYOF_BITMAP_TEST(n, c))
6289 match = TRUE;
6290 else if (flags & ANYOF_FOLD) {
eb160463 6291 U8 f;
a0ed51b3 6292
ffc61ed2
JH
6293 if (flags & ANYOF_LOCALE) {
6294 PL_reg_flags |= RF_tainted;
6295 f = PL_fold_locale[c];
6296 }
6297 else
6298 f = PL_fold[c];
6299 if (f != c && ANYOF_BITMAP_TEST(n, f))
6300 match = TRUE;
6301 }
6302
6303 if (!match && (flags & ANYOF_CLASS)) {
a0ed51b3 6304 PL_reg_flags |= RF_tainted;
ffc61ed2
JH
6305 if (
6306 (ANYOF_CLASS_TEST(n, ANYOF_ALNUM) && isALNUM_LC(c)) ||
6307 (ANYOF_CLASS_TEST(n, ANYOF_NALNUM) && !isALNUM_LC(c)) ||
6308 (ANYOF_CLASS_TEST(n, ANYOF_SPACE) && isSPACE_LC(c)) ||
6309 (ANYOF_CLASS_TEST(n, ANYOF_NSPACE) && !isSPACE_LC(c)) ||
6310 (ANYOF_CLASS_TEST(n, ANYOF_DIGIT) && isDIGIT_LC(c)) ||
6311 (ANYOF_CLASS_TEST(n, ANYOF_NDIGIT) && !isDIGIT_LC(c)) ||
6312 (ANYOF_CLASS_TEST(n, ANYOF_ALNUMC) && isALNUMC_LC(c)) ||
6313 (ANYOF_CLASS_TEST(n, ANYOF_NALNUMC) && !isALNUMC_LC(c)) ||
6314 (ANYOF_CLASS_TEST(n, ANYOF_ALPHA) && isALPHA_LC(c)) ||
6315 (ANYOF_CLASS_TEST(n, ANYOF_NALPHA) && !isALPHA_LC(c)) ||
6316 (ANYOF_CLASS_TEST(n, ANYOF_ASCII) && isASCII(c)) ||
6317 (ANYOF_CLASS_TEST(n, ANYOF_NASCII) && !isASCII(c)) ||
6318 (ANYOF_CLASS_TEST(n, ANYOF_CNTRL) && isCNTRL_LC(c)) ||
6319 (ANYOF_CLASS_TEST(n, ANYOF_NCNTRL) && !isCNTRL_LC(c)) ||
6320 (ANYOF_CLASS_TEST(n, ANYOF_GRAPH) && isGRAPH_LC(c)) ||
6321 (ANYOF_CLASS_TEST(n, ANYOF_NGRAPH) && !isGRAPH_LC(c)) ||
6322 (ANYOF_CLASS_TEST(n, ANYOF_LOWER) && isLOWER_LC(c)) ||
6323 (ANYOF_CLASS_TEST(n, ANYOF_NLOWER) && !isLOWER_LC(c)) ||
6324 (ANYOF_CLASS_TEST(n, ANYOF_PRINT) && isPRINT_LC(c)) ||
6325 (ANYOF_CLASS_TEST(n, ANYOF_NPRINT) && !isPRINT_LC(c)) ||
6326 (ANYOF_CLASS_TEST(n, ANYOF_PUNCT) && isPUNCT_LC(c)) ||
6327 (ANYOF_CLASS_TEST(n, ANYOF_NPUNCT) && !isPUNCT_LC(c)) ||
6328 (ANYOF_CLASS_TEST(n, ANYOF_UPPER) && isUPPER_LC(c)) ||
6329 (ANYOF_CLASS_TEST(n, ANYOF_NUPPER) && !isUPPER_LC(c)) ||
6330 (ANYOF_CLASS_TEST(n, ANYOF_XDIGIT) && isXDIGIT(c)) ||
6331 (ANYOF_CLASS_TEST(n, ANYOF_NXDIGIT) && !isXDIGIT(c)) ||
6332 (ANYOF_CLASS_TEST(n, ANYOF_PSXSPC) && isPSXSPC(c)) ||
6333 (ANYOF_CLASS_TEST(n, ANYOF_NPSXSPC) && !isPSXSPC(c)) ||
6334 (ANYOF_CLASS_TEST(n, ANYOF_BLANK) && isBLANK(c)) ||
6335 (ANYOF_CLASS_TEST(n, ANYOF_NBLANK) && !isBLANK(c))
6336 ) /* How's that for a conditional? */
6337 {
6338 match = TRUE;
6339 }
a0ed51b3 6340 }
a0ed51b3
LW
6341 }
6342
a0ed51b3
LW
6343 return (flags & ANYOF_INVERT) ? !match : match;
6344}
161b471a 6345
dfe13c55 6346STATIC U8 *
0ce71af7 6347S_reghop3(U8 *s, I32 off, const U8* lim)
9041c2e3 6348{
97aff369 6349 dVAR;
7918f24d
NC
6350
6351 PERL_ARGS_ASSERT_REGHOP3;
6352
a0ed51b3 6353 if (off >= 0) {
1aa99e6b 6354 while (off-- && s < lim) {
ffc61ed2 6355 /* XXX could check well-formedness here */
a0ed51b3 6356 s += UTF8SKIP(s);
ffc61ed2 6357 }
a0ed51b3
LW
6358 }
6359 else {
1de06328
YO
6360 while (off++ && s > lim) {
6361 s--;
6362 if (UTF8_IS_CONTINUED(*s)) {
6363 while (s > lim && UTF8_IS_CONTINUATION(*s))
6364 s--;
a0ed51b3 6365 }
1de06328 6366 /* XXX could check well-formedness here */
a0ed51b3
LW
6367 }
6368 }
6369 return s;
6370}
161b471a 6371
f9f4320a
YO
6372#ifdef XXX_dmq
6373/* there are a bunch of places where we use two reghop3's that should
6374 be replaced with this routine. but since thats not done yet
6375 we ifdef it out - dmq
6376*/
dfe13c55 6377STATIC U8 *
1de06328
YO
6378S_reghop4(U8 *s, I32 off, const U8* llim, const U8* rlim)
6379{
6380 dVAR;
7918f24d
NC
6381
6382 PERL_ARGS_ASSERT_REGHOP4;
6383
1de06328
YO
6384 if (off >= 0) {
6385 while (off-- && s < rlim) {
6386 /* XXX could check well-formedness here */
6387 s += UTF8SKIP(s);
6388 }
6389 }
6390 else {
6391 while (off++ && s > llim) {
6392 s--;
6393 if (UTF8_IS_CONTINUED(*s)) {
6394 while (s > llim && UTF8_IS_CONTINUATION(*s))
6395 s--;
6396 }
6397 /* XXX could check well-formedness here */
6398 }
6399 }
6400 return s;
6401}
f9f4320a 6402#endif
1de06328
YO
6403
6404STATIC U8 *
0ce71af7 6405S_reghopmaybe3(U8* s, I32 off, const U8* lim)
a0ed51b3 6406{
97aff369 6407 dVAR;
7918f24d
NC
6408
6409 PERL_ARGS_ASSERT_REGHOPMAYBE3;
6410
a0ed51b3 6411 if (off >= 0) {
1aa99e6b 6412 while (off-- && s < lim) {
ffc61ed2 6413 /* XXX could check well-formedness here */
a0ed51b3 6414 s += UTF8SKIP(s);
ffc61ed2 6415 }
a0ed51b3 6416 if (off >= 0)
3dab1dad 6417 return NULL;
a0ed51b3
LW
6418 }
6419 else {
1de06328
YO
6420 while (off++ && s > lim) {
6421 s--;
6422 if (UTF8_IS_CONTINUED(*s)) {
6423 while (s > lim && UTF8_IS_CONTINUATION(*s))
6424 s--;
a0ed51b3 6425 }
1de06328 6426 /* XXX could check well-formedness here */
a0ed51b3
LW
6427 }
6428 if (off <= 0)
3dab1dad 6429 return NULL;
a0ed51b3
LW
6430 }
6431 return s;
6432}
51371543 6433
51371543 6434static void
acfe0abc 6435restore_pos(pTHX_ void *arg)
51371543 6436{
97aff369 6437 dVAR;
097eb12c 6438 regexp * const rex = (regexp *)arg;
51371543
GS
6439 if (PL_reg_eval_set) {
6440 if (PL_reg_oldsaved) {
4f639d21
DM
6441 rex->subbeg = PL_reg_oldsaved;
6442 rex->sublen = PL_reg_oldsavedlen;
f8c7b90f 6443#ifdef PERL_OLD_COPY_ON_WRITE
4f639d21 6444 rex->saved_copy = PL_nrs;
ed252734 6445#endif
07bc277f 6446 RXp_MATCH_COPIED_on(rex);
51371543
GS
6447 }
6448 PL_reg_magic->mg_len = PL_reg_oldpos;
6449 PL_reg_eval_set = 0;
6450 PL_curpm = PL_reg_oldcurpm;
6451 }
6452}
33b8afdf
JH
6453
6454STATIC void
6455S_to_utf8_substr(pTHX_ register regexp *prog)
6456{
a1cac82e 6457 int i = 1;
7918f24d
NC
6458
6459 PERL_ARGS_ASSERT_TO_UTF8_SUBSTR;
6460
a1cac82e
NC
6461 do {
6462 if (prog->substrs->data[i].substr
6463 && !prog->substrs->data[i].utf8_substr) {
6464 SV* const sv = newSVsv(prog->substrs->data[i].substr);
6465 prog->substrs->data[i].utf8_substr = sv;
6466 sv_utf8_upgrade(sv);
610460f9
NC
6467 if (SvVALID(prog->substrs->data[i].substr)) {
6468 const U8 flags = BmFLAGS(prog->substrs->data[i].substr);
6469 if (flags & FBMcf_TAIL) {
6470 /* Trim the trailing \n that fbm_compile added last
6471 time. */
6472 SvCUR_set(sv, SvCUR(sv) - 1);
6473 /* Whilst this makes the SV technically "invalid" (as its
6474 buffer is no longer followed by "\0") when fbm_compile()
6475 adds the "\n" back, a "\0" is restored. */
6476 }
6477 fbm_compile(sv, flags);
6478 }
a1cac82e
NC
6479 if (prog->substrs->data[i].substr == prog->check_substr)
6480 prog->check_utf8 = sv;
6481 }
6482 } while (i--);
33b8afdf
JH
6483}
6484
6485STATIC void
6486S_to_byte_substr(pTHX_ register regexp *prog)
6487{
97aff369 6488 dVAR;
a1cac82e 6489 int i = 1;
7918f24d
NC
6490
6491 PERL_ARGS_ASSERT_TO_BYTE_SUBSTR;
6492
a1cac82e
NC
6493 do {
6494 if (prog->substrs->data[i].utf8_substr
6495 && !prog->substrs->data[i].substr) {
6496 SV* sv = newSVsv(prog->substrs->data[i].utf8_substr);
6497 if (sv_utf8_downgrade(sv, TRUE)) {
610460f9
NC
6498 if (SvVALID(prog->substrs->data[i].utf8_substr)) {
6499 const U8 flags
6500 = BmFLAGS(prog->substrs->data[i].utf8_substr);
6501 if (flags & FBMcf_TAIL) {
6502 /* Trim the trailing \n that fbm_compile added last
6503 time. */
6504 SvCUR_set(sv, SvCUR(sv) - 1);
6505 }
6506 fbm_compile(sv, flags);
6507 }
a1cac82e
NC
6508 } else {
6509 SvREFCNT_dec(sv);
6510 sv = &PL_sv_undef;
6511 }
6512 prog->substrs->data[i].substr = sv;
6513 if (prog->substrs->data[i].utf8_substr == prog->check_utf8)
6514 prog->check_substr = sv;
33b8afdf 6515 }
a1cac82e 6516 } while (i--);
33b8afdf 6517}
66610fdd
RGS
6518
6519/*
6520 * Local variables:
6521 * c-indentation-style: bsd
6522 * c-basic-offset: 4
6523 * indent-tabs-mode: t
6524 * End:
6525 *
37442d52
RGS
6526 * ex: set ts=8 sts=4 sw=4 noet:
6527 */