Commit | Line | Data |
---|---|---|
a0d0e21e LW |
1 | /* regexec.c |
2 | */ | |
3 | ||
4 | /* | |
4ac71550 TC |
5 | * One Ring to rule them all, One Ring to find them |
6 | & | |
7 | * [p.v of _The Lord of the Rings_, opening poem] | |
8 | * [p.50 of _The Lord of the Rings_, I/iii: "The Shadow of the Past"] | |
9 | * [p.254 of _The Lord of the Rings_, II/ii: "The Council of Elrond"] | |
a0d0e21e LW |
10 | */ |
11 | ||
61296642 DM |
12 | /* This file contains functions for executing a regular expression. See |
13 | * also regcomp.c which funnily enough, contains functions for compiling | |
166f8a29 | 14 | * a regular expression. |
e4a054ea DM |
15 | * |
16 | * This file is also copied at build time to ext/re/re_exec.c, where | |
17 | * it's built with -DPERL_EXT_RE_BUILD -DPERL_EXT_RE_DEBUG -DPERL_EXT. | |
18 | * This causes the main functions to be compiled under new names and with | |
19 | * debugging support added, which makes "use re 'debug'" work. | |
166f8a29 DM |
20 | */ |
21 | ||
a687059c LW |
22 | /* NOTE: this is derived from Henry Spencer's regexp code, and should not |
23 | * confused with the original package (see point 3 below). Thanks, Henry! | |
24 | */ | |
25 | ||
26 | /* Additional note: this code is very heavily munged from Henry's version | |
27 | * in places. In some spots I've traded clarity for efficiency, so don't | |
28 | * blame Henry for some of the lack of readability. | |
29 | */ | |
30 | ||
e50aee73 AD |
31 | /* The names of the functions have been changed from regcomp and |
32 | * regexec to pregcomp and pregexec in order to avoid conflicts | |
33 | * with the POSIX routines of the same names. | |
34 | */ | |
35 | ||
b9d5759e | 36 | #ifdef PERL_EXT_RE_BUILD |
54df2634 | 37 | #include "re_top.h" |
9041c2e3 | 38 | #endif |
56953603 | 39 | |
a687059c | 40 | /* |
e50aee73 | 41 | * pregcomp and pregexec -- regsub and regerror are not used in perl |
a687059c LW |
42 | * |
43 | * Copyright (c) 1986 by University of Toronto. | |
44 | * Written by Henry Spencer. Not derived from licensed software. | |
45 | * | |
46 | * Permission is granted to anyone to use this software for any | |
47 | * purpose on any computer system, and to redistribute it freely, | |
48 | * subject to the following restrictions: | |
49 | * | |
50 | * 1. The author is not responsible for the consequences of use of | |
51 | * this software, no matter how awful, even if they arise | |
52 | * from defects in it. | |
53 | * | |
54 | * 2. The origin of this software must not be misrepresented, either | |
55 | * by explicit claim or by omission. | |
56 | * | |
57 | * 3. Altered versions must be plainly marked as such, and must not | |
58 | * be misrepresented as being the original software. | |
59 | * | |
60 | **** Alterations to Henry's code are... | |
61 | **** | |
4bb101f2 | 62 | **** Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, |
1129b882 NC |
63 | **** 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 |
64 | **** by Larry Wall and others | |
a687059c | 65 | **** |
9ef589d8 LW |
66 | **** You may distribute under the terms of either the GNU General Public |
67 | **** License or the Artistic License, as specified in the README file. | |
a687059c LW |
68 | * |
69 | * Beware that some of this code is subtly aware of the way operator | |
70 | * precedence is structured in regular expressions. Serious changes in | |
71 | * regular-expression syntax might require a total rethink. | |
72 | */ | |
73 | #include "EXTERN.h" | |
864dbfa3 | 74 | #define PERL_IN_REGEXEC_C |
a687059c | 75 | #include "perl.h" |
0f5d15d6 | 76 | |
54df2634 NC |
77 | #ifdef PERL_IN_XSUB_RE |
78 | # include "re_comp.h" | |
79 | #else | |
80 | # include "regcomp.h" | |
81 | #endif | |
a687059c | 82 | |
c277df42 IZ |
83 | #define RF_tainted 1 /* tainted information used? */ |
84 | #define RF_warned 2 /* warned about big count? */ | |
faec1544 | 85 | |
ab3bbdeb | 86 | #define RF_utf8 8 /* Pattern contains multibyte chars? */ |
a0ed51b3 | 87 | |
eb160463 | 88 | #define UTF ((PL_reg_flags & RF_utf8) != 0) |
ce862d02 IZ |
89 | |
90 | #define RS_init 1 /* eval environment created */ | |
91 | #define RS_set 2 /* replsv value is set */ | |
c277df42 | 92 | |
a687059c LW |
93 | #ifndef STATIC |
94 | #define STATIC static | |
95 | #endif | |
96 | ||
32fc9b6a | 97 | #define REGINCLASS(prog,p,c) (ANYOF_FLAGS(p) ? reginclass(prog,p,c,0,0) : ANYOF_BITMAP_TEST(p,*(c))) |
7d3e948e | 98 | |
c277df42 IZ |
99 | /* |
100 | * Forwards. | |
101 | */ | |
102 | ||
33b8afdf | 103 | #define CHR_SVLEN(sv) (do_utf8 ? sv_len_utf8(sv) : SvCUR(sv)) |
53c4c00c | 104 | #define CHR_DIST(a,b) (PL_reg_match_utf8 ? utf8_distance(a,b) : a - b) |
a0ed51b3 | 105 | |
3dab1dad YO |
106 | #define HOPc(pos,off) \ |
107 | (char *)(PL_reg_match_utf8 \ | |
52657f30 | 108 | ? reghop3((U8*)pos, off, (U8*)(off >= 0 ? PL_regeol : PL_bostr)) \ |
3dab1dad YO |
109 | : (U8*)(pos + off)) |
110 | #define HOPBACKc(pos, off) \ | |
07be1b83 YO |
111 | (char*)(PL_reg_match_utf8\ |
112 | ? reghopmaybe3((U8*)pos, -off, (U8*)PL_bostr) \ | |
113 | : (pos - off >= PL_bostr) \ | |
8e11feef | 114 | ? (U8*)pos - off \ |
3dab1dad | 115 | : NULL) |
efb30f32 | 116 | |
e7409c1b | 117 | #define HOP3(pos,off,lim) (PL_reg_match_utf8 ? reghop3((U8*)(pos), off, (U8*)(lim)) : (U8*)(pos + off)) |
1aa99e6b | 118 | #define HOP3c(pos,off,lim) ((char*)HOP3(pos,off,lim)) |
1aa99e6b | 119 | |
20d0b1e9 | 120 | /* these are unrolled below in the CCC_TRY_XXX defined */ |
1a4fad37 AL |
121 | #define LOAD_UTF8_CHARCLASS(class,str) STMT_START { \ |
122 | if (!CAT2(PL_utf8_,class)) { bool ok; ENTER; save_re_context(); ok=CAT2(is_utf8_,class)((const U8*)str); assert(ok); LEAVE; } } STMT_END | |
37e2e78e KW |
123 | |
124 | /* Doesn't do an assert to verify that is correct */ | |
125 | #define LOAD_UTF8_CHARCLASS_NO_CHECK(class) STMT_START { \ | |
126 | if (!CAT2(PL_utf8_,class)) { bool ok; ENTER; save_re_context(); ok=CAT2(is_utf8_,class)((const U8*)" "); LEAVE; } } STMT_END | |
127 | ||
1a4fad37 AL |
128 | #define LOAD_UTF8_CHARCLASS_ALNUM() LOAD_UTF8_CHARCLASS(alnum,"a") |
129 | #define LOAD_UTF8_CHARCLASS_DIGIT() LOAD_UTF8_CHARCLASS(digit,"0") | |
130 | #define LOAD_UTF8_CHARCLASS_SPACE() LOAD_UTF8_CHARCLASS(space," ") | |
51371543 | 131 | |
37e2e78e | 132 | #define LOAD_UTF8_CHARCLASS_GCB() /* Grapheme cluster boundaries */ \ |
d275fa5e CB |
133 | LOAD_UTF8_CHARCLASS(X_begin, " "); \ |
134 | LOAD_UTF8_CHARCLASS(X_non_hangul, "A"); \ | |
37e2e78e KW |
135 | /* These are utf8 constants, and not utf-ebcdic constants, so the \ |
136 | * assert should likely and hopefully fail on an EBCDIC machine */ \ | |
d275fa5e | 137 | LOAD_UTF8_CHARCLASS(X_extend, "\xcc\x80"); /* U+0300 */ \ |
37e2e78e KW |
138 | \ |
139 | /* No asserts are done for these, in case called on an early \ | |
140 | * Unicode version in which they map to nothing */ \ | |
d275fa5e CB |
141 | LOAD_UTF8_CHARCLASS_NO_CHECK(X_prepend);/* U+0E40 "\xe0\xb9\x80" */ \ |
142 | LOAD_UTF8_CHARCLASS_NO_CHECK(X_L); /* U+1100 "\xe1\x84\x80" */ \ | |
143 | LOAD_UTF8_CHARCLASS_NO_CHECK(X_LV); /* U+AC00 "\xea\xb0\x80" */ \ | |
144 | LOAD_UTF8_CHARCLASS_NO_CHECK(X_LVT); /* U+AC01 "\xea\xb0\x81" */ \ | |
145 | LOAD_UTF8_CHARCLASS_NO_CHECK(X_LV_LVT_V);/* U+AC01 "\xea\xb0\x81" */\ | |
146 | LOAD_UTF8_CHARCLASS_NO_CHECK(X_T); /* U+11A8 "\xe1\x86\xa8" */ \ | |
37e2e78e | 147 | LOAD_UTF8_CHARCLASS_NO_CHECK(X_V) /* U+1160 "\xe1\x85\xa0" */ |
20d0b1e9 | 148 | |
d1eb3177 YO |
149 | /* |
150 | We dont use PERL_LEGACY_UNICODE_CHARCLASS_MAPPINGS as the direct test | |
151 | so that it is possible to override the option here without having to | |
152 | rebuild the entire core. as we are required to do if we change regcomp.h | |
153 | which is where PERL_LEGACY_UNICODE_CHARCLASS_MAPPINGS is defined. | |
154 | */ | |
155 | #if PERL_LEGACY_UNICODE_CHARCLASS_MAPPINGS | |
156 | #define BROKEN_UNICODE_CHARCLASS_MAPPINGS | |
157 | #endif | |
158 | ||
159 | #ifdef BROKEN_UNICODE_CHARCLASS_MAPPINGS | |
160 | #define LOAD_UTF8_CHARCLASS_PERL_WORD() LOAD_UTF8_CHARCLASS_ALNUM() | |
161 | #define LOAD_UTF8_CHARCLASS_PERL_SPACE() LOAD_UTF8_CHARCLASS_SPACE() | |
162 | #define LOAD_UTF8_CHARCLASS_POSIX_DIGIT() LOAD_UTF8_CHARCLASS_DIGIT() | |
163 | #define RE_utf8_perl_word PL_utf8_alnum | |
164 | #define RE_utf8_perl_space PL_utf8_space | |
165 | #define RE_utf8_posix_digit PL_utf8_digit | |
166 | #define perl_word alnum | |
167 | #define perl_space space | |
168 | #define posix_digit digit | |
169 | #else | |
170 | #define LOAD_UTF8_CHARCLASS_PERL_WORD() LOAD_UTF8_CHARCLASS(perl_word,"a") | |
171 | #define LOAD_UTF8_CHARCLASS_PERL_SPACE() LOAD_UTF8_CHARCLASS(perl_space," ") | |
172 | #define LOAD_UTF8_CHARCLASS_POSIX_DIGIT() LOAD_UTF8_CHARCLASS(posix_digit,"0") | |
173 | #define RE_utf8_perl_word PL_utf8_perl_word | |
174 | #define RE_utf8_perl_space PL_utf8_perl_space | |
175 | #define RE_utf8_posix_digit PL_utf8_posix_digit | |
176 | #endif | |
177 | ||
178 | ||
20d0b1e9 YO |
179 | #define CCC_TRY_AFF(NAME,NAMEL,CLASS,STR,LCFUNC_utf8,FUNC,LCFUNC) \ |
180 | case NAMEL: \ | |
181 | PL_reg_flags |= RF_tainted; \ | |
182 | /* FALL THROUGH */ \ | |
183 | case NAME: \ | |
184 | if (!nextchr) \ | |
185 | sayNO; \ | |
186 | if (do_utf8 && UTF8_IS_CONTINUED(nextchr)) { \ | |
187 | if (!CAT2(PL_utf8_,CLASS)) { \ | |
188 | bool ok; \ | |
189 | ENTER; \ | |
190 | save_re_context(); \ | |
191 | ok=CAT2(is_utf8_,CLASS)((const U8*)STR); \ | |
192 | assert(ok); \ | |
193 | LEAVE; \ | |
194 | } \ | |
195 | if (!(OP(scan) == NAME \ | |
f2338a2e | 196 | ? cBOOL(swash_fetch(CAT2(PL_utf8_,CLASS), (U8*)locinput, do_utf8)) \ |
20d0b1e9 YO |
197 | : LCFUNC_utf8((U8*)locinput))) \ |
198 | { \ | |
199 | sayNO; \ | |
200 | } \ | |
201 | locinput += PL_utf8skip[nextchr]; \ | |
202 | nextchr = UCHARAT(locinput); \ | |
203 | break; \ | |
204 | } \ | |
205 | if (!(OP(scan) == NAME ? FUNC(nextchr) : LCFUNC(nextchr))) \ | |
206 | sayNO; \ | |
207 | nextchr = UCHARAT(++locinput); \ | |
208 | break | |
209 | ||
210 | #define CCC_TRY_NEG(NAME,NAMEL,CLASS,STR,LCFUNC_utf8,FUNC,LCFUNC) \ | |
211 | case NAMEL: \ | |
212 | PL_reg_flags |= RF_tainted; \ | |
213 | /* FALL THROUGH */ \ | |
214 | case NAME : \ | |
215 | if (!nextchr && locinput >= PL_regeol) \ | |
216 | sayNO; \ | |
217 | if (do_utf8 && UTF8_IS_CONTINUED(nextchr)) { \ | |
218 | if (!CAT2(PL_utf8_,CLASS)) { \ | |
219 | bool ok; \ | |
220 | ENTER; \ | |
221 | save_re_context(); \ | |
222 | ok=CAT2(is_utf8_,CLASS)((const U8*)STR); \ | |
223 | assert(ok); \ | |
224 | LEAVE; \ | |
225 | } \ | |
226 | if ((OP(scan) == NAME \ | |
f2338a2e | 227 | ? cBOOL(swash_fetch(CAT2(PL_utf8_,CLASS), (U8*)locinput, do_utf8)) \ |
20d0b1e9 YO |
228 | : LCFUNC_utf8((U8*)locinput))) \ |
229 | { \ | |
230 | sayNO; \ | |
231 | } \ | |
232 | locinput += PL_utf8skip[nextchr]; \ | |
233 | nextchr = UCHARAT(locinput); \ | |
234 | break; \ | |
235 | } \ | |
236 | if ((OP(scan) == NAME ? FUNC(nextchr) : LCFUNC(nextchr))) \ | |
237 | sayNO; \ | |
238 | nextchr = UCHARAT(++locinput); \ | |
239 | break | |
240 | ||
241 | ||
d1eb3177 YO |
242 | |
243 | ||
244 | ||
3dab1dad YO |
245 | /* TODO: Combine JUMPABLE and HAS_TEXT to cache OP(rn) */ |
246 | ||
5f80c4cf | 247 | /* for use after a quantifier and before an EXACT-like node -- japhy */ |
3e901dc0 YO |
248 | /* it would be nice to rework regcomp.sym to generate this stuff. sigh */ |
249 | #define JUMPABLE(rn) ( \ | |
250 | OP(rn) == OPEN || \ | |
251 | (OP(rn) == CLOSE && (!cur_eval || cur_eval->u.eval.close_paren != ARG(rn))) || \ | |
252 | OP(rn) == EVAL || \ | |
cca55fe3 JP |
253 | OP(rn) == SUSPEND || OP(rn) == IFMATCH || \ |
254 | OP(rn) == PLUS || OP(rn) == MINMOD || \ | |
ee9b8eae | 255 | OP(rn) == KEEPS || (PL_regkind[OP(rn)] == VERB) || \ |
3dab1dad | 256 | (PL_regkind[OP(rn)] == CURLY && ARG1(rn) > 0) \ |
e2d8ce26 | 257 | ) |
ee9b8eae | 258 | #define IS_EXACT(rn) (PL_regkind[OP(rn)] == EXACT) |
e2d8ce26 | 259 | |
ee9b8eae YO |
260 | #define HAS_TEXT(rn) ( IS_EXACT(rn) || PL_regkind[OP(rn)] == REF ) |
261 | ||
262 | #if 0 | |
263 | /* Currently these are only used when PL_regkind[OP(rn)] == EXACT so | |
264 | we don't need this definition. */ | |
265 | #define IS_TEXT(rn) ( OP(rn)==EXACT || OP(rn)==REF || OP(rn)==NREF ) | |
266 | #define IS_TEXTF(rn) ( OP(rn)==EXACTF || OP(rn)==REFF || OP(rn)==NREFF ) | |
267 | #define IS_TEXTFL(rn) ( OP(rn)==EXACTFL || OP(rn)==REFFL || OP(rn)==NREFFL ) | |
268 | ||
269 | #else | |
270 | /* ... so we use this as its faster. */ | |
271 | #define IS_TEXT(rn) ( OP(rn)==EXACT ) | |
272 | #define IS_TEXTF(rn) ( OP(rn)==EXACTF ) | |
273 | #define IS_TEXTFL(rn) ( OP(rn)==EXACTFL ) | |
274 | ||
275 | #endif | |
e2d8ce26 | 276 | |
a84d97b6 HS |
277 | /* |
278 | Search for mandatory following text node; for lookahead, the text must | |
279 | follow but for lookbehind (rn->flags != 0) we skip to the next step. | |
280 | */ | |
cca55fe3 | 281 | #define FIND_NEXT_IMPT(rn) STMT_START { \ |
3dab1dad YO |
282 | while (JUMPABLE(rn)) { \ |
283 | const OPCODE type = OP(rn); \ | |
284 | if (type == SUSPEND || PL_regkind[type] == CURLY) \ | |
e2d8ce26 | 285 | rn = NEXTOPER(NEXTOPER(rn)); \ |
3dab1dad | 286 | else if (type == PLUS) \ |
cca55fe3 | 287 | rn = NEXTOPER(rn); \ |
3dab1dad | 288 | else if (type == IFMATCH) \ |
a84d97b6 | 289 | rn = (rn->flags == 0) ? NEXTOPER(NEXTOPER(rn)) : rn + ARG(rn); \ |
e2d8ce26 | 290 | else rn += NEXT_OFF(rn); \ |
3dab1dad | 291 | } \ |
5f80c4cf | 292 | } STMT_END |
74750237 | 293 | |
c476f425 | 294 | |
acfe0abc | 295 | static void restore_pos(pTHX_ void *arg); |
51371543 | 296 | |
620d5b66 NC |
297 | #define REGCP_PAREN_ELEMS 4 |
298 | #define REGCP_OTHER_ELEMS 5 | |
e0fa7e2b | 299 | #define REGCP_FRAME_ELEMS 1 |
620d5b66 NC |
300 | /* REGCP_FRAME_ELEMS are not part of the REGCP_OTHER_ELEMS and |
301 | * are needed for the regexp context stack bookkeeping. */ | |
302 | ||
76e3520e | 303 | STATIC CHECKPOINT |
cea2e8a9 | 304 | S_regcppush(pTHX_ I32 parenfloor) |
a0d0e21e | 305 | { |
97aff369 | 306 | dVAR; |
a3b680e6 | 307 | const int retval = PL_savestack_ix; |
a3b680e6 | 308 | const int paren_elems_to_push = (PL_regsize - parenfloor) * REGCP_PAREN_ELEMS; |
e0fa7e2b NC |
309 | const UV total_elems = paren_elems_to_push + REGCP_OTHER_ELEMS; |
310 | const UV elems_shifted = total_elems << SAVE_TIGHT_SHIFT; | |
a0d0e21e | 311 | int p; |
40a82448 | 312 | GET_RE_DEBUG_FLAGS_DECL; |
a0d0e21e | 313 | |
e49a9654 IH |
314 | if (paren_elems_to_push < 0) |
315 | Perl_croak(aTHX_ "panic: paren_elems_to_push < 0"); | |
316 | ||
e0fa7e2b NC |
317 | if ((elems_shifted >> SAVE_TIGHT_SHIFT) != total_elems) |
318 | Perl_croak(aTHX_ "panic: paren_elems_to_push offset %"UVuf | |
5df417d0 JH |
319 | " out of range (%lu-%ld)", |
320 | total_elems, (unsigned long)PL_regsize, (long)parenfloor); | |
e0fa7e2b | 321 | |
620d5b66 | 322 | SSGROW(total_elems + REGCP_FRAME_ELEMS); |
7f69552c | 323 | |
3280af22 | 324 | for (p = PL_regsize; p > parenfloor; p--) { |
b1ce53c5 | 325 | /* REGCP_PARENS_ELEMS are pushed per pairs of parentheses. */ |
f0ab9afb NC |
326 | SSPUSHINT(PL_regoffs[p].end); |
327 | SSPUSHINT(PL_regoffs[p].start); | |
3280af22 | 328 | SSPUSHPTR(PL_reg_start_tmp[p]); |
a0d0e21e | 329 | SSPUSHINT(p); |
e7707071 | 330 | DEBUG_BUFFERS_r(PerlIO_printf(Perl_debug_log, |
40a82448 | 331 | " saving \\%"UVuf" %"IVdf"(%"IVdf")..%"IVdf"\n", |
f0ab9afb | 332 | (UV)p, (IV)PL_regoffs[p].start, |
40a82448 | 333 | (IV)(PL_reg_start_tmp[p] - PL_bostr), |
f0ab9afb | 334 | (IV)PL_regoffs[p].end |
40a82448 | 335 | )); |
a0d0e21e | 336 | } |
b1ce53c5 | 337 | /* REGCP_OTHER_ELEMS are pushed in any case, parentheses or no. */ |
f0ab9afb | 338 | SSPUSHPTR(PL_regoffs); |
3280af22 NIS |
339 | SSPUSHINT(PL_regsize); |
340 | SSPUSHINT(*PL_reglastparen); | |
a01268b5 | 341 | SSPUSHINT(*PL_reglastcloseparen); |
3280af22 | 342 | SSPUSHPTR(PL_reginput); |
e0fa7e2b | 343 | SSPUSHUV(SAVEt_REGCONTEXT | elems_shifted); /* Magic cookie. */ |
41123dfd | 344 | |
a0d0e21e LW |
345 | return retval; |
346 | } | |
347 | ||
c277df42 | 348 | /* These are needed since we do not localize EVAL nodes: */ |
ab3bbdeb YO |
349 | #define REGCP_SET(cp) \ |
350 | DEBUG_STATE_r( \ | |
ab3bbdeb | 351 | PerlIO_printf(Perl_debug_log, \ |
e4f74956 | 352 | " Setting an EVAL scope, savestack=%"IVdf"\n", \ |
ab3bbdeb YO |
353 | (IV)PL_savestack_ix)); \ |
354 | cp = PL_savestack_ix | |
c3464db5 | 355 | |
ab3bbdeb | 356 | #define REGCP_UNWIND(cp) \ |
e4f74956 | 357 | DEBUG_STATE_r( \ |
ab3bbdeb | 358 | if (cp != PL_savestack_ix) \ |
e4f74956 YO |
359 | PerlIO_printf(Perl_debug_log, \ |
360 | " Clearing an EVAL scope, savestack=%"IVdf"..%"IVdf"\n", \ | |
ab3bbdeb YO |
361 | (IV)(cp), (IV)PL_savestack_ix)); \ |
362 | regcpblow(cp) | |
c277df42 | 363 | |
76e3520e | 364 | STATIC char * |
097eb12c | 365 | S_regcppop(pTHX_ const regexp *rex) |
a0d0e21e | 366 | { |
97aff369 | 367 | dVAR; |
e0fa7e2b | 368 | UV i; |
a0d0e21e | 369 | char *input; |
a3621e74 YO |
370 | GET_RE_DEBUG_FLAGS_DECL; |
371 | ||
7918f24d NC |
372 | PERL_ARGS_ASSERT_REGCPPOP; |
373 | ||
b1ce53c5 | 374 | /* Pop REGCP_OTHER_ELEMS before the parentheses loop starts. */ |
c6bf6a65 | 375 | i = SSPOPUV; |
e0fa7e2b NC |
376 | assert((i & SAVE_MASK) == SAVEt_REGCONTEXT); /* Check that the magic cookie is there. */ |
377 | i >>= SAVE_TIGHT_SHIFT; /* Parentheses elements to pop. */ | |
a0d0e21e | 378 | input = (char *) SSPOPPTR; |
a01268b5 | 379 | *PL_reglastcloseparen = SSPOPINT; |
3280af22 NIS |
380 | *PL_reglastparen = SSPOPINT; |
381 | PL_regsize = SSPOPINT; | |
f0ab9afb | 382 | PL_regoffs=(regexp_paren_pair *) SSPOPPTR; |
b1ce53c5 | 383 | |
620d5b66 | 384 | i -= REGCP_OTHER_ELEMS; |
b1ce53c5 | 385 | /* Now restore the parentheses context. */ |
620d5b66 | 386 | for ( ; i > 0; i -= REGCP_PAREN_ELEMS) { |
1df70142 | 387 | I32 tmps; |
097eb12c | 388 | U32 paren = (U32)SSPOPINT; |
3280af22 | 389 | PL_reg_start_tmp[paren] = (char *) SSPOPPTR; |
f0ab9afb | 390 | PL_regoffs[paren].start = SSPOPINT; |
cf93c79d | 391 | tmps = SSPOPINT; |
3280af22 | 392 | if (paren <= *PL_reglastparen) |
f0ab9afb | 393 | PL_regoffs[paren].end = tmps; |
e7707071 | 394 | DEBUG_BUFFERS_r( |
c3464db5 | 395 | PerlIO_printf(Perl_debug_log, |
b900a521 | 396 | " restoring \\%"UVuf" to %"IVdf"(%"IVdf")..%"IVdf"%s\n", |
f0ab9afb | 397 | (UV)paren, (IV)PL_regoffs[paren].start, |
b900a521 | 398 | (IV)(PL_reg_start_tmp[paren] - PL_bostr), |
f0ab9afb | 399 | (IV)PL_regoffs[paren].end, |
3280af22 | 400 | (paren > *PL_reglastparen ? "(no)" : "")); |
c277df42 | 401 | ); |
a0d0e21e | 402 | } |
e7707071 | 403 | DEBUG_BUFFERS_r( |
bb7a0f54 | 404 | if (*PL_reglastparen + 1 <= rex->nparens) { |
c3464db5 | 405 | PerlIO_printf(Perl_debug_log, |
faccc32b | 406 | " restoring \\%"IVdf"..\\%"IVdf" to undef\n", |
4f639d21 | 407 | (IV)(*PL_reglastparen + 1), (IV)rex->nparens); |
c277df42 IZ |
408 | } |
409 | ); | |
daf18116 | 410 | #if 1 |
dafc8851 JH |
411 | /* It would seem that the similar code in regtry() |
412 | * already takes care of this, and in fact it is in | |
413 | * a better location to since this code can #if 0-ed out | |
414 | * but the code in regtry() is needed or otherwise tests | |
415 | * requiring null fields (pat.t#187 and split.t#{13,14} | |
daf18116 JH |
416 | * (as of patchlevel 7877) will fail. Then again, |
417 | * this code seems to be necessary or otherwise | |
225593e1 DM |
418 | * this erroneously leaves $1 defined: "1" =~ /^(?:(\d)x)?\d$/ |
419 | * --jhi updated by dapm */ | |
3b6647e0 | 420 | for (i = *PL_reglastparen + 1; i <= rex->nparens; i++) { |
097eb12c | 421 | if (i > PL_regsize) |
f0ab9afb NC |
422 | PL_regoffs[i].start = -1; |
423 | PL_regoffs[i].end = -1; | |
a0d0e21e | 424 | } |
dafc8851 | 425 | #endif |
a0d0e21e LW |
426 | return input; |
427 | } | |
428 | ||
02db2b7b | 429 | #define regcpblow(cp) LEAVE_SCOPE(cp) /* Ignores regcppush()ed data. */ |
a0d0e21e | 430 | |
a687059c | 431 | /* |
e50aee73 | 432 | * pregexec and friends |
a687059c LW |
433 | */ |
434 | ||
76234dfb | 435 | #ifndef PERL_IN_XSUB_RE |
a687059c | 436 | /* |
c277df42 | 437 | - pregexec - match a regexp against a string |
a687059c | 438 | */ |
c277df42 | 439 | I32 |
49d7dfbc | 440 | Perl_pregexec(pTHX_ REGEXP * const prog, char* stringarg, register char *strend, |
c3464db5 | 441 | char *strbeg, I32 minend, SV *screamer, U32 nosave) |
c277df42 IZ |
442 | /* strend: pointer to null at end of string */ |
443 | /* strbeg: real beginning of string */ | |
444 | /* minend: end of match must be >=minend after stringarg. */ | |
445 | /* nosave: For optimizations. */ | |
446 | { | |
7918f24d NC |
447 | PERL_ARGS_ASSERT_PREGEXEC; |
448 | ||
c277df42 | 449 | return |
9041c2e3 | 450 | regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL, |
c277df42 IZ |
451 | nosave ? 0 : REXEC_COPY_STR); |
452 | } | |
76234dfb | 453 | #endif |
22e551b9 | 454 | |
9041c2e3 | 455 | /* |
cad2e5aa JH |
456 | * Need to implement the following flags for reg_anch: |
457 | * | |
458 | * USE_INTUIT_NOML - Useful to call re_intuit_start() first | |
459 | * USE_INTUIT_ML | |
460 | * INTUIT_AUTORITATIVE_NOML - Can trust a positive answer | |
461 | * INTUIT_AUTORITATIVE_ML | |
462 | * INTUIT_ONCE_NOML - Intuit can match in one location only. | |
463 | * INTUIT_ONCE_ML | |
464 | * | |
465 | * Another flag for this function: SECOND_TIME (so that float substrs | |
466 | * with giant delta may be not rechecked). | |
467 | */ | |
468 | ||
469 | /* Assumptions: if ANCH_GPOS, then strpos is anchored. XXXX Check GPOS logic */ | |
470 | ||
3f7c398e | 471 | /* If SCREAM, then SvPVX_const(sv) should be compatible with strpos and strend. |
cad2e5aa JH |
472 | Otherwise, only SvCUR(sv) is used to get strbeg. */ |
473 | ||
474 | /* XXXX We assume that strpos is strbeg unless sv. */ | |
475 | ||
6eb5f6b9 JH |
476 | /* XXXX Some places assume that there is a fixed substring. |
477 | An update may be needed if optimizer marks as "INTUITable" | |
478 | RExen without fixed substrings. Similarly, it is assumed that | |
479 | lengths of all the strings are no more than minlen, thus they | |
480 | cannot come from lookahead. | |
40d049e4 YO |
481 | (Or minlen should take into account lookahead.) |
482 | NOTE: Some of this comment is not correct. minlen does now take account | |
483 | of lookahead/behind. Further research is required. -- demerphq | |
484 | ||
485 | */ | |
6eb5f6b9 | 486 | |
2c2d71f5 JH |
487 | /* A failure to find a constant substring means that there is no need to make |
488 | an expensive call to REx engine, thus we celebrate a failure. Similarly, | |
489 | finding a substring too deep into the string means that less calls to | |
30944b6d IZ |
490 | regtry() should be needed. |
491 | ||
492 | REx compiler's optimizer found 4 possible hints: | |
493 | a) Anchored substring; | |
494 | b) Fixed substring; | |
495 | c) Whether we are anchored (beginning-of-line or \G); | |
496 | d) First node (of those at offset 0) which may distingush positions; | |
6eb5f6b9 | 497 | We use a)b)d) and multiline-part of c), and try to find a position in the |
30944b6d IZ |
498 | string which does not contradict any of them. |
499 | */ | |
2c2d71f5 | 500 | |
6eb5f6b9 JH |
501 | /* Most of decisions we do here should have been done at compile time. |
502 | The nodes of the REx which we used for the search should have been | |
503 | deleted from the finite automaton. */ | |
504 | ||
cad2e5aa | 505 | char * |
288b8c02 | 506 | Perl_re_intuit_start(pTHX_ REGEXP * const rx, SV *sv, char *strpos, |
9f61653a | 507 | char *strend, const U32 flags, re_scream_pos_data *data) |
cad2e5aa | 508 | { |
97aff369 | 509 | dVAR; |
288b8c02 | 510 | struct regexp *const prog = (struct regexp *)SvANY(rx); |
b7953727 | 511 | register I32 start_shift = 0; |
cad2e5aa | 512 | /* Should be nonnegative! */ |
b7953727 | 513 | register I32 end_shift = 0; |
2c2d71f5 JH |
514 | register char *s; |
515 | register SV *check; | |
a1933d95 | 516 | char *strbeg; |
cad2e5aa | 517 | char *t; |
1de06328 | 518 | const bool do_utf8 = (sv && SvUTF8(sv)) ? 1 : 0; /* if no sv we have to assume bytes */ |
cad2e5aa | 519 | I32 ml_anch; |
bd61b366 SS |
520 | register char *other_last = NULL; /* other substr checked before this */ |
521 | char *check_at = NULL; /* check substr found at this pos */ | |
bbe252da | 522 | const I32 multiline = prog->extflags & RXf_PMf_MULTILINE; |
f8fc2ecf | 523 | RXi_GET_DECL(prog,progi); |
30944b6d | 524 | #ifdef DEBUGGING |
890ce7af | 525 | const char * const i_strpos = strpos; |
30944b6d | 526 | #endif |
a3621e74 YO |
527 | GET_RE_DEBUG_FLAGS_DECL; |
528 | ||
7918f24d NC |
529 | PERL_ARGS_ASSERT_RE_INTUIT_START; |
530 | ||
288b8c02 | 531 | RX_MATCH_UTF8_set(rx,do_utf8); |
cad2e5aa | 532 | |
3c8556c3 | 533 | if (RX_UTF8(rx)) { |
b8d68ded JH |
534 | PL_reg_flags |= RF_utf8; |
535 | } | |
ab3bbdeb | 536 | DEBUG_EXECUTE_r( |
efd26800 | 537 | debug_start_match(rx, do_utf8, strpos, strend, |
1de06328 YO |
538 | sv ? "Guessing start of match in sv for" |
539 | : "Guessing start of match in string for"); | |
2a782b5b | 540 | ); |
cad2e5aa | 541 | |
c344f387 JH |
542 | /* CHR_DIST() would be more correct here but it makes things slow. */ |
543 | if (prog->minlen > strend - strpos) { | |
a3621e74 | 544 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, |
a72c7584 | 545 | "String too short... [re_intuit_start]\n")); |
cad2e5aa | 546 | goto fail; |
2c2d71f5 | 547 | } |
1de06328 | 548 | |
a1933d95 | 549 | strbeg = (sv && SvPOK(sv)) ? strend - SvCUR(sv) : strpos; |
1aa99e6b | 550 | PL_regeol = strend; |
33b8afdf JH |
551 | if (do_utf8) { |
552 | if (!prog->check_utf8 && prog->check_substr) | |
553 | to_utf8_substr(prog); | |
554 | check = prog->check_utf8; | |
555 | } else { | |
556 | if (!prog->check_substr && prog->check_utf8) | |
557 | to_byte_substr(prog); | |
558 | check = prog->check_substr; | |
559 | } | |
1de06328 | 560 | if (check == &PL_sv_undef) { |
a3621e74 | 561 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, |
1de06328 | 562 | "Non-utf8 string cannot match utf8 check string\n")); |
33b8afdf JH |
563 | goto fail; |
564 | } | |
bbe252da YO |
565 | if (prog->extflags & RXf_ANCH) { /* Match at beg-of-str or after \n */ |
566 | ml_anch = !( (prog->extflags & RXf_ANCH_SINGLE) | |
567 | || ( (prog->extflags & RXf_ANCH_BOL) | |
7fba1cd6 | 568 | && !multiline ) ); /* Check after \n? */ |
cad2e5aa | 569 | |
7e25d62c | 570 | if (!ml_anch) { |
bbe252da YO |
571 | if ( !(prog->extflags & RXf_ANCH_GPOS) /* Checked by the caller */ |
572 | && !(prog->intflags & PREGf_IMPLICIT) /* not a real BOL */ | |
3f7c398e | 573 | /* SvCUR is not set on references: SvRV and SvPVX_const overlap */ |
7e25d62c JH |
574 | && sv && !SvROK(sv) |
575 | && (strpos != strbeg)) { | |
a3621e74 | 576 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Not at start...\n")); |
7e25d62c JH |
577 | goto fail; |
578 | } | |
579 | if (prog->check_offset_min == prog->check_offset_max && | |
bbe252da | 580 | !(prog->extflags & RXf_CANY_SEEN)) { |
2c2d71f5 | 581 | /* Substring at constant offset from beg-of-str... */ |
cad2e5aa JH |
582 | I32 slen; |
583 | ||
1aa99e6b | 584 | s = HOP3c(strpos, prog->check_offset_min, strend); |
1de06328 | 585 | |
653099ff GS |
586 | if (SvTAIL(check)) { |
587 | slen = SvCUR(check); /* >= 1 */ | |
cad2e5aa | 588 | |
9041c2e3 | 589 | if ( strend - s > slen || strend - s < slen - 1 |
2c2d71f5 | 590 | || (strend - s == slen && strend[-1] != '\n')) { |
a3621e74 | 591 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "String too long...\n")); |
2c2d71f5 | 592 | goto fail_finish; |
cad2e5aa JH |
593 | } |
594 | /* Now should match s[0..slen-2] */ | |
595 | slen--; | |
3f7c398e | 596 | if (slen && (*SvPVX_const(check) != *s |
cad2e5aa | 597 | || (slen > 1 |
3f7c398e | 598 | && memNE(SvPVX_const(check), s, slen)))) { |
2c2d71f5 | 599 | report_neq: |
a3621e74 | 600 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "String not equal...\n")); |
2c2d71f5 JH |
601 | goto fail_finish; |
602 | } | |
cad2e5aa | 603 | } |
3f7c398e | 604 | else if (*SvPVX_const(check) != *s |
653099ff | 605 | || ((slen = SvCUR(check)) > 1 |
3f7c398e | 606 | && memNE(SvPVX_const(check), s, slen))) |
2c2d71f5 | 607 | goto report_neq; |
c315bfe8 | 608 | check_at = s; |
2c2d71f5 | 609 | goto success_at_start; |
7e25d62c | 610 | } |
cad2e5aa | 611 | } |
2c2d71f5 | 612 | /* Match is anchored, but substr is not anchored wrt beg-of-str. */ |
cad2e5aa | 613 | s = strpos; |
2c2d71f5 | 614 | start_shift = prog->check_offset_min; /* okay to underestimate on CC */ |
1de06328 YO |
615 | end_shift = prog->check_end_shift; |
616 | ||
2c2d71f5 | 617 | if (!ml_anch) { |
a3b680e6 | 618 | const I32 end = prog->check_offset_max + CHR_SVLEN(check) |
653099ff | 619 | - (SvTAIL(check) != 0); |
a3b680e6 | 620 | const I32 eshift = CHR_DIST((U8*)strend, (U8*)s) - end; |
2c2d71f5 JH |
621 | |
622 | if (end_shift < eshift) | |
623 | end_shift = eshift; | |
624 | } | |
cad2e5aa | 625 | } |
2c2d71f5 | 626 | else { /* Can match at random position */ |
cad2e5aa JH |
627 | ml_anch = 0; |
628 | s = strpos; | |
1de06328 YO |
629 | start_shift = prog->check_offset_min; /* okay to underestimate on CC */ |
630 | end_shift = prog->check_end_shift; | |
631 | ||
632 | /* end shift should be non negative here */ | |
cad2e5aa JH |
633 | } |
634 | ||
bcdf7404 | 635 | #ifdef QDEBUGGING /* 7/99: reports of failure (with the older version) */ |
0033605d | 636 | if (end_shift < 0) |
1de06328 | 637 | Perl_croak(aTHX_ "panic: end_shift: %"IVdf" pattern:\n%s\n ", |
220fc49f | 638 | (IV)end_shift, RX_PRECOMP(prog)); |
2c2d71f5 JH |
639 | #endif |
640 | ||
2c2d71f5 JH |
641 | restart: |
642 | /* Find a possible match in the region s..strend by looking for | |
643 | the "check" substring in the region corrected by start/end_shift. */ | |
1de06328 YO |
644 | |
645 | { | |
646 | I32 srch_start_shift = start_shift; | |
647 | I32 srch_end_shift = end_shift; | |
648 | if (srch_start_shift < 0 && strbeg - s > srch_start_shift) { | |
649 | srch_end_shift -= ((strbeg - s) - srch_start_shift); | |
650 | srch_start_shift = strbeg - s; | |
651 | } | |
6bda09f9 | 652 | DEBUG_OPTIMISE_MORE_r({ |
1de06328 YO |
653 | PerlIO_printf(Perl_debug_log, "Check offset min: %"IVdf" Start shift: %"IVdf" End shift %"IVdf" Real End Shift: %"IVdf"\n", |
654 | (IV)prog->check_offset_min, | |
655 | (IV)srch_start_shift, | |
656 | (IV)srch_end_shift, | |
657 | (IV)prog->check_end_shift); | |
658 | }); | |
659 | ||
cad2e5aa | 660 | if (flags & REXEC_SCREAM) { |
cad2e5aa | 661 | I32 p = -1; /* Internal iterator of scream. */ |
a3b680e6 | 662 | I32 * const pp = data ? data->scream_pos : &p; |
cad2e5aa | 663 | |
2c2d71f5 JH |
664 | if (PL_screamfirst[BmRARE(check)] >= 0 |
665 | || ( BmRARE(check) == '\n' | |
85c508c3 | 666 | && (BmPREVIOUS(check) == SvCUR(check) - 1) |
2c2d71f5 | 667 | && SvTAIL(check) )) |
9041c2e3 | 668 | s = screaminstr(sv, check, |
1de06328 | 669 | srch_start_shift + (s - strbeg), srch_end_shift, pp, 0); |
cad2e5aa | 670 | else |
2c2d71f5 | 671 | goto fail_finish; |
4addbd3b | 672 | /* we may be pointing at the wrong string */ |
07bc277f | 673 | if (s && RXp_MATCH_COPIED(prog)) |
3f7c398e | 674 | s = strbeg + (s - SvPVX_const(sv)); |
cad2e5aa JH |
675 | if (data) |
676 | *data->scream_olds = s; | |
677 | } | |
1de06328 YO |
678 | else { |
679 | U8* start_point; | |
680 | U8* end_point; | |
bbe252da | 681 | if (prog->extflags & RXf_CANY_SEEN) { |
1de06328 YO |
682 | start_point= (U8*)(s + srch_start_shift); |
683 | end_point= (U8*)(strend - srch_end_shift); | |
684 | } else { | |
685 | start_point= HOP3(s, srch_start_shift, srch_start_shift < 0 ? strbeg : strend); | |
686 | end_point= HOP3(strend, -srch_end_shift, strbeg); | |
687 | } | |
6bda09f9 | 688 | DEBUG_OPTIMISE_MORE_r({ |
56570a2c | 689 | PerlIO_printf(Perl_debug_log, "fbm_instr len=%d str=<%.*s>\n", |
1de06328 | 690 | (int)(end_point - start_point), |
fc8cd66c | 691 | (int)(end_point - start_point) > 20 ? 20 : (int)(end_point - start_point), |
1de06328 YO |
692 | start_point); |
693 | }); | |
694 | ||
695 | s = fbm_instr( start_point, end_point, | |
7fba1cd6 | 696 | check, multiline ? FBMrf_MULTILINE : 0); |
1de06328 YO |
697 | } |
698 | } | |
cad2e5aa JH |
699 | /* Update the count-of-usability, remove useless subpatterns, |
700 | unshift s. */ | |
2c2d71f5 | 701 | |
ab3bbdeb YO |
702 | DEBUG_EXECUTE_r({ |
703 | RE_PV_QUOTED_DECL(quoted, do_utf8, PERL_DEBUG_PAD_ZERO(0), | |
704 | SvPVX_const(check), RE_SV_DUMPLEN(check), 30); | |
705 | PerlIO_printf(Perl_debug_log, "%s %s substr %s%s%s", | |
2c2d71f5 | 706 | (s ? "Found" : "Did not find"), |
ab3bbdeb YO |
707 | (check == (do_utf8 ? prog->anchored_utf8 : prog->anchored_substr) |
708 | ? "anchored" : "floating"), | |
709 | quoted, | |
710 | RE_SV_TAIL(check), | |
711 | (s ? " at offset " : "...\n") ); | |
712 | }); | |
2c2d71f5 JH |
713 | |
714 | if (!s) | |
715 | goto fail_finish; | |
2c2d71f5 | 716 | /* Finish the diagnostic message */ |
a3621e74 | 717 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%ld...\n", (long)(s - i_strpos)) ); |
2c2d71f5 | 718 | |
1de06328 YO |
719 | /* XXX dmq: first branch is for positive lookbehind... |
720 | Our check string is offset from the beginning of the pattern. | |
721 | So we need to do any stclass tests offset forward from that | |
722 | point. I think. :-( | |
723 | */ | |
724 | ||
725 | ||
726 | ||
727 | check_at=s; | |
728 | ||
729 | ||
2c2d71f5 JH |
730 | /* Got a candidate. Check MBOL anchoring, and the *other* substr. |
731 | Start with the other substr. | |
732 | XXXX no SCREAM optimization yet - and a very coarse implementation | |
a0288114 | 733 | XXXX /ttx+/ results in anchored="ttx", floating="x". floating will |
2c2d71f5 JH |
734 | *always* match. Probably should be marked during compile... |
735 | Probably it is right to do no SCREAM here... | |
736 | */ | |
737 | ||
1de06328 YO |
738 | if (do_utf8 ? (prog->float_utf8 && prog->anchored_utf8) |
739 | : (prog->float_substr && prog->anchored_substr)) | |
740 | { | |
30944b6d | 741 | /* Take into account the "other" substring. */ |
2c2d71f5 JH |
742 | /* XXXX May be hopelessly wrong for UTF... */ |
743 | if (!other_last) | |
6eb5f6b9 | 744 | other_last = strpos; |
33b8afdf | 745 | if (check == (do_utf8 ? prog->float_utf8 : prog->float_substr)) { |
30944b6d IZ |
746 | do_other_anchored: |
747 | { | |
890ce7af AL |
748 | char * const last = HOP3c(s, -start_shift, strbeg); |
749 | char *last1, *last2; | |
be8e71aa | 750 | char * const saved_s = s; |
33b8afdf | 751 | SV* must; |
2c2d71f5 | 752 | |
2c2d71f5 JH |
753 | t = s - prog->check_offset_max; |
754 | if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */ | |
1d86a7f9 | 755 | && (!do_utf8 |
0ce71af7 | 756 | || ((t = (char*)reghopmaybe3((U8*)s, -(prog->check_offset_max), (U8*)strpos)) |
2c2d71f5 | 757 | && t > strpos))) |
6f207bd3 | 758 | NOOP; |
2c2d71f5 JH |
759 | else |
760 | t = strpos; | |
1aa99e6b | 761 | t = HOP3c(t, prog->anchored_offset, strend); |
6eb5f6b9 JH |
762 | if (t < other_last) /* These positions already checked */ |
763 | t = other_last; | |
1aa99e6b | 764 | last2 = last1 = HOP3c(strend, -prog->minlen, strbeg); |
2c2d71f5 JH |
765 | if (last < last1) |
766 | last1 = last; | |
1de06328 YO |
767 | /* XXXX It is not documented what units *_offsets are in. |
768 | We assume bytes, but this is clearly wrong. | |
769 | Meaning this code needs to be carefully reviewed for errors. | |
770 | dmq. | |
771 | */ | |
772 | ||
2c2d71f5 | 773 | /* On end-of-str: see comment below. */ |
33b8afdf JH |
774 | must = do_utf8 ? prog->anchored_utf8 : prog->anchored_substr; |
775 | if (must == &PL_sv_undef) { | |
776 | s = (char*)NULL; | |
1de06328 | 777 | DEBUG_r(must = prog->anchored_utf8); /* for debug */ |
33b8afdf JH |
778 | } |
779 | else | |
780 | s = fbm_instr( | |
781 | (unsigned char*)t, | |
782 | HOP3(HOP3(last1, prog->anchored_offset, strend) | |
783 | + SvCUR(must), -(SvTAIL(must)!=0), strbeg), | |
784 | must, | |
7fba1cd6 | 785 | multiline ? FBMrf_MULTILINE : 0 |
33b8afdf | 786 | ); |
ab3bbdeb YO |
787 | DEBUG_EXECUTE_r({ |
788 | RE_PV_QUOTED_DECL(quoted, do_utf8, PERL_DEBUG_PAD_ZERO(0), | |
789 | SvPVX_const(must), RE_SV_DUMPLEN(must), 30); | |
790 | PerlIO_printf(Perl_debug_log, "%s anchored substr %s%s", | |
2c2d71f5 | 791 | (s ? "Found" : "Contradicts"), |
ab3bbdeb YO |
792 | quoted, RE_SV_TAIL(must)); |
793 | }); | |
794 | ||
795 | ||
2c2d71f5 JH |
796 | if (!s) { |
797 | if (last1 >= last2) { | |
a3621e74 | 798 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, |
2c2d71f5 JH |
799 | ", giving up...\n")); |
800 | goto fail_finish; | |
801 | } | |
a3621e74 | 802 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, |
2c2d71f5 | 803 | ", trying floating at offset %ld...\n", |
be8e71aa | 804 | (long)(HOP3c(saved_s, 1, strend) - i_strpos))); |
1aa99e6b IH |
805 | other_last = HOP3c(last1, prog->anchored_offset+1, strend); |
806 | s = HOP3c(last, 1, strend); | |
2c2d71f5 JH |
807 | goto restart; |
808 | } | |
809 | else { | |
a3621e74 | 810 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n", |
30944b6d | 811 | (long)(s - i_strpos))); |
1aa99e6b IH |
812 | t = HOP3c(s, -prog->anchored_offset, strbeg); |
813 | other_last = HOP3c(s, 1, strend); | |
be8e71aa | 814 | s = saved_s; |
2c2d71f5 JH |
815 | if (t == strpos) |
816 | goto try_at_start; | |
2c2d71f5 JH |
817 | goto try_at_offset; |
818 | } | |
30944b6d | 819 | } |
2c2d71f5 JH |
820 | } |
821 | else { /* Take into account the floating substring. */ | |
33b8afdf | 822 | char *last, *last1; |
be8e71aa | 823 | char * const saved_s = s; |
33b8afdf JH |
824 | SV* must; |
825 | ||
826 | t = HOP3c(s, -start_shift, strbeg); | |
827 | last1 = last = | |
828 | HOP3c(strend, -prog->minlen + prog->float_min_offset, strbeg); | |
829 | if (CHR_DIST((U8*)last, (U8*)t) > prog->float_max_offset) | |
830 | last = HOP3c(t, prog->float_max_offset, strend); | |
831 | s = HOP3c(t, prog->float_min_offset, strend); | |
832 | if (s < other_last) | |
833 | s = other_last; | |
2c2d71f5 | 834 | /* XXXX It is not documented what units *_offsets are in. Assume bytes. */ |
33b8afdf JH |
835 | must = do_utf8 ? prog->float_utf8 : prog->float_substr; |
836 | /* fbm_instr() takes into account exact value of end-of-str | |
837 | if the check is SvTAIL(ed). Since false positives are OK, | |
838 | and end-of-str is not later than strend we are OK. */ | |
839 | if (must == &PL_sv_undef) { | |
840 | s = (char*)NULL; | |
1de06328 | 841 | DEBUG_r(must = prog->float_utf8); /* for debug message */ |
33b8afdf JH |
842 | } |
843 | else | |
2c2d71f5 | 844 | s = fbm_instr((unsigned char*)s, |
33b8afdf JH |
845 | (unsigned char*)last + SvCUR(must) |
846 | - (SvTAIL(must)!=0), | |
7fba1cd6 | 847 | must, multiline ? FBMrf_MULTILINE : 0); |
ab3bbdeb YO |
848 | DEBUG_EXECUTE_r({ |
849 | RE_PV_QUOTED_DECL(quoted, do_utf8, PERL_DEBUG_PAD_ZERO(0), | |
850 | SvPVX_const(must), RE_SV_DUMPLEN(must), 30); | |
851 | PerlIO_printf(Perl_debug_log, "%s floating substr %s%s", | |
33b8afdf | 852 | (s ? "Found" : "Contradicts"), |
ab3bbdeb YO |
853 | quoted, RE_SV_TAIL(must)); |
854 | }); | |
33b8afdf JH |
855 | if (!s) { |
856 | if (last1 == last) { | |
a3621e74 | 857 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, |
33b8afdf JH |
858 | ", giving up...\n")); |
859 | goto fail_finish; | |
2c2d71f5 | 860 | } |
a3621e74 | 861 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, |
33b8afdf | 862 | ", trying anchored starting at offset %ld...\n", |
be8e71aa | 863 | (long)(saved_s + 1 - i_strpos))); |
33b8afdf JH |
864 | other_last = last; |
865 | s = HOP3c(t, 1, strend); | |
866 | goto restart; | |
867 | } | |
868 | else { | |
a3621e74 | 869 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n", |
33b8afdf JH |
870 | (long)(s - i_strpos))); |
871 | other_last = s; /* Fix this later. --Hugo */ | |
be8e71aa | 872 | s = saved_s; |
33b8afdf JH |
873 | if (t == strpos) |
874 | goto try_at_start; | |
875 | goto try_at_offset; | |
876 | } | |
2c2d71f5 | 877 | } |
cad2e5aa | 878 | } |
2c2d71f5 | 879 | |
1de06328 | 880 | |
9ef43ace | 881 | t= (char*)HOP3( s, -prog->check_offset_max, (prog->check_offset_max<0) ? strend : strpos); |
1de06328 | 882 | |
6bda09f9 | 883 | DEBUG_OPTIMISE_MORE_r( |
1de06328 YO |
884 | PerlIO_printf(Perl_debug_log, |
885 | "Check offset min:%"IVdf" max:%"IVdf" S:%"IVdf" t:%"IVdf" D:%"IVdf" end:%"IVdf"\n", | |
886 | (IV)prog->check_offset_min, | |
887 | (IV)prog->check_offset_max, | |
888 | (IV)(s-strpos), | |
889 | (IV)(t-strpos), | |
890 | (IV)(t-s), | |
891 | (IV)(strend-strpos) | |
892 | ) | |
893 | ); | |
894 | ||
2c2d71f5 | 895 | if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */ |
1d86a7f9 | 896 | && (!do_utf8 |
9ef43ace | 897 | || ((t = (char*)reghopmaybe3((U8*)s, -prog->check_offset_max, (U8*) ((prog->check_offset_max<0) ? strend : strpos))) |
1de06328 YO |
898 | && t > strpos))) |
899 | { | |
2c2d71f5 JH |
900 | /* Fixed substring is found far enough so that the match |
901 | cannot start at strpos. */ | |
902 | try_at_offset: | |
cad2e5aa | 903 | if (ml_anch && t[-1] != '\n') { |
30944b6d IZ |
904 | /* Eventually fbm_*() should handle this, but often |
905 | anchored_offset is not 0, so this check will not be wasted. */ | |
906 | /* XXXX In the code below we prefer to look for "^" even in | |
907 | presence of anchored substrings. And we search even | |
908 | beyond the found float position. These pessimizations | |
909 | are historical artefacts only. */ | |
910 | find_anchor: | |
2c2d71f5 | 911 | while (t < strend - prog->minlen) { |
cad2e5aa | 912 | if (*t == '\n') { |
4ee3650e | 913 | if (t < check_at - prog->check_offset_min) { |
33b8afdf | 914 | if (do_utf8 ? prog->anchored_utf8 : prog->anchored_substr) { |
4ee3650e GS |
915 | /* Since we moved from the found position, |
916 | we definitely contradict the found anchored | |
30944b6d IZ |
917 | substr. Due to the above check we do not |
918 | contradict "check" substr. | |
919 | Thus we can arrive here only if check substr | |
920 | is float. Redo checking for "other"=="fixed". | |
921 | */ | |
9041c2e3 | 922 | strpos = t + 1; |
a3621e74 | 923 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld, rescanning for anchored from offset %ld...\n", |
e4584336 | 924 | PL_colors[0], PL_colors[1], (long)(strpos - i_strpos), (long)(strpos - i_strpos + prog->anchored_offset))); |
30944b6d IZ |
925 | goto do_other_anchored; |
926 | } | |
4ee3650e GS |
927 | /* We don't contradict the found floating substring. */ |
928 | /* XXXX Why not check for STCLASS? */ | |
cad2e5aa | 929 | s = t + 1; |
a3621e74 | 930 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld...\n", |
e4584336 | 931 | PL_colors[0], PL_colors[1], (long)(s - i_strpos))); |
cad2e5aa JH |
932 | goto set_useful; |
933 | } | |
4ee3650e GS |
934 | /* Position contradicts check-string */ |
935 | /* XXXX probably better to look for check-string | |
936 | than for "\n", so one should lower the limit for t? */ | |
a3621e74 | 937 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m, restarting lookup for check-string at offset %ld...\n", |
e4584336 | 938 | PL_colors[0], PL_colors[1], (long)(t + 1 - i_strpos))); |
0e41cd87 | 939 | other_last = strpos = s = t + 1; |
cad2e5aa JH |
940 | goto restart; |
941 | } | |
942 | t++; | |
943 | } | |
a3621e74 | 944 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Did not find /%s^%s/m...\n", |
e4584336 | 945 | PL_colors[0], PL_colors[1])); |
2c2d71f5 | 946 | goto fail_finish; |
cad2e5aa | 947 | } |
f5952150 | 948 | else { |
a3621e74 | 949 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Starting position does not contradict /%s^%s/m...\n", |
e4584336 | 950 | PL_colors[0], PL_colors[1])); |
f5952150 | 951 | } |
cad2e5aa JH |
952 | s = t; |
953 | set_useful: | |
33b8afdf | 954 | ++BmUSEFUL(do_utf8 ? prog->check_utf8 : prog->check_substr); /* hooray/5 */ |
cad2e5aa JH |
955 | } |
956 | else { | |
f5952150 | 957 | /* The found string does not prohibit matching at strpos, |
2c2d71f5 | 958 | - no optimization of calling REx engine can be performed, |
f5952150 GS |
959 | unless it was an MBOL and we are not after MBOL, |
960 | or a future STCLASS check will fail this. */ | |
2c2d71f5 JH |
961 | try_at_start: |
962 | /* Even in this situation we may use MBOL flag if strpos is offset | |
963 | wrt the start of the string. */ | |
05b4157f | 964 | if (ml_anch && sv && !SvROK(sv) /* See prev comment on SvROK */ |
a1933d95 | 965 | && (strpos != strbeg) && strpos[-1] != '\n' |
d506a20d | 966 | /* May be due to an implicit anchor of m{.*foo} */ |
bbe252da | 967 | && !(prog->intflags & PREGf_IMPLICIT)) |
d506a20d | 968 | { |
cad2e5aa JH |
969 | t = strpos; |
970 | goto find_anchor; | |
971 | } | |
a3621e74 | 972 | DEBUG_EXECUTE_r( if (ml_anch) |
f5952150 | 973 | PerlIO_printf(Perl_debug_log, "Position at offset %ld does not contradict /%s^%s/m...\n", |
70685ca0 | 974 | (long)(strpos - i_strpos), PL_colors[0], PL_colors[1]); |
30944b6d | 975 | ); |
2c2d71f5 | 976 | success_at_start: |
bbe252da | 977 | if (!(prog->intflags & PREGf_NAUGHTY) /* XXXX If strpos moved? */ |
33b8afdf JH |
978 | && (do_utf8 ? ( |
979 | prog->check_utf8 /* Could be deleted already */ | |
980 | && --BmUSEFUL(prog->check_utf8) < 0 | |
981 | && (prog->check_utf8 == prog->float_utf8) | |
982 | ) : ( | |
983 | prog->check_substr /* Could be deleted already */ | |
984 | && --BmUSEFUL(prog->check_substr) < 0 | |
985 | && (prog->check_substr == prog->float_substr) | |
986 | ))) | |
66e933ab | 987 | { |
cad2e5aa | 988 | /* If flags & SOMETHING - do not do it many times on the same match */ |
a3621e74 | 989 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "... Disabling check substring...\n")); |
ef8d46e8 | 990 | /* XXX Does the destruction order has to change with do_utf8? */ |
33b8afdf | 991 | SvREFCNT_dec(do_utf8 ? prog->check_utf8 : prog->check_substr); |
ef8d46e8 | 992 | SvREFCNT_dec(do_utf8 ? prog->check_substr : prog->check_utf8); |
a0714e2c SS |
993 | prog->check_substr = prog->check_utf8 = NULL; /* disable */ |
994 | prog->float_substr = prog->float_utf8 = NULL; /* clear */ | |
995 | check = NULL; /* abort */ | |
cad2e5aa | 996 | s = strpos; |
3cf5c195 IZ |
997 | /* XXXX This is a remnant of the old implementation. It |
998 | looks wasteful, since now INTUIT can use many | |
6eb5f6b9 | 999 | other heuristics. */ |
bbe252da | 1000 | prog->extflags &= ~RXf_USE_INTUIT; |
cad2e5aa JH |
1001 | } |
1002 | else | |
1003 | s = strpos; | |
1004 | } | |
1005 | ||
6eb5f6b9 JH |
1006 | /* Last resort... */ |
1007 | /* XXXX BmUSEFUL already changed, maybe multiple change is meaningful... */ | |
1de06328 YO |
1008 | /* trie stclasses are too expensive to use here, we are better off to |
1009 | leave it to regmatch itself */ | |
f8fc2ecf | 1010 | if (progi->regstclass && PL_regkind[OP(progi->regstclass)]!=TRIE) { |
6eb5f6b9 JH |
1011 | /* minlen == 0 is possible if regstclass is \b or \B, |
1012 | and the fixed substr is ''$. | |
1013 | Since minlen is already taken into account, s+1 is before strend; | |
1014 | accidentally, minlen >= 1 guaranties no false positives at s + 1 | |
1015 | even for \b or \B. But (minlen? 1 : 0) below assumes that | |
1016 | regstclass does not come from lookahead... */ | |
1017 | /* If regstclass takes bytelength more than 1: If charlength==1, OK. | |
1018 | This leaves EXACTF only, which is dealt with in find_byclass(). */ | |
f8fc2ecf YO |
1019 | const U8* const str = (U8*)STRING(progi->regstclass); |
1020 | const int cl_l = (PL_regkind[OP(progi->regstclass)] == EXACT | |
1021 | ? CHR_DIST(str+STR_LEN(progi->regstclass), str) | |
66e933ab | 1022 | : 1); |
1de06328 YO |
1023 | char * endpos; |
1024 | if (prog->anchored_substr || prog->anchored_utf8 || ml_anch) | |
1025 | endpos= HOP3c(s, (prog->minlen ? cl_l : 0), strend); | |
1026 | else if (prog->float_substr || prog->float_utf8) | |
1027 | endpos= HOP3c(HOP3c(check_at, -start_shift, strbeg), cl_l, strend); | |
1028 | else | |
1029 | endpos= strend; | |
1030 | ||
70685ca0 JH |
1031 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "start_shift: %"IVdf" check_at: %"IVdf" s: %"IVdf" endpos: %"IVdf"\n", |
1032 | (IV)start_shift, (IV)(check_at - strbeg), (IV)(s - strbeg), (IV)(endpos - strbeg))); | |
1de06328 | 1033 | |
6eb5f6b9 | 1034 | t = s; |
f8fc2ecf | 1035 | s = find_byclass(prog, progi->regstclass, s, endpos, NULL); |
6eb5f6b9 JH |
1036 | if (!s) { |
1037 | #ifdef DEBUGGING | |
cbbf8932 | 1038 | const char *what = NULL; |
6eb5f6b9 JH |
1039 | #endif |
1040 | if (endpos == strend) { | |
a3621e74 | 1041 | DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log, |
6eb5f6b9 JH |
1042 | "Could not match STCLASS...\n") ); |
1043 | goto fail; | |
1044 | } | |
a3621e74 | 1045 | DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log, |
66e933ab | 1046 | "This position contradicts STCLASS...\n") ); |
bbe252da | 1047 | if ((prog->extflags & RXf_ANCH) && !ml_anch) |
653099ff | 1048 | goto fail; |
6eb5f6b9 | 1049 | /* Contradict one of substrings */ |
33b8afdf JH |
1050 | if (prog->anchored_substr || prog->anchored_utf8) { |
1051 | if ((do_utf8 ? prog->anchored_utf8 : prog->anchored_substr) == check) { | |
a3621e74 | 1052 | DEBUG_EXECUTE_r( what = "anchored" ); |
6eb5f6b9 | 1053 | hop_and_restart: |
1aa99e6b | 1054 | s = HOP3c(t, 1, strend); |
66e933ab GS |
1055 | if (s + start_shift + end_shift > strend) { |
1056 | /* XXXX Should be taken into account earlier? */ | |
a3621e74 | 1057 | DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log, |
66e933ab GS |
1058 | "Could not match STCLASS...\n") ); |
1059 | goto fail; | |
1060 | } | |
5e39e1e5 HS |
1061 | if (!check) |
1062 | goto giveup; | |
a3621e74 | 1063 | DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log, |
f5952150 | 1064 | "Looking for %s substr starting at offset %ld...\n", |
6eb5f6b9 JH |
1065 | what, (long)(s + start_shift - i_strpos)) ); |
1066 | goto restart; | |
1067 | } | |
66e933ab | 1068 | /* Have both, check_string is floating */ |
6eb5f6b9 JH |
1069 | if (t + start_shift >= check_at) /* Contradicts floating=check */ |
1070 | goto retry_floating_check; | |
1071 | /* Recheck anchored substring, but not floating... */ | |
9041c2e3 | 1072 | s = check_at; |
5e39e1e5 HS |
1073 | if (!check) |
1074 | goto giveup; | |
a3621e74 | 1075 | DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log, |
f5952150 | 1076 | "Looking for anchored substr starting at offset %ld...\n", |
6eb5f6b9 JH |
1077 | (long)(other_last - i_strpos)) ); |
1078 | goto do_other_anchored; | |
1079 | } | |
60e71179 GS |
1080 | /* Another way we could have checked stclass at the |
1081 | current position only: */ | |
1082 | if (ml_anch) { | |
1083 | s = t = t + 1; | |
5e39e1e5 HS |
1084 | if (!check) |
1085 | goto giveup; | |
a3621e74 | 1086 | DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log, |
f5952150 | 1087 | "Looking for /%s^%s/m starting at offset %ld...\n", |
e4584336 | 1088 | PL_colors[0], PL_colors[1], (long)(t - i_strpos)) ); |
60e71179 | 1089 | goto try_at_offset; |
66e933ab | 1090 | } |
33b8afdf | 1091 | if (!(do_utf8 ? prog->float_utf8 : prog->float_substr)) /* Could have been deleted */ |
60e71179 | 1092 | goto fail; |
6eb5f6b9 JH |
1093 | /* Check is floating subtring. */ |
1094 | retry_floating_check: | |
1095 | t = check_at - start_shift; | |
a3621e74 | 1096 | DEBUG_EXECUTE_r( what = "floating" ); |
6eb5f6b9 JH |
1097 | goto hop_and_restart; |
1098 | } | |
b7953727 | 1099 | if (t != s) { |
a3621e74 | 1100 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, |
6eb5f6b9 | 1101 | "By STCLASS: moving %ld --> %ld\n", |
b7953727 JH |
1102 | (long)(t - i_strpos), (long)(s - i_strpos)) |
1103 | ); | |
1104 | } | |
1105 | else { | |
a3621e74 | 1106 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, |
b7953727 JH |
1107 | "Does not contradict STCLASS...\n"); |
1108 | ); | |
1109 | } | |
6eb5f6b9 | 1110 | } |
5e39e1e5 | 1111 | giveup: |
a3621e74 | 1112 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%s%s:%s match at offset %ld\n", |
5e39e1e5 HS |
1113 | PL_colors[4], (check ? "Guessed" : "Giving up"), |
1114 | PL_colors[5], (long)(s - i_strpos)) ); | |
cad2e5aa | 1115 | return s; |
2c2d71f5 JH |
1116 | |
1117 | fail_finish: /* Substring not found */ | |
33b8afdf JH |
1118 | if (prog->check_substr || prog->check_utf8) /* could be removed already */ |
1119 | BmUSEFUL(do_utf8 ? prog->check_utf8 : prog->check_substr) += 5; /* hooray */ | |
cad2e5aa | 1120 | fail: |
a3621e74 | 1121 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch rejected by optimizer%s\n", |
e4584336 | 1122 | PL_colors[4], PL_colors[5])); |
bd61b366 | 1123 | return NULL; |
cad2e5aa | 1124 | } |
9661b544 | 1125 | |
a0a388a1 YO |
1126 | #define DECL_TRIE_TYPE(scan) \ |
1127 | const enum { trie_plain, trie_utf8, trie_utf8_fold, trie_latin_utf8_fold } \ | |
1128 | trie_type = (scan->flags != EXACT) \ | |
1129 | ? (do_utf8 ? trie_utf8_fold : (UTF ? trie_latin_utf8_fold : trie_plain)) \ | |
1130 | : (do_utf8 ? trie_utf8 : trie_plain) | |
3b0527fe | 1131 | |
55eed653 NC |
1132 | #define REXEC_TRIE_READ_CHAR(trie_type, trie, widecharmap, uc, uscan, len, \ |
1133 | uvc, charid, foldlen, foldbuf, uniflags) STMT_START { \ | |
4cadc6a9 YO |
1134 | switch (trie_type) { \ |
1135 | case trie_utf8_fold: \ | |
1136 | if ( foldlen>0 ) { \ | |
0abd0d78 | 1137 | uvc = utf8n_to_uvuni( uscan, UTF8_MAXLEN, &len, uniflags ); \ |
4cadc6a9 YO |
1138 | foldlen -= len; \ |
1139 | uscan += len; \ | |
1140 | len=0; \ | |
1141 | } else { \ | |
0abd0d78 | 1142 | uvc = utf8n_to_uvuni( (U8*)uc, UTF8_MAXLEN, &len, uniflags ); \ |
4cadc6a9 YO |
1143 | uvc = to_uni_fold( uvc, foldbuf, &foldlen ); \ |
1144 | foldlen -= UNISKIP( uvc ); \ | |
1145 | uscan = foldbuf + UNISKIP( uvc ); \ | |
1146 | } \ | |
1147 | break; \ | |
a0a388a1 YO |
1148 | case trie_latin_utf8_fold: \ |
1149 | if ( foldlen>0 ) { \ | |
1150 | uvc = utf8n_to_uvuni( uscan, UTF8_MAXLEN, &len, uniflags ); \ | |
1151 | foldlen -= len; \ | |
1152 | uscan += len; \ | |
1153 | len=0; \ | |
1154 | } else { \ | |
1155 | len = 1; \ | |
1156 | uvc = to_uni_fold( *(U8*)uc, foldbuf, &foldlen ); \ | |
1157 | foldlen -= UNISKIP( uvc ); \ | |
1158 | uscan = foldbuf + UNISKIP( uvc ); \ | |
1159 | } \ | |
1160 | break; \ | |
4cadc6a9 YO |
1161 | case trie_utf8: \ |
1162 | uvc = utf8n_to_uvuni( (U8*)uc, UTF8_MAXLEN, &len, uniflags ); \ | |
1163 | break; \ | |
1164 | case trie_plain: \ | |
1165 | uvc = (UV)*uc; \ | |
1166 | len = 1; \ | |
1167 | } \ | |
4cadc6a9 YO |
1168 | if (uvc < 256) { \ |
1169 | charid = trie->charmap[ uvc ]; \ | |
1170 | } \ | |
1171 | else { \ | |
1172 | charid = 0; \ | |
55eed653 NC |
1173 | if (widecharmap) { \ |
1174 | SV** const svpp = hv_fetch(widecharmap, \ | |
4cadc6a9 YO |
1175 | (char*)&uvc, sizeof(UV), 0); \ |
1176 | if (svpp) \ | |
1177 | charid = (U16)SvIV(*svpp); \ | |
1178 | } \ | |
1179 | } \ | |
1180 | } STMT_END | |
1181 | ||
a0a388a1 YO |
1182 | #define REXEC_FBC_EXACTISH_CHECK(CoNd) \ |
1183 | { \ | |
1184 | char *my_strend= (char *)strend; \ | |
4cadc6a9 YO |
1185 | if ( (CoNd) \ |
1186 | && (ln == len || \ | |
4c1b470c | 1187 | foldEQ_utf8(s, &my_strend, 0, do_utf8, \ |
f2338a2e | 1188 | m, NULL, ln, cBOOL(UTF))) \ |
a0a388a1 | 1189 | && (!reginfo || regtry(reginfo, &s)) ) \ |
4cadc6a9 YO |
1190 | goto got_it; \ |
1191 | else { \ | |
1192 | U8 foldbuf[UTF8_MAXBYTES_CASE+1]; \ | |
1193 | uvchr_to_utf8(tmpbuf, c); \ | |
1194 | f = to_utf8_fold(tmpbuf, foldbuf, &foldlen); \ | |
1195 | if ( f != c \ | |
1196 | && (f == c1 || f == c2) \ | |
a0a388a1 | 1197 | && (ln == len || \ |
4c1b470c | 1198 | foldEQ_utf8(s, &my_strend, 0, do_utf8,\ |
f2338a2e | 1199 | m, NULL, ln, cBOOL(UTF)))\ |
a0a388a1 | 1200 | && (!reginfo || regtry(reginfo, &s)) ) \ |
4cadc6a9 YO |
1201 | goto got_it; \ |
1202 | } \ | |
a0a388a1 YO |
1203 | } \ |
1204 | s += len | |
4cadc6a9 YO |
1205 | |
1206 | #define REXEC_FBC_EXACTISH_SCAN(CoNd) \ | |
1207 | STMT_START { \ | |
1208 | while (s <= e) { \ | |
1209 | if ( (CoNd) \ | |
4c1b470c KW |
1210 | && (ln == 1 || (OP(c) == EXACTF \ |
1211 | ? foldEQ(s, m, ln) \ | |
1212 | : foldEQ_locale(s, m, ln))) \ | |
24b23f37 | 1213 | && (!reginfo || regtry(reginfo, &s)) ) \ |
4cadc6a9 YO |
1214 | goto got_it; \ |
1215 | s++; \ | |
1216 | } \ | |
1217 | } STMT_END | |
1218 | ||
1219 | #define REXEC_FBC_UTF8_SCAN(CoDe) \ | |
1220 | STMT_START { \ | |
1221 | while (s + (uskip = UTF8SKIP(s)) <= strend) { \ | |
1222 | CoDe \ | |
1223 | s += uskip; \ | |
1224 | } \ | |
1225 | } STMT_END | |
1226 | ||
1227 | #define REXEC_FBC_SCAN(CoDe) \ | |
1228 | STMT_START { \ | |
1229 | while (s < strend) { \ | |
1230 | CoDe \ | |
1231 | s++; \ | |
1232 | } \ | |
1233 | } STMT_END | |
1234 | ||
1235 | #define REXEC_FBC_UTF8_CLASS_SCAN(CoNd) \ | |
1236 | REXEC_FBC_UTF8_SCAN( \ | |
1237 | if (CoNd) { \ | |
24b23f37 | 1238 | if (tmp && (!reginfo || regtry(reginfo, &s))) \ |
4cadc6a9 YO |
1239 | goto got_it; \ |
1240 | else \ | |
1241 | tmp = doevery; \ | |
1242 | } \ | |
1243 | else \ | |
1244 | tmp = 1; \ | |
1245 | ) | |
1246 | ||
1247 | #define REXEC_FBC_CLASS_SCAN(CoNd) \ | |
1248 | REXEC_FBC_SCAN( \ | |
1249 | if (CoNd) { \ | |
24b23f37 | 1250 | if (tmp && (!reginfo || regtry(reginfo, &s))) \ |
4cadc6a9 YO |
1251 | goto got_it; \ |
1252 | else \ | |
1253 | tmp = doevery; \ | |
1254 | } \ | |
1255 | else \ | |
1256 | tmp = 1; \ | |
1257 | ) | |
1258 | ||
1259 | #define REXEC_FBC_TRYIT \ | |
24b23f37 | 1260 | if ((!reginfo || regtry(reginfo, &s))) \ |
4cadc6a9 YO |
1261 | goto got_it |
1262 | ||
e1d1eefb YO |
1263 | #define REXEC_FBC_CSCAN(CoNdUtF8,CoNd) \ |
1264 | if (do_utf8) { \ | |
1265 | REXEC_FBC_UTF8_CLASS_SCAN(CoNdUtF8); \ | |
1266 | } \ | |
1267 | else { \ | |
1268 | REXEC_FBC_CLASS_SCAN(CoNd); \ | |
1269 | } \ | |
1270 | break | |
1271 | ||
4cadc6a9 YO |
1272 | #define REXEC_FBC_CSCAN_PRELOAD(UtFpReLoAd,CoNdUtF8,CoNd) \ |
1273 | if (do_utf8) { \ | |
1274 | UtFpReLoAd; \ | |
1275 | REXEC_FBC_UTF8_CLASS_SCAN(CoNdUtF8); \ | |
1276 | } \ | |
1277 | else { \ | |
1278 | REXEC_FBC_CLASS_SCAN(CoNd); \ | |
1279 | } \ | |
1280 | break | |
1281 | ||
1282 | #define REXEC_FBC_CSCAN_TAINT(CoNdUtF8,CoNd) \ | |
1283 | PL_reg_flags |= RF_tainted; \ | |
1284 | if (do_utf8) { \ | |
1285 | REXEC_FBC_UTF8_CLASS_SCAN(CoNdUtF8); \ | |
1286 | } \ | |
1287 | else { \ | |
1288 | REXEC_FBC_CLASS_SCAN(CoNd); \ | |
1289 | } \ | |
1290 | break | |
1291 | ||
786e8c11 YO |
1292 | #define DUMP_EXEC_POS(li,s,doutf8) \ |
1293 | dump_exec_pos(li,s,(PL_regeol),(PL_bostr),(PL_reg_starttry),doutf8) | |
1294 | ||
1295 | /* We know what class REx starts with. Try to find this position... */ | |
1296 | /* if reginfo is NULL, its a dryrun */ | |
1297 | /* annoyingly all the vars in this routine have different names from their counterparts | |
1298 | in regmatch. /grrr */ | |
1299 | ||
3c3eec57 | 1300 | STATIC char * |
07be1b83 | 1301 | S_find_byclass(pTHX_ regexp * prog, const regnode *c, char *s, |
24b23f37 | 1302 | const char *strend, regmatch_info *reginfo) |
a687059c | 1303 | { |
27da23d5 | 1304 | dVAR; |
bbe252da | 1305 | const I32 doevery = (prog->intflags & PREGf_SKIP) == 0; |
6eb5f6b9 | 1306 | char *m; |
d8093b23 | 1307 | STRLEN ln; |
5dab1207 | 1308 | STRLEN lnc; |
078c425b | 1309 | register STRLEN uskip; |
d8093b23 G |
1310 | unsigned int c1; |
1311 | unsigned int c2; | |
6eb5f6b9 JH |
1312 | char *e; |
1313 | register I32 tmp = 1; /* Scratch variable? */ | |
a3b680e6 | 1314 | register const bool do_utf8 = PL_reg_match_utf8; |
f8fc2ecf | 1315 | RXi_GET_DECL(prog,progi); |
7918f24d NC |
1316 | |
1317 | PERL_ARGS_ASSERT_FIND_BYCLASS; | |
f8fc2ecf | 1318 | |
6eb5f6b9 JH |
1319 | /* We know what class it must start with. */ |
1320 | switch (OP(c)) { | |
6eb5f6b9 | 1321 | case ANYOF: |
388cc4de | 1322 | if (do_utf8) { |
4cadc6a9 | 1323 | REXEC_FBC_UTF8_CLASS_SCAN((ANYOF_FLAGS(c) & ANYOF_UNICODE) || |
388cc4de | 1324 | !UTF8_IS_INVARIANT((U8)s[0]) ? |
32fc9b6a | 1325 | reginclass(prog, c, (U8*)s, 0, do_utf8) : |
4cadc6a9 | 1326 | REGINCLASS(prog, c, (U8*)s)); |
388cc4de HS |
1327 | } |
1328 | else { | |
1329 | while (s < strend) { | |
1330 | STRLEN skip = 1; | |
1331 | ||
32fc9b6a | 1332 | if (REGINCLASS(prog, c, (U8*)s) || |
388cc4de HS |
1333 | (ANYOF_FOLD_SHARP_S(c, s, strend) && |
1334 | /* The assignment of 2 is intentional: | |
1335 | * for the folded sharp s, the skip is 2. */ | |
1336 | (skip = SHARP_S_SKIP))) { | |
24b23f37 | 1337 | if (tmp && (!reginfo || regtry(reginfo, &s))) |
388cc4de HS |
1338 | goto got_it; |
1339 | else | |
1340 | tmp = doevery; | |
1341 | } | |
1342 | else | |
1343 | tmp = 1; | |
1344 | s += skip; | |
1345 | } | |
a0d0e21e | 1346 | } |
6eb5f6b9 | 1347 | break; |
f33976b4 | 1348 | case CANY: |
4cadc6a9 | 1349 | REXEC_FBC_SCAN( |
24b23f37 | 1350 | if (tmp && (!reginfo || regtry(reginfo, &s))) |
f33976b4 DB |
1351 | goto got_it; |
1352 | else | |
1353 | tmp = doevery; | |
4cadc6a9 | 1354 | ); |
f33976b4 | 1355 | break; |
6eb5f6b9 | 1356 | case EXACTF: |
5dab1207 NIS |
1357 | m = STRING(c); |
1358 | ln = STR_LEN(c); /* length to match in octets/bytes */ | |
1359 | lnc = (I32) ln; /* length to match in characters */ | |
1aa99e6b | 1360 | if (UTF) { |
a2a2844f | 1361 | STRLEN ulen1, ulen2; |
5dab1207 | 1362 | U8 *sm = (U8 *) m; |
89ebb4a3 JH |
1363 | U8 tmpbuf1[UTF8_MAXBYTES_CASE+1]; |
1364 | U8 tmpbuf2[UTF8_MAXBYTES_CASE+1]; | |
97dc7d3e RGS |
1365 | /* used by commented-out code below */ |
1366 | /*const U32 uniflags = UTF8_ALLOW_DEFAULT;*/ | |
a0a388a1 YO |
1367 | |
1368 | /* XXX: Since the node will be case folded at compile | |
1369 | time this logic is a little odd, although im not | |
1370 | sure that its actually wrong. --dmq */ | |
1371 | ||
1372 | c1 = to_utf8_lower((U8*)m, tmpbuf1, &ulen1); | |
1373 | c2 = to_utf8_upper((U8*)m, tmpbuf2, &ulen2); | |
1374 | ||
1375 | /* XXX: This is kinda strange. to_utf8_XYZ returns the | |
1376 | codepoint of the first character in the converted | |
1377 | form, yet originally we did the extra step. | |
1378 | No tests fail by commenting this code out however | |
1379 | so Ive left it out. -- dmq. | |
1380 | ||
89ebb4a3 | 1381 | c1 = utf8n_to_uvchr(tmpbuf1, UTF8_MAXBYTES_CASE, |
041457d9 | 1382 | 0, uniflags); |
89ebb4a3 | 1383 | c2 = utf8n_to_uvchr(tmpbuf2, UTF8_MAXBYTES_CASE, |
041457d9 | 1384 | 0, uniflags); |
a0a388a1 YO |
1385 | */ |
1386 | ||
5dab1207 NIS |
1387 | lnc = 0; |
1388 | while (sm < ((U8 *) m + ln)) { | |
1389 | lnc++; | |
1390 | sm += UTF8SKIP(sm); | |
1391 | } | |
1aa99e6b IH |
1392 | } |
1393 | else { | |
1394 | c1 = *(U8*)m; | |
1395 | c2 = PL_fold[c1]; | |
1396 | } | |
6eb5f6b9 JH |
1397 | goto do_exactf; |
1398 | case EXACTFL: | |
5dab1207 NIS |
1399 | m = STRING(c); |
1400 | ln = STR_LEN(c); | |
1401 | lnc = (I32) ln; | |
d8093b23 | 1402 | c1 = *(U8*)m; |
6eb5f6b9 JH |
1403 | c2 = PL_fold_locale[c1]; |
1404 | do_exactf: | |
db12adc6 | 1405 | e = HOP3c(strend, -((I32)lnc), s); |
b3c9acc1 | 1406 | |
3b0527fe | 1407 | if (!reginfo && e < s) |
6eb5f6b9 | 1408 | e = s; /* Due to minlen logic of intuit() */ |
1aa99e6b | 1409 | |
60a8b682 JH |
1410 | /* The idea in the EXACTF* cases is to first find the |
1411 | * first character of the EXACTF* node and then, if | |
1412 | * necessary, case-insensitively compare the full | |
1413 | * text of the node. The c1 and c2 are the first | |
1414 | * characters (though in Unicode it gets a bit | |
1415 | * more complicated because there are more cases | |
7f16dd3d JH |
1416 | * than just upper and lower: one needs to use |
1417 | * the so-called folding case for case-insensitive | |
1418 | * matching (called "loose matching" in Unicode). | |
4c1b470c | 1419 | * foldEQ_utf8() will do just that. */ |
60a8b682 | 1420 | |
a0a388a1 | 1421 | if (do_utf8 || UTF) { |
575cac57 | 1422 | UV c, f; |
89ebb4a3 | 1423 | U8 tmpbuf [UTF8_MAXBYTES+1]; |
a0a388a1 YO |
1424 | STRLEN len = 1; |
1425 | STRLEN foldlen; | |
4ad0818d | 1426 | const U32 uniflags = UTF8_ALLOW_DEFAULT; |
09091399 | 1427 | if (c1 == c2) { |
5dab1207 NIS |
1428 | /* Upper and lower of 1st char are equal - |
1429 | * probably not a "letter". */ | |
1aa99e6b | 1430 | while (s <= e) { |
a0a388a1 YO |
1431 | if (do_utf8) { |
1432 | c = utf8n_to_uvchr((U8*)s, UTF8_MAXBYTES, &len, | |
041457d9 | 1433 | uniflags); |
a0a388a1 YO |
1434 | } else { |
1435 | c = *((U8*)s); | |
1436 | } | |
4cadc6a9 | 1437 | REXEC_FBC_EXACTISH_CHECK(c == c1); |
1aa99e6b | 1438 | } |
09091399 JH |
1439 | } |
1440 | else { | |
1aa99e6b | 1441 | while (s <= e) { |
a0a388a1 YO |
1442 | if (do_utf8) { |
1443 | c = utf8n_to_uvchr((U8*)s, UTF8_MAXBYTES, &len, | |
041457d9 | 1444 | uniflags); |
a0a388a1 YO |
1445 | } else { |
1446 | c = *((U8*)s); | |
1447 | } | |
80aecb99 | 1448 | |
60a8b682 | 1449 | /* Handle some of the three Greek sigmas cases. |
8c01da3c JH |
1450 | * Note that not all the possible combinations |
1451 | * are handled here: some of them are handled | |
1452 | * by the standard folding rules, and some of | |
1453 | * them (the character class or ANYOF cases) | |
1454 | * are handled during compiletime in | |
1455 | * regexec.c:S_regclass(). */ | |
880bd946 JH |
1456 | if (c == (UV)UNICODE_GREEK_CAPITAL_LETTER_SIGMA || |
1457 | c == (UV)UNICODE_GREEK_SMALL_LETTER_FINAL_SIGMA) | |
1458 | c = (UV)UNICODE_GREEK_SMALL_LETTER_SIGMA; | |
80aecb99 | 1459 | |
4cadc6a9 | 1460 | REXEC_FBC_EXACTISH_CHECK(c == c1 || c == c2); |
1aa99e6b | 1461 | } |
09091399 | 1462 | } |
1aa99e6b IH |
1463 | } |
1464 | else { | |
a0a388a1 | 1465 | /* Neither pattern nor string are UTF8 */ |
1aa99e6b | 1466 | if (c1 == c2) |
4cadc6a9 | 1467 | REXEC_FBC_EXACTISH_SCAN(*(U8*)s == c1); |
1aa99e6b | 1468 | else |
4cadc6a9 | 1469 | REXEC_FBC_EXACTISH_SCAN(*(U8*)s == c1 || *(U8*)s == c2); |
b3c9acc1 IZ |
1470 | } |
1471 | break; | |
bbce6d69 | 1472 | case BOUNDL: |
3280af22 | 1473 | PL_reg_flags |= RF_tainted; |
bbce6d69 | 1474 | /* FALL THROUGH */ |
a0d0e21e | 1475 | case BOUND: |
ffc61ed2 | 1476 | if (do_utf8) { |
12d33761 | 1477 | if (s == PL_bostr) |
ffc61ed2 JH |
1478 | tmp = '\n'; |
1479 | else { | |
6136c704 | 1480 | U8 * const r = reghop3((U8*)s, -1, (U8*)PL_bostr); |
4ad0818d | 1481 | tmp = utf8n_to_uvchr(r, UTF8SKIP(r), 0, UTF8_ALLOW_DEFAULT); |
ffc61ed2 JH |
1482 | } |
1483 | tmp = ((OP(c) == BOUND ? | |
9041c2e3 | 1484 | isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0); |
1a4fad37 | 1485 | LOAD_UTF8_CHARCLASS_ALNUM(); |
4cadc6a9 | 1486 | REXEC_FBC_UTF8_SCAN( |
ffc61ed2 | 1487 | if (tmp == !(OP(c) == BOUND ? |
f2338a2e | 1488 | cBOOL(swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) : |
ffc61ed2 JH |
1489 | isALNUM_LC_utf8((U8*)s))) |
1490 | { | |
1491 | tmp = !tmp; | |
4cadc6a9 | 1492 | REXEC_FBC_TRYIT; |
a687059c | 1493 | } |
4cadc6a9 | 1494 | ); |
a0d0e21e | 1495 | } |
667bb95a | 1496 | else { |
12d33761 | 1497 | tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n'; |
ffc61ed2 | 1498 | tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0); |
4cadc6a9 | 1499 | REXEC_FBC_SCAN( |
ffc61ed2 JH |
1500 | if (tmp == |
1501 | !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) { | |
1502 | tmp = !tmp; | |
4cadc6a9 | 1503 | REXEC_FBC_TRYIT; |
a0ed51b3 | 1504 | } |
4cadc6a9 | 1505 | ); |
a0ed51b3 | 1506 | } |
24b23f37 | 1507 | if ((!prog->minlen && tmp) && (!reginfo || regtry(reginfo, &s))) |
a0ed51b3 LW |
1508 | goto got_it; |
1509 | break; | |
bbce6d69 | 1510 | case NBOUNDL: |
3280af22 | 1511 | PL_reg_flags |= RF_tainted; |
bbce6d69 | 1512 | /* FALL THROUGH */ |
a0d0e21e | 1513 | case NBOUND: |
ffc61ed2 | 1514 | if (do_utf8) { |
12d33761 | 1515 | if (s == PL_bostr) |
ffc61ed2 JH |
1516 | tmp = '\n'; |
1517 | else { | |
6136c704 | 1518 | U8 * const r = reghop3((U8*)s, -1, (U8*)PL_bostr); |
4ad0818d | 1519 | tmp = utf8n_to_uvchr(r, UTF8SKIP(r), 0, UTF8_ALLOW_DEFAULT); |
ffc61ed2 JH |
1520 | } |
1521 | tmp = ((OP(c) == NBOUND ? | |
9041c2e3 | 1522 | isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0); |
1a4fad37 | 1523 | LOAD_UTF8_CHARCLASS_ALNUM(); |
4cadc6a9 | 1524 | REXEC_FBC_UTF8_SCAN( |
ffc61ed2 | 1525 | if (tmp == !(OP(c) == NBOUND ? |
f2338a2e | 1526 | cBOOL(swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) : |
ffc61ed2 JH |
1527 | isALNUM_LC_utf8((U8*)s))) |
1528 | tmp = !tmp; | |
4cadc6a9 YO |
1529 | else REXEC_FBC_TRYIT; |
1530 | ); | |
a0d0e21e | 1531 | } |
667bb95a | 1532 | else { |
12d33761 | 1533 | tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n'; |
ffc61ed2 JH |
1534 | tmp = ((OP(c) == NBOUND ? |
1535 | isALNUM(tmp) : isALNUM_LC(tmp)) != 0); | |
4cadc6a9 | 1536 | REXEC_FBC_SCAN( |
ffc61ed2 JH |
1537 | if (tmp == |
1538 | !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s))) | |
1539 | tmp = !tmp; | |
4cadc6a9 YO |
1540 | else REXEC_FBC_TRYIT; |
1541 | ); | |
a0ed51b3 | 1542 | } |
24b23f37 | 1543 | if ((!prog->minlen && !tmp) && (!reginfo || regtry(reginfo, &s))) |
a0ed51b3 LW |
1544 | goto got_it; |
1545 | break; | |
a0d0e21e | 1546 | case ALNUM: |
4cadc6a9 | 1547 | REXEC_FBC_CSCAN_PRELOAD( |
d1eb3177 YO |
1548 | LOAD_UTF8_CHARCLASS_PERL_WORD(), |
1549 | swash_fetch(RE_utf8_perl_word, (U8*)s, do_utf8), | |
4cadc6a9 YO |
1550 | isALNUM(*s) |
1551 | ); | |
bbce6d69 | 1552 | case ALNUML: |
4cadc6a9 YO |
1553 | REXEC_FBC_CSCAN_TAINT( |
1554 | isALNUM_LC_utf8((U8*)s), | |
1555 | isALNUM_LC(*s) | |
1556 | ); | |
a0d0e21e | 1557 | case NALNUM: |
4cadc6a9 | 1558 | REXEC_FBC_CSCAN_PRELOAD( |
d1eb3177 YO |
1559 | LOAD_UTF8_CHARCLASS_PERL_WORD(), |
1560 | !swash_fetch(RE_utf8_perl_word, (U8*)s, do_utf8), | |
4cadc6a9 YO |
1561 | !isALNUM(*s) |
1562 | ); | |
bbce6d69 | 1563 | case NALNUML: |
4cadc6a9 YO |
1564 | REXEC_FBC_CSCAN_TAINT( |
1565 | !isALNUM_LC_utf8((U8*)s), | |
1566 | !isALNUM_LC(*s) | |
1567 | ); | |
a0d0e21e | 1568 | case SPACE: |
4cadc6a9 | 1569 | REXEC_FBC_CSCAN_PRELOAD( |
d1eb3177 YO |
1570 | LOAD_UTF8_CHARCLASS_PERL_SPACE(), |
1571 | *s == ' ' || swash_fetch(RE_utf8_perl_space,(U8*)s, do_utf8), | |
4cadc6a9 YO |
1572 | isSPACE(*s) |
1573 | ); | |
bbce6d69 | 1574 | case SPACEL: |
4cadc6a9 YO |
1575 | REXEC_FBC_CSCAN_TAINT( |
1576 | *s == ' ' || isSPACE_LC_utf8((U8*)s), | |
1577 | isSPACE_LC(*s) | |
1578 | ); | |
a0d0e21e | 1579 | case NSPACE: |
4cadc6a9 | 1580 | REXEC_FBC_CSCAN_PRELOAD( |
d1eb3177 YO |
1581 | LOAD_UTF8_CHARCLASS_PERL_SPACE(), |
1582 | !(*s == ' ' || swash_fetch(RE_utf8_perl_space,(U8*)s, do_utf8)), | |
4cadc6a9 YO |
1583 | !isSPACE(*s) |
1584 | ); | |
bbce6d69 | 1585 | case NSPACEL: |
4cadc6a9 YO |
1586 | REXEC_FBC_CSCAN_TAINT( |
1587 | !(*s == ' ' || isSPACE_LC_utf8((U8*)s)), | |
1588 | !isSPACE_LC(*s) | |
1589 | ); | |
a0d0e21e | 1590 | case DIGIT: |
4cadc6a9 | 1591 | REXEC_FBC_CSCAN_PRELOAD( |
d1eb3177 YO |
1592 | LOAD_UTF8_CHARCLASS_POSIX_DIGIT(), |
1593 | swash_fetch(RE_utf8_posix_digit,(U8*)s, do_utf8), | |
4cadc6a9 YO |
1594 | isDIGIT(*s) |
1595 | ); | |
b8c5462f | 1596 | case DIGITL: |
4cadc6a9 YO |
1597 | REXEC_FBC_CSCAN_TAINT( |
1598 | isDIGIT_LC_utf8((U8*)s), | |
1599 | isDIGIT_LC(*s) | |
1600 | ); | |
a0d0e21e | 1601 | case NDIGIT: |
4cadc6a9 | 1602 | REXEC_FBC_CSCAN_PRELOAD( |
d1eb3177 YO |
1603 | LOAD_UTF8_CHARCLASS_POSIX_DIGIT(), |
1604 | !swash_fetch(RE_utf8_posix_digit,(U8*)s, do_utf8), | |
4cadc6a9 YO |
1605 | !isDIGIT(*s) |
1606 | ); | |
b8c5462f | 1607 | case NDIGITL: |
4cadc6a9 YO |
1608 | REXEC_FBC_CSCAN_TAINT( |
1609 | !isDIGIT_LC_utf8((U8*)s), | |
1610 | !isDIGIT_LC(*s) | |
1611 | ); | |
e1d1eefb YO |
1612 | case LNBREAK: |
1613 | REXEC_FBC_CSCAN( | |
1614 | is_LNBREAK_utf8(s), | |
1615 | is_LNBREAK_latin1(s) | |
1616 | ); | |
1617 | case VERTWS: | |
1618 | REXEC_FBC_CSCAN( | |
1619 | is_VERTWS_utf8(s), | |
1620 | is_VERTWS_latin1(s) | |
1621 | ); | |
1622 | case NVERTWS: | |
1623 | REXEC_FBC_CSCAN( | |
1624 | !is_VERTWS_utf8(s), | |
1625 | !is_VERTWS_latin1(s) | |
1626 | ); | |
1627 | case HORIZWS: | |
1628 | REXEC_FBC_CSCAN( | |
1629 | is_HORIZWS_utf8(s), | |
1630 | is_HORIZWS_latin1(s) | |
1631 | ); | |
1632 | case NHORIZWS: | |
1633 | REXEC_FBC_CSCAN( | |
1634 | !is_HORIZWS_utf8(s), | |
1635 | !is_HORIZWS_latin1(s) | |
1636 | ); | |
1de06328 YO |
1637 | case AHOCORASICKC: |
1638 | case AHOCORASICK: | |
07be1b83 | 1639 | { |
a0a388a1 | 1640 | DECL_TRIE_TYPE(c); |
07be1b83 YO |
1641 | /* what trie are we using right now */ |
1642 | reg_ac_data *aho | |
f8fc2ecf | 1643 | = (reg_ac_data*)progi->data->data[ ARG( c ) ]; |
3251b653 NC |
1644 | reg_trie_data *trie |
1645 | = (reg_trie_data*)progi->data->data[ aho->trie ]; | |
85fbaab2 | 1646 | HV *widecharmap = MUTABLE_HV(progi->data->data[ aho->trie + 1 ]); |
07be1b83 YO |
1647 | |
1648 | const char *last_start = strend - trie->minlen; | |
6148ee25 | 1649 | #ifdef DEBUGGING |
07be1b83 | 1650 | const char *real_start = s; |
6148ee25 | 1651 | #endif |
07be1b83 | 1652 | STRLEN maxlen = trie->maxlen; |
be8e71aa YO |
1653 | SV *sv_points; |
1654 | U8 **points; /* map of where we were in the input string | |
786e8c11 | 1655 | when reading a given char. For ASCII this |
be8e71aa | 1656 | is unnecessary overhead as the relationship |
38a44b82 NC |
1657 | is always 1:1, but for Unicode, especially |
1658 | case folded Unicode this is not true. */ | |
f9e705e8 | 1659 | U8 foldbuf[ UTF8_MAXBYTES_CASE + 1 ]; |
786e8c11 YO |
1660 | U8 *bitmap=NULL; |
1661 | ||
07be1b83 YO |
1662 | |
1663 | GET_RE_DEBUG_FLAGS_DECL; | |
1664 | ||
be8e71aa YO |
1665 | /* We can't just allocate points here. We need to wrap it in |
1666 | * an SV so it gets freed properly if there is a croak while | |
1667 | * running the match */ | |
1668 | ENTER; | |
1669 | SAVETMPS; | |
1670 | sv_points=newSV(maxlen * sizeof(U8 *)); | |
1671 | SvCUR_set(sv_points, | |
1672 | maxlen * sizeof(U8 *)); | |
1673 | SvPOK_on(sv_points); | |
1674 | sv_2mortal(sv_points); | |
1675 | points=(U8**)SvPV_nolen(sv_points ); | |
1de06328 YO |
1676 | if ( trie_type != trie_utf8_fold |
1677 | && (trie->bitmap || OP(c)==AHOCORASICKC) ) | |
1678 | { | |
786e8c11 YO |
1679 | if (trie->bitmap) |
1680 | bitmap=(U8*)trie->bitmap; | |
1681 | else | |
1682 | bitmap=(U8*)ANYOF_BITMAP(c); | |
07be1b83 | 1683 | } |
786e8c11 YO |
1684 | /* this is the Aho-Corasick algorithm modified a touch |
1685 | to include special handling for long "unknown char" | |
1686 | sequences. The basic idea being that we use AC as long | |
1687 | as we are dealing with a possible matching char, when | |
1688 | we encounter an unknown char (and we have not encountered | |
1689 | an accepting state) we scan forward until we find a legal | |
1690 | starting char. | |
1691 | AC matching is basically that of trie matching, except | |
1692 | that when we encounter a failing transition, we fall back | |
1693 | to the current states "fail state", and try the current char | |
1694 | again, a process we repeat until we reach the root state, | |
1695 | state 1, or a legal transition. If we fail on the root state | |
1696 | then we can either terminate if we have reached an accepting | |
1697 | state previously, or restart the entire process from the beginning | |
1698 | if we have not. | |
1699 | ||
1700 | */ | |
07be1b83 YO |
1701 | while (s <= last_start) { |
1702 | const U32 uniflags = UTF8_ALLOW_DEFAULT; | |
1703 | U8 *uc = (U8*)s; | |
1704 | U16 charid = 0; | |
1705 | U32 base = 1; | |
1706 | U32 state = 1; | |
1707 | UV uvc = 0; | |
1708 | STRLEN len = 0; | |
1709 | STRLEN foldlen = 0; | |
1710 | U8 *uscan = (U8*)NULL; | |
1711 | U8 *leftmost = NULL; | |
786e8c11 YO |
1712 | #ifdef DEBUGGING |
1713 | U32 accepted_word= 0; | |
1714 | #endif | |
07be1b83 YO |
1715 | U32 pointpos = 0; |
1716 | ||
1717 | while ( state && uc <= (U8*)strend ) { | |
1718 | int failed=0; | |
786e8c11 YO |
1719 | U32 word = aho->states[ state ].wordnum; |
1720 | ||
1de06328 YO |
1721 | if( state==1 ) { |
1722 | if ( bitmap ) { | |
1723 | DEBUG_TRIE_EXECUTE_r( | |
1724 | if ( uc <= (U8*)last_start && !BITMAP_TEST(bitmap,*uc) ) { | |
1725 | dump_exec_pos( (char *)uc, c, strend, real_start, | |
1726 | (char *)uc, do_utf8 ); | |
1727 | PerlIO_printf( Perl_debug_log, | |
1728 | " Scanning for legal start char...\n"); | |
1729 | } | |
1730 | ); | |
1731 | while ( uc <= (U8*)last_start && !BITMAP_TEST(bitmap,*uc) ) { | |
1732 | uc++; | |
786e8c11 | 1733 | } |
1de06328 | 1734 | s= (char *)uc; |
786e8c11 | 1735 | } |
786e8c11 YO |
1736 | if (uc >(U8*)last_start) break; |
1737 | } | |
1738 | ||
1739 | if ( word ) { | |
2e64971a | 1740 | U8 *lpos= points[ (pointpos - trie->wordinfo[word].len) % maxlen ]; |
786e8c11 YO |
1741 | if (!leftmost || lpos < leftmost) { |
1742 | DEBUG_r(accepted_word=word); | |
07be1b83 | 1743 | leftmost= lpos; |
786e8c11 | 1744 | } |
07be1b83 | 1745 | if (base==0) break; |
786e8c11 | 1746 | |
07be1b83 YO |
1747 | } |
1748 | points[pointpos++ % maxlen]= uc; | |
55eed653 NC |
1749 | REXEC_TRIE_READ_CHAR(trie_type, trie, widecharmap, uc, |
1750 | uscan, len, uvc, charid, foldlen, | |
1751 | foldbuf, uniflags); | |
786e8c11 YO |
1752 | DEBUG_TRIE_EXECUTE_r({ |
1753 | dump_exec_pos( (char *)uc, c, strend, real_start, | |
1754 | s, do_utf8 ); | |
07be1b83 | 1755 | PerlIO_printf(Perl_debug_log, |
786e8c11 YO |
1756 | " Charid:%3u CP:%4"UVxf" ", |
1757 | charid, uvc); | |
1758 | }); | |
07be1b83 YO |
1759 | |
1760 | do { | |
6148ee25 | 1761 | #ifdef DEBUGGING |
786e8c11 | 1762 | word = aho->states[ state ].wordnum; |
6148ee25 | 1763 | #endif |
07be1b83 YO |
1764 | base = aho->states[ state ].trans.base; |
1765 | ||
786e8c11 YO |
1766 | DEBUG_TRIE_EXECUTE_r({ |
1767 | if (failed) | |
1768 | dump_exec_pos( (char *)uc, c, strend, real_start, | |
1769 | s, do_utf8 ); | |
07be1b83 | 1770 | PerlIO_printf( Perl_debug_log, |
786e8c11 YO |
1771 | "%sState: %4"UVxf", word=%"UVxf, |
1772 | failed ? " Fail transition to " : "", | |
1773 | (UV)state, (UV)word); | |
1774 | }); | |
07be1b83 YO |
1775 | if ( base ) { |
1776 | U32 tmp; | |
6dd2be57 | 1777 | I32 offset; |
07be1b83 | 1778 | if (charid && |
6dd2be57 DM |
1779 | ( ((offset = base + charid |
1780 | - 1 - trie->uniquecharcount)) >= 0) | |
1781 | && ((U32)offset < trie->lasttrans) | |
1782 | && trie->trans[offset].check == state | |
1783 | && (tmp=trie->trans[offset].next)) | |
07be1b83 | 1784 | { |
786e8c11 YO |
1785 | DEBUG_TRIE_EXECUTE_r( |
1786 | PerlIO_printf( Perl_debug_log," - legal\n")); | |
07be1b83 YO |
1787 | state = tmp; |
1788 | break; | |
1789 | } | |
1790 | else { | |
786e8c11 YO |
1791 | DEBUG_TRIE_EXECUTE_r( |
1792 | PerlIO_printf( Perl_debug_log," - fail\n")); | |
1793 | failed = 1; | |
1794 | state = aho->fail[state]; | |
07be1b83 YO |
1795 | } |
1796 | } | |
1797 | else { | |
1798 | /* we must be accepting here */ | |
786e8c11 YO |
1799 | DEBUG_TRIE_EXECUTE_r( |
1800 | PerlIO_printf( Perl_debug_log," - accepting\n")); | |
1801 | failed = 1; | |
07be1b83 YO |
1802 | break; |
1803 | } | |
1804 | } while(state); | |
786e8c11 | 1805 | uc += len; |
07be1b83 YO |
1806 | if (failed) { |
1807 | if (leftmost) | |
1808 | break; | |
786e8c11 | 1809 | if (!state) state = 1; |
07be1b83 YO |
1810 | } |
1811 | } | |
1812 | if ( aho->states[ state ].wordnum ) { | |
2e64971a | 1813 | U8 *lpos = points[ (pointpos - trie->wordinfo[aho->states[ state ].wordnum].len) % maxlen ]; |
786e8c11 YO |
1814 | if (!leftmost || lpos < leftmost) { |
1815 | DEBUG_r(accepted_word=aho->states[ state ].wordnum); | |
07be1b83 | 1816 | leftmost = lpos; |
786e8c11 | 1817 | } |
07be1b83 | 1818 | } |
07be1b83 YO |
1819 | if (leftmost) { |
1820 | s = (char*)leftmost; | |
786e8c11 YO |
1821 | DEBUG_TRIE_EXECUTE_r({ |
1822 | PerlIO_printf( | |
70685ca0 JH |
1823 | Perl_debug_log,"Matches word #%"UVxf" at position %"IVdf". Trying full pattern...\n", |
1824 | (UV)accepted_word, (IV)(s - real_start) | |
786e8c11 YO |
1825 | ); |
1826 | }); | |
24b23f37 | 1827 | if (!reginfo || regtry(reginfo, &s)) { |
be8e71aa YO |
1828 | FREETMPS; |
1829 | LEAVE; | |
07be1b83 | 1830 | goto got_it; |
be8e71aa | 1831 | } |
07be1b83 | 1832 | s = HOPc(s,1); |
786e8c11 YO |
1833 | DEBUG_TRIE_EXECUTE_r({ |
1834 | PerlIO_printf( Perl_debug_log,"Pattern failed. Looking for new start point...\n"); | |
1835 | }); | |
07be1b83 | 1836 | } else { |
786e8c11 YO |
1837 | DEBUG_TRIE_EXECUTE_r( |
1838 | PerlIO_printf( Perl_debug_log,"No match.\n")); | |
07be1b83 YO |
1839 | break; |
1840 | } | |
1841 | } | |
be8e71aa YO |
1842 | FREETMPS; |
1843 | LEAVE; | |
07be1b83 YO |
1844 | } |
1845 | break; | |
b3c9acc1 | 1846 | default: |
3c3eec57 GS |
1847 | Perl_croak(aTHX_ "panic: unknown regstclass %d", (int)OP(c)); |
1848 | break; | |
d6a28714 | 1849 | } |
6eb5f6b9 JH |
1850 | return 0; |
1851 | got_it: | |
1852 | return s; | |
1853 | } | |
1854 | ||
fae667d5 | 1855 | |
6eb5f6b9 JH |
1856 | /* |
1857 | - regexec_flags - match a regexp against a string | |
1858 | */ | |
1859 | I32 | |
288b8c02 | 1860 | Perl_regexec_flags(pTHX_ REGEXP * const rx, char *stringarg, register char *strend, |
6eb5f6b9 JH |
1861 | char *strbeg, I32 minend, SV *sv, void *data, U32 flags) |
1862 | /* strend: pointer to null at end of string */ | |
1863 | /* strbeg: real beginning of string */ | |
1864 | /* minend: end of match must be >=minend after stringarg. */ | |
58e23c8d YO |
1865 | /* data: May be used for some additional optimizations. |
1866 | Currently its only used, with a U32 cast, for transmitting | |
1867 | the ganch offset when doing a /g match. This will change */ | |
6eb5f6b9 JH |
1868 | /* nosave: For optimizations. */ |
1869 | { | |
97aff369 | 1870 | dVAR; |
288b8c02 | 1871 | struct regexp *const prog = (struct regexp *)SvANY(rx); |
24b23f37 | 1872 | /*register*/ char *s; |
6eb5f6b9 | 1873 | register regnode *c; |
24b23f37 | 1874 | /*register*/ char *startpos = stringarg; |
6eb5f6b9 JH |
1875 | I32 minlen; /* must match at least this many chars */ |
1876 | I32 dontbother = 0; /* how many characters not to try at end */ | |
6eb5f6b9 JH |
1877 | I32 end_shift = 0; /* Same for the end. */ /* CC */ |
1878 | I32 scream_pos = -1; /* Internal iterator of scream. */ | |
ccac19ea | 1879 | char *scream_olds = NULL; |
f2338a2e | 1880 | const bool do_utf8 = cBOOL(DO_UTF8(sv)); |
2757e526 | 1881 | I32 multiline; |
f8fc2ecf | 1882 | RXi_GET_DECL(prog,progi); |
3b0527fe | 1883 | regmatch_info reginfo; /* create some info to pass to regtry etc */ |
e9105d30 | 1884 | regexp_paren_pair *swap = NULL; |
a3621e74 YO |
1885 | GET_RE_DEBUG_FLAGS_DECL; |
1886 | ||
7918f24d | 1887 | PERL_ARGS_ASSERT_REGEXEC_FLAGS; |
9d4ba2ae | 1888 | PERL_UNUSED_ARG(data); |
6eb5f6b9 JH |
1889 | |
1890 | /* Be paranoid... */ | |
1891 | if (prog == NULL || startpos == NULL) { | |
1892 | Perl_croak(aTHX_ "NULL regexp parameter"); | |
1893 | return 0; | |
1894 | } | |
1895 | ||
bbe252da | 1896 | multiline = prog->extflags & RXf_PMf_MULTILINE; |
288b8c02 | 1897 | reginfo.prog = rx; /* Yes, sorry that this is confusing. */ |
2757e526 | 1898 | |
288b8c02 | 1899 | RX_MATCH_UTF8_set(rx, do_utf8); |
1de06328 | 1900 | DEBUG_EXECUTE_r( |
efd26800 | 1901 | debug_start_match(rx, do_utf8, startpos, strend, |
1de06328 YO |
1902 | "Matching"); |
1903 | ); | |
bac06658 | 1904 | |
6eb5f6b9 | 1905 | minlen = prog->minlen; |
1de06328 YO |
1906 | |
1907 | if (strend - startpos < (minlen+(prog->check_offset_min<0?prog->check_offset_min:0))) { | |
a3621e74 | 1908 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, |
a72c7584 JH |
1909 | "String too short [regexec_flags]...\n")); |
1910 | goto phooey; | |
1aa99e6b | 1911 | } |
6eb5f6b9 | 1912 | |
1de06328 | 1913 | |
6eb5f6b9 | 1914 | /* Check validity of program. */ |
f8fc2ecf | 1915 | if (UCHARAT(progi->program) != REG_MAGIC) { |
6eb5f6b9 JH |
1916 | Perl_croak(aTHX_ "corrupted regexp program"); |
1917 | } | |
1918 | ||
1919 | PL_reg_flags = 0; | |
1920 | PL_reg_eval_set = 0; | |
1921 | PL_reg_maxiter = 0; | |
1922 | ||
3c8556c3 | 1923 | if (RX_UTF8(rx)) |
6eb5f6b9 JH |
1924 | PL_reg_flags |= RF_utf8; |
1925 | ||
1926 | /* Mark beginning of line for ^ and lookbehind. */ | |
3b0527fe | 1927 | reginfo.bol = startpos; /* XXX not used ??? */ |
6eb5f6b9 | 1928 | PL_bostr = strbeg; |
3b0527fe | 1929 | reginfo.sv = sv; |
6eb5f6b9 JH |
1930 | |
1931 | /* Mark end of line for $ (and such) */ | |
1932 | PL_regeol = strend; | |
1933 | ||
1934 | /* see how far we have to get to not match where we matched before */ | |
3b0527fe | 1935 | reginfo.till = startpos+minend; |
6eb5f6b9 | 1936 | |
6eb5f6b9 JH |
1937 | /* If there is a "must appear" string, look for it. */ |
1938 | s = startpos; | |
1939 | ||
bbe252da | 1940 | if (prog->extflags & RXf_GPOS_SEEN) { /* Need to set reginfo->ganch */ |
6eb5f6b9 | 1941 | MAGIC *mg; |
2c296965 | 1942 | if (flags & REXEC_IGNOREPOS){ /* Means: check only at start */ |
58e23c8d | 1943 | reginfo.ganch = startpos + prog->gofs; |
2c296965 | 1944 | DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log, |
ed549f2e | 1945 | "GPOS IGNOREPOS: reginfo.ganch = startpos + %"UVxf"\n",(UV)prog->gofs)); |
2c296965 | 1946 | } else if (sv && SvTYPE(sv) >= SVt_PVMG |
6eb5f6b9 | 1947 | && SvMAGIC(sv) |
14befaf4 DM |
1948 | && (mg = mg_find(sv, PERL_MAGIC_regex_global)) |
1949 | && mg->mg_len >= 0) { | |
3b0527fe | 1950 | reginfo.ganch = strbeg + mg->mg_len; /* Defined pos() */ |
2c296965 | 1951 | DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log, |
ed549f2e | 1952 | "GPOS MAGIC: reginfo.ganch = strbeg + %"IVdf"\n",(IV)mg->mg_len)); |
2c296965 | 1953 | |
bbe252da | 1954 | if (prog->extflags & RXf_ANCH_GPOS) { |
3b0527fe | 1955 | if (s > reginfo.ganch) |
6eb5f6b9 | 1956 | goto phooey; |
58e23c8d | 1957 | s = reginfo.ganch - prog->gofs; |
2c296965 | 1958 | DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log, |
ed549f2e | 1959 | "GPOS ANCH_GPOS: s = ganch - %"UVxf"\n",(UV)prog->gofs)); |
c584a96e YO |
1960 | if (s < strbeg) |
1961 | goto phooey; | |
6eb5f6b9 JH |
1962 | } |
1963 | } | |
58e23c8d | 1964 | else if (data) { |
70685ca0 | 1965 | reginfo.ganch = strbeg + PTR2UV(data); |
2c296965 YO |
1966 | DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log, |
1967 | "GPOS DATA: reginfo.ganch= strbeg + %"UVxf"\n",PTR2UV(data))); | |
1968 | ||
1969 | } else { /* pos() not defined */ | |
3b0527fe | 1970 | reginfo.ganch = strbeg; |
2c296965 YO |
1971 | DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log, |
1972 | "GPOS: reginfo.ganch = strbeg\n")); | |
1973 | } | |
6eb5f6b9 | 1974 | } |
288b8c02 | 1975 | if (PL_curpm && (PM_GETRE(PL_curpm) == rx)) { |
e9105d30 GG |
1976 | /* We have to be careful. If the previous successful match |
1977 | was from this regex we don't want a subsequent partially | |
1978 | successful match to clobber the old results. | |
1979 | So when we detect this possibility we add a swap buffer | |
1980 | to the re, and switch the buffer each match. If we fail | |
1981 | we switch it back, otherwise we leave it swapped. | |
1982 | */ | |
1983 | swap = prog->offs; | |
1984 | /* do we need a save destructor here for eval dies? */ | |
1985 | Newxz(prog->offs, (prog->nparens + 1), regexp_paren_pair); | |
c74340f9 | 1986 | } |
a0714e2c | 1987 | if (!(flags & REXEC_CHECKED) && (prog->check_substr != NULL || prog->check_utf8 != NULL)) { |
6eb5f6b9 JH |
1988 | re_scream_pos_data d; |
1989 | ||
1990 | d.scream_olds = &scream_olds; | |
1991 | d.scream_pos = &scream_pos; | |
288b8c02 | 1992 | s = re_intuit_start(rx, sv, s, strend, flags, &d); |
3fa9c3d7 | 1993 | if (!s) { |
a3621e74 | 1994 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Not present...\n")); |
6eb5f6b9 | 1995 | goto phooey; /* not present */ |
3fa9c3d7 | 1996 | } |
6eb5f6b9 JH |
1997 | } |
1998 | ||
1de06328 | 1999 | |
6eb5f6b9 JH |
2000 | |
2001 | /* Simplest case: anchored match need be tried only once. */ | |
2002 | /* [unless only anchor is BOL and multiline is set] */ | |
bbe252da | 2003 | if (prog->extflags & (RXf_ANCH & ~RXf_ANCH_GPOS)) { |
24b23f37 | 2004 | if (s == startpos && regtry(®info, &startpos)) |
6eb5f6b9 | 2005 | goto got_it; |
bbe252da YO |
2006 | else if (multiline || (prog->intflags & PREGf_IMPLICIT) |
2007 | || (prog->extflags & RXf_ANCH_MBOL)) /* XXXX SBOL? */ | |
6eb5f6b9 JH |
2008 | { |
2009 | char *end; | |
2010 | ||
2011 | if (minlen) | |
2012 | dontbother = minlen - 1; | |
1aa99e6b | 2013 | end = HOP3c(strend, -dontbother, strbeg) - 1; |
6eb5f6b9 | 2014 | /* for multiline we only have to try after newlines */ |
33b8afdf | 2015 | if (prog->check_substr || prog->check_utf8) { |
6eb5f6b9 JH |
2016 | if (s == startpos) |
2017 | goto after_try; | |
2018 | while (1) { | |
24b23f37 | 2019 | if (regtry(®info, &s)) |
6eb5f6b9 JH |
2020 | goto got_it; |
2021 | after_try: | |
5339e136 | 2022 | if (s > end) |
6eb5f6b9 | 2023 | goto phooey; |
bbe252da | 2024 | if (prog->extflags & RXf_USE_INTUIT) { |
288b8c02 | 2025 | s = re_intuit_start(rx, sv, s + 1, strend, flags, NULL); |
6eb5f6b9 JH |
2026 | if (!s) |
2027 | goto phooey; | |
2028 | } | |
2029 | else | |
2030 | s++; | |
2031 | } | |
2032 | } else { | |
2033 | if (s > startpos) | |
2034 | s--; | |
2035 | while (s < end) { | |
2036 | if (*s++ == '\n') { /* don't need PL_utf8skip here */ | |
24b23f37 | 2037 | if (regtry(®info, &s)) |
6eb5f6b9 JH |
2038 | goto got_it; |
2039 | } | |
2040 | } | |
2041 | } | |
2042 | } | |
2043 | goto phooey; | |
bbe252da | 2044 | } else if (RXf_GPOS_CHECK == (prog->extflags & RXf_GPOS_CHECK)) |
f9f4320a YO |
2045 | { |
2046 | /* the warning about reginfo.ganch being used without intialization | |
bbe252da | 2047 | is bogus -- we set it above, when prog->extflags & RXf_GPOS_SEEN |
f9f4320a | 2048 | and we only enter this block when the same bit is set. */ |
58e23c8d | 2049 | char *tmp_s = reginfo.ganch - prog->gofs; |
c584a96e YO |
2050 | |
2051 | if (tmp_s >= strbeg && regtry(®info, &tmp_s)) | |
6eb5f6b9 JH |
2052 | goto got_it; |
2053 | goto phooey; | |
2054 | } | |
2055 | ||
2056 | /* Messy cases: unanchored match. */ | |
bbe252da | 2057 | if ((prog->anchored_substr || prog->anchored_utf8) && prog->intflags & PREGf_SKIP) { |
6eb5f6b9 JH |
2058 | /* we have /x+whatever/ */ |
2059 | /* it must be a one character string (XXXX Except UTF?) */ | |
33b8afdf | 2060 | char ch; |
bf93d4cc GS |
2061 | #ifdef DEBUGGING |
2062 | int did_match = 0; | |
2063 | #endif | |
33b8afdf JH |
2064 | if (!(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr)) |
2065 | do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog); | |
3f7c398e | 2066 | ch = SvPVX_const(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr)[0]; |
bf93d4cc | 2067 | |
1aa99e6b | 2068 | if (do_utf8) { |
4cadc6a9 | 2069 | REXEC_FBC_SCAN( |
6eb5f6b9 | 2070 | if (*s == ch) { |
a3621e74 | 2071 | DEBUG_EXECUTE_r( did_match = 1 ); |
24b23f37 | 2072 | if (regtry(®info, &s)) goto got_it; |
6eb5f6b9 JH |
2073 | s += UTF8SKIP(s); |
2074 | while (s < strend && *s == ch) | |
2075 | s += UTF8SKIP(s); | |
2076 | } | |
4cadc6a9 | 2077 | ); |
6eb5f6b9 JH |
2078 | } |
2079 | else { | |
4cadc6a9 | 2080 | REXEC_FBC_SCAN( |
6eb5f6b9 | 2081 | if (*s == ch) { |
a3621e74 | 2082 | DEBUG_EXECUTE_r( did_match = 1 ); |
24b23f37 | 2083 | if (regtry(®info, &s)) goto got_it; |
6eb5f6b9 JH |
2084 | s++; |
2085 | while (s < strend && *s == ch) | |
2086 | s++; | |
2087 | } | |
4cadc6a9 | 2088 | ); |
6eb5f6b9 | 2089 | } |
a3621e74 | 2090 | DEBUG_EXECUTE_r(if (!did_match) |
bf93d4cc | 2091 | PerlIO_printf(Perl_debug_log, |
b7953727 JH |
2092 | "Did not find anchored character...\n") |
2093 | ); | |
6eb5f6b9 | 2094 | } |
a0714e2c SS |
2095 | else if (prog->anchored_substr != NULL |
2096 | || prog->anchored_utf8 != NULL | |
2097 | || ((prog->float_substr != NULL || prog->float_utf8 != NULL) | |
33b8afdf JH |
2098 | && prog->float_max_offset < strend - s)) { |
2099 | SV *must; | |
2100 | I32 back_max; | |
2101 | I32 back_min; | |
2102 | char *last; | |
6eb5f6b9 | 2103 | char *last1; /* Last position checked before */ |
bf93d4cc GS |
2104 | #ifdef DEBUGGING |
2105 | int did_match = 0; | |
2106 | #endif | |
33b8afdf JH |
2107 | if (prog->anchored_substr || prog->anchored_utf8) { |
2108 | if (!(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr)) | |
2109 | do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog); | |
2110 | must = do_utf8 ? prog->anchored_utf8 : prog->anchored_substr; | |
2111 | back_max = back_min = prog->anchored_offset; | |
2112 | } else { | |
2113 | if (!(do_utf8 ? prog->float_utf8 : prog->float_substr)) | |
2114 | do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog); | |
2115 | must = do_utf8 ? prog->float_utf8 : prog->float_substr; | |
2116 | back_max = prog->float_max_offset; | |
2117 | back_min = prog->float_min_offset; | |
2118 | } | |
1de06328 YO |
2119 | |
2120 | ||
33b8afdf JH |
2121 | if (must == &PL_sv_undef) |
2122 | /* could not downgrade utf8 check substring, so must fail */ | |
2123 | goto phooey; | |
2124 | ||
1de06328 YO |
2125 | if (back_min<0) { |
2126 | last = strend; | |
2127 | } else { | |
2128 | last = HOP3c(strend, /* Cannot start after this */ | |
2129 | -(I32)(CHR_SVLEN(must) | |
2130 | - (SvTAIL(must) != 0) + back_min), strbeg); | |
2131 | } | |
6eb5f6b9 JH |
2132 | if (s > PL_bostr) |
2133 | last1 = HOPc(s, -1); | |
2134 | else | |
2135 | last1 = s - 1; /* bogus */ | |
2136 | ||
a0288114 | 2137 | /* XXXX check_substr already used to find "s", can optimize if |
6eb5f6b9 JH |
2138 | check_substr==must. */ |
2139 | scream_pos = -1; | |
2140 | dontbother = end_shift; | |
2141 | strend = HOPc(strend, -dontbother); | |
2142 | while ( (s <= last) && | |
9041c2e3 | 2143 | ((flags & REXEC_SCREAM) |
1de06328 | 2144 | ? (s = screaminstr(sv, must, HOP3c(s, back_min, (back_min<0 ? strbeg : strend)) - strbeg, |
6eb5f6b9 | 2145 | end_shift, &scream_pos, 0)) |
1de06328 | 2146 | : (s = fbm_instr((unsigned char*)HOP3(s, back_min, (back_min<0 ? strbeg : strend)), |
9041c2e3 | 2147 | (unsigned char*)strend, must, |
7fba1cd6 | 2148 | multiline ? FBMrf_MULTILINE : 0))) ) { |
4addbd3b | 2149 | /* we may be pointing at the wrong string */ |
07bc277f | 2150 | if ((flags & REXEC_SCREAM) && RXp_MATCH_COPIED(prog)) |
3f7c398e | 2151 | s = strbeg + (s - SvPVX_const(sv)); |
a3621e74 | 2152 | DEBUG_EXECUTE_r( did_match = 1 ); |
6eb5f6b9 JH |
2153 | if (HOPc(s, -back_max) > last1) { |
2154 | last1 = HOPc(s, -back_min); | |
2155 | s = HOPc(s, -back_max); | |
2156 | } | |
2157 | else { | |
52657f30 | 2158 | char * const t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1; |
6eb5f6b9 JH |
2159 | |
2160 | last1 = HOPc(s, -back_min); | |
52657f30 | 2161 | s = t; |
6eb5f6b9 | 2162 | } |
1aa99e6b | 2163 | if (do_utf8) { |
6eb5f6b9 | 2164 | while (s <= last1) { |
24b23f37 | 2165 | if (regtry(®info, &s)) |
6eb5f6b9 JH |
2166 | goto got_it; |
2167 | s += UTF8SKIP(s); | |
2168 | } | |
2169 | } | |
2170 | else { | |
2171 | while (s <= last1) { | |
24b23f37 | 2172 | if (regtry(®info, &s)) |
6eb5f6b9 JH |
2173 | goto got_it; |
2174 | s++; | |
2175 | } | |
2176 | } | |
2177 | } | |
ab3bbdeb YO |
2178 | DEBUG_EXECUTE_r(if (!did_match) { |
2179 | RE_PV_QUOTED_DECL(quoted, do_utf8, PERL_DEBUG_PAD_ZERO(0), | |
2180 | SvPVX_const(must), RE_SV_DUMPLEN(must), 30); | |
2181 | PerlIO_printf(Perl_debug_log, "Did not find %s substr %s%s...\n", | |
33b8afdf | 2182 | ((must == prog->anchored_substr || must == prog->anchored_utf8) |
bf93d4cc | 2183 | ? "anchored" : "floating"), |
ab3bbdeb YO |
2184 | quoted, RE_SV_TAIL(must)); |
2185 | }); | |
6eb5f6b9 JH |
2186 | goto phooey; |
2187 | } | |
f8fc2ecf | 2188 | else if ( (c = progi->regstclass) ) { |
f14c76ed | 2189 | if (minlen) { |
f8fc2ecf | 2190 | const OPCODE op = OP(progi->regstclass); |
66e933ab | 2191 | /* don't bother with what can't match */ |
786e8c11 | 2192 | if (PL_regkind[op] != EXACT && op != CANY && PL_regkind[op] != TRIE) |
f14c76ed RGS |
2193 | strend = HOPc(strend, -(minlen - 1)); |
2194 | } | |
a3621e74 | 2195 | DEBUG_EXECUTE_r({ |
be8e71aa | 2196 | SV * const prop = sv_newmortal(); |
32fc9b6a | 2197 | regprop(prog, prop, c); |
0df25f3d | 2198 | { |
02daf0ab | 2199 | RE_PV_QUOTED_DECL(quoted,do_utf8,PERL_DEBUG_PAD_ZERO(1), |
ab3bbdeb | 2200 | s,strend-s,60); |
0df25f3d | 2201 | PerlIO_printf(Perl_debug_log, |
1c8f8eb1 | 2202 | "Matching stclass %.*s against %s (%d bytes)\n", |
e4f74956 | 2203 | (int)SvCUR(prop), SvPVX_const(prop), |
ab3bbdeb | 2204 | quoted, (int)(strend - s)); |
0df25f3d | 2205 | } |
ffc61ed2 | 2206 | }); |
3b0527fe | 2207 | if (find_byclass(prog, c, s, strend, ®info)) |
6eb5f6b9 | 2208 | goto got_it; |
07be1b83 | 2209 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Contradicts stclass... [regexec_flags]\n")); |
d6a28714 JH |
2210 | } |
2211 | else { | |
2212 | dontbother = 0; | |
a0714e2c | 2213 | if (prog->float_substr != NULL || prog->float_utf8 != NULL) { |
33b8afdf | 2214 | /* Trim the end. */ |
d6a28714 | 2215 | char *last; |
33b8afdf JH |
2216 | SV* float_real; |
2217 | ||
2218 | if (!(do_utf8 ? prog->float_utf8 : prog->float_substr)) | |
2219 | do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog); | |
2220 | float_real = do_utf8 ? prog->float_utf8 : prog->float_substr; | |
d6a28714 JH |
2221 | |
2222 | if (flags & REXEC_SCREAM) { | |
33b8afdf | 2223 | last = screaminstr(sv, float_real, s - strbeg, |
d6a28714 JH |
2224 | end_shift, &scream_pos, 1); /* last one */ |
2225 | if (!last) | |
ffc61ed2 | 2226 | last = scream_olds; /* Only one occurrence. */ |
4addbd3b | 2227 | /* we may be pointing at the wrong string */ |
07bc277f | 2228 | else if (RXp_MATCH_COPIED(prog)) |
3f7c398e | 2229 | s = strbeg + (s - SvPVX_const(sv)); |
b8c5462f | 2230 | } |
d6a28714 JH |
2231 | else { |
2232 | STRLEN len; | |
cfd0369c | 2233 | const char * const little = SvPV_const(float_real, len); |
d6a28714 | 2234 | |
33b8afdf | 2235 | if (SvTAIL(float_real)) { |
d6a28714 JH |
2236 | if (memEQ(strend - len + 1, little, len - 1)) |
2237 | last = strend - len + 1; | |
7fba1cd6 | 2238 | else if (!multiline) |
9041c2e3 | 2239 | last = memEQ(strend - len, little, len) |
bd61b366 | 2240 | ? strend - len : NULL; |
b8c5462f | 2241 | else |
d6a28714 JH |
2242 | goto find_last; |
2243 | } else { | |
2244 | find_last: | |
9041c2e3 | 2245 | if (len) |
d6a28714 | 2246 | last = rninstr(s, strend, little, little + len); |
b8c5462f | 2247 | else |
a0288114 | 2248 | last = strend; /* matching "$" */ |
b8c5462f | 2249 | } |
b8c5462f | 2250 | } |
bf93d4cc | 2251 | if (last == NULL) { |
6bda09f9 YO |
2252 | DEBUG_EXECUTE_r( |
2253 | PerlIO_printf(Perl_debug_log, | |
2254 | "%sCan't trim the tail, match fails (should not happen)%s\n", | |
2255 | PL_colors[4], PL_colors[5])); | |
bf93d4cc GS |
2256 | goto phooey; /* Should not happen! */ |
2257 | } | |
d6a28714 JH |
2258 | dontbother = strend - last + prog->float_min_offset; |
2259 | } | |
2260 | if (minlen && (dontbother < minlen)) | |
2261 | dontbother = minlen - 1; | |
2262 | strend -= dontbother; /* this one's always in bytes! */ | |
2263 | /* We don't know much -- general case. */ | |
1aa99e6b | 2264 | if (do_utf8) { |
d6a28714 | 2265 | for (;;) { |
24b23f37 | 2266 | if (regtry(®info, &s)) |
d6a28714 JH |
2267 | goto got_it; |
2268 | if (s >= strend) | |
2269 | break; | |
b8c5462f | 2270 | s += UTF8SKIP(s); |
d6a28714 JH |
2271 | }; |
2272 | } | |
2273 | else { | |
2274 | do { | |
24b23f37 | 2275 | if (regtry(®info, &s)) |
d6a28714 JH |
2276 | goto got_it; |
2277 | } while (s++ < strend); | |
2278 | } | |
2279 | } | |
2280 | ||
2281 | /* Failure. */ | |
2282 | goto phooey; | |
2283 | ||
2284 | got_it: | |
e9105d30 | 2285 | Safefree(swap); |
288b8c02 | 2286 | RX_MATCH_TAINTED_set(rx, PL_reg_flags & RF_tainted); |
d6a28714 | 2287 | |
19b95bf0 | 2288 | if (PL_reg_eval_set) |
4f639d21 | 2289 | restore_pos(aTHX_ prog); |
5daac39c NC |
2290 | if (RXp_PAREN_NAMES(prog)) |
2291 | (void)hv_iterinit(RXp_PAREN_NAMES(prog)); | |
d6a28714 JH |
2292 | |
2293 | /* make sure $`, $&, $', and $digit will work later */ | |
2294 | if ( !(flags & REXEC_NOT_FIRST) ) { | |
288b8c02 | 2295 | RX_MATCH_COPY_FREE(rx); |
d6a28714 | 2296 | if (flags & REXEC_COPY_STR) { |
be8e71aa | 2297 | const I32 i = PL_regeol - startpos + (stringarg - strbeg); |
f8c7b90f | 2298 | #ifdef PERL_OLD_COPY_ON_WRITE |
ed252734 NC |
2299 | if ((SvIsCOW(sv) |
2300 | || (SvFLAGS(sv) & CAN_COW_MASK) == CAN_COW_FLAGS)) { | |
2301 | if (DEBUG_C_TEST) { | |
2302 | PerlIO_printf(Perl_debug_log, | |
2303 | "Copy on write: regexp capture, type %d\n", | |
2304 | (int) SvTYPE(sv)); | |
2305 | } | |
2306 | prog->saved_copy = sv_setsv_cow(prog->saved_copy, sv); | |
d5263905 | 2307 | prog->subbeg = (char *)SvPVX_const(prog->saved_copy); |
ed252734 NC |
2308 | assert (SvPOKp(prog->saved_copy)); |
2309 | } else | |
2310 | #endif | |
2311 | { | |
288b8c02 | 2312 | RX_MATCH_COPIED_on(rx); |
ed252734 NC |
2313 | s = savepvn(strbeg, i); |
2314 | prog->subbeg = s; | |
2315 | } | |
d6a28714 | 2316 | prog->sublen = i; |
d6a28714 JH |
2317 | } |
2318 | else { | |
2319 | prog->subbeg = strbeg; | |
2320 | prog->sublen = PL_regeol - strbeg; /* strend may have been modified */ | |
2321 | } | |
2322 | } | |
9041c2e3 | 2323 | |
d6a28714 JH |
2324 | return 1; |
2325 | ||
2326 | phooey: | |
a3621e74 | 2327 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch failed%s\n", |
e4584336 | 2328 | PL_colors[4], PL_colors[5])); |
d6a28714 | 2329 | if (PL_reg_eval_set) |
4f639d21 | 2330 | restore_pos(aTHX_ prog); |
e9105d30 | 2331 | if (swap) { |
c74340f9 | 2332 | /* we failed :-( roll it back */ |
e9105d30 GG |
2333 | Safefree(prog->offs); |
2334 | prog->offs = swap; | |
2335 | } | |
2336 | ||
d6a28714 JH |
2337 | return 0; |
2338 | } | |
2339 | ||
6bda09f9 | 2340 | |
d6a28714 JH |
2341 | /* |
2342 | - regtry - try match at specific point | |
2343 | */ | |
2344 | STATIC I32 /* 0 failure, 1 success */ | |
24b23f37 | 2345 | S_regtry(pTHX_ regmatch_info *reginfo, char **startpos) |
d6a28714 | 2346 | { |
97aff369 | 2347 | dVAR; |
d6a28714 | 2348 | CHECKPOINT lastcp; |
288b8c02 NC |
2349 | REGEXP *const rx = reginfo->prog; |
2350 | regexp *const prog = (struct regexp *)SvANY(rx); | |
f8fc2ecf | 2351 | RXi_GET_DECL(prog,progi); |
a3621e74 | 2352 | GET_RE_DEBUG_FLAGS_DECL; |
7918f24d NC |
2353 | |
2354 | PERL_ARGS_ASSERT_REGTRY; | |
2355 | ||
24b23f37 | 2356 | reginfo->cutpoint=NULL; |
d6a28714 | 2357 | |
bbe252da | 2358 | if ((prog->extflags & RXf_EVAL_SEEN) && !PL_reg_eval_set) { |
d6a28714 JH |
2359 | MAGIC *mg; |
2360 | ||
2361 | PL_reg_eval_set = RS_init; | |
a3621e74 | 2362 | DEBUG_EXECUTE_r(DEBUG_s( |
b900a521 JH |
2363 | PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %"IVdf"\n", |
2364 | (IV)(PL_stack_sp - PL_stack_base)); | |
d6a28714 | 2365 | )); |
ea8d6ae1 | 2366 | SAVESTACK_CXPOS(); |
d6a28714 JH |
2367 | cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base; |
2368 | /* Otherwise OP_NEXTSTATE will free whatever on stack now. */ | |
2369 | SAVETMPS; | |
2370 | /* Apparently this is not needed, judging by wantarray. */ | |
e8347627 | 2371 | /* SAVEI8(cxstack[cxstack_ix].blk_gimme); |
d6a28714 JH |
2372 | cxstack[cxstack_ix].blk_gimme = G_SCALAR; */ |
2373 | ||
3b0527fe | 2374 | if (reginfo->sv) { |
d6a28714 | 2375 | /* Make $_ available to executed code. */ |
3b0527fe | 2376 | if (reginfo->sv != DEFSV) { |
59f00321 | 2377 | SAVE_DEFSV; |
414bf5ae | 2378 | DEFSV_set(reginfo->sv); |
b8c5462f | 2379 | } |
d6a28714 | 2380 | |
3b0527fe DM |
2381 | if (!(SvTYPE(reginfo->sv) >= SVt_PVMG && SvMAGIC(reginfo->sv) |
2382 | && (mg = mg_find(reginfo->sv, PERL_MAGIC_regex_global)))) { | |
d6a28714 | 2383 | /* prepare for quick setting of pos */ |
d300d9fa | 2384 | #ifdef PERL_OLD_COPY_ON_WRITE |
51a9ea20 NC |
2385 | if (SvIsCOW(reginfo->sv)) |
2386 | sv_force_normal_flags(reginfo->sv, 0); | |
d300d9fa | 2387 | #endif |
3dab1dad | 2388 | mg = sv_magicext(reginfo->sv, NULL, PERL_MAGIC_regex_global, |
d300d9fa | 2389 | &PL_vtbl_mglob, NULL, 0); |
d6a28714 | 2390 | mg->mg_len = -1; |
b8c5462f | 2391 | } |
d6a28714 JH |
2392 | PL_reg_magic = mg; |
2393 | PL_reg_oldpos = mg->mg_len; | |
4f639d21 | 2394 | SAVEDESTRUCTOR_X(restore_pos, prog); |
d6a28714 | 2395 | } |
09687e5a | 2396 | if (!PL_reg_curpm) { |
a02a5408 | 2397 | Newxz(PL_reg_curpm, 1, PMOP); |
09687e5a AB |
2398 | #ifdef USE_ITHREADS |
2399 | { | |
14a49a24 | 2400 | SV* const repointer = &PL_sv_undef; |
92313705 NC |
2401 | /* this regexp is also owned by the new PL_reg_curpm, which |
2402 | will try to free it. */ | |
d2ece331 | 2403 | av_push(PL_regex_padav, repointer); |
09687e5a AB |
2404 | PL_reg_curpm->op_pmoffset = av_len(PL_regex_padav); |
2405 | PL_regex_pad = AvARRAY(PL_regex_padav); | |
2406 | } | |
2407 | #endif | |
2408 | } | |
86c29d75 NC |
2409 | #ifdef USE_ITHREADS |
2410 | /* It seems that non-ithreads works both with and without this code. | |
2411 | So for efficiency reasons it seems best not to have the code | |
2412 | compiled when it is not needed. */ | |
92313705 NC |
2413 | /* This is safe against NULLs: */ |
2414 | ReREFCNT_dec(PM_GETRE(PL_reg_curpm)); | |
2415 | /* PM_reg_curpm owns a reference to this regexp. */ | |
2416 | ReREFCNT_inc(rx); | |
86c29d75 | 2417 | #endif |
288b8c02 | 2418 | PM_SETRE(PL_reg_curpm, rx); |
d6a28714 JH |
2419 | PL_reg_oldcurpm = PL_curpm; |
2420 | PL_curpm = PL_reg_curpm; | |
07bc277f | 2421 | if (RXp_MATCH_COPIED(prog)) { |
d6a28714 JH |
2422 | /* Here is a serious problem: we cannot rewrite subbeg, |
2423 | since it may be needed if this match fails. Thus | |
2424 | $` inside (?{}) could fail... */ | |
2425 | PL_reg_oldsaved = prog->subbeg; | |
2426 | PL_reg_oldsavedlen = prog->sublen; | |
f8c7b90f | 2427 | #ifdef PERL_OLD_COPY_ON_WRITE |
ed252734 NC |
2428 | PL_nrs = prog->saved_copy; |
2429 | #endif | |
07bc277f | 2430 | RXp_MATCH_COPIED_off(prog); |
d6a28714 JH |
2431 | } |
2432 | else | |
bd61b366 | 2433 | PL_reg_oldsaved = NULL; |
d6a28714 JH |
2434 | prog->subbeg = PL_bostr; |
2435 | prog->sublen = PL_regeol - PL_bostr; /* strend may have been modified */ | |
2436 | } | |
24b23f37 | 2437 | DEBUG_EXECUTE_r(PL_reg_starttry = *startpos); |
f0ab9afb | 2438 | prog->offs[0].start = *startpos - PL_bostr; |
24b23f37 | 2439 | PL_reginput = *startpos; |
d6a28714 | 2440 | PL_reglastparen = &prog->lastparen; |
a01268b5 | 2441 | PL_reglastcloseparen = &prog->lastcloseparen; |
d6a28714 | 2442 | prog->lastparen = 0; |
03994de8 | 2443 | prog->lastcloseparen = 0; |
d6a28714 | 2444 | PL_regsize = 0; |
f0ab9afb | 2445 | PL_regoffs = prog->offs; |
d6a28714 JH |
2446 | if (PL_reg_start_tmpl <= prog->nparens) { |
2447 | PL_reg_start_tmpl = prog->nparens*3/2 + 3; | |
2448 | if(PL_reg_start_tmp) | |
2449 | Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*); | |
2450 | else | |
a02a5408 | 2451 | Newx(PL_reg_start_tmp, PL_reg_start_tmpl, char*); |
d6a28714 JH |
2452 | } |
2453 | ||
2454 | /* XXXX What this code is doing here?!!! There should be no need | |
2455 | to do this again and again, PL_reglastparen should take care of | |
3dd2943c | 2456 | this! --ilya*/ |
dafc8851 JH |
2457 | |
2458 | /* Tests pat.t#187 and split.t#{13,14} seem to depend on this code. | |
2459 | * Actually, the code in regcppop() (which Ilya may be meaning by | |
daf18116 | 2460 | * PL_reglastparen), is not needed at all by the test suite |
225593e1 DM |
2461 | * (op/regexp, op/pat, op/split), but that code is needed otherwise |
2462 | * this erroneously leaves $1 defined: "1" =~ /^(?:(\d)x)?\d$/ | |
2463 | * Meanwhile, this code *is* needed for the | |
daf18116 JH |
2464 | * above-mentioned test suite tests to succeed. The common theme |
2465 | * on those tests seems to be returning null fields from matches. | |
225593e1 | 2466 | * --jhi updated by dapm */ |
dafc8851 | 2467 | #if 1 |
d6a28714 | 2468 | if (prog->nparens) { |
f0ab9afb | 2469 | regexp_paren_pair *pp = PL_regoffs; |
097eb12c | 2470 | register I32 i; |
eb160463 | 2471 | for (i = prog->nparens; i > (I32)*PL_reglastparen; i--) { |
f0ab9afb NC |
2472 | ++pp; |
2473 | pp->start = -1; | |
2474 | pp->end = -1; | |
d6a28714 JH |
2475 | } |
2476 | } | |
dafc8851 | 2477 | #endif |
02db2b7b | 2478 | REGCP_SET(lastcp); |
f8fc2ecf | 2479 | if (regmatch(reginfo, progi->program + 1)) { |
f0ab9afb | 2480 | PL_regoffs[0].end = PL_reginput - PL_bostr; |
d6a28714 JH |
2481 | return 1; |
2482 | } | |
24b23f37 YO |
2483 | if (reginfo->cutpoint) |
2484 | *startpos= reginfo->cutpoint; | |
02db2b7b | 2485 | REGCP_UNWIND(lastcp); |
d6a28714 JH |
2486 | return 0; |
2487 | } | |
2488 | ||
02db2b7b | 2489 | |
8ba1375e MJD |
2490 | #define sayYES goto yes |
2491 | #define sayNO goto no | |
262b90c4 | 2492 | #define sayNO_SILENT goto no_silent |
8ba1375e | 2493 | |
f9f4320a YO |
2494 | /* we dont use STMT_START/END here because it leads to |
2495 | "unreachable code" warnings, which are bogus, but distracting. */ | |
2496 | #define CACHEsayNO \ | |
c476f425 DM |
2497 | if (ST.cache_mask) \ |
2498 | PL_reg_poscache[ST.cache_offset] |= ST.cache_mask; \ | |
f9f4320a | 2499 | sayNO |
3298f257 | 2500 | |
a3621e74 | 2501 | /* this is used to determine how far from the left messages like |
265c4333 YO |
2502 | 'failed...' are printed. It should be set such that messages |
2503 | are inline with the regop output that created them. | |
a3621e74 | 2504 | */ |
265c4333 | 2505 | #define REPORT_CODE_OFF 32 |
a3621e74 YO |
2506 | |
2507 | ||
40a82448 DM |
2508 | #define CHRTEST_UNINIT -1001 /* c1/c2 haven't been calculated yet */ |
2509 | #define CHRTEST_VOID -1000 /* the c1/c2 "next char" test should be skipped */ | |
9e137952 | 2510 | |
86545054 DM |
2511 | #define SLAB_FIRST(s) (&(s)->states[0]) |
2512 | #define SLAB_LAST(s) (&(s)->states[PERL_REGMATCH_SLAB_SLOTS-1]) | |
2513 | ||
5d9a96ca DM |
2514 | /* grab a new slab and return the first slot in it */ |
2515 | ||
2516 | STATIC regmatch_state * | |
2517 | S_push_slab(pTHX) | |
2518 | { | |
a35a87e7 | 2519 | #if PERL_VERSION < 9 && !defined(PERL_CORE) |
54df2634 NC |
2520 | dMY_CXT; |
2521 | #endif | |
5d9a96ca DM |
2522 | regmatch_slab *s = PL_regmatch_slab->next; |
2523 | if (!s) { | |
2524 | Newx(s, 1, regmatch_slab); | |
2525 | s->prev = PL_regmatch_slab; | |
2526 | s->next = NULL; | |
2527 | PL_regmatch_slab->next = s; | |
2528 | } | |
2529 | PL_regmatch_slab = s; | |
86545054 | 2530 | return SLAB_FIRST(s); |
5d9a96ca | 2531 | } |
5b47454d | 2532 | |
95b24440 | 2533 | |
40a82448 DM |
2534 | /* push a new state then goto it */ |
2535 | ||
2536 | #define PUSH_STATE_GOTO(state, node) \ | |
2537 | scan = node; \ | |
2538 | st->resume_state = state; \ | |
2539 | goto push_state; | |
2540 | ||
2541 | /* push a new state with success backtracking, then goto it */ | |
2542 | ||
2543 | #define PUSH_YES_STATE_GOTO(state, node) \ | |
2544 | scan = node; \ | |
2545 | st->resume_state = state; \ | |
2546 | goto push_yes_state; | |
2547 | ||
aa283a38 | 2548 | |
aa283a38 | 2549 | |
d6a28714 | 2550 | /* |
95b24440 | 2551 | |
bf1f174e DM |
2552 | regmatch() - main matching routine |
2553 | ||
2554 | This is basically one big switch statement in a loop. We execute an op, | |
2555 | set 'next' to point the next op, and continue. If we come to a point which | |
2556 | we may need to backtrack to on failure such as (A|B|C), we push a | |
2557 | backtrack state onto the backtrack stack. On failure, we pop the top | |
2558 | state, and re-enter the loop at the state indicated. If there are no more | |
2559 | states to pop, we return failure. | |
2560 | ||
2561 | Sometimes we also need to backtrack on success; for example /A+/, where | |
2562 | after successfully matching one A, we need to go back and try to | |
2563 | match another one; similarly for lookahead assertions: if the assertion | |
2564 | completes successfully, we backtrack to the state just before the assertion | |
2565 | and then carry on. In these cases, the pushed state is marked as | |
2566 | 'backtrack on success too'. This marking is in fact done by a chain of | |
2567 | pointers, each pointing to the previous 'yes' state. On success, we pop to | |
2568 | the nearest yes state, discarding any intermediate failure-only states. | |
2569 | Sometimes a yes state is pushed just to force some cleanup code to be | |
2570 | called at the end of a successful match or submatch; e.g. (??{$re}) uses | |
2571 | it to free the inner regex. | |
2572 | ||
2573 | Note that failure backtracking rewinds the cursor position, while | |
2574 | success backtracking leaves it alone. | |
2575 | ||
2576 | A pattern is complete when the END op is executed, while a subpattern | |
2577 | such as (?=foo) is complete when the SUCCESS op is executed. Both of these | |
2578 | ops trigger the "pop to last yes state if any, otherwise return true" | |
2579 | behaviour. | |
2580 | ||
2581 | A common convention in this function is to use A and B to refer to the two | |
2582 | subpatterns (or to the first nodes thereof) in patterns like /A*B/: so A is | |
2583 | the subpattern to be matched possibly multiple times, while B is the entire | |
2584 | rest of the pattern. Variable and state names reflect this convention. | |
2585 | ||
2586 | The states in the main switch are the union of ops and failure/success of | |
2587 | substates associated with with that op. For example, IFMATCH is the op | |
2588 | that does lookahead assertions /(?=A)B/ and so the IFMATCH state means | |
2589 | 'execute IFMATCH'; while IFMATCH_A is a state saying that we have just | |
2590 | successfully matched A and IFMATCH_A_fail is a state saying that we have | |
2591 | just failed to match A. Resume states always come in pairs. The backtrack | |
2592 | state we push is marked as 'IFMATCH_A', but when that is popped, we resume | |
2593 | at IFMATCH_A or IFMATCH_A_fail, depending on whether we are backtracking | |
2594 | on success or failure. | |
2595 | ||
2596 | The struct that holds a backtracking state is actually a big union, with | |
2597 | one variant for each major type of op. The variable st points to the | |
2598 | top-most backtrack struct. To make the code clearer, within each | |
2599 | block of code we #define ST to alias the relevant union. | |
2600 | ||
2601 | Here's a concrete example of a (vastly oversimplified) IFMATCH | |
2602 | implementation: | |
2603 | ||
2604 | switch (state) { | |
2605 | .... | |
2606 | ||
2607 | #define ST st->u.ifmatch | |
2608 | ||
2609 | case IFMATCH: // we are executing the IFMATCH op, (?=A)B | |
2610 | ST.foo = ...; // some state we wish to save | |
95b24440 | 2611 | ... |
bf1f174e DM |
2612 | // push a yes backtrack state with a resume value of |
2613 | // IFMATCH_A/IFMATCH_A_fail, then continue execution at the | |
2614 | // first node of A: | |
2615 | PUSH_YES_STATE_GOTO(IFMATCH_A, A); | |
2616 | // NOTREACHED | |
2617 | ||
2618 | case IFMATCH_A: // we have successfully executed A; now continue with B | |
2619 | next = B; | |
2620 | bar = ST.foo; // do something with the preserved value | |
2621 | break; | |
2622 | ||
2623 | case IFMATCH_A_fail: // A failed, so the assertion failed | |
2624 | ...; // do some housekeeping, then ... | |
2625 | sayNO; // propagate the failure | |
2626 | ||
2627 | #undef ST | |
95b24440 | 2628 | |
bf1f174e DM |
2629 | ... |
2630 | } | |
95b24440 | 2631 | |
bf1f174e DM |
2632 | For any old-timers reading this who are familiar with the old recursive |
2633 | approach, the code above is equivalent to: | |
95b24440 | 2634 | |
bf1f174e DM |
2635 | case IFMATCH: // we are executing the IFMATCH op, (?=A)B |
2636 | { | |
2637 | int foo = ... | |
95b24440 | 2638 | ... |
bf1f174e DM |
2639 | if (regmatch(A)) { |
2640 | next = B; | |
2641 | bar = foo; | |
2642 | break; | |
95b24440 | 2643 | } |
bf1f174e DM |
2644 | ...; // do some housekeeping, then ... |
2645 | sayNO; // propagate the failure | |
95b24440 | 2646 | } |
bf1f174e DM |
2647 | |
2648 | The topmost backtrack state, pointed to by st, is usually free. If you | |
2649 | want to claim it, populate any ST.foo fields in it with values you wish to | |
2650 | save, then do one of | |
2651 | ||
2652 | PUSH_STATE_GOTO(resume_state, node); | |
2653 | PUSH_YES_STATE_GOTO(resume_state, node); | |
2654 | ||
2655 | which sets that backtrack state's resume value to 'resume_state', pushes a | |
2656 | new free entry to the top of the backtrack stack, then goes to 'node'. | |
2657 | On backtracking, the free slot is popped, and the saved state becomes the | |
2658 | new free state. An ST.foo field in this new top state can be temporarily | |
2659 | accessed to retrieve values, but once the main loop is re-entered, it | |
2660 | becomes available for reuse. | |
2661 | ||
2662 | Note that the depth of the backtrack stack constantly increases during the | |
2663 | left-to-right execution of the pattern, rather than going up and down with | |
2664 | the pattern nesting. For example the stack is at its maximum at Z at the | |
2665 | end of the pattern, rather than at X in the following: | |
2666 | ||
2667 | /(((X)+)+)+....(Y)+....Z/ | |
2668 | ||
2669 | The only exceptions to this are lookahead/behind assertions and the cut, | |
2670 | (?>A), which pop all the backtrack states associated with A before | |
2671 | continuing. | |
2672 | ||
2673 | Bascktrack state structs are allocated in slabs of about 4K in size. | |
2674 | PL_regmatch_state and st always point to the currently active state, | |
2675 | and PL_regmatch_slab points to the slab currently containing | |
2676 | PL_regmatch_state. The first time regmatch() is called, the first slab is | |
2677 | allocated, and is never freed until interpreter destruction. When the slab | |
2678 | is full, a new one is allocated and chained to the end. At exit from | |
2679 | regmatch(), slabs allocated since entry are freed. | |
2680 | ||
2681 | */ | |
95b24440 | 2682 | |
40a82448 | 2683 | |
5bc10b2c | 2684 | #define DEBUG_STATE_pp(pp) \ |
265c4333 | 2685 | DEBUG_STATE_r({ \ |
5bc10b2c DM |
2686 | DUMP_EXEC_POS(locinput, scan, do_utf8); \ |
2687 | PerlIO_printf(Perl_debug_log, \ | |
5d458dd8 | 2688 | " %*s"pp" %s%s%s%s%s\n", \ |
5bc10b2c | 2689 | depth*2, "", \ |
13d6edb4 | 2690 | PL_reg_name[st->resume_state], \ |
5d458dd8 YO |
2691 | ((st==yes_state||st==mark_state) ? "[" : ""), \ |
2692 | ((st==yes_state) ? "Y" : ""), \ | |
2693 | ((st==mark_state) ? "M" : ""), \ | |
2694 | ((st==yes_state||st==mark_state) ? "]" : "") \ | |
2695 | ); \ | |
265c4333 | 2696 | }); |
5bc10b2c | 2697 | |
40a82448 | 2698 | |
3dab1dad | 2699 | #define REG_NODE_NUM(x) ((x) ? (int)((x)-prog) : -1) |
95b24440 | 2700 | |
3df15adc | 2701 | #ifdef DEBUGGING |
5bc10b2c | 2702 | |
ab3bbdeb | 2703 | STATIC void |
efd26800 | 2704 | S_debug_start_match(pTHX_ const REGEXP *prog, const bool do_utf8, |
ab3bbdeb YO |
2705 | const char *start, const char *end, const char *blurb) |
2706 | { | |
efd26800 | 2707 | const bool utf8_pat = RX_UTF8(prog) ? 1 : 0; |
7918f24d NC |
2708 | |
2709 | PERL_ARGS_ASSERT_DEBUG_START_MATCH; | |
2710 | ||
ab3bbdeb YO |
2711 | if (!PL_colorset) |
2712 | reginitcolors(); | |
2713 | { | |
2714 | RE_PV_QUOTED_DECL(s0, utf8_pat, PERL_DEBUG_PAD_ZERO(0), | |
d2c6dc5e | 2715 | RX_PRECOMP_const(prog), RX_PRELEN(prog), 60); |
ab3bbdeb YO |
2716 | |
2717 | RE_PV_QUOTED_DECL(s1, do_utf8, PERL_DEBUG_PAD_ZERO(1), | |
2718 | start, end - start, 60); | |
2719 | ||
2720 | PerlIO_printf(Perl_debug_log, | |
2721 | "%s%s REx%s %s against %s\n", | |
2722 | PL_colors[4], blurb, PL_colors[5], s0, s1); | |
2723 | ||
2724 | if (do_utf8||utf8_pat) | |
1de06328 YO |
2725 | PerlIO_printf(Perl_debug_log, "UTF-8 %s%s%s...\n", |
2726 | utf8_pat ? "pattern" : "", | |
2727 | utf8_pat && do_utf8 ? " and " : "", | |
2728 | do_utf8 ? "string" : "" | |
ab3bbdeb YO |
2729 | ); |
2730 | } | |
2731 | } | |
3df15adc YO |
2732 | |
2733 | STATIC void | |
786e8c11 YO |
2734 | S_dump_exec_pos(pTHX_ const char *locinput, |
2735 | const regnode *scan, | |
2736 | const char *loc_regeol, | |
2737 | const char *loc_bostr, | |
2738 | const char *loc_reg_starttry, | |
2739 | const bool do_utf8) | |
07be1b83 | 2740 | { |
786e8c11 | 2741 | const int docolor = *PL_colors[0] || *PL_colors[2] || *PL_colors[4]; |
07be1b83 | 2742 | const int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */ |
786e8c11 | 2743 | int l = (loc_regeol - locinput) > taill ? taill : (loc_regeol - locinput); |
07be1b83 YO |
2744 | /* The part of the string before starttry has one color |
2745 | (pref0_len chars), between starttry and current | |
2746 | position another one (pref_len - pref0_len chars), | |
2747 | after the current position the third one. | |
2748 | We assume that pref0_len <= pref_len, otherwise we | |
2749 | decrease pref0_len. */ | |
786e8c11 YO |
2750 | int pref_len = (locinput - loc_bostr) > (5 + taill) - l |
2751 | ? (5 + taill) - l : locinput - loc_bostr; | |
07be1b83 YO |
2752 | int pref0_len; |
2753 | ||
7918f24d NC |
2754 | PERL_ARGS_ASSERT_DUMP_EXEC_POS; |
2755 | ||
07be1b83 YO |
2756 | while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput - pref_len))) |
2757 | pref_len++; | |
786e8c11 YO |
2758 | pref0_len = pref_len - (locinput - loc_reg_starttry); |
2759 | if (l + pref_len < (5 + taill) && l < loc_regeol - locinput) | |
2760 | l = ( loc_regeol - locinput > (5 + taill) - pref_len | |
2761 | ? (5 + taill) - pref_len : loc_regeol - locinput); | |
07be1b83 YO |
2762 | while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput + l))) |
2763 | l--; | |
2764 | if (pref0_len < 0) | |
2765 | pref0_len = 0; | |
2766 | if (pref0_len > pref_len) | |
2767 | pref0_len = pref_len; | |
2768 | { | |
3df15adc | 2769 | const int is_uni = (do_utf8 && OP(scan) != CANY) ? 1 : 0; |
0df25f3d | 2770 | |
ab3bbdeb | 2771 | RE_PV_COLOR_DECL(s0,len0,is_uni,PERL_DEBUG_PAD(0), |
1de06328 | 2772 | (locinput - pref_len),pref0_len, 60, 4, 5); |
0df25f3d | 2773 | |
ab3bbdeb | 2774 | RE_PV_COLOR_DECL(s1,len1,is_uni,PERL_DEBUG_PAD(1), |
3df15adc | 2775 | (locinput - pref_len + pref0_len), |
1de06328 | 2776 | pref_len - pref0_len, 60, 2, 3); |
0df25f3d | 2777 | |
ab3bbdeb | 2778 | RE_PV_COLOR_DECL(s2,len2,is_uni,PERL_DEBUG_PAD(2), |
1de06328 | 2779 | locinput, loc_regeol - locinput, 10, 0, 1); |
0df25f3d | 2780 | |
1de06328 | 2781 | const STRLEN tlen=len0+len1+len2; |
3df15adc | 2782 | PerlIO_printf(Perl_debug_log, |
ab3bbdeb | 2783 | "%4"IVdf" <%.*s%.*s%s%.*s>%*s|", |
786e8c11 | 2784 | (IV)(locinput - loc_bostr), |
07be1b83 | 2785 | len0, s0, |
07be1b83 | 2786 | len1, s1, |
07be1b83 | 2787 | (docolor ? "" : "> <"), |
07be1b83 | 2788 | len2, s2, |
f9f4320a | 2789 | (int)(tlen > 19 ? 0 : 19 - tlen), |
07be1b83 YO |
2790 | ""); |
2791 | } | |
2792 | } | |
3df15adc | 2793 | |
07be1b83 YO |
2794 | #endif |
2795 | ||
0a4db386 YO |
2796 | /* reg_check_named_buff_matched() |
2797 | * Checks to see if a named buffer has matched. The data array of | |
2798 | * buffer numbers corresponding to the buffer is expected to reside | |
2799 | * in the regexp->data->data array in the slot stored in the ARG() of | |
2800 | * node involved. Note that this routine doesn't actually care about the | |
2801 | * name, that information is not preserved from compilation to execution. | |
2802 | * Returns the index of the leftmost defined buffer with the given name | |
2803 | * or 0 if non of the buffers matched. | |
2804 | */ | |
2805 | STATIC I32 | |
7918f24d NC |
2806 | S_reg_check_named_buff_matched(pTHX_ const regexp *rex, const regnode *scan) |
2807 | { | |
0a4db386 | 2808 | I32 n; |
f8fc2ecf | 2809 | RXi_GET_DECL(rex,rexi); |
ad64d0ec | 2810 | SV *sv_dat= MUTABLE_SV(rexi->data->data[ ARG( scan ) ]); |
0a4db386 | 2811 | I32 *nums=(I32*)SvPVX(sv_dat); |
7918f24d NC |
2812 | |
2813 | PERL_ARGS_ASSERT_REG_CHECK_NAMED_BUFF_MATCHED; | |
2814 | ||
0a4db386 YO |
2815 | for ( n=0; n<SvIVX(sv_dat); n++ ) { |
2816 | if ((I32)*PL_reglastparen >= nums[n] && | |
f0ab9afb | 2817 | PL_regoffs[nums[n]].end != -1) |
0a4db386 YO |
2818 | { |
2819 | return nums[n]; | |
2820 | } | |
2821 | } | |
2822 | return 0; | |
2823 | } | |
2824 | ||
2f554ef7 DM |
2825 | |
2826 | /* free all slabs above current one - called during LEAVE_SCOPE */ | |
2827 | ||
2828 | STATIC void | |
2829 | S_clear_backtrack_stack(pTHX_ void *p) | |
2830 | { | |
2831 | regmatch_slab *s = PL_regmatch_slab->next; | |
2832 | PERL_UNUSED_ARG(p); | |
2833 | ||
2834 | if (!s) | |
2835 | return; | |
2836 | PL_regmatch_slab->next = NULL; | |
2837 | while (s) { | |
2838 | regmatch_slab * const osl = s; | |
2839 | s = s->next; | |
2840 | Safefree(osl); | |
2841 | } | |
2842 | } | |
2843 | ||
2844 | ||
28d8d7f4 YO |
2845 | #define SETREX(Re1,Re2) \ |
2846 | if (PL_reg_eval_set) PM_SETRE((PL_reg_curpm), (Re2)); \ | |
2847 | Re1 = (Re2) | |
2848 | ||
d6a28714 | 2849 | STATIC I32 /* 0 failure, 1 success */ |
24b23f37 | 2850 | S_regmatch(pTHX_ regmatch_info *reginfo, regnode *prog) |
d6a28714 | 2851 | { |
a35a87e7 | 2852 | #if PERL_VERSION < 9 && !defined(PERL_CORE) |
54df2634 NC |
2853 | dMY_CXT; |
2854 | #endif | |
27da23d5 | 2855 | dVAR; |
95b24440 | 2856 | register const bool do_utf8 = PL_reg_match_utf8; |
4ad0818d | 2857 | const U32 uniflags = UTF8_ALLOW_DEFAULT; |
288b8c02 NC |
2858 | REGEXP *rex_sv = reginfo->prog; |
2859 | regexp *rex = (struct regexp *)SvANY(rex_sv); | |
f8fc2ecf | 2860 | RXi_GET_DECL(rex,rexi); |
2f554ef7 | 2861 | I32 oldsave; |
5d9a96ca DM |
2862 | /* the current state. This is a cached copy of PL_regmatch_state */ |
2863 | register regmatch_state *st; | |
5d9a96ca DM |
2864 | /* cache heavy used fields of st in registers */ |
2865 | register regnode *scan; | |
2866 | register regnode *next; | |
438e9bae | 2867 | register U32 n = 0; /* general value; init to avoid compiler warning */ |
24d3c4a9 | 2868 | register I32 ln = 0; /* len or last; init to avoid compiler warning */ |
5d9a96ca | 2869 | register char *locinput = PL_reginput; |
5d9a96ca | 2870 | register I32 nextchr; /* is always set to UCHARAT(locinput) */ |
24d3c4a9 | 2871 | |
b69b0499 | 2872 | bool result = 0; /* return value of S_regmatch */ |
24d3c4a9 | 2873 | int depth = 0; /* depth of backtrack stack */ |
4b196cd4 YO |
2874 | U32 nochange_depth = 0; /* depth of GOSUB recursion with nochange */ |
2875 | const U32 max_nochange_depth = | |
2876 | (3 * rex->nparens > MAX_RECURSE_EVAL_NOCHANGE_DEPTH) ? | |
2877 | 3 * rex->nparens : MAX_RECURSE_EVAL_NOCHANGE_DEPTH; | |
77cb431f DM |
2878 | regmatch_state *yes_state = NULL; /* state to pop to on success of |
2879 | subpattern */ | |
e2e6a0f1 YO |
2880 | /* mark_state piggy backs on the yes_state logic so that when we unwind |
2881 | the stack on success we can update the mark_state as we go */ | |
2882 | regmatch_state *mark_state = NULL; /* last mark state we have seen */ | |
faec1544 | 2883 | regmatch_state *cur_eval = NULL; /* most recent EVAL_AB state */ |
b8591aee | 2884 | struct regmatch_state *cur_curlyx = NULL; /* most recent curlyx */ |
40a82448 | 2885 | U32 state_num; |
5d458dd8 YO |
2886 | bool no_final = 0; /* prevent failure from backtracking? */ |
2887 | bool do_cutgroup = 0; /* no_final only until next branch/trie entry */ | |
e2e6a0f1 | 2888 | char *startpoint = PL_reginput; |
5d458dd8 YO |
2889 | SV *popmark = NULL; /* are we looking for a mark? */ |
2890 | SV *sv_commit = NULL; /* last mark name seen in failure */ | |
2891 | SV *sv_yes_mark = NULL; /* last mark name we have seen | |
2892 | during a successfull match */ | |
2893 | U32 lastopen = 0; /* last open we saw */ | |
2894 | bool has_cutgroup = RX_HAS_CUTGROUP(rex) ? 1 : 0; | |
19b95bf0 | 2895 | SV* const oreplsv = GvSV(PL_replgv); |
24d3c4a9 DM |
2896 | /* these three flags are set by various ops to signal information to |
2897 | * the very next op. They have a useful lifetime of exactly one loop | |
2898 | * iteration, and are not preserved or restored by state pushes/pops | |
2899 | */ | |
2900 | bool sw = 0; /* the condition value in (?(cond)a|b) */ | |
2901 | bool minmod = 0; /* the next "{n,m}" is a "{n,m}?" */ | |
2902 | int logical = 0; /* the following EVAL is: | |
2903 | 0: (?{...}) | |
2904 | 1: (?(?{...})X|Y) | |
2905 | 2: (??{...}) | |
2906 | or the following IFMATCH/UNLESSM is: | |
2907 | false: plain (?=foo) | |
2908 | true: used as a condition: (?(?=foo)) | |
2909 | */ | |
95b24440 | 2910 | #ifdef DEBUGGING |
e68ec53f | 2911 | GET_RE_DEBUG_FLAGS_DECL; |
d6a28714 JH |
2912 | #endif |
2913 | ||
7918f24d NC |
2914 | PERL_ARGS_ASSERT_REGMATCH; |
2915 | ||
3b57cd43 | 2916 | DEBUG_OPTIMISE_r( DEBUG_EXECUTE_r({ |
24b23f37 | 2917 | PerlIO_printf(Perl_debug_log,"regmatch start\n"); |
3b57cd43 | 2918 | })); |
5d9a96ca DM |
2919 | /* on first ever call to regmatch, allocate first slab */ |
2920 | if (!PL_regmatch_slab) { | |
2921 | Newx(PL_regmatch_slab, 1, regmatch_slab); | |
2922 | PL_regmatch_slab->prev = NULL; | |
2923 | PL_regmatch_slab->next = NULL; | |
86545054 | 2924 | PL_regmatch_state = SLAB_FIRST(PL_regmatch_slab); |
5d9a96ca DM |
2925 | } |
2926 | ||
2f554ef7 DM |
2927 | oldsave = PL_savestack_ix; |
2928 | SAVEDESTRUCTOR_X(S_clear_backtrack_stack, NULL); | |
2929 | SAVEVPTR(PL_regmatch_slab); | |
2930 | SAVEVPTR(PL_regmatch_state); | |
5d9a96ca DM |
2931 | |
2932 | /* grab next free state slot */ | |
2933 | st = ++PL_regmatch_state; | |
86545054 | 2934 | if (st > SLAB_LAST(PL_regmatch_slab)) |
5d9a96ca DM |
2935 | st = PL_regmatch_state = S_push_slab(aTHX); |
2936 | ||
d6a28714 JH |
2937 | /* Note that nextchr is a byte even in UTF */ |
2938 | nextchr = UCHARAT(locinput); | |
2939 | scan = prog; | |
2940 | while (scan != NULL) { | |
8ba1375e | 2941 | |
a3621e74 | 2942 | DEBUG_EXECUTE_r( { |
6136c704 | 2943 | SV * const prop = sv_newmortal(); |
1de06328 | 2944 | regnode *rnext=regnext(scan); |
786e8c11 | 2945 | DUMP_EXEC_POS( locinput, scan, do_utf8 ); |
32fc9b6a | 2946 | regprop(rex, prop, scan); |
07be1b83 YO |
2947 | |
2948 | PerlIO_printf(Perl_debug_log, | |
2949 | "%3"IVdf":%*s%s(%"IVdf")\n", | |
f8fc2ecf | 2950 | (IV)(scan - rexi->program), depth*2, "", |
07be1b83 | 2951 | SvPVX_const(prop), |
1de06328 | 2952 | (PL_regkind[OP(scan)] == END || !rnext) ? |
f8fc2ecf | 2953 | 0 : (IV)(rnext - rexi->program)); |
2a782b5b | 2954 | }); |
d6a28714 JH |
2955 | |
2956 | next = scan + NEXT_OFF(scan); | |
2957 | if (next == scan) | |
2958 | next = NULL; | |
40a82448 | 2959 | state_num = OP(scan); |
d6a28714 | 2960 | |
40a82448 | 2961 | reenter_switch: |
34a81e2b B |
2962 | |
2963 | assert(PL_reglastparen == &rex->lastparen); | |
2964 | assert(PL_reglastcloseparen == &rex->lastcloseparen); | |
2965 | assert(PL_regoffs == rex->offs); | |
2966 | ||
40a82448 | 2967 | switch (state_num) { |
d6a28714 | 2968 | case BOL: |
7fba1cd6 | 2969 | if (locinput == PL_bostr) |
d6a28714 | 2970 | { |
3b0527fe | 2971 | /* reginfo->till = reginfo->bol; */ |
b8c5462f JH |
2972 | break; |
2973 | } | |
d6a28714 JH |
2974 | sayNO; |
2975 | case MBOL: | |
12d33761 HS |
2976 | if (locinput == PL_bostr || |
2977 | ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n')) | |
d6a28714 | 2978 | { |
b8c5462f JH |
2979 | break; |
2980 | } | |
d6a28714 JH |
2981 | sayNO; |
2982 | case SBOL: | |
c2a73568 | 2983 | if (locinput == PL_bostr) |
b8c5462f | 2984 | break; |
d6a28714 JH |
2985 | sayNO; |
2986 | case GPOS: | |
3b0527fe | 2987 | if (locinput == reginfo->ganch) |
d6a28714 JH |
2988 | break; |
2989 | sayNO; | |
ee9b8eae YO |
2990 | |
2991 | case KEEPS: | |
2992 | /* update the startpoint */ | |
f0ab9afb | 2993 | st->u.keeper.val = PL_regoffs[0].start; |
ee9b8eae | 2994 | PL_reginput = locinput; |
f0ab9afb | 2995 | PL_regoffs[0].start = locinput - PL_bostr; |
ee9b8eae YO |
2996 | PUSH_STATE_GOTO(KEEPS_next, next); |
2997 | /*NOT-REACHED*/ | |
2998 | case KEEPS_next_fail: | |
2999 | /* rollback the start point change */ | |
f0ab9afb | 3000 | PL_regoffs[0].start = st->u.keeper.val; |
ee9b8eae YO |
3001 | sayNO_SILENT; |
3002 | /*NOT-REACHED*/ | |
d6a28714 | 3003 | case EOL: |
d6a28714 JH |
3004 | goto seol; |
3005 | case MEOL: | |
d6a28714 | 3006 | if ((nextchr || locinput < PL_regeol) && nextchr != '\n') |
b8c5462f | 3007 | sayNO; |
b8c5462f | 3008 | break; |
d6a28714 JH |
3009 | case SEOL: |
3010 | seol: | |
3011 | if ((nextchr || locinput < PL_regeol) && nextchr != '\n') | |
b8c5462f | 3012 | sayNO; |
d6a28714 | 3013 | if (PL_regeol - locinput > 1) |
b8c5462f | 3014 | sayNO; |
b8c5462f | 3015 | break; |
d6a28714 JH |
3016 | case EOS: |
3017 | if (PL_regeol != locinput) | |
b8c5462f | 3018 | sayNO; |
d6a28714 | 3019 | break; |
ffc61ed2 | 3020 | case SANY: |
d6a28714 | 3021 | if (!nextchr && locinput >= PL_regeol) |
4633a7c4 | 3022 | sayNO; |
f33976b4 DB |
3023 | if (do_utf8) { |
3024 | locinput += PL_utf8skip[nextchr]; | |
3025 | if (locinput > PL_regeol) | |
3026 | sayNO; | |
3027 | nextchr = UCHARAT(locinput); | |
3028 | } | |
3029 | else | |
3030 | nextchr = UCHARAT(++locinput); | |
3031 | break; | |
3032 | case CANY: | |
3033 | if (!nextchr && locinput >= PL_regeol) | |
3034 | sayNO; | |
b8c5462f | 3035 | nextchr = UCHARAT(++locinput); |
a0d0e21e | 3036 | break; |
ffc61ed2 | 3037 | case REG_ANY: |
1aa99e6b IH |
3038 | if ((!nextchr && locinput >= PL_regeol) || nextchr == '\n') |
3039 | sayNO; | |
3040 | if (do_utf8) { | |
b8c5462f | 3041 | locinput += PL_utf8skip[nextchr]; |
d6a28714 JH |
3042 | if (locinput > PL_regeol) |
3043 | sayNO; | |
a0ed51b3 | 3044 | nextchr = UCHARAT(locinput); |
a0ed51b3 | 3045 | } |
1aa99e6b IH |
3046 | else |
3047 | nextchr = UCHARAT(++locinput); | |
a0ed51b3 | 3048 | break; |
166ba7cd DM |
3049 | |
3050 | #undef ST | |
3051 | #define ST st->u.trie | |
786e8c11 YO |
3052 | case TRIEC: |
3053 | /* In this case the charclass data is available inline so | |
3054 | we can fail fast without a lot of extra overhead. | |
3055 | */ | |
3056 | if (scan->flags == EXACT || !do_utf8) { | |
3057 | if(!ANYOF_BITMAP_TEST(scan, *locinput)) { | |
3058 | DEBUG_EXECUTE_r( | |
3059 | PerlIO_printf(Perl_debug_log, | |
3060 | "%*s %sfailed to match trie start class...%s\n", | |
5bc10b2c | 3061 | REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5]) |
786e8c11 YO |
3062 | ); |
3063 | sayNO_SILENT; | |
3064 | /* NOTREACHED */ | |
3065 | } | |
3066 | } | |
3067 | /* FALL THROUGH */ | |
5b47454d | 3068 | case TRIE: |
2e64971a DM |
3069 | /* the basic plan of execution of the trie is: |
3070 | * At the beginning, run though all the states, and | |
3071 | * find the longest-matching word. Also remember the position | |
3072 | * of the shortest matching word. For example, this pattern: | |
3073 | * 1 2 3 4 5 | |
3074 | * ab|a|x|abcd|abc | |
3075 | * when matched against the string "abcde", will generate | |
3076 | * accept states for all words except 3, with the longest | |
3077 | * matching word being 4, and the shortest being 1 (with | |
3078 | * the position being after char 1 of the string). | |
3079 | * | |
3080 | * Then for each matching word, in word order (i.e. 1,2,4,5), | |
3081 | * we run the remainder of the pattern; on each try setting | |
3082 | * the current position to the character following the word, | |
3083 | * returning to try the next word on failure. | |
3084 | * | |
3085 | * We avoid having to build a list of words at runtime by | |
3086 | * using a compile-time structure, wordinfo[].prev, which | |
3087 | * gives, for each word, the previous accepting word (if any). | |
3088 | * In the case above it would contain the mappings 1->2, 2->0, | |
3089 | * 3->0, 4->5, 5->1. We can use this table to generate, from | |
3090 | * the longest word (4 above), a list of all words, by | |
3091 | * following the list of prev pointers; this gives us the | |
3092 | * unordered list 4,5,1,2. Then given the current word we have | |
3093 | * just tried, we can go through the list and find the | |
3094 | * next-biggest word to try (so if we just failed on word 2, | |
3095 | * the next in the list is 4). | |
3096 | * | |
3097 | * Since at runtime we don't record the matching position in | |
3098 | * the string for each word, we have to work that out for | |
3099 | * each word we're about to process. The wordinfo table holds | |
3100 | * the character length of each word; given that we recorded | |
3101 | * at the start: the position of the shortest word and its | |
3102 | * length in chars, we just need to move the pointer the | |
3103 | * difference between the two char lengths. Depending on | |
3104 | * Unicode status and folding, that's cheap or expensive. | |
3105 | * | |
3106 | * This algorithm is optimised for the case where are only a | |
3107 | * small number of accept states, i.e. 0,1, or maybe 2. | |
3108 | * With lots of accepts states, and having to try all of them, | |
3109 | * it becomes quadratic on number of accept states to find all | |
3110 | * the next words. | |
3111 | */ | |
3112 | ||
3dab1dad | 3113 | { |
07be1b83 | 3114 | /* what type of TRIE am I? (utf8 makes this contextual) */ |
a0a388a1 | 3115 | DECL_TRIE_TYPE(scan); |
3dab1dad YO |
3116 | |
3117 | /* what trie are we using right now */ | |
be8e71aa | 3118 | reg_trie_data * const trie |
f8fc2ecf | 3119 | = (reg_trie_data*)rexi->data->data[ ARG( scan ) ]; |
85fbaab2 | 3120 | HV * widecharmap = MUTABLE_HV(rexi->data->data[ ARG( scan ) + 1 ]); |
3dab1dad | 3121 | U32 state = trie->startstate; |
166ba7cd | 3122 | |
3dab1dad YO |
3123 | if (trie->bitmap && trie_type != trie_utf8_fold && |
3124 | !TRIE_BITMAP_TEST(trie,*locinput) | |
3125 | ) { | |
3126 | if (trie->states[ state ].wordnum) { | |
3127 | DEBUG_EXECUTE_r( | |
3128 | PerlIO_printf(Perl_debug_log, | |
3129 | "%*s %smatched empty string...%s\n", | |
5bc10b2c | 3130 | REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5]) |
3dab1dad YO |
3131 | ); |
3132 | break; | |
3133 | } else { | |
3134 | DEBUG_EXECUTE_r( | |
3135 | PerlIO_printf(Perl_debug_log, | |
786e8c11 | 3136 | "%*s %sfailed to match trie start class...%s\n", |
5bc10b2c | 3137 | REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5]) |
3dab1dad YO |
3138 | ); |
3139 | sayNO_SILENT; | |
3140 | } | |
3141 | } | |
166ba7cd | 3142 | |
786e8c11 YO |
3143 | { |
3144 | U8 *uc = ( U8* )locinput; | |
3145 | ||
3146 | STRLEN len = 0; | |
3147 | STRLEN foldlen = 0; | |
3148 | U8 *uscan = (U8*)NULL; | |
786e8c11 | 3149 | U8 foldbuf[ UTF8_MAXBYTES_CASE + 1 ]; |
2e64971a DM |
3150 | U32 charcount = 0; /* how many input chars we have matched */ |
3151 | U32 accepted = 0; /* have we seen any accepting states? */ | |
786e8c11 | 3152 | |
786e8c11 YO |
3153 | ST.B = next; |
3154 | ST.jump = trie->jump; | |
786e8c11 | 3155 | ST.me = scan; |
2e64971a DM |
3156 | ST.firstpos = NULL; |
3157 | ST.longfold = FALSE; /* char longer if folded => it's harder */ | |
3158 | ST.nextword = 0; | |
3159 | ||
3160 | /* fully traverse the TRIE; note the position of the | |
3161 | shortest accept state and the wordnum of the longest | |
3162 | accept state */ | |
07be1b83 | 3163 | |
a3621e74 | 3164 | while ( state && uc <= (U8*)PL_regeol ) { |
786e8c11 | 3165 | U32 base = trie->states[ state ].trans.base; |
f9f4320a | 3166 | UV uvc = 0; |
786e8c11 | 3167 | U16 charid; |
2e64971a DM |
3168 | U16 wordnum; |
3169 | wordnum = trie->states[ state ].wordnum; | |
3170 | ||
3171 | if (wordnum) { /* it's an accept state */ | |
3172 | if (!accepted) { | |
3173 | accepted = 1; | |
3174 | /* record first match position */ | |
3175 | if (ST.longfold) { | |
3176 | ST.firstpos = (U8*)locinput; | |
3177 | ST.firstchars = 0; | |
5b47454d | 3178 | } |
2e64971a DM |
3179 | else { |
3180 | ST.firstpos = uc; | |
3181 | ST.firstchars = charcount; | |
3182 | } | |
3183 | } | |
3184 | if (!ST.nextword || wordnum < ST.nextword) | |
3185 | ST.nextword = wordnum; | |
3186 | ST.topword = wordnum; | |
786e8c11 | 3187 | } |
a3621e74 | 3188 | |
07be1b83 | 3189 | DEBUG_TRIE_EXECUTE_r({ |
786e8c11 | 3190 | DUMP_EXEC_POS( (char *)uc, scan, do_utf8 ); |
a3621e74 | 3191 | PerlIO_printf( Perl_debug_log, |
2e64971a | 3192 | "%*s %sState: %4"UVxf" Accepted: %c ", |
5bc10b2c | 3193 | 2+depth * 2, "", PL_colors[4], |
2e64971a | 3194 | (UV)state, (accepted ? 'Y' : 'N')); |
07be1b83 | 3195 | }); |
a3621e74 | 3196 | |
2e64971a | 3197 | /* read a char and goto next state */ |
a3621e74 | 3198 | if ( base ) { |
6dd2be57 | 3199 | I32 offset; |
55eed653 NC |
3200 | REXEC_TRIE_READ_CHAR(trie_type, trie, widecharmap, uc, |
3201 | uscan, len, uvc, charid, foldlen, | |
3202 | foldbuf, uniflags); | |
2e64971a DM |
3203 | charcount++; |
3204 | if (foldlen>0) | |
3205 | ST.longfold = TRUE; | |
5b47454d | 3206 | if (charid && |
6dd2be57 DM |
3207 | ( ((offset = |
3208 | base + charid - 1 - trie->uniquecharcount)) >= 0) | |
3209 | ||
3210 | && ((U32)offset < trie->lasttrans) | |
3211 | && trie->trans[offset].check == state) | |
5b47454d | 3212 | { |
6dd2be57 | 3213 | state = trie->trans[offset].next; |
5b47454d DM |
3214 | } |
3215 | else { | |
3216 | state = 0; | |
3217 | } | |
3218 | uc += len; | |
3219 | ||
3220 | } | |
3221 | else { | |
a3621e74 YO |
3222 | state = 0; |
3223 | } | |
3224 | DEBUG_TRIE_EXECUTE_r( | |
e4584336 | 3225 | PerlIO_printf( Perl_debug_log, |
786e8c11 | 3226 | "Charid:%3x CP:%4"UVxf" After State: %4"UVxf"%s\n", |
e4584336 | 3227 | charid, uvc, (UV)state, PL_colors[5] ); |
a3621e74 YO |
3228 | ); |
3229 | } | |
2e64971a | 3230 | if (!accepted) |
a3621e74 | 3231 | sayNO; |
a3621e74 | 3232 | |
2e64971a DM |
3233 | /* calculate total number of accept states */ |
3234 | { | |
3235 | U16 w = ST.topword; | |
3236 | accepted = 0; | |
3237 | while (w) { | |
3238 | w = trie->wordinfo[w].prev; | |
3239 | accepted++; | |
3240 | } | |
3241 | ST.accepted = accepted; | |
3242 | } | |
3243 | ||
166ba7cd DM |
3244 | DEBUG_EXECUTE_r( |
3245 | PerlIO_printf( Perl_debug_log, | |
3246 | "%*s %sgot %"IVdf" possible matches%s\n", | |
5bc10b2c | 3247 | REPORT_CODE_OFF + depth * 2, "", |
166ba7cd DM |
3248 | PL_colors[4], (IV)ST.accepted, PL_colors[5] ); |
3249 | ); | |
2e64971a | 3250 | goto trie_first_try; /* jump into the fail handler */ |
786e8c11 | 3251 | }} |
fae667d5 | 3252 | /* NOTREACHED */ |
2e64971a DM |
3253 | |
3254 | case TRIE_next_fail: /* we failed - try next alternative */ | |
fae667d5 YO |
3255 | if ( ST.jump) { |
3256 | REGCP_UNWIND(ST.cp); | |
3257 | for (n = *PL_reglastparen; n > ST.lastparen; n--) | |
f0ab9afb | 3258 | PL_regoffs[n].end = -1; |
fae667d5 YO |
3259 | *PL_reglastparen = n; |
3260 | } | |
2e64971a DM |
3261 | if (!--ST.accepted) { |
3262 | DEBUG_EXECUTE_r({ | |
3263 | PerlIO_printf( Perl_debug_log, | |
3264 | "%*s %sTRIE failed...%s\n", | |
3265 | REPORT_CODE_OFF+depth*2, "", | |
3266 | PL_colors[4], | |
3267 | PL_colors[5] ); | |
3268 | }); | |
3269 | sayNO_SILENT; | |
3270 | } | |
3271 | { | |
3272 | /* Find next-highest word to process. Note that this code | |
3273 | * is O(N^2) per trie run (O(N) per branch), so keep tight */ | |
d9a396a3 DM |
3274 | register U16 min = 0; |
3275 | register U16 word; | |
2e64971a DM |
3276 | register U16 const nextword = ST.nextword; |
3277 | register reg_trie_wordinfo * const wordinfo | |
3278 | = ((reg_trie_data*)rexi->data->data[ARG(ST.me)])->wordinfo; | |
3279 | for (word=ST.topword; word; word=wordinfo[word].prev) { | |
3280 | if (word > nextword && (!min || word < min)) | |
3281 | min = word; | |
3282 | } | |
3283 | ST.nextword = min; | |
3284 | } | |
3285 | ||
fae667d5 | 3286 | trie_first_try: |
5d458dd8 YO |
3287 | if (do_cutgroup) { |
3288 | do_cutgroup = 0; | |
3289 | no_final = 0; | |
3290 | } | |
fae667d5 YO |
3291 | |
3292 | if ( ST.jump) { | |
3293 | ST.lastparen = *PL_reglastparen; | |
3294 | REGCP_SET(ST.cp); | |
2e64971a | 3295 | } |
a3621e74 | 3296 | |
2e64971a | 3297 | /* find start char of end of current word */ |
166ba7cd | 3298 | { |
2e64971a DM |
3299 | U32 chars; /* how many chars to skip */ |
3300 | U8 *uc = ST.firstpos; | |
3301 | reg_trie_data * const trie | |
3302 | = (reg_trie_data*)rexi->data->data[ARG(ST.me)]; | |
3303 | ||
3304 | assert((trie->wordinfo[ST.nextword].len - trie->prefixlen) | |
3305 | >= ST.firstchars); | |
3306 | chars = (trie->wordinfo[ST.nextword].len - trie->prefixlen) | |
3307 | - ST.firstchars; | |
3308 | ||
3309 | if (ST.longfold) { | |
3310 | /* the hard option - fold each char in turn and find | |
3311 | * its folded length (which may be different */ | |
3312 | U8 foldbuf[UTF8_MAXBYTES_CASE + 1]; | |
3313 | STRLEN foldlen; | |
3314 | STRLEN len; | |
d9a396a3 | 3315 | UV uvc; |
2e64971a DM |
3316 | U8 *uscan; |
3317 | ||
3318 | while (chars) { | |
3319 | if (do_utf8) { | |
3320 | uvc = utf8n_to_uvuni((U8*)uc, UTF8_MAXLEN, &len, | |
3321 | uniflags); | |
3322 | uc += len; | |
3323 | } | |
3324 | else { | |
3325 | uvc = *uc; | |
3326 | uc++; | |
3327 | } | |
3328 | uvc = to_uni_fold(uvc, foldbuf, &foldlen); | |
3329 | uscan = foldbuf; | |
3330 | while (foldlen) { | |
3331 | if (!--chars) | |
3332 | break; | |
3333 | uvc = utf8n_to_uvuni(uscan, UTF8_MAXLEN, &len, | |
3334 | uniflags); | |
3335 | uscan += len; | |
3336 | foldlen -= len; | |
3337 | } | |
3338 | } | |
a3621e74 | 3339 | } |
2e64971a DM |
3340 | else { |
3341 | if (do_utf8) | |
3342 | while (chars--) | |
3343 | uc += UTF8SKIP(uc); | |
3344 | else | |
3345 | uc += chars; | |
3346 | } | |
3347 | PL_reginput = (char *)uc; | |
3348 | } | |
166ba7cd | 3349 | |
2e64971a DM |
3350 | scan = (ST.jump && ST.jump[ST.nextword]) |
3351 | ? ST.me + ST.jump[ST.nextword] | |
3352 | : ST.B; | |
166ba7cd | 3353 | |
2e64971a DM |
3354 | DEBUG_EXECUTE_r({ |
3355 | PerlIO_printf( Perl_debug_log, | |
3356 | "%*s %sTRIE matched word #%d, continuing%s\n", | |
3357 | REPORT_CODE_OFF+depth*2, "", | |
3358 | PL_colors[4], | |
3359 | ST.nextword, | |
3360 | PL_colors[5] | |
3361 | ); | |
3362 | }); | |
3363 | ||
3364 | if (ST.accepted > 1 || has_cutgroup) { | |
3365 | PUSH_STATE_GOTO(TRIE_next, scan); | |
3366 | /* NOTREACHED */ | |
166ba7cd | 3367 | } |
2e64971a DM |
3368 | /* only one choice left - just continue */ |
3369 | DEBUG_EXECUTE_r({ | |
3370 | AV *const trie_words | |
3371 | = MUTABLE_AV(rexi->data->data[ARG(ST.me)+TRIE_WORDS_OFFSET]); | |
3372 | SV ** const tmp = av_fetch( trie_words, | |
3373 | ST.nextword-1, 0 ); | |
3374 | SV *sv= tmp ? sv_newmortal() : NULL; | |
3375 | ||
3376 | PerlIO_printf( Perl_debug_log, | |
3377 | "%*s %sonly one match left, short-circuiting: #%d <%s>%s\n", | |
3378 | REPORT_CODE_OFF+depth*2, "", PL_colors[4], | |
3379 | ST.nextword, | |
3380 | tmp ? pv_pretty(sv, SvPV_nolen_const(*tmp), SvCUR(*tmp), 0, | |
3381 | PL_colors[0], PL_colors[1], | |
3382 | (SvUTF8(*tmp) ? PERL_PV_ESCAPE_UNI : 0) | |
3383 | ) | |
3384 | : "not compiled under -Dr", | |
3385 | PL_colors[5] ); | |
3386 | }); | |
3387 | ||
3388 | locinput = PL_reginput; | |
3389 | nextchr = UCHARAT(locinput); | |
3390 | continue; /* execute rest of RE */ | |
166ba7cd | 3391 | /* NOTREACHED */ |
166ba7cd DM |
3392 | #undef ST |
3393 | ||
95b24440 DM |
3394 | case EXACT: { |
3395 | char *s = STRING(scan); | |
24d3c4a9 | 3396 | ln = STR_LEN(scan); |
eb160463 | 3397 | if (do_utf8 != UTF) { |
bc517b45 | 3398 | /* The target and the pattern have differing utf8ness. */ |
1aa99e6b | 3399 | char *l = locinput; |
24d3c4a9 | 3400 | const char * const e = s + ln; |
a72c7584 | 3401 | |
5ff6fc6d JH |
3402 | if (do_utf8) { |
3403 | /* The target is utf8, the pattern is not utf8. */ | |
1aa99e6b | 3404 | while (s < e) { |
a3b680e6 | 3405 | STRLEN ulen; |
1aa99e6b | 3406 | if (l >= PL_regeol) |
5ff6fc6d JH |
3407 | sayNO; |
3408 | if (NATIVE_TO_UNI(*(U8*)s) != | |
89ebb4a3 | 3409 | utf8n_to_uvuni((U8*)l, UTF8_MAXBYTES, &ulen, |
041457d9 | 3410 | uniflags)) |
5ff6fc6d | 3411 | sayNO; |
bc517b45 | 3412 | l += ulen; |
5ff6fc6d | 3413 | s ++; |
1aa99e6b | 3414 | } |
5ff6fc6d JH |
3415 | } |
3416 | else { | |
3417 | /* The target is not utf8, the pattern is utf8. */ | |
1aa99e6b | 3418 | while (s < e) { |
a3b680e6 | 3419 | STRLEN ulen; |
1aa99e6b IH |
3420 | if (l >= PL_regeol) |
3421 | sayNO; | |
5ff6fc6d | 3422 | if (NATIVE_TO_UNI(*((U8*)l)) != |
89ebb4a3 | 3423 | utf8n_to_uvuni((U8*)s, UTF8_MAXBYTES, &ulen, |
041457d9 | 3424 | uniflags)) |
1aa99e6b | 3425 | sayNO; |
bc517b45 | 3426 | s += ulen; |
a72c7584 | 3427 | l ++; |
1aa99e6b | 3428 | } |
5ff6fc6d | 3429 | } |
1aa99e6b IH |
3430 | locinput = l; |
3431 | nextchr = UCHARAT(locinput); | |
3432 | break; | |
3433 | } | |
bc517b45 | 3434 | /* The target and the pattern have the same utf8ness. */ |
d6a28714 JH |
3435 | /* Inline the first character, for speed. */ |
3436 | if (UCHARAT(s) != nextchr) | |
3437 | sayNO; | |
24d3c4a9 | 3438 | if (PL_regeol - locinput < ln) |
d6a28714 | 3439 | sayNO; |
24d3c4a9 | 3440 | if (ln > 1 && memNE(s, locinput, ln)) |
d6a28714 | 3441 | sayNO; |
24d3c4a9 | 3442 | locinput += ln; |
d6a28714 JH |
3443 | nextchr = UCHARAT(locinput); |
3444 | break; | |
95b24440 | 3445 | } |
d6a28714 | 3446 | case EXACTFL: |
b8c5462f JH |
3447 | PL_reg_flags |= RF_tainted; |
3448 | /* FALL THROUGH */ | |
95b24440 | 3449 | case EXACTF: { |
be8e71aa | 3450 | char * const s = STRING(scan); |
24d3c4a9 | 3451 | ln = STR_LEN(scan); |
d6a28714 | 3452 | |
d07ddd77 JH |
3453 | if (do_utf8 || UTF) { |
3454 | /* Either target or the pattern are utf8. */ | |
be8e71aa | 3455 | const char * const l = locinput; |
d07ddd77 | 3456 | char *e = PL_regeol; |
bc517b45 | 3457 | |
4c1b470c | 3458 | if (! foldEQ_utf8(s, 0, ln, cBOOL(UTF), |
1feea2c7 | 3459 | l, &e, 0, do_utf8)) { |
5486206c JH |
3460 | /* One more case for the sharp s: |
3461 | * pack("U0U*", 0xDF) =~ /ss/i, | |
3462 | * the 0xC3 0x9F are the UTF-8 | |
3463 | * byte sequence for the U+00DF. */ | |
e1d1eefb | 3464 | |
5486206c | 3465 | if (!(do_utf8 && |
e1d1eefb | 3466 | toLOWER(s[0]) == 's' && |
24d3c4a9 | 3467 | ln >= 2 && |
5486206c JH |
3468 | toLOWER(s[1]) == 's' && |
3469 | (U8)l[0] == 0xC3 && | |
3470 | e - l >= 2 && | |
3471 | (U8)l[1] == 0x9F)) | |
3472 | sayNO; | |
3473 | } | |
d07ddd77 JH |
3474 | locinput = e; |
3475 | nextchr = UCHARAT(locinput); | |
3476 | break; | |
a0ed51b3 | 3477 | } |
d6a28714 | 3478 | |
bc517b45 JH |
3479 | /* Neither the target and the pattern are utf8. */ |
3480 | ||
d6a28714 JH |
3481 | /* Inline the first character, for speed. */ |
3482 | if (UCHARAT(s) != nextchr && | |
3483 | UCHARAT(s) != ((OP(scan) == EXACTF) | |
3484 | ? PL_fold : PL_fold_locale)[nextchr]) | |
a0ed51b3 | 3485 | sayNO; |
24d3c4a9 | 3486 | if (PL_regeol - locinput < ln) |
b8c5462f | 3487 | sayNO; |
24d3c4a9 | 3488 | if (ln > 1 && (OP(scan) == EXACTF |
4c1b470c KW |
3489 | ? ! foldEQ(s, locinput, ln) |
3490 | : ! foldEQ_locale(s, locinput, ln))) | |
4633a7c4 | 3491 | sayNO; |
24d3c4a9 | 3492 | locinput += ln; |
d6a28714 | 3493 | nextchr = UCHARAT(locinput); |
a0d0e21e | 3494 | break; |
95b24440 | 3495 | } |
b2680017 YO |
3496 | case BOUNDL: |
3497 | case NBOUNDL: | |
3498 | PL_reg_flags |= RF_tainted; | |
3499 | /* FALL THROUGH */ | |
3500 | case BOUND: | |
3501 | case NBOUND: | |
3502 | /* was last char in word? */ | |
3503 | if (do_utf8) { | |
3504 | if (locinput == PL_bostr) | |
3505 | ln = '\n'; | |
3506 | else { | |
3507 | const U8 * const r = reghop3((U8*)locinput, -1, (U8*)PL_bostr); | |
3508 | ||
3509 | ln = utf8n_to_uvchr(r, UTF8SKIP(r), 0, uniflags); | |
3510 | } | |
3511 | if (OP(scan) == BOUND || OP(scan) == NBOUND) { | |
3512 | ln = isALNUM_uni(ln); | |
3513 | LOAD_UTF8_CHARCLASS_ALNUM(); | |
3514 | n = swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8); | |
3515 | } | |
3516 | else { | |
3517 | ln = isALNUM_LC_uvchr(UNI_TO_NATIVE(ln)); | |
3518 | n = isALNUM_LC_utf8((U8*)locinput); | |
3519 | } | |
3520 | } | |
3521 | else { | |
3522 | ln = (locinput != PL_bostr) ? | |
3523 | UCHARAT(locinput - 1) : '\n'; | |
3524 | if (OP(scan) == BOUND || OP(scan) == NBOUND) { | |
3525 | ln = isALNUM(ln); | |
3526 | n = isALNUM(nextchr); | |
3527 | } | |
3528 | else { | |
3529 | ln = isALNUM_LC(ln); | |
3530 | n = isALNUM_LC(nextchr); | |
3531 | } | |
3532 | } | |
3533 | if (((!ln) == (!n)) == (OP(scan) == BOUND || | |
3534 | OP(scan) == BOUNDL)) | |
3535 | sayNO; | |
3536 | break; | |
d6a28714 | 3537 | case ANYOF: |
ffc61ed2 | 3538 | if (do_utf8) { |
9e55ce06 JH |
3539 | STRLEN inclasslen = PL_regeol - locinput; |
3540 | ||
32fc9b6a | 3541 | if (!reginclass(rex, scan, (U8*)locinput, &inclasslen, do_utf8)) |
262b90c4 | 3542 | goto anyof_fail; |
ffc61ed2 JH |
3543 | if (locinput >= PL_regeol) |
3544 | sayNO; | |
0f0076b4 | 3545 | locinput += inclasslen ? inclasslen : UTF8SKIP(locinput); |
b8c5462f | 3546 | nextchr = UCHARAT(locinput); |
e0f9d4a8 | 3547 | break; |
ffc61ed2 JH |
3548 | } |
3549 | else { | |
3550 | if (nextchr < 0) | |
3551 | nextchr = UCHARAT(locinput); | |
32fc9b6a | 3552 | if (!REGINCLASS(rex, scan, (U8*)locinput)) |
262b90c4 | 3553 | goto anyof_fail; |
ffc61ed2 JH |
3554 | if (!nextchr && locinput >= PL_regeol) |
3555 | sayNO; | |
3556 | nextchr = UCHARAT(++locinput); | |
e0f9d4a8 JH |
3557 | break; |
3558 | } | |
262b90c4 | 3559 | anyof_fail: |
e0f9d4a8 JH |
3560 | /* If we might have the case of the German sharp s |
3561 | * in a casefolding Unicode character class. */ | |
3562 | ||
ebc501f0 JH |
3563 | if (ANYOF_FOLD_SHARP_S(scan, locinput, PL_regeol)) { |
3564 | locinput += SHARP_S_SKIP; | |
e0f9d4a8 | 3565 | nextchr = UCHARAT(locinput); |
ffc61ed2 | 3566 | } |
e0f9d4a8 JH |
3567 | else |
3568 | sayNO; | |
b8c5462f | 3569 | break; |
20d0b1e9 | 3570 | /* Special char classes - The defines start on line 129 or so */ |
d1eb3177 YO |
3571 | CCC_TRY_AFF( ALNUM, ALNUML, perl_word, "a", isALNUM_LC_utf8, isALNUM, isALNUM_LC); |
3572 | CCC_TRY_NEG(NALNUM, NALNUML, perl_word, "a", isALNUM_LC_utf8, isALNUM, isALNUM_LC); | |
20d0b1e9 | 3573 | |
d1eb3177 YO |
3574 | CCC_TRY_AFF( SPACE, SPACEL, perl_space, " ", isSPACE_LC_utf8, isSPACE, isSPACE_LC); |
3575 | CCC_TRY_NEG(NSPACE, NSPACEL, perl_space, " ", isSPACE_LC_utf8, isSPACE, isSPACE_LC); | |
20d0b1e9 | 3576 | |
d1eb3177 YO |
3577 | CCC_TRY_AFF( DIGIT, DIGITL, posix_digit, "0", isDIGIT_LC_utf8, isDIGIT, isDIGIT_LC); |
3578 | CCC_TRY_NEG(NDIGIT, NDIGITL, posix_digit, "0", isDIGIT_LC_utf8, isDIGIT, isDIGIT_LC); | |
20d0b1e9 | 3579 | |
37e2e78e KW |
3580 | case CLUMP: /* Match \X: logical Unicode character. This is defined as |
3581 | a Unicode extended Grapheme Cluster */ | |
3582 | /* From http://www.unicode.org/reports/tr29 (5.2 version). An | |
3583 | extended Grapheme Cluster is: | |
3584 | ||
3585 | CR LF | |
3586 | | Prepend* Begin Extend* | |
3587 | | . | |
3588 | ||
3589 | Begin is (Hangul-syllable | ! Control) | |
3590 | Extend is (Grapheme_Extend | Spacing_Mark) | |
3591 | Control is [ GCB_Control CR LF ] | |
3592 | ||
3593 | The discussion below shows how the code for CLUMP is derived | |
3594 | from this regex. Note that most of these concepts are from | |
3595 | property values of the Grapheme Cluster Boundary (GCB) property. | |
3596 | No code point can have multiple property values for a given | |
3597 | property. Thus a code point in Prepend can't be in Control, but | |
3598 | it must be in !Control. This is why Control above includes | |
3599 | GCB_Control plus CR plus LF. The latter two are used in the GCB | |
3600 | property separately, and so can't be in GCB_Control, even though | |
3601 | they logically are controls. Control is not the same as gc=cc, | |
3602 | but includes format and other characters as well. | |
3603 | ||
3604 | The Unicode definition of Hangul-syllable is: | |
3605 | L+ | |
3606 | | (L* ( ( V | LV ) V* | LVT ) T*) | |
3607 | | T+ | |
3608 | ) | |
3609 | Each of these is a value for the GCB property, and hence must be | |
3610 | disjoint, so the order they are tested is immaterial, so the | |
3611 | above can safely be changed to | |
3612 | T+ | |
3613 | | L+ | |
3614 | | (L* ( LVT | ( V | LV ) V*) T*) | |
3615 | ||
3616 | The last two terms can be combined like this: | |
3617 | L* ( L | |
3618 | | (( LVT | ( V | LV ) V*) T*)) | |
3619 | ||
3620 | And refactored into this: | |
3621 | L* (L | LVT T* | V V* T* | LV V* T*) | |
3622 | ||
3623 | That means that if we have seen any L's at all we can quit | |
3624 | there, but if the next character is a LVT, a V or and LV we | |
3625 | should keep going. | |
3626 | ||
3627 | There is a subtlety with Prepend* which showed up in testing. | |
3628 | Note that the Begin, and only the Begin is required in: | |
3629 | | Prepend* Begin Extend* | |
3630 | Also, Begin contains '! Control'. A Prepend must be a '! | |
3631 | Control', which means it must be a Begin. What it comes down to | |
3632 | is that if we match Prepend* and then find no suitable Begin | |
3633 | afterwards, that if we backtrack the last Prepend, that one will | |
3634 | be a suitable Begin. | |
3635 | */ | |
3636 | ||
b7c83a7e | 3637 | if (locinput >= PL_regeol) |
a0ed51b3 | 3638 | sayNO; |
37e2e78e KW |
3639 | if (! do_utf8) { |
3640 | ||
3641 | /* Match either CR LF or '.', as all the other possibilities | |
3642 | * require utf8 */ | |
3643 | locinput++; /* Match the . or CR */ | |
3644 | if (nextchr == '\r' | |
3645 | && locinput < PL_regeol | |
3646 | && UCHARAT(locinput) == '\n') locinput++; | |
3647 | } | |
3648 | else { | |
3649 | ||
3650 | /* Utf8: See if is ( CR LF ); already know that locinput < | |
3651 | * PL_regeol, so locinput+1 is in bounds */ | |
3652 | if (nextchr == '\r' && UCHARAT(locinput + 1) == '\n') { | |
3653 | locinput += 2; | |
3654 | } | |
3655 | else { | |
3656 | /* In case have to backtrack to beginning, then match '.' */ | |
3657 | char *starting = locinput; | |
3658 | ||
3659 | /* In case have to backtrack the last prepend */ | |
3660 | char *previous_prepend = 0; | |
3661 | ||
3662 | LOAD_UTF8_CHARCLASS_GCB(); | |
3663 | ||
3664 | /* Match (prepend)* */ | |
3665 | while (locinput < PL_regeol | |
3666 | && swash_fetch(PL_utf8_X_prepend, | |
3667 | (U8*)locinput, do_utf8)) | |
3668 | { | |
3669 | previous_prepend = locinput; | |
3670 | locinput += UTF8SKIP(locinput); | |
3671 | } | |
3672 | ||
3673 | /* As noted above, if we matched a prepend character, but | |
3674 | * the next thing won't match, back off the last prepend we | |
3675 | * matched, as it is guaranteed to match the begin */ | |
3676 | if (previous_prepend | |
3677 | && (locinput >= PL_regeol | |
3678 | || ! swash_fetch(PL_utf8_X_begin, | |
3679 | (U8*)locinput, do_utf8))) | |
3680 | { | |
3681 | locinput = previous_prepend; | |
3682 | } | |
3683 | ||
3684 | /* Note that here we know PL_regeol > locinput, as we | |
3685 | * tested that upon input to this switch case, and if we | |
3686 | * moved locinput forward, we tested the result just above | |
3687 | * and it either passed, or we backed off so that it will | |
3688 | * now pass */ | |
3689 | if (! swash_fetch(PL_utf8_X_begin, (U8*)locinput, do_utf8)) { | |
3690 | ||
3691 | /* Here did not match the required 'Begin' in the | |
3692 | * second term. So just match the very first | |
3693 | * character, the '.' of the final term of the regex */ | |
3694 | locinput = starting + UTF8SKIP(starting); | |
3695 | } else { | |
3696 | ||
3697 | /* Here is the beginning of a character that can have | |
3698 | * an extender. It is either a hangul syllable, or a | |
3699 | * non-control */ | |
3700 | if (swash_fetch(PL_utf8_X_non_hangul, | |
3701 | (U8*)locinput, do_utf8)) | |
3702 | { | |
3703 | ||
3704 | /* Here not a Hangul syllable, must be a | |
3705 | * ('! * Control') */ | |
3706 | locinput += UTF8SKIP(locinput); | |
3707 | } else { | |
3708 | ||
3709 | /* Here is a Hangul syllable. It can be composed | |
3710 | * of several individual characters. One | |
3711 | * possibility is T+ */ | |
3712 | if (swash_fetch(PL_utf8_X_T, | |
3713 | (U8*)locinput, do_utf8)) | |
3714 | { | |
3715 | while (locinput < PL_regeol | |
3716 | && swash_fetch(PL_utf8_X_T, | |
3717 | (U8*)locinput, do_utf8)) | |
3718 | { | |
3719 | locinput += UTF8SKIP(locinput); | |
3720 | } | |
3721 | } else { | |
3722 | ||
3723 | /* Here, not T+, but is a Hangul. That means | |
3724 | * it is one of the others: L, LV, LVT or V, | |
3725 | * and matches: | |
3726 | * L* (L | LVT T* | V V* T* | LV V* T*) */ | |
3727 | ||
3728 | /* Match L* */ | |
3729 | while (locinput < PL_regeol | |
3730 | && swash_fetch(PL_utf8_X_L, | |
3731 | (U8*)locinput, do_utf8)) | |
3732 | { | |
3733 | locinput += UTF8SKIP(locinput); | |
3734 | } | |
3735 | ||
3736 | /* Here, have exhausted L*. If the next | |
3737 | * character is not an LV, LVT nor V, it means | |
3738 | * we had to have at least one L, so matches L+ | |
3739 | * in the original equation, we have a complete | |
3740 | * hangul syllable. Are done. */ | |
3741 | ||
3742 | if (locinput < PL_regeol | |
3743 | && swash_fetch(PL_utf8_X_LV_LVT_V, | |
3744 | (U8*)locinput, do_utf8)) | |
3745 | { | |
3746 | ||
3747 | /* Otherwise keep going. Must be LV, LVT | |
3748 | * or V. See if LVT */ | |
3749 | if (swash_fetch(PL_utf8_X_LVT, | |
3750 | (U8*)locinput, do_utf8)) | |
3751 | { | |
3752 | locinput += UTF8SKIP(locinput); | |
3753 | } else { | |
3754 | ||
3755 | /* Must be V or LV. Take it, then | |
3756 | * match V* */ | |
3757 | locinput += UTF8SKIP(locinput); | |
3758 | while (locinput < PL_regeol | |
3759 | && swash_fetch(PL_utf8_X_V, | |
3760 | (U8*)locinput, do_utf8)) | |
3761 | { | |
3762 | locinput += UTF8SKIP(locinput); | |
3763 | } | |
3764 | } | |
3765 | ||
3766 | /* And any of LV, LVT, or V can be followed | |
3767 | * by T* */ | |
3768 | while (locinput < PL_regeol | |
3769 | && swash_fetch(PL_utf8_X_T, | |
3770 | (U8*)locinput, | |
3771 | do_utf8)) | |
3772 | { | |
3773 | locinput += UTF8SKIP(locinput); | |
3774 | } | |
3775 | } | |
3776 | } | |
3777 | } | |
3778 | ||
3779 | /* Match any extender */ | |
3780 | while (locinput < PL_regeol | |
3781 | && swash_fetch(PL_utf8_X_extend, | |
3782 | (U8*)locinput, do_utf8)) | |
3783 | { | |
3784 | locinput += UTF8SKIP(locinput); | |
3785 | } | |
3786 | } | |
3787 | } | |
3788 | if (locinput > PL_regeol) sayNO; | |
3789 | } | |
a0ed51b3 LW |
3790 | nextchr = UCHARAT(locinput); |
3791 | break; | |
81714fb9 YO |
3792 | |
3793 | case NREFFL: | |
3794 | { | |
3795 | char *s; | |
ff1157ca | 3796 | char type; |
81714fb9 YO |
3797 | PL_reg_flags |= RF_tainted; |
3798 | /* FALL THROUGH */ | |
3799 | case NREF: | |
3800 | case NREFF: | |
ff1157ca | 3801 | type = OP(scan); |
0a4db386 YO |
3802 | n = reg_check_named_buff_matched(rex,scan); |
3803 | ||
3804 | if ( n ) { | |
3805 | type = REF + ( type - NREF ); | |
3806 | goto do_ref; | |
3807 | } else { | |
81714fb9 | 3808 | sayNO; |
0a4db386 YO |
3809 | } |
3810 | /* unreached */ | |
c8756f30 | 3811 | case REFFL: |
3280af22 | 3812 | PL_reg_flags |= RF_tainted; |
c8756f30 | 3813 | /* FALL THROUGH */ |
c277df42 | 3814 | case REF: |
81714fb9 | 3815 | case REFF: |
c277df42 | 3816 | n = ARG(scan); /* which paren pair */ |
81714fb9 YO |
3817 | type = OP(scan); |
3818 | do_ref: | |
f0ab9afb | 3819 | ln = PL_regoffs[n].start; |
2c2d71f5 | 3820 | PL_reg_leftiter = PL_reg_maxiter; /* Void cache */ |
3b6647e0 | 3821 | if (*PL_reglastparen < n || ln == -1) |
af3f8c16 | 3822 | sayNO; /* Do not match unless seen CLOSEn. */ |
f0ab9afb | 3823 | if (ln == PL_regoffs[n].end) |
a0d0e21e | 3824 | break; |
a0ed51b3 | 3825 | |
24d3c4a9 | 3826 | s = PL_bostr + ln; |
81714fb9 | 3827 | if (do_utf8 && type != REF) { /* REF can do byte comparison */ |
a0ed51b3 | 3828 | char *l = locinput; |
f0ab9afb | 3829 | const char *e = PL_bostr + PL_regoffs[n].end; |
a0ed51b3 LW |
3830 | /* |
3831 | * Note that we can't do the "other character" lookup trick as | |
3832 | * in the 8-bit case (no pun intended) because in Unicode we | |
3833 | * have to map both upper and title case to lower case. | |
3834 | */ | |
81714fb9 | 3835 | if (type == REFF) { |
a0ed51b3 | 3836 | while (s < e) { |
a3b680e6 AL |
3837 | STRLEN ulen1, ulen2; |
3838 | U8 tmpbuf1[UTF8_MAXBYTES_CASE+1]; | |
3839 | U8 tmpbuf2[UTF8_MAXBYTES_CASE+1]; | |
3840 | ||
a0ed51b3 LW |
3841 | if (l >= PL_regeol) |
3842 | sayNO; | |
a2a2844f JH |
3843 | toLOWER_utf8((U8*)s, tmpbuf1, &ulen1); |
3844 | toLOWER_utf8((U8*)l, tmpbuf2, &ulen2); | |
7114a2d2 | 3845 | if (ulen1 != ulen2 || memNE((char *)tmpbuf1, (char *)tmpbuf2, ulen1)) |
a0ed51b3 | 3846 | sayNO; |
a2a2844f JH |
3847 | s += ulen1; |
3848 | l += ulen2; | |
a0ed51b3 LW |
3849 | } |
3850 | } | |
3851 | locinput = l; | |
3852 | nextchr = UCHARAT(locinput); | |
3853 | break; | |
3854 | } | |
3855 | ||
a0d0e21e | 3856 | /* Inline the first character, for speed. */ |
76e3520e | 3857 | if (UCHARAT(s) != nextchr && |
81714fb9 YO |
3858 | (type == REF || |
3859 | (UCHARAT(s) != (type == REFF | |
3860 | ? PL_fold : PL_fold_locale)[nextchr]))) | |
4633a7c4 | 3861 | sayNO; |
f0ab9afb | 3862 | ln = PL_regoffs[n].end - ln; |
24d3c4a9 | 3863 | if (locinput + ln > PL_regeol) |
4633a7c4 | 3864 | sayNO; |
81714fb9 | 3865 | if (ln > 1 && (type == REF |
24d3c4a9 | 3866 | ? memNE(s, locinput, ln) |
81714fb9 | 3867 | : (type == REFF |
4c1b470c KW |
3868 | ? ! foldEQ(s, locinput, ln) |
3869 | : ! foldEQ_locale(s, locinput, ln)))) | |
4633a7c4 | 3870 | sayNO; |
24d3c4a9 | 3871 | locinput += ln; |
76e3520e | 3872 | nextchr = UCHARAT(locinput); |
a0d0e21e | 3873 | break; |
81714fb9 | 3874 | } |
a0d0e21e | 3875 | case NOTHING: |
c277df42 | 3876 | case TAIL: |
a0d0e21e LW |
3877 | break; |
3878 | case BACK: | |
3879 | break; | |
40a82448 DM |
3880 | |
3881 | #undef ST | |
3882 | #define ST st->u.eval | |
c277df42 | 3883 | { |
c277df42 | 3884 | SV *ret; |
d2f13c59 | 3885 | REGEXP *re_sv; |
6bda09f9 | 3886 | regexp *re; |
f8fc2ecf | 3887 | regexp_internal *rei; |
1a147d38 YO |
3888 | regnode *startpoint; |
3889 | ||
3890 | case GOSTART: | |
e7707071 YO |
3891 | case GOSUB: /* /(...(?1))/ /(...(?&foo))/ */ |
3892 | if (cur_eval && cur_eval->locinput==locinput) { | |
24b23f37 | 3893 | if (cur_eval->u.eval.close_paren == (U32)ARG(scan)) |
1a147d38 | 3894 | Perl_croak(aTHX_ "Infinite recursion in regex"); |
4b196cd4 | 3895 | if ( ++nochange_depth > max_nochange_depth ) |
1a147d38 YO |
3896 | Perl_croak(aTHX_ |
3897 | "Pattern subroutine nesting without pos change" | |
3898 | " exceeded limit in regex"); | |
6bda09f9 YO |
3899 | } else { |
3900 | nochange_depth = 0; | |
1a147d38 | 3901 | } |
288b8c02 | 3902 | re_sv = rex_sv; |
6bda09f9 | 3903 | re = rex; |
f8fc2ecf | 3904 | rei = rexi; |
288b8c02 | 3905 | (void)ReREFCNT_inc(rex_sv); |
1a147d38 | 3906 | if (OP(scan)==GOSUB) { |
6bda09f9 YO |
3907 | startpoint = scan + ARG2L(scan); |
3908 | ST.close_paren = ARG(scan); | |
3909 | } else { | |
f8fc2ecf | 3910 | startpoint = rei->program+1; |
6bda09f9 YO |
3911 | ST.close_paren = 0; |
3912 | } | |
3913 | goto eval_recurse_doit; | |
3914 | /* NOTREACHED */ | |
3915 | case EVAL: /* /(?{A})B/ /(??{A})B/ and /(?(?{A})X|Y)B/ */ | |
3916 | if (cur_eval && cur_eval->locinput==locinput) { | |
4b196cd4 | 3917 | if ( ++nochange_depth > max_nochange_depth ) |
1a147d38 | 3918 | Perl_croak(aTHX_ "EVAL without pos change exceeded limit in regex"); |
6bda09f9 YO |
3919 | } else { |
3920 | nochange_depth = 0; | |
3921 | } | |
8e5e9ebe | 3922 | { |
4aabdb9b DM |
3923 | /* execute the code in the {...} */ |
3924 | dSP; | |
6136c704 | 3925 | SV ** const before = SP; |
4aabdb9b DM |
3926 | OP_4tree * const oop = PL_op; |
3927 | COP * const ocurcop = PL_curcop; | |
3928 | PAD *old_comppad; | |
d80618d2 | 3929 | char *saved_regeol = PL_regeol; |
4aabdb9b DM |
3930 | |
3931 | n = ARG(scan); | |
f8fc2ecf | 3932 | PL_op = (OP_4tree*)rexi->data->data[n]; |
24b23f37 YO |
3933 | DEBUG_STATE_r( PerlIO_printf(Perl_debug_log, |
3934 | " re_eval 0x%"UVxf"\n", PTR2UV(PL_op)) ); | |
f8fc2ecf | 3935 | PAD_SAVE_LOCAL(old_comppad, (PAD*)rexi->data->data[n + 2]); |
f0ab9afb | 3936 | PL_regoffs[0].end = PL_reg_magic->mg_len = locinput - PL_bostr; |
4aabdb9b | 3937 | |
2bf803e2 YO |
3938 | if (sv_yes_mark) { |
3939 | SV *sv_mrk = get_sv("REGMARK", 1); | |
3940 | sv_setsv(sv_mrk, sv_yes_mark); | |
3941 | } | |
3942 | ||
8e5e9ebe RGS |
3943 | CALLRUNOPS(aTHX); /* Scalar context. */ |
3944 | SPAGAIN; | |
3945 | if (SP == before) | |
075aa684 | 3946 | ret = &PL_sv_undef; /* protect against empty (?{}) blocks. */ |
8e5e9ebe RGS |
3947 | else { |
3948 | ret = POPs; | |
3949 | PUTBACK; | |
3950 | } | |
4aabdb9b DM |
3951 | |
3952 | PL_op = oop; | |
3953 | PAD_RESTORE_LOCAL(old_comppad); | |
3954 | PL_curcop = ocurcop; | |
d80618d2 | 3955 | PL_regeol = saved_regeol; |
24d3c4a9 | 3956 | if (!logical) { |
4aabdb9b DM |
3957 | /* /(?{...})/ */ |
3958 | sv_setsv(save_scalar(PL_replgv), ret); | |
4aabdb9b DM |
3959 | break; |
3960 | } | |
8e5e9ebe | 3961 | } |
24d3c4a9 DM |
3962 | if (logical == 2) { /* Postponed subexpression: /(??{...})/ */ |
3963 | logical = 0; | |
4aabdb9b | 3964 | { |
4f639d21 DM |
3965 | /* extract RE object from returned value; compiling if |
3966 | * necessary */ | |
6136c704 | 3967 | MAGIC *mg = NULL; |
288b8c02 | 3968 | REGEXP *rx = NULL; |
5c35adbb NC |
3969 | |
3970 | if (SvROK(ret)) { | |
288b8c02 | 3971 | SV *const sv = SvRV(ret); |
5c35adbb NC |
3972 | |
3973 | if (SvTYPE(sv) == SVt_REGEXP) { | |
d2f13c59 | 3974 | rx = (REGEXP*) sv; |
5c35adbb NC |
3975 | } else if (SvSMAGICAL(sv)) { |
3976 | mg = mg_find(sv, PERL_MAGIC_qr); | |
3977 | assert(mg); | |
3978 | } | |
3979 | } else if (SvTYPE(ret) == SVt_REGEXP) { | |
d2f13c59 | 3980 | rx = (REGEXP*) ret; |
5c35adbb | 3981 | } else if (SvSMAGICAL(ret)) { |
124ee91a NC |
3982 | if (SvGMAGICAL(ret)) { |
3983 | /* I don't believe that there is ever qr magic | |
3984 | here. */ | |
3985 | assert(!mg_find(ret, PERL_MAGIC_qr)); | |
faf82a0b | 3986 | sv_unmagic(ret, PERL_MAGIC_qr); |
124ee91a NC |
3987 | } |
3988 | else { | |
faf82a0b | 3989 | mg = mg_find(ret, PERL_MAGIC_qr); |
124ee91a NC |
3990 | /* testing suggests mg only ends up non-NULL for |
3991 | scalars who were upgraded and compiled in the | |
3992 | else block below. In turn, this is only | |
3993 | triggered in the "postponed utf8 string" tests | |
3994 | in t/op/pat.t */ | |
3995 | } | |
0f5d15d6 | 3996 | } |
faf82a0b | 3997 | |
0f5d15d6 | 3998 | if (mg) { |
d2f13c59 | 3999 | rx = (REGEXP *) mg->mg_obj; /*XXX:dmq*/ |
e2560c33 | 4000 | assert(rx); |
0f5d15d6 | 4001 | } |
288b8c02 | 4002 | if (rx) { |
f0826785 | 4003 | rx = reg_temp_copy(NULL, rx); |
288b8c02 | 4004 | } |
0f5d15d6 | 4005 | else { |
c737faaf | 4006 | U32 pm_flags = 0; |
a3b680e6 | 4007 | const I32 osize = PL_regsize; |
0f5d15d6 | 4008 | |
b9ad30b4 NC |
4009 | if (DO_UTF8(ret)) { |
4010 | assert (SvUTF8(ret)); | |
4011 | } else if (SvUTF8(ret)) { | |
4012 | /* Not doing UTF-8, despite what the SV says. Is | |
4013 | this only if we're trapped in use 'bytes'? */ | |
4014 | /* Make a copy of the octet sequence, but without | |
4015 | the flag on, as the compiler now honours the | |
4016 | SvUTF8 flag on ret. */ | |
4017 | STRLEN len; | |
4018 | const char *const p = SvPV(ret, len); | |
4019 | ret = newSVpvn_flags(p, len, SVs_TEMP); | |
4020 | } | |
288b8c02 | 4021 | rx = CALLREGCOMP(ret, pm_flags); |
9041c2e3 | 4022 | if (!(SvFLAGS(ret) |
faf82a0b | 4023 | & (SVs_TEMP | SVs_PADTMP | SVf_READONLY |
3ce3ed55 | 4024 | | SVs_GMG))) { |
a2794585 NC |
4025 | /* This isn't a first class regexp. Instead, it's |
4026 | caching a regexp onto an existing, Perl visible | |
4027 | scalar. */ | |
ad64d0ec | 4028 | sv_magic(ret, MUTABLE_SV(rx), PERL_MAGIC_qr, 0, 0); |
3ce3ed55 | 4029 | } |
0f5d15d6 | 4030 | PL_regsize = osize; |
0f5d15d6 | 4031 | } |
288b8c02 NC |
4032 | re_sv = rx; |
4033 | re = (struct regexp *)SvANY(rx); | |
4aabdb9b | 4034 | } |
07bc277f | 4035 | RXp_MATCH_COPIED_off(re); |
28d8d7f4 YO |
4036 | re->subbeg = rex->subbeg; |
4037 | re->sublen = rex->sublen; | |
f8fc2ecf | 4038 | rei = RXi_GET(re); |
6bda09f9 | 4039 | DEBUG_EXECUTE_r( |
efd26800 | 4040 | debug_start_match(re_sv, do_utf8, locinput, PL_regeol, |
6bda09f9 YO |
4041 | "Matching embedded"); |
4042 | ); | |
f8fc2ecf | 4043 | startpoint = rei->program + 1; |
1a147d38 | 4044 | ST.close_paren = 0; /* only used for GOSUB */ |
6bda09f9 YO |
4045 | /* borrowed from regtry */ |
4046 | if (PL_reg_start_tmpl <= re->nparens) { | |
4047 | PL_reg_start_tmpl = re->nparens*3/2 + 3; | |
4048 | if(PL_reg_start_tmp) | |
4049 | Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*); | |
4050 | else | |
4051 | Newx(PL_reg_start_tmp, PL_reg_start_tmpl, char*); | |
dd5def09 | 4052 | } |
aa283a38 | 4053 | |
1a147d38 | 4054 | eval_recurse_doit: /* Share code with GOSUB below this line */ |
aa283a38 | 4055 | /* run the pattern returned from (??{...}) */ |
40a82448 DM |
4056 | ST.cp = regcppush(0); /* Save *all* the positions. */ |
4057 | REGCP_SET(ST.lastcp); | |
6bda09f9 | 4058 | |
f0ab9afb | 4059 | PL_regoffs = re->offs; /* essentially NOOP on GOSUB */ |
6bda09f9 | 4060 | |
0357f1fd ML |
4061 | /* see regtry, specifically PL_reglast(?:close)?paren is a pointer! (i dont know why) :dmq */ |
4062 | PL_reglastparen = &re->lastparen; | |
4063 | PL_reglastcloseparen = &re->lastcloseparen; | |
4064 | re->lastparen = 0; | |
4065 | re->lastcloseparen = 0; | |
4066 | ||
4aabdb9b | 4067 | PL_reginput = locinput; |
ae0beba1 | 4068 | PL_regsize = 0; |
4aabdb9b DM |
4069 | |
4070 | /* XXXX This is too dramatic a measure... */ | |
4071 | PL_reg_maxiter = 0; | |
4072 | ||
faec1544 | 4073 | ST.toggle_reg_flags = PL_reg_flags; |
3c8556c3 | 4074 | if (RX_UTF8(re_sv)) |
faec1544 DM |
4075 | PL_reg_flags |= RF_utf8; |
4076 | else | |
4077 | PL_reg_flags &= ~RF_utf8; | |
4078 | ST.toggle_reg_flags ^= PL_reg_flags; /* diff of old and new */ | |
4079 | ||
288b8c02 | 4080 | ST.prev_rex = rex_sv; |
faec1544 | 4081 | ST.prev_curlyx = cur_curlyx; |
288b8c02 NC |
4082 | SETREX(rex_sv,re_sv); |
4083 | rex = re; | |
f8fc2ecf | 4084 | rexi = rei; |
faec1544 | 4085 | cur_curlyx = NULL; |
40a82448 | 4086 | ST.B = next; |
faec1544 DM |
4087 | ST.prev_eval = cur_eval; |
4088 | cur_eval = st; | |
faec1544 | 4089 | /* now continue from first node in postoned RE */ |
6bda09f9 | 4090 | PUSH_YES_STATE_GOTO(EVAL_AB, startpoint); |
4aabdb9b | 4091 | /* NOTREACHED */ |
a0ed51b3 | 4092 | } |
24d3c4a9 | 4093 | /* logical is 1, /(?(?{...})X|Y)/ */ |
f2338a2e | 4094 | sw = cBOOL(SvTRUE(ret)); |
24d3c4a9 | 4095 | logical = 0; |
c277df42 IZ |
4096 | break; |
4097 | } | |
40a82448 | 4098 | |
faec1544 DM |
4099 | case EVAL_AB: /* cleanup after a successful (??{A})B */ |
4100 | /* note: this is called twice; first after popping B, then A */ | |
4101 | PL_reg_flags ^= ST.toggle_reg_flags; | |
288b8c02 NC |
4102 | ReREFCNT_dec(rex_sv); |
4103 | SETREX(rex_sv,ST.prev_rex); | |
4104 | rex = (struct regexp *)SvANY(rex_sv); | |
f8fc2ecf | 4105 | rexi = RXi_GET(rex); |
faec1544 DM |
4106 | regcpblow(ST.cp); |
4107 | cur_eval = ST.prev_eval; | |
4108 | cur_curlyx = ST.prev_curlyx; | |
34a81e2b B |
4109 | |
4110 | /* rex was changed so update the pointer in PL_reglastparen and PL_reglastcloseparen */ | |
0357f1fd ML |
4111 | PL_reglastparen = &rex->lastparen; |
4112 | PL_reglastcloseparen = &rex->lastcloseparen; | |
34a81e2b B |
4113 | /* also update PL_regoffs */ |
4114 | PL_regoffs = rex->offs; | |
0357f1fd | 4115 | |
40a82448 DM |
4116 | /* XXXX This is too dramatic a measure... */ |
4117 | PL_reg_maxiter = 0; | |
e7707071 | 4118 | if ( nochange_depth ) |
4b196cd4 | 4119 | nochange_depth--; |
262b90c4 | 4120 | sayYES; |
40a82448 | 4121 | |
40a82448 | 4122 | |
faec1544 DM |
4123 | case EVAL_AB_fail: /* unsuccessfully ran A or B in (??{A})B */ |
4124 | /* note: this is called twice; first after popping B, then A */ | |
4125 | PL_reg_flags ^= ST.toggle_reg_flags; | |
288b8c02 NC |
4126 | ReREFCNT_dec(rex_sv); |
4127 | SETREX(rex_sv,ST.prev_rex); | |
4128 | rex = (struct regexp *)SvANY(rex_sv); | |
f8fc2ecf | 4129 | rexi = RXi_GET(rex); |
34a81e2b | 4130 | /* rex was changed so update the pointer in PL_reglastparen and PL_reglastcloseparen */ |
0357f1fd ML |
4131 | PL_reglastparen = &rex->lastparen; |
4132 | PL_reglastcloseparen = &rex->lastcloseparen; | |
4133 | ||
40a82448 DM |
4134 | PL_reginput = locinput; |
4135 | REGCP_UNWIND(ST.lastcp); | |
4136 | regcppop(rex); | |
faec1544 DM |
4137 | cur_eval = ST.prev_eval; |
4138 | cur_curlyx = ST.prev_curlyx; | |
4139 | /* XXXX This is too dramatic a measure... */ | |
4140 | PL_reg_maxiter = 0; | |
e7707071 | 4141 | if ( nochange_depth ) |
4b196cd4 | 4142 | nochange_depth--; |
40a82448 | 4143 | sayNO_SILENT; |
40a82448 DM |
4144 | #undef ST |
4145 | ||
a0d0e21e | 4146 | case OPEN: |
c277df42 | 4147 | n = ARG(scan); /* which paren pair */ |
3280af22 NIS |
4148 | PL_reg_start_tmp[n] = locinput; |
4149 | if (n > PL_regsize) | |
4150 | PL_regsize = n; | |
e2e6a0f1 | 4151 | lastopen = n; |
a0d0e21e LW |
4152 | break; |
4153 | case CLOSE: | |
c277df42 | 4154 | n = ARG(scan); /* which paren pair */ |
f0ab9afb NC |
4155 | PL_regoffs[n].start = PL_reg_start_tmp[n] - PL_bostr; |
4156 | PL_regoffs[n].end = locinput - PL_bostr; | |
7f69552c YO |
4157 | /*if (n > PL_regsize) |
4158 | PL_regsize = n;*/ | |
3b6647e0 | 4159 | if (n > *PL_reglastparen) |
3280af22 | 4160 | *PL_reglastparen = n; |
a01268b5 | 4161 | *PL_reglastcloseparen = n; |
3b6647e0 | 4162 | if (cur_eval && cur_eval->u.eval.close_paren == n) { |
6bda09f9 YO |
4163 | goto fake_end; |
4164 | } | |
a0d0e21e | 4165 | break; |
e2e6a0f1 YO |
4166 | case ACCEPT: |
4167 | if (ARG(scan)){ | |
4168 | regnode *cursor; | |
4169 | for (cursor=scan; | |
4170 | cursor && OP(cursor)!=END; | |
4171 | cursor=regnext(cursor)) | |
4172 | { | |
4173 | if ( OP(cursor)==CLOSE ){ | |
4174 | n = ARG(cursor); | |
4175 | if ( n <= lastopen ) { | |
f0ab9afb NC |
4176 | PL_regoffs[n].start |
4177 | = PL_reg_start_tmp[n] - PL_bostr; | |
4178 | PL_regoffs[n].end = locinput - PL_bostr; | |
e2e6a0f1 YO |
4179 | /*if (n > PL_regsize) |
4180 | PL_regsize = n;*/ | |
3b6647e0 | 4181 | if (n > *PL_reglastparen) |
e2e6a0f1 YO |
4182 | *PL_reglastparen = n; |
4183 | *PL_reglastcloseparen = n; | |
3b6647e0 RB |
4184 | if ( n == ARG(scan) || (cur_eval && |
4185 | cur_eval->u.eval.close_paren == n)) | |
e2e6a0f1 YO |
4186 | break; |
4187 | } | |
4188 | } | |
4189 | } | |
4190 | } | |
4191 | goto fake_end; | |
4192 | /*NOTREACHED*/ | |
c277df42 IZ |
4193 | case GROUPP: |
4194 | n = ARG(scan); /* which paren pair */ | |
f2338a2e | 4195 | sw = cBOOL(*PL_reglastparen >= n && PL_regoffs[n].end != -1); |
c277df42 | 4196 | break; |
0a4db386 YO |
4197 | case NGROUPP: |
4198 | /* reg_check_named_buff_matched returns 0 for no match */ | |
f2338a2e | 4199 | sw = cBOOL(0 < reg_check_named_buff_matched(rex,scan)); |
0a4db386 | 4200 | break; |
1a147d38 | 4201 | case INSUBP: |
0a4db386 | 4202 | n = ARG(scan); |
3b6647e0 | 4203 | sw = (cur_eval && (!n || cur_eval->u.eval.close_paren == n)); |
0a4db386 YO |
4204 | break; |
4205 | case DEFINEP: | |
4206 | sw = 0; | |
4207 | break; | |
c277df42 | 4208 | case IFTHEN: |
2c2d71f5 | 4209 | PL_reg_leftiter = PL_reg_maxiter; /* Void cache */ |
24d3c4a9 | 4210 | if (sw) |
c277df42 IZ |
4211 | next = NEXTOPER(NEXTOPER(scan)); |
4212 | else { | |
4213 | next = scan + ARG(scan); | |
4214 | if (OP(next) == IFTHEN) /* Fake one. */ | |
4215 | next = NEXTOPER(NEXTOPER(next)); | |
4216 | } | |
4217 | break; | |
4218 | case LOGICAL: | |
24d3c4a9 | 4219 | logical = scan->flags; |
c277df42 | 4220 | break; |
c476f425 | 4221 | |
2ab05381 | 4222 | /******************************************************************* |
2ab05381 | 4223 | |
c476f425 DM |
4224 | The CURLYX/WHILEM pair of ops handle the most generic case of the /A*B/ |
4225 | pattern, where A and B are subpatterns. (For simple A, CURLYM or | |
4226 | STAR/PLUS/CURLY/CURLYN are used instead.) | |
2ab05381 | 4227 | |
c476f425 | 4228 | A*B is compiled as <CURLYX><A><WHILEM><B> |
2ab05381 | 4229 | |
c476f425 DM |
4230 | On entry to the subpattern, CURLYX is called. This pushes a CURLYX |
4231 | state, which contains the current count, initialised to -1. It also sets | |
4232 | cur_curlyx to point to this state, with any previous value saved in the | |
4233 | state block. | |
2ab05381 | 4234 | |
c476f425 DM |
4235 | CURLYX then jumps straight to the WHILEM op, rather than executing A, |
4236 | since the pattern may possibly match zero times (i.e. it's a while {} loop | |
4237 | rather than a do {} while loop). | |
2ab05381 | 4238 | |
c476f425 DM |
4239 | Each entry to WHILEM represents a successful match of A. The count in the |
4240 | CURLYX block is incremented, another WHILEM state is pushed, and execution | |
4241 | passes to A or B depending on greediness and the current count. | |
2ab05381 | 4242 | |
c476f425 DM |
4243 | For example, if matching against the string a1a2a3b (where the aN are |
4244 | substrings that match /A/), then the match progresses as follows: (the | |
4245 | pushed states are interspersed with the bits of strings matched so far): | |
2ab05381 | 4246 | |
c476f425 DM |
4247 | <CURLYX cnt=-1> |
4248 | <CURLYX cnt=0><WHILEM> | |
4249 | <CURLYX cnt=1><WHILEM> a1 <WHILEM> | |
4250 | <CURLYX cnt=2><WHILEM> a1 <WHILEM> a2 <WHILEM> | |
4251 | <CURLYX cnt=3><WHILEM> a1 <WHILEM> a2 <WHILEM> a3 <WHILEM> | |
4252 | <CURLYX cnt=3><WHILEM> a1 <WHILEM> a2 <WHILEM> a3 <WHILEM> b | |
2ab05381 | 4253 | |
c476f425 DM |
4254 | (Contrast this with something like CURLYM, which maintains only a single |
4255 | backtrack state: | |
2ab05381 | 4256 | |
c476f425 DM |
4257 | <CURLYM cnt=0> a1 |
4258 | a1 <CURLYM cnt=1> a2 | |
4259 | a1 a2 <CURLYM cnt=2> a3 | |
4260 | a1 a2 a3 <CURLYM cnt=3> b | |
4261 | ) | |
2ab05381 | 4262 | |
c476f425 DM |
4263 | Each WHILEM state block marks a point to backtrack to upon partial failure |
4264 | of A or B, and also contains some minor state data related to that | |
4265 | iteration. The CURLYX block, pointed to by cur_curlyx, contains the | |
4266 | overall state, such as the count, and pointers to the A and B ops. | |
2ab05381 | 4267 | |
c476f425 DM |
4268 | This is complicated slightly by nested CURLYX/WHILEM's. Since cur_curlyx |
4269 | must always point to the *current* CURLYX block, the rules are: | |
2ab05381 | 4270 | |
c476f425 DM |
4271 | When executing CURLYX, save the old cur_curlyx in the CURLYX state block, |
4272 | and set cur_curlyx to point the new block. | |
2ab05381 | 4273 | |
c476f425 DM |
4274 | When popping the CURLYX block after a successful or unsuccessful match, |
4275 | restore the previous cur_curlyx. | |
2ab05381 | 4276 | |
c476f425 DM |
4277 | When WHILEM is about to execute B, save the current cur_curlyx, and set it |
4278 | to the outer one saved in the CURLYX block. | |
2ab05381 | 4279 | |
c476f425 DM |
4280 | When popping the WHILEM block after a successful or unsuccessful B match, |
4281 | restore the previous cur_curlyx. | |
2ab05381 | 4282 | |
c476f425 DM |
4283 | Here's an example for the pattern (AI* BI)*BO |
4284 | I and O refer to inner and outer, C and W refer to CURLYX and WHILEM: | |
2ab05381 | 4285 | |
c476f425 DM |
4286 | cur_ |
4287 | curlyx backtrack stack | |
4288 | ------ --------------- | |
4289 | NULL | |
4290 | CO <CO prev=NULL> <WO> | |
4291 | CI <CO prev=NULL> <WO> <CI prev=CO> <WI> ai | |
4292 | CO <CO prev=NULL> <WO> <CI prev=CO> <WI> ai <WI prev=CI> bi | |
4293 | NULL <CO prev=NULL> <WO> <CI prev=CO> <WI> ai <WI prev=CI> bi <WO prev=CO> bo | |
2ab05381 | 4294 | |
c476f425 DM |
4295 | At this point the pattern succeeds, and we work back down the stack to |
4296 | clean up, restoring as we go: | |
95b24440 | 4297 | |
c476f425 DM |
4298 | CO <CO prev=NULL> <WO> <CI prev=CO> <WI> ai <WI prev=CI> bi |
4299 | CI <CO prev=NULL> <WO> <CI prev=CO> <WI> ai | |
4300 | CO <CO prev=NULL> <WO> | |
4301 | NULL | |
a0374537 | 4302 | |
c476f425 DM |
4303 | *******************************************************************/ |
4304 | ||
4305 | #define ST st->u.curlyx | |
4306 | ||
4307 | case CURLYX: /* start of /A*B/ (for complex A) */ | |
4308 | { | |
4309 | /* No need to save/restore up to this paren */ | |
4310 | I32 parenfloor = scan->flags; | |
4311 | ||
4312 | assert(next); /* keep Coverity happy */ | |
4313 | if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */ | |
4314 | next += ARG(next); | |
4315 | ||
4316 | /* XXXX Probably it is better to teach regpush to support | |
4317 | parenfloor > PL_regsize... */ | |
4318 | if (parenfloor > (I32)*PL_reglastparen) | |
4319 | parenfloor = *PL_reglastparen; /* Pessimization... */ | |
4320 | ||
4321 | ST.prev_curlyx= cur_curlyx; | |
4322 | cur_curlyx = st; | |
4323 | ST.cp = PL_savestack_ix; | |
4324 | ||
4325 | /* these fields contain the state of the current curly. | |
4326 | * they are accessed by subsequent WHILEMs */ | |
4327 | ST.parenfloor = parenfloor; | |
d02d6d97 | 4328 | ST.me = scan; |
c476f425 | 4329 | ST.B = next; |
24d3c4a9 DM |
4330 | ST.minmod = minmod; |
4331 | minmod = 0; | |
c476f425 DM |
4332 | ST.count = -1; /* this will be updated by WHILEM */ |
4333 | ST.lastloc = NULL; /* this will be updated by WHILEM */ | |
4334 | ||
4335 | PL_reginput = locinput; | |
4336 | PUSH_YES_STATE_GOTO(CURLYX_end, PREVOPER(next)); | |
5f66b61c | 4337 | /* NOTREACHED */ |
c476f425 | 4338 | } |
a0d0e21e | 4339 | |
c476f425 | 4340 | case CURLYX_end: /* just finished matching all of A*B */ |
c476f425 DM |
4341 | cur_curlyx = ST.prev_curlyx; |
4342 | sayYES; | |
4343 | /* NOTREACHED */ | |
a0d0e21e | 4344 | |
c476f425 DM |
4345 | case CURLYX_end_fail: /* just failed to match all of A*B */ |
4346 | regcpblow(ST.cp); | |
4347 | cur_curlyx = ST.prev_curlyx; | |
4348 | sayNO; | |
4349 | /* NOTREACHED */ | |
4633a7c4 | 4350 | |
a0d0e21e | 4351 | |
c476f425 DM |
4352 | #undef ST |
4353 | #define ST st->u.whilem | |
4354 | ||
4355 | case WHILEM: /* just matched an A in /A*B/ (for complex A) */ | |
4356 | { | |
4357 | /* see the discussion above about CURLYX/WHILEM */ | |
c476f425 | 4358 | I32 n; |
d02d6d97 DM |
4359 | int min = ARG1(cur_curlyx->u.curlyx.me); |
4360 | int max = ARG2(cur_curlyx->u.curlyx.me); | |
4361 | regnode *A = NEXTOPER(cur_curlyx->u.curlyx.me) + EXTRA_STEP_2ARGS; | |
4362 | ||
c476f425 DM |
4363 | assert(cur_curlyx); /* keep Coverity happy */ |
4364 | n = ++cur_curlyx->u.curlyx.count; /* how many A's matched */ | |
4365 | ST.save_lastloc = cur_curlyx->u.curlyx.lastloc; | |
4366 | ST.cache_offset = 0; | |
4367 | ST.cache_mask = 0; | |
4368 | ||
4369 | PL_reginput = locinput; | |
4370 | ||
4371 | DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log, | |
d02d6d97 DM |
4372 | "%*s whilem: matched %ld out of %d..%d\n", |
4373 | REPORT_CODE_OFF+depth*2, "", (long)n, min, max) | |
c476f425 | 4374 | ); |
a0d0e21e | 4375 | |
c476f425 | 4376 | /* First just match a string of min A's. */ |
a0d0e21e | 4377 | |
d02d6d97 | 4378 | if (n < min) { |
c476f425 | 4379 | cur_curlyx->u.curlyx.lastloc = locinput; |
d02d6d97 | 4380 | PUSH_STATE_GOTO(WHILEM_A_pre, A); |
c476f425 DM |
4381 | /* NOTREACHED */ |
4382 | } | |
4383 | ||
4384 | /* If degenerate A matches "", assume A done. */ | |
4385 | ||
4386 | if (locinput == cur_curlyx->u.curlyx.lastloc) { | |
4387 | DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log, | |
4388 | "%*s whilem: empty match detected, trying continuation...\n", | |
4389 | REPORT_CODE_OFF+depth*2, "") | |
4390 | ); | |
4391 | goto do_whilem_B_max; | |
4392 | } | |
4393 | ||
4394 | /* super-linear cache processing */ | |
4395 | ||
4396 | if (scan->flags) { | |
a0d0e21e | 4397 | |
2c2d71f5 | 4398 | if (!PL_reg_maxiter) { |
c476f425 DM |
4399 | /* start the countdown: Postpone detection until we |
4400 | * know the match is not *that* much linear. */ | |
2c2d71f5 | 4401 | PL_reg_maxiter = (PL_regeol - PL_bostr + 1) * (scan->flags>>4); |
66bf836d DM |
4402 | /* possible overflow for long strings and many CURLYX's */ |
4403 | if (PL_reg_maxiter < 0) | |
4404 | PL_reg_maxiter = I32_MAX; | |
2c2d71f5 JH |
4405 | PL_reg_leftiter = PL_reg_maxiter; |
4406 | } | |
c476f425 | 4407 | |
2c2d71f5 | 4408 | if (PL_reg_leftiter-- == 0) { |
c476f425 | 4409 | /* initialise cache */ |
3298f257 | 4410 | const I32 size = (PL_reg_maxiter + 7)/8; |
2c2d71f5 | 4411 | if (PL_reg_poscache) { |
eb160463 | 4412 | if ((I32)PL_reg_poscache_size < size) { |
2c2d71f5 JH |
4413 | Renew(PL_reg_poscache, size, char); |
4414 | PL_reg_poscache_size = size; | |
4415 | } | |
4416 | Zero(PL_reg_poscache, size, char); | |
4417 | } | |
4418 | else { | |
4419 | PL_reg_poscache_size = size; | |
a02a5408 | 4420 | Newxz(PL_reg_poscache, size, char); |
2c2d71f5 | 4421 | } |
c476f425 DM |
4422 | DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log, |
4423 | "%swhilem: Detected a super-linear match, switching on caching%s...\n", | |
4424 | PL_colors[4], PL_colors[5]) | |
4425 | ); | |
2c2d71f5 | 4426 | } |
c476f425 | 4427 | |
2c2d71f5 | 4428 | if (PL_reg_leftiter < 0) { |
c476f425 DM |
4429 | /* have we already failed at this position? */ |
4430 | I32 offset, mask; | |
4431 | offset = (scan->flags & 0xf) - 1 | |
4432 | + (locinput - PL_bostr) * (scan->flags>>4); | |
4433 | mask = 1 << (offset % 8); | |
4434 | offset /= 8; | |
4435 | if (PL_reg_poscache[offset] & mask) { | |
4436 | DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log, | |
4437 | "%*s whilem: (cache) already tried at this position...\n", | |
4438 | REPORT_CODE_OFF+depth*2, "") | |
2c2d71f5 | 4439 | ); |
3298f257 | 4440 | sayNO; /* cache records failure */ |
2c2d71f5 | 4441 | } |
c476f425 DM |
4442 | ST.cache_offset = offset; |
4443 | ST.cache_mask = mask; | |
2c2d71f5 | 4444 | } |
c476f425 | 4445 | } |
2c2d71f5 | 4446 | |
c476f425 | 4447 | /* Prefer B over A for minimal matching. */ |
a687059c | 4448 | |
c476f425 DM |
4449 | if (cur_curlyx->u.curlyx.minmod) { |
4450 | ST.save_curlyx = cur_curlyx; | |
4451 | cur_curlyx = cur_curlyx->u.curlyx.prev_curlyx; | |
4452 | ST.cp = regcppush(ST.save_curlyx->u.curlyx.parenfloor); | |
4453 | REGCP_SET(ST.lastcp); | |
4454 | PUSH_YES_STATE_GOTO(WHILEM_B_min, ST.save_curlyx->u.curlyx.B); | |
4455 | /* NOTREACHED */ | |
4456 | } | |
a0d0e21e | 4457 | |
c476f425 DM |
4458 | /* Prefer A over B for maximal matching. */ |
4459 | ||
d02d6d97 | 4460 | if (n < max) { /* More greed allowed? */ |
c476f425 DM |
4461 | ST.cp = regcppush(cur_curlyx->u.curlyx.parenfloor); |
4462 | cur_curlyx->u.curlyx.lastloc = locinput; | |
4463 | REGCP_SET(ST.lastcp); | |
d02d6d97 | 4464 | PUSH_STATE_GOTO(WHILEM_A_max, A); |
c476f425 DM |
4465 | /* NOTREACHED */ |
4466 | } | |
4467 | goto do_whilem_B_max; | |
4468 | } | |
4469 | /* NOTREACHED */ | |
4470 | ||
4471 | case WHILEM_B_min: /* just matched B in a minimal match */ | |
4472 | case WHILEM_B_max: /* just matched B in a maximal match */ | |
4473 | cur_curlyx = ST.save_curlyx; | |
4474 | sayYES; | |
4475 | /* NOTREACHED */ | |
4476 | ||
4477 | case WHILEM_B_max_fail: /* just failed to match B in a maximal match */ | |
4478 | cur_curlyx = ST.save_curlyx; | |
4479 | cur_curlyx->u.curlyx.lastloc = ST.save_lastloc; | |
4480 | cur_curlyx->u.curlyx.count--; | |
4481 | CACHEsayNO; | |
4482 | /* NOTREACHED */ | |
4483 | ||
4484 | case WHILEM_A_min_fail: /* just failed to match A in a minimal match */ | |
4485 | REGCP_UNWIND(ST.lastcp); | |
4486 | regcppop(rex); | |
4487 | /* FALL THROUGH */ | |
4488 | case WHILEM_A_pre_fail: /* just failed to match even minimal A */ | |
4489 | cur_curlyx->u.curlyx.lastloc = ST.save_lastloc; | |
4490 | cur_curlyx->u.curlyx.count--; | |
4491 | CACHEsayNO; | |
4492 | /* NOTREACHED */ | |
4493 | ||
4494 | case WHILEM_A_max_fail: /* just failed to match A in a maximal match */ | |
4495 | REGCP_UNWIND(ST.lastcp); | |
4496 | regcppop(rex); /* Restore some previous $<digit>s? */ | |
4497 | PL_reginput = locinput; | |
4498 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, | |
4499 | "%*s whilem: failed, trying continuation...\n", | |
4500 | REPORT_CODE_OFF+depth*2, "") | |
4501 | ); | |
4502 | do_whilem_B_max: | |
4503 | if (cur_curlyx->u.curlyx.count >= REG_INFTY | |
4504 | && ckWARN(WARN_REGEXP) | |
4505 | && !(PL_reg_flags & RF_warned)) | |
4506 | { | |
4507 | PL_reg_flags |= RF_warned; | |
4508 | Perl_warner(aTHX_ packWARN(WARN_REGEXP), "%s limit (%d) exceeded", | |
4509 | "Complex regular subexpression recursion", | |
4510 | REG_INFTY - 1); | |
4511 | } | |
4512 | ||
4513 | /* now try B */ | |
4514 | ST.save_curlyx = cur_curlyx; | |
4515 | cur_curlyx = cur_curlyx->u.curlyx.prev_curlyx; | |
4516 | PUSH_YES_STATE_GOTO(WHILEM_B_max, ST.save_curlyx->u.curlyx.B); | |
4517 | /* NOTREACHED */ | |
4518 | ||
4519 | case WHILEM_B_min_fail: /* just failed to match B in a minimal match */ | |
4520 | cur_curlyx = ST.save_curlyx; | |
4521 | REGCP_UNWIND(ST.lastcp); | |
4522 | regcppop(rex); | |
4523 | ||
d02d6d97 | 4524 | if (cur_curlyx->u.curlyx.count >= /*max*/ARG2(cur_curlyx->u.curlyx.me)) { |
c476f425 DM |
4525 | /* Maximum greed exceeded */ |
4526 | if (cur_curlyx->u.curlyx.count >= REG_INFTY | |
4527 | && ckWARN(WARN_REGEXP) | |
4528 | && !(PL_reg_flags & RF_warned)) | |
4529 | { | |
3280af22 | 4530 | PL_reg_flags |= RF_warned; |
c476f425 DM |
4531 | Perl_warner(aTHX_ packWARN(WARN_REGEXP), |
4532 | "%s limit (%d) exceeded", | |
4533 | "Complex regular subexpression recursion", | |
4534 | REG_INFTY - 1); | |
a0d0e21e | 4535 | } |
c476f425 | 4536 | cur_curlyx->u.curlyx.count--; |
3ab3c9b4 | 4537 | CACHEsayNO; |
a0d0e21e | 4538 | } |
c476f425 DM |
4539 | |
4540 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, | |
4541 | "%*s trying longer...\n", REPORT_CODE_OFF+depth*2, "") | |
4542 | ); | |
4543 | /* Try grabbing another A and see if it helps. */ | |
4544 | PL_reginput = locinput; | |
4545 | cur_curlyx->u.curlyx.lastloc = locinput; | |
4546 | ST.cp = regcppush(cur_curlyx->u.curlyx.parenfloor); | |
4547 | REGCP_SET(ST.lastcp); | |
d02d6d97 DM |
4548 | PUSH_STATE_GOTO(WHILEM_A_min, |
4549 | /*A*/ NEXTOPER(ST.save_curlyx->u.curlyx.me) + EXTRA_STEP_2ARGS); | |
5f66b61c | 4550 | /* NOTREACHED */ |
40a82448 DM |
4551 | |
4552 | #undef ST | |
4553 | #define ST st->u.branch | |
4554 | ||
4555 | case BRANCHJ: /* /(...|A|...)/ with long next pointer */ | |
c277df42 IZ |
4556 | next = scan + ARG(scan); |
4557 | if (next == scan) | |
4558 | next = NULL; | |
40a82448 DM |
4559 | scan = NEXTOPER(scan); |
4560 | /* FALL THROUGH */ | |
c277df42 | 4561 | |
40a82448 DM |
4562 | case BRANCH: /* /(...|A|...)/ */ |
4563 | scan = NEXTOPER(scan); /* scan now points to inner node */ | |
40a82448 DM |
4564 | ST.lastparen = *PL_reglastparen; |
4565 | ST.next_branch = next; | |
4566 | REGCP_SET(ST.cp); | |
4567 | PL_reginput = locinput; | |
02db2b7b | 4568 | |
40a82448 | 4569 | /* Now go into the branch */ |
5d458dd8 YO |
4570 | if (has_cutgroup) { |
4571 | PUSH_YES_STATE_GOTO(BRANCH_next, scan); | |
4572 | } else { | |
4573 | PUSH_STATE_GOTO(BRANCH_next, scan); | |
4574 | } | |
40a82448 | 4575 | /* NOTREACHED */ |
5d458dd8 YO |
4576 | case CUTGROUP: |
4577 | PL_reginput = locinput; | |
4578 | sv_yes_mark = st->u.mark.mark_name = scan->flags ? NULL : | |
ad64d0ec | 4579 | MUTABLE_SV(rexi->data->data[ ARG( scan ) ]); |
5d458dd8 YO |
4580 | PUSH_STATE_GOTO(CUTGROUP_next,next); |
4581 | /* NOTREACHED */ | |
4582 | case CUTGROUP_next_fail: | |
4583 | do_cutgroup = 1; | |
4584 | no_final = 1; | |
4585 | if (st->u.mark.mark_name) | |
4586 | sv_commit = st->u.mark.mark_name; | |
4587 | sayNO; | |
4588 | /* NOTREACHED */ | |
4589 | case BRANCH_next: | |
4590 | sayYES; | |
4591 | /* NOTREACHED */ | |
40a82448 | 4592 | case BRANCH_next_fail: /* that branch failed; try the next, if any */ |
5d458dd8 YO |
4593 | if (do_cutgroup) { |
4594 | do_cutgroup = 0; | |
4595 | no_final = 0; | |
4596 | } | |
40a82448 DM |
4597 | REGCP_UNWIND(ST.cp); |
4598 | for (n = *PL_reglastparen; n > ST.lastparen; n--) | |
f0ab9afb | 4599 | PL_regoffs[n].end = -1; |
40a82448 | 4600 | *PL_reglastparen = n; |
0a4db386 | 4601 | /*dmq: *PL_reglastcloseparen = n; */ |
40a82448 DM |
4602 | scan = ST.next_branch; |
4603 | /* no more branches? */ | |
5d458dd8 YO |
4604 | if (!scan || (OP(scan) != BRANCH && OP(scan) != BRANCHJ)) { |
4605 | DEBUG_EXECUTE_r({ | |
4606 | PerlIO_printf( Perl_debug_log, | |
4607 | "%*s %sBRANCH failed...%s\n", | |
4608 | REPORT_CODE_OFF+depth*2, "", | |
4609 | PL_colors[4], | |
4610 | PL_colors[5] ); | |
4611 | }); | |
4612 | sayNO_SILENT; | |
4613 | } | |
40a82448 DM |
4614 | continue; /* execute next BRANCH[J] op */ |
4615 | /* NOTREACHED */ | |
4616 | ||
a0d0e21e | 4617 | case MINMOD: |
24d3c4a9 | 4618 | minmod = 1; |
a0d0e21e | 4619 | break; |
40a82448 DM |
4620 | |
4621 | #undef ST | |
4622 | #define ST st->u.curlym | |
4623 | ||
4624 | case CURLYM: /* /A{m,n}B/ where A is fixed-length */ | |
4625 | ||
4626 | /* This is an optimisation of CURLYX that enables us to push | |
84d2fa14 | 4627 | * only a single backtracking state, no matter how many matches |
40a82448 DM |
4628 | * there are in {m,n}. It relies on the pattern being constant |
4629 | * length, with no parens to influence future backrefs | |
4630 | */ | |
4631 | ||
4632 | ST.me = scan; | |
dc45a647 | 4633 | scan = NEXTOPER(scan) + NODE_STEP_REGNODE; |
40a82448 DM |
4634 | |
4635 | /* if paren positive, emulate an OPEN/CLOSE around A */ | |
4636 | if (ST.me->flags) { | |
3b6647e0 | 4637 | U32 paren = ST.me->flags; |
40a82448 DM |
4638 | if (paren > PL_regsize) |
4639 | PL_regsize = paren; | |
3b6647e0 | 4640 | if (paren > *PL_reglastparen) |
40a82448 | 4641 | *PL_reglastparen = paren; |
c277df42 | 4642 | scan += NEXT_OFF(scan); /* Skip former OPEN. */ |
6407bf3b | 4643 | } |
40a82448 DM |
4644 | ST.A = scan; |
4645 | ST.B = next; | |
4646 | ST.alen = 0; | |
4647 | ST.count = 0; | |
24d3c4a9 DM |
4648 | ST.minmod = minmod; |
4649 | minmod = 0; | |
40a82448 DM |
4650 | ST.c1 = CHRTEST_UNINIT; |
4651 | REGCP_SET(ST.cp); | |
6407bf3b | 4652 | |
40a82448 DM |
4653 | if (!(ST.minmod ? ARG1(ST.me) : ARG2(ST.me))) /* min/max */ |
4654 | goto curlym_do_B; | |
4655 | ||
4656 | curlym_do_A: /* execute the A in /A{m,n}B/ */ | |
6407bf3b | 4657 | PL_reginput = locinput; |
40a82448 DM |
4658 | PUSH_YES_STATE_GOTO(CURLYM_A, ST.A); /* match A */ |
4659 | /* NOTREACHED */ | |
5f80c4cf | 4660 | |
40a82448 DM |
4661 | case CURLYM_A: /* we've just matched an A */ |
4662 | locinput = st->locinput; | |
4663 | nextchr = UCHARAT(locinput); | |
4664 | ||
4665 | ST.count++; | |
4666 | /* after first match, determine A's length: u.curlym.alen */ | |
4667 | if (ST.count == 1) { | |
4668 | if (PL_reg_match_utf8) { | |
4669 | char *s = locinput; | |
4670 | while (s < PL_reginput) { | |
4671 | ST.alen++; | |
4672 | s += UTF8SKIP(s); | |
4673 | } | |
4674 | } | |
4675 | else { | |
4676 | ST.alen = PL_reginput - locinput; | |
4677 | } | |
4678 | if (ST.alen == 0) | |
4679 | ST.count = ST.minmod ? ARG1(ST.me) : ARG2(ST.me); | |
4680 | } | |
0cadcf80 DM |
4681 | DEBUG_EXECUTE_r( |
4682 | PerlIO_printf(Perl_debug_log, | |
40a82448 | 4683 | "%*s CURLYM now matched %"IVdf" times, len=%"IVdf"...\n", |
5bc10b2c | 4684 | (int)(REPORT_CODE_OFF+(depth*2)), "", |
40a82448 | 4685 | (IV) ST.count, (IV)ST.alen) |
0cadcf80 DM |
4686 | ); |
4687 | ||
40a82448 | 4688 | locinput = PL_reginput; |
0a4db386 YO |
4689 | |
4690 | if (cur_eval && cur_eval->u.eval.close_paren && | |
24b23f37 | 4691 | cur_eval->u.eval.close_paren == (U32)ST.me->flags) |
0a4db386 YO |
4692 | goto fake_end; |
4693 | ||
c966426a DM |
4694 | { |
4695 | I32 max = (ST.minmod ? ARG1(ST.me) : ARG2(ST.me)); | |
4696 | if ( max == REG_INFTY || ST.count < max ) | |
4697 | goto curlym_do_A; /* try to match another A */ | |
4698 | } | |
40a82448 | 4699 | goto curlym_do_B; /* try to match B */ |
5f80c4cf | 4700 | |
40a82448 DM |
4701 | case CURLYM_A_fail: /* just failed to match an A */ |
4702 | REGCP_UNWIND(ST.cp); | |
0a4db386 YO |
4703 | |
4704 | if (ST.minmod || ST.count < ARG1(ST.me) /* min*/ | |
4705 | || (cur_eval && cur_eval->u.eval.close_paren && | |
24b23f37 | 4706 | cur_eval->u.eval.close_paren == (U32)ST.me->flags)) |
40a82448 | 4707 | sayNO; |
0cadcf80 | 4708 | |
40a82448 DM |
4709 | curlym_do_B: /* execute the B in /A{m,n}B/ */ |
4710 | PL_reginput = locinput; | |
4711 | if (ST.c1 == CHRTEST_UNINIT) { | |
4712 | /* calculate c1 and c2 for possible match of 1st char | |
4713 | * following curly */ | |
4714 | ST.c1 = ST.c2 = CHRTEST_VOID; | |
4715 | if (HAS_TEXT(ST.B) || JUMPABLE(ST.B)) { | |
4716 | regnode *text_node = ST.B; | |
4717 | if (! HAS_TEXT(text_node)) | |
4718 | FIND_NEXT_IMPT(text_node); | |
ee9b8eae YO |
4719 | /* this used to be |
4720 | ||
4721 | (HAS_TEXT(text_node) && PL_regkind[OP(text_node)] == EXACT) | |
4722 | ||
4723 | But the former is redundant in light of the latter. | |
4724 | ||
4725 | if this changes back then the macro for | |
4726 | IS_TEXT and friends need to change. | |
4727 | */ | |
4728 | if (PL_regkind[OP(text_node)] == EXACT) | |
40a82448 | 4729 | { |
ee9b8eae | 4730 | |
40a82448 DM |
4731 | ST.c1 = (U8)*STRING(text_node); |
4732 | ST.c2 = | |
ee9b8eae | 4733 | (IS_TEXTF(text_node)) |
40a82448 | 4734 | ? PL_fold[ST.c1] |
ee9b8eae | 4735 | : (IS_TEXTFL(text_node)) |
40a82448 DM |
4736 | ? PL_fold_locale[ST.c1] |
4737 | : ST.c1; | |
c277df42 | 4738 | } |
c277df42 | 4739 | } |
40a82448 DM |
4740 | } |
4741 | ||
4742 | DEBUG_EXECUTE_r( | |
4743 | PerlIO_printf(Perl_debug_log, | |
4744 | "%*s CURLYM trying tail with matches=%"IVdf"...\n", | |
5bc10b2c | 4745 | (int)(REPORT_CODE_OFF+(depth*2)), |
40a82448 DM |
4746 | "", (IV)ST.count) |
4747 | ); | |
4748 | if (ST.c1 != CHRTEST_VOID | |
4749 | && UCHARAT(PL_reginput) != ST.c1 | |
4750 | && UCHARAT(PL_reginput) != ST.c2) | |
4751 | { | |
4752 | /* simulate B failing */ | |
3e901dc0 YO |
4753 | DEBUG_OPTIMISE_r( |
4754 | PerlIO_printf(Perl_debug_log, | |
4755 | "%*s CURLYM Fast bail c1=%"IVdf" c2=%"IVdf"\n", | |
4756 | (int)(REPORT_CODE_OFF+(depth*2)),"", | |
4757 | (IV)ST.c1,(IV)ST.c2 | |
4758 | )); | |
40a82448 DM |
4759 | state_num = CURLYM_B_fail; |
4760 | goto reenter_switch; | |
4761 | } | |
4762 | ||
4763 | if (ST.me->flags) { | |
4764 | /* mark current A as captured */ | |
4765 | I32 paren = ST.me->flags; | |
4766 | if (ST.count) { | |
f0ab9afb | 4767 | PL_regoffs[paren].start |
40a82448 | 4768 | = HOPc(PL_reginput, -ST.alen) - PL_bostr; |
f0ab9afb | 4769 | PL_regoffs[paren].end = PL_reginput - PL_bostr; |
0a4db386 | 4770 | /*dmq: *PL_reglastcloseparen = paren; */ |
c277df42 | 4771 | } |
40a82448 | 4772 | else |
f0ab9afb | 4773 | PL_regoffs[paren].end = -1; |
0a4db386 | 4774 | if (cur_eval && cur_eval->u.eval.close_paren && |
24b23f37 | 4775 | cur_eval->u.eval.close_paren == (U32)ST.me->flags) |
0a4db386 YO |
4776 | { |
4777 | if (ST.count) | |
4778 | goto fake_end; | |
4779 | else | |
4780 | sayNO; | |
4781 | } | |
c277df42 | 4782 | } |
0a4db386 | 4783 | |
40a82448 | 4784 | PUSH_STATE_GOTO(CURLYM_B, ST.B); /* match B */ |
5f66b61c | 4785 | /* NOTREACHED */ |
40a82448 DM |
4786 | |
4787 | case CURLYM_B_fail: /* just failed to match a B */ | |
4788 | REGCP_UNWIND(ST.cp); | |
4789 | if (ST.minmod) { | |
84d2fa14 HS |
4790 | I32 max = ARG2(ST.me); |
4791 | if (max != REG_INFTY && ST.count == max) | |
40a82448 DM |
4792 | sayNO; |
4793 | goto curlym_do_A; /* try to match a further A */ | |
4794 | } | |
4795 | /* backtrack one A */ | |
4796 | if (ST.count == ARG1(ST.me) /* min */) | |
4797 | sayNO; | |
4798 | ST.count--; | |
4799 | locinput = HOPc(locinput, -ST.alen); | |
4800 | goto curlym_do_B; /* try to match B */ | |
4801 | ||
c255a977 DM |
4802 | #undef ST |
4803 | #define ST st->u.curly | |
40a82448 | 4804 | |
c255a977 DM |
4805 | #define CURLY_SETPAREN(paren, success) \ |
4806 | if (paren) { \ | |
4807 | if (success) { \ | |
f0ab9afb NC |
4808 | PL_regoffs[paren].start = HOPc(locinput, -1) - PL_bostr; \ |
4809 | PL_regoffs[paren].end = locinput - PL_bostr; \ | |
0a4db386 | 4810 | *PL_reglastcloseparen = paren; \ |
c255a977 DM |
4811 | } \ |
4812 | else \ | |
f0ab9afb | 4813 | PL_regoffs[paren].end = -1; \ |
c255a977 DM |
4814 | } |
4815 | ||
4816 | case STAR: /* /A*B/ where A is width 1 */ | |
4817 | ST.paren = 0; | |
4818 | ST.min = 0; | |
4819 | ST.max = REG_INFTY; | |
a0d0e21e LW |
4820 | scan = NEXTOPER(scan); |
4821 | goto repeat; | |
c255a977 DM |
4822 | case PLUS: /* /A+B/ where A is width 1 */ |
4823 | ST.paren = 0; | |
4824 | ST.min = 1; | |
4825 | ST.max = REG_INFTY; | |
c277df42 | 4826 | scan = NEXTOPER(scan); |
c255a977 DM |
4827 | goto repeat; |
4828 | case CURLYN: /* /(A){m,n}B/ where A is width 1 */ | |
4829 | ST.paren = scan->flags; /* Which paren to set */ | |
4830 | if (ST.paren > PL_regsize) | |
4831 | PL_regsize = ST.paren; | |
3b6647e0 | 4832 | if (ST.paren > *PL_reglastparen) |
c255a977 DM |
4833 | *PL_reglastparen = ST.paren; |
4834 | ST.min = ARG1(scan); /* min to match */ | |
4835 | ST.max = ARG2(scan); /* max to match */ | |
0a4db386 | 4836 | if (cur_eval && cur_eval->u.eval.close_paren && |
86413ec0 | 4837 | cur_eval->u.eval.close_paren == (U32)ST.paren) { |
0a4db386 YO |
4838 | ST.min=1; |
4839 | ST.max=1; | |
4840 | } | |
c255a977 DM |
4841 | scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE); |
4842 | goto repeat; | |
4843 | case CURLY: /* /A{m,n}B/ where A is width 1 */ | |
4844 | ST.paren = 0; | |
4845 | ST.min = ARG1(scan); /* min to match */ | |
4846 | ST.max = ARG2(scan); /* max to match */ | |
4847 | scan = NEXTOPER(scan) + NODE_STEP_REGNODE; | |
c277df42 | 4848 | repeat: |
a0d0e21e LW |
4849 | /* |
4850 | * Lookahead to avoid useless match attempts | |
4851 | * when we know what character comes next. | |
c255a977 | 4852 | * |
5f80c4cf JP |
4853 | * Used to only do .*x and .*?x, but now it allows |
4854 | * for )'s, ('s and (?{ ... })'s to be in the way | |
4855 | * of the quantifier and the EXACT-like node. -- japhy | |
4856 | */ | |
4857 | ||
c255a977 DM |
4858 | if (ST.min > ST.max) /* XXX make this a compile-time check? */ |
4859 | sayNO; | |
cca55fe3 | 4860 | if (HAS_TEXT(next) || JUMPABLE(next)) { |
5f80c4cf JP |
4861 | U8 *s; |
4862 | regnode *text_node = next; | |
4863 | ||
3dab1dad YO |
4864 | if (! HAS_TEXT(text_node)) |
4865 | FIND_NEXT_IMPT(text_node); | |
5f80c4cf | 4866 | |
9e137952 | 4867 | if (! HAS_TEXT(text_node)) |
c255a977 | 4868 | ST.c1 = ST.c2 = CHRTEST_VOID; |
5f80c4cf | 4869 | else { |
ee9b8eae | 4870 | if ( PL_regkind[OP(text_node)] != EXACT ) { |
c255a977 | 4871 | ST.c1 = ST.c2 = CHRTEST_VOID; |
44a68960 | 4872 | goto assume_ok_easy; |
cca55fe3 | 4873 | } |
be8e71aa YO |
4874 | else |
4875 | s = (U8*)STRING(text_node); | |
ee9b8eae YO |
4876 | |
4877 | /* Currently we only get here when | |
4878 | ||
4879 | PL_rekind[OP(text_node)] == EXACT | |
4880 | ||
4881 | if this changes back then the macro for IS_TEXT and | |
4882 | friends need to change. */ | |
5f80c4cf | 4883 | if (!UTF) { |
c255a977 | 4884 | ST.c2 = ST.c1 = *s; |
ee9b8eae | 4885 | if (IS_TEXTF(text_node)) |
c255a977 | 4886 | ST.c2 = PL_fold[ST.c1]; |
ee9b8eae | 4887 | else if (IS_TEXTFL(text_node)) |
c255a977 | 4888 | ST.c2 = PL_fold_locale[ST.c1]; |
1aa99e6b | 4889 | } |
5f80c4cf | 4890 | else { /* UTF */ |
ee9b8eae | 4891 | if (IS_TEXTF(text_node)) { |
a2a2844f | 4892 | STRLEN ulen1, ulen2; |
89ebb4a3 JH |
4893 | U8 tmpbuf1[UTF8_MAXBYTES_CASE+1]; |
4894 | U8 tmpbuf2[UTF8_MAXBYTES_CASE+1]; | |
a2a2844f JH |
4895 | |
4896 | to_utf8_lower((U8*)s, tmpbuf1, &ulen1); | |
4897 | to_utf8_upper((U8*)s, tmpbuf2, &ulen2); | |
e294cc5d JH |
4898 | #ifdef EBCDIC |
4899 | ST.c1 = utf8n_to_uvchr(tmpbuf1, UTF8_MAXLEN, 0, | |
4900 | ckWARN(WARN_UTF8) ? | |
4901 | 0 : UTF8_ALLOW_ANY); | |
4902 | ST.c2 = utf8n_to_uvchr(tmpbuf2, UTF8_MAXLEN, 0, | |
4903 | ckWARN(WARN_UTF8) ? | |
4904 | 0 : UTF8_ALLOW_ANY); | |
4905 | #else | |
c255a977 | 4906 | ST.c1 = utf8n_to_uvuni(tmpbuf1, UTF8_MAXBYTES, 0, |
e294cc5d | 4907 | uniflags); |
c255a977 | 4908 | ST.c2 = utf8n_to_uvuni(tmpbuf2, UTF8_MAXBYTES, 0, |
e294cc5d JH |
4909 | uniflags); |
4910 | #endif | |
5f80c4cf JP |
4911 | } |
4912 | else { | |
c255a977 | 4913 | ST.c2 = ST.c1 = utf8n_to_uvchr(s, UTF8_MAXBYTES, 0, |
041457d9 | 4914 | uniflags); |
5f80c4cf | 4915 | } |
1aa99e6b IH |
4916 | } |
4917 | } | |
bbce6d69 | 4918 | } |
a0d0e21e | 4919 | else |
c255a977 | 4920 | ST.c1 = ST.c2 = CHRTEST_VOID; |
cca55fe3 | 4921 | assume_ok_easy: |
c255a977 DM |
4922 | |
4923 | ST.A = scan; | |
4924 | ST.B = next; | |
3280af22 | 4925 | PL_reginput = locinput; |
24d3c4a9 DM |
4926 | if (minmod) { |
4927 | minmod = 0; | |
e2e6a0f1 | 4928 | if (ST.min && regrepeat(rex, ST.A, ST.min, depth) < ST.min) |
4633a7c4 | 4929 | sayNO; |
c255a977 | 4930 | ST.count = ST.min; |
a0ed51b3 | 4931 | locinput = PL_reginput; |
c255a977 DM |
4932 | REGCP_SET(ST.cp); |
4933 | if (ST.c1 == CHRTEST_VOID) | |
4934 | goto curly_try_B_min; | |
4935 | ||
4936 | ST.oldloc = locinput; | |
4937 | ||
4938 | /* set ST.maxpos to the furthest point along the | |
4939 | * string that could possibly match */ | |
4940 | if (ST.max == REG_INFTY) { | |
4941 | ST.maxpos = PL_regeol - 1; | |
4942 | if (do_utf8) | |
4943 | while (UTF8_IS_CONTINUATION(*(U8*)ST.maxpos)) | |
4944 | ST.maxpos--; | |
4945 | } | |
4946 | else if (do_utf8) { | |
4947 | int m = ST.max - ST.min; | |
4948 | for (ST.maxpos = locinput; | |
4949 | m >0 && ST.maxpos + UTF8SKIP(ST.maxpos) <= PL_regeol; m--) | |
4950 | ST.maxpos += UTF8SKIP(ST.maxpos); | |
4951 | } | |
4952 | else { | |
4953 | ST.maxpos = locinput + ST.max - ST.min; | |
4954 | if (ST.maxpos >= PL_regeol) | |
4955 | ST.maxpos = PL_regeol - 1; | |
4956 | } | |
4957 | goto curly_try_B_min_known; | |
4958 | ||
4959 | } | |
4960 | else { | |
e2e6a0f1 | 4961 | ST.count = regrepeat(rex, ST.A, ST.max, depth); |
c255a977 DM |
4962 | locinput = PL_reginput; |
4963 | if (ST.count < ST.min) | |
4964 | sayNO; | |
4965 | if ((ST.count > ST.min) | |
4966 | && (PL_regkind[OP(ST.B)] == EOL) && (OP(ST.B) != MEOL)) | |
4967 | { | |
4968 | /* A{m,n} must come at the end of the string, there's | |
4969 | * no point in backing off ... */ | |
4970 | ST.min = ST.count; | |
4971 | /* ...except that $ and \Z can match before *and* after | |
4972 | newline at the end. Consider "\n\n" =~ /\n+\Z\n/. | |
4973 | We may back off by one in this case. */ | |
4974 | if (UCHARAT(PL_reginput - 1) == '\n' && OP(ST.B) != EOS) | |
4975 | ST.min--; | |
4976 | } | |
4977 | REGCP_SET(ST.cp); | |
4978 | goto curly_try_B_max; | |
4979 | } | |
4980 | /* NOTREACHED */ | |
4981 | ||
4982 | ||
4983 | case CURLY_B_min_known_fail: | |
4984 | /* failed to find B in a non-greedy match where c1,c2 valid */ | |
4985 | if (ST.paren && ST.count) | |
f0ab9afb | 4986 | PL_regoffs[ST.paren].end = -1; |
c255a977 DM |
4987 | |
4988 | PL_reginput = locinput; /* Could be reset... */ | |
4989 | REGCP_UNWIND(ST.cp); | |
4990 | /* Couldn't or didn't -- move forward. */ | |
4991 | ST.oldloc = locinput; | |
4992 | if (do_utf8) | |
4993 | locinput += UTF8SKIP(locinput); | |
4994 | else | |
4995 | locinput++; | |
4996 | ST.count++; | |
4997 | curly_try_B_min_known: | |
4998 | /* find the next place where 'B' could work, then call B */ | |
4999 | { | |
5000 | int n; | |
5001 | if (do_utf8) { | |
5002 | n = (ST.oldloc == locinput) ? 0 : 1; | |
5003 | if (ST.c1 == ST.c2) { | |
5004 | STRLEN len; | |
5005 | /* set n to utf8_distance(oldloc, locinput) */ | |
5006 | while (locinput <= ST.maxpos && | |
5007 | utf8n_to_uvchr((U8*)locinput, | |
5008 | UTF8_MAXBYTES, &len, | |
5009 | uniflags) != (UV)ST.c1) { | |
5010 | locinput += len; | |
5011 | n++; | |
5012 | } | |
1aa99e6b IH |
5013 | } |
5014 | else { | |
c255a977 DM |
5015 | /* set n to utf8_distance(oldloc, locinput) */ |
5016 | while (locinput <= ST.maxpos) { | |
5017 | STRLEN len; | |
5018 | const UV c = utf8n_to_uvchr((U8*)locinput, | |
5019 | UTF8_MAXBYTES, &len, | |
5020 | uniflags); | |
5021 | if (c == (UV)ST.c1 || c == (UV)ST.c2) | |
5022 | break; | |
5023 | locinput += len; | |
5024 | n++; | |
1aa99e6b | 5025 | } |
0fe9bf95 IZ |
5026 | } |
5027 | } | |
c255a977 DM |
5028 | else { |
5029 | if (ST.c1 == ST.c2) { | |
5030 | while (locinput <= ST.maxpos && | |
5031 | UCHARAT(locinput) != ST.c1) | |
5032 | locinput++; | |
bbce6d69 | 5033 | } |
c255a977 DM |
5034 | else { |
5035 | while (locinput <= ST.maxpos | |
5036 | && UCHARAT(locinput) != ST.c1 | |
5037 | && UCHARAT(locinput) != ST.c2) | |
5038 | locinput++; | |
a0ed51b3 | 5039 | } |
c255a977 DM |
5040 | n = locinput - ST.oldloc; |
5041 | } | |
5042 | if (locinput > ST.maxpos) | |
5043 | sayNO; | |
5044 | /* PL_reginput == oldloc now */ | |
5045 | if (n) { | |
5046 | ST.count += n; | |
e2e6a0f1 | 5047 | if (regrepeat(rex, ST.A, n, depth) < n) |
4633a7c4 | 5048 | sayNO; |
a0d0e21e | 5049 | } |
c255a977 DM |
5050 | PL_reginput = locinput; |
5051 | CURLY_SETPAREN(ST.paren, ST.count); | |
0a4db386 | 5052 | if (cur_eval && cur_eval->u.eval.close_paren && |
86413ec0 | 5053 | cur_eval->u.eval.close_paren == (U32)ST.paren) { |
0a4db386 YO |
5054 | goto fake_end; |
5055 | } | |
c255a977 | 5056 | PUSH_STATE_GOTO(CURLY_B_min_known, ST.B); |
a0d0e21e | 5057 | } |
c255a977 DM |
5058 | /* NOTREACHED */ |
5059 | ||
5060 | ||
5061 | case CURLY_B_min_fail: | |
5062 | /* failed to find B in a non-greedy match where c1,c2 invalid */ | |
5063 | if (ST.paren && ST.count) | |
f0ab9afb | 5064 | PL_regoffs[ST.paren].end = -1; |
c255a977 DM |
5065 | |
5066 | REGCP_UNWIND(ST.cp); | |
5067 | /* failed -- move forward one */ | |
5068 | PL_reginput = locinput; | |
e2e6a0f1 | 5069 | if (regrepeat(rex, ST.A, 1, depth)) { |
c255a977 | 5070 | ST.count++; |
a0ed51b3 | 5071 | locinput = PL_reginput; |
c255a977 DM |
5072 | if (ST.count <= ST.max || (ST.max == REG_INFTY && |
5073 | ST.count > 0)) /* count overflow ? */ | |
15272685 | 5074 | { |
c255a977 DM |
5075 | curly_try_B_min: |
5076 | CURLY_SETPAREN(ST.paren, ST.count); | |
0a4db386 | 5077 | if (cur_eval && cur_eval->u.eval.close_paren && |
86413ec0 | 5078 | cur_eval->u.eval.close_paren == (U32)ST.paren) { |
0a4db386 YO |
5079 | goto fake_end; |
5080 | } | |
c255a977 | 5081 | PUSH_STATE_GOTO(CURLY_B_min, ST.B); |
a0d0e21e LW |
5082 | } |
5083 | } | |
4633a7c4 | 5084 | sayNO; |
c255a977 DM |
5085 | /* NOTREACHED */ |
5086 | ||
5087 | ||
5088 | curly_try_B_max: | |
5089 | /* a successful greedy match: now try to match B */ | |
40d049e4 | 5090 | if (cur_eval && cur_eval->u.eval.close_paren && |
86413ec0 | 5091 | cur_eval->u.eval.close_paren == (U32)ST.paren) { |
40d049e4 YO |
5092 | goto fake_end; |
5093 | } | |
c255a977 DM |
5094 | { |
5095 | UV c = 0; | |
5096 | if (ST.c1 != CHRTEST_VOID) | |
5097 | c = do_utf8 ? utf8n_to_uvchr((U8*)PL_reginput, | |
5098 | UTF8_MAXBYTES, 0, uniflags) | |
466787eb | 5099 | : (UV) UCHARAT(PL_reginput); |
c255a977 DM |
5100 | /* If it could work, try it. */ |
5101 | if (ST.c1 == CHRTEST_VOID || c == (UV)ST.c1 || c == (UV)ST.c2) { | |
5102 | CURLY_SETPAREN(ST.paren, ST.count); | |
5103 | PUSH_STATE_GOTO(CURLY_B_max, ST.B); | |
5104 | /* NOTREACHED */ | |
5105 | } | |
5106 | } | |
5107 | /* FALL THROUGH */ | |
5108 | case CURLY_B_max_fail: | |
5109 | /* failed to find B in a greedy match */ | |
5110 | if (ST.paren && ST.count) | |
f0ab9afb | 5111 | PL_regoffs[ST.paren].end = -1; |
c255a977 DM |
5112 | |
5113 | REGCP_UNWIND(ST.cp); | |
5114 | /* back up. */ | |
5115 | if (--ST.count < ST.min) | |
5116 | sayNO; | |
5117 | PL_reginput = locinput = HOPc(locinput, -1); | |
5118 | goto curly_try_B_max; | |
5119 | ||
5120 | #undef ST | |
5121 | ||
a0d0e21e | 5122 | case END: |
6bda09f9 | 5123 | fake_end: |
faec1544 DM |
5124 | if (cur_eval) { |
5125 | /* we've just finished A in /(??{A})B/; now continue with B */ | |
5126 | I32 tmpix; | |
faec1544 DM |
5127 | st->u.eval.toggle_reg_flags |
5128 | = cur_eval->u.eval.toggle_reg_flags; | |
5129 | PL_reg_flags ^= st->u.eval.toggle_reg_flags; | |
5130 | ||
288b8c02 NC |
5131 | st->u.eval.prev_rex = rex_sv; /* inner */ |
5132 | SETREX(rex_sv,cur_eval->u.eval.prev_rex); | |
5133 | rex = (struct regexp *)SvANY(rex_sv); | |
f8fc2ecf | 5134 | rexi = RXi_GET(rex); |
faec1544 | 5135 | cur_curlyx = cur_eval->u.eval.prev_curlyx; |
288b8c02 | 5136 | ReREFCNT_inc(rex_sv); |
faec1544 | 5137 | st->u.eval.cp = regcppush(0); /* Save *all* the positions. */ |
34a81e2b B |
5138 | |
5139 | /* rex was changed so update the pointer in PL_reglastparen and PL_reglastcloseparen */ | |
5140 | PL_reglastparen = &rex->lastparen; | |
5141 | PL_reglastcloseparen = &rex->lastcloseparen; | |
5142 | ||
faec1544 DM |
5143 | REGCP_SET(st->u.eval.lastcp); |
5144 | PL_reginput = locinput; | |
5145 | ||
5146 | /* Restore parens of the outer rex without popping the | |
5147 | * savestack */ | |
5148 | tmpix = PL_savestack_ix; | |
5149 | PL_savestack_ix = cur_eval->u.eval.lastcp; | |
5150 | regcppop(rex); | |
5151 | PL_savestack_ix = tmpix; | |
5152 | ||
5153 | st->u.eval.prev_eval = cur_eval; | |
5154 | cur_eval = cur_eval->u.eval.prev_eval; | |
5155 | DEBUG_EXECUTE_r( | |
2a49f0f5 JH |
5156 | PerlIO_printf(Perl_debug_log, "%*s EVAL trying tail ... %"UVxf"\n", |
5157 | REPORT_CODE_OFF+depth*2, "",PTR2UV(cur_eval));); | |
e7707071 YO |
5158 | if ( nochange_depth ) |
5159 | nochange_depth--; | |
5160 | ||
5161 | PUSH_YES_STATE_GOTO(EVAL_AB, | |
faec1544 DM |
5162 | st->u.eval.prev_eval->u.eval.B); /* match B */ |
5163 | } | |
5164 | ||
3b0527fe | 5165 | if (locinput < reginfo->till) { |
a3621e74 | 5166 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, |
7821416a IZ |
5167 | "%sMatch possible, but length=%ld is smaller than requested=%ld, failing!%s\n", |
5168 | PL_colors[4], | |
5169 | (long)(locinput - PL_reg_starttry), | |
3b0527fe | 5170 | (long)(reginfo->till - PL_reg_starttry), |
7821416a | 5171 | PL_colors[5])); |
58e23c8d | 5172 | |
262b90c4 | 5173 | sayNO_SILENT; /* Cannot match: too short. */ |
7821416a IZ |
5174 | } |
5175 | PL_reginput = locinput; /* put where regtry can find it */ | |
262b90c4 | 5176 | sayYES; /* Success! */ |
dad79028 DM |
5177 | |
5178 | case SUCCEED: /* successful SUSPEND/UNLESSM/IFMATCH/CURLYM */ | |
5179 | DEBUG_EXECUTE_r( | |
5180 | PerlIO_printf(Perl_debug_log, | |
5181 | "%*s %ssubpattern success...%s\n", | |
5bc10b2c | 5182 | REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5])); |
3280af22 | 5183 | PL_reginput = locinput; /* put where regtry can find it */ |
262b90c4 | 5184 | sayYES; /* Success! */ |
dad79028 | 5185 | |
40a82448 DM |
5186 | #undef ST |
5187 | #define ST st->u.ifmatch | |
5188 | ||
5189 | case SUSPEND: /* (?>A) */ | |
5190 | ST.wanted = 1; | |
9fe1d20c | 5191 | PL_reginput = locinput; |
9041c2e3 | 5192 | goto do_ifmatch; |
dad79028 | 5193 | |
40a82448 DM |
5194 | case UNLESSM: /* -ve lookaround: (?!A), or with flags, (?<!A) */ |
5195 | ST.wanted = 0; | |
dad79028 DM |
5196 | goto ifmatch_trivial_fail_test; |
5197 | ||
40a82448 DM |
5198 | case IFMATCH: /* +ve lookaround: (?=A), or with flags, (?<=A) */ |
5199 | ST.wanted = 1; | |
dad79028 | 5200 | ifmatch_trivial_fail_test: |
a0ed51b3 | 5201 | if (scan->flags) { |
52657f30 | 5202 | char * const s = HOPBACKc(locinput, scan->flags); |
dad79028 DM |
5203 | if (!s) { |
5204 | /* trivial fail */ | |
24d3c4a9 DM |
5205 | if (logical) { |
5206 | logical = 0; | |
f2338a2e | 5207 | sw = 1 - cBOOL(ST.wanted); |
dad79028 | 5208 | } |
40a82448 | 5209 | else if (ST.wanted) |
dad79028 DM |
5210 | sayNO; |
5211 | next = scan + ARG(scan); | |
5212 | if (next == scan) | |
5213 | next = NULL; | |
5214 | break; | |
5215 | } | |
efb30f32 | 5216 | PL_reginput = s; |
a0ed51b3 LW |
5217 | } |
5218 | else | |
5219 | PL_reginput = locinput; | |
5220 | ||
c277df42 | 5221 | do_ifmatch: |
40a82448 | 5222 | ST.me = scan; |
24d3c4a9 | 5223 | ST.logical = logical; |
24d786f4 YO |
5224 | logical = 0; /* XXX: reset state of logical once it has been saved into ST */ |
5225 | ||
40a82448 DM |
5226 | /* execute body of (?...A) */ |
5227 | PUSH_YES_STATE_GOTO(IFMATCH_A, NEXTOPER(NEXTOPER(scan))); | |
5228 | /* NOTREACHED */ | |
5229 | ||
5230 | case IFMATCH_A_fail: /* body of (?...A) failed */ | |
5231 | ST.wanted = !ST.wanted; | |
5232 | /* FALL THROUGH */ | |
5233 | ||
5234 | case IFMATCH_A: /* body of (?...A) succeeded */ | |
24d3c4a9 | 5235 | if (ST.logical) { |
f2338a2e | 5236 | sw = cBOOL(ST.wanted); |
40a82448 DM |
5237 | } |
5238 | else if (!ST.wanted) | |
5239 | sayNO; | |
5240 | ||
5241 | if (OP(ST.me) == SUSPEND) | |
5242 | locinput = PL_reginput; | |
5243 | else { | |
5244 | locinput = PL_reginput = st->locinput; | |
5245 | nextchr = UCHARAT(locinput); | |
5246 | } | |
5247 | scan = ST.me + ARG(ST.me); | |
5248 | if (scan == ST.me) | |
5249 | scan = NULL; | |
5250 | continue; /* execute B */ | |
5251 | ||
5252 | #undef ST | |
dad79028 | 5253 | |
c277df42 | 5254 | case LONGJMP: |
c277df42 IZ |
5255 | next = scan + ARG(scan); |
5256 | if (next == scan) | |
5257 | next = NULL; | |
a0d0e21e | 5258 | break; |
54612592 | 5259 | case COMMIT: |
e2e6a0f1 YO |
5260 | reginfo->cutpoint = PL_regeol; |
5261 | /* FALLTHROUGH */ | |
5d458dd8 | 5262 | case PRUNE: |
24b23f37 | 5263 | PL_reginput = locinput; |
e2e6a0f1 | 5264 | if (!scan->flags) |
ad64d0ec | 5265 | sv_yes_mark = sv_commit = MUTABLE_SV(rexi->data->data[ ARG( scan ) ]); |
54612592 YO |
5266 | PUSH_STATE_GOTO(COMMIT_next,next); |
5267 | /* NOTREACHED */ | |
5268 | case COMMIT_next_fail: | |
5269 | no_final = 1; | |
5270 | /* FALLTHROUGH */ | |
7f69552c YO |
5271 | case OPFAIL: |
5272 | sayNO; | |
e2e6a0f1 YO |
5273 | /* NOTREACHED */ |
5274 | ||
5275 | #define ST st->u.mark | |
5276 | case MARKPOINT: | |
5277 | ST.prev_mark = mark_state; | |
5d458dd8 | 5278 | ST.mark_name = sv_commit = sv_yes_mark |
ad64d0ec | 5279 | = MUTABLE_SV(rexi->data->data[ ARG( scan ) ]); |
e2e6a0f1 YO |
5280 | mark_state = st; |
5281 | ST.mark_loc = PL_reginput = locinput; | |
5282 | PUSH_YES_STATE_GOTO(MARKPOINT_next,next); | |
5283 | /* NOTREACHED */ | |
5284 | case MARKPOINT_next: | |
5285 | mark_state = ST.prev_mark; | |
5286 | sayYES; | |
5287 | /* NOTREACHED */ | |
5288 | case MARKPOINT_next_fail: | |
5d458dd8 | 5289 | if (popmark && sv_eq(ST.mark_name,popmark)) |
e2e6a0f1 YO |
5290 | { |
5291 | if (ST.mark_loc > startpoint) | |
5292 | reginfo->cutpoint = HOPBACKc(ST.mark_loc, 1); | |
5293 | popmark = NULL; /* we found our mark */ | |
5294 | sv_commit = ST.mark_name; | |
5295 | ||
5296 | DEBUG_EXECUTE_r({ | |
5d458dd8 | 5297 | PerlIO_printf(Perl_debug_log, |
e2e6a0f1 YO |
5298 | "%*s %ssetting cutpoint to mark:%"SVf"...%s\n", |
5299 | REPORT_CODE_OFF+depth*2, "", | |
be2597df | 5300 | PL_colors[4], SVfARG(sv_commit), PL_colors[5]); |
e2e6a0f1 YO |
5301 | }); |
5302 | } | |
5303 | mark_state = ST.prev_mark; | |
5d458dd8 YO |
5304 | sv_yes_mark = mark_state ? |
5305 | mark_state->u.mark.mark_name : NULL; | |
e2e6a0f1 YO |
5306 | sayNO; |
5307 | /* NOTREACHED */ | |
5d458dd8 YO |
5308 | case SKIP: |
5309 | PL_reginput = locinput; | |
5310 | if (scan->flags) { | |
2bf803e2 | 5311 | /* (*SKIP) : if we fail we cut here*/ |
5d458dd8 | 5312 | ST.mark_name = NULL; |
e2e6a0f1 | 5313 | ST.mark_loc = locinput; |
5d458dd8 YO |
5314 | PUSH_STATE_GOTO(SKIP_next,next); |
5315 | } else { | |
2bf803e2 | 5316 | /* (*SKIP:NAME) : if there is a (*MARK:NAME) fail where it was, |
5d458dd8 YO |
5317 | otherwise do nothing. Meaning we need to scan |
5318 | */ | |
5319 | regmatch_state *cur = mark_state; | |
ad64d0ec | 5320 | SV *find = MUTABLE_SV(rexi->data->data[ ARG( scan ) ]); |
5d458dd8 YO |
5321 | |
5322 | while (cur) { | |
5323 | if ( sv_eq( cur->u.mark.mark_name, | |
5324 | find ) ) | |
5325 | { | |
5326 | ST.mark_name = find; | |
5327 | PUSH_STATE_GOTO( SKIP_next, next ); | |
5328 | } | |
5329 | cur = cur->u.mark.prev_mark; | |
5330 | } | |
e2e6a0f1 | 5331 | } |
2bf803e2 | 5332 | /* Didn't find our (*MARK:NAME) so ignore this (*SKIP:NAME) */ |
5d458dd8 YO |
5333 | break; |
5334 | case SKIP_next_fail: | |
5335 | if (ST.mark_name) { | |
5336 | /* (*CUT:NAME) - Set up to search for the name as we | |
5337 | collapse the stack*/ | |
5338 | popmark = ST.mark_name; | |
5339 | } else { | |
5340 | /* (*CUT) - No name, we cut here.*/ | |
e2e6a0f1 YO |
5341 | if (ST.mark_loc > startpoint) |
5342 | reginfo->cutpoint = HOPBACKc(ST.mark_loc, 1); | |
5d458dd8 YO |
5343 | /* but we set sv_commit to latest mark_name if there |
5344 | is one so they can test to see how things lead to this | |
5345 | cut */ | |
5346 | if (mark_state) | |
5347 | sv_commit=mark_state->u.mark.mark_name; | |
5348 | } | |
e2e6a0f1 YO |
5349 | no_final = 1; |
5350 | sayNO; | |
5351 | /* NOTREACHED */ | |
5352 | #undef ST | |
32e6a07c YO |
5353 | case FOLDCHAR: |
5354 | n = ARG(scan); | |
81d4fa0f | 5355 | if ( n == (U32)what_len_TRICKYFOLD(locinput,do_utf8,ln) ) { |
e64b1bd1 YO |
5356 | locinput += ln; |
5357 | } else if ( 0xDF == n && !do_utf8 && !UTF ) { | |
5358 | sayNO; | |
5359 | } else { | |
5360 | U8 folded[UTF8_MAXBYTES_CASE+1]; | |
5361 | STRLEN foldlen; | |
5362 | const char * const l = locinput; | |
5363 | char *e = PL_regeol; | |
5364 | to_uni_fold(n, folded, &foldlen); | |
5365 | ||
4c1b470c | 5366 | if (! foldEQ_utf8((const char*) folded, 0, foldlen, 1, |
e64b1bd1 | 5367 | l, &e, 0, do_utf8)) { |
32e6a07c | 5368 | sayNO; |
e64b1bd1 YO |
5369 | } |
5370 | locinput = e; | |
32e6a07c YO |
5371 | } |
5372 | nextchr = UCHARAT(locinput); | |
5373 | break; | |
e1d1eefb YO |
5374 | case LNBREAK: |
5375 | if ((n=is_LNBREAK(locinput,do_utf8))) { | |
5376 | locinput += n; | |
5377 | nextchr = UCHARAT(locinput); | |
5378 | } else | |
5379 | sayNO; | |
5380 | break; | |
5381 | ||
5382 | #define CASE_CLASS(nAmE) \ | |
5383 | case nAmE: \ | |
5384 | if ((n=is_##nAmE(locinput,do_utf8))) { \ | |
5385 | locinput += n; \ | |
5386 | nextchr = UCHARAT(locinput); \ | |
5387 | } else \ | |
5388 | sayNO; \ | |
5389 | break; \ | |
5390 | case N##nAmE: \ | |
5391 | if ((n=is_##nAmE(locinput,do_utf8))) { \ | |
5392 | sayNO; \ | |
5393 | } else { \ | |
5394 | locinput += UTF8SKIP(locinput); \ | |
5395 | nextchr = UCHARAT(locinput); \ | |
5396 | } \ | |
5397 | break | |
5398 | ||
5399 | CASE_CLASS(VERTWS); | |
5400 | CASE_CLASS(HORIZWS); | |
5401 | #undef CASE_CLASS | |
5402 | ||
a0d0e21e | 5403 | default: |
b900a521 | 5404 | PerlIO_printf(Perl_error_log, "%"UVxf" %d\n", |
d7d93a81 | 5405 | PTR2UV(scan), OP(scan)); |
cea2e8a9 | 5406 | Perl_croak(aTHX_ "regexp memory corruption"); |
5d458dd8 YO |
5407 | |
5408 | } /* end switch */ | |
95b24440 | 5409 | |
5d458dd8 YO |
5410 | /* switch break jumps here */ |
5411 | scan = next; /* prepare to execute the next op and ... */ | |
5412 | continue; /* ... jump back to the top, reusing st */ | |
95b24440 DM |
5413 | /* NOTREACHED */ |
5414 | ||
40a82448 DM |
5415 | push_yes_state: |
5416 | /* push a state that backtracks on success */ | |
5417 | st->u.yes.prev_yes_state = yes_state; | |
5418 | yes_state = st; | |
5419 | /* FALL THROUGH */ | |
5420 | push_state: | |
5421 | /* push a new regex state, then continue at scan */ | |
5422 | { | |
5423 | regmatch_state *newst; | |
5424 | ||
24b23f37 YO |
5425 | DEBUG_STACK_r({ |
5426 | regmatch_state *cur = st; | |
5427 | regmatch_state *curyes = yes_state; | |
5428 | int curd = depth; | |
5429 | regmatch_slab *slab = PL_regmatch_slab; | |
5430 | for (;curd > -1;cur--,curd--) { | |
5431 | if (cur < SLAB_FIRST(slab)) { | |
5432 | slab = slab->prev; | |
5433 | cur = SLAB_LAST(slab); | |
5434 | } | |
5435 | PerlIO_printf(Perl_error_log, "%*s#%-3d %-10s %s\n", | |
5436 | REPORT_CODE_OFF + 2 + depth * 2,"", | |
13d6edb4 | 5437 | curd, PL_reg_name[cur->resume_state], |
24b23f37 YO |
5438 | (curyes == cur) ? "yes" : "" |
5439 | ); | |
5440 | if (curyes == cur) | |
5441 | curyes = cur->u.yes.prev_yes_state; | |
5442 | } | |
5443 | } else | |
5444 | DEBUG_STATE_pp("push") | |
5445 | ); | |
40a82448 | 5446 | depth++; |
40a82448 DM |
5447 | st->locinput = locinput; |
5448 | newst = st+1; | |
5449 | if (newst > SLAB_LAST(PL_regmatch_slab)) | |
5450 | newst = S_push_slab(aTHX); | |
5451 | PL_regmatch_state = newst; | |
786e8c11 | 5452 | |
40a82448 DM |
5453 | locinput = PL_reginput; |
5454 | nextchr = UCHARAT(locinput); | |
5455 | st = newst; | |
5456 | continue; | |
5457 | /* NOTREACHED */ | |
5458 | } | |
a0d0e21e | 5459 | } |
a687059c | 5460 | |
a0d0e21e LW |
5461 | /* |
5462 | * We get here only if there's trouble -- normally "case END" is | |
5463 | * the terminating point. | |
5464 | */ | |
cea2e8a9 | 5465 | Perl_croak(aTHX_ "corrupted regexp pointers"); |
a0d0e21e | 5466 | /*NOTREACHED*/ |
4633a7c4 LW |
5467 | sayNO; |
5468 | ||
262b90c4 | 5469 | yes: |
77cb431f DM |
5470 | if (yes_state) { |
5471 | /* we have successfully completed a subexpression, but we must now | |
5472 | * pop to the state marked by yes_state and continue from there */ | |
77cb431f | 5473 | assert(st != yes_state); |
5bc10b2c DM |
5474 | #ifdef DEBUGGING |
5475 | while (st != yes_state) { | |
5476 | st--; | |
5477 | if (st < SLAB_FIRST(PL_regmatch_slab)) { | |
5478 | PL_regmatch_slab = PL_regmatch_slab->prev; | |
5479 | st = SLAB_LAST(PL_regmatch_slab); | |
5480 | } | |
e2e6a0f1 | 5481 | DEBUG_STATE_r({ |
54612592 YO |
5482 | if (no_final) { |
5483 | DEBUG_STATE_pp("pop (no final)"); | |
5484 | } else { | |
5485 | DEBUG_STATE_pp("pop (yes)"); | |
5486 | } | |
e2e6a0f1 | 5487 | }); |
5bc10b2c DM |
5488 | depth--; |
5489 | } | |
5490 | #else | |
77cb431f DM |
5491 | while (yes_state < SLAB_FIRST(PL_regmatch_slab) |
5492 | || yes_state > SLAB_LAST(PL_regmatch_slab)) | |
5493 | { | |
5494 | /* not in this slab, pop slab */ | |
5495 | depth -= (st - SLAB_FIRST(PL_regmatch_slab) + 1); | |
5496 | PL_regmatch_slab = PL_regmatch_slab->prev; | |
5497 | st = SLAB_LAST(PL_regmatch_slab); | |
5498 | } | |
5499 | depth -= (st - yes_state); | |
5bc10b2c | 5500 | #endif |
77cb431f DM |
5501 | st = yes_state; |
5502 | yes_state = st->u.yes.prev_yes_state; | |
5503 | PL_regmatch_state = st; | |
24b23f37 | 5504 | |
5d458dd8 YO |
5505 | if (no_final) { |
5506 | locinput= st->locinput; | |
5507 | nextchr = UCHARAT(locinput); | |
5508 | } | |
54612592 | 5509 | state_num = st->resume_state + no_final; |
24d3c4a9 | 5510 | goto reenter_switch; |
77cb431f DM |
5511 | } |
5512 | ||
a3621e74 | 5513 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch successful!%s\n", |
e4584336 | 5514 | PL_colors[4], PL_colors[5])); |
02db2b7b | 5515 | |
19b95bf0 DM |
5516 | if (PL_reg_eval_set) { |
5517 | /* each successfully executed (?{...}) block does the equivalent of | |
5518 | * local $^R = do {...} | |
5519 | * When popping the save stack, all these locals would be undone; | |
5520 | * bypass this by setting the outermost saved $^R to the latest | |
5521 | * value */ | |
5522 | if (oreplsv != GvSV(PL_replgv)) | |
5523 | sv_setsv(oreplsv, GvSV(PL_replgv)); | |
5524 | } | |
95b24440 | 5525 | result = 1; |
aa283a38 | 5526 | goto final_exit; |
4633a7c4 LW |
5527 | |
5528 | no: | |
a3621e74 | 5529 | DEBUG_EXECUTE_r( |
7821416a | 5530 | PerlIO_printf(Perl_debug_log, |
786e8c11 | 5531 | "%*s %sfailed...%s\n", |
5bc10b2c | 5532 | REPORT_CODE_OFF+depth*2, "", |
786e8c11 | 5533 | PL_colors[4], PL_colors[5]) |
7821416a | 5534 | ); |
aa283a38 | 5535 | |
262b90c4 | 5536 | no_silent: |
54612592 YO |
5537 | if (no_final) { |
5538 | if (yes_state) { | |
5539 | goto yes; | |
5540 | } else { | |
5541 | goto final_exit; | |
5542 | } | |
5543 | } | |
aa283a38 DM |
5544 | if (depth) { |
5545 | /* there's a previous state to backtrack to */ | |
40a82448 DM |
5546 | st--; |
5547 | if (st < SLAB_FIRST(PL_regmatch_slab)) { | |
5548 | PL_regmatch_slab = PL_regmatch_slab->prev; | |
5549 | st = SLAB_LAST(PL_regmatch_slab); | |
5550 | } | |
5551 | PL_regmatch_state = st; | |
40a82448 DM |
5552 | locinput= st->locinput; |
5553 | nextchr = UCHARAT(locinput); | |
5554 | ||
5bc10b2c DM |
5555 | DEBUG_STATE_pp("pop"); |
5556 | depth--; | |
262b90c4 DM |
5557 | if (yes_state == st) |
5558 | yes_state = st->u.yes.prev_yes_state; | |
5bc10b2c | 5559 | |
24d3c4a9 DM |
5560 | state_num = st->resume_state + 1; /* failure = success + 1 */ |
5561 | goto reenter_switch; | |
95b24440 | 5562 | } |
24d3c4a9 | 5563 | result = 0; |
aa283a38 | 5564 | |
262b90c4 | 5565 | final_exit: |
bbe252da | 5566 | if (rex->intflags & PREGf_VERBARG_SEEN) { |
5d458dd8 YO |
5567 | SV *sv_err = get_sv("REGERROR", 1); |
5568 | SV *sv_mrk = get_sv("REGMARK", 1); | |
5569 | if (result) { | |
e2e6a0f1 | 5570 | sv_commit = &PL_sv_no; |
5d458dd8 YO |
5571 | if (!sv_yes_mark) |
5572 | sv_yes_mark = &PL_sv_yes; | |
5573 | } else { | |
5574 | if (!sv_commit) | |
5575 | sv_commit = &PL_sv_yes; | |
5576 | sv_yes_mark = &PL_sv_no; | |
5577 | } | |
5578 | sv_setsv(sv_err, sv_commit); | |
5579 | sv_setsv(sv_mrk, sv_yes_mark); | |
e2e6a0f1 | 5580 | } |
19b95bf0 | 5581 | |
2f554ef7 DM |
5582 | /* clean up; in particular, free all slabs above current one */ |
5583 | LEAVE_SCOPE(oldsave); | |
5d9a96ca | 5584 | |
95b24440 | 5585 | return result; |
a687059c LW |
5586 | } |
5587 | ||
5588 | /* | |
5589 | - regrepeat - repeatedly match something simple, report how many | |
5590 | */ | |
5591 | /* | |
5592 | * [This routine now assumes that it will only match on things of length 1. | |
5593 | * That was true before, but now we assume scan - reginput is the count, | |
a0ed51b3 | 5594 | * rather than incrementing count on every character. [Er, except utf8.]] |
a687059c | 5595 | */ |
76e3520e | 5596 | STATIC I32 |
e2e6a0f1 | 5597 | S_regrepeat(pTHX_ const regexp *prog, const regnode *p, I32 max, int depth) |
a687059c | 5598 | { |
27da23d5 | 5599 | dVAR; |
a0d0e21e | 5600 | register char *scan; |
a0d0e21e | 5601 | register I32 c; |
3280af22 | 5602 | register char *loceol = PL_regeol; |
a0ed51b3 | 5603 | register I32 hardcount = 0; |
53c4c00c | 5604 | register bool do_utf8 = PL_reg_match_utf8; |
4f55667c SP |
5605 | #ifndef DEBUGGING |
5606 | PERL_UNUSED_ARG(depth); | |
5607 | #endif | |
a0d0e21e | 5608 | |
7918f24d NC |
5609 | PERL_ARGS_ASSERT_REGREPEAT; |
5610 | ||
3280af22 | 5611 | scan = PL_reginput; |
faf11cac HS |
5612 | if (max == REG_INFTY) |
5613 | max = I32_MAX; | |
5614 | else if (max < loceol - scan) | |
7f596f4c | 5615 | loceol = scan + max; |
a0d0e21e | 5616 | switch (OP(p)) { |
22c35a8c | 5617 | case REG_ANY: |
1aa99e6b | 5618 | if (do_utf8) { |
ffc61ed2 | 5619 | loceol = PL_regeol; |
1aa99e6b | 5620 | while (scan < loceol && hardcount < max && *scan != '\n') { |
ffc61ed2 JH |
5621 | scan += UTF8SKIP(scan); |
5622 | hardcount++; | |
5623 | } | |
5624 | } else { | |
5625 | while (scan < loceol && *scan != '\n') | |
5626 | scan++; | |
a0ed51b3 LW |
5627 | } |
5628 | break; | |
ffc61ed2 | 5629 | case SANY: |
def8e4ea JH |
5630 | if (do_utf8) { |
5631 | loceol = PL_regeol; | |
a0804c9e | 5632 | while (scan < loceol && hardcount < max) { |
def8e4ea JH |
5633 | scan += UTF8SKIP(scan); |
5634 | hardcount++; | |
5635 | } | |
5636 | } | |
5637 | else | |
5638 | scan = loceol; | |
a0ed51b3 | 5639 | break; |
f33976b4 DB |
5640 | case CANY: |
5641 | scan = loceol; | |
5642 | break; | |
090f7165 JH |
5643 | case EXACT: /* length of string is 1 */ |
5644 | c = (U8)*STRING(p); | |
5645 | while (scan < loceol && UCHARAT(scan) == c) | |
5646 | scan++; | |
bbce6d69 | 5647 | break; |
5648 | case EXACTF: /* length of string is 1 */ | |
cd439c50 | 5649 | c = (U8)*STRING(p); |
bbce6d69 | 5650 | while (scan < loceol && |
22c35a8c | 5651 | (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold[c])) |
bbce6d69 | 5652 | scan++; |
5653 | break; | |
5654 | case EXACTFL: /* length of string is 1 */ | |
3280af22 | 5655 | PL_reg_flags |= RF_tainted; |
cd439c50 | 5656 | c = (U8)*STRING(p); |
bbce6d69 | 5657 | while (scan < loceol && |
22c35a8c | 5658 | (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold_locale[c])) |
a0d0e21e LW |
5659 | scan++; |
5660 | break; | |
5661 | case ANYOF: | |
ffc61ed2 JH |
5662 | if (do_utf8) { |
5663 | loceol = PL_regeol; | |
cfc92286 | 5664 | while (hardcount < max && scan < loceol && |
32fc9b6a | 5665 | reginclass(prog, p, (U8*)scan, 0, do_utf8)) { |
ffc61ed2 JH |
5666 | scan += UTF8SKIP(scan); |
5667 | hardcount++; | |
5668 | } | |
5669 | } else { | |
32fc9b6a | 5670 | while (scan < loceol && REGINCLASS(prog, p, (U8*)scan)) |
ffc61ed2 JH |
5671 | scan++; |
5672 | } | |
a0d0e21e LW |
5673 | break; |
5674 | case ALNUM: | |
1aa99e6b | 5675 | if (do_utf8) { |
ffc61ed2 | 5676 | loceol = PL_regeol; |
1a4fad37 | 5677 | LOAD_UTF8_CHARCLASS_ALNUM(); |
1aa99e6b | 5678 | while (hardcount < max && scan < loceol && |
3568d838 | 5679 | swash_fetch(PL_utf8_alnum, (U8*)scan, do_utf8)) { |
ffc61ed2 JH |
5680 | scan += UTF8SKIP(scan); |
5681 | hardcount++; | |
5682 | } | |
5683 | } else { | |
5684 | while (scan < loceol && isALNUM(*scan)) | |
5685 | scan++; | |
a0ed51b3 LW |
5686 | } |
5687 | break; | |
bbce6d69 | 5688 | case ALNUML: |
3280af22 | 5689 | PL_reg_flags |= RF_tainted; |
1aa99e6b | 5690 | if (do_utf8) { |
ffc61ed2 | 5691 | loceol = PL_regeol; |
1aa99e6b IH |
5692 | while (hardcount < max && scan < loceol && |
5693 | isALNUM_LC_utf8((U8*)scan)) { | |
ffc61ed2 JH |
5694 | scan += UTF8SKIP(scan); |
5695 | hardcount++; | |
5696 | } | |
5697 | } else { | |
5698 | while (scan < loceol && isALNUM_LC(*scan)) | |
5699 | scan++; | |
a0ed51b3 LW |
5700 | } |
5701 | break; | |
a0d0e21e | 5702 | case NALNUM: |
1aa99e6b | 5703 | if (do_utf8) { |
ffc61ed2 | 5704 | loceol = PL_regeol; |
1a4fad37 | 5705 | LOAD_UTF8_CHARCLASS_ALNUM(); |
1aa99e6b | 5706 | while (hardcount < max && scan < loceol && |
3568d838 | 5707 | !swash_fetch(PL_utf8_alnum, (U8*)scan, do_utf8)) { |
ffc61ed2 JH |
5708 | scan += UTF8SKIP(scan); |
5709 | hardcount++; | |
5710 | } | |
5711 | } else { | |
5712 | while (scan < loceol && !isALNUM(*scan)) | |
5713 | scan++; | |
a0ed51b3 LW |
5714 | } |
5715 | break; | |
bbce6d69 | 5716 | case NALNUML: |
3280af22 | 5717 | PL_reg_flags |= RF_tainted; |
1aa99e6b | 5718 | if (do_utf8) { |
ffc61ed2 | 5719 | loceol = PL_regeol; |
1aa99e6b IH |
5720 | while (hardcount < max && scan < loceol && |
5721 | !isALNUM_LC_utf8((U8*)scan)) { | |
ffc61ed2 JH |
5722 | scan += UTF8SKIP(scan); |
5723 | hardcount++; | |
5724 | } | |
5725 | } else { | |
5726 | while (scan < loceol && !isALNUM_LC(*scan)) | |
5727 | scan++; | |
a0ed51b3 LW |
5728 | } |
5729 | break; | |
a0d0e21e | 5730 | case SPACE: |
1aa99e6b | 5731 | if (do_utf8) { |
ffc61ed2 | 5732 | loceol = PL_regeol; |
1a4fad37 | 5733 | LOAD_UTF8_CHARCLASS_SPACE(); |
1aa99e6b | 5734 | while (hardcount < max && scan < loceol && |
3568d838 JH |
5735 | (*scan == ' ' || |
5736 | swash_fetch(PL_utf8_space,(U8*)scan, do_utf8))) { | |
ffc61ed2 JH |
5737 | scan += UTF8SKIP(scan); |
5738 | hardcount++; | |
5739 | } | |
5740 | } else { | |
5741 | while (scan < loceol && isSPACE(*scan)) | |
5742 | scan++; | |
a0ed51b3 LW |
5743 | } |
5744 | break; | |
bbce6d69 | 5745 | case SPACEL: |
3280af22 | 5746 | PL_reg_flags |= RF_tainted; |
1aa99e6b | 5747 | if (do_utf8) { |
ffc61ed2 | 5748 | loceol = PL_regeol; |
1aa99e6b | 5749 | while (hardcount < max && scan < loceol && |
ffc61ed2 JH |
5750 | (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) { |
5751 | scan += UTF8SKIP(scan); | |
5752 | hardcount++; | |
5753 | } | |
5754 | } else { | |
5755 | while (scan < loceol && isSPACE_LC(*scan)) | |
5756 | scan++; | |
a0ed51b3 LW |
5757 | } |
5758 | break; | |
a0d0e21e | 5759 | case NSPACE: |
1aa99e6b | 5760 | if (do_utf8) { |
ffc61ed2 | 5761 | loceol = PL_regeol; |
1a4fad37 | 5762 | LOAD_UTF8_CHARCLASS_SPACE(); |
1aa99e6b | 5763 | while (hardcount < max && scan < loceol && |
3568d838 JH |
5764 | !(*scan == ' ' || |
5765 | swash_fetch(PL_utf8_space,(U8*)scan, do_utf8))) { | |
ffc61ed2 JH |
5766 | scan += UTF8SKIP(scan); |
5767 | hardcount++; | |
5768 | } | |
5769 | } else { | |
5770 | while (scan < loceol && !isSPACE(*scan)) | |
5771 | scan++; | |
a0ed51b3 | 5772 | } |
0008a298 | 5773 | break; |
bbce6d69 | 5774 | case NSPACEL: |
3280af22 | 5775 | PL_reg_flags |= RF_tainted; |
1aa99e6b | 5776 | if (do_utf8) { |
ffc61ed2 | 5777 | loceol = PL_regeol; |
1aa99e6b | 5778 | while (hardcount < max && scan < loceol && |
ffc61ed2 JH |
5779 | !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) { |
5780 | scan += UTF8SKIP(scan); | |
5781 | hardcount++; | |
5782 | } | |
5783 | } else { | |
5784 | while (scan < loceol && !isSPACE_LC(*scan)) | |
5785 | scan++; | |
a0ed51b3 LW |
5786 | } |
5787 | break; | |
a0d0e21e | 5788 | case DIGIT: |
1aa99e6b | 5789 | if (do_utf8) { |
ffc61ed2 | 5790 | loceol = PL_regeol; |
1a4fad37 | 5791 | LOAD_UTF8_CHARCLASS_DIGIT(); |
1aa99e6b | 5792 | while (hardcount < max && scan < loceol && |
3568d838 | 5793 | swash_fetch(PL_utf8_digit, (U8*)scan, do_utf8)) { |
ffc61ed2 JH |
5794 | scan += UTF8SKIP(scan); |
5795 | hardcount++; | |
5796 | } | |
5797 | } else { | |
5798 | while (scan < loceol && isDIGIT(*scan)) | |
5799 | scan++; | |
a0ed51b3 LW |
5800 | } |
5801 | break; | |
a0d0e21e | 5802 | case NDIGIT: |
1aa99e6b | 5803 | if (do_utf8) { |
ffc61ed2 | 5804 | loceol = PL_regeol; |
1a4fad37 | 5805 | LOAD_UTF8_CHARCLASS_DIGIT(); |
1aa99e6b | 5806 | while (hardcount < max && scan < loceol && |
3568d838 | 5807 | !swash_fetch(PL_utf8_digit, (U8*)scan, do_utf8)) { |
ffc61ed2 JH |
5808 | scan += UTF8SKIP(scan); |
5809 | hardcount++; | |
5810 | } | |
5811 | } else { | |
5812 | while (scan < loceol && !isDIGIT(*scan)) | |
5813 | scan++; | |
a0ed51b3 | 5814 | } |
e1d1eefb YO |
5815 | case LNBREAK: |
5816 | if (do_utf8) { | |
5817 | loceol = PL_regeol; | |
5818 | while (hardcount < max && scan < loceol && (c=is_LNBREAK_utf8(scan))) { | |
5819 | scan += c; | |
5820 | hardcount++; | |
5821 | } | |
5822 | } else { | |
5823 | /* | |
5824 | LNBREAK can match two latin chars, which is ok, | |
5825 | because we have a null terminated string, but we | |
5826 | have to use hardcount in this situation | |
5827 | */ | |
5828 | while (scan < loceol && (c=is_LNBREAK_latin1(scan))) { | |
5829 | scan+=c; | |
5830 | hardcount++; | |
5831 | } | |
5832 | } | |
5833 | break; | |
5834 | case HORIZWS: | |
5835 | if (do_utf8) { | |
5836 | loceol = PL_regeol; | |
5837 | while (hardcount < max && scan < loceol && (c=is_HORIZWS_utf8(scan))) { | |
5838 | scan += c; | |
5839 | hardcount++; | |
5840 | } | |
5841 | } else { | |
5842 | while (scan < loceol && is_HORIZWS_latin1(scan)) | |
5843 | scan++; | |
5844 | } | |
a0ed51b3 | 5845 | break; |
e1d1eefb YO |
5846 | case NHORIZWS: |
5847 | if (do_utf8) { | |
5848 | loceol = PL_regeol; | |
5849 | while (hardcount < max && scan < loceol && !is_HORIZWS_utf8(scan)) { | |
5850 | scan += UTF8SKIP(scan); | |
5851 | hardcount++; | |
5852 | } | |
5853 | } else { | |
5854 | while (scan < loceol && !is_HORIZWS_latin1(scan)) | |
5855 | scan++; | |
5856 | ||
5857 | } | |
5858 | break; | |
5859 | case VERTWS: | |
5860 | if (do_utf8) { | |
5861 | loceol = PL_regeol; | |
5862 | while (hardcount < max && scan < loceol && (c=is_VERTWS_utf8(scan))) { | |
5863 | scan += c; | |
5864 | hardcount++; | |
5865 | } | |
5866 | } else { | |
5867 | while (scan < loceol && is_VERTWS_latin1(scan)) | |
5868 | scan++; | |
5869 | ||
5870 | } | |
5871 | break; | |
5872 | case NVERTWS: | |
5873 | if (do_utf8) { | |
5874 | loceol = PL_regeol; | |
5875 | while (hardcount < max && scan < loceol && !is_VERTWS_utf8(scan)) { | |
5876 | scan += UTF8SKIP(scan); | |
5877 | hardcount++; | |
5878 | } | |
5879 | } else { | |
5880 | while (scan < loceol && !is_VERTWS_latin1(scan)) | |
5881 | scan++; | |
5882 | ||
5883 | } | |
5884 | break; | |
5885 | ||
a0d0e21e LW |
5886 | default: /* Called on something of 0 width. */ |
5887 | break; /* So match right here or not at all. */ | |
5888 | } | |
a687059c | 5889 | |
a0ed51b3 LW |
5890 | if (hardcount) |
5891 | c = hardcount; | |
5892 | else | |
5893 | c = scan - PL_reginput; | |
3280af22 | 5894 | PL_reginput = scan; |
a687059c | 5895 | |
a3621e74 | 5896 | DEBUG_r({ |
e68ec53f | 5897 | GET_RE_DEBUG_FLAGS_DECL; |
be8e71aa | 5898 | DEBUG_EXECUTE_r({ |
e68ec53f YO |
5899 | SV * const prop = sv_newmortal(); |
5900 | regprop(prog, prop, p); | |
5901 | PerlIO_printf(Perl_debug_log, | |
be8e71aa | 5902 | "%*s %s can match %"IVdf" times out of %"IVdf"...\n", |
e2e6a0f1 | 5903 | REPORT_CODE_OFF + depth*2, "", SvPVX_const(prop),(IV)c,(IV)max); |
a3621e74 | 5904 | }); |
be8e71aa | 5905 | }); |
9041c2e3 | 5906 | |
a0d0e21e | 5907 | return(c); |
a687059c LW |
5908 | } |
5909 | ||
c277df42 | 5910 | |
be8e71aa | 5911 | #if !defined(PERL_IN_XSUB_RE) || defined(PLUGGABLE_RE_EXTENSION) |
c277df42 | 5912 | /* |
ffc61ed2 JH |
5913 | - regclass_swash - prepare the utf8 swash |
5914 | */ | |
5915 | ||
5916 | SV * | |
32fc9b6a | 5917 | Perl_regclass_swash(pTHX_ const regexp *prog, register const regnode* node, bool doinit, SV** listsvp, SV **altsvp) |
ffc61ed2 | 5918 | { |
97aff369 | 5919 | dVAR; |
9e55ce06 JH |
5920 | SV *sw = NULL; |
5921 | SV *si = NULL; | |
5922 | SV *alt = NULL; | |
f8fc2ecf YO |
5923 | RXi_GET_DECL(prog,progi); |
5924 | const struct reg_data * const data = prog ? progi->data : NULL; | |
ffc61ed2 | 5925 | |
7918f24d NC |
5926 | PERL_ARGS_ASSERT_REGCLASS_SWASH; |
5927 | ||
4f639d21 | 5928 | if (data && data->count) { |
a3b680e6 | 5929 | const U32 n = ARG(node); |
ffc61ed2 | 5930 | |
4f639d21 | 5931 | if (data->what[n] == 's') { |
ad64d0ec NC |
5932 | SV * const rv = MUTABLE_SV(data->data[n]); |
5933 | AV * const av = MUTABLE_AV(SvRV(rv)); | |
2d03de9c | 5934 | SV **const ary = AvARRAY(av); |
9e55ce06 | 5935 | SV **a, **b; |
9041c2e3 | 5936 | |
711a919c | 5937 | /* See the end of regcomp.c:S_regclass() for |
9e55ce06 JH |
5938 | * documentation of these array elements. */ |
5939 | ||
b11f357e | 5940 | si = *ary; |
fe5bfecd JH |
5941 | a = SvROK(ary[1]) ? &ary[1] : NULL; |
5942 | b = SvTYPE(ary[2]) == SVt_PVAV ? &ary[2] : NULL; | |
b11f357e | 5943 | |
ffc61ed2 JH |
5944 | if (a) |
5945 | sw = *a; | |
5946 | else if (si && doinit) { | |
5947 | sw = swash_init("utf8", "", si, 1, 0); | |
5948 | (void)av_store(av, 1, sw); | |
5949 | } | |
9e55ce06 JH |
5950 | if (b) |
5951 | alt = *b; | |
ffc61ed2 JH |
5952 | } |
5953 | } | |
5954 | ||
9e55ce06 JH |
5955 | if (listsvp) |
5956 | *listsvp = si; | |
5957 | if (altsvp) | |
5958 | *altsvp = alt; | |
ffc61ed2 JH |
5959 | |
5960 | return sw; | |
5961 | } | |
76234dfb | 5962 | #endif |
ffc61ed2 JH |
5963 | |
5964 | /* | |
ba7b4546 | 5965 | - reginclass - determine if a character falls into a character class |
832705d4 JH |
5966 | |
5967 | The n is the ANYOF regnode, the p is the target string, lenp | |
5968 | is pointer to the maximum length of how far to go in the p | |
5969 | (if the lenp is zero, UTF8SKIP(p) is used), | |
5970 | do_utf8 tells whether the target string is in UTF-8. | |
5971 | ||
bbce6d69 | 5972 | */ |
5973 | ||
76e3520e | 5974 | STATIC bool |
32fc9b6a | 5975 | S_reginclass(pTHX_ const regexp *prog, register const regnode *n, register const U8* p, STRLEN* lenp, register bool do_utf8) |
bbce6d69 | 5976 | { |
27da23d5 | 5977 | dVAR; |
a3b680e6 | 5978 | const char flags = ANYOF_FLAGS(n); |
bbce6d69 | 5979 | bool match = FALSE; |
cc07378b | 5980 | UV c = *p; |
ae9ddab8 | 5981 | STRLEN len = 0; |
9e55ce06 | 5982 | STRLEN plen; |
1aa99e6b | 5983 | |
7918f24d NC |
5984 | PERL_ARGS_ASSERT_REGINCLASS; |
5985 | ||
19f67299 TS |
5986 | if (do_utf8 && !UTF8_IS_INVARIANT(c)) { |
5987 | c = utf8n_to_uvchr(p, UTF8_MAXBYTES, &len, | |
6182169b KW |
5988 | (UTF8_ALLOW_DEFAULT & UTF8_ALLOW_ANYUV) |
5989 | | UTF8_ALLOW_FFFF | UTF8_CHECK_ONLY); | |
5990 | /* see [perl #37836] for UTF8_ALLOW_ANYUV; [perl #38293] for | |
5991 | * UTF8_ALLOW_FFFF */ | |
e8a70c6f SP |
5992 | if (len == (STRLEN)-1) |
5993 | Perl_croak(aTHX_ "Malformed UTF-8 character (fatal)"); | |
19f67299 | 5994 | } |
bbce6d69 | 5995 | |
0f0076b4 | 5996 | plen = lenp ? *lenp : UNISKIP(NATIVE_TO_UNI(c)); |
ffc61ed2 | 5997 | if (do_utf8 || (flags & ANYOF_UNICODE)) { |
9e55ce06 JH |
5998 | if (lenp) |
5999 | *lenp = 0; | |
ffc61ed2 | 6000 | if (do_utf8 && !ANYOF_RUNTIME(n)) { |
ffc61ed2 JH |
6001 | if (len != (STRLEN)-1 && c < 256 && ANYOF_BITMAP_TEST(n, c)) |
6002 | match = TRUE; | |
bbce6d69 | 6003 | } |
3568d838 | 6004 | if (!match && do_utf8 && (flags & ANYOF_UNICODE_ALL) && c >= 256) |
1aa99e6b | 6005 | match = TRUE; |
ffc61ed2 | 6006 | if (!match) { |
9e55ce06 | 6007 | AV *av; |
32fc9b6a | 6008 | SV * const sw = regclass_swash(prog, n, TRUE, 0, (SV**)&av); |
ffc61ed2 JH |
6009 | |
6010 | if (sw) { | |
3f0c5693 KW |
6011 | U8 * utf8_p; |
6012 | if (do_utf8) { | |
6013 | utf8_p = (U8 *) p; | |
6014 | } else { | |
6015 | STRLEN len = 1; | |
6016 | utf8_p = bytes_to_utf8(p, &len); | |
6017 | } | |
6018 | if (swash_fetch(sw, utf8_p, 1)) | |
ffc61ed2 JH |
6019 | match = TRUE; |
6020 | else if (flags & ANYOF_FOLD) { | |
9e55ce06 JH |
6021 | if (!match && lenp && av) { |
6022 | I32 i; | |
9e55ce06 | 6023 | for (i = 0; i <= av_len(av); i++) { |
890ce7af | 6024 | SV* const sv = *av_fetch(av, i, FALSE); |
9e55ce06 | 6025 | STRLEN len; |
890ce7af | 6026 | const char * const s = SvPV_const(sv, len); |
3f0c5693 | 6027 | if (len <= plen && memEQ(s, (char*)utf8_p, len)) { |
9e55ce06 JH |
6028 | *lenp = len; |
6029 | match = TRUE; | |
6030 | break; | |
6031 | } | |
6032 | } | |
6033 | } | |
6034 | if (!match) { | |
89ebb4a3 | 6035 | U8 tmpbuf[UTF8_MAXBYTES_CASE+1]; |
4a623e43 | 6036 | |
3f0c5693 KW |
6037 | STRLEN tmplen; |
6038 | to_utf8_fold(utf8_p, tmpbuf, &tmplen); | |
6039 | if (swash_fetch(sw, tmpbuf, 1)) | |
9e55ce06 JH |
6040 | match = TRUE; |
6041 | } | |
ffc61ed2 | 6042 | } |
b3a04dd3 KW |
6043 | |
6044 | /* If we allocated a string above, free it */ | |
6045 | if (! do_utf8) Safefree(utf8_p); | |
ffc61ed2 | 6046 | } |
bbce6d69 | 6047 | } |
9e55ce06 | 6048 | if (match && lenp && *lenp == 0) |
0f0076b4 | 6049 | *lenp = UNISKIP(NATIVE_TO_UNI(c)); |
bbce6d69 | 6050 | } |
1aa99e6b | 6051 | if (!match && c < 256) { |
ffc61ed2 JH |
6052 | if (ANYOF_BITMAP_TEST(n, c)) |
6053 | match = TRUE; | |
6054 | else if (flags & ANYOF_FOLD) { | |
eb160463 | 6055 | U8 f; |
a0ed51b3 | 6056 | |
ffc61ed2 JH |
6057 | if (flags & ANYOF_LOCALE) { |
6058 | PL_reg_flags |= RF_tainted; | |
6059 | f = PL_fold_locale[c]; | |
6060 | } | |
6061 | else | |
6062 | f = PL_fold[c]; | |
6063 | if (f != c && ANYOF_BITMAP_TEST(n, f)) | |
6064 | match = TRUE; | |
6065 | } | |
6066 | ||
6067 | if (!match && (flags & ANYOF_CLASS)) { | |
a0ed51b3 | 6068 | PL_reg_flags |= RF_tainted; |
ffc61ed2 JH |
6069 | if ( |
6070 | (ANYOF_CLASS_TEST(n, ANYOF_ALNUM) && isALNUM_LC(c)) || | |
6071 | (ANYOF_CLASS_TEST(n, ANYOF_NALNUM) && !isALNUM_LC(c)) || | |
6072 | (ANYOF_CLASS_TEST(n, ANYOF_SPACE) && isSPACE_LC(c)) || | |
6073 | (ANYOF_CLASS_TEST(n, ANYOF_NSPACE) && !isSPACE_LC(c)) || | |
6074 | (ANYOF_CLASS_TEST(n, ANYOF_DIGIT) && isDIGIT_LC(c)) || | |
6075 | (ANYOF_CLASS_TEST(n, ANYOF_NDIGIT) && !isDIGIT_LC(c)) || | |
6076 | (ANYOF_CLASS_TEST(n, ANYOF_ALNUMC) && isALNUMC_LC(c)) || | |
6077 | (ANYOF_CLASS_TEST(n, ANYOF_NALNUMC) && !isALNUMC_LC(c)) || | |
6078 | (ANYOF_CLASS_TEST(n, ANYOF_ALPHA) && isALPHA_LC(c)) || | |
6079 | (ANYOF_CLASS_TEST(n, ANYOF_NALPHA) && !isALPHA_LC(c)) || | |
6080 | (ANYOF_CLASS_TEST(n, ANYOF_ASCII) && isASCII(c)) || | |
6081 | (ANYOF_CLASS_TEST(n, ANYOF_NASCII) && !isASCII(c)) || | |
6082 | (ANYOF_CLASS_TEST(n, ANYOF_CNTRL) && isCNTRL_LC(c)) || | |
6083 | (ANYOF_CLASS_TEST(n, ANYOF_NCNTRL) && !isCNTRL_LC(c)) || | |
6084 | (ANYOF_CLASS_TEST(n, ANYOF_GRAPH) && isGRAPH_LC(c)) || | |
6085 | (ANYOF_CLASS_TEST(n, ANYOF_NGRAPH) && !isGRAPH_LC(c)) || | |
6086 | (ANYOF_CLASS_TEST(n, ANYOF_LOWER) && isLOWER_LC(c)) || | |
6087 | (ANYOF_CLASS_TEST(n, ANYOF_NLOWER) && !isLOWER_LC(c)) || | |
6088 | (ANYOF_CLASS_TEST(n, ANYOF_PRINT) && isPRINT_LC(c)) || | |
6089 | (ANYOF_CLASS_TEST(n, ANYOF_NPRINT) && !isPRINT_LC(c)) || | |
6090 | (ANYOF_CLASS_TEST(n, ANYOF_PUNCT) && isPUNCT_LC(c)) || | |
6091 | (ANYOF_CLASS_TEST(n, ANYOF_NPUNCT) && !isPUNCT_LC(c)) || | |
6092 | (ANYOF_CLASS_TEST(n, ANYOF_UPPER) && isUPPER_LC(c)) || | |
6093 | (ANYOF_CLASS_TEST(n, ANYOF_NUPPER) && !isUPPER_LC(c)) || | |
6094 | (ANYOF_CLASS_TEST(n, ANYOF_XDIGIT) && isXDIGIT(c)) || | |
6095 | (ANYOF_CLASS_TEST(n, ANYOF_NXDIGIT) && !isXDIGIT(c)) || | |
6096 | (ANYOF_CLASS_TEST(n, ANYOF_PSXSPC) && isPSXSPC(c)) || | |
6097 | (ANYOF_CLASS_TEST(n, ANYOF_NPSXSPC) && !isPSXSPC(c)) || | |
6098 | (ANYOF_CLASS_TEST(n, ANYOF_BLANK) && isBLANK(c)) || | |
6099 | (ANYOF_CLASS_TEST(n, ANYOF_NBLANK) && !isBLANK(c)) | |
6100 | ) /* How's that for a conditional? */ | |
6101 | { | |
6102 | match = TRUE; | |
6103 | } | |
a0ed51b3 | 6104 | } |
a0ed51b3 LW |
6105 | } |
6106 | ||
a0ed51b3 LW |
6107 | return (flags & ANYOF_INVERT) ? !match : match; |
6108 | } | |
161b471a | 6109 | |
dfe13c55 | 6110 | STATIC U8 * |
0ce71af7 | 6111 | S_reghop3(U8 *s, I32 off, const U8* lim) |
9041c2e3 | 6112 | { |
97aff369 | 6113 | dVAR; |
7918f24d NC |
6114 | |
6115 | PERL_ARGS_ASSERT_REGHOP3; | |
6116 | ||
a0ed51b3 | 6117 | if (off >= 0) { |
1aa99e6b | 6118 | while (off-- && s < lim) { |
ffc61ed2 | 6119 | /* XXX could check well-formedness here */ |
a0ed51b3 | 6120 | s += UTF8SKIP(s); |
ffc61ed2 | 6121 | } |
a0ed51b3 LW |
6122 | } |
6123 | else { | |
1de06328 YO |
6124 | while (off++ && s > lim) { |
6125 | s--; | |
6126 | if (UTF8_IS_CONTINUED(*s)) { | |
6127 | while (s > lim && UTF8_IS_CONTINUATION(*s)) | |
6128 | s--; | |
a0ed51b3 | 6129 | } |
1de06328 | 6130 | /* XXX could check well-formedness here */ |
a0ed51b3 LW |
6131 | } |
6132 | } | |
6133 | return s; | |
6134 | } | |
161b471a | 6135 | |
f9f4320a YO |
6136 | #ifdef XXX_dmq |
6137 | /* there are a bunch of places where we use two reghop3's that should | |
6138 | be replaced with this routine. but since thats not done yet | |
6139 | we ifdef it out - dmq | |
6140 | */ | |
dfe13c55 | 6141 | STATIC U8 * |
1de06328 YO |
6142 | S_reghop4(U8 *s, I32 off, const U8* llim, const U8* rlim) |
6143 | { | |
6144 | dVAR; | |
7918f24d NC |
6145 | |
6146 | PERL_ARGS_ASSERT_REGHOP4; | |
6147 | ||
1de06328 YO |
6148 | if (off >= 0) { |
6149 | while (off-- && s < rlim) { | |
6150 | /* XXX could check well-formedness here */ | |
6151 | s += UTF8SKIP(s); | |
6152 | } | |
6153 | } | |
6154 | else { | |
6155 | while (off++ && s > llim) { | |
6156 | s--; | |
6157 | if (UTF8_IS_CONTINUED(*s)) { | |
6158 | while (s > llim && UTF8_IS_CONTINUATION(*s)) | |
6159 | s--; | |
6160 | } | |
6161 | /* XXX could check well-formedness here */ | |
6162 | } | |
6163 | } | |
6164 | return s; | |
6165 | } | |
f9f4320a | 6166 | #endif |
1de06328 YO |
6167 | |
6168 | STATIC U8 * | |
0ce71af7 | 6169 | S_reghopmaybe3(U8* s, I32 off, const U8* lim) |
a0ed51b3 | 6170 | { |
97aff369 | 6171 | dVAR; |
7918f24d NC |
6172 | |
6173 | PERL_ARGS_ASSERT_REGHOPMAYBE3; | |
6174 | ||
a0ed51b3 | 6175 | if (off >= 0) { |
1aa99e6b | 6176 | while (off-- && s < lim) { |
ffc61ed2 | 6177 | /* XXX could check well-formedness here */ |
a0ed51b3 | 6178 | s += UTF8SKIP(s); |
ffc61ed2 | 6179 | } |
a0ed51b3 | 6180 | if (off >= 0) |
3dab1dad | 6181 | return NULL; |
a0ed51b3 LW |
6182 | } |
6183 | else { | |
1de06328 YO |
6184 | while (off++ && s > lim) { |
6185 | s--; | |
6186 | if (UTF8_IS_CONTINUED(*s)) { | |
6187 | while (s > lim && UTF8_IS_CONTINUATION(*s)) | |
6188 | s--; | |
a0ed51b3 | 6189 | } |
1de06328 | 6190 | /* XXX could check well-formedness here */ |
a0ed51b3 LW |
6191 | } |
6192 | if (off <= 0) | |
3dab1dad | 6193 | return NULL; |
a0ed51b3 LW |
6194 | } |
6195 | return s; | |
6196 | } | |
51371543 | 6197 | |
51371543 | 6198 | static void |
acfe0abc | 6199 | restore_pos(pTHX_ void *arg) |
51371543 | 6200 | { |
97aff369 | 6201 | dVAR; |
097eb12c | 6202 | regexp * const rex = (regexp *)arg; |
51371543 GS |
6203 | if (PL_reg_eval_set) { |
6204 | if (PL_reg_oldsaved) { | |
4f639d21 DM |
6205 | rex->subbeg = PL_reg_oldsaved; |
6206 | rex->sublen = PL_reg_oldsavedlen; | |
f8c7b90f | 6207 | #ifdef PERL_OLD_COPY_ON_WRITE |
4f639d21 | 6208 | rex->saved_copy = PL_nrs; |
ed252734 | 6209 | #endif |
07bc277f | 6210 | RXp_MATCH_COPIED_on(rex); |
51371543 GS |
6211 | } |
6212 | PL_reg_magic->mg_len = PL_reg_oldpos; | |
6213 | PL_reg_eval_set = 0; | |
6214 | PL_curpm = PL_reg_oldcurpm; | |
6215 | } | |
6216 | } | |
33b8afdf JH |
6217 | |
6218 | STATIC void | |
6219 | S_to_utf8_substr(pTHX_ register regexp *prog) | |
6220 | { | |
a1cac82e | 6221 | int i = 1; |
7918f24d NC |
6222 | |
6223 | PERL_ARGS_ASSERT_TO_UTF8_SUBSTR; | |
6224 | ||
a1cac82e NC |
6225 | do { |
6226 | if (prog->substrs->data[i].substr | |
6227 | && !prog->substrs->data[i].utf8_substr) { | |
6228 | SV* const sv = newSVsv(prog->substrs->data[i].substr); | |
6229 | prog->substrs->data[i].utf8_substr = sv; | |
6230 | sv_utf8_upgrade(sv); | |
610460f9 NC |
6231 | if (SvVALID(prog->substrs->data[i].substr)) { |
6232 | const U8 flags = BmFLAGS(prog->substrs->data[i].substr); | |
6233 | if (flags & FBMcf_TAIL) { | |
6234 | /* Trim the trailing \n that fbm_compile added last | |
6235 | time. */ | |
6236 | SvCUR_set(sv, SvCUR(sv) - 1); | |
6237 | /* Whilst this makes the SV technically "invalid" (as its | |
6238 | buffer is no longer followed by "\0") when fbm_compile() | |
6239 | adds the "\n" back, a "\0" is restored. */ | |
6240 | } | |
6241 | fbm_compile(sv, flags); | |
6242 | } | |
a1cac82e NC |
6243 | if (prog->substrs->data[i].substr == prog->check_substr) |
6244 | prog->check_utf8 = sv; | |
6245 | } | |
6246 | } while (i--); | |
33b8afdf JH |
6247 | } |
6248 | ||
6249 | STATIC void | |
6250 | S_to_byte_substr(pTHX_ register regexp *prog) | |
6251 | { | |
97aff369 | 6252 | dVAR; |
a1cac82e | 6253 | int i = 1; |
7918f24d NC |
6254 | |
6255 | PERL_ARGS_ASSERT_TO_BYTE_SUBSTR; | |
6256 | ||
a1cac82e NC |
6257 | do { |
6258 | if (prog->substrs->data[i].utf8_substr | |
6259 | && !prog->substrs->data[i].substr) { | |
6260 | SV* sv = newSVsv(prog->substrs->data[i].utf8_substr); | |
6261 | if (sv_utf8_downgrade(sv, TRUE)) { | |
610460f9 NC |
6262 | if (SvVALID(prog->substrs->data[i].utf8_substr)) { |
6263 | const U8 flags | |
6264 | = BmFLAGS(prog->substrs->data[i].utf8_substr); | |
6265 | if (flags & FBMcf_TAIL) { | |
6266 | /* Trim the trailing \n that fbm_compile added last | |
6267 | time. */ | |
6268 | SvCUR_set(sv, SvCUR(sv) - 1); | |
6269 | } | |
6270 | fbm_compile(sv, flags); | |
6271 | } | |
a1cac82e NC |
6272 | } else { |
6273 | SvREFCNT_dec(sv); | |
6274 | sv = &PL_sv_undef; | |
6275 | } | |
6276 | prog->substrs->data[i].substr = sv; | |
6277 | if (prog->substrs->data[i].utf8_substr == prog->check_utf8) | |
6278 | prog->check_substr = sv; | |
33b8afdf | 6279 | } |
a1cac82e | 6280 | } while (i--); |
33b8afdf | 6281 | } |
66610fdd RGS |
6282 | |
6283 | /* | |
6284 | * Local variables: | |
6285 | * c-indentation-style: bsd | |
6286 | * c-basic-offset: 4 | |
6287 | * indent-tabs-mode: t | |
6288 | * End: | |
6289 | * | |
37442d52 RGS |
6290 | * ex: set ts=8 sts=4 sw=4 noet: |
6291 | */ |