Commit | Line | Data |
---|---|---|
a0d0e21e LW |
1 | /* regexec.c |
2 | */ | |
3 | ||
4 | /* | |
4ac71550 TC |
5 | * One Ring to rule them all, One Ring to find them |
6 | & | |
7 | * [p.v of _The Lord of the Rings_, opening poem] | |
8 | * [p.50 of _The Lord of the Rings_, I/iii: "The Shadow of the Past"] | |
9 | * [p.254 of _The Lord of the Rings_, II/ii: "The Council of Elrond"] | |
a0d0e21e LW |
10 | */ |
11 | ||
61296642 DM |
12 | /* This file contains functions for executing a regular expression. See |
13 | * also regcomp.c which funnily enough, contains functions for compiling | |
166f8a29 | 14 | * a regular expression. |
e4a054ea DM |
15 | * |
16 | * This file is also copied at build time to ext/re/re_exec.c, where | |
17 | * it's built with -DPERL_EXT_RE_BUILD -DPERL_EXT_RE_DEBUG -DPERL_EXT. | |
18 | * This causes the main functions to be compiled under new names and with | |
19 | * debugging support added, which makes "use re 'debug'" work. | |
166f8a29 DM |
20 | */ |
21 | ||
a687059c LW |
22 | /* NOTE: this is derived from Henry Spencer's regexp code, and should not |
23 | * confused with the original package (see point 3 below). Thanks, Henry! | |
24 | */ | |
25 | ||
26 | /* Additional note: this code is very heavily munged from Henry's version | |
27 | * in places. In some spots I've traded clarity for efficiency, so don't | |
28 | * blame Henry for some of the lack of readability. | |
29 | */ | |
30 | ||
e50aee73 AD |
31 | /* The names of the functions have been changed from regcomp and |
32 | * regexec to pregcomp and pregexec in order to avoid conflicts | |
33 | * with the POSIX routines of the same names. | |
34 | */ | |
35 | ||
b9d5759e | 36 | #ifdef PERL_EXT_RE_BUILD |
54df2634 | 37 | #include "re_top.h" |
9041c2e3 | 38 | #endif |
56953603 | 39 | |
a687059c | 40 | /* |
e50aee73 | 41 | * pregcomp and pregexec -- regsub and regerror are not used in perl |
a687059c LW |
42 | * |
43 | * Copyright (c) 1986 by University of Toronto. | |
44 | * Written by Henry Spencer. Not derived from licensed software. | |
45 | * | |
46 | * Permission is granted to anyone to use this software for any | |
47 | * purpose on any computer system, and to redistribute it freely, | |
48 | * subject to the following restrictions: | |
49 | * | |
50 | * 1. The author is not responsible for the consequences of use of | |
51 | * this software, no matter how awful, even if they arise | |
52 | * from defects in it. | |
53 | * | |
54 | * 2. The origin of this software must not be misrepresented, either | |
55 | * by explicit claim or by omission. | |
56 | * | |
57 | * 3. Altered versions must be plainly marked as such, and must not | |
58 | * be misrepresented as being the original software. | |
59 | * | |
60 | **** Alterations to Henry's code are... | |
61 | **** | |
4bb101f2 | 62 | **** Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, |
1129b882 NC |
63 | **** 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 |
64 | **** by Larry Wall and others | |
a687059c | 65 | **** |
9ef589d8 LW |
66 | **** You may distribute under the terms of either the GNU General Public |
67 | **** License or the Artistic License, as specified in the README file. | |
a687059c LW |
68 | * |
69 | * Beware that some of this code is subtly aware of the way operator | |
70 | * precedence is structured in regular expressions. Serious changes in | |
71 | * regular-expression syntax might require a total rethink. | |
72 | */ | |
73 | #include "EXTERN.h" | |
864dbfa3 | 74 | #define PERL_IN_REGEXEC_C |
a687059c | 75 | #include "perl.h" |
0f5d15d6 | 76 | |
54df2634 NC |
77 | #ifdef PERL_IN_XSUB_RE |
78 | # include "re_comp.h" | |
79 | #else | |
80 | # include "regcomp.h" | |
81 | #endif | |
a687059c | 82 | |
81e983c1 | 83 | #include "inline_invlist.c" |
1b0f46bf | 84 | #include "unicode_constants.h" |
81e983c1 | 85 | |
e1cf74e3 CB |
86 | #ifdef DEBUGGING |
87 | /* At least one required character in the target string is expressible only in | |
88 | * UTF-8. */ | |
89 | static const char* const non_utf8_target_but_utf8_required | |
90 | = "Can't match, because target string needs to be in UTF-8\n"; | |
91 | #endif | |
92 | ||
93 | #define NON_UTF8_TARGET_BUT_UTF8_REQUIRED(target) STMT_START { \ | |
94 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%s", non_utf8_target_but_utf8_required));\ | |
95 | goto target; \ | |
96 | } STMT_END | |
97 | ||
c74f6de9 KW |
98 | #define HAS_NONLATIN1_FOLD_CLOSURE(i) _HAS_NONLATIN1_FOLD_CLOSURE_ONLY_FOR_USE_BY_REGCOMP_DOT_C_AND_REGEXEC_DOT_C(i) |
99 | ||
a687059c LW |
100 | #ifndef STATIC |
101 | #define STATIC static | |
102 | #endif | |
103 | ||
2d66f61e | 104 | /* Valid only for non-utf8 strings: avoids the reginclass |
7e2509c1 KW |
105 | * call if there are no complications: i.e., if everything matchable is |
106 | * straight forward in the bitmap */ | |
3db24e1e | 107 | #define REGINCLASS(prog,p,c) (ANYOF_FLAGS(p) ? reginclass(prog,p,c,c+1,0) \ |
af364d03 | 108 | : ANYOF_BITMAP_TEST(p,*(c))) |
7d3e948e | 109 | |
c277df42 IZ |
110 | /* |
111 | * Forwards. | |
112 | */ | |
113 | ||
f2ed9b32 | 114 | #define CHR_SVLEN(sv) (utf8_target ? sv_len_utf8(sv) : SvCUR(sv)) |
ba44c216 | 115 | #define CHR_DIST(a,b) (reginfo->is_utf8_target ? utf8_distance(a,b) : a - b) |
a0ed51b3 | 116 | |
3dab1dad | 117 | #define HOPc(pos,off) \ |
ba44c216 | 118 | (char *)(reginfo->is_utf8_target \ |
220db18a | 119 | ? reghop3((U8*)pos, off, \ |
9d9163fb | 120 | (U8*)(off >= 0 ? reginfo->strend : reginfo->strbeg)) \ |
3dab1dad | 121 | : (U8*)(pos + off)) |
557f47af | 122 | |
3dab1dad | 123 | #define HOPBACKc(pos, off) \ |
ba44c216 | 124 | (char*)(reginfo->is_utf8_target \ |
9d9163fb DM |
125 | ? reghopmaybe3((U8*)pos, -off, (U8*)(reginfo->strbeg)) \ |
126 | : (pos - off >= reginfo->strbeg) \ | |
8e11feef | 127 | ? (U8*)pos - off \ |
3dab1dad | 128 | : NULL) |
efb30f32 | 129 | |
ba44c216 | 130 | #define HOP3(pos,off,lim) (reginfo->is_utf8_target ? reghop3((U8*)(pos), off, (U8*)(lim)) : (U8*)(pos + off)) |
1aa99e6b | 131 | #define HOP3c(pos,off,lim) ((char*)HOP3(pos,off,lim)) |
1aa99e6b | 132 | |
557f47af DM |
133 | /* lim must be +ve. Returns NULL on overshoot */ |
134 | #define HOPMAYBE3(pos,off,lim) \ | |
135 | (reginfo->is_utf8_target \ | |
136 | ? reghopmaybe3((U8*)pos, off, (U8*)(lim)) \ | |
137 | : ((U8*)pos + off <= lim) \ | |
138 | ? (U8*)pos + off \ | |
139 | : NULL) | |
140 | ||
8e9f2289 DM |
141 | /* like HOP3, but limits the result to <= lim even for the non-utf8 case. |
142 | * off must be >=0; args should be vars rather than expressions */ | |
143 | #define HOP3lim(pos,off,lim) (reginfo->is_utf8_target \ | |
144 | ? reghop3((U8*)(pos), off, (U8*)(lim)) \ | |
145 | : (U8*)((pos + off) > lim ? lim : (pos + off))) | |
146 | ||
2974eaec DM |
147 | #define HOP4(pos,off,llim, rlim) (reginfo->is_utf8_target \ |
148 | ? reghop4((U8*)(pos), off, (U8*)(llim), (U8*)(rlim)) \ | |
149 | : (U8*)(pos + off)) | |
150 | #define HOP4c(pos,off,llim, rlim) ((char*)HOP4(pos,off,llim, rlim)) | |
7016d6eb DM |
151 | |
152 | #define NEXTCHR_EOS -10 /* nextchr has fallen off the end */ | |
153 | #define NEXTCHR_IS_EOS (nextchr < 0) | |
154 | ||
155 | #define SET_nextchr \ | |
220db18a | 156 | nextchr = ((locinput < reginfo->strend) ? UCHARAT(locinput) : NEXTCHR_EOS) |
7016d6eb DM |
157 | |
158 | #define SET_locinput(p) \ | |
159 | locinput = (p); \ | |
160 | SET_nextchr | |
161 | ||
162 | ||
2a16ac92 | 163 | #define LOAD_UTF8_CHARCLASS(swash_ptr, property_name, invlist) STMT_START { \ |
c7304fe2 KW |
164 | if (!swash_ptr) { \ |
165 | U8 flags = _CORE_SWASH_INIT_ACCEPT_INVLIST; \ | |
c7304fe2 | 166 | swash_ptr = _core_swash_init("utf8", property_name, &PL_sv_undef, \ |
2a16ac92 | 167 | 1, 0, invlist, &flags); \ |
c7304fe2 KW |
168 | assert(swash_ptr); \ |
169 | } \ | |
170 | } STMT_END | |
171 | ||
172 | /* If in debug mode, we test that a known character properly matches */ | |
173 | #ifdef DEBUGGING | |
174 | # define LOAD_UTF8_CHARCLASS_DEBUG_TEST(swash_ptr, \ | |
175 | property_name, \ | |
2a16ac92 | 176 | invlist, \ |
c7304fe2 | 177 | utf8_char_in_property) \ |
2a16ac92 | 178 | LOAD_UTF8_CHARCLASS(swash_ptr, property_name, invlist); \ |
c7304fe2 KW |
179 | assert(swash_fetch(swash_ptr, (U8 *) utf8_char_in_property, TRUE)); |
180 | #else | |
181 | # define LOAD_UTF8_CHARCLASS_DEBUG_TEST(swash_ptr, \ | |
182 | property_name, \ | |
2a16ac92 | 183 | invlist, \ |
c7304fe2 | 184 | utf8_char_in_property) \ |
2a16ac92 | 185 | LOAD_UTF8_CHARCLASS(swash_ptr, property_name, invlist) |
c7304fe2 | 186 | #endif |
d1eb3177 | 187 | |
c7304fe2 KW |
188 | #define LOAD_UTF8_CHARCLASS_ALNUM() LOAD_UTF8_CHARCLASS_DEBUG_TEST( \ |
189 | PL_utf8_swash_ptrs[_CC_WORDCHAR], \ | |
2a16ac92 KW |
190 | "", \ |
191 | PL_XPosix_ptrs[_CC_WORDCHAR], \ | |
df38da56 | 192 | LATIN_CAPITAL_LETTER_SHARP_S_UTF8); |
c7304fe2 KW |
193 | |
194 | #define LOAD_UTF8_CHARCLASS_GCB() /* Grapheme cluster boundaries */ \ | |
195 | STMT_START { \ | |
196 | LOAD_UTF8_CHARCLASS_DEBUG_TEST(PL_utf8_X_regular_begin, \ | |
197 | "_X_regular_begin", \ | |
2a16ac92 | 198 | NULL, \ |
df38da56 | 199 | LATIN_CAPITAL_LETTER_SHARP_S_UTF8); \ |
c7304fe2 KW |
200 | LOAD_UTF8_CHARCLASS_DEBUG_TEST(PL_utf8_X_extend, \ |
201 | "_X_extend", \ | |
2a16ac92 | 202 | NULL, \ |
c7304fe2 KW |
203 | COMBINING_GRAVE_ACCENT_UTF8); \ |
204 | } STMT_END | |
d1eb3177 | 205 | |
c7304fe2 | 206 | #define PLACEHOLDER /* Something for the preprocessor to grab onto */ |
3dab1dad YO |
207 | /* TODO: Combine JUMPABLE and HAS_TEXT to cache OP(rn) */ |
208 | ||
5f80c4cf | 209 | /* for use after a quantifier and before an EXACT-like node -- japhy */ |
c35dcbe2 YO |
210 | /* it would be nice to rework regcomp.sym to generate this stuff. sigh |
211 | * | |
212 | * NOTE that *nothing* that affects backtracking should be in here, specifically | |
213 | * VERBS must NOT be included. JUMPABLE is used to determine if we can ignore a | |
214 | * node that is in between two EXACT like nodes when ascertaining what the required | |
215 | * "follow" character is. This should probably be moved to regex compile time | |
216 | * although it may be done at run time beause of the REF possibility - more | |
217 | * investigation required. -- demerphq | |
218 | */ | |
baa60164 KW |
219 | #define JUMPABLE(rn) ( \ |
220 | OP(rn) == OPEN || \ | |
3e901dc0 | 221 | (OP(rn) == CLOSE && (!cur_eval || cur_eval->u.eval.close_paren != ARG(rn))) || \ |
baa60164 KW |
222 | OP(rn) == EVAL || \ |
223 | OP(rn) == SUSPEND || OP(rn) == IFMATCH || \ | |
224 | OP(rn) == PLUS || OP(rn) == MINMOD || \ | |
225 | OP(rn) == KEEPS || \ | |
226 | (PL_regkind[OP(rn)] == CURLY && ARG1(rn) > 0) \ | |
e2d8ce26 | 227 | ) |
ee9b8eae | 228 | #define IS_EXACT(rn) (PL_regkind[OP(rn)] == EXACT) |
e2d8ce26 | 229 | |
ee9b8eae YO |
230 | #define HAS_TEXT(rn) ( IS_EXACT(rn) || PL_regkind[OP(rn)] == REF ) |
231 | ||
232 | #if 0 | |
233 | /* Currently these are only used when PL_regkind[OP(rn)] == EXACT so | |
234 | we don't need this definition. */ | |
235 | #define IS_TEXT(rn) ( OP(rn)==EXACT || OP(rn)==REF || OP(rn)==NREF ) | |
098b07d5 | 236 | #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 |
237 | #define IS_TEXTFL(rn) ( OP(rn)==EXACTFL || OP(rn)==REFFL || OP(rn)==NREFFL ) |
238 | ||
239 | #else | |
240 | /* ... so we use this as its faster. */ | |
241 | #define IS_TEXT(rn) ( OP(rn)==EXACT ) | |
098b07d5 | 242 | #define IS_TEXTFU(rn) ( OP(rn)==EXACTFU || OP(rn)==EXACTFU_SS || OP(rn) == EXACTFA || OP(rn) == EXACTFA_NO_TRIE) |
ee9b8eae YO |
243 | #define IS_TEXTF(rn) ( OP(rn)==EXACTF ) |
244 | #define IS_TEXTFL(rn) ( OP(rn)==EXACTFL ) | |
245 | ||
246 | #endif | |
e2d8ce26 | 247 | |
a84d97b6 HS |
248 | /* |
249 | Search for mandatory following text node; for lookahead, the text must | |
250 | follow but for lookbehind (rn->flags != 0) we skip to the next step. | |
251 | */ | |
baa60164 | 252 | #define FIND_NEXT_IMPT(rn) STMT_START { \ |
3dab1dad YO |
253 | while (JUMPABLE(rn)) { \ |
254 | const OPCODE type = OP(rn); \ | |
255 | if (type == SUSPEND || PL_regkind[type] == CURLY) \ | |
e2d8ce26 | 256 | rn = NEXTOPER(NEXTOPER(rn)); \ |
3dab1dad | 257 | else if (type == PLUS) \ |
cca55fe3 | 258 | rn = NEXTOPER(rn); \ |
3dab1dad | 259 | else if (type == IFMATCH) \ |
a84d97b6 | 260 | rn = (rn->flags == 0) ? NEXTOPER(NEXTOPER(rn)) : rn + ARG(rn); \ |
e2d8ce26 | 261 | else rn += NEXT_OFF(rn); \ |
3dab1dad | 262 | } \ |
5f80c4cf | 263 | } STMT_END |
74750237 | 264 | |
22913b96 KW |
265 | /* These constants are for finding GCB=LV and GCB=LVT in the CLUMP regnode. |
266 | * These are for the pre-composed Hangul syllables, which are all in a | |
267 | * contiguous block and arranged there in such a way so as to facilitate | |
268 | * alorithmic determination of their characteristics. As such, they don't need | |
269 | * a swash, but can be determined by simple arithmetic. Almost all are | |
270 | * GCB=LVT, but every 28th one is a GCB=LV */ | |
271 | #define SBASE 0xAC00 /* Start of block */ | |
272 | #define SCount 11172 /* Length of block */ | |
273 | #define TCount 28 | |
c476f425 | 274 | |
006f26b2 DM |
275 | #define SLAB_FIRST(s) (&(s)->states[0]) |
276 | #define SLAB_LAST(s) (&(s)->states[PERL_REGMATCH_SLAB_SLOTS-1]) | |
277 | ||
a75351a1 | 278 | static void S_setup_eval_state(pTHX_ regmatch_info *const reginfo); |
bf2039a9 | 279 | static void S_cleanup_regmatch_info_aux(pTHX_ void *arg); |
bf2039a9 | 280 | static regmatch_state * S_push_slab(pTHX); |
51371543 | 281 | |
87c0511b | 282 | #define REGCP_PAREN_ELEMS 3 |
f067efbf | 283 | #define REGCP_OTHER_ELEMS 3 |
e0fa7e2b | 284 | #define REGCP_FRAME_ELEMS 1 |
620d5b66 NC |
285 | /* REGCP_FRAME_ELEMS are not part of the REGCP_OTHER_ELEMS and |
286 | * are needed for the regexp context stack bookkeeping. */ | |
287 | ||
76e3520e | 288 | STATIC CHECKPOINT |
92da3157 | 289 | S_regcppush(pTHX_ const regexp *rex, I32 parenfloor, U32 maxopenparen) |
a0d0e21e | 290 | { |
97aff369 | 291 | dVAR; |
a3b680e6 | 292 | const int retval = PL_savestack_ix; |
92da3157 DM |
293 | const int paren_elems_to_push = |
294 | (maxopenparen - parenfloor) * REGCP_PAREN_ELEMS; | |
e0fa7e2b NC |
295 | const UV total_elems = paren_elems_to_push + REGCP_OTHER_ELEMS; |
296 | const UV elems_shifted = total_elems << SAVE_TIGHT_SHIFT; | |
87c0511b | 297 | I32 p; |
40a82448 | 298 | GET_RE_DEBUG_FLAGS_DECL; |
a0d0e21e | 299 | |
b93070ed DM |
300 | PERL_ARGS_ASSERT_REGCPPUSH; |
301 | ||
e49a9654 | 302 | if (paren_elems_to_push < 0) |
d1b2014a YO |
303 | Perl_croak(aTHX_ "panic: paren_elems_to_push, %i < 0, maxopenparen: %i parenfloor: %i REGCP_PAREN_ELEMS: %i", |
304 | paren_elems_to_push, maxopenparen, parenfloor, REGCP_PAREN_ELEMS); | |
e49a9654 | 305 | |
e0fa7e2b NC |
306 | if ((elems_shifted >> SAVE_TIGHT_SHIFT) != total_elems) |
307 | Perl_croak(aTHX_ "panic: paren_elems_to_push offset %"UVuf | |
5df417d0 | 308 | " out of range (%lu-%ld)", |
92da3157 DM |
309 | total_elems, |
310 | (unsigned long)maxopenparen, | |
311 | (long)parenfloor); | |
e0fa7e2b | 312 | |
620d5b66 | 313 | SSGROW(total_elems + REGCP_FRAME_ELEMS); |
7f69552c | 314 | |
495f47a5 | 315 | DEBUG_BUFFERS_r( |
92da3157 | 316 | if ((int)maxopenparen > (int)parenfloor) |
495f47a5 DM |
317 | PerlIO_printf(Perl_debug_log, |
318 | "rex=0x%"UVxf" offs=0x%"UVxf": saving capture indices:\n", | |
319 | PTR2UV(rex), | |
320 | PTR2UV(rex->offs) | |
321 | ); | |
322 | ); | |
92da3157 | 323 | for (p = parenfloor+1; p <= (I32)maxopenparen; p++) { |
b1ce53c5 | 324 | /* REGCP_PARENS_ELEMS are pushed per pairs of parentheses. */ |
99a90e59 FC |
325 | SSPUSHIV(rex->offs[p].end); |
326 | SSPUSHIV(rex->offs[p].start); | |
1ca2007e | 327 | SSPUSHINT(rex->offs[p].start_tmp); |
e7707071 | 328 | DEBUG_BUFFERS_r(PerlIO_printf(Perl_debug_log, |
495f47a5 DM |
329 | " \\%"UVuf": %"IVdf"(%"IVdf")..%"IVdf"\n", |
330 | (UV)p, | |
331 | (IV)rex->offs[p].start, | |
332 | (IV)rex->offs[p].start_tmp, | |
333 | (IV)rex->offs[p].end | |
40a82448 | 334 | )); |
a0d0e21e | 335 | } |
b1ce53c5 | 336 | /* REGCP_OTHER_ELEMS are pushed in any case, parentheses or no. */ |
92da3157 | 337 | SSPUSHINT(maxopenparen); |
b93070ed DM |
338 | SSPUSHINT(rex->lastparen); |
339 | SSPUSHINT(rex->lastcloseparen); | |
e0fa7e2b | 340 | SSPUSHUV(SAVEt_REGCONTEXT | elems_shifted); /* Magic cookie. */ |
41123dfd | 341 | |
a0d0e21e LW |
342 | return retval; |
343 | } | |
344 | ||
c277df42 | 345 | /* These are needed since we do not localize EVAL nodes: */ |
ab3bbdeb YO |
346 | #define REGCP_SET(cp) \ |
347 | DEBUG_STATE_r( \ | |
ab3bbdeb | 348 | PerlIO_printf(Perl_debug_log, \ |
e4f74956 | 349 | " Setting an EVAL scope, savestack=%"IVdf"\n", \ |
ab3bbdeb YO |
350 | (IV)PL_savestack_ix)); \ |
351 | cp = PL_savestack_ix | |
c3464db5 | 352 | |
ab3bbdeb | 353 | #define REGCP_UNWIND(cp) \ |
e4f74956 | 354 | DEBUG_STATE_r( \ |
ab3bbdeb | 355 | if (cp != PL_savestack_ix) \ |
e4f74956 YO |
356 | PerlIO_printf(Perl_debug_log, \ |
357 | " Clearing an EVAL scope, savestack=%"IVdf"..%"IVdf"\n", \ | |
ab3bbdeb YO |
358 | (IV)(cp), (IV)PL_savestack_ix)); \ |
359 | regcpblow(cp) | |
c277df42 | 360 | |
a8d1f4b4 DM |
361 | #define UNWIND_PAREN(lp, lcp) \ |
362 | for (n = rex->lastparen; n > lp; n--) \ | |
363 | rex->offs[n].end = -1; \ | |
364 | rex->lastparen = n; \ | |
365 | rex->lastcloseparen = lcp; | |
366 | ||
367 | ||
f067efbf | 368 | STATIC void |
92da3157 | 369 | S_regcppop(pTHX_ regexp *rex, U32 *maxopenparen_p) |
a0d0e21e | 370 | { |
97aff369 | 371 | dVAR; |
e0fa7e2b | 372 | UV i; |
87c0511b | 373 | U32 paren; |
a3621e74 YO |
374 | GET_RE_DEBUG_FLAGS_DECL; |
375 | ||
7918f24d NC |
376 | PERL_ARGS_ASSERT_REGCPPOP; |
377 | ||
b1ce53c5 | 378 | /* Pop REGCP_OTHER_ELEMS before the parentheses loop starts. */ |
c6bf6a65 | 379 | i = SSPOPUV; |
e0fa7e2b NC |
380 | assert((i & SAVE_MASK) == SAVEt_REGCONTEXT); /* Check that the magic cookie is there. */ |
381 | i >>= SAVE_TIGHT_SHIFT; /* Parentheses elements to pop. */ | |
b93070ed DM |
382 | rex->lastcloseparen = SSPOPINT; |
383 | rex->lastparen = SSPOPINT; | |
92da3157 | 384 | *maxopenparen_p = SSPOPINT; |
b1ce53c5 | 385 | |
620d5b66 | 386 | i -= REGCP_OTHER_ELEMS; |
b1ce53c5 | 387 | /* Now restore the parentheses context. */ |
495f47a5 DM |
388 | DEBUG_BUFFERS_r( |
389 | if (i || rex->lastparen + 1 <= rex->nparens) | |
390 | PerlIO_printf(Perl_debug_log, | |
391 | "rex=0x%"UVxf" offs=0x%"UVxf": restoring capture indices to:\n", | |
392 | PTR2UV(rex), | |
393 | PTR2UV(rex->offs) | |
394 | ); | |
395 | ); | |
92da3157 | 396 | paren = *maxopenparen_p; |
620d5b66 | 397 | for ( ; i > 0; i -= REGCP_PAREN_ELEMS) { |
ea3daa5d | 398 | SSize_t tmps; |
1ca2007e | 399 | rex->offs[paren].start_tmp = SSPOPINT; |
99a90e59 FC |
400 | rex->offs[paren].start = SSPOPIV; |
401 | tmps = SSPOPIV; | |
b93070ed DM |
402 | if (paren <= rex->lastparen) |
403 | rex->offs[paren].end = tmps; | |
495f47a5 DM |
404 | DEBUG_BUFFERS_r( PerlIO_printf(Perl_debug_log, |
405 | " \\%"UVuf": %"IVdf"(%"IVdf")..%"IVdf"%s\n", | |
406 | (UV)paren, | |
407 | (IV)rex->offs[paren].start, | |
408 | (IV)rex->offs[paren].start_tmp, | |
409 | (IV)rex->offs[paren].end, | |
410 | (paren > rex->lastparen ? "(skipped)" : "")); | |
c277df42 | 411 | ); |
87c0511b | 412 | paren--; |
a0d0e21e | 413 | } |
daf18116 | 414 | #if 1 |
dafc8851 JH |
415 | /* It would seem that the similar code in regtry() |
416 | * already takes care of this, and in fact it is in | |
417 | * a better location to since this code can #if 0-ed out | |
418 | * but the code in regtry() is needed or otherwise tests | |
419 | * requiring null fields (pat.t#187 and split.t#{13,14} | |
daf18116 JH |
420 | * (as of patchlevel 7877) will fail. Then again, |
421 | * this code seems to be necessary or otherwise | |
225593e1 DM |
422 | * this erroneously leaves $1 defined: "1" =~ /^(?:(\d)x)?\d$/ |
423 | * --jhi updated by dapm */ | |
b93070ed | 424 | for (i = rex->lastparen + 1; i <= rex->nparens; i++) { |
92da3157 | 425 | if (i > *maxopenparen_p) |
b93070ed DM |
426 | rex->offs[i].start = -1; |
427 | rex->offs[i].end = -1; | |
495f47a5 DM |
428 | DEBUG_BUFFERS_r( PerlIO_printf(Perl_debug_log, |
429 | " \\%"UVuf": %s ..-1 undeffing\n", | |
430 | (UV)i, | |
92da3157 | 431 | (i > *maxopenparen_p) ? "-1" : " " |
495f47a5 | 432 | )); |
a0d0e21e | 433 | } |
dafc8851 | 434 | #endif |
a0d0e21e LW |
435 | } |
436 | ||
74088413 DM |
437 | /* restore the parens and associated vars at savestack position ix, |
438 | * but without popping the stack */ | |
439 | ||
440 | STATIC void | |
92da3157 | 441 | S_regcp_restore(pTHX_ regexp *rex, I32 ix, U32 *maxopenparen_p) |
74088413 DM |
442 | { |
443 | I32 tmpix = PL_savestack_ix; | |
444 | PL_savestack_ix = ix; | |
92da3157 | 445 | regcppop(rex, maxopenparen_p); |
74088413 DM |
446 | PL_savestack_ix = tmpix; |
447 | } | |
448 | ||
02db2b7b | 449 | #define regcpblow(cp) LEAVE_SCOPE(cp) /* Ignores regcppush()ed data. */ |
a0d0e21e | 450 | |
31c7f561 KW |
451 | STATIC bool |
452 | S_isFOO_lc(pTHX_ const U8 classnum, const U8 character) | |
453 | { | |
454 | /* Returns a boolean as to whether or not 'character' is a member of the | |
455 | * Posix character class given by 'classnum' that should be equivalent to a | |
456 | * value in the typedef '_char_class_number'. | |
457 | * | |
458 | * Ideally this could be replaced by a just an array of function pointers | |
459 | * to the C library functions that implement the macros this calls. | |
460 | * However, to compile, the precise function signatures are required, and | |
461 | * these may vary from platform to to platform. To avoid having to figure | |
462 | * out what those all are on each platform, I (khw) am using this method, | |
7aee35ff KW |
463 | * which adds an extra layer of function call overhead (unless the C |
464 | * optimizer strips it away). But we don't particularly care about | |
465 | * performance with locales anyway. */ | |
31c7f561 KW |
466 | |
467 | switch ((_char_class_number) classnum) { | |
15861f94 | 468 | case _CC_ENUM_ALPHANUMERIC: return isALPHANUMERIC_LC(character); |
31c7f561 | 469 | case _CC_ENUM_ALPHA: return isALPHA_LC(character); |
e8d596e0 KW |
470 | case _CC_ENUM_ASCII: return isASCII_LC(character); |
471 | case _CC_ENUM_BLANK: return isBLANK_LC(character); | |
b0d691b2 KW |
472 | case _CC_ENUM_CASED: return isLOWER_LC(character) |
473 | || isUPPER_LC(character); | |
e8d596e0 | 474 | case _CC_ENUM_CNTRL: return isCNTRL_LC(character); |
31c7f561 KW |
475 | case _CC_ENUM_DIGIT: return isDIGIT_LC(character); |
476 | case _CC_ENUM_GRAPH: return isGRAPH_LC(character); | |
477 | case _CC_ENUM_LOWER: return isLOWER_LC(character); | |
478 | case _CC_ENUM_PRINT: return isPRINT_LC(character); | |
e8d596e0 | 479 | case _CC_ENUM_PSXSPC: return isPSXSPC_LC(character); |
31c7f561 | 480 | case _CC_ENUM_PUNCT: return isPUNCT_LC(character); |
e8d596e0 | 481 | case _CC_ENUM_SPACE: return isSPACE_LC(character); |
31c7f561 KW |
482 | case _CC_ENUM_UPPER: return isUPPER_LC(character); |
483 | case _CC_ENUM_WORDCHAR: return isWORDCHAR_LC(character); | |
31c7f561 | 484 | case _CC_ENUM_XDIGIT: return isXDIGIT_LC(character); |
31c7f561 KW |
485 | default: /* VERTSPACE should never occur in locales */ |
486 | Perl_croak(aTHX_ "panic: isFOO_lc() has an unexpected character class '%d'", classnum); | |
487 | } | |
488 | ||
489 | assert(0); /* NOTREACHED */ | |
490 | return FALSE; | |
491 | } | |
492 | ||
3018b823 KW |
493 | STATIC bool |
494 | S_isFOO_utf8_lc(pTHX_ const U8 classnum, const U8* character) | |
495 | { | |
496 | /* Returns a boolean as to whether or not the (well-formed) UTF-8-encoded | |
497 | * 'character' is a member of the Posix character class given by 'classnum' | |
498 | * that should be equivalent to a value in the typedef | |
499 | * '_char_class_number'. | |
500 | * | |
501 | * This just calls isFOO_lc on the code point for the character if it is in | |
502 | * the range 0-255. Outside that range, all characters avoid Unicode | |
503 | * rules, ignoring any locale. So use the Unicode function if this class | |
504 | * requires a swash, and use the Unicode macro otherwise. */ | |
505 | ||
506 | PERL_ARGS_ASSERT_ISFOO_UTF8_LC; | |
507 | ||
508 | if (UTF8_IS_INVARIANT(*character)) { | |
509 | return isFOO_lc(classnum, *character); | |
510 | } | |
511 | else if (UTF8_IS_DOWNGRADEABLE_START(*character)) { | |
512 | return isFOO_lc(classnum, | |
94bb8c36 | 513 | TWO_BYTE_UTF8_TO_NATIVE(*character, *(character + 1))); |
3018b823 KW |
514 | } |
515 | ||
516 | if (classnum < _FIRST_NON_SWASH_CC) { | |
517 | ||
518 | /* Initialize the swash unless done already */ | |
519 | if (! PL_utf8_swash_ptrs[classnum]) { | |
520 | U8 flags = _CORE_SWASH_INIT_ACCEPT_INVLIST; | |
2a16ac92 KW |
521 | PL_utf8_swash_ptrs[classnum] = |
522 | _core_swash_init("utf8", | |
523 | "", | |
524 | &PL_sv_undef, 1, 0, | |
525 | PL_XPosix_ptrs[classnum], &flags); | |
3018b823 KW |
526 | } |
527 | ||
92a2046b KW |
528 | return cBOOL(swash_fetch(PL_utf8_swash_ptrs[classnum], (U8 *) |
529 | character, | |
530 | TRUE /* is UTF */ )); | |
3018b823 KW |
531 | } |
532 | ||
533 | switch ((_char_class_number) classnum) { | |
534 | case _CC_ENUM_SPACE: | |
535 | case _CC_ENUM_PSXSPC: return is_XPERLSPACE_high(character); | |
536 | ||
537 | case _CC_ENUM_BLANK: return is_HORIZWS_high(character); | |
538 | case _CC_ENUM_XDIGIT: return is_XDIGIT_high(character); | |
539 | case _CC_ENUM_VERTSPACE: return is_VERTWS_high(character); | |
540 | default: return 0; /* Things like CNTRL are always | |
541 | below 256 */ | |
542 | } | |
543 | ||
544 | assert(0); /* NOTREACHED */ | |
545 | return FALSE; | |
546 | } | |
547 | ||
a687059c | 548 | /* |
e50aee73 | 549 | * pregexec and friends |
a687059c LW |
550 | */ |
551 | ||
76234dfb | 552 | #ifndef PERL_IN_XSUB_RE |
a687059c | 553 | /* |
c277df42 | 554 | - pregexec - match a regexp against a string |
a687059c | 555 | */ |
c277df42 | 556 | I32 |
5aaab254 | 557 | Perl_pregexec(pTHX_ REGEXP * const prog, char* stringarg, char *strend, |
ea3daa5d | 558 | char *strbeg, SSize_t minend, SV *screamer, U32 nosave) |
8fd1a950 DM |
559 | /* stringarg: the point in the string at which to begin matching */ |
560 | /* strend: pointer to null at end of string */ | |
561 | /* strbeg: real beginning of string */ | |
562 | /* minend: end of match must be >= minend bytes after stringarg. */ | |
563 | /* screamer: SV being matched: only used for utf8 flag, pos() etc; string | |
564 | * itself is accessed via the pointers above */ | |
565 | /* nosave: For optimizations. */ | |
c277df42 | 566 | { |
7918f24d NC |
567 | PERL_ARGS_ASSERT_PREGEXEC; |
568 | ||
c277df42 | 569 | return |
9041c2e3 | 570 | regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL, |
c277df42 IZ |
571 | nosave ? 0 : REXEC_COPY_STR); |
572 | } | |
76234dfb | 573 | #endif |
22e551b9 | 574 | |
cad2e5aa | 575 | |
6eb5f6b9 | 576 | |
1a4edc3c DM |
577 | /* re_intuit_start(): |
578 | * | |
579 | * Based on some optimiser hints, try to find the earliest position in the | |
580 | * string where the regex could match. | |
581 | * | |
582 | * rx: the regex to match against | |
583 | * sv: the SV being matched: only used for utf8 flag; the string | |
584 | * itself is accessed via the pointers below. Note that on | |
585 | * something like an overloaded SV, SvPOK(sv) may be false | |
586 | * and the string pointers may point to something unrelated to | |
587 | * the SV itself. | |
588 | * strbeg: real beginning of string | |
589 | * strpos: the point in the string at which to begin matching | |
590 | * strend: pointer to the byte following the last char of the string | |
591 | * flags currently unused; set to 0 | |
592 | * data: currently unused; set to NULL | |
593 | * | |
594 | * The basic idea of re_intuit_start() is to use some known information | |
595 | * about the pattern, namely: | |
596 | * | |
597 | * a) the longest known anchored substring (i.e. one that's at a | |
598 | * constant offset from the beginning of the pattern; but not | |
599 | * necessarily at a fixed offset from the beginning of the | |
600 | * string); | |
601 | * b) the longest floating substring (i.e. one that's not at a constant | |
602 | * offset from the beginning of the pattern); | |
603 | * c) Whether the pattern is anchored to the string; either | |
604 | * an absolute anchor: /^../, or anchored to \n: /^.../m, | |
605 | * or anchored to pos(): /\G/; | |
606 | * d) A start class: a real or synthetic character class which | |
607 | * represents which characters are legal at the start of the pattern; | |
608 | * | |
609 | * to either quickly reject the match, or to find the earliest position | |
610 | * within the string at which the pattern might match, thus avoiding | |
611 | * running the full NFA engine at those earlier locations, only to | |
612 | * eventually fail and retry further along. | |
613 | * | |
614 | * Returns NULL if the pattern can't match, or returns the address within | |
615 | * the string which is the earliest place the match could occur. | |
616 | * | |
617 | * The longest of the anchored and floating substrings is called 'check' | |
618 | * and is checked first. The other is called 'other' and is checked | |
619 | * second. The 'other' substring may not be present. For example, | |
620 | * | |
621 | * /(abc|xyz)ABC\d{0,3}DEFG/ | |
622 | * | |
623 | * will have | |
624 | * | |
625 | * check substr (float) = "DEFG", offset 6..9 chars | |
626 | * other substr (anchored) = "ABC", offset 3..3 chars | |
627 | * stclass = [ax] | |
628 | * | |
629 | * Be aware that during the course of this function, sometimes 'anchored' | |
630 | * refers to a substring being anchored relative to the start of the | |
631 | * pattern, and sometimes to the pattern itself being anchored relative to | |
632 | * the string. For example: | |
633 | * | |
634 | * /\dabc/: "abc" is anchored to the pattern; | |
635 | * /^\dabc/: "abc" is anchored to the pattern and the string; | |
636 | * /\d+abc/: "abc" is anchored to neither the pattern nor the string; | |
637 | * /^\d+abc/: "abc" is anchored to neither the pattern nor the string, | |
638 | * but the pattern is anchored to the string. | |
52a21eb3 DM |
639 | */ |
640 | ||
cad2e5aa | 641 | char * |
52a21eb3 DM |
642 | Perl_re_intuit_start(pTHX_ |
643 | REGEXP * const rx, | |
644 | SV *sv, | |
645 | const char * const strbeg, | |
646 | char *strpos, | |
647 | char *strend, | |
648 | const U32 flags, | |
649 | re_scream_pos_data *data) | |
cad2e5aa | 650 | { |
97aff369 | 651 | dVAR; |
8d919b0a | 652 | struct regexp *const prog = ReANY(rx); |
6b071d16 | 653 | SSize_t start_shift = prog->check_offset_min; |
cad2e5aa | 654 | /* Should be nonnegative! */ |
ea3daa5d | 655 | SSize_t end_shift = 0; |
0fc004dd DM |
656 | /* current lowest pos in string where the regex can start matching */ |
657 | char *rx_origin = strpos; | |
eb578fdb | 658 | SV *check; |
f2ed9b32 | 659 | const bool utf8_target = (sv && SvUTF8(sv)) ? 1 : 0; /* if no sv we have to assume bytes */ |
6480a6c4 | 660 | U8 other_ix = 1 - prog->substrs->check_ix; |
6ad5ffb3 | 661 | bool ml_anch = 0; |
8f4bf5fc | 662 | char *other_last = strpos;/* latest pos 'other' substr already checked to */ |
bd61b366 | 663 | char *check_at = NULL; /* check substr found at this pos */ |
bbe252da | 664 | const I32 multiline = prog->extflags & RXf_PMf_MULTILINE; |
f8fc2ecf | 665 | RXi_GET_DECL(prog,progi); |
02d5137b DM |
666 | regmatch_info reginfo_buf; /* create some info to pass to find_byclass */ |
667 | regmatch_info *const reginfo = ®info_buf; | |
a3621e74 YO |
668 | GET_RE_DEBUG_FLAGS_DECL; |
669 | ||
7918f24d | 670 | PERL_ARGS_ASSERT_RE_INTUIT_START; |
c33e64f0 FC |
671 | PERL_UNUSED_ARG(flags); |
672 | PERL_UNUSED_ARG(data); | |
7918f24d | 673 | |
1dc475d0 DM |
674 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, |
675 | "Intuit: trying to determine minimum start position...\n")); | |
676 | ||
fb9bbddb DM |
677 | /* for now, assume that all substr offsets are positive. If at some point |
678 | * in the future someone wants to do clever things with look-behind and | |
679 | * -ve offsets, they'll need to fix up any code in this function | |
680 | * which uses these offsets. See the thread beginning | |
681 | * <20140113145929.GF27210@iabyn.com> | |
682 | */ | |
683 | assert(prog->substrs->data[0].min_offset >= 0); | |
684 | assert(prog->substrs->data[0].max_offset >= 0); | |
685 | assert(prog->substrs->data[1].min_offset >= 0); | |
686 | assert(prog->substrs->data[1].max_offset >= 0); | |
687 | assert(prog->substrs->data[2].min_offset >= 0); | |
688 | assert(prog->substrs->data[2].max_offset >= 0); | |
689 | ||
f7022b5a | 690 | /* for now, assume that if both present, that the floating substring |
83f2232d | 691 | * doesn't start before the anchored substring. |
f7022b5a DM |
692 | * If you break this assumption (e.g. doing better optimisations |
693 | * with lookahead/behind), then you'll need to audit the code in this | |
694 | * function carefully first | |
695 | */ | |
696 | assert( | |
697 | ! ( (prog->anchored_utf8 || prog->anchored_substr) | |
698 | && (prog->float_utf8 || prog->float_substr)) | |
699 | || (prog->float_min_offset >= prog->anchored_offset)); | |
700 | ||
1a4edc3c DM |
701 | /* byte rather than char calculation for efficiency. It fails |
702 | * to quickly reject some cases that can't match, but will reject | |
703 | * them later after doing full char arithmetic */ | |
c344f387 | 704 | if (prog->minlen > strend - strpos) { |
a3621e74 | 705 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, |
1dc475d0 | 706 | " String too short...\n")); |
cad2e5aa | 707 | goto fail; |
2c2d71f5 | 708 | } |
d8da0584 | 709 | |
6c3fea77 | 710 | reginfo->is_utf8_target = cBOOL(utf8_target); |
bf2039a9 | 711 | reginfo->info_aux = NULL; |
9d9163fb | 712 | reginfo->strbeg = strbeg; |
220db18a | 713 | reginfo->strend = strend; |
aed7b151 | 714 | reginfo->is_utf8_pat = cBOOL(RX_UTF8(rx)); |
02d5137b | 715 | reginfo->intuit = 1; |
1cb48e53 DM |
716 | /* not actually used within intuit, but zero for safety anyway */ |
717 | reginfo->poscache_maxiter = 0; | |
02d5137b | 718 | |
f2ed9b32 | 719 | if (utf8_target) { |
33b8afdf JH |
720 | if (!prog->check_utf8 && prog->check_substr) |
721 | to_utf8_substr(prog); | |
722 | check = prog->check_utf8; | |
723 | } else { | |
7e0d5ad7 KW |
724 | if (!prog->check_substr && prog->check_utf8) { |
725 | if (! to_byte_substr(prog)) { | |
6b54ddc5 | 726 | NON_UTF8_TARGET_BUT_UTF8_REQUIRED(fail); |
7e0d5ad7 KW |
727 | } |
728 | } | |
33b8afdf JH |
729 | check = prog->check_substr; |
730 | } | |
274cd312 | 731 | |
1dc475d0 DM |
732 | /* dump the various substring data */ |
733 | DEBUG_OPTIMISE_MORE_r({ | |
734 | int i; | |
735 | for (i=0; i<=2; i++) { | |
736 | SV *sv = (utf8_target ? prog->substrs->data[i].utf8_substr | |
737 | : prog->substrs->data[i].substr); | |
738 | if (!sv) | |
739 | continue; | |
740 | ||
741 | PerlIO_printf(Perl_debug_log, | |
742 | " substrs[%d]: min=%"IVdf" max=%"IVdf" end shift=%"IVdf | |
743 | " useful=%"IVdf" utf8=%d [%s]\n", | |
744 | i, | |
745 | (IV)prog->substrs->data[i].min_offset, | |
746 | (IV)prog->substrs->data[i].max_offset, | |
747 | (IV)prog->substrs->data[i].end_shift, | |
748 | BmUSEFUL(sv), | |
749 | utf8_target ? 1 : 0, | |
750 | SvPEEK(sv)); | |
751 | } | |
752 | }); | |
753 | ||
8e1490ee | 754 | if (prog->intflags & PREGf_ANCH) { /* Match at \G, beg-of-str or after \n */ |
9fc7410e DM |
755 | |
756 | /* ml_anch: check after \n? | |
757 | * | |
758 | * A note about IMPLICIT: on an un-anchored pattern beginning | |
759 | * with /.*.../, these flags will have been added by the | |
760 | * compiler: | |
761 | * /.*abc/, /.*abc/m: PREGf_IMPLICIT | PREGf_ANCH_MBOL | |
762 | * /.*abc/s: PREGf_IMPLICIT | PREGf_ANCH_SBOL | |
763 | */ | |
7d2d37f5 DM |
764 | ml_anch = (prog->intflags & PREGf_ANCH_MBOL) |
765 | && !(prog->intflags & PREGf_IMPLICIT); | |
cad2e5aa | 766 | |
343c8a29 | 767 | if (!ml_anch && !(prog->intflags & PREGf_IMPLICIT)) { |
c889ccc8 DM |
768 | /* we are only allowed to match at BOS or \G */ |
769 | ||
57fcbfa7 | 770 | /* trivially reject if there's a BOS anchor and we're not at BOS. |
7bb3b9eb DM |
771 | * |
772 | * Note that we don't try to do a similar quick reject for | |
773 | * \G, since generally the caller will have calculated strpos | |
774 | * based on pos() and gofs, so the string is already correctly | |
775 | * anchored by definition; and handling the exceptions would | |
776 | * be too fiddly (e.g. REXEC_IGNOREPOS). | |
57fcbfa7 | 777 | */ |
7bb3b9eb | 778 | if ( strpos != strbeg |
343c8a29 | 779 | && (prog->intflags & (PREGf_ANCH_BOL|PREGf_ANCH_SBOL))) |
c889ccc8 DM |
780 | { |
781 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, | |
1dc475d0 | 782 | " Not at start...\n")); |
c889ccc8 DM |
783 | goto fail; |
784 | } | |
785 | ||
a5d12a4b DM |
786 | /* in the presence of an anchor, the anchored (relative to the |
787 | * start of the regex) substr must also be anchored relative | |
66b7ec5c DM |
788 | * to strpos. So quickly reject if substr isn't found there. |
789 | * This works for \G too, because the caller will already have | |
790 | * subtracted gofs from pos, and gofs is the offset from the | |
791 | * \G to the start of the regex. For example, in /.abc\Gdef/, | |
792 | * where substr="abcdef", pos()=3, gofs=4, offset_min=1: | |
793 | * caller will have set strpos=pos()-4; we look for the substr | |
794 | * at position pos()-4+1, which lines up with the "a" */ | |
a5d12a4b | 795 | |
c889ccc8 | 796 | if (prog->check_offset_min == prog->check_offset_max |
d0d44648 | 797 | && !(prog->intflags & PREGf_CANY_SEEN)) |
c889ccc8 DM |
798 | { |
799 | /* Substring at constant offset from beg-of-str... */ | |
d307bf57 | 800 | SSize_t slen = SvCUR(check); |
343c8a29 | 801 | char *s = HOP3c(strpos, prog->check_offset_min, strend); |
1de06328 | 802 | |
1dc475d0 DM |
803 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, |
804 | " Looking for check substr at fixed offset %"IVdf"...\n", | |
805 | (IV)prog->check_offset_min)); | |
806 | ||
7742aa66 DM |
807 | if (SvTAIL(check)) { |
808 | /* In this case, the regex is anchored at the end too. | |
809 | * Unless it's a multiline match, the lengths must match | |
810 | * exactly, give or take a \n. NB: slen >= 1 since | |
811 | * the last char of check is \n */ | |
812 | if (!multiline | |
813 | && ( strend - s > slen | |
814 | || strend - s < slen - 1 | |
815 | || (strend - s == slen && strend[-1] != '\n'))) | |
c889ccc8 DM |
816 | { |
817 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, | |
1dc475d0 | 818 | " String too long...\n")); |
c889ccc8 DM |
819 | goto fail_finish; |
820 | } | |
821 | /* Now should match s[0..slen-2] */ | |
822 | slen--; | |
c889ccc8 | 823 | } |
d307bf57 DM |
824 | if (slen && (*SvPVX_const(check) != *s |
825 | || (slen > 1 && memNE(SvPVX_const(check), s, slen)))) | |
826 | { | |
827 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, | |
1dc475d0 | 828 | " String not equal...\n")); |
d307bf57 DM |
829 | goto fail_finish; |
830 | } | |
c889ccc8 DM |
831 | |
832 | check_at = s; | |
833 | goto success_at_start; | |
cad2e5aa | 834 | } |
cad2e5aa | 835 | } |
cad2e5aa | 836 | } |
0fc004dd | 837 | |
c0e0ec46 | 838 | end_shift = prog->check_end_shift; |
cad2e5aa | 839 | |
19188028 | 840 | #ifdef DEBUGGING /* 7/99: reports of failure (with the older version) */ |
0033605d | 841 | if (end_shift < 0) |
1de06328 | 842 | Perl_croak(aTHX_ "panic: end_shift: %"IVdf" pattern:\n%s\n ", |
220fc49f | 843 | (IV)end_shift, RX_PRECOMP(prog)); |
2c2d71f5 JH |
844 | #endif |
845 | ||
2c2d71f5 | 846 | restart: |
1de06328 | 847 | |
66b7ec5c DM |
848 | /* This is the (re)entry point of the main loop in this function. |
849 | * The goal of this loop is to: | |
850 | * 1) find the "check" substring in the region rx_origin..strend | |
851 | * (adjusted by start_shift / end_shift). If not found, reject | |
852 | * immediately. | |
853 | * 2) If it exists, look for the "other" substr too if defined; for | |
854 | * example, if the check substr maps to the anchored substr, then | |
855 | * check the floating substr, and vice-versa. If not found, go | |
856 | * back to (1) with rx_origin suitably incremented. | |
857 | * 3) If we find an rx_origin position that doesn't contradict | |
858 | * either of the substrings, then check the possible additional | |
859 | * constraints on rx_origin of /^.../m or a known start class. | |
860 | * If these fail, then depending on which constraints fail, jump | |
861 | * back to here, or to various other re-entry points further along | |
862 | * that skip some of the first steps. | |
863 | * 4) If we pass all those tests, update the BmUSEFUL() count on the | |
864 | * substring. If the start position was determined to be at the | |
865 | * beginning of the string - so, not rejected, but not optimised, | |
866 | * since we have to run regmatch from position 0 - decrement the | |
867 | * BmUSEFUL() count. Otherwise increment it. | |
868 | */ | |
869 | ||
1a4edc3c DM |
870 | |
871 | /* first, look for the 'check' substring */ | |
872 | ||
1de06328 | 873 | { |
c33e64f0 FC |
874 | U8* start_point; |
875 | U8* end_point; | |
c889ccc8 | 876 | |
c889ccc8 | 877 | DEBUG_OPTIMISE_MORE_r({ |
1dc475d0 | 878 | PerlIO_printf(Perl_debug_log, |
ae5d4331 | 879 | " At restart: rx_origin=%"IVdf" Check offset min: %"IVdf |
1dc475d0 | 880 | " Start shift: %"IVdf" End shift %"IVdf |
4d281893 | 881 | " Real end Shift: %"IVdf"\n", |
197641b6 | 882 | (IV)(rx_origin - strpos), |
c889ccc8 | 883 | (IV)prog->check_offset_min, |
12fbc530 DM |
884 | (IV)start_shift, |
885 | (IV)end_shift, | |
c889ccc8 DM |
886 | (IV)prog->check_end_shift); |
887 | }); | |
1de06328 | 888 | |
0d331aaf | 889 | if (prog->intflags & PREGf_CANY_SEEN) { |
0fc004dd | 890 | start_point= (U8*)(rx_origin + start_shift); |
12fbc530 | 891 | end_point= (U8*)(strend - end_shift); |
557f47af DM |
892 | if (start_point > end_point) |
893 | goto fail_finish; | |
1de06328 | 894 | } else { |
557f47af DM |
895 | end_point = HOP3(strend, -end_shift, strbeg); |
896 | start_point = HOPMAYBE3(rx_origin, start_shift, end_point); | |
897 | if (!start_point) | |
898 | goto fail_finish; | |
1de06328 | 899 | } |
c889ccc8 | 900 | |
557f47af | 901 | |
e0362b86 DM |
902 | /* If the regex is absolutely anchored to either the start of the |
903 | * string (BOL,SBOL) or to pos() (ANCH_GPOS), then | |
904 | * check_offset_max represents an upper bound on the string where | |
905 | * the substr could start. For the ANCH_GPOS case, we assume that | |
906 | * the caller of intuit will have already set strpos to | |
907 | * pos()-gofs, so in this case strpos + offset_max will still be | |
908 | * an upper bound on the substr. | |
909 | */ | |
c19c836a DM |
910 | if (!ml_anch |
911 | && prog->intflags & PREGf_ANCH | |
e0362b86 | 912 | && prog->check_offset_max != SSize_t_MAX) |
c19c836a | 913 | { |
1a08ba3a | 914 | SSize_t len = SvCUR(check) - !!SvTAIL(check); |
e0362b86 DM |
915 | const char * const anchor = |
916 | (prog->intflags & PREGf_ANCH_GPOS ? strpos : strbeg); | |
917 | ||
918 | /* do a bytes rather than chars comparison. It's conservative; | |
919 | * so it skips doing the HOP if the result can't possibly end | |
920 | * up earlier than the old value of end_point. | |
921 | */ | |
922 | if ((char*)end_point - anchor > prog->check_offset_max) { | |
923 | end_point = HOP3lim((U8*)anchor, | |
924 | prog->check_offset_max, | |
925 | end_point -len) | |
926 | + len; | |
927 | } | |
d6ef1678 DM |
928 | } |
929 | ||
6bda09f9 | 930 | DEBUG_OPTIMISE_MORE_r({ |
1dc475d0 | 931 | PerlIO_printf(Perl_debug_log, " fbm_instr len=%d str=<%.*s>\n", |
1de06328 | 932 | (int)(end_point - start_point), |
fc8cd66c | 933 | (int)(end_point - start_point) > 20 ? 20 : (int)(end_point - start_point), |
1de06328 YO |
934 | start_point); |
935 | }); | |
936 | ||
ae5d4331 | 937 | check_at = fbm_instr( start_point, end_point, |
7fba1cd6 | 938 | check, multiline ? FBMrf_MULTILINE : 0); |
c889ccc8 | 939 | |
8fd34720 DM |
940 | /* Update the count-of-usability, remove useless subpatterns, |
941 | unshift s. */ | |
942 | ||
943 | DEBUG_EXECUTE_r({ | |
944 | RE_PV_QUOTED_DECL(quoted, utf8_target, PERL_DEBUG_PAD_ZERO(0), | |
945 | SvPVX_const(check), RE_SV_DUMPLEN(check), 30); | |
946 | PerlIO_printf(Perl_debug_log, " %s %s substr %s%s%s", | |
947 | (check_at ? "Found" : "Did not find"), | |
948 | (check == (utf8_target ? prog->anchored_utf8 : prog->anchored_substr) | |
949 | ? "anchored" : "floating"), | |
950 | quoted, | |
951 | RE_SV_TAIL(check), | |
952 | (check_at ? " at offset " : "...\n") ); | |
953 | }); | |
2c2d71f5 | 954 | |
8fd34720 DM |
955 | if (!check_at) |
956 | goto fail_finish; | |
957 | /* Finish the diagnostic message */ | |
958 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%ld...\n", (long)(check_at - strpos)) ); | |
2c2d71f5 | 959 | |
8fd34720 DM |
960 | /* set rx_origin to the minimum position where the regex could start |
961 | * matching, given the constraint of the just-matched check substring. | |
962 | * But don't set it lower than previously. | |
963 | */ | |
fdc003fd | 964 | |
8fd34720 DM |
965 | if (check_at - rx_origin > prog->check_offset_max) |
966 | rx_origin = HOP3c(check_at, -prog->check_offset_max, rx_origin); | |
967 | } | |
fdc003fd DM |
968 | |
969 | ||
1a4edc3c | 970 | /* now look for the 'other' substring if defined */ |
2c2d71f5 | 971 | |
6480a6c4 DM |
972 | if (utf8_target ? prog->substrs->data[other_ix].utf8_substr |
973 | : prog->substrs->data[other_ix].substr) | |
1de06328 | 974 | { |
30944b6d | 975 | /* Take into account the "other" substring. */ |
6c3343a6 DM |
976 | char *last, *last1; |
977 | char *s; | |
978 | SV* must; | |
979 | struct reg_substr_datum *other; | |
980 | ||
981 | do_other_substr: | |
982 | other = &prog->substrs->data[other_ix]; | |
983 | ||
984 | /* if "other" is anchored: | |
985 | * we've previously found a floating substr starting at check_at. | |
986 | * This means that the regex origin must lie somewhere | |
987 | * between min (rx_origin): HOP3(check_at, -check_offset_max) | |
988 | * and max: HOP3(check_at, -check_offset_min) | |
989 | * (except that min will be >= strpos) | |
990 | * So the fixed substr must lie somewhere between | |
991 | * HOP3(min, anchored_offset) | |
992 | * HOP3(max, anchored_offset) + SvCUR(substr) | |
993 | */ | |
994 | ||
995 | /* if "other" is floating | |
996 | * Calculate last1, the absolute latest point where the | |
997 | * floating substr could start in the string, ignoring any | |
998 | * constraints from the earlier fixed match. It is calculated | |
999 | * as follows: | |
1000 | * | |
1001 | * strend - prog->minlen (in chars) is the absolute latest | |
1002 | * position within the string where the origin of the regex | |
1003 | * could appear. The latest start point for the floating | |
1004 | * substr is float_min_offset(*) on from the start of the | |
1005 | * regex. last1 simply combines thee two offsets. | |
1006 | * | |
1007 | * (*) You might think the latest start point should be | |
1008 | * float_max_offset from the regex origin, and technically | |
1009 | * you'd be correct. However, consider | |
1010 | * /a\d{2,4}bcd\w/ | |
1011 | * Here, float min, max are 3,5 and minlen is 7. | |
1012 | * This can match either | |
1013 | * /a\d\dbcd\w/ | |
1014 | * /a\d\d\dbcd\w/ | |
1015 | * /a\d\d\d\dbcd\w/ | |
1016 | * In the first case, the regex matches minlen chars; in the | |
1017 | * second, minlen+1, in the third, minlen+2. | |
1018 | * In the first case, the floating offset is 3 (which equals | |
1019 | * float_min), in the second, 4, and in the third, 5 (which | |
1020 | * equals float_max). In all cases, the floating string bcd | |
1021 | * can never start more than 4 chars from the end of the | |
1022 | * string, which equals minlen - float_min. As the substring | |
1023 | * starts to match more than float_min from the start of the | |
1024 | * regex, it makes the regex match more than minlen chars, | |
1025 | * and the two cancel each other out. So we can always use | |
1026 | * float_min - minlen, rather than float_max - minlen for the | |
1027 | * latest position in the string. | |
1028 | * | |
1029 | * Note that -minlen + float_min_offset is equivalent (AFAIKT) | |
1030 | * to CHR_SVLEN(must) - !!SvTAIL(must) + prog->float_end_shift | |
1031 | */ | |
1032 | ||
e7a14a9c | 1033 | assert(prog->minlen >= other->min_offset); |
6c3343a6 DM |
1034 | last1 = HOP3c(strend, |
1035 | other->min_offset - prog->minlen, strbeg); | |
1036 | ||
4d006249 | 1037 | if (other_ix) {/* i.e. if (other-is-float) */ |
6c3343a6 DM |
1038 | /* last is the latest point where the floating substr could |
1039 | * start, *given* any constraints from the earlier fixed | |
1040 | * match. This constraint is that the floating string starts | |
1041 | * <= float_max_offset chars from the regex origin (rx_origin). | |
1042 | * If this value is less than last1, use it instead. | |
eb3831ce | 1043 | */ |
6c3343a6 DM |
1044 | assert(rx_origin <= last1); |
1045 | last = | |
1046 | /* this condition handles the offset==infinity case, and | |
1047 | * is a short-cut otherwise. Although it's comparing a | |
1048 | * byte offset to a char length, it does so in a safe way, | |
1049 | * since 1 char always occupies 1 or more bytes, | |
1050 | * so if a string range is (last1 - rx_origin) bytes, | |
1051 | * it will be less than or equal to (last1 - rx_origin) | |
1052 | * chars; meaning it errs towards doing the accurate HOP3 | |
1053 | * rather than just using last1 as a short-cut */ | |
1054 | (last1 - rx_origin) < other->max_offset | |
1055 | ? last1 | |
1056 | : (char*)HOP3lim(rx_origin, other->max_offset, last1); | |
1057 | } | |
1058 | else { | |
1059 | assert(strpos + start_shift <= check_at); | |
1060 | last = HOP4c(check_at, other->min_offset - start_shift, | |
1061 | strbeg, strend); | |
1062 | } | |
ead917d0 | 1063 | |
6c3343a6 DM |
1064 | s = HOP3c(rx_origin, other->min_offset, strend); |
1065 | if (s < other_last) /* These positions already checked */ | |
1066 | s = other_last; | |
1067 | ||
1068 | must = utf8_target ? other->utf8_substr : other->substr; | |
1069 | assert(SvPOK(must)); | |
1070 | s = fbm_instr( | |
1071 | (unsigned char*)s, | |
1072 | (unsigned char*)last + SvCUR(must) - (SvTAIL(must)!=0), | |
1073 | must, | |
1074 | multiline ? FBMrf_MULTILINE : 0 | |
1075 | ); | |
1076 | DEBUG_EXECUTE_r({ | |
1077 | RE_PV_QUOTED_DECL(quoted, utf8_target, PERL_DEBUG_PAD_ZERO(0), | |
1078 | SvPVX_const(must), RE_SV_DUMPLEN(must), 30); | |
1079 | PerlIO_printf(Perl_debug_log, " %s %s substr %s%s", | |
1080 | s ? "Found" : "Contradicts", | |
1081 | other_ix ? "floating" : "anchored", | |
1082 | quoted, RE_SV_TAIL(must)); | |
1083 | }); | |
ead917d0 | 1084 | |
ead917d0 | 1085 | |
6c3343a6 DM |
1086 | if (!s) { |
1087 | /* last1 is latest possible substr location. If we didn't | |
1088 | * find it before there, we never will */ | |
1089 | if (last >= last1) { | |
1090 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, | |
1091 | ", giving up...\n")); | |
1092 | goto fail_finish; | |
ead917d0 DM |
1093 | } |
1094 | ||
6c3343a6 DM |
1095 | /* try to find the check substr again at a later |
1096 | * position. Maybe next time we'll find the "other" substr | |
1097 | * in range too */ | |
1098 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, | |
1099 | ", trying %s at offset %ld...\n", | |
1100 | (other_ix ? "floating" : "anchored"), | |
197641b6 | 1101 | (long)(HOP3c(check_at, 1, strend) - strpos))); |
6c3343a6 DM |
1102 | |
1103 | other_last = HOP3c(last, 1, strend) /* highest failure */; | |
1104 | rx_origin = | |
4d006249 | 1105 | other_ix /* i.e. if other-is-float */ |
6c3343a6 DM |
1106 | ? HOP3c(rx_origin, 1, strend) |
1107 | : HOP4c(last, 1 - other->min_offset, strbeg, strend); | |
1108 | goto restart; | |
1109 | } | |
1110 | else { | |
1111 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n", | |
197641b6 | 1112 | (long)(s - strpos))); |
ead917d0 | 1113 | |
4d006249 | 1114 | if (other_ix) { /* if (other-is-float) */ |
6c3343a6 DM |
1115 | /* other_last is set to s, not s+1, since its possible for |
1116 | * a floating substr to fail first time, then succeed | |
1117 | * second time at the same floating position; e.g.: | |
1118 | * "-AB--AABZ" =~ /\wAB\d*Z/ | |
1119 | * The first time round, anchored and float match at | |
1120 | * "-(AB)--AAB(Z)" then fail on the initial \w character | |
1121 | * class. Second time round, they match at "-AB--A(AB)(Z)". | |
1122 | */ | |
1123 | other_last = s; | |
ead917d0 DM |
1124 | } |
1125 | else { | |
6c3343a6 DM |
1126 | rx_origin = HOP3c(s, -other->min_offset, strbeg); |
1127 | other_last = HOP3c(s, 1, strend); | |
ead917d0 | 1128 | } |
6c3343a6 | 1129 | } |
cad2e5aa | 1130 | } |
acba93e8 DM |
1131 | else { |
1132 | DEBUG_OPTIMISE_MORE_r( | |
1133 | PerlIO_printf(Perl_debug_log, | |
1134 | " Check-only match: offset min:%"IVdf" max:%"IVdf | |
1c1c599d | 1135 | " check_at:%"IVdf" rx_origin:%"IVdf" rx_origin-check_at:%"IVdf |
acba93e8 DM |
1136 | " strend-strpos:%"IVdf"\n", |
1137 | (IV)prog->check_offset_min, | |
1138 | (IV)prog->check_offset_max, | |
1c1c599d | 1139 | (IV)(check_at-strpos), |
acba93e8 | 1140 | (IV)(rx_origin-strpos), |
1c1c599d | 1141 | (IV)(rx_origin-check_at), |
acba93e8 DM |
1142 | (IV)(strend-strpos) |
1143 | ) | |
1144 | ); | |
1145 | } | |
2c2d71f5 | 1146 | |
acba93e8 | 1147 | postprocess_substr_matches: |
0991020e | 1148 | |
1a4edc3c | 1149 | /* handle the extra constraint of /^.../m if present */ |
e3c6feb0 | 1150 | |
7d2d37f5 | 1151 | if (ml_anch && rx_origin != strbeg && rx_origin[-1] != '\n') { |
4620cb61 DM |
1152 | char *s; |
1153 | ||
a62659bd DM |
1154 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, |
1155 | " looking for /^/m anchor")); | |
d0880ea7 DM |
1156 | |
1157 | /* we have failed the constraint of a \n before rx_origin. | |
2e759faa DM |
1158 | * Find the next \n, if any, even if it's beyond the current |
1159 | * anchored and/or floating substrings. Whether we should be | |
1160 | * scanning ahead for the next \n or the next substr is debatable. | |
1161 | * On the one hand you'd expect rare substrings to appear less | |
1162 | * often than \n's. On the other hand, searching for \n means | |
1163 | * we're effectively flipping been check_substr and "\n" on each | |
1164 | * iteration as the current "rarest" string candidate, which | |
1165 | * means for example that we'll quickly reject the whole string if | |
1166 | * hasn't got a \n, rather than trying every substr position | |
1167 | * first | |
1168 | */ | |
d0880ea7 | 1169 | |
4620cb61 DM |
1170 | s = HOP3c(strend, - prog->minlen, strpos); |
1171 | if (s <= rx_origin || | |
1172 | ! ( rx_origin = (char *)memchr(rx_origin, '\n', s - rx_origin))) | |
1173 | { | |
d0880ea7 DM |
1174 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, |
1175 | " Did not find /%s^%s/m...\n", | |
1176 | PL_colors[0], PL_colors[1])); | |
a62659bd DM |
1177 | goto fail_finish; |
1178 | } | |
d0880ea7 | 1179 | |
4ada1233 DM |
1180 | /* earliest possible origin is 1 char after the \n. |
1181 | * (since *rx_origin == '\n', it's safe to ++ here rather than | |
1182 | * HOP(rx_origin, 1)) */ | |
1183 | rx_origin++; | |
d0880ea7 | 1184 | |
f4f115de | 1185 | if (prog->substrs->check_ix == 0 /* check is anchored */ |
4ada1233 | 1186 | || rx_origin >= HOP3c(check_at, - prog->check_offset_min, strpos)) |
f4f115de | 1187 | { |
d0880ea7 DM |
1188 | /* Position contradicts check-string; either because |
1189 | * check was anchored (and thus has no wiggle room), | |
4ada1233 | 1190 | * or check was float and rx_origin is above the float range */ |
d0880ea7 DM |
1191 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, |
1192 | " Found /%s^%s/m, restarting lookup for check-string at offset %ld...\n", | |
4ada1233 | 1193 | PL_colors[0], PL_colors[1], (long)(rx_origin - strpos))); |
d0880ea7 DM |
1194 | goto restart; |
1195 | } | |
1196 | ||
1197 | /* if we get here, the check substr must have been float, | |
2e759faa | 1198 | * is in range, and we may or may not have had an anchored |
d0880ea7 DM |
1199 | * "other" substr which still contradicts */ |
1200 | assert(prog->substrs->check_ix); /* check is float */ | |
1201 | ||
1202 | if (utf8_target ? prog->anchored_utf8 : prog->anchored_substr) { | |
1203 | /* whoops, the anchored "other" substr exists, so we still | |
1204 | * contradict. On the other hand, the float "check" substr | |
1205 | * didn't contradict, so just retry the anchored "other" | |
1206 | * substr */ | |
1207 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, | |
1208 | " Found /%s^%s/m at offset %ld, rescanning for anchored from offset %ld...\n", | |
1209 | PL_colors[0], PL_colors[1], | |
1210 | (long)(rx_origin - strpos), | |
1211 | (long)(rx_origin - strpos + prog->anchored_offset))); | |
1212 | goto do_other_substr; | |
1213 | } | |
1214 | ||
1215 | /* success: we don't contradict the found floating substring | |
1216 | * (and there's no anchored substr). */ | |
d0880ea7 DM |
1217 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, |
1218 | " Found /%s^%s/m at offset %ld...\n", | |
1219 | PL_colors[0], PL_colors[1], (long)(rx_origin - strpos))); | |
e3c6feb0 DM |
1220 | } |
1221 | else { | |
2e759faa | 1222 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, |
fe4f3442 | 1223 | " (multiline anchor test skipped)\n")); |
e3c6feb0 DM |
1224 | } |
1225 | ||
ffad1e6a | 1226 | success_at_start: |
e3c6feb0 | 1227 | |
cad2e5aa | 1228 | |
dd170ff5 DM |
1229 | /* if we have a starting character class, then test that extra constraint. |
1230 | * (trie stclasses are too expensive to use here, we are better off to | |
1231 | * leave it to regmatch itself) */ | |
1232 | ||
f8fc2ecf | 1233 | if (progi->regstclass && PL_regkind[OP(progi->regstclass)]!=TRIE) { |
f8fc2ecf | 1234 | const U8* const str = (U8*)STRING(progi->regstclass); |
0991020e | 1235 | |
2c75e362 | 1236 | /* XXX this value could be pre-computed */ |
f8fc2ecf | 1237 | const int cl_l = (PL_regkind[OP(progi->regstclass)] == EXACT |
2c75e362 DM |
1238 | ? (reginfo->is_utf8_pat |
1239 | ? utf8_distance(str + STR_LEN(progi->regstclass), str) | |
1240 | : STR_LEN(progi->regstclass)) | |
66e933ab | 1241 | : 1); |
1de06328 | 1242 | char * endpos; |
fa3bb21d | 1243 | char *s; |
000dfd2d DM |
1244 | /* latest pos that a matching float substr constrains rx start to */ |
1245 | char *rx_max_float = NULL; | |
1246 | ||
c75a3985 DM |
1247 | /* if the current rx_origin is anchored, either by satisfying an |
1248 | * anchored substring constraint, or a /^.../m constraint, then we | |
1249 | * can reject the current origin if the start class isn't found | |
1250 | * at the current position. If we have a float-only match, then | |
1251 | * rx_origin is constrained to a range; so look for the start class | |
1252 | * in that range. if neither, then look for the start class in the | |
1253 | * whole rest of the string */ | |
1254 | ||
dd170ff5 DM |
1255 | /* XXX DAPM it's not clear what the minlen test is for, and why |
1256 | * it's not used in the floating case. Nothing in the test suite | |
1257 | * causes minlen == 0 here. See <20140313134639.GS12844@iabyn.com>. | |
1258 | * Here are some old comments, which may or may not be correct: | |
1259 | * | |
1260 | * minlen == 0 is possible if regstclass is \b or \B, | |
1261 | * and the fixed substr is ''$. | |
1262 | * Since minlen is already taken into account, rx_origin+1 is | |
1263 | * before strend; accidentally, minlen >= 1 guaranties no false | |
1264 | * positives at rx_origin + 1 even for \b or \B. But (minlen? 1 : | |
1265 | * 0) below assumes that regstclass does not come from lookahead... | |
1266 | * If regstclass takes bytelength more than 1: If charlength==1, OK. | |
1267 | * This leaves EXACTF-ish only, which are dealt with in | |
1268 | * find_byclass(). | |
1269 | */ | |
1270 | ||
7d2d37f5 | 1271 | if (prog->anchored_substr || prog->anchored_utf8 || ml_anch) |
fa3bb21d | 1272 | endpos= HOP3c(rx_origin, (prog->minlen ? cl_l : 0), strend); |
000dfd2d DM |
1273 | else if (prog->float_substr || prog->float_utf8) { |
1274 | rx_max_float = HOP3c(check_at, -start_shift, strbeg); | |
1275 | endpos= HOP3c(rx_max_float, cl_l, strend); | |
1276 | } | |
1de06328 YO |
1277 | else |
1278 | endpos= strend; | |
1279 | ||
1dc475d0 DM |
1280 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, |
1281 | " looking for class: start_shift: %"IVdf" check_at: %"IVdf | |
c43b5520 | 1282 | " rx_origin: %"IVdf" endpos: %"IVdf"\n", |
1dc475d0 | 1283 | (IV)start_shift, (IV)(check_at - strbeg), |
c43b5520 | 1284 | (IV)(rx_origin - strbeg), (IV)(endpos - strbeg))); |
d8080198 | 1285 | |
c43b5520 | 1286 | s = find_byclass(prog, progi->regstclass, rx_origin, endpos, |
f9176b44 | 1287 | reginfo); |
be778b1a | 1288 | if (!s) { |
6eb5f6b9 | 1289 | if (endpos == strend) { |
a3621e74 | 1290 | DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log, |
1dc475d0 | 1291 | " Could not match STCLASS...\n") ); |
6eb5f6b9 JH |
1292 | goto fail; |
1293 | } | |
a3621e74 | 1294 | DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log, |
1dc475d0 | 1295 | " This position contradicts STCLASS...\n") ); |
e0eb31e7 DM |
1296 | if ((prog->intflags & PREGf_ANCH) && !ml_anch |
1297 | && !(prog->intflags & PREGf_IMPLICIT)) | |
653099ff | 1298 | goto fail; |
9fed8d02 | 1299 | |
6eb5f6b9 | 1300 | /* Contradict one of substrings */ |
97136c8a DM |
1301 | if (prog->anchored_substr || prog->anchored_utf8) { |
1302 | if (prog->substrs->check_ix == 1) { /* check is float */ | |
1303 | /* Have both, check_string is floating */ | |
1304 | assert(rx_origin + start_shift <= check_at); | |
1305 | if (rx_origin + start_shift != check_at) { | |
1306 | /* not at latest position float substr could match: | |
c75a3985 DM |
1307 | * Recheck anchored substring, but not floating. |
1308 | * The condition above is in bytes rather than | |
1309 | * chars for efficiency. It's conservative, in | |
1310 | * that it errs on the side of doing 'goto | |
1311 | * do_other_substr', where a more accurate | |
1312 | * char-based calculation will be done */ | |
97136c8a DM |
1313 | DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log, |
1314 | " Looking for anchored substr starting at offset %ld...\n", | |
1315 | (long)(other_last - strpos)) ); | |
1316 | goto do_other_substr; | |
3369914b | 1317 | } |
3369914b DM |
1318 | } |
1319 | } | |
97136c8a | 1320 | else { |
9fed8d02 DM |
1321 | /* float-only */ |
1322 | ||
7d2d37f5 | 1323 | if (ml_anch) { |
c75a3985 DM |
1324 | /* In the presence of ml_anch, we might be able to |
1325 | * find another \n without breaking the current float | |
1326 | * constraint. */ | |
1327 | ||
1328 | /* strictly speaking this should be HOP3c(..., 1, ...), | |
1329 | * but since we goto a block of code that's going to | |
1330 | * search for the next \n if any, its safe here */ | |
9fed8d02 | 1331 | rx_origin++; |
9fed8d02 DM |
1332 | DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log, |
1333 | " Looking for /%s^%s/m starting at offset %ld...\n", | |
1334 | PL_colors[0], PL_colors[1], | |
1335 | (long)(rx_origin - strpos)) ); | |
9fed8d02 | 1336 | goto postprocess_substr_matches; |
ab60c45a | 1337 | } |
c75a3985 DM |
1338 | |
1339 | /* strictly speaking this can never be true; but might | |
1340 | * be if we ever allow intuit without substrings */ | |
1341 | if (!(utf8_target ? prog->float_utf8 : prog->float_substr)) | |
9fed8d02 | 1342 | goto fail; |
c75a3985 | 1343 | |
000dfd2d | 1344 | rx_origin = rx_max_float; |
9fed8d02 DM |
1345 | } |
1346 | ||
c75a3985 DM |
1347 | /* at this point, any matching substrings have been |
1348 | * contradicted. Start again... */ | |
1349 | ||
9fed8d02 | 1350 | rx_origin = HOP3c(rx_origin, 1, strend); |
557f47af DM |
1351 | |
1352 | /* uses bytes rather than char calculations for efficiency. | |
1353 | * It's conservative: it errs on the side of doing 'goto restart', | |
1354 | * where there is code that does a proper char-based test */ | |
9fed8d02 | 1355 | if (rx_origin + start_shift + end_shift > strend) { |
40268e5b | 1356 | DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log, |
9fed8d02 DM |
1357 | " Could not match STCLASS...\n") ); |
1358 | goto fail; | |
1359 | } | |
1360 | DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log, | |
1361 | " Looking for %s substr starting at offset %ld...\n", | |
1362 | (prog->substrs->check_ix ? "floating" : "anchored"), | |
1363 | (long)(rx_origin + start_shift - strpos)) ); | |
1364 | goto restart; | |
6eb5f6b9 | 1365 | } |
9fed8d02 | 1366 | |
c75a3985 DM |
1367 | /* Success !!! */ |
1368 | ||
5f9c6575 | 1369 | if (rx_origin != s) { |
a3621e74 | 1370 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, |
1dc475d0 | 1371 | " By STCLASS: moving %ld --> %ld\n", |
5f9c6575 | 1372 | (long)(rx_origin - strpos), (long)(s - strpos)) |
b7953727 JH |
1373 | ); |
1374 | } | |
1375 | else { | |
a3621e74 | 1376 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, |
1dc475d0 | 1377 | " Does not contradict STCLASS...\n"); |
b7953727 JH |
1378 | ); |
1379 | } | |
6eb5f6b9 | 1380 | } |
ffad1e6a DM |
1381 | |
1382 | /* Decide whether using the substrings helped */ | |
1383 | ||
1384 | if (rx_origin != strpos) { | |
1385 | /* Fixed substring is found far enough so that the match | |
1386 | cannot start at strpos. */ | |
1387 | ||
1388 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, " try at offset...\n")); | |
1389 | ++BmUSEFUL(utf8_target ? prog->check_utf8 : prog->check_substr); /* hooray/5 */ | |
1390 | } | |
1391 | else { | |
70563e16 DM |
1392 | /* The found rx_origin position does not prohibit matching at |
1393 | * strpos, so calling intuit didn't gain us anything. Decrement | |
1394 | * the BmUSEFUL() count on the check substring, and if we reach | |
1395 | * zero, free it. */ | |
1396 | if (!(prog->intflags & PREGf_NAUGHTY) | |
ffad1e6a DM |
1397 | && (utf8_target ? ( |
1398 | prog->check_utf8 /* Could be deleted already */ | |
1399 | && --BmUSEFUL(prog->check_utf8) < 0 | |
1400 | && (prog->check_utf8 == prog->float_utf8) | |
1401 | ) : ( | |
1402 | prog->check_substr /* Could be deleted already */ | |
1403 | && --BmUSEFUL(prog->check_substr) < 0 | |
1404 | && (prog->check_substr == prog->float_substr) | |
1405 | ))) | |
1406 | { | |
1407 | /* If flags & SOMETHING - do not do it many times on the same match */ | |
1408 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, " ... Disabling check substring...\n")); | |
1409 | /* XXX Does the destruction order has to change with utf8_target? */ | |
1410 | SvREFCNT_dec(utf8_target ? prog->check_utf8 : prog->check_substr); | |
1411 | SvREFCNT_dec(utf8_target ? prog->check_substr : prog->check_utf8); | |
1412 | prog->check_substr = prog->check_utf8 = NULL; /* disable */ | |
1413 | prog->float_substr = prog->float_utf8 = NULL; /* clear */ | |
1414 | check = NULL; /* abort */ | |
ffad1e6a DM |
1415 | /* XXXX This is a remnant of the old implementation. It |
1416 | looks wasteful, since now INTUIT can use many | |
1417 | other heuristics. */ | |
1418 | prog->extflags &= ~RXf_USE_INTUIT; | |
ffad1e6a DM |
1419 | } |
1420 | } | |
1421 | ||
1422 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, | |
1423 | "Intuit: %sSuccessfully guessed:%s match at offset %ld\n", | |
1424 | PL_colors[4], PL_colors[5], (long)(rx_origin - strpos)) ); | |
1425 | ||
c765d6e0 | 1426 | return rx_origin; |
2c2d71f5 JH |
1427 | |
1428 | fail_finish: /* Substring not found */ | |
33b8afdf | 1429 | if (prog->check_substr || prog->check_utf8) /* could be removed already */ |
f2ed9b32 | 1430 | BmUSEFUL(utf8_target ? prog->check_utf8 : prog->check_substr) += 5; /* hooray */ |
cad2e5aa | 1431 | fail: |
a3621e74 | 1432 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch rejected by optimizer%s\n", |
e4584336 | 1433 | PL_colors[4], PL_colors[5])); |
bd61b366 | 1434 | return NULL; |
cad2e5aa | 1435 | } |
9661b544 | 1436 | |
70563e16 | 1437 | |
a0a388a1 | 1438 | #define DECL_TRIE_TYPE(scan) \ |
098b07d5 KW |
1439 | const enum { trie_plain, trie_utf8, trie_utf8_fold, trie_latin_utf8_fold, \ |
1440 | trie_utf8_exactfa_fold, trie_latin_utf8_exactfa_fold } \ | |
fab2782b YO |
1441 | trie_type = ((scan->flags == EXACT) \ |
1442 | ? (utf8_target ? trie_utf8 : trie_plain) \ | |
098b07d5 KW |
1443 | : (scan->flags == EXACTFA) \ |
1444 | ? (utf8_target ? trie_utf8_exactfa_fold : trie_latin_utf8_exactfa_fold) \ | |
1445 | : (utf8_target ? trie_utf8_fold : trie_latin_utf8_fold)) | |
fab2782b | 1446 | |
fd3249ee | 1447 | #define REXEC_TRIE_READ_CHAR(trie_type, trie, widecharmap, uc, uscan, len, uvc, charid, foldlen, foldbuf, uniflags) \ |
baa60164 | 1448 | STMT_START { \ |
fab2782b | 1449 | STRLEN skiplen; \ |
baa60164 | 1450 | U8 flags = FOLD_FLAGS_FULL; \ |
fab2782b | 1451 | switch (trie_type) { \ |
31f05a37 | 1452 | case trie_utf8_exactfa_fold: \ |
baa60164 KW |
1453 | flags |= FOLD_FLAGS_NOMIX_ASCII; \ |
1454 | /* FALL THROUGH */ \ | |
fab2782b YO |
1455 | case trie_utf8_fold: \ |
1456 | if ( foldlen>0 ) { \ | |
c80e42f3 | 1457 | uvc = utf8n_to_uvchr( (const U8*) uscan, UTF8_MAXLEN, &len, uniflags ); \ |
fab2782b YO |
1458 | foldlen -= len; \ |
1459 | uscan += len; \ | |
1460 | len=0; \ | |
1461 | } else { \ | |
445bf929 | 1462 | uvc = _to_utf8_fold_flags( (const U8*) uc, foldbuf, &foldlen, flags); \ |
fab2782b YO |
1463 | len = UTF8SKIP(uc); \ |
1464 | skiplen = UNISKIP( uvc ); \ | |
1465 | foldlen -= skiplen; \ | |
1466 | uscan = foldbuf + skiplen; \ | |
1467 | } \ | |
1468 | break; \ | |
baa60164 KW |
1469 | case trie_latin_utf8_exactfa_fold: \ |
1470 | flags |= FOLD_FLAGS_NOMIX_ASCII; \ | |
1471 | /* FALL THROUGH */ \ | |
fab2782b YO |
1472 | case trie_latin_utf8_fold: \ |
1473 | if ( foldlen>0 ) { \ | |
c80e42f3 | 1474 | uvc = utf8n_to_uvchr( (const U8*) uscan, UTF8_MAXLEN, &len, uniflags ); \ |
fab2782b YO |
1475 | foldlen -= len; \ |
1476 | uscan += len; \ | |
1477 | len=0; \ | |
1478 | } else { \ | |
1479 | len = 1; \ | |
31f05a37 | 1480 | uvc = _to_fold_latin1( (U8) *uc, foldbuf, &foldlen, flags); \ |
fab2782b YO |
1481 | skiplen = UNISKIP( uvc ); \ |
1482 | foldlen -= skiplen; \ | |
1483 | uscan = foldbuf + skiplen; \ | |
1484 | } \ | |
1485 | break; \ | |
1486 | case trie_utf8: \ | |
c80e42f3 | 1487 | uvc = utf8n_to_uvchr( (const U8*) uc, UTF8_MAXLEN, &len, uniflags ); \ |
fab2782b YO |
1488 | break; \ |
1489 | case trie_plain: \ | |
1490 | uvc = (UV)*uc; \ | |
1491 | len = 1; \ | |
1492 | } \ | |
1493 | if (uvc < 256) { \ | |
1494 | charid = trie->charmap[ uvc ]; \ | |
1495 | } \ | |
1496 | else { \ | |
1497 | charid = 0; \ | |
1498 | if (widecharmap) { \ | |
1499 | SV** const svpp = hv_fetch(widecharmap, \ | |
1500 | (char*)&uvc, sizeof(UV), 0); \ | |
1501 | if (svpp) \ | |
1502 | charid = (U16)SvIV(*svpp); \ | |
1503 | } \ | |
1504 | } \ | |
4cadc6a9 YO |
1505 | } STMT_END |
1506 | ||
4cadc6a9 YO |
1507 | #define REXEC_FBC_EXACTISH_SCAN(CoNd) \ |
1508 | STMT_START { \ | |
1509 | while (s <= e) { \ | |
1510 | if ( (CoNd) \ | |
fac1af77 | 1511 | && (ln == 1 || folder(s, pat_string, ln)) \ |
02d5137b | 1512 | && (reginfo->intuit || regtry(reginfo, &s)) )\ |
4cadc6a9 YO |
1513 | goto got_it; \ |
1514 | s++; \ | |
1515 | } \ | |
1516 | } STMT_END | |
1517 | ||
1518 | #define REXEC_FBC_UTF8_SCAN(CoDe) \ | |
1519 | STMT_START { \ | |
9a902117 | 1520 | while (s < strend) { \ |
4cadc6a9 | 1521 | CoDe \ |
9a902117 | 1522 | s += UTF8SKIP(s); \ |
4cadc6a9 YO |
1523 | } \ |
1524 | } STMT_END | |
1525 | ||
1526 | #define REXEC_FBC_SCAN(CoDe) \ | |
1527 | STMT_START { \ | |
1528 | while (s < strend) { \ | |
1529 | CoDe \ | |
1530 | s++; \ | |
1531 | } \ | |
1532 | } STMT_END | |
1533 | ||
1534 | #define REXEC_FBC_UTF8_CLASS_SCAN(CoNd) \ | |
1535 | REXEC_FBC_UTF8_SCAN( \ | |
1536 | if (CoNd) { \ | |
02d5137b | 1537 | if (tmp && (reginfo->intuit || regtry(reginfo, &s))) \ |
4cadc6a9 YO |
1538 | goto got_it; \ |
1539 | else \ | |
1540 | tmp = doevery; \ | |
1541 | } \ | |
1542 | else \ | |
1543 | tmp = 1; \ | |
1544 | ) | |
1545 | ||
1546 | #define REXEC_FBC_CLASS_SCAN(CoNd) \ | |
1547 | REXEC_FBC_SCAN( \ | |
1548 | if (CoNd) { \ | |
02d5137b | 1549 | if (tmp && (reginfo->intuit || regtry(reginfo, &s))) \ |
4cadc6a9 YO |
1550 | goto got_it; \ |
1551 | else \ | |
1552 | tmp = doevery; \ | |
1553 | } \ | |
1554 | else \ | |
1555 | tmp = 1; \ | |
1556 | ) | |
1557 | ||
baa60164 | 1558 | #define REXEC_FBC_TRYIT \ |
02d5137b | 1559 | if ((reginfo->intuit || regtry(reginfo, &s))) \ |
4cadc6a9 YO |
1560 | goto got_it |
1561 | ||
e1d1eefb | 1562 | #define REXEC_FBC_CSCAN(CoNdUtF8,CoNd) \ |
baa60164 | 1563 | if (utf8_target) { \ |
e1d1eefb YO |
1564 | REXEC_FBC_UTF8_CLASS_SCAN(CoNdUtF8); \ |
1565 | } \ | |
1566 | else { \ | |
1567 | REXEC_FBC_CLASS_SCAN(CoNd); \ | |
d981ef24 | 1568 | } |
e1d1eefb | 1569 | |
baa60164 | 1570 | #define DUMP_EXEC_POS(li,s,doutf8) \ |
9d9163fb | 1571 | dump_exec_pos(li,s,(reginfo->strend),(reginfo->strbeg), \ |
6d59b646 | 1572 | startpos, doutf8) |
786e8c11 | 1573 | |
cfaf538b | 1574 | |
baa60164 | 1575 | #define UTF8_NOLOAD(TEST_NON_UTF8, IF_SUCCESS, IF_FAIL) \ |
9d9163fb | 1576 | tmp = (s != reginfo->strbeg) ? UCHARAT(s - 1) : '\n'; \ |
cfaf538b KW |
1577 | tmp = TEST_NON_UTF8(tmp); \ |
1578 | REXEC_FBC_UTF8_SCAN( \ | |
ce1d15d5 | 1579 | if (tmp == ! TEST_NON_UTF8((U8) *s)) { \ |
cfaf538b KW |
1580 | tmp = !tmp; \ |
1581 | IF_SUCCESS; \ | |
1582 | } \ | |
1583 | else { \ | |
1584 | IF_FAIL; \ | |
1585 | } \ | |
1586 | ); \ | |
1587 | ||
baa60164 | 1588 | #define UTF8_LOAD(TeSt1_UtF8, TeSt2_UtF8, IF_SUCCESS, IF_FAIL) \ |
9d9163fb | 1589 | if (s == reginfo->strbeg) { \ |
cfaf538b KW |
1590 | tmp = '\n'; \ |
1591 | } \ | |
1592 | else { \ | |
9d9163fb | 1593 | U8 * const r = reghop3((U8*)s, -1, (U8*)reginfo->strbeg); \ |
3db24e1e KW |
1594 | tmp = utf8n_to_uvchr(r, (U8*) reginfo->strend - r, \ |
1595 | 0, UTF8_ALLOW_DEFAULT); \ | |
cfaf538b KW |
1596 | } \ |
1597 | tmp = TeSt1_UtF8; \ | |
3db24e1e | 1598 | LOAD_UTF8_CHARCLASS_ALNUM(); \ |
cfaf538b | 1599 | REXEC_FBC_UTF8_SCAN( \ |
3db24e1e | 1600 | if (tmp == ! (TeSt2_UtF8)) { \ |
cfaf538b KW |
1601 | tmp = !tmp; \ |
1602 | IF_SUCCESS; \ | |
1603 | } \ | |
1604 | else { \ | |
1605 | IF_FAIL; \ | |
1606 | } \ | |
1607 | ); \ | |
1608 | ||
63ac0dad KW |
1609 | /* The only difference between the BOUND and NBOUND cases is that |
1610 | * REXEC_FBC_TRYIT is called when matched in BOUND, and when non-matched in | |
1611 | * NBOUND. This is accomplished by passing it in either the if or else clause, | |
1612 | * with the other one being empty */ | |
1613 | #define FBC_BOUND(TEST_NON_UTF8, TEST1_UTF8, TEST2_UTF8) \ | |
1dcf4a1b | 1614 | FBC_BOUND_COMMON(UTF8_LOAD(TEST1_UTF8, TEST2_UTF8, REXEC_FBC_TRYIT, PLACEHOLDER), TEST_NON_UTF8, REXEC_FBC_TRYIT, PLACEHOLDER) |
cfaf538b KW |
1615 | |
1616 | #define FBC_BOUND_NOLOAD(TEST_NON_UTF8, TEST1_UTF8, TEST2_UTF8) \ | |
1dcf4a1b | 1617 | FBC_BOUND_COMMON(UTF8_NOLOAD(TEST_NON_UTF8, REXEC_FBC_TRYIT, PLACEHOLDER), TEST_NON_UTF8, REXEC_FBC_TRYIT, PLACEHOLDER) |
63ac0dad KW |
1618 | |
1619 | #define FBC_NBOUND(TEST_NON_UTF8, TEST1_UTF8, TEST2_UTF8) \ | |
1dcf4a1b | 1620 | FBC_BOUND_COMMON(UTF8_LOAD(TEST1_UTF8, TEST2_UTF8, PLACEHOLDER, REXEC_FBC_TRYIT), TEST_NON_UTF8, PLACEHOLDER, REXEC_FBC_TRYIT) |
cfaf538b KW |
1621 | |
1622 | #define FBC_NBOUND_NOLOAD(TEST_NON_UTF8, TEST1_UTF8, TEST2_UTF8) \ | |
1dcf4a1b | 1623 | FBC_BOUND_COMMON(UTF8_NOLOAD(TEST_NON_UTF8, PLACEHOLDER, REXEC_FBC_TRYIT), TEST_NON_UTF8, PLACEHOLDER, REXEC_FBC_TRYIT) |
cfaf538b | 1624 | |
63ac0dad KW |
1625 | |
1626 | /* Common to the BOUND and NBOUND cases. Unfortunately the UTF8 tests need to | |
1627 | * be passed in completely with the variable name being tested, which isn't | |
1628 | * such a clean interface, but this is easier to read than it was before. We | |
1629 | * are looking for the boundary (or non-boundary between a word and non-word | |
1630 | * character. The utf8 and non-utf8 cases have the same logic, but the details | |
1631 | * must be different. Find the "wordness" of the character just prior to this | |
1632 | * one, and compare it with the wordness of this one. If they differ, we have | |
1633 | * a boundary. At the beginning of the string, pretend that the previous | |
1634 | * character was a new-line */ | |
baa60164 | 1635 | #define FBC_BOUND_COMMON(UTF8_CODE, TEST_NON_UTF8, IF_SUCCESS, IF_FAIL) \ |
63ac0dad | 1636 | if (utf8_target) { \ |
baa60164 | 1637 | UTF8_CODE \ |
63ac0dad KW |
1638 | } \ |
1639 | else { /* Not utf8 */ \ | |
9d9163fb | 1640 | tmp = (s != reginfo->strbeg) ? UCHARAT(s - 1) : '\n'; \ |
63ac0dad KW |
1641 | tmp = TEST_NON_UTF8(tmp); \ |
1642 | REXEC_FBC_SCAN( \ | |
1643 | if (tmp == ! TEST_NON_UTF8((U8) *s)) { \ | |
1644 | tmp = !tmp; \ | |
1645 | IF_SUCCESS; \ | |
1646 | } \ | |
1647 | else { \ | |
1648 | IF_FAIL; \ | |
1649 | } \ | |
1650 | ); \ | |
1651 | } \ | |
baa60164 | 1652 | if ((!prog->minlen && tmp) && (reginfo->intuit || regtry(reginfo, &s))) \ |
63ac0dad KW |
1653 | goto got_it; |
1654 | ||
786e8c11 | 1655 | /* We know what class REx starts with. Try to find this position... */ |
02d5137b | 1656 | /* if reginfo->intuit, its a dryrun */ |
786e8c11 YO |
1657 | /* annoyingly all the vars in this routine have different names from their counterparts |
1658 | in regmatch. /grrr */ | |
1659 | ||
3c3eec57 | 1660 | STATIC char * |
07be1b83 | 1661 | S_find_byclass(pTHX_ regexp * prog, const regnode *c, char *s, |
f9176b44 | 1662 | const char *strend, regmatch_info *reginfo) |
a687059c | 1663 | { |
73104a1b KW |
1664 | dVAR; |
1665 | const I32 doevery = (prog->intflags & PREGf_SKIP) == 0; | |
1666 | char *pat_string; /* The pattern's exactish string */ | |
1667 | char *pat_end; /* ptr to end char of pat_string */ | |
1668 | re_fold_t folder; /* Function for computing non-utf8 folds */ | |
1669 | const U8 *fold_array; /* array for folding ords < 256 */ | |
1670 | STRLEN ln; | |
1671 | STRLEN lnc; | |
73104a1b KW |
1672 | U8 c1; |
1673 | U8 c2; | |
1674 | char *e; | |
1675 | I32 tmp = 1; /* Scratch variable? */ | |
ba44c216 | 1676 | const bool utf8_target = reginfo->is_utf8_target; |
73104a1b | 1677 | UV utf8_fold_flags = 0; |
f9176b44 | 1678 | const bool is_utf8_pat = reginfo->is_utf8_pat; |
3018b823 KW |
1679 | bool to_complement = FALSE; /* Invert the result? Taking the xor of this |
1680 | with a result inverts that result, as 0^1 = | |
1681 | 1 and 1^1 = 0 */ | |
1682 | _char_class_number classnum; | |
1683 | ||
73104a1b | 1684 | RXi_GET_DECL(prog,progi); |
2f7f8cb1 | 1685 | |
73104a1b | 1686 | PERL_ARGS_ASSERT_FIND_BYCLASS; |
2f7f8cb1 | 1687 | |
73104a1b KW |
1688 | /* We know what class it must start with. */ |
1689 | switch (OP(c)) { | |
1690 | case ANYOF: | |
1691 | if (utf8_target) { | |
1692 | REXEC_FBC_UTF8_CLASS_SCAN( | |
3db24e1e | 1693 | reginclass(prog, c, (U8*)s, (U8*) strend, utf8_target)); |
73104a1b KW |
1694 | } |
1695 | else { | |
1696 | REXEC_FBC_CLASS_SCAN(REGINCLASS(prog, c, (U8*)s)); | |
1697 | } | |
1698 | break; | |
1699 | case CANY: | |
1700 | REXEC_FBC_SCAN( | |
02d5137b | 1701 | if (tmp && (reginfo->intuit || regtry(reginfo, &s))) |
73104a1b KW |
1702 | goto got_it; |
1703 | else | |
1704 | tmp = doevery; | |
1705 | ); | |
1706 | break; | |
1707 | ||
098b07d5 KW |
1708 | case EXACTFA_NO_TRIE: /* This node only generated for non-utf8 patterns */ |
1709 | assert(! is_utf8_pat); | |
1710 | /* FALL THROUGH */ | |
73104a1b | 1711 | case EXACTFA: |
984e6dd1 | 1712 | if (is_utf8_pat || utf8_target) { |
73104a1b KW |
1713 | utf8_fold_flags = FOLDEQ_UTF8_NOMIX_ASCII; |
1714 | goto do_exactf_utf8; | |
1715 | } | |
1716 | fold_array = PL_fold_latin1; /* Latin1 folds are not affected by */ | |
1717 | folder = foldEQ_latin1; /* /a, except the sharp s one which */ | |
1718 | goto do_exactf_non_utf8; /* isn't dealt with by these */ | |
77a6d856 | 1719 | |
2fdb7295 KW |
1720 | case EXACTF: /* This node only generated for non-utf8 patterns */ |
1721 | assert(! is_utf8_pat); | |
73104a1b | 1722 | if (utf8_target) { |
73104a1b KW |
1723 | utf8_fold_flags = 0; |
1724 | goto do_exactf_utf8; | |
1725 | } | |
1726 | fold_array = PL_fold; | |
1727 | folder = foldEQ; | |
1728 | goto do_exactf_non_utf8; | |
1729 | ||
1730 | case EXACTFL: | |
31f05a37 | 1731 | if (is_utf8_pat || utf8_target || IN_UTF8_CTYPE_LOCALE) { |
cea315b6 | 1732 | utf8_fold_flags = FOLDEQ_LOCALE; |
73104a1b KW |
1733 | goto do_exactf_utf8; |
1734 | } | |
1735 | fold_array = PL_fold_locale; | |
1736 | folder = foldEQ_locale; | |
1737 | goto do_exactf_non_utf8; | |
3c760661 | 1738 | |
73104a1b | 1739 | case EXACTFU_SS: |
984e6dd1 | 1740 | if (is_utf8_pat) { |
73104a1b KW |
1741 | utf8_fold_flags = FOLDEQ_S2_ALREADY_FOLDED; |
1742 | } | |
1743 | goto do_exactf_utf8; | |
16d951b7 | 1744 | |
73104a1b | 1745 | case EXACTFU: |
984e6dd1 DM |
1746 | if (is_utf8_pat || utf8_target) { |
1747 | utf8_fold_flags = is_utf8_pat ? FOLDEQ_S2_ALREADY_FOLDED : 0; | |
73104a1b KW |
1748 | goto do_exactf_utf8; |
1749 | } | |
fac1af77 | 1750 | |
73104a1b KW |
1751 | /* Any 'ss' in the pattern should have been replaced by regcomp, |
1752 | * so we don't have to worry here about this single special case | |
1753 | * in the Latin1 range */ | |
1754 | fold_array = PL_fold_latin1; | |
1755 | folder = foldEQ_latin1; | |
1756 | ||
1757 | /* FALL THROUGH */ | |
1758 | ||
1759 | do_exactf_non_utf8: /* Neither pattern nor string are UTF8, and there | |
1760 | are no glitches with fold-length differences | |
1761 | between the target string and pattern */ | |
1762 | ||
1763 | /* The idea in the non-utf8 EXACTF* cases is to first find the | |
1764 | * first character of the EXACTF* node and then, if necessary, | |
1765 | * case-insensitively compare the full text of the node. c1 is the | |
1766 | * first character. c2 is its fold. This logic will not work for | |
1767 | * Unicode semantics and the german sharp ss, which hence should | |
1768 | * not be compiled into a node that gets here. */ | |
1769 | pat_string = STRING(c); | |
1770 | ln = STR_LEN(c); /* length to match in octets/bytes */ | |
1771 | ||
1772 | /* We know that we have to match at least 'ln' bytes (which is the | |
1773 | * same as characters, since not utf8). If we have to match 3 | |
1774 | * characters, and there are only 2 availabe, we know without | |
1775 | * trying that it will fail; so don't start a match past the | |
1776 | * required minimum number from the far end */ | |
ea3daa5d | 1777 | e = HOP3c(strend, -((SSize_t)ln), s); |
73104a1b | 1778 | |
02d5137b | 1779 | if (reginfo->intuit && e < s) { |
73104a1b KW |
1780 | e = s; /* Due to minlen logic of intuit() */ |
1781 | } | |
fac1af77 | 1782 | |
73104a1b KW |
1783 | c1 = *pat_string; |
1784 | c2 = fold_array[c1]; | |
1785 | if (c1 == c2) { /* If char and fold are the same */ | |
1786 | REXEC_FBC_EXACTISH_SCAN(*(U8*)s == c1); | |
1787 | } | |
1788 | else { | |
1789 | REXEC_FBC_EXACTISH_SCAN(*(U8*)s == c1 || *(U8*)s == c2); | |
1790 | } | |
1791 | break; | |
fac1af77 | 1792 | |
73104a1b KW |
1793 | do_exactf_utf8: |
1794 | { | |
1795 | unsigned expansion; | |
1796 | ||
1797 | /* If one of the operands is in utf8, we can't use the simpler folding | |
1798 | * above, due to the fact that many different characters can have the | |
1799 | * same fold, or portion of a fold, or different- length fold */ | |
1800 | pat_string = STRING(c); | |
1801 | ln = STR_LEN(c); /* length to match in octets/bytes */ | |
1802 | pat_end = pat_string + ln; | |
984e6dd1 | 1803 | lnc = is_utf8_pat /* length to match in characters */ |
73104a1b KW |
1804 | ? utf8_length((U8 *) pat_string, (U8 *) pat_end) |
1805 | : ln; | |
1806 | ||
1807 | /* We have 'lnc' characters to match in the pattern, but because of | |
1808 | * multi-character folding, each character in the target can match | |
1809 | * up to 3 characters (Unicode guarantees it will never exceed | |
1810 | * this) if it is utf8-encoded; and up to 2 if not (based on the | |
1811 | * fact that the Latin 1 folds are already determined, and the | |
1812 | * only multi-char fold in that range is the sharp-s folding to | |
1813 | * 'ss'. Thus, a pattern character can match as little as 1/3 of a | |
1814 | * string character. Adjust lnc accordingly, rounding up, so that | |
1815 | * if we need to match at least 4+1/3 chars, that really is 5. */ | |
1816 | expansion = (utf8_target) ? UTF8_MAX_FOLD_CHAR_EXPAND : 2; | |
1817 | lnc = (lnc + expansion - 1) / expansion; | |
1818 | ||
1819 | /* As in the non-UTF8 case, if we have to match 3 characters, and | |
1820 | * only 2 are left, it's guaranteed to fail, so don't start a | |
1821 | * match that would require us to go beyond the end of the string | |
1822 | */ | |
ea3daa5d | 1823 | e = HOP3c(strend, -((SSize_t)lnc), s); |
73104a1b | 1824 | |
02d5137b | 1825 | if (reginfo->intuit && e < s) { |
73104a1b KW |
1826 | e = s; /* Due to minlen logic of intuit() */ |
1827 | } | |
0658cdde | 1828 | |
73104a1b KW |
1829 | /* XXX Note that we could recalculate e to stop the loop earlier, |
1830 | * as the worst case expansion above will rarely be met, and as we | |
1831 | * go along we would usually find that e moves further to the left. | |
1832 | * This would happen only after we reached the point in the loop | |
1833 | * where if there were no expansion we should fail. Unclear if | |
1834 | * worth the expense */ | |
1835 | ||
1836 | while (s <= e) { | |
1837 | char *my_strend= (char *)strend; | |
1838 | if (foldEQ_utf8_flags(s, &my_strend, 0, utf8_target, | |
984e6dd1 | 1839 | pat_string, NULL, ln, is_utf8_pat, utf8_fold_flags) |
02d5137b | 1840 | && (reginfo->intuit || regtry(reginfo, &s)) ) |
73104a1b KW |
1841 | { |
1842 | goto got_it; | |
1843 | } | |
1844 | s += (utf8_target) ? UTF8SKIP(s) : 1; | |
1845 | } | |
1846 | break; | |
1847 | } | |
1848 | case BOUNDL: | |
0eb30aeb | 1849 | FBC_BOUND(isWORDCHAR_LC, |
f4cd282c | 1850 | isWORDCHAR_LC_uvchr(tmp), |
0eb30aeb | 1851 | isWORDCHAR_LC_utf8((U8*)s)); |
73104a1b KW |
1852 | break; |
1853 | case NBOUNDL: | |
0eb30aeb | 1854 | FBC_NBOUND(isWORDCHAR_LC, |
f4cd282c | 1855 | isWORDCHAR_LC_uvchr(tmp), |
0eb30aeb | 1856 | isWORDCHAR_LC_utf8((U8*)s)); |
73104a1b KW |
1857 | break; |
1858 | case BOUND: | |
1859 | FBC_BOUND(isWORDCHAR, | |
0eb30aeb | 1860 | isWORDCHAR_uni(tmp), |
03940dc2 | 1861 | cBOOL(swash_fetch(PL_utf8_swash_ptrs[_CC_WORDCHAR], (U8*)s, utf8_target))); |
73104a1b KW |
1862 | break; |
1863 | case BOUNDA: | |
1864 | FBC_BOUND_NOLOAD(isWORDCHAR_A, | |
1865 | isWORDCHAR_A(tmp), | |
1866 | isWORDCHAR_A((U8*)s)); | |
1867 | break; | |
1868 | case NBOUND: | |
1869 | FBC_NBOUND(isWORDCHAR, | |
0eb30aeb | 1870 | isWORDCHAR_uni(tmp), |
03940dc2 | 1871 | cBOOL(swash_fetch(PL_utf8_swash_ptrs[_CC_WORDCHAR], (U8*)s, utf8_target))); |
73104a1b KW |
1872 | break; |
1873 | case NBOUNDA: | |
1874 | FBC_NBOUND_NOLOAD(isWORDCHAR_A, | |
1875 | isWORDCHAR_A(tmp), | |
1876 | isWORDCHAR_A((U8*)s)); | |
1877 | break; | |
1878 | case BOUNDU: | |
1879 | FBC_BOUND(isWORDCHAR_L1, | |
0eb30aeb | 1880 | isWORDCHAR_uni(tmp), |
03940dc2 | 1881 | cBOOL(swash_fetch(PL_utf8_swash_ptrs[_CC_WORDCHAR], (U8*)s, utf8_target))); |
73104a1b KW |
1882 | break; |
1883 | case NBOUNDU: | |
1884 | FBC_NBOUND(isWORDCHAR_L1, | |
0eb30aeb | 1885 | isWORDCHAR_uni(tmp), |
03940dc2 | 1886 | cBOOL(swash_fetch(PL_utf8_swash_ptrs[_CC_WORDCHAR], (U8*)s, utf8_target))); |
73104a1b | 1887 | break; |
73104a1b KW |
1888 | case LNBREAK: |
1889 | REXEC_FBC_CSCAN(is_LNBREAK_utf8_safe(s, strend), | |
1890 | is_LNBREAK_latin1_safe(s, strend) | |
1891 | ); | |
1892 | break; | |
3018b823 KW |
1893 | |
1894 | /* The argument to all the POSIX node types is the class number to pass to | |
1895 | * _generic_isCC() to build a mask for searching in PL_charclass[] */ | |
1896 | ||
1897 | case NPOSIXL: | |
1898 | to_complement = 1; | |
1899 | /* FALLTHROUGH */ | |
1900 | ||
1901 | case POSIXL: | |
3018b823 KW |
1902 | REXEC_FBC_CSCAN(to_complement ^ cBOOL(isFOO_utf8_lc(FLAGS(c), (U8 *) s)), |
1903 | to_complement ^ cBOOL(isFOO_lc(FLAGS(c), *s))); | |
73104a1b | 1904 | break; |
3018b823 KW |
1905 | |
1906 | case NPOSIXD: | |
1907 | to_complement = 1; | |
1908 | /* FALLTHROUGH */ | |
1909 | ||
1910 | case POSIXD: | |
1911 | if (utf8_target) { | |
1912 | goto posix_utf8; | |
1913 | } | |
1914 | goto posixa; | |
1915 | ||
1916 | case NPOSIXA: | |
1917 | if (utf8_target) { | |
1918 | /* The complement of something that matches only ASCII matches all | |
1919 | * UTF-8 variant code points, plus everything in ASCII that isn't | |
1920 | * in the class */ | |
1921 | REXEC_FBC_UTF8_CLASS_SCAN(! UTF8_IS_INVARIANT(*s) | |
1922 | || ! _generic_isCC_A(*s, FLAGS(c))); | |
1923 | break; | |
1924 | } | |
1925 | ||
1926 | to_complement = 1; | |
1927 | /* FALLTHROUGH */ | |
1928 | ||
73104a1b | 1929 | case POSIXA: |
3018b823 | 1930 | posixa: |
73104a1b | 1931 | /* Don't need to worry about utf8, as it can match only a single |
3018b823 KW |
1932 | * byte invariant character. */ |
1933 | REXEC_FBC_CLASS_SCAN( | |
1934 | to_complement ^ cBOOL(_generic_isCC_A(*s, FLAGS(c)))); | |
73104a1b | 1935 | break; |
3018b823 KW |
1936 | |
1937 | case NPOSIXU: | |
1938 | to_complement = 1; | |
1939 | /* FALLTHROUGH */ | |
1940 | ||
1941 | case POSIXU: | |
1942 | if (! utf8_target) { | |
1943 | REXEC_FBC_CLASS_SCAN(to_complement ^ cBOOL(_generic_isCC(*s, | |
1944 | FLAGS(c)))); | |
1945 | } | |
1946 | else { | |
1947 | ||
1948 | posix_utf8: | |
1949 | classnum = (_char_class_number) FLAGS(c); | |
1950 | if (classnum < _FIRST_NON_SWASH_CC) { | |
1951 | while (s < strend) { | |
1952 | ||
1953 | /* We avoid loading in the swash as long as possible, but | |
1954 | * should we have to, we jump to a separate loop. This | |
1955 | * extra 'if' statement is what keeps this code from being | |
1956 | * just a call to REXEC_FBC_UTF8_CLASS_SCAN() */ | |
1957 | if (UTF8_IS_ABOVE_LATIN1(*s)) { | |
1958 | goto found_above_latin1; | |
1959 | } | |
1960 | if ((UTF8_IS_INVARIANT(*s) | |
1961 | && to_complement ^ cBOOL(_generic_isCC((U8) *s, | |
1962 | classnum))) | |
1963 | || (UTF8_IS_DOWNGRADEABLE_START(*s) | |
1964 | && to_complement ^ cBOOL( | |
94bb8c36 KW |
1965 | _generic_isCC(TWO_BYTE_UTF8_TO_NATIVE(*s, |
1966 | *(s + 1)), | |
3018b823 KW |
1967 | classnum)))) |
1968 | { | |
02d5137b | 1969 | if (tmp && (reginfo->intuit || regtry(reginfo, &s))) |
3018b823 KW |
1970 | goto got_it; |
1971 | else { | |
1972 | tmp = doevery; | |
1973 | } | |
1974 | } | |
1975 | else { | |
1976 | tmp = 1; | |
1977 | } | |
1978 | s += UTF8SKIP(s); | |
1979 | } | |
1980 | } | |
1981 | else switch (classnum) { /* These classes are implemented as | |
1982 | macros */ | |
1983 | case _CC_ENUM_SPACE: /* XXX would require separate code if we | |
1984 | revert the change of \v matching this */ | |
1985 | /* FALL THROUGH */ | |
1986 | ||
1987 | case _CC_ENUM_PSXSPC: | |
1988 | REXEC_FBC_UTF8_CLASS_SCAN( | |
1989 | to_complement ^ cBOOL(isSPACE_utf8(s))); | |
1990 | break; | |
1991 | ||
1992 | case _CC_ENUM_BLANK: | |
1993 | REXEC_FBC_UTF8_CLASS_SCAN( | |
1994 | to_complement ^ cBOOL(isBLANK_utf8(s))); | |
1995 | break; | |
1996 | ||
1997 | case _CC_ENUM_XDIGIT: | |
1998 | REXEC_FBC_UTF8_CLASS_SCAN( | |
1999 | to_complement ^ cBOOL(isXDIGIT_utf8(s))); | |
2000 | break; | |
2001 | ||
2002 | case _CC_ENUM_VERTSPACE: | |
2003 | REXEC_FBC_UTF8_CLASS_SCAN( | |
2004 | to_complement ^ cBOOL(isVERTWS_utf8(s))); | |
2005 | break; | |
2006 | ||
2007 | case _CC_ENUM_CNTRL: | |
2008 | REXEC_FBC_UTF8_CLASS_SCAN( | |
2009 | to_complement ^ cBOOL(isCNTRL_utf8(s))); | |
2010 | break; | |
2011 | ||
2012 | default: | |
2013 | Perl_croak(aTHX_ "panic: find_byclass() node %d='%s' has an unexpected character class '%d'", OP(c), PL_reg_name[OP(c)], classnum); | |
2014 | assert(0); /* NOTREACHED */ | |
2015 | } | |
2016 | } | |
2017 | break; | |
2018 | ||
2019 | found_above_latin1: /* Here we have to load a swash to get the result | |
2020 | for the current code point */ | |
2021 | if (! PL_utf8_swash_ptrs[classnum]) { | |
2022 | U8 flags = _CORE_SWASH_INIT_ACCEPT_INVLIST; | |
2023 | PL_utf8_swash_ptrs[classnum] = | |
2a16ac92 KW |
2024 | _core_swash_init("utf8", |
2025 | "", | |
2026 | &PL_sv_undef, 1, 0, | |
2027 | PL_XPosix_ptrs[classnum], &flags); | |
3018b823 KW |
2028 | } |
2029 | ||
2030 | /* This is a copy of the loop above for swash classes, though using the | |
2031 | * FBC macro instead of being expanded out. Since we've loaded the | |
2032 | * swash, we don't have to check for that each time through the loop */ | |
2033 | REXEC_FBC_UTF8_CLASS_SCAN( | |
2034 | to_complement ^ cBOOL(_generic_utf8( | |
2035 | classnum, | |
2036 | s, | |
2037 | swash_fetch(PL_utf8_swash_ptrs[classnum], | |
2038 | (U8 *) s, TRUE)))); | |
73104a1b KW |
2039 | break; |
2040 | ||
2041 | case AHOCORASICKC: | |
2042 | case AHOCORASICK: | |
2043 | { | |
2044 | DECL_TRIE_TYPE(c); | |
2045 | /* what trie are we using right now */ | |
2046 | reg_ac_data *aho = (reg_ac_data*)progi->data->data[ ARG( c ) ]; | |
2047 | reg_trie_data *trie = (reg_trie_data*)progi->data->data[ aho->trie ]; | |
2048 | HV *widecharmap = MUTABLE_HV(progi->data->data[ aho->trie + 1 ]); | |
2049 | ||
2050 | const char *last_start = strend - trie->minlen; | |
6148ee25 | 2051 | #ifdef DEBUGGING |
73104a1b | 2052 | const char *real_start = s; |
6148ee25 | 2053 | #endif |
73104a1b KW |
2054 | STRLEN maxlen = trie->maxlen; |
2055 | SV *sv_points; | |
2056 | U8 **points; /* map of where we were in the input string | |
2057 | when reading a given char. For ASCII this | |
2058 | is unnecessary overhead as the relationship | |
2059 | is always 1:1, but for Unicode, especially | |
2060 | case folded Unicode this is not true. */ | |
2061 | U8 foldbuf[ UTF8_MAXBYTES_CASE + 1 ]; | |
2062 | U8 *bitmap=NULL; | |
2063 | ||
2064 | ||
2065 | GET_RE_DEBUG_FLAGS_DECL; | |
2066 | ||
2067 | /* We can't just allocate points here. We need to wrap it in | |
2068 | * an SV so it gets freed properly if there is a croak while | |
2069 | * running the match */ | |
2070 | ENTER; | |
2071 | SAVETMPS; | |
2072 | sv_points=newSV(maxlen * sizeof(U8 *)); | |
2073 | SvCUR_set(sv_points, | |
2074 | maxlen * sizeof(U8 *)); | |
2075 | SvPOK_on(sv_points); | |
2076 | sv_2mortal(sv_points); | |
2077 | points=(U8**)SvPV_nolen(sv_points ); | |
2078 | if ( trie_type != trie_utf8_fold | |
2079 | && (trie->bitmap || OP(c)==AHOCORASICKC) ) | |
2080 | { | |
2081 | if (trie->bitmap) | |
2082 | bitmap=(U8*)trie->bitmap; | |
2083 | else | |
2084 | bitmap=(U8*)ANYOF_BITMAP(c); | |
2085 | } | |
2086 | /* this is the Aho-Corasick algorithm modified a touch | |
2087 | to include special handling for long "unknown char" sequences. | |
2088 | The basic idea being that we use AC as long as we are dealing | |
2089 | with a possible matching char, when we encounter an unknown char | |
2090 | (and we have not encountered an accepting state) we scan forward | |
2091 | until we find a legal starting char. | |
2092 | AC matching is basically that of trie matching, except that when | |
2093 | we encounter a failing transition, we fall back to the current | |
2094 | states "fail state", and try the current char again, a process | |
2095 | we repeat until we reach the root state, state 1, or a legal | |
2096 | transition. If we fail on the root state then we can either | |
2097 | terminate if we have reached an accepting state previously, or | |
2098 | restart the entire process from the beginning if we have not. | |
2099 | ||
2100 | */ | |
2101 | while (s <= last_start) { | |
2102 | const U32 uniflags = UTF8_ALLOW_DEFAULT; | |
2103 | U8 *uc = (U8*)s; | |
2104 | U16 charid = 0; | |
2105 | U32 base = 1; | |
2106 | U32 state = 1; | |
2107 | UV uvc = 0; | |
2108 | STRLEN len = 0; | |
2109 | STRLEN foldlen = 0; | |
2110 | U8 *uscan = (U8*)NULL; | |
2111 | U8 *leftmost = NULL; | |
2112 | #ifdef DEBUGGING | |
2113 | U32 accepted_word= 0; | |
786e8c11 | 2114 | #endif |
73104a1b KW |
2115 | U32 pointpos = 0; |
2116 | ||
2117 | while ( state && uc <= (U8*)strend ) { | |
2118 | int failed=0; | |
2119 | U32 word = aho->states[ state ].wordnum; | |
2120 | ||
2121 | if( state==1 ) { | |
2122 | if ( bitmap ) { | |
2123 | DEBUG_TRIE_EXECUTE_r( | |
2124 | if ( uc <= (U8*)last_start && !BITMAP_TEST(bitmap,*uc) ) { | |
2125 | dump_exec_pos( (char *)uc, c, strend, real_start, | |
2126 | (char *)uc, utf8_target ); | |
2127 | PerlIO_printf( Perl_debug_log, | |
2128 | " Scanning for legal start char...\n"); | |
2129 | } | |
2130 | ); | |
2131 | if (utf8_target) { | |
2132 | while ( uc <= (U8*)last_start && !BITMAP_TEST(bitmap,*uc) ) { | |
2133 | uc += UTF8SKIP(uc); | |
2134 | } | |
2135 | } else { | |
2136 | while ( uc <= (U8*)last_start && !BITMAP_TEST(bitmap,*uc) ) { | |
2137 | uc++; | |
2138 | } | |
786e8c11 | 2139 | } |
73104a1b | 2140 | s= (char *)uc; |
07be1b83 | 2141 | } |
73104a1b KW |
2142 | if (uc >(U8*)last_start) break; |
2143 | } | |
2144 | ||
2145 | if ( word ) { | |
2146 | U8 *lpos= points[ (pointpos - trie->wordinfo[word].len) % maxlen ]; | |
2147 | if (!leftmost || lpos < leftmost) { | |
2148 | DEBUG_r(accepted_word=word); | |
2149 | leftmost= lpos; | |
7016d6eb | 2150 | } |
73104a1b | 2151 | if (base==0) break; |
7016d6eb | 2152 | |
73104a1b KW |
2153 | } |
2154 | points[pointpos++ % maxlen]= uc; | |
2155 | if (foldlen || uc < (U8*)strend) { | |
2156 | REXEC_TRIE_READ_CHAR(trie_type, trie, | |
2157 | widecharmap, uc, | |
2158 | uscan, len, uvc, charid, foldlen, | |
2159 | foldbuf, uniflags); | |
2160 | DEBUG_TRIE_EXECUTE_r({ | |
2161 | dump_exec_pos( (char *)uc, c, strend, | |
2162 | real_start, s, utf8_target); | |
2163 | PerlIO_printf(Perl_debug_log, | |
2164 | " Charid:%3u CP:%4"UVxf" ", | |
2165 | charid, uvc); | |
2166 | }); | |
2167 | } | |
2168 | else { | |
2169 | len = 0; | |
2170 | charid = 0; | |
2171 | } | |
07be1b83 | 2172 | |
73104a1b KW |
2173 | |
2174 | do { | |
6148ee25 | 2175 | #ifdef DEBUGGING |
73104a1b | 2176 | word = aho->states[ state ].wordnum; |
6148ee25 | 2177 | #endif |
73104a1b KW |
2178 | base = aho->states[ state ].trans.base; |
2179 | ||
2180 | DEBUG_TRIE_EXECUTE_r({ | |
2181 | if (failed) | |
2182 | dump_exec_pos( (char *)uc, c, strend, real_start, | |
2183 | s, utf8_target ); | |
2184 | PerlIO_printf( Perl_debug_log, | |
2185 | "%sState: %4"UVxf", word=%"UVxf, | |
2186 | failed ? " Fail transition to " : "", | |
2187 | (UV)state, (UV)word); | |
2188 | }); | |
2189 | if ( base ) { | |
2190 | U32 tmp; | |
2191 | I32 offset; | |
2192 | if (charid && | |
2193 | ( ((offset = base + charid | |
2194 | - 1 - trie->uniquecharcount)) >= 0) | |
2195 | && ((U32)offset < trie->lasttrans) | |
2196 | && trie->trans[offset].check == state | |
2197 | && (tmp=trie->trans[offset].next)) | |
2198 | { | |
2199 | DEBUG_TRIE_EXECUTE_r( | |
2200 | PerlIO_printf( Perl_debug_log," - legal\n")); | |
2201 | state = tmp; | |
2202 | break; | |
07be1b83 YO |
2203 | } |
2204 | else { | |
786e8c11 | 2205 | DEBUG_TRIE_EXECUTE_r( |
73104a1b | 2206 | PerlIO_printf( Perl_debug_log," - fail\n")); |
786e8c11 | 2207 | failed = 1; |
73104a1b | 2208 | state = aho->fail[state]; |
07be1b83 | 2209 | } |
07be1b83 | 2210 | } |
73104a1b KW |
2211 | else { |
2212 | /* we must be accepting here */ | |
2213 | DEBUG_TRIE_EXECUTE_r( | |
2214 | PerlIO_printf( Perl_debug_log," - accepting\n")); | |
2215 | failed = 1; | |
2216 | break; | |
786e8c11 | 2217 | } |
73104a1b KW |
2218 | } while(state); |
2219 | uc += len; | |
2220 | if (failed) { | |
2221 | if (leftmost) | |
2222 | break; | |
2223 | if (!state) state = 1; | |
07be1b83 | 2224 | } |
73104a1b KW |
2225 | } |
2226 | if ( aho->states[ state ].wordnum ) { | |
2227 | U8 *lpos = points[ (pointpos - trie->wordinfo[aho->states[ state ].wordnum].len) % maxlen ]; | |
2228 | if (!leftmost || lpos < leftmost) { | |
2229 | DEBUG_r(accepted_word=aho->states[ state ].wordnum); | |
2230 | leftmost = lpos; | |
07be1b83 YO |
2231 | } |
2232 | } | |
73104a1b KW |
2233 | if (leftmost) { |
2234 | s = (char*)leftmost; | |
2235 | DEBUG_TRIE_EXECUTE_r({ | |
2236 | PerlIO_printf( | |
2237 | Perl_debug_log,"Matches word #%"UVxf" at position %"IVdf". Trying full pattern...\n", | |
2238 | (UV)accepted_word, (IV)(s - real_start) | |
2239 | ); | |
2240 | }); | |
02d5137b | 2241 | if (reginfo->intuit || regtry(reginfo, &s)) { |
73104a1b KW |
2242 | FREETMPS; |
2243 | LEAVE; | |
2244 | goto got_it; | |
2245 | } | |
2246 | s = HOPc(s,1); | |
2247 | DEBUG_TRIE_EXECUTE_r({ | |
2248 | PerlIO_printf( Perl_debug_log,"Pattern failed. Looking for new start point...\n"); | |
2249 | }); | |
2250 | } else { | |
2251 | DEBUG_TRIE_EXECUTE_r( | |
2252 | PerlIO_printf( Perl_debug_log,"No match.\n")); | |
2253 | break; | |
2254 | } | |
2255 | } | |
2256 | FREETMPS; | |
2257 | LEAVE; | |
2258 | } | |
2259 | break; | |
2260 | default: | |
2261 | Perl_croak(aTHX_ "panic: unknown regstclass %d", (int)OP(c)); | |
2262 | break; | |
2263 | } | |
2264 | return 0; | |
2265 | got_it: | |
2266 | return s; | |
6eb5f6b9 JH |
2267 | } |
2268 | ||
60165aa4 DM |
2269 | /* set RX_SAVED_COPY, RX_SUBBEG etc. |
2270 | * flags have same meanings as with regexec_flags() */ | |
2271 | ||
749f4950 DM |
2272 | static void |
2273 | S_reg_set_capture_string(pTHX_ REGEXP * const rx, | |
60165aa4 DM |
2274 | char *strbeg, |
2275 | char *strend, | |
2276 | SV *sv, | |
2277 | U32 flags, | |
2278 | bool utf8_target) | |
2279 | { | |
2280 | struct regexp *const prog = ReANY(rx); | |
2281 | ||
60165aa4 DM |
2282 | if (flags & REXEC_COPY_STR) { |
2283 | #ifdef PERL_ANY_COW | |
2284 | if (SvCANCOW(sv)) { | |
2285 | if (DEBUG_C_TEST) { | |
2286 | PerlIO_printf(Perl_debug_log, | |
2287 | "Copy on write: regexp capture, type %d\n", | |
2288 | (int) SvTYPE(sv)); | |
2289 | } | |
5411a0e5 DM |
2290 | /* Create a new COW SV to share the match string and store |
2291 | * in saved_copy, unless the current COW SV in saved_copy | |
2292 | * is valid and suitable for our purpose */ | |
2293 | if (( prog->saved_copy | |
2294 | && SvIsCOW(prog->saved_copy) | |
2295 | && SvPOKp(prog->saved_copy) | |
2296 | && SvIsCOW(sv) | |
2297 | && SvPOKp(sv) | |
2298 | && SvPVX(sv) == SvPVX(prog->saved_copy))) | |
a76b0e90 | 2299 | { |
5411a0e5 DM |
2300 | /* just reuse saved_copy SV */ |
2301 | if (RXp_MATCH_COPIED(prog)) { | |
2302 | Safefree(prog->subbeg); | |
2303 | RXp_MATCH_COPIED_off(prog); | |
2304 | } | |
2305 | } | |
2306 | else { | |
2307 | /* create new COW SV to share string */ | |
a76b0e90 DM |
2308 | RX_MATCH_COPY_FREE(rx); |
2309 | prog->saved_copy = sv_setsv_cow(prog->saved_copy, sv); | |
a76b0e90 | 2310 | } |
5411a0e5 DM |
2311 | prog->subbeg = (char *)SvPVX_const(prog->saved_copy); |
2312 | assert (SvPOKp(prog->saved_copy)); | |
60165aa4 DM |
2313 | prog->sublen = strend - strbeg; |
2314 | prog->suboffset = 0; | |
2315 | prog->subcoffset = 0; | |
2316 | } else | |
2317 | #endif | |
2318 | { | |
99a90e59 FC |
2319 | SSize_t min = 0; |
2320 | SSize_t max = strend - strbeg; | |
ea3daa5d | 2321 | SSize_t sublen; |
60165aa4 DM |
2322 | |
2323 | if ( (flags & REXEC_COPY_SKIP_POST) | |
e322109a | 2324 | && !(prog->extflags & RXf_PMf_KEEPCOPY) /* //p */ |
60165aa4 DM |
2325 | && !(PL_sawampersand & SAWAMPERSAND_RIGHT) |
2326 | ) { /* don't copy $' part of string */ | |
2327 | U32 n = 0; | |
2328 | max = -1; | |
2329 | /* calculate the right-most part of the string covered | |
2330 | * by a capture. Due to look-ahead, this may be to | |
2331 | * the right of $&, so we have to scan all captures */ | |
2332 | while (n <= prog->lastparen) { | |
2333 | if (prog->offs[n].end > max) | |
2334 | max = prog->offs[n].end; | |
2335 | n++; | |
2336 | } | |
2337 | if (max == -1) | |
2338 | max = (PL_sawampersand & SAWAMPERSAND_LEFT) | |
2339 | ? prog->offs[0].start | |
2340 | : 0; | |
2341 | assert(max >= 0 && max <= strend - strbeg); | |
2342 | } | |
2343 | ||
2344 | if ( (flags & REXEC_COPY_SKIP_PRE) | |
e322109a | 2345 | && !(prog->extflags & RXf_PMf_KEEPCOPY) /* //p */ |
60165aa4 DM |
2346 | && !(PL_sawampersand & SAWAMPERSAND_LEFT) |
2347 | ) { /* don't copy $` part of string */ | |
2348 | U32 n = 0; | |
2349 | min = max; | |
2350 | /* calculate the left-most part of the string covered | |
2351 | * by a capture. Due to look-behind, this may be to | |
2352 | * the left of $&, so we have to scan all captures */ | |
2353 | while (min && n <= prog->lastparen) { | |
2354 | if ( prog->offs[n].start != -1 | |
2355 | && prog->offs[n].start < min) | |
2356 | { | |
2357 | min = prog->offs[n].start; | |
2358 | } | |
2359 | n++; | |
2360 | } | |
2361 | if ((PL_sawampersand & SAWAMPERSAND_RIGHT) | |
2362 | && min > prog->offs[0].end | |
2363 | ) | |
2364 | min = prog->offs[0].end; | |
2365 | ||
2366 | } | |
2367 | ||
2368 | assert(min >= 0 && min <= max && min <= strend - strbeg); | |
2369 | sublen = max - min; | |
2370 | ||
2371 | if (RX_MATCH_COPIED(rx)) { | |
2372 | if (sublen > prog->sublen) | |
2373 | prog->subbeg = | |
2374 | (char*)saferealloc(prog->subbeg, sublen+1); | |
2375 | } | |
2376 | else | |
2377 | prog->subbeg = (char*)safemalloc(sublen+1); | |
2378 | Copy(strbeg + min, prog->subbeg, sublen, char); | |
2379 | prog->subbeg[sublen] = '\0'; | |
2380 | prog->suboffset = min; | |
2381 | prog->sublen = sublen; | |
2382 | RX_MATCH_COPIED_on(rx); | |
2383 | } | |
2384 | prog->subcoffset = prog->suboffset; | |
2385 | if (prog->suboffset && utf8_target) { | |
2386 | /* Convert byte offset to chars. | |
2387 | * XXX ideally should only compute this if @-/@+ | |
2388 | * has been seen, a la PL_sawampersand ??? */ | |
2389 | ||
2390 | /* If there's a direct correspondence between the | |
2391 | * string which we're matching and the original SV, | |
2392 | * then we can use the utf8 len cache associated with | |
2393 | * the SV. In particular, it means that under //g, | |
2394 | * sv_pos_b2u() will use the previously cached | |
2395 | * position to speed up working out the new length of | |
2396 | * subcoffset, rather than counting from the start of | |
2397 | * the string each time. This stops | |
2398 | * $x = "\x{100}" x 1E6; 1 while $x =~ /(.)/g; | |
2399 | * from going quadratic */ | |
2400 | if (SvPOKp(sv) && SvPVX(sv) == strbeg) | |
ea3daa5d FC |
2401 | prog->subcoffset = sv_pos_b2u_flags(sv, prog->subcoffset, |
2402 | SV_GMAGIC|SV_CONST_RETURN); | |
60165aa4 DM |
2403 | else |
2404 | prog->subcoffset = utf8_length((U8*)strbeg, | |
2405 | (U8*)(strbeg+prog->suboffset)); | |
2406 | } | |
2407 | } | |
2408 | else { | |
2409 | RX_MATCH_COPY_FREE(rx); | |
2410 | prog->subbeg = strbeg; | |
2411 | prog->suboffset = 0; | |
2412 | prog->subcoffset = 0; | |
2413 | prog->sublen = strend - strbeg; | |
2414 | } | |
2415 | } | |
2416 | ||
2417 | ||
2418 | ||
fae667d5 | 2419 | |
6eb5f6b9 JH |
2420 | /* |
2421 | - regexec_flags - match a regexp against a string | |
2422 | */ | |
2423 | I32 | |
5aaab254 | 2424 | Perl_regexec_flags(pTHX_ REGEXP * const rx, char *stringarg, char *strend, |
ea3daa5d | 2425 | char *strbeg, SSize_t minend, SV *sv, void *data, U32 flags) |
8fd1a950 DM |
2426 | /* stringarg: the point in the string at which to begin matching */ |
2427 | /* strend: pointer to null at end of string */ | |
2428 | /* strbeg: real beginning of string */ | |
2429 | /* minend: end of match must be >= minend bytes after stringarg. */ | |
2430 | /* sv: SV being matched: only used for utf8 flag, pos() etc; string | |
2431 | * itself is accessed via the pointers above */ | |
2432 | /* data: May be used for some additional optimizations. | |
d058ec57 | 2433 | Currently unused. */ |
a340edde | 2434 | /* flags: For optimizations. See REXEC_* in regexp.h */ |
8fd1a950 | 2435 | |
6eb5f6b9 | 2436 | { |
97aff369 | 2437 | dVAR; |
8d919b0a | 2438 | struct regexp *const prog = ReANY(rx); |
5aaab254 | 2439 | char *s; |
eb578fdb | 2440 | regnode *c; |
03c83e26 | 2441 | char *startpos; |
ea3daa5d FC |
2442 | SSize_t minlen; /* must match at least this many chars */ |
2443 | SSize_t dontbother = 0; /* how many characters not to try at end */ | |
f2ed9b32 | 2444 | const bool utf8_target = cBOOL(DO_UTF8(sv)); |
2757e526 | 2445 | I32 multiline; |
f8fc2ecf | 2446 | RXi_GET_DECL(prog,progi); |
02d5137b DM |
2447 | regmatch_info reginfo_buf; /* create some info to pass to regtry etc */ |
2448 | regmatch_info *const reginfo = ®info_buf; | |
e9105d30 | 2449 | regexp_paren_pair *swap = NULL; |
006f26b2 | 2450 | I32 oldsave; |
a3621e74 YO |
2451 | GET_RE_DEBUG_FLAGS_DECL; |
2452 | ||
7918f24d | 2453 | PERL_ARGS_ASSERT_REGEXEC_FLAGS; |
9d4ba2ae | 2454 | PERL_UNUSED_ARG(data); |
6eb5f6b9 JH |
2455 | |
2456 | /* Be paranoid... */ | |
03c83e26 | 2457 | if (prog == NULL || stringarg == NULL) { |
6eb5f6b9 JH |
2458 | Perl_croak(aTHX_ "NULL regexp parameter"); |
2459 | return 0; | |
2460 | } | |
2461 | ||
6c3fea77 | 2462 | DEBUG_EXECUTE_r( |
03c83e26 | 2463 | debug_start_match(rx, utf8_target, stringarg, strend, |
6c3fea77 DM |
2464 | "Matching"); |
2465 | ); | |
8adc0f72 | 2466 | |
b342a604 DM |
2467 | startpos = stringarg; |
2468 | ||
58430ea8 | 2469 | if (prog->intflags & PREGf_GPOS_SEEN) { |
d307c076 DM |
2470 | MAGIC *mg; |
2471 | ||
fef7148b DM |
2472 | /* set reginfo->ganch, the position where \G can match */ |
2473 | ||
2474 | reginfo->ganch = | |
2475 | (flags & REXEC_IGNOREPOS) | |
2476 | ? stringarg /* use start pos rather than pos() */ | |
2477 | : (sv && (mg = mg_find_mglob(sv)) && mg->mg_len >= 0) | |
25fdce4a FC |
2478 | /* Defined pos(): */ |
2479 | ? strbeg + MgBYTEPOS(mg, sv, strbeg, strend-strbeg) | |
fef7148b DM |
2480 | : strbeg; /* pos() not defined; use start of string */ |
2481 | ||
2482 | DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log, | |
7b0eb0b8 | 2483 | "GPOS ganch set to strbeg[%"IVdf"]\n", (IV)(reginfo->ganch - strbeg))); |
fef7148b | 2484 | |
03c83e26 DM |
2485 | /* in the presence of \G, we may need to start looking earlier in |
2486 | * the string than the suggested start point of stringarg: | |
0b2c2a84 | 2487 | * if prog->gofs is set, then that's a known, fixed minimum |
03c83e26 DM |
2488 | * offset, such as |
2489 | * /..\G/: gofs = 2 | |
2490 | * /ab|c\G/: gofs = 1 | |
2491 | * or if the minimum offset isn't known, then we have to go back | |
2492 | * to the start of the string, e.g. /w+\G/ | |
2493 | */ | |
2bfbe302 | 2494 | |
8e1490ee | 2495 | if (prog->intflags & PREGf_ANCH_GPOS) { |
2bfbe302 DM |
2496 | startpos = reginfo->ganch - prog->gofs; |
2497 | if (startpos < | |
2498 | ((flags & REXEC_FAIL_ON_UNDERFLOW) ? stringarg : strbeg)) | |
2499 | { | |
2500 | DEBUG_r(PerlIO_printf(Perl_debug_log, | |
2501 | "fail: ganch-gofs before earliest possible start\n")); | |
2502 | return 0; | |
2503 | } | |
2504 | } | |
2505 | else if (prog->gofs) { | |
b342a604 DM |
2506 | if (startpos - prog->gofs < strbeg) |
2507 | startpos = strbeg; | |
2508 | else | |
2509 | startpos -= prog->gofs; | |
03c83e26 | 2510 | } |
58430ea8 | 2511 | else if (prog->intflags & PREGf_GPOS_FLOAT) |
b342a604 | 2512 | startpos = strbeg; |
03c83e26 DM |
2513 | } |
2514 | ||
2515 | minlen = prog->minlen; | |
b342a604 | 2516 | if ((startpos + minlen) > strend || startpos < strbeg) { |
03c83e26 DM |
2517 | DEBUG_r(PerlIO_printf(Perl_debug_log, |
2518 | "Regex match can't succeed, so not even tried\n")); | |
2519 | return 0; | |
2520 | } | |
2521 | ||
63a3746a DM |
2522 | /* at the end of this function, we'll do a LEAVE_SCOPE(oldsave), |
2523 | * which will call destuctors to reset PL_regmatch_state, free higher | |
2524 | * PL_regmatch_slabs, and clean up regmatch_info_aux and | |
2525 | * regmatch_info_aux_eval */ | |
2526 | ||
2527 | oldsave = PL_savestack_ix; | |
2528 | ||
dfa77d06 DM |
2529 | s = startpos; |
2530 | ||
e322109a | 2531 | if ((prog->extflags & RXf_USE_INTUIT) |
7fadf4a7 DM |
2532 | && !(flags & REXEC_CHECKED)) |
2533 | { | |
dfa77d06 | 2534 | s = re_intuit_start(rx, sv, strbeg, startpos, strend, |
7fadf4a7 | 2535 | flags, NULL); |
dfa77d06 | 2536 | if (!s) |
7fadf4a7 DM |
2537 | return 0; |
2538 | ||
e322109a | 2539 | if (prog->extflags & RXf_CHECK_ALL) { |
7fadf4a7 DM |
2540 | /* we can match based purely on the result of INTUIT. |
2541 | * Set up captures etc just for $& and $-[0] | |
2542 | * (an intuit-only match wont have $1,$2,..) */ | |
2543 | assert(!prog->nparens); | |
d5e7783a DM |
2544 | |
2545 | /* s/// doesn't like it if $& is earlier than where we asked it to | |
2546 | * start searching (which can happen on something like /.\G/) */ | |
2547 | if ( (flags & REXEC_FAIL_ON_UNDERFLOW) | |
2548 | && (s < stringarg)) | |
2549 | { | |
2550 | /* this should only be possible under \G */ | |
58430ea8 | 2551 | assert(prog->intflags & PREGf_GPOS_SEEN); |
d5e7783a DM |
2552 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, |
2553 | "matched, but failing for REXEC_FAIL_ON_UNDERFLOW\n")); | |
2554 | goto phooey; | |
2555 | } | |
2556 | ||
7fadf4a7 DM |
2557 | /* match via INTUIT shouldn't have any captures. |
2558 | * Let @-, @+, $^N know */ | |
2559 | prog->lastparen = prog->lastcloseparen = 0; | |
2560 | RX_MATCH_UTF8_set(rx, utf8_target); | |
3ff69bd6 DM |
2561 | prog->offs[0].start = s - strbeg; |
2562 | prog->offs[0].end = utf8_target | |
2563 | ? (char*)utf8_hop((U8*)s, prog->minlenret) - strbeg | |
2564 | : s - strbeg + prog->minlenret; | |
7fadf4a7 | 2565 | if ( !(flags & REXEC_NOT_FIRST) ) |
749f4950 | 2566 | S_reg_set_capture_string(aTHX_ rx, |
7fadf4a7 DM |
2567 | strbeg, strend, |
2568 | sv, flags, utf8_target); | |
2569 | ||
7fadf4a7 DM |
2570 | return 1; |
2571 | } | |
2572 | } | |
2573 | ||
6c3fea77 | 2574 | multiline = prog->extflags & RXf_PMf_MULTILINE; |
1de06328 | 2575 | |
dfa77d06 | 2576 | if (strend - s < (minlen+(prog->check_offset_min<0?prog->check_offset_min:0))) { |
a3621e74 | 2577 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, |
a72c7584 JH |
2578 | "String too short [regexec_flags]...\n")); |
2579 | goto phooey; | |
1aa99e6b | 2580 | } |
1de06328 | 2581 | |
6eb5f6b9 | 2582 | /* Check validity of program. */ |
f8fc2ecf | 2583 | if (UCHARAT(progi->program) != REG_MAGIC) { |
6eb5f6b9 JH |
2584 | Perl_croak(aTHX_ "corrupted regexp program"); |
2585 | } | |
2586 | ||
6c3fea77 DM |
2587 | reginfo->prog = rx; /* Yes, sorry that this is confusing. */ |
2588 | reginfo->intuit = 0; | |
2589 | reginfo->is_utf8_target = cBOOL(utf8_target); | |
02d5137b DM |
2590 | reginfo->is_utf8_pat = cBOOL(RX_UTF8(rx)); |
2591 | reginfo->warned = FALSE; | |
9d9163fb | 2592 | reginfo->strbeg = strbeg; |
02d5137b | 2593 | reginfo->sv = sv; |
1cb48e53 | 2594 | reginfo->poscache_maxiter = 0; /* not yet started a countdown */ |
220db18a | 2595 | reginfo->strend = strend; |
6eb5f6b9 | 2596 | /* see how far we have to get to not match where we matched before */ |
fe3974be | 2597 | reginfo->till = stringarg + minend; |
6eb5f6b9 | 2598 | |
60779a30 | 2599 | if (prog->extflags & RXf_EVAL_SEEN && SvPADTMP(sv)) { |
82c23608 FC |
2600 | /* SAVEFREESV, not sv_mortalcopy, as this SV must last until after |
2601 | S_cleanup_regmatch_info_aux has executed (registered by | |
2602 | SAVEDESTRUCTOR_X below). S_cleanup_regmatch_info_aux modifies | |
2603 | magic belonging to this SV. | |
2604 | Not newSVsv, either, as it does not COW. | |
2605 | */ | |
60779a30 | 2606 | assert(!IS_PADGV(sv)); |
82c23608 | 2607 | reginfo->sv = newSV(0); |
4cba5ac0 | 2608 | SvSetSV_nosteal(reginfo->sv, sv); |
82c23608 FC |
2609 | SAVEFREESV(reginfo->sv); |
2610 | } | |
2611 | ||
331b2dcc DM |
2612 | /* reserve next 2 or 3 slots in PL_regmatch_state: |
2613 | * slot N+0: may currently be in use: skip it | |
2614 | * slot N+1: use for regmatch_info_aux struct | |
2615 | * slot N+2: use for regmatch_info_aux_eval struct if we have (?{})'s | |
2616 | * slot N+3: ready for use by regmatch() | |
2617 | */ | |
bf2039a9 | 2618 | |
331b2dcc DM |
2619 | { |
2620 | regmatch_state *old_regmatch_state; | |
2621 | regmatch_slab *old_regmatch_slab; | |
2622 | int i, max = (prog->extflags & RXf_EVAL_SEEN) ? 2 : 1; | |
2623 | ||
2624 | /* on first ever match, allocate first slab */ | |
2625 | if (!PL_regmatch_slab) { | |
2626 | Newx(PL_regmatch_slab, 1, regmatch_slab); | |
2627 | PL_regmatch_slab->prev = NULL; | |
2628 | PL_regmatch_slab->next = NULL; | |
2629 | PL_regmatch_state = SLAB_FIRST(PL_regmatch_slab); | |
2630 | } | |
bf2039a9 | 2631 | |
331b2dcc DM |
2632 | old_regmatch_state = PL_regmatch_state; |
2633 | old_regmatch_slab = PL_regmatch_slab; | |
bf2039a9 | 2634 | |
331b2dcc DM |
2635 | for (i=0; i <= max; i++) { |
2636 | if (i == 1) | |
2637 | reginfo->info_aux = &(PL_regmatch_state->u.info_aux); | |
2638 | else if (i ==2) | |
2639 | reginfo->info_aux_eval = | |
2640 | reginfo->info_aux->info_aux_eval = | |
2641 | &(PL_regmatch_state->u.info_aux_eval); | |
bf2039a9 | 2642 | |
331b2dcc DM |
2643 | if (++PL_regmatch_state > SLAB_LAST(PL_regmatch_slab)) |
2644 | PL_regmatch_state = S_push_slab(aTHX); | |
2645 | } | |
bf2039a9 | 2646 | |
331b2dcc DM |
2647 | /* note initial PL_regmatch_state position; at end of match we'll |
2648 | * pop back to there and free any higher slabs */ | |
bf2039a9 | 2649 | |
331b2dcc DM |
2650 | reginfo->info_aux->old_regmatch_state = old_regmatch_state; |
2651 | reginfo->info_aux->old_regmatch_slab = old_regmatch_slab; | |
2ac8ff4b | 2652 | reginfo->info_aux->poscache = NULL; |
bf2039a9 | 2653 | |
331b2dcc | 2654 | SAVEDESTRUCTOR_X(S_cleanup_regmatch_info_aux, reginfo->info_aux); |
bf2039a9 | 2655 | |
331b2dcc DM |
2656 | if ((prog->extflags & RXf_EVAL_SEEN)) |
2657 | S_setup_eval_state(aTHX_ reginfo); | |
2658 | else | |
2659 | reginfo->info_aux_eval = reginfo->info_aux->info_aux_eval = NULL; | |
bf2039a9 | 2660 | } |
d3aa529c | 2661 | |
6eb5f6b9 | 2662 | /* If there is a "must appear" string, look for it. */ |
6eb5f6b9 | 2663 | |
288b8c02 | 2664 | if (PL_curpm && (PM_GETRE(PL_curpm) == rx)) { |
e9105d30 GG |
2665 | /* We have to be careful. If the previous successful match |
2666 | was from this regex we don't want a subsequent partially | |
2667 | successful match to clobber the old results. | |
2668 | So when we detect this possibility we add a swap buffer | |
d8da0584 KW |
2669 | to the re, and switch the buffer each match. If we fail, |
2670 | we switch it back; otherwise we leave it swapped. | |
e9105d30 GG |
2671 | */ |
2672 | swap = prog->offs; | |
2673 | /* do we need a save destructor here for eval dies? */ | |
2674 | Newxz(prog->offs, (prog->nparens + 1), regexp_paren_pair); | |
495f47a5 DM |
2675 | DEBUG_BUFFERS_r(PerlIO_printf(Perl_debug_log, |
2676 | "rex=0x%"UVxf" saving offs: orig=0x%"UVxf" new=0x%"UVxf"\n", | |
2677 | PTR2UV(prog), | |
2678 | PTR2UV(swap), | |
2679 | PTR2UV(prog->offs) | |
2680 | )); | |
c74340f9 | 2681 | } |
6eb5f6b9 JH |
2682 | |
2683 | /* Simplest case: anchored match need be tried only once. */ | |
2684 | /* [unless only anchor is BOL and multiline is set] */ | |
8e1490ee | 2685 | if (prog->intflags & (PREGf_ANCH & ~PREGf_ANCH_GPOS)) { |
3542935d | 2686 | if (s == startpos && regtry(reginfo, &s)) |
6eb5f6b9 | 2687 | goto got_it; |
8e1490ee | 2688 | else if (multiline || (prog->intflags & (PREGf_IMPLICIT | PREGf_ANCH_MBOL))) /* XXXX SBOL? */ |
6eb5f6b9 JH |
2689 | { |
2690 | char *end; | |
2691 | ||
2692 | if (minlen) | |
2693 | dontbother = minlen - 1; | |
1aa99e6b | 2694 | end = HOP3c(strend, -dontbother, strbeg) - 1; |
6eb5f6b9 | 2695 | /* for multiline we only have to try after newlines */ |
33b8afdf | 2696 | if (prog->check_substr || prog->check_utf8) { |
92f3d482 YO |
2697 | /* because of the goto we can not easily reuse the macros for bifurcating the |
2698 | unicode/non-unicode match modes here like we do elsewhere - demerphq */ | |
2699 | if (utf8_target) { | |
2700 | if (s == startpos) | |
2701 | goto after_try_utf8; | |
2702 | while (1) { | |
02d5137b | 2703 | if (regtry(reginfo, &s)) { |
92f3d482 YO |
2704 | goto got_it; |
2705 | } | |
2706 | after_try_utf8: | |
2707 | if (s > end) { | |
2708 | goto phooey; | |
2709 | } | |
2710 | if (prog->extflags & RXf_USE_INTUIT) { | |
52a21eb3 DM |
2711 | s = re_intuit_start(rx, sv, strbeg, |
2712 | s + UTF8SKIP(s), strend, flags, NULL); | |
92f3d482 YO |
2713 | if (!s) { |
2714 | goto phooey; | |
2715 | } | |
2716 | } | |
2717 | else { | |
2718 | s += UTF8SKIP(s); | |
2719 | } | |
2720 | } | |
2721 | } /* end search for check string in unicode */ | |
2722 | else { | |
2723 | if (s == startpos) { | |
2724 | goto after_try_latin; | |
2725 | } | |
2726 | while (1) { | |
02d5137b | 2727 | if (regtry(reginfo, &s)) { |
92f3d482 YO |
2728 | goto got_it; |
2729 | } | |
2730 | after_try_latin: | |
2731 | if (s > end) { | |
2732 | goto phooey; | |
2733 | } | |
2734 | if (prog->extflags & RXf_USE_INTUIT) { | |
52a21eb3 DM |
2735 | s = re_intuit_start(rx, sv, strbeg, |
2736 | s + 1, strend, flags, NULL); | |
92f3d482 YO |
2737 | if (!s) { |
2738 | goto phooey; | |
2739 | } | |
2740 | } | |
2741 | else { | |
2742 | s++; | |
2743 | } | |
2744 | } | |
2745 | } /* end search for check string in latin*/ | |
2746 | } /* end search for check string */ | |
2747 | else { /* search for newline */ | |
2748 | if (s > startpos) { | |
2749 | /*XXX: The s-- is almost definitely wrong here under unicode - demeprhq*/ | |
6eb5f6b9 | 2750 | s--; |
92f3d482 | 2751 | } |
21eede78 YO |
2752 | /* We can use a more efficient search as newlines are the same in unicode as they are in latin */ |
2753 | while (s <= end) { /* note it could be possible to match at the end of the string */ | |
6eb5f6b9 | 2754 | if (*s++ == '\n') { /* don't need PL_utf8skip here */ |
02d5137b | 2755 | if (regtry(reginfo, &s)) |
6eb5f6b9 JH |
2756 | goto got_it; |
2757 | } | |
92f3d482 YO |
2758 | } |
2759 | } /* end search for newline */ | |
2760 | } /* end anchored/multiline check string search */ | |
6eb5f6b9 | 2761 | goto phooey; |
a8430a8b | 2762 | } else if (prog->intflags & PREGf_ANCH_GPOS) |
f9f4320a | 2763 | { |
a8430a8b YO |
2764 | /* PREGf_ANCH_GPOS should never be true if PREGf_GPOS_SEEN is not true */ |
2765 | assert(prog->intflags & PREGf_GPOS_SEEN); | |
2bfbe302 DM |
2766 | /* For anchored \G, the only position it can match from is |
2767 | * (ganch-gofs); we already set startpos to this above; if intuit | |
2768 | * moved us on from there, we can't possibly succeed */ | |
2769 | assert(startpos == reginfo->ganch - prog->gofs); | |
2770 | if (s == startpos && regtry(reginfo, &s)) | |
6eb5f6b9 JH |
2771 | goto got_it; |
2772 | goto phooey; | |
2773 | } | |
2774 | ||
2775 | /* Messy cases: unanchored match. */ | |
bbe252da | 2776 | if ((prog->anchored_substr || prog->anchored_utf8) && prog->intflags & PREGf_SKIP) { |
6eb5f6b9 | 2777 | /* we have /x+whatever/ */ |
984e6dd1 | 2778 | /* it must be a one character string (XXXX Except is_utf8_pat?) */ |
33b8afdf | 2779 | char ch; |
bf93d4cc GS |
2780 | #ifdef DEBUGGING |
2781 | int did_match = 0; | |
2782 | #endif | |
f2ed9b32 | 2783 | if (utf8_target) { |
7e0d5ad7 KW |
2784 | if (! prog->anchored_utf8) { |
2785 | to_utf8_substr(prog); | |
2786 | } | |
2787 | ch = SvPVX_const(prog->anchored_utf8)[0]; | |
4cadc6a9 | 2788 | REXEC_FBC_SCAN( |
6eb5f6b9 | 2789 | if (*s == ch) { |
a3621e74 | 2790 | DEBUG_EXECUTE_r( did_match = 1 ); |
02d5137b | 2791 | if (regtry(reginfo, &s)) goto got_it; |
6eb5f6b9 JH |
2792 | s += UTF8SKIP(s); |
2793 | while (s < strend && *s == ch) | |
2794 | s += UTF8SKIP(s); | |
2795 | } | |
4cadc6a9 | 2796 | ); |
7e0d5ad7 | 2797 | |
6eb5f6b9 JH |
2798 | } |
2799 | else { | |
7e0d5ad7 KW |
2800 | if (! prog->anchored_substr) { |
2801 | if (! to_byte_substr(prog)) { | |
6b54ddc5 | 2802 | NON_UTF8_TARGET_BUT_UTF8_REQUIRED(phooey); |
7e0d5ad7 KW |
2803 | } |
2804 | } | |
2805 | ch = SvPVX_const(prog->anchored_substr)[0]; | |
4cadc6a9 | 2806 | REXEC_FBC_SCAN( |
6eb5f6b9 | 2807 | if (*s == ch) { |
a3621e74 | 2808 | DEBUG_EXECUTE_r( did_match = 1 ); |
02d5137b | 2809 | if (regtry(reginfo, &s)) goto got_it; |
6eb5f6b9 JH |
2810 | s++; |
2811 | while (s < strend && *s == ch) | |
2812 | s++; | |
2813 | } | |
4cadc6a9 | 2814 | ); |
6eb5f6b9 | 2815 | } |
a3621e74 | 2816 | DEBUG_EXECUTE_r(if (!did_match) |
bf93d4cc | 2817 | PerlIO_printf(Perl_debug_log, |
b7953727 JH |
2818 | "Did not find anchored character...\n") |
2819 | ); | |
6eb5f6b9 | 2820 | } |
a0714e2c SS |
2821 | else if (prog->anchored_substr != NULL |
2822 | || prog->anchored_utf8 != NULL | |
2823 | || ((prog->float_substr != NULL || prog->float_utf8 != NULL) | |
33b8afdf JH |
2824 | && prog->float_max_offset < strend - s)) { |
2825 | SV *must; | |
ea3daa5d FC |
2826 | SSize_t back_max; |
2827 | SSize_t back_min; | |
33b8afdf | 2828 | char *last; |
6eb5f6b9 | 2829 | char *last1; /* Last position checked before */ |
bf93d4cc GS |
2830 | #ifdef DEBUGGING |
2831 | int did_match = 0; | |
2832 | #endif | |
33b8afdf | 2833 | if (prog->anchored_substr || prog->anchored_utf8) { |
7e0d5ad7 KW |
2834 | if (utf8_target) { |
2835 | if (! prog->anchored_utf8) { | |
2836 | to_utf8_substr(prog); | |
2837 | } | |
2838 | must = prog->anchored_utf8; | |
2839 | } | |
2840 | else { | |
2841 | if (! prog->anchored_substr) { | |
2842 | if (! to_byte_substr(prog)) { | |
6b54ddc5 | 2843 | NON_UTF8_TARGET_BUT_UTF8_REQUIRED(phooey); |
7e0d5ad7 KW |
2844 | } |
2845 | } | |
2846 | must = prog->anchored_substr; | |
2847 | } | |
33b8afdf JH |
2848 | back_max = back_min = prog->anchored_offset; |
2849 | } else { | |
7e0d5ad7 KW |
2850 | if (utf8_target) { |
2851 | if (! prog->float_utf8) { | |
2852 | to_utf8_substr(prog); | |
2853 | } | |
2854 | must = prog->float_utf8; | |
2855 | } | |
2856 | else { | |
2857 | if (! prog->float_substr) { | |
2858 | if (! to_byte_substr(prog)) { | |
6b54ddc5 | 2859 | NON_UTF8_TARGET_BUT_UTF8_REQUIRED(phooey); |
7e0d5ad7 KW |
2860 | } |
2861 | } | |
2862 | must = prog->float_substr; | |
2863 | } | |
33b8afdf JH |
2864 | back_max = prog->float_max_offset; |
2865 | back_min = prog->float_min_offset; | |
2866 | } | |
1de06328 | 2867 | |
1de06328 YO |
2868 | if (back_min<0) { |
2869 | last = strend; | |
2870 | } else { | |
2871 | last = HOP3c(strend, /* Cannot start after this */ | |
ea3daa5d | 2872 | -(SSize_t)(CHR_SVLEN(must) |
1de06328 YO |
2873 | - (SvTAIL(must) != 0) + back_min), strbeg); |
2874 | } | |
9d9163fb | 2875 | if (s > reginfo->strbeg) |
6eb5f6b9 JH |
2876 | last1 = HOPc(s, -1); |
2877 | else | |
2878 | last1 = s - 1; /* bogus */ | |
2879 | ||
a0288114 | 2880 | /* XXXX check_substr already used to find "s", can optimize if |
6eb5f6b9 | 2881 | check_substr==must. */ |
bf05793b | 2882 | dontbother = 0; |
6eb5f6b9 JH |
2883 | strend = HOPc(strend, -dontbother); |
2884 | while ( (s <= last) && | |
e50d57d4 | 2885 | (s = fbm_instr((unsigned char*)HOP4c(s, back_min, strbeg, strend), |
9041c2e3 | 2886 | (unsigned char*)strend, must, |
c33e64f0 | 2887 | multiline ? FBMrf_MULTILINE : 0)) ) { |
a3621e74 | 2888 | DEBUG_EXECUTE_r( did_match = 1 ); |
6eb5f6b9 JH |
2889 | if (HOPc(s, -back_max) > last1) { |
2890 | last1 = HOPc(s, -back_min); | |
2891 | s = HOPc(s, -back_max); | |
2892 | } | |
2893 | else { | |
9d9163fb DM |
2894 | char * const t = (last1 >= reginfo->strbeg) |
2895 | ? HOPc(last1, 1) : last1 + 1; | |
6eb5f6b9 JH |
2896 | |
2897 | last1 = HOPc(s, -back_min); | |
52657f30 | 2898 | s = t; |
6eb5f6b9 | 2899 | } |
f2ed9b32 | 2900 | if (utf8_target) { |
6eb5f6b9 | 2901 | while (s <= last1) { |
02d5137b | 2902 | if (regtry(reginfo, &s)) |
6eb5f6b9 | 2903 | goto got_it; |
7016d6eb DM |
2904 | if (s >= last1) { |
2905 | s++; /* to break out of outer loop */ | |
2906 | break; | |
2907 | } | |
2908 | s += UTF8SKIP(s); | |
6eb5f6b9 JH |
2909 | } |
2910 | } | |
2911 | else { | |
2912 | while (s <= last1) { | |
02d5137b | 2913 | if (regtry(reginfo, &s)) |
6eb5f6b9 JH |
2914 | goto got_it; |
2915 | s++; | |
2916 | } | |
2917 | } | |
2918 | } | |
ab3bbdeb | 2919 | DEBUG_EXECUTE_r(if (!did_match) { |
f2ed9b32 | 2920 | RE_PV_QUOTED_DECL(quoted, utf8_target, PERL_DEBUG_PAD_ZERO(0), |
ab3bbdeb YO |
2921 | SvPVX_const(must), RE_SV_DUMPLEN(must), 30); |
2922 | PerlIO_printf(Perl_debug_log, "Did not find %s substr %s%s...\n", | |
33b8afdf | 2923 | ((must == prog->anchored_substr || must == prog->anchored_utf8) |
bf93d4cc | 2924 | ? "anchored" : "floating"), |
ab3bbdeb YO |
2925 | quoted, RE_SV_TAIL(must)); |
2926 | }); | |
6eb5f6b9 JH |
2927 | goto phooey; |
2928 | } | |
f8fc2ecf | 2929 | else if ( (c = progi->regstclass) ) { |
f14c76ed | 2930 | if (minlen) { |
f8fc2ecf | 2931 | const OPCODE op = OP(progi->regstclass); |
66e933ab | 2932 | /* don't bother with what can't match */ |
786e8c11 | 2933 | if (PL_regkind[op] != EXACT && op != CANY && PL_regkind[op] != TRIE) |
f14c76ed RGS |
2934 | strend = HOPc(strend, -(minlen - 1)); |
2935 | } | |
a3621e74 | 2936 | DEBUG_EXECUTE_r({ |
be8e71aa | 2937 | SV * const prop = sv_newmortal(); |
2395827c | 2938 | regprop(prog, prop, c, reginfo); |
0df25f3d | 2939 | { |
f2ed9b32 | 2940 | RE_PV_QUOTED_DECL(quoted,utf8_target,PERL_DEBUG_PAD_ZERO(1), |
ab3bbdeb | 2941 | s,strend-s,60); |
0df25f3d | 2942 | PerlIO_printf(Perl_debug_log, |
1c8f8eb1 | 2943 | "Matching stclass %.*s against %s (%d bytes)\n", |
e4f74956 | 2944 | (int)SvCUR(prop), SvPVX_const(prop), |
ab3bbdeb | 2945 | quoted, (int)(strend - s)); |
0df25f3d | 2946 | } |
ffc61ed2 | 2947 | }); |
f9176b44 | 2948 | if (find_byclass(prog, c, s, strend, reginfo)) |
6eb5f6b9 | 2949 | goto got_it; |
07be1b83 | 2950 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Contradicts stclass... [regexec_flags]\n")); |
d6a28714 JH |
2951 | } |
2952 | else { | |
2953 | dontbother = 0; | |
a0714e2c | 2954 | if (prog->float_substr != NULL || prog->float_utf8 != NULL) { |
33b8afdf | 2955 | /* Trim the end. */ |
6af40bd7 | 2956 | char *last= NULL; |
33b8afdf | 2957 | SV* float_real; |
c33e64f0 FC |
2958 | STRLEN len; |
2959 | const char *little; | |
33b8afdf | 2960 | |
7e0d5ad7 KW |
2961 | if (utf8_target) { |
2962 | if (! prog->float_utf8) { | |
2963 | to_utf8_substr(prog); | |
2964 | } | |
2965 | float_real = prog->float_utf8; | |
2966 | } | |
2967 | else { | |
2968 | if (! prog->float_substr) { | |
2969 | if (! to_byte_substr(prog)) { | |
6b54ddc5 | 2970 | NON_UTF8_TARGET_BUT_UTF8_REQUIRED(phooey); |
7e0d5ad7 KW |
2971 | } |
2972 | } | |
2973 | float_real = prog->float_substr; | |
2974 | } | |
d6a28714 | 2975 | |
c33e64f0 FC |
2976 | little = SvPV_const(float_real, len); |
2977 | if (SvTAIL(float_real)) { | |
7f18ad16 KW |
2978 | /* This means that float_real contains an artificial \n on |
2979 | * the end due to the presence of something like this: | |
2980 | * /foo$/ where we can match both "foo" and "foo\n" at the | |
2981 | * end of the string. So we have to compare the end of the | |
2982 | * string first against the float_real without the \n and | |
2983 | * then against the full float_real with the string. We | |
2984 | * have to watch out for cases where the string might be | |
2985 | * smaller than the float_real or the float_real without | |
2986 | * the \n. */ | |
1a13b075 YO |
2987 | char *checkpos= strend - len; |
2988 | DEBUG_OPTIMISE_r( | |
2989 | PerlIO_printf(Perl_debug_log, | |
2990 | "%sChecking for float_real.%s\n", | |
2991 | PL_colors[4], PL_colors[5])); | |
2992 | if (checkpos + 1 < strbeg) { | |
7f18ad16 KW |
2993 | /* can't match, even if we remove the trailing \n |
2994 | * string is too short to match */ | |
1a13b075 YO |
2995 | DEBUG_EXECUTE_r( |
2996 | PerlIO_printf(Perl_debug_log, | |
2997 | "%sString shorter than required trailing substring, cannot match.%s\n", | |
2998 | PL_colors[4], PL_colors[5])); | |
2999 | goto phooey; | |
3000 | } else if (memEQ(checkpos + 1, little, len - 1)) { | |
7f18ad16 KW |
3001 | /* can match, the end of the string matches without the |
3002 | * "\n" */ | |
1a13b075 YO |
3003 | last = checkpos + 1; |
3004 | } else if (checkpos < strbeg) { | |
7f18ad16 KW |
3005 | /* cant match, string is too short when the "\n" is |
3006 | * included */ | |
1a13b075 YO |
3007 | DEBUG_EXECUTE_r( |
3008 | PerlIO_printf(Perl_debug_log, | |
3009 | "%sString does not contain required trailing substring, cannot match.%s\n", | |
3010 | PL_colors[4], PL_colors[5])); | |
3011 | goto phooey; | |
3012 | } else if (!multiline) { | |
7f18ad16 KW |
3013 | /* non multiline match, so compare with the "\n" at the |
3014 | * end of the string */ | |
1a13b075 YO |
3015 | if (memEQ(checkpos, little, len)) { |
3016 | last= checkpos; | |
3017 | } else { | |
3018 | DEBUG_EXECUTE_r( | |
3019 | PerlIO_printf(Perl_debug_log, | |
3020 | "%sString does not contain required trailing substring, cannot match.%s\n", | |
3021 | PL_colors[4], PL_colors[5])); | |
3022 | goto phooey; | |
3023 | } | |
3024 | } else { | |
7f18ad16 KW |
3025 | /* multiline match, so we have to search for a place |
3026 | * where the full string is located */ | |
d6a28714 | 3027 | goto find_last; |
1a13b075 | 3028 | } |
c33e64f0 | 3029 | } else { |
d6a28714 | 3030 | find_last: |
9041c2e3 | 3031 | if (len) |
d6a28714 | 3032 | last = rninstr(s, strend, little, little + len); |
b8c5462f | 3033 | else |
a0288114 | 3034 | last = strend; /* matching "$" */ |
b8c5462f | 3035 | } |
6af40bd7 | 3036 | if (!last) { |
7f18ad16 KW |
3037 | /* at one point this block contained a comment which was |
3038 | * probably incorrect, which said that this was a "should not | |
3039 | * happen" case. Even if it was true when it was written I am | |
3040 | * pretty sure it is not anymore, so I have removed the comment | |
3041 | * and replaced it with this one. Yves */ | |
6bda09f9 YO |
3042 | DEBUG_EXECUTE_r( |
3043 | PerlIO_printf(Perl_debug_log, | |
6af40bd7 YO |
3044 | "String does not contain required substring, cannot match.\n" |
3045 | )); | |
3046 | goto phooey; | |
bf93d4cc | 3047 | } |
d6a28714 JH |
3048 | dontbother = strend - last + prog->float_min_offset; |
3049 | } | |
3050 | if (minlen && (dontbother < minlen)) | |
3051 | dontbother = minlen - 1; | |
3052 | strend -= dontbother; /* this one's always in bytes! */ | |
3053 | /* We don't know much -- general case. */ | |
f2ed9b32 | 3054 | if (utf8_target) { |
d6a28714 | 3055 | for (;;) { |
02d5137b | 3056 | if (regtry(reginfo, &s)) |
d6a28714 JH |
3057 | goto got_it; |
3058 | if (s >= strend) | |
3059 | break; | |
b8c5462f | 3060 | s += UTF8SKIP(s); |
d6a28714 JH |
3061 | }; |
3062 | } | |
3063 | else { | |
3064 | do { | |
02d5137b | 3065 | if (regtry(reginfo, &s)) |
d6a28714 JH |
3066 | goto got_it; |
3067 | } while (s++ < strend); | |
3068 | } | |
3069 | } | |
3070 | ||
3071 | /* Failure. */ | |
3072 | goto phooey; | |
3073 | ||
3074 | got_it: | |
d5e7783a DM |
3075 | /* s/// doesn't like it if $& is earlier than where we asked it to |
3076 | * start searching (which can happen on something like /.\G/) */ | |
3077 | if ( (flags & REXEC_FAIL_ON_UNDERFLOW) | |
3078 | && (prog->offs[0].start < stringarg - strbeg)) | |
3079 | { | |
3080 | /* this should only be possible under \G */ | |
58430ea8 | 3081 | assert(prog->intflags & PREGf_GPOS_SEEN); |
d5e7783a DM |
3082 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, |
3083 | "matched, but failing for REXEC_FAIL_ON_UNDERFLOW\n")); | |
3084 | goto phooey; | |
3085 | } | |
3086 | ||
495f47a5 DM |
3087 | DEBUG_BUFFERS_r( |
3088 | if (swap) | |
3089 | PerlIO_printf(Perl_debug_log, | |
3090 | "rex=0x%"UVxf" freeing offs: 0x%"UVxf"\n", | |
3091 | PTR2UV(prog), | |
3092 | PTR2UV(swap) | |
3093 | ); | |
3094 | ); | |
e9105d30 | 3095 | Safefree(swap); |
d6a28714 | 3096 | |
bf2039a9 DM |
3097 | /* clean up; this will trigger destructors that will free all slabs |
3098 | * above the current one, and cleanup the regmatch_info_aux | |
3099 | * and regmatch_info_aux_eval sructs */ | |
8adc0f72 | 3100 | |
006f26b2 DM |
3101 | LEAVE_SCOPE(oldsave); |
3102 | ||
5daac39c NC |
3103 | if (RXp_PAREN_NAMES(prog)) |
3104 | (void)hv_iterinit(RXp_PAREN_NAMES(prog)); | |
d6a28714 | 3105 | |
0254aed9 DM |
3106 | RX_MATCH_UTF8_set(rx, utf8_target); |
3107 | ||
d6a28714 | 3108 | /* make sure $`, $&, $', and $digit will work later */ |
60165aa4 | 3109 | if ( !(flags & REXEC_NOT_FIRST) ) |
749f4950 | 3110 | S_reg_set_capture_string(aTHX_ rx, |
60165aa4 DM |
3111 | strbeg, reginfo->strend, |
3112 | sv, flags, utf8_target); | |
9041c2e3 | 3113 | |
d6a28714 JH |
3114 | return 1; |
3115 | ||
3116 | phooey: | |
a3621e74 | 3117 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch failed%s\n", |
e4584336 | 3118 | PL_colors[4], PL_colors[5])); |
8adc0f72 | 3119 | |
bf2039a9 DM |
3120 | /* clean up; this will trigger destructors that will free all slabs |
3121 | * above the current one, and cleanup the regmatch_info_aux | |
3122 | * and regmatch_info_aux_eval sructs */ | |
8adc0f72 | 3123 | |
006f26b2 DM |
3124 | LEAVE_SCOPE(oldsave); |
3125 | ||
e9105d30 | 3126 | if (swap) { |
c74340f9 | 3127 | /* we failed :-( roll it back */ |
495f47a5 DM |
3128 | DEBUG_BUFFERS_r(PerlIO_printf(Perl_debug_log, |
3129 | "rex=0x%"UVxf" rolling back offs: freeing=0x%"UVxf" restoring=0x%"UVxf"\n", | |
3130 | PTR2UV(prog), | |
3131 | PTR2UV(prog->offs), | |
3132 | PTR2UV(swap) | |
3133 | )); | |
e9105d30 GG |
3134 | Safefree(prog->offs); |
3135 | prog->offs = swap; | |
3136 | } | |
d6a28714 JH |
3137 | return 0; |
3138 | } | |
3139 | ||
6bda09f9 | 3140 | |
b3d298be | 3141 | /* Set which rex is pointed to by PL_reg_curpm, handling ref counting. |
ec43f78b | 3142 | * Do inc before dec, in case old and new rex are the same */ |
baa60164 | 3143 | #define SET_reg_curpm(Re2) \ |
bf2039a9 | 3144 | if (reginfo->info_aux_eval) { \ |
ec43f78b DM |
3145 | (void)ReREFCNT_inc(Re2); \ |
3146 | ReREFCNT_dec(PM_GETRE(PL_reg_curpm)); \ | |
3147 | PM_SETRE((PL_reg_curpm), (Re2)); \ | |
3148 | } | |
3149 | ||
3150 | ||
d6a28714 JH |
3151 | /* |
3152 | - regtry - try match at specific point | |
3153 | */ | |
3154 | STATIC I32 /* 0 failure, 1 success */ | |
f73aaa43 | 3155 | S_regtry(pTHX_ regmatch_info *reginfo, char **startposp) |
d6a28714 | 3156 | { |
97aff369 | 3157 | dVAR; |
d6a28714 | 3158 | CHECKPOINT lastcp; |
288b8c02 | 3159 | REGEXP *const rx = reginfo->prog; |
8d919b0a | 3160 | regexp *const prog = ReANY(rx); |
99a90e59 | 3161 | SSize_t result; |
f8fc2ecf | 3162 | RXi_GET_DECL(prog,progi); |
a3621e74 | 3163 | GET_RE_DEBUG_FLAGS_DECL; |
7918f24d NC |
3164 | |
3165 | PERL_ARGS_ASSERT_REGTRY; | |
3166 | ||
24b23f37 | 3167 | reginfo->cutpoint=NULL; |
d6a28714 | 3168 | |
9d9163fb | 3169 | prog->offs[0].start = *startposp - reginfo->strbeg; |
d6a28714 | 3170 | prog->lastparen = 0; |
03994de8 | 3171 | prog->lastcloseparen = 0; |
d6a28714 JH |
3172 | |
3173 | /* XXXX What this code is doing here?!!! There should be no need | |
b93070ed | 3174 | to do this again and again, prog->lastparen should take care of |
3dd2943c | 3175 | this! --ilya*/ |
dafc8851 JH |
3176 | |
3177 | /* Tests pat.t#187 and split.t#{13,14} seem to depend on this code. | |
3178 | * Actually, the code in regcppop() (which Ilya may be meaning by | |
b93070ed | 3179 | * prog->lastparen), is not needed at all by the test suite |
225593e1 DM |
3180 | * (op/regexp, op/pat, op/split), but that code is needed otherwise |
3181 | * this erroneously leaves $1 defined: "1" =~ /^(?:(\d)x)?\d$/ | |
3182 | * Meanwhile, this code *is* needed for the | |
daf18116 JH |
3183 | * above-mentioned test suite tests to succeed. The common theme |
3184 | * on those tests seems to be returning null fields from matches. | |
225593e1 | 3185 | * --jhi updated by dapm */ |
dafc8851 | 3186 | #if 1 |
d6a28714 | 3187 | if (prog->nparens) { |
b93070ed | 3188 | regexp_paren_pair *pp = prog->offs; |
eb578fdb | 3189 | I32 i; |
b93070ed | 3190 | for (i = prog->nparens; i > (I32)prog->lastparen; i--) { |
f0ab9afb NC |
3191 | ++pp; |
3192 | pp->start = -1; | |
3193 | pp->end = -1; | |
d6a28714 JH |
3194 | } |
3195 | } | |
dafc8851 | 3196 | #endif |
02db2b7b | 3197 | REGCP_SET(lastcp); |
f73aaa43 DM |
3198 | result = regmatch(reginfo, *startposp, progi->program + 1); |
3199 | if (result != -1) { | |
3200 | prog->offs[0].end = result; | |
d6a28714 JH |
3201 | return 1; |
3202 | } | |
24b23f37 | 3203 | if (reginfo->cutpoint) |
f73aaa43 | 3204 | *startposp= reginfo->cutpoint; |
02db2b7b | 3205 | REGCP_UNWIND(lastcp); |
d6a28714 JH |
3206 | return 0; |
3207 | } | |
3208 | ||
02db2b7b | 3209 | |
8ba1375e MJD |
3210 | #define sayYES goto yes |
3211 | #define sayNO goto no | |
262b90c4 | 3212 | #define sayNO_SILENT goto no_silent |
8ba1375e | 3213 | |
f9f4320a YO |
3214 | /* we dont use STMT_START/END here because it leads to |
3215 | "unreachable code" warnings, which are bogus, but distracting. */ | |
3216 | #define CACHEsayNO \ | |
c476f425 | 3217 | if (ST.cache_mask) \ |
2ac8ff4b | 3218 | reginfo->info_aux->poscache[ST.cache_offset] |= ST.cache_mask; \ |
f9f4320a | 3219 | sayNO |
3298f257 | 3220 | |
a3621e74 | 3221 | /* this is used to determine how far from the left messages like |
265c4333 YO |
3222 | 'failed...' are printed. It should be set such that messages |
3223 | are inline with the regop output that created them. | |
a3621e74 | 3224 | */ |
265c4333 | 3225 | #define REPORT_CODE_OFF 32 |
a3621e74 YO |
3226 | |
3227 | ||
40a82448 DM |
3228 | #define CHRTEST_UNINIT -1001 /* c1/c2 haven't been calculated yet */ |
3229 | #define CHRTEST_VOID -1000 /* the c1/c2 "next char" test should be skipped */ | |
79a2a0e8 KW |
3230 | #define CHRTEST_NOT_A_CP_1 -999 |
3231 | #define CHRTEST_NOT_A_CP_2 -998 | |
9e137952 | 3232 | |
5d9a96ca DM |
3233 | /* grab a new slab and return the first slot in it */ |
3234 | ||
3235 | STATIC regmatch_state * | |
3236 | S_push_slab(pTHX) | |
3237 | { | |
a35a87e7 | 3238 | #if PERL_VERSION < 9 && !defined(PERL_CORE) |
54df2634 NC |
3239 | dMY_CXT; |
3240 | #endif | |
5d9a96ca DM |
3241 | regmatch_slab *s = PL_regmatch_slab->next; |
3242 | if (!s) { | |
3243 | Newx(s, 1, regmatch_slab); | |
3244 | s->prev = PL_regmatch_slab; | |
3245 | s->next = NULL; | |
3246 | PL_regmatch_slab->next = s; | |
3247 | } | |
3248 | PL_regmatch_slab = s; | |
86545054 | 3249 | return SLAB_FIRST(s); |
5d9a96ca | 3250 | } |
5b47454d | 3251 | |
95b24440 | 3252 | |
40a82448 DM |
3253 | /* push a new state then goto it */ |
3254 | ||
4d5016e5 DM |
3255 | #define PUSH_STATE_GOTO(state, node, input) \ |
3256 | pushinput = input; \ | |
40a82448 DM |
3257 | scan = node; \ |
3258 | st->resume_state = state; \ | |
3259 | goto push_state; | |
3260 | ||
3261 | /* push a new state with success backtracking, then goto it */ | |
3262 | ||
4d5016e5 DM |
3263 | #define PUSH_YES_STATE_GOTO(state, node, input) \ |
3264 | pushinput = input; \ | |
40a82448 DM |
3265 | scan = node; \ |
3266 | st->resume_state = state; \ | |
3267 | goto push_yes_state; | |
3268 | ||
aa283a38 | 3269 | |
aa283a38 | 3270 | |
4d5016e5 | 3271 | |
d6a28714 | 3272 | /* |
95b24440 | 3273 | |
bf1f174e DM |
3274 | regmatch() - main matching routine |
3275 | ||
3276 | This is basically one big switch statement in a loop. We execute an op, | |
3277 | set 'next' to point the next op, and continue. If we come to a point which | |
3278 | we may need to backtrack to on failure such as (A|B|C), we push a | |
3279 | backtrack state onto the backtrack stack. On failure, we pop the top | |
3280 | state, and re-enter the loop at the state indicated. If there are no more | |
3281 | states to pop, we return failure. | |
3282 | ||
3283 | Sometimes we also need to backtrack on success; for example /A+/, where | |
3284 | after successfully matching one A, we need to go back and try to | |
3285 | match another one; similarly for lookahead assertions: if the assertion | |
3286 | completes successfully, we backtrack to the state just before the assertion | |
3287 | and then carry on. In these cases, the pushed state is marked as | |
3288 | 'backtrack on success too'. This marking is in fact done by a chain of | |
3289 | pointers, each pointing to the previous 'yes' state. On success, we pop to | |
3290 | the nearest yes state, discarding any intermediate failure-only states. | |
3291 | Sometimes a yes state is pushed just to force some cleanup code to be | |
3292 | called at the end of a successful match or submatch; e.g. (??{$re}) uses | |
3293 | it to free the inner regex. | |
3294 | ||
3295 | Note that failure backtracking rewinds the cursor position, while | |
3296 | success backtracking leaves it alone. | |
3297 | ||
3298 | A pattern is complete when the END op is executed, while a subpattern | |
3299 | such as (?=foo) is complete when the SUCCESS op is executed. Both of these | |
3300 | ops trigger the "pop to last yes state if any, otherwise return true" | |
3301 | behaviour. | |
3302 | ||
3303 | A common convention in this function is to use A and B to refer to the two | |
3304 | subpatterns (or to the first nodes thereof) in patterns like /A*B/: so A is | |
3305 | the subpattern to be matched possibly multiple times, while B is the entire | |
3306 | rest of the pattern. Variable and state names reflect this convention. | |
3307 | ||
3308 | The states in the main switch are the union of ops and failure/success of | |
3309 | substates associated with with that op. For example, IFMATCH is the op | |
3310 | that does lookahead assertions /(?=A)B/ and so the IFMATCH state means | |
3311 | 'execute IFMATCH'; while IFMATCH_A is a state saying that we have just | |
3312 | successfully matched A and IFMATCH_A_fail is a state saying that we have | |
3313 | just failed to match A. Resume states always come in pairs. The backtrack | |
3314 | state we push is marked as 'IFMATCH_A', but when that is popped, we resume | |
3315 | at IFMATCH_A or IFMATCH_A_fail, depending on whether we are backtracking | |
3316 | on success or failure. | |
3317 | ||
3318 | The struct that holds a backtracking state is actually a big union, with | |
3319 | one variant for each major type of op. The variable st points to the | |
3320 | top-most backtrack struct. To make the code clearer, within each | |
3321 | block of code we #define ST to alias the relevant union. | |
3322 | ||
3323 | Here's a concrete example of a (vastly oversimplified) IFMATCH | |
3324 | implementation: | |
3325 | ||
3326 | switch (state) { | |
3327 | .... | |
3328 | ||
3329 | #define ST st->u.ifmatch | |
3330 | ||
3331 | case IFMATCH: // we are executing the IFMATCH op, (?=A)B | |
3332 | ST.foo = ...; // some state we wish to save | |
95b24440 | 3333 | ... |
bf1f174e DM |
3334 | // push a yes backtrack state with a resume value of |
3335 | // IFMATCH_A/IFMATCH_A_fail, then continue execution at the | |
3336 | // first node of A: | |
4d5016e5 | 3337 | PUSH_YES_STATE_GOTO(IFMATCH_A, A, newinput); |
bf1f174e DM |
3338 | // NOTREACHED |
3339 | ||
3340 | case IFMATCH_A: // we have successfully executed A; now continue with B | |
3341 | next = B; | |
3342 | bar = ST.foo; // do something with the preserved value | |
3343 | break; | |
3344 | ||
3345 | case IFMATCH_A_fail: // A failed, so the assertion failed | |
3346 | ...; // do some housekeeping, then ... | |
3347 | sayNO; // propagate the failure | |
3348 | ||
3349 | #undef ST | |
95b24440 | 3350 | |
bf1f174e DM |
3351 | ... |
3352 | } | |
95b24440 | 3353 | |
bf1f174e DM |
3354 | For any old-timers reading this who are familiar with the old recursive |
3355 | approach, the code above is equivalent to: | |
95b24440 | 3356 | |
bf1f174e DM |
3357 | case IFMATCH: // we are executing the IFMATCH op, (?=A)B |
3358 | { | |
3359 | int foo = ... | |
95b24440 | 3360 | ... |
bf1f174e DM |
3361 | if (regmatch(A)) { |
3362 | next = B; | |
3363 | bar = foo; | |
3364 | break; | |
95b24440 | 3365 | } |
bf1f174e DM |
3366 | ...; // do some housekeeping, then ... |
3367 | sayNO; // propagate the failure | |
95b24440 | 3368 | } |
bf1f174e DM |
3369 | |
3370 | The topmost backtrack state, pointed to by st, is usually free. If you | |
3371 | want to claim it, populate any ST.foo fields in it with values you wish to | |
3372 | save, then do one of | |
3373 | ||
4d5016e5 DM |
3374 | PUSH_STATE_GOTO(resume_state, node, newinput); |
3375 | PUSH_YES_STATE_GOTO(resume_state, node, newinput); | |
bf1f174e DM |
3376 | |
3377 | which sets that backtrack state's resume value to 'resume_state', pushes a | |
3378 | new free entry to the top of the backtrack stack, then goes to 'node'. | |
3379 | On backtracking, the free slot is popped, and the saved state becomes the | |
3380 | new free state. An ST.foo field in this new top state can be temporarily | |
3381 | accessed to retrieve values, but once the main loop is re-entered, it | |
3382 | becomes available for reuse. | |
3383 | ||
3384 | Note that the depth of the backtrack stack constantly increases during the | |
3385 | left-to-right execution of the pattern, rather than going up and down with | |
3386 | the pattern nesting. For example the stack is at its maximum at Z at the | |
3387 | end of the pattern, rather than at X in the following: | |
3388 | ||
3389 | /(((X)+)+)+....(Y)+....Z/ | |
3390 | ||
3391 | The only exceptions to this are lookahead/behind assertions and the cut, | |
3392 | (?>A), which pop all the backtrack states associated with A before | |
3393 | continuing. | |
3394 | ||
486ec47a | 3395 | Backtrack state structs are allocated in slabs of about 4K in size. |
bf1f174e DM |
3396 | PL_regmatch_state and st always point to the currently active state, |
3397 | and PL_regmatch_slab points to the slab currently containing | |
3398 | PL_regmatch_state. The first time regmatch() is called, the first slab is | |
3399 | allocated, and is never freed until interpreter destruction. When the slab | |
3400 | is full, a new one is allocated and chained to the end. At exit from | |
3401 | regmatch(), slabs allocated since entry are freed. | |
3402 | ||
3403 | */ | |
95b24440 | 3404 | |
40a82448 | 3405 | |
5bc10b2c | 3406 | #define DEBUG_STATE_pp(pp) \ |
265c4333 | 3407 | DEBUG_STATE_r({ \ |
baa60164 | 3408 | DUMP_EXEC_POS(locinput, scan, utf8_target); \ |
5bc10b2c | 3409 | PerlIO_printf(Perl_debug_log, \ |
5d458dd8 | 3410 | " %*s"pp" %s%s%s%s%s\n", \ |
5bc10b2c | 3411 | depth*2, "", \ |
baa60164 | 3412 | PL_reg_name[st->resume_state], \ |
5d458dd8 YO |
3413 | ((st==yes_state||st==mark_state) ? "[" : ""), \ |
3414 | ((st==yes_state) ? "Y" : ""), \ | |
3415 | ((st==mark_state) ? "M" : ""), \ | |
3416 | ((st==yes_state||st==mark_state) ? "]" : "") \ | |
3417 | ); \ | |
265c4333 | 3418 | }); |
5bc10b2c | 3419 | |
40a82448 | 3420 | |
3dab1dad | 3421 | #define REG_NODE_NUM(x) ((x) ? (int)((x)-prog) : -1) |
95b24440 | 3422 | |
3df15adc | 3423 | #ifdef DEBUGGING |
5bc10b2c | 3424 | |
ab3bbdeb | 3425 | STATIC void |
f2ed9b32 | 3426 | S_debug_start_match(pTHX_ const REGEXP *prog, const bool utf8_target, |
ab3bbdeb YO |
3427 | const char *start, const char *end, const char *blurb) |
3428 | { | |
efd26800 | 3429 | const bool utf8_pat = RX_UTF8(prog) ? 1 : 0; |
7918f24d NC |
3430 | |
3431 | PERL_ARGS_ASSERT_DEBUG_START_MATCH; | |
3432 | ||
ab3bbdeb YO |
3433 | if (!PL_colorset) |
3434 | reginitcolors(); | |
3435 | { | |
3436 | RE_PV_QUOTED_DECL(s0, utf8_pat, PERL_DEBUG_PAD_ZERO(0), | |
d2c6dc5e | 3437 | RX_PRECOMP_const(prog), RX_PRELEN(prog), 60); |
ab3bbdeb | 3438 | |
f2ed9b32 | 3439 | RE_PV_QUOTED_DECL(s1, utf8_target, PERL_DEBUG_PAD_ZERO(1), |
ab3bbdeb YO |
3440 | start, end - start, 60); |
3441 | ||
3442 | PerlIO_printf(Perl_debug_log, | |
3443 | "%s%s REx%s %s against %s\n", | |
3444 | PL_colors[4], blurb, PL_colors[5], s0, s1); | |
3445 | ||
f2ed9b32 | 3446 | if (utf8_target||utf8_pat) |
1de06328 YO |
3447 | PerlIO_printf(Perl_debug_log, "UTF-8 %s%s%s...\n", |
3448 | utf8_pat ? "pattern" : "", | |
f2ed9b32 KW |
3449 | utf8_pat && utf8_target ? " and " : "", |
3450 | utf8_target ? "string" : "" | |
ab3bbdeb YO |
3451 | ); |
3452 | } | |
3453 | } | |
3df15adc YO |
3454 | |
3455 | STATIC void | |
786e8c11 YO |
3456 | S_dump_exec_pos(pTHX_ const char *locinput, |
3457 | const regnode *scan, | |
3458 | const char *loc_regeol, | |
3459 | const char *loc_bostr, | |
3460 | const char *loc_reg_starttry, | |
f2ed9b32 | 3461 | const bool utf8_target) |
07be1b83 | 3462 | { |
786e8c11 | 3463 | const int docolor = *PL_colors[0] || *PL_colors[2] || *PL_colors[4]; |
07be1b83 | 3464 | const int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */ |
786e8c11 | 3465 | int l = (loc_regeol - locinput) > taill ? taill : (loc_regeol - locinput); |
07be1b83 YO |
3466 | /* The part of the string before starttry has one color |
3467 | (pref0_len chars), between starttry and current | |
3468 | position another one (pref_len - pref0_len chars), | |
3469 | after the current position the third one. | |
3470 | We assume that pref0_len <= pref_len, otherwise we | |
3471 | decrease pref0_len. */ | |
786e8c11 YO |
3472 | int pref_len = (locinput - loc_bostr) > (5 + taill) - l |
3473 | ? (5 + taill) - l : locinput - loc_bostr; | |
07be1b83 YO |
3474 | int pref0_len; |
3475 | ||
7918f24d NC |
3476 | PERL_ARGS_ASSERT_DUMP_EXEC_POS; |
3477 | ||
f2ed9b32 | 3478 | while (utf8_target && UTF8_IS_CONTINUATION(*(U8*)(locinput - pref_len))) |
07be1b83 | 3479 | pref_len++; |
786e8c11 YO |
3480 | pref0_len = pref_len - (locinput - loc_reg_starttry); |
3481 | if (l + pref_len < (5 + taill) && l < loc_regeol - locinput) | |
3482 | l = ( loc_regeol - locinput > (5 + taill) - pref_len | |
3483 | ? (5 + taill) - pref_len : loc_regeol - locinput); | |
f2ed9b32 | 3484 | while (utf8_target && UTF8_IS_CONTINUATION(*(U8*)(locinput + l))) |
07be1b83 YO |
3485 | l--; |
3486 | if (pref0_len < 0) | |
3487 | pref0_len = 0; | |
3488 | if (pref0_len > pref_len) | |
3489 | pref0_len = pref_len; | |
3490 | { | |
f2ed9b32 | 3491 | const int is_uni = (utf8_target && OP(scan) != CANY) ? 1 : 0; |
0df25f3d | 3492 | |
ab3bbdeb | 3493 | RE_PV_COLOR_DECL(s0,len0,is_uni,PERL_DEBUG_PAD(0), |
1de06328 | 3494 | (locinput - pref_len),pref0_len, 60, 4, 5); |
0df25f3d | 3495 | |
ab3bbdeb | 3496 | RE_PV_COLOR_DECL(s1,len1,is_uni,PERL_DEBUG_PAD(1), |
3df15adc | 3497 | (locinput - pref_len + pref0_len), |
1de06328 | 3498 | pref_len - pref0_len, 60, 2, 3); |
0df25f3d | 3499 | |
ab3bbdeb | 3500 | RE_PV_COLOR_DECL(s2,len2,is_uni,PERL_DEBUG_PAD(2), |
1de06328 | 3501 | locinput, loc_regeol - locinput, 10, 0, 1); |
0df25f3d | 3502 | |
1de06328 | 3503 | const STRLEN tlen=len0+len1+len2; |
3df15adc | 3504 | PerlIO_printf(Perl_debug_log, |
ab3bbdeb | 3505 | "%4"IVdf" <%.*s%.*s%s%.*s>%*s|", |
786e8c11 | 3506 | (IV)(locinput - loc_bostr), |
07be1b83 | 3507 | len0, s0, |
07be1b83 | 3508 | len1, s1, |
07be1b83 | 3509 | (docolor ? "" : "> <"), |
07be1b83 | 3510 | len2, s2, |
f9f4320a | 3511 | (int)(tlen > 19 ? 0 : 19 - tlen), |
07be1b83 YO |
3512 | ""); |
3513 | } | |
3514 | } | |
3df15adc | 3515 | |
07be1b83 YO |
3516 | #endif |
3517 | ||
0a4db386 YO |
3518 | /* reg_check_named_buff_matched() |
3519 | * Checks to see if a named buffer has matched. The data array of | |
3520 | * buffer numbers corresponding to the buffer is expected to reside | |
3521 | * in the regexp->data->data array in the slot stored in the ARG() of | |
3522 | * node involved. Note that this routine doesn't actually care about the | |
3523 | * name, that information is not preserved from compilation to execution. | |
3524 | * Returns the index of the leftmost defined buffer with the given name | |
3525 | * or 0 if non of the buffers matched. | |
3526 | */ | |
3527 | STATIC I32 | |
7918f24d NC |
3528 | S_reg_check_named_buff_matched(pTHX_ const regexp *rex, const regnode *scan) |
3529 | { | |
0a4db386 | 3530 | I32 n; |
f8fc2ecf | 3531 | RXi_GET_DECL(rex,rexi); |
ad64d0ec | 3532 | SV *sv_dat= MUTABLE_SV(rexi->data->data[ ARG( scan ) ]); |
0a4db386 | 3533 | I32 *nums=(I32*)SvPVX(sv_dat); |
7918f24d NC |
3534 | |
3535 | PERL_ARGS_ASSERT_REG_CHECK_NAMED_BUFF_MATCHED; | |
3536 | ||
0a4db386 | 3537 | for ( n=0; n<SvIVX(sv_dat); n++ ) { |
b93070ed DM |
3538 | if ((I32)rex->lastparen >= nums[n] && |
3539 | rex->offs[nums[n]].end != -1) | |
0a4db386 YO |
3540 | { |
3541 | return nums[n]; | |
3542 | } | |
3543 | } | |
3544 | return 0; | |
3545 | } | |
3546 | ||
2f554ef7 | 3547 | |
c74f6de9 | 3548 | static bool |
984e6dd1 | 3549 | S_setup_EXACTISH_ST_c1_c2(pTHX_ const regnode * const text_node, int *c1p, |
aed7b151 | 3550 | U8* c1_utf8, int *c2p, U8* c2_utf8, regmatch_info *reginfo) |
c74f6de9 | 3551 | { |
79a2a0e8 KW |
3552 | /* This function determines if there are one or two characters that match |
3553 | * the first character of the passed-in EXACTish node <text_node>, and if | |
3554 | * so, returns them in the passed-in pointers. | |
c74f6de9 | 3555 | * |
79a2a0e8 KW |
3556 | * If it determines that no possible character in the target string can |
3557 | * match, it returns FALSE; otherwise TRUE. (The FALSE situation occurs if | |
3558 | * the first character in <text_node> requires UTF-8 to represent, and the | |
3559 | * target string isn't in UTF-8.) | |
c74f6de9 | 3560 | * |
79a2a0e8 KW |
3561 | * If there are more than two characters that could match the beginning of |
3562 | * <text_node>, or if more context is required to determine a match or not, | |
3563 | * it sets both *<c1p> and *<c2p> to CHRTEST_VOID. | |
3564 | * | |
3565 | * The motiviation behind this function is to allow the caller to set up | |
3566 | * tight loops for matching. If <text_node> is of type EXACT, there is | |
3567 | * only one possible character that can match its first character, and so | |
3568 | * the situation is quite simple. But things get much more complicated if | |
3569 | * folding is involved. It may be that the first character of an EXACTFish | |
3570 | * node doesn't participate in any possible fold, e.g., punctuation, so it | |
3571 | * can be matched only by itself. The vast majority of characters that are | |
3572 | * in folds match just two things, their lower and upper-case equivalents. | |
3573 | * But not all are like that; some have multiple possible matches, or match | |
3574 | * sequences of more than one character. This function sorts all that out. | |
3575 | * | |
3576 | * Consider the patterns A*B or A*?B where A and B are arbitrary. In a | |
3577 | * loop of trying to match A*, we know we can't exit where the thing | |
3578 | * following it isn't a B. And something can't be a B unless it is the | |
3579 | * beginning of B. By putting a quick test for that beginning in a tight | |
3580 | * loop, we can rule out things that can't possibly be B without having to | |
3581 | * break out of the loop, thus avoiding work. Similarly, if A is a single | |
3582 | * character, we can make a tight loop matching A*, using the outputs of | |
3583 | * this function. | |
3584 | * | |
3585 | * If the target string to match isn't in UTF-8, and there aren't | |
3586 | * complications which require CHRTEST_VOID, *<c1p> and *<c2p> are set to | |
3587 | * the one or two possible octets (which are characters in this situation) | |
3588 | * that can match. In all cases, if there is only one character that can | |
3589 | * match, *<c1p> and *<c2p> will be identical. | |
3590 | * | |
3591 | * If the target string is in UTF-8, the buffers pointed to by <c1_utf8> | |
3592 | * and <c2_utf8> will contain the one or two UTF-8 sequences of bytes that | |
3593 | * can match the beginning of <text_node>. They should be declared with at | |
3594 | * least length UTF8_MAXBYTES+1. (If the target string isn't in UTF-8, it is | |
3595 | * undefined what these contain.) If one or both of the buffers are | |
3596 | * invariant under UTF-8, *<c1p>, and *<c2p> will also be set to the | |
3597 | * corresponding invariant. If variant, the corresponding *<c1p> and/or | |
3598 | * *<c2p> will be set to a negative number(s) that shouldn't match any code | |
3599 | * point (unless inappropriately coerced to unsigned). *<c1p> will equal | |
3600 | * *<c2p> if and only if <c1_utf8> and <c2_utf8> are the same. */ | |
c74f6de9 | 3601 | |
ba44c216 | 3602 | const bool utf8_target = reginfo->is_utf8_target; |
79a2a0e8 | 3603 | |
ddb0d839 KW |
3604 | UV c1 = CHRTEST_NOT_A_CP_1; |
3605 | UV c2 = CHRTEST_NOT_A_CP_2; | |
79a2a0e8 | 3606 | bool use_chrtest_void = FALSE; |
aed7b151 | 3607 | const bool is_utf8_pat = reginfo->is_utf8_pat; |
79a2a0e8 KW |
3608 | |
3609 | /* Used when we have both utf8 input and utf8 output, to avoid converting | |
3610 | * to/from code points */ | |
3611 | bool utf8_has_been_setup = FALSE; | |
3612 | ||
c74f6de9 KW |
3613 | dVAR; |
3614 | ||
b4291290 | 3615 | U8 *pat = (U8*)STRING(text_node); |
a6715020 | 3616 | U8 folded[UTF8_MAX_FOLD_CHAR_EXPAND * UTF8_MAXBYTES_CASE + 1] = { '\0' }; |
c74f6de9 | 3617 | |
79a2a0e8 KW |
3618 | if (OP(text_node) == EXACT) { |
3619 | ||
3620 | /* In an exact node, only one thing can be matched, that first | |
3621 | * character. If both the pat and the target are UTF-8, we can just | |
3622 | * copy the input to the output, avoiding finding the code point of | |
3623 | * that character */ | |
984e6dd1 | 3624 | if (!is_utf8_pat) { |
79a2a0e8 KW |
3625 | c2 = c1 = *pat; |
3626 | } | |
3627 | else if (utf8_target) { | |
3628 | Copy(pat, c1_utf8, UTF8SKIP(pat), U8); | |
3629 | Copy(pat, c2_utf8, UTF8SKIP(pat), U8); | |
3630 | utf8_has_been_setup = TRUE; | |
3631 | } | |
3632 | else { | |
3633 | c2 = c1 = valid_utf8_to_uvchr(pat, NULL); | |
c74f6de9 | 3634 | } |
79a2a0e8 | 3635 | } |
31f05a37 KW |
3636 | else { /* an EXACTFish node */ |
3637 | U8 *pat_end = pat + STR_LEN(text_node); | |
3638 | ||
3639 | /* An EXACTFL node has at least some characters unfolded, because what | |
3640 | * they match is not known until now. So, now is the time to fold | |
3641 | * the first few of them, as many as are needed to determine 'c1' and | |
3642 | * 'c2' later in the routine. If the pattern isn't UTF-8, we only need | |
3643 | * to fold if in a UTF-8 locale, and then only the Sharp S; everything | |
3644 | * else is 1-1 and isn't assumed to be folded. In a UTF-8 pattern, we | |
3645 | * need to fold as many characters as a single character can fold to, | |
3646 | * so that later we can check if the first ones are such a multi-char | |
3647 | * fold. But, in such a pattern only locale-problematic characters | |
3648 | * aren't folded, so we can skip this completely if the first character | |
3649 | * in the node isn't one of the tricky ones */ | |
3650 | if (OP(text_node) == EXACTFL) { | |
3651 | ||
3652 | if (! is_utf8_pat) { | |
3653 | if (IN_UTF8_CTYPE_LOCALE && *pat == LATIN_SMALL_LETTER_SHARP_S) | |
3654 | { | |
3655 | folded[0] = folded[1] = 's'; | |
3656 | pat = folded; | |
3657 | pat_end = folded + 2; | |
3658 | } | |
3659 | } | |
3660 | else if (is_PROBLEMATIC_LOCALE_FOLDEDS_START_utf8(pat)) { | |
3661 | U8 *s = pat; | |
3662 | U8 *d = folded; | |
3663 | int i; | |
3664 | ||
3665 | for (i = 0; i < UTF8_MAX_FOLD_CHAR_EXPAND && s < pat_end; i++) { | |
3666 | if (isASCII(*s)) { | |
3667 | *(d++) = (U8) toFOLD_LC(*s); | |
3668 | s++; | |
3669 | } | |
3670 | else { | |
3671 | STRLEN len; | |
3672 | _to_utf8_fold_flags(s, | |
3673 | d, | |
3674 | &len, | |
3675 | FOLD_FLAGS_FULL | FOLD_FLAGS_LOCALE); | |
3676 | d += len; | |
3677 | s += UTF8SKIP(s); | |
3678 | } | |
3679 | } | |
3680 | ||
3681 | pat = folded; | |
3682 | pat_end = d; | |
3683 | } | |
3684 | } | |
3685 | ||
251b239f KW |
3686 | if ((is_utf8_pat && is_MULTI_CHAR_FOLD_utf8_safe(pat, pat_end)) |
3687 | || (!is_utf8_pat && is_MULTI_CHAR_FOLD_latin1_safe(pat, pat_end))) | |
baa60164 KW |
3688 | { |
3689 | /* Multi-character folds require more context to sort out. Also | |
3690 | * PL_utf8_foldclosures used below doesn't handle them, so have to | |
3691 | * be handled outside this routine */ | |
3692 | use_chrtest_void = TRUE; | |
3693 | } | |
3694 | else { /* an EXACTFish node which doesn't begin with a multi-char fold */ | |
3695 | c1 = is_utf8_pat ? valid_utf8_to_uvchr(pat, NULL) : *pat; | |
3696 | if (c1 > 256) { | |
3697 | /* Load the folds hash, if not already done */ | |
3698 | SV** listp; | |
3699 | if (! PL_utf8_foldclosures) { | |
3700 | if (! PL_utf8_tofold) { | |
3701 | U8 dummy[UTF8_MAXBYTES_CASE+1]; | |
3702 | ||
3703 | /* Force loading this by folding an above-Latin1 char */ | |
3704 | to_utf8_fold((U8*) HYPHEN_UTF8, dummy, NULL); | |
3705 | assert(PL_utf8_tofold); /* Verify that worked */ | |
3706 | } | |
3707 | PL_utf8_foldclosures = _swash_inversion_hash(PL_utf8_tofold); | |
79a2a0e8 | 3708 | } |
79a2a0e8 | 3709 | |
baa60164 KW |
3710 | /* The fold closures data structure is a hash with the keys |
3711 | * being the UTF-8 of every character that is folded to, like | |
3712 | * 'k', and the values each an array of all code points that | |
3713 | * fold to its key. e.g. [ 'k', 'K', KELVIN_SIGN ]. | |
3714 | * Multi-character folds are not included */ | |
3715 | if ((! (listp = hv_fetch(PL_utf8_foldclosures, | |
3716 | (char *) pat, | |
3717 | UTF8SKIP(pat), | |
3718 | FALSE)))) | |
3719 | { | |
3720 | /* Not found in the hash, therefore there are no folds | |
3721 | * containing it, so there is only a single character that | |
3722 | * could match */ | |
3723 | c2 = c1; | |
79a2a0e8 | 3724 | } |
baa60164 KW |
3725 | else { /* Does participate in folds */ |
3726 | AV* list = (AV*) *listp; | |
b9f2b683 | 3727 | if (av_tindex(list) != 1) { |
79a2a0e8 | 3728 | |
baa60164 KW |
3729 | /* If there aren't exactly two folds to this, it is |
3730 | * outside the scope of this function */ | |
3731 | use_chrtest_void = TRUE; | |
79a2a0e8 | 3732 | } |
baa60164 KW |
3733 | else { /* There are two. Get them */ |
3734 | SV** c_p = av_fetch(list, 0, FALSE); | |
3735 | if (c_p == NULL) { | |
3736 | Perl_croak(aTHX_ "panic: invalid PL_utf8_foldclosures structure"); | |
3737 | } | |
3738 | c1 = SvUV(*c_p); | |
3739 | ||
3740 | c_p = av_fetch(list, 1, FALSE); | |
3741 | if (c_p == NULL) { | |
3742 | Perl_croak(aTHX_ "panic: invalid PL_utf8_foldclosures structure"); | |
3743 | } | |
3744 | c2 = SvUV(*c_p); | |
3745 | ||
3746 | /* Folds that cross the 255/256 boundary are forbidden | |
3747 | * if EXACTFL (and isnt a UTF8 locale), or EXACTFA and | |
3748 | * one is ASCIII. Since the pattern character is above | |
3749 | * 256, and its only other match is below 256, the only | |
3750 | * legal match will be to itself. We have thrown away | |
3751 | * the original, so have to compute which is the one | |
3752 | * above 255 */ | |
3753 | if ((c1 < 256) != (c2 < 256)) { | |
3754 | if ((OP(text_node) == EXACTFL | |
3755 | && ! IN_UTF8_CTYPE_LOCALE) | |
3756 | || ((OP(text_node) == EXACTFA | |
3757 | || OP(text_node) == EXACTFA_NO_TRIE) | |
3758 | && (isASCII(c1) || isASCII(c2)))) | |
3759 | { | |
3760 | if (c1 < 256) { | |
3761 | c1 = c2; | |
3762 | } | |
3763 | else { | |
3764 | c2 = c1; | |
3765 | } | |
79a2a0e8 KW |
3766 | } |
3767 | } | |
3768 | } | |
3769 | } | |
3770 | } | |
baa60164 KW |
3771 | else /* Here, c1 is < 255 */ |
3772 | if (utf8_target | |
3773 | && HAS_NONLATIN1_FOLD_CLOSURE(c1) | |
3774 | && ( ! (OP(text_node) == EXACTFL && ! IN_UTF8_CTYPE_LOCALE)) | |
3775 | && ((OP(text_node) != EXACTFA | |
3776 | && OP(text_node) != EXACTFA_NO_TRIE) | |
3777 | || ! isASCII(c1))) | |
3778 | { | |
3779 | /* Here, there could be something above Latin1 in the target | |
3780 | * which folds to this character in the pattern. All such | |
3781 | * cases except LATIN SMALL LETTER Y WITH DIAERESIS have more | |
3782 | * than two characters involved in their folds, so are outside | |
3783 | * the scope of this function */ | |
3784 | if (UNLIKELY(c1 == LATIN_SMALL_LETTER_Y_WITH_DIAERESIS)) { | |
3785 | c2 = LATIN_CAPITAL_LETTER_Y_WITH_DIAERESIS; | |
3786 | } | |
3787 | else { | |
3788 | use_chrtest_void = TRUE; | |
3789 | } | |
79a2a0e8 | 3790 | } |
baa60164 KW |
3791 | else { /* Here nothing above Latin1 can fold to the pattern |
3792 | character */ | |
3793 | switch (OP(text_node)) { | |
c74f6de9 | 3794 | |
baa60164 KW |
3795 | case EXACTFL: /* /l rules */ |
3796 | c2 = PL_fold_locale[c1]; | |
3797 | break; | |
c74f6de9 | 3798 | |
baa60164 KW |
3799 | case EXACTF: /* This node only generated for non-utf8 |
3800 | patterns */ | |
3801 | assert(! is_utf8_pat); | |
3802 | if (! utf8_target) { /* /d rules */ | |
3803 | c2 = PL_fold[c1]; | |
3804 | break; | |
3805 | } | |
3806 | /* FALLTHROUGH */ | |
3807 | /* /u rules for all these. This happens to work for | |
3808 | * EXACTFA as nothing in Latin1 folds to ASCII */ | |
3809 | case EXACTFA_NO_TRIE: /* This node only generated for | |
3810 | non-utf8 patterns */ | |
3811 | assert(! is_utf8_pat); | |
3812 | /* FALL THROUGH */ | |
3813 | case EXACTFA: | |
3814 | case EXACTFU_SS: | |
3815 | case EXACTFU: | |
3816 | c2 = PL_fold_latin1[c1]; | |
c74f6de9 | 3817 | break; |
c74f6de9 | 3818 | |
baa60164 KW |
3819 | default: |
3820 | Perl_croak(aTHX_ "panic: Unexpected op %u", OP(text_node)); | |
3821 | assert(0); /* NOTREACHED */ | |
3822 | } | |
c74f6de9 KW |
3823 | } |
3824 | } | |
3825 | } | |
79a2a0e8 KW |
3826 | |
3827 | /* Here have figured things out. Set up the returns */ | |
3828 | if (use_chrtest_void) { | |
3829 | *c2p = *c1p = CHRTEST_VOID; | |
3830 | } | |
3831 | else if (utf8_target) { | |
3832 | if (! utf8_has_been_setup) { /* Don't have the utf8; must get it */ | |
3833 | uvchr_to_utf8(c1_utf8, c1); | |
3834 | uvchr_to_utf8(c2_utf8, c2); | |
c74f6de9 | 3835 | } |
c74f6de9 | 3836 | |
79a2a0e8 KW |
3837 | /* Invariants are stored in both the utf8 and byte outputs; Use |
3838 | * negative numbers otherwise for the byte ones. Make sure that the | |
3839 | * byte ones are the same iff the utf8 ones are the same */ | |
3840 | *c1p = (UTF8_IS_INVARIANT(*c1_utf8)) ? *c1_utf8 : CHRTEST_NOT_A_CP_1; | |
3841 | *c2p = (UTF8_IS_INVARIANT(*c2_utf8)) | |
3842 | ? *c2_utf8 | |
3843 | : (c1 == c2) | |
3844 | ? CHRTEST_NOT_A_CP_1 | |
3845 | : CHRTEST_NOT_A_CP_2; | |
3846 | } | |
3847 | else if (c1 > 255) { | |
3848 | if (c2 > 255) { /* both possibilities are above what a non-utf8 string | |
3849 | can represent */ | |
3850 | return FALSE; | |
3851 | } | |
c74f6de9 | 3852 | |
79a2a0e8 KW |
3853 | *c1p = *c2p = c2; /* c2 is the only representable value */ |
3854 | } | |
3855 | else { /* c1 is representable; see about c2 */ | |
3856 | *c1p = c1; | |
3857 | *c2p = (c2 < 256) ? c2 : c1; | |
c74f6de9 | 3858 | } |
2f554ef7 | 3859 | |
c74f6de9 KW |
3860 | return TRUE; |
3861 | } | |
2f554ef7 | 3862 | |
f73aaa43 | 3863 | /* returns -1 on failure, $+[0] on success */ |
99a90e59 | 3864 | STATIC SSize_t |
f73aaa43 | 3865 | S_regmatch(pTHX_ regmatch_info *reginfo, char *startpos, regnode *prog) |
d6a28714 | 3866 | { |
a35a87e7 | 3867 | #if PERL_VERSION < 9 && !defined(PERL_CORE) |
54df2634 NC |
3868 | dMY_CXT; |
3869 | #endif | |
27da23d5 | 3870 | dVAR; |
ba44c216 | 3871 | const bool utf8_target = reginfo->is_utf8_target; |
4ad0818d | 3872 | const U32 uniflags = UTF8_ALLOW_DEFAULT; |
288b8c02 | 3873 | REGEXP *rex_sv = reginfo->prog; |
8d919b0a | 3874 | regexp *rex = ReANY(rex_sv); |
f8fc2ecf | 3875 | RXi_GET_DECL(rex,rexi); |
5d9a96ca | 3876 | /* the current state. This is a cached copy of PL_regmatch_state */ |
eb578fdb | 3877 | regmatch_state *st; |
5d9a96ca | 3878 | /* cache heavy used fields of st in registers */ |
eb578fdb KW |
3879 | regnode *scan; |
3880 | regnode *next; | |
3881 | U32 n = 0; /* general value; init to avoid compiler warning */ | |
ea3daa5d | 3882 | SSize_t ln = 0; /* len or last; init to avoid compiler warning */ |
d60de1d1 | 3883 | char *locinput = startpos; |
4d5016e5 | 3884 | char *pushinput; /* where to continue after a PUSH */ |
eb578fdb | 3885 | I32 nextchr; /* is always set to UCHARAT(locinput) */ |
24d3c4a9 | 3886 | |
b69b0499 | 3887 | bool result = 0; /* return value of S_regmatch */ |
24d3c4a9 | 3888 | int depth = 0; /* depth of backtrack stack */ |
4b196cd4 YO |
3889 | U32 nochange_depth = 0; /* depth of GOSUB recursion with nochange */ |
3890 | const U32 max_nochange_depth = | |
3891 | (3 * rex->nparens > MAX_RECURSE_EVAL_NOCHANGE_DEPTH) ? | |
3892 | 3 * rex->nparens : MAX_RECURSE_EVAL_NOCHANGE_DEPTH; | |
77cb431f DM |
3893 | regmatch_state *yes_state = NULL; /* state to pop to on success of |
3894 | subpattern */ | |
e2e6a0f1 YO |
3895 | /* mark_state piggy backs on the yes_state logic so that when we unwind |
3896 | the stack on success we can update the mark_state as we go */ | |
3897 | regmatch_state *mark_state = NULL; /* last mark state we have seen */ | |
faec1544 | 3898 | regmatch_state *cur_eval = NULL; /* most recent EVAL_AB state */ |
b8591aee | 3899 | struct regmatch_state *cur_curlyx = NULL; /* most recent curlyx */ |
40a82448 | 3900 | U32 state_num; |
5d458dd8 YO |
3901 | bool no_final = 0; /* prevent failure from backtracking? */ |
3902 | bool do_cutgroup = 0; /* no_final only until next branch/trie entry */ | |
d60de1d1 | 3903 | char *startpoint = locinput; |
5d458dd8 YO |
3904 | SV *popmark = NULL; /* are we looking for a mark? */ |
3905 | SV *sv_commit = NULL; /* last mark name seen in failure */ | |
3906 | SV *sv_yes_mark = NULL; /* last mark name we have seen | |
486ec47a | 3907 | during a successful match */ |
5d458dd8 YO |
3908 | U32 lastopen = 0; /* last open we saw */ |
3909 | bool has_cutgroup = RX_HAS_CUTGROUP(rex) ? 1 : 0; | |
4a2b275c | 3910 | SV* const oreplsv = GvSVn(PL_replgv); |
24d3c4a9 DM |
3911 | /* these three flags are set by various ops to signal information to |
3912 | * the very next op. They have a useful lifetime of exactly one loop | |
3913 | * iteration, and are not preserved or restored by state pushes/pops | |
3914 | */ | |
3915 | bool sw = 0; /* the condition value in (?(cond)a|b) */ | |
3916 | bool minmod = 0; /* the next "{n,m}" is a "{n,m}?" */ | |
3917 | int logical = 0; /* the following EVAL is: | |
3918 | 0: (?{...}) | |
3919 | 1: (?(?{...})X|Y) | |
3920 | 2: (??{...}) | |
3921 | or the following IFMATCH/UNLESSM is: | |
3922 | false: plain (?=foo) | |
3923 | true: used as a condition: (?(?=foo)) | |
3924 | */ | |
81ed78b2 DM |
3925 | PAD* last_pad = NULL; |
3926 | dMULTICALL; | |
3927 | I32 gimme = G_SCALAR; | |
3928 | CV *caller_cv = NULL; /* who called us */ | |
3929 | CV *last_pushed_cv = NULL; /* most recently called (?{}) CV */ | |
74088413 | 3930 | CHECKPOINT runops_cp; /* savestack position before executing EVAL */ |
92da3157 | 3931 | U32 maxopenparen = 0; /* max '(' index seen so far */ |
3018b823 KW |
3932 | int to_complement; /* Invert the result? */ |
3933 | _char_class_number classnum; | |
984e6dd1 | 3934 | bool is_utf8_pat = reginfo->is_utf8_pat; |
81ed78b2 | 3935 | |
95b24440 | 3936 | #ifdef DEBUGGING |
e68ec53f | 3937 | GET_RE_DEBUG_FLAGS_DECL; |
d6a28714 JH |
3938 | #endif |
3939 | ||
7e68f152 FC |
3940 | /* protect against undef(*^R) */ |
3941 | SAVEFREESV(SvREFCNT_inc_simple_NN(oreplsv)); | |
3942 | ||
81ed78b2 DM |
3943 | /* shut up 'may be used uninitialized' compiler warnings for dMULTICALL */ |
3944 | multicall_oldcatch = 0; | |
3945 | multicall_cv = NULL; | |
3946 | cx = NULL; | |
4f8dbb2d JL |
3947 | PERL_UNUSED_VAR(multicall_cop); |
3948 | PERL_UNUSED_VAR(newsp); | |
81ed78b2 DM |
3949 | |
3950 | ||
7918f24d NC |
3951 | PERL_ARGS_ASSERT_REGMATCH; |
3952 | ||
3b57cd43 | 3953 | DEBUG_OPTIMISE_r( DEBUG_EXECUTE_r({ |
24b23f37 | 3954 | PerlIO_printf(Perl_debug_log,"regmatch start\n"); |
3b57cd43 | 3955 | })); |
5d9a96ca | 3956 | |
331b2dcc | 3957 | st = PL_regmatch_state; |
5d9a96ca | 3958 | |
d6a28714 | 3959 | /* Note that nextchr is a byte even in UTF */ |
7016d6eb | 3960 | SET_nextchr; |
d6a28714 JH |
3961 | scan = prog; |
3962 | while (scan != NULL) { | |
8ba1375e | 3963 | |
a3621e74 | 3964 | DEBUG_EXECUTE_r( { |
6136c704 | 3965 | SV * const prop = sv_newmortal(); |
1de06328 | 3966 | regnode *rnext=regnext(scan); |
f2ed9b32 | 3967 | DUMP_EXEC_POS( locinput, scan, utf8_target ); |
2395827c | 3968 | regprop(rex, prop, scan, reginfo); |
07be1b83 YO |
3969 | |
3970 | PerlIO_printf(Perl_debug_log, | |
3971 | "%3"IVdf":%*s%s(%"IVdf")\n", | |
f8fc2ecf | 3972 | (IV)(scan - rexi->program), depth*2, "", |
07be1b83 | 3973 | SvPVX_const(prop), |
1de06328 | 3974 | (PL_regkind[OP(scan)] == END || !rnext) ? |
f8fc2ecf | 3975 | 0 : (IV)(rnext - rexi->program)); |
2a782b5b | 3976 | }); |
d6a28714 JH |
3977 | |
3978 | next = scan + NEXT_OFF(scan); | |
3979 | if (next == scan) | |
3980 | next = NULL; | |
40a82448 | 3981 | state_num = OP(scan); |
d6a28714 | 3982 | |
40a82448 | 3983 | reenter_switch: |
3018b823 | 3984 | to_complement = 0; |
34a81e2b | 3985 | |
7016d6eb | 3986 | SET_nextchr; |
e6ca698c | 3987 | assert(nextchr < 256 && (nextchr >= 0 || nextchr == NEXTCHR_EOS)); |
bf798dc4 | 3988 | |
40a82448 | 3989 | switch (state_num) { |
b838857b DM |
3990 | case BOL: /* /^../ */ |
3991 | case SBOL: /* /^../s */ | |
9d9163fb | 3992 | if (locinput == reginfo->strbeg) |
b8c5462f | 3993 | break; |
d6a28714 | 3994 | sayNO; |
3c0563b9 DM |
3995 | |
3996 | case MBOL: /* /^../m */ | |
9d9163fb | 3997 | if (locinput == reginfo->strbeg || |
7016d6eb | 3998 | (!NEXTCHR_IS_EOS && locinput[-1] == '\n')) |
d6a28714 | 3999 | { |
b8c5462f JH |
4000 | break; |
4001 | } | |
d6a28714 | 4002 | sayNO; |
3c0563b9 | 4003 | |
3c0563b9 | 4004 | case GPOS: /* \G */ |
3b0527fe | 4005 | if (locinput == reginfo->ganch) |
d6a28714 JH |
4006 | break; |
4007 | sayNO; | |
ee9b8eae | 4008 | |
3c0563b9 | 4009 | case KEEPS: /* \K */ |
ee9b8eae | 4010 | /* update the startpoint */ |
b93070ed | 4011 | st->u.keeper.val = rex->offs[0].start; |
9d9163fb | 4012 | rex->offs[0].start = locinput - reginfo->strbeg; |
4d5016e5 | 4013 | PUSH_STATE_GOTO(KEEPS_next, next, locinput); |
878531d3 | 4014 | assert(0); /*NOTREACHED*/ |
ee9b8eae YO |
4015 | case KEEPS_next_fail: |
4016 | /* rollback the start point change */ | |
b93070ed | 4017 | rex->offs[0].start = st->u.keeper.val; |
ee9b8eae | 4018 | sayNO_SILENT; |
878531d3 | 4019 | assert(0); /*NOTREACHED*/ |
3c0563b9 | 4020 | |
3c0563b9 | 4021 | case MEOL: /* /..$/m */ |
7016d6eb | 4022 | if (!NEXTCHR_IS_EOS && nextchr != '\n') |
b8c5462f | 4023 | sayNO; |
b8c5462f | 4024 | break; |
3c0563b9 | 4025 | |
62e16618 KW |
4026 | case EOL: /* /..$/ */ |
4027 | /* FALL THROUGH */ | |
3c0563b9 | 4028 | case SEOL: /* /..$/s */ |
7016d6eb | 4029 | if (!NEXTCHR_IS_EOS && nextchr != '\n') |
b8c5462f | 4030 | sayNO; |
220db18a | 4031 | if (reginfo->strend - locinput > 1) |
b8c5462f | 4032 | sayNO; |
b8c5462f | 4033 | break; |
3c0563b9 DM |
4034 | |
4035 | case EOS: /* \z */ | |
7016d6eb | 4036 | if (!NEXTCHR_IS_EOS) |
b8c5462f | 4037 | sayNO; |
d6a28714 | 4038 | break; |
3c0563b9 DM |
4039 | |
4040 | case SANY: /* /./s */ | |
7016d6eb | 4041 | if (NEXTCHR_IS_EOS) |
4633a7c4 | 4042 | sayNO; |
28b98f76 | 4043 | goto increment_locinput; |
3c0563b9 DM |
4044 | |
4045 | case CANY: /* \C */ | |
7016d6eb | 4046 | if (NEXTCHR_IS_EOS) |
f33976b4 | 4047 | sayNO; |
3640db6b | 4048 | locinput++; |
a0d0e21e | 4049 | break; |
3c0563b9 DM |
4050 | |
4051 | case REG_ANY: /* /./ */ | |
7016d6eb | 4052 | if ((NEXTCHR_IS_EOS) || nextchr == '\n') |
1aa99e6b | 4053 | sayNO; |
28b98f76 DM |
4054 | goto increment_locinput; |
4055 | ||
166ba7cd DM |
4056 | |
4057 | #undef ST | |
4058 | #define ST st->u.trie | |
3c0563b9 | 4059 | case TRIEC: /* (ab|cd) with known charclass */ |
786e8c11 YO |
4060 | /* In this case the charclass data is available inline so |
4061 | we can fail fast without a lot of extra overhead. | |
4062 | */ | |
7016d6eb | 4063 | if(!NEXTCHR_IS_EOS && !ANYOF_BITMAP_TEST(scan, nextchr)) { |
fab2782b YO |
4064 | DEBUG_EXECUTE_r( |
4065 | PerlIO_printf(Perl_debug_log, | |
4066 | "%*s %sfailed to match trie start class...%s\n", | |
4067 | REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5]) | |
4068 | ); | |
4069 | sayNO_SILENT; | |
118e2215 | 4070 | assert(0); /* NOTREACHED */ |
786e8c11 YO |
4071 | } |
4072 | /* FALL THROUGH */ | |
3c0563b9 | 4073 | case TRIE: /* (ab|cd) */ |
2e64971a DM |
4074 | /* the basic plan of execution of the trie is: |
4075 | * At the beginning, run though all the states, and | |
4076 | * find the longest-matching word. Also remember the position | |
4077 | * of the shortest matching word. For example, this pattern: | |
4078 | * 1 2 3 4 5 | |
4079 | * ab|a|x|abcd|abc | |
4080 | * when matched against the string "abcde", will generate | |
4081 | * accept states for all words except 3, with the longest | |
895cc420 | 4082 | * matching word being 4, and the shortest being 2 (with |
2e64971a DM |
4083 | * the position being after char 1 of the string). |
4084 | * | |
4085 | * Then for each matching word, in word order (i.e. 1,2,4,5), | |
4086 | * we run the remainder of the pattern; on each try setting | |
4087 | * the current position to the character following the word, | |
4088 | * returning to try the next word on failure. | |
4089 | * | |
4090 | * We avoid having to build a list of words at runtime by | |
4091 | * using a compile-time structure, wordinfo[].prev, which | |
4092 | * gives, for each word, the previous accepting word (if any). | |
4093 | * In the case above it would contain the mappings 1->2, 2->0, | |
4094 | * 3->0, 4->5, 5->1. We can use this table to generate, from | |
4095 | * the longest word (4 above), a list of all words, by | |
4096 | * following the list of prev pointers; this gives us the | |
4097 | * unordered list 4,5,1,2. Then given the current word we have | |
4098 | * just tried, we can go through the list and find the | |
4099 | * next-biggest word to try (so if we just failed on word 2, | |
4100 | * the next in the list is 4). | |
4101 | * | |
4102 | * Since at runtime we don't record the matching position in | |
4103 | * the string for each word, we have to work that out for | |
4104 | * each word we're about to process. The wordinfo table holds | |
4105 | * the character length of each word; given that we recorded | |
4106 | * at the start: the position of the shortest word and its | |
4107 | * length in chars, we just need to move the pointer the | |
4108 | * difference between the two char lengths. Depending on | |
4109 | * Unicode status and folding, that's cheap or expensive. | |
4110 | * | |
4111 | * This algorithm is optimised for the case where are only a | |
4112 | * small number of accept states, i.e. 0,1, or maybe 2. | |
4113 | * With lots of accepts states, and having to try all of them, | |
4114 | * it becomes quadratic on number of accept states to find all | |
4115 | * the next words. | |
4116 | */ | |
4117 | ||
3dab1dad | 4118 | { |
07be1b83 | 4119 | /* what type of TRIE am I? (utf8 makes this contextual) */ |
a0a388a1 | 4120 | DECL_TRIE_TYPE(scan); |
3dab1dad YO |
4121 | |
4122 | /* what trie are we using right now */ | |
be8e71aa | 4123 | reg_trie_data * const trie |
f8fc2ecf | 4124 | = (reg_trie_data*)rexi->data->data[ ARG( scan ) ]; |
85fbaab2 | 4125 | HV * widecharmap = MUTABLE_HV(rexi->data->data[ ARG( scan ) + 1 ]); |
3dab1dad | 4126 | U32 state = trie->startstate; |
166ba7cd | 4127 | |
7016d6eb DM |
4128 | if ( trie->bitmap |
4129 | && (NEXTCHR_IS_EOS || !TRIE_BITMAP_TEST(trie, nextchr))) | |
4130 | { | |
3dab1dad YO |
4131 | if (trie->states[ state ].wordnum) { |
4132 | DEBUG_EXECUTE_r( | |
4133 | PerlIO_printf(Perl_debug_log, | |
4134 | "%*s %smatched empty string...%s\n", | |
5bc10b2c | 4135 | REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5]) |
3dab1dad | 4136 | ); |
20dbff7c YO |
4137 | if (!trie->jump) |
4138 | break; | |
3dab1dad YO |
4139 | } else { |
4140 | DEBUG_EXECUTE_r( | |
4141 | PerlIO_printf(Perl_debug_log, | |
786e8c11 | 4142 | "%*s %sfailed to match trie start class...%s\n", |
5bc10b2c | 4143 | REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5]) |
3dab1dad YO |
4144 | ); |
4145 | sayNO_SILENT; | |
4146 | } | |
4147 | } | |
166ba7cd | 4148 | |
786e8c11 YO |
4149 | { |
4150 | U8 *uc = ( U8* )locinput; | |
4151 | ||
4152 | STRLEN len = 0; | |
4153 | STRLEN foldlen = 0; | |
4154 | U8 *uscan = (U8*)NULL; | |
786e8c11 | 4155 | U8 foldbuf[ UTF8_MAXBYTES_CASE + 1 ]; |
2e64971a DM |
4156 | U32 charcount = 0; /* how many input chars we have matched */ |
4157 | U32 accepted = 0; /* have we seen any accepting states? */ | |
786e8c11 | 4158 | |
786e8c11 | 4159 | ST.jump = trie->jump; |
786e8c11 | 4160 | ST.me = scan; |
2e64971a DM |
4161 | ST.firstpos = NULL; |
4162 | ST.longfold = FALSE; /* char longer if folded => it's harder */ | |
4163 | ST.nextword = 0; | |
4164 | ||
4165 | /* fully traverse the TRIE; note the position of the | |
4166 | shortest accept state and the wordnum of the longest | |
4167 | accept state */ | |
07be1b83 | 4168 | |
220db18a | 4169 | while ( state && uc <= (U8*)(reginfo->strend) ) { |
786e8c11 | 4170 | U32 base = trie->states[ state ].trans.base; |
f9f4320a | 4171 | UV uvc = 0; |
acb909b4 | 4172 | U16 charid = 0; |
2e64971a DM |
4173 | U16 wordnum; |
4174 | wordnum = trie->states[ state ].wordnum; | |
4175 | ||
4176 | if (wordnum) { /* it's an accept state */ | |
4177 | if (!accepted) { | |
4178 | accepted = 1; | |
4179 | /* record first match position */ | |
4180 | if (ST.longfold) { | |
4181 | ST.firstpos = (U8*)locinput; | |
4182 | ST.firstchars = 0; | |
5b47454d | 4183 | } |
2e64971a DM |
4184 | else { |
4185 | ST.firstpos = uc; | |
4186 | ST.firstchars = charcount; | |
4187 | } | |
4188 | } | |
4189 | if (!ST.nextword || wordnum < ST.nextword) | |
4190 | ST.nextword = wordnum; | |
4191 | ST.topword = wordnum; | |
786e8c11 | 4192 | } |
a3621e74 | 4193 | |
07be1b83 | 4194 | DEBUG_TRIE_EXECUTE_r({ |
f2ed9b32 | 4195 | DUMP_EXEC_POS( (char *)uc, scan, utf8_target ); |
a3621e74 | 4196 | PerlIO_printf( Perl_debug_log, |
2e64971a | 4197 | "%*s %sState: %4"UVxf" Accepted: %c ", |
5bc10b2c | 4198 | 2+depth * 2, "", PL_colors[4], |
2e64971a | 4199 | (UV)state, (accepted ? 'Y' : 'N')); |
07be1b83 | 4200 | }); |
a3621e74 | 4201 | |
2e64971a | 4202 | /* read a char and goto next state */ |
220db18a | 4203 | if ( base && (foldlen || uc < (U8*)(reginfo->strend))) { |
6dd2be57 | 4204 | I32 offset; |
55eed653 NC |
4205 | REXEC_TRIE_READ_CHAR(trie_type, trie, widecharmap, uc, |
4206 | uscan, len, uvc, charid, foldlen, | |
4207 | foldbuf, uniflags); | |
2e64971a DM |
4208 | charcount++; |
4209 | if (foldlen>0) | |
4210 | ST.longfold = TRUE; | |
5b47454d | 4211 | if (charid && |
6dd2be57 DM |
4212 | ( ((offset = |
4213 | base + charid - 1 - trie->uniquecharcount)) >= 0) | |
4214 | ||
4215 | && ((U32)offset < trie->lasttrans) | |
4216 | && trie->trans[offset].check == state) | |
5b47454d | 4217 | { |
6dd2be57 | 4218 | state = trie->trans[offset].next; |
5b47454d DM |
4219 | } |
4220 | else { | |
4221 | state = 0; | |
4222 | } | |
4223 | uc += len; | |
4224 | ||
4225 | } | |
4226 | else { | |
a3621e74 YO |
4227 | state = 0; |
4228 | } | |
4229 | DEBUG_TRIE_EXECUTE_r( | |
e4584336 | 4230 | PerlIO_printf( Perl_debug_log, |
786e8c11 | 4231 | "Charid:%3x CP:%4"UVxf" After State: %4"UVxf"%s\n", |
e4584336 | 4232 | charid, uvc, (UV)state, PL_colors[5] ); |
a3621e74 YO |
4233 | ); |
4234 | } | |
2e64971a | 4235 | if (!accepted) |
a3621e74 | 4236 | sayNO; |
a3621e74 | 4237 | |
2e64971a DM |
4238 | /* calculate total number of accept states */ |
4239 | { | |
4240 | U16 w = ST.topword; | |
4241 | accepted = 0; | |
4242 | while (w) { | |
4243 | w = trie->wordinfo[w].prev; | |
4244 | accepted++; | |
4245 | } | |
4246 | ST.accepted = accepted; | |
4247 | } | |
4248 | ||
166ba7cd DM |
4249 | DEBUG_EXECUTE_r( |
4250 | PerlIO_printf( Perl_debug_log, | |
4251 | "%*s %sgot %"IVdf" possible matches%s\n", | |
5bc10b2c | 4252 | REPORT_CODE_OFF + depth * 2, "", |
166ba7cd DM |
4253 | PL_colors[4], (IV)ST.accepted, PL_colors[5] ); |
4254 | ); | |
2e64971a | 4255 | goto trie_first_try; /* jump into the fail handler */ |
786e8c11 | 4256 | }} |
118e2215 | 4257 | assert(0); /* NOTREACHED */ |
2e64971a DM |
4258 | |
4259 | case TRIE_next_fail: /* we failed - try next alternative */ | |
a059a757 DM |
4260 | { |
4261 | U8 *uc; | |
fae667d5 YO |
4262 | if ( ST.jump) { |
4263 | REGCP_UNWIND(ST.cp); | |
a8d1f4b4 | 4264 | UNWIND_PAREN(ST.lastparen, ST.lastcloseparen); |
fae667d5 | 4265 | } |
2e64971a DM |
4266 | if (!--ST.accepted) { |
4267 | DEBUG_EXECUTE_r({ | |
4268 | PerlIO_printf( Perl_debug_log, | |
4269 | "%*s %sTRIE failed...%s\n", | |
4270 | REPORT_CODE_OFF+depth*2, "", | |
4271 | PL_colors[4], | |
4272 | PL_colors[5] ); | |
4273 | }); | |
4274 | sayNO_SILENT; | |
4275 | } | |
4276 | { | |
4277 | /* Find next-highest word to process. Note that this code | |
4278 | * is O(N^2) per trie run (O(N) per branch), so keep tight */ | |
eb578fdb KW |
4279 | U16 min = 0; |
4280 | U16 word; | |
4281 | U16 const nextword = ST.nextword; | |
4282 | reg_trie_wordinfo * const wordinfo | |
2e64971a DM |
4283 | = ((reg_trie_data*)rexi->data->data[ARG(ST.me)])->wordinfo; |
4284 | for (word=ST.topword; word; word=wordinfo[word].prev) { | |
4285 | if (word > nextword && (!min || word < min)) | |
4286 | min = word; | |
4287 | } | |
4288 | ST.nextword = min; | |
4289 | } | |
4290 | ||
fae667d5 | 4291 | trie_first_try: |
5d458dd8 YO |
4292 | if (do_cutgroup) { |
4293 | do_cutgroup = 0; | |
4294 | no_final = 0; | |
4295 | } | |
fae667d5 YO |
4296 | |
4297 | if ( ST.jump) { | |
b93070ed | 4298 | ST.lastparen = rex->lastparen; |
f6033a9d | 4299 | ST.lastcloseparen = rex->lastcloseparen; |
fae667d5 | 4300 | REGCP_SET(ST.cp); |
2e64971a | 4301 | } |
a3621e74 | 4302 | |
2e64971a | 4303 | /* find start char of end of current word */ |
166ba7cd | 4304 | { |
2e64971a | 4305 | U32 chars; /* how many chars to skip */ |
2e64971a DM |
4306 | reg_trie_data * const trie |
4307 | = (reg_trie_data*)rexi->data->data[ARG(ST.me)]; | |
4308 | ||
4309 | assert((trie->wordinfo[ST.nextword].len - trie->prefixlen) | |
4310 | >= ST.firstchars); | |
4311 | chars = (trie->wordinfo[ST.nextword].len - trie->prefixlen) | |
4312 | - ST.firstchars; | |
a059a757 | 4313 | uc = ST.firstpos; |
2e64971a DM |
4314 | |
4315 | if (ST.longfold) { | |
4316 | /* the hard option - fold each char in turn and find | |
4317 | * its folded length (which may be different */ | |
4318 | U8 foldbuf[UTF8_MAXBYTES_CASE + 1]; | |
4319 | STRLEN foldlen; | |
4320 | STRLEN len; | |
d9a396a3 | 4321 | UV uvc; |
2e64971a DM |
4322 | U8 *uscan; |
4323 | ||
4324 | while (chars) { | |
f2ed9b32 | 4325 | if (utf8_target) { |
c80e42f3 | 4326 | uvc = utf8n_to_uvchr((U8*)uc, UTF8_MAXLEN, &len, |
2e64971a DM |
4327 | uniflags); |
4328 | uc += len; | |
4329 | } | |
4330 | else { | |
4331 | uvc = *uc; | |
4332 | uc++; | |
4333 | } | |
4334 | uvc = to_uni_fold(uvc, foldbuf, &foldlen); | |
4335 | uscan = foldbuf; | |
4336 | while (foldlen) { | |
4337 | if (!--chars) | |
4338 | break; | |
c80e42f3 | 4339 | uvc = utf8n_to_uvchr(uscan, UTF8_MAXLEN, &len, |
2e64971a DM |
4340 | uniflags); |
4341 | uscan += len; | |
4342 | foldlen -= len; | |
4343 | } | |
4344 | } | |
a3621e74 | 4345 | } |
2e64971a | 4346 | else { |
f2ed9b32 | 4347 | if (utf8_target) |
2e64971a DM |
4348 | while (chars--) |
4349 | uc += UTF8SKIP(uc); | |
4350 | else | |
4351 | uc += chars; | |
4352 | } | |
2e64971a | 4353 | } |
166ba7cd | 4354 | |
6603fe3e DM |
4355 | scan = ST.me + ((ST.jump && ST.jump[ST.nextword]) |
4356 | ? ST.jump[ST.nextword] | |
4357 | : NEXT_OFF(ST.me)); | |
166ba7cd | 4358 | |
2e64971a DM |
4359 | DEBUG_EXECUTE_r({ |
4360 | PerlIO_printf( Perl_debug_log, | |
4361 | "%*s %sTRIE matched word #%d, continuing%s\n", | |
4362 | REPORT_CODE_OFF+depth*2, "", | |
4363 | PL_colors[4], | |
4364 | ST.nextword, | |
4365 | PL_colors[5] | |
4366 | ); | |
4367 | }); | |
4368 | ||
4369 | if (ST.accepted > 1 || has_cutgroup) { | |
a059a757 | 4370 | PUSH_STATE_GOTO(TRIE_next, scan, (char*)uc); |
118e2215 | 4371 | assert(0); /* NOTREACHED */ |
166ba7cd | 4372 | } |
2e64971a DM |
4373 | /* only one choice left - just continue */ |
4374 | DEBUG_EXECUTE_r({ | |
4375 | AV *const trie_words | |
4376 | = MUTABLE_AV(rexi->data->data[ARG(ST.me)+TRIE_WORDS_OFFSET]); | |
4377 | SV ** const tmp = av_fetch( trie_words, | |
4378 | ST.nextword-1, 0 ); | |
4379 | SV *sv= tmp ? sv_newmortal() : NULL; | |
4380 | ||
4381 | PerlIO_printf( Perl_debug_log, | |
4382 | "%*s %sonly one match left, short-circuiting: #%d <%s>%s\n", | |
4383 | REPORT_CODE_OFF+depth*2, "", PL_colors[4], | |
4384 | ST.nextword, | |
4385 | tmp ? pv_pretty(sv, SvPV_nolen_const(*tmp), SvCUR(*tmp), 0, | |
4386 | PL_colors[0], PL_colors[1], | |
c89df6cf | 4387 | (SvUTF8(*tmp) ? PERL_PV_ESCAPE_UNI : 0)|PERL_PV_ESCAPE_NONASCII |
2e64971a DM |
4388 | ) |
4389 | : "not compiled under -Dr", | |
4390 | PL_colors[5] ); | |
4391 | }); | |
4392 | ||
a059a757 | 4393 | locinput = (char*)uc; |
2e64971a | 4394 | continue; /* execute rest of RE */ |
118e2215 | 4395 | assert(0); /* NOTREACHED */ |
a059a757 | 4396 | } |
166ba7cd DM |
4397 | #undef ST |
4398 | ||
3c0563b9 | 4399 | case EXACT: { /* /abc/ */ |
95b24440 | 4400 | char *s = STRING(scan); |
24d3c4a9 | 4401 | ln = STR_LEN(scan); |
984e6dd1 | 4402 | if (utf8_target != is_utf8_pat) { |
bc517b45 | 4403 | /* The target and the pattern have differing utf8ness. */ |
1aa99e6b | 4404 | char *l = locinput; |
24d3c4a9 | 4405 | const char * const e = s + ln; |
a72c7584 | 4406 | |
f2ed9b32 | 4407 | if (utf8_target) { |
e6a3850e KW |
4408 | /* The target is utf8, the pattern is not utf8. |
4409 | * Above-Latin1 code points can't match the pattern; | |
4410 | * invariants match exactly, and the other Latin1 ones need | |
4411 | * to be downgraded to a single byte in order to do the | |
4412 | * comparison. (If we could be confident that the target | |
4413 | * is not malformed, this could be refactored to have fewer | |
4414 | * tests by just assuming that if the first bytes match, it | |
4415 | * is an invariant, but there are tests in the test suite | |
4416 | * dealing with (??{...}) which violate this) */ | |
1aa99e6b | 4417 | while (s < e) { |
220db18a DM |
4418 | if (l >= reginfo->strend |
4419 | || UTF8_IS_ABOVE_LATIN1(* (U8*) l)) | |
4420 | { | |
e6a3850e KW |
4421 | sayNO; |
4422 | } | |
4423 | if (UTF8_IS_INVARIANT(*(U8*)l)) { | |
4424 | if (*l != *s) { | |
4425 | sayNO; | |
4426 | } | |
4427 | l++; | |
4428 | } | |
4429 | else { | |
94bb8c36 KW |
4430 | if (TWO_BYTE_UTF8_TO_NATIVE(*l, *(l+1)) != * (U8*) s) |
4431 | { | |
e6a3850e KW |
4432 | sayNO; |
4433 | } | |
4434 | l += 2; | |
4435 | } | |
4436 | s++; | |
1aa99e6b | 4437 | } |
5ff6fc6d JH |
4438 | } |
4439 | else { | |
4440 | /* The target is not utf8, the pattern is utf8. */ | |
1aa99e6b | 4441 | while (s < e) { |
220db18a DM |
4442 | if (l >= reginfo->strend |
4443 | || UTF8_IS_ABOVE_LATIN1(* (U8*) s)) | |
e6a3850e KW |
4444 | { |
4445 | sayNO; | |
4446 | } | |
4447 | if (UTF8_IS_INVARIANT(*(U8*)s)) { | |
4448 | if (*s != *l) { | |
4449 | sayNO; | |
4450 | } | |
4451 | s++; | |
4452 | } | |
4453 | else { | |
94bb8c36 KW |
4454 | if (TWO_BYTE_UTF8_TO_NATIVE(*s, *(s+1)) != * (U8*) l) |
4455 | { | |
e6a3850e KW |
4456 | sayNO; |
4457 | } | |
4458 | s += 2; | |
4459 | } | |
4460 | l++; | |
1aa99e6b | 4461 | } |
5ff6fc6d | 4462 | } |
1aa99e6b | 4463 | locinput = l; |
1aa99e6b | 4464 | } |
5ac65bff KW |
4465 | else { |
4466 | /* The target and the pattern have the same utf8ness. */ | |
4467 | /* Inline the first character, for speed. */ | |
220db18a | 4468 | if (reginfo->strend - locinput < ln |
5ac65bff KW |
4469 | || UCHARAT(s) != nextchr |
4470 | || (ln > 1 && memNE(s, locinput, ln))) | |
4471 | { | |
4472 | sayNO; | |
4473 | } | |
4474 | locinput += ln; | |
4475 | } | |
d6a28714 | 4476 | break; |
95b24440 | 4477 | } |
7016d6eb | 4478 | |
3c0563b9 | 4479 | case EXACTFL: { /* /abc/il */ |
a932d541 | 4480 | re_fold_t folder; |
9a5a5549 KW |
4481 | const U8 * fold_array; |
4482 | const char * s; | |
d513472c | 4483 | U32 fold_utf8_flags; |
9a5a5549 | 4484 | |
f67f9e53 KW |
4485 | folder = foldEQ_locale; |
4486 | fold_array = PL_fold_locale; | |
cea315b6 | 4487 | fold_utf8_flags = FOLDEQ_LOCALE; |
9a5a5549 KW |
4488 | goto do_exactf; |
4489 | ||
3c0563b9 | 4490 | case EXACTFU_SS: /* /\x{df}/iu */ |
3c0563b9 | 4491 | case EXACTFU: /* /abc/iu */ |
9a5a5549 KW |
4492 | folder = foldEQ_latin1; |
4493 | fold_array = PL_fold_latin1; | |
984e6dd1 | 4494 | fold_utf8_flags = is_utf8_pat ? FOLDEQ_S1_ALREADY_FOLDED : 0; |
9a5a5549 KW |
4495 | goto do_exactf; |
4496 | ||
098b07d5 KW |
4497 | case EXACTFA_NO_TRIE: /* This node only generated for non-utf8 |
4498 | patterns */ | |
4499 | assert(! is_utf8_pat); | |
4500 | /* FALL THROUGH */ | |
3c0563b9 | 4501 | case EXACTFA: /* /abc/iaa */ |
2f7f8cb1 KW |
4502 | folder = foldEQ_latin1; |
4503 | fold_array = PL_fold_latin1; | |
57014d77 | 4504 | fold_utf8_flags = FOLDEQ_UTF8_NOMIX_ASCII; |
2f7f8cb1 KW |
4505 | goto do_exactf; |
4506 | ||
2fdb7295 KW |
4507 | case EXACTF: /* /abc/i This node only generated for |
4508 | non-utf8 patterns */ | |
4509 | assert(! is_utf8_pat); | |
9a5a5549 KW |
4510 | folder = foldEQ; |
4511 | fold_array = PL_fold; | |
62bf7766 | 4512 | fold_utf8_flags = 0; |
9a5a5549 KW |
4513 | |
4514 | do_exactf: | |
4515 | s = STRING(scan); | |
24d3c4a9 | 4516 | ln = STR_LEN(scan); |
d6a28714 | 4517 | |
31f05a37 KW |
4518 | if (utf8_target |
4519 | || is_utf8_pat | |
4520 | || state_num == EXACTFU_SS | |
4521 | || (state_num == EXACTFL && IN_UTF8_CTYPE_LOCALE)) | |
4522 | { | |
3c760661 KW |
4523 | /* Either target or the pattern are utf8, or has the issue where |
4524 | * the fold lengths may differ. */ | |
be8e71aa | 4525 | const char * const l = locinput; |
220db18a | 4526 | char *e = reginfo->strend; |
bc517b45 | 4527 | |
984e6dd1 | 4528 | if (! foldEQ_utf8_flags(s, 0, ln, is_utf8_pat, |
fa5b1667 | 4529 | l, &e, 0, utf8_target, fold_utf8_flags)) |
c3e1d013 KW |
4530 | { |
4531 | sayNO; | |
5486206c | 4532 | } |
d07ddd77 | 4533 | locinput = e; |
d07ddd77 | 4534 | break; |
a0ed51b3 | 4535 | } |
d6a28714 | 4536 | |
0a138b74 | 4537 | /* Neither the target nor the pattern are utf8 */ |
1443c94c DM |
4538 | if (UCHARAT(s) != nextchr |
4539 | && !NEXTCHR_IS_EOS | |
4540 | && UCHARAT(s) != fold_array[nextchr]) | |
9a5a5549 | 4541 | { |
a0ed51b3 | 4542 | sayNO; |
9a5a5549 | 4543 | } |
220db18a | 4544 | if (reginfo->strend - locinput < ln) |
b8c5462f | 4545 | sayNO; |
9a5a5549 | 4546 | if (ln > 1 && ! folder(s, locinput, ln)) |
4633a7c4 | 4547 | sayNO; |
24d3c4a9 | 4548 | locinput += ln; |
a0d0e21e | 4549 | break; |
9a5a5549 | 4550 | } |
63ac0dad KW |
4551 | |
4552 | /* XXX Could improve efficiency by separating these all out using a | |
4553 | * macro or in-line function. At that point regcomp.c would no longer | |
4554 | * have to set the FLAGS fields of these */ | |
3c0563b9 DM |
4555 | case BOUNDL: /* /\b/l */ |
4556 | case NBOUNDL: /* /\B/l */ | |
3c0563b9 DM |
4557 | case BOUND: /* /\b/ */ |
4558 | case BOUNDU: /* /\b/u */ | |
4559 | case BOUNDA: /* /\b/a */ | |
4560 | case NBOUND: /* /\B/ */ | |
4561 | case NBOUNDU: /* /\B/u */ | |
4562 | case NBOUNDA: /* /\B/a */ | |
b2680017 | 4563 | /* was last char in word? */ |
f2e96b5d KW |
4564 | if (utf8_target |
4565 | && FLAGS(scan) != REGEX_ASCII_RESTRICTED_CHARSET | |
4566 | && FLAGS(scan) != REGEX_ASCII_MORE_RESTRICTED_CHARSET) | |
4567 | { | |
9d9163fb | 4568 | if (locinput == reginfo->strbeg) |
b2680017 YO |
4569 | ln = '\n'; |
4570 | else { | |
9d9163fb DM |
4571 | const U8 * const r = |
4572 | reghop3((U8*)locinput, -1, (U8*)(reginfo->strbeg)); | |
b2680017 | 4573 | |
3db24e1e KW |
4574 | ln = utf8n_to_uvchr(r, (U8*) reginfo->strend - r, |
4575 | 0, uniflags); | |
b2680017 | 4576 | } |
63ac0dad | 4577 | if (FLAGS(scan) != REGEX_LOCALE_CHARSET) { |
0eb30aeb | 4578 | ln = isWORDCHAR_uni(ln); |
7016d6eb DM |
4579 | if (NEXTCHR_IS_EOS) |
4580 | n = 0; | |
4581 | else { | |
4582 | LOAD_UTF8_CHARCLASS_ALNUM(); | |
03940dc2 | 4583 | n = swash_fetch(PL_utf8_swash_ptrs[_CC_WORDCHAR], (U8*)locinput, |
7016d6eb DM |
4584 | utf8_target); |
4585 | } | |
b2680017 YO |
4586 | } |
4587 | else { | |
f4cd282c | 4588 | ln = isWORDCHAR_LC_uvchr(ln); |
0eb30aeb | 4589 | n = NEXTCHR_IS_EOS ? 0 : isWORDCHAR_LC_utf8((U8*)locinput); |
b2680017 YO |
4590 | } |
4591 | } | |
4592 | else { | |
cfaf538b KW |
4593 | |
4594 | /* Here the string isn't utf8, or is utf8 and only ascii | |
4595 | * characters are to match \w. In the latter case looking at | |
4596 | * the byte just prior to the current one may be just the final | |
4597 | * byte of a multi-byte character. This is ok. There are two | |
4598 | * cases: | |
4599 | * 1) it is a single byte character, and then the test is doing | |
4600 | * just what it's supposed to. | |
4601 | * 2) it is a multi-byte character, in which case the final | |
4602 | * byte is never mistakable for ASCII, and so the test | |
4603 | * will say it is not a word character, which is the | |
4604 | * correct answer. */ | |
9d9163fb | 4605 | ln = (locinput != reginfo->strbeg) ? |
b2680017 | 4606 | UCHARAT(locinput - 1) : '\n'; |
63ac0dad KW |
4607 | switch (FLAGS(scan)) { |
4608 | case REGEX_UNICODE_CHARSET: | |
4609 | ln = isWORDCHAR_L1(ln); | |
7016d6eb | 4610 | n = NEXTCHR_IS_EOS ? 0 : isWORDCHAR_L1(nextchr); |
63ac0dad KW |
4611 | break; |
4612 | case REGEX_LOCALE_CHARSET: | |
0eb30aeb KW |
4613 | ln = isWORDCHAR_LC(ln); |
4614 | n = NEXTCHR_IS_EOS ? 0 : isWORDCHAR_LC(nextchr); | |
63ac0dad KW |
4615 | break; |
4616 | case REGEX_DEPENDS_CHARSET: | |
0eb30aeb KW |
4617 | ln = isWORDCHAR(ln); |
4618 | n = NEXTCHR_IS_EOS ? 0 : isWORDCHAR(nextchr); | |
63ac0dad | 4619 | break; |
cfaf538b | 4620 | case REGEX_ASCII_RESTRICTED_CHARSET: |
c973bd4f | 4621 | case REGEX_ASCII_MORE_RESTRICTED_CHARSET: |
cfaf538b | 4622 | ln = isWORDCHAR_A(ln); |
7016d6eb | 4623 | n = NEXTCHR_IS_EOS ? 0 : isWORDCHAR_A(nextchr); |
cfaf538b | 4624 | break; |
63ac0dad KW |
4625 | default: |
4626 | Perl_croak(aTHX_ "panic: Unexpected FLAGS %u in op %u", FLAGS(scan), OP(scan)); | |
4627 | break; | |
b2680017 YO |
4628 | } |
4629 | } | |
63ac0dad KW |
4630 | /* Note requires that all BOUNDs be lower than all NBOUNDs in |
4631 | * regcomp.sym */ | |
4632 | if (((!ln) == (!n)) == (OP(scan) < NBOUND)) | |
b2680017 YO |
4633 | sayNO; |
4634 | break; | |
3c0563b9 | 4635 | |
3c0563b9 | 4636 | case ANYOF: /* /[abc]/ */ |
7016d6eb DM |
4637 | if (NEXTCHR_IS_EOS) |
4638 | sayNO; | |
e0193e47 | 4639 | if (utf8_target) { |
3db24e1e KW |
4640 | if (!reginclass(rex, scan, (U8*)locinput, (U8*)reginfo->strend, |
4641 | utf8_target)) | |
09b08e9b | 4642 | sayNO; |
635cd5d4 | 4643 | locinput += UTF8SKIP(locinput); |
ffc61ed2 JH |
4644 | } |
4645 | else { | |
20ed0b26 | 4646 | if (!REGINCLASS(rex, scan, (U8*)locinput)) |
09b08e9b | 4647 | sayNO; |
3640db6b | 4648 | locinput++; |
e0f9d4a8 | 4649 | } |
b8c5462f | 4650 | break; |
3c0563b9 | 4651 | |
3018b823 KW |
4652 | /* The argument (FLAGS) to all the POSIX node types is the class number |
4653 | * */ | |
ee9a90b8 | 4654 | |
3018b823 KW |
4655 | case NPOSIXL: /* \W or [:^punct:] etc. under /l */ |
4656 | to_complement = 1; | |
4657 | /* FALLTHROUGH */ | |
4658 | ||
4659 | case POSIXL: /* \w or [:punct:] etc. under /l */ | |
4660 | if (NEXTCHR_IS_EOS) | |
bedac28b | 4661 | sayNO; |
bedac28b | 4662 | |
3018b823 KW |
4663 | /* Use isFOO_lc() for characters within Latin1. (Note that |
4664 | * UTF8_IS_INVARIANT works even on non-UTF-8 strings, or else | |
4665 | * wouldn't be invariant) */ | |
4666 | if (UTF8_IS_INVARIANT(nextchr) || ! utf8_target) { | |
eb4e9c04 | 4667 | if (! (to_complement ^ cBOOL(isFOO_lc(FLAGS(scan), (U8) nextchr)))) { |
bedac28b KW |
4668 | sayNO; |
4669 | } | |
4670 | } | |
3018b823 KW |
4671 | else if (UTF8_IS_DOWNGRADEABLE_START(nextchr)) { |
4672 | if (! (to_complement ^ cBOOL(isFOO_lc(FLAGS(scan), | |
94bb8c36 | 4673 | (U8) TWO_BYTE_UTF8_TO_NATIVE(nextchr, |
3018b823 KW |
4674 | *(locinput + 1)))))) |
4675 | { | |
bedac28b | 4676 | sayNO; |
3018b823 | 4677 | } |
bedac28b | 4678 | } |
3018b823 KW |
4679 | else { /* Here, must be an above Latin-1 code point */ |
4680 | goto utf8_posix_not_eos; | |
bedac28b | 4681 | } |
3018b823 KW |
4682 | |
4683 | /* Here, must be utf8 */ | |
4684 | locinput += UTF8SKIP(locinput); | |
bedac28b KW |
4685 | break; |
4686 | ||
3018b823 KW |
4687 | case NPOSIXD: /* \W or [:^punct:] etc. under /d */ |
4688 | to_complement = 1; | |
4689 | /* FALLTHROUGH */ | |
4690 | ||
4691 | case POSIXD: /* \w or [:punct:] etc. under /d */ | |
bedac28b | 4692 | if (utf8_target) { |
3018b823 | 4693 | goto utf8_posix; |
bedac28b | 4694 | } |
3018b823 KW |
4695 | goto posixa; |
4696 | ||
4697 | case NPOSIXA: /* \W or [:^punct:] etc. under /a */ | |
bedac28b | 4698 | |
3018b823 | 4699 | if (NEXTCHR_IS_EOS) { |
bedac28b KW |
4700 | sayNO; |
4701 | } | |
bedac28b | 4702 | |
3018b823 KW |
4703 | /* All UTF-8 variants match */ |
4704 | if (! UTF8_IS_INVARIANT(nextchr)) { | |
4705 | goto increment_locinput; | |
bedac28b | 4706 | } |
ee9a90b8 | 4707 | |
3018b823 KW |
4708 | to_complement = 1; |
4709 | /* FALLTHROUGH */ | |
4710 | ||
4711 | case POSIXA: /* \w or [:punct:] etc. under /a */ | |
4712 | ||
4713 | posixa: | |
4714 | /* We get here through POSIXD, NPOSIXD, and NPOSIXA when not in | |
4715 | * UTF-8, and also from NPOSIXA even in UTF-8 when the current | |
4716 | * character is a single byte */ | |
20d0b1e9 | 4717 | |
3018b823 KW |
4718 | if (NEXTCHR_IS_EOS |
4719 | || ! (to_complement ^ cBOOL(_generic_isCC_A(nextchr, | |
4720 | FLAGS(scan))))) | |
4721 | { | |
0658cdde KW |
4722 | sayNO; |
4723 | } | |
3018b823 KW |
4724 | |
4725 | /* Here we are either not in utf8, or we matched a utf8-invariant, | |
4726 | * so the next char is the next byte */ | |
3640db6b | 4727 | locinput++; |
0658cdde | 4728 | break; |
3c0563b9 | 4729 | |
3018b823 KW |
4730 | case NPOSIXU: /* \W or [:^punct:] etc. under /u */ |
4731 | to_complement = 1; | |
4732 | /* FALLTHROUGH */ | |
4733 | ||
4734 | case POSIXU: /* \w or [:punct:] etc. under /u */ | |
4735 | utf8_posix: | |
4736 | if (NEXTCHR_IS_EOS) { | |
0658cdde KW |
4737 | sayNO; |
4738 | } | |
3018b823 KW |
4739 | utf8_posix_not_eos: |
4740 | ||
4741 | /* Use _generic_isCC() for characters within Latin1. (Note that | |
4742 | * UTF8_IS_INVARIANT works even on non-UTF-8 strings, or else | |
4743 | * wouldn't be invariant) */ | |
4744 | if (UTF8_IS_INVARIANT(nextchr) || ! utf8_target) { | |
4745 | if (! (to_complement ^ cBOOL(_generic_isCC(nextchr, | |
4746 | FLAGS(scan))))) | |
4747 | { | |
4748 | sayNO; | |
4749 | } | |
4750 | locinput++; | |
4751 | } | |
4752 | else if (UTF8_IS_DOWNGRADEABLE_START(nextchr)) { | |
4753 | if (! (to_complement | |
94bb8c36 | 4754 | ^ cBOOL(_generic_isCC(TWO_BYTE_UTF8_TO_NATIVE(nextchr, |
3018b823 | 4755 | *(locinput + 1)), |
94bb8c36 | 4756 | FLAGS(scan))))) |
3018b823 KW |
4757 | { |
4758 | sayNO; | |
4759 | } | |
4760 | locinput += 2; | |
4761 | } | |
4762 | else { /* Handle above Latin-1 code points */ | |
4763 | classnum = (_char_class_number) FLAGS(scan); | |
4764 | if (classnum < _FIRST_NON_SWASH_CC) { | |
4765 | ||
4766 | /* Here, uses a swash to find such code points. Load if if | |
4767 | * not done already */ | |
4768 | if (! PL_utf8_swash_ptrs[classnum]) { | |
4769 | U8 flags = _CORE_SWASH_INIT_ACCEPT_INVLIST; | |
4770 | PL_utf8_swash_ptrs[classnum] | |
4771 | = _core_swash_init("utf8", | |
2a16ac92 KW |
4772 | "", |
4773 | &PL_sv_undef, 1, 0, | |
4774 | PL_XPosix_ptrs[classnum], &flags); | |
3018b823 KW |
4775 | } |
4776 | if (! (to_complement | |
4777 | ^ cBOOL(swash_fetch(PL_utf8_swash_ptrs[classnum], | |
4778 | (U8 *) locinput, TRUE)))) | |
4779 | { | |
4780 | sayNO; | |
4781 | } | |
4782 | } | |
4783 | else { /* Here, uses macros to find above Latin-1 code points */ | |
4784 | switch (classnum) { | |
4785 | case _CC_ENUM_SPACE: /* XXX would require separate | |
4786 | code if we revert the change | |
4787 | of \v matching this */ | |
4788 | case _CC_ENUM_PSXSPC: | |
4789 | if (! (to_complement | |
4790 | ^ cBOOL(is_XPERLSPACE_high(locinput)))) | |
4791 | { | |
4792 | sayNO; | |
4793 | } | |
4794 | break; | |
4795 | case _CC_ENUM_BLANK: | |
4796 | if (! (to_complement | |
4797 | ^ cBOOL(is_HORIZWS_high(locinput)))) | |
4798 | { | |
4799 | sayNO; | |
4800 | } | |
4801 | break; | |
4802 | case _CC_ENUM_XDIGIT: | |
4803 | if (! (to_complement | |
4804 | ^ cBOOL(is_XDIGIT_high(locinput)))) | |
4805 | { | |
4806 | sayNO; | |
4807 | } | |
4808 | break; | |
4809 | case _CC_ENUM_VERTSPACE: | |
4810 | if (! (to_complement | |
4811 | ^ cBOOL(is_VERTWS_high(locinput)))) | |
4812 | { | |
4813 | sayNO; | |
4814 | } | |
4815 | break; | |
4816 | default: /* The rest, e.g. [:cntrl:], can't match | |
4817 | above Latin1 */ | |
4818 | if (! to_complement) { | |
4819 | sayNO; | |
4820 | } | |
4821 | break; | |
4822 | } | |
4823 | } | |
4824 | locinput += UTF8SKIP(locinput); | |
4825 | } | |
4826 | break; | |
0658cdde | 4827 | |
37e2e78e KW |
4828 | case CLUMP: /* Match \X: logical Unicode character. This is defined as |
4829 | a Unicode extended Grapheme Cluster */ | |
4830 | /* From http://www.unicode.org/reports/tr29 (5.2 version). An | |
4831 | extended Grapheme Cluster is: | |
4832 | ||
7aee35ff KW |
4833 | CR LF |
4834 | | Prepend* Begin Extend* | |
4835 | | . | |
37e2e78e | 4836 | |
7aee35ff KW |
4837 | Begin is: ( Special_Begin | ! Control ) |
4838 | Special_Begin is: ( Regional-Indicator+ | Hangul-syllable ) | |
4839 | Extend is: ( Grapheme_Extend | Spacing_Mark ) | |
4840 | Control is: [ GCB_Control | CR | LF ] | |
4841 | Hangul-syllable is: ( T+ | ( L* ( L | ( LVT | ( V | LV ) V* ) T* ) )) | |
37e2e78e | 4842 | |
27d4fc33 KW |
4843 | If we create a 'Regular_Begin' = Begin - Special_Begin, then |
4844 | we can rewrite | |
4845 | ||
4846 | Begin is ( Regular_Begin + Special Begin ) | |
4847 | ||
4848 | It turns out that 98.4% of all Unicode code points match | |
4849 | Regular_Begin. Doing it this way eliminates a table match in | |
c101f46d | 4850 | the previous implementation for almost all Unicode code points. |
27d4fc33 | 4851 | |
37e2e78e KW |
4852 | There is a subtlety with Prepend* which showed up in testing. |
4853 | Note that the Begin, and only the Begin is required in: | |
4854 | | Prepend* Begin Extend* | |
cc3b396d KW |
4855 | Also, Begin contains '! Control'. A Prepend must be a |
4856 | '! Control', which means it must also be a Begin. What it | |
4857 | comes down to is that if we match Prepend* and then find no | |
4858 | suitable Begin afterwards, that if we backtrack the last | |
4859 | Prepend, that one will be a suitable Begin. | |
37e2e78e KW |
4860 | */ |
4861 | ||
7016d6eb | 4862 | if (NEXTCHR_IS_EOS) |
a0ed51b3 | 4863 | sayNO; |
f2ed9b32 | 4864 | if (! utf8_target) { |
37e2e78e KW |
4865 | |
4866 | /* Match either CR LF or '.', as all the other possibilities | |
4867 | * require utf8 */ | |
4868 | locinput++; /* Match the . or CR */ | |
cc3b396d KW |
4869 | if (nextchr == '\r' /* And if it was CR, and the next is LF, |
4870 | match the LF */ | |
220db18a | 4871 | && locinput < reginfo->strend |
e699a1d5 KW |
4872 | && UCHARAT(locinput) == '\n') |
4873 | { | |
4874 | locinput++; | |
4875 | } | |
37e2e78e KW |
4876 | } |
4877 | else { | |
4878 | ||
4879 | /* Utf8: See if is ( CR LF ); already know that locinput < | |
220db18a DM |
4880 | * reginfo->strend, so locinput+1 is in bounds */ |
4881 | if ( nextchr == '\r' && locinput+1 < reginfo->strend | |
f67f9e53 | 4882 | && UCHARAT(locinput + 1) == '\n') |
7016d6eb | 4883 | { |
37e2e78e KW |
4884 | locinput += 2; |
4885 | } | |
4886 | else { | |
45fdf108 KW |
4887 | STRLEN len; |
4888 | ||
37e2e78e KW |
4889 | /* In case have to backtrack to beginning, then match '.' */ |
4890 | char *starting = locinput; | |
4891 | ||
4892 | /* In case have to backtrack the last prepend */ | |
e699a1d5 | 4893 | char *previous_prepend = NULL; |
37e2e78e KW |
4894 | |
4895 | LOAD_UTF8_CHARCLASS_GCB(); | |
4896 | ||
45fdf108 | 4897 | /* Match (prepend)* */ |
220db18a | 4898 | while (locinput < reginfo->strend |
45fdf108 KW |
4899 | && (len = is_GCB_Prepend_utf8(locinput))) |
4900 | { | |
4901 | previous_prepend = locinput; | |
4902 | locinput += len; | |
a1853d78 | 4903 | } |
37e2e78e KW |
4904 | |
4905 | /* As noted above, if we matched a prepend character, but | |
4906 | * the next thing won't match, back off the last prepend we | |
4907 | * matched, as it is guaranteed to match the begin */ | |
4908 | if (previous_prepend | |
220db18a | 4909 | && (locinput >= reginfo->strend |
c101f46d KW |
4910 | || (! swash_fetch(PL_utf8_X_regular_begin, |
4911 | (U8*)locinput, utf8_target) | |
bff53399 | 4912 | && ! is_GCB_SPECIAL_BEGIN_START_utf8(locinput))) |
c101f46d | 4913 | ) |
37e2e78e KW |
4914 | { |
4915 | locinput = previous_prepend; | |
4916 | } | |
4917 | ||
220db18a | 4918 | /* Note that here we know reginfo->strend > locinput, as we |
37e2e78e KW |
4919 | * tested that upon input to this switch case, and if we |
4920 | * moved locinput forward, we tested the result just above | |
4921 | * and it either passed, or we backed off so that it will | |
4922 | * now pass */ | |
11dfcd49 KW |
4923 | if (swash_fetch(PL_utf8_X_regular_begin, |
4924 | (U8*)locinput, utf8_target)) { | |
27d4fc33 KW |
4925 | locinput += UTF8SKIP(locinput); |
4926 | } | |
bff53399 | 4927 | else if (! is_GCB_SPECIAL_BEGIN_START_utf8(locinput)) { |
37e2e78e KW |
4928 | |
4929 | /* Here did not match the required 'Begin' in the | |
4930 | * second term. So just match the very first | |
4931 | * character, the '.' of the final term of the regex */ | |
4932 | locinput = starting + UTF8SKIP(starting); | |
27d4fc33 | 4933 | goto exit_utf8; |
37e2e78e KW |
4934 | } else { |
4935 | ||
11dfcd49 KW |
4936 | /* Here is a special begin. It can be composed of |
4937 | * several individual characters. One possibility is | |
4938 | * RI+ */ | |
45fdf108 KW |
4939 | if ((len = is_GCB_RI_utf8(locinput))) { |
4940 | locinput += len; | |
220db18a | 4941 | while (locinput < reginfo->strend |
45fdf108 | 4942 | && (len = is_GCB_RI_utf8(locinput))) |
11dfcd49 | 4943 | { |
45fdf108 | 4944 | locinput += len; |
11dfcd49 | 4945 | } |
45fdf108 KW |
4946 | } else if ((len = is_GCB_T_utf8(locinput))) { |
4947 | /* Another possibility is T+ */ | |
4948 | locinput += len; | |
220db18a | 4949 | while (locinput < reginfo->strend |
45fdf108 | 4950 | && (len = is_GCB_T_utf8(locinput))) |
11dfcd49 | 4951 | { |
45fdf108 | 4952 | locinput += len; |
11dfcd49 KW |
4953 | } |
4954 | } else { | |
4955 | ||
4956 | /* Here, neither RI+ nor T+; must be some other | |
4957 | * Hangul. That means it is one of the others: L, | |
4958 | * LV, LVT or V, and matches: | |
4959 | * L* (L | LVT T* | V * V* T* | LV V* T*) */ | |
4960 | ||
4961 | /* Match L* */ | |
220db18a | 4962 | while (locinput < reginfo->strend |
45fdf108 | 4963 | && (len = is_GCB_L_utf8(locinput))) |
11dfcd49 | 4964 | { |
45fdf108 | 4965 | locinput += len; |
11dfcd49 | 4966 | } |
37e2e78e | 4967 | |
11dfcd49 KW |
4968 | /* Here, have exhausted L*. If the next character |
4969 | * is not an LV, LVT nor V, it means we had to have | |
4970 | * at least one L, so matches L+ in the original | |
4971 | * equation, we have a complete hangul syllable. | |
4972 | * Are done. */ | |
4973 | ||
220db18a | 4974 | if (locinput < reginfo->strend |
45fdf108 | 4975 | && is_GCB_LV_LVT_V_utf8(locinput)) |
11dfcd49 | 4976 | { |
11dfcd49 | 4977 | /* Otherwise keep going. Must be LV, LVT or V. |
7d43c479 KW |
4978 | * See if LVT, by first ruling out V, then LV */ |
4979 | if (! is_GCB_V_utf8(locinput) | |
4980 | /* All but every TCount one is LV */ | |
4981 | && (valid_utf8_to_uvchr((U8 *) locinput, | |
4982 | NULL) | |
4983 | - SBASE) | |
4984 | % TCount != 0) | |
4985 | { | |
11dfcd49 KW |
4986 | locinput += UTF8SKIP(locinput); |
4987 | } else { | |
4988 | ||
4989 | /* Must be V or LV. Take it, then match | |
4990 | * V* */ | |
4991 | locinput += UTF8SKIP(locinput); | |
220db18a | 4992 | while (locinput < reginfo->strend |
45fdf108 | 4993 | && (len = is_GCB_V_utf8(locinput))) |
11dfcd49 | 4994 | { |
45fdf108 | 4995 | locinput += len; |
11dfcd49 KW |
4996 | } |
4997 | } | |
37e2e78e | 4998 | |
11dfcd49 | 4999 | /* And any of LV, LVT, or V can be followed |
45fdf108 | 5000 | * by T* */ |
220db18a | 5001 | while (locinput < reginfo->strend |
45fdf108 | 5002 | && (len = is_GCB_T_utf8(locinput))) |
11dfcd49 | 5003 | { |
45fdf108 | 5004 | locinput += len; |
11dfcd49 KW |
5005 | } |
5006 | } | |
cd94d768 | 5007 | } |
11dfcd49 | 5008 | } |
37e2e78e | 5009 | |
11dfcd49 | 5010 | /* Match any extender */ |
220db18a | 5011 | while (locinput < reginfo->strend |
11dfcd49 KW |
5012 | && swash_fetch(PL_utf8_X_extend, |
5013 | (U8*)locinput, utf8_target)) | |
5014 | { | |
5015 | locinput += UTF8SKIP(locinput); | |
5016 | } | |
37e2e78e | 5017 | } |
27d4fc33 | 5018 | exit_utf8: |
220db18a | 5019 | if (locinput > reginfo->strend) sayNO; |
37e2e78e | 5020 | } |
a0ed51b3 | 5021 | break; |
81714fb9 | 5022 | |
3c0563b9 | 5023 | case NREFFL: /* /\g{name}/il */ |
d7ef4b73 KW |
5024 | { /* The capture buffer cases. The ones beginning with N for the |
5025 | named buffers just convert to the equivalent numbered and | |
5026 | pretend they were called as the corresponding numbered buffer | |
5027 | op. */ | |
26ecd678 TC |
5028 | /* don't initialize these in the declaration, it makes C++ |
5029 | unhappy */ | |
9d9163fb | 5030 | const char *s; |
ff1157ca | 5031 | char type; |
8368298a TC |
5032 | re_fold_t folder; |
5033 | const U8 *fold_array; | |
26ecd678 | 5034 | UV utf8_fold_flags; |
8368298a | 5035 | |
d7ef4b73 KW |
5036 | folder = foldEQ_locale; |
5037 | fold_array = PL_fold_locale; | |
5038 | type = REFFL; | |
cea315b6 | 5039 | utf8_fold_flags = FOLDEQ_LOCALE; |
d7ef4b73 KW |
5040 | goto do_nref; |
5041 | ||
3c0563b9 | 5042 | case NREFFA: /* /\g{name}/iaa */ |
2f7f8cb1 KW |
5043 | folder = foldEQ_latin1; |
5044 | fold_array = PL_fold_latin1; | |
5045 | type = REFFA; | |
5046 | utf8_fold_flags = FOLDEQ_UTF8_NOMIX_ASCII; | |
5047 | goto do_nref; | |
5048 | ||
3c0563b9 | 5049 | case NREFFU: /* /\g{name}/iu */ |
d7ef4b73 KW |
5050 | folder = foldEQ_latin1; |
5051 | fold_array = PL_fold_latin1; | |
5052 | type = REFFU; | |
d513472c | 5053 | utf8_fold_flags = 0; |
d7ef4b73 KW |
5054 | goto do_nref; |
5055 | ||
3c0563b9 | 5056 | case NREFF: /* /\g{name}/i */ |
d7ef4b73 KW |
5057 | folder = foldEQ; |
5058 | fold_array = PL_fold; | |
5059 | type = REFF; | |
d513472c | 5060 | utf8_fold_flags = 0; |
d7ef4b73 KW |
5061 | goto do_nref; |
5062 | ||
3c0563b9 | 5063 | case NREF: /* /\g{name}/ */ |
d7ef4b73 | 5064 | type = REF; |
83d7b90b KW |
5065 | folder = NULL; |
5066 | fold_array = NULL; | |
d513472c | 5067 | utf8_fold_flags = 0; |
d7ef4b73 KW |
5068 | do_nref: |
5069 | ||
5070 | /* For the named back references, find the corresponding buffer | |
5071 | * number */ | |
0a4db386 YO |
5072 | n = reg_check_named_buff_matched(rex,scan); |
5073 | ||
d7ef4b73 | 5074 | if ( ! n ) { |
81714fb9 | 5075 | sayNO; |
d7ef4b73 KW |
5076 | } |
5077 | goto do_nref_ref_common; | |
5078 | ||
3c0563b9 | 5079 | case REFFL: /* /\1/il */ |
d7ef4b73 KW |
5080 | folder = foldEQ_locale; |
5081 | fold_array = PL_fold_locale; | |
cea315b6 | 5082 | utf8_fold_flags = FOLDEQ_LOCALE; |
d7ef4b73 KW |
5083 | goto do_ref; |
5084 | ||
3c0563b9 | 5085 | case REFFA: /* /\1/iaa */ |
2f7f8cb1 KW |
5086 | folder = foldEQ_latin1; |
5087 | fold_array = PL_fold_latin1; | |
5088 | utf8_fold_flags = FOLDEQ_UTF8_NOMIX_ASCII; | |
5089 | goto do_ref; | |
5090 | ||
3c0563b9 | 5091 | case REFFU: /* /\1/iu */ |
d7ef4b73 KW |
5092 | folder = foldEQ_latin1; |
5093 | fold_array = PL_fold_latin1; | |
d513472c | 5094 | utf8_fold_flags = 0; |
d7ef4b73 KW |
5095 | goto do_ref; |
5096 | ||
3c0563b9 | 5097 | case REFF: /* /\1/i */ |
d7ef4b73 KW |
5098 | folder = foldEQ; |
5099 | fold_array = PL_fold; | |
d513472c | 5100 | utf8_fold_flags = 0; |
83d7b90b | 5101 | goto do_ref; |
d7ef4b73 | 5102 | |
3c0563b9 | 5103 | case REF: /* /\1/ */ |
83d7b90b KW |
5104 | folder = NULL; |
5105 | fold_array = NULL; | |
d513472c | 5106 | utf8_fold_flags = 0; |
83d7b90b | 5107 | |
d7ef4b73 | 5108 | do_ref: |
81714fb9 | 5109 | type = OP(scan); |
d7ef4b73 KW |
5110 | n = ARG(scan); /* which paren pair */ |
5111 | ||
5112 | do_nref_ref_common: | |
b93070ed | 5113 | ln = rex->offs[n].start; |
1cb48e53 | 5114 | reginfo->poscache_iter = reginfo->poscache_maxiter; /* Void cache */ |
b93070ed | 5115 | if (rex->lastparen < n || ln == -1) |
af3f8c16 | 5116 | sayNO; /* Do not match unless seen CLOSEn. */ |
b93070ed | 5117 | if (ln == rex->offs[n].end) |
a0d0e21e | 5118 | break; |
a0ed51b3 | 5119 | |
9d9163fb | 5120 | s = reginfo->strbeg + ln; |
d7ef4b73 | 5121 | if (type != REF /* REF can do byte comparison */ |
31f05a37 KW |
5122 | && (utf8_target || type == REFFU || type == REFFL)) |
5123 | { | |
220db18a | 5124 | char * limit = reginfo->strend; |
d7ef4b73 KW |
5125 | |
5126 | /* This call case insensitively compares the entire buffer | |
5127 | * at s, with the current input starting at locinput, but | |
220db18a DM |
5128 | * not going off the end given by reginfo->strend, and |
5129 | * returns in <limit> upon success, how much of the | |
5130 | * current input was matched */ | |
b93070ed | 5131 | if (! foldEQ_utf8_flags(s, NULL, rex->offs[n].end - ln, utf8_target, |
d513472c | 5132 | locinput, &limit, 0, utf8_target, utf8_fold_flags)) |
d7ef4b73 KW |
5133 | { |
5134 | sayNO; | |
a0ed51b3 | 5135 | } |
d7ef4b73 | 5136 | locinput = limit; |
a0ed51b3 LW |
5137 | break; |
5138 | } | |
5139 | ||
d7ef4b73 | 5140 | /* Not utf8: Inline the first character, for speed. */ |
7016d6eb DM |
5141 | if (!NEXTCHR_IS_EOS && |
5142 | UCHARAT(s) != nextchr && | |
81714fb9 | 5143 | (type == REF || |
d7ef4b73 | 5144 | UCHARAT(s) != fold_array[nextchr])) |
4633a7c4 | 5145 | sayNO; |
b93070ed | 5146 | ln = rex->offs[n].end - ln; |
220db18a | 5147 | if (locinput + ln > reginfo->strend) |
4633a7c4 | 5148 | sayNO; |
81714fb9 | 5149 | if (ln > 1 && (type == REF |
24d3c4a9 | 5150 | ? memNE(s, locinput, ln) |
d7ef4b73 | 5151 | : ! folder(s, locinput, ln))) |
4633a7c4 | 5152 | sayNO; |
24d3c4a9 | 5153 | locinput += ln; |
a0d0e21e | 5154 | break; |
81714fb9 | 5155 | } |
3c0563b9 DM |
5156 | |
5157 | case NOTHING: /* null op; e.g. the 'nothing' following | |
5158 | * the '*' in m{(a+|b)*}' */ | |
5159 | break; | |
5160 | case TAIL: /* placeholder while compiling (A|B|C) */ | |
a0d0e21e | 5161 | break; |
3c0563b9 DM |
5162 | |
5163 | case BACK: /* ??? doesn't appear to be used ??? */ | |
a0d0e21e | 5164 | break; |
40a82448 DM |
5165 | |
5166 | #undef ST | |
5167 | #define ST st->u.eval | |
c277df42 | 5168 | { |
c277df42 | 5169 | SV *ret; |
d2f13c59 | 5170 | REGEXP *re_sv; |
6bda09f9 | 5171 | regexp *re; |
f8fc2ecf | 5172 | regexp_internal *rei; |
1a147d38 YO |
5173 | regnode *startpoint; |
5174 | ||
3c0563b9 | 5175 | case GOSTART: /* (?R) */ |
e7707071 YO |
5176 | case GOSUB: /* /(...(?1))/ /(...(?&foo))/ */ |
5177 | if (cur_eval && cur_eval->locinput==locinput) { | |
24b23f37 | 5178 | if (cur_eval->u.eval.close_paren == (U32)ARG(scan)) |
1a147d38 | 5179 | Perl_croak(aTHX_ "Infinite recursion in regex"); |
4b196cd4 | 5180 | if ( ++nochange_depth > max_nochange_depth ) |
1a147d38 YO |
5181 | Perl_croak(aTHX_ |
5182 | "Pattern subroutine nesting without pos change" | |
5183 | " exceeded limit in regex"); | |
6bda09f9 YO |
5184 | } else { |
5185 | nochange_depth = 0; | |
1a147d38 | 5186 | } |
288b8c02 | 5187 | re_sv = rex_sv; |
6bda09f9 | 5188 | re = rex; |
f8fc2ecf | 5189 | rei = rexi; |
1a147d38 | 5190 | if (OP(scan)==GOSUB) { |
6bda09f9 YO |
5191 | startpoint = scan + ARG2L(scan); |
5192 | ST.close_paren = ARG(scan); | |
5193 | } else { | |
f8fc2ecf | 5194 | startpoint = rei->program+1; |
6bda09f9 YO |
5195 | ST.close_paren = 0; |
5196 | } | |
d1b2014a YO |
5197 | |
5198 | /* Save all the positions seen so far. */ | |
5199 | ST.cp = regcppush(rex, 0, maxopenparen); | |
5200 | REGCP_SET(ST.lastcp); | |
5201 | ||
5202 | /* and then jump to the code we share with EVAL */ | |
6bda09f9 | 5203 | goto eval_recurse_doit; |
d1b2014a | 5204 | |
118e2215 | 5205 | assert(0); /* NOTREACHED */ |
3c0563b9 | 5206 | |
6bda09f9 YO |
5207 | case EVAL: /* /(?{A})B/ /(??{A})B/ and /(?(?{A})X|Y)B/ */ |
5208 | if (cur_eval && cur_eval->locinput==locinput) { | |
4b196cd4 | 5209 | if ( ++nochange_depth > max_nochange_depth ) |
1a147d38 | 5210 | Perl_croak(aTHX_ "EVAL without pos change exceeded limit in regex"); |
6bda09f9 YO |
5211 | } else { |
5212 | nochange_depth = 0; | |
5213 | } | |
8e5e9ebe | 5214 | { |
4aabdb9b | 5215 | /* execute the code in the {...} */ |
81ed78b2 | 5216 | |
4aabdb9b | 5217 | dSP; |
a6dc34f1 | 5218 | IV before; |
1f4d1a1e | 5219 | OP * const oop = PL_op; |
4aabdb9b | 5220 | COP * const ocurcop = PL_curcop; |
81ed78b2 | 5221 | OP *nop; |
81ed78b2 | 5222 | CV *newcv; |
91332126 | 5223 | |
74088413 | 5224 | /* save *all* paren positions */ |
92da3157 | 5225 | regcppush(rex, 0, maxopenparen); |
74088413 DM |
5226 | REGCP_SET(runops_cp); |
5227 | ||
81ed78b2 DM |
5228 | if (!caller_cv) |
5229 | caller_cv = find_runcv(NULL); | |
5230 | ||
4aabdb9b | 5231 | n = ARG(scan); |
81ed78b2 | 5232 | |
b30fcab9 | 5233 | if (rexi->data->what[n] == 'r') { /* code from an external qr */ |
8d919b0a | 5234 | newcv = (ReANY( |
b30fcab9 DM |
5235 | (REGEXP*)(rexi->data->data[n]) |
5236 | ))->qr_anoncv | |
81ed78b2 DM |
5237 | ; |
5238 | nop = (OP*)rexi->data->data[n+1]; | |
b30fcab9 DM |
5239 | } |
5240 | else if (rexi->data->what[n] == 'l') { /* literal code */ | |
81ed78b2 DM |
5241 | newcv = caller_cv; |
5242 | nop = (OP*)rexi->data->data[n]; | |
5243 | assert(CvDEPTH(newcv)); | |
68e2671b DM |
5244 | } |
5245 | else { | |
d24ca0c5 DM |
5246 | /* literal with own CV */ |
5247 | assert(rexi->data->what[n] == 'L'); | |
81ed78b2 DM |
5248 | newcv = rex->qr_anoncv; |
5249 | nop = (OP*)rexi->data->data[n]; | |
68e2671b | 5250 | } |
81ed78b2 | 5251 | |
0e458318 DM |
5252 | /* normally if we're about to execute code from the same |
5253 | * CV that we used previously, we just use the existing | |
5254 | * CX stack entry. However, its possible that in the | |
5255 | * meantime we may have backtracked, popped from the save | |
5256 | * stack, and undone the SAVECOMPPAD(s) associated with | |
5257 | * PUSH_MULTICALL; in which case PL_comppad no longer | |
5258 | * points to newcv's pad. */ | |
5259 | if (newcv != last_pushed_cv || PL_comppad != last_pad) | |
5260 | { | |
b0065247 DM |
5261 | U8 flags = (CXp_SUB_RE | |
5262 | ((newcv == caller_cv) ? CXp_SUB_RE_FAKE : 0)); | |
0e458318 | 5263 | if (last_pushed_cv) { |
b0065247 | 5264 | CHANGE_MULTICALL_FLAGS(newcv, flags); |
0e458318 DM |
5265 | } |
5266 | else { | |
b0065247 | 5267 | PUSH_MULTICALL_FLAGS(newcv, flags); |
0e458318 DM |
5268 | } |
5269 | last_pushed_cv = newcv; | |
5270 | } | |
c31ee3bb DM |
5271 | else { |
5272 | /* these assignments are just to silence compiler | |
5273 | * warnings */ | |
5274 | multicall_cop = NULL; | |
5275 | newsp = NULL; | |
5276 | } | |
0e458318 DM |
5277 | last_pad = PL_comppad; |
5278 | ||
2e2e3f36 DM |
5279 | /* the initial nextstate you would normally execute |
5280 | * at the start of an eval (which would cause error | |
5281 | * messages to come from the eval), may be optimised | |
5282 | * away from the execution path in the regex code blocks; | |
5283 | * so manually set PL_curcop to it initially */ | |
5284 | { | |
81ed78b2 | 5285 | OP *o = cUNOPx(nop)->op_first; |
2e2e3f36 DM |
5286 | assert(o->op_type == OP_NULL); |
5287 | if (o->op_targ == OP_SCOPE) { | |
5288 | o = cUNOPo->op_first; | |
5289 | } | |
5290 | else { | |
5291 | assert(o->op_targ == OP_LEAVE); | |
5292 | o = cUNOPo->op_first; | |
5293 | assert(o->op_type == OP_ENTER); | |
5294 | o = o->op_sibling; | |
5295 | } | |
5296 | ||
5297 | if (o->op_type != OP_STUB) { | |
5298 | assert( o->op_type == OP_NEXTSTATE | |
5299 | || o->op_type == OP_DBSTATE | |
5300 | || (o->op_type == OP_NULL | |
5301 | && ( o->op_targ == OP_NEXTSTATE | |
5302 | || o->op_targ == OP_DBSTATE | |
5303 | ) | |
5304 | ) | |
5305 | ); | |
5306 | PL_curcop = (COP*)o; | |
5307 | } | |
5308 | } | |
81ed78b2 | 5309 | nop = nop->op_next; |
2e2e3f36 | 5310 | |
24b23f37 | 5311 | DEBUG_STATE_r( PerlIO_printf(Perl_debug_log, |
81ed78b2 DM |
5312 | " re EVAL PL_op=0x%"UVxf"\n", PTR2UV(nop)) ); |
5313 | ||
8adc0f72 | 5314 | rex->offs[0].end = locinput - reginfo->strbeg; |
bf2039a9 | 5315 | if (reginfo->info_aux_eval->pos_magic) |
25fdce4a FC |
5316 | MgBYTEPOS_set(reginfo->info_aux_eval->pos_magic, |
5317 | reginfo->sv, reginfo->strbeg, | |
5318 | locinput - reginfo->strbeg); | |
4aabdb9b | 5319 | |
2bf803e2 YO |
5320 | if (sv_yes_mark) { |
5321 | SV *sv_mrk = get_sv("REGMARK", 1); | |
5322 | sv_setsv(sv_mrk, sv_yes_mark); | |
5323 | } | |
5324 | ||
81ed78b2 DM |
5325 | /* we don't use MULTICALL here as we want to call the |
5326 | * first op of the block of interest, rather than the | |
5327 | * first op of the sub */ | |
a6dc34f1 | 5328 | before = (IV)(SP-PL_stack_base); |
81ed78b2 | 5329 | PL_op = nop; |
8e5e9ebe RGS |
5330 | CALLRUNOPS(aTHX); /* Scalar context. */ |
5331 | SPAGAIN; | |
a6dc34f1 | 5332 | if ((IV)(SP-PL_stack_base) == before) |
075aa684 | 5333 | ret = &PL_sv_undef; /* protect against empty (?{}) blocks. */ |
8e5e9ebe RGS |
5334 | else { |
5335 | ret = POPs; | |
5336 | PUTBACK; | |
5337 | } | |
4aabdb9b | 5338 | |
e4bfbed3 DM |
5339 | /* before restoring everything, evaluate the returned |
5340 | * value, so that 'uninit' warnings don't use the wrong | |
497d0a96 DM |
5341 | * PL_op or pad. Also need to process any magic vars |
5342 | * (e.g. $1) *before* parentheses are restored */ | |
e4bfbed3 DM |
5343 | |
5344 | PL_op = NULL; | |
5345 | ||
5e98dac2 | 5346 | re_sv = NULL; |
e4bfbed3 DM |
5347 | if (logical == 0) /* (?{})/ */ |
5348 | sv_setsv(save_scalar(PL_replgv), ret); /* $^R */ | |
5349 | else if (logical == 1) { /* /(?(?{...})X|Y)/ */ | |
5350 | sw = cBOOL(SvTRUE(ret)); | |
5351 | logical = 0; | |
5352 | } | |
5353 | else { /* /(??{}) */ | |
497d0a96 DM |
5354 | /* if its overloaded, let the regex compiler handle |
5355 | * it; otherwise extract regex, or stringify */ | |
237da807 | 5356 | if (SvGMAGICAL(ret)) |
2685dc2d | 5357 | ret = sv_mortalcopy(ret); |
497d0a96 DM |
5358 | if (!SvAMAGIC(ret)) { |
5359 | SV *sv = ret; | |
5360 | if (SvROK(sv)) | |
5361 | sv = SvRV(sv); | |
5362 | if (SvTYPE(sv) == SVt_REGEXP) | |
5363 | re_sv = (REGEXP*) sv; | |
63620942 FC |
5364 | else if (SvSMAGICAL(ret)) { |
5365 | MAGIC *mg = mg_find(ret, PERL_MAGIC_qr); | |
497d0a96 DM |
5366 | if (mg) |
5367 | re_sv = (REGEXP *) mg->mg_obj; | |
5368 | } | |
e4bfbed3 | 5369 | |
2685dc2d | 5370 | /* force any undef warnings here */ |
237da807 FC |
5371 | if (!re_sv && !SvPOK(ret) && !SvNIOK(ret)) { |
5372 | ret = sv_mortalcopy(ret); | |
497d0a96 DM |
5373 | (void) SvPV_force_nolen(ret); |
5374 | } | |
e4bfbed3 DM |
5375 | } |
5376 | ||
5377 | } | |
5378 | ||
81ed78b2 DM |
5379 | /* *** Note that at this point we don't restore |
5380 | * PL_comppad, (or pop the CxSUB) on the assumption it may | |
5381 | * be used again soon. This is safe as long as nothing | |
5382 | * in the regexp code uses the pad ! */ | |
4aabdb9b | 5383 | PL_op = oop; |
4aabdb9b | 5384 | PL_curcop = ocurcop; |
92da3157 | 5385 | S_regcp_restore(aTHX_ rex, runops_cp, &maxopenparen); |
f5df269c | 5386 | PL_curpm = PL_reg_curpm; |
e4bfbed3 DM |
5387 | |
5388 | if (logical != 2) | |
4aabdb9b | 5389 | break; |
8e5e9ebe | 5390 | } |
e4bfbed3 DM |
5391 | |
5392 | /* only /(??{})/ from now on */ | |
24d3c4a9 | 5393 | logical = 0; |
4aabdb9b | 5394 | { |
4f639d21 DM |
5395 | /* extract RE object from returned value; compiling if |
5396 | * necessary */ | |
5c35adbb | 5397 | |
575c37f6 DM |
5398 | if (re_sv) { |
5399 | re_sv = reg_temp_copy(NULL, re_sv); | |
288b8c02 | 5400 | } |
0f5d15d6 | 5401 | else { |
c737faaf | 5402 | U32 pm_flags = 0; |
0f5d15d6 | 5403 | |
9753d940 DM |
5404 | if (SvUTF8(ret) && IN_BYTES) { |
5405 | /* In use 'bytes': make a copy of the octet | |
5406 | * sequence, but without the flag on */ | |
b9ad30b4 NC |
5407 | STRLEN len; |
5408 | const char *const p = SvPV(ret, len); | |
5409 | ret = newSVpvn_flags(p, len, SVs_TEMP); | |
5410 | } | |
732caac7 DM |
5411 | if (rex->intflags & PREGf_USE_RE_EVAL) |
5412 | pm_flags |= PMf_USE_RE_EVAL; | |
5413 | ||
5414 | /* if we got here, it should be an engine which | |
5415 | * supports compiling code blocks and stuff */ | |
5416 | assert(rex->engine && rex->engine->op_comp); | |
ec841a27 | 5417 | assert(!(scan->flags & ~RXf_PMf_COMPILETIME)); |
575c37f6 | 5418 | re_sv = rex->engine->op_comp(aTHX_ &ret, 1, NULL, |
ec841a27 DM |
5419 | rex->engine, NULL, NULL, |
5420 | /* copy /msix etc to inner pattern */ | |
5421 | scan->flags, | |
5422 | pm_flags); | |
732caac7 | 5423 | |
9041c2e3 | 5424 | if (!(SvFLAGS(ret) |
237da807 FC |
5425 | & (SVs_TEMP | SVs_GMG | SVf_ROK)) |
5426 | && (!SvPADTMP(ret) || SvREADONLY(ret))) { | |
a2794585 NC |
5427 | /* This isn't a first class regexp. Instead, it's |
5428 | caching a regexp onto an existing, Perl visible | |
5429 | scalar. */ | |
575c37f6 | 5430 | sv_magic(ret, MUTABLE_SV(re_sv), PERL_MAGIC_qr, 0, 0); |
3ce3ed55 | 5431 | } |
0f5d15d6 | 5432 | } |
e1ff3a88 | 5433 | SAVEFREESV(re_sv); |
8d919b0a | 5434 | re = ReANY(re_sv); |
4aabdb9b | 5435 | } |
07bc277f | 5436 | RXp_MATCH_COPIED_off(re); |
28d8d7f4 YO |
5437 | re->subbeg = rex->subbeg; |
5438 | re->sublen = rex->sublen; | |
6502e081 DM |
5439 | re->suboffset = rex->suboffset; |
5440 | re->subcoffset = rex->subcoffset; | |
d1b2014a YO |
5441 | re->lastparen = 0; |
5442 | re->lastcloseparen = 0; | |
f8fc2ecf | 5443 | rei = RXi_GET(re); |
6bda09f9 | 5444 | DEBUG_EXECUTE_r( |
220db18a DM |
5445 | debug_start_match(re_sv, utf8_target, locinput, |
5446 | reginfo->strend, "Matching embedded"); | |
6bda09f9 | 5447 | ); |
f8fc2ecf | 5448 | startpoint = rei->program + 1; |
1a147d38 | 5449 | ST.close_paren = 0; /* only used for GOSUB */ |
d1b2014a YO |
5450 | /* Save all the seen positions so far. */ |
5451 | ST.cp = regcppush(rex, 0, maxopenparen); | |
5452 | REGCP_SET(ST.lastcp); | |
5453 | /* and set maxopenparen to 0, since we are starting a "fresh" match */ | |
5454 | maxopenparen = 0; | |
5455 | /* run the pattern returned from (??{...}) */ | |
aa283a38 | 5456 | |
d1b2014a YO |
5457 | eval_recurse_doit: /* Share code with GOSUB below this line |
5458 | * At this point we expect the stack context to be | |
5459 | * set up correctly */ | |
4aabdb9b | 5460 | |
1cb95af7 DM |
5461 | /* invalidate the S-L poscache. We're now executing a |
5462 | * different set of WHILEM ops (and their associated | |
5463 | * indexes) against the same string, so the bits in the | |
5464 | * cache are meaningless. Setting maxiter to zero forces | |
5465 | * the cache to be invalidated and zeroed before reuse. | |
5466 | * XXX This is too dramatic a measure. Ideally we should | |
5467 | * save the old cache and restore when running the outer | |
5468 | * pattern again */ | |
1cb48e53 | 5469 | reginfo->poscache_maxiter = 0; |
4aabdb9b | 5470 | |
d1b2014a | 5471 | /* the new regexp might have a different is_utf8_pat than we do */ |
aed7b151 | 5472 | is_utf8_pat = reginfo->is_utf8_pat = cBOOL(RX_UTF8(re_sv)); |
faec1544 | 5473 | |
288b8c02 | 5474 | ST.prev_rex = rex_sv; |
faec1544 | 5475 | ST.prev_curlyx = cur_curlyx; |
ec43f78b DM |
5476 | rex_sv = re_sv; |
5477 | SET_reg_curpm(rex_sv); | |
288b8c02 | 5478 | rex = re; |
f8fc2ecf | 5479 | rexi = rei; |
faec1544 | 5480 | cur_curlyx = NULL; |
40a82448 | 5481 | ST.B = next; |
faec1544 DM |
5482 | ST.prev_eval = cur_eval; |
5483 | cur_eval = st; | |
faec1544 | 5484 | /* now continue from first node in postoned RE */ |
4d5016e5 | 5485 | PUSH_YES_STATE_GOTO(EVAL_AB, startpoint, locinput); |
118e2215 | 5486 | assert(0); /* NOTREACHED */ |
c277df42 | 5487 | } |
40a82448 | 5488 | |
faec1544 DM |
5489 | case EVAL_AB: /* cleanup after a successful (??{A})B */ |
5490 | /* note: this is called twice; first after popping B, then A */ | |
ec43f78b | 5491 | rex_sv = ST.prev_rex; |
aed7b151 | 5492 | is_utf8_pat = reginfo->is_utf8_pat = cBOOL(RX_UTF8(rex_sv)); |
ec43f78b | 5493 | SET_reg_curpm(rex_sv); |
8d919b0a | 5494 | rex = ReANY(rex_sv); |
f8fc2ecf | 5495 | rexi = RXi_GET(rex); |
4b22688e YO |
5496 | { |
5497 | /* preserve $^R across LEAVE's. See Bug 121070. */ | |
5498 | SV *save_sv= GvSV(PL_replgv); | |
5499 | SvREFCNT_inc(save_sv); | |
5500 | regcpblow(ST.cp); /* LEAVE in disguise */ | |
5501 | sv_setsv(GvSV(PL_replgv), save_sv); | |
5502 | SvREFCNT_dec(save_sv); | |
5503 | } | |
faec1544 DM |
5504 | cur_eval = ST.prev_eval; |
5505 | cur_curlyx = ST.prev_curlyx; | |
34a81e2b | 5506 | |
1cb95af7 | 5507 | /* Invalidate cache. See "invalidate" comment above. */ |
1cb48e53 | 5508 | reginfo->poscache_maxiter = 0; |
e7707071 | 5509 | if ( nochange_depth ) |
4b196cd4 | 5510 | nochange_depth--; |
262b90c4 | 5511 | sayYES; |
40a82448 | 5512 | |
40a82448 | 5513 | |
faec1544 DM |
5514 | case EVAL_AB_fail: /* unsuccessfully ran A or B in (??{A})B */ |
5515 | /* note: this is called twice; first after popping B, then A */ | |
ec43f78b | 5516 | rex_sv = ST.prev_rex; |
aed7b151 | 5517 | is_utf8_pat = reginfo->is_utf8_pat = cBOOL(RX_UTF8(rex_sv)); |
ec43f78b | 5518 | SET_reg_curpm(rex_sv); |
8d919b0a | 5519 | rex = ReANY(rex_sv); |
f8fc2ecf | 5520 | rexi = RXi_GET(rex); |
0357f1fd | 5521 | |
40a82448 | 5522 | REGCP_UNWIND(ST.lastcp); |
92da3157 | 5523 | regcppop(rex, &maxopenparen); |
faec1544 DM |
5524 | cur_eval = ST.prev_eval; |
5525 | cur_curlyx = ST.prev_curlyx; | |
1cb95af7 | 5526 | /* Invalidate cache. See "invalidate" comment above. */ |
1cb48e53 | 5527 | reginfo->poscache_maxiter = 0; |
e7707071 | 5528 | if ( nochange_depth ) |
4b196cd4 | 5529 | nochange_depth--; |
40a82448 | 5530 | sayNO_SILENT; |
40a82448 DM |
5531 | #undef ST |
5532 | ||
3c0563b9 | 5533 | case OPEN: /* ( */ |
c277df42 | 5534 | n = ARG(scan); /* which paren pair */ |
9d9163fb | 5535 | rex->offs[n].start_tmp = locinput - reginfo->strbeg; |
92da3157 DM |
5536 | if (n > maxopenparen) |
5537 | maxopenparen = n; | |
495f47a5 | 5538 | DEBUG_BUFFERS_r(PerlIO_printf(Perl_debug_log, |
92da3157 | 5539 | "rex=0x%"UVxf" offs=0x%"UVxf": \\%"UVuf": set %"IVdf" tmp; maxopenparen=%"UVuf"\n", |
495f47a5 DM |
5540 | PTR2UV(rex), |
5541 | PTR2UV(rex->offs), | |
5542 | (UV)n, | |
5543 | (IV)rex->offs[n].start_tmp, | |
92da3157 | 5544 | (UV)maxopenparen |
495f47a5 | 5545 | )); |
e2e6a0f1 | 5546 | lastopen = n; |
a0d0e21e | 5547 | break; |
495f47a5 DM |
5548 | |
5549 | /* XXX really need to log other places start/end are set too */ | |
5550 | #define CLOSE_CAPTURE \ | |
5551 | rex->offs[n].start = rex->offs[n].start_tmp; \ | |
9d9163fb | 5552 | rex->offs[n].end = locinput - reginfo->strbeg; \ |
495f47a5 DM |
5553 | DEBUG_BUFFERS_r(PerlIO_printf(Perl_debug_log, \ |
5554 | "rex=0x%"UVxf" offs=0x%"UVxf": \\%"UVuf": set %"IVdf"..%"IVdf"\n", \ | |
5555 | PTR2UV(rex), \ | |
5556 | PTR2UV(rex->offs), \ | |
5557 | (UV)n, \ | |
5558 | (IV)rex->offs[n].start, \ | |
5559 | (IV)rex->offs[n].end \ | |
5560 | )) | |
5561 | ||
3c0563b9 | 5562 | case CLOSE: /* ) */ |
c277df42 | 5563 | n = ARG(scan); /* which paren pair */ |
495f47a5 | 5564 | CLOSE_CAPTURE; |
b93070ed DM |
5565 | if (n > rex->lastparen) |
5566 | rex->lastparen = n; | |
5567 | rex->lastcloseparen = n; | |
3b6647e0 | 5568 | if (cur_eval && cur_eval->u.eval.close_paren == n) { |
6bda09f9 YO |
5569 | goto fake_end; |
5570 | } | |
a0d0e21e | 5571 | break; |
3c0563b9 DM |
5572 | |
5573 | case ACCEPT: /* (*ACCEPT) */ | |
e2e6a0f1 YO |
5574 | if (ARG(scan)){ |
5575 | regnode *cursor; | |
5576 | for (cursor=scan; | |
5577 | cursor && OP(cursor)!=END; | |
5578 | cursor=regnext(cursor)) | |
5579 | { | |
5580 | if ( OP(cursor)==CLOSE ){ | |
5581 | n = ARG(cursor); | |
5582 | if ( n <= lastopen ) { | |
495f47a5 | 5583 | CLOSE_CAPTURE; |
b93070ed DM |
5584 | if (n > rex->lastparen) |
5585 | rex->lastparen = n; | |
5586 | rex->lastcloseparen = n; | |
3b6647e0 RB |
5587 | if ( n == ARG(scan) || (cur_eval && |
5588 | cur_eval->u.eval.close_paren == n)) | |
e2e6a0f1 YO |
5589 | break; |
5590 | } | |
5591 | } | |
5592 | } | |
5593 | } | |
5594 | goto fake_end; | |
5595 | /*NOTREACHED*/ | |
3c0563b9 DM |
5596 | |
5597 | case GROUPP: /* (?(1)) */ | |
c277df42 | 5598 | n = ARG(scan); /* which paren pair */ |
b93070ed | 5599 | sw = cBOOL(rex->lastparen >= n && rex->offs[n].end != -1); |
c277df42 | 5600 | break; |
3c0563b9 DM |
5601 | |
5602 | case NGROUPP: /* (?(<name>)) */ | |
0a4db386 | 5603 | /* reg_check_named_buff_matched returns 0 for no match */ |
f2338a2e | 5604 | sw = cBOOL(0 < reg_check_named_buff_matched(rex,scan)); |
0a4db386 | 5605 | break; |
3c0563b9 DM |
5606 | |
5607 | case INSUBP: /* (?(R)) */ | |
0a4db386 | 5608 | n = ARG(scan); |
3b6647e0 | 5609 | sw = (cur_eval && (!n || cur_eval->u.eval.close_paren == n)); |
0a4db386 | 5610 | break; |
3c0563b9 DM |
5611 | |
5612 | case DEFINEP: /* (?(DEFINE)) */ | |
0a4db386 YO |
5613 | sw = 0; |
5614 | break; | |
3c0563b9 DM |
5615 | |
5616 | case IFTHEN: /* (?(cond)A|B) */ | |
1cb48e53 | 5617 | reginfo->poscache_iter = reginfo->poscache_maxiter; /* Void cache */ |
24d3c4a9 | 5618 | if (sw) |
c277df42 IZ |
5619 | next = NEXTOPER(NEXTOPER(scan)); |
5620 | else { | |
5621 | next = scan + ARG(scan); | |
5622 | if (OP(next) == IFTHEN) /* Fake one. */ | |
5623 | next = NEXTOPER(NEXTOPER(next)); | |
5624 | } | |
5625 | break; | |
3c0563b9 DM |
5626 | |
5627 | case LOGICAL: /* modifier for EVAL and IFMATCH */ | |
24d3c4a9 | 5628 | logical = scan->flags; |
c277df42 | 5629 | break; |
c476f425 | 5630 | |
2ab05381 | 5631 | /******************************************************************* |
2ab05381 | 5632 | |
c476f425 DM |
5633 | The CURLYX/WHILEM pair of ops handle the most generic case of the /A*B/ |
5634 | pattern, where A and B are subpatterns. (For simple A, CURLYM or | |
5635 | STAR/PLUS/CURLY/CURLYN are used instead.) | |
2ab05381 | 5636 | |
c476f425 | 5637 | A*B is compiled as <CURLYX><A><WHILEM><B> |
2ab05381 | 5638 | |
c476f425 DM |
5639 | On entry to the subpattern, CURLYX is called. This pushes a CURLYX |
5640 | state, which contains the current count, initialised to -1. It also sets | |
5641 | cur_curlyx to point to this state, with any previous value saved in the | |
5642 | state block. | |
2ab05381 | 5643 | |
c476f425 DM |
5644 | CURLYX then jumps straight to the WHILEM op, rather than executing A, |
5645 | since the pattern may possibly match zero times (i.e. it's a while {} loop | |
5646 | rather than a do {} while loop). | |
2ab05381 | 5647 | |
c476f425 DM |
5648 | Each entry to WHILEM represents a successful match of A. The count in the |
5649 | CURLYX block is incremented, another WHILEM state is pushed, and execution | |
5650 | passes to A or B depending on greediness and the current count. | |
2ab05381 | 5651 | |
c476f425 DM |
5652 | For example, if matching against the string a1a2a3b (where the aN are |
5653 | substrings that match /A/), then the match progresses as follows: (the | |
5654 | pushed states are interspersed with the bits of strings matched so far): | |
2ab05381 | 5655 | |
c476f425 DM |
5656 | <CURLYX cnt=-1> |
5657 | <CURLYX cnt=0><WHILEM> | |
5658 | <CURLYX cnt=1><WHILEM> a1 <WHILEM> | |
5659 | <CURLYX cnt=2><WHILEM> a1 <WHILEM> a2 <WHILEM> | |
5660 | <CURLYX cnt=3><WHILEM> a1 <WHILEM> a2 <WHILEM> a3 <WHILEM> | |
5661 | <CURLYX cnt=3><WHILEM> a1 <WHILEM> a2 <WHILEM> a3 <WHILEM> b | |
2ab05381 | 5662 | |
c476f425 DM |
5663 | (Contrast this with something like CURLYM, which maintains only a single |
5664 | backtrack state: | |
2ab05381 | 5665 | |
c476f425 DM |
5666 | <CURLYM cnt=0> a1 |
5667 | a1 <CURLYM cnt=1> a2 | |
5668 | a1 a2 <CURLYM cnt=2> a3 | |
5669 | a1 a2 a3 <CURLYM cnt=3> b | |
5670 | ) | |
2ab05381 | 5671 | |
c476f425 DM |
5672 | Each WHILEM state block marks a point to backtrack to upon partial failure |
5673 | of A or B, and also contains some minor state data related to that | |
5674 | iteration. The CURLYX block, pointed to by cur_curlyx, contains the | |
5675 | overall state, such as the count, and pointers to the A and B ops. | |
2ab05381 | 5676 | |
c476f425 DM |
5677 | This is complicated slightly by nested CURLYX/WHILEM's. Since cur_curlyx |
5678 | must always point to the *current* CURLYX block, the rules are: | |
2ab05381 | 5679 | |
c476f425 DM |
5680 | When executing CURLYX, save the old cur_curlyx in the CURLYX state block, |
5681 | and set cur_curlyx to point the new block. | |
2ab05381 | 5682 | |
c476f425 DM |
5683 | When popping the CURLYX block after a successful or unsuccessful match, |
5684 | restore the previous cur_curlyx. | |
2ab05381 | 5685 | |
c476f425 DM |
5686 | When WHILEM is about to execute B, save the current cur_curlyx, and set it |
5687 | to the outer one saved in the CURLYX block. | |
2ab05381 | 5688 | |
c476f425 DM |
5689 | When popping the WHILEM block after a successful or unsuccessful B match, |
5690 | restore the previous cur_curlyx. | |
2ab05381 | 5691 | |
c476f425 DM |
5692 | Here's an example for the pattern (AI* BI)*BO |
5693 | I and O refer to inner and outer, C and W refer to CURLYX and WHILEM: | |
2ab05381 | 5694 | |
c476f425 DM |
5695 | cur_ |
5696 | curlyx backtrack stack | |
5697 | ------ --------------- | |
5698 | NULL | |
5699 | CO <CO prev=NULL> <WO> | |
5700 | CI <CO prev=NULL> <WO> <CI prev=CO> <WI> ai | |
5701 | CO <CO prev=NULL> <WO> <CI prev=CO> <WI> ai <WI prev=CI> bi | |
5702 | NULL <CO prev=NULL> <WO> <CI prev=CO> <WI> ai <WI prev=CI> bi <WO prev=CO> bo | |
2ab05381 | 5703 | |
c476f425 DM |
5704 | At this point the pattern succeeds, and we work back down the stack to |
5705 | clean up, restoring as we go: | |
95b24440 | 5706 | |
c476f425 DM |
5707 | CO <CO prev=NULL> <WO> <CI prev=CO> <WI> ai <WI prev=CI> bi |
5708 | CI <CO prev=NULL> <WO> <CI prev=CO> <WI> ai | |
5709 | CO <CO prev=NULL> <WO> | |
5710 | NULL | |
a0374537 | 5711 | |
c476f425 DM |
5712 | *******************************************************************/ |
5713 | ||
5714 | #define ST st->u.curlyx | |
5715 | ||
5716 | case CURLYX: /* start of /A*B/ (for complex A) */ | |
5717 | { | |
5718 | /* No need to save/restore up to this paren */ | |
5719 | I32 parenfloor = scan->flags; | |
5720 | ||
5721 | assert(next); /* keep Coverity happy */ | |
5722 | if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */ | |
5723 | next += ARG(next); | |
5724 | ||
5725 | /* XXXX Probably it is better to teach regpush to support | |
92da3157 | 5726 | parenfloor > maxopenparen ... */ |
b93070ed DM |
5727 | if (parenfloor > (I32)rex->lastparen) |
5728 | parenfloor = rex->lastparen; /* Pessimization... */ | |
c476f425 DM |
5729 | |
5730 | ST.prev_curlyx= cur_curlyx; | |
5731 | cur_curlyx = st; | |
5732 | ST.cp = PL_savestack_ix; | |
5733 | ||
5734 | /* these fields contain the state of the current curly. | |
5735 | * they are accessed by subsequent WHILEMs */ | |
5736 | ST.parenfloor = parenfloor; | |
d02d6d97 | 5737 | ST.me = scan; |
c476f425 | 5738 | ST.B = next; |
24d3c4a9 DM |
5739 | ST.minmod = minmod; |
5740 | minmod = 0; | |
c476f425 DM |
5741 | ST.count = -1; /* this will be updated by WHILEM */ |
5742 | ST.lastloc = NULL; /* this will be updated by WHILEM */ | |
5743 | ||
4d5016e5 | 5744 | PUSH_YES_STATE_GOTO(CURLYX_end, PREVOPER(next), locinput); |
118e2215 | 5745 | assert(0); /* NOTREACHED */ |
c476f425 | 5746 | } |
a0d0e21e | 5747 | |
c476f425 | 5748 | case CURLYX_end: /* just finished matching all of A*B */ |
c476f425 DM |
5749 | cur_curlyx = ST.prev_curlyx; |
5750 | sayYES; | |
118e2215 | 5751 | assert(0); /* NOTREACHED */ |
a0d0e21e | 5752 | |
c476f425 DM |
5753 | case CURLYX_end_fail: /* just failed to match all of A*B */ |
5754 | regcpblow(ST.cp); | |
5755 | cur_curlyx = ST.prev_curlyx; | |
5756 | sayNO; | |
118e2215 | 5757 | assert(0); /* NOTREACHED */ |
4633a7c4 | 5758 | |
a0d0e21e | 5759 | |
c476f425 DM |
5760 | #undef ST |
5761 | #define ST st->u.whilem | |
5762 | ||
5763 | case WHILEM: /* just matched an A in /A*B/ (for complex A) */ | |
5764 | { | |
5765 | /* see the discussion above about CURLYX/WHILEM */ | |
c476f425 | 5766 | I32 n; |
d02d6d97 DM |
5767 | int min = ARG1(cur_curlyx->u.curlyx.me); |
5768 | int max = ARG2(cur_curlyx->u.curlyx.me); | |
5769 | regnode *A = NEXTOPER(cur_curlyx->u.curlyx.me) + EXTRA_STEP_2ARGS; | |
5770 | ||
c476f425 DM |
5771 | assert(cur_curlyx); /* keep Coverity happy */ |
5772 | n = ++cur_curlyx->u.curlyx.count; /* how many A's matched */ | |
5773 | ST.save_lastloc = cur_curlyx->u.curlyx.lastloc; | |
5774 | ST.cache_offset = 0; | |
5775 | ST.cache_mask = 0; | |
5776 | ||
c476f425 DM |
5777 | |
5778 | DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log, | |
d02d6d97 DM |
5779 | "%*s whilem: matched %ld out of %d..%d\n", |
5780 | REPORT_CODE_OFF+depth*2, "", (long)n, min, max) | |
c476f425 | 5781 | ); |
a0d0e21e | 5782 | |
c476f425 | 5783 | /* First just match a string of min A's. */ |
a0d0e21e | 5784 | |
d02d6d97 | 5785 | if (n < min) { |
92da3157 DM |
5786 | ST.cp = regcppush(rex, cur_curlyx->u.curlyx.parenfloor, |
5787 | maxopenparen); | |
c476f425 | 5788 | cur_curlyx->u.curlyx.lastloc = locinput; |
92e82afa YO |
5789 | REGCP_SET(ST.lastcp); |
5790 | ||
4d5016e5 | 5791 | PUSH_STATE_GOTO(WHILEM_A_pre, A, locinput); |
118e2215 | 5792 | assert(0); /* NOTREACHED */ |
c476f425 DM |
5793 | } |
5794 | ||
5795 | /* If degenerate A matches "", assume A done. */ | |
5796 | ||
5797 | if (locinput == cur_curlyx->u.curlyx.lastloc) { | |
5798 | DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log, | |
5799 | "%*s whilem: empty match detected, trying continuation...\n", | |
5800 | REPORT_CODE_OFF+depth*2, "") | |
5801 | ); | |
5802 | goto do_whilem_B_max; | |
5803 | } | |
5804 | ||
1cb95af7 DM |
5805 | /* super-linear cache processing. |
5806 | * | |
5807 | * The idea here is that for certain types of CURLYX/WHILEM - | |
5808 | * principally those whose upper bound is infinity (and | |
5809 | * excluding regexes that have things like \1 and other very | |
5810 | * non-regular expresssiony things), then if a pattern like | |
5811 | * /....A*.../ fails and we backtrack to the WHILEM, then we | |
5812 | * make a note that this particular WHILEM op was at string | |
5813 | * position 47 (say) when the rest of pattern failed. Then, if | |
5814 | * we ever find ourselves back at that WHILEM, and at string | |
5815 | * position 47 again, we can just fail immediately rather than | |
5816 | * running the rest of the pattern again. | |
5817 | * | |
5818 | * This is very handy when patterns start to go | |
5819 | * 'super-linear', like in (a+)*(a+)*(a+)*, where you end up | |
5820 | * with a combinatorial explosion of backtracking. | |
5821 | * | |
5822 | * The cache is implemented as a bit array, with one bit per | |
5823 | * string byte position per WHILEM op (up to 16) - so its | |
5824 | * between 0.25 and 2x the string size. | |
5825 | * | |
5826 | * To avoid allocating a poscache buffer every time, we do an | |
5827 | * initially countdown; only after we have executed a WHILEM | |
5828 | * op (string-length x #WHILEMs) times do we allocate the | |
5829 | * cache. | |
5830 | * | |
5831 | * The top 4 bits of scan->flags byte say how many different | |
5832 | * relevant CURLLYX/WHILEM op pairs there are, while the | |
5833 | * bottom 4-bits is the identifying index number of this | |
5834 | * WHILEM. | |
5835 | */ | |
c476f425 DM |
5836 | |
5837 | if (scan->flags) { | |
a0d0e21e | 5838 | |
1cb48e53 | 5839 | if (!reginfo->poscache_maxiter) { |
c476f425 DM |
5840 | /* start the countdown: Postpone detection until we |
5841 | * know the match is not *that* much linear. */ | |
1cb48e53 | 5842 | reginfo->poscache_maxiter |
9d9163fb DM |
5843 | = (reginfo->strend - reginfo->strbeg + 1) |
5844 | * (scan->flags>>4); | |
66bf836d | 5845 | /* possible overflow for long strings and many CURLYX's */ |
1cb48e53 DM |
5846 | if (reginfo->poscache_maxiter < 0) |
5847 | reginfo->poscache_maxiter = I32_MAX; | |
5848 | reginfo->poscache_iter = reginfo->poscache_maxiter; | |
2c2d71f5 | 5849 | } |
c476f425 | 5850 | |
1cb48e53 | 5851 | if (reginfo->poscache_iter-- == 0) { |
c476f425 | 5852 | /* initialise cache */ |
ea3daa5d | 5853 | const SSize_t size = (reginfo->poscache_maxiter + 7)/8; |
2ac8ff4b DM |
5854 | regmatch_info_aux *const aux = reginfo->info_aux; |
5855 | if (aux->poscache) { | |
ea3daa5d | 5856 | if ((SSize_t)reginfo->poscache_size < size) { |
2ac8ff4b DM |
5857 | Renew(aux->poscache, size, char); |
5858 | reginfo->poscache_size = size; | |
2c2d71f5 | 5859 | } |
2ac8ff4b | 5860 | Zero(aux->poscache, size, char); |
2c2d71f5 JH |
5861 | } |
5862 | else { | |
2ac8ff4b DM |
5863 | reginfo->poscache_size = size; |
5864 | Newxz(aux->poscache, size, char); | |
2c2d71f5 | 5865 | } |
c476f425 DM |
5866 | DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log, |
5867 | "%swhilem: Detected a super-linear match, switching on caching%s...\n", | |
5868 | PL_colors[4], PL_colors[5]) | |
5869 | ); | |
2c2d71f5 | 5870 | } |
c476f425 | 5871 | |
1cb48e53 | 5872 | if (reginfo->poscache_iter < 0) { |
c476f425 | 5873 | /* have we already failed at this position? */ |
ea3daa5d | 5874 | SSize_t offset, mask; |
338e600a DM |
5875 | |
5876 | reginfo->poscache_iter = -1; /* stop eventual underflow */ | |
c476f425 | 5877 | offset = (scan->flags & 0xf) - 1 |
9d9163fb DM |
5878 | + (locinput - reginfo->strbeg) |
5879 | * (scan->flags>>4); | |
c476f425 DM |
5880 | mask = 1 << (offset % 8); |
5881 | offset /= 8; | |
2ac8ff4b | 5882 | if (reginfo->info_aux->poscache[offset] & mask) { |
c476f425 DM |
5883 | DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log, |
5884 | "%*s whilem: (cache) already tried at this position...\n", | |
5885 | REPORT_CODE_OFF+depth*2, "") | |
2c2d71f5 | 5886 | ); |
3298f257 | 5887 | sayNO; /* cache records failure */ |
2c2d71f5 | 5888 | } |
c476f425 DM |
5889 | ST.cache_offset = offset; |
5890 | ST.cache_mask = mask; | |
2c2d71f5 | 5891 | } |
c476f425 | 5892 | } |
2c2d71f5 | 5893 | |
c476f425 | 5894 | /* Prefer B over A for minimal matching. */ |
a687059c | 5895 | |
c476f425 DM |
5896 | if (cur_curlyx->u.curlyx.minmod) { |
5897 | ST.save_curlyx = cur_curlyx; | |
5898 | cur_curlyx = cur_curlyx->u.curlyx.prev_curlyx; | |
92da3157 DM |
5899 | ST.cp = regcppush(rex, ST.save_curlyx->u.curlyx.parenfloor, |
5900 | maxopenparen); | |
c476f425 | 5901 | REGCP_SET(ST.lastcp); |
4d5016e5 DM |
5902 | PUSH_YES_STATE_GOTO(WHILEM_B_min, ST.save_curlyx->u.curlyx.B, |
5903 | locinput); | |
118e2215 | 5904 | assert(0); /* NOTREACHED */ |
c476f425 | 5905 | } |
a0d0e21e | 5906 | |
c476f425 DM |
5907 | /* Prefer A over B for maximal matching. */ |
5908 | ||
d02d6d97 | 5909 | if (n < max) { /* More greed allowed? */ |
92da3157 DM |
5910 | ST.cp = regcppush(rex, cur_curlyx->u.curlyx.parenfloor, |
5911 | maxopenparen); | |
c476f425 DM |
5912 | cur_curlyx->u.curlyx.lastloc = locinput; |
5913 | REGCP_SET(ST.lastcp); | |
4d5016e5 | 5914 | PUSH_STATE_GOTO(WHILEM_A_max, A, locinput); |
118e2215 | 5915 | assert(0); /* NOTREACHED */ |
c476f425 DM |
5916 | } |
5917 | goto do_whilem_B_max; | |
5918 | } | |
118e2215 | 5919 | assert(0); /* NOTREACHED */ |
c476f425 DM |
5920 | |
5921 | case WHILEM_B_min: /* just matched B in a minimal match */ | |
5922 | case WHILEM_B_max: /* just matched B in a maximal match */ | |
5923 | cur_curlyx = ST.save_curlyx; | |
5924 | sayYES; | |
118e2215 | 5925 | assert(0); /* NOTREACHED */ |
c476f425 DM |
5926 | |
5927 | case WHILEM_B_max_fail: /* just failed to match B in a maximal match */ | |
5928 | cur_curlyx = ST.save_curlyx; | |
5929 | cur_curlyx->u.curlyx.lastloc = ST.save_lastloc; | |
5930 | cur_curlyx->u.curlyx.count--; | |
5931 | CACHEsayNO; | |
118e2215 | 5932 | assert(0); /* NOTREACHED */ |
c476f425 DM |
5933 | |
5934 | case WHILEM_A_min_fail: /* just failed to match A in a minimal match */ | |
c476f425 DM |
5935 | /* FALL THROUGH */ |
5936 | case WHILEM_A_pre_fail: /* just failed to match even minimal A */ | |
92e82afa | 5937 | REGCP_UNWIND(ST.lastcp); |
92da3157 | 5938 | regcppop(rex, &maxopenparen); |
c476f425 DM |
5939 | cur_curlyx->u.curlyx.lastloc = ST.save_lastloc; |
5940 | cur_curlyx->u.curlyx.count--; | |
5941 | CACHEsayNO; | |
118e2215 | 5942 | assert(0); /* NOTREACHED */ |
c476f425 DM |
5943 | |
5944 | case WHILEM_A_max_fail: /* just failed to match A in a maximal match */ | |
5945 | REGCP_UNWIND(ST.lastcp); | |
92da3157 | 5946 | regcppop(rex, &maxopenparen); /* Restore some previous $<digit>s? */ |
c476f425 DM |
5947 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, |
5948 | "%*s whilem: failed, trying continuation...\n", | |
5949 | REPORT_CODE_OFF+depth*2, "") | |
5950 | ); | |
5951 | do_whilem_B_max: | |
5952 | if (cur_curlyx->u.curlyx.count >= REG_INFTY | |
5953 | && ckWARN(WARN_REGEXP) | |
39819bd9 | 5954 | && !reginfo->warned) |
c476f425 | 5955 | { |
39819bd9 | 5956 | reginfo->warned = TRUE; |
dcbac5bb FC |
5957 | Perl_warner(aTHX_ packWARN(WARN_REGEXP), |
5958 | "Complex regular subexpression recursion limit (%d) " | |
5959 | "exceeded", | |
c476f425 DM |
5960 | REG_INFTY - 1); |
5961 | } | |
5962 | ||
5963 | /* now try B */ | |
5964 | ST.save_curlyx = cur_curlyx; | |
5965 | cur_curlyx = cur_curlyx->u.curlyx.prev_curlyx; | |
4d5016e5 DM |
5966 | PUSH_YES_STATE_GOTO(WHILEM_B_max, ST.save_curlyx->u.curlyx.B, |
5967 | locinput); | |
118e2215 | 5968 | assert(0); /* NOTREACHED */ |
c476f425 DM |
5969 | |
5970 | case WHILEM_B_min_fail: /* just failed to match B in a minimal match */ | |
5971 | cur_curlyx = ST.save_curlyx; | |
5972 | REGCP_UNWIND(ST.lastcp); | |
92da3157 | 5973 | regcppop(rex, &maxopenparen); |
c476f425 | 5974 | |
d02d6d97 | 5975 | if (cur_curlyx->u.curlyx.count >= /*max*/ARG2(cur_curlyx->u.curlyx.me)) { |
c476f425 DM |
5976 | /* Maximum greed exceeded */ |
5977 | if (cur_curlyx->u.curlyx.count >= REG_INFTY | |
5978 | && ckWARN(WARN_REGEXP) | |
39819bd9 | 5979 | && !reginfo->warned) |
c476f425 | 5980 | { |
39819bd9 | 5981 | reginfo->warned = TRUE; |
c476f425 | 5982 | Perl_warner(aTHX_ packWARN(WARN_REGEXP), |
dcbac5bb FC |
5983 | "Complex regular subexpression recursion " |
5984 | "limit (%d) exceeded", | |
c476f425 | 5985 | REG_INFTY - 1); |
a0d0e21e | 5986 | } |
c476f425 | 5987 | cur_curlyx->u.curlyx.count--; |
3ab3c9b4 | 5988 | CACHEsayNO; |
a0d0e21e | 5989 | } |
c476f425 DM |
5990 | |
5991 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, | |
5992 | "%*s trying longer...\n", REPORT_CODE_OFF+depth*2, "") | |
5993 | ); | |
5994 | /* Try grabbing another A and see if it helps. */ | |
c476f425 | 5995 | cur_curlyx->u.curlyx.lastloc = locinput; |
92da3157 DM |
5996 | ST.cp = regcppush(rex, cur_curlyx->u.curlyx.parenfloor, |
5997 | maxopenparen); | |
c476f425 | 5998 | REGCP_SET(ST.lastcp); |
d02d6d97 | 5999 | PUSH_STATE_GOTO(WHILEM_A_min, |
4d5016e5 DM |
6000 | /*A*/ NEXTOPER(ST.save_curlyx->u.curlyx.me) + EXTRA_STEP_2ARGS, |
6001 | locinput); | |
118e2215 | 6002 | assert(0); /* NOTREACHED */ |
40a82448 DM |
6003 | |
6004 | #undef ST | |
6005 | #define ST st->u.branch | |
6006 | ||
6007 | case BRANCHJ: /* /(...|A|...)/ with long next pointer */ | |
c277df42 IZ |
6008 | next = scan + ARG(scan); |
6009 | if (next == scan) | |
6010 | next = NULL; | |
40a82448 DM |
6011 | scan = NEXTOPER(scan); |
6012 | /* FALL THROUGH */ | |
c277df42 | 6013 | |
40a82448 DM |
6014 | case BRANCH: /* /(...|A|...)/ */ |
6015 | scan = NEXTOPER(scan); /* scan now points to inner node */ | |
b93070ed | 6016 | ST.lastparen = rex->lastparen; |
f6033a9d | 6017 | ST.lastcloseparen = rex->lastcloseparen; |
40a82448 DM |
6018 | ST.next_branch = next; |
6019 | REGCP_SET(ST.cp); | |
02db2b7b | 6020 | |
40a82448 | 6021 | /* Now go into the branch */ |
5d458dd8 | 6022 | if (has_cutgroup) { |
4d5016e5 | 6023 | PUSH_YES_STATE_GOTO(BRANCH_next, scan, locinput); |
5d458dd8 | 6024 | } else { |
4d5016e5 | 6025 | PUSH_STATE_GOTO(BRANCH_next, scan, locinput); |
5d458dd8 | 6026 | } |
118e2215 | 6027 | assert(0); /* NOTREACHED */ |
3c0563b9 DM |
6028 | |
6029 | case CUTGROUP: /* /(*THEN)/ */ | |
5d458dd8 | 6030 | sv_yes_mark = st->u.mark.mark_name = scan->flags ? NULL : |
ad64d0ec | 6031 | MUTABLE_SV(rexi->data->data[ ARG( scan ) ]); |
4d5016e5 | 6032 | PUSH_STATE_GOTO(CUTGROUP_next, next, locinput); |
118e2215 | 6033 | assert(0); /* NOTREACHED */ |
3c0563b9 | 6034 | |
5d458dd8 YO |
6035 | case CUTGROUP_next_fail: |
6036 | do_cutgroup = 1; | |
6037 | no_final = 1; | |
6038 | if (st->u.mark.mark_name) | |
6039 | sv_commit = st->u.mark.mark_name; | |
6040 | sayNO; | |
118e2215 | 6041 | assert(0); /* NOTREACHED */ |
3c0563b9 | 6042 | |
5d458dd8 YO |
6043 | case BRANCH_next: |
6044 | sayYES; | |
118e2215 | 6045 | assert(0); /* NOTREACHED */ |
3c0563b9 | 6046 | |
40a82448 | 6047 | case BRANCH_next_fail: /* that branch failed; try the next, if any */ |
5d458dd8 YO |
6048 | if (do_cutgroup) { |
6049 | do_cutgroup = 0; | |
6050 | no_final = 0; | |
6051 | } | |
40a82448 | 6052 | REGCP_UNWIND(ST.cp); |
a8d1f4b4 | 6053 | UNWIND_PAREN(ST.lastparen, ST.lastcloseparen); |
40a82448 DM |
6054 | scan = ST.next_branch; |
6055 | /* no more branches? */ | |
5d458dd8 YO |
6056 | if (!scan || (OP(scan) != BRANCH && OP(scan) != BRANCHJ)) { |
6057 | DEBUG_EXECUTE_r({ | |
6058 | PerlIO_printf( Perl_debug_log, | |
6059 | "%*s %sBRANCH failed...%s\n", | |
6060 | REPORT_CODE_OFF+depth*2, "", | |
6061 | PL_colors[4], | |
6062 | PL_colors[5] ); | |
6063 | }); | |
6064 | sayNO_SILENT; | |
6065 | } | |
40a82448 | 6066 | continue; /* execute next BRANCH[J] op */ |
118e2215 | 6067 | assert(0); /* NOTREACHED */ |
40a82448 | 6068 | |
3c0563b9 | 6069 | case MINMOD: /* next op will be non-greedy, e.g. A*? */ |
24d3c4a9 | 6070 | minmod = 1; |
a0d0e21e | 6071 | break; |
40a82448 DM |
6072 | |
6073 | #undef ST | |
6074 | #define ST st->u.curlym | |
6075 | ||
6076 | case CURLYM: /* /A{m,n}B/ where A is fixed-length */ | |
6077 | ||
6078 | /* This is an optimisation of CURLYX that enables us to push | |
84d2fa14 | 6079 | * only a single backtracking state, no matter how many matches |
40a82448 DM |
6080 | * there are in {m,n}. It relies on the pattern being constant |
6081 | * length, with no parens to influence future backrefs | |
6082 | */ | |
6083 | ||
6084 | ST.me = scan; | |
dc45a647 | 6085 | scan = NEXTOPER(scan) + NODE_STEP_REGNODE; |
40a82448 | 6086 | |
f6033a9d DM |
6087 | ST.lastparen = rex->lastparen; |
6088 | ST.lastcloseparen = rex->lastcloseparen; | |
6089 | ||
40a82448 DM |
6090 | /* if paren positive, emulate an OPEN/CLOSE around A */ |
6091 | if (ST.me->flags) { | |
3b6647e0 | 6092 | U32 paren = ST.me->flags; |
92da3157 DM |
6093 | if (paren > maxopenparen) |
6094 | maxopenparen = paren; | |
c277df42 | 6095 | scan += NEXT_OFF(scan); /* Skip former OPEN. */ |
6407bf3b | 6096 | } |
40a82448 DM |
6097 | ST.A = scan; |
6098 | ST.B = next; | |
6099 | ST.alen = 0; | |
6100 | ST.count = 0; | |
24d3c4a9 DM |
6101 | ST.minmod = minmod; |
6102 | minmod = 0; | |
40a82448 DM |
6103 | ST.c1 = CHRTEST_UNINIT; |
6104 | REGCP_SET(ST.cp); | |
6407bf3b | 6105 | |
40a82448 DM |
6106 | if (!(ST.minmod ? ARG1(ST.me) : ARG2(ST.me))) /* min/max */ |
6107 | goto curlym_do_B; | |
6108 | ||
6109 | curlym_do_A: /* execute the A in /A{m,n}B/ */ | |
4d5016e5 | 6110 | PUSH_YES_STATE_GOTO(CURLYM_A, ST.A, locinput); /* match A */ |
118e2215 | 6111 | assert(0); /* NOTREACHED */ |
5f80c4cf | 6112 | |
40a82448 | 6113 | case CURLYM_A: /* we've just matched an A */ |
40a82448 DM |
6114 | ST.count++; |
6115 | /* after first match, determine A's length: u.curlym.alen */ | |
6116 | if (ST.count == 1) { | |
ba44c216 | 6117 | if (reginfo->is_utf8_target) { |
c07e9d7b DM |
6118 | char *s = st->locinput; |
6119 | while (s < locinput) { | |
40a82448 DM |
6120 | ST.alen++; |
6121 | s += UTF8SKIP(s); | |
6122 | } | |
6123 | } | |
6124 | else { | |
c07e9d7b | 6125 | ST.alen = locinput - st->locinput; |
40a82448 DM |
6126 | } |
6127 | if (ST.alen == 0) | |
6128 | ST.count = ST.minmod ? ARG1(ST.me) : ARG2(ST.me); | |
6129 | } | |
0cadcf80 DM |
6130 | DEBUG_EXECUTE_r( |
6131 | PerlIO_printf(Perl_debug_log, | |
40a82448 | 6132 | "%*s CURLYM now matched %"IVdf" times, len=%"IVdf"...\n", |
5bc10b2c | 6133 | (int)(REPORT_CODE_OFF+(depth*2)), "", |
40a82448 | 6134 | (IV) ST.count, (IV)ST.alen) |
0cadcf80 DM |
6135 | ); |
6136 | ||
0a4db386 | 6137 | if (cur_eval && cur_eval->u.eval.close_paren && |
24b23f37 | 6138 | cur_eval->u.eval.close_paren == (U32)ST.me->flags) |
0a4db386 YO |
6139 | goto fake_end; |
6140 | ||
c966426a DM |
6141 | { |
6142 | I32 max = (ST.minmod ? ARG1(ST.me) : ARG2(ST.me)); | |
6143 | if ( max == REG_INFTY || ST.count < max ) | |
6144 | goto curlym_do_A; /* try to match another A */ | |
6145 | } | |
40a82448 | 6146 | goto curlym_do_B; /* try to match B */ |
5f80c4cf | 6147 | |
40a82448 DM |
6148 | case CURLYM_A_fail: /* just failed to match an A */ |
6149 | REGCP_UNWIND(ST.cp); | |
0a4db386 YO |
6150 | |
6151 | if (ST.minmod || ST.count < ARG1(ST.me) /* min*/ | |
6152 | || (cur_eval && cur_eval->u.eval.close_paren && | |
24b23f37 | 6153 | cur_eval->u.eval.close_paren == (U32)ST.me->flags)) |
40a82448 | 6154 | sayNO; |
0cadcf80 | 6155 | |
40a82448 | 6156 | curlym_do_B: /* execute the B in /A{m,n}B/ */ |
40a82448 DM |
6157 | if (ST.c1 == CHRTEST_UNINIT) { |
6158 | /* calculate c1 and c2 for possible match of 1st char | |
6159 | * following curly */ | |
6160 | ST.c1 = ST.c2 = CHRTEST_VOID; | |
6161 | if (HAS_TEXT(ST.B) || JUMPABLE(ST.B)) { | |
6162 | regnode *text_node = ST.B; | |
6163 | if (! HAS_TEXT(text_node)) | |
6164 | FIND_NEXT_IMPT(text_node); | |
ee9b8eae YO |
6165 | /* this used to be |
6166 | ||
6167 | (HAS_TEXT(text_node) && PL_regkind[OP(text_node)] == EXACT) | |
6168 | ||
6169 | But the former is redundant in light of the latter. | |
6170 | ||
6171 | if this changes back then the macro for | |
6172 | IS_TEXT and friends need to change. | |
6173 | */ | |
c74f6de9 | 6174 | if (PL_regkind[OP(text_node)] == EXACT) { |
79a2a0e8 | 6175 | if (! S_setup_EXACTISH_ST_c1_c2(aTHX_ |
984e6dd1 | 6176 | text_node, &ST.c1, ST.c1_utf8, &ST.c2, ST.c2_utf8, |
aed7b151 | 6177 | reginfo)) |
c74f6de9 KW |
6178 | { |
6179 | sayNO; | |
6180 | } | |
c277df42 | 6181 | } |
c277df42 | 6182 | } |
40a82448 DM |
6183 | } |
6184 | ||
6185 | DEBUG_EXECUTE_r( | |
6186 | PerlIO_printf(Perl_debug_log, | |
6187 | "%*s CURLYM trying tail with matches=%"IVdf"...\n", | |
5bc10b2c | 6188 | (int)(REPORT_CODE_OFF+(depth*2)), |
40a82448 DM |
6189 | "", (IV)ST.count) |
6190 | ); | |
c74f6de9 | 6191 | if (! NEXTCHR_IS_EOS && ST.c1 != CHRTEST_VOID) { |
79a2a0e8 KW |
6192 | if (! UTF8_IS_INVARIANT(nextchr) && utf8_target) { |
6193 | if (memNE(locinput, ST.c1_utf8, UTF8SKIP(locinput)) | |
6194 | && memNE(locinput, ST.c2_utf8, UTF8SKIP(locinput))) | |
6195 | { | |
6196 | /* simulate B failing */ | |
6197 | DEBUG_OPTIMISE_r( | |
6198 | PerlIO_printf(Perl_debug_log, | |
33daa3a5 | 6199 | "%*s CURLYM Fast bail next target=0x%"UVXf" c1=0x%"UVXf" c2=0x%"UVXf"\n", |
79a2a0e8 KW |
6200 | (int)(REPORT_CODE_OFF+(depth*2)),"", |
6201 | valid_utf8_to_uvchr((U8 *) locinput, NULL), | |
6202 | valid_utf8_to_uvchr(ST.c1_utf8, NULL), | |
6203 | valid_utf8_to_uvchr(ST.c2_utf8, NULL)) | |
6204 | ); | |
6205 | state_num = CURLYM_B_fail; | |
6206 | goto reenter_switch; | |
6207 | } | |
6208 | } | |
6209 | else if (nextchr != ST.c1 && nextchr != ST.c2) { | |
5400f398 KW |
6210 | /* simulate B failing */ |
6211 | DEBUG_OPTIMISE_r( | |
6212 | PerlIO_printf(Perl_debug_log, | |
33daa3a5 | 6213 | "%*s CURLYM Fast bail next target=0x%X c1=0x%X c2=0x%X\n", |
5400f398 | 6214 | (int)(REPORT_CODE_OFF+(depth*2)),"", |
79a2a0e8 KW |
6215 | (int) nextchr, ST.c1, ST.c2) |
6216 | ); | |
5400f398 KW |
6217 | state_num = CURLYM_B_fail; |
6218 | goto reenter_switch; | |
6219 | } | |
c74f6de9 | 6220 | } |
40a82448 DM |
6221 | |
6222 | if (ST.me->flags) { | |
f6033a9d | 6223 | /* emulate CLOSE: mark current A as captured */ |
40a82448 DM |
6224 | I32 paren = ST.me->flags; |
6225 | if (ST.count) { | |
b93070ed | 6226 | rex->offs[paren].start |
9d9163fb DM |
6227 | = HOPc(locinput, -ST.alen) - reginfo->strbeg; |
6228 | rex->offs[paren].end = locinput - reginfo->strbeg; | |
f6033a9d DM |
6229 | if ((U32)paren > rex->lastparen) |
6230 | rex->lastparen = paren; | |
6231 | rex->lastcloseparen = paren; | |
c277df42 | 6232 | } |
40a82448 | 6233 | else |
b93070ed | 6234 | rex->offs[paren].end = -1; |
0a4db386 | 6235 | if (cur_eval && cur_eval->u.eval.close_paren && |
24b23f37 | 6236 | cur_eval->u.eval.close_paren == (U32)ST.me->flags) |
0a4db386 YO |
6237 | { |
6238 | if (ST.count) | |
6239 | goto fake_end; | |
6240 | else | |
6241 | sayNO; | |
6242 | } | |
c277df42 | 6243 | } |
0a4db386 | 6244 | |
4d5016e5 | 6245 | PUSH_STATE_GOTO(CURLYM_B, ST.B, locinput); /* match B */ |
118e2215 | 6246 | assert(0); /* NOTREACHED */ |
40a82448 DM |
6247 | |
6248 | case CURLYM_B_fail: /* just failed to match a B */ | |
6249 | REGCP_UNWIND(ST.cp); | |
a8d1f4b4 | 6250 | UNWIND_PAREN(ST.lastparen, ST.lastcloseparen); |
40a82448 | 6251 | if (ST.minmod) { |
84d2fa14 HS |
6252 | I32 max = ARG2(ST.me); |
6253 | if (max != REG_INFTY && ST.count == max) | |
40a82448 DM |
6254 | sayNO; |
6255 | goto curlym_do_A; /* try to match a further A */ | |
6256 | } | |
6257 | /* backtrack one A */ | |
6258 | if (ST.count == ARG1(ST.me) /* min */) | |
6259 | sayNO; | |
6260 | ST.count--; | |
7016d6eb | 6261 | SET_locinput(HOPc(locinput, -ST.alen)); |
40a82448 DM |
6262 | goto curlym_do_B; /* try to match B */ |
6263 | ||
c255a977 DM |
6264 | #undef ST |
6265 | #define ST st->u.curly | |
40a82448 | 6266 | |
c255a977 DM |
6267 | #define CURLY_SETPAREN(paren, success) \ |
6268 | if (paren) { \ | |
6269 | if (success) { \ | |
9d9163fb DM |
6270 | rex->offs[paren].start = HOPc(locinput, -1) - reginfo->strbeg; \ |
6271 | rex->offs[paren].end = locinput - reginfo->strbeg; \ | |
f6033a9d DM |
6272 | if (paren > rex->lastparen) \ |
6273 | rex->lastparen = paren; \ | |
b93070ed | 6274 | rex->lastcloseparen = paren; \ |
c255a977 | 6275 | } \ |
f6033a9d | 6276 | else { \ |
b93070ed | 6277 | rex->offs[paren].end = -1; \ |
f6033a9d DM |
6278 | rex->lastparen = ST.lastparen; \ |
6279 | rex->lastcloseparen = ST.lastcloseparen; \ | |
6280 | } \ | |
c255a977 DM |
6281 | } |
6282 | ||
b40a2c17 | 6283 | case STAR: /* /A*B/ where A is width 1 char */ |
c255a977 DM |
6284 | ST.paren = 0; |
6285 | ST.min = 0; | |
6286 | ST.max = REG_INFTY; | |
a0d0e21e LW |
6287 | scan = NEXTOPER(scan); |
6288 | goto repeat; | |
3c0563b9 | 6289 | |
b40a2c17 | 6290 | case PLUS: /* /A+B/ where A is width 1 char */ |
c255a977 DM |
6291 | ST.paren = 0; |
6292 | ST.min = 1; | |
6293 | ST.max = REG_INFTY; | |
c277df42 | 6294 | scan = NEXTOPER(scan); |
c255a977 | 6295 | goto repeat; |
3c0563b9 | 6296 | |
b40a2c17 | 6297 | case CURLYN: /* /(A){m,n}B/ where A is width 1 char */ |
5400f398 KW |
6298 | ST.paren = scan->flags; /* Which paren to set */ |
6299 | ST.lastparen = rex->lastparen; | |
f6033a9d | 6300 | ST.lastcloseparen = rex->lastcloseparen; |
92da3157 DM |
6301 | if (ST.paren > maxopenparen) |
6302 | maxopenparen = ST.paren; | |
c255a977 DM |
6303 | ST.min = ARG1(scan); /* min to match */ |
6304 | ST.max = ARG2(scan); /* max to match */ | |
0a4db386 | 6305 | if (cur_eval && cur_eval->u.eval.close_paren && |
86413ec0 | 6306 | cur_eval->u.eval.close_paren == (U32)ST.paren) { |
0a4db386 YO |
6307 | ST.min=1; |
6308 | ST.max=1; | |
6309 | } | |
c255a977 DM |
6310 | scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE); |
6311 | goto repeat; | |
3c0563b9 | 6312 | |
b40a2c17 | 6313 | case CURLY: /* /A{m,n}B/ where A is width 1 char */ |
c255a977 DM |
6314 | ST.paren = 0; |
6315 | ST.min = ARG1(scan); /* min to match */ | |
6316 | ST.max = ARG2(scan); /* max to match */ | |
6317 | scan = NEXTOPER(scan) + NODE_STEP_REGNODE; | |
c277df42 | 6318 | repeat: |
a0d0e21e LW |
6319 | /* |
6320 | * Lookahead to avoid useless match attempts | |
6321 | * when we know what character comes next. | |
c255a977 | 6322 | * |
5f80c4cf JP |
6323 | * Used to only do .*x and .*?x, but now it allows |
6324 | * for )'s, ('s and (?{ ... })'s to be in the way | |
6325 | * of the quantifier and the EXACT-like node. -- japhy | |
6326 | */ | |
6327 | ||
eb5c1be8 | 6328 | assert(ST.min <= ST.max); |
3337dfe3 KW |
6329 | if (! HAS_TEXT(next) && ! JUMPABLE(next)) { |
6330 | ST.c1 = ST.c2 = CHRTEST_VOID; | |
6331 | } | |
6332 | else { | |
5f80c4cf JP |
6333 | regnode *text_node = next; |
6334 | ||
3dab1dad YO |
6335 | if (! HAS_TEXT(text_node)) |
6336 | FIND_NEXT_IMPT(text_node); | |
5f80c4cf | 6337 | |
9e137952 | 6338 | if (! HAS_TEXT(text_node)) |
c255a977 | 6339 | ST.c1 = ST.c2 = CHRTEST_VOID; |
5f80c4cf | 6340 | else { |
ee9b8eae | 6341 | if ( PL_regkind[OP(text_node)] != EXACT ) { |
c255a977 | 6342 | ST.c1 = ST.c2 = CHRTEST_VOID; |
cca55fe3 | 6343 | } |
c74f6de9 | 6344 | else { |
ee9b8eae YO |
6345 | |
6346 | /* Currently we only get here when | |
6347 | ||
6348 | PL_rekind[OP(text_node)] == EXACT | |
6349 | ||
6350 | if this changes back then the macro for IS_TEXT and | |
6351 | friends need to change. */ | |
79a2a0e8 | 6352 | if (! S_setup_EXACTISH_ST_c1_c2(aTHX_ |
984e6dd1 | 6353 | text_node, &ST.c1, ST.c1_utf8, &ST.c2, ST.c2_utf8, |
aed7b151 | 6354 | reginfo)) |
c74f6de9 KW |
6355 | { |
6356 | sayNO; | |
6357 | } | |
6358 | } | |
1aa99e6b | 6359 | } |
bbce6d69 | 6360 | } |
c255a977 DM |
6361 | |
6362 | ST.A = scan; | |
6363 | ST.B = next; | |
24d3c4a9 | 6364 | if (minmod) { |
eb72505d | 6365 | char *li = locinput; |
24d3c4a9 | 6366 | minmod = 0; |
984e6dd1 | 6367 | if (ST.min && |
f9176b44 | 6368 | regrepeat(rex, &li, ST.A, reginfo, ST.min, depth) |
984e6dd1 | 6369 | < ST.min) |
4633a7c4 | 6370 | sayNO; |
7016d6eb | 6371 | SET_locinput(li); |
c255a977 | 6372 | ST.count = ST.min; |
c255a977 DM |
6373 | REGCP_SET(ST.cp); |
6374 | if (ST.c1 == CHRTEST_VOID) | |
6375 | goto curly_try_B_min; | |
6376 | ||
6377 | ST.oldloc = locinput; | |
6378 | ||
6379 | /* set ST.maxpos to the furthest point along the | |
6380 | * string that could possibly match */ | |
6381 | if (ST.max == REG_INFTY) { | |
220db18a | 6382 | ST.maxpos = reginfo->strend - 1; |
f2ed9b32 | 6383 | if (utf8_target) |
c255a977 DM |
6384 | while (UTF8_IS_CONTINUATION(*(U8*)ST.maxpos)) |
6385 | ST.maxpos--; | |
6386 | } | |
f2ed9b32 | 6387 | else if (utf8_target) { |
c255a977 DM |
6388 | int m = ST.max - ST.min; |
6389 | for (ST.maxpos = locinput; | |
220db18a | 6390 | m >0 && ST.maxpos < reginfo->strend; m--) |
c255a977 DM |
6391 | ST.maxpos += UTF8SKIP(ST.maxpos); |
6392 | } | |
6393 | else { | |
6394 | ST.maxpos = locinput + ST.max - ST.min; | |
220db18a DM |
6395 | if (ST.maxpos >= reginfo->strend) |
6396 | ST.maxpos = reginfo->strend - 1; | |
c255a977 DM |
6397 | } |
6398 | goto curly_try_B_min_known; | |
6399 | ||
6400 | } | |
6401 | else { | |
eb72505d DM |
6402 | /* avoid taking address of locinput, so it can remain |
6403 | * a register var */ | |
6404 | char *li = locinput; | |
f9176b44 | 6405 | ST.count = regrepeat(rex, &li, ST.A, reginfo, ST.max, depth); |
c255a977 DM |
6406 | if (ST.count < ST.min) |
6407 | sayNO; | |
7016d6eb | 6408 | SET_locinput(li); |
c255a977 DM |
6409 | if ((ST.count > ST.min) |
6410 | && (PL_regkind[OP(ST.B)] == EOL) && (OP(ST.B) != MEOL)) | |
6411 | { | |
6412 | /* A{m,n} must come at the end of the string, there's | |
6413 | * no point in backing off ... */ | |
6414 | ST.min = ST.count; | |
6415 | /* ...except that $ and \Z can match before *and* after | |
6416 | newline at the end. Consider "\n\n" =~ /\n+\Z\n/. | |
6417 | We may back off by one in this case. */ | |
eb72505d | 6418 | if (UCHARAT(locinput - 1) == '\n' && OP(ST.B) != EOS) |
c255a977 DM |
6419 | ST.min--; |
6420 | } | |
6421 | REGCP_SET(ST.cp); | |
6422 | goto curly_try_B_max; | |
6423 | } | |
118e2215 | 6424 | assert(0); /* NOTREACHED */ |
c255a977 DM |
6425 | |
6426 | ||
6427 | case CURLY_B_min_known_fail: | |
6428 | /* failed to find B in a non-greedy match where c1,c2 valid */ | |
c255a977 | 6429 | |
c255a977 | 6430 | REGCP_UNWIND(ST.cp); |
a8d1f4b4 DM |
6431 | if (ST.paren) { |
6432 | UNWIND_PAREN(ST.lastparen, ST.lastcloseparen); | |
6433 | } | |
c255a977 DM |
6434 | /* Couldn't or didn't -- move forward. */ |
6435 | ST.oldloc = locinput; | |
f2ed9b32 | 6436 | if (utf8_target) |
c255a977 DM |
6437 | locinput += UTF8SKIP(locinput); |
6438 | else | |
6439 | locinput++; | |
6440 | ST.count++; | |
6441 | curly_try_B_min_known: | |
6442 | /* find the next place where 'B' could work, then call B */ | |
6443 | { | |
6444 | int n; | |
f2ed9b32 | 6445 | if (utf8_target) { |
c255a977 DM |
6446 | n = (ST.oldloc == locinput) ? 0 : 1; |
6447 | if (ST.c1 == ST.c2) { | |
c255a977 | 6448 | /* set n to utf8_distance(oldloc, locinput) */ |
79a2a0e8 KW |
6449 | while (locinput <= ST.maxpos |
6450 | && memNE(locinput, ST.c1_utf8, UTF8SKIP(locinput))) | |
6451 | { | |
6452 | locinput += UTF8SKIP(locinput); | |
c255a977 DM |
6453 | n++; |
6454 | } | |
1aa99e6b IH |
6455 | } |
6456 | else { | |
c255a977 | 6457 | /* set n to utf8_distance(oldloc, locinput) */ |
79a2a0e8 KW |
6458 | while (locinput <= ST.maxpos |
6459 | && memNE(locinput, ST.c1_utf8, UTF8SKIP(locinput)) | |
6460 | && memNE(locinput, ST.c2_utf8, UTF8SKIP(locinput))) | |
6461 | { | |
6462 | locinput += UTF8SKIP(locinput); | |
c255a977 | 6463 | n++; |
1aa99e6b | 6464 | } |
0fe9bf95 IZ |
6465 | } |
6466 | } | |
5400f398 | 6467 | else { /* Not utf8_target */ |
c255a977 DM |
6468 | if (ST.c1 == ST.c2) { |
6469 | while (locinput <= ST.maxpos && | |
6470 | UCHARAT(locinput) != ST.c1) | |
6471 | locinput++; | |
bbce6d69 | 6472 | } |
c255a977 DM |
6473 | else { |
6474 | while (locinput <= ST.maxpos | |
6475 | && UCHARAT(locinput) != ST.c1 | |
6476 | && UCHARAT(locinput) != ST.c2) | |
6477 | locinput++; | |
a0ed51b3 | 6478 | } |
c255a977 DM |
6479 | n = locinput - ST.oldloc; |
6480 | } | |
6481 | if (locinput > ST.maxpos) | |
6482 | sayNO; | |
c255a977 | 6483 | if (n) { |
eb72505d DM |
6484 | /* In /a{m,n}b/, ST.oldloc is at "a" x m, locinput is |
6485 | * at b; check that everything between oldloc and | |
6486 | * locinput matches */ | |
6487 | char *li = ST.oldloc; | |
c255a977 | 6488 | ST.count += n; |
f9176b44 | 6489 | if (regrepeat(rex, &li, ST.A, reginfo, n, depth) < n) |
4633a7c4 | 6490 | sayNO; |
eb72505d | 6491 | assert(n == REG_INFTY || locinput == li); |
a0d0e21e | 6492 | } |
c255a977 | 6493 | CURLY_SETPAREN(ST.paren, ST.count); |
0a4db386 | 6494 | if (cur_eval && cur_eval->u.eval.close_paren && |
86413ec0 | 6495 | cur_eval->u.eval.close_paren == (U32)ST.paren) { |
0a4db386 YO |
6496 | goto fake_end; |
6497 | } | |
4d5016e5 | 6498 | PUSH_STATE_GOTO(CURLY_B_min_known, ST.B, locinput); |
a0d0e21e | 6499 | } |
118e2215 | 6500 | assert(0); /* NOTREACHED */ |
c255a977 DM |
6501 | |
6502 | ||
6503 | case CURLY_B_min_fail: | |
6504 | /* failed to find B in a non-greedy match where c1,c2 invalid */ | |
c255a977 DM |
6505 | |
6506 | REGCP_UNWIND(ST.cp); | |
a8d1f4b4 DM |
6507 | if (ST.paren) { |
6508 | UNWIND_PAREN(ST.lastparen, ST.lastcloseparen); | |
6509 | } | |
c255a977 | 6510 | /* failed -- move forward one */ |
f73aaa43 | 6511 | { |
eb72505d | 6512 | char *li = locinput; |
f9176b44 | 6513 | if (!regrepeat(rex, &li, ST.A, reginfo, 1, depth)) { |
f73aaa43 DM |
6514 | sayNO; |
6515 | } | |
eb72505d | 6516 | locinput = li; |
f73aaa43 DM |
6517 | } |
6518 | { | |
c255a977 | 6519 | ST.count++; |
c255a977 DM |
6520 | if (ST.count <= ST.max || (ST.max == REG_INFTY && |
6521 | ST.count > 0)) /* count overflow ? */ | |
15272685 | 6522 | { |
c255a977 DM |
6523 | curly_try_B_min: |
6524 | CURLY_SETPAREN(ST.paren, ST.count); | |
0a4db386 | 6525 | if (cur_eval && cur_eval->u.eval.close_paren && |
86413ec0 | 6526 | cur_eval->u.eval.close_paren == (U32)ST.paren) { |
0a4db386 YO |
6527 | goto fake_end; |
6528 | } | |
4d5016e5 | 6529 | PUSH_STATE_GOTO(CURLY_B_min, ST.B, locinput); |
a0d0e21e LW |
6530 | } |
6531 | } | |
c74f6de9 | 6532 | sayNO; |
118e2215 | 6533 | assert(0); /* NOTREACHED */ |
c255a977 DM |
6534 | |
6535 | ||
6536 | curly_try_B_max: | |
6537 | /* a successful greedy match: now try to match B */ | |
40d049e4 | 6538 | if (cur_eval && cur_eval->u.eval.close_paren && |
86413ec0 | 6539 | cur_eval->u.eval.close_paren == (U32)ST.paren) { |
40d049e4 YO |
6540 | goto fake_end; |
6541 | } | |
c255a977 | 6542 | { |
220db18a | 6543 | bool could_match = locinput < reginfo->strend; |
79a2a0e8 | 6544 | |
c255a977 | 6545 | /* If it could work, try it. */ |
79a2a0e8 KW |
6546 | if (ST.c1 != CHRTEST_VOID && could_match) { |
6547 | if (! UTF8_IS_INVARIANT(UCHARAT(locinput)) && utf8_target) | |
6548 | { | |
6549 | could_match = memEQ(locinput, | |
6550 | ST.c1_utf8, | |
6551 | UTF8SKIP(locinput)) | |
6552 | || memEQ(locinput, | |
6553 | ST.c2_utf8, | |
6554 | UTF8SKIP(locinput)); | |
6555 | } | |
6556 | else { | |
6557 | could_match = UCHARAT(locinput) == ST.c1 | |
6558 | || UCHARAT(locinput) == ST.c2; | |
6559 | } | |
6560 | } | |
6561 | if (ST.c1 == CHRTEST_VOID || could_match) { | |
c255a977 | 6562 | CURLY_SETPAREN(ST.paren, ST.count); |
4d5016e5 | 6563 | PUSH_STATE_GOTO(CURLY_B_max, ST.B, locinput); |
118e2215 | 6564 | assert(0); /* NOTREACHED */ |
c255a977 DM |
6565 | } |
6566 | } | |
6567 | /* FALL THROUGH */ | |
3c0563b9 | 6568 | |
c255a977 DM |
6569 | case CURLY_B_max_fail: |
6570 | /* failed to find B in a greedy match */ | |
c255a977 DM |
6571 | |
6572 | REGCP_UNWIND(ST.cp); | |
a8d1f4b4 DM |
6573 | if (ST.paren) { |
6574 | UNWIND_PAREN(ST.lastparen, ST.lastcloseparen); | |
6575 | } | |
c255a977 DM |
6576 | /* back up. */ |
6577 | if (--ST.count < ST.min) | |
6578 | sayNO; | |
eb72505d | 6579 | locinput = HOPc(locinput, -1); |
c255a977 DM |
6580 | goto curly_try_B_max; |
6581 | ||
6582 | #undef ST | |
6583 | ||
3c0563b9 | 6584 | case END: /* last op of main pattern */ |
6bda09f9 | 6585 | fake_end: |
faec1544 DM |
6586 | if (cur_eval) { |
6587 | /* we've just finished A in /(??{A})B/; now continue with B */ | |
faec1544 | 6588 | |
288b8c02 | 6589 | st->u.eval.prev_rex = rex_sv; /* inner */ |
92da3157 DM |
6590 | |
6591 | /* Save *all* the positions. */ | |
6592 | st->u.eval.cp = regcppush(rex, 0, maxopenparen); | |
ec43f78b | 6593 | rex_sv = cur_eval->u.eval.prev_rex; |
aed7b151 | 6594 | is_utf8_pat = reginfo->is_utf8_pat = cBOOL(RX_UTF8(rex_sv)); |
ec43f78b | 6595 | SET_reg_curpm(rex_sv); |
8d919b0a | 6596 | rex = ReANY(rex_sv); |
f8fc2ecf | 6597 | rexi = RXi_GET(rex); |
faec1544 | 6598 | cur_curlyx = cur_eval->u.eval.prev_curlyx; |
34a81e2b | 6599 | |
faec1544 | 6600 | REGCP_SET(st->u.eval.lastcp); |
faec1544 DM |
6601 | |
6602 | /* Restore parens of the outer rex without popping the | |
6603 | * savestack */ | |
92da3157 DM |
6604 | S_regcp_restore(aTHX_ rex, cur_eval->u.eval.lastcp, |
6605 | &maxopenparen); | |
faec1544 DM |
6606 | |
6607 | st->u.eval.prev_eval = cur_eval; | |
6608 | cur_eval = cur_eval->u.eval.prev_eval; | |
6609 | DEBUG_EXECUTE_r( | |
2a49f0f5 JH |
6610 | PerlIO_printf(Perl_debug_log, "%*s EVAL trying tail ... %"UVxf"\n", |
6611 | REPORT_CODE_OFF+depth*2, "",PTR2UV(cur_eval));); | |
e7707071 YO |
6612 | if ( nochange_depth ) |
6613 | nochange_depth--; | |
6614 | ||
4d5016e5 DM |
6615 | PUSH_YES_STATE_GOTO(EVAL_AB, st->u.eval.prev_eval->u.eval.B, |
6616 | locinput); /* match B */ | |
faec1544 DM |
6617 | } |
6618 | ||
3b0527fe | 6619 | if (locinput < reginfo->till) { |
a3621e74 | 6620 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, |
7821416a IZ |
6621 | "%sMatch possible, but length=%ld is smaller than requested=%ld, failing!%s\n", |
6622 | PL_colors[4], | |
6d59b646 DM |
6623 | (long)(locinput - startpos), |
6624 | (long)(reginfo->till - startpos), | |
7821416a | 6625 | PL_colors[5])); |
58e23c8d | 6626 | |
262b90c4 | 6627 | sayNO_SILENT; /* Cannot match: too short. */ |
7821416a | 6628 | } |
262b90c4 | 6629 | sayYES; /* Success! */ |
dad79028 DM |
6630 | |
6631 | case SUCCEED: /* successful SUSPEND/UNLESSM/IFMATCH/CURLYM */ | |
6632 | DEBUG_EXECUTE_r( | |
6633 | PerlIO_printf(Perl_debug_log, | |
6634 | "%*s %ssubpattern success...%s\n", | |
5bc10b2c | 6635 | REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5])); |
262b90c4 | 6636 | sayYES; /* Success! */ |
dad79028 | 6637 | |
40a82448 DM |
6638 | #undef ST |
6639 | #define ST st->u.ifmatch | |
6640 | ||
37f53970 DM |
6641 | { |
6642 | char *newstart; | |
6643 | ||
40a82448 DM |
6644 | case SUSPEND: /* (?>A) */ |
6645 | ST.wanted = 1; | |
37f53970 | 6646 | newstart = locinput; |
9041c2e3 | 6647 | goto do_ifmatch; |
dad79028 | 6648 | |
40a82448 DM |
6649 | case UNLESSM: /* -ve lookaround: (?!A), or with flags, (?<!A) */ |
6650 | ST.wanted = 0; | |
dad79028 DM |
6651 | goto ifmatch_trivial_fail_test; |
6652 | ||
40a82448 DM |
6653 | case IFMATCH: /* +ve lookaround: (?=A), or with flags, (?<=A) */ |
6654 | ST.wanted = 1; | |
dad79028 | 6655 | ifmatch_trivial_fail_test: |
a0ed51b3 | 6656 | if (scan->flags) { |
52657f30 | 6657 | char * const s = HOPBACKc(locinput, scan->flags); |
dad79028 DM |
6658 | if (!s) { |
6659 | /* trivial fail */ | |
24d3c4a9 DM |
6660 | if (logical) { |
6661 | logical = 0; | |
f2338a2e | 6662 | sw = 1 - cBOOL(ST.wanted); |
dad79028 | 6663 | } |
40a82448 | 6664 | else if (ST.wanted) |
dad79028 DM |
6665 | sayNO; |
6666 | next = scan + ARG(scan); | |
6667 | if (next == scan) | |
6668 | next = NULL; | |
6669 | break; | |
6670 | } | |
37f53970 | 6671 | newstart = s; |
a0ed51b3 LW |
6672 | } |
6673 | else | |
37f53970 | 6674 | newstart = locinput; |
a0ed51b3 | 6675 | |
c277df42 | 6676 | do_ifmatch: |
40a82448 | 6677 | ST.me = scan; |
24d3c4a9 | 6678 | ST.logical = logical; |
24d786f4 YO |
6679 | logical = 0; /* XXX: reset state of logical once it has been saved into ST */ |
6680 | ||
40a82448 | 6681 | /* execute body of (?...A) */ |
37f53970 | 6682 | PUSH_YES_STATE_GOTO(IFMATCH_A, NEXTOPER(NEXTOPER(scan)), newstart); |
118e2215 | 6683 | assert(0); /* NOTREACHED */ |
37f53970 | 6684 | } |
40a82448 DM |
6685 | |
6686 | case IFMATCH_A_fail: /* body of (?...A) failed */ | |
6687 | ST.wanted = !ST.wanted; | |
6688 | /* FALL THROUGH */ | |
6689 | ||
6690 | case IFMATCH_A: /* body of (?...A) succeeded */ | |
24d3c4a9 | 6691 | if (ST.logical) { |
f2338a2e | 6692 | sw = cBOOL(ST.wanted); |
40a82448 DM |
6693 | } |
6694 | else if (!ST.wanted) | |
6695 | sayNO; | |
6696 | ||
37f53970 DM |
6697 | if (OP(ST.me) != SUSPEND) { |
6698 | /* restore old position except for (?>...) */ | |
6699 | locinput = st->locinput; | |
40a82448 DM |
6700 | } |
6701 | scan = ST.me + ARG(ST.me); | |
6702 | if (scan == ST.me) | |
6703 | scan = NULL; | |
6704 | continue; /* execute B */ | |
6705 | ||
6706 | #undef ST | |
dad79028 | 6707 | |
3c0563b9 DM |
6708 | case LONGJMP: /* alternative with many branches compiles to |
6709 | * (BRANCHJ; EXACT ...; LONGJMP ) x N */ | |
c277df42 IZ |
6710 | next = scan + ARG(scan); |
6711 | if (next == scan) | |
6712 | next = NULL; | |
a0d0e21e | 6713 | break; |
3c0563b9 DM |
6714 | |
6715 | case COMMIT: /* (*COMMIT) */ | |
220db18a | 6716 | reginfo->cutpoint = reginfo->strend; |
e2e6a0f1 | 6717 | /* FALLTHROUGH */ |
3c0563b9 DM |
6718 | |
6719 | case PRUNE: /* (*PRUNE) */ | |
e2e6a0f1 | 6720 | if (!scan->flags) |
ad64d0ec | 6721 | sv_yes_mark = sv_commit = MUTABLE_SV(rexi->data->data[ ARG( scan ) ]); |
4d5016e5 | 6722 | PUSH_STATE_GOTO(COMMIT_next, next, locinput); |
118e2215 | 6723 | assert(0); /* NOTREACHED */ |
3c0563b9 | 6724 | |
54612592 YO |
6725 | case COMMIT_next_fail: |
6726 | no_final = 1; | |
6727 | /* FALLTHROUGH */ | |
3c0563b9 DM |
6728 | |
6729 | case OPFAIL: /* (*FAIL) */ | |
7f69552c | 6730 | sayNO; |
118e2215 | 6731 | assert(0); /* NOTREACHED */ |
e2e6a0f1 YO |
6732 | |
6733 | #define ST st->u.mark | |
3c0563b9 | 6734 | case MARKPOINT: /* (*MARK:foo) */ |
e2e6a0f1 | 6735 | ST.prev_mark = mark_state; |
5d458dd8 | 6736 | ST.mark_name = sv_commit = sv_yes_mark |
ad64d0ec | 6737 | = MUTABLE_SV(rexi->data->data[ ARG( scan ) ]); |
e2e6a0f1 | 6738 | mark_state = st; |
4d5016e5 DM |
6739 | ST.mark_loc = locinput; |
6740 | PUSH_YES_STATE_GOTO(MARKPOINT_next, next, locinput); | |
118e2215 | 6741 | assert(0); /* NOTREACHED */ |
3c0563b9 | 6742 | |
e2e6a0f1 YO |
6743 | case MARKPOINT_next: |
6744 | mark_state = ST.prev_mark; | |
6745 | sayYES; | |
118e2215 | 6746 | assert(0); /* NOTREACHED */ |
3c0563b9 | 6747 | |
e2e6a0f1 | 6748 | case MARKPOINT_next_fail: |
5d458dd8 | 6749 | if (popmark && sv_eq(ST.mark_name,popmark)) |
e2e6a0f1 YO |
6750 | { |
6751 | if (ST.mark_loc > startpoint) | |
6752 | reginfo->cutpoint = HOPBACKc(ST.mark_loc, 1); | |
6753 | popmark = NULL; /* we found our mark */ | |
6754 | sv_commit = ST.mark_name; | |
6755 | ||
6756 | DEBUG_EXECUTE_r({ | |
5d458dd8 | 6757 | PerlIO_printf(Perl_debug_log, |
e2e6a0f1 YO |
6758 | "%*s %ssetting cutpoint to mark:%"SVf"...%s\n", |
6759 | REPORT_CODE_OFF+depth*2, "", | |
be2597df | 6760 | PL_colors[4], SVfARG(sv_commit), PL_colors[5]); |
e2e6a0f1 YO |
6761 | }); |
6762 | } | |
6763 | mark_state = ST.prev_mark; | |
5d458dd8 YO |
6764 | sv_yes_mark = mark_state ? |
6765 | mark_state->u.mark.mark_name : NULL; | |
e2e6a0f1 | 6766 | sayNO; |
118e2215 | 6767 | assert(0); /* NOTREACHED */ |
3c0563b9 DM |
6768 | |
6769 | case SKIP: /* (*SKIP) */ | |
5d458dd8 | 6770 | if (scan->flags) { |
2bf803e2 | 6771 | /* (*SKIP) : if we fail we cut here*/ |
5d458dd8 | 6772 | ST.mark_name = NULL; |
e2e6a0f1 | 6773 | ST.mark_loc = locinput; |
4d5016e5 | 6774 | PUSH_STATE_GOTO(SKIP_next,next, locinput); |
5d458dd8 | 6775 | } else { |
2bf803e2 | 6776 | /* (*SKIP:NAME) : if there is a (*MARK:NAME) fail where it was, |
5d458dd8 YO |
6777 | otherwise do nothing. Meaning we need to scan |
6778 | */ | |
6779 | regmatch_state *cur = mark_state; | |
ad64d0ec | 6780 | SV *find = MUTABLE_SV(rexi->data->data[ ARG( scan ) ]); |
5d458dd8 YO |
6781 | |
6782 | while (cur) { | |
6783 | if ( sv_eq( cur->u.mark.mark_name, | |
6784 | find ) ) | |
6785 | { | |
6786 | ST.mark_name = find; | |
4d5016e5 | 6787 | PUSH_STATE_GOTO( SKIP_next, next, locinput); |
5d458dd8 YO |
6788 | } |
6789 | cur = cur->u.mark.prev_mark; | |
6790 | } | |
e2e6a0f1 | 6791 | } |
2bf803e2 | 6792 | /* Didn't find our (*MARK:NAME) so ignore this (*SKIP:NAME) */ |
5d458dd8 | 6793 | break; |
3c0563b9 | 6794 | |
5d458dd8 YO |
6795 | case SKIP_next_fail: |
6796 | if (ST.mark_name) { | |
6797 | /* (*CUT:NAME) - Set up to search for the name as we | |
6798 | collapse the stack*/ | |
6799 | popmark = ST.mark_name; | |
6800 | } else { | |
6801 | /* (*CUT) - No name, we cut here.*/ | |
e2e6a0f1 YO |
6802 | if (ST.mark_loc > startpoint) |
6803 | reginfo->cutpoint = HOPBACKc(ST.mark_loc, 1); | |
5d458dd8 YO |
6804 | /* but we set sv_commit to latest mark_name if there |
6805 | is one so they can test to see how things lead to this | |
6806 | cut */ | |
6807 | if (mark_state) | |
6808 | sv_commit=mark_state->u.mark.mark_name; | |
6809 | } | |
e2e6a0f1 YO |
6810 | no_final = 1; |
6811 | sayNO; | |
118e2215 | 6812 | assert(0); /* NOTREACHED */ |
e2e6a0f1 | 6813 | #undef ST |
3c0563b9 DM |
6814 | |
6815 | case LNBREAK: /* \R */ | |
220db18a | 6816 | if ((n=is_LNBREAK_safe(locinput, reginfo->strend, utf8_target))) { |
e1d1eefb | 6817 | locinput += n; |
e1d1eefb YO |
6818 | } else |
6819 | sayNO; | |
6820 | break; | |
6821 | ||
a0d0e21e | 6822 | default: |
b900a521 | 6823 | PerlIO_printf(Perl_error_log, "%"UVxf" %d\n", |
d7d93a81 | 6824 | PTR2UV(scan), OP(scan)); |
cea2e8a9 | 6825 | Perl_croak(aTHX_ "regexp memory corruption"); |
28b98f76 DM |
6826 | |
6827 | /* this is a point to jump to in order to increment | |
6828 | * locinput by one character */ | |
6829 | increment_locinput: | |
e6ca698c | 6830 | assert(!NEXTCHR_IS_EOS); |
28b98f76 DM |
6831 | if (utf8_target) { |
6832 | locinput += PL_utf8skip[nextchr]; | |
7016d6eb | 6833 | /* locinput is allowed to go 1 char off the end, but not 2+ */ |
220db18a | 6834 | if (locinput > reginfo->strend) |
28b98f76 | 6835 | sayNO; |
28b98f76 DM |
6836 | } |
6837 | else | |
3640db6b | 6838 | locinput++; |
28b98f76 | 6839 | break; |
5d458dd8 YO |
6840 | |
6841 | } /* end switch */ | |
95b24440 | 6842 | |
5d458dd8 YO |
6843 | /* switch break jumps here */ |
6844 | scan = next; /* prepare to execute the next op and ... */ | |
6845 | continue; /* ... jump back to the top, reusing st */ | |
118e2215 | 6846 | assert(0); /* NOTREACHED */ |
95b24440 | 6847 | |
40a82448 DM |
6848 | push_yes_state: |
6849 | /* push a state that backtracks on success */ | |
6850 | st->u.yes.prev_yes_state = yes_state; | |
6851 | yes_state = st; | |
6852 | /* FALL THROUGH */ | |
6853 | push_state: | |
6854 | /* push a new regex state, then continue at scan */ | |
6855 | { | |
6856 | regmatch_state *newst; | |
6857 | ||
24b23f37 YO |
6858 | DEBUG_STACK_r({ |
6859 | regmatch_state *cur = st; | |
6860 | regmatch_state *curyes = yes_state; | |
6861 | int curd = depth; | |
6862 | regmatch_slab *slab = PL_regmatch_slab; | |
6863 | for (;curd > -1;cur--,curd--) { | |
6864 | if (cur < SLAB_FIRST(slab)) { | |
6865 | slab = slab->prev; | |
6866 | cur = SLAB_LAST(slab); | |
6867 | } | |
6868 | PerlIO_printf(Perl_error_log, "%*s#%-3d %-10s %s\n", | |
6869 | REPORT_CODE_OFF + 2 + depth * 2,"", | |
13d6edb4 | 6870 | curd, PL_reg_name[cur->resume_state], |
24b23f37 YO |
6871 | (curyes == cur) ? "yes" : "" |
6872 | ); | |
6873 | if (curyes == cur) | |
6874 | curyes = cur->u.yes.prev_yes_state; | |
6875 | } | |
6876 | } else | |
6877 | DEBUG_STATE_pp("push") | |
6878 | ); | |
40a82448 | 6879 | depth++; |
40a82448 DM |
6880 | st->locinput = locinput; |
6881 | newst = st+1; | |
6882 | if (newst > SLAB_LAST(PL_regmatch_slab)) | |
6883 | newst = S_push_slab(aTHX); | |
6884 | PL_regmatch_state = newst; | |
786e8c11 | 6885 | |
4d5016e5 | 6886 | locinput = pushinput; |
40a82448 DM |
6887 | st = newst; |
6888 | continue; | |
118e2215 | 6889 | assert(0); /* NOTREACHED */ |
40a82448 | 6890 | } |
a0d0e21e | 6891 | } |
a687059c | 6892 | |
a0d0e21e LW |
6893 | /* |
6894 | * We get here only if there's trouble -- normally "case END" is | |
6895 | * the terminating point. | |
6896 | */ | |
cea2e8a9 | 6897 | Perl_croak(aTHX_ "corrupted regexp pointers"); |
a0d0e21e | 6898 | /*NOTREACHED*/ |
4633a7c4 LW |
6899 | sayNO; |
6900 | ||
262b90c4 | 6901 | yes: |
77cb431f DM |
6902 | if (yes_state) { |
6903 | /* we have successfully completed a subexpression, but we must now | |
6904 | * pop to the state marked by yes_state and continue from there */ | |
77cb431f | 6905 | assert(st != yes_state); |
5bc10b2c DM |
6906 | #ifdef DEBUGGING |
6907 | while (st != yes_state) { | |
6908 | st--; | |
6909 | if (st < SLAB_FIRST(PL_regmatch_slab)) { | |
6910 | PL_regmatch_slab = PL_regmatch_slab->prev; | |
6911 | st = SLAB_LAST(PL_regmatch_slab); | |
6912 | } | |
e2e6a0f1 | 6913 | DEBUG_STATE_r({ |
54612592 YO |
6914 | if (no_final) { |
6915 | DEBUG_STATE_pp("pop (no final)"); | |
6916 | } else { | |
6917 | DEBUG_STATE_pp("pop (yes)"); | |
6918 | } | |
e2e6a0f1 | 6919 | }); |
5bc10b2c DM |
6920 | depth--; |
6921 | } | |
6922 | #else | |
77cb431f DM |
6923 | while (yes_state < SLAB_FIRST(PL_regmatch_slab) |
6924 | || yes_state > SLAB_LAST(PL_regmatch_slab)) | |
6925 | { | |
6926 | /* not in this slab, pop slab */ | |
6927 | depth -= (st - SLAB_FIRST(PL_regmatch_slab) + 1); | |
6928 | PL_regmatch_slab = PL_regmatch_slab->prev; | |
6929 | st = SLAB_LAST(PL_regmatch_slab); | |
6930 | } | |
6931 | depth -= (st - yes_state); | |
5bc10b2c | 6932 | #endif |
77cb431f DM |
6933 | st = yes_state; |
6934 | yes_state = st->u.yes.prev_yes_state; | |
6935 | PL_regmatch_state = st; | |
24b23f37 | 6936 | |
3640db6b | 6937 | if (no_final) |
5d458dd8 | 6938 | locinput= st->locinput; |
54612592 | 6939 | state_num = st->resume_state + no_final; |
24d3c4a9 | 6940 | goto reenter_switch; |
77cb431f DM |
6941 | } |
6942 | ||
a3621e74 | 6943 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch successful!%s\n", |
e4584336 | 6944 | PL_colors[4], PL_colors[5])); |
02db2b7b | 6945 | |
bf2039a9 | 6946 | if (reginfo->info_aux_eval) { |
19b95bf0 DM |
6947 | /* each successfully executed (?{...}) block does the equivalent of |
6948 | * local $^R = do {...} | |
6949 | * When popping the save stack, all these locals would be undone; | |
6950 | * bypass this by setting the outermost saved $^R to the latest | |
6951 | * value */ | |
4b22688e YO |
6952 | /* I dont know if this is needed or works properly now. |
6953 | * see code related to PL_replgv elsewhere in this file. | |
6954 | * Yves | |
6955 | */ | |
19b95bf0 DM |
6956 | if (oreplsv != GvSV(PL_replgv)) |
6957 | sv_setsv(oreplsv, GvSV(PL_replgv)); | |
6958 | } | |
95b24440 | 6959 | result = 1; |
aa283a38 | 6960 | goto final_exit; |
4633a7c4 LW |
6961 | |
6962 | no: | |
a3621e74 | 6963 | DEBUG_EXECUTE_r( |
7821416a | 6964 | PerlIO_printf(Perl_debug_log, |
786e8c11 | 6965 | "%*s %sfailed...%s\n", |
5bc10b2c | 6966 | REPORT_CODE_OFF+depth*2, "", |
786e8c11 | 6967 | PL_colors[4], PL_colors[5]) |
7821416a | 6968 | ); |
aa283a38 | 6969 | |
262b90c4 | 6970 | no_silent: |
54612592 YO |
6971 | if (no_final) { |
6972 | if (yes_state) { | |
6973 | goto yes; | |
6974 | } else { | |
6975 | goto final_exit; | |
6976 | } | |
6977 | } | |
aa283a38 DM |
6978 | if (depth) { |
6979 | /* there's a previous state to backtrack to */ | |
40a82448 DM |
6980 | st--; |
6981 | if (st < SLAB_FIRST(PL_regmatch_slab)) { | |
6982 | PL_regmatch_slab = PL_regmatch_slab->prev; | |
6983 | st = SLAB_LAST(PL_regmatch_slab); | |
6984 | } | |
6985 | PL_regmatch_state = st; | |
40a82448 | 6986 | locinput= st->locinput; |
40a82448 | 6987 | |
5bc10b2c DM |
6988 | DEBUG_STATE_pp("pop"); |
6989 | depth--; | |
262b90c4 DM |
6990 | if (yes_state == st) |
6991 | yes_state = st->u.yes.prev_yes_state; | |
5bc10b2c | 6992 | |
24d3c4a9 DM |
6993 | state_num = st->resume_state + 1; /* failure = success + 1 */ |
6994 | goto reenter_switch; | |
95b24440 | 6995 | } |
24d3c4a9 | 6996 | result = 0; |
aa283a38 | 6997 | |
262b90c4 | 6998 | final_exit: |
bbe252da | 6999 | if (rex->intflags & PREGf_VERBARG_SEEN) { |
5d458dd8 YO |
7000 | SV *sv_err = get_sv("REGERROR", 1); |
7001 | SV *sv_mrk = get_sv("REGMARK", 1); | |
7002 | if (result) { | |
e2e6a0f1 | 7003 | sv_commit = &PL_sv_no; |
5d458dd8 YO |
7004 | if (!sv_yes_mark) |
7005 | sv_yes_mark = &PL_sv_yes; | |
7006 | } else { | |
7007 | if (!sv_commit) | |
7008 | sv_commit = &PL_sv_yes; | |
7009 | sv_yes_mark = &PL_sv_no; | |
7010 | } | |
7011 | sv_setsv(sv_err, sv_commit); | |
7012 | sv_setsv(sv_mrk, sv_yes_mark); | |
e2e6a0f1 | 7013 | } |
19b95bf0 | 7014 | |
81ed78b2 DM |
7015 | |
7016 | if (last_pushed_cv) { | |
7017 | dSP; | |
7018 | POP_MULTICALL; | |
4f8dbb2d | 7019 | PERL_UNUSED_VAR(SP); |
81ed78b2 DM |
7020 | } |
7021 | ||
9d9163fb DM |
7022 | assert(!result || locinput - reginfo->strbeg >= 0); |
7023 | return result ? locinput - reginfo->strbeg : -1; | |
a687059c LW |
7024 | } |
7025 | ||
7026 | /* | |
7027 | - regrepeat - repeatedly match something simple, report how many | |
d60de1d1 | 7028 | * |
e64f369d KW |
7029 | * What 'simple' means is a node which can be the operand of a quantifier like |
7030 | * '+', or {1,3} | |
7031 | * | |
d60de1d1 DM |
7032 | * startposp - pointer a pointer to the start position. This is updated |
7033 | * to point to the byte following the highest successful | |
7034 | * match. | |
7035 | * p - the regnode to be repeatedly matched against. | |
220db18a | 7036 | * reginfo - struct holding match state, such as strend |
4063ade8 | 7037 | * max - maximum number of things to match. |
d60de1d1 | 7038 | * depth - (for debugging) backtracking depth. |
a687059c | 7039 | */ |
76e3520e | 7040 | STATIC I32 |
272d35c9 | 7041 | S_regrepeat(pTHX_ regexp *prog, char **startposp, const regnode *p, |
f9176b44 | 7042 | regmatch_info *const reginfo, I32 max, int depth) |
a687059c | 7043 | { |
27da23d5 | 7044 | dVAR; |
4063ade8 | 7045 | char *scan; /* Pointer to current position in target string */ |
eb578fdb | 7046 | I32 c; |
220db18a | 7047 | char *loceol = reginfo->strend; /* local version */ |
4063ade8 | 7048 | I32 hardcount = 0; /* How many matches so far */ |
ba44c216 | 7049 | bool utf8_target = reginfo->is_utf8_target; |
3018b823 | 7050 | int to_complement = 0; /* Invert the result? */ |
d513472c | 7051 | UV utf8_flags; |
3018b823 | 7052 | _char_class_number classnum; |
4f55667c SP |
7053 | #ifndef DEBUGGING |
7054 | PERL_UNUSED_ARG(depth); | |
7055 | #endif | |
a0d0e21e | 7056 | |
7918f24d NC |
7057 | PERL_ARGS_ASSERT_REGREPEAT; |
7058 | ||
f73aaa43 | 7059 | scan = *startposp; |
faf11cac HS |
7060 | if (max == REG_INFTY) |
7061 | max = I32_MAX; | |
dfb8f192 | 7062 | else if (! utf8_target && loceol - scan > max) |
7f596f4c | 7063 | loceol = scan + max; |
4063ade8 KW |
7064 | |
7065 | /* Here, for the case of a non-UTF-8 target we have adjusted <loceol> down | |
7066 | * to the maximum of how far we should go in it (leaving it set to the real | |
7067 | * end, if the maximum permissible would take us beyond that). This allows | |
7068 | * us to make the loop exit condition that we haven't gone past <loceol> to | |
7069 | * also mean that we haven't exceeded the max permissible count, saving a | |
7070 | * test each time through the loop. But it assumes that the OP matches a | |
7071 | * single byte, which is true for most of the OPs below when applied to a | |
7072 | * non-UTF-8 target. Those relatively few OPs that don't have this | |
7073 | * characteristic will have to compensate. | |
7074 | * | |
7075 | * There is no adjustment for UTF-8 targets, as the number of bytes per | |
7076 | * character varies. OPs will have to test both that the count is less | |
7077 | * than the max permissible (using <hardcount> to keep track), and that we | |
7078 | * are still within the bounds of the string (using <loceol>. A few OPs | |
7079 | * match a single byte no matter what the encoding. They can omit the max | |
7080 | * test if, for the UTF-8 case, they do the adjustment that was skipped | |
7081 | * above. | |
7082 | * | |
7083 | * Thus, the code above sets things up for the common case; and exceptional | |
7084 | * cases need extra work; the common case is to make sure <scan> doesn't | |
7085 | * go past <loceol>, and for UTF-8 to also use <hardcount> to make sure the | |
7086 | * count doesn't exceed the maximum permissible */ | |
7087 | ||
a0d0e21e | 7088 | switch (OP(p)) { |
22c35a8c | 7089 | case REG_ANY: |
f2ed9b32 | 7090 | if (utf8_target) { |
1aa99e6b | 7091 | while (scan < loceol && hardcount < max && *scan != '\n') { |
ffc61ed2 JH |
7092 | scan += UTF8SKIP(scan); |
7093 | hardcount++; | |
7094 | } | |
7095 | } else { | |
7096 | while (scan < loceol && *scan != '\n') | |
7097 | scan++; | |
a0ed51b3 LW |
7098 | } |
7099 | break; | |
ffc61ed2 | 7100 | case SANY: |
f2ed9b32 | 7101 | if (utf8_target) { |
a0804c9e | 7102 | while (scan < loceol && hardcount < max) { |
def8e4ea JH |
7103 | scan += UTF8SKIP(scan); |
7104 | hardcount++; | |
7105 | } | |
7106 | } | |
7107 | else | |
7108 | scan = loceol; | |
a0ed51b3 | 7109 | break; |
4063ade8 | 7110 | case CANY: /* Move <scan> forward <max> bytes, unless goes off end */ |
9597860a | 7111 | if (utf8_target && loceol - scan > max) { |
4063ade8 KW |
7112 | |
7113 | /* <loceol> hadn't been adjusted in the UTF-8 case */ | |
7114 | scan += max; | |
7115 | } | |
7116 | else { | |
7117 | scan = loceol; | |
7118 | } | |
f33976b4 | 7119 | break; |
59d32103 | 7120 | case EXACT: |
f9176b44 | 7121 | assert(STR_LEN(p) == reginfo->is_utf8_pat ? UTF8SKIP(STRING(p)) : 1); |
613a425d | 7122 | |
59d32103 | 7123 | c = (U8)*STRING(p); |
59d32103 | 7124 | |
5e4a1da1 KW |
7125 | /* Can use a simple loop if the pattern char to match on is invariant |
7126 | * under UTF-8, or both target and pattern aren't UTF-8. Note that we | |
7127 | * can use UTF8_IS_INVARIANT() even if the pattern isn't UTF-8, as it's | |
7128 | * true iff it doesn't matter if the argument is in UTF-8 or not */ | |
f9176b44 | 7129 | if (UTF8_IS_INVARIANT(c) || (! utf8_target && ! reginfo->is_utf8_pat)) { |
e9369824 | 7130 | if (utf8_target && loceol - scan > max) { |
4063ade8 KW |
7131 | /* We didn't adjust <loceol> because is UTF-8, but ok to do so, |
7132 | * since here, to match at all, 1 char == 1 byte */ | |
7133 | loceol = scan + max; | |
7134 | } | |
59d32103 KW |
7135 | while (scan < loceol && UCHARAT(scan) == c) { |
7136 | scan++; | |
7137 | } | |
7138 | } | |
f9176b44 | 7139 | else if (reginfo->is_utf8_pat) { |
5e4a1da1 KW |
7140 | if (utf8_target) { |
7141 | STRLEN scan_char_len; | |
5e4a1da1 | 7142 | |
4063ade8 | 7143 | /* When both target and pattern are UTF-8, we have to do |
5e4a1da1 KW |
7144 | * string EQ */ |
7145 | while (hardcount < max | |
9a902117 KW |
7146 | && scan < loceol |
7147 | && (scan_char_len = UTF8SKIP(scan)) <= STR_LEN(p) | |
5e4a1da1 KW |
7148 | && memEQ(scan, STRING(p), scan_char_len)) |
7149 | { | |
4200a00c | 7150 | scan += scan_char_len; |
5e4a1da1 KW |
7151 | hardcount++; |
7152 | } | |
7153 | } | |
7154 | else if (! UTF8_IS_ABOVE_LATIN1(c)) { | |
b40a2c17 | 7155 | |
5e4a1da1 KW |
7156 | /* Target isn't utf8; convert the character in the UTF-8 |
7157 | * pattern to non-UTF8, and do a simple loop */ | |
94bb8c36 | 7158 | c = TWO_BYTE_UTF8_TO_NATIVE(c, *(STRING(p) + 1)); |
5e4a1da1 KW |
7159 | while (scan < loceol && UCHARAT(scan) == c) { |
7160 | scan++; | |
7161 | } | |
7162 | } /* else pattern char is above Latin1, can't possibly match the | |
7163 | non-UTF-8 target */ | |
b40a2c17 | 7164 | } |
5e4a1da1 | 7165 | else { |
59d32103 | 7166 | |
5e4a1da1 KW |
7167 | /* Here, the string must be utf8; pattern isn't, and <c> is |
7168 | * different in utf8 than not, so can't compare them directly. | |
7169 | * Outside the loop, find the two utf8 bytes that represent c, and | |
7170 | * then look for those in sequence in the utf8 string */ | |
59d32103 KW |
7171 | U8 high = UTF8_TWO_BYTE_HI(c); |
7172 | U8 low = UTF8_TWO_BYTE_LO(c); | |
59d32103 KW |
7173 | |
7174 | while (hardcount < max | |
7175 | && scan + 1 < loceol | |
7176 | && UCHARAT(scan) == high | |
7177 | && UCHARAT(scan + 1) == low) | |
7178 | { | |
7179 | scan += 2; | |
7180 | hardcount++; | |
7181 | } | |
7182 | } | |
7183 | break; | |
5e4a1da1 | 7184 | |
098b07d5 KW |
7185 | case EXACTFA_NO_TRIE: /* This node only generated for non-utf8 patterns */ |
7186 | assert(! reginfo->is_utf8_pat); | |
7187 | /* FALL THROUGH */ | |
2f7f8cb1 | 7188 | case EXACTFA: |
098b07d5 | 7189 | utf8_flags = FOLDEQ_UTF8_NOMIX_ASCII; |
2f7f8cb1 KW |
7190 | goto do_exactf; |
7191 | ||
d4e0b827 | 7192 | case EXACTFL: |
cea315b6 | 7193 | utf8_flags = FOLDEQ_LOCALE; |
17580e7a KW |
7194 | goto do_exactf; |
7195 | ||
2fdb7295 KW |
7196 | case EXACTF: /* This node only generated for non-utf8 patterns */ |
7197 | assert(! reginfo->is_utf8_pat); | |
098b07d5 KW |
7198 | utf8_flags = 0; |
7199 | goto do_exactf; | |
62bf7766 | 7200 | |
3c760661 | 7201 | case EXACTFU_SS: |
9a5a5549 | 7202 | case EXACTFU: |
f9176b44 | 7203 | utf8_flags = reginfo->is_utf8_pat ? FOLDEQ_S2_ALREADY_FOLDED : 0; |
59d32103 | 7204 | |
613a425d KW |
7205 | do_exactf: { |
7206 | int c1, c2; | |
7207 | U8 c1_utf8[UTF8_MAXBYTES+1], c2_utf8[UTF8_MAXBYTES+1]; | |
d4e0b827 | 7208 | |
f9176b44 | 7209 | assert(STR_LEN(p) == reginfo->is_utf8_pat ? UTF8SKIP(STRING(p)) : 1); |
613a425d | 7210 | |
984e6dd1 | 7211 | if (S_setup_EXACTISH_ST_c1_c2(aTHX_ p, &c1, c1_utf8, &c2, c2_utf8, |
aed7b151 | 7212 | reginfo)) |
984e6dd1 | 7213 | { |
613a425d | 7214 | if (c1 == CHRTEST_VOID) { |
49b95fad | 7215 | /* Use full Unicode fold matching */ |
220db18a | 7216 | char *tmpeol = reginfo->strend; |
f9176b44 | 7217 | STRLEN pat_len = reginfo->is_utf8_pat ? UTF8SKIP(STRING(p)) : 1; |
49b95fad KW |
7218 | while (hardcount < max |
7219 | && foldEQ_utf8_flags(scan, &tmpeol, 0, utf8_target, | |
7220 | STRING(p), NULL, pat_len, | |
f9176b44 | 7221 | reginfo->is_utf8_pat, utf8_flags)) |
49b95fad KW |
7222 | { |
7223 | scan = tmpeol; | |
220db18a | 7224 | tmpeol = reginfo->strend; |
49b95fad KW |
7225 | hardcount++; |
7226 | } | |
613a425d KW |
7227 | } |
7228 | else if (utf8_target) { | |
7229 | if (c1 == c2) { | |
4063ade8 KW |
7230 | while (scan < loceol |
7231 | && hardcount < max | |
613a425d KW |
7232 | && memEQ(scan, c1_utf8, UTF8SKIP(scan))) |
7233 | { | |
7234 | scan += UTF8SKIP(scan); | |
7235 | hardcount++; | |
7236 | } | |
7237 | } | |
7238 | else { | |
4063ade8 KW |
7239 | while (scan < loceol |
7240 | && hardcount < max | |
613a425d KW |
7241 | && (memEQ(scan, c1_utf8, UTF8SKIP(scan)) |
7242 | || memEQ(scan, c2_utf8, UTF8SKIP(scan)))) | |
7243 | { | |
7244 | scan += UTF8SKIP(scan); | |
7245 | hardcount++; | |
7246 | } | |
7247 | } | |
7248 | } | |
7249 | else if (c1 == c2) { | |
7250 | while (scan < loceol && UCHARAT(scan) == c1) { | |
7251 | scan++; | |
7252 | } | |
7253 | } | |
7254 | else { | |
7255 | while (scan < loceol && | |
7256 | (UCHARAT(scan) == c1 || UCHARAT(scan) == c2)) | |
7257 | { | |
7258 | scan++; | |
7259 | } | |
7260 | } | |
634c83a2 | 7261 | } |
bbce6d69 | 7262 | break; |
613a425d | 7263 | } |
a0d0e21e | 7264 | case ANYOF: |
e0193e47 | 7265 | if (utf8_target) { |
4e8910e0 | 7266 | while (hardcount < max |
9a902117 | 7267 | && scan < loceol |
3db24e1e | 7268 | && reginclass(prog, p, (U8*)scan, (U8*) loceol, utf8_target)) |
4e8910e0 | 7269 | { |
9a902117 | 7270 | scan += UTF8SKIP(scan); |
ffc61ed2 JH |
7271 | hardcount++; |
7272 | } | |
7273 | } else { | |
32fc9b6a | 7274 | while (scan < loceol && REGINCLASS(prog, p, (U8*)scan)) |
ffc61ed2 JH |
7275 | scan++; |
7276 | } | |
a0d0e21e | 7277 | break; |
4063ade8 | 7278 | |
3018b823 | 7279 | /* The argument (FLAGS) to all the POSIX node types is the class number */ |
980866de | 7280 | |
3018b823 KW |
7281 | case NPOSIXL: |
7282 | to_complement = 1; | |
7283 | /* FALLTHROUGH */ | |
980866de | 7284 | |
3018b823 | 7285 | case POSIXL: |
3018b823 KW |
7286 | if (! utf8_target) { |
7287 | while (scan < loceol && to_complement ^ cBOOL(isFOO_lc(FLAGS(p), | |
7288 | *scan))) | |
a12cf05f | 7289 | { |
3018b823 KW |
7290 | scan++; |
7291 | } | |
7292 | } else { | |
7293 | while (hardcount < max && scan < loceol | |
7294 | && to_complement ^ cBOOL(isFOO_utf8_lc(FLAGS(p), | |
7295 | (U8 *) scan))) | |
7296 | { | |
7297 | scan += UTF8SKIP(scan); | |
ffc61ed2 JH |
7298 | hardcount++; |
7299 | } | |
a0ed51b3 LW |
7300 | } |
7301 | break; | |
0658cdde | 7302 | |
3018b823 KW |
7303 | case POSIXD: |
7304 | if (utf8_target) { | |
7305 | goto utf8_posix; | |
7306 | } | |
7307 | /* FALLTHROUGH */ | |
7308 | ||
0658cdde | 7309 | case POSIXA: |
0430522f | 7310 | if (utf8_target && loceol - scan > max) { |
4063ade8 | 7311 | |
7aee35ff KW |
7312 | /* We didn't adjust <loceol> at the beginning of this routine |
7313 | * because is UTF-8, but it is actually ok to do so, since here, to | |
7314 | * match, 1 char == 1 byte. */ | |
4063ade8 KW |
7315 | loceol = scan + max; |
7316 | } | |
7317 | while (scan < loceol && _generic_isCC_A((U8) *scan, FLAGS(p))) { | |
0658cdde KW |
7318 | scan++; |
7319 | } | |
7320 | break; | |
980866de | 7321 | |
3018b823 KW |
7322 | case NPOSIXD: |
7323 | if (utf8_target) { | |
7324 | to_complement = 1; | |
7325 | goto utf8_posix; | |
7326 | } | |
7327 | /* FALL THROUGH */ | |
980866de | 7328 | |
3018b823 KW |
7329 | case NPOSIXA: |
7330 | if (! utf8_target) { | |
7331 | while (scan < loceol && ! _generic_isCC_A((U8) *scan, FLAGS(p))) { | |
a12cf05f KW |
7332 | scan++; |
7333 | } | |
4063ade8 | 7334 | } |
3018b823 | 7335 | else { |
980866de | 7336 | |
3018b823 KW |
7337 | /* The complement of something that matches only ASCII matches all |
7338 | * UTF-8 variant code points, plus everything in ASCII that isn't | |
7339 | * in the class. */ | |
bedac28b | 7340 | while (hardcount < max && scan < loceol |
3018b823 KW |
7341 | && (! UTF8_IS_INVARIANT(*scan) |
7342 | || ! _generic_isCC_A((U8) *scan, FLAGS(p)))) | |
a12cf05f | 7343 | { |
3018b823 | 7344 | scan += UTF8SKIP(scan); |
ffc61ed2 JH |
7345 | hardcount++; |
7346 | } | |
3018b823 KW |
7347 | } |
7348 | break; | |
980866de | 7349 | |
3018b823 KW |
7350 | case NPOSIXU: |
7351 | to_complement = 1; | |
7352 | /* FALLTHROUGH */ | |
7353 | ||
7354 | case POSIXU: | |
7355 | if (! utf8_target) { | |
7356 | while (scan < loceol && to_complement | |
7357 | ^ cBOOL(_generic_isCC((U8) *scan, FLAGS(p)))) | |
4063ade8 | 7358 | { |
3018b823 KW |
7359 | scan++; |
7360 | } | |
cfaf538b KW |
7361 | } |
7362 | else { | |
3018b823 KW |
7363 | utf8_posix: |
7364 | classnum = (_char_class_number) FLAGS(p); | |
7365 | if (classnum < _FIRST_NON_SWASH_CC) { | |
7366 | ||
7367 | /* Here, a swash is needed for above-Latin1 code points. | |
7368 | * Process as many Latin1 code points using the built-in rules. | |
7369 | * Go to another loop to finish processing upon encountering | |
7370 | * the first Latin1 code point. We could do that in this loop | |
7371 | * as well, but the other way saves having to test if the swash | |
7372 | * has been loaded every time through the loop: extra space to | |
7373 | * save a test. */ | |
7374 | while (hardcount < max && scan < loceol) { | |
7375 | if (UTF8_IS_INVARIANT(*scan)) { | |
7376 | if (! (to_complement ^ cBOOL(_generic_isCC((U8) *scan, | |
7377 | classnum)))) | |
7378 | { | |
7379 | break; | |
7380 | } | |
7381 | scan++; | |
7382 | } | |
7383 | else if (UTF8_IS_DOWNGRADEABLE_START(*scan)) { | |
7384 | if (! (to_complement | |
94bb8c36 KW |
7385 | ^ cBOOL(_generic_isCC(TWO_BYTE_UTF8_TO_NATIVE(*scan, |
7386 | *(scan + 1)), | |
3018b823 KW |
7387 | classnum)))) |
7388 | { | |
7389 | break; | |
7390 | } | |
7391 | scan += 2; | |
7392 | } | |
7393 | else { | |
7394 | goto found_above_latin1; | |
7395 | } | |
7396 | ||
7397 | hardcount++; | |
7398 | } | |
7399 | } | |
7400 | else { | |
7401 | /* For these character classes, the knowledge of how to handle | |
7402 | * every code point is compiled in to Perl via a macro. This | |
7403 | * code is written for making the loops as tight as possible. | |
7404 | * It could be refactored to save space instead */ | |
7405 | switch (classnum) { | |
7406 | case _CC_ENUM_SPACE: /* XXX would require separate code | |
7407 | if we revert the change of \v | |
7408 | matching this */ | |
7409 | /* FALL THROUGH */ | |
7410 | case _CC_ENUM_PSXSPC: | |
7411 | while (hardcount < max | |
7412 | && scan < loceol | |
7413 | && (to_complement ^ cBOOL(isSPACE_utf8(scan)))) | |
7414 | { | |
7415 | scan += UTF8SKIP(scan); | |
7416 | hardcount++; | |
7417 | } | |
7418 | break; | |
7419 | case _CC_ENUM_BLANK: | |
7420 | while (hardcount < max | |
7421 | && scan < loceol | |
7422 | && (to_complement ^ cBOOL(isBLANK_utf8(scan)))) | |
7423 | { | |
7424 | scan += UTF8SKIP(scan); | |
7425 | hardcount++; | |
7426 | } | |
7427 | break; | |
7428 | case _CC_ENUM_XDIGIT: | |
7429 | while (hardcount < max | |
7430 | && scan < loceol | |
7431 | && (to_complement ^ cBOOL(isXDIGIT_utf8(scan)))) | |
7432 | { | |
7433 | scan += UTF8SKIP(scan); | |
7434 | hardcount++; | |
7435 | } | |
7436 | break; | |
7437 | case _CC_ENUM_VERTSPACE: | |
7438 | while (hardcount < max | |
7439 | && scan < loceol | |
7440 | && (to_complement ^ cBOOL(isVERTWS_utf8(scan)))) | |
7441 | { | |
7442 | scan += UTF8SKIP(scan); | |
7443 | hardcount++; | |
7444 | } | |
7445 | break; | |
7446 | case _CC_ENUM_CNTRL: | |
7447 | while (hardcount < max | |
7448 | && scan < loceol | |
7449 | && (to_complement ^ cBOOL(isCNTRL_utf8(scan)))) | |
7450 | { | |
7451 | scan += UTF8SKIP(scan); | |
7452 | hardcount++; | |
7453 | } | |
7454 | break; | |
7455 | default: | |
7456 | Perl_croak(aTHX_ "panic: regrepeat() node %d='%s' has an unexpected character class '%d'", OP(p), PL_reg_name[OP(p)], classnum); | |
7457 | } | |
7458 | } | |
a0ed51b3 | 7459 | } |
3018b823 | 7460 | break; |
4063ade8 | 7461 | |
3018b823 KW |
7462 | found_above_latin1: /* Continuation of POSIXU and NPOSIXU */ |
7463 | ||
7464 | /* Load the swash if not already present */ | |
7465 | if (! PL_utf8_swash_ptrs[classnum]) { | |
7466 | U8 flags = _CORE_SWASH_INIT_ACCEPT_INVLIST; | |
7467 | PL_utf8_swash_ptrs[classnum] = _core_swash_init( | |
2a16ac92 KW |
7468 | "utf8", |
7469 | "", | |
7470 | &PL_sv_undef, 1, 0, | |
7471 | PL_XPosix_ptrs[classnum], &flags); | |
4063ade8 | 7472 | } |
3018b823 KW |
7473 | |
7474 | while (hardcount < max && scan < loceol | |
7475 | && to_complement ^ cBOOL(_generic_utf8( | |
7476 | classnum, | |
7477 | scan, | |
7478 | swash_fetch(PL_utf8_swash_ptrs[classnum], | |
7479 | (U8 *) scan, | |
7480 | TRUE)))) | |
7481 | { | |
7482 | scan += UTF8SKIP(scan); | |
7483 | hardcount++; | |
7484 | } | |
7485 | break; | |
7486 | ||
e1d1eefb | 7487 | case LNBREAK: |
e64f369d KW |
7488 | if (utf8_target) { |
7489 | while (hardcount < max && scan < loceol && | |
7490 | (c=is_LNBREAK_utf8_safe(scan, loceol))) { | |
7491 | scan += c; | |
7492 | hardcount++; | |
7493 | } | |
7494 | } else { | |
7495 | /* LNBREAK can match one or two latin chars, which is ok, but we | |
7496 | * have to use hardcount in this situation, and throw away the | |
7497 | * adjustment to <loceol> done before the switch statement */ | |
220db18a | 7498 | loceol = reginfo->strend; |
e64f369d KW |
7499 | while (scan < loceol && (c=is_LNBREAK_latin1_safe(scan, loceol))) { |
7500 | scan+=c; | |
7501 | hardcount++; | |
7502 | } | |
7503 | } | |
7504 | break; | |
e1d1eefb | 7505 | |
584b1f02 KW |
7506 | case BOUND: |
7507 | case BOUNDA: | |
7508 | case BOUNDL: | |
7509 | case BOUNDU: | |
7510 | case EOS: | |
7511 | case GPOS: | |
7512 | case KEEPS: | |
7513 | case NBOUND: | |
7514 | case NBOUNDA: | |
7515 | case NBOUNDL: | |
7516 | case NBOUNDU: | |
7517 | case OPFAIL: | |
7518 | case SBOL: | |
7519 | case SEOL: | |
7520 | /* These are all 0 width, so match right here or not at all. */ | |
7521 | break; | |
7522 | ||
7523 | default: | |
7524 | Perl_croak(aTHX_ "panic: regrepeat() called with unrecognized node type %d='%s'", OP(p), PL_reg_name[OP(p)]); | |
7525 | assert(0); /* NOTREACHED */ | |
7526 | ||
a0d0e21e | 7527 | } |
a687059c | 7528 | |
a0ed51b3 LW |
7529 | if (hardcount) |
7530 | c = hardcount; | |
7531 | else | |
f73aaa43 DM |
7532 | c = scan - *startposp; |
7533 | *startposp = scan; | |
a687059c | 7534 | |
a3621e74 | 7535 | DEBUG_r({ |
e68ec53f | 7536 | GET_RE_DEBUG_FLAGS_DECL; |
be8e71aa | 7537 | DEBUG_EXECUTE_r({ |
e68ec53f | 7538 | SV * const prop = sv_newmortal(); |
2395827c | 7539 | regprop(prog, prop, p, reginfo); |
e68ec53f | 7540 | PerlIO_printf(Perl_debug_log, |
be8e71aa | 7541 | "%*s %s can match %"IVdf" times out of %"IVdf"...\n", |
e2e6a0f1 | 7542 | REPORT_CODE_OFF + depth*2, "", SvPVX_const(prop),(IV)c,(IV)max); |
a3621e74 | 7543 | }); |
be8e71aa | 7544 | }); |
9041c2e3 | 7545 | |
a0d0e21e | 7546 | return(c); |
a687059c LW |
7547 | } |
7548 | ||
c277df42 | 7549 | |
be8e71aa | 7550 | #if !defined(PERL_IN_XSUB_RE) || defined(PLUGGABLE_RE_EXTENSION) |
c277df42 | 7551 | /* |
6c6525b8 | 7552 | - regclass_swash - prepare the utf8 swash. Wraps the shared core version to |
e0193e47 KW |
7553 | create a copy so that changes the caller makes won't change the shared one. |
7554 | If <altsvp> is non-null, will return NULL in it, for back-compat. | |
6c6525b8 | 7555 | */ |
ffc61ed2 | 7556 | SV * |
5aaab254 | 7557 | Perl_regclass_swash(pTHX_ const regexp *prog, const regnode* node, bool doinit, SV** listsvp, SV **altsvp) |
ffc61ed2 | 7558 | { |
6c6525b8 | 7559 | PERL_ARGS_ASSERT_REGCLASS_SWASH; |
e0193e47 KW |
7560 | |
7561 | if (altsvp) { | |
7562 | *altsvp = NULL; | |
7563 | } | |
7564 | ||
77d654fb | 7565 | return newSVsv(_get_regclass_nonbitmap_data(prog, node, doinit, listsvp, NULL)); |
6c6525b8 | 7566 | } |
6c6525b8 | 7567 | |
77d654fb KW |
7568 | SV * |
7569 | Perl__get_regclass_nonbitmap_data(pTHX_ const regexp *prog, | |
7570 | const regnode* node, | |
7571 | bool doinit, | |
7572 | SV** listsvp, | |
7573 | SV** only_utf8_locale_ptr) | |
6c6525b8 | 7574 | { |
77d654fb KW |
7575 | /* For internal core use only. |
7576 | * Returns the swash for the input 'node' in the regex 'prog'. | |
a498e2f8 | 7577 | * If <doinit> is 'true', will attempt to create the swash if not already |
8c9eb58f | 7578 | * done. |
a498e2f8 KW |
7579 | * If <listsvp> is non-null, will return the printable contents of the |
7580 | * swash. This can be used to get debugging information even before the | |
7581 | * swash exists, by calling this function with 'doinit' set to false, in | |
7582 | * which case the components that will be used to eventually create the | |
7583 | * swash are returned (in a printable form). | |
8c9eb58f KW |
7584 | * Tied intimately to how regcomp.c sets up the data structure */ |
7585 | ||
97aff369 | 7586 | dVAR; |
9e55ce06 | 7587 | SV *sw = NULL; |
a498e2f8 | 7588 | SV *si = NULL; /* Input swash initialization string */ |
7a6c6baa KW |
7589 | SV* invlist = NULL; |
7590 | ||
f8fc2ecf YO |
7591 | RXi_GET_DECL(prog,progi); |
7592 | const struct reg_data * const data = prog ? progi->data : NULL; | |
ffc61ed2 | 7593 | |
77d654fb | 7594 | PERL_ARGS_ASSERT__GET_REGCLASS_NONBITMAP_DATA; |
7918f24d | 7595 | |
1ee208c4 KW |
7596 | assert(ANYOF_FLAGS(node) |
7597 | & (ANYOF_UTF8|ANYOF_NONBITMAP_NON_UTF8|ANYOF_LOC_FOLD)); | |
ccb2541c | 7598 | |
4f639d21 | 7599 | if (data && data->count) { |
a3b680e6 | 7600 | const U32 n = ARG(node); |
ffc61ed2 | 7601 | |
4f639d21 | 7602 | if (data->what[n] == 's') { |
ad64d0ec NC |
7603 | SV * const rv = MUTABLE_SV(data->data[n]); |
7604 | AV * const av = MUTABLE_AV(SvRV(rv)); | |
2d03de9c | 7605 | SV **const ary = AvARRAY(av); |
87367d5f | 7606 | U8 swash_init_flags = _CORE_SWASH_INIT_ACCEPT_INVLIST; |
9041c2e3 | 7607 | |
8c9eb58f | 7608 | si = *ary; /* ary[0] = the string to initialize the swash with */ |
b11f357e | 7609 | |
1ee208c4 KW |
7610 | /* Elements 3 and 4 are either both present or both absent. [3] is |
7611 | * any inversion list generated at compile time; [4] indicates if | |
7a6c6baa | 7612 | * that inversion list has any user-defined properties in it. */ |
1ee208c4 KW |
7613 | if (av_tindex(av) >= 2) { |
7614 | if (only_utf8_locale_ptr | |
7615 | && ary[2] | |
7616 | && ary[2] != &PL_sv_undef) | |
7617 | { | |
7618 | *only_utf8_locale_ptr = ary[2]; | |
7619 | } | |
7620 | else { | |
7621 | *only_utf8_locale_ptr = NULL; | |
7622 | } | |
7623 | ||
b9f2b683 | 7624 | if (av_tindex(av) >= 3) { |
b574cd74 KW |
7625 | invlist = ary[3]; |
7626 | if (SvUV(ary[4])) { | |
7627 | swash_init_flags |= _CORE_SWASH_INIT_USER_DEFINED_PROPERTY; | |
7628 | } | |
7629 | } | |
7630 | else { | |
7631 | invlist = NULL; | |
83199d38 | 7632 | } |
1ee208c4 | 7633 | } |
7a6c6baa | 7634 | |
8c9eb58f KW |
7635 | /* Element [1] is reserved for the set-up swash. If already there, |
7636 | * return it; if not, create it and store it there */ | |
ce0d59fd | 7637 | if (ary[1] && SvROK(ary[1])) { |
f192cf32 KW |
7638 | sw = ary[1]; |
7639 | } | |
cd3413cb KW |
7640 | else if (doinit && ((si && si != &PL_sv_undef) |
7641 | || (invlist && invlist != &PL_sv_undef))) { | |
7a6c6baa KW |
7642 | |
7643 | sw = _core_swash_init("utf8", /* the utf8 package */ | |
7644 | "", /* nameless */ | |
7645 | si, | |
7646 | 1, /* binary */ | |
7647 | 0, /* not from tr/// */ | |
7a6c6baa | 7648 | invlist, |
83199d38 | 7649 | &swash_init_flags); |
ffc61ed2 JH |
7650 | (void)av_store(av, 1, sw); |
7651 | } | |
7652 | } | |
7653 | } | |
7654 | ||
a498e2f8 | 7655 | /* If requested, return a printable version of what this swash matches */ |
7a6c6baa KW |
7656 | if (listsvp) { |
7657 | SV* matches_string = newSVpvn("", 0); | |
7a6c6baa | 7658 | |
a498e2f8 KW |
7659 | /* The swash should be used, if possible, to get the data, as it |
7660 | * contains the resolved data. But this function can be called at | |
7661 | * compile-time, before everything gets resolved, in which case we | |
7662 | * return the currently best available information, which is the string | |
7663 | * that will eventually be used to do that resolving, 'si' */ | |
872dd7e0 KW |
7664 | if ((! sw || (invlist = _get_swash_invlist(sw)) == NULL) |
7665 | && (si && si != &PL_sv_undef)) | |
7666 | { | |
7a6c6baa KW |
7667 | sv_catsv(matches_string, si); |
7668 | } | |
7669 | ||
7670 | /* Add the inversion list to whatever we have. This may have come from | |
7671 | * the swash, or from an input parameter */ | |
7672 | if (invlist) { | |
7673 | sv_catsv(matches_string, _invlist_contents(invlist)); | |
7674 | } | |
7675 | *listsvp = matches_string; | |
7676 | } | |
7677 | ||
ffc61ed2 JH |
7678 | return sw; |
7679 | } | |
3e63bed3 | 7680 | #endif /* !defined(PERL_IN_XSUB_RE) || defined(PLUGGABLE_RE_EXTENSION) */ |
ffc61ed2 JH |
7681 | |
7682 | /* | |
ba7b4546 | 7683 | - reginclass - determine if a character falls into a character class |
832705d4 | 7684 | |
6698fab5 KW |
7685 | n is the ANYOF regnode |
7686 | p is the target string | |
3db24e1e | 7687 | p_end points to one byte beyond the end of the target string |
6698fab5 | 7688 | utf8_target tells whether p is in UTF-8. |
832705d4 | 7689 | |
635cd5d4 | 7690 | Returns true if matched; false otherwise. |
eba1359e | 7691 | |
d5788240 KW |
7692 | Note that this can be a synthetic start class, a combination of various |
7693 | nodes, so things you think might be mutually exclusive, such as locale, | |
7694 | aren't. It can match both locale and non-locale | |
7695 | ||
bbce6d69 | 7696 | */ |
7697 | ||
76e3520e | 7698 | STATIC bool |
3db24e1e | 7699 | S_reginclass(pTHX_ regexp * const prog, const regnode * const n, const U8* const p, const U8* const p_end, const bool utf8_target) |
bbce6d69 | 7700 | { |
27da23d5 | 7701 | dVAR; |
a3b680e6 | 7702 | const char flags = ANYOF_FLAGS(n); |
bbce6d69 | 7703 | bool match = FALSE; |
cc07378b | 7704 | UV c = *p; |
1aa99e6b | 7705 | |
7918f24d NC |
7706 | PERL_ARGS_ASSERT_REGINCLASS; |
7707 | ||
afd2eb18 KW |
7708 | /* If c is not already the code point, get it. Note that |
7709 | * UTF8_IS_INVARIANT() works even if not in UTF-8 */ | |
7710 | if (! UTF8_IS_INVARIANT(c) && utf8_target) { | |
635cd5d4 | 7711 | STRLEN c_len = 0; |
3db24e1e | 7712 | c = utf8n_to_uvchr(p, p_end - p, &c_len, |
6182169b KW |
7713 | (UTF8_ALLOW_DEFAULT & UTF8_ALLOW_ANYUV) |
7714 | | UTF8_ALLOW_FFFF | UTF8_CHECK_ONLY); | |
7715 | /* see [perl #37836] for UTF8_ALLOW_ANYUV; [perl #38293] for | |
7716 | * UTF8_ALLOW_FFFF */ | |
f7ab54c6 | 7717 | if (c_len == (STRLEN)-1) |
e8a70c6f | 7718 | Perl_croak(aTHX_ "Malformed UTF-8 character (fatal)"); |
19f67299 | 7719 | } |
4b3cda86 | 7720 | |
7cdde544 KW |
7721 | /* If this character is potentially in the bitmap, check it */ |
7722 | if (c < 256) { | |
ffc61ed2 JH |
7723 | if (ANYOF_BITMAP_TEST(n, c)) |
7724 | match = TRUE; | |
7bc66b18 | 7725 | else if (flags & ANYOF_NON_UTF8_NON_ASCII_ALL |
11454c59 KW |
7726 | && ! utf8_target |
7727 | && ! isASCII(c)) | |
7728 | { | |
7729 | match = TRUE; | |
7730 | } | |
1462525b | 7731 | else if (flags & ANYOF_LOCALE_FLAGS) { |
b99851e1 | 7732 | if (flags & ANYOF_LOC_FOLD) { |
b99851e1 KW |
7733 | if (ANYOF_BITMAP_TEST(n, PL_fold_locale[c])) { |
7734 | match = TRUE; | |
7735 | } | |
7736 | } | |
ee48a02a | 7737 | if (! match && ANYOF_POSIXL_TEST_ANY_SET(n)) { |
31c7f561 KW |
7738 | |
7739 | /* The data structure is arranged so bits 0, 2, 4, ... are set | |
7740 | * if the class includes the Posix character class given by | |
7741 | * bit/2; and 1, 3, 5, ... are set if the class includes the | |
7742 | * complemented Posix class given by int(bit/2). So we loop | |
7743 | * through the bits, each time changing whether we complement | |
7744 | * the result or not. Suppose for the sake of illustration | |
7745 | * that bits 0-3 mean respectively, \w, \W, \s, \S. If bit 0 | |
7746 | * is set, it means there is a match for this ANYOF node if the | |
7747 | * character is in the class given by the expression (0 / 2 = 0 | |
7748 | * = \w). If it is in that class, isFOO_lc() will return 1, | |
7749 | * and since 'to_complement' is 0, the result will stay TRUE, | |
7750 | * and we exit the loop. Suppose instead that bit 0 is 0, but | |
7751 | * bit 1 is 1. That means there is a match if the character | |
7752 | * matches \W. We won't bother to call isFOO_lc() on bit 0, | |
7753 | * but will on bit 1. On the second iteration 'to_complement' | |
7754 | * will be 1, so the exclusive or will reverse things, so we | |
7755 | * are testing for \W. On the third iteration, 'to_complement' | |
7756 | * will be 0, and we would be testing for \s; the fourth | |
b0d691b2 KW |
7757 | * iteration would test for \S, etc. |
7758 | * | |
7759 | * Note that this code assumes that all the classes are closed | |
7760 | * under folding. For example, if a character matches \w, then | |
7761 | * its fold does too; and vice versa. This should be true for | |
7762 | * any well-behaved locale for all the currently defined Posix | |
7763 | * classes, except for :lower: and :upper:, which are handled | |
7764 | * by the pseudo-class :cased: which matches if either of the | |
7765 | * other two does. To get rid of this assumption, an outer | |
7766 | * loop could be used below to iterate over both the source | |
7767 | * character, and its fold (if different) */ | |
31c7f561 KW |
7768 | |
7769 | int count = 0; | |
7770 | int to_complement = 0; | |
522b3c1e | 7771 | |
31c7f561 | 7772 | while (count < ANYOF_MAX) { |
8efd3f97 | 7773 | if (ANYOF_POSIXL_TEST(n, count) |
31c7f561 KW |
7774 | && to_complement ^ cBOOL(isFOO_lc(count/2, (U8) c))) |
7775 | { | |
7776 | match = TRUE; | |
7777 | break; | |
7778 | } | |
7779 | count++; | |
7780 | to_complement ^= 1; | |
7781 | } | |
ffc61ed2 | 7782 | } |
a0ed51b3 | 7783 | } |
a0ed51b3 LW |
7784 | } |
7785 | ||
31f05a37 | 7786 | |
7cdde544 | 7787 | /* If the bitmap didn't (or couldn't) match, and something outside the |
3b04b210 | 7788 | * bitmap could match, try that. */ |
ef87b810 | 7789 | if (!match) { |
5c2c7b4d | 7790 | if (c >= 256 && (flags & ANYOF_ABOVE_LATIN1_ALL)) { |
10ee90d2 | 7791 | match = TRUE; /* Everything above 255 matches */ |
e051a21d | 7792 | } |
3b04b210 | 7793 | else if ((flags & ANYOF_NONBITMAP_NON_UTF8) |
1ee208c4 KW |
7794 | || (utf8_target && (flags & ANYOF_UTF8)) |
7795 | || ((flags & ANYOF_LOC_FOLD) | |
7796 | && IN_UTF8_CTYPE_LOCALE | |
7797 | && ARG(n) != ANYOF_NONBITMAP_EMPTY)) | |
3b04b210 | 7798 | { |
1ee208c4 KW |
7799 | SV* only_utf8_locale = NULL; |
7800 | SV * const sw = _get_regclass_nonbitmap_data(prog, n, TRUE, 0, | |
7801 | &only_utf8_locale); | |
7cdde544 KW |
7802 | if (sw) { |
7803 | U8 * utf8_p; | |
7804 | if (utf8_target) { | |
7805 | utf8_p = (U8 *) p; | |
e0193e47 KW |
7806 | } else { /* Convert to utf8 */ |
7807 | STRLEN len = 1; | |
7cdde544 KW |
7808 | utf8_p = bytes_to_utf8(p, &len); |
7809 | } | |
f56b6394 | 7810 | |
e0193e47 | 7811 | if (swash_fetch(sw, utf8_p, TRUE)) { |
7cdde544 | 7812 | match = TRUE; |
e0193e47 | 7813 | } |
7cdde544 KW |
7814 | |
7815 | /* If we allocated a string above, free it */ | |
7816 | if (! utf8_target) Safefree(utf8_p); | |
7817 | } | |
1ee208c4 KW |
7818 | if (! match && only_utf8_locale && IN_UTF8_CTYPE_LOCALE) { |
7819 | match = _invlist_contains_cp(only_utf8_locale, c); | |
7820 | } | |
7cdde544 | 7821 | } |
5073ffbd KW |
7822 | |
7823 | if (UNICODE_IS_SUPER(c) | |
ae986089 | 7824 | && (flags & ANYOF_WARN_SUPER) |
5073ffbd KW |
7825 | && ckWARN_d(WARN_NON_UNICODE)) |
7826 | { | |
7827 | Perl_warner(aTHX_ packWARN(WARN_NON_UNICODE), | |
2d88a86a | 7828 | "Matched non-Unicode code point 0x%04"UVXf" against Unicode property; may not be portable", c); |
5073ffbd | 7829 | } |
7cdde544 KW |
7830 | } |
7831 | ||
5dbb0c08 KW |
7832 | #if ANYOF_INVERT != 1 |
7833 | /* Depending on compiler optimization cBOOL takes time, so if don't have to | |
7834 | * use it, don't */ | |
7835 | # error ANYOF_INVERT needs to be set to 1, or guarded with cBOOL below, | |
7836 | #endif | |
7837 | ||
f0fdc1c9 | 7838 | /* The xor complements the return if to invert: 1^1 = 0, 1^0 = 1 */ |
5dbb0c08 | 7839 | return (flags & ANYOF_INVERT) ^ match; |
a0ed51b3 | 7840 | } |
161b471a | 7841 | |
dfe13c55 | 7842 | STATIC U8 * |
ea3daa5d | 7843 | S_reghop3(U8 *s, SSize_t off, const U8* lim) |
9041c2e3 | 7844 | { |
6af86488 KW |
7845 | /* return the position 'off' UTF-8 characters away from 's', forward if |
7846 | * 'off' >= 0, backwards if negative. But don't go outside of position | |
7847 | * 'lim', which better be < s if off < 0 */ | |
7848 | ||
97aff369 | 7849 | dVAR; |
7918f24d NC |
7850 | |
7851 | PERL_ARGS_ASSERT_REGHOP3; | |
7852 | ||
a0ed51b3 | 7853 | if (off >= 0) { |
1aa99e6b | 7854 | while (off-- && s < lim) { |
ffc61ed2 | 7855 | /* XXX could check well-formedness here */ |
a0ed51b3 | 7856 | s += UTF8SKIP(s); |
ffc61ed2 | 7857 | } |
a0ed51b3 LW |
7858 | } |
7859 | else { | |
1de06328 YO |
7860 | while (off++ && s > lim) { |
7861 | s--; | |
7862 | if (UTF8_IS_CONTINUED(*s)) { | |
7863 | while (s > lim && UTF8_IS_CONTINUATION(*s)) | |
7864 | s--; | |
a0ed51b3 | 7865 | } |
1de06328 | 7866 | /* XXX could check well-formedness here */ |
a0ed51b3 LW |
7867 | } |
7868 | } | |
7869 | return s; | |
7870 | } | |
161b471a | 7871 | |
dfe13c55 | 7872 | STATIC U8 * |
ea3daa5d | 7873 | S_reghop4(U8 *s, SSize_t off, const U8* llim, const U8* rlim) |
1de06328 YO |
7874 | { |
7875 | dVAR; | |
7918f24d NC |
7876 | |
7877 | PERL_ARGS_ASSERT_REGHOP4; | |
7878 | ||
1de06328 YO |
7879 | if (off >= 0) { |
7880 | while (off-- && s < rlim) { | |
7881 | /* XXX could check well-formedness here */ | |
7882 | s += UTF8SKIP(s); | |
7883 | } | |
7884 | } | |
7885 | else { | |
7886 | while (off++ && s > llim) { | |
7887 | s--; | |
7888 | if (UTF8_IS_CONTINUED(*s)) { | |
7889 | while (s > llim && UTF8_IS_CONTINUATION(*s)) | |
7890 | s--; | |
7891 | } | |
7892 | /* XXX could check well-formedness here */ | |
7893 | } | |
7894 | } | |
7895 | return s; | |
7896 | } | |
1de06328 | 7897 | |
557f47af DM |
7898 | /* like reghop3, but returns NULL on overrun, rather than returning last |
7899 | * char pos */ | |
7900 | ||
1de06328 | 7901 | STATIC U8 * |
ea3daa5d | 7902 | S_reghopmaybe3(U8* s, SSize_t off, const U8* lim) |
a0ed51b3 | 7903 | { |
97aff369 | 7904 | dVAR; |
7918f24d NC |
7905 | |
7906 | PERL_ARGS_ASSERT_REGHOPMAYBE3; | |
7907 | ||
a0ed51b3 | 7908 | if (off >= 0) { |
1aa99e6b | 7909 | while (off-- && s < lim) { |
ffc61ed2 | 7910 | /* XXX could check well-formedness here */ |
a0ed51b3 | 7911 | s += UTF8SKIP(s); |
ffc61ed2 | 7912 | } |
a0ed51b3 | 7913 | if (off >= 0) |
3dab1dad | 7914 | return NULL; |
a0ed51b3 LW |
7915 | } |
7916 | else { | |
1de06328 YO |
7917 | while (off++ && s > lim) { |
7918 | s--; | |
7919 | if (UTF8_IS_CONTINUED(*s)) { | |
7920 | while (s > lim && UTF8_IS_CONTINUATION(*s)) | |
7921 | s--; | |
a0ed51b3 | 7922 | } |
1de06328 | 7923 | /* XXX could check well-formedness here */ |
a0ed51b3 LW |
7924 | } |
7925 | if (off <= 0) | |
3dab1dad | 7926 | return NULL; |
a0ed51b3 LW |
7927 | } |
7928 | return s; | |
7929 | } | |
51371543 | 7930 | |
a75351a1 DM |
7931 | |
7932 | /* when executing a regex that may have (?{}), extra stuff needs setting | |
7933 | up that will be visible to the called code, even before the current | |
7934 | match has finished. In particular: | |
7935 | ||
7936 | * $_ is localised to the SV currently being matched; | |
7937 | * pos($_) is created if necessary, ready to be updated on each call-out | |
7938 | to code; | |
7939 | * a fake PMOP is created that can be set to PL_curpm (normally PL_curpm | |
7940 | isn't set until the current pattern is successfully finished), so that | |
7941 | $1 etc of the match-so-far can be seen; | |
7942 | * save the old values of subbeg etc of the current regex, and set then | |
7943 | to the current string (again, this is normally only done at the end | |
7944 | of execution) | |
a75351a1 DM |
7945 | */ |
7946 | ||
7947 | static void | |
7948 | S_setup_eval_state(pTHX_ regmatch_info *const reginfo) | |
7949 | { | |
7950 | MAGIC *mg; | |
7951 | regexp *const rex = ReANY(reginfo->prog); | |
bf2039a9 | 7952 | regmatch_info_aux_eval *eval_state = reginfo->info_aux_eval; |
8adc0f72 | 7953 | |
8adc0f72 | 7954 | eval_state->rex = rex; |
a75351a1 | 7955 | |
a75351a1 DM |
7956 | if (reginfo->sv) { |
7957 | /* Make $_ available to executed code. */ | |
7958 | if (reginfo->sv != DEFSV) { | |
7959 | SAVE_DEFSV; | |
7960 | DEFSV_set(reginfo->sv); | |
7961 | } | |
7962 | ||
96c2a8ff | 7963 | if (!(mg = mg_find_mglob(reginfo->sv))) { |
a75351a1 | 7964 | /* prepare for quick setting of pos */ |
96c2a8ff | 7965 | mg = sv_magicext_mglob(reginfo->sv); |
a75351a1 DM |
7966 | mg->mg_len = -1; |
7967 | } | |
8adc0f72 DM |
7968 | eval_state->pos_magic = mg; |
7969 | eval_state->pos = mg->mg_len; | |
25fdce4a | 7970 | eval_state->pos_flags = mg->mg_flags; |
a75351a1 | 7971 | } |
8adc0f72 DM |
7972 | else |
7973 | eval_state->pos_magic = NULL; | |
7974 | ||
a75351a1 | 7975 | if (!PL_reg_curpm) { |
f65e70f5 DM |
7976 | /* PL_reg_curpm is a fake PMOP that we can attach the current |
7977 | * regex to and point PL_curpm at, so that $1 et al are visible | |
7978 | * within a /(?{})/. It's just allocated once per interpreter the | |
7979 | * first time its needed */ | |
a75351a1 DM |
7980 | Newxz(PL_reg_curpm, 1, PMOP); |
7981 | #ifdef USE_ITHREADS | |
7982 | { | |
7983 | SV* const repointer = &PL_sv_undef; | |
7984 | /* this regexp is also owned by the new PL_reg_curpm, which | |
7985 | will try to free it. */ | |
7986 | av_push(PL_regex_padav, repointer); | |
b9f2b683 | 7987 | PL_reg_curpm->op_pmoffset = av_tindex(PL_regex_padav); |
a75351a1 DM |
7988 | PL_regex_pad = AvARRAY(PL_regex_padav); |
7989 | } | |
7990 | #endif | |
7991 | } | |
7992 | SET_reg_curpm(reginfo->prog); | |
8adc0f72 | 7993 | eval_state->curpm = PL_curpm; |
a75351a1 DM |
7994 | PL_curpm = PL_reg_curpm; |
7995 | if (RXp_MATCH_COPIED(rex)) { | |
7996 | /* Here is a serious problem: we cannot rewrite subbeg, | |
7997 | since it may be needed if this match fails. Thus | |
7998 | $` inside (?{}) could fail... */ | |
8adc0f72 DM |
7999 | eval_state->subbeg = rex->subbeg; |
8000 | eval_state->sublen = rex->sublen; | |
8001 | eval_state->suboffset = rex->suboffset; | |
a8ee055f | 8002 | eval_state->subcoffset = rex->subcoffset; |
a75351a1 | 8003 | #ifdef PERL_ANY_COW |
8adc0f72 | 8004 | eval_state->saved_copy = rex->saved_copy; |
a75351a1 DM |
8005 | #endif |
8006 | RXp_MATCH_COPIED_off(rex); | |
8007 | } | |
8008 | else | |
8adc0f72 | 8009 | eval_state->subbeg = NULL; |
a75351a1 DM |
8010 | rex->subbeg = (char *)reginfo->strbeg; |
8011 | rex->suboffset = 0; | |
8012 | rex->subcoffset = 0; | |
8013 | rex->sublen = reginfo->strend - reginfo->strbeg; | |
8014 | } | |
8015 | ||
bf2039a9 DM |
8016 | |
8017 | /* destructor to clear up regmatch_info_aux and regmatch_info_aux_eval */ | |
a75351a1 | 8018 | |
51371543 | 8019 | static void |
bf2039a9 | 8020 | S_cleanup_regmatch_info_aux(pTHX_ void *arg) |
51371543 | 8021 | { |
97aff369 | 8022 | dVAR; |
bf2039a9 DM |
8023 | regmatch_info_aux *aux = (regmatch_info_aux *) arg; |
8024 | regmatch_info_aux_eval *eval_state = aux->info_aux_eval; | |
331b2dcc | 8025 | regmatch_slab *s; |
bf2039a9 | 8026 | |
2ac8ff4b DM |
8027 | Safefree(aux->poscache); |
8028 | ||
331b2dcc | 8029 | if (eval_state) { |
bf2039a9 | 8030 | |
331b2dcc | 8031 | /* undo the effects of S_setup_eval_state() */ |
bf2039a9 | 8032 | |
331b2dcc DM |
8033 | if (eval_state->subbeg) { |
8034 | regexp * const rex = eval_state->rex; | |
8035 | rex->subbeg = eval_state->subbeg; | |
8036 | rex->sublen = eval_state->sublen; | |
8037 | rex->suboffset = eval_state->suboffset; | |
8038 | rex->subcoffset = eval_state->subcoffset; | |
db2c6cb3 | 8039 | #ifdef PERL_ANY_COW |
331b2dcc | 8040 | rex->saved_copy = eval_state->saved_copy; |
ed252734 | 8041 | #endif |
331b2dcc DM |
8042 | RXp_MATCH_COPIED_on(rex); |
8043 | } | |
8044 | if (eval_state->pos_magic) | |
25fdce4a | 8045 | { |
331b2dcc | 8046 | eval_state->pos_magic->mg_len = eval_state->pos; |
25fdce4a FC |
8047 | eval_state->pos_magic->mg_flags = |
8048 | (eval_state->pos_magic->mg_flags & ~MGf_BYTES) | |
8049 | | (eval_state->pos_flags & MGf_BYTES); | |
8050 | } | |
331b2dcc DM |
8051 | |
8052 | PL_curpm = eval_state->curpm; | |
8adc0f72 | 8053 | } |
bf2039a9 | 8054 | |
331b2dcc DM |
8055 | PL_regmatch_state = aux->old_regmatch_state; |
8056 | PL_regmatch_slab = aux->old_regmatch_slab; | |
8057 | ||
8058 | /* free all slabs above current one - this must be the last action | |
8059 | * of this function, as aux and eval_state are allocated within | |
8060 | * slabs and may be freed here */ | |
8061 | ||
8062 | s = PL_regmatch_slab->next; | |
8063 | if (s) { | |
8064 | PL_regmatch_slab->next = NULL; | |
8065 | while (s) { | |
8066 | regmatch_slab * const osl = s; | |
8067 | s = s->next; | |
8068 | Safefree(osl); | |
8069 | } | |
8070 | } | |
51371543 | 8071 | } |
33b8afdf | 8072 | |
8adc0f72 | 8073 | |
33b8afdf | 8074 | STATIC void |
5aaab254 | 8075 | S_to_utf8_substr(pTHX_ regexp *prog) |
33b8afdf | 8076 | { |
7e0d5ad7 KW |
8077 | /* Converts substr fields in prog from bytes to UTF-8, calling fbm_compile |
8078 | * on the converted value */ | |
8079 | ||
a1cac82e | 8080 | int i = 1; |
7918f24d NC |
8081 | |
8082 | PERL_ARGS_ASSERT_TO_UTF8_SUBSTR; | |
8083 | ||
a1cac82e NC |
8084 | do { |
8085 | if (prog->substrs->data[i].substr | |
8086 | && !prog->substrs->data[i].utf8_substr) { | |
8087 | SV* const sv = newSVsv(prog->substrs->data[i].substr); | |
8088 | prog->substrs->data[i].utf8_substr = sv; | |
8089 | sv_utf8_upgrade(sv); | |
610460f9 | 8090 | if (SvVALID(prog->substrs->data[i].substr)) { |
cffe132d | 8091 | if (SvTAIL(prog->substrs->data[i].substr)) { |
610460f9 NC |
8092 | /* Trim the trailing \n that fbm_compile added last |
8093 | time. */ | |
8094 | SvCUR_set(sv, SvCUR(sv) - 1); | |
8095 | /* Whilst this makes the SV technically "invalid" (as its | |
8096 | buffer is no longer followed by "\0") when fbm_compile() | |
8097 | adds the "\n" back, a "\0" is restored. */ | |
cffe132d NC |
8098 | fbm_compile(sv, FBMcf_TAIL); |
8099 | } else | |
8100 | fbm_compile(sv, 0); | |
610460f9 | 8101 | } |
a1cac82e NC |
8102 | if (prog->substrs->data[i].substr == prog->check_substr) |
8103 | prog->check_utf8 = sv; | |
8104 | } | |
8105 | } while (i--); | |
33b8afdf JH |
8106 | } |
8107 | ||
7e0d5ad7 | 8108 | STATIC bool |
5aaab254 | 8109 | S_to_byte_substr(pTHX_ regexp *prog) |
33b8afdf | 8110 | { |
7e0d5ad7 KW |
8111 | /* Converts substr fields in prog from UTF-8 to bytes, calling fbm_compile |
8112 | * on the converted value; returns FALSE if can't be converted. */ | |
8113 | ||
97aff369 | 8114 | dVAR; |
a1cac82e | 8115 | int i = 1; |
7918f24d NC |
8116 | |
8117 | PERL_ARGS_ASSERT_TO_BYTE_SUBSTR; | |
8118 | ||
a1cac82e NC |
8119 | do { |
8120 | if (prog->substrs->data[i].utf8_substr | |
8121 | && !prog->substrs->data[i].substr) { | |
8122 | SV* sv = newSVsv(prog->substrs->data[i].utf8_substr); | |
7e0d5ad7 KW |
8123 | if (! sv_utf8_downgrade(sv, TRUE)) { |
8124 | return FALSE; | |
8125 | } | |
5400f398 KW |
8126 | if (SvVALID(prog->substrs->data[i].utf8_substr)) { |
8127 | if (SvTAIL(prog->substrs->data[i].utf8_substr)) { | |
8128 | /* Trim the trailing \n that fbm_compile added last | |
8129 | time. */ | |
8130 | SvCUR_set(sv, SvCUR(sv) - 1); | |
8131 | fbm_compile(sv, FBMcf_TAIL); | |
8132 | } else | |
8133 | fbm_compile(sv, 0); | |
8134 | } | |
a1cac82e NC |
8135 | prog->substrs->data[i].substr = sv; |
8136 | if (prog->substrs->data[i].utf8_substr == prog->check_utf8) | |
8137 | prog->check_substr = sv; | |
33b8afdf | 8138 | } |
a1cac82e | 8139 | } while (i--); |
7e0d5ad7 KW |
8140 | |
8141 | return TRUE; | |
33b8afdf | 8142 | } |
66610fdd RGS |
8143 | |
8144 | /* | |
8145 | * Local variables: | |
8146 | * c-indentation-style: bsd | |
8147 | * c-basic-offset: 4 | |
14d04a33 | 8148 | * indent-tabs-mode: nil |
66610fdd RGS |
8149 | * End: |
8150 | * | |
14d04a33 | 8151 | * ex: set ts=8 sts=4 sw=4 et: |
37442d52 | 8152 | */ |