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