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