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