This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
inline_invlist.c -> invlist_inline.h
[perl5.git] / regexec.c
CommitLineData
a0d0e21e
LW
1/* regexec.c
2 */
3
4/*
f65819ce
CO
5 * One Ring to rule them all, One Ring to find them
6 *
4ac71550
TC
7 * [p.v of _The Lord of the Rings_, opening poem]
8 * [p.50 of _The Lord of the Rings_, I/iii: "The Shadow of the Past"]
9 * [p.254 of _The Lord of the Rings_, II/ii: "The Council of Elrond"]
a0d0e21e
LW
10 */
11
61296642
DM
12/* This file contains functions for executing a regular expression. See
13 * also regcomp.c which funnily enough, contains functions for compiling
166f8a29 14 * a regular expression.
e4a054ea
DM
15 *
16 * This file is also copied at build time to ext/re/re_exec.c, where
17 * it's built with -DPERL_EXT_RE_BUILD -DPERL_EXT_RE_DEBUG -DPERL_EXT.
18 * This causes the main functions to be compiled under new names and with
19 * debugging support added, which makes "use re 'debug'" work.
166f8a29
DM
20 */
21
a687059c
LW
22/* NOTE: this is derived from Henry Spencer's regexp code, and should not
23 * confused with the original package (see point 3 below). Thanks, Henry!
24 */
25
26/* Additional note: this code is very heavily munged from Henry's version
27 * in places. In some spots I've traded clarity for efficiency, so don't
28 * blame Henry for some of the lack of readability.
29 */
30
e50aee73
AD
31/* The names of the functions have been changed from regcomp and
32 * regexec to pregcomp and pregexec in order to avoid conflicts
33 * with the POSIX routines of the same names.
34*/
35
b9d5759e 36#ifdef PERL_EXT_RE_BUILD
54df2634 37#include "re_top.h"
9041c2e3 38#endif
56953603 39
64935bc6 40#define B_ON_NON_UTF8_LOCALE_IS_WRONG \
89ad707a 41 "Use of \\b{} or \\B{} for non-UTF-8 locale is wrong. Assuming a UTF-8 locale"
64935bc6 42
a687059c 43/*
e50aee73 44 * pregcomp and pregexec -- regsub and regerror are not used in perl
a687059c
LW
45 *
46 * Copyright (c) 1986 by University of Toronto.
47 * Written by Henry Spencer. Not derived from licensed software.
48 *
49 * Permission is granted to anyone to use this software for any
50 * purpose on any computer system, and to redistribute it freely,
51 * subject to the following restrictions:
52 *
53 * 1. The author is not responsible for the consequences of use of
54 * this software, no matter how awful, even if they arise
55 * from defects in it.
56 *
57 * 2. The origin of this software must not be misrepresented, either
58 * by explicit claim or by omission.
59 *
60 * 3. Altered versions must be plainly marked as such, and must not
61 * be misrepresented as being the original software.
62 *
63 **** Alterations to Henry's code are...
64 ****
4bb101f2 65 **** Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
1129b882
NC
66 **** 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
67 **** by Larry Wall and others
a687059c 68 ****
9ef589d8
LW
69 **** You may distribute under the terms of either the GNU General Public
70 **** License or the Artistic License, as specified in the README file.
a687059c
LW
71 *
72 * Beware that some of this code is subtly aware of the way operator
73 * precedence is structured in regular expressions. Serious changes in
74 * regular-expression syntax might require a total rethink.
75 */
76#include "EXTERN.h"
864dbfa3 77#define PERL_IN_REGEXEC_C
a687059c 78#include "perl.h"
0f5d15d6 79
54df2634
NC
80#ifdef PERL_IN_XSUB_RE
81# include "re_comp.h"
82#else
83# include "regcomp.h"
84#endif
a687059c 85
b992490d 86#include "invlist_inline.h"
1b0f46bf 87#include "unicode_constants.h"
81e983c1 88
e1cf74e3
CB
89#ifdef DEBUGGING
90/* At least one required character in the target string is expressible only in
91 * UTF-8. */
92static const char* const non_utf8_target_but_utf8_required
93 = "Can't match, because target string needs to be in UTF-8\n";
94#endif
95
96#define NON_UTF8_TARGET_BUT_UTF8_REQUIRED(target) STMT_START { \
97 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%s", non_utf8_target_but_utf8_required));\
98 goto target; \
99} STMT_END
100
c74f6de9
KW
101#define HAS_NONLATIN1_FOLD_CLOSURE(i) _HAS_NONLATIN1_FOLD_CLOSURE_ONLY_FOR_USE_BY_REGCOMP_DOT_C_AND_REGEXEC_DOT_C(i)
102
a687059c
LW
103#ifndef STATIC
104#define STATIC static
105#endif
106
2d66f61e 107/* Valid only for non-utf8 strings: avoids the reginclass
7e2509c1
KW
108 * call if there are no complications: i.e., if everything matchable is
109 * straight forward in the bitmap */
3db24e1e 110#define REGINCLASS(prog,p,c) (ANYOF_FLAGS(p) ? reginclass(prog,p,c,c+1,0) \
af364d03 111 : ANYOF_BITMAP_TEST(p,*(c)))
7d3e948e 112
c277df42
IZ
113/*
114 * Forwards.
115 */
116
f2ed9b32 117#define CHR_SVLEN(sv) (utf8_target ? sv_len_utf8(sv) : SvCUR(sv))
ba44c216 118#define CHR_DIST(a,b) (reginfo->is_utf8_target ? utf8_distance(a,b) : a - b)
a0ed51b3 119
3dab1dad 120#define HOPc(pos,off) \
ba44c216 121 (char *)(reginfo->is_utf8_target \
220db18a 122 ? reghop3((U8*)pos, off, \
9d9163fb 123 (U8*)(off >= 0 ? reginfo->strend : reginfo->strbeg)) \
3dab1dad 124 : (U8*)(pos + off))
557f47af 125
3dab1dad 126#define HOPBACKc(pos, off) \
ba44c216 127 (char*)(reginfo->is_utf8_target \
9d9163fb
DM
128 ? reghopmaybe3((U8*)pos, -off, (U8*)(reginfo->strbeg)) \
129 : (pos - off >= reginfo->strbeg) \
8e11feef 130 ? (U8*)pos - off \
3dab1dad 131 : NULL)
efb30f32 132
ba44c216 133#define HOP3(pos,off,lim) (reginfo->is_utf8_target ? reghop3((U8*)(pos), off, (U8*)(lim)) : (U8*)(pos + off))
1aa99e6b 134#define HOP3c(pos,off,lim) ((char*)HOP3(pos,off,lim))
1aa99e6b 135
557f47af
DM
136/* lim must be +ve. Returns NULL on overshoot */
137#define HOPMAYBE3(pos,off,lim) \
138 (reginfo->is_utf8_target \
139 ? reghopmaybe3((U8*)pos, off, (U8*)(lim)) \
140 : ((U8*)pos + off <= lim) \
141 ? (U8*)pos + off \
142 : NULL)
143
8e9f2289
DM
144/* like HOP3, but limits the result to <= lim even for the non-utf8 case.
145 * off must be >=0; args should be vars rather than expressions */
146#define HOP3lim(pos,off,lim) (reginfo->is_utf8_target \
147 ? reghop3((U8*)(pos), off, (U8*)(lim)) \
148 : (U8*)((pos + off) > lim ? lim : (pos + off)))
149
2974eaec
DM
150#define HOP4(pos,off,llim, rlim) (reginfo->is_utf8_target \
151 ? reghop4((U8*)(pos), off, (U8*)(llim), (U8*)(rlim)) \
152 : (U8*)(pos + off))
153#define HOP4c(pos,off,llim, rlim) ((char*)HOP4(pos,off,llim, rlim))
7016d6eb
DM
154
155#define NEXTCHR_EOS -10 /* nextchr has fallen off the end */
156#define NEXTCHR_IS_EOS (nextchr < 0)
157
158#define SET_nextchr \
220db18a 159 nextchr = ((locinput < reginfo->strend) ? UCHARAT(locinput) : NEXTCHR_EOS)
7016d6eb
DM
160
161#define SET_locinput(p) \
162 locinput = (p); \
163 SET_nextchr
164
165
2a16ac92 166#define LOAD_UTF8_CHARCLASS(swash_ptr, property_name, invlist) STMT_START { \
c7304fe2
KW
167 if (!swash_ptr) { \
168 U8 flags = _CORE_SWASH_INIT_ACCEPT_INVLIST; \
c7304fe2 169 swash_ptr = _core_swash_init("utf8", property_name, &PL_sv_undef, \
2a16ac92 170 1, 0, invlist, &flags); \
c7304fe2
KW
171 assert(swash_ptr); \
172 } \
173 } STMT_END
174
175/* If in debug mode, we test that a known character properly matches */
176#ifdef DEBUGGING
177# define LOAD_UTF8_CHARCLASS_DEBUG_TEST(swash_ptr, \
178 property_name, \
2a16ac92 179 invlist, \
c7304fe2 180 utf8_char_in_property) \
2a16ac92 181 LOAD_UTF8_CHARCLASS(swash_ptr, property_name, invlist); \
c7304fe2
KW
182 assert(swash_fetch(swash_ptr, (U8 *) utf8_char_in_property, TRUE));
183#else
184# define LOAD_UTF8_CHARCLASS_DEBUG_TEST(swash_ptr, \
185 property_name, \
2a16ac92 186 invlist, \
c7304fe2 187 utf8_char_in_property) \
2a16ac92 188 LOAD_UTF8_CHARCLASS(swash_ptr, property_name, invlist)
c7304fe2 189#endif
d1eb3177 190
c7304fe2
KW
191#define LOAD_UTF8_CHARCLASS_ALNUM() LOAD_UTF8_CHARCLASS_DEBUG_TEST( \
192 PL_utf8_swash_ptrs[_CC_WORDCHAR], \
2a16ac92
KW
193 "", \
194 PL_XPosix_ptrs[_CC_WORDCHAR], \
df38da56 195 LATIN_CAPITAL_LETTER_SHARP_S_UTF8);
c7304fe2 196
c7304fe2 197#define PLACEHOLDER /* Something for the preprocessor to grab onto */
3dab1dad
YO
198/* TODO: Combine JUMPABLE and HAS_TEXT to cache OP(rn) */
199
5f80c4cf 200/* for use after a quantifier and before an EXACT-like node -- japhy */
c35dcbe2
YO
201/* it would be nice to rework regcomp.sym to generate this stuff. sigh
202 *
203 * NOTE that *nothing* that affects backtracking should be in here, specifically
204 * VERBS must NOT be included. JUMPABLE is used to determine if we can ignore a
205 * node that is in between two EXACT like nodes when ascertaining what the required
206 * "follow" character is. This should probably be moved to regex compile time
207 * although it may be done at run time beause of the REF possibility - more
208 * investigation required. -- demerphq
209*/
baa60164
KW
210#define JUMPABLE(rn) ( \
211 OP(rn) == OPEN || \
3e901dc0 212 (OP(rn) == CLOSE && (!cur_eval || cur_eval->u.eval.close_paren != ARG(rn))) || \
baa60164
KW
213 OP(rn) == EVAL || \
214 OP(rn) == SUSPEND || OP(rn) == IFMATCH || \
215 OP(rn) == PLUS || OP(rn) == MINMOD || \
216 OP(rn) == KEEPS || \
217 (PL_regkind[OP(rn)] == CURLY && ARG1(rn) > 0) \
e2d8ce26 218)
ee9b8eae 219#define IS_EXACT(rn) (PL_regkind[OP(rn)] == EXACT)
e2d8ce26 220
ee9b8eae
YO
221#define HAS_TEXT(rn) ( IS_EXACT(rn) || PL_regkind[OP(rn)] == REF )
222
223#if 0
224/* Currently these are only used when PL_regkind[OP(rn)] == EXACT so
a4525e78 225 we don't need this definition. XXX These are now out-of-sync*/
ee9b8eae 226#define IS_TEXT(rn) ( OP(rn)==EXACT || OP(rn)==REF || OP(rn)==NREF )
098b07d5 227#define IS_TEXTF(rn) ( OP(rn)==EXACTFU || OP(rn)==EXACTFU_SS || OP(rn)==EXACTFA || OP(rn)==EXACTFA_NO_TRIE || OP(rn)==EXACTF || OP(rn)==REFF || OP(rn)==NREFF )
ee9b8eae
YO
228#define IS_TEXTFL(rn) ( OP(rn)==EXACTFL || OP(rn)==REFFL || OP(rn)==NREFFL )
229
230#else
231/* ... so we use this as its faster. */
a4525e78
KW
232#define IS_TEXT(rn) ( OP(rn)==EXACT || OP(rn)==EXACTL )
233#define IS_TEXTFU(rn) ( OP(rn)==EXACTFU || OP(rn)==EXACTFLU8 || OP(rn)==EXACTFU_SS || OP(rn) == EXACTFA || OP(rn) == EXACTFA_NO_TRIE)
ee9b8eae
YO
234#define IS_TEXTF(rn) ( OP(rn)==EXACTF )
235#define IS_TEXTFL(rn) ( OP(rn)==EXACTFL )
236
237#endif
e2d8ce26 238
a84d97b6
HS
239/*
240 Search for mandatory following text node; for lookahead, the text must
241 follow but for lookbehind (rn->flags != 0) we skip to the next step.
242*/
baa60164 243#define FIND_NEXT_IMPT(rn) STMT_START { \
3dab1dad
YO
244 while (JUMPABLE(rn)) { \
245 const OPCODE type = OP(rn); \
246 if (type == SUSPEND || PL_regkind[type] == CURLY) \
e2d8ce26 247 rn = NEXTOPER(NEXTOPER(rn)); \
3dab1dad 248 else if (type == PLUS) \
cca55fe3 249 rn = NEXTOPER(rn); \
3dab1dad 250 else if (type == IFMATCH) \
a84d97b6 251 rn = (rn->flags == 0) ? NEXTOPER(NEXTOPER(rn)) : rn + ARG(rn); \
e2d8ce26 252 else rn += NEXT_OFF(rn); \
3dab1dad 253 } \
5f80c4cf 254} STMT_END
74750237 255
006f26b2
DM
256#define SLAB_FIRST(s) (&(s)->states[0])
257#define SLAB_LAST(s) (&(s)->states[PERL_REGMATCH_SLAB_SLOTS-1])
258
a75351a1 259static void S_setup_eval_state(pTHX_ regmatch_info *const reginfo);
bf2039a9 260static void S_cleanup_regmatch_info_aux(pTHX_ void *arg);
bf2039a9 261static regmatch_state * S_push_slab(pTHX);
51371543 262
87c0511b 263#define REGCP_PAREN_ELEMS 3
f067efbf 264#define REGCP_OTHER_ELEMS 3
e0fa7e2b 265#define REGCP_FRAME_ELEMS 1
620d5b66
NC
266/* REGCP_FRAME_ELEMS are not part of the REGCP_OTHER_ELEMS and
267 * are needed for the regexp context stack bookkeeping. */
268
76e3520e 269STATIC CHECKPOINT
92da3157 270S_regcppush(pTHX_ const regexp *rex, I32 parenfloor, U32 maxopenparen)
a0d0e21e 271{
a3b680e6 272 const int retval = PL_savestack_ix;
92da3157
DM
273 const int paren_elems_to_push =
274 (maxopenparen - parenfloor) * REGCP_PAREN_ELEMS;
e0fa7e2b
NC
275 const UV total_elems = paren_elems_to_push + REGCP_OTHER_ELEMS;
276 const UV elems_shifted = total_elems << SAVE_TIGHT_SHIFT;
87c0511b 277 I32 p;
40a82448 278 GET_RE_DEBUG_FLAGS_DECL;
a0d0e21e 279
b93070ed
DM
280 PERL_ARGS_ASSERT_REGCPPUSH;
281
e49a9654 282 if (paren_elems_to_push < 0)
e8a85d26
JH
283 Perl_croak(aTHX_ "panic: paren_elems_to_push, %i < 0, maxopenparen: %i parenfloor: %i REGCP_PAREN_ELEMS: %u",
284 (int)paren_elems_to_push, (int)maxopenparen,
285 (int)parenfloor, (unsigned)REGCP_PAREN_ELEMS);
e49a9654 286
e0fa7e2b
NC
287 if ((elems_shifted >> SAVE_TIGHT_SHIFT) != total_elems)
288 Perl_croak(aTHX_ "panic: paren_elems_to_push offset %"UVuf
5df417d0 289 " out of range (%lu-%ld)",
92da3157
DM
290 total_elems,
291 (unsigned long)maxopenparen,
292 (long)parenfloor);
e0fa7e2b 293
620d5b66 294 SSGROW(total_elems + REGCP_FRAME_ELEMS);
7f69552c 295
495f47a5 296 DEBUG_BUFFERS_r(
92da3157 297 if ((int)maxopenparen > (int)parenfloor)
495f47a5
DM
298 PerlIO_printf(Perl_debug_log,
299 "rex=0x%"UVxf" offs=0x%"UVxf": saving capture indices:\n",
300 PTR2UV(rex),
301 PTR2UV(rex->offs)
302 );
303 );
92da3157 304 for (p = parenfloor+1; p <= (I32)maxopenparen; p++) {
b1ce53c5 305/* REGCP_PARENS_ELEMS are pushed per pairs of parentheses. */
99a90e59
FC
306 SSPUSHIV(rex->offs[p].end);
307 SSPUSHIV(rex->offs[p].start);
1ca2007e 308 SSPUSHINT(rex->offs[p].start_tmp);
e7707071 309 DEBUG_BUFFERS_r(PerlIO_printf(Perl_debug_log,
495f47a5
DM
310 " \\%"UVuf": %"IVdf"(%"IVdf")..%"IVdf"\n",
311 (UV)p,
312 (IV)rex->offs[p].start,
313 (IV)rex->offs[p].start_tmp,
314 (IV)rex->offs[p].end
40a82448 315 ));
a0d0e21e 316 }
b1ce53c5 317/* REGCP_OTHER_ELEMS are pushed in any case, parentheses or no. */
92da3157 318 SSPUSHINT(maxopenparen);
b93070ed
DM
319 SSPUSHINT(rex->lastparen);
320 SSPUSHINT(rex->lastcloseparen);
e0fa7e2b 321 SSPUSHUV(SAVEt_REGCONTEXT | elems_shifted); /* Magic cookie. */
41123dfd 322
a0d0e21e
LW
323 return retval;
324}
325
c277df42 326/* These are needed since we do not localize EVAL nodes: */
ab3bbdeb
YO
327#define REGCP_SET(cp) \
328 DEBUG_STATE_r( \
ab3bbdeb 329 PerlIO_printf(Perl_debug_log, \
e4f74956 330 " Setting an EVAL scope, savestack=%"IVdf"\n", \
ab3bbdeb
YO
331 (IV)PL_savestack_ix)); \
332 cp = PL_savestack_ix
c3464db5 333
ab3bbdeb 334#define REGCP_UNWIND(cp) \
e4f74956 335 DEBUG_STATE_r( \
ab3bbdeb 336 if (cp != PL_savestack_ix) \
e4f74956
YO
337 PerlIO_printf(Perl_debug_log, \
338 " Clearing an EVAL scope, savestack=%"IVdf"..%"IVdf"\n", \
ab3bbdeb
YO
339 (IV)(cp), (IV)PL_savestack_ix)); \
340 regcpblow(cp)
c277df42 341
a8d1f4b4
DM
342#define UNWIND_PAREN(lp, lcp) \
343 for (n = rex->lastparen; n > lp; n--) \
344 rex->offs[n].end = -1; \
345 rex->lastparen = n; \
346 rex->lastcloseparen = lcp;
347
348
f067efbf 349STATIC void
92da3157 350S_regcppop(pTHX_ regexp *rex, U32 *maxopenparen_p)
a0d0e21e 351{
e0fa7e2b 352 UV i;
87c0511b 353 U32 paren;
a3621e74
YO
354 GET_RE_DEBUG_FLAGS_DECL;
355
7918f24d
NC
356 PERL_ARGS_ASSERT_REGCPPOP;
357
b1ce53c5 358 /* Pop REGCP_OTHER_ELEMS before the parentheses loop starts. */
c6bf6a65 359 i = SSPOPUV;
e0fa7e2b
NC
360 assert((i & SAVE_MASK) == SAVEt_REGCONTEXT); /* Check that the magic cookie is there. */
361 i >>= SAVE_TIGHT_SHIFT; /* Parentheses elements to pop. */
b93070ed
DM
362 rex->lastcloseparen = SSPOPINT;
363 rex->lastparen = SSPOPINT;
92da3157 364 *maxopenparen_p = SSPOPINT;
b1ce53c5 365
620d5b66 366 i -= REGCP_OTHER_ELEMS;
b1ce53c5 367 /* Now restore the parentheses context. */
495f47a5
DM
368 DEBUG_BUFFERS_r(
369 if (i || rex->lastparen + 1 <= rex->nparens)
370 PerlIO_printf(Perl_debug_log,
371 "rex=0x%"UVxf" offs=0x%"UVxf": restoring capture indices to:\n",
372 PTR2UV(rex),
373 PTR2UV(rex->offs)
374 );
375 );
92da3157 376 paren = *maxopenparen_p;
620d5b66 377 for ( ; i > 0; i -= REGCP_PAREN_ELEMS) {
ea3daa5d 378 SSize_t tmps;
1ca2007e 379 rex->offs[paren].start_tmp = SSPOPINT;
99a90e59
FC
380 rex->offs[paren].start = SSPOPIV;
381 tmps = SSPOPIV;
b93070ed
DM
382 if (paren <= rex->lastparen)
383 rex->offs[paren].end = tmps;
495f47a5
DM
384 DEBUG_BUFFERS_r( PerlIO_printf(Perl_debug_log,
385 " \\%"UVuf": %"IVdf"(%"IVdf")..%"IVdf"%s\n",
386 (UV)paren,
387 (IV)rex->offs[paren].start,
388 (IV)rex->offs[paren].start_tmp,
389 (IV)rex->offs[paren].end,
390 (paren > rex->lastparen ? "(skipped)" : ""));
c277df42 391 );
87c0511b 392 paren--;
a0d0e21e 393 }
daf18116 394#if 1
dafc8851
JH
395 /* It would seem that the similar code in regtry()
396 * already takes care of this, and in fact it is in
397 * a better location to since this code can #if 0-ed out
398 * but the code in regtry() is needed or otherwise tests
399 * requiring null fields (pat.t#187 and split.t#{13,14}
daf18116
JH
400 * (as of patchlevel 7877) will fail. Then again,
401 * this code seems to be necessary or otherwise
225593e1
DM
402 * this erroneously leaves $1 defined: "1" =~ /^(?:(\d)x)?\d$/
403 * --jhi updated by dapm */
b93070ed 404 for (i = rex->lastparen + 1; i <= rex->nparens; i++) {
92da3157 405 if (i > *maxopenparen_p)
b93070ed
DM
406 rex->offs[i].start = -1;
407 rex->offs[i].end = -1;
495f47a5
DM
408 DEBUG_BUFFERS_r( PerlIO_printf(Perl_debug_log,
409 " \\%"UVuf": %s ..-1 undeffing\n",
410 (UV)i,
92da3157 411 (i > *maxopenparen_p) ? "-1" : " "
495f47a5 412 ));
a0d0e21e 413 }
dafc8851 414#endif
a0d0e21e
LW
415}
416
74088413
DM
417/* restore the parens and associated vars at savestack position ix,
418 * but without popping the stack */
419
420STATIC void
92da3157 421S_regcp_restore(pTHX_ regexp *rex, I32 ix, U32 *maxopenparen_p)
74088413
DM
422{
423 I32 tmpix = PL_savestack_ix;
424 PL_savestack_ix = ix;
92da3157 425 regcppop(rex, maxopenparen_p);
74088413
DM
426 PL_savestack_ix = tmpix;
427}
428
02db2b7b 429#define regcpblow(cp) LEAVE_SCOPE(cp) /* Ignores regcppush()ed data. */
a0d0e21e 430
31c7f561
KW
431STATIC bool
432S_isFOO_lc(pTHX_ const U8 classnum, const U8 character)
433{
434 /* Returns a boolean as to whether or not 'character' is a member of the
435 * Posix character class given by 'classnum' that should be equivalent to a
436 * value in the typedef '_char_class_number'.
437 *
438 * Ideally this could be replaced by a just an array of function pointers
439 * to the C library functions that implement the macros this calls.
440 * However, to compile, the precise function signatures are required, and
441 * these may vary from platform to to platform. To avoid having to figure
442 * out what those all are on each platform, I (khw) am using this method,
7aee35ff
KW
443 * which adds an extra layer of function call overhead (unless the C
444 * optimizer strips it away). But we don't particularly care about
445 * performance with locales anyway. */
31c7f561
KW
446
447 switch ((_char_class_number) classnum) {
15861f94 448 case _CC_ENUM_ALPHANUMERIC: return isALPHANUMERIC_LC(character);
31c7f561 449 case _CC_ENUM_ALPHA: return isALPHA_LC(character);
e8d596e0
KW
450 case _CC_ENUM_ASCII: return isASCII_LC(character);
451 case _CC_ENUM_BLANK: return isBLANK_LC(character);
b0d691b2
KW
452 case _CC_ENUM_CASED: return isLOWER_LC(character)
453 || isUPPER_LC(character);
e8d596e0 454 case _CC_ENUM_CNTRL: return isCNTRL_LC(character);
31c7f561
KW
455 case _CC_ENUM_DIGIT: return isDIGIT_LC(character);
456 case _CC_ENUM_GRAPH: return isGRAPH_LC(character);
457 case _CC_ENUM_LOWER: return isLOWER_LC(character);
458 case _CC_ENUM_PRINT: return isPRINT_LC(character);
459 case _CC_ENUM_PUNCT: return isPUNCT_LC(character);
e8d596e0 460 case _CC_ENUM_SPACE: return isSPACE_LC(character);
31c7f561
KW
461 case _CC_ENUM_UPPER: return isUPPER_LC(character);
462 case _CC_ENUM_WORDCHAR: return isWORDCHAR_LC(character);
31c7f561 463 case _CC_ENUM_XDIGIT: return isXDIGIT_LC(character);
31c7f561
KW
464 default: /* VERTSPACE should never occur in locales */
465 Perl_croak(aTHX_ "panic: isFOO_lc() has an unexpected character class '%d'", classnum);
466 }
467
e5964223 468 NOT_REACHED; /* NOTREACHED */
31c7f561
KW
469 return FALSE;
470}
471
3018b823
KW
472STATIC bool
473S_isFOO_utf8_lc(pTHX_ const U8 classnum, const U8* character)
474{
475 /* Returns a boolean as to whether or not the (well-formed) UTF-8-encoded
476 * 'character' is a member of the Posix character class given by 'classnum'
477 * that should be equivalent to a value in the typedef
478 * '_char_class_number'.
479 *
480 * This just calls isFOO_lc on the code point for the character if it is in
2f306ab9 481 * the range 0-255. Outside that range, all characters use Unicode
3018b823
KW
482 * rules, ignoring any locale. So use the Unicode function if this class
483 * requires a swash, and use the Unicode macro otherwise. */
484
485 PERL_ARGS_ASSERT_ISFOO_UTF8_LC;
486
487 if (UTF8_IS_INVARIANT(*character)) {
488 return isFOO_lc(classnum, *character);
489 }
490 else if (UTF8_IS_DOWNGRADEABLE_START(*character)) {
491 return isFOO_lc(classnum,
94bb8c36 492 TWO_BYTE_UTF8_TO_NATIVE(*character, *(character + 1)));
3018b823
KW
493 }
494
613abc6d
KW
495 _CHECK_AND_OUTPUT_WIDE_LOCALE_UTF8_MSG(character, character + UTF8SKIP(character));
496
3018b823
KW
497 if (classnum < _FIRST_NON_SWASH_CC) {
498
499 /* Initialize the swash unless done already */
500 if (! PL_utf8_swash_ptrs[classnum]) {
501 U8 flags = _CORE_SWASH_INIT_ACCEPT_INVLIST;
2a16ac92
KW
502 PL_utf8_swash_ptrs[classnum] =
503 _core_swash_init("utf8",
504 "",
505 &PL_sv_undef, 1, 0,
506 PL_XPosix_ptrs[classnum], &flags);
3018b823
KW
507 }
508
92a2046b
KW
509 return cBOOL(swash_fetch(PL_utf8_swash_ptrs[classnum], (U8 *)
510 character,
511 TRUE /* is UTF */ ));
3018b823
KW
512 }
513
514 switch ((_char_class_number) classnum) {
779cf272 515 case _CC_ENUM_SPACE: return is_XPERLSPACE_high(character);
3018b823
KW
516 case _CC_ENUM_BLANK: return is_HORIZWS_high(character);
517 case _CC_ENUM_XDIGIT: return is_XDIGIT_high(character);
518 case _CC_ENUM_VERTSPACE: return is_VERTWS_high(character);
e1ee3960 519 default: break;
3018b823
KW
520 }
521
e1ee3960 522 return FALSE; /* Things like CNTRL are always below 256 */
3018b823
KW
523}
524
a687059c 525/*
e50aee73 526 * pregexec and friends
a687059c
LW
527 */
528
76234dfb 529#ifndef PERL_IN_XSUB_RE
a687059c 530/*
c277df42 531 - pregexec - match a regexp against a string
a687059c 532 */
c277df42 533I32
5aaab254 534Perl_pregexec(pTHX_ REGEXP * const prog, char* stringarg, char *strend,
ea3daa5d 535 char *strbeg, SSize_t minend, SV *screamer, U32 nosave)
8fd1a950
DM
536/* stringarg: the point in the string at which to begin matching */
537/* strend: pointer to null at end of string */
538/* strbeg: real beginning of string */
539/* minend: end of match must be >= minend bytes after stringarg. */
540/* screamer: SV being matched: only used for utf8 flag, pos() etc; string
541 * itself is accessed via the pointers above */
542/* nosave: For optimizations. */
c277df42 543{
7918f24d
NC
544 PERL_ARGS_ASSERT_PREGEXEC;
545
c277df42 546 return
9041c2e3 547 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
c277df42
IZ
548 nosave ? 0 : REXEC_COPY_STR);
549}
76234dfb 550#endif
22e551b9 551
cad2e5aa 552
6eb5f6b9 553
1a4edc3c
DM
554/* re_intuit_start():
555 *
556 * Based on some optimiser hints, try to find the earliest position in the
557 * string where the regex could match.
558 *
559 * rx: the regex to match against
560 * sv: the SV being matched: only used for utf8 flag; the string
561 * itself is accessed via the pointers below. Note that on
562 * something like an overloaded SV, SvPOK(sv) may be false
563 * and the string pointers may point to something unrelated to
564 * the SV itself.
565 * strbeg: real beginning of string
566 * strpos: the point in the string at which to begin matching
567 * strend: pointer to the byte following the last char of the string
568 * flags currently unused; set to 0
569 * data: currently unused; set to NULL
570 *
571 * The basic idea of re_intuit_start() is to use some known information
572 * about the pattern, namely:
573 *
574 * a) the longest known anchored substring (i.e. one that's at a
575 * constant offset from the beginning of the pattern; but not
576 * necessarily at a fixed offset from the beginning of the
577 * string);
578 * b) the longest floating substring (i.e. one that's not at a constant
579 * offset from the beginning of the pattern);
580 * c) Whether the pattern is anchored to the string; either
581 * an absolute anchor: /^../, or anchored to \n: /^.../m,
582 * or anchored to pos(): /\G/;
583 * d) A start class: a real or synthetic character class which
584 * represents which characters are legal at the start of the pattern;
585 *
586 * to either quickly reject the match, or to find the earliest position
587 * within the string at which the pattern might match, thus avoiding
588 * running the full NFA engine at those earlier locations, only to
589 * eventually fail and retry further along.
590 *
591 * Returns NULL if the pattern can't match, or returns the address within
592 * the string which is the earliest place the match could occur.
593 *
594 * The longest of the anchored and floating substrings is called 'check'
595 * and is checked first. The other is called 'other' and is checked
596 * second. The 'other' substring may not be present. For example,
597 *
598 * /(abc|xyz)ABC\d{0,3}DEFG/
599 *
600 * will have
601 *
602 * check substr (float) = "DEFG", offset 6..9 chars
603 * other substr (anchored) = "ABC", offset 3..3 chars
604 * stclass = [ax]
605 *
606 * Be aware that during the course of this function, sometimes 'anchored'
607 * refers to a substring being anchored relative to the start of the
608 * pattern, and sometimes to the pattern itself being anchored relative to
609 * the string. For example:
610 *
611 * /\dabc/: "abc" is anchored to the pattern;
612 * /^\dabc/: "abc" is anchored to the pattern and the string;
613 * /\d+abc/: "abc" is anchored to neither the pattern nor the string;
614 * /^\d+abc/: "abc" is anchored to neither the pattern nor the string,
615 * but the pattern is anchored to the string.
52a21eb3
DM
616 */
617
cad2e5aa 618char *
52a21eb3
DM
619Perl_re_intuit_start(pTHX_
620 REGEXP * const rx,
621 SV *sv,
622 const char * const strbeg,
623 char *strpos,
624 char *strend,
625 const U32 flags,
626 re_scream_pos_data *data)
cad2e5aa 627{
8d919b0a 628 struct regexp *const prog = ReANY(rx);
6b071d16 629 SSize_t start_shift = prog->check_offset_min;
cad2e5aa 630 /* Should be nonnegative! */
ea3daa5d 631 SSize_t end_shift = 0;
0fc004dd
DM
632 /* current lowest pos in string where the regex can start matching */
633 char *rx_origin = strpos;
eb578fdb 634 SV *check;
f2ed9b32 635 const bool utf8_target = (sv && SvUTF8(sv)) ? 1 : 0; /* if no sv we have to assume bytes */
6480a6c4 636 U8 other_ix = 1 - prog->substrs->check_ix;
6ad5ffb3 637 bool ml_anch = 0;
8f4bf5fc 638 char *other_last = strpos;/* latest pos 'other' substr already checked to */
bd61b366 639 char *check_at = NULL; /* check substr found at this pos */
bbe252da 640 const I32 multiline = prog->extflags & RXf_PMf_MULTILINE;
f8fc2ecf 641 RXi_GET_DECL(prog,progi);
02d5137b
DM
642 regmatch_info reginfo_buf; /* create some info to pass to find_byclass */
643 regmatch_info *const reginfo = &reginfo_buf;
a3621e74
YO
644 GET_RE_DEBUG_FLAGS_DECL;
645
7918f24d 646 PERL_ARGS_ASSERT_RE_INTUIT_START;
c33e64f0
FC
647 PERL_UNUSED_ARG(flags);
648 PERL_UNUSED_ARG(data);
7918f24d 649
1dc475d0
DM
650 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
651 "Intuit: trying to determine minimum start position...\n"));
652
fb9bbddb
DM
653 /* for now, assume that all substr offsets are positive. If at some point
654 * in the future someone wants to do clever things with look-behind and
655 * -ve offsets, they'll need to fix up any code in this function
656 * which uses these offsets. See the thread beginning
657 * <20140113145929.GF27210@iabyn.com>
658 */
659 assert(prog->substrs->data[0].min_offset >= 0);
660 assert(prog->substrs->data[0].max_offset >= 0);
661 assert(prog->substrs->data[1].min_offset >= 0);
662 assert(prog->substrs->data[1].max_offset >= 0);
663 assert(prog->substrs->data[2].min_offset >= 0);
664 assert(prog->substrs->data[2].max_offset >= 0);
665
f7022b5a 666 /* for now, assume that if both present, that the floating substring
83f2232d 667 * doesn't start before the anchored substring.
f7022b5a
DM
668 * If you break this assumption (e.g. doing better optimisations
669 * with lookahead/behind), then you'll need to audit the code in this
670 * function carefully first
671 */
672 assert(
673 ! ( (prog->anchored_utf8 || prog->anchored_substr)
674 && (prog->float_utf8 || prog->float_substr))
675 || (prog->float_min_offset >= prog->anchored_offset));
676
1a4edc3c
DM
677 /* byte rather than char calculation for efficiency. It fails
678 * to quickly reject some cases that can't match, but will reject
679 * them later after doing full char arithmetic */
c344f387 680 if (prog->minlen > strend - strpos) {
a3621e74 681 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1dc475d0 682 " String too short...\n"));
cad2e5aa 683 goto fail;
2c2d71f5 684 }
d8da0584 685
ab4e48c1 686 RX_MATCH_UTF8_set(rx,utf8_target);
6c3fea77 687 reginfo->is_utf8_target = cBOOL(utf8_target);
bf2039a9 688 reginfo->info_aux = NULL;
9d9163fb 689 reginfo->strbeg = strbeg;
220db18a 690 reginfo->strend = strend;
aed7b151 691 reginfo->is_utf8_pat = cBOOL(RX_UTF8(rx));
02d5137b 692 reginfo->intuit = 1;
1cb48e53
DM
693 /* not actually used within intuit, but zero for safety anyway */
694 reginfo->poscache_maxiter = 0;
02d5137b 695
f2ed9b32 696 if (utf8_target) {
33b8afdf
JH
697 if (!prog->check_utf8 && prog->check_substr)
698 to_utf8_substr(prog);
699 check = prog->check_utf8;
700 } else {
7e0d5ad7
KW
701 if (!prog->check_substr && prog->check_utf8) {
702 if (! to_byte_substr(prog)) {
6b54ddc5 703 NON_UTF8_TARGET_BUT_UTF8_REQUIRED(fail);
7e0d5ad7
KW
704 }
705 }
33b8afdf
JH
706 check = prog->check_substr;
707 }
274cd312 708
1dc475d0
DM
709 /* dump the various substring data */
710 DEBUG_OPTIMISE_MORE_r({
711 int i;
712 for (i=0; i<=2; i++) {
713 SV *sv = (utf8_target ? prog->substrs->data[i].utf8_substr
714 : prog->substrs->data[i].substr);
715 if (!sv)
716 continue;
717
718 PerlIO_printf(Perl_debug_log,
719 " substrs[%d]: min=%"IVdf" max=%"IVdf" end shift=%"IVdf
720 " useful=%"IVdf" utf8=%d [%s]\n",
721 i,
722 (IV)prog->substrs->data[i].min_offset,
723 (IV)prog->substrs->data[i].max_offset,
724 (IV)prog->substrs->data[i].end_shift,
725 BmUSEFUL(sv),
726 utf8_target ? 1 : 0,
727 SvPEEK(sv));
728 }
729 });
730
8e1490ee 731 if (prog->intflags & PREGf_ANCH) { /* Match at \G, beg-of-str or after \n */
9fc7410e
DM
732
733 /* ml_anch: check after \n?
734 *
0fa70a06 735 * A note about PREGf_IMPLICIT: on an un-anchored pattern beginning
9fc7410e
DM
736 * with /.*.../, these flags will have been added by the
737 * compiler:
738 * /.*abc/, /.*abc/m: PREGf_IMPLICIT | PREGf_ANCH_MBOL
739 * /.*abc/s: PREGf_IMPLICIT | PREGf_ANCH_SBOL
740 */
7d2d37f5
DM
741 ml_anch = (prog->intflags & PREGf_ANCH_MBOL)
742 && !(prog->intflags & PREGf_IMPLICIT);
cad2e5aa 743
343c8a29 744 if (!ml_anch && !(prog->intflags & PREGf_IMPLICIT)) {
c889ccc8
DM
745 /* we are only allowed to match at BOS or \G */
746
57fcbfa7 747 /* trivially reject if there's a BOS anchor and we're not at BOS.
7bb3b9eb
DM
748 *
749 * Note that we don't try to do a similar quick reject for
750 * \G, since generally the caller will have calculated strpos
751 * based on pos() and gofs, so the string is already correctly
752 * anchored by definition; and handling the exceptions would
753 * be too fiddly (e.g. REXEC_IGNOREPOS).
57fcbfa7 754 */
7bb3b9eb 755 if ( strpos != strbeg
d3d47aac 756 && (prog->intflags & PREGf_ANCH_SBOL))
c889ccc8
DM
757 {
758 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1dc475d0 759 " Not at start...\n"));
c889ccc8
DM
760 goto fail;
761 }
762
a5d12a4b
DM
763 /* in the presence of an anchor, the anchored (relative to the
764 * start of the regex) substr must also be anchored relative
66b7ec5c
DM
765 * to strpos. So quickly reject if substr isn't found there.
766 * This works for \G too, because the caller will already have
767 * subtracted gofs from pos, and gofs is the offset from the
768 * \G to the start of the regex. For example, in /.abc\Gdef/,
769 * where substr="abcdef", pos()=3, gofs=4, offset_min=1:
770 * caller will have set strpos=pos()-4; we look for the substr
771 * at position pos()-4+1, which lines up with the "a" */
a5d12a4b 772
33c28ab2 773 if (prog->check_offset_min == prog->check_offset_max) {
c889ccc8 774 /* Substring at constant offset from beg-of-str... */
d307bf57 775 SSize_t slen = SvCUR(check);
343c8a29 776 char *s = HOP3c(strpos, prog->check_offset_min, strend);
1de06328 777
1dc475d0
DM
778 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
779 " Looking for check substr at fixed offset %"IVdf"...\n",
780 (IV)prog->check_offset_min));
781
7742aa66
DM
782 if (SvTAIL(check)) {
783 /* In this case, the regex is anchored at the end too.
784 * Unless it's a multiline match, the lengths must match
785 * exactly, give or take a \n. NB: slen >= 1 since
786 * the last char of check is \n */
787 if (!multiline
788 && ( strend - s > slen
789 || strend - s < slen - 1
790 || (strend - s == slen && strend[-1] != '\n')))
c889ccc8
DM
791 {
792 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1dc475d0 793 " String too long...\n"));
c889ccc8
DM
794 goto fail_finish;
795 }
796 /* Now should match s[0..slen-2] */
797 slen--;
c889ccc8 798 }
d307bf57
DM
799 if (slen && (*SvPVX_const(check) != *s
800 || (slen > 1 && memNE(SvPVX_const(check), s, slen))))
801 {
802 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1dc475d0 803 " String not equal...\n"));
d307bf57
DM
804 goto fail_finish;
805 }
c889ccc8
DM
806
807 check_at = s;
808 goto success_at_start;
cad2e5aa 809 }
cad2e5aa 810 }
cad2e5aa 811 }
0fc004dd 812
c0e0ec46 813 end_shift = prog->check_end_shift;
cad2e5aa 814
19188028 815#ifdef DEBUGGING /* 7/99: reports of failure (with the older version) */
0033605d 816 if (end_shift < 0)
1de06328 817 Perl_croak(aTHX_ "panic: end_shift: %"IVdf" pattern:\n%s\n ",
220fc49f 818 (IV)end_shift, RX_PRECOMP(prog));
2c2d71f5
JH
819#endif
820
2c2d71f5 821 restart:
1de06328 822
66b7ec5c
DM
823 /* This is the (re)entry point of the main loop in this function.
824 * The goal of this loop is to:
825 * 1) find the "check" substring in the region rx_origin..strend
826 * (adjusted by start_shift / end_shift). If not found, reject
827 * immediately.
828 * 2) If it exists, look for the "other" substr too if defined; for
829 * example, if the check substr maps to the anchored substr, then
830 * check the floating substr, and vice-versa. If not found, go
831 * back to (1) with rx_origin suitably incremented.
832 * 3) If we find an rx_origin position that doesn't contradict
833 * either of the substrings, then check the possible additional
834 * constraints on rx_origin of /^.../m or a known start class.
835 * If these fail, then depending on which constraints fail, jump
836 * back to here, or to various other re-entry points further along
837 * that skip some of the first steps.
838 * 4) If we pass all those tests, update the BmUSEFUL() count on the
839 * substring. If the start position was determined to be at the
840 * beginning of the string - so, not rejected, but not optimised,
841 * since we have to run regmatch from position 0 - decrement the
842 * BmUSEFUL() count. Otherwise increment it.
843 */
844
1a4edc3c
DM
845
846 /* first, look for the 'check' substring */
847
1de06328 848 {
c33e64f0
FC
849 U8* start_point;
850 U8* end_point;
c889ccc8 851
c889ccc8 852 DEBUG_OPTIMISE_MORE_r({
1dc475d0 853 PerlIO_printf(Perl_debug_log,
ae5d4331 854 " At restart: rx_origin=%"IVdf" Check offset min: %"IVdf
1dc475d0 855 " Start shift: %"IVdf" End shift %"IVdf
4d281893 856 " Real end Shift: %"IVdf"\n",
675e93ee 857 (IV)(rx_origin - strbeg),
c889ccc8 858 (IV)prog->check_offset_min,
12fbc530
DM
859 (IV)start_shift,
860 (IV)end_shift,
c889ccc8
DM
861 (IV)prog->check_end_shift);
862 });
1de06328 863
33c28ab2
DM
864 end_point = HOP3(strend, -end_shift, strbeg);
865 start_point = HOPMAYBE3(rx_origin, start_shift, end_point);
866 if (!start_point)
867 goto fail_finish;
c889ccc8 868
557f47af 869
e0362b86 870 /* If the regex is absolutely anchored to either the start of the
d3d47aac 871 * string (SBOL) or to pos() (ANCH_GPOS), then
e0362b86
DM
872 * check_offset_max represents an upper bound on the string where
873 * the substr could start. For the ANCH_GPOS case, we assume that
874 * the caller of intuit will have already set strpos to
875 * pos()-gofs, so in this case strpos + offset_max will still be
876 * an upper bound on the substr.
877 */
c19c836a
DM
878 if (!ml_anch
879 && prog->intflags & PREGf_ANCH
e0362b86 880 && prog->check_offset_max != SSize_t_MAX)
c19c836a 881 {
1a08ba3a 882 SSize_t len = SvCUR(check) - !!SvTAIL(check);
e0362b86
DM
883 const char * const anchor =
884 (prog->intflags & PREGf_ANCH_GPOS ? strpos : strbeg);
885
886 /* do a bytes rather than chars comparison. It's conservative;
887 * so it skips doing the HOP if the result can't possibly end
888 * up earlier than the old value of end_point.
889 */
890 if ((char*)end_point - anchor > prog->check_offset_max) {
891 end_point = HOP3lim((U8*)anchor,
892 prog->check_offset_max,
893 end_point -len)
894 + len;
895 }
d6ef1678
DM
896 }
897
ae5d4331 898 check_at = fbm_instr( start_point, end_point,
7fba1cd6 899 check, multiline ? FBMrf_MULTILINE : 0);
c889ccc8 900
675e93ee
DM
901 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
902 " doing 'check' fbm scan, [%"IVdf"..%"IVdf"] gave %"IVdf"\n",
903 (IV)((char*)start_point - strbeg),
904 (IV)((char*)end_point - strbeg),
905 (IV)(check_at ? check_at - strbeg : -1)
906 ));
907
8fd34720
DM
908 /* Update the count-of-usability, remove useless subpatterns,
909 unshift s. */
910
911 DEBUG_EXECUTE_r({
912 RE_PV_QUOTED_DECL(quoted, utf8_target, PERL_DEBUG_PAD_ZERO(0),
913 SvPVX_const(check), RE_SV_DUMPLEN(check), 30);
914 PerlIO_printf(Perl_debug_log, " %s %s substr %s%s%s",
915 (check_at ? "Found" : "Did not find"),
916 (check == (utf8_target ? prog->anchored_utf8 : prog->anchored_substr)
917 ? "anchored" : "floating"),
918 quoted,
919 RE_SV_TAIL(check),
920 (check_at ? " at offset " : "...\n") );
921 });
2c2d71f5 922
8fd34720
DM
923 if (!check_at)
924 goto fail_finish;
8fd34720
DM
925 /* set rx_origin to the minimum position where the regex could start
926 * matching, given the constraint of the just-matched check substring.
927 * But don't set it lower than previously.
928 */
fdc003fd 929
8fd34720
DM
930 if (check_at - rx_origin > prog->check_offset_max)
931 rx_origin = HOP3c(check_at, -prog->check_offset_max, rx_origin);
675e93ee
DM
932 /* Finish the diagnostic message */
933 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
934 "%ld (rx_origin now %"IVdf")...\n",
935 (long)(check_at - strbeg),
936 (IV)(rx_origin - strbeg)
937 ));
8fd34720 938 }
fdc003fd
DM
939
940
1a4edc3c 941 /* now look for the 'other' substring if defined */
2c2d71f5 942
6480a6c4
DM
943 if (utf8_target ? prog->substrs->data[other_ix].utf8_substr
944 : prog->substrs->data[other_ix].substr)
1de06328 945 {
30944b6d 946 /* Take into account the "other" substring. */
6c3343a6
DM
947 char *last, *last1;
948 char *s;
949 SV* must;
950 struct reg_substr_datum *other;
951
952 do_other_substr:
953 other = &prog->substrs->data[other_ix];
954
955 /* if "other" is anchored:
956 * we've previously found a floating substr starting at check_at.
957 * This means that the regex origin must lie somewhere
958 * between min (rx_origin): HOP3(check_at, -check_offset_max)
959 * and max: HOP3(check_at, -check_offset_min)
960 * (except that min will be >= strpos)
961 * So the fixed substr must lie somewhere between
962 * HOP3(min, anchored_offset)
963 * HOP3(max, anchored_offset) + SvCUR(substr)
964 */
965
966 /* if "other" is floating
967 * Calculate last1, the absolute latest point where the
968 * floating substr could start in the string, ignoring any
969 * constraints from the earlier fixed match. It is calculated
970 * as follows:
971 *
972 * strend - prog->minlen (in chars) is the absolute latest
973 * position within the string where the origin of the regex
974 * could appear. The latest start point for the floating
975 * substr is float_min_offset(*) on from the start of the
976 * regex. last1 simply combines thee two offsets.
977 *
978 * (*) You might think the latest start point should be
979 * float_max_offset from the regex origin, and technically
980 * you'd be correct. However, consider
981 * /a\d{2,4}bcd\w/
982 * Here, float min, max are 3,5 and minlen is 7.
983 * This can match either
984 * /a\d\dbcd\w/
985 * /a\d\d\dbcd\w/
986 * /a\d\d\d\dbcd\w/
987 * In the first case, the regex matches minlen chars; in the
988 * second, minlen+1, in the third, minlen+2.
989 * In the first case, the floating offset is 3 (which equals
990 * float_min), in the second, 4, and in the third, 5 (which
991 * equals float_max). In all cases, the floating string bcd
992 * can never start more than 4 chars from the end of the
993 * string, which equals minlen - float_min. As the substring
994 * starts to match more than float_min from the start of the
995 * regex, it makes the regex match more than minlen chars,
996 * and the two cancel each other out. So we can always use
997 * float_min - minlen, rather than float_max - minlen for the
998 * latest position in the string.
999 *
1000 * Note that -minlen + float_min_offset is equivalent (AFAIKT)
1001 * to CHR_SVLEN(must) - !!SvTAIL(must) + prog->float_end_shift
1002 */
1003
e7a14a9c 1004 assert(prog->minlen >= other->min_offset);
6c3343a6
DM
1005 last1 = HOP3c(strend,
1006 other->min_offset - prog->minlen, strbeg);
1007
4d006249 1008 if (other_ix) {/* i.e. if (other-is-float) */
6c3343a6
DM
1009 /* last is the latest point where the floating substr could
1010 * start, *given* any constraints from the earlier fixed
1011 * match. This constraint is that the floating string starts
1012 * <= float_max_offset chars from the regex origin (rx_origin).
1013 * If this value is less than last1, use it instead.
eb3831ce 1014 */
6c3343a6
DM
1015 assert(rx_origin <= last1);
1016 last =
1017 /* this condition handles the offset==infinity case, and
1018 * is a short-cut otherwise. Although it's comparing a
1019 * byte offset to a char length, it does so in a safe way,
1020 * since 1 char always occupies 1 or more bytes,
1021 * so if a string range is (last1 - rx_origin) bytes,
1022 * it will be less than or equal to (last1 - rx_origin)
1023 * chars; meaning it errs towards doing the accurate HOP3
1024 * rather than just using last1 as a short-cut */
1025 (last1 - rx_origin) < other->max_offset
1026 ? last1
1027 : (char*)HOP3lim(rx_origin, other->max_offset, last1);
1028 }
1029 else {
1030 assert(strpos + start_shift <= check_at);
1031 last = HOP4c(check_at, other->min_offset - start_shift,
1032 strbeg, strend);
1033 }
ead917d0 1034
6c3343a6
DM
1035 s = HOP3c(rx_origin, other->min_offset, strend);
1036 if (s < other_last) /* These positions already checked */
1037 s = other_last;
1038
1039 must = utf8_target ? other->utf8_substr : other->substr;
1040 assert(SvPOK(must));
675e93ee
DM
1041 {
1042 char *from = s;
1043 char *to = last + SvCUR(must) - (SvTAIL(must)!=0);
1044
88203927
DM
1045 if (from > to) {
1046 s = NULL;
1047 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1048 " skipping 'other' fbm scan: %"IVdf" > %"IVdf"\n",
1049 (IV)(from - strbeg),
1050 (IV)(to - strbeg)
1051 ));
1052 }
1053 else {
1054 s = fbm_instr(
1055 (unsigned char*)from,
1056 (unsigned char*)to,
1057 must,
1058 multiline ? FBMrf_MULTILINE : 0
1059 );
1060 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1061 " doing 'other' fbm scan, [%"IVdf"..%"IVdf"] gave %"IVdf"\n",
1062 (IV)(from - strbeg),
1063 (IV)(to - strbeg),
1064 (IV)(s ? s - strbeg : -1)
1065 ));
1066 }
675e93ee
DM
1067 }
1068
6c3343a6
DM
1069 DEBUG_EXECUTE_r({
1070 RE_PV_QUOTED_DECL(quoted, utf8_target, PERL_DEBUG_PAD_ZERO(0),
1071 SvPVX_const(must), RE_SV_DUMPLEN(must), 30);
1072 PerlIO_printf(Perl_debug_log, " %s %s substr %s%s",
1073 s ? "Found" : "Contradicts",
1074 other_ix ? "floating" : "anchored",
1075 quoted, RE_SV_TAIL(must));
1076 });
ead917d0 1077
ead917d0 1078
6c3343a6
DM
1079 if (!s) {
1080 /* last1 is latest possible substr location. If we didn't
1081 * find it before there, we never will */
1082 if (last >= last1) {
1083 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
675e93ee 1084 "; giving up...\n"));
6c3343a6 1085 goto fail_finish;
ead917d0
DM
1086 }
1087
6c3343a6
DM
1088 /* try to find the check substr again at a later
1089 * position. Maybe next time we'll find the "other" substr
1090 * in range too */
6c3343a6
DM
1091 other_last = HOP3c(last, 1, strend) /* highest failure */;
1092 rx_origin =
4d006249 1093 other_ix /* i.e. if other-is-float */
6c3343a6
DM
1094 ? HOP3c(rx_origin, 1, strend)
1095 : HOP4c(last, 1 - other->min_offset, strbeg, strend);
675e93ee
DM
1096 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1097 "; about to retry %s at offset %ld (rx_origin now %"IVdf")...\n",
1098 (other_ix ? "floating" : "anchored"),
1099 (long)(HOP3c(check_at, 1, strend) - strbeg),
1100 (IV)(rx_origin - strbeg)
1101 ));
6c3343a6
DM
1102 goto restart;
1103 }
1104 else {
4d006249 1105 if (other_ix) { /* if (other-is-float) */
6c3343a6
DM
1106 /* other_last is set to s, not s+1, since its possible for
1107 * a floating substr to fail first time, then succeed
1108 * second time at the same floating position; e.g.:
1109 * "-AB--AABZ" =~ /\wAB\d*Z/
1110 * The first time round, anchored and float match at
1111 * "-(AB)--AAB(Z)" then fail on the initial \w character
1112 * class. Second time round, they match at "-AB--A(AB)(Z)".
1113 */
1114 other_last = s;
ead917d0
DM
1115 }
1116 else {
6c3343a6
DM
1117 rx_origin = HOP3c(s, -other->min_offset, strbeg);
1118 other_last = HOP3c(s, 1, strend);
ead917d0 1119 }
675e93ee
DM
1120 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1121 " at offset %ld (rx_origin now %"IVdf")...\n",
1122 (long)(s - strbeg),
1123 (IV)(rx_origin - strbeg)
1124 ));
1125
6c3343a6 1126 }
cad2e5aa 1127 }
acba93e8
DM
1128 else {
1129 DEBUG_OPTIMISE_MORE_r(
1130 PerlIO_printf(Perl_debug_log,
1131 " Check-only match: offset min:%"IVdf" max:%"IVdf
1c1c599d 1132 " check_at:%"IVdf" rx_origin:%"IVdf" rx_origin-check_at:%"IVdf
675e93ee 1133 " strend:%"IVdf"\n",
acba93e8
DM
1134 (IV)prog->check_offset_min,
1135 (IV)prog->check_offset_max,
675e93ee
DM
1136 (IV)(check_at-strbeg),
1137 (IV)(rx_origin-strbeg),
1c1c599d 1138 (IV)(rx_origin-check_at),
675e93ee 1139 (IV)(strend-strbeg)
acba93e8
DM
1140 )
1141 );
1142 }
2c2d71f5 1143
acba93e8 1144 postprocess_substr_matches:
0991020e 1145
1a4edc3c 1146 /* handle the extra constraint of /^.../m if present */
e3c6feb0 1147
7d2d37f5 1148 if (ml_anch && rx_origin != strbeg && rx_origin[-1] != '\n') {
4620cb61
DM
1149 char *s;
1150
a62659bd
DM
1151 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1152 " looking for /^/m anchor"));
d0880ea7
DM
1153
1154 /* we have failed the constraint of a \n before rx_origin.
2e759faa
DM
1155 * Find the next \n, if any, even if it's beyond the current
1156 * anchored and/or floating substrings. Whether we should be
1157 * scanning ahead for the next \n or the next substr is debatable.
1158 * On the one hand you'd expect rare substrings to appear less
1159 * often than \n's. On the other hand, searching for \n means
675e93ee 1160 * we're effectively flipping between check_substr and "\n" on each
2e759faa
DM
1161 * iteration as the current "rarest" string candidate, which
1162 * means for example that we'll quickly reject the whole string if
1163 * hasn't got a \n, rather than trying every substr position
1164 * first
1165 */
d0880ea7 1166
4620cb61
DM
1167 s = HOP3c(strend, - prog->minlen, strpos);
1168 if (s <= rx_origin ||
1169 ! ( rx_origin = (char *)memchr(rx_origin, '\n', s - rx_origin)))
1170 {
d0880ea7
DM
1171 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1172 " Did not find /%s^%s/m...\n",
1173 PL_colors[0], PL_colors[1]));
a62659bd
DM
1174 goto fail_finish;
1175 }
d0880ea7 1176
4ada1233
DM
1177 /* earliest possible origin is 1 char after the \n.
1178 * (since *rx_origin == '\n', it's safe to ++ here rather than
1179 * HOP(rx_origin, 1)) */
1180 rx_origin++;
d0880ea7 1181
f4f115de 1182 if (prog->substrs->check_ix == 0 /* check is anchored */
4ada1233 1183 || rx_origin >= HOP3c(check_at, - prog->check_offset_min, strpos))
f4f115de 1184 {
d0880ea7
DM
1185 /* Position contradicts check-string; either because
1186 * check was anchored (and thus has no wiggle room),
4ada1233 1187 * or check was float and rx_origin is above the float range */
d0880ea7 1188 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
675e93ee
DM
1189 " Found /%s^%s/m, about to restart lookup for check-string with rx_origin %ld...\n",
1190 PL_colors[0], PL_colors[1], (long)(rx_origin - strbeg)));
d0880ea7
DM
1191 goto restart;
1192 }
1193
1194 /* if we get here, the check substr must have been float,
2e759faa 1195 * is in range, and we may or may not have had an anchored
d0880ea7
DM
1196 * "other" substr which still contradicts */
1197 assert(prog->substrs->check_ix); /* check is float */
1198
1199 if (utf8_target ? prog->anchored_utf8 : prog->anchored_substr) {
1200 /* whoops, the anchored "other" substr exists, so we still
1201 * contradict. On the other hand, the float "check" substr
1202 * didn't contradict, so just retry the anchored "other"
1203 * substr */
1204 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
675e93ee 1205 " Found /%s^%s/m, rescanning for anchored from offset %ld (rx_origin now %"IVdf")...\n",
d0880ea7 1206 PL_colors[0], PL_colors[1],
675e93ee
DM
1207 (long)(rx_origin - strbeg + prog->anchored_offset),
1208 (long)(rx_origin - strbeg)
1209 ));
d0880ea7
DM
1210 goto do_other_substr;
1211 }
1212
1213 /* success: we don't contradict the found floating substring
1214 * (and there's no anchored substr). */
d0880ea7 1215 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
675e93ee
DM
1216 " Found /%s^%s/m with rx_origin %ld...\n",
1217 PL_colors[0], PL_colors[1], (long)(rx_origin - strbeg)));
e3c6feb0
DM
1218 }
1219 else {
2e759faa 1220 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
fe4f3442 1221 " (multiline anchor test skipped)\n"));
e3c6feb0
DM
1222 }
1223
ffad1e6a 1224 success_at_start:
e3c6feb0 1225
cad2e5aa 1226
dd170ff5
DM
1227 /* if we have a starting character class, then test that extra constraint.
1228 * (trie stclasses are too expensive to use here, we are better off to
1229 * leave it to regmatch itself) */
1230
f8fc2ecf 1231 if (progi->regstclass && PL_regkind[OP(progi->regstclass)]!=TRIE) {
f8fc2ecf 1232 const U8* const str = (U8*)STRING(progi->regstclass);
0991020e 1233
2c75e362 1234 /* XXX this value could be pre-computed */
f8fc2ecf 1235 const int cl_l = (PL_regkind[OP(progi->regstclass)] == EXACT
2c75e362
DM
1236 ? (reginfo->is_utf8_pat
1237 ? utf8_distance(str + STR_LEN(progi->regstclass), str)
1238 : STR_LEN(progi->regstclass))
66e933ab 1239 : 1);
1de06328 1240 char * endpos;
fa3bb21d 1241 char *s;
000dfd2d
DM
1242 /* latest pos that a matching float substr constrains rx start to */
1243 char *rx_max_float = NULL;
1244
c75a3985
DM
1245 /* if the current rx_origin is anchored, either by satisfying an
1246 * anchored substring constraint, or a /^.../m constraint, then we
1247 * can reject the current origin if the start class isn't found
1248 * at the current position. If we have a float-only match, then
1249 * rx_origin is constrained to a range; so look for the start class
1250 * in that range. if neither, then look for the start class in the
1251 * whole rest of the string */
1252
dd170ff5
DM
1253 /* XXX DAPM it's not clear what the minlen test is for, and why
1254 * it's not used in the floating case. Nothing in the test suite
1255 * causes minlen == 0 here. See <20140313134639.GS12844@iabyn.com>.
1256 * Here are some old comments, which may or may not be correct:
1257 *
1258 * minlen == 0 is possible if regstclass is \b or \B,
1259 * and the fixed substr is ''$.
1260 * Since minlen is already taken into account, rx_origin+1 is
1261 * before strend; accidentally, minlen >= 1 guaranties no false
1262 * positives at rx_origin + 1 even for \b or \B. But (minlen? 1 :
1263 * 0) below assumes that regstclass does not come from lookahead...
1264 * If regstclass takes bytelength more than 1: If charlength==1, OK.
1265 * This leaves EXACTF-ish only, which are dealt with in
1266 * find_byclass().
1267 */
1268
7d2d37f5 1269 if (prog->anchored_substr || prog->anchored_utf8 || ml_anch)
fa3bb21d 1270 endpos= HOP3c(rx_origin, (prog->minlen ? cl_l : 0), strend);
000dfd2d
DM
1271 else if (prog->float_substr || prog->float_utf8) {
1272 rx_max_float = HOP3c(check_at, -start_shift, strbeg);
1273 endpos= HOP3c(rx_max_float, cl_l, strend);
1274 }
1de06328
YO
1275 else
1276 endpos= strend;
1277
1dc475d0
DM
1278 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1279 " looking for class: start_shift: %"IVdf" check_at: %"IVdf
c43b5520 1280 " rx_origin: %"IVdf" endpos: %"IVdf"\n",
1dc475d0 1281 (IV)start_shift, (IV)(check_at - strbeg),
c43b5520 1282 (IV)(rx_origin - strbeg), (IV)(endpos - strbeg)));
d8080198 1283
c43b5520 1284 s = find_byclass(prog, progi->regstclass, rx_origin, endpos,
f9176b44 1285 reginfo);
be778b1a 1286 if (!s) {
6eb5f6b9 1287 if (endpos == strend) {
a3621e74 1288 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
1dc475d0 1289 " Could not match STCLASS...\n") );
6eb5f6b9
JH
1290 goto fail;
1291 }
a3621e74 1292 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
1dc475d0 1293 " This position contradicts STCLASS...\n") );
e0eb31e7
DM
1294 if ((prog->intflags & PREGf_ANCH) && !ml_anch
1295 && !(prog->intflags & PREGf_IMPLICIT))
653099ff 1296 goto fail;
9fed8d02 1297
6eb5f6b9 1298 /* Contradict one of substrings */
97136c8a
DM
1299 if (prog->anchored_substr || prog->anchored_utf8) {
1300 if (prog->substrs->check_ix == 1) { /* check is float */
1301 /* Have both, check_string is floating */
1302 assert(rx_origin + start_shift <= check_at);
1303 if (rx_origin + start_shift != check_at) {
1304 /* not at latest position float substr could match:
c75a3985
DM
1305 * Recheck anchored substring, but not floating.
1306 * The condition above is in bytes rather than
1307 * chars for efficiency. It's conservative, in
1308 * that it errs on the side of doing 'goto
88203927
DM
1309 * do_other_substr'. In this case, at worst,
1310 * an extra anchored search may get done, but in
1311 * practice the extra fbm_instr() is likely to
1312 * get skipped anyway. */
97136c8a 1313 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
675e93ee
DM
1314 " about to retry anchored at offset %ld (rx_origin now %"IVdf")...\n",
1315 (long)(other_last - strbeg),
1316 (IV)(rx_origin - strbeg)
1317 ));
97136c8a 1318 goto do_other_substr;
3369914b 1319 }
3369914b
DM
1320 }
1321 }
97136c8a 1322 else {
9fed8d02
DM
1323 /* float-only */
1324
7d2d37f5 1325 if (ml_anch) {
c75a3985
DM
1326 /* In the presence of ml_anch, we might be able to
1327 * find another \n without breaking the current float
1328 * constraint. */
1329
1330 /* strictly speaking this should be HOP3c(..., 1, ...),
1331 * but since we goto a block of code that's going to
1332 * search for the next \n if any, its safe here */
9fed8d02 1333 rx_origin++;
9fed8d02 1334 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
675e93ee 1335 " about to look for /%s^%s/m starting at rx_origin %ld...\n",
9fed8d02 1336 PL_colors[0], PL_colors[1],
675e93ee 1337 (long)(rx_origin - strbeg)) );
9fed8d02 1338 goto postprocess_substr_matches;
ab60c45a 1339 }
c75a3985
DM
1340
1341 /* strictly speaking this can never be true; but might
1342 * be if we ever allow intuit without substrings */
1343 if (!(utf8_target ? prog->float_utf8 : prog->float_substr))
9fed8d02 1344 goto fail;
c75a3985 1345
000dfd2d 1346 rx_origin = rx_max_float;
9fed8d02
DM
1347 }
1348
c75a3985
DM
1349 /* at this point, any matching substrings have been
1350 * contradicted. Start again... */
1351
9fed8d02 1352 rx_origin = HOP3c(rx_origin, 1, strend);
557f47af
DM
1353
1354 /* uses bytes rather than char calculations for efficiency.
1355 * It's conservative: it errs on the side of doing 'goto restart',
1356 * where there is code that does a proper char-based test */
9fed8d02 1357 if (rx_origin + start_shift + end_shift > strend) {
40268e5b 1358 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
9fed8d02
DM
1359 " Could not match STCLASS...\n") );
1360 goto fail;
1361 }
1362 DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log,
675e93ee 1363 " about to look for %s substr starting at offset %ld (rx_origin now %"IVdf")...\n",
9fed8d02 1364 (prog->substrs->check_ix ? "floating" : "anchored"),
675e93ee
DM
1365 (long)(rx_origin + start_shift - strbeg),
1366 (IV)(rx_origin - strbeg)
1367 ));
9fed8d02 1368 goto restart;
6eb5f6b9 1369 }
9fed8d02 1370
c75a3985
DM
1371 /* Success !!! */
1372
5f9c6575 1373 if (rx_origin != s) {
a3621e74 1374 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1dc475d0 1375 " By STCLASS: moving %ld --> %ld\n",
675e93ee 1376 (long)(rx_origin - strbeg), (long)(s - strbeg))
b7953727
JH
1377 );
1378 }
1379 else {
a3621e74 1380 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1dc475d0 1381 " Does not contradict STCLASS...\n");
b7953727
JH
1382 );
1383 }
6eb5f6b9 1384 }
ffad1e6a
DM
1385
1386 /* Decide whether using the substrings helped */
1387
1388 if (rx_origin != strpos) {
1389 /* Fixed substring is found far enough so that the match
1390 cannot start at strpos. */
1391
1392 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, " try at offset...\n"));
1393 ++BmUSEFUL(utf8_target ? prog->check_utf8 : prog->check_substr); /* hooray/5 */
1394 }
1395 else {
70563e16
DM
1396 /* The found rx_origin position does not prohibit matching at
1397 * strpos, so calling intuit didn't gain us anything. Decrement
1398 * the BmUSEFUL() count on the check substring, and if we reach
1399 * zero, free it. */
1400 if (!(prog->intflags & PREGf_NAUGHTY)
ffad1e6a
DM
1401 && (utf8_target ? (
1402 prog->check_utf8 /* Could be deleted already */
1403 && --BmUSEFUL(prog->check_utf8) < 0
1404 && (prog->check_utf8 == prog->float_utf8)
1405 ) : (
1406 prog->check_substr /* Could be deleted already */
1407 && --BmUSEFUL(prog->check_substr) < 0
1408 && (prog->check_substr == prog->float_substr)
1409 )))
1410 {
1411 /* If flags & SOMETHING - do not do it many times on the same match */
1412 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, " ... Disabling check substring...\n"));
1413 /* XXX Does the destruction order has to change with utf8_target? */
1414 SvREFCNT_dec(utf8_target ? prog->check_utf8 : prog->check_substr);
1415 SvREFCNT_dec(utf8_target ? prog->check_substr : prog->check_utf8);
1416 prog->check_substr = prog->check_utf8 = NULL; /* disable */
1417 prog->float_substr = prog->float_utf8 = NULL; /* clear */
1418 check = NULL; /* abort */
ffad1e6a
DM
1419 /* XXXX This is a remnant of the old implementation. It
1420 looks wasteful, since now INTUIT can use many
1421 other heuristics. */
1422 prog->extflags &= ~RXf_USE_INTUIT;
ffad1e6a
DM
1423 }
1424 }
1425
1426 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
1427 "Intuit: %sSuccessfully guessed:%s match at offset %ld\n",
675e93ee 1428 PL_colors[4], PL_colors[5], (long)(rx_origin - strbeg)) );
ffad1e6a 1429
c765d6e0 1430 return rx_origin;
2c2d71f5
JH
1431
1432 fail_finish: /* Substring not found */
33b8afdf 1433 if (prog->check_substr || prog->check_utf8) /* could be removed already */
f2ed9b32 1434 BmUSEFUL(utf8_target ? prog->check_utf8 : prog->check_substr) += 5; /* hooray */
cad2e5aa 1435 fail:
a3621e74 1436 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch rejected by optimizer%s\n",
e4584336 1437 PL_colors[4], PL_colors[5]));
bd61b366 1438 return NULL;
cad2e5aa 1439}
9661b544 1440
70563e16 1441
a0a388a1 1442#define DECL_TRIE_TYPE(scan) \
e7fd4aa1 1443 const enum { trie_plain, trie_utf8, trie_utf8_fold, trie_latin_utf8_fold, \
a4525e78
KW
1444 trie_utf8_exactfa_fold, trie_latin_utf8_exactfa_fold, \
1445 trie_utf8l, trie_flu8 } \
e7fd4aa1
KW
1446 trie_type = ((scan->flags == EXACT) \
1447 ? (utf8_target ? trie_utf8 : trie_plain) \
a4525e78
KW
1448 : (scan->flags == EXACTL) \
1449 ? (utf8_target ? trie_utf8l : trie_plain) \
1450 : (scan->flags == EXACTFA) \
1451 ? (utf8_target \
1452 ? trie_utf8_exactfa_fold \
1453 : trie_latin_utf8_exactfa_fold) \
1454 : (scan->flags == EXACTFLU8 \
1455 ? trie_flu8 \
1456 : (utf8_target \
1457 ? trie_utf8_fold \
1458 : trie_latin_utf8_fold)))
fab2782b 1459
fd3249ee 1460#define REXEC_TRIE_READ_CHAR(trie_type, trie, widecharmap, uc, uscan, len, uvc, charid, foldlen, foldbuf, uniflags) \
baa60164 1461STMT_START { \
fab2782b 1462 STRLEN skiplen; \
baa60164 1463 U8 flags = FOLD_FLAGS_FULL; \
fab2782b 1464 switch (trie_type) { \
a4525e78 1465 case trie_flu8: \
780fcc9f 1466 _CHECK_AND_WARN_PROBLEMATIC_LOCALE; \
613abc6d
KW
1467 if (utf8_target && UTF8_IS_ABOVE_LATIN1(*uc)) { \
1468 _CHECK_AND_OUTPUT_WIDE_LOCALE_UTF8_MSG(uc, uc + UTF8SKIP(uc)); \
1469 } \
a4525e78 1470 goto do_trie_utf8_fold; \
31f05a37 1471 case trie_utf8_exactfa_fold: \
baa60164 1472 flags |= FOLD_FLAGS_NOMIX_ASCII; \
8e57b935 1473 /* FALLTHROUGH */ \
fab2782b 1474 case trie_utf8_fold: \
a4525e78 1475 do_trie_utf8_fold: \
fab2782b 1476 if ( foldlen>0 ) { \
c80e42f3 1477 uvc = utf8n_to_uvchr( (const U8*) uscan, UTF8_MAXLEN, &len, uniflags ); \
fab2782b
YO
1478 foldlen -= len; \
1479 uscan += len; \
1480 len=0; \
1481 } else { \
445bf929 1482 uvc = _to_utf8_fold_flags( (const U8*) uc, foldbuf, &foldlen, flags); \
fab2782b
YO
1483 len = UTF8SKIP(uc); \
1484 skiplen = UNISKIP( uvc ); \
1485 foldlen -= skiplen; \
1486 uscan = foldbuf + skiplen; \
1487 } \
1488 break; \
baa60164
KW
1489 case trie_latin_utf8_exactfa_fold: \
1490 flags |= FOLD_FLAGS_NOMIX_ASCII; \
8e57b935 1491 /* FALLTHROUGH */ \
fab2782b
YO
1492 case trie_latin_utf8_fold: \
1493 if ( foldlen>0 ) { \
c80e42f3 1494 uvc = utf8n_to_uvchr( (const U8*) uscan, UTF8_MAXLEN, &len, uniflags ); \
fab2782b
YO
1495 foldlen -= len; \
1496 uscan += len; \
1497 len=0; \
1498 } else { \
1499 len = 1; \
31f05a37 1500 uvc = _to_fold_latin1( (U8) *uc, foldbuf, &foldlen, flags); \
fab2782b
YO
1501 skiplen = UNISKIP( uvc ); \
1502 foldlen -= skiplen; \
1503 uscan = foldbuf + skiplen; \
1504 } \
1505 break; \
a4525e78 1506 case trie_utf8l: \
780fcc9f 1507 _CHECK_AND_WARN_PROBLEMATIC_LOCALE; \
613abc6d
KW
1508 if (utf8_target && UTF8_IS_ABOVE_LATIN1(*uc)) { \
1509 _CHECK_AND_OUTPUT_WIDE_LOCALE_UTF8_MSG(uc, uc + UTF8SKIP(uc)); \
1510 } \
780fcc9f 1511 /* FALLTHROUGH */ \
fab2782b 1512 case trie_utf8: \
c80e42f3 1513 uvc = utf8n_to_uvchr( (const U8*) uc, UTF8_MAXLEN, &len, uniflags ); \
fab2782b
YO
1514 break; \
1515 case trie_plain: \
1516 uvc = (UV)*uc; \
1517 len = 1; \
1518 } \
1519 if (uvc < 256) { \
1520 charid = trie->charmap[ uvc ]; \
1521 } \
1522 else { \
1523 charid = 0; \
1524 if (widecharmap) { \
1525 SV** const svpp = hv_fetch(widecharmap, \
1526 (char*)&uvc, sizeof(UV), 0); \
1527 if (svpp) \
1528 charid = (U16)SvIV(*svpp); \
1529 } \
1530 } \
4cadc6a9
YO
1531} STMT_END
1532
ae7c5b9b
KW
1533#define DUMP_EXEC_POS(li,s,doutf8) \
1534 dump_exec_pos(li,s,(reginfo->strend),(reginfo->strbeg), \
1535 startpos, doutf8)
1536
c84a03c5 1537#define REXEC_FBC_EXACTISH_SCAN(COND) \
4cadc6a9
YO
1538STMT_START { \
1539 while (s <= e) { \
c84a03c5 1540 if ( (COND) \
fac1af77 1541 && (ln == 1 || folder(s, pat_string, ln)) \
02d5137b 1542 && (reginfo->intuit || regtry(reginfo, &s)) )\
4cadc6a9
YO
1543 goto got_it; \
1544 s++; \
1545 } \
1546} STMT_END
1547
c84a03c5 1548#define REXEC_FBC_UTF8_SCAN(CODE) \
4cadc6a9 1549STMT_START { \
9a902117 1550 while (s < strend) { \
c84a03c5 1551 CODE \
9a902117 1552 s += UTF8SKIP(s); \
4cadc6a9
YO
1553 } \
1554} STMT_END
1555
c84a03c5 1556#define REXEC_FBC_SCAN(CODE) \
4cadc6a9
YO
1557STMT_START { \
1558 while (s < strend) { \
c84a03c5 1559 CODE \
4cadc6a9
YO
1560 s++; \
1561 } \
1562} STMT_END
1563
05bd126c
KW
1564#define REXEC_FBC_UTF8_CLASS_SCAN(COND) \
1565REXEC_FBC_UTF8_SCAN( /* Loops while (s < strend) */ \
1566 if (COND) { \
1567 if (tmp && (reginfo->intuit || regtry(reginfo, &s))) \
1568 goto got_it; \
1569 else \
1570 tmp = doevery; \
1571 } \
1572 else \
1573 tmp = 1; \
4cadc6a9
YO
1574)
1575
05bd126c
KW
1576#define REXEC_FBC_CLASS_SCAN(COND) \
1577REXEC_FBC_SCAN( /* Loops while (s < strend) */ \
1578 if (COND) { \
1579 if (tmp && (reginfo->intuit || regtry(reginfo, &s))) \
1580 goto got_it; \
1581 else \
1582 tmp = doevery; \
1583 } \
1584 else \
1585 tmp = 1; \
4cadc6a9
YO
1586)
1587
c84a03c5 1588#define REXEC_FBC_CSCAN(CONDUTF8,COND) \
baa60164 1589 if (utf8_target) { \
c84a03c5 1590 REXEC_FBC_UTF8_CLASS_SCAN(CONDUTF8); \
e1d1eefb
YO
1591 } \
1592 else { \
c84a03c5 1593 REXEC_FBC_CLASS_SCAN(COND); \
d981ef24 1594 }
05bd126c 1595
05bd126c
KW
1596/* The three macros below are slightly different versions of the same logic.
1597 *
1598 * The first is for /a and /aa when the target string is UTF-8. This can only
1599 * match ascii, but it must advance based on UTF-8. The other two handle the
1600 * non-UTF-8 and the more generic UTF-8 cases. In all three, we are looking
1601 * for the boundary (or non-boundary) between a word and non-word character.
1602 * The utf8 and non-utf8 cases have the same logic, but the details must be
1603 * different. Find the "wordness" of the character just prior to this one, and
1604 * compare it with the wordness of this one. If they differ, we have a
1605 * boundary. At the beginning of the string, pretend that the previous
1606 * character was a new-line.
1607 *
1608 * All these macros uncleanly have side-effects with each other and outside
1609 * variables. So far it's been too much trouble to clean-up
1610 *
1611 * TEST_NON_UTF8 is the macro or function to call to test if its byte input is
1612 * a word character or not.
1613 * IF_SUCCESS is code to do if it finds that we are at a boundary between
1614 * word/non-word
1615 * IF_FAIL is code to do if we aren't at a boundary between word/non-word
1616 *
1617 * Exactly one of the two IF_FOO parameters is a no-op, depending on whether we
1618 * are looking for a boundary or for a non-boundary. If we are looking for a
1619 * boundary, we want IF_FAIL to be the no-op, and for IF_SUCCESS to go out and
1620 * see if this tentative match actually works, and if so, to quit the loop
1621 * here. And vice-versa if we are looking for a non-boundary.
1622 *
1623 * 'tmp' below in the next three macros in the REXEC_FBC_SCAN and
1624 * REXEC_FBC_UTF8_SCAN loops is a loop invariant, a bool giving the return of
1625 * TEST_NON_UTF8(s-1). To see this, note that that's what it is defined to be
1626 * at entry to the loop, and to get to the IF_FAIL branch, tmp must equal
1627 * TEST_NON_UTF8(s), and in the opposite branch, IF_SUCCESS, tmp is that
1628 * complement. But in that branch we complement tmp, meaning that at the
1629 * bottom of the loop tmp is always going to be equal to TEST_NON_UTF8(s),
1630 * which means at the top of the loop in the next iteration, it is
1631 * TEST_NON_UTF8(s-1) */
b2f4e957 1632#define FBC_UTF8_A(TEST_NON_UTF8, IF_SUCCESS, IF_FAIL) \
05bd126c
KW
1633 tmp = (s != reginfo->strbeg) ? UCHARAT(s - 1) : '\n'; \
1634 tmp = TEST_NON_UTF8(tmp); \
1635 REXEC_FBC_UTF8_SCAN( /* advances s while s < strend */ \
1636 if (tmp == ! TEST_NON_UTF8((U8) *s)) { \
1637 tmp = !tmp; \
1638 IF_SUCCESS; /* Is a boundary if values for s-1 and s differ */ \
1639 } \
1640 else { \
1641 IF_FAIL; \
1642 } \
1643 ); \
1644
1645/* Like FBC_UTF8_A, but TEST_UV is a macro which takes a UV as its input, and
1646 * TEST_UTF8 is a macro that for the same input code points returns identically
1647 * to TEST_UV, but takes a pointer to a UTF-8 encoded string instead */
236d82fd 1648#define FBC_UTF8(TEST_UV, TEST_UTF8, IF_SUCCESS, IF_FAIL) \
05bd126c
KW
1649 if (s == reginfo->strbeg) { \
1650 tmp = '\n'; \
1651 } \
1652 else { /* Back-up to the start of the previous character */ \
1653 U8 * const r = reghop3((U8*)s, -1, (U8*)reginfo->strbeg); \
1654 tmp = utf8n_to_uvchr(r, (U8*) reginfo->strend - r, \
3db24e1e 1655 0, UTF8_ALLOW_DEFAULT); \
05bd126c
KW
1656 } \
1657 tmp = TEST_UV(tmp); \
1658 LOAD_UTF8_CHARCLASS_ALNUM(); \
1659 REXEC_FBC_UTF8_SCAN( /* advances s while s < strend */ \
1660 if (tmp == ! (TEST_UTF8((U8 *) s))) { \
1661 tmp = !tmp; \
1662 IF_SUCCESS; \
1663 } \
1664 else { \
1665 IF_FAIL; \
1666 } \
1667 );
cfaf538b 1668
05bd126c
KW
1669/* Like the above two macros. UTF8_CODE is the complete code for handling
1670 * UTF-8. Common to the BOUND and NBOUND cases, set-up by the FBC_BOUND, etc
1671 * macros below */
baa60164 1672#define FBC_BOUND_COMMON(UTF8_CODE, TEST_NON_UTF8, IF_SUCCESS, IF_FAIL) \
63ac0dad 1673 if (utf8_target) { \
05bd126c 1674 UTF8_CODE \
63ac0dad
KW
1675 } \
1676 else { /* Not utf8 */ \
9d9163fb 1677 tmp = (s != reginfo->strbeg) ? UCHARAT(s - 1) : '\n'; \
63ac0dad 1678 tmp = TEST_NON_UTF8(tmp); \
05bd126c 1679 REXEC_FBC_SCAN( /* advances s while s < strend */ \
63ac0dad 1680 if (tmp == ! TEST_NON_UTF8((U8) *s)) { \
63ac0dad 1681 IF_SUCCESS; \
760cfa8e 1682 tmp = !tmp; \
63ac0dad
KW
1683 } \
1684 else { \
1685 IF_FAIL; \
1686 } \
1687 ); \
1688 } \
c8519dc7
KW
1689 /* Here, things have been set up by the previous code so that tmp is the \
1690 * return of TEST_NON_UTF(s-1) or TEST_UTF8(s-1) (depending on the \
1691 * utf8ness of the target). We also have to check if this matches against \
1692 * the EOS, which we treat as a \n (which is the same value in both UTF-8 \
1693 * or non-UTF8, so can use the non-utf8 test condition even for a UTF-8 \
1694 * string */ \
1695 if (tmp == ! TEST_NON_UTF8('\n')) { \
1696 IF_SUCCESS; \
1697 } \
1698 else { \
1699 IF_FAIL; \
1700 }
63ac0dad 1701
ae7c5b9b
KW
1702/* This is the macro to use when we want to see if something that looks like it
1703 * could match, actually does, and if so exits the loop */
1704#define REXEC_FBC_TRYIT \
1705 if ((reginfo->intuit || regtry(reginfo, &s))) \
1706 goto got_it
1707
1708/* The only difference between the BOUND and NBOUND cases is that
1709 * REXEC_FBC_TRYIT is called when matched in BOUND, and when non-matched in
1710 * NBOUND. This is accomplished by passing it as either the if or else clause,
1711 * with the other one being empty (PLACEHOLDER is defined as empty).
1712 *
1713 * The TEST_FOO parameters are for operating on different forms of input, but
1714 * all should be ones that return identically for the same underlying code
1715 * points */
1716#define FBC_BOUND(TEST_NON_UTF8, TEST_UV, TEST_UTF8) \
1717 FBC_BOUND_COMMON( \
1718 FBC_UTF8(TEST_UV, TEST_UTF8, REXEC_FBC_TRYIT, PLACEHOLDER), \
1719 TEST_NON_UTF8, REXEC_FBC_TRYIT, PLACEHOLDER)
1720
44129e46 1721#define FBC_BOUND_A(TEST_NON_UTF8) \
ae7c5b9b
KW
1722 FBC_BOUND_COMMON( \
1723 FBC_UTF8_A(TEST_NON_UTF8, REXEC_FBC_TRYIT, PLACEHOLDER), \
1724 TEST_NON_UTF8, REXEC_FBC_TRYIT, PLACEHOLDER)
1725
1726#define FBC_NBOUND(TEST_NON_UTF8, TEST_UV, TEST_UTF8) \
1727 FBC_BOUND_COMMON( \
1728 FBC_UTF8(TEST_UV, TEST_UTF8, PLACEHOLDER, REXEC_FBC_TRYIT), \
1729 TEST_NON_UTF8, PLACEHOLDER, REXEC_FBC_TRYIT)
1730
44129e46 1731#define FBC_NBOUND_A(TEST_NON_UTF8) \
ae7c5b9b
KW
1732 FBC_BOUND_COMMON( \
1733 FBC_UTF8_A(TEST_NON_UTF8, PLACEHOLDER, REXEC_FBC_TRYIT), \
1734 TEST_NON_UTF8, PLACEHOLDER, REXEC_FBC_TRYIT)
1735
64935bc6
KW
1736/* Takes a pointer to an inversion list, a pointer to its corresponding
1737 * inversion map, and a code point, and returns the code point's value
1738 * according to the two arrays. It assumes that all code points have a value.
1739 * This is used as the base macro for macros for particular properties */
1740#define _generic_GET_BREAK_VAL_CP(invlist, invmap, cp) \
1741 invmap[_invlist_search(invlist, cp)]
1742
1743/* Same as above, but takes begin, end ptrs to a UTF-8 encoded string instead
1744 * of a code point, returning the value for the first code point in the string.
1745 * And it takes the particular macro name that finds the desired value given a
1746 * code point. Merely convert the UTF-8 to code point and call the cp macro */
1747#define _generic_GET_BREAK_VAL_UTF8(cp_macro, pos, strend) \
1748 (__ASSERT_(pos < strend) \
1749 /* Note assumes is valid UTF-8 */ \
1750 (cp_macro(utf8_to_uvchr_buf((pos), (strend), NULL))))
1751
1752/* Returns the GCB value for the input code point */
1753#define getGCB_VAL_CP(cp) \
1754 _generic_GET_BREAK_VAL_CP( \
1755 PL_GCB_invlist, \
1756 Grapheme_Cluster_Break_invmap, \
1757 (cp))
1758
1759/* Returns the GCB value for the first code point in the UTF-8 encoded string
1760 * bounded by pos and strend */
1761#define getGCB_VAL_UTF8(pos, strend) \
1762 _generic_GET_BREAK_VAL_UTF8(getGCB_VAL_CP, pos, strend)
05bd126c 1763
06ae2722
KW
1764
1765/* Returns the SB value for the input code point */
1766#define getSB_VAL_CP(cp) \
1767 _generic_GET_BREAK_VAL_CP( \
1768 PL_SB_invlist, \
1769 Sentence_Break_invmap, \
1770 (cp))
1771
1772/* Returns the SB value for the first code point in the UTF-8 encoded string
1773 * bounded by pos and strend */
1774#define getSB_VAL_UTF8(pos, strend) \
1775 _generic_GET_BREAK_VAL_UTF8(getSB_VAL_CP, pos, strend)
1776
ae3bb8ea
KW
1777/* Returns the WB value for the input code point */
1778#define getWB_VAL_CP(cp) \
1779 _generic_GET_BREAK_VAL_CP( \
1780 PL_WB_invlist, \
1781 Word_Break_invmap, \
1782 (cp))
1783
1784/* Returns the WB value for the first code point in the UTF-8 encoded string
1785 * bounded by pos and strend */
1786#define getWB_VAL_UTF8(pos, strend) \
1787 _generic_GET_BREAK_VAL_UTF8(getWB_VAL_CP, pos, strend)
1788
786e8c11 1789/* We know what class REx starts with. Try to find this position... */
02d5137b 1790/* if reginfo->intuit, its a dryrun */
786e8c11
YO
1791/* annoyingly all the vars in this routine have different names from their counterparts
1792 in regmatch. /grrr */
3c3eec57 1793STATIC char *
07be1b83 1794S_find_byclass(pTHX_ regexp * prog, const regnode *c, char *s,
f9176b44 1795 const char *strend, regmatch_info *reginfo)
a687059c 1796{
73104a1b
KW
1797 dVAR;
1798 const I32 doevery = (prog->intflags & PREGf_SKIP) == 0;
1799 char *pat_string; /* The pattern's exactish string */
1800 char *pat_end; /* ptr to end char of pat_string */
1801 re_fold_t folder; /* Function for computing non-utf8 folds */
1802 const U8 *fold_array; /* array for folding ords < 256 */
1803 STRLEN ln;
1804 STRLEN lnc;
73104a1b
KW
1805 U8 c1;
1806 U8 c2;
1807 char *e;
1808 I32 tmp = 1; /* Scratch variable? */
ba44c216 1809 const bool utf8_target = reginfo->is_utf8_target;
73104a1b 1810 UV utf8_fold_flags = 0;
f9176b44 1811 const bool is_utf8_pat = reginfo->is_utf8_pat;
3018b823
KW
1812 bool to_complement = FALSE; /* Invert the result? Taking the xor of this
1813 with a result inverts that result, as 0^1 =
1814 1 and 1^1 = 0 */
1815 _char_class_number classnum;
1816
73104a1b 1817 RXi_GET_DECL(prog,progi);
2f7f8cb1 1818
73104a1b 1819 PERL_ARGS_ASSERT_FIND_BYCLASS;
2f7f8cb1 1820
73104a1b
KW
1821 /* We know what class it must start with. */
1822 switch (OP(c)) {
a4525e78 1823 case ANYOFL:
780fcc9f
KW
1824 _CHECK_AND_WARN_PROBLEMATIC_LOCALE;
1825 /* FALLTHROUGH */
73104a1b
KW
1826 case ANYOF:
1827 if (utf8_target) {
1828 REXEC_FBC_UTF8_CLASS_SCAN(
3db24e1e 1829 reginclass(prog, c, (U8*)s, (U8*) strend, utf8_target));
73104a1b
KW
1830 }
1831 else {
1832 REXEC_FBC_CLASS_SCAN(REGINCLASS(prog, c, (U8*)s));
1833 }
1834 break;
73104a1b 1835
098b07d5
KW
1836 case EXACTFA_NO_TRIE: /* This node only generated for non-utf8 patterns */
1837 assert(! is_utf8_pat);
924ba076 1838 /* FALLTHROUGH */
73104a1b 1839 case EXACTFA:
984e6dd1 1840 if (is_utf8_pat || utf8_target) {
73104a1b
KW
1841 utf8_fold_flags = FOLDEQ_UTF8_NOMIX_ASCII;
1842 goto do_exactf_utf8;
1843 }
1844 fold_array = PL_fold_latin1; /* Latin1 folds are not affected by */
1845 folder = foldEQ_latin1; /* /a, except the sharp s one which */
1846 goto do_exactf_non_utf8; /* isn't dealt with by these */
77a6d856 1847
2fdb7295
KW
1848 case EXACTF: /* This node only generated for non-utf8 patterns */
1849 assert(! is_utf8_pat);
73104a1b 1850 if (utf8_target) {
73104a1b
KW
1851 utf8_fold_flags = 0;
1852 goto do_exactf_utf8;
1853 }
1854 fold_array = PL_fold;
1855 folder = foldEQ;
1856 goto do_exactf_non_utf8;
1857
1858 case EXACTFL:
780fcc9f 1859 _CHECK_AND_WARN_PROBLEMATIC_LOCALE;
31f05a37 1860 if (is_utf8_pat || utf8_target || IN_UTF8_CTYPE_LOCALE) {
cea315b6 1861 utf8_fold_flags = FOLDEQ_LOCALE;
73104a1b
KW
1862 goto do_exactf_utf8;
1863 }
1864 fold_array = PL_fold_locale;
1865 folder = foldEQ_locale;
1866 goto do_exactf_non_utf8;
3c760661 1867
73104a1b 1868 case EXACTFU_SS:
984e6dd1 1869 if (is_utf8_pat) {
73104a1b
KW
1870 utf8_fold_flags = FOLDEQ_S2_ALREADY_FOLDED;
1871 }
1872 goto do_exactf_utf8;
16d951b7 1873
a4525e78
KW
1874 case EXACTFLU8:
1875 if (! utf8_target) { /* All code points in this node require
1876 UTF-8 to express. */
1877 break;
1878 }
613abc6d
KW
1879 utf8_fold_flags = FOLDEQ_LOCALE | FOLDEQ_S2_ALREADY_FOLDED
1880 | FOLDEQ_S2_FOLDS_SANE;
a4525e78
KW
1881 goto do_exactf_utf8;
1882
73104a1b 1883 case EXACTFU:
984e6dd1
DM
1884 if (is_utf8_pat || utf8_target) {
1885 utf8_fold_flags = is_utf8_pat ? FOLDEQ_S2_ALREADY_FOLDED : 0;
73104a1b
KW
1886 goto do_exactf_utf8;
1887 }
fac1af77 1888
73104a1b
KW
1889 /* Any 'ss' in the pattern should have been replaced by regcomp,
1890 * so we don't have to worry here about this single special case
1891 * in the Latin1 range */
1892 fold_array = PL_fold_latin1;
1893 folder = foldEQ_latin1;
1894
924ba076 1895 /* FALLTHROUGH */
73104a1b 1896
c52b8b12 1897 do_exactf_non_utf8: /* Neither pattern nor string are UTF8, and there
73104a1b
KW
1898 are no glitches with fold-length differences
1899 between the target string and pattern */
1900
1901 /* The idea in the non-utf8 EXACTF* cases is to first find the
1902 * first character of the EXACTF* node and then, if necessary,
1903 * case-insensitively compare the full text of the node. c1 is the
1904 * first character. c2 is its fold. This logic will not work for
1905 * Unicode semantics and the german sharp ss, which hence should
1906 * not be compiled into a node that gets here. */
1907 pat_string = STRING(c);
1908 ln = STR_LEN(c); /* length to match in octets/bytes */
1909
1910 /* We know that we have to match at least 'ln' bytes (which is the
1911 * same as characters, since not utf8). If we have to match 3
1912 * characters, and there are only 2 availabe, we know without
1913 * trying that it will fail; so don't start a match past the
1914 * required minimum number from the far end */
ea3daa5d 1915 e = HOP3c(strend, -((SSize_t)ln), s);
73104a1b 1916
02d5137b 1917 if (reginfo->intuit && e < s) {
73104a1b
KW
1918 e = s; /* Due to minlen logic of intuit() */
1919 }
fac1af77 1920
73104a1b
KW
1921 c1 = *pat_string;
1922 c2 = fold_array[c1];
1923 if (c1 == c2) { /* If char and fold are the same */
1924 REXEC_FBC_EXACTISH_SCAN(*(U8*)s == c1);
1925 }
1926 else {
1927 REXEC_FBC_EXACTISH_SCAN(*(U8*)s == c1 || *(U8*)s == c2);
1928 }
1929 break;
fac1af77 1930
c52b8b12
KW
1931 do_exactf_utf8:
1932 {
73104a1b
KW
1933 unsigned expansion;
1934
1935 /* If one of the operands is in utf8, we can't use the simpler folding
1936 * above, due to the fact that many different characters can have the
1937 * same fold, or portion of a fold, or different- length fold */
1938 pat_string = STRING(c);
1939 ln = STR_LEN(c); /* length to match in octets/bytes */
1940 pat_end = pat_string + ln;
984e6dd1 1941 lnc = is_utf8_pat /* length to match in characters */
73104a1b
KW
1942 ? utf8_length((U8 *) pat_string, (U8 *) pat_end)
1943 : ln;
1944
1945 /* We have 'lnc' characters to match in the pattern, but because of
1946 * multi-character folding, each character in the target can match
1947 * up to 3 characters (Unicode guarantees it will never exceed
1948 * this) if it is utf8-encoded; and up to 2 if not (based on the
1949 * fact that the Latin 1 folds are already determined, and the
1950 * only multi-char fold in that range is the sharp-s folding to
1951 * 'ss'. Thus, a pattern character can match as little as 1/3 of a
1952 * string character. Adjust lnc accordingly, rounding up, so that
1953 * if we need to match at least 4+1/3 chars, that really is 5. */
1954 expansion = (utf8_target) ? UTF8_MAX_FOLD_CHAR_EXPAND : 2;
1955 lnc = (lnc + expansion - 1) / expansion;
1956
1957 /* As in the non-UTF8 case, if we have to match 3 characters, and
1958 * only 2 are left, it's guaranteed to fail, so don't start a
1959 * match that would require us to go beyond the end of the string
1960 */
ea3daa5d 1961 e = HOP3c(strend, -((SSize_t)lnc), s);
73104a1b 1962
02d5137b 1963 if (reginfo->intuit && e < s) {
73104a1b
KW
1964 e = s; /* Due to minlen logic of intuit() */
1965 }
0658cdde 1966
73104a1b
KW
1967 /* XXX Note that we could recalculate e to stop the loop earlier,
1968 * as the worst case expansion above will rarely be met, and as we
1969 * go along we would usually find that e moves further to the left.
1970 * This would happen only after we reached the point in the loop
1971 * where if there were no expansion we should fail. Unclear if
1972 * worth the expense */
1973
1974 while (s <= e) {
1975 char *my_strend= (char *)strend;
1976 if (foldEQ_utf8_flags(s, &my_strend, 0, utf8_target,
984e6dd1 1977 pat_string, NULL, ln, is_utf8_pat, utf8_fold_flags)
02d5137b 1978 && (reginfo->intuit || regtry(reginfo, &s)) )
73104a1b
KW
1979 {
1980 goto got_it;
1981 }
1982 s += (utf8_target) ? UTF8SKIP(s) : 1;
1983 }
1984 break;
1985 }
236d82fd 1986
73104a1b 1987 case BOUNDL:
780fcc9f 1988 _CHECK_AND_WARN_PROBLEMATIC_LOCALE;
64935bc6 1989 if (FLAGS(c) != TRADITIONAL_BOUND) {
89ad707a
KW
1990 if (! IN_UTF8_CTYPE_LOCALE) {
1991 Perl_ck_warner(aTHX_ packWARN(WARN_LOCALE),
64935bc6 1992 B_ON_NON_UTF8_LOCALE_IS_WRONG);
89ad707a 1993 }
64935bc6
KW
1994 goto do_boundu;
1995 }
1996
236d82fd 1997 FBC_BOUND(isWORDCHAR_LC, isWORDCHAR_LC_uvchr, isWORDCHAR_LC_utf8);
73104a1b 1998 break;
64935bc6 1999
73104a1b 2000 case NBOUNDL:
780fcc9f 2001 _CHECK_AND_WARN_PROBLEMATIC_LOCALE;
64935bc6 2002 if (FLAGS(c) != TRADITIONAL_BOUND) {
89ad707a
KW
2003 if (! IN_UTF8_CTYPE_LOCALE) {
2004 Perl_ck_warner(aTHX_ packWARN(WARN_LOCALE),
64935bc6 2005 B_ON_NON_UTF8_LOCALE_IS_WRONG);
89ad707a 2006 }
64935bc6
KW
2007 goto do_nboundu;
2008 }
2009
236d82fd 2010 FBC_NBOUND(isWORDCHAR_LC, isWORDCHAR_LC_uvchr, isWORDCHAR_LC_utf8);
73104a1b 2011 break;
64935bc6
KW
2012
2013 case BOUND: /* regcomp.c makes sure that this only has the traditional \b
2014 meaning */
2015 assert(FLAGS(c) == TRADITIONAL_BOUND);
2016
236d82fd 2017 FBC_BOUND(isWORDCHAR, isWORDCHAR_uni, isWORDCHAR_utf8);
73104a1b 2018 break;
64935bc6
KW
2019
2020 case BOUNDA: /* regcomp.c makes sure that this only has the traditional \b
2021 meaning */
2022 assert(FLAGS(c) == TRADITIONAL_BOUND);
2023
44129e46 2024 FBC_BOUND_A(isWORDCHAR_A);
73104a1b 2025 break;
64935bc6
KW
2026
2027 case NBOUND: /* regcomp.c makes sure that this only has the traditional \b
2028 meaning */
2029 assert(FLAGS(c) == TRADITIONAL_BOUND);
2030
236d82fd 2031 FBC_NBOUND(isWORDCHAR, isWORDCHAR_uni, isWORDCHAR_utf8);
73104a1b 2032 break;
64935bc6
KW
2033
2034 case NBOUNDA: /* regcomp.c makes sure that this only has the traditional \b
2035 meaning */
2036 assert(FLAGS(c) == TRADITIONAL_BOUND);
2037
44129e46 2038 FBC_NBOUND_A(isWORDCHAR_A);
73104a1b 2039 break;
64935bc6 2040
73104a1b 2041 case NBOUNDU:
64935bc6
KW
2042 if ((bound_type) FLAGS(c) == TRADITIONAL_BOUND) {
2043 FBC_NBOUND(isWORDCHAR_L1, isWORDCHAR_uni, isWORDCHAR_utf8);
2044 break;
2045 }
2046
2047 do_nboundu:
2048
2049 to_complement = 1;
2050 /* FALLTHROUGH */
2051
2052 case BOUNDU:
2053 do_boundu:
2054 switch((bound_type) FLAGS(c)) {
2055 case TRADITIONAL_BOUND:
2056 FBC_BOUND(isWORDCHAR_L1, isWORDCHAR_uni, isWORDCHAR_utf8);
2057 break;
2058 case GCB_BOUND:
2059 if (s == reginfo->strbeg) { /* GCB always matches at begin and
2060 end */
2061 if (to_complement ^ cBOOL(reginfo->intuit
2062 || regtry(reginfo, &s)))
2063 {
2064 goto got_it;
2065 }
2066 s += (utf8_target) ? UTF8SKIP(s) : 1;
2067 }
2068
2069 if (utf8_target) {
85e5f08b 2070 GCB_enum before = getGCB_VAL_UTF8(
64935bc6
KW
2071 reghop3((U8*)s, -1,
2072 (U8*)(reginfo->strbeg)),
2073 (U8*) reginfo->strend);
2074 while (s < strend) {
85e5f08b 2075 GCB_enum after = getGCB_VAL_UTF8((U8*) s,
64935bc6
KW
2076 (U8*) reginfo->strend);
2077 if (to_complement ^ isGCB(before, after)) {
2078 if (reginfo->intuit || regtry(reginfo, &s)) {
2079 goto got_it;
2080 }
2081 before = after;
2082 }
2083 s += UTF8SKIP(s);
2084 }
2085 }
2086 else { /* Not utf8. Everything is a GCB except between CR and
2087 LF */
2088 while (s < strend) {
2089 if (to_complement ^ (UCHARAT(s - 1) != '\r'
2090 || UCHARAT(s) != '\n'))
2091 {
2092 if (reginfo->intuit || regtry(reginfo, &s)) {
2093 goto got_it;
2094 }
2095 s++;
2096 }
2097 }
2098 }
2099
2100 if (to_complement ^ cBOOL(reginfo->intuit || regtry(reginfo, &s))) {
2101 goto got_it;
2102 }
2103 break;
ae3bb8ea 2104
06ae2722
KW
2105 case SB_BOUND:
2106 if (s == reginfo->strbeg) { /* SB always matches at beginning */
2107 if (to_complement
2108 ^ cBOOL(reginfo->intuit || regtry(reginfo, &s)))
2109 {
2110 goto got_it;
2111 }
2112
2113 /* Didn't match. Go try at the next position */
2114 s += (utf8_target) ? UTF8SKIP(s) : 1;
2115 }
2116
2117 if (utf8_target) {
85e5f08b 2118 SB_enum before = getSB_VAL_UTF8(reghop3((U8*)s,
06ae2722
KW
2119 -1,
2120 (U8*)(reginfo->strbeg)),
2121 (U8*) reginfo->strend);
2122 while (s < strend) {
85e5f08b 2123 SB_enum after = getSB_VAL_UTF8((U8*) s,
06ae2722
KW
2124 (U8*) reginfo->strend);
2125 if (to_complement ^ isSB(before,
2126 after,
2127 (U8*) reginfo->strbeg,
2128 (U8*) s,
2129 (U8*) reginfo->strend,
2130 utf8_target))
2131 {
2132 if (reginfo->intuit || regtry(reginfo, &s)) {
2133 goto got_it;
2134 }
2135 before = after;
2136 }
2137 s += UTF8SKIP(s);
2138 }
2139 }
2140 else { /* Not utf8. */
85e5f08b 2141 SB_enum before = getSB_VAL_CP((U8) *(s -1));
06ae2722 2142 while (s < strend) {
85e5f08b 2143 SB_enum after = getSB_VAL_CP((U8) *s);
06ae2722
KW
2144 if (to_complement ^ isSB(before,
2145 after,
2146 (U8*) reginfo->strbeg,
2147 (U8*) s,
2148 (U8*) reginfo->strend,
2149 utf8_target))
2150 {
2151 if (reginfo->intuit || regtry(reginfo, &s)) {
2152 goto got_it;
2153 }
2154 before = after;
2155 }
2156 s++;
2157 }
2158 }
2159
2160 /* Here are at the final position in the target string. The SB
2161 * value is always true here, so matches, depending on other
2162 * constraints */
2163 if (to_complement ^ cBOOL(reginfo->intuit
2164 || regtry(reginfo, &s)))
2165 {
2166 goto got_it;
2167 }
2168
2169 break;
2170
ae3bb8ea
KW
2171 case WB_BOUND:
2172 if (s == reginfo->strbeg) {
2173 if (to_complement ^ cBOOL(reginfo->intuit
2174 || regtry(reginfo, &s)))
2175 {
2176 goto got_it;
2177 }
2178 s += (utf8_target) ? UTF8SKIP(s) : 1;
2179 }
2180
2181 if (utf8_target) {
2182 /* We are at a boundary between char_sub_0 and char_sub_1.
2183 * We also keep track of the value for char_sub_-1 as we
2184 * loop through the line. Context may be needed to make a
2185 * determination, and if so, this can save having to
2186 * recalculate it */
85e5f08b
KW
2187 WB_enum previous = WB_UNKNOWN;
2188 WB_enum before = getWB_VAL_UTF8(
ae3bb8ea
KW
2189 reghop3((U8*)s,
2190 -1,
2191 (U8*)(reginfo->strbeg)),
2192 (U8*) reginfo->strend);
2193 while (s < strend) {
85e5f08b 2194 WB_enum after = getWB_VAL_UTF8((U8*) s,
ae3bb8ea
KW
2195 (U8*) reginfo->strend);
2196 if (to_complement ^ isWB(previous,
2197 before,
2198 after,
2199 (U8*) reginfo->strbeg,
2200 (U8*) s,
2201 (U8*) reginfo->strend,
2202 utf8_target))
2203 {
2204 if (reginfo->intuit || regtry(reginfo, &s)) {
2205 goto got_it;
2206 }
2207 previous = before;
2208 before = after;
2209 }
2210 s += UTF8SKIP(s);
2211 }
2212 }
2213 else { /* Not utf8. */
85e5f08b
KW
2214 WB_enum previous = WB_UNKNOWN;
2215 WB_enum before = getWB_VAL_CP((U8) *(s -1));
ae3bb8ea 2216 while (s < strend) {
85e5f08b 2217 WB_enum after = getWB_VAL_CP((U8) *s);
ae3bb8ea
KW
2218 if (to_complement ^ isWB(previous,
2219 before,
2220 after,
2221 (U8*) reginfo->strbeg,
2222 (U8*) s,
2223 (U8*) reginfo->strend,
2224 utf8_target))
2225 {
2226 if (reginfo->intuit || regtry(reginfo, &s)) {
2227 goto got_it;
2228 }
2229 previous = before;
2230 before = after;
2231 }
2232 s++;
2233 }
2234 }
2235
2236 if (to_complement ^ cBOOL(reginfo->intuit
2237 || regtry(reginfo, &s)))
2238 {
2239 goto got_it;
2240 }
2241
2242 break;
64935bc6 2243 }
73104a1b 2244 break;
64935bc6 2245
73104a1b
KW
2246 case LNBREAK:
2247 REXEC_FBC_CSCAN(is_LNBREAK_utf8_safe(s, strend),
2248 is_LNBREAK_latin1_safe(s, strend)
2249 );
2250 break;
3018b823
KW
2251
2252 /* The argument to all the POSIX node types is the class number to pass to
2253 * _generic_isCC() to build a mask for searching in PL_charclass[] */
2254
2255 case NPOSIXL:
2256 to_complement = 1;
2257 /* FALLTHROUGH */
2258
2259 case POSIXL:
780fcc9f 2260 _CHECK_AND_WARN_PROBLEMATIC_LOCALE;
3018b823
KW
2261 REXEC_FBC_CSCAN(to_complement ^ cBOOL(isFOO_utf8_lc(FLAGS(c), (U8 *) s)),
2262 to_complement ^ cBOOL(isFOO_lc(FLAGS(c), *s)));
73104a1b 2263 break;
3018b823
KW
2264
2265 case NPOSIXD:
2266 to_complement = 1;
2267 /* FALLTHROUGH */
2268
2269 case POSIXD:
2270 if (utf8_target) {
2271 goto posix_utf8;
2272 }
2273 goto posixa;
2274
2275 case NPOSIXA:
2276 if (utf8_target) {
2277 /* The complement of something that matches only ASCII matches all
837226c8
KW
2278 * non-ASCII, plus everything in ASCII that isn't in the class. */
2279 REXEC_FBC_UTF8_CLASS_SCAN(! isASCII_utf8(s)
3018b823
KW
2280 || ! _generic_isCC_A(*s, FLAGS(c)));
2281 break;
2282 }
2283
2284 to_complement = 1;
2285 /* FALLTHROUGH */
2286
73104a1b 2287 case POSIXA:
3018b823 2288 posixa:
73104a1b 2289 /* Don't need to worry about utf8, as it can match only a single
3018b823
KW
2290 * byte invariant character. */
2291 REXEC_FBC_CLASS_SCAN(
2292 to_complement ^ cBOOL(_generic_isCC_A(*s, FLAGS(c))));
73104a1b 2293 break;
3018b823
KW
2294
2295 case NPOSIXU:
2296 to_complement = 1;
2297 /* FALLTHROUGH */
2298
2299 case POSIXU:
2300 if (! utf8_target) {
2301 REXEC_FBC_CLASS_SCAN(to_complement ^ cBOOL(_generic_isCC(*s,
2302 FLAGS(c))));
2303 }
2304 else {
2305
c52b8b12 2306 posix_utf8:
3018b823
KW
2307 classnum = (_char_class_number) FLAGS(c);
2308 if (classnum < _FIRST_NON_SWASH_CC) {
2309 while (s < strend) {
2310
2311 /* We avoid loading in the swash as long as possible, but
2312 * should we have to, we jump to a separate loop. This
2313 * extra 'if' statement is what keeps this code from being
2314 * just a call to REXEC_FBC_UTF8_CLASS_SCAN() */
2315 if (UTF8_IS_ABOVE_LATIN1(*s)) {
2316 goto found_above_latin1;
2317 }
2318 if ((UTF8_IS_INVARIANT(*s)
2319 && to_complement ^ cBOOL(_generic_isCC((U8) *s,
2320 classnum)))
2321 || (UTF8_IS_DOWNGRADEABLE_START(*s)
2322 && to_complement ^ cBOOL(
94bb8c36
KW
2323 _generic_isCC(TWO_BYTE_UTF8_TO_NATIVE(*s,
2324 *(s + 1)),
3018b823
KW
2325 classnum))))
2326 {
02d5137b 2327 if (tmp && (reginfo->intuit || regtry(reginfo, &s)))
3018b823
KW
2328 goto got_it;
2329 else {
2330 tmp = doevery;
2331 }
2332 }
2333 else {
2334 tmp = 1;
2335 }
2336 s += UTF8SKIP(s);
2337 }
2338 }
2339 else switch (classnum) { /* These classes are implemented as
2340 macros */
779cf272 2341 case _CC_ENUM_SPACE:
3018b823
KW
2342 REXEC_FBC_UTF8_CLASS_SCAN(
2343 to_complement ^ cBOOL(isSPACE_utf8(s)));
2344 break;
2345
2346 case _CC_ENUM_BLANK:
2347 REXEC_FBC_UTF8_CLASS_SCAN(
2348 to_complement ^ cBOOL(isBLANK_utf8(s)));
2349 break;
2350
2351 case _CC_ENUM_XDIGIT:
2352 REXEC_FBC_UTF8_CLASS_SCAN(
2353 to_complement ^ cBOOL(isXDIGIT_utf8(s)));
2354 break;
2355
2356 case _CC_ENUM_VERTSPACE:
2357 REXEC_FBC_UTF8_CLASS_SCAN(
2358 to_complement ^ cBOOL(isVERTWS_utf8(s)));
2359 break;
2360
2361 case _CC_ENUM_CNTRL:
2362 REXEC_FBC_UTF8_CLASS_SCAN(
2363 to_complement ^ cBOOL(isCNTRL_utf8(s)));
2364 break;
2365
2366 default:
2367 Perl_croak(aTHX_ "panic: find_byclass() node %d='%s' has an unexpected character class '%d'", OP(c), PL_reg_name[OP(c)], classnum);
e5964223 2368 NOT_REACHED; /* NOTREACHED */
3018b823
KW
2369 }
2370 }
2371 break;
2372
2373 found_above_latin1: /* Here we have to load a swash to get the result
2374 for the current code point */
2375 if (! PL_utf8_swash_ptrs[classnum]) {
2376 U8 flags = _CORE_SWASH_INIT_ACCEPT_INVLIST;
2377 PL_utf8_swash_ptrs[classnum] =
2a16ac92
KW
2378 _core_swash_init("utf8",
2379 "",
2380 &PL_sv_undef, 1, 0,
2381 PL_XPosix_ptrs[classnum], &flags);
3018b823
KW
2382 }
2383
2384 /* This is a copy of the loop above for swash classes, though using the
2385 * FBC macro instead of being expanded out. Since we've loaded the
2386 * swash, we don't have to check for that each time through the loop */
2387 REXEC_FBC_UTF8_CLASS_SCAN(
2388 to_complement ^ cBOOL(_generic_utf8(
2389 classnum,
2390 s,
2391 swash_fetch(PL_utf8_swash_ptrs[classnum],
2392 (U8 *) s, TRUE))));
73104a1b
KW
2393 break;
2394
2395 case AHOCORASICKC:
2396 case AHOCORASICK:
2397 {
2398 DECL_TRIE_TYPE(c);
2399 /* what trie are we using right now */
2400 reg_ac_data *aho = (reg_ac_data*)progi->data->data[ ARG( c ) ];
2401 reg_trie_data *trie = (reg_trie_data*)progi->data->data[ aho->trie ];
2402 HV *widecharmap = MUTABLE_HV(progi->data->data[ aho->trie + 1 ]);
2403
2404 const char *last_start = strend - trie->minlen;
6148ee25 2405#ifdef DEBUGGING
73104a1b 2406 const char *real_start = s;
6148ee25 2407#endif
73104a1b
KW
2408 STRLEN maxlen = trie->maxlen;
2409 SV *sv_points;
2410 U8 **points; /* map of where we were in the input string
2411 when reading a given char. For ASCII this
2412 is unnecessary overhead as the relationship
2413 is always 1:1, but for Unicode, especially
2414 case folded Unicode this is not true. */
2415 U8 foldbuf[ UTF8_MAXBYTES_CASE + 1 ];
2416 U8 *bitmap=NULL;
2417
2418
2419 GET_RE_DEBUG_FLAGS_DECL;
2420
2421 /* We can't just allocate points here. We need to wrap it in
2422 * an SV so it gets freed properly if there is a croak while
2423 * running the match */
2424 ENTER;
2425 SAVETMPS;
2426 sv_points=newSV(maxlen * sizeof(U8 *));
2427 SvCUR_set(sv_points,
2428 maxlen * sizeof(U8 *));
2429 SvPOK_on(sv_points);
2430 sv_2mortal(sv_points);
2431 points=(U8**)SvPV_nolen(sv_points );
2432 if ( trie_type != trie_utf8_fold
2433 && (trie->bitmap || OP(c)==AHOCORASICKC) )
2434 {
2435 if (trie->bitmap)
2436 bitmap=(U8*)trie->bitmap;
2437 else
2438 bitmap=(U8*)ANYOF_BITMAP(c);
2439 }
2440 /* this is the Aho-Corasick algorithm modified a touch
2441 to include special handling for long "unknown char" sequences.
2442 The basic idea being that we use AC as long as we are dealing
2443 with a possible matching char, when we encounter an unknown char
2444 (and we have not encountered an accepting state) we scan forward
2445 until we find a legal starting char.
2446 AC matching is basically that of trie matching, except that when
2447 we encounter a failing transition, we fall back to the current
2448 states "fail state", and try the current char again, a process
2449 we repeat until we reach the root state, state 1, or a legal
2450 transition. If we fail on the root state then we can either
2451 terminate if we have reached an accepting state previously, or
2452 restart the entire process from the beginning if we have not.
2453
2454 */
2455 while (s <= last_start) {
2456 const U32 uniflags = UTF8_ALLOW_DEFAULT;
2457 U8 *uc = (U8*)s;
2458 U16 charid = 0;
2459 U32 base = 1;
2460 U32 state = 1;
2461 UV uvc = 0;
2462 STRLEN len = 0;
2463 STRLEN foldlen = 0;
2464 U8 *uscan = (U8*)NULL;
2465 U8 *leftmost = NULL;
2466#ifdef DEBUGGING
2467 U32 accepted_word= 0;
786e8c11 2468#endif
73104a1b
KW
2469 U32 pointpos = 0;
2470
2471 while ( state && uc <= (U8*)strend ) {
2472 int failed=0;
2473 U32 word = aho->states[ state ].wordnum;
2474
2475 if( state==1 ) {
2476 if ( bitmap ) {
2477 DEBUG_TRIE_EXECUTE_r(
2478 if ( uc <= (U8*)last_start && !BITMAP_TEST(bitmap,*uc) ) {
2479 dump_exec_pos( (char *)uc, c, strend, real_start,
2480 (char *)uc, utf8_target );
2481 PerlIO_printf( Perl_debug_log,
2482 " Scanning for legal start char...\n");
2483 }
2484 );
2485 if (utf8_target) {
2486 while ( uc <= (U8*)last_start && !BITMAP_TEST(bitmap,*uc) ) {
2487 uc += UTF8SKIP(uc);
2488 }
2489 } else {
2490 while ( uc <= (U8*)last_start && !BITMAP_TEST(bitmap,*uc) ) {
2491 uc++;
2492 }
786e8c11 2493 }
73104a1b 2494 s= (char *)uc;
07be1b83 2495 }
73104a1b
KW
2496 if (uc >(U8*)last_start) break;
2497 }
2498
2499 if ( word ) {
2500 U8 *lpos= points[ (pointpos - trie->wordinfo[word].len) % maxlen ];
2501 if (!leftmost || lpos < leftmost) {
2502 DEBUG_r(accepted_word=word);
2503 leftmost= lpos;
7016d6eb 2504 }
73104a1b 2505 if (base==0) break;
7016d6eb 2506
73104a1b
KW
2507 }
2508 points[pointpos++ % maxlen]= uc;
2509 if (foldlen || uc < (U8*)strend) {
2510 REXEC_TRIE_READ_CHAR(trie_type, trie,
2511 widecharmap, uc,
2512 uscan, len, uvc, charid, foldlen,
2513 foldbuf, uniflags);
2514 DEBUG_TRIE_EXECUTE_r({
2515 dump_exec_pos( (char *)uc, c, strend,
2516 real_start, s, utf8_target);
2517 PerlIO_printf(Perl_debug_log,
2518 " Charid:%3u CP:%4"UVxf" ",
2519 charid, uvc);
2520 });
2521 }
2522 else {
2523 len = 0;
2524 charid = 0;
2525 }
07be1b83 2526
73104a1b
KW
2527
2528 do {
6148ee25 2529#ifdef DEBUGGING
73104a1b 2530 word = aho->states[ state ].wordnum;
6148ee25 2531#endif
73104a1b
KW
2532 base = aho->states[ state ].trans.base;
2533
2534 DEBUG_TRIE_EXECUTE_r({
2535 if (failed)
2536 dump_exec_pos( (char *)uc, c, strend, real_start,
2537 s, utf8_target );
2538 PerlIO_printf( Perl_debug_log,
2539 "%sState: %4"UVxf", word=%"UVxf,
2540 failed ? " Fail transition to " : "",
2541 (UV)state, (UV)word);
2542 });
2543 if ( base ) {
2544 U32 tmp;
2545 I32 offset;
2546 if (charid &&
2547 ( ((offset = base + charid
2548 - 1 - trie->uniquecharcount)) >= 0)
2549 && ((U32)offset < trie->lasttrans)
2550 && trie->trans[offset].check == state
2551 && (tmp=trie->trans[offset].next))
2552 {
2553 DEBUG_TRIE_EXECUTE_r(
2554 PerlIO_printf( Perl_debug_log," - legal\n"));
2555 state = tmp;
2556 break;
07be1b83
YO
2557 }
2558 else {
786e8c11 2559 DEBUG_TRIE_EXECUTE_r(
73104a1b 2560 PerlIO_printf( Perl_debug_log," - fail\n"));
786e8c11 2561 failed = 1;
73104a1b 2562 state = aho->fail[state];
07be1b83 2563 }
07be1b83 2564 }
73104a1b
KW
2565 else {
2566 /* we must be accepting here */
2567 DEBUG_TRIE_EXECUTE_r(
2568 PerlIO_printf( Perl_debug_log," - accepting\n"));
2569 failed = 1;
2570 break;
786e8c11 2571 }
73104a1b
KW
2572 } while(state);
2573 uc += len;
2574 if (failed) {
2575 if (leftmost)
2576 break;
2577 if (!state) state = 1;
07be1b83 2578 }
73104a1b
KW
2579 }
2580 if ( aho->states[ state ].wordnum ) {
2581 U8 *lpos = points[ (pointpos - trie->wordinfo[aho->states[ state ].wordnum].len) % maxlen ];
2582 if (!leftmost || lpos < leftmost) {
2583 DEBUG_r(accepted_word=aho->states[ state ].wordnum);
2584 leftmost = lpos;
07be1b83
YO
2585 }
2586 }
73104a1b
KW
2587 if (leftmost) {
2588 s = (char*)leftmost;
2589 DEBUG_TRIE_EXECUTE_r({
2590 PerlIO_printf(
2591 Perl_debug_log,"Matches word #%"UVxf" at position %"IVdf". Trying full pattern...\n",
2592 (UV)accepted_word, (IV)(s - real_start)
2593 );
2594 });
02d5137b 2595 if (reginfo->intuit || regtry(reginfo, &s)) {
73104a1b
KW
2596 FREETMPS;
2597 LEAVE;
2598 goto got_it;
2599 }
2600 s = HOPc(s,1);
2601 DEBUG_TRIE_EXECUTE_r({
2602 PerlIO_printf( Perl_debug_log,"Pattern failed. Looking for new start point...\n");
2603 });
2604 } else {
2605 DEBUG_TRIE_EXECUTE_r(
2606 PerlIO_printf( Perl_debug_log,"No match.\n"));
2607 break;
2608 }
2609 }
2610 FREETMPS;
2611 LEAVE;
2612 }
2613 break;
2614 default:
2615 Perl_croak(aTHX_ "panic: unknown regstclass %d", (int)OP(c));
73104a1b
KW
2616 }
2617 return 0;
2618 got_it:
2619 return s;
6eb5f6b9
JH
2620}
2621
60165aa4
DM
2622/* set RX_SAVED_COPY, RX_SUBBEG etc.
2623 * flags have same meanings as with regexec_flags() */
2624
749f4950
DM
2625static void
2626S_reg_set_capture_string(pTHX_ REGEXP * const rx,
60165aa4
DM
2627 char *strbeg,
2628 char *strend,
2629 SV *sv,
2630 U32 flags,
2631 bool utf8_target)
2632{
2633 struct regexp *const prog = ReANY(rx);
2634
60165aa4
DM
2635 if (flags & REXEC_COPY_STR) {
2636#ifdef PERL_ANY_COW
2637 if (SvCANCOW(sv)) {
2638 if (DEBUG_C_TEST) {
2639 PerlIO_printf(Perl_debug_log,
2640 "Copy on write: regexp capture, type %d\n",
2641 (int) SvTYPE(sv));
2642 }
5411a0e5
DM
2643 /* Create a new COW SV to share the match string and store
2644 * in saved_copy, unless the current COW SV in saved_copy
2645 * is valid and suitable for our purpose */
2646 if (( prog->saved_copy
2647 && SvIsCOW(prog->saved_copy)
2648 && SvPOKp(prog->saved_copy)
2649 && SvIsCOW(sv)
2650 && SvPOKp(sv)
2651 && SvPVX(sv) == SvPVX(prog->saved_copy)))
a76b0e90 2652 {
5411a0e5
DM
2653 /* just reuse saved_copy SV */
2654 if (RXp_MATCH_COPIED(prog)) {
2655 Safefree(prog->subbeg);
2656 RXp_MATCH_COPIED_off(prog);
2657 }
2658 }
2659 else {
2660 /* create new COW SV to share string */
a76b0e90
DM
2661 RX_MATCH_COPY_FREE(rx);
2662 prog->saved_copy = sv_setsv_cow(prog->saved_copy, sv);
a76b0e90 2663 }
5411a0e5
DM
2664 prog->subbeg = (char *)SvPVX_const(prog->saved_copy);
2665 assert (SvPOKp(prog->saved_copy));
60165aa4
DM
2666 prog->sublen = strend - strbeg;
2667 prog->suboffset = 0;
2668 prog->subcoffset = 0;
2669 } else
2670#endif
2671 {
99a90e59
FC
2672 SSize_t min = 0;
2673 SSize_t max = strend - strbeg;
ea3daa5d 2674 SSize_t sublen;
60165aa4
DM
2675
2676 if ( (flags & REXEC_COPY_SKIP_POST)
e322109a 2677 && !(prog->extflags & RXf_PMf_KEEPCOPY) /* //p */
60165aa4
DM
2678 && !(PL_sawampersand & SAWAMPERSAND_RIGHT)
2679 ) { /* don't copy $' part of string */
2680 U32 n = 0;
2681 max = -1;
2682 /* calculate the right-most part of the string covered
2683 * by a capture. Due to look-ahead, this may be to
2684 * the right of $&, so we have to scan all captures */
2685 while (n <= prog->lastparen) {
2686 if (prog->offs[n].end > max)
2687 max = prog->offs[n].end;
2688 n++;
2689 }
2690 if (max == -1)
2691 max = (PL_sawampersand & SAWAMPERSAND_LEFT)
2692 ? prog->offs[0].start
2693 : 0;
2694 assert(max >= 0 && max <= strend - strbeg);
2695 }
2696
2697 if ( (flags & REXEC_COPY_SKIP_PRE)
e322109a 2698 && !(prog->extflags & RXf_PMf_KEEPCOPY) /* //p */
60165aa4
DM
2699 && !(PL_sawampersand & SAWAMPERSAND_LEFT)
2700 ) { /* don't copy $` part of string */
2701 U32 n = 0;
2702 min = max;
2703 /* calculate the left-most part of the string covered
2704 * by a capture. Due to look-behind, this may be to
2705 * the left of $&, so we have to scan all captures */
2706 while (min && n <= prog->lastparen) {
2707 if ( prog->offs[n].start != -1
2708 && prog->offs[n].start < min)
2709 {
2710 min = prog->offs[n].start;
2711 }
2712 n++;
2713 }
2714 if ((PL_sawampersand & SAWAMPERSAND_RIGHT)
2715 && min > prog->offs[0].end
2716 )
2717 min = prog->offs[0].end;
2718
2719 }
2720
2721 assert(min >= 0 && min <= max && min <= strend - strbeg);
2722 sublen = max - min;
2723
2724 if (RX_MATCH_COPIED(rx)) {
2725 if (sublen > prog->sublen)
2726 prog->subbeg =
2727 (char*)saferealloc(prog->subbeg, sublen+1);
2728 }
2729 else
2730 prog->subbeg = (char*)safemalloc(sublen+1);
2731 Copy(strbeg + min, prog->subbeg, sublen, char);
2732 prog->subbeg[sublen] = '\0';
2733 prog->suboffset = min;
2734 prog->sublen = sublen;
2735 RX_MATCH_COPIED_on(rx);
2736 }
2737 prog->subcoffset = prog->suboffset;
2738 if (prog->suboffset && utf8_target) {
2739 /* Convert byte offset to chars.
2740 * XXX ideally should only compute this if @-/@+
2741 * has been seen, a la PL_sawampersand ??? */
2742
2743 /* If there's a direct correspondence between the
2744 * string which we're matching and the original SV,
2745 * then we can use the utf8 len cache associated with
2746 * the SV. In particular, it means that under //g,
2747 * sv_pos_b2u() will use the previously cached
2748 * position to speed up working out the new length of
2749 * subcoffset, rather than counting from the start of
2750 * the string each time. This stops
2751 * $x = "\x{100}" x 1E6; 1 while $x =~ /(.)/g;
2752 * from going quadratic */
2753 if (SvPOKp(sv) && SvPVX(sv) == strbeg)
ea3daa5d
FC
2754 prog->subcoffset = sv_pos_b2u_flags(sv, prog->subcoffset,
2755 SV_GMAGIC|SV_CONST_RETURN);
60165aa4
DM
2756 else
2757 prog->subcoffset = utf8_length((U8*)strbeg,
2758 (U8*)(strbeg+prog->suboffset));
2759 }
2760 }
2761 else {
2762 RX_MATCH_COPY_FREE(rx);
2763 prog->subbeg = strbeg;
2764 prog->suboffset = 0;
2765 prog->subcoffset = 0;
2766 prog->sublen = strend - strbeg;
2767 }
2768}
2769
2770
2771
fae667d5 2772
6eb5f6b9
JH
2773/*
2774 - regexec_flags - match a regexp against a string
2775 */
2776I32
5aaab254 2777Perl_regexec_flags(pTHX_ REGEXP * const rx, char *stringarg, char *strend,
ea3daa5d 2778 char *strbeg, SSize_t minend, SV *sv, void *data, U32 flags)
8fd1a950
DM
2779/* stringarg: the point in the string at which to begin matching */
2780/* strend: pointer to null at end of string */
2781/* strbeg: real beginning of string */
2782/* minend: end of match must be >= minend bytes after stringarg. */
2783/* sv: SV being matched: only used for utf8 flag, pos() etc; string
2784 * itself is accessed via the pointers above */
2785/* data: May be used for some additional optimizations.
d058ec57 2786 Currently unused. */
a340edde 2787/* flags: For optimizations. See REXEC_* in regexp.h */
8fd1a950 2788
6eb5f6b9 2789{
8d919b0a 2790 struct regexp *const prog = ReANY(rx);
5aaab254 2791 char *s;
eb578fdb 2792 regnode *c;
03c83e26 2793 char *startpos;
ea3daa5d
FC
2794 SSize_t minlen; /* must match at least this many chars */
2795 SSize_t dontbother = 0; /* how many characters not to try at end */
f2ed9b32 2796 const bool utf8_target = cBOOL(DO_UTF8(sv));
2757e526 2797 I32 multiline;
f8fc2ecf 2798 RXi_GET_DECL(prog,progi);
02d5137b
DM
2799 regmatch_info reginfo_buf; /* create some info to pass to regtry etc */
2800 regmatch_info *const reginfo = &reginfo_buf;
e9105d30 2801 regexp_paren_pair *swap = NULL;
006f26b2 2802 I32 oldsave;
a3621e74
YO
2803 GET_RE_DEBUG_FLAGS_DECL;
2804
7918f24d 2805 PERL_ARGS_ASSERT_REGEXEC_FLAGS;
9d4ba2ae 2806 PERL_UNUSED_ARG(data);
6eb5f6b9
JH
2807
2808 /* Be paranoid... */
3dc78631 2809 if (prog == NULL) {
6eb5f6b9 2810 Perl_croak(aTHX_ "NULL regexp parameter");
6eb5f6b9
JH
2811 }
2812
6c3fea77 2813 DEBUG_EXECUTE_r(
03c83e26 2814 debug_start_match(rx, utf8_target, stringarg, strend,
6c3fea77
DM
2815 "Matching");
2816 );
8adc0f72 2817
b342a604
DM
2818 startpos = stringarg;
2819
58430ea8 2820 if (prog->intflags & PREGf_GPOS_SEEN) {
d307c076
DM
2821 MAGIC *mg;
2822
fef7148b
DM
2823 /* set reginfo->ganch, the position where \G can match */
2824
2825 reginfo->ganch =
2826 (flags & REXEC_IGNOREPOS)
2827 ? stringarg /* use start pos rather than pos() */
3dc78631 2828 : ((mg = mg_find_mglob(sv)) && mg->mg_len >= 0)
25fdce4a
FC
2829 /* Defined pos(): */
2830 ? strbeg + MgBYTEPOS(mg, sv, strbeg, strend-strbeg)
fef7148b
DM
2831 : strbeg; /* pos() not defined; use start of string */
2832
2833 DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log,
7b0eb0b8 2834 "GPOS ganch set to strbeg[%"IVdf"]\n", (IV)(reginfo->ganch - strbeg)));
fef7148b 2835
03c83e26
DM
2836 /* in the presence of \G, we may need to start looking earlier in
2837 * the string than the suggested start point of stringarg:
0b2c2a84 2838 * if prog->gofs is set, then that's a known, fixed minimum
03c83e26
DM
2839 * offset, such as
2840 * /..\G/: gofs = 2
2841 * /ab|c\G/: gofs = 1
2842 * or if the minimum offset isn't known, then we have to go back
2843 * to the start of the string, e.g. /w+\G/
2844 */
2bfbe302 2845
8e1490ee 2846 if (prog->intflags & PREGf_ANCH_GPOS) {
2bfbe302
DM
2847 startpos = reginfo->ganch - prog->gofs;
2848 if (startpos <
2849 ((flags & REXEC_FAIL_ON_UNDERFLOW) ? stringarg : strbeg))
2850 {
2851 DEBUG_r(PerlIO_printf(Perl_debug_log,
2852 "fail: ganch-gofs before earliest possible start\n"));
2853 return 0;
2854 }
2855 }
2856 else if (prog->gofs) {
b342a604
DM
2857 if (startpos - prog->gofs < strbeg)
2858 startpos = strbeg;
2859 else
2860 startpos -= prog->gofs;
03c83e26 2861 }
58430ea8 2862 else if (prog->intflags & PREGf_GPOS_FLOAT)
b342a604 2863 startpos = strbeg;
03c83e26
DM
2864 }
2865
2866 minlen = prog->minlen;
b342a604 2867 if ((startpos + minlen) > strend || startpos < strbeg) {
03c83e26
DM
2868 DEBUG_r(PerlIO_printf(Perl_debug_log,
2869 "Regex match can't succeed, so not even tried\n"));
2870 return 0;
2871 }
2872
63a3746a
DM
2873 /* at the end of this function, we'll do a LEAVE_SCOPE(oldsave),
2874 * which will call destuctors to reset PL_regmatch_state, free higher
2875 * PL_regmatch_slabs, and clean up regmatch_info_aux and
2876 * regmatch_info_aux_eval */
2877
2878 oldsave = PL_savestack_ix;
2879
dfa77d06
DM
2880 s = startpos;
2881
e322109a 2882 if ((prog->extflags & RXf_USE_INTUIT)
7fadf4a7
DM
2883 && !(flags & REXEC_CHECKED))
2884 {
dfa77d06 2885 s = re_intuit_start(rx, sv, strbeg, startpos, strend,
7fadf4a7 2886 flags, NULL);
dfa77d06 2887 if (!s)
7fadf4a7
DM
2888 return 0;
2889
e322109a 2890 if (prog->extflags & RXf_CHECK_ALL) {
7fadf4a7
DM
2891 /* we can match based purely on the result of INTUIT.
2892 * Set up captures etc just for $& and $-[0]
2893 * (an intuit-only match wont have $1,$2,..) */
2894 assert(!prog->nparens);
d5e7783a
DM
2895
2896 /* s/// doesn't like it if $& is earlier than where we asked it to
2897 * start searching (which can happen on something like /.\G/) */
2898 if ( (flags & REXEC_FAIL_ON_UNDERFLOW)
2899 && (s < stringarg))
2900 {
2901 /* this should only be possible under \G */
58430ea8 2902 assert(prog->intflags & PREGf_GPOS_SEEN);
d5e7783a
DM
2903 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
2904 "matched, but failing for REXEC_FAIL_ON_UNDERFLOW\n"));
2905 goto phooey;
2906 }
2907
7fadf4a7
DM
2908 /* match via INTUIT shouldn't have any captures.
2909 * Let @-, @+, $^N know */
2910 prog->lastparen = prog->lastcloseparen = 0;
2911 RX_MATCH_UTF8_set(rx, utf8_target);
3ff69bd6
DM
2912 prog->offs[0].start = s - strbeg;
2913 prog->offs[0].end = utf8_target
2914 ? (char*)utf8_hop((U8*)s, prog->minlenret) - strbeg
2915 : s - strbeg + prog->minlenret;
7fadf4a7 2916 if ( !(flags & REXEC_NOT_FIRST) )
749f4950 2917 S_reg_set_capture_string(aTHX_ rx,
7fadf4a7
DM
2918 strbeg, strend,
2919 sv, flags, utf8_target);
2920
7fadf4a7
DM
2921 return 1;
2922 }
2923 }
2924
6c3fea77 2925 multiline = prog->extflags & RXf_PMf_MULTILINE;
1de06328 2926
dfa77d06 2927 if (strend - s < (minlen+(prog->check_offset_min<0?prog->check_offset_min:0))) {
a3621e74 2928 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
a72c7584
JH
2929 "String too short [regexec_flags]...\n"));
2930 goto phooey;
1aa99e6b 2931 }
1de06328 2932
6eb5f6b9 2933 /* Check validity of program. */
f8fc2ecf 2934 if (UCHARAT(progi->program) != REG_MAGIC) {
6eb5f6b9
JH
2935 Perl_croak(aTHX_ "corrupted regexp program");
2936 }
2937
1738e041 2938 RX_MATCH_TAINTED_off(rx);
ab4e48c1 2939 RX_MATCH_UTF8_set(rx, utf8_target);
1738e041 2940
6c3fea77
DM
2941 reginfo->prog = rx; /* Yes, sorry that this is confusing. */
2942 reginfo->intuit = 0;
2943 reginfo->is_utf8_target = cBOOL(utf8_target);
02d5137b
DM
2944 reginfo->is_utf8_pat = cBOOL(RX_UTF8(rx));
2945 reginfo->warned = FALSE;
9d9163fb 2946 reginfo->strbeg = strbeg;
02d5137b 2947 reginfo->sv = sv;
1cb48e53 2948 reginfo->poscache_maxiter = 0; /* not yet started a countdown */
220db18a 2949 reginfo->strend = strend;
6eb5f6b9 2950 /* see how far we have to get to not match where we matched before */
fe3974be 2951 reginfo->till = stringarg + minend;
6eb5f6b9 2952
60779a30 2953 if (prog->extflags & RXf_EVAL_SEEN && SvPADTMP(sv)) {
82c23608
FC
2954 /* SAVEFREESV, not sv_mortalcopy, as this SV must last until after
2955 S_cleanup_regmatch_info_aux has executed (registered by
2956 SAVEDESTRUCTOR_X below). S_cleanup_regmatch_info_aux modifies
2957 magic belonging to this SV.
2958 Not newSVsv, either, as it does not COW.
2959 */
2960 reginfo->sv = newSV(0);
4cba5ac0 2961 SvSetSV_nosteal(reginfo->sv, sv);
82c23608
FC
2962 SAVEFREESV(reginfo->sv);
2963 }
2964
331b2dcc
DM
2965 /* reserve next 2 or 3 slots in PL_regmatch_state:
2966 * slot N+0: may currently be in use: skip it
2967 * slot N+1: use for regmatch_info_aux struct
2968 * slot N+2: use for regmatch_info_aux_eval struct if we have (?{})'s
2969 * slot N+3: ready for use by regmatch()
2970 */
bf2039a9 2971
331b2dcc
DM
2972 {
2973 regmatch_state *old_regmatch_state;
2974 regmatch_slab *old_regmatch_slab;
2975 int i, max = (prog->extflags & RXf_EVAL_SEEN) ? 2 : 1;
2976
2977 /* on first ever match, allocate first slab */
2978 if (!PL_regmatch_slab) {
2979 Newx(PL_regmatch_slab, 1, regmatch_slab);
2980 PL_regmatch_slab->prev = NULL;
2981 PL_regmatch_slab->next = NULL;
2982 PL_regmatch_state = SLAB_FIRST(PL_regmatch_slab);
2983 }
bf2039a9 2984
331b2dcc
DM
2985 old_regmatch_state = PL_regmatch_state;
2986 old_regmatch_slab = PL_regmatch_slab;
bf2039a9 2987
331b2dcc
DM
2988 for (i=0; i <= max; i++) {
2989 if (i == 1)
2990 reginfo->info_aux = &(PL_regmatch_state->u.info_aux);
2991 else if (i ==2)
2992 reginfo->info_aux_eval =
2993 reginfo->info_aux->info_aux_eval =
2994 &(PL_regmatch_state->u.info_aux_eval);
bf2039a9 2995
331b2dcc
DM
2996 if (++PL_regmatch_state > SLAB_LAST(PL_regmatch_slab))
2997 PL_regmatch_state = S_push_slab(aTHX);
2998 }
bf2039a9 2999
331b2dcc
DM
3000 /* note initial PL_regmatch_state position; at end of match we'll
3001 * pop back to there and free any higher slabs */
bf2039a9 3002
331b2dcc
DM
3003 reginfo->info_aux->old_regmatch_state = old_regmatch_state;
3004 reginfo->info_aux->old_regmatch_slab = old_regmatch_slab;
2ac8ff4b 3005 reginfo->info_aux->poscache = NULL;
bf2039a9 3006
331b2dcc 3007 SAVEDESTRUCTOR_X(S_cleanup_regmatch_info_aux, reginfo->info_aux);
bf2039a9 3008
331b2dcc
DM
3009 if ((prog->extflags & RXf_EVAL_SEEN))
3010 S_setup_eval_state(aTHX_ reginfo);
3011 else
3012 reginfo->info_aux_eval = reginfo->info_aux->info_aux_eval = NULL;
bf2039a9 3013 }
d3aa529c 3014
6eb5f6b9 3015 /* If there is a "must appear" string, look for it. */
6eb5f6b9 3016
288b8c02 3017 if (PL_curpm && (PM_GETRE(PL_curpm) == rx)) {
e9105d30
GG
3018 /* We have to be careful. If the previous successful match
3019 was from this regex we don't want a subsequent partially
3020 successful match to clobber the old results.
3021 So when we detect this possibility we add a swap buffer
d8da0584
KW
3022 to the re, and switch the buffer each match. If we fail,
3023 we switch it back; otherwise we leave it swapped.
e9105d30
GG
3024 */
3025 swap = prog->offs;
3026 /* do we need a save destructor here for eval dies? */
3027 Newxz(prog->offs, (prog->nparens + 1), regexp_paren_pair);
495f47a5
DM
3028 DEBUG_BUFFERS_r(PerlIO_printf(Perl_debug_log,
3029 "rex=0x%"UVxf" saving offs: orig=0x%"UVxf" new=0x%"UVxf"\n",
3030 PTR2UV(prog),
3031 PTR2UV(swap),
3032 PTR2UV(prog->offs)
3033 ));
c74340f9 3034 }
6eb5f6b9 3035
0fa70a06
DM
3036 /* Simplest case: anchored match need be tried only once, or with
3037 * MBOL, only at the beginning of each line.
3038 *
3039 * Note that /.*.../ sets PREGf_IMPLICIT|MBOL, while /.*.../s sets
3040 * PREGf_IMPLICIT|SBOL. The idea is that with /.*.../s, if it doesn't
3041 * match at the start of the string then it won't match anywhere else
3042 * either; while with /.*.../, if it doesn't match at the beginning,
3043 * the earliest it could match is at the start of the next line */
3044
8e1490ee 3045 if (prog->intflags & (PREGf_ANCH & ~PREGf_ANCH_GPOS)) {
0fa70a06
DM
3046 char *end;
3047
3048 if (regtry(reginfo, &s))
6eb5f6b9 3049 goto got_it;
0fa70a06
DM
3050
3051 if (!(prog->intflags & PREGf_ANCH_MBOL))
3052 goto phooey;
3053
3054 /* didn't match at start, try at other newline positions */
3055
3056 if (minlen)
3057 dontbother = minlen - 1;
3058 end = HOP3c(strend, -dontbother, strbeg) - 1;
3059
3060 /* skip to next newline */
3061
3062 while (s <= end) { /* note it could be possible to match at the end of the string */
3063 /* NB: newlines are the same in unicode as they are in latin */
3064 if (*s++ != '\n')
3065 continue;
3066 if (prog->check_substr || prog->check_utf8) {
3067 /* note that with PREGf_IMPLICIT, intuit can only fail
3068 * or return the start position, so it's of limited utility.
3069 * Nevertheless, I made the decision that the potential for
3070 * quick fail was still worth it - DAPM */
3071 s = re_intuit_start(rx, sv, strbeg, s, strend, flags, NULL);
3072 if (!s)
3073 goto phooey;
3074 }
3075 if (regtry(reginfo, &s))
3076 goto got_it;
3077 }
3078 goto phooey;
3079 } /* end anchored search */
3080
3081 if (prog->intflags & PREGf_ANCH_GPOS)
f9f4320a 3082 {
a8430a8b
YO
3083 /* PREGf_ANCH_GPOS should never be true if PREGf_GPOS_SEEN is not true */
3084 assert(prog->intflags & PREGf_GPOS_SEEN);
2bfbe302
DM
3085 /* For anchored \G, the only position it can match from is
3086 * (ganch-gofs); we already set startpos to this above; if intuit
3087 * moved us on from there, we can't possibly succeed */
3088 assert(startpos == reginfo->ganch - prog->gofs);
3089 if (s == startpos && regtry(reginfo, &s))
6eb5f6b9
JH
3090 goto got_it;
3091 goto phooey;
3092 }
3093
3094 /* Messy cases: unanchored match. */
bbe252da 3095 if ((prog->anchored_substr || prog->anchored_utf8) && prog->intflags & PREGf_SKIP) {
6eb5f6b9 3096 /* we have /x+whatever/ */
984e6dd1 3097 /* it must be a one character string (XXXX Except is_utf8_pat?) */
33b8afdf 3098 char ch;
bf93d4cc
GS
3099#ifdef DEBUGGING
3100 int did_match = 0;
3101#endif
f2ed9b32 3102 if (utf8_target) {
7e0d5ad7
KW
3103 if (! prog->anchored_utf8) {
3104 to_utf8_substr(prog);
3105 }
3106 ch = SvPVX_const(prog->anchored_utf8)[0];
4cadc6a9 3107 REXEC_FBC_SCAN(
6eb5f6b9 3108 if (*s == ch) {
a3621e74 3109 DEBUG_EXECUTE_r( did_match = 1 );
02d5137b 3110 if (regtry(reginfo, &s)) goto got_it;
6eb5f6b9
JH
3111 s += UTF8SKIP(s);
3112 while (s < strend && *s == ch)
3113 s += UTF8SKIP(s);
3114 }
4cadc6a9 3115 );
7e0d5ad7 3116
6eb5f6b9
JH
3117 }
3118 else {
7e0d5ad7
KW
3119 if (! prog->anchored_substr) {
3120 if (! to_byte_substr(prog)) {
6b54ddc5 3121 NON_UTF8_TARGET_BUT_UTF8_REQUIRED(phooey);
7e0d5ad7
KW
3122 }
3123 }
3124 ch = SvPVX_const(prog->anchored_substr)[0];
4cadc6a9 3125 REXEC_FBC_SCAN(
6eb5f6b9 3126 if (*s == ch) {
a3621e74 3127 DEBUG_EXECUTE_r( did_match = 1 );
02d5137b 3128 if (regtry(reginfo, &s)) goto got_it;
6eb5f6b9
JH
3129 s++;
3130 while (s < strend && *s == ch)
3131 s++;
3132 }
4cadc6a9 3133 );
6eb5f6b9 3134 }
a3621e74 3135 DEBUG_EXECUTE_r(if (!did_match)
bf93d4cc 3136 PerlIO_printf(Perl_debug_log,
b7953727
JH
3137 "Did not find anchored character...\n")
3138 );
6eb5f6b9 3139 }
a0714e2c
SS
3140 else if (prog->anchored_substr != NULL
3141 || prog->anchored_utf8 != NULL
3142 || ((prog->float_substr != NULL || prog->float_utf8 != NULL)
33b8afdf
JH
3143 && prog->float_max_offset < strend - s)) {
3144 SV *must;
ea3daa5d
FC
3145 SSize_t back_max;
3146 SSize_t back_min;
33b8afdf 3147 char *last;
6eb5f6b9 3148 char *last1; /* Last position checked before */
bf93d4cc
GS
3149#ifdef DEBUGGING
3150 int did_match = 0;
3151#endif
33b8afdf 3152 if (prog->anchored_substr || prog->anchored_utf8) {
7e0d5ad7
KW
3153 if (utf8_target) {
3154 if (! prog->anchored_utf8) {
3155 to_utf8_substr(prog);
3156 }
3157 must = prog->anchored_utf8;
3158 }
3159 else {
3160 if (! prog->anchored_substr) {
3161 if (! to_byte_substr(prog)) {
6b54ddc5 3162 NON_UTF8_TARGET_BUT_UTF8_REQUIRED(phooey);
7e0d5ad7
KW
3163 }
3164 }
3165 must = prog->anchored_substr;
3166 }
33b8afdf
JH
3167 back_max = back_min = prog->anchored_offset;
3168 } else {
7e0d5ad7
KW
3169 if (utf8_target) {
3170 if (! prog->float_utf8) {
3171 to_utf8_substr(prog);
3172 }
3173 must = prog->float_utf8;
3174 }
3175 else {
3176 if (! prog->float_substr) {
3177 if (! to_byte_substr(prog)) {
6b54ddc5 3178 NON_UTF8_TARGET_BUT_UTF8_REQUIRED(phooey);
7e0d5ad7
KW
3179 }
3180 }
3181 must = prog->float_substr;
3182 }
33b8afdf
JH
3183 back_max = prog->float_max_offset;
3184 back_min = prog->float_min_offset;
3185 }
1de06328 3186
1de06328
YO
3187 if (back_min<0) {
3188 last = strend;
3189 } else {
3190 last = HOP3c(strend, /* Cannot start after this */
ea3daa5d 3191 -(SSize_t)(CHR_SVLEN(must)
1de06328
YO
3192 - (SvTAIL(must) != 0) + back_min), strbeg);
3193 }
9d9163fb 3194 if (s > reginfo->strbeg)
6eb5f6b9
JH
3195 last1 = HOPc(s, -1);
3196 else
3197 last1 = s - 1; /* bogus */
3198
a0288114 3199 /* XXXX check_substr already used to find "s", can optimize if
6eb5f6b9 3200 check_substr==must. */
bf05793b 3201 dontbother = 0;
6eb5f6b9
JH
3202 strend = HOPc(strend, -dontbother);
3203 while ( (s <= last) &&
e50d57d4 3204 (s = fbm_instr((unsigned char*)HOP4c(s, back_min, strbeg, strend),
9041c2e3 3205 (unsigned char*)strend, must,
c33e64f0 3206 multiline ? FBMrf_MULTILINE : 0)) ) {
a3621e74 3207 DEBUG_EXECUTE_r( did_match = 1 );
6eb5f6b9
JH
3208 if (HOPc(s, -back_max) > last1) {
3209 last1 = HOPc(s, -back_min);
3210 s = HOPc(s, -back_max);
3211 }
3212 else {
9d9163fb
DM
3213 char * const t = (last1 >= reginfo->strbeg)
3214 ? HOPc(last1, 1) : last1 + 1;
6eb5f6b9
JH
3215
3216 last1 = HOPc(s, -back_min);
52657f30 3217 s = t;
6eb5f6b9 3218 }
f2ed9b32 3219 if (utf8_target) {
6eb5f6b9 3220 while (s <= last1) {
02d5137b 3221 if (regtry(reginfo, &s))
6eb5f6b9 3222 goto got_it;
7016d6eb
DM
3223 if (s >= last1) {
3224 s++; /* to break out of outer loop */
3225 break;
3226 }
3227 s += UTF8SKIP(s);
6eb5f6b9
JH
3228 }
3229 }
3230 else {
3231 while (s <= last1) {
02d5137b 3232 if (regtry(reginfo, &s))
6eb5f6b9
JH
3233 goto got_it;
3234 s++;
3235 }
3236 }
3237 }
ab3bbdeb 3238 DEBUG_EXECUTE_r(if (!did_match) {
f2ed9b32 3239 RE_PV_QUOTED_DECL(quoted, utf8_target, PERL_DEBUG_PAD_ZERO(0),
ab3bbdeb
YO
3240 SvPVX_const(must), RE_SV_DUMPLEN(must), 30);
3241 PerlIO_printf(Perl_debug_log, "Did not find %s substr %s%s...\n",
33b8afdf 3242 ((must == prog->anchored_substr || must == prog->anchored_utf8)
bf93d4cc 3243 ? "anchored" : "floating"),
ab3bbdeb
YO
3244 quoted, RE_SV_TAIL(must));
3245 });
6eb5f6b9
JH
3246 goto phooey;
3247 }
f8fc2ecf 3248 else if ( (c = progi->regstclass) ) {
f14c76ed 3249 if (minlen) {
f8fc2ecf 3250 const OPCODE op = OP(progi->regstclass);
66e933ab 3251 /* don't bother with what can't match */
33c28ab2 3252 if (PL_regkind[op] != EXACT && PL_regkind[op] != TRIE)
f14c76ed
RGS
3253 strend = HOPc(strend, -(minlen - 1));
3254 }
a3621e74 3255 DEBUG_EXECUTE_r({
be8e71aa 3256 SV * const prop = sv_newmortal();
8b9781c9 3257 regprop(prog, prop, c, reginfo, NULL);
0df25f3d 3258 {
f2ed9b32 3259 RE_PV_QUOTED_DECL(quoted,utf8_target,PERL_DEBUG_PAD_ZERO(1),
ab3bbdeb 3260 s,strend-s,60);
0df25f3d 3261 PerlIO_printf(Perl_debug_log,
1c8f8eb1 3262 "Matching stclass %.*s against %s (%d bytes)\n",
e4f74956 3263 (int)SvCUR(prop), SvPVX_const(prop),
ab3bbdeb 3264 quoted, (int)(strend - s));
0df25f3d 3265 }
ffc61ed2 3266 });
f9176b44 3267 if (find_byclass(prog, c, s, strend, reginfo))
6eb5f6b9 3268 goto got_it;
07be1b83 3269 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Contradicts stclass... [regexec_flags]\n"));
d6a28714
JH
3270 }
3271 else {
3272 dontbother = 0;
a0714e2c 3273 if (prog->float_substr != NULL || prog->float_utf8 != NULL) {
33b8afdf 3274 /* Trim the end. */
6af40bd7 3275 char *last= NULL;
33b8afdf 3276 SV* float_real;
c33e64f0
FC
3277 STRLEN len;
3278 const char *little;
33b8afdf 3279
7e0d5ad7
KW
3280 if (utf8_target) {
3281 if (! prog->float_utf8) {
3282 to_utf8_substr(prog);
3283 }
3284 float_real = prog->float_utf8;
3285 }
3286 else {
3287 if (! prog->float_substr) {
3288 if (! to_byte_substr(prog)) {
6b54ddc5 3289 NON_UTF8_TARGET_BUT_UTF8_REQUIRED(phooey);
7e0d5ad7
KW
3290 }
3291 }
3292 float_real = prog->float_substr;
3293 }
d6a28714 3294
c33e64f0
FC
3295 little = SvPV_const(float_real, len);
3296 if (SvTAIL(float_real)) {
7f18ad16
KW
3297 /* This means that float_real contains an artificial \n on
3298 * the end due to the presence of something like this:
3299 * /foo$/ where we can match both "foo" and "foo\n" at the
3300 * end of the string. So we have to compare the end of the
3301 * string first against the float_real without the \n and
3302 * then against the full float_real with the string. We
3303 * have to watch out for cases where the string might be
3304 * smaller than the float_real or the float_real without
3305 * the \n. */
1a13b075
YO
3306 char *checkpos= strend - len;
3307 DEBUG_OPTIMISE_r(
3308 PerlIO_printf(Perl_debug_log,
3309 "%sChecking for float_real.%s\n",
3310 PL_colors[4], PL_colors[5]));
3311 if (checkpos + 1 < strbeg) {
7f18ad16
KW
3312 /* can't match, even if we remove the trailing \n
3313 * string is too short to match */
1a13b075
YO
3314 DEBUG_EXECUTE_r(
3315 PerlIO_printf(Perl_debug_log,
3316 "%sString shorter than required trailing substring, cannot match.%s\n",
3317 PL_colors[4], PL_colors[5]));
3318 goto phooey;
3319 } else if (memEQ(checkpos + 1, little, len - 1)) {
7f18ad16
KW
3320 /* can match, the end of the string matches without the
3321 * "\n" */
1a13b075
YO
3322 last = checkpos + 1;
3323 } else if (checkpos < strbeg) {
7f18ad16
KW
3324 /* cant match, string is too short when the "\n" is
3325 * included */
1a13b075
YO
3326 DEBUG_EXECUTE_r(
3327 PerlIO_printf(Perl_debug_log,
3328 "%sString does not contain required trailing substring, cannot match.%s\n",
3329 PL_colors[4], PL_colors[5]));
3330 goto phooey;
3331 } else if (!multiline) {
7f18ad16
KW
3332 /* non multiline match, so compare with the "\n" at the
3333 * end of the string */
1a13b075
YO
3334 if (memEQ(checkpos, little, len)) {
3335 last= checkpos;
3336 } else {
3337 DEBUG_EXECUTE_r(
3338 PerlIO_printf(Perl_debug_log,
3339 "%sString does not contain required trailing substring, cannot match.%s\n",
3340 PL_colors[4], PL_colors[5]));
3341 goto phooey;
3342 }
3343 } else {
7f18ad16
KW
3344 /* multiline match, so we have to search for a place
3345 * where the full string is located */
d6a28714 3346 goto find_last;
1a13b075 3347 }
c33e64f0 3348 } else {
d6a28714 3349 find_last:
9041c2e3 3350 if (len)
d6a28714 3351 last = rninstr(s, strend, little, little + len);
b8c5462f 3352 else
a0288114 3353 last = strend; /* matching "$" */
b8c5462f 3354 }
6af40bd7 3355 if (!last) {
7f18ad16
KW
3356 /* at one point this block contained a comment which was
3357 * probably incorrect, which said that this was a "should not
3358 * happen" case. Even if it was true when it was written I am
3359 * pretty sure it is not anymore, so I have removed the comment
3360 * and replaced it with this one. Yves */
6bda09f9
YO
3361 DEBUG_EXECUTE_r(
3362 PerlIO_printf(Perl_debug_log,
b729e729
YO
3363 "%sString does not contain required substring, cannot match.%s\n",
3364 PL_colors[4], PL_colors[5]
6af40bd7
YO
3365 ));
3366 goto phooey;
bf93d4cc 3367 }
d6a28714
JH
3368 dontbother = strend - last + prog->float_min_offset;
3369 }
3370 if (minlen && (dontbother < minlen))
3371 dontbother = minlen - 1;
3372 strend -= dontbother; /* this one's always in bytes! */
3373 /* We don't know much -- general case. */
f2ed9b32 3374 if (utf8_target) {
d6a28714 3375 for (;;) {
02d5137b 3376 if (regtry(reginfo, &s))
d6a28714
JH
3377 goto got_it;
3378 if (s >= strend)
3379 break;
b8c5462f 3380 s += UTF8SKIP(s);
d6a28714
JH
3381 };
3382 }
3383 else {
3384 do {
02d5137b 3385 if (regtry(reginfo, &s))
d6a28714
JH
3386 goto got_it;
3387 } while (s++ < strend);
3388 }
3389 }
3390
3391 /* Failure. */
3392 goto phooey;
3393
7b52d656 3394 got_it:
d5e7783a
DM
3395 /* s/// doesn't like it if $& is earlier than where we asked it to
3396 * start searching (which can happen on something like /.\G/) */
3397 if ( (flags & REXEC_FAIL_ON_UNDERFLOW)
3398 && (prog->offs[0].start < stringarg - strbeg))
3399 {
3400 /* this should only be possible under \G */
58430ea8 3401 assert(prog->intflags & PREGf_GPOS_SEEN);
d5e7783a
DM
3402 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log,
3403 "matched, but failing for REXEC_FAIL_ON_UNDERFLOW\n"));
3404 goto phooey;
3405 }
3406
495f47a5
DM
3407 DEBUG_BUFFERS_r(
3408 if (swap)
3409 PerlIO_printf(Perl_debug_log,
3410 "rex=0x%"UVxf" freeing offs: 0x%"UVxf"\n",
3411 PTR2UV(prog),
3412 PTR2UV(swap)
3413 );
3414 );
e9105d30 3415 Safefree(swap);
d6a28714 3416
bf2039a9
DM
3417 /* clean up; this will trigger destructors that will free all slabs
3418 * above the current one, and cleanup the regmatch_info_aux
3419 * and regmatch_info_aux_eval sructs */
8adc0f72 3420
006f26b2
DM
3421 LEAVE_SCOPE(oldsave);
3422
5daac39c
NC
3423 if (RXp_PAREN_NAMES(prog))
3424 (void)hv_iterinit(RXp_PAREN_NAMES(prog));
d6a28714
JH
3425
3426 /* make sure $`, $&, $', and $digit will work later */
60165aa4 3427 if ( !(flags & REXEC_NOT_FIRST) )
749f4950 3428 S_reg_set_capture_string(aTHX_ rx,
60165aa4
DM
3429 strbeg, reginfo->strend,
3430 sv, flags, utf8_target);
9041c2e3 3431
d6a28714
JH
3432 return 1;
3433
7b52d656 3434 phooey:
a3621e74 3435 DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch failed%s\n",
e4584336 3436 PL_colors[4], PL_colors[5]));
8adc0f72 3437
bf2039a9
DM
3438 /* clean up; this will trigger destructors that will free all slabs
3439 * above the current one, and cleanup the regmatch_info_aux
3440 * and regmatch_info_aux_eval sructs */
8adc0f72 3441
006f26b2
DM
3442 LEAVE_SCOPE(oldsave);
3443
e9105d30 3444 if (swap) {
c74340f9 3445 /* we failed :-( roll it back */
495f47a5
DM
3446 DEBUG_BUFFERS_r(PerlIO_printf(Perl_debug_log,
3447 "rex=0x%"UVxf" rolling back offs: freeing=0x%"UVxf" restoring=0x%"UVxf"\n",
3448 PTR2UV(prog),
3449 PTR2UV(prog->offs),
3450 PTR2UV(swap)
3451 ));
e9105d30
GG
3452 Safefree(prog->offs);
3453 prog->offs = swap;
3454 }
d6a28714
JH
3455 return 0;
3456}
3457
6bda09f9 3458
b3d298be 3459/* Set which rex is pointed to by PL_reg_curpm, handling ref counting.
ec43f78b 3460 * Do inc before dec, in case old and new rex are the same */
baa60164 3461#define SET_reg_curpm(Re2) \
bf2039a9 3462 if (reginfo->info_aux_eval) { \
ec43f78b
DM
3463 (void)ReREFCNT_inc(Re2); \
3464 ReREFCNT_dec(PM_GETRE(PL_reg_curpm)); \
3465 PM_SETRE((PL_reg_curpm), (Re2)); \
3466 }
3467
3468
d6a28714
JH
3469/*
3470 - regtry - try match at specific point
3471 */
3472STATIC I32 /* 0 failure, 1 success */
f73aaa43 3473S_regtry(pTHX_ regmatch_info *reginfo, char **startposp)
d6a28714 3474{
d6a28714 3475 CHECKPOINT lastcp;
288b8c02 3476 REGEXP *const rx = reginfo->prog;
8d919b0a 3477 regexp *const prog = ReANY(rx);
99a90e59 3478 SSize_t result;
f8fc2ecf 3479 RXi_GET_DECL(prog,progi);
a3621e74 3480 GET_RE_DEBUG_FLAGS_DECL;
7918f24d
NC
3481
3482 PERL_ARGS_ASSERT_REGTRY;
3483
24b23f37 3484 reginfo->cutpoint=NULL;
d6a28714 3485
9d9163fb 3486 prog->offs[0].start = *startposp - reginfo->strbeg;
d6a28714 3487 prog->lastparen = 0;
03994de8 3488 prog->lastcloseparen = 0;
d6a28714
JH
3489
3490 /* XXXX What this code is doing here?!!! There should be no need
b93070ed 3491 to do this again and again, prog->lastparen should take care of
3dd2943c 3492 this! --ilya*/
dafc8851
JH
3493
3494 /* Tests pat.t#187 and split.t#{13,14} seem to depend on this code.
3495 * Actually, the code in regcppop() (which Ilya may be meaning by
b93070ed 3496 * prog->lastparen), is not needed at all by the test suite
225593e1
DM
3497 * (op/regexp, op/pat, op/split), but that code is needed otherwise
3498 * this erroneously leaves $1 defined: "1" =~ /^(?:(\d)x)?\d$/
3499 * Meanwhile, this code *is* needed for the
daf18116
JH
3500 * above-mentioned test suite tests to succeed. The common theme
3501 * on those tests seems to be returning null fields from matches.
225593e1 3502 * --jhi updated by dapm */
dafc8851 3503#if 1
d6a28714 3504 if (prog->nparens) {
b93070ed 3505 regexp_paren_pair *pp = prog->offs;
eb578fdb 3506 I32 i;
b93070ed 3507 for (i = prog->nparens; i > (I32)prog->lastparen; i--) {
f0ab9afb
NC
3508 ++pp;
3509 pp->start = -1;
3510 pp->end = -1;
d6a28714
JH
3511 }
3512 }
dafc8851 3513#endif
02db2b7b 3514 REGCP_SET(lastcp);
f73aaa43
DM
3515 result = regmatch(reginfo, *startposp, progi->program + 1);
3516 if (result != -1) {
3517 prog->offs[0].end = result;
d6a28714
JH
3518 return 1;
3519 }
24b23f37 3520 if (reginfo->cutpoint)
f73aaa43 3521 *startposp= reginfo->cutpoint;
02db2b7b 3522 REGCP_UNWIND(lastcp);
d6a28714
JH
3523 return 0;
3524}
3525
02db2b7b 3526
8ba1375e
MJD
3527#define sayYES goto yes
3528#define sayNO goto no
262b90c4 3529#define sayNO_SILENT goto no_silent
8ba1375e 3530
f9f4320a
YO
3531/* we dont use STMT_START/END here because it leads to
3532 "unreachable code" warnings, which are bogus, but distracting. */
3533#define CACHEsayNO \
c476f425 3534 if (ST.cache_mask) \
2ac8ff4b 3535 reginfo->info_aux->poscache[ST.cache_offset] |= ST.cache_mask; \
f9f4320a 3536 sayNO
3298f257 3537
a3621e74 3538/* this is used to determine how far from the left messages like
265c4333
YO
3539 'failed...' are printed. It should be set such that messages
3540 are inline with the regop output that created them.
a3621e74 3541*/
265c4333 3542#define REPORT_CODE_OFF 32
a3621e74
YO
3543
3544
40a82448
DM
3545#define CHRTEST_UNINIT -1001 /* c1/c2 haven't been calculated yet */
3546#define CHRTEST_VOID -1000 /* the c1/c2 "next char" test should be skipped */
79a2a0e8
KW
3547#define CHRTEST_NOT_A_CP_1 -999
3548#define CHRTEST_NOT_A_CP_2 -998
9e137952 3549
5d9a96ca
DM
3550/* grab a new slab and return the first slot in it */
3551
3552STATIC regmatch_state *
3553S_push_slab(pTHX)
3554{