Commit | Line | Data |
a0d0e21e |
1 | /* regexec.c |
2 | */ |
3 | |
4 | /* |
5 | * "One Ring to rule them all, One Ring to find them..." |
6 | */ |
7 | |
a687059c |
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 |
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 |
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 */ |
28 | # ifndef DEBUGGING |
29 | # define DEBUGGING |
30 | # endif |
31 | #endif |
32 | |
33 | #ifdef PERL_IN_XSUB_RE |
d06ea78c |
34 | /* We *really* need to overwrite these symbols: */ |
56953603 |
35 | # define Perl_regexec_flags my_regexec |
36 | # define Perl_regdump my_regdump |
37 | # define Perl_regprop my_regprop |
d06ea78c |
38 | /* *These* symbols are masked to allow static link. */ |
39 | # define Perl_pregexec my_pregexec |
56953603 |
40 | #endif |
41 | |
f0fcb552 |
42 | /*SUPPRESS 112*/ |
a687059c |
43 | /* |
e50aee73 |
44 | * pregcomp and pregexec -- regsub and regerror are not used in perl |
a687059c |
45 | * |
46 | * Copyright (c) 1986 by University of Toronto. |
47 | * Written by Henry Spencer. Not derived from licensed software. |
48 | * |
49 | * Permission is granted to anyone to use this software for any |
50 | * purpose on any computer system, and to redistribute it freely, |
51 | * subject to the following restrictions: |
52 | * |
53 | * 1. The author is not responsible for the consequences of use of |
54 | * this software, no matter how awful, even if they arise |
55 | * from defects in it. |
56 | * |
57 | * 2. The origin of this software must not be misrepresented, either |
58 | * by explicit claim or by omission. |
59 | * |
60 | * 3. Altered versions must be plainly marked as such, and must not |
61 | * be misrepresented as being the original software. |
62 | * |
63 | **** Alterations to Henry's code are... |
64 | **** |
9607fc9c |
65 | **** Copyright (c) 1991-1997, Larry Wall |
a687059c |
66 | **** |
9ef589d8 |
67 | **** You may distribute under the terms of either the GNU General Public |
68 | **** License or the Artistic License, as specified in the README file. |
a687059c |
69 | * |
70 | * Beware that some of this code is subtly aware of the way operator |
71 | * precedence is structured in regular expressions. Serious changes in |
72 | * regular-expression syntax might require a total rethink. |
73 | */ |
74 | #include "EXTERN.h" |
75 | #include "perl.h" |
76 | #include "regcomp.h" |
77 | |
c277df42 |
78 | #define RF_tainted 1 /* tainted information used? */ |
79 | #define RF_warned 2 /* warned about big count? */ |
ce862d02 |
80 | #define RF_evaled 4 /* Did an EVAL with setting? */ |
81 | |
82 | #define RS_init 1 /* eval environment created */ |
83 | #define RS_set 2 /* replsv value is set */ |
c277df42 |
84 | |
a687059c |
85 | #ifndef STATIC |
86 | #define STATIC static |
87 | #endif |
88 | |
76e3520e |
89 | #ifndef PERL_OBJECT |
a0d0e21e |
90 | typedef I32 CHECKPOINT; |
91 | |
c277df42 |
92 | /* |
93 | * Forwards. |
94 | */ |
95 | |
96 | static I32 regmatch _((regnode *prog)); |
97 | static I32 regrepeat _((regnode *p, I32 max)); |
98 | static I32 regrepeat_hard _((regnode *p, I32 max, I32 *lp)); |
99 | static I32 regtry _((regexp *prog, char *startpos)); |
ae5c130c |
100 | |
c277df42 |
101 | static bool reginclass _((char *p, I32 c)); |
55497cff |
102 | static CHECKPOINT regcppush _((I32 parenfloor)); |
103 | static char * regcppop _((void)); |
76e3520e |
104 | #endif |
ae5c130c |
105 | #define REGINCLASS(p,c) (*(p) ? reginclass(p,c) : ANYOF_TEST(p,c)) |
a0d0e21e |
106 | |
76e3520e |
107 | STATIC CHECKPOINT |
8ac85365 |
108 | regcppush(I32 parenfloor) |
a0d0e21e |
109 | { |
11343788 |
110 | dTHR; |
3280af22 |
111 | int retval = PL_savestack_ix; |
112 | int i = (PL_regsize - parenfloor) * 4; |
a0d0e21e |
113 | int p; |
114 | |
115 | SSCHECK(i + 5); |
3280af22 |
116 | for (p = PL_regsize; p > parenfloor; p--) { |
117 | SSPUSHPTR(PL_regendp[p]); |
118 | SSPUSHPTR(PL_regstartp[p]); |
119 | SSPUSHPTR(PL_reg_start_tmp[p]); |
a0d0e21e |
120 | SSPUSHINT(p); |
121 | } |
3280af22 |
122 | SSPUSHINT(PL_regsize); |
123 | SSPUSHINT(*PL_reglastparen); |
124 | SSPUSHPTR(PL_reginput); |
a0d0e21e |
125 | SSPUSHINT(i + 3); |
126 | SSPUSHINT(SAVEt_REGCONTEXT); |
127 | return retval; |
128 | } |
129 | |
c277df42 |
130 | /* These are needed since we do not localize EVAL nodes: */ |
c3464db5 |
131 | # define REGCP_SET DEBUG_r(PerlIO_printf(Perl_debug_log, \ |
132 | " Setting an EVAL scope, savestack=%i\n", \ |
3280af22 |
133 | PL_savestack_ix)); lastcp = PL_savestack_ix |
c3464db5 |
134 | |
3280af22 |
135 | # define REGCP_UNWIND DEBUG_r(lastcp != PL_savestack_ix ? \ |
c3464db5 |
136 | PerlIO_printf(Perl_debug_log, \ |
137 | " Clearing an EVAL scope, savestack=%i..%i\n", \ |
3280af22 |
138 | lastcp, PL_savestack_ix) : 0); regcpblow(lastcp) |
c277df42 |
139 | |
76e3520e |
140 | STATIC char * |
8ac85365 |
141 | regcppop(void) |
a0d0e21e |
142 | { |
11343788 |
143 | dTHR; |
a0d0e21e |
144 | I32 i = SSPOPINT; |
145 | U32 paren = 0; |
146 | char *input; |
147 | char *tmps; |
148 | assert(i == SAVEt_REGCONTEXT); |
149 | i = SSPOPINT; |
150 | input = (char *) SSPOPPTR; |
3280af22 |
151 | *PL_reglastparen = SSPOPINT; |
152 | PL_regsize = SSPOPINT; |
c277df42 |
153 | for (i -= 3; i > 0; i -= 4) { |
a0d0e21e |
154 | paren = (U32)SSPOPINT; |
3280af22 |
155 | PL_reg_start_tmp[paren] = (char *) SSPOPPTR; |
156 | PL_regstartp[paren] = (char *) SSPOPPTR; |
a0d0e21e |
157 | tmps = (char*)SSPOPPTR; |
3280af22 |
158 | if (paren <= *PL_reglastparen) |
159 | PL_regendp[paren] = tmps; |
c277df42 |
160 | DEBUG_r( |
c3464db5 |
161 | PerlIO_printf(Perl_debug_log, |
162 | " restoring \\%d to %d(%d)..%d%s\n", |
3280af22 |
163 | paren, PL_regstartp[paren] - PL_regbol, |
164 | PL_reg_start_tmp[paren] - PL_regbol, |
165 | PL_regendp[paren] - PL_regbol, |
166 | (paren > *PL_reglastparen ? "(no)" : "")); |
c277df42 |
167 | ); |
a0d0e21e |
168 | } |
c277df42 |
169 | DEBUG_r( |
3280af22 |
170 | if (*PL_reglastparen + 1 <= PL_regnpar) { |
c3464db5 |
171 | PerlIO_printf(Perl_debug_log, |
172 | " restoring \\%d..\\%d to undef\n", |
3280af22 |
173 | *PL_reglastparen + 1, PL_regnpar); |
c277df42 |
174 | } |
175 | ); |
3280af22 |
176 | for (paren = *PL_reglastparen + 1; paren <= PL_regnpar; paren++) { |
177 | if (paren > PL_regsize) |
178 | PL_regstartp[paren] = Nullch; |
179 | PL_regendp[paren] = Nullch; |
a0d0e21e |
180 | } |
181 | return input; |
182 | } |
183 | |
c277df42 |
184 | #define regcpblow(cp) LEAVE_SCOPE(cp) |
a0d0e21e |
185 | |
a687059c |
186 | /* |
e50aee73 |
187 | * pregexec and friends |
a687059c |
188 | */ |
189 | |
190 | /* |
c277df42 |
191 | - pregexec - match a regexp against a string |
a687059c |
192 | */ |
c277df42 |
193 | I32 |
c3464db5 |
194 | pregexec(register regexp *prog, char *stringarg, register char *strend, |
195 | char *strbeg, I32 minend, SV *screamer, U32 nosave) |
c277df42 |
196 | /* strend: pointer to null at end of string */ |
197 | /* strbeg: real beginning of string */ |
198 | /* minend: end of match must be >=minend after stringarg. */ |
199 | /* nosave: For optimizations. */ |
200 | { |
201 | return |
202 | regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL, |
203 | nosave ? 0 : REXEC_COPY_STR); |
204 | } |
205 | |
a687059c |
206 | /* |
c277df42 |
207 | - regexec_flags - match a regexp against a string |
a687059c |
208 | */ |
79072805 |
209 | I32 |
c3464db5 |
210 | regexec_flags(register regexp *prog, char *stringarg, register char *strend, |
211 | char *strbeg, I32 minend, SV *screamer, void *data, U32 flags) |
c277df42 |
212 | /* strend: pointer to null at end of string */ |
213 | /* strbeg: real beginning of string */ |
214 | /* minend: end of match must be >=minend after stringarg. */ |
215 | /* data: May be used for some additional optimizations. */ |
216 | /* nosave: For optimizations. */ |
a687059c |
217 | { |
5c0ca799 |
218 | dTHR; |
a0d0e21e |
219 | register char *s; |
c277df42 |
220 | register regnode *c; |
a0d0e21e |
221 | register char *startpos = stringarg; |
222 | register I32 tmp; |
c277df42 |
223 | I32 minlen; /* must match at least this many chars */ |
a0d0e21e |
224 | I32 dontbother = 0; /* how many characters not to try at end */ |
225 | CURCUR cc; |
c277df42 |
226 | I32 start_shift = 0; /* Offset of the start to find |
227 | constant substr. */ |
228 | I32 end_shift = 0; /* Same for the end. */ |
229 | I32 scream_pos = -1; /* Internal iterator of scream. */ |
230 | char *scream_olds; |
3280af22 |
231 | SV* oreplsv = GvSV(PL_replgv); |
a687059c |
232 | |
a0d0e21e |
233 | cc.cur = 0; |
4633a7c4 |
234 | cc.oldcc = 0; |
3280af22 |
235 | PL_regcc = &cc; |
a0d0e21e |
236 | |
3280af22 |
237 | PL_regprecomp = prog->precomp; /* Needed for error messages. */ |
a0d0e21e |
238 | #ifdef DEBUGGING |
3280af22 |
239 | PL_regnarrate = PL_debug & 512; |
240 | PL_regprogram = prog->program; |
a0d0e21e |
241 | #endif |
242 | |
243 | /* Be paranoid... */ |
244 | if (prog == NULL || startpos == NULL) { |
245 | croak("NULL regexp parameter"); |
246 | return 0; |
247 | } |
248 | |
c277df42 |
249 | minlen = prog->minlen; |
250 | if (strend - startpos < minlen) goto phooey; |
251 | |
a0d0e21e |
252 | if (startpos == strbeg) /* is ^ valid at stringarg? */ |
3280af22 |
253 | PL_regprev = '\n'; |
a0d0e21e |
254 | else { |
3280af22 |
255 | PL_regprev = stringarg[-1]; |
256 | if (!PL_multiline && PL_regprev == '\n') |
257 | PL_regprev = '\0'; /* force ^ to NOT match */ |
a0d0e21e |
258 | } |
bbce6d69 |
259 | |
a0d0e21e |
260 | /* Check validity of program. */ |
261 | if (UCHARAT(prog->program) != MAGIC) { |
262 | FAIL("corrupted regexp program"); |
263 | } |
264 | |
3280af22 |
265 | PL_regnpar = prog->nparens; |
266 | PL_reg_flags = 0; |
267 | PL_reg_eval_set = 0; |
a0d0e21e |
268 | |
269 | /* If there is a "must appear" string, look for it. */ |
270 | s = startpos; |
c277df42 |
271 | if (!(flags & REXEC_CHECKED) |
272 | && prog->check_substr != Nullsv && |
774d564b |
273 | !(prog->reganch & ROPT_ANCH_GPOS) && |
c277df42 |
274 | (!(prog->reganch & (ROPT_ANCH_BOL | ROPT_ANCH_MBOL)) |
3280af22 |
275 | || (PL_multiline && prog->check_substr == prog->anchored_substr)) ) |
a0d0e21e |
276 | { |
c277df42 |
277 | start_shift = prog->check_offset_min; |
278 | /* Should be nonnegative! */ |
279 | end_shift = minlen - start_shift - SvCUR(prog->check_substr); |
280 | if (screamer) { |
3280af22 |
281 | if (PL_screamfirst[BmRARE(prog->check_substr)] >= 0) |
c277df42 |
282 | s = screaminstr(screamer, prog->check_substr, |
283 | start_shift + (stringarg - strbeg), |
284 | end_shift, &scream_pos, 0); |
a0d0e21e |
285 | else |
286 | s = Nullch; |
c277df42 |
287 | scream_olds = s; |
0a12ae7d |
288 | } |
a0d0e21e |
289 | else |
c277df42 |
290 | s = fbm_instr((unsigned char*)s + start_shift, |
291 | (unsigned char*)strend - end_shift, |
411d5715 |
292 | prog->check_substr, 0); |
a0d0e21e |
293 | if (!s) { |
c277df42 |
294 | ++BmUSEFUL(prog->check_substr); /* hooray */ |
a0d0e21e |
295 | goto phooey; /* not present */ |
c277df42 |
296 | } else if ((s - stringarg) > prog->check_offset_max) { |
297 | ++BmUSEFUL(prog->check_substr); /* hooray/2 */ |
298 | s -= prog->check_offset_max; |
299 | } else if (!prog->naughty |
300 | && --BmUSEFUL(prog->check_substr) < 0 |
301 | && prog->check_substr == prog->float_substr) { /* boo */ |
302 | SvREFCNT_dec(prog->check_substr); |
303 | prog->check_substr = Nullsv; /* disable */ |
304 | prog->float_substr = Nullsv; /* clear */ |
a0d0e21e |
305 | s = startpos; |
c277df42 |
306 | } else s = startpos; |
a0d0e21e |
307 | } |
a687059c |
308 | |
c277df42 |
309 | /* Mark beginning of line for ^ and lookbehind. */ |
3280af22 |
310 | PL_regbol = startpos; |
311 | PL_bostr = strbeg; |
a687059c |
312 | |
a0d0e21e |
313 | /* Mark end of line for $ (and such) */ |
3280af22 |
314 | PL_regeol = strend; |
a687059c |
315 | |
a0d0e21e |
316 | /* see how far we have to get to not match where we matched before */ |
3280af22 |
317 | PL_regtill = startpos+minend; |
a687059c |
318 | |
c277df42 |
319 | DEBUG_r( |
320 | PerlIO_printf(Perl_debug_log, |
af819cba |
321 | "%sMatching%s `%s%.60s%s%s' against `%s%.*s%s%s'\n", |
322 | PL_colors[4],PL_colors[5],PL_colors[0], |
323 | prog->precomp, |
324 | PL_colors[1], |
c277df42 |
325 | (strlen(prog->precomp) > 60 ? "..." : ""), |
af819cba |
326 | PL_colors[0], |
c277df42 |
327 | (strend - startpos > 60 ? 60 : strend - startpos), |
af819cba |
328 | startpos, PL_colors[1], |
c277df42 |
329 | (strend - startpos > 60 ? "..." : "")) |
330 | ); |
331 | |
a0d0e21e |
332 | /* Simplest case: anchored match need be tried only once. */ |
774d564b |
333 | /* [unless only anchor is BOL and multiline is set] */ |
a0d0e21e |
334 | if (prog->reganch & ROPT_ANCH) { |
335 | if (regtry(prog, startpos)) |
336 | goto got_it; |
774d564b |
337 | else if (!(prog->reganch & ROPT_ANCH_GPOS) && |
3280af22 |
338 | (PL_multiline || (prog->reganch & ROPT_IMPLICIT) |
c277df42 |
339 | || (prog->reganch & ROPT_ANCH_MBOL))) |
774d564b |
340 | { |
a0d0e21e |
341 | if (minlen) |
342 | dontbother = minlen - 1; |
343 | strend -= dontbother; |
344 | /* for multiline we only have to try after newlines */ |
345 | if (s > startpos) |
346 | s--; |
347 | while (s < strend) { |
348 | if (*s++ == '\n') { |
349 | if (s < strend && regtry(prog, s)) |
350 | goto got_it; |
351 | } |
35c8bce7 |
352 | } |
35c8bce7 |
353 | } |
a0d0e21e |
354 | goto phooey; |
355 | } |
35c8bce7 |
356 | |
a0d0e21e |
357 | /* Messy cases: unanchored match. */ |
c277df42 |
358 | if (prog->anchored_substr && prog->reganch & ROPT_SKIP) { |
359 | /* we have /x+whatever/ */ |
360 | /* it must be a one character string */ |
361 | char ch = SvPVX(prog->anchored_substr)[0]; |
362 | while (s < strend) { |
363 | if (*s == ch) { |
364 | if (regtry(prog, s)) goto got_it; |
a0d0e21e |
365 | s++; |
c277df42 |
366 | while (s < strend && *s == ch) |
367 | s++; |
a0d0e21e |
368 | } |
c277df42 |
369 | s++; |
a687059c |
370 | } |
c277df42 |
371 | } |
372 | /*SUPPRESS 560*/ |
373 | else if (prog->anchored_substr != Nullsv |
374 | || (prog->float_substr != Nullsv |
375 | && prog->float_max_offset < strend - s)) { |
376 | SV *must = prog->anchored_substr |
377 | ? prog->anchored_substr : prog->float_substr; |
378 | I32 back_max = |
379 | prog->anchored_substr ? prog->anchored_offset : prog->float_max_offset; |
380 | I32 back_min = |
381 | prog->anchored_substr ? prog->anchored_offset : prog->float_min_offset; |
382 | I32 delta = back_max - back_min; |
383 | char *last = strend - SvCUR(must) - back_min; /* Cannot start after this */ |
384 | char *last1 = s - 1; /* Last position checked before */ |
385 | |
386 | /* XXXX check_substr already used to find `s', can optimize if |
387 | check_substr==must. */ |
388 | scream_pos = -1; |
389 | dontbother = end_shift; |
390 | strend -= dontbother; |
391 | while ( (s <= last) && |
392 | (screamer |
393 | ? (s = screaminstr(screamer, must, s + back_min - strbeg, |
394 | end_shift, &scream_pos, 0)) |
395 | : (s = fbm_instr((unsigned char*)s + back_min, |
411d5715 |
396 | (unsigned char*)strend, must, 0))) ) { |
c277df42 |
397 | if (s - back_max > last1) { |
398 | last1 = s - back_min; |
399 | s = s - back_max; |
400 | } else { |
401 | char *t = last1 + 1; |
402 | |
403 | last1 = s - back_min; |
404 | s = t; |
a0d0e21e |
405 | } |
c277df42 |
406 | while (s <= last1) { |
a0d0e21e |
407 | if (regtry(prog, s)) |
408 | goto got_it; |
409 | s++; |
410 | } |
411 | } |
412 | goto phooey; |
c277df42 |
413 | } else if (c = prog->regstclass) { |
a0d0e21e |
414 | I32 doevery = (prog->reganch & ROPT_SKIP) == 0; |
161b471a |
415 | char *Class; |
a687059c |
416 | |
a0d0e21e |
417 | if (minlen) |
418 | dontbother = minlen - 1; |
419 | strend -= dontbother; /* don't bother with what can't match */ |
420 | tmp = 1; |
421 | /* We know what class it must start with. */ |
422 | switch (OP(c)) { |
423 | case ANYOF: |
161b471a |
424 | Class = (char *) OPERAND(c); |
a0d0e21e |
425 | while (s < strend) { |
ae5c130c |
426 | if (REGINCLASS(Class, *s)) { |
a0d0e21e |
427 | if (tmp && regtry(prog, s)) |
428 | goto got_it; |
429 | else |
430 | tmp = doevery; |
a687059c |
431 | } |
a0d0e21e |
432 | else |
433 | tmp = 1; |
434 | s++; |
435 | } |
436 | break; |
bbce6d69 |
437 | case BOUNDL: |
3280af22 |
438 | PL_reg_flags |= RF_tainted; |
bbce6d69 |
439 | /* FALL THROUGH */ |
a0d0e21e |
440 | case BOUND: |
441 | if (minlen) |
442 | dontbother++,strend--; |
3280af22 |
443 | tmp = (s != startpos) ? UCHARAT(s - 1) : PL_regprev; |
95bac841 |
444 | tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0); |
a0d0e21e |
445 | while (s < strend) { |
95bac841 |
446 | if (tmp == !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) { |
a0d0e21e |
447 | tmp = !tmp; |
448 | if (regtry(prog, s)) |
449 | goto got_it; |
a687059c |
450 | } |
a0d0e21e |
451 | s++; |
452 | } |
453 | if ((minlen || tmp) && regtry(prog,s)) |
454 | goto got_it; |
455 | break; |
bbce6d69 |
456 | case NBOUNDL: |
3280af22 |
457 | PL_reg_flags |= RF_tainted; |
bbce6d69 |
458 | /* FALL THROUGH */ |
a0d0e21e |
459 | case NBOUND: |
460 | if (minlen) |
461 | dontbother++,strend--; |
3280af22 |
462 | tmp = (s != startpos) ? UCHARAT(s - 1) : PL_regprev; |
95bac841 |
463 | tmp = ((OP(c) == NBOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0); |
a0d0e21e |
464 | while (s < strend) { |
95bac841 |
465 | if (tmp == !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s))) |
a0d0e21e |
466 | tmp = !tmp; |
467 | else if (regtry(prog, s)) |
468 | goto got_it; |
469 | s++; |
470 | } |
471 | if ((minlen || !tmp) && regtry(prog,s)) |
472 | goto got_it; |
473 | break; |
474 | case ALNUM: |
475 | while (s < strend) { |
bbce6d69 |
476 | if (isALNUM(*s)) { |
477 | if (tmp && regtry(prog, s)) |
478 | goto got_it; |
479 | else |
480 | tmp = doevery; |
481 | } |
482 | else |
483 | tmp = 1; |
484 | s++; |
485 | } |
486 | break; |
487 | case ALNUML: |
3280af22 |
488 | PL_reg_flags |= RF_tainted; |
bbce6d69 |
489 | while (s < strend) { |
490 | if (isALNUM_LC(*s)) { |
a0d0e21e |
491 | if (tmp && regtry(prog, s)) |
492 | goto got_it; |
a687059c |
493 | else |
a0d0e21e |
494 | tmp = doevery; |
495 | } |
496 | else |
497 | tmp = 1; |
498 | s++; |
499 | } |
500 | break; |
501 | case NALNUM: |
502 | while (s < strend) { |
bbce6d69 |
503 | if (!isALNUM(*s)) { |
504 | if (tmp && regtry(prog, s)) |
505 | goto got_it; |
506 | else |
507 | tmp = doevery; |
508 | } |
509 | else |
510 | tmp = 1; |
511 | s++; |
512 | } |
513 | break; |
514 | case NALNUML: |
3280af22 |
515 | PL_reg_flags |= RF_tainted; |
bbce6d69 |
516 | while (s < strend) { |
517 | if (!isALNUM_LC(*s)) { |
a0d0e21e |
518 | if (tmp && regtry(prog, s)) |
519 | goto got_it; |
a687059c |
520 | else |
a0d0e21e |
521 | tmp = doevery; |
a687059c |
522 | } |
a0d0e21e |
523 | else |
524 | tmp = 1; |
525 | s++; |
526 | } |
527 | break; |
528 | case SPACE: |
529 | while (s < strend) { |
530 | if (isSPACE(*s)) { |
531 | if (tmp && regtry(prog, s)) |
532 | goto got_it; |
533 | else |
534 | tmp = doevery; |
2304df62 |
535 | } |
a0d0e21e |
536 | else |
537 | tmp = 1; |
538 | s++; |
539 | } |
540 | break; |
bbce6d69 |
541 | case SPACEL: |
3280af22 |
542 | PL_reg_flags |= RF_tainted; |
bbce6d69 |
543 | while (s < strend) { |
544 | if (isSPACE_LC(*s)) { |
545 | if (tmp && regtry(prog, s)) |
546 | goto got_it; |
547 | else |
548 | tmp = doevery; |
549 | } |
550 | else |
551 | tmp = 1; |
552 | s++; |
553 | } |
554 | break; |
a0d0e21e |
555 | case NSPACE: |
556 | while (s < strend) { |
557 | if (!isSPACE(*s)) { |
558 | if (tmp && regtry(prog, s)) |
559 | goto got_it; |
560 | else |
561 | tmp = doevery; |
a687059c |
562 | } |
a0d0e21e |
563 | else |
564 | tmp = 1; |
565 | s++; |
566 | } |
567 | break; |
bbce6d69 |
568 | case NSPACEL: |
3280af22 |
569 | PL_reg_flags |= RF_tainted; |
bbce6d69 |
570 | while (s < strend) { |
571 | if (!isSPACE_LC(*s)) { |
572 | if (tmp && regtry(prog, s)) |
573 | goto got_it; |
574 | else |
575 | tmp = doevery; |
576 | } |
577 | else |
578 | tmp = 1; |
579 | s++; |
580 | } |
581 | break; |
a0d0e21e |
582 | case DIGIT: |
583 | while (s < strend) { |
584 | if (isDIGIT(*s)) { |
585 | if (tmp && regtry(prog, s)) |
586 | goto got_it; |
587 | else |
588 | tmp = doevery; |
2b69d0c2 |
589 | } |
a0d0e21e |
590 | else |
591 | tmp = 1; |
592 | s++; |
593 | } |
594 | break; |
595 | case NDIGIT: |
596 | while (s < strend) { |
597 | if (!isDIGIT(*s)) { |
598 | if (tmp && regtry(prog, s)) |
599 | goto got_it; |
600 | else |
601 | tmp = doevery; |
a687059c |
602 | } |
a0d0e21e |
603 | else |
604 | tmp = 1; |
605 | s++; |
606 | } |
607 | break; |
a687059c |
608 | } |
a0d0e21e |
609 | } |
610 | else { |
c277df42 |
611 | dontbother = 0; |
612 | if (prog->float_substr != Nullsv) { /* Trim the end. */ |
613 | char *last; |
614 | I32 oldpos = scream_pos; |
615 | |
616 | if (screamer) { |
617 | last = screaminstr(screamer, prog->float_substr, s - strbeg, |
618 | end_shift, &scream_pos, 1); /* last one */ |
619 | if (!last) { |
620 | last = scream_olds; /* Only one occurence. */ |
621 | } |
622 | } else { |
623 | STRLEN len; |
624 | char *little = SvPV(prog->float_substr, len); |
19b4f81a |
625 | if (len) |
626 | last = rninstr(s, strend, little, little + len); |
627 | else |
628 | last = strend; /* matching `$' */ |
c277df42 |
629 | } |
630 | if (last == NULL) goto phooey; /* Should not happen! */ |
19b4f81a |
631 | dontbother = strend - last + prog->float_min_offset; |
c277df42 |
632 | } |
633 | if (minlen && (dontbother < minlen)) |
a0d0e21e |
634 | dontbother = minlen - 1; |
635 | strend -= dontbother; |
636 | /* We don't know much -- general case. */ |
637 | do { |
638 | if (regtry(prog, s)) |
639 | goto got_it; |
640 | } while (s++ < strend); |
641 | } |
642 | |
643 | /* Failure. */ |
644 | goto phooey; |
a687059c |
645 | |
a0d0e21e |
646 | got_it: |
647 | prog->subbeg = strbeg; |
19b4f81a |
648 | prog->subend = PL_regeol; /* strend may have been modified */ |
3280af22 |
649 | RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted); |
5f05dabc |
650 | |
651 | /* make sure $`, $&, $', and $digit will work later */ |
c277df42 |
652 | if (strbeg != prog->subbase) { /* second+ //g match. */ |
653 | if (!(flags & REXEC_COPY_STR)) { |
137443ea |
654 | if (prog->subbase) { |
655 | Safefree(prog->subbase); |
656 | prog->subbase = Nullch; |
657 | } |
658 | } |
659 | else { |
19b4f81a |
660 | I32 i = PL_regeol - startpos + (stringarg - strbeg); |
137443ea |
661 | s = savepvn(strbeg, i); |
662 | Safefree(prog->subbase); |
663 | prog->subbase = s; |
664 | prog->subbeg = prog->subbase; |
665 | prog->subend = prog->subbase + i; |
666 | s = prog->subbase + (stringarg - strbeg); |
667 | for (i = 0; i <= prog->nparens; i++) { |
668 | if (prog->endp[i]) { |
669 | prog->startp[i] = s + (prog->startp[i] - startpos); |
670 | prog->endp[i] = s + (prog->endp[i] - startpos); |
671 | } |
a0d0e21e |
672 | } |
673 | } |
a0d0e21e |
674 | } |
ce862d02 |
675 | /* Preserve the current value of $^R */ |
3280af22 |
676 | if (oreplsv != GvSV(PL_replgv)) { |
677 | sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is |
ce862d02 |
678 | restored, the value remains |
679 | the same. */ |
680 | } |
a0d0e21e |
681 | return 1; |
682 | |
683 | phooey: |
a0d0e21e |
684 | return 0; |
a687059c |
685 | } |
686 | |
687 | /* |
688 | - regtry - try match at specific point |
689 | */ |
76e3520e |
690 | STATIC I32 /* 0 failure, 1 success */ |
8ac85365 |
691 | regtry(regexp *prog, char *startpos) |
a687059c |
692 | { |
c277df42 |
693 | dTHR; |
a0d0e21e |
694 | register I32 i; |
695 | register char **sp; |
696 | register char **ep; |
c277df42 |
697 | CHECKPOINT lastcp; |
a0d0e21e |
698 | |
3280af22 |
699 | if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) { |
700 | PL_reg_eval_set = RS_init; |
ce862d02 |
701 | DEBUG_r(DEBUG_s( |
c3464db5 |
702 | PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %i\n", |
3280af22 |
703 | PL_stack_sp - PL_stack_base); |
ce862d02 |
704 | )); |
705 | SAVEINT(cxstack[cxstack_ix].blk_oldsp); |
3280af22 |
706 | cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base; |
ce862d02 |
707 | /* Otherwise OP_NEXTSTATE will free whatever on stack now. */ |
708 | SAVETMPS; |
709 | /* Apparently this is not needed, judging by wantarray. */ |
710 | /* SAVEINT(cxstack[cxstack_ix].blk_gimme); |
711 | cxstack[cxstack_ix].blk_gimme = G_SCALAR; */ |
712 | } |
3280af22 |
713 | PL_reginput = startpos; |
714 | PL_regstartp = prog->startp; |
715 | PL_regendp = prog->endp; |
716 | PL_reglastparen = &prog->lastparen; |
a0d0e21e |
717 | prog->lastparen = 0; |
3280af22 |
718 | PL_regsize = 0; |
719 | if (PL_reg_start_tmpl <= prog->nparens) { |
720 | PL_reg_start_tmpl = prog->nparens*3/2 + 3; |
721 | if(PL_reg_start_tmp) |
722 | Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*); |
c277df42 |
723 | else |
3280af22 |
724 | New(22,PL_reg_start_tmp, PL_reg_start_tmpl, char*); |
c277df42 |
725 | } |
a0d0e21e |
726 | |
727 | sp = prog->startp; |
728 | ep = prog->endp; |
3280af22 |
729 | PL_regdata = prog->data; |
a0d0e21e |
730 | if (prog->nparens) { |
731 | for (i = prog->nparens; i >= 0; i--) { |
732 | *sp++ = NULL; |
733 | *ep++ = NULL; |
a687059c |
734 | } |
a0d0e21e |
735 | } |
c277df42 |
736 | REGCP_SET; |
7e5428c5 |
737 | if (regmatch(prog->program + 1)) { |
a0d0e21e |
738 | prog->startp[0] = startpos; |
3280af22 |
739 | prog->endp[0] = PL_reginput; |
a0d0e21e |
740 | return 1; |
741 | } |
c277df42 |
742 | REGCP_UNWIND; |
743 | return 0; |
a687059c |
744 | } |
745 | |
746 | /* |
747 | - regmatch - main matching routine |
748 | * |
749 | * Conceptually the strategy is simple: check to see whether the current |
750 | * node matches, call self recursively to see whether the rest matches, |
751 | * and then act accordingly. In practice we make some effort to avoid |
752 | * recursion, in particular by going through "ordinary" nodes (that don't |
753 | * need to know whether the rest of the match failed) by a loop instead of |
754 | * by recursion. |
755 | */ |
756 | /* [lwall] I've hoisted the register declarations to the outer block in order to |
757 | * maybe save a little bit of pushing and popping on the stack. It also takes |
758 | * advantage of machines that use a register save mask on subroutine entry. |
759 | */ |
76e3520e |
760 | STATIC I32 /* 0 failure, 1 success */ |
c277df42 |
761 | regmatch(regnode *prog) |
a687059c |
762 | { |
c277df42 |
763 | dTHR; |
764 | register regnode *scan; /* Current node. */ |
765 | regnode *next; /* Next node. */ |
766 | regnode *inner; /* Next node in internal branch. */ |
c3464db5 |
767 | register I32 nextchr; /* renamed nextchr - nextchar colides with |
768 | function of same name */ |
a0d0e21e |
769 | register I32 n; /* no or next */ |
770 | register I32 ln; /* len or last */ |
771 | register char *s; /* operand or save */ |
3280af22 |
772 | register char *locinput = PL_reginput; |
c277df42 |
773 | register I32 c1, c2, paren; /* case fold search, parenth */ |
774 | int minmod = 0, sw = 0, logical = 0; |
4633a7c4 |
775 | #ifdef DEBUGGING |
3280af22 |
776 | PL_regindent++; |
4633a7c4 |
777 | #endif |
a0d0e21e |
778 | |
76e3520e |
779 | nextchr = UCHARAT(locinput); |
a0d0e21e |
780 | scan = prog; |
781 | while (scan != NULL) { |
c277df42 |
782 | #define sayNO_L (logical ? (logical = 0, sw = 0, goto cont) : sayNO) |
a687059c |
783 | #ifdef DEBUGGING |
c277df42 |
784 | # define sayYES goto yes |
785 | # define sayNO goto no |
786 | # define saySAME(x) if (x) goto yes; else goto no |
787 | # define REPORT_CODE_OFF 24 |
4633a7c4 |
788 | #else |
c277df42 |
789 | # define sayYES return 1 |
790 | # define sayNO return 0 |
791 | # define saySAME(x) return x |
a687059c |
792 | #endif |
c277df42 |
793 | DEBUG_r( { |
794 | SV *prop = sv_newmortal(); |
3280af22 |
795 | int docolor = *PL_colors[0]; |
c277df42 |
796 | int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */ |
3280af22 |
797 | int l = (PL_regeol - locinput > taill ? taill : PL_regeol - locinput); |
798 | int pref_len = (locinput - PL_bostr > (5 + taill) - l |
799 | ? (5 + taill) - l : locinput - PL_bostr); |
af819cba |
800 | int pref0_len = pref_len - (locinput - PL_reginput); |
c277df42 |
801 | |
3280af22 |
802 | if (l + pref_len < (5 + taill) && l < PL_regeol - locinput) |
803 | l = ( PL_regeol - locinput > (5 + taill) - pref_len |
804 | ? (5 + taill) - pref_len : PL_regeol - locinput); |
af819cba |
805 | if (pref0_len < 0) |
806 | pref0_len = 0; |
c277df42 |
807 | regprop(prop, scan); |
808 | PerlIO_printf(Perl_debug_log, |
af819cba |
809 | "%4i <%s%.*s%s%s%.*s%s%s%s%.*s%s>%*s|%3d:%*s%s\n", |
3280af22 |
810 | locinput - PL_bostr, |
af819cba |
811 | PL_colors[4], pref0_len, |
812 | locinput - pref_len, PL_colors[5], |
813 | PL_colors[2], pref_len - pref0_len, |
814 | locinput - pref_len + pref0_len, PL_colors[3], |
c277df42 |
815 | (docolor ? "" : "> <"), |
3280af22 |
816 | PL_colors[0], l, locinput, PL_colors[1], |
c277df42 |
817 | 15 - l - pref_len + 1, |
818 | "", |
3280af22 |
819 | scan - PL_regprogram, PL_regindent*2, "", |
c277df42 |
820 | SvPVX(prop)); |
821 | } ); |
a687059c |
822 | |
c277df42 |
823 | next = scan + NEXT_OFF(scan); |
a0d0e21e |
824 | if (next == scan) |
825 | next = NULL; |
a687059c |
826 | |
a0d0e21e |
827 | switch (OP(scan)) { |
828 | case BOL: |
3280af22 |
829 | if (locinput == PL_bostr |
830 | ? PL_regprev == '\n' |
831 | : (PL_multiline && |
832 | (nextchr || locinput < PL_regeol) && locinput[-1] == '\n') ) |
a0d0e21e |
833 | { |
6b88bc9c |
834 | /* PL_regtill = PL_regbol; */ |
a0d0e21e |
835 | break; |
836 | } |
4633a7c4 |
837 | sayNO; |
a0d0e21e |
838 | case MBOL: |
3280af22 |
839 | if (locinput == PL_bostr |
840 | ? PL_regprev == '\n' |
841 | : ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n') ) |
a0d0e21e |
842 | { |
843 | break; |
844 | } |
4633a7c4 |
845 | sayNO; |
a0d0e21e |
846 | case SBOL: |
3280af22 |
847 | if (locinput == PL_regbol && PL_regprev == '\n') |
a0d0e21e |
848 | break; |
4633a7c4 |
849 | sayNO; |
774d564b |
850 | case GPOS: |
3280af22 |
851 | if (locinput == PL_regbol) |
a0d0e21e |
852 | break; |
4633a7c4 |
853 | sayNO; |
a0d0e21e |
854 | case EOL: |
3280af22 |
855 | if (PL_multiline) |
a0d0e21e |
856 | goto meol; |
857 | else |
858 | goto seol; |
859 | case MEOL: |
860 | meol: |
3280af22 |
861 | if ((nextchr || locinput < PL_regeol) && nextchr != '\n') |
4633a7c4 |
862 | sayNO; |
a0d0e21e |
863 | break; |
864 | case SEOL: |
865 | seol: |
3280af22 |
866 | if ((nextchr || locinput < PL_regeol) && nextchr != '\n') |
4633a7c4 |
867 | sayNO; |
3280af22 |
868 | if (PL_regeol - locinput > 1) |
4633a7c4 |
869 | sayNO; |
a0d0e21e |
870 | break; |
b85d18e9 |
871 | case EOS: |
3280af22 |
872 | if (PL_regeol != locinput) |
b85d18e9 |
873 | sayNO; |
874 | break; |
a0d0e21e |
875 | case SANY: |
3280af22 |
876 | if (!nextchr && locinput >= PL_regeol) |
4633a7c4 |
877 | sayNO; |
76e3520e |
878 | nextchr = UCHARAT(++locinput); |
a0d0e21e |
879 | break; |
880 | case ANY: |
3280af22 |
881 | if (!nextchr && locinput >= PL_regeol || nextchr == '\n') |
4633a7c4 |
882 | sayNO; |
76e3520e |
883 | nextchr = UCHARAT(++locinput); |
a0d0e21e |
884 | break; |
bbce6d69 |
885 | case EXACT: |
161b471a |
886 | s = (char *) OPERAND(scan); |
c277df42 |
887 | ln = UCHARAT(s++); |
a0d0e21e |
888 | /* Inline the first character, for speed. */ |
76e3520e |
889 | if (UCHARAT(s) != nextchr) |
4633a7c4 |
890 | sayNO; |
3280af22 |
891 | if (PL_regeol - locinput < ln) |
4633a7c4 |
892 | sayNO; |
36477c24 |
893 | if (ln > 1 && memNE(s, locinput, ln)) |
4633a7c4 |
894 | sayNO; |
a0d0e21e |
895 | locinput += ln; |
76e3520e |
896 | nextchr = UCHARAT(locinput); |
bbce6d69 |
897 | break; |
898 | case EXACTFL: |
3280af22 |
899 | PL_reg_flags |= RF_tainted; |
bbce6d69 |
900 | /* FALL THROUGH */ |
901 | case EXACTF: |
161b471a |
902 | s = (char *) OPERAND(scan); |
c277df42 |
903 | ln = UCHARAT(s++); |
bbce6d69 |
904 | /* Inline the first character, for speed. */ |
76e3520e |
905 | if (UCHARAT(s) != nextchr && |
bbce6d69 |
906 | UCHARAT(s) != ((OP(scan) == EXACTF) |
76e3520e |
907 | ? fold : fold_locale)[nextchr]) |
bbce6d69 |
908 | sayNO; |
3280af22 |
909 | if (PL_regeol - locinput < ln) |
bbce6d69 |
910 | sayNO; |
5f05dabc |
911 | if (ln > 1 && (OP(scan) == EXACTF |
912 | ? ibcmp(s, locinput, ln) |
913 | : ibcmp_locale(s, locinput, ln))) |
bbce6d69 |
914 | sayNO; |
915 | locinput += ln; |
76e3520e |
916 | nextchr = UCHARAT(locinput); |
a0d0e21e |
917 | break; |
918 | case ANYOF: |
161b471a |
919 | s = (char *) OPERAND(scan); |
76e3520e |
920 | if (nextchr < 0) |
921 | nextchr = UCHARAT(locinput); |
873ef191 |
922 | if (!REGINCLASS(s, nextchr)) |
4633a7c4 |
923 | sayNO; |
3280af22 |
924 | if (!nextchr && locinput >= PL_regeol) |
4633a7c4 |
925 | sayNO; |
76e3520e |
926 | nextchr = UCHARAT(++locinput); |
a0d0e21e |
927 | break; |
bbce6d69 |
928 | case ALNUML: |
3280af22 |
929 | PL_reg_flags |= RF_tainted; |
bbce6d69 |
930 | /* FALL THROUGH */ |
a0d0e21e |
931 | case ALNUM: |
76e3520e |
932 | if (!nextchr) |
4633a7c4 |
933 | sayNO; |
bbce6d69 |
934 | if (!(OP(scan) == ALNUM |
76e3520e |
935 | ? isALNUM(nextchr) : isALNUM_LC(nextchr))) |
4633a7c4 |
936 | sayNO; |
76e3520e |
937 | nextchr = UCHARAT(++locinput); |
a0d0e21e |
938 | break; |
bbce6d69 |
939 | case NALNUML: |
3280af22 |
940 | PL_reg_flags |= RF_tainted; |
bbce6d69 |
941 | /* FALL THROUGH */ |
a0d0e21e |
942 | case NALNUM: |
3280af22 |
943 | if (!nextchr && locinput >= PL_regeol) |
4633a7c4 |
944 | sayNO; |
bbce6d69 |
945 | if (OP(scan) == NALNUM |
76e3520e |
946 | ? isALNUM(nextchr) : isALNUM_LC(nextchr)) |
4633a7c4 |
947 | sayNO; |
76e3520e |
948 | nextchr = UCHARAT(++locinput); |
a0d0e21e |
949 | break; |
bbce6d69 |
950 | case BOUNDL: |
951 | case NBOUNDL: |
3280af22 |
952 | PL_reg_flags |= RF_tainted; |
bbce6d69 |
953 | /* FALL THROUGH */ |
a0d0e21e |
954 | case BOUND: |
bbce6d69 |
955 | case NBOUND: |
956 | /* was last char in word? */ |
3280af22 |
957 | ln = (locinput != PL_regbol) ? UCHARAT(locinput - 1) : PL_regprev; |
bbce6d69 |
958 | if (OP(scan) == BOUND || OP(scan) == NBOUND) { |
959 | ln = isALNUM(ln); |
76e3520e |
960 | n = isALNUM(nextchr); |
bbce6d69 |
961 | } |
962 | else { |
963 | ln = isALNUM_LC(ln); |
76e3520e |
964 | n = isALNUM_LC(nextchr); |
bbce6d69 |
965 | } |
95bac841 |
966 | if (((!ln) == (!n)) == (OP(scan) == BOUND || OP(scan) == BOUNDL)) |
4633a7c4 |
967 | sayNO; |
a0d0e21e |
968 | break; |
bbce6d69 |
969 | case SPACEL: |
3280af22 |
970 | PL_reg_flags |= RF_tainted; |
bbce6d69 |
971 | /* FALL THROUGH */ |
a0d0e21e |
972 | case SPACE: |
3280af22 |
973 | if (!nextchr && locinput >= PL_regeol) |
4633a7c4 |
974 | sayNO; |
bbce6d69 |
975 | if (!(OP(scan) == SPACE |
76e3520e |
976 | ? isSPACE(nextchr) : isSPACE_LC(nextchr))) |
4633a7c4 |
977 | sayNO; |
76e3520e |
978 | nextchr = UCHARAT(++locinput); |
a0d0e21e |
979 | break; |
bbce6d69 |
980 | case NSPACEL: |
3280af22 |
981 | PL_reg_flags |= RF_tainted; |
bbce6d69 |
982 | /* FALL THROUGH */ |
a0d0e21e |
983 | case NSPACE: |
76e3520e |
984 | if (!nextchr) |
4633a7c4 |
985 | sayNO; |
bbce6d69 |
986 | if (OP(scan) == SPACE |
76e3520e |
987 | ? isSPACE(nextchr) : isSPACE_LC(nextchr)) |
4633a7c4 |
988 | sayNO; |
76e3520e |
989 | nextchr = UCHARAT(++locinput); |
a0d0e21e |
990 | break; |
991 | case DIGIT: |
76e3520e |
992 | if (!isDIGIT(nextchr)) |
4633a7c4 |
993 | sayNO; |
76e3520e |
994 | nextchr = UCHARAT(++locinput); |
a0d0e21e |
995 | break; |
996 | case NDIGIT: |
3280af22 |
997 | if (!nextchr && locinput >= PL_regeol) |
4633a7c4 |
998 | sayNO; |
76e3520e |
999 | if (isDIGIT(nextchr)) |
4633a7c4 |
1000 | sayNO; |
76e3520e |
1001 | nextchr = UCHARAT(++locinput); |
a0d0e21e |
1002 | break; |
c8756f30 |
1003 | case REFFL: |
3280af22 |
1004 | PL_reg_flags |= RF_tainted; |
c8756f30 |
1005 | /* FALL THROUGH */ |
c277df42 |
1006 | case REF: |
c8756f30 |
1007 | case REFF: |
c277df42 |
1008 | n = ARG(scan); /* which paren pair */ |
3280af22 |
1009 | s = PL_regstartp[n]; |
1010 | if (*PL_reglastparen < n || !s) |
af3f8c16 |
1011 | sayNO; /* Do not match unless seen CLOSEn. */ |
3280af22 |
1012 | if (s == PL_regendp[n]) |
a0d0e21e |
1013 | break; |
1014 | /* Inline the first character, for speed. */ |
76e3520e |
1015 | if (UCHARAT(s) != nextchr && |
c8756f30 |
1016 | (OP(scan) == REF || |
1017 | (UCHARAT(s) != ((OP(scan) == REFF |
76e3520e |
1018 | ? fold : fold_locale)[nextchr])))) |
4633a7c4 |
1019 | sayNO; |
3280af22 |
1020 | ln = PL_regendp[n] - s; |
1021 | if (locinput + ln > PL_regeol) |
4633a7c4 |
1022 | sayNO; |
c8756f30 |
1023 | if (ln > 1 && (OP(scan) == REF |
1024 | ? memNE(s, locinput, ln) |
1025 | : (OP(scan) == REFF |
1026 | ? ibcmp(s, locinput, ln) |
1027 | : ibcmp_locale(s, locinput, ln)))) |
4633a7c4 |
1028 | sayNO; |
a0d0e21e |
1029 | locinput += ln; |
76e3520e |
1030 | nextchr = UCHARAT(locinput); |
a0d0e21e |
1031 | break; |
1032 | |
1033 | case NOTHING: |
c277df42 |
1034 | case TAIL: |
a0d0e21e |
1035 | break; |
1036 | case BACK: |
1037 | break; |
c277df42 |
1038 | case EVAL: |
1039 | { |
1040 | dSP; |
533c011a |
1041 | OP_4tree *oop = PL_op; |
3280af22 |
1042 | COP *ocurcop = PL_curcop; |
1043 | SV **ocurpad = PL_curpad; |
c277df42 |
1044 | SV *ret; |
1045 | |
1046 | n = ARG(scan); |
533c011a |
1047 | PL_op = (OP_4tree*)PL_regdata->data[n]; |
1048 | DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%x\n", PL_op) ); |
3280af22 |
1049 | PL_curpad = AvARRAY((AV*)PL_regdata->data[n + 1]); |
c277df42 |
1050 | |
76e3520e |
1051 | CALLRUNOPS(); /* Scalar context. */ |
c277df42 |
1052 | SPAGAIN; |
1053 | ret = POPs; |
1054 | PUTBACK; |
1055 | |
1056 | if (logical) { |
1057 | logical = 0; |
1058 | sw = SvTRUE(ret); |
ce862d02 |
1059 | } else |
3280af22 |
1060 | sv_setsv(save_scalar(PL_replgv), ret); |
533c011a |
1061 | PL_op = oop; |
3280af22 |
1062 | PL_curpad = ocurpad; |
1063 | PL_curcop = ocurcop; |
c277df42 |
1064 | break; |
1065 | } |
a0d0e21e |
1066 | case OPEN: |
c277df42 |
1067 | n = ARG(scan); /* which paren pair */ |
3280af22 |
1068 | PL_reg_start_tmp[n] = locinput; |
1069 | if (n > PL_regsize) |
1070 | PL_regsize = n; |
a0d0e21e |
1071 | break; |
1072 | case CLOSE: |
c277df42 |
1073 | n = ARG(scan); /* which paren pair */ |
3280af22 |
1074 | PL_regstartp[n] = PL_reg_start_tmp[n]; |
1075 | PL_regendp[n] = locinput; |
1076 | if (n > *PL_reglastparen) |
1077 | *PL_reglastparen = n; |
a0d0e21e |
1078 | break; |
c277df42 |
1079 | case GROUPP: |
1080 | n = ARG(scan); /* which paren pair */ |
3280af22 |
1081 | sw = (*PL_reglastparen >= n && PL_regendp[n] != NULL); |
c277df42 |
1082 | break; |
1083 | case IFTHEN: |
1084 | if (sw) |
1085 | next = NEXTOPER(NEXTOPER(scan)); |
1086 | else { |
1087 | next = scan + ARG(scan); |
1088 | if (OP(next) == IFTHEN) /* Fake one. */ |
1089 | next = NEXTOPER(NEXTOPER(next)); |
1090 | } |
1091 | break; |
1092 | case LOGICAL: |
1093 | logical = 1; |
1094 | break; |
a0d0e21e |
1095 | case CURLYX: { |
1096 | CURCUR cc; |
3280af22 |
1097 | CHECKPOINT cp = PL_savestack_ix; |
c277df42 |
1098 | |
1099 | if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */ |
1100 | next += ARG(next); |
3280af22 |
1101 | cc.oldcc = PL_regcc; |
1102 | PL_regcc = &cc; |
1103 | cc.parenfloor = *PL_reglastparen; |
a0d0e21e |
1104 | cc.cur = -1; |
1105 | cc.min = ARG1(scan); |
1106 | cc.max = ARG2(scan); |
c277df42 |
1107 | cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS; |
a0d0e21e |
1108 | cc.next = next; |
1109 | cc.minmod = minmod; |
1110 | cc.lastloc = 0; |
3280af22 |
1111 | PL_reginput = locinput; |
a0d0e21e |
1112 | n = regmatch(PREVOPER(next)); /* start on the WHILEM */ |
1113 | regcpblow(cp); |
3280af22 |
1114 | PL_regcc = cc.oldcc; |
4633a7c4 |
1115 | saySAME(n); |
a0d0e21e |
1116 | } |
1117 | /* NOT REACHED */ |
1118 | case WHILEM: { |
1119 | /* |
1120 | * This is really hard to understand, because after we match |
1121 | * what we're trying to match, we must make sure the rest of |
1122 | * the RE is going to match for sure, and to do that we have |
1123 | * to go back UP the parse tree by recursing ever deeper. And |
1124 | * if it fails, we have to reset our parent's current state |
1125 | * that we can try again after backing off. |
1126 | */ |
1127 | |
c277df42 |
1128 | CHECKPOINT cp, lastcp; |
3280af22 |
1129 | CURCUR* cc = PL_regcc; |
c277df42 |
1130 | char *lastloc = cc->lastloc; /* Detection of 0-len. */ |
1131 | |
4633a7c4 |
1132 | n = cc->cur + 1; /* how many we know we matched */ |
3280af22 |
1133 | PL_reginput = locinput; |
a0d0e21e |
1134 | |
c277df42 |
1135 | DEBUG_r( |
1136 | PerlIO_printf(Perl_debug_log, |
1137 | "%*s %ld out of %ld..%ld cc=%lx\n", |
3280af22 |
1138 | REPORT_CODE_OFF+PL_regindent*2, "", |
c277df42 |
1139 | (long)n, (long)cc->min, |
1140 | (long)cc->max, (long)cc) |
1141 | ); |
4633a7c4 |
1142 | |
a0d0e21e |
1143 | /* If degenerate scan matches "", assume scan done. */ |
1144 | |
579cf2c3 |
1145 | if (locinput == cc->lastloc && n >= cc->min) { |
3280af22 |
1146 | PL_regcc = cc->oldcc; |
1147 | ln = PL_regcc->cur; |
c277df42 |
1148 | DEBUG_r( |
c3464db5 |
1149 | PerlIO_printf(Perl_debug_log, |
1150 | "%*s empty match detected, try continuation...\n", |
3280af22 |
1151 | REPORT_CODE_OFF+PL_regindent*2, "") |
c277df42 |
1152 | ); |
a0d0e21e |
1153 | if (regmatch(cc->next)) |
4633a7c4 |
1154 | sayYES; |
c277df42 |
1155 | DEBUG_r( |
c3464db5 |
1156 | PerlIO_printf(Perl_debug_log, |
1157 | "%*s failed...\n", |
3280af22 |
1158 | REPORT_CODE_OFF+PL_regindent*2, "") |
c277df42 |
1159 | ); |
3280af22 |
1160 | PL_regcc->cur = ln; |
1161 | PL_regcc = cc; |
4633a7c4 |
1162 | sayNO; |
a0d0e21e |
1163 | } |
1164 | |
1165 | /* First just match a string of min scans. */ |
1166 | |
1167 | if (n < cc->min) { |
1168 | cc->cur = n; |
1169 | cc->lastloc = locinput; |
4633a7c4 |
1170 | if (regmatch(cc->scan)) |
1171 | sayYES; |
1172 | cc->cur = n - 1; |
c277df42 |
1173 | cc->lastloc = lastloc; |
1174 | DEBUG_r( |
c3464db5 |
1175 | PerlIO_printf(Perl_debug_log, |
1176 | "%*s failed...\n", |
3280af22 |
1177 | REPORT_CODE_OFF+PL_regindent*2, "") |
c277df42 |
1178 | ); |
4633a7c4 |
1179 | sayNO; |
a0d0e21e |
1180 | } |
1181 | |
1182 | /* Prefer next over scan for minimal matching. */ |
1183 | |
1184 | if (cc->minmod) { |
3280af22 |
1185 | PL_regcc = cc->oldcc; |
1186 | ln = PL_regcc->cur; |
5f05dabc |
1187 | cp = regcppush(cc->parenfloor); |
c277df42 |
1188 | REGCP_SET; |
5f05dabc |
1189 | if (regmatch(cc->next)) { |
c277df42 |
1190 | regcpblow(cp); |
4633a7c4 |
1191 | sayYES; /* All done. */ |
5f05dabc |
1192 | } |
c277df42 |
1193 | REGCP_UNWIND; |
5f05dabc |
1194 | regcppop(); |
3280af22 |
1195 | PL_regcc->cur = ln; |
1196 | PL_regcc = cc; |
a0d0e21e |
1197 | |
c277df42 |
1198 | if (n >= cc->max) { /* Maximum greed exceeded? */ |
3280af22 |
1199 | if (PL_dowarn && n >= REG_INFTY |
1200 | && !(PL_reg_flags & RF_warned)) { |
1201 | PL_reg_flags |= RF_warned; |
2f3ca594 |
1202 | warn("%s limit (%d) exceeded", |
1203 | "Complex regular subexpression recursion", |
1204 | REG_INFTY - 1); |
c277df42 |
1205 | } |
4633a7c4 |
1206 | sayNO; |
c277df42 |
1207 | } |
a687059c |
1208 | |
c277df42 |
1209 | DEBUG_r( |
c3464db5 |
1210 | PerlIO_printf(Perl_debug_log, |
1211 | "%*s trying longer...\n", |
3280af22 |
1212 | REPORT_CODE_OFF+PL_regindent*2, "") |
c277df42 |
1213 | ); |
a0d0e21e |
1214 | /* Try scanning more and see if it helps. */ |
3280af22 |
1215 | PL_reginput = locinput; |
a0d0e21e |
1216 | cc->cur = n; |
1217 | cc->lastloc = locinput; |
5f05dabc |
1218 | cp = regcppush(cc->parenfloor); |
c277df42 |
1219 | REGCP_SET; |
5f05dabc |
1220 | if (regmatch(cc->scan)) { |
c277df42 |
1221 | regcpblow(cp); |
4633a7c4 |
1222 | sayYES; |
5f05dabc |
1223 | } |
c277df42 |
1224 | DEBUG_r( |
c3464db5 |
1225 | PerlIO_printf(Perl_debug_log, |
1226 | "%*s failed...\n", |
3280af22 |
1227 | REPORT_CODE_OFF+PL_regindent*2, "") |
c277df42 |
1228 | ); |
1229 | REGCP_UNWIND; |
5f05dabc |
1230 | regcppop(); |
4633a7c4 |
1231 | cc->cur = n - 1; |
c277df42 |
1232 | cc->lastloc = lastloc; |
4633a7c4 |
1233 | sayNO; |
a0d0e21e |
1234 | } |
1235 | |
1236 | /* Prefer scan over next for maximal matching. */ |
1237 | |
1238 | if (n < cc->max) { /* More greed allowed? */ |
5f05dabc |
1239 | cp = regcppush(cc->parenfloor); |
a0d0e21e |
1240 | cc->cur = n; |
1241 | cc->lastloc = locinput; |
c277df42 |
1242 | REGCP_SET; |
5f05dabc |
1243 | if (regmatch(cc->scan)) { |
c277df42 |
1244 | regcpblow(cp); |
4633a7c4 |
1245 | sayYES; |
5f05dabc |
1246 | } |
c277df42 |
1247 | REGCP_UNWIND; |
a0d0e21e |
1248 | regcppop(); /* Restore some previous $<digit>s? */ |
3280af22 |
1249 | PL_reginput = locinput; |
c277df42 |
1250 | DEBUG_r( |
c3464db5 |
1251 | PerlIO_printf(Perl_debug_log, |
1252 | "%*s failed, try continuation...\n", |
3280af22 |
1253 | REPORT_CODE_OFF+PL_regindent*2, "") |
c277df42 |
1254 | ); |
1255 | } |
3280af22 |
1256 | if (PL_dowarn && n >= REG_INFTY && !(PL_reg_flags & RF_warned)) { |
1257 | PL_reg_flags |= RF_warned; |
cb5d145d |
1258 | warn("%s limit (%d) exceeded", |
1259 | "Complex regular subexpression recursion", |
1260 | REG_INFTY - 1); |
a0d0e21e |
1261 | } |
1262 | |
1263 | /* Failed deeper matches of scan, so see if this one works. */ |
3280af22 |
1264 | PL_regcc = cc->oldcc; |
1265 | ln = PL_regcc->cur; |
a0d0e21e |
1266 | if (regmatch(cc->next)) |
4633a7c4 |
1267 | sayYES; |
c277df42 |
1268 | DEBUG_r( |
c3464db5 |
1269 | PerlIO_printf(Perl_debug_log, "%*s failed...\n", |
3280af22 |
1270 | REPORT_CODE_OFF+PL_regindent*2, "") |
c277df42 |
1271 | ); |
3280af22 |
1272 | PL_regcc->cur = ln; |
1273 | PL_regcc = cc; |
4633a7c4 |
1274 | cc->cur = n - 1; |
c277df42 |
1275 | cc->lastloc = lastloc; |
4633a7c4 |
1276 | sayNO; |
a0d0e21e |
1277 | } |
1278 | /* NOT REACHED */ |
c277df42 |
1279 | case BRANCHJ: |
1280 | next = scan + ARG(scan); |
1281 | if (next == scan) |
1282 | next = NULL; |
1283 | inner = NEXTOPER(NEXTOPER(scan)); |
1284 | goto do_branch; |
1285 | case BRANCH: |
1286 | inner = NEXTOPER(scan); |
1287 | do_branch: |
1288 | { |
1289 | CHECKPOINT lastcp; |
1290 | c1 = OP(scan); |
1291 | if (OP(next) != c1) /* No choice. */ |
1292 | next = inner; /* Avoid recursion. */ |
a0d0e21e |
1293 | else { |
3280af22 |
1294 | int lastparen = *PL_reglastparen; |
c277df42 |
1295 | |
1296 | REGCP_SET; |
a0d0e21e |
1297 | do { |
3280af22 |
1298 | PL_reginput = locinput; |
c277df42 |
1299 | if (regmatch(inner)) |
4633a7c4 |
1300 | sayYES; |
c277df42 |
1301 | REGCP_UNWIND; |
3280af22 |
1302 | for (n = *PL_reglastparen; n > lastparen; n--) |
1303 | PL_regendp[n] = 0; |
1304 | *PL_reglastparen = n; |
c277df42 |
1305 | scan = next; |
a0d0e21e |
1306 | /*SUPPRESS 560*/ |
c277df42 |
1307 | if (n = (c1 == BRANCH ? NEXT_OFF(next) : ARG(next))) |
1308 | next += n; |
a0d0e21e |
1309 | else |
c277df42 |
1310 | next = NULL; |
c277df42 |
1311 | inner = NEXTOPER(scan); |
1312 | if (c1 == BRANCHJ) { |
1313 | inner = NEXTOPER(inner); |
1314 | } |
1315 | } while (scan != NULL && OP(scan) == c1); |
4633a7c4 |
1316 | sayNO; |
a0d0e21e |
1317 | /* NOTREACHED */ |
a687059c |
1318 | } |
a0d0e21e |
1319 | } |
1320 | break; |
1321 | case MINMOD: |
1322 | minmod = 1; |
1323 | break; |
c277df42 |
1324 | case CURLYM: |
1325 | { |
00db4c45 |
1326 | I32 l = 0; |
c277df42 |
1327 | CHECKPOINT lastcp; |
1328 | |
1329 | /* We suppose that the next guy does not need |
1330 | backtracking: in particular, it is of constant length, |
1331 | and has no parenths to influence future backrefs. */ |
1332 | ln = ARG1(scan); /* min to match */ |
1333 | n = ARG2(scan); /* max to match */ |
c277df42 |
1334 | paren = scan->flags; |
1335 | if (paren) { |
3280af22 |
1336 | if (paren > PL_regsize) |
1337 | PL_regsize = paren; |
1338 | if (paren > *PL_reglastparen) |
1339 | *PL_reglastparen = paren; |
c277df42 |
1340 | } |
dc45a647 |
1341 | scan = NEXTOPER(scan) + NODE_STEP_REGNODE; |
c277df42 |
1342 | if (paren) |
1343 | scan += NEXT_OFF(scan); /* Skip former OPEN. */ |
3280af22 |
1344 | PL_reginput = locinput; |
c277df42 |
1345 | if (minmod) { |
1346 | minmod = 0; |
1347 | if (ln && regrepeat_hard(scan, ln, &l) < ln) |
1348 | sayNO; |
5f4b28b2 |
1349 | if (ln && l == 0 && n >= ln |
c277df42 |
1350 | /* In fact, this is tricky. If paren, then the |
1351 | fact that we did/didnot match may influence |
1352 | future execution. */ |
1353 | && !(paren && ln == 0)) |
1354 | ln = n; |
3280af22 |
1355 | locinput = PL_reginput; |
c277df42 |
1356 | if (regkind[(U8)OP(next)] == EXACT) { |
1357 | c1 = UCHARAT(OPERAND(next) + 1); |
1358 | if (OP(next) == EXACTF) |
1359 | c2 = fold[c1]; |
1360 | else if (OP(next) == EXACTFL) |
1361 | c2 = fold_locale[c1]; |
1362 | else |
1363 | c2 = c1; |
1364 | } else |
1365 | c1 = c2 = -1000; |
1366 | REGCP_SET; |
5f4b28b2 |
1367 | /* This may be improved if l == 0. */ |
c277df42 |
1368 | while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */ |
1369 | /* If it could work, try it. */ |
1370 | if (c1 == -1000 || |
3280af22 |
1371 | UCHARAT(PL_reginput) == c1 || |
1372 | UCHARAT(PL_reginput) == c2) |
c277df42 |
1373 | { |
1374 | if (paren) { |
1375 | if (n) { |
3280af22 |
1376 | PL_regstartp[paren] = PL_reginput - l; |
1377 | PL_regendp[paren] = PL_reginput; |
c277df42 |
1378 | } else |
3280af22 |
1379 | PL_regendp[paren] = NULL; |
c277df42 |
1380 | } |
1381 | if (regmatch(next)) |
1382 | sayYES; |
1383 | REGCP_UNWIND; |
1384 | } |
1385 | /* Couldn't or didn't -- move forward. */ |
3280af22 |
1386 | PL_reginput = locinput; |
c277df42 |
1387 | if (regrepeat_hard(scan, 1, &l)) { |
1388 | ln++; |
3280af22 |
1389 | locinput = PL_reginput; |
c277df42 |
1390 | } |
1391 | else |
1392 | sayNO; |
1393 | } |
1394 | } else { |
1395 | n = regrepeat_hard(scan, n, &l); |
1396 | if (n != 0 && l == 0 |
1397 | /* In fact, this is tricky. If paren, then the |
1398 | fact that we did/didnot match may influence |
1399 | future execution. */ |
1400 | && !(paren && ln == 0)) |
1401 | ln = n; |
3280af22 |
1402 | locinput = PL_reginput; |
c277df42 |
1403 | DEBUG_r( |
5c0ca799 |
1404 | PerlIO_printf(Perl_debug_log, |
1405 | "%*s matched %ld times, len=%ld...\n", |
3280af22 |
1406 | REPORT_CODE_OFF+PL_regindent*2, "", n, l) |
c277df42 |
1407 | ); |
1408 | if (n >= ln) { |
1409 | if (regkind[(U8)OP(next)] == EXACT) { |
1410 | c1 = UCHARAT(OPERAND(next) + 1); |
1411 | if (OP(next) == EXACTF) |
1412 | c2 = fold[c1]; |
1413 | else if (OP(next) == EXACTFL) |
1414 | c2 = fold_locale[c1]; |
1415 | else |
1416 | c2 = c1; |
1417 | } else |
1418 | c1 = c2 = -1000; |
1419 | } |
1420 | REGCP_SET; |
1421 | while (n >= ln) { |
1422 | /* If it could work, try it. */ |
1423 | if (c1 == -1000 || |
3280af22 |
1424 | UCHARAT(PL_reginput) == c1 || |
1425 | UCHARAT(PL_reginput) == c2) |
c277df42 |
1426 | { |
1427 | DEBUG_r( |
c3464db5 |
1428 | PerlIO_printf(Perl_debug_log, |
1429 | "%*s trying tail with n=%ld...\n", |
3280af22 |
1430 | REPORT_CODE_OFF+PL_regindent*2, "", n) |
c277df42 |
1431 | ); |
1432 | if (paren) { |
1433 | if (n) { |
3280af22 |
1434 | PL_regstartp[paren] = PL_reginput - l; |
1435 | PL_regendp[paren] = PL_reginput; |
c277df42 |
1436 | } else |
3280af22 |
1437 | PL_regendp[paren] = NULL; |
c277df42 |
1438 | } |
1439 | if (regmatch(next)) |
1440 | sayYES; |
1441 | REGCP_UNWIND; |
1442 | } |
1443 | /* Couldn't or didn't -- back up. */ |
1444 | n--; |
1445 | locinput -= l; |
3280af22 |
1446 | PL_reginput = locinput; |
c277df42 |
1447 | } |
1448 | } |
1449 | sayNO; |
1450 | break; |
1451 | } |
1452 | case CURLYN: |
1453 | paren = scan->flags; /* Which paren to set */ |
3280af22 |
1454 | if (paren > PL_regsize) |
1455 | PL_regsize = paren; |
1456 | if (paren > *PL_reglastparen) |
1457 | *PL_reglastparen = paren; |
c277df42 |
1458 | ln = ARG1(scan); /* min to match */ |
1459 | n = ARG2(scan); /* max to match */ |
dc45a647 |
1460 | scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE); |
c277df42 |
1461 | goto repeat; |
a0d0e21e |
1462 | case CURLY: |
c277df42 |
1463 | paren = 0; |
a0d0e21e |
1464 | ln = ARG1(scan); /* min to match */ |
1465 | n = ARG2(scan); /* max to match */ |
dc45a647 |
1466 | scan = NEXTOPER(scan) + NODE_STEP_REGNODE; |
a0d0e21e |
1467 | goto repeat; |
1468 | case STAR: |
1469 | ln = 0; |
c277df42 |
1470 | n = REG_INFTY; |
a0d0e21e |
1471 | scan = NEXTOPER(scan); |
c277df42 |
1472 | paren = 0; |
a0d0e21e |
1473 | goto repeat; |
1474 | case PLUS: |
c277df42 |
1475 | ln = 1; |
1476 | n = REG_INFTY; |
1477 | scan = NEXTOPER(scan); |
1478 | paren = 0; |
1479 | repeat: |
a0d0e21e |
1480 | /* |
1481 | * Lookahead to avoid useless match attempts |
1482 | * when we know what character comes next. |
1483 | */ |
bbce6d69 |
1484 | if (regkind[(U8)OP(next)] == EXACT) { |
1485 | c1 = UCHARAT(OPERAND(next) + 1); |
1486 | if (OP(next) == EXACTF) |
1487 | c2 = fold[c1]; |
1488 | else if (OP(next) == EXACTFL) |
1489 | c2 = fold_locale[c1]; |
1490 | else |
1491 | c2 = c1; |
1492 | } |
a0d0e21e |
1493 | else |
bbce6d69 |
1494 | c1 = c2 = -1000; |
3280af22 |
1495 | PL_reginput = locinput; |
a0d0e21e |
1496 | if (minmod) { |
c277df42 |
1497 | CHECKPOINT lastcp; |
a0d0e21e |
1498 | minmod = 0; |
1499 | if (ln && regrepeat(scan, ln) < ln) |
4633a7c4 |
1500 | sayNO; |
c277df42 |
1501 | REGCP_SET; |
1502 | while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */ |
a0d0e21e |
1503 | /* If it could work, try it. */ |
bbce6d69 |
1504 | if (c1 == -1000 || |
3280af22 |
1505 | UCHARAT(PL_reginput) == c1 || |
1506 | UCHARAT(PL_reginput) == c2) |
bbce6d69 |
1507 | { |
c277df42 |
1508 | if (paren) { |
1509 | if (n) { |
3280af22 |
1510 | PL_regstartp[paren] = PL_reginput - 1; |
1511 | PL_regendp[paren] = PL_reginput; |
c277df42 |
1512 | } else |
3280af22 |
1513 | PL_regendp[paren] = NULL; |
c277df42 |
1514 | } |
a0d0e21e |
1515 | if (regmatch(next)) |
4633a7c4 |
1516 | sayYES; |
c277df42 |
1517 | REGCP_UNWIND; |
bbce6d69 |
1518 | } |
c277df42 |
1519 | /* Couldn't or didn't -- move forward. */ |
3280af22 |
1520 | PL_reginput = locinput + ln; |
a0d0e21e |
1521 | if (regrepeat(scan, 1)) { |
1522 | ln++; |
3280af22 |
1523 | PL_reginput = locinput + ln; |
c277df42 |
1524 | } else |
4633a7c4 |
1525 | sayNO; |
a0d0e21e |
1526 | } |
1527 | } |
1528 | else { |
c277df42 |
1529 | CHECKPOINT lastcp; |
a0d0e21e |
1530 | n = regrepeat(scan, n); |
1531 | if (ln < n && regkind[(U8)OP(next)] == EOL && |
3280af22 |
1532 | (!PL_multiline || OP(next) == SEOL)) |
a0d0e21e |
1533 | ln = n; /* why back off? */ |
c277df42 |
1534 | REGCP_SET; |
1535 | if (paren) { |
1536 | while (n >= ln) { |
1537 | /* If it could work, try it. */ |
1538 | if (c1 == -1000 || |
3280af22 |
1539 | UCHARAT(PL_reginput) == c1 || |
1540 | UCHARAT(PL_reginput) == c2) |
c277df42 |
1541 | { |
1542 | if (paren && n) { |
1543 | if (n) { |
3280af22 |
1544 | PL_regstartp[paren] = PL_reginput - 1; |
1545 | PL_regendp[paren] = PL_reginput; |
c277df42 |
1546 | } else |
3280af22 |
1547 | PL_regendp[paren] = NULL; |
c277df42 |
1548 | } |
1549 | if (regmatch(next)) |
1550 | sayYES; |
1551 | REGCP_UNWIND; |
1552 | } |
1553 | /* Couldn't or didn't -- back up. */ |
1554 | n--; |
3280af22 |
1555 | PL_reginput = locinput + n; |
c277df42 |
1556 | } |
1557 | } else { |
1558 | while (n >= ln) { |
1559 | /* If it could work, try it. */ |
1560 | if (c1 == -1000 || |
3280af22 |
1561 | UCHARAT(PL_reginput) == c1 || |
1562 | UCHARAT(PL_reginput) == c2) |
c277df42 |
1563 | { |
1564 | if (regmatch(next)) |
1565 | sayYES; |
1566 | REGCP_UNWIND; |
1567 | } |
1568 | /* Couldn't or didn't -- back up. */ |
1569 | n--; |
3280af22 |
1570 | PL_reginput = locinput + n; |
bbce6d69 |
1571 | } |
a0d0e21e |
1572 | } |
1573 | } |
4633a7c4 |
1574 | sayNO; |
c277df42 |
1575 | break; |
a0d0e21e |
1576 | case END: |
3280af22 |
1577 | if (locinput < PL_regtill) |
7e5428c5 |
1578 | sayNO; /* Cannot match: too short. */ |
1579 | /* Fall through */ |
1580 | case SUCCEED: |
3280af22 |
1581 | PL_reginput = locinput; /* put where regtry can find it */ |
4633a7c4 |
1582 | sayYES; /* Success! */ |
c277df42 |
1583 | case SUSPEND: |
1584 | n = 1; |
1585 | goto do_ifmatch; |
a0d0e21e |
1586 | case UNLESSM: |
c277df42 |
1587 | n = 0; |
3280af22 |
1588 | if (locinput < PL_bostr + scan->flags) |
c277df42 |
1589 | goto say_yes; |
1590 | goto do_ifmatch; |
1591 | case IFMATCH: |
1592 | n = 1; |
3280af22 |
1593 | if (locinput < PL_bostr + scan->flags) |
c277df42 |
1594 | goto say_no; |
1595 | do_ifmatch: |
3280af22 |
1596 | PL_reginput = locinput - scan->flags; |
c277df42 |
1597 | inner = NEXTOPER(NEXTOPER(scan)); |
1598 | if (regmatch(inner) != n) { |
1599 | say_no: |
1600 | if (logical) { |
1601 | logical = 0; |
1602 | sw = 0; |
1603 | goto do_longjump; |
1604 | } else |
1605 | sayNO; |
1606 | } |
1607 | say_yes: |
1608 | if (logical) { |
1609 | logical = 0; |
1610 | sw = 1; |
1611 | } |
fe44a5e8 |
1612 | if (OP(scan) == SUSPEND) { |
3280af22 |
1613 | locinput = PL_reginput; |
565764a8 |
1614 | nextchr = UCHARAT(locinput); |
fe44a5e8 |
1615 | } |
c277df42 |
1616 | /* FALL THROUGH. */ |
1617 | case LONGJMP: |
1618 | do_longjump: |
1619 | next = scan + ARG(scan); |
1620 | if (next == scan) |
1621 | next = NULL; |
a0d0e21e |
1622 | break; |
1623 | default: |
c030ccd9 |
1624 | PerlIO_printf(PerlIO_stderr(), "%lx %d\n", |
c277df42 |
1625 | (unsigned long)scan, OP(scan)); |
a0d0e21e |
1626 | FAIL("regexp memory corruption"); |
a687059c |
1627 | } |
a0d0e21e |
1628 | scan = next; |
1629 | } |
a687059c |
1630 | |
a0d0e21e |
1631 | /* |
1632 | * We get here only if there's trouble -- normally "case END" is |
1633 | * the terminating point. |
1634 | */ |
1635 | FAIL("corrupted regexp pointers"); |
1636 | /*NOTREACHED*/ |
4633a7c4 |
1637 | sayNO; |
1638 | |
1639 | yes: |
1640 | #ifdef DEBUGGING |
3280af22 |
1641 | PL_regindent--; |
4633a7c4 |
1642 | #endif |
1643 | return 1; |
1644 | |
1645 | no: |
1646 | #ifdef DEBUGGING |
3280af22 |
1647 | PL_regindent--; |
4633a7c4 |
1648 | #endif |
a0d0e21e |
1649 | return 0; |
a687059c |
1650 | } |
1651 | |
1652 | /* |
1653 | - regrepeat - repeatedly match something simple, report how many |
1654 | */ |
1655 | /* |
1656 | * [This routine now assumes that it will only match on things of length 1. |
1657 | * That was true before, but now we assume scan - reginput is the count, |
1658 | * rather than incrementing count on every character.] |
1659 | */ |
76e3520e |
1660 | STATIC I32 |
c277df42 |
1661 | regrepeat(regnode *p, I32 max) |
a687059c |
1662 | { |
5c0ca799 |
1663 | dTHR; |
a0d0e21e |
1664 | register char *scan; |
1665 | register char *opnd; |
1666 | register I32 c; |
3280af22 |
1667 | register char *loceol = PL_regeol; |
a0d0e21e |
1668 | |
3280af22 |
1669 | scan = PL_reginput; |
c277df42 |
1670 | if (max != REG_INFTY && max < loceol - scan) |
a0d0e21e |
1671 | loceol = scan + max; |
161b471a |
1672 | opnd = (char *) OPERAND(p); |
a0d0e21e |
1673 | switch (OP(p)) { |
1674 | case ANY: |
1675 | while (scan < loceol && *scan != '\n') |
1676 | scan++; |
1677 | break; |
1678 | case SANY: |
1679 | scan = loceol; |
1680 | break; |
bbce6d69 |
1681 | case EXACT: /* length of string is 1 */ |
1682 | c = UCHARAT(++opnd); |
1683 | while (scan < loceol && UCHARAT(scan) == c) |
1684 | scan++; |
1685 | break; |
1686 | case EXACTF: /* length of string is 1 */ |
1687 | c = UCHARAT(++opnd); |
1688 | while (scan < loceol && |
1689 | (UCHARAT(scan) == c || UCHARAT(scan) == fold[c])) |
1690 | scan++; |
1691 | break; |
1692 | case EXACTFL: /* length of string is 1 */ |
3280af22 |
1693 | PL_reg_flags |= RF_tainted; |
bbce6d69 |
1694 | c = UCHARAT(++opnd); |
1695 | while (scan < loceol && |
1696 | (UCHARAT(scan) == c || UCHARAT(scan) == fold_locale[c])) |
a0d0e21e |
1697 | scan++; |
1698 | break; |
1699 | case ANYOF: |
ae5c130c |
1700 | while (scan < loceol && REGINCLASS(opnd, *scan)) |
a0d0e21e |
1701 | scan++; |
a0d0e21e |
1702 | break; |
1703 | case ALNUM: |
1704 | while (scan < loceol && isALNUM(*scan)) |
1705 | scan++; |
1706 | break; |
bbce6d69 |
1707 | case ALNUML: |
3280af22 |
1708 | PL_reg_flags |= RF_tainted; |
bbce6d69 |
1709 | while (scan < loceol && isALNUM_LC(*scan)) |
1710 | scan++; |
1711 | break; |
a0d0e21e |
1712 | case NALNUM: |
1713 | while (scan < loceol && !isALNUM(*scan)) |
1714 | scan++; |
1715 | break; |
bbce6d69 |
1716 | case NALNUML: |
3280af22 |
1717 | PL_reg_flags |= RF_tainted; |
bbce6d69 |
1718 | while (scan < loceol && !isALNUM_LC(*scan)) |
1719 | scan++; |
1720 | break; |
a0d0e21e |
1721 | case SPACE: |
1722 | while (scan < loceol && isSPACE(*scan)) |
1723 | scan++; |
1724 | break; |
bbce6d69 |
1725 | case SPACEL: |
3280af22 |
1726 | PL_reg_flags |= RF_tainted; |
bbce6d69 |
1727 | while (scan < loceol && isSPACE_LC(*scan)) |
1728 | scan++; |
1729 | break; |
a0d0e21e |
1730 | case NSPACE: |
1731 | while (scan < loceol && !isSPACE(*scan)) |
1732 | scan++; |
1733 | break; |
bbce6d69 |
1734 | case NSPACEL: |
3280af22 |
1735 | PL_reg_flags |= RF_tainted; |
bbce6d69 |
1736 | while (scan < loceol && !isSPACE_LC(*scan)) |
1737 | scan++; |
1738 | break; |
a0d0e21e |
1739 | case DIGIT: |
1740 | while (scan < loceol && isDIGIT(*scan)) |
1741 | scan++; |
1742 | break; |
1743 | case NDIGIT: |
1744 | while (scan < loceol && !isDIGIT(*scan)) |
1745 | scan++; |
1746 | break; |
1747 | default: /* Called on something of 0 width. */ |
1748 | break; /* So match right here or not at all. */ |
1749 | } |
a687059c |
1750 | |
3280af22 |
1751 | c = scan - PL_reginput; |
1752 | PL_reginput = scan; |
a687059c |
1753 | |
c277df42 |
1754 | DEBUG_r( |
1755 | { |
1756 | SV *prop = sv_newmortal(); |
1757 | |
1758 | regprop(prop, p); |
1759 | PerlIO_printf(Perl_debug_log, |
1760 | "%*s %s can match %ld times out of %ld...\n", |
1761 | REPORT_CODE_OFF+1, "", SvPVX(prop),c,max); |
1762 | }); |
1763 | |
a0d0e21e |
1764 | return(c); |
a687059c |
1765 | } |
1766 | |
1767 | /* |
c277df42 |
1768 | - regrepeat_hard - repeatedly match something, report total lenth and length |
1769 | * |
1770 | * The repeater is supposed to have constant length. |
1771 | */ |
1772 | |
76e3520e |
1773 | STATIC I32 |
c277df42 |
1774 | regrepeat_hard(regnode *p, I32 max, I32 *lp) |
1775 | { |
5c0ca799 |
1776 | dTHR; |
c277df42 |
1777 | register char *scan; |
1778 | register char *start; |
3280af22 |
1779 | register char *loceol = PL_regeol; |
c277df42 |
1780 | I32 l = -1; |
1781 | |
3280af22 |
1782 | start = PL_reginput; |
1783 | while (PL_reginput < loceol && (scan = PL_reginput, regmatch(p))) { |
c277df42 |
1784 | if (l == -1) { |
3280af22 |
1785 | *lp = l = PL_reginput - start; |
c277df42 |
1786 | if (max != REG_INFTY && l*max < loceol - scan) |
1787 | loceol = scan + l*max; |
1788 | if (l == 0) { |
1789 | return max; |
1790 | } |
1791 | } |
1792 | } |
3280af22 |
1793 | if (PL_reginput < loceol) |
1794 | PL_reginput = scan; |
c277df42 |
1795 | else |
3280af22 |
1796 | scan = PL_reginput; |
c277df42 |
1797 | |
1798 | return (scan - start)/l; |
1799 | } |
1800 | |
1801 | /* |
bbce6d69 |
1802 | - regclass - determine if a character falls into a character class |
1803 | */ |
1804 | |
76e3520e |
1805 | STATIC bool |
8ac85365 |
1806 | reginclass(register char *p, register I32 c) |
bbce6d69 |
1807 | { |
5c0ca799 |
1808 | dTHR; |
bbce6d69 |
1809 | char flags = *p; |
1810 | bool match = FALSE; |
1811 | |
1812 | c &= 0xFF; |
ae5c130c |
1813 | if (ANYOF_TEST(p, c)) |
bbce6d69 |
1814 | match = TRUE; |
1815 | else if (flags & ANYOF_FOLD) { |
1816 | I32 cf; |
1817 | if (flags & ANYOF_LOCALE) { |
3280af22 |
1818 | PL_reg_flags |= RF_tainted; |
bbce6d69 |
1819 | cf = fold_locale[c]; |
1820 | } |
1821 | else |
1822 | cf = fold[c]; |
ae5c130c |
1823 | if (ANYOF_TEST(p, cf)) |
bbce6d69 |
1824 | match = TRUE; |
1825 | } |
1826 | |
1827 | if (!match && (flags & ANYOF_ISA)) { |
3280af22 |
1828 | PL_reg_flags |= RF_tainted; |
bbce6d69 |
1829 | |
1830 | if (((flags & ANYOF_ALNUML) && isALNUM_LC(c)) || |
1831 | ((flags & ANYOF_NALNUML) && !isALNUM_LC(c)) || |
1832 | ((flags & ANYOF_SPACEL) && isSPACE_LC(c)) || |
1833 | ((flags & ANYOF_NSPACEL) && !isSPACE_LC(c))) |
1834 | { |
1835 | match = TRUE; |
1836 | } |
1837 | } |
1838 | |
ae5c130c |
1839 | return (flags & ANYOF_INVERT) ? !match : match; |
bbce6d69 |
1840 | } |
1841 | |
161b471a |
1842 | |
1843 | |