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