Commit | Line | Data |
a0d0e21e |
1 | /* regexec.c |
2 | */ |
3 | |
4 | /* |
5 | * "One Ring to rule them all, One Ring to find them..." |
6 | */ |
7 | |
a687059c |
8 | /* NOTE: this is derived from Henry Spencer's regexp code, and should not |
9 | * confused with the original package (see point 3 below). Thanks, Henry! |
10 | */ |
11 | |
12 | /* Additional note: this code is very heavily munged from Henry's version |
13 | * in places. In some spots I've traded clarity for efficiency, so don't |
14 | * blame Henry for some of the lack of readability. |
15 | */ |
16 | |
e50aee73 |
17 | /* The names of the functions have been changed from regcomp and |
18 | * regexec to pregcomp and pregexec in order to avoid conflicts |
19 | * with the POSIX routines of the same names. |
20 | */ |
21 | |
b9d5759e |
22 | #ifdef PERL_EXT_RE_BUILD |
23 | /* need to replace pregcomp et al, so enable that */ |
24 | # ifndef PERL_IN_XSUB_RE |
25 | # define PERL_IN_XSUB_RE |
26 | # endif |
27 | /* need access to debugger hooks */ |
28 | # ifndef DEBUGGING |
29 | # define DEBUGGING |
30 | # endif |
31 | #endif |
32 | |
33 | #ifdef PERL_IN_XSUB_RE |
d06ea78c |
34 | /* We *really* need to overwrite these symbols: */ |
56953603 |
35 | # define Perl_regexec_flags my_regexec |
36 | # define Perl_regdump my_regdump |
37 | # define Perl_regprop my_regprop |
d06ea78c |
38 | /* *These* symbols are masked to allow static link. */ |
39 | # define Perl_pregexec my_pregexec |
56953603 |
40 | #endif |
41 | |
f0fcb552 |
42 | /*SUPPRESS 112*/ |
a687059c |
43 | /* |
e50aee73 |
44 | * pregcomp and pregexec -- regsub and regerror are not used in perl |
a687059c |
45 | * |
46 | * Copyright (c) 1986 by University of Toronto. |
47 | * Written by Henry Spencer. Not derived from licensed software. |
48 | * |
49 | * Permission is granted to anyone to use this software for any |
50 | * purpose on any computer system, and to redistribute it freely, |
51 | * subject to the following restrictions: |
52 | * |
53 | * 1. The author is not responsible for the consequences of use of |
54 | * this software, no matter how awful, even if they arise |
55 | * from defects in it. |
56 | * |
57 | * 2. The origin of this software must not be misrepresented, either |
58 | * by explicit claim or by omission. |
59 | * |
60 | * 3. Altered versions must be plainly marked as such, and must not |
61 | * be misrepresented as being the original software. |
62 | * |
63 | **** Alterations to Henry's code are... |
64 | **** |
a0ed51b3 |
65 | **** Copyright (c) 1991-1998, Larry Wall |
a687059c |
66 | **** |
9ef589d8 |
67 | **** You may distribute under the terms of either the GNU General Public |
68 | **** License or the Artistic License, as specified in the README file. |
a687059c |
69 | * |
70 | * Beware that some of this code is subtly aware of the way operator |
71 | * precedence is structured in regular expressions. Serious changes in |
72 | * regular-expression syntax might require a total rethink. |
73 | */ |
74 | #include "EXTERN.h" |
75 | #include "perl.h" |
0f5d15d6 |
76 | |
a687059c |
77 | #include "regcomp.h" |
78 | |
c277df42 |
79 | #define RF_tainted 1 /* tainted information used? */ |
80 | #define RF_warned 2 /* warned about big count? */ |
ce862d02 |
81 | #define RF_evaled 4 /* Did an EVAL with setting? */ |
a0ed51b3 |
82 | #define RF_utf8 8 /* String contains multibyte chars? */ |
83 | |
84 | #define UTF (PL_reg_flags & RF_utf8) |
ce862d02 |
85 | |
86 | #define RS_init 1 /* eval environment created */ |
87 | #define RS_set 2 /* replsv value is set */ |
c277df42 |
88 | |
a687059c |
89 | #ifndef STATIC |
90 | #define STATIC static |
91 | #endif |
92 | |
76e3520e |
93 | #ifndef PERL_OBJECT |
a0d0e21e |
94 | typedef I32 CHECKPOINT; |
95 | |
c277df42 |
96 | /* |
97 | * Forwards. |
98 | */ |
99 | |
100 | static I32 regmatch _((regnode *prog)); |
101 | static I32 regrepeat _((regnode *p, I32 max)); |
102 | static I32 regrepeat_hard _((regnode *p, I32 max, I32 *lp)); |
103 | static I32 regtry _((regexp *prog, char *startpos)); |
ae5c130c |
104 | |
c277df42 |
105 | static bool reginclass _((char *p, I32 c)); |
a0ed51b3 |
106 | static bool reginclassutf8 _((regnode *f, U8* p)); |
55497cff |
107 | static CHECKPOINT regcppush _((I32 parenfloor)); |
108 | static char * regcppop _((void)); |
942e002e |
109 | static char * regcp_set_to _((I32 ss)); |
110 | static void cache_re _((regexp *prog)); |
76e3520e |
111 | #endif |
a0ed51b3 |
112 | |
ae5c130c |
113 | #define REGINCLASS(p,c) (*(p) ? reginclass(p,c) : ANYOF_TEST(p,c)) |
a0ed51b3 |
114 | #define REGINCLASSUTF8(f,p) (ARG1(f) ? reginclassutf8(f,p) : swash_fetch((SV*)PL_regdata->data[ARG2(f)],p)) |
115 | |
116 | #define CHR_SVLEN(sv) (UTF ? sv_len_utf8(sv) : SvCUR(sv)) |
117 | #define CHR_DIST(a,b) (UTF ? utf8_distance(a,b) : a - b) |
118 | |
dfe13c55 |
119 | #ifndef PERL_OBJECT |
120 | static U8 * reghop _((U8 *pos, I32 off)); |
121 | static U8 * reghopmaybe _((U8 *pos, I32 off)); |
122 | #endif |
123 | #define reghop_c(pos,off) ((char*)reghop((U8*)pos, off)) |
124 | #define reghopmaybe_c(pos,off) ((char*)reghopmaybe((U8*)pos, off)) |
125 | #define HOP(pos,off) (UTF ? reghop((U8*)pos, off) : (U8*)(pos + off)) |
126 | #define HOPMAYBE(pos,off) (UTF ? reghopmaybe((U8*)pos, off) : (U8*)(pos + off)) |
127 | #define HOPc(pos,off) ((char*)HOP(pos,off)) |
128 | #define HOPMAYBEc(pos,off) ((char*)HOPMAYBE(pos,off)) |
a0d0e21e |
129 | |
76e3520e |
130 | STATIC CHECKPOINT |
8ac85365 |
131 | regcppush(I32 parenfloor) |
a0d0e21e |
132 | { |
11343788 |
133 | dTHR; |
3280af22 |
134 | int retval = PL_savestack_ix; |
135 | int i = (PL_regsize - parenfloor) * 4; |
a0d0e21e |
136 | int p; |
137 | |
138 | SSCHECK(i + 5); |
3280af22 |
139 | for (p = PL_regsize; p > parenfloor; p--) { |
140 | SSPUSHPTR(PL_regendp[p]); |
141 | SSPUSHPTR(PL_regstartp[p]); |
142 | SSPUSHPTR(PL_reg_start_tmp[p]); |
a0d0e21e |
143 | SSPUSHINT(p); |
144 | } |
3280af22 |
145 | SSPUSHINT(PL_regsize); |
146 | SSPUSHINT(*PL_reglastparen); |
147 | SSPUSHPTR(PL_reginput); |
a0d0e21e |
148 | SSPUSHINT(i + 3); |
149 | SSPUSHINT(SAVEt_REGCONTEXT); |
150 | return retval; |
151 | } |
152 | |
c277df42 |
153 | /* These are needed since we do not localize EVAL nodes: */ |
c3464db5 |
154 | # define REGCP_SET DEBUG_r(PerlIO_printf(Perl_debug_log, \ |
155 | " Setting an EVAL scope, savestack=%i\n", \ |
3280af22 |
156 | PL_savestack_ix)); lastcp = PL_savestack_ix |
c3464db5 |
157 | |
3280af22 |
158 | # define REGCP_UNWIND DEBUG_r(lastcp != PL_savestack_ix ? \ |
c3464db5 |
159 | PerlIO_printf(Perl_debug_log, \ |
160 | " Clearing an EVAL scope, savestack=%i..%i\n", \ |
3280af22 |
161 | lastcp, PL_savestack_ix) : 0); regcpblow(lastcp) |
c277df42 |
162 | |
76e3520e |
163 | STATIC char * |
8ac85365 |
164 | regcppop(void) |
a0d0e21e |
165 | { |
11343788 |
166 | dTHR; |
a0d0e21e |
167 | I32 i = SSPOPINT; |
168 | U32 paren = 0; |
169 | char *input; |
170 | char *tmps; |
171 | assert(i == SAVEt_REGCONTEXT); |
172 | i = SSPOPINT; |
173 | input = (char *) SSPOPPTR; |
3280af22 |
174 | *PL_reglastparen = SSPOPINT; |
175 | PL_regsize = SSPOPINT; |
c277df42 |
176 | for (i -= 3; i > 0; i -= 4) { |
a0d0e21e |
177 | paren = (U32)SSPOPINT; |
3280af22 |
178 | PL_reg_start_tmp[paren] = (char *) SSPOPPTR; |
179 | PL_regstartp[paren] = (char *) SSPOPPTR; |
a0d0e21e |
180 | tmps = (char*)SSPOPPTR; |
3280af22 |
181 | if (paren <= *PL_reglastparen) |
182 | PL_regendp[paren] = tmps; |
c277df42 |
183 | DEBUG_r( |
c3464db5 |
184 | PerlIO_printf(Perl_debug_log, |
185 | " restoring \\%d to %d(%d)..%d%s\n", |
3280af22 |
186 | paren, PL_regstartp[paren] - PL_regbol, |
187 | PL_reg_start_tmp[paren] - PL_regbol, |
188 | PL_regendp[paren] - PL_regbol, |
189 | (paren > *PL_reglastparen ? "(no)" : "")); |
c277df42 |
190 | ); |
a0d0e21e |
191 | } |
c277df42 |
192 | DEBUG_r( |
3280af22 |
193 | if (*PL_reglastparen + 1 <= PL_regnpar) { |
c3464db5 |
194 | PerlIO_printf(Perl_debug_log, |
195 | " restoring \\%d..\\%d to undef\n", |
3280af22 |
196 | *PL_reglastparen + 1, PL_regnpar); |
c277df42 |
197 | } |
198 | ); |
3280af22 |
199 | for (paren = *PL_reglastparen + 1; paren <= PL_regnpar; paren++) { |
200 | if (paren > PL_regsize) |
201 | PL_regstartp[paren] = Nullch; |
202 | PL_regendp[paren] = Nullch; |
a0d0e21e |
203 | } |
204 | return input; |
205 | } |
206 | |
0f5d15d6 |
207 | STATIC char * |
208 | regcp_set_to(I32 ss) |
209 | { |
46124e9e |
210 | dTHR; |
0f5d15d6 |
211 | I32 tmp = PL_savestack_ix; |
212 | |
213 | PL_savestack_ix = ss; |
214 | regcppop(); |
215 | PL_savestack_ix = tmp; |
942e002e |
216 | return Nullch; |
0f5d15d6 |
217 | } |
218 | |
219 | typedef struct re_cc_state |
220 | { |
221 | I32 ss; |
222 | regnode *node; |
223 | struct re_cc_state *prev; |
224 | CURCUR *cc; |
225 | regexp *re; |
226 | } re_cc_state; |
227 | |
c277df42 |
228 | #define regcpblow(cp) LEAVE_SCOPE(cp) |
a0d0e21e |
229 | |
a687059c |
230 | /* |
e50aee73 |
231 | * pregexec and friends |
a687059c |
232 | */ |
233 | |
234 | /* |
c277df42 |
235 | - pregexec - match a regexp against a string |
a687059c |
236 | */ |
c277df42 |
237 | I32 |
c3464db5 |
238 | pregexec(register regexp *prog, char *stringarg, register char *strend, |
239 | char *strbeg, I32 minend, SV *screamer, U32 nosave) |
c277df42 |
240 | /* strend: pointer to null at end of string */ |
241 | /* strbeg: real beginning of string */ |
242 | /* minend: end of match must be >=minend after stringarg. */ |
243 | /* nosave: For optimizations. */ |
244 | { |
245 | return |
246 | regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL, |
247 | nosave ? 0 : REXEC_COPY_STR); |
248 | } |
0f5d15d6 |
249 | |
250 | STATIC void |
251 | cache_re(regexp *prog) |
252 | { |
46124e9e |
253 | dTHR; |
0f5d15d6 |
254 | PL_regprecomp = prog->precomp; /* Needed for FAIL. */ |
255 | #ifdef DEBUGGING |
256 | PL_regprogram = prog->program; |
257 | #endif |
258 | PL_regnpar = prog->nparens; |
259 | PL_regdata = prog->data; |
260 | PL_reg_re = prog; |
261 | } |
22e551b9 |
262 | |
a687059c |
263 | /* |
c277df42 |
264 | - regexec_flags - match a regexp against a string |
a687059c |
265 | */ |
79072805 |
266 | I32 |
c3464db5 |
267 | regexec_flags(register regexp *prog, char *stringarg, register char *strend, |
22e551b9 |
268 | char *strbeg, I32 minend, SV *sv, void *data, U32 flags) |
c277df42 |
269 | /* strend: pointer to null at end of string */ |
270 | /* strbeg: real beginning of string */ |
271 | /* minend: end of match must be >=minend after stringarg. */ |
272 | /* data: May be used for some additional optimizations. */ |
273 | /* nosave: For optimizations. */ |
a687059c |
274 | { |
5c0ca799 |
275 | dTHR; |
a0d0e21e |
276 | register char *s; |
c277df42 |
277 | register regnode *c; |
a0d0e21e |
278 | register char *startpos = stringarg; |
279 | register I32 tmp; |
c277df42 |
280 | I32 minlen; /* must match at least this many chars */ |
a0d0e21e |
281 | I32 dontbother = 0; /* how many characters not to try at end */ |
282 | CURCUR cc; |
c277df42 |
283 | I32 start_shift = 0; /* Offset of the start to find |
a0ed51b3 |
284 | constant substr. */ /* CC */ |
285 | I32 end_shift = 0; /* Same for the end. */ /* CC */ |
c277df42 |
286 | I32 scream_pos = -1; /* Internal iterator of scream. */ |
287 | char *scream_olds; |
3280af22 |
288 | SV* oreplsv = GvSV(PL_replgv); |
a687059c |
289 | |
a0d0e21e |
290 | cc.cur = 0; |
4633a7c4 |
291 | cc.oldcc = 0; |
3280af22 |
292 | PL_regcc = &cc; |
a0d0e21e |
293 | |
0f5d15d6 |
294 | cache_re(prog); |
a0d0e21e |
295 | #ifdef DEBUGGING |
3280af22 |
296 | PL_regnarrate = PL_debug & 512; |
a0d0e21e |
297 | #endif |
298 | |
299 | /* Be paranoid... */ |
300 | if (prog == NULL || startpos == NULL) { |
301 | croak("NULL regexp parameter"); |
302 | return 0; |
303 | } |
304 | |
c277df42 |
305 | minlen = prog->minlen; |
306 | if (strend - startpos < minlen) goto phooey; |
307 | |
a0d0e21e |
308 | if (startpos == strbeg) /* is ^ valid at stringarg? */ |
3280af22 |
309 | PL_regprev = '\n'; |
a0d0e21e |
310 | else { |
a0ed51b3 |
311 | PL_regprev = (U32)stringarg[-1]; |
3280af22 |
312 | if (!PL_multiline && PL_regprev == '\n') |
313 | PL_regprev = '\0'; /* force ^ to NOT match */ |
a0d0e21e |
314 | } |
bbce6d69 |
315 | |
a0d0e21e |
316 | /* Check validity of program. */ |
22c35a8c |
317 | if (UCHARAT(prog->program) != REG_MAGIC) { |
a0d0e21e |
318 | FAIL("corrupted regexp program"); |
319 | } |
320 | |
3280af22 |
321 | PL_reg_flags = 0; |
322 | PL_reg_eval_set = 0; |
a0d0e21e |
323 | |
a0ed51b3 |
324 | if (prog->reganch & ROPT_UTF8) |
325 | PL_reg_flags |= RF_utf8; |
326 | |
327 | /* Mark beginning of line for ^ and lookbehind. */ |
328 | PL_regbol = startpos; |
329 | PL_bostr = strbeg; |
330 | |
331 | /* Mark end of line for $ (and such) */ |
332 | PL_regeol = strend; |
333 | |
334 | /* see how far we have to get to not match where we matched before */ |
335 | PL_regtill = startpos+minend; |
336 | |
0f5d15d6 |
337 | /* We start without call_cc context. */ |
338 | PL_reg_call_cc = 0; |
339 | |
a0d0e21e |
340 | /* If there is a "must appear" string, look for it. */ |
341 | s = startpos; |
c277df42 |
342 | if (!(flags & REXEC_CHECKED) |
343 | && prog->check_substr != Nullsv && |
774d564b |
344 | !(prog->reganch & ROPT_ANCH_GPOS) && |
c277df42 |
345 | (!(prog->reganch & (ROPT_ANCH_BOL | ROPT_ANCH_MBOL)) |
3280af22 |
346 | || (PL_multiline && prog->check_substr == prog->anchored_substr)) ) |
a0d0e21e |
347 | { |
a0ed51b3 |
348 | char *t; |
349 | start_shift = prog->check_offset_min; /* okay to underestimate on CC */ |
c277df42 |
350 | /* Should be nonnegative! */ |
a0ed51b3 |
351 | end_shift = minlen - start_shift - CHR_SVLEN(prog->check_substr); |
22e551b9 |
352 | if (flags & REXEC_SCREAM) { |
3280af22 |
353 | if (PL_screamfirst[BmRARE(prog->check_substr)] >= 0) |
22e551b9 |
354 | s = screaminstr(sv, prog->check_substr, |
c277df42 |
355 | start_shift + (stringarg - strbeg), |
356 | end_shift, &scream_pos, 0); |
a0d0e21e |
357 | else |
358 | s = Nullch; |
c277df42 |
359 | scream_olds = s; |
0a12ae7d |
360 | } |
a0d0e21e |
361 | else |
c277df42 |
362 | s = fbm_instr((unsigned char*)s + start_shift, |
363 | (unsigned char*)strend - end_shift, |
411d5715 |
364 | prog->check_substr, 0); |
a0d0e21e |
365 | if (!s) { |
c277df42 |
366 | ++BmUSEFUL(prog->check_substr); /* hooray */ |
a0d0e21e |
367 | goto phooey; /* not present */ |
a0ed51b3 |
368 | } |
369 | else if (s - stringarg > prog->check_offset_max && |
370 | (UTF |
dfe13c55 |
371 | ? ((t = reghopmaybe_c(s, -(prog->check_offset_max))) && t >= stringarg) |
a0ed51b3 |
372 | : (t = s - prog->check_offset_max) != 0 |
373 | ) |
374 | ) |
375 | { |
c277df42 |
376 | ++BmUSEFUL(prog->check_substr); /* hooray/2 */ |
a0ed51b3 |
377 | s = t; |
378 | } |
379 | else if (!(prog->reganch & ROPT_NAUGHTY) |
c277df42 |
380 | && --BmUSEFUL(prog->check_substr) < 0 |
381 | && prog->check_substr == prog->float_substr) { /* boo */ |
382 | SvREFCNT_dec(prog->check_substr); |
383 | prog->check_substr = Nullsv; /* disable */ |
384 | prog->float_substr = Nullsv; /* clear */ |
a0d0e21e |
385 | s = startpos; |
a0ed51b3 |
386 | } |
387 | else |
388 | s = startpos; |
a0d0e21e |
389 | } |
a687059c |
390 | |
c277df42 |
391 | DEBUG_r( |
392 | PerlIO_printf(Perl_debug_log, |
8d300b32 |
393 | "%sMatching%s `%s%.60s%s%s' against `%s%.*s%s%s'\n", |
394 | PL_colors[4],PL_colors[5],PL_colors[0], |
395 | prog->precomp, |
396 | PL_colors[1], |
c277df42 |
397 | (strlen(prog->precomp) > 60 ? "..." : ""), |
8d300b32 |
398 | PL_colors[0], |
c277df42 |
399 | (strend - startpos > 60 ? 60 : strend - startpos), |
8d300b32 |
400 | startpos, PL_colors[1], |
c277df42 |
401 | (strend - startpos > 60 ? "..." : "")) |
402 | ); |
403 | |
22e551b9 |
404 | if (prog->reganch & ROPT_GPOS_SEEN) { |
405 | MAGIC *mg; |
406 | int pos = 0; |
407 | |
408 | if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv) |
409 | && (mg = mg_find(sv, 'g')) && mg->mg_len >= 0) |
410 | pos = mg->mg_len; |
411 | PL_reg_ganch = startpos + pos; |
412 | } |
413 | |
a0d0e21e |
414 | /* Simplest case: anchored match need be tried only once. */ |
774d564b |
415 | /* [unless only anchor is BOL and multiline is set] */ |
22e551b9 |
416 | if (prog->reganch & (ROPT_ANCH & ~ROPT_ANCH_GPOS)) { |
a0d0e21e |
417 | if (regtry(prog, startpos)) |
418 | goto got_it; |
22e551b9 |
419 | else if (PL_multiline || (prog->reganch & ROPT_IMPLICIT) |
420 | || (prog->reganch & ROPT_ANCH_MBOL)) /* XXXX SBOL? */ |
774d564b |
421 | { |
a0d0e21e |
422 | if (minlen) |
423 | dontbother = minlen - 1; |
dfe13c55 |
424 | strend = HOPc(strend, -dontbother); |
a0d0e21e |
425 | /* for multiline we only have to try after newlines */ |
426 | if (s > startpos) |
427 | s--; |
428 | while (s < strend) { |
6f06b55f |
429 | if (*s++ == '\n') { /* don't need PL_utf8skip here */ |
a0d0e21e |
430 | if (s < strend && regtry(prog, s)) |
431 | goto got_it; |
432 | } |
35c8bce7 |
433 | } |
35c8bce7 |
434 | } |
a0d0e21e |
435 | goto phooey; |
22e551b9 |
436 | } else if (prog->reganch & ROPT_ANCH_GPOS) { |
437 | if (regtry(prog, PL_reg_ganch)) |
438 | goto got_it; |
439 | goto phooey; |
a0d0e21e |
440 | } |
35c8bce7 |
441 | |
a0d0e21e |
442 | /* Messy cases: unanchored match. */ |
c277df42 |
443 | if (prog->anchored_substr && prog->reganch & ROPT_SKIP) { |
444 | /* we have /x+whatever/ */ |
445 | /* it must be a one character string */ |
446 | char ch = SvPVX(prog->anchored_substr)[0]; |
a0ed51b3 |
447 | if (UTF) { |
448 | while (s < strend) { |
449 | if (*s == ch) { |
450 | if (regtry(prog, s)) goto got_it; |
451 | s += UTF8SKIP(s); |
452 | while (s < strend && *s == ch) |
453 | s += UTF8SKIP(s); |
454 | } |
455 | s += UTF8SKIP(s); |
456 | } |
457 | } |
458 | else { |
459 | while (s < strend) { |
460 | if (*s == ch) { |
461 | if (regtry(prog, s)) goto got_it; |
c277df42 |
462 | s++; |
a0ed51b3 |
463 | while (s < strend && *s == ch) |
464 | s++; |
465 | } |
466 | s++; |
a0d0e21e |
467 | } |
a687059c |
468 | } |
c277df42 |
469 | } |
470 | /*SUPPRESS 560*/ |
471 | else if (prog->anchored_substr != Nullsv |
472 | || (prog->float_substr != Nullsv |
473 | && prog->float_max_offset < strend - s)) { |
474 | SV *must = prog->anchored_substr |
475 | ? prog->anchored_substr : prog->float_substr; |
476 | I32 back_max = |
477 | prog->anchored_substr ? prog->anchored_offset : prog->float_max_offset; |
478 | I32 back_min = |
479 | prog->anchored_substr ? prog->anchored_offset : prog->float_min_offset; |
480 | I32 delta = back_max - back_min; |
dfe13c55 |
481 | char *last = HOPc(strend, 0-(CHR_SVLEN(must) + back_min)); /* Cannot start after this */ |
a0ed51b3 |
482 | char *last1; /* Last position checked before */ |
483 | |
484 | if (s > PL_bostr) |
dfe13c55 |
485 | last1 = HOPc(s, -1); |
a0ed51b3 |
486 | else |
487 | last1 = s - 1; /* bogus */ |
c277df42 |
488 | |
489 | /* XXXX check_substr already used to find `s', can optimize if |
490 | check_substr==must. */ |
491 | scream_pos = -1; |
492 | dontbother = end_shift; |
dfe13c55 |
493 | strend = HOPc(strend, -dontbother); |
c277df42 |
494 | while ( (s <= last) && |
22e551b9 |
495 | ((flags & REXEC_SCREAM) |
496 | ? (s = screaminstr(sv, must, HOPc(s, back_min) - strbeg, |
c277df42 |
497 | end_shift, &scream_pos, 0)) |
a0ed51b3 |
498 | : (s = fbm_instr((unsigned char*)HOP(s, back_min), |
411d5715 |
499 | (unsigned char*)strend, must, 0))) ) { |
dfe13c55 |
500 | if (HOPc(s, -back_max) > last1) { |
501 | last1 = HOPc(s, -back_min); |
502 | s = HOPc(s, -back_max); |
a0ed51b3 |
503 | } |
504 | else { |
dfe13c55 |
505 | char *t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1; |
c277df42 |
506 | |
dfe13c55 |
507 | last1 = HOPc(s, -back_min); |
c277df42 |
508 | s = t; |
a0d0e21e |
509 | } |
a0ed51b3 |
510 | if (UTF) { |
511 | while (s <= last1) { |
512 | if (regtry(prog, s)) |
513 | goto got_it; |
514 | s += UTF8SKIP(s); |
515 | } |
516 | } |
517 | else { |
518 | while (s <= last1) { |
519 | if (regtry(prog, s)) |
520 | goto got_it; |
521 | s++; |
522 | } |
a0d0e21e |
523 | } |
524 | } |
525 | goto phooey; |
a0ed51b3 |
526 | } |
527 | else if (c = prog->regstclass) { |
a0d0e21e |
528 | I32 doevery = (prog->reganch & ROPT_SKIP) == 0; |
a0ed51b3 |
529 | char *cc; |
a687059c |
530 | |
a0d0e21e |
531 | if (minlen) |
532 | dontbother = minlen - 1; |
dfe13c55 |
533 | strend = HOPc(strend, -dontbother); /* don't bother with what can't match */ |
a0d0e21e |
534 | tmp = 1; |
535 | /* We know what class it must start with. */ |
536 | switch (OP(c)) { |
a0ed51b3 |
537 | case ANYOFUTF8: |
538 | cc = (char *) OPERAND(c); |
539 | while (s < strend) { |
540 | if (REGINCLASSUTF8(c, (U8*)s)) { |
541 | if (tmp && regtry(prog, s)) |
542 | goto got_it; |
543 | else |
544 | tmp = doevery; |
545 | } |
546 | else |
547 | tmp = 1; |
548 | s += UTF8SKIP(s); |
549 | } |
550 | break; |
a0d0e21e |
551 | case ANYOF: |
a0ed51b3 |
552 | cc = (char *) OPERAND(c); |
a0d0e21e |
553 | while (s < strend) { |
a0ed51b3 |
554 | if (REGINCLASS(cc, *s)) { |
a0d0e21e |
555 | if (tmp && regtry(prog, s)) |
556 | goto got_it; |
557 | else |
558 | tmp = doevery; |
a687059c |
559 | } |
a0d0e21e |
560 | else |
561 | tmp = 1; |
562 | s++; |
563 | } |
564 | break; |
bbce6d69 |
565 | case BOUNDL: |
3280af22 |
566 | PL_reg_flags |= RF_tainted; |
bbce6d69 |
567 | /* FALL THROUGH */ |
a0d0e21e |
568 | case BOUND: |
a0ed51b3 |
569 | if (minlen) { |
570 | dontbother++; |
571 | strend -= 1; |
572 | } |
3280af22 |
573 | tmp = (s != startpos) ? UCHARAT(s - 1) : PL_regprev; |
95bac841 |
574 | tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0); |
a0d0e21e |
575 | while (s < strend) { |
95bac841 |
576 | if (tmp == !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) { |
a0d0e21e |
577 | tmp = !tmp; |
578 | if (regtry(prog, s)) |
579 | goto got_it; |
a687059c |
580 | } |
a0d0e21e |
581 | s++; |
582 | } |
583 | if ((minlen || tmp) && regtry(prog,s)) |
584 | goto got_it; |
585 | break; |
a0ed51b3 |
586 | case BOUNDLUTF8: |
587 | PL_reg_flags |= RF_tainted; |
588 | /* FALL THROUGH */ |
589 | case BOUNDUTF8: |
590 | if (minlen) { |
591 | dontbother++; |
dfe13c55 |
592 | strend = reghop_c(strend, -1); |
a0ed51b3 |
593 | } |
dfe13c55 |
594 | tmp = (I32)(s != startpos) ? utf8_to_uv(reghop((U8*)s, -1), 0) : PL_regprev; |
a0ed51b3 |
595 | tmp = ((OP(c) == BOUND ? isALNUM_uni(tmp) : isALNUM_LC_uni(tmp)) != 0); |
596 | while (s < strend) { |
dfe13c55 |
597 | if (tmp == !(OP(c) == BOUND ? |
598 | swash_fetch(PL_utf8_alnum, (U8*)s) : |
599 | isALNUM_LC_utf8((U8*)s))) |
600 | { |
a0ed51b3 |
601 | tmp = !tmp; |
602 | if (regtry(prog, s)) |
603 | goto got_it; |
604 | } |
605 | s += UTF8SKIP(s); |
606 | } |
607 | if ((minlen || tmp) && regtry(prog,s)) |
608 | goto got_it; |
609 | break; |
bbce6d69 |
610 | case NBOUNDL: |
3280af22 |
611 | PL_reg_flags |= RF_tainted; |
bbce6d69 |
612 | /* FALL THROUGH */ |
a0d0e21e |
613 | case NBOUND: |
a0ed51b3 |
614 | if (minlen) { |
615 | dontbother++; |
616 | strend -= 1; |
617 | } |
3280af22 |
618 | tmp = (s != startpos) ? UCHARAT(s - 1) : PL_regprev; |
95bac841 |
619 | tmp = ((OP(c) == NBOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0); |
a0d0e21e |
620 | while (s < strend) { |
95bac841 |
621 | if (tmp == !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s))) |
a0d0e21e |
622 | tmp = !tmp; |
623 | else if (regtry(prog, s)) |
624 | goto got_it; |
625 | s++; |
626 | } |
627 | if ((minlen || !tmp) && regtry(prog,s)) |
628 | goto got_it; |
629 | break; |
a0ed51b3 |
630 | case NBOUNDLUTF8: |
631 | PL_reg_flags |= RF_tainted; |
632 | /* FALL THROUGH */ |
633 | case NBOUNDUTF8: |
634 | if (minlen) { |
635 | dontbother++; |
dfe13c55 |
636 | strend = reghop_c(strend, -1); |
a0ed51b3 |
637 | } |
dfe13c55 |
638 | tmp = (I32)(s != startpos) ? utf8_to_uv(reghop((U8*)s, -1), 0) : PL_regprev; |
a0ed51b3 |
639 | tmp = ((OP(c) == NBOUND ? isALNUM_uni(tmp) : isALNUM_LC_uni(tmp)) != 0); |
640 | while (s < strend) { |
dfe13c55 |
641 | if (tmp == !(OP(c) == NBOUND ? |
642 | swash_fetch(PL_utf8_alnum, (U8*)s) : |
643 | isALNUM_LC_utf8((U8*)s))) |
a0ed51b3 |
644 | tmp = !tmp; |
645 | else if (regtry(prog, s)) |
646 | goto got_it; |
647 | s += UTF8SKIP(s); |
648 | } |
649 | if ((minlen || !tmp) && regtry(prog,s)) |
650 | goto got_it; |
651 | break; |
a0d0e21e |
652 | case ALNUM: |
653 | while (s < strend) { |
bbce6d69 |
654 | if (isALNUM(*s)) { |
655 | if (tmp && regtry(prog, s)) |
656 | goto got_it; |
657 | else |
658 | tmp = doevery; |
659 | } |
660 | else |
661 | tmp = 1; |
662 | s++; |
663 | } |
664 | break; |
a0ed51b3 |
665 | case ALNUMUTF8: |
666 | while (s < strend) { |
dfe13c55 |
667 | if (swash_fetch(PL_utf8_alnum, (U8*)s)) { |
a0ed51b3 |
668 | if (tmp && regtry(prog, s)) |
669 | goto got_it; |
670 | else |
671 | tmp = doevery; |
672 | } |
673 | else |
674 | tmp = 1; |
675 | s += UTF8SKIP(s); |
676 | } |
677 | break; |
bbce6d69 |
678 | case ALNUML: |
3280af22 |
679 | PL_reg_flags |= RF_tainted; |
bbce6d69 |
680 | while (s < strend) { |
681 | if (isALNUM_LC(*s)) { |
a0d0e21e |
682 | if (tmp && regtry(prog, s)) |
683 | goto got_it; |
a687059c |
684 | else |
a0d0e21e |
685 | tmp = doevery; |
686 | } |
687 | else |
688 | tmp = 1; |
689 | s++; |
690 | } |
691 | break; |
a0ed51b3 |
692 | case ALNUMLUTF8: |
693 | PL_reg_flags |= RF_tainted; |
694 | while (s < strend) { |
dfe13c55 |
695 | if (isALNUM_LC_utf8((U8*)s)) { |
a0ed51b3 |
696 | if (tmp && regtry(prog, s)) |
697 | goto got_it; |
698 | else |
699 | tmp = doevery; |
700 | } |
701 | else |
702 | tmp = 1; |
703 | s += UTF8SKIP(s); |
704 | } |
705 | break; |
a0d0e21e |
706 | case NALNUM: |
707 | while (s < strend) { |
bbce6d69 |
708 | if (!isALNUM(*s)) { |
709 | if (tmp && regtry(prog, s)) |
710 | goto got_it; |
711 | else |
712 | tmp = doevery; |
713 | } |
714 | else |
715 | tmp = 1; |
716 | s++; |
717 | } |
718 | break; |
a0ed51b3 |
719 | case NALNUMUTF8: |
720 | while (s < strend) { |
dfe13c55 |
721 | if (!swash_fetch(PL_utf8_alnum, (U8*)s)) { |
a0ed51b3 |
722 | if (tmp && regtry(prog, s)) |
723 | goto got_it; |
724 | else |
725 | tmp = doevery; |
726 | } |
727 | else |
728 | tmp = 1; |
729 | s += UTF8SKIP(s); |
730 | } |
731 | break; |
bbce6d69 |
732 | case NALNUML: |
3280af22 |
733 | PL_reg_flags |= RF_tainted; |
bbce6d69 |
734 | while (s < strend) { |
735 | if (!isALNUM_LC(*s)) { |
a0d0e21e |
736 | if (tmp && regtry(prog, s)) |
737 | goto got_it; |
a687059c |
738 | else |
a0d0e21e |
739 | tmp = doevery; |
a687059c |
740 | } |
a0d0e21e |
741 | else |
742 | tmp = 1; |
743 | s++; |
744 | } |
745 | break; |
a0ed51b3 |
746 | case NALNUMLUTF8: |
747 | PL_reg_flags |= RF_tainted; |
748 | while (s < strend) { |
dfe13c55 |
749 | if (!isALNUM_LC_utf8((U8*)s)) { |
a0ed51b3 |
750 | if (tmp && regtry(prog, s)) |
751 | goto got_it; |
752 | else |
753 | tmp = doevery; |
754 | } |
755 | else |
756 | tmp = 1; |
757 | s += UTF8SKIP(s); |
758 | } |
759 | break; |
a0d0e21e |
760 | case SPACE: |
761 | while (s < strend) { |
762 | if (isSPACE(*s)) { |
763 | if (tmp && regtry(prog, s)) |
764 | goto got_it; |
765 | else |
766 | tmp = doevery; |
2304df62 |
767 | } |
a0d0e21e |
768 | else |
769 | tmp = 1; |
770 | s++; |
771 | } |
772 | break; |
a0ed51b3 |
773 | case SPACEUTF8: |
774 | while (s < strend) { |
dfe13c55 |
775 | if (*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s)) { |
a0ed51b3 |
776 | if (tmp && regtry(prog, s)) |
777 | goto got_it; |
778 | else |
779 | tmp = doevery; |
780 | } |
781 | else |
782 | tmp = 1; |
783 | s += UTF8SKIP(s); |
784 | } |
785 | break; |
bbce6d69 |
786 | case SPACEL: |
3280af22 |
787 | PL_reg_flags |= RF_tainted; |
bbce6d69 |
788 | while (s < strend) { |
789 | if (isSPACE_LC(*s)) { |
790 | if (tmp && regtry(prog, s)) |
791 | goto got_it; |
792 | else |
793 | tmp = doevery; |
794 | } |
795 | else |
796 | tmp = 1; |
797 | s++; |
798 | } |
799 | break; |
a0ed51b3 |
800 | case SPACELUTF8: |
801 | PL_reg_flags |= RF_tainted; |
802 | while (s < strend) { |
dfe13c55 |
803 | if (*s == ' ' || isSPACE_LC_utf8((U8*)s)) { |
a0ed51b3 |
804 | if (tmp && regtry(prog, s)) |
805 | goto got_it; |
806 | else |
807 | tmp = doevery; |
808 | } |
809 | else |
810 | tmp = 1; |
811 | s += UTF8SKIP(s); |
812 | } |
813 | break; |
a0d0e21e |
814 | case NSPACE: |
815 | while (s < strend) { |
816 | if (!isSPACE(*s)) { |
817 | if (tmp && regtry(prog, s)) |
818 | goto got_it; |
819 | else |
820 | tmp = doevery; |
a687059c |
821 | } |
a0d0e21e |
822 | else |
823 | tmp = 1; |
824 | s++; |
825 | } |
826 | break; |
a0ed51b3 |
827 | case NSPACEUTF8: |
828 | while (s < strend) { |
dfe13c55 |
829 | if (!(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s))) { |
a0ed51b3 |
830 | if (tmp && regtry(prog, s)) |
831 | goto got_it; |
832 | else |
833 | tmp = doevery; |
834 | } |
835 | else |
836 | tmp = 1; |
837 | s += UTF8SKIP(s); |
838 | } |
839 | break; |
bbce6d69 |
840 | case NSPACEL: |
3280af22 |
841 | PL_reg_flags |= RF_tainted; |
bbce6d69 |
842 | while (s < strend) { |
843 | if (!isSPACE_LC(*s)) { |
844 | if (tmp && regtry(prog, s)) |
845 | goto got_it; |
846 | else |
847 | tmp = doevery; |
848 | } |
849 | else |
850 | tmp = 1; |
851 | s++; |
852 | } |
853 | break; |
a0ed51b3 |
854 | case NSPACELUTF8: |
855 | PL_reg_flags |= RF_tainted; |
856 | while (s < strend) { |
dfe13c55 |
857 | if (!(*s == ' ' || isSPACE_LC_utf8((U8*)s))) { |
a0ed51b3 |
858 | if (tmp && regtry(prog, s)) |
859 | goto got_it; |
860 | else |
861 | tmp = doevery; |
862 | } |
863 | else |
864 | tmp = 1; |
865 | s += UTF8SKIP(s); |
866 | } |
867 | break; |
a0d0e21e |
868 | case DIGIT: |
869 | while (s < strend) { |
870 | if (isDIGIT(*s)) { |
871 | if (tmp && regtry(prog, s)) |
872 | goto got_it; |
873 | else |
874 | tmp = doevery; |
2b69d0c2 |
875 | } |
a0d0e21e |
876 | else |
877 | tmp = 1; |
878 | s++; |
879 | } |
880 | break; |
a0ed51b3 |
881 | case DIGITUTF8: |
882 | while (s < strend) { |
dfe13c55 |
883 | if (swash_fetch(PL_utf8_digit,(U8*)s)) { |
a0ed51b3 |
884 | if (tmp && regtry(prog, s)) |
885 | goto got_it; |
886 | else |
887 | tmp = doevery; |
888 | } |
889 | else |
890 | tmp = 1; |
891 | s += UTF8SKIP(s); |
892 | } |
893 | break; |
a0d0e21e |
894 | case NDIGIT: |
895 | while (s < strend) { |
896 | if (!isDIGIT(*s)) { |
897 | if (tmp && regtry(prog, s)) |
898 | goto got_it; |
899 | else |
900 | tmp = doevery; |
a687059c |
901 | } |
a0d0e21e |
902 | else |
903 | tmp = 1; |
904 | s++; |
905 | } |
906 | break; |
a0ed51b3 |
907 | case NDIGITUTF8: |
908 | while (s < strend) { |
dfe13c55 |
909 | if (!swash_fetch(PL_utf8_digit,(U8*)s)) { |
a0ed51b3 |
910 | if (tmp && regtry(prog, s)) |
911 | goto got_it; |
912 | else |
913 | tmp = doevery; |
914 | } |
915 | else |
916 | tmp = 1; |
917 | s += UTF8SKIP(s); |
918 | } |
919 | break; |
a687059c |
920 | } |
a0d0e21e |
921 | } |
922 | else { |
c277df42 |
923 | dontbother = 0; |
924 | if (prog->float_substr != Nullsv) { /* Trim the end. */ |
925 | char *last; |
926 | I32 oldpos = scream_pos; |
927 | |
22e551b9 |
928 | if (flags & REXEC_SCREAM) { |
929 | last = screaminstr(sv, prog->float_substr, s - strbeg, |
c277df42 |
930 | end_shift, &scream_pos, 1); /* last one */ |
931 | if (!last) { |
932 | last = scream_olds; /* Only one occurence. */ |
933 | } |
a0ed51b3 |
934 | } |
935 | else { |
c277df42 |
936 | STRLEN len; |
937 | char *little = SvPV(prog->float_substr, len); |
19b4f81a |
938 | if (len) |
939 | last = rninstr(s, strend, little, little + len); |
940 | else |
941 | last = strend; /* matching `$' */ |
c277df42 |
942 | } |
943 | if (last == NULL) goto phooey; /* Should not happen! */ |
19b4f81a |
944 | dontbother = strend - last + prog->float_min_offset; |
c277df42 |
945 | } |
946 | if (minlen && (dontbother < minlen)) |
a0d0e21e |
947 | dontbother = minlen - 1; |
a0ed51b3 |
948 | strend -= dontbother; /* this one's always in bytes! */ |
a0d0e21e |
949 | /* We don't know much -- general case. */ |
a0ed51b3 |
950 | if (UTF) { |
951 | for (;;) { |
84df6dba |
952 | if (regtry(prog, s)) |
a0ed51b3 |
953 | goto got_it; |
a0ed51b3 |
954 | if (s >= strend) |
955 | break; |
956 | s += UTF8SKIP(s); |
957 | }; |
958 | } |
959 | else { |
960 | do { |
961 | if (regtry(prog, s)) |
962 | goto got_it; |
963 | } while (s++ < strend); |
964 | } |
a0d0e21e |
965 | } |
966 | |
967 | /* Failure. */ |
968 | goto phooey; |
a687059c |
969 | |
a0d0e21e |
970 | got_it: |
971 | prog->subbeg = strbeg; |
19b4f81a |
972 | prog->subend = PL_regeol; /* strend may have been modified */ |
3280af22 |
973 | RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted); |
5f05dabc |
974 | |
975 | /* make sure $`, $&, $', and $digit will work later */ |
c277df42 |
976 | if (strbeg != prog->subbase) { /* second+ //g match. */ |
977 | if (!(flags & REXEC_COPY_STR)) { |
137443ea |
978 | if (prog->subbase) { |
979 | Safefree(prog->subbase); |
980 | prog->subbase = Nullch; |
981 | } |
982 | } |
983 | else { |
19b4f81a |
984 | I32 i = PL_regeol - startpos + (stringarg - strbeg); |
137443ea |
985 | s = savepvn(strbeg, i); |
986 | Safefree(prog->subbase); |
987 | prog->subbase = s; |
988 | prog->subbeg = prog->subbase; |
989 | prog->subend = prog->subbase + i; |
990 | s = prog->subbase + (stringarg - strbeg); |
991 | for (i = 0; i <= prog->nparens; i++) { |
992 | if (prog->endp[i]) { |
993 | prog->startp[i] = s + (prog->startp[i] - startpos); |
994 | prog->endp[i] = s + (prog->endp[i] - startpos); |
995 | } |
a0d0e21e |
996 | } |
997 | } |
a0d0e21e |
998 | } |
ce862d02 |
999 | /* Preserve the current value of $^R */ |
3280af22 |
1000 | if (oreplsv != GvSV(PL_replgv)) { |
1001 | sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is |
ce862d02 |
1002 | restored, the value remains |
1003 | the same. */ |
1004 | } |
a0d0e21e |
1005 | return 1; |
1006 | |
1007 | phooey: |
a0d0e21e |
1008 | return 0; |
a687059c |
1009 | } |
1010 | |
1011 | /* |
1012 | - regtry - try match at specific point |
1013 | */ |
76e3520e |
1014 | STATIC I32 /* 0 failure, 1 success */ |
8ac85365 |
1015 | regtry(regexp *prog, char *startpos) |
a687059c |
1016 | { |
c277df42 |
1017 | dTHR; |
a0d0e21e |
1018 | register I32 i; |
1019 | register char **sp; |
1020 | register char **ep; |
c277df42 |
1021 | CHECKPOINT lastcp; |
a0d0e21e |
1022 | |
3280af22 |
1023 | if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) { |
1024 | PL_reg_eval_set = RS_init; |
ce862d02 |
1025 | DEBUG_r(DEBUG_s( |
c3464db5 |
1026 | PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %i\n", |
3280af22 |
1027 | PL_stack_sp - PL_stack_base); |
ce862d02 |
1028 | )); |
1029 | SAVEINT(cxstack[cxstack_ix].blk_oldsp); |
3280af22 |
1030 | cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base; |
ce862d02 |
1031 | /* Otherwise OP_NEXTSTATE will free whatever on stack now. */ |
1032 | SAVETMPS; |
1033 | /* Apparently this is not needed, judging by wantarray. */ |
1034 | /* SAVEINT(cxstack[cxstack_ix].blk_gimme); |
1035 | cxstack[cxstack_ix].blk_gimme = G_SCALAR; */ |
1036 | } |
3280af22 |
1037 | PL_reginput = startpos; |
1038 | PL_regstartp = prog->startp; |
1039 | PL_regendp = prog->endp; |
1040 | PL_reglastparen = &prog->lastparen; |
a0d0e21e |
1041 | prog->lastparen = 0; |
3280af22 |
1042 | PL_regsize = 0; |
1043 | if (PL_reg_start_tmpl <= prog->nparens) { |
1044 | PL_reg_start_tmpl = prog->nparens*3/2 + 3; |
1045 | if(PL_reg_start_tmp) |
1046 | Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*); |
c277df42 |
1047 | else |
3280af22 |
1048 | New(22,PL_reg_start_tmp, PL_reg_start_tmpl, char*); |
c277df42 |
1049 | } |
a0d0e21e |
1050 | |
1051 | sp = prog->startp; |
1052 | ep = prog->endp; |
1053 | if (prog->nparens) { |
1054 | for (i = prog->nparens; i >= 0; i--) { |
1055 | *sp++ = NULL; |
1056 | *ep++ = NULL; |
a687059c |
1057 | } |
a0d0e21e |
1058 | } |
c277df42 |
1059 | REGCP_SET; |
7e5428c5 |
1060 | if (regmatch(prog->program + 1)) { |
a0d0e21e |
1061 | prog->startp[0] = startpos; |
3280af22 |
1062 | prog->endp[0] = PL_reginput; |
a0d0e21e |
1063 | return 1; |
1064 | } |
c277df42 |
1065 | REGCP_UNWIND; |
1066 | return 0; |
a687059c |
1067 | } |
1068 | |
1069 | /* |
1070 | - regmatch - main matching routine |
1071 | * |
1072 | * Conceptually the strategy is simple: check to see whether the current |
1073 | * node matches, call self recursively to see whether the rest matches, |
1074 | * and then act accordingly. In practice we make some effort to avoid |
1075 | * recursion, in particular by going through "ordinary" nodes (that don't |
1076 | * need to know whether the rest of the match failed) by a loop instead of |
1077 | * by recursion. |
1078 | */ |
1079 | /* [lwall] I've hoisted the register declarations to the outer block in order to |
1080 | * maybe save a little bit of pushing and popping on the stack. It also takes |
1081 | * advantage of machines that use a register save mask on subroutine entry. |
1082 | */ |
76e3520e |
1083 | STATIC I32 /* 0 failure, 1 success */ |
c277df42 |
1084 | regmatch(regnode *prog) |
a687059c |
1085 | { |
c277df42 |
1086 | dTHR; |
1087 | register regnode *scan; /* Current node. */ |
1088 | regnode *next; /* Next node. */ |
1089 | regnode *inner; /* Next node in internal branch. */ |
c3464db5 |
1090 | register I32 nextchr; /* renamed nextchr - nextchar colides with |
1091 | function of same name */ |
a0d0e21e |
1092 | register I32 n; /* no or next */ |
1093 | register I32 ln; /* len or last */ |
1094 | register char *s; /* operand or save */ |
3280af22 |
1095 | register char *locinput = PL_reginput; |
c277df42 |
1096 | register I32 c1, c2, paren; /* case fold search, parenth */ |
1097 | int minmod = 0, sw = 0, logical = 0; |
4633a7c4 |
1098 | #ifdef DEBUGGING |
3280af22 |
1099 | PL_regindent++; |
4633a7c4 |
1100 | #endif |
a0d0e21e |
1101 | |
a0ed51b3 |
1102 | /* Note that nextchr is a byte even in UTF */ |
76e3520e |
1103 | nextchr = UCHARAT(locinput); |
a0d0e21e |
1104 | scan = prog; |
1105 | while (scan != NULL) { |
c277df42 |
1106 | #define sayNO_L (logical ? (logical = 0, sw = 0, goto cont) : sayNO) |
a687059c |
1107 | #ifdef DEBUGGING |
c277df42 |
1108 | # define sayYES goto yes |
1109 | # define sayNO goto no |
1110 | # define saySAME(x) if (x) goto yes; else goto no |
1111 | # define REPORT_CODE_OFF 24 |
4633a7c4 |
1112 | #else |
c277df42 |
1113 | # define sayYES return 1 |
1114 | # define sayNO return 0 |
1115 | # define saySAME(x) return x |
a687059c |
1116 | #endif |
c277df42 |
1117 | DEBUG_r( { |
1118 | SV *prop = sv_newmortal(); |
3280af22 |
1119 | int docolor = *PL_colors[0]; |
c277df42 |
1120 | int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */ |
3280af22 |
1121 | int l = (PL_regeol - locinput > taill ? taill : PL_regeol - locinput); |
1122 | int pref_len = (locinput - PL_bostr > (5 + taill) - l |
1123 | ? (5 + taill) - l : locinput - PL_bostr); |
8d300b32 |
1124 | int pref0_len = pref_len - (locinput - PL_reginput); |
c277df42 |
1125 | |
3280af22 |
1126 | if (l + pref_len < (5 + taill) && l < PL_regeol - locinput) |
1127 | l = ( PL_regeol - locinput > (5 + taill) - pref_len |
1128 | ? (5 + taill) - pref_len : PL_regeol - locinput); |
8d300b32 |
1129 | if (pref0_len < 0) |
1130 | pref0_len = 0; |
c277df42 |
1131 | regprop(prop, scan); |
1132 | PerlIO_printf(Perl_debug_log, |
8d300b32 |
1133 | "%4i <%s%.*s%s%s%.*s%s%s%s%.*s%s>%*s|%3d:%*s%s\n", |
3280af22 |
1134 | locinput - PL_bostr, |
8d300b32 |
1135 | PL_colors[4], pref0_len, |
1136 | locinput - pref_len, PL_colors[5], |
1137 | PL_colors[2], pref_len - pref0_len, |
1138 | locinput - pref_len + pref0_len, PL_colors[3], |
c277df42 |
1139 | (docolor ? "" : "> <"), |
3280af22 |
1140 | PL_colors[0], l, locinput, PL_colors[1], |
c277df42 |
1141 | 15 - l - pref_len + 1, |
1142 | "", |
3280af22 |
1143 | scan - PL_regprogram, PL_regindent*2, "", |
c277df42 |
1144 | SvPVX(prop)); |
1145 | } ); |
a687059c |
1146 | |
c277df42 |
1147 | next = scan + NEXT_OFF(scan); |
a0d0e21e |
1148 | if (next == scan) |
1149 | next = NULL; |
a687059c |
1150 | |
a0d0e21e |
1151 | switch (OP(scan)) { |
1152 | case BOL: |
3280af22 |
1153 | if (locinput == PL_bostr |
1154 | ? PL_regprev == '\n' |
1155 | : (PL_multiline && |
1156 | (nextchr || locinput < PL_regeol) && locinput[-1] == '\n') ) |
a0d0e21e |
1157 | { |
a0ed51b3 |
1158 | /* regtill = regbol; */ |
a0d0e21e |
1159 | break; |
1160 | } |
4633a7c4 |
1161 | sayNO; |
a0d0e21e |
1162 | case MBOL: |
3280af22 |
1163 | if (locinput == PL_bostr |
1164 | ? PL_regprev == '\n' |
1165 | : ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n') ) |
a0d0e21e |
1166 | { |
1167 | break; |
1168 | } |
4633a7c4 |
1169 | sayNO; |
a0d0e21e |
1170 | case SBOL: |
3280af22 |
1171 | if (locinput == PL_regbol && PL_regprev == '\n') |
a0d0e21e |
1172 | break; |
4633a7c4 |
1173 | sayNO; |
774d564b |
1174 | case GPOS: |
22e551b9 |
1175 | if (locinput == PL_reg_ganch) |
a0d0e21e |
1176 | break; |
4633a7c4 |
1177 | sayNO; |
a0d0e21e |
1178 | case EOL: |
3280af22 |
1179 | if (PL_multiline) |
a0d0e21e |
1180 | goto meol; |
1181 | else |
1182 | goto seol; |
1183 | case MEOL: |
1184 | meol: |
3280af22 |
1185 | if ((nextchr || locinput < PL_regeol) && nextchr != '\n') |
4633a7c4 |
1186 | sayNO; |
a0d0e21e |
1187 | break; |
1188 | case SEOL: |
1189 | seol: |
3280af22 |
1190 | if ((nextchr || locinput < PL_regeol) && nextchr != '\n') |
4633a7c4 |
1191 | sayNO; |
3280af22 |
1192 | if (PL_regeol - locinput > 1) |
4633a7c4 |
1193 | sayNO; |
a0d0e21e |
1194 | break; |
b85d18e9 |
1195 | case EOS: |
3280af22 |
1196 | if (PL_regeol != locinput) |
b85d18e9 |
1197 | sayNO; |
1198 | break; |
a0ed51b3 |
1199 | case SANYUTF8: |
1200 | if (nextchr & 0x80) { |
6f06b55f |
1201 | locinput += PL_utf8skip[nextchr]; |
a0ed51b3 |
1202 | if (locinput > PL_regeol) |
1203 | sayNO; |
1204 | nextchr = UCHARAT(locinput); |
1205 | break; |
1206 | } |
1207 | if (!nextchr && locinput >= PL_regeol) |
1208 | sayNO; |
1209 | nextchr = UCHARAT(++locinput); |
1210 | break; |
a0d0e21e |
1211 | case SANY: |
3280af22 |
1212 | if (!nextchr && locinput >= PL_regeol) |
4633a7c4 |
1213 | sayNO; |
76e3520e |
1214 | nextchr = UCHARAT(++locinput); |
a0d0e21e |
1215 | break; |
a0ed51b3 |
1216 | case ANYUTF8: |
1217 | if (nextchr & 0x80) { |
6f06b55f |
1218 | locinput += PL_utf8skip[nextchr]; |
a0ed51b3 |
1219 | if (locinput > PL_regeol) |
1220 | sayNO; |
1221 | nextchr = UCHARAT(locinput); |
1222 | break; |
1223 | } |
1224 | if (!nextchr && locinput >= PL_regeol || nextchr == '\n') |
1225 | sayNO; |
1226 | nextchr = UCHARAT(++locinput); |
1227 | break; |
22c35a8c |
1228 | case REG_ANY: |
3280af22 |
1229 | if (!nextchr && locinput >= PL_regeol || nextchr == '\n') |
4633a7c4 |
1230 | sayNO; |
76e3520e |
1231 | nextchr = UCHARAT(++locinput); |
a0d0e21e |
1232 | break; |
bbce6d69 |
1233 | case EXACT: |
161b471a |
1234 | s = (char *) OPERAND(scan); |
c277df42 |
1235 | ln = UCHARAT(s++); |
a0d0e21e |
1236 | /* Inline the first character, for speed. */ |
76e3520e |
1237 | if (UCHARAT(s) != nextchr) |
4633a7c4 |
1238 | sayNO; |
3280af22 |
1239 | if (PL_regeol - locinput < ln) |
4633a7c4 |
1240 | sayNO; |
36477c24 |
1241 | if (ln > 1 && memNE(s, locinput, ln)) |
4633a7c4 |
1242 | sayNO; |
a0d0e21e |
1243 | locinput += ln; |
76e3520e |
1244 | nextchr = UCHARAT(locinput); |
bbce6d69 |
1245 | break; |
1246 | case EXACTFL: |
3280af22 |
1247 | PL_reg_flags |= RF_tainted; |
bbce6d69 |
1248 | /* FALL THROUGH */ |
1249 | case EXACTF: |
161b471a |
1250 | s = (char *) OPERAND(scan); |
c277df42 |
1251 | ln = UCHARAT(s++); |
a0ed51b3 |
1252 | |
1253 | if (UTF) { |
1254 | char *l = locinput; |
1255 | char *e = s + ln; |
1256 | c1 = OP(scan) == EXACTF; |
1257 | while (s < e) { |
1258 | if (l >= PL_regeol) |
1259 | sayNO; |
dfe13c55 |
1260 | if (utf8_to_uv((U8*)s, 0) != (c1 ? |
1261 | toLOWER_utf8((U8*)l) : |
1262 | toLOWER_LC_utf8((U8*)l))) |
1263 | { |
a0ed51b3 |
1264 | sayNO; |
dfe13c55 |
1265 | } |
a0ed51b3 |
1266 | s += UTF8SKIP(s); |
1267 | l += UTF8SKIP(l); |
1268 | } |
1269 | locinput = l; |
1270 | nextchr = UCHARAT(locinput); |
1271 | break; |
1272 | } |
1273 | |
bbce6d69 |
1274 | /* Inline the first character, for speed. */ |
76e3520e |
1275 | if (UCHARAT(s) != nextchr && |
bbce6d69 |
1276 | UCHARAT(s) != ((OP(scan) == EXACTF) |
22c35a8c |
1277 | ? PL_fold : PL_fold_locale)[nextchr]) |
bbce6d69 |
1278 | sayNO; |
3280af22 |
1279 | if (PL_regeol - locinput < ln) |
bbce6d69 |
1280 | sayNO; |
5f05dabc |
1281 | if (ln > 1 && (OP(scan) == EXACTF |
1282 | ? ibcmp(s, locinput, ln) |
1283 | : ibcmp_locale(s, locinput, ln))) |
bbce6d69 |
1284 | sayNO; |
1285 | locinput += ln; |
76e3520e |
1286 | nextchr = UCHARAT(locinput); |
a0d0e21e |
1287 | break; |
a0ed51b3 |
1288 | case ANYOFUTF8: |
1289 | s = (char *) OPERAND(scan); |
1290 | if (!REGINCLASSUTF8(scan, (U8*)locinput)) |
1291 | sayNO; |
1292 | if (locinput >= PL_regeol) |
1293 | sayNO; |
6f06b55f |
1294 | locinput += PL_utf8skip[nextchr]; |
a0ed51b3 |
1295 | nextchr = UCHARAT(locinput); |
1296 | break; |
a0d0e21e |
1297 | case ANYOF: |
161b471a |
1298 | s = (char *) OPERAND(scan); |
76e3520e |
1299 | if (nextchr < 0) |
1300 | nextchr = UCHARAT(locinput); |
873ef191 |
1301 | if (!REGINCLASS(s, nextchr)) |
4633a7c4 |
1302 | sayNO; |
3280af22 |
1303 | if (!nextchr && locinput >= PL_regeol) |
4633a7c4 |
1304 | sayNO; |
76e3520e |
1305 | nextchr = UCHARAT(++locinput); |
a0d0e21e |
1306 | break; |
bbce6d69 |
1307 | case ALNUML: |
3280af22 |
1308 | PL_reg_flags |= RF_tainted; |
bbce6d69 |
1309 | /* FALL THROUGH */ |
a0d0e21e |
1310 | case ALNUM: |
76e3520e |
1311 | if (!nextchr) |
4633a7c4 |
1312 | sayNO; |
bbce6d69 |
1313 | if (!(OP(scan) == ALNUM |
76e3520e |
1314 | ? isALNUM(nextchr) : isALNUM_LC(nextchr))) |
4633a7c4 |
1315 | sayNO; |
76e3520e |
1316 | nextchr = UCHARAT(++locinput); |
a0d0e21e |
1317 | break; |
a0ed51b3 |
1318 | case ALNUMLUTF8: |
1319 | PL_reg_flags |= RF_tainted; |
1320 | /* FALL THROUGH */ |
1321 | case ALNUMUTF8: |
1322 | if (!nextchr) |
1323 | sayNO; |
1324 | if (nextchr & 0x80) { |
1325 | if (!(OP(scan) == ALNUMUTF8 |
dfe13c55 |
1326 | ? swash_fetch(PL_utf8_alnum, (U8*)locinput) |
1327 | : isALNUM_LC_utf8((U8*)locinput))) |
1328 | { |
a0ed51b3 |
1329 | sayNO; |
dfe13c55 |
1330 | } |
6f06b55f |
1331 | locinput += PL_utf8skip[nextchr]; |
a0ed51b3 |
1332 | nextchr = UCHARAT(locinput); |
1333 | break; |
1334 | } |
1335 | if (!(OP(scan) == ALNUMUTF8 |
1336 | ? isALNUM(nextchr) : isALNUM_LC(nextchr))) |
1337 | sayNO; |
1338 | nextchr = UCHARAT(++locinput); |
1339 | break; |
bbce6d69 |
1340 | case NALNUML: |
3280af22 |
1341 | PL_reg_flags |= RF_tainted; |
bbce6d69 |
1342 | /* FALL THROUGH */ |
a0d0e21e |
1343 | case NALNUM: |
3280af22 |
1344 | if (!nextchr && locinput >= PL_regeol) |
4633a7c4 |
1345 | sayNO; |
bbce6d69 |
1346 | if (OP(scan) == NALNUM |
76e3520e |
1347 | ? isALNUM(nextchr) : isALNUM_LC(nextchr)) |
4633a7c4 |
1348 | sayNO; |
76e3520e |
1349 | nextchr = UCHARAT(++locinput); |
a0d0e21e |
1350 | break; |
a0ed51b3 |
1351 | case NALNUMLUTF8: |
1352 | PL_reg_flags |= RF_tainted; |
1353 | /* FALL THROUGH */ |
1354 | case NALNUMUTF8: |
1355 | if (!nextchr && locinput >= PL_regeol) |
1356 | sayNO; |
1357 | if (nextchr & 0x80) { |
1358 | if (OP(scan) == NALNUMUTF8 |
dfe13c55 |
1359 | ? swash_fetch(PL_utf8_alnum, (U8*)locinput) |
1360 | : isALNUM_LC_utf8((U8*)locinput)) |
1361 | { |
a0ed51b3 |
1362 | sayNO; |
dfe13c55 |
1363 | } |
6f06b55f |
1364 | locinput += PL_utf8skip[nextchr]; |
a0ed51b3 |
1365 | nextchr = UCHARAT(locinput); |
1366 | break; |
1367 | } |
1368 | if (OP(scan) == NALNUMUTF8 |
1369 | ? isALNUM(nextchr) : isALNUM_LC(nextchr)) |
1370 | sayNO; |
1371 | nextchr = UCHARAT(++locinput); |
1372 | break; |
bbce6d69 |
1373 | case BOUNDL: |
1374 | case NBOUNDL: |
3280af22 |
1375 | PL_reg_flags |= RF_tainted; |
bbce6d69 |
1376 | /* FALL THROUGH */ |
a0d0e21e |
1377 | case BOUND: |
bbce6d69 |
1378 | case NBOUND: |
1379 | /* was last char in word? */ |
3280af22 |
1380 | ln = (locinput != PL_regbol) ? UCHARAT(locinput - 1) : PL_regprev; |
bbce6d69 |
1381 | if (OP(scan) == BOUND || OP(scan) == NBOUND) { |
1382 | ln = isALNUM(ln); |
76e3520e |
1383 | n = isALNUM(nextchr); |
bbce6d69 |
1384 | } |
1385 | else { |
1386 | ln = isALNUM_LC(ln); |
76e3520e |
1387 | n = isALNUM_LC(nextchr); |
bbce6d69 |
1388 | } |
95bac841 |
1389 | if (((!ln) == (!n)) == (OP(scan) == BOUND || OP(scan) == BOUNDL)) |
4633a7c4 |
1390 | sayNO; |
a0d0e21e |
1391 | break; |
a0ed51b3 |
1392 | case BOUNDLUTF8: |
1393 | case NBOUNDLUTF8: |
1394 | PL_reg_flags |= RF_tainted; |
1395 | /* FALL THROUGH */ |
1396 | case BOUNDUTF8: |
1397 | case NBOUNDUTF8: |
1398 | /* was last char in word? */ |
dfe13c55 |
1399 | ln = (locinput != PL_regbol) |
1400 | ? utf8_to_uv(reghop((U8*)locinput, -1), 0) : PL_regprev; |
a0ed51b3 |
1401 | if (OP(scan) == BOUNDUTF8 || OP(scan) == NBOUNDUTF8) { |
1402 | ln = isALNUM_uni(ln); |
dfe13c55 |
1403 | n = swash_fetch(PL_utf8_alnum, (U8*)locinput); |
a0ed51b3 |
1404 | } |
1405 | else { |
1406 | ln = isALNUM_LC_uni(ln); |
dfe13c55 |
1407 | n = isALNUM_LC_utf8((U8*)locinput); |
a0ed51b3 |
1408 | } |
1409 | if (((!ln) == (!n)) == (OP(scan) == BOUNDUTF8 || OP(scan) == BOUNDLUTF8)) |
1410 | sayNO; |
1411 | break; |
bbce6d69 |
1412 | case SPACEL: |
3280af22 |
1413 | PL_reg_flags |= RF_tainted; |
bbce6d69 |
1414 | /* FALL THROUGH */ |
a0d0e21e |
1415 | case SPACE: |
3280af22 |
1416 | if (!nextchr && locinput >= PL_regeol) |
4633a7c4 |
1417 | sayNO; |
bbce6d69 |
1418 | if (!(OP(scan) == SPACE |
76e3520e |
1419 | ? isSPACE(nextchr) : isSPACE_LC(nextchr))) |
4633a7c4 |
1420 | sayNO; |
76e3520e |
1421 | nextchr = UCHARAT(++locinput); |
a0d0e21e |
1422 | break; |
a0ed51b3 |
1423 | case SPACELUTF8: |
1424 | PL_reg_flags |= RF_tainted; |
1425 | /* FALL THROUGH */ |
1426 | case SPACEUTF8: |
1427 | if (!nextchr && locinput >= PL_regeol) |
1428 | sayNO; |
1429 | if (nextchr & 0x80) { |
1430 | if (!(OP(scan) == SPACEUTF8 |
dfe13c55 |
1431 | ? swash_fetch(PL_utf8_space,(U8*)locinput) |
1432 | : isSPACE_LC_utf8((U8*)locinput))) |
1433 | { |
a0ed51b3 |
1434 | sayNO; |
dfe13c55 |
1435 | } |
6f06b55f |
1436 | locinput += PL_utf8skip[nextchr]; |
a0ed51b3 |
1437 | nextchr = UCHARAT(locinput); |
1438 | break; |
1439 | } |
1440 | if (!(OP(scan) == SPACEUTF8 |
1441 | ? isSPACE(nextchr) : isSPACE_LC(nextchr))) |
1442 | sayNO; |
1443 | nextchr = UCHARAT(++locinput); |
1444 | break; |
bbce6d69 |
1445 | case NSPACEL: |
3280af22 |
1446 | PL_reg_flags |= RF_tainted; |
bbce6d69 |
1447 | /* FALL THROUGH */ |
a0d0e21e |
1448 | case NSPACE: |
76e3520e |
1449 | if (!nextchr) |
4633a7c4 |
1450 | sayNO; |
bbce6d69 |
1451 | if (OP(scan) == SPACE |
76e3520e |
1452 | ? isSPACE(nextchr) : isSPACE_LC(nextchr)) |
4633a7c4 |
1453 | sayNO; |
76e3520e |
1454 | nextchr = UCHARAT(++locinput); |
a0d0e21e |
1455 | break; |
a0ed51b3 |
1456 | case NSPACELUTF8: |
1457 | PL_reg_flags |= RF_tainted; |
1458 | /* FALL THROUGH */ |
1459 | case NSPACEUTF8: |
1460 | if (!nextchr) |
1461 | sayNO; |
1462 | if (nextchr & 0x80) { |
1463 | if (OP(scan) == NSPACEUTF8 |
dfe13c55 |
1464 | ? swash_fetch(PL_utf8_space,(U8*)locinput) |
1465 | : isSPACE_LC_utf8((U8*)locinput)) |
1466 | { |
a0ed51b3 |
1467 | sayNO; |
dfe13c55 |
1468 | } |
6f06b55f |
1469 | locinput += PL_utf8skip[nextchr]; |
a0ed51b3 |
1470 | nextchr = UCHARAT(locinput); |
1471 | break; |
1472 | } |
1473 | if (OP(scan) == NSPACEUTF8 |
1474 | ? isSPACE(nextchr) : isSPACE_LC(nextchr)) |
1475 | sayNO; |
1476 | nextchr = UCHARAT(++locinput); |
1477 | break; |
a0d0e21e |
1478 | case DIGIT: |
76e3520e |
1479 | if (!isDIGIT(nextchr)) |
4633a7c4 |
1480 | sayNO; |
76e3520e |
1481 | nextchr = UCHARAT(++locinput); |
a0d0e21e |
1482 | break; |
a0ed51b3 |
1483 | case DIGITUTF8: |
1484 | if (nextchr & 0x80) { |
dfe13c55 |
1485 | if (!(swash_fetch(PL_utf8_digit,(U8*)locinput))) |
a0ed51b3 |
1486 | sayNO; |
6f06b55f |
1487 | locinput += PL_utf8skip[nextchr]; |
a0ed51b3 |
1488 | nextchr = UCHARAT(locinput); |
1489 | break; |
1490 | } |
1491 | if (!isDIGIT(nextchr)) |
1492 | sayNO; |
1493 | nextchr = UCHARAT(++locinput); |
1494 | break; |
a0d0e21e |
1495 | case NDIGIT: |
3280af22 |
1496 | if (!nextchr && locinput >= PL_regeol) |
4633a7c4 |
1497 | sayNO; |
76e3520e |
1498 | if (isDIGIT(nextchr)) |
4633a7c4 |
1499 | sayNO; |
76e3520e |
1500 | nextchr = UCHARAT(++locinput); |
a0d0e21e |
1501 | break; |
a0ed51b3 |
1502 | case NDIGITUTF8: |
1503 | if (!nextchr && locinput >= PL_regeol) |
1504 | sayNO; |
1505 | if (nextchr & 0x80) { |
dfe13c55 |
1506 | if (swash_fetch(PL_utf8_digit,(U8*)locinput)) |
a0ed51b3 |
1507 | sayNO; |
6f06b55f |
1508 | locinput += PL_utf8skip[nextchr]; |
a0ed51b3 |
1509 | nextchr = UCHARAT(locinput); |
1510 | break; |
1511 | } |
1512 | if (isDIGIT(nextchr)) |
1513 | sayNO; |
1514 | nextchr = UCHARAT(++locinput); |
1515 | break; |
1516 | case CLUMP: |
dfe13c55 |
1517 | if (locinput >= PL_regeol || swash_fetch(PL_utf8_mark,(U8*)locinput)) |
a0ed51b3 |
1518 | sayNO; |
6f06b55f |
1519 | locinput += PL_utf8skip[nextchr]; |
dfe13c55 |
1520 | while (locinput < PL_regeol && swash_fetch(PL_utf8_mark,(U8*)locinput)) |
a0ed51b3 |
1521 | locinput += UTF8SKIP(locinput); |
1522 | if (locinput > PL_regeol) |
1523 | sayNO; |
1524 | nextchr = UCHARAT(locinput); |
1525 | break; |
c8756f30 |
1526 | case REFFL: |
3280af22 |
1527 | PL_reg_flags |= RF_tainted; |
c8756f30 |
1528 | /* FALL THROUGH */ |
c277df42 |
1529 | case REF: |
c8756f30 |
1530 | case REFF: |
c277df42 |
1531 | n = ARG(scan); /* which paren pair */ |
3280af22 |
1532 | s = PL_regstartp[n]; |
1533 | if (*PL_reglastparen < n || !s) |
af3f8c16 |
1534 | sayNO; /* Do not match unless seen CLOSEn. */ |
3280af22 |
1535 | if (s == PL_regendp[n]) |
a0d0e21e |
1536 | break; |
a0ed51b3 |
1537 | |
1538 | if (UTF && OP(scan) != REF) { /* REF can do byte comparison */ |
1539 | char *l = locinput; |
1540 | char *e = PL_regendp[n]; |
1541 | /* |
1542 | * Note that we can't do the "other character" lookup trick as |
1543 | * in the 8-bit case (no pun intended) because in Unicode we |
1544 | * have to map both upper and title case to lower case. |
1545 | */ |
1546 | if (OP(scan) == REFF) { |
1547 | while (s < e) { |
1548 | if (l >= PL_regeol) |
1549 | sayNO; |
dfe13c55 |
1550 | if (toLOWER_utf8((U8*)s) != toLOWER_utf8((U8*)l)) |
a0ed51b3 |
1551 | sayNO; |
1552 | s += UTF8SKIP(s); |
1553 | l += UTF8SKIP(l); |
1554 | } |
1555 | } |
1556 | else { |
1557 | while (s < e) { |
1558 | if (l >= PL_regeol) |
1559 | sayNO; |
dfe13c55 |
1560 | if (toLOWER_LC_utf8((U8*)s) != toLOWER_LC_utf8((U8*)l)) |
a0ed51b3 |
1561 | sayNO; |
1562 | s += UTF8SKIP(s); |
1563 | l += UTF8SKIP(l); |
1564 | } |
1565 | } |
1566 | locinput = l; |
1567 | nextchr = UCHARAT(locinput); |
1568 | break; |
1569 | } |
1570 | |
a0d0e21e |
1571 | /* Inline the first character, for speed. */ |
76e3520e |
1572 | if (UCHARAT(s) != nextchr && |
c8756f30 |
1573 | (OP(scan) == REF || |
1574 | (UCHARAT(s) != ((OP(scan) == REFF |
22c35a8c |
1575 | ? PL_fold : PL_fold_locale)[nextchr])))) |
4633a7c4 |
1576 | sayNO; |
3280af22 |
1577 | ln = PL_regendp[n] - s; |
1578 | if (locinput + ln > PL_regeol) |
4633a7c4 |
1579 | sayNO; |
c8756f30 |
1580 | if (ln > 1 && (OP(scan) == REF |
1581 | ? memNE(s, locinput, ln) |
1582 | : (OP(scan) == REFF |
1583 | ? ibcmp(s, locinput, ln) |
1584 | : ibcmp_locale(s, locinput, ln)))) |
4633a7c4 |
1585 | sayNO; |
a0d0e21e |
1586 | locinput += ln; |
76e3520e |
1587 | nextchr = UCHARAT(locinput); |
a0d0e21e |
1588 | break; |
1589 | |
1590 | case NOTHING: |
c277df42 |
1591 | case TAIL: |
a0d0e21e |
1592 | break; |
1593 | case BACK: |
1594 | break; |
c277df42 |
1595 | case EVAL: |
1596 | { |
1597 | dSP; |
533c011a |
1598 | OP_4tree *oop = PL_op; |
3280af22 |
1599 | COP *ocurcop = PL_curcop; |
1600 | SV **ocurpad = PL_curpad; |
c277df42 |
1601 | SV *ret; |
1602 | |
1603 | n = ARG(scan); |
533c011a |
1604 | PL_op = (OP_4tree*)PL_regdata->data[n]; |
1605 | DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%x\n", PL_op) ); |
3280af22 |
1606 | PL_curpad = AvARRAY((AV*)PL_regdata->data[n + 1]); |
c277df42 |
1607 | |
76e3520e |
1608 | CALLRUNOPS(); /* Scalar context. */ |
c277df42 |
1609 | SPAGAIN; |
1610 | ret = POPs; |
1611 | PUTBACK; |
1612 | |
0f5d15d6 |
1613 | PL_op = oop; |
1614 | PL_curpad = ocurpad; |
1615 | PL_curcop = ocurcop; |
c277df42 |
1616 | if (logical) { |
0f5d15d6 |
1617 | if (logical == 2) { /* Postponed subexpression. */ |
1618 | regexp *re; |
22c35a8c |
1619 | MAGIC *mg = Null(MAGIC*); |
0f5d15d6 |
1620 | re_cc_state state; |
1621 | CURCUR cctmp; |
1622 | CHECKPOINT cp, lastcp; |
1623 | |
1624 | if(SvROK(ret) || SvRMAGICAL(ret)) { |
1625 | SV *sv = SvROK(ret) ? SvRV(ret) : ret; |
1626 | |
1627 | if(SvMAGICAL(sv)) |
1628 | mg = mg_find(sv, 'r'); |
1629 | } |
1630 | if (mg) { |
1631 | re = (regexp *)mg->mg_obj; |
df0003d4 |
1632 | (void)ReREFCNT_inc(re); |
0f5d15d6 |
1633 | } |
1634 | else { |
1635 | STRLEN len; |
1636 | char *t = SvPV(ret, len); |
1637 | PMOP pm; |
1638 | char *oprecomp = PL_regprecomp; |
1639 | I32 osize = PL_regsize; |
1640 | I32 onpar = PL_regnpar; |
1641 | |
1642 | pm.op_pmflags = 0; |
1643 | re = CALLREGCOMP(t, t + len, &pm); |
1644 | if (!(SvFLAGS(ret) |
1645 | & (SVs_TEMP | SVs_PADTMP | SVf_READONLY))) |
1646 | sv_magic(ret,(SV*)ReREFCNT_inc(re),'r',0,0); |
1647 | PL_regprecomp = oprecomp; |
1648 | PL_regsize = osize; |
1649 | PL_regnpar = onpar; |
1650 | } |
1651 | DEBUG_r( |
1652 | PerlIO_printf(Perl_debug_log, |
1653 | "Entering embedded `%s%.60s%s%s'\n", |
1654 | PL_colors[0], |
1655 | re->precomp, |
1656 | PL_colors[1], |
1657 | (strlen(re->precomp) > 60 ? "..." : "")) |
1658 | ); |
1659 | state.node = next; |
1660 | state.prev = PL_reg_call_cc; |
1661 | state.cc = PL_regcc; |
1662 | state.re = PL_reg_re; |
1663 | |
1664 | cctmp.cur = 0; |
1665 | cctmp.oldcc = 0; |
1666 | PL_regcc = &cctmp; |
1667 | |
1668 | cp = regcppush(0); /* Save *all* the positions. */ |
1669 | REGCP_SET; |
1670 | cache_re(re); |
1671 | state.ss = PL_savestack_ix; |
1672 | *PL_reglastparen = 0; |
1673 | PL_reg_call_cc = &state; |
1674 | PL_reginput = locinput; |
1675 | if (regmatch(re->program + 1)) { |
1676 | ReREFCNT_dec(re); |
1677 | regcpblow(cp); |
1678 | sayYES; |
1679 | } |
1680 | DEBUG_r( |
1681 | PerlIO_printf(Perl_debug_log, |
1682 | "%*s failed...\n", |
1683 | REPORT_CODE_OFF+PL_regindent*2, "") |
1684 | ); |
1685 | ReREFCNT_dec(re); |
1686 | REGCP_UNWIND; |
1687 | regcppop(); |
1688 | PL_reg_call_cc = state.prev; |
1689 | PL_regcc = state.cc; |
1690 | PL_reg_re = state.re; |
d3790889 |
1691 | cache_re(PL_reg_re); |
0f5d15d6 |
1692 | sayNO; |
1693 | } |
c277df42 |
1694 | sw = SvTRUE(ret); |
0f5d15d6 |
1695 | logical = 0; |
a0ed51b3 |
1696 | } |
1697 | else |
3280af22 |
1698 | sv_setsv(save_scalar(PL_replgv), ret); |
c277df42 |
1699 | break; |
1700 | } |
a0d0e21e |
1701 | case OPEN: |
c277df42 |
1702 | n = ARG(scan); /* which paren pair */ |
3280af22 |
1703 | PL_reg_start_tmp[n] = locinput; |
1704 | if (n > PL_regsize) |
1705 | PL_regsize = n; |
a0d0e21e |
1706 | break; |
1707 | case CLOSE: |
c277df42 |
1708 | n = ARG(scan); /* which paren pair */ |
3280af22 |
1709 | PL_regstartp[n] = PL_reg_start_tmp[n]; |
1710 | PL_regendp[n] = locinput; |
1711 | if (n > *PL_reglastparen) |
1712 | *PL_reglastparen = n; |
a0d0e21e |
1713 | break; |
c277df42 |
1714 | case GROUPP: |
1715 | n = ARG(scan); /* which paren pair */ |
3280af22 |
1716 | sw = (*PL_reglastparen >= n && PL_regendp[n] != NULL); |
c277df42 |
1717 | break; |
1718 | case IFTHEN: |
1719 | if (sw) |
1720 | next = NEXTOPER(NEXTOPER(scan)); |
1721 | else { |
1722 | next = scan + ARG(scan); |
1723 | if (OP(next) == IFTHEN) /* Fake one. */ |
1724 | next = NEXTOPER(NEXTOPER(next)); |
1725 | } |
1726 | break; |
1727 | case LOGICAL: |
0f5d15d6 |
1728 | logical = scan->flags; |
c277df42 |
1729 | break; |
a0d0e21e |
1730 | case CURLYX: { |
1731 | CURCUR cc; |
3280af22 |
1732 | CHECKPOINT cp = PL_savestack_ix; |
c277df42 |
1733 | |
1734 | if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */ |
1735 | next += ARG(next); |
3280af22 |
1736 | cc.oldcc = PL_regcc; |
1737 | PL_regcc = &cc; |
1738 | cc.parenfloor = *PL_reglastparen; |
a0d0e21e |
1739 | cc.cur = -1; |
1740 | cc.min = ARG1(scan); |
1741 | cc.max = ARG2(scan); |
c277df42 |
1742 | cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS; |
a0d0e21e |
1743 | cc.next = next; |
1744 | cc.minmod = minmod; |
1745 | cc.lastloc = 0; |
3280af22 |
1746 | PL_reginput = locinput; |
a0d0e21e |
1747 | n = regmatch(PREVOPER(next)); /* start on the WHILEM */ |
1748 | regcpblow(cp); |
3280af22 |
1749 | PL_regcc = cc.oldcc; |
4633a7c4 |
1750 | saySAME(n); |
a0d0e21e |
1751 | } |
1752 | /* NOT REACHED */ |
1753 | case WHILEM: { |
1754 | /* |
1755 | * This is really hard to understand, because after we match |
1756 | * what we're trying to match, we must make sure the rest of |
1757 | * the RE is going to match for sure, and to do that we have |
1758 | * to go back UP the parse tree by recursing ever deeper. And |
1759 | * if it fails, we have to reset our parent's current state |
1760 | * that we can try again after backing off. |
1761 | */ |
1762 | |
c277df42 |
1763 | CHECKPOINT cp, lastcp; |
3280af22 |
1764 | CURCUR* cc = PL_regcc; |
c277df42 |
1765 | char *lastloc = cc->lastloc; /* Detection of 0-len. */ |
1766 | |
4633a7c4 |
1767 | n = cc->cur + 1; /* how many we know we matched */ |
3280af22 |
1768 | PL_reginput = locinput; |
a0d0e21e |
1769 | |
c277df42 |
1770 | DEBUG_r( |
1771 | PerlIO_printf(Perl_debug_log, |
1772 | "%*s %ld out of %ld..%ld cc=%lx\n", |
3280af22 |
1773 | REPORT_CODE_OFF+PL_regindent*2, "", |
c277df42 |
1774 | (long)n, (long)cc->min, |
1775 | (long)cc->max, (long)cc) |
1776 | ); |
4633a7c4 |
1777 | |
a0d0e21e |
1778 | /* If degenerate scan matches "", assume scan done. */ |
1779 | |
579cf2c3 |
1780 | if (locinput == cc->lastloc && n >= cc->min) { |
3280af22 |
1781 | PL_regcc = cc->oldcc; |
1782 | ln = PL_regcc->cur; |
c277df42 |
1783 | DEBUG_r( |
c3464db5 |
1784 | PerlIO_printf(Perl_debug_log, |
1785 | "%*s empty match detected, try continuation...\n", |
3280af22 |
1786 | REPORT_CODE_OFF+PL_regindent*2, "") |
c277df42 |
1787 | ); |
a0d0e21e |
1788 | if (regmatch(cc->next)) |
4633a7c4 |
1789 | sayYES; |
c277df42 |
1790 | DEBUG_r( |
c3464db5 |
1791 | PerlIO_printf(Perl_debug_log, |
1792 | "%*s failed...\n", |
3280af22 |
1793 | REPORT_CODE_OFF+PL_regindent*2, "") |
c277df42 |
1794 | ); |
3280af22 |
1795 | PL_regcc->cur = ln; |
1796 | PL_regcc = cc; |
4633a7c4 |
1797 | sayNO; |
a0d0e21e |
1798 | } |
1799 | |
1800 | /* First just match a string of min scans. */ |
1801 | |
1802 | if (n < cc->min) { |
1803 | cc->cur = n; |
1804 | cc->lastloc = locinput; |
4633a7c4 |
1805 | if (regmatch(cc->scan)) |
1806 | sayYES; |
1807 | cc->cur = n - 1; |
c277df42 |
1808 | cc->lastloc = lastloc; |
1809 | DEBUG_r( |
c3464db5 |
1810 | PerlIO_printf(Perl_debug_log, |
1811 | "%*s failed...\n", |
3280af22 |
1812 | REPORT_CODE_OFF+PL_regindent*2, "") |
c277df42 |
1813 | ); |
4633a7c4 |
1814 | sayNO; |
a0d0e21e |
1815 | } |
1816 | |
1817 | /* Prefer next over scan for minimal matching. */ |
1818 | |
1819 | if (cc->minmod) { |
3280af22 |
1820 | PL_regcc = cc->oldcc; |
1821 | ln = PL_regcc->cur; |
5f05dabc |
1822 | cp = regcppush(cc->parenfloor); |
c277df42 |
1823 | REGCP_SET; |
5f05dabc |
1824 | if (regmatch(cc->next)) { |
c277df42 |
1825 | regcpblow(cp); |
4633a7c4 |
1826 | sayYES; /* All done. */ |
5f05dabc |
1827 | } |
c277df42 |
1828 | REGCP_UNWIND; |
5f05dabc |
1829 | regcppop(); |
3280af22 |
1830 | PL_regcc->cur = ln; |
1831 | PL_regcc = cc; |
a0d0e21e |
1832 | |
c277df42 |
1833 | if (n >= cc->max) { /* Maximum greed exceeded? */ |
599cee73 |
1834 | if (ckWARN(WARN_UNSAFE) && n >= REG_INFTY |
3280af22 |
1835 | && !(PL_reg_flags & RF_warned)) { |
1836 | PL_reg_flags |= RF_warned; |
599cee73 |
1837 | warner(WARN_UNSAFE, "%s limit (%d) exceeded", |
2f3ca594 |
1838 | "Complex regular subexpression recursion", |
1839 | REG_INFTY - 1); |
c277df42 |
1840 | } |
4633a7c4 |
1841 | sayNO; |
c277df42 |
1842 | } |
a687059c |
1843 | |
c277df42 |
1844 | DEBUG_r( |
c3464db5 |
1845 | PerlIO_printf(Perl_debug_log, |
1846 | "%*s trying longer...\n", |
3280af22 |
1847 | REPORT_CODE_OFF+PL_regindent*2, "") |
c277df42 |
1848 | ); |
a0d0e21e |
1849 | /* Try scanning more and see if it helps. */ |
3280af22 |
1850 | PL_reginput = locinput; |
a0d0e21e |
1851 | cc->cur = n; |
1852 | cc->lastloc = locinput; |
5f05dabc |
1853 | cp = regcppush(cc->parenfloor); |
c277df42 |
1854 | REGCP_SET; |
5f05dabc |
1855 | if (regmatch(cc->scan)) { |
c277df42 |
1856 | regcpblow(cp); |
4633a7c4 |
1857 | sayYES; |
5f05dabc |
1858 | } |
c277df42 |
1859 | DEBUG_r( |
c3464db5 |
1860 | PerlIO_printf(Perl_debug_log, |
1861 | "%*s failed...\n", |
3280af22 |
1862 | REPORT_CODE_OFF+PL_regindent*2, "") |
c277df42 |
1863 | ); |
1864 | REGCP_UNWIND; |
5f05dabc |
1865 | regcppop(); |
4633a7c4 |
1866 | cc->cur = n - 1; |
c277df42 |
1867 | cc->lastloc = lastloc; |
4633a7c4 |
1868 | sayNO; |
a0d0e21e |
1869 | } |
1870 | |
1871 | /* Prefer scan over next for maximal matching. */ |
1872 | |
1873 | if (n < cc->max) { /* More greed allowed? */ |
5f05dabc |
1874 | cp = regcppush(cc->parenfloor); |
a0d0e21e |
1875 | cc->cur = n; |
1876 | cc->lastloc = locinput; |
c277df42 |
1877 | REGCP_SET; |
5f05dabc |
1878 | if (regmatch(cc->scan)) { |
c277df42 |
1879 | regcpblow(cp); |
4633a7c4 |
1880 | sayYES; |
5f05dabc |
1881 | } |
c277df42 |
1882 | REGCP_UNWIND; |
a0d0e21e |
1883 | regcppop(); /* Restore some previous $<digit>s? */ |
3280af22 |
1884 | PL_reginput = locinput; |
c277df42 |
1885 | DEBUG_r( |
c3464db5 |
1886 | PerlIO_printf(Perl_debug_log, |
1887 | "%*s failed, try continuation...\n", |
3280af22 |
1888 | REPORT_CODE_OFF+PL_regindent*2, "") |
c277df42 |
1889 | ); |
1890 | } |
599cee73 |
1891 | if (ckWARN(WARN_UNSAFE) && n >= REG_INFTY |
1892 | && !(PL_reg_flags & RF_warned)) { |
3280af22 |
1893 | PL_reg_flags |= RF_warned; |
599cee73 |
1894 | warner(WARN_UNSAFE, "%s limit (%d) exceeded", |
cb5d145d |
1895 | "Complex regular subexpression recursion", |
1896 | REG_INFTY - 1); |
a0d0e21e |
1897 | } |
1898 | |
1899 | /* Failed deeper matches of scan, so see if this one works. */ |
3280af22 |
1900 | PL_regcc = cc->oldcc; |
1901 | ln = PL_regcc->cur; |
a0d0e21e |
1902 | if (regmatch(cc->next)) |
4633a7c4 |
1903 | sayYES; |
c277df42 |
1904 | DEBUG_r( |
c3464db5 |
1905 | PerlIO_printf(Perl_debug_log, "%*s failed...\n", |
3280af22 |
1906 | REPORT_CODE_OFF+PL_regindent*2, "") |
c277df42 |
1907 | ); |
3280af22 |
1908 | PL_regcc->cur = ln; |
1909 | PL_regcc = cc; |
4633a7c4 |
1910 | cc->cur = n - 1; |
c277df42 |
1911 | cc->lastloc = lastloc; |
4633a7c4 |
1912 | sayNO; |
a0d0e21e |
1913 | } |
1914 | /* NOT REACHED */ |
c277df42 |
1915 | case BRANCHJ: |
1916 | next = scan + ARG(scan); |
1917 | if (next == scan) |
1918 | next = NULL; |
1919 | inner = NEXTOPER(NEXTOPER(scan)); |
1920 | goto do_branch; |
1921 | case BRANCH: |
1922 | inner = NEXTOPER(scan); |
1923 | do_branch: |
1924 | { |
1925 | CHECKPOINT lastcp; |
1926 | c1 = OP(scan); |
1927 | if (OP(next) != c1) /* No choice. */ |
1928 | next = inner; /* Avoid recursion. */ |
a0d0e21e |
1929 | else { |
3280af22 |
1930 | int lastparen = *PL_reglastparen; |
c277df42 |
1931 | |
1932 | REGCP_SET; |
a0d0e21e |
1933 | do { |
3280af22 |
1934 | PL_reginput = locinput; |
c277df42 |
1935 | if (regmatch(inner)) |
4633a7c4 |
1936 | sayYES; |
c277df42 |
1937 | REGCP_UNWIND; |
3280af22 |
1938 | for (n = *PL_reglastparen; n > lastparen; n--) |
1939 | PL_regendp[n] = 0; |
1940 | *PL_reglastparen = n; |
c277df42 |
1941 | scan = next; |
a0d0e21e |
1942 | /*SUPPRESS 560*/ |
c277df42 |
1943 | if (n = (c1 == BRANCH ? NEXT_OFF(next) : ARG(next))) |
1944 | next += n; |
a0d0e21e |
1945 | else |
c277df42 |
1946 | next = NULL; |
c277df42 |
1947 | inner = NEXTOPER(scan); |
1948 | if (c1 == BRANCHJ) { |
1949 | inner = NEXTOPER(inner); |
1950 | } |
1951 | } while (scan != NULL && OP(scan) == c1); |
4633a7c4 |
1952 | sayNO; |
a0d0e21e |
1953 | /* NOTREACHED */ |
a687059c |
1954 | } |
a0d0e21e |
1955 | } |
1956 | break; |
1957 | case MINMOD: |
1958 | minmod = 1; |
1959 | break; |
c277df42 |
1960 | case CURLYM: |
1961 | { |
00db4c45 |
1962 | I32 l = 0; |
c277df42 |
1963 | CHECKPOINT lastcp; |
1964 | |
1965 | /* We suppose that the next guy does not need |
1966 | backtracking: in particular, it is of constant length, |
1967 | and has no parenths to influence future backrefs. */ |
1968 | ln = ARG1(scan); /* min to match */ |
1969 | n = ARG2(scan); /* max to match */ |
c277df42 |
1970 | paren = scan->flags; |
1971 | if (paren) { |
3280af22 |
1972 | if (paren > PL_regsize) |
1973 | PL_regsize = paren; |
1974 | if (paren > *PL_reglastparen) |
1975 | *PL_reglastparen = paren; |
c277df42 |
1976 | } |
dc45a647 |
1977 | scan = NEXTOPER(scan) + NODE_STEP_REGNODE; |
c277df42 |
1978 | if (paren) |
1979 | scan += NEXT_OFF(scan); /* Skip former OPEN. */ |
3280af22 |
1980 | PL_reginput = locinput; |
c277df42 |
1981 | if (minmod) { |
1982 | minmod = 0; |
1983 | if (ln && regrepeat_hard(scan, ln, &l) < ln) |
1984 | sayNO; |
5f4b28b2 |
1985 | if (ln && l == 0 && n >= ln |
c277df42 |
1986 | /* In fact, this is tricky. If paren, then the |
1987 | fact that we did/didnot match may influence |
1988 | future execution. */ |
1989 | && !(paren && ln == 0)) |
1990 | ln = n; |
3280af22 |
1991 | locinput = PL_reginput; |
22c35a8c |
1992 | if (PL_regkind[(U8)OP(next)] == EXACT) { |
c277df42 |
1993 | c1 = UCHARAT(OPERAND(next) + 1); |
1994 | if (OP(next) == EXACTF) |
22c35a8c |
1995 | c2 = PL_fold[c1]; |
c277df42 |
1996 | else if (OP(next) == EXACTFL) |
22c35a8c |
1997 | c2 = PL_fold_locale[c1]; |
c277df42 |
1998 | else |
1999 | c2 = c1; |
a0ed51b3 |
2000 | } |
2001 | else |
c277df42 |
2002 | c1 = c2 = -1000; |
2003 | REGCP_SET; |
5f4b28b2 |
2004 | /* This may be improved if l == 0. */ |
c277df42 |
2005 | while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */ |
2006 | /* If it could work, try it. */ |
2007 | if (c1 == -1000 || |
3280af22 |
2008 | UCHARAT(PL_reginput) == c1 || |
2009 | UCHARAT(PL_reginput) == c2) |
c277df42 |
2010 | { |
2011 | if (paren) { |
2012 | if (n) { |
dfe13c55 |
2013 | PL_regstartp[paren] = HOPc(PL_reginput, -l); |
3280af22 |
2014 | PL_regendp[paren] = PL_reginput; |
a0ed51b3 |
2015 | } |
2016 | else |
3280af22 |
2017 | PL_regendp[paren] = NULL; |
c277df42 |
2018 | } |
2019 | if (regmatch(next)) |
2020 | sayYES; |
2021 | REGCP_UNWIND; |
2022 | } |
2023 | /* Couldn't or didn't -- move forward. */ |
3280af22 |
2024 | PL_reginput = locinput; |
c277df42 |
2025 | if (regrepeat_hard(scan, 1, &l)) { |
2026 | ln++; |
3280af22 |
2027 | locinput = PL_reginput; |
c277df42 |
2028 | } |
2029 | else |
2030 | sayNO; |
2031 | } |
a0ed51b3 |
2032 | } |
2033 | else { |
c277df42 |
2034 | n = regrepeat_hard(scan, n, &l); |
2035 | if (n != 0 && l == 0 |
2036 | /* In fact, this is tricky. If paren, then the |
2037 | fact that we did/didnot match may influence |
2038 | future execution. */ |
2039 | && !(paren && ln == 0)) |
2040 | ln = n; |
3280af22 |
2041 | locinput = PL_reginput; |
c277df42 |
2042 | DEBUG_r( |
5c0ca799 |
2043 | PerlIO_printf(Perl_debug_log, |
2044 | "%*s matched %ld times, len=%ld...\n", |
3280af22 |
2045 | REPORT_CODE_OFF+PL_regindent*2, "", n, l) |
c277df42 |
2046 | ); |
2047 | if (n >= ln) { |
22c35a8c |
2048 | if (PL_regkind[(U8)OP(next)] == EXACT) { |
c277df42 |
2049 | c1 = UCHARAT(OPERAND(next) + 1); |
2050 | if (OP(next) == EXACTF) |
22c35a8c |
2051 | c2 = PL_fold[c1]; |
c277df42 |
2052 | else if (OP(next) == EXACTFL) |
22c35a8c |
2053 | c2 = PL_fold_locale[c1]; |
c277df42 |
2054 | else |
2055 | c2 = c1; |
a0ed51b3 |
2056 | } |
2057 | else |
c277df42 |
2058 | c1 = c2 = -1000; |
2059 | } |
2060 | REGCP_SET; |
2061 | while (n >= ln) { |
2062 | /* If it could work, try it. */ |
2063 | if (c1 == -1000 || |
3280af22 |
2064 | UCHARAT(PL_reginput) == c1 || |
2065 | UCHARAT(PL_reginput) == c2) |
a0ed51b3 |
2066 | { |
2067 | DEBUG_r( |
c3464db5 |
2068 | PerlIO_printf(Perl_debug_log, |
2069 | "%*s trying tail with n=%ld...\n", |
3280af22 |
2070 | REPORT_CODE_OFF+PL_regindent*2, "", n) |
a0ed51b3 |
2071 | ); |
2072 | if (paren) { |
2073 | if (n) { |
dfe13c55 |
2074 | PL_regstartp[paren] = HOPc(PL_reginput, -l); |
a0ed51b3 |
2075 | PL_regendp[paren] = PL_reginput; |
c277df42 |
2076 | } |
a0ed51b3 |
2077 | else |
2078 | PL_regendp[paren] = NULL; |
c277df42 |
2079 | } |
a0ed51b3 |
2080 | if (regmatch(next)) |
2081 | sayYES; |
2082 | REGCP_UNWIND; |
2083 | } |
c277df42 |
2084 | /* Couldn't or didn't -- back up. */ |
2085 | n--; |
dfe13c55 |
2086 | locinput = HOPc(locinput, -l); |
3280af22 |
2087 | PL_reginput = locinput; |
c277df42 |
2088 | } |
2089 | } |
2090 | sayNO; |
2091 | break; |
2092 | } |
2093 | case CURLYN: |
2094 | paren = scan->flags; /* Which paren to set */ |
3280af22 |
2095 | if (paren > PL_regsize) |
2096 | PL_regsize = paren; |
2097 | if (paren > *PL_reglastparen) |
2098 | *PL_reglastparen = paren; |
c277df42 |
2099 | ln = ARG1(scan); /* min to match */ |
2100 | n = ARG2(scan); /* max to match */ |
dc45a647 |
2101 | scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE); |
c277df42 |
2102 | goto repeat; |
a0d0e21e |
2103 | case CURLY: |
c277df42 |
2104 | paren = 0; |
a0d0e21e |
2105 | ln = ARG1(scan); /* min to match */ |
2106 | n = ARG2(scan); /* max to match */ |
dc45a647 |
2107 | scan = NEXTOPER(scan) + NODE_STEP_REGNODE; |
a0d0e21e |
2108 | goto repeat; |
2109 | case STAR: |
2110 | ln = 0; |
c277df42 |
2111 | n = REG_INFTY; |
a0d0e21e |
2112 | scan = NEXTOPER(scan); |
c277df42 |
2113 | paren = 0; |
a0d0e21e |
2114 | goto repeat; |
2115 | case PLUS: |
c277df42 |
2116 | ln = 1; |
2117 | n = REG_INFTY; |
2118 | scan = NEXTOPER(scan); |
2119 | paren = 0; |
2120 | repeat: |
a0d0e21e |
2121 | /* |
2122 | * Lookahead to avoid useless match attempts |
2123 | * when we know what character comes next. |
2124 | */ |
22c35a8c |
2125 | if (PL_regkind[(U8)OP(next)] == EXACT) { |
bbce6d69 |
2126 | c1 = UCHARAT(OPERAND(next) + 1); |
2127 | if (OP(next) == EXACTF) |
22c35a8c |
2128 | c2 = PL_fold[c1]; |
bbce6d69 |
2129 | else if (OP(next) == EXACTFL) |
22c35a8c |
2130 | c2 = PL_fold_locale[c1]; |
bbce6d69 |
2131 | else |
2132 | c2 = c1; |
2133 | } |
a0d0e21e |
2134 | else |
bbce6d69 |
2135 | c1 = c2 = -1000; |
3280af22 |
2136 | PL_reginput = locinput; |
a0d0e21e |
2137 | if (minmod) { |
c277df42 |
2138 | CHECKPOINT lastcp; |
a0d0e21e |
2139 | minmod = 0; |
2140 | if (ln && regrepeat(scan, ln) < ln) |
4633a7c4 |
2141 | sayNO; |
a0ed51b3 |
2142 | locinput = PL_reginput; |
c277df42 |
2143 | REGCP_SET; |
2144 | while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */ |
a0d0e21e |
2145 | /* If it could work, try it. */ |
bbce6d69 |
2146 | if (c1 == -1000 || |
3280af22 |
2147 | UCHARAT(PL_reginput) == c1 || |
2148 | UCHARAT(PL_reginput) == c2) |
bbce6d69 |
2149 | { |
c277df42 |
2150 | if (paren) { |
2151 | if (n) { |
dfe13c55 |
2152 | PL_regstartp[paren] = HOPc(PL_reginput, -1); |
3280af22 |
2153 | PL_regendp[paren] = PL_reginput; |
a0ed51b3 |
2154 | } |
2155 | else |
3280af22 |
2156 | PL_regendp[paren] = NULL; |
c277df42 |
2157 | } |
a0d0e21e |
2158 | if (regmatch(next)) |
4633a7c4 |
2159 | sayYES; |
c277df42 |
2160 | REGCP_UNWIND; |
bbce6d69 |
2161 | } |
c277df42 |
2162 | /* Couldn't or didn't -- move forward. */ |
a0ed51b3 |
2163 | PL_reginput = locinput; |
a0d0e21e |
2164 | if (regrepeat(scan, 1)) { |
2165 | ln++; |
a0ed51b3 |
2166 | locinput = PL_reginput; |
2167 | } |
2168 | else |
4633a7c4 |
2169 | sayNO; |
a0d0e21e |
2170 | } |
2171 | } |
2172 | else { |
c277df42 |
2173 | CHECKPOINT lastcp; |
a0d0e21e |
2174 | n = regrepeat(scan, n); |
a0ed51b3 |
2175 | locinput = PL_reginput; |
22c35a8c |
2176 | if (ln < n && PL_regkind[(U8)OP(next)] == EOL && |
3280af22 |
2177 | (!PL_multiline || OP(next) == SEOL)) |
a0d0e21e |
2178 | ln = n; /* why back off? */ |
c277df42 |
2179 | REGCP_SET; |
2180 | if (paren) { |
2181 | while (n >= ln) { |
2182 | /* If it could work, try it. */ |
2183 | if (c1 == -1000 || |
3280af22 |
2184 | UCHARAT(PL_reginput) == c1 || |
2185 | UCHARAT(PL_reginput) == c2) |
c277df42 |
2186 | { |
2187 | if (paren && n) { |
2188 | if (n) { |
dfe13c55 |
2189 | PL_regstartp[paren] = HOPc(PL_reginput, -1); |
3280af22 |
2190 | PL_regendp[paren] = PL_reginput; |
a0ed51b3 |
2191 | } |
2192 | else |
3280af22 |
2193 | PL_regendp[paren] = NULL; |
c277df42 |
2194 | } |
2195 | if (regmatch(next)) |
2196 | sayYES; |
2197 | REGCP_UNWIND; |
2198 | } |
2199 | /* Couldn't or didn't -- back up. */ |
2200 | n--; |
dfe13c55 |
2201 | PL_reginput = locinput = HOPc(locinput, -1); |
c277df42 |
2202 | } |
a0ed51b3 |
2203 | } |
2204 | else { |
c277df42 |
2205 | while (n >= ln) { |
2206 | /* If it could work, try it. */ |
2207 | if (c1 == -1000 || |
3280af22 |
2208 | UCHARAT(PL_reginput) == c1 || |
2209 | UCHARAT(PL_reginput) == c2) |
c277df42 |
2210 | { |
2211 | if (regmatch(next)) |
2212 | sayYES; |
2213 | REGCP_UNWIND; |
2214 | } |
2215 | /* Couldn't or didn't -- back up. */ |
2216 | n--; |
dfe13c55 |
2217 | PL_reginput = locinput = HOPc(locinput, -1); |
bbce6d69 |
2218 | } |
a0d0e21e |
2219 | } |
2220 | } |
4633a7c4 |
2221 | sayNO; |
c277df42 |
2222 | break; |
a0d0e21e |
2223 | case END: |
0f5d15d6 |
2224 | if (PL_reg_call_cc) { |
2225 | re_cc_state *cur_call_cc = PL_reg_call_cc; |
2226 | CURCUR *cctmp = PL_regcc; |
2227 | regexp *re = PL_reg_re; |
2228 | CHECKPOINT cp, lastcp; |
2229 | |
2230 | cp = regcppush(0); /* Save *all* the positions. */ |
2231 | REGCP_SET; |
2232 | regcp_set_to(PL_reg_call_cc->ss); /* Restore parens of |
2233 | the caller. */ |
2234 | PL_reginput = locinput; /* Make position available to |
2235 | the callcc. */ |
2236 | cache_re(PL_reg_call_cc->re); |
2237 | PL_regcc = PL_reg_call_cc->cc; |
2238 | PL_reg_call_cc = PL_reg_call_cc->prev; |
2239 | if (regmatch(cur_call_cc->node)) { |
2240 | PL_reg_call_cc = cur_call_cc; |
2241 | regcpblow(cp); |
2242 | sayYES; |
2243 | } |
2244 | REGCP_UNWIND; |
2245 | regcppop(); |
2246 | PL_reg_call_cc = cur_call_cc; |
2247 | PL_regcc = cctmp; |
2248 | PL_reg_re = re; |
2249 | cache_re(re); |
2250 | |
2251 | DEBUG_r( |
2252 | PerlIO_printf(Perl_debug_log, |
2253 | "%*s continuation failed...\n", |
2254 | REPORT_CODE_OFF+PL_regindent*2, "") |
2255 | ); |
2256 | sayNO; |
2257 | } |
3280af22 |
2258 | if (locinput < PL_regtill) |
7e5428c5 |
2259 | sayNO; /* Cannot match: too short. */ |
2260 | /* Fall through */ |
2261 | case SUCCEED: |
3280af22 |
2262 | PL_reginput = locinput; /* put where regtry can find it */ |
4633a7c4 |
2263 | sayYES; /* Success! */ |
c277df42 |
2264 | case SUSPEND: |
2265 | n = 1; |
9fe1d20c |
2266 | PL_reginput = locinput; |
c277df42 |
2267 | goto do_ifmatch; |
a0d0e21e |
2268 | case UNLESSM: |
c277df42 |
2269 | n = 0; |
a0ed51b3 |
2270 | if (scan->flags) { |
dfe13c55 |
2271 | s = HOPMAYBEc(locinput, -scan->flags); |
a0ed51b3 |
2272 | if (!s) |
2273 | goto say_yes; |
2274 | PL_reginput = s; |
2275 | } |
2276 | else |
2277 | PL_reginput = locinput; |
c277df42 |
2278 | goto do_ifmatch; |
2279 | case IFMATCH: |
2280 | n = 1; |
a0ed51b3 |
2281 | if (scan->flags) { |
dfe13c55 |
2282 | s = HOPMAYBEc(locinput, -scan->flags); |
642653c7 |
2283 | if (!s || s < PL_bostr) |
a0ed51b3 |
2284 | goto say_no; |
2285 | PL_reginput = s; |
2286 | } |
2287 | else |
2288 | PL_reginput = locinput; |
2289 | |
c277df42 |
2290 | do_ifmatch: |
c277df42 |
2291 | inner = NEXTOPER(NEXTOPER(scan)); |
2292 | if (regmatch(inner) != n) { |
2293 | say_no: |
2294 | if (logical) { |
2295 | logical = 0; |
2296 | sw = 0; |
2297 | goto do_longjump; |
a0ed51b3 |
2298 | } |
2299 | else |
c277df42 |
2300 | sayNO; |
2301 | } |
2302 | say_yes: |
2303 | if (logical) { |
2304 | logical = 0; |
2305 | sw = 1; |
2306 | } |
fe44a5e8 |
2307 | if (OP(scan) == SUSPEND) { |
3280af22 |
2308 | locinput = PL_reginput; |
565764a8 |
2309 | nextchr = UCHARAT(locinput); |
fe44a5e8 |
2310 | } |
c277df42 |
2311 | /* FALL THROUGH. */ |
2312 | case LONGJMP: |
2313 | do_longjump: |
2314 | next = scan + ARG(scan); |
2315 | if (next == scan) |
2316 | next = NULL; |
a0d0e21e |
2317 | break; |
2318 | default: |
c030ccd9 |
2319 | PerlIO_printf(PerlIO_stderr(), "%lx %d\n", |
c277df42 |
2320 | (unsigned long)scan, OP(scan)); |
a0d0e21e |
2321 | FAIL("regexp memory corruption"); |
a687059c |
2322 | } |
a0d0e21e |
2323 | scan = next; |
2324 | } |
a687059c |
2325 | |
a0d0e21e |
2326 | /* |
2327 | * We get here only if there's trouble -- normally "case END" is |
2328 | * the terminating point. |
2329 | */ |
2330 | FAIL("corrupted regexp pointers"); |
2331 | /*NOTREACHED*/ |
4633a7c4 |
2332 | sayNO; |
2333 | |
2334 | yes: |
2335 | #ifdef DEBUGGING |
3280af22 |
2336 | PL_regindent--; |
4633a7c4 |
2337 | #endif |
2338 | return 1; |
2339 | |
2340 | no: |
2341 | #ifdef DEBUGGING |
3280af22 |
2342 | PL_regindent--; |
4633a7c4 |
2343 | #endif |
a0d0e21e |
2344 | return 0; |
a687059c |
2345 | } |
2346 | |
2347 | /* |
2348 | - regrepeat - repeatedly match something simple, report how many |
2349 | */ |
2350 | /* |
2351 | * [This routine now assumes that it will only match on things of length 1. |
2352 | * That was true before, but now we assume scan - reginput is the count, |
a0ed51b3 |
2353 | * rather than incrementing count on every character. [Er, except utf8.]] |
a687059c |
2354 | */ |
76e3520e |
2355 | STATIC I32 |
c277df42 |
2356 | regrepeat(regnode *p, I32 max) |
a687059c |
2357 | { |
5c0ca799 |
2358 | dTHR; |
a0d0e21e |
2359 | register char *scan; |
2360 | register char *opnd; |
2361 | register I32 c; |
3280af22 |
2362 | register char *loceol = PL_regeol; |
a0ed51b3 |
2363 | register I32 hardcount = 0; |
a0d0e21e |
2364 | |
3280af22 |
2365 | scan = PL_reginput; |
c277df42 |
2366 | if (max != REG_INFTY && max < loceol - scan) |
a0d0e21e |
2367 | loceol = scan + max; |
161b471a |
2368 | opnd = (char *) OPERAND(p); |
a0d0e21e |
2369 | switch (OP(p)) { |
22c35a8c |
2370 | case REG_ANY: |
a0d0e21e |
2371 | while (scan < loceol && *scan != '\n') |
2372 | scan++; |
2373 | break; |
2374 | case SANY: |
2375 | scan = loceol; |
2376 | break; |
a0ed51b3 |
2377 | case ANYUTF8: |
2378 | loceol = PL_regeol; |
2379 | while (scan < loceol && *scan != '\n') { |
2380 | scan += UTF8SKIP(scan); |
2381 | hardcount++; |
2382 | } |
2383 | break; |
2384 | case SANYUTF8: |
2385 | loceol = PL_regeol; |
2386 | while (scan < loceol) { |
2387 | scan += UTF8SKIP(scan); |
2388 | hardcount++; |
2389 | } |
2390 | break; |
bbce6d69 |
2391 | case EXACT: /* length of string is 1 */ |
2392 | c = UCHARAT(++opnd); |
2393 | while (scan < loceol && UCHARAT(scan) == c) |
2394 | scan++; |
2395 | break; |
2396 | case EXACTF: /* length of string is 1 */ |
2397 | c = UCHARAT(++opnd); |
2398 | while (scan < loceol && |
22c35a8c |
2399 | (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold[c])) |
bbce6d69 |
2400 | scan++; |
2401 | break; |
2402 | case EXACTFL: /* length of string is 1 */ |
3280af22 |
2403 | PL_reg_flags |= RF_tainted; |
bbce6d69 |
2404 | c = UCHARAT(++opnd); |
2405 | while (scan < loceol && |
22c35a8c |
2406 | (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold_locale[c])) |
a0d0e21e |
2407 | scan++; |
2408 | break; |
a0ed51b3 |
2409 | case ANYOFUTF8: |
2410 | loceol = PL_regeol; |
2411 | while (scan < loceol && REGINCLASSUTF8(p, (U8*)scan)) { |
2412 | scan += UTF8SKIP(scan); |
2413 | hardcount++; |
2414 | } |
2415 | break; |
a0d0e21e |
2416 | case ANYOF: |
ae5c130c |
2417 | while (scan < loceol && REGINCLASS(opnd, *scan)) |
a0d0e21e |
2418 | scan++; |
a0d0e21e |
2419 | break; |
2420 | case ALNUM: |
2421 | while (scan < loceol && isALNUM(*scan)) |
2422 | scan++; |
2423 | break; |
a0ed51b3 |
2424 | case ALNUMUTF8: |
2425 | loceol = PL_regeol; |
dfe13c55 |
2426 | while (scan < loceol && swash_fetch(PL_utf8_alnum, (U8*)scan)) { |
a0ed51b3 |
2427 | scan += UTF8SKIP(scan); |
2428 | hardcount++; |
2429 | } |
2430 | break; |
bbce6d69 |
2431 | case ALNUML: |
3280af22 |
2432 | PL_reg_flags |= RF_tainted; |
bbce6d69 |
2433 | while (scan < loceol && isALNUM_LC(*scan)) |
2434 | scan++; |
2435 | break; |
a0ed51b3 |
2436 | case ALNUMLUTF8: |
2437 | PL_reg_flags |= RF_tainted; |
2438 | loceol = PL_regeol; |
dfe13c55 |
2439 | while (scan < loceol && isALNUM_LC_utf8((U8*)scan)) { |
a0ed51b3 |
2440 | scan += UTF8SKIP(scan); |
2441 | hardcount++; |
2442 | } |
2443 | break; |
2444 | break; |
a0d0e21e |
2445 | case NALNUM: |
2446 | while (scan < loceol && !isALNUM(*scan)) |
2447 | scan++; |
2448 | break; |
a0ed51b3 |
2449 | case NALNUMUTF8: |
2450 | loceol = PL_regeol; |
dfe13c55 |
2451 | while (scan < loceol && !swash_fetch(PL_utf8_alnum, (U8*)scan)) { |
a0ed51b3 |
2452 | scan += UTF8SKIP(scan); |
2453 | hardcount++; |
2454 | } |
2455 | break; |
bbce6d69 |
2456 | case NALNUML: |
3280af22 |
2457 | PL_reg_flags |= RF_tainted; |
bbce6d69 |
2458 | while (scan < loceol && !isALNUM_LC(*scan)) |
2459 | scan++; |
2460 | break; |
a0ed51b3 |
2461 | case NALNUMLUTF8: |
2462 | PL_reg_flags |= RF_tainted; |
2463 | loceol = PL_regeol; |
dfe13c55 |
2464 | while (scan < loceol && !isALNUM_LC_utf8((U8*)scan)) { |
a0ed51b3 |
2465 | scan += UTF8SKIP(scan); |
2466 | hardcount++; |
2467 | } |
2468 | break; |
a0d0e21e |
2469 | case SPACE: |
2470 | while (scan < loceol && isSPACE(*scan)) |
2471 | scan++; |
2472 | break; |
a0ed51b3 |
2473 | case SPACEUTF8: |
2474 | loceol = PL_regeol; |
dfe13c55 |
2475 | while (scan < loceol && (*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) { |
a0ed51b3 |
2476 | scan += UTF8SKIP(scan); |
2477 | hardcount++; |
2478 | } |
2479 | break; |
bbce6d69 |
2480 | case SPACEL: |
3280af22 |
2481 | PL_reg_flags |= RF_tainted; |
bbce6d69 |
2482 | while (scan < loceol && isSPACE_LC(*scan)) |
2483 | scan++; |
2484 | break; |
a0ed51b3 |
2485 | case SPACELUTF8: |
2486 | PL_reg_flags |= RF_tainted; |
2487 | loceol = PL_regeol; |
dfe13c55 |
2488 | while (scan < loceol && (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) { |
a0ed51b3 |
2489 | scan += UTF8SKIP(scan); |
2490 | hardcount++; |
2491 | } |
2492 | break; |
a0d0e21e |
2493 | case NSPACE: |
2494 | while (scan < loceol && !isSPACE(*scan)) |
2495 | scan++; |
2496 | break; |
a0ed51b3 |
2497 | case NSPACEUTF8: |
2498 | loceol = PL_regeol; |
dfe13c55 |
2499 | while (scan < loceol && !(*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) { |
a0ed51b3 |
2500 | scan += UTF8SKIP(scan); |
2501 | hardcount++; |
2502 | } |
2503 | break; |
bbce6d69 |
2504 | case NSPACEL: |
3280af22 |
2505 | PL_reg_flags |= RF_tainted; |
bbce6d69 |
2506 | while (scan < loceol && !isSPACE_LC(*scan)) |
2507 | scan++; |
2508 | break; |
a0ed51b3 |
2509 | case NSPACELUTF8: |
2510 | PL_reg_flags |= RF_tainted; |
2511 | loceol = PL_regeol; |
dfe13c55 |
2512 | while (scan < loceol && !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) { |
a0ed51b3 |
2513 | scan += UTF8SKIP(scan); |
2514 | hardcount++; |
2515 | } |
2516 | break; |
a0d0e21e |
2517 | case DIGIT: |
2518 | while (scan < loceol && isDIGIT(*scan)) |
2519 | scan++; |
2520 | break; |
a0ed51b3 |
2521 | case DIGITUTF8: |
2522 | loceol = PL_regeol; |
dfe13c55 |
2523 | while (scan < loceol && swash_fetch(PL_utf8_digit,(U8*)scan)) { |
a0ed51b3 |
2524 | scan += UTF8SKIP(scan); |
2525 | hardcount++; |
2526 | } |
2527 | break; |
2528 | break; |
a0d0e21e |
2529 | case NDIGIT: |
2530 | while (scan < loceol && !isDIGIT(*scan)) |
2531 | scan++; |
2532 | break; |
a0ed51b3 |
2533 | case NDIGITUTF8: |
2534 | loceol = PL_regeol; |
dfe13c55 |
2535 | while (scan < loceol && !swash_fetch(PL_utf8_digit,(U8*)scan)) { |
a0ed51b3 |
2536 | scan += UTF8SKIP(scan); |
2537 | hardcount++; |
2538 | } |
2539 | break; |
a0d0e21e |
2540 | default: /* Called on something of 0 width. */ |
2541 | break; /* So match right here or not at all. */ |
2542 | } |
a687059c |
2543 | |
a0ed51b3 |
2544 | if (hardcount) |
2545 | c = hardcount; |
2546 | else |
2547 | c = scan - PL_reginput; |
3280af22 |
2548 | PL_reginput = scan; |
a687059c |
2549 | |
c277df42 |
2550 | DEBUG_r( |
2551 | { |
2552 | SV *prop = sv_newmortal(); |
2553 | |
2554 | regprop(prop, p); |
2555 | PerlIO_printf(Perl_debug_log, |
2556 | "%*s %s can match %ld times out of %ld...\n", |
2557 | REPORT_CODE_OFF+1, "", SvPVX(prop),c,max); |
2558 | }); |
2559 | |
a0d0e21e |
2560 | return(c); |
a687059c |
2561 | } |
2562 | |
2563 | /* |
c277df42 |
2564 | - regrepeat_hard - repeatedly match something, report total lenth and length |
2565 | * |
2566 | * The repeater is supposed to have constant length. |
2567 | */ |
2568 | |
76e3520e |
2569 | STATIC I32 |
c277df42 |
2570 | regrepeat_hard(regnode *p, I32 max, I32 *lp) |
2571 | { |
5c0ca799 |
2572 | dTHR; |
c277df42 |
2573 | register char *scan; |
2574 | register char *start; |
3280af22 |
2575 | register char *loceol = PL_regeol; |
a0ed51b3 |
2576 | I32 l = 0; |
708e3b05 |
2577 | I32 count = 0, res = 1; |
a0ed51b3 |
2578 | |
2579 | if (!max) |
2580 | return 0; |
c277df42 |
2581 | |
3280af22 |
2582 | start = PL_reginput; |
a0ed51b3 |
2583 | if (UTF) { |
708e3b05 |
2584 | while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) { |
a0ed51b3 |
2585 | if (!count++) { |
2586 | l = 0; |
2587 | while (start < PL_reginput) { |
2588 | l++; |
2589 | start += UTF8SKIP(start); |
2590 | } |
2591 | *lp = l; |
2592 | if (l == 0) |
2593 | return max; |
2594 | } |
2595 | if (count == max) |
2596 | return count; |
2597 | } |
2598 | } |
2599 | else { |
708e3b05 |
2600 | while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) { |
a0ed51b3 |
2601 | if (!count++) { |
2602 | *lp = l = PL_reginput - start; |
2603 | if (max != REG_INFTY && l*max < loceol - scan) |
2604 | loceol = scan + l*max; |
2605 | if (l == 0) |
2606 | return max; |
c277df42 |
2607 | } |
2608 | } |
2609 | } |
708e3b05 |
2610 | if (!res) |
3280af22 |
2611 | PL_reginput = scan; |
c277df42 |
2612 | |
a0ed51b3 |
2613 | return count; |
c277df42 |
2614 | } |
2615 | |
2616 | /* |
bbce6d69 |
2617 | - regclass - determine if a character falls into a character class |
2618 | */ |
2619 | |
76e3520e |
2620 | STATIC bool |
8ac85365 |
2621 | reginclass(register char *p, register I32 c) |
bbce6d69 |
2622 | { |
5c0ca799 |
2623 | dTHR; |
bbce6d69 |
2624 | char flags = *p; |
2625 | bool match = FALSE; |
2626 | |
2627 | c &= 0xFF; |
ae5c130c |
2628 | if (ANYOF_TEST(p, c)) |
bbce6d69 |
2629 | match = TRUE; |
2630 | else if (flags & ANYOF_FOLD) { |
2631 | I32 cf; |
2632 | if (flags & ANYOF_LOCALE) { |
3280af22 |
2633 | PL_reg_flags |= RF_tainted; |
22c35a8c |
2634 | cf = PL_fold_locale[c]; |
bbce6d69 |
2635 | } |
2636 | else |
22c35a8c |
2637 | cf = PL_fold[c]; |
ae5c130c |
2638 | if (ANYOF_TEST(p, cf)) |
bbce6d69 |
2639 | match = TRUE; |
2640 | } |
2641 | |
2642 | if (!match && (flags & ANYOF_ISA)) { |
3280af22 |
2643 | PL_reg_flags |= RF_tainted; |
bbce6d69 |
2644 | |
2645 | if (((flags & ANYOF_ALNUML) && isALNUM_LC(c)) || |
2646 | ((flags & ANYOF_NALNUML) && !isALNUM_LC(c)) || |
2647 | ((flags & ANYOF_SPACEL) && isSPACE_LC(c)) || |
2648 | ((flags & ANYOF_NSPACEL) && !isSPACE_LC(c))) |
2649 | { |
2650 | match = TRUE; |
2651 | } |
2652 | } |
2653 | |
ae5c130c |
2654 | return (flags & ANYOF_INVERT) ? !match : match; |
bbce6d69 |
2655 | } |
2656 | |
a0ed51b3 |
2657 | STATIC bool |
2658 | reginclassutf8(regnode *f, U8 *p) |
c485e607 |
2659 | { |
2660 | dTHR; |
a0ed51b3 |
2661 | char flags = ARG1(f); |
2662 | bool match = FALSE; |
2663 | SV *sv = (SV*)PL_regdata->data[ARG2(f)]; |
2664 | |
2665 | if (swash_fetch(sv, p)) |
2666 | match = TRUE; |
2667 | else if (flags & ANYOF_FOLD) { |
2668 | I32 cf; |
dfe13c55 |
2669 | U8 tmpbuf[10]; |
a0ed51b3 |
2670 | if (flags & ANYOF_LOCALE) { |
2671 | PL_reg_flags |= RF_tainted; |
2672 | uv_to_utf8(tmpbuf, toLOWER_LC_utf8(p)); |
2673 | } |
2674 | else |
2675 | uv_to_utf8(tmpbuf, toLOWER_utf8(p)); |
2676 | if (swash_fetch(sv, tmpbuf)) |
2677 | match = TRUE; |
2678 | } |
2679 | |
2680 | if (!match && (flags & ANYOF_ISA)) { |
2681 | PL_reg_flags |= RF_tainted; |
2682 | |
2683 | if (((flags & ANYOF_ALNUML) && isALNUM_LC_utf8(p)) || |
2684 | ((flags & ANYOF_NALNUML) && !isALNUM_LC_utf8(p)) || |
2685 | ((flags & ANYOF_SPACEL) && isSPACE_LC_utf8(p)) || |
2686 | ((flags & ANYOF_NSPACEL) && !isSPACE_LC_utf8(p))) |
2687 | { |
2688 | match = TRUE; |
2689 | } |
2690 | } |
2691 | |
2692 | return (flags & ANYOF_INVERT) ? !match : match; |
2693 | } |
161b471a |
2694 | |
dfe13c55 |
2695 | STATIC U8 * |
2696 | reghop(U8 *s, I32 off) |
c485e607 |
2697 | { |
2698 | dTHR; |
a0ed51b3 |
2699 | if (off >= 0) { |
2700 | while (off-- && s < (U8*)PL_regeol) |
2701 | s += UTF8SKIP(s); |
2702 | } |
2703 | else { |
2704 | while (off++) { |
2705 | if (s > (U8*)PL_bostr) { |
2706 | s--; |
2707 | if (*s & 0x80) { |
2708 | while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80) |
2709 | s--; |
2710 | } /* XXX could check well-formedness here */ |
2711 | } |
2712 | } |
2713 | } |
2714 | return s; |
2715 | } |
161b471a |
2716 | |
dfe13c55 |
2717 | STATIC U8 * |
2718 | reghopmaybe(U8* s, I32 off) |
a0ed51b3 |
2719 | { |
c485e607 |
2720 | dTHR; |
a0ed51b3 |
2721 | if (off >= 0) { |
2722 | while (off-- && s < (U8*)PL_regeol) |
2723 | s += UTF8SKIP(s); |
2724 | if (off >= 0) |
2725 | return 0; |
2726 | } |
2727 | else { |
2728 | while (off++) { |
2729 | if (s > (U8*)PL_bostr) { |
2730 | s--; |
2731 | if (*s & 0x80) { |
2732 | while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80) |
2733 | s--; |
2734 | } /* XXX could check well-formedness here */ |
2735 | } |
2736 | else |
2737 | break; |
2738 | } |
2739 | if (off <= 0) |
2740 | return 0; |
2741 | } |
2742 | return s; |
2743 | } |