This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
generalize the 'continue on success' mechanism of deferred REs
[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.
16
166f8a29
DM
17 */
18
a687059c
LW
19/* NOTE: this is derived from Henry Spencer's regexp code, and should not
20 * confused with the original package (see point 3 below). Thanks, Henry!
21 */
22
23/* Additional note: this code is very heavily munged from Henry's version
24 * in places. In some spots I've traded clarity for efficiency, so don't
25 * blame Henry for some of the lack of readability.
26 */
27
e50aee73
AD
28/* The names of the functions have been changed from regcomp and
29 * regexec to pregcomp and pregexec in order to avoid conflicts
30 * with the POSIX routines of the same names.
31*/
32
b9d5759e
AD
33#ifdef PERL_EXT_RE_BUILD
34/* need to replace pregcomp et al, so enable that */
35# ifndef PERL_IN_XSUB_RE
36# define PERL_IN_XSUB_RE
37# endif
38/* need access to debugger hooks */
cad2e5aa 39# if defined(PERL_EXT_RE_DEBUG) && !defined(DEBUGGING)
b9d5759e
AD
40# define DEBUGGING
41# endif
42#endif
43
44#ifdef PERL_IN_XSUB_RE
d06ea78c 45/* We *really* need to overwrite these symbols: */
56953603
IZ
46# define Perl_regexec_flags my_regexec
47# define Perl_regdump my_regdump
48# define Perl_regprop my_regprop
cad2e5aa 49# define Perl_re_intuit_start my_re_intuit_start
d06ea78c
GS
50/* *These* symbols are masked to allow static link. */
51# define Perl_pregexec my_pregexec
9041c2e3 52# define Perl_reginitcolors my_reginitcolors
490a3f88 53# define Perl_regclass_swash my_regclass_swash
c5be433b
GS
54
55# define PERL_NO_GET_CONTEXT
9041c2e3 56#endif
56953603 57
a687059c 58/*
e50aee73 59 * pregcomp and pregexec -- regsub and regerror are not used in perl
a687059c
LW
60 *
61 * Copyright (c) 1986 by University of Toronto.
62 * Written by Henry Spencer. Not derived from licensed software.
63 *
64 * Permission is granted to anyone to use this software for any
65 * purpose on any computer system, and to redistribute it freely,
66 * subject to the following restrictions:
67 *
68 * 1. The author is not responsible for the consequences of use of
69 * this software, no matter how awful, even if they arise
70 * from defects in it.
71 *
72 * 2. The origin of this software must not be misrepresented, either
73 * by explicit claim or by omission.
74 *
75 * 3. Altered versions must be plainly marked as such, and must not
76 * be misrepresented as being the original software.
77 *
78 **** Alterations to Henry's code are...
79 ****
4bb101f2 80 **** Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
b94e2f88 81 **** 2000, 2001, 2002, 2003, 2004, 2005, 2006, by Larry Wall and others
a687059c 82 ****
9ef589d8
LW
83 **** You may distribute under the terms of either the GNU General Public
84 **** License or the Artistic License, as specified in the README file.
a687059c
LW
85 *
86 * Beware that some of this code is subtly aware of the way operator
87 * precedence is structured in regular expressions. Serious changes in
88 * regular-expression syntax might require a total rethink.
89 */
90#include "EXTERN.h"
864dbfa3 91#define PERL_IN_REGEXEC_C
a687059c 92#include "perl.h"
0f5d15d6 93
a687059c
LW
94#include "regcomp.h"
95
c277df42
IZ
96#define RF_tainted 1 /* tainted information used? */
97#define RF_warned 2 /* warned about big count? */
ce862d02 98#define RF_evaled 4 /* Did an EVAL with setting? */
a0ed51b3
LW
99#define RF_utf8 8 /* String contains multibyte chars? */
100
eb160463 101#define UTF ((PL_reg_flags & RF_utf8) != 0)
ce862d02
IZ
102
103#define RS_init 1 /* eval environment created */
104#define RS_set 2 /* replsv value is set */
c277df42 105
a687059c
LW
106#ifndef STATIC
107#define STATIC static
108#endif
109
32fc9b6a 110#define REGINCLASS(prog,p,c) (ANYOF_FLAGS(p) ? reginclass(prog,p,c,0,0) : ANYOF_BITMAP_TEST(p,*(c)))
7d3e948e 111
c277df42
IZ
112/*
113 * Forwards.
114 */
115
33b8afdf 116#define CHR_SVLEN(sv) (do_utf8 ? sv_len_utf8(sv) : SvCUR(sv))
53c4c00c 117#define CHR_DIST(a,b) (PL_reg_match_utf8 ? utf8_distance(a,b) : a - b)
a0ed51b3 118
52657f30
AL
119#define HOPc(pos,off) ((char *)(PL_reg_match_utf8 \
120 ? reghop3((U8*)pos, off, (U8*)(off >= 0 ? PL_regeol : PL_bostr)) \
121 : (U8*)(pos + off)))
122#define HOPBACKc(pos, off) ((char*) \
123 ((PL_reg_match_utf8) \
124 ? reghopmaybe3((U8*)pos, -off, ((U8*)(off < 0 ? PL_regeol : PL_bostr))) \
efb30f32
HS
125 : (pos - off >= PL_bostr) \
126 ? (U8*)(pos - off) \
52657f30 127 : (U8*)NULL) \
efb30f32 128)
efb30f32 129
1aa99e6b 130#define reghopmaybe3_c(pos,off,lim) ((char*)reghopmaybe3((U8*)pos, off, (U8*)lim))
53c4c00c 131#define HOP3(pos,off,lim) (PL_reg_match_utf8 ? reghop3((U8*)pos, off, (U8*)lim) : (U8*)(pos + off))
1aa99e6b 132#define HOP3c(pos,off,lim) ((char*)HOP3(pos,off,lim))
1aa99e6b 133
1a4fad37
AL
134#define LOAD_UTF8_CHARCLASS(class,str) STMT_START { \
135 if (!CAT2(PL_utf8_,class)) { bool ok; ENTER; save_re_context(); ok=CAT2(is_utf8_,class)((const U8*)str); assert(ok); LEAVE; } } STMT_END
136#define LOAD_UTF8_CHARCLASS_ALNUM() LOAD_UTF8_CHARCLASS(alnum,"a")
137#define LOAD_UTF8_CHARCLASS_DIGIT() LOAD_UTF8_CHARCLASS(digit,"0")
138#define LOAD_UTF8_CHARCLASS_SPACE() LOAD_UTF8_CHARCLASS(space," ")
139#define LOAD_UTF8_CHARCLASS_MARK() LOAD_UTF8_CHARCLASS(mark, "\xcd\x86")
51371543 140
5f80c4cf 141/* for use after a quantifier and before an EXACT-like node -- japhy */
e2d8ce26
JP
142#define JUMPABLE(rn) ( \
143 OP(rn) == OPEN || OP(rn) == CLOSE || OP(rn) == EVAL || \
cca55fe3
JP
144 OP(rn) == SUSPEND || OP(rn) == IFMATCH || \
145 OP(rn) == PLUS || OP(rn) == MINMOD || \
146 (PL_regkind[(U8)OP(rn)] == CURLY && ARG1(rn) > 0) \
e2d8ce26
JP
147)
148
cca55fe3
JP
149#define HAS_TEXT(rn) ( \
150 PL_regkind[(U8)OP(rn)] == EXACT || PL_regkind[(U8)OP(rn)] == REF \
151)
e2d8ce26 152
a84d97b6
HS
153/*
154 Search for mandatory following text node; for lookahead, the text must
155 follow but for lookbehind (rn->flags != 0) we skip to the next step.
156*/
cca55fe3 157#define FIND_NEXT_IMPT(rn) STMT_START { \
e2d8ce26 158 while (JUMPABLE(rn)) \
a84d97b6 159 if (OP(rn) == SUSPEND || PL_regkind[(U8)OP(rn)] == CURLY) \
e2d8ce26 160 rn = NEXTOPER(NEXTOPER(rn)); \
cca55fe3
JP
161 else if (OP(rn) == PLUS) \
162 rn = NEXTOPER(rn); \
a84d97b6
HS
163 else if (OP(rn) == IFMATCH) \
164 rn = (rn->flags == 0) ? NEXTOPER(NEXTOPER(rn)) : rn + ARG(rn); \
e2d8ce26 165 else rn += NEXT_OFF(rn); \
5f80c4cf 166} STMT_END
74750237 167
acfe0abc 168static void restore_pos(pTHX_ void *arg);
51371543 169
76e3520e 170STATIC CHECKPOINT
cea2e8a9 171S_regcppush(pTHX_ I32 parenfloor)
a0d0e21e 172{
97aff369 173 dVAR;
a3b680e6 174 const int retval = PL_savestack_ix;
b1ce53c5 175#define REGCP_PAREN_ELEMS 4
a3b680e6 176 const int paren_elems_to_push = (PL_regsize - parenfloor) * REGCP_PAREN_ELEMS;
a0d0e21e
LW
177 int p;
178
e49a9654
IH
179 if (paren_elems_to_push < 0)
180 Perl_croak(aTHX_ "panic: paren_elems_to_push < 0");
181
a01268b5 182#define REGCP_OTHER_ELEMS 6
4b3c1a47 183 SSGROW(paren_elems_to_push + REGCP_OTHER_ELEMS);
3280af22 184 for (p = PL_regsize; p > parenfloor; p--) {
b1ce53c5 185/* REGCP_PARENS_ELEMS are pushed per pairs of parentheses. */
cf93c79d
IZ
186 SSPUSHINT(PL_regendp[p]);
187 SSPUSHINT(PL_regstartp[p]);
3280af22 188 SSPUSHPTR(PL_reg_start_tmp[p]);
a0d0e21e
LW
189 SSPUSHINT(p);
190 }
b1ce53c5 191/* REGCP_OTHER_ELEMS are pushed in any case, parentheses or no. */
3280af22
NIS
192 SSPUSHINT(PL_regsize);
193 SSPUSHINT(*PL_reglastparen);
a01268b5 194 SSPUSHINT(*PL_reglastcloseparen);
3280af22 195 SSPUSHPTR(PL_reginput);
41123dfd
JH
196#define REGCP_FRAME_ELEMS 2
197/* REGCP_FRAME_ELEMS are part of the REGCP_OTHER_ELEMS and
198 * are needed for the regexp context stack bookkeeping. */
199 SSPUSHINT(paren_elems_to_push + REGCP_OTHER_ELEMS - REGCP_FRAME_ELEMS);
b1ce53c5 200 SSPUSHINT(SAVEt_REGCONTEXT); /* Magic cookie. */
41123dfd 201
a0d0e21e
LW
202 return retval;
203}
204
c277df42 205/* These are needed since we do not localize EVAL nodes: */
a3621e74 206# define REGCP_SET(cp) DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, \
faccc32b 207 " Setting an EVAL scope, savestack=%"IVdf"\n", \
02db2b7b 208 (IV)PL_savestack_ix)); cp = PL_savestack_ix
c3464db5 209
a3621e74 210# define REGCP_UNWIND(cp) DEBUG_EXECUTE_r(cp != PL_savestack_ix ? \
c3464db5 211 PerlIO_printf(Perl_debug_log, \
faccc32b 212 " Clearing an EVAL scope, savestack=%"IVdf"..%"IVdf"\n", \
02db2b7b 213 (IV)(cp), (IV)PL_savestack_ix) : 0); regcpblow(cp)
c277df42 214
76e3520e 215STATIC char *
097eb12c 216S_regcppop(pTHX_ const regexp *rex)
a0d0e21e 217{
97aff369 218 dVAR;
b1ce53c5 219 I32 i;
a0d0e21e 220 char *input;
b1ce53c5 221
a3621e74
YO
222 GET_RE_DEBUG_FLAGS_DECL;
223
b1ce53c5 224 /* Pop REGCP_OTHER_ELEMS before the parentheses loop starts. */
a0d0e21e 225 i = SSPOPINT;
b1ce53c5
JH
226 assert(i == SAVEt_REGCONTEXT); /* Check that the magic cookie is there. */
227 i = SSPOPINT; /* Parentheses elements to pop. */
a0d0e21e 228 input = (char *) SSPOPPTR;
a01268b5 229 *PL_reglastcloseparen = SSPOPINT;
3280af22
NIS
230 *PL_reglastparen = SSPOPINT;
231 PL_regsize = SSPOPINT;
b1ce53c5
JH
232
233 /* Now restore the parentheses context. */
41123dfd
JH
234 for (i -= (REGCP_OTHER_ELEMS - REGCP_FRAME_ELEMS);
235 i > 0; i -= REGCP_PAREN_ELEMS) {
1df70142 236 I32 tmps;
097eb12c 237 U32 paren = (U32)SSPOPINT;
3280af22 238 PL_reg_start_tmp[paren] = (char *) SSPOPPTR;
cf93c79d
IZ
239 PL_regstartp[paren] = SSPOPINT;
240 tmps = SSPOPINT;
3280af22
NIS
241 if (paren <= *PL_reglastparen)
242 PL_regendp[paren] = tmps;
a3621e74 243 DEBUG_EXECUTE_r(
c3464db5 244 PerlIO_printf(Perl_debug_log,
b900a521 245 " restoring \\%"UVuf" to %"IVdf"(%"IVdf")..%"IVdf"%s\n",
9041c2e3 246 (UV)paren, (IV)PL_regstartp[paren],
b900a521 247 (IV)(PL_reg_start_tmp[paren] - PL_bostr),
9041c2e3 248 (IV)PL_regendp[paren],
3280af22 249 (paren > *PL_reglastparen ? "(no)" : ""));
c277df42 250 );
a0d0e21e 251 }
a3621e74 252 DEBUG_EXECUTE_r(
4f639d21 253 if ((I32)(*PL_reglastparen + 1) <= rex->nparens) {
c3464db5 254 PerlIO_printf(Perl_debug_log,
faccc32b 255 " restoring \\%"IVdf"..\\%"IVdf" to undef\n",
4f639d21 256 (IV)(*PL_reglastparen + 1), (IV)rex->nparens);
c277df42
IZ
257 }
258 );
daf18116 259#if 1
dafc8851
JH
260 /* It would seem that the similar code in regtry()
261 * already takes care of this, and in fact it is in
262 * a better location to since this code can #if 0-ed out
263 * but the code in regtry() is needed or otherwise tests
264 * requiring null fields (pat.t#187 and split.t#{13,14}
daf18116
JH
265 * (as of patchlevel 7877) will fail. Then again,
266 * this code seems to be necessary or otherwise
267 * building DynaLoader will fail:
268 * "Error: '*' not in typemap in DynaLoader.xs, line 164"
269 * --jhi */
097eb12c
AL
270 for (i = *PL_reglastparen + 1; i <= rex->nparens; i++) {
271 if (i > PL_regsize)
272 PL_regstartp[i] = -1;
273 PL_regendp[i] = -1;
a0d0e21e 274 }
dafc8851 275#endif
a0d0e21e
LW
276 return input;
277}
278
02db2b7b 279#define regcpblow(cp) LEAVE_SCOPE(cp) /* Ignores regcppush()ed data. */
a0d0e21e 280
95b24440 281#define TRYPAREN(paren, n, input, where) { \
29d1e993
HS
282 if (paren) { \
283 if (n) { \
284 PL_regstartp[paren] = HOPc(input, -1) - PL_bostr; \
285 PL_regendp[paren] = input - PL_bostr; \
286 } \
287 else \
288 PL_regendp[paren] = -1; \
289 } \
95b24440
DM
290 REGMATCH(next, where); \
291 if (result) \
29d1e993
HS
292 sayYES; \
293 if (paren && n) \
294 PL_regendp[paren] = -1; \
295}
296
297
a687059c 298/*
e50aee73 299 * pregexec and friends
a687059c
LW
300 */
301
302/*
c277df42 303 - pregexec - match a regexp against a string
a687059c 304 */
c277df42 305I32
864dbfa3 306Perl_pregexec(pTHX_ register regexp *prog, char *stringarg, register char *strend,
c3464db5 307 char *strbeg, I32 minend, SV *screamer, U32 nosave)
c277df42
IZ
308/* strend: pointer to null at end of string */
309/* strbeg: real beginning of string */
310/* minend: end of match must be >=minend after stringarg. */
311/* nosave: For optimizations. */
312{
313 return
9041c2e3 314 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
c277df42
IZ
315 nosave ? 0 : REXEC_COPY_STR);
316}
0f5d15d6 317
22e551b9 318
9041c2e3 319/*
cad2e5aa
JH
320 * Need to implement the following flags for reg_anch:
321 *
322 * USE_INTUIT_NOML - Useful to call re_intuit_start() first
323 * USE_INTUIT_ML
324 * INTUIT_AUTORITATIVE_NOML - Can trust a positive answer
325 * INTUIT_AUTORITATIVE_ML
326 * INTUIT_ONCE_NOML - Intuit can match in one location only.
327 * INTUIT_ONCE_ML
328 *
329 * Another flag for this function: SECOND_TIME (so that float substrs
330 * with giant delta may be not rechecked).
331 */
332
333/* Assumptions: if ANCH_GPOS, then strpos is anchored. XXXX Check GPOS logic */
334
3f7c398e 335/* If SCREAM, then SvPVX_const(sv) should be compatible with strpos and strend.
cad2e5aa
JH
336 Otherwise, only SvCUR(sv) is used to get strbeg. */
337
338/* XXXX We assume that strpos is strbeg unless sv. */
339
6eb5f6b9
JH
340/* XXXX Some places assume that there is a fixed substring.
341 An update may be needed if optimizer marks as "INTUITable"
342 RExen without fixed substrings. Similarly, it is assumed that
343 lengths of all the strings are no more than minlen, thus they
344 cannot come from lookahead.
345 (Or minlen should take into account lookahead.) */
346
2c2d71f5
JH
347/* A failure to find a constant substring means that there is no need to make
348 an expensive call to REx engine, thus we celebrate a failure. Similarly,
349 finding a substring too deep into the string means that less calls to
30944b6d
IZ
350 regtry() should be needed.
351
352 REx compiler's optimizer found 4 possible hints:
353 a) Anchored substring;
354 b) Fixed substring;
355 c) Whether we are anchored (beginning-of-line or \G);
356 d) First node (of those at offset 0) which may distingush positions;
6eb5f6b9 357 We use a)b)d) and multiline-part of c), and try to find a position in the
30944b6d
IZ
358 string which does not contradict any of them.
359 */
2c2d71f5 360
6eb5f6b9
JH
361/* Most of decisions we do here should have been done at compile time.
362 The nodes of the REx which we used for the search should have been
363 deleted from the finite automaton. */
364
cad2e5aa
JH
365char *
366Perl_re_intuit_start(pTHX_ regexp *prog, SV *sv, char *strpos,
367 char *strend, U32 flags, re_scream_pos_data *data)
368{
97aff369 369 dVAR;
b7953727 370 register I32 start_shift = 0;
cad2e5aa 371 /* Should be nonnegative! */
b7953727 372 register I32 end_shift = 0;
2c2d71f5
JH
373 register char *s;
374 register SV *check;
a1933d95 375 char *strbeg;
cad2e5aa 376 char *t;
a3b680e6 377 const int do_utf8 = sv ? SvUTF8(sv) : 0; /* if no sv we have to assume bytes */
cad2e5aa 378 I32 ml_anch;
bd61b366
SS
379 register char *other_last = NULL; /* other substr checked before this */
380 char *check_at = NULL; /* check substr found at this pos */
1df70142 381 const I32 multiline = prog->reganch & PMf_MULTILINE;
30944b6d 382#ifdef DEBUGGING
890ce7af
AL
383 const char * const i_strpos = strpos;
384 SV * const dsv = PERL_DEBUG_PAD_ZERO(0);
30944b6d 385#endif
a3621e74
YO
386
387 GET_RE_DEBUG_FLAGS_DECL;
388
a30b2f1f 389 RX_MATCH_UTF8_set(prog,do_utf8);
cad2e5aa 390
b8d68ded 391 if (prog->reganch & ROPT_UTF8) {
a3621e74 392 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
b8d68ded
JH
393 "UTF-8 regex...\n"));
394 PL_reg_flags |= RF_utf8;
395 }
396
a3621e74 397 DEBUG_EXECUTE_r({
1df70142 398 const char *s = PL_reg_match_utf8 ?
c728cb41
JH
399 sv_uni_display(dsv, sv, 60, UNI_DISPLAY_REGEX) :
400 strpos;
1df70142 401 const int len = PL_reg_match_utf8 ?
b8d68ded 402 strlen(s) : strend - strpos;
2a782b5b
JH
403 if (!PL_colorset)
404 reginitcolors();
b8d68ded 405 if (PL_reg_match_utf8)
a3621e74 406 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
b8d68ded 407 "UTF-8 target...\n"));
2a782b5b 408 PerlIO_printf(Perl_debug_log,
a0288114 409 "%sGuessing start of match, REx%s \"%s%.60s%s%s\" against \"%s%.*s%s%s\"...\n",
e4584336 410 PL_colors[4], PL_colors[5], PL_colors[0],
2a782b5b
JH
411 prog->precomp,
412 PL_colors[1],
413 (strlen(prog->precomp) > 60 ? "..." : ""),
414 PL_colors[0],
415 (int)(len > 60 ? 60 : len),
416 s, PL_colors[1],
417 (len > 60 ? "..." : "")
418 );
419 });
cad2e5aa 420
c344f387
JH
421 /* CHR_DIST() would be more correct here but it makes things slow. */
422 if (prog->minlen > strend - strpos) {
a3621e74 423 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
a72c7584 424 "String too short... [re_intuit_start]\n"));
cad2e5aa 425 goto fail;
2c2d71f5 426 }
a1933d95 427 strbeg = (sv && SvPOK(sv)) ? strend - SvCUR(sv) : strpos;
1aa99e6b 428 PL_regeol = strend;
33b8afdf
JH
429 if (do_utf8) {
430 if (!prog->check_utf8 && prog->check_substr)
431 to_utf8_substr(prog);
432 check = prog->check_utf8;
433 } else {
434 if (!prog->check_substr && prog->check_utf8)
435 to_byte_substr(prog);
436 check = prog->check_substr;
437 }
438 if (check == &PL_sv_undef) {
a3621e74 439 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
33b8afdf
JH
440 "Non-utf string cannot match utf check string\n"));
441 goto fail;
442 }
2c2d71f5 443 if (prog->reganch & ROPT_ANCH) { /* Match at beg-of-str or after \n */
cad2e5aa
JH
444 ml_anch = !( (prog->reganch & ROPT_ANCH_SINGLE)
445 || ( (prog->reganch & ROPT_ANCH_BOL)
7fba1cd6 446 && !multiline ) ); /* Check after \n? */
cad2e5aa 447
7e25d62c
JH
448 if (!ml_anch) {
449 if ( !(prog->reganch & (ROPT_ANCH_GPOS /* Checked by the caller */
450 | ROPT_IMPLICIT)) /* not a real BOL */
3f7c398e 451 /* SvCUR is not set on references: SvRV and SvPVX_const overlap */
7e25d62c
JH
452 && sv && !SvROK(sv)
453 && (strpos != strbeg)) {
a3621e74 454 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Not at start...\n"));
7e25d62c
JH
455 goto fail;
456 }
457 if (prog->check_offset_min == prog->check_offset_max &&
cce850e4 458 !(prog->reganch & ROPT_CANY_SEEN)) {
2c2d71f5 459 /* Substring at constant offset from beg-of-str... */
cad2e5aa
JH
460 I32 slen;
461
1aa99e6b 462 s = HOP3c(strpos, prog->check_offset_min, strend);
653099ff
GS
463 if (SvTAIL(check)) {
464 slen = SvCUR(check); /* >= 1 */
cad2e5aa 465
9041c2e3 466 if ( strend - s > slen || strend - s < slen - 1
2c2d71f5 467 || (strend - s == slen && strend[-1] != '\n')) {
a3621e74 468 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "String too long...\n"));
2c2d71f5 469 goto fail_finish;
cad2e5aa
JH
470 }
471 /* Now should match s[0..slen-2] */
472 slen--;
3f7c398e 473 if (slen && (*SvPVX_const(check) != *s
cad2e5aa 474 || (slen > 1
3f7c398e 475 && memNE(SvPVX_const(check), s, slen)))) {
2c2d71f5 476 report_neq:
a3621e74 477 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "String not equal...\n"));
2c2d71f5
JH
478 goto fail_finish;
479 }
cad2e5aa 480 }
3f7c398e 481 else if (*SvPVX_const(check) != *s
653099ff 482 || ((slen = SvCUR(check)) > 1
3f7c398e 483 && memNE(SvPVX_const(check), s, slen)))
2c2d71f5 484 goto report_neq;
c315bfe8 485 check_at = s;
2c2d71f5 486 goto success_at_start;
7e25d62c 487 }
cad2e5aa 488 }
2c2d71f5 489 /* Match is anchored, but substr is not anchored wrt beg-of-str. */
cad2e5aa 490 s = strpos;
2c2d71f5 491 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
2c2d71f5 492 end_shift = prog->minlen - start_shift -
653099ff 493 CHR_SVLEN(check) + (SvTAIL(check) != 0);
2c2d71f5 494 if (!ml_anch) {
a3b680e6 495 const I32 end = prog->check_offset_max + CHR_SVLEN(check)
653099ff 496 - (SvTAIL(check) != 0);
a3b680e6 497 const I32 eshift = CHR_DIST((U8*)strend, (U8*)s) - end;
2c2d71f5
JH
498
499 if (end_shift < eshift)
500 end_shift = eshift;
501 }
cad2e5aa 502 }
2c2d71f5 503 else { /* Can match at random position */
cad2e5aa
JH
504 ml_anch = 0;
505 s = strpos;
2c2d71f5
JH
506 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
507 /* Should be nonnegative! */
508 end_shift = prog->minlen - start_shift -
653099ff 509 CHR_SVLEN(check) + (SvTAIL(check) != 0);
cad2e5aa
JH
510 }
511
2c2d71f5 512#ifdef DEBUGGING /* 7/99: reports of failure (with the older version) */
0033605d 513 if (end_shift < 0)
6bbae5e6 514 Perl_croak(aTHX_ "panic: end_shift");
2c2d71f5
JH
515#endif
516
2c2d71f5
JH
517 restart:
518 /* Find a possible match in the region s..strend by looking for
519 the "check" substring in the region corrected by start/end_shift. */
cad2e5aa 520 if (flags & REXEC_SCREAM) {
cad2e5aa 521 I32 p = -1; /* Internal iterator of scream. */
a3b680e6 522 I32 * const pp = data ? data->scream_pos : &p;
cad2e5aa 523
2c2d71f5
JH
524 if (PL_screamfirst[BmRARE(check)] >= 0
525 || ( BmRARE(check) == '\n'
526 && (BmPREVIOUS(check) == SvCUR(check) - 1)
527 && SvTAIL(check) ))
9041c2e3 528 s = screaminstr(sv, check,
2c2d71f5 529 start_shift + (s - strbeg), end_shift, pp, 0);
cad2e5aa 530 else
2c2d71f5 531 goto fail_finish;
4addbd3b
HS
532 /* we may be pointing at the wrong string */
533 if (s && RX_MATCH_COPIED(prog))
3f7c398e 534 s = strbeg + (s - SvPVX_const(sv));
cad2e5aa
JH
535 if (data)
536 *data->scream_olds = s;
537 }
f33976b4 538 else if (prog->reganch & ROPT_CANY_SEEN)
3baa4c62
JH
539 s = fbm_instr((U8*)(s + start_shift),
540 (U8*)(strend - end_shift),
7fba1cd6 541 check, multiline ? FBMrf_MULTILINE : 0);
cad2e5aa 542 else
1aa99e6b
IH
543 s = fbm_instr(HOP3(s, start_shift, strend),
544 HOP3(strend, -end_shift, strbeg),
7fba1cd6 545 check, multiline ? FBMrf_MULTILINE : 0);
cad2e5aa
JH
546
547 /* Update the count-of-usability, remove useless subpatterns,
548 unshift s. */
2c2d71f5 549
a0288114 550 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%s %s substr \"%s%.*s%s\"%s%s",
2c2d71f5 551 (s ? "Found" : "Did not find"),
33b8afdf 552 (check == (do_utf8 ? prog->anchored_utf8 : prog->anchored_substr) ? "anchored" : "floating"),
2c2d71f5 553 PL_colors[0],
7b0972df 554 (int)(SvCUR(check) - (SvTAIL(check)!=0)),
3f7c398e 555 SvPVX_const(check),
2c2d71f5
JH
556 PL_colors[1], (SvTAIL(check) ? "$" : ""),
557 (s ? " at offset " : "...\n") ) );
558
559 if (!s)
560 goto fail_finish;
561
6eb5f6b9
JH
562 check_at = s;
563
2c2d71f5 564 /* Finish the diagnostic message */
a3621e74 565 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%ld...\n", (long)(s - i_strpos)) );
2c2d71f5
JH
566
567 /* Got a candidate. Check MBOL anchoring, and the *other* substr.
568 Start with the other substr.
569 XXXX no SCREAM optimization yet - and a very coarse implementation
a0288114 570 XXXX /ttx+/ results in anchored="ttx", floating="x". floating will
2c2d71f5
JH
571 *always* match. Probably should be marked during compile...
572 Probably it is right to do no SCREAM here...
573 */
574
33b8afdf 575 if (do_utf8 ? (prog->float_utf8 && prog->anchored_utf8) : (prog->float_substr && prog->anchored_substr)) {
30944b6d 576 /* Take into account the "other" substring. */
2c2d71f5
JH
577 /* XXXX May be hopelessly wrong for UTF... */
578 if (!other_last)
6eb5f6b9 579 other_last = strpos;
33b8afdf 580 if (check == (do_utf8 ? prog->float_utf8 : prog->float_substr)) {
30944b6d
IZ
581 do_other_anchored:
582 {
890ce7af
AL
583 char * const last = HOP3c(s, -start_shift, strbeg);
584 char *last1, *last2;
2c2d71f5 585 char *s1 = s;
33b8afdf 586 SV* must;
2c2d71f5 587
2c2d71f5
JH
588 t = s - prog->check_offset_max;
589 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
1d86a7f9 590 && (!do_utf8
1aa99e6b 591 || ((t = reghopmaybe3_c(s, -(prog->check_offset_max), strpos))
2c2d71f5 592 && t > strpos)))
30944b6d 593 /* EMPTY */;
2c2d71f5
JH
594 else
595 t = strpos;
1aa99e6b 596 t = HOP3c(t, prog->anchored_offset, strend);
6eb5f6b9
JH
597 if (t < other_last) /* These positions already checked */
598 t = other_last;
1aa99e6b 599 last2 = last1 = HOP3c(strend, -prog->minlen, strbeg);
2c2d71f5
JH
600 if (last < last1)
601 last1 = last;
602 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
603 /* On end-of-str: see comment below. */
33b8afdf
JH
604 must = do_utf8 ? prog->anchored_utf8 : prog->anchored_substr;
605 if (must == &PL_sv_undef) {
606 s = (char*)NULL;
a3621e74 607 DEBUG_EXECUTE_r(must = prog->anchored_utf8); /* for debug */
33b8afdf
JH
608 }
609 else
610 s = fbm_instr(
611 (unsigned char*)t,
612 HOP3(HOP3(last1, prog->anchored_offset, strend)
613 + SvCUR(must), -(SvTAIL(must)!=0), strbeg),
614 must,
7fba1cd6 615 multiline ? FBMrf_MULTILINE : 0
33b8afdf 616 );
a3621e74 617 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
a0288114 618 "%s anchored substr \"%s%.*s%s\"%s",
2c2d71f5
JH
619 (s ? "Found" : "Contradicts"),
620 PL_colors[0],
33b8afdf
JH
621 (int)(SvCUR(must)
622 - (SvTAIL(must)!=0)),
3f7c398e 623 SvPVX_const(must),
33b8afdf 624 PL_colors[1], (SvTAIL(must) ? "$" : "")));
2c2d71f5
JH
625 if (!s) {
626 if (last1 >= last2) {
a3621e74 627 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
2c2d71f5
JH
628 ", giving up...\n"));
629 goto fail_finish;
630 }
a3621e74 631 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
2c2d71f5 632 ", trying floating at offset %ld...\n",
1aa99e6b
IH
633 (long)(HOP3c(s1, 1, strend) - i_strpos)));
634 other_last = HOP3c(last1, prog->anchored_offset+1, strend);
635 s = HOP3c(last, 1, strend);
2c2d71f5
JH
636 goto restart;
637 }
638 else {
a3621e74 639 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
30944b6d 640 (long)(s - i_strpos)));
1aa99e6b
IH
641 t = HOP3c(s, -prog->anchored_offset, strbeg);
642 other_last = HOP3c(s, 1, strend);
30944b6d 643 s = s1;
2c2d71f5
JH
644 if (t == strpos)
645 goto try_at_start;
2c2d71f5
JH
646 goto try_at_offset;
647 }
30944b6d 648 }
2c2d71f5
JH
649 }
650 else { /* Take into account the floating substring. */
33b8afdf
JH
651 char *last, *last1;
652 char *s1 = s;
653 SV* must;
654
655 t = HOP3c(s, -start_shift, strbeg);
656 last1 = last =
657 HOP3c(strend, -prog->minlen + prog->float_min_offset, strbeg);
658 if (CHR_DIST((U8*)last, (U8*)t) > prog->float_max_offset)
659 last = HOP3c(t, prog->float_max_offset, strend);
660 s = HOP3c(t, prog->float_min_offset, strend);
661 if (s < other_last)
662 s = other_last;
2c2d71f5 663 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
33b8afdf
JH
664 must = do_utf8 ? prog->float_utf8 : prog->float_substr;
665 /* fbm_instr() takes into account exact value of end-of-str
666 if the check is SvTAIL(ed). Since false positives are OK,
667 and end-of-str is not later than strend we are OK. */
668 if (must == &PL_sv_undef) {
669 s = (char*)NULL;
a3621e74 670 DEBUG_EXECUTE_r(must = prog->float_utf8); /* for debug message */
33b8afdf
JH
671 }
672 else
2c2d71f5 673 s = fbm_instr((unsigned char*)s,
33b8afdf
JH
674 (unsigned char*)last + SvCUR(must)
675 - (SvTAIL(must)!=0),
7fba1cd6 676 must, multiline ? FBMrf_MULTILINE : 0);
a0288114 677 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%s floating substr \"%s%.*s%s\"%s",
33b8afdf
JH
678 (s ? "Found" : "Contradicts"),
679 PL_colors[0],
680 (int)(SvCUR(must) - (SvTAIL(must)!=0)),
3f7c398e 681 SvPVX_const(must),
33b8afdf
JH
682 PL_colors[1], (SvTAIL(must) ? "$" : "")));
683 if (!s) {
684 if (last1 == last) {
a3621e74 685 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
33b8afdf
JH
686 ", giving up...\n"));
687 goto fail_finish;
2c2d71f5 688 }
a3621e74 689 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
33b8afdf
JH
690 ", trying anchored starting at offset %ld...\n",
691 (long)(s1 + 1 - i_strpos)));
692 other_last = last;
693 s = HOP3c(t, 1, strend);
694 goto restart;
695 }
696 else {
a3621e74 697 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
33b8afdf
JH
698 (long)(s - i_strpos)));
699 other_last = s; /* Fix this later. --Hugo */
700 s = s1;
701 if (t == strpos)
702 goto try_at_start;
703 goto try_at_offset;
704 }
2c2d71f5 705 }
cad2e5aa 706 }
2c2d71f5
JH
707
708 t = s - prog->check_offset_max;
2c2d71f5 709 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
1d86a7f9 710 && (!do_utf8
1aa99e6b
IH
711 || ((t = reghopmaybe3_c(s, -prog->check_offset_max, strpos))
712 && t > strpos))) {
2c2d71f5
JH
713 /* Fixed substring is found far enough so that the match
714 cannot start at strpos. */
715 try_at_offset:
cad2e5aa 716 if (ml_anch && t[-1] != '\n') {
30944b6d
IZ
717 /* Eventually fbm_*() should handle this, but often
718 anchored_offset is not 0, so this check will not be wasted. */
719 /* XXXX In the code below we prefer to look for "^" even in
720 presence of anchored substrings. And we search even
721 beyond the found float position. These pessimizations
722 are historical artefacts only. */
723 find_anchor:
2c2d71f5 724 while (t < strend - prog->minlen) {
cad2e5aa 725 if (*t == '\n') {
4ee3650e 726 if (t < check_at - prog->check_offset_min) {
33b8afdf 727 if (do_utf8 ? prog->anchored_utf8 : prog->anchored_substr) {
4ee3650e
GS
728 /* Since we moved from the found position,
729 we definitely contradict the found anchored
30944b6d
IZ
730 substr. Due to the above check we do not
731 contradict "check" substr.
732 Thus we can arrive here only if check substr
733 is float. Redo checking for "other"=="fixed".
734 */
9041c2e3 735 strpos = t + 1;
a3621e74 736 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld, rescanning for anchored from offset %ld...\n",
e4584336 737 PL_colors[0], PL_colors[1], (long)(strpos - i_strpos), (long)(strpos - i_strpos + prog->anchored_offset)));
30944b6d
IZ
738 goto do_other_anchored;
739 }
4ee3650e
GS
740 /* We don't contradict the found floating substring. */
741 /* XXXX Why not check for STCLASS? */
cad2e5aa 742 s = t + 1;
a3621e74 743 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld...\n",
e4584336 744 PL_colors[0], PL_colors[1], (long)(s - i_strpos)));
cad2e5aa
JH
745 goto set_useful;
746 }
4ee3650e
GS
747 /* Position contradicts check-string */
748 /* XXXX probably better to look for check-string
749 than for "\n", so one should lower the limit for t? */
a3621e74 750 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m, restarting lookup for check-string at offset %ld...\n",
e4584336 751 PL_colors[0], PL_colors[1], (long)(t + 1 - i_strpos)));
0e41cd87 752 other_last = strpos = s = t + 1;
cad2e5aa
JH
753 goto restart;
754 }
755 t++;
756 }
a3621e74 757 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Did not find /%s^%s/m...\n",
e4584336 758 PL_colors[0], PL_colors[1]));
2c2d71f5 759 goto fail_finish;
cad2e5aa 760 }
f5952150 761 else {
a3621e74 762 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Starting position does not contradict /%s^%s/m...\n",
e4584336 763 PL_colors[0], PL_colors[1]));
f5952150 764 }
cad2e5aa
JH
765 s = t;
766 set_useful:
33b8afdf 767 ++BmUSEFUL(do_utf8 ? prog->check_utf8 : prog->check_substr); /* hooray/5 */
cad2e5aa
JH
768 }
769 else {
f5952150 770 /* The found string does not prohibit matching at strpos,
2c2d71f5 771 - no optimization of calling REx engine can be performed,
f5952150
GS
772 unless it was an MBOL and we are not after MBOL,
773 or a future STCLASS check will fail this. */
2c2d71f5
JH
774 try_at_start:
775 /* Even in this situation we may use MBOL flag if strpos is offset
776 wrt the start of the string. */
05b4157f 777 if (ml_anch && sv && !SvROK(sv) /* See prev comment on SvROK */
a1933d95 778 && (strpos != strbeg) && strpos[-1] != '\n'
d506a20d
IZ
779 /* May be due to an implicit anchor of m{.*foo} */
780 && !(prog->reganch & ROPT_IMPLICIT))
781 {
cad2e5aa
JH
782 t = strpos;
783 goto find_anchor;
784 }
a3621e74 785 DEBUG_EXECUTE_r( if (ml_anch)
f5952150 786 PerlIO_printf(Perl_debug_log, "Position at offset %ld does not contradict /%s^%s/m...\n",
e4584336 787 (long)(strpos - i_strpos), PL_colors[0], PL_colors[1]);
30944b6d 788 );
2c2d71f5 789 success_at_start:
30944b6d 790 if (!(prog->reganch & ROPT_NAUGHTY) /* XXXX If strpos moved? */
33b8afdf
JH
791 && (do_utf8 ? (
792 prog->check_utf8 /* Could be deleted already */
793 && --BmUSEFUL(prog->check_utf8) < 0
794 && (prog->check_utf8 == prog->float_utf8)
795 ) : (
796 prog->check_substr /* Could be deleted already */
797 && --BmUSEFUL(prog->check_substr) < 0
798 && (prog->check_substr == prog->float_substr)
799 )))
66e933ab 800 {
cad2e5aa 801 /* If flags & SOMETHING - do not do it many times on the same match */
a3621e74 802 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "... Disabling check substring...\n"));
33b8afdf
JH
803 SvREFCNT_dec(do_utf8 ? prog->check_utf8 : prog->check_substr);
804 if (do_utf8 ? prog->check_substr : prog->check_utf8)
805 SvREFCNT_dec(do_utf8 ? prog->check_substr : prog->check_utf8);
a0714e2c
SS
806 prog->check_substr = prog->check_utf8 = NULL; /* disable */
807 prog->float_substr = prog->float_utf8 = NULL; /* clear */
808 check = NULL; /* abort */
cad2e5aa 809 s = strpos;
3cf5c195
IZ
810 /* XXXX This is a remnant of the old implementation. It
811 looks wasteful, since now INTUIT can use many
6eb5f6b9 812 other heuristics. */
cad2e5aa
JH
813 prog->reganch &= ~RE_USE_INTUIT;
814 }
815 else
816 s = strpos;
817 }
818
6eb5f6b9
JH
819 /* Last resort... */
820 /* XXXX BmUSEFUL already changed, maybe multiple change is meaningful... */
821 if (prog->regstclass) {
822 /* minlen == 0 is possible if regstclass is \b or \B,
823 and the fixed substr is ''$.
824 Since minlen is already taken into account, s+1 is before strend;
825 accidentally, minlen >= 1 guaranties no false positives at s + 1
826 even for \b or \B. But (minlen? 1 : 0) below assumes that
827 regstclass does not come from lookahead... */
828 /* If regstclass takes bytelength more than 1: If charlength==1, OK.
829 This leaves EXACTF only, which is dealt with in find_byclass(). */
890ce7af 830 const U8* const str = (U8*)STRING(prog->regstclass);
06b5626a 831 const int cl_l = (PL_regkind[(U8)OP(prog->regstclass)] == EXACT
1aa99e6b 832 ? CHR_DIST(str+STR_LEN(prog->regstclass), str)
66e933ab 833 : 1);
a3b680e6 834 const char * const endpos = (prog->anchored_substr || prog->anchored_utf8 || ml_anch)
1aa99e6b 835 ? HOP3c(s, (prog->minlen ? cl_l : 0), strend)
33b8afdf 836 : (prog->float_substr || prog->float_utf8
1aa99e6b
IH
837 ? HOP3c(HOP3c(check_at, -start_shift, strbeg),
838 cl_l, strend)
839 : strend);
6eb5f6b9
JH
840
841 t = s;
3b0527fe 842 s = find_byclass(prog, prog->regstclass, s, endpos, NULL);
6eb5f6b9
JH
843 if (!s) {
844#ifdef DEBUGGING
cbbf8932 845 const char *what = NULL;
6eb5f6b9
JH
846#endif
847 if (endpos == strend) {
a3621e74 848 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
6eb5f6b9
JH
849 "Could not match STCLASS...\n") );
850 goto fail;
851 }
a3621e74 852 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
66e933ab 853 "This position contradicts STCLASS...\n") );
653099ff
GS
854 if ((prog->reganch & ROPT_ANCH) && !ml_anch)
855 goto fail;
6eb5f6b9 856 /* Contradict one of substrings */
33b8afdf
JH
857 if (prog->anchored_substr || prog->anchored_utf8) {
858 if ((do_utf8 ? prog->anchored_utf8 : prog->anchored_substr) == check) {
a3621e74 859 DEBUG_EXECUTE_r( what = "anchored" );
6eb5f6b9 860 hop_and_restart:
1aa99e6b 861 s = HOP3c(t, 1, strend);
66e933ab
GS
862 if (s + start_shift + end_shift > strend) {
863 /* XXXX Should be taken into account earlier? */
a3621e74 864 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
66e933ab
GS
865 "Could not match STCLASS...\n") );
866 goto fail;
867 }
5e39e1e5
HS
868 if (!check)
869 goto giveup;
a3621e74 870 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
f5952150 871 "Looking for %s substr starting at offset %ld...\n",
6eb5f6b9
JH
872 what, (long)(s + start_shift - i_strpos)) );
873 goto restart;
874 }
66e933ab 875 /* Have both, check_string is floating */
6eb5f6b9
JH
876 if (t + start_shift >= check_at) /* Contradicts floating=check */
877 goto retry_floating_check;
878 /* Recheck anchored substring, but not floating... */
9041c2e3 879 s = check_at;
5e39e1e5
HS
880 if (!check)
881 goto giveup;
a3621e74 882 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
f5952150 883 "Looking for anchored substr starting at offset %ld...\n",
6eb5f6b9
JH
884 (long)(other_last - i_strpos)) );
885 goto do_other_anchored;
886 }
60e71179
GS
887 /* Another way we could have checked stclass at the
888 current position only: */
889 if (ml_anch) {
890 s = t = t + 1;
5e39e1e5
HS
891 if (!check)
892 goto giveup;
a3621e74 893 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
f5952150 894 "Looking for /%s^%s/m starting at offset %ld...\n",
e4584336 895 PL_colors[0], PL_colors[1], (long)(t - i_strpos)) );
60e71179 896 goto try_at_offset;
66e933ab 897 }
33b8afdf 898 if (!(do_utf8 ? prog->float_utf8 : prog->float_substr)) /* Could have been deleted */
60e71179 899 goto fail;
6eb5f6b9
JH
900 /* Check is floating subtring. */
901 retry_floating_check:
902 t = check_at - start_shift;
a3621e74 903 DEBUG_EXECUTE_r( what = "floating" );
6eb5f6b9
JH
904 goto hop_and_restart;
905 }
b7953727 906 if (t != s) {
a3621e74 907 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
6eb5f6b9 908 "By STCLASS: moving %ld --> %ld\n",
b7953727
JH
909 (long)(t - i_strpos), (long)(s - i_strpos))
910 );
911 }
912 else {
a3621e74 913 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
b7953727
JH
914 "Does not contradict STCLASS...\n");
915 );
916 }
6eb5f6b9 917 }
5e39e1e5 918 giveup:
a3621e74 919 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%s%s:%s match at offset %ld\n",
5e39e1e5
HS
920 PL_colors[4], (check ? "Guessed" : "Giving up"),
921 PL_colors[5], (long)(s - i_strpos)) );
cad2e5aa 922 return s;
2c2d71f5
JH
923
924 fail_finish: /* Substring not found */
33b8afdf
JH
925 if (prog->check_substr || prog->check_utf8) /* could be removed already */
926 BmUSEFUL(do_utf8 ? prog->check_utf8 : prog->check_substr) += 5; /* hooray */
cad2e5aa 927 fail:
a3621e74 928 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch rejected by optimizer%s\n",
e4584336 929 PL_colors[4], PL_colors[5]));
bd61b366 930 return NULL;
cad2e5aa 931}
9661b544 932
6eb5f6b9 933/* We know what class REx starts with. Try to find this position... */
3b0527fe
DM
934/* if reginfo is NULL, its a dryrun */
935
3c3eec57 936STATIC char *
3b0527fe
DM
937S_find_byclass(pTHX_ regexp * prog, const regnode *c, char *s, const char
938*strend, const regmatch_info *reginfo)
a687059c 939{
27da23d5 940 dVAR;
1df70142 941 const I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
6eb5f6b9 942 char *m;
d8093b23 943 STRLEN ln;
5dab1207 944 STRLEN lnc;
078c425b 945 register STRLEN uskip;
d8093b23 946 unsigned int c1;
947 unsigned int c2;
6eb5f6b9
JH
948 char *e;
949 register I32 tmp = 1; /* Scratch variable? */
a3b680e6 950 register const bool do_utf8 = PL_reg_match_utf8;
cad2e5aa 951
6eb5f6b9
JH
952 /* We know what class it must start with. */
953 switch (OP(c)) {
6eb5f6b9 954 case ANYOF:
388cc4de 955 if (do_utf8) {
078c425b 956 while (s + (uskip = UTF8SKIP(s)) <= strend) {
388cc4de
HS
957 if ((ANYOF_FLAGS(c) & ANYOF_UNICODE) ||
958 !UTF8_IS_INVARIANT((U8)s[0]) ?
32fc9b6a
DM
959 reginclass(prog, c, (U8*)s, 0, do_utf8) :
960 REGINCLASS(prog, c, (U8*)s)) {
3b0527fe 961 if (tmp && (!reginfo || regtry(reginfo, s)))
388cc4de
HS
962 goto got_it;
963 else
964 tmp = doevery;
965 }
966 else
967 tmp = 1;
078c425b 968 s += uskip;
388cc4de
HS
969 }
970 }
971 else {
972 while (s < strend) {
973 STRLEN skip = 1;
974
32fc9b6a 975 if (REGINCLASS(prog, c, (U8*)s) ||
388cc4de
HS
976 (ANYOF_FOLD_SHARP_S(c, s, strend) &&
977 /* The assignment of 2 is intentional:
978 * for the folded sharp s, the skip is 2. */
979 (skip = SHARP_S_SKIP))) {
3b0527fe 980 if (tmp && (!reginfo || regtry(reginfo, s)))
388cc4de
HS
981 goto got_it;
982 else
983 tmp = doevery;
984 }
985 else
986 tmp = 1;
987 s += skip;
988 }
a0d0e21e 989 }
6eb5f6b9 990 break;
f33976b4
DB
991 case CANY:
992 while (s < strend) {
3b0527fe 993 if (tmp && (!reginfo || regtry(reginfo, s)))
f33976b4
DB
994 goto got_it;
995 else
996 tmp = doevery;
997 s++;
998 }
999 break;
6eb5f6b9 1000 case EXACTF:
5dab1207
NIS
1001 m = STRING(c);
1002 ln = STR_LEN(c); /* length to match in octets/bytes */
1003 lnc = (I32) ln; /* length to match in characters */
1aa99e6b 1004 if (UTF) {
a2a2844f 1005 STRLEN ulen1, ulen2;
5dab1207 1006 U8 *sm = (U8 *) m;
89ebb4a3
JH
1007 U8 tmpbuf1[UTF8_MAXBYTES_CASE+1];
1008 U8 tmpbuf2[UTF8_MAXBYTES_CASE+1];
4ad0818d 1009 const U32 uniflags = UTF8_ALLOW_DEFAULT;
a2a2844f
JH
1010
1011 to_utf8_lower((U8*)m, tmpbuf1, &ulen1);
1012 to_utf8_upper((U8*)m, tmpbuf2, &ulen2);
1013
89ebb4a3 1014 c1 = utf8n_to_uvchr(tmpbuf1, UTF8_MAXBYTES_CASE,
041457d9 1015 0, uniflags);
89ebb4a3 1016 c2 = utf8n_to_uvchr(tmpbuf2, UTF8_MAXBYTES_CASE,
041457d9 1017 0, uniflags);
5dab1207
NIS
1018 lnc = 0;
1019 while (sm < ((U8 *) m + ln)) {
1020 lnc++;
1021 sm += UTF8SKIP(sm);
1022 }
1aa99e6b
IH
1023 }
1024 else {
1025 c1 = *(U8*)m;
1026 c2 = PL_fold[c1];
1027 }
6eb5f6b9
JH
1028 goto do_exactf;
1029 case EXACTFL:
5dab1207
NIS
1030 m = STRING(c);
1031 ln = STR_LEN(c);
1032 lnc = (I32) ln;
d8093b23 1033 c1 = *(U8*)m;
6eb5f6b9
JH
1034 c2 = PL_fold_locale[c1];
1035 do_exactf:
db12adc6 1036 e = HOP3c(strend, -((I32)lnc), s);
b3c9acc1 1037
3b0527fe 1038 if (!reginfo && e < s)
6eb5f6b9 1039 e = s; /* Due to minlen logic of intuit() */
1aa99e6b 1040
60a8b682
JH
1041 /* The idea in the EXACTF* cases is to first find the
1042 * first character of the EXACTF* node and then, if
1043 * necessary, case-insensitively compare the full
1044 * text of the node. The c1 and c2 are the first
1045 * characters (though in Unicode it gets a bit
1046 * more complicated because there are more cases
7f16dd3d
JH
1047 * than just upper and lower: one needs to use
1048 * the so-called folding case for case-insensitive
1049 * matching (called "loose matching" in Unicode).
1050 * ibcmp_utf8() will do just that. */
60a8b682 1051
1aa99e6b 1052 if (do_utf8) {
575cac57 1053 UV c, f;
89ebb4a3 1054 U8 tmpbuf [UTF8_MAXBYTES+1];
575cac57 1055 STRLEN len, foldlen;
4ad0818d 1056 const U32 uniflags = UTF8_ALLOW_DEFAULT;
09091399 1057 if (c1 == c2) {
5dab1207
NIS
1058 /* Upper and lower of 1st char are equal -
1059 * probably not a "letter". */
1aa99e6b 1060 while (s <= e) {
89ebb4a3 1061 c = utf8n_to_uvchr((U8*)s, UTF8_MAXBYTES, &len,
041457d9 1062 uniflags);
80aecb99
JH
1063 if ( c == c1
1064 && (ln == len ||
66423254 1065 ibcmp_utf8(s, (char **)0, 0, do_utf8,
eb160463 1066 m, (char **)0, ln, (bool)UTF))
3b0527fe 1067 && (!reginfo || regtry(reginfo, s)) )
1aa99e6b 1068 goto got_it;
80aecb99 1069 else {
1df70142 1070 U8 foldbuf[UTF8_MAXBYTES_CASE+1];
80aecb99 1071 uvchr_to_utf8(tmpbuf, c);
ac7e0132 1072 f = to_utf8_fold(tmpbuf, foldbuf, &foldlen);
80aecb99
JH
1073 if ( f != c
1074 && (f == c1 || f == c2)
1075 && (ln == foldlen ||
66423254
JH
1076 !ibcmp_utf8((char *) foldbuf,
1077 (char **)0, foldlen, do_utf8,
d07ddd77 1078 m,
eb160463 1079 (char **)0, ln, (bool)UTF))
3b0527fe 1080 && (!reginfo || regtry(reginfo, s)) )
80aecb99
JH
1081 goto got_it;
1082 }
1aa99e6b
IH
1083 s += len;
1084 }
09091399
JH
1085 }
1086 else {
1aa99e6b 1087 while (s <= e) {
89ebb4a3 1088 c = utf8n_to_uvchr((U8*)s, UTF8_MAXBYTES, &len,
041457d9 1089 uniflags);
80aecb99 1090
60a8b682 1091 /* Handle some of the three Greek sigmas cases.
8c01da3c
JH
1092 * Note that not all the possible combinations
1093 * are handled here: some of them are handled
1094 * by the standard folding rules, and some of
1095 * them (the character class or ANYOF cases)
1096 * are handled during compiletime in
1097 * regexec.c:S_regclass(). */
880bd946
JH
1098 if (c == (UV)UNICODE_GREEK_CAPITAL_LETTER_SIGMA ||
1099 c == (UV)UNICODE_GREEK_SMALL_LETTER_FINAL_SIGMA)
1100 c = (UV)UNICODE_GREEK_SMALL_LETTER_SIGMA;
80aecb99
JH
1101
1102 if ( (c == c1 || c == c2)
1103 && (ln == len ||
66423254 1104 ibcmp_utf8(s, (char **)0, 0, do_utf8,
eb160463 1105 m, (char **)0, ln, (bool)UTF))
3b0527fe 1106 && (!reginfo || regtry(reginfo, s)) )
1aa99e6b 1107 goto got_it;
80aecb99 1108 else {
1df70142 1109 U8 foldbuf[UTF8_MAXBYTES_CASE+1];
80aecb99 1110 uvchr_to_utf8(tmpbuf, c);
ac7e0132 1111 f = to_utf8_fold(tmpbuf, foldbuf, &foldlen);
80aecb99
JH
1112 if ( f != c
1113 && (f == c1 || f == c2)
1114 && (ln == foldlen ||
a6872d42 1115 !ibcmp_utf8((char *) foldbuf,
66423254 1116 (char **)0, foldlen, do_utf8,
d07ddd77 1117 m,
eb160463 1118 (char **)0, ln, (bool)UTF))
3b0527fe 1119 && (!reginfo || regtry(reginfo, s)) )
80aecb99
JH
1120 goto got_it;
1121 }
1aa99e6b
IH
1122 s += len;
1123 }
09091399 1124 }
1aa99e6b
IH
1125 }
1126 else {
1127 if (c1 == c2)
1128 while (s <= e) {
1129 if ( *(U8*)s == c1
1130 && (ln == 1 || !(OP(c) == EXACTF
1131 ? ibcmp(s, m, ln)
1132 : ibcmp_locale(s, m, ln)))
3b0527fe 1133 && (!reginfo || regtry(reginfo, s)) )
1aa99e6b
IH
1134 goto got_it;
1135 s++;
1136 }
1137 else
1138 while (s <= e) {
1139 if ( (*(U8*)s == c1 || *(U8*)s == c2)
1140 && (ln == 1 || !(OP(c) == EXACTF
1141 ? ibcmp(s, m, ln)
1142 : ibcmp_locale(s, m, ln)))
3b0527fe 1143 && (!reginfo || regtry(reginfo, s)) )
1aa99e6b
IH
1144 goto got_it;
1145 s++;
1146 }
b3c9acc1
IZ
1147 }
1148 break;
bbce6d69 1149 case BOUNDL:
3280af22 1150 PL_reg_flags |= RF_tainted;
bbce6d69 1151 /* FALL THROUGH */
a0d0e21e 1152 case BOUND:
ffc61ed2 1153 if (do_utf8) {
12d33761 1154 if (s == PL_bostr)
ffc61ed2
JH
1155 tmp = '\n';
1156 else {
6136c704 1157 U8 * const r = reghop3((U8*)s, -1, (U8*)PL_bostr);
4ad0818d 1158 tmp = utf8n_to_uvchr(r, UTF8SKIP(r), 0, UTF8_ALLOW_DEFAULT);
ffc61ed2
JH
1159 }
1160 tmp = ((OP(c) == BOUND ?
9041c2e3 1161 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1a4fad37 1162 LOAD_UTF8_CHARCLASS_ALNUM();
078c425b 1163 while (s + (uskip = UTF8SKIP(s)) <= strend) {
ffc61ed2 1164 if (tmp == !(OP(c) == BOUND ?
3568d838 1165 swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) :
ffc61ed2
JH
1166 isALNUM_LC_utf8((U8*)s)))
1167 {
1168 tmp = !tmp;
3b0527fe 1169 if ((!reginfo || regtry(reginfo, s)))
ffc61ed2
JH
1170 goto got_it;
1171 }
078c425b 1172 s += uskip;
a687059c 1173 }
a0d0e21e 1174 }
667bb95a 1175 else {
12d33761 1176 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
ffc61ed2
JH
1177 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1178 while (s < strend) {
1179 if (tmp ==
1180 !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
1181 tmp = !tmp;
3b0527fe 1182 if ((!reginfo || regtry(reginfo, s)))
ffc61ed2
JH
1183 goto got_it;
1184 }
1185 s++;
a0ed51b3 1186 }
a0ed51b3 1187 }
3b0527fe 1188 if ((!prog->minlen && tmp) && (!reginfo || regtry(reginfo, s)))
a0ed51b3
LW
1189 goto got_it;
1190 break;
bbce6d69 1191 case NBOUNDL:
3280af22 1192 PL_reg_flags |= RF_tainted;
bbce6d69 1193 /* FALL THROUGH */
a0d0e21e 1194 case NBOUND:
ffc61ed2 1195 if (do_utf8) {
12d33761 1196 if (s == PL_bostr)
ffc61ed2
JH
1197 tmp = '\n';
1198 else {
6136c704 1199 U8 * const r = reghop3((U8*)s, -1, (U8*)PL_bostr);
4ad0818d 1200 tmp = utf8n_to_uvchr(r, UTF8SKIP(r), 0, UTF8_ALLOW_DEFAULT);
ffc61ed2
JH
1201 }
1202 tmp = ((OP(c) == NBOUND ?
9041c2e3 1203 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1a4fad37 1204 LOAD_UTF8_CHARCLASS_ALNUM();
078c425b 1205 while (s + (uskip = UTF8SKIP(s)) <= strend) {
ffc61ed2 1206 if (tmp == !(OP(c) == NBOUND ?
3568d838 1207 swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) :
ffc61ed2
JH
1208 isALNUM_LC_utf8((U8*)s)))
1209 tmp = !tmp;
3b0527fe 1210 else if ((!reginfo || regtry(reginfo, s)))
ffc61ed2 1211 goto got_it;
078c425b 1212 s += uskip;
ffc61ed2 1213 }
a0d0e21e 1214 }
667bb95a 1215 else {
12d33761 1216 tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n';
ffc61ed2
JH
1217 tmp = ((OP(c) == NBOUND ?
1218 isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1219 while (s < strend) {
1220 if (tmp ==
1221 !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
1222 tmp = !tmp;
3b0527fe 1223 else if ((!reginfo || regtry(reginfo, s)))
ffc61ed2
JH
1224 goto got_it;
1225 s++;
1226 }
a0ed51b3 1227 }
3b0527fe 1228 if ((!prog->minlen && !tmp) && (!reginfo || regtry(reginfo, s)))
a0ed51b3
LW
1229 goto got_it;
1230 break;
a0d0e21e 1231 case ALNUM:
ffc61ed2 1232 if (do_utf8) {
1a4fad37 1233 LOAD_UTF8_CHARCLASS_ALNUM();
078c425b 1234 while (s + (uskip = UTF8SKIP(s)) <= strend) {
3568d838 1235 if (swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) {
3b0527fe 1236 if (tmp && (!reginfo || regtry(reginfo, s)))
ffc61ed2
JH
1237 goto got_it;
1238 else
1239 tmp = doevery;
1240 }
bbce6d69 1241 else
ffc61ed2 1242 tmp = 1;
078c425b 1243 s += uskip;
bbce6d69 1244 }
bbce6d69 1245 }
ffc61ed2
JH
1246 else {
1247 while (s < strend) {
1248 if (isALNUM(*s)) {
3b0527fe 1249 if (tmp && (!reginfo || regtry(reginfo, s)))
ffc61ed2
JH
1250 goto got_it;
1251 else
1252 tmp = doevery;
1253 }
a0ed51b3 1254 else
ffc61ed2
JH
1255 tmp = 1;
1256 s++;
a0ed51b3 1257 }
a0ed51b3
LW
1258 }
1259 break;
bbce6d69 1260 case ALNUML:
3280af22 1261 PL_reg_flags |= RF_tainted;
ffc61ed2 1262 if (do_utf8) {
078c425b 1263 while (s + (uskip = UTF8SKIP(s)) <= strend) {
ffc61ed2 1264 if (isALNUM_LC_utf8((U8*)s)) {
3b0527fe 1265 if (tmp && (!reginfo || regtry(reginfo, s)))
ffc61ed2
JH
1266 goto got_it;
1267 else
1268 tmp = doevery;
1269 }
a687059c 1270 else
ffc61ed2 1271 tmp = 1;
078c425b 1272 s += uskip;
a0d0e21e 1273 }
a0d0e21e 1274 }
ffc61ed2
JH
1275 else {
1276 while (s < strend) {
1277 if (isALNUM_LC(*s)) {
3b0527fe 1278 if (tmp && (!reginfo || regtry(reginfo, s)))
ffc61ed2
JH
1279 goto got_it;
1280 else
1281 tmp = doevery;
1282 }
a0ed51b3 1283 else
ffc61ed2
JH
1284 tmp = 1;
1285 s++;
a0ed51b3 1286 }
a0ed51b3
LW
1287 }
1288 break;
a0d0e21e 1289 case NALNUM:
ffc61ed2 1290 if (do_utf8) {
1a4fad37 1291 LOAD_UTF8_CHARCLASS_ALNUM();
078c425b 1292 while (s + (uskip = UTF8SKIP(s)) <= strend) {
3568d838 1293 if (!swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) {
3b0527fe 1294 if (tmp && (!reginfo || regtry(reginfo, s)))
ffc61ed2
JH
1295 goto got_it;
1296 else
1297 tmp = doevery;
1298 }
bbce6d69 1299 else
ffc61ed2 1300 tmp = 1;
078c425b 1301 s += uskip;
bbce6d69 1302 }
bbce6d69 1303 }
ffc61ed2
JH
1304 else {
1305 while (s < strend) {
1306 if (!isALNUM(*s)) {
3b0527fe 1307 if (tmp && (!reginfo || regtry(reginfo, s)))
ffc61ed2
JH
1308 goto got_it;
1309 else
1310 tmp = doevery;
1311 }
a0ed51b3 1312 else
ffc61ed2
JH
1313 tmp = 1;
1314 s++;
a0ed51b3 1315 }
a0ed51b3
LW
1316 }
1317 break;
bbce6d69 1318 case NALNUML:
3280af22 1319 PL_reg_flags |= RF_tainted;
ffc61ed2 1320 if (do_utf8) {
078c425b 1321 while (s + (uskip = UTF8SKIP(s)) <= strend) {
ffc61ed2 1322 if (!isALNUM_LC_utf8((U8*)s)) {
3b0527fe 1323 if (tmp && (!reginfo || regtry(reginfo, s)))
ffc61ed2
JH
1324 goto got_it;
1325 else
1326 tmp = doevery;
1327 }
a687059c 1328 else
ffc61ed2 1329 tmp = 1;
078c425b 1330 s += uskip;
a687059c 1331 }
a0d0e21e 1332 }
ffc61ed2
JH
1333 else {
1334 while (s < strend) {
1335 if (!isALNUM_LC(*s)) {
3b0527fe 1336 if (tmp && (!reginfo || regtry(reginfo, s)))
ffc61ed2
JH
1337 goto got_it;
1338 else
1339 tmp = doevery;
1340 }
a0ed51b3 1341 else
ffc61ed2
JH
1342 tmp = 1;
1343 s++;
a0ed51b3 1344 }
a0ed51b3
LW
1345 }
1346 break;
a0d0e21e 1347 case SPACE:
ffc61ed2 1348 if (do_utf8) {
1a4fad37 1349 LOAD_UTF8_CHARCLASS_SPACE();
078c425b 1350 while (s + (uskip = UTF8SKIP(s)) <= strend) {
3568d838 1351 if (*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, do_utf8)) {
3b0527fe 1352 if (tmp && (!reginfo || regtry(reginfo, s)))
ffc61ed2
JH
1353 goto got_it;
1354 else
1355 tmp = doevery;
1356 }
a0d0e21e 1357 else
ffc61ed2 1358 tmp = 1;
078c425b 1359 s += uskip;
2304df62 1360 }
a0d0e21e 1361 }
ffc61ed2
JH
1362 else {
1363 while (s < strend) {
1364 if (isSPACE(*s)) {
3b0527fe 1365 if (tmp && (!reginfo || regtry(reginfo, s)))
ffc61ed2
JH
1366 goto got_it;
1367 else
1368 tmp = doevery;
1369 }
a0ed51b3 1370 else
ffc61ed2
JH
1371 tmp = 1;
1372 s++;
a0ed51b3 1373 }
a0ed51b3
LW
1374 }
1375 break;
bbce6d69 1376 case SPACEL:
3280af22 1377 PL_reg_flags |= RF_tainted;
ffc61ed2 1378 if (do_utf8) {
078c425b 1379 while (s + (uskip = UTF8SKIP(s)) <= strend) {
ffc61ed2 1380 if (*s == ' ' || isSPACE_LC_utf8((U8*)s)) {
3b0527fe 1381 if (tmp && (!reginfo || regtry(reginfo, s)))
ffc61ed2
JH
1382 goto got_it;
1383 else
1384 tmp = doevery;
1385 }
bbce6d69 1386 else
ffc61ed2 1387 tmp = 1;
078c425b 1388 s += uskip;
bbce6d69 1389 }
bbce6d69 1390 }
ffc61ed2
JH
1391 else {
1392 while (s < strend) {
1393 if (isSPACE_LC(*s)) {
3b0527fe 1394 if (tmp && (!reginfo || regtry(reginfo, s)))
ffc61ed2
JH
1395 goto got_it;
1396 else
1397 tmp = doevery;
1398 }
a0ed51b3 1399 else
ffc61ed2
JH
1400 tmp = 1;
1401 s++;
a0ed51b3 1402 }
a0ed51b3
LW
1403 }
1404 break;
a0d0e21e 1405 case NSPACE:
ffc61ed2 1406 if (do_utf8) {
1a4fad37 1407 LOAD_UTF8_CHARCLASS_SPACE();
078c425b 1408 while (s + (uskip = UTF8SKIP(s)) <= strend) {
3568d838 1409 if (!(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, do_utf8))) {
3b0527fe 1410 if (tmp && (!reginfo || regtry(reginfo, s)))
ffc61ed2
JH
1411 goto got_it;
1412 else
1413 tmp = doevery;
1414 }
a0d0e21e 1415 else
ffc61ed2 1416 tmp = 1;
078c425b 1417 s += uskip;
a687059c 1418 }
a0d0e21e 1419 }
ffc61ed2
JH
1420 else {
1421 while (s < strend) {
1422 if (!isSPACE(*s)) {
3b0527fe 1423 if (tmp && (!reginfo || regtry(reginfo, s)))
ffc61ed2
JH
1424 goto got_it;
1425 else
1426 tmp = doevery;
1427 }
a0ed51b3 1428 else
ffc61ed2
JH
1429 tmp = 1;
1430 s++;
a0ed51b3 1431 }
a0ed51b3
LW
1432 }
1433 break;
bbce6d69 1434 case NSPACEL:
3280af22 1435 PL_reg_flags |= RF_tainted;
ffc61ed2 1436 if (do_utf8) {
078c425b 1437 while (s + (uskip = UTF8SKIP(s)) <= strend) {
ffc61ed2 1438 if (!(*s == ' ' || isSPACE_LC_utf8((U8*)s))) {
3b0527fe 1439 if (tmp && (!reginfo || regtry(reginfo, s)))
ffc61ed2
JH
1440 goto got_it;
1441 else
1442 tmp = doevery;
1443 }
bbce6d69 1444 else
ffc61ed2 1445 tmp = 1;
078c425b 1446 s += uskip;
bbce6d69 1447 }
bbce6d69 1448 }
ffc61ed2
JH
1449 else {
1450 while (s < strend) {
1451 if (!isSPACE_LC(*s)) {
3b0527fe 1452 if (tmp && (!reginfo || regtry(reginfo, s)))
ffc61ed2
JH
1453 goto got_it;
1454 else
1455 tmp = doevery;
1456 }
a0ed51b3 1457 else
ffc61ed2
JH
1458 tmp = 1;
1459 s++;
a0ed51b3 1460 }
a0ed51b3
LW
1461 }
1462 break;
a0d0e21e 1463 case DIGIT:
ffc61ed2 1464 if (do_utf8) {
1a4fad37 1465 LOAD_UTF8_CHARCLASS_DIGIT();
078c425b 1466 while (s + (uskip = UTF8SKIP(s)) <= strend) {
3568d838 1467 if (swash_fetch(PL_utf8_digit,(U8*)s, do_utf8)) {
3b0527fe 1468 if (tmp && (!reginfo || regtry(reginfo, s)))
ffc61ed2
JH
1469 goto got_it;
1470 else
1471 tmp = doevery;
1472 }
a0d0e21e 1473 else
ffc61ed2 1474 tmp = 1;
078c425b 1475 s += uskip;
2b69d0c2 1476 }
a0d0e21e 1477 }
ffc61ed2
JH
1478 else {
1479 while (s < strend) {
1480 if (isDIGIT(*s)) {
3b0527fe 1481 if (tmp && (!reginfo || regtry(reginfo, s)))
ffc61ed2
JH
1482 goto got_it;
1483 else
1484 tmp = doevery;
1485 }
a0ed51b3 1486 else
ffc61ed2
JH
1487 tmp = 1;
1488 s++;
a0ed51b3 1489 }
a0ed51b3
LW
1490 }
1491 break;
b8c5462f
JH
1492 case DIGITL:
1493 PL_reg_flags |= RF_tainted;
ffc61ed2 1494 if (do_utf8) {
078c425b 1495 while (s + (uskip = UTF8SKIP(s)) <= strend) {
ffc61ed2 1496 if (isDIGIT_LC_utf8((U8*)s)) {
3b0527fe 1497 if (tmp && (!reginfo || regtry(reginfo, s)))
ffc61ed2
JH
1498 goto got_it;
1499 else
1500 tmp = doevery;
1501 }
b8c5462f 1502 else
ffc61ed2 1503 tmp = 1;
078c425b 1504 s += uskip;
b8c5462f 1505 }
b8c5462f 1506 }
ffc61ed2
JH
1507 else {
1508 while (s < strend) {
1509 if (isDIGIT_LC(*s)) {
3b0527fe 1510 if (tmp && (!reginfo || regtry(reginfo, s)))
ffc61ed2
JH
1511 goto got_it;
1512 else
1513 tmp = doevery;
1514 }
b8c5462f 1515 else
ffc61ed2
JH
1516 tmp = 1;
1517 s++;
b8c5462f 1518 }
b8c5462f
JH
1519 }
1520 break;
a0d0e21e 1521 case NDIGIT:
ffc61ed2 1522 if (do_utf8) {
1a4fad37 1523 LOAD_UTF8_CHARCLASS_DIGIT();
078c425b 1524 while (s + (uskip = UTF8SKIP(s)) <= strend) {
3568d838 1525 if (!swash_fetch(PL_utf8_digit,(U8*)s, do_utf8)) {
3b0527fe 1526 if (tmp && (!reginfo || regtry(reginfo, s)))
ffc61ed2
JH
1527 goto got_it;
1528 else
1529 tmp = doevery;
1530 }
a0d0e21e 1531 else
ffc61ed2 1532 tmp = 1;
078c425b 1533 s += uskip;
a687059c 1534 }
a0d0e21e 1535 }
ffc61ed2
JH
1536 else {
1537 while (s < strend) {
1538 if (!isDIGIT(*s)) {
3b0527fe 1539 if (tmp && (!reginfo || regtry(reginfo, s)))
ffc61ed2
JH
1540 goto got_it;
1541 else
1542 tmp = doevery;
1543 }
a0ed51b3 1544 else
ffc61ed2
JH
1545 tmp = 1;
1546 s++;
a0ed51b3 1547 }
a0ed51b3
LW
1548 }
1549 break;
b8c5462f
JH
1550 case NDIGITL:
1551 PL_reg_flags |= RF_tainted;
ffc61ed2 1552 if (do_utf8) {
078c425b 1553 while (s + (uskip = UTF8SKIP(s)) <= strend) {
ffc61ed2 1554 if (!isDIGIT_LC_utf8((U8*)s)) {
3b0527fe 1555 if (tmp && (!reginfo || regtry(reginfo, s)))
ffc61ed2
JH
1556 goto got_it;
1557 else
1558 tmp = doevery;
1559 }
b8c5462f 1560 else
ffc61ed2 1561 tmp = 1;
078c425b 1562 s += uskip;
b8c5462f 1563 }
a0ed51b3 1564 }
ffc61ed2
JH
1565 else {
1566 while (s < strend) {
1567 if (!isDIGIT_LC(*s)) {
3b0527fe 1568 if (tmp && (!reginfo || regtry(reginfo, s)))
ffc61ed2
JH
1569 goto got_it;
1570 else
1571 tmp = doevery;
1572 }
cf93c79d 1573 else
ffc61ed2
JH
1574 tmp = 1;
1575 s++;
b8c5462f 1576 }
b8c5462f
JH
1577 }
1578 break;
b3c9acc1 1579 default:
3c3eec57
GS
1580 Perl_croak(aTHX_ "panic: unknown regstclass %d", (int)OP(c));
1581 break;
d6a28714 1582 }
6eb5f6b9
JH
1583 return 0;
1584 got_it:
1585 return s;
1586}
1587
1588/*
1589 - regexec_flags - match a regexp against a string
1590 */
1591I32
1592Perl_regexec_flags(pTHX_ register regexp *prog, char *stringarg, register char *strend,
1593 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
1594/* strend: pointer to null at end of string */
1595/* strbeg: real beginning of string */
1596/* minend: end of match must be >=minend after stringarg. */
1597/* data: May be used for some additional optimizations. */
1598/* nosave: For optimizations. */
1599{
97aff369 1600 dVAR;
6eb5f6b9
JH
1601 register char *s;
1602 register regnode *c;
1603 register char *startpos = stringarg;
6eb5f6b9
JH
1604 I32 minlen; /* must match at least this many chars */
1605 I32 dontbother = 0; /* how many characters not to try at end */
6eb5f6b9
JH
1606 I32 end_shift = 0; /* Same for the end. */ /* CC */
1607 I32 scream_pos = -1; /* Internal iterator of scream. */
ccac19ea 1608 char *scream_olds = NULL;
6eb5f6b9 1609 SV* oreplsv = GvSV(PL_replgv);
1df70142 1610 const bool do_utf8 = DO_UTF8(sv);
2757e526 1611 I32 multiline;
2a782b5b 1612#ifdef DEBUGGING
2757e526
JH
1613 SV* dsv0;
1614 SV* dsv1;
2a782b5b 1615#endif
3b0527fe 1616 regmatch_info reginfo; /* create some info to pass to regtry etc */
a3621e74
YO
1617
1618 GET_RE_DEBUG_FLAGS_DECL;
1619
9d4ba2ae 1620 PERL_UNUSED_ARG(data);
6eb5f6b9
JH
1621
1622 /* Be paranoid... */
1623 if (prog == NULL || startpos == NULL) {
1624 Perl_croak(aTHX_ "NULL regexp parameter");
1625 return 0;
1626 }
1627
2757e526 1628 multiline = prog->reganch & PMf_MULTILINE;
3b0527fe 1629 reginfo.prog = prog;
2757e526
JH
1630
1631#ifdef DEBUGGING
1632 dsv0 = PERL_DEBUG_PAD_ZERO(0);
1633 dsv1 = PERL_DEBUG_PAD_ZERO(1);
1634#endif
1635
bac06658
JH
1636 RX_MATCH_UTF8_set(prog, do_utf8);
1637
6eb5f6b9 1638 minlen = prog->minlen;
61a36c01 1639 if (strend - startpos < minlen) {
a3621e74 1640 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
a72c7584
JH
1641 "String too short [regexec_flags]...\n"));
1642 goto phooey;
1aa99e6b 1643 }
6eb5f6b9 1644
6eb5f6b9
JH
1645 /* Check validity of program. */
1646 if (UCHARAT(prog->program) != REG_MAGIC) {
1647 Perl_croak(aTHX_ "corrupted regexp program");
1648 }
1649
1650 PL_reg_flags = 0;
1651 PL_reg_eval_set = 0;
1652 PL_reg_maxiter = 0;
1653
1654 if (prog->reganch & ROPT_UTF8)
1655 PL_reg_flags |= RF_utf8;
1656
1657 /* Mark beginning of line for ^ and lookbehind. */
3b0527fe 1658 reginfo.bol = startpos; /* XXX not used ??? */
6eb5f6b9 1659 PL_bostr = strbeg;
3b0527fe 1660 reginfo.sv = sv;
6eb5f6b9
JH
1661
1662 /* Mark end of line for $ (and such) */
1663 PL_regeol = strend;
1664
1665 /* see how far we have to get to not match where we matched before */
3b0527fe 1666 reginfo.till = startpos+minend;
6eb5f6b9 1667
6eb5f6b9
JH
1668 /* If there is a "must appear" string, look for it. */
1669 s = startpos;
1670
3b0527fe 1671 if (prog->reganch & ROPT_GPOS_SEEN) { /* Need to set reginfo->ganch */
6eb5f6b9
JH
1672 MAGIC *mg;
1673
1674 if (flags & REXEC_IGNOREPOS) /* Means: check only at start */
3b0527fe 1675 reginfo.ganch = startpos;
6eb5f6b9
JH
1676 else if (sv && SvTYPE(sv) >= SVt_PVMG
1677 && SvMAGIC(sv)
14befaf4
DM
1678 && (mg = mg_find(sv, PERL_MAGIC_regex_global))
1679 && mg->mg_len >= 0) {
3b0527fe 1680 reginfo.ganch = strbeg + mg->mg_len; /* Defined pos() */
6eb5f6b9 1681 if (prog->reganch & ROPT_ANCH_GPOS) {
3b0527fe 1682 if (s > reginfo.ganch)
6eb5f6b9 1683 goto phooey;
3b0527fe 1684 s = reginfo.ganch;
6eb5f6b9
JH
1685 }
1686 }
1687 else /* pos() not defined */
3b0527fe 1688 reginfo.ganch = strbeg;
6eb5f6b9
JH
1689 }
1690
a0714e2c 1691 if (!(flags & REXEC_CHECKED) && (prog->check_substr != NULL || prog->check_utf8 != NULL)) {
6eb5f6b9
JH
1692 re_scream_pos_data d;
1693
1694 d.scream_olds = &scream_olds;
1695 d.scream_pos = &scream_pos;
1696 s = re_intuit_start(prog, sv, s, strend, flags, &d);
3fa9c3d7 1697 if (!s) {
a3621e74 1698 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Not present...\n"));
6eb5f6b9 1699 goto phooey; /* not present */
3fa9c3d7 1700 }
6eb5f6b9
JH
1701 }
1702
a3621e74 1703 DEBUG_EXECUTE_r({
1df70142
AL
1704 const char * const s0 = UTF
1705 ? pv_uni_display(dsv0, (U8*)prog->precomp, prog->prelen, 60,
1706 UNI_DISPLAY_REGEX)
1707 : prog->precomp;
1708 const int len0 = UTF ? SvCUR(dsv0) : prog->prelen;
1709 const char * const s1 = do_utf8 ? sv_uni_display(dsv1, sv, 60,
c728cb41 1710 UNI_DISPLAY_REGEX) : startpos;
1df70142 1711 const int len1 = do_utf8 ? SvCUR(dsv1) : strend - startpos;
2a782b5b
JH
1712 if (!PL_colorset)
1713 reginitcolors();
1714 PerlIO_printf(Perl_debug_log,
a0288114 1715 "%sMatching REx%s \"%s%*.*s%s%s\" against \"%s%.*s%s%s\"\n",
e4584336 1716 PL_colors[4], PL_colors[5], PL_colors[0],
9e55ce06 1717 len0, len0, s0,
2a782b5b 1718 PL_colors[1],
9e55ce06 1719 len0 > 60 ? "..." : "",
2a782b5b 1720 PL_colors[0],
9e55ce06
JH
1721 (int)(len1 > 60 ? 60 : len1),
1722 s1, PL_colors[1],
1723 (len1 > 60 ? "..." : "")
2a782b5b
JH
1724 );
1725 });
6eb5f6b9
JH
1726
1727 /* Simplest case: anchored match need be tried only once. */
1728 /* [unless only anchor is BOL and multiline is set] */
1729 if (prog->reganch & (ROPT_ANCH & ~ROPT_ANCH_GPOS)) {
3b0527fe 1730 if (s == startpos && regtry(&reginfo, startpos))
6eb5f6b9 1731 goto got_it;
7fba1cd6 1732 else if (multiline || (prog->reganch & ROPT_IMPLICIT)
6eb5f6b9
JH
1733 || (prog->reganch & ROPT_ANCH_MBOL)) /* XXXX SBOL? */
1734 {
1735 char *end;
1736
1737 if (minlen)
1738 dontbother = minlen - 1;
1aa99e6b 1739 end = HOP3c(strend, -dontbother, strbeg) - 1;
6eb5f6b9 1740 /* for multiline we only have to try after newlines */
33b8afdf 1741 if (prog->check_substr || prog->check_utf8) {
6eb5f6b9
JH
1742 if (s == startpos)
1743 goto after_try;
1744 while (1) {
3b0527fe 1745 if (regtry(&reginfo, s))
6eb5f6b9
JH
1746 goto got_it;
1747 after_try:
1748 if (s >= end)
1749 goto phooey;
1750 if (prog->reganch & RE_USE_INTUIT) {
1751 s = re_intuit_start(prog, sv, s + 1, strend, flags, NULL);
1752 if (!s)
1753 goto phooey;
1754 }
1755 else
1756 s++;
1757 }
1758 } else {
1759 if (s > startpos)
1760 s--;
1761 while (s < end) {
1762 if (*s++ == '\n') { /* don't need PL_utf8skip here */
3b0527fe 1763 if (regtry(&reginfo, s))
6eb5f6b9
JH
1764 goto got_it;
1765 }
1766 }
1767 }
1768 }
1769 goto phooey;
1770 } else if (prog->reganch & ROPT_ANCH_GPOS) {
3b0527fe 1771 if (regtry(&reginfo, reginfo.ganch))
6eb5f6b9
JH
1772 goto got_it;
1773 goto phooey;
1774 }
1775
1776 /* Messy cases: unanchored match. */
33b8afdf 1777 if ((prog->anchored_substr || prog->anchored_utf8) && prog->reganch & ROPT_SKIP) {
6eb5f6b9
JH
1778 /* we have /x+whatever/ */
1779 /* it must be a one character string (XXXX Except UTF?) */
33b8afdf 1780 char ch;
bf93d4cc
GS
1781#ifdef DEBUGGING
1782 int did_match = 0;
1783#endif
33b8afdf
JH
1784 if (!(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr))
1785 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
3f7c398e 1786 ch = SvPVX_const(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr)[0];
bf93d4cc 1787
1aa99e6b 1788 if (do_utf8) {
6eb5f6b9
JH
1789 while (s < strend) {
1790 if (*s == ch) {
a3621e74 1791 DEBUG_EXECUTE_r( did_match = 1 );
3b0527fe 1792 if (regtry(&reginfo, s)) goto got_it;
6eb5f6b9
JH
1793 s += UTF8SKIP(s);
1794 while (s < strend && *s == ch)
1795 s += UTF8SKIP(s);
1796 }
1797 s += UTF8SKIP(s);
1798 }
1799 }
1800 else {
1801 while (s < strend) {
1802 if (*s == ch) {
a3621e74 1803 DEBUG_EXECUTE_r( did_match = 1 );
3b0527fe 1804 if (regtry(&reginfo, s)) goto got_it;
6eb5f6b9
JH
1805 s++;
1806 while (s < strend && *s == ch)
1807 s++;
1808 }
1809 s++;
1810 }
1811 }
a3621e74 1812 DEBUG_EXECUTE_r(if (!did_match)
bf93d4cc 1813 PerlIO_printf(Perl_debug_log,
b7953727
JH
1814 "Did not find anchored character...\n")
1815 );
6eb5f6b9 1816 }
a0714e2c
SS
1817 else if (prog->anchored_substr != NULL
1818 || prog->anchored_utf8 != NULL
1819 || ((prog->float_substr != NULL || prog->float_utf8 != NULL)
33b8afdf
JH
1820 && prog->float_max_offset < strend - s)) {
1821 SV *must;
1822 I32 back_max;
1823 I32 back_min;
1824 char *last;
6eb5f6b9 1825 char *last1; /* Last position checked before */
bf93d4cc
GS
1826#ifdef DEBUGGING
1827 int did_match = 0;
1828#endif
33b8afdf
JH
1829 if (prog->anchored_substr || prog->anchored_utf8) {
1830 if (!(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr))
1831 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1832 must = do_utf8 ? prog->anchored_utf8 : prog->anchored_substr;
1833 back_max = back_min = prog->anchored_offset;
1834 } else {
1835 if (!(do_utf8 ? prog->float_utf8 : prog->float_substr))
1836 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1837 must = do_utf8 ? prog->float_utf8 : prog->float_substr;
1838 back_max = prog->float_max_offset;
1839 back_min = prog->float_min_offset;
1840 }
1841 if (must == &PL_sv_undef)
1842 /* could not downgrade utf8 check substring, so must fail */
1843 goto phooey;
1844
1845 last = HOP3c(strend, /* Cannot start after this */
1846 -(I32)(CHR_SVLEN(must)
1847 - (SvTAIL(must) != 0) + back_min), strbeg);
6eb5f6b9
JH
1848
1849 if (s > PL_bostr)
1850 last1 = HOPc(s, -1);
1851 else
1852 last1 = s - 1; /* bogus */
1853
a0288114 1854 /* XXXX check_substr already used to find "s", can optimize if
6eb5f6b9
JH
1855 check_substr==must. */
1856 scream_pos = -1;
1857 dontbother = end_shift;
1858 strend = HOPc(strend, -dontbother);
1859 while ( (s <= last) &&
9041c2e3 1860 ((flags & REXEC_SCREAM)
1aa99e6b 1861 ? (s = screaminstr(sv, must, HOP3c(s, back_min, strend) - strbeg,
6eb5f6b9 1862 end_shift, &scream_pos, 0))
1aa99e6b 1863 : (s = fbm_instr((unsigned char*)HOP3(s, back_min, strend),
9041c2e3 1864 (unsigned char*)strend, must,
7fba1cd6 1865 multiline ? FBMrf_MULTILINE : 0))) ) {
4addbd3b
HS
1866 /* we may be pointing at the wrong string */
1867 if ((flags & REXEC_SCREAM) && RX_MATCH_COPIED(prog))
3f7c398e 1868 s = strbeg + (s - SvPVX_const(sv));
a3621e74 1869 DEBUG_EXECUTE_r( did_match = 1 );
6eb5f6b9
JH
1870 if (HOPc(s, -back_max) > last1) {
1871 last1 = HOPc(s, -back_min);
1872 s = HOPc(s, -back_max);
1873 }
1874 else {
52657f30 1875 char * const t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
6eb5f6b9
JH
1876
1877 last1 = HOPc(s, -back_min);
52657f30 1878 s = t;
6eb5f6b9 1879 }
1aa99e6b 1880 if (do_utf8) {
6eb5f6b9 1881 while (s <= last1) {
3b0527fe 1882 if (regtry(&reginfo, s))
6eb5f6b9
JH
1883 goto got_it;
1884 s += UTF8SKIP(s);
1885 }
1886 }
1887 else {
1888 while (s <= last1) {
3b0527fe 1889 if (regtry(&reginfo, s))
6eb5f6b9
JH
1890 goto got_it;
1891 s++;
1892 }
1893 }
1894 }
a3621e74 1895 DEBUG_EXECUTE_r(if (!did_match)
b7953727 1896 PerlIO_printf(Perl_debug_log,
a0288114 1897 "Did not find %s substr \"%s%.*s%s\"%s...\n",
33b8afdf 1898 ((must == prog->anchored_substr || must == prog->anchored_utf8)
bf93d4cc
GS
1899 ? "anchored" : "floating"),
1900 PL_colors[0],
1901 (int)(SvCUR(must) - (SvTAIL(must)!=0)),
3f7c398e 1902 SvPVX_const(must),
b7953727
JH
1903 PL_colors[1], (SvTAIL(must) ? "$" : ""))
1904 );
6eb5f6b9
JH
1905 goto phooey;
1906 }
155aba94 1907 else if ((c = prog->regstclass)) {
f14c76ed
RGS
1908 if (minlen) {
1909 I32 op = (U8)OP(prog->regstclass);
66e933ab 1910 /* don't bother with what can't match */
f14c76ed
RGS
1911 if (PL_regkind[op] != EXACT && op != CANY)
1912 strend = HOPc(strend, -(minlen - 1));
1913 }
a3621e74 1914 DEBUG_EXECUTE_r({
ffc61ed2 1915 SV *prop = sv_newmortal();
cfd0369c
NC
1916 const char *s0;
1917 const char *s1;
9e55ce06
JH
1918 int len0;
1919 int len1;
1920
32fc9b6a 1921 regprop(prog, prop, c);
9e55ce06 1922 s0 = UTF ?
3f7c398e 1923 pv_uni_display(dsv0, (U8*)SvPVX_const(prop), SvCUR(prop), 60,
c728cb41 1924 UNI_DISPLAY_REGEX) :
cfd0369c 1925 SvPVX_const(prop);
9e55ce06
JH
1926 len0 = UTF ? SvCUR(dsv0) : SvCUR(prop);
1927 s1 = UTF ?
c728cb41 1928 sv_uni_display(dsv1, sv, 60, UNI_DISPLAY_REGEX) : s;
9e55ce06
JH
1929 len1 = UTF ? SvCUR(dsv1) : strend - s;
1930 PerlIO_printf(Perl_debug_log,
a0288114 1931 "Matching stclass \"%*.*s\" against \"%*.*s\"\n",
9e55ce06
JH
1932 len0, len0, s0,
1933 len1, len1, s1);
ffc61ed2 1934 });
3b0527fe 1935 if (find_byclass(prog, c, s, strend, &reginfo))
6eb5f6b9 1936 goto got_it;
a3621e74 1937 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Contradicts stclass...\n"));
d6a28714
JH
1938 }
1939 else {
1940 dontbother = 0;
a0714e2c 1941 if (prog->float_substr != NULL || prog->float_utf8 != NULL) {
33b8afdf 1942 /* Trim the end. */
d6a28714 1943 char *last;
33b8afdf
JH
1944 SV* float_real;
1945
1946 if (!(do_utf8 ? prog->float_utf8 : prog->float_substr))
1947 do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog);
1948 float_real = do_utf8 ? prog->float_utf8 : prog->float_substr;
d6a28714
JH
1949
1950 if (flags & REXEC_SCREAM) {
33b8afdf 1951 last = screaminstr(sv, float_real, s - strbeg,
d6a28714
JH
1952 end_shift, &scream_pos, 1); /* last one */
1953 if (!last)
ffc61ed2 1954 last = scream_olds; /* Only one occurrence. */
4addbd3b
HS
1955 /* we may be pointing at the wrong string */
1956 else if (RX_MATCH_COPIED(prog))
3f7c398e 1957 s = strbeg + (s - SvPVX_const(sv));
b8c5462f 1958 }
d6a28714
JH
1959 else {
1960 STRLEN len;
cfd0369c 1961 const char * const little = SvPV_const(float_real, len);
d6a28714 1962
33b8afdf 1963 if (SvTAIL(float_real)) {
d6a28714
JH
1964 if (memEQ(strend - len + 1, little, len - 1))
1965 last = strend - len + 1;
7fba1cd6 1966 else if (!multiline)
9041c2e3 1967 last = memEQ(strend - len, little, len)
bd61b366 1968 ? strend - len : NULL;
b8c5462f 1969 else
d6a28714
JH
1970 goto find_last;
1971 } else {
1972 find_last:
9041c2e3 1973 if (len)
d6a28714 1974 last = rninstr(s, strend, little, little + len);
b8c5462f 1975 else
a0288114 1976 last = strend; /* matching "$" */
b8c5462f 1977 }
b8c5462f 1978 }
bf93d4cc 1979 if (last == NULL) {
a3621e74 1980 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
bf93d4cc 1981 "%sCan't trim the tail, match fails (should not happen)%s\n",
e4584336 1982 PL_colors[4], PL_colors[5]));
bf93d4cc
GS
1983 goto phooey; /* Should not happen! */
1984 }
d6a28714
JH
1985 dontbother = strend - last + prog->float_min_offset;
1986 }
1987 if (minlen && (dontbother < minlen))
1988 dontbother = minlen - 1;
1989 strend -= dontbother; /* this one's always in bytes! */
1990 /* We don't know much -- general case. */
1aa99e6b 1991 if (do_utf8) {
d6a28714 1992 for (;;) {
3b0527fe 1993 if (regtry(&reginfo, s))
d6a28714
JH
1994 goto got_it;
1995 if (s >= strend)
1996 break;
b8c5462f 1997 s += UTF8SKIP(s);
d6a28714
JH
1998 };
1999 }
2000 else {
2001 do {
3b0527fe 2002 if (regtry(&reginfo, s))
d6a28714
JH
2003 goto got_it;
2004 } while (s++ < strend);
2005 }
2006 }
2007
2008 /* Failure. */
2009 goto phooey;
2010
2011got_it:
2012 RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted);
2013
2014 if (PL_reg_eval_set) {
2015 /* Preserve the current value of $^R */
2016 if (oreplsv != GvSV(PL_replgv))
2017 sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is
2018 restored, the value remains
2019 the same. */
4f639d21 2020 restore_pos(aTHX_ prog);
d6a28714
JH
2021 }
2022
2023 /* make sure $`, $&, $', and $digit will work later */
2024 if ( !(flags & REXEC_NOT_FIRST) ) {
ed252734 2025 RX_MATCH_COPY_FREE(prog);
d6a28714
JH
2026 if (flags & REXEC_COPY_STR) {
2027 I32 i = PL_regeol - startpos + (stringarg - strbeg);
f8c7b90f 2028#ifdef PERL_OLD_COPY_ON_WRITE
ed252734
NC
2029 if ((SvIsCOW(sv)
2030 || (SvFLAGS(sv) & CAN_COW_MASK) == CAN_COW_FLAGS)) {
2031 if (DEBUG_C_TEST) {
2032 PerlIO_printf(Perl_debug_log,
2033 "Copy on write: regexp capture, type %d\n",
2034 (int) SvTYPE(sv));
2035 }
2036 prog->saved_copy = sv_setsv_cow(prog->saved_copy, sv);
d5263905 2037 prog->subbeg = (char *)SvPVX_const(prog->saved_copy);
ed252734
NC
2038 assert (SvPOKp(prog->saved_copy));
2039 } else
2040#endif
2041 {
2042 RX_MATCH_COPIED_on(prog);
2043 s = savepvn(strbeg, i);
2044 prog->subbeg = s;
2045 }
d6a28714 2046 prog->sublen = i;
d6a28714
JH
2047 }
2048 else {
2049 prog->subbeg = strbeg;
2050 prog->sublen = PL_regeol - strbeg; /* strend may have been modified */
2051 }
2052 }
9041c2e3 2053
d6a28714
JH
2054 return 1;
2055
2056phooey:
a3621e74 2057 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch failed%s\n",
e4584336 2058 PL_colors[4], PL_colors[5]));
d6a28714 2059 if (PL_reg_eval_set)
4f639d21 2060 restore_pos(aTHX_ prog);
d6a28714
JH
2061 return 0;
2062}
2063
2064/*
2065 - regtry - try match at specific point
2066 */
2067STATIC I32 /* 0 failure, 1 success */
3b0527fe 2068S_regtry(pTHX_ const regmatch_info *reginfo, char *startpos)
d6a28714 2069{
97aff369 2070 dVAR;
d6a28714
JH
2071 register I32 *sp;
2072 register I32 *ep;
2073 CHECKPOINT lastcp;
3b0527fe 2074 regexp *prog = reginfo->prog;
a3621e74 2075 GET_RE_DEBUG_FLAGS_DECL;
d6a28714 2076
02db2b7b
IZ
2077#ifdef DEBUGGING
2078 PL_regindent = 0; /* XXXX Not good when matches are reenterable... */
2079#endif
d6a28714
JH
2080 if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) {
2081 MAGIC *mg;
2082
2083 PL_reg_eval_set = RS_init;
a3621e74 2084 DEBUG_EXECUTE_r(DEBUG_s(
b900a521
JH
2085 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %"IVdf"\n",
2086 (IV)(PL_stack_sp - PL_stack_base));
d6a28714 2087 ));
e8347627 2088 SAVEI32(cxstack[cxstack_ix].blk_oldsp);
d6a28714
JH
2089 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
2090 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
2091 SAVETMPS;
2092 /* Apparently this is not needed, judging by wantarray. */
e8347627 2093 /* SAVEI8(cxstack[cxstack_ix].blk_gimme);
d6a28714
JH
2094 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
2095
3b0527fe 2096 if (reginfo->sv) {
d6a28714 2097 /* Make $_ available to executed code. */
3b0527fe 2098 if (reginfo->sv != DEFSV) {
59f00321 2099 SAVE_DEFSV;
3b0527fe 2100 DEFSV = reginfo->sv;
b8c5462f 2101 }
d6a28714 2102
3b0527fe
DM
2103 if (!(SvTYPE(reginfo->sv) >= SVt_PVMG && SvMAGIC(reginfo->sv)
2104 && (mg = mg_find(reginfo->sv, PERL_MAGIC_regex_global)))) {
d6a28714 2105 /* prepare for quick setting of pos */
d300d9fa
NC
2106#ifdef PERL_OLD_COPY_ON_WRITE
2107 if (SvIsCOW(sv))
2108 sv_force_normal_flags(sv, 0);
2109#endif
3b0527fe 2110 mg = sv_magicext(reginfo->sv, (SV*)0, PERL_MAGIC_regex_global,
d300d9fa 2111 &PL_vtbl_mglob, NULL, 0);
d6a28714 2112 mg->mg_len = -1;
b8c5462f 2113 }
d6a28714
JH
2114 PL_reg_magic = mg;
2115 PL_reg_oldpos = mg->mg_len;
4f639d21 2116 SAVEDESTRUCTOR_X(restore_pos, prog);
d6a28714 2117 }
09687e5a 2118 if (!PL_reg_curpm) {
a02a5408 2119 Newxz(PL_reg_curpm, 1, PMOP);
09687e5a
AB
2120#ifdef USE_ITHREADS
2121 {
2122 SV* repointer = newSViv(0);
577e12cc 2123 /* so we know which PL_regex_padav element is PL_reg_curpm */
35061a7e 2124 SvFLAGS(repointer) |= SVf_BREAK;
09687e5a
AB
2125 av_push(PL_regex_padav,repointer);
2126 PL_reg_curpm->op_pmoffset = av_len(PL_regex_padav);
2127 PL_regex_pad = AvARRAY(PL_regex_padav);
2128 }
2129#endif
2130 }
aaa362c4 2131 PM_SETRE(PL_reg_curpm, prog);
d6a28714
JH
2132 PL_reg_oldcurpm = PL_curpm;
2133 PL_curpm = PL_reg_curpm;
2134 if (RX_MATCH_COPIED(prog)) {
2135 /* Here is a serious problem: we cannot rewrite subbeg,
2136 since it may be needed if this match fails. Thus
2137 $` inside (?{}) could fail... */
2138 PL_reg_oldsaved = prog->subbeg;
2139 PL_reg_oldsavedlen = prog->sublen;
f8c7b90f 2140#ifdef PERL_OLD_COPY_ON_WRITE
ed252734
NC
2141 PL_nrs = prog->saved_copy;
2142#endif
d6a28714
JH
2143 RX_MATCH_COPIED_off(prog);
2144 }
2145 else
bd61b366 2146 PL_reg_oldsaved = NULL;
d6a28714
JH
2147 prog->subbeg = PL_bostr;
2148 prog->sublen = PL_regeol - PL_bostr; /* strend may have been modified */
2149 }
973dddac 2150 prog->startp[0] = startpos - PL_bostr;
d6a28714
JH
2151 PL_reginput = startpos;
2152 PL_regstartp = prog->startp;
2153 PL_regendp = prog->endp;
2154 PL_reglastparen = &prog->lastparen;
a01268b5 2155 PL_reglastcloseparen = &prog->lastcloseparen;
d6a28714 2156 prog->lastparen = 0;
03994de8 2157 prog->lastcloseparen = 0;
d6a28714 2158 PL_regsize = 0;
a3621e74 2159 DEBUG_EXECUTE_r(PL_reg_starttry = startpos);
d6a28714
JH
2160 if (PL_reg_start_tmpl <= prog->nparens) {
2161 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
2162 if(PL_reg_start_tmp)
2163 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
2164 else
a02a5408 2165 Newx(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
d6a28714
JH
2166 }
2167
2168 /* XXXX What this code is doing here?!!! There should be no need
2169 to do this again and again, PL_reglastparen should take care of
3dd2943c 2170 this! --ilya*/
dafc8851
JH
2171
2172 /* Tests pat.t#187 and split.t#{13,14} seem to depend on this code.
2173 * Actually, the code in regcppop() (which Ilya may be meaning by
daf18116
JH
2174 * PL_reglastparen), is not needed at all by the test suite
2175 * (op/regexp, op/pat, op/split), but that code is needed, oddly
2176 * enough, for building DynaLoader, or otherwise this
2177 * "Error: '*' not in typemap in DynaLoader.xs, line 164"
2178 * will happen. Meanwhile, this code *is* needed for the
2179 * above-mentioned test suite tests to succeed. The common theme
2180 * on those tests seems to be returning null fields from matches.
2181 * --jhi */
dafc8851 2182#if 1
d6a28714
JH
2183 sp = prog->startp;
2184 ep = prog->endp;
2185 if (prog->nparens) {
097eb12c 2186 register I32 i;
eb160463 2187 for (i = prog->nparens; i > (I32)*PL_reglastparen; i--) {
d6a28714
JH
2188 *++sp = -1;
2189 *++ep = -1;
2190 }
2191 }
dafc8851 2192#endif
02db2b7b 2193 REGCP_SET(lastcp);
3b0527fe 2194 if (regmatch(reginfo, prog->program + 1)) {
d6a28714
JH
2195 prog->endp[0] = PL_reginput - PL_bostr;
2196 return 1;
2197 }
02db2b7b 2198 REGCP_UNWIND(lastcp);
d6a28714
JH
2199 return 0;
2200}
2201
02db2b7b
IZ
2202#define RE_UNWIND_BRANCH 1
2203#define RE_UNWIND_BRANCHJ 2
2204
2205union re_unwind_t;
2206
2207typedef struct { /* XX: makes sense to enlarge it... */
2208 I32 type;
2209 I32 prev;
2210 CHECKPOINT lastcp;
2211} re_unwind_generic_t;
2212
2213typedef struct {
2214 I32 type;
2215 I32 prev;
2216 CHECKPOINT lastcp;
2217 I32 lastparen;
2218 regnode *next;
2219 char *locinput;
2220 I32 nextchr;
3a2830be 2221 int minmod;
02db2b7b
IZ
2222#ifdef DEBUGGING
2223 int regindent;
2224#endif
2225} re_unwind_branch_t;
2226
2227typedef union re_unwind_t {
2228 I32 type;
2229 re_unwind_generic_t generic;
2230 re_unwind_branch_t branch;
2231} re_unwind_t;
2232
8ba1375e
MJD
2233#define sayYES goto yes
2234#define sayNO goto no
e0f9d4a8 2235#define sayNO_ANYOF goto no_anyof
8ba1375e
MJD
2236#define sayYES_FINAL goto yes_final
2237#define sayYES_LOUD goto yes_loud
2238#define sayNO_FINAL goto no_final
2239#define sayNO_SILENT goto do_no
2240#define saySAME(x) if (x) goto yes; else goto no
2241
3ab3c9b4
HS
2242#define POSCACHE_SUCCESS 0 /* caching success rather than failure */
2243#define POSCACHE_SEEN 1 /* we know what we're caching */
2244#define POSCACHE_START 2 /* the real cache: this bit maps to pos 0 */
2245#define CACHEsayYES STMT_START { \
d8319b27 2246 if (st->u.whilem.cache_offset | st->u.whilem.cache_bit) { \
3ab3c9b4
HS
2247 if (!(PL_reg_poscache[0] & (1<<POSCACHE_SEEN))) \
2248 PL_reg_poscache[0] |= (1<<POSCACHE_SUCCESS) || (1<<POSCACHE_SEEN); \
2249 else if (!(PL_reg_poscache[0] & (1<<POSCACHE_SUCCESS))) { \
2250 /* cache records failure, but this is success */ \
2251 DEBUG_r( \
2252 PerlIO_printf(Perl_debug_log, \
2253 "%*s (remove success from failure cache)\n", \
2254 REPORT_CODE_OFF+PL_regindent*2, "") \
2255 ); \
d8319b27 2256 PL_reg_poscache[st->u.whilem.cache_offset] &= ~(1<<st->u.whilem.cache_bit); \
3ab3c9b4
HS
2257 } \
2258 } \
2259 sayYES; \
2260} STMT_END
2261#define CACHEsayNO STMT_START { \
d8319b27 2262 if (st->u.whilem.cache_offset | st->u.whilem.cache_bit) { \
3ab3c9b4
HS
2263 if (!(PL_reg_poscache[0] & (1<<POSCACHE_SEEN))) \
2264 PL_reg_poscache[0] |= (1<<POSCACHE_SEEN); \
2265 else if ((PL_reg_poscache[0] & (1<<POSCACHE_SUCCESS))) { \
2266 /* cache records success, but this is failure */ \
2267 DEBUG_r( \
2268 PerlIO_printf(Perl_debug_log, \
2269 "%*s (remove failure from success cache)\n", \
2270 REPORT_CODE_OFF+PL_regindent*2, "") \
2271 ); \
d8319b27 2272 PL_reg_poscache[st->u.whilem.cache_offset] &= ~(1<<st->u.whilem.cache_bit); \
3ab3c9b4
HS
2273 } \
2274 } \
2275 sayNO; \
2276} STMT_END
2277
a3621e74
YO
2278/* this is used to determine how far from the left messages like
2279 'failed...' are printed. Currently 29 makes these messages line
2280 up with the opcode they refer to. Earlier perls used 25 which
2281 left these messages outdented making reviewing a debug output
2282 quite difficult.
2283*/
2284#define REPORT_CODE_OFF 29
2285
2286
2287/* Make sure there is a test for this +1 options in re_tests */
2288#define TRIE_INITAL_ACCEPT_BUFFLEN 4;
2289
86545054
DM
2290#define SLAB_FIRST(s) (&(s)->states[0])
2291#define SLAB_LAST(s) (&(s)->states[PERL_REGMATCH_SLAB_SLOTS-1])
2292
5d9a96ca
DM
2293/* grab a new slab and return the first slot in it */
2294
2295STATIC regmatch_state *
2296S_push_slab(pTHX)
2297{
2298 regmatch_slab *s = PL_regmatch_slab->next;
2299 if (!s) {
2300 Newx(s, 1, regmatch_slab);
2301 s->prev = PL_regmatch_slab;
2302 s->next = NULL;
2303 PL_regmatch_slab->next = s;
2304 }
2305 PL_regmatch_slab = s;
86545054 2306 return SLAB_FIRST(s);
5d9a96ca 2307}
5b47454d 2308
95b24440
DM
2309/* simulate a recursive call to regmatch */
2310
2311#define REGMATCH(ns, where) \
5d9a96ca
DM
2312 st->scan = scan; \
2313 scan = (ns); \
2314 st->resume_state = resume_##where; \
95b24440
DM
2315 goto start_recurse; \
2316 resume_point_##where:
2317
aa283a38
DM
2318
2319/* push a new regex state. Set newst to point to it */
2320
2321#define PUSH_STATE(newst, resume) \
2322 depth++; \
2323 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "PUSH STATE(%d)\n", depth)); \
2324 st->scan = scan; \
2325 st->next = next; \
2326 st->n = n; \
2327 st->locinput = locinput; \
2328 st->resume_state = resume; \
2329 newst = st+1; \
86545054 2330 if (newst > SLAB_LAST(PL_regmatch_slab)) \
aa283a38
DM
2331 newst = S_push_slab(aTHX); \
2332 PL_regmatch_state = newst; \
2333 newst->cc = 0; \
2334 newst->minmod = 0; \
2335 newst->sw = 0; \
2336 newst->logical = 0; \
2337 newst->unwind = 0; \
2338 locinput = PL_reginput; \
2339 nextchr = UCHARAT(locinput);
2340
2341#define POP_STATE \
2342 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "POP STATE(%d)\n", depth)); \
2343 depth--; \
2344 st--; \
86545054 2345 if (st < SLAB_FIRST(PL_regmatch_slab)) { \
aa283a38 2346 PL_regmatch_slab = PL_regmatch_slab->prev; \
86545054 2347 st = SLAB_LAST(PL_regmatch_slab); \
aa283a38
DM
2348 } \
2349 PL_regmatch_state = st; \
2350 scan = st->scan; \
2351 next = st->next; \
2352 n = st->n; \
2353 locinput = st->locinput; \
2354 nextchr = UCHARAT(locinput);
2355
d6a28714
JH
2356/*
2357 - regmatch - main matching routine
2358 *
2359 * Conceptually the strategy is simple: check to see whether the current
2360 * node matches, call self recursively to see whether the rest matches,
2361 * and then act accordingly. In practice we make some effort to avoid
2362 * recursion, in particular by going through "ordinary" nodes (that don't
2363 * need to know whether the rest of the match failed) by a loop instead of
2364 * by recursion.
2365 */
2366/* [lwall] I've hoisted the register declarations to the outer block in order to
2367 * maybe save a little bit of pushing and popping on the stack. It also takes
2368 * advantage of machines that use a register save mask on subroutine entry.
95b24440
DM
2369 *
2370 * This function used to be heavily recursive, but since this had the
2371 * effect of blowing the CPU stack on complex regexes, it has been
2372 * restructured to be iterative, and to save state onto the heap rather
2373 * than the stack. Essentially whereever regmatch() used to be called, it
2374 * pushes the current state, notes where to return, then jumps back into
2375 * the main loop.
2376 *
2377 * Originally the structure of this function used to look something like
2378
2379 S_regmatch() {
2380 int a = 1, b = 2;
2381 ...
2382 while (scan != NULL) {
5d9a96ca 2383 a++; // do stuff with a and b
95b24440
DM
2384 ...
2385 switch (OP(scan)) {
2386 case FOO: {
2387 int local = 3;
2388 ...
2389 if (regmatch(...)) // recurse
2390 goto yes;
2391 }
2392 ...
2393 }
2394 }
2395 yes:
2396 return 1;
2397 }
2398
2399 * Now it looks something like this:
2400
5d9a96ca 2401 typedef struct {
95b24440
DM
2402 int a, b, local;
2403 int resume_state;
5d9a96ca 2404 } regmatch_state;
95b24440
DM
2405
2406 S_regmatch() {
5d9a96ca
DM
2407 regmatch_state *st = new();
2408 int depth=0;
2409 st->a++; // do stuff with a and b
95b24440
DM
2410 ...
2411 while (scan != NULL) {
2412 ...
2413 switch (OP(scan)) {
2414 case FOO: {
5d9a96ca 2415 st->local = 3;
95b24440 2416 ...
5d9a96ca
DM
2417 st->scan = scan;
2418 scan = ...;
2419 st->resume_state = resume_FOO;
2420 goto start_recurse; // recurse
95b24440 2421
5d9a96ca
DM
2422 resume_point_FOO:
2423 if (result)
95b24440
DM
2424 goto yes;
2425 }
2426 ...
2427 }
5d9a96ca
DM
2428 start_recurse:
2429 st = new(); push a new state
2430 st->a = 1; st->b = 2;
2431 depth++;
95b24440 2432 }
5d9a96ca 2433 yes:
95b24440 2434 result = 1;
5d9a96ca
DM
2435 if (depth--) {
2436 st = pop();
95b24440
DM
2437 switch (resume_state) {
2438 case resume_FOO:
2439 goto resume_point_FOO;
2440 ...
2441 }
2442 }
2443 return result
2444 }
2445
2446 * WARNING: this means that any line in this function that contains a
2447 * REGMATCH() or TRYPAREN() is actually simulating a recursive call to
2448 * regmatch() using gotos instead. Thus the values of any local variables
2449 * not saved in the regmatch_state structure will have been lost when
2450 * execution resumes on the next line .
5d9a96ca
DM
2451 *
2452 * States (ie the st pointer) are allocated in slabs of about 4K in size.
2453 * PL_regmatch_state always points to the currently active state, and
2454 * PL_regmatch_slab points to the slab currently containing PL_regmatch_state.
2455 * The first time regmatch is called, the first slab is allocated, and is
2456 * never freed until interpreter desctruction. When the slab is full,
2457 * a new one is allocated chained to the end. At exit from regmatch, slabs
2458 * allocated since entry are freed.
d6a28714 2459 */
95b24440
DM
2460
2461
d6a28714 2462STATIC I32 /* 0 failure, 1 success */
3b0527fe 2463S_regmatch(pTHX_ const regmatch_info *reginfo, regnode *prog)
d6a28714 2464{
27da23d5 2465 dVAR;
95b24440 2466 register const bool do_utf8 = PL_reg_match_utf8;
4ad0818d 2467 const U32 uniflags = UTF8_ALLOW_DEFAULT;
95b24440 2468
3b0527fe
DM
2469 regexp *rex = reginfo->prog;
2470
5d9a96ca
DM
2471 regmatch_slab *orig_slab;
2472 regmatch_state *orig_state;
a3621e74 2473
5d9a96ca
DM
2474 /* the current state. This is a cached copy of PL_regmatch_state */
2475 register regmatch_state *st;
95b24440 2476
5d9a96ca
DM
2477 /* cache heavy used fields of st in registers */
2478 register regnode *scan;
2479 register regnode *next;
2480 register I32 n = 0; /* initialize to shut up compiler warning */
2481 register char *locinput = PL_reginput;
95b24440 2482
5d9a96ca
DM
2483 /* these variables are NOT saved during a recusive RFEGMATCH: */
2484 register I32 nextchr; /* is always set to UCHARAT(locinput) */
2485 bool result; /* return value of S_regmatch */
2486 regnode *inner; /* Next node in internal branch. */
2487 int depth = 0; /* depth of recursion */
aa283a38 2488 regmatch_state *newst; /* when pushing a state, this is the new one */
77cb431f
DM
2489 regmatch_state *yes_state = NULL; /* state to pop to on success of
2490 subpattern */
95b24440
DM
2491
2492#ifdef DEBUGGING
ab74612d 2493 SV *re_debug_flags = NULL;
a3621e74 2494 GET_RE_DEBUG_FLAGS;
d6a28714
JH
2495 PL_regindent++;
2496#endif
2497
5d9a96ca
DM
2498 /* on first ever call to regmatch, allocate first slab */
2499 if (!PL_regmatch_slab) {
2500 Newx(PL_regmatch_slab, 1, regmatch_slab);
2501 PL_regmatch_slab->prev = NULL;
2502 PL_regmatch_slab->next = NULL;
86545054 2503 PL_regmatch_state = SLAB_FIRST(PL_regmatch_slab);
5d9a96ca
DM
2504 }
2505
2506 /* remember current high-water mark for exit */
2507 /* XXX this should be done with SAVE* instead */
2508 orig_slab = PL_regmatch_slab;
2509 orig_state = PL_regmatch_state;
2510
2511 /* grab next free state slot */
2512 st = ++PL_regmatch_state;
86545054 2513 if (st > SLAB_LAST(PL_regmatch_slab))
5d9a96ca
DM
2514 st = PL_regmatch_state = S_push_slab(aTHX);
2515
2516 st->minmod = 0;
2517 st->sw = 0;
2518 st->logical = 0;
2519 st->unwind = 0;
2520 st->cc = NULL;
d6a28714
JH
2521 /* Note that nextchr is a byte even in UTF */
2522 nextchr = UCHARAT(locinput);
2523 scan = prog;
2524 while (scan != NULL) {
8ba1375e 2525
a3621e74 2526 DEBUG_EXECUTE_r( {
6136c704 2527 SV * const prop = sv_newmortal();
1df70142
AL
2528 const int docolor = *PL_colors[0];
2529 const int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
1aa99e6b 2530 int l = (PL_regeol - locinput) > taill ? taill : (PL_regeol - locinput);
d6a28714
JH
2531 /* The part of the string before starttry has one color
2532 (pref0_len chars), between starttry and current
2533 position another one (pref_len - pref0_len chars),
2534 after the current position the third one.
2535 We assume that pref0_len <= pref_len, otherwise we
2536 decrease pref0_len. */
9041c2e3 2537 int pref_len = (locinput - PL_bostr) > (5 + taill) - l
1aa99e6b
IH
2538 ? (5 + taill) - l : locinput - PL_bostr;
2539 int pref0_len;
d6a28714 2540
df1ffd02 2541 while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput - pref_len)))
1aa99e6b
IH
2542 pref_len++;
2543 pref0_len = pref_len - (locinput - PL_reg_starttry);
d6a28714 2544 if (l + pref_len < (5 + taill) && l < PL_regeol - locinput)
9041c2e3 2545 l = ( PL_regeol - locinput > (5 + taill) - pref_len
d6a28714 2546 ? (5 + taill) - pref_len : PL_regeol - locinput);
df1ffd02 2547 while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput + l)))
1aa99e6b 2548 l--;
d6a28714
JH
2549 if (pref0_len < 0)
2550 pref0_len = 0;
2551 if (pref0_len > pref_len)
2552 pref0_len = pref_len;
32fc9b6a 2553 regprop(rex, prop, scan);
2a782b5b 2554 {
1df70142 2555 const char * const s0 =
f14c76ed 2556 do_utf8 && OP(scan) != CANY ?
95b24440 2557 pv_uni_display(PERL_DEBUG_PAD(0), (U8*)(locinput - pref_len),
c728cb41 2558 pref0_len, 60, UNI_DISPLAY_REGEX) :
2a782b5b 2559 locinput - pref_len;
1df70142
AL
2560 const int len0 = do_utf8 ? strlen(s0) : pref0_len;
2561 const char * const s1 = do_utf8 && OP(scan) != CANY ?
95b24440
DM
2562 pv_uni_display(PERL_DEBUG_PAD(1),
2563 (U8*)(locinput - pref_len + pref0_len),
c728cb41 2564 pref_len - pref0_len, 60, UNI_DISPLAY_REGEX) :
2a782b5b 2565 locinput - pref_len + pref0_len;
1df70142
AL
2566 const int len1 = do_utf8 ? strlen(s1) : pref_len - pref0_len;
2567 const char * const s2 = do_utf8 && OP(scan) != CANY ?
95b24440 2568 pv_uni_display(PERL_DEBUG_PAD(2), (U8*)locinput,
c728cb41 2569 PL_regeol - locinput, 60, UNI_DISPLAY_REGEX) :
2a782b5b 2570 locinput;
1df70142 2571 const int len2 = do_utf8 ? strlen(s2) : l;
2a782b5b
JH
2572 PerlIO_printf(Perl_debug_log,
2573 "%4"IVdf" <%s%.*s%s%s%.*s%s%s%s%.*s%s>%*s|%3"IVdf":%*s%s\n",
2574 (IV)(locinput - PL_bostr),
2575 PL_colors[4],
2576 len0, s0,
2577 PL_colors[5],
2578 PL_colors[2],
2579 len1, s1,
2580 PL_colors[3],
2581 (docolor ? "" : "> <"),
2582 PL_colors[0],
2583 len2, s2,
2584 PL_colors[1],
2585 15 - l - pref_len + 1,
2586 "",
4f639d21 2587 (IV)(scan - rex->program), PL_regindent*2, "",
3f7c398e 2588 SvPVX_const(prop));
2a782b5b
JH
2589 }
2590 });
d6a28714
JH
2591
2592 next = scan + NEXT_OFF(scan);
2593 if (next == scan)
2594 next = NULL;
2595
2596 switch (OP(scan)) {
2597 case BOL:
7fba1cd6 2598 if (locinput == PL_bostr)
d6a28714 2599 {
3b0527fe 2600 /* reginfo->till = reginfo->bol; */
b8c5462f
JH
2601 break;
2602 }
d6a28714
JH
2603 sayNO;
2604 case MBOL:
12d33761
HS
2605 if (locinput == PL_bostr ||
2606 ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n'))
d6a28714 2607 {
b8c5462f
JH
2608 break;
2609 }
d6a28714
JH
2610 sayNO;
2611 case SBOL:
c2a73568 2612 if (locinput == PL_bostr)
b8c5462f 2613 break;
d6a28714
JH
2614 sayNO;
2615 case GPOS:
3b0527fe 2616 if (locinput == reginfo->ganch)
d6a28714
JH
2617 break;
2618 sayNO;
2619 case EOL:
d6a28714
JH
2620 goto seol;
2621 case MEOL:
d6a28714 2622 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
b8c5462f 2623 sayNO;
b8c5462f 2624 break;
d6a28714
JH
2625 case SEOL:
2626 seol:
2627 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
b8c5462f 2628 sayNO;
d6a28714 2629 if (PL_regeol - locinput > 1)
b8c5462f 2630 sayNO;
b8c5462f 2631 break;
d6a28714
JH
2632 case EOS:
2633 if (PL_regeol != locinput)
b8c5462f 2634 sayNO;
d6a28714 2635 break;
ffc61ed2 2636 case SANY:
d6a28714 2637 if (!nextchr && locinput >= PL_regeol)
4633a7c4 2638 sayNO;
f33976b4
DB
2639 if (do_utf8) {
2640 locinput += PL_utf8skip[nextchr];
2641 if (locinput > PL_regeol)
2642 sayNO;
2643 nextchr = UCHARAT(locinput);
2644 }
2645 else
2646 nextchr = UCHARAT(++locinput);
2647 break;
2648 case CANY:
2649 if (!nextchr && locinput >= PL_regeol)
2650 sayNO;
b8c5462f 2651 nextchr = UCHARAT(++locinput);
a0d0e21e 2652 break;
ffc61ed2 2653 case REG_ANY:
1aa99e6b
IH
2654 if ((!nextchr && locinput >= PL_regeol) || nextchr == '\n')
2655 sayNO;
2656 if (do_utf8) {
b8c5462f 2657 locinput += PL_utf8skip[nextchr];
d6a28714
JH
2658 if (locinput > PL_regeol)
2659 sayNO;
a0ed51b3 2660 nextchr = UCHARAT(locinput);
a0ed51b3 2661 }
1aa99e6b
IH
2662 else
2663 nextchr = UCHARAT(++locinput);
a0ed51b3 2664 break;
a3621e74
YO
2665
2666
2667
2668 /*
2669 traverse the TRIE keeping track of all accepting states
2670 we transition through until we get to a failing node.
2671
a3621e74
YO
2672
2673 */
5b47454d 2674 case TRIE:
a3621e74
YO
2675 case TRIEF:
2676 case TRIEFL:
2677 {
a3621e74
YO
2678 U8 *uc = ( U8* )locinput;
2679 U32 state = 1;
2680 U16 charid = 0;
2681 U32 base = 0;
2682 UV uvc = 0;
2683 STRLEN len = 0;
2684 STRLEN foldlen = 0;
a3621e74
YO
2685 U8 *uscan = (U8*)NULL;
2686 STRLEN bufflen=0;
95b24440 2687 SV *sv_accept_buff = NULL;
5b47454d
DM
2688 const enum { trie_plain, trie_utf8, trie_uft8_fold }
2689 trie_type = do_utf8 ?
2690 (OP(scan) == TRIE ? trie_utf8 : trie_uft8_fold)
2691 : trie_plain;
2692
7087a21c
NC
2693 /* what trie are we using right now */
2694 reg_trie_data *trie
32fc9b6a 2695 = (reg_trie_data*)rex->data->data[ ARG( scan ) ];
d8319b27 2696 st->u.trie.accepted = 0; /* how many accepting states we have seen */
95b24440 2697 result = 0;
a3621e74
YO
2698
2699 while ( state && uc <= (U8*)PL_regeol ) {
2700
5b47454d 2701 if (trie->states[ state ].wordnum) {
d8319b27 2702 if (!st->u.trie.accepted ) {
5b47454d
DM
2703 ENTER;
2704 SAVETMPS;
2705 bufflen = TRIE_INITAL_ACCEPT_BUFFLEN;
2706 sv_accept_buff=newSV(bufflen *
2707 sizeof(reg_trie_accepted) - 1);
2708 SvCUR_set(sv_accept_buff,
2709 sizeof(reg_trie_accepted));
2710 SvPOK_on(sv_accept_buff);
2711 sv_2mortal(sv_accept_buff);
d8319b27 2712 st->u.trie.accept_buff =
5b47454d
DM
2713 (reg_trie_accepted*)SvPV_nolen(sv_accept_buff );
2714 }
2715 else {
d8319b27 2716 if (st->u.trie.accepted >= bufflen) {
5b47454d 2717 bufflen *= 2;
d8319b27 2718 st->u.trie.accept_buff =(reg_trie_accepted*)
5b47454d
DM
2719 SvGROW(sv_accept_buff,
2720 bufflen * sizeof(reg_trie_accepted));
2721 }
2722 SvCUR_set(sv_accept_buff,SvCUR(sv_accept_buff)
2723 + sizeof(reg_trie_accepted));
2724 }
d8319b27
DM
2725 st->u.trie.accept_buff[st->u.trie.accepted].wordnum = trie->states[state].wordnum;
2726 st->u.trie.accept_buff[st->u.trie.accepted].endpos = uc;
2727 ++st->u.trie.accepted;
5b47454d 2728 }
a3621e74
YO
2729
2730 base = trie->states[ state ].trans.base;
2731
2732 DEBUG_TRIE_EXECUTE_r(
2733 PerlIO_printf( Perl_debug_log,
e4584336 2734 "%*s %sState: %4"UVxf", Base: %4"UVxf", Accepted: %4"UVxf" ",
a3621e74 2735 REPORT_CODE_OFF + PL_regindent * 2, "", PL_colors[4],
d8319b27 2736 (UV)state, (UV)base, (UV)st->u.trie.accepted );
a3621e74
YO
2737 );
2738
2739 if ( base ) {
5b47454d
DM
2740 switch (trie_type) {
2741 case trie_uft8_fold:
a3621e74
YO
2742 if ( foldlen>0 ) {
2743 uvc = utf8n_to_uvuni( uscan, UTF8_MAXLEN, &len, uniflags );
2744 foldlen -= len;
2745 uscan += len;
2746 len=0;
2747 } else {
1df70142 2748 U8 foldbuf[ UTF8_MAXBYTES_CASE + 1 ];
a3621e74
YO
2749 uvc = utf8n_to_uvuni( (U8*)uc, UTF8_MAXLEN, &len, uniflags );
2750 uvc = to_uni_fold( uvc, foldbuf, &foldlen );
2751 foldlen -= UNISKIP( uvc );
2752 uscan = foldbuf + UNISKIP( uvc );
2753 }
5b47454d
DM
2754 break;
2755 case trie_utf8:
2756 uvc = utf8n_to_uvuni( (U8*)uc, UTF8_MAXLEN,
2757 &len, uniflags );
2758 break;
2759 case trie_plain:
e4584336 2760 uvc = (UV)*uc;
a3621e74
YO
2761 len = 1;
2762 }
2763
5b47454d
DM
2764 if (uvc < 256) {
2765 charid = trie->charmap[ uvc ];
2766 }
2767 else {
2768 charid = 0;
2769 if (trie->widecharmap) {
2770 SV** svpp = (SV**)NULL;
2771 svpp = hv_fetch(trie->widecharmap,
2772 (char*)&uvc, sizeof(UV), 0);
2773 if (svpp)
2774 charid = (U16)SvIV(*svpp);
2775 }
2776 }
a3621e74 2777
5b47454d
DM
2778 if (charid &&
2779 (base + charid > trie->uniquecharcount )
2780 && (base + charid - 1 - trie->uniquecharcount
2781 < trie->lasttrans)
2782 && trie->trans[base + charid - 1 -
2783 trie->uniquecharcount].check == state)
2784 {
2785 state = trie->trans[base + charid - 1 -
2786 trie->uniquecharcount ].next;
2787 }
2788 else {
2789 state = 0;
2790 }
2791 uc += len;
2792
2793 }
2794 else {
a3621e74
YO
2795 state = 0;
2796 }
2797 DEBUG_TRIE_EXECUTE_r(
e4584336
RB
2798 PerlIO_printf( Perl_debug_log,
2799 "Charid:%3x CV:%4"UVxf" After State: %4"UVxf"%s\n",
2800 charid, uvc, (UV)state, PL_colors[5] );
a3621e74
YO
2801 );
2802 }
d8319b27 2803 if (!st->u.trie.accepted )
a3621e74 2804 sayNO;
a3621e74
YO
2805
2806 /*
2807 There was at least one accepting state that we
2808 transitioned through. Presumably the number of accepting
2809 states is going to be low, typically one or two. So we
2810 simply scan through to find the one with lowest wordnum.
2811 Once we find it, we swap the last state into its place
2812 and decrement the size. We then try to match the rest of
2813 the pattern at the point where the word ends, if we
2814 succeed then we end the loop, otherwise the loop
2815 eventually terminates once all of the accepting states
2816 have been tried.
2817 */
a3621e74 2818
d8319b27 2819 if ( st->u.trie.accepted == 1 ) {
a3621e74 2820 DEBUG_EXECUTE_r({
097eb12c 2821 SV ** const tmp = av_fetch( trie->words, st->u.trie.accept_buff[ 0 ].wordnum-1, 0 );
a3621e74
YO
2822 PerlIO_printf( Perl_debug_log,
2823 "%*s %sonly one match : #%d <%s>%s\n",
e4584336 2824 REPORT_CODE_OFF+PL_regindent*2, "", PL_colors[4],
d8319b27 2825 st->u.trie.accept_buff[ 0 ].wordnum,
cfd0369c 2826 tmp ? SvPV_nolen_const( *tmp ) : "not compiled under -Dr",
a3621e74
YO
2827 PL_colors[5] );
2828 });
d8319b27 2829 PL_reginput = (char *)st->u.trie.accept_buff[ 0 ].endpos;
a3621e74
YO
2830 /* in this case we free tmps/leave before we call regmatch
2831 as we wont be using accept_buff again. */
2832 FREETMPS;
2833 LEAVE;
95b24440
DM
2834 REGMATCH(scan + NEXT_OFF(scan), TRIE1);
2835 /*** all unsaved local vars undefined at this point */
a3621e74
YO
2836 } else {
2837 DEBUG_EXECUTE_r(
e4584336 2838 PerlIO_printf( Perl_debug_log,"%*s %sgot %"IVdf" possible matches%s\n",
d8319b27 2839 REPORT_CODE_OFF + PL_regindent * 2, "", PL_colors[4], (IV)st->u.trie.accepted,
a3621e74
YO
2840 PL_colors[5] );
2841 );
d8319b27 2842 while ( !result && st->u.trie.accepted-- ) {
a3621e74
YO
2843 U32 best = 0;
2844 U32 cur;
d8319b27 2845 for( cur = 1 ; cur <= st->u.trie.accepted ; cur++ ) {
e4584336
RB
2846 DEBUG_TRIE_EXECUTE_r(
2847 PerlIO_printf( Perl_debug_log,
2848 "%*s %sgot %"IVdf" (%d) as best, looking at %"IVdf" (%d)%s\n",
2849 REPORT_CODE_OFF + PL_regindent * 2, "", PL_colors[4],
d8319b27
DM
2850 (IV)best, st->u.trie.accept_buff[ best ].wordnum, (IV)cur,
2851 st->u.trie.accept_buff[ cur ].wordnum, PL_colors[5] );
e4584336 2852 );
a3621e74 2853
d8319b27
DM
2854 if (st->u.trie.accept_buff[cur].wordnum <
2855 st->u.trie.accept_buff[best].wordnum)
e822a8b4 2856 best = cur;
a3621e74
YO
2857 }
2858 DEBUG_EXECUTE_r({
87830502 2859 reg_trie_data * const trie = (reg_trie_data*)
32fc9b6a 2860 rex->data->data[ARG(scan)];
d8319b27 2861 SV ** const tmp = av_fetch( trie->words, st->u.trie.accept_buff[ best ].wordnum - 1, 0 );
a3621e74 2862 PerlIO_printf( Perl_debug_log, "%*s %strying alternation #%d <%s> at 0x%p%s\n",
e4584336 2863 REPORT_CODE_OFF+PL_regindent*2, "", PL_colors[4],
d8319b27 2864 st->u.trie.accept_buff[best].wordnum,
cfd0369c 2865 tmp ? SvPV_nolen_const( *tmp ) : "not compiled under -Dr",scan,
a3621e74
YO
2866 PL_colors[5] );
2867 });
d8319b27
DM
2868 if ( best<st->u.trie.accepted ) {
2869 reg_trie_accepted tmp = st->u.trie.accept_buff[ best ];
2870 st->u.trie.accept_buff[ best ] = st->u.trie.accept_buff[ st->u.trie.accepted ];
2871 st->u.trie.accept_buff[ st->u.trie.accepted ] = tmp;
2872 best = st->u.trie.accepted;
a3621e74 2873 }
d8319b27 2874 PL_reginput = (char *)st->u.trie.accept_buff[ best ].endpos;
a3621e74
YO
2875
2876 /*
2877 as far as I can tell we only need the SAVETMPS/FREETMPS
2878 for re's with EVAL in them but I'm leaving them in for
2879 all until I can be sure.
2880 */
2881 SAVETMPS;
95b24440
DM
2882 REGMATCH(scan + NEXT_OFF(scan), TRIE2);
2883 /*** all unsaved local vars undefined at this point */
a3621e74
YO
2884 FREETMPS;
2885 }
2886 FREETMPS;
2887 LEAVE;
2888 }
2889
95b24440 2890 if (result) {
a3621e74
YO
2891 sayYES;
2892 } else {
2893 sayNO;
2894 }
2895 }
2896 /* unreached codepoint */
95b24440
DM
2897 case EXACT: {
2898 char *s = STRING(scan);
5d9a96ca 2899 st->ln = STR_LEN(scan);
eb160463 2900 if (do_utf8 != UTF) {
bc517b45 2901 /* The target and the pattern have differing utf8ness. */
1aa99e6b 2902 char *l = locinput;
5d9a96ca 2903 const char *e = s + st->ln;
a72c7584 2904
5ff6fc6d
JH
2905 if (do_utf8) {
2906 /* The target is utf8, the pattern is not utf8. */
1aa99e6b 2907 while (s < e) {
a3b680e6 2908 STRLEN ulen;
1aa99e6b 2909 if (l >= PL_regeol)
5ff6fc6d
JH
2910 sayNO;
2911 if (NATIVE_TO_UNI(*(U8*)s) !=
89ebb4a3 2912 utf8n_to_uvuni((U8*)l, UTF8_MAXBYTES, &ulen,
041457d9 2913 uniflags))
5ff6fc6d 2914 sayNO;
bc517b45 2915 l += ulen;
5ff6fc6d 2916 s ++;
1aa99e6b 2917 }
5ff6fc6d
JH
2918 }
2919 else {
2920 /* The target is not utf8, the pattern is utf8. */
1aa99e6b 2921 while (s < e) {
a3b680e6 2922 STRLEN ulen;
1aa99e6b
IH
2923 if (l >= PL_regeol)
2924 sayNO;
5ff6fc6d 2925 if (NATIVE_TO_UNI(*((U8*)l)) !=
89ebb4a3 2926 utf8n_to_uvuni((U8*)s, UTF8_MAXBYTES, &ulen,
041457d9 2927 uniflags))
1aa99e6b 2928 sayNO;
bc517b45 2929 s += ulen;
a72c7584 2930 l ++;
1aa99e6b 2931 }
5ff6fc6d 2932 }
1aa99e6b
IH
2933 locinput = l;
2934 nextchr = UCHARAT(locinput);
2935 break;
2936 }
bc517b45 2937 /* The target and the pattern have the same utf8ness. */
d6a28714
JH
2938 /* Inline the first character, for speed. */
2939 if (UCHARAT(s) != nextchr)
2940 sayNO;
5d9a96ca 2941 if (PL_regeol - locinput < st->ln)
d6a28714 2942 sayNO;
5d9a96ca 2943 if (st->ln > 1 && memNE(s, locinput, st->ln))
d6a28714 2944 sayNO;
5d9a96ca 2945 locinput += st->ln;
d6a28714
JH
2946 nextchr = UCHARAT(locinput);
2947 break;
95b24440 2948 }
d6a28714 2949 case EXACTFL:
b8c5462f
JH
2950 PL_reg_flags |= RF_tainted;
2951 /* FALL THROUGH */
95b24440
DM
2952 case EXACTF: {
2953 char *s = STRING(scan);
5d9a96ca 2954 st->ln = STR_LEN(scan);
d6a28714 2955
d07ddd77
JH
2956 if (do_utf8 || UTF) {
2957 /* Either target or the pattern are utf8. */
d6a28714 2958 char *l = locinput;
d07ddd77 2959 char *e = PL_regeol;
bc517b45 2960
5d9a96ca 2961 if (ibcmp_utf8(s, 0, st->ln, (bool)UTF,
1feea2c7 2962 l, &e, 0, do_utf8)) {
5486206c
JH
2963 /* One more case for the sharp s:
2964 * pack("U0U*", 0xDF) =~ /ss/i,
2965 * the 0xC3 0x9F are the UTF-8
2966 * byte sequence for the U+00DF. */
2967 if (!(do_utf8 &&
2968 toLOWER(s[0]) == 's' &&
5d9a96ca 2969 st->ln >= 2 &&
5486206c
JH
2970 toLOWER(s[1]) == 's' &&
2971 (U8)l[0] == 0xC3 &&
2972 e - l >= 2 &&
2973 (U8)l[1] == 0x9F))
2974 sayNO;
2975 }
d07ddd77
JH
2976 locinput = e;
2977 nextchr = UCHARAT(locinput);
2978 break;
a0ed51b3 2979 }
d6a28714 2980
bc517b45
JH
2981 /* Neither the target and the pattern are utf8. */
2982
d6a28714
JH
2983 /* Inline the first character, for speed. */
2984 if (UCHARAT(s) != nextchr &&
2985 UCHARAT(s) != ((OP(scan) == EXACTF)
2986 ? PL_fold : PL_fold_locale)[nextchr])
a0ed51b3 2987 sayNO;
5d9a96ca 2988 if (PL_regeol - locinput < st->ln)
b8c5462f 2989 sayNO;
5d9a96ca
DM
2990 if (st->ln > 1 && (OP(scan) == EXACTF
2991 ? ibcmp(s, locinput, st->ln)
2992 : ibcmp_locale(s, locinput, st->ln)))
4633a7c4 2993 sayNO;
5d9a96ca 2994 locinput += st->ln;
d6a28714 2995 nextchr = UCHARAT(locinput);
a0d0e21e 2996 break;
95b24440 2997 }
d6a28714 2998 case ANYOF:
ffc61ed2 2999 if (do_utf8) {
9e55ce06
JH
3000 STRLEN inclasslen = PL_regeol - locinput;
3001
32fc9b6a 3002 if (!reginclass(rex, scan, (U8*)locinput, &inclasslen, do_utf8))
e0f9d4a8 3003 sayNO_ANYOF;
ffc61ed2
JH
3004 if (locinput >= PL_regeol)
3005 sayNO;
0f0076b4 3006 locinput += inclasslen ? inclasslen : UTF8SKIP(locinput);
b8c5462f 3007 nextchr = UCHARAT(locinput);
e0f9d4a8 3008 break;
ffc61ed2
JH
3009 }
3010 else {
3011 if (nextchr < 0)
3012 nextchr = UCHARAT(locinput);
32fc9b6a 3013 if (!REGINCLASS(rex, scan, (U8*)locinput))
e0f9d4a8 3014 sayNO_ANYOF;
ffc61ed2
JH
3015 if (!nextchr && locinput >= PL_regeol)
3016 sayNO;
3017 nextchr = UCHARAT(++locinput);
e0f9d4a8
JH
3018 break;
3019 }
3020 no_anyof:
3021 /* If we might have the case of the German sharp s
3022 * in a casefolding Unicode character class. */
3023
ebc501f0
JH
3024 if (ANYOF_FOLD_SHARP_S(scan, locinput, PL_regeol)) {
3025 locinput += SHARP_S_SKIP;
e0f9d4a8 3026 nextchr = UCHARAT(locinput);
ffc61ed2 3027 }
e0f9d4a8
JH
3028 else
3029 sayNO;
b8c5462f 3030 break;
d6a28714 3031 case ALNUML:
b8c5462f
JH
3032 PL_reg_flags |= RF_tainted;
3033 /* FALL THROUGH */
d6a28714 3034 case ALNUM:
b8c5462f 3035 if (!nextchr)
4633a7c4 3036 sayNO;
ffc61ed2 3037 if (do_utf8) {
1a4fad37 3038 LOAD_UTF8_CHARCLASS_ALNUM();
ffc61ed2 3039 if (!(OP(scan) == ALNUM
3568d838 3040 ? swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8)
d6a28714 3041 : isALNUM_LC_utf8((U8*)locinput)))
b8c5462f
JH
3042 {
3043 sayNO;
a0ed51b3 3044 }
b8c5462f 3045 locinput += PL_utf8skip[nextchr];
a0ed51b3
LW
3046 nextchr = UCHARAT(locinput);
3047 break;
3048 }
ffc61ed2 3049 if (!(OP(scan) == ALNUM
d6a28714 3050 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
bbce6d69 3051 sayNO;
b8c5462f 3052 nextchr = UCHARAT(++locinput);
a0d0e21e 3053 break;
d6a28714 3054 case NALNUML:
b8c5462f
JH
3055 PL_reg_flags |= RF_tainted;
3056 /* FALL THROUGH */
d6a28714
JH
3057 case NALNUM:
3058 if (!nextchr && locinput >= PL_regeol)
a0ed51b3 3059 sayNO;
ffc61ed2 3060 if (do_utf8) {
1a4fad37 3061 LOAD_UTF8_CHARCLASS_ALNUM();
ffc61ed2 3062 if (OP(scan) == NALNUM
3568d838 3063 ? swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8)
d6a28714
JH
3064 : isALNUM_LC_utf8((U8*)locinput))
3065 {
b8c5462f 3066 sayNO;
d6a28714 3067 }
b8c5462f
JH
3068 locinput += PL_utf8skip[nextchr];
3069 nextchr = UCHARAT(locinput);
3070 break;
3071 }
ffc61ed2 3072 if (OP(scan) == NALNUM
d6a28714 3073 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
b8c5462f 3074 sayNO;
76e3520e 3075 nextchr = UCHARAT(++locinput);
a0d0e21e 3076 break;
d6a28714
JH
3077 case BOUNDL:
3078 case NBOUNDL:
3280af22 3079 PL_reg_flags |= RF_tainted;
bbce6d69 3080 /* FALL THROUGH */
d6a28714
JH
3081 case BOUND:
3082 case NBOUND:
3083 /* was last char in word? */
ffc61ed2 3084 if (do_utf8) {
12d33761 3085 if (locinput == PL_bostr)
5d9a96ca 3086 st->ln = '\n';
ffc61ed2 3087 else {
a3b680e6 3088 const U8 * const r = reghop3((U8*)locinput, -1, (U8*)PL_bostr);
9041c2e3 3089
4ad0818d 3090 st->ln = utf8n_to_uvchr(r, UTF8SKIP(r), 0, uniflags);
ffc61ed2
JH
3091 }
3092 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
5d9a96ca 3093 st->ln = isALNUM_uni(st->ln);
1a4fad37 3094 LOAD_UTF8_CHARCLASS_ALNUM();
3568d838 3095 n = swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8);
ffc61ed2
JH
3096 }
3097 else {
5d9a96ca 3098 st->ln = isALNUM_LC_uvchr(UNI_TO_NATIVE(st->ln));
ffc61ed2
JH
3099 n = isALNUM_LC_utf8((U8*)locinput);
3100 }
a0ed51b3 3101 }
d6a28714 3102 else {
5d9a96ca 3103 st->ln = (locinput != PL_bostr) ?
12d33761 3104 UCHARAT(locinput - 1) : '\n';
ffc61ed2 3105 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
5d9a96ca 3106 st->ln = isALNUM(st->ln);
ffc61ed2
JH
3107 n = isALNUM(nextchr);
3108 }
3109 else {
5d9a96ca 3110 st->ln = isALNUM_LC(st->ln);
ffc61ed2
JH
3111 n = isALNUM_LC(nextchr);
3112 }
d6a28714 3113 }
5d9a96ca 3114 if (((!st->ln) == (!n)) == (OP(scan) == BOUND ||
ffc61ed2
JH
3115 OP(scan) == BOUNDL))
3116 sayNO;
a0ed51b3 3117 break;
d6a28714 3118 case SPACEL:
3280af22 3119 PL_reg_flags |= RF_tainted;
bbce6d69 3120 /* FALL THROUGH */
d6a28714 3121 case SPACE:
9442cb0e 3122 if (!nextchr)
4633a7c4 3123 sayNO;
1aa99e6b 3124 if (do_utf8) {
fd400ab9 3125 if (UTF8_IS_CONTINUED(nextchr)) {
1a4fad37 3126 LOAD_UTF8_CHARCLASS_SPACE();
ffc61ed2 3127 if (!(OP(scan) == SPACE
3568d838 3128 ? swash_fetch(PL_utf8_space, (U8*)locinput, do_utf8)
ffc61ed2
JH
3129 : isSPACE_LC_utf8((U8*)locinput)))
3130 {
3131 sayNO;
3132 }
3133 locinput += PL_utf8skip[nextchr];
3134 nextchr = UCHARAT(locinput);
3135 break;
d6a28714 3136 }
ffc61ed2
JH
3137 if (!(OP(scan) == SPACE
3138 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
3139 sayNO;
3140 nextchr = UCHARAT(++locinput);
3141 }
3142 else {
3143 if (!(OP(scan) == SPACE
3144 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
3145 sayNO;
3146 nextchr = UCHARAT(++locinput);
a0ed51b3 3147 }
a0ed51b3 3148 break;
d6a28714 3149 case NSPACEL:
3280af22 3150 PL_reg_flags |= RF_tainted;
bbce6d69 3151 /* FALL THROUGH */
d6a28714 3152 case NSPACE:
9442cb0e 3153 if (!nextchr && locinput >= PL_regeol)
b8c5462f 3154 sayNO;
1aa99e6b 3155 if (do_utf8) {
1a4fad37 3156 LOAD_UTF8_CHARCLASS_SPACE();
ffc61ed2 3157 if (OP(scan) == NSPACE
3568d838 3158 ? swash_fetch(PL_utf8_space, (U8*)locinput, do_utf8)
d6a28714 3159 : isSPACE_LC_utf8((U8*)locinput))
b8c5462f
JH
3160 {
3161 sayNO;
3162 }
3163 locinput += PL_utf8skip[nextchr];
3164 nextchr = UCHARAT(locinput);
3165 break;
a0ed51b3 3166 }
ffc61ed2 3167 if (OP(scan) == NSPACE
d6a28714 3168 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
4633a7c4 3169 sayNO;
76e3520e 3170 nextchr = UCHARAT(++locinput);
a0d0e21e 3171 break;
d6a28714 3172 case DIGITL:
a0ed51b3
LW
3173 PL_reg_flags |= RF_tainted;
3174 /* FALL THROUGH */
d6a28714 3175 case DIGIT:
9442cb0e 3176 if (!nextchr)
a0ed51b3 3177 sayNO;
1aa99e6b 3178 if (do_utf8) {
1a4fad37 3179 LOAD_UTF8_CHARCLASS_DIGIT();
ffc61ed2 3180 if (!(OP(scan) == DIGIT
3568d838 3181 ? swash_fetch(PL_utf8_digit, (U8*)locinput, do_utf8)
9442cb0e 3182 : isDIGIT_LC_utf8((U8*)locinput)))
dfe13c55 3183 {
a0ed51b3 3184 sayNO;
dfe13c55 3185 }
6f06b55f 3186 locinput += PL_utf8skip[nextchr];
a0ed51b3
LW
3187 nextchr = UCHARAT(locinput);
3188 break;
3189 }
ffc61ed2 3190 if (!(OP(scan) == DIGIT
9442cb0e 3191 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr)))
a0ed51b3
LW
3192 sayNO;
3193 nextchr = UCHARAT(++locinput);
3194 break;
d6a28714 3195 case NDIGITL:
b8c5462f
JH
3196 PL_reg_flags |= RF_tainted;
3197 /* FALL THROUGH */
d6a28714 3198 case NDIGIT:
9442cb0e 3199 if (!nextchr && locinput >= PL_regeol)
b8c5462f 3200 sayNO;
1aa99e6b 3201 if (do_utf8) {
1a4fad37 3202 LOAD_UTF8_CHARCLASS_DIGIT();
ffc61ed2 3203 if (OP(scan) == NDIGIT
3568d838 3204 ? swash_fetch(PL_utf8_digit, (U8*)locinput, do_utf8)
9442cb0e
GS
3205 : isDIGIT_LC_utf8((U8*)locinput))
3206 {
a0ed51b3 3207 sayNO;
9442cb0e 3208 }
6f06b55f 3209 locinput += PL_utf8skip[nextchr];
a0ed51b3
LW
3210 nextchr = UCHARAT(locinput);
3211 break;
3212 }
ffc61ed2 3213 if (OP(scan) == NDIGIT
9442cb0e 3214 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr))
a0ed51b3
LW
3215 sayNO;
3216 nextchr = UCHARAT(++locinput);
3217 break;
3218 case CLUMP:
b7c83a7e 3219 if (locinput >= PL_regeol)
a0ed51b3 3220 sayNO;
b7c83a7e 3221 if (do_utf8) {
1a4fad37 3222 LOAD_UTF8_CHARCLASS_MARK();
b7c83a7e
JH
3223 if (swash_fetch(PL_utf8_mark,(U8*)locinput, do_utf8))
3224 sayNO;
3225 locinput += PL_utf8skip[nextchr];
3226 while (locinput < PL_regeol &&
3227 swash_fetch(PL_utf8_mark,(U8*)locinput, do_utf8))
3228 locinput += UTF8SKIP(locinput);
3229 if (locinput > PL_regeol)
3230 sayNO;
eb08e2da
JH
3231 }
3232 else
3233 locinput++;
a0ed51b3
LW
3234 nextchr = UCHARAT(locinput);
3235 break;
c8756f30 3236 case REFFL:
3280af22 3237 PL_reg_flags |= RF_tainted;
c8756f30 3238 /* FALL THROUGH */
c277df42 3239 case REF:
95b24440
DM
3240 case REFF: {
3241 char *s;
c277df42 3242 n = ARG(scan); /* which paren pair */
5d9a96ca 3243 st->ln = PL_regstartp[n];
2c2d71f5 3244 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
5d9a96ca 3245 if ((I32)*PL_reglastparen < n || st->ln == -1)
af3f8c16 3246 sayNO; /* Do not match unless seen CLOSEn. */
5d9a96ca 3247 if (st->ln == PL_regendp[n])
a0d0e21e 3248 break;
a0ed51b3 3249
5d9a96ca 3250 s = PL_bostr + st->ln;
1aa99e6b 3251 if (do_utf8 && OP(scan) != REF) { /* REF can do byte comparison */
a0ed51b3 3252 char *l = locinput;
a3b680e6 3253 const char *e = PL_bostr + PL_regendp[n];
a0ed51b3
LW
3254 /*
3255 * Note that we can't do the "other character" lookup trick as
3256 * in the 8-bit case (no pun intended) because in Unicode we
3257 * have to map both upper and title case to lower case.
3258 */
3259 if (OP(scan) == REFF) {
3260 while (s < e) {
a3b680e6
AL
3261 STRLEN ulen1, ulen2;
3262 U8 tmpbuf1[UTF8_MAXBYTES_CASE+1];
3263 U8 tmpbuf2[UTF8_MAXBYTES_CASE+1];
3264
a0ed51b3
LW
3265 if (l >= PL_regeol)
3266 sayNO;
a2a2844f
JH
3267 toLOWER_utf8((U8*)s, tmpbuf1, &ulen1);
3268 toLOWER_utf8((U8*)l, tmpbuf2, &ulen2);
7114a2d2 3269 if (ulen1 != ulen2 || memNE((char *)tmpbuf1, (char *)tmpbuf2, ulen1))
a0ed51b3 3270 sayNO;
a2a2844f
JH
3271 s += ulen1;
3272 l += ulen2;
a0ed51b3
LW
3273 }
3274 }
3275 locinput = l;
3276 nextchr = UCHARAT(locinput);
3277 break;
3278 }
3279
a0d0e21e 3280 /* Inline the first character, for speed. */
76e3520e 3281 if (UCHARAT(s) != nextchr &&
c8756f30
AK
3282 (OP(scan) == REF ||
3283 (UCHARAT(s) != ((OP(scan) == REFF
22c35a8c 3284 ? PL_fold : PL_fold_locale)[nextchr]))))
4633a7c4 3285 sayNO;
5d9a96ca
DM
3286 st->ln = PL_regendp[n] - st->ln;
3287 if (locinput + st->ln > PL_regeol)
4633a7c4 3288 sayNO;
5d9a96ca
DM
3289 if (st->ln > 1 && (OP(scan) == REF
3290 ? memNE(s, locinput, st->ln)
c8756f30 3291 : (OP(scan) == REFF
5d9a96ca
DM
3292 ? ibcmp(s, locinput, st->ln)
3293 : ibcmp_locale(s, locinput, st->ln))))
4633a7c4 3294 sayNO;
5d9a96ca 3295 locinput += st->ln;
76e3520e 3296 nextchr = UCHARAT(locinput);
a0d0e21e 3297 break;
95b24440 3298 }
a0d0e21e
LW
3299
3300 case NOTHING:
c277df42 3301 case TAIL:
a0d0e21e
LW
3302 break;
3303 case BACK:
3304 break;
c277df42
IZ
3305 case EVAL:
3306 {
c277df42 3307 SV *ret;
8e5e9ebe 3308 {
4aabdb9b
DM
3309 /* execute the code in the {...} */
3310 dSP;
6136c704 3311 SV ** const before = SP;
4aabdb9b
DM
3312 OP_4tree * const oop = PL_op;
3313 COP * const ocurcop = PL_curcop;
3314 PAD *old_comppad;
4aabdb9b
DM
3315
3316 n = ARG(scan);
32fc9b6a 3317 PL_op = (OP_4tree*)rex->data->data[n];
4aabdb9b 3318 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%"UVxf"\n", PTR2UV(PL_op)) );
32fc9b6a 3319 PAD_SAVE_LOCAL(old_comppad, (PAD*)rex->data->data[n + 2]);
4aabdb9b
DM
3320 PL_regendp[0] = PL_reg_magic->mg_len = locinput - PL_bostr;
3321
8e5e9ebe
RGS
3322 CALLRUNOPS(aTHX); /* Scalar context. */
3323 SPAGAIN;
3324 if (SP == before)
075aa684 3325 ret = &PL_sv_undef; /* protect against empty (?{}) blocks. */
8e5e9ebe
RGS
3326 else {
3327 ret = POPs;
3328 PUTBACK;
3329 }
4aabdb9b
DM
3330
3331 PL_op = oop;
3332 PAD_RESTORE_LOCAL(old_comppad);
3333 PL_curcop = ocurcop;
3334 if (!st->logical) {
3335 /* /(?{...})/ */
3336 sv_setsv(save_scalar(PL_replgv), ret);
4aabdb9b
DM
3337 break;
3338 }
8e5e9ebe 3339 }
4aabdb9b
DM
3340 if (st->logical == 2) { /* Postponed subexpression: /(??{...})/ */
3341 regexp *re;
4aabdb9b 3342 {
4f639d21
DM
3343 /* extract RE object from returned value; compiling if
3344 * necessary */
3345
6136c704 3346 MAGIC *mg = NULL;
4aabdb9b 3347 SV *sv;
faf82a0b
AE
3348 if(SvROK(ret) && SvSMAGICAL(sv = SvRV(ret)))
3349 mg = mg_find(sv, PERL_MAGIC_qr);
3350 else if (SvSMAGICAL(ret)) {
3351 if (SvGMAGICAL(ret))
3352 sv_unmagic(ret, PERL_MAGIC_qr);
3353 else
3354 mg = mg_find(ret, PERL_MAGIC_qr);
0f5d15d6 3355 }
faf82a0b 3356
0f5d15d6
IZ
3357 if (mg) {
3358 re = (regexp *)mg->mg_obj;
df0003d4 3359 (void)ReREFCNT_inc(re);
0f5d15d6
IZ
3360 }
3361 else {
3362 STRLEN len;
6136c704 3363 const char * const t = SvPV_const(ret, len);
0f5d15d6 3364 PMOP pm;
a3b680e6 3365 const I32 osize = PL_regsize;
0f5d15d6 3366
5fcd1c1b 3367 Zero(&pm, 1, PMOP);
4aabdb9b 3368 if (DO_UTF8(ret)) pm.op_pmdynflags |= PMdf_DYN_UTF8;
83003860 3369 re = CALLREGCOMP(aTHX_ (char*)t, (char*)t + len, &pm);
9041c2e3 3370 if (!(SvFLAGS(ret)
faf82a0b
AE
3371 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY
3372 | SVs_GMG)))
14befaf4
DM
3373 sv_magic(ret,(SV*)ReREFCNT_inc(re),
3374 PERL_MAGIC_qr,0,0);
0f5d15d6 3375 PL_regsize = osize;
0f5d15d6 3376 }
4aabdb9b 3377 }
aa283a38
DM
3378
3379 /* run the pattern returned from (??{...}) */
3380
4aabdb9b
DM
3381 DEBUG_EXECUTE_r(