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 | |
64935bc6 | 40 | #define B_ON_NON_UTF8_LOCALE_IS_WRONG \ |
89ad707a | 41 | "Use of \\b{} or \\B{} for non-UTF-8 locale is wrong. Assuming a UTF-8 locale" |
64935bc6 | 42 | |
a687059c | 43 | /* |
e50aee73 | 44 | * pregcomp and pregexec -- regsub and regerror are not used in perl |
a687059c LW |
45 | * |
46 | * Copyright (c) 1986 by University of Toronto. | |
47 | * Written by Henry Spencer. Not derived from licensed software. | |
48 | * | |
49 | * Permission is granted to anyone to use this software for any | |
50 | * purpose on any computer system, and to redistribute it freely, | |
51 | * subject to the following restrictions: | |
52 | * | |
53 | * 1. The author is not responsible for the consequences of use of | |
54 | * this software, no matter how awful, even if they arise | |
55 | * from defects in it. | |
56 | * | |
57 | * 2. The origin of this software must not be misrepresented, either | |
58 | * by explicit claim or by omission. | |
59 | * | |
60 | * 3. Altered versions must be plainly marked as such, and must not | |
61 | * be misrepresented as being the original software. | |
62 | * | |
63 | **** Alterations to Henry's code are... | |
64 | **** | |
4bb101f2 | 65 | **** Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, |
1129b882 NC |
66 | **** 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 |
67 | **** by Larry Wall and others | |
a687059c | 68 | **** |
9ef589d8 LW |
69 | **** You may distribute under the terms of either the GNU General Public |
70 | **** License or the Artistic License, as specified in the README file. | |
a687059c LW |
71 | * |
72 | * Beware that some of this code is subtly aware of the way operator | |
73 | * precedence is structured in regular expressions. Serious changes in | |
74 | * regular-expression syntax might require a total rethink. | |
75 | */ | |
76 | #include "EXTERN.h" | |
864dbfa3 | 77 | #define PERL_IN_REGEXEC_C |
a687059c | 78 | #include "perl.h" |
0f5d15d6 | 79 | |
54df2634 NC |
80 | #ifdef PERL_IN_XSUB_RE |
81 | # include "re_comp.h" | |
82 | #else | |
83 | # include "regcomp.h" | |
84 | #endif | |
a687059c | 85 | |
81e983c1 | 86 | #include "inline_invlist.c" |
1b0f46bf | 87 | #include "unicode_constants.h" |
81e983c1 | 88 | |
e1cf74e3 CB |
89 | #ifdef DEBUGGING |
90 | /* At least one required character in the target string is expressible only in | |
91 | * UTF-8. */ | |
92 | static const char* const non_utf8_target_but_utf8_required | |
93 | = "Can't match, because target string needs to be in UTF-8\n"; | |
94 | #endif | |
95 | ||
96 | #define NON_UTF8_TARGET_BUT_UTF8_REQUIRED(target) STMT_START { \ | |
97 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%s", non_utf8_target_but_utf8_required));\ | |
98 | goto target; \ | |
99 | } STMT_END | |
100 | ||
c74f6de9 KW |
101 | #define HAS_NONLATIN1_FOLD_CLOSURE(i) _HAS_NONLATIN1_FOLD_CLOSURE_ONLY_FOR_USE_BY_REGCOMP_DOT_C_AND_REGEXEC_DOT_C(i) |
102 | ||
a687059c LW |
103 | #ifndef STATIC |
104 | #define STATIC static | |
105 | #endif | |
106 | ||
2d66f61e | 107 | /* Valid only for non-utf8 strings: avoids the reginclass |
7e2509c1 KW |
108 | * call if there are no complications: i.e., if everything matchable is |
109 | * straight forward in the bitmap */ | |
3db24e1e | 110 | #define REGINCLASS(prog,p,c) (ANYOF_FLAGS(p) ? reginclass(prog,p,c,c+1,0) \ |
af364d03 | 111 | : ANYOF_BITMAP_TEST(p,*(c))) |
7d3e948e | 112 | |
c277df42 IZ |
113 | /* |
114 | * Forwards. | |
115 | */ | |
116 | ||
f2ed9b32 | 117 | #define CHR_SVLEN(sv) (utf8_target ? sv_len_utf8(sv) : SvCUR(sv)) |
ba44c216 | 118 | #define CHR_DIST(a,b) (reginfo->is_utf8_target ? utf8_distance(a,b) : a - b) |
a0ed51b3 | 119 | |
3dab1dad | 120 | #define HOPc(pos,off) \ |
ba44c216 | 121 | (char *)(reginfo->is_utf8_target \ |
220db18a | 122 | ? reghop3((U8*)pos, off, \ |
9d9163fb | 123 | (U8*)(off >= 0 ? reginfo->strend : reginfo->strbeg)) \ |
3dab1dad | 124 | : (U8*)(pos + off)) |
557f47af | 125 | |
3dab1dad | 126 | #define HOPBACKc(pos, off) \ |
ba44c216 | 127 | (char*)(reginfo->is_utf8_target \ |
9d9163fb DM |
128 | ? reghopmaybe3((U8*)pos, -off, (U8*)(reginfo->strbeg)) \ |
129 | : (pos - off >= reginfo->strbeg) \ | |
8e11feef | 130 | ? (U8*)pos - off \ |
3dab1dad | 131 | : NULL) |
efb30f32 | 132 | |
ba44c216 | 133 | #define HOP3(pos,off,lim) (reginfo->is_utf8_target ? reghop3((U8*)(pos), off, (U8*)(lim)) : (U8*)(pos + off)) |
1aa99e6b | 134 | #define HOP3c(pos,off,lim) ((char*)HOP3(pos,off,lim)) |
1aa99e6b | 135 | |
557f47af DM |
136 | /* lim must be +ve. Returns NULL on overshoot */ |
137 | #define HOPMAYBE3(pos,off,lim) \ | |
138 | (reginfo->is_utf8_target \ | |
139 | ? reghopmaybe3((U8*)pos, off, (U8*)(lim)) \ | |
140 | : ((U8*)pos + off <= lim) \ | |
141 | ? (U8*)pos + off \ | |
142 | : NULL) | |
143 | ||
8e9f2289 DM |
144 | /* like HOP3, but limits the result to <= lim even for the non-utf8 case. |
145 | * off must be >=0; args should be vars rather than expressions */ | |
146 | #define HOP3lim(pos,off,lim) (reginfo->is_utf8_target \ | |
147 | ? reghop3((U8*)(pos), off, (U8*)(lim)) \ | |
148 | : (U8*)((pos + off) > lim ? lim : (pos + off))) | |
149 | ||
2974eaec DM |
150 | #define HOP4(pos,off,llim, rlim) (reginfo->is_utf8_target \ |
151 | ? reghop4((U8*)(pos), off, (U8*)(llim), (U8*)(rlim)) \ | |
152 | : (U8*)(pos + off)) | |
153 | #define HOP4c(pos,off,llim, rlim) ((char*)HOP4(pos,off,llim, rlim)) | |
7016d6eb DM |
154 | |
155 | #define NEXTCHR_EOS -10 /* nextchr has fallen off the end */ | |
156 | #define NEXTCHR_IS_EOS (nextchr < 0) | |
157 | ||
158 | #define SET_nextchr \ | |
220db18a | 159 | nextchr = ((locinput < reginfo->strend) ? UCHARAT(locinput) : NEXTCHR_EOS) |
7016d6eb DM |
160 | |
161 | #define SET_locinput(p) \ | |
162 | locinput = (p); \ | |
163 | SET_nextchr | |
164 | ||
165 | ||
2a16ac92 | 166 | #define LOAD_UTF8_CHARCLASS(swash_ptr, property_name, invlist) STMT_START { \ |
c7304fe2 KW |
167 | if (!swash_ptr) { \ |
168 | U8 flags = _CORE_SWASH_INIT_ACCEPT_INVLIST; \ | |
c7304fe2 | 169 | swash_ptr = _core_swash_init("utf8", property_name, &PL_sv_undef, \ |
2a16ac92 | 170 | 1, 0, invlist, &flags); \ |
c7304fe2 KW |
171 | assert(swash_ptr); \ |
172 | } \ | |
173 | } STMT_END | |
174 | ||
175 | /* If in debug mode, we test that a known character properly matches */ | |
176 | #ifdef DEBUGGING | |
177 | # define LOAD_UTF8_CHARCLASS_DEBUG_TEST(swash_ptr, \ | |
178 | property_name, \ | |
2a16ac92 | 179 | invlist, \ |
c7304fe2 | 180 | utf8_char_in_property) \ |
2a16ac92 | 181 | LOAD_UTF8_CHARCLASS(swash_ptr, property_name, invlist); \ |
c7304fe2 KW |
182 | assert(swash_fetch(swash_ptr, (U8 *) utf8_char_in_property, TRUE)); |
183 | #else | |
184 | # define LOAD_UTF8_CHARCLASS_DEBUG_TEST(swash_ptr, \ | |
185 | property_name, \ | |
2a16ac92 | 186 | invlist, \ |
c7304fe2 | 187 | utf8_char_in_property) \ |
2a16ac92 | 188 | LOAD_UTF8_CHARCLASS(swash_ptr, property_name, invlist) |
c7304fe2 | 189 | #endif |
d1eb3177 | 190 | |
c7304fe2 KW |
191 | #define LOAD_UTF8_CHARCLASS_ALNUM() LOAD_UTF8_CHARCLASS_DEBUG_TEST( \ |
192 | PL_utf8_swash_ptrs[_CC_WORDCHAR], \ | |
2a16ac92 KW |
193 | "", \ |
194 | PL_XPosix_ptrs[_CC_WORDCHAR], \ | |
df38da56 | 195 | LATIN_CAPITAL_LETTER_SHARP_S_UTF8); |
c7304fe2 | 196 | |
c7304fe2 | 197 | #define PLACEHOLDER /* Something for the preprocessor to grab onto */ |
3dab1dad YO |
198 | /* TODO: Combine JUMPABLE and HAS_TEXT to cache OP(rn) */ |
199 | ||
5f80c4cf | 200 | /* for use after a quantifier and before an EXACT-like node -- japhy */ |
c35dcbe2 YO |
201 | /* it would be nice to rework regcomp.sym to generate this stuff. sigh |
202 | * | |
203 | * NOTE that *nothing* that affects backtracking should be in here, specifically | |
204 | * VERBS must NOT be included. JUMPABLE is used to determine if we can ignore a | |
205 | * node that is in between two EXACT like nodes when ascertaining what the required | |
206 | * "follow" character is. This should probably be moved to regex compile time | |
207 | * although it may be done at run time beause of the REF possibility - more | |
208 | * investigation required. -- demerphq | |
209 | */ | |
baa60164 KW |
210 | #define JUMPABLE(rn) ( \ |
211 | OP(rn) == OPEN || \ | |
3e901dc0 | 212 | (OP(rn) == CLOSE && (!cur_eval || cur_eval->u.eval.close_paren != ARG(rn))) || \ |
baa60164 KW |
213 | OP(rn) == EVAL || \ |
214 | OP(rn) == SUSPEND || OP(rn) == IFMATCH || \ | |
215 | OP(rn) == PLUS || OP(rn) == MINMOD || \ | |
216 | OP(rn) == KEEPS || \ | |
217 | (PL_regkind[OP(rn)] == CURLY && ARG1(rn) > 0) \ | |
e2d8ce26 | 218 | ) |
ee9b8eae | 219 | #define IS_EXACT(rn) (PL_regkind[OP(rn)] == EXACT) |
e2d8ce26 | 220 | |
ee9b8eae YO |
221 | #define HAS_TEXT(rn) ( IS_EXACT(rn) || PL_regkind[OP(rn)] == REF ) |
222 | ||
223 | #if 0 | |
224 | /* Currently these are only used when PL_regkind[OP(rn)] == EXACT so | |
a4525e78 | 225 | we don't need this definition. XXX These are now out-of-sync*/ |
ee9b8eae | 226 | #define IS_TEXT(rn) ( OP(rn)==EXACT || OP(rn)==REF || OP(rn)==NREF ) |
098b07d5 | 227 | #define IS_TEXTF(rn) ( OP(rn)==EXACTFU || OP(rn)==EXACTFU_SS || OP(rn)==EXACTFA || OP(rn)==EXACTFA_NO_TRIE || OP(rn)==EXACTF || OP(rn)==REFF || OP(rn)==NREFF ) |
ee9b8eae YO |
228 | #define IS_TEXTFL(rn) ( OP(rn)==EXACTFL || OP(rn)==REFFL || OP(rn)==NREFFL ) |
229 | ||
230 | #else | |
231 | /* ... so we use this as its faster. */ | |
a4525e78 KW |
232 | #define IS_TEXT(rn) ( OP(rn)==EXACT || OP(rn)==EXACTL ) |
233 | #define IS_TEXTFU(rn) ( OP(rn)==EXACTFU || OP(rn)==EXACTFLU8 || OP(rn)==EXACTFU_SS || OP(rn) == EXACTFA || OP(rn) == EXACTFA_NO_TRIE) | |
ee9b8eae YO |
234 | #define IS_TEXTF(rn) ( OP(rn)==EXACTF ) |
235 | #define IS_TEXTFL(rn) ( OP(rn)==EXACTFL ) | |
236 | ||
237 | #endif | |
e2d8ce26 | 238 | |
a84d97b6 HS |
239 | /* |
240 | Search for mandatory following text node; for lookahead, the text must | |
241 | follow but for lookbehind (rn->flags != 0) we skip to the next step. | |
242 | */ | |
baa60164 | 243 | #define FIND_NEXT_IMPT(rn) STMT_START { \ |
3dab1dad YO |
244 | while (JUMPABLE(rn)) { \ |
245 | const OPCODE type = OP(rn); \ | |
246 | if (type == SUSPEND || PL_regkind[type] == CURLY) \ | |
e2d8ce26 | 247 | rn = NEXTOPER(NEXTOPER(rn)); \ |
3dab1dad | 248 | else if (type == PLUS) \ |
cca55fe3 | 249 | rn = NEXTOPER(rn); \ |
3dab1dad | 250 | else if (type == IFMATCH) \ |
a84d97b6 | 251 | rn = (rn->flags == 0) ? NEXTOPER(NEXTOPER(rn)) : rn + ARG(rn); \ |
e2d8ce26 | 252 | else rn += NEXT_OFF(rn); \ |
3dab1dad | 253 | } \ |
5f80c4cf | 254 | } STMT_END |
74750237 | 255 | |
006f26b2 DM |
256 | #define SLAB_FIRST(s) (&(s)->states[0]) |
257 | #define SLAB_LAST(s) (&(s)->states[PERL_REGMATCH_SLAB_SLOTS-1]) | |
258 | ||
a75351a1 | 259 | static void S_setup_eval_state(pTHX_ regmatch_info *const reginfo); |
bf2039a9 | 260 | static void S_cleanup_regmatch_info_aux(pTHX_ void *arg); |
bf2039a9 | 261 | static regmatch_state * S_push_slab(pTHX); |
51371543 | 262 | |
87c0511b | 263 | #define REGCP_PAREN_ELEMS 3 |
f067efbf | 264 | #define REGCP_OTHER_ELEMS 3 |
e0fa7e2b | 265 | #define REGCP_FRAME_ELEMS 1 |
620d5b66 NC |
266 | /* REGCP_FRAME_ELEMS are not part of the REGCP_OTHER_ELEMS and |
267 | * are needed for the regexp context stack bookkeeping. */ | |
268 | ||
76e3520e | 269 | STATIC CHECKPOINT |
92da3157 | 270 | S_regcppush(pTHX_ const regexp *rex, I32 parenfloor, U32 maxopenparen) |
a0d0e21e | 271 | { |
a3b680e6 | 272 | const int retval = PL_savestack_ix; |
92da3157 DM |
273 | const int paren_elems_to_push = |
274 | (maxopenparen - parenfloor) * REGCP_PAREN_ELEMS; | |
e0fa7e2b NC |
275 | const UV total_elems = paren_elems_to_push + REGCP_OTHER_ELEMS; |
276 | const UV elems_shifted = total_elems << SAVE_TIGHT_SHIFT; | |
87c0511b | 277 | I32 p; |
40a82448 | 278 | GET_RE_DEBUG_FLAGS_DECL; |
a0d0e21e | 279 | |
b93070ed DM |
280 | PERL_ARGS_ASSERT_REGCPPUSH; |
281 | ||
e49a9654 | 282 | if (paren_elems_to_push < 0) |
e8a85d26 JH |
283 | Perl_croak(aTHX_ "panic: paren_elems_to_push, %i < 0, maxopenparen: %i parenfloor: %i REGCP_PAREN_ELEMS: %u", |
284 | (int)paren_elems_to_push, (int)maxopenparen, | |
285 | (int)parenfloor, (unsigned)REGCP_PAREN_ELEMS); | |
e49a9654 | 286 | |
e0fa7e2b NC |
287 | if ((elems_shifted >> SAVE_TIGHT_SHIFT) != total_elems) |
288 | Perl_croak(aTHX_ "panic: paren_elems_to_push offset %"UVuf | |
5df417d0 | 289 | " out of range (%lu-%ld)", |
92da3157 DM |
290 | total_elems, |
291 | (unsigned long)maxopenparen, | |
292 | (long)parenfloor); | |
e0fa7e2b | 293 | |
620d5b66 | 294 | SSGROW(total_elems + REGCP_FRAME_ELEMS); |
7f69552c | 295 | |
495f47a5 | 296 | DEBUG_BUFFERS_r( |
92da3157 | 297 | if ((int)maxopenparen > (int)parenfloor) |
495f47a5 DM |
298 | PerlIO_printf(Perl_debug_log, |
299 | "rex=0x%"UVxf" offs=0x%"UVxf": saving capture indices:\n", | |
300 | PTR2UV(rex), | |
301 | PTR2UV(rex->offs) | |
302 | ); | |
303 | ); | |
92da3157 | 304 | for (p = parenfloor+1; p <= (I32)maxopenparen; p++) { |
b1ce53c5 | 305 | /* REGCP_PARENS_ELEMS are pushed per pairs of parentheses. */ |
99a90e59 FC |
306 | SSPUSHIV(rex->offs[p].end); |
307 | SSPUSHIV(rex->offs[p].start); | |
1ca2007e | 308 | SSPUSHINT(rex->offs[p].start_tmp); |
e7707071 | 309 | DEBUG_BUFFERS_r(PerlIO_printf(Perl_debug_log, |
495f47a5 DM |
310 | " \\%"UVuf": %"IVdf"(%"IVdf")..%"IVdf"\n", |
311 | (UV)p, | |
312 | (IV)rex->offs[p].start, | |
313 | (IV)rex->offs[p].start_tmp, | |
314 | (IV)rex->offs[p].end | |
40a82448 | 315 | )); |
a0d0e21e | 316 | } |
b1ce53c5 | 317 | /* REGCP_OTHER_ELEMS are pushed in any case, parentheses or no. */ |
92da3157 | 318 | SSPUSHINT(maxopenparen); |
b93070ed DM |
319 | SSPUSHINT(rex->lastparen); |
320 | SSPUSHINT(rex->lastcloseparen); | |
e0fa7e2b | 321 | SSPUSHUV(SAVEt_REGCONTEXT | elems_shifted); /* Magic cookie. */ |
41123dfd | 322 | |
a0d0e21e LW |
323 | return retval; |
324 | } | |
325 | ||
c277df42 | 326 | /* These are needed since we do not localize EVAL nodes: */ |
ab3bbdeb YO |
327 | #define REGCP_SET(cp) \ |
328 | DEBUG_STATE_r( \ | |
ab3bbdeb | 329 | PerlIO_printf(Perl_debug_log, \ |
e4f74956 | 330 | " Setting an EVAL scope, savestack=%"IVdf"\n", \ |
ab3bbdeb YO |
331 | (IV)PL_savestack_ix)); \ |
332 | cp = PL_savestack_ix | |
c3464db5 | 333 | |
ab3bbdeb | 334 | #define REGCP_UNWIND(cp) \ |
e4f74956 | 335 | DEBUG_STATE_r( \ |
ab3bbdeb | 336 | if (cp != PL_savestack_ix) \ |
e4f74956 YO |
337 | PerlIO_printf(Perl_debug_log, \ |
338 | " Clearing an EVAL scope, savestack=%"IVdf"..%"IVdf"\n", \ | |
ab3bbdeb YO |
339 | (IV)(cp), (IV)PL_savestack_ix)); \ |
340 | regcpblow(cp) | |
c277df42 | 341 | |
a8d1f4b4 DM |
342 | #define UNWIND_PAREN(lp, lcp) \ |
343 | for (n = rex->lastparen; n > lp; n--) \ | |
344 | rex->offs[n].end = -1; \ | |
345 | rex->lastparen = n; \ | |
346 | rex->lastcloseparen = lcp; | |
347 | ||
348 | ||
f067efbf | 349 | STATIC void |
92da3157 | 350 | S_regcppop(pTHX_ regexp *rex, U32 *maxopenparen_p) |
a0d0e21e | 351 | { |
e0fa7e2b | 352 | UV i; |
87c0511b | 353 | U32 paren; |
a3621e74 YO |
354 | GET_RE_DEBUG_FLAGS_DECL; |
355 | ||
7918f24d NC |
356 | PERL_ARGS_ASSERT_REGCPPOP; |
357 | ||
b1ce53c5 | 358 | /* Pop REGCP_OTHER_ELEMS before the parentheses loop starts. */ |
c6bf6a65 | 359 | i = SSPOPUV; |
e0fa7e2b NC |
360 | assert((i & SAVE_MASK) == SAVEt_REGCONTEXT); /* Check that the magic cookie is there. */ |
361 | i >>= SAVE_TIGHT_SHIFT; /* Parentheses elements to pop. */ | |
b93070ed DM |
362 | rex->lastcloseparen = SSPOPINT; |
363 | rex->lastparen = SSPOPINT; | |
92da3157 | 364 | *maxopenparen_p = SSPOPINT; |
b1ce53c5 | 365 | |
620d5b66 | 366 | i -= REGCP_OTHER_ELEMS; |
b1ce53c5 | 367 | /* Now restore the parentheses context. */ |
495f47a5 DM |
368 | DEBUG_BUFFERS_r( |
369 | if (i || rex->lastparen + 1 <= rex->nparens) | |
370 | PerlIO_printf(Perl_debug_log, | |
371 | "rex=0x%"UVxf" offs=0x%"UVxf": restoring capture indices to:\n", | |
372 | PTR2UV(rex), | |
373 | PTR2UV(rex->offs) | |
374 | ); | |
375 | ); | |
92da3157 | 376 | paren = *maxopenparen_p; |
620d5b66 | 377 | for ( ; i > 0; i -= REGCP_PAREN_ELEMS) { |
ea3daa5d | 378 | SSize_t tmps; |
1ca2007e | 379 | rex->offs[paren].start_tmp = SSPOPINT; |
99a90e59 FC |
380 | rex->offs[paren].start = SSPOPIV; |
381 | tmps = SSPOPIV; | |
b93070ed DM |
382 | if (paren <= rex->lastparen) |
383 | rex->offs[paren].end = tmps; | |
495f47a5 DM |
384 | DEBUG_BUFFERS_r( PerlIO_printf(Perl_debug_log, |
385 | " \\%"UVuf": %"IVdf"(%"IVdf")..%"IVdf"%s\n", | |
386 | (UV)paren, | |
387 | (IV)rex->offs[paren].start, | |
388 | (IV)rex->offs[paren].start_tmp, | |
389 | (IV)rex->offs[paren].end, | |
390 | (paren > rex->lastparen ? "(skipped)" : "")); | |
c277df42 | 391 | ); |
87c0511b | 392 | paren--; |
a0d0e21e | 393 | } |
daf18116 | 394 | #if 1 |
dafc8851 JH |
395 | /* It would seem that the similar code in regtry() |
396 | * already takes care of this, and in fact it is in | |
397 | * a better location to since this code can #if 0-ed out | |
398 | * but the code in regtry() is needed or otherwise tests | |
399 | * requiring null fields (pat.t#187 and split.t#{13,14} | |
daf18116 JH |
400 | * (as of patchlevel 7877) will fail. Then again, |
401 | * this code seems to be necessary or otherwise | |
225593e1 DM |
402 | * this erroneously leaves $1 defined: "1" =~ /^(?:(\d)x)?\d$/ |
403 | * --jhi updated by dapm */ | |
b93070ed | 404 | for (i = rex->lastparen + 1; i <= rex->nparens; i++) { |
92da3157 | 405 | if (i > *maxopenparen_p) |
b93070ed DM |
406 | rex->offs[i].start = -1; |
407 | rex->offs[i].end = -1; | |
495f47a5 DM |
408 | DEBUG_BUFFERS_r( PerlIO_printf(Perl_debug_log, |
409 | " \\%"UVuf": %s ..-1 undeffing\n", | |
410 | (UV)i, | |
92da3157 | 411 | (i > *maxopenparen_p) ? "-1" : " " |
495f47a5 | 412 | )); |
a0d0e21e | 413 | } |
dafc8851 | 414 | #endif |
a0d0e21e LW |
415 | } |
416 | ||
74088413 DM |
417 | /* restore the parens and associated vars at savestack position ix, |
418 | * but without popping the stack */ | |
419 | ||
420 | STATIC void | |
92da3157 | 421 | S_regcp_restore(pTHX_ regexp *rex, I32 ix, U32 *maxopenparen_p) |
74088413 DM |
422 | { |
423 | I32 tmpix = PL_savestack_ix; | |
424 | PL_savestack_ix = ix; | |
92da3157 | 425 | regcppop(rex, maxopenparen_p); |
74088413 DM |
426 | PL_savestack_ix = tmpix; |
427 | } | |
428 | ||
02db2b7b | 429 | #define regcpblow(cp) LEAVE_SCOPE(cp) /* Ignores regcppush()ed data. */ |
a0d0e21e | 430 | |
31c7f561 KW |
431 | STATIC bool |
432 | S_isFOO_lc(pTHX_ const U8 classnum, const U8 character) | |
433 | { | |
434 | /* Returns a boolean as to whether or not 'character' is a member of the | |
435 | * Posix character class given by 'classnum' that should be equivalent to a | |
436 | * value in the typedef '_char_class_number'. | |
437 | * | |
438 | * Ideally this could be replaced by a just an array of function pointers | |
439 | * to the C library functions that implement the macros this calls. | |
440 | * However, to compile, the precise function signatures are required, and | |
441 | * these may vary from platform to to platform. To avoid having to figure | |
442 | * out what those all are on each platform, I (khw) am using this method, | |
7aee35ff KW |
443 | * which adds an extra layer of function call overhead (unless the C |
444 | * optimizer strips it away). But we don't particularly care about | |
445 | * performance with locales anyway. */ | |
31c7f561 KW |
446 | |
447 | switch ((_char_class_number) classnum) { | |
15861f94 | 448 | case _CC_ENUM_ALPHANUMERIC: return isALPHANUMERIC_LC(character); |
31c7f561 | 449 | case _CC_ENUM_ALPHA: return isALPHA_LC(character); |
e8d596e0 KW |
450 | case _CC_ENUM_ASCII: return isASCII_LC(character); |
451 | case _CC_ENUM_BLANK: return isBLANK_LC(character); | |
b0d691b2 KW |
452 | case _CC_ENUM_CASED: return isLOWER_LC(character) |
453 | || isUPPER_LC(character); | |
e8d596e0 | 454 | case _CC_ENUM_CNTRL: return isCNTRL_LC(character); |
31c7f561 KW |
455 | case _CC_ENUM_DIGIT: return isDIGIT_LC(character); |
456 | case _CC_ENUM_GRAPH: return isGRAPH_LC(character); | |
457 | case _CC_ENUM_LOWER: return isLOWER_LC(character); | |
458 | case _CC_ENUM_PRINT: return isPRINT_LC(character); | |
459 | case _CC_ENUM_PUNCT: return isPUNCT_LC(character); | |
e8d596e0 | 460 | case _CC_ENUM_SPACE: return isSPACE_LC(character); |
31c7f561 KW |
461 | case _CC_ENUM_UPPER: return isUPPER_LC(character); |
462 | case _CC_ENUM_WORDCHAR: return isWORDCHAR_LC(character); | |
31c7f561 | 463 | case _CC_ENUM_XDIGIT: return isXDIGIT_LC(character); |
31c7f561 KW |
464 | default: /* VERTSPACE should never occur in locales */ |
465 | Perl_croak(aTHX_ "panic: isFOO_lc() has an unexpected character class '%d'", classnum); | |
466 | } | |
467 | ||
e5964223 | 468 | NOT_REACHED; /* NOTREACHED */ |
31c7f561 KW |
469 | return FALSE; |
470 | } | |
471 | ||
3018b823 KW |
472 | STATIC bool |
473 | S_isFOO_utf8_lc(pTHX_ const U8 classnum, const U8* character) | |
474 | { | |
475 | /* Returns a boolean as to whether or not the (well-formed) UTF-8-encoded | |
476 | * 'character' is a member of the Posix character class given by 'classnum' | |
477 | * that should be equivalent to a value in the typedef | |
478 | * '_char_class_number'. | |
479 | * | |
480 | * This just calls isFOO_lc on the code point for the character if it is in | |
2f306ab9 | 481 | * the range 0-255. Outside that range, all characters use Unicode |
3018b823 KW |
482 | * rules, ignoring any locale. So use the Unicode function if this class |
483 | * requires a swash, and use the Unicode macro otherwise. */ | |
484 | ||
485 | PERL_ARGS_ASSERT_ISFOO_UTF8_LC; | |
486 | ||
487 | if (UTF8_IS_INVARIANT(*character)) { | |
488 | return isFOO_lc(classnum, *character); | |
489 | } | |
490 | else if (UTF8_IS_DOWNGRADEABLE_START(*character)) { | |
491 | return isFOO_lc(classnum, | |
94bb8c36 | 492 | TWO_BYTE_UTF8_TO_NATIVE(*character, *(character + 1))); |
3018b823 KW |
493 | } |
494 | ||
613abc6d KW |
495 | _CHECK_AND_OUTPUT_WIDE_LOCALE_UTF8_MSG(character, character + UTF8SKIP(character)); |
496 | ||
3018b823 KW |
497 | if (classnum < _FIRST_NON_SWASH_CC) { |
498 | ||
499 | /* Initialize the swash unless done already */ | |
500 | if (! PL_utf8_swash_ptrs[classnum]) { | |
501 | U8 flags = _CORE_SWASH_INIT_ACCEPT_INVLIST; | |
2a16ac92 KW |
502 | PL_utf8_swash_ptrs[classnum] = |
503 | _core_swash_init("utf8", | |
504 | "", | |
505 | &PL_sv_undef, 1, 0, | |
506 | PL_XPosix_ptrs[classnum], &flags); | |
3018b823 KW |
507 | } |
508 | ||
92a2046b KW |
509 | return cBOOL(swash_fetch(PL_utf8_swash_ptrs[classnum], (U8 *) |
510 | character, | |
511 | TRUE /* is UTF */ )); | |
3018b823 KW |
512 | } |
513 | ||
514 | switch ((_char_class_number) classnum) { | |
779cf272 | 515 | case _CC_ENUM_SPACE: return is_XPERLSPACE_high(character); |
3018b823 KW |
516 | case _CC_ENUM_BLANK: return is_HORIZWS_high(character); |
517 | case _CC_ENUM_XDIGIT: return is_XDIGIT_high(character); | |
518 | case _CC_ENUM_VERTSPACE: return is_VERTWS_high(character); | |
e1ee3960 | 519 | default: break; |
3018b823 KW |
520 | } |
521 | ||
e1ee3960 | 522 | return FALSE; /* Things like CNTRL are always below 256 */ |
3018b823 KW |
523 | } |
524 | ||
a687059c | 525 | /* |
e50aee73 | 526 | * pregexec and friends |
a687059c LW |
527 | */ |
528 | ||
76234dfb | 529 | #ifndef PERL_IN_XSUB_RE |
a687059c | 530 | /* |
c277df42 | 531 | - pregexec - match a regexp against a string |
a687059c | 532 | */ |
c277df42 | 533 | I32 |
5aaab254 | 534 | Perl_pregexec(pTHX_ REGEXP * const prog, char* stringarg, char *strend, |
ea3daa5d | 535 | char *strbeg, SSize_t minend, SV *screamer, U32 nosave) |
8fd1a950 DM |
536 | /* stringarg: the point in the string at which to begin matching */ |
537 | /* strend: pointer to null at end of string */ | |
538 | /* strbeg: real beginning of string */ | |
539 | /* minend: end of match must be >= minend bytes after stringarg. */ | |
540 | /* screamer: SV being matched: only used for utf8 flag, pos() etc; string | |
541 | * itself is accessed via the pointers above */ | |
542 | /* nosave: For optimizations. */ | |
c277df42 | 543 | { |
7918f24d NC |
544 | PERL_ARGS_ASSERT_PREGEXEC; |
545 | ||
c277df42 | 546 | return |
9041c2e3 | 547 | regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL, |
c277df42 IZ |
548 | nosave ? 0 : REXEC_COPY_STR); |
549 | } | |
76234dfb | 550 | #endif |
22e551b9 | 551 | |
cad2e5aa | 552 | |
6eb5f6b9 | 553 | |
1a4edc3c DM |
554 | /* re_intuit_start(): |
555 | * | |
556 | * Based on some optimiser hints, try to find the earliest position in the | |
557 | * string where the regex could match. | |
558 | * | |
559 | * rx: the regex to match against | |
560 | * sv: the SV being matched: only used for utf8 flag; the string | |
561 | * itself is accessed via the pointers below. Note that on | |
562 | * something like an overloaded SV, SvPOK(sv) may be false | |
563 | * and the string pointers may point to something unrelated to | |
564 | * the SV itself. | |
565 | * strbeg: real beginning of string | |
566 | * strpos: the point in the string at which to begin matching | |
567 | * strend: pointer to the byte following the last char of the string | |
568 | * flags currently unused; set to 0 | |
569 | * data: currently unused; set to NULL | |
570 | * | |
571 | * The basic idea of re_intuit_start() is to use some known information | |
572 | * about the pattern, namely: | |
573 | * | |
574 | * a) the longest known anchored substring (i.e. one that's at a | |
575 | * constant offset from the beginning of the pattern; but not | |
576 | * necessarily at a fixed offset from the beginning of the | |
577 | * string); | |
578 | * b) the longest floating substring (i.e. one that's not at a constant | |
579 | * offset from the beginning of the pattern); | |
580 | * c) Whether the pattern is anchored to the string; either | |
581 | * an absolute anchor: /^../, or anchored to \n: /^.../m, | |
582 | * or anchored to pos(): /\G/; | |
583 | * d) A start class: a real or synthetic character class which | |
584 | * represents which characters are legal at the start of the pattern; | |
585 | * | |
586 | * to either quickly reject the match, or to find the earliest position | |
587 | * within the string at which the pattern might match, thus avoiding | |
588 | * running the full NFA engine at those earlier locations, only to | |
589 | * eventually fail and retry further along. | |
590 | * | |
591 | * Returns NULL if the pattern can't match, or returns the address within | |
592 | * the string which is the earliest place the match could occur. | |
593 | * | |
594 | * The longest of the anchored and floating substrings is called 'check' | |
595 | * and is checked first. The other is called 'other' and is checked | |
596 | * second. The 'other' substring may not be present. For example, | |
597 | * | |
598 | * /(abc|xyz)ABC\d{0,3}DEFG/ | |
599 | * | |
600 | * will have | |
601 | * | |
602 | * check substr (float) = "DEFG", offset 6..9 chars | |
603 | * other substr (anchored) = "ABC", offset 3..3 chars | |
604 | * stclass = [ax] | |
605 | * | |
606 | * Be aware that during the course of this function, sometimes 'anchored' | |
607 | * refers to a substring being anchored relative to the start of the | |
608 | * pattern, and sometimes to the pattern itself being anchored relative to | |
609 | * the string. For example: | |
610 | * | |
611 | * /\dabc/: "abc" is anchored to the pattern; | |
612 | * /^\dabc/: "abc" is anchored to the pattern and the string; | |
613 | * /\d+abc/: "abc" is anchored to neither the pattern nor the string; | |
614 | * /^\d+abc/: "abc" is anchored to neither the pattern nor the string, | |
615 | * but the pattern is anchored to the string. | |
52a21eb3 DM |
616 | */ |
617 | ||
cad2e5aa | 618 | char * |
52a21eb3 DM |
619 | Perl_re_intuit_start(pTHX_ |
620 | REGEXP * const rx, | |
621 | SV *sv, | |
622 | const char * const strbeg, | |
623 | char *strpos, | |
624 | char *strend, | |
625 | const U32 flags, | |
626 | re_scream_pos_data *data) | |
cad2e5aa | 627 | { |
8d919b0a | 628 | struct regexp *const prog = ReANY(rx); |
6b071d16 | 629 | SSize_t start_shift = prog->check_offset_min; |
cad2e5aa | 630 | /* Should be nonnegative! */ |
ea3daa5d | 631 | SSize_t end_shift = 0; |
0fc004dd DM |
632 | /* current lowest pos in string where the regex can start matching */ |
633 | char *rx_origin = strpos; | |
eb578fdb | 634 | SV *check; |
f2ed9b32 | 635 | const bool utf8_target = (sv && SvUTF8(sv)) ? 1 : 0; /* if no sv we have to assume bytes */ |
6480a6c4 | 636 | U8 other_ix = 1 - prog->substrs->check_ix; |
6ad5ffb3 | 637 | bool ml_anch = 0; |
8f4bf5fc | 638 | char *other_last = strpos;/* latest pos 'other' substr already checked to */ |
bd61b366 | 639 | char *check_at = NULL; /* check substr found at this pos */ |
bbe252da | 640 | const I32 multiline = prog->extflags & RXf_PMf_MULTILINE; |
f8fc2ecf | 641 | RXi_GET_DECL(prog,progi); |
02d5137b DM |
642 | regmatch_info reginfo_buf; /* create some info to pass to find_byclass */ |
643 | regmatch_info *const reginfo = ®info_buf; | |
a3621e74 YO |
644 | GET_RE_DEBUG_FLAGS_DECL; |
645 | ||
7918f24d | 646 | PERL_ARGS_ASSERT_RE_INTUIT_START; |
c33e64f0 FC |
647 | PERL_UNUSED_ARG(flags); |
648 | PERL_UNUSED_ARG(data); | |
7918f24d | 649 | |
1dc475d0 DM |
650 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, |
651 | "Intuit: trying to determine minimum start position...\n")); | |
652 | ||
fb9bbddb DM |
653 | /* for now, assume that all substr offsets are positive. If at some point |
654 | * in the future someone wants to do clever things with look-behind and | |
655 | * -ve offsets, they'll need to fix up any code in this function | |
656 | * which uses these offsets. See the thread beginning | |
657 | * <20140113145929.GF27210@iabyn.com> | |
658 | */ | |
659 | assert(prog->substrs->data[0].min_offset >= 0); | |
660 | assert(prog->substrs->data[0].max_offset >= 0); | |
661 | assert(prog->substrs->data[1].min_offset >= 0); | |
662 | assert(prog->substrs->data[1].max_offset >= 0); | |
663 | assert(prog->substrs->data[2].min_offset >= 0); | |
664 | assert(prog->substrs->data[2].max_offset >= 0); | |
665 | ||
f7022b5a | 666 | /* for now, assume that if both present, that the floating substring |
83f2232d | 667 | * doesn't start before the anchored substring. |
f7022b5a DM |
668 | * If you break this assumption (e.g. doing better optimisations |
669 | * with lookahead/behind), then you'll need to audit the code in this | |
670 | * function carefully first | |
671 | */ | |
672 | assert( | |
673 | ! ( (prog->anchored_utf8 || prog->anchored_substr) | |
674 | && (prog->float_utf8 || prog->float_substr)) | |
675 | || (prog->float_min_offset >= prog->anchored_offset)); | |
676 | ||
1a4edc3c DM |
677 | /* byte rather than char calculation for efficiency. It fails |
678 | * to quickly reject some cases that can't match, but will reject | |
679 | * them later after doing full char arithmetic */ | |
c344f387 | 680 | if (prog->minlen > strend - strpos) { |
a3621e74 | 681 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, |
1dc475d0 | 682 | " String too short...\n")); |
cad2e5aa | 683 | goto fail; |
2c2d71f5 | 684 | } |
d8da0584 | 685 | |
ab4e48c1 | 686 | RX_MATCH_UTF8_set(rx,utf8_target); |
6c3fea77 | 687 | reginfo->is_utf8_target = cBOOL(utf8_target); |
bf2039a9 | 688 | reginfo->info_aux = NULL; |
9d9163fb | 689 | reginfo->strbeg = strbeg; |
220db18a | 690 | reginfo->strend = strend; |
aed7b151 | 691 | reginfo->is_utf8_pat = cBOOL(RX_UTF8(rx)); |
02d5137b | 692 | reginfo->intuit = 1; |
1cb48e53 DM |
693 | /* not actually used within intuit, but zero for safety anyway */ |
694 | reginfo->poscache_maxiter = 0; | |
02d5137b | 695 | |
f2ed9b32 | 696 | if (utf8_target) { |
33b8afdf JH |
697 | if (!prog->check_utf8 && prog->check_substr) |
698 | to_utf8_substr(prog); | |
699 | check = prog->check_utf8; | |
700 | } else { | |
7e0d5ad7 KW |
701 | if (!prog->check_substr && prog->check_utf8) { |
702 | if (! to_byte_substr(prog)) { | |
6b54ddc5 | 703 | NON_UTF8_TARGET_BUT_UTF8_REQUIRED(fail); |
7e0d5ad7 KW |
704 | } |
705 | } | |
33b8afdf JH |
706 | check = prog->check_substr; |
707 | } | |
274cd312 | 708 | |
1dc475d0 DM |
709 | /* dump the various substring data */ |
710 | DEBUG_OPTIMISE_MORE_r({ | |
711 | int i; | |
712 | for (i=0; i<=2; i++) { | |
713 | SV *sv = (utf8_target ? prog->substrs->data[i].utf8_substr | |
714 | : prog->substrs->data[i].substr); | |
715 | if (!sv) | |
716 | continue; | |
717 | ||
718 | PerlIO_printf(Perl_debug_log, | |
719 | " substrs[%d]: min=%"IVdf" max=%"IVdf" end shift=%"IVdf | |
720 | " useful=%"IVdf" utf8=%d [%s]\n", | |
721 | i, | |
722 | (IV)prog->substrs->data[i].min_offset, | |
723 | (IV)prog->substrs->data[i].max_offset, | |
724 | (IV)prog->substrs->data[i].end_shift, | |
725 | BmUSEFUL(sv), | |
726 | utf8_target ? 1 : 0, | |
727 | SvPEEK(sv)); | |
728 | } | |
729 | }); | |
730 | ||
8e1490ee | 731 | if (prog->intflags & PREGf_ANCH) { /* Match at \G, beg-of-str or after \n */ |
9fc7410e DM |
732 | |
733 | /* ml_anch: check after \n? | |
734 | * | |
0fa70a06 | 735 | * A note about PREGf_IMPLICIT: on an un-anchored pattern beginning |
9fc7410e DM |
736 | * with /.*.../, these flags will have been added by the |
737 | * compiler: | |
738 | * /.*abc/, /.*abc/m: PREGf_IMPLICIT | PREGf_ANCH_MBOL | |
739 | * /.*abc/s: PREGf_IMPLICIT | PREGf_ANCH_SBOL | |
740 | */ | |
7d2d37f5 DM |
741 | ml_anch = (prog->intflags & PREGf_ANCH_MBOL) |
742 | && !(prog->intflags & PREGf_IMPLICIT); | |
cad2e5aa | 743 | |
343c8a29 | 744 | if (!ml_anch && !(prog->intflags & PREGf_IMPLICIT)) { |
c889ccc8 DM |
745 | /* we are only allowed to match at BOS or \G */ |
746 | ||
57fcbfa7 | 747 | /* trivially reject if there's a BOS anchor and we're not at BOS. |
7bb3b9eb DM |
748 | * |
749 | * Note that we don't try to do a similar quick reject for | |
750 | * \G, since generally the caller will have calculated strpos | |
751 | * based on pos() and gofs, so the string is already correctly | |
752 | * anchored by definition; and handling the exceptions would | |
753 | * be too fiddly (e.g. REXEC_IGNOREPOS). | |
57fcbfa7 | 754 | */ |
7bb3b9eb | 755 | if ( strpos != strbeg |
d3d47aac | 756 | && (prog->intflags & PREGf_ANCH_SBOL)) |
c889ccc8 DM |
757 | { |
758 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, | |
1dc475d0 | 759 | " Not at start...\n")); |
c889ccc8 DM |
760 | goto fail; |
761 | } | |
762 | ||
a5d12a4b DM |
763 | /* in the presence of an anchor, the anchored (relative to the |
764 | * start of the regex) substr must also be anchored relative | |
66b7ec5c DM |
765 | * to strpos. So quickly reject if substr isn't found there. |
766 | * This works for \G too, because the caller will already have | |
767 | * subtracted gofs from pos, and gofs is the offset from the | |
768 | * \G to the start of the regex. For example, in /.abc\Gdef/, | |
769 | * where substr="abcdef", pos()=3, gofs=4, offset_min=1: | |
770 | * caller will have set strpos=pos()-4; we look for the substr | |
771 | * at position pos()-4+1, which lines up with the "a" */ | |
a5d12a4b | 772 | |
c889ccc8 | 773 | if (prog->check_offset_min == prog->check_offset_max |
d0d44648 | 774 | && !(prog->intflags & PREGf_CANY_SEEN)) |
c889ccc8 DM |
775 | { |
776 | /* Substring at constant offset from beg-of-str... */ | |
d307bf57 | 777 | SSize_t slen = SvCUR(check); |
343c8a29 | 778 | char *s = HOP3c(strpos, prog->check_offset_min, strend); |
1de06328 | 779 | |
1dc475d0 DM |
780 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, |
781 | " Looking for check substr at fixed offset %"IVdf"...\n", | |
782 | (IV)prog->check_offset_min)); | |
783 | ||
7742aa66 DM |
784 | if (SvTAIL(check)) { |
785 | /* In this case, the regex is anchored at the end too. | |
786 | * Unless it's a multiline match, the lengths must match | |
787 | * exactly, give or take a \n. NB: slen >= 1 since | |
788 | * the last char of check is \n */ | |
789 | if (!multiline | |
790 | && ( strend - s > slen | |
791 | || strend - s < slen - 1 | |
792 | || (strend - s == slen && strend[-1] != '\n'))) | |
c889ccc8 DM |
793 | { |
794 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, | |
1dc475d0 | 795 | " String too long...\n")); |
c889ccc8 DM |
796 | goto fail_finish; |
797 | } | |
798 | /* Now should match s[0..slen-2] */ | |
799 | slen--; | |
c889ccc8 | 800 | } |
d307bf57 DM |
801 | if (slen && (*SvPVX_const(check) != *s |
802 | || (slen > 1 && memNE(SvPVX_const(check), s, slen)))) | |
803 | { | |
804 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, | |
1dc475d0 | 805 | " String not equal...\n")); |
d307bf57 DM |
806 | goto fail_finish; |
807 | } | |
c889ccc8 DM |
808 | |
809 | check_at = s; | |
810 | goto success_at_start; | |
cad2e5aa | 811 | } |
cad2e5aa | 812 | } |
cad2e5aa | 813 | } |
0fc004dd | 814 | |
c0e0ec46 | 815 | end_shift = prog->check_end_shift; |
cad2e5aa | 816 | |
19188028 | 817 | #ifdef DEBUGGING /* 7/99: reports of failure (with the older version) */ |
0033605d | 818 | if (end_shift < 0) |
1de06328 | 819 | Perl_croak(aTHX_ "panic: end_shift: %"IVdf" pattern:\n%s\n ", |
220fc49f | 820 | (IV)end_shift, RX_PRECOMP(prog)); |
2c2d71f5 JH |
821 | #endif |
822 | ||
2c2d71f5 | 823 | restart: |
1de06328 | 824 | |
66b7ec5c DM |
825 | /* This is the (re)entry point of the main loop in this function. |
826 | * The goal of this loop is to: | |
827 | * 1) find the "check" substring in the region rx_origin..strend | |
828 | * (adjusted by start_shift / end_shift). If not found, reject | |
829 | * immediately. | |
830 | * 2) If it exists, look for the "other" substr too if defined; for | |
831 | * example, if the check substr maps to the anchored substr, then | |
832 | * check the floating substr, and vice-versa. If not found, go | |
833 | * back to (1) with rx_origin suitably incremented. | |
834 | * 3) If we find an rx_origin position that doesn't contradict | |
835 | * either of the substrings, then check the possible additional | |
836 | * constraints on rx_origin of /^.../m or a known start class. | |
837 | * If these fail, then depending on which constraints fail, jump | |
838 | * back to here, or to various other re-entry points further along | |
839 | * that skip some of the first steps. | |
840 | * 4) If we pass all those tests, update the BmUSEFUL() count on the | |
841 | * substring. If the start position was determined to be at the | |
842 | * beginning of the string - so, not rejected, but not optimised, | |
843 | * since we have to run regmatch from position 0 - decrement the | |
844 | * BmUSEFUL() count. Otherwise increment it. | |
845 | */ | |
846 | ||
1a4edc3c DM |
847 | |
848 | /* first, look for the 'check' substring */ | |
849 | ||
1de06328 | 850 | { |
c33e64f0 FC |
851 | U8* start_point; |
852 | U8* end_point; | |
c889ccc8 | 853 | |
c889ccc8 | 854 | DEBUG_OPTIMISE_MORE_r({ |
1dc475d0 | 855 | PerlIO_printf(Perl_debug_log, |
ae5d4331 | 856 | " At restart: rx_origin=%"IVdf" Check offset min: %"IVdf |
1dc475d0 | 857 | " Start shift: %"IVdf" End shift %"IVdf |
4d281893 | 858 | " Real end Shift: %"IVdf"\n", |
675e93ee | 859 | (IV)(rx_origin - strbeg), |
c889ccc8 | 860 | (IV)prog->check_offset_min, |
12fbc530 DM |
861 | (IV)start_shift, |
862 | (IV)end_shift, | |
c889ccc8 DM |
863 | (IV)prog->check_end_shift); |
864 | }); | |
1de06328 | 865 | |
0d331aaf | 866 | if (prog->intflags & PREGf_CANY_SEEN) { |
0fc004dd | 867 | start_point= (U8*)(rx_origin + start_shift); |
12fbc530 | 868 | end_point= (U8*)(strend - end_shift); |
557f47af DM |
869 | if (start_point > end_point) |
870 | goto fail_finish; | |
1de06328 | 871 | } else { |
557f47af DM |
872 | end_point = HOP3(strend, -end_shift, strbeg); |
873 | start_point = HOPMAYBE3(rx_origin, start_shift, end_point); | |
874 | if (!start_point) | |
875 | goto fail_finish; | |
1de06328 | 876 | } |
c889ccc8 | 877 | |
557f47af | 878 | |
e0362b86 | 879 | /* If the regex is absolutely anchored to either the start of the |
d3d47aac | 880 | * string (SBOL) or to pos() (ANCH_GPOS), then |
e0362b86 DM |
881 | * check_offset_max represents an upper bound on the string where |
882 | * the substr could start. For the ANCH_GPOS case, we assume that | |
883 | * the caller of intuit will have already set strpos to | |
884 | * pos()-gofs, so in this case strpos + offset_max will still be | |
885 | * an upper bound on the substr. | |
886 | */ | |
c19c836a DM |
887 | if (!ml_anch |
888 | && prog->intflags & PREGf_ANCH | |
e0362b86 | 889 | && prog->check_offset_max != SSize_t_MAX) |
c19c836a | 890 | { |
1a08ba3a | 891 | SSize_t len = SvCUR(check) - !!SvTAIL(check); |
e0362b86 DM |
892 | const char * const anchor = |
893 | (prog->intflags & PREGf_ANCH_GPOS ? strpos : strbeg); | |
894 | ||
895 | /* do a bytes rather than chars comparison. It's conservative; | |
896 | * so it skips doing the HOP if the result can't possibly end | |
897 | * up earlier than the old value of end_point. | |
898 | */ | |
899 | if ((char*)end_point - anchor > prog->check_offset_max) { | |
900 | end_point = HOP3lim((U8*)anchor, | |
901 | prog->check_offset_max, | |
902 | end_point -len) | |
903 | + len; | |
904 | } | |
d6ef1678 DM |
905 | } |
906 | ||
ae5d4331 | 907 | check_at = fbm_instr( start_point, end_point, |
7fba1cd6 | 908 | check, multiline ? FBMrf_MULTILINE : 0); |
c889ccc8 | 909 | |
675e93ee DM |
910 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, |
911 | " doing 'check' fbm scan, [%"IVdf"..%"IVdf"] gave %"IVdf"\n", | |
912 | (IV)((char*)start_point - strbeg), | |
913 | (IV)((char*)end_point - strbeg), | |
914 | (IV)(check_at ? check_at - strbeg : -1) | |
915 | )); | |
916 | ||
8fd34720 DM |
917 | /* Update the count-of-usability, remove useless subpatterns, |
918 | unshift s. */ | |
919 | ||
920 | DEBUG_EXECUTE_r({ | |
921 | RE_PV_QUOTED_DECL(quoted, utf8_target, PERL_DEBUG_PAD_ZERO(0), | |
922 | SvPVX_const(check), RE_SV_DUMPLEN(check), 30); | |
923 | PerlIO_printf(Perl_debug_log, " %s %s substr %s%s%s", | |
924 | (check_at ? "Found" : "Did not find"), | |
925 | (check == (utf8_target ? prog->anchored_utf8 : prog->anchored_substr) | |
926 | ? "anchored" : "floating"), | |
927 | quoted, | |
928 | RE_SV_TAIL(check), | |
929 | (check_at ? " at offset " : "...\n") ); | |
930 | }); | |
2c2d71f5 | 931 | |
8fd34720 DM |
932 | if (!check_at) |
933 | goto fail_finish; | |
8fd34720 DM |
934 | /* set rx_origin to the minimum position where the regex could start |
935 | * matching, given the constraint of the just-matched check substring. | |
936 | * But don't set it lower than previously. | |
937 | */ | |
fdc003fd | 938 | |
8fd34720 DM |
939 | if (check_at - rx_origin > prog->check_offset_max) |
940 | rx_origin = HOP3c(check_at, -prog->check_offset_max, rx_origin); | |
675e93ee DM |
941 | /* Finish the diagnostic message */ |
942 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, | |
943 | "%ld (rx_origin now %"IVdf")...\n", | |
944 | (long)(check_at - strbeg), | |
945 | (IV)(rx_origin - strbeg) | |
946 | )); | |
8fd34720 | 947 | } |
fdc003fd DM |
948 | |
949 | ||
1a4edc3c | 950 | /* now look for the 'other' substring if defined */ |
2c2d71f5 | 951 | |
6480a6c4 DM |
952 | if (utf8_target ? prog->substrs->data[other_ix].utf8_substr |
953 | : prog->substrs->data[other_ix].substr) | |
1de06328 | 954 | { |
30944b6d | 955 | /* Take into account the "other" substring. */ |
6c3343a6 DM |
956 | char *last, *last1; |
957 | char *s; | |
958 | SV* must; | |
959 | struct reg_substr_datum *other; | |
960 | ||
961 | do_other_substr: | |
962 | other = &prog->substrs->data[other_ix]; | |
963 | ||
964 | /* if "other" is anchored: | |
965 | * we've previously found a floating substr starting at check_at. | |
966 | * This means that the regex origin must lie somewhere | |
967 | * between min (rx_origin): HOP3(check_at, -check_offset_max) | |
968 | * and max: HOP3(check_at, -check_offset_min) | |
969 | * (except that min will be >= strpos) | |
970 | * So the fixed substr must lie somewhere between | |
971 | * HOP3(min, anchored_offset) | |
972 | * HOP3(max, anchored_offset) + SvCUR(substr) | |
973 | */ | |
974 | ||
975 | /* if "other" is floating | |
976 | * Calculate last1, the absolute latest point where the | |
977 | * floating substr could start in the string, ignoring any | |
978 | * constraints from the earlier fixed match. It is calculated | |
979 | * as follows: | |
980 | * | |
981 | * strend - prog->minlen (in chars) is the absolute latest | |
982 | * position within the string where the origin of the regex | |
983 | * could appear. The latest start point for the floating | |
984 | * substr is float_min_offset(*) on from the start of the | |
985 | * regex. last1 simply combines thee two offsets. | |
986 | * | |
987 | * (*) You might think the latest start point should be | |
988 | * float_max_offset from the regex origin, and technically | |
989 | * you'd be correct. However, consider | |
990 | * /a\d{2,4}bcd\w/ | |
991 | * Here, float min, max are 3,5 and minlen is 7. | |
992 | * This can match either | |
993 | * /a\d\dbcd\w/ | |
994 | * /a\d\d\dbcd\w/ | |
995 | * /a\d\d\d\dbcd\w/ | |
996 | * In the first case, the regex matches minlen chars; in the | |
997 | * second, minlen+1, in the third, minlen+2. | |
998 | * In the first case, the floating offset is 3 (which equals | |
999 | * float_min), in the second, 4, and in the third, 5 (which | |
1000 | * equals float_max). In all cases, the floating string bcd | |
1001 | * can never start more than 4 chars from the end of the | |
1002 | * string, which equals minlen - float_min. As the substring | |
1003 | * starts to match more than float_min from the start of the | |
1004 | * regex, it makes the regex match more than minlen chars, | |
1005 | * and the two cancel each other out. So we can always use | |
1006 | * float_min - minlen, rather than float_max - minlen for the | |
1007 | * latest position in the string. | |
1008 | * | |
1009 | * Note that -minlen + float_min_offset is equivalent (AFAIKT) | |
1010 | * to CHR_SVLEN(must) - !!SvTAIL(must) + prog->float_end_shift | |
1011 | */ | |
1012 | ||
e7a14a9c | 1013 | assert(prog->minlen >= other->min_offset); |
6c3343a6 DM |
1014 | last1 = HOP3c(strend, |
1015 | other->min_offset - prog->minlen, strbeg); | |
1016 | ||
4d006249 | 1017 | if (other_ix) {/* i.e. if (other-is-float) */ |
6c3343a6 DM |
1018 | /* last is the latest point where the floating substr could |
1019 | * start, *given* any constraints from the earlier fixed | |
1020 | * match. This constraint is that the floating string starts | |
1021 | * <= float_max_offset chars from the regex origin (rx_origin). | |
1022 | * If this value is less than last1, use it instead. | |
eb3831ce | 1023 | */ |
6c3343a6 DM |
1024 | assert(rx_origin <= last1); |
1025 | last = | |
1026 | /* this condition handles the offset==infinity case, and | |
1027 | * is a short-cut otherwise. Although it's comparing a | |
1028 | * byte offset to a char length, it does so in a safe way, | |
1029 | * since 1 char always occupies 1 or more bytes, | |
1030 | * so if a string range is (last1 - rx_origin) bytes, | |
1031 | * it will be less than or equal to (last1 - rx_origin) | |
1032 | * chars; meaning it errs towards doing the accurate HOP3 | |
1033 | * rather than just using last1 as a short-cut */ | |
1034 | (last1 - rx_origin) < other->max_offset | |
1035 | ? last1 | |
1036 | : (char*)HOP3lim(rx_origin, other->max_offset, last1); | |
1037 | } | |
1038 | else { | |
1039 | assert(strpos + start_shift <= check_at); | |
1040 | last = HOP4c(check_at, other->min_offset - start_shift, | |
1041 | strbeg, strend); | |
1042 | } | |
ead917d0 | 1043 | |
6c3343a6 DM |
1044 | s = HOP3c(rx_origin, other->min_offset, strend); |
1045 | if (s < other_last) /* These positions already checked */ | |
1046 | s = other_last; | |
1047 | ||
1048 | must = utf8_target ? other->utf8_substr : other->substr; | |
1049 | assert(SvPOK(must)); | |
675e93ee DM |
1050 | { |
1051 | char *from = s; | |
1052 | char *to = last + SvCUR(must) - (SvTAIL(must)!=0); | |
1053 | ||
88203927 DM |
1054 | if (from > to) { |
1055 | s = NULL; | |
1056 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, | |
1057 | " skipping 'other' fbm scan: %"IVdf" > %"IVdf"\n", | |
1058 | (IV)(from - strbeg), | |
1059 | (IV)(to - strbeg) | |
1060 | )); | |
1061 | } | |
1062 | else { | |
1063 | s = fbm_instr( | |
1064 | (unsigned char*)from, | |
1065 | (unsigned char*)to, | |
1066 | must, | |
1067 | multiline ? FBMrf_MULTILINE : 0 | |
1068 | ); | |
1069 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, | |
1070 | " doing 'other' fbm scan, [%"IVdf"..%"IVdf"] gave %"IVdf"\n", | |
1071 | (IV)(from - strbeg), | |
1072 | (IV)(to - strbeg), | |
1073 | (IV)(s ? s - strbeg : -1) | |
1074 | )); | |
1075 | } | |
675e93ee DM |
1076 | } |
1077 | ||
6c3343a6 DM |
1078 | DEBUG_EXECUTE_r({ |
1079 | RE_PV_QUOTED_DECL(quoted, utf8_target, PERL_DEBUG_PAD_ZERO(0), | |
1080 | SvPVX_const(must), RE_SV_DUMPLEN(must), 30); | |
1081 | PerlIO_printf(Perl_debug_log, " %s %s substr %s%s", | |
1082 | s ? "Found" : "Contradicts", | |
1083 | other_ix ? "floating" : "anchored", | |
1084 | quoted, RE_SV_TAIL(must)); | |
1085 | }); | |
ead917d0 | 1086 | |
ead917d0 | 1087 | |
6c3343a6 DM |
1088 | if (!s) { |
1089 | /* last1 is latest possible substr location. If we didn't | |
1090 | * find it before there, we never will */ | |
1091 | if (last >= last1) { | |
1092 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, | |
675e93ee | 1093 | "; giving up...\n")); |
6c3343a6 | 1094 | goto fail_finish; |
ead917d0 DM |
1095 | } |
1096 | ||
6c3343a6 DM |
1097 | /* try to find the check substr again at a later |
1098 | * position. Maybe next time we'll find the "other" substr | |
1099 | * in range too */ | |
6c3343a6 DM |
1100 | other_last = HOP3c(last, 1, strend) /* highest failure */; |
1101 | rx_origin = | |
4d006249 | 1102 | other_ix /* i.e. if other-is-float */ |
6c3343a6 DM |
1103 | ? HOP3c(rx_origin, 1, strend) |
1104 | : HOP4c(last, 1 - other->min_offset, strbeg, strend); | |
675e93ee DM |
1105 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, |
1106 | "; about to retry %s at offset %ld (rx_origin now %"IVdf")...\n", | |
1107 | (other_ix ? "floating" : "anchored"), | |
1108 | (long)(HOP3c(check_at, 1, strend) - strbeg), | |
1109 | (IV)(rx_origin - strbeg) | |
1110 | )); | |
6c3343a6 DM |
1111 | goto restart; |
1112 | } | |
1113 | else { | |
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 | } |
675e93ee DM |
1129 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, |
1130 | " at offset %ld (rx_origin now %"IVdf")...\n", | |
1131 | (long)(s - strbeg), | |
1132 | (IV)(rx_origin - strbeg) | |
1133 | )); | |
1134 | ||
6c3343a6 | 1135 | } |
cad2e5aa | 1136 | } |
acba93e8 DM |
1137 | else { |
1138 | DEBUG_OPTIMISE_MORE_r( | |
1139 | PerlIO_printf(Perl_debug_log, | |
1140 | " Check-only match: offset min:%"IVdf" max:%"IVdf | |
1c1c599d | 1141 | " check_at:%"IVdf" rx_origin:%"IVdf" rx_origin-check_at:%"IVdf |
675e93ee | 1142 | " strend:%"IVdf"\n", |
acba93e8 DM |
1143 | (IV)prog->check_offset_min, |
1144 | (IV)prog->check_offset_max, | |
675e93ee DM |
1145 | (IV)(check_at-strbeg), |
1146 | (IV)(rx_origin-strbeg), | |
1c1c599d | 1147 | (IV)(rx_origin-check_at), |
675e93ee | 1148 | (IV)(strend-strbeg) |
acba93e8 DM |
1149 | ) |
1150 | ); | |
1151 | } | |
2c2d71f5 | 1152 | |
acba93e8 | 1153 | postprocess_substr_matches: |
0991020e | 1154 | |
1a4edc3c | 1155 | /* handle the extra constraint of /^.../m if present */ |
e3c6feb0 | 1156 | |
7d2d37f5 | 1157 | if (ml_anch && rx_origin != strbeg && rx_origin[-1] != '\n') { |
4620cb61 DM |
1158 | char *s; |
1159 | ||
a62659bd DM |
1160 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, |
1161 | " looking for /^/m anchor")); | |
d0880ea7 DM |
1162 | |
1163 | /* we have failed the constraint of a \n before rx_origin. | |
2e759faa DM |
1164 | * Find the next \n, if any, even if it's beyond the current |
1165 | * anchored and/or floating substrings. Whether we should be | |
1166 | * scanning ahead for the next \n or the next substr is debatable. | |
1167 | * On the one hand you'd expect rare substrings to appear less | |
1168 | * often than \n's. On the other hand, searching for \n means | |
675e93ee | 1169 | * we're effectively flipping between check_substr and "\n" on each |
2e759faa DM |
1170 | * iteration as the current "rarest" string candidate, which |
1171 | * means for example that we'll quickly reject the whole string if | |
1172 | * hasn't got a \n, rather than trying every substr position | |
1173 | * first | |
1174 | */ | |
d0880ea7 | 1175 | |
4620cb61 DM |
1176 | s = HOP3c(strend, - prog->minlen, strpos); |
1177 | if (s <= rx_origin || | |
1178 | ! ( rx_origin = (char *)memchr(rx_origin, '\n', s - rx_origin))) | |
1179 | { | |
d0880ea7 DM |
1180 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, |
1181 | " Did not find /%s^%s/m...\n", | |
1182 | PL_colors[0], PL_colors[1])); | |
a62659bd DM |
1183 | goto fail_finish; |
1184 | } | |
d0880ea7 | 1185 | |
4ada1233 DM |
1186 | /* earliest possible origin is 1 char after the \n. |
1187 | * (since *rx_origin == '\n', it's safe to ++ here rather than | |
1188 | * HOP(rx_origin, 1)) */ | |
1189 | rx_origin++; | |
d0880ea7 | 1190 | |
f4f115de | 1191 | if (prog->substrs->check_ix == 0 /* check is anchored */ |
4ada1233 | 1192 | || rx_origin >= HOP3c(check_at, - prog->check_offset_min, strpos)) |
f4f115de | 1193 | { |
d0880ea7 DM |
1194 | /* Position contradicts check-string; either because |
1195 | * check was anchored (and thus has no wiggle room), | |
4ada1233 | 1196 | * or check was float and rx_origin is above the float range */ |
d0880ea7 | 1197 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, |
675e93ee DM |
1198 | " Found /%s^%s/m, about to restart lookup for check-string with rx_origin %ld...\n", |
1199 | PL_colors[0], PL_colors[1], (long)(rx_origin - strbeg))); | |
d0880ea7 DM |
1200 | goto restart; |
1201 | } | |
1202 | ||
1203 | /* if we get here, the check substr must have been float, | |
2e759faa | 1204 | * is in range, and we may or may not have had an anchored |
d0880ea7 DM |
1205 | * "other" substr which still contradicts */ |
1206 | assert(prog->substrs->check_ix); /* check is float */ | |
1207 | ||
1208 | if (utf8_target ? prog->anchored_utf8 : prog->anchored_substr) { | |
1209 | /* whoops, the anchored "other" substr exists, so we still | |
1210 | * contradict. On the other hand, the float "check" substr | |
1211 | * didn't contradict, so just retry the anchored "other" | |
1212 | * substr */ | |
1213 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, | |
675e93ee | 1214 | " Found /%s^%s/m, rescanning for anchored from offset %ld (rx_origin now %"IVdf")...\n", |
d0880ea7 | 1215 | PL_colors[0], PL_colors[1], |
675e93ee DM |
1216 | (long)(rx_origin - strbeg + prog->anchored_offset), |
1217 | (long)(rx_origin - strbeg) | |
1218 | )); | |
d0880ea7 DM |
1219 | goto do_other_substr; |
1220 | } | |
1221 | ||
1222 | /* success: we don't contradict the found floating substring | |
1223 | * (and there's no anchored substr). */ | |
d0880ea7 | 1224 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, |
675e93ee DM |
1225 | " Found /%s^%s/m with rx_origin %ld...\n", |
1226 | PL_colors[0], PL_colors[1], (long)(rx_origin - strbeg))); | |
e3c6feb0 DM |
1227 | } |
1228 | else { | |
2e759faa | 1229 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, |
fe4f3442 | 1230 | " (multiline anchor test skipped)\n")); |
e3c6feb0 DM |
1231 | } |
1232 | ||
ffad1e6a | 1233 | success_at_start: |
e3c6feb0 | 1234 | |
cad2e5aa | 1235 | |
dd170ff5 DM |
1236 | /* if we have a starting character class, then test that extra constraint. |
1237 | * (trie stclasses are too expensive to use here, we are better off to | |
1238 | * leave it to regmatch itself) */ | |
1239 | ||
f8fc2ecf | 1240 | if (progi->regstclass && PL_regkind[OP(progi->regstclass)]!=TRIE) { |
f8fc2ecf | 1241 | const U8* const str = (U8*)STRING(progi->regstclass); |
0991020e | 1242 | |
2c75e362 | 1243 | /* XXX this value could be pre-computed */ |
f8fc2ecf | 1244 | const int cl_l = (PL_regkind[OP(progi->regstclass)] == EXACT |
2c75e362 DM |
1245 | ? (reginfo->is_utf8_pat |
1246 | ? utf8_distance(str + STR_LEN(progi->regstclass), str) | |
1247 | : STR_LEN(progi->regstclass)) | |
66e933ab | 1248 | : 1); |
1de06328 | 1249 | char * endpos; |
fa3bb21d | 1250 | char *s; |
000dfd2d DM |
1251 | /* latest pos that a matching float substr constrains rx start to */ |
1252 | char *rx_max_float = NULL; | |
1253 | ||
c75a3985 DM |
1254 | /* if the current rx_origin is anchored, either by satisfying an |
1255 | * anchored substring constraint, or a /^.../m constraint, then we | |
1256 | * can reject the current origin if the start class isn't found | |
1257 | * at the current position. If we have a float-only match, then | |
1258 | * rx_origin is constrained to a range; so look for the start class | |
1259 | * in that range. if neither, then look for the start class in the | |
1260 | * whole rest of the string */ | |
1261 | ||
dd170ff5 DM |
1262 | /* XXX DAPM it's not clear what the minlen test is for, and why |
1263 | * it's not used in the floating case. Nothing in the test suite | |
1264 | * causes minlen == 0 here. See <20140313134639.GS12844@iabyn.com>. | |
1265 | * Here are some old comments, which may or may not be correct: | |
1266 | * | |
1267 | * minlen == 0 is possible if regstclass is \b or \B, | |
1268 | * and the fixed substr is ''$. | |
1269 | * Since minlen is already taken into account, rx_origin+1 is | |
1270 | * before strend; accidentally, minlen >= 1 guaranties no false | |
1271 | * positives at rx_origin + 1 even for \b or \B. But (minlen? 1 : | |
1272 | * 0) below assumes that regstclass does not come from lookahead... | |
1273 | * If regstclass takes bytelength more than 1: If charlength==1, OK. | |
1274 | * This leaves EXACTF-ish only, which are dealt with in | |
1275 | * find_byclass(). | |
1276 | */ | |
1277 | ||
7d2d37f5 | 1278 | if (prog->anchored_substr || prog->anchored_utf8 || ml_anch) |
fa3bb21d | 1279 | endpos= HOP3c(rx_origin, (prog->minlen ? cl_l : 0), strend); |
000dfd2d DM |
1280 | else if (prog->float_substr || prog->float_utf8) { |
1281 | rx_max_float = HOP3c(check_at, -start_shift, strbeg); | |
1282 | endpos= HOP3c(rx_max_float, cl_l, strend); | |
1283 | } | |
1de06328 YO |
1284 | else |
1285 | endpos= strend; | |
1286 | ||
1dc475d0 DM |
1287 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, |
1288 | " looking for class: start_shift: %"IVdf" check_at: %"IVdf | |
c43b5520 | 1289 | " rx_origin: %"IVdf" endpos: %"IVdf"\n", |
1dc475d0 | 1290 | (IV)start_shift, (IV)(check_at - strbeg), |
c43b5520 | 1291 | (IV)(rx_origin - strbeg), (IV)(endpos - strbeg))); |
d8080198 | 1292 | |
c43b5520 | 1293 | s = find_byclass(prog, progi->regstclass, rx_origin, endpos, |
f9176b44 | 1294 | reginfo); |
be778b1a | 1295 | if (!s) { |
6eb5f6b9 | 1296 | if (endpos == strend) { |
a3621e74 | 1297 | DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log, |
1dc475d0 | 1298 | " Could not match STCLASS...\n") ); |
6eb5f6b9 JH |
1299 | goto fail; |
1300 | } | |
a3621e74 | 1301 | DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log, |
1dc475d0 | 1302 | " This position contradicts STCLASS...\n") ); |
e0eb31e7 DM |
1303 | if ((prog->intflags & PREGf_ANCH) && !ml_anch |
1304 | && !(prog->intflags & PREGf_IMPLICIT)) | |
653099ff | 1305 | goto fail; |
9fed8d02 | 1306 | |
6eb5f6b9 | 1307 | /* Contradict one of substrings */ |
97136c8a DM |
1308 | if (prog->anchored_substr || prog->anchored_utf8) { |
1309 | if (prog->substrs->check_ix == 1) { /* check is float */ | |
1310 | /* Have both, check_string is floating */ | |
1311 | assert(rx_origin + start_shift <= check_at); | |
1312 | if (rx_origin + start_shift != check_at) { | |
1313 | /* not at latest position float substr could match: | |
c75a3985 DM |
1314 | * Recheck anchored substring, but not floating. |
1315 | * The condition above is in bytes rather than | |
1316 | * chars for efficiency. It's conservative, in | |
1317 | * that it errs on the side of doing 'goto | |
88203927 DM |
1318 | * do_other_substr'. In this case, at worst, |
1319 | * an extra anchored search may get done, but in | |
1320 | * practice the extra fbm_instr() is likely to | |
1321 | * get skipped anyway. */ | |
97136c8a | 1322 | DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log, |
675e93ee DM |
1323 | " about to retry anchored at offset %ld (rx_origin now %"IVdf")...\n", |
1324 | (long)(other_last - strbeg), | |
1325 | (IV)(rx_origin - strbeg) | |
1326 | )); | |
97136c8a | 1327 | goto do_other_substr; |
3369914b | 1328 | } |
3369914b DM |
1329 | } |
1330 | } | |
97136c8a | 1331 | else { |
9fed8d02 DM |
1332 | /* float-only */ |
1333 | ||
7d2d37f5 | 1334 | if (ml_anch) { |
c75a3985 DM |
1335 | /* In the presence of ml_anch, we might be able to |
1336 | * find another \n without breaking the current float | |
1337 | * constraint. */ | |
1338 | ||
1339 | /* strictly speaking this should be HOP3c(..., 1, ...), | |
1340 | * but since we goto a block of code that's going to | |
1341 | * search for the next \n if any, its safe here */ | |
9fed8d02 | 1342 | rx_origin++; |
9fed8d02 | 1343 | DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log, |
675e93ee | 1344 | " about to look for /%s^%s/m starting at rx_origin %ld...\n", |
9fed8d02 | 1345 | PL_colors[0], PL_colors[1], |
675e93ee | 1346 | (long)(rx_origin - strbeg)) ); |
9fed8d02 | 1347 | goto postprocess_substr_matches; |
ab60c45a | 1348 | } |
c75a3985 DM |
1349 | |
1350 | /* strictly speaking this can never be true; but might | |
1351 | * be if we ever allow intuit without substrings */ | |
1352 | if (!(utf8_target ? prog->float_utf8 : prog->float_substr)) | |
9fed8d02 | 1353 | goto fail; |
c75a3985 | 1354 | |
000dfd2d | 1355 | rx_origin = rx_max_float; |
9fed8d02 DM |
1356 | } |
1357 | ||
c75a3985 DM |
1358 | /* at this point, any matching substrings have been |
1359 | * contradicted. Start again... */ | |
1360 | ||
9fed8d02 | 1361 | rx_origin = HOP3c(rx_origin, 1, strend); |
557f47af DM |
1362 | |
1363 | /* uses bytes rather than char calculations for efficiency. | |
1364 | * It's conservative: it errs on the side of doing 'goto restart', | |
1365 | * where there is code that does a proper char-based test */ | |
9fed8d02 | 1366 | if (rx_origin + start_shift + end_shift > strend) { |
40268e5b | 1367 | DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log, |
9fed8d02 DM |
1368 | " Could not match STCLASS...\n") ); |
1369 | goto fail; | |
1370 | } | |
1371 | DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log, | |
675e93ee | 1372 | " about to look for %s substr starting at offset %ld (rx_origin now %"IVdf")...\n", |
9fed8d02 | 1373 | (prog->substrs->check_ix ? "floating" : "anchored"), |
675e93ee DM |
1374 | (long)(rx_origin + start_shift - strbeg), |
1375 | (IV)(rx_origin - strbeg) | |
1376 | )); | |
9fed8d02 | 1377 | goto restart; |
6eb5f6b9 | 1378 | } |
9fed8d02 | 1379 | |
c75a3985 DM |
1380 | /* Success !!! */ |
1381 | ||
5f9c6575 | 1382 | if (rx_origin != s) { |
a3621e74 | 1383 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, |
1dc475d0 | 1384 | " By STCLASS: moving %ld --> %ld\n", |
675e93ee | 1385 | (long)(rx_origin - strbeg), (long)(s - strbeg)) |
b7953727 JH |
1386 | ); |
1387 | } | |
1388 | else { | |
a3621e74 | 1389 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, |
1dc475d0 | 1390 | " Does not contradict STCLASS...\n"); |
b7953727 JH |
1391 | ); |
1392 | } | |
6eb5f6b9 | 1393 | } |
ffad1e6a DM |
1394 | |
1395 | /* Decide whether using the substrings helped */ | |
1396 | ||
1397 | if (rx_origin != strpos) { | |
1398 | /* Fixed substring is found far enough so that the match | |
1399 | cannot start at strpos. */ | |
1400 | ||
1401 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, " try at offset...\n")); | |
1402 | ++BmUSEFUL(utf8_target ? prog->check_utf8 : prog->check_substr); /* hooray/5 */ | |
1403 | } | |
1404 | else { | |
70563e16 DM |
1405 | /* The found rx_origin position does not prohibit matching at |
1406 | * strpos, so calling intuit didn't gain us anything. Decrement | |
1407 | * the BmUSEFUL() count on the check substring, and if we reach | |
1408 | * zero, free it. */ | |
1409 | if (!(prog->intflags & PREGf_NAUGHTY) | |
ffad1e6a DM |
1410 | && (utf8_target ? ( |
1411 | prog->check_utf8 /* Could be deleted already */ | |
1412 | && --BmUSEFUL(prog->check_utf8) < 0 | |
1413 | && (prog->check_utf8 == prog->float_utf8) | |
1414 | ) : ( | |
1415 | prog->check_substr /* Could be deleted already */ | |
1416 | && --BmUSEFUL(prog->check_substr) < 0 | |
1417 | && (prog->check_substr == prog->float_substr) | |
1418 | ))) | |
1419 | { | |
1420 | /* If flags & SOMETHING - do not do it many times on the same match */ | |
1421 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, " ... Disabling check substring...\n")); | |
1422 | /* XXX Does the destruction order has to change with utf8_target? */ | |
1423 | SvREFCNT_dec(utf8_target ? prog->check_utf8 : prog->check_substr); | |
1424 | SvREFCNT_dec(utf8_target ? prog->check_substr : prog->check_utf8); | |
1425 | prog->check_substr = prog->check_utf8 = NULL; /* disable */ | |
1426 | prog->float_substr = prog->float_utf8 = NULL; /* clear */ | |
1427 | check = NULL; /* abort */ | |
ffad1e6a DM |
1428 | /* XXXX This is a remnant of the old implementation. It |
1429 | looks wasteful, since now INTUIT can use many | |
1430 | other heuristics. */ | |
1431 | prog->extflags &= ~RXf_USE_INTUIT; | |
ffad1e6a DM |
1432 | } |
1433 | } | |
1434 | ||
1435 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, | |
1436 | "Intuit: %sSuccessfully guessed:%s match at offset %ld\n", | |
675e93ee | 1437 | PL_colors[4], PL_colors[5], (long)(rx_origin - strbeg)) ); |
ffad1e6a | 1438 | |
c765d6e0 | 1439 | return rx_origin; |
2c2d71f5 JH |
1440 | |
1441 | fail_finish: /* Substring not found */ | |
33b8afdf | 1442 | if (prog->check_substr || prog->check_utf8) /* could be removed already */ |
f2ed9b32 | 1443 | BmUSEFUL(utf8_target ? prog->check_utf8 : prog->check_substr) += 5; /* hooray */ |
cad2e5aa | 1444 | fail: |
a3621e74 | 1445 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch rejected by optimizer%s\n", |
e4584336 | 1446 | PL_colors[4], PL_colors[5])); |
bd61b366 | 1447 | return NULL; |
cad2e5aa | 1448 | } |
9661b544 | 1449 | |
70563e16 | 1450 | |
a0a388a1 | 1451 | #define DECL_TRIE_TYPE(scan) \ |
e7fd4aa1 | 1452 | const enum { trie_plain, trie_utf8, trie_utf8_fold, trie_latin_utf8_fold, \ |
a4525e78 KW |
1453 | trie_utf8_exactfa_fold, trie_latin_utf8_exactfa_fold, \ |
1454 | trie_utf8l, trie_flu8 } \ | |
e7fd4aa1 KW |
1455 | trie_type = ((scan->flags == EXACT) \ |
1456 | ? (utf8_target ? trie_utf8 : trie_plain) \ | |
a4525e78 KW |
1457 | : (scan->flags == EXACTL) \ |
1458 | ? (utf8_target ? trie_utf8l : trie_plain) \ | |
1459 | : (scan->flags == EXACTFA) \ | |
1460 | ? (utf8_target \ | |
1461 | ? trie_utf8_exactfa_fold \ | |
1462 | : trie_latin_utf8_exactfa_fold) \ | |
1463 | : (scan->flags == EXACTFLU8 \ | |
1464 | ? trie_flu8 \ | |
1465 | : (utf8_target \ | |
1466 | ? trie_utf8_fold \ | |
1467 | : trie_latin_utf8_fold))) | |
fab2782b | 1468 | |
fd3249ee | 1469 | #define REXEC_TRIE_READ_CHAR(trie_type, trie, widecharmap, uc, uscan, len, uvc, charid, foldlen, foldbuf, uniflags) \ |
baa60164 | 1470 | STMT_START { \ |
fab2782b | 1471 | STRLEN skiplen; \ |
baa60164 | 1472 | U8 flags = FOLD_FLAGS_FULL; \ |
fab2782b | 1473 | switch (trie_type) { \ |
a4525e78 | 1474 | case trie_flu8: \ |
780fcc9f | 1475 | _CHECK_AND_WARN_PROBLEMATIC_LOCALE; \ |
613abc6d KW |
1476 | if (utf8_target && UTF8_IS_ABOVE_LATIN1(*uc)) { \ |
1477 | _CHECK_AND_OUTPUT_WIDE_LOCALE_UTF8_MSG(uc, uc + UTF8SKIP(uc)); \ | |
1478 | } \ | |
a4525e78 | 1479 | goto do_trie_utf8_fold; \ |
31f05a37 | 1480 | case trie_utf8_exactfa_fold: \ |
baa60164 | 1481 | flags |= FOLD_FLAGS_NOMIX_ASCII; \ |
8e57b935 | 1482 | /* FALLTHROUGH */ \ |
fab2782b | 1483 | case trie_utf8_fold: \ |
a4525e78 | 1484 | do_trie_utf8_fold: \ |
fab2782b | 1485 | if ( foldlen>0 ) { \ |
c80e42f3 | 1486 | uvc = utf8n_to_uvchr( (const U8*) uscan, UTF8_MAXLEN, &len, uniflags ); \ |
fab2782b YO |
1487 | foldlen -= len; \ |
1488 | uscan += len; \ | |
1489 | len=0; \ | |
1490 | } else { \ | |
445bf929 | 1491 | uvc = _to_utf8_fold_flags( (const U8*) uc, foldbuf, &foldlen, flags); \ |
fab2782b YO |
1492 | len = UTF8SKIP(uc); \ |
1493 | skiplen = UNISKIP( uvc ); \ | |
1494 | foldlen -= skiplen; \ | |
1495 | uscan = foldbuf + skiplen; \ | |
1496 | } \ | |
1497 | break; \ | |
baa60164 KW |
1498 | case trie_latin_utf8_exactfa_fold: \ |
1499 | flags |= FOLD_FLAGS_NOMIX_ASCII; \ | |
8e57b935 | 1500 | /* FALLTHROUGH */ \ |
fab2782b YO |
1501 | case trie_latin_utf8_fold: \ |
1502 | if ( foldlen>0 ) { \ | |
c80e42f3 | 1503 | uvc = utf8n_to_uvchr( (const U8*) uscan, UTF8_MAXLEN, &len, uniflags ); \ |
fab2782b YO |
1504 | foldlen -= len; \ |
1505 | uscan += len; \ | |
1506 | len=0; \ | |
1507 | } else { \ | |
1508 | len = 1; \ | |
31f05a37 | 1509 | uvc = _to_fold_latin1( (U8) *uc, foldbuf, &foldlen, flags); \ |
fab2782b YO |
1510 | skiplen = UNISKIP( uvc ); \ |
1511 | foldlen -= skiplen; \ | |
1512 | uscan = foldbuf + skiplen; \ | |
1513 | } \ | |
1514 | break; \ | |
a4525e78 | 1515 | case trie_utf8l: \ |
780fcc9f | 1516 | _CHECK_AND_WARN_PROBLEMATIC_LOCALE; \ |
613abc6d KW |
1517 | if (utf8_target && UTF8_IS_ABOVE_LATIN1(*uc)) { \ |
1518 | _CHECK_AND_OUTPUT_WIDE_LOCALE_UTF8_MSG(uc, uc + UTF8SKIP(uc)); \ | |
1519 | } \ | |
780fcc9f | 1520 | /* FALLTHROUGH */ \ |
fab2782b | 1521 | case trie_utf8: \ |
c80e42f3 | 1522 | uvc = utf8n_to_uvchr( (const U8*) uc, UTF8_MAXLEN, &len, uniflags ); \ |
fab2782b YO |
1523 | break; \ |
1524 | case trie_plain: \ | |
1525 | uvc = (UV)*uc; \ | |
1526 | len = 1; \ | |
1527 | } \ | |
1528 | if (uvc < 256) { \ | |
1529 | charid = trie->charmap[ uvc ]; \ | |
1530 | } \ | |
1531 | else { \ | |
1532 | charid = 0; \ | |
1533 | if (widecharmap) { \ | |
1534 | SV** const svpp = hv_fetch(widecharmap, \ | |
1535 | (char*)&uvc, sizeof(UV), 0); \ | |
1536 | if (svpp) \ | |
1537 | charid = (U16)SvIV(*svpp); \ | |
1538 | } \ | |
1539 | } \ | |
4cadc6a9 YO |
1540 | } STMT_END |
1541 | ||
ae7c5b9b KW |
1542 | #define DUMP_EXEC_POS(li,s,doutf8) \ |
1543 | dump_exec_pos(li,s,(reginfo->strend),(reginfo->strbeg), \ | |
1544 | startpos, doutf8) | |
1545 | ||
c84a03c5 | 1546 | #define REXEC_FBC_EXACTISH_SCAN(COND) \ |
4cadc6a9 YO |
1547 | STMT_START { \ |
1548 | while (s <= e) { \ | |
c84a03c5 | 1549 | if ( (COND) \ |
fac1af77 | 1550 | && (ln == 1 || folder(s, pat_string, ln)) \ |
02d5137b | 1551 | && (reginfo->intuit || regtry(reginfo, &s)) )\ |
4cadc6a9 YO |
1552 | goto got_it; \ |
1553 | s++; \ | |
1554 | } \ | |
1555 | } STMT_END | |
1556 | ||
c84a03c5 | 1557 | #define REXEC_FBC_UTF8_SCAN(CODE) \ |
4cadc6a9 | 1558 | STMT_START { \ |
9a902117 | 1559 | while (s < strend) { \ |
c84a03c5 | 1560 | CODE \ |
9a902117 | 1561 | s += UTF8SKIP(s); \ |
4cadc6a9 YO |
1562 | } \ |
1563 | } STMT_END | |
1564 | ||
c84a03c5 | 1565 | #define REXEC_FBC_SCAN(CODE) \ |
4cadc6a9 YO |
1566 | STMT_START { \ |
1567 | while (s < strend) { \ | |
c84a03c5 | 1568 | CODE \ |
4cadc6a9 YO |
1569 | s++; \ |
1570 | } \ | |
1571 | } STMT_END | |
1572 | ||
05bd126c KW |
1573 | #define REXEC_FBC_UTF8_CLASS_SCAN(COND) \ |
1574 | REXEC_FBC_UTF8_SCAN( /* Loops while (s < strend) */ \ | |
1575 | if (COND) { \ | |
1576 | if (tmp && (reginfo->intuit || regtry(reginfo, &s))) \ | |
1577 | goto got_it; \ | |
1578 | else \ | |
1579 | tmp = doevery; \ | |
1580 | } \ | |
1581 | else \ | |
1582 | tmp = 1; \ | |
4cadc6a9 YO |
1583 | ) |
1584 | ||
05bd126c KW |
1585 | #define REXEC_FBC_CLASS_SCAN(COND) \ |
1586 | REXEC_FBC_SCAN( /* Loops while (s < strend) */ \ | |
1587 | if (COND) { \ | |
1588 | if (tmp && (reginfo->intuit || regtry(reginfo, &s))) \ | |
1589 | goto got_it; \ | |
1590 | else \ | |
1591 | tmp = doevery; \ | |
1592 | } \ | |
1593 | else \ | |
1594 | tmp = 1; \ | |
4cadc6a9 YO |
1595 | ) |
1596 | ||
c84a03c5 | 1597 | #define REXEC_FBC_CSCAN(CONDUTF8,COND) \ |
baa60164 | 1598 | if (utf8_target) { \ |
c84a03c5 | 1599 | REXEC_FBC_UTF8_CLASS_SCAN(CONDUTF8); \ |
e1d1eefb YO |
1600 | } \ |
1601 | else { \ | |
c84a03c5 | 1602 | REXEC_FBC_CLASS_SCAN(COND); \ |
d981ef24 | 1603 | } |
05bd126c | 1604 | |
05bd126c KW |
1605 | /* The three macros below are slightly different versions of the same logic. |
1606 | * | |
1607 | * The first is for /a and /aa when the target string is UTF-8. This can only | |
1608 | * match ascii, but it must advance based on UTF-8. The other two handle the | |
1609 | * non-UTF-8 and the more generic UTF-8 cases. In all three, we are looking | |
1610 | * for the boundary (or non-boundary) between a word and non-word character. | |
1611 | * The utf8 and non-utf8 cases have the same logic, but the details must be | |
1612 | * different. Find the "wordness" of the character just prior to this one, and | |
1613 | * compare it with the wordness of this one. If they differ, we have a | |
1614 | * boundary. At the beginning of the string, pretend that the previous | |
1615 | * character was a new-line. | |
1616 | * | |
1617 | * All these macros uncleanly have side-effects with each other and outside | |
1618 | * variables. So far it's been too much trouble to clean-up | |
1619 | * | |
1620 | * TEST_NON_UTF8 is the macro or function to call to test if its byte input is | |
1621 | * a word character or not. | |
1622 | * IF_SUCCESS is code to do if it finds that we are at a boundary between | |
1623 | * word/non-word | |
1624 | * IF_FAIL is code to do if we aren't at a boundary between word/non-word | |
1625 | * | |
1626 | * Exactly one of the two IF_FOO parameters is a no-op, depending on whether we | |
1627 | * are looking for a boundary or for a non-boundary. If we are looking for a | |
1628 | * boundary, we want IF_FAIL to be the no-op, and for IF_SUCCESS to go out and | |
1629 | * see if this tentative match actually works, and if so, to quit the loop | |
1630 | * here. And vice-versa if we are looking for a non-boundary. | |
1631 | * | |
1632 | * 'tmp' below in the next three macros in the REXEC_FBC_SCAN and | |
1633 | * REXEC_FBC_UTF8_SCAN loops is a loop invariant, a bool giving the return of | |
1634 | * TEST_NON_UTF8(s-1). To see this, note that that's what it is defined to be | |
1635 | * at entry to the loop, and to get to the IF_FAIL branch, tmp must equal | |
1636 | * TEST_NON_UTF8(s), and in the opposite branch, IF_SUCCESS, tmp is that | |
1637 | * complement. But in that branch we complement tmp, meaning that at the | |
1638 | * bottom of the loop tmp is always going to be equal to TEST_NON_UTF8(s), | |
1639 | * which means at the top of the loop in the next iteration, it is | |
1640 | * TEST_NON_UTF8(s-1) */ | |
b2f4e957 | 1641 | #define FBC_UTF8_A(TEST_NON_UTF8, IF_SUCCESS, IF_FAIL) \ |
05bd126c KW |
1642 | tmp = (s != reginfo->strbeg) ? UCHARAT(s - 1) : '\n'; \ |
1643 | tmp = TEST_NON_UTF8(tmp); \ | |
1644 | REXEC_FBC_UTF8_SCAN( /* advances s while s < strend */ \ | |
1645 | if (tmp == ! TEST_NON_UTF8((U8) *s)) { \ | |
1646 | tmp = !tmp; \ | |
1647 | IF_SUCCESS; /* Is a boundary if values for s-1 and s differ */ \ | |
1648 | } \ | |
1649 | else { \ | |
1650 | IF_FAIL; \ | |
1651 | } \ | |
1652 | ); \ | |
1653 | ||
1654 | /* Like FBC_UTF8_A, but TEST_UV is a macro which takes a UV as its input, and | |
1655 | * TEST_UTF8 is a macro that for the same input code points returns identically | |
1656 | * to TEST_UV, but takes a pointer to a UTF-8 encoded string instead */ | |
236d82fd | 1657 | #define FBC_UTF8(TEST_UV, TEST_UTF8, IF_SUCCESS, IF_FAIL) \ |
05bd126c KW |
1658 | if (s == reginfo->strbeg) { \ |
1659 | tmp = '\n'; \ | |
1660 | } \ | |
1661 | else { /* Back-up to the start of the previous character */ \ | |
1662 | U8 * const r = reghop3((U8*)s, -1, (U8*)reginfo->strbeg); \ | |
1663 | tmp = utf8n_to_uvchr(r, (U8*) reginfo->strend - r, \ | |
3db24e1e | 1664 | 0, UTF8_ALLOW_DEFAULT); \ |
05bd126c KW |
1665 | } \ |
1666 | tmp = TEST_UV(tmp); \ | |
1667 | LOAD_UTF8_CHARCLASS_ALNUM(); \ | |
1668 | REXEC_FBC_UTF8_SCAN( /* advances s while s < strend */ \ | |
1669 | if (tmp == ! (TEST_UTF8((U8 *) s))) { \ | |
1670 | tmp = !tmp; \ | |
1671 | IF_SUCCESS; \ | |
1672 | } \ | |
1673 | else { \ | |
1674 | IF_FAIL; \ | |
1675 | } \ | |
1676 | ); | |
cfaf538b | 1677 | |
05bd126c KW |
1678 | /* Like the above two macros. UTF8_CODE is the complete code for handling |
1679 | * UTF-8. Common to the BOUND and NBOUND cases, set-up by the FBC_BOUND, etc | |
1680 | * macros below */ | |
baa60164 | 1681 | #define FBC_BOUND_COMMON(UTF8_CODE, TEST_NON_UTF8, IF_SUCCESS, IF_FAIL) \ |
63ac0dad | 1682 | if (utf8_target) { \ |
05bd126c | 1683 | UTF8_CODE \ |
63ac0dad KW |
1684 | } \ |
1685 | else { /* Not utf8 */ \ | |
9d9163fb | 1686 | tmp = (s != reginfo->strbeg) ? UCHARAT(s - 1) : '\n'; \ |
63ac0dad | 1687 | tmp = TEST_NON_UTF8(tmp); \ |
05bd126c | 1688 | REXEC_FBC_SCAN( /* advances s while s < strend */ \ |
63ac0dad | 1689 | if (tmp == ! TEST_NON_UTF8((U8) *s)) { \ |
63ac0dad | 1690 | IF_SUCCESS; \ |
760cfa8e | 1691 | tmp = !tmp; \ |
63ac0dad KW |
1692 | } \ |
1693 | else { \ | |
1694 | IF_FAIL; \ | |
1695 | } \ | |
1696 | ); \ | |
1697 | } \ | |
c8519dc7 KW |
1698 | /* Here, things have been set up by the previous code so that tmp is the \ |
1699 | * return of TEST_NON_UTF(s-1) or TEST_UTF8(s-1) (depending on the \ | |
1700 | * utf8ness of the target). We also have to check if this matches against \ | |
1701 | * the EOS, which we treat as a \n (which is the same value in both UTF-8 \ | |
1702 | * or non-UTF8, so can use the non-utf8 test condition even for a UTF-8 \ | |
1703 | * string */ \ | |
1704 | if (tmp == ! TEST_NON_UTF8('\n')) { \ | |
1705 | IF_SUCCESS; \ | |
1706 | } \ | |
1707 | else { \ | |
1708 | IF_FAIL; \ | |
1709 | } | |
63ac0dad | 1710 | |
ae7c5b9b KW |
1711 | /* This is the macro to use when we want to see if something that looks like it |
1712 | * could match, actually does, and if so exits the loop */ | |
1713 | #define REXEC_FBC_TRYIT \ | |
1714 | if ((reginfo->intuit || regtry(reginfo, &s))) \ | |
1715 | goto got_it | |
1716 | ||
1717 | /* The only difference between the BOUND and NBOUND cases is that | |
1718 | * REXEC_FBC_TRYIT is called when matched in BOUND, and when non-matched in | |
1719 | * NBOUND. This is accomplished by passing it as either the if or else clause, | |
1720 | * with the other one being empty (PLACEHOLDER is defined as empty). | |
1721 | * | |
1722 | * The TEST_FOO parameters are for operating on different forms of input, but | |
1723 | * all should be ones that return identically for the same underlying code | |
1724 | * points */ | |
1725 | #define FBC_BOUND(TEST_NON_UTF8, TEST_UV, TEST_UTF8) \ | |
1726 | FBC_BOUND_COMMON( \ | |
1727 | FBC_UTF8(TEST_UV, TEST_UTF8, REXEC_FBC_TRYIT, PLACEHOLDER), \ | |
1728 | TEST_NON_UTF8, REXEC_FBC_TRYIT, PLACEHOLDER) | |
1729 | ||
44129e46 | 1730 | #define FBC_BOUND_A(TEST_NON_UTF8) \ |
ae7c5b9b KW |
1731 | FBC_BOUND_COMMON( \ |
1732 | FBC_UTF8_A(TEST_NON_UTF8, REXEC_FBC_TRYIT, PLACEHOLDER), \ | |
1733 | TEST_NON_UTF8, REXEC_FBC_TRYIT, PLACEHOLDER) | |
1734 | ||
1735 | #define FBC_NBOUND(TEST_NON_UTF8, TEST_UV, TEST_UTF8) \ | |
1736 | FBC_BOUND_COMMON( \ | |
1737 | FBC_UTF8(TEST_UV, TEST_UTF8, PLACEHOLDER, REXEC_FBC_TRYIT), \ | |
1738 | TEST_NON_UTF8, PLACEHOLDER, REXEC_FBC_TRYIT) | |
1739 | ||
44129e46 | 1740 | #define FBC_NBOUND_A(TEST_NON_UTF8) \ |
ae7c5b9b KW |
1741 | FBC_BOUND_COMMON( \ |
1742 | FBC_UTF8_A(TEST_NON_UTF8, PLACEHOLDER, REXEC_FBC_TRYIT), \ | |
1743 | TEST_NON_UTF8, PLACEHOLDER, REXEC_FBC_TRYIT) | |
1744 | ||
64935bc6 KW |
1745 | /* Takes a pointer to an inversion list, a pointer to its corresponding |
1746 | * inversion map, and a code point, and returns the code point's value | |
1747 | * according to the two arrays. It assumes that all code points have a value. | |
1748 | * This is used as the base macro for macros for particular properties */ | |
1749 | #define _generic_GET_BREAK_VAL_CP(invlist, invmap, cp) \ | |
1750 | invmap[_invlist_search(invlist, cp)] | |
1751 | ||
1752 | /* Same as above, but takes begin, end ptrs to a UTF-8 encoded string instead | |
1753 | * of a code point, returning the value for the first code point in the string. | |
1754 | * And it takes the particular macro name that finds the desired value given a | |
1755 | * code point. Merely convert the UTF-8 to code point and call the cp macro */ | |
1756 | #define _generic_GET_BREAK_VAL_UTF8(cp_macro, pos, strend) \ | |
1757 | (__ASSERT_(pos < strend) \ | |
1758 | /* Note assumes is valid UTF-8 */ \ | |
1759 | (cp_macro(utf8_to_uvchr_buf((pos), (strend), NULL)))) | |
1760 | ||
1761 | /* Returns the GCB value for the input code point */ | |
1762 | #define getGCB_VAL_CP(cp) \ | |
1763 | _generic_GET_BREAK_VAL_CP( \ | |
1764 | PL_GCB_invlist, \ | |
1765 | Grapheme_Cluster_Break_invmap, \ | |
1766 | (cp)) | |
1767 | ||
1768 | /* Returns the GCB value for the first code point in the UTF-8 encoded string | |
1769 | * bounded by pos and strend */ | |
1770 | #define getGCB_VAL_UTF8(pos, strend) \ | |
1771 | _generic_GET_BREAK_VAL_UTF8(getGCB_VAL_CP, pos, strend) | |
05bd126c | 1772 | |
06ae2722 KW |
1773 | |
1774 | /* Returns the SB value for the input code point */ | |
1775 | #define getSB_VAL_CP(cp) \ | |
1776 | _generic_GET_BREAK_VAL_CP( \ | |
1777 | PL_SB_invlist, \ | |
1778 | Sentence_Break_invmap, \ | |
1779 | (cp)) | |
1780 | ||
1781 | /* Returns the SB value for the first code point in the UTF-8 encoded string | |
1782 | * bounded by pos and strend */ | |
1783 | #define getSB_VAL_UTF8(pos, strend) \ | |
1784 | _generic_GET_BREAK_VAL_UTF8(getSB_VAL_CP, pos, strend) | |
1785 | ||
ae3bb8ea KW |
1786 | /* Returns the WB value for the input code point */ |
1787 | #define getWB_VAL_CP(cp) \ | |
1788 | _generic_GET_BREAK_VAL_CP( \ | |
1789 | PL_WB_invlist, \ | |
1790 | Word_Break_invmap, \ | |
1791 | (cp)) | |
1792 | ||
1793 | /* Returns the WB value for the first code point in the UTF-8 encoded string | |
1794 | * bounded by pos and strend */ | |
1795 | #define getWB_VAL_UTF8(pos, strend) \ | |
1796 | _generic_GET_BREAK_VAL_UTF8(getWB_VAL_CP, pos, strend) | |
1797 | ||
786e8c11 | 1798 | /* We know what class REx starts with. Try to find this position... */ |
02d5137b | 1799 | /* if reginfo->intuit, its a dryrun */ |
786e8c11 YO |
1800 | /* annoyingly all the vars in this routine have different names from their counterparts |
1801 | in regmatch. /grrr */ | |
3c3eec57 | 1802 | STATIC char * |
07be1b83 | 1803 | S_find_byclass(pTHX_ regexp * prog, const regnode *c, char *s, |
f9176b44 | 1804 | const char *strend, regmatch_info *reginfo) |
a687059c | 1805 | { |
73104a1b KW |
1806 | dVAR; |
1807 | const I32 doevery = (prog->intflags & PREGf_SKIP) == 0; | |
1808 | char *pat_string; /* The pattern's exactish string */ | |
1809 | char *pat_end; /* ptr to end char of pat_string */ | |
1810 | re_fold_t folder; /* Function for computing non-utf8 folds */ | |
1811 | const U8 *fold_array; /* array for folding ords < 256 */ | |
1812 | STRLEN ln; | |
1813 | STRLEN lnc; | |
73104a1b KW |
1814 | U8 c1; |
1815 | U8 c2; | |
1816 | char *e; | |
1817 | I32 tmp = 1; /* Scratch variable? */ | |
ba44c216 | 1818 | const bool utf8_target = reginfo->is_utf8_target; |
73104a1b | 1819 | UV utf8_fold_flags = 0; |
f9176b44 | 1820 | const bool is_utf8_pat = reginfo->is_utf8_pat; |
3018b823 KW |
1821 | bool to_complement = FALSE; /* Invert the result? Taking the xor of this |
1822 | with a result inverts that result, as 0^1 = | |
1823 | 1 and 1^1 = 0 */ | |
1824 | _char_class_number classnum; | |
1825 | ||
73104a1b | 1826 | RXi_GET_DECL(prog,progi); |
2f7f8cb1 | 1827 | |
73104a1b | 1828 | PERL_ARGS_ASSERT_FIND_BYCLASS; |
2f7f8cb1 | 1829 | |
73104a1b KW |
1830 | /* We know what class it must start with. */ |
1831 | switch (OP(c)) { | |
a4525e78 | 1832 | case ANYOFL: |
780fcc9f KW |
1833 | _CHECK_AND_WARN_PROBLEMATIC_LOCALE; |
1834 | /* FALLTHROUGH */ | |
73104a1b KW |
1835 | case ANYOF: |
1836 | if (utf8_target) { | |
1837 | REXEC_FBC_UTF8_CLASS_SCAN( | |
3db24e1e | 1838 | reginclass(prog, c, (U8*)s, (U8*) strend, utf8_target)); |
73104a1b KW |
1839 | } |
1840 | else { | |
1841 | REXEC_FBC_CLASS_SCAN(REGINCLASS(prog, c, (U8*)s)); | |
1842 | } | |
1843 | break; | |
1844 | case CANY: | |
1845 | REXEC_FBC_SCAN( | |
02d5137b | 1846 | if (tmp && (reginfo->intuit || regtry(reginfo, &s))) |
73104a1b KW |
1847 | goto got_it; |
1848 | else | |
1849 | tmp = doevery; | |
1850 | ); | |
1851 | break; | |
1852 | ||
098b07d5 KW |
1853 | case EXACTFA_NO_TRIE: /* This node only generated for non-utf8 patterns */ |
1854 | assert(! is_utf8_pat); | |
924ba076 | 1855 | /* FALLTHROUGH */ |
73104a1b | 1856 | case EXACTFA: |
984e6dd1 | 1857 | if (is_utf8_pat || utf8_target) { |
73104a1b KW |
1858 | utf8_fold_flags = FOLDEQ_UTF8_NOMIX_ASCII; |
1859 | goto do_exactf_utf8; | |
1860 | } | |
1861 | fold_array = PL_fold_latin1; /* Latin1 folds are not affected by */ | |
1862 | folder = foldEQ_latin1; /* /a, except the sharp s one which */ | |
1863 | goto do_exactf_non_utf8; /* isn't dealt with by these */ | |
77a6d856 | 1864 | |
2fdb7295 KW |
1865 | case EXACTF: /* This node only generated for non-utf8 patterns */ |
1866 | assert(! is_utf8_pat); | |
73104a1b | 1867 | if (utf8_target) { |
73104a1b KW |
1868 | utf8_fold_flags = 0; |
1869 | goto do_exactf_utf8; | |
1870 | } | |
1871 | fold_array = PL_fold; | |
1872 | folder = foldEQ; | |
1873 | goto do_exactf_non_utf8; | |
1874 | ||
1875 | case EXACTFL: | |
780fcc9f | 1876 | _CHECK_AND_WARN_PROBLEMATIC_LOCALE; |
31f05a37 | 1877 | if (is_utf8_pat || utf8_target || IN_UTF8_CTYPE_LOCALE) { |
cea315b6 | 1878 | utf8_fold_flags = FOLDEQ_LOCALE; |
73104a1b KW |
1879 | goto do_exactf_utf8; |
1880 | } | |
1881 | fold_array = PL_fold_locale; | |
1882 | folder = foldEQ_locale; | |
1883 | goto do_exactf_non_utf8; | |
3c760661 | 1884 | |
73104a1b | 1885 | case EXACTFU_SS: |
984e6dd1 | 1886 | if (is_utf8_pat) { |
73104a1b KW |
1887 | utf8_fold_flags = FOLDEQ_S2_ALREADY_FOLDED; |
1888 | } | |
1889 | goto do_exactf_utf8; | |
16d951b7 | 1890 | |
a4525e78 KW |
1891 | case EXACTFLU8: |
1892 | if (! utf8_target) { /* All code points in this node require | |
1893 | UTF-8 to express. */ | |
1894 | break; | |
1895 | } | |
613abc6d KW |
1896 | utf8_fold_flags = FOLDEQ_LOCALE | FOLDEQ_S2_ALREADY_FOLDED |
1897 | | FOLDEQ_S2_FOLDS_SANE; | |
a4525e78 KW |
1898 | goto do_exactf_utf8; |
1899 | ||
73104a1b | 1900 | case EXACTFU: |
984e6dd1 DM |
1901 | if (is_utf8_pat || utf8_target) { |
1902 | utf8_fold_flags = is_utf8_pat ? FOLDEQ_S2_ALREADY_FOLDED : 0; | |
73104a1b KW |
1903 | goto do_exactf_utf8; |
1904 | } | |
fac1af77 | 1905 | |
73104a1b KW |
1906 | /* Any 'ss' in the pattern should have been replaced by regcomp, |
1907 | * so we don't have to worry here about this single special case | |
1908 | * in the Latin1 range */ | |
1909 | fold_array = PL_fold_latin1; | |
1910 | folder = foldEQ_latin1; | |
1911 | ||
924ba076 | 1912 | /* FALLTHROUGH */ |
73104a1b | 1913 | |
c52b8b12 | 1914 | do_exactf_non_utf8: /* Neither pattern nor string are UTF8, and there |
73104a1b KW |
1915 | are no glitches with fold-length differences |
1916 | between the target string and pattern */ | |
1917 | ||
1918 | /* The idea in the non-utf8 EXACTF* cases is to first find the | |
1919 | * first character of the EXACTF* node and then, if necessary, | |
1920 | * case-insensitively compare the full text of the node. c1 is the | |
1921 | * first character. c2 is its fold. This logic will not work for | |
1922 | * Unicode semantics and the german sharp ss, which hence should | |
1923 | * not be compiled into a node that gets here. */ | |
1924 | pat_string = STRING(c); | |
1925 | ln = STR_LEN(c); /* length to match in octets/bytes */ | |
1926 | ||
1927 | /* We know that we have to match at least 'ln' bytes (which is the | |
1928 | * same as characters, since not utf8). If we have to match 3 | |
1929 | * characters, and there are only 2 availabe, we know without | |
1930 | * trying that it will fail; so don't start a match past the | |
1931 | * required minimum number from the far end */ | |
ea3daa5d | 1932 | e = HOP3c(strend, -((SSize_t)ln), s); |
73104a1b | 1933 | |
02d5137b | 1934 | if (reginfo->intuit && e < s) { |
73104a1b KW |
1935 | e = s; /* Due to minlen logic of intuit() */ |
1936 | } | |
fac1af77 | 1937 | |
73104a1b KW |
1938 | c1 = *pat_string; |
1939 | c2 = fold_array[c1]; | |
1940 | if (c1 == c2) { /* If char and fold are the same */ | |
1941 | REXEC_FBC_EXACTISH_SCAN(*(U8*)s == c1); | |
1942 | } | |
1943 | else { | |
1944 | REXEC_FBC_EXACTISH_SCAN(*(U8*)s == c1 || *(U8*)s == c2); | |
1945 | } | |
1946 | break; | |
fac1af77 | 1947 | |
c52b8b12 KW |
1948 | do_exactf_utf8: |
1949 | { | |
73104a1b KW |
1950 | unsigned expansion; |
1951 | ||
1952 | /* If one of the operands is in utf8, we can't use the simpler folding | |
1953 | * above, due to the fact that many different characters can have the | |
1954 | * same fold, or portion of a fold, or different- length fold */ | |
1955 | pat_string = STRING(c); | |
1956 | ln = STR_LEN(c); /* length to match in octets/bytes */ | |
1957 | pat_end = pat_string + ln; | |
984e6dd1 | 1958 | lnc = is_utf8_pat /* length to match in characters */ |
73104a1b KW |
1959 | ? utf8_length((U8 *) pat_string, (U8 *) pat_end) |
1960 | : ln; | |
1961 | ||
1962 | /* We have 'lnc' characters to match in the pattern, but because of | |
1963 | * multi-character folding, each character in the target can match | |
1964 | * up to 3 characters (Unicode guarantees it will never exceed | |
1965 | * this) if it is utf8-encoded; and up to 2 if not (based on the | |
1966 | * fact that the Latin 1 folds are already determined, and the | |
1967 | * only multi-char fold in that range is the sharp-s folding to | |
1968 | * 'ss'. Thus, a pattern character can match as little as 1/3 of a | |
1969 | * string character. Adjust lnc accordingly, rounding up, so that | |
1970 | * if we need to match at least 4+1/3 chars, that really is 5. */ | |
1971 | expansion = (utf8_target) ? UTF8_MAX_FOLD_CHAR_EXPAND : 2; | |
1972 | lnc = (lnc + expansion - 1) / expansion; | |
1973 | ||
1974 | /* As in the non-UTF8 case, if we have to match 3 characters, and | |
1975 | * only 2 are left, it's guaranteed to fail, so don't start a | |
1976 | * match that would require us to go beyond the end of the string | |
1977 | */ | |
ea3daa5d | 1978 | e = HOP3c(strend, -((SSize_t)lnc), s); |
73104a1b | 1979 | |
02d5137b | 1980 | if (reginfo->intuit && e < s) { |
73104a1b KW |
1981 | e = s; /* Due to minlen logic of intuit() */ |
1982 | } | |
0658cdde | 1983 | |
73104a1b KW |
1984 | /* XXX Note that we could recalculate e to stop the loop earlier, |
1985 | * as the worst case expansion above will rarely be met, and as we | |
1986 | * go along we would usually find that e moves further to the left. | |
1987 | * This would happen only after we reached the point in the loop | |
1988 | * where if there were no expansion we should fail. Unclear if | |
1989 | * worth the expense */ | |
1990 | ||
1991 | while (s <= e) { | |
1992 | char *my_strend= (char *)strend; | |
1993 | if (foldEQ_utf8_flags(s, &my_strend, 0, utf8_target, | |
984e6dd1 | 1994 | pat_string, NULL, ln, is_utf8_pat, utf8_fold_flags) |
02d5137b | 1995 | && (reginfo->intuit || regtry(reginfo, &s)) ) |
73104a1b KW |
1996 | { |
1997 | goto got_it; | |
1998 | } | |
1999 | s += (utf8_target) ? UTF8SKIP(s) : 1; | |
2000 | } | |
2001 | break; | |
2002 | } | |
236d82fd | 2003 | |
73104a1b | 2004 | case BOUNDL: |
780fcc9f | 2005 | _CHECK_AND_WARN_PROBLEMATIC_LOCALE; |
64935bc6 | 2006 | if (FLAGS(c) != TRADITIONAL_BOUND) { |
89ad707a KW |
2007 | if (! IN_UTF8_CTYPE_LOCALE) { |
2008 | Perl_ck_warner(aTHX_ packWARN(WARN_LOCALE), | |
64935bc6 | 2009 | B_ON_NON_UTF8_LOCALE_IS_WRONG); |
89ad707a | 2010 | } |
64935bc6 KW |
2011 | goto do_boundu; |
2012 | } | |
2013 | ||
236d82fd | 2014 | FBC_BOUND(isWORDCHAR_LC, isWORDCHAR_LC_uvchr, isWORDCHAR_LC_utf8); |
73104a1b | 2015 | break; |
64935bc6 | 2016 | |
73104a1b | 2017 | case NBOUNDL: |
780fcc9f | 2018 | _CHECK_AND_WARN_PROBLEMATIC_LOCALE; |
64935bc6 | 2019 | if (FLAGS(c) != TRADITIONAL_BOUND) { |
89ad707a KW |
2020 | if (! IN_UTF8_CTYPE_LOCALE) { |
2021 | Perl_ck_warner(aTHX_ packWARN(WARN_LOCALE), | |
64935bc6 | 2022 | B_ON_NON_UTF8_LOCALE_IS_WRONG); |
89ad707a | 2023 | } |
64935bc6 KW |
2024 | goto do_nboundu; |
2025 | } | |
2026 | ||
236d82fd | 2027 | FBC_NBOUND(isWORDCHAR_LC, isWORDCHAR_LC_uvchr, isWORDCHAR_LC_utf8); |
73104a1b | 2028 | break; |
64935bc6 KW |
2029 | |
2030 | case BOUND: /* regcomp.c makes sure that this only has the traditional \b | |
2031 | meaning */ | |
2032 | assert(FLAGS(c) == TRADITIONAL_BOUND); | |
2033 | ||
236d82fd | 2034 | FBC_BOUND(isWORDCHAR, isWORDCHAR_uni, isWORDCHAR_utf8); |
73104a1b | 2035 | break; |
64935bc6 KW |
2036 | |
2037 | case BOUNDA: /* regcomp.c makes sure that this only has the traditional \b | |
2038 | meaning */ | |
2039 | assert(FLAGS(c) == TRADITIONAL_BOUND); | |
2040 | ||
44129e46 | 2041 | FBC_BOUND_A(isWORDCHAR_A); |
73104a1b | 2042 | break; |
64935bc6 KW |
2043 | |
2044 | case NBOUND: /* regcomp.c makes sure that this only has the traditional \b | |
2045 | meaning */ | |
2046 | assert(FLAGS(c) == TRADITIONAL_BOUND); | |
2047 | ||
236d82fd | 2048 | FBC_NBOUND(isWORDCHAR, isWORDCHAR_uni, isWORDCHAR_utf8); |
73104a1b | 2049 | break; |
64935bc6 KW |
2050 | |
2051 | case NBOUNDA: /* regcomp.c makes sure that this only has the traditional \b | |
2052 | meaning */ | |
2053 | assert(FLAGS(c) == TRADITIONAL_BOUND); | |
2054 | ||
44129e46 | 2055 | FBC_NBOUND_A(isWORDCHAR_A); |
73104a1b | 2056 | break; |
64935bc6 | 2057 | |
73104a1b | 2058 | case NBOUNDU: |
64935bc6 KW |
2059 | if ((bound_type) FLAGS(c) == TRADITIONAL_BOUND) { |
2060 | FBC_NBOUND(isWORDCHAR_L1, isWORDCHAR_uni, isWORDCHAR_utf8); | |
2061 | break; | |
2062 | } | |
2063 | ||
2064 | do_nboundu: | |
2065 | ||
2066 | to_complement = 1; | |
2067 | /* FALLTHROUGH */ | |
2068 | ||
2069 | case BOUNDU: | |
2070 | do_boundu: | |
2071 | switch((bound_type) FLAGS(c)) { | |
2072 | case TRADITIONAL_BOUND: | |
2073 | FBC_BOUND(isWORDCHAR_L1, isWORDCHAR_uni, isWORDCHAR_utf8); | |
2074 | break; | |
2075 | case GCB_BOUND: | |
2076 | if (s == reginfo->strbeg) { /* GCB always matches at begin and | |
2077 | end */ | |
2078 | if (to_complement ^ cBOOL(reginfo->intuit | |
2079 | || regtry(reginfo, &s))) | |
2080 | { | |
2081 | goto got_it; | |
2082 | } | |
2083 | s += (utf8_target) ? UTF8SKIP(s) : 1; | |
2084 | } | |
2085 | ||
2086 | if (utf8_target) { | |
85e5f08b | 2087 | GCB_enum before = getGCB_VAL_UTF8( |
64935bc6 KW |
2088 | reghop3((U8*)s, -1, |
2089 | (U8*)(reginfo->strbeg)), | |
2090 | (U8*) reginfo->strend); | |
2091 | while (s < strend) { | |
85e5f08b | 2092 | GCB_enum after = getGCB_VAL_UTF8((U8*) s, |
64935bc6 KW |
2093 | (U8*) reginfo->strend); |
2094 | if (to_complement ^ isGCB(before, after)) { | |
2095 | if (reginfo->intuit || regtry(reginfo, &s)) { | |
2096 | goto got_it; | |
2097 | } | |
2098 | before = after; | |
2099 | } | |
2100 | s += UTF8SKIP(s); | |
2101 | } | |
2102 | } | |
2103 | else { /* Not utf8. Everything is a GCB except between CR and | |
2104 | LF */ | |
2105 | while (s < strend) { | |
2106 | if (to_complement ^ (UCHARAT(s - 1) != '\r' | |
2107 | || UCHARAT(s) != '\n')) | |
2108 | { | |
2109 | if (reginfo->intuit || regtry(reginfo, &s)) { | |
2110 | goto got_it; | |
2111 | } | |
2112 | s++; | |
2113 | } | |
2114 | } | |
2115 | } | |
2116 | ||
2117 | if (to_complement ^ cBOOL(reginfo->intuit || regtry(reginfo, &s))) { | |
2118 | goto got_it; | |
2119 | } | |
2120 | break; | |
ae3bb8ea | 2121 | |
06ae2722 KW |
2122 | case SB_BOUND: |
2123 | if (s == reginfo->strbeg) { /* SB always matches at beginning */ | |
2124 | if (to_complement | |
2125 | ^ cBOOL(reginfo->intuit || regtry(reginfo, &s))) | |
2126 | { | |
2127 | goto got_it; | |
2128 | } | |
2129 | ||
2130 | /* Didn't match. Go try at the next position */ | |
2131 | s += (utf8_target) ? UTF8SKIP(s) : 1; | |
2132 | } | |
2133 | ||
2134 | if (utf8_target) { | |
85e5f08b | 2135 | SB_enum before = getSB_VAL_UTF8(reghop3((U8*)s, |
06ae2722 KW |
2136 | -1, |
2137 | (U8*)(reginfo->strbeg)), | |
2138 | (U8*) reginfo->strend); | |
2139 | while (s < strend) { | |
85e5f08b | 2140 | SB_enum after = getSB_VAL_UTF8((U8*) s, |
06ae2722 KW |
2141 | (U8*) reginfo->strend); |
2142 | if (to_complement ^ isSB(before, | |
2143 | after, | |
2144 | (U8*) reginfo->strbeg, | |
2145 | (U8*) s, | |
2146 | (U8*) reginfo->strend, | |
2147 | utf8_target)) | |
2148 | { | |
2149 | if (reginfo->intuit || regtry(reginfo, &s)) { | |
2150 | goto got_it; | |
2151 | } | |
2152 | before = after; | |
2153 | } | |
2154 | s += UTF8SKIP(s); | |
2155 | } | |
2156 | } | |
2157 | else { /* Not utf8. */ | |
85e5f08b | 2158 | SB_enum before = getSB_VAL_CP((U8) *(s -1)); |
06ae2722 | 2159 | while (s < strend) { |
85e5f08b | 2160 | SB_enum after = getSB_VAL_CP((U8) *s); |
06ae2722 KW |
2161 | if (to_complement ^ isSB(before, |
2162 | after, | |
2163 | (U8*) reginfo->strbeg, | |
2164 | (U8*) s, | |
2165 | (U8*) reginfo->strend, | |
2166 | utf8_target)) | |
2167 | { | |
2168 | if (reginfo->intuit || regtry(reginfo, &s)) { | |
2169 | goto got_it; | |
2170 | } | |
2171 | before = after; | |
2172 | } | |
2173 | s++; | |
2174 | } | |
2175 | } | |
2176 | ||
2177 | /* Here are at the final position in the target string. The SB | |
2178 | * value is always true here, so matches, depending on other | |
2179 | * constraints */ | |
2180 | if (to_complement ^ cBOOL(reginfo->intuit | |
2181 | || regtry(reginfo, &s))) | |
2182 | { | |
2183 | goto got_it; | |
2184 | } | |
2185 | ||
2186 | break; | |
2187 | ||
ae3bb8ea KW |
2188 | case WB_BOUND: |
2189 | if (s == reginfo->strbeg) { | |
2190 | if (to_complement ^ cBOOL(reginfo->intuit | |
2191 | || regtry(reginfo, &s))) | |
2192 | { | |
2193 | goto got_it; | |
2194 | } | |
2195 | s += (utf8_target) ? UTF8SKIP(s) : 1; | |
2196 | } | |
2197 | ||
2198 | if (utf8_target) { | |
2199 | /* We are at a boundary between char_sub_0 and char_sub_1. | |
2200 | * We also keep track of the value for char_sub_-1 as we | |
2201 | * loop through the line. Context may be needed to make a | |
2202 | * determination, and if so, this can save having to | |
2203 | * recalculate it */ | |
85e5f08b KW |
2204 | WB_enum previous = WB_UNKNOWN; |
2205 | WB_enum before = getWB_VAL_UTF8( | |
ae3bb8ea KW |
2206 | reghop3((U8*)s, |
2207 | -1, | |
2208 | (U8*)(reginfo->strbeg)), | |
2209 | (U8*) reginfo->strend); | |
2210 | while (s < strend) { | |
85e5f08b | 2211 | WB_enum after = getWB_VAL_UTF8((U8*) s, |
ae3bb8ea KW |
2212 | (U8*) reginfo->strend); |
2213 | if (to_complement ^ isWB(previous, | |
2214 | before, | |
2215 | after, | |
2216 | (U8*) reginfo->strbeg, | |
2217 | (U8*) s, | |
2218 | (U8*) reginfo->strend, | |
2219 | utf8_target)) | |
2220 | { | |
2221 | if (reginfo->intuit || regtry(reginfo, &s)) { | |
2222 | goto got_it; | |
2223 | } | |
2224 | previous = before; | |
2225 | before = after; | |
2226 | } | |
2227 | s += UTF8SKIP(s); | |
2228 | } | |
2229 | } | |
2230 | else { /* Not utf8. */ | |
85e5f08b KW |
2231 | WB_enum previous = WB_UNKNOWN; |
2232 | WB_enum before = getWB_VAL_CP((U8) *(s -1)); | |
ae3bb8ea | 2233 | while (s < strend) { |
85e5f08b | 2234 | WB_enum after = getWB_VAL_CP((U8) *s); |
ae3bb8ea KW |
2235 | if (to_complement ^ isWB(previous, |
2236 | before, | |
2237 | after, | |
2238 | (U8*) reginfo->strbeg, | |
2239 | (U8*) s, | |
2240 | (U8*) reginfo->strend, | |
2241 | utf8_target)) | |
2242 | { | |
2243 | if (reginfo->intuit || regtry(reginfo, &s)) { | |
2244 | goto got_it; | |
2245 | } | |
2246 | previous = before; | |
2247 | before = after; | |
2248 | } | |
2249 | s++; | |
2250 | } | |
2251 | } | |
2252 | ||
2253 | if (to_complement ^ cBOOL(reginfo->intuit | |
2254 | || regtry(reginfo, &s))) | |
2255 | { | |
2256 | goto got_it; | |
2257 | } | |
2258 | ||
2259 | break; | |
64935bc6 | 2260 | } |
73104a1b | 2261 | break; |
64935bc6 | 2262 | |
73104a1b KW |
2263 | case LNBREAK: |
2264 | REXEC_FBC_CSCAN(is_LNBREAK_utf8_safe(s, strend), | |
2265 | is_LNBREAK_latin1_safe(s, strend) | |
2266 | ); | |
2267 | break; | |
3018b823 KW |
2268 | |
2269 | /* The argument to all the POSIX node types is the class number to pass to | |
2270 | * _generic_isCC() to build a mask for searching in PL_charclass[] */ | |
2271 | ||
2272 | case NPOSIXL: | |
2273 | to_complement = 1; | |
2274 | /* FALLTHROUGH */ | |
2275 | ||
2276 | case POSIXL: | |
780fcc9f | 2277 | _CHECK_AND_WARN_PROBLEMATIC_LOCALE; |
3018b823 KW |
2278 | REXEC_FBC_CSCAN(to_complement ^ cBOOL(isFOO_utf8_lc(FLAGS(c), (U8 *) s)), |
2279 | to_complement ^ cBOOL(isFOO_lc(FLAGS(c), *s))); | |
73104a1b | 2280 | break; |
3018b823 KW |
2281 | |
2282 | case NPOSIXD: | |
2283 | to_complement = 1; | |
2284 | /* FALLTHROUGH */ | |
2285 | ||
2286 | case POSIXD: | |
2287 | if (utf8_target) { | |
2288 | goto posix_utf8; | |
2289 | } | |
2290 | goto posixa; | |
2291 | ||
2292 | case NPOSIXA: | |
2293 | if (utf8_target) { | |
2294 | /* The complement of something that matches only ASCII matches all | |
837226c8 KW |
2295 | * non-ASCII, plus everything in ASCII that isn't in the class. */ |
2296 | REXEC_FBC_UTF8_CLASS_SCAN(! isASCII_utf8(s) | |
3018b823 KW |
2297 | || ! _generic_isCC_A(*s, FLAGS(c))); |
2298 | break; | |
2299 | } | |
2300 | ||
2301 | to_complement = 1; | |
2302 | /* FALLTHROUGH */ | |
2303 | ||
73104a1b | 2304 | case POSIXA: |
3018b823 | 2305 | posixa: |
73104a1b | 2306 | /* Don't need to worry about utf8, as it can match only a single |
3018b823 KW |
2307 | * byte invariant character. */ |
2308 | REXEC_FBC_CLASS_SCAN( | |
2309 | to_complement ^ cBOOL(_generic_isCC_A(*s, FLAGS(c)))); | |
73104a1b | 2310 | break; |
3018b823 KW |
2311 | |
2312 | case NPOSIXU: | |
2313 | to_complement = 1; | |
2314 | /* FALLTHROUGH */ | |
2315 | ||
2316 | case POSIXU: | |
2317 | if (! utf8_target) { | |
2318 | REXEC_FBC_CLASS_SCAN(to_complement ^ cBOOL(_generic_isCC(*s, | |
2319 | FLAGS(c)))); | |
2320 | } | |
2321 | else { | |
2322 | ||
c52b8b12 | 2323 | posix_utf8: |
3018b823 KW |
2324 | classnum = (_char_class_number) FLAGS(c); |
2325 | if (classnum < _FIRST_NON_SWASH_CC) { | |
2326 | while (s < strend) { | |
2327 | ||
2328 | /* We avoid loading in the swash as long as possible, but | |
2329 | * should we have to, we jump to a separate loop. This | |
2330 | * extra 'if' statement is what keeps this code from being | |
2331 | * just a call to REXEC_FBC_UTF8_CLASS_SCAN() */ | |
2332 | if (UTF8_IS_ABOVE_LATIN1(*s)) { | |
2333 | goto found_above_latin1; | |
2334 | } | |
2335 | if ((UTF8_IS_INVARIANT(*s) | |
2336 | && to_complement ^ cBOOL(_generic_isCC((U8) *s, | |
2337 | classnum))) | |
2338 | || (UTF8_IS_DOWNGRADEABLE_START(*s) | |
2339 | && to_complement ^ cBOOL( | |
94bb8c36 KW |
2340 | _generic_isCC(TWO_BYTE_UTF8_TO_NATIVE(*s, |
2341 | *(s + 1)), | |
3018b823 KW |
2342 | classnum)))) |
2343 | { | |
02d5137b | 2344 | if (tmp && (reginfo->intuit || regtry(reginfo, &s))) |
3018b823 KW |
2345 | goto got_it; |
2346 | else { | |
2347 | tmp = doevery; | |
2348 | } | |
2349 | } | |
2350 | else { | |
2351 | tmp = 1; | |
2352 | } | |
2353 | s += UTF8SKIP(s); | |
2354 | } | |
2355 | } | |
2356 | else switch (classnum) { /* These classes are implemented as | |
2357 | macros */ | |
779cf272 | 2358 | case _CC_ENUM_SPACE: |
3018b823 KW |
2359 | REXEC_FBC_UTF8_CLASS_SCAN( |
2360 | to_complement ^ cBOOL(isSPACE_utf8(s))); | |
2361 | break; | |
2362 | ||
2363 | case _CC_ENUM_BLANK: | |
2364 | REXEC_FBC_UTF8_CLASS_SCAN( | |
2365 | to_complement ^ cBOOL(isBLANK_utf8(s))); | |
2366 | break; | |
2367 | ||
2368 | case _CC_ENUM_XDIGIT: | |
2369 | REXEC_FBC_UTF8_CLASS_SCAN( | |
2370 | to_complement ^ cBOOL(isXDIGIT_utf8(s))); | |
2371 | break; | |
2372 | ||
2373 | case _CC_ENUM_VERTSPACE: | |
2374 | REXEC_FBC_UTF8_CLASS_SCAN( | |
2375 | to_complement ^ cBOOL(isVERTWS_utf8(s))); | |
2376 | break; | |
2377 | ||
2378 | case _CC_ENUM_CNTRL: | |
2379 | REXEC_FBC_UTF8_CLASS_SCAN( | |
2380 | to_complement ^ cBOOL(isCNTRL_utf8(s))); | |
2381 | break; | |
2382 | ||
2383 | default: | |
2384 | Perl_croak(aTHX_ "panic: find_byclass() node %d='%s' has an unexpected character class '%d'", OP(c), PL_reg_name[OP(c)], classnum); | |
e5964223 | 2385 | NOT_REACHED; /* NOTREACHED */ |
3018b823 KW |
2386 | } |
2387 | } | |
2388 | break; | |
2389 | ||
2390 | found_above_latin1: /* Here we have to load a swash to get the result | |
2391 | for the current code point */ | |
2392 | if (! PL_utf8_swash_ptrs[classnum]) { | |
2393 | U8 flags = _CORE_SWASH_INIT_ACCEPT_INVLIST; | |
2394 | PL_utf8_swash_ptrs[classnum] = | |
2a16ac92 KW |
2395 | _core_swash_init("utf8", |
2396 | "", | |
2397 | &PL_sv_undef, 1, 0, | |
2398 | PL_XPosix_ptrs[classnum], &flags); | |
3018b823 KW |
2399 | } |
2400 | ||
2401 | /* This is a copy of the loop above for swash classes, though using the | |
2402 | * FBC macro instead of being expanded out. Since we've loaded the | |
2403 | * swash, we don't have to check for that each time through the loop */ | |
2404 | REXEC_FBC_UTF8_CLASS_SCAN( | |
2405 | to_complement ^ cBOOL(_generic_utf8( | |
2406 | classnum, | |
2407 | s, | |
2408 | swash_fetch(PL_utf8_swash_ptrs[classnum], | |
2409 | (U8 *) s, TRUE)))); | |
73104a1b KW |
2410 | break; |
2411 | ||
2412 | case AHOCORASICKC: | |
2413 | case AHOCORASICK: | |
2414 | { | |
2415 | DECL_TRIE_TYPE(c); | |
2416 | /* what trie are we using right now */ | |
2417 | reg_ac_data *aho = (reg_ac_data*)progi->data->data[ ARG( c ) ]; | |
2418 | reg_trie_data *trie = (reg_trie_data*)progi->data->data[ aho->trie ]; | |
2419 | HV *widecharmap = MUTABLE_HV(progi->data->data[ aho->trie + 1 ]); | |
2420 | ||
2421 | const char *last_start = strend - trie->minlen; | |
6148ee25 | 2422 | #ifdef DEBUGGING |
73104a1b | 2423 | const char *real_start = s; |
6148ee25 | 2424 | #endif |
73104a1b KW |
2425 | STRLEN maxlen = trie->maxlen; |
2426 | SV *sv_points; | |
2427 | U8 **points; /* map of where we were in the input string | |
2428 | when reading a given char. For ASCII this | |
2429 | is unnecessary overhead as the relationship | |
2430 | is always 1:1, but for Unicode, especially | |
2431 | case folded Unicode this is not true. */ | |
2432 | U8 foldbuf[ UTF8_MAXBYTES_CASE + 1 ]; | |
2433 | U8 *bitmap=NULL; | |
2434 | ||
2435 | ||
2436 | GET_RE_DEBUG_FLAGS_DECL; | |
2437 | ||
2438 | /* We can't just allocate points here. We need to wrap it in | |
2439 | * an SV so it gets freed properly if there is a croak while | |
2440 | * running the match */ | |
2441 | ENTER; | |
2442 | SAVETMPS; | |
2443 | sv_points=newSV(maxlen * sizeof(U8 *)); | |
2444 | SvCUR_set(sv_points, | |
2445 | maxlen * sizeof(U8 *)); | |
2446 | SvPOK_on(sv_points); | |
2447 | sv_2mortal(sv_points); | |
2448 | points=(U8**)SvPV_nolen(sv_points ); | |
2449 | if ( trie_type != trie_utf8_fold | |
2450 | && (trie->bitmap || OP(c)==AHOCORASICKC) ) | |
2451 | { | |
2452 | if (trie->bitmap) | |
2453 | bitmap=(U8*)trie->bitmap; | |
2454 | else | |
2455 | bitmap=(U8*)ANYOF_BITMAP(c); | |
2456 | } | |
2457 | /* this is the Aho-Corasick algorithm modified a touch | |
2458 | to include special handling for long "unknown char" sequences. | |
2459 | The basic idea being that we use AC as long as we are dealing | |
2460 | with a possible matching char, when we encounter an unknown char | |
2461 | (and we have not encountered an accepting state) we scan forward | |
2462 | until we find a legal starting char. | |
2463 | AC matching is basically that of trie matching, except that when | |
2464 | we encounter a failing transition, we fall back to the current | |
2465 | states "fail state", and try the current char again, a process | |
2466 | we repeat until we reach the root state, state 1, or a legal | |
2467 | transition. If we fail on the root state then we can either | |
2468 | terminate if we have reached an accepting state previously, or | |
2469 | restart the entire process from the beginning if we have not. | |
2470 | ||
2471 | */ | |
2472 | while (s <= last_start) { | |
2473 | const U32 uniflags = UTF8_ALLOW_DEFAULT; | |
2474 | U8 *uc = (U8*)s; | |
2475 | U16 charid = 0; | |
2476 | U32 base = 1; | |
2477 | U32 state = 1; | |
2478 | UV uvc = 0; | |
2479 | STRLEN len = 0; | |
2480 | STRLEN foldlen = 0; | |
2481 | U8 *uscan = (U8*)NULL; | |
2482 | U8 *leftmost = NULL; | |
2483 | #ifdef DEBUGGING | |
2484 | U32 accepted_word= 0; | |
786e8c11 | 2485 | #endif |
73104a1b KW |
2486 | U32 pointpos = 0; |
2487 | ||
2488 | while ( state && uc <= (U8*)strend ) { | |
2489 | int failed=0; | |
2490 | U32 word = aho->states[ state ].wordnum; | |
2491 | ||
2492 | if( state==1 ) { | |
2493 | if ( bitmap ) { | |
2494 | DEBUG_TRIE_EXECUTE_r( | |
2495 | if ( uc <= (U8*)last_start && !BITMAP_TEST(bitmap,*uc) ) { | |
2496 | dump_exec_pos( (char *)uc, c, strend, real_start, | |
2497 | (char *)uc, utf8_target ); | |
2498 | PerlIO_printf( Perl_debug_log, | |
2499 | " Scanning for legal start char...\n"); | |
2500 | } | |
2501 | ); | |
2502 | if (utf8_target) { | |
2503 | while ( uc <= (U8*)last_start && !BITMAP_TEST(bitmap,*uc) ) { | |
2504 | uc += UTF8SKIP(uc); | |
2505 | } | |
2506 | } else { | |
2507 | while ( uc <= (U8*)last_start && !BITMAP_TEST(bitmap,*uc) ) { | |
2508 | uc++; | |
2509 | } | |
786e8c11 | 2510 | } |
73104a1b | 2511 | s= (char *)uc; |
07be1b83 | 2512 | } |
73104a1b KW |
2513 | if (uc >(U8*)last_start) break; |
2514 | } | |
2515 | ||
2516 | if ( word ) { | |
2517 | U8 *lpos= points[ (pointpos - trie->wordinfo[word].len) % maxlen ]; | |
2518 | if (!leftmost || lpos < leftmost) { | |
2519 | DEBUG_r(accepted_word=word); | |
2520 | leftmost= lpos; | |
7016d6eb | 2521 | } |
73104a1b | 2522 | if (base==0) break; |
7016d6eb | 2523 | |
73104a1b KW |
2524 | } |
2525 | points[pointpos++ % maxlen]= uc; | |
2526 | if (foldlen || uc < (U8*)strend) { | |
2527 | REXEC_TRIE_READ_CHAR(trie_type, trie, | |
2528 | widecharmap, uc, | |
2529 | uscan, len, uvc, charid, foldlen, | |
2530 | foldbuf, uniflags); | |
2531 | DEBUG_TRIE_EXECUTE_r({ | |
2532 | dump_exec_pos( (char *)uc, c, strend, | |
2533 | real_start, s, utf8_target); | |
2534 | PerlIO_printf(Perl_debug_log, | |
2535 | " Charid:%3u CP:%4"UVxf" ", | |
2536 | charid, uvc); | |
2537 | }); | |
2538 | } | |
2539 | else { | |
2540 | len = 0; | |
2541 | charid = 0; | |
2542 | } | |
07be1b83 | 2543 | |
73104a1b KW |
2544 | |
2545 | do { | |
6148ee25 | 2546 | #ifdef DEBUGGING |
73104a1b | 2547 | word = aho->states[ state ].wordnum; |
6148ee25 | 2548 | #endif |
73104a1b KW |
2549 | base = aho->states[ state ].trans.base; |
2550 | ||
2551 | DEBUG_TRIE_EXECUTE_r({ | |
2552 | if (failed) | |
2553 | dump_exec_pos( (char *)uc, c, strend, real_start, | |
2554 | s, utf8_target ); | |
2555 | PerlIO_printf( Perl_debug_log, | |
2556 | "%sState: %4"UVxf", word=%"UVxf, | |
2557 | failed ? " Fail transition to " : "", | |
2558 | (UV)state, (UV)word); | |
2559 | }); | |
2560 | if ( base ) { | |
2561 | U32 tmp; | |
2562 | I32 offset; | |
2563 | if (charid && | |
2564 | ( ((offset = base + charid | |
2565 | - 1 - trie->uniquecharcount)) >= 0) | |
2566 | && ((U32)offset < trie->lasttrans) | |
2567 | && trie->trans[offset].check == state | |
2568 | && (tmp=trie->trans[offset].next)) | |
2569 | { | |
2570 | DEBUG_TRIE_EXECUTE_r( | |
2571 | PerlIO_printf( Perl_debug_log," - legal\n")); | |
2572 | state = tmp; | |
2573 | break; | |
07be1b83 YO |
2574 | } |
2575 | else { | |
786e8c11 | 2576 | DEBUG_TRIE_EXECUTE_r( |
73104a1b | 2577 | PerlIO_printf( Perl_debug_log," - fail\n")); |
786e8c11 | 2578 | failed = 1; |
73104a1b | 2579 | state = aho->fail[state]; |
07be1b83 | 2580 | } |
07be1b83 | 2581 | } |
73104a1b KW |
2582 | else { |
2583 | /* we must be accepting here */ | |
2584 | DEBUG_TRIE_EXECUTE_r( | |
2585 | PerlIO_printf( Perl_debug_log," - accepting\n")); | |
2586 | failed = 1; | |
2587 | break; | |
786e8c11 | 2588 | } |
73104a1b KW |
2589 | } while(state); |
2590 | uc += len; | |
2591 | if (failed) { | |
2592 | if (leftmost) | |
2593 | break; | |
2594 | if (!state) state = 1; | |
07be1b83 | 2595 | } |
73104a1b KW |
2596 | } |
2597 | if ( aho->states[ state ].wordnum ) { | |
2598 | U8 *lpos = points[ (pointpos - trie->wordinfo[aho->states[ state ].wordnum].len) % maxlen ]; | |
2599 | if (!leftmost || lpos < leftmost) { | |
2600 | DEBUG_r(accepted_word=aho->states[ state ].wordnum); | |
2601 | leftmost = lpos; | |
07be1b83 YO |
2602 | } |
2603 | } | |
73104a1b KW |
2604 | if (leftmost) { |
2605 | s = (char*)leftmost; | |
2606 | DEBUG_TRIE_EXECUTE_r({ | |
2607 | PerlIO_printf( | |
2608 | Perl_debug_log,"Matches word #%"UVxf" at position %"IVdf". Trying full pattern...\n", | |
2609 | (UV)accepted_word, (IV)(s - real_start) | |
2610 | ); | |
2611 | }); | |
02d5137b | 2612 | if (reginfo->intuit || regtry(reginfo, &s)) { |
73104a1b KW |
2613 | FREETMPS; |
2614 | LEAVE; | |
2615 | goto got_it; | |
2616 | } | |
2617 | s = HOPc(s,1); | |
2618 | DEBUG_TRIE_EXECUTE_r({ | |
2619 | PerlIO_printf( Perl_debug_log,"Pattern failed. Looking for new start point...\n"); | |
2620 | }); | |
2621 | } else { | |
2622 | DEBUG_TRIE_EXECUTE_r( | |
2623 | PerlIO_printf( Perl_debug_log,"No match.\n")); | |
2624 | break; | |
2625 | } | |
2626 | } | |
2627 | FREETMPS; | |
2628 | LEAVE; | |
2629 | } | |
2630 | break; | |
2631 | default: | |
2632 | Perl_croak(aTHX_ "panic: unknown regstclass %d", (int)OP(c)); | |
73104a1b KW |
2633 | } |
2634 | return 0; | |
2635 | got_it: | |
2636 | return s; | |
6eb5f6b9 JH |
2637 | } |
2638 | ||
60165aa4 DM |
2639 | /* set RX_SAVED_COPY, RX_SUBBEG etc. |
2640 | * flags have same meanings as with regexec_flags() */ | |
2641 | ||
749f4950 DM |
2642 | static void |
2643 | S_reg_set_capture_string(pTHX_ REGEXP * const rx, | |
60165aa4 DM |
2644 | char *strbeg, |
2645 | char *strend, | |
2646 | SV *sv, | |
2647 | U32 flags, | |
2648 | bool utf8_target) | |
2649 | { | |
2650 | struct regexp *const prog = ReANY(rx); | |
2651 | ||
60165aa4 DM |
2652 | if (flags & REXEC_COPY_STR) { |
2653 | #ifdef PERL_ANY_COW | |
2654 | if (SvCANCOW(sv)) { | |
2655 | if (DEBUG_C_TEST) { | |
2656 | PerlIO_printf(Perl_debug_log, | |
2657 | "Copy on write: regexp capture, type %d\n", | |
2658 | (int) SvTYPE(sv)); | |
2659 | } | |
5411a0e5 DM |
2660 | /* Create a new COW SV to share the match string and store |
2661 | * in saved_copy, unless the current COW SV in saved_copy | |
2662 | * is valid and suitable for our purpose */ | |
2663 | if (( prog->saved_copy | |
2664 | && SvIsCOW(prog->saved_copy) | |
2665 | && SvPOKp(prog->saved_copy) | |
2666 | && SvIsCOW(sv) | |
2667 | && SvPOKp(sv) | |
2668 | && SvPVX(sv) == SvPVX(prog->saved_copy))) | |
a76b0e90 | 2669 | { |
5411a0e5 DM |
2670 | /* just reuse saved_copy SV */ |
2671 | if (RXp_MATCH_COPIED(prog)) { | |
2672 | Safefree(prog->subbeg); | |
2673 | RXp_MATCH_COPIED_off(prog); | |
2674 | } | |
2675 | } | |
2676 | else { | |
2677 | /* create new COW SV to share string */ | |
a76b0e90 DM |
2678 | RX_MATCH_COPY_FREE(rx); |
2679 | prog->saved_copy = sv_setsv_cow(prog->saved_copy, sv); | |
a76b0e90 | 2680 | } |
5411a0e5 DM |
2681 | prog->subbeg = (char *)SvPVX_const(prog->saved_copy); |
2682 | assert (SvPOKp(prog->saved_copy)); | |
60165aa4 DM |
2683 | prog->sublen = strend - strbeg; |
2684 | prog->suboffset = 0; | |
2685 | prog->subcoffset = 0; | |
2686 | } else | |
2687 | #endif | |
2688 | { | |
99a90e59 FC |
2689 | SSize_t min = 0; |
2690 | SSize_t max = strend - strbeg; | |
ea3daa5d | 2691 | SSize_t sublen; |
60165aa4 DM |
2692 | |
2693 | if ( (flags & REXEC_COPY_SKIP_POST) | |
e322109a | 2694 | && !(prog->extflags & RXf_PMf_KEEPCOPY) /* //p */ |
60165aa4 DM |
2695 | && !(PL_sawampersand & SAWAMPERSAND_RIGHT) |
2696 | ) { /* don't copy $' part of string */ | |
2697 | U32 n = 0; | |
2698 | max = -1; | |
2699 | /* calculate the right-most part of the string covered | |
2700 | * by a capture. Due to look-ahead, this may be to | |
2701 | * the right of $&, so we have to scan all captures */ | |
2702 | while (n <= prog->lastparen) { | |
2703 | if (prog->offs[n].end > max) | |
2704 | max = prog->offs[n].end; | |
2705 | n++; | |
2706 | } | |
2707 | if (max == -1) | |
2708 | max = (PL_sawampersand & SAWAMPERSAND_LEFT) | |
2709 | ? prog->offs[0].start | |
2710 | : 0; | |
2711 | assert(max >= 0 && max <= strend - strbeg); | |
2712 | } | |
2713 | ||
2714 | if ( (flags & REXEC_COPY_SKIP_PRE) | |
e322109a | 2715 | && !(prog->extflags & RXf_PMf_KEEPCOPY) /* //p */ |
60165aa4 DM |
2716 | && !(PL_sawampersand & SAWAMPERSAND_LEFT) |
2717 | ) { /* don't copy $` part of string */ | |
2718 | U32 n = 0; | |
2719 | min = max; | |
2720 | /* calculate the left-most part of the string covered | |
2721 | * by a capture. Due to look-behind, this may be to | |
2722 | * the left of $&, so we have to scan all captures */ | |
2723 | while (min && n <= prog->lastparen) { | |
2724 | if ( prog->offs[n].start != -1 | |
2725 | && prog->offs[n].start < min) | |
2726 | { | |
2727 | min = prog->offs[n].start; | |
2728 | } | |
2729 | n++; | |
2730 | } | |
2731 | if ((PL_sawampersand & SAWAMPERSAND_RIGHT) | |
2732 | && min > prog->offs[0].end | |
2733 | ) | |
2734 | min = prog->offs[0].end; | |
2735 | ||
2736 | } | |
2737 | ||
2738 | assert(min >= 0 && min <= max && min <= strend - strbeg); | |
2739 | sublen = max - min; | |
2740 | ||
2741 | if (RX_MATCH_COPIED(rx)) { | |
2742 | if (sublen > prog->sublen) | |
2743 | prog->subbeg = | |
2744 | (char*)saferealloc(prog->subbeg, sublen+1); | |
2745 | } | |
2746 | else | |
2747 | prog->subbeg = (char*)safemalloc(sublen+1); | |
2748 | Copy(strbeg + min, prog->subbeg, sublen, char); | |
2749 | prog->subbeg[sublen] = '\0'; | |
2750 | prog->suboffset = min; | |
2751 | prog->sublen = sublen; | |
2752 | RX_MATCH_COPIED_on(rx); | |
2753 | } | |
2754 | prog->subcoffset = prog->suboffset; | |
2755 | if (prog->suboffset && utf8_target) { | |
2756 | /* Convert byte offset to chars. | |
2757 | * XXX ideally should only compute this if @-/@+ | |
2758 | * has been seen, a la PL_sawampersand ??? */ | |
2759 | ||
2760 | /* If there's a direct correspondence between the | |
2761 | * string which we're matching and the original SV, | |
2762 | * then we can use the utf8 len cache associated with | |
2763 | * the SV. In particular, it means that under //g, | |
2764 | * sv_pos_b2u() will use the previously cached | |
2765 | * position to speed up working out the new length of | |
2766 | * subcoffset, rather than counting from the start of | |
2767 | * the string each time. This stops | |
2768 | * $x = "\x{100}" x 1E6; 1 while $x =~ /(.)/g; | |
2769 | * from going quadratic */ | |
2770 | if (SvPOKp(sv) && SvPVX(sv) == strbeg) | |
ea3daa5d FC |
2771 | prog->subcoffset = sv_pos_b2u_flags(sv, prog->subcoffset, |
2772 | SV_GMAGIC|SV_CONST_RETURN); | |
60165aa4 DM |
2773 | else |
2774 | prog->subcoffset = utf8_length((U8*)strbeg, | |
2775 | (U8*)(strbeg+prog->suboffset)); | |
2776 | } | |
2777 | } | |
2778 | else { | |
2779 | RX_MATCH_COPY_FREE(rx); | |
2780 | prog->subbeg = strbeg; | |
2781 | prog->suboffset = 0; | |
2782 | prog->subcoffset = 0; | |
2783 | prog->sublen = strend - strbeg; | |
2784 | } | |
2785 | } | |
2786 | ||
2787 | ||
2788 | ||
fae667d5 | 2789 | |
6eb5f6b9 JH |
2790 | /* |
2791 | - regexec_flags - match a regexp against a string | |
2792 | */ | |
2793 | I32 | |
5aaab254 | 2794 | Perl_regexec_flags(pTHX_ REGEXP * const rx, char *stringarg, char *strend, |
ea3daa5d | 2795 | char *strbeg, SSize_t minend, SV *sv, void *data, U32 flags) |
8fd1a950 DM |
2796 | /* stringarg: the point in the string at which to begin matching */ |
2797 | /* strend: pointer to null at end of string */ | |
2798 | /* strbeg: real beginning of string */ | |
2799 | /* minend: end of match must be >= minend bytes after stringarg. */ | |
2800 | /* sv: SV being matched: only used for utf8 flag, pos() etc; string | |
2801 | * itself is accessed via the pointers above */ | |
2802 | /* data: May be used for some additional optimizations. | |
d058ec57 | 2803 | Currently unused. */ |
a340edde | 2804 | /* flags: For optimizations. See REXEC_* in regexp.h */ |
8fd1a950 | 2805 | |
6eb5f6b9 | 2806 | { |
8d919b0a | 2807 | struct regexp *const prog = ReANY(rx); |
5aaab254 | 2808 | char *s; |
eb578fdb | 2809 | regnode *c; |
03c83e26 | 2810 | char *startpos; |
ea3daa5d FC |
2811 | SSize_t minlen; /* must match at least this many chars */ |
2812 | SSize_t dontbother = 0; /* how many characters not to try at end */ | |
f2ed9b32 | 2813 | const bool utf8_target = cBOOL(DO_UTF8(sv)); |
2757e526 | 2814 | I32 multiline; |
f8fc2ecf | 2815 | RXi_GET_DECL(prog,progi); |
02d5137b DM |
2816 | regmatch_info reginfo_buf; /* create some info to pass to regtry etc */ |
2817 | regmatch_info *const reginfo = ®info_buf; | |
e9105d30 | 2818 | regexp_paren_pair *swap = NULL; |
006f26b2 | 2819 | I32 oldsave; |
a3621e74 YO |
2820 | GET_RE_DEBUG_FLAGS_DECL; |
2821 | ||
7918f24d | 2822 | PERL_ARGS_ASSERT_REGEXEC_FLAGS; |
9d4ba2ae | 2823 | PERL_UNUSED_ARG(data); |
6eb5f6b9 JH |
2824 | |
2825 | /* Be paranoid... */ | |
3dc78631 | 2826 | if (prog == NULL) { |
6eb5f6b9 | 2827 | Perl_croak(aTHX_ "NULL regexp parameter"); |
6eb5f6b9 JH |
2828 | } |
2829 | ||
6c3fea77 | 2830 | DEBUG_EXECUTE_r( |
03c83e26 | 2831 | debug_start_match(rx, utf8_target, stringarg, strend, |
6c3fea77 DM |
2832 | "Matching"); |
2833 | ); | |
8adc0f72 | 2834 | |
b342a604 DM |
2835 | startpos = stringarg; |
2836 | ||
58430ea8 | 2837 | if (prog->intflags & PREGf_GPOS_SEEN) { |
d307c076 DM |
2838 | MAGIC *mg; |
2839 | ||
fef7148b DM |
2840 | /* set reginfo->ganch, the position where \G can match */ |
2841 | ||
2842 | reginfo->ganch = | |
2843 | (flags & REXEC_IGNOREPOS) | |
2844 | ? stringarg /* use start pos rather than pos() */ | |
3dc78631 | 2845 | : ((mg = mg_find_mglob(sv)) && mg->mg_len >= 0) |
25fdce4a FC |
2846 | /* Defined pos(): */ |
2847 | ? strbeg + MgBYTEPOS(mg, sv, strbeg, strend-strbeg) | |
fef7148b DM |
2848 | : strbeg; /* pos() not defined; use start of string */ |
2849 | ||
2850 | DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log, | |
7b0eb0b8 | 2851 | "GPOS ganch set to strbeg[%"IVdf"]\n", (IV)(reginfo->ganch - strbeg))); |
fef7148b | 2852 | |
03c83e26 DM |
2853 | /* in the presence of \G, we may need to start looking earlier in |
2854 | * the string than the suggested start point of stringarg: | |
0b2c2a84 | 2855 | * if prog->gofs is set, then that's a known, fixed minimum |
03c83e26 DM |
2856 | * offset, such as |
2857 | * /..\G/: gofs = 2 | |
2858 | * /ab|c\G/: gofs = 1 | |
2859 | * or if the minimum offset isn't known, then we have to go back | |
2860 | * to the start of the string, e.g. /w+\G/ | |
2861 | */ | |
2bfbe302 | 2862 | |
8e1490ee | 2863 | if (prog->intflags & PREGf_ANCH_GPOS) { |
2bfbe302 DM |
2864 | startpos = reginfo->ganch - prog->gofs; |
2865 | if (startpos < | |
2866 | ((flags & REXEC_FAIL_ON_UNDERFLOW) ? stringarg : strbeg)) | |
2867 | { | |
2868 | DEBUG_r(PerlIO_printf(Perl_debug_log, | |
2869 | "fail: ganch-gofs before earliest possible start\n")); | |
2870 | return 0; | |
2871 | } | |
2872 | } | |
2873 | else if (prog->gofs) { | |
b342a604 DM |
2874 | if (startpos - prog->gofs < strbeg) |
2875 | startpos = strbeg; | |
2876 | else | |
2877 | startpos -= prog->gofs; | |
03c83e26 | 2878 | } |
58430ea8 | 2879 | else if (prog->intflags & PREGf_GPOS_FLOAT) |
b342a604 | 2880 | startpos = strbeg; |
03c83e26 DM |
2881 | } |
2882 | ||
2883 | minlen = prog->minlen; | |
b342a604 | 2884 | if ((startpos + minlen) > strend || startpos < strbeg) { |
03c83e26 DM |
2885 | DEBUG_r(PerlIO_printf(Perl_debug_log, |
2886 | "Regex match can't succeed, so not even tried\n")); | |
2887 | return 0; | |
2888 | } | |
2889 | ||
63a3746a DM |
2890 | /* at the end of this function, we'll do a LEAVE_SCOPE(oldsave), |
2891 | * which will call destuctors to reset PL_regmatch_state, free higher | |
2892 | * PL_regmatch_slabs, and clean up regmatch_info_aux and | |
2893 | * regmatch_info_aux_eval */ | |
2894 | ||
2895 | oldsave = PL_savestack_ix; | |
2896 | ||
dfa77d06 DM |
2897 | s = startpos; |
2898 | ||
e322109a | 2899 | if ((prog->extflags & RXf_USE_INTUIT) |
7fadf4a7 DM |
2900 | && !(flags & REXEC_CHECKED)) |
2901 | { | |
dfa77d06 | 2902 | s = re_intuit_start(rx, sv, strbeg, startpos, strend, |
7fadf4a7 | 2903 | flags, NULL); |
dfa77d06 | 2904 | if (!s) |
7fadf4a7 DM |
2905 | return 0; |
2906 | ||
e322109a | 2907 | if (prog->extflags & RXf_CHECK_ALL) { |
7fadf4a7 DM |
2908 | /* we can match based purely on the result of INTUIT. |
2909 | * Set up captures etc just for $& and $-[0] | |
2910 | * (an intuit-only match wont have $1,$2,..) */ | |
2911 | assert(!prog->nparens); | |
d5e7783a DM |
2912 | |
2913 | /* s/// doesn't like it if $& is earlier than where we asked it to | |
2914 | * start searching (which can happen on something like /.\G/) */ | |
2915 | if ( (flags & REXEC_FAIL_ON_UNDERFLOW) | |
2916 | && (s < stringarg)) | |
2917 | { | |
2918 | /* this should only be possible under \G */ | |
58430ea8 | 2919 | assert(prog->intflags & PREGf_GPOS_SEEN); |
d5e7783a DM |
2920 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, |
2921 | "matched, but failing for REXEC_FAIL_ON_UNDERFLOW\n")); | |
2922 | goto phooey; | |
2923 | } | |
2924 | ||
7fadf4a7 DM |
2925 | /* match via INTUIT shouldn't have any captures. |
2926 | * Let @-, @+, $^N know */ | |
2927 | prog->lastparen = prog->lastcloseparen = 0; | |
2928 | RX_MATCH_UTF8_set(rx, utf8_target); | |
3ff69bd6 DM |
2929 | prog->offs[0].start = s - strbeg; |
2930 | prog->offs[0].end = utf8_target | |
2931 | ? (char*)utf8_hop((U8*)s, prog->minlenret) - strbeg | |
2932 | : s - strbeg + prog->minlenret; | |
7fadf4a7 | 2933 | if ( !(flags & REXEC_NOT_FIRST) ) |
749f4950 | 2934 | S_reg_set_capture_string(aTHX_ rx, |
7fadf4a7 DM |
2935 | strbeg, strend, |
2936 | sv, flags, utf8_target); | |
2937 | ||
7fadf4a7 DM |
2938 | return 1; |
2939 | } | |
2940 | } | |
2941 | ||
6c3fea77 | 2942 | multiline = prog->extflags & RXf_PMf_MULTILINE; |
1de06328 | 2943 | |
dfa77d06 | 2944 | if (strend - s < (minlen+(prog->check_offset_min<0?prog->check_offset_min:0))) { |
a3621e74 | 2945 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, |
a72c7584 JH |
2946 | "String too short [regexec_flags]...\n")); |
2947 | goto phooey; | |
1aa99e6b | 2948 | } |
1de06328 | 2949 | |
6eb5f6b9 | 2950 | /* Check validity of program. */ |
f8fc2ecf | 2951 | if (UCHARAT(progi->program) != REG_MAGIC) { |
6eb5f6b9 JH |
2952 | Perl_croak(aTHX_ "corrupted regexp program"); |
2953 | } | |
2954 | ||
1738e041 | 2955 | RX_MATCH_TAINTED_off(rx); |
ab4e48c1 | 2956 | RX_MATCH_UTF8_set(rx, utf8_target); |
1738e041 | 2957 | |
6c3fea77 DM |
2958 | reginfo->prog = rx; /* Yes, sorry that this is confusing. */ |
2959 | reginfo->intuit = 0; | |
2960 | reginfo->is_utf8_target = cBOOL(utf8_target); | |
02d5137b DM |
2961 | reginfo->is_utf8_pat = cBOOL(RX_UTF8(rx)); |
2962 | reginfo->warned = FALSE; | |
9d9163fb | 2963 | reginfo->strbeg = strbeg; |
02d5137b | 2964 | reginfo->sv = sv; |
1cb48e53 | 2965 | reginfo->poscache_maxiter = 0; /* not yet started a countdown */ |
220db18a | 2966 | reginfo->strend = strend; |
6eb5f6b9 | 2967 | /* see how far we have to get to not match where we matched before */ |
fe3974be | 2968 | reginfo->till = stringarg + minend; |
6eb5f6b9 | 2969 | |
60779a30 | 2970 | if (prog->extflags & RXf_EVAL_SEEN && SvPADTMP(sv)) { |
82c23608 FC |
2971 | /* SAVEFREESV, not sv_mortalcopy, as this SV must last until after |
2972 | S_cleanup_regmatch_info_aux has executed (registered by | |
2973 | SAVEDESTRUCTOR_X below). S_cleanup_regmatch_info_aux modifies | |
2974 | magic belonging to this SV. | |
2975 | Not newSVsv, either, as it does not COW. | |
2976 | */ | |
2977 | reginfo->sv = newSV(0); | |
4cba5ac0 | 2978 | SvSetSV_nosteal(reginfo->sv, sv); |
82c23608 FC |
2979 | SAVEFREESV(reginfo->sv); |
2980 | } | |
2981 | ||
331b2dcc DM |
2982 | /* reserve next 2 or 3 slots in PL_regmatch_state: |
2983 | * slot N+0: may currently be in use: skip it | |
2984 | * slot N+1: use for regmatch_info_aux struct | |
2985 | * slot N+2: use for regmatch_info_aux_eval struct if we have (?{})'s | |
2986 | * slot N+3: ready for use by regmatch() | |
2987 | */ | |
bf2039a9 | 2988 | |
331b2dcc DM |
2989 | { |
2990 | regmatch_state *old_regmatch_state; | |
2991 | regmatch_slab *old_regmatch_slab; | |
2992 | int i, max = (prog->extflags & RXf_EVAL_SEEN) ? 2 : 1; | |
2993 | ||
2994 | /* on first ever match, allocate first slab */ | |
2995 | if (!PL_regmatch_slab) { | |
2996 | Newx(PL_regmatch_slab, 1, regmatch_slab); | |
2997 | PL_regmatch_slab->prev = NULL; | |
2998 | PL_regmatch_slab->next = NULL; | |
2999 | PL_regmatch_state = SLAB_FIRST(PL_regmatch_slab); | |
3000 | } | |
bf2039a9 | 3001 | |
331b2dcc DM |
3002 | old_regmatch_state = PL_regmatch_state; |
3003 | old_regmatch_slab = PL_regmatch_slab; | |
bf2039a9 | 3004 | |
331b2dcc DM |
3005 | for (i=0; i <= max; i++) { |
3006 | if (i == 1) | |
3007 | reginfo->info_aux = &(PL_regmatch_state->u.info_aux); | |
3008 | else if (i ==2) | |
3009 | reginfo->info_aux_eval = | |
3010 | reginfo->info_aux->info_aux_eval = | |
3011 | &(PL_regmatch_state->u.info_aux_eval); | |
bf2039a9 | 3012 | |
331b2dcc DM |
3013 | if (++PL_regmatch_state > SLAB_LAST(PL_regmatch_slab)) |
3014 | PL_regmatch_state = S_push_slab(aTHX); | |
3015 | } | |
bf2039a9 | 3016 | |
331b2dcc DM |
3017 | /* note initial PL_regmatch_state position; at end of match we'll |
3018 | * pop back to there and free any higher slabs */ | |
bf2039a9 | 3019 | |
331b2dcc DM |
3020 | reginfo->info_aux->old_regmatch_state = old_regmatch_state; |
3021 | reginfo->info_aux->old_regmatch_slab = old_regmatch_slab; | |
2ac8ff4b | 3022 | reginfo->info_aux->poscache = NULL; |
bf2039a9 | 3023 | |
331b2dcc | 3024 | SAVEDESTRUCTOR_X(S_cleanup_regmatch_info_aux, reginfo->info_aux); |
bf2039a9 | 3025 | |
331b2dcc DM |
3026 | if ((prog->extflags & RXf_EVAL_SEEN)) |
3027 | S_setup_eval_state(aTHX_ reginfo); | |
3028 | else | |
3029 | reginfo->info_aux_eval = reginfo->info_aux->info_aux_eval = NULL; | |
bf2039a9 | 3030 | } |
d3aa529c | 3031 | |
6eb5f6b9 | 3032 | /* If there is a "must appear" string, look for it. */ |
6eb5f6b9 | 3033 | |
288b8c02 | 3034 | if (PL_curpm && (PM_GETRE(PL_curpm) == rx)) { |
e9105d30 GG |
3035 | /* We have to be careful. If the previous successful match |
3036 | was from this regex we don't want a subsequent partially | |
3037 | successful match to clobber the old results. | |
3038 | So when we detect this possibility we add a swap buffer | |
d8da0584 KW |
3039 | to the re, and switch the buffer each match. If we fail, |
3040 | we switch it back; otherwise we leave it swapped. | |
e9105d30 GG |
3041 | */ |
3042 | swap = prog->offs; | |
3043 | /* do we need a save destructor here for eval dies? */ | |
3044 | Newxz(prog->offs, (prog->nparens + 1), regexp_paren_pair); | |
495f47a5 DM |
3045 | DEBUG_BUFFERS_r(PerlIO_printf(Perl_debug_log, |
3046 | "rex=0x%"UVxf" saving offs: orig=0x%"UVxf" new=0x%"UVxf"\n", | |
3047 | PTR2UV(prog), | |
3048 | PTR2UV(swap), | |
3049 | PTR2UV(prog->offs) | |
3050 | )); | |
c74340f9 | 3051 | } |
6eb5f6b9 | 3052 | |
0fa70a06 DM |
3053 | /* Simplest case: anchored match need be tried only once, or with |
3054 | * MBOL, only at the beginning of each line. | |
3055 | * | |
3056 | * Note that /.*.../ sets PREGf_IMPLICIT|MBOL, while /.*.../s sets | |
3057 | * PREGf_IMPLICIT|SBOL. The idea is that with /.*.../s, if it doesn't | |
3058 | * match at the start of the string then it won't match anywhere else | |
3059 | * either; while with /.*.../, if it doesn't match at the beginning, | |
3060 | * the earliest it could match is at the start of the next line */ | |
3061 | ||
8e1490ee | 3062 | if (prog->intflags & (PREGf_ANCH & ~PREGf_ANCH_GPOS)) { |
0fa70a06 DM |
3063 | char *end; |
3064 | ||
3065 | if (regtry(reginfo, &s)) | |
6eb5f6b9 | 3066 | goto got_it; |
0fa70a06 DM |
3067 | |
3068 | if (!(prog->intflags & PREGf_ANCH_MBOL)) | |
3069 | goto phooey; | |
3070 | ||
3071 | /* didn't match at start, try at other newline positions */ | |
3072 | ||
3073 | if (minlen) | |
3074 | dontbother = minlen - 1; | |
3075 | end = HOP3c(strend, -dontbother, strbeg) - 1; | |
3076 | ||
3077 | /* skip to next newline */ | |
3078 | ||
3079 | while (s <= end) { /* note it could be possible to match at the end of the string */ | |
3080 | /* NB: newlines are the same in unicode as they are in latin */ | |
3081 | if (*s++ != '\n') | |
3082 | continue; | |
3083 | if (prog->check_substr || prog->check_utf8) { | |
3084 | /* note that with PREGf_IMPLICIT, intuit can only fail | |
3085 | * or return the start position, so it's of limited utility. | |
3086 | * Nevertheless, I made the decision that the potential for | |
3087 | * quick fail was still worth it - DAPM */ | |
3088 | s = re_intuit_start(rx, sv, strbeg, s, strend, flags, NULL); | |
3089 | if (!s) | |
3090 | goto phooey; | |
3091 | } | |
3092 | if (regtry(reginfo, &s)) | |
3093 | goto got_it; | |
3094 | } | |
3095 | goto phooey; | |
3096 | } /* end anchored search */ | |
3097 | ||
3098 | if (prog->intflags & PREGf_ANCH_GPOS) | |
f9f4320a | 3099 | { |
a8430a8b YO |
3100 | /* PREGf_ANCH_GPOS should never be true if PREGf_GPOS_SEEN is not true */ |
3101 | assert(prog->intflags & PREGf_GPOS_SEEN); | |
2bfbe302 DM |
3102 | /* For anchored \G, the only position it can match from is |
3103 | * (ganch-gofs); we already set startpos to this above; if intuit | |
3104 | * moved us on from there, we can't possibly succeed */ | |
3105 | assert(startpos == reginfo->ganch - prog->gofs); | |
3106 | if (s == startpos && regtry(reginfo, &s)) | |
6eb5f6b9 JH |
3107 | goto got_it; |
3108 | goto phooey; | |
3109 | } | |
3110 | ||
3111 | /* Messy cases: unanchored match. */ | |
bbe252da | 3112 | if ((prog->anchored_substr || prog->anchored_utf8) && prog->intflags & PREGf_SKIP) { |
6eb5f6b9 | 3113 | /* we have /x+whatever/ */ |
984e6dd1 | 3114 | /* it must be a one character string (XXXX Except is_utf8_pat?) */ |
33b8afdf | 3115 | char ch; |
bf93d4cc GS |
3116 | #ifdef DEBUGGING |
3117 | int did_match = 0; | |
3118 | #endif | |
f2ed9b32 | 3119 | if (utf8_target) { |
7e0d5ad7 KW |
3120 | if (! prog->anchored_utf8) { |
3121 | to_utf8_substr(prog); | |
3122 | } | |
3123 | ch = SvPVX_const(prog->anchored_utf8)[0]; | |
4cadc6a9 | 3124 | REXEC_FBC_SCAN( |
6eb5f6b9 | 3125 | if (*s == ch) { |
a3621e74 | 3126 | DEBUG_EXECUTE_r( did_match = 1 ); |
02d5137b | 3127 | if (regtry(reginfo, &s)) goto got_it; |
6eb5f6b9 JH |
3128 | s += UTF8SKIP(s); |
3129 | while (s < strend && *s == ch) | |
3130 | s += UTF8SKIP(s); | |
3131 | } | |
4cadc6a9 | 3132 | ); |
7e0d5ad7 | 3133 | |
6eb5f6b9 JH |
3134 | } |
3135 | else { | |
7e0d5ad7 KW |
3136 | if (! prog->anchored_substr) { |
3137 | if (! to_byte_substr(prog)) { | |
6b54ddc5 | 3138 | NON_UTF8_TARGET_BUT_UTF8_REQUIRED(phooey); |
7e0d5ad7 KW |
3139 | } |
3140 | } | |
3141 | ch = SvPVX_const(prog->anchored_substr)[0]; | |
4cadc6a9 | 3142 | REXEC_FBC_SCAN( |
6eb5f6b9 | 3143 | if (*s == ch) { |
a3621e74 | 3144 | DEBUG_EXECUTE_r( did_match = 1 ); |
02d5137b | 3145 | if (regtry(reginfo, &s)) goto got_it; |
6eb5f6b9 JH |
3146 | s++; |
3147 | while (s < strend && *s == ch) | |
3148 | s++; | |
3149 | } | |
4cadc6a9 | 3150 | ); |
6eb5f6b9 | 3151 | } |
a3621e74 | 3152 | DEBUG_EXECUTE_r(if (!did_match) |
bf93d4cc | 3153 | PerlIO_printf(Perl_debug_log, |
b7953727 JH |
3154 | "Did not find anchored character...\n") |
3155 | ); | |
6eb5f6b9 | 3156 | } |
a0714e2c SS |
3157 | else if (prog->anchored_substr != NULL |
3158 | || prog->anchored_utf8 != NULL | |
3159 | || ((prog->float_substr != NULL || prog->float_utf8 != NULL) | |
33b8afdf JH |
3160 | && prog->float_max_offset < strend - s)) { |
3161 | SV *must; | |
ea3daa5d FC |
3162 | SSize_t back_max; |
3163 | SSize_t back_min; | |
33b8afdf | 3164 | char *last; |
6eb5f6b9 | 3165 | char *last1; /* Last position checked before */ |
bf93d4cc GS |
3166 | #ifdef DEBUGGING |
3167 | int did_match = 0; | |
3168 | #endif | |
33b8afdf | 3169 | if (prog->anchored_substr || prog->anchored_utf8) { |
7e0d5ad7 KW |
3170 | if (utf8_target) { |
3171 | if (! prog->anchored_utf8) { | |
3172 | to_utf8_substr(prog); | |
3173 | } | |
3174 | must = prog->anchored_utf8; | |
3175 | } | |
3176 | else { | |
3177 | if (! prog->anchored_substr) { | |
3178 | if (! to_byte_substr(prog)) { | |
6b54ddc5 | 3179 | NON_UTF8_TARGET_BUT_UTF8_REQUIRED(phooey); |
7e0d5ad7 KW |
3180 | } |
3181 | } | |
3182 | must = prog->anchored_substr; | |
3183 | } | |
33b8afdf JH |
3184 | back_max = back_min = prog->anchored_offset; |
3185 | } else { | |
7e0d5ad7 KW |
3186 | if (utf8_target) { |
3187 | if (! prog->float_utf8) { | |
3188 | to_utf8_substr(prog); | |
3189 | } | |
3190 | must = prog->float_utf8; | |
3191 | } | |
3192 | else { | |
3193 | if (! prog->float_substr) { | |
3194 | if (! to_byte_substr(prog)) { | |
6b54ddc5 | 3195 | NON_UTF8_TARGET_BUT_UTF8_REQUIRED(phooey); |
7e0d5ad7 KW |
3196 | } |
3197 | } | |
3198 | must = prog->float_substr; | |
3199 | } | |
33b8afdf JH |
3200 | back_max = prog->float_max_offset; |
3201 | back_min = prog->float_min_offset; | |
3202 | } | |
1de06328 | 3203 | |
1de06328 YO |
3204 | if (back_min<0) { |
3205 | last = strend; | |
3206 | } else { | |
3207 | last = HOP3c(strend, /* Cannot start after this */ | |
ea3daa5d | 3208 | -(SSize_t)(CHR_SVLEN(must) |
1de06328 YO |
3209 | - (SvTAIL(must) != 0) + back_min), strbeg); |
3210 | } | |
9d9163fb | 3211 | if (s > reginfo->strbeg) |
6eb5f6b9 JH |
3212 | last1 = HOPc(s, -1); |
3213 | else | |
3214 | last1 = s - 1; /* bogus */ | |
3215 | ||
a0288114 | 3216 | /* XXXX check_substr already used to find "s", can optimize if |
6eb5f6b9 | 3217 | check_substr==must. */ |
bf05793b | 3218 | dontbother = 0; |
6eb5f6b9 JH |
3219 | strend = HOPc(strend, -dontbother); |
3220 | while ( (s <= last) && | |
e50d57d4 | 3221 | (s = fbm_instr((unsigned char*)HOP4c(s, back_min, strbeg, strend), |
9041c2e3 | 3222 | (unsigned char*)strend, must, |
c33e64f0 | 3223 | multiline ? FBMrf_MULTILINE : 0)) ) { |
a3621e74 | 3224 | DEBUG_EXECUTE_r( did_match = 1 ); |
6eb5f6b9 JH |
3225 | if (HOPc(s, -back_max) > last1) { |
3226 | last1 = HOPc(s, -back_min); | |
3227 | s = HOPc(s, -back_max); | |
3228 | } | |
3229 | else { | |
9d9163fb DM |
3230 | char * const t = (last1 >= reginfo->strbeg) |
3231 | ? HOPc(last1, 1) : last1 + 1; | |
6eb5f6b9 JH |
3232 | |
3233 | last1 = HOPc(s, -back_min); | |
52657f30 | 3234 | s = t; |
6eb5f6b9 | 3235 | } |
f2ed9b32 | 3236 | if (utf8_target) { |
6eb5f6b9 | 3237 | while (s <= last1) { |
02d5137b | 3238 | if (regtry(reginfo, &s)) |
6eb5f6b9 | 3239 | goto got_it; |
7016d6eb DM |
3240 | if (s >= last1) { |
3241 | s++; /* to break out of outer loop */ | |
3242 | break; | |
3243 | } | |
3244 | s += UTF8SKIP(s); | |
6eb5f6b9 JH |
3245 | } |
3246 | } | |
3247 | else { | |
3248 | while (s <= last1) { | |
02d5137b | 3249 | if (regtry(reginfo, &s)) |
6eb5f6b9 JH |
3250 | goto got_it; |
3251 | s++; | |
3252 | } | |
3253 | } | |
3254 | } | |
ab3bbdeb | 3255 | DEBUG_EXECUTE_r(if (!did_match) { |
f2ed9b32 | 3256 | RE_PV_QUOTED_DECL(quoted, utf8_target, PERL_DEBUG_PAD_ZERO(0), |
ab3bbdeb YO |
3257 | SvPVX_const(must), RE_SV_DUMPLEN(must), 30); |
3258 | PerlIO_printf(Perl_debug_log, "Did not find %s substr %s%s...\n", | |
33b8afdf | 3259 | ((must == prog->anchored_substr || must == prog->anchored_utf8) |
bf93d4cc | 3260 | ? "anchored" : "floating"), |
ab3bbdeb YO |
3261 | quoted, RE_SV_TAIL(must)); |
3262 | }); | |
6eb5f6b9 JH |
3263 | goto phooey; |
3264 | } | |
f8fc2ecf | 3265 | else if ( (c = progi->regstclass) ) { |
f14c76ed | 3266 | if (minlen) { |
f8fc2ecf | 3267 | const OPCODE op = OP(progi->regstclass); |
66e933ab | 3268 | /* don't bother with what can't match */ |
786e8c11 | 3269 | if (PL_regkind[op] != EXACT && op != CANY && PL_regkind[op] != TRIE) |
f14c76ed RGS |
3270 | strend = HOPc(strend, -(minlen - 1)); |
3271 | } | |
a3621e74 | 3272 | DEBUG_EXECUTE_r({ |
be8e71aa | 3273 | SV * const prop = sv_newmortal(); |
8b9781c9 | 3274 | regprop(prog, prop, c, reginfo, NULL); |
0df25f3d | 3275 | { |
f2ed9b32 | 3276 | RE_PV_QUOTED_DECL(quoted,utf8_target,PERL_DEBUG_PAD_ZERO(1), |
ab3bbdeb | 3277 | s,strend-s,60); |
0df25f3d | 3278 | PerlIO_printf(Perl_debug_log, |
1c8f8eb1 | 3279 | "Matching stclass %.*s against %s (%d bytes)\n", |
e4f74956 | 3280 | (int)SvCUR(prop), SvPVX_const(prop), |
ab3bbdeb | 3281 | quoted, (int)(strend - s)); |
0df25f3d | 3282 | } |
ffc61ed2 | 3283 | }); |
f9176b44 | 3284 | if (find_byclass(prog, c, s, strend, reginfo)) |
6eb5f6b9 | 3285 | goto got_it; |
07be1b83 | 3286 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Contradicts stclass... [regexec_flags]\n")); |
d6a28714 JH |
3287 | } |
3288 | else { | |
3289 | dontbother = 0; | |
a0714e2c | 3290 | if (prog->float_substr != NULL || prog->float_utf8 != NULL) { |
33b8afdf | 3291 | /* Trim the end. */ |
6af40bd7 | 3292 | char *last= NULL; |
33b8afdf | 3293 | SV* float_real; |
c33e64f0 FC |
3294 | STRLEN len; |
3295 | const char *little; | |
33b8afdf | 3296 | |
7e0d5ad7 KW |
3297 | if (utf8_target) { |
3298 | if (! prog->float_utf8) { | |
3299 | to_utf8_substr(prog); | |
3300 | } | |
3301 | float_real = prog->float_utf8; | |
3302 | } | |
3303 | else { | |
3304 | if (! prog->float_substr) { | |
3305 | if (! to_byte_substr(prog)) { | |
6b54ddc5 | 3306 | NON_UTF8_TARGET_BUT_UTF8_REQUIRED(phooey); |
7e0d5ad7 KW |
3307 | } |
3308 | } | |
3309 | float_real = prog->float_substr; | |
3310 | } | |
d6a28714 | 3311 | |
c33e64f0 FC |
3312 | little = SvPV_const(float_real, len); |
3313 | if (SvTAIL(float_real)) { | |
7f18ad16 KW |
3314 | /* This means that float_real contains an artificial \n on |
3315 | * the end due to the presence of something like this: | |
3316 | * /foo$/ where we can match both "foo" and "foo\n" at the | |
3317 | * end of the string. So we have to compare the end of the | |
3318 | * string first against the float_real without the \n and | |
3319 | * then against the full float_real with the string. We | |
3320 | * have to watch out for cases where the string might be | |
3321 | * smaller than the float_real or the float_real without | |
3322 | * the \n. */ | |
1a13b075 YO |
3323 | char *checkpos= strend - len; |
3324 | DEBUG_OPTIMISE_r( | |
3325 | PerlIO_printf(Perl_debug_log, | |
3326 | "%sChecking for float_real.%s\n", | |
3327 | PL_colors[4], PL_colors[5])); | |
3328 | if (checkpos + 1 < strbeg) { | |
7f18ad16 KW |
3329 | /* can't match, even if we remove the trailing \n |
3330 | * string is too short to match */ | |
1a13b075 YO |
3331 | DEBUG_EXECUTE_r( |
3332 | PerlIO_printf(Perl_debug_log, | |
3333 | "%sString shorter than required trailing substring, cannot match.%s\n", | |
3334 | PL_colors[4], PL_colors[5])); | |
3335 | goto phooey; | |
3336 | } else if (memEQ(checkpos + 1, little, len - 1)) { | |
7f18ad16 KW |
3337 | /* can match, the end of the string matches without the |
3338 | * "\n" */ | |
1a13b075 YO |
3339 | last = checkpos + 1; |
3340 | } else if (checkpos < strbeg) { | |
7f18ad16 KW |
3341 | /* cant match, string is too short when the "\n" is |
3342 | * included */ | |
1a13b075 YO |
3343 | DEBUG_EXECUTE_r( |
3344 | PerlIO_printf(Perl_debug_log, | |
3345 | "%sString does not contain required trailing substring, cannot match.%s\n", | |
3346 | PL_colors[4], PL_colors[5])); | |
3347 | goto phooey; | |
3348 | } else if (!multiline) { | |
7f18ad16 KW |
3349 | /* non multiline match, so compare with the "\n" at the |
3350 | * end of the string */ | |
1a13b075 YO |
3351 | if (memEQ(checkpos, little, len)) { |
3352 | last= checkpos; | |
3353 | } else { | |
3354 | DEBUG_EXECUTE_r( | |
3355 | PerlIO_printf(Perl_debug_log, | |
3356 | "%sString does not contain required trailing substring, cannot match.%s\n", | |
3357 | PL_colors[4], PL_colors[5])); | |
3358 | goto phooey; | |
3359 | } | |
3360 | } else { | |
7f18ad16 KW |
3361 | /* multiline match, so we have to search for a place |
3362 | * where the full string is located */ | |
d6a28714 | 3363 | goto find_last; |
1a13b075 | 3364 | } |
c33e64f0 | 3365 | } else { |
d6a28714 | 3366 | find_last: |
9041c2e3 | 3367 | if (len) |
d6a28714 | 3368 | last = rninstr(s, strend, little, little + len); |
b8c5462f | 3369 | else |
a0288114 | 3370 | last = strend; /* matching "$" */ |
b8c5462f | 3371 | } |
6af40bd7 | 3372 | if (!last) { |
7f18ad16 KW |
3373 | /* at one point this block contained a comment which was |
3374 | * probably incorrect, which said that this was a "should not | |
3375 | * happen" case. Even if it was true when it was written I am | |
3376 | * pretty sure it is not anymore, so I have removed the comment | |
3377 | * and replaced it with this one. Yves */ | |
6bda09f9 YO |
3378 | DEBUG_EXECUTE_r( |
3379 | PerlIO_printf(Perl_debug_log, | |
b729e729 YO |
3380 | "%sString does not contain required substring, cannot match.%s\n", |
3381 | PL_colors[4], PL_colors[5] | |
6af40bd7 YO |
3382 | )); |
3383 | goto phooey; | |
bf93d4cc | 3384 | } |
d6a28714 JH |
3385 | dontbother = strend - last + prog->float_min_offset; |
3386 | } | |
3387 | if (minlen && (dontbother < minlen)) | |
3388 | dontbother = minlen - 1; | |
3389 | strend -= dontbother; /* this one's always in bytes! */ | |
3390 | /* We don't know much -- general case. */ | |
f2ed9b32 | 3391 | if (utf8_target) { |
d6a28714 | 3392 | for (;;) { |
02d5137b | 3393 | if (regtry(reginfo, &s)) |
d6a28714 JH |
3394 | goto got_it; |
3395 | if (s >= strend) | |
3396 | break; | |
b8c5462f | 3397 | s += UTF8SKIP(s); |
d6a28714 JH |
3398 | }; |
3399 | } | |
3400 | else { | |
3401 | do { | |
02d5137b | 3402 | if (regtry(reginfo, &s)) |
d6a28714 JH |
3403 | goto got_it; |
3404 | } while (s++ < strend); | |
3405 | } | |
3406 | } | |
3407 | ||
3408 | /* Failure. */ | |
3409 | goto phooey; | |
3410 | ||
7b52d656 | 3411 | got_it: |
d5e7783a DM |
3412 | /* s/// doesn't like it if $& is earlier than where we asked it to |
3413 | * start searching (which can happen on something like /.\G/) */ | |
3414 | if ( (flags & REXEC_FAIL_ON_UNDERFLOW) | |
3415 | && (prog->offs[0].start < stringarg - strbeg)) | |
3416 | { | |
3417 | /* this should only be possible under \G */ | |
58430ea8 | 3418 | assert(prog->intflags & PREGf_GPOS_SEEN); |
d5e7783a DM |
3419 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, |
3420 | "matched, but failing for REXEC_FAIL_ON_UNDERFLOW\n")); | |
3421 | goto phooey; | |
3422 | } | |
3423 | ||
495f47a5 DM |
3424 | DEBUG_BUFFERS_r( |
3425 | if (swap) | |
3426 | PerlIO_printf(Perl_debug_log, | |
3427 | "rex=0x%"UVxf" freeing offs: 0x%"UVxf"\n", | |
3428 | PTR2UV(prog), | |
3429 | PTR2UV(swap) | |
3430 | ); | |
3431 | ); | |
e9105d30 | 3432 | Safefree(swap); |
d6a28714 | 3433 | |
bf2039a9 DM |
3434 | /* clean up; this will trigger destructors that will free all slabs |
3435 | * above the current one, and cleanup the regmatch_info_aux | |
3436 | * and regmatch_info_aux_eval sructs */ | |
8adc0f72 | 3437 | |
006f26b2 DM |
3438 | LEAVE_SCOPE(oldsave); |
3439 | ||
5daac39c NC |
3440 | if (RXp_PAREN_NAMES(prog)) |
3441 | (void)hv_iterinit(RXp_PAREN_NAMES(prog)); | |
d6a28714 JH |
3442 | |
3443 | /* make sure $`, $&, $', and $digit will work later */ | |
60165aa4 | 3444 | if ( !(flags & REXEC_NOT_FIRST) ) |
749f4950 | 3445 | S_reg_set_capture_string(aTHX_ rx, |
60165aa4 DM |
3446 | strbeg, reginfo->strend, |
3447 | sv, flags, utf8_target); | |
9041c2e3 | 3448 | |
d6a28714 JH |
3449 | return 1; |
3450 | ||
7b52d656 | 3451 | phooey: |
a3621e74 | 3452 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch failed%s\n", |
e4584336 | 3453 | PL_colors[4], PL_colors[5])); |
8adc0f72 | 3454 | |
bf2039a9 DM |
3455 | /* clean up; this will trigger destructors that will free all slabs |
3456 | * above the current one, and cleanup the regmatch_info_aux | |
3457 | * and regmatch_info_aux_eval sructs */ | |
8adc0f72 | 3458 | |
006f26b2 DM |
3459 | LEAVE_SCOPE(oldsave); |
3460 | ||
e9105d30 | 3461 | if (swap) { |
c74340f9 | 3462 | /* we failed :-( roll it back */ |
495f47a5 DM |
3463 | DEBUG_BUFFERS_r(PerlIO_printf(Perl_debug_log, |
3464 | "rex=0x%"UVxf" rolling back offs: freeing=0x%"UVxf" restoring=0x%"UVxf"\n", | |
3465 | PTR2UV(prog), | |
3466 | PTR2UV(prog->offs), | |
3467 | PTR2UV(swap) | |
3468 | )); | |
e9105d30 GG |
3469 | Safefree(prog->offs); |
3470 | prog->offs = swap; | |
3471 | } | |
d6a28714 JH |
3472 | return 0; |
3473 | } | |
3474 | ||
6bda09f9 | 3475 | |
b3d298be | 3476 | /* Set which rex is pointed to by PL_reg_curpm, handling ref counting. |
ec43f78b | 3477 | * Do inc before dec, in case old and new rex are the same */ |
baa60164 | 3478 | #define SET_reg_curpm(Re2) \ |
bf2039a9 | 3479 | if (reginfo->info_aux_eval) { \ |
ec43f78b DM |
3480 | (void)ReREFCNT_inc(Re2); \ |
3481 | ReREFCNT_dec(PM_GETRE(PL_reg_curpm)); \ | |
3482 | PM_SETRE((PL_reg_curpm), (Re2)); \ | |
3483 | } | |
3484 | ||
3485 | ||
d6a28714 JH |
3486 | /* |
3487 | - regtry - try match at specific point | |
3488 | */ | |
3489 | STATIC I32 /* 0 failure, 1 success */ | |
f73aaa43 | 3490 | S_regtry(pTHX_ regmatch_info *reginfo, char **startposp) |
d6a28714 | 3491 | { |
d6a28714 | 3492 | CHECKPOINT lastcp; |
288b8c02 | 3493 | REGEXP *const rx = reginfo->prog; |
8d919b0a | 3494 | regexp *const prog = ReANY(rx); |
99a90e59 | 3495 | SSize_t result; |
f8fc2ecf | 3496 | RXi_GET_DECL(prog,progi); |
a3621e74 | 3497 | GET_RE_DEBUG_FLAGS_DECL; |
7918f24d NC |
3498 | |
3499 | PERL_ARGS_ASSERT_REGTRY; | |
3500 | ||
24b23f37 | 3501 | reginfo->cutpoint=NULL; |
d6a28714 | 3502 | |
9d9163fb | 3503 | prog->offs[0].start = *startposp - reginfo->strbeg; |
d6a28714 | 3504 | prog->lastparen = 0; |
03994de8 | 3505 | prog->lastcloseparen = 0; |
d6a28714 JH |
3506 | |
3507 | /* XXXX What this code is doing here?!!! There should be no need | |
b93070ed | 3508 | to do this again and again, prog->lastparen should take care of |
3dd2943c | 3509 | this! --ilya*/ |
dafc8851 JH |
3510 | |
3511 | /* Tests pat.t#187 and split.t#{13,14} seem to depend on this code. | |
3512 | * Actually, the code in regcppop() (which Ilya may be meaning by | |
b93070ed | 3513 | * prog->lastparen), is not needed at all by the test suite |
225593e1 DM |
3514 | * (op/regexp, op/pat, op/split), but that code is needed otherwise |
3515 | * this erroneously leaves $1 defined: "1" =~ /^(?:(\d)x)?\d$/ | |
3516 | * Meanwhile, this code *is* needed for the | |
daf18116 JH |
3517 | * above-mentioned test suite tests to succeed. The common theme |
3518 | * on those tests seems to be returning null fields from matches. | |
225593e1 | 3519 | * --jhi updated by dapm */ |
dafc8851 | 3520 | #if 1 |
d6a28714 | 3521 | if (prog->nparens) { |
b93070ed | 3522 | regexp_paren_pair *pp = prog->offs; |
eb578fdb | 3523 | I32 i; |
b93070ed | 3524 | for (i = prog->nparens; i > (I32)prog->lastparen; i--) { |
f0ab9afb NC |
3525 | ++pp; |
3526 | pp->start = -1; | |
3527 | pp->end = -1; | |
d6a28714 JH |
3528 | } |
3529 | } | |
dafc8851 | 3530 | #endif |
02db2b7b | 3531 | REGCP_SET(lastcp); |
f73aaa43 DM |
3532 | result = regmatch(reginfo, *startposp, progi->program + 1); |
3533 | if (result != -1) { | |
3534 | prog->offs[0].end = result; | |
d6a28714 JH |
3535 | return 1; |
3536 | } | |
24b23f37 | 3537 | if (reginfo->cutpoint) |
f73aaa43 | 3538 | *startposp= reginfo->cutpoint; |
02db2b7b | 3539 | REGCP_UNWIND(lastcp); |
d6a28714 JH |
3540 | return 0; |
3541 | } | |
3542 | ||
02db2b7b | 3543 | |
8ba1375e MJD |
3544 | #define sayYES goto yes |
3545 | #define sayNO goto no | |
262b90c4 | 3546 | #define sayNO_SILENT goto no_silent |
8ba1375e | 3547 | |
f9f4320a YO |
3548 | /* we dont use STMT_START/END here because it leads to |
3549 | "unreachable code" warnings, which are bogus, but distracting. */ | |
3550 | #define CACHEsayNO \ | |
c476f425 | 3551 | if (ST.cache_mask) \ |
2ac8ff4b | 3552 | reginfo->info_aux->poscache[ST.cache_offset] |= ST.cache_mask; \ |
f9f4320a | 3553 | sayNO |
3298f257 | 3554 | |
a3621e74 | 3555 | /* this is used to determine how far from the left messages like |
265c4333 YO |
3556 | 'failed...' are printed. It should be set such that messages |
3557 | are inline with the regop output that created them. | |
a3621e74 | 3558 | */ |
265c4333 | 3559 | #define REPORT_CODE_OFF 32 |
a3621e74 YO |
3560 | |
3561 | ||
40a82448 DM |
3562 | #define CHRTEST_UNINIT -1001 /* c1/c2 haven't been calculated yet */ |
3563 | #define CHRTEST_VOID -1000 /* the c1/c2 "next char" test should be skipped */ | |
79a2a0e8 KW |
3564 | #define CHRTEST_NOT_A_CP_1 -999 |
3565 | #define CHRTEST_NOT_A_CP_2 -998 | |
9e137952 | 3566 | |
5d9a96ca DM |
3567 | /* grab a new slab and return the first slot in it */ |
3568 | ||
3569 | STATIC regmatch_state * | |
3570 | S_push_slab(pTHX) | |
3571 | { | |
a35a87e7 | 3572 | #if PERL_VERSION < 9 && !defined(PERL_CORE) |
54df2634 NC |
3573 | dMY_CXT; |
3574 | #endif | |
5d9a96ca DM |
3575 | regmatch_slab *s = PL_regmatch_slab->next; |
3576 | if (!s) { | |
3577 | Newx(s, 1, regmatch_slab); | |
3578 | s->prev = PL_regmatch_slab; | |
3579 | s->next = NULL; | |
3580 | PL_regmatch_slab->next = s; | |
3581 | } | |
3582 | PL_regmatch_slab = s; | |
86545054 | 3583 | return SLAB_FIRST(s); |
5d9a96ca | 3584 | } |
5b47454d | 3585 | |
95b24440 | 3586 | |
40a82448 DM |
3587 | /* push a new state then goto it */ |
3588 | ||
4d5016e5 DM |
3589 | #define PUSH_STATE_GOTO(state, node, input) \ |
3590 | pushinput = input; \ | |
40a82448 DM |
3591 | scan = node; \ |
3592 | st->resume_state = state; \ | |
3593 | goto push_state; | |
3594 | ||
3595 | /* push a new state with success backtracking, then goto it */ | |
3596 | ||
4d5016e5 DM |
3597 | #define PUSH_YES_STATE_GOTO(state, node, input) \ |
3598 | pushinput = input; \ | |
40a82448 DM |
3599 | scan = node; \ |
3600 | st->resume_state = state; \ | |
3601 | goto push_yes_state; | |
3602 | ||
aa283a38 | 3603 | |
aa283a38 | 3604 | |
4d5016e5 | 3605 | |
d6a28714 | 3606 | /* |
95b24440 | 3607 | |
bf1f174e DM |
3608 | regmatch() - main matching routine |
3609 | ||
3610 | This is basically one big switch statement in a loop. We execute an op, | |
3611 | set 'next' to point the next op, and continue. If we come to a point which | |
3612 | we may need to backtrack to on failure such as (A|B|C), we push a | |
3613 | backtrack state onto the backtrack stack. On failure, we pop the top | |
3614 | state, and re-enter the loop at the state indicated. If there are no more | |
3615 | states to pop, we return failure. | |
3616 | ||
3617 | Sometimes we also need to backtrack on success; for example /A+/, where | |
3618 | after successfully matching one A, we need to go back and try to | |
3619 | match another one; similarly for lookahead assertions: if the assertion | |
3620 | completes successfully, we backtrack to the state just before the assertion | |
3621 | and then carry on. In these cases, the pushed state is marked as | |
3622 | 'backtrack on success too'. This marking is in fact done by a chain of | |
3623 | pointers, each pointing to the previous 'yes' state. On success, we pop to | |
3624 | the nearest yes state, discarding any intermediate failure-only states. | |
3625 | Sometimes a yes state is pushed just to force some cleanup code to be | |
3626 | called at the end of a successful match or submatch; e.g. (??{$re}) uses | |
3627 | it to free the inner regex. | |
3628 | ||
3629 | Note that failure backtracking rewinds the cursor position, while | |
3630 | success backtracking leaves it alone. | |
3631 | ||
3632 | A pattern is complete when the END op is executed, while a subpattern | |
3633 | such as (?=foo) is complete when the SUCCESS op is executed. Both of these | |
3634 | ops trigger the "pop to last yes state if any, otherwise return true" | |
3635 | behaviour. | |
3636 | ||
3637 | A common convention in this function is to use A and B to refer to the two | |
3638 | subpatterns (or to the first nodes thereof) in patterns like /A*B/: so A is | |
3639 | the subpattern to be matched possibly multiple times, while B is the entire | |
3640 | rest of the pattern. Variable and state names reflect this convention. | |
3641 | ||
3642 | The states in the main switch are the union of ops and failure/success of | |
3643 | substates associated with with that op. For example, IFMATCH is the op | |
3644 | that does lookahead assertions /(?=A)B/ and so the IFMATCH state means | |
3645 | 'execute IFMATCH'; while IFMATCH_A is a state saying that we have just | |
3646 | successfully matched A and IFMATCH_A_fail is a state saying that we have | |
3647 | just failed to match A. Resume states always come in pairs. The backtrack | |
3648 | state we push is marked as 'IFMATCH_A', but when that is popped, we resume | |
3649 | at IFMATCH_A or IFMATCH_A_fail, depending on whether we are backtracking | |
3650 | on success or failure. | |
3651 | ||
3652 | The struct that holds a backtracking state is actually a big union, with | |
3653 | one variant for each major type of op. The variable st points to the | |
3654 | top-most backtrack struct. To make the code clearer, within each | |
3655 | block of code we #define ST to alias the relevant union. | |
3656 | ||
3657 | Here's a concrete example of a (vastly oversimplified) IFMATCH | |
3658 | implementation: | |
3659 | ||
3660 | switch (state) { | |
3661 | .... | |
3662 | ||
3663 | #define ST st->u.ifmatch | |
3664 | ||
3665 | case IFMATCH: // we are executing the IFMATCH op, (?=A)B | |
3666 | ST.foo = ...; // some state we wish to save | |
95b24440 | 3667 | ... |
bf1f174e DM |
3668 | // push a yes backtrack state with a resume value of |
3669 | // IFMATCH_A/IFMATCH_A_fail, then continue execution at the | |
3670 | // first node of A: | |
4d5016e5 | 3671 | PUSH_YES_STATE_GOTO(IFMATCH_A, A, newinput); |
bf1f174e DM |
3672 | // NOTREACHED |
3673 | ||
3674 | case IFMATCH_A: // we have successfully executed A; now continue with B | |
3675 | next = B; | |
3676 | bar = ST.foo; // do something with the preserved value | |
3677 | break; | |
3678 | ||
3679 | case IFMATCH_A_fail: // A failed, so the assertion failed | |
3680 | ...; // do some housekeeping, then ... | |
3681 | sayNO; // propagate the failure | |
3682 | ||
3683 | #undef ST | |
95b24440 | 3684 | |
bf1f174e DM |
3685 | ... |
3686 | } | |
95b24440 | 3687 | |
bf1f174e DM |
3688 | For any old-timers reading this who are familiar with the old recursive |
3689 | approach, the code above is equivalent to: | |
95b24440 | 3690 | |
bf1f174e DM |
3691 | case IFMATCH: // we are executing the IFMATCH op, (?=A)B |
3692 | { | |
3693 | int foo = ... | |
95b24440 | 3694 | ... |
bf1f174e DM |
3695 | if (regmatch(A)) { |
3696 | next = B; | |
3697 | bar = foo; | |
3698 | break; | |
95b24440 | 3699 | } |
bf1f174e DM |
3700 | ...; // do some housekeeping, then ... |
3701 | sayNO; // propagate the failure | |
95b24440 | 3702 | } |
bf1f174e DM |
3703 | |
3704 | The topmost backtrack state, pointed to by st, is usually free. If you | |
3705 | want to claim it, populate any ST.foo fields in it with values you wish to | |
3706 | save, then do one of | |
3707 | ||
4d5016e5 DM |
3708 | PUSH_STATE_GOTO(resume_state, node, newinput); |
3709 | PUSH_YES_STATE_GOTO(resume_state, node, newinput); | |
bf1f174e DM |
3710 | |
3711 | which sets that backtrack state's resume value to 'resume_state', pushes a | |
3712 | new free entry to the top of the backtrack stack, then goes to 'node'. | |
3713 | On backtracking, the free slot is popped, and the saved state becomes the | |
3714 | new free state. An ST.foo field in this new top state can be temporarily | |
3715 | accessed to retrieve values, but once the main loop is re-entered, it | |
3716 | becomes available for reuse. | |
3717 | ||
3718 | Note that the depth of the backtrack stack constantly increases during the | |
3719 | left-to-right execution of the pattern, rather than going up and down with | |
3720 | the pattern nesting. For example the stack is at its maximum at Z at the | |
3721 | end of the pattern, rather than at X in the following: | |
3722 | ||
3723 | /(((X)+)+)+....(Y)+....Z/ | |
3724 | ||
3725 | The only exceptions to this are lookahead/behind assertions and the cut, | |
3726 | (?>A), which pop all the backtrack states associated with A before | |
3727 | continuing. | |
3728 | ||
486ec47a | 3729 | Backtrack state structs are allocated in slabs of about 4K in size. |
bf1f174e DM |
3730 | PL_regmatch_state and st always point to the currently active state, |
3731 | and PL_regmatch_slab points to the slab currently containing | |
3732 | PL_regmatch_state. The first time regmatch() is called, the first slab is | |
3733 | allocated, and is never freed until interpreter destruction. When the slab | |
3734 | is full, a new one is allocated and chained to the end. At exit from | |
3735 | regmatch(), slabs allocated since entry are freed. | |
3736 | ||
3737 | */ | |
95b24440 | 3738 | |
40a82448 | 3739 | |
5bc10b2c | 3740 | #define DEBUG_STATE_pp(pp) \ |
265c4333 | 3741 | DEBUG_STATE_r({ \ |
baa60164 | 3742 | DUMP_EXEC_POS(locinput, scan, utf8_target); \ |
5bc10b2c | 3743 | PerlIO_printf(Perl_debug_log, \ |
5d458dd8 | 3744 | " %*s"pp" %s%s%s%s%s\n", \ |
5bc10b2c | 3745 | depth*2, "", \ |
baa60164 | 3746 | PL_reg_name[st->resume_state], \ |
5d458dd8 YO |
3747 | ((st==yes_state||st==mark_state) ? "[" : ""), \ |
3748 | ((st==yes_state) ? "Y" : ""), \ | |
3749 | ((st==mark_state) ? "M" : ""), \ | |
3750 | ((st==yes_state||st==mark_state) ? "]" : "") \ | |
3751 | ); \ | |
265c4333 | 3752 | }); |
5bc10b2c | 3753 | |
40a82448 | 3754 | |
3dab1dad | 3755 | #define REG_NODE_NUM(x) ((x) ? (int)((x)-prog) : -1) |
95b24440 | 3756 | |
3df15adc | 3757 | #ifdef DEBUGGING |
5bc10b2c | 3758 | |
ab3bbdeb | 3759 | STATIC void |
f2ed9b32 | 3760 | S_debug_start_match(pTHX_ const REGEXP *prog, const bool utf8_target, |
ab3bbdeb YO |
3761 | const char *start, const char *end, const char *blurb) |
3762 | { | |
efd26800 | 3763 | const bool utf8_pat = RX_UTF8(prog) ? 1 : 0; |
7918f24d NC |
3764 | |
3765 | PERL_ARGS_ASSERT_DEBUG_START_MATCH; | |
3766 | ||
ab3bbdeb YO |
3767 | if (!PL_colorset) |
3768 | reginitcolors(); | |
3769 | { | |
3770 | RE_PV_QUOTED_DECL(s0, utf8_pat, PERL_DEBUG_PAD_ZERO(0), | |
d2c6dc5e | 3771 | RX_PRECOMP_const(prog), RX_PRELEN(prog), 60); |
ab3bbdeb | 3772 | |
f2ed9b32 | 3773 | RE_PV_QUOTED_DECL(s1, utf8_target, PERL_DEBUG_PAD_ZERO(1), |
ab3bbdeb YO |
3774 | start, end - start, 60); |
3775 | ||
3776 | PerlIO_printf(Perl_debug_log, | |
3777 | "%s%s REx%s %s against %s\n", | |
3778 | PL_colors[4], blurb, PL_colors[5], s0, s1); | |
3779 | ||
f2ed9b32 | 3780 | if (utf8_target||utf8_pat) |
1de06328 YO |
3781 | PerlIO_printf(Perl_debug_log, "UTF-8 %s%s%s...\n", |
3782 | utf8_pat ? "pattern" : "", | |
f2ed9b32 KW |
3783 | utf8_pat && utf8_target ? " and " : "", |
3784 | utf8_target ? "string" : "" | |
ab3bbdeb YO |
3785 | ); |
3786 | } | |
3787 | } | |
3df15adc YO |
3788 | |
3789 | STATIC void | |
786e8c11 YO |
3790 | S_dump_exec_pos(pTHX_ const char *locinput, |
3791 | const regnode *scan, | |
3792 | const char *loc_regeol, | |
3793 | const char *loc_bostr, | |
3794 | const char *loc_reg_starttry, | |
f2ed9b32 | 3795 | const bool utf8_target) |
07be1b83 | 3796 | { |
786e8c11 | 3797 | const int docolor = *PL_colors[0] || *PL_colors[2] || *PL_colors[4]; |
07be1b83 | 3798 | const int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */ |
786e8c11 | 3799 | int l = (loc_regeol - locinput) > taill ? taill : (loc_regeol - locinput); |
07be1b83 YO |
3800 | /* The part of the string before starttry has one color |
3801 | (pref0_len chars), between starttry and current | |
3802 | position another one (pref_len - pref0_len chars), | |
3803 | after the current position the third one. | |
3804 | We assume that pref0_len <= pref_len, otherwise we | |
3805 | decrease pref0_len. */ | |
786e8c11 YO |
3806 | int pref_len = (locinput - loc_bostr) > (5 + taill) - l |
3807 | ? (5 + taill) - l : locinput - loc_bostr; | |
07be1b83 YO |
3808 | int pref0_len; |
3809 | ||
7918f24d NC |
3810 | PERL_ARGS_ASSERT_DUMP_EXEC_POS; |
3811 | ||
f2ed9b32 | 3812 | while (utf8_target && UTF8_IS_CONTINUATION(*(U8*)(locinput - pref_len))) |
07be1b83 | 3813 | pref_len++; |
786e8c11 YO |
3814 | pref0_len = pref_len - (locinput - loc_reg_starttry); |
3815 | if (l + pref_len < (5 + taill) && l < loc_regeol - locinput) | |
3816 | l = ( loc_regeol - locinput > (5 + taill) - pref_len | |
3817 | ? (5 + taill) - pref_len : loc_regeol - locinput); | |
f2ed9b32 | 3818 | while (utf8_target && UTF8_IS_CONTINUATION(*(U8*)(locinput + l))) |
07be1b83 YO |
3819 | l--; |
3820 | if (pref0_len < 0) | |
3821 | pref0_len = 0; | |
3822 | if (pref0_len > pref_len) | |
3823 | pref0_len = pref_len; | |
3824 | { | |
f2ed9b32 | 3825 | const int is_uni = (utf8_target && OP(scan) != CANY) ? 1 : 0; |
0df25f3d | 3826 | |
ab3bbdeb | 3827 | RE_PV_COLOR_DECL(s0,len0,is_uni,PERL_DEBUG_PAD(0), |
1de06328 | 3828 | (locinput - pref_len),pref0_len, 60, 4, 5); |
0df25f3d | 3829 | |
ab3bbdeb | 3830 | RE_PV_COLOR_DECL(s1,len1,is_uni,PERL_DEBUG_PAD(1), |
3df15adc | 3831 | (locinput - pref_len + pref0_len), |
1de06328 | 3832 | pref_len - pref0_len, 60, 2, 3); |
0df25f3d | 3833 | |
ab3bbdeb | 3834 | RE_PV_COLOR_DECL(s2,len2,is_uni,PERL_DEBUG_PAD(2), |
1de06328 | 3835 | locinput, loc_regeol - locinput, 10, 0, 1); |
0df25f3d | 3836 | |
1de06328 | 3837 | const STRLEN tlen=len0+len1+len2; |
3df15adc | 3838 | PerlIO_printf(Perl_debug_log, |
ab3bbdeb | 3839 | "%4"IVdf" <%.*s%.*s%s%.*s>%*s|", |
786e8c11 | 3840 | (IV)(locinput - loc_bostr), |
07be1b83 | 3841 | len0, s0, |
07be1b83 | 3842 | len1, s1, |
07be1b83 | 3843 | (docolor ? "" : "> <"), |
07be1b83 | 3844 | len2, s2, |
f9f4320a | 3845 | (int)(tlen > 19 ? 0 : 19 - tlen), |
07be1b83 YO |
3846 | ""); |
3847 | } | |
3848 | } | |
3df15adc | 3849 | |
07be1b83 YO |
3850 | #endif |
3851 | ||
0a4db386 YO |
3852 | /* reg_check_named_buff_matched() |
3853 | * Checks to see if a named buffer has matched. The data array of | |
3854 | * buffer numbers corresponding to the buffer is expected to reside | |
3855 | * in the regexp->data->data array in the slot stored in the ARG() of | |
3856 | * node involved. Note that this routine doesn't actually care about the | |
3857 | * name, that information is not preserved from compilation to execution. | |
3858 | * Returns the index of the leftmost defined buffer with the given name | |
3859 | * or 0 if non of the buffers matched. | |
3860 | */ | |
3861 | STATIC I32 | |
dc3bf405 | 3862 | S_reg_check_named_buff_matched(const regexp *rex, const regnode *scan) |
7918f24d | 3863 | { |
0a4db386 | 3864 | I32 n; |
f8fc2ecf | 3865 | RXi_GET_DECL(rex,rexi); |
ad64d0ec | 3866 | SV *sv_dat= MUTABLE_SV(rexi->data->data[ ARG( scan ) ]); |
0a4db386 | 3867 | I32 *nums=(I32*)SvPVX(sv_dat); |
7918f24d NC |
3868 | |
3869 | PERL_ARGS_ASSERT_REG_CHECK_NAMED_BUFF_MATCHED; | |
3870 | ||
0a4db386 | 3871 | for ( n=0; n<SvIVX(sv_dat); n++ ) { |
b93070ed DM |
3872 | if ((I32)rex->lastparen >= nums[n] && |
3873 | rex->offs[nums[n]].end != -1) | |
0a4db386 YO |
3874 | { |
3875 | return nums[n]; | |
3876 | } | |
3877 | } | |
3878 | return 0; | |
3879 | } | |
3880 | ||
2f554ef7 | 3881 | |
c74f6de9 | 3882 | static bool |
984e6dd1 | 3883 | S_setup_EXACTISH_ST_c1_c2(pTHX_ const regnode * const text_node, int *c1p, |
aed7b151 | 3884 | U8* c1_utf8, int *c2p, U8* c2_utf8, regmatch_info *reginfo) |
c74f6de9 | 3885 | { |
79a2a0e8 KW |
3886 | /* This function determines if there are one or two characters that match |
3887 | * the first character of the passed-in EXACTish node <text_node>, and if | |
3888 | * so, returns them in the passed-in pointers. | |
c74f6de9 | 3889 | * |
79a2a0e8 KW |
3890 | * If it determines that no possible character in the target string can |
3891 | * match, it returns FALSE; otherwise TRUE. (The FALSE situation occurs if | |
3892 | * the first character in <text_node> requires UTF-8 to represent, and the | |
3893 | * target string isn't in UTF-8.) | |
c74f6de9 | 3894 | * |
79a2a0e8 KW |
3895 | * If there are more than two characters that could match the beginning of |
3896 | * <text_node>, or if more context is required to determine a match or not, | |
3897 | * it sets both *<c1p> and *<c2p> to CHRTEST_VOID. | |
3898 | * | |
3899 | * The motiviation behind this function is to allow the caller to set up | |
3900 | * tight loops for matching. If <text_node> is of type EXACT, there is | |
3901 | * only one possible character that can match its first character, and so | |
3902 | * the situation is quite simple. But things get much more complicated if | |
3903 | * folding is involved. It may be that the first character of an EXACTFish | |
3904 | * node doesn't participate in any possible fold, e.g., punctuation, so it | |
3905 | * can be matched only by itself. The vast majority of characters that are | |
3906 | * in folds match just two things, their lower and upper-case equivalents. | |
3907 | * But not all are like that; some have multiple possible matches, or match | |
3908 | * sequences of more than one character. This function sorts all that out. | |
3909 | * | |
3910 | * Consider the patterns A*B or A*?B where A and B are arbitrary. In a | |
3911 | * loop of trying to match A*, we know we can't exit where the thing | |
3912 | * following it isn't a B. And something can't be a B unless it is the | |
3913 | * beginning of B. By putting a quick test for that beginning in a tight | |
3914 | * loop, we can rule out things that can't possibly be B without having to | |
3915 | * break out of the loop, thus avoiding work. Similarly, if A is a single | |
3916 | * character, we can make a tight loop matching A*, using the outputs of | |
3917 | * this function. | |
3918 | * | |
3919 | * If the target string to match isn't in UTF-8, and there aren't | |
3920 | * complications which require CHRTEST_VOID, *<c1p> and *<c2p> are set to | |
3921 | * the one or two possible octets (which are characters in this situation) | |
3922 | * that can match. In all cases, if there is only one character that can | |
3923 | * match, *<c1p> and *<c2p> will be identical. | |
3924 | * | |
3925 | * If the target string is in UTF-8, the buffers pointed to by <c1_utf8> | |
3926 | * and <c2_utf8> will contain the one or two UTF-8 sequences of bytes that | |
3927 | * can match the beginning of <text_node>. They should be declared with at | |
3928 | * least length UTF8_MAXBYTES+1. (If the target string isn't in UTF-8, it is | |
3929 | * undefined what these contain.) If one or both of the buffers are | |
3930 | * invariant under UTF-8, *<c1p>, and *<c2p> will also be set to the | |
3931 | * corresponding invariant. If variant, the corresponding *<c1p> and/or | |
3932 | * *<c2p> will be set to a negative number(s) that shouldn't match any code | |
3933 | * point (unless inappropriately coerced to unsigned). *<c1p> will equal | |
3934 | * *<c2p> if and only if <c1_utf8> and <c2_utf8> are the same. */ | |
c74f6de9 | 3935 | |
ba44c216 | 3936 | const bool utf8_target = reginfo->is_utf8_target; |
79a2a0e8 | 3937 | |
9cd990bf JH |
3938 | UV c1 = (UV)CHRTEST_NOT_A_CP_1; |
3939 | UV c2 = (UV)CHRTEST_NOT_A_CP_2; | |
79a2a0e8 | 3940 | bool use_chrtest_void = FALSE; |
aed7b151 | 3941 | const bool is_utf8_pat = reginfo->is_utf8_pat; |
79a2a0e8 KW |
3942 | |
3943 | /* Used when we have both utf8 input and utf8 output, to avoid converting | |
3944 | * to/from code points */ | |
3945 | bool utf8_has_been_setup = FALSE; | |
3946 | ||
c74f6de9 KW |
3947 | dVAR; |
3948 | ||
b4291290 | 3949 | U8 *pat = (U8*)STRING(text_node); |
a6715020 | 3950 | U8 folded[UTF8_MAX_FOLD_CHAR_EXPAND * UTF8_MAXBYTES_CASE + 1] = { '\0' }; |
c74f6de9 | 3951 | |
a4525e78 | 3952 | if (OP(text_node) == EXACT || OP(text_node) == EXACTL) { |
79a2a0e8 KW |
3953 | |
3954 | /* In an exact node, only one thing can be matched, that first | |
3955 | * character. If both the pat and the target are UTF-8, we can just | |
3956 | * copy the input to the output, avoiding finding the code point of | |
3957 | * that character */ | |
984e6dd1 | 3958 | if (!is_utf8_pat) { |
79a2a0e8 KW |
3959 | c2 = c1 = *pat; |
3960 | } | |
3961 | else if (utf8_target) { | |
3962 | Copy(pat, c1_utf8, UTF8SKIP(pat), U8); | |
3963 | Copy(pat, c2_utf8, UTF8SKIP(pat), U8); | |
3964 | utf8_has_been_setup = TRUE; | |
3965 | } | |
3966 | else { | |
3967 | c2 = c1 = valid_utf8_to_uvchr(pat, NULL); | |
c74f6de9 | 3968 | } |
79a2a0e8 | 3969 | } |
31f05a37 KW |
3970 | else { /* an EXACTFish node */ |
3971 | U8 *pat_end = pat + STR_LEN(text_node); | |
3972 | ||
3973 | /* An EXACTFL node has at least some characters unfolded, because what | |
3974 | * they match is not known until now. So, now is the time to fold | |
3975 | * the first few of them, as many as are needed to determine 'c1' and | |
3976 | * 'c2' later in the routine. If the pattern isn't UTF-8, we only need | |
3977 | * to fold if in a UTF-8 locale, and then only the Sharp S; everything | |
3978 | * else is 1-1 and isn't assumed to be folded. In a UTF-8 pattern, we | |
3979 | * need to fold as many characters as a single character can fold to, | |
3980 | * so that later we can check if the first ones are such a multi-char | |
3981 | * fold. But, in such a pattern only locale-problematic characters | |
3982 | * aren't folded, so we can skip this completely if the first character | |
3983 | * in the node isn't one of the tricky ones */ | |
3984 | if (OP(text_node) == EXACTFL) { | |
3985 | ||
3986 | if (! is_utf8_pat) { | |
3987 | if (IN_UTF8_CTYPE_LOCALE && *pat == LATIN_SMALL_LETTER_SHARP_S) | |
3988 | { | |
3989 | folded[0] = folded[1] = 's'; | |
3990 | pat = folded; | |
3991 | pat_end = folded + 2; | |
3992 | } | |
3993 | } | |
3994 | else if (is_PROBLEMATIC_LOCALE_FOLDEDS_START_utf8(pat)) { | |
3995 | U8 *s = pat; | |
3996 | U8 *d = folded; | |
3997 | int i; | |
3998 | ||
3999 | for (i = 0; i < UTF8_MAX_FOLD_CHAR_EXPAND && s < pat_end; i++) { | |
4000 | if (isASCII(*s)) { | |
4001 | *(d++) = (U8) toFOLD_LC(*s); | |
4002 | s++; | |
4003 | } | |
4004 | else { | |
4005 | STRLEN len; | |
4006 | _to_utf8_fold_flags(s, | |
4007 | d, | |
4008 | &len, | |
4009 | FOLD_FLAGS_FULL | FOLD_FLAGS_LOCALE); | |
4010 | d += len; | |
4011 | s += UTF8SKIP(s); | |
4012 | } | |
4013 | } | |
4014 | ||
4015 | pat = folded; | |
4016 | pat_end = d; | |
4017 | } | |
4018 | } | |
4019 | ||
251b239f KW |
4020 | if ((is_utf8_pat && is_MULTI_CHAR_FOLD_utf8_safe(pat, pat_end)) |
4021 | || (!is_utf8_pat && is_MULTI_CHAR_FOLD_latin1_safe(pat, pat_end))) | |
baa60164 KW |
4022 | { |
4023 | /* Multi-character folds require more context to sort out. Also | |
4024 | * PL_utf8_foldclosures used below doesn't handle them, so have to | |
4025 | * be handled outside this routine */ | |
4026 | use_chrtest_void = TRUE; | |
4027 | } | |
4028 | else { /* an EXACTFish node which doesn't begin with a multi-char fold */ | |
4029 | c1 = is_utf8_pat ? valid_utf8_to_uvchr(pat, NULL) : *pat; | |
e8ea8356 | 4030 | if (c1 > 255) { |
baa60164 KW |
4031 | /* Load the folds hash, if not already done */ |
4032 | SV** listp; | |
4033 | if (! PL_utf8_foldclosures) { | |
31667e6b | 4034 | _load_PL_utf8_foldclosures(); |
79a2a0e8 | 4035 | } |
79a2a0e8 | 4036 | |
baa60164 KW |
4037 | /* The fold closures data structure is a hash with the keys |
4038 | * being the UTF-8 of every character that is folded to, like | |
4039 | * 'k', and the values each an array of all code points that | |
4040 | * fold to its key. e.g. [ 'k', 'K', KELVIN_SIGN ]. | |
4041 | * Multi-character folds are not included */ | |
4042 | if ((! (listp = hv_fetch(PL_utf8_foldclosures, | |
4043 | (char *) pat, | |
4044 | UTF8SKIP(pat), | |
4045 | FALSE)))) | |
4046 | { | |
4047 | /* Not found in the hash, therefore there are no folds | |
4048 | * containing it, so there is only a single character that | |
4049 | * could match */ | |
4050 | c2 = c1; | |
79a2a0e8 | 4051 | } |
baa60164 KW |
4052 | else { /* Does participate in folds */ |
4053 | AV* list = (AV*) *listp; | |
b9f2b683 | 4054 | if (av_tindex(list) != 1) { |
79a2a0e8 | 4055 | |
baa60164 KW |
4056 | /* If there aren't exactly two folds to this, it is |
4057 | * outside the scope of this function */ | |
4058 | use_chrtest_void = TRUE; | |
79a2a0e8 | 4059 | } |
baa60164 KW |
4060 | else { /* There are two. Get them */ |
4061 | SV** c_p = av_fetch(list, 0, FALSE); | |
4062 | if (c_p == NULL) { | |
4063 | Perl_croak(aTHX_ "panic: invalid PL_utf8_foldclosures structure"); | |
4064 | } | |
4065 | c1 = SvUV(*c_p); | |
4066 | ||
4067 | c_p = av_fetch(list, 1, FALSE); | |
4068 | if (c_p == NULL) { | |
4069 | Perl_croak(aTHX_ "panic: invalid PL_utf8_foldclosures structure"); | |
4070 | } | |
4071 | c2 = SvUV(*c_p); | |
4072 | ||
4073 | /* Folds that cross the 255/256 boundary are forbidden | |
4074 | * if EXACTFL (and isnt a UTF8 locale), or EXACTFA and | |
4075 | * one is ASCIII. Since the pattern character is above | |
e8ea8356 | 4076 | * 255, and its only other match is below 256, the only |
baa60164 KW |
4077 | * legal match will be to itself. We have thrown away |
4078 | * the original, so have to compute which is the one | |
e8ea8356 | 4079 | * above 255. */ |
baa60164 KW |
4080 | if ((c1 < 256) != (c2 < 256)) { |
4081 | if ((OP(text_node) == EXACTFL | |
4082 | && ! IN_UTF8_CTYPE_LOCALE) | |
4083 | || ((OP(text_node) == EXACTFA | |
4084 | || OP(text_node) == EXACTFA_NO_TRIE) | |
4085 | && (isASCII(c1) || isASCII(c2)))) | |
4086 | { | |
4087 | if (c1 < 256) { | |
4088 | c1 = c2; | |
4089 | } | |
4090 | else { | |
4091 | c2 = c1; | |
4092 | } | |
79a2a0e8 KW |
4093 | } |
4094 | } | |
4095 | } | |
4096 | } | |
4097 | } | |
e8ea8356 | 4098 | else /* Here, c1 is <= 255 */ |
baa60164 KW |
4099 | if (utf8_target |
4100 | && HAS_NONLATIN1_FOLD_CLOSURE(c1) | |
4101 | && ( ! (OP(text_node) == EXACTFL && ! IN_UTF8_CTYPE_LOCALE)) | |
4102 | && ((OP(text_node) != EXACTFA | |
4103 | && OP(text_node) != EXACTFA_NO_TRIE) | |
4104 | || ! isASCII(c1))) | |
4105 | { | |
4106 | /* Here, there could be something above Latin1 in the target | |
4107 | * which folds to this character in the pattern. All such | |
4108 | * cases except LATIN SMALL LETTER Y WITH DIAERESIS have more | |
4109 | * than two characters involved in their folds, so are outside | |
4110 | * the scope of this function */ | |
4111 | if (UNLIKELY(c1 == LATIN_SMALL_LETTER_Y_WITH_DIAERESIS)) { | |
4112 | c2 = LATIN_CAPITAL_LETTER_Y_WITH_DIAERESIS; | |
4113 | } | |
4114 | else { | |
4115 | use_chrtest_void = TRUE; | |
4116 | } | |
79a2a0e8 | 4117 | } |
baa60164 KW |
4118 | else { /* Here nothing above Latin1 can fold to the pattern |
4119 | character */ | |
4120 | switch (OP(text_node)) { | |
c74f6de9 | 4121 | |
baa60164 KW |
4122 | case EXACTFL: /* /l rules */ |
4123 | c2 = PL_fold_locale[c1]; | |
4124 | break; | |
c74f6de9 | 4125 | |
baa60164 KW |
4126 | case EXACTF: /* This node only generated for non-utf8 |
4127 | patterns */ | |
4128 | assert(! is_utf8_pat); | |
4129 | if (! utf8_target) { /* /d rules */ | |
4130 | c2 = PL_fold[c1]; | |
4131 | break; | |
4132 | } | |
4133 | /* FALLTHROUGH */ | |
4134 | /* /u rules for all these. This happens to work for | |
4135 | * EXACTFA as nothing in Latin1 folds to ASCII */ | |
4136 | case EXACTFA_NO_TRIE: /* This node only generated for | |
4137 | non-utf8 patterns */ | |
4138 | assert(! is_utf8_pat); | |
924ba076 | 4139 | /* FALLTHROUGH */ |
baa60164 KW |
4140 | case EXACTFA: |
4141 | case EXACTFU_SS: | |
4142 | case EXACTFU: | |
4143 | c2 = PL_fold_latin1[c1]; | |
c74f6de9 | 4144 | break; |
c74f6de9 | 4145 | |
baa60164 KW |
4146 | default: |
4147 | Perl_croak(aTHX_ "panic: Unexpected op %u", OP(text_node)); | |
e5964223 | 4148 | NOT_REACHED; /* NOTREACHED */ |
baa60164 | 4149 | } |
c74f6de9 KW |
4150 | } |
4151 | } | |
4152 | } | |
79a2a0e8 KW |
4153 | |
4154 | /* Here have figured things out. Set up the returns */ | |
4155 | if (use_chrtest_void) { | |
4156 | *c2p = *c1p = CHRTEST_VOID; | |
4157 | } | |
4158 | else if (utf8_target) { | |
4159 | if (! utf8_has_been_setup) { /* Don't have the utf8; must get it */ | |
4160 | uvchr_to_utf8(c1_utf8, c1); | |
4161 | uvchr_to_utf8(c2_utf8, c2); | |
c74f6de9 | 4162 | } |
c74f6de9 | 4163 | |
79a2a0e8 KW |
4164 | /* Invariants are stored in both the utf8 and byte outputs; Use |
4165 | * negative numbers otherwise for the byte ones. Make sure that the | |
4166 | * byte ones are the same iff the utf8 ones are the same */ | |
4167 | *c1p = (UTF8_IS_INVARIANT(*c1_utf8)) ? *c1_utf8 : CHRTEST_NOT_A_CP_1; | |
4168 | *c2p = (UTF8_IS_INVARIANT(*c2_utf8)) | |
4169 | ? *c2_utf8 | |
4170 | : (c1 == c2) | |
4171 | ? CHRTEST_NOT_A_CP_1 | |
4172 | : CHRTEST_NOT_A_CP_2; | |
4173 | } | |
4174 | else if (c1 > 255) { | |
4175 | if (c2 > 255) { /* both possibilities are above what a non-utf8 string | |
4176 | can represent */ | |
4177 | return FALSE; | |
4178 | } | |
c74f6de9 | 4179 | |
79a2a0e8 KW |
4180 | *c1p = *c2p = c2; /* c2 is the only representable value */ |
4181 | } | |
4182 | else { /* c1 is representable; see about c2 */ | |
4183 | *c1p = c1; | |
4184 | *c2p = (c2 < 256) ? c2 : c1; | |
c74f6de9 | 4185 | } |
2f554ef7 | 4186 | |
c74f6de9 KW |
4187 | return TRUE; |
4188 | } | |
2f554ef7 | 4189 | |
64935bc6 KW |
4190 | /* This creates a single number by combining two, with 'before' being like the |
4191 | * 10's digit, but this isn't necessarily base 10; it is base however many | |
4192 | * elements of the enum there are */ | |
85e5f08b | 4193 | #define GCBcase(before, after) ((GCB_ENUM_COUNT * before) + after) |
64935bc6 KW |
4194 | |
4195 | STATIC bool | |
85e5f08b | 4196 | S_isGCB(const GCB_enum before, const GCB_enum after) |
64935bc6 KW |
4197 | { |
4198 | /* returns a boolean indicating if there is a Grapheme Cluster Boundary | |
4199 | * between the inputs. See http://www.unicode.org/reports/tr29/ */ | |
4200 | ||
4201 | switch (GCBcase(before, after)) { | |
4202 | ||
4203 | /* Break at the start and end of text. | |
4204 | GB1. sot ÷ | |
4205 | GB2. ÷ eot | |
4206 | ||
4207 | Break before and after controls except between CR and LF | |
4208 | GB4. ( Control | CR | LF ) ÷ | |
4209 | GB5. ÷ ( Control | CR | LF ) | |
4210 | ||
4211 | Otherwise, break everywhere. | |
4212 | GB10. Any ÷ Any */ | |
4213 | default: | |
4214 | return TRUE; | |
4215 | ||
4216 | /* Do not break between a CR and LF. | |
4217 | GB3. CR × LF */ | |
85e5f08b | 4218 | case GCBcase(GCB_CR, GCB_LF): |
64935bc6 KW |
4219 | return FALSE; |
4220 | ||
4221 | /* Do not break Hangul syllable sequences. | |
4222 | GB6. L × ( L | V | LV | LVT ) */ | |
85e5f08b KW |
4223 | case GCBcase(GCB_L, GCB_L): |
4224 | case GCBcase(GCB_L, GCB_V): | |
4225 | case GCBcase(GCB_L, GCB_LV): | |
4226 | case GCBcase(GCB_L, GCB_LVT): | |
64935bc6 KW |
4227 | return FALSE; |
4228 | ||
4229 | /* GB7. ( LV | V ) × ( V | T ) */ | |
85e5f08b KW |
4230 | case GCBcase(GCB_LV, GCB_V): |
4231 | case GCBcase(GCB_LV, GCB_T): | |
4232 | case GCBcase(GCB_V, GCB_V): | |
4233 | case GCBcase(GCB_V, GCB_T): | |
64935bc6 KW |
4234 | return FALSE; |
4235 | ||
4236 | /* GB8. ( LVT | T) × T */ | |
85e5f08b KW |
4237 | case GCBcase(GCB_LVT, GCB_T): |
4238 | case GCBcase(GCB_T, GCB_T): | |
64935bc6 KW |
4239 | return FALSE; |
4240 | ||
4241 | /* Do not break between regional indicator symbols. | |
4242 | GB8a. Regional_Indicator × Regional_Indicator */ | |
85e5f08b | 4243 | case GCBcase(GCB_Regional_Indicator, GCB_Regional_Indicator): |
64935bc6 KW |
4244 | return FALSE; |
4245 | ||
4246 | /* Do not break before extending characters. | |
4247 | GB9. × Extend */ | |
85e5f08b KW |
4248 | case GCBcase(GCB_Other, GCB_Extend): |
4249 | case GCBcase(GCB_Extend, GCB_Extend): | |
4250 | case GCBcase(GCB_L, GCB_Extend): | |
4251 | case GCBcase(GCB_LV, GCB_Extend): | |
4252 | case GCBcase(GCB_LVT, GCB_Extend): | |
4253 | case GCBcase(GCB_Prepend, GCB_Extend): | |
4254 | case GCBcase(GCB_Regional_Indicator, GCB_Extend): | |
4255 | case GCBcase(GCB_SpacingMark, GCB_Extend): | |
4256 | case GCBcase(GCB_T, GCB_Extend): | |
4257 | case GCBcase(GCB_V, GCB_Extend): | |
64935bc6 KW |
4258 | return FALSE; |
4259 | ||
4260 | /* Do not break before SpacingMarks, or after Prepend characters. | |
4261 | GB9a. × SpacingMark */ | |
85e5f08b KW |
4262 | case GCBcase(GCB_Other, GCB_SpacingMark): |
4263 | case GCBcase(GCB_Extend, GCB_SpacingMark): | |
4264 | case GCBcase(GCB_L, GCB_SpacingMark): | |
4265 | case GCBcase(GCB_LV, GCB_SpacingMark): | |
4266 | case GCBcase(GCB_LVT, GCB_SpacingMark): | |
4267 | case GCBcase(GCB_Prepend, GCB_SpacingMark): | |
4268 | case GCBcase(GCB_Regional_Indicator, GCB_SpacingMark): | |
4269 | case GCBcase(GCB_SpacingMark, GCB_SpacingMark): | |
4270 | case GCBcase(GCB_T, GCB_SpacingMark): | |
4271 | case GCBcase(GCB_V, GCB_SpacingMark): | |
64935bc6 KW |
4272 | return FALSE; |
4273 | ||
4274 | /* GB9b. Prepend × */ | |
85e5f08b KW |
4275 | case GCBcase(GCB_Prepend, GCB_Other): |
4276 | case GCBcase(GCB_Prepend, GCB_L): | |
4277 | case GCBcase(GCB_Prepend, GCB_LV): | |
4278 | case GCBcase(GCB_Prepend, GCB_LVT): | |
4279 | case GCBcase(GCB_Prepend, GCB_Prepend): | |
4280 | case GCBcase(GCB_Prepend, GCB_Regional_Indicator): | |
4281 | case GCBcase(GCB_Prepend, GCB_T): | |
4282 | case GCBcase(GCB_Prepend, GCB_V): | |
64935bc6 KW |
4283 | return FALSE; |
4284 | } | |
4285 | ||
661d43c4 | 4286 | NOT_REACHED; /* NOTREACHED */ |
64935bc6 KW |
4287 | } |
4288 | ||
06ae2722 KW |
4289 | #define SBcase(before, after) ((SB_ENUM_COUNT * before) + after) |
4290 | ||
4291 | STATIC bool | |
85e5f08b KW |
4292 | S_isSB(pTHX_ SB_enum before, |
4293 | SB_enum after, | |
06ae2722 KW |
4294 | const U8 * const strbeg, |
4295 | const U8 * const curpos, | |
4296 | const U8 * const strend, | |
4297 | const bool utf8_target) | |
4298 | { | |
4299 | /* returns a boolean indicating if there is a Sentence Boundary Break | |
4300 | * between the inputs. See http://www.unicode.org/reports/tr29/ */ | |
4301 | ||
4302 | U8 * lpos = (U8 *) curpos; | |
4303 | U8 * temp_pos; | |
85e5f08b | 4304 | SB_enum backup; |
06ae2722 KW |
4305 | |
4306 | PERL_ARGS_ASSERT_ISSB; | |
4307 | ||
4308 | /* Break at the start and end of text. | |
4309 | SB1. sot ÷ | |
4310 | SB2. ÷ eot */ | |
85e5f08b | 4311 | if (before == SB_EDGE || after == SB_EDGE) { |
06ae2722 KW |
4312 | return TRUE; |
4313 | } | |
4314 | ||
4315 | /* SB 3: Do not break within CRLF. */ | |
85e5f08b | 4316 | if (before == SB_CR && after == SB_LF) { |
06ae2722 KW |
4317 | return FALSE; |
4318 | } | |
4319 | ||
4320 | /* Break after paragraph separators. (though why CR and LF are considered | |
4321 | * so is beyond me (khw) | |
4322 | SB4. Sep | CR | LF ÷ */ | |
85e5f08b | 4323 | if (before == SB_Sep || before == SB_CR || before == SB_LF) { |
06ae2722 KW |
4324 | return TRUE; |
4325 | } | |
4326 | ||
4327 | /* Ignore Format and Extend characters, except after sot, Sep, CR, or LF. | |
4328 | * (See Section 6.2, Replacing Ignore Rules.) | |
4329 | SB5. X (Extend | Format)* → X */ | |
85e5f08b | 4330 | if (after == SB_Extend || after == SB_Format) { |
06ae2722 KW |
4331 | return FALSE; |
4332 | } | |
4333 | ||
85e5f08b | 4334 | if (before == SB_Extend || before == SB_Format) { |
06ae2722 KW |
4335 | before = backup_one_SB(strbeg, &lpos, utf8_target); |
4336 | } | |
4337 | ||
4338 | /* Do not break after ambiguous terminators like period, if they are | |
4339 | * immediately followed by a number or lowercase letter, if they are | |
4340 | * between uppercase letters, if the first following letter (optionally | |
4341 | * after certain punctuation) is lowercase, or if they are followed by | |
4342 | * "continuation" punctuation such as comma, colon, or semicolon. For | |
4343 | * example, a period may be an abbreviation or numeric period, and thus may | |
4344 | * not mark the end of a sentence. | |
4345 | ||
4346 | * SB6. ATerm × Numeric */ | |
85e5f08b | 4347 | if (before == SB_ATerm && after == SB_Numeric) { |
06ae2722 KW |
4348 | return FALSE; |
4349 | } | |
4350 | ||
4351 | /* SB7. Upper ATerm × Upper */ | |
85e5f08b | 4352 | if (before == SB_ATerm && after == SB_Upper) { |
06ae2722 | 4353 | temp_pos = lpos; |
85e5f08b | 4354 | if (SB_Upper == backup_one_SB(strbeg, &temp_pos, utf8_target)) { |
06ae2722 KW |
4355 | return FALSE; |
4356 | } | |
4357 | } | |
4358 | ||
4359 | /* SB8a. (STerm | ATerm) Close* Sp* × (SContinue | STerm | ATerm) | |
4360 | * SB10. (STerm | ATerm) Close* Sp* × ( Sp | Sep | CR | LF ) */ | |
4361 | backup = before; | |
4362 | temp_pos = lpos; | |
85e5f08b | 4363 | while (backup == SB_Sp) { |
06ae2722 KW |
4364 | backup = backup_one_SB(strbeg, &temp_pos, utf8_target); |
4365 | } | |
85e5f08b | 4366 | while (backup == SB_Close) { |
06ae2722 KW |
4367 | backup = backup_one_SB(strbeg, &temp_pos, utf8_target); |
4368 | } | |
85e5f08b KW |
4369 | if ((backup == SB_STerm || backup == SB_ATerm) |
4370 | && ( after == SB_SContinue | |
4371 | || after == SB_STerm | |
4372 | || after == SB_ATerm | |
4373 | || after == SB_Sp | |
4374 | || after == SB_Sep | |
4375 | || after == SB_CR | |
4376 | || after == SB_LF)) | |
06ae2722 KW |
4377 | { |
4378 | return FALSE; | |
4379 | } | |
4380 | ||
4381 | /* SB8. ATerm Close* Sp* × ( ¬(OLetter | Upper | Lower | Sep | CR | LF | | |
4382 | * STerm | ATerm) )* Lower */ | |
85e5f08b | 4383 | if (backup == SB_ATerm) { |
06ae2722 | 4384 | U8 * rpos = (U8 *) curpos; |
85e5f08b KW |
4385 | SB_enum later = after; |
4386 | ||
4387 | while ( later != SB_OLetter | |
4388 | && later != SB_Upper | |
4389 | && later != SB_Lower | |
4390 | && later != SB_Sep | |
4391 | && later != SB_CR | |
4392 | && later != SB_LF | |
4393 | && later != SB_STerm | |
4394 | && later != SB_ATerm | |
4395 | && later != SB_EDGE) | |
06ae2722 KW |
4396 | { |
4397 | later = advance_one_SB(&rpos, strend, utf8_target); | |
4398 | } | |
85e5f08b | 4399 | if (later == SB_Lower) { |
06ae2722 KW |
4400 | return FALSE; |
4401 | } | |
4402 | } | |
4403 | ||
4404 | /* Break after sentence terminators, but include closing punctuation, | |
4405 | * trailing spaces, and a paragraph separator (if present). [See note | |
4406 | * below.] | |
4407 | * SB9. ( STerm | ATerm ) Close* × ( Close | Sp | Sep | CR | LF ) */ | |
4408 | backup = before; | |
4409 | temp_pos = lpos; | |
85e5f08b | 4410 | while (backup == SB_Close) { |
06ae2722 KW |
4411 | backup = backup_one_SB(strbeg, &temp_pos, utf8_target); |
4412 | } | |
85e5f08b KW |
4413 | if ((backup == SB_STerm || backup == SB_ATerm) |
4414 | && ( after == SB_Close | |
4415 | || after == SB_Sp | |
4416 | || after == SB_Sep | |
4417 | || after == SB_CR | |
4418 | || after == SB_LF)) | |
06ae2722 KW |
4419 | { |
4420 | return FALSE; | |
4421 | } | |
4422 | ||
4423 | ||
4424 | /* SB11. ( STerm | ATerm ) Close* Sp* ( Sep | CR | LF )? ÷ */ | |
4425 | temp_pos = lpos; | |
4426 | backup = backup_one_SB(strbeg, &temp_pos, utf8_target); | |
85e5f08b KW |
4427 | if ( backup == SB_Sep |
4428 | || backup == SB_CR | |
4429 | || backup == SB_LF) | |
06ae2722 KW |
4430 | { |
4431 | lpos = temp_pos; | |
4432 | } | |
4433 | else { | |
4434 | backup = before; | |
4435 | } | |
85e5f08b | 4436 | while (backup == SB_Sp) { |
06ae2722 KW |
4437 | backup = backup_one_SB(strbeg, &lpos, utf8_target); |
4438 | } | |
85e5f08b | 4439 | while (backup == SB_Close) { |
06ae2722 KW |
4440 | backup = backup_one_SB(strbeg, &lpos, utf8_target); |
4441 | } | |
85e5f08b | 4442 | if (backup == SB_STerm || backup == SB_ATerm) { |
06ae2722 KW |
4443 | return TRUE; |
4444 | } | |
4445 | ||
4446 | /* Otherwise, do not break. | |
4447 | SB12. Any × Any */ | |
4448 | ||
4449 | return FALSE; | |
4450 | } | |
4451 | ||
85e5f08b | 4452 | STATIC SB_enum |
06ae2722 KW |
4453 | S_advance_one_SB(pTHX_ U8 ** curpos, const U8 * const strend, const bool utf8_target) |
4454 | { | |
85e5f08b | 4455 | SB_enum sb; |
06ae2722 KW |
4456 | |
4457 | PERL_ARGS_ASSERT_ADVANCE_ONE_SB; | |
4458 | ||
4459 | if (*curpos >= strend) { | |
85e5f08b | 4460 | return SB_EDGE; |
06ae2722 KW |
4461 | } |
4462 | ||
4463 | if (utf8_target) { | |
4464 | do { | |
4465 | *curpos += UTF8SKIP(*curpos); | |
4466 | if (*curpos >= strend) { | |
85e5f08b | 4467 | return SB_EDGE; |
06ae2722 KW |
4468 | } |
4469 | sb = getSB_VAL_UTF8(*curpos, strend); | |
85e5f08b | 4470 | } while (sb == SB_Extend || sb == SB_Format); |
06ae2722 KW |
4471 | } |
4472 | else { | |
4473 | do { | |
4474 | (*curpos)++; | |
4475 | if (*curpos >= strend) { | |
85e5f08b | 4476 | return SB_EDGE; |
06ae2722 KW |
4477 | } |
4478 | sb = getSB_VAL_CP(**curpos); | |
85e5f08b | 4479 | } while (sb == SB_Extend || sb == SB_Format); |
06ae2722 KW |
4480 | } |
4481 | ||
4482 | return sb; | |
4483 | } | |
4484 | ||
85e5f08b | 4485 | STATIC SB_enum |
06ae2722 KW |
4486 | S_backup_one_SB(pTHX_ const U8 * const strbeg, U8 ** curpos, const bool utf8_target) |
4487 | { | |
85e5f08b | 4488 | SB_enum sb; |
06ae2722 KW |
4489 | |
4490 | PERL_ARGS_ASSERT_BACKUP_ONE_SB; | |
4491 | ||
4492 | if (*curpos < strbeg) { | |
85e5f08b | 4493 | return SB_EDGE; |
06ae2722 KW |
4494 | } |
4495 | ||
4496 | if (utf8_target) { | |
4497 | U8 * prev_char_pos = reghopmaybe3(*curpos, -1, strbeg); | |
4498 | if (! prev_char_pos) { | |
85e5f08b | 4499 | return SB_EDGE; |
06ae2722 KW |
4500 | } |
4501 | ||
4502 | /* Back up over Extend and Format. curpos is always just to the right | |
4503 | * of the characater whose value we are getting */ | |
4504 | do { | |
4505 | U8 * prev_prev_char_pos; | |
4506 | if ((prev_prev_char_pos = reghopmaybe3((U8 *) prev_char_pos, -1, | |
4507 | strbeg))) | |
4508 | { | |
4509 | sb = getSB_VAL_UTF8(prev_prev_char_pos, prev_char_pos); | |
4510 | *curpos = prev_char_pos; | |
4511 | prev_char_pos = prev_prev_char_pos; | |
4512 | } | |
4513 | else { | |
4514 | *curpos = (U8 *) strbeg; | |
85e5f08b | 4515 | return SB_EDGE; |
06ae2722 | 4516 | } |
85e5f08b | 4517 | } while (sb == SB_Extend || sb == SB_Format); |
06ae2722 KW |
4518 | } |
4519 | else { | |
4520 | do { | |
4521 | if (*curpos - 2 < strbeg) { | |
4522 | *curpos = (U8 *) strbeg; | |
85e5f08b | 4523 | return SB_EDGE; |
06ae2722 KW |
4524 | } |
4525 | (*curpos)--; | |
4526 | sb = getSB_VAL_CP(*(*curpos - 1)); | |
85e5f08b | 4527 | } while (sb == SB_Extend || sb == SB_Format); |
06ae2722 KW |
4528 | } |
4529 | ||
4530 | return sb; | |
4531 | } | |
4532 | ||
85e5f08b | 4533 | #define WBcase(before, after) ((WB_ENUM_COUNT * before) + after) |
ae3bb8ea KW |
4534 | |
4535 | STATIC bool | |
85e5f08b KW |
4536 | S_isWB(pTHX_ WB_enum previous, |
4537 | WB_enum before, | |
4538 | WB_enum after, | |
ae3bb8ea KW |
4539 | const U8 * const strbeg, |
4540 | const U8 * const curpos, | |
4541 | const U8 * const strend, | |
4542 | const bool utf8_target) | |
4543 | { | |
4544 | /* Return a boolean as to if the boundary between 'before' and 'after' is | |
4545 | * a Unicode word break, using their published algorithm. Context may be | |
4546 | * needed to make this determination. If the value for the character | |
4547 | * before 'before' is known, it is passed as 'previous'; otherwise that | |
85e5f08b | 4548 | * should be set to WB_UNKNOWN. The other input parameters give the |
ae3bb8ea KW |
4549 | * boundaries and current position in the matching of the string. That |
4550 | * is, 'curpos' marks the position where the character whose wb value is | |
4551 | * 'after' begins. See http://www.unicode.org/reports/tr29/ */ | |
4552 | ||
4553 | U8 * before_pos = (U8 *) curpos; | |
4554 | U8 * after_pos = (U8 *) curpos; | |
4555 | ||
4556 | PERL_ARGS_ASSERT_ISWB; | |
4557 | ||
4558 | /* WB1 and WB2: Break at the start and end of text. */ | |
85e5f08b | 4559 | if (before == WB_EDGE || after == WB_EDGE) { |
ae3bb8ea KW |
4560 | return TRUE; |
4561 | } | |
4562 | ||
4563 | /* WB 3: Do not break within CRLF. */ | |
85e5f08b | 4564 | if (before == WB_CR && after == WB_LF) { |
ae3bb8ea KW |
4565 | return FALSE; |
4566 | } | |
4567 | ||
4568 | /* WB 3a and WB 3b: Otherwise break before and after Newlines (including CR | |
4569 | * and LF) */ | |
85e5f08b KW |
4570 | if ( before == WB_CR || before == WB_LF || before == WB_Newline |
4571 | || after == WB_CR || after == WB_LF || after == WB_Newline) | |
ae3bb8ea KW |
4572 | { |
4573 | return TRUE; | |
4574 | } | |
4575 | ||
4576 | /* Ignore Format and Extend characters, except when they appear at the | |
4577 | * beginning of a region of text. | |
4578 | * WB4. X (Extend | Format)* → X. */ | |
4579 | ||
85e5f08b | 4580 | if (after == WB_Extend || after == WB_Format) { |
ae3bb8ea KW |
4581 | return FALSE; |
4582 | } | |
4583 | ||
85e5f08b | 4584 | if (before == WB_Extend || before == WB_Format) { |
ae3bb8ea KW |
4585 | before = backup_one_WB(&previous, strbeg, &before_pos, utf8_target); |
4586 | } | |
4587 | ||
4588 | switch (WBcase(before, after)) { | |
4589 | /* Otherwise, break everywhere (including around ideographs). | |
4590 | WB14. Any ÷ Any */ | |
4591 | default: | |
4592 | return TRUE; | |
4593 | ||
4594 | /* Do not break between most letters. | |
4595 | WB5. (ALetter | Hebrew_Letter) × (ALetter | Hebrew_Letter) */ | |
85e5f08b KW |
4596 | case WBcase(WB_ALetter, WB_ALetter): |
4597 | case WBcase(WB_ALetter, WB_Hebrew_Letter): | |
4598 | case WBcase(WB_Hebrew_Letter, WB_ALetter): | |
4599 | case WBcase(WB_Hebrew_Letter, WB_Hebrew_Letter): | |
ae3bb8ea KW |
4600 | return FALSE; |
4601 | ||
4602 | /* Do not break letters across certain punctuation. | |
4603 | WB6. (ALetter | Hebrew_Letter) | |
4604 | × (MidLetter | MidNumLet | Single_Quote) (ALetter | |
4605 | | Hebrew_Letter) */ | |
85e5f08b KW |
4606 | case WBcase(WB_ALetter, WB_MidLetter): |
4607 | case WBcase(WB_ALetter, WB_MidNumLet): | |
4608 | case WBcase(WB_ALetter, WB_Single_Quote): | |
4609 | case WBcase(WB_Hebrew_Letter, WB_MidLetter): | |
4610 | case WBcase(WB_Hebrew_Letter, WB_MidNumLet): | |
4611 | /*case WBcase(WB_Hebrew_Letter, WB_Single_Quote):*/ | |
ae3bb8ea | 4612 | after = advance_one_WB(&after_pos, strend, utf8_target); |
85e5f08b | 4613 | return after != WB_ALetter && after != WB_Hebrew_Letter; |
ae3bb8ea KW |
4614 | |
4615 | /* WB7. (ALetter | Hebrew_Letter) (MidLetter | MidNumLet | | |
4616 | * Single_Quote) × (ALetter | Hebrew_Letter) */ | |
85e5f08b KW |
4617 | case WBcase(WB_MidLetter, WB_ALetter): |
4618 | case WBcase(WB_MidLetter, WB_Hebrew_Letter): | |
4619 | case WBcase(WB_MidNumLet, WB_ALetter): | |
4620 | case WBcase(WB_MidNumLet, WB_Hebrew_Letter): | |
4621 | case WBcase(WB_Single_Quote, WB_ALetter): | |
4622 | case WBcase(WB_Single_Quote, WB_Hebrew_Letter): | |
ae3bb8ea KW |
4623 | before |
4624 | = backup_one_WB(&previous, strbeg, &before_pos, utf8_target); | |
85e5f08b | 4625 | return before != WB_ALetter && before != WB_Hebrew_Letter; |
ae3bb8ea KW |
4626 | |
4627 | /* WB7a. Hebrew_Letter × Single_Quote */ | |
85e5f08b | 4628 | case WBcase(WB_Hebrew_Letter, WB_Single_Quote): |
ae3bb8ea KW |
4629 | return FALSE; |
4630 | ||
4631 | /* WB7b. Hebrew_Letter × Double_Quote Hebrew_Letter */ | |
85e5f08b | 4632 | case WBcase(WB_Hebrew_Letter, WB_Double_Quote): |
ae3bb8ea | 4633 | return advance_one_WB(&after_pos, strend, utf8_target) |
85e5f08b | 4634 | != WB_Hebrew_Letter; |
ae3bb8ea KW |
4635 | |
4636 | /* WB7c. Hebrew_Letter Double_Quote × Hebrew_Letter */ | |
85e5f08b | 4637 | case WBcase(WB_Double_Quote, WB_Hebrew_Letter): |
ae3bb8ea | 4638 | return backup_one_WB(&previous, strbeg, &before_pos, utf8_target) |
85e5f08b | 4639 | != WB_Hebrew_Letter; |
ae3bb8ea KW |
4640 | |
4641 | /* Do not break within sequences of digits, or digits adjacent to | |
4642 | * letters (“3a”, or “A3”). | |
4643 | WB8. Numeric × Numeric */ | |
85e5f08b | 4644 | case WBcase(WB_Numeric, WB_Numeric): |
ae3bb8ea KW |
4645 | return FALSE; |
4646 | ||
4647 | /* WB9. (ALetter | Hebrew_Letter) × Numeric */ | |
85e5f08b KW |
4648 | case WBcase(WB_ALetter, WB_Numeric): |
4649 | case WBcase(WB_Hebrew_Letter, WB_Numeric): | |
ae3bb8ea KW |
4650 | return FALSE; |
4651 | ||
4652 | /* WB10. Numeric × (ALetter | Hebrew_Letter) */ | |
85e5f08b KW |
4653 | case WBcase(WB_Numeric, WB_ALetter): |
4654 | case WBcase(WB_Numeric, WB_Hebrew_Letter): | |
ae3bb8ea KW |
4655 | return FALSE; |
4656 | ||
4657 | /* Do not break within sequences, such as “3.2” or “3,456.789”. | |
4658 | WB11. Numeric (MidNum | MidNumLet | Single_Quote) × Numeric | |
4659 | */ | |
85e5f08b KW |
4660 | case WBcase(WB_MidNum, WB_Numeric): |
4661 | case WBcase(WB_MidNumLet, WB_Numeric): | |
4662 | case WBcase(WB_Single_Quote, WB_Numeric): | |
ae3bb8ea | 4663 | return backup_one_WB(&previous, strbeg, &before_pos, utf8_target) |
85e5f08b | 4664 | != WB_Numeric; |
ae3bb8ea KW |
4665 | |
4666 | /* WB12. Numeric × (MidNum | MidNumLet | Single_Quote) Numeric | |
4667 | * */ | |
85e5f08b KW |
4668 | case WBcase(WB_Numeric, WB_MidNum): |
4669 | case WBcase(WB_Numeric, WB_MidNumLet): | |
4670 | case WBcase(WB_Numeric, WB_Single_Quote): | |
ae3bb8ea | 4671 | return advance_one_WB(&after_pos, strend, utf8_target) |
85e5f08b | 4672 | != WB_Numeric; |
ae3bb8ea KW |
4673 | |
4674 | /* Do not break between Katakana. | |
4675 | WB13. Katakana × Katakana */ | |
85e5f08b | 4676 | case WBcase(WB_Katakana, WB_Katakana): |
ae3bb8ea KW |
4677 | return FALSE; |
4678 | ||
4679 | /* Do not break from extenders. | |
4680 | WB13a. (ALetter | Hebrew_Letter | Numeric | Katakana | | |
4681 | ExtendNumLet) × ExtendNumLet */ | |
85e5f08b KW |
4682 | case WBcase(WB_ALetter, WB_ExtendNumLet): |
4683 | case WBcase(WB_Hebrew_Letter, WB_ExtendNumLet): | |
4684 | case WBcase(WB_Numeric, WB_ExtendNumLet): | |
4685 | case WBcase(WB_Katakana, WB_ExtendNumLet): | |
4686 | case WBcase(WB_ExtendNumLet, WB_ExtendNumLet): | |
ae3bb8ea KW |
4687 | return FALSE; |
4688 | ||
4689 | /* WB13b. ExtendNumLet × (ALetter | Hebrew_Letter | Numeric | |
4690 | * | Katakana) */ | |
85e5f08b KW |
4691 | case WBcase(WB_ExtendNumLet, WB_ALetter): |
4692 | case WBcase(WB_ExtendNumLet, WB_Hebrew_Letter): | |
4693 | case WBcase(WB_ExtendNumLet, WB_Numeric): | |
4694 | case WBcase(WB_ExtendNumLet, WB_Katakana): | |
ae3bb8ea KW |
4695 | return FALSE; |
4696 | ||
4697 | /* Do not break between regional indicator symbols. | |
4698 | WB13c. Regional_Indicator × Regional_Indicator */ | |
85e5f08b | 4699 | case WBcase(WB_Regional_Indicator, WB_Regional_Indicator): |
ae3bb8ea KW |
4700 | return FALSE; |
4701 | ||
4702 | } | |
4703 | ||
661d43c4 | 4704 | NOT_REACHED; /* NOTREACHED */ |
ae3bb8ea KW |
4705 | } |
4706 | ||
85e5f08b | 4707 | STATIC WB_enum |
ae3bb8ea KW |
4708 | S_advance_one_WB(pTHX_ U8 ** curpos, const U8 * const strend, const bool utf8_target) |
4709 | { | |
85e5f08b | 4710 | WB_enum wb; |
ae3bb8ea KW |
4711 | |
4712 | PERL_ARGS_ASSERT_ADVANCE_ONE_WB; | |
4713 | ||
4714 | if (*curpos >= strend) { | |
85e5f08b | 4715 | return WB_EDGE; |
ae3bb8ea KW |
4716 | } |
4717 | ||
4718 | if (utf8_target) { | |
4719 | ||
4720 | /* Advance over Extend and Format */ | |
4721 | do { | |
4722 | *curpos += UTF8SKIP(*curpos); | |
4723 | if (*curpos >= strend) { | |
85e5f08b | 4724 | return WB_EDGE; |
ae3bb8ea KW |
4725 | } |
4726 | wb = getWB_VAL_UTF8(*curpos, strend); | |
85e5f08b | 4727 | } while (wb == WB_Extend || wb == WB_Format); |
ae3bb8ea KW |
4728 | } |
4729 | else { | |
4730 | do { | |
4731 | (*curpos)++; | |
4732 | if (*curpos >= strend) { | |
85e5f08b | 4733 | return WB_EDGE; |
ae3bb8ea KW |
4734 | } |
4735 | wb = getWB_VAL_CP(**curpos); | |
85e5f08b | 4736 | } while (wb == WB_Extend || wb == WB_Format); |
ae3bb8ea KW |
4737 | } |
4738 | ||
4739 | return wb; | |
4740 | } | |
4741 | ||
85e5f08b KW |
4742 | STATIC WB_enum |
4743 | S_backup_one_WB(pTHX_ WB_enum * previous, const U8 * const strbeg, U8 ** curpos, const bool utf8_target) | |
ae3bb8ea | 4744 | { |
85e5f08b | 4745 | WB_enum wb; |
ae3bb8ea KW |
4746 | |
4747 | PERL_ARGS_ASSERT_BACKUP_ONE_WB; | |
4748 | ||
4749 | /* If we know what the previous character's break value is, don't have | |
4750 | * to look it up */ | |
85e5f08b | 4751 | if (*previous != WB_UNKNOWN) { |
ae3bb8ea | 4752 | wb = *previous; |
85e5f08b | 4753 | *previous = WB_UNKNOWN; |
ae3bb8ea KW |
4754 | /* XXX Note that doesn't change curpos, and maybe should */ |
4755 | ||
4756 | /* But we always back up over these two types */ | |
85e5f08b | 4757 | if (wb != WB_Extend && wb != WB_Format) { |
ae3bb8ea KW |
4758 | return wb; |
4759 | } | |
4760 | } | |
4761 | ||
4762 | if (*curpos < strbeg) { | |
85e5f08b | 4763 | return WB_EDGE; |
ae3bb8ea KW |
4764 | } |
4765 | ||
4766 | if (utf8_target) { | |
4767 | U8 * prev_char_pos = reghopmaybe3(*curpos, -1, strbeg); | |
4768 | if (! prev_char_pos) { | |
85e5f08b | 4769 | return WB_EDGE; |
ae3bb8ea KW |
4770 | } |
4771 | ||
4772 | /* Back up over Extend and Format. curpos is always just to the right | |
4773 | * of the characater whose value we are getting */ | |
4774 | do { | |
4775 | U8 * prev_prev_char_pos; | |
4776 | if ((prev_prev_char_pos = reghopmaybe3((U8 *) prev_char_pos, | |
4777 | -1, | |
4778 | strbeg))) | |
4779 | { | |
4780 | wb = getWB_VAL_UTF8(prev_prev_char_pos, prev_char_pos); | |
4781 | *curpos = prev_char_pos; | |
4782 | prev_char_pos = prev_prev_char_pos; | |
4783 | } | |
4784 | else { | |
4785 | *curpos = (U8 *) strbeg; | |
85e5f08b | 4786 | return WB_EDGE; |
ae3bb8ea | 4787 | } |
85e5f08b | 4788 | } while (wb == WB_Extend || wb == WB_Format); |
ae3bb8ea KW |
4789 | } |
4790 | else { | |
4791 | do { | |
4792 | if (*curpos - 2 < strbeg) { | |
4793 | *curpos = (U8 *) strbeg; | |
85e5f08b | 4794 | return WB_EDGE; |
ae3bb8ea KW |
4795 | } |
4796 | (*curpos)--; | |
4797 | wb = getWB_VAL_CP(*(*curpos - 1)); | |
85e5f08b | 4798 | } while (wb == WB_Extend || wb == WB_Format); |
ae3bb8ea KW |
4799 | } |
4800 | ||
4801 | return wb; | |
4802 | } | |
4803 | ||
f73aaa43 | 4804 | /* returns -1 on failure, $+[0] on success */ |
99a90e59 | 4805 | STATIC SSize_t |
f73aaa43 | 4806 | S_regmatch(pTHX_ regmatch_info *reginfo, char *startpos, regnode *prog) |
d6a28714 | 4807 | { |
a35a87e7 | 4808 | #if PERL_VERSION < 9 && !defined(PERL_CORE) |
54df2634 NC |
4809 | dMY_CXT; |
4810 | #endif | |
27da23d5 | 4811 | dVAR; |
ba44c216 | 4812 | const bool utf8_target = reginfo->is_utf8_target; |
4ad0818d | 4813 | const U32 uniflags = UTF8_ALLOW_DEFAULT; |
288b8c02 | 4814 | REGEXP *rex_sv = reginfo->prog; |
8d919b0a | 4815 | regexp *rex = ReANY(rex_sv); |
f8fc2ecf | 4816 | RXi_GET_DECL(rex,rexi); |
5d9a96ca | 4817 | /* the current state. This is a cached copy of PL_regmatch_state */ |
eb578fdb | 4818 | regmatch_state *st; |
5d9a96ca | 4819 | /* cache heavy used fields of st in registers */ |
eb578fdb KW |
4820 | regnode *scan; |
4821 | regnode *next; | |
4822 | U32 n = 0; /* general value; init to avoid compiler warning */ | |
ea3daa5d | 4823 | SSize_t ln = 0; /* len or last; init to avoid compiler warning */ |
d60de1d1 | 4824 | char *locinput = startpos; |
4d5016e5 | 4825 | char *pushinput; /* where to continue after a PUSH */ |
eb578fdb | 4826 | I32 nextchr; /* is always set to UCHARAT(locinput) */ |
24d3c4a9 | 4827 | |
b69b0499 | 4828 | bool result = 0; /* return value of S_regmatch */ |
24d3c4a9 | 4829 | int depth = 0; /* depth of backtrack stack */ |
4b196cd4 YO |
4830 | U32 nochange_depth = 0; /* depth of GOSUB recursion with nochange */ |
4831 | const U32 max_nochange_depth = | |
4832 | (3 * rex->nparens > MAX_RECURSE_EVAL_NOCHANGE_DEPTH) ? | |
4833 | 3 * rex->nparens : MAX_RECURSE_EVAL_NOCHANGE_DEPTH; | |
77cb431f DM |
4834 | regmatch_state *yes_state = NULL; /* state to pop to on success of |
4835 | subpattern */ | |
e2e6a0f1 YO |
4836 | /* mark_state piggy backs on the yes_state logic so that when we unwind |
4837 | the stack on success we can update the mark_state as we go */ | |
4838 | regmatch_state *mark_state = NULL; /* last mark state we have seen */ | |
faec1544 | 4839 | regmatch_state *cur_eval = NULL; /* most recent EVAL_AB state */ |
b8591aee | 4840 | struct regmatch_state *cur_curlyx = NULL; /* most recent curlyx */ |
40a82448 | 4841 | U32 state_num; |
5d458dd8 YO |
4842 | bool no_final = 0; /* prevent failure from backtracking? */ |
4843 | bool do_cutgroup = 0; /* no_final only until next branch/trie entry */ | |
d60de1d1 | 4844 | char *startpoint = locinput; |
5d458dd8 YO |
4845 | SV *popmark = NULL; /* are we looking for a mark? */ |
4846 | SV *sv_commit = NULL; /* last mark name seen in failure */ | |
4847 | SV *sv_yes_mark = NULL; /* last mark name we have seen | |
486ec47a | 4848 | during a successful match */ |
5d458dd8 YO |
4849 | U32 lastopen = 0; /* last open we saw */ |
4850 | bool has_cutgroup = RX_HAS_CUTGROUP(rex) ? 1 : 0; | |
4a2b275c | 4851 | SV* const oreplsv = GvSVn(PL_replgv); |
24d3c4a9 DM |
4852 | /* these three flags are set by various ops to signal information to |
4853 | * the very next op. They have a useful lifetime of exactly one loop | |
4854 | * iteration, and are not preserved or restored by state pushes/pops | |
4855 | */ | |
4856 | bool sw = 0; /* the condition value in (?(cond)a|b) */ | |
4857 | bool minmod = 0; /* the next "{n,m}" is a "{n,m}?" */ | |
4858 | int logical = 0; /* the following EVAL is: | |
4859 | 0: (?{...}) | |
4860 | 1: (?(?{...})X|Y) | |
4861 | 2: (??{...}) | |
4862 | or the following IFMATCH/UNLESSM is: | |
4863 | false: plain (?=foo) | |
4864 | true: used as a condition: (?(?=foo)) | |
4865 | */ | |
81ed78b2 DM |
4866 | PAD* last_pad = NULL; |
4867 | dMULTICALL; | |
4868 | I32 gimme = G_SCALAR; | |
4869 | CV *caller_cv = NULL; /* who called us */ | |
4870 | CV *last_pushed_cv = NULL; /* most recently called (?{}) CV */ | |
74088413 | 4871 | CHECKPOINT runops_cp; /* savestack position before executing EVAL */ |
92da3157 | 4872 | U32 maxopenparen = 0; /* max '(' index seen so far */ |
3018b823 KW |
4873 | int to_complement; /* Invert the result? */ |
4874 | _char_class_number classnum; | |
984e6dd1 | 4875 | bool is_utf8_pat = reginfo->is_utf8_pat; |
64935bc6 KW |
4876 | bool match = FALSE; |
4877 | ||
81ed78b2 | 4878 | |
95b24440 | 4879 | #ifdef DEBUGGING |
e68ec53f | 4880 | GET_RE_DEBUG_FLAGS_DECL; |
d6a28714 JH |
4881 | #endif |
4882 | ||
7e68f152 FC |
4883 | /* protect against undef(*^R) */ |
4884 | SAVEFREESV(SvREFCNT_inc_simple_NN(oreplsv)); | |
4885 | ||
81ed78b2 DM |
4886 | /* shut up 'may be used uninitialized' compiler warnings for dMULTICALL */ |
4887 | multicall_oldcatch = 0; | |
4888 | multicall_cv = NULL; | |
4889 | cx = NULL; | |
4f8dbb2d JL |
4890 | PERL_UNUSED_VAR(multicall_cop); |
4891 | PERL_UNUSED_VAR(newsp); | |
81ed78b2 DM |
4892 | |
4893 | ||
7918f24d NC |
4894 | PERL_ARGS_ASSERT_REGMATCH; |
4895 | ||
3b57cd43 | 4896 | DEBUG_OPTIMISE_r( DEBUG_EXECUTE_r({ |
24b23f37 | 4897 | PerlIO_printf(Perl_debug_log,"regmatch start\n"); |
3b57cd43 | 4898 | })); |
5d9a96ca | 4899 | |
331b2dcc | 4900 | st = PL_regmatch_state; |
5d9a96ca | 4901 | |
d6a28714 | 4902 | /* Note that nextchr is a byte even in UTF */ |
7016d6eb | 4903 | SET_nextchr; |
d6a28714 JH |
4904 | scan = prog; |
4905 | while (scan != NULL) { | |
8ba1375e | 4906 | |
a3621e74 | 4907 | DEBUG_EXECUTE_r( { |
6136c704 | 4908 | SV * const prop = sv_newmortal(); |
1de06328 | 4909 | regnode *rnext=regnext(scan); |
f2ed9b32 | 4910 | DUMP_EXEC_POS( locinput, scan, utf8_target ); |
8b9781c9 | 4911 | regprop(rex, prop, scan, reginfo, NULL); |
07be1b83 YO |
4912 | |
4913 | PerlIO_printf(Perl_debug_log, | |
4914 | "%3"IVdf":%*s%s(%"IVdf")\n", | |
f8fc2ecf | 4915 | (IV)(scan - rexi->program), depth*2, "", |
07be1b83 | 4916 | SvPVX_const(prop), |
1de06328 | 4917 | (PL_regkind[OP(scan)] == END || !rnext) ? |
f8fc2ecf | 4918 | 0 : (IV)(rnext - rexi->program)); |
2a782b5b | 4919 | }); |
d6a28714 JH |
4920 | |
4921 | next = scan + NEXT_OFF(scan); | |
4922 | if (next == scan) | |
4923 | next = NULL; | |
40a82448 | 4924 | state_num = OP(scan); |
d6a28714 | 4925 | |
40a82448 | 4926 | reenter_switch: |
3018b823 | 4927 | to_complement = 0; |
34a81e2b | 4928 | |
7016d6eb | 4929 | SET_nextchr; |
e6ca698c | 4930 | assert(nextchr < 256 && (nextchr >= 0 || nextchr == NEXTCHR_EOS)); |
bf798dc4 | 4931 | |
40a82448 | 4932 | switch (state_num) { |
d3d47aac | 4933 | case SBOL: /* /^../ and /\A../ */ |
9d9163fb | 4934 | if (locinput == reginfo->strbeg) |
b8c5462f | 4935 | break; |
d6a28714 | 4936 | sayNO; |
3c0563b9 DM |
4937 | |
4938 | case MBOL: /* /^../m */ | |
9d9163fb | 4939 | if (locinput == reginfo->strbeg || |
7016d6eb | 4940 | (!NEXTCHR_IS_EOS && locinput[-1] == '\n')) |
d6a28714 | 4941 | { |
b8c5462f JH |
4942 | break; |
4943 | } | |
d6a28714 | 4944 | sayNO; |
3c0563b9 | 4945 | |
3c0563b9 | 4946 | case GPOS: /* \G */ |
3b0527fe | 4947 | if (locinput == reginfo->ganch) |
d6a28714 JH |
4948 | break; |
4949 | sayNO; | |
ee9b8eae | 4950 | |
3c0563b9 | 4951 | case KEEPS: /* \K */ |
ee9b8eae | 4952 | /* update the startpoint */ |
b93070ed | 4953 | st->u.keeper.val = rex->offs[0].start; |
9d9163fb | 4954 | rex->offs[0].start = locinput - reginfo->strbeg; |
4d5016e5 | 4955 | PUSH_STATE_GOTO(KEEPS_next, next, locinput); |
a74ff37d | 4956 | /* NOTREACHED */ |
661d43c4 | 4957 | NOT_REACHED; /* NOTREACHED */ |
a74ff37d | 4958 | |
ee9b8eae YO |
4959 | case KEEPS_next_fail: |
4960 | /* rollback the start point change */ | |
b93070ed | 4961 | rex->offs[0].start = st->u.keeper.val; |
ee9b8eae | 4962 | sayNO_SILENT; |
a74ff37d | 4963 | /* NOTREACHED */ |
661d43c4 | 4964 | NOT_REACHED; /* NOTREACHED */ |
3c0563b9 | 4965 | |
3c0563b9 | 4966 | case MEOL: /* /..$/m */ |
7016d6eb | 4967 | if (!NEXTCHR_IS_EOS && nextchr != '\n') |
b8c5462f | 4968 | sayNO; |
b8c5462f | 4969 | break; |
3c0563b9 | 4970 | |
d3d47aac | 4971 | case SEOL: /* /..$/ */ |
7016d6eb | 4972 | if (!NEXTCHR_IS_EOS && nextchr != '\n') |
b8c5462f | 4973 | sayNO; |
220db18a | 4974 | if (reginfo->strend - locinput > 1) |
b8c5462f | 4975 | sayNO; |
b8c5462f | 4976 | break; |
3c0563b9 DM |
4977 | |
4978 | case EOS: /* \z */ | |
7016d6eb | 4979 | if (!NEXTCHR_IS_EOS) |
b8c5462f | 4980 | sayNO; |
d6a28714 | 4981 | break; |
3c0563b9 DM |
4982 | |
4983 | case SANY: /* /./s */ | |
7016d6eb | 4984 | if (NEXTCHR_IS_EOS) |
4633a7c4 | 4985 | sayNO; |
28b98f76 | 4986 | goto increment_locinput; |
3c0563b9 DM |
4987 | |
4988 | case CANY: /* \C */ | |
7016d6eb | 4989 | if (NEXTCHR_IS_EOS) |
f33976b4 | 4990 | sayNO; |
3640db6b | 4991 | locinput++; |
a0d0e21e | 4992 | break; |
3c0563b9 DM |
4993 | |
4994 | case REG_ANY: /* /./ */ | |
7016d6eb | 4995 | if ((NEXTCHR_IS_EOS) || nextchr == '\n') |
1aa99e6b | 4996 | sayNO; |
28b98f76 DM |
4997 | goto increment_locinput; |
4998 | ||
166ba7cd DM |
4999 | |
5000 | #undef ST | |
5001 | #define ST st->u.trie | |
3c0563b9 | 5002 | case TRIEC: /* (ab|cd) with known charclass */ |
786e8c11 YO |
5003 | /* In this case the charclass data is available inline so |
5004 | we can fail fast without a lot of extra overhead. | |
5005 | */ | |
7016d6eb | 5006 | if(!NEXTCHR_IS_EOS && !ANYOF_BITMAP_TEST(scan, nextchr)) { |
fab2782b YO |
5007 | DEBUG_EXECUTE_r( |
5008 | PerlIO_printf(Perl_debug_log, | |
5009 | "%*s %sfailed to match trie start class...%s\n", | |
5010 | REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5]) | |
5011 | ); | |
5012 | sayNO_SILENT; | |
a74ff37d | 5013 | /* NOTREACHED */ |
661d43c4 | 5014 | NOT_REACHED; /* NOTREACHED */ |
786e8c11 | 5015 | } |
924ba076 | 5016 | /* FALLTHROUGH */ |
3c0563b9 | 5017 | case TRIE: /* (ab|cd) */ |
2e64971a DM |
5018 | /* the basic plan of execution of the trie is: |
5019 | * At the beginning, run though all the states, and | |
5020 | * find the longest-matching word. Also remember the position | |
5021 | * of the shortest matching word. For example, this pattern: | |
5022 | * 1 2 3 4 5 | |
5023 | * ab|a|x|abcd|abc | |
5024 | * when matched against the string "abcde", will generate | |
5025 | * accept states for all words except 3, with the longest | |
895cc420 | 5026 | * matching word being 4, and the shortest being 2 (with |
2e64971a DM |
5027 | * the position being after char 1 of the string). |
5028 | * | |
5029 | * Then for each matching word, in word order (i.e. 1,2,4,5), | |
5030 | * we run the remainder of the pattern; on each try setting | |
5031 | * the current position to the character following the word, | |
5032 | * returning to try the next word on failure. | |
5033 | * | |
5034 | * We avoid having to build a list of words at runtime by | |
5035 | * using a compile-time structure, wordinfo[].prev, which | |
5036 | * gives, for each word, the previous accepting word (if any). | |
5037 | * In the case above it would contain the mappings 1->2, 2->0, | |
5038 | * 3->0, 4->5, 5->1. We can use this table to generate, from | |
5039 | * the longest word (4 above), a list of all words, by | |
5040 | * following the list of prev pointers; this gives us the | |
5041 | * unordered list 4,5,1,2. Then given the current word we have | |
5042 | * just tried, we can go through the list and find the | |
5043 | * next-biggest word to try (so if we just failed on word 2, | |
5044 | * the next in the list is 4). | |
5045 | * | |
5046 | * Since at runtime we don't record the matching position in | |
5047 | * the string for each word, we have to work that out for | |
5048 | * each word we're about to process. The wordinfo table holds | |
5049 | * the character length of each word; given that we recorded | |
5050 | * at the start: the position of the shortest word and its | |
5051 | * length in chars, we just need to move the pointer the | |
5052 | * difference between the two char lengths. Depending on | |
5053 | * Unicode status and folding, that's cheap or expensive. | |
5054 | * | |
5055 | * This algorithm is optimised for the case where are only a | |
5056 | * small number of accept states, i.e. 0,1, or maybe 2. | |
5057 | * With lots of accepts states, and having to try all of them, | |
5058 | * it becomes quadratic on number of accept states to find all | |
5059 | * the next words. | |
5060 | */ | |
5061 | ||
3dab1dad | 5062 | { |
07be1b83 | 5063 | /* what type of TRIE am I? (utf8 makes this contextual) */ |
a0a388a1 | 5064 | DECL_TRIE_TYPE(scan); |
3dab1dad YO |
5065 | |
5066 | /* what trie are we using right now */ | |
be8e71aa | 5067 | reg_trie_data * const trie |
f8fc2ecf | 5068 | = (reg_trie_data*)rexi->data->data[ ARG( scan ) ]; |
85fbaab2 | 5069 | HV * widecharmap = MUTABLE_HV(rexi->data->data[ ARG( scan ) + 1 ]); |
3dab1dad | 5070 | U32 state = trie->startstate; |
166ba7cd | 5071 | |
780fcc9f KW |
5072 | if (scan->flags == EXACTL || scan->flags == EXACTFLU8) { |
5073 | _CHECK_AND_WARN_PROBLEMATIC_LOCALE; | |
613abc6d KW |
5074 | if (utf8_target |
5075 | && UTF8_IS_ABOVE_LATIN1(nextchr) | |
5076 | && scan->flags == EXACTL) | |
5077 | { | |
5078 | /* We only output for EXACTL, as we let the folder | |
5079 | * output this message for EXACTFLU8 to avoid | |
5080 | * duplication */ | |
5081 | _CHECK_AND_OUTPUT_WIDE_LOCALE_UTF8_MSG(locinput, | |
5082 | reginfo->strend); | |
5083 | } | |
780fcc9f | 5084 | } |
7016d6eb DM |
5085 | if ( trie->bitmap |
5086 | && (NEXTCHR_IS_EOS || !TRIE_BITMAP_TEST(trie, nextchr))) | |
5087 | { | |
3dab1dad YO |
5088 | if (trie->states[ state ].wordnum) { |
5089 | DEBUG_EXECUTE_r( | |
5090 | PerlIO_printf(Perl_debug_log, | |
5091 | "%*s %smatched empty string...%s\n", | |
5bc10b2c | 5092 | REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5]) |
3dab1dad | 5093 | ); |
20dbff7c YO |
5094 | if (!trie->jump) |
5095 | break; | |
3dab1dad YO |
5096 | } else { |
5097 | DEBUG_EXECUTE_r( | |
5098 | PerlIO_printf(Perl_debug_log, | |
786e8c11 | 5099 | "%*s %sfailed to match trie start class...%s\n", |
5bc10b2c | 5100 | REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5]) |
3dab1dad YO |
5101 | ); |
5102 | sayNO_SILENT; | |
5103 | } | |
5104 | } | |
166ba7cd | 5105 | |
786e8c11 YO |
5106 | { |
5107 | U8 *uc = ( U8* )locinput; | |
5108 | ||
5109 | STRLEN len = 0; | |
5110 | STRLEN foldlen = 0; | |
5111 | U8 *uscan = (U8*)NULL; | |
786e8c11 | 5112 | U8 foldbuf[ UTF8_MAXBYTES_CASE + 1 ]; |
2e64971a DM |
5113 | U32 charcount = 0; /* how many input chars we have matched */ |
5114 | U32 accepted = 0; /* have we seen any accepting states? */ | |
786e8c11 | 5115 | |
786e8c11 | 5116 | ST.jump = trie->jump; |
786e8c11 | 5117 | ST.me = scan; |
2e64971a DM |
5118 | ST.firstpos = NULL; |
5119 | ST.longfold = FALSE; /* char longer if folded => it's harder */ | |
5120 | ST.nextword = 0; | |
5121 | ||
5122 | /* fully traverse the TRIE; note the position of the | |
5123 | shortest accept state and the wordnum of the longest | |
5124 | accept state */ | |
07be1b83 | 5125 | |
220db18a | 5126 | while ( state && uc <= (U8*)(reginfo->strend) ) { |
786e8c11 | 5127 | U32 base = trie->states[ state ].trans.base; |
f9f4320a | 5128 | UV uvc = 0; |
acb909b4 | 5129 | U16 charid = 0; |
2e64971a DM |
5130 | U16 wordnum; |
5131 | wordnum = trie->states[ state ].wordnum; | |
5132 | ||
5133 | if (wordnum) { /* it's an accept state */ | |
5134 | if (!accepted) { | |
5135 | accepted = 1; | |
5136 | /* record first match position */ | |
5137 | if (ST.longfold) { | |
5138 | ST.firstpos = (U8*)locinput; | |
5139 | ST.firstchars = 0; | |
5b47454d | 5140 | } |
2e64971a DM |
5141 | else { |
5142 | ST.firstpos = uc; | |
5143 | ST.firstchars = charcount; | |
5144 | } | |
5145 | } | |
5146 | if (!ST.nextword || wordnum < ST.nextword) | |
5147 | ST.nextword = wordnum; | |
5148 | ST.topword = wordnum; | |
786e8c11 | 5149 | } |
a3621e74 | 5150 | |
07be1b83 | 5151 | DEBUG_TRIE_EXECUTE_r({ |
f2ed9b32 | 5152 | DUMP_EXEC_POS( (char *)uc, scan, utf8_target ); |
a3621e74 | 5153 | PerlIO_printf( Perl_debug_log, |
2e64971a | 5154 | "%*s %sState: %4"UVxf" Accepted: %c ", |
5bc10b2c | 5155 | 2+depth * 2, "", PL_colors[4], |
2e64971a | 5156 | (UV)state, (accepted ? 'Y' : 'N')); |
07be1b83 | 5157 | }); |
a3621e74 | 5158 | |
2e64971a | 5159 | /* read a char and goto next state */ |
220db18a | 5160 | if ( base && (foldlen || uc < (U8*)(reginfo->strend))) { |
6dd2be57 | 5161 | I32 offset; |
55eed653 NC |
5162 | REXEC_TRIE_READ_CHAR(trie_type, trie, widecharmap, uc, |
5163 | uscan, len, uvc, charid, foldlen, | |
5164 | foldbuf, uniflags); | |
2e64971a DM |
5165 | charcount++; |
5166 | if (foldlen>0) | |
5167 | ST.longfold = TRUE; | |
5b47454d | 5168 | if (charid && |
6dd2be57 DM |
5169 | ( ((offset = |
5170 | base + charid - 1 - trie->uniquecharcount)) >= 0) | |
5171 | ||
5172 | && ((U32)offset < trie->lasttrans) | |
5173 | && trie->trans[offset].check == state) | |
5b47454d | 5174 | { |
6dd2be57 | 5175 | state = trie->trans[offset].next; |
5b47454d DM |
5176 | } |
5177 | else { | |
5178 | state = 0; | |
5179 | } | |
5180 | uc += len; | |
5181 | ||
5182 | } | |
5183 | else { | |
a3621e74 YO |
5184 | state = 0; |
5185 | } | |
5186 | DEBUG_TRIE_EXECUTE_r( | |
e4584336 | 5187 | PerlIO_printf( Perl_debug_log, |
786e8c11 | 5188 | "Charid:%3x CP:%4"UVxf" After State: %4"UVxf"%s\n", |
e4584336 | 5189 | charid, uvc, (UV)state, PL_colors[5] ); |
a3621e74 YO |
5190 | ); |
5191 | } | |
2e64971a | 5192 | if (!accepted) |
a3621e74 | 5193 | sayNO; |
a3621e74 | 5194 | |
2e64971a DM |
5195 | /* calculate total number of accept states */ |
5196 | { | |
5197 | U16 w = ST.topword; | |
5198 | accepted = 0; | |
5199 | while (w) { | |
5200 | w = trie->wordinfo[w].prev; | |
5201 | accepted++; | |
5202 | } | |
5203 | ST.accepted = accepted; | |
5204 | } | |
5205 | ||
166ba7cd DM |
5206 | DEBUG_EXECUTE_r( |
5207 | PerlIO_printf( Perl_debug_log, | |
5208 | "%*s %sgot %"IVdf" possible matches%s\n", | |
5bc10b2c | 5209 | REPORT_CODE_OFF + depth * 2, "", |
166ba7cd DM |
5210 | PL_colors[4], (IV)ST.accepted, PL_colors[5] ); |
5211 | ); | |
2e64971a | 5212 | goto trie_first_try; /* jump into the fail handler */ |
786e8c11 | 5213 | }} |
a74ff37d | 5214 | /* NOTREACHED */ |
661d43c4 | 5215 | NOT_REACHED; /* NOTREACHED */ |
2e64971a DM |
5216 | |
5217 | case TRIE_next_fail: /* we failed - try next alternative */ | |
a059a757 DM |
5218 | { |
5219 | U8 *uc; | |
fae667d5 YO |
5220 | if ( ST.jump) { |
5221 | REGCP_UNWIND(ST.cp); | |
a8d1f4b4 | 5222 | UNWIND_PAREN(ST.lastparen, ST.lastcloseparen); |
fae667d5 | 5223 | } |
2e64971a DM |
5224 | if (!--ST.accepted) { |
5225 | DEBUG_EXECUTE_r({ | |
5226 | PerlIO_printf( Perl_debug_log, | |
5227 | "%*s %sTRIE failed...%s\n", | |
5228 | REPORT_CODE_OFF+depth*2, "", | |
5229 | PL_colors[4], | |
5230 | PL_colors[5] ); | |
5231 | }); | |
5232 | sayNO_SILENT; | |
5233 | } | |
5234 | { | |
5235 | /* Find next-highest word to process. Note that this code | |
5236 | * is O(N^2) per trie run (O(N) per branch), so keep tight */ | |
eb578fdb KW |
5237 | U16 min = 0; |
5238 | U16 word; | |
5239 | U16 const nextword = ST.nextword; | |
5240 | reg_trie_wordinfo * const wordinfo | |
2e64971a DM |
5241 | = ((reg_trie_data*)rexi->data->data[ARG(ST.me)])->wordinfo; |
5242 | for (word=ST.topword; word; word=wordinfo[word].prev) { | |
5243 | if (word > nextword && (!min || word < min)) | |
5244 | min = word; | |
5245 | } | |
5246 | ST.nextword = min; | |
5247 | } | |
5248 | ||
fae667d5 | 5249 | trie_first_try: |
5d458dd8 YO |
5250 | if (do_cutgroup) { |
5251 | do_cutgroup = 0; | |
5252 | no_final = 0; | |
5253 | } | |
fae667d5 YO |
5254 | |
5255 | if ( ST.jump) { | |
b93070ed | 5256 | ST.lastparen = rex->lastparen; |
f6033a9d | 5257 | ST.lastcloseparen = rex->lastcloseparen; |
fae667d5 | 5258 | REGCP_SET(ST.cp); |
2e64971a | 5259 | } |
a3621e74 | 5260 | |
2e64971a | 5261 | /* find start char of end of current word */ |
166ba7cd | 5262 | { |
2e64971a | 5263 | U32 chars; /* how many chars to skip */ |
2e64971a DM |
5264 | reg_trie_data * const trie |
5265 | = (reg_trie_data*)rexi->data->data[ARG(ST.me)]; | |
5266 | ||
5267 | assert((trie->wordinfo[ST.nextword].len - trie->prefixlen) | |
5268 | >= ST.firstchars); | |
5269 | chars = (trie->wordinfo[ST.nextword].len - trie->prefixlen) | |
5270 | - ST.firstchars; | |
a059a757 | 5271 | uc = ST.firstpos; |
2e64971a DM |
5272 | |
5273 | if (ST.longfold) { | |
5274 | /* the hard option - fold each char in turn and find | |
5275 | * its folded length (which may be different */ | |
5276 | U8 foldbuf[UTF8_MAXBYTES_CASE + 1]; | |
5277 | STRLEN foldlen; | |
5278 | STRLEN len; | |
d9a396a3 | 5279 | UV uvc; |
2e64971a DM |
5280 | U8 *uscan; |
5281 | ||
5282 | while (chars) { | |
f2ed9b32 | 5283 | if (utf8_target) { |
c80e42f3 | 5284 | uvc = utf8n_to_uvchr((U8*)uc, UTF8_MAXLEN, &len, |
2e64971a DM |
5285 | uniflags); |
5286 | uc += len; | |
5287 | } | |
5288 | else { | |
5289 | uvc = *uc; | |
5290 | uc++; | |
5291 | } | |
5292 | uvc = to_uni_fold(uvc, foldbuf, &foldlen); | |
5293 | uscan = foldbuf; | |
5294 | while (foldlen) { | |
5295 | if (!--chars) | |
5296 | break; | |
c80e42f3 | 5297 | uvc = utf8n_to_uvchr(uscan, UTF8_MAXLEN, &len, |
2e64971a DM |
5298 | uniflags); |
5299 | uscan += len; | |
5300 | foldlen -= len; | |
5301 | } | |
5302 | } | |
a3621e74 | 5303 | } |
2e64971a | 5304 | else { |
f2ed9b32 | 5305 | if (utf8_target) |
2e64971a DM |
5306 | while (chars--) |
5307 | uc += UTF8SKIP(uc); | |
5308 | else | |
5309 | uc += chars; | |
5310 | } | |
2e64971a | 5311 | } |
166ba7cd | 5312 | |
6603fe3e DM |
5313 | scan = ST.me + ((ST.jump && ST.jump[ST.nextword]) |
5314 | ? ST.jump[ST.nextword] | |
5315 | : NEXT_OFF(ST.me)); | |
166ba7cd | 5316 | |
2e64971a DM |
5317 | DEBUG_EXECUTE_r({ |
5318 | PerlIO_printf( Perl_debug_log, | |
5319 | "%*s %sTRIE matched word #%d, continuing%s\n", | |
5320 | REPORT_CODE_OFF+depth*2, "", | |
5321 | PL_colors[4], | |
5322 | ST.nextword, | |
5323 | PL_colors[5] | |
5324 | ); | |
5325 | }); | |
5326 | ||
5327 | if (ST.accepted > 1 || has_cutgroup) { | |
a059a757 | 5328 | PUSH_STATE_GOTO(TRIE_next, scan, (char*)uc); |
a74ff37d | 5329 | /* NOTREACHED */ |
661d43c4 | 5330 | NOT_REACHED; /* NOTREACHED */ |
166ba7cd | 5331 | } |
2e64971a DM |
5332 | /* only one choice left - just continue */ |
5333 | DEBUG_EXECUTE_r({ | |
5334 | AV *const trie_words | |
5335 | = MUTABLE_AV(rexi->data->data[ARG(ST.me)+TRIE_WORDS_OFFSET]); | |
d0bec203 HS |
5336 | SV ** const tmp = trie_words |
5337 | ? av_fetch(trie_words, ST.nextword - 1, 0) : NULL; | |
2e64971a DM |
5338 | SV *sv= tmp ? sv_newmortal() : NULL; |
5339 | ||
5340 | PerlIO_printf( Perl_debug_log, | |
5341 | "%*s %sonly one match left, short-circuiting: #%d <%s>%s\n", | |
5342 | REPORT_CODE_OFF+depth*2, "", PL_colors[4], | |
5343 | ST.nextword, | |
5344 | tmp ? pv_pretty(sv, SvPV_nolen_const(*tmp), SvCUR(*tmp), 0, | |
5345 | PL_colors[0], PL_colors[1], | |
c89df6cf | 5346 | (SvUTF8(*tmp) ? PERL_PV_ESCAPE_UNI : 0)|PERL_PV_ESCAPE_NONASCII |
2e64971a DM |
5347 | ) |
5348 | : "not compiled under -Dr", | |
5349 | PL_colors[5] ); | |
5350 | }); | |
5351 | ||
a059a757 | 5352 | locinput = (char*)uc; |
2e64971a | 5353 | continue; /* execute rest of RE */ |
a74ff37d | 5354 | /* NOTREACHED */ |
a059a757 | 5355 | } |
166ba7cd DM |
5356 | #undef ST |
5357 | ||
a4525e78 | 5358 | case EXACTL: /* /abc/l */ |
780fcc9f | 5359 | _CHECK_AND_WARN_PROBLEMATIC_LOCALE; |
613abc6d KW |
5360 | |
5361 | /* Complete checking would involve going through every character | |
5362 | * matched by the string to see if any is above latin1. But the | |
5363 | * comparision otherwise might very well be a fast assembly | |
5364 | * language routine, and I (khw) don't think slowing things down | |
5365 | * just to check for this warning is worth it. So this just checks | |
5366 | * the first character */ | |
5367 | if (utf8_target && UTF8_IS_ABOVE_LATIN1(*locinput)) { | |
5368 | _CHECK_AND_OUTPUT_WIDE_LOCALE_UTF8_MSG(locinput, reginfo->strend); | |
5369 | } | |
780fcc9f | 5370 | /* FALLTHROUGH */ |
3c0563b9 | 5371 | case EXACT: { /* /abc/ */ |
95b24440 | 5372 | char *s = STRING(scan); |
24d3c4a9 | 5373 | ln = STR_LEN(scan); |
984e6dd1 | 5374 | if (utf8_target != is_utf8_pat) { |
bc517b45 | 5375 | /* The target and the pattern have differing utf8ness. */ |
1aa99e6b | 5376 | char *l = locinput; |
24d3c4a9 | 5377 | const char * const e = s + ln; |
a72c7584 | 5378 | |
f2ed9b32 | 5379 | if (utf8_target) { |
e6a3850e KW |
5380 | /* The target is utf8, the pattern is not utf8. |
5381 | * Above-Latin1 code points can't match the pattern; | |
5382 | * invariants match exactly, and the other Latin1 ones need | |
5383 | * to be downgraded to a single byte in order to do the | |
5384 | * comparison. (If we could be confident that the target | |
5385 | * is not malformed, this could be refactored to have fewer | |
5386 | * tests by just assuming that if the first bytes match, it | |
5387 | * is an invariant, but there are tests in the test suite | |
5388 | * dealing with (??{...}) which violate this) */ | |
1aa99e6b | 5389 | while (s < e) { |
220db18a DM |
5390 | if (l >= reginfo->strend |
5391 | || UTF8_IS_ABOVE_LATIN1(* (U8*) l)) | |
5392 | { | |
e6a3850e KW |
5393 | sayNO; |
5394 | } | |
5395 | if (UTF8_IS_INVARIANT(*(U8*)l)) { | |
5396 | if (*l != *s) { | |
5397 | sayNO; | |
5398 | } | |
5399 | l++; | |
5400 | } | |
5401 | else { | |
94bb8c36 KW |
5402 | if (TWO_BYTE_UTF8_TO_NATIVE(*l, *(l+1)) != * (U8*) s) |
5403 | { | |
e6a3850e KW |
5404 | sayNO; |
5405 | } | |
5406 | l += 2; | |
5407 | } | |
5408 | s++; | |
1aa99e6b | 5409 | } |
5ff6fc6d JH |
5410 | } |
5411 | else { | |
5412 | /* The target is not utf8, the pattern is utf8. */ | |
1aa99e6b | 5413 | while (s < e) { |
220db18a DM |
5414 | if (l >= reginfo->strend |
5415 | || UTF8_IS_ABOVE_LATIN1(* (U8*) s)) | |
e6a3850e KW |
5416 | { |
5417 | sayNO; | |
5418 | } | |
5419 | if (UTF8_IS_INVARIANT(*(U8*)s)) { | |
5420 | if (*s != *l) { | |
5421 | sayNO; | |
5422 | } | |
5423 | s++; | |
5424 | } | |
5425 | else { | |
94bb8c36 KW |
5426 | if (TWO_BYTE_UTF8_TO_NATIVE(*s, *(s+1)) != * (U8*) l) |
5427 | { | |
e6a3850e KW |
5428 | sayNO; |
5429 | } | |
5430 | s += 2; | |
5431 | } | |
5432 | l++; | |
1aa99e6b | 5433 | } |
5ff6fc6d | 5434 | } |
1aa99e6b | 5435 | locinput = l; |
1aa99e6b | 5436 | } |
5ac65bff KW |
5437 | else { |
5438 | /* The target and the pattern have the same utf8ness. */ | |
5439 | /* Inline the first character, for speed. */ | |
220db18a | 5440 | if (reginfo->strend - locinput < ln |
5ac65bff KW |
5441 | || UCHARAT(s) != nextchr |
5442 | || (ln > 1 && memNE(s, locinput, ln))) | |
5443 | { | |
5444 | sayNO; | |
5445 | } | |
5446 | locinput += ln; | |
5447 | } | |
d6a28714 | 5448 | break; |
95b24440 | 5449 | } |
7016d6eb | 5450 | |
3c0563b9 | 5451 | case EXACTFL: { /* /abc/il */ |
a932d541 | 5452 | re_fold_t folder; |
9a5a5549 KW |
5453 | const U8 * fold_array; |
5454 | const char * s; | |
d513472c | 5455 | U32 fold_utf8_flags; |
9a5a5549 | 5456 | |
780fcc9f | 5457 | _CHECK_AND_WARN_PROBLEMATIC_LOCALE; |
f67f9e53 KW |
5458 | folder = foldEQ_locale; |
5459 | fold_array = PL_fold_locale; | |
cea315b6 | 5460 | fold_utf8_flags = FOLDEQ_LOCALE; |
9a5a5549 KW |
5461 | goto do_exactf; |
5462 | ||
a4525e78 KW |
5463 | case EXACTFLU8: /* /abc/il; but all 'abc' are above 255, so |
5464 | is effectively /u; hence to match, target | |
5465 | must be UTF-8. */ | |
5466 | if (! utf8_target) { | |
5467 | sayNO; | |
5468 | } | |
613abc6d KW |
5469 | fold_utf8_flags = FOLDEQ_LOCALE | FOLDEQ_S1_ALREADY_FOLDED |
5470 | | FOLDEQ_S1_FOLDS_SANE; | |
ec5e0e1c KW |
5471 | folder = foldEQ_latin1; |
5472 | fold_array = PL_fold_latin1; | |
a4525e78 KW |
5473 | goto do_exactf; |
5474 | ||
3c0563b9 | 5475 | case EXACTFU_SS: /* /\x{df}/iu */ |
3c0563b9 | 5476 | case EXACTFU: /* /abc/iu */ |
9a5a5549 KW |
5477 | folder = foldEQ_latin1; |
5478 | fold_array = PL_fold_latin1; | |
984e6dd1 | 5479 | fold_utf8_flags = is_utf8_pat ? FOLDEQ_S1_ALREADY_FOLDED : 0; |
9a5a5549 KW |
5480 | goto do_exactf; |
5481 | ||
098b07d5 KW |
5482 | case EXACTFA_NO_TRIE: /* This node only generated for non-utf8 |
5483 | patterns */ | |
5484 | assert(! is_utf8_pat); | |
924ba076 | 5485 | /* FALLTHROUGH */ |
3c0563b9 | 5486 | case EXACTFA: /* /abc/iaa */ |
2f7f8cb1 KW |
5487 | folder = foldEQ_latin1; |
5488 | fold_array = PL_fold_latin1; | |
57014d77 | 5489 | fold_utf8_flags = FOLDEQ_UTF8_NOMIX_ASCII; |
2f7f8cb1 KW |
5490 | goto do_exactf; |
5491 | ||
2fdb7295 KW |
5492 | case EXACTF: /* /abc/i This node only generated for |
5493 | non-utf8 patterns */ | |
5494 | assert(! is_utf8_pat); | |
9a5a5549 KW |
5495 | folder = foldEQ; |
5496 | fold_array = PL_fold; | |
62bf7766 | 5497 | fold_utf8_flags = 0; |
9a5a5549 KW |
5498 | |
5499 | do_exactf: | |
5500 | s = STRING(scan); | |
24d3c4a9 | 5501 | ln = STR_LEN(scan); |
d6a28714 | 5502 | |
31f05a37 KW |
5503 | if (utf8_target |
5504 | || is_utf8_pat | |
5505 | || state_num == EXACTFU_SS | |
5506 | || (state_num == EXACTFL && IN_UTF8_CTYPE_LOCALE)) | |
5507 | { | |
3c760661 KW |
5508 | /* Either target or the pattern are utf8, or has the issue where |
5509 | * the fold lengths may differ. */ | |
be8e71aa | 5510 | const char * const l = locinput; |
220db18a | 5511 | char *e = reginfo->strend; |
bc517b45 | 5512 | |
984e6dd1 | 5513 | if (! foldEQ_utf8_flags(s, 0, ln, is_utf8_pat, |
fa5b1667 | 5514 | l, &e, 0, utf8_target, fold_utf8_flags)) |
c3e1d013 KW |
5515 | { |
5516 | sayNO; | |
5486206c | 5517 | } |
d07ddd77 | 5518 | locinput = e; |
d07ddd77 | 5519 | break; |
a0ed51b3 | 5520 | } |
d6a28714 | 5521 | |
0a138b74 | 5522 | /* Neither the target nor the pattern are utf8 */ |
1443c94c DM |
5523 | if (UCHARAT(s) != nextchr |
5524 | && !NEXTCHR_IS_EOS | |
5525 | && UCHARAT(s) != fold_array[nextchr]) | |
9a5a5549 | 5526 | { |
a0ed51b3 | 5527 | sayNO; |
9a5a5549 | 5528 | } |
220db18a | 5529 | if (reginfo->strend - locinput < ln) |
b8c5462f | 5530 | sayNO; |
9a5a5549 | 5531 | if (ln > 1 && ! folder(s, locinput, ln)) |
4633a7c4 | 5532 | sayNO; |
24d3c4a9 | 5533 | locinput += ln; |
a0d0e21e | 5534 | break; |
9a5a5549 | 5535 | } |
63ac0dad | 5536 | |
3c0563b9 | 5537 | case NBOUNDL: /* /\B/l */ |
5c388b33 KW |
5538 | to_complement = 1; |
5539 | /* FALLTHROUGH */ | |
5540 | ||
5541 | case BOUNDL: /* /\b/l */ | |
780fcc9f | 5542 | _CHECK_AND_WARN_PROBLEMATIC_LOCALE; |
64935bc6 KW |
5543 | |
5544 | if (FLAGS(scan) != TRADITIONAL_BOUND) { | |
5545 | if (! IN_UTF8_CTYPE_LOCALE) { | |
5546 | Perl_ck_warner(aTHX_ packWARN(WARN_LOCALE), | |
5547 | B_ON_NON_UTF8_LOCALE_IS_WRONG); | |
5548 | } | |
5549 | goto boundu; | |
5550 | } | |
5551 | ||
5c388b33 KW |
5552 | if (utf8_target) { |
5553 | if (locinput == reginfo->strbeg) | |
5554 | ln = isWORDCHAR_LC('\n'); | |
5555 | else { | |
5556 | ln = isWORDCHAR_LC_utf8(reghop3((U8*)locinput, -1, | |
5557 | (U8*)(reginfo->strbeg))); | |
5558 | } | |
5559 | n = (NEXTCHR_IS_EOS) | |
5560 | ? isWORDCHAR_LC('\n') | |
5561 | : isWORDCHAR_LC_utf8((U8*)locinput); | |
5562 | } | |
5563 | else { /* Here the string isn't utf8 */ | |
5564 | ln = (locinput == reginfo->strbeg) | |
5565 | ? isWORDCHAR_LC('\n') | |
5566 | : isWORDCHAR_LC(UCHARAT(locinput - 1)); | |
5567 | n = (NEXTCHR_IS_EOS) | |
5568 | ? isWORDCHAR_LC('\n') | |
5569 | : isWORDCHAR_LC(nextchr); | |
5570 | } | |
5571 | if (to_complement ^ (ln == n)) { | |
5572 | sayNO; | |
5573 | } | |
5574 | break; | |
5575 | ||
5576 | case NBOUND: /* /\B/ */ | |
5577 | to_complement = 1; | |
780fcc9f | 5578 | /* FALLTHROUGH */ |
5c388b33 | 5579 | |
3c0563b9 | 5580 | case BOUND: /* /\b/ */ |
5c388b33 KW |
5581 | if (utf8_target) { |
5582 | goto bound_utf8; | |
5583 | } | |
5584 | goto bound_ascii_match_only; | |
5585 | ||
5586 | case NBOUNDA: /* /\B/a */ | |
5587 | to_complement = 1; | |
5588 | /* FALLTHROUGH */ | |
5589 | ||
3c0563b9 | 5590 | case BOUNDA: /* /\b/a */ |
5c388b33 | 5591 | |
c52b8b12 | 5592 | bound_ascii_match_only: |
5c388b33 KW |
5593 | /* Here the string isn't utf8, or is utf8 and only ascii characters |
5594 | * are to match \w. In the latter case looking at the byte just | |
5595 | * prior to the current one may be just the final byte of a | |
5596 | * multi-byte character. This is ok. There are two cases: | |
5597 | * 1) it is a single byte character, and then the test is doing | |
5598 | * just what it's supposed to. | |
5599 | * 2) it is a multi-byte character, in which case the final byte is | |
5600 | * never mistakable for ASCII, and so the test will say it is | |
5601 | * not a word character, which is the correct answer. */ | |
5602 | ln = (locinput == reginfo->strbeg) | |
5603 | ? isWORDCHAR_A('\n') | |
5604 | : isWORDCHAR_A(UCHARAT(locinput - 1)); | |
5605 | n = (NEXTCHR_IS_EOS) | |
5606 | ? isWORDCHAR_A('\n') | |
5607 | : isWORDCHAR_A(nextchr); | |
5608 | if (to_complement ^ (ln == n)) { | |
5609 | sayNO; | |
5610 | } | |
5611 | break; | |
5612 | ||
3c0563b9 | 5613 | case NBOUNDU: /* /\B/u */ |
5c388b33 KW |
5614 | to_complement = 1; |
5615 | /* FALLTHROUGH */ | |
b2680017 | 5616 | |
5c388b33 | 5617 | case BOUNDU: /* /\b/u */ |
64935bc6 KW |
5618 | |
5619 | boundu: | |
5c388b33 KW |
5620 | if (utf8_target) { |
5621 | ||
64935bc6 KW |
5622 | bound_utf8: |
5623 | switch((bound_type) FLAGS(scan)) { | |
5624 | case TRADITIONAL_BOUND: | |
aa383448 KW |
5625 | ln = (locinput == reginfo->strbeg) |
5626 | ? isWORDCHAR_L1('\n') | |
5627 | : isWORDCHAR_utf8(reghop3((U8*)locinput, -1, | |
5628 | (U8*)(reginfo->strbeg))); | |
5629 | n = (NEXTCHR_IS_EOS) | |
5630 | ? isWORDCHAR_L1('\n') | |
5631 | : isWORDCHAR_utf8((U8*)locinput); | |
b53eee5d | 5632 | match = cBOOL(ln != n); |
64935bc6 KW |
5633 | break; |
5634 | case GCB_BOUND: | |
5635 | if (locinput == reginfo->strbeg || NEXTCHR_IS_EOS) { | |
5636 | match = TRUE; /* GCB always matches at begin and | |
5637 | end */ | |
5638 | } | |
5639 | else { | |
5640 | /* Find the gcb values of previous and current | |
5641 | * chars, then see if is a break point */ | |
5642 | match = isGCB(getGCB_VAL_UTF8( | |
5643 | reghop3((U8*)locinput, | |
5644 | -1, | |
5645 | (U8*)(reginfo->strbeg)), | |
5646 | (U8*) reginfo->strend), | |
5647 | getGCB_VAL_UTF8((U8*) locinput, | |
5648 | (U8*) reginfo->strend)); | |
5649 | } | |
5650 | break; | |
06ae2722 KW |
5651 | |
5652 | case SB_BOUND: /* Always matches at begin and end */ | |
5653 | if (locinput == reginfo->strbeg || NEXTCHR_IS_EOS) { | |
5654 | match = TRUE; | |
5655 | } | |
5656 | else { | |
5657 | match = isSB(getSB_VAL_UTF8( | |
5658 | reghop3((U8*)locinput, | |
5659 | -1, | |
5660 | (U8*)(reginfo->strbeg)), | |
5661 | (U8*) reginfo->strend), | |
5662 | getSB_VAL_UTF8((U8*) locinput, | |
5663 | (U8*) reginfo->strend), | |
5664 | (U8*) reginfo->strbeg, | |
5665 | (U8*) locinput, | |
5666 | (U8*) reginfo->strend, | |
5667 | utf8_target); | |
5668 | } | |
5669 | break; | |
5670 | ||
ae3bb8ea KW |
5671 | case WB_BOUND: |
5672 | if (locinput == reginfo->strbeg || NEXTCHR_IS_EOS) { | |
5673 | match = TRUE; | |
5674 | } | |
5675 | else { | |
85e5f08b | 5676 | match = isWB(WB_UNKNOWN, |
ae3bb8ea KW |
5677 | getWB_VAL_UTF8( |
5678 | reghop3((U8*)locinput, | |
5679 | -1, | |
5680 | (U8*)(reginfo->strbeg)), | |
5681 | (U8*) reginfo->strend), | |
5682 | getWB_VAL_UTF8((U8*) locinput, | |
5683 | (U8*) reginfo->strend), | |
5684 | (U8*) reginfo->strbeg, | |
5685 | (U8*) locinput, | |
5686 | (U8*) reginfo->strend, | |
5687 | utf8_target); | |
5688 | } | |
5689 | break; | |
64935bc6 | 5690 | } |
b2680017 | 5691 | } |
64935bc6 KW |
5692 | else { /* Not utf8 target */ |
5693 | switch((bound_type) FLAGS(scan)) { | |
5694 | case TRADITIONAL_BOUND: | |
aa383448 KW |
5695 | ln = (locinput == reginfo->strbeg) |
5696 | ? isWORDCHAR_L1('\n') | |
5697 | : isWORDCHAR_L1(UCHARAT(locinput - 1)); | |
5698 | n = (NEXTCHR_IS_EOS) | |
5699 | ? isWORDCHAR_L1('\n') | |
5700 | : isWORDCHAR_L1(nextchr); | |
b53eee5d | 5701 | match = cBOOL(ln != n); |
64935bc6 | 5702 | break; |
cfaf538b | 5703 | |
64935bc6 KW |
5704 | case GCB_BOUND: |
5705 | if (locinput == reginfo->strbeg || NEXTCHR_IS_EOS) { | |
5706 | match = TRUE; /* GCB always matches at begin and | |
5707 | end */ | |
5708 | } | |
5709 | else { /* Only CR-LF combo isn't a GCB in 0-255 | |
5710 | range */ | |
5711 | match = UCHARAT(locinput - 1) != '\r' | |
5712 | || UCHARAT(locinput) != '\n'; | |
5713 | } | |
5714 | break; | |
06ae2722 KW |
5715 | |
5716 | case SB_BOUND: /* Always matches at begin and end */ | |
5717 | if (locinput == reginfo->strbeg || NEXTCHR_IS_EOS) { | |
5718 | match = TRUE; | |
5719 | } | |
5720 | else { | |
5721 | match = isSB(getSB_VAL_CP(UCHARAT(locinput -1)), | |
5722 | getSB_VAL_CP(UCHARAT(locinput)), | |
5723 | (U8*) reginfo->strbeg, | |
5724 | (U8*) locinput, | |
5725 | (U8*) reginfo->strend, | |
5726 | utf8_target); | |
5727 | } | |
5728 | break; | |
5729 | ||
ae3bb8ea KW |
5730 | case WB_BOUND: |
5731 | if (locinput == reginfo->strbeg || NEXTCHR_IS_EOS) { | |
5732 | match = TRUE; | |
5733 | } | |
5734 | else { | |
85e5f08b | 5735 | match = isWB(WB_UNKNOWN, |
ae3bb8ea KW |
5736 | getWB_VAL_CP(UCHARAT(locinput -1)), |
5737 | getWB_VAL_CP(UCHARAT(locinput)), | |
5738 | (U8*) reginfo->strbeg, | |
5739 | (U8*) locinput, | |
5740 | (U8*) reginfo->strend, | |
5741 | utf8_target); | |
5742 | } | |
5743 | break; | |
64935bc6 | 5744 | } |
b2680017 | 5745 | } |
5c388b33 | 5746 | |
64935bc6 | 5747 | if (to_complement ^ ! match) { |
5c388b33 KW |
5748 | sayNO; |
5749 | } | |
b2680017 | 5750 | break; |
3c0563b9 | 5751 | |
a4525e78 | 5752 | case ANYOFL: /* /[abc]/l */ |
780fcc9f KW |
5753 | _CHECK_AND_WARN_PROBLEMATIC_LOCALE; |
5754 | /* FALLTHROUGH */ | |
a4525e78 | 5755 | case ANYOF: /* /[abc]/ */ |
7016d6eb DM |
5756 | if (NEXTCHR_IS_EOS) |
5757 | sayNO; | |
e0193e47 | 5758 | if (utf8_target) { |
3db24e1e KW |
5759 | if (!reginclass(rex, scan, (U8*)locinput, (U8*)reginfo->strend, |
5760 | utf8_target)) | |
09b08e9b | 5761 | sayNO; |
635cd5d4 | 5762 | locinput += UTF8SKIP(locinput); |
ffc61ed2 JH |
5763 | } |
5764 | else { | |
20ed0b26 | 5765 | if (!REGINCLASS(rex, scan, (U8*)locinput)) |
09b08e9b | 5766 | sayNO; |
3640db6b | 5767 | locinput++; |
e0f9d4a8 | 5768 | } |
b8c5462f | 5769 | break; |
3c0563b9 | 5770 | |
3018b823 KW |
5771 | /* The argument (FLAGS) to all the POSIX node types is the class number |
5772 | * */ | |
ee9a90b8 | 5773 | |
3018b823 KW |
5774 | case NPOSIXL: /* \W or [:^punct:] etc. under /l */ |
5775 | to_complement = 1; | |
5776 | /* FALLTHROUGH */ | |
5777 | ||
5778 | case POSIXL: /* \w or [:punct:] etc. under /l */ | |
780fcc9f | 5779 | _CHECK_AND_WARN_PROBLEMATIC_LOCALE; |
3018b823 | 5780 | if (NEXTCHR_IS_EOS) |
bedac28b | 5781 | sayNO; |
bedac28b | 5782 | |
3018b823 KW |
5783 | /* Use isFOO_lc() for characters within Latin1. (Note that |
5784 | * UTF8_IS_INVARIANT works even on non-UTF-8 strings, or else | |
5785 | * wouldn't be invariant) */ | |
5786 | if (UTF8_IS_INVARIANT(nextchr) || ! utf8_target) { | |
eb4e9c04 | 5787 | if (! (to_complement ^ cBOOL(isFOO_lc(FLAGS(scan), (U8) nextchr)))) { |
bedac28b KW |
5788 | sayNO; |
5789 | } | |
5790 | } | |
3018b823 KW |
5791 | else if (UTF8_IS_DOWNGRADEABLE_START(nextchr)) { |
5792 | if (! (to_complement ^ cBOOL(isFOO_lc(FLAGS(scan), | |
94bb8c36 | 5793 | (U8) TWO_BYTE_UTF8_TO_NATIVE(nextchr, |
3018b823 KW |
5794 | *(locinput + 1)))))) |
5795 | { | |
bedac28b | 5796 | sayNO; |
3018b823 | 5797 | } |
bedac28b | 5798 | } |
3018b823 | 5799 | else { /* Here, must be an above Latin-1 code point */ |
613abc6d | 5800 | _CHECK_AND_OUTPUT_WIDE_LOCALE_UTF8_MSG(locinput, reginfo->strend); |
01f55654 | 5801 | goto utf8_posix_above_latin1; |
bedac28b | 5802 | } |
3018b823 KW |
5803 | |
5804 | /* Here, must be utf8 */ | |
5805 | locinput += UTF8SKIP(locinput); | |
bedac28b KW |
5806 | break; |
5807 | ||
3018b823 KW |
5808 | case NPOSIXD: /* \W or [:^punct:] etc. under /d */ |
5809 | to_complement = 1; | |
5810 | /* FALLTHROUGH */ | |
5811 | ||
5812 | case POSIXD: /* \w or [:punct:] etc. under /d */ | |
bedac28b | 5813 | if (utf8_target) { |
3018b823 | 5814 | goto utf8_posix; |
bedac28b | 5815 | } |
3018b823 KW |
5816 | goto posixa; |
5817 | ||
5818 | case NPOSIXA: /* \W or [:^punct:] etc. under /a */ | |
bedac28b | 5819 | |
3018b823 | 5820 | if (NEXTCHR_IS_EOS) { |
bedac28b KW |
5821 | sayNO; |
5822 | } | |
bedac28b | 5823 | |
3018b823 KW |
5824 | /* All UTF-8 variants match */ |
5825 | if (! UTF8_IS_INVARIANT(nextchr)) { | |
5826 | goto increment_locinput; | |
bedac28b | 5827 | } |
ee9a90b8 | 5828 | |
3018b823 KW |
5829 | to_complement = 1; |
5830 | /* FALLTHROUGH */ | |
5831 | ||
5832 | case POSIXA: /* \w or [:punct:] etc. under /a */ | |
5833 | ||
5834 | posixa: | |
5835 | /* We get here through POSIXD, NPOSIXD, and NPOSIXA when not in | |
5836 | * UTF-8, and also from NPOSIXA even in UTF-8 when the current | |
5837 | * character is a single byte */ | |
20d0b1e9 | 5838 | |
3018b823 KW |
5839 | if (NEXTCHR_IS_EOS |
5840 | || ! (to_complement ^ cBOOL(_generic_isCC_A(nextchr, | |
5841 | FLAGS(scan))))) | |
5842 | { | |
0658cdde KW |
5843 | sayNO; |
5844 | } | |
3018b823 KW |
5845 | |
5846 | /* Here we are either not in utf8, or we matched a utf8-invariant, | |
5847 | * so the next char is the next byte */ | |
3640db6b | 5848 | locinput++; |
0658cdde | 5849 | break; |
3c0563b9 | 5850 | |
3018b823 KW |
5851 | case NPOSIXU: /* \W or [:^punct:] etc. under /u */ |
5852 | to_complement = 1; | |
5853 | /* FALLTHROUGH */ | |
5854 | ||
5855 | case POSIXU: /* \w or [:punct:] etc. under /u */ | |
5856 | utf8_posix: | |
5857 | if (NEXTCHR_IS_EOS) { | |
0658cdde KW |
5858 | sayNO; |
5859 | } | |
3018b823 KW |
5860 | |
5861 | /* Use _generic_isCC() for characters within Latin1. (Note that | |
5862 | * UTF8_IS_INVARIANT works even on non-UTF-8 strings, or else | |
5863 | * wouldn't be invariant) */ | |
5864 | if (UTF8_IS_INVARIANT(nextchr) || ! utf8_target) { | |
5865 | if (! (to_complement ^ cBOOL(_generic_isCC(nextchr, | |
5866 | FLAGS(scan))))) | |
5867 | { | |
5868 | sayNO; | |
5869 | } | |
5870 | locinput++; | |
5871 | } | |
5872 | else if (UTF8_IS_DOWNGRADEABLE_START(nextchr)) { | |
5873 | if (! (to_complement | |
94bb8c36 | 5874 | ^ cBOOL(_generic_isCC(TWO_BYTE_UTF8_TO_NATIVE(nextchr, |
3018b823 | 5875 | *(locinput + 1)), |
94bb8c36 | 5876 | FLAGS(scan))))) |
3018b823 KW |
5877 | { |
5878 | sayNO; | |
5879 | } | |
5880 | locinput += 2; | |
5881 | } | |
5882 | else { /* Handle above Latin-1 code points */ | |
c52b8b12 | 5883 | utf8_posix_above_latin1: |
3018b823 KW |
5884 | classnum = (_char_class_number) FLAGS(scan); |
5885 | if (classnum < _FIRST_NON_SWASH_CC) { | |
5886 | ||
5887 | /* Here, uses a swash to find such code points. Load if if | |
5888 | * not done already */ | |
5889 | if (! PL_utf8_swash_ptrs[classnum]) { | |
5890 | U8 flags = _CORE_SWASH_INIT_ACCEPT_INVLIST; | |
5891 | PL_utf8_swash_ptrs[classnum] | |
5892 | = _core_swash_init("utf8", | |
2a16ac92 KW |
5893 | "", |
5894 | &PL_sv_undef, 1, 0, | |
5895 | PL_XPosix_ptrs[classnum], &flags); | |
3018b823 KW |
5896 | } |
5897 | if (! (to_complement | |
5898 | ^ cBOOL(swash_fetch(PL_utf8_swash_ptrs[classnum], | |
5899 | (U8 *) locinput, TRUE)))) | |
5900 | { | |
5901 | sayNO; | |
5902 | } | |
5903 | } | |
5904 | else { /* Here, uses macros to find above Latin-1 code points */ | |
5905 | switch (classnum) { | |
779cf272 | 5906 | case _CC_ENUM_SPACE: |
3018b823 KW |
5907 | if (! (to_complement |
5908 | ^ cBOOL(is_XPERLSPACE_high(locinput)))) | |
5909 | { | |
5910 | sayNO; | |
5911 | } | |
5912 | break; | |
5913 | case _CC_ENUM_BLANK: | |
5914 | if (! (to_complement | |
5915 | ^ cBOOL(is_HORIZWS_high(locinput)))) | |
5916 | { | |
5917 | sayNO; | |
5918 | } | |
5919 | break; | |
5920 | case _CC_ENUM_XDIGIT: | |
5921 | if (! (to_complement | |
5922 | ^ cBOOL(is_XDIGIT_high(locinput)))) | |
5923 | { | |
5924 | sayNO; | |
5925 | } | |
5926 | break; | |
5927 | case _CC_ENUM_VERTSPACE: | |
5928 | if (! (to_complement | |
5929 | ^ cBOOL(is_VERTWS_high(locinput)))) | |
5930 | { | |
5931 | sayNO; | |
5932 | } | |
5933 | break; | |
5934 | default: /* The rest, e.g. [:cntrl:], can't match | |
5935 | above Latin1 */ | |
5936 | if (! to_complement) { | |
5937 | sayNO; | |
5938 | } | |
5939 | break; | |
5940 | } | |
5941 | } | |
5942 | locinput += UTF8SKIP(locinput); | |
5943 | } | |
5944 | break; | |
0658cdde | 5945 | |
37e2e78e KW |
5946 | case CLUMP: /* Match \X: logical Unicode character. This is defined as |
5947 | a Unicode extended Grapheme Cluster */ | |
7016d6eb | 5948 | if (NEXTCHR_IS_EOS) |
a0ed51b3 | 5949 | sayNO; |
f2ed9b32 | 5950 | if (! utf8_target) { |
37e2e78e KW |
5951 | |
5952 | /* Match either CR LF or '.', as all the other possibilities | |
5953 | * require utf8 */ | |
5954 | locinput++; /* Match the . or CR */ | |
cc3b396d KW |
5955 | if (nextchr == '\r' /* And if it was CR, and the next is LF, |
5956 | match the LF */ | |
220db18a | 5957 | && locinput < reginfo->strend |
e699a1d5 KW |
5958 | && UCHARAT(locinput) == '\n') |
5959 | { | |
5960 | locinput++; | |
5961 | } | |
37e2e78e KW |
5962 | } |
5963 | else { | |
5964 | ||
64935bc6 | 5965 | /* Get the gcb type for the current character */ |
85e5f08b | 5966 | GCB_enum prev_gcb = getGCB_VAL_UTF8((U8*) locinput, |
64935bc6 | 5967 | (U8*) reginfo->strend); |
37e2e78e | 5968 | |
64935bc6 KW |
5969 | /* Then scan through the input until we get to the first |
5970 | * character whose type is supposed to be a gcb with the | |
5971 | * current character. (There is always a break at the | |
5972 | * end-of-input) */ | |
5973 | locinput += UTF8SKIP(locinput); | |
5974 | while (locinput < reginfo->strend) { | |
85e5f08b | 5975 | GCB_enum cur_gcb = getGCB_VAL_UTF8((U8*) locinput, |
64935bc6 KW |
5976 | (U8*) reginfo->strend); |
5977 | if (isGCB(prev_gcb, cur_gcb)) { | |
5978 | break; | |
27d4fc33 | 5979 | } |
11dfcd49 | 5980 | |
64935bc6 KW |
5981 | prev_gcb = cur_gcb; |
5982 | locinput += UTF8SKIP(locinput); | |
5983 | } | |
37e2e78e | 5984 | |
37e2e78e | 5985 | |
37e2e78e | 5986 | } |
a0ed51b3 | 5987 | break; |
81714fb9 | 5988 | |
3c0563b9 | 5989 | case NREFFL: /* /\g{name}/il */ |
d7ef4b73 KW |
5990 | { /* The capture buffer cases. The ones beginning with N for the |
5991 | named buffers just convert to the equivalent numbered and | |
5992 | pretend they were called as the corresponding numbered buffer | |
5993 | op. */ | |
26ecd678 TC |
5994 | /* don't initialize these in the declaration, it makes C++ |
5995 | unhappy */ | |
9d9163fb | 5996 | const char *s; |
ff1157ca | 5997 | char type; |
8368298a TC |
5998 | re_fold_t folder; |
5999 | const U8 *fold_array; | |
26ecd678 | 6000 | UV utf8_fold_flags; |
8368298a | 6001 | |
780fcc9f | 6002 | _CHECK_AND_WARN_PROBLEMATIC_LOCALE; |
d7ef4b73 KW |
6003 | folder = foldEQ_locale; |
6004 | fold_array = PL_fold_locale; | |
6005 | type = REFFL; | |
cea315b6 | 6006 | utf8_fold_flags = FOLDEQ_LOCALE; |
d7ef4b73 KW |
6007 | goto do_nref; |
6008 | ||
3c0563b9 | 6009 | case NREFFA: /* /\g{name}/iaa */ |
2f7f8cb1 KW |
6010 | folder = foldEQ_latin1; |
6011 | fold_array = PL_fold_latin1; | |
6012 | type = REFFA; | |
6013 | utf8_fold_flags = FOLDEQ_UTF8_NOMIX_ASCII; | |
6014 | goto do_nref; | |
6015 | ||
3c0563b9 | 6016 | case NREFFU: /* /\g{name}/iu */ |
d7ef4b73 KW |
6017 | folder = foldEQ_latin1; |
6018 | fold_array = PL_fold_latin1; | |
6019 | type = REFFU; | |
d513472c | 6020 | utf8_fold_flags = 0; |
d7ef4b73 KW |
6021 | goto do_nref; |
6022 | ||
3c0563b9 | 6023 | case NREFF: /* /\g{name}/i */ |
d7ef4b73 KW |
6024 | folder = foldEQ; |
6025 | fold_array = PL_fold; | |
6026 | type = REFF; | |
d513472c | 6027 | utf8_fold_flags = 0; |
d7ef4b73 KW |
6028 | goto do_nref; |
6029 | ||
3c0563b9 | 6030 | case NREF: /* /\g{name}/ */ |
d7ef4b73 | 6031 | type = REF; |
83d7b90b KW |
6032 | folder = NULL; |
6033 | fold_array = NULL; | |
d513472c | 6034 | utf8_fold_flags = 0; |
d7ef4b73 KW |
6035 | do_nref: |
6036 | ||
6037 | /* For the named back references, find the corresponding buffer | |
6038 | * number */ | |
0a4db386 YO |
6039 | n = reg_check_named_buff_matched(rex,scan); |
6040 | ||
d7ef4b73 | 6041 | if ( ! n ) { |
81714fb9 | 6042 | sayNO; |
d7ef4b73 KW |
6043 | } |
6044 | goto do_nref_ref_common; | |
6045 | ||
3c0563b9 | 6046 | case REFFL: /* /\1/il */ |
780fcc9f | 6047 | _CHECK_AND_WARN_PROBLEMATIC_LOCALE; |
d7ef4b73 KW |
6048 | folder = foldEQ_locale; |
6049 | fold_array = PL_fold_locale; | |
cea315b6 | 6050 | utf8_fold_flags = FOLDEQ_LOCALE; |
d7ef4b73 KW |
6051 | goto do_ref; |
6052 | ||
3c0563b9 | 6053 | case REFFA: /* /\1/iaa */ |
2f7f8cb1 KW |
6054 | folder = foldEQ_latin1; |
6055 | fold_array = PL_fold_latin1; | |
6056 | utf8_fold_flags = FOLDEQ_UTF8_NOMIX_ASCII; | |
6057 | goto do_ref; | |
6058 | ||
3c0563b9 | 6059 | case REFFU: /* /\1/iu */ |
d7ef4b73 KW |
6060 | folder = foldEQ_latin1; |
6061 | fold_array = PL_fold_latin1; | |
d513472c | 6062 | utf8_fold_flags = 0; |
d7ef4b73 KW |
6063 | goto do_ref; |
6064 | ||
3c0563b9 | 6065 | case REFF: /* /\1/i */ |
d7ef4b73 KW |
6066 | folder = foldEQ; |
6067 | fold_array = PL_fold; | |
d513472c | 6068 | utf8_fold_flags = 0; |
83d7b90b | 6069 | goto do_ref; |
d7ef4b73 | 6070 | |
3c0563b9 | 6071 | case REF: /* /\1/ */ |
83d7b90b KW |
6072 | folder = NULL; |
6073 | fold_array = NULL; | |
d513472c | 6074 | utf8_fold_flags = 0; |
83d7b90b | 6075 | |
d7ef4b73 | 6076 | do_ref: |
81714fb9 | 6077 | type = OP(scan); |
d7ef4b73 KW |
6078 | n = ARG(scan); /* which paren pair */ |
6079 | ||
6080 | do_nref_ref_common: | |
b93070ed | 6081 | ln = rex->offs[n].start; |
1cb48e53 | 6082 | reginfo->poscache_iter = reginfo->poscache_maxiter; /* Void cache */ |
b93070ed | 6083 | if (rex->lastparen < n || ln == -1) |
af3f8c16 | 6084 | sayNO; /* Do not match unless seen CLOSEn. */ |
b93070ed | 6085 | if (ln == rex->offs[n].end) |
a0d0e21e | 6086 | break; |
a0ed51b3 | 6087 | |
9d9163fb | 6088 | s = reginfo->strbeg + ln; |
d7ef4b73 | 6089 | if (type != REF /* REF can do byte comparison */ |
31f05a37 KW |
6090 | && (utf8_target || type == REFFU || type == REFFL)) |
6091 | { | |
220db18a | 6092 | char * limit = reginfo->strend; |
d7ef4b73 KW |
6093 | |
6094 | /* This call case insensitively compares the entire buffer | |
6095 | * at s, with the current input starting at locinput, but | |
220db18a DM |
6096 | * not going off the end given by reginfo->strend, and |
6097 | * returns in <limit> upon success, how much of the | |
6098 | * current input was matched */ | |
b93070ed | 6099 | if (! foldEQ_utf8_flags(s, NULL, rex->offs[n].end - ln, utf8_target, |
d513472c | 6100 | locinput, &limit, 0, utf8_target, utf8_fold_flags)) |
d7ef4b73 KW |
6101 | { |
6102 | sayNO; | |
a0ed51b3 | 6103 | } |
d7ef4b73 | 6104 | locinput = limit; |
a0ed51b3 LW |
6105 | break; |
6106 | } | |
6107 | ||
d7ef4b73 | 6108 | /* Not utf8: Inline the first character, for speed. */ |
7016d6eb DM |
6109 | if (!NEXTCHR_IS_EOS && |
6110 | UCHARAT(s) != nextchr && | |
81714fb9 | 6111 | (type == REF || |
d7ef4b73 | 6112 | UCHARAT(s) != fold_array[nextchr])) |
4633a7c4 | 6113 | sayNO; |
b93070ed | 6114 | ln = rex->offs[n].end - ln; |
220db18a | 6115 | if (locinput + ln > reginfo->strend) |
4633a7c4 | 6116 | sayNO; |
81714fb9 | 6117 | if (ln > 1 && (type == REF |
24d3c4a9 | 6118 | ? memNE(s, locinput, ln) |
d7ef4b73 | 6119 | : ! folder(s, locinput, ln))) |
4633a7c4 | 6120 | sayNO; |
24d3c4a9 | 6121 | locinput += ln; |
a0d0e21e | 6122 | break; |
81714fb9 | 6123 | } |
3c0563b9 DM |
6124 | |
6125 | case NOTHING: /* null op; e.g. the 'nothing' following | |
6126 | * the '*' in m{(a+|b)*}' */ | |
6127 | break; | |
6128 | case TAIL: /* placeholder while compiling (A|B|C) */ | |
a0d0e21e | 6129 | break; |
3c0563b9 | 6130 | |
40a82448 DM |
6131 | #undef ST |
6132 | #define ST st->u.eval | |
c277df42 | 6133 | { |
c277df42 | 6134 | SV *ret; |
d2f13c59 | 6135 | REGEXP *re_sv; |
6bda09f9 | 6136 | regexp *re; |
f8fc2ecf | 6137 | regexp_internal *rei; |
1a147d38 YO |
6138 | regnode *startpoint; |
6139 | ||
3c0563b9 | 6140 | case GOSTART: /* (?R) */ |
e7707071 YO |
6141 | case GOSUB: /* /(...(?1))/ /(...(?&foo))/ */ |
6142 | if (cur_eval && cur_eval->locinput==locinput) { | |
24b23f37 | 6143 | if (cur_eval->u.eval.close_paren == (U32)ARG(scan)) |
1a147d38 | 6144 | Perl_croak(aTHX_ "Infinite recursion in regex"); |
4b196cd4 | 6145 | if ( ++nochange_depth > max_nochange_depth ) |
1a147d38 YO |
6146 | Perl_croak(aTHX_ |
6147 | "Pattern subroutine nesting without pos change" | |
6148 | " exceeded limit in regex"); | |
6bda09f9 YO |
6149 | } else { |
6150 | nochange_depth = 0; | |
1a147d38 | 6151 | } |
288b8c02 | 6152 | re_sv = rex_sv; |
6bda09f9 | 6153 | re = rex; |
f8fc2ecf | 6154 | rei = rexi; |
1a147d38 | 6155 | if (OP(scan)==GOSUB) { |
6bda09f9 YO |
6156 | startpoint = scan + ARG2L(scan); |
6157 | ST.close_paren = ARG(scan); | |
6158 | } else { | |
f8fc2ecf | 6159 | startpoint = rei->program+1; |
6bda09f9 YO |
6160 | ST.close_paren = 0; |
6161 | } | |
d1b2014a YO |
6162 | |
6163 | /* Save all the positions seen so far. */ | |
6164 | ST.cp = regcppush(rex, 0, maxopenparen); | |
6165 | REGCP_SET(ST.lastcp); | |
6166 | ||
6167 | /* and then jump to the code we share with EVAL */ | |
6bda09f9 | 6168 | goto eval_recurse_doit; |
a74ff37d | 6169 | /* NOTREACHED */ |
3c0563b9 | 6170 | |
6bda09f9 YO |
6171 | case EVAL: /* /(?{A})B/ /(??{A})B/ and /(?(?{A})X|Y)B/ */ |
6172 | if (cur_eval && cur_eval->locinput==locinput) { | |
4b196cd4 | 6173 | if ( ++nochange_depth > max_nochange_depth ) |
1a147d38 | 6174 | Perl_croak(aTHX_ "EVAL without pos change exceeded limit in regex"); |
6bda09f9 YO |
6175 | } else { |
6176 | nochange_depth = 0; | |
6177 | } | |
8e5e9ebe | 6178 | { |
4aabdb9b | 6179 | /* execute the code in the {...} */ |
81ed78b2 | 6180 | |
4aabdb9b | 6181 | dSP; |
a6dc34f1 | 6182 | IV before; |
1f4d1a1e | 6183 | OP * const oop = PL_op; |
4aabdb9b | 6184 | COP * const ocurcop = PL_curcop; |
81ed78b2 | 6185 | OP *nop; |
81ed78b2 | 6186 | CV *newcv; |
91332126 | 6187 | |
74088413 | 6188 | /* save *all* paren positions */ |
92da3157 | 6189 | regcppush(rex, 0, maxopenparen); |
74088413 DM |
6190 | REGCP_SET(runops_cp); |
6191 | ||
81ed78b2 DM |
6192 | if (!caller_cv) |
6193 | caller_cv = find_runcv(NULL); | |
6194 | ||
4aabdb9b | 6195 | n = ARG(scan); |
81ed78b2 | 6196 | |
b30fcab9 | 6197 | if (rexi->data->what[n] == 'r') { /* code from an external qr */ |
8d919b0a | 6198 | newcv = (ReANY( |
b30fcab9 DM |
6199 | (REGEXP*)(rexi->data->data[n]) |
6200 | ))->qr_anoncv | |
81ed78b2 DM |
6201 | ; |
6202 | nop = (OP*)rexi->data->data[n+1]; | |
b30fcab9 DM |
6203 | } |
6204 | else if (rexi->data->what[n] == 'l') { /* literal code */ | |
81ed78b2 DM |
6205 | newcv = caller_cv; |
6206 | nop = (OP*)rexi->data->data[n]; | |
6207 | assert(CvDEPTH(newcv)); | |
68e2671b DM |
6208 | } |
6209 | else { | |
d24ca0c5 DM |
6210 | /* literal with own CV */ |
6211 | assert(rexi->data->what[n] == 'L'); | |
81ed78b2 DM |
6212 | newcv = rex->qr_anoncv; |
6213 | nop = (OP*)rexi->data->data[n]; | |
68e2671b | 6214 | } |
81ed78b2 | 6215 | |
0e458318 DM |
6216 | /* normally if we're about to execute code from the same |
6217 | * CV that we used previously, we just use the existing | |
6218 | * CX stack entry. However, its possible that in the | |
6219 | * meantime we may have backtracked, popped from the save | |
6220 | * stack, and undone the SAVECOMPPAD(s) associated with | |
6221 | * PUSH_MULTICALL; in which case PL_comppad no longer | |
6222 | * points to newcv's pad. */ | |
6223 | if (newcv != last_pushed_cv || PL_comppad != last_pad) | |
6224 | { | |
b0065247 DM |
6225 | U8 flags = (CXp_SUB_RE | |
6226 | ((newcv == caller_cv) ? CXp_SUB_RE_FAKE : 0)); | |
0e458318 | 6227 | if (last_pushed_cv) { |
b0065247 | 6228 | CHANGE_MULTICALL_FLAGS(newcv, flags); |
0e458318 DM |
6229 | } |
6230 | else { | |
b0065247 | 6231 | PUSH_MULTICALL_FLAGS(newcv, flags); |
0e458318 DM |
6232 | } |
6233 | last_pushed_cv = newcv; | |
6234 | } | |
c31ee3bb DM |
6235 | else { |
6236 | /* these assignments are just to silence compiler | |
6237 | * warnings */ | |
6238 | multicall_cop = NULL; | |
6239 | newsp = NULL; | |
6240 | } | |
0e458318 DM |
6241 | last_pad = PL_comppad; |
6242 | ||
2e2e3f36 DM |
6243 | /* the initial nextstate you would normally execute |
6244 | * at the start of an eval (which would cause error | |
6245 | * messages to come from the eval), may be optimised | |
6246 | * away from the execution path in the regex code blocks; | |
6247 | * so manually set PL_curcop to it initially */ | |
6248 | { | |
81ed78b2 | 6249 | OP *o = cUNOPx(nop)->op_first; |
2e2e3f36 DM |
6250 | assert(o->op_type == OP_NULL); |
6251 | if (o->op_targ == OP_SCOPE) { | |
6252 | o = cUNOPo->op_first; | |
6253 | } | |
6254 | else { | |
6255 | assert(o->op_targ == OP_LEAVE); | |
6256 | o = cUNOPo->op_first; | |
6257 | assert(o->op_type == OP_ENTER); | |
e6dae479 | 6258 | o = OpSIBLING(o); |
2e2e3f36 DM |
6259 | } |
6260 | ||
6261 | if (o->op_type != OP_STUB) { | |
6262 | assert( o->op_type == OP_NEXTSTATE | |
6263 | || o->op_type == OP_DBSTATE | |
6264 | || (o->op_type == OP_NULL | |
6265 | && ( o->op_targ == OP_NEXTSTATE | |
6266 | || o->op_targ == OP_DBSTATE | |
6267 | ) | |
6268 | ) | |
6269 | ); | |
6270 | PL_curcop = (COP*)o; | |
6271 | } | |
6272 | } | |
81ed78b2 | 6273 | nop = nop->op_next; |
2e2e3f36 | 6274 | |
24b23f37 | 6275 | DEBUG_STATE_r( PerlIO_printf(Perl_debug_log, |
81ed78b2 DM |
6276 | " re EVAL PL_op=0x%"UVxf"\n", PTR2UV(nop)) ); |
6277 | ||
8adc0f72 | 6278 | rex->offs[0].end = locinput - reginfo->strbeg; |
bf2039a9 | 6279 | if (reginfo->info_aux_eval->pos_magic) |
25fdce4a FC |
6280 | MgBYTEPOS_set(reginfo->info_aux_eval->pos_magic, |
6281 | reginfo->sv, reginfo->strbeg, | |
6282 | locinput - reginfo->strbeg); | |
4aabdb9b | 6283 | |
2bf803e2 YO |
6284 | if (sv_yes_mark) { |
6285 | SV *sv_mrk = get_sv("REGMARK", 1); | |
6286 | sv_setsv(sv_mrk, sv_yes_mark); | |
6287 | } | |
6288 | ||
81ed78b2 DM |
6289 | /* we don't use MULTICALL here as we want to call the |
6290 | * first op of the block of interest, rather than the | |
6291 | * first op of the sub */ | |
a6dc34f1 | 6292 | before = (IV)(SP-PL_stack_base); |
81ed78b2 | 6293 | PL_op = nop; |
8e5e9ebe RGS |
6294 | CALLRUNOPS(aTHX); /* Scalar context. */ |
6295 | SPAGAIN; | |
a6dc34f1 | 6296 | if ((IV)(SP-PL_stack_base) == before) |
075aa684 | 6297 | ret = &PL_sv_undef; /* protect against empty (?{}) blocks. */ |
8e5e9ebe RGS |
6298 | else { |
6299 | ret = POPs; | |
6300 | PUTBACK; | |
6301 | } | |
4aabdb9b | 6302 | |
e4bfbed3 DM |
6303 | /* before restoring everything, evaluate the returned |
6304 | * value, so that 'uninit' warnings don't use the wrong | |
497d0a96 DM |
6305 | * PL_op or pad. Also need to process any magic vars |
6306 | * (e.g. $1) *before* parentheses are restored */ | |
e4bfbed3 DM |
6307 | |
6308 | PL_op = NULL; | |
6309 | ||
5e98dac2 | 6310 | re_sv = NULL; |
e4bfbed3 DM |
6311 | if (logical == 0) /* (?{})/ */ |
6312 | sv_setsv(save_scalar(PL_replgv), ret); /* $^R */ | |
6313 | else if (logical == 1) { /* /(?(?{...})X|Y)/ */ | |
6314 | sw = cBOOL(SvTRUE(ret)); | |
6315 | logical = 0; | |
6316 | } | |
6317 | else { /* /(??{}) */ | |
497d0a96 DM |
6318 | /* if its overloaded, let the regex compiler handle |
6319 | * it; otherwise extract regex, or stringify */ | |
237da807 | 6320 | if (SvGMAGICAL(ret)) |
2685dc2d | 6321 | ret = sv_mortalcopy(ret); |
497d0a96 DM |
6322 | if (!SvAMAGIC(ret)) { |
6323 | SV *sv = ret; | |
6324 | if (SvROK(sv)) | |
6325 | sv = SvRV(sv); | |
6326 | if (SvTYPE(sv) == SVt_REGEXP) | |
6327 | re_sv = (REGEXP*) sv; | |
63620942 FC |
6328 | else if (SvSMAGICAL(ret)) { |
6329 | MAGIC *mg = mg_find(ret, PERL_MAGIC_qr); | |
497d0a96 DM |
6330 | if (mg) |
6331 | re_sv = (REGEXP *) mg->mg_obj; | |
6332 | } | |
e4bfbed3 | 6333 | |
2685dc2d | 6334 | /* force any undef warnings here */ |
237da807 FC |
6335 | if (!re_sv && !SvPOK(ret) && !SvNIOK(ret)) { |
6336 | ret = sv_mortalcopy(ret); | |
497d0a96 DM |
6337 | (void) SvPV_force_nolen(ret); |
6338 | } | |
e4bfbed3 DM |
6339 | } |
6340 | ||
6341 | } | |
6342 | ||
81ed78b2 DM |
6343 | /* *** Note that at this point we don't restore |
6344 | * PL_comppad, (or pop the CxSUB) on the assumption it may | |
6345 | * be used again soon. This is safe as long as nothing | |
6346 | * in the regexp code uses the pad ! */ | |
4aabdb9b | 6347 | PL_op = oop; |
4aabdb9b | 6348 | PL_curcop = ocurcop; |
92da3157 | 6349 | S_regcp_restore(aTHX_ rex, runops_cp, &maxopenparen); |
f5df269c | 6350 | PL_curpm = PL_reg_curpm; |
e4bfbed3 DM |
6351 | |
6352 | if (logical != 2) | |
4aabdb9b | 6353 | break; |
8e5e9ebe | 6354 | } |
e4bfbed3 DM |
6355 | |
6356 | /* only /(??{})/ from now on */ | |
24d3c4a9 | 6357 | logical = 0; |
4aabdb9b | 6358 | { |
4f639d21 DM |
6359 | /* extract RE object from returned value; compiling if |
6360 | * necessary */ | |
5c35adbb | 6361 | |
575c37f6 DM |
6362 | if (re_sv) { |
6363 | re_sv = reg_temp_copy(NULL, re_sv); | |
288b8c02 | 6364 | } |
0f5d15d6 | 6365 | else { |
c737faaf | 6366 | U32 pm_flags = 0; |
0f5d15d6 | 6367 | |
9753d940 DM |
6368 | if (SvUTF8(ret) && IN_BYTES) { |
6369 | /* In use 'bytes': make a copy of the octet | |
6370 | * sequence, but without the flag on */ | |
b9ad30b4 NC |
6371 | STRLEN len; |
6372 | const char *const p = SvPV(ret, len); | |
6373 | ret = newSVpvn_flags(p, len, SVs_TEMP); | |
6374 | } | |
732caac7 DM |
6375 | if (rex->intflags & PREGf_USE_RE_EVAL) |
6376 | pm_flags |= PMf_USE_RE_EVAL; | |
6377 | ||
6378 | /* if we got here, it should be an engine which | |
6379 | * supports compiling code blocks and stuff */ | |
6380 | assert(rex->engine && rex->engine->op_comp); | |
ec841a27 | 6381 | assert(!(scan->flags & ~RXf_PMf_COMPILETIME)); |
575c37f6 | 6382 | re_sv = rex->engine->op_comp(aTHX_ &ret, 1, NULL, |
ec841a27 | 6383 | rex->engine, NULL, NULL, |
33be4c61 | 6384 | /* copy /msixn etc to inner pattern */ |
13f27704 | 6385 | ARG2L(scan), |
ec841a27 | 6386 | pm_flags); |
732caac7 | 6387 | |
9041c2e3 | 6388 | if (!(SvFLAGS(ret) |
237da807 FC |
6389 | & (SVs_TEMP | SVs_GMG | SVf_ROK)) |
6390 | && (!SvPADTMP(ret) || SvREADONLY(ret))) { | |
a2794585 NC |
6391 | /* This isn't a first class regexp. Instead, it's |
6392 | caching a regexp onto an existing, Perl visible | |
6393 | scalar. */ | |
575c37f6 | 6394 | sv_magic(ret, MUTABLE_SV(re_sv), PERL_MAGIC_qr, 0, 0); |
3ce3ed55 | 6395 | } |
0f5d15d6 | 6396 | } |
e1ff3a88 | 6397 | SAVEFREESV(re_sv); |
8d919b0a | 6398 | re = ReANY(re_sv); |
4aabdb9b | 6399 | } |
07bc277f | 6400 | RXp_MATCH_COPIED_off(re); |
28d8d7f4 YO |
6401 | re->subbeg = rex->subbeg; |
6402 | re->sublen = rex->sublen; | |
6502e081 DM |
6403 | re->suboffset = rex->suboffset; |
6404 | re->subcoffset = rex->subcoffset; | |
d1b2014a YO |
6405 | re->lastparen = 0; |
6406 | re->lastcloseparen = 0; | |
f8fc2ecf | 6407 | rei = RXi_GET(re); |
6bda09f9 | 6408 | DEBUG_EXECUTE_r( |
220db18a DM |
6409 | debug_start_match(re_sv, utf8_target, locinput, |
6410 | reginfo->strend, "Matching embedded"); | |
6bda09f9 | 6411 | ); |
f8fc2ecf | 6412 | startpoint = rei->program + 1; |
1a147d38 | 6413 | ST.close_paren = 0; /* only used for GOSUB */ |
d1b2014a YO |
6414 | /* Save all the seen positions so far. */ |
6415 | ST.cp = regcppush(rex, 0, maxopenparen); | |
6416 | REGCP_SET(ST.lastcp); | |
6417 | /* and set maxopenparen to 0, since we are starting a "fresh" match */ | |
6418 | maxopenparen = 0; | |
6419 | /* run the pattern returned from (??{...}) */ | |
aa283a38 | 6420 | |
c52b8b12 | 6421 | eval_recurse_doit: /* Share code with GOSUB below this line |
d1b2014a YO |
6422 | * At this point we expect the stack context to be |
6423 | * set up correctly */ | |
4aabdb9b | 6424 | |
1cb95af7 DM |
6425 | /* invalidate the S-L poscache. We're now executing a |
6426 | * different set of WHILEM ops (and their associated | |
6427 | * indexes) against the same string, so the bits in the | |
6428 | * cache are meaningless. Setting maxiter to zero forces | |
6429 | * the cache to be invalidated and zeroed before reuse. | |
6430 | * XXX This is too dramatic a measure. Ideally we should | |
6431 | * save the old cache and restore when running the outer | |
6432 | * pattern again */ | |
1cb48e53 | 6433 | reginfo->poscache_maxiter = 0; |
4aabdb9b | 6434 | |
d1b2014a | 6435 | /* the new regexp might have a different is_utf8_pat than we do */ |
aed7b151 | 6436 | is_utf8_pat = reginfo->is_utf8_pat = cBOOL(RX_UTF8(re_sv)); |
faec1544 | 6437 | |
288b8c02 | 6438 | ST.prev_rex = rex_sv; |
faec1544 | 6439 | ST.prev_curlyx = cur_curlyx; |
ec43f78b DM |
6440 | rex_sv = re_sv; |
6441 | SET_reg_curpm(rex_sv); | |
288b8c02 | 6442 | rex = re; |
f8fc2ecf | 6443 | rexi = rei; |
faec1544 | 6444 | cur_curlyx = NULL; |
40a82448 | 6445 | ST.B = next; |
faec1544 DM |
6446 | ST.prev_eval = cur_eval; |
6447 | cur_eval = st; | |
faec1544 | 6448 | /* now continue from first node in postoned RE */ |
4d5016e5 | 6449 | PUSH_YES_STATE_GOTO(EVAL_AB, startpoint, locinput); |
a74ff37d | 6450 | /* NOTREACHED */ |
661d43c4 | 6451 | NOT_REACHED; /* NOTREACHED */ |
c277df42 | 6452 | } |
40a82448 | 6453 | |
faec1544 DM |
6454 | case EVAL_AB: /* cleanup after a successful (??{A})B */ |
6455 | /* note: this is called twice; first after popping B, then A */ | |
ec43f78b | 6456 | rex_sv = ST.prev_rex; |
aed7b151 | 6457 | is_utf8_pat = reginfo->is_utf8_pat = cBOOL(RX_UTF8(rex_sv)); |
ec43f78b | 6458 | SET_reg_curpm(rex_sv); |
8d919b0a | 6459 | rex = ReANY(rex_sv); |
f8fc2ecf | 6460 | rexi = RXi_GET(rex); |
4b22688e YO |
6461 | { |
6462 | /* preserve $^R across LEAVE's. See Bug 121070. */ | |
6463 | SV *save_sv= GvSV(PL_replgv); | |
6464 | SvREFCNT_inc(save_sv); | |
6465 | regcpblow(ST.cp); /* LEAVE in disguise */ | |
6466 | sv_setsv(GvSV(PL_replgv), save_sv); | |
6467 | SvREFCNT_dec(save_sv); | |
6468 | } | |
faec1544 DM |
6469 | cur_eval = ST.prev_eval; |
6470 | cur_curlyx = ST.prev_curlyx; | |
34a81e2b | 6471 | |
1cb95af7 | 6472 | /* Invalidate cache. See "invalidate" comment above. */ |
1cb48e53 | 6473 | reginfo->poscache_maxiter = 0; |
e7707071 | 6474 | if ( nochange_depth ) |
4b196cd4 | 6475 | nochange_depth--; |
262b90c4 | 6476 | sayYES; |
40a82448 | 6477 | |
40a82448 | 6478 | |
faec1544 DM |
6479 | case EVAL_AB_fail: /* unsuccessfully ran A or B in (??{A})B */ |
6480 | /* note: this is called twice; first after popping B, then A */ | |
ec43f78b | 6481 | rex_sv = ST.prev_rex; |
aed7b151 | 6482 | is_utf8_pat = reginfo->is_utf8_pat = cBOOL(RX_UTF8(rex_sv)); |
ec43f78b | 6483 | SET_reg_curpm(rex_sv); |
8d919b0a | 6484 | rex = ReANY(rex_sv); |
f8fc2ecf | 6485 | rexi = RXi_GET(rex); |
0357f1fd | 6486 | |
40a82448 | 6487 | REGCP_UNWIND(ST.lastcp); |
92da3157 | 6488 | regcppop(rex, &maxopenparen); |
faec1544 DM |
6489 | cur_eval = ST.prev_eval; |
6490 | cur_curlyx = ST.prev_curlyx; | |
1cb95af7 | 6491 | /* Invalidate cache. See "invalidate" comment above. */ |
1cb48e53 | 6492 | reginfo->poscache_maxiter = 0; |
e7707071 | 6493 | if ( nochange_depth ) |
4b196cd4 | 6494 | nochange_depth--; |
40a82448 | 6495 | sayNO_SILENT; |
40a82448 DM |
6496 | #undef ST |
6497 | ||
3c0563b9 | 6498 | case OPEN: /* ( */ |
c277df42 | 6499 | n = ARG(scan); /* which paren pair */ |
9d9163fb | 6500 | rex->offs[n].start_tmp = locinput - reginfo->strbeg; |
92da3157 DM |
6501 | if (n > maxopenparen) |
6502 | maxopenparen = n; | |
495f47a5 | 6503 | DEBUG_BUFFERS_r(PerlIO_printf(Perl_debug_log, |
92da3157 | 6504 | "rex=0x%"UVxf" offs=0x%"UVxf": \\%"UVuf": set %"IVdf" tmp; maxopenparen=%"UVuf"\n", |
495f47a5 DM |
6505 | PTR2UV(rex), |
6506 | PTR2UV(rex->offs), | |
6507 | (UV)n, | |
6508 | (IV)rex->offs[n].start_tmp, | |
92da3157 | 6509 | (UV)maxopenparen |
495f47a5 | 6510 | )); |
e2e6a0f1 | 6511 | lastopen = n; |
a0d0e21e | 6512 | break; |
495f47a5 DM |
6513 | |
6514 | /* XXX really need to log other places start/end are set too */ | |
6515 | #define CLOSE_CAPTURE \ | |
6516 | rex->offs[n].start = rex->offs[n].start_tmp; \ | |
9d9163fb | 6517 | rex->offs[n].end = locinput - reginfo->strbeg; \ |
495f47a5 DM |
6518 | DEBUG_BUFFERS_r(PerlIO_printf(Perl_debug_log, \ |
6519 | "rex=0x%"UVxf" offs=0x%"UVxf": \\%"UVuf": set %"IVdf"..%"IVdf"\n", \ | |
6520 | PTR2UV(rex), \ | |
6521 | PTR2UV(rex->offs), \ | |
6522 | (UV)n, \ | |
6523 | (IV)rex->offs[n].start, \ | |
6524 | (IV)rex->offs[n].end \ | |
6525 | )) | |
6526 | ||
3c0563b9 | 6527 | case CLOSE: /* ) */ |
c277df42 | 6528 | n = ARG(scan); /* which paren pair */ |
495f47a5 | 6529 | CLOSE_CAPTURE; |
b93070ed DM |
6530 | if (n > rex->lastparen) |
6531 | rex->lastparen = n; | |
6532 | rex->lastcloseparen = n; | |
3b6647e0 | 6533 | if (cur_eval && cur_eval->u.eval.close_paren == n) { |
6bda09f9 YO |
6534 | goto fake_end; |
6535 | } | |
a0d0e21e | 6536 | break; |
3c0563b9 DM |
6537 | |
6538 | case ACCEPT: /* (*ACCEPT) */ | |
e2e6a0f1 YO |
6539 | if (ARG(scan)){ |
6540 | regnode *cursor; | |
6541 | for (cursor=scan; | |
6542 | cursor && OP(cursor)!=END; | |
6543 | cursor=regnext(cursor)) | |
6544 | { | |
6545 | if ( OP(cursor)==CLOSE ){ | |
6546 | n = ARG(cursor); | |
6547 | if ( n <= lastopen ) { | |
495f47a5 | 6548 | CLOSE_CAPTURE; |
b93070ed DM |
6549 | if (n > rex->lastparen) |
6550 | rex->lastparen = n; | |
6551 | rex->lastcloseparen = n; | |
3b6647e0 RB |
6552 | if ( n == ARG(scan) || (cur_eval && |
6553 | cur_eval->u.eval.close_paren == n)) | |
e2e6a0f1 YO |
6554 | break; |
6555 | } | |
6556 | } | |
6557 | } | |
6558 | } | |
6559 | goto fake_end; | |
a74ff37d | 6560 | /* NOTREACHED */ |
3c0563b9 DM |
6561 | |
6562 | case GROUPP: /* (?(1)) */ | |
c277df42 | 6563 | n = ARG(scan); /* which paren pair */ |
b93070ed | 6564 | sw = cBOOL(rex->lastparen >= n && rex->offs[n].end != -1); |
c277df42 | 6565 | break; |
3c0563b9 DM |
6566 | |
6567 | case NGROUPP: /* (?(<name>)) */ | |
0a4db386 | 6568 | /* reg_check_named_buff_matched returns 0 for no match */ |
f2338a2e | 6569 | sw = cBOOL(0 < reg_check_named_buff_matched(rex,scan)); |
0a4db386 | 6570 | break; |
3c0563b9 DM |
6571 | |
6572 | case INSUBP: /* (?(R)) */ | |
0a4db386 | 6573 | n = ARG(scan); |
3b6647e0 | 6574 | sw = (cur_eval && (!n || cur_eval->u.eval.close_paren == n)); |
0a4db386 | 6575 | break; |
3c0563b9 DM |
6576 | |
6577 | case DEFINEP: /* (?(DEFINE)) */ | |
0a4db386 YO |
6578 | sw = 0; |
6579 | break; | |
3c0563b9 DM |
6580 | |
6581 | case IFTHEN: /* (?(cond)A|B) */ | |
1cb48e53 | 6582 | reginfo->poscache_iter = reginfo->poscache_maxiter; /* Void cache */ |
24d3c4a9 | 6583 | if (sw) |
c277df42 IZ |
6584 | next = NEXTOPER(NEXTOPER(scan)); |
6585 | else { | |
6586 | next = scan + ARG(scan); | |
6587 | if (OP(next) == IFTHEN) /* Fake one. */ | |
6588 | next = NEXTOPER(NEXTOPER(next)); | |
6589 | } | |
6590 | break; | |
3c0563b9 DM |
6591 | |
6592 | case LOGICAL: /* modifier for EVAL and IFMATCH */ | |
24d3c4a9 | 6593 | logical = scan->flags; |
c277df42 | 6594 | break; |
c476f425 | 6595 | |
2ab05381 | 6596 | /******************************************************************* |
2ab05381 | 6597 | |
c476f425 DM |
6598 | The CURLYX/WHILEM pair of ops handle the most generic case of the /A*B/ |
6599 | pattern, where A and B are subpatterns. (For simple A, CURLYM or | |
6600 | STAR/PLUS/CURLY/CURLYN are used instead.) | |
2ab05381 | 6601 | |
c476f425 | 6602 | A*B is compiled as <CURLYX><A><WHILEM><B> |
2ab05381 | 6603 | |
c476f425 DM |
6604 | On entry to the subpattern, CURLYX is called. This pushes a CURLYX |
6605 | state, which contains the current count, initialised to -1. It also sets | |
6606 | cur_curlyx to point to this state, with any previous value saved in the | |
6607 | state block. | |
2ab05381 | 6608 | |
c476f425 DM |
6609 | CURLYX then jumps straight to the WHILEM op, rather than executing A, |
6610 | since the pattern may possibly match zero times (i.e. it's a while {} loop | |
6611 | rather than a do {} while loop). | |
2ab05381 | 6612 | |
c476f425 DM |
6613 | Each entry to WHILEM represents a successful match of A. The count in the |
6614 | CURLYX block is incremented, another WHILEM state is pushed, and execution | |
6615 | passes to A or B depending on greediness and the current count. | |
2ab05381 | 6616 | |
c476f425 DM |
6617 | For example, if matching against the string a1a2a3b (where the aN are |
6618 | substrings that match /A/), then the match progresses as follows: (the | |
6619 | pushed states are interspersed with the bits of strings matched so far): | |
2ab05381 | 6620 | |
c476f425 DM |
6621 | <CURLYX cnt=-1> |
6622 | <CURLYX cnt=0><WHILEM> | |
6623 | <CURLYX cnt=1><WHILEM> a1 <WHILEM> | |
6624 | <CURLYX cnt=2><WHILEM> a1 <WHILEM> a2 <WHILEM> | |
6625 | <CURLYX cnt=3><WHILEM> a1 <WHILEM> a2 <WHILEM> a3 <WHILEM> | |
6626 | <CURLYX cnt=3><WHILEM> a1 <WHILEM> a2 <WHILEM> a3 <WHILEM> b | |
2ab05381 | 6627 | |
c476f425 DM |
6628 | (Contrast this with something like CURLYM, which maintains only a single |
6629 | backtrack state: | |
2ab05381 | 6630 | |
c476f425 DM |
6631 | <CURLYM cnt=0> a1 |
6632 | a1 <CURLYM cnt=1> a2 | |
6633 | a1 a2 <CURLYM cnt=2> a3 | |
6634 | a1 a2 a3 <CURLYM cnt=3> b | |
6635 | ) | |
2ab05381 | 6636 | |
c476f425 DM |
6637 | Each WHILEM state block marks a point to backtrack to upon partial failure |
6638 | of A or B, and also contains some minor state data related to that | |
6639 | iteration. The CURLYX block, pointed to by cur_curlyx, contains the | |
6640 | overall state, such as the count, and pointers to the A and B ops. | |
2ab05381 | 6641 | |
c476f425 DM |
6642 | This is complicated slightly by nested CURLYX/WHILEM's. Since cur_curlyx |
6643 | must always point to the *current* CURLYX block, the rules are: | |
2ab05381 | 6644 | |
c476f425 DM |
6645 | When executing CURLYX, save the old cur_curlyx in the CURLYX state block, |
6646 | and set cur_curlyx to point the new block. | |
2ab05381 | 6647 | |
c476f425 DM |
6648 | When popping the CURLYX block after a successful or unsuccessful match, |
6649 | restore the previous cur_curlyx. | |
2ab05381 | 6650 | |
c476f425 DM |
6651 | When WHILEM is about to execute B, save the current cur_curlyx, and set it |
6652 | to the outer one saved in the CURLYX block. | |
2ab05381 | 6653 | |
c476f425 DM |
6654 | When popping the WHILEM block after a successful or unsuccessful B match, |
6655 | restore the previous cur_curlyx. | |
2ab05381 | 6656 | |
c476f425 DM |
6657 | Here's an example for the pattern (AI* BI)*BO |
6658 | I and O refer to inner and outer, C and W refer to CURLYX and WHILEM: | |
2ab05381 | 6659 | |
c476f425 DM |
6660 | cur_ |
6661 | curlyx backtrack stack | |
6662 | ------ --------------- | |
6663 | NULL | |
6664 | CO <CO prev=NULL> <WO> | |
6665 | CI <CO prev=NULL> <WO> <CI prev=CO> <WI> ai | |
6666 | CO <CO prev=NULL> <WO> <CI prev=CO> <WI> ai <WI prev=CI> bi | |
6667 | NULL <CO prev=NULL> <WO> <CI prev=CO> <WI> ai <WI prev=CI> bi <WO prev=CO> bo | |
2ab05381 | 6668 | |
c476f425 DM |
6669 | At this point the pattern succeeds, and we work back down the stack to |
6670 | clean up, restoring as we go: | |
95b24440 | 6671 | |
c476f425 DM |
6672 | CO <CO prev=NULL> <WO> <CI prev=CO> <WI> ai <WI prev=CI> bi |
6673 | CI <CO prev=NULL> <WO> <CI prev=CO> <WI> ai | |
6674 | CO <CO prev=NULL> <WO> | |
6675 | NULL | |
a0374537 | 6676 | |
c476f425 DM |
6677 | *******************************************************************/ |
6678 | ||
6679 | #define ST st->u.curlyx | |
6680 | ||
6681 | case CURLYX: /* start of /A*B/ (for complex A) */ | |
6682 | { | |
6683 | /* No need to save/restore up to this paren */ | |
6684 | I32 parenfloor = scan->flags; | |
6685 | ||
6686 | assert(next); /* keep Coverity happy */ | |
6687 | if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */ | |
6688 | next += ARG(next); | |
6689 | ||
6690 | /* XXXX Probably it is better to teach regpush to support | |
92da3157 | 6691 | parenfloor > maxopenparen ... */ |
b93070ed DM |
6692 | if (parenfloor > (I32)rex->lastparen) |
6693 | parenfloor = rex->lastparen; /* Pessimization... */ | |
c476f425 DM |
6694 | |
6695 | ST.prev_curlyx= cur_curlyx; | |
6696 | cur_curlyx = st; | |
6697 | ST.cp = PL_savestack_ix; | |
6698 | ||
6699 | /* these fields contain the state of the current curly. | |
6700 | * they are accessed by subsequent WHILEMs */ | |
6701 | ST.parenfloor = parenfloor; | |
d02d6d97 | 6702 | ST.me = scan; |
c476f425 | 6703 | ST.B = next; |
24d3c4a9 DM |
6704 | ST.minmod = minmod; |
6705 | minmod = 0; | |
c476f425 DM |
6706 | ST.count = -1; /* this will be updated by WHILEM */ |
6707 | ST.lastloc = NULL; /* this will be updated by WHILEM */ | |
6708 | ||
4d5016e5 | 6709 | PUSH_YES_STATE_GOTO(CURLYX_end, PREVOPER(next), locinput); |
a74ff37d | 6710 | /* NOTREACHED */ |
661d43c4 | 6711 | NOT_REACHED; /* NOTREACHED */ |
c476f425 | 6712 | } |
a0d0e21e | 6713 | |
c476f425 | 6714 | case CURLYX_end: /* just finished matching all of A*B */ |
c476f425 DM |
6715 | cur_curlyx = ST.prev_curlyx; |
6716 | sayYES; | |
a74ff37d | 6717 | /* NOTREACHED */ |
661d43c4 | 6718 | NOT_REACHED; /* NOTREACHED */ |
a0d0e21e | 6719 | |
c476f425 DM |
6720 | case CURLYX_end_fail: /* just failed to match all of A*B */ |
6721 | regcpblow(ST.cp); | |
6722 | cur_curlyx = ST.prev_curlyx; | |
6723 | sayNO; | |
a74ff37d | 6724 | /* NOTREACHED */ |
661d43c4 | 6725 | NOT_REACHED; /* NOTREACHED */ |
4633a7c4 | 6726 | |
a0d0e21e | 6727 | |
c476f425 DM |
6728 | #undef ST |
6729 | #define ST st->u.whilem | |
6730 | ||
6731 | case WHILEM: /* just matched an A in /A*B/ (for complex A) */ | |
6732 | { | |
6733 | /* see the discussion above about CURLYX/WHILEM */ | |
c476f425 | 6734 | I32 n; |
1a522d5d JH |
6735 | int min, max; |
6736 | regnode *A; | |
d02d6d97 | 6737 | |
c476f425 | 6738 | assert(cur_curlyx); /* keep Coverity happy */ |
1a522d5d JH |
6739 | |
6740 | min = ARG1(cur_curlyx->u.curlyx.me); | |
6741 | max = ARG2(cur_curlyx->u.curlyx.me); | |
6742 | A = NEXTOPER(cur_curlyx->u.curlyx.me) + EXTRA_STEP_2ARGS; | |
c476f425 DM |
6743 | n = ++cur_curlyx->u.curlyx.count; /* how many A's matched */ |
6744 | ST.save_lastloc = cur_curlyx->u.curlyx.lastloc; | |
6745 | ST.cache_offset = 0; | |
6746 | ST.cache_mask = 0; | |
6747 | ||
c476f425 DM |
6748 | |
6749 | DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log, | |
d02d6d97 DM |
6750 | "%*s whilem: matched %ld out of %d..%d\n", |
6751 | REPORT_CODE_OFF+depth*2, "", (long)n, min, max) | |
c476f425 | 6752 | ); |
a0d0e21e | 6753 | |
c476f425 | 6754 | /* First just match a string of min A's. */ |
a0d0e21e | 6755 | |
d02d6d97 | 6756 | if (n < min) { |
92da3157 DM |
6757 | ST.cp = regcppush(rex, cur_curlyx->u.curlyx.parenfloor, |
6758 | maxopenparen); | |
c476f425 | 6759 | cur_curlyx->u.curlyx.lastloc = locinput; |
92e82afa YO |
6760 | REGCP_SET(ST.lastcp); |
6761 | ||
4d5016e5 | 6762 | PUSH_STATE_GOTO(WHILEM_A_pre, A, locinput); |
a74ff37d | 6763 | /* NOTREACHED */ |
661d43c4 | 6764 | NOT_REACHED; /* NOTREACHED */ |
c476f425 DM |
6765 | } |
6766 | ||
6767 | /* If degenerate A matches "", assume A done. */ | |
6768 | ||
6769 | if (locinput == cur_curlyx->u.curlyx.lastloc) { | |
6770 | DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log, | |
6771 | "%*s whilem: empty match detected, trying continuation...\n", | |
6772 | REPORT_CODE_OFF+depth*2, "") | |
6773 | ); | |
6774 | goto do_whilem_B_max; | |
6775 | } | |
6776 | ||
1cb95af7 DM |
6777 | /* super-linear cache processing. |
6778 | * | |
6779 | * The idea here is that for certain types of CURLYX/WHILEM - | |
6780 | * principally those whose upper bound is infinity (and | |
6781 | * excluding regexes that have things like \1 and other very | |
6782 | * non-regular expresssiony things), then if a pattern like | |
6783 | * /....A*.../ fails and we backtrack to the WHILEM, then we | |
6784 | * make a note that this particular WHILEM op was at string | |
6785 | * position 47 (say) when the rest of pattern failed. Then, if | |
6786 | * we ever find ourselves back at that WHILEM, and at string | |
6787 | * position 47 again, we can just fail immediately rather than | |
6788 | * running the rest of the pattern again. | |
6789 | * | |
6790 | * This is very handy when patterns start to go | |
6791 | * 'super-linear', like in (a+)*(a+)*(a+)*, where you end up | |
6792 | * with a combinatorial explosion of backtracking. | |
6793 | * | |
6794 | * The cache is implemented as a bit array, with one bit per | |
6795 | * string byte position per WHILEM op (up to 16) - so its | |
6796 | * between 0.25 and 2x the string size. | |
6797 | * | |
6798 | * To avoid allocating a poscache buffer every time, we do an | |
6799 | * initially countdown; only after we have executed a WHILEM | |
6800 | * op (string-length x #WHILEMs) times do we allocate the | |
6801 | * cache. | |
6802 | * | |
6803 | * The top 4 bits of scan->flags byte say how many different | |
6804 | * relevant CURLLYX/WHILEM op pairs there are, while the | |
6805 | * bottom 4-bits is the identifying index number of this | |
6806 | * WHILEM. | |
6807 | */ | |
c476f425 DM |
6808 | |
6809 | if (scan->flags) { | |
a0d0e21e | 6810 | |
1cb48e53 | 6811 | if (!reginfo->poscache_maxiter) { |
c476f425 DM |
6812 | /* start the countdown: Postpone detection until we |
6813 | * know the match is not *that* much linear. */ | |
1cb48e53 | 6814 | reginfo->poscache_maxiter |
9d9163fb DM |
6815 | = (reginfo->strend - reginfo->strbeg + 1) |
6816 | * (scan->flags>>4); | |
66bf836d | 6817 | /* possible overflow for long strings and many CURLYX's */ |
1cb48e53 DM |
6818 | if (reginfo->poscache_maxiter < 0) |
6819 | reginfo->poscache_maxiter = I32_MAX; | |
6820 | reginfo->poscache_iter = reginfo->poscache_maxiter; | |
2c2d71f5 | 6821 | } |
c476f425 | 6822 | |
1cb48e53 | 6823 | if (reginfo->poscache_iter-- == 0) { |
c476f425 | 6824 | /* initialise cache */ |
ea3daa5d | 6825 | const SSize_t size = (reginfo->poscache_maxiter + 7)/8; |
2ac8ff4b DM |
6826 | regmatch_info_aux *const aux = reginfo->info_aux; |
6827 | if (aux->poscache) { | |
ea3daa5d | 6828 | if ((SSize_t)reginfo->poscache_size < size) { |
2ac8ff4b DM |
6829 | Renew(aux->poscache, size, char); |
6830 | reginfo->poscache_size = size; | |
2c2d71f5 | 6831 | } |
2ac8ff4b | 6832 | Zero(aux->poscache, size, char); |
2c2d71f5 JH |
6833 | } |
6834 | else { | |
2ac8ff4b DM |
6835 | reginfo->poscache_size = size; |
6836 | Newxz(aux->poscache, size, char); | |
2c2d71f5 | 6837 | } |
c476f425 DM |
6838 | DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log, |
6839 | "%swhilem: Detected a super-linear match, switching on caching%s...\n", | |
6840 | PL_colors[4], PL_colors[5]) | |
6841 | ); | |
2c2d71f5 | 6842 | } |
c476f425 | 6843 | |
1cb48e53 | 6844 | if (reginfo->poscache_iter < 0) { |
c476f425 | 6845 | /* have we already failed at this position? */ |
ea3daa5d | 6846 | SSize_t offset, mask; |
338e600a DM |
6847 | |
6848 | reginfo->poscache_iter = -1; /* stop eventual underflow */ | |
c476f425 | 6849 | offset = (scan->flags & 0xf) - 1 |
9d9163fb DM |
6850 | + (locinput - reginfo->strbeg) |
6851 | * (scan->flags>>4); | |
c476f425 DM |
6852 | mask = 1 << (offset % 8); |
6853 | offset /= 8; | |
2ac8ff4b | 6854 | if (reginfo->info_aux->poscache[offset] & mask) { |
c476f425 DM |
6855 | DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log, |
6856 | "%*s whilem: (cache) already tried at this position...\n", | |
6857 | REPORT_CODE_OFF+depth*2, "") | |
2c2d71f5 | 6858 | ); |
3298f257 | 6859 | sayNO; /* cache records failure */ |
2c2d71f5 | 6860 | } |
c476f425 DM |
6861 | ST.cache_offset = offset; |
6862 | ST.cache_mask = mask; | |
2c2d71f5 | 6863 | } |
c476f425 | 6864 | } |
2c2d71f5 | 6865 | |
c476f425 | 6866 | /* Prefer B over A for minimal matching. */ |
a687059c | 6867 | |
c476f425 DM |
6868 | if (cur_curlyx->u.curlyx.minmod) { |
6869 | ST.save_curlyx = cur_curlyx; | |
6870 | cur_curlyx = cur_curlyx->u.curlyx.prev_curlyx; | |
92da3157 DM |
6871 | ST.cp = regcppush(rex, ST.save_curlyx->u.curlyx.parenfloor, |
6872 | maxopenparen); | |
c476f425 | 6873 | REGCP_SET(ST.lastcp); |
4d5016e5 DM |
6874 | PUSH_YES_STATE_GOTO(WHILEM_B_min, ST.save_curlyx->u.curlyx.B, |
6875 | locinput); | |
a74ff37d | 6876 | /* NOTREACHED */ |
661d43c4 | 6877 | NOT_REACHED; /* NOTREACHED */ |
c476f425 | 6878 | } |
a0d0e21e | 6879 | |
c476f425 DM |
6880 | /* Prefer A over B for maximal matching. */ |
6881 | ||
d02d6d97 | 6882 | if (n < max) { /* More greed allowed? */ |
92da3157 DM |
6883 | ST.cp = regcppush(rex, cur_curlyx->u.curlyx.parenfloor, |
6884 | maxopenparen); | |
c476f425 DM |
6885 | cur_curlyx->u.curlyx.lastloc = locinput; |
6886 | REGCP_SET(ST.lastcp); | |
4d5016e5 | 6887 | PUSH_STATE_GOTO(WHILEM_A_max, A, locinput); |
a74ff37d | 6888 | /* NOTREACHED */ |
661d43c4 | 6889 | NOT_REACHED; /* NOTREACHED */ |
c476f425 DM |
6890 | } |
6891 | goto do_whilem_B_max; | |
6892 | } | |
a74ff37d | 6893 | /* NOTREACHED */ |
661d43c4 | 6894 | NOT_REACHED; /* NOTREACHED */ |
c476f425 DM |
6895 | |
6896 | case WHILEM_B_min: /* just matched B in a minimal match */ | |
6897 | case WHILEM_B_max: /* just matched B in a maximal match */ | |
6898 | cur_curlyx = ST.save_curlyx; | |
6899 | sayYES; | |
a74ff37d | 6900 | /* NOTREACHED */ |
661d43c4 | 6901 | NOT_REACHED; /* NOTREACHED */ |
c476f425 DM |
6902 | |
6903 | case WHILEM_B_max_fail: /* just failed to match B in a maximal match */ | |
6904 | cur_curlyx = ST.save_curlyx; | |
6905 | cur_curlyx->u.curlyx.lastloc = ST.save_lastloc; | |
6906 | cur_curlyx->u.curlyx.count--; | |
6907 | CACHEsayNO; | |
a74ff37d | 6908 | /* NOTREACHED */ |
661d43c4 | 6909 | NOT_REACHED; /* NOTREACHED */ |
c476f425 DM |
6910 | |
6911 | case WHILEM_A_min_fail: /* just failed to match A in a minimal match */ | |
924ba076 | 6912 | /* FALLTHROUGH */ |
c476f425 | 6913 | case WHILEM_A_pre_fail: /* just failed to match even minimal A */ |
92e82afa | 6914 | REGCP_UNWIND(ST.lastcp); |
92da3157 | 6915 | regcppop(rex, &maxopenparen); |
c476f425 DM |
6916 | cur_curlyx->u.curlyx.lastloc = ST.save_lastloc; |
6917 | cur_curlyx->u.curlyx.count--; | |
6918 | CACHEsayNO; | |
a74ff37d | 6919 | /* NOTREACHED */ |
661d43c4 | 6920 | NOT_REACHED; /* NOTREACHED */ |
c476f425 DM |
6921 | |
6922 | case WHILEM_A_max_fail: /* just failed to match A in a maximal match */ | |
6923 | REGCP_UNWIND(ST.lastcp); | |
92da3157 | 6924 | regcppop(rex, &maxopenparen); /* Restore some previous $<digit>s? */ |
c476f425 DM |
6925 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, |
6926 | "%*s whilem: failed, trying continuation...\n", | |
6927 | REPORT_CODE_OFF+depth*2, "") | |
6928 | ); | |
6929 | do_whilem_B_max: | |
6930 | if (cur_curlyx->u.curlyx.count >= REG_INFTY | |
6931 | && ckWARN(WARN_REGEXP) | |
39819bd9 | 6932 | && !reginfo->warned) |
c476f425 | 6933 | { |
39819bd9 | 6934 | reginfo->warned = TRUE; |
dcbac5bb FC |
6935 | Perl_warner(aTHX_ packWARN(WARN_REGEXP), |
6936 | "Complex regular subexpression recursion limit (%d) " | |
6937 | "exceeded", | |
c476f425 DM |
6938 | REG_INFTY - 1); |
6939 | } | |
6940 | ||
6941 | /* now try B */ | |
6942 | ST.save_curlyx = cur_curlyx; | |
6943 | cur_curlyx = cur_curlyx->u.curlyx.prev_curlyx; | |
4d5016e5 DM |
6944 | PUSH_YES_STATE_GOTO(WHILEM_B_max, ST.save_curlyx->u.curlyx.B, |
6945 | locinput); | |
a74ff37d | 6946 | /* NOTREACHED */ |
661d43c4 | 6947 | NOT_REACHED; /* NOTREACHED */ |
c476f425 DM |
6948 | |
6949 | case WHILEM_B_min_fail: /* just failed to match B in a minimal match */ | |
6950 | cur_curlyx = ST.save_curlyx; | |
6951 | REGCP_UNWIND(ST.lastcp); | |
92da3157 | 6952 | regcppop(rex, &maxopenparen); |
c476f425 | 6953 | |
d02d6d97 | 6954 | if (cur_curlyx->u.curlyx.count >= /*max*/ARG2(cur_curlyx->u.curlyx.me)) { |
c476f425 DM |
6955 | /* Maximum greed exceeded */ |
6956 | if (cur_curlyx->u.curlyx.count >= REG_INFTY | |
6957 | && ckWARN(WARN_REGEXP) | |
39819bd9 | 6958 | && !reginfo->warned) |
c476f425 | 6959 | { |
39819bd9 | 6960 | reginfo->warned = TRUE; |
c476f425 | 6961 | Perl_warner(aTHX_ packWARN(WARN_REGEXP), |
dcbac5bb FC |
6962 | "Complex regular subexpression recursion " |
6963 | "limit (%d) exceeded", | |
c476f425 | 6964 | REG_INFTY - 1); |
a0d0e21e | 6965 | } |
c476f425 | 6966 | cur_curlyx->u.curlyx.count--; |
3ab3c9b4 | 6967 | CACHEsayNO; |
a0d0e21e | 6968 | } |
c476f425 DM |
6969 | |
6970 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, | |
6971 | "%*s trying longer...\n", REPORT_CODE_OFF+depth*2, "") | |
6972 | ); | |
6973 | /* Try grabbing another A and see if it helps. */ | |
c476f425 | 6974 | cur_curlyx->u.curlyx.lastloc = locinput; |
92da3157 DM |
6975 | ST.cp = regcppush(rex, cur_curlyx->u.curlyx.parenfloor, |
6976 | maxopenparen); | |
c476f425 | 6977 | REGCP_SET(ST.lastcp); |
d02d6d97 | 6978 | PUSH_STATE_GOTO(WHILEM_A_min, |
4d5016e5 DM |
6979 | /*A*/ NEXTOPER(ST.save_curlyx->u.curlyx.me) + EXTRA_STEP_2ARGS, |
6980 | locinput); | |
a74ff37d | 6981 | /* NOTREACHED */ |
661d43c4 | 6982 | NOT_REACHED; /* NOTREACHED */ |
40a82448 DM |
6983 | |
6984 | #undef ST | |
6985 | #define ST st->u.branch | |
6986 | ||
6987 | case BRANCHJ: /* /(...|A|...)/ with long next pointer */ | |
c277df42 IZ |
6988 | next = scan + ARG(scan); |
6989 | if (next == scan) | |
6990 | next = NULL; | |
40a82448 | 6991 | scan = NEXTOPER(scan); |
924ba076 | 6992 | /* FALLTHROUGH */ |
c277df42 | 6993 | |
40a82448 DM |
6994 | case BRANCH: /* /(...|A|...)/ */ |
6995 | scan = NEXTOPER(scan); /* scan now points to inner node */ | |
b93070ed | 6996 | ST.lastparen = rex->lastparen; |
f6033a9d | 6997 | ST.lastcloseparen = rex->lastcloseparen; |
40a82448 DM |
6998 | ST.next_branch = next; |
6999 | REGCP_SET(ST.cp); | |
02db2b7b | 7000 | |
40a82448 | 7001 | /* Now go into the branch */ |
5d458dd8 | 7002 | if (has_cutgroup) { |
4d5016e5 | 7003 | PUSH_YES_STATE_GOTO(BRANCH_next, scan, locinput); |
5d458dd8 | 7004 | } else { |
4d5016e5 | 7005 | PUSH_STATE_GOTO(BRANCH_next, scan, locinput); |
5d458dd8 | 7006 | } |
a74ff37d | 7007 | /* NOTREACHED */ |
661d43c4 | 7008 | NOT_REACHED; /* NOTREACHED */ |
3c0563b9 DM |
7009 | |
7010 | case CUTGROUP: /* /(*THEN)/ */ | |
5d458dd8 | 7011 | sv_yes_mark = st->u.mark.mark_name = scan->flags ? NULL : |
ad64d0ec | 7012 | MUTABLE_SV(rexi->data->data[ ARG( scan ) ]); |
4d5016e5 | 7013 | PUSH_STATE_GOTO(CUTGROUP_next, next, locinput); |
a74ff37d | 7014 | /* NOTREACHED */ |
661d43c4 | 7015 | NOT_REACHED; /* NOTREACHED */ |
3c0563b9 | 7016 | |
5d458dd8 YO |
7017 | case CUTGROUP_next_fail: |
7018 | do_cutgroup = 1; | |
7019 | no_final = 1; | |
7020 | if (st->u.mark.mark_name) | |
7021 | sv_commit = st->u.mark.mark_name; | |
7022 | sayNO; | |
a74ff37d | 7023 | /* NOTREACHED */ |
661d43c4 | 7024 | NOT_REACHED; /* NOTREACHED */ |
3c0563b9 | 7025 | |
5d458dd8 YO |
7026 | case BRANCH_next: |
7027 | sayYES; | |
a74ff37d | 7028 | /* NOTREACHED */ |
661d43c4 | 7029 | NOT_REACHED; /* NOTREACHED */ |
3c0563b9 | 7030 | |
40a82448 | 7031 | case BRANCH_next_fail: /* that branch failed; try the next, if any */ |
5d458dd8 YO |
7032 | if (do_cutgroup) { |
7033 | do_cutgroup = 0; | |
7034 | no_final = 0; | |
7035 | } | |
40a82448 | 7036 | REGCP_UNWIND(ST.cp); |
a8d1f4b4 | 7037 | UNWIND_PAREN(ST.lastparen, ST.lastcloseparen); |
40a82448 DM |
7038 | scan = ST.next_branch; |
7039 | /* no more branches? */ | |
5d458dd8 YO |
7040 | if (!scan || (OP(scan) != BRANCH && OP(scan) != BRANCHJ)) { |
7041 | DEBUG_EXECUTE_r({ | |
7042 | PerlIO_printf( Perl_debug_log, | |
7043 | "%*s %sBRANCH failed...%s\n", | |
7044 | REPORT_CODE_OFF+depth*2, "", | |
7045 | PL_colors[4], | |
7046 | PL_colors[5] ); | |
7047 | }); | |
7048 | sayNO_SILENT; | |
7049 | } | |
40a82448 | 7050 | continue; /* execute next BRANCH[J] op */ |
a74ff37d | 7051 | /* NOTREACHED */ |
40a82448 | 7052 | |
3c0563b9 | 7053 | case MINMOD: /* next op will be non-greedy, e.g. A*? */ |
24d3c4a9 | 7054 | minmod = 1; |
a0d0e21e | 7055 | break; |
40a82448 DM |
7056 | |
7057 | #undef ST | |
7058 | #define ST st->u.curlym | |
7059 | ||
7060 | case CURLYM: /* /A{m,n}B/ where A is fixed-length */ | |
7061 | ||
7062 | /* This is an optimisation of CURLYX that enables us to push | |
84d2fa14 | 7063 | * only a single backtracking state, no matter how many matches |
40a82448 DM |
7064 | * there are in {m,n}. It relies on the pattern being constant |
7065 | * length, with no parens to influence future backrefs | |
7066 | */ | |
7067 | ||
7068 | ST.me = scan; | |
dc45a647 | 7069 | scan = NEXTOPER(scan) + NODE_STEP_REGNODE; |
40a82448 | 7070 | |
f6033a9d DM |
7071 | ST.lastparen = rex->lastparen; |
7072 | ST.lastcloseparen = rex->lastcloseparen; | |
7073 | ||
40a82448 DM |
7074 | /* if paren positive, emulate an OPEN/CLOSE around A */ |
7075 | if (ST.me->flags) { | |
3b6647e0 | 7076 | U32 paren = ST.me->flags; |
92da3157 DM |
7077 | if (paren > maxopenparen) |
7078 | maxopenparen = paren; | |
c277df42 | 7079 | scan += NEXT_OFF(scan); /* Skip former OPEN. */ |
6407bf3b | 7080 | } |
40a82448 DM |
7081 | ST.A = scan; |
7082 | ST.B = next; | |
7083 | ST.alen = 0; | |
7084 | ST.count = 0; | |
24d3c4a9 DM |
7085 | ST.minmod = minmod; |
7086 | minmod = 0; | |
40a82448 DM |
7087 | ST.c1 = CHRTEST_UNINIT; |
7088 | REGCP_SET(ST.cp); | |
6407bf3b | 7089 | |
40a82448 DM |
7090 | if (!(ST.minmod ? ARG1(ST.me) : ARG2(ST.me))) /* min/max */ |
7091 | goto curlym_do_B; | |
7092 | ||
7093 | curlym_do_A: /* execute the A in /A{m,n}B/ */ | |
4d5016e5 | 7094 | PUSH_YES_STATE_GOTO(CURLYM_A, ST.A, locinput); /* match A */ |
a74ff37d | 7095 | /* NOTREACHED */ |
661d43c4 | 7096 | NOT_REACHED; /* NOTREACHED */ |
5f80c4cf | 7097 | |
40a82448 | 7098 | case CURLYM_A: /* we've just matched an A */ |
40a82448 DM |
7099 | ST.count++; |
7100 | /* after first match, determine A's length: u.curlym.alen */ | |
7101 | if (ST.count == 1) { | |
ba44c216 | 7102 | if (reginfo->is_utf8_target) { |
c07e9d7b DM |
7103 | char *s = st->locinput; |
7104 | while (s < locinput) { | |
40a82448 DM |
7105 | ST.alen++; |
7106 | s += UTF8SKIP(s); | |
7107 | } | |
7108 | } | |
7109 | else { | |
c07e9d7b | 7110 | ST.alen = locinput - st->locinput; |
40a82448 DM |
7111 | } |
7112 | if (ST.alen == 0) | |
7113 | ST.count = ST.minmod ? ARG1(ST.me) : ARG2(ST.me); | |
7114 | } | |
0cadcf80 DM |
7115 | DEBUG_EXECUTE_r( |
7116 | PerlIO_printf(Perl_debug_log, | |
40a82448 | 7117 | "%*s CURLYM now matched %"IVdf" times, len=%"IVdf"...\n", |
5bc10b2c | 7118 | (int)(REPORT_CODE_OFF+(depth*2)), "", |
40a82448 | 7119 | (IV) ST.count, (IV)ST.alen) |
0cadcf80 DM |
7120 | ); |
7121 | ||
0a4db386 | 7122 | if (cur_eval && cur_eval->u.eval.close_paren && |
24b23f37 | 7123 | cur_eval->u.eval.close_paren == (U32)ST.me->flags) |
0a4db386 YO |
7124 | goto fake_end; |
7125 | ||
c966426a DM |
7126 | { |
7127 | I32 max = (ST.minmod ? ARG1(ST.me) : ARG2(ST.me)); | |
7128 | if ( max == REG_INFTY || ST.count < max ) | |
7129 | goto curlym_do_A; /* try to match another A */ | |
7130 | } | |
40a82448 | 7131 | goto curlym_do_B; /* try to match B */ |
5f80c4cf | 7132 | |
40a82448 DM |
7133 | case CURLYM_A_fail: /* just failed to match an A */ |
7134 | REGCP_UNWIND(ST.cp); | |
0a4db386 YO |
7135 | |
7136 | if (ST.minmod || ST.count < ARG1(ST.me) /* min*/ | |
7137 | || (cur_eval && cur_eval->u.eval.close_paren && | |
24b23f37 | 7138 | cur_eval->u.eval.close_paren == (U32)ST.me->flags)) |
40a82448 | 7139 | sayNO; |
0cadcf80 | 7140 | |
40a82448 | 7141 | curlym_do_B: /* execute the B in /A{m,n}B/ */ |
40a82448 DM |
7142 | if (ST.c1 == CHRTEST_UNINIT) { |
7143 | /* calculate c1 and c2 for possible match of 1st char | |
7144 | * following curly */ | |
7145 | ST.c1 = ST.c2 = CHRTEST_VOID; | |
d20a21f4 | 7146 | assert(ST.B); |
40a82448 DM |
7147 | if (HAS_TEXT(ST.B) || JUMPABLE(ST.B)) { |
7148 | regnode *text_node = ST.B; | |
7149 | if (! HAS_TEXT(text_node)) | |
7150 | FIND_NEXT_IMPT(text_node); | |
ee9b8eae YO |
7151 | /* this used to be |
7152 | ||
7153 | (HAS_TEXT(text_node) && PL_regkind[OP(text_node)] == EXACT) | |
7154 | ||
7155 | But the former is redundant in light of the latter. | |
7156 | ||
7157 | if this changes back then the macro for | |
7158 | IS_TEXT and friends need to change. | |
7159 | */ | |
c74f6de9 | 7160 | if (PL_regkind[OP(text_node)] == EXACT) { |
79a2a0e8 | 7161 | if (! S_setup_EXACTISH_ST_c1_c2(aTHX_ |
984e6dd1 | 7162 | text_node, &ST.c1, ST.c1_utf8, &ST.c2, ST.c2_utf8, |
aed7b151 | 7163 | reginfo)) |
c74f6de9 KW |
7164 | { |
7165 | sayNO; | |
7166 | } | |
c277df42 | 7167 | } |
c277df42 | 7168 | } |
40a82448 DM |
7169 | } |
7170 | ||
7171 | DEBUG_EXECUTE_r( | |
7172 | PerlIO_printf(Perl_debug_log, | |
7173 | "%*s CURLYM trying tail with matches=%"IVdf"...\n", | |
5bc10b2c | 7174 | (int)(REPORT_CODE_OFF+(depth*2)), |
40a82448 DM |
7175 | "", (IV)ST.count) |
7176 | ); | |
c74f6de9 | 7177 | if (! NEXTCHR_IS_EOS && ST.c1 != CHRTEST_VOID) { |
79a2a0e8 KW |
7178 | if (! UTF8_IS_INVARIANT(nextchr) && utf8_target) { |
7179 | if (memNE(locinput, ST.c1_utf8, UTF8SKIP(locinput)) | |
7180 | && memNE(locinput, ST.c2_utf8, UTF8SKIP(locinput))) | |
7181 | { | |
7182 | /* simulate B failing */ | |
7183 | DEBUG_OPTIMISE_r( | |
7184 | PerlIO_printf(Perl_debug_log, | |
33daa3a5 | 7185 | "%*s CURLYM Fast bail next target=0x%"UVXf" c1=0x%"UVXf" c2=0x%"UVXf"\n", |
79a2a0e8 KW |
7186 | (int)(REPORT_CODE_OFF+(depth*2)),"", |
7187 | valid_utf8_to_uvchr((U8 *) locinput, NULL), | |
7188 | valid_utf8_to_uvchr(ST.c1_utf8, NULL), | |
7189 | valid_utf8_to_uvchr(ST.c2_utf8, NULL)) | |
7190 | ); | |
7191 | state_num = CURLYM_B_fail; | |
7192 | goto reenter_switch; | |
7193 | } | |
7194 | } | |
7195 | else if (nextchr != ST.c1 && nextchr != ST.c2) { | |
5400f398 KW |
7196 | /* simulate B failing */ |
7197 | DEBUG_OPTIMISE_r( | |
7198 | PerlIO_printf(Perl_debug_log, | |
33daa3a5 | 7199 | "%*s CURLYM Fast bail next target=0x%X c1=0x%X c2=0x%X\n", |
5400f398 | 7200 | (int)(REPORT_CODE_OFF+(depth*2)),"", |
79a2a0e8 KW |
7201 | (int) nextchr, ST.c1, ST.c2) |
7202 | ); | |
5400f398 KW |
7203 | state_num = CURLYM_B_fail; |
7204 | goto reenter_switch; | |
7205 | } | |
c74f6de9 | 7206 | } |
40a82448 DM |
7207 | |
7208 | if (ST.me->flags) { | |
f6033a9d | 7209 | /* emulate CLOSE: mark current A as captured */ |
40a82448 DM |
7210 | I32 paren = ST.me->flags; |
7211 | if (ST.count) { | |
b93070ed | 7212 | rex->offs[paren].start |
9d9163fb DM |
7213 | = HOPc(locinput, -ST.alen) - reginfo->strbeg; |
7214 | rex->offs[paren].end = locinput - reginfo->strbeg; | |
f6033a9d DM |
7215 | if ((U32)paren > rex->lastparen) |
7216 | rex->lastparen = paren; | |
7217 | rex->lastcloseparen = paren; | |
c277df42 | 7218 | } |
40a82448 | 7219 | else |
b93070ed | 7220 | rex->offs[paren].end = -1; |
0a4db386 | 7221 | if (cur_eval && cur_eval->u.eval.close_paren && |
24b23f37 | 7222 | cur_eval->u.eval.close_paren == (U32)ST.me->flags) |
0a4db386 YO |
7223 | { |
7224 | if (ST.count) | |
7225 | goto fake_end; | |
7226 | else | |
7227 | sayNO; | |
7228 | } | |
c277df42 | 7229 | } |
0a4db386 | 7230 | |
4d5016e5 | 7231 | PUSH_STATE_GOTO(CURLYM_B, ST.B, locinput); /* match B */ |
a74ff37d | 7232 | /* NOTREACHED */ |
661d43c4 | 7233 | NOT_REACHED; /* NOTREACHED */ |
40a82448 DM |
7234 | |
7235 | case CURLYM_B_fail: /* just failed to match a B */ | |
7236 | REGCP_UNWIND(ST.cp); | |
a8d1f4b4 | 7237 | UNWIND_PAREN(ST.lastparen, ST.lastcloseparen); |
40a82448 | 7238 | if (ST.minmod) { |
84d2fa14 HS |
7239 | I32 max = ARG2(ST.me); |
7240 | if (max != REG_INFTY && ST.count == max) | |
40a82448 DM |
7241 | sayNO; |
7242 | goto curlym_do_A; /* try to match a further A */ | |
7243 | } | |
7244 | /* backtrack one A */ | |
7245 | if (ST.count == ARG1(ST.me) /* min */) | |
7246 | sayNO; | |
7247 | ST.count--; | |
7016d6eb | 7248 | SET_locinput(HOPc(locinput, -ST.alen)); |
40a82448 DM |
7249 | goto curlym_do_B; /* try to match B */ |
7250 | ||
c255a977 DM |
7251 | #undef ST |
7252 | #define ST st->u.curly | |
40a82448 | 7253 | |
c255a977 DM |
7254 | #define CURLY_SETPAREN(paren, success) \ |
7255 | if (paren) { \ | |
7256 | if (success) { \ | |
9d9163fb DM |
7257 | rex->offs[paren].start = HOPc(locinput, -1) - reginfo->strbeg; \ |
7258 | rex->offs[paren].end = locinput - reginfo->strbeg; \ | |
f6033a9d DM |
7259 | if (paren > rex->lastparen) \ |
7260 | rex->lastparen = paren; \ | |
b93070ed | 7261 | rex->lastcloseparen = paren; \ |
c255a977 | 7262 | } \ |
f6033a9d | 7263 | else { \ |
b93070ed | 7264 | rex->offs[paren].end = -1; \ |
f6033a9d DM |
7265 | rex->lastparen = ST.lastparen; \ |
7266 | rex->lastcloseparen = ST.lastcloseparen; \ | |
7267 | } \ | |
c255a977 DM |
7268 | } |
7269 | ||
b40a2c17 | 7270 | case STAR: /* /A*B/ where A is width 1 char */ |
c255a977 DM |
7271 | ST.paren = 0; |
7272 | ST.min = 0; | |
7273 | ST.max = REG_INFTY; | |
a0d0e21e LW |
7274 | scan = NEXTOPER(scan); |
7275 | goto repeat; | |
3c0563b9 | 7276 | |
b40a2c17 | 7277 | case PLUS: /* /A+B/ where A is width 1 char */ |
c255a977 DM |
7278 | ST.paren = 0; |
7279 | ST.min = 1; | |
7280 | ST.max = REG_INFTY; | |
c277df42 | 7281 | scan = NEXTOPER(scan); |
c255a977 | 7282 | goto repeat; |
3c0563b9 | 7283 | |
b40a2c17 | 7284 | case CURLYN: /* /(A){m,n}B/ where A is width 1 char */ |
5400f398 KW |
7285 | ST.paren = scan->flags; /* Which paren to set */ |
7286 | ST.lastparen = rex->lastparen; | |
f6033a9d | 7287 | ST.lastcloseparen = rex->lastcloseparen; |
92da3157 DM |
7288 | if (ST.paren > maxopenparen) |
7289 | maxopenparen = ST.paren; | |
c255a977 DM |
7290 | ST.min = ARG1(scan); /* min to match */ |
7291 | ST.max = ARG2(scan); /* max to match */ | |
0a4db386 | 7292 | if (cur_eval && cur_eval->u.eval.close_paren && |
86413ec0 | 7293 | cur_eval->u.eval.close_paren == (U32)ST.paren) { |
0a4db386 YO |
7294 | ST.min=1; |
7295 | ST.max=1; | |
7296 | } | |
c255a977 DM |
7297 | scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE); |
7298 | goto repeat; | |
3c0563b9 | 7299 | |
b40a2c17 | 7300 | case CURLY: /* /A{m,n}B/ where A is width 1 char */ |
c255a977 DM |
7301 | ST.paren = 0; |
7302 | ST.min = ARG1(scan); /* min to match */ | |
7303 | ST.max = ARG2(scan); /* max to match */ | |
7304 | scan = NEXTOPER(scan) + NODE_STEP_REGNODE; | |
c277df42 | 7305 | repeat: |
a0d0e21e LW |
7306 | /* |
7307 | * Lookahead to avoid useless match attempts | |
7308 | * when we know what character comes next. | |
c255a977 | 7309 | * |
5f80c4cf JP |
7310 | * Used to only do .*x and .*?x, but now it allows |
7311 | * for )'s, ('s and (?{ ... })'s to be in the way | |
7312 | * of the quantifier and the EXACT-like node. -- japhy | |
7313 | */ | |
7314 | ||
eb5c1be8 | 7315 | assert(ST.min <= ST.max); |
3337dfe3 KW |
7316 | if (! HAS_TEXT(next) && ! JUMPABLE(next)) { |
7317 | ST.c1 = ST.c2 = CHRTEST_VOID; | |
7318 | } | |
7319 | else { | |
5f80c4cf JP |
7320 | regnode *text_node = next; |
7321 | ||
3dab1dad YO |
7322 | if (! HAS_TEXT(text_node)) |
7323 | FIND_NEXT_IMPT(text_node); | |
5f80c4cf | 7324 | |
9e137952 | 7325 | if (! HAS_TEXT(text_node)) |
c255a977 | 7326 | ST.c1 = ST.c2 = CHRTEST_VOID; |
5f80c4cf | 7327 | else { |
ee9b8eae | 7328 | if ( PL_regkind[OP(text_node)] != EXACT ) { |
c255a977 | 7329 | ST.c1 = ST.c2 = CHRTEST_VOID; |
cca55fe3 | 7330 | } |
c74f6de9 | 7331 | else { |
ee9b8eae YO |
7332 | |
7333 | /* Currently we only get here when | |
7334 | ||
7335 | PL_rekind[OP(text_node)] == EXACT | |
7336 | ||
7337 | if this changes back then the macro for IS_TEXT and | |
7338 | friends need to change. */ | |
79a2a0e8 | 7339 | if (! S_setup_EXACTISH_ST_c1_c2(aTHX_ |
984e6dd1 | 7340 | text_node, &ST.c1, ST.c1_utf8, &ST.c2, ST.c2_utf8, |
aed7b151 | 7341 | reginfo)) |
c74f6de9 KW |
7342 | { |
7343 | sayNO; | |
7344 | } | |
7345 | } | |
1aa99e6b | 7346 | } |
bbce6d69 | 7347 | } |
c255a977 DM |
7348 | |
7349 | ST.A = scan; | |
7350 | ST.B = next; | |
24d3c4a9 | 7351 | if (minmod) { |
eb72505d | 7352 | char *li = locinput; |
24d3c4a9 | 7353 | minmod = 0; |
984e6dd1 | 7354 | if (ST.min && |
f9176b44 | 7355 | regrepeat(rex, &li, ST.A, reginfo, ST.min, depth) |
984e6dd1 | 7356 | < ST.min) |
4633a7c4 | 7357 | sayNO; |
7016d6eb | 7358 | SET_locinput(li); |
c255a977 | 7359 | ST.count = ST.min; |
c255a977 DM |
7360 | REGCP_SET(ST.cp); |
7361 | if (ST.c1 == CHRTEST_VOID) | |
7362 | goto curly_try_B_min; | |
7363 | ||
7364 | ST.oldloc = locinput; | |
7365 | ||
7366 | /* set ST.maxpos to the furthest point along the | |
7367 | * string that could possibly match */ | |
7368 | if (ST.max == REG_INFTY) { | |
220db18a | 7369 | ST.maxpos = reginfo->strend - 1; |
f2ed9b32 | 7370 | if (utf8_target) |
c255a977 DM |
7371 | while (UTF8_IS_CONTINUATION(*(U8*)ST.maxpos)) |
7372 | ST.maxpos--; | |
7373 | } | |
f2ed9b32 | 7374 | else if (utf8_target) { |
c255a977 DM |
7375 | int m = ST.max - ST.min; |
7376 | for (ST.maxpos = locinput; | |
220db18a | 7377 | m >0 && ST.maxpos < reginfo->strend; m--) |
c255a977 DM |
7378 | ST.maxpos += UTF8SKIP(ST.maxpos); |
7379 | } | |
7380 | else { | |
7381 | ST.maxpos = locinput + ST.max - ST.min; | |
220db18a DM |
7382 | if (ST.maxpos >= reginfo->strend) |
7383 | ST.maxpos = reginfo->strend - 1; | |
c255a977 DM |
7384 | } |
7385 | goto curly_try_B_min_known; | |
7386 | ||
7387 | } | |
7388 | else { | |
eb72505d DM |
7389 | /* avoid taking address of locinput, so it can remain |
7390 | * a register var */ | |
7391 | char *li = locinput; | |
f9176b44 | 7392 | ST.count = regrepeat(rex, &li, ST.A, reginfo, ST.max, depth); |
c255a977 DM |
7393 | if (ST.count < ST.min) |
7394 | sayNO; | |
7016d6eb | 7395 | SET_locinput(li); |
c255a977 DM |
7396 | if ((ST.count > ST.min) |
7397 | && (PL_regkind[OP(ST.B)] == EOL) && (OP(ST.B) != MEOL)) | |
7398 | { | |
7399 | /* A{m,n} must come at the end of the string, there's | |
7400 | * no point in backing off ... */ | |
7401 | ST.min = ST.count; | |
7402 | /* ...except that $ and \Z can match before *and* after | |
7403 | newline at the end. Consider "\n\n" =~ /\n+\Z\n/. | |
7404 | We may back off by one in this case. */ | |
eb72505d | 7405 | if (UCHARAT(locinput - 1) == '\n' && OP(ST.B) != EOS) |
c255a977 DM |
7406 | ST.min--; |
7407 | } | |
7408 | REGCP_SET(ST.cp); | |
7409 | goto curly_try_B_max; | |
7410 | } | |
a74ff37d | 7411 | /* NOTREACHED */ |
661d43c4 | 7412 | NOT_REACHED; /* NOTREACHED */ |
c255a977 DM |
7413 | |
7414 | case CURLY_B_min_known_fail: | |
7415 | /* failed to find B in a non-greedy match where c1,c2 valid */ | |
c255a977 | 7416 | |
c255a977 | 7417 | REGCP_UNWIND(ST.cp); |
a8d1f4b4 DM |
7418 | if (ST.paren) { |
7419 | UNWIND_PAREN(ST.lastparen, ST.lastcloseparen); | |
7420 | } | |
c255a977 DM |
7421 | /* Couldn't or didn't -- move forward. */ |
7422 | ST.oldloc = locinput; | |
f2ed9b32 | 7423 | if (utf8_target) |
c255a977 DM |
7424 | locinput += UTF8SKIP(locinput); |
7425 | else | |
7426 | locinput++; | |
7427 | ST.count++; | |
7428 | curly_try_B_min_known: | |
7429 | /* find the next place where 'B' could work, then call B */ | |
7430 | { | |
7431 | int n; | |
f2ed9b32 | 7432 | if (utf8_target) { |
c255a977 DM |
7433 | n = (ST.oldloc == locinput) ? 0 : 1; |
7434 | if (ST.c1 == ST.c2) { | |
c255a977 | 7435 | /* set n to utf8_distance(oldloc, locinput) */ |
79a2a0e8 KW |
7436 | while (locinput <= ST.maxpos |
7437 | && memNE(locinput, ST.c1_utf8, UTF8SKIP(locinput))) | |
7438 | { | |
7439 | locinput += UTF8SKIP(locinput); | |
c255a977 DM |
7440 | n++; |
7441 | } | |
1aa99e6b IH |
7442 | } |
7443 | else { | |
c255a977 | 7444 | /* set n to utf8_distance(oldloc, locinput) */ |
79a2a0e8 KW |
7445 | while (locinput <= ST.maxpos |
7446 | && memNE(locinput, ST.c1_utf8, UTF8SKIP(locinput)) | |
7447 | && memNE(locinput, ST.c2_utf8, UTF8SKIP(locinput))) | |
7448 | { | |
7449 | locinput += UTF8SKIP(locinput); | |
c255a977 | 7450 | n++; |
1aa99e6b | 7451 | } |
0fe9bf95 IZ |
7452 | } |
7453 | } | |
5400f398 | 7454 | else { /* Not utf8_target */ |
c255a977 DM |
7455 | if (ST.c1 == ST.c2) { |
7456 | while (locinput <= ST.maxpos && | |
7457 | UCHARAT(locinput) != ST.c1) | |
7458 | locinput++; | |
bbce6d69 | 7459 | } |
c255a977 DM |
7460 | else { |
7461 | while (locinput <= ST.maxpos | |
7462 | && UCHARAT(locinput) != ST.c1 | |
7463 | && UCHARAT(locinput) != ST.c2) | |
7464 | locinput++; | |
a0ed51b3 | 7465 | } |
c255a977 DM |
7466 | n = locinput - ST.oldloc; |
7467 | } | |
7468 | if (locinput > ST.maxpos) | |
7469 | sayNO; | |
c255a977 | 7470 | if (n) { |
eb72505d DM |
7471 | /* In /a{m,n}b/, ST.oldloc is at "a" x m, locinput is |
7472 | * at b; check that everything between oldloc and | |
7473 | * locinput matches */ | |
7474 | char *li = ST.oldloc; | |
c255a977 | 7475 | ST.count += n; |
f9176b44 | 7476 | if (regrepeat(rex, &li, ST.A, reginfo, n, depth) < n) |
4633a7c4 | 7477 | sayNO; |
eb72505d | 7478 | assert(n == REG_INFTY || locinput == li); |
a0d0e21e | 7479 | } |
c255a977 | 7480 | CURLY_SETPAREN(ST.paren, ST.count); |
0a4db386 | 7481 | if (cur_eval && cur_eval->u.eval.close_paren && |
86413ec0 | 7482 | cur_eval->u.eval.close_paren == (U32)ST.paren) { |
0a4db386 YO |
7483 | goto fake_end; |
7484 | } | |
4d5016e5 | 7485 | PUSH_STATE_GOTO(CURLY_B_min_known, ST.B, locinput); |
a0d0e21e | 7486 | } |
a74ff37d | 7487 | /* NOTREACHED */ |
661d43c4 | 7488 | NOT_REACHED; /* NOTREACHED */ |
c255a977 DM |
7489 | |
7490 | case CURLY_B_min_fail: | |
7491 | /* failed to find B in a non-greedy match where c1,c2 invalid */ | |
c255a977 DM |
7492 | |
7493 | REGCP_UNWIND(ST.cp); | |
a8d1f4b4 DM |
7494 | if (ST.paren) { |
7495 | UNWIND_PAREN(ST.lastparen, ST.lastcloseparen); | |
7496 | } | |
c255a977 | 7497 | /* failed -- move forward one */ |
f73aaa43 | 7498 | { |
eb72505d | 7499 | char *li = locinput; |
f9176b44 | 7500 | if (!regrepeat(rex, &li, ST.A, reginfo, 1, depth)) { |
f73aaa43 DM |
7501 | sayNO; |
7502 | } | |
eb72505d | 7503 | locinput = li; |
f73aaa43 DM |
7504 | } |
7505 | { | |
c255a977 | 7506 | ST.count++; |
c255a977 DM |
7507 | if (ST.count <= ST.max || (ST.max == REG_INFTY && |
7508 | ST.count > 0)) /* count overflow ? */ | |
15272685 | 7509 | { |
c255a977 DM |
7510 | curly_try_B_min: |
7511 | CURLY_SETPAREN(ST.paren, ST.count); | |
0a4db386 | 7512 | if (cur_eval && cur_eval->u.eval.close_paren && |
86413ec0 | 7513 | cur_eval->u.eval.close_paren == (U32)ST.paren) { |
0a4db386 YO |
7514 | goto fake_end; |
7515 | } | |
4d5016e5 | 7516 | PUSH_STATE_GOTO(CURLY_B_min, ST.B, locinput); |
a0d0e21e LW |
7517 | } |
7518 | } | |
c74f6de9 | 7519 | sayNO; |
a74ff37d | 7520 | /* NOTREACHED */ |
661d43c4 | 7521 | NOT_REACHED; /* NOTREACHED */ |
c255a977 | 7522 | |
c52b8b12 | 7523 | curly_try_B_max: |
c255a977 | 7524 | /* a successful greedy match: now try to match B */ |
40d049e4 | 7525 | if (cur_eval && cur_eval->u.eval.close_paren && |
86413ec0 | 7526 | cur_eval->u.eval.close_paren == (U32)ST.paren) { |
40d049e4 YO |
7527 | goto fake_end; |
7528 | } | |
c255a977 | 7529 | { |
220db18a | 7530 | bool could_match = locinput < reginfo->strend; |
79a2a0e8 | 7531 | |
c255a977 | 7532 | /* If it could work, try it. */ |
79a2a0e8 KW |
7533 | if (ST.c1 != CHRTEST_VOID && could_match) { |
7534 | if (! UTF8_IS_INVARIANT(UCHARAT(locinput)) && utf8_target) | |
7535 | { | |
7536 | could_match = memEQ(locinput, | |
7537 | ST.c1_utf8, | |
7538 | UTF8SKIP(locinput)) | |
7539 | || memEQ(locinput, | |
7540 | ST.c2_utf8, | |
7541 | UTF8SKIP(locinput)); | |
7542 | } | |
7543 | else { | |
7544 | could_match = UCHARAT(locinput) == ST.c1 | |
7545 | || UCHARAT(locinput) == ST.c2; | |
7546 | } | |
7547 | } | |
7548 | if (ST.c1 == CHRTEST_VOID || could_match) { | |
c255a977 | 7549 | CURLY_SETPAREN(ST.paren, ST.count); |
4d5016e5 | 7550 | PUSH_STATE_GOTO(CURLY_B_max, ST.B, locinput); |
a74ff37d | 7551 | /* NOTREACHED */ |
661d43c4 | 7552 | NOT_REACHED; /* NOTREACHED */ |
c255a977 DM |
7553 | } |
7554 | } | |
924ba076 | 7555 | /* FALLTHROUGH */ |
3c0563b9 | 7556 | |
c255a977 DM |
7557 | case CURLY_B_max_fail: |
7558 | /* failed to find B in a greedy match */ | |
c255a977 DM |
7559 | |
7560 | REGCP_UNWIND(ST.cp); | |
a8d1f4b4 DM |
7561 | if (ST.paren) { |
7562 | UNWIND_PAREN(ST.lastparen, ST.lastcloseparen); | |
7563 | } | |
c255a977 DM |
7564 | /* back up. */ |
7565 | if (--ST.count < ST.min) | |
7566 | sayNO; | |
eb72505d | 7567 | locinput = HOPc(locinput, -1); |
c255a977 DM |
7568 | goto curly_try_B_max; |
7569 | ||
7570 | #undef ST | |
7571 | ||
3c0563b9 | 7572 | case END: /* last op of main pattern */ |
c52b8b12 | 7573 | fake_end: |
faec1544 DM |
7574 | if (cur_eval) { |
7575 | /* we've just finished A in /(??{A})B/; now continue with B */ | |
faec1544 | 7576 | |
288b8c02 | 7577 | st->u.eval.prev_rex = rex_sv; /* inner */ |
92da3157 DM |
7578 | |
7579 | /* Save *all* the positions. */ | |
7580 | st->u.eval.cp = regcppush(rex, 0, maxopenparen); | |
ec43f78b | 7581 | rex_sv = cur_eval->u.eval.prev_rex; |
aed7b151 | 7582 | is_utf8_pat = reginfo->is_utf8_pat = cBOOL(RX_UTF8(rex_sv)); |
ec43f78b | 7583 | SET_reg_curpm(rex_sv); |
8d919b0a | 7584 | rex = ReANY(rex_sv); |
f8fc2ecf | 7585 | rexi = RXi_GET(rex); |
faec1544 | 7586 | cur_curlyx = cur_eval->u.eval.prev_curlyx; |
34a81e2b | 7587 | |
faec1544 | 7588 | REGCP_SET(st->u.eval.lastcp); |
faec1544 DM |
7589 | |
7590 | /* Restore parens of the outer rex without popping the | |
7591 | * savestack */ | |
92da3157 DM |
7592 | S_regcp_restore(aTHX_ rex, cur_eval->u.eval.lastcp, |
7593 | &maxopenparen); | |
faec1544 DM |
7594 | |
7595 | st->u.eval.prev_eval = cur_eval; | |
7596 | cur_eval = cur_eval->u.eval.prev_eval; | |
7597 | DEBUG_EXECUTE_r( | |
2a49f0f5 JH |
7598 | PerlIO_printf(Perl_debug_log, "%*s EVAL trying tail ... %"UVxf"\n", |
7599 | REPORT_CODE_OFF+depth*2, "",PTR2UV(cur_eval));); | |
e7707071 YO |
7600 | if ( nochange_depth ) |
7601 | nochange_depth--; | |
7602 | ||
4d5016e5 DM |
7603 | PUSH_YES_STATE_GOTO(EVAL_AB, st->u.eval.prev_eval->u.eval.B, |
7604 | locinput); /* match B */ | |
faec1544 DM |
7605 | } |
7606 | ||
3b0527fe | 7607 | if (locinput < reginfo->till) { |
a3621e74 | 7608 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, |
7821416a IZ |
7609 | "%sMatch possible, but length=%ld is smaller than requested=%ld, failing!%s\n", |
7610 | PL_colors[4], | |
6d59b646 DM |
7611 | (long)(locinput - startpos), |
7612 | (long)(reginfo->till - startpos), | |
7821416a | 7613 | PL_colors[5])); |
58e23c8d | 7614 | |
262b90c4 | 7615 | sayNO_SILENT; /* Cannot match: too short. */ |
7821416a | 7616 | } |
262b90c4 | 7617 | sayYES; /* Success! */ |
dad79028 DM |
7618 | |
7619 | case SUCCEED: /* successful SUSPEND/UNLESSM/IFMATCH/CURLYM */ | |
7620 | DEBUG_EXECUTE_r( | |
7621 | PerlIO_printf(Perl_debug_log, | |
7622 | "%*s %ssubpattern success...%s\n", | |
5bc10b2c | 7623 | REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5])); |
262b90c4 | 7624 | sayYES; /* Success! */ |
dad79028 | 7625 | |
40a82448 DM |
7626 | #undef ST |
7627 | #define ST st->u.ifmatch | |
7628 | ||
37f53970 DM |
7629 | { |
7630 | char *newstart; | |
7631 | ||
40a82448 DM |
7632 | case SUSPEND: /* (?>A) */ |
7633 | ST.wanted = 1; | |
37f53970 | 7634 | newstart = locinput; |
9041c2e3 | 7635 | goto do_ifmatch; |
dad79028 | 7636 | |
40a82448 DM |
7637 | case UNLESSM: /* -ve lookaround: (?!A), or with flags, (?<!A) */ |
7638 | ST.wanted = 0; | |
dad79028 DM |
7639 | goto ifmatch_trivial_fail_test; |
7640 | ||
40a82448 DM |
7641 | case IFMATCH: /* +ve lookaround: (?=A), or with flags, (?<=A) */ |
7642 | ST.wanted = 1; | |
dad79028 | 7643 | ifmatch_trivial_fail_test: |
a0ed51b3 | 7644 | if (scan->flags) { |
52657f30 | 7645 | char * const s = HOPBACKc(locinput, scan->flags); |
dad79028 DM |
7646 | if (!s) { |
7647 | /* trivial fail */ | |
24d3c4a9 DM |
7648 | if (logical) { |
7649 | logical = 0; | |
f2338a2e | 7650 | sw = 1 - cBOOL(ST.wanted); |
dad79028 | 7651 | } |
40a82448 | 7652 | else if (ST.wanted) |
dad79028 DM |
7653 | sayNO; |
7654 | next = scan + ARG(scan); | |
7655 | if (next == scan) | |
7656 | next = NULL; | |
7657 | break; | |
7658 | } | |
37f53970 | 7659 | newstart = s; |
a0ed51b3 LW |
7660 | } |
7661 | else | |
37f53970 | 7662 | newstart = locinput; |
a0ed51b3 | 7663 | |
c277df42 | 7664 | do_ifmatch: |
40a82448 | 7665 | ST.me = scan; |
24d3c4a9 | 7666 | ST.logical = logical; |
24d786f4 YO |
7667 | logical = 0; /* XXX: reset state of logical once it has been saved into ST */ |
7668 | ||
40a82448 | 7669 | /* execute body of (?...A) */ |
37f53970 | 7670 | PUSH_YES_STATE_GOTO(IFMATCH_A, NEXTOPER(NEXTOPER(scan)), newstart); |
a74ff37d | 7671 | /* NOTREACHED */ |
661d43c4 | 7672 | NOT_REACHED; /* NOTREACHED */ |
37f53970 | 7673 | } |
40a82448 DM |
7674 | |
7675 | case IFMATCH_A_fail: /* body of (?...A) failed */ | |
7676 | ST.wanted = !ST.wanted; | |
924ba076 | 7677 | /* FALLTHROUGH */ |
40a82448 DM |
7678 | |
7679 | case IFMATCH_A: /* body of (?...A) succeeded */ | |
24d3c4a9 | 7680 | if (ST.logical) { |
f2338a2e | 7681 | sw = cBOOL(ST.wanted); |
40a82448 DM |
7682 | } |
7683 | else if (!ST.wanted) | |
7684 | sayNO; | |
7685 | ||
37f53970 DM |
7686 | if (OP(ST.me) != SUSPEND) { |
7687 | /* restore old position except for (?>...) */ | |
7688 | locinput = st->locinput; | |
40a82448 DM |
7689 | } |
7690 | scan = ST.me + ARG(ST.me); | |
7691 | if (scan == ST.me) | |
7692 | scan = NULL; | |
7693 | continue; /* execute B */ | |
7694 | ||
7695 | #undef ST | |
dad79028 | 7696 | |
3c0563b9 DM |
7697 | case LONGJMP: /* alternative with many branches compiles to |
7698 | * (BRANCHJ; EXACT ...; LONGJMP ) x N */ | |
c277df42 IZ |
7699 | next = scan + ARG(scan); |
7700 | if (next == scan) | |
7701 | next = NULL; | |
a0d0e21e | 7702 | break; |
3c0563b9 DM |
7703 | |
7704 | case COMMIT: /* (*COMMIT) */ | |
220db18a | 7705 | reginfo->cutpoint = reginfo->strend; |
e2e6a0f1 | 7706 | /* FALLTHROUGH */ |
3c0563b9 DM |
7707 | |
7708 | case PRUNE: /* (*PRUNE) */ | |
e2e6a0f1 | 7709 | if (!scan->flags) |
ad64d0ec | 7710 | sv_yes_mark = sv_commit = MUTABLE_SV(rexi->data->data[ ARG( scan ) ]); |
4d5016e5 | 7711 | PUSH_STATE_GOTO(COMMIT_next, next, locinput); |
a74ff37d | 7712 | /* NOTREACHED */ |
661d43c4 | 7713 | NOT_REACHED; /* NOTREACHED */ |
3c0563b9 | 7714 | |
54612592 YO |
7715 | case COMMIT_next_fail: |
7716 | no_final = 1; | |
7717 | /* FALLTHROUGH */ | |
3c0563b9 DM |
7718 | |
7719 | case OPFAIL: /* (*FAIL) */ | |
7f69552c | 7720 | sayNO; |
a74ff37d | 7721 | /* NOTREACHED */ |
661d43c4 | 7722 | NOT_REACHED; /* NOTREACHED */ |
e2e6a0f1 YO |
7723 | |
7724 | #define ST st->u.mark | |
3c0563b9 | 7725 | case MARKPOINT: /* (*MARK:foo) */ |
e2e6a0f1 | 7726 | ST.prev_mark = mark_state; |
5d458dd8 | 7727 | ST.mark_name = sv_commit = sv_yes_mark |
ad64d0ec | 7728 | = MUTABLE_SV(rexi->data->data[ ARG( scan ) ]); |
e2e6a0f1 | 7729 | mark_state = st; |
4d5016e5 DM |
7730 | ST.mark_loc = locinput; |
7731 | PUSH_YES_STATE_GOTO(MARKPOINT_next, next, locinput); | |
a74ff37d | 7732 | /* NOTREACHED */ |
661d43c4 | 7733 | NOT_REACHED; /* NOTREACHED */ |
3c0563b9 | 7734 | |
e2e6a0f1 YO |
7735 | case MARKPOINT_next: |
7736 | mark_state = ST.prev_mark; | |
7737 | sayYES; | |
a74ff37d | 7738 | /* NOTREACHED */ |
661d43c4 | 7739 | NOT_REACHED; /* NOTREACHED */ |
3c0563b9 | 7740 | |
e2e6a0f1 | 7741 | case MARKPOINT_next_fail: |
5d458dd8 | 7742 | if (popmark && sv_eq(ST.mark_name,popmark)) |
e2e6a0f1 YO |
7743 | { |
7744 | if (ST.mark_loc > startpoint) | |
7745 | reginfo->cutpoint = HOPBACKc(ST.mark_loc, 1); | |
7746 | popmark = NULL; /* we found our mark */ | |
7747 | sv_commit = ST.mark_name; | |
7748 | ||
7749 | DEBUG_EXECUTE_r({ | |
5d458dd8 | 7750 | PerlIO_printf(Perl_debug_log, |
e2e6a0f1 YO |
7751 | "%*s %ssetting cutpoint to mark:%"SVf"...%s\n", |
7752 | REPORT_CODE_OFF+depth*2, "", | |
be2597df | 7753 | PL_colors[4], SVfARG(sv_commit), PL_colors[5]); |
e2e6a0f1 YO |
7754 | }); |
7755 | } | |
7756 | mark_state = ST.prev_mark; | |
5d458dd8 YO |
7757 | sv_yes_mark = mark_state ? |
7758 | mark_state->u.mark.mark_name : NULL; | |
e2e6a0f1 | 7759 | sayNO; |
a74ff37d | 7760 | /* NOTREACHED */ |
661d43c4 | 7761 | NOT_REACHED; /* NOTREACHED */ |
3c0563b9 DM |
7762 | |
7763 | case SKIP: /* (*SKIP) */ | |
5d458dd8 | 7764 | if (scan->flags) { |
2bf803e2 | 7765 | /* (*SKIP) : if we fail we cut here*/ |
5d458dd8 | 7766 | ST.mark_name = NULL; |
e2e6a0f1 | 7767 | ST.mark_loc = locinput; |
4d5016e5 | 7768 | PUSH_STATE_GOTO(SKIP_next,next, locinput); |
5d458dd8 | 7769 | } else { |
2bf803e2 | 7770 | /* (*SKIP:NAME) : if there is a (*MARK:NAME) fail where it was, |
5d458dd8 YO |
7771 | otherwise do nothing. Meaning we need to scan |
7772 | */ | |
7773 | regmatch_state *cur = mark_state; | |
ad64d0ec | 7774 | SV *find = MUTABLE_SV(rexi->data->data[ ARG( scan ) ]); |
5d458dd8 YO |
7775 | |
7776 | while (cur) { | |
7777 | if ( sv_eq( cur->u.mark.mark_name, | |
7778 | find ) ) | |
7779 | { | |
7780 | ST.mark_name = find; | |
4d5016e5 | 7781 | PUSH_STATE_GOTO( SKIP_next, next, locinput); |
5d458dd8 YO |
7782 | } |
7783 | cur = cur->u.mark.prev_mark; | |
7784 | } | |
e2e6a0f1 | 7785 | } |
2bf803e2 | 7786 | /* Didn't find our (*MARK:NAME) so ignore this (*SKIP:NAME) */ |
5d458dd8 | 7787 | break; |
3c0563b9 | 7788 | |
5d458dd8 YO |
7789 | case SKIP_next_fail: |
7790 | if (ST.mark_name) { | |
7791 | /* (*CUT:NAME) - Set up to search for the name as we | |
7792 | collapse the stack*/ | |
7793 | popmark = ST.mark_name; | |
7794 | } else { | |
7795 | /* (*CUT) - No name, we cut here.*/ | |
e2e6a0f1 YO |
7796 | if (ST.mark_loc > startpoint) |
7797 | reginfo->cutpoint = HOPBACKc(ST.mark_loc, 1); | |
5d458dd8 YO |
7798 | /* but we set sv_commit to latest mark_name if there |
7799 | is one so they can test to see how things lead to this | |
7800 | cut */ | |
7801 | if (mark_state) | |
7802 | sv_commit=mark_state->u.mark.mark_name; | |
7803 | } | |
e2e6a0f1 YO |
7804 | no_final = 1; |
7805 | sayNO; | |
a74ff37d | 7806 | /* NOTREACHED */ |
661d43c4 | 7807 | NOT_REACHED; /* NOTREACHED */ |
e2e6a0f1 | 7808 | #undef ST |
3c0563b9 DM |
7809 | |
7810 | case LNBREAK: /* \R */ | |
220db18a | 7811 | if ((n=is_LNBREAK_safe(locinput, reginfo->strend, utf8_target))) { |
e1d1eefb | 7812 | locinput += n; |
e1d1eefb YO |
7813 | } else |
7814 | sayNO; | |
7815 | break; | |
7816 | ||
a0d0e21e | 7817 | default: |
b900a521 | 7818 | PerlIO_printf(Perl_error_log, "%"UVxf" %d\n", |
d7d93a81 | 7819 | PTR2UV(scan), OP(scan)); |
cea2e8a9 | 7820 | Perl_croak(aTHX_ "regexp memory corruption"); |
28b98f76 DM |
7821 | |
7822 | /* this is a point to jump to in order to increment | |
7823 | * locinput by one character */ | |
c52b8b12 | 7824 | increment_locinput: |
e6ca698c | 7825 | assert(!NEXTCHR_IS_EOS); |
28b98f76 DM |
7826 | if (utf8_target) { |
7827 | locinput += PL_utf8skip[nextchr]; | |
7016d6eb | 7828 | /* locinput is allowed to go 1 char off the end, but not 2+ */ |
220db18a | 7829 | if (locinput > reginfo->strend) |
28b98f76 | 7830 | sayNO; |
28b98f76 DM |
7831 | } |
7832 | else | |
3640db6b | 7833 | locinput++; |
28b98f76 | 7834 | break; |
5d458dd8 YO |
7835 | |
7836 | } /* end switch */ | |
95b24440 | 7837 | |
5d458dd8 YO |
7838 | /* switch break jumps here */ |
7839 | scan = next; /* prepare to execute the next op and ... */ | |
7840 | continue; /* ... jump back to the top, reusing st */ | |
a74ff37d | 7841 | /* NOTREACHED */ |
95b24440 | 7842 | |
40a82448 DM |
7843 | push_yes_state: |
7844 | /* push a state that backtracks on success */ | |
7845 | st->u.yes.prev_yes_state = yes_state; | |
7846 | yes_state = st; | |
924ba076 | 7847 | /* FALLTHROUGH */ |
40a82448 DM |
7848 | push_state: |
7849 | /* push a new regex state, then continue at scan */ | |
7850 | { | |
7851 | regmatch_state *newst; | |
7852 | ||
24b23f37 YO |
7853 | DEBUG_STACK_r({ |
7854 | regmatch_state *cur = st; | |
7855 | regmatch_state *curyes = yes_state; | |
7856 | int curd = depth; | |
7857 | regmatch_slab *slab = PL_regmatch_slab; | |
7858 | for (;curd > -1;cur--,curd--) { | |
7859 | if (cur < SLAB_FIRST(slab)) { | |
7860 | slab = slab->prev; | |
7861 | cur = SLAB_LAST(slab); | |
7862 | } | |
7863 | PerlIO_printf(Perl_error_log, "%*s#%-3d %-10s %s\n", | |
7864 | REPORT_CODE_OFF + 2 + depth * 2,"", | |
13d6edb4 | 7865 | curd, PL_reg_name[cur->resume_state], |
24b23f37 YO |
7866 | (curyes == cur) ? "yes" : "" |
7867 | ); | |
7868 | if (curyes == cur) | |
7869 | curyes = cur->u.yes.prev_yes_state; | |
7870 | } | |
7871 | } else | |
7872 | DEBUG_STATE_pp("push") | |
7873 | ); | |
40a82448 | 7874 | depth++; |
40a82448 DM |
7875 | st->locinput = locinput; |
7876 | newst = st+1; | |
7877 | if (newst > SLAB_LAST(PL_regmatch_slab)) | |
7878 | newst = S_push_slab(aTHX); | |
7879 | PL_regmatch_state = newst; | |
786e8c11 | 7880 | |
4d5016e5 | 7881 | locinput = pushinput; |
40a82448 DM |
7882 | st = newst; |
7883 | continue; | |
a74ff37d | 7884 | /* NOTREACHED */ |
40a82448 | 7885 | } |
a0d0e21e | 7886 | } |
a687059c | 7887 | |
a0d0e21e LW |
7888 | /* |
7889 | * We get here only if there's trouble -- normally "case END" is | |
7890 | * the terminating point. | |
7891 | */ | |
cea2e8a9 | 7892 | Perl_croak(aTHX_ "corrupted regexp pointers"); |
a74ff37d | 7893 | /* NOTREACHED */ |
4633a7c4 | 7894 | sayNO; |
661d43c4 | 7895 | NOT_REACHED; /* NOTREACHED */ |
4633a7c4 | 7896 | |
7b52d656 | 7897 | yes: |
77cb431f DM |
7898 | if (yes_state) { |
7899 | /* we have successfully completed a subexpression, but we must now | |
7900 | * pop to the state marked by yes_state and continue from there */ | |
77cb431f | 7901 | assert(st != yes_state); |
5bc10b2c DM |
7902 | #ifdef DEBUGGING |
7903 | while (st != yes_state) { | |
7904 | st--; | |
7905 | if (st < SLAB_FIRST(PL_regmatch_slab)) { | |
7906 | PL_regmatch_slab = PL_regmatch_slab->prev; | |
7907 | st = SLAB_LAST(PL_regmatch_slab); | |
7908 | } | |
e2e6a0f1 | 7909 | DEBUG_STATE_r({ |
54612592 YO |
7910 | if (no_final) { |
7911 | DEBUG_STATE_pp("pop (no final)"); | |
7912 | } else { | |
7913 | DEBUG_STATE_pp("pop (yes)"); | |
7914 | } | |
e2e6a0f1 | 7915 | }); |
5bc10b2c DM |
7916 | depth--; |
7917 | } | |
7918 | #else | |
77cb431f DM |
7919 | while (yes_state < SLAB_FIRST(PL_regmatch_slab) |
7920 | || yes_state > SLAB_LAST(PL_regmatch_slab)) | |
7921 | { | |
7922 | /* not in this slab, pop slab */ | |
7923 | depth -= (st - SLAB_FIRST(PL_regmatch_slab) + 1); | |
7924 | PL_regmatch_slab = PL_regmatch_slab->prev; | |
7925 | st = SLAB_LAST(PL_regmatch_slab); | |
7926 | } | |
7927 | depth -= (st - yes_state); | |
5bc10b2c | 7928 | #endif |
77cb431f DM |
7929 | st = yes_state; |
7930 | yes_state = st->u.yes.prev_yes_state; | |
7931 | PL_regmatch_state = st; | |
24b23f37 | 7932 | |
3640db6b | 7933 | if (no_final) |
5d458dd8 | 7934 | locinput= st->locinput; |
54612592 | 7935 | state_num = st->resume_state + no_final; |
24d3c4a9 | 7936 | goto reenter_switch; |
77cb431f DM |
7937 | } |
7938 | ||
a3621e74 | 7939 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch successful!%s\n", |
e4584336 | 7940 | PL_colors[4], PL_colors[5])); |
02db2b7b | 7941 | |
bf2039a9 | 7942 | if (reginfo->info_aux_eval) { |
19b95bf0 DM |
7943 | /* each successfully executed (?{...}) block does the equivalent of |
7944 | * local $^R = do {...} | |
7945 | * When popping the save stack, all these locals would be undone; | |
7946 | * bypass this by setting the outermost saved $^R to the latest | |
7947 | * value */ | |
4b22688e YO |
7948 | /* I dont know if this is needed or works properly now. |
7949 | * see code related to PL_replgv elsewhere in this file. | |
7950 | * Yves | |
7951 | */ | |
19b95bf0 DM |
7952 | if (oreplsv != GvSV(PL_replgv)) |
7953 | sv_setsv(oreplsv, GvSV(PL_replgv)); | |
7954 | } | |
95b24440 | 7955 | result = 1; |
aa283a38 | 7956 | goto final_exit; |
4633a7c4 | 7957 | |
7b52d656 | 7958 | no: |
a3621e74 | 7959 | DEBUG_EXECUTE_r( |
7821416a | 7960 | PerlIO_printf(Perl_debug_log, |
786e8c11 | 7961 | "%*s %sfailed...%s\n", |
5bc10b2c | 7962 | REPORT_CODE_OFF+depth*2, "", |
786e8c11 | 7963 | PL_colors[4], PL_colors[5]) |
7821416a | 7964 | ); |
aa283a38 | 7965 | |
7b52d656 | 7966 | no_silent: |
54612592 YO |
7967 | if (no_final) { |
7968 | if (yes_state) { | |
7969 | goto yes; | |
7970 | } else { | |
7971 | goto final_exit; | |
7972 | } | |
7973 | } | |
aa283a38 DM |
7974 | if (depth) { |
7975 | /* there's a previous state to backtrack to */ | |
40a82448 DM |
7976 | st--; |
7977 | if (st < SLAB_FIRST(PL_regmatch_slab)) { | |
7978 | PL_regmatch_slab = PL_regmatch_slab->prev; | |
7979 | st = SLAB_LAST(PL_regmatch_slab); | |
7980 | } | |
7981 | PL_regmatch_state = st; | |
40a82448 | 7982 | locinput= st->locinput; |
40a82448 | 7983 | |
5bc10b2c DM |
7984 | DEBUG_STATE_pp("pop"); |
7985 | depth--; | |
262b90c4 DM |
7986 | if (yes_state == st) |
7987 | yes_state = st->u.yes.prev_yes_state; | |
5bc10b2c | 7988 | |
24d3c4a9 DM |
7989 | state_num = st->resume_state + 1; /* failure = success + 1 */ |
7990 | goto reenter_switch; | |
95b24440 | 7991 | } |
24d3c4a9 | 7992 | result = 0; |
aa283a38 | 7993 | |
262b90c4 | 7994 | final_exit: |
bbe252da | 7995 | if (rex->intflags & PREGf_VERBARG_SEEN) { |
5d458dd8 YO |
7996 | SV *sv_err = get_sv("REGERROR", 1); |
7997 | SV *sv_mrk = get_sv("REGMARK", 1); | |
7998 | if (result) { | |
e2e6a0f1 | 7999 | sv_commit = &PL_sv_no; |
5d458dd8 YO |
8000 | if (!sv_yes_mark) |
8001 | sv_yes_mark = &PL_sv_yes; | |
8002 | } else { | |
8003 | if (!sv_commit) | |
8004 | sv_commit = &PL_sv_yes; | |
8005 | sv_yes_mark = &PL_sv_no; | |
8006 | } | |
316ebaf2 JH |
8007 | assert(sv_err); |
8008 | assert(sv_mrk); | |
5d458dd8 YO |
8009 | sv_setsv(sv_err, sv_commit); |
8010 | sv_setsv(sv_mrk, sv_yes_mark); | |
e2e6a0f1 | 8011 | } |
19b95bf0 | 8012 | |
81ed78b2 DM |
8013 | |
8014 | if (last_pushed_cv) { | |
8015 | dSP; | |
8016 | POP_MULTICALL; | |
4f8dbb2d | 8017 | PERL_UNUSED_VAR(SP); |
81ed78b2 DM |
8018 | } |
8019 | ||
9d9163fb DM |
8020 | assert(!result || locinput - reginfo->strbeg >= 0); |
8021 | return result ? locinput - reginfo->strbeg : -1; | |
a687059c LW |
8022 | } |
8023 | ||
8024 | /* | |
8025 | - regrepeat - repeatedly match something simple, report how many | |
d60de1d1 | 8026 | * |
e64f369d KW |
8027 | * What 'simple' means is a node which can be the operand of a quantifier like |
8028 | * '+', or {1,3} | |
8029 | * | |
d60de1d1 DM |
8030 | * startposp - pointer a pointer to the start position. This is updated |
8031 | * to point to the byte following the highest successful | |
8032 | * match. | |
8033 | * p - the regnode to be repeatedly matched against. | |
220db18a | 8034 | * reginfo - struct holding match state, such as strend |
4063ade8 | 8035 | * max - maximum number of things to match. |
d60de1d1 | 8036 | * depth - (for debugging) backtracking depth. |
a687059c | 8037 | */ |
76e3520e | 8038 | STATIC I32 |
272d35c9 | 8039 | S_regrepeat(pTHX_ regexp *prog, char **startposp, const regnode *p, |
f9176b44 | 8040 | regmatch_info *const reginfo, I32 max, int depth) |
a687059c | 8041 | { |
4063ade8 | 8042 | char *scan; /* Pointer to current position in target string */ |
eb578fdb | 8043 | I32 c; |
220db18a | 8044 | char *loceol = reginfo->strend; /* local version */ |
4063ade8 | 8045 | I32 hardcount = 0; /* How many matches so far */ |
ba44c216 | 8046 | bool utf8_target = reginfo->is_utf8_target; |
b53eee5d | 8047 | unsigned int to_complement = 0; /* Invert the result? */ |
d513472c | 8048 | UV utf8_flags; |
3018b823 | 8049 | _char_class_number classnum; |
4f55667c SP |
8050 | #ifndef DEBUGGING |
8051 | PERL_UNUSED_ARG(depth); | |
8052 | #endif | |
a0d0e21e | 8053 | |
7918f24d NC |
8054 | PERL_ARGS_ASSERT_REGREPEAT; |
8055 | ||
f73aaa43 | 8056 | scan = *startposp; |
faf11cac HS |
8057 | if (max == REG_INFTY) |
8058 | max = I32_MAX; | |
dfb8f192 | 8059 | else if (! utf8_target && loceol - scan > max) |
7f596f4c | 8060 | loceol = scan + max; |
4063ade8 KW |
8061 | |
8062 | /* Here, for the case of a non-UTF-8 target we have adjusted <loceol> down | |
8063 | * to the maximum of how far we should go in it (leaving it set to the real | |
8064 | * end, if the maximum permissible would take us beyond that). This allows | |
8065 | * us to make the loop exit condition that we haven't gone past <loceol> to | |
8066 | * also mean that we haven't exceeded the max permissible count, saving a | |
8067 | * test each time through the loop. But it assumes that the OP matches a | |
8068 | * single byte, which is true for most of the OPs below when applied to a | |
8069 | * non-UTF-8 target. Those relatively few OPs that don't have this | |
8070 | * characteristic will have to compensate. | |
8071 | * | |
8072 | * There is no adjustment for UTF-8 targets, as the number of bytes per | |
8073 | * character varies. OPs will have to test both that the count is less | |
8074 | * than the max permissible (using <hardcount> to keep track), and that we | |
8075 | * are still within the bounds of the string (using <loceol>. A few OPs | |
8076 | * match a single byte no matter what the encoding. They can omit the max | |
8077 | * test if, for the UTF-8 case, they do the adjustment that was skipped | |
8078 | * above. | |
8079 | * | |
8080 | * Thus, the code above sets things up for the common case; and exceptional | |
8081 | * cases need extra work; the common case is to make sure <scan> doesn't | |
8082 | * go past <loceol>, and for UTF-8 to also use <hardcount> to make sure the | |
8083 | * count doesn't exceed the maximum permissible */ | |
8084 | ||
a0d0e21e | 8085 | switch (OP(p)) { |
22c35a8c | 8086 | case REG_ANY: |
f2ed9b32 | 8087 | if (utf8_target) { |
1aa99e6b | 8088 | while (scan < loceol && hardcount < max && *scan != '\n') { |
ffc61ed2 JH |
8089 | scan += UTF8SKIP(scan); |
8090 | hardcount++; | |
8091 | } | |
8092 | } else { | |
8093 | while (scan < loceol && *scan != '\n') | |
8094 | scan++; | |
a0ed51b3 LW |
8095 | } |
8096 | break; | |
ffc61ed2 | 8097 | case SANY: |
f2ed9b32 | 8098 | if (utf8_target) { |
a0804c9e | 8099 | while (scan < loceol && hardcount < max) { |
def8e4ea JH |
8100 | scan += UTF8SKIP(scan); |
8101 | hardcount++; | |
8102 | } | |
8103 | } | |
8104 | else | |
8105 | scan = loceol; | |
a0ed51b3 | 8106 | break; |
4063ade8 | 8107 | case CANY: /* Move <scan> forward <max> bytes, unless goes off end */ |
9597860a | 8108 | if (utf8_target && loceol - scan > max) { |
4063ade8 KW |
8109 | |
8110 | /* <loceol> hadn't been adjusted in the UTF-8 case */ | |
8111 | scan += max; | |
8112 | } | |
8113 | else { | |
8114 | scan = loceol; | |
8115 | } | |
f33976b4 | 8116 | break; |
a4525e78 | 8117 | case EXACTL: |
780fcc9f | 8118 | _CHECK_AND_WARN_PROBLEMATIC_LOCALE; |
613abc6d KW |
8119 | if (utf8_target && UTF8_IS_ABOVE_LATIN1(*scan)) { |
8120 | _CHECK_AND_OUTPUT_WIDE_LOCALE_UTF8_MSG(scan, loceol); | |
8121 | } | |
780fcc9f | 8122 | /* FALLTHROUGH */ |
59d32103 | 8123 | case EXACT: |
f9176b44 | 8124 | assert(STR_LEN(p) == reginfo->is_utf8_pat ? UTF8SKIP(STRING(p)) : 1); |
613a425d | 8125 | |
59d32103 | 8126 | c = (U8)*STRING(p); |
59d32103 | 8127 | |
5e4a1da1 KW |
8128 | /* Can use a simple loop if the pattern char to match on is invariant |
8129 | * under UTF-8, or both target and pattern aren't UTF-8. Note that we | |
8130 | * can use UTF8_IS_INVARIANT() even if the pattern isn't UTF-8, as it's | |
8131 | * true iff it doesn't matter if the argument is in UTF-8 or not */ | |
f9176b44 | 8132 | if (UTF8_IS_INVARIANT(c) || (! utf8_target && ! reginfo->is_utf8_pat)) { |
e9369824 | 8133 | if (utf8_target && loceol - scan > max) { |
4063ade8 KW |
8134 | /* We didn't adjust <loceol> because is UTF-8, but ok to do so, |
8135 | * since here, to match at all, 1 char == 1 byte */ | |
8136 | loceol = scan + max; | |
8137 | } | |
59d32103 KW |
8138 | while (scan < loceol && UCHARAT(scan) == c) { |
8139 | scan++; | |
8140 | } | |
8141 | } | |
f9176b44 | 8142 | else if (reginfo->is_utf8_pat) { |
5e4a1da1 KW |
8143 | if (utf8_target) { |
8144 | STRLEN scan_char_len; | |
5e4a1da1 | 8145 | |
4063ade8 | 8146 | /* When both target and pattern are UTF-8, we have to do |
5e4a1da1 KW |
8147 | * string EQ */ |
8148 | while (hardcount < max | |
9a902117 KW |
8149 | && scan < loceol |
8150 | && (scan_char_len = UTF8SKIP(scan)) <= STR_LEN(p) | |
5e4a1da1 KW |
8151 | && memEQ(scan, STRING(p), scan_char_len)) |
8152 | { | |
4200a00c | 8153 | scan += scan_char_len; |
5e4a1da1 KW |
8154 | hardcount++; |
8155 | } | |
8156 | } | |
8157 | else if (! UTF8_IS_ABOVE_LATIN1(c)) { | |
b40a2c17 | 8158 | |
5e4a1da1 KW |
8159 | /* Target isn't utf8; convert the character in the UTF-8 |
8160 | * pattern to non-UTF8, and do a simple loop */ | |
94bb8c36 | 8161 | c = TWO_BYTE_UTF8_TO_NATIVE(c, *(STRING(p) + 1)); |
5e4a1da1 KW |
8162 | while (scan < loceol && UCHARAT(scan) == c) { |
8163 | scan++; | |
8164 | } | |
8165 | } /* else pattern char is above Latin1, can't possibly match the | |
8166 | non-UTF-8 target */ | |
b40a2c17 | 8167 | } |
5e4a1da1 | 8168 | else { |
59d32103 | 8169 | |
5e4a1da1 KW |
8170 | /* Here, the string must be utf8; pattern isn't, and <c> is |
8171 | * different in utf8 than not, so can't compare them directly. | |
8172 | * Outside the loop, find the two utf8 bytes that represent c, and | |
8173 | * then look for those in sequence in the utf8 string */ | |
59d32103 KW |
8174 | U8 high = UTF8_TWO_BYTE_HI(c); |
8175 | U8 low = UTF8_TWO_BYTE_LO(c); | |
59d32103 KW |
8176 | |
8177 | while (hardcount < max | |
8178 | && scan + 1 < loceol | |
8179 | && UCHARAT(scan) == high | |
8180 | && UCHARAT(scan + 1) == low) | |
8181 | { | |
8182 | scan += 2; | |
8183 | hardcount++; | |
8184 | } | |
8185 | } | |
8186 | break; | |
5e4a1da1 | 8187 | |
098b07d5 KW |
8188 | case EXACTFA_NO_TRIE: /* This node only generated for non-utf8 patterns */ |
8189 | assert(! reginfo->is_utf8_pat); | |
924ba076 | 8190 | /* FALLTHROUGH */ |
2f7f8cb1 | 8191 | case EXACTFA: |
098b07d5 | 8192 | utf8_flags = FOLDEQ_UTF8_NOMIX_ASCII; |
2f7f8cb1 KW |
8193 | goto do_exactf; |
8194 | ||
d4e0b827 | 8195 | case EXACTFL: |
780fcc9f | 8196 | _CHECK_AND_WARN_PROBLEMATIC_LOCALE; |
cea315b6 | 8197 | utf8_flags = FOLDEQ_LOCALE; |
17580e7a KW |
8198 | goto do_exactf; |
8199 | ||
2fdb7295 KW |
8200 | case EXACTF: /* This node only generated for non-utf8 patterns */ |
8201 | assert(! reginfo->is_utf8_pat); | |
098b07d5 KW |
8202 | utf8_flags = 0; |
8203 | goto do_exactf; | |
62bf7766 | 8204 | |
a4525e78 KW |
8205 | case EXACTFLU8: |
8206 | if (! utf8_target) { | |
8207 | break; | |
8208 | } | |
613abc6d KW |
8209 | utf8_flags = FOLDEQ_LOCALE | FOLDEQ_S2_ALREADY_FOLDED |
8210 | | FOLDEQ_S2_FOLDS_SANE; | |
a4525e78 KW |
8211 | goto do_exactf; |
8212 | ||
3c760661 | 8213 | case EXACTFU_SS: |
9a5a5549 | 8214 | case EXACTFU: |
f9176b44 | 8215 | utf8_flags = reginfo->is_utf8_pat ? FOLDEQ_S2_ALREADY_FOLDED : 0; |
59d32103 | 8216 | |
c52b8b12 | 8217 | do_exactf: { |
613a425d KW |
8218 | int c1, c2; |
8219 | U8 c1_utf8[UTF8_MAXBYTES+1], c2_utf8[UTF8_MAXBYTES+1]; | |
d4e0b827 | 8220 | |
f9176b44 | 8221 | assert(STR_LEN(p) == reginfo->is_utf8_pat ? UTF8SKIP(STRING(p)) : 1); |
613a425d | 8222 | |
984e6dd1 | 8223 | if (S_setup_EXACTISH_ST_c1_c2(aTHX_ p, &c1, c1_utf8, &c2, c2_utf8, |
aed7b151 | 8224 | reginfo)) |
984e6dd1 | 8225 | { |
613a425d | 8226 | if (c1 == CHRTEST_VOID) { |
49b95fad | 8227 | /* Use full Unicode fold matching */ |
220db18a | 8228 | char *tmpeol = reginfo->strend; |
f9176b44 | 8229 | STRLEN pat_len = reginfo->is_utf8_pat ? UTF8SKIP(STRING(p)) : 1; |
49b95fad KW |
8230 | while (hardcount < max |
8231 | && foldEQ_utf8_flags(scan, &tmpeol, 0, utf8_target, | |
8232 | STRING(p), NULL, pat_len, | |
f9176b44 | 8233 | reginfo->is_utf8_pat, utf8_flags)) |
49b95fad KW |
8234 | { |
8235 | scan = tmpeol; | |
220db18a | 8236 | tmpeol = reginfo->strend; |
49b95fad KW |
8237 | hardcount++; |
8238 | } | |
613a425d KW |
8239 | } |
8240 | else if (utf8_target) { | |
8241 | if (c1 == c2) { | |
4063ade8 KW |
8242 | while (scan < loceol |
8243 | && hardcount < max | |
613a425d KW |
8244 | && memEQ(scan, c1_utf8, UTF8SKIP(scan))) |
8245 | { | |
8246 | scan += UTF8SKIP(scan); | |
8247 | hardcount++; | |
8248 | } | |
8249 | } | |
8250 | else { | |
4063ade8 KW |
8251 | while (scan < loceol |
8252 | && hardcount < max | |
613a425d KW |
8253 | && (memEQ(scan, c1_utf8, UTF8SKIP(scan)) |
8254 | || memEQ(scan, c2_utf8, UTF8SKIP(scan)))) | |
8255 | { | |
8256 | scan += UTF8SKIP(scan); | |
8257 | hardcount++; | |
8258 | } | |
8259 | } | |
8260 | } | |
8261 | else if (c1 == c2) { | |
8262 | while (scan < loceol && UCHARAT(scan) == c1) { | |
8263 | scan++; | |
8264 | } | |
8265 | } | |
8266 | else { | |
8267 | while (scan < loceol && | |
8268 | (UCHARAT(scan) == c1 || UCHARAT(scan) == c2)) | |
8269 | { | |
8270 | scan++; | |
8271 | } | |
8272 | } | |
634c83a2 | 8273 | } |
bbce6d69 | 8274 | break; |
613a425d | 8275 | } |
a4525e78 | 8276 | case ANYOFL: |
780fcc9f KW |
8277 | _CHECK_AND_WARN_PROBLEMATIC_LOCALE; |
8278 | /* FALLTHROUGH */ | |
a0d0e21e | 8279 | case ANYOF: |
e0193e47 | 8280 | if (utf8_target) { |
4e8910e0 | 8281 | while (hardcount < max |
9a902117 | 8282 | && scan < loceol |
3db24e1e | 8283 | && reginclass(prog, p, (U8*)scan, (U8*) loceol, utf8_target)) |
4e8910e0 | 8284 | { |
9a902117 | 8285 | scan += UTF8SKIP(scan); |
ffc61ed2 JH |
8286 | hardcount++; |
8287 | } | |
8288 | } else { | |
32fc9b6a | 8289 | while (scan < loceol && REGINCLASS(prog, p, (U8*)scan)) |
ffc61ed2 JH |
8290 | scan++; |
8291 | } | |
a0d0e21e | 8292 | break; |
4063ade8 | 8293 | |
3018b823 | 8294 | /* The argument (FLAGS) to all the POSIX node types is the class number */ |
980866de | 8295 | |
3018b823 KW |
8296 | case NPOSIXL: |
8297 | to_complement = 1; | |
8298 | /* FALLTHROUGH */ | |
980866de | 8299 | |
3018b823 | 8300 | case POSIXL: |
780fcc9f | 8301 | _CHECK_AND_WARN_PROBLEMATIC_LOCALE; |
3018b823 KW |
8302 | if (! utf8_target) { |
8303 | while (scan < loceol && to_complement ^ cBOOL(isFOO_lc(FLAGS(p), | |
8304 | *scan))) | |
a12cf05f | 8305 | { |
3018b823 KW |
8306 | scan++; |
8307 | } | |
8308 | } else { | |
8309 | while (hardcount < max && scan < loceol | |
8310 | && to_complement ^ cBOOL(isFOO_utf8_lc(FLAGS(p), | |
8311 | (U8 *) scan))) | |
8312 | { | |
8313 | scan += UTF8SKIP(scan); | |
ffc61ed2 JH |
8314 | hardcount++; |
8315 | } | |
a0ed51b3 LW |
8316 | } |
8317 | break; | |
0658cdde | 8318 | |
3018b823 KW |
8319 | case POSIXD: |
8320 | if (utf8_target) { | |
8321 | goto utf8_posix; | |
8322 | } | |
8323 | /* FALLTHROUGH */ | |
8324 | ||
0658cdde | 8325 | case POSIXA: |
0430522f | 8326 | if (utf8_target && loceol - scan > max) { |
4063ade8 | 8327 | |
7aee35ff KW |
8328 | /* We didn't adjust <loceol> at the beginning of this routine |
8329 | * because is UTF-8, but it is actually ok to do so, since here, to | |
8330 | * match, 1 char == 1 byte. */ | |
4063ade8 KW |
8331 | loceol = scan + max; |
8332 | } | |
8333 | while (scan < loceol && _generic_isCC_A((U8) *scan, FLAGS(p))) { | |
0658cdde KW |
8334 | scan++; |
8335 | } | |
8336 | break; | |
980866de | 8337 | |
3018b823 KW |
8338 | case NPOSIXD: |
8339 | if (utf8_target) { | |
8340 | to_complement = 1; | |
8341 | goto utf8_posix; | |
8342 | } | |
924ba076 | 8343 | /* FALLTHROUGH */ |
980866de | 8344 | |
3018b823 KW |
8345 | case NPOSIXA: |
8346 | if (! utf8_target) { | |
8347 | while (scan < loceol && ! _generic_isCC_A((U8) *scan, FLAGS(p))) { | |
a12cf05f KW |
8348 | scan++; |
8349 | } | |
4063ade8 | 8350 | } |
3018b823 | 8351 | else { |
980866de | 8352 | |
3018b823 | 8353 | /* The complement of something that matches only ASCII matches all |
837226c8 | 8354 | * non-ASCII, plus everything in ASCII that isn't in the class. */ |
bedac28b | 8355 | while (hardcount < max && scan < loceol |
837226c8 | 8356 | && (! isASCII_utf8(scan) |
3018b823 | 8357 | || ! _generic_isCC_A((U8) *scan, FLAGS(p)))) |
a12cf05f | 8358 | { |
3018b823 | 8359 | scan += UTF8SKIP(scan); |
ffc61ed2 JH |
8360 | hardcount++; |
8361 | } | |
3018b823 KW |
8362 | } |
8363 | break; | |
980866de | 8364 | |
3018b823 KW |
8365 | case NPOSIXU: |
8366 | to_complement = 1; | |
8367 | /* FALLTHROUGH */ | |
8368 | ||
8369 | case POSIXU: | |
8370 | if (! utf8_target) { | |
8371 | while (scan < loceol && to_complement | |
8372 | ^ cBOOL(_generic_isCC((U8) *scan, FLAGS(p)))) | |
4063ade8 | 8373 | { |
3018b823 KW |
8374 | scan++; |
8375 | } | |
cfaf538b KW |
8376 | } |
8377 | else { | |
c52b8b12 | 8378 | utf8_posix: |
3018b823 KW |
8379 | classnum = (_char_class_number) FLAGS(p); |
8380 | if (classnum < _FIRST_NON_SWASH_CC) { | |
8381 | ||
8382 | /* Here, a swash is needed for above-Latin1 code points. | |
8383 | * Process as many Latin1 code points using the built-in rules. | |
8384 | * Go to another loop to finish processing upon encountering | |
8385 | * the first Latin1 code point. We could do that in this loop | |
8386 | * as well, but the other way saves having to test if the swash | |
8387 | * has been loaded every time through the loop: extra space to | |
8388 | * save a test. */ | |
8389 | while (hardcount < max && scan < loceol) { | |
8390 | if (UTF8_IS_INVARIANT(*scan)) { | |
8391 | if (! (to_complement ^ cBOOL(_generic_isCC((U8) *scan, | |
8392 | classnum)))) | |
8393 | { | |
8394 | break; | |
8395 | } | |
8396 | scan++; | |
8397 | } | |
8398 | else if (UTF8_IS_DOWNGRADEABLE_START(*scan)) { | |
8399 | if (! (to_complement | |
94bb8c36 KW |
8400 | ^ cBOOL(_generic_isCC(TWO_BYTE_UTF8_TO_NATIVE(*scan, |
8401 | *(scan + 1)), | |
3018b823 KW |
8402 | classnum)))) |
8403 | { | |
8404 | break; | |
8405 | } | |
8406 | scan += 2; | |
8407 | } | |
8408 | else { | |
8409 | goto found_above_latin1; | |
8410 | } | |
8411 | ||
8412 | hardcount++; | |
8413 | } | |
8414 | } | |
8415 | else { | |
8416 | /* For these character classes, the knowledge of how to handle | |
8417 | * every code point is compiled in to Perl via a macro. This | |
8418 | * code is written for making the loops as tight as possible. | |
8419 | * It could be refactored to save space instead */ | |
8420 | switch (classnum) { | |
779cf272 | 8421 | case _CC_ENUM_SPACE: |
3018b823 KW |
8422 | while (hardcount < max |
8423 | && scan < loceol | |
8424 | && (to_complement ^ cBOOL(isSPACE_utf8(scan)))) | |
8425 | { | |
8426 | scan += UTF8SKIP(scan); | |
8427 | hardcount++; | |
8428 | } | |
8429 | break; | |
8430 | case _CC_ENUM_BLANK: | |
8431 | while (hardcount < max | |
8432 | && scan < loceol | |
8433 | && (to_complement ^ cBOOL(isBLANK_utf8(scan)))) | |
8434 | { | |
8435 | scan += UTF8SKIP(scan); | |
8436 | hardcount++; | |
8437 | } | |
8438 | break; | |
8439 | case _CC_ENUM_XDIGIT: | |
8440 | while (hardcount < max | |
8441 | && scan < loceol | |
8442 | && (to_complement ^ cBOOL(isXDIGIT_utf8(scan)))) | |
8443 | { | |
8444 | scan += UTF8SKIP(scan); | |
8445 | hardcount++; | |
8446 | } | |
8447 | break; | |
8448 | case _CC_ENUM_VERTSPACE: | |
8449 | while (hardcount < max | |
8450 | && scan < loceol | |
8451 | && (to_complement ^ cBOOL(isVERTWS_utf8(scan)))) | |
8452 | { | |
8453 | scan += UTF8SKIP(scan); | |
8454 | hardcount++; | |
8455 | } | |
8456 | break; | |
8457 | case _CC_ENUM_CNTRL: | |
8458 | while (hardcount < max | |
8459 | && scan < loceol | |
8460 | && (to_complement ^ cBOOL(isCNTRL_utf8(scan)))) | |
8461 | { | |
8462 | scan += UTF8SKIP(scan); | |
8463 | hardcount++; | |
8464 | } | |
8465 | break; | |
8466 | default: | |
8467 | Perl_croak(aTHX_ "panic: regrepeat() node %d='%s' has an unexpected character class '%d'", OP(p), PL_reg_name[OP(p)], classnum); | |
8468 | } | |
8469 | } | |
a0ed51b3 | 8470 | } |
3018b823 | 8471 | break; |
4063ade8 | 8472 | |
3018b823 KW |
8473 | found_above_latin1: /* Continuation of POSIXU and NPOSIXU */ |
8474 | ||
8475 | /* Load the swash if not already present */ | |
8476 | if (! PL_utf8_swash_ptrs[classnum]) { | |
8477 | U8 flags = _CORE_SWASH_INIT_ACCEPT_INVLIST; | |
8478 | PL_utf8_swash_ptrs[classnum] = _core_swash_init( | |
2a16ac92 KW |
8479 | "utf8", |
8480 | "", | |
8481 | &PL_sv_undef, 1, 0, | |
8482 | PL_XPosix_ptrs[classnum], &flags); | |
4063ade8 | 8483 | } |
3018b823 KW |
8484 | |
8485 | while (hardcount < max && scan < loceol | |
8486 | && to_complement ^ cBOOL(_generic_utf8( | |
8487 | classnum, | |
8488 | scan, | |
8489 | swash_fetch(PL_utf8_swash_ptrs[classnum], | |
8490 | (U8 *) scan, | |
8491 | TRUE)))) | |
8492 | { | |
8493 | scan += UTF8SKIP(scan); | |
8494 | hardcount++; | |
8495 | } | |
8496 | break; | |
8497 | ||
e1d1eefb | 8498 | case LNBREAK: |
e64f369d KW |
8499 | if (utf8_target) { |
8500 | while (hardcount < max && scan < loceol && | |
8501 | (c=is_LNBREAK_utf8_safe(scan, loceol))) { | |
8502 | scan += c; | |
8503 | hardcount++; | |
8504 | } | |
8505 | } else { | |
8506 | /* LNBREAK can match one or two latin chars, which is ok, but we | |
8507 | * have to use hardcount in this situation, and throw away the | |
8508 | * adjustment to <loceol> done before the switch statement */ | |
220db18a | 8509 | loceol = reginfo->strend; |
e64f369d KW |
8510 | while (scan < loceol && (c=is_LNBREAK_latin1_safe(scan, loceol))) { |
8511 | scan+=c; | |
8512 | hardcount++; | |
8513 | } | |
8514 | } | |
8515 | break; | |
e1d1eefb | 8516 | |
780fcc9f KW |
8517 | case BOUNDL: |
8518 | case NBOUNDL: | |
8519 | _CHECK_AND_WARN_PROBLEMATIC_LOCALE; | |
8520 | /* FALLTHROUGH */ | |
584b1f02 KW |
8521 | case BOUND: |
8522 | case BOUNDA: | |
584b1f02 KW |
8523 | case BOUNDU: |
8524 | case EOS: | |
8525 | case GPOS: | |
8526 | case KEEPS: | |
8527 | case NBOUND: | |
8528 | case NBOUNDA: | |
584b1f02 KW |
8529 | case NBOUNDU: |
8530 | case OPFAIL: | |
8531 | case SBOL: | |
8532 | case SEOL: | |
8533 | /* These are all 0 width, so match right here or not at all. */ | |
8534 | break; | |
8535 | ||
8536 | default: | |
8537 | Perl_croak(aTHX_ "panic: regrepeat() called with unrecognized node type %d='%s'", OP(p), PL_reg_name[OP(p)]); | |
a74ff37d | 8538 | /* NOTREACHED */ |
661d43c4 | 8539 | NOT_REACHED; /* NOTREACHED */ |
584b1f02 | 8540 | |
a0d0e21e | 8541 | } |
a687059c | 8542 | |
a0ed51b3 LW |
8543 | if (hardcount) |
8544 | c = hardcount; | |
8545 | else | |
f73aaa43 DM |
8546 | c = scan - *startposp; |
8547 | *startposp = scan; | |
a687059c | 8548 | |
a3621e74 | 8549 | DEBUG_r({ |
e68ec53f | 8550 | GET_RE_DEBUG_FLAGS_DECL; |
be8e71aa | 8551 | DEBUG_EXECUTE_r({ |
e68ec53f | 8552 | SV * const prop = sv_newmortal(); |
8b9781c9 | 8553 | regprop(prog, prop, p, reginfo, NULL); |
e68ec53f | 8554 | PerlIO_printf(Perl_debug_log, |
be8e71aa | 8555 | "%*s %s can match %"IVdf" times out of %"IVdf"...\n", |
e2e6a0f1 | 8556 | REPORT_CODE_OFF + depth*2, "", SvPVX_const(prop),(IV)c,(IV)max); |
a3621e74 | 8557 | }); |
be8e71aa | 8558 | }); |
9041c2e3 | 8559 | |
a0d0e21e | 8560 | return(c); |
a687059c LW |
8561 | } |
8562 | ||
c277df42 | 8563 | |
be8e71aa | 8564 | #if !defined(PERL_IN_XSUB_RE) || defined(PLUGGABLE_RE_EXTENSION) |
c277df42 | 8565 | /* |
6c6525b8 | 8566 | - regclass_swash - prepare the utf8 swash. Wraps the shared core version to |
e0193e47 KW |
8567 | create a copy so that changes the caller makes won't change the shared one. |
8568 | If <altsvp> is non-null, will return NULL in it, for back-compat. | |
6c6525b8 | 8569 | */ |
ffc61ed2 | 8570 | SV * |
5aaab254 | 8571 | Perl_regclass_swash(pTHX_ const regexp *prog, const regnode* node, bool doinit, SV** listsvp, SV **altsvp) |
ffc61ed2 | 8572 | { |
6c6525b8 | 8573 | PERL_ARGS_ASSERT_REGCLASS_SWASH; |
e0193e47 KW |
8574 | |
8575 | if (altsvp) { | |
8576 | *altsvp = NULL; | |
8577 | } | |
8578 | ||
ef9bc832 | 8579 | return newSVsv(_get_regclass_nonbitmap_data(prog, node, doinit, listsvp, NULL, NULL)); |
6c6525b8 | 8580 | } |
6c6525b8 | 8581 | |
3e63bed3 | 8582 | #endif /* !defined(PERL_IN_XSUB_RE) || defined(PLUGGABLE_RE_EXTENSION) */ |
ffc61ed2 JH |
8583 | |
8584 | /* | |
ba7b4546 | 8585 | - reginclass - determine if a character falls into a character class |
832705d4 | 8586 | |
a4525e78 | 8587 | n is the ANYOF-type regnode |
6698fab5 | 8588 | p is the target string |
3db24e1e | 8589 | p_end points to one byte beyond the end of the target string |
6698fab5 | 8590 | utf8_target tells whether p is in UTF-8. |
832705d4 | 8591 | |
635cd5d4 | 8592 | Returns true if matched; false otherwise. |
eba1359e | 8593 | |
d5788240 KW |
8594 | Note that this can be a synthetic start class, a combination of various |
8595 | nodes, so things you think might be mutually exclusive, such as locale, | |
8596 | aren't. It can match both locale and non-locale | |
8597 | ||
bbce6d69 | 8598 | */ |
8599 | ||
76e3520e | 8600 | STATIC bool |
3db24e1e | 8601 | S_reginclass(pTHX_ regexp * const prog, const regnode * const n, const U8* const p, const U8* const p_end, const bool utf8_target) |
bbce6d69 | 8602 | { |
27da23d5 | 8603 | dVAR; |
a3b680e6 | 8604 | const char flags = ANYOF_FLAGS(n); |
bbce6d69 | 8605 | bool match = FALSE; |
cc07378b | 8606 | UV c = *p; |
1aa99e6b | 8607 | |
7918f24d NC |
8608 | PERL_ARGS_ASSERT_REGINCLASS; |
8609 | ||
afd2eb18 KW |
8610 | /* If c is not already the code point, get it. Note that |
8611 | * UTF8_IS_INVARIANT() works even if not in UTF-8 */ | |
8612 | if (! UTF8_IS_INVARIANT(c) && utf8_target) { | |
635cd5d4 | 8613 | STRLEN c_len = 0; |
3db24e1e | 8614 | c = utf8n_to_uvchr(p, p_end - p, &c_len, |
6182169b KW |
8615 | (UTF8_ALLOW_DEFAULT & UTF8_ALLOW_ANYUV) |
8616 | | UTF8_ALLOW_FFFF | UTF8_CHECK_ONLY); | |
8617 | /* see [perl #37836] for UTF8_ALLOW_ANYUV; [perl #38293] for | |
8618 | * UTF8_ALLOW_FFFF */ | |
f7ab54c6 | 8619 | if (c_len == (STRLEN)-1) |
e8a70c6f | 8620 | Perl_croak(aTHX_ "Malformed UTF-8 character (fatal)"); |
613abc6d KW |
8621 | if (c > 255 && OP(n) == ANYOFL && ! is_ANYOF_SYNTHETIC(n)) { |
8622 | _CHECK_AND_OUTPUT_WIDE_LOCALE_CP_MSG(c); | |
8623 | } | |
19f67299 | 8624 | } |
4b3cda86 | 8625 | |
7cdde544 | 8626 | /* If this character is potentially in the bitmap, check it */ |
dcb20b36 | 8627 | if (c < NUM_ANYOF_CODE_POINTS) { |
ffc61ed2 JH |
8628 | if (ANYOF_BITMAP_TEST(n, c)) |
8629 | match = TRUE; | |
93e92956 KW |
8630 | else if ((flags & ANYOF_MATCHES_ALL_NON_UTF8_NON_ASCII) |
8631 | && ! utf8_target | |
8632 | && ! isASCII(c)) | |
11454c59 KW |
8633 | { |
8634 | match = TRUE; | |
8635 | } | |
1462525b | 8636 | else if (flags & ANYOF_LOCALE_FLAGS) { |
6942fd9a | 8637 | if ((flags & ANYOF_LOC_FOLD) |
e0a1ff7a | 8638 | && c < 256 |
6942fd9a KW |
8639 | && ANYOF_BITMAP_TEST(n, PL_fold_locale[c])) |
8640 | { | |
8641 | match = TRUE; | |
b99851e1 | 8642 | } |
e0a1ff7a KW |
8643 | else if (ANYOF_POSIXL_TEST_ANY_SET(n) |
8644 | && c < 256 | |
8645 | ) { | |
31c7f561 KW |
8646 | |
8647 | /* The data structure is arranged so bits 0, 2, 4, ... are set | |
8648 | * if the class includes the Posix character class given by | |
8649 | * bit/2; and 1, 3, 5, ... are set if the class includes the | |
8650 | * complemented Posix class given by int(bit/2). So we loop | |
8651 | * through the bits, each time changing whether we complement | |
8652 | * the result or not. Suppose for the sake of illustration | |
8653 | * that bits 0-3 mean respectively, \w, \W, \s, \S. If bit 0 | |
8654 | * is set, it means there is a match for this ANYOF node if the | |
8655 | * character is in the class given by the expression (0 / 2 = 0 | |
8656 | * = \w). If it is in that class, isFOO_lc() will return 1, | |
8657 | * and since 'to_complement' is 0, the result will stay TRUE, | |
8658 | * and we exit the loop. Suppose instead that bit 0 is 0, but | |
8659 | * bit 1 is 1. That means there is a match if the character | |
8660 | * matches \W. We won't bother to call isFOO_lc() on bit 0, | |
8661 | * but will on bit 1. On the second iteration 'to_complement' | |
8662 | * will be 1, so the exclusive or will reverse things, so we | |
8663 | * are testing for \W. On the third iteration, 'to_complement' | |
8664 | * will be 0, and we would be testing for \s; the fourth | |
b0d691b2 KW |
8665 | * iteration would test for \S, etc. |
8666 | * | |
8667 | * Note that this code assumes that all the classes are closed | |
8668 | * under folding. For example, if a character matches \w, then | |
8669 | * its fold does too; and vice versa. This should be true for | |
8670 | * any well-behaved locale for all the currently defined Posix | |
8671 | * classes, except for :lower: and :upper:, which are handled | |
8672 | * by the pseudo-class :cased: which matches if either of the | |
8673 | * other two does. To get rid of this assumption, an outer | |
8674 | * loop could be used below to iterate over both the source | |
8675 | * character, and its fold (if different) */ | |
31c7f561 KW |
8676 | |
8677 | int count = 0; | |
8678 | int to_complement = 0; | |
522b3c1e | 8679 | |
31c7f561 | 8680 | while (count < ANYOF_MAX) { |
8efd3f97 | 8681 | if (ANYOF_POSIXL_TEST(n, count) |
31c7f561 KW |
8682 | && to_complement ^ cBOOL(isFOO_lc(count/2, (U8) c))) |
8683 | { | |
8684 | match = TRUE; | |
8685 | break; | |
8686 | } | |
8687 | count++; | |
8688 | to_complement ^= 1; | |
8689 | } | |
ffc61ed2 | 8690 | } |
a0ed51b3 | 8691 | } |
a0ed51b3 LW |
8692 | } |
8693 | ||
31f05a37 | 8694 | |
7cdde544 | 8695 | /* If the bitmap didn't (or couldn't) match, and something outside the |
3b04b210 | 8696 | * bitmap could match, try that. */ |
ef87b810 | 8697 | if (!match) { |
93e92956 KW |
8698 | if (c >= NUM_ANYOF_CODE_POINTS |
8699 | && (flags & ANYOF_MATCHES_ALL_ABOVE_BITMAP)) | |
8700 | { | |
8701 | match = TRUE; /* Everything above the bitmap matches */ | |
e051a21d | 8702 | } |
93e92956 KW |
8703 | else if ((flags & ANYOF_HAS_NONBITMAP_NON_UTF8_MATCHES) |
8704 | || (utf8_target && (flags & ANYOF_HAS_UTF8_NONBITMAP_MATCHES)) | |
1ee208c4 KW |
8705 | || ((flags & ANYOF_LOC_FOLD) |
8706 | && IN_UTF8_CTYPE_LOCALE | |
93e92956 | 8707 | && ARG(n) != ANYOF_ONLY_HAS_BITMAP)) |
3b04b210 | 8708 | { |
1ee208c4 KW |
8709 | SV* only_utf8_locale = NULL; |
8710 | SV * const sw = _get_regclass_nonbitmap_data(prog, n, TRUE, 0, | |
ef9bc832 | 8711 | &only_utf8_locale, NULL); |
7cdde544 | 8712 | if (sw) { |
893ef8be | 8713 | U8 utf8_buffer[2]; |
7cdde544 KW |
8714 | U8 * utf8_p; |
8715 | if (utf8_target) { | |
8716 | utf8_p = (U8 *) p; | |
e0193e47 | 8717 | } else { /* Convert to utf8 */ |
893ef8be KW |
8718 | utf8_p = utf8_buffer; |
8719 | append_utf8_from_native_byte(*p, &utf8_p); | |
8720 | utf8_p = utf8_buffer; | |
7cdde544 | 8721 | } |
f56b6394 | 8722 | |
e0193e47 | 8723 | if (swash_fetch(sw, utf8_p, TRUE)) { |
7cdde544 | 8724 | match = TRUE; |
e0193e47 | 8725 | } |
7cdde544 | 8726 | } |
1ee208c4 KW |
8727 | if (! match && only_utf8_locale && IN_UTF8_CTYPE_LOCALE) { |
8728 | match = _invlist_contains_cp(only_utf8_locale, c); | |
8729 | } | |
7cdde544 | 8730 | } |
5073ffbd KW |
8731 | |
8732 | if (UNICODE_IS_SUPER(c) | |
ae986089 | 8733 | && (flags & ANYOF_WARN_SUPER) |
5073ffbd KW |
8734 | && ckWARN_d(WARN_NON_UNICODE)) |
8735 | { | |
8736 | Perl_warner(aTHX_ packWARN(WARN_NON_UNICODE), | |
2d88a86a | 8737 | "Matched non-Unicode code point 0x%04"UVXf" against Unicode property; may not be portable", c); |
5073ffbd | 8738 | } |
7cdde544 KW |
8739 | } |
8740 | ||
5dbb0c08 KW |
8741 | #if ANYOF_INVERT != 1 |
8742 | /* Depending on compiler optimization cBOOL takes time, so if don't have to | |
8743 | * use it, don't */ | |
8744 | # error ANYOF_INVERT needs to be set to 1, or guarded with cBOOL below, | |
8745 | #endif | |
8746 | ||
f0fdc1c9 | 8747 | /* The xor complements the return if to invert: 1^1 = 0, 1^0 = 1 */ |
5dbb0c08 | 8748 | return (flags & ANYOF_INVERT) ^ match; |
a0ed51b3 | 8749 | } |
161b471a | 8750 | |
dfe13c55 | 8751 | STATIC U8 * |
ea3daa5d | 8752 | S_reghop3(U8 *s, SSize_t off, const U8* lim) |
9041c2e3 | 8753 | { |
6af86488 KW |
8754 | /* return the position 'off' UTF-8 characters away from 's', forward if |
8755 | * 'off' >= 0, backwards if negative. But don't go outside of position | |
8756 | * 'lim', which better be < s if off < 0 */ | |
8757 | ||
7918f24d NC |
8758 | PERL_ARGS_ASSERT_REGHOP3; |
8759 | ||
a0ed51b3 | 8760 | if (off >= 0) { |
1aa99e6b | 8761 | while (off-- && s < lim) { |
ffc61ed2 | 8762 | /* XXX could check well-formedness here */ |
a0ed51b3 | 8763 | s += UTF8SKIP(s); |
ffc61ed2 | 8764 | } |
a0ed51b3 LW |
8765 | } |
8766 | else { | |
1de06328 YO |
8767 | while (off++ && s > lim) { |
8768 | s--; | |
8769 | if (UTF8_IS_CONTINUED(*s)) { | |
8770 | while (s > lim && UTF8_IS_CONTINUATION(*s)) | |
8771 | s--; | |
a0ed51b3 | 8772 | } |
1de06328 | 8773 | /* XXX could check well-formedness here */ |
a0ed51b3 LW |
8774 | } |
8775 | } | |
8776 | return s; | |
8777 | } | |
161b471a | 8778 | |
dfe13c55 | 8779 | STATIC U8 * |
ea3daa5d | 8780 | S_reghop4(U8 *s, SSize_t off, const U8* llim, const U8* rlim) |
1de06328 | 8781 | { |
7918f24d NC |
8782 | PERL_ARGS_ASSERT_REGHOP4; |
8783 | ||
1de06328 YO |
8784 | if (off >= 0) { |
8785 | while (off-- && s < rlim) { | |
8786 | /* XXX could check well-formedness here */ | |
8787 | s += UTF8SKIP(s); | |
8788 | } | |
8789 | } | |
8790 | else { | |
8791 | while (off++ && s > llim) { | |
8792 | s--; | |
8793 | if (UTF8_IS_CONTINUED(*s)) { | |
8794 | while (s > llim && UTF8_IS_CONTINUATION(*s)) | |
8795 | s--; | |
8796 | } | |
8797 | /* XXX could check well-formedness here */ | |
8798 | } | |
8799 | } | |
8800 | return s; | |
8801 | } | |
1de06328 | 8802 | |
557f47af DM |
8803 | /* like reghop3, but returns NULL on overrun, rather than returning last |
8804 | * char pos */ | |
8805 | ||
1de06328 | 8806 | STATIC U8 * |
ea3daa5d | 8807 | S_reghopmaybe3(U8* s, SSize_t off, const U8* lim) |
a0ed51b3 | 8808 | { |
7918f24d NC |
8809 | PERL_ARGS_ASSERT_REGHOPMAYBE3; |
8810 | ||
a0ed51b3 | 8811 | if (off >= 0) { |
1aa99e6b | 8812 | while (off-- && s < lim) { |
ffc61ed2 | 8813 | /* XXX could check well-formedness here */ |
a0ed51b3 | 8814 | s += UTF8SKIP(s); |
ffc61ed2 | 8815 | } |
a0ed51b3 | 8816 | if (off >= 0) |
3dab1dad | 8817 | return NULL; |
a0ed51b3 LW |
8818 | } |
8819 | else { | |
1de06328 YO |
8820 | while (off++ && s > lim) { |
8821 | s--; | |
8822 | if (UTF8_IS_CONTINUED(*s)) { | |
8823 | while (s > lim && UTF8_IS_CONTINUATION(*s)) | |
8824 | s--; | |
a0ed51b3 | 8825 | } |
1de06328 | 8826 | /* XXX could check well-formedness here */ |
a0ed51b3 LW |
8827 | } |
8828 | if (off <= 0) | |
3dab1dad | 8829 | return NULL; |
a0ed51b3 LW |
8830 | } |
8831 | return s; | |
8832 | } | |
51371543 | 8833 | |
a75351a1 DM |
8834 | |
8835 | /* when executing a regex that may have (?{}), extra stuff needs setting | |
8836 | up that will be visible to the called code, even before the current | |
8837 | match has finished. In particular: | |
8838 | ||
8839 | * $_ is localised to the SV currently being matched; | |
8840 | * pos($_) is created if necessary, ready to be updated on each call-out | |
8841 | to code; | |
8842 | * a fake PMOP is created that can be set to PL_curpm (normally PL_curpm | |
8843 | isn't set until the current pattern is successfully finished), so that | |
8844 | $1 etc of the match-so-far can be seen; | |
8845 | * save the old values of subbeg etc of the current regex, and set then | |
8846 | to the current string (again, this is normally only done at the end | |
8847 | of execution) | |
a75351a1 DM |
8848 | */ |
8849 | ||
8850 | static void | |
8851 | S_setup_eval_state(pTHX_ regmatch_info *const reginfo) | |
8852 | { | |
8853 | MAGIC *mg; | |
8854 | regexp *const rex = ReANY(reginfo->prog); | |
bf2039a9 | 8855 | regmatch_info_aux_eval *eval_state = reginfo->info_aux_eval; |
8adc0f72 | 8856 | |
8adc0f72 | 8857 | eval_state->rex = rex; |
a75351a1 | 8858 | |
a75351a1 DM |
8859 | if (reginfo->sv) { |
8860 | /* Make $_ available to executed code. */ | |
8861 | if (reginfo->sv != DEFSV) { | |
8862 | SAVE_DEFSV; | |
8863 | DEFSV_set(reginfo->sv); | |
8864 | } | |
8865 | ||
96c2a8ff | 8866 | if (!(mg = mg_find_mglob(reginfo->sv))) { |
a75351a1 | 8867 | /* prepare for quick setting of pos */ |
96c2a8ff | 8868 | mg = sv_magicext_mglob(reginfo->sv); |
a75351a1 DM |
8869 | mg->mg_len = -1; |
8870 | } | |
8adc0f72 DM |
8871 | eval_state->pos_magic = mg; |
8872 | eval_state->pos = mg->mg_len; | |
25fdce4a | 8873 | eval_state->pos_flags = mg->mg_flags; |
a75351a1 | 8874 | } |
8adc0f72 DM |
8875 | else |
8876 | eval_state->pos_magic = NULL; | |
8877 | ||
a75351a1 | 8878 | if (!PL_reg_curpm) { |
f65e70f5 DM |
8879 | /* PL_reg_curpm is a fake PMOP that we can attach the current |
8880 | * regex to and point PL_curpm at, so that $1 et al are visible | |
8881 | * within a /(?{})/. It's just allocated once per interpreter the | |
8882 | * first time its needed */ | |
a75351a1 DM |
8883 | Newxz(PL_reg_curpm, 1, PMOP); |
8884 | #ifdef USE_ITHREADS | |
8885 | { | |
8886 | SV* const repointer = &PL_sv_undef; | |
8887 | /* this regexp is also owned by the new PL_reg_curpm, which | |
8888 | will try to free it. */ | |
8889 | av_push(PL_regex_padav, repointer); | |
b9f2b683 | 8890 | PL_reg_curpm->op_pmoffset = av_tindex(PL_regex_padav); |
a75351a1 DM |
8891 | PL_regex_pad = AvARRAY(PL_regex_padav); |
8892 | } | |
8893 | #endif | |
8894 | } | |
8895 | SET_reg_curpm(reginfo->prog); | |
8adc0f72 | 8896 | eval_state->curpm = PL_curpm; |
a75351a1 DM |
8897 | PL_curpm = PL_reg_curpm; |
8898 | if (RXp_MATCH_COPIED(rex)) { | |
8899 | /* Here is a serious problem: we cannot rewrite subbeg, | |
8900 | since it may be needed if this match fails. Thus | |
8901 | $` inside (?{}) could fail... */ | |
8adc0f72 DM |
8902 | eval_state->subbeg = rex->subbeg; |
8903 | eval_state->sublen = rex->sublen; | |
8904 | eval_state->suboffset = rex->suboffset; | |
a8ee055f | 8905 | eval_state->subcoffset = rex->subcoffset; |
a75351a1 | 8906 | #ifdef PERL_ANY_COW |
8adc0f72 | 8907 | eval_state->saved_copy = rex->saved_copy; |
a75351a1 DM |
8908 | #endif |
8909 | RXp_MATCH_COPIED_off(rex); | |
8910 | } | |
8911 | else | |
8adc0f72 | 8912 | eval_state->subbeg = NULL; |
a75351a1 DM |
8913 | rex->subbeg = (char *)reginfo->strbeg; |
8914 | rex->suboffset = 0; | |
8915 | rex->subcoffset = 0; | |
8916 | rex->sublen = reginfo->strend - reginfo->strbeg; | |
8917 | } | |
8918 | ||
bf2039a9 DM |
8919 | |
8920 | /* destructor to clear up regmatch_info_aux and regmatch_info_aux_eval */ | |
a75351a1 | 8921 | |
51371543 | 8922 | static void |
bf2039a9 | 8923 | S_cleanup_regmatch_info_aux(pTHX_ void *arg) |
51371543 | 8924 | { |
bf2039a9 DM |
8925 | regmatch_info_aux *aux = (regmatch_info_aux *) arg; |
8926 | regmatch_info_aux_eval *eval_state = aux->info_aux_eval; | |
331b2dcc | 8927 | regmatch_slab *s; |
bf2039a9 | 8928 | |
2ac8ff4b DM |
8929 | Safefree(aux->poscache); |
8930 | ||
331b2dcc | 8931 | if (eval_state) { |
bf2039a9 | 8932 | |
331b2dcc | 8933 | /* undo the effects of S_setup_eval_state() */ |
bf2039a9 | 8934 | |
331b2dcc DM |
8935 | if (eval_state->subbeg) { |
8936 | regexp * const rex = eval_state->rex; | |
8937 | rex->subbeg = eval_state->subbeg; | |
8938 | rex->sublen = eval_state->sublen; | |
8939 | rex->suboffset = eval_state->suboffset; | |
8940 | rex->subcoffset = eval_state->subcoffset; | |
db2c6cb3 | 8941 | #ifdef PERL_ANY_COW |
331b2dcc | 8942 | rex->saved_copy = eval_state->saved_copy; |
ed252734 | 8943 | #endif |
331b2dcc DM |
8944 | RXp_MATCH_COPIED_on(rex); |
8945 | } | |
8946 | if (eval_state->pos_magic) | |
25fdce4a | 8947 | { |
331b2dcc | 8948 | eval_state->pos_magic->mg_len = eval_state->pos; |
25fdce4a FC |
8949 | eval_state->pos_magic->mg_flags = |
8950 | (eval_state->pos_magic->mg_flags & ~MGf_BYTES) | |
8951 | | (eval_state->pos_flags & MGf_BYTES); | |
8952 | } | |
331b2dcc DM |
8953 | |
8954 | PL_curpm = eval_state->curpm; | |
8adc0f72 | 8955 | } |
bf2039a9 | 8956 | |
331b2dcc DM |
8957 | PL_regmatch_state = aux->old_regmatch_state; |
8958 | PL_regmatch_slab = aux->old_regmatch_slab; | |
8959 | ||
8960 | /* free all slabs above current one - this must be the last action | |
8961 | * of this function, as aux and eval_state are allocated within | |
8962 | * slabs and may be freed here */ | |
8963 | ||
8964 | s = PL_regmatch_slab->next; | |
8965 | if (s) { | |
8966 | PL_regmatch_slab->next = NULL; | |
8967 | while (s) { | |
8968 | regmatch_slab * const osl = s; | |
8969 | s = s->next; | |
8970 | Safefree(osl); | |
8971 | } | |
8972 | } | |
51371543 | 8973 | } |
33b8afdf | 8974 | |
8adc0f72 | 8975 | |
33b8afdf | 8976 | STATIC void |
5aaab254 | 8977 | S_to_utf8_substr(pTHX_ regexp *prog) |
33b8afdf | 8978 | { |
7e0d5ad7 KW |
8979 | /* Converts substr fields in prog from bytes to UTF-8, calling fbm_compile |
8980 | * on the converted value */ | |
8981 | ||
a1cac82e | 8982 | int i = 1; |
7918f24d NC |
8983 | |
8984 | PERL_ARGS_ASSERT_TO_UTF8_SUBSTR; | |
8985 | ||
a1cac82e NC |
8986 | do { |
8987 | if (prog->substrs->data[i].substr | |
8988 | && !prog->substrs->data[i].utf8_substr) { | |
8989 | SV* const sv = newSVsv(prog->substrs->data[i].substr); | |
8990 | prog->substrs->data[i].utf8_substr = sv; | |
8991 | sv_utf8_upgrade(sv); | |
610460f9 | 8992 | if (SvVALID(prog->substrs->data[i].substr)) { |
cffe132d | 8993 | if (SvTAIL(prog->substrs->data[i].substr)) { |
610460f9 NC |
8994 | /* Trim the trailing \n that fbm_compile added last |
8995 | time. */ | |
8996 | SvCUR_set(sv, SvCUR(sv) - 1); | |
8997 | /* Whilst this makes the SV technically "invalid" (as its | |
8998 | buffer is no longer followed by "\0") when fbm_compile() | |
8999 | adds the "\n" back, a "\0" is restored. */ | |
cffe132d NC |
9000 | fbm_compile(sv, FBMcf_TAIL); |
9001 | } else | |
9002 | fbm_compile(sv, 0); | |
610460f9 | 9003 | } |
a1cac82e NC |
9004 | if (prog->substrs->data[i].substr == prog->check_substr) |
9005 | prog->check_utf8 = sv; | |
9006 | } | |
9007 | } while (i--); | |
33b8afdf JH |
9008 | } |
9009 | ||
7e0d5ad7 | 9010 | STATIC bool |
5aaab254 | 9011 | S_to_byte_substr(pTHX_ regexp *prog) |
33b8afdf | 9012 | { |
7e0d5ad7 KW |
9013 | /* Converts substr fields in prog from UTF-8 to bytes, calling fbm_compile |
9014 | * on the converted value; returns FALSE if can't be converted. */ | |
9015 | ||
a1cac82e | 9016 | int i = 1; |
7918f24d NC |
9017 | |
9018 | PERL_ARGS_ASSERT_TO_BYTE_SUBSTR; | |
9019 | ||
a1cac82e NC |
9020 | do { |
9021 | if (prog->substrs->data[i].utf8_substr | |
9022 | && !prog->substrs->data[i].substr) { | |
9023 | SV* sv = newSVsv(prog->substrs->data[i].utf8_substr); | |
7e0d5ad7 KW |
9024 | if (! sv_utf8_downgrade(sv, TRUE)) { |
9025 | return FALSE; | |
9026 | } | |
5400f398 KW |
9027 | if (SvVALID(prog->substrs->data[i].utf8_substr)) { |
9028 | if (SvTAIL(prog->substrs->data[i].utf8_substr)) { | |
9029 | /* Trim the trailing \n that fbm_compile added last | |
9030 | time. */ | |
9031 | SvCUR_set(sv, SvCUR(sv) - 1); | |
9032 | fbm_compile(sv, FBMcf_TAIL); | |
9033 | } else | |
9034 | fbm_compile(sv, 0); | |
9035 | } | |
a1cac82e NC |
9036 | prog->substrs->data[i].substr = sv; |
9037 | if (prog->substrs->data[i].utf8_substr == prog->check_utf8) | |
9038 | prog->check_substr = sv; | |
33b8afdf | 9039 | } |
a1cac82e | 9040 | } while (i--); |
7e0d5ad7 KW |
9041 | |
9042 | return TRUE; | |
33b8afdf | 9043 | } |
66610fdd RGS |
9044 | |
9045 | /* | |
14d04a33 | 9046 | * ex: set ts=8 sts=4 sw=4 et: |
37442d52 | 9047 | */ |