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 | |
299 | #define REGCP_FRAME_ELEMS 2 | |
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; |
620d5b66 | 309 | const unsigned int total_elems = paren_elems_to_push + REGCP_OTHER_ELEMS; |
a0d0e21e | 310 | int p; |
40a82448 | 311 | GET_RE_DEBUG_FLAGS_DECL; |
a0d0e21e | 312 | |
e49a9654 IH |
313 | if (paren_elems_to_push < 0) |
314 | Perl_croak(aTHX_ "panic: paren_elems_to_push < 0"); | |
315 | ||
620d5b66 | 316 | SSGROW(total_elems + REGCP_FRAME_ELEMS); |
7f69552c | 317 | |
3280af22 | 318 | for (p = PL_regsize; p > parenfloor; p--) { |
b1ce53c5 | 319 | /* REGCP_PARENS_ELEMS are pushed per pairs of parentheses. */ |
f0ab9afb NC |
320 | SSPUSHINT(PL_regoffs[p].end); |
321 | SSPUSHINT(PL_regoffs[p].start); | |
3280af22 | 322 | SSPUSHPTR(PL_reg_start_tmp[p]); |
a0d0e21e | 323 | SSPUSHINT(p); |
e7707071 | 324 | DEBUG_BUFFERS_r(PerlIO_printf(Perl_debug_log, |
40a82448 | 325 | " saving \\%"UVuf" %"IVdf"(%"IVdf")..%"IVdf"\n", |
f0ab9afb | 326 | (UV)p, (IV)PL_regoffs[p].start, |
40a82448 | 327 | (IV)(PL_reg_start_tmp[p] - PL_bostr), |
f0ab9afb | 328 | (IV)PL_regoffs[p].end |
40a82448 | 329 | )); |
a0d0e21e | 330 | } |
b1ce53c5 | 331 | /* REGCP_OTHER_ELEMS are pushed in any case, parentheses or no. */ |
f0ab9afb | 332 | SSPUSHPTR(PL_regoffs); |
3280af22 NIS |
333 | SSPUSHINT(PL_regsize); |
334 | SSPUSHINT(*PL_reglastparen); | |
a01268b5 | 335 | SSPUSHINT(*PL_reglastcloseparen); |
3280af22 | 336 | SSPUSHPTR(PL_reginput); |
620d5b66 | 337 | SSPUSHINT(total_elems); |
c6bf6a65 | 338 | SSPUSHUV(SAVEt_REGCONTEXT); /* Magic cookie. */ |
41123dfd | 339 | |
a0d0e21e LW |
340 | return retval; |
341 | } | |
342 | ||
c277df42 | 343 | /* These are needed since we do not localize EVAL nodes: */ |
ab3bbdeb YO |
344 | #define REGCP_SET(cp) \ |
345 | DEBUG_STATE_r( \ | |
ab3bbdeb | 346 | PerlIO_printf(Perl_debug_log, \ |
e4f74956 | 347 | " Setting an EVAL scope, savestack=%"IVdf"\n", \ |
ab3bbdeb YO |
348 | (IV)PL_savestack_ix)); \ |
349 | cp = PL_savestack_ix | |
c3464db5 | 350 | |
ab3bbdeb | 351 | #define REGCP_UNWIND(cp) \ |
e4f74956 | 352 | DEBUG_STATE_r( \ |
ab3bbdeb | 353 | if (cp != PL_savestack_ix) \ |
e4f74956 YO |
354 | PerlIO_printf(Perl_debug_log, \ |
355 | " Clearing an EVAL scope, savestack=%"IVdf"..%"IVdf"\n", \ | |
ab3bbdeb YO |
356 | (IV)(cp), (IV)PL_savestack_ix)); \ |
357 | regcpblow(cp) | |
c277df42 | 358 | |
76e3520e | 359 | STATIC char * |
097eb12c | 360 | S_regcppop(pTHX_ const regexp *rex) |
a0d0e21e | 361 | { |
97aff369 | 362 | dVAR; |
3b6647e0 | 363 | U32 i; |
a0d0e21e | 364 | char *input; |
a3621e74 YO |
365 | GET_RE_DEBUG_FLAGS_DECL; |
366 | ||
7918f24d NC |
367 | PERL_ARGS_ASSERT_REGCPPOP; |
368 | ||
b1ce53c5 | 369 | /* Pop REGCP_OTHER_ELEMS before the parentheses loop starts. */ |
c6bf6a65 | 370 | i = SSPOPUV; |
b1ce53c5 JH |
371 | assert(i == SAVEt_REGCONTEXT); /* Check that the magic cookie is there. */ |
372 | i = SSPOPINT; /* Parentheses elements to pop. */ | |
a0d0e21e | 373 | input = (char *) SSPOPPTR; |
a01268b5 | 374 | *PL_reglastcloseparen = SSPOPINT; |
3280af22 NIS |
375 | *PL_reglastparen = SSPOPINT; |
376 | PL_regsize = SSPOPINT; | |
f0ab9afb | 377 | PL_regoffs=(regexp_paren_pair *) SSPOPPTR; |
b1ce53c5 | 378 | |
620d5b66 | 379 | i -= REGCP_OTHER_ELEMS; |
b1ce53c5 | 380 | /* Now restore the parentheses context. */ |
620d5b66 | 381 | for ( ; i > 0; i -= REGCP_PAREN_ELEMS) { |
1df70142 | 382 | I32 tmps; |
097eb12c | 383 | U32 paren = (U32)SSPOPINT; |
3280af22 | 384 | PL_reg_start_tmp[paren] = (char *) SSPOPPTR; |
f0ab9afb | 385 | PL_regoffs[paren].start = SSPOPINT; |
cf93c79d | 386 | tmps = SSPOPINT; |
3280af22 | 387 | if (paren <= *PL_reglastparen) |
f0ab9afb | 388 | PL_regoffs[paren].end = tmps; |
e7707071 | 389 | DEBUG_BUFFERS_r( |
c3464db5 | 390 | PerlIO_printf(Perl_debug_log, |
b900a521 | 391 | " restoring \\%"UVuf" to %"IVdf"(%"IVdf")..%"IVdf"%s\n", |
f0ab9afb | 392 | (UV)paren, (IV)PL_regoffs[paren].start, |
b900a521 | 393 | (IV)(PL_reg_start_tmp[paren] - PL_bostr), |
f0ab9afb | 394 | (IV)PL_regoffs[paren].end, |
3280af22 | 395 | (paren > *PL_reglastparen ? "(no)" : "")); |
c277df42 | 396 | ); |
a0d0e21e | 397 | } |
e7707071 | 398 | DEBUG_BUFFERS_r( |
bb7a0f54 | 399 | if (*PL_reglastparen + 1 <= rex->nparens) { |
c3464db5 | 400 | PerlIO_printf(Perl_debug_log, |
faccc32b | 401 | " restoring \\%"IVdf"..\\%"IVdf" to undef\n", |
4f639d21 | 402 | (IV)(*PL_reglastparen + 1), (IV)rex->nparens); |
c277df42 IZ |
403 | } |
404 | ); | |
daf18116 | 405 | #if 1 |
dafc8851 JH |
406 | /* It would seem that the similar code in regtry() |
407 | * already takes care of this, and in fact it is in | |
408 | * a better location to since this code can #if 0-ed out | |
409 | * but the code in regtry() is needed or otherwise tests | |
410 | * requiring null fields (pat.t#187 and split.t#{13,14} | |
daf18116 JH |
411 | * (as of patchlevel 7877) will fail. Then again, |
412 | * this code seems to be necessary or otherwise | |
225593e1 DM |
413 | * this erroneously leaves $1 defined: "1" =~ /^(?:(\d)x)?\d$/ |
414 | * --jhi updated by dapm */ | |
3b6647e0 | 415 | for (i = *PL_reglastparen + 1; i <= rex->nparens; i++) { |
097eb12c | 416 | if (i > PL_regsize) |
f0ab9afb NC |
417 | PL_regoffs[i].start = -1; |
418 | PL_regoffs[i].end = -1; | |
a0d0e21e | 419 | } |
dafc8851 | 420 | #endif |
a0d0e21e LW |
421 | return input; |
422 | } | |
423 | ||
02db2b7b | 424 | #define regcpblow(cp) LEAVE_SCOPE(cp) /* Ignores regcppush()ed data. */ |
a0d0e21e | 425 | |
a687059c | 426 | /* |
e50aee73 | 427 | * pregexec and friends |
a687059c LW |
428 | */ |
429 | ||
76234dfb | 430 | #ifndef PERL_IN_XSUB_RE |
a687059c | 431 | /* |
c277df42 | 432 | - pregexec - match a regexp against a string |
a687059c | 433 | */ |
c277df42 | 434 | I32 |
49d7dfbc | 435 | Perl_pregexec(pTHX_ REGEXP * const prog, char* stringarg, register char *strend, |
c3464db5 | 436 | char *strbeg, I32 minend, SV *screamer, U32 nosave) |
c277df42 IZ |
437 | /* strend: pointer to null at end of string */ |
438 | /* strbeg: real beginning of string */ | |
439 | /* minend: end of match must be >=minend after stringarg. */ | |
440 | /* nosave: For optimizations. */ | |
441 | { | |
7918f24d NC |
442 | PERL_ARGS_ASSERT_PREGEXEC; |
443 | ||
c277df42 | 444 | return |
9041c2e3 | 445 | regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL, |
c277df42 IZ |
446 | nosave ? 0 : REXEC_COPY_STR); |
447 | } | |
76234dfb | 448 | #endif |
22e551b9 | 449 | |
9041c2e3 | 450 | /* |
cad2e5aa JH |
451 | * Need to implement the following flags for reg_anch: |
452 | * | |
453 | * USE_INTUIT_NOML - Useful to call re_intuit_start() first | |
454 | * USE_INTUIT_ML | |
455 | * INTUIT_AUTORITATIVE_NOML - Can trust a positive answer | |
456 | * INTUIT_AUTORITATIVE_ML | |
457 | * INTUIT_ONCE_NOML - Intuit can match in one location only. | |
458 | * INTUIT_ONCE_ML | |
459 | * | |
460 | * Another flag for this function: SECOND_TIME (so that float substrs | |
461 | * with giant delta may be not rechecked). | |
462 | */ | |
463 | ||
464 | /* Assumptions: if ANCH_GPOS, then strpos is anchored. XXXX Check GPOS logic */ | |
465 | ||
3f7c398e | 466 | /* If SCREAM, then SvPVX_const(sv) should be compatible with strpos and strend. |
cad2e5aa JH |
467 | Otherwise, only SvCUR(sv) is used to get strbeg. */ |
468 | ||
469 | /* XXXX We assume that strpos is strbeg unless sv. */ | |
470 | ||
6eb5f6b9 JH |
471 | /* XXXX Some places assume that there is a fixed substring. |
472 | An update may be needed if optimizer marks as "INTUITable" | |
473 | RExen without fixed substrings. Similarly, it is assumed that | |
474 | lengths of all the strings are no more than minlen, thus they | |
475 | cannot come from lookahead. | |
40d049e4 YO |
476 | (Or minlen should take into account lookahead.) |
477 | NOTE: Some of this comment is not correct. minlen does now take account | |
478 | of lookahead/behind. Further research is required. -- demerphq | |
479 | ||
480 | */ | |
6eb5f6b9 | 481 | |
2c2d71f5 JH |
482 | /* A failure to find a constant substring means that there is no need to make |
483 | an expensive call to REx engine, thus we celebrate a failure. Similarly, | |
484 | finding a substring too deep into the string means that less calls to | |
30944b6d IZ |
485 | regtry() should be needed. |
486 | ||
487 | REx compiler's optimizer found 4 possible hints: | |
488 | a) Anchored substring; | |
489 | b) Fixed substring; | |
490 | c) Whether we are anchored (beginning-of-line or \G); | |
491 | d) First node (of those at offset 0) which may distingush positions; | |
6eb5f6b9 | 492 | We use a)b)d) and multiline-part of c), and try to find a position in the |
30944b6d IZ |
493 | string which does not contradict any of them. |
494 | */ | |
2c2d71f5 | 495 | |
6eb5f6b9 JH |
496 | /* Most of decisions we do here should have been done at compile time. |
497 | The nodes of the REx which we used for the search should have been | |
498 | deleted from the finite automaton. */ | |
499 | ||
cad2e5aa | 500 | char * |
288b8c02 | 501 | Perl_re_intuit_start(pTHX_ REGEXP * const rx, SV *sv, char *strpos, |
9f61653a | 502 | char *strend, const U32 flags, re_scream_pos_data *data) |
cad2e5aa | 503 | { |
97aff369 | 504 | dVAR; |
288b8c02 | 505 | struct regexp *const prog = (struct regexp *)SvANY(rx); |
b7953727 | 506 | register I32 start_shift = 0; |
cad2e5aa | 507 | /* Should be nonnegative! */ |
b7953727 | 508 | register I32 end_shift = 0; |
2c2d71f5 JH |
509 | register char *s; |
510 | register SV *check; | |
a1933d95 | 511 | char *strbeg; |
cad2e5aa | 512 | char *t; |
1de06328 | 513 | const bool do_utf8 = (sv && SvUTF8(sv)) ? 1 : 0; /* if no sv we have to assume bytes */ |
cad2e5aa | 514 | I32 ml_anch; |
bd61b366 SS |
515 | register char *other_last = NULL; /* other substr checked before this */ |
516 | char *check_at = NULL; /* check substr found at this pos */ | |
bbe252da | 517 | const I32 multiline = prog->extflags & RXf_PMf_MULTILINE; |
f8fc2ecf | 518 | RXi_GET_DECL(prog,progi); |
30944b6d | 519 | #ifdef DEBUGGING |
890ce7af | 520 | const char * const i_strpos = strpos; |
30944b6d | 521 | #endif |
a3621e74 YO |
522 | GET_RE_DEBUG_FLAGS_DECL; |
523 | ||
7918f24d NC |
524 | PERL_ARGS_ASSERT_RE_INTUIT_START; |
525 | ||
288b8c02 | 526 | RX_MATCH_UTF8_set(rx,do_utf8); |
cad2e5aa | 527 | |
3c8556c3 | 528 | if (RX_UTF8(rx)) { |
b8d68ded JH |
529 | PL_reg_flags |= RF_utf8; |
530 | } | |
ab3bbdeb | 531 | DEBUG_EXECUTE_r( |
efd26800 | 532 | debug_start_match(rx, do_utf8, strpos, strend, |
1de06328 YO |
533 | sv ? "Guessing start of match in sv for" |
534 | : "Guessing start of match in string for"); | |
2a782b5b | 535 | ); |
cad2e5aa | 536 | |
c344f387 JH |
537 | /* CHR_DIST() would be more correct here but it makes things slow. */ |
538 | if (prog->minlen > strend - strpos) { | |
a3621e74 | 539 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, |
a72c7584 | 540 | "String too short... [re_intuit_start]\n")); |
cad2e5aa | 541 | goto fail; |
2c2d71f5 | 542 | } |
1de06328 | 543 | |
a1933d95 | 544 | strbeg = (sv && SvPOK(sv)) ? strend - SvCUR(sv) : strpos; |
1aa99e6b | 545 | PL_regeol = strend; |
33b8afdf JH |
546 | if (do_utf8) { |
547 | if (!prog->check_utf8 && prog->check_substr) | |
548 | to_utf8_substr(prog); | |
549 | check = prog->check_utf8; | |
550 | } else { | |
551 | if (!prog->check_substr && prog->check_utf8) | |
552 | to_byte_substr(prog); | |
553 | check = prog->check_substr; | |
554 | } | |
1de06328 | 555 | if (check == &PL_sv_undef) { |
a3621e74 | 556 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, |
1de06328 | 557 | "Non-utf8 string cannot match utf8 check string\n")); |
33b8afdf JH |
558 | goto fail; |
559 | } | |
bbe252da YO |
560 | if (prog->extflags & RXf_ANCH) { /* Match at beg-of-str or after \n */ |
561 | ml_anch = !( (prog->extflags & RXf_ANCH_SINGLE) | |
562 | || ( (prog->extflags & RXf_ANCH_BOL) | |
7fba1cd6 | 563 | && !multiline ) ); /* Check after \n? */ |
cad2e5aa | 564 | |
7e25d62c | 565 | if (!ml_anch) { |
bbe252da YO |
566 | if ( !(prog->extflags & RXf_ANCH_GPOS) /* Checked by the caller */ |
567 | && !(prog->intflags & PREGf_IMPLICIT) /* not a real BOL */ | |
3f7c398e | 568 | /* SvCUR is not set on references: SvRV and SvPVX_const overlap */ |
7e25d62c JH |
569 | && sv && !SvROK(sv) |
570 | && (strpos != strbeg)) { | |
a3621e74 | 571 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Not at start...\n")); |
7e25d62c JH |
572 | goto fail; |
573 | } | |
574 | if (prog->check_offset_min == prog->check_offset_max && | |
bbe252da | 575 | !(prog->extflags & RXf_CANY_SEEN)) { |
2c2d71f5 | 576 | /* Substring at constant offset from beg-of-str... */ |
cad2e5aa JH |
577 | I32 slen; |
578 | ||
1aa99e6b | 579 | s = HOP3c(strpos, prog->check_offset_min, strend); |
1de06328 | 580 | |
653099ff GS |
581 | if (SvTAIL(check)) { |
582 | slen = SvCUR(check); /* >= 1 */ | |
cad2e5aa | 583 | |
9041c2e3 | 584 | if ( strend - s > slen || strend - s < slen - 1 |
2c2d71f5 | 585 | || (strend - s == slen && strend[-1] != '\n')) { |
a3621e74 | 586 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "String too long...\n")); |
2c2d71f5 | 587 | goto fail_finish; |
cad2e5aa JH |
588 | } |
589 | /* Now should match s[0..slen-2] */ | |
590 | slen--; | |
3f7c398e | 591 | if (slen && (*SvPVX_const(check) != *s |
cad2e5aa | 592 | || (slen > 1 |
3f7c398e | 593 | && memNE(SvPVX_const(check), s, slen)))) { |
2c2d71f5 | 594 | report_neq: |
a3621e74 | 595 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "String not equal...\n")); |
2c2d71f5 JH |
596 | goto fail_finish; |
597 | } | |
cad2e5aa | 598 | } |
3f7c398e | 599 | else if (*SvPVX_const(check) != *s |
653099ff | 600 | || ((slen = SvCUR(check)) > 1 |
3f7c398e | 601 | && memNE(SvPVX_const(check), s, slen))) |
2c2d71f5 | 602 | goto report_neq; |
c315bfe8 | 603 | check_at = s; |
2c2d71f5 | 604 | goto success_at_start; |
7e25d62c | 605 | } |
cad2e5aa | 606 | } |
2c2d71f5 | 607 | /* Match is anchored, but substr is not anchored wrt beg-of-str. */ |
cad2e5aa | 608 | s = strpos; |
2c2d71f5 | 609 | start_shift = prog->check_offset_min; /* okay to underestimate on CC */ |
1de06328 YO |
610 | end_shift = prog->check_end_shift; |
611 | ||
2c2d71f5 | 612 | if (!ml_anch) { |
a3b680e6 | 613 | const I32 end = prog->check_offset_max + CHR_SVLEN(check) |
653099ff | 614 | - (SvTAIL(check) != 0); |
a3b680e6 | 615 | const I32 eshift = CHR_DIST((U8*)strend, (U8*)s) - end; |
2c2d71f5 JH |
616 | |
617 | if (end_shift < eshift) | |
618 | end_shift = eshift; | |
619 | } | |
cad2e5aa | 620 | } |
2c2d71f5 | 621 | else { /* Can match at random position */ |
cad2e5aa JH |
622 | ml_anch = 0; |
623 | s = strpos; | |
1de06328 YO |
624 | start_shift = prog->check_offset_min; /* okay to underestimate on CC */ |
625 | end_shift = prog->check_end_shift; | |
626 | ||
627 | /* end shift should be non negative here */ | |
cad2e5aa JH |
628 | } |
629 | ||
bcdf7404 | 630 | #ifdef QDEBUGGING /* 7/99: reports of failure (with the older version) */ |
0033605d | 631 | if (end_shift < 0) |
1de06328 | 632 | Perl_croak(aTHX_ "panic: end_shift: %"IVdf" pattern:\n%s\n ", |
220fc49f | 633 | (IV)end_shift, RX_PRECOMP(prog)); |
2c2d71f5 JH |
634 | #endif |
635 | ||
2c2d71f5 JH |
636 | restart: |
637 | /* Find a possible match in the region s..strend by looking for | |
638 | the "check" substring in the region corrected by start/end_shift. */ | |
1de06328 YO |
639 | |
640 | { | |
641 | I32 srch_start_shift = start_shift; | |
642 | I32 srch_end_shift = end_shift; | |
643 | if (srch_start_shift < 0 && strbeg - s > srch_start_shift) { | |
644 | srch_end_shift -= ((strbeg - s) - srch_start_shift); | |
645 | srch_start_shift = strbeg - s; | |
646 | } | |
6bda09f9 | 647 | DEBUG_OPTIMISE_MORE_r({ |
1de06328 YO |
648 | PerlIO_printf(Perl_debug_log, "Check offset min: %"IVdf" Start shift: %"IVdf" End shift %"IVdf" Real End Shift: %"IVdf"\n", |
649 | (IV)prog->check_offset_min, | |
650 | (IV)srch_start_shift, | |
651 | (IV)srch_end_shift, | |
652 | (IV)prog->check_end_shift); | |
653 | }); | |
654 | ||
cad2e5aa | 655 | if (flags & REXEC_SCREAM) { |
cad2e5aa | 656 | I32 p = -1; /* Internal iterator of scream. */ |
a3b680e6 | 657 | I32 * const pp = data ? data->scream_pos : &p; |
cad2e5aa | 658 | |
2c2d71f5 JH |
659 | if (PL_screamfirst[BmRARE(check)] >= 0 |
660 | || ( BmRARE(check) == '\n' | |
85c508c3 | 661 | && (BmPREVIOUS(check) == SvCUR(check) - 1) |
2c2d71f5 | 662 | && SvTAIL(check) )) |
9041c2e3 | 663 | s = screaminstr(sv, check, |
1de06328 | 664 | srch_start_shift + (s - strbeg), srch_end_shift, pp, 0); |
cad2e5aa | 665 | else |
2c2d71f5 | 666 | goto fail_finish; |
4addbd3b | 667 | /* we may be pointing at the wrong string */ |
07bc277f | 668 | if (s && RXp_MATCH_COPIED(prog)) |
3f7c398e | 669 | s = strbeg + (s - SvPVX_const(sv)); |
cad2e5aa JH |
670 | if (data) |
671 | *data->scream_olds = s; | |
672 | } | |
1de06328 YO |
673 | else { |
674 | U8* start_point; | |
675 | U8* end_point; | |
bbe252da | 676 | if (prog->extflags & RXf_CANY_SEEN) { |
1de06328 YO |
677 | start_point= (U8*)(s + srch_start_shift); |
678 | end_point= (U8*)(strend - srch_end_shift); | |
679 | } else { | |
680 | start_point= HOP3(s, srch_start_shift, srch_start_shift < 0 ? strbeg : strend); | |
681 | end_point= HOP3(strend, -srch_end_shift, strbeg); | |
682 | } | |
6bda09f9 | 683 | DEBUG_OPTIMISE_MORE_r({ |
56570a2c | 684 | PerlIO_printf(Perl_debug_log, "fbm_instr len=%d str=<%.*s>\n", |
1de06328 | 685 | (int)(end_point - start_point), |
fc8cd66c | 686 | (int)(end_point - start_point) > 20 ? 20 : (int)(end_point - start_point), |
1de06328 YO |
687 | start_point); |
688 | }); | |
689 | ||
690 | s = fbm_instr( start_point, end_point, | |
7fba1cd6 | 691 | check, multiline ? FBMrf_MULTILINE : 0); |
1de06328 YO |
692 | } |
693 | } | |
cad2e5aa JH |
694 | /* Update the count-of-usability, remove useless subpatterns, |
695 | unshift s. */ | |
2c2d71f5 | 696 | |
ab3bbdeb YO |
697 | DEBUG_EXECUTE_r({ |
698 | RE_PV_QUOTED_DECL(quoted, do_utf8, PERL_DEBUG_PAD_ZERO(0), | |
699 | SvPVX_const(check), RE_SV_DUMPLEN(check), 30); | |
700 | PerlIO_printf(Perl_debug_log, "%s %s substr %s%s%s", | |
2c2d71f5 | 701 | (s ? "Found" : "Did not find"), |
ab3bbdeb YO |
702 | (check == (do_utf8 ? prog->anchored_utf8 : prog->anchored_substr) |
703 | ? "anchored" : "floating"), | |
704 | quoted, | |
705 | RE_SV_TAIL(check), | |
706 | (s ? " at offset " : "...\n") ); | |
707 | }); | |
2c2d71f5 JH |
708 | |
709 | if (!s) | |
710 | goto fail_finish; | |
2c2d71f5 | 711 | /* Finish the diagnostic message */ |
a3621e74 | 712 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%ld...\n", (long)(s - i_strpos)) ); |
2c2d71f5 | 713 | |
1de06328 YO |
714 | /* XXX dmq: first branch is for positive lookbehind... |
715 | Our check string is offset from the beginning of the pattern. | |
716 | So we need to do any stclass tests offset forward from that | |
717 | point. I think. :-( | |
718 | */ | |
719 | ||
720 | ||
721 | ||
722 | check_at=s; | |
723 | ||
724 | ||
2c2d71f5 JH |
725 | /* Got a candidate. Check MBOL anchoring, and the *other* substr. |
726 | Start with the other substr. | |
727 | XXXX no SCREAM optimization yet - and a very coarse implementation | |
a0288114 | 728 | XXXX /ttx+/ results in anchored="ttx", floating="x". floating will |
2c2d71f5 JH |
729 | *always* match. Probably should be marked during compile... |
730 | Probably it is right to do no SCREAM here... | |
731 | */ | |
732 | ||
1de06328 YO |
733 | if (do_utf8 ? (prog->float_utf8 && prog->anchored_utf8) |
734 | : (prog->float_substr && prog->anchored_substr)) | |
735 | { | |
30944b6d | 736 | /* Take into account the "other" substring. */ |
2c2d71f5 JH |
737 | /* XXXX May be hopelessly wrong for UTF... */ |
738 | if (!other_last) | |
6eb5f6b9 | 739 | other_last = strpos; |
33b8afdf | 740 | if (check == (do_utf8 ? prog->float_utf8 : prog->float_substr)) { |
30944b6d IZ |
741 | do_other_anchored: |
742 | { | |
890ce7af AL |
743 | char * const last = HOP3c(s, -start_shift, strbeg); |
744 | char *last1, *last2; | |
be8e71aa | 745 | char * const saved_s = s; |
33b8afdf | 746 | SV* must; |
2c2d71f5 | 747 | |
2c2d71f5 JH |
748 | t = s - prog->check_offset_max; |
749 | if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */ | |
1d86a7f9 | 750 | && (!do_utf8 |
0ce71af7 | 751 | || ((t = (char*)reghopmaybe3((U8*)s, -(prog->check_offset_max), (U8*)strpos)) |
2c2d71f5 | 752 | && t > strpos))) |
6f207bd3 | 753 | NOOP; |
2c2d71f5 JH |
754 | else |
755 | t = strpos; | |
1aa99e6b | 756 | t = HOP3c(t, prog->anchored_offset, strend); |
6eb5f6b9 JH |
757 | if (t < other_last) /* These positions already checked */ |
758 | t = other_last; | |
1aa99e6b | 759 | last2 = last1 = HOP3c(strend, -prog->minlen, strbeg); |
2c2d71f5 JH |
760 | if (last < last1) |
761 | last1 = last; | |
1de06328 YO |
762 | /* XXXX It is not documented what units *_offsets are in. |
763 | We assume bytes, but this is clearly wrong. | |
764 | Meaning this code needs to be carefully reviewed for errors. | |
765 | dmq. | |
766 | */ | |
767 | ||
2c2d71f5 | 768 | /* On end-of-str: see comment below. */ |
33b8afdf JH |
769 | must = do_utf8 ? prog->anchored_utf8 : prog->anchored_substr; |
770 | if (must == &PL_sv_undef) { | |
771 | s = (char*)NULL; | |
1de06328 | 772 | DEBUG_r(must = prog->anchored_utf8); /* for debug */ |
33b8afdf JH |
773 | } |
774 | else | |
775 | s = fbm_instr( | |
776 | (unsigned char*)t, | |
777 | HOP3(HOP3(last1, prog->anchored_offset, strend) | |
778 | + SvCUR(must), -(SvTAIL(must)!=0), strbeg), | |
779 | must, | |
7fba1cd6 | 780 | multiline ? FBMrf_MULTILINE : 0 |
33b8afdf | 781 | ); |
ab3bbdeb YO |
782 | DEBUG_EXECUTE_r({ |
783 | RE_PV_QUOTED_DECL(quoted, do_utf8, PERL_DEBUG_PAD_ZERO(0), | |
784 | SvPVX_const(must), RE_SV_DUMPLEN(must), 30); | |
785 | PerlIO_printf(Perl_debug_log, "%s anchored substr %s%s", | |
2c2d71f5 | 786 | (s ? "Found" : "Contradicts"), |
ab3bbdeb YO |
787 | quoted, RE_SV_TAIL(must)); |
788 | }); | |
789 | ||
790 | ||
2c2d71f5 JH |
791 | if (!s) { |
792 | if (last1 >= last2) { | |
a3621e74 | 793 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, |
2c2d71f5 JH |
794 | ", giving up...\n")); |
795 | goto fail_finish; | |
796 | } | |
a3621e74 | 797 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, |
2c2d71f5 | 798 | ", trying floating at offset %ld...\n", |
be8e71aa | 799 | (long)(HOP3c(saved_s, 1, strend) - i_strpos))); |
1aa99e6b IH |
800 | other_last = HOP3c(last1, prog->anchored_offset+1, strend); |
801 | s = HOP3c(last, 1, strend); | |
2c2d71f5 JH |
802 | goto restart; |
803 | } | |
804 | else { | |
a3621e74 | 805 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n", |
30944b6d | 806 | (long)(s - i_strpos))); |
1aa99e6b IH |
807 | t = HOP3c(s, -prog->anchored_offset, strbeg); |
808 | other_last = HOP3c(s, 1, strend); | |
be8e71aa | 809 | s = saved_s; |
2c2d71f5 JH |
810 | if (t == strpos) |
811 | goto try_at_start; | |
2c2d71f5 JH |
812 | goto try_at_offset; |
813 | } | |
30944b6d | 814 | } |
2c2d71f5 JH |
815 | } |
816 | else { /* Take into account the floating substring. */ | |
33b8afdf | 817 | char *last, *last1; |
be8e71aa | 818 | char * const saved_s = s; |
33b8afdf JH |
819 | SV* must; |
820 | ||
821 | t = HOP3c(s, -start_shift, strbeg); | |
822 | last1 = last = | |
823 | HOP3c(strend, -prog->minlen + prog->float_min_offset, strbeg); | |
824 | if (CHR_DIST((U8*)last, (U8*)t) > prog->float_max_offset) | |
825 | last = HOP3c(t, prog->float_max_offset, strend); | |
826 | s = HOP3c(t, prog->float_min_offset, strend); | |
827 | if (s < other_last) | |
828 | s = other_last; | |
2c2d71f5 | 829 | /* XXXX It is not documented what units *_offsets are in. Assume bytes. */ |
33b8afdf JH |
830 | must = do_utf8 ? prog->float_utf8 : prog->float_substr; |
831 | /* fbm_instr() takes into account exact value of end-of-str | |
832 | if the check is SvTAIL(ed). Since false positives are OK, | |
833 | and end-of-str is not later than strend we are OK. */ | |
834 | if (must == &PL_sv_undef) { | |
835 | s = (char*)NULL; | |
1de06328 | 836 | DEBUG_r(must = prog->float_utf8); /* for debug message */ |
33b8afdf JH |
837 | } |
838 | else | |
2c2d71f5 | 839 | s = fbm_instr((unsigned char*)s, |
33b8afdf JH |
840 | (unsigned char*)last + SvCUR(must) |
841 | - (SvTAIL(must)!=0), | |
7fba1cd6 | 842 | must, multiline ? FBMrf_MULTILINE : 0); |
ab3bbdeb YO |
843 | DEBUG_EXECUTE_r({ |
844 | RE_PV_QUOTED_DECL(quoted, do_utf8, PERL_DEBUG_PAD_ZERO(0), | |
845 | SvPVX_const(must), RE_SV_DUMPLEN(must), 30); | |
846 | PerlIO_printf(Perl_debug_log, "%s floating substr %s%s", | |
33b8afdf | 847 | (s ? "Found" : "Contradicts"), |
ab3bbdeb YO |
848 | quoted, RE_SV_TAIL(must)); |
849 | }); | |
33b8afdf JH |
850 | if (!s) { |
851 | if (last1 == last) { | |
a3621e74 | 852 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, |
33b8afdf JH |
853 | ", giving up...\n")); |
854 | goto fail_finish; | |
2c2d71f5 | 855 | } |
a3621e74 | 856 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, |
33b8afdf | 857 | ", trying anchored starting at offset %ld...\n", |
be8e71aa | 858 | (long)(saved_s + 1 - i_strpos))); |
33b8afdf JH |
859 | other_last = last; |
860 | s = HOP3c(t, 1, strend); | |
861 | goto restart; | |
862 | } | |
863 | else { | |
a3621e74 | 864 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n", |
33b8afdf JH |
865 | (long)(s - i_strpos))); |
866 | other_last = s; /* Fix this later. --Hugo */ | |
be8e71aa | 867 | s = saved_s; |
33b8afdf JH |
868 | if (t == strpos) |
869 | goto try_at_start; | |
870 | goto try_at_offset; | |
871 | } | |
2c2d71f5 | 872 | } |
cad2e5aa | 873 | } |
2c2d71f5 | 874 | |
1de06328 | 875 | |
9ef43ace | 876 | t= (char*)HOP3( s, -prog->check_offset_max, (prog->check_offset_max<0) ? strend : strpos); |
1de06328 | 877 | |
6bda09f9 | 878 | DEBUG_OPTIMISE_MORE_r( |
1de06328 YO |
879 | PerlIO_printf(Perl_debug_log, |
880 | "Check offset min:%"IVdf" max:%"IVdf" S:%"IVdf" t:%"IVdf" D:%"IVdf" end:%"IVdf"\n", | |
881 | (IV)prog->check_offset_min, | |
882 | (IV)prog->check_offset_max, | |
883 | (IV)(s-strpos), | |
884 | (IV)(t-strpos), | |
885 | (IV)(t-s), | |
886 | (IV)(strend-strpos) | |
887 | ) | |
888 | ); | |
889 | ||
2c2d71f5 | 890 | if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */ |
1d86a7f9 | 891 | && (!do_utf8 |
9ef43ace | 892 | || ((t = (char*)reghopmaybe3((U8*)s, -prog->check_offset_max, (U8*) ((prog->check_offset_max<0) ? strend : strpos))) |
1de06328 YO |
893 | && t > strpos))) |
894 | { | |
2c2d71f5 JH |
895 | /* Fixed substring is found far enough so that the match |
896 | cannot start at strpos. */ | |
897 | try_at_offset: | |
cad2e5aa | 898 | if (ml_anch && t[-1] != '\n') { |
30944b6d IZ |
899 | /* Eventually fbm_*() should handle this, but often |
900 | anchored_offset is not 0, so this check will not be wasted. */ | |
901 | /* XXXX In the code below we prefer to look for "^" even in | |
902 | presence of anchored substrings. And we search even | |
903 | beyond the found float position. These pessimizations | |
904 | are historical artefacts only. */ | |
905 | find_anchor: | |
2c2d71f5 | 906 | while (t < strend - prog->minlen) { |
cad2e5aa | 907 | if (*t == '\n') { |
4ee3650e | 908 | if (t < check_at - prog->check_offset_min) { |
33b8afdf | 909 | if (do_utf8 ? prog->anchored_utf8 : prog->anchored_substr) { |
4ee3650e GS |
910 | /* Since we moved from the found position, |
911 | we definitely contradict the found anchored | |
30944b6d IZ |
912 | substr. Due to the above check we do not |
913 | contradict "check" substr. | |
914 | Thus we can arrive here only if check substr | |
915 | is float. Redo checking for "other"=="fixed". | |
916 | */ | |
9041c2e3 | 917 | strpos = t + 1; |
a3621e74 | 918 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld, rescanning for anchored from offset %ld...\n", |
e4584336 | 919 | PL_colors[0], PL_colors[1], (long)(strpos - i_strpos), (long)(strpos - i_strpos + prog->anchored_offset))); |
30944b6d IZ |
920 | goto do_other_anchored; |
921 | } | |
4ee3650e GS |
922 | /* We don't contradict the found floating substring. */ |
923 | /* XXXX Why not check for STCLASS? */ | |
cad2e5aa | 924 | s = t + 1; |
a3621e74 | 925 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld...\n", |
e4584336 | 926 | PL_colors[0], PL_colors[1], (long)(s - i_strpos))); |
cad2e5aa JH |
927 | goto set_useful; |
928 | } | |
4ee3650e GS |
929 | /* Position contradicts check-string */ |
930 | /* XXXX probably better to look for check-string | |
931 | than for "\n", so one should lower the limit for t? */ | |
a3621e74 | 932 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m, restarting lookup for check-string at offset %ld...\n", |
e4584336 | 933 | PL_colors[0], PL_colors[1], (long)(t + 1 - i_strpos))); |
0e41cd87 | 934 | other_last = strpos = s = t + 1; |
cad2e5aa JH |
935 | goto restart; |
936 | } | |
937 | t++; | |
938 | } | |
a3621e74 | 939 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Did not find /%s^%s/m...\n", |
e4584336 | 940 | PL_colors[0], PL_colors[1])); |
2c2d71f5 | 941 | goto fail_finish; |
cad2e5aa | 942 | } |
f5952150 | 943 | else { |
a3621e74 | 944 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Starting position does not contradict /%s^%s/m...\n", |
e4584336 | 945 | PL_colors[0], PL_colors[1])); |
f5952150 | 946 | } |
cad2e5aa JH |
947 | s = t; |
948 | set_useful: | |
33b8afdf | 949 | ++BmUSEFUL(do_utf8 ? prog->check_utf8 : prog->check_substr); /* hooray/5 */ |
cad2e5aa JH |
950 | } |
951 | else { | |
f5952150 | 952 | /* The found string does not prohibit matching at strpos, |
2c2d71f5 | 953 | - no optimization of calling REx engine can be performed, |
f5952150 GS |
954 | unless it was an MBOL and we are not after MBOL, |
955 | or a future STCLASS check will fail this. */ | |
2c2d71f5 JH |
956 | try_at_start: |
957 | /* Even in this situation we may use MBOL flag if strpos is offset | |
958 | wrt the start of the string. */ | |
05b4157f | 959 | if (ml_anch && sv && !SvROK(sv) /* See prev comment on SvROK */ |
a1933d95 | 960 | && (strpos != strbeg) && strpos[-1] != '\n' |
d506a20d | 961 | /* May be due to an implicit anchor of m{.*foo} */ |
bbe252da | 962 | && !(prog->intflags & PREGf_IMPLICIT)) |
d506a20d | 963 | { |
cad2e5aa JH |
964 | t = strpos; |
965 | goto find_anchor; | |
966 | } | |
a3621e74 | 967 | DEBUG_EXECUTE_r( if (ml_anch) |
f5952150 | 968 | PerlIO_printf(Perl_debug_log, "Position at offset %ld does not contradict /%s^%s/m...\n", |
70685ca0 | 969 | (long)(strpos - i_strpos), PL_colors[0], PL_colors[1]); |
30944b6d | 970 | ); |
2c2d71f5 | 971 | success_at_start: |
bbe252da | 972 | if (!(prog->intflags & PREGf_NAUGHTY) /* XXXX If strpos moved? */ |
33b8afdf JH |
973 | && (do_utf8 ? ( |
974 | prog->check_utf8 /* Could be deleted already */ | |
975 | && --BmUSEFUL(prog->check_utf8) < 0 | |
976 | && (prog->check_utf8 == prog->float_utf8) | |
977 | ) : ( | |
978 | prog->check_substr /* Could be deleted already */ | |
979 | && --BmUSEFUL(prog->check_substr) < 0 | |
980 | && (prog->check_substr == prog->float_substr) | |
981 | ))) | |
66e933ab | 982 | { |
cad2e5aa | 983 | /* If flags & SOMETHING - do not do it many times on the same match */ |
a3621e74 | 984 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "... Disabling check substring...\n")); |
ef8d46e8 | 985 | /* XXX Does the destruction order has to change with do_utf8? */ |
33b8afdf | 986 | SvREFCNT_dec(do_utf8 ? prog->check_utf8 : prog->check_substr); |
ef8d46e8 | 987 | SvREFCNT_dec(do_utf8 ? prog->check_substr : prog->check_utf8); |
a0714e2c SS |
988 | prog->check_substr = prog->check_utf8 = NULL; /* disable */ |
989 | prog->float_substr = prog->float_utf8 = NULL; /* clear */ | |
990 | check = NULL; /* abort */ | |
cad2e5aa | 991 | s = strpos; |
3cf5c195 IZ |
992 | /* XXXX This is a remnant of the old implementation. It |
993 | looks wasteful, since now INTUIT can use many | |
6eb5f6b9 | 994 | other heuristics. */ |
bbe252da | 995 | prog->extflags &= ~RXf_USE_INTUIT; |
cad2e5aa JH |
996 | } |
997 | else | |
998 | s = strpos; | |
999 | } | |
1000 | ||
6eb5f6b9 JH |
1001 | /* Last resort... */ |
1002 | /* XXXX BmUSEFUL already changed, maybe multiple change is meaningful... */ | |
1de06328 YO |
1003 | /* trie stclasses are too expensive to use here, we are better off to |
1004 | leave it to regmatch itself */ | |
f8fc2ecf | 1005 | if (progi->regstclass && PL_regkind[OP(progi->regstclass)]!=TRIE) { |
6eb5f6b9 JH |
1006 | /* minlen == 0 is possible if regstclass is \b or \B, |
1007 | and the fixed substr is ''$. | |
1008 | Since minlen is already taken into account, s+1 is before strend; | |
1009 | accidentally, minlen >= 1 guaranties no false positives at s + 1 | |
1010 | even for \b or \B. But (minlen? 1 : 0) below assumes that | |
1011 | regstclass does not come from lookahead... */ | |
1012 | /* If regstclass takes bytelength more than 1: If charlength==1, OK. | |
1013 | This leaves EXACTF only, which is dealt with in find_byclass(). */ | |
f8fc2ecf YO |
1014 | const U8* const str = (U8*)STRING(progi->regstclass); |
1015 | const int cl_l = (PL_regkind[OP(progi->regstclass)] == EXACT | |
1016 | ? CHR_DIST(str+STR_LEN(progi->regstclass), str) | |
66e933ab | 1017 | : 1); |
1de06328 YO |
1018 | char * endpos; |
1019 | if (prog->anchored_substr || prog->anchored_utf8 || ml_anch) | |
1020 | endpos= HOP3c(s, (prog->minlen ? cl_l : 0), strend); | |
1021 | else if (prog->float_substr || prog->float_utf8) | |
1022 | endpos= HOP3c(HOP3c(check_at, -start_shift, strbeg), cl_l, strend); | |
1023 | else | |
1024 | endpos= strend; | |
1025 | ||
70685ca0 JH |
1026 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "start_shift: %"IVdf" check_at: %"IVdf" s: %"IVdf" endpos: %"IVdf"\n", |
1027 | (IV)start_shift, (IV)(check_at - strbeg), (IV)(s - strbeg), (IV)(endpos - strbeg))); | |
1de06328 | 1028 | |
6eb5f6b9 | 1029 | t = s; |
f8fc2ecf | 1030 | s = find_byclass(prog, progi->regstclass, s, endpos, NULL); |
6eb5f6b9 JH |
1031 | if (!s) { |
1032 | #ifdef DEBUGGING | |
cbbf8932 | 1033 | const char *what = NULL; |
6eb5f6b9 JH |
1034 | #endif |
1035 | if (endpos == strend) { | |
a3621e74 | 1036 | DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log, |
6eb5f6b9 JH |
1037 | "Could not match STCLASS...\n") ); |
1038 | goto fail; | |
1039 | } | |
a3621e74 | 1040 | DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log, |
66e933ab | 1041 | "This position contradicts STCLASS...\n") ); |
bbe252da | 1042 | if ((prog->extflags & RXf_ANCH) && !ml_anch) |
653099ff | 1043 | goto fail; |
6eb5f6b9 | 1044 | /* Contradict one of substrings */ |
33b8afdf JH |
1045 | if (prog->anchored_substr || prog->anchored_utf8) { |
1046 | if ((do_utf8 ? prog->anchored_utf8 : prog->anchored_substr) == check) { | |
a3621e74 | 1047 | DEBUG_EXECUTE_r( what = "anchored" ); |
6eb5f6b9 | 1048 | hop_and_restart: |
1aa99e6b | 1049 | s = HOP3c(t, 1, strend); |
66e933ab GS |
1050 | if (s + start_shift + end_shift > strend) { |
1051 | /* XXXX Should be taken into account earlier? */ | |
a3621e74 | 1052 | DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log, |
66e933ab GS |
1053 | "Could not match STCLASS...\n") ); |
1054 | goto fail; | |
1055 | } | |
5e39e1e5 HS |
1056 | if (!check) |
1057 | goto giveup; | |
a3621e74 | 1058 | DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log, |
f5952150 | 1059 | "Looking for %s substr starting at offset %ld...\n", |
6eb5f6b9 JH |
1060 | what, (long)(s + start_shift - i_strpos)) ); |
1061 | goto restart; | |
1062 | } | |
66e933ab | 1063 | /* Have both, check_string is floating */ |
6eb5f6b9 JH |
1064 | if (t + start_shift >= check_at) /* Contradicts floating=check */ |
1065 | goto retry_floating_check; | |
1066 | /* Recheck anchored substring, but not floating... */ | |
9041c2e3 | 1067 | s = check_at; |
5e39e1e5 HS |
1068 | if (!check) |
1069 | goto giveup; | |
a3621e74 | 1070 | DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log, |
f5952150 | 1071 | "Looking for anchored substr starting at offset %ld...\n", |
6eb5f6b9 JH |
1072 | (long)(other_last - i_strpos)) ); |
1073 | goto do_other_anchored; | |
1074 | } | |
60e71179 GS |
1075 | /* Another way we could have checked stclass at the |
1076 | current position only: */ | |
1077 | if (ml_anch) { | |
1078 | s = t = t + 1; | |
5e39e1e5 HS |
1079 | if (!check) |
1080 | goto giveup; | |
a3621e74 | 1081 | DEBUG_EXECUTE_r( PerlIO_printf(Perl_debug_log, |
f5952150 | 1082 | "Looking for /%s^%s/m starting at offset %ld...\n", |
e4584336 | 1083 | PL_colors[0], PL_colors[1], (long)(t - i_strpos)) ); |
60e71179 | 1084 | goto try_at_offset; |
66e933ab | 1085 | } |
33b8afdf | 1086 | if (!(do_utf8 ? prog->float_utf8 : prog->float_substr)) /* Could have been deleted */ |
60e71179 | 1087 | goto fail; |
6eb5f6b9 JH |
1088 | /* Check is floating subtring. */ |
1089 | retry_floating_check: | |
1090 | t = check_at - start_shift; | |
a3621e74 | 1091 | DEBUG_EXECUTE_r( what = "floating" ); |
6eb5f6b9 JH |
1092 | goto hop_and_restart; |
1093 | } | |
b7953727 | 1094 | if (t != s) { |
a3621e74 | 1095 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, |
6eb5f6b9 | 1096 | "By STCLASS: moving %ld --> %ld\n", |
b7953727 JH |
1097 | (long)(t - i_strpos), (long)(s - i_strpos)) |
1098 | ); | |
1099 | } | |
1100 | else { | |
a3621e74 | 1101 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, |
b7953727 JH |
1102 | "Does not contradict STCLASS...\n"); |
1103 | ); | |
1104 | } | |
6eb5f6b9 | 1105 | } |
5e39e1e5 | 1106 | giveup: |
a3621e74 | 1107 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%s%s:%s match at offset %ld\n", |
5e39e1e5 HS |
1108 | PL_colors[4], (check ? "Guessed" : "Giving up"), |
1109 | PL_colors[5], (long)(s - i_strpos)) ); | |
cad2e5aa | 1110 | return s; |
2c2d71f5 JH |
1111 | |
1112 | fail_finish: /* Substring not found */ | |
33b8afdf JH |
1113 | if (prog->check_substr || prog->check_utf8) /* could be removed already */ |
1114 | BmUSEFUL(do_utf8 ? prog->check_utf8 : prog->check_substr) += 5; /* hooray */ | |
cad2e5aa | 1115 | fail: |
a3621e74 | 1116 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch rejected by optimizer%s\n", |
e4584336 | 1117 | PL_colors[4], PL_colors[5])); |
bd61b366 | 1118 | return NULL; |
cad2e5aa | 1119 | } |
9661b544 | 1120 | |
a0a388a1 YO |
1121 | #define DECL_TRIE_TYPE(scan) \ |
1122 | const enum { trie_plain, trie_utf8, trie_utf8_fold, trie_latin_utf8_fold } \ | |
1123 | trie_type = (scan->flags != EXACT) \ | |
1124 | ? (do_utf8 ? trie_utf8_fold : (UTF ? trie_latin_utf8_fold : trie_plain)) \ | |
1125 | : (do_utf8 ? trie_utf8 : trie_plain) | |
3b0527fe | 1126 | |
55eed653 NC |
1127 | #define REXEC_TRIE_READ_CHAR(trie_type, trie, widecharmap, uc, uscan, len, \ |
1128 | uvc, charid, foldlen, foldbuf, uniflags) STMT_START { \ | |
4cadc6a9 YO |
1129 | switch (trie_type) { \ |
1130 | case trie_utf8_fold: \ | |
1131 | if ( foldlen>0 ) { \ | |
0abd0d78 | 1132 | uvc = utf8n_to_uvuni( uscan, UTF8_MAXLEN, &len, uniflags ); \ |
4cadc6a9 YO |
1133 | foldlen -= len; \ |
1134 | uscan += len; \ | |
1135 | len=0; \ | |
1136 | } else { \ | |
0abd0d78 | 1137 | uvc = utf8n_to_uvuni( (U8*)uc, UTF8_MAXLEN, &len, uniflags ); \ |
4cadc6a9 YO |
1138 | uvc = to_uni_fold( uvc, foldbuf, &foldlen ); \ |
1139 | foldlen -= UNISKIP( uvc ); \ | |
1140 | uscan = foldbuf + UNISKIP( uvc ); \ | |
1141 | } \ | |
1142 | break; \ | |
a0a388a1 YO |
1143 | case trie_latin_utf8_fold: \ |
1144 | if ( foldlen>0 ) { \ | |
1145 | uvc = utf8n_to_uvuni( uscan, UTF8_MAXLEN, &len, uniflags ); \ | |
1146 | foldlen -= len; \ | |
1147 | uscan += len; \ | |
1148 | len=0; \ | |
1149 | } else { \ | |
1150 | len = 1; \ | |
1151 | uvc = to_uni_fold( *(U8*)uc, foldbuf, &foldlen ); \ | |
1152 | foldlen -= UNISKIP( uvc ); \ | |
1153 | uscan = foldbuf + UNISKIP( uvc ); \ | |
1154 | } \ | |
1155 | break; \ | |
4cadc6a9 YO |
1156 | case trie_utf8: \ |
1157 | uvc = utf8n_to_uvuni( (U8*)uc, UTF8_MAXLEN, &len, uniflags ); \ | |
1158 | break; \ | |
1159 | case trie_plain: \ | |
1160 | uvc = (UV)*uc; \ | |
1161 | len = 1; \ | |
1162 | } \ | |
4cadc6a9 YO |
1163 | if (uvc < 256) { \ |
1164 | charid = trie->charmap[ uvc ]; \ | |
1165 | } \ | |
1166 | else { \ | |
1167 | charid = 0; \ | |
55eed653 NC |
1168 | if (widecharmap) { \ |
1169 | SV** const svpp = hv_fetch(widecharmap, \ | |
4cadc6a9 YO |
1170 | (char*)&uvc, sizeof(UV), 0); \ |
1171 | if (svpp) \ | |
1172 | charid = (U16)SvIV(*svpp); \ | |
1173 | } \ | |
1174 | } \ | |
1175 | } STMT_END | |
1176 | ||
a0a388a1 YO |
1177 | #define REXEC_FBC_EXACTISH_CHECK(CoNd) \ |
1178 | { \ | |
1179 | char *my_strend= (char *)strend; \ | |
4cadc6a9 YO |
1180 | if ( (CoNd) \ |
1181 | && (ln == len || \ | |
a0a388a1 | 1182 | !ibcmp_utf8(s, &my_strend, 0, do_utf8, \ |
f2338a2e | 1183 | m, NULL, ln, cBOOL(UTF))) \ |
a0a388a1 | 1184 | && (!reginfo || regtry(reginfo, &s)) ) \ |
4cadc6a9 YO |
1185 | goto got_it; \ |
1186 | else { \ | |
1187 | U8 foldbuf[UTF8_MAXBYTES_CASE+1]; \ | |
1188 | uvchr_to_utf8(tmpbuf, c); \ | |
1189 | f = to_utf8_fold(tmpbuf, foldbuf, &foldlen); \ | |
1190 | if ( f != c \ | |
1191 | && (f == c1 || f == c2) \ | |
a0a388a1 YO |
1192 | && (ln == len || \ |
1193 | !ibcmp_utf8(s, &my_strend, 0, do_utf8,\ | |
f2338a2e | 1194 | m, NULL, ln, cBOOL(UTF)))\ |
a0a388a1 | 1195 | && (!reginfo || regtry(reginfo, &s)) ) \ |
4cadc6a9 YO |
1196 | goto got_it; \ |
1197 | } \ | |
a0a388a1 YO |
1198 | } \ |
1199 | s += len | |
4cadc6a9 YO |
1200 | |
1201 | #define REXEC_FBC_EXACTISH_SCAN(CoNd) \ | |
1202 | STMT_START { \ | |
1203 | while (s <= e) { \ | |
1204 | if ( (CoNd) \ | |
1205 | && (ln == 1 || !(OP(c) == EXACTF \ | |
1206 | ? ibcmp(s, m, ln) \ | |
1207 | : ibcmp_locale(s, m, ln))) \ | |
24b23f37 | 1208 | && (!reginfo || regtry(reginfo, &s)) ) \ |
4cadc6a9 YO |
1209 | goto got_it; \ |
1210 | s++; \ | |
1211 | } \ | |
1212 | } STMT_END | |
1213 | ||
1214 | #define REXEC_FBC_UTF8_SCAN(CoDe) \ | |
1215 | STMT_START { \ | |
1216 | while (s + (uskip = UTF8SKIP(s)) <= strend) { \ | |
1217 | CoDe \ | |
1218 | s += uskip; \ | |
1219 | } \ | |
1220 | } STMT_END | |
1221 | ||
1222 | #define REXEC_FBC_SCAN(CoDe) \ | |
1223 | STMT_START { \ | |
1224 | while (s < strend) { \ | |
1225 | CoDe \ | |
1226 | s++; \ | |
1227 | } \ | |
1228 | } STMT_END | |
1229 | ||
1230 | #define REXEC_FBC_UTF8_CLASS_SCAN(CoNd) \ | |
1231 | REXEC_FBC_UTF8_SCAN( \ | |
1232 | if (CoNd) { \ | |
24b23f37 | 1233 | if (tmp && (!reginfo || regtry(reginfo, &s))) \ |
4cadc6a9 YO |
1234 | goto got_it; \ |
1235 | else \ | |
1236 | tmp = doevery; \ | |
1237 | } \ | |
1238 | else \ | |
1239 | tmp = 1; \ | |
1240 | ) | |
1241 | ||
1242 | #define REXEC_FBC_CLASS_SCAN(CoNd) \ | |
1243 | REXEC_FBC_SCAN( \ | |
1244 | if (CoNd) { \ | |
24b23f37 | 1245 | if (tmp && (!reginfo || regtry(reginfo, &s))) \ |
4cadc6a9 YO |
1246 | goto got_it; \ |
1247 | else \ | |
1248 | tmp = doevery; \ | |
1249 | } \ | |
1250 | else \ | |
1251 | tmp = 1; \ | |
1252 | ) | |
1253 | ||
1254 | #define REXEC_FBC_TRYIT \ | |
24b23f37 | 1255 | if ((!reginfo || regtry(reginfo, &s))) \ |
4cadc6a9 YO |
1256 | goto got_it |
1257 | ||
e1d1eefb YO |
1258 | #define REXEC_FBC_CSCAN(CoNdUtF8,CoNd) \ |
1259 | if (do_utf8) { \ | |
1260 | REXEC_FBC_UTF8_CLASS_SCAN(CoNdUtF8); \ | |
1261 | } \ | |
1262 | else { \ | |
1263 | REXEC_FBC_CLASS_SCAN(CoNd); \ | |
1264 | } \ | |
1265 | break | |
1266 | ||
4cadc6a9 YO |
1267 | #define REXEC_FBC_CSCAN_PRELOAD(UtFpReLoAd,CoNdUtF8,CoNd) \ |
1268 | if (do_utf8) { \ | |
1269 | UtFpReLoAd; \ | |
1270 | REXEC_FBC_UTF8_CLASS_SCAN(CoNdUtF8); \ | |
1271 | } \ | |
1272 | else { \ | |
1273 | REXEC_FBC_CLASS_SCAN(CoNd); \ | |
1274 | } \ | |
1275 | break | |
1276 | ||
1277 | #define REXEC_FBC_CSCAN_TAINT(CoNdUtF8,CoNd) \ | |
1278 | PL_reg_flags |= RF_tainted; \ | |
1279 | if (do_utf8) { \ | |
1280 | REXEC_FBC_UTF8_CLASS_SCAN(CoNdUtF8); \ | |
1281 | } \ | |
1282 | else { \ | |
1283 | REXEC_FBC_CLASS_SCAN(CoNd); \ | |
1284 | } \ | |
1285 | break | |
1286 | ||
786e8c11 YO |
1287 | #define DUMP_EXEC_POS(li,s,doutf8) \ |
1288 | dump_exec_pos(li,s,(PL_regeol),(PL_bostr),(PL_reg_starttry),doutf8) | |
1289 | ||
1290 | /* We know what class REx starts with. Try to find this position... */ | |
1291 | /* if reginfo is NULL, its a dryrun */ | |
1292 | /* annoyingly all the vars in this routine have different names from their counterparts | |
1293 | in regmatch. /grrr */ | |
1294 | ||
3c3eec57 | 1295 | STATIC char * |
07be1b83 | 1296 | S_find_byclass(pTHX_ regexp * prog, const regnode *c, char *s, |
24b23f37 | 1297 | const char *strend, regmatch_info *reginfo) |
a687059c | 1298 | { |
27da23d5 | 1299 | dVAR; |
bbe252da | 1300 | const I32 doevery = (prog->intflags & PREGf_SKIP) == 0; |
6eb5f6b9 | 1301 | char *m; |
d8093b23 | 1302 | STRLEN ln; |
5dab1207 | 1303 | STRLEN lnc; |
078c425b | 1304 | register STRLEN uskip; |
d8093b23 G |
1305 | unsigned int c1; |
1306 | unsigned int c2; | |
6eb5f6b9 JH |
1307 | char *e; |
1308 | register I32 tmp = 1; /* Scratch variable? */ | |
a3b680e6 | 1309 | register const bool do_utf8 = PL_reg_match_utf8; |
f8fc2ecf | 1310 | RXi_GET_DECL(prog,progi); |
7918f24d NC |
1311 | |
1312 | PERL_ARGS_ASSERT_FIND_BYCLASS; | |
f8fc2ecf | 1313 | |
6eb5f6b9 JH |
1314 | /* We know what class it must start with. */ |
1315 | switch (OP(c)) { | |
6eb5f6b9 | 1316 | case ANYOF: |
388cc4de | 1317 | if (do_utf8) { |
4cadc6a9 | 1318 | REXEC_FBC_UTF8_CLASS_SCAN((ANYOF_FLAGS(c) & ANYOF_UNICODE) || |
388cc4de | 1319 | !UTF8_IS_INVARIANT((U8)s[0]) ? |
32fc9b6a | 1320 | reginclass(prog, c, (U8*)s, 0, do_utf8) : |
4cadc6a9 | 1321 | REGINCLASS(prog, c, (U8*)s)); |
388cc4de HS |
1322 | } |
1323 | else { | |
1324 | while (s < strend) { | |
1325 | STRLEN skip = 1; | |
1326 | ||
32fc9b6a | 1327 | if (REGINCLASS(prog, c, (U8*)s) || |
388cc4de HS |
1328 | (ANYOF_FOLD_SHARP_S(c, s, strend) && |
1329 | /* The assignment of 2 is intentional: | |
1330 | * for the folded sharp s, the skip is 2. */ | |
1331 | (skip = SHARP_S_SKIP))) { | |
24b23f37 | 1332 | if (tmp && (!reginfo || regtry(reginfo, &s))) |
388cc4de HS |
1333 | goto got_it; |
1334 | else | |
1335 | tmp = doevery; | |
1336 | } | |
1337 | else | |
1338 | tmp = 1; | |
1339 | s += skip; | |
1340 | } | |
a0d0e21e | 1341 | } |
6eb5f6b9 | 1342 | break; |
f33976b4 | 1343 | case CANY: |
4cadc6a9 | 1344 | REXEC_FBC_SCAN( |
24b23f37 | 1345 | if (tmp && (!reginfo || regtry(reginfo, &s))) |
f33976b4 DB |
1346 | goto got_it; |
1347 | else | |
1348 | tmp = doevery; | |
4cadc6a9 | 1349 | ); |
f33976b4 | 1350 | break; |
6eb5f6b9 | 1351 | case EXACTF: |
5dab1207 NIS |
1352 | m = STRING(c); |
1353 | ln = STR_LEN(c); /* length to match in octets/bytes */ | |
1354 | lnc = (I32) ln; /* length to match in characters */ | |
1aa99e6b | 1355 | if (UTF) { |
a2a2844f | 1356 | STRLEN ulen1, ulen2; |
5dab1207 | 1357 | U8 *sm = (U8 *) m; |
89ebb4a3 JH |
1358 | U8 tmpbuf1[UTF8_MAXBYTES_CASE+1]; |
1359 | U8 tmpbuf2[UTF8_MAXBYTES_CASE+1]; | |
97dc7d3e RGS |
1360 | /* used by commented-out code below */ |
1361 | /*const U32 uniflags = UTF8_ALLOW_DEFAULT;*/ | |
a0a388a1 YO |
1362 | |
1363 | /* XXX: Since the node will be case folded at compile | |
1364 | time this logic is a little odd, although im not | |
1365 | sure that its actually wrong. --dmq */ | |
1366 | ||
1367 | c1 = to_utf8_lower((U8*)m, tmpbuf1, &ulen1); | |
1368 | c2 = to_utf8_upper((U8*)m, tmpbuf2, &ulen2); | |
1369 | ||
1370 | /* XXX: This is kinda strange. to_utf8_XYZ returns the | |
1371 | codepoint of the first character in the converted | |
1372 | form, yet originally we did the extra step. | |
1373 | No tests fail by commenting this code out however | |
1374 | so Ive left it out. -- dmq. | |
1375 | ||
89ebb4a3 | 1376 | c1 = utf8n_to_uvchr(tmpbuf1, UTF8_MAXBYTES_CASE, |
041457d9 | 1377 | 0, uniflags); |
89ebb4a3 | 1378 | c2 = utf8n_to_uvchr(tmpbuf2, UTF8_MAXBYTES_CASE, |
041457d9 | 1379 | 0, uniflags); |
a0a388a1 YO |
1380 | */ |
1381 | ||
5dab1207 NIS |
1382 | lnc = 0; |
1383 | while (sm < ((U8 *) m + ln)) { | |
1384 | lnc++; | |
1385 | sm += UTF8SKIP(sm); | |
1386 | } | |
1aa99e6b IH |
1387 | } |
1388 | else { | |
1389 | c1 = *(U8*)m; | |
1390 | c2 = PL_fold[c1]; | |
1391 | } | |
6eb5f6b9 JH |
1392 | goto do_exactf; |
1393 | case EXACTFL: | |
5dab1207 NIS |
1394 | m = STRING(c); |
1395 | ln = STR_LEN(c); | |
1396 | lnc = (I32) ln; | |
d8093b23 | 1397 | c1 = *(U8*)m; |
6eb5f6b9 JH |
1398 | c2 = PL_fold_locale[c1]; |
1399 | do_exactf: | |
db12adc6 | 1400 | e = HOP3c(strend, -((I32)lnc), s); |
b3c9acc1 | 1401 | |
3b0527fe | 1402 | if (!reginfo && e < s) |
6eb5f6b9 | 1403 | e = s; /* Due to minlen logic of intuit() */ |
1aa99e6b | 1404 | |
60a8b682 JH |
1405 | /* The idea in the EXACTF* cases is to first find the |
1406 | * first character of the EXACTF* node and then, if | |
1407 | * necessary, case-insensitively compare the full | |
1408 | * text of the node. The c1 and c2 are the first | |
1409 | * characters (though in Unicode it gets a bit | |
1410 | * more complicated because there are more cases | |
7f16dd3d JH |
1411 | * than just upper and lower: one needs to use |
1412 | * the so-called folding case for case-insensitive | |
1413 | * matching (called "loose matching" in Unicode). | |
1414 | * ibcmp_utf8() will do just that. */ | |
60a8b682 | 1415 | |
a0a388a1 | 1416 | if (do_utf8 || UTF) { |
575cac57 | 1417 | UV c, f; |
89ebb4a3 | 1418 | U8 tmpbuf [UTF8_MAXBYTES+1]; |
a0a388a1 YO |
1419 | STRLEN len = 1; |
1420 | STRLEN foldlen; | |
4ad0818d | 1421 | const U32 uniflags = UTF8_ALLOW_DEFAULT; |
09091399 | 1422 | if (c1 == c2) { |
5dab1207 NIS |
1423 | /* Upper and lower of 1st char are equal - |
1424 | * probably not a "letter". */ | |
1aa99e6b | 1425 | while (s <= e) { |
a0a388a1 YO |
1426 | if (do_utf8) { |
1427 | c = utf8n_to_uvchr((U8*)s, UTF8_MAXBYTES, &len, | |
041457d9 | 1428 | uniflags); |
a0a388a1 YO |
1429 | } else { |
1430 | c = *((U8*)s); | |
1431 | } | |
4cadc6a9 | 1432 | REXEC_FBC_EXACTISH_CHECK(c == c1); |
1aa99e6b | 1433 | } |
09091399 JH |
1434 | } |
1435 | else { | |
1aa99e6b | 1436 | while (s <= e) { |
a0a388a1 YO |
1437 | if (do_utf8) { |
1438 | c = utf8n_to_uvchr((U8*)s, UTF8_MAXBYTES, &len, | |
041457d9 | 1439 | uniflags); |
a0a388a1 YO |
1440 | } else { |
1441 | c = *((U8*)s); | |
1442 | } | |
80aecb99 | 1443 | |
60a8b682 | 1444 | /* Handle some of the three Greek sigmas cases. |
8c01da3c JH |
1445 | * Note that not all the possible combinations |
1446 | * are handled here: some of them are handled | |
1447 | * by the standard folding rules, and some of | |
1448 | * them (the character class or ANYOF cases) | |
1449 | * are handled during compiletime in | |
1450 | * regexec.c:S_regclass(). */ | |
880bd946 JH |
1451 | if (c == (UV)UNICODE_GREEK_CAPITAL_LETTER_SIGMA || |
1452 | c == (UV)UNICODE_GREEK_SMALL_LETTER_FINAL_SIGMA) | |
1453 | c = (UV)UNICODE_GREEK_SMALL_LETTER_SIGMA; | |
80aecb99 | 1454 | |
4cadc6a9 | 1455 | REXEC_FBC_EXACTISH_CHECK(c == c1 || c == c2); |
1aa99e6b | 1456 | } |
09091399 | 1457 | } |
1aa99e6b IH |
1458 | } |
1459 | else { | |
a0a388a1 | 1460 | /* Neither pattern nor string are UTF8 */ |
1aa99e6b | 1461 | if (c1 == c2) |
4cadc6a9 | 1462 | REXEC_FBC_EXACTISH_SCAN(*(U8*)s == c1); |
1aa99e6b | 1463 | else |
4cadc6a9 | 1464 | REXEC_FBC_EXACTISH_SCAN(*(U8*)s == c1 || *(U8*)s == c2); |
b3c9acc1 IZ |
1465 | } |
1466 | break; | |
bbce6d69 | 1467 | case BOUNDL: |
3280af22 | 1468 | PL_reg_flags |= RF_tainted; |
bbce6d69 | 1469 | /* FALL THROUGH */ |
a0d0e21e | 1470 | case BOUND: |
ffc61ed2 | 1471 | if (do_utf8) { |
12d33761 | 1472 | if (s == PL_bostr) |
ffc61ed2 JH |
1473 | tmp = '\n'; |
1474 | else { | |
6136c704 | 1475 | U8 * const r = reghop3((U8*)s, -1, (U8*)PL_bostr); |
4ad0818d | 1476 | tmp = utf8n_to_uvchr(r, UTF8SKIP(r), 0, UTF8_ALLOW_DEFAULT); |
ffc61ed2 JH |
1477 | } |
1478 | tmp = ((OP(c) == BOUND ? | |
9041c2e3 | 1479 | isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0); |
1a4fad37 | 1480 | LOAD_UTF8_CHARCLASS_ALNUM(); |
4cadc6a9 | 1481 | REXEC_FBC_UTF8_SCAN( |
ffc61ed2 | 1482 | if (tmp == !(OP(c) == BOUND ? |
f2338a2e | 1483 | cBOOL(swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) : |
ffc61ed2 JH |
1484 | isALNUM_LC_utf8((U8*)s))) |
1485 | { | |
1486 | tmp = !tmp; | |
4cadc6a9 | 1487 | REXEC_FBC_TRYIT; |
a687059c | 1488 | } |
4cadc6a9 | 1489 | ); |
a0d0e21e | 1490 | } |
667bb95a | 1491 | else { |
12d33761 | 1492 | tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n'; |
ffc61ed2 | 1493 | tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0); |
4cadc6a9 | 1494 | REXEC_FBC_SCAN( |
ffc61ed2 JH |
1495 | if (tmp == |
1496 | !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) { | |
1497 | tmp = !tmp; | |
4cadc6a9 | 1498 | REXEC_FBC_TRYIT; |
a0ed51b3 | 1499 | } |
4cadc6a9 | 1500 | ); |
a0ed51b3 | 1501 | } |
24b23f37 | 1502 | if ((!prog->minlen && tmp) && (!reginfo || regtry(reginfo, &s))) |
a0ed51b3 LW |
1503 | goto got_it; |
1504 | break; | |
bbce6d69 | 1505 | case NBOUNDL: |
3280af22 | 1506 | PL_reg_flags |= RF_tainted; |
bbce6d69 | 1507 | /* FALL THROUGH */ |
a0d0e21e | 1508 | case NBOUND: |
ffc61ed2 | 1509 | if (do_utf8) { |
12d33761 | 1510 | if (s == PL_bostr) |
ffc61ed2 JH |
1511 | tmp = '\n'; |
1512 | else { | |
6136c704 | 1513 | U8 * const r = reghop3((U8*)s, -1, (U8*)PL_bostr); |
4ad0818d | 1514 | tmp = utf8n_to_uvchr(r, UTF8SKIP(r), 0, UTF8_ALLOW_DEFAULT); |
ffc61ed2 JH |
1515 | } |
1516 | tmp = ((OP(c) == NBOUND ? | |
9041c2e3 | 1517 | isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0); |
1a4fad37 | 1518 | LOAD_UTF8_CHARCLASS_ALNUM(); |
4cadc6a9 | 1519 | REXEC_FBC_UTF8_SCAN( |
ffc61ed2 | 1520 | if (tmp == !(OP(c) == NBOUND ? |
f2338a2e | 1521 | cBOOL(swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) : |
ffc61ed2 JH |
1522 | isALNUM_LC_utf8((U8*)s))) |
1523 | tmp = !tmp; | |
4cadc6a9 YO |
1524 | else REXEC_FBC_TRYIT; |
1525 | ); | |
a0d0e21e | 1526 | } |
667bb95a | 1527 | else { |
12d33761 | 1528 | tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n'; |
ffc61ed2 JH |
1529 | tmp = ((OP(c) == NBOUND ? |
1530 | isALNUM(tmp) : isALNUM_LC(tmp)) != 0); | |
4cadc6a9 | 1531 | REXEC_FBC_SCAN( |
ffc61ed2 JH |
1532 | if (tmp == |
1533 | !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s))) | |
1534 | tmp = !tmp; | |
4cadc6a9 YO |
1535 | else REXEC_FBC_TRYIT; |
1536 | ); | |
a0ed51b3 | 1537 | } |
24b23f37 | 1538 | if ((!prog->minlen && !tmp) && (!reginfo || regtry(reginfo, &s))) |
a0ed51b3 LW |
1539 | goto got_it; |
1540 | break; | |
a0d0e21e | 1541 | case ALNUM: |
4cadc6a9 | 1542 | REXEC_FBC_CSCAN_PRELOAD( |
d1eb3177 YO |
1543 | LOAD_UTF8_CHARCLASS_PERL_WORD(), |
1544 | swash_fetch(RE_utf8_perl_word, (U8*)s, do_utf8), | |
4cadc6a9 YO |
1545 | isALNUM(*s) |
1546 | ); | |
bbce6d69 | 1547 | case ALNUML: |
4cadc6a9 YO |
1548 | REXEC_FBC_CSCAN_TAINT( |
1549 | isALNUM_LC_utf8((U8*)s), | |
1550 | isALNUM_LC(*s) | |
1551 | ); | |
a0d0e21e | 1552 | case NALNUM: |
4cadc6a9 | 1553 | REXEC_FBC_CSCAN_PRELOAD( |
d1eb3177 YO |
1554 | LOAD_UTF8_CHARCLASS_PERL_WORD(), |
1555 | !swash_fetch(RE_utf8_perl_word, (U8*)s, do_utf8), | |
4cadc6a9 YO |
1556 | !isALNUM(*s) |
1557 | ); | |
bbce6d69 | 1558 | case NALNUML: |
4cadc6a9 YO |
1559 | REXEC_FBC_CSCAN_TAINT( |
1560 | !isALNUM_LC_utf8((U8*)s), | |
1561 | !isALNUM_LC(*s) | |
1562 | ); | |
a0d0e21e | 1563 | case SPACE: |
4cadc6a9 | 1564 | REXEC_FBC_CSCAN_PRELOAD( |
d1eb3177 YO |
1565 | LOAD_UTF8_CHARCLASS_PERL_SPACE(), |
1566 | *s == ' ' || swash_fetch(RE_utf8_perl_space,(U8*)s, do_utf8), | |
4cadc6a9 YO |
1567 | isSPACE(*s) |
1568 | ); | |
bbce6d69 | 1569 | case SPACEL: |
4cadc6a9 YO |
1570 | REXEC_FBC_CSCAN_TAINT( |
1571 | *s == ' ' || isSPACE_LC_utf8((U8*)s), | |
1572 | isSPACE_LC(*s) | |
1573 | ); | |
a0d0e21e | 1574 | case NSPACE: |
4cadc6a9 | 1575 | REXEC_FBC_CSCAN_PRELOAD( |
d1eb3177 YO |
1576 | LOAD_UTF8_CHARCLASS_PERL_SPACE(), |
1577 | !(*s == ' ' || swash_fetch(RE_utf8_perl_space,(U8*)s, do_utf8)), | |
4cadc6a9 YO |
1578 | !isSPACE(*s) |
1579 | ); | |
bbce6d69 | 1580 | case NSPACEL: |
4cadc6a9 YO |
1581 | REXEC_FBC_CSCAN_TAINT( |
1582 | !(*s == ' ' || isSPACE_LC_utf8((U8*)s)), | |
1583 | !isSPACE_LC(*s) | |
1584 | ); | |
a0d0e21e | 1585 | case DIGIT: |
4cadc6a9 | 1586 | REXEC_FBC_CSCAN_PRELOAD( |
d1eb3177 YO |
1587 | LOAD_UTF8_CHARCLASS_POSIX_DIGIT(), |
1588 | swash_fetch(RE_utf8_posix_digit,(U8*)s, do_utf8), | |
4cadc6a9 YO |
1589 | isDIGIT(*s) |
1590 | ); | |
b8c5462f | 1591 | case DIGITL: |
4cadc6a9 YO |
1592 | REXEC_FBC_CSCAN_TAINT( |
1593 | isDIGIT_LC_utf8((U8*)s), | |
1594 | isDIGIT_LC(*s) | |
1595 | ); | |
a0d0e21e | 1596 | case NDIGIT: |
4cadc6a9 | 1597 | REXEC_FBC_CSCAN_PRELOAD( |
d1eb3177 YO |
1598 | LOAD_UTF8_CHARCLASS_POSIX_DIGIT(), |
1599 | !swash_fetch(RE_utf8_posix_digit,(U8*)s, do_utf8), | |
4cadc6a9 YO |
1600 | !isDIGIT(*s) |
1601 | ); | |
b8c5462f | 1602 | case NDIGITL: |
4cadc6a9 YO |
1603 | REXEC_FBC_CSCAN_TAINT( |
1604 | !isDIGIT_LC_utf8((U8*)s), | |
1605 | !isDIGIT_LC(*s) | |
1606 | ); | |
e1d1eefb YO |
1607 | case LNBREAK: |
1608 | REXEC_FBC_CSCAN( | |
1609 | is_LNBREAK_utf8(s), | |
1610 | is_LNBREAK_latin1(s) | |
1611 | ); | |
1612 | case VERTWS: | |
1613 | REXEC_FBC_CSCAN( | |
1614 | is_VERTWS_utf8(s), | |
1615 | is_VERTWS_latin1(s) | |
1616 | ); | |
1617 | case NVERTWS: | |
1618 | REXEC_FBC_CSCAN( | |
1619 | !is_VERTWS_utf8(s), | |
1620 | !is_VERTWS_latin1(s) | |
1621 | ); | |
1622 | case HORIZWS: | |
1623 | REXEC_FBC_CSCAN( | |
1624 | is_HORIZWS_utf8(s), | |
1625 | is_HORIZWS_latin1(s) | |
1626 | ); | |
1627 | case NHORIZWS: | |
1628 | REXEC_FBC_CSCAN( | |
1629 | !is_HORIZWS_utf8(s), | |
1630 | !is_HORIZWS_latin1(s) | |
1631 | ); | |
1de06328 YO |
1632 | case AHOCORASICKC: |
1633 | case AHOCORASICK: | |
07be1b83 | 1634 | { |
a0a388a1 | 1635 | DECL_TRIE_TYPE(c); |
07be1b83 YO |
1636 | /* what trie are we using right now */ |
1637 | reg_ac_data *aho | |
f8fc2ecf | 1638 | = (reg_ac_data*)progi->data->data[ ARG( c ) ]; |
3251b653 NC |
1639 | reg_trie_data *trie |
1640 | = (reg_trie_data*)progi->data->data[ aho->trie ]; | |
85fbaab2 | 1641 | HV *widecharmap = MUTABLE_HV(progi->data->data[ aho->trie + 1 ]); |
07be1b83 YO |
1642 | |
1643 | const char *last_start = strend - trie->minlen; | |
6148ee25 | 1644 | #ifdef DEBUGGING |
07be1b83 | 1645 | const char *real_start = s; |
6148ee25 | 1646 | #endif |
07be1b83 | 1647 | STRLEN maxlen = trie->maxlen; |
be8e71aa YO |
1648 | SV *sv_points; |
1649 | U8 **points; /* map of where we were in the input string | |
786e8c11 | 1650 | when reading a given char. For ASCII this |
be8e71aa | 1651 | is unnecessary overhead as the relationship |
38a44b82 NC |
1652 | is always 1:1, but for Unicode, especially |
1653 | case folded Unicode this is not true. */ | |
f9e705e8 | 1654 | U8 foldbuf[ UTF8_MAXBYTES_CASE + 1 ]; |
786e8c11 YO |
1655 | U8 *bitmap=NULL; |
1656 | ||
07be1b83 YO |
1657 | |
1658 | GET_RE_DEBUG_FLAGS_DECL; | |
1659 | ||
be8e71aa YO |
1660 | /* We can't just allocate points here. We need to wrap it in |
1661 | * an SV so it gets freed properly if there is a croak while | |
1662 | * running the match */ | |
1663 | ENTER; | |
1664 | SAVETMPS; | |
1665 | sv_points=newSV(maxlen * sizeof(U8 *)); | |
1666 | SvCUR_set(sv_points, | |
1667 | maxlen * sizeof(U8 *)); | |
1668 | SvPOK_on(sv_points); | |
1669 | sv_2mortal(sv_points); | |
1670 | points=(U8**)SvPV_nolen(sv_points ); | |
1de06328 YO |
1671 | if ( trie_type != trie_utf8_fold |
1672 | && (trie->bitmap || OP(c)==AHOCORASICKC) ) | |
1673 | { | |
786e8c11 YO |
1674 | if (trie->bitmap) |
1675 | bitmap=(U8*)trie->bitmap; | |
1676 | else | |
1677 | bitmap=(U8*)ANYOF_BITMAP(c); | |
07be1b83 | 1678 | } |
786e8c11 YO |
1679 | /* this is the Aho-Corasick algorithm modified a touch |
1680 | to include special handling for long "unknown char" | |
1681 | sequences. The basic idea being that we use AC as long | |
1682 | as we are dealing with a possible matching char, when | |
1683 | we encounter an unknown char (and we have not encountered | |
1684 | an accepting state) we scan forward until we find a legal | |
1685 | starting char. | |
1686 | AC matching is basically that of trie matching, except | |
1687 | that when we encounter a failing transition, we fall back | |
1688 | to the current states "fail state", and try the current char | |
1689 | again, a process we repeat until we reach the root state, | |
1690 | state 1, or a legal transition. If we fail on the root state | |
1691 | then we can either terminate if we have reached an accepting | |
1692 | state previously, or restart the entire process from the beginning | |
1693 | if we have not. | |
1694 | ||
1695 | */ | |
07be1b83 YO |
1696 | while (s <= last_start) { |
1697 | const U32 uniflags = UTF8_ALLOW_DEFAULT; | |
1698 | U8 *uc = (U8*)s; | |
1699 | U16 charid = 0; | |
1700 | U32 base = 1; | |
1701 | U32 state = 1; | |
1702 | UV uvc = 0; | |
1703 | STRLEN len = 0; | |
1704 | STRLEN foldlen = 0; | |
1705 | U8 *uscan = (U8*)NULL; | |
1706 | U8 *leftmost = NULL; | |
786e8c11 YO |
1707 | #ifdef DEBUGGING |
1708 | U32 accepted_word= 0; | |
1709 | #endif | |
07be1b83 YO |
1710 | U32 pointpos = 0; |
1711 | ||
1712 | while ( state && uc <= (U8*)strend ) { | |
1713 | int failed=0; | |
786e8c11 YO |
1714 | U32 word = aho->states[ state ].wordnum; |
1715 | ||
1de06328 YO |
1716 | if( state==1 ) { |
1717 | if ( bitmap ) { | |
1718 | DEBUG_TRIE_EXECUTE_r( | |
1719 | if ( uc <= (U8*)last_start && !BITMAP_TEST(bitmap,*uc) ) { | |
1720 | dump_exec_pos( (char *)uc, c, strend, real_start, | |
1721 | (char *)uc, do_utf8 ); | |
1722 | PerlIO_printf( Perl_debug_log, | |
1723 | " Scanning for legal start char...\n"); | |
1724 | } | |
1725 | ); | |
1726 | while ( uc <= (U8*)last_start && !BITMAP_TEST(bitmap,*uc) ) { | |
1727 | uc++; | |
786e8c11 | 1728 | } |
1de06328 | 1729 | s= (char *)uc; |
786e8c11 | 1730 | } |
786e8c11 YO |
1731 | if (uc >(U8*)last_start) break; |
1732 | } | |
1733 | ||
1734 | if ( word ) { | |
1735 | U8 *lpos= points[ (pointpos - trie->wordlen[word-1] ) % maxlen ]; | |
1736 | if (!leftmost || lpos < leftmost) { | |
1737 | DEBUG_r(accepted_word=word); | |
07be1b83 | 1738 | leftmost= lpos; |
786e8c11 | 1739 | } |
07be1b83 | 1740 | if (base==0) break; |
786e8c11 | 1741 | |
07be1b83 YO |
1742 | } |
1743 | points[pointpos++ % maxlen]= uc; | |
55eed653 NC |
1744 | REXEC_TRIE_READ_CHAR(trie_type, trie, widecharmap, uc, |
1745 | uscan, len, uvc, charid, foldlen, | |
1746 | foldbuf, uniflags); | |
786e8c11 YO |
1747 | DEBUG_TRIE_EXECUTE_r({ |
1748 | dump_exec_pos( (char *)uc, c, strend, real_start, | |
1749 | s, do_utf8 ); | |
07be1b83 | 1750 | PerlIO_printf(Perl_debug_log, |
786e8c11 YO |
1751 | " Charid:%3u CP:%4"UVxf" ", |
1752 | charid, uvc); | |
1753 | }); | |
07be1b83 YO |
1754 | |
1755 | do { | |
6148ee25 | 1756 | #ifdef DEBUGGING |
786e8c11 | 1757 | word = aho->states[ state ].wordnum; |
6148ee25 | 1758 | #endif |
07be1b83 YO |
1759 | base = aho->states[ state ].trans.base; |
1760 | ||
786e8c11 YO |
1761 | DEBUG_TRIE_EXECUTE_r({ |
1762 | if (failed) | |
1763 | dump_exec_pos( (char *)uc, c, strend, real_start, | |
1764 | s, do_utf8 ); | |
07be1b83 | 1765 | PerlIO_printf( Perl_debug_log, |
786e8c11 YO |
1766 | "%sState: %4"UVxf", word=%"UVxf, |
1767 | failed ? " Fail transition to " : "", | |
1768 | (UV)state, (UV)word); | |
1769 | }); | |
07be1b83 YO |
1770 | if ( base ) { |
1771 | U32 tmp; | |
1772 | if (charid && | |
1773 | (base + charid > trie->uniquecharcount ) | |
1774 | && (base + charid - 1 - trie->uniquecharcount | |
1775 | < trie->lasttrans) | |
1776 | && trie->trans[base + charid - 1 - | |
1777 | trie->uniquecharcount].check == state | |
1778 | && (tmp=trie->trans[base + charid - 1 - | |
1779 | trie->uniquecharcount ].next)) | |
1780 | { | |
786e8c11 YO |
1781 | DEBUG_TRIE_EXECUTE_r( |
1782 | PerlIO_printf( Perl_debug_log," - legal\n")); | |
07be1b83 YO |
1783 | state = tmp; |
1784 | break; | |
1785 | } | |
1786 | else { | |
786e8c11 YO |
1787 | DEBUG_TRIE_EXECUTE_r( |
1788 | PerlIO_printf( Perl_debug_log," - fail\n")); | |
1789 | failed = 1; | |
1790 | state = aho->fail[state]; | |
07be1b83 YO |
1791 | } |
1792 | } | |
1793 | else { | |
1794 | /* we must be accepting here */ | |
786e8c11 YO |
1795 | DEBUG_TRIE_EXECUTE_r( |
1796 | PerlIO_printf( Perl_debug_log," - accepting\n")); | |
1797 | failed = 1; | |
07be1b83 YO |
1798 | break; |
1799 | } | |
1800 | } while(state); | |
786e8c11 | 1801 | uc += len; |
07be1b83 YO |
1802 | if (failed) { |
1803 | if (leftmost) | |
1804 | break; | |
786e8c11 | 1805 | if (!state) state = 1; |
07be1b83 YO |
1806 | } |
1807 | } | |
1808 | if ( aho->states[ state ].wordnum ) { | |
1809 | U8 *lpos = points[ (pointpos - trie->wordlen[aho->states[ state ].wordnum-1]) % maxlen ]; | |
786e8c11 YO |
1810 | if (!leftmost || lpos < leftmost) { |
1811 | DEBUG_r(accepted_word=aho->states[ state ].wordnum); | |
07be1b83 | 1812 | leftmost = lpos; |
786e8c11 | 1813 | } |
07be1b83 | 1814 | } |
07be1b83 YO |
1815 | if (leftmost) { |
1816 | s = (char*)leftmost; | |
786e8c11 YO |
1817 | DEBUG_TRIE_EXECUTE_r({ |
1818 | PerlIO_printf( | |
70685ca0 JH |
1819 | Perl_debug_log,"Matches word #%"UVxf" at position %"IVdf". Trying full pattern...\n", |
1820 | (UV)accepted_word, (IV)(s - real_start) | |
786e8c11 YO |
1821 | ); |
1822 | }); | |
24b23f37 | 1823 | if (!reginfo || regtry(reginfo, &s)) { |
be8e71aa YO |
1824 | FREETMPS; |
1825 | LEAVE; | |
07be1b83 | 1826 | goto got_it; |
be8e71aa | 1827 | } |
07be1b83 | 1828 | s = HOPc(s,1); |
786e8c11 YO |
1829 | DEBUG_TRIE_EXECUTE_r({ |
1830 | PerlIO_printf( Perl_debug_log,"Pattern failed. Looking for new start point...\n"); | |
1831 | }); | |
07be1b83 | 1832 | } else { |
786e8c11 YO |
1833 | DEBUG_TRIE_EXECUTE_r( |
1834 | PerlIO_printf( Perl_debug_log,"No match.\n")); | |
07be1b83 YO |
1835 | break; |
1836 | } | |
1837 | } | |
be8e71aa YO |
1838 | FREETMPS; |
1839 | LEAVE; | |
07be1b83 YO |
1840 | } |
1841 | break; | |
b3c9acc1 | 1842 | default: |
3c3eec57 GS |
1843 | Perl_croak(aTHX_ "panic: unknown regstclass %d", (int)OP(c)); |
1844 | break; | |
d6a28714 | 1845 | } |
6eb5f6b9 JH |
1846 | return 0; |
1847 | got_it: | |
1848 | return s; | |
1849 | } | |
1850 | ||
fae667d5 | 1851 | |
6eb5f6b9 JH |
1852 | /* |
1853 | - regexec_flags - match a regexp against a string | |
1854 | */ | |
1855 | I32 | |
288b8c02 | 1856 | Perl_regexec_flags(pTHX_ REGEXP * const rx, char *stringarg, register char *strend, |
6eb5f6b9 JH |
1857 | char *strbeg, I32 minend, SV *sv, void *data, U32 flags) |
1858 | /* strend: pointer to null at end of string */ | |
1859 | /* strbeg: real beginning of string */ | |
1860 | /* minend: end of match must be >=minend after stringarg. */ | |
58e23c8d YO |
1861 | /* data: May be used for some additional optimizations. |
1862 | Currently its only used, with a U32 cast, for transmitting | |
1863 | the ganch offset when doing a /g match. This will change */ | |
6eb5f6b9 JH |
1864 | /* nosave: For optimizations. */ |
1865 | { | |
97aff369 | 1866 | dVAR; |
288b8c02 | 1867 | struct regexp *const prog = (struct regexp *)SvANY(rx); |
24b23f37 | 1868 | /*register*/ char *s; |
6eb5f6b9 | 1869 | register regnode *c; |
24b23f37 | 1870 | /*register*/ char *startpos = stringarg; |
6eb5f6b9 JH |
1871 | I32 minlen; /* must match at least this many chars */ |
1872 | I32 dontbother = 0; /* how many characters not to try at end */ | |
6eb5f6b9 JH |
1873 | I32 end_shift = 0; /* Same for the end. */ /* CC */ |
1874 | I32 scream_pos = -1; /* Internal iterator of scream. */ | |
ccac19ea | 1875 | char *scream_olds = NULL; |
f2338a2e | 1876 | const bool do_utf8 = cBOOL(DO_UTF8(sv)); |
2757e526 | 1877 | I32 multiline; |
f8fc2ecf | 1878 | RXi_GET_DECL(prog,progi); |
3b0527fe | 1879 | regmatch_info reginfo; /* create some info to pass to regtry etc */ |
e9105d30 | 1880 | regexp_paren_pair *swap = NULL; |
a3621e74 YO |
1881 | GET_RE_DEBUG_FLAGS_DECL; |
1882 | ||
7918f24d | 1883 | PERL_ARGS_ASSERT_REGEXEC_FLAGS; |
9d4ba2ae | 1884 | PERL_UNUSED_ARG(data); |
6eb5f6b9 JH |
1885 | |
1886 | /* Be paranoid... */ | |
1887 | if (prog == NULL || startpos == NULL) { | |
1888 | Perl_croak(aTHX_ "NULL regexp parameter"); | |
1889 | return 0; | |
1890 | } | |
1891 | ||
bbe252da | 1892 | multiline = prog->extflags & RXf_PMf_MULTILINE; |
288b8c02 | 1893 | reginfo.prog = rx; /* Yes, sorry that this is confusing. */ |
2757e526 | 1894 | |
288b8c02 | 1895 | RX_MATCH_UTF8_set(rx, do_utf8); |
1de06328 | 1896 | DEBUG_EXECUTE_r( |
efd26800 | 1897 | debug_start_match(rx, do_utf8, startpos, strend, |
1de06328 YO |
1898 | "Matching"); |
1899 | ); | |
bac06658 | 1900 | |
6eb5f6b9 | 1901 | minlen = prog->minlen; |
1de06328 YO |
1902 | |
1903 | if (strend - startpos < (minlen+(prog->check_offset_min<0?prog->check_offset_min:0))) { | |
a3621e74 | 1904 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, |
a72c7584 JH |
1905 | "String too short [regexec_flags]...\n")); |
1906 | goto phooey; | |
1aa99e6b | 1907 | } |
6eb5f6b9 | 1908 | |
1de06328 | 1909 | |
6eb5f6b9 | 1910 | /* Check validity of program. */ |
f8fc2ecf | 1911 | if (UCHARAT(progi->program) != REG_MAGIC) { |
6eb5f6b9 JH |
1912 | Perl_croak(aTHX_ "corrupted regexp program"); |
1913 | } | |
1914 | ||
1915 | PL_reg_flags = 0; | |
1916 | PL_reg_eval_set = 0; | |
1917 | PL_reg_maxiter = 0; | |
1918 | ||
3c8556c3 | 1919 | if (RX_UTF8(rx)) |
6eb5f6b9 JH |
1920 | PL_reg_flags |= RF_utf8; |
1921 | ||
1922 | /* Mark beginning of line for ^ and lookbehind. */ | |
3b0527fe | 1923 | reginfo.bol = startpos; /* XXX not used ??? */ |
6eb5f6b9 | 1924 | PL_bostr = strbeg; |
3b0527fe | 1925 | reginfo.sv = sv; |
6eb5f6b9 JH |
1926 | |
1927 | /* Mark end of line for $ (and such) */ | |
1928 | PL_regeol = strend; | |
1929 | ||
1930 | /* see how far we have to get to not match where we matched before */ | |
3b0527fe | 1931 | reginfo.till = startpos+minend; |
6eb5f6b9 | 1932 | |
6eb5f6b9 JH |
1933 | /* If there is a "must appear" string, look for it. */ |
1934 | s = startpos; | |
1935 | ||
bbe252da | 1936 | if (prog->extflags & RXf_GPOS_SEEN) { /* Need to set reginfo->ganch */ |
6eb5f6b9 | 1937 | MAGIC *mg; |
2c296965 | 1938 | if (flags & REXEC_IGNOREPOS){ /* Means: check only at start */ |
58e23c8d | 1939 | reginfo.ganch = startpos + prog->gofs; |
2c296965 | 1940 | DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log, |
ed549f2e | 1941 | "GPOS IGNOREPOS: reginfo.ganch = startpos + %"UVxf"\n",(UV)prog->gofs)); |
2c296965 | 1942 | } else if (sv && SvTYPE(sv) >= SVt_PVMG |
6eb5f6b9 | 1943 | && SvMAGIC(sv) |
14befaf4 DM |
1944 | && (mg = mg_find(sv, PERL_MAGIC_regex_global)) |
1945 | && mg->mg_len >= 0) { | |
3b0527fe | 1946 | reginfo.ganch = strbeg + mg->mg_len; /* Defined pos() */ |
2c296965 | 1947 | DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log, |
ed549f2e | 1948 | "GPOS MAGIC: reginfo.ganch = strbeg + %"IVdf"\n",(IV)mg->mg_len)); |
2c296965 | 1949 | |
bbe252da | 1950 | if (prog->extflags & RXf_ANCH_GPOS) { |
3b0527fe | 1951 | if (s > reginfo.ganch) |
6eb5f6b9 | 1952 | goto phooey; |
58e23c8d | 1953 | s = reginfo.ganch - prog->gofs; |
2c296965 | 1954 | DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log, |
ed549f2e | 1955 | "GPOS ANCH_GPOS: s = ganch - %"UVxf"\n",(UV)prog->gofs)); |
c584a96e YO |
1956 | if (s < strbeg) |
1957 | goto phooey; | |
6eb5f6b9 JH |
1958 | } |
1959 | } | |
58e23c8d | 1960 | else if (data) { |
70685ca0 | 1961 | reginfo.ganch = strbeg + PTR2UV(data); |
2c296965 YO |
1962 | DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log, |
1963 | "GPOS DATA: reginfo.ganch= strbeg + %"UVxf"\n",PTR2UV(data))); | |
1964 | ||
1965 | } else { /* pos() not defined */ | |
3b0527fe | 1966 | reginfo.ganch = strbeg; |
2c296965 YO |
1967 | DEBUG_GPOS_r(PerlIO_printf(Perl_debug_log, |
1968 | "GPOS: reginfo.ganch = strbeg\n")); | |
1969 | } | |
6eb5f6b9 | 1970 | } |
288b8c02 | 1971 | if (PL_curpm && (PM_GETRE(PL_curpm) == rx)) { |
e9105d30 GG |
1972 | /* We have to be careful. If the previous successful match |
1973 | was from this regex we don't want a subsequent partially | |
1974 | successful match to clobber the old results. | |
1975 | So when we detect this possibility we add a swap buffer | |
1976 | to the re, and switch the buffer each match. If we fail | |
1977 | we switch it back, otherwise we leave it swapped. | |
1978 | */ | |
1979 | swap = prog->offs; | |
1980 | /* do we need a save destructor here for eval dies? */ | |
1981 | Newxz(prog->offs, (prog->nparens + 1), regexp_paren_pair); | |
c74340f9 | 1982 | } |
a0714e2c | 1983 | if (!(flags & REXEC_CHECKED) && (prog->check_substr != NULL || prog->check_utf8 != NULL)) { |
6eb5f6b9 JH |
1984 | re_scream_pos_data d; |
1985 | ||
1986 | d.scream_olds = &scream_olds; | |
1987 | d.scream_pos = &scream_pos; | |
288b8c02 | 1988 | s = re_intuit_start(rx, sv, s, strend, flags, &d); |
3fa9c3d7 | 1989 | if (!s) { |
a3621e74 | 1990 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Not present...\n")); |
6eb5f6b9 | 1991 | goto phooey; /* not present */ |
3fa9c3d7 | 1992 | } |
6eb5f6b9 JH |
1993 | } |
1994 | ||
1de06328 | 1995 | |
6eb5f6b9 JH |
1996 | |
1997 | /* Simplest case: anchored match need be tried only once. */ | |
1998 | /* [unless only anchor is BOL and multiline is set] */ | |
bbe252da | 1999 | if (prog->extflags & (RXf_ANCH & ~RXf_ANCH_GPOS)) { |
24b23f37 | 2000 | if (s == startpos && regtry(®info, &startpos)) |
6eb5f6b9 | 2001 | goto got_it; |
bbe252da YO |
2002 | else if (multiline || (prog->intflags & PREGf_IMPLICIT) |
2003 | || (prog->extflags & RXf_ANCH_MBOL)) /* XXXX SBOL? */ | |
6eb5f6b9 JH |
2004 | { |
2005 | char *end; | |
2006 | ||
2007 | if (minlen) | |
2008 | dontbother = minlen - 1; | |
1aa99e6b | 2009 | end = HOP3c(strend, -dontbother, strbeg) - 1; |
6eb5f6b9 | 2010 | /* for multiline we only have to try after newlines */ |
33b8afdf | 2011 | if (prog->check_substr || prog->check_utf8) { |
6eb5f6b9 JH |
2012 | if (s == startpos) |
2013 | goto after_try; | |
2014 | while (1) { | |
24b23f37 | 2015 | if (regtry(®info, &s)) |
6eb5f6b9 JH |
2016 | goto got_it; |
2017 | after_try: | |
5339e136 | 2018 | if (s > end) |
6eb5f6b9 | 2019 | goto phooey; |
bbe252da | 2020 | if (prog->extflags & RXf_USE_INTUIT) { |
288b8c02 | 2021 | s = re_intuit_start(rx, sv, s + 1, strend, flags, NULL); |
6eb5f6b9 JH |
2022 | if (!s) |
2023 | goto phooey; | |
2024 | } | |
2025 | else | |
2026 | s++; | |
2027 | } | |
2028 | } else { | |
2029 | if (s > startpos) | |
2030 | s--; | |
2031 | while (s < end) { | |
2032 | if (*s++ == '\n') { /* don't need PL_utf8skip here */ | |
24b23f37 | 2033 | if (regtry(®info, &s)) |
6eb5f6b9 JH |
2034 | goto got_it; |
2035 | } | |
2036 | } | |
2037 | } | |
2038 | } | |
2039 | goto phooey; | |
bbe252da | 2040 | } else if (RXf_GPOS_CHECK == (prog->extflags & RXf_GPOS_CHECK)) |
f9f4320a YO |
2041 | { |
2042 | /* the warning about reginfo.ganch being used without intialization | |
bbe252da | 2043 | is bogus -- we set it above, when prog->extflags & RXf_GPOS_SEEN |
f9f4320a | 2044 | and we only enter this block when the same bit is set. */ |
58e23c8d | 2045 | char *tmp_s = reginfo.ganch - prog->gofs; |
c584a96e YO |
2046 | |
2047 | if (tmp_s >= strbeg && regtry(®info, &tmp_s)) | |
6eb5f6b9 JH |
2048 | goto got_it; |
2049 | goto phooey; | |
2050 | } | |
2051 | ||
2052 | /* Messy cases: unanchored match. */ | |
bbe252da | 2053 | if ((prog->anchored_substr || prog->anchored_utf8) && prog->intflags & PREGf_SKIP) { |
6eb5f6b9 JH |
2054 | /* we have /x+whatever/ */ |
2055 | /* it must be a one character string (XXXX Except UTF?) */ | |
33b8afdf | 2056 | char ch; |
bf93d4cc GS |
2057 | #ifdef DEBUGGING |
2058 | int did_match = 0; | |
2059 | #endif | |
33b8afdf JH |
2060 | if (!(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr)) |
2061 | do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog); | |
3f7c398e | 2062 | ch = SvPVX_const(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr)[0]; |
bf93d4cc | 2063 | |
1aa99e6b | 2064 | if (do_utf8) { |
4cadc6a9 | 2065 | REXEC_FBC_SCAN( |
6eb5f6b9 | 2066 | if (*s == ch) { |
a3621e74 | 2067 | DEBUG_EXECUTE_r( did_match = 1 ); |
24b23f37 | 2068 | if (regtry(®info, &s)) goto got_it; |
6eb5f6b9 JH |
2069 | s += UTF8SKIP(s); |
2070 | while (s < strend && *s == ch) | |
2071 | s += UTF8SKIP(s); | |
2072 | } | |
4cadc6a9 | 2073 | ); |
6eb5f6b9 JH |
2074 | } |
2075 | else { | |
4cadc6a9 | 2076 | REXEC_FBC_SCAN( |
6eb5f6b9 | 2077 | if (*s == ch) { |
a3621e74 | 2078 | DEBUG_EXECUTE_r( did_match = 1 ); |
24b23f37 | 2079 | if (regtry(®info, &s)) goto got_it; |
6eb5f6b9 JH |
2080 | s++; |
2081 | while (s < strend && *s == ch) | |
2082 | s++; | |
2083 | } | |
4cadc6a9 | 2084 | ); |
6eb5f6b9 | 2085 | } |
a3621e74 | 2086 | DEBUG_EXECUTE_r(if (!did_match) |
bf93d4cc | 2087 | PerlIO_printf(Perl_debug_log, |
b7953727 JH |
2088 | "Did not find anchored character...\n") |
2089 | ); | |
6eb5f6b9 | 2090 | } |
a0714e2c SS |
2091 | else if (prog->anchored_substr != NULL |
2092 | || prog->anchored_utf8 != NULL | |
2093 | || ((prog->float_substr != NULL || prog->float_utf8 != NULL) | |
33b8afdf JH |
2094 | && prog->float_max_offset < strend - s)) { |
2095 | SV *must; | |
2096 | I32 back_max; | |
2097 | I32 back_min; | |
2098 | char *last; | |
6eb5f6b9 | 2099 | char *last1; /* Last position checked before */ |
bf93d4cc GS |
2100 | #ifdef DEBUGGING |
2101 | int did_match = 0; | |
2102 | #endif | |
33b8afdf JH |
2103 | if (prog->anchored_substr || prog->anchored_utf8) { |
2104 | if (!(do_utf8 ? prog->anchored_utf8 : prog->anchored_substr)) | |
2105 | do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog); | |
2106 | must = do_utf8 ? prog->anchored_utf8 : prog->anchored_substr; | |
2107 | back_max = back_min = prog->anchored_offset; | |
2108 | } else { | |
2109 | if (!(do_utf8 ? prog->float_utf8 : prog->float_substr)) | |
2110 | do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog); | |
2111 | must = do_utf8 ? prog->float_utf8 : prog->float_substr; | |
2112 | back_max = prog->float_max_offset; | |
2113 | back_min = prog->float_min_offset; | |
2114 | } | |
1de06328 YO |
2115 | |
2116 | ||
33b8afdf JH |
2117 | if (must == &PL_sv_undef) |
2118 | /* could not downgrade utf8 check substring, so must fail */ | |
2119 | goto phooey; | |
2120 | ||
1de06328 YO |
2121 | if (back_min<0) { |
2122 | last = strend; | |
2123 | } else { | |
2124 | last = HOP3c(strend, /* Cannot start after this */ | |
2125 | -(I32)(CHR_SVLEN(must) | |
2126 | - (SvTAIL(must) != 0) + back_min), strbeg); | |
2127 | } | |
6eb5f6b9 JH |
2128 | if (s > PL_bostr) |
2129 | last1 = HOPc(s, -1); | |
2130 | else | |
2131 | last1 = s - 1; /* bogus */ | |
2132 | ||
a0288114 | 2133 | /* XXXX check_substr already used to find "s", can optimize if |
6eb5f6b9 JH |
2134 | check_substr==must. */ |
2135 | scream_pos = -1; | |
2136 | dontbother = end_shift; | |
2137 | strend = HOPc(strend, -dontbother); | |
2138 | while ( (s <= last) && | |
9041c2e3 | 2139 | ((flags & REXEC_SCREAM) |
1de06328 | 2140 | ? (s = screaminstr(sv, must, HOP3c(s, back_min, (back_min<0 ? strbeg : strend)) - strbeg, |
6eb5f6b9 | 2141 | end_shift, &scream_pos, 0)) |
1de06328 | 2142 | : (s = fbm_instr((unsigned char*)HOP3(s, back_min, (back_min<0 ? strbeg : strend)), |
9041c2e3 | 2143 | (unsigned char*)strend, must, |
7fba1cd6 | 2144 | multiline ? FBMrf_MULTILINE : 0))) ) { |
4addbd3b | 2145 | /* we may be pointing at the wrong string */ |
07bc277f | 2146 | if ((flags & REXEC_SCREAM) && RXp_MATCH_COPIED(prog)) |
3f7c398e | 2147 | s = strbeg + (s - SvPVX_const(sv)); |
a3621e74 | 2148 | DEBUG_EXECUTE_r( did_match = 1 ); |
6eb5f6b9 JH |
2149 | if (HOPc(s, -back_max) > last1) { |
2150 | last1 = HOPc(s, -back_min); | |
2151 | s = HOPc(s, -back_max); | |
2152 | } | |
2153 | else { | |
52657f30 | 2154 | char * const t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1; |
6eb5f6b9 JH |
2155 | |
2156 | last1 = HOPc(s, -back_min); | |
52657f30 | 2157 | s = t; |
6eb5f6b9 | 2158 | } |
1aa99e6b | 2159 | if (do_utf8) { |
6eb5f6b9 | 2160 | while (s <= last1) { |
24b23f37 | 2161 | if (regtry(®info, &s)) |
6eb5f6b9 JH |
2162 | goto got_it; |
2163 | s += UTF8SKIP(s); | |
2164 | } | |
2165 | } | |
2166 | else { | |
2167 | while (s <= last1) { | |
24b23f37 | 2168 | if (regtry(®info, &s)) |
6eb5f6b9 JH |
2169 | goto got_it; |
2170 | s++; | |
2171 | } | |
2172 | } | |
2173 | } | |
ab3bbdeb YO |
2174 | DEBUG_EXECUTE_r(if (!did_match) { |
2175 | RE_PV_QUOTED_DECL(quoted, do_utf8, PERL_DEBUG_PAD_ZERO(0), | |
2176 | SvPVX_const(must), RE_SV_DUMPLEN(must), 30); | |
2177 | PerlIO_printf(Perl_debug_log, "Did not find %s substr %s%s...\n", | |
33b8afdf | 2178 | ((must == prog->anchored_substr || must == prog->anchored_utf8) |
bf93d4cc | 2179 | ? "anchored" : "floating"), |
ab3bbdeb YO |
2180 | quoted, RE_SV_TAIL(must)); |
2181 | }); | |
6eb5f6b9 JH |
2182 | goto phooey; |
2183 | } | |
f8fc2ecf | 2184 | else if ( (c = progi->regstclass) ) { |
f14c76ed | 2185 | if (minlen) { |
f8fc2ecf | 2186 | const OPCODE op = OP(progi->regstclass); |
66e933ab | 2187 | /* don't bother with what can't match */ |
786e8c11 | 2188 | if (PL_regkind[op] != EXACT && op != CANY && PL_regkind[op] != TRIE) |
f14c76ed RGS |
2189 | strend = HOPc(strend, -(minlen - 1)); |
2190 | } | |
a3621e74 | 2191 | DEBUG_EXECUTE_r({ |
be8e71aa | 2192 | SV * const prop = sv_newmortal(); |
32fc9b6a | 2193 | regprop(prog, prop, c); |
0df25f3d | 2194 | { |
02daf0ab | 2195 | RE_PV_QUOTED_DECL(quoted,do_utf8,PERL_DEBUG_PAD_ZERO(1), |
ab3bbdeb | 2196 | s,strend-s,60); |
0df25f3d | 2197 | PerlIO_printf(Perl_debug_log, |
ab3bbdeb | 2198 | "Matching stclass %.*s against %s (%d chars)\n", |
e4f74956 | 2199 | (int)SvCUR(prop), SvPVX_const(prop), |
ab3bbdeb | 2200 | quoted, (int)(strend - s)); |
0df25f3d | 2201 | } |
ffc61ed2 | 2202 | }); |
3b0527fe | 2203 | if (find_byclass(prog, c, s, strend, ®info)) |
6eb5f6b9 | 2204 | goto got_it; |
07be1b83 | 2205 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "Contradicts stclass... [regexec_flags]\n")); |
d6a28714 JH |
2206 | } |
2207 | else { | |
2208 | dontbother = 0; | |
a0714e2c | 2209 | if (prog->float_substr != NULL || prog->float_utf8 != NULL) { |
33b8afdf | 2210 | /* Trim the end. */ |
d6a28714 | 2211 | char *last; |
33b8afdf JH |
2212 | SV* float_real; |
2213 | ||
2214 | if (!(do_utf8 ? prog->float_utf8 : prog->float_substr)) | |
2215 | do_utf8 ? to_utf8_substr(prog) : to_byte_substr(prog); | |
2216 | float_real = do_utf8 ? prog->float_utf8 : prog->float_substr; | |
d6a28714 JH |
2217 | |
2218 | if (flags & REXEC_SCREAM) { | |
33b8afdf | 2219 | last = screaminstr(sv, float_real, s - strbeg, |
d6a28714 JH |
2220 | end_shift, &scream_pos, 1); /* last one */ |
2221 | if (!last) | |
ffc61ed2 | 2222 | last = scream_olds; /* Only one occurrence. */ |
4addbd3b | 2223 | /* we may be pointing at the wrong string */ |
07bc277f | 2224 | else if (RXp_MATCH_COPIED(prog)) |
3f7c398e | 2225 | s = strbeg + (s - SvPVX_const(sv)); |
b8c5462f | 2226 | } |
d6a28714 JH |
2227 | else { |
2228 | STRLEN len; | |
cfd0369c | 2229 | const char * const little = SvPV_const(float_real, len); |
d6a28714 | 2230 | |
33b8afdf | 2231 | if (SvTAIL(float_real)) { |
d6a28714 JH |
2232 | if (memEQ(strend - len + 1, little, len - 1)) |
2233 | last = strend - len + 1; | |
7fba1cd6 | 2234 | else if (!multiline) |
9041c2e3 | 2235 | last = memEQ(strend - len, little, len) |
bd61b366 | 2236 | ? strend - len : NULL; |
b8c5462f | 2237 | else |
d6a28714 JH |
2238 | goto find_last; |
2239 | } else { | |
2240 | find_last: | |
9041c2e3 | 2241 | if (len) |
d6a28714 | 2242 | last = rninstr(s, strend, little, little + len); |
b8c5462f | 2243 | else |
a0288114 | 2244 | last = strend; /* matching "$" */ |
b8c5462f | 2245 | } |
b8c5462f | 2246 | } |
bf93d4cc | 2247 | if (last == NULL) { |
6bda09f9 YO |
2248 | DEBUG_EXECUTE_r( |
2249 | PerlIO_printf(Perl_debug_log, | |
2250 | "%sCan't trim the tail, match fails (should not happen)%s\n", | |
2251 | PL_colors[4], PL_colors[5])); | |
bf93d4cc GS |
2252 | goto phooey; /* Should not happen! */ |
2253 | } | |
d6a28714 JH |
2254 | dontbother = strend - last + prog->float_min_offset; |
2255 | } | |
2256 | if (minlen && (dontbother < minlen)) | |
2257 | dontbother = minlen - 1; | |
2258 | strend -= dontbother; /* this one's always in bytes! */ | |
2259 | /* We don't know much -- general case. */ | |
1aa99e6b | 2260 | if (do_utf8) { |
d6a28714 | 2261 | for (;;) { |
24b23f37 | 2262 | if (regtry(®info, &s)) |
d6a28714 JH |
2263 | goto got_it; |
2264 | if (s >= strend) | |
2265 | break; | |
b8c5462f | 2266 | s += UTF8SKIP(s); |
d6a28714 JH |
2267 | }; |
2268 | } | |
2269 | else { | |
2270 | do { | |
24b23f37 | 2271 | if (regtry(®info, &s)) |
d6a28714 JH |
2272 | goto got_it; |
2273 | } while (s++ < strend); | |
2274 | } | |
2275 | } | |
2276 | ||
2277 | /* Failure. */ | |
2278 | goto phooey; | |
2279 | ||
2280 | got_it: | |
e9105d30 | 2281 | Safefree(swap); |
288b8c02 | 2282 | RX_MATCH_TAINTED_set(rx, PL_reg_flags & RF_tainted); |
d6a28714 | 2283 | |
19b95bf0 | 2284 | if (PL_reg_eval_set) |
4f639d21 | 2285 | restore_pos(aTHX_ prog); |
5daac39c NC |
2286 | if (RXp_PAREN_NAMES(prog)) |
2287 | (void)hv_iterinit(RXp_PAREN_NAMES(prog)); | |
d6a28714 JH |
2288 | |
2289 | /* make sure $`, $&, $', and $digit will work later */ | |
2290 | if ( !(flags & REXEC_NOT_FIRST) ) { | |
288b8c02 | 2291 | RX_MATCH_COPY_FREE(rx); |
d6a28714 | 2292 | if (flags & REXEC_COPY_STR) { |
be8e71aa | 2293 | const I32 i = PL_regeol - startpos + (stringarg - strbeg); |
f8c7b90f | 2294 | #ifdef PERL_OLD_COPY_ON_WRITE |
ed252734 NC |
2295 | if ((SvIsCOW(sv) |
2296 | || (SvFLAGS(sv) & CAN_COW_MASK) == CAN_COW_FLAGS)) { | |
2297 | if (DEBUG_C_TEST) { | |
2298 | PerlIO_printf(Perl_debug_log, | |
2299 | "Copy on write: regexp capture, type %d\n", | |
2300 | (int) SvTYPE(sv)); | |
2301 | } | |
2302 | prog->saved_copy = sv_setsv_cow(prog->saved_copy, sv); | |
d5263905 | 2303 | prog->subbeg = (char *)SvPVX_const(prog->saved_copy); |
ed252734 NC |
2304 | assert (SvPOKp(prog->saved_copy)); |
2305 | } else | |
2306 | #endif | |
2307 | { | |
288b8c02 | 2308 | RX_MATCH_COPIED_on(rx); |
ed252734 NC |
2309 | s = savepvn(strbeg, i); |
2310 | prog->subbeg = s; | |
2311 | } | |
d6a28714 | 2312 | prog->sublen = i; |
d6a28714 JH |
2313 | } |
2314 | else { | |
2315 | prog->subbeg = strbeg; | |
2316 | prog->sublen = PL_regeol - strbeg; /* strend may have been modified */ | |
2317 | } | |
2318 | } | |
9041c2e3 | 2319 | |
d6a28714 JH |
2320 | return 1; |
2321 | ||
2322 | phooey: | |
a3621e74 | 2323 | DEBUG_EXECUTE_r(PerlIO_printf(Perl_debug_log, "%sMatch failed%s\n", |
e4584336 | 2324 | PL_colors[4], PL_colors[5])); |
d6a28714 | 2325 | if (PL_reg_eval_set) |
4f639d21 | 2326 | restore_pos(aTHX_ prog); |
e9105d30 | 2327 | if (swap) { |
c74340f9 | 2328 | /* we failed :-( roll it back */ |
e9105d30 GG |
2329 | Safefree(prog->offs); |
2330 | prog->offs = swap; | |
2331 | } | |
2332 | ||
d6a28714 JH |
2333 | return 0; |
2334 | } | |
2335 | ||
6bda09f9 | 2336 | |
d6a28714 JH |
2337 | /* |
2338 | - regtry - try match at specific point | |
2339 | */ | |
2340 | STATIC I32 /* 0 failure, 1 success */ | |
24b23f37 | 2341 | S_regtry(pTHX_ regmatch_info *reginfo, char **startpos) |
d6a28714 | 2342 | { |
97aff369 | 2343 | dVAR; |
d6a28714 | 2344 | CHECKPOINT lastcp; |
288b8c02 NC |
2345 | REGEXP *const rx = reginfo->prog; |
2346 | regexp *const prog = (struct regexp *)SvANY(rx); | |
f8fc2ecf | 2347 | RXi_GET_DECL(prog,progi); |
a3621e74 | 2348 | GET_RE_DEBUG_FLAGS_DECL; |
7918f24d NC |
2349 | |
2350 | PERL_ARGS_ASSERT_REGTRY; | |
2351 | ||
24b23f37 | 2352 | reginfo->cutpoint=NULL; |
d6a28714 | 2353 | |
bbe252da | 2354 | if ((prog->extflags & RXf_EVAL_SEEN) && !PL_reg_eval_set) { |
d6a28714 JH |
2355 | MAGIC *mg; |
2356 | ||
2357 | PL_reg_eval_set = RS_init; | |
a3621e74 | 2358 | DEBUG_EXECUTE_r(DEBUG_s( |
b900a521 JH |
2359 | PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %"IVdf"\n", |
2360 | (IV)(PL_stack_sp - PL_stack_base)); | |
d6a28714 | 2361 | )); |
ea8d6ae1 | 2362 | SAVESTACK_CXPOS(); |
d6a28714 JH |
2363 | cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base; |
2364 | /* Otherwise OP_NEXTSTATE will free whatever on stack now. */ | |
2365 | SAVETMPS; | |
2366 | /* Apparently this is not needed, judging by wantarray. */ | |
e8347627 | 2367 | /* SAVEI8(cxstack[cxstack_ix].blk_gimme); |
d6a28714 JH |
2368 | cxstack[cxstack_ix].blk_gimme = G_SCALAR; */ |
2369 | ||
3b0527fe | 2370 | if (reginfo->sv) { |
d6a28714 | 2371 | /* Make $_ available to executed code. */ |
3b0527fe | 2372 | if (reginfo->sv != DEFSV) { |
59f00321 | 2373 | SAVE_DEFSV; |
414bf5ae | 2374 | DEFSV_set(reginfo->sv); |
b8c5462f | 2375 | } |
d6a28714 | 2376 | |
3b0527fe DM |
2377 | if (!(SvTYPE(reginfo->sv) >= SVt_PVMG && SvMAGIC(reginfo->sv) |
2378 | && (mg = mg_find(reginfo->sv, PERL_MAGIC_regex_global)))) { | |
d6a28714 | 2379 | /* prepare for quick setting of pos */ |
d300d9fa | 2380 | #ifdef PERL_OLD_COPY_ON_WRITE |
51a9ea20 NC |
2381 | if (SvIsCOW(reginfo->sv)) |
2382 | sv_force_normal_flags(reginfo->sv, 0); | |
d300d9fa | 2383 | #endif |
3dab1dad | 2384 | mg = sv_magicext(reginfo->sv, NULL, PERL_MAGIC_regex_global, |
d300d9fa | 2385 | &PL_vtbl_mglob, NULL, 0); |
d6a28714 | 2386 | mg->mg_len = -1; |
b8c5462f | 2387 | } |
d6a28714 JH |
2388 | PL_reg_magic = mg; |
2389 | PL_reg_oldpos = mg->mg_len; | |
4f639d21 | 2390 | SAVEDESTRUCTOR_X(restore_pos, prog); |
d6a28714 | 2391 | } |
09687e5a | 2392 | if (!PL_reg_curpm) { |
a02a5408 | 2393 | Newxz(PL_reg_curpm, 1, PMOP); |
09687e5a AB |
2394 | #ifdef USE_ITHREADS |
2395 | { | |
14a49a24 | 2396 | SV* const repointer = &PL_sv_undef; |
92313705 NC |
2397 | /* this regexp is also owned by the new PL_reg_curpm, which |
2398 | will try to free it. */ | |
d2ece331 | 2399 | av_push(PL_regex_padav, repointer); |
09687e5a AB |
2400 | PL_reg_curpm->op_pmoffset = av_len(PL_regex_padav); |
2401 | PL_regex_pad = AvARRAY(PL_regex_padav); | |
2402 | } | |
2403 | #endif | |
2404 | } | |
86c29d75 NC |
2405 | #ifdef USE_ITHREADS |
2406 | /* It seems that non-ithreads works both with and without this code. | |
2407 | So for efficiency reasons it seems best not to have the code | |
2408 | compiled when it is not needed. */ | |
92313705 NC |
2409 | /* This is safe against NULLs: */ |
2410 | ReREFCNT_dec(PM_GETRE(PL_reg_curpm)); | |
2411 | /* PM_reg_curpm owns a reference to this regexp. */ | |
2412 | ReREFCNT_inc(rx); | |
86c29d75 | 2413 | #endif |
288b8c02 | 2414 | PM_SETRE(PL_reg_curpm, rx); |
d6a28714 JH |
2415 | PL_reg_oldcurpm = PL_curpm; |
2416 | PL_curpm = PL_reg_curpm; | |
07bc277f | 2417 | if (RXp_MATCH_COPIED(prog)) { |
d6a28714 JH |
2418 | /* Here is a serious problem: we cannot rewrite subbeg, |
2419 | since it may be needed if this match fails. Thus | |
2420 | $` inside (?{}) could fail... */ | |
2421 | PL_reg_oldsaved = prog->subbeg; | |
2422 | PL_reg_oldsavedlen = prog->sublen; | |
f8c7b90f | 2423 | #ifdef PERL_OLD_COPY_ON_WRITE |
ed252734 NC |
2424 | PL_nrs = prog->saved_copy; |
2425 | #endif | |
07bc277f | 2426 | RXp_MATCH_COPIED_off(prog); |
d6a28714 JH |
2427 | } |
2428 | else | |
bd61b366 | 2429 | PL_reg_oldsaved = NULL; |
d6a28714 JH |
2430 | prog->subbeg = PL_bostr; |
2431 | prog->sublen = PL_regeol - PL_bostr; /* strend may have been modified */ | |
2432 | } | |
24b23f37 | 2433 | DEBUG_EXECUTE_r(PL_reg_starttry = *startpos); |
f0ab9afb | 2434 | prog->offs[0].start = *startpos - PL_bostr; |
24b23f37 | 2435 | PL_reginput = *startpos; |
d6a28714 | 2436 | PL_reglastparen = &prog->lastparen; |
a01268b5 | 2437 | PL_reglastcloseparen = &prog->lastcloseparen; |
d6a28714 | 2438 | prog->lastparen = 0; |
03994de8 | 2439 | prog->lastcloseparen = 0; |
d6a28714 | 2440 | PL_regsize = 0; |
f0ab9afb | 2441 | PL_regoffs = prog->offs; |
d6a28714 JH |
2442 | if (PL_reg_start_tmpl <= prog->nparens) { |
2443 | PL_reg_start_tmpl = prog->nparens*3/2 + 3; | |
2444 | if(PL_reg_start_tmp) | |
2445 | Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*); | |
2446 | else | |
a02a5408 | 2447 | Newx(PL_reg_start_tmp, PL_reg_start_tmpl, char*); |
d6a28714 JH |
2448 | } |
2449 | ||
2450 | /* XXXX What this code is doing here?!!! There should be no need | |
2451 | to do this again and again, PL_reglastparen should take care of | |
3dd2943c | 2452 | this! --ilya*/ |
dafc8851 JH |
2453 | |
2454 | /* Tests pat.t#187 and split.t#{13,14} seem to depend on this code. | |
2455 | * Actually, the code in regcppop() (which Ilya may be meaning by | |
daf18116 | 2456 | * PL_reglastparen), is not needed at all by the test suite |
225593e1 DM |
2457 | * (op/regexp, op/pat, op/split), but that code is needed otherwise |
2458 | * this erroneously leaves $1 defined: "1" =~ /^(?:(\d)x)?\d$/ | |
2459 | * Meanwhile, this code *is* needed for the | |
daf18116 JH |
2460 | * above-mentioned test suite tests to succeed. The common theme |
2461 | * on those tests seems to be returning null fields from matches. | |
225593e1 | 2462 | * --jhi updated by dapm */ |
dafc8851 | 2463 | #if 1 |
d6a28714 | 2464 | if (prog->nparens) { |
f0ab9afb | 2465 | regexp_paren_pair *pp = PL_regoffs; |
097eb12c | 2466 | register I32 i; |
eb160463 | 2467 | for (i = prog->nparens; i > (I32)*PL_reglastparen; i--) { |
f0ab9afb NC |
2468 | ++pp; |
2469 | pp->start = -1; | |
2470 | pp->end = -1; | |
d6a28714 JH |
2471 | } |
2472 | } | |
dafc8851 | 2473 | #endif |
02db2b7b | 2474 | REGCP_SET(lastcp); |
f8fc2ecf | 2475 | if (regmatch(reginfo, progi->program + 1)) { |
f0ab9afb | 2476 | PL_regoffs[0].end = PL_reginput - PL_bostr; |
d6a28714 JH |
2477 | return 1; |
2478 | } | |
24b23f37 YO |
2479 | if (reginfo->cutpoint) |
2480 | *startpos= reginfo->cutpoint; | |
02db2b7b | 2481 | REGCP_UNWIND(lastcp); |
d6a28714 JH |
2482 | return 0; |
2483 | } | |
2484 | ||
02db2b7b | 2485 | |
8ba1375e MJD |
2486 | #define sayYES goto yes |
2487 | #define sayNO goto no | |
262b90c4 | 2488 | #define sayNO_SILENT goto no_silent |
8ba1375e | 2489 | |
f9f4320a YO |
2490 | /* we dont use STMT_START/END here because it leads to |
2491 | "unreachable code" warnings, which are bogus, but distracting. */ | |
2492 | #define CACHEsayNO \ | |
c476f425 DM |
2493 | if (ST.cache_mask) \ |
2494 | PL_reg_poscache[ST.cache_offset] |= ST.cache_mask; \ | |
f9f4320a | 2495 | sayNO |
3298f257 | 2496 | |
a3621e74 | 2497 | /* this is used to determine how far from the left messages like |
265c4333 YO |
2498 | 'failed...' are printed. It should be set such that messages |
2499 | are inline with the regop output that created them. | |
a3621e74 | 2500 | */ |
265c4333 | 2501 | #define REPORT_CODE_OFF 32 |
a3621e74 YO |
2502 | |
2503 | ||
2504 | /* Make sure there is a test for this +1 options in re_tests */ | |
2505 | #define TRIE_INITAL_ACCEPT_BUFFLEN 4; | |
2506 | ||
40a82448 DM |
2507 | #define CHRTEST_UNINIT -1001 /* c1/c2 haven't been calculated yet */ |
2508 | #define CHRTEST_VOID -1000 /* the c1/c2 "next char" test should be skipped */ | |
9e137952 | 2509 | |
86545054 DM |
2510 | #define SLAB_FIRST(s) (&(s)->states[0]) |
2511 | #define SLAB_LAST(s) (&(s)->states[PERL_REGMATCH_SLAB_SLOTS-1]) | |
2512 | ||
5d9a96ca DM |
2513 | /* grab a new slab and return the first slot in it */ |
2514 | ||
2515 | STATIC regmatch_state * | |
2516 | S_push_slab(pTHX) | |
2517 | { | |
a35a87e7 | 2518 | #if PERL_VERSION < 9 && !defined(PERL_CORE) |
54df2634 NC |
2519 | dMY_CXT; |
2520 | #endif | |
5d9a96ca DM |
2521 | regmatch_slab *s = PL_regmatch_slab->next; |
2522 | if (!s) { | |
2523 | Newx(s, 1, regmatch_slab); | |
2524 | s->prev = PL_regmatch_slab; | |
2525 | s->next = NULL; | |
2526 | PL_regmatch_slab->next = s; | |
2527 | } | |
2528 | PL_regmatch_slab = s; | |
86545054 | 2529 | return SLAB_FIRST(s); |
5d9a96ca | 2530 | } |
5b47454d | 2531 | |
95b24440 | 2532 | |
40a82448 DM |
2533 | /* push a new state then goto it */ |
2534 | ||
2535 | #define PUSH_STATE_GOTO(state, node) \ | |
2536 | scan = node; \ | |
2537 | st->resume_state = state; \ | |
2538 | goto push_state; | |
2539 | ||
2540 | /* push a new state with success backtracking, then goto it */ | |
2541 | ||
2542 | #define PUSH_YES_STATE_GOTO(state, node) \ | |
2543 | scan = node; \ | |
2544 | st->resume_state = state; \ | |
2545 | goto push_yes_state; | |
2546 | ||
aa283a38 | 2547 | |
aa283a38 | 2548 | |
d6a28714 | 2549 | /* |
95b24440 | 2550 | |
bf1f174e DM |
2551 | regmatch() - main matching routine |
2552 | ||
2553 | This is basically one big switch statement in a loop. We execute an op, | |
2554 | set 'next' to point the next op, and continue. If we come to a point which | |
2555 | we may need to backtrack to on failure such as (A|B|C), we push a | |
2556 | backtrack state onto the backtrack stack. On failure, we pop the top | |
2557 | state, and re-enter the loop at the state indicated. If there are no more | |
2558 | states to pop, we return failure. | |
2559 | ||
2560 | Sometimes we also need to backtrack on success; for example /A+/, where | |
2561 | after successfully matching one A, we need to go back and try to | |
2562 | match another one; similarly for lookahead assertions: if the assertion | |
2563 | completes successfully, we backtrack to the state just before the assertion | |
2564 | and then carry on. In these cases, the pushed state is marked as | |
2565 | 'backtrack on success too'. This marking is in fact done by a chain of | |
2566 | pointers, each pointing to the previous 'yes' state. On success, we pop to | |
2567 | the nearest yes state, discarding any intermediate failure-only states. | |
2568 | Sometimes a yes state is pushed just to force some cleanup code to be | |
2569 | called at the end of a successful match or submatch; e.g. (??{$re}) uses | |
2570 | it to free the inner regex. | |
2571 | ||
2572 | Note that failure backtracking rewinds the cursor position, while | |
2573 | success backtracking leaves it alone. | |
2574 | ||
2575 | A pattern is complete when the END op is executed, while a subpattern | |
2576 | such as (?=foo) is complete when the SUCCESS op is executed. Both of these | |
2577 | ops trigger the "pop to last yes state if any, otherwise return true" | |
2578 | behaviour. | |
2579 | ||
2580 | A common convention in this function is to use A and B to refer to the two | |
2581 | subpatterns (or to the first nodes thereof) in patterns like /A*B/: so A is | |
2582 | the subpattern to be matched possibly multiple times, while B is the entire | |
2583 | rest of the pattern. Variable and state names reflect this convention. | |
2584 | ||
2585 | The states in the main switch are the union of ops and failure/success of | |
2586 | substates associated with with that op. For example, IFMATCH is the op | |
2587 | that does lookahead assertions /(?=A)B/ and so the IFMATCH state means | |
2588 | 'execute IFMATCH'; while IFMATCH_A is a state saying that we have just | |
2589 | successfully matched A and IFMATCH_A_fail is a state saying that we have | |
2590 | just failed to match A. Resume states always come in pairs. The backtrack | |
2591 | state we push is marked as 'IFMATCH_A', but when that is popped, we resume | |
2592 | at IFMATCH_A or IFMATCH_A_fail, depending on whether we are backtracking | |
2593 | on success or failure. | |
2594 | ||
2595 | The struct that holds a backtracking state is actually a big union, with | |
2596 | one variant for each major type of op. The variable st points to the | |
2597 | top-most backtrack struct. To make the code clearer, within each | |
2598 | block of code we #define ST to alias the relevant union. | |
2599 | ||
2600 | Here's a concrete example of a (vastly oversimplified) IFMATCH | |
2601 | implementation: | |
2602 | ||
2603 | switch (state) { | |
2604 | .... | |
2605 | ||
2606 | #define ST st->u.ifmatch | |
2607 | ||
2608 | case IFMATCH: // we are executing the IFMATCH op, (?=A)B | |
2609 | ST.foo = ...; // some state we wish to save | |
95b24440 | 2610 | ... |
bf1f174e DM |
2611 | // push a yes backtrack state with a resume value of |
2612 | // IFMATCH_A/IFMATCH_A_fail, then continue execution at the | |
2613 | // first node of A: | |
2614 | PUSH_YES_STATE_GOTO(IFMATCH_A, A); | |
2615 | // NOTREACHED | |
2616 | ||
2617 | case IFMATCH_A: // we have successfully executed A; now continue with B | |
2618 | next = B; | |
2619 | bar = ST.foo; // do something with the preserved value | |
2620 | break; | |
2621 | ||
2622 | case IFMATCH_A_fail: // A failed, so the assertion failed | |
2623 | ...; // do some housekeeping, then ... | |
2624 | sayNO; // propagate the failure | |
2625 | ||
2626 | #undef ST | |
95b24440 | 2627 | |
bf1f174e DM |
2628 | ... |
2629 | } | |
95b24440 | 2630 | |
bf1f174e DM |
2631 | For any old-timers reading this who are familiar with the old recursive |
2632 | approach, the code above is equivalent to: | |
95b24440 | 2633 | |
bf1f174e DM |
2634 | case IFMATCH: // we are executing the IFMATCH op, (?=A)B |
2635 | { | |
2636 | int foo = ... | |
95b24440 | 2637 | ... |
bf1f174e DM |
2638 | if (regmatch(A)) { |
2639 | next = B; | |
2640 | bar = foo; | |
2641 | break; | |
95b24440 | 2642 | } |
bf1f174e DM |
2643 | ...; // do some housekeeping, then ... |
2644 | sayNO; // propagate the failure | |
95b24440 | 2645 | } |
bf1f174e DM |
2646 | |
2647 | The topmost backtrack state, pointed to by st, is usually free. If you | |
2648 | want to claim it, populate any ST.foo fields in it with values you wish to | |
2649 | save, then do one of | |
2650 | ||
2651 | PUSH_STATE_GOTO(resume_state, node); | |
2652 | PUSH_YES_STATE_GOTO(resume_state, node); | |
2653 | ||
2654 | which sets that backtrack state's resume value to 'resume_state', pushes a | |
2655 | new free entry to the top of the backtrack stack, then goes to 'node'. | |
2656 | On backtracking, the free slot is popped, and the saved state becomes the | |
2657 | new free state. An ST.foo field in this new top state can be temporarily | |
2658 | accessed to retrieve values, but once the main loop is re-entered, it | |
2659 | becomes available for reuse. | |
2660 | ||
2661 | Note that the depth of the backtrack stack constantly increases during the | |
2662 | left-to-right execution of the pattern, rather than going up and down with | |
2663 | the pattern nesting. For example the stack is at its maximum at Z at the | |
2664 | end of the pattern, rather than at X in the following: | |
2665 | ||
2666 | /(((X)+)+)+....(Y)+....Z/ | |
2667 | ||
2668 | The only exceptions to this are lookahead/behind assertions and the cut, | |
2669 | (?>A), which pop all the backtrack states associated with A before | |
2670 | continuing. | |
2671 | ||
2672 | Bascktrack state structs are allocated in slabs of about 4K in size. | |
2673 | PL_regmatch_state and st always point to the currently active state, | |
2674 | and PL_regmatch_slab points to the slab currently containing | |
2675 | PL_regmatch_state. The first time regmatch() is called, the first slab is | |
2676 | allocated, and is never freed until interpreter destruction. When the slab | |
2677 | is full, a new one is allocated and chained to the end. At exit from | |
2678 | regmatch(), slabs allocated since entry are freed. | |
2679 | ||
2680 | */ | |
95b24440 | 2681 | |
40a82448 | 2682 | |
5bc10b2c | 2683 | #define DEBUG_STATE_pp(pp) \ |
265c4333 | 2684 | DEBUG_STATE_r({ \ |
5bc10b2c DM |
2685 | DUMP_EXEC_POS(locinput, scan, do_utf8); \ |
2686 | PerlIO_printf(Perl_debug_log, \ | |
5d458dd8 | 2687 | " %*s"pp" %s%s%s%s%s\n", \ |
5bc10b2c | 2688 | depth*2, "", \ |
13d6edb4 | 2689 | PL_reg_name[st->resume_state], \ |
5d458dd8 YO |
2690 | ((st==yes_state||st==mark_state) ? "[" : ""), \ |
2691 | ((st==yes_state) ? "Y" : ""), \ | |
2692 | ((st==mark_state) ? "M" : ""), \ | |
2693 | ((st==yes_state||st==mark_state) ? "]" : "") \ | |
2694 | ); \ | |
265c4333 | 2695 | }); |
5bc10b2c | 2696 | |
40a82448 | 2697 | |
3dab1dad | 2698 | #define REG_NODE_NUM(x) ((x) ? (int)((x)-prog) : -1) |
95b24440 | 2699 | |
3df15adc | 2700 | #ifdef DEBUGGING |
5bc10b2c | 2701 | |
ab3bbdeb | 2702 | STATIC void |
efd26800 | 2703 | S_debug_start_match(pTHX_ const REGEXP *prog, const bool do_utf8, |
ab3bbdeb YO |
2704 | const char *start, const char *end, const char *blurb) |
2705 | { | |
efd26800 | 2706 | const bool utf8_pat = RX_UTF8(prog) ? 1 : 0; |
7918f24d NC |
2707 | |
2708 | PERL_ARGS_ASSERT_DEBUG_START_MATCH; | |
2709 | ||
ab3bbdeb YO |
2710 | if (!PL_colorset) |
2711 | reginitcolors(); | |
2712 | { | |
2713 | RE_PV_QUOTED_DECL(s0, utf8_pat, PERL_DEBUG_PAD_ZERO(0), | |
d2c6dc5e | 2714 | RX_PRECOMP_const(prog), RX_PRELEN(prog), 60); |
ab3bbdeb YO |
2715 | |
2716 | RE_PV_QUOTED_DECL(s1, do_utf8, PERL_DEBUG_PAD_ZERO(1), | |
2717 | start, end - start, 60); | |
2718 | ||
2719 | PerlIO_printf(Perl_debug_log, | |
2720 | "%s%s REx%s %s against %s\n", | |
2721 | PL_colors[4], blurb, PL_colors[5], s0, s1); | |
2722 | ||
2723 | if (do_utf8||utf8_pat) | |
1de06328 YO |
2724 | PerlIO_printf(Perl_debug_log, "UTF-8 %s%s%s...\n", |
2725 | utf8_pat ? "pattern" : "", | |
2726 | utf8_pat && do_utf8 ? " and " : "", | |
2727 | do_utf8 ? "string" : "" | |
ab3bbdeb YO |
2728 | ); |
2729 | } | |
2730 | } | |
3df15adc YO |
2731 | |
2732 | STATIC void | |
786e8c11 YO |
2733 | S_dump_exec_pos(pTHX_ const char *locinput, |
2734 | const regnode *scan, | |
2735 | const char *loc_regeol, | |
2736 | const char *loc_bostr, | |
2737 | const char *loc_reg_starttry, | |
2738 | const bool do_utf8) | |
07be1b83 | 2739 | { |
786e8c11 | 2740 | const int docolor = *PL_colors[0] || *PL_colors[2] || *PL_colors[4]; |
07be1b83 | 2741 | const int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */ |
786e8c11 | 2742 | int l = (loc_regeol - locinput) > taill ? taill : (loc_regeol - locinput); |
07be1b83 YO |
2743 | /* The part of the string before starttry has one color |
2744 | (pref0_len chars), between starttry and current | |
2745 | position another one (pref_len - pref0_len chars), | |
2746 | after the current position the third one. | |
2747 | We assume that pref0_len <= pref_len, otherwise we | |
2748 | decrease pref0_len. */ | |
786e8c11 YO |
2749 | int pref_len = (locinput - loc_bostr) > (5 + taill) - l |
2750 | ? (5 + taill) - l : locinput - loc_bostr; | |
07be1b83 YO |
2751 | int pref0_len; |
2752 | ||
7918f24d NC |
2753 | PERL_ARGS_ASSERT_DUMP_EXEC_POS; |
2754 | ||
07be1b83 YO |
2755 | while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput - pref_len))) |
2756 | pref_len++; | |
786e8c11 YO |
2757 | pref0_len = pref_len - (locinput - loc_reg_starttry); |
2758 | if (l + pref_len < (5 + taill) && l < loc_regeol - locinput) | |
2759 | l = ( loc_regeol - locinput > (5 + taill) - pref_len | |
2760 | ? (5 + taill) - pref_len : loc_regeol - locinput); | |
07be1b83 YO |
2761 | while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput + l))) |
2762 | l--; | |
2763 | if (pref0_len < 0) | |
2764 | pref0_len = 0; | |
2765 | if (pref0_len > pref_len) | |
2766 | pref0_len = pref_len; | |
2767 | { | |
3df15adc | 2768 | const int is_uni = (do_utf8 && OP(scan) != CANY) ? 1 : 0; |
0df25f3d | 2769 | |
ab3bbdeb | 2770 | RE_PV_COLOR_DECL(s0,len0,is_uni,PERL_DEBUG_PAD(0), |
1de06328 | 2771 | (locinput - pref_len),pref0_len, 60, 4, 5); |
0df25f3d | 2772 | |
ab3bbdeb | 2773 | RE_PV_COLOR_DECL(s1,len1,is_uni,PERL_DEBUG_PAD(1), |
3df15adc | 2774 | (locinput - pref_len + pref0_len), |
1de06328 | 2775 | pref_len - pref0_len, 60, 2, 3); |
0df25f3d | 2776 | |
ab3bbdeb | 2777 | RE_PV_COLOR_DECL(s2,len2,is_uni,PERL_DEBUG_PAD(2), |
1de06328 | 2778 | locinput, loc_regeol - locinput, 10, 0, 1); |
0df25f3d | 2779 | |
1de06328 | 2780 | const STRLEN tlen=len0+len1+len2; |
3df15adc | 2781 | PerlIO_printf(Perl_debug_log, |
ab3bbdeb | 2782 | "%4"IVdf" <%.*s%.*s%s%.*s>%*s|", |
786e8c11 | 2783 | (IV)(locinput - loc_bostr), |
07be1b83 | 2784 | len0, s0, |
07be1b83 | 2785 | len1, s1, |
07be1b83 | 2786 | (docolor ? "" : "> <"), |
07be1b83 | 2787 | len2, s2, |
f9f4320a | 2788 | (int)(tlen > 19 ? 0 : 19 - tlen), |
07be1b83 YO |
2789 | ""); |
2790 | } | |
2791 | } | |
3df15adc | 2792 | |
07be1b83 YO |
2793 | #endif |
2794 | ||
0a4db386 YO |
2795 | /* reg_check_named_buff_matched() |
2796 | * Checks to see if a named buffer has matched. The data array of | |
2797 | * buffer numbers corresponding to the buffer is expected to reside | |
2798 | * in the regexp->data->data array in the slot stored in the ARG() of | |
2799 | * node involved. Note that this routine doesn't actually care about the | |
2800 | * name, that information is not preserved from compilation to execution. | |
2801 | * Returns the index of the leftmost defined buffer with the given name | |
2802 | * or 0 if non of the buffers matched. | |
2803 | */ | |
2804 | STATIC I32 | |
7918f24d NC |
2805 | S_reg_check_named_buff_matched(pTHX_ const regexp *rex, const regnode *scan) |
2806 | { | |
0a4db386 | 2807 | I32 n; |
f8fc2ecf | 2808 | RXi_GET_DECL(rex,rexi); |
ad64d0ec | 2809 | SV *sv_dat= MUTABLE_SV(rexi->data->data[ ARG( scan ) ]); |
0a4db386 | 2810 | I32 *nums=(I32*)SvPVX(sv_dat); |
7918f24d NC |
2811 | |
2812 | PERL_ARGS_ASSERT_REG_CHECK_NAMED_BUFF_MATCHED; | |
2813 | ||
0a4db386 YO |
2814 | for ( n=0; n<SvIVX(sv_dat); n++ ) { |
2815 | if ((I32)*PL_reglastparen >= nums[n] && | |
f0ab9afb | 2816 | PL_regoffs[nums[n]].end != -1) |
0a4db386 YO |
2817 | { |
2818 | return nums[n]; | |
2819 | } | |
2820 | } | |
2821 | return 0; | |
2822 | } | |
2823 | ||
2f554ef7 DM |
2824 | |
2825 | /* free all slabs above current one - called during LEAVE_SCOPE */ | |
2826 | ||
2827 | STATIC void | |
2828 | S_clear_backtrack_stack(pTHX_ void *p) | |
2829 | { | |
2830 | regmatch_slab *s = PL_regmatch_slab->next; | |
2831 | PERL_UNUSED_ARG(p); | |
2832 | ||
2833 | if (!s) | |
2834 | return; | |
2835 | PL_regmatch_slab->next = NULL; | |
2836 | while (s) { | |
2837 | regmatch_slab * const osl = s; | |
2838 | s = s->next; | |
2839 | Safefree(osl); | |
2840 | } | |
2841 | } | |
2842 | ||
2843 | ||
28d8d7f4 YO |
2844 | #define SETREX(Re1,Re2) \ |
2845 | if (PL_reg_eval_set) PM_SETRE((PL_reg_curpm), (Re2)); \ | |
2846 | Re1 = (Re2) | |
2847 | ||
d6a28714 | 2848 | STATIC I32 /* 0 failure, 1 success */ |
24b23f37 | 2849 | S_regmatch(pTHX_ regmatch_info *reginfo, regnode *prog) |
d6a28714 | 2850 | { |
a35a87e7 | 2851 | #if PERL_VERSION < 9 && !defined(PERL_CORE) |
54df2634 NC |
2852 | dMY_CXT; |
2853 | #endif | |
27da23d5 | 2854 | dVAR; |
95b24440 | 2855 | register const bool do_utf8 = PL_reg_match_utf8; |
4ad0818d | 2856 | const U32 uniflags = UTF8_ALLOW_DEFAULT; |
288b8c02 NC |
2857 | REGEXP *rex_sv = reginfo->prog; |
2858 | regexp *rex = (struct regexp *)SvANY(rex_sv); | |
f8fc2ecf | 2859 | RXi_GET_DECL(rex,rexi); |
2f554ef7 | 2860 | I32 oldsave; |
5d9a96ca DM |
2861 | /* the current state. This is a cached copy of PL_regmatch_state */ |
2862 | register regmatch_state *st; | |
5d9a96ca DM |
2863 | /* cache heavy used fields of st in registers */ |
2864 | register regnode *scan; | |
2865 | register regnode *next; | |
438e9bae | 2866 | register U32 n = 0; /* general value; init to avoid compiler warning */ |
24d3c4a9 | 2867 | register I32 ln = 0; /* len or last; init to avoid compiler warning */ |
5d9a96ca | 2868 | register char *locinput = PL_reginput; |
5d9a96ca | 2869 | register I32 nextchr; /* is always set to UCHARAT(locinput) */ |
24d3c4a9 | 2870 | |
b69b0499 | 2871 | bool result = 0; /* return value of S_regmatch */ |
24d3c4a9 | 2872 | int depth = 0; /* depth of backtrack stack */ |
4b196cd4 YO |
2873 | U32 nochange_depth = 0; /* depth of GOSUB recursion with nochange */ |
2874 | const U32 max_nochange_depth = | |
2875 | (3 * rex->nparens > MAX_RECURSE_EVAL_NOCHANGE_DEPTH) ? | |
2876 | 3 * rex->nparens : MAX_RECURSE_EVAL_NOCHANGE_DEPTH; | |
77cb431f DM |
2877 | regmatch_state *yes_state = NULL; /* state to pop to on success of |
2878 | subpattern */ | |
e2e6a0f1 YO |
2879 | /* mark_state piggy backs on the yes_state logic so that when we unwind |
2880 | the stack on success we can update the mark_state as we go */ | |
2881 | regmatch_state *mark_state = NULL; /* last mark state we have seen */ | |
faec1544 | 2882 | regmatch_state *cur_eval = NULL; /* most recent EVAL_AB state */ |
b8591aee | 2883 | struct regmatch_state *cur_curlyx = NULL; /* most recent curlyx */ |
40a82448 | 2884 | U32 state_num; |
5d458dd8 YO |
2885 | bool no_final = 0; /* prevent failure from backtracking? */ |
2886 | bool do_cutgroup = 0; /* no_final only until next branch/trie entry */ | |
e2e6a0f1 | 2887 | char *startpoint = PL_reginput; |
5d458dd8 YO |
2888 | SV *popmark = NULL; /* are we looking for a mark? */ |
2889 | SV *sv_commit = NULL; /* last mark name seen in failure */ | |
2890 | SV *sv_yes_mark = NULL; /* last mark name we have seen | |
2891 | during a successfull match */ | |
2892 | U32 lastopen = 0; /* last open we saw */ | |
2893 | bool has_cutgroup = RX_HAS_CUTGROUP(rex) ? 1 : 0; | |
19b95bf0 | 2894 | SV* const oreplsv = GvSV(PL_replgv); |
24d3c4a9 DM |
2895 | /* these three flags are set by various ops to signal information to |
2896 | * the very next op. They have a useful lifetime of exactly one loop | |
2897 | * iteration, and are not preserved or restored by state pushes/pops | |
2898 | */ | |
2899 | bool sw = 0; /* the condition value in (?(cond)a|b) */ | |
2900 | bool minmod = 0; /* the next "{n,m}" is a "{n,m}?" */ | |
2901 | int logical = 0; /* the following EVAL is: | |
2902 | 0: (?{...}) | |
2903 | 1: (?(?{...})X|Y) | |
2904 | 2: (??{...}) | |
2905 | or the following IFMATCH/UNLESSM is: | |
2906 | false: plain (?=foo) | |
2907 | true: used as a condition: (?(?=foo)) | |
2908 | */ | |
95b24440 | 2909 | #ifdef DEBUGGING |
e68ec53f | 2910 | GET_RE_DEBUG_FLAGS_DECL; |
d6a28714 JH |
2911 | #endif |
2912 | ||
7918f24d NC |
2913 | PERL_ARGS_ASSERT_REGMATCH; |
2914 | ||
3b57cd43 | 2915 | DEBUG_OPTIMISE_r( DEBUG_EXECUTE_r({ |
24b23f37 | 2916 | PerlIO_printf(Perl_debug_log,"regmatch start\n"); |
3b57cd43 | 2917 | })); |
5d9a96ca DM |
2918 | /* on first ever call to regmatch, allocate first slab */ |
2919 | if (!PL_regmatch_slab) { | |
2920 | Newx(PL_regmatch_slab, 1, regmatch_slab); | |
2921 | PL_regmatch_slab->prev = NULL; | |
2922 | PL_regmatch_slab->next = NULL; | |
86545054 | 2923 | PL_regmatch_state = SLAB_FIRST(PL_regmatch_slab); |
5d9a96ca DM |
2924 | } |
2925 | ||
2f554ef7 DM |
2926 | oldsave = PL_savestack_ix; |
2927 | SAVEDESTRUCTOR_X(S_clear_backtrack_stack, NULL); | |
2928 | SAVEVPTR(PL_regmatch_slab); | |
2929 | SAVEVPTR(PL_regmatch_state); | |
5d9a96ca DM |
2930 | |
2931 | /* grab next free state slot */ | |
2932 | st = ++PL_regmatch_state; | |
86545054 | 2933 | if (st > SLAB_LAST(PL_regmatch_slab)) |
5d9a96ca DM |
2934 | st = PL_regmatch_state = S_push_slab(aTHX); |
2935 | ||
d6a28714 JH |
2936 | /* Note that nextchr is a byte even in UTF */ |
2937 | nextchr = UCHARAT(locinput); | |
2938 | scan = prog; | |
2939 | while (scan != NULL) { | |
8ba1375e | 2940 | |
a3621e74 | 2941 | DEBUG_EXECUTE_r( { |
6136c704 | 2942 | SV * const prop = sv_newmortal(); |
1de06328 | 2943 | regnode *rnext=regnext(scan); |
786e8c11 | 2944 | DUMP_EXEC_POS( locinput, scan, do_utf8 ); |
32fc9b6a | 2945 | regprop(rex, prop, scan); |
07be1b83 YO |
2946 | |
2947 | PerlIO_printf(Perl_debug_log, | |
2948 | "%3"IVdf":%*s%s(%"IVdf")\n", | |
f8fc2ecf | 2949 | (IV)(scan - rexi->program), depth*2, "", |
07be1b83 | 2950 | SvPVX_const(prop), |
1de06328 | 2951 | (PL_regkind[OP(scan)] == END || !rnext) ? |
f8fc2ecf | 2952 | 0 : (IV)(rnext - rexi->program)); |
2a782b5b | 2953 | }); |
d6a28714 JH |
2954 | |
2955 | next = scan + NEXT_OFF(scan); | |
2956 | if (next == scan) | |
2957 | next = NULL; | |
40a82448 | 2958 | state_num = OP(scan); |
d6a28714 | 2959 | |
40a82448 | 2960 | reenter_switch: |
34a81e2b B |
2961 | |
2962 | assert(PL_reglastparen == &rex->lastparen); | |
2963 | assert(PL_reglastcloseparen == &rex->lastcloseparen); | |
2964 | assert(PL_regoffs == rex->offs); | |
2965 | ||
40a82448 | 2966 | switch (state_num) { |
d6a28714 | 2967 | case BOL: |
7fba1cd6 | 2968 | if (locinput == PL_bostr) |
d6a28714 | 2969 | { |
3b0527fe | 2970 | /* reginfo->till = reginfo->bol; */ |
b8c5462f JH |
2971 | break; |
2972 | } | |
d6a28714 JH |
2973 | sayNO; |
2974 | case MBOL: | |
12d33761 HS |
2975 | if (locinput == PL_bostr || |
2976 | ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n')) | |
d6a28714 | 2977 | { |
b8c5462f JH |
2978 | break; |
2979 | } | |
d6a28714 JH |
2980 | sayNO; |
2981 | case SBOL: | |
c2a73568 | 2982 | if (locinput == PL_bostr) |
b8c5462f | 2983 | break; |
d6a28714 JH |
2984 | sayNO; |
2985 | case GPOS: | |
3b0527fe | 2986 | if (locinput == reginfo->ganch) |
d6a28714 JH |
2987 | break; |
2988 | sayNO; | |
ee9b8eae YO |
2989 | |
2990 | case KEEPS: | |
2991 | /* update the startpoint */ | |
f0ab9afb | 2992 | st->u.keeper.val = PL_regoffs[0].start; |
ee9b8eae | 2993 | PL_reginput = locinput; |
f0ab9afb | 2994 | PL_regoffs[0].start = locinput - PL_bostr; |
ee9b8eae YO |
2995 | PUSH_STATE_GOTO(KEEPS_next, next); |
2996 | /*NOT-REACHED*/ | |
2997 | case KEEPS_next_fail: | |
2998 | /* rollback the start point change */ | |
f0ab9afb | 2999 | PL_regoffs[0].start = st->u.keeper.val; |
ee9b8eae YO |
3000 | sayNO_SILENT; |
3001 | /*NOT-REACHED*/ | |
d6a28714 | 3002 | case EOL: |
d6a28714 JH |
3003 | goto seol; |
3004 | case MEOL: | |
d6a28714 | 3005 | if ((nextchr || locinput < PL_regeol) && nextchr != '\n') |
b8c5462f | 3006 | sayNO; |
b8c5462f | 3007 | break; |
d6a28714 JH |
3008 | case SEOL: |
3009 | seol: | |
3010 | if ((nextchr || locinput < PL_regeol) && nextchr != '\n') | |
b8c5462f | 3011 | sayNO; |
d6a28714 | 3012 | if (PL_regeol - locinput > 1) |
b8c5462f | 3013 | sayNO; |
b8c5462f | 3014 | break; |
d6a28714 JH |
3015 | case EOS: |
3016 | if (PL_regeol != locinput) | |
b8c5462f | 3017 | sayNO; |
d6a28714 | 3018 | break; |
ffc61ed2 | 3019 | case SANY: |
d6a28714 | 3020 | if (!nextchr && locinput >= PL_regeol) |
4633a7c4 | 3021 | sayNO; |
f33976b4 DB |
3022 | if (do_utf8) { |
3023 | locinput += PL_utf8skip[nextchr]; | |
3024 | if (locinput > PL_regeol) | |
3025 | sayNO; | |
3026 | nextchr = UCHARAT(locinput); | |
3027 | } | |
3028 | else | |
3029 | nextchr = UCHARAT(++locinput); | |
3030 | break; | |
3031 | case CANY: | |
3032 | if (!nextchr && locinput >= PL_regeol) | |
3033 | sayNO; | |
b8c5462f | 3034 | nextchr = UCHARAT(++locinput); |
a0d0e21e | 3035 | break; |
ffc61ed2 | 3036 | case REG_ANY: |
1aa99e6b IH |
3037 | if ((!nextchr && locinput >= PL_regeol) || nextchr == '\n') |
3038 | sayNO; | |
3039 | if (do_utf8) { | |
b8c5462f | 3040 | locinput += PL_utf8skip[nextchr]; |
d6a28714 JH |
3041 | if (locinput > PL_regeol) |
3042 | sayNO; | |
a0ed51b3 | 3043 | nextchr = UCHARAT(locinput); |
a0ed51b3 | 3044 | } |
1aa99e6b IH |
3045 | else |
3046 | nextchr = UCHARAT(++locinput); | |
a0ed51b3 | 3047 | break; |
166ba7cd DM |
3048 | |
3049 | #undef ST | |
3050 | #define ST st->u.trie | |
786e8c11 YO |
3051 | case TRIEC: |
3052 | /* In this case the charclass data is available inline so | |
3053 | we can fail fast without a lot of extra overhead. | |
3054 | */ | |
3055 | if (scan->flags == EXACT || !do_utf8) { | |
3056 | if(!ANYOF_BITMAP_TEST(scan, *locinput)) { | |
3057 | DEBUG_EXECUTE_r( | |
3058 | PerlIO_printf(Perl_debug_log, | |
3059 | "%*s %sfailed to match trie start class...%s\n", | |
5bc10b2c | 3060 | REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5]) |
786e8c11 YO |
3061 | ); |
3062 | sayNO_SILENT; | |
3063 | /* NOTREACHED */ | |
3064 | } | |
3065 | } | |
3066 | /* FALL THROUGH */ | |
5b47454d | 3067 | case TRIE: |
3dab1dad | 3068 | { |
07be1b83 | 3069 | /* what type of TRIE am I? (utf8 makes this contextual) */ |
a0a388a1 | 3070 | DECL_TRIE_TYPE(scan); |
3dab1dad YO |
3071 | |
3072 | /* what trie are we using right now */ | |
be8e71aa | 3073 | reg_trie_data * const trie |
f8fc2ecf | 3074 | = (reg_trie_data*)rexi->data->data[ ARG( scan ) ]; |
85fbaab2 | 3075 | HV * widecharmap = MUTABLE_HV(rexi->data->data[ ARG( scan ) + 1 ]); |
3dab1dad | 3076 | U32 state = trie->startstate; |
166ba7cd | 3077 | |
3dab1dad YO |
3078 | if (trie->bitmap && trie_type != trie_utf8_fold && |
3079 | !TRIE_BITMAP_TEST(trie,*locinput) | |
3080 | ) { | |
3081 | if (trie->states[ state ].wordnum) { | |
3082 | DEBUG_EXECUTE_r( | |
3083 | PerlIO_printf(Perl_debug_log, | |
3084 | "%*s %smatched empty string...%s\n", | |
5bc10b2c | 3085 | REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5]) |
3dab1dad YO |
3086 | ); |
3087 | break; | |
3088 | } else { | |
3089 | DEBUG_EXECUTE_r( | |
3090 | PerlIO_printf(Perl_debug_log, | |
786e8c11 | 3091 | "%*s %sfailed to match trie start class...%s\n", |
5bc10b2c | 3092 | REPORT_CODE_OFF+depth*2, "", PL_colors[4], PL_colors[5]) |
3dab1dad YO |
3093 | ); |
3094 | sayNO_SILENT; | |
3095 | } | |
3096 | } | |
166ba7cd | 3097 | |
786e8c11 YO |
3098 | { |
3099 | U8 *uc = ( U8* )locinput; | |
3100 | ||
3101 | STRLEN len = 0; | |
3102 | STRLEN foldlen = 0; | |
3103 | U8 *uscan = (U8*)NULL; | |
3104 | STRLEN bufflen=0; | |
3105 | SV *sv_accept_buff = NULL; | |
3106 | U8 foldbuf[ UTF8_MAXBYTES_CASE + 1 ]; | |
3107 | ||
3108 | ST.accepted = 0; /* how many accepting states we have seen */ | |
3109 | ST.B = next; | |
3110 | ST.jump = trie->jump; | |
786e8c11 | 3111 | ST.me = scan; |
07be1b83 YO |
3112 | /* |
3113 | traverse the TRIE keeping track of all accepting states | |
3114 | we transition through until we get to a failing node. | |
3115 | */ | |
3116 | ||
a3621e74 | 3117 | while ( state && uc <= (U8*)PL_regeol ) { |
786e8c11 | 3118 | U32 base = trie->states[ state ].trans.base; |
f9f4320a | 3119 | UV uvc = 0; |
786e8c11 YO |
3120 | U16 charid; |
3121 | /* We use charid to hold the wordnum as we don't use it | |
3122 | for charid until after we have done the wordnum logic. | |
3123 | We define an alias just so that the wordnum logic reads | |
3124 | more naturally. */ | |
3125 | ||
3126 | #define got_wordnum charid | |
3127 | got_wordnum = trie->states[ state ].wordnum; | |
3128 | ||
3129 | if ( got_wordnum ) { | |
3130 | if ( ! ST.accepted ) { | |
5b47454d | 3131 | ENTER; |
6962fb1a | 3132 | SAVETMPS; /* XXX is this necessary? dmq */ |
5b47454d DM |
3133 | bufflen = TRIE_INITAL_ACCEPT_BUFFLEN; |
3134 | sv_accept_buff=newSV(bufflen * | |
3135 | sizeof(reg_trie_accepted) - 1); | |
786e8c11 | 3136 | SvCUR_set(sv_accept_buff, 0); |
5b47454d DM |
3137 | SvPOK_on(sv_accept_buff); |
3138 | sv_2mortal(sv_accept_buff); | |
166ba7cd DM |
3139 | SAVETMPS; |
3140 | ST.accept_buff = | |
5b47454d DM |
3141 | (reg_trie_accepted*)SvPV_nolen(sv_accept_buff ); |
3142 | } | |
786e8c11 | 3143 | do { |
166ba7cd | 3144 | if (ST.accepted >= bufflen) { |
5b47454d | 3145 | bufflen *= 2; |
166ba7cd | 3146 | ST.accept_buff =(reg_trie_accepted*) |
5b47454d DM |
3147 | SvGROW(sv_accept_buff, |
3148 | bufflen * sizeof(reg_trie_accepted)); | |
3149 | } | |
3150 | SvCUR_set(sv_accept_buff,SvCUR(sv_accept_buff) | |
3151 | + sizeof(reg_trie_accepted)); | |
a3621e74 | 3152 | |
786e8c11 YO |
3153 | |
3154 | ST.accept_buff[ST.accepted].wordnum = got_wordnum; | |
3155 | ST.accept_buff[ST.accepted].endpos = uc; | |
3156 | ++ST.accepted; | |
3157 | } while (trie->nextword && (got_wordnum= trie->nextword[got_wordnum])); | |
3158 | } | |
3159 | #undef got_wordnum | |
a3621e74 | 3160 | |
07be1b83 | 3161 | DEBUG_TRIE_EXECUTE_r({ |
786e8c11 | 3162 | DUMP_EXEC_POS( (char *)uc, scan, do_utf8 ); |
a3621e74 | 3163 | PerlIO_printf( Perl_debug_log, |
786e8c11 | 3164 | "%*s %sState: %4"UVxf" Accepted: %4"UVxf" ", |
5bc10b2c | 3165 | 2+depth * 2, "", PL_colors[4], |
786e8c11 | 3166 | (UV)state, (UV)ST.accepted ); |
07be1b83 | 3167 | }); |
a3621e74 YO |
3168 | |
3169 | if ( base ) { | |
55eed653 NC |
3170 | REXEC_TRIE_READ_CHAR(trie_type, trie, widecharmap, uc, |
3171 | uscan, len, uvc, charid, foldlen, | |
3172 | foldbuf, uniflags); | |
a3621e74 | 3173 | |
5b47454d DM |
3174 | if (charid && |
3175 | (base + charid > trie->uniquecharcount ) | |
3176 | && (base + charid - 1 - trie->uniquecharcount | |
3177 | < trie->lasttrans) | |
3178 | && trie->trans[base + charid - 1 - | |
3179 | trie->uniquecharcount].check == state) | |
3180 | { | |
3181 | state = trie->trans[base + charid - 1 - | |
3182 | trie->uniquecharcount ].next; | |
3183 | } | |
3184 | else { | |
3185 | state = 0; | |
3186 | } | |
3187 | uc += len; | |
3188 | ||
3189 | } | |
3190 | else { | |
a3621e74 YO |
3191 | state = 0; |
3192 | } | |
3193 | DEBUG_TRIE_EXECUTE_r( | |
e4584336 | 3194 | PerlIO_printf( Perl_debug_log, |
786e8c11 | 3195 | "Charid:%3x CP:%4"UVxf" After State: %4"UVxf"%s\n", |
e4584336 | 3196 | charid, uvc, (UV)state, PL_colors[5] ); |
a3621e74 YO |
3197 | ); |
3198 | } | |
166ba7cd | 3199 | if (!ST.accepted ) |
a3621e74 | 3200 | sayNO; |
a3621e74 | 3201 | |
166ba7cd DM |
3202 | DEBUG_EXECUTE_r( |
3203 | PerlIO_printf( Perl_debug_log, | |
3204 | "%*s %sgot %"IVdf" possible matches%s\n", | |
5bc10b2c | 3205 | REPORT_CODE_OFF + depth * 2, "", |
166ba7cd DM |
3206 | PL_colors[4], (IV)ST.accepted, PL_colors[5] ); |
3207 | ); | |
786e8c11 | 3208 | }} |
fae667d5 YO |
3209 | goto trie_first_try; /* jump into the fail handler */ |
3210 | /* NOTREACHED */ | |
166ba7cd | 3211 | case TRIE_next_fail: /* we failed - try next alterative */ |
fae667d5 YO |
3212 | if ( ST.jump) { |
3213 | REGCP_UNWIND(ST.cp); | |
3214 | for (n = *PL_reglastparen; n > ST.lastparen; n--) | |
f0ab9afb | 3215 | PL_regoffs[n].end = -1; |
fae667d5 YO |
3216 | *PL_reglastparen = n; |
3217 | } | |
3218 | trie_first_try: | |
5d458dd8 YO |
3219 | if (do_cutgroup) { |
3220 | do_cutgroup = 0; | |
3221 | no_final = 0; | |
3222 | } | |
fae667d5 YO |
3223 | |
3224 | if ( ST.jump) { | |
3225 | ST.lastparen = *PL_reglastparen; | |
3226 | REGCP_SET(ST.cp); | |
3227 | } | |
166ba7cd DM |
3228 | if ( ST.accepted == 1 ) { |
3229 | /* only one choice left - just continue */ | |
3230 | DEBUG_EXECUTE_r({ | |
2b8b4781 | 3231 | AV *const trie_words |
502c6561 | 3232 | = MUTABLE_AV(rexi->data->data[ARG(ST.me)+TRIE_WORDS_OFFSET]); |
2b8b4781 | 3233 | SV ** const tmp = av_fetch( trie_words, |
5c9f2f80 | 3234 | ST.accept_buff[ 0 ].wordnum-1, 0 ); |
de734bd5 A |
3235 | SV *sv= tmp ? sv_newmortal() : NULL; |
3236 | ||
166ba7cd DM |
3237 | PerlIO_printf( Perl_debug_log, |
3238 | "%*s %sonly one match left: #%d <%s>%s\n", | |
5bc10b2c | 3239 | REPORT_CODE_OFF+depth*2, "", PL_colors[4], |
166ba7cd | 3240 | ST.accept_buff[ 0 ].wordnum, |
de734bd5 A |
3241 | tmp ? pv_pretty(sv, SvPV_nolen_const(*tmp), SvCUR(*tmp), 0, |
3242 | PL_colors[0], PL_colors[1], | |
3243 | (SvUTF8(*tmp) ? PERL_PV_ESCAPE_UNI : 0) | |
3244 | ) | |
3245 | : "not compiled under -Dr", | |
166ba7cd DM |
3246 | PL_colors[5] ); |
3247 | }); | |
3248 | PL_reginput = (char *)ST.accept_buff[ 0 ].endpos; | |
3249 | /* in this case we free tmps/leave before we call regmatch | |
3250 | as we wont be using accept_buff again. */ | |
5d458dd8 | 3251 | |
166ba7cd DM |
3252 | locinput = PL_reginput; |
3253 | nextchr = UCHARAT(locinput); | |
5d458dd8 YO |
3254 | if ( !ST.jump || !ST.jump[ST.accept_buff[0].wordnum]) |
3255 | scan = ST.B; | |
3256 | else | |
3257 | scan = ST.me + ST.jump[ST.accept_buff[0].wordnum]; | |
3258 | if (!has_cutgroup) { | |
3259 | FREETMPS; | |
3260 | LEAVE; | |
3261 | } else { | |
3262 | ST.accepted--; | |
3263 | PUSH_YES_STATE_GOTO(TRIE_next, scan); | |
3264 | } | |
786e8c11 | 3265 | |
166ba7cd DM |
3266 | continue; /* execute rest of RE */ |
3267 | } | |
fae667d5 YO |
3268 | |
3269 | if ( !ST.accepted-- ) { | |
5d458dd8 YO |
3270 | DEBUG_EXECUTE_r({ |
3271 | PerlIO_printf( Perl_debug_log, | |
3272 | "%*s %sTRIE failed...%s\n", | |
3273 | REPORT_CODE_OFF+depth*2, "", | |
3274 | PL_colors[4], | |
3275 | PL_colors[5] ); | |
3276 | }); | |
166ba7cd DM |
3277 | FREETMPS; |
3278 | LEAVE; | |
5d458dd8 | 3279 | sayNO_SILENT; |
fae667d5 YO |
3280 | /*NOTREACHED*/ |
3281 | } | |
166ba7cd | 3282 | |
a3621e74 | 3283 | /* |
166ba7cd DM |
3284 | There are at least two accepting states left. Presumably |
3285 | the number of accepting states is going to be low, | |
3286 | typically two. So we simply scan through to find the one | |
3287 | with lowest wordnum. Once we find it, we swap the last | |
3288 | state into its place and decrement the size. We then try to | |
3289 | match the rest of the pattern at the point where the word | |
3290 | ends. If we succeed, control just continues along the | |
3291 | regex; if we fail we return here to try the next accepting | |
3292 | state | |
3293 | */ | |
a3621e74 | 3294 | |
166ba7cd DM |
3295 | { |
3296 | U32 best = 0; | |
3297 | U32 cur; | |
3298 | for( cur = 1 ; cur <= ST.accepted ; cur++ ) { | |
3299 | DEBUG_TRIE_EXECUTE_r( | |
f2278c82 | 3300 | PerlIO_printf( Perl_debug_log, |
166ba7cd | 3301 | "%*s %sgot %"IVdf" (%d) as best, looking at %"IVdf" (%d)%s\n", |
5bc10b2c | 3302 | REPORT_CODE_OFF + depth * 2, "", PL_colors[4], |
166ba7cd DM |
3303 | (IV)best, ST.accept_buff[ best ].wordnum, (IV)cur, |
3304 | ST.accept_buff[ cur ].wordnum, PL_colors[5] ); | |
3305 | ); | |
3306 | ||
3307 | if (ST.accept_buff[cur].wordnum < | |
3308 | ST.accept_buff[best].wordnum) | |
3309 | best = cur; | |
a3621e74 | 3310 | } |
166ba7cd DM |
3311 | |
3312 | DEBUG_EXECUTE_r({ | |
2b8b4781 | 3313 | AV *const trie_words |
502c6561 | 3314 | = MUTABLE_AV(rexi->data->data[ARG(ST.me)+TRIE_WORDS_OFFSET]); |
2b8b4781 | 3315 | SV ** const tmp = av_fetch( trie_words, |
5c9f2f80 | 3316 | ST.accept_buff[ best ].wordnum - 1, 0 ); |
7f69552c | 3317 | regnode *nextop=(!ST.jump || !ST.jump[ST.accept_buff[best].wordnum]) ? |
265c4333 | 3318 | ST.B : |
7f69552c | 3319 | ST.me + ST.jump[ST.accept_buff[best].wordnum]; |
de734bd5 A |
3320 | SV *sv= tmp ? sv_newmortal() : NULL; |
3321 | ||
265c4333 YO |
3322 | PerlIO_printf( Perl_debug_log, |
3323 | "%*s %strying alternation #%d <%s> at node #%d %s\n", | |
5bc10b2c | 3324 | REPORT_CODE_OFF+depth*2, "", PL_colors[4], |
166ba7cd | 3325 | ST.accept_buff[best].wordnum, |
de734bd5 A |
3326 | tmp ? pv_pretty(sv, SvPV_nolen_const(*tmp), SvCUR(*tmp), 0, |
3327 | PL_colors[0], PL_colors[1], | |
3328 | (SvUTF8(*tmp) ? PERL_PV_ESCAPE_UNI : 0) | |
3329 | ) : "not compiled under -Dr", | |
265c4333 | 3330 | REG_NODE_NUM(nextop), |
166ba7cd DM |
3331 | PL_colors[5] ); |
3332 | }); | |
3333 | ||
3334 | if ( best<ST.accepted ) { | |
3335 | reg_trie_accepted tmp = ST.accept_buff[ best ]; | |
3336 | ST.accept_buff[ best ] = ST.accept_buff[ ST.accepted ]; | |
3337 | ST.accept_buff[ ST.accepted ] = tmp; | |
3338 | best = ST.accepted; | |
a3621e74 | 3339 | } |
166ba7cd | 3340 | PL_reginput = (char *)ST.accept_buff[ best ].endpos; |
7f69552c | 3341 | if ( !ST.jump || !ST.jump[ST.accept_buff[best].wordnum]) { |
5d458dd8 | 3342 | scan = ST.B; |
786e8c11 | 3343 | } else { |
5d458dd8 | 3344 | scan = ST.me + ST.jump[ST.accept_buff[best].wordnum]; |
5d458dd8 | 3345 | } |
6b173516 | 3346 | PUSH_YES_STATE_GOTO(TRIE_next, scan); |
786e8c11 | 3347 | /* NOTREACHED */ |
166ba7cd | 3348 | } |
166ba7cd | 3349 | /* NOTREACHED */ |
5d458dd8 | 3350 | case TRIE_next: |
6962fb1a YO |
3351 | /* we dont want to throw this away, see bug 57042*/ |
3352 | if (oreplsv != GvSV(PL_replgv)) | |
3353 | sv_setsv(oreplsv, GvSV(PL_replgv)); | |
5d458dd8 YO |
3354 | FREETMPS; |
3355 | LEAVE; | |
3356 | sayYES; | |
166ba7cd DM |
3357 | #undef ST |
3358 | ||
95b24440 DM |
3359 | case EXACT: { |
3360 | char *s = STRING(scan); | |
24d3c4a9 | 3361 | ln = STR_LEN(scan); |
eb160463 | 3362 | if (do_utf8 != UTF) { |
bc517b45 | 3363 | /* The target and the pattern have differing utf8ness. */ |
1aa99e6b | 3364 | char *l = locinput; |
24d3c4a9 | 3365 | const char * const e = s + ln; |
a72c7584 | 3366 | |
5ff6fc6d JH |
3367 | if (do_utf8) { |
3368 | /* The target is utf8, the pattern is not utf8. */ | |
1aa99e6b | 3369 | while (s < e) { |
a3b680e6 | 3370 | STRLEN ulen; |
1aa99e6b | 3371 | if (l >= PL_regeol) |
5ff6fc6d JH |
3372 | sayNO; |
3373 | if (NATIVE_TO_UNI(*(U8*)s) != | |
89ebb4a3 | 3374 | utf8n_to_uvuni((U8*)l, UTF8_MAXBYTES, &ulen, |
041457d9 | 3375 | uniflags)) |
5ff6fc6d | 3376 | sayNO; |
bc517b45 | 3377 | l += ulen; |
5ff6fc6d | 3378 | s ++; |
1aa99e6b | 3379 | } |
5ff6fc6d JH |
3380 | } |
3381 | else { | |
3382 | /* The target is not utf8, the pattern is utf8. */ | |
1aa99e6b | 3383 | while (s < e) { |
a3b680e6 | 3384 | STRLEN ulen; |
1aa99e6b IH |
3385 | if (l >= PL_regeol) |
3386 | sayNO; | |
5ff6fc6d | 3387 | if (NATIVE_TO_UNI(*((U8*)l)) != |
89ebb4a3 | 3388 | utf8n_to_uvuni((U8*)s, UTF8_MAXBYTES, &ulen, |
041457d9 | 3389 | unifl |