This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Tweak tests to notice $dont_use_nlink
[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 ****
a0d0e21e 45 **** Copyright (c) 1991-1994, 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
58#ifndef STATIC
59#define STATIC static
60#endif
61
62#ifdef DEBUGGING
a0d0e21e
LW
63static I32 regnarrate = 0;
64static char* regprogram = 0;
a687059c
LW
65#endif
66
a0d0e21e
LW
67/* Current curly descriptor */
68typedef struct curcur CURCUR;
69struct curcur {
70 int parenfloor; /* how far back to strip paren data */
71 int cur; /* how many instances of scan we've matched */
72 int min; /* the minimal number of scans to match */
73 int max; /* the maximal number of scans to match */
74 int minmod; /* whether to work our way up or down */
75 char * scan; /* the thing to match */
76 char * next; /* what has to match after it */
77 char * lastloc; /* where we started matching this scan */
78 CURCUR * oldcc; /* current curly before we started this one */
79};
80
81static CURCUR* regcc;
82
83typedef I32 CHECKPOINT;
84
55497cff 85static CHECKPOINT regcppush _((I32 parenfloor));
86static char * regcppop _((void));
a0d0e21e 87
55497cff 88static CHECKPOINT
a0d0e21e
LW
89regcppush(parenfloor)
90I32 parenfloor;
91{
92 int retval = savestack_ix;
93 int i = (regsize - parenfloor) * 3;
94 int p;
95
96 SSCHECK(i + 5);
97 for (p = regsize; p > parenfloor; p--) {
98 SSPUSHPTR(regendp[p]);
99 SSPUSHPTR(regstartp[p]);
100 SSPUSHINT(p);
101 }
102 SSPUSHINT(regsize);
103 SSPUSHINT(*reglastparen);
104 SSPUSHPTR(reginput);
105 SSPUSHINT(i + 3);
106 SSPUSHINT(SAVEt_REGCONTEXT);
107 return retval;
108}
109
55497cff 110static char *
a0d0e21e
LW
111regcppop()
112{
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;
122 for (i -= 3; i > 0; i -= 3) {
123 paren = (U32)SSPOPINT;
124 regstartp[paren] = (char *) SSPOPPTR;
125 tmps = (char*)SSPOPPTR;
126 if (paren <= *reglastparen)
127 regendp[paren] = tmps;
128 }
129 for (paren = *reglastparen + 1; paren <= regnpar; paren++) {
130 if (paren > regsize)
131 regstartp[paren] = Nullch;
132 regendp[paren] = Nullch;
133 }
134 return input;
135}
136
137#define regcpblow(cp) leave_scope(cp)
138
a687059c 139/*
e50aee73 140 * pregexec and friends
a687059c
LW
141 */
142
143/*
a687059c
LW
144 * Forwards.
145 */
a0d0e21e
LW
146
147static I32 regmatch _((char *prog));
148static I32 regrepeat _((char *p, I32 max));
149static I32 regtry _((regexp *prog, char *startpos));
bbce6d69 150static bool reginclass _((char *p, I32 c));
151
152static bool regtainted; /* tainted information used? */
a687059c
LW
153
154/*
e50aee73 155 - pregexec - match a regexp against a string
a687059c 156 */
79072805 157I32
e50aee73 158pregexec(prog, stringarg, strend, strbeg, minend, screamer, safebase)
a687059c
LW
159register regexp *prog;
160char *stringarg;
161register char *strend; /* pointer to null at end of string */
162char *strbeg; /* real beginning of string */
79072805
LW
163I32 minend; /* end of match must be at least minend after stringarg */
164SV *screamer;
165I32 safebase; /* no need to remember string in subbase */
a687059c 166{
a0d0e21e 167 register char *s;
a0d0e21e
LW
168 register char *c;
169 register char *startpos = stringarg;
170 register I32 tmp;
171 I32 minlen = 0; /* must match at least this many chars */
172 I32 dontbother = 0; /* how many characters not to try at end */
173 CURCUR cc;
a687059c 174
a0d0e21e 175 cc.cur = 0;
4633a7c4 176 cc.oldcc = 0;
a0d0e21e
LW
177 regcc = &cc;
178
179#ifdef DEBUGGING
180 regnarrate = debug & 512;
181 regprogram = prog->program;
182#endif
183
184 /* Be paranoid... */
185 if (prog == NULL || startpos == NULL) {
186 croak("NULL regexp parameter");
187 return 0;
188 }
189
190 if (startpos == strbeg) /* is ^ valid at stringarg? */
191 regprev = '\n';
192 else {
193 regprev = stringarg[-1];
194 if (!multiline && regprev == '\n')
195 regprev = '\0'; /* force ^ to NOT match */
196 }
bbce6d69 197
a0d0e21e 198 regprecomp = prog->precomp;
a0d0e21e
LW
199 /* Check validity of program. */
200 if (UCHARAT(prog->program) != MAGIC) {
201 FAIL("corrupted regexp program");
202 }
203
bbce6d69 204 regnpar = prog->nparens;
205 regtainted = FALSE;
a0d0e21e
LW
206
207 /* If there is a "must appear" string, look for it. */
208 s = startpos;
209 if (prog->regmust != Nullsv &&
774d564b 210 !(prog->reganch & ROPT_ANCH_GPOS) &&
211 (!(prog->reganch & ROPT_ANCH_BOL)
a0d0e21e
LW
212 || (multiline && prog->regback >= 0)) )
213 {
214 if (stringarg == strbeg && screamer) {
215 if (screamfirst[BmRARE(prog->regmust)] >= 0)
216 s = screaminstr(screamer,prog->regmust);
217 else
218 s = Nullch;
0a12ae7d 219 }
a0d0e21e
LW
220 else
221 s = fbm_instr((unsigned char*)s, (unsigned char*)strend,
222 prog->regmust);
223 if (!s) {
224 ++BmUSEFUL(prog->regmust); /* hooray */
225 goto phooey; /* not present */
a687059c 226 }
a0d0e21e
LW
227 else if (prog->regback >= 0) {
228 s -= prog->regback;
229 if (s < startpos)
230 s = startpos;
231 minlen = prog->regback + SvCUR(prog->regmust);
a687059c 232 }
a0d0e21e
LW
233 else if (!prog->naughty && --BmUSEFUL(prog->regmust) < 0) { /* boo */
234 SvREFCNT_dec(prog->regmust);
235 prog->regmust = Nullsv; /* disable regmust */
236 s = startpos;
237 }
238 else {
239 s = startpos;
240 minlen = SvCUR(prog->regmust);
a687059c 241 }
a0d0e21e 242 }
a687059c 243
a0d0e21e
LW
244 /* Mark beginning of line for ^ . */
245 regbol = startpos;
a687059c 246
a0d0e21e
LW
247 /* Mark end of line for $ (and such) */
248 regeol = strend;
a687059c 249
a0d0e21e
LW
250 /* see how far we have to get to not match where we matched before */
251 regtill = startpos+minend;
a687059c 252
a0d0e21e 253 /* Simplest case: anchored match need be tried only once. */
774d564b 254 /* [unless only anchor is BOL and multiline is set] */
a0d0e21e
LW
255 if (prog->reganch & ROPT_ANCH) {
256 if (regtry(prog, startpos))
257 goto got_it;
774d564b 258 else if (!(prog->reganch & ROPT_ANCH_GPOS) &&
259 (multiline || (prog->reganch & ROPT_IMPLICIT)))
260 {
a0d0e21e
LW
261 if (minlen)
262 dontbother = minlen - 1;
263 strend -= dontbother;
264 /* for multiline we only have to try after newlines */
265 if (s > startpos)
266 s--;
267 while (s < strend) {
268 if (*s++ == '\n') {
269 if (s < strend && regtry(prog, s))
270 goto got_it;
271 }
35c8bce7 272 }
35c8bce7 273 }
a0d0e21e
LW
274 goto phooey;
275 }
35c8bce7 276
a0d0e21e
LW
277 /* Messy cases: unanchored match. */
278 if (prog->regstart) {
279 if (prog->reganch & ROPT_SKIP) { /* we have /x+whatever/ */
280 /* it must be a one character string */
bbce6d69 281 char ch = SvPVX(prog->regstart)[0];
a0d0e21e 282 while (s < strend) {
bbce6d69 283 if (*s == ch) {
a0d0e21e 284 if (regtry(prog, s))
a687059c 285 goto got_it;
a0d0e21e 286 s++;
bbce6d69 287 while (s < strend && *s == ch)
a0d0e21e 288 s++;
a687059c 289 }
a0d0e21e
LW
290 s++;
291 }
a687059c 292 }
47109bfb 293 else if (SvTYPE(prog->regstart) == SVt_PVBM) {
a0d0e21e
LW
294 /* We know what string it must start with. */
295 while ((s = fbm_instr((unsigned char*)s,
296 (unsigned char*)strend, prog->regstart)) != NULL)
297 {
298 if (regtry(prog, s))
299 goto got_it;
300 s++;
301 }
302 }
47109bfb 303 else { /* Optimized fbm_instr: */
a0d0e21e
LW
304 c = SvPVX(prog->regstart);
305 while ((s = ninstr(s, strend, c, c + SvCUR(prog->regstart))) != NULL)
306 {
307 if (regtry(prog, s))
308 goto got_it;
309 s++;
310 }
311 }
312 goto phooey;
313 }
314 /*SUPPRESS 560*/
315 if (c = prog->regstclass) {
316 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
a687059c 317
a0d0e21e
LW
318 if (minlen)
319 dontbother = minlen - 1;
320 strend -= dontbother; /* don't bother with what can't match */
321 tmp = 1;
322 /* We know what class it must start with. */
323 switch (OP(c)) {
324 case ANYOF:
325 c = OPERAND(c);
326 while (s < strend) {
bbce6d69 327 if (reginclass(c, *s)) {
a0d0e21e
LW
328 if (tmp && regtry(prog, s))
329 goto got_it;
330 else
331 tmp = doevery;
a687059c 332 }
a0d0e21e
LW
333 else
334 tmp = 1;
335 s++;
336 }
337 break;
bbce6d69 338 case BOUNDL:
339 regtainted = TRUE;
340 /* FALL THROUGH */
a0d0e21e
LW
341 case BOUND:
342 if (minlen)
343 dontbother++,strend--;
bbce6d69 344 tmp = (s != startpos) ? UCHARAT(s - 1) : regprev;
95bac841 345 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
a0d0e21e 346 while (s < strend) {
95bac841 347 if (tmp == !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
a0d0e21e
LW
348 tmp = !tmp;
349 if (regtry(prog, s))
350 goto got_it;
a687059c 351 }
a0d0e21e
LW
352 s++;
353 }
354 if ((minlen || tmp) && regtry(prog,s))
355 goto got_it;
356 break;
bbce6d69 357 case NBOUNDL:
358 regtainted = TRUE;
359 /* FALL THROUGH */
a0d0e21e
LW
360 case NBOUND:
361 if (minlen)
362 dontbother++,strend--;
bbce6d69 363 tmp = (s != startpos) ? UCHARAT(s - 1) : regprev;
95bac841 364 tmp = ((OP(c) == NBOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
a0d0e21e 365 while (s < strend) {
95bac841 366 if (tmp == !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
a0d0e21e
LW
367 tmp = !tmp;
368 else if (regtry(prog, s))
369 goto got_it;
370 s++;
371 }
372 if ((minlen || !tmp) && regtry(prog,s))
373 goto got_it;
374 break;
375 case ALNUM:
376 while (s < strend) {
bbce6d69 377 if (isALNUM(*s)) {
378 if (tmp && regtry(prog, s))
379 goto got_it;
380 else
381 tmp = doevery;
382 }
383 else
384 tmp = 1;
385 s++;
386 }
387 break;
388 case ALNUML:
389 regtainted = TRUE;
390 while (s < strend) {
391 if (isALNUM_LC(*s)) {
a0d0e21e
LW
392 if (tmp && regtry(prog, s))
393 goto got_it;
a687059c 394 else
a0d0e21e
LW
395 tmp = doevery;
396 }
397 else
398 tmp = 1;
399 s++;
400 }
401 break;
402 case NALNUM:
403 while (s < strend) {
bbce6d69 404 if (!isALNUM(*s)) {
405 if (tmp && regtry(prog, s))
406 goto got_it;
407 else
408 tmp = doevery;
409 }
410 else
411 tmp = 1;
412 s++;
413 }
414 break;
415 case NALNUML:
416 regtainted = TRUE;
417 while (s < strend) {
418 if (!isALNUM_LC(*s)) {
a0d0e21e
LW
419 if (tmp && regtry(prog, s))
420 goto got_it;
a687059c 421 else
a0d0e21e 422 tmp = doevery;
a687059c 423 }
a0d0e21e
LW
424 else
425 tmp = 1;
426 s++;
427 }
428 break;
429 case SPACE:
430 while (s < strend) {
431 if (isSPACE(*s)) {
432 if (tmp && regtry(prog, s))
433 goto got_it;
434 else
435 tmp = doevery;
2304df62 436 }
a0d0e21e
LW
437 else
438 tmp = 1;
439 s++;
440 }
441 break;
bbce6d69 442 case SPACEL:
443 regtainted = TRUE;
444 while (s < strend) {
445 if (isSPACE_LC(*s)) {
446 if (tmp && regtry(prog, s))
447 goto got_it;
448 else
449 tmp = doevery;
450 }
451 else
452 tmp = 1;
453 s++;
454 }
455 break;
a0d0e21e
LW
456 case NSPACE:
457 while (s < strend) {
458 if (!isSPACE(*s)) {
459 if (tmp && regtry(prog, s))
460 goto got_it;
461 else
462 tmp = doevery;
a687059c 463 }
a0d0e21e
LW
464 else
465 tmp = 1;
466 s++;
467 }
468 break;
bbce6d69 469 case NSPACEL:
470 regtainted = TRUE;
471 while (s < strend) {
472 if (!isSPACE_LC(*s)) {
473 if (tmp && regtry(prog, s))
474 goto got_it;
475 else
476 tmp = doevery;
477 }
478 else
479 tmp = 1;
480 s++;
481 }
482 break;
a0d0e21e
LW
483 case DIGIT:
484 while (s < strend) {
485 if (isDIGIT(*s)) {
486 if (tmp && regtry(prog, s))
487 goto got_it;
488 else
489 tmp = doevery;
2b69d0c2 490 }
a0d0e21e
LW
491 else
492 tmp = 1;
493 s++;
494 }
495 break;
496 case NDIGIT:
497 while (s < strend) {
498 if (!isDIGIT(*s)) {
499 if (tmp && regtry(prog, s))
500 goto got_it;
501 else
502 tmp = doevery;
a687059c 503 }
a0d0e21e
LW
504 else
505 tmp = 1;
506 s++;
507 }
508 break;
a687059c 509 }
a0d0e21e
LW
510 }
511 else {
512 if (minlen)
513 dontbother = minlen - 1;
514 strend -= dontbother;
515 /* We don't know much -- general case. */
516 do {
517 if (regtry(prog, s))
518 goto got_it;
519 } while (s++ < strend);
520 }
521
522 /* Failure. */
523 goto phooey;
a687059c 524
a0d0e21e 525got_it:
420218e7 526 strend += dontbother; /* uncheat */
a0d0e21e
LW
527 prog->subbeg = strbeg;
528 prog->subend = strend;
bbce6d69 529 prog->exec_tainted = regtainted;
5f05dabc 530
531 /* make sure $`, $&, $', and $digit will work later */
532 if (!safebase && (strbeg != prog->subbase)) {
bbce6d69 533 I32 i = strend - startpos + (stringarg - strbeg);
5f05dabc 534 s = savepvn(strbeg, i);
535 Safefree(prog->subbase);
536 prog->subbase = s;
537 prog->subbeg = prog->subbase;
538 prog->subend = prog->subbase + i;
539 s = prog->subbase + (stringarg - strbeg);
a0d0e21e
LW
540 for (i = 0; i <= prog->nparens; i++) {
541 if (prog->endp[i]) {
542 prog->startp[i] = s + (prog->startp[i] - startpos);
543 prog->endp[i] = s + (prog->endp[i] - startpos);
544 }
545 }
a0d0e21e
LW
546 }
547 return 1;
548
549phooey:
a0d0e21e 550 return 0;
a687059c
LW
551}
552
553/*
554 - regtry - try match at specific point
555 */
79072805 556static I32 /* 0 failure, 1 success */
a0d0e21e 557regtry(prog, startpos)
a687059c 558regexp *prog;
a0d0e21e 559char *startpos;
a687059c 560{
a0d0e21e
LW
561 register I32 i;
562 register char **sp;
563 register char **ep;
564
565 reginput = startpos;
566 regstartp = prog->startp;
567 regendp = prog->endp;
568 reglastparen = &prog->lastparen;
569 prog->lastparen = 0;
570 regsize = 0;
571
572 sp = prog->startp;
573 ep = prog->endp;
574 if (prog->nparens) {
575 for (i = prog->nparens; i >= 0; i--) {
576 *sp++ = NULL;
577 *ep++ = NULL;
a687059c 578 }
a0d0e21e
LW
579 }
580 if (regmatch(prog->program + 1) && reginput >= regtill) {
581 prog->startp[0] = startpos;
582 prog->endp[0] = reginput;
583 return 1;
584 }
585 else
586 return 0;
a687059c
LW
587}
588
589/*
590 - regmatch - main matching routine
591 *
592 * Conceptually the strategy is simple: check to see whether the current
593 * node matches, call self recursively to see whether the rest matches,
594 * and then act accordingly. In practice we make some effort to avoid
595 * recursion, in particular by going through "ordinary" nodes (that don't
596 * need to know whether the rest of the match failed) by a loop instead of
597 * by recursion.
598 */
599/* [lwall] I've hoisted the register declarations to the outer block in order to
600 * maybe save a little bit of pushing and popping on the stack. It also takes
601 * advantage of machines that use a register save mask on subroutine entry.
602 */
79072805 603static I32 /* 0 failure, 1 success */
a687059c
LW
604regmatch(prog)
605char *prog;
606{
a0d0e21e
LW
607 register char *scan; /* Current node. */
608 char *next; /* Next node. */
609 register I32 nextchar;
610 register I32 n; /* no or next */
611 register I32 ln; /* len or last */
612 register char *s; /* operand or save */
613 register char *locinput = reginput;
bbce6d69 614 register I32 c1, c2; /* case fold search */
a0d0e21e 615 int minmod = 0;
4633a7c4
LW
616#ifdef DEBUGGING
617 static int regindent = 0;
618 regindent++;
619#endif
a0d0e21e 620
bbce6d69 621 nextchar = UCHARAT(locinput);
a0d0e21e
LW
622 scan = prog;
623 while (scan != NULL) {
a687059c 624#ifdef DEBUGGING
4633a7c4
LW
625#define sayYES goto yes
626#define sayNO goto no
627#define saySAME(x) if (x) goto yes; else goto no
628 if (regnarrate) {
760ac839 629 PerlIO_printf(Perl_debug_log, "%*s%2d%-8.8s\t<%.10s>\n", regindent*2, "",
a0d0e21e 630 scan - regprogram, regprop(scan), locinput);
4633a7c4
LW
631 }
632#else
633#define sayYES return 1
634#define sayNO return 0
635#define saySAME(x) return x
a687059c
LW
636#endif
637
638#ifdef REGALIGN
a0d0e21e
LW
639 next = scan + NEXT(scan);
640 if (next == scan)
641 next = NULL;
a687059c 642#else
a0d0e21e 643 next = regnext(scan);
a687059c
LW
644#endif
645
a0d0e21e
LW
646 switch (OP(scan)) {
647 case BOL:
648 if (locinput == regbol
649 ? regprev == '\n'
650 : ((nextchar || locinput < regeol) && locinput[-1] == '\n') )
651 {
652 /* regtill = regbol; */
653 break;
654 }
4633a7c4 655 sayNO;
a0d0e21e
LW
656 case MBOL:
657 if (locinput == regbol
658 ? regprev == '\n'
659 : ((nextchar || locinput < regeol) && locinput[-1] == '\n') )
660 {
661 break;
662 }
4633a7c4 663 sayNO;
a0d0e21e
LW
664 case SBOL:
665 if (locinput == regbol && regprev == '\n')
666 break;
4633a7c4 667 sayNO;
774d564b 668 case GPOS:
a0d0e21e
LW
669 if (locinput == regbol)
670 break;
4633a7c4 671 sayNO;
a0d0e21e
LW
672 case EOL:
673 if (multiline)
674 goto meol;
675 else
676 goto seol;
677 case MEOL:
678 meol:
679 if ((nextchar || locinput < regeol) && nextchar != '\n')
4633a7c4 680 sayNO;
a0d0e21e
LW
681 break;
682 case SEOL:
683 seol:
684 if ((nextchar || locinput < regeol) && nextchar != '\n')
4633a7c4 685 sayNO;
a0d0e21e 686 if (regeol - locinput > 1)
4633a7c4 687 sayNO;
a0d0e21e
LW
688 break;
689 case SANY:
690 if (!nextchar && locinput >= regeol)
4633a7c4 691 sayNO;
bbce6d69 692 nextchar = UCHARAT(++locinput);
a0d0e21e
LW
693 break;
694 case ANY:
695 if (!nextchar && locinput >= regeol || nextchar == '\n')
4633a7c4 696 sayNO;
bbce6d69 697 nextchar = UCHARAT(++locinput);
a0d0e21e 698 break;
bbce6d69 699 case EXACT:
a0d0e21e
LW
700 s = OPERAND(scan);
701 ln = *s++;
702 /* Inline the first character, for speed. */
95bac841 703 if (UCHARAT(s) != nextchar)
4633a7c4 704 sayNO;
a0d0e21e 705 if (regeol - locinput < ln)
4633a7c4 706 sayNO;
36477c24 707 if (ln > 1 && memNE(s, locinput, ln))
4633a7c4 708 sayNO;
a0d0e21e 709 locinput += ln;
bbce6d69 710 nextchar = UCHARAT(locinput);
711 break;
712 case EXACTFL:
713 regtainted = TRUE;
714 /* FALL THROUGH */
715 case EXACTF:
716 s = OPERAND(scan);
717 ln = *s++;
718 /* Inline the first character, for speed. */
719 if (UCHARAT(s) != nextchar &&
720 UCHARAT(s) != ((OP(scan) == EXACTF)
721 ? fold : fold_locale)[nextchar])
722 sayNO;
723 if (regeol - locinput < ln)
724 sayNO;
5f05dabc 725 if (ln > 1 && (OP(scan) == EXACTF
726 ? ibcmp(s, locinput, ln)
727 : ibcmp_locale(s, locinput, ln)))
bbce6d69 728 sayNO;
729 locinput += ln;
730 nextchar = UCHARAT(locinput);
a0d0e21e
LW
731 break;
732 case ANYOF:
733 s = OPERAND(scan);
734 if (nextchar < 0)
735 nextchar = UCHARAT(locinput);
bbce6d69 736 if (!reginclass(s, nextchar))
4633a7c4 737 sayNO;
a0d0e21e 738 if (!nextchar && locinput >= regeol)
4633a7c4 739 sayNO;
bbce6d69 740 nextchar = UCHARAT(++locinput);
a0d0e21e 741 break;
bbce6d69 742 case ALNUML:
743 regtainted = TRUE;
744 /* FALL THROUGH */
a0d0e21e
LW
745 case ALNUM:
746 if (!nextchar)
4633a7c4 747 sayNO;
bbce6d69 748 if (!(OP(scan) == ALNUM
749 ? isALNUM(nextchar) : isALNUM_LC(nextchar)))
4633a7c4 750 sayNO;
bbce6d69 751 nextchar = UCHARAT(++locinput);
a0d0e21e 752 break;
bbce6d69 753 case NALNUML:
754 regtainted = TRUE;
755 /* FALL THROUGH */
a0d0e21e
LW
756 case NALNUM:
757 if (!nextchar && locinput >= regeol)
4633a7c4 758 sayNO;
bbce6d69 759 if (OP(scan) == NALNUM
760 ? isALNUM(nextchar) : isALNUM_LC(nextchar))
4633a7c4 761 sayNO;
bbce6d69 762 nextchar = UCHARAT(++locinput);
a0d0e21e 763 break;
bbce6d69 764 case BOUNDL:
765 case NBOUNDL:
766 regtainted = TRUE;
767 /* FALL THROUGH */
a0d0e21e 768 case BOUND:
bbce6d69 769 case NBOUND:
770 /* was last char in word? */
771 ln = (locinput != regbol) ? UCHARAT(locinput - 1) : regprev;
772 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
773 ln = isALNUM(ln);
774 n = isALNUM(nextchar);
775 }
776 else {
777 ln = isALNUM_LC(ln);
778 n = isALNUM_LC(nextchar);
779 }
95bac841 780 if (((!ln) == (!n)) == (OP(scan) == BOUND || OP(scan) == BOUNDL))
4633a7c4 781 sayNO;
a0d0e21e 782 break;
bbce6d69 783 case SPACEL:
784 regtainted = TRUE;
785 /* FALL THROUGH */
a0d0e21e
LW
786 case SPACE:
787 if (!nextchar && locinput >= regeol)
4633a7c4 788 sayNO;
bbce6d69 789 if (!(OP(scan) == SPACE
790 ? isSPACE(nextchar) : isSPACE_LC(nextchar)))
4633a7c4 791 sayNO;
bbce6d69 792 nextchar = UCHARAT(++locinput);
a0d0e21e 793 break;
bbce6d69 794 case NSPACEL:
795 regtainted = TRUE;
796 /* FALL THROUGH */
a0d0e21e
LW
797 case NSPACE:
798 if (!nextchar)
4633a7c4 799 sayNO;
bbce6d69 800 if (OP(scan) == SPACE
801 ? isSPACE(nextchar) : isSPACE_LC(nextchar))
4633a7c4 802 sayNO;
bbce6d69 803 nextchar = UCHARAT(++locinput);
a0d0e21e
LW
804 break;
805 case DIGIT:
806 if (!isDIGIT(nextchar))
4633a7c4 807 sayNO;
bbce6d69 808 nextchar = UCHARAT(++locinput);
a0d0e21e
LW
809 break;
810 case NDIGIT:
811 if (!nextchar && locinput >= regeol)
4633a7c4 812 sayNO;
a0d0e21e 813 if (isDIGIT(nextchar))
4633a7c4 814 sayNO;
bbce6d69 815 nextchar = UCHARAT(++locinput);
a0d0e21e
LW
816 break;
817 case REF:
818 n = ARG1(scan); /* which paren pair */
819 s = regstartp[n];
820 if (!s)
4633a7c4 821 sayNO;
a0d0e21e 822 if (!regendp[n])
4633a7c4 823 sayNO;
a0d0e21e
LW
824 if (s == regendp[n])
825 break;
826 /* Inline the first character, for speed. */
95bac841 827 if (UCHARAT(s) != nextchar)
4633a7c4 828 sayNO;
a0d0e21e
LW
829 ln = regendp[n] - s;
830 if (locinput + ln > regeol)
4633a7c4 831 sayNO;
36477c24 832 if (ln > 1 && memNE(s, locinput, ln))
4633a7c4 833 sayNO;
a0d0e21e 834 locinput += ln;
bbce6d69 835 nextchar = UCHARAT(locinput);
a0d0e21e
LW
836 break;
837
838 case NOTHING:
839 break;
840 case BACK:
841 break;
842 case OPEN:
843 n = ARG1(scan); /* which paren pair */
844 regstartp[n] = locinput;
845 if (n > regsize)
846 regsize = n;
847 break;
848 case CLOSE:
849 n = ARG1(scan); /* which paren pair */
850 regendp[n] = locinput;
851 if (n > *reglastparen)
852 *reglastparen = n;
853 break;
854 case CURLYX: {
855 CURCUR cc;
856 CHECKPOINT cp = savestack_ix;
857 cc.oldcc = regcc;
858 regcc = &cc;
859 cc.parenfloor = *reglastparen;
860 cc.cur = -1;
861 cc.min = ARG1(scan);
862 cc.max = ARG2(scan);
863 cc.scan = NEXTOPER(scan) + 4;
864 cc.next = next;
865 cc.minmod = minmod;
866 cc.lastloc = 0;
867 reginput = locinput;
868 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
869 regcpblow(cp);
870 regcc = cc.oldcc;
4633a7c4 871 saySAME(n);
a0d0e21e
LW
872 }
873 /* NOT REACHED */
874 case WHILEM: {
875 /*
876 * This is really hard to understand, because after we match
877 * what we're trying to match, we must make sure the rest of
878 * the RE is going to match for sure, and to do that we have
879 * to go back UP the parse tree by recursing ever deeper. And
880 * if it fails, we have to reset our parent's current state
881 * that we can try again after backing off.
882 */
883
5f05dabc 884 CHECKPOINT cp;
a0d0e21e 885 CURCUR* cc = regcc;
4633a7c4 886 n = cc->cur + 1; /* how many we know we matched */
a0d0e21e
LW
887 reginput = locinput;
888
4633a7c4
LW
889#ifdef DEBUGGING
890 if (regnarrate)
760ac839 891 PerlIO_printf(Perl_debug_log, "%*s %d %lx\n", regindent*2, "",
4633a7c4
LW
892 n, (long)cc);
893#endif
894
a0d0e21e
LW
895 /* If degenerate scan matches "", assume scan done. */
896
579cf2c3 897 if (locinput == cc->lastloc && n >= cc->min) {
a0d0e21e
LW
898 regcc = cc->oldcc;
899 ln = regcc->cur;
900 if (regmatch(cc->next))
4633a7c4 901 sayYES;
a0d0e21e
LW
902 regcc->cur = ln;
903 regcc = cc;
4633a7c4 904 sayNO;
a0d0e21e
LW
905 }
906
907 /* First just match a string of min scans. */
908
909 if (n < cc->min) {
910 cc->cur = n;
911 cc->lastloc = locinput;
4633a7c4
LW
912 if (regmatch(cc->scan))
913 sayYES;
914 cc->cur = n - 1;
915 sayNO;
a0d0e21e
LW
916 }
917
918 /* Prefer next over scan for minimal matching. */
919
920 if (cc->minmod) {
921 regcc = cc->oldcc;
922 ln = regcc->cur;
5f05dabc 923 cp = regcppush(cc->parenfloor);
924 if (regmatch(cc->next)) {
925 regcpblow(cp);
4633a7c4 926 sayYES; /* All done. */
5f05dabc 927 }
928 regcppop();
a0d0e21e
LW
929 regcc->cur = ln;
930 regcc = cc;
931
932 if (n >= cc->max) /* Maximum greed exceeded? */
4633a7c4 933 sayNO;
a687059c 934
a0d0e21e
LW
935 /* Try scanning more and see if it helps. */
936 reginput = locinput;
937 cc->cur = n;
938 cc->lastloc = locinput;
5f05dabc 939 cp = regcppush(cc->parenfloor);
940 if (regmatch(cc->scan)) {
941 regcpblow(cp);
4633a7c4 942 sayYES;
5f05dabc 943 }
944 regcppop();
4633a7c4
LW
945 cc->cur = n - 1;
946 sayNO;
a0d0e21e
LW
947 }
948
949 /* Prefer scan over next for maximal matching. */
950
951 if (n < cc->max) { /* More greed allowed? */
5f05dabc 952 cp = regcppush(cc->parenfloor);
a0d0e21e
LW
953 cc->cur = n;
954 cc->lastloc = locinput;
5f05dabc 955 if (regmatch(cc->scan)) {
956 regcpblow(cp);
4633a7c4 957 sayYES;
5f05dabc 958 }
a0d0e21e
LW
959 regcppop(); /* Restore some previous $<digit>s? */
960 reginput = locinput;
961 }
962
963 /* Failed deeper matches of scan, so see if this one works. */
964 regcc = cc->oldcc;
965 ln = regcc->cur;
966 if (regmatch(cc->next))
4633a7c4 967 sayYES;
a0d0e21e
LW
968 regcc->cur = ln;
969 regcc = cc;
4633a7c4
LW
970 cc->cur = n - 1;
971 sayNO;
a0d0e21e
LW
972 }
973 /* NOT REACHED */
974 case BRANCH: {
975 if (OP(next) != BRANCH) /* No choice. */
976 next = NEXTOPER(scan);/* Avoid recursion. */
977 else {
748a9306 978 int lastparen = *reglastparen;
a0d0e21e
LW
979 do {
980 reginput = locinput;
981 if (regmatch(NEXTOPER(scan)))
4633a7c4 982 sayYES;
748a9306
LW
983 for (n = *reglastparen; n > lastparen; n--)
984 regendp[n] = 0;
985 *reglastparen = n;
986
a687059c 987#ifdef REGALIGN
a0d0e21e
LW
988 /*SUPPRESS 560*/
989 if (n = NEXT(scan))
990 scan += n;
991 else
992 scan = NULL;
a687059c 993#else
a0d0e21e 994 scan = regnext(scan);
79072805 995#endif
a0d0e21e 996 } while (scan != NULL && OP(scan) == BRANCH);
4633a7c4 997 sayNO;
a0d0e21e 998 /* NOTREACHED */
a687059c 999 }
a0d0e21e
LW
1000 }
1001 break;
1002 case MINMOD:
1003 minmod = 1;
1004 break;
1005 case CURLY:
1006 ln = ARG1(scan); /* min to match */
1007 n = ARG2(scan); /* max to match */
1008 scan = NEXTOPER(scan) + 4;
1009 goto repeat;
1010 case STAR:
1011 ln = 0;
1012 n = 32767;
1013 scan = NEXTOPER(scan);
1014 goto repeat;
1015 case PLUS:
1016 /*
1017 * Lookahead to avoid useless match attempts
1018 * when we know what character comes next.
1019 */
1020 ln = 1;
1021 n = 32767;
1022 scan = NEXTOPER(scan);
1023 repeat:
bbce6d69 1024 if (regkind[(U8)OP(next)] == EXACT) {
1025 c1 = UCHARAT(OPERAND(next) + 1);
1026 if (OP(next) == EXACTF)
1027 c2 = fold[c1];
1028 else if (OP(next) == EXACTFL)
1029 c2 = fold_locale[c1];
1030 else
1031 c2 = c1;
1032 }
a0d0e21e 1033 else
bbce6d69 1034 c1 = c2 = -1000;
a0d0e21e
LW
1035 reginput = locinput;
1036 if (minmod) {
1037 minmod = 0;
1038 if (ln && regrepeat(scan, ln) < ln)
4633a7c4 1039 sayNO;
8e07c86e 1040 while (n >= ln || (n == 32767 && ln > 0)) { /* ln overflow ? */
a0d0e21e 1041 /* If it could work, try it. */
bbce6d69 1042 if (c1 == -1000 ||
1043 UCHARAT(reginput) == c1 ||
1044 UCHARAT(reginput) == c2)
1045 {
a0d0e21e 1046 if (regmatch(next))
4633a7c4 1047 sayYES;
bbce6d69 1048 }
a0d0e21e 1049 /* Couldn't or didn't -- back up. */
748a9306 1050 reginput = locinput + ln;
a0d0e21e
LW
1051 if (regrepeat(scan, 1)) {
1052 ln++;
1053 reginput = locinput + ln;
1054 }
1055 else
4633a7c4 1056 sayNO;
a0d0e21e
LW
1057 }
1058 }
1059 else {
1060 n = regrepeat(scan, n);
1061 if (ln < n && regkind[(U8)OP(next)] == EOL &&
1062 (!multiline || OP(next) == SEOL))
1063 ln = n; /* why back off? */
1064 while (n >= ln) {
1065 /* If it could work, try it. */
bbce6d69 1066 if (c1 == -1000 ||
1067 UCHARAT(reginput) == c1 ||
1068 UCHARAT(reginput) == c2)
1069 {
a0d0e21e 1070 if (regmatch(next))
4633a7c4 1071 sayYES;
bbce6d69 1072 }
a0d0e21e
LW
1073 /* Couldn't or didn't -- back up. */
1074 n--;
1075 reginput = locinput + n;
1076 }
1077 }
4633a7c4 1078 sayNO;
a0d0e21e
LW
1079 case SUCCEED:
1080 case END:
1081 reginput = locinput; /* put where regtry can find it */
4633a7c4 1082 sayYES; /* Success! */
a0d0e21e
LW
1083 case IFMATCH:
1084 reginput = locinput;
1085 scan = NEXTOPER(scan);
1086 if (!regmatch(scan))
4633a7c4 1087 sayNO;
a0d0e21e
LW
1088 break;
1089 case UNLESSM:
1090 reginput = locinput;
1091 scan = NEXTOPER(scan);
1092 if (regmatch(scan))
4633a7c4 1093 sayNO;
a0d0e21e
LW
1094 break;
1095 default:
760ac839 1096 PerlIO_printf(PerlIO_stderr(), "%x %d\n",(unsigned)scan,scan[1]);
a0d0e21e 1097 FAIL("regexp memory corruption");
a687059c 1098 }
a0d0e21e
LW
1099 scan = next;
1100 }
a687059c 1101
a0d0e21e
LW
1102 /*
1103 * We get here only if there's trouble -- normally "case END" is
1104 * the terminating point.
1105 */
1106 FAIL("corrupted regexp pointers");
1107 /*NOTREACHED*/
4633a7c4
LW
1108 sayNO;
1109
1110yes:
1111#ifdef DEBUGGING
1112 regindent--;
1113#endif
1114 return 1;
1115
1116no:
1117#ifdef DEBUGGING
1118 regindent--;
1119#endif
a0d0e21e 1120 return 0;
a687059c
LW
1121}
1122
1123/*
1124 - regrepeat - repeatedly match something simple, report how many
1125 */
1126/*
1127 * [This routine now assumes that it will only match on things of length 1.
1128 * That was true before, but now we assume scan - reginput is the count,
1129 * rather than incrementing count on every character.]
1130 */
79072805 1131static I32
00bf170e 1132regrepeat(p, max)
a687059c 1133char *p;
79072805 1134I32 max;
a687059c 1135{
a0d0e21e
LW
1136 register char *scan;
1137 register char *opnd;
1138 register I32 c;
1139 register char *loceol = regeol;
1140
1141 scan = reginput;
1142 if (max != 32767 && max < loceol - scan)
1143 loceol = scan + max;
1144 opnd = OPERAND(p);
1145 switch (OP(p)) {
1146 case ANY:
1147 while (scan < loceol && *scan != '\n')
1148 scan++;
1149 break;
1150 case SANY:
1151 scan = loceol;
1152 break;
bbce6d69 1153 case EXACT: /* length of string is 1 */
1154 c = UCHARAT(++opnd);
1155 while (scan < loceol && UCHARAT(scan) == c)
1156 scan++;
1157 break;
1158 case EXACTF: /* length of string is 1 */
1159 c = UCHARAT(++opnd);
1160 while (scan < loceol &&
1161 (UCHARAT(scan) == c || UCHARAT(scan) == fold[c]))
1162 scan++;
1163 break;
1164 case EXACTFL: /* length of string is 1 */
1165 regtainted = TRUE;
1166 c = UCHARAT(++opnd);
1167 while (scan < loceol &&
1168 (UCHARAT(scan) == c || UCHARAT(scan) == fold_locale[c]))
a0d0e21e
LW
1169 scan++;
1170 break;
1171 case ANYOF:
bbce6d69 1172 while (scan < loceol && reginclass(opnd, *scan))
a0d0e21e 1173 scan++;
a0d0e21e
LW
1174 break;
1175 case ALNUM:
1176 while (scan < loceol && isALNUM(*scan))
1177 scan++;
1178 break;
bbce6d69 1179 case ALNUML:
1180 regtainted = TRUE;
1181 while (scan < loceol && isALNUM_LC(*scan))
1182 scan++;
1183 break;
a0d0e21e
LW
1184 case NALNUM:
1185 while (scan < loceol && !isALNUM(*scan))
1186 scan++;
1187 break;
bbce6d69 1188 case NALNUML:
1189 regtainted = TRUE;
1190 while (scan < loceol && !isALNUM_LC(*scan))
1191 scan++;
1192 break;
a0d0e21e
LW
1193 case SPACE:
1194 while (scan < loceol && isSPACE(*scan))
1195 scan++;
1196 break;
bbce6d69 1197 case SPACEL:
1198 regtainted = TRUE;
1199 while (scan < loceol && isSPACE_LC(*scan))
1200 scan++;
1201 break;
a0d0e21e
LW
1202 case NSPACE:
1203 while (scan < loceol && !isSPACE(*scan))
1204 scan++;
1205 break;
bbce6d69 1206 case NSPACEL:
1207 regtainted = TRUE;
1208 while (scan < loceol && !isSPACE_LC(*scan))
1209 scan++;
1210 break;
a0d0e21e
LW
1211 case DIGIT:
1212 while (scan < loceol && isDIGIT(*scan))
1213 scan++;
1214 break;
1215 case NDIGIT:
1216 while (scan < loceol && !isDIGIT(*scan))
1217 scan++;
1218 break;
1219 default: /* Called on something of 0 width. */
1220 break; /* So match right here or not at all. */
1221 }
a687059c 1222
a0d0e21e
LW
1223 c = scan - reginput;
1224 reginput = scan;
a687059c 1225
a0d0e21e 1226 return(c);
a687059c
LW
1227}
1228
1229/*
bbce6d69 1230 - regclass - determine if a character falls into a character class
1231 */
1232
1233static bool
1234reginclass(p, c)
1235register char *p;
1236register I32 c;
1237{
1238 char flags = *p;
1239 bool match = FALSE;
1240
1241 c &= 0xFF;
1242 if (p[1 + (c >> 3)] & (1 << (c & 7)))
1243 match = TRUE;
1244 else if (flags & ANYOF_FOLD) {
1245 I32 cf;
1246 if (flags & ANYOF_LOCALE) {
1247 regtainted = TRUE;
1248 cf = fold_locale[c];
1249 }
1250 else
1251 cf = fold[c];
1252 if (p[1 + (cf >> 3)] & (1 << (cf & 7)))
1253 match = TRUE;
1254 }
1255
1256 if (!match && (flags & ANYOF_ISA)) {
1257 regtainted = TRUE;
1258
1259 if (((flags & ANYOF_ALNUML) && isALNUM_LC(c)) ||
1260 ((flags & ANYOF_NALNUML) && !isALNUM_LC(c)) ||
1261 ((flags & ANYOF_SPACEL) && isSPACE_LC(c)) ||
1262 ((flags & ANYOF_NSPACEL) && !isSPACE_LC(c)))
1263 {
1264 match = TRUE;
1265 }
1266 }
1267
1268 return match ^ ((flags & ANYOF_INVERT) != 0);
1269}
1270
1271/*
a687059c
LW
1272 - regnext - dig the "next" pointer out of a node
1273 *
1274 * [Note, when REGALIGN is defined there are two places in regmatch()
1275 * that bypass this code for speed.]
1276 */
1277char *
1278regnext(p)
1279register char *p;
1280{
a0d0e21e 1281 register I32 offset;
a687059c 1282
a0d0e21e
LW
1283 if (p == &regdummy)
1284 return(NULL);
a687059c 1285
a0d0e21e
LW
1286 offset = NEXT(p);
1287 if (offset == 0)
1288 return(NULL);
a687059c
LW
1289
1290#ifdef REGALIGN
a0d0e21e 1291 return(p+offset);
a687059c 1292#else
a0d0e21e
LW
1293 if (OP(p) == BACK)
1294 return(p-offset);
1295 else
1296 return(p+offset);
a687059c
LW
1297#endif
1298}