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