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