This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Adding Module::Build::Version missed in upgrade.
[perl5.git] / regexec.c
CommitLineData
a0d0e21e
LW
1/* regexec.c
2 */
3
4/*
5 * "One Ring to rule them all, One Ring to find them..."
6 */
7
61296642
DM
8/* This file contains functions for executing a regular expression. See
9 * also regcomp.c which funnily enough, contains functions for compiling
166f8a29 10 * a regular expression.
e4a054ea
DM
11 *
12 * This file is also copied at build time to ext/re/re_exec.c, where
13 * it's built with -DPERL_EXT_RE_BUILD -DPERL_EXT_RE_DEBUG -DPERL_EXT.
14 * This causes the main functions to be compiled under new names and with
15 * debugging support added, which makes "use re 'debug'" work.
166f8a29
DM
16 */
17
a687059c
LW
18/* NOTE: this is derived from Henry Spencer's regexp code, and should not
19 * confused with the original package (see point 3 below). Thanks, Henry!
20 */
21
22/* Additional note: this code is very heavily munged from Henry's version
23 * in places. In some spots I've traded clarity for efficiency, so don't
24 * blame Henry for some of the lack of readability.
25 */
26
e50aee73
AD
27/* The names of the functions have been changed from regcomp and
28 * regexec to pregcomp and pregexec in order to avoid conflicts
29 * with the POSIX routines of the same names.
30*/
31
b9d5759e 32#ifdef PERL_EXT_RE_BUILD
54df2634 33#include "re_top.h"
9041c2e3 34#endif
56953603 35
a687059c 36/*
e50aee73 37 * pregcomp and pregexec -- regsub and regerror are not used in perl
a687059c
LW
38 *
39 * Copyright (c) 1986 by University of Toronto.
40 * Written by Henry Spencer. Not derived from licensed software.
41 *
42 * Permission is granted to anyone to use this software for any
43 * purpose on any computer system, and to redistribute it freely,
44 * subject to the following restrictions:
45 *
46 * 1. The author is not responsible for the consequences of use of
47 * this software, no matter how awful, even if they arise
48 * from defects in it.
49 *
50 * 2. The origin of this software must not be misrepresented, either
51 * by explicit claim or by omission.
52 *
53 * 3. Altered versions must be plainly marked as such, and must not
54 * be misrepresented as being the original software.
55 *
56 **** Alterations to Henry's code are...
57 ****
4bb101f2 58 **** Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
b94e2f88 59 **** 2000, 2001, 2002, 2003, 2004, 2005, 2006, by Larry Wall and others
a687059c 60 ****
9ef589d8
LW
61 **** You may distribute under the terms of either the GNU General Public
62 **** License or the Artistic License, as specified in the README file.
a687059c
LW
63 *
64 * Beware that some of this code is subtly aware of the way operator
65 * precedence is structured in regular expressions. Serious changes in
66 * regular-expression syntax might require a total rethink.
67 */
68#include "EXTERN.h"
864dbfa3 69#define PERL_IN_REGEXEC_C
a687059c 70#include "perl.h"
0f5d15d6 71
54df2634
NC
72#ifdef PERL_IN_XSUB_RE
73# include "re_comp.h"
74#else
75# include "regcomp.h"
76#endif
a687059c 77
c277df42
IZ
78#define RF_tainted 1 /* tainted information used? */
79#define RF_warned 2 /* warned about big count? */
ce862d02 80#define RF_evaled 4 /* Did an EVAL with setting? */
a0ed51b3
LW
81#define RF_utf8 8 /* String contains multibyte chars? */
82
eb160463 83#define UTF ((PL_reg_flags & RF_utf8) != 0)
ce862d02
IZ
84
85#define RS_init 1 /* eval environment created */
86#define RS_set 2 /* replsv value is set */
c277df42 87
a687059c
LW
88#ifndef STATIC
89#define STATIC static
90#endif
91
32fc9b6a 92#define REGINCLASS(prog,p,c) (ANYOF_FLAGS(p) ? reginclass(prog,p,c,0,0) : ANYOF_BITMAP_TEST(p,*(c)))
7d3e948e 93
c277df42
IZ
94/*
95 * Forwards.
96 */
97
33b8afdf 98#define CHR_SVLEN(sv) (do_utf8 ? sv_len_utf8(sv) : SvCUR(sv))
53c4c00c 99#define CHR_DIST(a,b) (PL_reg_match_utf8 ? utf8_distance(a,b) : a - b)
a0ed51b3 100
3dab1dad
YO
101#define HOPc(pos,off) \
102 (char *)(PL_reg_match_utf8 \
52657f30 103 ? reghop3((U8*)pos, off, (U8*)(off >= 0 ? PL_regeol : PL_bostr)) \
3dab1dad
YO
104 : (U8*)(pos + off))
105#define HOPBACKc(pos, off) \
07be1b83
YO
106 (char*)(PL_reg_match_utf8\
107 ? reghopmaybe3((U8*)pos, -off, (U8*)PL_bostr) \
108 : (pos - off >= PL_bostr) \
8e11feef 109 ? (U8*)pos - off \
3dab1dad 110 : NULL)
efb30f32 111
53c4c00c 112#define HOP3(pos,off,lim) (PL_reg_match_utf8 ? reghop3((U8*)pos, off, (U8*)lim) : (U8*)(pos + off))
1aa99e6b 113#define HOP3c(pos,off,lim) ((char*)HOP3(pos,off,lim))
1aa99e6b 114
1a4fad37
AL
115#define LOAD_UTF8_CHARCLASS(class,str) STMT_START { \
116 if (!CAT2(PL_utf8_,class)) { bool ok; ENTER; save_re_context(); ok=CAT2(is_utf8_,class)((const U8*)str); assert(ok); LEAVE; } } STMT_END
117#define LOAD_UTF8_CHARCLASS_ALNUM() LOAD_UTF8_CHARCLASS(alnum,"a")
118#define LOAD_UTF8_CHARCLASS_DIGIT() LOAD_UTF8_CHARCLASS(digit,"0")
119#define LOAD_UTF8_CHARCLASS_SPACE() LOAD_UTF8_CHARCLASS(space," ")
120#define LOAD_UTF8_CHARCLASS_MARK() LOAD_UTF8_CHARCLASS(mark, "\xcd\x86")
51371543 121
3dab1dad
YO
122/* TODO: Combine JUMPABLE and HAS_TEXT to cache OP(rn) */
123
5f80c4cf 124/* for use after a quantifier and before an EXACT-like node -- japhy */
e2d8ce26
JP
125#define JUMPABLE(rn) ( \
126 OP(rn) == OPEN || OP(rn) == CLOSE || OP(rn) == EVAL || \
cca55fe3
JP
127 OP(rn) == SUSPEND || OP(rn) == IFMATCH || \
128 OP(rn) == PLUS || OP(rn) == MINMOD || \
3dab1dad 129 (PL_regkind[OP(rn)] == CURLY && ARG1(rn) > 0) \
e2d8ce26
JP
130)
131
cca55fe3 132#define HAS_TEXT(rn) ( \
3dab1dad 133 PL_regkind[OP(rn)] == EXACT || PL_regkind[OP(rn)] == REF \
cca55fe3 134)
e2d8ce26 135
a84d97b6
HS
136/*
137 Search for mandatory following text node; for lookahead, the text must
138 follow but for lookbehind (rn->flags != 0) we skip to the next step.
139*/
cca55fe3 140#define FIND_NEXT_IMPT(rn) STMT_START { \
3dab1dad
YO
141 while (JUMPABLE(rn)) { \
142 const OPCODE type = OP(rn); \
143 if (type == SUSPEND || PL_regkind[type] == CURLY) \
e2d8ce26 144 rn = NEXTOPER(NEXTOPER(rn)); \
3dab1dad 145 else if (type == PLUS) \
cca55fe3 146 rn = NEXTOPER(rn); \
3dab1dad 147 else if (type == IFMATCH) \
a84d97b6 148 rn = (rn->flags == 0) ? NEXTOPER(NEXTOPER(rn)) : rn + ARG(rn); \
e2d8ce26 149 else rn += NEXT_OFF(rn); \
3dab1dad 150 } \
5f80c4cf 151} STMT_END
74750237 152
acfe0abc 153static void restore_pos(pTHX_ void *arg);
51371543 154
76e3520e 155STATIC CHECKPOINT
cea2e8a9 156S_regcppush(pTHX_ I32 parenfloor)
a0d0e21e 157{
97aff369 158 dVAR;
a3b680e6 159 const int retval = PL_savestack_ix;
b1ce53c5 160#define REGCP_PAREN_ELEMS 4
a3b680e6 161 const int paren_elems_to_push = (PL_regsize - parenfloor) * REGCP_PAREN_ELEMS;
a0d0e21e 162 int p;
40a82448 163 GET_RE_DEBUG_FLAGS_DECL;
a0d0e21e 164
e49a9654
IH
165 if (paren_elems_to_push < 0)
166 Perl_croak(aTHX_ "panic: paren_elems_to_push < 0");
167
a01268b5 168#define REGCP_OTHER_ELEMS 6
4b3c1a47 169 SSGROW(paren_elems_to_push + REGCP_OTHER_ELEMS);
3280af22 170 for (p = PL_regsize; p > parenfloor; p--) {
b1ce53c5 171/* REGCP_PARENS_ELEMS are pushed per pairs of parentheses. */
cf93c79d
IZ
172 SSPUSHINT(PL_regendp[p]);
173 SSPUSHINT(PL_regstartp[p]);
3280af22 174 SSPUSHPTR(PL_reg_start_tmp[p]);
a0d0e21e 175 SSPUSHINT(p);
40a82448
DM
176 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
177 " saving \\%"UVuf" %"IVdf"(%"IVdf")..%"IVdf"\n",
178 (UV)p, (IV)PL_regstartp[p],
179 (IV)(PL_reg_start_tmp[p] - PL_bostr),
180 (IV)PL_regendp[p]
181 ));
a0d0e21e 182 }
b1ce53c5 183/* REGCP_OTHER_ELEMS are pushed in any case, parentheses or no. */
3280af22
NIS
184 SSPUSHINT(PL_regsize);
185 SSPUSHINT(*PL_reglastparen);
a01268b5 186 SSPUSHINT(*PL_reglastcloseparen);
3280af22 187 SSPUSHPTR(PL_reginput);
41123dfd
JH
188#define REGCP_FRAME_ELEMS 2
189/* REGCP_FRAME_ELEMS are part of the REGCP_OTHER_ELEMS and
190 * are needed for the regexp context stack bookkeeping. */
191 SSPUSHINT(paren_elems_to_push + REGCP_OTHER_ELEMS - REGCP_FRAME_ELEMS);
b1ce53c5 192 SSPUSHINT(SAVEt_REGCONTEXT); /* Magic cookie. */
41123dfd 193
a0d0e21e
LW
194 return retval;
195}
196
c277df42 197/* These are needed since we do not localize EVAL nodes: */
a3621e74 198# define REGCP_SET(cp) DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, \
faccc32b 199 " Setting an EVAL scope, savestack=%"IVdf"\n", \
02db2b7b 200 (IV)PL_savestack_ix)); cp = PL_savestack_ix
c3464db5 201
a3621e74 202# define REGCP_UNWIND(cp) DEBUG_EXECUTE_r(cp != PL_savestack_ix ? \
c3464db5 203 PerlIO_printf(Perl_debug_log, \
faccc32b 204 " Clearing an EVAL scope, savestack=%"IVdf"..%"IVdf"\n", \
02db2b7b 205 (IV)(cp), (IV)PL_savestack_ix) : 0); regcpblow(cp)
c277df42 206
76e3520e 207STATIC char *
097eb12c 208S_regcppop(pTHX_ const regexp *rex)
a0d0e21e 209{
97aff369 210 dVAR;
b1ce53c5 211 I32 i;
a0d0e21e 212 char *input;
b1ce53c5 213
a3621e74
YO
214 GET_RE_DEBUG_FLAGS_DECL;
215
b1ce53c5 216 /* Pop REGCP_OTHER_ELEMS before the parentheses loop starts. */
a0d0e21e 217 i = SSPOPINT;
b1ce53c5
JH
218 assert(i == SAVEt_REGCONTEXT); /* Check that the magic cookie is there. */
219 i = SSPOPINT; /* Parentheses elements to pop. */
a0d0e21e 220 input = (char *) SSPOPPTR;
a01268b5 221 *PL_reglastcloseparen = SSPOPINT;
3280af22
NIS
222 *PL_reglastparen = SSPOPINT;
223 PL_regsize = SSPOPINT;
b1ce53c5
JH
224
225 /* Now restore the parentheses context. */
41123dfd
JH
226 for (i -= (REGCP_OTHER_ELEMS - REGCP_FRAME_ELEMS);
227 i > 0; i -= REGCP_PAREN_ELEMS) {
1df70142 228 I32 tmps;
097eb12c 229 U32 paren = (U32)SSPOPINT;
3280af22 230 PL_reg_start_tmp[paren] = (char *) SSPOPPTR;
cf93c79d
IZ
231 PL_regstartp[paren] = SSPOPINT;
232 tmps = SSPOPINT;
3280af22
NIS
233 if (paren <= *PL_reglastparen)
234 PL_regendp[paren] = tmps;
a3621e74 235 DEBUG_EXECUTE_r(
c3464db5 236 PerlIO_printf(Perl_debug_log,
b900a521 237 " restoring \\%"UVuf" to %"IVdf"(%"IVdf")..%"IVdf"%s\n",
9041c2e3 238 (UV)paren, (IV)PL_regstartp[paren],
b900a521 239 (IV)(PL_reg_start_tmp[paren] - PL_bostr),
9041c2e3 240 (IV)PL_regendp[paren],
3280af22 241 (paren > *PL_reglastparen ? "(no)" : ""));
c277df42 242 );
a0d0e21e 243 }
a3621e74 244 DEBUG_EXECUTE_r(
bb7a0f54 245 if (*PL_reglastparen + 1 <= rex->nparens) {
c3464db5 246 PerlIO_printf(Perl_debug_log,
faccc32b 247 " restoring \\%"IVdf"..\\%"IVdf" to undef\n",
4f639d21 248 (IV)(*PL_reglastparen + 1), (IV)rex->nparens);
c277df42
IZ
249 }
250 );
daf18116 251#if 1
dafc8851
JH
252 /* It would seem that the similar code in regtry()
253 * already takes care of this, and in fact it is in
254 * a better location to since this code can #if 0-ed out
255 * but the code in regtry() is needed or otherwise tests
256 * requiring null fields (pat.t#187 and split.t#{13,14}
daf18116
JH
257 * (as of patchlevel 7877) will fail. Then again,
258 * this code seems to be necessary or otherwise
259 * building DynaLoader will fail:
260 * "Error: '*' not in typemap in DynaLoader.xs, line 164"
261 * --jhi */
bb7a0f54 262 for (i = *PL_reglastparen + 1; (U32)i <= rex->nparens; i++) {
097eb12c
AL
263 if (i > PL_regsize)
264 PL_regstartp[i] = -1;
265 PL_regendp[i] = -1;
a0d0e21e 266 }
dafc8851 267#endif
a0d0e21e
LW
268 return input;
269}
270
02db2b7b 271#define regcpblow(cp) LEAVE_SCOPE(cp) /* Ignores regcppush()ed data. */
a0d0e21e 272
a687059c 273/*
e50aee73 274 * pregexec and friends
a687059c
LW
275 */
276
76234dfb 277#ifndef PERL_IN_XSUB_RE
a687059c 278/*
c277df42 279 - pregexec - match a regexp against a string
a687059c 280 */
c277df42 281I32
864dbfa3 282Perl_pregexec(pTHX_ register regexp *prog, char *stringarg, register char *strend,
c3464db5 283 char *strbeg, I32 minend, SV *screamer, U32 nosave)
c277df42
IZ
284/* strend: pointer to null at end of string */
285/* strbeg: real beginning of string */
286/* minend: end of match must be >=minend after stringarg. */
287/* nosave: For optimizations. */
288{
289 return
9041c2e3 290 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
c277df42
IZ
291 nosave ? 0 : REXEC_COPY_STR);
292}
76234dfb 293#endif
22e551b9 294
9041c2e3 295/*
cad2e5aa
JH
296 * Need to implement the following flags for reg_anch:
297 *
298 * USE_INTUIT_NOML - Useful to call re_intuit_start() first
299 * USE_INTUIT_ML
300 * INTUIT_AUTORITATIVE_NOML - Can trust a positive answer
301 * INTUIT_AUTORITATIVE_ML
302 * INTUIT_ONCE_NOML - Intuit can match in one location only.
303 * INTUIT_ONCE_ML
304 *
305 * Another flag for this function: SECOND_TIME (so that float substrs
306 * with giant delta may be not rechecked).
307 */
308
309/* Assumptions: if ANCH_GPOS, then strpos is anchored. XXXX Check GPOS logic */
310
3f7c398e 311/* If SCREAM, then SvPVX_const(sv) should be compatible with strpos and strend.
cad2e5aa
JH
312 Otherwise, only SvCUR(sv) is used to get strbeg. */
313
314/* XXXX We assume that strpos is strbeg unless sv. */
315
6eb5f6b9
JH
316/* XXXX Some places assume that there is a fixed substring.
317 An update may be needed if optimizer marks as "INTUITable"
318 RExen without fixed substrings. Similarly, it is assumed that
319 lengths of all the strings are no more than minlen, thus they
320 cannot come from lookahead.
321 (Or minlen should take into account lookahead.) */
322
2c2d71f5
JH
323/* A failure to find a constant substring means that there is no need to make
324 an expensive call to REx engine, thus we celebrate a failure. Similarly,
325 finding a substring too deep into the string means that less calls to
30944b6d
IZ
326 regtry() should be needed.
327
328 REx compiler's optimizer found 4 possible hints:
329 a) Anchored substring;
330 b) Fixed substring;
331 c) Whether we are anchored (beginning-of-line or \G);
332 d) First node (of those at offset 0) which may distingush positions;
6eb5f6b9 333 We use a)b)d) and multiline-part of c), and try to find a position in the
30944b6d
IZ
334 string which does not contradict any of them.
335 */
2c2d71f5 336
6eb5f6b9
JH
337/* Most of decisions we do here should have been done at compile time.
338 The nodes of the REx which we used for the search should have been
339 deleted from the finite automaton. */
340
cad2e5aa
JH
341char *
342Perl_re_intuit_start(pTHX_ regexp *prog, SV *sv, char *strpos,
343 char *strend, U32 flags, re_scream_pos_data *data)
344{
97aff369 345 dVAR;
b7953727 346 register I32 start_shift = 0;
cad2e5aa 347 /* Should be nonnegative! */
b7953727 348 register I32 end_shift = 0;
2c2d71f5
JH
349 register char *s;
350 register SV *check;
a1933d95 351 char *strbeg;
cad2e5aa 352 char *t;
a3b680e6 353 const int do_utf8 = sv ? SvUTF8(sv) : 0; /* if no sv we have to assume bytes */
cad2e5aa 354 I32 ml_anch;
bd61b366
SS
355 register char *other_last = NULL; /* other substr checked before this */
356 char *check_at = NULL; /* check substr found at this pos */
1df70142 357 const I32 multiline = prog->reganch & PMf_MULTILINE;
30944b6d 358#ifdef DEBUGGING
890ce7af 359 const char * const i_strpos = strpos;
30944b6d 360#endif
a3621e74
YO
361
362 GET_RE_DEBUG_FLAGS_DECL;
363
a30b2f1f 364 RX_MATCH_UTF8_set(prog,do_utf8);
cad2e5aa 365
b8d68ded 366 if (prog->reganch & ROPT_UTF8) {
a3621e74 367 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
b8d68ded
JH
368 "UTF-8 regex...\n"));
369 PL_reg_flags |= RF_utf8;
370 }
371
a3621e74 372 DEBUG_EXECUTE_r({
0df25f3d
YO
373 RE_PV_DISPLAY_DECL(s, len, PL_reg_match_utf8,
374 PERL_DEBUG_PAD_ZERO(0), strpos, strend - strpos, 60);
375
2a782b5b
JH
376 if (!PL_colorset)
377 reginitcolors();
b8d68ded 378 if (PL_reg_match_utf8)
a3621e74 379 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
b8d68ded 380 "UTF-8 target...\n"));
2a782b5b 381 PerlIO_printf(Perl_debug_log,
a0288114 382 "%sGuessing start of match, REx%s \"%s%.60s%s%s\" against \"%s%.*s%s%s\"...\n",
e4584336 383 PL_colors[4], PL_colors[5], PL_colors[0],
2a782b5b
JH
384 prog->precomp,
385 PL_colors[1],
386 (strlen(prog->precomp) > 60 ? "..." : ""),
387 PL_colors[0],
388 (int)(len > 60 ? 60 : len),
389 s, PL_colors[1],
390 (len > 60 ? "..." : "")
391 );
392 });
cad2e5aa 393
c344f387
JH
394 /* CHR_DIST() would be more correct here but it makes things slow. */
395 if (prog->minlen > strend - strpos) {
a3621e74 396 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
a72c7584 397 "String too short... [re_intuit_start]\n"));
cad2e5aa 398 goto fail;
2c2d71f5 399 }
a1933d95 400 strbeg = (sv && SvPOK(sv)) ? strend - SvCUR(sv) : strpos;
1aa99e6b 401 PL_regeol = strend;
33b8afdf
JH
402 if (do_utf8) {
403 if (!prog->check_utf8 && prog->check_substr)
404 to_utf8_substr(prog);
405 check = prog->check_utf8;
406 } else {
407 if (!prog->check_substr && prog->check_utf8)
408 to_byte_substr(prog);
409 check = prog->check_substr;
410 }
411 if (check == &PL_sv_undef) {
a3621e74 412 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
33b8afdf
JH
413 "Non-utf string cannot match utf check string\n"));
414 goto fail;
415 }
2c2d71f5 416 if (prog->reganch & ROPT_ANCH) { /* Match at beg-of-str or after \n */
cad2e5aa
JH
417 ml_anch = !( (prog->reganch & ROPT_ANCH_SINGLE)
418 || ( (prog->reganch & ROPT_ANCH_BOL)
7fba1cd6 419 && !multiline ) ); /* Check after \n? */
cad2e5aa 420
7e25d62c
JH
421 if (!ml_anch) {
422 if ( !(prog->reganch & (ROPT_ANCH_GPOS /* Checked by the caller */
423 | ROPT_IMPLICIT)) /* not a real BOL */
3f7c398e 424 /* SvCUR is not set on references: SvRV and SvPVX_const overlap */
7e25d62c
JH
425 && sv && !SvROK(sv)
426 && (strpos != strbeg)) {
a3621e74 427 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Not at start...\n"));
7e25d62c
JH
428 goto fail;
429 }
430 if (prog->check_offset_min == prog->check_offset_max &&
cce850e4 431 !(prog->reganch & ROPT_CANY_SEEN)) {
2c2d71f5 432 /* Substring at constant offset from beg-of-str... */
cad2e5aa
JH
433 I32 slen;
434
1aa99e6b 435 s = HOP3c(strpos, prog->check_offset_min, strend);
653099ff
GS
436 if (SvTAIL(check)) {
437 slen = SvCUR(check); /* >= 1 */
cad2e5aa 438
9041c2e3 439 if ( strend - s > slen || strend - s < slen - 1
2c2d71f5 440 || (strend - s == slen && strend[-1] != '\n')) {
a3621e74 441 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "String too long...\n"));
2c2d71f5 442 goto fail_finish;
cad2e5aa
JH
443 }
444 /* Now should match s[0..slen-2] */
445 slen--;
3f7c398e 446 if (slen && (*SvPVX_const(check) != *s
cad2e5aa 447 || (slen > 1
3f7c398e 448 && memNE(SvPVX_const(check), s, slen)))) {
2c2d71f5 449 report_neq:
a3621e74 450 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "String not equal...\n"));
2c2d71f5
JH
451 goto fail_finish;
452 }
cad2e5aa 453 }
3f7c398e 454 else if (*SvPVX_const(check) != *s
653099ff 455 || ((slen = SvCUR(check)) > 1
3f7c398e 456 && memNE(SvPVX_const(check), s, slen)))
2c2d71f5 457 goto report_neq;
c315bfe8 458 check_at = s;
2c2d71f5 459 goto success_at_start;
7e25d62c 460 }
cad2e5aa 461 }
2c2d71f5 462 /* Match is anchored, but substr is not anchored wrt beg-of-str. */
cad2e5aa 463 s = strpos;
2c2d71f5 464 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
2c2d71f5 465 end_shift = prog->minlen - start_shift -
653099ff 466 CHR_SVLEN(check) + (SvTAIL(check) != 0);
2c2d71f5 467 if (!ml_anch) {
a3b680e6 468 const I32 end = prog->check_offset_max + CHR_SVLEN(check)
653099ff 469 - (SvTAIL(check) != 0);
a3b680e6 470 const I32 eshift = CHR_DIST((U8*)strend, (U8*)s) - end;
2c2d71f5
JH
471
472 if (end_shift < eshift)
473 end_shift = eshift;
474 }
cad2e5aa 475 }
2c2d71f5 476 else { /* Can match at random position */
cad2e5aa
JH
477 ml_anch = 0;
478 s = strpos;
2c2d71f5
JH
479 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
480 /* Should be nonnegative! */
481 end_shift = prog->minlen - start_shift -
653099ff 482 CHR_SVLEN(check) + (SvTAIL(check) != 0);
cad2e5aa
JH
483 }
484
2c2d71f5 485#ifdef DEBUGGING /* 7/99: reports of failure (with the older version) */
0033605d 486 if (end_shift < 0)
6bbae5e6 487 Perl_croak(aTHX_ "panic: end_shift");
2c2d71f5
JH
488#endif
489
2c2d71f5
JH
490 restart:
491 /* Find a possible match in the region s..strend by looking for
492 the "check" substring in the region corrected by start/end_shift. */
cad2e5aa 493 if (flags & REXEC_SCREAM) {
cad2e5aa 494 I32 p = -1; /* Internal iterator of scream. */
a3b680e6 495 I32 * const pp = data ? data->scream_pos : &p;
cad2e5aa 496
2c2d71f5
JH
497 if (PL_screamfirst[BmRARE(check)] >= 0
498 || ( BmRARE(check) == '\n'
499 && (BmPREVIOUS(check) == SvCUR(check) - 1)
500 && SvTAIL(check) ))
9041c2e3 501 s = screaminstr(sv, check,
2c2d71f5 502 start_shift + (s - strbeg), end_shift, pp, 0);
cad2e5aa 503 else
2c2d71f5 504 goto fail_finish;
4addbd3b
HS
505 /* we may be pointing at the wrong string */
506 if (s && RX_MATCH_COPIED(prog))
3f7c398e 507 s = strbeg + (s - SvPVX_const(sv));
cad2e5aa
JH
508 if (data)
509 *data->scream_olds = s;
510 }
f33976b4 511 else if (prog->reganch & ROPT_CANY_SEEN)
3baa4c62
JH
512 s = fbm_instr((U8*)(s + start_shift),
513 (U8*)(strend - end_shift),
7fba1cd6 514 check, multiline ? FBMrf_MULTILINE : 0);
cad2e5aa 515 else
1aa99e6b
IH
516 s = fbm_instr(HOP3(s, start_shift, strend),
517 HOP3(strend, -end_shift, strbeg),
7fba1cd6 518 check, multiline ? FBMrf_MULTILINE : 0);
cad2e5aa
JH
519
520 /* Update the count-of-usability, remove useless subpatterns,
521 unshift s. */
2c2d71f5 522
a0288114 523 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%s %s substr \"%s%.*s%s\"%s%s",
2c2d71f5 524 (s ? "Found" : "Did not find"),
33b8afdf 525 (check == (do_utf8 ? prog->anchored_utf8 : prog->anchored_substr) ? "anchored" : "floating"),
2c2d71f5 526 PL_colors[0],
7b0972df 527 (int)(SvCUR(check) - (SvTAIL(check)!=0)),
3f7c398e 528 SvPVX_const(check),
2c2d71f5
JH
529 PL_colors[1], (SvTAIL(check) ? "$" : ""),
530 (s ? " at offset " : "...\n") ) );
531
532 if (!s)
533 goto fail_finish;
534
6eb5f6b9
JH
535 check_at = s;
536
2c2d71f5 537 /* Finish the diagnostic message */
a3621e74 538 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%ld...\n", (long)(s - i_strpos)) );
2c2d71f5
JH
539
540 /* Got a candidate. Check MBOL anchoring, and the *other* substr.
541 Start with the other substr.
542 XXXX no SCREAM optimization yet - and a very coarse implementation
a0288114 543 XXXX /ttx+/ results in anchored="ttx", floating="x". floating will
2c2d71f5
JH
544 *always* match. Probably should be marked during compile...
545 Probably it is right to do no SCREAM here...
546 */
547
33b8afdf 548 if (do_utf8 ? (prog->float_utf8 && prog->anchored_utf8) : (prog->float_substr && prog->anchored_substr)) {
30944b6d 549 /* Take into account the "other" substring. */
2c2d71f5
JH
550 /* XXXX May be hopelessly wrong for UTF... */
551 if (!other_last)
6eb5f6b9 552 other_last = strpos;
33b8afdf 553 if (check == (do_utf8 ? prog->float_utf8 : prog->float_substr)) {
30944b6d
IZ
554 do_other_anchored:
555 {
890ce7af
AL
556 char * const last = HOP3c(s, -start_shift, strbeg);
557 char *last1, *last2;
be8e71aa 558 char * const saved_s = s;
33b8afdf 559 SV* must;
2c2d71f5 560
2c2d71f5
JH
561 t = s - prog->check_offset_max;
562 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
1d86a7f9 563 && (!do_utf8
0ce71af7 564 || ((t = (char*)reghopmaybe3((U8*)s, -(prog->check_offset_max), (U8*)strpos))
2c2d71f5 565 && t > strpos)))
6f207bd3 566 NOOP;
2c2d71f5
JH
567 else
568 t = strpos;
1aa99e6b 569 t = HOP3c(t, prog->anchored_offset, strend);
6eb5f6b9
JH
570 if (t < other_last) /* These positions already checked */
571 t = other_last;
1aa99e6b 572 last2 = last1 = HOP3c(strend, -prog->minlen, strbeg);
2c2d71f5
JH
573 if (last < last1)
574 last1 = last;
575 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
576 /* On end-of-str: see comment below. */
33b8afdf
JH
577 must = do_utf8 ? prog->anchored_utf8 : prog->anchored_substr;
578 if (must == &PL_sv_undef) {
579 s = (char*)NULL;
a3621e74 580 DEBUG_EXECUTE_r(must = prog->anchored_utf8); /* for debug */
33b8afdf
JH
581 }
582 else
583 s = fbm_instr(
584 (unsigned char*)t,
585 HOP3(HOP3(last1, prog->anchored_offset, strend)
586 + SvCUR(must), -(SvTAIL(must)!=0), strbeg),
587 must,
7fba1cd6 588 multiline ? FBMrf_MULTILINE : 0
33b8afdf 589 );
a3621e74 590 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
a0288114 591 "%s anchored substr \"%s%.*s%s\"%s",
2c2d71f5
JH
592 (s ? "Found" : "Contradicts"),
593 PL_colors[0],
33b8afdf
JH
594 (int)(SvCUR(must)
595 - (SvTAIL(must)!=0)),
3f7c398e 596 SvPVX_const(must),
33b8afdf 597 PL_colors[1], (SvTAIL(must) ? "$" : "")));
2c2d71f5
JH
598 if (!s) {
599 if (last1 >= last2) {
a3621e74 600 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
2c2d71f5
JH
601 ", giving up...\n"));
602 goto fail_finish;
603 }
a3621e74 604 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
2c2d71f5 605 ", trying floating at offset %ld...\n",
be8e71aa 606 (long)(HOP3c(saved_s, 1, strend) - i_strpos)));
1aa99e6b
IH
607 other_last = HOP3c(last1, prog->anchored_offset+1, strend);
608 s = HOP3c(last, 1, strend);
2c2d71f5
JH
609 goto restart;
610 }
611 else {
a3621e74 612 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
30944b6d 613 (long)(s - i_strpos)));
1aa99e6b
IH
614 t = HOP3c(s, -prog->anchored_offset, strbeg);
615 other_last = HOP3c(s, 1, strend);
be8e71aa 616 s = saved_s;
2c2d71f5
JH
617 if (t == strpos)
618 goto try_at_start;
2c2d71f5
JH
619 goto try_at_offset;
620 }
30944b6d 621 }
2c2d71f5
JH
622 }
623 else { /* Take into account the floating substring. */
33b8afdf 624 char *last, *last1;
be8e71aa 625 char * const saved_s = s;
33b8afdf
JH
626 SV* must;
627
628 t = HOP3c(s, -start_shift, strbeg);
629 last1 = last =
630 HOP3c(strend, -prog->minlen + prog->float_min_offset, strbeg);
631 if (CHR_DIST((U8*)last, (U8*)t) > prog->float_max_offset)
632 last = HOP3c(t, prog->float_max_offset, strend);
633 s = HOP3c(t, prog->float_min_offset, strend);
634 if (s < other_last)
635 s = other_last;
2c2d71f5 636 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
33b8afdf
JH
637 must = do_utf8 ? prog->float_utf8 : prog->float_substr;
638 /* fbm_instr() takes into account exact value of end-of-str
639 if the check is SvTAIL(ed). Since false positives are OK,
640 and end-of-str is not later than strend we are OK. */
641 if (must == &PL_sv_undef) {
642 s = (char*)NULL;
a3621e74 643 DEBUG_EXECUTE_r(must = prog->float_utf8); /* for debug message */
33b8afdf
JH
644 }
645 else
2c2d71f5 646 s = fbm_instr((unsigned char*)s,
33b8afdf
JH
647 (unsigned char*)last + SvCUR(must)
648 - (SvTAIL(must)!=0),
7fba1cd6 649 must, multiline ? FBMrf_MULTILINE : 0);
a0288114 650 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%s floating substr \"%s%.*s%s\"%s",
33b8afdf
JH
651 (s ? "Found" : "Contradicts"),
652 PL_colors[0],
653 (int)(SvCUR(must) - (SvTAIL(must)!=0)),
3f7c398e 654 SvPVX_const(must),
33b8afdf
JH
655 PL_colors[1], (SvTAIL(must) ? "$" : "")));
656 if (!s) {
657 if (last1 == last) {
a3621e74 658 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
33b8afdf
JH
659 ", giving up...\n"));
660 goto fail_finish;
2c2d71f5 661 }
a3621e74 662 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
33b8afdf 663 ", trying anchored starting at offset %ld...\n",
be8e71aa 664 (long)(saved_s + 1 - i_strpos)));
33b8afdf
JH
665 other_last = last;
666 s = HOP3c(t, 1, strend);
667 goto restart;
668 }
669 else {
a3621e74 670 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
33b8afdf
JH
671 (long)(s - i_strpos)));
672 other_last = s; /* Fix this later. --Hugo */
be8e71aa 673 s = saved_s;
33b8afdf
JH
674 if (t == strpos)
675 goto try_at_start;
676 goto try_at_offset;
677 }
2c2d71f5 678 }
cad2e5aa 679 }
2c2d71f5
JH
680
681 t = s - prog->check_offset_max;
2c2d71f5 682 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
1d86a7f9 683 && (!do_utf8
0ce71af7 684 || ((t = (char*)reghopmaybe3((U8*)s, -prog->check_offset_max, (U8*)strpos))
1aa99e6b 685 && t > strpos))) {
2c2d71f5
JH
686 /* Fixed substring is found far enough so that the match
687 cannot start at strpos. */
688 try_at_offset:
cad2e5aa 689 if (ml_anch && t[-1] != '\n') {
30944b6d
IZ
690 /* Eventually fbm_*() should handle this, but often
691 anchored_offset is not 0, so this check will not be wasted. */
692 /* XXXX In the code below we prefer to look for "^" even in
693 presence of anchored substrings. And we search even
694 beyond the found float position. These pessimizations
695 are historical artefacts only. */
696 find_anchor:
2c2d71f5 697 while (t < strend - prog->minlen) {
cad2e5aa 698 if (*t == '\n') {
4ee3650e 699 if (t < check_at - prog->check_offset_min) {
33b8afdf 700 if (do_utf8 ? prog->anchored_utf8 : prog->anchored_substr) {
4ee3650e
GS
701 /* Since we moved from the found position,
702 we definitely contradict the found anchored
30944b6d
IZ
703 substr. Due to the above check we do not
704 contradict "check" substr.
705 Thus we can arrive here only if check substr
706 is float. Redo checking for "other"=="fixed".
707 */
9041c2e3 708 strpos = t + 1;
a3621e74 709 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld, rescanning for anchored from offset %ld...\n",
e4584336 710 PL_colors[0], PL_colors[1], (long)(strpos - i_strpos), (long)(strpos - i_strpos + prog->anchored_offset)));
30944b6d
IZ
711 goto do_other_anchored;
712 }
4ee3650e
GS
713 /* We don't contradict the found floating substring. */
714 /* XXXX Why not check for STCLASS? */
cad2e5aa 715 s = t + 1;
a3621e74 716 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld...\n",
e4584336 717 PL_colors[0], PL_colors[1], (long)(s - i_strpos)));
cad2e5aa
JH
718 goto set_useful;
719 }
4ee3650e
GS
720 /* Position contradicts check-string */
721 /* XXXX probably better to look for check-string
722 than for "\n", so one should lower the limit for t? */
a3621e74 723 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m, restarting lookup for check-string at offset %ld...\n",
e4584336 724 PL_colors[0], PL_colors[1], (long)(t + 1 - i_strpos)));
0e41cd87 725 other_last = strpos = s = t + 1;
cad2e5aa
JH
726 goto restart;
727 }
728 t++;
729 }
a3621e74 730 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Did not find /%s^%s/m...\n",
e4584336 731 PL_colors[0], PL_colors[1]));
2c2d71f5 732 goto fail_finish;
cad2e5aa 733 }
f5952150 734 else {
a3621e74 735 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Starting position does not contradict /%s^%s/m...\n",
e4584336 736 PL_colors[0], PL_colors[1]));
f5952150 737 }
cad2e5aa
JH
738 s = t;
739 set_useful:
33b8afdf 740 ++BmUSEFUL(do_utf8 ? prog->check_utf8 : prog->check_substr); /* hooray/5 */
cad2e5aa
JH
741 }
742 else {
f5952150 743 /* The found string does not prohibit matching at strpos,
2c2d71f5 744 - no optimization of calling REx engine can be performed,
f5952150
GS
745 unless it was an MBOL and we are not after MBOL,
746 or a future STCLASS check will fail this. */
2c2d71f5
JH
747 try_at_start:
748 /* Even in this situation we may use MBOL flag if strpos is offset
749 wrt the start of the string. */
05b4157f 750 if (ml_anch && sv && !SvROK(sv) /* See prev comment on SvROK */
a1933d95 751 && (strpos != strbeg) && strpos[-1] != '\n'
d506a20d
IZ
752 /* May be due to an implicit anchor of m{.*foo} */
753 && !(prog->reganch & ROPT_IMPLICIT))
754 {
cad2e5aa
JH
755 t = strpos;
756 goto find_anchor;
757 }
a3621e74 758 DEBUG_EXECUTE_r( if (ml_anch)
f5952150 759 PerlIO_printf(Perl_debug_log, "Position at offset %ld does not contradict /%s^%s/m...\n",
e4584336 760 (long)(strpos - i_strpos), PL_colors[0], PL_colors[1]);
30944b6d 761 );
2c2d71f5 762 success_at_start:
30944b6d 763 if (!(prog->reganch & ROPT_NAUGHTY) /* XXXX If strpos moved? */
33b8afdf
JH
764 && (do_utf8 ? (
765 prog->check_utf8 /* Could be deleted already */
766 && --BmUSEFUL(prog->check_utf8) < 0
767 && (prog->check_utf8 == prog->float_utf8)
768 ) : (
769 prog->check_substr /* Could be deleted already */
770 && --BmUSEFUL(prog->check_substr) < 0
771 && (prog->check_substr == prog->float_substr)
772 )))
66e933ab 773 {
cad2e5aa 774 /* If flags & SOMETHING - do not do it many times on the same match */
a3621e74 775 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "... Disabling check substring...\n"));
33b8afdf
JH
776 SvREFCNT_dec(do_utf8 ? prog->check_utf8 : prog->check_substr);
777 if (do_utf8 ? prog->check_substr : prog->check_utf8)
778 SvREFCNT_dec(do_utf8 ? prog->check_substr : prog->check_utf8);
a0714e2c
SS
779 prog->check_substr = prog->check_utf8 = NULL; /* disable */
780 prog->float_substr = prog->float_utf8 = NULL; /* clear */
781 check = NULL; /* abort */
cad2e5aa 782 s = strpos;
3cf5c195
IZ
783 /* XXXX This is a remnant of the old implementation. It
784 looks wasteful, since now INTUIT can use many
6eb5f6b9 785 other heuristics. */
cad2e5aa
JH
786 prog->reganch &= ~RE_USE_INTUIT;
787 }
788 else
789 s = strpos;
790 }
791
6eb5f6b9
JH
792 /* Last resort... */
793 /* XXXX BmUSEFUL already changed, maybe multiple change is meaningful... */
07be1b83 794 if (prog->regstclass && OP(prog->regstclass)!=TRIE) {
6eb5f6b9
JH
795 /* minlen == 0 is possible if regstclass is \b or \B,
796 and the fixed substr is ''$.
797 Since minlen is already taken into account, s+1 is before strend;
798 accidentally, minlen >= 1 guaranties no false positives at s + 1
799 even for \b or \B. But (minlen? 1 : 0) below assumes that
800 regstclass does not come from lookahead... */
801 /* If regstclass takes bytelength more than 1: If charlength==1, OK.
802 This leaves EXACTF only, which is dealt with in find_byclass(). */
890ce7af 803 const U8* const str = (U8*)STRING(prog->regstclass);
3dab1dad 804 const int cl_l = (PL_regkind[OP(prog->regstclass)] == EXACT
1aa99e6b 805 ? CHR_DIST(str+STR_LEN(prog->regstclass), str)
66e933ab 806 : 1);
07be1b83 807 const char * endpos = (prog->anchored_substr || prog->anchored_utf8 || ml_anch)
1aa99e6b 808 ? HOP3c(s, (prog->minlen ? cl_l : 0), strend)
33b8afdf 809 : (prog->float_substr || prog->float_utf8
1aa99e6b
IH
810 ? HOP3c(HOP3c(check_at, -start_shift, strbeg),
811 cl_l, strend)
812 : strend);
07be1b83
YO
813 /*if (OP(prog->regstclass) == TRIE)
814 endpos++;*/
6eb5f6b9 815 t = s;
3b0527fe 816 s = find_byclass(prog, prog->regstclass, s, endpos, NULL);
6eb5f6b9
JH
817 if (!s) {
818#ifdef DEBUGGING
cbbf8932 819 const char *what = NULL;
6eb5f6b9
JH
820#endif
821 if (endpos == strend) {
a3621e74 822 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
6eb5f6b9
JH
823 "Could not match STCLASS...\n") );
824 goto fail;
825 }
a3621e74 826 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
66e933ab 827 "This position contradicts STCLASS...\n") );
653099ff
GS
828 if ((prog->reganch & ROPT_ANCH) && !ml_anch)
829 goto fail;
6eb5f6b9 830 /* Contradict one of substrings */
33b8afdf
JH
831 if (prog->anchored_substr || prog->anchored_utf8) {
832 if ((do_utf8 ? prog->anchored_utf8 : prog->anchored_substr) == check) {
a3621e74 833 DEBUG_EXECUTE_r( what = "anchored" );
6eb5f6b9 834 hop_and_restart:
1aa99e6b 835 s = HOP3c(t, 1, strend);
66e933ab
GS
836 if (s + start_shift + end_shift > strend) {
837 /* XXXX Should be taken into account earlier? */
a3621e74 838 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
66e933ab
GS
839 "Could not match STCLASS...\n") );
840 goto fail;
841 }
5e39e1e5
HS
842 if (!check)
843 goto giveup;
a3621e74 844 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
f5952150 845 "Looking for %s substr starting at offset %ld...\n",
6eb5f6b9
JH
846 what, (long)(s + start_shift - i_strpos)) );
847 goto restart;
848 }
66e933ab 849 /* Have both, check_string is floating */
6eb5f6b9
JH
850 if (t + start_shift >= check_at) /* Contradicts floating=check */
851 goto retry_floating_check;
852 /* Recheck anchored substring, but not floating... */
9041c2e3 853 s = check_at;
5e39e1e5
HS
854 if (!check)
855 goto giveup;
a3621e74 856 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
f5952150 857 "Looking for anchored substr starting at offset %ld...\n",
6eb5f6b9
JH
858 (long)(other_last - i_strpos)) );
859 goto do_other_anchored;
860 }
60e71179
GS
861 /* Another way we could have checked stclass at the
862 current position only: */
863 if (ml_anch) {
864 s = t = t + 1;
5e39e1e5
HS
865 if (!check)
866 goto giveup;
a3621e74 867 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
f5952150 868 "Looking for /%s^%s/m starting at offset %ld...\n",
e4584336 869 PL_colors[0], PL_colors[1], (long)(t - i_strpos)) );
60e71179 870 goto try_at_offset;
66e933ab 871 }
33b8afdf 872 if (!(do_utf8 ? prog->float_utf8 : prog->float_substr)) /* Could have been deleted */
60e71179 873 goto fail;
6eb5f6b9
JH
874 /* Check is floating subtring. */
875 retry_floating_check:
876 t = check_at - start_shift;
a3621e74 877 DEBUG_EXECUTE_r( what = "floating" );
6eb5f6b9
JH
878 goto hop_and_restart;
879 }
b7953727 880 if (t != s) {
a3621e74 881 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
6eb5f6b9 882 "By STCLASS: moving %ld --> %ld\n",
b7953727
JH
883 (long)(t - i_strpos), (long)(s - i_strpos))
884 );
885 }
886 else {
a3621e74 887 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
b7953727
JH
888 "Does not contradict STCLASS...\n");
889 );
890 }
6eb5f6b9 891 }
5e39e1e5 892 giveup:
a3621e74 893 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%s%s:%s match at offset %ld\n",
5e39e1e5
HS
894 PL_colors[4], (check ? "Guessed" : "Giving up"),
895 PL_colors[5], (long)(s - i_strpos)) );
cad2e5aa 896 return s;
2c2d71f5
JH
897
898 fail_finish: /* Substring not found */
33b8afdf
JH
899 if (prog->check_substr || prog->check_utf8) /* could be removed already */
900 BmUSEFUL(do_utf8 ? prog->check_utf8 : prog->check_substr) += 5; /* hooray */
cad2e5aa 901 fail:
a3621e74 902 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch rejected by optimizer%s\n",
e4584336 903 PL_colors[4], PL_colors[5]));
bd61b366 904 return NULL;
cad2e5aa 905}
9661b544 906
6eb5f6b9 907/* We know what class REx starts with. Try to find this position... */
3b0527fe 908/* if reginfo is NULL, its a dryrun */
07be1b83
YO
909/* annoyingly all the vars in this routine have different names from their counterparts
910 in regmatch. /grrr */
3b0527fe 911
4cadc6a9
YO
912#define REXEC_TRIE_READ_CHAR(trie_type, trie, uc, uscan, len, uvc, charid, \
913foldlen, foldbuf, uniflags) STMT_START { \
914 switch (trie_type) { \
915 case trie_utf8_fold: \
916 if ( foldlen>0 ) { \
917 uvc = utf8n_to_uvuni( uscan, UTF8_MAXLEN, &len, uniflags ); \
918 foldlen -= len; \
919 uscan += len; \
920 len=0; \
921 } else { \
922 uvc = utf8n_to_uvuni( (U8*)uc, UTF8_MAXLEN, &len, uniflags ); \
923 uvc = to_uni_fold( uvc, foldbuf, &foldlen ); \
924 foldlen -= UNISKIP( uvc ); \
925 uscan = foldbuf + UNISKIP( uvc ); \
926 } \
927 break; \
928 case trie_utf8: \
929 uvc = utf8n_to_uvuni( (U8*)uc, UTF8_MAXLEN, &len, uniflags ); \
930 break; \
931 case trie_plain: \
932 uvc = (UV)*uc; \
933 len = 1; \
934 } \
935 \
936 if (uvc < 256) { \
937 charid = trie->charmap[ uvc ]; \
938 } \
939 else { \
940 charid = 0; \
941 if (trie->widecharmap) { \
942 SV** const svpp = hv_fetch(trie->widecharmap, \
943 (char*)&uvc, sizeof(UV), 0); \
944 if (svpp) \
945 charid = (U16)SvIV(*svpp); \
946 } \
947 } \
948} STMT_END
949
950#define REXEC_FBC_EXACTISH_CHECK(CoNd) \
951 if ( (CoNd) \
952 && (ln == len || \
953 ibcmp_utf8(s, NULL, 0, do_utf8, \
954 m, NULL, ln, (bool)UTF)) \
955 && (!reginfo || regtry(reginfo, s)) ) \
956 goto got_it; \
957 else { \
958 U8 foldbuf[UTF8_MAXBYTES_CASE+1]; \
959 uvchr_to_utf8(tmpbuf, c); \
960 f = to_utf8_fold(tmpbuf, foldbuf, &foldlen); \
961 if ( f != c \
962 && (f == c1 || f == c2) \
963 && (ln == foldlen || \
964 !ibcmp_utf8((char *) foldbuf, \
965 NULL, foldlen, do_utf8, \
966 m, \
967 NULL, ln, (bool)UTF)) \
968 && (!reginfo || regtry(reginfo, s)) ) \
969 goto got_it; \
970 } \
971 s += len
972
973#define REXEC_FBC_EXACTISH_SCAN(CoNd) \
974STMT_START { \
975 while (s <= e) { \
976 if ( (CoNd) \
977 && (ln == 1 || !(OP(c) == EXACTF \
978 ? ibcmp(s, m, ln) \
979 : ibcmp_locale(s, m, ln))) \
980 && (!reginfo || regtry(reginfo, s)) ) \
981 goto got_it; \
982 s++; \
983 } \
984} STMT_END
985
986#define REXEC_FBC_UTF8_SCAN(CoDe) \
987STMT_START { \
988 while (s + (uskip = UTF8SKIP(s)) <= strend) { \
989 CoDe \
990 s += uskip; \
991 } \
992} STMT_END
993
994#define REXEC_FBC_SCAN(CoDe) \
995STMT_START { \
996 while (s < strend) { \
997 CoDe \
998 s++; \
999 } \
1000} STMT_END
1001
1002#define REXEC_FBC_UTF8_CLASS_SCAN(CoNd) \
1003REXEC_FBC_UTF8_SCAN( \
1004 if (CoNd) { \
1005 if (tmp && (!reginfo || regtry(reginfo, s))) \
1006 goto got_it; \
1007 else \
1008 tmp = doevery; \
1009 } \
1010 else \
1011 tmp = 1; \
1012)
1013
1014#define REXEC_FBC_CLASS_SCAN(CoNd) \
1015REXEC_FBC_SCAN( \
1016 if (CoNd) { \
1017 if (tmp && (!reginfo || regtry(reginfo, s))) \
1018 goto got_it; \
1019 else \
1020 tmp = doevery; \
1021 } \
1022 else \
1023 tmp = 1; \
1024)
1025
1026#define REXEC_FBC_TRYIT \
1027if ((!reginfo || regtry(reginfo, s))) \
1028 goto got_it
1029
1030#define REXEC_FBC_CSCAN_PRELOAD(UtFpReLoAd,CoNdUtF8,CoNd) \
1031 if (do_utf8) { \
1032 UtFpReLoAd; \
1033 REXEC_FBC_UTF8_CLASS_SCAN(CoNdUtF8); \
1034 } \
1035 else { \
1036 REXEC_FBC_CLASS_SCAN(CoNd); \
1037 } \
1038 break
1039
1040#define REXEC_FBC_CSCAN_TAINT(CoNdUtF8,CoNd) \
1041 PL_reg_flags |= RF_tainted; \
1042 if (do_utf8) { \
1043 REXEC_FBC_UTF8_CLASS_SCAN(CoNdUtF8); \
1044 } \
1045 else { \
1046 REXEC_FBC_CLASS_SCAN(CoNd); \
1047 } \
1048 break
1049
3c3eec57 1050STATIC char *
07be1b83
YO
1051S_find_byclass(pTHX_ regexp * prog, const regnode *c, char *s,
1052 const char *strend, const regmatch_info *reginfo)
a687059c 1053{
27da23d5 1054 dVAR;
1df70142 1055 const I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
6eb5f6b9 1056 char *m;
d8093b23 1057 STRLEN ln;
5dab1207 1058 STRLEN lnc;
078c425b 1059 register STRLEN uskip;
d8093b23
G
1060 unsigned int c1;
1061 unsigned int c2;
6eb5f6b9
JH
1062 char *e;
1063 register I32 tmp = 1; /* Scratch variable? */
a3b680e6 1064 register const bool do_utf8 = PL_reg_match_utf8;
cad2e5aa 1065
6eb5f6b9
JH
1066 /* We know what class it must start with. */
1067 switch (OP(c)) {
6eb5f6b9 1068 case ANYOF:
388cc4de 1069 if (do_utf8) {
4cadc6a9 1070 REXEC_FBC_UTF8_CLASS_SCAN((ANYOF_FLAGS(c) & ANYOF_UNICODE) ||
388cc4de 1071 !UTF8_IS_INVARIANT((U8)s[0]) ?
32fc9b6a 1072 reginclass(prog, c, (U8*)s, 0, do_utf8) :
4cadc6a9 1073 REGINCLASS(prog, c, (U8*)s));
388cc4de
HS
1074 }
1075 else {
1076 while (s < strend) {
1077 STRLEN skip = 1;
1078
32fc9b6a 1079 if (REGINCLASS(prog, c, (U8*)s) ||
388cc4de
HS
1080 (ANYOF_FOLD_SHARP_S(c, s, strend) &&
1081 /* The assignment of 2 is intentional:
1082 * for the folded sharp s, the skip is 2. */
1083 (skip = SHARP_S_SKIP))) {
3b0527fe 1084 if (tmp && (!reginfo || regtry(reginfo, s)))
388cc4de
HS
1085 goto got_it;
1086 else
1087 tmp = doevery;
1088 }
1089 else
1090 tmp = 1;
1091 s += skip;
1092 }
a0d0e21e 1093 }
6eb5f6b9 1094 break;
f33976b4 1095 case CANY:
4cadc6a9 1096 REXEC_FBC_SCAN(
3b0527fe 1097 if (tmp && (!reginfo || regtry(reginfo, s)))
f33976b4
DB
1098 goto got_it;
1099 else
1100 tmp = doevery;
4cadc6a9 1101 );
f33976b4 1102 break;
6eb5f6b9 1103 case EXACTF:
5dab1207
NIS
1104 m = STRING(c);
1105 ln = STR_LEN(c); /* length to match in octets/bytes */
1106 lnc = (I32) ln; /* length to match in characters */
1aa99e6b 1107 if (UTF) {
a2a2844f 1108 STRLEN ulen1, ulen2;
5dab1207 1109 U8 *sm = (U8 *) m;
89ebb4a3
JH
1110 U8 tmpbuf1[UTF8_MAXBYTES_CASE+1];
1111 U8 tmpbuf2[UTF8_MAXBYTES_CASE+1];
4ad0818d 1112 const U32 uniflags = UTF8_ALLOW_DEFAULT;
a2a2844f
JH
1113
1114 to_utf8_lower((U8*)m, tmpbuf1, &ulen1);
1115 to_utf8_upper((U8*)m, tmpbuf2, &ulen2);
1116
89ebb4a3 1117 c1 = utf8n_to_uvchr(tmpbuf1, UTF8_MAXBYTES_CASE,
041457d9 1118 0, uniflags);
89ebb4a3 1119 c2 = utf8n_to_uvchr(tmpbuf2, UTF8_MAXBYTES_CASE,
041457d9 1120 0, uniflags);
5dab1207
NIS
1121 lnc = 0;
1122 while (sm < ((U8 *) m + ln)) {
1123 lnc++;
1124 sm += UTF8SKIP(sm);
1125 }
1aa99e6b
IH
1126 }
1127 else {
1128 c1 = *(U8*)m;
1129 c2 = PL_fold[c1];
1130 }
6eb5f6b9
JH
1131 goto do_exactf;
1132 case EXACTFL:
5dab1207
NIS
1133 m = STRING(c);
1134 ln = STR_LEN(c);
1135 lnc = (I32) ln;
d8093b23 1136 c1 = *(U8*)m;
6eb5f6b9
JH
1137 c2 = PL_fold_locale[c1];
1138 do_exactf:
db12adc6 1139 e = HOP3c(strend, -((I32)lnc), s);
b3c9acc1 1140
3b0527fe 1141 if (!reginfo && e < s)
6eb5f6b9 1142 e = s; /* Due to minlen logic of intuit() */
1aa99e6b 1143
60a8b682
JH
1144 /* The idea in the EXACTF* cases is to first find the
1145 * first character of the EXACTF* node and then, if
1146 * necessary, case-insensitively compare the full
1147 * text of the node. The c1 and c2 are the first
1148 * characters (though in Unicode it gets a bit
1149 * more complicated because there are more cases
7f16dd3d
JH
1150 * than just upper and lower: one needs to use
1151 * the so-called folding case for case-insensitive
1152 * matching (called "loose matching" in Unicode).
1153 * ibcmp_utf8() will do just that. */
60a8b682 1154
1aa99e6b 1155 if (do_utf8) {
575cac57 1156 UV c, f;
89ebb4a3 1157 U8 tmpbuf [UTF8_MAXBYTES+1];
575cac57 1158 STRLEN len, foldlen;
4ad0818d 1159 const U32 uniflags = UTF8_ALLOW_DEFAULT;
09091399 1160 if (c1 == c2) {
5dab1207
NIS
1161 /* Upper and lower of 1st char are equal -
1162 * probably not a "letter". */
1aa99e6b 1163 while (s <= e) {
89ebb4a3 1164 c = utf8n_to_uvchr((U8*)s, UTF8_MAXBYTES, &len,
041457d9 1165 uniflags);
4cadc6a9 1166 REXEC_FBC_EXACTISH_CHECK(c == c1);
1aa99e6b 1167 }
09091399
JH
1168 }
1169 else {
1aa99e6b 1170 while (s <= e) {
89ebb4a3 1171 c = utf8n_to_uvchr((U8*)s, UTF8_MAXBYTES, &len,
041457d9 1172 uniflags);
80aecb99 1173
60a8b682 1174 /* Handle some of the three Greek sigmas cases.
8c01da3c
JH
1175 * Note that not all the possible combinations
1176 * are handled here: some of them are handled
1177 * by the standard folding rules, and some of
1178 * them (the character class or ANYOF cases)
1179 * are handled during compiletime in
1180 * regexec.c:S_regclass(). */
880bd946
JH
1181 if (c == (UV)UNICODE_GREEK_CAPITAL_LETTER_SIGMA ||
1182 c == (UV)UNICODE_GREEK_SMALL_LETTER_FINAL_SIGMA)
1183 c = (UV)UNICODE_GREEK_SMALL_LETTER_SIGMA;
80aecb99 1184
4cadc6a9 1185 REXEC_FBC_EXACTISH_CHECK(c == c1 || c == c2);
1aa99e6b 1186 }
09091399 1187 }
1aa99e6b
IH
1188 }
1189 else {
1190 if (c1 == c2)
4cadc6a9 1191 REXEC_FBC_EXACTISH_SCAN(*(U8*)s == c1);
1aa99e6b 1192 else
4cadc6a9 1193 REXEC_FBC_EXACTISH_SCAN(*(U8*)s == c1 || *(U8*)s == c2);
b3c9acc1
IZ
1194 }
1195 break;
bbce6d69 1196 case BOUNDL:
3280af22 1197 PL_reg_flags |= RF_tainted;
bbce6d69 1198 /* FALL THROUGH */
a0d0e21e 1199 case BOUND:
ffc61ed2 1200 if (do_utf8) {
12d33761 1201 if (s == PL_bostr)
ffc61ed2
JH
1202 tmp = '\n';
1203 else {
6136c704 1204 U8 * const r = reghop3((U8*)s, -1, (U8*)PL_bostr);
4ad0818d 1205 tmp = utf8n_to_uvchr(r, UTF8SKIP(r), 0, UTF8_ALLOW_DEFAULT);
ffc61ed2
JH
1206 }
1207 tmp = ((OP(c) == BOUND ?
9041c2e3 1208 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1a4fad37 1209 LOAD_UTF8_CHARCLASS_ALNUM();
4cadc6a9 1210 REXEC_FBC_UTF8_SCAN(
ffc61ed2 1211 if (tmp == !(OP(c) == BOUND ?
bb7a0f54 1212 (bool)swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) :
ffc61ed2
JH
1213 isALNUM_LC_utf8((U8*)s)))
1214 {
1215 tmp = !tmp;
4cadc6a9 1216 REXEC_FBC_TRYIT;
a687059c 1217 }
4cadc6a9 1218 );
a0d0e21e 1219 }
667bb95a 1220 else {
12d33761 1221 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
ffc61ed2 1222 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
4cadc6a9 1223 REXEC_FBC_SCAN(
ffc61ed2
JH
1224 if (tmp ==
1225 !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
1226 tmp = !tmp;
4cadc6a9 1227 REXEC_FBC_TRYIT;
a0ed51b3 1228 }
4cadc6a9 1229 );
a0ed51b3 1230 }
3b0527fe 1231 if ((!prog->minlen && tmp) && (!reginfo || regtry(reginfo, s)))
a0ed51b3
LW
1232 goto got_it;
1233 break;
bbce6d69 1234 case NBOUNDL:
3280af22 1235 PL_reg_flags |= RF_tainted;
bbce6d69 1236 /* FALL THROUGH */
a0d0e21e 1237 case NBOUND:
ffc61ed2 1238 if (do_utf8) {
12d33761 1239 if (s == PL_bostr)
ffc61ed2
JH
1240 tmp = '\n';
1241 else {
6136c704 1242 U8 * const r = reghop3((U8*)s, -1, (U8*)PL_bostr);
4ad0818d 1243 tmp = utf8n_to_uvchr(r, UTF8SKIP(r), 0, UTF8_ALLOW_DEFAULT);
ffc61ed2
JH
1244 }
1245 tmp = ((OP(c) == NBOUND ?
9041c2e3 1246 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1a4fad37 1247 LOAD_UTF8_CHARCLASS_ALNUM();
4cadc6a9 1248 REXEC_FBC_UTF8_SCAN(
ffc61ed2 1249 if (tmp == !(OP(c) == NBOUND ?
bb7a0f54 1250 (bool)swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) :
ffc61ed2
JH
1251 isALNUM_LC_utf8((U8*)s)))
1252 tmp = !tmp;
4cadc6a9
YO
1253 else REXEC_FBC_TRYIT;
1254 );
a0d0e21e 1255 }
667bb95a 1256 else {
12d33761 1257 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
ffc61ed2
JH
1258 tmp = ((OP(c) == NBOUND ?
1259 isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
4cadc6a9 1260 REXEC_FBC_SCAN(
ffc61ed2
JH
1261 if (tmp ==
1262 !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
1263 tmp = !tmp;
4cadc6a9
YO
1264 else REXEC_FBC_TRYIT;
1265 );
a0ed51b3 1266 }
3b0527fe 1267 if ((!prog->minlen && !tmp) && (!reginfo || regtry(reginfo, s)))
a0ed51b3
LW
1268 goto got_it;
1269 break;
a0d0e21e 1270 case ALNUM:
4cadc6a9
YO
1271 REXEC_FBC_CSCAN_PRELOAD(
1272 LOAD_UTF8_CHARCLASS_ALNUM(),
1273 swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8),
1274 isALNUM(*s)
1275 );
bbce6d69 1276 case ALNUML:
4cadc6a9
YO
1277 REXEC_FBC_CSCAN_TAINT(
1278 isALNUM_LC_utf8((U8*)s),
1279 isALNUM_LC(*s)
1280 );
a0d0e21e 1281 case NALNUM:
4cadc6a9
YO
1282 REXEC_FBC_CSCAN_PRELOAD(
1283 LOAD_UTF8_CHARCLASS_ALNUM(),
1284 !swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8),
1285 !isALNUM(*s)
1286 );
bbce6d69 1287 case NALNUML:
4cadc6a9
YO
1288 REXEC_FBC_CSCAN_TAINT(
1289 !isALNUM_LC_utf8((U8*)s),
1290 !isALNUM_LC(*s)
1291 );
a0d0e21e 1292 case SPACE:
4cadc6a9
YO
1293 REXEC_FBC_CSCAN_PRELOAD(
1294 LOAD_UTF8_CHARCLASS_SPACE(),
1295 *s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, do_utf8),
1296 isSPACE(*s)
1297 );
bbce6d69 1298 case SPACEL:
4cadc6a9
YO
1299 REXEC_FBC_CSCAN_TAINT(
1300 *s == ' ' || isSPACE_LC_utf8((U8*)s),
1301 isSPACE_LC(*s)
1302 );
a0d0e21e 1303 case NSPACE:
4cadc6a9
YO
1304 REXEC_FBC_CSCAN_PRELOAD(
1305 LOAD_UTF8_CHARCLASS_SPACE(),
1306 !(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, do_utf8)),
1307 !isSPACE(*s)
1308 );
bbce6d69 1309 case NSPACEL:
4cadc6a9
YO
1310 REXEC_FBC_CSCAN_TAINT(
1311 !(*s == ' ' || isSPACE_LC_utf8((U8*)s)),
1312 !isSPACE_LC(*s)
1313 );
a0d0e21e 1314 case DIGIT:
4cadc6a9
YO
1315 REXEC_FBC_CSCAN_PRELOAD(
1316 LOAD_UTF8_CHARCLASS_DIGIT(),
1317 swash_fetch(PL_utf8_digit,(U8*)s, do_utf8),
1318 isDIGIT(*s)
1319 );
b8c5462f 1320 case DIGITL:
4cadc6a9
YO
1321 REXEC_FBC_CSCAN_TAINT(
1322 isDIGIT_LC_utf8((U8*)s),
1323 isDIGIT_LC(*s)
1324 );
a0d0e21e 1325 case NDIGIT:
4cadc6a9
YO
1326 REXEC_FBC_CSCAN_PRELOAD(
1327 LOAD_UTF8_CHARCLASS_DIGIT(),
1328 !swash_fetch(PL_utf8_digit,(U8*)s, do_utf8),
1329 !isDIGIT(*s)
1330 );
b8c5462f 1331 case NDIGITL:
4cadc6a9
YO
1332 REXEC_FBC_CSCAN_TAINT(
1333 !isDIGIT_LC_utf8((U8*)s),
1334 !isDIGIT_LC(*s)
1335 );
07be1b83
YO
1336 case TRIE:
1337 /*Perl_croak(aTHX_ "panic: unknown regstclass TRIE");*/
1338 {
1339 const enum { trie_plain, trie_utf8, trie_utf8_fold }
1340 trie_type = do_utf8 ?
1341 (c->flags == EXACT ? trie_utf8 : trie_utf8_fold)
1342 : trie_plain;
1343 /* what trie are we using right now */
1344 reg_ac_data *aho
1345 = (reg_ac_data*)prog->data->data[ ARG( c ) ];
1346 reg_trie_data *trie=aho->trie;
1347
1348 const char *last_start = strend - trie->minlen;
6148ee25 1349#ifdef DEBUGGING
07be1b83 1350 const char *real_start = s;
6148ee25 1351#endif
07be1b83 1352 STRLEN maxlen = trie->maxlen;
be8e71aa
YO
1353 SV *sv_points;
1354 U8 **points; /* map of where we were in the input string
1355 when reading a given string. For ASCII this
1356 is unnecessary overhead as the relationship
1357 is always 1:1, but for unicode, especially
1358 case folded unicode this is not true. */
f9e705e8 1359 U8 foldbuf[ UTF8_MAXBYTES_CASE + 1 ];
07be1b83
YO
1360
1361 GET_RE_DEBUG_FLAGS_DECL;
1362
be8e71aa
YO
1363 /* We can't just allocate points here. We need to wrap it in
1364 * an SV so it gets freed properly if there is a croak while
1365 * running the match */
1366 ENTER;
1367 SAVETMPS;
1368 sv_points=newSV(maxlen * sizeof(U8 *));
1369 SvCUR_set(sv_points,
1370 maxlen * sizeof(U8 *));
1371 SvPOK_on(sv_points);
1372 sv_2mortal(sv_points);
1373 points=(U8**)SvPV_nolen(sv_points );
07be1b83
YO
1374
1375 if (trie->bitmap && trie_type != trie_utf8_fold) {
13a24bad 1376 while (s <= last_start && !TRIE_BITMAP_TEST(trie,*s) ) {
07be1b83
YO
1377 s++;
1378 }
1379 }
1380
1381 while (s <= last_start) {
1382 const U32 uniflags = UTF8_ALLOW_DEFAULT;
1383 U8 *uc = (U8*)s;
1384 U16 charid = 0;
1385 U32 base = 1;
1386 U32 state = 1;
1387 UV uvc = 0;
1388 STRLEN len = 0;
1389 STRLEN foldlen = 0;
1390 U8 *uscan = (U8*)NULL;
1391 U8 *leftmost = NULL;
1392
1393 U32 pointpos = 0;
1394
1395 while ( state && uc <= (U8*)strend ) {
1396 int failed=0;
1397 if (aho->states[ state ].wordnum) {
1398 U8 *lpos= points[ (pointpos - trie->wordlen[aho->states[ state ].wordnum-1] ) % maxlen ];
1399 if (!leftmost || lpos < leftmost)
1400 leftmost= lpos;
1401 if (base==0) break;
1402 }
1403 points[pointpos++ % maxlen]= uc;
4cadc6a9
YO
1404 REXEC_TRIE_READ_CHAR(trie_type, trie, uc, uscan, len,
1405 uvc, charid, foldlen, foldbuf, uniflags);
07be1b83
YO
1406 DEBUG_TRIE_EXECUTE_r(
1407 PerlIO_printf(Perl_debug_log,
1408 "Pos: %d Charid:%3x CV:%4"UVxf" ",
1409 (int)((const char*)uc - real_start), charid, uvc)
1410 );
1411 uc += len;
1412
1413 do {
6148ee25 1414#ifdef DEBUGGING
07be1b83 1415 U32 word = aho->states[ state ].wordnum;
6148ee25 1416#endif
07be1b83
YO
1417 base = aho->states[ state ].trans.base;
1418
1419 DEBUG_TRIE_EXECUTE_r(
1420 PerlIO_printf( Perl_debug_log,
1421 "%sState: %4"UVxf", Base: 0x%-4"UVxf" uvc=%"UVxf" word=%"UVxf"\n",
1422 failed ? "Fail transition to " : "",
1423 state, base, uvc, word)
1424 );
1425 if ( base ) {
1426 U32 tmp;
1427 if (charid &&
1428 (base + charid > trie->uniquecharcount )
1429 && (base + charid - 1 - trie->uniquecharcount
1430 < trie->lasttrans)
1431 && trie->trans[base + charid - 1 -
1432 trie->uniquecharcount].check == state
1433 && (tmp=trie->trans[base + charid - 1 -
1434 trie->uniquecharcount ].next))
1435 {
1436 state = tmp;
1437 break;
1438 }
1439 else {
1440 failed++;
1441 if ( state == 1 )
1442 break;
1443 else
1444 state = aho->fail[state];
1445 }
1446 }
1447 else {
1448 /* we must be accepting here */
1449 failed++;
1450 break;
1451 }
1452 } while(state);
1453 if (failed) {
1454 if (leftmost)
1455 break;
1456 else if (!charid && trie->bitmap && trie_type != trie_utf8_fold) {
13a24bad 1457 while ( uc <= (U8*)last_start && !TRIE_BITMAP_TEST(trie,*uc) ) {
07be1b83
YO
1458 uc++;
1459 }
1460 }
1461 }
1462 }
1463 if ( aho->states[ state ].wordnum ) {
1464 U8 *lpos = points[ (pointpos - trie->wordlen[aho->states[ state ].wordnum-1]) % maxlen ];
1465 if (!leftmost || lpos < leftmost)
1466 leftmost = lpos;
1467 }
1468 DEBUG_TRIE_EXECUTE_r(
1469 PerlIO_printf( Perl_debug_log,
1470 "%sState: %4"UVxf", Base: 0x%-4"UVxf" uvc=%"UVxf"\n",
1471 "All done: ",
1472 state, base, uvc)
1473 );
1474 if (leftmost) {
1475 s = (char*)leftmost;
be8e71aa
YO
1476 if (!reginfo || regtry(reginfo, s)) {
1477 FREETMPS;
1478 LEAVE;
07be1b83 1479 goto got_it;
be8e71aa 1480 }
07be1b83
YO
1481 s = HOPc(s,1);
1482 } else {
1483 break;
1484 }
1485 }
be8e71aa
YO
1486 FREETMPS;
1487 LEAVE;
07be1b83
YO
1488 }
1489 break;
b3c9acc1 1490 default:
3c3eec57
GS
1491 Perl_croak(aTHX_ "panic: unknown regstclass %d", (int)OP(c));
1492 break;
d6a28714 1493 }
6eb5f6b9
JH
1494 return 0;
1495 got_it:
1496 return s;
1497}
1498
1499/*
1500 - regexec_flags - match a regexp against a string
1501 */
1502I32
1503Perl_regexec_flags(pTHX_ register regexp *prog, char *stringarg, register char *strend,
1504 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
1505/* strend: pointer to null at end of string */
1506/* strbeg: real beginning of string */
1507/* minend: end of match must be >=minend after stringarg. */
1508/* data: May be used for some additional optimizations. */
1509/* nosave: For optimizations. */
1510{
97aff369 1511 dVAR;
6eb5f6b9
JH
1512 register char *s;
1513 register regnode *c;
1514 register char *startpos = stringarg;
6eb5f6b9
JH
1515 I32 minlen; /* must match at least this many chars */
1516 I32 dontbother = 0; /* how many characters not to try at end */
6eb5f6b9
JH
1517 I32 end_shift = 0; /* Same for the end. */ /* CC */
1518 I32 scream_pos = -1; /* Internal iterator of scream. */
ccac19ea 1519 char *scream_olds = NULL;
3dab1dad 1520 SV* const oreplsv = GvSV(PL_replgv);
1df70142 1521 const bool do_utf8 = DO_UTF8(sv);
2757e526 1522 I32 multiline;
0df25f3d 1523
3b0527fe 1524 regmatch_info reginfo; /* create some info to pass to regtry etc */
a3621e74
YO
1525
1526 GET_RE_DEBUG_FLAGS_DECL;
1527
9d4ba2ae 1528 PERL_UNUSED_ARG(data);
6eb5f6b9
JH
1529
1530 /* Be paranoid... */
1531 if (prog == NULL || startpos == NULL) {
1532 Perl_croak(aTHX_ "NULL regexp parameter");
1533 return 0;
1534 }
1535
2757e526 1536 multiline = prog->reganch & PMf_MULTILINE;
3b0527fe 1537 reginfo.prog = prog;
2757e526 1538
bac06658
JH
1539 RX_MATCH_UTF8_set(prog, do_utf8);
1540
6eb5f6b9 1541 minlen = prog->minlen;
61a36c01 1542 if (strend - startpos < minlen) {
a3621e74 1543 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
a72c7584
JH
1544 "String too short [regexec_flags]...\n"));
1545 goto phooey;
1aa99e6b 1546 }
6eb5f6b9 1547
6eb5f6b9
JH
1548 /* Check validity of program. */
1549 if (UCHARAT(prog->program) != REG_MAGIC) {
1550 Perl_croak(aTHX_ "corrupted regexp program");
1551 }
1552
1553 PL_reg_flags = 0;
1554 PL_reg_eval_set = 0;
1555 PL_reg_maxiter = 0;
1556
1557 if (prog->reganch & ROPT_UTF8)
1558 PL_reg_flags |= RF_utf8;
1559
1560 /* Mark beginning of line for ^ and lookbehind. */
3b0527fe 1561 reginfo.bol = startpos; /* XXX not used ??? */
6eb5f6b9 1562 PL_bostr = strbeg;
3b0527fe 1563 reginfo.sv = sv;
6eb5f6b9
JH
1564
1565 /* Mark end of line for $ (and such) */
1566 PL_regeol = strend;
1567
1568 /* see how far we have to get to not match where we matched before */
3b0527fe 1569 reginfo.till = startpos+minend;
6eb5f6b9 1570
6eb5f6b9
JH
1571 /* If there is a "must appear" string, look for it. */
1572 s = startpos;
1573
3b0527fe 1574 if (prog->reganch & ROPT_GPOS_SEEN) { /* Need to set reginfo->ganch */
6eb5f6b9
JH
1575 MAGIC *mg;
1576
1577 if (flags & REXEC_IGNOREPOS) /* Means: check only at start */
3b0527fe 1578 reginfo.ganch = startpos;
6eb5f6b9
JH
1579 else if (sv && SvTYPE(sv) >= SVt_PVMG
1580 && SvMAGIC(sv)
14befaf4
DM
1581 && (mg = mg_find(sv, PERL_MAGIC_regex_global))
1582 && mg->mg_len >= 0) {
3b0527fe 1583 reginfo.ganch = strbeg + mg->mg_len; /* Defined pos() */
6eb5f6b9 1584 if (prog->reganch & ROPT_ANCH_GPOS) {
3b0527fe 1585 if (s > reginfo.ganch)
6eb5f6b9 1586 goto phooey;
3b0527fe 1587 s = reginfo.ganch;
6eb5f6b9
JH
1588 }
1589 }
1590 else /* pos() not defined */
3b0527fe 1591 reginfo.ganch = strbeg;
6eb5f6b9
JH
1592 }
1593
a0714e2c 1594 if (!(flags & REXEC_CHECKED) && (prog->check_substr != NULL || prog->check_utf8 != NULL)) {
6eb5f6b9
JH
1595 re_scream_pos_data d;
1596
1597 d.scream_olds = &scream_olds;
1598 d.scream_pos = &scream_pos;
1599 s = re_intuit_start(prog, sv, s, strend, flags, &d);
3fa9c3d7 1600 if (!s) {
a3621e74 1601 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Not present...\n"));
6eb5f6b9 1602 goto phooey; /* not present */
3fa9c3d7 1603 }
6eb5f6b9
JH
1604 }
1605
a3621e74 1606 DEBUG_EXECUTE_r({
0df25f3d
YO
1607 RE_PV_DISPLAY_DECL(s0, len0, UTF,
1608 PERL_DEBUG_PAD_ZERO(0), prog->precomp, prog->prelen, 60);
1609 RE_PV_DISPLAY_DECL(s1, len1, do_utf8,
1610 PERL_DEBUG_PAD_ZERO(1), startpos, strend - startpos, 60);
1611
2a782b5b
JH
1612 if (!PL_colorset)
1613 reginitcolors();
1614 PerlIO_printf(Perl_debug_log,
a0288114 1615 "%sMatching REx%s \"%s%*.*s%s%s\" against \"%s%.*s%s%s\"\n",
e4584336 1616 PL_colors[4], PL_colors[5], PL_colors[0],
9e55ce06 1617 len0, len0, s0,
2a782b5b 1618 PL_colors[1],
9e55ce06 1619 len0 > 60 ? "..." : "",
2a782b5b 1620 PL_colors[0],
9e55ce06
JH
1621 (int)(len1 > 60 ? 60 : len1),
1622 s1, PL_colors[1],
1623 (len1 > 60 ? "..." : "")
2a782b5b
JH
1624 );
1625 });
6eb5f6b9
JH
1626
1627 /* Simplest case: anchored match need be tried only once. */
1628 /* [unless only anchor is BOL and multiline is set] */
1629 if (prog->reganch & (ROPT_ANCH & ~ROPT_ANCH_GPOS)) {
3b0527fe 1630 if (s == startpos && regtry(&reginfo, startpos))
6eb5f6b9 1631 goto got_it;
7fba1cd6 1632 else if (multiline || (prog->reganch & ROPT_IMPLICIT)
6eb5f6b9
JH
1633 || (prog->reganch & ROPT_ANCH_MBOL)) /* XXXX SBOL? */
1634 {
1635 char *end;
1636
1637 if (minlen)
1638 dontbother = minlen - 1;
1aa99e6b 1639 end = HOP3c(strend, -dontbother, strbeg) - 1;
6eb5f6b9 1640 /* for multiline we only have to try after newlines */
33b8afdf 1641 if (prog->check_substr || prog->check_utf8) {
6eb5f6b9
JH
1642 if (s == startpos)
1643 goto after_try;
1644 while (1) {
3b0527fe 1645 if (regtry(&reginfo, s))
6eb5f6b9
JH
1646 goto got_it;
1647 after_try:
1648 if (s >= end)
1649 goto phooey;
1650 if (prog->reganch & RE_USE_INTUIT) {
1651 s = re_intuit_start(prog, sv, s + 1, strend, flags, NULL);
1652 if (!s)
1653 goto phooey;
1654 }
1655 else
1656 s++;
1657 }
1658 } else {
1659 if (s > startpos)
1660 s--;
1661 while (s < end) {
1662 if (*s++ == '\n') { /* don't need PL_utf8skip here */
3b0527fe 1663 if (regtry(&reginfo, s))
6eb5f6b9
JH
1664 goto got_it;
1665 }
1666 }
1667 }
1668 }
1669 goto phooey;
1670 } else if (prog->reganch & ROPT_ANCH_GPOS) {
3b0527fe 1671 if (regtry(&reginfo, reginfo.ganch))
6eb5f6b9
JH
1672 goto got_it;
1673 goto phooey;
1674 }
1675
1676 /* Messy cases: unanchored match. */
33b8afdf 1677 if ((prog->anchored_substr || prog->anchored_utf8) && prog->reganch & ROPT_SKIP) {
6eb5f6b9
JH
1678 /* we have /x+whatever/ */
1679 /* it must be a one character string (XXXX Except UTF?) */
33b8afdf 1680 char ch;
bf93d4cc
GS
1681#ifdef DEBUGGING
1682 int did_match = 0;
1683#endif
33b8afdf
JH
1684 if (!(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr))
1685 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
3f7c398e 1686 ch = SvPVX_const(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr)[0];
bf93d4cc 1687
1aa99e6b 1688 if (do_utf8) {
4cadc6a9 1689 REXEC_FBC_SCAN(
6eb5f6b9 1690 if (*s == ch) {
a3621e74 1691 DEBUG_EXECUTE_r( did_match = 1 );
3b0527fe 1692 if (regtry(&reginfo, s)) goto got_it;
6eb5f6b9
JH
1693 s += UTF8SKIP(s);
1694 while (s < strend && *s == ch)
1695 s += UTF8SKIP(s);
1696 }
4cadc6a9 1697 );
6eb5f6b9
JH
1698 }
1699 else {
4cadc6a9 1700 REXEC_FBC_SCAN(
6eb5f6b9 1701 if (*s == ch) {
a3621e74 1702 DEBUG_EXECUTE_r( did_match = 1 );
3b0527fe 1703 if (regtry(&reginfo, s)) goto got_it;
6eb5f6b9
JH
1704 s++;
1705 while (s < strend && *s == ch)
1706 s++;
1707 }
4cadc6a9 1708 );
6eb5f6b9 1709 }
a3621e74 1710 DEBUG_EXECUTE_r(if (!did_match)
bf93d4cc 1711 PerlIO_printf(Perl_debug_log,
b7953727
JH
1712 "Did not find anchored character...\n")
1713 );
6eb5f6b9 1714 }
a0714e2c
SS
1715 else if (prog->anchored_substr != NULL
1716 || prog->anchored_utf8 != NULL
1717 || ((prog->float_substr != NULL || prog->float_utf8 != NULL)
33b8afdf
JH
1718 && prog->float_max_offset < strend - s)) {
1719 SV *must;
1720 I32 back_max;
1721 I32 back_min;
1722 char *last;
6eb5f6b9 1723 char *last1; /* Last position checked before */
bf93d4cc
GS
1724#ifdef DEBUGGING
1725 int did_match = 0;
1726#endif
33b8afdf
JH
1727 if (prog->anchored_substr || prog->anchored_utf8) {
1728 if (!(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr))
1729 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1730 must = do_utf8 ? prog->anchored_utf8 : prog->anchored_substr;
1731 back_max = back_min = prog->anchored_offset;
1732 } else {
1733 if (!(do_utf8 ? prog->float_utf8 : prog->float_substr))
1734 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1735 must = do_utf8 ? prog->float_utf8 : prog->float_substr;
1736 back_max = prog->float_max_offset;
1737 back_min = prog->float_min_offset;
1738 }
1739 if (must == &PL_sv_undef)
1740 /* could not downgrade utf8 check substring, so must fail */
1741 goto phooey;
1742
1743 last = HOP3c(strend, /* Cannot start after this */
1744 -(I32)(CHR_SVLEN(must)
1745 - (SvTAIL(must) != 0) + back_min), strbeg);
6eb5f6b9
JH
1746
1747 if (s > PL_bostr)
1748 last1 = HOPc(s, -1);
1749 else
1750 last1 = s - 1; /* bogus */
1751
a0288114 1752 /* XXXX check_substr already used to find "s", can optimize if
6eb5f6b9
JH
1753 check_substr==must. */
1754 scream_pos = -1;
1755 dontbother = end_shift;
1756 strend = HOPc(strend, -dontbother);
1757 while ( (s <= last) &&
9041c2e3 1758 ((flags & REXEC_SCREAM)
1aa99e6b 1759 ? (s = screaminstr(sv, must, HOP3c(s, back_min, strend) - strbeg,
6eb5f6b9 1760 end_shift, &scream_pos, 0))
1aa99e6b 1761 : (s = fbm_instr((unsigned char*)HOP3(s, back_min, strend),
9041c2e3 1762 (unsigned char*)strend, must,
7fba1cd6 1763 multiline ? FBMrf_MULTILINE : 0))) ) {
4addbd3b
HS
1764 /* we may be pointing at the wrong string */
1765 if ((flags & REXEC_SCREAM) && RX_MATCH_COPIED(prog))
3f7c398e 1766 s = strbeg + (s - SvPVX_const(sv));
a3621e74 1767 DEBUG_EXECUTE_r( did_match = 1 );
6eb5f6b9
JH
1768 if (HOPc(s, -back_max) > last1) {
1769 last1 = HOPc(s, -back_min);
1770 s = HOPc(s, -back_max);
1771 }
1772 else {
52657f30 1773 char * const t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
6eb5f6b9
JH
1774
1775 last1 = HOPc(s, -back_min);
52657f30 1776 s = t;
6eb5f6b9 1777 }
1aa99e6b 1778 if (do_utf8) {
6eb5f6b9 1779 while (s <= last1) {
3b0527fe 1780 if (regtry(&reginfo, s))
6eb5f6b9
JH
1781 goto got_it;
1782 s += UTF8SKIP(s);
1783 }
1784 }
1785 else {
1786 while (s <= last1) {
3b0527fe 1787 if (regtry(&reginfo, s))
6eb5f6b9
JH
1788 goto got_it;
1789 s++;
1790 }
1791 }
1792 }
a3621e74 1793 DEBUG_EXECUTE_r(if (!did_match)
b7953727 1794 PerlIO_printf(Perl_debug_log,
a0288114 1795 "Did not find %s substr \"%s%.*s%s\"%s...\n",
33b8afdf 1796 ((must == prog->anchored_substr || must == prog->anchored_utf8)
bf93d4cc
GS
1797 ? "anchored" : "floating"),
1798 PL_colors[0],
1799 (int)(SvCUR(must) - (SvTAIL(must)!=0)),
3f7c398e 1800 SvPVX_const(must),
b7953727
JH
1801 PL_colors[1], (SvTAIL(must) ? "$" : ""))
1802 );
6eb5f6b9
JH
1803 goto phooey;
1804 }
155aba94 1805 else if ((c = prog->regstclass)) {
f14c76ed 1806 if (minlen) {
be8e71aa 1807 const OPCODE op = OP(prog->regstclass);
66e933ab 1808 /* don't bother with what can't match */
07be1b83 1809 if (PL_regkind[op] != EXACT && op != CANY && op != TRIE)
f14c76ed
RGS
1810 strend = HOPc(strend, -(minlen - 1));
1811 }
a3621e74 1812 DEBUG_EXECUTE_r({
be8e71aa 1813 SV * const prop = sv_newmortal();
32fc9b6a 1814 regprop(prog, prop, c);
0df25f3d
YO
1815 {
1816 RE_PV_DISPLAY_DECL(s0,len0,UTF,
1817 PERL_DEBUG_PAD_ZERO(0),SvPVX_const(prop),SvCUR(prop),60);
1818 RE_PV_DISPLAY_DECL(s1,len1,UTF,
1819 PERL_DEBUG_PAD_ZERO(1),s,strend-s,60);
1820 PerlIO_printf(Perl_debug_log,
1821 "Matching stclass \"%*.*s\" against \"%*.*s\" (%d chars)\n",
1822 len0, len0, s0,
1823 len1, len1, s1, (int)(strend - s));
1824 }
ffc61ed2 1825 });
3b0527fe 1826 if (find_byclass(prog, c, s, strend, &reginfo))
6eb5f6b9 1827 goto got_it;
07be1b83 1828 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Contradicts stclass... [regexec_flags]\n"));
d6a28714
JH
1829 }
1830 else {
1831 dontbother = 0;
a0714e2c 1832 if (prog->float_substr != NULL || prog->float_utf8 != NULL) {
33b8afdf 1833 /* Trim the end. */
d6a28714 1834 char *last;
33b8afdf
JH
1835 SV* float_real;
1836
1837 if (!(do_utf8 ? prog->float_utf8 : prog->float_substr))
1838 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1839 float_real = do_utf8 ? prog->float_utf8 : prog->float_substr;
d6a28714
JH
1840
1841 if (flags & REXEC_SCREAM) {
33b8afdf 1842 last = screaminstr(sv, float_real, s - strbeg,
d6a28714
JH
1843 end_shift, &scream_pos, 1); /* last one */
1844 if (!last)
ffc61ed2 1845 last = scream_olds; /* Only one occurrence. */
4addbd3b
HS
1846 /* we may be pointing at the wrong string */
1847 else if (RX_MATCH_COPIED(prog))
3f7c398e 1848 s = strbeg + (s - SvPVX_const(sv));
b8c5462f 1849 }
d6a28714
JH
1850 else {
1851 STRLEN len;
cfd0369c 1852 const char * const little = SvPV_const(float_real, len);
d6a28714 1853
33b8afdf 1854 if (SvTAIL(float_real)) {
d6a28714
JH
1855 if (memEQ(strend - len + 1, little, len - 1))
1856 last = strend - len + 1;
7fba1cd6 1857 else if (!multiline)
9041c2e3 1858 last = memEQ(strend - len, little, len)
bd61b366 1859 ? strend - len : NULL;
b8c5462f 1860 else
d6a28714
JH
1861 goto find_last;
1862 } else {
1863 find_last:
9041c2e3 1864 if (len)
d6a28714 1865 last = rninstr(s, strend, little, little + len);
b8c5462f 1866 else
a0288114 1867 last = strend; /* matching "$" */
b8c5462f 1868 }
b8c5462f 1869 }
bf93d4cc 1870 if (last == NULL) {
a3621e74 1871 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
bf93d4cc 1872 "%sCan't trim the tail, match fails (should not happen)%s\n",
e4584336 1873 PL_colors[4], PL_colors[5]));
bf93d4cc
GS
1874 goto phooey; /* Should not happen! */
1875 }
d6a28714
JH
1876 dontbother = strend - last + prog->float_min_offset;
1877 }
1878 if (minlen && (dontbother < minlen))
1879 dontbother = minlen - 1;
1880 strend -= dontbother; /* this one's always in bytes! */
1881 /* We don't know much -- general case. */
1aa99e6b 1882 if (do_utf8) {
d6a28714 1883 for (;;) {
3b0527fe 1884 if (regtry(&reginfo, s))
d6a28714
JH
1885 goto got_it;
1886 if (s >= strend)
1887 break;
b8c5462f 1888 s += UTF8SKIP(s);
d6a28714
JH
1889 };
1890 }
1891 else {
1892 do {
3b0527fe 1893 if (regtry(&reginfo, s))
d6a28714
JH
1894 goto got_it;
1895 } while (s++ < strend);
1896 }
1897 }
1898
1899 /* Failure. */
1900 goto phooey;
1901
1902got_it:
1903 RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted);
1904
1905 if (PL_reg_eval_set) {
1906 /* Preserve the current value of $^R */
1907 if (oreplsv != GvSV(PL_replgv))
1908 sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is
1909 restored, the value remains
1910 the same. */
4f639d21 1911 restore_pos(aTHX_ prog);
d6a28714
JH
1912 }
1913
1914 /* make sure $`, $&, $', and $digit will work later */
1915 if ( !(flags & REXEC_NOT_FIRST) ) {
ed252734 1916 RX_MATCH_COPY_FREE(prog);
d6a28714 1917 if (flags & REXEC_COPY_STR) {
be8e71aa 1918 const I32 i = PL_regeol - startpos + (stringarg - strbeg);
f8c7b90f 1919#ifdef PERL_OLD_COPY_ON_WRITE
ed252734
NC
1920 if ((SvIsCOW(sv)
1921 || (SvFLAGS(sv) & CAN_COW_MASK) == CAN_COW_FLAGS)) {
1922 if (DEBUG_C_TEST) {
1923 PerlIO_printf(Perl_debug_log,
1924 "Copy on write: regexp capture, type %d\n",
1925 (int) SvTYPE(sv));
1926 }
1927 prog->saved_copy = sv_setsv_cow(prog->saved_copy, sv);
d5263905 1928 prog->subbeg = (char *)SvPVX_const(prog->saved_copy);
ed252734
NC
1929 assert (SvPOKp(prog->saved_copy));
1930 } else
1931#endif
1932 {
1933 RX_MATCH_COPIED_on(prog);
1934 s = savepvn(strbeg, i);
1935 prog->subbeg = s;
1936 }
d6a28714 1937 prog->sublen = i;
d6a28714
JH
1938 }
1939 else {
1940 prog->subbeg = strbeg;
1941 prog->sublen = PL_regeol - strbeg; /* strend may have been modified */
1942 }
1943 }
9041c2e3 1944
d6a28714
JH
1945 return 1;
1946
1947phooey:
a3621e74 1948 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch failed%s\n",
e4584336 1949 PL_colors[4], PL_colors[5]));
d6a28714 1950 if (PL_reg_eval_set)
4f639d21 1951 restore_pos(aTHX_ prog);
d6a28714
JH
1952 return 0;
1953}
1954
1955/*
1956 - regtry - try match at specific point
1957 */
1958STATIC I32 /* 0 failure, 1 success */
3b0527fe 1959S_regtry(pTHX_ const regmatch_info *reginfo, char *startpos)
d6a28714 1960{
97aff369 1961 dVAR;
d6a28714
JH
1962 register I32 *sp;
1963 register I32 *ep;
1964 CHECKPOINT lastcp;
3b0527fe 1965 regexp *prog = reginfo->prog;
a3621e74 1966 GET_RE_DEBUG_FLAGS_DECL;
d6a28714 1967
02db2b7b
IZ
1968#ifdef DEBUGGING
1969 PL_regindent = 0; /* XXXX Not good when matches are reenterable... */
1970#endif
d6a28714
JH
1971 if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) {
1972 MAGIC *mg;
1973
1974 PL_reg_eval_set = RS_init;
a3621e74 1975 DEBUG_EXECUTE_r(DEBUG_s(
b900a521
JH
1976 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %"IVdf"\n",
1977 (IV)(PL_stack_sp - PL_stack_base));
d6a28714 1978 ));
e8347627 1979 SAVEI32(cxstack[cxstack_ix].blk_oldsp);
d6a28714
JH
1980 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
1981 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
1982 SAVETMPS;
1983 /* Apparently this is not needed, judging by wantarray. */
e8347627 1984 /* SAVEI8(cxstack[cxstack_ix].blk_gimme);
d6a28714
JH
1985 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
1986
3b0527fe 1987 if (reginfo->sv) {
d6a28714 1988 /* Make $_ available to executed code. */
3b0527fe 1989 if (reginfo->sv != DEFSV) {
59f00321 1990 SAVE_DEFSV;
3b0527fe 1991 DEFSV = reginfo->sv;
b8c5462f 1992 }
d6a28714 1993
3b0527fe
DM
1994 if (!(SvTYPE(reginfo->sv) >= SVt_PVMG && SvMAGIC(reginfo->sv)
1995 && (mg = mg_find(reginfo->sv, PERL_MAGIC_regex_global)))) {
d6a28714 1996 /* prepare for quick setting of pos */
d300d9fa
NC
1997#ifdef PERL_OLD_COPY_ON_WRITE
1998 if (SvIsCOW(sv))
1999 sv_force_normal_flags(sv, 0);
2000#endif
3dab1dad 2001 mg = sv_magicext(reginfo->sv, NULL, PERL_MAGIC_regex_global,
d300d9fa 2002 &PL_vtbl_mglob, NULL, 0);
d6a28714 2003 mg->mg_len = -1;
b8c5462f 2004 }
d6a28714
JH
2005 PL_reg_magic = mg;
2006 PL_reg_oldpos = mg->mg_len;
4f639d21 2007 SAVEDESTRUCTOR_X(restore_pos, prog);
d6a28714 2008 }
09687e5a 2009 if (!PL_reg_curpm) {
a02a5408 2010 Newxz(PL_reg_curpm, 1, PMOP);
09687e5a
AB
2011#ifdef USE_ITHREADS
2012 {
be8e71aa 2013 SV* const repointer = newSViv(0);
577e12cc 2014 /* so we know which PL_regex_padav element is PL_reg_curpm */
35061a7e 2015 SvFLAGS(repointer) |= SVf_BREAK;
09687e5a
AB
2016 av_push(PL_regex_padav,repointer);
2017 PL_reg_curpm->op_pmoffset = av_len(PL_regex_padav);
2018 PL_regex_pad = AvARRAY(PL_regex_padav);
2019 }
2020#endif
2021 }
aaa362c4 2022 PM_SETRE(PL_reg_curpm, prog);
d6a28714
JH
2023 PL_reg_oldcurpm = PL_curpm;
2024 PL_curpm = PL_reg_curpm;
2025 if (RX_MATCH_COPIED(prog)) {
2026 /* Here is a serious problem: we cannot rewrite subbeg,
2027 since it may be needed if this match fails. Thus
2028 $` inside (?{}) could fail... */
2029 PL_reg_oldsaved = prog->subbeg;
2030 PL_reg_oldsavedlen = prog->sublen;
f8c7b90f 2031#ifdef PERL_OLD_COPY_ON_WRITE
ed252734
NC
2032 PL_nrs = prog->saved_copy;
2033#endif
d6a28714
JH
2034 RX_MATCH_COPIED_off(prog);
2035 }
2036 else
bd61b366 2037 PL_reg_oldsaved = NULL;
d6a28714
JH
2038 prog->subbeg = PL_bostr;
2039 prog->sublen = PL_regeol - PL_bostr; /* strend may have been modified */
2040 }
973dddac 2041 prog->startp[0] = startpos - PL_bostr;
d6a28714
JH
2042 PL_reginput = startpos;
2043 PL_regstartp = prog->startp;
2044 PL_regendp = prog->endp;
2045 PL_reglastparen = &prog->lastparen;
a01268b5 2046 PL_reglastcloseparen = &prog->lastcloseparen;
d6a28714 2047 prog->lastparen = 0;
03994de8 2048 prog->lastcloseparen = 0;
d6a28714 2049 PL_regsize = 0;
a3621e74 2050 DEBUG_EXECUTE_r(PL_reg_starttry = startpos);
d6a28714
JH
2051 if (PL_reg_start_tmpl <= prog->nparens) {
2052 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
2053 if(PL_reg_start_tmp)
2054 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2055 else
a02a5408 2056 Newx(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
d6a28714
JH
2057 }
2058
2059 /* XXXX What this code is doing here?!!! There should be no need
2060 to do this again and again, PL_reglastparen should take care of
3dd2943c 2061 this! --ilya*/
dafc8851
JH
2062
2063 /* Tests pat.t#187 and split.t#{13,14} seem to depend on this code.
2064 * Actually, the code in regcppop() (which Ilya may be meaning by
daf18116
JH
2065 * PL_reglastparen), is not needed at all by the test suite
2066 * (op/regexp, op/pat, op/split), but that code is needed, oddly
2067 * enough, for building DynaLoader, or otherwise this
2068 * "Error: '*' not in typemap in DynaLoader.xs, line 164"
2069 * will happen. Meanwhile, this code *is* needed for the
2070 * above-mentioned test suite tests to succeed. The common theme
2071 * on those tests seems to be returning null fields from matches.
2072 * --jhi */
dafc8851 2073#if 1
d6a28714
JH
2074 sp = prog->startp;
2075 ep = prog->endp;
2076 if (prog->nparens) {
097eb12c 2077 register I32 i;
eb160463 2078 for (i = prog->nparens; i > (I32)*PL_reglastparen; i--) {
d6a28714
JH
2079 *++sp = -1;
2080 *++ep = -1;
2081 }
2082 }
dafc8851 2083#endif
02db2b7b 2084 REGCP_SET(lastcp);
3b0527fe 2085 if (regmatch(reginfo, prog->program + 1)) {
d6a28714
JH
2086 prog->endp[0] = PL_reginput - PL_bostr;
2087 return 1;
2088 }
02db2b7b 2089 REGCP_UNWIND(lastcp);
d6a28714
JH
2090 return 0;
2091}
2092
02db2b7b 2093
8ba1375e
MJD
2094#define sayYES goto yes
2095#define sayNO goto no
e0f9d4a8 2096#define sayNO_ANYOF goto no_anyof
8ba1375e 2097#define sayYES_FINAL goto yes_final
8ba1375e
MJD
2098#define sayNO_FINAL goto no_final
2099#define sayNO_SILENT goto do_no
2100#define saySAME(x) if (x) goto yes; else goto no
2101
3ab3c9b4 2102#define CACHEsayNO STMT_START { \
3298f257
DM
2103 if (st->u.whilem.cache_offset | st->u.whilem.cache_bit) \
2104 PL_reg_poscache[st->u.whilem.cache_offset] |= \
2105 (1<<st->u.whilem.cache_bit); \
3ab3c9b4
HS
2106 sayNO; \
2107} STMT_END
2108
3298f257 2109
a3621e74
YO
2110/* this is used to determine how far from the left messages like
2111 'failed...' are printed. Currently 29 makes these messages line
2112 up with the opcode they refer to. Earlier perls used 25 which
2113 left these messages outdented making reviewing a debug output
2114 quite difficult.
2115*/
2116#define REPORT_CODE_OFF 29
2117
2118
2119/* Make sure there is a test for this +1 options in re_tests */
2120#define TRIE_INITAL_ACCEPT_BUFFLEN 4;
2121
40a82448
DM
2122#define CHRTEST_UNINIT -1001 /* c1/c2 haven't been calculated yet */
2123#define CHRTEST_VOID -1000 /* the c1/c2 "next char" test should be skipped */
9e137952 2124
86545054
DM
2125#define SLAB_FIRST(s) (&(s)->states[0])
2126#define SLAB_LAST(s) (&(s)->states[PERL_REGMATCH_SLAB_SLOTS-1])
2127
5d9a96ca
DM
2128/* grab a new slab and return the first slot in it */
2129
2130STATIC regmatch_state *
2131S_push_slab(pTHX)
2132{
54df2634
NC
2133#if PERL_VERSION < 9
2134 dMY_CXT;
2135#endif
5d9a96ca
DM
2136 regmatch_slab *s = PL_regmatch_slab->next;
2137 if (!s) {
2138 Newx(s, 1, regmatch_slab);
2139 s->prev = PL_regmatch_slab;
2140 s->next = NULL;
2141 PL_regmatch_slab->next = s;
2142 }
2143 PL_regmatch_slab = s;
86545054 2144 return SLAB_FIRST(s);
5d9a96ca 2145}
5b47454d 2146
95b24440
DM
2147/* simulate a recursive call to regmatch */
2148
2149#define REGMATCH(ns, where) \
5d9a96ca
DM
2150 st->scan = scan; \
2151 scan = (ns); \
2152 st->resume_state = resume_##where; \
95b24440
DM
2153 goto start_recurse; \
2154 resume_point_##where:
2155
40a82448
DM
2156/* push a new state then goto it */
2157
2158#define PUSH_STATE_GOTO(state, node) \
2159 scan = node; \
2160 st->resume_state = state; \
2161 goto push_state;
2162
2163/* push a new state with success backtracking, then goto it */
2164
2165#define PUSH_YES_STATE_GOTO(state, node) \
2166 scan = node; \
2167 st->resume_state = state; \
2168 goto push_yes_state;
2169
aa283a38 2170
aa283a38 2171
d6a28714
JH
2172/*
2173 - regmatch - main matching routine
2174 *
2175 * Conceptually the strategy is simple: check to see whether the current
2176 * node matches, call self recursively to see whether the rest matches,
2177 * and then act accordingly. In practice we make some effort to avoid
2178 * recursion, in particular by going through "ordinary" nodes (that don't
2179 * need to know whether the rest of the match failed) by a loop instead of
2180 * by recursion.
2181 */
2182/* [lwall] I've hoisted the register declarations to the outer block in order to
2183 * maybe save a little bit of pushing and popping on the stack. It also takes
2184 * advantage of machines that use a register save mask on subroutine entry.
95b24440
DM
2185 *
2186 * This function used to be heavily recursive, but since this had the
2187 * effect of blowing the CPU stack on complex regexes, it has been
2188 * restructured to be iterative, and to save state onto the heap rather
2189 * than the stack. Essentially whereever regmatch() used to be called, it
2190 * pushes the current state, notes where to return, then jumps back into
2191 * the main loop.
2192 *
2193 * Originally the structure of this function used to look something like
2194
2195 S_regmatch() {
2196 int a = 1, b = 2;
2197 ...
2198 while (scan != NULL) {
5d9a96ca 2199 a++; // do stuff with a and b
95b24440
DM
2200 ...
2201 switch (OP(scan)) {
2202 case FOO: {
2203 int local = 3;
2204 ...
2205 if (regmatch(...)) // recurse
2206 goto yes;
2207 }
2208 ...
2209 }
2210 }
2211 yes:
2212 return 1;
2213 }
2214
2215 * Now it looks something like this:
2216
5d9a96ca 2217 typedef struct {
95b24440
DM
2218 int a, b, local;
2219 int resume_state;
5d9a96ca 2220 } regmatch_state;
95b24440
DM
2221
2222 S_regmatch() {
5d9a96ca
DM
2223 regmatch_state *st = new();
2224 int depth=0;
2225 st->a++; // do stuff with a and b
95b24440
DM
2226 ...
2227 while (scan != NULL) {
2228 ...
2229 switch (OP(scan)) {
2230 case FOO: {
5d9a96ca 2231 st->local = 3;
95b24440 2232 ...
5d9a96ca
DM
2233 st->scan = scan;
2234 scan = ...;
2235 st->resume_state = resume_FOO;
2236 goto start_recurse; // recurse
95b24440 2237
5d9a96ca
DM
2238 resume_point_FOO:
2239 if (result)
95b24440
DM
2240 goto yes;
2241 }
2242 ...
2243 }
5d9a96ca
DM
2244 start_recurse:
2245 st = new(); push a new state
2246 st->a = 1; st->b = 2;
2247 depth++;
95b24440 2248 }
5d9a96ca 2249 yes:
95b24440 2250 result = 1;
5d9a96ca
DM
2251 if (depth--) {
2252 st = pop();
95b24440
DM
2253 switch (resume_state) {
2254 case resume_FOO:
2255 goto resume_point_FOO;
2256 ...
2257 }
2258 }
2259 return result
2260 }
2261
2262 * WARNING: this means that any line in this function that contains a
2263 * REGMATCH() or TRYPAREN() is actually simulating a recursive call to
2264 * regmatch() using gotos instead. Thus the values of any local variables
2265 * not saved in the regmatch_state structure will have been lost when
2266 * execution resumes on the next line .
5d9a96ca
DM
2267 *
2268 * States (ie the st pointer) are allocated in slabs of about 4K in size.
2269 * PL_regmatch_state always points to the currently active state, and
2270 * PL_regmatch_slab points to the slab currently containing PL_regmatch_state.
2271 * The first time regmatch is called, the first slab is allocated, and is
2272 * never freed until interpreter desctruction. When the slab is full,
2273 * a new one is allocated chained to the end. At exit from regmatch, slabs
2274 * allocated since entry are freed.
d6a28714 2275 */
95b24440 2276
40a82448 2277/* *** every FOO_fail should = FOO+1 */
c255a977
DM
2278#define TRIE_next (REGNODE_MAX+1)
2279#define TRIE_next_fail (REGNODE_MAX+2)
2280#define EVAL_A (REGNODE_MAX+3)
2281#define EVAL_A_fail (REGNODE_MAX+4)
2282#define resume_CURLYX (REGNODE_MAX+5)
2283#define resume_WHILEM1 (REGNODE_MAX+6)
2284#define resume_WHILEM2 (REGNODE_MAX+7)
2285#define resume_WHILEM3 (REGNODE_MAX+8)
2286#define resume_WHILEM4 (REGNODE_MAX+9)
2287#define resume_WHILEM5 (REGNODE_MAX+10)
2288#define resume_WHILEM6 (REGNODE_MAX+11)
2289#define BRANCH_next (REGNODE_MAX+12)
2290#define BRANCH_next_fail (REGNODE_MAX+13)
2291#define CURLYM_A (REGNODE_MAX+14)
2292#define CURLYM_A_fail (REGNODE_MAX+15)
2293#define CURLYM_B (REGNODE_MAX+16)
2294#define CURLYM_B_fail (REGNODE_MAX+17)
2295#define IFMATCH_A (REGNODE_MAX+18)
2296#define IFMATCH_A_fail (REGNODE_MAX+19)
2297#define CURLY_B_min_known (REGNODE_MAX+20)
2298#define CURLY_B_min_known_fail (REGNODE_MAX+21)
2299#define CURLY_B_min (REGNODE_MAX+22)
2300#define CURLY_B_min_fail (REGNODE_MAX+23)
2301#define CURLY_B_max (REGNODE_MAX+24)
2302#define CURLY_B_max_fail (REGNODE_MAX+25)
40a82448
DM
2303
2304
3dab1dad 2305#define REG_NODE_NUM(x) ((x) ? (int)((x)-prog) : -1)
95b24440 2306
3df15adc
YO
2307#ifdef DEBUGGING
2308
2309STATIC void
07be1b83
YO
2310S_dump_exec_pos(pTHX_ const char *locinput, const regnode *scan, const bool do_utf8)
2311{
2312 const int docolor = *PL_colors[0];
2313 const int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
2314 int l = (PL_regeol - locinput) > taill ? taill : (PL_regeol - locinput);
2315 /* The part of the string before starttry has one color
2316 (pref0_len chars), between starttry and current
2317 position another one (pref_len - pref0_len chars),
2318 after the current position the third one.
2319 We assume that pref0_len <= pref_len, otherwise we
2320 decrease pref0_len. */
2321 int pref_len = (locinput - PL_bostr) > (5 + taill) - l
2322 ? (5 + taill) - l : locinput - PL_bostr;
2323 int pref0_len;
2324
2325 while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput - pref_len)))
2326 pref_len++;
2327 pref0_len = pref_len - (locinput - PL_reg_starttry);
2328 if (l + pref_len < (5 + taill) && l < PL_regeol - locinput)
2329 l = ( PL_regeol - locinput > (5 + taill) - pref_len
2330 ? (5 + taill) - pref_len : PL_regeol - locinput);
2331 while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput + l)))
2332 l--;
2333 if (pref0_len < 0)
2334 pref0_len = 0;
2335 if (pref0_len > pref_len)
2336 pref0_len = pref_len;
2337 {
3df15adc 2338 const int is_uni = (do_utf8 && OP(scan) != CANY) ? 1 : 0;
0df25f3d
YO
2339
2340 RE_PV_DISPLAY_DECL(s0,len0,is_uni,PERL_DEBUG_PAD(0),
2341 (locinput - pref_len),pref0_len, 60);
2342
2343 RE_PV_DISPLAY_DECL(s1,len1,is_uni,PERL_DEBUG_PAD(1),
3df15adc 2344 (locinput - pref_len + pref0_len),
0df25f3d
YO
2345 pref_len - pref0_len, 60);
2346
2347 RE_PV_DISPLAY_DECL(s2,len2,is_uni,PERL_DEBUG_PAD(2),
2348 locinput, PL_regeol - locinput, 60);
2349
3df15adc 2350 PerlIO_printf(Perl_debug_log,
07be1b83
YO
2351 "%4"IVdf" <%s%.*s%s%s%.*s%s%s%s%.*s%s>%*s|",
2352 (IV)(locinput - PL_bostr),
2353 PL_colors[4],
2354 len0, s0,
2355 PL_colors[5],
2356 PL_colors[2],
2357 len1, s1,
2358 PL_colors[3],
2359 (docolor ? "" : "> <"),
2360 PL_colors[0],
2361 len2, s2,
2362 PL_colors[1],
2363 15 - l - pref_len + 1,
2364 "");
2365 }
2366}
3df15adc 2367
07be1b83
YO
2368#endif
2369
d6a28714 2370STATIC I32 /* 0 failure, 1 success */
3b0527fe 2371S_regmatch(pTHX_ const regmatch_info *reginfo, regnode *prog)
d6a28714 2372{
54df2634
NC
2373#if PERL_VERSION < 9
2374 dMY_CXT;
2375#endif
27da23d5 2376 dVAR;
95b24440 2377 register const bool do_utf8 = PL_reg_match_utf8;
4ad0818d 2378 const U32 uniflags = UTF8_ALLOW_DEFAULT;
95b24440 2379
3b0527fe
DM
2380 regexp *rex = reginfo->prog;
2381
5d9a96ca
DM
2382 regmatch_slab *orig_slab;
2383 regmatch_state *orig_state;
a3621e74 2384
5d9a96ca
DM
2385 /* the current state. This is a cached copy of PL_regmatch_state */
2386 register regmatch_state *st;
95b24440 2387
5d9a96ca
DM
2388 /* cache heavy used fields of st in registers */
2389 register regnode *scan;
2390 register regnode *next;
2391 register I32 n = 0; /* initialize to shut up compiler warning */
2392 register char *locinput = PL_reginput;
95b24440 2393
5d9a96ca
DM
2394 /* these variables are NOT saved during a recusive RFEGMATCH: */
2395 register I32 nextchr; /* is always set to UCHARAT(locinput) */
2396 bool result; /* return value of S_regmatch */
5d9a96ca 2397 int depth = 0; /* depth of recursion */
77cb431f
DM
2398 regmatch_state *yes_state = NULL; /* state to pop to on success of
2399 subpattern */
40a82448 2400 U32 state_num;
95b24440
DM
2401
2402#ifdef DEBUGGING
e68ec53f 2403 GET_RE_DEBUG_FLAGS_DECL;
d6a28714
JH
2404 PL_regindent++;
2405#endif
2406
5d9a96ca
DM
2407 /* on first ever call to regmatch, allocate first slab */
2408 if (!PL_regmatch_slab) {
2409 Newx(PL_regmatch_slab, 1, regmatch_slab);
2410 PL_regmatch_slab->prev = NULL;
2411 PL_regmatch_slab->next = NULL;
86545054 2412 PL_regmatch_state = SLAB_FIRST(PL_regmatch_slab);
5d9a96ca
DM
2413 }
2414
2415 /* remember current high-water mark for exit */
2416 /* XXX this should be done with SAVE* instead */
2417 orig_slab = PL_regmatch_slab;
2418 orig_state = PL_regmatch_state;
2419
2420 /* grab next free state slot */
2421 st = ++PL_regmatch_state;
86545054 2422 if (st > SLAB_LAST(PL_regmatch_slab))
5d9a96ca
DM
2423 st = PL_regmatch_state = S_push_slab(aTHX);
2424
2425 st->minmod = 0;
2426 st->sw = 0;
2427 st->logical = 0;
5d9a96ca 2428 st->cc = NULL;
d6a28714
JH
2429 /* Note that nextchr is a byte even in UTF */
2430 nextchr = UCHARAT(locinput);
2431 scan = prog;
2432 while (scan != NULL) {
8ba1375e 2433
a3621e74 2434 DEBUG_EXECUTE_r( {
6136c704 2435 SV * const prop = sv_newmortal();
07be1b83 2436 dump_exec_pos( locinput, scan, do_utf8 );
32fc9b6a 2437 regprop(rex, prop, scan);
07be1b83
YO
2438
2439 PerlIO_printf(Perl_debug_log,
2440 "%3"IVdf":%*s%s(%"IVdf")\n",
2441 (IV)(scan - rex->program), PL_regindent*2, "",
2442 SvPVX_const(prop),
2443 PL_regkind[OP(scan)] == END ? 0 : (IV)(regnext(scan) - rex->program));
2a782b5b 2444 });
d6a28714
JH
2445
2446 next = scan + NEXT_OFF(scan);
2447 if (next == scan)
2448 next = NULL;
40a82448 2449 state_num = OP(scan);
d6a28714 2450
40a82448
DM
2451 reenter_switch:
2452 switch (state_num) {
d6a28714 2453 case BOL:
7fba1cd6 2454 if (locinput == PL_bostr)
d6a28714 2455 {
3b0527fe 2456 /* reginfo->till = reginfo->bol; */
b8c5462f
JH
2457 break;
2458 }
d6a28714
JH
2459 sayNO;
2460 case MBOL:
12d33761
HS
2461 if (locinput == PL_bostr ||
2462 ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n'))
d6a28714 2463 {
b8c5462f
JH
2464 break;
2465 }
d6a28714
JH
2466 sayNO;
2467 case SBOL:
c2a73568 2468 if (locinput == PL_bostr)
b8c5462f 2469 break;
d6a28714
JH
2470 sayNO;
2471 case GPOS:
3b0527fe 2472 if (locinput == reginfo->ganch)
d6a28714
JH
2473 break;
2474 sayNO;
2475 case EOL:
d6a28714
JH
2476 goto seol;
2477 case MEOL:
d6a28714 2478 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
b8c5462f 2479 sayNO;
b8c5462f 2480 break;
d6a28714
JH
2481 case SEOL:
2482 seol:
2483 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
b8c5462f 2484 sayNO;
d6a28714 2485 if (PL_regeol - locinput > 1)
b8c5462f 2486 sayNO;
b8c5462f 2487 break;
d6a28714
JH
2488 case EOS:
2489 if (PL_regeol != locinput)
b8c5462f 2490 sayNO;
d6a28714 2491 break;
ffc61ed2 2492 case SANY:
d6a28714 2493 if (!nextchr && locinput >= PL_regeol)
4633a7c4 2494 sayNO;
f33976b4
DB
2495 if (do_utf8) {
2496 locinput += PL_utf8skip[nextchr];
2497 if (locinput > PL_regeol)
2498 sayNO;
2499 nextchr = UCHARAT(locinput);
2500 }
2501 else
2502 nextchr = UCHARAT(++locinput);
2503 break;
2504 case CANY:
2505 if (!nextchr && locinput >= PL_regeol)
2506 sayNO;
b8c5462f 2507 nextchr = UCHARAT(++locinput);
a0d0e21e 2508 break;
ffc61ed2 2509 case REG_ANY:
1aa99e6b
IH
2510 if ((!nextchr && locinput >= PL_regeol) || nextchr == '\n')
2511 sayNO;
2512 if (do_utf8) {
b8c5462f 2513 locinput += PL_utf8skip[nextchr];
d6a28714
JH
2514 if (locinput > PL_regeol)
2515 sayNO;
a0ed51b3 2516 nextchr = UCHARAT(locinput);
a0ed51b3 2517 }
1aa99e6b
IH
2518 else
2519 nextchr = UCHARAT(++locinput);
a0ed51b3 2520 break;
166ba7cd
DM
2521
2522#undef ST
2523#define ST st->u.trie
2524
5b47454d 2525 case TRIE:
3dab1dad 2526 {
07be1b83 2527 /* what type of TRIE am I? (utf8 makes this contextual) */
3dab1dad
YO
2528 const enum { trie_plain, trie_utf8, trie_utf8_fold }
2529 trie_type = do_utf8 ?
2530 (scan->flags == EXACT ? trie_utf8 : trie_utf8_fold)
2531 : trie_plain;
2532
2533 /* what trie are we using right now */
be8e71aa 2534 reg_trie_data * const trie
3dab1dad
YO
2535 = (reg_trie_data*)rex->data->data[ ARG( scan ) ];
2536 U32 state = trie->startstate;
166ba7cd
DM
2537
2538 U8 *uc = ( U8* )locinput;
2539 U16 charid = 0;
2540 U32 base = 0;
2541 UV uvc = 0;
2542 STRLEN len = 0;
2543 STRLEN foldlen = 0;
2544 U8 *uscan = (U8*)NULL;
2545 STRLEN bufflen=0;
2546 SV *sv_accept_buff = NULL;
2547 U8 foldbuf[ UTF8_MAXBYTES_CASE + 1 ];
2548
2549 ST.accepted = 0; /* how many accepting states we have seen */
2550 ST.B = next;
2551#ifdef DEBUGGING
2552 ST.me = scan;
2553#endif
3dab1dad
YO
2554
2555 if (trie->bitmap && trie_type != trie_utf8_fold &&
2556 !TRIE_BITMAP_TEST(trie,*locinput)
2557 ) {
2558 if (trie->states[ state ].wordnum) {
2559 DEBUG_EXECUTE_r(
2560 PerlIO_printf(Perl_debug_log,
2561 "%*s %smatched empty string...%s\n",
2562 REPORT_CODE_OFF+PL_regindent*2, "", PL_colors[4], PL_colors[5])
2563 );
2564 break;
2565 } else {
2566 DEBUG_EXECUTE_r(
2567 PerlIO_printf(Perl_debug_log,
2568 "%*s %sfailed to match start class...%s\n",
2569 REPORT_CODE_OFF+PL_regindent*2, "", PL_colors[4], PL_colors[5])
2570 );
2571 sayNO_SILENT;
2572 }
2573 }
166ba7cd 2574
07be1b83
YO
2575 /*
2576 traverse the TRIE keeping track of all accepting states
2577 we transition through until we get to a failing node.
2578 */
2579
a3621e74
YO
2580 while ( state && uc <= (U8*)PL_regeol ) {
2581
5b47454d 2582 if (trie->states[ state ].wordnum) {
166ba7cd 2583 if (!ST.accepted ) {
5b47454d
DM
2584 ENTER;
2585 SAVETMPS;
2586 bufflen = TRIE_INITAL_ACCEPT_BUFFLEN;
2587 sv_accept_buff=newSV(bufflen *
2588 sizeof(reg_trie_accepted) - 1);
2589 SvCUR_set(sv_accept_buff,
2590 sizeof(reg_trie_accepted));
2591 SvPOK_on(sv_accept_buff);
2592 sv_2mortal(sv_accept_buff);
166ba7cd
DM
2593 SAVETMPS;
2594 ST.accept_buff =
5b47454d
DM
2595 (reg_trie_accepted*)SvPV_nolen(sv_accept_buff );
2596 }
2597 else {
166ba7cd 2598 if (ST.accepted >= bufflen) {
5b47454d 2599 bufflen *= 2;
166ba7cd 2600 ST.accept_buff =(reg_trie_accepted*)
5b47454d
DM
2601 SvGROW(sv_accept_buff,
2602 bufflen * sizeof(reg_trie_accepted));
2603 }
2604 SvCUR_set(sv_accept_buff,SvCUR(sv_accept_buff)
2605 + sizeof(reg_trie_accepted));
2606 }
166ba7cd
DM
2607 ST.accept_buff[ST.accepted].wordnum = trie->states[state].wordnum;
2608 ST.accept_buff[ST.accepted].endpos = uc;
2609 ++ST.accepted;
5b47454d 2610 }
a3621e74
YO
2611
2612 base = trie->states[ state ].trans.base;
2613
07be1b83
YO
2614 DEBUG_TRIE_EXECUTE_r({
2615 dump_exec_pos( (char *)uc, scan, do_utf8 );
a3621e74 2616 PerlIO_printf( Perl_debug_log,
e4584336 2617 "%*s %sState: %4"UVxf", Base: %4"UVxf", Accepted: %4"UVxf" ",
07be1b83 2618 2+PL_regindent * 2, "", PL_colors[4],
166ba7cd 2619 (UV)state, (UV)base, (UV)ST.accepted );
07be1b83 2620 });
a3621e74
YO
2621
2622 if ( base ) {
4cadc6a9
YO
2623 REXEC_TRIE_READ_CHAR(trie_type, trie, uc, uscan, len,
2624 uvc, charid, foldlen, foldbuf, uniflags);
a3621e74 2625
5b47454d
DM
2626 if (charid &&
2627 (base + charid > trie->uniquecharcount )
2628 && (base + charid - 1 - trie->uniquecharcount
2629 < trie->lasttrans)
2630 && trie->trans[base + charid - 1 -
2631 trie->uniquecharcount].check == state)
2632 {
2633 state = trie->trans[base + charid - 1 -
2634 trie->uniquecharcount ].next;
2635 }
2636 else {
2637 state = 0;
2638 }
2639 uc += len;
2640
2641 }
2642 else {
a3621e74
YO
2643 state = 0;
2644 }
2645 DEBUG_TRIE_EXECUTE_r(
e4584336
RB
2646 PerlIO_printf( Perl_debug_log,
2647 "Charid:%3x CV:%4"UVxf" After State: %4"UVxf"%s\n",
2648 charid, uvc, (UV)state, PL_colors[5] );
a3621e74
YO
2649 );
2650 }
166ba7cd 2651 if (!ST.accepted )
a3621e74 2652 sayNO;
a3621e74 2653
166ba7cd
DM
2654 DEBUG_EXECUTE_r(
2655 PerlIO_printf( Perl_debug_log,
2656 "%*s %sgot %"IVdf" possible matches%s\n",
2657 REPORT_CODE_OFF + PL_regindent * 2, "",
2658 PL_colors[4], (IV)ST.accepted, PL_colors[5] );
2659 );
2660 }
2661
2662 /* FALL THROUGH */
2663
2664 case TRIE_next_fail: /* we failed - try next alterative */
2665
2666 if ( ST.accepted == 1 ) {
2667 /* only one choice left - just continue */
2668 DEBUG_EXECUTE_r({
2669 reg_trie_data * const trie
2670 = (reg_trie_data*)rex->data->data[ ARG(ST.me) ];
2671 SV ** const tmp = RX_DEBUG(reginfo->prog)
2672 ? av_fetch( trie->words, ST.accept_buff[ 0 ].wordnum-1, 0 )
2673 : NULL;
2674 PerlIO_printf( Perl_debug_log,
2675 "%*s %sonly one match left: #%d <%s>%s\n",
2676 REPORT_CODE_OFF+PL_regindent*2, "", PL_colors[4],
2677 ST.accept_buff[ 0 ].wordnum,
2678 tmp ? SvPV_nolen_const( *tmp ) : "not compiled under -Dr",
2679 PL_colors[5] );
2680 });
2681 PL_reginput = (char *)ST.accept_buff[ 0 ].endpos;
2682 /* in this case we free tmps/leave before we call regmatch
2683 as we wont be using accept_buff again. */
2684 FREETMPS;
2685 LEAVE;
2686 locinput = PL_reginput;
2687 nextchr = UCHARAT(locinput);
2688 scan = ST.B;
2689 continue; /* execute rest of RE */
2690 }
2691
2692 if (!ST.accepted-- ) {
2693 FREETMPS;
2694 LEAVE;
2695 sayNO;
2696 }
2697
a3621e74 2698 /*
166ba7cd
DM
2699 There are at least two accepting states left. Presumably
2700 the number of accepting states is going to be low,
2701 typically two. So we simply scan through to find the one
2702 with lowest wordnum. Once we find it, we swap the last
2703 state into its place and decrement the size. We then try to
2704 match the rest of the pattern at the point where the word
2705 ends. If we succeed, control just continues along the
2706 regex; if we fail we return here to try the next accepting
2707 state
2708 */
a3621e74 2709
166ba7cd
DM
2710 {
2711 U32 best = 0;
2712 U32 cur;
2713 for( cur = 1 ; cur <= ST.accepted ; cur++ ) {
2714 DEBUG_TRIE_EXECUTE_r(
f2278c82 2715 PerlIO_printf( Perl_debug_log,
166ba7cd
DM
2716 "%*s %sgot %"IVdf" (%d) as best, looking at %"IVdf" (%d)%s\n",
2717 REPORT_CODE_OFF + PL_regindent * 2, "", PL_colors[4],
2718 (IV)best, ST.accept_buff[ best ].wordnum, (IV)cur,
2719 ST.accept_buff[ cur ].wordnum, PL_colors[5] );
2720 );
2721
2722 if (ST.accept_buff[cur].wordnum <
2723 ST.accept_buff[best].wordnum)
2724 best = cur;
a3621e74 2725 }
166ba7cd
DM
2726
2727 DEBUG_EXECUTE_r({
2728 reg_trie_data * const trie
2729 = (reg_trie_data*)rex->data->data[ ARG(ST.me) ];
2730 SV ** const tmp = RX_DEBUG(reginfo->prog)
2731 ? av_fetch( trie->words, ST.accept_buff[ best ].wordnum - 1, 0 )
2732 : NULL;
2733 PerlIO_printf( Perl_debug_log, "%*s %strying alternation #%d <%s> at node #%d %s\n",
2734 REPORT_CODE_OFF+PL_regindent*2, "", PL_colors[4],
2735 ST.accept_buff[best].wordnum,
2736 tmp ? SvPV_nolen_const( *tmp ) : "not compiled under -Dr", REG_NODE_NUM(scan),
2737 PL_colors[5] );
2738 });
2739
2740 if ( best<ST.accepted ) {
2741 reg_trie_accepted tmp = ST.accept_buff[ best ];
2742 ST.accept_buff[ best ] = ST.accept_buff[ ST.accepted ];
2743 ST.accept_buff[ ST.accepted ] = tmp;
2744 best = ST.accepted;
a3621e74 2745 }
166ba7cd
DM
2746 PL_reginput = (char *)ST.accept_buff[ best ].endpos;
2747 }
2748 PUSH_STATE_GOTO(TRIE_next, ST.B);
2749 /* NOTREACHED */
2750
2751#undef ST
2752
95b24440
DM
2753 case EXACT: {
2754 char *s = STRING(scan);
5d9a96ca 2755 st->ln = STR_LEN(scan);
eb160463 2756 if (do_utf8 != UTF) {
bc517b45 2757 /* The target and the pattern have differing utf8ness. */
1aa99e6b 2758 char *l = locinput;
be8e71aa 2759 const char * const e = s + st->ln;
a72c7584 2760
5ff6fc6d
JH
2761 if (do_utf8) {
2762 /* The target is utf8, the pattern is not utf8. */
1aa99e6b 2763 while (s < e) {
a3b680e6 2764 STRLEN ulen;
1aa99e6b 2765 if (l >= PL_regeol)
5ff6fc6d
JH
2766 sayNO;
2767 if (NATIVE_TO_UNI(*(U8*)s) !=
89ebb4a3 2768 utf8n_to_uvuni((U8*)l, UTF8_MAXBYTES, &ulen,
041457d9 2769 uniflags))
5ff6fc6d 2770 sayNO;
bc517b45 2771 l += ulen;
5ff6fc6d 2772 s ++;
1aa99e6b 2773 }
5ff6fc6d
JH
2774 }
2775 else {
2776 /* The target is not utf8, the pattern is utf8. */
1aa99e6b 2777 while (s < e) {
a3b680e6 2778 STRLEN ulen;
1aa99e6b
IH
2779 if (l >= PL_regeol)
2780 sayNO;
5ff6fc6d 2781 if (NATIVE_TO_UNI(*((U8*)l)) !=
89ebb4a3 2782 utf8n_to_uvuni((U8*)s, UTF8_MAXBYTES, &ulen,
041457d9 2783 uniflags))
1aa99e6b 2784 sayNO;
bc517b45 2785 s += ulen;
a72c7584 2786 l ++;
1aa99e6b 2787 }
5ff6fc6d 2788 }
1aa99e6b
IH
2789 locinput = l;
2790 nextchr = UCHARAT(locinput);
2791 break;
2792 }
bc517b45 2793 /* The target and the pattern have the same utf8ness. */
d6a28714
JH
2794 /* Inline the first character, for speed. */
2795 if (UCHARAT(s) != nextchr)
2796 sayNO;
5d9a96ca 2797 if (PL_regeol - locinput < st->ln)
d6a28714 2798 sayNO;
5d9a96ca 2799 if (st->ln > 1 && memNE(s, locinput, st->ln))
d6a28714 2800 sayNO;
5d9a96ca 2801 locinput += st->ln;
d6a28714
JH
2802 nextchr = UCHARAT(locinput);
2803 break;
95b24440 2804 }
d6a28714 2805 case EXACTFL:
b8c5462f
JH
2806 PL_reg_flags |= RF_tainted;
2807 /* FALL THROUGH */
95b24440 2808 case EXACTF: {
be8e71aa 2809 char * const s = STRING(scan);
5d9a96ca 2810 st->ln = STR_LEN(scan);
d6a28714 2811
d07ddd77
JH
2812 if (do_utf8 || UTF) {
2813 /* Either target or the pattern are utf8. */
be8e71aa 2814 const char * const l = locinput;
d07ddd77 2815 char *e = PL_regeol;
bc517b45 2816
5d9a96ca 2817 if (ibcmp_utf8(s, 0, st->ln, (bool)UTF,
1feea2c7 2818 l, &e, 0, do_utf8)) {
5486206c
JH
2819 /* One more case for the sharp s:
2820 * pack("U0U*", 0xDF) =~ /ss/i,
2821 * the 0xC3 0x9F are the UTF-8
2822 * byte sequence for the U+00DF. */
2823 if (!(do_utf8 &&
2824 toLOWER(s[0]) == 's' &&
5d9a96ca 2825 st->ln >= 2 &&
5486206c
JH
2826 toLOWER(s[1]) == 's' &&
2827 (U8)l[0] == 0xC3 &&
2828 e - l >= 2 &&
2829 (U8)l[1] == 0x9F))
2830 sayNO;
2831 }
d07ddd77
JH
2832 locinput = e;
2833 nextchr = UCHARAT(locinput);
2834 break;
a0ed51b3 2835 }
d6a28714 2836
bc517b45
JH
2837 /* Neither the target and the pattern are utf8. */
2838
d6a28714
JH
2839 /* Inline the first character, for speed. */
2840 if (UCHARAT(s) != nextchr &&
2841 UCHARAT(s) != ((OP(scan) == EXACTF)
2842 ? PL_fold : PL_fold_locale)[nextchr])
a0ed51b3 2843 sayNO;
5d9a96ca 2844 if (PL_regeol - locinput < st->ln)
b8c5462f 2845 sayNO;
5d9a96ca
DM
2846 if (st->ln > 1 && (OP(scan) == EXACTF
2847 ? ibcmp(s, locinput, st->ln)
2848 : ibcmp_locale(s, locinput, st->ln)))
4633a7c4 2849 sayNO;
5d9a96ca 2850 locinput += st->ln;
d6a28714 2851 nextchr = UCHARAT(locinput);
a0d0e21e 2852 break;
95b24440 2853 }
d6a28714 2854 case ANYOF:
ffc61ed2 2855 if (do_utf8) {
9e55ce06
JH
2856 STRLEN inclasslen = PL_regeol - locinput;
2857
32fc9b6a 2858 if (!reginclass(rex, scan, (U8*)locinput, &inclasslen, do_utf8))
e0f9d4a8 2859 sayNO_ANYOF;
ffc61ed2
JH
2860 if (locinput >= PL_regeol)
2861 sayNO;
0f0076b4 2862 locinput += inclasslen ? inclasslen : UTF8SKIP(locinput);
b8c5462f 2863 nextchr = UCHARAT(locinput);
e0f9d4a8 2864 break;
ffc61ed2
JH
2865 }
2866 else {
2867 if (nextchr < 0)
2868 nextchr = UCHARAT(locinput);
32fc9b6a 2869 if (!REGINCLASS(rex, scan, (U8*)locinput))
e0f9d4a8 2870 sayNO_ANYOF;
ffc61ed2
JH
2871 if (!nextchr && locinput >= PL_regeol)
2872 sayNO;
2873 nextchr = UCHARAT(++locinput);
e0f9d4a8
JH
2874 break;
2875 }
2876 no_anyof:
2877 /* If we might have the case of the German sharp s
2878 * in a casefolding Unicode character class. */
2879
ebc501f0
JH
2880 if (ANYOF_FOLD_SHARP_S(scan, locinput, PL_regeol)) {
2881 locinput += SHARP_S_SKIP;
e0f9d4a8 2882 nextchr = UCHARAT(locinput);
ffc61ed2 2883 }
e0f9d4a8
JH
2884 else
2885 sayNO;
b8c5462f 2886 break;
d6a28714 2887 case ALNUML:
b8c5462f
JH
2888 PL_reg_flags |= RF_tainted;
2889 /* FALL THROUGH */
d6a28714 2890 case ALNUM:
b8c5462f 2891 if (!nextchr)
4633a7c4 2892 sayNO;
ffc61ed2 2893 if (do_utf8) {
1a4fad37 2894 LOAD_UTF8_CHARCLASS_ALNUM();
ffc61ed2 2895 if (!(OP(scan) == ALNUM
bb7a0f54 2896 ? (bool)swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8)
d6a28714 2897 : isALNUM_LC_utf8((U8*)locinput)))
b8c5462f
JH
2898 {
2899 sayNO;
a0ed51b3 2900 }
b8c5462f 2901 locinput += PL_utf8skip[nextchr];
a0ed51b3
LW
2902 nextchr = UCHARAT(locinput);
2903 break;
2904 }
ffc61ed2 2905 if (!(OP(scan) == ALNUM
d6a28714 2906 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
bbce6d69 2907 sayNO;
b8c5462f 2908 nextchr = UCHARAT(++locinput);
a0d0e21e 2909 break;
d6a28714 2910 case NALNUML:
b8c5462f
JH
2911 PL_reg_flags |= RF_tainted;
2912 /* FALL THROUGH */
d6a28714
JH
2913 case NALNUM:
2914 if (!nextchr && locinput >= PL_regeol)
a0ed51b3 2915 sayNO;
ffc61ed2 2916 if (do_utf8) {
1a4fad37 2917 LOAD_UTF8_CHARCLASS_ALNUM();
ffc61ed2 2918 if (OP(scan) == NALNUM
bb7a0f54 2919 ? (bool)swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8)
d6a28714
JH
2920 : isALNUM_LC_utf8((U8*)locinput))
2921 {
b8c5462f 2922 sayNO;
d6a28714 2923 }
b8c5462f
JH
2924 locinput += PL_utf8skip[nextchr];
2925 nextchr = UCHARAT(locinput);
2926 break;
2927 }
ffc61ed2 2928 if (OP(scan) == NALNUM
d6a28714 2929 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
b8c5462f 2930 sayNO;
76e3520e 2931 nextchr = UCHARAT(++locinput);
a0d0e21e 2932 break;
d6a28714
JH
2933 case BOUNDL:
2934 case NBOUNDL:
3280af22 2935 PL_reg_flags |= RF_tainted;
bbce6d69 2936 /* FALL THROUGH */
d6a28714
JH
2937 case BOUND:
2938 case NBOUND:
2939 /* was last char in word? */
ffc61ed2 2940 if (do_utf8) {
12d33761 2941 if (locinput == PL_bostr)
5d9a96ca 2942 st->ln = '\n';
ffc61ed2 2943 else {
a3b680e6 2944 const U8 * const r = reghop3((U8*)locinput, -1, (U8*)PL_bostr);
9041c2e3 2945
4ad0818d 2946 st->ln = utf8n_to_uvchr(r, UTF8SKIP(r), 0, uniflags);
ffc61ed2
JH
2947 }
2948 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
5d9a96ca 2949 st->ln = isALNUM_uni(st->ln);
1a4fad37 2950 LOAD_UTF8_CHARCLASS_ALNUM();
3568d838 2951 n = swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8);
ffc61ed2
JH
2952 }
2953 else {
5d9a96ca 2954 st->ln = isALNUM_LC_uvchr(UNI_TO_NATIVE(st->ln));
ffc61ed2
JH
2955 n = isALNUM_LC_utf8((U8*)locinput);
2956 }
a0ed51b3 2957 }
d6a28714 2958 else {
5d9a96ca 2959 st->ln = (locinput != PL_bostr) ?
12d33761 2960 UCHARAT(locinput - 1) : '\n';
ffc61ed2 2961 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
5d9a96ca 2962 st->ln = isALNUM(st->ln);
ffc61ed2
JH
2963 n = isALNUM(nextchr);
2964 }
2965 else {
5d9a96ca 2966 st->ln = isALNUM_LC(st->ln);
ffc61ed2
JH
2967 n = isALNUM_LC(nextchr);
2968 }
d6a28714 2969 }
5d9a96ca 2970 if (((!st->ln) == (!n)) == (OP(scan) == BOUND ||
ffc61ed2
JH
2971 OP(scan) == BOUNDL))
2972 sayNO;
a0ed51b3 2973 break;
d6a28714 2974 case SPACEL:
3280af22 2975 PL_reg_flags |= RF_tainted;
bbce6d69 2976 /* FALL THROUGH */
d6a28714 2977 case SPACE:
9442cb0e 2978 if (!nextchr)
4633a7c4 2979 sayNO;
1aa99e6b 2980 if (do_utf8) {
fd400ab9 2981 if (UTF8_IS_CONTINUED(nextchr)) {
1a4fad37 2982 LOAD_UTF8_CHARCLASS_SPACE();
ffc61ed2 2983 if (!(OP(scan) == SPACE
bb7a0f54 2984 ? (bool)swash_fetch(PL_utf8_space, (U8*)locinput, do_utf8)
ffc61ed2
JH
2985 : isSPACE_LC_utf8((U8*)locinput)))
2986 {
2987 sayNO;
2988 }
2989 locinput += PL_utf8skip[nextchr];
2990 nextchr = UCHARAT(locinput);
2991 break;
d6a28714 2992 }
ffc61ed2
JH
2993 if (!(OP(scan) == SPACE
2994 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2995 sayNO;
2996 nextchr = UCHARAT(++locinput);
2997 }
2998 else {
2999 if (!(OP(scan) == SPACE
3000 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
3001 sayNO;
3002 nextchr = UCHARAT(++locinput);
a0ed51b3 3003 }
a0ed51b3 3004 break;
d6a28714 3005 case NSPACEL:
3280af22 3006 PL_reg_flags |= RF_tainted;
bbce6d69 3007 /* FALL THROUGH */
d6a28714 3008 case NSPACE:
9442cb0e 3009 if (!nextchr && locinput >= PL_regeol)
b8c5462f 3010 sayNO;
1aa99e6b 3011 if (do_utf8) {
1a4fad37 3012 LOAD_UTF8_CHARCLASS_SPACE();
ffc61ed2 3013 if (OP(scan) == NSPACE
bb7a0f54 3014 ? (bool)swash_fetch(PL_utf8_space, (U8*)locinput, do_utf8)
d6a28714 3015 : isSPACE_LC_utf8((U8*)locinput))
b8c5462f
JH
3016 {
3017 sayNO;
3018 }
3019 locinput += PL_utf8skip[nextchr];
3020 nextchr = UCHARAT(locinput);
3021 break;
a0ed51b3 3022 }
ffc61ed2 3023 if (OP(scan) == NSPACE
d6a28714 3024 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
4633a7c4 3025 sayNO;
76e3520e 3026 nextchr = UCHARAT(++locinput);
a0d0e21e 3027 break;
d6a28714 3028 case DIGITL:
a0ed51b3
LW
3029 PL_reg_flags |= RF_tainted;
3030 /* FALL THROUGH */
d6a28714 3031 case DIGIT:
9442cb0e 3032 if (!nextchr)
a0ed51b3 3033 sayNO;
1aa99e6b 3034 if (do_utf8) {
1a4fad37 3035 LOAD_UTF8_CHARCLASS_DIGIT();
ffc61ed2 3036 if (!(OP(scan) == DIGIT
bb7a0f54 3037 ? (bool)swash_fetch(PL_utf8_digit, (U8*)locinput, do_utf8)
9442cb0e 3038 : isDIGIT_LC_utf8((U8*)locinput)))
dfe13c55 3039 {
a0ed51b3 3040 sayNO;
dfe13c55 3041 }
6f06b55f 3042 locinput += PL_utf8skip[nextchr];
a0ed51b3
LW
3043 nextchr = UCHARAT(locinput);
3044 break;
3045 }
ffc61ed2 3046 if (!(OP(scan) == DIGIT
9442cb0e 3047 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr)))
a0ed51b3
LW
3048 sayNO;
3049 nextchr = UCHARAT(++locinput);
3050 break;
d6a28714 3051 case NDIGITL:
b8c5462f
JH
3052 PL_reg_flags |= RF_tainted;
3053 /* FALL THROUGH */
d6a28714 3054 case NDIGIT:
9442cb0e 3055 if (!nextchr && locinput >= PL_regeol)
b8c5462f 3056 sayNO;
1aa99e6b 3057 if (do_utf8) {
1a4fad37 3058 LOAD_UTF8_CHARCLASS_DIGIT();
ffc61ed2 3059 if (OP(scan) == NDIGIT
bb7a0f54 3060 ? (bool)swash_fetch(PL_utf8_digit, (U8*)locinput, do_utf8)
9442cb0e
GS
3061 : isDIGIT_LC_utf8((U8*)locinput))
3062 {
a0ed51b3 3063 sayNO;
9442cb0e 3064 }
6f06b55f 3065 locinput += PL_utf8skip[nextchr];
a0ed51b3
LW
3066 nextchr = UCHARAT(locinput);
3067 break;
3068 }
ffc61ed2 3069 if (OP(scan) == NDIGIT
9442cb0e 3070 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr))
a0ed51b3
LW
3071 sayNO;
3072 nextchr = UCHARAT(++locinput);
3073 break;
3074 case CLUMP:
b7c83a7e 3075 if (locinput >= PL_regeol)
a0ed51b3 3076 sayNO;
b7c83a7e 3077 if (do_utf8) {
1a4fad37 3078 LOAD_UTF8_CHARCLASS_MARK();
b7c83a7e
JH
3079 if (swash_fetch(PL_utf8_mark,(U8*)locinput, do_utf8))
3080 sayNO;
3081 locinput += PL_utf8skip[nextchr];
3082 while (locinput < PL_regeol &&
3083 swash_fetch(PL_utf8_mark,(U8*)locinput, do_utf8))
3084 locinput += UTF8SKIP(locinput);
3085 if (locinput > PL_regeol)
3086 sayNO;
eb08e2da
JH
3087 }
3088 else
3089 locinput++;
a0ed51b3
LW
3090 nextchr = UCHARAT(locinput);
3091 break;
c8756f30 3092 case REFFL:
3280af22 3093 PL_reg_flags |= RF_tainted;
c8756f30 3094 /* FALL THROUGH */
c277df42 3095 case REF:
95b24440
DM
3096 case REFF: {
3097 char *s;
c277df42 3098 n = ARG(scan); /* which paren pair */
5d9a96ca 3099 st->ln = PL_regstartp[n];
2c2d71f5 3100 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
5d9a96ca 3101 if ((I32)*PL_reglastparen < n || st->ln == -1)
af3f8c16 3102 sayNO; /* Do not match unless seen CLOSEn. */
5d9a96ca 3103 if (st->ln == PL_regendp[n])
a0d0e21e 3104 break;
a0ed51b3 3105
5d9a96ca 3106 s = PL_bostr + st->ln;
1aa99e6b 3107 if (do_utf8 && OP(scan) != REF) { /* REF can do byte comparison */
a0ed51b3 3108 char *l = locinput;
a3b680e6 3109 const char *e = PL_bostr + PL_regendp[n];
a0ed51b3
LW
3110 /*
3111 * Note that we can't do the "other character" lookup trick as
3112 * in the 8-bit case (no pun intended) because in Unicode we
3113 * have to map both upper and title case to lower case.
3114 */
3115 if (OP(scan) == REFF) {
3116 while (s < e) {
a3b680e6
AL
3117 STRLEN ulen1, ulen2;
3118 U8 tmpbuf1[UTF8_MAXBYTES_CASE+1];
3119 U8 tmpbuf2[UTF8_MAXBYTES_CASE+1];
3120
a0ed51b3
LW
3121 if (l >= PL_regeol)
3122 sayNO;
a2a2844f
JH
3123 toLOWER_utf8((U8*)s, tmpbuf1, &ulen1);
3124 toLOWER_utf8((U8*)l, tmpbuf2, &ulen2);
7114a2d2 3125 if (ulen1 != ulen2 || memNE((char *)tmpbuf1, (char *)tmpbuf2, ulen1))
a0ed51b3 3126 sayNO;
a2a2844f
JH
3127 s += ulen1;
3128 l += ulen2;
a0ed51b3
LW
3129 }
3130 }
3131 locinput = l;
3132 nextchr = UCHARAT(locinput);
3133 break;
3134 }
3135
a0d0e21e 3136 /* Inline the first character, for speed. */
76e3520e 3137 if (UCHARAT(s) != nextchr &&
c8756f30
AK
3138 (OP(scan) == REF ||
3139 (UCHARAT(s) != ((OP(scan) == REFF
22c35a8c 3140 ? PL_fold : PL_fold_locale)[nextchr]))))
4633a7c4 3141 sayNO;
5d9a96ca
DM
3142 st->ln = PL_regendp[n] - st->ln;
3143 if (locinput + st->ln > PL_regeol)
4633a7c4 3144 sayNO;
5d9a96ca
DM
3145 if (st->ln > 1 && (OP(scan) == REF
3146 ? memNE(s, locinput, st->ln)
c8756f30 3147 : (OP(scan) == REFF
5d9a96ca
DM
3148 ? ibcmp(s, locinput, st->ln)
3149 : ibcmp_locale(s, locinput, st->ln))))
4633a7c4 3150 sayNO;
5d9a96ca 3151 locinput += st->ln;
76e3520e 3152 nextchr = UCHARAT(locinput);
a0d0e21e 3153 break;
95b24440 3154 }
a0d0e21e
LW
3155
3156 case NOTHING:
c277df42 3157 case TAIL:
a0d0e21e
LW
3158 break;
3159 case BACK:
3160 break;
40a82448
DM
3161
3162#undef ST
3163#define ST st->u.eval
3164
3165 case EVAL: /* /(?{A})B/ /(??{A})B/ and /(?(?{A})X|Y)B/ */
c277df42 3166 {
c277df42 3167 SV *ret;
8e5e9ebe 3168 {
4aabdb9b
DM
3169 /* execute the code in the {...} */
3170 dSP;
6136c704 3171 SV ** const before = SP;
4aabdb9b
DM
3172 OP_4tree * const oop = PL_op;
3173 COP * const ocurcop = PL_curcop;
3174 PAD *old_comppad;
4aabdb9b
DM
3175
3176 n = ARG(scan);
32fc9b6a 3177 PL_op = (OP_4tree*)rex->data->data[n];
4aabdb9b 3178 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%"UVxf"\n", PTR2UV(PL_op)) );
32fc9b6a 3179 PAD_SAVE_LOCAL(old_comppad, (PAD*)rex->data->data[n + 2]);
4aabdb9b
DM
3180 PL_regendp[0] = PL_reg_magic->mg_len = locinput - PL_bostr;
3181
8e5e9ebe
RGS
3182 CALLRUNOPS(aTHX); /* Scalar context. */
3183 SPAGAIN;
3184 if (SP == before)
075aa684 3185 ret = &PL_sv_undef; /* protect against empty (?{}) blocks. */
8e5e9ebe
RGS
3186 else {
3187 ret = POPs;
3188 PUTBACK;
3189 }
4aabdb9b
DM
3190
3191 PL_op = oop;
3192 PAD_RESTORE_LOCAL(old_comppad);
3193 PL_curcop = ocurcop;
3194 if (!st->logical) {
3195 /* /(?{...})/ */
3196 sv_setsv(save_scalar(PL_replgv), ret);
4aabdb9b
DM
3197 break;
3198 }
8e5e9ebe 3199 }
4aabdb9b
DM
3200 if (st->logical == 2) { /* Postponed subexpression: /(??{...})/ */
3201 regexp *re;
4aabdb9b 3202 {
4f639d21
DM
3203 /* extract RE object from returned value; compiling if
3204 * necessary */
3205
6136c704 3206 MAGIC *mg = NULL;
be8e71aa 3207 const SV *sv;
faf82a0b
AE
3208 if(SvROK(ret) && SvSMAGICAL(sv = SvRV(ret)))
3209 mg = mg_find(sv, PERL_MAGIC_qr);
3210 else if (SvSMAGICAL(ret)) {
3211 if (SvGMAGICAL(ret))
3212 sv_unmagic(ret, PERL_MAGIC_qr);
3213 else
3214 mg = mg_find(ret, PERL_MAGIC_qr);
0f5d15d6 3215 }
faf82a0b 3216
0f5d15d6
IZ
3217 if (mg) {
3218 re = (regexp *)mg->mg_obj;
df0003d4 3219 (void)ReREFCNT_inc(re);
0f5d15d6
IZ
3220 }
3221 else {
3222 STRLEN len;
6136c704 3223 const char * const t = SvPV_const(ret, len);
0f5d15d6 3224 PMOP pm;
a3b680e6 3225 const I32 osize = PL_regsize;
0f5d15d6 3226
5fcd1c1b 3227 Zero(&pm, 1, PMOP);
4aabdb9b 3228 if (DO_UTF8(ret)) pm.op_pmdynflags |= PMdf_DYN_UTF8;
83003860 3229 re = CALLREGCOMP(aTHX_ (char*)t, (char*)t + len, &pm);
9041c2e3 3230 if (!(SvFLAGS(ret)
faf82a0b
AE
3231 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY
3232 | SVs_GMG)))
14befaf4
DM
3233 sv_magic(ret,(SV*)ReREFCNT_inc(re),
3234 PERL_MAGIC_qr,0,0);
0f5d15d6 3235 PL_regsize = osize;
0f5d15d6 3236 }
4aabdb9b 3237 }
aa283a38
DM
3238
3239 /* run the pattern returned from (??{...}) */
3240
4aabdb9b
DM
3241 DEBUG_EXECUTE_r(
3242 PerlIO_printf(Perl_debug_log,
3243 "Entering embedded \"%s%.60s%s%s\"\n",
3244 PL_colors[0],
3245 re->precomp,
3246 PL_colors[1],
3247 (strlen(re->precomp) > 60 ? "..." : ""))
3248 );
2c2d71f5 3249
40a82448
DM
3250 ST.cp = regcppush(0); /* Save *all* the positions. */
3251 REGCP_SET(ST.lastcp);
4aabdb9b
DM
3252 *PL_reglastparen = 0;
3253 *PL_reglastcloseparen = 0;
4aabdb9b 3254 PL_reginput = locinput;
4aabdb9b
DM
3255
3256 /* XXXX This is too dramatic a measure... */
3257 PL_reg_maxiter = 0;
3258
5d9a96ca 3259 st->logical = 0;
40a82448 3260 ST.toggleutf = ((PL_reg_flags & RF_utf8) != 0) ^
aa283a38 3261 ((re->reganch & ROPT_UTF8) != 0);
40a82448
DM
3262 if (ST.toggleutf) PL_reg_flags ^= RF_utf8;
3263 ST.prev_rex = rex;
aa283a38 3264 rex = re;
aa283a38 3265
40a82448 3266 ST.B = next;
aa283a38 3267 /* now continue from first node in postoned RE */
40a82448 3268 PUSH_YES_STATE_GOTO(EVAL_A, re->program + 1);
4aabdb9b 3269 /* NOTREACHED */
a0ed51b3 3270 }
4aabdb9b
DM
3271 /* /(?(?{...})X|Y)/ */
3272 st->sw = SvTRUE(ret);
3273 st->logical = 0;
c277df42
IZ
3274 break;
3275 }
40a82448
DM
3276
3277 case EVAL_A: /* successfully ran inner rex (??{rex}) */
3278 if (ST.toggleutf)
3279 PL_reg_flags ^= RF_utf8;
3280 ReREFCNT_dec(rex);
3281 rex = ST.prev_rex;
3282 /* XXXX This is too dramatic a measure... */
3283 PL_reg_maxiter = 0;
3284 /* Restore parens of the caller without popping the
3285 * savestack */
3286 {
3287 const I32 tmp = PL_savestack_ix;
3288 PL_savestack_ix = ST.lastcp;
3289 regcppop(rex);
3290 PL_savestack_ix = tmp;
3291 }
3292 PL_reginput = locinput;
3293 /* continue at the node following the (??{...}) */
3294 scan = ST.B;
3295 continue;
3296
3297 case EVAL_A_fail: /* unsuccessfully ran inner rex (??{rex}) */
3298 /* Restore state to the outer re then re-throw the failure */
3299 if (ST.toggleutf)
3300 PL_reg_flags ^= RF_utf8;
3301 ReREFCNT_dec(rex);
3302 rex = ST.prev_rex;
3303
3304 /* XXXX This is too dramatic a measure... */
3305 PL_reg_maxiter = 0;
3306
3307 PL_reginput = locinput;
3308 REGCP_UNWIND(ST.lastcp);
3309 regcppop(rex);
3310 sayNO_SILENT;
3311
3312#undef ST
3313
a0d0e21e 3314 case OPEN:
c277df42 3315 n = ARG(scan); /* which paren pair */
3280af22
NIS
3316 PL_reg_start_tmp[n] = locinput;
3317 if (n > PL_regsize)
3318 PL_regsize = n;
a0d0e21e
LW
3319 break;
3320 case CLOSE:
c277df42 3321 n = ARG(scan); /* which paren pair */
cf93c79d
IZ
3322 PL_regstartp[n] = PL_reg_start_tmp[n] - PL_bostr;
3323 PL_regendp[n] = locinput - PL_bostr;
eb160463 3324 if (n > (I32)*PL_reglastparen)
3280af22 3325 *PL_reglastparen = n;
a01268b5 3326 *PL_reglastcloseparen = n;
a0d0e21e 3327 break;
c277df42
IZ
3328 case GROUPP:
3329 n = ARG(scan); /* which paren pair */
5d9a96ca 3330 st->sw = ((I32)*PL_reglastparen >= n && PL_regendp[n] != -1);
c277df42
IZ
3331 break;
3332 case IFTHEN:
2c2d71f5 3333 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
5d9a96ca 3334 if (st->sw)
c277df42
IZ
3335 next = NEXTOPER(NEXTOPER(scan));
3336 else {
3337 next = scan + ARG(scan);
3338 if (OP(next) == IFTHEN) /* Fake one. */
3339 next = NEXTOPER(NEXTOPER(next));
3340 }
3341 break;
3342 case LOGICAL:
5d9a96ca 3343 st->logical = scan->flags;
c277df42 3344 break;
2ab05381 3345/*******************************************************************
a0374537
DM
3346 cc points to the regmatch_state associated with the most recent CURLYX.
3347 This struct contains info about the innermost (...)* loop (an
3348 "infoblock"), and a pointer to the next outer cc.
2ab05381
IZ
3349
3350 Here is how Y(A)*Z is processed (if it is compiled into CURLYX/WHILEM):
3351
95b24440 3352 1) After matching Y, regnode for CURLYX is processed;
2ab05381 3353
a0374537 3354 2) This regnode populates cc, and calls regmatch() recursively
95b24440 3355 with the starting point at WHILEM node;
2ab05381
IZ
3356
3357 3) Each hit of WHILEM node tries to match A and Z (in the order
3358 depending on the current iteration, min/max of {min,max} and
3359 greediness). The information about where are nodes for "A"
a0374537 3360 and "Z" is read from cc, as is info on how many times "A"
2ab05381
IZ
3361 was already matched, and greediness.
3362
3363 4) After A matches, the same WHILEM node is hit again.
3364
95b24440 3365 5) Each time WHILEM is hit, cc is the infoblock created by CURLYX
2ab05381 3366 of the same pair. Thus when WHILEM tries to match Z, it temporarily
95b24440 3367 resets cc, since this Y(A)*Z can be a part of some other loop:
2ab05381
IZ
3368 as in (Y(A)*Z)*. If Z matches, the automaton will hit the WHILEM node
3369 of the external loop.
3370
a0374537 3371 Currently present infoblocks form a tree with a stem formed by st->cc
2ab05381
IZ
3372 and whatever it mentions via ->next, and additional attached trees
3373 corresponding to temporarily unset infoblocks as in "5" above.
3374
95b24440 3375 In the following picture, infoblocks for outer loop of
2ab05381
IZ
3376 (Y(A)*?Z)*?T are denoted O, for inner I. NULL starting block
3377 is denoted by x. The matched string is YAAZYAZT. Temporarily postponed
3378 infoblocks are drawn below the "reset" infoblock.
3379
3380 In fact in the picture below we do not show failed matches for Z and T
3381 by WHILEM blocks. [We illustrate minimal matches, since for them it is
3382 more obvious *why* one needs to *temporary* unset infoblocks.]
3383
3384 Matched REx position InfoBlocks Comment
3385 (Y(A)*?Z)*?T x
3386 Y(A)*?Z)*?T x <- O
3387 Y (A)*?Z)*?T x <- O
3388 Y A)*?Z)*?T x <- O <- I
3389 YA )*?Z)*?T x <- O <- I
3390 YA A)*?Z)*?T x <- O <- I
3391 YAA )*?Z)*?T x <- O <- I
3392 YAA Z)*?T x <- O # Temporary unset I
3393 I
3394
3395 YAAZ Y(A)*?Z)*?T x <- O
3396 I
3397
3398 YAAZY (A)*?Z)*?T x <- O
3399 I
3400
3401 YAAZY A)*?Z)*?T x <- O <- I
3402 I
3403
3404 YAAZYA )*?Z)*?T x <- O <- I
3405 I
3406
3407 YAAZYA Z)*?T x <- O # Temporary unset I
3408 I,I
3409
3410 YAAZYAZ )*?T x <- O
3411 I,I
3412
3413 YAAZYAZ T x # Temporary unset O
3414 O
3415 I,I
3416
3417 YAAZYAZT x
3418 O
3419 I,I
3420 *******************************************************************/
95b24440 3421
a0d0e21e 3422 case CURLYX: {
cb434fcc
IZ
3423 /* No need to save/restore up to this paren */
3424 I32 parenfloor = scan->flags;
c277df42 3425
c2b7afd3
NC
3426 /* Dave says:
3427
3428 CURLYX and WHILEM are always paired: they're the moral
3429 equivalent of pp_enteriter anbd pp_iter.
3430
3431 The only time next could be null is if the node tree is
3432 corrupt. This was mentioned on p5p a few days ago.
3433
3434 See http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2006-04/msg00556.html
3435 So we'll assert that this is true:
3436 */
3437 assert(next);
30b2893d 3438 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
c277df42 3439 next += ARG(next);
cb434fcc
IZ
3440 /* XXXX Probably it is better to teach regpush to support
3441 parenfloor > PL_regsize... */
eb160463 3442 if (parenfloor > (I32)*PL_reglastparen)
cb434fcc 3443 parenfloor = *PL_reglastparen; /* Pessimization... */
a0374537 3444
d8319b27
DM
3445 st->u.curlyx.cp = PL_savestack_ix;
3446 st->u.curlyx.outercc = st->cc;
a0374537
DM
3447 st->cc = st;
3448 /* these fields contain the state of the current curly.
3449 * they are accessed by subsequent WHILEMs;
3450 * cur and lastloc are also updated by WHILEM */
d8319b27
DM
3451 st->u.curlyx.parenfloor = parenfloor;
3452 st->u.curlyx.cur = -1; /* this will be updated by WHILEM */
3453 st->u.curlyx.min = ARG1(scan);
3454 st->u.curlyx.max = ARG2(scan);
3455 st->u.curlyx.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
3456 st->u.curlyx.lastloc = 0;
a0374537
DM
3457 /* st->next and st->minmod are also read by WHILEM */
3458
3280af22 3459 PL_reginput = locinput;
95b24440
DM
3460 REGMATCH(PREVOPER(next), CURLYX); /* start on the WHILEM */
3461 /*** all unsaved local vars undefined at this point */
d8319b27
DM
3462 regcpblow(st->u.curlyx.cp);
3463 st->cc = st->u.curlyx.outercc;
95b24440 3464 saySAME(result);
a0d0e21e 3465 }
5f66b61c 3466 /* NOTREACHED */
a0d0e21e
LW
3467 case WHILEM: {
3468 /*
3469 * This is really hard to understand, because after we match
3470 * what we're trying to match, we must make sure the rest of
2c2d71f5 3471 * the REx is going to match for sure, and to do that we have
a0d0e21e
LW
3472 * to go back UP the parse tree by recursing ever deeper. And
3473 * if it fails, we have to reset our parent's current state
3474 * that we can try again after backing off.
3475 */
3476
c2b7afd3
NC
3477 /* Dave says:
3478
3479 st->cc gets initialised by CURLYX ready for use by WHILEM.
3480 So again, unless somethings been corrupted, st->cc cannot
3481 be null at that point in WHILEM.
3482
3483 See http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2006-04/msg00556.html
3484 So we'll assert that this is true:
3485 */
3486 assert(st->cc);
d8319b27
DM
3487 st->u.whilem.lastloc = st->cc->u.curlyx.lastloc; /* Detection of 0-len. */
3488 st->u.whilem.cache_offset = 0;
3489 st->u.whilem.cache_bit = 0;
c277df42 3490
d8319b27 3491 n = st->cc->u.curlyx.cur + 1; /* how many we know we matched */
3280af22 3492 PL_reginput = locinput;
a0d0e21e 3493
a3621e74 3494 DEBUG_EXECUTE_r(
9041c2e3 3495 PerlIO_printf(Perl_debug_log,
91f3b821 3496 "%*s %ld out of %ld..%ld cc=%"UVxf"\n",
3280af22 3497 REPORT_CODE_OFF+PL_regindent*2, "",
d8319b27
DM
3498 (long)n, (long)st->cc->u.curlyx.min,
3499 (long)st->cc->u.curlyx.max, PTR2UV(st->cc))
c277df42 3500 );
4633a7c4 3501
a0d0e21e
LW
3502 /* If degenerate scan matches "", assume scan done. */
3503
d8319b27
DM
3504 if (locinput == st->cc->u.curlyx.lastloc && n >= st->cc->u.curlyx.min) {
3505 st->u.whilem.savecc = st->cc;
3506 st->cc = st->cc->u.curlyx.outercc;
5d9a96ca 3507 if (st->cc)
d8319b27 3508 st->ln = st->cc->u.curlyx.cur;
a3621e74 3509 DEBUG_EXECUTE_r(
c3464db5
DD
3510 PerlIO_printf(Perl_debug_log,
3511 "%*s empty match detected, try continuation...\n",
3280af22 3512 REPORT_CODE_OFF+PL_regindent*2, "")
c277df42 3513 );
d8319b27 3514 REGMATCH(st->u.whilem.savecc->next, WHILEM1);
95b24440 3515 /*** all unsaved local vars undefined at this point */
d8319b27 3516 st->cc = st->u.whilem.savecc;
95b24440 3517 if (result)
4633a7c4 3518 sayYES;
d8319b27
DM
3519 if (st->cc->u.curlyx.outercc)
3520 st->cc->u.curlyx.outercc->u.curlyx.cur = st->ln;
4633a7c4 3521 sayNO;
a0d0e21e
LW
3522 }
3523
3524 /* First just match a string of min scans. */
3525
d8319b27
DM
3526 if (n < st->cc->u.curlyx.min) {
3527 st->cc->u.curlyx.cur = n;
3528 st->cc->u.curlyx.lastloc = locinput;
3529 REGMATCH(st->cc->u.curlyx.scan, WHILEM2);
95b24440
DM
3530 /*** all unsaved local vars undefined at this point */
3531 if (result)
4633a7c4 3532 sayYES;
d8319b27
DM
3533 st->cc->u.curlyx.cur = n - 1;
3534 st->cc->u.curlyx.lastloc = st->u.whilem.lastloc;
4633a7c4 3535 sayNO;
a0d0e21e
LW
3536 }
3537
2c2d71f5
JH
3538 if (scan->flags) {
3539 /* Check whether we already were at this position.
3540 Postpone detection until we know the match is not
3541 *that* much linear. */
3542 if (!PL_reg_maxiter) {
3543 PL_reg_maxiter = (PL_regeol - PL_bostr + 1) * (scan->flags>>4);
66bf836d
DM
3544 /* possible overflow for long strings and many CURLYX's */
3545 if (PL_reg_maxiter < 0)
3546 PL_reg_maxiter = I32_MAX;
2c2d71f5
JH
3547 PL_reg_leftiter = PL_reg_maxiter;
3548 }
3549 if (PL_reg_leftiter-- == 0) {
3298f257 3550 const I32 size = (PL_reg_maxiter + 7)/8;
2c2d71f5 3551 if (PL_reg_poscache) {
eb160463 3552 if ((I32)PL_reg_poscache_size < size) {
2c2d71f5
JH
3553 Renew(PL_reg_poscache, size, char);
3554 PL_reg_poscache_size = size;
3555 }
3556 Zero(PL_reg_poscache, size, char);
3557 }
3558 else {
3559 PL_reg_poscache_size = size;
a02a5408 3560 Newxz(PL_reg_poscache, size, char);
2c2d71f5 3561 }
a3621e74 3562 DEBUG_EXECUTE_r(
2c2d71f5
JH
3563 PerlIO_printf(Perl_debug_log,
3564 "%sDetected a super-linear match, switching on caching%s...\n",
3565 PL_colors[4], PL_colors[5])
3566 );
3567 }
3568 if (PL_reg_leftiter < 0) {
d8319b27 3569 st->u.whilem.cache_offset = locinput - PL_bostr;
2c2d71f5 3570
3298f257 3571 st->u.whilem.cache_offset = (scan->flags & 0xf) - 1
d8319b27
DM
3572 + st->u.whilem.cache_offset * (scan->flags>>4);
3573 st->u.whilem.cache_bit = st->u.whilem.cache_offset % 8;
3574 st->u.whilem.cache_offset /= 8;
3575 if (PL_reg_poscache[st->u.whilem.cache_offset] & (1<<st->u.whilem.cache_bit)) {
a3621e74 3576 DEBUG_EXECUTE_r(
2c2d71f5
JH
3577 PerlIO_printf(Perl_debug_log,
3578 "%*s already tried at this position...\n",
3579 REPORT_CODE_OFF+PL_regindent*2, "")
3580 );
3298f257 3581 sayNO; /* cache records failure */
2c2d71f5 3582 }
2c2d71f5
JH
3583 }
3584 }
3585
a0d0e21e
LW
3586 /* Prefer next over scan for minimal matching. */
3587
5d9a96ca 3588 if (st->cc->minmod) {
d8319b27
DM
3589 st->u.whilem.savecc = st->cc;
3590 st->cc = st->cc->u.curlyx.outercc;
5d9a96ca 3591 if (st->cc)
d8319b27
DM
3592 st->ln = st->cc->u.curlyx.cur;
3593 st->u.whilem.cp = regcppush(st->u.whilem.savecc->u.curlyx.parenfloor);
3594 REGCP_SET(st->u.whilem.lastcp);
3595 REGMATCH(st->u.whilem.savecc->next, WHILEM3);
95b24440 3596 /*** all unsaved local vars undefined at this point */
d8319b27 3597 st->cc = st->u.whilem.savecc;
95b24440 3598 if (result) {
d8319b27 3599 regcpblow(st->u.whilem.cp);
3298f257 3600 sayYES; /* All done. */
5f05dabc 3601 }
d8319b27 3602 REGCP_UNWIND(st->u.whilem.lastcp);
4f639d21 3603 regcppop(rex);
d8319b27
DM
3604 if (st->cc->u.curlyx.outercc)
3605 st->cc->u.curlyx.outercc->u.curlyx.cur = st->ln;
a0d0e21e 3606
d8319b27 3607 if (n >= st->cc->u.curlyx.max) { /* Maximum greed exceeded? */
9041c2e3 3608 if (ckWARN(WARN_REGEXP) && n >= REG_INFTY
3280af22
NIS
3609 && !(PL_reg_flags & RF_warned)) {
3610 PL_reg_flags |= RF_warned;
9014280d 3611 Perl_warner(aTHX_ packWARN(WARN_REGEXP), "%s limit (%d) exceeded",
2f3ca594
GS
3612 "Complex regular subexpression recursion",
3613 REG_INFTY - 1);
c277df42 3614 }
3ab3c9b4 3615 CACHEsayNO;
c277df42 3616 }
a687059c 3617
a3621e74 3618 DEBUG_EXECUTE_r(
c3464db5
DD
3619 PerlIO_printf(Perl_debug_log,
3620 "%*s trying longer...\n",
3280af22 3621 REPORT_CODE_OFF+PL_regindent*2, "")
c277df42 3622 );
a0d0e21e 3623 /* Try scanning more and see if it helps. */
3280af22 3624 PL_reginput = locinput;
d8319b27
DM
3625 st->cc->u.curlyx.cur = n;
3626 st->cc->u.curlyx.lastloc = locinput;
3627 st->u.whilem.cp = regcppush(st->cc->u.curlyx.parenfloor);
3628 REGCP_SET(st->u.whilem.lastcp);
3629 REGMATCH(st->cc->u.curlyx.scan, WHILEM4);
95b24440
DM
3630 /*** all unsaved local vars undefined at this point */
3631 if (result) {
d8319b27 3632 regcpblow(st->u.whilem.cp);
3298f257 3633 sayYES;
5f05dabc 3634 }
d8319b27 3635 REGCP_UNWIND(st->u.whilem.lastcp);
4f639d21 3636 regcppop(rex);
d8319b27
DM
3637 st->cc->u.curlyx.cur = n - 1;
3638 st->cc->u.curlyx.lastloc = st->u.whilem.lastloc;
3ab3c9b4 3639 CACHEsayNO;
a0d0e21e
LW
3640 }
3641
3642 /* Prefer scan over next for maximal matching. */
3643
d8319b27
DM
3644 if (n < st->cc->u.curlyx.max) { /* More greed allowed? */
3645 st->u.whilem.cp = regcppush(st->cc->u.curlyx.parenfloor);
3646 st->cc->u.curlyx.cur = n;
3647 st->cc->u.curlyx.lastloc = locinput;
3648 REGCP_SET(st->u.whilem.lastcp);
3649 REGMATCH(st->cc->u.curlyx.scan, WHILEM5);
95b24440
DM
3650 /*** all unsaved local vars undefined at this point */
3651 if (result) {
d8319b27 3652 regcpblow(st->u.whilem.cp);
3298f257 3653 sayYES;
5f05dabc 3654 }
d8319b27 3655 REGCP_UNWIND(st->u.whilem.lastcp);
4f639d21 3656 regcppop(rex); /* Restore some previous $<digit>s? */
3280af22 3657 PL_reginput = locinput;
a3621e74 3658 DEBUG_EXECUTE_r(
c3464db5
DD
3659 PerlIO_printf(Perl_debug_log,
3660 "%*s failed, try continuation...\n",
3280af22 3661 REPORT_CODE_OFF+PL_regindent*2, "")
c277df42
IZ
3662 );
3663 }
9041c2e3 3664 if (ckWARN(WARN_REGEXP) && n >= REG_INFTY
599cee73 3665 && !(PL_reg_flags & RF_warned)) {
3280af22 3666 PL_reg_flags |= RF_warned;
9014280d 3667 Perl_warner(aTHX_ packWARN(WARN_REGEXP), "%s limit (%d) exceeded",
cb5d145d
GS
3668 "Complex regular subexpression recursion",
3669 REG_INFTY - 1);
a0d0e21e
LW
3670 }
3671
3672 /* Failed deeper matches of scan, so see if this one works. */
d8319b27
DM
3673 st->u.whilem.savecc = st->cc;
3674 st->cc = st->cc->u.curlyx.outercc;
5d9a96ca 3675 if (st->cc)
d8319b27
DM
3676 st->ln = st->cc->u.curlyx.cur;
3677 REGMATCH(st->u.whilem.savecc->next, WHILEM6);
95b24440 3678 /*** all unsaved local vars undefined at this point */
d8319b27 3679 st->cc = st->u.whilem.savecc;
95b24440 3680 if (result)
3298f257 3681 sayYES;
d8319b27
DM
3682 if (st->cc->u.curlyx.outercc)
3683 st->cc->u.curlyx.outercc->u.curlyx.cur = st->ln;
3684 st->cc->u.curlyx.cur = n - 1;
3685 st->cc->u.curlyx.lastloc = st->u.whilem.lastloc;
3ab3c9b4 3686 CACHEsayNO;
a0d0e21e 3687 }
5f66b61c 3688 /* NOTREACHED */
40a82448
DM
3689
3690#undef ST
3691#define ST st->u.branch
3692
3693 case BRANCHJ: /* /(...|A|...)/ with long next pointer */
c277df42
IZ
3694 next = scan + ARG(scan);
3695 if (next == scan)
3696 next = NULL;
40a82448
DM
3697 scan = NEXTOPER(scan);
3698 /* FALL THROUGH */
c277df42 3699
40a82448
DM
3700 case BRANCH: /* /(...|A|...)/ */
3701 scan = NEXTOPER(scan); /* scan now points to inner node */
3702 if (!next || (OP(next) != BRANCH && OP(next) != BRANCHJ))
3703 /* last branch; skip state push and jump direct to node */
3704 continue;
3705 ST.lastparen = *PL_reglastparen;
3706 ST.next_branch = next;
3707 REGCP_SET(ST.cp);
3708 PL_reginput = locinput;
02db2b7b 3709
40a82448
DM
3710 /* Now go into the branch */
3711 PUSH_STATE_GOTO(BRANCH_next, scan);
3712 /* NOTREACHED */
3713
3714 case BRANCH_next_fail: /* that branch failed; try the next, if any */
3715 REGCP_UNWIND(ST.cp);
3716 for (n = *PL_reglastparen; n > ST.lastparen; n--)
3717 PL_regendp[n] = -1;
3718 *PL_reglastparen = n;
3719 scan = ST.next_branch;
3720 /* no more branches? */
3721 if (!scan || (OP(scan) != BRANCH && OP(scan) != BRANCHJ))
3722 sayNO;
3723 continue; /* execute next BRANCH[J] op */
3724 /* NOTREACHED */
3725
a0d0e21e 3726 case MINMOD:
5d9a96ca 3727 st->minmod = 1;
a0d0e21e 3728 break;
40a82448
DM
3729
3730#undef ST
3731#define ST st->u.curlym
3732
3733 case CURLYM: /* /A{m,n}B/ where A is fixed-length */
3734
3735 /* This is an optimisation of CURLYX that enables us to push
3736 * only a single backtracking state, no matter now many matches
3737 * there are in {m,n}. It relies on the pattern being constant
3738 * length, with no parens to influence future backrefs
3739 */
3740
3741 ST.me = scan;
dc45a647 3742 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
40a82448
DM
3743
3744 /* if paren positive, emulate an OPEN/CLOSE around A */
3745 if (ST.me->flags) {
3746 I32 paren = ST.me->flags;
3747 if (paren > PL_regsize)
3748 PL_regsize = paren;
3749 if (paren > (I32)*PL_reglastparen)
3750 *PL_reglastparen = paren;
c277df42 3751 scan += NEXT_OFF(scan); /* Skip former OPEN. */
6407bf3b 3752 }
40a82448
DM
3753 ST.A = scan;
3754 ST.B = next;
3755 ST.alen = 0;
3756 ST.count = 0;
3757 ST.minmod = st->minmod;
3758 st->minmod = 0;
3759 ST.c1 = CHRTEST_UNINIT;
3760 REGCP_SET(ST.cp);
6407bf3b 3761
40a82448
DM
3762 if (!(ST.minmod ? ARG1(ST.me) : ARG2(ST.me))) /* min/max */
3763 goto curlym_do_B;
3764
3765 curlym_do_A: /* execute the A in /A{m,n}B/ */
6407bf3b 3766 PL_reginput = locinput;
40a82448
DM
3767 PUSH_YES_STATE_GOTO(CURLYM_A, ST.A); /* match A */
3768 /* NOTREACHED */
5f80c4cf 3769
40a82448
DM
3770 case CURLYM_A: /* we've just matched an A */
3771 locinput = st->locinput;
3772 nextchr = UCHARAT(locinput);
3773
3774 ST.count++;
3775 /* after first match, determine A's length: u.curlym.alen */
3776 if (ST.count == 1) {
3777 if (PL_reg_match_utf8) {
3778 char *s = locinput;
3779 while (s < PL_reginput) {
3780 ST.alen++;
3781 s += UTF8SKIP(s);
3782 }
3783 }
3784 else {
3785 ST.alen = PL_reginput - locinput;
3786 }
3787 if (ST.alen == 0)
3788 ST.count = ST.minmod ? ARG1(ST.me) : ARG2(ST.me);
3789 }
0cadcf80
DM
3790 DEBUG_EXECUTE_r(
3791 PerlIO_printf(Perl_debug_log,
40a82448 3792 "%*s CURLYM now matched %"IVdf" times, len=%"IVdf"...\n",
0cadcf80 3793 (int)(REPORT_CODE_OFF+PL_regindent*2), "",
40a82448 3794 (IV) ST.count, (IV)ST.alen)
0cadcf80
DM
3795 );
3796
40a82448
DM
3797 locinput = PL_reginput;
3798 if (ST.count < (ST.minmod ? ARG1(ST.me) : ARG2(ST.me)))
3799 goto curlym_do_A; /* try to match another A */
3800 goto curlym_do_B; /* try to match B */
5f80c4cf 3801
40a82448
DM
3802 case CURLYM_A_fail: /* just failed to match an A */
3803 REGCP_UNWIND(ST.cp);
3804 if (ST.minmod || ST.count < ARG1(ST.me) /* min*/ )
3805 sayNO;
0cadcf80 3806
40a82448
DM
3807 curlym_do_B: /* execute the B in /A{m,n}B/ */
3808 PL_reginput = locinput;
3809 if (ST.c1 == CHRTEST_UNINIT) {
3810 /* calculate c1 and c2 for possible match of 1st char
3811 * following curly */
3812 ST.c1 = ST.c2 = CHRTEST_VOID;
3813 if (HAS_TEXT(ST.B) || JUMPABLE(ST.B)) {
3814 regnode *text_node = ST.B;
3815 if (! HAS_TEXT(text_node))
3816 FIND_NEXT_IMPT(text_node);
3817 if (HAS_TEXT(text_node)
3818 && PL_regkind[OP(text_node)] != REF)
3819 {
3820 ST.c1 = (U8)*STRING(text_node);
3821 ST.c2 =
3822 (OP(text_node) == EXACTF || OP(text_node) == REFF)
3823 ? PL_fold[ST.c1]
3824 : (OP(text_node) == EXACTFL || OP(text_node) == REFFL)
3825 ? PL_fold_locale[ST.c1]
3826 : ST.c1;
c277df42 3827 }
c277df42 3828 }
40a82448
DM
3829 }
3830
3831 DEBUG_EXECUTE_r(
3832 PerlIO_printf(Perl_debug_log,
3833 "%*s CURLYM trying tail with matches=%"IVdf"...\n",
3834 (int)(REPORT_CODE_OFF+PL_regindent*2),
3835 "", (IV)ST.count)
3836 );
3837 if (ST.c1 != CHRTEST_VOID
3838 && UCHARAT(PL_reginput) != ST.c1
3839 && UCHARAT(PL_reginput) != ST.c2)
3840 {
3841 /* simulate B failing */
3842 state_num = CURLYM_B_fail;
3843 goto reenter_switch;
3844 }
3845
3846 if (ST.me->flags) {
3847 /* mark current A as captured */
3848 I32 paren = ST.me->flags;
3849 if (ST.count) {
3850 PL_regstartp[paren]
3851 = HOPc(PL_reginput, -ST.alen) - PL_bostr;
3852 PL_regendp[paren] = PL_reginput - PL_bostr;
c277df42 3853 }
40a82448
DM
3854 else
3855 PL_regendp[paren] = -1;
c277df42 3856 }
40a82448 3857 PUSH_STATE_GOTO(CURLYM_B, ST.B); /* match B */
5f66b61c 3858 /* NOTREACHED */
40a82448
DM
3859
3860 case CURLYM_B_fail: /* just failed to match a B */
3861 REGCP_UNWIND(ST.cp);
3862 if (ST.minmod) {
3863 if (ST.count == ARG2(ST.me) /* max */)
3864 sayNO;
3865 goto curlym_do_A; /* try to match a further A */
3866 }
3867 /* backtrack one A */
3868 if (ST.count == ARG1(ST.me) /* min */)
3869 sayNO;
3870 ST.count--;
3871 locinput = HOPc(locinput, -ST.alen);
3872 goto curlym_do_B; /* try to match B */
3873
c255a977
DM
3874#undef ST
3875#define ST st->u.curly
40a82448 3876
c255a977
DM
3877#define CURLY_SETPAREN(paren, success) \
3878 if (paren) { \
3879 if (success) { \
3880 PL_regstartp[paren] = HOPc(locinput, -1) - PL_bostr; \
3881 PL_regendp[paren] = locinput - PL_bostr; \
3882 } \
3883 else \
3884 PL_regendp[paren] = -1; \
3885 }
3886
3887 case STAR: /* /A*B/ where A is width 1 */
3888 ST.paren = 0;
3889 ST.min = 0;
3890 ST.max = REG_INFTY;
a0d0e21e
LW
3891 scan = NEXTOPER(scan);
3892 goto repeat;
c255a977
DM
3893 case PLUS: /* /A+B/ where A is width 1 */
3894 ST.paren = 0;
3895 ST.min = 1;
3896 ST.max = REG_INFTY;
c277df42 3897 scan = NEXTOPER(scan);
c255a977
DM
3898 goto repeat;
3899 case CURLYN: /* /(A){m,n}B/ where A is width 1 */
3900 ST.paren = scan->flags; /* Which paren to set */
3901 if (ST.paren > PL_regsize)
3902 PL_regsize = ST.paren;
3903 if (ST.paren > (I32)*PL_reglastparen)
3904 *PL_reglastparen = ST.paren;
3905 ST.min = ARG1(scan); /* min to match */
3906 ST.max = ARG2(scan); /* max to match */
3907 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
3908 goto repeat;
3909 case CURLY: /* /A{m,n}B/ where A is width 1 */
3910 ST.paren = 0;
3911 ST.min = ARG1(scan); /* min to match */
3912 ST.max = ARG2(scan); /* max to match */
3913 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
c277df42 3914 repeat:
a0d0e21e
LW
3915 /*
3916 * Lookahead to avoid useless match attempts
3917 * when we know what character comes next.
c255a977 3918 *
5f80c4cf
JP
3919 * Used to only do .*x and .*?x, but now it allows
3920 * for )'s, ('s and (?{ ... })'s to be in the way
3921 * of the quantifier and the EXACT-like node. -- japhy
3922 */
3923
c255a977
DM
3924 if (ST.min > ST.max) /* XXX make this a compile-time check? */
3925 sayNO;
cca55fe3 3926 if (HAS_TEXT(next) || JUMPABLE(next)) {
5f80c4cf
JP
3927 U8 *s;
3928 regnode *text_node = next;
3929
3dab1dad
YO
3930 if (! HAS_TEXT(text_node))
3931 FIND_NEXT_IMPT(text_node);
5f80c4cf 3932
9e137952 3933 if (! HAS_TEXT(text_node))
c255a977 3934 ST.c1 = ST.c2 = CHRTEST_VOID;
5f80c4cf 3935 else {
3dab1dad 3936 if (PL_regkind[OP(text_node)] == REF) {
c255a977 3937 ST.c1 = ST.c2 = CHRTEST_VOID;
44a68960 3938 goto assume_ok_easy;
cca55fe3 3939 }
be8e71aa
YO
3940 else
3941 s = (U8*)STRING(text_node);
5f80c4cf
JP
3942
3943 if (!UTF) {
c255a977 3944 ST.c2 = ST.c1 = *s;
f65d3ee7 3945 if (OP(text_node) == EXACTF || OP(text_node) == REFF)
c255a977 3946 ST.c2 = PL_fold[ST.c1];
f65d3ee7 3947 else if (OP(text_node) == EXACTFL || OP(text_node) == REFFL)
c255a977 3948 ST.c2 = PL_fold_locale[ST.c1];
1aa99e6b 3949 }
5f80c4cf 3950 else { /* UTF */
f65d3ee7 3951 if (OP(text_node) == EXACTF || OP(text_node) == REFF) {
a2a2844f 3952 STRLEN ulen1, ulen2;
89ebb4a3
JH
3953 U8 tmpbuf1[UTF8_MAXBYTES_CASE+1];
3954 U8 tmpbuf2[UTF8_MAXBYTES_CASE+1];
a2a2844f
JH
3955
3956 to_utf8_lower((U8*)s, tmpbuf1, &ulen1);
3957 to_utf8_upper((U8*)s, tmpbuf2, &ulen2);
3958
c255a977 3959 ST.c1 = utf8n_to_uvuni(tmpbuf1, UTF8_MAXBYTES, 0,
041457d9 3960 uniflags);
c255a977 3961 ST.c2 = utf8n_to_uvuni(tmpbuf2, UTF8_MAXBYTES, 0,
041457d9 3962 uniflags);
5f80c4cf
JP
3963 }
3964 else {
c255a977 3965 ST.c2 = ST.c1 = utf8n_to_uvchr(s, UTF8_MAXBYTES, 0,
041457d9 3966 uniflags);
5f80c4cf 3967 }
1aa99e6b
IH
3968 }
3969 }
bbce6d69 3970 }
a0d0e21e 3971 else
c255a977 3972 ST.c1 = ST.c2 = CHRTEST_VOID;
cca55fe3 3973 assume_ok_easy:
c255a977
DM
3974
3975 ST.A = scan;
3976 ST.B = next;
3280af22 3977 PL_reginput = locinput;
5d9a96ca
DM
3978 if (st->minmod) {
3979 st->minmod = 0;
c255a977 3980 if (ST.min && regrepeat(rex, ST.A, ST.min) < ST.min)
4633a7c4 3981 sayNO;
c255a977 3982 ST.count = ST.min;
a0ed51b3 3983 locinput = PL_reginput;
c255a977
DM
3984 REGCP_SET(ST.cp);
3985 if (ST.c1 == CHRTEST_VOID)
3986 goto curly_try_B_min;
3987
3988 ST.oldloc = locinput;
3989
3990 /* set ST.maxpos to the furthest point along the
3991 * string that could possibly match */
3992 if (ST.max == REG_INFTY) {
3993 ST.maxpos = PL_regeol - 1;
3994 if (do_utf8)
3995 while (UTF8_IS_CONTINUATION(*(U8*)ST.maxpos))
3996 ST.maxpos--;
3997 }
3998 else if (do_utf8) {
3999 int m = ST.max - ST.min;
4000 for (ST.maxpos = locinput;
4001 m >0 && ST.maxpos + UTF8SKIP(ST.maxpos) <= PL_regeol; m--)
4002 ST.maxpos += UTF8SKIP(ST.maxpos);
4003 }
4004 else {
4005 ST.maxpos = locinput + ST.max - ST.min;
4006 if (ST.maxpos >= PL_regeol)
4007 ST.maxpos = PL_regeol - 1;
4008 }
4009 goto curly_try_B_min_known;
4010
4011 }
4012 else {
4013 ST.count = regrepeat(rex, ST.A, ST.max);
4014 locinput = PL_reginput;
4015 if (ST.count < ST.min)
4016 sayNO;
4017 if ((ST.count > ST.min)
4018 && (PL_regkind[OP(ST.B)] == EOL) && (OP(ST.B) != MEOL))
4019 {
4020 /* A{m,n} must come at the end of the string, there's
4021 * no point in backing off ... */
4022 ST.min = ST.count;
4023 /* ...except that $ and \Z can match before *and* after
4024 newline at the end. Consider "\n\n" =~ /\n+\Z\n/.
4025 We may back off by one in this case. */
4026 if (UCHARAT(PL_reginput - 1) == '\n' && OP(ST.B) != EOS)
4027 ST.min--;
4028 }
4029 REGCP_SET(ST.cp);
4030 goto curly_try_B_max;
4031 }
4032 /* NOTREACHED */
4033
4034
4035 case CURLY_B_min_known_fail:
4036 /* failed to find B in a non-greedy match where c1,c2 valid */
4037 if (ST.paren && ST.count)
4038 PL_regendp[ST.paren] = -1;
4039
4040 PL_reginput = locinput; /* Could be reset... */
4041 REGCP_UNWIND(ST.cp);
4042 /* Couldn't or didn't -- move forward. */
4043 ST.oldloc = locinput;
4044 if (do_utf8)
4045 locinput += UTF8SKIP(locinput);
4046 else
4047 locinput++;
4048 ST.count++;
4049 curly_try_B_min_known:
4050 /* find the next place where 'B' could work, then call B */
4051 {
4052 int n;
4053 if (do_utf8) {
4054 n = (ST.oldloc == locinput) ? 0 : 1;
4055 if (ST.c1 == ST.c2) {
4056 STRLEN len;
4057 /* set n to utf8_distance(oldloc, locinput) */
4058 while (locinput <= ST.maxpos &&
4059 utf8n_to_uvchr((U8*)locinput,
4060 UTF8_MAXBYTES, &len,
4061 uniflags) != (UV)ST.c1) {
4062 locinput += len;
4063 n++;
4064 }
1aa99e6b
IH
4065 }
4066 else {
c255a977
DM
4067 /* set n to utf8_distance(oldloc, locinput) */
4068 while (locinput <= ST.maxpos) {
4069 STRLEN len;
4070 const UV c = utf8n_to_uvchr((U8*)locinput,
4071 UTF8_MAXBYTES, &len,
4072 uniflags);
4073 if (c == (UV)ST.c1 || c == (UV)ST.c2)
4074 break;
4075 locinput += len;
4076 n++;
1aa99e6b 4077 }
0fe9bf95
IZ
4078 }
4079 }
c255a977
DM
4080 else {
4081 if (ST.c1 == ST.c2) {
4082 while (locinput <= ST.maxpos &&
4083 UCHARAT(locinput) != ST.c1)
4084 locinput++;
bbce6d69 4085 }
c255a977
DM
4086 else {
4087 while (locinput <= ST.maxpos
4088 && UCHARAT(locinput) != ST.c1
4089 && UCHARAT(locinput) != ST.c2)
4090 locinput++;
a0ed51b3 4091 }
c255a977
DM
4092 n = locinput - ST.oldloc;
4093 }
4094 if (locinput > ST.maxpos)
4095 sayNO;
4096 /* PL_reginput == oldloc now */
4097 if (n) {
4098 ST.count += n;
4099 if (regrepeat(rex, ST.A, n) < n)
4633a7c4 4100 sayNO;
a0d0e21e 4101 }
c255a977
DM
4102 PL_reginput = locinput;
4103 CURLY_SETPAREN(ST.paren, ST.count);
4104 PUSH_STATE_GOTO(CURLY_B_min_known, ST.B);
a0d0e21e 4105 }
c255a977
DM
4106 /* NOTREACHED */
4107
4108
4109 case CURLY_B_min_fail:
4110 /* failed to find B in a non-greedy match where c1,c2 invalid */
4111 if (ST.paren && ST.count)
4112 PL_regendp[ST.paren] = -1;
4113
4114 REGCP_UNWIND(ST.cp);
4115 /* failed -- move forward one */
4116 PL_reginput = locinput;
4117 if (regrepeat(rex, ST.A, 1)) {
4118 ST.count++;
a0ed51b3 4119 locinput = PL_reginput;
c255a977
DM
4120 if (ST.count <= ST.max || (ST.max == REG_INFTY &&
4121 ST.count > 0)) /* count overflow ? */
15272685 4122 {
c255a977
DM
4123 curly_try_B_min:
4124 CURLY_SETPAREN(ST.paren, ST.count);
4125 PUSH_STATE_GOTO(CURLY_B_min, ST.B);
a0d0e21e
LW
4126 }
4127 }
4633a7c4 4128 sayNO;
c255a977
DM
4129 /* NOTREACHED */
4130
4131
4132 curly_try_B_max:
4133 /* a successful greedy match: now try to match B */
4134 {
4135 UV c = 0;
4136 if (ST.c1 != CHRTEST_VOID)
4137 c = do_utf8 ? utf8n_to_uvchr((U8*)PL_reginput,
4138 UTF8_MAXBYTES, 0, uniflags)
466787eb 4139 : (UV) UCHARAT(PL_reginput);
c255a977
DM
4140 /* If it could work, try it. */
4141 if (ST.c1 == CHRTEST_VOID || c == (UV)ST.c1 || c == (UV)ST.c2) {
4142 CURLY_SETPAREN(ST.paren, ST.count);
4143 PUSH_STATE_GOTO(CURLY_B_max, ST.B);
4144 /* NOTREACHED */
4145 }
4146 }
4147 /* FALL THROUGH */
4148 case CURLY_B_max_fail:
4149 /* failed to find B in a greedy match */
4150 if (ST.paren && ST.count)
4151 PL_regendp[ST.paren] = -1;
4152
4153 REGCP_UNWIND(ST.cp);
4154 /* back up. */
4155 if (--ST.count < ST.min)
4156 sayNO;
4157 PL_reginput = locinput = HOPc(locinput, -1);
4158 goto curly_try_B_max;
4159
4160#undef ST
4161
4162
a0d0e21e 4163 case END:
3b0527fe 4164 if (locinput < reginfo->till) {
a3621e74 4165 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
7821416a
IZ
4166 "%sMatch possible, but length=%ld is smaller than requested=%ld, failing!%s\n",
4167 PL_colors[4],
4168 (long)(locinput - PL_reg_starttry),
3b0527fe 4169 (long)(reginfo->till - PL_reg_starttry),
7821416a
IZ
4170 PL_colors[5]));
4171 sayNO_FINAL; /* Cannot match: too short. */
4172 }
4173 PL_reginput = locinput; /* put where regtry can find it */
4174 sayYES_FINAL; /* Success! */
dad79028
DM
4175
4176 case SUCCEED: /* successful SUSPEND/UNLESSM/IFMATCH/CURLYM */
4177 DEBUG_EXECUTE_r(
4178 PerlIO_printf(Perl_debug_log,
4179 "%*s %ssubpattern success...%s\n",
4180 REPORT_CODE_OFF+PL_regindent*2, "", PL_colors[4], PL_colors[5]));
3280af22 4181 PL_reginput = locinput; /* put where regtry can find it */
dad79028
DM
4182 sayYES_FINAL; /* Success! */
4183
40a82448
DM
4184#undef ST
4185#define ST st->u.ifmatch
4186
4187 case SUSPEND: /* (?>A) */
4188 ST.wanted = 1;
9fe1d20c 4189 PL_reginput = locinput;
9041c2e3 4190 goto do_ifmatch;
dad79028 4191
40a82448
DM
4192 case UNLESSM: /* -ve lookaround: (?!A), or with flags, (?<!A) */
4193 ST.wanted = 0;
dad79028
DM
4194 goto ifmatch_trivial_fail_test;
4195
40a82448
DM
4196 case IFMATCH: /* +ve lookaround: (?=A), or with flags, (?<=A) */
4197 ST.wanted = 1;
dad79028 4198 ifmatch_trivial_fail_test:
a0ed51b3 4199 if (scan->flags) {
52657f30 4200 char * const s = HOPBACKc(locinput, scan->flags);
dad79028
DM
4201 if (!s) {
4202 /* trivial fail */
4203 if (st->logical) {
4204 st->logical = 0;
40a82448 4205 st->sw = 1 - (bool)ST.wanted;
dad79028 4206 }
40a82448 4207 else if (ST.wanted)
dad79028
DM
4208 sayNO;
4209 next = scan + ARG(scan);
4210 if (next == scan)
4211 next = NULL;
4212 break;
4213 }
efb30f32 4214 PL_reginput = s;
a0ed51b3
LW
4215 }
4216 else
4217 PL_reginput = locinput;
4218
c277df42 4219 do_ifmatch:
40a82448
DM
4220 ST.me = scan;
4221 /* execute body of (?...A) */
4222 PUSH_YES_STATE_GOTO(IFMATCH_A, NEXTOPER(NEXTOPER(scan)));
4223 /* NOTREACHED */
4224
4225 case IFMATCH_A_fail: /* body of (?...A) failed */
4226 ST.wanted = !ST.wanted;
4227 /* FALL THROUGH */
4228
4229 case IFMATCH_A: /* body of (?...A) succeeded */
4230 if (st->logical) {
4231 st->logical = 0;
4232 st->sw = (bool)ST.wanted;
4233 }
4234 else if (!ST.wanted)
4235 sayNO;
4236
4237 if (OP(ST.me) == SUSPEND)
4238 locinput = PL_reginput;
4239 else {
4240 locinput = PL_reginput = st->locinput;
4241 nextchr = UCHARAT(locinput);
4242 }
4243 scan = ST.me + ARG(ST.me);
4244 if (scan == ST.me)
4245 scan = NULL;
4246 continue; /* execute B */
4247
4248#undef ST
dad79028 4249
c277df42 4250 case LONGJMP:
c277df42
IZ
4251 next = scan + ARG(scan);
4252 if (next == scan)
4253 next = NULL;
a0d0e21e
LW
4254 break;
4255 default:
b900a521 4256 PerlIO_printf(Perl_error_log, "%"UVxf" %d\n",
d7d93a81 4257 PTR2UV(scan), OP(scan));
cea2e8a9 4258 Perl_croak(aTHX_ "regexp memory corruption");
a687059c 4259 }
95b24440 4260
a0d0e21e 4261 scan = next;
95b24440
DM
4262 continue;
4263 /* NOTREACHED */
4264
40a82448
DM
4265 push_yes_state:
4266 /* push a state that backtracks on success */
4267 st->u.yes.prev_yes_state = yes_state;
4268 yes_state = st;
4269 /* FALL THROUGH */
4270 push_state:
4271 /* push a new regex state, then continue at scan */
4272 {
4273 regmatch_state *newst;
4274
4275 depth++;
4276 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
4277 "PUSH STATE(%d)\n", depth));
4278 st->locinput = locinput;
4279 newst = st+1;
4280 if (newst > SLAB_LAST(PL_regmatch_slab))
4281 newst = S_push_slab(aTHX);
4282 PL_regmatch_state = newst;
4283 newst->cc = st->cc;
4284 /* XXX probably don't need to initialise these */
4285 newst->minmod = 0;
4286 newst->sw = 0;
4287 newst->logical = 0;
4288
4289 locinput = PL_reginput;
4290 nextchr = UCHARAT(locinput);
4291 st = newst;
4292 continue;
4293 /* NOTREACHED */
4294 }
4295
95b24440
DM
4296 /* simulate recursively calling regmatch(), but without actually
4297 * recursing - ie save the current state on the heap rather than on
4298 * the stack, then re-enter the loop. This avoids complex regexes
4299 * blowing the processor stack */
4300
4301 start_recurse:
4302 {
5d9a96ca
DM
4303 /* push new state */
4304 regmatch_state *oldst = st;
4305
4306 depth++;
40a82448 4307 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "PUSH RECURSE STATE(%d)\n", depth));
5d9a96ca
DM
4308
4309 /* grab the next free state slot */
4310 st++;
86545054 4311 if (st > SLAB_LAST(PL_regmatch_slab))
5d9a96ca
DM
4312 st = S_push_slab(aTHX);
4313 PL_regmatch_state = st;
4314
4315 oldst->next = next;
4316 oldst->n = n;
4317 oldst->locinput = locinput;
5d9a96ca
DM
4318
4319 st->cc = oldst->cc;
95b24440
DM
4320 locinput = PL_reginput;
4321 nextchr = UCHARAT(locinput);
5d9a96ca
DM
4322 st->minmod = 0;
4323 st->sw = 0;
4324 st->logical = 0;
95b24440
DM
4325#ifdef DEBUGGING
4326 PL_regindent++;
4327#endif
4328 }
a0d0e21e 4329 }
a687059c 4330
aa283a38
DM
4331
4332
a0d0e21e
LW
4333 /*
4334 * We get here only if there's trouble -- normally "case END" is
4335 * the terminating point.
4336 */
cea2e8a9 4337 Perl_croak(aTHX_ "corrupted regexp pointers");
a0d0e21e 4338 /*NOTREACHED*/
4633a7c4
LW
4339 sayNO;
4340
7821416a 4341yes_final:
77cb431f
DM
4342
4343 if (yes_state) {
4344 /* we have successfully completed a subexpression, but we must now
4345 * pop to the state marked by yes_state and continue from there */
4346
4347 assert(st != yes_state);
4348 while (yes_state < SLAB_FIRST(PL_regmatch_slab)
4349 || yes_state > SLAB_LAST(PL_regmatch_slab))
4350 {
4351 /* not in this slab, pop slab */
4352 depth -= (st - SLAB_FIRST(PL_regmatch_slab) + 1);
4353 PL_regmatch_slab = PL_regmatch_slab->prev;
4354 st = SLAB_LAST(PL_regmatch_slab);
4355 }
4356 depth -= (st - yes_state);
40a82448
DM
4357 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "POP STATES (%d..%d)\n",
4358 depth+1, depth+(st - yes_state)));
77cb431f
DM
4359 st = yes_state;
4360 yes_state = st->u.yes.prev_yes_state;
4361 PL_regmatch_state = st;
4362
4363 switch (st->resume_state) {
40a82448
DM
4364 case IFMATCH_A:
4365 case CURLYM_A:
40a82448
DM
4366 case EVAL_A:
4367 state_num = st->resume_state;
4368 goto reenter_switch;
4369
4370 case CURLYM_B:
166ba7cd
DM
4371 case BRANCH_next:
4372 case TRIE_next:
c255a977 4373 case CURLY_B_max:
77cb431f 4374 default:
40a82448 4375 Perl_croak(aTHX_ "unexpected yes resume state");
77cb431f
DM
4376 }
4377 }
4378
a3621e74 4379 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch successful!%s\n",
e4584336 4380 PL_colors[4], PL_colors[5]));
4633a7c4
LW
4381yes:
4382#ifdef DEBUGGING
3280af22 4383 PL_regindent--;
4633a7c4 4384#endif
02db2b7b 4385
95b24440 4386 result = 1;
aa283a38 4387 /* XXX this is duplicate(ish) code to that in the do_no section.
40a82448 4388 * will disappear when REGFMATCH goes */
aa283a38
DM
4389 if (depth) {
4390 /* restore previous state and re-enter */
40a82448
DM
4391 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "POP STATE(%d)\n", depth));
4392 depth--;
4393 st--;
4394 if (st < SLAB_FIRST(PL_regmatch_slab)) {
4395 PL_regmatch_slab = PL_regmatch_slab->prev;
4396 st = SLAB_LAST(PL_regmatch_slab);
4397 }
4398 PL_regmatch_state = st;
4399 scan = st->scan;
4400 next = st->next;
4401 n = st->n;
4402 locinput= st->locinput;
4403 nextchr = UCHARAT(locinput);
aa283a38
DM
4404
4405 switch (st->resume_state) {
aa283a38
DM
4406 case resume_CURLYX:
4407 goto resume_point_CURLYX;
4408 case resume_WHILEM1:
4409 goto resume_point_WHILEM1;
4410 case resume_WHILEM2:
4411 goto resume_point_WHILEM2;
4412 case resume_WHILEM3:
4413 goto resume_point_WHILEM3;
4414 case resume_WHILEM4:
4415 goto resume_point_WHILEM4;
4416 case resume_WHILEM5:
4417 goto resume_point_WHILEM5;
4418 case resume_WHILEM6:
4419 goto resume_point_WHILEM6;
77cb431f 4420
166ba7cd 4421 case TRIE_next:
40a82448
DM
4422 case CURLYM_A:
4423 case CURLYM_B:
4424 case EVAL_A:
4425 case IFMATCH_A:
4426 case BRANCH_next:
c255a977
DM
4427 case CURLY_B_max:
4428 case CURLY_B_min:
4429 case CURLY_B_min_known:
40a82448
DM
4430 break;
4431
aa283a38
DM
4432 default:
4433 Perl_croak(aTHX_ "regexp resume memory corruption");
4434 }
4435 }
4436 goto final_exit;
4633a7c4
LW
4437
4438no:
a3621e74 4439 DEBUG_EXECUTE_r(
7821416a
IZ
4440 PerlIO_printf(Perl_debug_log,
4441 "%*s %sfailed...%s\n",
e4584336 4442 REPORT_CODE_OFF+PL_regindent*2, "", PL_colors[4], PL_colors[5])
7821416a 4443 );
7821416a
IZ
4444no_final:
4445do_no:
aa283a38 4446
4633a7c4 4447#ifdef DEBUGGING
3280af22 4448 PL_regindent--;
4633a7c4 4449#endif
95b24440 4450 result = 0;
5d9a96ca 4451
aa283a38
DM
4452 if (depth) {
4453 /* there's a previous state to backtrack to */
40a82448
DM
4454 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "POP STATE(%d)\n", depth));
4455 depth--;
4456 st--;
4457 if (st < SLAB_FIRST(PL_regmatch_slab)) {
4458 PL_regmatch_slab = PL_regmatch_slab->prev;
4459 st = SLAB_LAST(PL_regmatch_slab);
4460 }
4461 PL_regmatch_state = st;
4462 scan = st->scan;
4463 next = st->next;
4464 n = st->n;
4465 locinput= st->locinput;
4466 nextchr = UCHARAT(locinput);
4467
5d9a96ca 4468 switch (st->resume_state) {
95b24440
DM
4469 case resume_CURLYX:
4470 goto resume_point_CURLYX;
4471 case resume_WHILEM1:
4472 goto resume_point_WHILEM1;
4473 case resume_WHILEM2:
4474 goto resume_point_WHILEM2;
4475 case resume_WHILEM3:
4476 goto resume_point_WHILEM3;
4477 case resume_WHILEM4:
4478 goto resume_point_WHILEM4;
4479 case resume_WHILEM5:
4480 goto resume_point_WHILEM5;
4481 case resume_WHILEM6:
4482 goto resume_point_WHILEM6;
dad79028 4483
166ba7cd 4484 case TRIE_next:
40a82448
DM
4485 case EVAL_A:
4486 case BRANCH_next:
4487 case CURLYM_A:
4488 case CURLYM_B:
4489 case IFMATCH_A:
c255a977
DM
4490 case CURLY_B_max:
4491 case CURLY_B_min:
4492 case CURLY_B_min_known:
40a82448
DM
4493 if (yes_state == st)
4494 yes_state = st->u.yes.prev_yes_state;
4495 state_num = st->resume_state + 1; /* failure = success + 1 */
4496 goto reenter_switch;
dad79028 4497
95b24440
DM
4498 default:
4499 Perl_croak(aTHX_ "regexp resume memory corruption");
4500 }
95b24440 4501 }
aa283a38
DM
4502
4503final_exit:
4504
5d9a96ca
DM
4505 /* restore original high-water mark */
4506 PL_regmatch_slab = orig_slab;
4507 PL_regmatch_state = orig_state;
4508
4509 /* free all slabs above current one */
4510 if (orig_slab->next) {
c4fd8992 4511 regmatch_slab *sl = orig_slab->next;
5d9a96ca
DM
4512 orig_slab->next = NULL;
4513 while (sl) {
c4fd8992 4514 regmatch_slab * const osl = sl;
5d9a96ca 4515 sl = sl->next;
ad65c075 4516 Safefree(osl);
5d9a96ca
DM
4517 }
4518 }
4519
95b24440
DM
4520 return result;
4521
a687059c
LW
4522}
4523
4524/*
4525 - regrepeat - repeatedly match something simple, report how many
4526 */
4527/*
4528 * [This routine now assumes that it will only match on things of length 1.
4529 * That was true before, but now we assume scan - reginput is the count,
a0ed51b3 4530 * rather than incrementing count on every character. [Er, except utf8.]]
a687059c 4531 */
76e3520e 4532STATIC I32
32fc9b6a 4533S_regrepeat(pTHX_ const regexp *prog, const regnode *p, I32 max)
a687059c 4534{
27da23d5 4535 dVAR;
a0d0e21e 4536 register char *scan;
a0d0e21e 4537 register I32 c;
3280af22 4538 register char *loceol = PL_regeol;
a0ed51b3 4539 register I32 hardcount = 0;
53c4c00c 4540 register bool do_utf8 = PL_reg_match_utf8;
a0d0e21e 4541
3280af22 4542 scan = PL_reginput;
faf11cac
HS
4543 if (max == REG_INFTY)
4544 max = I32_MAX;
4545 else if (max < loceol - scan)
7f596f4c 4546 loceol = scan + max;
a0d0e21e 4547 switch (OP(p)) {
22c35a8c 4548 case REG_ANY:
1aa99e6b 4549 if (do_utf8) {
ffc61ed2 4550 loceol = PL_regeol;
1aa99e6b 4551 while (scan < loceol && hardcount < max && *scan != '\n') {
ffc61ed2
JH
4552 scan += UTF8SKIP(scan);
4553 hardcount++;
4554 }
4555 } else {
4556 while (scan < loceol && *scan != '\n')
4557 scan++;
a0ed51b3
LW
4558 }
4559 break;
ffc61ed2 4560 case SANY:
def8e4ea
JH
4561 if (do_utf8) {
4562 loceol = PL_regeol;
a0804c9e 4563 while (scan < loceol && hardcount < max) {
def8e4ea
JH
4564 scan += UTF8SKIP(scan);
4565 hardcount++;
4566 }
4567 }
4568 else
4569 scan = loceol;
a0ed51b3 4570 break;
f33976b4
DB
4571 case CANY:
4572 scan = loceol;
4573 break;
090f7165
JH
4574 case EXACT: /* length of string is 1 */
4575 c = (U8)*STRING(p);
4576 while (scan < loceol && UCHARAT(scan) == c)
4577 scan++;
bbce6d69 4578 break;
4579 case EXACTF: /* length of string is 1 */
cd439c50 4580 c = (U8)*STRING(p);
bbce6d69 4581 while (scan < loceol &&
22c35a8c 4582 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold[c]))
bbce6d69 4583 scan++;
4584 break;
4585 case EXACTFL: /* length of string is 1 */
3280af22 4586 PL_reg_flags |= RF_tainted;
cd439c50 4587 c = (U8)*STRING(p);
bbce6d69 4588 while (scan < loceol &&
22c35a8c 4589 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold_locale[c]))
a0d0e21e
LW
4590 scan++;
4591 break;
4592 case ANYOF:
ffc61ed2
JH
4593 if (do_utf8) {
4594 loceol = PL_regeol;
cfc92286 4595 while (hardcount < max && scan < loceol &&
32fc9b6a 4596 reginclass(prog, p, (U8*)scan, 0, do_utf8)) {
ffc61ed2
JH
4597 scan += UTF8SKIP(scan);
4598 hardcount++;
4599 }
4600 } else {
32fc9b6a 4601 while (scan < loceol && REGINCLASS(prog, p, (U8*)scan))
ffc61ed2
JH
4602 scan++;
4603 }
a0d0e21e
LW
4604 break;
4605 case ALNUM:
1aa99e6b 4606 if (do_utf8) {
ffc61ed2 4607 loceol = PL_regeol;
1a4fad37 4608 LOAD_UTF8_CHARCLASS_ALNUM();
1aa99e6b 4609 while (hardcount < max && scan < loceol &&
3568d838 4610 swash_fetch(PL_utf8_alnum, (U8*)scan, do_utf8)) {
ffc61ed2
JH
4611 scan += UTF8SKIP(scan);
4612 hardcount++;
4613 }
4614 } else {
4615 while (scan < loceol && isALNUM(*scan))
4616 scan++;
a0ed51b3
LW
4617 }
4618 break;
bbce6d69 4619 case ALNUML:
3280af22 4620 PL_reg_flags |= RF_tainted;
1aa99e6b 4621 if (do_utf8) {
ffc61ed2 4622 loceol = PL_regeol;
1aa99e6b
IH
4623 while (hardcount < max && scan < loceol &&
4624 isALNUM_LC_utf8((U8*)scan)) {
ffc61ed2
JH
4625 scan += UTF8SKIP(scan);
4626 hardcount++;
4627 }
4628 } else {
4629 while (scan < loceol && isALNUM_LC(*scan))
4630 scan++;
a0ed51b3
LW
4631 }
4632 break;
a0d0e21e 4633 case NALNUM:
1aa99e6b 4634 if (do_utf8) {
ffc61ed2 4635 loceol = PL_regeol;
1a4fad37 4636 LOAD_UTF8_CHARCLASS_ALNUM();
1aa99e6b 4637 while (hardcount < max && scan < loceol &&
3568d838 4638 !swash_fetch(PL_utf8_alnum, (U8*)scan, do_utf8)) {
ffc61ed2
JH
4639 scan += UTF8SKIP(scan);
4640 hardcount++;
4641 }
4642 } else {
4643 while (scan < loceol && !isALNUM(*scan))
4644 scan++;
a0ed51b3
LW
4645 }
4646 break;
bbce6d69 4647 case NALNUML:
3280af22 4648 PL_reg_flags |= RF_tainted;
1aa99e6b 4649 if (do_utf8) {
ffc61ed2 4650 loceol = PL_regeol;
1aa99e6b
IH
4651 while (hardcount < max && scan < loceol &&
4652 !isALNUM_LC_utf8((U8*)scan)) {
ffc61ed2
JH
4653 scan += UTF8SKIP(scan);
4654 hardcount++;
4655 }
4656 } else {
4657 while (scan < loceol && !isALNUM_LC(*scan))
4658 scan++;
a0ed51b3
LW
4659 }
4660 break;
a0d0e21e 4661 case SPACE:
1aa99e6b 4662 if (do_utf8) {
ffc61ed2 4663 loceol = PL_regeol;
1a4fad37 4664 LOAD_UTF8_CHARCLASS_SPACE();
1aa99e6b 4665 while (hardcount < max && scan < loceol &&
3568d838
JH
4666 (*scan == ' ' ||
4667 swash_fetch(PL_utf8_space,(U8*)scan, do_utf8))) {
ffc61ed2
JH
4668 scan += UTF8SKIP(scan);
4669 hardcount++;
4670 }
4671 } else {
4672 while (scan < loceol && isSPACE(*scan))
4673 scan++;
a0ed51b3
LW
4674 }
4675 break;
bbce6d69 4676 case SPACEL:
3280af22 4677 PL_reg_flags |= RF_tainted;
1aa99e6b 4678 if (do_utf8) {
ffc61ed2 4679 loceol = PL_regeol;
1aa99e6b 4680 while (hardcount < max && scan < loceol &&
ffc61ed2
JH
4681 (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
4682 scan += UTF8SKIP(scan);
4683 hardcount++;
4684 }
4685 } else {
4686 while (scan < loceol && isSPACE_LC(*scan))
4687 scan++;
a0ed51b3
LW
4688 }
4689 break;
a0d0e21e 4690 case NSPACE:
1aa99e6b 4691 if (do_utf8) {
ffc61ed2 4692 loceol = PL_regeol;
1a4fad37 4693 LOAD_UTF8_CHARCLASS_SPACE();
1aa99e6b 4694 while (hardcount < max && scan < loceol &&
3568d838
JH
4695 !(*scan == ' ' ||
4696 swash_fetch(PL_utf8_space,(U8*)scan, do_utf8))) {
ffc61ed2
JH
4697 scan += UTF8SKIP(scan);
4698 hardcount++;
4699 }
4700 } else {
4701 while (scan < loceol && !isSPACE(*scan))
4702 scan++;
4703 break;
a0ed51b3 4704 }
bbce6d69 4705 case NSPACEL:
3280af22 4706 PL_reg_flags |= RF_tainted;
1aa99e6b 4707 if (do_utf8) {
ffc61ed2 4708 loceol = PL_regeol;
1aa99e6b 4709 while (hardcount < max && scan < loceol &&
ffc61ed2
JH
4710 !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
4711 scan += UTF8SKIP(scan);
4712 hardcount++;
4713 }
4714 } else {
4715 while (scan < loceol && !isSPACE_LC(*scan))
4716 scan++;
a0ed51b3
LW
4717 }
4718 break;
a0d0e21e 4719 case DIGIT:
1aa99e6b 4720 if (do_utf8) {
ffc61ed2 4721 loceol = PL_regeol;
1a4fad37 4722 LOAD_UTF8_CHARCLASS_DIGIT();
1aa99e6b 4723 while (hardcount < max && scan < loceol &&
3568d838 4724 swash_fetch(PL_utf8_digit, (U8*)scan, do_utf8)) {
ffc61ed2
JH
4725 scan += UTF8SKIP(scan);
4726 hardcount++;
4727 }
4728 } else {
4729 while (scan < loceol && isDIGIT(*scan))
4730 scan++;
a0ed51b3
LW
4731 }
4732 break;
a0d0e21e 4733 case NDIGIT:
1aa99e6b 4734 if (do_utf8) {
ffc61ed2 4735 loceol = PL_regeol;
1a4fad37 4736 LOAD_UTF8_CHARCLASS_DIGIT();
1aa99e6b 4737 while (hardcount < max && scan < loceol &&
3568d838 4738 !swash_fetch(PL_utf8_digit, (U8*)scan, do_utf8)) {
ffc61ed2
JH
4739 scan += UTF8SKIP(scan);
4740 hardcount++;
4741 }
4742 } else {
4743 while (scan < loceol && !isDIGIT(*scan))
4744 scan++;
a0ed51b3
LW
4745 }
4746 break;
a0d0e21e
LW
4747 default: /* Called on something of 0 width. */
4748 break; /* So match right here or not at all. */
4749 }
a687059c 4750
a0ed51b3
LW
4751 if (hardcount)
4752 c = hardcount;
4753 else
4754 c = scan - PL_reginput;
3280af22 4755 PL_reginput = scan;
a687059c 4756
a3621e74 4757 DEBUG_r({
e68ec53f 4758 GET_RE_DEBUG_FLAGS_DECL;
be8e71aa 4759 DEBUG_EXECUTE_r({
e68ec53f
YO
4760 SV * const prop = sv_newmortal();
4761 regprop(prog, prop, p);
4762 PerlIO_printf(Perl_debug_log,
be8e71aa
YO
4763 "%*s %s can match %"IVdf" times out of %"IVdf"...\n",
4764 REPORT_CODE_OFF+1, "", SvPVX_const(prop),(IV)c,(IV)max);
a3621e74 4765 });
be8e71aa 4766 });
9041c2e3 4767
a0d0e21e 4768 return(c);
a687059c
LW
4769}
4770
c277df42 4771
be8e71aa 4772#if !defined(PERL_IN_XSUB_RE) || defined(PLUGGABLE_RE_EXTENSION)
c277df42 4773/*
ffc61ed2
JH
4774- regclass_swash - prepare the utf8 swash
4775*/
4776
4777SV *
32fc9b6a 4778Perl_regclass_swash(pTHX_ const regexp *prog, register const regnode* node, bool doinit, SV** listsvp, SV **altsvp)
ffc61ed2 4779{
97aff369 4780 dVAR;
9e55ce06
JH
4781 SV *sw = NULL;
4782 SV *si = NULL;
4783 SV *alt = NULL;
3dab1dad 4784 const struct reg_data * const data = prog ? prog->data : NULL;
ffc61ed2 4785
4f639d21 4786 if (data && data->count) {
a3b680e6 4787 const U32 n = ARG(node);
ffc61ed2 4788
4f639d21
DM
4789 if (data->what[n] == 's') {
4790 SV * const rv = (SV*)data->data[n];
890ce7af 4791 AV * const av = (AV*)SvRV((SV*)rv);
2d03de9c 4792 SV **const ary = AvARRAY(av);
9e55ce06 4793 SV **a, **b;
9041c2e3 4794
711a919c 4795 /* See the end of regcomp.c:S_regclass() for
9e55ce06
JH
4796 * documentation of these array elements. */
4797
b11f357e 4798 si = *ary;
8f7f7219 4799 a = SvROK(ary[1]) ? &ary[1] : 0;
b11f357e
JH
4800 b = SvTYPE(ary[2]) == SVt_PVAV ? &ary[2] : 0;
4801
ffc61ed2
JH
4802 if (a)
4803 sw = *a;
4804 else if (si && doinit) {
4805 sw = swash_init("utf8", "", si, 1, 0);
4806 (void)av_store(av, 1, sw);
4807 }
9e55ce06
JH
4808 if (b)
4809 alt = *b;
ffc61ed2
JH
4810 }
4811 }
4812
9e55ce06
JH
4813 if (listsvp)
4814 *listsvp = si;
4815 if (altsvp)
4816 *altsvp = alt;
ffc61ed2
JH
4817
4818 return sw;
4819}
76234dfb 4820#endif
ffc61ed2
JH
4821
4822/*
ba7b4546 4823 - reginclass - determine if a character falls into a character class
832705d4
JH
4824
4825 The n is the ANYOF regnode, the p is the target string, lenp
4826 is pointer to the maximum length of how far to go in the p
4827 (if the lenp is zero, UTF8SKIP(p) is used),
4828 do_utf8 tells whether the target string is in UTF-8.
4829
bbce6d69 4830 */
4831
76e3520e 4832STATIC bool
32fc9b6a 4833S_reginclass(pTHX_ const regexp *prog, register const regnode *n, register const U8* p, STRLEN* lenp, register bool do_utf8)
bbce6d69 4834{
27da23d5 4835 dVAR;
a3b680e6 4836 const char flags = ANYOF_FLAGS(n);
bbce6d69 4837 bool match = FALSE;
cc07378b 4838 UV c = *p;
ae9ddab8 4839 STRLEN len = 0;
9e55ce06 4840 STRLEN plen;
1aa99e6b 4841
19f67299
TS
4842 if (do_utf8 && !UTF8_IS_INVARIANT(c)) {
4843 c = utf8n_to_uvchr(p, UTF8_MAXBYTES, &len,
4ad0818d
DM
4844 (UTF8_ALLOW_DEFAULT & UTF8_ALLOW_ANYUV) | UTF8_CHECK_ONLY);
4845 /* see [perl #37836] for UTF8_ALLOW_ANYUV */
19f67299
TS
4846 if (len == (STRLEN)-1)
4847 Perl_croak(aTHX_ "Malformed UTF-8 character (fatal)");
4848 }
bbce6d69 4849
0f0076b4 4850 plen = lenp ? *lenp : UNISKIP(NATIVE_TO_UNI(c));
ffc61ed2 4851 if (do_utf8 || (flags & ANYOF_UNICODE)) {
9e55ce06
JH
4852 if (lenp)
4853 *lenp = 0;
ffc61ed2 4854 if (do_utf8 && !ANYOF_RUNTIME(n)) {
ffc61ed2
JH
4855 if (len != (STRLEN)-1 && c < 256 && ANYOF_BITMAP_TEST(n, c))
4856 match = TRUE;
bbce6d69 4857 }
3568d838 4858 if (!match && do_utf8 && (flags & ANYOF_UNICODE_ALL) && c >= 256)
1aa99e6b 4859 match = TRUE;
ffc61ed2 4860 if (!match) {
9e55ce06 4861 AV *av;
32fc9b6a 4862 SV * const sw = regclass_swash(prog, n, TRUE, 0, (SV**)&av);
ffc61ed2
JH
4863
4864 if (sw) {
3568d838 4865 if (swash_fetch(sw, p, do_utf8))
ffc61ed2
JH
4866 match = TRUE;
4867 else if (flags & ANYOF_FOLD) {
9e55ce06
JH
4868 if (!match && lenp && av) {
4869 I32 i;
9e55ce06 4870 for (i = 0; i <= av_len(av); i++) {
890ce7af 4871 SV* const sv = *av_fetch(av, i, FALSE);
9e55ce06 4872 STRLEN len;
890ce7af 4873 const char * const s = SvPV_const(sv, len);
9e55ce06 4874
061b10df 4875 if (len <= plen && memEQ(s, (char*)p, len)) {
9e55ce06
JH
4876 *lenp = len;
4877 match = TRUE;
4878 break;
4879 }
4880 }
4881 }
4882 if (!match) {
89ebb4a3 4883 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
4a623e43
JH
4884 STRLEN tmplen;
4885
9e55ce06
JH
4886 to_utf8_fold(p, tmpbuf, &tmplen);
4887 if (swash_fetch(sw, tmpbuf, do_utf8))
4888 match = TRUE;
4889 }
ffc61ed2
JH
4890 }
4891 }
bbce6d69 4892 }
9e55ce06 4893 if (match && lenp && *lenp == 0)
0f0076b4 4894 *lenp = UNISKIP(NATIVE_TO_UNI(c));
bbce6d69 4895 }
1aa99e6b 4896 if (!match && c < 256) {
ffc61ed2
JH
4897 if (ANYOF_BITMAP_TEST(n, c))
4898 match = TRUE;
4899 else if (flags & ANYOF_FOLD) {
eb160463 4900 U8 f;
a0ed51b3 4901
ffc61ed2
JH
4902 if (flags & ANYOF_LOCALE) {
4903 PL_reg_flags |= RF_tainted;
4904 f = PL_fold_locale[c];
4905 }
4906 else
4907 f = PL_fold[c];
4908 if (f != c && ANYOF_BITMAP_TEST(n, f))
4909 match = TRUE;
4910 }
4911
4912 if (!match && (flags & ANYOF_CLASS)) {
a0ed51b3 4913 PL_reg_flags |= RF_tainted;
ffc61ed2
JH
4914 if (
4915 (ANYOF_CLASS_TEST(n, ANYOF_ALNUM) && isALNUM_LC(c)) ||
4916 (ANYOF_CLASS_TEST(n, ANYOF_NALNUM) && !isALNUM_LC(c)) ||
4917 (ANYOF_CLASS_TEST(n, ANYOF_SPACE) && isSPACE_LC(c)) ||
4918 (ANYOF_CLASS_TEST(n, ANYOF_NSPACE) && !isSPACE_LC(c)) ||
4919 (ANYOF_CLASS_TEST(n, ANYOF_DIGIT) && isDIGIT_LC(c)) ||
4920 (ANYOF_CLASS_TEST(n, ANYOF_NDIGIT) && !isDIGIT_LC(c)) ||
4921 (ANYOF_CLASS_TEST(n, ANYOF_ALNUMC) && isALNUMC_LC(c)) ||
4922 (ANYOF_CLASS_TEST(n, ANYOF_NALNUMC) && !isALNUMC_LC(c)) ||
4923 (ANYOF_CLASS_TEST(n, ANYOF_ALPHA) && isALPHA_LC(c)) ||
4924 (ANYOF_CLASS_TEST(n, ANYOF_NALPHA) && !isALPHA_LC(c)) ||
4925 (ANYOF_CLASS_TEST(n, ANYOF_ASCII) && isASCII(c)) ||
4926 (ANYOF_CLASS_TEST(n, ANYOF_NASCII) && !isASCII(c)) ||
4927 (ANYOF_CLASS_TEST(n, ANYOF_CNTRL) && isCNTRL_LC(c)) ||
4928 (ANYOF_CLASS_TEST(n, ANYOF_NCNTRL) && !isCNTRL_LC(c)) ||
4929 (ANYOF_CLASS_TEST(n, ANYOF_GRAPH) && isGRAPH_LC(c)) ||
4930 (ANYOF_CLASS_TEST(n, ANYOF_NGRAPH) && !isGRAPH_LC(c)) ||
4931 (ANYOF_CLASS_TEST(n, ANYOF_LOWER) && isLOWER_LC(c)) ||
4932 (ANYOF_CLASS_TEST(n, ANYOF_NLOWER) && !isLOWER_LC(c)) ||
4933 (ANYOF_CLASS_TEST(n, ANYOF_PRINT) && isPRINT_LC(c)) ||
4934 (ANYOF_CLASS_TEST(n, ANYOF_NPRINT) && !isPRINT_LC(c)) ||
4935 (ANYOF_CLASS_TEST(n, ANYOF_PUNCT) && isPUNCT_LC(c)) ||
4936 (ANYOF_CLASS_TEST(n, ANYOF_NPUNCT) && !isPUNCT_LC(c)) ||
4937 (ANYOF_CLASS_TEST(n, ANYOF_UPPER) && isUPPER_LC(c)) ||
4938 (ANYOF_CLASS_TEST(n, ANYOF_NUPPER) && !isUPPER_LC(c)) ||
4939 (ANYOF_CLASS_TEST(n, ANYOF_XDIGIT) && isXDIGIT(c)) ||
4940 (ANYOF_CLASS_TEST(n, ANYOF_NXDIGIT) && !isXDIGIT(c)) ||
4941 (ANYOF_CLASS_TEST(n, ANYOF_PSXSPC) && isPSXSPC(c)) ||
4942 (ANYOF_CLASS_TEST(n, ANYOF_NPSXSPC) && !isPSXSPC(c)) ||
4943 (ANYOF_CLASS_TEST(n, ANYOF_BLANK) && isBLANK(c)) ||
4944 (ANYOF_CLASS_TEST(n, ANYOF_NBLANK) && !isBLANK(c))
4945 ) /* How's that for a conditional? */
4946 {
4947 match = TRUE;
4948 }
a0ed51b3 4949 }
a0ed51b3
LW
4950 }
4951
a0ed51b3
LW
4952 return (flags & ANYOF_INVERT) ? !match : match;
4953}
161b471a 4954
dfe13c55 4955STATIC U8 *
0ce71af7 4956S_reghop3(U8 *s, I32 off, const U8* lim)
9041c2e3 4957{
97aff369 4958 dVAR;
a0ed51b3 4959 if (off >= 0) {
1aa99e6b 4960 while (off-- && s < lim) {
ffc61ed2 4961 /* XXX could check well-formedness here */
a0ed51b3 4962 s += UTF8SKIP(s);
ffc61ed2 4963 }
a0ed51b3
LW
4964 }
4965 else {
4966 while (off++) {
1aa99e6b 4967 if (s > lim) {
a0ed51b3 4968 s--;
ffc61ed2 4969 if (UTF8_IS_CONTINUED(*s)) {
1aa99e6b 4970 while (s > (U8*)lim && UTF8_IS_CONTINUATION(*s))
a0ed51b3 4971 s--;
ffc61ed2
JH
4972 }
4973 /* XXX could check well-formedness here */
a0ed51b3
LW
4974 }
4975 }
4976 }
4977 return s;
4978}
161b471a 4979
dfe13c55 4980STATIC U8 *
0ce71af7 4981S_reghopmaybe3(U8* s, I32 off, const U8* lim)
a0ed51b3 4982{
97aff369 4983 dVAR;
a0ed51b3 4984 if (off >= 0) {
1aa99e6b 4985 while (off-- && s < lim) {
ffc61ed2 4986 /* XXX could check well-formedness here */
a0ed51b3 4987 s += UTF8SKIP(s);
ffc61ed2 4988 }
a0ed51b3 4989 if (off >= 0)
3dab1dad 4990 return NULL;
a0ed51b3
LW
4991 }
4992 else {
4993 while (off++) {
1aa99e6b 4994 if (s > lim) {
a0ed51b3 4995 s--;
ffc61ed2 4996 if (UTF8_IS_CONTINUED(*s)) {
1aa99e6b 4997 while (s > (U8*)lim && UTF8_IS_CONTINUATION(*s))
a0ed51b3 4998 s--;
ffc61ed2
JH
4999 }
5000 /* XXX could check well-formedness here */
a0ed51b3
LW
5001 }
5002 else
5003 break;
5004 }
5005 if (off <= 0)
3dab1dad 5006 return NULL;
a0ed51b3
LW
5007 }
5008 return s;
5009}
51371543 5010
51371543 5011static void
acfe0abc 5012restore_pos(pTHX_ void *arg)
51371543 5013{
97aff369 5014 dVAR;
097eb12c 5015 regexp * const rex = (regexp *)arg;
51371543
GS
5016 if (PL_reg_eval_set) {
5017 if (PL_reg_oldsaved) {
4f639d21
DM
5018 rex->subbeg = PL_reg_oldsaved;
5019 rex->sublen = PL_reg_oldsavedlen;
f8c7b90f 5020#ifdef PERL_OLD_COPY_ON_WRITE
4f639d21 5021 rex->saved_copy = PL_nrs;
ed252734 5022#endif
4f639d21 5023 RX_MATCH_COPIED_on(rex);
51371543
GS
5024 }
5025 PL_reg_magic->mg_len = PL_reg_oldpos;
5026 PL_reg_eval_set = 0;
5027 PL_curpm = PL_reg_oldcurpm;
5028 }
5029}
33b8afdf
JH
5030
5031STATIC void
5032S_to_utf8_substr(pTHX_ register regexp *prog)
5033{
33b8afdf 5034 if (prog->float_substr && !prog->float_utf8) {
097eb12c
AL
5035 SV* const sv = newSVsv(prog->float_substr);
5036 prog->float_utf8 = sv;
33b8afdf
JH
5037 sv_utf8_upgrade(sv);
5038 if (SvTAIL(prog->float_substr))
5039 SvTAIL_on(sv);
5040 if (prog->float_substr == prog->check_substr)
5041 prog->check_utf8 = sv;
5042 }
5043 if (prog->anchored_substr && !prog->anchored_utf8) {
097eb12c
AL
5044 SV* const sv = newSVsv(prog->anchored_substr);
5045 prog->anchored_utf8 = sv;
33b8afdf
JH
5046 sv_utf8_upgrade(sv);
5047 if (SvTAIL(prog->anchored_substr))
5048 SvTAIL_on(sv);
5049 if (prog->anchored_substr == prog->check_substr)
5050 prog->check_utf8 = sv;
5051 }
5052}
5053
5054STATIC void
5055S_to_byte_substr(pTHX_ register regexp *prog)
5056{
97aff369 5057 dVAR;
33b8afdf 5058 if (prog->float_utf8 && !prog->float_substr) {
097eb12c
AL
5059 SV* sv = newSVsv(prog->float_utf8);
5060 prog->float_substr = sv;
33b8afdf
JH
5061 if (sv_utf8_downgrade(sv, TRUE)) {
5062 if (SvTAIL(prog->float_utf8))
5063 SvTAIL_on(sv);
5064 } else {
5065 SvREFCNT_dec(sv);
5066 prog->float_substr = sv = &PL_sv_undef;
5067 }
5068 if (prog->float_utf8 == prog->check_utf8)
5069 prog->check_substr = sv;
5070 }
5071 if (prog->anchored_utf8 && !prog->anchored_substr) {
097eb12c
AL
5072 SV* sv = newSVsv(prog->anchored_utf8);
5073 prog->anchored_substr = sv;
33b8afdf
JH
5074 if (sv_utf8_downgrade(sv, TRUE)) {
5075 if (SvTAIL(prog->anchored_utf8))
5076 SvTAIL_on(sv);
5077 } else {
5078 SvREFCNT_dec(sv);
5079 prog->anchored_substr = sv = &PL_sv_undef;
5080 }
5081 if (prog->anchored_utf8 == prog->check_utf8)
5082 prog->check_substr = sv;
5083 }
5084}
66610fdd
RGS
5085
5086/*
5087 * Local variables:
5088 * c-indentation-style: bsd
5089 * c-basic-offset: 4
5090 * indent-tabs-mode: t
5091 * End:
5092 *
37442d52
RGS
5093 * ex: set ts=8 sts=4 sw=4 noet:
5094 */