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