5 * "A fair jaw-cracker dwarf-language must be." --Samwise Gamgee
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!
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.
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.
24 * pregcomp and pregexec -- regsub and regerror are not used in perl
26 * Copyright (c) 1986 by University of Toronto.
27 * Written by Henry Spencer. Not derived from licensed software.
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:
33 * 1. The author is not responsible for the consequences of use of
34 * this software, no matter how awful, even if they arise
37 * 2. The origin of this software must not be misrepresented, either
38 * by explicit claim or by omission.
40 * 3. Altered versions must be plainly marked as such, and must not
41 * be misrepresented as being the original software.
44 **** Alterations to Henry's code are...
46 **** Copyright (c) 1991-1997, Larry Wall
48 **** You may distribute under the terms of either the GNU General Public
49 **** License or the Artistic License, as specified in the README file.
52 * Beware that some of this code is subtly aware of the way operator
53 * precedence is structured in regular expressions. Serious changes in
54 * regular-expression syntax might require a total rethink.
68 # if defined(BUGGY_MSC6)
69 /* MSC 6.00A breaks on op/regexp.t test 85 unless we turn this off */
70 # pragma optimize("a",off)
71 /* But MSC 6.00A is happy with 'w', for aliases only across function calls*/
72 # pragma optimize("w",on )
73 # endif /* BUGGY_MSC6 */
80 #define ISMULT1(c) ((c) == '*' || (c) == '+' || (c) == '?')
81 #define ISMULT2(s) ((*s) == '*' || (*s) == '+' || (*s) == '?' || \
82 ((*s) == '{' && regcurly(s)))
84 #define PERL_META "^$.[()|?+*\\"
86 #define META "^$.[()|?+*\\"
90 #undef SPSTART /* dratted cpp namespace... */
93 * Flags to be passed up and down.
95 #define WORST 0 /* Worst case. */
96 #define HASWIDTH 0x1 /* Known never to match null string. */
97 #define SIMPLE 0x2 /* Simple enough to be STAR/PLUS operand. */
98 #define SPSTART 0x4 /* Starts with * or +. */
99 #define TRYAGAIN 0x8 /* Weeded out a declaration. */
102 * Forward declarations for pregcomp()'s friends.
106 static regnode *reg _((I32, I32 *));
107 static regnode *reganode _((U8, U32));
108 static regnode *regatom _((I32 *));
109 static regnode *regbranch _((I32 *, I32));
110 static void regc _((U8, char *));
111 static regnode *regclass _((void));
112 STATIC I32 regcurly _((char *));
113 static regnode *reg_node _((U8));
114 static regnode *regpiece _((I32 *));
115 static void reginsert _((U8, regnode *));
116 static void regoptail _((regnode *, regnode *));
117 static void regtail _((regnode *, regnode *));
118 static char* regwhite _((char *, char *));
119 static char* nextchar _((void));
120 static void re_croak2 _((const char* pat1,const char* pat2,...)) __attribute__((noreturn));
123 /* Length of a variant. */
132 I32 last_end; /* min value, <0 unless valid. */
135 SV **longest; /* Either &l_fixed, or &l_float. */
139 I32 offset_float_min;
140 I32 offset_float_max;
145 static scan_data_t zero_scan_data = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
147 #define SF_BEFORE_EOL (SF_BEFORE_SEOL|SF_BEFORE_MEOL)
148 #define SF_BEFORE_SEOL 0x1
149 #define SF_BEFORE_MEOL 0x2
150 #define SF_FIX_BEFORE_EOL (SF_FIX_BEFORE_SEOL|SF_FIX_BEFORE_MEOL)
151 #define SF_FL_BEFORE_EOL (SF_FL_BEFORE_SEOL|SF_FL_BEFORE_MEOL)
154 # define SF_FIX_SHIFT_EOL (0+2)
155 # define SF_FL_SHIFT_EOL (0+4)
157 # define SF_FIX_SHIFT_EOL (+2)
158 # define SF_FL_SHIFT_EOL (+4)
161 #define SF_FIX_BEFORE_SEOL (SF_BEFORE_SEOL << SF_FIX_SHIFT_EOL)
162 #define SF_FIX_BEFORE_MEOL (SF_BEFORE_MEOL << SF_FIX_SHIFT_EOL)
164 #define SF_FL_BEFORE_SEOL (SF_BEFORE_SEOL << SF_FL_SHIFT_EOL)
165 #define SF_FL_BEFORE_MEOL (SF_BEFORE_MEOL << SF_FL_SHIFT_EOL) /* 0x20 */
166 #define SF_IS_INF 0x40
167 #define SF_HAS_PAR 0x80
168 #define SF_IN_PAR 0x100
169 #define SF_HAS_EVAL 0x200
170 #define SCF_DO_SUBSTR 0x400
173 scan_commit(scan_data_t *data)
175 STRLEN l = SvCUR(data->last_found);
176 STRLEN old_l = SvCUR(*data->longest);
178 if ((l >= old_l) && ((l > old_l) || (data->flags & SF_BEFORE_EOL))) {
179 sv_setsv(*data->longest, data->last_found);
180 if (*data->longest == data->longest_fixed) {
181 data->offset_fixed = l ? data->last_start_min : data->pos_min;
182 if (data->flags & SF_BEFORE_EOL)
184 |= ((data->flags & SF_BEFORE_EOL) << SF_FIX_SHIFT_EOL);
186 data->flags &= ~SF_FIX_BEFORE_EOL;
188 data->offset_float_min = l ? data->last_start_min : data->pos_min;
189 data->offset_float_max = (l
190 ? data->last_start_max
191 : data->pos_min + data->pos_delta);
192 if (data->flags & SF_BEFORE_EOL)
194 |= ((data->flags & SF_BEFORE_EOL) << SF_FL_SHIFT_EOL);
196 data->flags &= ~SF_FL_BEFORE_EOL;
199 SvCUR_set(data->last_found, 0);
201 data->flags &= ~SF_BEFORE_EOL;
204 /* Stops at toplevel WHILEM as well as at `last'. At end *scanp is set
205 to the position after last scanned or to NULL. */
208 study_chunk(regnode **scanp, I32 *deltap, regnode *last, scan_data_t *data, U32 flags)
209 /* scanp: Start here (read-write). */
210 /* deltap: Write maxlen-minlen here. */
211 /* last: Stop before this one. */
213 I32 min = 0, pars = 0, code;
214 regnode *scan = *scanp, *next;
216 int is_inf = (flags & SCF_DO_SUBSTR) && (data->flags & SF_IS_INF);
217 I32 is_par = OP(scan) == OPEN ? ARG(scan) : 0;
218 scan_data_t data_fake;
220 while (scan && OP(scan) != END && scan < last) {
221 /* Peephole optimizer: */
223 if (regkind[(U8)OP(scan)] == EXACT) {
224 regnode *n = regnext(scan);
227 regnode *stop = scan;
230 next = scan + (*OPERAND(scan) + 2 - 1)/sizeof(regnode) + 2;
231 /* Skip NOTHING, merge EXACT*. */
233 ( regkind[(U8)OP(n)] == NOTHING ||
234 (stringok && (OP(n) == OP(scan))))
236 && NEXT_OFF(scan) + NEXT_OFF(n) < I16_MAX) {
237 if (OP(n) == TAIL || n > next)
239 if (regkind[(U8)OP(n)] == NOTHING) {
240 NEXT_OFF(scan) += NEXT_OFF(n);
241 next = n + NODE_STEP_REGNODE;
248 int oldl = *OPERAND(scan);
249 regnode *nnext = regnext(n);
251 if (oldl + *OPERAND(n) > U8_MAX)
253 NEXT_OFF(scan) += NEXT_OFF(n);
254 *OPERAND(scan) += *OPERAND(n);
255 next = n + (*OPERAND(n) + 2 - 1)/sizeof(regnode) + 2;
256 /* Now we can overwrite *n : */
257 Move(OPERAND(n) + 1, OPERAND(scan) + oldl + 1,
258 *OPERAND(n) + 1, char);
268 n = scan + (*OPERAND(scan) + 2 - 1)/sizeof(regnode) + 2;
270 if (regkind[(U8)OP(n)] != NOTHING || OP(n) == NOTHING) {
279 if (OP(scan) != CURLYX) {
280 int max = (reg_off_by_arg[OP(scan)] ? I32_MAX : U16_MAX);
281 int off = (reg_off_by_arg[OP(scan)] ? ARG(scan) : NEXT_OFF(scan));
285 /* Skip NOTHING and LONGJMP. */
286 while ((n = regnext(n))
287 && ((regkind[(U8)OP(n)] == NOTHING && (noff = NEXT_OFF(n)))
288 || ((OP(n) == LONGJMP) && (noff = ARG(n))))
291 if (reg_off_by_arg[OP(scan)])
294 NEXT_OFF(scan) = off;
296 if (OP(scan) == BRANCH || OP(scan) == BRANCHJ
297 || OP(scan) == IFTHEN || OP(scan) == SUSPEND) {
298 next = regnext(scan);
301 if (OP(next) == code || code == IFTHEN || code == SUSPEND) {
302 I32 max1 = 0, min1 = I32_MAX, num = 0;
304 if (flags & SCF_DO_SUBSTR)
306 while (OP(scan) == code) {
307 I32 deltanext, minnext;
311 next = regnext(scan);
312 scan = NEXTOPER(scan);
314 scan = NEXTOPER(scan);
315 /* We suppose the run is continuous, last=next...*/
316 minnext = study_chunk(&scan, &deltanext, next,
320 if (max1 < minnext + deltanext)
321 max1 = minnext + deltanext;
322 if (deltanext == I32_MAX)
325 if (data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
327 if (data && (data_fake.flags & SF_HAS_EVAL))
328 data->flags |= SF_HAS_EVAL;
332 if (code == IFTHEN && num < 2) /* Empty ELSE branch */
334 if (flags & SCF_DO_SUBSTR) {
335 data->pos_min += min1;
336 data->pos_delta += max1 - min1;
337 if (max1 != min1 || is_inf)
338 data->longest = &(data->longest_float);
341 delta += max1 - min1;
342 } else if (code == BRANCHJ) /* single branch is optimized. */
343 scan = NEXTOPER(NEXTOPER(scan));
344 else /* single branch is optimized. */
345 scan = NEXTOPER(scan);
347 } else if (OP(scan) == EXACT) {
348 min += *OPERAND(scan);
349 if (flags & SCF_DO_SUBSTR) { /* Update longest substr. */
350 I32 l = *OPERAND(scan);
352 /* The code below prefers earlier match for fixed
353 offset, later match for variable offset. */
354 if (data->last_end == -1) { /* Update the start info. */
355 data->last_start_min = data->pos_min;
356 data->last_start_max = is_inf
357 ? I32_MAX : data->pos_min + data->pos_delta;
359 sv_catpvn(data->last_found, (char *)(OPERAND(scan)+1), l);
360 data->last_end = data->pos_min + l;
361 data->pos_min += l; /* As in the first entry. */
362 data->flags &= ~SF_BEFORE_EOL;
364 } else if (regkind[(U8)OP(scan)] == EXACT) {
365 if (flags & SCF_DO_SUBSTR)
367 min += *OPERAND(scan);
368 if (data && (flags & SCF_DO_SUBSTR))
369 data->pos_min += *OPERAND(scan);
370 } else if (strchr(varies,OP(scan))) {
371 I32 mincount, maxcount, minnext, deltanext, pos_before, fl;
372 regnode *oscan = scan;
374 switch (regkind[(U8)OP(scan)]) {
376 scan = NEXTOPER(scan);
379 if (flags & SCF_DO_SUBSTR) {
380 next = NEXTOPER(scan);
381 if (OP(next) == EXACT) {
383 maxcount = REG_INFTY;
384 next = regnext(scan);
385 scan = NEXTOPER(scan);
389 if (flags & SCF_DO_SUBSTR)
395 scan = regnext(scan);
396 if (flags & SCF_DO_SUBSTR) {
398 data->longest = &(data->longest_float);
400 goto optimize_curly_tail;
402 mincount = ARG1(scan);
403 maxcount = ARG2(scan);
404 next = regnext(scan);
405 scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
407 if (flags & SCF_DO_SUBSTR) {
408 if (mincount == 0) scan_commit(data);
409 pos_before = data->pos_min;
413 data->flags &= ~(SF_HAS_PAR|SF_IN_PAR|SF_HAS_EVAL);
415 data->flags |= SF_IS_INF;
417 /* This will finish on WHILEM, setting scan, or on NULL: */
418 minnext = study_chunk(&scan, &deltanext, last, data,
420 ? (flags & ~SCF_DO_SUBSTR) : flags);
421 if (!scan) /* It was not CURLYX, but CURLY. */
423 if (dowarn && (minnext + deltanext == 0)
424 && !(data->flags & (SF_HAS_PAR|SF_IN_PAR)))
425 warn("Strange *+?{} on zero-length expression");
426 min += minnext * mincount;
427 is_inf |= (maxcount == REG_INFTY && (minnext + deltanext) > 0
428 || deltanext == I32_MAX);
429 delta += (minnext + deltanext) * maxcount - minnext * mincount;
431 /* Try powerful optimization CURLYX => CURLYN. */
432 if ( OP(oscan) == CURLYX && data
433 && data->flags & SF_IN_PAR
434 && !(data->flags & SF_HAS_EVAL)
435 && !deltanext && minnext == 1 ) {
436 /* Try to optimize to CURLYN. */
437 regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS;
438 regnode *nxt1 = nxt, *nxt2;
442 if (!strchr(simple,OP(nxt))
443 && !(regkind[(U8)OP(nxt)] == EXACT
444 && *OPERAND(nxt) == 1))
448 if (OP(nxt) != CLOSE)
450 /* Now we know that nxt2 is the only contents: */
451 oscan->flags = ARG(nxt);
453 OP(nxt1) = NOTHING; /* was OPEN. */
455 OP(nxt1 + 1) = OPTIMIZED; /* was count. */
456 NEXT_OFF(nxt1+ 1) = 0; /* just for consistancy. */
457 NEXT_OFF(nxt2) = 0; /* just for consistancy with CURLY. */
458 OP(nxt) = OPTIMIZED; /* was CLOSE. */
459 OP(nxt + 1) = OPTIMIZED; /* was count. */
460 NEXT_OFF(nxt+ 1) = 0; /* just for consistancy. */
465 /* Try optimization CURLYX => CURLYM. */
466 if ( OP(oscan) == CURLYX && data
467 && !(data->flags & SF_HAS_PAR)
468 && !(data->flags & SF_HAS_EVAL)
470 /* XXXX How to optimize if data == 0? */
471 /* Optimize to a simpler form. */
472 regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN */
476 while ( (nxt2 = regnext(nxt)) /* skip over embedded stuff*/
477 && (OP(nxt2) != WHILEM))
479 OP(nxt2) = SUCCEED; /* Whas WHILEM */
480 /* Need to optimize away parenths. */
481 if (data->flags & SF_IN_PAR) {
482 /* Set the parenth number. */
483 regnode *nxt1 = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN*/
485 if (OP(nxt) != CLOSE)
486 FAIL("panic opt close");
487 oscan->flags = ARG(nxt);
488 OP(nxt1) = OPTIMIZED; /* was OPEN. */
489 OP(nxt) = OPTIMIZED; /* was CLOSE. */
491 OP(nxt1 + 1) = OPTIMIZED; /* was count. */
492 OP(nxt + 1) = OPTIMIZED; /* was count. */
493 NEXT_OFF(nxt1 + 1) = 0; /* just for consistancy. */
494 NEXT_OFF(nxt + 1) = 0; /* just for consistancy. */
497 while ( nxt1 && (OP(nxt1) != WHILEM)) {
498 regnode *nnxt = regnext(nxt1);
501 if (reg_off_by_arg[OP(nxt1)])
502 ARG_SET(nxt1, nxt2 - nxt1);
503 else if (nxt2 - nxt1 < U16_MAX)
504 NEXT_OFF(nxt1) = nxt2 - nxt1;
506 OP(nxt) = NOTHING; /* Cannot beautify */
511 /* Optimize again: */
512 study_chunk(&nxt1, &deltanext, nxt, NULL, 0);
516 if (data && fl & (SF_HAS_PAR|SF_IN_PAR))
518 if (flags & SCF_DO_SUBSTR) {
519 SV *last_str = Nullsv;
520 int counted = mincount != 0;
522 if (data->last_end > 0 && mincount != 0) { /* Ends with a string. */
523 I32 b = pos_before >= data->last_start_min
524 ? pos_before : data->last_start_min;
526 char *s = SvPV(data->last_found, l);
528 l -= b - data->last_start_min;
529 /* Get the added string: */
530 last_str = newSVpv(s + b - data->last_start_min, l);
531 if (deltanext == 0 && pos_before == b) {
532 /* What was added is a constant string */
534 SvGROW(last_str, (mincount * l) + 1);
535 repeatcpy(SvPVX(last_str) + l,
536 SvPVX(last_str), l, mincount - 1);
537 SvCUR(last_str) *= mincount;
538 /* Add additional parts. */
539 SvCUR_set(data->last_found,
540 SvCUR(data->last_found) - l);
541 sv_catsv(data->last_found, last_str);
542 data->last_end += l * (mincount - 1);
546 /* It is counted once already... */
547 data->pos_min += minnext * (mincount - counted);
548 data->pos_delta += - counted * deltanext +
549 (minnext + deltanext) * maxcount - minnext * mincount;
550 if (mincount != maxcount) {
552 if (mincount && last_str) {
553 sv_setsv(data->last_found, last_str);
554 data->last_end = data->pos_min;
555 data->last_start_min =
556 data->pos_min - SvCUR(last_str);
557 data->last_start_max = is_inf
559 : data->pos_min + data->pos_delta
562 data->longest = &(data->longest_float);
565 if (data && (fl & SF_HAS_EVAL))
566 data->flags |= SF_HAS_EVAL;
568 if (OP(oscan) != CURLYX) {
569 while (regkind[(U8)OP(next = regnext(oscan))] == NOTHING
571 NEXT_OFF(oscan) += NEXT_OFF(next);
574 default: /* REF only? */
575 if (flags & SCF_DO_SUBSTR) {
577 data->longest = &(data->longest_float);
582 } else if (strchr(simple,OP(scan))) {
583 if (flags & SCF_DO_SUBSTR) {
588 } else if (regkind[(U8)OP(scan)] == EOL && flags & SCF_DO_SUBSTR) {
589 data->flags |= (OP(scan) == MEOL
592 } else if (regkind[(U8)OP(scan)] == BRANCHJ
593 && (scan->flags || data)
594 && (OP(scan) == IFMATCH || OP(scan) == UNLESSM)) {
595 I32 deltanext, minnext;
599 next = regnext(scan);
600 nscan = NEXTOPER(NEXTOPER(scan));
601 minnext = study_chunk(&nscan, &deltanext, last, &data_fake, 0);
604 FAIL("variable length lookbehind not implemented");
605 } else if (minnext > U8_MAX) {
606 FAIL2("lookbehind longer than %d not implemented", U8_MAX);
608 scan->flags = minnext;
610 if (data && data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
612 if (data && (data_fake.flags & SF_HAS_EVAL))
613 data->flags |= SF_HAS_EVAL;
614 } else if (OP(scan) == OPEN) {
616 } else if (OP(scan) == CLOSE && ARG(scan) == is_par) {
617 next = regnext(scan);
619 if ( next && (OP(next) != WHILEM) && next < last)
620 is_par = 0; /* Disable optimization */
621 } else if (OP(scan) == EVAL) {
623 data->flags |= SF_HAS_EVAL;
625 /* Else: zero-length, ignore. */
626 scan = regnext(scan);
631 *deltap = is_inf ? I32_MAX : delta;
632 if (flags & SCF_DO_SUBSTR && is_inf)
633 data->pos_delta = I32_MAX - data->pos_min;
636 if (is_par && pars==1 && data) {
637 data->flags |= SF_IN_PAR;
638 data->flags &= ~SF_HAS_PAR;
639 } else if (pars && data) {
640 data->flags |= SF_HAS_PAR;
641 data->flags &= ~SF_IN_PAR;
647 add_data(I32 n, char *s)
649 if (regcomp_rx->data) {
650 Renewc(regcomp_rx->data,
651 sizeof(*regcomp_rx->data) + sizeof(void*) * (regcomp_rx->data->count + n - 1),
652 char, struct reg_data);
653 Renew(regcomp_rx->data->what, regcomp_rx->data->count + n, U8);
654 regcomp_rx->data->count += n;
656 Newc(1207, regcomp_rx->data, sizeof(*regcomp_rx->data) + sizeof(void*) * (n - 1),
657 char, struct reg_data);
658 New(1208, regcomp_rx->data->what, n, U8);
659 regcomp_rx->data->count = n;
661 Copy(s, regcomp_rx->data->what + regcomp_rx->data->count - n, n, U8);
662 return regcomp_rx->data->count - n;
666 - pregcomp - compile a regular expression into internal code
668 * We can't allocate space until we know how big the compiled form will be,
669 * but we can't compile it (and thus know how big it is) until we've got a
670 * place to put the code. So we cheat: we compile it twice, once with code
671 * generation turned off and size counting turned on, and once "for real".
672 * This also means that we don't allocate space until we are sure that the
673 * thing really will compile successfully, and we never have to move the
674 * code and thus invalidate pointers into it. (Note that it has to be in
675 * one piece because free() must be able to free it all.) [NB: not true in perl]
677 * Beware that the optimization-preparation code in here knows about some
678 * of the structure of the compiled regexp. [I'll say.]
681 pregcomp(char *exp, char *xend, PMOP *pm)
695 FAIL("NULL regexp argument");
697 regprecomp = savepvn(exp, xend - exp);
698 DEBUG_r(PerlIO_printf(Perl_debug_log, "compiling RE `%*s'\n",
699 xend - exp, regprecomp));
700 regflags = pm->op_pmflags;
704 seen_zerolen = *exp == '^' ? -1 : 0;
707 /* First pass: determine size, legality. */
714 regc((U8)MAGIC, (char*)regcode);
715 if (reg(0, &flags) == NULL) {
716 Safefree(regprecomp);
720 DEBUG_r(PerlIO_printf(Perl_debug_log, "size %d ", regsize));
725 char *s = PerlEnv_getenv("TERMCAP_COLORS");
729 colors[0] = s = savepv(s);
733 FAIL("Not enough TABs in TERMCAP_COLORS");
742 PerlIO_printf(Perl_debug_log, "%s%s%s%s",
743 colors[0],colors[1],colors[2],colors[3]);
747 /* Small enough for pointer-storage convention?
748 If extralen==0, this means that we will not need long jumps. */
749 if (regsize >= 0x10000L && extralen)
754 /* Allocate space and initialize. */
755 Newc(1001, r, sizeof(regexp) + (unsigned)regsize * sizeof(regnode),
758 FAIL("regexp out of space");
760 r->prelen = xend - exp;
761 r->precomp = regprecomp;
762 r->subbeg = r->subbase = NULL;
765 /* Second pass: emit code. */
770 regcode = r->program;
771 regc((U8)MAGIC, (char*) regcode++);
773 if (reg(0, &flags) == NULL)
776 /* Dig out information for optimizations. */
777 pm->op_pmflags = regflags;
779 r->regstclass = NULL;
780 r->naughty = regnaughty >= 10; /* Probably an expensive pattern. */
781 scan = r->program + 1; /* First BRANCH. */
783 /* XXXX To minimize changes to RE engine we always allocate
784 3-units-long substrs field. */
785 Newz(1004, r->substrs, 1, struct reg_substr_data);
787 if (OP(scan) != BRANCH) { /* Only one top-level choice. */
790 STRLEN longest_float_length, longest_fixed_length;
792 StructCopy(&zero_scan_data, &data, scan_data_t);
794 /* Skip introductions and multiplicators >= 1. */
795 while ((OP(first) == OPEN && (sawopen = 1)) ||
796 (OP(first) == BRANCH && OP(regnext(first)) != BRANCH) ||
797 (OP(first) == PLUS) ||
798 (OP(first) == MINMOD) ||
799 (regkind[(U8)OP(first)] == CURLY && ARG1(first) > 0) ) {
800 if (OP(first) == PLUS)
803 first += regarglen[(U8)OP(first)];
804 first = NEXTOPER(first);
807 /* Starting-point info. */
809 if (OP(first) == EXACT); /* Empty, get anchored substr later. */
810 else if (strchr(simple+2,OP(first)))
811 r->regstclass = first;
812 else if (regkind[(U8)OP(first)] == BOUND ||
813 regkind[(U8)OP(first)] == NBOUND)
814 r->regstclass = first;
815 else if (regkind[(U8)OP(first)] == BOL) {
816 r->reganch |= (OP(first) == MBOL ? ROPT_ANCH_MBOL: ROPT_ANCH_BOL);
817 first = NEXTOPER(first);
820 else if (OP(first) == GPOS) {
821 r->reganch |= ROPT_ANCH_GPOS;
822 first = NEXTOPER(first);
825 else if ((OP(first) == STAR &&
826 regkind[(U8)OP(NEXTOPER(first))] == ANY) &&
827 !(r->reganch & ROPT_ANCH) )
829 /* turn .* into ^.* with an implied $*=1 */
830 r->reganch |= ROPT_ANCH_BOL | ROPT_IMPLICIT;
831 first = NEXTOPER(first);
834 if (sawplus && (!sawopen || !regsawback))
835 r->reganch |= ROPT_SKIP; /* x+ must match 1st of run */
837 /* Scan is after the zeroth branch, first is atomic matcher. */
838 DEBUG_r(PerlIO_printf(Perl_debug_log, "first at %d\n",
841 * If there's something expensive in the r.e., find the
842 * longest literal string that must appear and make it the
843 * regmust. Resolve ties in favor of later strings, since
844 * the regstart check works with the beginning of the r.e.
845 * and avoiding duplication strengthens checking. Not a
846 * strong reason, but sufficient in the absence of others.
847 * [Now we resolve ties in favor of the earlier string if
848 * it happens that c_offset_min has been invalidated, since the
849 * earlier string may buy us something the later one won't.]
853 data.longest_fixed = newSVpv("",0);
854 data.longest_float = newSVpv("",0);
855 data.last_found = newSVpv("",0);
856 data.longest = &(data.longest_fixed);
859 minlen = study_chunk(&first, &fake, scan + regsize, /* Up to end */
860 &data, SCF_DO_SUBSTR);
861 if ( regnpar == 1 && data.longest == &(data.longest_fixed)
862 && data.last_start_min == 0 && data.last_end > 0
864 && (!(regseen & REG_SEEN_GPOS) || (r->reganch & ROPT_ANCH_GPOS)))
865 r->reganch |= ROPT_CHECK_ALL;
867 SvREFCNT_dec(data.last_found);
869 longest_float_length = SvCUR(data.longest_float);
870 if (longest_float_length
871 || (data.flags & SF_FL_BEFORE_EOL
872 && (!(data.flags & SF_FL_BEFORE_MEOL)
873 || (regflags & PMf_MULTILINE)))) {
874 if (SvCUR(data.longest_fixed)
875 && data.offset_fixed == data.offset_float_min)
876 goto remove; /* Like in (a)+. */
878 r->float_substr = data.longest_float;
879 r->float_min_offset = data.offset_float_min;
880 r->float_max_offset = data.offset_float_max;
881 fbm_compile(r->float_substr, 0);
882 BmUSEFUL(r->float_substr) = 100;
883 if (data.flags & SF_FL_BEFORE_EOL /* Cannot have SEOL and MULTI */
884 && (!(data.flags & SF_FL_BEFORE_MEOL)
885 || (regflags & PMf_MULTILINE)))
886 SvTAIL_on(r->float_substr);
889 r->float_substr = Nullsv;
890 SvREFCNT_dec(data.longest_float);
891 longest_float_length = 0;
894 longest_fixed_length = SvCUR(data.longest_fixed);
895 if (longest_fixed_length
896 || (data.flags & SF_FIX_BEFORE_EOL /* Cannot have SEOL and MULTI */
897 && (!(data.flags & SF_FIX_BEFORE_MEOL)
898 || (regflags & PMf_MULTILINE)))) {
899 r->anchored_substr = data.longest_fixed;
900 r->anchored_offset = data.offset_fixed;
901 fbm_compile(r->anchored_substr, 0);
902 BmUSEFUL(r->anchored_substr) = 100;
903 if (data.flags & SF_FIX_BEFORE_EOL /* Cannot have SEOL and MULTI */
904 && (!(data.flags & SF_FIX_BEFORE_MEOL)
905 || (regflags & PMf_MULTILINE)))
906 SvTAIL_on(r->anchored_substr);
908 r->anchored_substr = Nullsv;
909 SvREFCNT_dec(data.longest_fixed);
910 longest_fixed_length = 0;
913 /* A temporary algorithm prefers floated substr to fixed one to dig more info. */
914 if (longest_fixed_length > longest_float_length) {
915 r->check_substr = r->anchored_substr;
916 r->check_offset_min = r->check_offset_max = r->anchored_offset;
917 if (r->reganch & ROPT_ANCH_SINGLE)
918 r->reganch |= ROPT_NOSCAN;
920 r->check_substr = r->float_substr;
921 r->check_offset_min = data.offset_float_min;
922 r->check_offset_max = data.offset_float_max;
925 /* Several toplevels. Best we can is to set minlen. */
928 DEBUG_r(PerlIO_printf(Perl_debug_log, "\n"));
929 scan = r->program + 1;
930 minlen = study_chunk(&scan, &fake, scan + regsize, NULL, 0);
931 r->check_substr = r->anchored_substr = r->float_substr = Nullsv;
934 r->nparens = regnpar - 1;
936 if (regseen & REG_SEEN_GPOS)
937 r->reganch |= ROPT_GPOS_SEEN;
938 if (regseen & REG_SEEN_LOOKBEHIND)
939 r->reganch |= ROPT_LOOKBEHIND_SEEN;
940 if (regseen & REG_SEEN_EVAL)
941 r->reganch |= ROPT_EVAL_SEEN;
942 Newz(1002, r->startp, regnpar, char*);
943 Newz(1002, r->endp, regnpar, char*);
949 - reg - regular expression, i.e. main body or parenthesized thing
951 * Caller must absorb opening parenthesis.
953 * Combining parenthesis handling with the base level of regular expression
954 * is a trifle forced, but the need to tie the tails of the branches to what
955 * follows makes it hard to avoid.
958 reg(I32 paren, I32 *flagp)
959 /* paren: Parenthesized? 0=top, 1=(, inside: changed to letter. */
961 register regnode *ret; /* Will be the head of the group. */
962 register regnode *br;
963 register regnode *lastbr;
964 register regnode *ender = 0;
965 register I32 parno = 0;
966 I32 flags, oregflags = regflags, have_branch = 0, open = 0;
969 *flagp = HASWIDTH; /* Tentatively. */
971 /* Make an OPEN node, if parenthesized. */
973 if (*regcomp_parse == '?') {
975 paren = *regcomp_parse++;
976 ret = NULL; /* For look-ahead/behind. */
979 regseen |= REG_SEEN_LOOKBEHIND;
980 if (*regcomp_parse == '!')
982 if (*regcomp_parse != '=' && *regcomp_parse != '!')
993 FAIL2("Sequence (?%c...) not implemented", (int)paren);
996 while (*regcomp_parse && *regcomp_parse != ')')
998 if (*regcomp_parse != ')')
999 FAIL("Sequence (?#... not terminated");
1006 I32 count = 1, n = 0;
1008 char *s = regcomp_parse;
1010 OP_4tree *sop, *rop;
1013 regseen |= REG_SEEN_EVAL;
1014 while (count && (c = *regcomp_parse)) {
1015 if (c == '\\' && regcomp_parse[1])
1023 if (*regcomp_parse != ')')
1024 FAIL("Sequence (?{...}) not terminated or not {}-balanced");
1028 if (regcomp_parse - 1 - s)
1029 sv = newSVpv(s, regcomp_parse - 1 - s);
1031 sv = newSVpv("", 0);
1033 rop = sv_compile_2op(sv, &sop, "re", &av);
1035 n = add_data(3, "nso");
1036 regcomp_rx->data->data[n] = (void*)rop;
1037 regcomp_rx->data->data[n+1] = (void*)av;
1038 regcomp_rx->data->data[n+2] = (void*)sop;
1040 } else { /* First pass */
1042 FAIL("Eval-group in insecure regular expression");
1046 return reganode(EVAL, n);
1050 if (regcomp_parse[0] == '?') {
1051 if (regcomp_parse[1] == '=' || regcomp_parse[1] == '!'
1052 || regcomp_parse[1] == '<'
1053 || regcomp_parse[1] == '{') { /* Lookahead or eval. */
1056 ret = reg_node(LOGICAL);
1057 regtail(ret, reg(1, &flag));
1060 } else if (regcomp_parse[0] >= '1' && regcomp_parse[0] <= '9' ) {
1061 parno = atoi(regcomp_parse++);
1063 while (isDIGIT(*regcomp_parse))
1065 ret = reganode(GROUPP, parno);
1066 if ((c = *nextchar()) != ')')
1067 FAIL2("Switch (?(number%c not recognized", c);
1069 regtail(ret, reganode(IFTHEN, 0));
1070 br = regbranch(&flags, 1);
1072 br = reganode(LONGJMP, 0);
1074 regtail(br, reganode(LONGJMP, 0));
1077 lastbr = reganode(IFTHEN, 0); /* Fake one for optimizer. */
1078 regbranch(&flags, 1);
1079 regtail(ret, lastbr);
1084 FAIL("Switch (?(condition)... contains too many branches");
1085 ender = reg_node(TAIL);
1088 regtail(lastbr, ender);
1089 regtail(NEXTOPER(NEXTOPER(lastbr)), ender);
1091 regtail(ret, ender);
1094 FAIL2("Unknown condition for (?(%.2s", regcomp_parse);
1098 FAIL("Sequence (? incomplete");
1102 while (*regcomp_parse && strchr("iogcmsx", *regcomp_parse)) {
1103 if (*regcomp_parse != 'o')
1104 pmflag(®flags, *regcomp_parse);
1108 if (*regcomp_parse != ')')
1109 FAIL2("Sequence (?%c...) not recognized", *regcomp_parse);
1118 ret = reganode(OPEN, parno);
1124 /* Pick up the branches, linking them together. */
1125 br = regbranch(&flags, 1);
1128 if (*regcomp_parse == '|') {
1129 if (!SIZE_ONLY && extralen) {
1130 reginsert(BRANCHJ, br);
1132 reginsert(BRANCH, br);
1135 extralen += 1; /* For BRANCHJ-BRANCH. */
1136 } else if (paren == ':') {
1137 *flagp |= flags&SIMPLE;
1139 if (open) { /* Starts with OPEN. */
1140 regtail(ret, br); /* OPEN -> first. */
1141 } else if (paren != '?') /* Not Conditional */
1143 if (!(flags&HASWIDTH))
1144 *flagp &= ~HASWIDTH;
1145 *flagp |= flags&SPSTART;
1147 while (*regcomp_parse == '|') {
1148 if (!SIZE_ONLY && extralen) {
1149 ender = reganode(LONGJMP,0);
1150 regtail(NEXTOPER(NEXTOPER(lastbr)), ender); /* Append to the previous. */
1153 extralen += 2; /* Account for LONGJMP. */
1155 br = regbranch(&flags, 0);
1158 regtail(lastbr, br); /* BRANCH -> BRANCH. */
1160 if (!(flags&HASWIDTH))
1161 *flagp &= ~HASWIDTH;
1162 *flagp |= flags&SPSTART;
1165 if (have_branch || paren != ':') {
1166 /* Make a closing node, and hook it on the end. */
1169 ender = reg_node(TAIL);
1172 ender = reganode(CLOSE, parno);
1179 ender = reg_node(SUCCEED);
1180 *flagp &= ~HASWIDTH;
1183 ender = reg_node(END);
1186 regtail(lastbr, ender);
1189 /* Hook the tails of the branches to the closing node. */
1190 for (br = ret; br != NULL; br = regnext(br)) {
1191 regoptail(br, ender);
1198 static char parens[] = "=!<,>";
1200 if (paren && (p = strchr(parens, paren))) {
1201 int node = ((p - parens) % 2) ? UNLESSM : IFMATCH;
1202 int flag = (p - parens) > 1;
1205 node = SUSPEND, flag = 0;
1206 reginsert(node,ret);
1208 regtail(ret, reg_node(TAIL));
1212 /* Check for proper termination. */
1213 if (paren && (regcomp_parse >= regxend || *nextchar() != ')')) {
1214 FAIL("unmatched () in regexp");
1215 } else if (!paren && regcomp_parse < regxend) {
1216 if (*regcomp_parse == ')') {
1217 FAIL("unmatched () in regexp");
1219 FAIL("junk on end of regexp"); /* "Can't happen". */
1223 regflags = oregflags;
1230 - regbranch - one alternative of an | operator
1232 * Implements the concatenation operator.
1235 regbranch(I32 *flagp, I32 first)
1237 register regnode *ret;
1238 register regnode *chain = NULL;
1239 register regnode *latest;
1240 I32 flags = 0, c = 0;
1245 if (!SIZE_ONLY && extralen)
1246 ret = reganode(BRANCHJ,0);
1248 ret = reg_node(BRANCH);
1251 if (!first && SIZE_ONLY)
1252 extralen += 1; /* BRANCHJ */
1254 *flagp = WORST; /* Tentatively. */
1258 while (regcomp_parse < regxend && *regcomp_parse != '|' && *regcomp_parse != ')') {
1260 latest = regpiece(&flags);
1261 if (latest == NULL) {
1262 if (flags & TRYAGAIN)
1265 } else if (ret == NULL)
1267 *flagp |= flags&HASWIDTH;
1268 if (chain == NULL) /* First piece. */
1269 *flagp |= flags&SPSTART;
1272 regtail(chain, latest);
1277 if (chain == NULL) { /* Loop ran zero times. */
1278 chain = reg_node(NOTHING);
1283 *flagp |= flags&SIMPLE;
1290 - regpiece - something followed by possible [*+?]
1292 * Note that the branching code sequences used for ? and the general cases
1293 * of * and + are somewhat optimized: they use the same NOTHING node as
1294 * both the endmarker for their branch list and the body of the last branch.
1295 * It might seem that this node could be dispensed with entirely, but the
1296 * endmarker role is not redundant.
1299 regpiece(I32 *flagp)
1301 register regnode *ret;
1303 register char *next;
1305 char *origparse = regcomp_parse;
1308 I32 max = REG_INFTY;
1310 ret = regatom(&flags);
1312 if (flags & TRYAGAIN)
1317 op = *regcomp_parse;
1319 if (op == '{' && regcurly(regcomp_parse)) {
1320 next = regcomp_parse + 1;
1322 while (isDIGIT(*next) || *next == ',') {
1331 if (*next == '}') { /* got one */
1335 min = atoi(regcomp_parse);
1339 maxpos = regcomp_parse;
1341 if (!max && *maxpos != '0')
1342 max = REG_INFTY; /* meaning "infinity" */
1343 else if (max >= REG_INFTY)
1344 FAIL2("Quantifier in {,} bigger than %d", REG_INFTY - 1);
1345 regcomp_parse = next;
1349 if ((flags&SIMPLE)) {
1350 regnaughty += 2 + regnaughty / 2;
1351 reginsert(CURLY, ret);
1354 regnaughty += 4 + regnaughty; /* compound interest */
1355 regtail(ret, reg_node(WHILEM));
1356 if (!SIZE_ONLY && extralen) {
1357 reginsert(LONGJMP,ret);
1358 reginsert(NOTHING,ret);
1359 NEXT_OFF(ret) = 3; /* Go over LONGJMP. */
1361 reginsert(CURLYX,ret);
1362 if (!SIZE_ONLY && extralen)
1363 NEXT_OFF(ret) = 3; /* Go over NOTHING to LONGJMP. */
1364 regtail(ret, reg_node(NOTHING));
1371 *flagp = (WORST|HASWIDTH);
1372 if (max && max < min)
1373 FAIL("Can't do {n,m} with n > m");
1388 #if 0 /* Now runtime fix should be reliable. */
1389 if (!(flags&HASWIDTH) && op != '?')
1390 FAIL("regexp *+ operand could be empty");
1395 *flagp = (op != '+') ? (WORST|SPSTART) : (WORST|HASWIDTH);
1397 if (op == '*' && (flags&SIMPLE)) {
1398 reginsert(STAR, ret);
1402 else if (op == '*') {
1405 } else if (op == '+' && (flags&SIMPLE)) {
1406 reginsert(PLUS, ret);
1410 else if (op == '+') {
1413 } else if (op == '?') {
1418 if (dowarn && !SIZE_ONLY && !(flags&HASWIDTH) && max > 10000) {
1419 warn("%.*s matches null string many times",
1420 regcomp_parse - origparse, origparse);
1423 if (*regcomp_parse == '?') {
1425 reginsert(MINMOD, ret);
1426 regtail(ret, ret + NODE_STEP_REGNODE);
1428 if (ISMULT2(regcomp_parse))
1429 FAIL("nested *?+ in regexp");
1435 - regatom - the lowest level
1437 * Optimization: gobbles an entire sequence of ordinary characters so that
1438 * it can turn them into a single node, which is smaller to store and
1439 * faster to run. Backslashed characters are exceptions, each becoming a
1440 * separate node; the code is simpler that way and it's not worth fixing.
1442 * [Yes, it is worth fixing, some scripts can run twice the speed.]
1447 register regnode *ret = 0;
1450 *flagp = WORST; /* Tentatively. */
1453 switch (*regcomp_parse) {
1457 if (regflags & PMf_MULTILINE)
1458 ret = reg_node(MBOL);
1459 else if (regflags & PMf_SINGLELINE)
1460 ret = reg_node(SBOL);
1462 ret = reg_node(BOL);
1465 if (regcomp_parse[1])
1468 if (regflags & PMf_MULTILINE)
1469 ret = reg_node(MEOL);
1470 else if (regflags & PMf_SINGLELINE)
1471 ret = reg_node(SEOL);
1473 ret = reg_node(EOL);
1477 if (regflags & PMf_SINGLELINE)
1478 ret = reg_node(SANY);
1480 ret = reg_node(ANY);
1482 *flagp |= HASWIDTH|SIMPLE;
1487 *flagp |= HASWIDTH|SIMPLE;
1491 ret = reg(1, &flags);
1493 if (flags & TRYAGAIN)
1497 *flagp |= flags&(HASWIDTH|SPSTART|SIMPLE);
1501 if (flags & TRYAGAIN) {
1505 FAIL2("internal urp in regexp at /%s/", regcomp_parse);
1506 /* Supposed to be caught earlier. */
1509 if (!regcurly(regcomp_parse)) {
1517 FAIL("?+*{} follows nothing in regexp");
1520 switch (*++regcomp_parse) {
1523 ret = reg_node(SBOL);
1528 ret = reg_node(GPOS);
1529 regseen |= REG_SEEN_GPOS;
1534 ret = reg_node(SEOL);
1539 ret = reg_node((regflags & PMf_LOCALE) ? ALNUML : ALNUM);
1540 *flagp |= HASWIDTH|SIMPLE;
1544 ret = reg_node((regflags & PMf_LOCALE) ? NALNUML : NALNUM);
1545 *flagp |= HASWIDTH|SIMPLE;
1550 ret = reg_node((regflags & PMf_LOCALE) ? BOUNDL : BOUND);
1556 ret = reg_node((regflags & PMf_LOCALE) ? NBOUNDL : NBOUND);
1561 ret = reg_node((regflags & PMf_LOCALE) ? SPACEL : SPACE);
1562 *flagp |= HASWIDTH|SIMPLE;
1566 ret = reg_node((regflags & PMf_LOCALE) ? NSPACEL : NSPACE);
1567 *flagp |= HASWIDTH|SIMPLE;
1571 ret = reg_node(DIGIT);
1572 *flagp |= HASWIDTH|SIMPLE;
1576 ret = reg_node(NDIGIT);
1577 *flagp |= HASWIDTH|SIMPLE;
1590 case '1': case '2': case '3': case '4':
1591 case '5': case '6': case '7': case '8': case '9':
1593 I32 num = atoi(regcomp_parse);
1595 if (num > 9 && num >= regnpar)
1599 ret = reganode((regflags & PMf_FOLD)
1600 ? ((regflags & PMf_LOCALE) ? REFFL : REFF)
1603 while (isDIGIT(*regcomp_parse))
1611 if (regcomp_parse >= regxend)
1612 FAIL("trailing \\ in regexp");
1620 if (regflags & PMf_EXTENDED) {
1621 while (regcomp_parse < regxend && *regcomp_parse != '\n') regcomp_parse++;
1622 if (regcomp_parse < regxend)
1637 ret = reg_node((regflags & PMf_FOLD)
1638 ? ((regflags & PMf_LOCALE) ? EXACTFL : EXACTF)
1640 s = (char *) OPERAND(ret);
1641 regc(0, s++); /* save spot for len */
1642 for (len = 0, p = regcomp_parse - 1;
1643 len < 127 && p < regxend;
1648 if (regflags & PMf_EXTENDED)
1649 p = regwhite(p, regxend);
1699 ender = scan_hex(++p, 2, &numlen);
1704 ender = UCHARAT(p++);
1705 ender = toCTRL(ender);
1707 case '0': case '1': case '2': case '3':case '4':
1708 case '5': case '6': case '7': case '8':case '9':
1710 (isDIGIT(p[1]) && atoi(p) >= regnpar) ) {
1711 ender = scan_oct(p, 3, &numlen);
1721 FAIL("trailing \\ in regexp");
1732 if (regflags & PMf_EXTENDED)
1733 p = regwhite(p, regxend);
1734 if (ISMULT2(p)) { /* Back off on ?+*. */
1746 regcomp_parse = p - 1;
1749 FAIL("internal disaster in regexp");
1755 *OPERAND(ret) = len;
1758 regsize += (len + 2 + sizeof(regnode) - 1) / sizeof(regnode);
1760 regcode += (len + 2 + sizeof(regnode) - 1) / sizeof(regnode);
1770 regwhite(char *p, char *e)
1775 else if (*p == '#') {
1778 } while (p < e && *p != '\n');
1789 register char *opnd, *s;
1791 register I32 lastclass = 1234;
1792 register I32 range = 0;
1793 register regnode *ret;
1797 s = opnd = (char *) OPERAND(regcode);
1798 ret = reg_node(ANYOF);
1799 for (Class = 0; Class < 33; Class++)
1801 if (*regcomp_parse == '^') { /* Complement of range. */
1805 *opnd |= ANYOF_INVERT;
1808 regcode += ANY_SKIP;
1809 if (regflags & PMf_FOLD)
1810 *opnd |= ANYOF_FOLD;
1811 if (regflags & PMf_LOCALE)
1812 *opnd |= ANYOF_LOCALE;
1814 regsize += ANY_SKIP;
1816 if (*regcomp_parse == ']' || *regcomp_parse == '-')
1817 goto skipcond; /* allow 1st char to be ] or - */
1818 while (regcomp_parse < regxend && *regcomp_parse != ']') {
1820 Class = UCHARAT(regcomp_parse++);
1821 if (Class == '[' && regcomp_parse + 1 < regxend &&
1822 /* I smell either [: or [= or [. -- POSIX has been here, right? */
1823 (*regcomp_parse == ':' || *regcomp_parse == '=' || *regcomp_parse == '.')) {
1824 char posixccc = *regcomp_parse;
1825 char* posixccs = regcomp_parse++;
1827 while (regcomp_parse < regxend && *regcomp_parse != posixccc)
1829 if (regcomp_parse == regxend)
1830 /* Grandfather lone [:, [=, [. */
1831 regcomp_parse = posixccs;
1833 regcomp_parse++; /* skip over the posixccc */
1834 if (*regcomp_parse == ']') {
1835 /* Not Implemented Yet.
1836 * (POSIX Extended Character Classes, that is)
1837 * The text between e.g. [: and :] would start
1838 * at posixccs + 1 and stop at regcomp_parse - 2. */
1839 if (dowarn && !SIZE_ONLY)
1840 warn("Character class syntax [%c %c] is reserved for future extensions", posixccc, posixccc);
1841 regcomp_parse++; /* skip over the ending ] */
1845 if (Class == '\\') {
1846 Class = UCHARAT(regcomp_parse++);
1850 if (regflags & PMf_LOCALE)
1851 *opnd |= ANYOF_ALNUML;
1853 for (Class = 0; Class < 256; Class++)
1855 ANYOF_SET(opnd, Class);
1862 if (regflags & PMf_LOCALE)
1863 *opnd |= ANYOF_NALNUML;
1865 for (Class = 0; Class < 256; Class++)
1866 if (!isALNUM(Class))
1867 ANYOF_SET(opnd, Class);
1874 if (regflags & PMf_LOCALE)
1875 *opnd |= ANYOF_SPACEL;
1877 for (Class = 0; Class < 256; Class++)
1879 ANYOF_SET(opnd, Class);
1886 if (regflags & PMf_LOCALE)
1887 *opnd |= ANYOF_NSPACEL;
1889 for (Class = 0; Class < 256; Class++)
1890 if (!isSPACE(Class))
1891 ANYOF_SET(opnd, Class);
1898 for (Class = '0'; Class <= '9'; Class++)
1899 ANYOF_SET(opnd, Class);
1905 for (Class = 0; Class < '0'; Class++)
1906 ANYOF_SET(opnd, Class);
1907 for (Class = '9' + 1; Class < 256; Class++)
1908 ANYOF_SET(opnd, Class);
1934 Class = scan_hex(regcomp_parse, 2, &numlen);
1935 regcomp_parse += numlen;
1938 Class = UCHARAT(regcomp_parse++);
1939 Class = toCTRL(Class);
1941 case '0': case '1': case '2': case '3': case '4':
1942 case '5': case '6': case '7': case '8': case '9':
1943 Class = scan_oct(--regcomp_parse, 3, &numlen);
1944 regcomp_parse += numlen;
1949 if (lastclass > Class)
1950 FAIL("invalid [] range in regexp");
1955 if (*regcomp_parse == '-' && regcomp_parse+1 < regxend &&
1956 regcomp_parse[1] != ']') {
1959 continue; /* do it next time */
1963 for ( ; lastclass <= Class; lastclass++)
1964 ANYOF_SET(opnd, lastclass);
1968 if (*regcomp_parse != ']')
1969 FAIL("unmatched [] in regexp");
1971 /* optimize case-insensitive simple patterns (e.g. /[a-z]/i) */
1972 if (!SIZE_ONLY && (*opnd & (0xFF ^ ANYOF_INVERT)) == ANYOF_FOLD) {
1973 for (Class = 0; Class < 256; ++Class) {
1974 if (ANYOF_TEST(opnd, Class)) {
1975 I32 cf = fold[Class];
1976 ANYOF_SET(opnd, cf);
1979 *opnd &= ~ANYOF_FOLD;
1981 /* optimize inverted simple patterns (e.g. [^a-z]) */
1982 if (!SIZE_ONLY && (*opnd & 0xFF) == ANYOF_INVERT) {
1983 for (Class = 0; Class < 32; ++Class)
1984 opnd[1 + Class] ^= 0xFF;
1993 char* retval = regcomp_parse++;
1996 if (*regcomp_parse == '(' && regcomp_parse[1] == '?' &&
1997 regcomp_parse[2] == '#') {
1998 while (*regcomp_parse && *regcomp_parse != ')')
2003 if (regflags & PMf_EXTENDED) {
2004 if (isSPACE(*regcomp_parse)) {
2008 else if (*regcomp_parse == '#') {
2009 while (*regcomp_parse && *regcomp_parse != '\n')
2020 - reg_node - emit a node
2022 STATIC regnode * /* Location. */
2025 register regnode *ret;
2026 register regnode *ptr;
2030 SIZE_ALIGN(regsize);
2035 NODE_ALIGN_FILL(ret);
2037 FILL_ADVANCE_NODE(ptr, op);
2044 - reganode - emit a node with an argument
2046 STATIC regnode * /* Location. */
2047 reganode(U8 op, U32 arg)
2049 register regnode *ret;
2050 register regnode *ptr;
2054 SIZE_ALIGN(regsize);
2059 NODE_ALIGN_FILL(ret);
2061 FILL_ADVANCE_NODE_ARG(ptr, op, arg);
2068 - regc - emit (if appropriate) a byte of code
2078 - reginsert - insert an operator in front of already-emitted operand
2080 * Means relocating the operand.
2083 reginsert(U8 op, regnode *opnd)
2085 register regnode *src;
2086 register regnode *dst;
2087 register regnode *place;
2088 register int offset = regarglen[(U8)op];
2090 /* (regkind[(U8)op] == CURLY ? EXTRA_STEP_2ARGS : 0); */
2093 regsize += NODE_STEP_REGNODE + offset;
2098 regcode += NODE_STEP_REGNODE + offset;
2101 StructCopy(--src, --dst, regnode);
2103 place = opnd; /* Op node, where operand used to be. */
2104 src = NEXTOPER(place);
2105 FILL_ADVANCE_NODE(place, op);
2106 Zero(src, offset, regnode);
2110 - regtail - set the next-pointer at the end of a node chain of p to val.
2113 regtail(regnode *p, regnode *val)
2115 register regnode *scan;
2116 register regnode *temp;
2117 register I32 offset;
2122 /* Find last node. */
2125 temp = regnext(scan);
2131 if (reg_off_by_arg[OP(scan)]) {
2132 ARG_SET(scan, val - scan);
2134 NEXT_OFF(scan) = val - scan;
2139 - regoptail - regtail on operand of first argument; nop if operandless
2142 regoptail(regnode *p, regnode *val)
2144 /* "Operandless" and "op != BRANCH" are synonymous in practice. */
2145 if (p == NULL || SIZE_ONLY)
2147 if (regkind[(U8)OP(p)] == BRANCH) {
2148 regtail(NEXTOPER(p), val);
2149 } else if ( regkind[(U8)OP(p)] == BRANCHJ) {
2150 regtail(NEXTOPER(NEXTOPER(p)), val);
2156 - regcurly - a little FSA that accepts {\d+,?\d*}
2159 regcurly(register char *s)
2178 dumpuntil(regnode *start, regnode *node, regnode *last, SV* sv, I32 l)
2181 register char op = EXACT; /* Arbitrary non-END op. */
2182 register regnode *next, *onode;
2184 while (op != END && (!last || node < last)) {
2185 /* While that wasn't END last time... */
2191 next = regnext(node);
2193 if (OP(node) == OPTIMIZED)
2196 PerlIO_printf(Perl_debug_log, "%4d%*s%s", node - start,
2197 2*l + 1, "", SvPVX(sv));
2198 if (next == NULL) /* Next ptr. */
2199 PerlIO_printf(Perl_debug_log, "(0)");
2201 PerlIO_printf(Perl_debug_log, "(%d)", next - start);
2202 (void)PerlIO_putc(Perl_debug_log, '\n');
2204 if (regkind[(U8)op] == BRANCHJ) {
2205 register regnode *nnode = (OP(next) == LONGJMP
2208 if (last && nnode > last)
2210 node = dumpuntil(start, NEXTOPER(NEXTOPER(node)), nnode, sv, l + 1);
2211 } else if (regkind[(U8)op] == BRANCH) {
2212 node = dumpuntil(start, NEXTOPER(node), next, sv, l + 1);
2213 } else if ( op == CURLY) { /* `next' might be very big: optimizer */
2214 node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
2215 NEXTOPER(node) + EXTRA_STEP_2ARGS + 1, sv, l + 1);
2216 } else if (regkind[(U8)op] == CURLY && op != CURLYX) {
2217 node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
2219 } else if ( op == PLUS || op == STAR) {
2220 node = dumpuntil(start, NEXTOPER(node), NEXTOPER(node) + 1, sv, l + 1);
2221 } else if (op == ANYOF) {
2222 node = NEXTOPER(node);
2224 } else if (regkind[(U8)op] == EXACT) {
2225 /* Literal string, where present. */
2226 node += ((*OPERAND(node)) + 2 + sizeof(regnode) - 1) / sizeof(regnode);
2227 node = NEXTOPER(node);
2229 node = NEXTOPER(node);
2230 node += regarglen[(U8)op];
2232 if (op == CURLYX || op == OPEN)
2234 else if (op == WHILEM)
2237 #endif /* DEBUGGING */
2242 - regdump - dump a regexp onto Perl_debug_log in vaguely comprehensible form
2248 SV *sv = sv_newmortal();
2250 (void)dumpuntil(r->program, r->program + 1, NULL, sv, 0);
2252 /* Header fields of interest. */
2253 if (r->anchored_substr)
2254 PerlIO_printf(Perl_debug_log, "anchored `%s%s%s'%s at %d ",
2256 SvPVX(r->anchored_substr),
2258 SvTAIL(r->anchored_substr) ? "$" : "",
2259 r->anchored_offset);
2260 if (r->float_substr)
2261 PerlIO_printf(Perl_debug_log, "floating `%s%s%s'%s at %d..%u ",
2263 SvPVX(r->float_substr),
2265 SvTAIL(r->float_substr) ? "$" : "",
2266 r->float_min_offset, r->float_max_offset);
2267 if (r->check_substr)
2268 PerlIO_printf(Perl_debug_log,
2269 r->check_substr == r->float_substr
2270 ? "(checking floating" : "(checking anchored");
2271 if (r->reganch & ROPT_NOSCAN)
2272 PerlIO_printf(Perl_debug_log, " noscan");
2273 if (r->reganch & ROPT_CHECK_ALL)
2274 PerlIO_printf(Perl_debug_log, " isall");
2275 if (r->check_substr)
2276 PerlIO_printf(Perl_debug_log, ") ");
2278 if (r->regstclass) {
2279 regprop(sv, r->regstclass);
2280 PerlIO_printf(Perl_debug_log, "stclass `%s' ", SvPVX(sv));
2282 if (r->reganch & ROPT_ANCH) {
2283 PerlIO_printf(Perl_debug_log, "anchored");
2284 if (r->reganch & ROPT_ANCH_BOL)
2285 PerlIO_printf(Perl_debug_log, "(BOL)");
2286 if (r->reganch & ROPT_ANCH_MBOL)
2287 PerlIO_printf(Perl_debug_log, "(MBOL)");
2288 if (r->reganch & ROPT_ANCH_GPOS)
2289 PerlIO_printf(Perl_debug_log, "(GPOS)");
2290 PerlIO_putc(Perl_debug_log, ' ');
2292 if (r->reganch & ROPT_GPOS_SEEN)
2293 PerlIO_printf(Perl_debug_log, "GPOS ");
2294 if (r->reganch & ROPT_SKIP)
2295 PerlIO_printf(Perl_debug_log, "plus ");
2296 if (r->reganch & ROPT_IMPLICIT)
2297 PerlIO_printf(Perl_debug_log, "implicit ");
2298 PerlIO_printf(Perl_debug_log, "minlen %ld ", (long) r->minlen);
2299 if (r->reganch & ROPT_EVAL_SEEN)
2300 PerlIO_printf(Perl_debug_log, "with eval ");
2301 PerlIO_printf(Perl_debug_log, "\n");
2302 #endif /* DEBUGGING */
2306 - regprop - printable representation of opcode
2309 regprop(SV *sv, regnode *o)
2312 register char *p = 0;
2347 sv_catpvf(sv, "EXACT <%s%s%s>", colors[0], OPERAND(o) + 1, colors[1]);
2350 sv_catpvf(sv, "EXACTF <%s%s%s>", colors[0], OPERAND(o) + 1, colors[1]);
2353 sv_catpvf(sv, "EXACTFL <%s%s%s>", colors[0], OPERAND(o) + 1, colors[1]);
2380 sv_catpvf(sv, "CURLY {%d,%d}", ARG1(o), ARG2(o));
2383 sv_catpvf(sv, "CURLYM[%d] {%d,%d}", o->flags, ARG1(o), ARG2(o));
2386 sv_catpvf(sv, "CURLYN[%d] {%d,%d}", o->flags, ARG1(o), ARG2(o));
2389 sv_catpvf(sv, "CURLYX {%d,%d}", ARG1(o), ARG2(o));
2392 sv_catpvf(sv, "REF%d", ARG(o));
2395 sv_catpvf(sv, "REFF%d", ARG(o));
2398 sv_catpvf(sv, "REFFL%d", ARG(o));
2401 sv_catpvf(sv, "OPEN%d", ARG(o));
2404 sv_catpvf(sv, "CLOSE%d", ARG(o));
2420 sv_catpvf(sv, "UNLESSM[-%d]", o->flags);
2423 sv_catpvf(sv, "IFMATCH[-%d]", o->flags);
2474 sv_catpvf(sv, "GROUPP%d", ARG(o));
2489 FAIL("corrupted regexp opcode");
2493 #endif /* DEBUGGING */
2497 pregfree(struct regexp *r)
2499 if (!r || (--r->refcnt > 0))
2502 Safefree(r->precomp);
2504 Safefree(r->subbase);
2506 if (r->anchored_substr)
2507 SvREFCNT_dec(r->anchored_substr);
2508 if (r->float_substr)
2509 SvREFCNT_dec(r->float_substr);
2510 Safefree(r->substrs);
2513 int n = r->data->count;
2515 switch (r->data->what[n]) {
2517 SvREFCNT_dec((SV*)r->data->data[n]);
2520 op_free((OP_4tree*)r->data->data[n]);
2525 FAIL2("panic: regfree data code '%c'", r->data->what[n]);
2528 Safefree(r->data->what);
2531 Safefree(r->startp);
2537 - regnext - dig the "next" pointer out of a node
2539 * [Note, when REGALIGN is defined there are two places in regmatch()
2540 * that bypass this code for speed.]
2543 regnext(register regnode *p)
2545 register I32 offset;
2550 offset = (reg_off_by_arg[OP(p)] ? ARG(p) : NEXT_OFF(p));
2558 re_croak2(const char* pat1,const char* pat2,...)
2561 STRLEN l1 = strlen(pat1);
2562 STRLEN l2 = strlen(pat2);
2570 Copy(pat1, buf, l1 , char);
2571 Copy(pat2, buf + l1, l2 , char);
2572 buf[l1 + l2 + 1] = '\n';
2573 buf[l1 + l2 + 2] = '\0';
2574 va_start(args, pat2);
2575 message = mess(buf, &args);
2577 l1 = strlen(message);
2580 Copy(message, buf, l1 , char);
2581 buf[l1] = '\0'; /* Overwrite \n */