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