Commit | Line | Data |
---|---|---|
a0d0e21e LW |
1 | /* regexec.c |
2 | */ | |
3 | ||
4 | /* | |
5 | * "One Ring to rule them all, One Ring to find them..." | |
6 | */ | |
7 | ||
a687059c LW |
8 | /* NOTE: this is derived from Henry Spencer's regexp code, and should not |
9 | * confused with the original package (see point 3 below). Thanks, Henry! | |
10 | */ | |
11 | ||
12 | /* Additional note: this code is very heavily munged from Henry's version | |
13 | * in places. In some spots I've traded clarity for efficiency, so don't | |
14 | * blame Henry for some of the lack of readability. | |
15 | */ | |
16 | ||
e50aee73 AD |
17 | /* The names of the functions have been changed from regcomp and |
18 | * regexec to pregcomp and pregexec in order to avoid conflicts | |
19 | * with the POSIX routines of the same names. | |
20 | */ | |
21 | ||
b9d5759e AD |
22 | #ifdef PERL_EXT_RE_BUILD |
23 | /* need to replace pregcomp et al, so enable that */ | |
24 | # ifndef PERL_IN_XSUB_RE | |
25 | # define PERL_IN_XSUB_RE | |
26 | # endif | |
27 | /* need access to debugger hooks */ | |
cad2e5aa | 28 | # if defined(PERL_EXT_RE_DEBUG) && !defined(DEBUGGING) |
b9d5759e AD |
29 | # define DEBUGGING |
30 | # endif | |
31 | #endif | |
32 | ||
33 | #ifdef PERL_IN_XSUB_RE | |
d06ea78c | 34 | /* We *really* need to overwrite these symbols: */ |
56953603 IZ |
35 | # define Perl_regexec_flags my_regexec |
36 | # define Perl_regdump my_regdump | |
37 | # define Perl_regprop my_regprop | |
cad2e5aa | 38 | # define Perl_re_intuit_start my_re_intuit_start |
d06ea78c GS |
39 | /* *These* symbols are masked to allow static link. */ |
40 | # define Perl_pregexec my_pregexec | |
9041c2e3 | 41 | # define Perl_reginitcolors my_reginitcolors |
490a3f88 | 42 | # define Perl_regclass_swash my_regclass_swash |
c5be433b GS |
43 | |
44 | # define PERL_NO_GET_CONTEXT | |
9041c2e3 | 45 | #endif |
56953603 | 46 | |
f0fcb552 | 47 | /*SUPPRESS 112*/ |
a687059c | 48 | /* |
e50aee73 | 49 | * pregcomp and pregexec -- regsub and regerror are not used in perl |
a687059c LW |
50 | * |
51 | * Copyright (c) 1986 by University of Toronto. | |
52 | * Written by Henry Spencer. Not derived from licensed software. | |
53 | * | |
54 | * Permission is granted to anyone to use this software for any | |
55 | * purpose on any computer system, and to redistribute it freely, | |
56 | * subject to the following restrictions: | |
57 | * | |
58 | * 1. The author is not responsible for the consequences of use of | |
59 | * this software, no matter how awful, even if they arise | |
60 | * from defects in it. | |
61 | * | |
62 | * 2. The origin of this software must not be misrepresented, either | |
63 | * by explicit claim or by omission. | |
64 | * | |
65 | * 3. Altered versions must be plainly marked as such, and must not | |
66 | * be misrepresented as being the original software. | |
67 | * | |
68 | **** Alterations to Henry's code are... | |
69 | **** | |
bc89e66f | 70 | **** Copyright (c) 1991-2001, Larry Wall |
a687059c | 71 | **** |
9ef589d8 LW |
72 | **** You may distribute under the terms of either the GNU General Public |
73 | **** License or the Artistic License, as specified in the README file. | |
a687059c LW |
74 | * |
75 | * Beware that some of this code is subtly aware of the way operator | |
76 | * precedence is structured in regular expressions. Serious changes in | |
77 | * regular-expression syntax might require a total rethink. | |
78 | */ | |
79 | #include "EXTERN.h" | |
864dbfa3 | 80 | #define PERL_IN_REGEXEC_C |
a687059c | 81 | #include "perl.h" |
0f5d15d6 | 82 | |
a687059c LW |
83 | #include "regcomp.h" |
84 | ||
c277df42 IZ |
85 | #define RF_tainted 1 /* tainted information used? */ |
86 | #define RF_warned 2 /* warned about big count? */ | |
ce862d02 | 87 | #define RF_evaled 4 /* Did an EVAL with setting? */ |
a0ed51b3 LW |
88 | #define RF_utf8 8 /* String contains multibyte chars? */ |
89 | ||
90 | #define UTF (PL_reg_flags & RF_utf8) | |
ce862d02 IZ |
91 | |
92 | #define RS_init 1 /* eval environment created */ | |
93 | #define RS_set 2 /* replsv value is set */ | |
c277df42 | 94 | |
a687059c LW |
95 | #ifndef STATIC |
96 | #define STATIC static | |
97 | #endif | |
98 | ||
c277df42 IZ |
99 | /* |
100 | * Forwards. | |
101 | */ | |
102 | ||
a0ed51b3 | 103 | #define CHR_SVLEN(sv) (UTF ? sv_len_utf8(sv) : SvCUR(sv)) |
53c4c00c | 104 | #define CHR_DIST(a,b) (PL_reg_match_utf8 ? utf8_distance(a,b) : a - b) |
a0ed51b3 | 105 | |
dfe13c55 GS |
106 | #define reghop_c(pos,off) ((char*)reghop((U8*)pos, off)) |
107 | #define reghopmaybe_c(pos,off) ((char*)reghopmaybe((U8*)pos, off)) | |
53c4c00c JH |
108 | #define HOP(pos,off) (PL_reg_match_utf8 ? reghop((U8*)pos, off) : (U8*)(pos + off)) |
109 | #define HOPMAYBE(pos,off) (PL_reg_match_utf8 ? reghopmaybe((U8*)pos, off) : (U8*)(pos + off)) | |
dfe13c55 GS |
110 | #define HOPc(pos,off) ((char*)HOP(pos,off)) |
111 | #define HOPMAYBEc(pos,off) ((char*)HOPMAYBE(pos,off)) | |
a0d0e21e | 112 | |
efb30f32 | 113 | #define HOPBACK(pos, off) ( \ |
53c4c00c | 114 | (UTF && PL_reg_match_utf8) \ |
efb30f32 HS |
115 | ? reghopmaybe((U8*)pos, -off) \ |
116 | : (pos - off >= PL_bostr) \ | |
117 | ? (U8*)(pos - off) \ | |
118 | : (U8*)NULL \ | |
119 | ) | |
120 | #define HOPBACKc(pos, off) (char*)HOPBACK(pos, off) | |
121 | ||
1aa99e6b IH |
122 | #define reghop3_c(pos,off,lim) ((char*)reghop3((U8*)pos, off, (U8*)lim)) |
123 | #define reghopmaybe3_c(pos,off,lim) ((char*)reghopmaybe3((U8*)pos, off, (U8*)lim)) | |
53c4c00c JH |
124 | #define HOP3(pos,off,lim) (PL_reg_match_utf8 ? reghop3((U8*)pos, off, (U8*)lim) : (U8*)(pos + off)) |
125 | #define HOPMAYBE3(pos,off,lim) (PL_reg_match_utf8 ? reghopmaybe3((U8*)pos, off, (U8*)lim) : (U8*)(pos + off)) | |
1aa99e6b IH |
126 | #define HOP3c(pos,off,lim) ((char*)HOP3(pos,off,lim)) |
127 | #define HOPMAYBE3c(pos,off,lim) ((char*)HOPMAYBE3(pos,off,lim)) | |
128 | ||
8269fa76 | 129 | #define LOAD_UTF8_CHARCLASS(a,b) STMT_START { if (!CAT2(PL_utf8_,a)) (void)CAT2(is_utf8_, a)((U8*)b); } STMT_END |
51371543 | 130 | |
5f80c4cf | 131 | /* for use after a quantifier and before an EXACT-like node -- japhy */ |
e2d8ce26 JP |
132 | #define JUMPABLE(rn) ( \ |
133 | OP(rn) == OPEN || OP(rn) == CLOSE || OP(rn) == EVAL || \ | |
cca55fe3 JP |
134 | OP(rn) == SUSPEND || OP(rn) == IFMATCH || \ |
135 | OP(rn) == PLUS || OP(rn) == MINMOD || \ | |
136 | (PL_regkind[(U8)OP(rn)] == CURLY && ARG1(rn) > 0) \ | |
e2d8ce26 JP |
137 | ) |
138 | ||
cca55fe3 JP |
139 | #define HAS_TEXT(rn) ( \ |
140 | PL_regkind[(U8)OP(rn)] == EXACT || PL_regkind[(U8)OP(rn)] == REF \ | |
141 | ) | |
e2d8ce26 | 142 | |
cca55fe3 | 143 | #define FIND_NEXT_IMPT(rn) STMT_START { \ |
e2d8ce26 | 144 | while (JUMPABLE(rn)) \ |
cca55fe3 JP |
145 | if (OP(rn) == SUSPEND || OP(rn) == IFMATCH || \ |
146 | PL_regkind[(U8)OP(rn)] == CURLY) \ | |
e2d8ce26 | 147 | rn = NEXTOPER(NEXTOPER(rn)); \ |
cca55fe3 JP |
148 | else if (OP(rn) == PLUS) \ |
149 | rn = NEXTOPER(rn); \ | |
e2d8ce26 | 150 | else rn += NEXT_OFF(rn); \ |
5f80c4cf | 151 | } STMT_END |
74750237 | 152 | |
acfe0abc | 153 | static void restore_pos(pTHX_ void *arg); |
51371543 | 154 | |
76e3520e | 155 | STATIC CHECKPOINT |
cea2e8a9 | 156 | S_regcppush(pTHX_ I32 parenfloor) |
a0d0e21e | 157 | { |
3280af22 | 158 | int retval = PL_savestack_ix; |
b1ce53c5 JH |
159 | #define REGCP_PAREN_ELEMS 4 |
160 | int paren_elems_to_push = (PL_regsize - parenfloor) * REGCP_PAREN_ELEMS; | |
a0d0e21e LW |
161 | int p; |
162 | ||
e49a9654 IH |
163 | if (paren_elems_to_push < 0) |
164 | Perl_croak(aTHX_ "panic: paren_elems_to_push < 0"); | |
165 | ||
a01268b5 | 166 | #define REGCP_OTHER_ELEMS 6 |
b1ce53c5 | 167 | SSCHECK(paren_elems_to_push + REGCP_OTHER_ELEMS); |
3280af22 | 168 | for (p = PL_regsize; p > parenfloor; p--) { |
b1ce53c5 | 169 | /* REGCP_PARENS_ELEMS are pushed per pairs of parentheses. */ |
cf93c79d IZ |
170 | SSPUSHINT(PL_regendp[p]); |
171 | SSPUSHINT(PL_regstartp[p]); | |
3280af22 | 172 | SSPUSHPTR(PL_reg_start_tmp[p]); |
a0d0e21e LW |
173 | SSPUSHINT(p); |
174 | } | |
b1ce53c5 | 175 | /* REGCP_OTHER_ELEMS are pushed in any case, parentheses or no. */ |
3280af22 NIS |
176 | SSPUSHINT(PL_regsize); |
177 | SSPUSHINT(*PL_reglastparen); | |
a01268b5 | 178 | SSPUSHINT(*PL_reglastcloseparen); |
3280af22 | 179 | SSPUSHPTR(PL_reginput); |
41123dfd JH |
180 | #define REGCP_FRAME_ELEMS 2 |
181 | /* REGCP_FRAME_ELEMS are part of the REGCP_OTHER_ELEMS and | |
182 | * are needed for the regexp context stack bookkeeping. */ | |
183 | SSPUSHINT(paren_elems_to_push + REGCP_OTHER_ELEMS - REGCP_FRAME_ELEMS); | |
b1ce53c5 | 184 | SSPUSHINT(SAVEt_REGCONTEXT); /* Magic cookie. */ |
41123dfd | 185 | |
a0d0e21e LW |
186 | return retval; |
187 | } | |
188 | ||
c277df42 | 189 | /* These are needed since we do not localize EVAL nodes: */ |
02db2b7b | 190 | # define REGCP_SET(cp) DEBUG_r(PerlIO_printf(Perl_debug_log, \ |
faccc32b | 191 | " Setting an EVAL scope, savestack=%"IVdf"\n", \ |
02db2b7b | 192 | (IV)PL_savestack_ix)); cp = PL_savestack_ix |
c3464db5 | 193 | |
02db2b7b | 194 | # define REGCP_UNWIND(cp) DEBUG_r(cp != PL_savestack_ix ? \ |
c3464db5 | 195 | PerlIO_printf(Perl_debug_log, \ |
faccc32b | 196 | " Clearing an EVAL scope, savestack=%"IVdf"..%"IVdf"\n", \ |
02db2b7b | 197 | (IV)(cp), (IV)PL_savestack_ix) : 0); regcpblow(cp) |
c277df42 | 198 | |
76e3520e | 199 | STATIC char * |
cea2e8a9 | 200 | S_regcppop(pTHX) |
a0d0e21e | 201 | { |
b1ce53c5 | 202 | I32 i; |
a0d0e21e LW |
203 | U32 paren = 0; |
204 | char *input; | |
cf93c79d | 205 | I32 tmps; |
b1ce53c5 JH |
206 | |
207 | /* Pop REGCP_OTHER_ELEMS before the parentheses loop starts. */ | |
a0d0e21e | 208 | i = SSPOPINT; |
b1ce53c5 JH |
209 | assert(i == SAVEt_REGCONTEXT); /* Check that the magic cookie is there. */ |
210 | i = SSPOPINT; /* Parentheses elements to pop. */ | |
a0d0e21e | 211 | input = (char *) SSPOPPTR; |
a01268b5 | 212 | *PL_reglastcloseparen = SSPOPINT; |
3280af22 NIS |
213 | *PL_reglastparen = SSPOPINT; |
214 | PL_regsize = SSPOPINT; | |
b1ce53c5 JH |
215 | |
216 | /* Now restore the parentheses context. */ | |
41123dfd JH |
217 | for (i -= (REGCP_OTHER_ELEMS - REGCP_FRAME_ELEMS); |
218 | i > 0; i -= REGCP_PAREN_ELEMS) { | |
a0d0e21e | 219 | paren = (U32)SSPOPINT; |
3280af22 | 220 | PL_reg_start_tmp[paren] = (char *) SSPOPPTR; |
cf93c79d IZ |
221 | PL_regstartp[paren] = SSPOPINT; |
222 | tmps = SSPOPINT; | |
3280af22 NIS |
223 | if (paren <= *PL_reglastparen) |
224 | PL_regendp[paren] = tmps; | |
c277df42 | 225 | DEBUG_r( |
c3464db5 | 226 | PerlIO_printf(Perl_debug_log, |
b900a521 | 227 | " restoring \\%"UVuf" to %"IVdf"(%"IVdf")..%"IVdf"%s\n", |
9041c2e3 | 228 | (UV)paren, (IV)PL_regstartp[paren], |
b900a521 | 229 | (IV)(PL_reg_start_tmp[paren] - PL_bostr), |
9041c2e3 | 230 | (IV)PL_regendp[paren], |
3280af22 | 231 | (paren > *PL_reglastparen ? "(no)" : "")); |
c277df42 | 232 | ); |
a0d0e21e | 233 | } |
c277df42 | 234 | DEBUG_r( |
3280af22 | 235 | if (*PL_reglastparen + 1 <= PL_regnpar) { |
c3464db5 | 236 | PerlIO_printf(Perl_debug_log, |
faccc32b JH |
237 | " restoring \\%"IVdf"..\\%"IVdf" to undef\n", |
238 | (IV)(*PL_reglastparen + 1), (IV)PL_regnpar); | |
c277df42 IZ |
239 | } |
240 | ); | |
daf18116 | 241 | #if 1 |
dafc8851 JH |
242 | /* It would seem that the similar code in regtry() |
243 | * already takes care of this, and in fact it is in | |
244 | * a better location to since this code can #if 0-ed out | |
245 | * but the code in regtry() is needed or otherwise tests | |
246 | * requiring null fields (pat.t#187 and split.t#{13,14} | |
daf18116 JH |
247 | * (as of patchlevel 7877) will fail. Then again, |
248 | * this code seems to be necessary or otherwise | |
249 | * building DynaLoader will fail: | |
250 | * "Error: '*' not in typemap in DynaLoader.xs, line 164" | |
251 | * --jhi */ | |
3280af22 NIS |
252 | for (paren = *PL_reglastparen + 1; paren <= PL_regnpar; paren++) { |
253 | if (paren > PL_regsize) | |
cf93c79d IZ |
254 | PL_regstartp[paren] = -1; |
255 | PL_regendp[paren] = -1; | |
a0d0e21e | 256 | } |
dafc8851 | 257 | #endif |
a0d0e21e LW |
258 | return input; |
259 | } | |
260 | ||
0f5d15d6 | 261 | STATIC char * |
cea2e8a9 | 262 | S_regcp_set_to(pTHX_ I32 ss) |
0f5d15d6 IZ |
263 | { |
264 | I32 tmp = PL_savestack_ix; | |
265 | ||
266 | PL_savestack_ix = ss; | |
267 | regcppop(); | |
268 | PL_savestack_ix = tmp; | |
942e002e | 269 | return Nullch; |
0f5d15d6 IZ |
270 | } |
271 | ||
272 | typedef struct re_cc_state | |
273 | { | |
274 | I32 ss; | |
275 | regnode *node; | |
276 | struct re_cc_state *prev; | |
277 | CURCUR *cc; | |
278 | regexp *re; | |
279 | } re_cc_state; | |
280 | ||
02db2b7b | 281 | #define regcpblow(cp) LEAVE_SCOPE(cp) /* Ignores regcppush()ed data. */ |
a0d0e21e | 282 | |
29d1e993 HS |
283 | #define TRYPAREN(paren, n, input) { \ |
284 | if (paren) { \ | |
285 | if (n) { \ | |
286 | PL_regstartp[paren] = HOPc(input, -1) - PL_bostr; \ | |
287 | PL_regendp[paren] = input - PL_bostr; \ | |
288 | } \ | |
289 | else \ | |
290 | PL_regendp[paren] = -1; \ | |
291 | } \ | |
292 | if (regmatch(next)) \ | |
293 | sayYES; \ | |
294 | if (paren && n) \ | |
295 | PL_regendp[paren] = -1; \ | |
296 | } | |
297 | ||
298 | ||
a687059c | 299 | /* |
e50aee73 | 300 | * pregexec and friends |
a687059c LW |
301 | */ |
302 | ||
303 | /* | |
c277df42 | 304 | - pregexec - match a regexp against a string |
a687059c | 305 | */ |
c277df42 | 306 | I32 |
864dbfa3 | 307 | Perl_pregexec(pTHX_ register regexp *prog, char *stringarg, register char *strend, |
c3464db5 | 308 | char *strbeg, I32 minend, SV *screamer, U32 nosave) |
c277df42 IZ |
309 | /* strend: pointer to null at end of string */ |
310 | /* strbeg: real beginning of string */ | |
311 | /* minend: end of match must be >=minend after stringarg. */ | |
312 | /* nosave: For optimizations. */ | |
313 | { | |
314 | return | |
9041c2e3 | 315 | regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL, |
c277df42 IZ |
316 | nosave ? 0 : REXEC_COPY_STR); |
317 | } | |
0f5d15d6 IZ |
318 | |
319 | STATIC void | |
cea2e8a9 | 320 | S_cache_re(pTHX_ regexp *prog) |
0f5d15d6 IZ |
321 | { |
322 | PL_regprecomp = prog->precomp; /* Needed for FAIL. */ | |
323 | #ifdef DEBUGGING | |
324 | PL_regprogram = prog->program; | |
325 | #endif | |
326 | PL_regnpar = prog->nparens; | |
9041c2e3 NIS |
327 | PL_regdata = prog->data; |
328 | PL_reg_re = prog; | |
0f5d15d6 | 329 | } |
22e551b9 | 330 | |
9041c2e3 | 331 | /* |
cad2e5aa JH |
332 | * Need to implement the following flags for reg_anch: |
333 | * | |
334 | * USE_INTUIT_NOML - Useful to call re_intuit_start() first | |
335 | * USE_INTUIT_ML | |
336 | * INTUIT_AUTORITATIVE_NOML - Can trust a positive answer | |
337 | * INTUIT_AUTORITATIVE_ML | |
338 | * INTUIT_ONCE_NOML - Intuit can match in one location only. | |
339 | * INTUIT_ONCE_ML | |
340 | * | |
341 | * Another flag for this function: SECOND_TIME (so that float substrs | |
342 | * with giant delta may be not rechecked). | |
343 | */ | |
344 | ||
345 | /* Assumptions: if ANCH_GPOS, then strpos is anchored. XXXX Check GPOS logic */ | |
346 | ||
2c2d71f5 | 347 | /* If SCREAM, then SvPVX(sv) should be compatible with strpos and strend. |
cad2e5aa JH |
348 | Otherwise, only SvCUR(sv) is used to get strbeg. */ |
349 | ||
350 | /* XXXX We assume that strpos is strbeg unless sv. */ | |
351 | ||
6eb5f6b9 JH |
352 | /* XXXX Some places assume that there is a fixed substring. |
353 | An update may be needed if optimizer marks as "INTUITable" | |
354 | RExen without fixed substrings. Similarly, it is assumed that | |
355 | lengths of all the strings are no more than minlen, thus they | |
356 | cannot come from lookahead. | |
357 | (Or minlen should take into account lookahead.) */ | |
358 | ||
2c2d71f5 JH |
359 | /* A failure to find a constant substring means that there is no need to make |
360 | an expensive call to REx engine, thus we celebrate a failure. Similarly, | |
361 | finding a substring too deep into the string means that less calls to | |
30944b6d IZ |
362 | regtry() should be needed. |
363 | ||
364 | REx compiler's optimizer found 4 possible hints: | |
365 | a) Anchored substring; | |
366 | b) Fixed substring; | |
367 | c) Whether we are anchored (beginning-of-line or \G); | |
368 | d) First node (of those at offset 0) which may distingush positions; | |
6eb5f6b9 | 369 | We use a)b)d) and multiline-part of c), and try to find a position in the |
30944b6d IZ |
370 | string which does not contradict any of them. |
371 | */ | |
2c2d71f5 | 372 | |
6eb5f6b9 JH |
373 | /* Most of decisions we do here should have been done at compile time. |
374 | The nodes of the REx which we used for the search should have been | |
375 | deleted from the finite automaton. */ | |
376 | ||
cad2e5aa JH |
377 | char * |
378 | Perl_re_intuit_start(pTHX_ regexp *prog, SV *sv, char *strpos, | |
379 | char *strend, U32 flags, re_scream_pos_data *data) | |
380 | { | |
b7953727 | 381 | register I32 start_shift = 0; |
cad2e5aa | 382 | /* Should be nonnegative! */ |
b7953727 | 383 | register I32 end_shift = 0; |
2c2d71f5 JH |
384 | register char *s; |
385 | register SV *check; | |
a1933d95 | 386 | char *strbeg; |
cad2e5aa JH |
387 | char *t; |
388 | I32 ml_anch; | |
6eb5f6b9 | 389 | register char *other_last = Nullch; /* other substr checked before this */ |
b7953727 | 390 | char *check_at = Nullch; /* check substr found at this pos */ |
30944b6d IZ |
391 | #ifdef DEBUGGING |
392 | char *i_strpos = strpos; | |
ce333219 | 393 | SV *dsv = PERL_DEBUG_PAD_ZERO(0); |
30944b6d | 394 | #endif |
cad2e5aa | 395 | |
b8d68ded JH |
396 | if (prog->reganch & ROPT_UTF8) { |
397 | DEBUG_r(PerlIO_printf(Perl_debug_log, | |
398 | "UTF-8 regex...\n")); | |
399 | PL_reg_flags |= RF_utf8; | |
400 | } | |
401 | ||
2a782b5b | 402 | DEBUG_r({ |
b8d68ded JH |
403 | char *s = PL_reg_match_utf8 ? |
404 | sv_uni_display(dsv, sv, 60, 0) : strpos; | |
405 | int len = PL_reg_match_utf8 ? | |
406 | strlen(s) : strend - strpos; | |
2a782b5b JH |
407 | if (!PL_colorset) |
408 | reginitcolors(); | |
b8d68ded JH |
409 | if (PL_reg_match_utf8) |
410 | DEBUG_r(PerlIO_printf(Perl_debug_log, | |
411 | "UTF-8 target...\n")); | |
2a782b5b JH |
412 | PerlIO_printf(Perl_debug_log, |
413 | "%sGuessing start of match, REx%s `%s%.60s%s%s' against `%s%.*s%s%s'...\n", | |
414 | PL_colors[4],PL_colors[5],PL_colors[0], | |
415 | prog->precomp, | |
416 | PL_colors[1], | |
417 | (strlen(prog->precomp) > 60 ? "..." : ""), | |
418 | PL_colors[0], | |
419 | (int)(len > 60 ? 60 : len), | |
420 | s, PL_colors[1], | |
421 | (len > 60 ? "..." : "") | |
422 | ); | |
423 | }); | |
cad2e5aa | 424 | |
1aa99e6b | 425 | if (prog->minlen > CHR_DIST((U8*)strend, (U8*)strpos)) { |
a72c7584 JH |
426 | DEBUG_r(PerlIO_printf(Perl_debug_log, |
427 | "String too short... [re_intuit_start]\n")); | |
cad2e5aa | 428 | goto fail; |
2c2d71f5 | 429 | } |
a1933d95 | 430 | strbeg = (sv && SvPOK(sv)) ? strend - SvCUR(sv) : strpos; |
1aa99e6b | 431 | PL_regeol = strend; |
653099ff | 432 | check = prog->check_substr; |
2c2d71f5 | 433 | if (prog->reganch & ROPT_ANCH) { /* Match at beg-of-str or after \n */ |
cad2e5aa JH |
434 | ml_anch = !( (prog->reganch & ROPT_ANCH_SINGLE) |
435 | || ( (prog->reganch & ROPT_ANCH_BOL) | |
2c2d71f5 | 436 | && !PL_multiline ) ); /* Check after \n? */ |
cad2e5aa | 437 | |
7e25d62c JH |
438 | if (!ml_anch) { |
439 | if ( !(prog->reganch & (ROPT_ANCH_GPOS /* Checked by the caller */ | |
440 | | ROPT_IMPLICIT)) /* not a real BOL */ | |
441 | /* SvCUR is not set on references: SvRV and SvPVX overlap */ | |
442 | && sv && !SvROK(sv) | |
443 | && (strpos != strbeg)) { | |
444 | DEBUG_r(PerlIO_printf(Perl_debug_log, "Not at start...\n")); | |
445 | goto fail; | |
446 | } | |
447 | if (prog->check_offset_min == prog->check_offset_max && | |
cce850e4 | 448 | !(prog->reganch & ROPT_CANY_SEEN)) { |
2c2d71f5 | 449 | /* Substring at constant offset from beg-of-str... */ |
cad2e5aa JH |
450 | I32 slen; |
451 | ||
1aa99e6b | 452 | s = HOP3c(strpos, prog->check_offset_min, strend); |
653099ff GS |
453 | if (SvTAIL(check)) { |
454 | slen = SvCUR(check); /* >= 1 */ | |
cad2e5aa | 455 | |
9041c2e3 | 456 | if ( strend - s > slen || strend - s < slen - 1 |
2c2d71f5 JH |
457 | || (strend - s == slen && strend[-1] != '\n')) { |
458 | DEBUG_r(PerlIO_printf(Perl_debug_log, "String too long...\n")); | |
459 | goto fail_finish; | |
cad2e5aa JH |
460 | } |
461 | /* Now should match s[0..slen-2] */ | |
462 | slen--; | |
653099ff | 463 | if (slen && (*SvPVX(check) != *s |
cad2e5aa | 464 | || (slen > 1 |
653099ff | 465 | && memNE(SvPVX(check), s, slen)))) { |
2c2d71f5 JH |
466 | report_neq: |
467 | DEBUG_r(PerlIO_printf(Perl_debug_log, "String not equal...\n")); | |
468 | goto fail_finish; | |
469 | } | |
cad2e5aa | 470 | } |
653099ff GS |
471 | else if (*SvPVX(check) != *s |
472 | || ((slen = SvCUR(check)) > 1 | |
473 | && memNE(SvPVX(check), s, slen))) | |
2c2d71f5 JH |
474 | goto report_neq; |
475 | goto success_at_start; | |
7e25d62c | 476 | } |
cad2e5aa | 477 | } |
2c2d71f5 | 478 | /* Match is anchored, but substr is not anchored wrt beg-of-str. */ |
cad2e5aa | 479 | s = strpos; |
2c2d71f5 | 480 | start_shift = prog->check_offset_min; /* okay to underestimate on CC */ |
2c2d71f5 | 481 | end_shift = prog->minlen - start_shift - |
653099ff | 482 | CHR_SVLEN(check) + (SvTAIL(check) != 0); |
2c2d71f5 | 483 | if (!ml_anch) { |
653099ff GS |
484 | I32 end = prog->check_offset_max + CHR_SVLEN(check) |
485 | - (SvTAIL(check) != 0); | |
1aa99e6b | 486 | I32 eshift = CHR_DIST((U8*)strend, (U8*)s) - end; |
2c2d71f5 JH |
487 | |
488 | if (end_shift < eshift) | |
489 | end_shift = eshift; | |
490 | } | |
cad2e5aa | 491 | } |
2c2d71f5 | 492 | else { /* Can match at random position */ |
cad2e5aa JH |
493 | ml_anch = 0; |
494 | s = strpos; | |
2c2d71f5 JH |
495 | start_shift = prog->check_offset_min; /* okay to underestimate on CC */ |
496 | /* Should be nonnegative! */ | |
497 | end_shift = prog->minlen - start_shift - | |
653099ff | 498 | CHR_SVLEN(check) + (SvTAIL(check) != 0); |
cad2e5aa JH |
499 | } |
500 | ||
2c2d71f5 | 501 | #ifdef DEBUGGING /* 7/99: reports of failure (with the older version) */ |
0033605d | 502 | if (end_shift < 0) |
6bbae5e6 | 503 | Perl_croak(aTHX_ "panic: end_shift"); |
2c2d71f5 JH |
504 | #endif |
505 | ||
2c2d71f5 JH |
506 | restart: |
507 | /* Find a possible match in the region s..strend by looking for | |
508 | the "check" substring in the region corrected by start/end_shift. */ | |
cad2e5aa | 509 | if (flags & REXEC_SCREAM) { |
cad2e5aa JH |
510 | I32 p = -1; /* Internal iterator of scream. */ |
511 | I32 *pp = data ? data->scream_pos : &p; | |
512 | ||
2c2d71f5 JH |
513 | if (PL_screamfirst[BmRARE(check)] >= 0 |
514 | || ( BmRARE(check) == '\n' | |
515 | && (BmPREVIOUS(check) == SvCUR(check) - 1) | |
516 | && SvTAIL(check) )) | |
9041c2e3 | 517 | s = screaminstr(sv, check, |
2c2d71f5 | 518 | start_shift + (s - strbeg), end_shift, pp, 0); |
cad2e5aa | 519 | else |
2c2d71f5 | 520 | goto fail_finish; |
cad2e5aa JH |
521 | if (data) |
522 | *data->scream_olds = s; | |
523 | } | |
f33976b4 | 524 | else if (prog->reganch & ROPT_CANY_SEEN) |
3baa4c62 JH |
525 | s = fbm_instr((U8*)(s + start_shift), |
526 | (U8*)(strend - end_shift), | |
527 | check, PL_multiline ? FBMrf_MULTILINE : 0); | |
cad2e5aa | 528 | else |
1aa99e6b IH |
529 | s = fbm_instr(HOP3(s, start_shift, strend), |
530 | HOP3(strend, -end_shift, strbeg), | |
2c2d71f5 | 531 | check, PL_multiline ? FBMrf_MULTILINE : 0); |
cad2e5aa JH |
532 | |
533 | /* Update the count-of-usability, remove useless subpatterns, | |
534 | unshift s. */ | |
2c2d71f5 JH |
535 | |
536 | DEBUG_r(PerlIO_printf(Perl_debug_log, "%s %s substr `%s%.*s%s'%s%s", | |
537 | (s ? "Found" : "Did not find"), | |
538 | ((check == prog->anchored_substr) ? "anchored" : "floating"), | |
539 | PL_colors[0], | |
7b0972df JH |
540 | (int)(SvCUR(check) - (SvTAIL(check)!=0)), |
541 | SvPVX(check), | |
2c2d71f5 JH |
542 | PL_colors[1], (SvTAIL(check) ? "$" : ""), |
543 | (s ? " at offset " : "...\n") ) ); | |
544 | ||
545 | if (!s) | |
546 | goto fail_finish; | |
547 | ||
6eb5f6b9 JH |
548 | check_at = s; |
549 | ||
2c2d71f5 | 550 | /* Finish the diagnostic message */ |
30944b6d | 551 | DEBUG_r(PerlIO_printf(Perl_debug_log, "%ld...\n", (long)(s - i_strpos)) ); |
2c2d71f5 JH |
552 | |
553 | /* Got a candidate. Check MBOL anchoring, and the *other* substr. | |
554 | Start with the other substr. | |
555 | XXXX no SCREAM optimization yet - and a very coarse implementation | |
556 | XXXX /ttx+/ results in anchored=`ttx', floating=`x'. floating will | |
557 | *always* match. Probably should be marked during compile... | |
558 | Probably it is right to do no SCREAM here... | |
559 | */ | |
560 | ||
561 | if (prog->float_substr && prog->anchored_substr) { | |
30944b6d | 562 | /* Take into account the "other" substring. */ |
2c2d71f5 JH |
563 | /* XXXX May be hopelessly wrong for UTF... */ |
564 | if (!other_last) | |
6eb5f6b9 | 565 | other_last = strpos; |
2c2d71f5 | 566 | if (check == prog->float_substr) { |
30944b6d IZ |
567 | do_other_anchored: |
568 | { | |
1aa99e6b | 569 | char *last = HOP3c(s, -start_shift, strbeg), *last1, *last2; |
2c2d71f5 JH |
570 | char *s1 = s; |
571 | ||
2c2d71f5 JH |
572 | t = s - prog->check_offset_max; |
573 | if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */ | |
574 | && (!(prog->reganch & ROPT_UTF8) | |
1aa99e6b | 575 | || ((t = reghopmaybe3_c(s, -(prog->check_offset_max), strpos)) |
2c2d71f5 | 576 | && t > strpos))) |
30944b6d | 577 | /* EMPTY */; |
2c2d71f5 JH |
578 | else |
579 | t = strpos; | |
1aa99e6b | 580 | t = HOP3c(t, prog->anchored_offset, strend); |
6eb5f6b9 JH |
581 | if (t < other_last) /* These positions already checked */ |
582 | t = other_last; | |
1aa99e6b | 583 | last2 = last1 = HOP3c(strend, -prog->minlen, strbeg); |
2c2d71f5 JH |
584 | if (last < last1) |
585 | last1 = last; | |
586 | /* XXXX It is not documented what units *_offsets are in. Assume bytes. */ | |
587 | /* On end-of-str: see comment below. */ | |
588 | s = fbm_instr((unsigned char*)t, | |
1aa99e6b IH |
589 | HOP3(HOP3(last1, prog->anchored_offset, strend) |
590 | + SvCUR(prog->anchored_substr), | |
591 | -(SvTAIL(prog->anchored_substr)!=0), strbeg), | |
592 | prog->anchored_substr, | |
593 | PL_multiline ? FBMrf_MULTILINE : 0); | |
594 | DEBUG_r(PerlIO_printf(Perl_debug_log, | |
595 | "%s anchored substr `%s%.*s%s'%s", | |
2c2d71f5 JH |
596 | (s ? "Found" : "Contradicts"), |
597 | PL_colors[0], | |
7b0972df JH |
598 | (int)(SvCUR(prog->anchored_substr) |
599 | - (SvTAIL(prog->anchored_substr)!=0)), | |
2c2d71f5 JH |
600 | SvPVX(prog->anchored_substr), |
601 | PL_colors[1], (SvTAIL(prog->anchored_substr) ? "$" : ""))); | |
602 | if (!s) { | |
603 | if (last1 >= last2) { | |
604 | DEBUG_r(PerlIO_printf(Perl_debug_log, | |
605 | ", giving up...\n")); | |
606 | goto fail_finish; | |
607 | } | |
608 | DEBUG_r(PerlIO_printf(Perl_debug_log, | |
609 | ", trying floating at offset %ld...\n", | |
1aa99e6b IH |
610 | (long)(HOP3c(s1, 1, strend) - i_strpos))); |
611 | other_last = HOP3c(last1, prog->anchored_offset+1, strend); | |
612 | s = HOP3c(last, 1, strend); | |
2c2d71f5 JH |
613 | goto restart; |
614 | } | |
615 | else { | |
616 | DEBUG_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n", | |
30944b6d | 617 | (long)(s - i_strpos))); |
1aa99e6b IH |
618 | t = HOP3c(s, -prog->anchored_offset, strbeg); |
619 | other_last = HOP3c(s, 1, strend); | |
30944b6d | 620 | s = s1; |
2c2d71f5 JH |
621 | if (t == strpos) |
622 | goto try_at_start; | |
2c2d71f5 JH |
623 | goto try_at_offset; |
624 | } | |
30944b6d | 625 | } |
2c2d71f5 JH |
626 | } |
627 | else { /* Take into account the floating substring. */ | |
628 | char *last, *last1; | |
629 | char *s1 = s; | |
630 | ||
1aa99e6b IH |
631 | t = HOP3c(s, -start_shift, strbeg); |
632 | last1 = last = | |
633 | HOP3c(strend, -prog->minlen + prog->float_min_offset, strbeg); | |
634 | if (CHR_DIST((U8*)last, (U8*)t) > prog->float_max_offset) | |
635 | last = HOP3c(t, prog->float_max_offset, strend); | |
636 | s = HOP3c(t, prog->float_min_offset, strend); | |
6eb5f6b9 JH |
637 | if (s < other_last) |
638 | s = other_last; | |
2c2d71f5 JH |
639 | /* XXXX It is not documented what units *_offsets are in. Assume bytes. */ |
640 | /* fbm_instr() takes into account exact value of end-of-str | |
641 | if the check is SvTAIL(ed). Since false positives are OK, | |
642 | and end-of-str is not later than strend we are OK. */ | |
643 | s = fbm_instr((unsigned char*)s, | |
644 | (unsigned char*)last + SvCUR(prog->float_substr) | |
645 | - (SvTAIL(prog->float_substr)!=0), | |
646 | prog->float_substr, PL_multiline ? FBMrf_MULTILINE : 0); | |
647 | DEBUG_r(PerlIO_printf(Perl_debug_log, "%s floating substr `%s%.*s%s'%s", | |
648 | (s ? "Found" : "Contradicts"), | |
649 | PL_colors[0], | |
7b0972df JH |
650 | (int)(SvCUR(prog->float_substr) |
651 | - (SvTAIL(prog->float_substr)!=0)), | |
2c2d71f5 JH |
652 | SvPVX(prog->float_substr), |
653 | PL_colors[1], (SvTAIL(prog->float_substr) ? "$" : ""))); | |
654 | if (!s) { | |
655 | if (last1 == last) { | |
656 | DEBUG_r(PerlIO_printf(Perl_debug_log, | |
657 | ", giving up...\n")); | |
658 | goto fail_finish; | |
659 | } | |
660 | DEBUG_r(PerlIO_printf(Perl_debug_log, | |
661 | ", trying anchored starting at offset %ld...\n", | |
30944b6d | 662 | (long)(s1 + 1 - i_strpos))); |
803ff556 | 663 | other_last = last; |
1aa99e6b | 664 | s = HOP3c(t, 1, strend); |
2c2d71f5 JH |
665 | goto restart; |
666 | } | |
667 | else { | |
668 | DEBUG_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n", | |
30944b6d | 669 | (long)(s - i_strpos))); |
803ff556 | 670 | other_last = s; /* Fix this later. --Hugo */ |
30944b6d | 671 | s = s1; |
2c2d71f5 JH |
672 | if (t == strpos) |
673 | goto try_at_start; | |
2c2d71f5 JH |
674 | goto try_at_offset; |
675 | } | |
676 | } | |
cad2e5aa | 677 | } |
2c2d71f5 JH |
678 | |
679 | t = s - prog->check_offset_max; | |
2c2d71f5 JH |
680 | if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */ |
681 | && (!(prog->reganch & ROPT_UTF8) | |
1aa99e6b IH |
682 | || ((t = reghopmaybe3_c(s, -prog->check_offset_max, strpos)) |
683 | && t > strpos))) { | |
2c2d71f5 JH |
684 | /* Fixed substring is found far enough so that the match |
685 | cannot start at strpos. */ | |
686 | try_at_offset: | |
cad2e5aa | 687 | if (ml_anch && t[-1] != '\n') { |
30944b6d IZ |
688 | /* Eventually fbm_*() should handle this, but often |
689 | anchored_offset is not 0, so this check will not be wasted. */ | |
690 | /* XXXX In the code below we prefer to look for "^" even in | |
691 | presence of anchored substrings. And we search even | |
692 | beyond the found float position. These pessimizations | |
693 | are historical artefacts only. */ | |
694 | find_anchor: | |
2c2d71f5 | 695 | while (t < strend - prog->minlen) { |
cad2e5aa | 696 | if (*t == '\n') { |
4ee3650e | 697 | if (t < check_at - prog->check_offset_min) { |
30944b6d | 698 | if (prog->anchored_substr) { |
4ee3650e GS |
699 | /* Since we moved from the found position, |
700 | we definitely contradict the found anchored | |
30944b6d IZ |
701 | substr. Due to the above check we do not |
702 | contradict "check" substr. | |
703 | Thus we can arrive here only if check substr | |
704 | is float. Redo checking for "other"=="fixed". | |
705 | */ | |
9041c2e3 | 706 | strpos = t + 1; |
30944b6d IZ |
707 | DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld, rescanning for anchored from offset %ld...\n", |
708 | PL_colors[0],PL_colors[1], (long)(strpos - i_strpos), (long)(strpos - i_strpos + prog->anchored_offset))); | |
709 | goto do_other_anchored; | |
710 | } | |
4ee3650e GS |
711 | /* We don't contradict the found floating substring. */ |
712 | /* XXXX Why not check for STCLASS? */ | |
cad2e5aa | 713 | s = t + 1; |
2c2d71f5 | 714 | DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld...\n", |
30944b6d | 715 | PL_colors[0],PL_colors[1], (long)(s - i_strpos))); |
cad2e5aa JH |
716 | goto set_useful; |
717 | } | |
4ee3650e GS |
718 | /* Position contradicts check-string */ |
719 | /* XXXX probably better to look for check-string | |
720 | than for "\n", so one should lower the limit for t? */ | |
721 | DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m, restarting lookup for check-string at offset %ld...\n", | |
30944b6d | 722 | PL_colors[0],PL_colors[1], (long)(t + 1 - i_strpos))); |
0e41cd87 | 723 | other_last = strpos = s = t + 1; |
cad2e5aa JH |
724 | goto restart; |
725 | } | |
726 | t++; | |
727 | } | |
2c2d71f5 JH |
728 | DEBUG_r(PerlIO_printf(Perl_debug_log, "Did not find /%s^%s/m...\n", |
729 | PL_colors[0],PL_colors[1])); | |
730 | goto fail_finish; | |
cad2e5aa | 731 | } |
f5952150 GS |
732 | else { |
733 | DEBUG_r(PerlIO_printf(Perl_debug_log, "Starting position does not contradict /%s^%s/m...\n", | |
734 | PL_colors[0],PL_colors[1])); | |
735 | } | |
cad2e5aa JH |
736 | s = t; |
737 | set_useful: | |
2c2d71f5 | 738 | ++BmUSEFUL(prog->check_substr); /* hooray/5 */ |
cad2e5aa JH |
739 | } |
740 | else { | |
f5952150 | 741 | /* The found string does not prohibit matching at strpos, |
2c2d71f5 | 742 | - no optimization of calling REx engine can be performed, |
f5952150 GS |
743 | unless it was an MBOL and we are not after MBOL, |
744 | or a future STCLASS check will fail this. */ | |
2c2d71f5 JH |
745 | try_at_start: |
746 | /* Even in this situation we may use MBOL flag if strpos is offset | |
747 | wrt the start of the string. */ | |
05b4157f | 748 | if (ml_anch && sv && !SvROK(sv) /* See prev comment on SvROK */ |
a1933d95 | 749 | && (strpos != strbeg) && strpos[-1] != '\n' |
d506a20d IZ |
750 | /* May be due to an implicit anchor of m{.*foo} */ |
751 | && !(prog->reganch & ROPT_IMPLICIT)) | |
752 | { | |
cad2e5aa JH |
753 | t = strpos; |
754 | goto find_anchor; | |
755 | } | |
30944b6d | 756 | DEBUG_r( if (ml_anch) |
f5952150 GS |
757 | PerlIO_printf(Perl_debug_log, "Position at offset %ld does not contradict /%s^%s/m...\n", |
758 | (long)(strpos - i_strpos), PL_colors[0],PL_colors[1]); | |
30944b6d | 759 | ); |
2c2d71f5 | 760 | success_at_start: |
30944b6d | 761 | if (!(prog->reganch & ROPT_NAUGHTY) /* XXXX If strpos moved? */ |
66e933ab | 762 | && prog->check_substr /* Could be deleted already */ |
cad2e5aa | 763 | && --BmUSEFUL(prog->check_substr) < 0 |
66e933ab GS |
764 | && prog->check_substr == prog->float_substr) |
765 | { | |
cad2e5aa | 766 | /* If flags & SOMETHING - do not do it many times on the same match */ |
f5952150 | 767 | DEBUG_r(PerlIO_printf(Perl_debug_log, "... Disabling check substring...\n")); |
cad2e5aa JH |
768 | SvREFCNT_dec(prog->check_substr); |
769 | prog->check_substr = Nullsv; /* disable */ | |
770 | prog->float_substr = Nullsv; /* clear */ | |
5e39e1e5 | 771 | check = Nullsv; /* abort */ |
cad2e5aa | 772 | s = strpos; |
3cf5c195 IZ |
773 | /* XXXX This is a remnant of the old implementation. It |
774 | looks wasteful, since now INTUIT can use many | |
6eb5f6b9 | 775 | other heuristics. */ |
cad2e5aa JH |
776 | prog->reganch &= ~RE_USE_INTUIT; |
777 | } | |
778 | else | |
779 | s = strpos; | |
780 | } | |
781 | ||
6eb5f6b9 JH |
782 | /* Last resort... */ |
783 | /* XXXX BmUSEFUL already changed, maybe multiple change is meaningful... */ | |
784 | if (prog->regstclass) { | |
785 | /* minlen == 0 is possible if regstclass is \b or \B, | |
786 | and the fixed substr is ''$. | |
787 | Since minlen is already taken into account, s+1 is before strend; | |
788 | accidentally, minlen >= 1 guaranties no false positives at s + 1 | |
789 | even for \b or \B. But (minlen? 1 : 0) below assumes that | |
790 | regstclass does not come from lookahead... */ | |
791 | /* If regstclass takes bytelength more than 1: If charlength==1, OK. | |
792 | This leaves EXACTF only, which is dealt with in find_byclass(). */ | |
1aa99e6b | 793 | U8* str = (U8*)STRING(prog->regstclass); |
66e933ab | 794 | int cl_l = (PL_regkind[(U8)OP(prog->regstclass)] == EXACT |
1aa99e6b | 795 | ? CHR_DIST(str+STR_LEN(prog->regstclass), str) |
66e933ab | 796 | : 1); |
6eb5f6b9 | 797 | char *endpos = (prog->anchored_substr || ml_anch) |
1aa99e6b IH |
798 | ? HOP3c(s, (prog->minlen ? cl_l : 0), strend) |
799 | : (prog->float_substr | |
800 | ? HOP3c(HOP3c(check_at, -start_shift, strbeg), | |
801 | cl_l, strend) | |
802 | : strend); | |
a1933d95 | 803 | char *startpos = strbeg; |
6eb5f6b9 JH |
804 | |
805 | t = s; | |
76384e4a | 806 | if (prog->reganch & ROPT_UTF8) { |
ffc61ed2 | 807 | PL_regdata = prog->data; |
76384e4a GS |
808 | PL_bostr = startpos; |
809 | } | |
f33976b4 | 810 | s = find_byclass(prog, prog->regstclass, s, endpos, startpos, 1); |
6eb5f6b9 JH |
811 | if (!s) { |
812 | #ifdef DEBUGGING | |
b7953727 | 813 | char *what = 0; |
6eb5f6b9 JH |
814 | #endif |
815 | if (endpos == strend) { | |
816 | DEBUG_r( PerlIO_printf(Perl_debug_log, | |
817 | "Could not match STCLASS...\n") ); | |
818 | goto fail; | |
819 | } | |
66e933ab GS |
820 | DEBUG_r( PerlIO_printf(Perl_debug_log, |
821 | "This position contradicts STCLASS...\n") ); | |
653099ff GS |
822 | if ((prog->reganch & ROPT_ANCH) && !ml_anch) |
823 | goto fail; | |
6eb5f6b9 JH |
824 | /* Contradict one of substrings */ |
825 | if (prog->anchored_substr) { | |
6eb5f6b9 JH |
826 | if (prog->anchored_substr == check) { |
827 | DEBUG_r( what = "anchored" ); | |
828 | hop_and_restart: | |
1aa99e6b | 829 | s = HOP3c(t, 1, strend); |
66e933ab GS |
830 | if (s + start_shift + end_shift > strend) { |
831 | /* XXXX Should be taken into account earlier? */ | |
832 | DEBUG_r( PerlIO_printf(Perl_debug_log, | |
833 | "Could not match STCLASS...\n") ); | |
834 | goto fail; | |
835 | } | |
5e39e1e5 HS |
836 | if (!check) |
837 | goto giveup; | |
6eb5f6b9 | 838 | DEBUG_r( PerlIO_printf(Perl_debug_log, |
f5952150 | 839 | "Looking for %s substr starting at offset %ld...\n", |
6eb5f6b9 JH |
840 | what, (long)(s + start_shift - i_strpos)) ); |
841 | goto restart; | |
842 | } | |
66e933ab | 843 | /* Have both, check_string is floating */ |
6eb5f6b9 JH |
844 | if (t + start_shift >= check_at) /* Contradicts floating=check */ |
845 | goto retry_floating_check; | |
846 | /* Recheck anchored substring, but not floating... */ | |
9041c2e3 | 847 | s = check_at; |
5e39e1e5 HS |
848 | if (!check) |
849 | goto giveup; | |
6eb5f6b9 | 850 | DEBUG_r( PerlIO_printf(Perl_debug_log, |
f5952150 | 851 | "Looking for anchored substr starting at offset %ld...\n", |
6eb5f6b9 JH |
852 | (long)(other_last - i_strpos)) ); |
853 | goto do_other_anchored; | |
854 | } | |
60e71179 GS |
855 | /* Another way we could have checked stclass at the |
856 | current position only: */ | |
857 | if (ml_anch) { | |
858 | s = t = t + 1; | |
5e39e1e5 HS |
859 | if (!check) |
860 | goto giveup; | |
60e71179 | 861 | DEBUG_r( PerlIO_printf(Perl_debug_log, |
f5952150 GS |
862 | "Looking for /%s^%s/m starting at offset %ld...\n", |
863 | PL_colors[0],PL_colors[1], (long)(t - i_strpos)) ); | |
60e71179 | 864 | goto try_at_offset; |
66e933ab | 865 | } |
60e71179 GS |
866 | if (!prog->float_substr) /* Could have been deleted */ |
867 | goto fail; | |
6eb5f6b9 JH |
868 | /* Check is floating subtring. */ |
869 | retry_floating_check: | |
870 | t = check_at - start_shift; | |
871 | DEBUG_r( what = "floating" ); | |
872 | goto hop_and_restart; | |
873 | } | |
b7953727 JH |
874 | if (t != s) { |
875 | DEBUG_r(PerlIO_printf(Perl_debug_log, | |
6eb5f6b9 | 876 | "By STCLASS: moving %ld --> %ld\n", |
b7953727 JH |
877 | (long)(t - i_strpos), (long)(s - i_strpos)) |
878 | ); | |
879 | } | |
880 | else { | |
881 | DEBUG_r(PerlIO_printf(Perl_debug_log, | |
882 | "Does not contradict STCLASS...\n"); | |
883 | ); | |
884 | } | |
6eb5f6b9 | 885 | } |
5e39e1e5 HS |
886 | giveup: |
887 | DEBUG_r(PerlIO_printf(Perl_debug_log, "%s%s:%s match at offset %ld\n", | |
888 | PL_colors[4], (check ? "Guessed" : "Giving up"), | |
889 | PL_colors[5], (long)(s - i_strpos)) ); | |
cad2e5aa | 890 | return s; |
2c2d71f5 JH |
891 | |
892 | fail_finish: /* Substring not found */ | |
66e933ab GS |
893 | if (prog->check_substr) /* could be removed already */ |
894 | BmUSEFUL(prog->check_substr) += 5; /* hooray */ | |
cad2e5aa | 895 | fail: |
2c2d71f5 | 896 | DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch rejected by optimizer%s\n", |
cad2e5aa JH |
897 | PL_colors[4],PL_colors[5])); |
898 | return Nullch; | |
899 | } | |
9661b544 | 900 | |
6eb5f6b9 | 901 | /* We know what class REx starts with. Try to find this position... */ |
3c3eec57 GS |
902 | STATIC char * |
903 | S_find_byclass(pTHX_ regexp * prog, regnode *c, char *s, char *strend, char *startpos, I32 norun) | |
a687059c | 904 | { |
6eb5f6b9 JH |
905 | I32 doevery = (prog->reganch & ROPT_SKIP) == 0; |
906 | char *m; | |
d8093b23 G |
907 | STRLEN ln; |
908 | unsigned int c1; | |
909 | unsigned int c2; | |
6eb5f6b9 JH |
910 | char *e; |
911 | register I32 tmp = 1; /* Scratch variable? */ | |
53c4c00c | 912 | register bool do_utf8 = PL_reg_match_utf8; |
cad2e5aa | 913 | |
6eb5f6b9 JH |
914 | /* We know what class it must start with. */ |
915 | switch (OP(c)) { | |
6eb5f6b9 | 916 | case ANYOF: |
a0ed51b3 | 917 | while (s < strend) { |
ffc61ed2 | 918 | if (reginclass(c, (U8*)s, do_utf8)) { |
6eb5f6b9 JH |
919 | if (tmp && (norun || regtry(prog, s))) |
920 | goto got_it; | |
921 | else | |
922 | tmp = doevery; | |
a0ed51b3 | 923 | } |
6eb5f6b9 JH |
924 | else |
925 | tmp = 1; | |
ffc61ed2 | 926 | s += do_utf8 ? UTF8SKIP(s) : 1; |
a0d0e21e | 927 | } |
6eb5f6b9 | 928 | break; |
f33976b4 DB |
929 | case CANY: |
930 | while (s < strend) { | |
931 | if (tmp && (norun || regtry(prog, s))) | |
932 | goto got_it; | |
933 | else | |
934 | tmp = doevery; | |
935 | s++; | |
936 | } | |
937 | break; | |
6eb5f6b9 JH |
938 | case EXACTF: |
939 | m = STRING(c); | |
940 | ln = STR_LEN(c); | |
1aa99e6b | 941 | if (UTF) { |
a2a2844f | 942 | STRLEN ulen1, ulen2; |
e7ae6809 JH |
943 | U8 tmpbuf1[UTF8_MAXLEN_UCLC+1]; |
944 | U8 tmpbuf2[UTF8_MAXLEN_UCLC+1]; | |
a2a2844f JH |
945 | |
946 | to_utf8_lower((U8*)m, tmpbuf1, &ulen1); | |
947 | to_utf8_upper((U8*)m, tmpbuf2, &ulen2); | |
948 | ||
949 | c1 = utf8_to_uvuni(tmpbuf1, 0); | |
950 | c2 = utf8_to_uvuni(tmpbuf2, 0); | |
1aa99e6b IH |
951 | } |
952 | else { | |
953 | c1 = *(U8*)m; | |
954 | c2 = PL_fold[c1]; | |
955 | } | |
6eb5f6b9 JH |
956 | goto do_exactf; |
957 | case EXACTFL: | |
958 | m = STRING(c); | |
959 | ln = STR_LEN(c); | |
d8093b23 | 960 | c1 = *(U8*)m; |
6eb5f6b9 JH |
961 | c2 = PL_fold_locale[c1]; |
962 | do_exactf: | |
c7254714 | 963 | e = do_utf8 ? s + ln - 1 : strend - ln; |
b3c9acc1 | 964 | |
6eb5f6b9 JH |
965 | if (norun && e < s) |
966 | e = s; /* Due to minlen logic of intuit() */ | |
1aa99e6b | 967 | |
60a8b682 JH |
968 | /* The idea in the EXACTF* cases is to first find the |
969 | * first character of the EXACTF* node and then, if | |
970 | * necessary, case-insensitively compare the full | |
971 | * text of the node. The c1 and c2 are the first | |
972 | * characters (though in Unicode it gets a bit | |
973 | * more complicated because there are more cases | |
974 | * than just upper and lower: one is really supposed | |
975 | * to use the so-called folding case for case-insensitive | |
976 | * matching (called "loose matching" in Unicode). */ | |
977 | ||
1aa99e6b | 978 | if (do_utf8) { |
575cac57 JH |
979 | UV c, f; |
980 | U8 tmpbuf [UTF8_MAXLEN+1]; | |
981 | U8 foldbuf[UTF8_MAXLEN_FOLD+1]; | |
982 | STRLEN len, foldlen; | |
983 | ||
cadb39a9 JH |
984 | /* The ibcmp_utf8() uses to_uni_fold() which is more |
985 | * correct folding for Unicode than using lowercase. | |
986 | * However, it doesn't work quite fully since the folding | |
987 | * is a one-to-many mapping and the regex optimizer is | |
988 | * unaware of this, so it may throw out good matches. | |
989 | * Fortunately, not getting this right is allowed | |
990 | * for Unicode Regular Expression Support level 1, | |
991 | * only one-to-one matching is required. --jhi */ | |
80aecb99 | 992 | |
09091399 | 993 | if (c1 == c2) { |
1aa99e6b | 994 | while (s <= e) { |
575cac57 | 995 | c = utf8_to_uvchr((U8*)s, &len); |
80aecb99 JH |
996 | if ( c == c1 |
997 | && (ln == len || | |
ffce6cc2 JH |
998 | !ibcmp_utf8(s, do_utf8, |
999 | strend - s > ln ? ln : strend - s, | |
80aecb99 | 1000 | m, UTF, ln)) |
55da9344 | 1001 | && (norun || regtry(prog, s)) ) |
1aa99e6b | 1002 | goto got_it; |
80aecb99 JH |
1003 | else { |
1004 | uvchr_to_utf8(tmpbuf, c); | |
ac7e0132 | 1005 | f = to_utf8_fold(tmpbuf, foldbuf, &foldlen); |
80aecb99 JH |
1006 | if ( f != c |
1007 | && (f == c1 || f == c2) | |
1008 | && (ln == foldlen || | |
1009 | !ibcmp_utf8((char *)foldbuf, | |
ffce6cc2 JH |
1010 | do_utf8, |
1011 | foldlen > ln ? ln : foldlen, | |
80aecb99 JH |
1012 | m, UTF, ln)) |
1013 | && (norun || regtry(prog, s)) ) | |
1014 | goto got_it; | |
1015 | } | |
1aa99e6b IH |
1016 | s += len; |
1017 | } | |
09091399 JH |
1018 | } |
1019 | else { | |
1aa99e6b | 1020 | while (s <= e) { |
575cac57 | 1021 | c = utf8_to_uvchr((U8*)s, &len); |
80aecb99 | 1022 | |
60a8b682 JH |
1023 | /* Handle some of the three Greek sigmas cases. |
1024 | * Note that not all the possible combinations | |
1025 | * are handled here: some of them are handled | |
1026 | * handled by the standard folding rules, and | |
1027 | * some of them (the character class or ANYOF | |
1028 | * cases) are handled during compiletime in | |
1029 | * regexec.c:S_regclass(). */ | |
880bd946 JH |
1030 | if (c == (UV)UNICODE_GREEK_CAPITAL_LETTER_SIGMA || |
1031 | c == (UV)UNICODE_GREEK_SMALL_LETTER_FINAL_SIGMA) | |
1032 | c = (UV)UNICODE_GREEK_SMALL_LETTER_SIGMA; | |
80aecb99 JH |
1033 | |
1034 | if ( (c == c1 || c == c2) | |
1035 | && (ln == len || | |
ffce6cc2 JH |
1036 | !ibcmp_utf8(s, do_utf8, |
1037 | strend - s > ln ? ln : strend - s, | |
80aecb99 | 1038 | m, UTF, ln)) |
55da9344 | 1039 | && (norun || regtry(prog, s)) ) |
1aa99e6b | 1040 | goto got_it; |
80aecb99 JH |
1041 | else { |
1042 | uvchr_to_utf8(tmpbuf, c); | |
ac7e0132 | 1043 | f = to_utf8_fold(tmpbuf, foldbuf, &foldlen); |
80aecb99 JH |
1044 | if ( f != c |
1045 | && (f == c1 || f == c2) | |
1046 | && (ln == foldlen || | |
1047 | !ibcmp_utf8((char *)foldbuf, | |
ffce6cc2 JH |
1048 | do_utf8, |
1049 | foldlen > ln ? ln : foldlen, | |
80aecb99 JH |
1050 | m, UTF, ln)) |
1051 | && (norun || regtry(prog, s)) ) | |
1052 | goto got_it; | |
1053 | } | |
1aa99e6b IH |
1054 | s += len; |
1055 | } | |
09091399 | 1056 | } |
1aa99e6b IH |
1057 | } |
1058 | else { | |
1059 | if (c1 == c2) | |
1060 | while (s <= e) { | |
1061 | if ( *(U8*)s == c1 | |
1062 | && (ln == 1 || !(OP(c) == EXACTF | |
1063 | ? ibcmp(s, m, ln) | |
1064 | : ibcmp_locale(s, m, ln))) | |
1065 | && (norun || regtry(prog, s)) ) | |
1066 | goto got_it; | |
1067 | s++; | |
1068 | } | |
1069 | else | |
1070 | while (s <= e) { | |
1071 | if ( (*(U8*)s == c1 || *(U8*)s == c2) | |
1072 | && (ln == 1 || !(OP(c) == EXACTF | |
1073 | ? ibcmp(s, m, ln) | |
1074 | : ibcmp_locale(s, m, ln))) | |
1075 | && (norun || regtry(prog, s)) ) | |
1076 | goto got_it; | |
1077 | s++; | |
1078 | } | |
b3c9acc1 IZ |
1079 | } |
1080 | break; | |
bbce6d69 | 1081 | case BOUNDL: |
3280af22 | 1082 | PL_reg_flags |= RF_tainted; |
bbce6d69 | 1083 | /* FALL THROUGH */ |
a0d0e21e | 1084 | case BOUND: |
ffc61ed2 | 1085 | if (do_utf8) { |
12d33761 | 1086 | if (s == PL_bostr) |
ffc61ed2 JH |
1087 | tmp = '\n'; |
1088 | else { | |
1aa99e6b | 1089 | U8 *r = reghop3((U8*)s, -1, (U8*)startpos); |
9041c2e3 | 1090 | |
0064a8a9 JH |
1091 | if (s > (char*)r) |
1092 | tmp = (I32)utf8n_to_uvchr(r, s - (char*)r, 0, 0); | |
ffc61ed2 JH |
1093 | } |
1094 | tmp = ((OP(c) == BOUND ? | |
9041c2e3 | 1095 | isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0); |
8269fa76 | 1096 | LOAD_UTF8_CHARCLASS(alnum,"a"); |
ffc61ed2 JH |
1097 | while (s < strend) { |
1098 | if (tmp == !(OP(c) == BOUND ? | |
3568d838 | 1099 | swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) : |
ffc61ed2 JH |
1100 | isALNUM_LC_utf8((U8*)s))) |
1101 | { | |
1102 | tmp = !tmp; | |
1103 | if ((norun || regtry(prog, s))) | |
1104 | goto got_it; | |
1105 | } | |
1106 | s += UTF8SKIP(s); | |
a687059c | 1107 | } |
a0d0e21e | 1108 | } |
667bb95a | 1109 | else { |
12d33761 | 1110 | tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n'; |
ffc61ed2 JH |
1111 | tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0); |
1112 | while (s < strend) { | |
1113 | if (tmp == | |
1114 | !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) { | |
1115 | tmp = !tmp; | |
1116 | if ((norun || regtry(prog, s))) | |
1117 | goto got_it; | |
1118 | } | |
1119 | s++; | |
a0ed51b3 | 1120 | } |
a0ed51b3 | 1121 | } |
6eb5f6b9 | 1122 | if ((!prog->minlen && tmp) && (norun || regtry(prog, s))) |
a0ed51b3 LW |
1123 | goto got_it; |
1124 | break; | |
bbce6d69 | 1125 | case NBOUNDL: |
3280af22 | 1126 | PL_reg_flags |= RF_tainted; |
bbce6d69 | 1127 | /* FALL THROUGH */ |
a0d0e21e | 1128 | case NBOUND: |
ffc61ed2 | 1129 | if (do_utf8) { |
12d33761 | 1130 | if (s == PL_bostr) |
ffc61ed2 JH |
1131 | tmp = '\n'; |
1132 | else { | |
1aa99e6b | 1133 | U8 *r = reghop3((U8*)s, -1, (U8*)startpos); |
9041c2e3 | 1134 | |
0064a8a9 JH |
1135 | if (s > (char*)r) |
1136 | tmp = (I32)utf8n_to_uvchr(r, s - (char*)r, 0, 0); | |
ffc61ed2 JH |
1137 | } |
1138 | tmp = ((OP(c) == NBOUND ? | |
9041c2e3 | 1139 | isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0); |
8269fa76 | 1140 | LOAD_UTF8_CHARCLASS(alnum,"a"); |
ffc61ed2 JH |
1141 | while (s < strend) { |
1142 | if (tmp == !(OP(c) == NBOUND ? | |
3568d838 | 1143 | swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8) : |
ffc61ed2 JH |
1144 | isALNUM_LC_utf8((U8*)s))) |
1145 | tmp = !tmp; | |
1146 | else if ((norun || regtry(prog, s))) | |
1147 | goto got_it; | |
1148 | s += UTF8SKIP(s); | |
1149 | } | |
a0d0e21e | 1150 | } |
667bb95a | 1151 | else { |
12d33761 | 1152 | tmp = (s != PL_bostr) ? UCHARAT(s - 1) : '\n'; |
ffc61ed2 JH |
1153 | tmp = ((OP(c) == NBOUND ? |
1154 | isALNUM(tmp) : isALNUM_LC(tmp)) != 0); | |
1155 | while (s < strend) { | |
1156 | if (tmp == | |
1157 | !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s))) | |
1158 | tmp = !tmp; | |
1159 | else if ((norun || regtry(prog, s))) | |
1160 | goto got_it; | |
1161 | s++; | |
1162 | } | |
a0ed51b3 | 1163 | } |
6eb5f6b9 | 1164 | if ((!prog->minlen && !tmp) && (norun || regtry(prog, s))) |
a0ed51b3 LW |
1165 | goto got_it; |
1166 | break; | |
a0d0e21e | 1167 | case ALNUM: |
ffc61ed2 | 1168 | if (do_utf8) { |
8269fa76 | 1169 | LOAD_UTF8_CHARCLASS(alnum,"a"); |
ffc61ed2 | 1170 | while (s < strend) { |
3568d838 | 1171 | if (swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) { |
ffc61ed2 JH |
1172 | if (tmp && (norun || regtry(prog, s))) |
1173 | goto got_it; | |
1174 | else | |
1175 | tmp = doevery; | |
1176 | } | |
bbce6d69 | 1177 | else |
ffc61ed2 JH |
1178 | tmp = 1; |
1179 | s += UTF8SKIP(s); | |
bbce6d69 | 1180 | } |
bbce6d69 | 1181 | } |
ffc61ed2 JH |
1182 | else { |
1183 | while (s < strend) { | |
1184 | if (isALNUM(*s)) { | |
1185 | if (tmp && (norun || regtry(prog, s))) | |
1186 | goto got_it; | |
1187 | else | |
1188 | tmp = doevery; | |
1189 | } | |
a0ed51b3 | 1190 | else |
ffc61ed2 JH |
1191 | tmp = 1; |
1192 | s++; | |
a0ed51b3 | 1193 | } |
a0ed51b3 LW |
1194 | } |
1195 | break; | |
bbce6d69 | 1196 | case ALNUML: |
3280af22 | 1197 | PL_reg_flags |= RF_tainted; |
ffc61ed2 JH |
1198 | if (do_utf8) { |
1199 | while (s < strend) { | |
1200 | if (isALNUM_LC_utf8((U8*)s)) { | |
1201 | if (tmp && (norun || regtry(prog, s))) | |
1202 | goto got_it; | |
1203 | else | |
1204 | tmp = doevery; | |
1205 | } | |
a687059c | 1206 | else |
ffc61ed2 JH |
1207 | tmp = 1; |
1208 | s += UTF8SKIP(s); | |
a0d0e21e | 1209 | } |
a0d0e21e | 1210 | } |
ffc61ed2 JH |
1211 | else { |
1212 | while (s < strend) { | |
1213 | if (isALNUM_LC(*s)) { | |
1214 | if (tmp && (norun || regtry(prog, s))) | |
1215 | goto got_it; | |
1216 | else | |
1217 | tmp = doevery; | |
1218 | } | |
a0ed51b3 | 1219 | else |
ffc61ed2 JH |
1220 | tmp = 1; |
1221 | s++; | |
a0ed51b3 | 1222 | } |
a0ed51b3 LW |
1223 | } |
1224 | break; | |
a0d0e21e | 1225 | case NALNUM: |
ffc61ed2 | 1226 | if (do_utf8) { |
8269fa76 | 1227 | LOAD_UTF8_CHARCLASS(alnum,"a"); |
ffc61ed2 | 1228 | while (s < strend) { |
3568d838 | 1229 | if (!swash_fetch(PL_utf8_alnum, (U8*)s, do_utf8)) { |
ffc61ed2 JH |
1230 | if (tmp && (norun || regtry(prog, s))) |
1231 | goto got_it; | |
1232 | else | |
1233 | tmp = doevery; | |
1234 | } | |
bbce6d69 | 1235 | else |
ffc61ed2 JH |
1236 | tmp = 1; |
1237 | s += UTF8SKIP(s); | |
bbce6d69 | 1238 | } |
bbce6d69 | 1239 | } |
ffc61ed2 JH |
1240 | else { |
1241 | while (s < strend) { | |
1242 | if (!isALNUM(*s)) { | |
1243 | if (tmp && (norun || regtry(prog, s))) | |
1244 | goto got_it; | |
1245 | else | |
1246 | tmp = doevery; | |
1247 | } | |
a0ed51b3 | 1248 | else |
ffc61ed2 JH |
1249 | tmp = 1; |
1250 | s++; | |
a0ed51b3 | 1251 | } |
a0ed51b3 LW |
1252 | } |
1253 | break; | |
bbce6d69 | 1254 | case NALNUML: |
3280af22 | 1255 | PL_reg_flags |= RF_tainted; |
ffc61ed2 JH |
1256 | if (do_utf8) { |
1257 | while (s < strend) { | |
1258 | if (!isALNUM_LC_utf8((U8*)s)) { | |
1259 | if (tmp && (norun || regtry(prog, s))) | |
1260 | goto got_it; | |
1261 | else | |
1262 | tmp = doevery; | |
1263 | } | |
a687059c | 1264 | else |
ffc61ed2 JH |
1265 | tmp = 1; |
1266 | s += UTF8SKIP(s); | |
a687059c | 1267 | } |
a0d0e21e | 1268 | } |
ffc61ed2 JH |
1269 | else { |
1270 | while (s < strend) { | |
1271 | if (!isALNUM_LC(*s)) { | |
1272 | if (tmp && (norun || regtry(prog, s))) | |
1273 | goto got_it; | |
1274 | else | |
1275 | tmp = doevery; | |
1276 | } | |
a0ed51b3 | 1277 | else |
ffc61ed2 JH |
1278 | tmp = 1; |
1279 | s++; | |
a0ed51b3 | 1280 | } |
a0ed51b3 LW |
1281 | } |
1282 | break; | |
a0d0e21e | 1283 | case SPACE: |
ffc61ed2 | 1284 | if (do_utf8) { |
8269fa76 | 1285 | LOAD_UTF8_CHARCLASS(space," "); |
ffc61ed2 | 1286 | while (s < strend) { |
3568d838 | 1287 | if (*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, do_utf8)) { |
ffc61ed2 JH |
1288 | if (tmp && (norun || regtry(prog, s))) |
1289 | goto got_it; | |
1290 | else | |
1291 | tmp = doevery; | |
1292 | } | |
a0d0e21e | 1293 | else |
ffc61ed2 JH |
1294 | tmp = 1; |
1295 | s += UTF8SKIP(s); | |
2304df62 | 1296 | } |
a0d0e21e | 1297 | } |
ffc61ed2 JH |
1298 | else { |
1299 | while (s < strend) { | |
1300 | if (isSPACE(*s)) { | |
1301 | if (tmp && (norun || regtry(prog, s))) | |
1302 | goto got_it; | |
1303 | else | |
1304 | tmp = doevery; | |
1305 | } | |
a0ed51b3 | 1306 | else |
ffc61ed2 JH |
1307 | tmp = 1; |
1308 | s++; | |
a0ed51b3 | 1309 | } |
a0ed51b3 LW |
1310 | } |
1311 | break; | |
bbce6d69 | 1312 | case SPACEL: |
3280af22 | 1313 | PL_reg_flags |= RF_tainted; |
ffc61ed2 JH |
1314 | if (do_utf8) { |
1315 | while (s < strend) { | |
1316 | if (*s == ' ' || isSPACE_LC_utf8((U8*)s)) { | |
1317 | if (tmp && (norun || regtry(prog, s))) | |
1318 | goto got_it; | |
1319 | else | |
1320 | tmp = doevery; | |
1321 | } | |
bbce6d69 | 1322 | else |
ffc61ed2 JH |
1323 | tmp = 1; |
1324 | s += UTF8SKIP(s); | |
bbce6d69 | 1325 | } |
bbce6d69 | 1326 | } |
ffc61ed2 JH |
1327 | else { |
1328 | while (s < strend) { | |
1329 | if (isSPACE_LC(*s)) { | |
1330 | if (tmp && (norun || regtry(prog, s))) | |
1331 | goto got_it; | |
1332 | else | |
1333 | tmp = doevery; | |
1334 | } | |
a0ed51b3 | 1335 | else |
ffc61ed2 JH |
1336 | tmp = 1; |
1337 | s++; | |
a0ed51b3 | 1338 | } |
a0ed51b3 LW |
1339 | } |
1340 | break; | |
a0d0e21e | 1341 | case NSPACE: |
ffc61ed2 | 1342 | if (do_utf8) { |
8269fa76 | 1343 | LOAD_UTF8_CHARCLASS(space," "); |
ffc61ed2 | 1344 | while (s < strend) { |
3568d838 | 1345 | if (!(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s, do_utf8))) { |
ffc61ed2 JH |
1346 | if (tmp && (norun || regtry(prog, s))) |
1347 | goto got_it; | |
1348 | else | |
1349 | tmp = doevery; | |
1350 | } | |
a0d0e21e | 1351 | else |
ffc61ed2 JH |
1352 | tmp = 1; |
1353 | s += UTF8SKIP(s); | |
a687059c | 1354 | } |
a0d0e21e | 1355 | } |
ffc61ed2 JH |
1356 | else { |
1357 | while (s < strend) { | |
1358 | if (!isSPACE(*s)) { | |
1359 | if (tmp && (norun || regtry(prog, s))) | |
1360 | goto got_it; | |
1361 | else | |
1362 | tmp = doevery; | |
1363 | } | |
a0ed51b3 | 1364 | else |
ffc61ed2 JH |
1365 | tmp = 1; |
1366 | s++; | |
a0ed51b3 | 1367 | } |
a0ed51b3 LW |
1368 | } |
1369 | break; | |
bbce6d69 | 1370 | case NSPACEL: |
3280af22 | 1371 | PL_reg_flags |= RF_tainted; |
ffc61ed2 JH |
1372 | if (do_utf8) { |
1373 | while (s < strend) { | |
1374 | if (!(*s == ' ' || isSPACE_LC_utf8((U8*)s))) { | |
1375 | if (tmp && (norun || regtry(prog, s))) | |
1376 | goto got_it; | |
1377 | else | |
1378 | tmp = doevery; | |
1379 | } | |
bbce6d69 | 1380 | else |
ffc61ed2 JH |
1381 | tmp = 1; |
1382 | s += UTF8SKIP(s); | |
bbce6d69 | 1383 | } |
bbce6d69 | 1384 | } |
ffc61ed2 JH |
1385 | else { |
1386 | while (s < strend) { | |
1387 | if (!isSPACE_LC(*s)) { | |
1388 | if (tmp && (norun || regtry(prog, s))) | |
1389 | goto got_it; | |
1390 | else | |
1391 | tmp = doevery; | |
1392 | } | |
a0ed51b3 | 1393 | else |
ffc61ed2 JH |
1394 | tmp = 1; |
1395 | s++; | |
a0ed51b3 | 1396 | } |
a0ed51b3 LW |
1397 | } |
1398 | break; | |
a0d0e21e | 1399 | case DIGIT: |
ffc61ed2 | 1400 | if (do_utf8) { |
8269fa76 | 1401 | LOAD_UTF8_CHARCLASS(digit,"0"); |
ffc61ed2 | 1402 | while (s < strend) { |
3568d838 | 1403 | if (swash_fetch(PL_utf8_digit,(U8*)s, do_utf8)) { |
ffc61ed2 JH |
1404 | if (tmp && (norun || regtry(prog, s))) |
1405 | goto got_it; | |
1406 | else | |
1407 | tmp = doevery; | |
1408 | } | |
a0d0e21e | 1409 | else |
ffc61ed2 JH |
1410 | tmp = 1; |
1411 | s += UTF8SKIP(s); | |
2b69d0c2 | 1412 | } |
a0d0e21e | 1413 | } |
ffc61ed2 JH |
1414 | else { |
1415 | while (s < strend) { | |
1416 | if (isDIGIT(*s)) { | |
1417 | if (tmp && (norun || regtry(prog, s))) | |
1418 | goto got_it; | |
1419 | else | |
1420 | tmp = doevery; | |
1421 | } | |
a0ed51b3 | 1422 | else |
ffc61ed2 JH |
1423 | tmp = 1; |
1424 | s++; | |
a0ed51b3 | 1425 | } |
a0ed51b3 LW |
1426 | } |
1427 | break; | |
b8c5462f JH |
1428 | case DIGITL: |
1429 | PL_reg_flags |= RF_tainted; | |
ffc61ed2 JH |
1430 | if (do_utf8) { |
1431 | while (s < strend) { | |
1432 | if (isDIGIT_LC_utf8((U8*)s)) { | |
1433 | if (tmp && (norun || regtry(prog, s))) | |
1434 | goto got_it; | |
1435 | else | |
1436 | tmp = doevery; | |
1437 | } | |
b8c5462f | 1438 | else |
ffc61ed2 JH |
1439 | tmp = 1; |
1440 | s += UTF8SKIP(s); | |
b8c5462f | 1441 | } |
b8c5462f | 1442 | } |
ffc61ed2 JH |
1443 | else { |
1444 | while (s < strend) { | |
1445 | if (isDIGIT_LC(*s)) { | |
1446 | if (tmp && (norun || regtry(prog, s))) | |
1447 | goto got_it; | |
1448 | else | |
1449 | tmp = doevery; | |
1450 | } | |
b8c5462f | 1451 | else |
ffc61ed2 JH |
1452 | tmp = 1; |
1453 | s++; | |
b8c5462f | 1454 | } |
b8c5462f JH |
1455 | } |
1456 | break; | |
a0d0e21e | 1457 | case NDIGIT: |
ffc61ed2 | 1458 | if (do_utf8) { |
8269fa76 | 1459 | LOAD_UTF8_CHARCLASS(digit,"0"); |
ffc61ed2 | 1460 | while (s < strend) { |
3568d838 | 1461 | if (!swash_fetch(PL_utf8_digit,(U8*)s, do_utf8)) { |
ffc61ed2 JH |
1462 | if (tmp && (norun || regtry(prog, s))) |
1463 | goto got_it; | |
1464 | else | |
1465 | tmp = doevery; | |
1466 | } | |
a0d0e21e | 1467 | else |
ffc61ed2 JH |
1468 | tmp = 1; |
1469 | s += UTF8SKIP(s); | |
a687059c | 1470 | } |
a0d0e21e | 1471 | } |
ffc61ed2 JH |
1472 | else { |
1473 | while (s < strend) { | |
1474 | if (!isDIGIT(*s)) { | |
1475 | if (tmp && (norun || regtry(prog, s))) | |
1476 | goto got_it; | |
1477 | else | |
1478 | tmp = doevery; | |
1479 | } | |
a0ed51b3 | 1480 | else |
ffc61ed2 JH |
1481 | tmp = 1; |
1482 | s++; | |
a0ed51b3 | 1483 | } |
a0ed51b3 LW |
1484 | } |
1485 | break; | |
b8c5462f JH |
1486 | case NDIGITL: |
1487 | PL_reg_flags |= RF_tainted; | |
ffc61ed2 JH |
1488 | if (do_utf8) { |
1489 | while (s < strend) { | |
1490 | if (!isDIGIT_LC_utf8((U8*)s)) { | |
1491 | if (tmp && (norun || regtry(prog, s))) | |
1492 | goto got_it; | |
1493 | else | |
1494 | tmp = doevery; | |
1495 | } | |
b8c5462f | 1496 | else |
ffc61ed2 JH |
1497 | tmp = 1; |
1498 | s += UTF8SKIP(s); | |
b8c5462f | 1499 | } |
a0ed51b3 | 1500 | } |
ffc61ed2 JH |
1501 | else { |
1502 | while (s < strend) { | |
1503 | if (!isDIGIT_LC(*s)) { | |
1504 | if (tmp && (norun || regtry(prog, s))) | |
1505 | goto got_it; | |
1506 | else | |
1507 | tmp = doevery; | |
1508 | } | |
cf93c79d | 1509 | else |
ffc61ed2 JH |
1510 | tmp = 1; |
1511 | s++; | |
b8c5462f | 1512 | } |
b8c5462f JH |
1513 | } |
1514 | break; | |
b3c9acc1 | 1515 | default: |
3c3eec57 GS |
1516 | Perl_croak(aTHX_ "panic: unknown regstclass %d", (int)OP(c)); |
1517 | break; | |
d6a28714 | 1518 | } |
6eb5f6b9 JH |
1519 | return 0; |
1520 | got_it: | |
1521 | return s; | |
1522 | } | |
1523 | ||
1524 | /* | |
1525 | - regexec_flags - match a regexp against a string | |
1526 | */ | |
1527 | I32 | |
1528 | Perl_regexec_flags(pTHX_ register regexp *prog, char *stringarg, register char *strend, | |
1529 | char *strbeg, I32 minend, SV *sv, void *data, U32 flags) | |
1530 | /* strend: pointer to null at end of string */ | |
1531 | /* strbeg: real beginning of string */ | |
1532 | /* minend: end of match must be >=minend after stringarg. */ | |
1533 | /* data: May be used for some additional optimizations. */ | |
1534 | /* nosave: For optimizations. */ | |
1535 | { | |
6eb5f6b9 JH |
1536 | register char *s; |
1537 | register regnode *c; | |
1538 | register char *startpos = stringarg; | |
6eb5f6b9 JH |
1539 | I32 minlen; /* must match at least this many chars */ |
1540 | I32 dontbother = 0; /* how many characters not to try at end */ | |
155aba94 | 1541 | /* I32 start_shift = 0; */ /* Offset of the start to find |
6eb5f6b9 JH |
1542 | constant substr. */ /* CC */ |
1543 | I32 end_shift = 0; /* Same for the end. */ /* CC */ | |
1544 | I32 scream_pos = -1; /* Internal iterator of scream. */ | |
1545 | char *scream_olds; | |
1546 | SV* oreplsv = GvSV(PL_replgv); | |
1aa99e6b | 1547 | bool do_utf8 = DO_UTF8(sv); |
2a782b5b | 1548 | #ifdef DEBUGGING |
ce333219 | 1549 | SV *dsv = PERL_DEBUG_PAD_ZERO(0); |
2a782b5b | 1550 | #endif |
6eb5f6b9 JH |
1551 | |
1552 | PL_regcc = 0; | |
1553 | ||
1554 | cache_re(prog); | |
1555 | #ifdef DEBUGGING | |
aea4f609 | 1556 | PL_regnarrate = DEBUG_r_TEST; |
6eb5f6b9 JH |
1557 | #endif |
1558 | ||
1559 | /* Be paranoid... */ | |
1560 | if (prog == NULL || startpos == NULL) { | |
1561 | Perl_croak(aTHX_ "NULL regexp parameter"); | |
1562 | return 0; | |
1563 | } | |
1564 | ||
1565 | minlen = prog->minlen; | |
a72c7584 JH |
1566 | if (strend - startpos < minlen) { |
1567 | DEBUG_r(PerlIO_printf(Perl_debug_log, | |
1568 | "String too short [regexec_flags]...\n")); | |
1569 | goto phooey; | |
1aa99e6b | 1570 | } |
6eb5f6b9 | 1571 | |
6eb5f6b9 JH |
1572 | /* Check validity of program. */ |
1573 | if (UCHARAT(prog->program) != REG_MAGIC) { | |
1574 | Perl_croak(aTHX_ "corrupted regexp program"); | |
1575 | } | |
1576 | ||
1577 | PL_reg_flags = 0; | |
1578 | PL_reg_eval_set = 0; | |
1579 | PL_reg_maxiter = 0; | |
1580 | ||
1581 | if (prog->reganch & ROPT_UTF8) | |
1582 | PL_reg_flags |= RF_utf8; | |
1583 | ||
1584 | /* Mark beginning of line for ^ and lookbehind. */ | |
1585 | PL_regbol = startpos; | |
1586 | PL_bostr = strbeg; | |
1587 | PL_reg_sv = sv; | |
1588 | ||
1589 | /* Mark end of line for $ (and such) */ | |
1590 | PL_regeol = strend; | |
1591 | ||
1592 | /* see how far we have to get to not match where we matched before */ | |
1593 | PL_regtill = startpos+minend; | |
1594 | ||
1595 | /* We start without call_cc context. */ | |
1596 | PL_reg_call_cc = 0; | |
1597 | ||
1598 | /* If there is a "must appear" string, look for it. */ | |
1599 | s = startpos; | |
1600 | ||
1601 | if (prog->reganch & ROPT_GPOS_SEEN) { /* Need to have PL_reg_ganch */ | |
1602 | MAGIC *mg; | |
1603 | ||
1604 | if (flags & REXEC_IGNOREPOS) /* Means: check only at start */ | |
1605 | PL_reg_ganch = startpos; | |
1606 | else if (sv && SvTYPE(sv) >= SVt_PVMG | |
1607 | && SvMAGIC(sv) | |
14befaf4 DM |
1608 | && (mg = mg_find(sv, PERL_MAGIC_regex_global)) |
1609 | && mg->mg_len >= 0) { | |
6eb5f6b9 JH |
1610 | PL_reg_ganch = strbeg + mg->mg_len; /* Defined pos() */ |
1611 | if (prog->reganch & ROPT_ANCH_GPOS) { | |
f33976b4 | 1612 | if (s > PL_reg_ganch) |
6eb5f6b9 JH |
1613 | goto phooey; |
1614 | s = PL_reg_ganch; | |
1615 | } | |
1616 | } | |
1617 | else /* pos() not defined */ | |
1618 | PL_reg_ganch = strbeg; | |
1619 | } | |
1620 | ||
699c3c34 JH |
1621 | if (do_utf8 == (UTF!=0) && |
1622 | !(flags & REXEC_CHECKED) && prog->check_substr != Nullsv) { | |
6eb5f6b9 JH |
1623 | re_scream_pos_data d; |
1624 | ||
1625 | d.scream_olds = &scream_olds; | |
1626 | d.scream_pos = &scream_pos; | |
1627 | s = re_intuit_start(prog, sv, s, strend, flags, &d); | |
3fa9c3d7 JH |
1628 | if (!s) { |
1629 | DEBUG_r(PerlIO_printf(Perl_debug_log, "Not present...\n")); | |
6eb5f6b9 | 1630 | goto phooey; /* not present */ |
3fa9c3d7 | 1631 | } |
6eb5f6b9 JH |
1632 | } |
1633 | ||
2a782b5b | 1634 | DEBUG_r({ |
df1ffd02 JH |
1635 | char *s = do_utf8 ? sv_uni_display(dsv, sv, 60, 0) : startpos; |
1636 | int len = do_utf8 ? strlen(s) : strend - startpos; | |
2a782b5b JH |
1637 | if (!PL_colorset) |
1638 | reginitcolors(); | |
1639 | PerlIO_printf(Perl_debug_log, | |
1640 | "%sMatching REx%s `%s%.60s%s%s' against `%s%.*s%s%s'\n", | |
1641 | PL_colors[4],PL_colors[5],PL_colors[0], | |
1642 | prog->precomp, | |
1643 | PL_colors[1], | |
1644 | (strlen(prog->precomp) > 60 ? "..." : ""), | |
1645 | PL_colors[0], | |
1646 | (int)(len > 60 ? 60 : len), | |
1647 | s, PL_colors[1], | |
1648 | (len > 60 ? "..." : "") | |
1649 | ); | |
1650 | }); | |
6eb5f6b9 JH |
1651 | |
1652 | /* Simplest case: anchored match need be tried only once. */ | |
1653 | /* [unless only anchor is BOL and multiline is set] */ | |
1654 | if (prog->reganch & (ROPT_ANCH & ~ROPT_ANCH_GPOS)) { | |
1655 | if (s == startpos && regtry(prog, startpos)) | |
1656 | goto got_it; | |
1657 | else if (PL_multiline || (prog->reganch & ROPT_IMPLICIT) | |
1658 | || (prog->reganch & ROPT_ANCH_MBOL)) /* XXXX SBOL? */ | |
1659 | { | |
1660 | char *end; | |
1661 | ||
1662 | if (minlen) | |
1663 | dontbother = minlen - 1; | |
1aa99e6b | 1664 | end = HOP3c(strend, -dontbother, strbeg) - 1; |
6eb5f6b9 JH |
1665 | /* for multiline we only have to try after newlines */ |
1666 | if (prog->check_substr) { | |
1667 | if (s == startpos) | |
1668 | goto after_try; | |
1669 | while (1) { | |
1670 | if (regtry(prog, s)) | |
1671 | goto got_it; | |
1672 | after_try: | |
1673 | if (s >= end) | |
1674 | goto phooey; | |
1675 | if (prog->reganch & RE_USE_INTUIT) { | |
1676 | s = re_intuit_start(prog, sv, s + 1, strend, flags, NULL); | |
1677 | if (!s) | |
1678 | goto phooey; | |
1679 | } | |
1680 | else | |
1681 | s++; | |
1682 | } | |
1683 | } else { | |
1684 | if (s > startpos) | |
1685 | s--; | |
1686 | while (s < end) { | |
1687 | if (*s++ == '\n') { /* don't need PL_utf8skip here */ | |
1688 | if (regtry(prog, s)) | |
1689 | goto got_it; | |
1690 | } | |
1691 | } | |
1692 | } | |
1693 | } | |
1694 | goto phooey; | |
1695 | } else if (prog->reganch & ROPT_ANCH_GPOS) { | |
1696 | if (regtry(prog, PL_reg_ganch)) | |
1697 | goto got_it; | |
1698 | goto phooey; | |
1699 | } | |
1700 | ||
1701 | /* Messy cases: unanchored match. */ | |
9041c2e3 | 1702 | if (prog->anchored_substr && prog->reganch & ROPT_SKIP) { |
6eb5f6b9 JH |
1703 | /* we have /x+whatever/ */ |
1704 | /* it must be a one character string (XXXX Except UTF?) */ | |
1705 | char ch = SvPVX(prog->anchored_substr)[0]; | |
bf93d4cc GS |
1706 | #ifdef DEBUGGING |
1707 | int did_match = 0; | |
1708 | #endif | |
1709 | ||
1aa99e6b | 1710 | if (do_utf8) { |
6eb5f6b9 JH |
1711 | while (s < strend) { |
1712 | if (*s == ch) { | |
bf93d4cc | 1713 | DEBUG_r( did_match = 1 ); |
6eb5f6b9 JH |
1714 | if (regtry(prog, s)) goto got_it; |
1715 | s += UTF8SKIP(s); | |
1716 | while (s < strend && *s == ch) | |
1717 | s += UTF8SKIP(s); | |
1718 | } | |
1719 | s += UTF8SKIP(s); | |
1720 | } | |
1721 | } | |
1722 | else { | |
1723 | while (s < strend) { | |
1724 | if (*s == ch) { | |
bf93d4cc | 1725 | DEBUG_r( did_match = 1 ); |
6eb5f6b9 JH |
1726 | if (regtry(prog, s)) goto got_it; |
1727 | s++; | |
1728 | while (s < strend && *s == ch) | |
1729 | s++; | |
1730 | } | |
1731 | s++; | |
1732 | } | |
1733 | } | |
b7953727 | 1734 | DEBUG_r(if (!did_match) |
bf93d4cc | 1735 | PerlIO_printf(Perl_debug_log, |
b7953727 JH |
1736 | "Did not find anchored character...\n") |
1737 | ); | |
6eb5f6b9 JH |
1738 | } |
1739 | /*SUPPRESS 560*/ | |
1aa99e6b IH |
1740 | else if (do_utf8 == (UTF!=0) && |
1741 | (prog->anchored_substr != Nullsv | |
9041c2e3 | 1742 | || (prog->float_substr != Nullsv |
1aa99e6b | 1743 | && prog->float_max_offset < strend - s))) { |
9041c2e3 | 1744 | SV *must = prog->anchored_substr |
6eb5f6b9 | 1745 | ? prog->anchored_substr : prog->float_substr; |
9041c2e3 | 1746 | I32 back_max = |
6eb5f6b9 | 1747 | prog->anchored_substr ? prog->anchored_offset : prog->float_max_offset; |
9041c2e3 | 1748 | I32 back_min = |
6eb5f6b9 | 1749 | prog->anchored_substr ? prog->anchored_offset : prog->float_min_offset; |
1aa99e6b | 1750 | char *last = HOP3c(strend, /* Cannot start after this */ |
6eb5f6b9 | 1751 | -(I32)(CHR_SVLEN(must) |
1aa99e6b | 1752 | - (SvTAIL(must) != 0) + back_min), strbeg); |
6eb5f6b9 | 1753 | char *last1; /* Last position checked before */ |
bf93d4cc GS |
1754 | #ifdef DEBUGGING |
1755 | int did_match = 0; | |
1756 | #endif | |
6eb5f6b9 JH |
1757 | |
1758 | if (s > PL_bostr) | |
1759 | last1 = HOPc(s, -1); | |
1760 | else | |
1761 | last1 = s - 1; /* bogus */ | |
1762 | ||
1763 | /* XXXX check_substr already used to find `s', can optimize if | |
1764 | check_substr==must. */ | |
1765 | scream_pos = -1; | |
1766 | dontbother = end_shift; | |
1767 | strend = HOPc(strend, -dontbother); | |
1768 | while ( (s <= last) && | |
9041c2e3 | 1769 | ((flags & REXEC_SCREAM) |
1aa99e6b | 1770 | ? (s = screaminstr(sv, must, HOP3c(s, back_min, strend) - strbeg, |
6eb5f6b9 | 1771 | end_shift, &scream_pos, 0)) |
1aa99e6b | 1772 | : (s = fbm_instr((unsigned char*)HOP3(s, back_min, strend), |
9041c2e3 | 1773 | (unsigned char*)strend, must, |
6eb5f6b9 | 1774 | PL_multiline ? FBMrf_MULTILINE : 0))) ) { |
bf93d4cc | 1775 | DEBUG_r( did_match = 1 ); |
6eb5f6b9 JH |
1776 | if (HOPc(s, -back_max) > last1) { |
1777 | last1 = HOPc(s, -back_min); | |
1778 | s = HOPc(s, -back_max); | |
1779 | } | |
1780 | else { | |
1781 | char *t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1; | |
1782 | ||
1783 | last1 = HOPc(s, -back_min); | |
1784 | s = t; | |
1785 | } | |
1aa99e6b | 1786 | if (do_utf8) { |
6eb5f6b9 JH |
1787 | while (s <= last1) { |
1788 | if (regtry(prog, s)) | |
1789 | goto got_it; | |
1790 | s += UTF8SKIP(s); | |
1791 | } | |
1792 | } | |
1793 | else { | |
1794 | while (s <= last1) { | |
1795 | if (regtry(prog, s)) | |
1796 | goto got_it; | |
1797 | s++; | |
1798 | } | |
1799 | } | |
1800 | } | |
b7953727 JH |
1801 | DEBUG_r(if (!did_match) |
1802 | PerlIO_printf(Perl_debug_log, | |
1803 | "Did not find %s substr `%s%.*s%s'%s...\n", | |
bf93d4cc GS |
1804 | ((must == prog->anchored_substr) |
1805 | ? "anchored" : "floating"), | |
1806 | PL_colors[0], | |
1807 | (int)(SvCUR(must) - (SvTAIL(must)!=0)), | |
1808 | SvPVX(must), | |
b7953727 JH |
1809 | PL_colors[1], (SvTAIL(must) ? "$" : "")) |
1810 | ); | |
6eb5f6b9 JH |
1811 | goto phooey; |
1812 | } | |
155aba94 | 1813 | else if ((c = prog->regstclass)) { |
66e933ab GS |
1814 | if (minlen && PL_regkind[(U8)OP(prog->regstclass)] != EXACT) |
1815 | /* don't bother with what can't match */ | |
6eb5f6b9 | 1816 | strend = HOPc(strend, -(minlen - 1)); |
ffc61ed2 JH |
1817 | DEBUG_r({ |
1818 | SV *prop = sv_newmortal(); | |
1819 | regprop(prop, c); | |
2a782b5b | 1820 | PerlIO_printf(Perl_debug_log, "Matching stclass `%s' against `%s'\n", SvPVX(prop), UTF ? sv_uni_display(dsv, sv, 60, 0) : s); |
ffc61ed2 | 1821 | }); |
6eb5f6b9 JH |
1822 | if (find_byclass(prog, c, s, strend, startpos, 0)) |
1823 | goto got_it; | |
bf93d4cc | 1824 | DEBUG_r(PerlIO_printf(Perl_debug_log, "Contradicts stclass...\n")); |
d6a28714 JH |
1825 | } |
1826 | else { | |
1827 | dontbother = 0; | |
1828 | if (prog->float_substr != Nullsv) { /* Trim the end. */ | |
1829 | char *last; | |
d6a28714 JH |
1830 | |
1831 | if (flags & REXEC_SCREAM) { | |
1832 | last = screaminstr(sv, prog->float_substr, s - strbeg, | |
1833 | end_shift, &scream_pos, 1); /* last one */ | |
1834 | if (!last) | |
ffc61ed2 | 1835 | last = scream_olds; /* Only one occurrence. */ |
b8c5462f | 1836 | } |
d6a28714 JH |
1837 | else { |
1838 | STRLEN len; | |
1839 | char *little = SvPV(prog->float_substr, len); | |
1840 | ||
1841 | if (SvTAIL(prog->float_substr)) { | |
1842 | if (memEQ(strend - len + 1, little, len - 1)) | |
1843 | last = strend - len + 1; | |
1844 | else if (!PL_multiline) | |
9041c2e3 | 1845 | last = memEQ(strend - len, little, len) |
d6a28714 | 1846 | ? strend - len : Nullch; |
b8c5462f | 1847 | else |
d6a28714 JH |
1848 | goto find_last; |
1849 | } else { | |
1850 | find_last: | |
9041c2e3 | 1851 | if (len) |
d6a28714 | 1852 | last = rninstr(s, strend, little, little + len); |
b8c5462f | 1853 | else |
d6a28714 | 1854 | last = strend; /* matching `$' */ |
b8c5462f | 1855 | } |
b8c5462f | 1856 | } |
bf93d4cc GS |
1857 | if (last == NULL) { |
1858 | DEBUG_r(PerlIO_printf(Perl_debug_log, | |
1859 | "%sCan't trim the tail, match fails (should not happen)%s\n", | |
1860 | PL_colors[4],PL_colors[5])); | |
1861 | goto phooey; /* Should not happen! */ | |
1862 | } | |
d6a28714 JH |
1863 | dontbother = strend - last + prog->float_min_offset; |
1864 | } | |
1865 | if (minlen && (dontbother < minlen)) | |
1866 | dontbother = minlen - 1; | |
1867 | strend -= dontbother; /* this one's always in bytes! */ | |
1868 | /* We don't know much -- general case. */ | |
1aa99e6b | 1869 | if (do_utf8) { |
d6a28714 JH |
1870 | for (;;) { |
1871 | if (regtry(prog, s)) | |
1872 | goto got_it; | |
1873 | if (s >= strend) | |
1874 | break; | |
b8c5462f | 1875 | s += UTF8SKIP(s); |
d6a28714 JH |
1876 | }; |
1877 | } | |
1878 | else { | |
1879 | do { | |
1880 | if (regtry(prog, s)) | |
1881 | goto got_it; | |
1882 | } while (s++ < strend); | |
1883 | } | |
1884 | } | |
1885 | ||
1886 | /* Failure. */ | |
1887 | goto phooey; | |
1888 | ||
1889 | got_it: | |
1890 | RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted); | |
1891 | ||
1892 | if (PL_reg_eval_set) { | |
1893 | /* Preserve the current value of $^R */ | |
1894 | if (oreplsv != GvSV(PL_replgv)) | |
1895 | sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is | |
1896 | restored, the value remains | |
1897 | the same. */ | |
acfe0abc | 1898 | restore_pos(aTHX_ 0); |
d6a28714 JH |
1899 | } |
1900 | ||
1901 | /* make sure $`, $&, $', and $digit will work later */ | |
1902 | if ( !(flags & REXEC_NOT_FIRST) ) { | |
1903 | if (RX_MATCH_COPIED(prog)) { | |
1904 | Safefree(prog->subbeg); | |
1905 | RX_MATCH_COPIED_off(prog); | |
1906 | } | |
1907 | if (flags & REXEC_COPY_STR) { | |
1908 | I32 i = PL_regeol - startpos + (stringarg - strbeg); | |
1909 | ||
1910 | s = savepvn(strbeg, i); | |
1911 | prog->subbeg = s; | |
1912 | prog->sublen = i; | |
1913 | RX_MATCH_COPIED_on(prog); | |
1914 | } | |
1915 | else { | |
1916 | prog->subbeg = strbeg; | |
1917 | prog->sublen = PL_regeol - strbeg; /* strend may have been modified */ | |
1918 | } | |
1919 | } | |
9041c2e3 | 1920 | |
d6a28714 JH |
1921 | return 1; |
1922 | ||
1923 | phooey: | |
bf93d4cc GS |
1924 | DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch failed%s\n", |
1925 | PL_colors[4],PL_colors[5])); | |
d6a28714 | 1926 | if (PL_reg_eval_set) |
acfe0abc | 1927 | restore_pos(aTHX_ 0); |
d6a28714 JH |
1928 | return 0; |
1929 | } | |
1930 | ||
1931 | /* | |
1932 | - regtry - try match at specific point | |
1933 | */ | |
1934 | STATIC I32 /* 0 failure, 1 success */ | |
1935 | S_regtry(pTHX_ regexp *prog, char *startpos) | |
1936 | { | |
d6a28714 JH |
1937 | register I32 i; |
1938 | register I32 *sp; | |
1939 | register I32 *ep; | |
1940 | CHECKPOINT lastcp; | |
1941 | ||
02db2b7b IZ |
1942 | #ifdef DEBUGGING |
1943 | PL_regindent = 0; /* XXXX Not good when matches are reenterable... */ | |
1944 | #endif | |
d6a28714 JH |
1945 | if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) { |
1946 | MAGIC *mg; | |
1947 | ||
1948 | PL_reg_eval_set = RS_init; | |
1949 | DEBUG_r(DEBUG_s( | |
b900a521 JH |
1950 | PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %"IVdf"\n", |
1951 | (IV)(PL_stack_sp - PL_stack_base)); | |
d6a28714 | 1952 | )); |
e8347627 | 1953 | SAVEI32(cxstack[cxstack_ix].blk_oldsp); |
d6a28714 JH |
1954 | cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base; |
1955 | /* Otherwise OP_NEXTSTATE will free whatever on stack now. */ | |
1956 | SAVETMPS; | |
1957 | /* Apparently this is not needed, judging by wantarray. */ | |
e8347627 | 1958 | /* SAVEI8(cxstack[cxstack_ix].blk_gimme); |
d6a28714 JH |
1959 | cxstack[cxstack_ix].blk_gimme = G_SCALAR; */ |
1960 | ||
1961 | if (PL_reg_sv) { | |
1962 | /* Make $_ available to executed code. */ | |
1963 | if (PL_reg_sv != DEFSV) { | |
4d1ff10f | 1964 | /* SAVE_DEFSV does *not* suffice here for USE_5005THREADS */ |
d6a28714 JH |
1965 | SAVESPTR(DEFSV); |
1966 | DEFSV = PL_reg_sv; | |
b8c5462f | 1967 | } |
d6a28714 | 1968 | |
9041c2e3 | 1969 | if (!(SvTYPE(PL_reg_sv) >= SVt_PVMG && SvMAGIC(PL_reg_sv) |
14befaf4 | 1970 | && (mg = mg_find(PL_reg_sv, PERL_MAGIC_regex_global)))) { |
d6a28714 | 1971 | /* prepare for quick setting of pos */ |
14befaf4 DM |
1972 | sv_magic(PL_reg_sv, (SV*)0, |
1973 | PERL_MAGIC_regex_global, Nullch, 0); | |
1974 | mg = mg_find(PL_reg_sv, PERL_MAGIC_regex_global); | |
d6a28714 | 1975 | mg->mg_len = -1; |
b8c5462f | 1976 | } |
d6a28714 JH |
1977 | PL_reg_magic = mg; |
1978 | PL_reg_oldpos = mg->mg_len; | |
c76ac1ee | 1979 | SAVEDESTRUCTOR_X(restore_pos, 0); |
d6a28714 | 1980 | } |
09687e5a | 1981 | if (!PL_reg_curpm) { |
0f79a09d | 1982 | Newz(22,PL_reg_curpm, 1, PMOP); |
09687e5a AB |
1983 | #ifdef USE_ITHREADS |
1984 | { | |
1985 | SV* repointer = newSViv(0); | |
577e12cc | 1986 | /* so we know which PL_regex_padav element is PL_reg_curpm */ |
35061a7e | 1987 | SvFLAGS(repointer) |= SVf_BREAK; |
09687e5a AB |
1988 | av_push(PL_regex_padav,repointer); |
1989 | PL_reg_curpm->op_pmoffset = av_len(PL_regex_padav); | |
1990 | PL_regex_pad = AvARRAY(PL_regex_padav); | |
1991 | } | |
1992 | #endif | |
1993 | } | |
aaa362c4 | 1994 | PM_SETRE(PL_reg_curpm, prog); |
d6a28714 JH |
1995 | PL_reg_oldcurpm = PL_curpm; |
1996 | PL_curpm = PL_reg_curpm; | |
1997 | if (RX_MATCH_COPIED(prog)) { | |
1998 | /* Here is a serious problem: we cannot rewrite subbeg, | |
1999 | since it may be needed if this match fails. Thus | |
2000 | $` inside (?{}) could fail... */ | |
2001 | PL_reg_oldsaved = prog->subbeg; | |
2002 | PL_reg_oldsavedlen = prog->sublen; | |
2003 | RX_MATCH_COPIED_off(prog); | |
2004 | } | |
2005 | else | |
2006 | PL_reg_oldsaved = Nullch; | |
2007 | prog->subbeg = PL_bostr; | |
2008 | prog->sublen = PL_regeol - PL_bostr; /* strend may have been modified */ | |
2009 | } | |
2010 | prog->startp[0] = startpos - PL_bostr; | |
2011 | PL_reginput = startpos; | |
2012 | PL_regstartp = prog->startp; | |
2013 | PL_regendp = prog->endp; | |
2014 | PL_reglastparen = &prog->lastparen; | |
a01268b5 | 2015 | PL_reglastcloseparen = &prog->lastcloseparen; |
d6a28714 JH |
2016 | prog->lastparen = 0; |
2017 | PL_regsize = 0; | |
2018 | DEBUG_r(PL_reg_starttry = startpos); | |
2019 | if (PL_reg_start_tmpl <= prog->nparens) { | |
2020 | PL_reg_start_tmpl = prog->nparens*3/2 + 3; | |
2021 | if(PL_reg_start_tmp) | |
2022 | Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*); | |
2023 | else | |
2024 | New(22,PL_reg_start_tmp, PL_reg_start_tmpl, char*); | |
2025 | } | |
2026 | ||
128e8167 JH |
2027 | #ifdef DEBUGGING |
2028 | sv_setpvn(PERL_DEBUG_PAD(0), "", 0); | |
2029 | sv_setpvn(PERL_DEBUG_PAD(1), "", 0); | |
2030 | sv_setpvn(PERL_DEBUG_PAD(2), "", 0); | |
2031 | #endif | |
2032 | ||
d6a28714 JH |
2033 | /* XXXX What this code is doing here?!!! There should be no need |
2034 | to do this again and again, PL_reglastparen should take care of | |
3dd2943c | 2035 | this! --ilya*/ |
dafc8851 JH |
2036 | |
2037 | /* Tests pat.t#187 and split.t#{13,14} seem to depend on this code. | |
2038 | * Actually, the code in regcppop() (which Ilya may be meaning by | |
daf18116 JH |
2039 | * PL_reglastparen), is not needed at all by the test suite |
2040 | * (op/regexp, op/pat, op/split), but that code is needed, oddly | |
2041 | * enough, for building DynaLoader, or otherwise this | |
2042 | * "Error: '*' not in typemap in DynaLoader.xs, line 164" | |
2043 | * will happen. Meanwhile, this code *is* needed for the | |
2044 | * above-mentioned test suite tests to succeed. The common theme | |
2045 | * on those tests seems to be returning null fields from matches. | |
2046 | * --jhi */ | |
dafc8851 | 2047 | #if 1 |
d6a28714 JH |
2048 | sp = prog->startp; |
2049 | ep = prog->endp; | |
2050 | if (prog->nparens) { | |
09e8ae3b | 2051 | for (i = prog->nparens; i > *PL_reglastparen; i--) { |
d6a28714 JH |
2052 | *++sp = -1; |
2053 | *++ep = -1; | |
2054 | } | |
2055 | } | |
dafc8851 | 2056 | #endif |
02db2b7b | 2057 | REGCP_SET(lastcp); |
d6a28714 JH |
2058 | if (regmatch(prog->program + 1)) { |
2059 | prog->endp[0] = PL_reginput - PL_bostr; | |
2060 | return 1; | |
2061 | } | |
02db2b7b | 2062 | REGCP_UNWIND(lastcp); |
d6a28714 JH |
2063 | return 0; |
2064 | } | |
2065 | ||
02db2b7b IZ |
2066 | #define RE_UNWIND_BRANCH 1 |
2067 | #define RE_UNWIND_BRANCHJ 2 | |
2068 | ||
2069 | union re_unwind_t; | |
2070 | ||
2071 | typedef struct { /* XX: makes sense to enlarge it... */ | |
2072 | I32 type; | |
2073 | I32 prev; | |
2074 | CHECKPOINT lastcp; | |
2075 | } re_unwind_generic_t; | |
2076 | ||
2077 | typedef struct { | |
2078 | I32 type; | |
2079 | I32 prev; | |
2080 | CHECKPOINT lastcp; | |
2081 | I32 lastparen; | |
2082 | regnode *next; | |
2083 | char *locinput; | |
2084 | I32 nextchr; | |
2085 | #ifdef DEBUGGING | |
2086 | int regindent; | |
2087 | #endif | |
2088 | } re_unwind_branch_t; | |
2089 | ||
2090 | typedef union re_unwind_t { | |
2091 | I32 type; | |
2092 | re_unwind_generic_t generic; | |
2093 | re_unwind_branch_t branch; | |
2094 | } re_unwind_t; | |
2095 | ||
8ba1375e MJD |
2096 | #define sayYES goto yes |
2097 | #define sayNO goto no | |
2098 | #define sayYES_FINAL goto yes_final | |
2099 | #define sayYES_LOUD goto yes_loud | |
2100 | #define sayNO_FINAL goto no_final | |
2101 | #define sayNO_SILENT goto do_no | |
2102 | #define saySAME(x) if (x) goto yes; else goto no | |
2103 | ||
2104 | #define REPORT_CODE_OFF 24 | |
2105 | ||
d6a28714 JH |
2106 | /* |
2107 | - regmatch - main matching routine | |
2108 | * | |
2109 | * Conceptually the strategy is simple: check to see whether the current | |
2110 | * node matches, call self recursively to see whether the rest matches, | |
2111 | * and then act accordingly. In practice we make some effort to avoid | |
2112 | * recursion, in particular by going through "ordinary" nodes (that don't | |
2113 | * need to know whether the rest of the match failed) by a loop instead of | |
2114 | * by recursion. | |
2115 | */ | |
2116 | /* [lwall] I've hoisted the register declarations to the outer block in order to | |
2117 | * maybe save a little bit of pushing and popping on the stack. It also takes | |
2118 | * advantage of machines that use a register save mask on subroutine entry. | |
2119 | */ | |
2120 | STATIC I32 /* 0 failure, 1 success */ | |
2121 | S_regmatch(pTHX_ regnode *prog) | |
2122 | { | |
d6a28714 JH |
2123 | register regnode *scan; /* Current node. */ |
2124 | regnode *next; /* Next node. */ | |
2125 | regnode *inner; /* Next node in internal branch. */ | |
2126 | register I32 nextchr; /* renamed nextchr - nextchar colides with | |
2127 | function of same name */ | |
2128 | register I32 n; /* no or next */ | |
b7953727 JH |
2129 | register I32 ln = 0; /* len or last */ |
2130 | register char *s = Nullch; /* operand or save */ | |
d6a28714 | 2131 | register char *locinput = PL_reginput; |
b7953727 | 2132 | register I32 c1 = 0, c2 = 0, paren; /* case fold search, parenth */ |
d6a28714 | 2133 | int minmod = 0, sw = 0, logical = 0; |
02db2b7b | 2134 | I32 unwind = 0; |
b7953727 | 2135 | #if 0 |
02db2b7b | 2136 | I32 firstcp = PL_savestack_ix; |
b7953727 | 2137 | #endif |
53c4c00c | 2138 | register bool do_utf8 = PL_reg_match_utf8; |
2a782b5b | 2139 | #ifdef DEBUGGING |
ce333219 JH |
2140 | SV *dsv0 = PERL_DEBUG_PAD_ZERO(0); |
2141 | SV *dsv1 = PERL_DEBUG_PAD_ZERO(1); | |
2142 | SV *dsv2 = PERL_DEBUG_PAD_ZERO(2); | |
2a782b5b | 2143 | #endif |
02db2b7b | 2144 | |
d6a28714 JH |
2145 | #ifdef DEBUGGING |
2146 | PL_regindent++; | |
2147 | #endif | |
2148 | ||
2149 | /* Note that nextchr is a byte even in UTF */ | |
2150 | nextchr = UCHARAT(locinput); | |
2151 | scan = prog; | |
2152 | while (scan != NULL) { | |
8ba1375e | 2153 | |
2a782b5b | 2154 | DEBUG_r( { |
d6a28714 JH |
2155 | SV *prop = sv_newmortal(); |
2156 | int docolor = *PL_colors[0]; | |
2157 | int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */ | |
1aa99e6b | 2158 | int l = (PL_regeol - locinput) > taill ? taill : (PL_regeol - locinput); |
d6a28714 JH |
2159 | /* The part of the string before starttry has one color |
2160 | (pref0_len chars), between starttry and current | |
2161 | position another one (pref_len - pref0_len chars), | |
2162 | after the current position the third one. | |
2163 | We assume that pref0_len <= pref_len, otherwise we | |
2164 | decrease pref0_len. */ | |
9041c2e3 | 2165 | int pref_len = (locinput - PL_bostr) > (5 + taill) - l |
1aa99e6b IH |
2166 | ? (5 + taill) - l : locinput - PL_bostr; |
2167 | int pref0_len; | |
d6a28714 | 2168 | |
df1ffd02 | 2169 | while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput - pref_len))) |
1aa99e6b IH |
2170 | pref_len++; |
2171 | pref0_len = pref_len - (locinput - PL_reg_starttry); | |
d6a28714 | 2172 | if (l + pref_len < (5 + taill) && l < PL_regeol - locinput) |
9041c2e3 | 2173 | l = ( PL_regeol - locinput > (5 + taill) - pref_len |
d6a28714 | 2174 | ? (5 + taill) - pref_len : PL_regeol - locinput); |
df1ffd02 | 2175 | while (do_utf8 && UTF8_IS_CONTINUATION(*(U8*)(locinput + l))) |
1aa99e6b | 2176 | l--; |
d6a28714 JH |
2177 | if (pref0_len < 0) |
2178 | pref0_len = 0; | |
2179 | if (pref0_len > pref_len) | |
2180 | pref0_len = pref_len; | |
2181 | regprop(prop, scan); | |
2a782b5b JH |
2182 | { |
2183 | char *s0 = | |
df1ffd02 | 2184 | do_utf8 ? |
2a782b5b JH |
2185 | pv_uni_display(dsv0, (U8*)(locinput - pref_len), |
2186 | pref0_len, 60, 0) : | |
2187 | locinput - pref_len; | |
df1ffd02 JH |
2188 | int len0 = do_utf8 ? strlen(s0) : pref0_len; |
2189 | char *s1 = do_utf8 ? | |
2a782b5b JH |
2190 | pv_uni_display(dsv1, (U8*)(locinput - pref_len + pref0_len), |
2191 | pref_len - pref0_len, 60, 0) : | |
2192 | locinput - pref_len + pref0_len; | |
df1ffd02 JH |
2193 | int len1 = do_utf8 ? strlen(s1) : pref_len - pref0_len; |
2194 | char *s2 = do_utf8 ? | |
2a782b5b JH |
2195 | pv_uni_display(dsv2, (U8*)locinput, |
2196 | PL_regeol - locinput, 60, 0) : | |
2197 | locinput; | |
df1ffd02 | 2198 | int len2 = do_utf8 ? strlen(s2) : l; |
2a782b5b JH |
2199 | PerlIO_printf(Perl_debug_log, |
2200 | "%4"IVdf" <%s%.*s%s%s%.*s%s%s%s%.*s%s>%*s|%3"IVdf":%*s%s\n", | |
2201 | (IV)(locinput - PL_bostr), | |
2202 | PL_colors[4], | |
2203 | len0, s0, | |
2204 | PL_colors[5], | |
2205 | PL_colors[2], | |
2206 | len1, s1, | |
2207 | PL_colors[3], | |
2208 | (docolor ? "" : "> <"), | |
2209 | PL_colors[0], | |
2210 | len2, s2, | |
2211 | PL_colors[1], | |
2212 | 15 - l - pref_len + 1, | |
2213 | "", | |
2214 | (IV)(scan - PL_regprogram), PL_regindent*2, "", | |
2215 | SvPVX(prop)); | |
2216 | } | |
2217 | }); | |
d6a28714 JH |
2218 | |
2219 | next = scan + NEXT_OFF(scan); | |
2220 | if (next == scan) | |
2221 | next = NULL; | |
2222 | ||
2223 | switch (OP(scan)) { | |
2224 | case BOL: | |
12d33761 HS |
2225 | if (locinput == PL_bostr || (PL_multiline && |
2226 | (nextchr || locinput < PL_regeol) && locinput[-1] == '\n') ) | |
d6a28714 JH |
2227 | { |
2228 | /* regtill = regbol; */ | |
b8c5462f JH |
2229 | break; |
2230 | } | |
d6a28714 JH |
2231 | sayNO; |
2232 | case MBOL: | |
12d33761 HS |
2233 | if (locinput == PL_bostr || |
2234 | ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n')) | |
d6a28714 | 2235 | { |
b8c5462f JH |
2236 | break; |
2237 | } | |
d6a28714 JH |
2238 | sayNO; |
2239 | case SBOL: | |
c2a73568 | 2240 | if (locinput == PL_bostr) |
b8c5462f | 2241 | break; |
d6a28714 JH |
2242 | sayNO; |
2243 | case GPOS: | |
2244 | if (locinput == PL_reg_ganch) | |
2245 | break; | |
2246 | sayNO; | |
2247 | case EOL: | |
2248 | if (PL_multiline) | |
2249 | goto meol; | |
2250 | else | |
2251 | goto seol; | |
2252 | case MEOL: | |
2253 | meol: | |
2254 | if ((nextchr || locinput < PL_regeol) && nextchr != '\n') | |
b8c5462f | 2255 | sayNO; |
b8c5462f | 2256 | break; |
d6a28714 JH |
2257 | case SEOL: |
2258 | seol: | |
2259 | if ((nextchr || locinput < PL_regeol) && nextchr != '\n') | |
b8c5462f | 2260 | sayNO; |
d6a28714 | 2261 | if (PL_regeol - locinput > 1) |
b8c5462f | 2262 | sayNO; |
b8c5462f | 2263 | break; |
d6a28714 JH |
2264 | case EOS: |
2265 | if (PL_regeol != locinput) | |
b8c5462f | 2266 | sayNO; |
d6a28714 | 2267 | break; |
ffc61ed2 | 2268 | case SANY: |
d6a28714 | 2269 | if (!nextchr && locinput >= PL_regeol) |
4633a7c4 | 2270 | sayNO; |
f33976b4 DB |
2271 | if (do_utf8) { |
2272 | locinput += PL_utf8skip[nextchr]; | |
2273 | if (locinput > PL_regeol) | |
2274 | sayNO; | |
2275 | nextchr = UCHARAT(locinput); | |
2276 | } | |
2277 | else | |
2278 | nextchr = UCHARAT(++locinput); | |
2279 | break; | |
2280 | case CANY: | |
2281 | if (!nextchr && locinput >= PL_regeol) | |
2282 | sayNO; | |
b8c5462f | 2283 | nextchr = UCHARAT(++locinput); |
a0d0e21e | 2284 | break; |
ffc61ed2 | 2285 | case REG_ANY: |
1aa99e6b IH |
2286 | if ((!nextchr && locinput >= PL_regeol) || nextchr == '\n') |
2287 | sayNO; | |
2288 | if (do_utf8) { | |
b8c5462f | 2289 | locinput += PL_utf8skip[nextchr]; |
d6a28714 JH |
2290 | if (locinput > PL_regeol) |
2291 | sayNO; | |
a0ed51b3 | 2292 | nextchr = UCHARAT(locinput); |
a0ed51b3 | 2293 | } |
1aa99e6b IH |
2294 | else |
2295 | nextchr = UCHARAT(++locinput); | |
a0ed51b3 | 2296 | break; |
d6a28714 | 2297 | case EXACT: |
cd439c50 IZ |
2298 | s = STRING(scan); |
2299 | ln = STR_LEN(scan); | |
1aa99e6b | 2300 | if (do_utf8 != (UTF!=0)) { |
bc517b45 | 2301 | /* The target and the pattern have differing utf8ness. */ |
1aa99e6b IH |
2302 | char *l = locinput; |
2303 | char *e = s + ln; | |
bc517b45 | 2304 | STRLEN ulen; |
a72c7584 | 2305 | |
5ff6fc6d JH |
2306 | if (do_utf8) { |
2307 | /* The target is utf8, the pattern is not utf8. */ | |
1aa99e6b IH |
2308 | while (s < e) { |
2309 | if (l >= PL_regeol) | |
5ff6fc6d JH |
2310 | sayNO; |
2311 | if (NATIVE_TO_UNI(*(U8*)s) != | |
bc517b45 | 2312 | utf8_to_uvchr((U8*)l, &ulen)) |
5ff6fc6d | 2313 | sayNO; |
bc517b45 | 2314 | l += ulen; |
5ff6fc6d | 2315 | s ++; |
1aa99e6b | 2316 | } |
5ff6fc6d JH |
2317 | } |
2318 | else { | |
2319 | /* The target is not utf8, the pattern is utf8. */ | |
1aa99e6b IH |
2320 | while (s < e) { |
2321 | if (l >= PL_regeol) | |
2322 | sayNO; | |
5ff6fc6d | 2323 | if (NATIVE_TO_UNI(*((U8*)l)) != |
bc517b45 | 2324 | utf8_to_uvchr((U8*)s, &ulen)) |
1aa99e6b | 2325 | sayNO; |
bc517b45 | 2326 | s += ulen; |
a72c7584 | 2327 | l ++; |
1aa99e6b | 2328 | } |
5ff6fc6d | 2329 | } |
1aa99e6b IH |
2330 | locinput = l; |
2331 | nextchr = UCHARAT(locinput); | |
2332 | break; | |
2333 | } | |
bc517b45 | 2334 | /* The target and the pattern have the same utf8ness. */ |
d6a28714 JH |
2335 | /* Inline the first character, for speed. */ |
2336 | if (UCHARAT(s) != nextchr) | |
2337 | sayNO; | |
2338 | if (PL_regeol - locinput < ln) | |
2339 | sayNO; | |
2340 | if (ln > 1 && memNE(s, locinput, ln)) | |
2341 | sayNO; | |
2342 | locinput += ln; | |
2343 | nextchr = UCHARAT(locinput); | |
2344 | break; | |
2345 | case EXACTFL: | |
b8c5462f JH |
2346 | PL_reg_flags |= RF_tainted; |
2347 | /* FALL THROUGH */ | |
d6a28714 | 2348 | case EXACTF: |
cd439c50 IZ |
2349 | s = STRING(scan); |
2350 | ln = STR_LEN(scan); | |
d6a28714 | 2351 | |
bc517b45 | 2352 | { |
d6a28714 | 2353 | char *l = locinput; |
bc517b45 | 2354 | char *e = s + ln; |
bc517b45 JH |
2355 | |
2356 | if (do_utf8 != (UTF!=0)) { | |
2357 | /* The target and the pattern have differing utf8ness. */ | |
2358 | STRLEN ulen1, ulen2; | |
2359 | UV cs, cl; | |
2360 | ||
2361 | if (do_utf8) { | |
2362 | /* The target is utf8, the pattern is not utf8. */ | |
2363 | while (s < e) { | |
2364 | if (l >= PL_regeol) | |
2365 | sayNO; | |
2366 | ||
2367 | cs = to_uni_fold(NATIVE_TO_UNI(*(U8*)s), | |
2368 | (U8*)s, &ulen1); | |
2369 | cl = utf8_to_uvchr((U8*)l, &ulen2); | |
2370 | ||
2371 | if (cs != cl) { | |
2372 | cl = to_uni_fold(cl, (U8*)l, &ulen2); | |
2373 | if (ulen1 != ulen2 || cs != cl) | |
2374 | sayNO; | |
2375 | } | |
2376 | l += ulen1; | |
2377 | s ++; | |
2378 | } | |
2379 | } | |
2380 | else { | |
2381 | /* The target is not utf8, the pattern is utf8. */ | |
2382 | while (s < e) { | |
2383 | if (l >= PL_regeol) | |
2384 | sayNO; | |
2385 | ||
2386 | cs = utf8_to_uvchr((U8*)s, &ulen1); | |
2387 | ||
2388 | cl = to_uni_fold(NATIVE_TO_UNI(*(U8*)l), | |
2389 | (U8*)l, &ulen2); | |
2390 | ||
2391 | if (cs != cl) { | |
2392 | cs = to_uni_fold(cs, (U8*)s, &ulen1); | |
2393 | if (ulen1 != ulen2 || cs != cl) | |
2394 | sayNO; | |
2395 | } | |
2396 | l ++; | |
2397 | s += ulen1; | |
2398 | } | |
2399 | } | |
2400 | locinput = l; | |
2401 | nextchr = UCHARAT(locinput); | |
2402 | break; | |
2403 | } | |
2404 | ||
2405 | if (do_utf8 && UTF) { | |
2406 | /* Both the target and the pattern are utf8. */ | |
c7254714 JH |
2407 | U8 lfoldbuf[UTF8_MAXLEN_FOLD+1], *lf; |
2408 | U8 sfoldbuf[UTF8_MAXLEN_FOLD+1], *sf; | |
2409 | STRLEN lfoldlen, sfoldlen; | |
2410 | STRLEN llen = 0; | |
2411 | STRLEN slen = 0; | |
80aecb99 | 2412 | |
c7254714 JH |
2413 | while (s < e) { |
2414 | /* Fold them and walk them characterwise. */ | |
60a8b682 | 2415 | |
c7254714 | 2416 | if (llen == 0) { |
80aecb99 | 2417 | to_utf8_fold((U8*)l, lfoldbuf, &lfoldlen); |
c7254714 JH |
2418 | lf = lfoldbuf; |
2419 | llen = lfoldlen; | |
2420 | } | |
2421 | ||
2422 | if (slen == 0) { | |
2423 | to_utf8_fold((U8*)s, sfoldbuf, &sfoldlen); | |
2424 | sf = sfoldbuf; | |
2425 | slen = sfoldlen; | |
2426 | } | |
2427 | ||
2428 | while (llen && slen) { | |
2429 | if (UTF8SKIP(lf) != UTF8SKIP(sf) || | |
2430 | memNE((char*)lf, (char*)sf, UTF8SKIP(lf))) | |
2431 | sayNO; | |
2432 | llen -= UTF8SKIP(lf); | |
2433 | lf += UTF8SKIP(lf); | |
2434 | slen -= UTF8SKIP(sf); | |
2435 | sf += UTF8SKIP(sf); | |
bc517b45 | 2436 | } |
c7254714 | 2437 | |
bc517b45 JH |
2438 | l += UTF8SKIP(l); |
2439 | s += UTF8SKIP(s); | |
2440 | } | |
2441 | locinput = l; | |
2442 | nextchr = UCHARAT(locinput); | |
2443 | break; | |
b8c5462f | 2444 | } |
a0ed51b3 | 2445 | } |
d6a28714 | 2446 | |
bc517b45 JH |
2447 | /* Neither the target and the pattern are utf8. */ |
2448 | ||
d6a28714 JH |
2449 | /* Inline the first character, for speed. */ |
2450 | if (UCHARAT(s) != nextchr && | |
2451 | UCHARAT(s) != ((OP(scan) == EXACTF) | |
2452 | ? PL_fold : PL_fold_locale)[nextchr]) | |
a0ed51b3 | 2453 | sayNO; |
d6a28714 | 2454 | if (PL_regeol - locinput < ln) |
b8c5462f | 2455 | sayNO; |
d6a28714 JH |
2456 | if (ln > 1 && (OP(scan) == EXACTF |
2457 | ? ibcmp(s, locinput, ln) | |
2458 | : ibcmp_locale(s, locinput, ln))) | |
4633a7c4 | 2459 | sayNO; |
d6a28714 JH |
2460 | locinput += ln; |
2461 | nextchr = UCHARAT(locinput); | |
a0d0e21e | 2462 | break; |
d6a28714 | 2463 | case ANYOF: |
ffc61ed2 JH |
2464 | if (do_utf8) { |
2465 | if (!reginclass(scan, (U8*)locinput, do_utf8)) | |
2466 | sayNO; | |
2467 | if (locinput >= PL_regeol) | |
2468 | sayNO; | |
2469 | locinput += PL_utf8skip[nextchr]; | |
b8c5462f | 2470 | nextchr = UCHARAT(locinput); |
ffc61ed2 JH |
2471 | } |
2472 | else { | |
2473 | if (nextchr < 0) | |
2474 | nextchr = UCHARAT(locinput); | |
2475 | if (!reginclass(scan, (U8*)locinput, do_utf8)) | |
2476 | sayNO; | |
2477 | if (!nextchr && locinput >= PL_regeol) | |
2478 | sayNO; | |
2479 | nextchr = UCHARAT(++locinput); | |
2480 | } | |
b8c5462f | 2481 | break; |
d6a28714 | 2482 | case ALNUML: |
b8c5462f JH |
2483 | PL_reg_flags |= RF_tainted; |
2484 | /* FALL THROUGH */ | |
d6a28714 | 2485 | case ALNUM: |
b8c5462f | 2486 | if (!nextchr) |
4633a7c4 | 2487 | sayNO; |
ffc61ed2 | 2488 | if (do_utf8) { |
ad24be35 | 2489 | LOAD_UTF8_CHARCLASS(alnum,"a"); |
ffc61ed2 | 2490 | if (!(OP(scan) == ALNUM |
3568d838 | 2491 | ? swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8) |
d6a28714 | 2492 | : isALNUM_LC_utf8((U8*)locinput))) |
b8c5462f JH |
2493 | { |
2494 | sayNO; | |
a0ed51b3 | 2495 | } |
b8c5462f | 2496 | locinput += PL_utf8skip[nextchr]; |
a0ed51b3 LW |
2497 | nextchr = UCHARAT(locinput); |
2498 | break; | |
2499 | } | |
ffc61ed2 | 2500 | if (!(OP(scan) == ALNUM |
d6a28714 | 2501 | ? isALNUM(nextchr) : isALNUM_LC(nextchr))) |
bbce6d69 | 2502 | sayNO; |
b8c5462f | 2503 | nextchr = UCHARAT(++locinput); |
a0d0e21e | 2504 | break; |
d6a28714 | 2505 | case NALNUML: |
b8c5462f JH |
2506 | PL_reg_flags |= RF_tainted; |
2507 | /* FALL THROUGH */ | |
d6a28714 JH |
2508 | case NALNUM: |
2509 | if (!nextchr && locinput >= PL_regeol) | |
a0ed51b3 | 2510 | sayNO; |
ffc61ed2 | 2511 | if (do_utf8) { |
8269fa76 | 2512 | LOAD_UTF8_CHARCLASS(alnum,"a"); |
ffc61ed2 | 2513 | if (OP(scan) == NALNUM |
3568d838 | 2514 | ? swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8) |
d6a28714 JH |
2515 | : isALNUM_LC_utf8((U8*)locinput)) |
2516 | { | |
b8c5462f | 2517 | sayNO; |
d6a28714 | 2518 | } |
b8c5462f JH |
2519 | locinput += PL_utf8skip[nextchr]; |
2520 | nextchr = UCHARAT(locinput); | |
2521 | break; | |
2522 | } | |
ffc61ed2 | 2523 | if (OP(scan) == NALNUM |
d6a28714 | 2524 | ? isALNUM(nextchr) : isALNUM_LC(nextchr)) |
b8c5462f | 2525 | sayNO; |
76e3520e | 2526 | nextchr = UCHARAT(++locinput); |
a0d0e21e | 2527 | break; |
d6a28714 JH |
2528 | case BOUNDL: |
2529 | case NBOUNDL: | |
3280af22 | 2530 | PL_reg_flags |= RF_tainted; |
bbce6d69 | 2531 | /* FALL THROUGH */ |
d6a28714 JH |
2532 | case BOUND: |
2533 | case NBOUND: | |
2534 | /* was last char in word? */ | |
ffc61ed2 | 2535 | if (do_utf8) { |
12d33761 HS |
2536 | if (locinput == PL_bostr) |
2537 | ln = '\n'; | |
ffc61ed2 JH |
2538 | else { |
2539 | U8 *r = reghop((U8*)locinput, -1); | |
9041c2e3 | 2540 | |
2b9d42f0 | 2541 | ln = utf8n_to_uvchr(r, s - (char*)r, 0, 0); |
ffc61ed2 JH |
2542 | } |
2543 | if (OP(scan) == BOUND || OP(scan) == NBOUND) { | |
2544 | ln = isALNUM_uni(ln); | |
8269fa76 | 2545 | LOAD_UTF8_CHARCLASS(alnum,"a"); |
3568d838 | 2546 | n = swash_fetch(PL_utf8_alnum, (U8*)locinput, do_utf8); |
ffc61ed2 JH |
2547 | } |
2548 | else { | |
9041c2e3 | 2549 | ln = isALNUM_LC_uvchr(UNI_TO_NATIVE(ln)); |
ffc61ed2 JH |
2550 | n = isALNUM_LC_utf8((U8*)locinput); |
2551 | } | |
a0ed51b3 | 2552 | } |
d6a28714 | 2553 | else { |
12d33761 HS |
2554 | ln = (locinput != PL_bostr) ? |
2555 | UCHARAT(locinput - 1) : '\n'; | |
ffc61ed2 JH |
2556 | if (OP(scan) == BOUND || OP(scan) == NBOUND) { |
2557 | ln = isALNUM(ln); | |
2558 | n = isALNUM(nextchr); | |
2559 | } | |
2560 | else { | |
2561 | ln = isALNUM_LC(ln); | |
2562 | n = isALNUM_LC(nextchr); | |
2563 | } | |
d6a28714 | 2564 | } |
ffc61ed2 JH |
2565 | if (((!ln) == (!n)) == (OP(scan) == BOUND || |
2566 | OP(scan) == BOUNDL)) | |
2567 | sayNO; | |
a0ed51b3 | 2568 | break; |
d6a28714 | 2569 | case SPACEL: |
3280af22 | 2570 | PL_reg_flags |= RF_tainted; |
bbce6d69 | 2571 | /* FALL THROUGH */ |
d6a28714 | 2572 | case SPACE: |
9442cb0e | 2573 | if (!nextchr) |
4633a7c4 | 2574 | sayNO; |
1aa99e6b | 2575 | if (do_utf8) { |
fd400ab9 | 2576 | if (UTF8_IS_CONTINUED(nextchr)) { |
8269fa76 | 2577 | LOAD_UTF8_CHARCLASS(space," "); |
ffc61ed2 | 2578 | if (!(OP(scan) == SPACE |
3568d838 | 2579 | ? swash_fetch(PL_utf8_space, (U8*)locinput, do_utf8) |
ffc61ed2 JH |
2580 | : isSPACE_LC_utf8((U8*)locinput))) |
2581 | { | |
2582 | sayNO; | |
2583 | } | |
2584 | locinput += PL_utf8skip[nextchr]; | |
2585 | nextchr = UCHARAT(locinput); | |
2586 | break; | |
d6a28714 | 2587 | } |
ffc61ed2 JH |
2588 | if (!(OP(scan) == SPACE |
2589 | ? isSPACE(nextchr) : isSPACE_LC(nextchr))) | |
2590 | sayNO; | |
2591 | nextchr = UCHARAT(++locinput); | |
2592 | } | |
2593 | else { | |
2594 | if (!(OP(scan) == SPACE | |
2595 | ? isSPACE(nextchr) : isSPACE_LC(nextchr))) | |
2596 | sayNO; | |
2597 | nextchr = UCHARAT(++locinput); | |
a0ed51b3 | 2598 | } |
a0ed51b3 | 2599 | break; |
d6a28714 | 2600 | case NSPACEL: |
3280af22 | 2601 | PL_reg_flags |= RF_tainted; |
bbce6d69 | 2602 | /* FALL THROUGH */ |
d6a28714 | 2603 | case NSPACE: |
9442cb0e | 2604 | if (!nextchr && locinput >= PL_regeol) |
b8c5462f | 2605 | sayNO; |
1aa99e6b | 2606 | if (do_utf8) { |
8269fa76 | 2607 | LOAD_UTF8_CHARCLASS(space," "); |
ffc61ed2 | 2608 | if (OP(scan) == NSPACE |
3568d838 | 2609 | ? swash_fetch(PL_utf8_space, (U8*)locinput, do_utf8) |
d6a28714 | 2610 | : isSPACE_LC_utf8((U8*)locinput)) |
b8c5462f JH |
2611 | { |
2612 | sayNO; | |
2613 | } | |
2614 | locinput += PL_utf8skip[nextchr]; | |
2615 | nextchr = UCHARAT(locinput); | |
2616 | break; | |
a0ed51b3 | 2617 | } |
ffc61ed2 | 2618 | if (OP(scan) == NSPACE |
d6a28714 | 2619 | ? isSPACE(nextchr) : isSPACE_LC(nextchr)) |
4633a7c4 | 2620 | sayNO; |
76e3520e | 2621 | nextchr = UCHARAT(++locinput); |
a0d0e21e | 2622 | break; |
d6a28714 | 2623 | case DIGITL: |
a0ed51b3 LW |
2624 | PL_reg_flags |= RF_tainted; |
2625 | /* FALL THROUGH */ | |
d6a28714 | 2626 | case DIGIT: |
9442cb0e | 2627 | if (!nextchr) |
a0ed51b3 | 2628 | sayNO; |
1aa99e6b | 2629 | if (do_utf8) { |
8269fa76 | 2630 | LOAD_UTF8_CHARCLASS(digit,"0"); |
ffc61ed2 | 2631 | if (!(OP(scan) == DIGIT |
3568d838 | 2632 | ? swash_fetch(PL_utf8_digit, (U8*)locinput, do_utf8) |
9442cb0e | 2633 | : isDIGIT_LC_utf8((U8*)locinput))) |
dfe13c55 | 2634 | { |
a0ed51b3 | 2635 | sayNO; |
dfe13c55 | 2636 | } |
6f06b55f | 2637 | locinput += PL_utf8skip[nextchr]; |
a0ed51b3 LW |
2638 | nextchr = UCHARAT(locinput); |
2639 | break; | |
2640 | } | |
ffc61ed2 | 2641 | if (!(OP(scan) == DIGIT |
9442cb0e | 2642 | ? isDIGIT(nextchr) : isDIGIT_LC(nextchr))) |
a0ed51b3 LW |
2643 | sayNO; |
2644 | nextchr = UCHARAT(++locinput); | |
2645 | break; | |
d6a28714 | 2646 | case NDIGITL: |
b8c5462f JH |
2647 | PL_reg_flags |= RF_tainted; |
2648 | /* FALL THROUGH */ | |
d6a28714 | 2649 | case NDIGIT: |
9442cb0e | 2650 | if (!nextchr && locinput >= PL_regeol) |
b8c5462f | 2651 | sayNO; |
1aa99e6b | 2652 | if (do_utf8) { |
8269fa76 | 2653 | LOAD_UTF8_CHARCLASS(digit,"0"); |
ffc61ed2 | 2654 | if (OP(scan) == NDIGIT |
3568d838 | 2655 | ? swash_fetch(PL_utf8_digit, (U8*)locinput, do_utf8) |
9442cb0e GS |
2656 | : isDIGIT_LC_utf8((U8*)locinput)) |
2657 | { | |
a0ed51b3 | 2658 | sayNO; |
9442cb0e | 2659 | } |
6f06b55f | 2660 | locinput += PL_utf8skip[nextchr]; |
a0ed51b3 LW |
2661 | nextchr = UCHARAT(locinput); |
2662 | break; | |
2663 | } | |
ffc61ed2 | 2664 | if (OP(scan) == NDIGIT |
9442cb0e | 2665 | ? isDIGIT(nextchr) : isDIGIT_LC(nextchr)) |
a0ed51b3 LW |
2666 | sayNO; |
2667 | nextchr = UCHARAT(++locinput); | |
2668 | break; | |
2669 | case CLUMP: | |
b7c83a7e | 2670 | if (locinput >= PL_regeol) |
a0ed51b3 | 2671 | sayNO; |
b7c83a7e JH |
2672 | if (do_utf8) { |
2673 | LOAD_UTF8_CHARCLASS(mark,"~"); | |
2674 | if (swash_fetch(PL_utf8_mark,(U8*)locinput, do_utf8)) | |
2675 | sayNO; | |
2676 | locinput += PL_utf8skip[nextchr]; | |
2677 | while (locinput < PL_regeol && | |
2678 | swash_fetch(PL_utf8_mark,(U8*)locinput, do_utf8)) | |
2679 | locinput += UTF8SKIP(locinput); | |
2680 | if (locinput > PL_regeol) | |
2681 | sayNO; | |
eb08e2da JH |
2682 | } |
2683 | else | |
2684 | locinput++; | |
a0ed51b3 LW |
2685 | nextchr = UCHARAT(locinput); |
2686 | break; | |
c8756f30 | 2687 | case REFFL: |
3280af22 | 2688 | PL_reg_flags |= RF_tainted; |
c8756f30 | 2689 | /* FALL THROUGH */ |
c277df42 | 2690 | case REF: |
c8756f30 | 2691 | case REFF: |
c277df42 | 2692 | n = ARG(scan); /* which paren pair */ |
cf93c79d | 2693 | ln = PL_regstartp[n]; |
2c2d71f5 | 2694 | PL_reg_leftiter = PL_reg_maxiter; /* Void cache */ |
cf93c79d | 2695 | if (*PL_reglastparen < n || ln == -1) |
af3f8c16 | 2696 | sayNO; /* Do not match unless seen CLOSEn. */ |
cf93c79d | 2697 | if (ln == PL_regendp[n]) |
a0d0e21e | 2698 | break; |
a0ed51b3 | 2699 | |
cf93c79d | 2700 | s = PL_bostr + ln; |
1aa99e6b | 2701 | if (do_utf8 && OP(scan) != REF) { /* REF can do byte comparison */ |
a0ed51b3 | 2702 | char *l = locinput; |
cf93c79d | 2703 | char *e = PL_bostr + PL_regendp[n]; |
a0ed51b3 LW |
2704 | /* |
2705 | * Note that we can't do the "other character" lookup trick as | |
2706 | * in the 8-bit case (no pun intended) because in Unicode we | |
2707 | * have to map both upper and title case to lower case. | |
2708 | */ | |
2709 | if (OP(scan) == REFF) { | |
a2a2844f | 2710 | STRLEN ulen1, ulen2; |
e7ae6809 JH |
2711 | U8 tmpbuf1[UTF8_MAXLEN_UCLC+1]; |
2712 | U8 tmpbuf2[UTF8_MAXLEN_UCLC+1]; | |
a0ed51b3 LW |
2713 | while (s < e) { |
2714 | if (l >= PL_regeol) | |
2715 | sayNO; | |
a2a2844f JH |
2716 | toLOWER_utf8((U8*)s, tmpbuf1, &ulen1); |
2717 | toLOWER_utf8((U8*)l, tmpbuf2, &ulen2); | |
7114a2d2 | 2718 | if (ulen1 != ulen2 || memNE((char *)tmpbuf1, (char *)tmpbuf2, ulen1)) |
a0ed51b3 | 2719 | sayNO; |
a2a2844f JH |
2720 | s += ulen1; |
2721 | l += ulen2; | |
a0ed51b3 LW |
2722 | } |
2723 | } | |
2724 | locinput = l; | |
2725 | nextchr = UCHARAT(locinput); | |
2726 | break; | |
2727 | } | |
2728 | ||
a0d0e21e | 2729 | /* Inline the first character, for speed. */ |
76e3520e | 2730 | if (UCHARAT(s) != nextchr && |
c8756f30 AK |
2731 | (OP(scan) == REF || |
2732 | (UCHARAT(s) != ((OP(scan) == REFF | |
22c35a8c | 2733 | ? PL_fold : PL_fold_locale)[nextchr])))) |
4633a7c4 | 2734 | sayNO; |
cf93c79d | 2735 | ln = PL_regendp[n] - ln; |
3280af22 | 2736 | if (locinput + ln > PL_regeol) |
4633a7c4 | 2737 | sayNO; |
c8756f30 AK |
2738 | if (ln > 1 && (OP(scan) == REF |
2739 | ? memNE(s, locinput, ln) | |
2740 | : (OP(scan) == REFF | |
2741 | ? ibcmp(s, locinput, ln) | |
2742 | : ibcmp_locale(s, locinput, ln)))) | |
4633a7c4 | 2743 | sayNO; |
a0d0e21e | 2744 | locinput += ln; |
76e3520e | 2745 | nextchr = UCHARAT(locinput); |
a0d0e21e LW |
2746 | break; |
2747 | ||
2748 | case NOTHING: | |
c277df42 | 2749 | case TAIL: |
a0d0e21e LW |
2750 | break; |
2751 | case BACK: | |
2752 | break; | |
c277df42 IZ |
2753 | case EVAL: |
2754 | { | |
2755 | dSP; | |
533c011a | 2756 | OP_4tree *oop = PL_op; |
3280af22 NIS |
2757 | COP *ocurcop = PL_curcop; |
2758 | SV **ocurpad = PL_curpad; | |
c277df42 | 2759 | SV *ret; |
9041c2e3 | 2760 | |
c277df42 | 2761 | n = ARG(scan); |
533c011a | 2762 | PL_op = (OP_4tree*)PL_regdata->data[n]; |
d7d93a81 | 2763 | DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%"UVxf"\n", PTR2UV(PL_op)) ); |
dfad63ad | 2764 | PL_curpad = AvARRAY((AV*)PL_regdata->data[n + 2]); |
cf93c79d | 2765 | PL_regendp[0] = PL_reg_magic->mg_len = locinput - PL_bostr; |
c277df42 | 2766 | |
8e5e9ebe RGS |
2767 | { |
2768 | SV **before = SP; | |
2769 | CALLRUNOPS(aTHX); /* Scalar context. */ | |
2770 | SPAGAIN; | |
2771 | if (SP == before) | |
2772 | ret = Nullsv; /* protect against empty (?{}) blocks. */ | |
2773 | else { | |
2774 | ret = POPs; | |
2775 | PUTBACK; | |
2776 | } | |
2777 | } | |
2778 | ||
0f5d15d6 IZ |
2779 | PL_op = oop; |
2780 | PL_curpad = ocurpad; | |
2781 | PL_curcop = ocurcop; | |
c277df42 | 2782 | if (logical) { |
0f5d15d6 IZ |
2783 | if (logical == 2) { /* Postponed subexpression. */ |
2784 | regexp *re; | |
22c35a8c | 2785 | MAGIC *mg = Null(MAGIC*); |
0f5d15d6 | 2786 | re_cc_state state; |
0f5d15d6 IZ |
2787 | CHECKPOINT cp, lastcp; |
2788 | ||
2789 | if(SvROK(ret) || SvRMAGICAL(ret)) { | |
2790 | SV *sv = SvROK(ret) ? SvRV(ret) : ret; | |
2791 | ||
2792 | if(SvMAGICAL(sv)) | |
14befaf4 | 2793 | mg = mg_find(sv, PERL_MAGIC_qr); |
0f5d15d6 IZ |
2794 | } |
2795 | if (mg) { | |
2796 | re = (regexp *)mg->mg_obj; | |
df0003d4 | 2797 | (void)ReREFCNT_inc(re); |
0f5d15d6 IZ |
2798 | } |
2799 | else { | |
2800 | STRLEN len; | |
2801 | char *t = SvPV(ret, len); | |
2802 | PMOP pm; | |
2803 | char *oprecomp = PL_regprecomp; | |
2804 | I32 osize = PL_regsize; | |
2805 | I32 onpar = PL_regnpar; | |
2806 | ||
5fcd1c1b | 2807 | Zero(&pm, 1, PMOP); |
cea2e8a9 | 2808 | re = CALLREGCOMP(aTHX_ t, t + len, &pm); |
9041c2e3 | 2809 | if (!(SvFLAGS(ret) |
0f5d15d6 | 2810 | & (SVs_TEMP | SVs_PADTMP | SVf_READONLY))) |
14befaf4 DM |
2811 | sv_magic(ret,(SV*)ReREFCNT_inc(re), |
2812 | PERL_MAGIC_qr,0,0); | |
0f5d15d6 IZ |
2813 | PL_regprecomp = oprecomp; |
2814 | PL_regsize = osize; | |
2815 | PL_regnpar = onpar; | |
2816 | } | |
2817 | DEBUG_r( | |
9041c2e3 | 2818 | PerlIO_printf(Perl_debug_log, |
0f5d15d6 IZ |
2819 | "Entering embedded `%s%.60s%s%s'\n", |
2820 | PL_colors[0], | |
2821 | re->precomp, | |
2822 | PL_colors[1], | |
2823 | (strlen(re->precomp) > 60 ? "..." : "")) | |
2824 | ); | |
2825 | state.node = next; | |
2826 | state.prev = PL_reg_call_cc; | |
2827 | state.cc = PL_regcc; | |
2828 | state.re = PL_reg_re; | |
2829 | ||
2ab05381 | 2830 | PL_regcc = 0; |
9041c2e3 | 2831 | |
0f5d15d6 | 2832 | cp = regcppush(0); /* Save *all* the positions. */ |
02db2b7b | 2833 | REGCP_SET(lastcp); |
0f5d15d6 IZ |
2834 | cache_re(re); |
2835 | state.ss = PL_savestack_ix; | |
2836 | *PL_reglastparen = 0; | |
a01268b5 | 2837 | *PL_reglastcloseparen = 0; |
0f5d15d6 IZ |
2838 | PL_reg_call_cc = &state; |
2839 | PL_reginput = locinput; | |
2c2d71f5 JH |
2840 | |
2841 | /* XXXX This is too dramatic a measure... */ | |
2842 | PL_reg_maxiter = 0; | |
2843 | ||
0f5d15d6 | 2844 | if (regmatch(re->program + 1)) { |
2c914db6 IZ |
2845 | /* Even though we succeeded, we need to restore |
2846 | global variables, since we may be wrapped inside | |
2847 | SUSPEND, thus the match may be not finished yet. */ | |
2848 | ||
2849 | /* XXXX Do this only if SUSPENDed? */ | |
2850 | PL_reg_call_cc = state.prev; | |
2851 | PL_regcc = state.cc; | |
2852 | PL_reg_re = state.re; | |
2853 | cache_re(PL_reg_re); | |
2854 | ||
2855 | /* XXXX This is too dramatic a measure... */ | |
2856 | PL_reg_maxiter = 0; | |
2857 | ||
2858 | /* These are needed even if not SUSPEND. */ | |
0f5d15d6 IZ |
2859 | ReREFCNT_dec(re); |
2860 | regcpblow(cp); | |
2861 | sayYES; | |
2862 | } | |
0f5d15d6 | 2863 | ReREFCNT_dec(re); |
02db2b7b | 2864 | REGCP_UNWIND(lastcp); |
0f5d15d6 IZ |
2865 | regcppop(); |
2866 | PL_reg_call_cc = state.prev; | |
2867 | PL_regcc = state.cc; | |
2868 | PL_reg_re = state.re; | |
d3790889 | 2869 | cache_re(PL_reg_re); |
2c2d71f5 JH |
2870 | |
2871 | /* XXXX This is too dramatic a measure... */ | |
2872 | PL_reg_maxiter = 0; | |
2873 | ||
8e514ae6 | 2874 | logical = 0; |
0f5d15d6 IZ |
2875 | sayNO; |
2876 | } | |
c277df42 | 2877 | sw = SvTRUE(ret); |
0f5d15d6 | 2878 | logical = 0; |
a0ed51b3 LW |
2879 | } |
2880 | else | |
3280af22 | 2881 | sv_setsv(save_scalar(PL_replgv), ret); |
c277df42 IZ |
2882 | break; |
2883 | } | |
a0d0e21e | 2884 | case OPEN: |
c277df42 | 2885 | n = ARG(scan); /* which paren pair */ |
3280af22 NIS |
2886 | PL_reg_start_tmp[n] = locinput; |
2887 | if (n > PL_regsize) | |
2888 | PL_regsize = n; | |
a0d0e21e LW |
2889 | break; |
2890 | case CLOSE: | |
c277df42 | 2891 | n = ARG(scan); /* which paren pair */ |
cf93c79d IZ |
2892 | PL_regstartp[n] = PL_reg_start_tmp[n] - PL_bostr; |
2893 | PL_regendp[n] = locinput - PL_bostr; | |
3280af22 NIS |
2894 | if (n > *PL_reglastparen) |
2895 | *PL_reglastparen = n; | |
a01268b5 | 2896 | *PL_reglastcloseparen = n; |
a0d0e21e | 2897 | break; |
c277df42 IZ |
2898 | case GROUPP: |
2899 | n = ARG(scan); /* which paren pair */ | |
cf93c79d | 2900 | sw = (*PL_reglastparen >= n && PL_regendp[n] != -1); |
c277df42 IZ |
2901 | break; |
2902 | case IFTHEN: | |
2c2d71f5 | 2903 | PL_reg_leftiter = PL_reg_maxiter; /* Void cache */ |
c277df42 IZ |
2904 | if (sw) |
2905 | next = NEXTOPER(NEXTOPER(scan)); | |
2906 | else { | |
2907 | next = scan + ARG(scan); | |
2908 | if (OP(next) == IFTHEN) /* Fake one. */ | |
2909 | next = NEXTOPER(NEXTOPER(next)); | |
2910 | } | |
2911 | break; | |
2912 | case LOGICAL: | |
0f5d15d6 | 2913 | logical = scan->flags; |
c277df42 | 2914 | break; |
2ab05381 IZ |
2915 | /******************************************************************* |
2916 | PL_regcc contains infoblock about the innermost (...)* loop, and | |
2917 | a pointer to the next outer infoblock. | |
2918 | ||
2919 | Here is how Y(A)*Z is processed (if it is compiled into CURLYX/WHILEM): | |
2920 | ||
2921 | 1) After matching X, regnode for CURLYX is processed; | |
2922 | ||
9041c2e3 | 2923 | 2) This regnode creates infoblock on the stack, and calls |
2ab05381 IZ |
2924 | regmatch() recursively with the starting point at WHILEM node; |
2925 | ||
2926 | 3) Each hit of WHILEM node tries to match A and Z (in the order | |
2927 | depending on the current iteration, min/max of {min,max} and | |
2928 | greediness). The information about where are nodes for "A" | |
2929 | and "Z" is read from the infoblock, as is info on how many times "A" | |
2930 | was already matched, and greediness. | |
2931 | ||
2932 | 4) After A matches, the same WHILEM node is hit again. | |
2933 | ||
2934 | 5) Each time WHILEM is hit, PL_regcc is the infoblock created by CURLYX | |
2935 | of the same pair. Thus when WHILEM tries to match Z, it temporarily | |
2936 | resets PL_regcc, since this Y(A)*Z can be a part of some other loop: | |
2937 | as in (Y(A)*Z)*. If Z matches, the automaton will hit the WHILEM node | |
2938 | of the external loop. | |
2939 | ||
2940 | Currently present infoblocks form a tree with a stem formed by PL_curcc | |
2941 | and whatever it mentions via ->next, and additional attached trees | |
2942 | corresponding to temporarily unset infoblocks as in "5" above. | |
2943 | ||
9041c2e3 | 2944 | In the following picture infoblocks for outer loop of |
2ab05381 IZ |
2945 | (Y(A)*?Z)*?T are denoted O, for inner I. NULL starting block |
2946 | is denoted by x. The matched string is YAAZYAZT. Temporarily postponed | |
2947 | infoblocks are drawn below the "reset" infoblock. | |
2948 | ||
2949 | In fact in the picture below we do not show failed matches for Z and T | |
2950 | by WHILEM blocks. [We illustrate minimal matches, since for them it is | |
2951 | more obvious *why* one needs to *temporary* unset infoblocks.] | |
2952 | ||
2953 | Matched REx position InfoBlocks Comment | |
2954 | (Y(A)*?Z)*?T x | |
2955 | Y(A)*?Z)*?T x <- O | |
2956 | Y (A)*?Z)*?T x <- O | |
2957 | Y A)*?Z)*?T x <- O <- I | |
2958 | YA )*?Z)*?T x <- O <- I | |
2959 | YA A)*?Z)*?T x <- O <- I | |
2960 | YAA )*?Z)*?T x <- O <- I | |
2961 | YAA Z)*?T x <- O # Temporary unset I | |
2962 | I | |
2963 | ||
2964 | YAAZ Y(A)*?Z)*?T x <- O | |
2965 | I | |
2966 | ||
2967 | YAAZY (A)*?Z)*?T x <- O | |
2968 | I | |
2969 | ||
2970 | YAAZY A)*?Z)*?T x <- O <- I | |
2971 | I | |
2972 | ||
2973 | YAAZYA )*?Z)*?T x <- O <- I | |
2974 | I | |
2975 | ||
2976 | YAAZYA Z)*?T x <- O # Temporary unset I | |
2977 | I,I | |
2978 | ||
2979 | YAAZYAZ )*?T x <- O | |
2980 | I,I | |
2981 | ||
2982 | YAAZYAZ T x # Temporary unset O | |
2983 | O | |
2984 | I,I | |
2985 | ||
2986 | YAAZYAZT x | |
2987 | O | |
2988 | I,I | |
2989 | *******************************************************************/ | |
a0d0e21e LW |
2990 | case CURLYX: { |
2991 | CURCUR cc; | |
3280af22 | 2992 | CHECKPOINT cp = PL_savestack_ix; |
cb434fcc IZ |
2993 | /* No need to save/restore up to this paren */ |
2994 | I32 parenfloor = scan->flags; | |
c277df42 IZ |
2995 | |
2996 | if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */ | |
2997 | next += ARG(next); | |
3280af22 NIS |
2998 | cc.oldcc = PL_regcc; |
2999 | PL_regcc = &cc; | |
cb434fcc IZ |
3000 | /* XXXX Probably it is better to teach regpush to support |
3001 | parenfloor > PL_regsize... */ | |
3002 | if (parenfloor > *PL_reglastparen) | |
3003 | parenfloor = *PL_reglastparen; /* Pessimization... */ | |
3004 | cc.parenfloor = parenfloor; | |
a0d0e21e LW |
3005 | cc.cur = -1; |
3006 | cc.min = ARG1(scan); | |
3007 | cc.max = ARG2(scan); | |
c277df42 | 3008 | cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS; |
a0d0e21e LW |
3009 | cc.next = next; |
3010 | cc.minmod = minmod; | |
3011 | cc.lastloc = 0; | |
3280af22 | 3012 | PL_reginput = locinput; |
a0d0e21e LW |
3013 | n = regmatch(PREVOPER(next)); /* start on the WHILEM */ |
3014 | regcpblow(cp); | |
3280af22 | 3015 | PL_regcc = cc.oldcc; |
4633a7c4 | 3016 | saySAME(n); |
a0d0e21e LW |
3017 | } |
3018 | /* NOT REACHED */ | |
3019 | case WHILEM: { | |
3020 | /* | |
3021 | * This is really hard to understand, because after we match | |
3022 | * what we're trying to match, we must make sure the rest of | |
2c2d71f5 | 3023 | * the REx is going to match for sure, and to do that we have |
a0d0e21e LW |
3024 | * to go back UP the parse tree by recursing ever deeper. And |
3025 | * if it fails, we have to reset our parent's current state | |
3026 | * that we can try again after backing off. | |
3027 | */ | |
3028 | ||
c277df42 | 3029 | CHECKPOINT cp, lastcp; |
3280af22 | 3030 | CURCUR* cc = PL_regcc; |
c277df42 IZ |
3031 | char *lastloc = cc->lastloc; /* Detection of 0-len. */ |
3032 | ||
4633a7c4 | 3033 | n = cc->cur + 1; /* how many we know we matched */ |
3280af22 | 3034 | PL_reginput = locinput; |
a0d0e21e | 3035 | |
c277df42 | 3036 | DEBUG_r( |
9041c2e3 NIS |
3037 | PerlIO_printf(Perl_debug_log, |
3038 | "%*s %ld out of %ld..%ld cc=%lx\n", | |
3280af22 | 3039 | REPORT_CODE_OFF+PL_regindent*2, "", |
9041c2e3 | 3040 | (long)n, (long)cc->min, |
c277df42 IZ |
3041 | (long)cc->max, (long)cc) |
3042 | ); | |
4633a7c4 | 3043 | |
a0d0e21e LW |
3044 | /* If degenerate scan matches "", assume scan done. */ |
3045 | ||
579cf2c3 | 3046 | if (locinput == cc->lastloc && n >= cc->min) { |
3280af22 | 3047 | PL_regcc = cc->oldcc; |
2ab05381 IZ |
3048 | if (PL_regcc) |
3049 | ln = PL_regcc->cur; | |
c277df42 | 3050 | DEBUG_r( |
c3464db5 DD |
3051 | PerlIO_printf(Perl_debug_log, |
3052 | "%*s empty match detected, try continuation...\n", | |
3280af22 | 3053 | REPORT_CODE_OFF+PL_regindent*2, "") |
c277df42 | 3054 | ); |
a0d0e21e | 3055 | if (regmatch(cc->next)) |
4633a7c4 | 3056 | sayYES; |
2ab05381 IZ |
3057 | if (PL_regcc) |
3058 | PL_regcc->cur = ln; | |
3280af22 | 3059 | PL_regcc = cc; |
4633a7c4 | 3060 | sayNO; |
a0d0e21e LW |
3061 | } |
3062 | ||
3063 | /* First just match a string of min scans. */ | |
3064 | ||
3065 | if (n < cc->min) { | |
3066 | cc->cur = n; | |
3067 | cc->lastloc = locinput; | |
4633a7c4 LW |
3068 | if (regmatch(cc->scan)) |
3069 | sayYES; | |
3070 | cc->cur = n - 1; | |
c277df42 | 3071 | cc->lastloc = lastloc; |
4633a7c4 | 3072 | sayNO; |
a0d0e21e LW |
3073 | } |
3074 | ||
2c2d71f5 JH |
3075 | if (scan->flags) { |
3076 | /* Check whether we already were at this position. | |
3077 | Postpone detection until we know the match is not | |
3078 | *that* much linear. */ | |
3079 | if (!PL_reg_maxiter) { | |
3080 | PL_reg_maxiter = (PL_regeol - PL_bostr + 1) * (scan->flags>>4); | |
3081 | PL_reg_leftiter = PL_reg_maxiter; | |
3082 | } | |
3083 | if (PL_reg_leftiter-- == 0) { | |
3084 | I32 size = (PL_reg_maxiter + 7)/8; | |
3085 | if (PL_reg_poscache) { | |
3086 | if (PL_reg_poscache_size < size) { | |
3087 | Renew(PL_reg_poscache, size, char); | |
3088 | PL_reg_poscache_size = size; | |
3089 | } | |
3090 | Zero(PL_reg_poscache, size, char); | |
3091 | } | |
3092 | else { | |
3093 | PL_reg_poscache_size = size; | |
3094 | Newz(29, PL_reg_poscache, size, char); | |
3095 | } | |
3096 | DEBUG_r( | |
3097 | PerlIO_printf(Perl_debug_log, | |
3098 | "%sDetected a super-linear match, switching on caching%s...\n", | |
3099 | PL_colors[4], PL_colors[5]) | |
3100 | ); | |
3101 | } | |
3102 | if (PL_reg_leftiter < 0) { | |
3103 | I32 o = locinput - PL_bostr, b; | |
3104 | ||
3105 | o = (scan->flags & 0xf) - 1 + o * (scan->flags>>4); | |
3106 | b = o % 8; | |
3107 | o /= 8; | |
3108 | if (PL_reg_poscache[o] & (1<<b)) { | |
3109 | DEBUG_r( | |
3110 | PerlIO_printf(Perl_debug_log, | |
3111 | "%*s already tried at this position...\n", | |
3112 | REPORT_CODE_OFF+PL_regindent*2, "") | |
3113 | ); | |
7821416a | 3114 | sayNO_SILENT; |
2c2d71f5 JH |
3115 | } |
3116 | PL_reg_poscache[o] |= (1<<b); | |
3117 | } | |
3118 | } | |
3119 | ||
a0d0e21e LW |
3120 | /* Prefer next over scan for minimal matching. */ |
3121 | ||
3122 | if (cc->minmod) { | |
3280af22 | 3123 | PL_regcc = cc->oldcc; |
2ab05381 IZ |
3124 | if (PL_regcc) |
3125 | ln = PL_regcc->cur; | |
5f05dabc | 3126 | cp = regcppush(cc->parenfloor); |
02db2b7b | 3127 | REGCP_SET(lastcp); |
5f05dabc | 3128 | if (regmatch(cc->next)) { |
c277df42 | 3129 | regcpblow(cp); |
4633a7c4 | 3130 | sayYES; /* All done. */ |
5f05dabc | 3131 | } |
02db2b7b | 3132 | REGCP_UNWIND(lastcp); |
5f05dabc | 3133 | regcppop(); |
2ab05381 IZ |
3134 | if (PL_regcc) |
3135 | PL_regcc->cur = ln; | |
3280af22 | 3136 | PL_regcc = cc; |
a0d0e21e | 3137 | |
c277df42 | 3138 | if (n >= cc->max) { /* Maximum greed exceeded? */ |
9041c2e3 | 3139 | if (ckWARN(WARN_REGEXP) && n >= REG_INFTY |
3280af22 NIS |
3140 | && !(PL_reg_flags & RF_warned)) { |
3141 | PL_reg_flags |= RF_warned; | |
e476b1b5 | 3142 | Perl_warner(aTHX_ WARN_REGEXP, "%s limit (%d) exceeded", |
2f3ca594 GS |
3143 | "Complex regular subexpression recursion", |
3144 | REG_INFTY - 1); | |
c277df42 | 3145 | } |
4633a7c4 | 3146 | sayNO; |
c277df42 | 3147 | } |
a687059c | 3148 | |
c277df42 | 3149 | DEBUG_r( |
c3464db5 DD |
3150 | PerlIO_printf(Perl_debug_log, |
3151 | "%*s trying longer...\n", | |
3280af22 | 3152 | REPORT_CODE_OFF+PL_regindent*2, "") |
c277df42 | 3153 | ); |
a0d0e21e | 3154 | /* Try scanning more and see if it helps. */ |
3280af22 | 3155 | PL_reginput = locinput; |
a0d0e21e LW |
3156 | cc->cur = n; |
3157 | cc->lastloc = locinput; | |
5f05dabc | 3158 | cp = regcppush(cc->parenfloor); |
02db2b7b | 3159 | REGCP_SET(lastcp); |
5f05dabc | 3160 | if (regmatch(cc->scan)) { |
c277df42 | 3161 | regcpblow(cp); |
4633a7c4 | 3162 | sayYES; |
5f05dabc | 3163 | } |
02db2b7b | 3164 | REGCP_UNWIND(lastcp); |
5f05dabc | 3165 | regcppop(); |
4633a7c4 | 3166 | cc->cur = n - 1; |
c277df42 | 3167 | cc->lastloc = lastloc; |
4633a7c4 | 3168 | sayNO; |
a0d0e21e LW |
3169 | } |
3170 | ||
3171 | /* Prefer scan over next for maximal matching. */ | |
3172 | ||
3173 | if (n < cc->max) { /* More greed allowed? */ | |
5f05dabc | 3174 | cp = regcppush(cc->parenfloor); |
a0d0e21e LW |
3175 | cc->cur = n; |
3176 | cc->lastloc = locinput; | |
02db2b7b | 3177 | REGCP_SET(lastcp); |
5f05dabc | 3178 | if (regmatch(cc->scan)) { |
c277df42 | 3179 | regcpblow(cp); |
4633a7c4 | 3180 | sayYES; |
5f05dabc | 3181 | } |
02db2b7b | 3182 | REGCP_UNWIND(lastcp); |
a0d0e21e | 3183 | regcppop(); /* Restore some previous $<digit>s? */ |
3280af22 | 3184 | PL_reginput = locinput; |
c277df42 | 3185 | DEBUG_r( |
c3464db5 DD |
3186 | PerlIO_printf(Perl_debug_log, |
3187 | "%*s failed, try continuation...\n", | |
3280af22 | 3188 | REPORT_CODE_OFF+PL_regindent*2, "") |
c277df42 IZ |
3189 | ); |
3190 | } | |
9041c2e3 | 3191 | if (ckWARN(WARN_REGEXP) && n >= REG_INFTY |
599cee73 | 3192 | && !(PL_reg_flags & RF_warned)) { |
3280af22 | 3193 | PL_reg_flags |= RF_warned; |
e476b1b5 | 3194 | Perl_warner(aTHX_ WARN_REGEXP, "%s limit (%d) exceeded", |
cb5d145d GS |
3195 | "Complex regular subexpression recursion", |
3196 | REG_INFTY - 1); | |
a0d0e21e LW |
3197 | } |
3198 | ||
3199 | /* Failed deeper matches of scan, so see if this one works. */ | |
3280af22 | 3200 | PL_regcc = cc->oldcc; |
2ab05381 IZ |
3201 | if (PL_regcc) |
3202 | ln = PL_regcc->cur; | |
a0d0e21e | 3203 | if (regmatch(cc->next)) |
4633a7c4 | 3204 | sayYES; |
2ab05381 IZ |
3205 | if (PL_regcc) |
3206 | PL_regcc->cur = ln; | |
3280af22 | 3207 | PL_regcc = cc; |
4633a7c4 | 3208 | cc->cur = n - 1; |
c277df42 | 3209 | cc->lastloc = lastloc; |
4633a7c4 | 3210 | sayNO; |
a0d0e21e LW |
3211 | } |
3212 | /* NOT REACHED */ | |
9041c2e3 | 3213 | case BRANCHJ: |
c277df42 IZ |
3214 | next = scan + ARG(scan); |
3215 | if (next == scan) | |
3216 | next = NULL; | |
3217 | inner = NEXTOPER(NEXTOPER(scan)); | |
3218 | goto do_branch; | |
9041c2e3 | 3219 | case BRANCH: |
c277df42 IZ |
3220 | inner = NEXTOPER(scan); |
3221 | do_branch: | |
3222 | { | |
c277df42 IZ |
3223 | c1 = OP(scan); |
3224 | if (OP(next) != c1) /* No choice. */ | |
3225 | next = inner; /* Avoid recursion. */ | |
a0d0e21e | 3226 | else { |
02db2b7b IZ |
3227 | I32 lastparen = *PL_reglastparen; |
3228 | I32 unwind1; | |
3229 | re_unwind_branch_t *uw; | |
3230 | ||
3231 | /* Put unwinding data on stack */ | |
3232 | unwind1 = SSNEWt(1,re_unwind_branch_t); | |
3233 | uw = SSPTRt(unwind1,re_unwind_branch_t); | |
3234 | uw->prev = unwind; | |
3235 | unwind = unwind1; | |
3236 | uw->type = ((c1 == BRANCH) | |
3237 | ? RE_UNWIND_BRANCH | |
3238 | : RE_UNWIND_BRANCHJ); | |
3239 | uw->lastparen = lastparen; | |
3240 | uw->next = next; | |
3241 | uw->locinput = locinput; | |
3242 | uw->nextchr = nextchr; | |
3243 | #ifdef DEBUGGING | |
3244 | uw->regindent = ++PL_regindent; | |
3245 | #endif | |
c277df42 | 3246 | |
02db2b7b IZ |
3247 | REGCP_SET(uw->lastcp); |
3248 | ||
3249 | /* Now go into the first branch */ | |
3250 | next = inner; | |
a687059c | 3251 | } |
a0d0e21e LW |
3252 | } |
3253 | break; | |
3254 | case MINMOD: | |
3255 | minmod = 1; | |
3256 | break; | |
c277df42 IZ |
3257 | case CURLYM: |
3258 | { | |
00db4c45 | 3259 | I32 l = 0; |
c277df42 | 3260 | CHECKPOINT lastcp; |
9041c2e3 | 3261 | |
c277df42 IZ |
3262 | /* We suppose that the next guy does not need |
3263 | backtracking: in particular, it is of constant length, | |
3264 | and has no parenths to influence future backrefs. */ | |
3265 | ln = ARG1(scan); /* min to match */ | |
3266 | n = ARG2(scan); /* max to match */ | |
c277df42 IZ |
3267 | paren = scan->flags; |
3268 | if (paren) { | |
3280af22 NIS |
3269 | if (paren > PL_regsize) |
3270 | PL_regsize = paren; | |
3271 | if (paren > *PL_reglastparen) | |
3272 | *PL_reglastparen = paren; | |
c277df42 | 3273 | } |
dc45a647 | 3274 | scan = NEXTOPER(scan) + NODE_STEP_REGNODE; |
c277df42 IZ |
3275 | if (paren) |
3276 | scan += NEXT_OFF(scan); /* Skip former OPEN. */ | |
3280af22 | 3277 | PL_reginput = locinput; |
c277df42 IZ |
3278 | if (minmod) { |
3279 | minmod = 0; | |
3280 | if (ln && regrepeat_hard(scan, ln, &l) < ln) | |
3281 | sayNO; | |
f31a99c8 HS |
3282 | /* if we matched something zero-length we don't need to |
3283 | backtrack - capturing parens are already defined, so | |
3284 | the caveat in the maximal case doesn't apply | |
3285 | ||
3286 | XXXX if ln == 0, we can redo this check first time | |
3287 | through the following loop | |
3288 | */ | |
3289 | if (ln && l == 0) | |
3290 | n = ln; /* don't backtrack */ | |
3280af22 | 3291 | locinput = PL_reginput; |
cca55fe3 | 3292 | if (HAS_TEXT(next) || JUMPABLE(next)) { |
5f80c4cf JP |
3293 | regnode *text_node = next; |
3294 | ||
cca55fe3 | 3295 | if (! HAS_TEXT(text_node)) FIND_NEXT_IMPT(text_node); |
5f80c4cf | 3296 | |
cca55fe3 | 3297 | if (! HAS_TEXT(text_node)) c1 = c2 = -1000; |
5f80c4cf | 3298 | else { |
cca55fe3 JP |
3299 | if (PL_regkind[(U8)OP(text_node)] == REF) { |
3300 | I32 n, ln; | |
3301 | n = ARG(text_node); /* which paren pair */ | |
3302 | ln = PL_regstartp[n]; | |
3303 | /* assume yes if we haven't seen CLOSEn */ | |
3304 | if ( | |
3305 | *PL_reglastparen < n || | |
3306 | ln == -1 || | |
3307 | ln == PL_regendp[n] | |
3308 | ) { | |
3309 | c1 = c2 = -1000; | |
3310 | goto assume_ok_MM; | |
3311 | } | |
3312 | c1 = *(PL_bostr + ln); | |
3313 | } | |
3314 | else { c1 = (U8)*STRING(text_node); } | |
af5decee | 3315 | if (OP(text_node) == EXACTF || OP(text_node) == REFF) |
5f80c4cf | 3316 | c2 = PL_fold[c1]; |
af5decee | 3317 | else if (OP(text_node) == EXACTFL || OP(text_node) == REFFL) |
5f80c4cf JP |
3318 | c2 = PL_fold_locale[c1]; |
3319 | else | |
3320 | c2 = c1; | |
3321 | } | |
a0ed51b3 LW |
3322 | } |
3323 | else | |
c277df42 | 3324 | c1 = c2 = -1000; |
cca55fe3 | 3325 | assume_ok_MM: |
02db2b7b | 3326 | REGCP_SET(lastcp); |
5f4b28b2 | 3327 | /* This may be improved if l == 0. */ |
c277df42 IZ |
3328 | while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */ |
3329 | /* If it could work, try it. */ | |
3330 | if (c1 == -1000 || | |
3280af22 NIS |
3331 | UCHARAT(PL_reginput) == c1 || |
3332 | UCHARAT(PL_reginput) == c2) | |
c277df42 IZ |
3333 | { |
3334 | if (paren) { | |
f31a99c8 | 3335 | if (ln) { |
cf93c79d IZ |
3336 | PL_regstartp[paren] = |
3337 | HOPc(PL_reginput, -l) - PL_bostr; | |
3338 | PL_regendp[paren] = PL_reginput - PL_bostr; | |
a0ed51b3 LW |
3339 | } |
3340 | else | |
cf93c79d | 3341 | PL_regendp[paren] = -1; |
c277df42 IZ |
3342 | } |
3343 | if (regmatch(next)) | |
3344 | sayYES; | |
02db2b7b | 3345 | REGCP_UNWIND(lastcp); |
c277df42 IZ |
3346 | } |
3347 | /* Couldn't or didn't -- move forward. */ | |
3280af22 | 3348 | PL_reginput = locinput; |
c277df42 IZ |
3349 | if (regrepeat_hard(scan, 1, &l)) { |
3350 | ln++; | |
3280af22 | 3351 | locinput = PL_reginput; |
c277df42 IZ |
3352 | } |
3353 | else | |
3354 | sayNO; | |
3355 | } | |
a0ed51b3 LW |
3356 | } |
3357 | else { | |
c277df42 | 3358 | n = regrepeat_hard(scan, n, &l); |
f31a99c8 HS |
3359 | /* if we matched something zero-length we don't need to |
3360 | backtrack, unless the minimum count is zero and we | |
3361 | are capturing the result - in that case the capture | |
3362 | being defined or not may affect later execution | |
3363 | */ | |
3364 | if (n != 0 && l == 0 && !(paren && ln == 0)) | |
3365 | ln = n; /* don't backtrack */ | |
3280af22 | 3366 | locinput = PL_reginput; |
c277df42 | 3367 | DEBUG_r( |
5c0ca799 | 3368 | PerlIO_printf(Perl_debug_log, |
faccc32b | 3369 | "%*s matched %"IVdf" times, len=%"IVdf"...\n", |
7b0972df | 3370 | (int)(REPORT_CODE_OFF+PL_regindent*2), "", |
faccc32b | 3371 | (IV) n, (IV)l) |
c277df42 IZ |
3372 | ); |
3373 | if (n >= ln) { | |
cca55fe3 | 3374 | if (HAS_TEXT(next) || JUMPABLE(next)) { |
5f80c4cf JP |
3375 | regnode *text_node = next; |
3376 | ||
cca55fe3 | 3377 | if (! HAS_TEXT(text_node)) FIND_NEXT_IMPT(text_node); |
5f80c4cf | 3378 | |
cca55fe3 | 3379 | if (! HAS_TEXT(text_node)) c1 = c2 = -1000; |
5f80c4cf | 3380 | else { |
cca55fe3 JP |
3381 | if (PL_regkind[(U8)OP(text_node)] == REF) { |
3382 | I32 n, ln; | |
3383 | n = ARG(text_node); /* which paren pair */ | |
3384 | ln = PL_regstartp[n]; | |
3385 | /* assume yes if we haven't seen CLOSEn */ | |
3386 | if ( | |
3387 | *PL_reglastparen < n || | |
3388 | ln == -1 || | |
3389 | ln == PL_regendp[n] | |
3390 | ) { | |
3391 | c1 = c2 = -1000; | |
3392 | goto assume_ok_REG; | |
3393 | } | |
3394 | c1 = *(PL_bostr + ln); | |
3395 | } | |
3396 | else { c1 = (U8)*STRING(text_node); } | |
3397 | ||
af5decee | 3398 | if (OP(text_node) == EXACTF || OP(text_node) == REFF) |
5f80c4cf | 3399 | c2 = PL_fold[c1]; |
af5decee | 3400 | else if (OP(text_node) == EXACTFL || OP(text_node) == REFFL) |
5f80c4cf JP |
3401 | c2 = PL_fold_locale[c1]; |
3402 | else | |
3403 | c2 = c1; | |
3404 | } | |
a0ed51b3 LW |
3405 | } |
3406 | else | |
c277df42 IZ |
3407 | c1 = c2 = -1000; |
3408 | } | |
cca55fe3 | 3409 | assume_ok_REG: |
02db2b7b | 3410 | REGCP_SET(lastcp); |
c277df42 IZ |
3411 | while (n >= ln) { |
3412 | /* If it could work, try it. */ | |
3413 | if (c1 == -1000 || | |
3280af22 NIS |
3414 | UCHARAT(PL_reginput) == c1 || |
3415 | UCHARAT(PL_reginput) == c2) | |
a0ed51b3 LW |
3416 | { |
3417 | DEBUG_r( | |
c3464db5 | 3418 | PerlIO_printf(Perl_debug_log, |
7b0972df JH |
3419 | "%*s trying tail with n=%"IVdf"...\n", |
3420 | (int)(REPORT_CODE_OFF+PL_regindent*2), "", (IV)n) | |
a0ed51b3 LW |
3421 | ); |
3422 | if (paren) { | |
3423 | if (n) { | |
cf93c79d IZ |
3424 | PL_regstartp[paren] = HOPc(PL_reginput, -l) - PL_bostr; |
3425 | PL_regendp[paren] = PL_reginput - PL_bostr; | |
c277df42 | 3426 | } |
a0ed51b3 | 3427 | else |
cf93c79d | 3428 | PL_regendp[paren] = -1; |
c277df42 | 3429 | } |
a0ed51b3 LW |
3430 | if (regmatch(next)) |
3431 | sayYES; | |
02db2b7b | 3432 | REGCP_UNWIND(lastcp); |
a0ed51b3 | 3433 | } |
c277df42 IZ |
3434 | /* Couldn't or didn't -- back up. */ |
3435 | n--; | |
dfe13c55 | 3436 | locinput = HOPc(locinput, -l); |
3280af22 | 3437 | PL_reginput = locinput; |
c277df42 IZ |
3438 | } |
3439 | } | |
3440 | sayNO; | |
3441 | break; | |
3442 | } | |
3443 | case CURLYN: | |
3444 | paren = scan->flags; /* Which paren to set */ | |
3280af22 NIS |
3445 | if (paren > PL_regsize) |
3446 | PL_regsize = paren; | |
3447 | if (paren > *PL_reglastparen) | |
3448 | *PL_reglastparen = paren; | |
c277df42 IZ |
3449 | ln = ARG1(scan); /* min to match */ |
3450 | n = ARG2(scan); /* max to match */ | |
dc45a647 | 3451 | scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE); |
c277df42 | 3452 | goto repeat; |
a0d0e21e | 3453 | case CURLY: |
c277df42 | 3454 | paren = 0; |
a0d0e21e LW |
3455 | ln = ARG1(scan); /* min to match */ |
3456 | n = ARG2(scan); /* max to match */ | |
dc45a647 | 3457 | scan = NEXTOPER(scan) + NODE_STEP_REGNODE; |
a0d0e21e LW |
3458 | goto repeat; |
3459 | case STAR: | |
3460 | ln = 0; | |
c277df42 | 3461 | n = REG_INFTY; |
a0d0e21e | 3462 | scan = NEXTOPER(scan); |
c277df42 | 3463 | paren = 0; |
a0d0e21e LW |
3464 | goto repeat; |
3465 | case PLUS: | |
c277df42 IZ |
3466 | ln = 1; |
3467 | n = REG_INFTY; | |
3468 | scan = NEXTOPER(scan); | |
3469 | paren = 0; | |
3470 | repeat: | |
a0d0e21e LW |
3471 | /* |
3472 | * Lookahead to avoid useless match attempts | |
3473 | * when we know what character comes next. | |
3474 | */ | |
5f80c4cf JP |
3475 | |
3476 | /* | |
3477 | * Used to only do .*x and .*?x, but now it allows | |
3478 | * for )'s, ('s and (?{ ... })'s to be in the way | |
3479 | * of the quantifier and the EXACT-like node. -- japhy | |
3480 | */ | |
3481 | ||
cca55fe3 | 3482 | if (HAS_TEXT(next) || JUMPABLE(next)) { |
5f80c4cf JP |
3483 | U8 *s; |
3484 | regnode *text_node = next; | |
3485 | ||
cca55fe3 | 3486 | if (! HAS_TEXT(text_node)) FIND_NEXT_IMPT(text_node); |
5f80c4cf | 3487 | |
cca55fe3 | 3488 | if (! HAS_TEXT(text_node)) c1 = c2 = -1000; |
5f80c4cf | 3489 | else { |
cca55fe3 JP |
3490 | if (PL_regkind[(U8)OP(text_node)] == REF) { |
3491 | I32 n, ln; | |
3492 | n = ARG(text_node); /* which paren pair */ | |
3493 | ln = PL_regstartp[n]; | |
3494 | /* assume yes if we haven't seen CLOSEn */ | |
3495 | if ( | |
3496 | *PL_reglastparen < n || | |
3497 | ln == -1 || | |
3498 | ln == PL_regendp[n] | |
3499 | ) { | |
3500 | c1 = c2 = -1000; | |
3501 | goto assume_ok_easy; | |
3502 | } | |
9246c65e | 3503 | s = (U8*)PL_bostr + ln; |
cca55fe3 JP |
3504 | } |
3505 | else { s = (U8*)STRING(text_node); } | |
5f80c4cf JP |
3506 | |
3507 | if (!UTF) { | |
3508 | c2 = c1 = *s; | |
f65d3ee7 | 3509 | if (OP(text_node) == EXACTF || OP(text_node) == REFF) |
5f80c4cf | 3510 | c2 = PL_fold[c1]; |
f65d3ee7 | 3511 | else if (OP(text_node) == EXACTFL || OP(text_node) == REFFL) |
5f80c4cf | 3512 | c2 = PL_fold_locale[c1]; |
1aa99e6b | 3513 | } |
5f80c4cf | 3514 | else { /* UTF */ |
f65d3ee7 | 3515 | if (OP(text_node) == EXACTF || OP(text_node) == REFF) { |
a2a2844f | 3516 | STRLEN ulen1, ulen2; |
e7ae6809 JH |
3517 | U8 tmpbuf1[UTF8_MAXLEN_UCLC+1]; |
3518 | U8 tmpbuf2[UTF8_MAXLEN_UCLC+1]; | |
a2a2844f JH |
3519 | |
3520 | to_utf8_lower((U8*)s, tmpbuf1, &ulen1); | |
3521 | to_utf8_upper((U8*)s, tmpbuf2, &ulen2); | |
3522 | ||
3523 | c1 = utf8_to_uvuni(tmpbuf1, 0); | |
3524 | c2 = utf8_to_uvuni(tmpbuf2, 0); | |
5f80c4cf JP |
3525 | } |
3526 | else { | |
3527 | c2 = c1 = utf8_to_uvchr(s, NULL); | |
3528 | } | |
1aa99e6b IH |
3529 | } |
3530 | } | |
bbce6d69 | 3531 | } |
a0d0e21e | 3532 | else |
bbce6d69 | 3533 | c1 = c2 = -1000; |
cca55fe3 | 3534 | assume_ok_easy: |
3280af22 | 3535 | PL_reginput = locinput; |
a0d0e21e | 3536 | if (minmod) { |
c277df42 | 3537 | CHECKPOINT lastcp; |
a0d0e21e LW |
3538 | minmod = 0; |
3539 | if (ln && regrepeat(scan, ln) < ln) | |
4633a7c4 | 3540 | sayNO; |
a0ed51b3 | 3541 | locinput = PL_reginput; |
02db2b7b | 3542 | REGCP_SET(lastcp); |
0fe9bf95 | 3543 | if (c1 != -1000) { |
1aa99e6b | 3544 | char *e; /* Should not check after this */ |
0fe9bf95 IZ |
3545 | char *old = locinput; |
3546 | ||
1aa99e6b | 3547 | if (n == REG_INFTY) { |
0fe9bf95 | 3548 | e = PL_regeol - 1; |
1aa99e6b IH |
3549 | if (do_utf8) |
3550 | while (UTF8_IS_CONTINUATION(*(U8*)e)) | |
3551 | e--; | |
3552 | } | |
3553 | else if (do_utf8) { | |
3554 | int m = n - ln; | |
3555 | for (e = locinput; | |
3556 | m >0 && e + UTF8SKIP(e) <= PL_regeol; m--) | |
3557 | e += UTF8SKIP(e); | |
3558 | } | |
3559 | else { | |
3560 | e = locinput + n - ln; | |
3561 | if (e >= PL_regeol) | |
3562 | e = PL_regeol - 1; | |
3563 | } | |
0fe9bf95 | 3564 | while (1) { |
1aa99e6b | 3565 | int count; |
0fe9bf95 | 3566 | /* Find place 'next' could work */ |
1aa99e6b IH |
3567 | if (!do_utf8) { |
3568 | if (c1 == c2) { | |
a8e8ab15 JH |
3569 | while (locinput <= e && |
3570 | UCHARAT(locinput) != c1) | |
1aa99e6b IH |
3571 | locinput++; |
3572 | } else { | |
9041c2e3 | 3573 | while (locinput <= e |
a8e8ab15 JH |
3574 | && UCHARAT(locinput) != c1 |
3575 | && UCHARAT(locinput) != c2) | |
1aa99e6b IH |
3576 | locinput++; |
3577 | } | |
3578 | count = locinput - old; | |
3579 | } | |
3580 | else { | |
3581 | STRLEN len; | |
3582 | if (c1 == c2) { | |
3583 | for (count = 0; | |
3584 | locinput <= e && | |
9041c2e3 | 3585 | utf8_to_uvchr((U8*)locinput, &len) != c1; |
1aa99e6b IH |
3586 | count++) |
3587 | locinput += len; | |
3588 | ||
3589 | } else { | |
3590 | for (count = 0; locinput <= e; count++) { | |
9041c2e3 | 3591 | UV c = utf8_to_uvchr((U8*)locinput, &len); |
1aa99e6b IH |
3592 | if (c == c1 || c == c2) |
3593 | break; | |
9041c2e3 | 3594 | locinput += len; |
1aa99e6b IH |
3595 | } |
3596 | } | |
0fe9bf95 | 3597 | } |
9041c2e3 | 3598 | if (locinput > e) |
0fe9bf95 IZ |
3599 | sayNO; |
3600 | /* PL_reginput == old now */ | |
3601 | if (locinput != old) { | |
3602 | ln = 1; /* Did some */ | |
1aa99e6b | 3603 | if (regrepeat(scan, count) < count) |
0fe9bf95 IZ |
3604 | sayNO; |
3605 | } | |
3606 | /* PL_reginput == locinput now */ | |
29d1e993 | 3607 | TRYPAREN(paren, ln, locinput); |
0fe9bf95 | 3608 | PL_reginput = locinput; /* Could be reset... */ |
02db2b7b | 3609 | REGCP_UNWIND(lastcp); |
0fe9bf95 | 3610 | /* Couldn't or didn't -- move forward. */ |
1aa99e6b IH |
3611 | old = locinput; |
3612 | if (do_utf8) | |
3613 | locinput += UTF8SKIP(locinput); | |
3614 | else | |
3615 | locinput++; | |
0fe9bf95 IZ |
3616 | } |
3617 | } | |
3618 | else | |
c277df42 | 3619 | while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */ |
1aa99e6b IH |
3620 | UV c; |
3621 | if (c1 != -1000) { | |
3622 | if (do_utf8) | |
9041c2e3 | 3623 | c = utf8_to_uvchr((U8*)PL_reginput, NULL); |
1aa99e6b | 3624 | else |
9041c2e3 | 3625 | c = UCHARAT(PL_reginput); |
2390ecbc PP |
3626 | /* If it could work, try it. */ |
3627 | if (c == c1 || c == c2) | |
3628 | { | |
3629 | TRYPAREN(paren, n, PL_reginput); | |
3630 | REGCP_UNWIND(lastcp); | |
3631 | } | |
1aa99e6b | 3632 | } |
a0d0e21e | 3633 | /* If it could work, try it. */ |
2390ecbc | 3634 | else if (c1 == -1000) |
bbce6d69 | 3635 | { |
29d1e993 | 3636 | TRYPAREN(paren, n, PL_reginput); |
02db2b7b | 3637 | REGCP_UNWIND(lastcp); |
bbce6d69 | 3638 | } |
c277df42 | 3639 | /* Couldn't or didn't -- move forward. */ |
a0ed51b3 | 3640 | PL_reginput = locinput; |
a0d0e21e LW |
3641 | if (regrepeat(scan, 1)) { |
3642 | ln++; | |
a0ed51b3 LW |
3643 | locinput = PL_reginput; |
3644 | } | |
3645 | else | |
4633a7c4 | 3646 | sayNO; |
a0d0e21e LW |
3647 | } |
3648 | } | |
3649 | else { | |
c277df42 | 3650 | CHECKPOINT lastcp; |
a0d0e21e | 3651 | n = regrepeat(scan, n); |
a0ed51b3 | 3652 | locinput = PL_reginput; |
22c35a8c | 3653 | if (ln < n && PL_regkind[(U8)OP(next)] == EOL && |
1aeab75a | 3654 | (!PL_multiline || OP(next) == SEOL || OP(next) == EOS)) { |
a0d0e21e | 3655 | ln = n; /* why back off? */ |
1aeab75a GS |
3656 | /* ...because $ and \Z can match before *and* after |
3657 | newline at the end. Consider "\n\n" =~ /\n+\Z\n/. | |
3658 | We should back off by one in this case. */ | |
3659 | if (UCHARAT(PL_reginput - 1) == '\n' && OP(next) != EOS) | |
3660 | ln--; | |
3661 | } | |
02db2b7b | 3662 | REGCP_SET(lastcp); |
c277df42 | 3663 | if (paren) { |
8fa7f367 | 3664 | UV c = 0; |
c277df42 | 3665 | while (n >= ln) { |
1aa99e6b IH |
3666 | if (c1 != -1000) { |
3667 | if (do_utf8) | |
9041c2e3 | 3668 | c = utf8_to_uvchr((U8*)PL_reginput, NULL); |
1aa99e6b | 3669 | else |
9041c2e3 | 3670 | c = UCHARAT(PL_reginput); |
1aa99e6b | 3671 | } |
c277df42 | 3672 | /* If it could work, try it. */ |
1aa99e6b | 3673 | if (c1 == -1000 || c == c1 || c == c2) |
c277df42 | 3674 | { |
29d1e993 | 3675 | TRYPAREN(paren, n, PL_reginput); |
02db2b7b | 3676 | REGCP_UNWIND(lastcp); |
c277df42 IZ |
3677 | } |
3678 | /* Couldn't or didn't -- back up. */ | |
3679 | n--; | |
dfe13c55 | 3680 | PL_reginput = locinput = HOPc(locinput, -1); |
c277df42 | 3681 | } |
a0ed51b3 LW |
3682 | } |
3683 | else { | |
8fa7f367 | 3684 | UV c = 0; |
c277df42 | 3685 | while (n >= ln) { |
1aa99e6b IH |
3686 | if (c1 != -1000) { |
3687 | if (do_utf8) | |
9041c2e3 | 3688 | c = utf8_to_uvchr((U8*)PL_reginput, NULL); |
1aa99e6b | 3689 | else |
9041c2e3 | 3690 | c = UCHARAT(PL_reginput); |
1aa99e6b | 3691 | } |
c277df42 | 3692 | /* If it could work, try it. */ |
1aa99e6b | 3693 | if (c1 == -1000 || c == c1 || c == c2) |
c277df42 | 3694 | { |
29d1e993 | 3695 | TRYPAREN(paren, n, PL_reginput); |
02db2b7b | 3696 | REGCP_UNWIND(lastcp); |
c277df42 IZ |
3697 | } |
3698 | /* Couldn't or didn't -- back up. */ | |
3699 | n--; | |
dfe13c55 | 3700 | PL_reginput = locinput = HOPc(locinput, -1); |
bbce6d69 | 3701 | } |
a0d0e21e LW |
3702 | } |
3703 | } | |
4633a7c4 | 3704 | sayNO; |
c277df42 | 3705 | break; |
a0d0e21e | 3706 | case END: |
0f5d15d6 IZ |
3707 | if (PL_reg_call_cc) { |
3708 | re_cc_state *cur_call_cc = PL_reg_call_cc; | |
3709 | CURCUR *cctmp = PL_regcc; | |
3710 | regexp *re = PL_reg_re; | |
3711 | CHECKPOINT cp, lastcp; | |
3712 | ||
3713 | cp = regcppush(0); /* Save *all* the positions. */ | |
02db2b7b | 3714 | REGCP_SET(lastcp); |
0f5d15d6 IZ |
3715 | regcp_set_to(PL_reg_call_cc->ss); /* Restore parens of |
3716 | the caller. */ | |
3717 | PL_reginput = locinput; /* Make position available to | |
3718 | the callcc. */ | |
3719 | cache_re(PL_reg_call_cc->re); | |
3720 | PL_regcc = PL_reg_call_cc->cc; | |
3721 | PL_reg_call_cc = PL_reg_call_cc->prev; | |
3722 | if (regmatch(cur_call_cc->node)) { | |
3723 | PL_reg_call_cc = cur_call_cc; | |
3724 | regcpblow(cp); | |
3725 | sayYES; | |
3726 | } | |
02db2b7b | 3727 | REGCP_UNWIND(lastcp); |
0f5d15d6 IZ |
3728 | regcppop(); |
3729 | PL_reg_call_cc = cur_call_cc; | |
3730 | PL_regcc = cctmp; | |
3731 | PL_reg_re = re; | |
3732 | cache_re(re); | |
3733 | ||
3734 | DEBUG_r( | |
3735 | PerlIO_printf(Perl_debug_log, | |
3736 | "%*s continuation failed...\n", | |
3737 | REPORT_CODE_OFF+PL_regindent*2, "") | |
3738 | ); | |
7821416a | 3739 | sayNO_SILENT; |
0f5d15d6 | 3740 | } |
7821416a IZ |
3741 | if (locinput < PL_regtill) { |
3742 | DEBUG_r(PerlIO_printf(Perl_debug_log, | |
3743 | "%sMatch possible, but length=%ld is smaller than requested=%ld, failing!%s\n", | |
3744 | PL_colors[4], | |
3745 | (long)(locinput - PL_reg_starttry), | |
3746 | (long)(PL_regtill - PL_reg_starttry), | |
3747 | PL_colors[5])); | |
3748 | sayNO_FINAL; /* Cannot match: too short. */ | |
3749 | } | |
3750 | PL_reginput = locinput; /* put where regtry can find it */ | |
3751 | sayYES_FINAL; /* Success! */ | |
7e5428c5 | 3752 | case SUCCEED: |
3280af22 | 3753 | PL_reginput = locinput; /* put where regtry can find it */ |
7821416a | 3754 | sayYES_LOUD; /* Success! */ |
c277df42 IZ |
3755 | case SUSPEND: |
3756 | n = 1; | |
9fe1d20c | 3757 | PL_reginput = locinput; |
9041c2e3 | 3758 | goto do_ifmatch; |
a0d0e21e | 3759 | case UNLESSM: |
c277df42 | 3760 | n = 0; |
a0ed51b3 | 3761 | if (scan->flags) { |
efb30f32 HS |
3762 | s = HOPBACKc(locinput, scan->flags); |
3763 | if (!s) | |
3764 | goto say_yes; | |
3765 | PL_reginput = s; | |
a0ed51b3 LW |
3766 | } |
3767 | else | |
3768 | PL_reginput = locinput; | |
c277df42 IZ |
3769 | goto do_ifmatch; |
3770 | case IFMATCH: | |
3771 | n = 1; | |
a0ed51b3 | 3772 | if (scan->flags) { |
efb30f32 HS |
3773 | s = HOPBACKc(locinput, scan->flags); |
3774 | if (!s) | |
3775 | goto say_no; | |
3776 | PL_reginput = s; | |
a0ed51b3 LW |
3777 | } |
3778 | else | |
3779 | PL_reginput = locinput; | |
3780 | ||
c277df42 | 3781 | do_ifmatch: |
c277df42 IZ |
3782 | inner = NEXTOPER(NEXTOPER(scan)); |
3783 | if (regmatch(inner) != n) { | |
3784 | say_no: | |
3785 | if (logical) { | |
3786 | logical = 0; | |
3787 | sw = 0; | |
3788 | goto do_longjump; | |
a0ed51b3 LW |
3789 | } |
3790 | else | |
c277df42 IZ |
3791 | sayNO; |
3792 | } | |
3793 | say_yes: | |
3794 | if (logical) { | |
3795 | logical = 0; | |
3796 | sw = 1; | |
3797 | } | |
fe44a5e8 | 3798 | if (OP(scan) == SUSPEND) { |
3280af22 | 3799 | locinput = PL_reginput; |
565764a8 | 3800 | nextchr = UCHARAT(locinput); |
fe44a5e8 | 3801 | } |
c277df42 IZ |
3802 | /* FALL THROUGH. */ |
3803 | case LONGJMP: | |
3804 | do_longjump: | |
3805 | next = scan + ARG(scan); | |
3806 | if (next == scan) | |
3807 | next = NULL; | |
a0d0e21e LW |
3808 | break; |
3809 | default: | |
b900a521 | 3810 | PerlIO_printf(Perl_error_log, "%"UVxf" %d\n", |
d7d93a81 | 3811 | PTR2UV(scan), OP(scan)); |
cea2e8a9 | 3812 | Perl_croak(aTHX_ "regexp memory corruption"); |
a687059c | 3813 | } |
02db2b7b | 3814 | reenter: |
a0d0e21e LW |
3815 | scan = next; |
3816 | } | |
a687059c | 3817 | |
a0d0e21e LW |
3818 | /* |
3819 | * We get here only if there's trouble -- normally "case END" is | |
3820 | * the terminating point. | |
3821 | */ | |
cea2e8a9 | 3822 | Perl_croak(aTHX_ "corrupted regexp pointers"); |
a0d0e21e | 3823 | /*NOTREACHED*/ |
4633a7c4 LW |
3824 | sayNO; |
3825 | ||
7821416a IZ |
3826 | yes_loud: |
3827 | DEBUG_r( | |
3828 | PerlIO_printf(Perl_debug_log, | |
3829 | "%*s %scould match...%s\n", | |
3830 | REPORT_CODE_OFF+PL_regindent*2, "", PL_colors[4],PL_colors[5]) | |
3831 | ); | |
3832 | goto yes; | |
3833 | yes_final: | |
3834 | DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch successful!%s\n", | |
3835 | PL_colors[4],PL_colors[5])); | |
4633a7c4 LW |
3836 | yes: |
3837 | #ifdef DEBUGGING | |
3280af22 | 3838 | PL_regindent--; |
4633a7c4 | 3839 | #endif |
02db2b7b IZ |
3840 | |
3841 | #if 0 /* Breaks $^R */ | |
3842 | if (unwind) | |
3843 | regcpblow(firstcp); | |
3844 | #endif | |
4633a7c4 LW |
3845 | return 1; |
3846 | ||
3847 | no: | |
7821416a IZ |
3848 | DEBUG_r( |
3849 | PerlIO_printf(Perl_debug_log, | |
3850 | "%*s %sfailed...%s\n", | |
3851 | REPORT_CODE_OFF+PL_regindent*2, "",PL_colors[4],PL_colors[5]) | |
3852 | ); | |
3853 | goto do_no; | |
3854 | no_final: | |
3855 | do_no: | |
02db2b7b IZ |
3856 | if (unwind) { |
3857 | re_unwind_t *uw = SSPTRt(unwind,re_unwind_t); | |
3858 | ||
3859 | switch (uw->type) { | |
3860 | case RE_UNWIND_BRANCH: | |
3861 | case RE_UNWIND_BRANCHJ: | |
3862 | { | |
3863 | re_unwind_branch_t *uwb = &(uw->branch); | |
3864 | I32 lastparen = uwb->lastparen; | |
9041c2e3 | 3865 | |
02db2b7b IZ |
3866 | REGCP_UNWIND(uwb->lastcp); |
3867 | for (n = *PL_reglastparen; n > lastparen; n--) | |
3868 | PL_regendp[n] = -1; | |
3869 | *PL_reglastparen = n; | |
3870 | scan = next = uwb->next; | |
9041c2e3 NIS |
3871 | if ( !scan || |
3872 | OP(scan) != (uwb->type == RE_UNWIND_BRANCH | |
02db2b7b IZ |
3873 | ? BRANCH : BRANCHJ) ) { /* Failure */ |
3874 | unwind = uwb->prev; | |
3875 | #ifdef DEBUGGING | |
3876 | PL_regindent--; | |
3877 | #endif | |
3878 | goto do_no; | |
3879 | } | |
3880 | /* Have more choice yet. Reuse the same uwb. */ | |
3881 | /*SUPPRESS 560*/ | |
3882 | if ((n = (uwb->type == RE_UNWIND_BRANCH | |
3883 | ? NEXT_OFF(next) : ARG(next)))) | |
3884 | next += n; | |
3885 | else | |
3886 | next = NULL; /* XXXX Needn't unwinding in this case... */ | |
3887 | uwb->next = next; | |
3888 | next = NEXTOPER(scan); | |
3889 | if (uwb->type == RE_UNWIND_BRANCHJ) | |
3890 | next = NEXTOPER(next); | |
3891 | locinput = uwb->locinput; | |
3892 | nextchr = uwb->nextchr; | |
3893 | #ifdef DEBUGGING | |
3894 | PL_regindent = uwb->regindent; | |
3895 | #endif | |
3896 | ||
3897 | goto reenter; | |
3898 | } | |
3899 | /* NOT REACHED */ | |
3900 | default: | |
3901 | Perl_croak(aTHX_ "regexp unwind memory corruption"); | |
3902 | } | |
3903 | /* NOT REACHED */ | |
3904 | } | |
4633a7c4 | 3905 | #ifdef DEBUGGING |
3280af22 | 3906 | PL_regindent--; |
4633a7c4 | 3907 | #endif |
a0d0e21e | 3908 | return 0; |
a687059c LW |
3909 | } |
3910 | ||
3911 | /* | |
3912 | - regrepeat - repeatedly match something simple, report how many | |
3913 | */ | |
3914 | /* | |
3915 | * [This routine now assumes that it will only match on things of length 1. | |
3916 | * That was true before, but now we assume scan - reginput is the count, | |
a0ed51b3 | 3917 | * rather than incrementing count on every character. [Er, except utf8.]] |
a687059c | 3918 | */ |
76e3520e | 3919 | STATIC I32 |
cea2e8a9 | 3920 | S_regrepeat(pTHX_ regnode *p, I32 max) |
a687059c | 3921 | { |
a0d0e21e | 3922 | register char *scan; |
a0d0e21e | 3923 | register I32 c; |
3280af22 | 3924 | register char *loceol = PL_regeol; |
a0ed51b3 | 3925 | register I32 hardcount = 0; |
53c4c00c | 3926 | register bool do_utf8 = PL_reg_match_utf8; |
a0d0e21e | 3927 | |
3280af22 | 3928 | scan = PL_reginput; |
c277df42 | 3929 | if (max != REG_INFTY && max < loceol - scan) |
a0d0e21e | 3930 | loceol = scan + max; |
a0d0e21e | 3931 | switch (OP(p)) { |
22c35a8c | 3932 | case REG_ANY: |
1aa99e6b | 3933 | if (do_utf8) { |
ffc61ed2 | 3934 | loceol = PL_regeol; |
1aa99e6b | 3935 | while (scan < loceol && hardcount < max && *scan != '\n') { |
ffc61ed2 JH |
3936 | scan += UTF8SKIP(scan); |
3937 | hardcount++; | |
3938 | } | |
3939 | } else { | |
3940 | while (scan < loceol && *scan != '\n') | |
3941 | scan++; | |
a0ed51b3 LW |
3942 | } |
3943 | break; | |
ffc61ed2 | 3944 | case SANY: |
3baa4c62 | 3945 | scan = loceol; |
a0ed51b3 | 3946 | break; |
f33976b4 DB |
3947 | case CANY: |
3948 | scan = loceol; | |
3949 | break; | |
bbce6d69 | 3950 | case EXACT: /* length of string is 1 */ |
cd439c50 | 3951 | c = (U8)*STRING(p); |
bbce6d69 | 3952 | while (scan < loceol && UCHARAT(scan) == c) |
3953 | scan++; | |
3954 | break; | |
3955 | case EXACTF: /* length of string is 1 */ | |
cd439c50 | 3956 | c = (U8)*STRING(p); |
bbce6d69 | 3957 | while (scan < loceol && |
22c35a8c | 3958 | (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold[c])) |
bbce6d69 | 3959 | scan++; |
3960 | break; | |
3961 | case EXACTFL: /* length of string is 1 */ | |
3280af22 | 3962 | PL_reg_flags |= RF_tainted; |
cd439c50 | 3963 | c = (U8)*STRING(p); |
bbce6d69 | 3964 | while (scan < loceol && |
22c35a8c | 3965 | (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold_locale[c])) |
a0d0e21e LW |
3966 | scan++; |
3967 | break; | |
3968 | case ANYOF: | |
ffc61ed2 JH |
3969 | if (do_utf8) { |
3970 | loceol = PL_regeol; | |
1aa99e6b IH |
3971 | while (hardcount < max && scan < loceol && |
3972 | reginclass(p, (U8*)scan, do_utf8)) { | |
ffc61ed2 JH |
3973 | scan += UTF8SKIP(scan); |
3974 | hardcount++; | |
3975 | } | |
3976 | } else { | |
3977 | while (scan < loceol && reginclass(p, (U8*)scan, do_utf8)) | |
3978 | scan++; | |
3979 | } | |
a0d0e21e LW |
3980 | break; |
3981 | case ALNUM: | |
1aa99e6b | 3982 | if (do_utf8) { |
ffc61ed2 | 3983 | loceol = PL_regeol; |
8269fa76 | 3984 | LOAD_UTF8_CHARCLASS(alnum,"a"); |
1aa99e6b | 3985 | while (hardcount < max && scan < loceol && |
3568d838 | 3986 | swash_fetch(PL_utf8_alnum, (U8*)scan, do_utf8)) { |
ffc61ed2 JH |
3987 | scan += UTF8SKIP(scan); |
3988 | hardcount++; | |
3989 | } | |
3990 | } else { | |
3991 | while (scan < loceol && isALNUM(*scan)) | |
3992 | scan++; | |
a0ed51b3 LW |
3993 | } |
3994 | break; | |
bbce6d69 | 3995 | case ALNUML: |
3280af22 | 3996 | PL_reg_flags |= RF_tainted; |
1aa99e6b | 3997 | if (do_utf8) { |
ffc61ed2 | 3998 | loceol = PL_regeol; |
1aa99e6b IH |
3999 | while (hardcount < max && scan < loceol && |
4000 | isALNUM_LC_utf8((U8*)scan)) { | |
ffc61ed2 JH |
4001 | scan += UTF8SKIP(scan); |
4002 | hardcount++; | |
4003 | } | |
4004 | } else { | |
4005 | while (scan < loceol && isALNUM_LC(*scan)) | |
4006 | scan++; | |
a0ed51b3 LW |
4007 | } |
4008 | break; | |
a0d0e21e | 4009 | case NALNUM: |
1aa99e6b | 4010 | if (do_utf8) { |
ffc61ed2 | 4011 | loceol = PL_regeol; |
8269fa76 | 4012 | LOAD_UTF8_CHARCLASS(alnum,"a"); |
1aa99e6b | 4013 | while (hardcount < max && scan < loceol && |
3568d838 | 4014 | !swash_fetch(PL_utf8_alnum, (U8*)scan, do_utf8)) { |
ffc61ed2 JH |
4015 | scan += UTF8SKIP(scan); |
4016 | hardcount++; | |
4017 | } | |
4018 | } else { | |
4019 | while (scan < loceol && !isALNUM(*scan)) | |
4020 | scan++; | |
a0ed51b3 LW |
4021 | } |
4022 | break; | |
bbce6d69 | 4023 | case NALNUML: |
3280af22 | 4024 | PL_reg_flags |= RF_tainted; |
1aa99e6b | 4025 | if (do_utf8) { |
ffc61ed2 | 4026 | loceol = PL_regeol; |
1aa99e6b IH |
4027 | while (hardcount < max && scan < loceol && |
4028 | !isALNUM_LC_utf8((U8*)scan)) { | |
ffc61ed2 JH |
4029 | scan += UTF8SKIP(scan); |
4030 | hardcount++; | |
4031 | } | |
4032 | } else { | |
4033 | while (scan < loceol && !isALNUM_LC(*scan)) | |
4034 | scan++; | |
a0ed51b3 LW |
4035 | } |
4036 | break; | |
a0d0e21e | 4037 | case SPACE: |
1aa99e6b | 4038 | if (do_utf8) { |
ffc61ed2 | 4039 | loceol = PL_regeol; |
8269fa76 | 4040 | LOAD_UTF8_CHARCLASS(space," "); |
1aa99e6b | 4041 | while (hardcount < max && scan < loceol && |
3568d838 JH |
4042 | (*scan == ' ' || |
4043 | swash_fetch(PL_utf8_space,(U8*)scan, do_utf8))) { | |
ffc61ed2 JH |
4044 | scan += UTF8SKIP(scan); |
4045 | hardcount++; | |
4046 | } | |
4047 | } else { | |
4048 | while (scan < loceol && isSPACE(*scan)) | |
4049 | scan++; | |
a0ed51b3 LW |
4050 | } |
4051 | break; | |
bbce6d69 | 4052 | case SPACEL: |
3280af22 | 4053 | PL_reg_flags |= RF_tainted; |
1aa99e6b | 4054 | if (do_utf8) { |
ffc61ed2 | 4055 | loceol = PL_regeol; |
1aa99e6b | 4056 | while (hardcount < max && scan < loceol && |
ffc61ed2 JH |
4057 | (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) { |
4058 | scan += UTF8SKIP(scan); | |
4059 | hardcount++; | |
4060 | } | |
4061 | } else { | |
4062 | while (scan < loceol && isSPACE_LC(*scan)) | |
4063 | scan++; | |
a0ed51b3 LW |
4064 | } |
4065 | break; | |
a0d0e21e | 4066 | case NSPACE: |
1aa99e6b | 4067 | if (do_utf8) { |
ffc61ed2 | 4068 | loceol = PL_regeol; |
8269fa76 | 4069 | LOAD_UTF8_CHARCLASS(space," "); |
1aa99e6b | 4070 | while (hardcount < max && scan < loceol && |
3568d838 JH |
4071 | !(*scan == ' ' || |
4072 | swash_fetch(PL_utf8_space,(U8*)scan, do_utf8))) { | |
ffc61ed2 JH |
4073 | scan += UTF8SKIP(scan); |
4074 | hardcount++; | |
4075 | } | |
4076 | } else { | |
4077 | while (scan < loceol && !isSPACE(*scan)) | |
4078 | scan++; | |
4079 | break; | |
a0ed51b3 | 4080 | } |
bbce6d69 | 4081 | case NSPACEL: |
3280af22 | 4082 | PL_reg_flags |= RF_tainted; |
1aa99e6b | 4083 | if (do_utf8) { |
ffc61ed2 | 4084 | loceol = PL_regeol; |
1aa99e6b | 4085 | while (hardcount < max && scan < loceol && |
ffc61ed2 JH |
4086 | !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) { |
4087 | scan += UTF8SKIP(scan); | |
4088 | hardcount++; | |
4089 | } | |
4090 | } else { | |
4091 | while (scan < loceol && !isSPACE_LC(*scan)) | |
4092 | scan++; | |
a0ed51b3 LW |
4093 | } |
4094 | break; | |
a0d0e21e | 4095 | case DIGIT: |
1aa99e6b | 4096 | if (do_utf8) { |
ffc61ed2 | 4097 | loceol = PL_regeol; |
8269fa76 | 4098 | LOAD_UTF8_CHARCLASS(digit,"0"); |
1aa99e6b | 4099 | while (hardcount < max && scan < loceol && |
3568d838 | 4100 | swash_fetch(PL_utf8_digit, (U8*)scan, do_utf8)) { |
ffc61ed2 JH |
4101 | scan += UTF8SKIP(scan); |
4102 | hardcount++; | |
4103 | } | |
4104 | } else { | |
4105 | while (scan < loceol && isDIGIT(*scan)) | |
4106 | scan++; | |
a0ed51b3 LW |
4107 | } |
4108 | break; | |
a0d0e21e | 4109 | case NDIGIT: |
1aa99e6b | 4110 | if (do_utf8) { |
ffc61ed2 | 4111 | loceol = PL_regeol; |
8269fa76 | 4112 | LOAD_UTF8_CHARCLASS(digit,"0"); |
1aa99e6b | 4113 | while (hardcount < max && scan < loceol && |
3568d838 | 4114 | !swash_fetch(PL_utf8_digit, (U8*)scan, do_utf8)) { |
ffc61ed2 JH |
4115 | scan += UTF8SKIP(scan); |
4116 | hardcount++; | |
4117 | } | |
4118 | } else { | |
4119 | while (scan < loceol && !isDIGIT(*scan)) | |
4120 | scan++; | |
a0ed51b3 LW |
4121 | } |
4122 | break; | |
a0d0e21e LW |
4123 | default: /* Called on something of 0 width. */ |
4124 | break; /* So match right here or not at all. */ | |
4125 | } | |
a687059c | 4126 | |
a0ed51b3 LW |
4127 | if (hardcount) |
4128 | c = hardcount; | |
4129 | else | |
4130 | c = scan - PL_reginput; | |
3280af22 | 4131 | PL_reginput = scan; |
a687059c | 4132 | |
9041c2e3 | 4133 | DEBUG_r( |
c277df42 IZ |
4134 | { |
4135 | SV *prop = sv_newmortal(); | |
4136 | ||
4137 | regprop(prop, p); | |
9041c2e3 NIS |
4138 | PerlIO_printf(Perl_debug_log, |
4139 | "%*s %s can match %"IVdf" times out of %"IVdf"...\n", | |
7b0972df | 4140 | REPORT_CODE_OFF+1, "", SvPVX(prop),(IV)c,(IV)max); |
c277df42 | 4141 | }); |
9041c2e3 | 4142 | |
a0d0e21e | 4143 | return(c); |
a687059c LW |
4144 | } |
4145 | ||
4146 | /* | |
c277df42 | 4147 | - regrepeat_hard - repeatedly match something, report total lenth and length |
9041c2e3 | 4148 | * |
c277df42 IZ |
4149 | * The repeater is supposed to have constant length. |
4150 | */ | |
4151 | ||
76e3520e | 4152 | STATIC I32 |
cea2e8a9 | 4153 | S_regrepeat_hard(pTHX_ regnode *p, I32 max, I32 *lp) |
c277df42 | 4154 | { |
b7953727 | 4155 | register char *scan = Nullch; |
c277df42 | 4156 | register char *start; |
3280af22 | 4157 | register char *loceol = PL_regeol; |
a0ed51b3 | 4158 | I32 l = 0; |
708e3b05 | 4159 | I32 count = 0, res = 1; |
a0ed51b3 LW |
4160 | |
4161 | if (!max) | |
4162 | return 0; | |
c277df42 | 4163 | |
3280af22 | 4164 | start = PL_reginput; |
53c4c00c | 4165 | if (PL_reg_match_utf8) { |
708e3b05 | 4166 | while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) { |
a0ed51b3 LW |
4167 | if (!count++) { |
4168 | l = 0; | |
4169 | while (start < PL_reginput) { | |
4170 | l++; | |
4171 | start += UTF8SKIP(start); | |
4172 | } | |
4173 | *lp = l; | |
4174 | if (l == 0) | |
4175 | return max; | |
4176 | } | |
4177 | if (count == max) | |
4178 | return count; | |
4179 | } | |
4180 | } | |
4181 | else { | |
708e3b05 | 4182 | while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) { |
a0ed51b3 LW |
4183 | if (!count++) { |
4184 | *lp = l = PL_reginput - start; | |
4185 | if (max != REG_INFTY && l*max < loceol - scan) | |
4186 | loceol = scan + l*max; | |
4187 | if (l == 0) | |
4188 | return max; | |
c277df42 IZ |
4189 | } |
4190 | } | |
4191 | } | |
708e3b05 | 4192 | if (!res) |
3280af22 | 4193 | PL_reginput = scan; |
9041c2e3 | 4194 | |
a0ed51b3 | 4195 | return count; |
c277df42 IZ |
4196 | } |
4197 | ||
4198 | /* | |
ffc61ed2 JH |
4199 | - regclass_swash - prepare the utf8 swash |
4200 | */ | |
4201 | ||
4202 | SV * | |
4203 | Perl_regclass_swash(pTHX_ register regnode* node, bool doinit, SV** initsvp) | |
4204 | { | |
4205 | SV *sw = NULL; | |
4206 | SV *si = NULL; | |
4207 | ||
4208 | if (PL_regdata && PL_regdata->count) { | |
4209 | U32 n = ARG(node); | |
4210 | ||
4211 | if (PL_regdata->what[n] == 's') { | |
4212 | SV *rv = (SV*)PL_regdata->data[n]; | |
4213 | AV *av = (AV*)SvRV((SV*)rv); | |
4214 | SV **a; | |
9041c2e3 | 4215 | |
ffc61ed2 JH |
4216 | si = *av_fetch(av, 0, FALSE); |
4217 | a = av_fetch(av, 1, FALSE); | |
9041c2e3 | 4218 | |
ffc61ed2 JH |
4219 | if (a) |
4220 | sw = *a; | |
4221 | else if (si && doinit) { | |
4222 | sw = swash_init("utf8", "", si, 1, 0); | |
4223 | (void)av_store(av, 1, sw); | |
4224 | } | |
4225 | } | |
4226 | } | |
4227 | ||
4228 | if (initsvp) | |
4229 | *initsvp = si; | |
4230 | ||
4231 | return sw; | |
4232 | } | |
4233 | ||
4234 | /* | |
cb8d8820 | 4235 | - reginclass - determine if a character falls into a character class |
bbce6d69 | 4236 | */ |
4237 | ||
76e3520e | 4238 | STATIC bool |
ffc61ed2 | 4239 | S_reginclass(pTHX_ register regnode *n, register U8* p, register bool do_utf8) |
bbce6d69 | 4240 | { |
ffc61ed2 | 4241 | char flags = ANYOF_FLAGS(n); |
bbce6d69 | 4242 | bool match = FALSE; |
1aa99e6b | 4243 | UV c; |
3568d838 | 4244 | STRLEN len = 0; |
1aa99e6b | 4245 | |
3568d838 | 4246 | c = do_utf8 ? utf8_to_uvchr(p, &len) : *p; |
bbce6d69 | 4247 | |
ffc61ed2 JH |
4248 | if (do_utf8 || (flags & ANYOF_UNICODE)) { |
4249 | if (do_utf8 && !ANYOF_RUNTIME(n)) { | |
ffc61ed2 JH |
4250 | if (len != (STRLEN)-1 && c < 256 && ANYOF_BITMAP_TEST(n, c)) |
4251 | match = TRUE; | |
bbce6d69 | 4252 | } |
3568d838 | 4253 | if (!match && do_utf8 && (flags & ANYOF_UNICODE_ALL) && c >= 256) |
1aa99e6b | 4254 | match = TRUE; |
ffc61ed2 JH |
4255 | if (!match) { |
4256 | SV *sw = regclass_swash(n, TRUE, 0); | |
4257 | ||
4258 | if (sw) { | |
3568d838 | 4259 | if (swash_fetch(sw, p, do_utf8)) |
ffc61ed2 JH |
4260 | match = TRUE; |
4261 | else if (flags & ANYOF_FOLD) { | |
a2a2844f | 4262 | STRLEN ulen; |
a5961de5 | 4263 | U8 tmpbuf[UTF8_MAXLEN_FOLD+1]; |
a2a2844f | 4264 | |
a5961de5 JH |
4265 | to_utf8_fold(p, tmpbuf, &ulen); |
4266 | if (swash_fetch(sw, tmpbuf, do_utf8)) | |
4267 | match = TRUE; | |
4268 | to_utf8_upper(p, tmpbuf, &ulen); | |
3568d838 | 4269 | if (swash_fetch(sw, tmpbuf, do_utf8)) |
ffc61ed2 JH |
4270 | match = TRUE; |
4271 | } | |
4272 | } | |
bbce6d69 | 4273 | } |
4274 | } | |
1aa99e6b | 4275 | if (!match && c < 256) { |
ffc61ed2 JH |
4276 | if (ANYOF_BITMAP_TEST(n, c)) |
4277 | match = TRUE; | |
4278 | else if (flags & ANYOF_FOLD) { | |
3568d838 | 4279 | I32 f; |
a0ed51b3 | 4280 | |
ffc61ed2 JH |
4281 | if (flags & ANYOF_LOCALE) { |
4282 | PL_reg_flags |= RF_tainted; | |
4283 | f = PL_fold_locale[c]; | |
4284 | } | |
4285 | else | |
4286 | f = PL_fold[c]; | |
4287 | if (f != c && ANYOF_BITMAP_TEST(n, f)) | |
4288 | match = TRUE; | |
4289 | } | |
4290 | ||
4291 | if (!match && (flags & ANYOF_CLASS)) { | |
a0ed51b3 | 4292 | PL_reg_flags |= RF_tainted; |
ffc61ed2 JH |
4293 | if ( |
4294 | (ANYOF_CLASS_TEST(n, ANYOF_ALNUM) && isALNUM_LC(c)) || | |
4295 | (ANYOF_CLASS_TEST(n, ANYOF_NALNUM) && !isALNUM_LC(c)) || | |
4296 | (ANYOF_CLASS_TEST(n, ANYOF_SPACE) && isSPACE_LC(c)) || | |
4297 | (ANYOF_CLASS_TEST(n, ANYOF_NSPACE) && !isSPACE_LC(c)) || | |
4298 | (ANYOF_CLASS_TEST(n, ANYOF_DIGIT) && isDIGIT_LC(c)) || | |
4299 | (ANYOF_CLASS_TEST(n, ANYOF_NDIGIT) && !isDIGIT_LC(c)) || | |
4300 | (ANYOF_CLASS_TEST(n, ANYOF_ALNUMC) && isALNUMC_LC(c)) || | |
4301 | (ANYOF_CLASS_TEST(n, ANYOF_NALNUMC) && !isALNUMC_LC(c)) || | |
4302 | (ANYOF_CLASS_TEST(n, ANYOF_ALPHA) && isALPHA_LC(c)) || | |
4303 | (ANYOF_CLASS_TEST(n, ANYOF_NALPHA) && !isALPHA_LC(c)) || | |
4304 | (ANYOF_CLASS_TEST(n, ANYOF_ASCII) && isASCII(c)) || | |
4305 | (ANYOF_CLASS_TEST(n, ANYOF_NASCII) && !isASCII(c)) || | |
4306 | (ANYOF_CLASS_TEST(n, ANYOF_CNTRL) && isCNTRL_LC(c)) || | |
4307 | (ANYOF_CLASS_TEST(n, ANYOF_NCNTRL) && !isCNTRL_LC(c)) || | |
4308 | (ANYOF_CLASS_TEST(n, ANYOF_GRAPH) && isGRAPH_LC(c)) || | |
4309 | (ANYOF_CLASS_TEST(n, ANYOF_NGRAPH) && !isGRAPH_LC(c)) || | |
4310 | (ANYOF_CLASS_TEST(n, ANYOF_LOWER) && isLOWER_LC(c)) || | |
4311 | (ANYOF_CLASS_TEST(n, ANYOF_NLOWER) && !isLOWER_LC(c)) || | |
4312 | (ANYOF_CLASS_TEST(n, ANYOF_PRINT) && isPRINT_LC(c)) || | |
4313 | (ANYOF_CLASS_TEST(n, ANYOF_NPRINT) && !isPRINT_LC(c)) || | |
4314 | (ANYOF_CLASS_TEST(n, ANYOF_PUNCT) && isPUNCT_LC(c)) || | |
4315 | (ANYOF_CLASS_TEST(n, ANYOF_NPUNCT) && !isPUNCT_LC(c)) || | |
4316 | (ANYOF_CLASS_TEST(n, ANYOF_UPPER) && isUPPER_LC(c)) || | |
4317 | (ANYOF_CLASS_TEST(n, ANYOF_NUPPER) && !isUPPER_LC(c)) || | |
4318 | (ANYOF_CLASS_TEST(n, ANYOF_XDIGIT) && isXDIGIT(c)) || | |
4319 | (ANYOF_CLASS_TEST(n, ANYOF_NXDIGIT) && !isXDIGIT(c)) || | |
4320 | (ANYOF_CLASS_TEST(n, ANYOF_PSXSPC) && isPSXSPC(c)) || | |
4321 | (ANYOF_CLASS_TEST(n, ANYOF_NPSXSPC) && !isPSXSPC(c)) || | |
4322 | (ANYOF_CLASS_TEST(n, ANYOF_BLANK) && isBLANK(c)) || | |
4323 | (ANYOF_CLASS_TEST(n, ANYOF_NBLANK) && !isBLANK(c)) | |
4324 | ) /* How's that for a conditional? */ | |
4325 | { | |
4326 | match = TRUE; | |
4327 | } | |
a0ed51b3 | 4328 | } |
a0ed51b3 LW |
4329 | } |
4330 | ||
a0ed51b3 LW |
4331 | return (flags & ANYOF_INVERT) ? !match : match; |
4332 | } | |
161b471a | 4333 | |
dfe13c55 | 4334 | STATIC U8 * |
cea2e8a9 | 4335 | S_reghop(pTHX_ U8 *s, I32 off) |
9041c2e3 | 4336 | { |
1aa99e6b IH |
4337 | return S_reghop3(aTHX_ s, off, (U8*)(off >= 0 ? PL_regeol : PL_bostr)); |
4338 | } | |
4339 | ||
4340 | STATIC U8 * | |
4341 | S_reghop3(pTHX_ U8 *s, I32 off, U8* lim) | |
9041c2e3 | 4342 | { |
a0ed51b3 | 4343 | if (off >= 0) { |
1aa99e6b | 4344 | while (off-- && s < lim) { |
ffc61ed2 | 4345 | /* XXX could check well-formedness here */ |
a0ed51b3 | 4346 | s += UTF8SKIP(s); |
ffc61ed2 | 4347 | } |
a0ed51b3 LW |
4348 | } |
4349 | else { | |
4350 | while (off++) { | |
1aa99e6b | 4351 | if (s > lim) { |
a0ed51b3 | 4352 | s--; |
ffc61ed2 | 4353 | if (UTF8_IS_CONTINUED(*s)) { |
1aa99e6b | 4354 | while (s > (U8*)lim && UTF8_IS_CONTINUATION(*s)) |
a0ed51b3 | 4355 | s--; |
ffc61ed2 JH |
4356 | } |
4357 | /* XXX could check well-formedness here */ | |
a0ed51b3 LW |
4358 | } |
4359 | } | |
4360 | } | |
4361 | return s; | |
4362 | } | |
161b471a | 4363 | |
dfe13c55 | 4364 | STATIC U8 * |
1aa99e6b | 4365 | S_reghopmaybe(pTHX_ U8 *s, I32 off) |
9041c2e3 | 4366 | { |
1aa99e6b IH |
4367 | return S_reghopmaybe3(aTHX_ s, off, (U8*)(off >= 0 ? PL_regeol : PL_bostr)); |
4368 | } | |
4369 | ||
4370 | STATIC U8 * | |
4371 | S_reghopmaybe3(pTHX_ U8* s, I32 off, U8* lim) | |
a0ed51b3 LW |
4372 | { |
4373 | if (off >= 0) { | |
1aa99e6b | 4374 | while (off-- && s < lim) { |
ffc61ed2 | 4375 | /* XXX could check well-formedness here */ |
a0ed51b3 | 4376 | s += UTF8SKIP(s); |
ffc61ed2 | 4377 | } |
a0ed51b3 LW |
4378 | if (off >= 0) |
4379 | return 0; | |
4380 | } | |
4381 | else { | |
4382 | while (off++) { | |
1aa99e6b | 4383 | if (s > lim) { |
a0ed51b3 | 4384 | s--; |
ffc61ed2 | 4385 | if (UTF8_IS_CONTINUED(*s)) { |
1aa99e6b | 4386 | while (s > (U8*)lim && UTF8_IS_CONTINUATION(*s)) |
a0ed51b3 | 4387 | s--; |
ffc61ed2 JH |
4388 | } |
4389 | /* XXX could check well-formedness here */ | |
a0ed51b3 LW |
4390 | } |
4391 | else | |
4392 | break; | |
4393 | } | |
4394 | if (off <= 0) | |
4395 | return 0; | |
4396 | } | |
4397 | return s; | |
4398 | } | |
51371543 | 4399 | |
51371543 | 4400 | static void |
acfe0abc | 4401 | restore_pos(pTHX_ void *arg) |
51371543 | 4402 | { |
51371543 GS |
4403 | if (PL_reg_eval_set) { |
4404 | if (PL_reg_oldsaved) { | |
4405 | PL_reg_re->subbeg = PL_reg_oldsaved; | |
4406 | PL_reg_re->sublen = PL_reg_oldsavedlen; | |
4407 | RX_MATCH_COPIED_on(PL_reg_re); | |
4408 | } | |
4409 | PL_reg_magic->mg_len = PL_reg_oldpos; | |
4410 | PL_reg_eval_set = 0; | |
4411 | PL_curpm = PL_reg_oldcurpm; | |
4412 | } | |
4413 | } |