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.
22 #ifdef PERL_EXT_RE_BUILD
23 /* need to replace pregcomp et al, so enable that */
24 # ifndef PERL_IN_XSUB_RE
25 # define PERL_IN_XSUB_RE
27 /* need access to debugger hooks */
33 #ifdef PERL_IN_XSUB_RE
34 /* We *really* need to overwrite these symbols: */
35 # define Perl_pregcomp my_regcomp
36 # define Perl_regdump my_regdump
37 # define Perl_regprop my_regprop
38 /* *These* symbols are masked to allow static link. */
39 # define Perl_pregfree my_regfree
40 # define Perl_regnext my_regnext
45 * pregcomp and pregexec -- regsub and regerror are not used in perl
47 * Copyright (c) 1986 by University of Toronto.
48 * Written by Henry Spencer. Not derived from licensed software.
50 * Permission is granted to anyone to use this software for any
51 * purpose on any computer system, and to redistribute it freely,
52 * subject to the following restrictions:
54 * 1. The author is not responsible for the consequences of use of
55 * this software, no matter how awful, even if they arise
58 * 2. The origin of this software must not be misrepresented, either
59 * by explicit claim or by omission.
61 * 3. Altered versions must be plainly marked as such, and must not
62 * be misrepresented as being the original software.
65 **** Alterations to Henry's code are...
67 **** Copyright (c) 1991-1997, Larry Wall
69 **** You may distribute under the terms of either the GNU General Public
70 **** License or the Artistic License, as specified in the README file.
73 * Beware that some of this code is subtly aware of the way operator
74 * precedence is structured in regular expressions. Serious changes in
75 * regular-expression syntax might require a total rethink.
80 #ifndef PERL_IN_XSUB_RE
92 # if defined(BUGGY_MSC6)
93 /* MSC 6.00A breaks on op/regexp.t test 85 unless we turn this off */
94 # pragma optimize("a",off)
95 /* But MSC 6.00A is happy with 'w', for aliases only across function calls*/
96 # pragma optimize("w",on )
97 # endif /* BUGGY_MSC6 */
101 #define STATIC static
104 #define ISMULT1(c) ((c) == '*' || (c) == '+' || (c) == '?')
105 #define ISMULT2(s) ((*s) == '*' || (*s) == '+' || (*s) == '?' || \
106 ((*s) == '{' && regcurly(s)))
108 #define PERL_META "^$.[()|?+*\\"
110 #define META "^$.[()|?+*\\"
114 #undef SPSTART /* dratted cpp namespace... */
117 * Flags to be passed up and down.
119 #define WORST 0 /* Worst case. */
120 #define HASWIDTH 0x1 /* Known to match non-null strings. */
121 #define SIMPLE 0x2 /* Simple enough to be STAR/PLUS operand. */
122 #define SPSTART 0x4 /* Starts with * or +. */
123 #define TRYAGAIN 0x8 /* Weeded out a declaration. */
126 * Forward declarations for pregcomp()'s friends.
130 static regnode *reg _((I32, I32 *));
131 static regnode *reganode _((U8, U32));
132 static regnode *regatom _((I32 *));
133 static regnode *regbranch _((I32 *, I32));
134 static void regc _((U8, char *));
135 static regnode *regclass _((void));
136 STATIC I32 regcurly _((char *));
137 static regnode *reg_node _((U8));
138 static regnode *regpiece _((I32 *));
139 static void reginsert _((U8, regnode *));
140 static void regoptail _((regnode *, regnode *));
141 static void regtail _((regnode *, regnode *));
142 static char* regwhite _((char *, char *));
143 static char* nextchar _((void));
144 static void re_croak2 _((const char* pat1,const char* pat2,...)) __attribute__((noreturn));
147 /* Length of a variant. */
156 I32 last_end; /* min value, <0 unless valid. */
159 SV **longest; /* Either &l_fixed, or &l_float. */
163 I32 offset_float_min;
164 I32 offset_float_max;
169 static scan_data_t zero_scan_data = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
171 #define SF_BEFORE_EOL (SF_BEFORE_SEOL|SF_BEFORE_MEOL)
172 #define SF_BEFORE_SEOL 0x1
173 #define SF_BEFORE_MEOL 0x2
174 #define SF_FIX_BEFORE_EOL (SF_FIX_BEFORE_SEOL|SF_FIX_BEFORE_MEOL)
175 #define SF_FL_BEFORE_EOL (SF_FL_BEFORE_SEOL|SF_FL_BEFORE_MEOL)
178 # define SF_FIX_SHIFT_EOL (0+2)
179 # define SF_FL_SHIFT_EOL (0+4)
181 # define SF_FIX_SHIFT_EOL (+2)
182 # define SF_FL_SHIFT_EOL (+4)
185 #define SF_FIX_BEFORE_SEOL (SF_BEFORE_SEOL << SF_FIX_SHIFT_EOL)
186 #define SF_FIX_BEFORE_MEOL (SF_BEFORE_MEOL << SF_FIX_SHIFT_EOL)
188 #define SF_FL_BEFORE_SEOL (SF_BEFORE_SEOL << SF_FL_SHIFT_EOL)
189 #define SF_FL_BEFORE_MEOL (SF_BEFORE_MEOL << SF_FL_SHIFT_EOL) /* 0x20 */
190 #define SF_IS_INF 0x40
191 #define SF_HAS_PAR 0x80
192 #define SF_IN_PAR 0x100
193 #define SF_HAS_EVAL 0x200
194 #define SCF_DO_SUBSTR 0x400
197 scan_commit(scan_data_t *data)
199 STRLEN l = SvCUR(data->last_found);
200 STRLEN old_l = SvCUR(*data->longest);
202 if ((l >= old_l) && ((l > old_l) || (data->flags & SF_BEFORE_EOL))) {
203 sv_setsv(*data->longest, data->last_found);
204 if (*data->longest == data->longest_fixed) {
205 data->offset_fixed = l ? data->last_start_min : data->pos_min;
206 if (data->flags & SF_BEFORE_EOL)
208 |= ((data->flags & SF_BEFORE_EOL) << SF_FIX_SHIFT_EOL);
210 data->flags &= ~SF_FIX_BEFORE_EOL;
212 data->offset_float_min = l ? data->last_start_min : data->pos_min;
213 data->offset_float_max = (l
214 ? data->last_start_max
215 : data->pos_min + data->pos_delta);
216 if (data->flags & SF_BEFORE_EOL)
218 |= ((data->flags & SF_BEFORE_EOL) << SF_FL_SHIFT_EOL);
220 data->flags &= ~SF_FL_BEFORE_EOL;
223 SvCUR_set(data->last_found, 0);
225 data->flags &= ~SF_BEFORE_EOL;
228 /* Stops at toplevel WHILEM as well as at `last'. At end *scanp is set
229 to the position after last scanned or to NULL. */
232 study_chunk(regnode **scanp, I32 *deltap, regnode *last, scan_data_t *data, U32 flags)
233 /* scanp: Start here (read-write). */
234 /* deltap: Write maxlen-minlen here. */
235 /* last: Stop before this one. */
238 I32 min = 0, pars = 0, code;
239 regnode *scan = *scanp, *next;
241 int is_inf = (flags & SCF_DO_SUBSTR) && (data->flags & SF_IS_INF);
242 I32 is_par = OP(scan) == OPEN ? ARG(scan) : 0;
243 scan_data_t data_fake;
245 while (scan && OP(scan) != END && scan < last) {
246 /* Peephole optimizer: */
248 if (regkind[(U8)OP(scan)] == EXACT) {
249 regnode *n = regnext(scan);
252 regnode *stop = scan;
255 next = scan + (*OPERAND(scan) + 2 - 1)/sizeof(regnode) + 2;
256 /* Skip NOTHING, merge EXACT*. */
258 ( regkind[(U8)OP(n)] == NOTHING ||
259 (stringok && (OP(n) == OP(scan))))
261 && NEXT_OFF(scan) + NEXT_OFF(n) < I16_MAX) {
262 if (OP(n) == TAIL || n > next)
264 if (regkind[(U8)OP(n)] == NOTHING) {
265 NEXT_OFF(scan) += NEXT_OFF(n);
266 next = n + NODE_STEP_REGNODE;
273 int oldl = *OPERAND(scan);
274 regnode *nnext = regnext(n);
276 if (oldl + *OPERAND(n) > U8_MAX)
278 NEXT_OFF(scan) += NEXT_OFF(n);
279 *OPERAND(scan) += *OPERAND(n);
280 next = n + (*OPERAND(n) + 2 - 1)/sizeof(regnode) + 2;
281 /* Now we can overwrite *n : */
282 Move(OPERAND(n) + 1, OPERAND(scan) + oldl + 1,
283 *OPERAND(n) + 1, char);
293 n = scan + (*OPERAND(scan) + 2 - 1)/sizeof(regnode) + 2;
295 /* Purify reports a benign UMR here sometimes, because we
296 * don't initialize the OP() slot of a node when that node
297 * is occupied by just the trailing null of the string in
299 if (regkind[(U8)OP(n)] != NOTHING || OP(n) == NOTHING) {
308 if (OP(scan) != CURLYX) {
309 int max = (reg_off_by_arg[OP(scan)] ? I32_MAX : U16_MAX);
310 int off = (reg_off_by_arg[OP(scan)] ? ARG(scan) : NEXT_OFF(scan));
314 /* Skip NOTHING and LONGJMP. */
315 while ((n = regnext(n))
316 && ((regkind[(U8)OP(n)] == NOTHING && (noff = NEXT_OFF(n)))
317 || ((OP(n) == LONGJMP) && (noff = ARG(n))))
320 if (reg_off_by_arg[OP(scan)])
323 NEXT_OFF(scan) = off;
325 if (OP(scan) == BRANCH || OP(scan) == BRANCHJ
326 || OP(scan) == IFTHEN || OP(scan) == SUSPEND) {
327 next = regnext(scan);
330 if (OP(next) == code || code == IFTHEN || code == SUSPEND) {
331 I32 max1 = 0, min1 = I32_MAX, num = 0;
333 if (flags & SCF_DO_SUBSTR)
335 while (OP(scan) == code) {
336 I32 deltanext, minnext;
340 next = regnext(scan);
341 scan = NEXTOPER(scan);
343 scan = NEXTOPER(scan);
344 /* We suppose the run is continuous, last=next...*/
345 minnext = study_chunk(&scan, &deltanext, next,
349 if (max1 < minnext + deltanext)
350 max1 = minnext + deltanext;
351 if (deltanext == I32_MAX)
354 if (data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
356 if (data && (data_fake.flags & SF_HAS_EVAL))
357 data->flags |= SF_HAS_EVAL;
361 if (code == IFTHEN && num < 2) /* Empty ELSE branch */
363 if (flags & SCF_DO_SUBSTR) {
364 data->pos_min += min1;
365 data->pos_delta += max1 - min1;
366 if (max1 != min1 || is_inf)
367 data->longest = &(data->longest_float);
370 delta += max1 - min1;
371 } else if (code == BRANCHJ) /* single branch is optimized. */
372 scan = NEXTOPER(NEXTOPER(scan));
373 else /* single branch is optimized. */
374 scan = NEXTOPER(scan);
376 } else if (OP(scan) == EXACT) {
377 min += *OPERAND(scan);
378 if (flags & SCF_DO_SUBSTR) { /* Update longest substr. */
379 I32 l = *OPERAND(scan);
381 /* The code below prefers earlier match for fixed
382 offset, later match for variable offset. */
383 if (data->last_end == -1) { /* Update the start info. */
384 data->last_start_min = data->pos_min;
385 data->last_start_max = is_inf
386 ? I32_MAX : data->pos_min + data->pos_delta;
388 sv_catpvn(data->last_found, (char *)(OPERAND(scan)+1), l);
389 data->last_end = data->pos_min + l;
390 data->pos_min += l; /* As in the first entry. */
391 data->flags &= ~SF_BEFORE_EOL;
393 } else if (regkind[(U8)OP(scan)] == EXACT) {
394 if (flags & SCF_DO_SUBSTR)
396 min += *OPERAND(scan);
397 if (data && (flags & SCF_DO_SUBSTR))
398 data->pos_min += *OPERAND(scan);
399 } else if (strchr(varies,OP(scan))) {
400 I32 mincount, maxcount, minnext, deltanext, pos_before, fl;
401 regnode *oscan = scan;
403 switch (regkind[(U8)OP(scan)]) {
405 scan = NEXTOPER(scan);
408 if (flags & SCF_DO_SUBSTR) {
409 next = NEXTOPER(scan);
410 if (OP(next) == EXACT) {
412 maxcount = REG_INFTY;
413 next = regnext(scan);
414 scan = NEXTOPER(scan);
418 if (flags & SCF_DO_SUBSTR)
424 scan = regnext(scan);
425 if (flags & SCF_DO_SUBSTR) {
427 data->longest = &(data->longest_float);
429 goto optimize_curly_tail;
431 mincount = ARG1(scan);
432 maxcount = ARG2(scan);
433 next = regnext(scan);
434 scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
436 if (flags & SCF_DO_SUBSTR) {
437 if (mincount == 0) scan_commit(data);
438 pos_before = data->pos_min;
442 data->flags &= ~(SF_HAS_PAR|SF_IN_PAR|SF_HAS_EVAL);
444 data->flags |= SF_IS_INF;
446 /* This will finish on WHILEM, setting scan, or on NULL: */
447 minnext = study_chunk(&scan, &deltanext, last, data,
449 ? (flags & ~SCF_DO_SUBSTR) : flags);
450 if (!scan) /* It was not CURLYX, but CURLY. */
452 if (PL_dowarn && (minnext + deltanext == 0)
453 && !(data->flags & (SF_HAS_PAR|SF_IN_PAR))
454 && maxcount <= 10000) /* Complement check for big count */
455 warn("Strange *+?{} on zero-length expression");
456 min += minnext * mincount;
457 is_inf |= (maxcount == REG_INFTY && (minnext + deltanext) > 0
458 || deltanext == I32_MAX);
459 delta += (minnext + deltanext) * maxcount - minnext * mincount;
461 /* Try powerful optimization CURLYX => CURLYN. */
462 if ( OP(oscan) == CURLYX && data
463 && data->flags & SF_IN_PAR
464 && !(data->flags & SF_HAS_EVAL)
465 && !deltanext && minnext == 1 ) {
466 /* Try to optimize to CURLYN. */
467 regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS;
468 regnode *nxt1 = nxt, *nxt2;
472 if (!strchr(simple,OP(nxt))
473 && !(regkind[(U8)OP(nxt)] == EXACT
474 && *OPERAND(nxt) == 1))
478 if (OP(nxt) != CLOSE)
480 /* Now we know that nxt2 is the only contents: */
481 oscan->flags = ARG(nxt);
483 OP(nxt1) = NOTHING; /* was OPEN. */
485 OP(nxt1 + 1) = OPTIMIZED; /* was count. */
486 NEXT_OFF(nxt1+ 1) = 0; /* just for consistancy. */
487 NEXT_OFF(nxt2) = 0; /* just for consistancy with CURLY. */
488 OP(nxt) = OPTIMIZED; /* was CLOSE. */
489 OP(nxt + 1) = OPTIMIZED; /* was count. */
490 NEXT_OFF(nxt+ 1) = 0; /* just for consistancy. */
495 /* Try optimization CURLYX => CURLYM. */
496 if ( OP(oscan) == CURLYX && data
497 && !(data->flags & SF_HAS_PAR)
498 && !(data->flags & SF_HAS_EVAL)
500 /* XXXX How to optimize if data == 0? */
501 /* Optimize to a simpler form. */
502 regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN */
506 while ( (nxt2 = regnext(nxt)) /* skip over embedded stuff*/
507 && (OP(nxt2) != WHILEM))
509 OP(nxt2) = SUCCEED; /* Whas WHILEM */
510 /* Need to optimize away parenths. */
511 if (data->flags & SF_IN_PAR) {
512 /* Set the parenth number. */
513 regnode *nxt1 = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN*/
515 if (OP(nxt) != CLOSE)
516 FAIL("panic opt close");
517 oscan->flags = ARG(nxt);
518 OP(nxt1) = OPTIMIZED; /* was OPEN. */
519 OP(nxt) = OPTIMIZED; /* was CLOSE. */
521 OP(nxt1 + 1) = OPTIMIZED; /* was count. */
522 OP(nxt + 1) = OPTIMIZED; /* was count. */
523 NEXT_OFF(nxt1 + 1) = 0; /* just for consistancy. */
524 NEXT_OFF(nxt + 1) = 0; /* just for consistancy. */
527 while ( nxt1 && (OP(nxt1) != WHILEM)) {
528 regnode *nnxt = regnext(nxt1);
531 if (reg_off_by_arg[OP(nxt1)])
532 ARG_SET(nxt1, nxt2 - nxt1);
533 else if (nxt2 - nxt1 < U16_MAX)
534 NEXT_OFF(nxt1) = nxt2 - nxt1;
536 OP(nxt) = NOTHING; /* Cannot beautify */
541 /* Optimize again: */
542 study_chunk(&nxt1, &deltanext, nxt, NULL, 0);
546 if (data && fl & (SF_HAS_PAR|SF_IN_PAR))
548 if (flags & SCF_DO_SUBSTR) {
549 SV *last_str = Nullsv;
550 int counted = mincount != 0;
552 if (data->last_end > 0 && mincount != 0) { /* Ends with a string. */
553 I32 b = pos_before >= data->last_start_min
554 ? pos_before : data->last_start_min;
556 char *s = SvPV(data->last_found, l);
558 l -= b - data->last_start_min;
559 /* Get the added string: */
560 last_str = newSVpv(s + b - data->last_start_min, l);
561 if (deltanext == 0 && pos_before == b) {
562 /* What was added is a constant string */
564 SvGROW(last_str, (mincount * l) + 1);
565 repeatcpy(SvPVX(last_str) + l,
566 SvPVX(last_str), l, mincount - 1);
567 SvCUR(last_str) *= mincount;
568 /* Add additional parts. */
569 SvCUR_set(data->last_found,
570 SvCUR(data->last_found) - l);
571 sv_catsv(data->last_found, last_str);
572 data->last_end += l * (mincount - 1);
576 /* It is counted once already... */
577 data->pos_min += minnext * (mincount - counted);
578 data->pos_delta += - counted * deltanext +
579 (minnext + deltanext) * maxcount - minnext * mincount;
580 if (mincount != maxcount) {
582 if (mincount && last_str) {
583 sv_setsv(data->last_found, last_str);
584 data->last_end = data->pos_min;
585 data->last_start_min =
586 data->pos_min - SvCUR(last_str);
587 data->last_start_max = is_inf
589 : data->pos_min + data->pos_delta
592 data->longest = &(data->longest_float);
595 if (data && (fl & SF_HAS_EVAL))
596 data->flags |= SF_HAS_EVAL;
598 if (OP(oscan) != CURLYX) {
599 while (regkind[(U8)OP(next = regnext(oscan))] == NOTHING
601 NEXT_OFF(oscan) += NEXT_OFF(next);
604 default: /* REF only? */
605 if (flags & SCF_DO_SUBSTR) {
607 data->longest = &(data->longest_float);
612 } else if (strchr(simple,OP(scan))) {
613 if (flags & SCF_DO_SUBSTR) {
618 } else if (regkind[(U8)OP(scan)] == EOL && flags & SCF_DO_SUBSTR) {
619 data->flags |= (OP(scan) == MEOL
622 } else if (regkind[(U8)OP(scan)] == BRANCHJ
623 && (scan->flags || data)
624 && (OP(scan) == IFMATCH || OP(scan) == UNLESSM)) {
625 I32 deltanext, minnext;
629 next = regnext(scan);
630 nscan = NEXTOPER(NEXTOPER(scan));
631 minnext = study_chunk(&nscan, &deltanext, last, &data_fake, 0);
634 FAIL("variable length lookbehind not implemented");
635 } else if (minnext > U8_MAX) {
636 FAIL2("lookbehind longer than %d not implemented", U8_MAX);
638 scan->flags = minnext;
640 if (data && data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
642 if (data && (data_fake.flags & SF_HAS_EVAL))
643 data->flags |= SF_HAS_EVAL;
644 } else if (OP(scan) == OPEN) {
646 } else if (OP(scan) == CLOSE && ARG(scan) == is_par) {
647 next = regnext(scan);
649 if ( next && (OP(next) != WHILEM) && next < last)
650 is_par = 0; /* Disable optimization */
651 } else if (OP(scan) == EVAL) {
653 data->flags |= SF_HAS_EVAL;
655 /* Else: zero-length, ignore. */
656 scan = regnext(scan);
661 *deltap = is_inf ? I32_MAX : delta;
662 if (flags & SCF_DO_SUBSTR && is_inf)
663 data->pos_delta = I32_MAX - data->pos_min;
666 if (is_par && pars==1 && data) {
667 data->flags |= SF_IN_PAR;
668 data->flags &= ~SF_HAS_PAR;
669 } else if (pars && data) {
670 data->flags |= SF_HAS_PAR;
671 data->flags &= ~SF_IN_PAR;
677 add_data(I32 n, char *s)
680 if (PL_regcomp_rx->data) {
681 Renewc(PL_regcomp_rx->data,
682 sizeof(*PL_regcomp_rx->data) + sizeof(void*) * (PL_regcomp_rx->data->count + n - 1),
683 char, struct reg_data);
684 Renew(PL_regcomp_rx->data->what, PL_regcomp_rx->data->count + n, U8);
685 PL_regcomp_rx->data->count += n;
687 Newc(1207, PL_regcomp_rx->data, sizeof(*PL_regcomp_rx->data) + sizeof(void*) * (n - 1),
688 char, struct reg_data);
689 New(1208, PL_regcomp_rx->data->what, n, U8);
690 PL_regcomp_rx->data->count = n;
692 Copy(s, PL_regcomp_rx->data->what + PL_regcomp_rx->data->count - n, n, U8);
693 return PL_regcomp_rx->data->count - n;
697 - pregcomp - compile a regular expression into internal code
699 * We can't allocate space until we know how big the compiled form will be,
700 * but we can't compile it (and thus know how big it is) until we've got a
701 * place to put the code. So we cheat: we compile it twice, once with code
702 * generation turned off and size counting turned on, and once "for real".
703 * This also means that we don't allocate space until we are sure that the
704 * thing really will compile successfully, and we never have to move the
705 * code and thus invalidate pointers into it. (Note that it has to be in
706 * one piece because free() must be able to free it all.) [NB: not true in perl]
708 * Beware that the optimization-preparation code in here knows about some
709 * of the structure of the compiled regexp. [I'll say.]
712 pregcomp(char *exp, char *xend, PMOP *pm)
727 FAIL("NULL regexp argument");
729 PL_regprecomp = savepvn(exp, xend - exp);
730 DEBUG_r(PerlIO_printf(Perl_debug_log, "compiling RE `%*s'\n",
731 xend - exp, PL_regprecomp));
732 PL_regflags = pm->op_pmflags;
736 PL_seen_zerolen = *exp == '^' ? -1 : 0;
740 /* First pass: determine size, legality. */
741 PL_regcomp_parse = exp;
746 PL_regcode = &PL_regdummy;
747 regc((U8)MAGIC, (char*)PL_regcode);
748 if (reg(0, &flags) == NULL) {
749 Safefree(PL_regprecomp);
750 PL_regprecomp = Nullch;
753 DEBUG_r(PerlIO_printf(Perl_debug_log, "size %d ", PL_regsize));
758 char *s = PerlEnv_getenv("TERMCAP_COLORS");
762 PL_colors[0] = s = savepv(s);
766 FAIL("Not enough TABs in TERMCAP_COLORS");
775 PerlIO_printf(Perl_debug_log, "%s%s%s%s",
776 PL_colors[0],PL_colors[1],PL_colors[2],PL_colors[3]);
780 /* Small enough for pointer-storage convention?
781 If extralen==0, this means that we will not need long jumps. */
782 if (PL_regsize >= 0x10000L && PL_extralen)
783 PL_regsize += PL_extralen;
787 /* Allocate space and initialize. */
788 Newc(1001, r, sizeof(regexp) + (unsigned)PL_regsize * sizeof(regnode),
791 FAIL("regexp out of space");
793 r->prelen = xend - exp;
794 r->precomp = PL_regprecomp;
795 r->subbeg = r->subbase = NULL;
796 r->nparens = PL_regnpar - 1; /* set early to validate backrefs */
799 /* Second pass: emit code. */
800 PL_regcomp_parse = exp;
804 PL_regcode = r->program;
805 /* Store the count of eval-groups for security checks: */
806 PL_regcode->next_off = ((PL_seen_evals > U16_MAX) ? U16_MAX : PL_seen_evals);
807 regc((U8)MAGIC, (char*) PL_regcode++);
809 if (reg(0, &flags) == NULL)
812 /* Dig out information for optimizations. */
813 r->reganch = pm->op_pmflags & PMf_COMPILETIME;
814 pm->op_pmflags = PL_regflags;
815 r->regstclass = NULL;
816 r->naughty = PL_regnaughty >= 10; /* Probably an expensive pattern. */
817 scan = r->program + 1; /* First BRANCH. */
819 /* XXXX To minimize changes to RE engine we always allocate
820 3-units-long substrs field. */
821 Newz(1004, r->substrs, 1, struct reg_substr_data);
823 if (OP(scan) != BRANCH) { /* Only one top-level choice. */
826 STRLEN longest_float_length, longest_fixed_length;
828 StructCopy(&zero_scan_data, &data, scan_data_t);
830 /* Skip introductions and multiplicators >= 1. */
831 while ((OP(first) == OPEN && (sawopen = 1)) ||
832 (OP(first) == BRANCH && OP(regnext(first)) != BRANCH) ||
833 (OP(first) == PLUS) ||
834 (OP(first) == MINMOD) ||
835 (regkind[(U8)OP(first)] == CURLY && ARG1(first) > 0) ) {
836 if (OP(first) == PLUS)
839 first += regarglen[(U8)OP(first)];
840 first = NEXTOPER(first);
843 /* Starting-point info. */
845 if (OP(first) == EXACT); /* Empty, get anchored substr later. */
846 else if (strchr(simple+2,OP(first)))
847 r->regstclass = first;
848 else if (regkind[(U8)OP(first)] == BOUND ||
849 regkind[(U8)OP(first)] == NBOUND)
850 r->regstclass = first;
851 else if (regkind[(U8)OP(first)] == BOL) {
852 r->reganch |= (OP(first) == MBOL ? ROPT_ANCH_MBOL: ROPT_ANCH_BOL);
853 first = NEXTOPER(first);
856 else if (OP(first) == GPOS) {
857 r->reganch |= ROPT_ANCH_GPOS;
858 first = NEXTOPER(first);
861 else if ((OP(first) == STAR &&
862 regkind[(U8)OP(NEXTOPER(first))] == ANY) &&
863 !(r->reganch & ROPT_ANCH) )
865 /* turn .* into ^.* with an implied $*=1 */
866 r->reganch |= ROPT_ANCH_BOL | ROPT_IMPLICIT;
867 first = NEXTOPER(first);
870 if (sawplus && (!sawopen || !PL_regsawback))
871 r->reganch |= ROPT_SKIP; /* x+ must match 1st of run */
873 /* Scan is after the zeroth branch, first is atomic matcher. */
874 DEBUG_r(PerlIO_printf(Perl_debug_log, "first at %d\n",
877 * If there's something expensive in the r.e., find the
878 * longest literal string that must appear and make it the
879 * regmust. Resolve ties in favor of later strings, since
880 * the regstart check works with the beginning of the r.e.
881 * and avoiding duplication strengthens checking. Not a
882 * strong reason, but sufficient in the absence of others.
883 * [Now we resolve ties in favor of the earlier string if
884 * it happens that c_offset_min has been invalidated, since the
885 * earlier string may buy us something the later one won't.]
889 data.longest_fixed = newSVpv("",0);
890 data.longest_float = newSVpv("",0);
891 data.last_found = newSVpv("",0);
892 data.longest = &(data.longest_fixed);
895 minlen = study_chunk(&first, &fake, scan + PL_regsize, /* Up to end */
896 &data, SCF_DO_SUBSTR);
897 if ( PL_regnpar == 1 && data.longest == &(data.longest_fixed)
898 && data.last_start_min == 0 && data.last_end > 0
900 && (!(PL_regseen & REG_SEEN_GPOS) || (r->reganch & ROPT_ANCH_GPOS)))
901 r->reganch |= ROPT_CHECK_ALL;
903 SvREFCNT_dec(data.last_found);
905 longest_float_length = SvCUR(data.longest_float);
906 if (longest_float_length
907 || (data.flags & SF_FL_BEFORE_EOL
908 && (!(data.flags & SF_FL_BEFORE_MEOL)
909 || (PL_regflags & PMf_MULTILINE)))) {
910 if (SvCUR(data.longest_fixed)
911 && data.offset_fixed == data.offset_float_min)
912 goto remove; /* Like in (a)+. */
914 r->float_substr = data.longest_float;
915 r->float_min_offset = data.offset_float_min;
916 r->float_max_offset = data.offset_float_max;
917 fbm_compile(r->float_substr, 0);
918 BmUSEFUL(r->float_substr) = 100;
919 if (data.flags & SF_FL_BEFORE_EOL /* Cannot have SEOL and MULTI */
920 && (!(data.flags & SF_FL_BEFORE_MEOL)
921 || (PL_regflags & PMf_MULTILINE)))
922 SvTAIL_on(r->float_substr);
925 r->float_substr = Nullsv;
926 SvREFCNT_dec(data.longest_float);
927 longest_float_length = 0;
930 longest_fixed_length = SvCUR(data.longest_fixed);
931 if (longest_fixed_length
932 || (data.flags & SF_FIX_BEFORE_EOL /* Cannot have SEOL and MULTI */
933 && (!(data.flags & SF_FIX_BEFORE_MEOL)
934 || (PL_regflags & PMf_MULTILINE)))) {
935 r->anchored_substr = data.longest_fixed;
936 r->anchored_offset = data.offset_fixed;
937 fbm_compile(r->anchored_substr, 0);
938 BmUSEFUL(r->anchored_substr) = 100;
939 if (data.flags & SF_FIX_BEFORE_EOL /* Cannot have SEOL and MULTI */
940 && (!(data.flags & SF_FIX_BEFORE_MEOL)
941 || (PL_regflags & PMf_MULTILINE)))
942 SvTAIL_on(r->anchored_substr);
944 r->anchored_substr = Nullsv;
945 SvREFCNT_dec(data.longest_fixed);
946 longest_fixed_length = 0;
949 /* A temporary algorithm prefers floated substr to fixed one to dig more info. */
950 if (longest_fixed_length > longest_float_length) {
951 r->check_substr = r->anchored_substr;
952 r->check_offset_min = r->check_offset_max = r->anchored_offset;
953 if (r->reganch & ROPT_ANCH_SINGLE)
954 r->reganch |= ROPT_NOSCAN;
956 r->check_substr = r->float_substr;
957 r->check_offset_min = data.offset_float_min;
958 r->check_offset_max = data.offset_float_max;
961 /* Several toplevels. Best we can is to set minlen. */
964 DEBUG_r(PerlIO_printf(Perl_debug_log, "\n"));
965 scan = r->program + 1;
966 minlen = study_chunk(&scan, &fake, scan + PL_regsize, NULL, 0);
967 r->check_substr = r->anchored_substr = r->float_substr = Nullsv;
971 if (PL_regseen & REG_SEEN_GPOS)
972 r->reganch |= ROPT_GPOS_SEEN;
973 if (PL_regseen & REG_SEEN_LOOKBEHIND)
974 r->reganch |= ROPT_LOOKBEHIND_SEEN;
975 if (PL_regseen & REG_SEEN_EVAL)
976 r->reganch |= ROPT_EVAL_SEEN;
977 Newz(1002, r->startp, PL_regnpar, char*);
978 Newz(1002, r->endp, PL_regnpar, char*);
984 - reg - regular expression, i.e. main body or parenthesized thing
986 * Caller must absorb opening parenthesis.
988 * Combining parenthesis handling with the base level of regular expression
989 * is a trifle forced, but the need to tie the tails of the branches to what
990 * follows makes it hard to avoid.
993 reg(I32 paren, I32 *flagp)
994 /* paren: Parenthesized? 0=top, 1=(, inside: changed to letter. */
997 register regnode *ret; /* Will be the head of the group. */
998 register regnode *br;
999 register regnode *lastbr;
1000 register regnode *ender = 0;
1001 register I32 parno = 0;
1002 I32 flags, oregflags = PL_regflags, have_branch = 0, open = 0;
1005 *flagp = 0; /* Tentatively. */
1007 /* Make an OPEN node, if parenthesized. */
1009 if (*PL_regcomp_parse == '?') {
1010 U16 posflags = 0, negflags = 0;
1011 U16 *flagsp = &posflags;
1014 paren = *PL_regcomp_parse++;
1015 ret = NULL; /* For look-ahead/behind. */
1018 PL_regseen |= REG_SEEN_LOOKBEHIND;
1019 if (*PL_regcomp_parse == '!')
1021 if (*PL_regcomp_parse != '=' && *PL_regcomp_parse != '!')
1032 FAIL2("Sequence (?%c...) not implemented", (int)paren);
1035 while (*PL_regcomp_parse && *PL_regcomp_parse != ')')
1037 if (*PL_regcomp_parse != ')')
1038 FAIL("Sequence (?#... not terminated");
1045 I32 count = 1, n = 0;
1047 char *s = PL_regcomp_parse;
1049 OP_4tree *sop, *rop;
1052 PL_regseen |= REG_SEEN_EVAL;
1053 while (count && (c = *PL_regcomp_parse)) {
1054 if (c == '\\' && PL_regcomp_parse[1])
1062 if (*PL_regcomp_parse != ')')
1063 FAIL("Sequence (?{...}) not terminated or not {}-balanced");
1067 if (PL_regcomp_parse - 1 - s)
1068 sv = newSVpv(s, PL_regcomp_parse - 1 - s);
1070 sv = newSVpv("", 0);
1072 rop = sv_compile_2op(sv, &sop, "re", &av);
1074 n = add_data(3, "nso");
1075 PL_regcomp_rx->data->data[n] = (void*)rop;
1076 PL_regcomp_rx->data->data[n+1] = (void*)av;
1077 PL_regcomp_rx->data->data[n+2] = (void*)sop;
1079 } else { /* First pass */
1080 if (PL_reginterp_cnt < ++PL_seen_evals && PL_curcop != &PL_compiling)
1081 /* No compiled RE interpolated, has runtime
1082 components ===> unsafe. */
1083 FAIL("Eval-group not allowed at runtime, use re 'eval'");
1085 FAIL("Eval-group in insecure regular expression");
1089 return reganode(EVAL, n);
1093 if (PL_regcomp_parse[0] == '?') {
1094 if (PL_regcomp_parse[1] == '=' || PL_regcomp_parse[1] == '!'
1095 || PL_regcomp_parse[1] == '<'
1096 || PL_regcomp_parse[1] == '{') { /* Lookahead or eval. */
1099 ret = reg_node(LOGICAL);
1100 regtail(ret, reg(1, &flag));
1103 } else if (PL_regcomp_parse[0] >= '1' && PL_regcomp_parse[0] <= '9' ) {
1104 parno = atoi(PL_regcomp_parse++);
1106 while (isDIGIT(*PL_regcomp_parse))
1108 ret = reganode(GROUPP, parno);
1109 if ((c = *nextchar()) != ')')
1110 FAIL2("Switch (?(number%c not recognized", c);
1112 regtail(ret, reganode(IFTHEN, 0));
1113 br = regbranch(&flags, 1);
1115 br = reganode(LONGJMP, 0);
1117 regtail(br, reganode(LONGJMP, 0));
1120 lastbr = reganode(IFTHEN, 0); /* Fake one for optimizer. */
1121 regbranch(&flags, 1);
1122 regtail(ret, lastbr);
1127 FAIL("Switch (?(condition)... contains too many branches");
1128 ender = reg_node(TAIL);
1131 regtail(lastbr, ender);
1132 regtail(NEXTOPER(NEXTOPER(lastbr)), ender);
1134 regtail(ret, ender);
1137 FAIL2("Unknown condition for (?(%.2s", PL_regcomp_parse);
1141 FAIL("Sequence (? incomplete");
1146 while (*PL_regcomp_parse && strchr("iogcmsx", *PL_regcomp_parse)) {
1147 if (*PL_regcomp_parse != 'o')
1148 pmflag(flagsp, *PL_regcomp_parse);
1151 if (*PL_regcomp_parse == '-') {
1156 PL_regflags |= posflags;
1157 PL_regflags &= ~negflags;
1158 if (*PL_regcomp_parse == ':') {
1164 if (*PL_regcomp_parse != ')')
1165 FAIL2("Sequence (?%c...) not recognized", *PL_regcomp_parse);
1174 ret = reganode(OPEN, parno);
1180 /* Pick up the branches, linking them together. */
1181 br = regbranch(&flags, 1);
1184 if (*PL_regcomp_parse == '|') {
1185 if (!SIZE_ONLY && PL_extralen) {
1186 reginsert(BRANCHJ, br);
1188 reginsert(BRANCH, br);
1191 PL_extralen += 1; /* For BRANCHJ-BRANCH. */
1192 } else if (paren == ':') {
1193 *flagp |= flags&SIMPLE;
1195 if (open) { /* Starts with OPEN. */
1196 regtail(ret, br); /* OPEN -> first. */
1197 } else if (paren != '?') /* Not Conditional */
1201 *flagp |= flags&SPSTART;
1203 while (*PL_regcomp_parse == '|') {
1204 if (!SIZE_ONLY && PL_extralen) {
1205 ender = reganode(LONGJMP,0);
1206 regtail(NEXTOPER(NEXTOPER(lastbr)), ender); /* Append to the previous. */
1209 PL_extralen += 2; /* Account for LONGJMP. */
1211 br = regbranch(&flags, 0);
1214 regtail(lastbr, br); /* BRANCH -> BRANCH. */
1218 *flagp |= flags&SPSTART;
1221 if (have_branch || paren != ':') {
1222 /* Make a closing node, and hook it on the end. */
1225 ender = reg_node(TAIL);
1228 ender = reganode(CLOSE, parno);
1234 *flagp &= ~HASWIDTH;
1237 ender = reg_node(SUCCEED);
1240 ender = reg_node(END);
1243 regtail(lastbr, ender);
1246 /* Hook the tails of the branches to the closing node. */
1247 for (br = ret; br != NULL; br = regnext(br)) {
1248 regoptail(br, ender);
1255 static char parens[] = "=!<,>";
1257 if (paren && (p = strchr(parens, paren))) {
1258 int node = ((p - parens) % 2) ? UNLESSM : IFMATCH;
1259 int flag = (p - parens) > 1;
1262 node = SUSPEND, flag = 0;
1263 reginsert(node,ret);
1265 regtail(ret, reg_node(TAIL));
1269 /* Check for proper termination. */
1270 if (paren && (PL_regcomp_parse >= PL_regxend || *nextchar() != ')')) {
1271 FAIL("unmatched () in regexp");
1272 } else if (!paren && PL_regcomp_parse < PL_regxend) {
1273 if (*PL_regcomp_parse == ')') {
1274 FAIL("unmatched () in regexp");
1276 FAIL("junk on end of regexp"); /* "Can't happen". */
1280 PL_regflags = oregflags;
1287 - regbranch - one alternative of an | operator
1289 * Implements the concatenation operator.
1292 regbranch(I32 *flagp, I32 first)
1295 register regnode *ret;
1296 register regnode *chain = NULL;
1297 register regnode *latest;
1298 I32 flags = 0, c = 0;
1303 if (!SIZE_ONLY && PL_extralen)
1304 ret = reganode(BRANCHJ,0);
1306 ret = reg_node(BRANCH);
1309 if (!first && SIZE_ONLY)
1310 PL_extralen += 1; /* BRANCHJ */
1312 *flagp = WORST; /* Tentatively. */
1316 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != '|' && *PL_regcomp_parse != ')') {
1318 latest = regpiece(&flags);
1319 if (latest == NULL) {
1320 if (flags & TRYAGAIN)
1323 } else if (ret == NULL)
1325 *flagp |= flags&HASWIDTH;
1326 if (chain == NULL) /* First piece. */
1327 *flagp |= flags&SPSTART;
1330 regtail(chain, latest);
1335 if (chain == NULL) { /* Loop ran zero times. */
1336 chain = reg_node(NOTHING);
1341 *flagp |= flags&SIMPLE;
1348 - regpiece - something followed by possible [*+?]
1350 * Note that the branching code sequences used for ? and the general cases
1351 * of * and + are somewhat optimized: they use the same NOTHING node as
1352 * both the endmarker for their branch list and the body of the last branch.
1353 * It might seem that this node could be dispensed with entirely, but the
1354 * endmarker role is not redundant.
1357 regpiece(I32 *flagp)
1360 register regnode *ret;
1362 register char *next;
1364 char *origparse = PL_regcomp_parse;
1367 I32 max = REG_INFTY;
1369 ret = regatom(&flags);
1371 if (flags & TRYAGAIN)
1376 op = *PL_regcomp_parse;
1378 if (op == '{' && regcurly(PL_regcomp_parse)) {
1379 next = PL_regcomp_parse + 1;
1381 while (isDIGIT(*next) || *next == ',') {
1390 if (*next == '}') { /* got one */
1394 min = atoi(PL_regcomp_parse);
1398 maxpos = PL_regcomp_parse;
1400 if (!max && *maxpos != '0')
1401 max = REG_INFTY; /* meaning "infinity" */
1402 else if (max >= REG_INFTY)
1403 FAIL2("Quantifier in {,} bigger than %d", REG_INFTY - 1);
1404 PL_regcomp_parse = next;
1408 if ((flags&SIMPLE)) {
1409 PL_regnaughty += 2 + PL_regnaughty / 2;
1410 reginsert(CURLY, ret);
1413 PL_regnaughty += 4 + PL_regnaughty; /* compound interest */
1414 regtail(ret, reg_node(WHILEM));
1415 if (!SIZE_ONLY && PL_extralen) {
1416 reginsert(LONGJMP,ret);
1417 reginsert(NOTHING,ret);
1418 NEXT_OFF(ret) = 3; /* Go over LONGJMP. */
1420 reginsert(CURLYX,ret);
1421 if (!SIZE_ONLY && PL_extralen)
1422 NEXT_OFF(ret) = 3; /* Go over NOTHING to LONGJMP. */
1423 regtail(ret, reg_node(NOTHING));
1433 if (max && max < min)
1434 FAIL("Can't do {n,m} with n > m");
1449 #if 0 /* Now runtime fix should be reliable. */
1450 if (!(flags&HASWIDTH) && op != '?')
1451 FAIL("regexp *+ operand could be empty");
1456 *flagp = (op != '+') ? (WORST|SPSTART|HASWIDTH) : (WORST|HASWIDTH);
1458 if (op == '*' && (flags&SIMPLE)) {
1459 reginsert(STAR, ret);
1463 else if (op == '*') {
1466 } else if (op == '+' && (flags&SIMPLE)) {
1467 reginsert(PLUS, ret);
1471 else if (op == '+') {
1474 } else if (op == '?') {
1479 if (PL_dowarn && !SIZE_ONLY && !(flags&HASWIDTH) && max > 10000) {
1480 warn("%.*s matches null string many times",
1481 PL_regcomp_parse - origparse, origparse);
1484 if (*PL_regcomp_parse == '?') {
1486 reginsert(MINMOD, ret);
1487 regtail(ret, ret + NODE_STEP_REGNODE);
1489 if (ISMULT2(PL_regcomp_parse))
1490 FAIL("nested *?+ in regexp");
1496 - regatom - the lowest level
1498 * Optimization: gobbles an entire sequence of ordinary characters so that
1499 * it can turn them into a single node, which is smaller to store and
1500 * faster to run. Backslashed characters are exceptions, each becoming a
1501 * separate node; the code is simpler that way and it's not worth fixing.
1503 * [Yes, it is worth fixing, some scripts can run twice the speed.]
1509 register regnode *ret = 0;
1512 *flagp = WORST; /* Tentatively. */
1515 switch (*PL_regcomp_parse) {
1519 if (PL_regflags & PMf_MULTILINE)
1520 ret = reg_node(MBOL);
1521 else if (PL_regflags & PMf_SINGLELINE)
1522 ret = reg_node(SBOL);
1524 ret = reg_node(BOL);
1527 if (PL_regcomp_parse[1])
1530 if (PL_regflags & PMf_MULTILINE)
1531 ret = reg_node(MEOL);
1532 else if (PL_regflags & PMf_SINGLELINE)
1533 ret = reg_node(SEOL);
1535 ret = reg_node(EOL);
1539 if (PL_regflags & PMf_SINGLELINE)
1540 ret = reg_node(SANY);
1542 ret = reg_node(ANY);
1544 *flagp |= HASWIDTH|SIMPLE;
1549 *flagp |= HASWIDTH|SIMPLE;
1553 ret = reg(1, &flags);
1555 if (flags & TRYAGAIN)
1559 *flagp |= flags&(HASWIDTH|SPSTART|SIMPLE);
1563 if (flags & TRYAGAIN) {
1567 FAIL2("internal urp in regexp at /%s/", PL_regcomp_parse);
1568 /* Supposed to be caught earlier. */
1571 if (!regcurly(PL_regcomp_parse)) {
1579 FAIL("?+*{} follows nothing in regexp");
1582 switch (*++PL_regcomp_parse) {
1585 ret = reg_node(SBOL);
1590 ret = reg_node(GPOS);
1591 PL_regseen |= REG_SEEN_GPOS;
1596 ret = reg_node(SEOL);
1601 ret = reg_node(EOS);
1603 PL_seen_zerolen++; /* Do not optimize RE away */
1607 ret = reg_node((PL_regflags & PMf_LOCALE) ? ALNUML : ALNUM);
1608 *flagp |= HASWIDTH|SIMPLE;
1612 ret = reg_node((PL_regflags & PMf_LOCALE) ? NALNUML : NALNUM);
1613 *flagp |= HASWIDTH|SIMPLE;
1618 ret = reg_node((PL_regflags & PMf_LOCALE) ? BOUNDL : BOUND);
1624 ret = reg_node((PL_regflags & PMf_LOCALE) ? NBOUNDL : NBOUND);
1629 ret = reg_node((PL_regflags & PMf_LOCALE) ? SPACEL : SPACE);
1630 *flagp |= HASWIDTH|SIMPLE;
1634 ret = reg_node((PL_regflags & PMf_LOCALE) ? NSPACEL : NSPACE);
1635 *flagp |= HASWIDTH|SIMPLE;
1639 ret = reg_node(DIGIT);
1640 *flagp |= HASWIDTH|SIMPLE;
1644 ret = reg_node(NDIGIT);
1645 *flagp |= HASWIDTH|SIMPLE;
1658 case '1': case '2': case '3': case '4':
1659 case '5': case '6': case '7': case '8': case '9':
1661 I32 num = atoi(PL_regcomp_parse);
1663 if (num > 9 && num >= PL_regnpar)
1666 if (!SIZE_ONLY && num > PL_regcomp_rx->nparens)
1667 FAIL("reference to nonexistent group");
1669 ret = reganode((PL_regflags & PMf_FOLD)
1670 ? ((PL_regflags & PMf_LOCALE) ? REFFL : REFF)
1673 while (isDIGIT(*PL_regcomp_parse))
1681 if (PL_regcomp_parse >= PL_regxend)
1682 FAIL("trailing \\ in regexp");
1690 if (PL_regflags & PMf_EXTENDED) {
1691 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != '\n') PL_regcomp_parse++;
1692 if (PL_regcomp_parse < PL_regxend)
1707 ret = reg_node((PL_regflags & PMf_FOLD)
1708 ? ((PL_regflags & PMf_LOCALE) ? EXACTFL : EXACTF)
1710 s = (char *) OPERAND(ret);
1711 regc(0, s++); /* save spot for len */
1712 for (len = 0, p = PL_regcomp_parse - 1;
1713 len < 127 && p < PL_regxend;
1718 if (PL_regflags & PMf_EXTENDED)
1719 p = regwhite(p, PL_regxend);
1770 ender = scan_hex(++p, 2, &numlen);
1775 ender = UCHARAT(p++);
1776 ender = toCTRL(ender);
1778 case '0': case '1': case '2': case '3':case '4':
1779 case '5': case '6': case '7': case '8':case '9':
1781 (isDIGIT(p[1]) && atoi(p) >= PL_regnpar) ) {
1782 ender = scan_oct(p, 3, &numlen);
1791 if (p >= PL_regxend)
1792 FAIL("trailing \\ in regexp");
1803 if (PL_regflags & PMf_EXTENDED)
1804 p = regwhite(p, PL_regxend);
1805 if (ISMULT2(p)) { /* Back off on ?+*. */
1817 PL_regcomp_parse = p - 1;
1820 FAIL("internal disaster in regexp");
1826 *OPERAND(ret) = len;
1829 PL_regsize += (len + 2 + sizeof(regnode) - 1) / sizeof(regnode);
1831 PL_regcode += (len + 2 + sizeof(regnode) - 1) / sizeof(regnode);
1841 regwhite(char *p, char *e)
1846 else if (*p == '#') {
1849 } while (p < e && *p != '\n');
1861 register char *opnd, *s;
1863 register I32 lastclass = 1234;
1864 register I32 range = 0;
1865 register regnode *ret;
1869 s = opnd = (char *) OPERAND(PL_regcode);
1870 ret = reg_node(ANYOF);
1871 for (Class = 0; Class < 33; Class++)
1873 if (*PL_regcomp_parse == '^') { /* Complement of range. */
1877 *opnd |= ANYOF_INVERT;
1880 PL_regcode += ANY_SKIP;
1881 if (PL_regflags & PMf_FOLD)
1882 *opnd |= ANYOF_FOLD;
1883 if (PL_regflags & PMf_LOCALE)
1884 *opnd |= ANYOF_LOCALE;
1886 PL_regsize += ANY_SKIP;
1888 if (*PL_regcomp_parse == ']' || *PL_regcomp_parse == '-')
1889 goto skipcond; /* allow 1st char to be ] or - */
1890 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != ']') {
1892 Class = UCHARAT(PL_regcomp_parse++);
1893 if (Class == '[' && PL_regcomp_parse + 1 < PL_regxend &&
1894 /* I smell either [: or [= or [. -- POSIX has been here, right? */
1895 (*PL_regcomp_parse == ':' || *PL_regcomp_parse == '=' || *PL_regcomp_parse == '.')) {
1896 char posixccc = *PL_regcomp_parse;
1897 char* posixccs = PL_regcomp_parse++;
1899 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != posixccc)
1901 if (PL_regcomp_parse == PL_regxend)
1902 /* Grandfather lone [:, [=, [. */
1903 PL_regcomp_parse = posixccs;
1905 PL_regcomp_parse++; /* skip over the posixccc */
1906 if (*PL_regcomp_parse == ']') {
1907 /* Not Implemented Yet.
1908 * (POSIX Extended Character Classes, that is)
1909 * The text between e.g. [: and :] would start
1910 * at posixccs + 1 and stop at regcomp_parse - 2. */
1911 if (PL_dowarn && !SIZE_ONLY)
1912 warn("Character class syntax [%c %c] is reserved for future extensions", posixccc, posixccc);
1913 PL_regcomp_parse++; /* skip over the ending ] */
1917 if (Class == '\\') {
1918 Class = UCHARAT(PL_regcomp_parse++);
1922 if (PL_regflags & PMf_LOCALE)
1923 *opnd |= ANYOF_ALNUML;
1925 for (Class = 0; Class < 256; Class++)
1927 ANYOF_SET(opnd, Class);
1934 if (PL_regflags & PMf_LOCALE)
1935 *opnd |= ANYOF_NALNUML;
1937 for (Class = 0; Class < 256; Class++)
1938 if (!isALNUM(Class))
1939 ANYOF_SET(opnd, Class);
1946 if (PL_regflags & PMf_LOCALE)
1947 *opnd |= ANYOF_SPACEL;
1949 for (Class = 0; Class < 256; Class++)
1951 ANYOF_SET(opnd, Class);
1958 if (PL_regflags & PMf_LOCALE)
1959 *opnd |= ANYOF_NSPACEL;
1961 for (Class = 0; Class < 256; Class++)
1962 if (!isSPACE(Class))
1963 ANYOF_SET(opnd, Class);
1970 for (Class = '0'; Class <= '9'; Class++)
1971 ANYOF_SET(opnd, Class);
1977 for (Class = 0; Class < '0'; Class++)
1978 ANYOF_SET(opnd, Class);
1979 for (Class = '9' + 1; Class < 256; Class++)
1980 ANYOF_SET(opnd, Class);
2006 Class = scan_hex(PL_regcomp_parse, 2, &numlen);
2007 PL_regcomp_parse += numlen;
2010 Class = UCHARAT(PL_regcomp_parse++);
2011 Class = toCTRL(Class);
2013 case '0': case '1': case '2': case '3': case '4':
2014 case '5': case '6': case '7': case '8': case '9':
2015 Class = scan_oct(--PL_regcomp_parse, 3, &numlen);
2016 PL_regcomp_parse += numlen;
2021 if (lastclass > Class)
2022 FAIL("invalid [] range in regexp");
2027 if (*PL_regcomp_parse == '-' && PL_regcomp_parse+1 < PL_regxend &&
2028 PL_regcomp_parse[1] != ']') {
2031 continue; /* do it next time */
2035 for ( ; lastclass <= Class; lastclass++)
2036 ANYOF_SET(opnd, lastclass);
2040 if (*PL_regcomp_parse != ']')
2041 FAIL("unmatched [] in regexp");
2043 /* optimize case-insensitive simple patterns (e.g. /[a-z]/i) */
2044 if (!SIZE_ONLY && (*opnd & (0xFF ^ ANYOF_INVERT)) == ANYOF_FOLD) {
2045 for (Class = 0; Class < 256; ++Class) {
2046 if (ANYOF_TEST(opnd, Class)) {
2047 I32 cf = fold[Class];
2048 ANYOF_SET(opnd, cf);
2051 *opnd &= ~ANYOF_FOLD;
2053 /* optimize inverted simple patterns (e.g. [^a-z]) */
2054 if (!SIZE_ONLY && (*opnd & 0xFF) == ANYOF_INVERT) {
2055 for (Class = 0; Class < 32; ++Class)
2056 opnd[1 + Class] ^= 0xFF;
2066 char* retval = PL_regcomp_parse++;
2069 if (*PL_regcomp_parse == '(' && PL_regcomp_parse[1] == '?' &&
2070 PL_regcomp_parse[2] == '#') {
2071 while (*PL_regcomp_parse && *PL_regcomp_parse != ')')
2076 if (PL_regflags & PMf_EXTENDED) {
2077 if (isSPACE(*PL_regcomp_parse)) {
2081 else if (*PL_regcomp_parse == '#') {
2082 while (*PL_regcomp_parse && *PL_regcomp_parse != '\n')
2093 - reg_node - emit a node
2095 STATIC regnode * /* Location. */
2099 register regnode *ret;
2100 register regnode *ptr;
2104 SIZE_ALIGN(PL_regsize);
2109 NODE_ALIGN_FILL(ret);
2111 FILL_ADVANCE_NODE(ptr, op);
2118 - reganode - emit a node with an argument
2120 STATIC regnode * /* Location. */
2121 reganode(U8 op, U32 arg)
2124 register regnode *ret;
2125 register regnode *ptr;
2129 SIZE_ALIGN(PL_regsize);
2134 NODE_ALIGN_FILL(ret);
2136 FILL_ADVANCE_NODE_ARG(ptr, op, arg);
2143 - regc - emit (if appropriate) a byte of code
2154 - reginsert - insert an operator in front of already-emitted operand
2156 * Means relocating the operand.
2159 reginsert(U8 op, regnode *opnd)
2162 register regnode *src;
2163 register regnode *dst;
2164 register regnode *place;
2165 register int offset = regarglen[(U8)op];
2167 /* (regkind[(U8)op] == CURLY ? EXTRA_STEP_2ARGS : 0); */
2170 PL_regsize += NODE_STEP_REGNODE + offset;
2175 PL_regcode += NODE_STEP_REGNODE + offset;
2178 StructCopy(--src, --dst, regnode);
2180 place = opnd; /* Op node, where operand used to be. */
2181 src = NEXTOPER(place);
2182 FILL_ADVANCE_NODE(place, op);
2183 Zero(src, offset, regnode);
2187 - regtail - set the next-pointer at the end of a node chain of p to val.
2190 regtail(regnode *p, regnode *val)
2193 register regnode *scan;
2194 register regnode *temp;
2195 register I32 offset;
2200 /* Find last node. */
2203 temp = regnext(scan);
2209 if (reg_off_by_arg[OP(scan)]) {
2210 ARG_SET(scan, val - scan);
2212 NEXT_OFF(scan) = val - scan;
2217 - regoptail - regtail on operand of first argument; nop if operandless
2220 regoptail(regnode *p, regnode *val)
2223 /* "Operandless" and "op != BRANCH" are synonymous in practice. */
2224 if (p == NULL || SIZE_ONLY)
2226 if (regkind[(U8)OP(p)] == BRANCH) {
2227 regtail(NEXTOPER(p), val);
2228 } else if ( regkind[(U8)OP(p)] == BRANCHJ) {
2229 regtail(NEXTOPER(NEXTOPER(p)), val);
2235 - regcurly - a little FSA that accepts {\d+,?\d*}
2238 regcurly(register char *s)
2257 dumpuntil(regnode *start, regnode *node, regnode *last, SV* sv, I32 l)
2260 register char op = EXACT; /* Arbitrary non-END op. */
2261 register regnode *next, *onode;
2263 while (op != END && (!last || node < last)) {
2264 /* While that wasn't END last time... */
2270 next = regnext(node);
2272 if (OP(node) == OPTIMIZED)
2275 PerlIO_printf(Perl_debug_log, "%4d:%*s%s", node - start,
2276 2*l + 1, "", SvPVX(sv));
2277 if (next == NULL) /* Next ptr. */
2278 PerlIO_printf(Perl_debug_log, "(0)");
2280 PerlIO_printf(Perl_debug_log, "(%d)", next - start);
2281 (void)PerlIO_putc(Perl_debug_log, '\n');
2283 if (regkind[(U8)op] == BRANCHJ) {
2284 register regnode *nnode = (OP(next) == LONGJMP
2287 if (last && nnode > last)
2289 node = dumpuntil(start, NEXTOPER(NEXTOPER(node)), nnode, sv, l + 1);
2290 } else if (regkind[(U8)op] == BRANCH) {
2291 node = dumpuntil(start, NEXTOPER(node), next, sv, l + 1);
2292 } else if ( op == CURLY) { /* `next' might be very big: optimizer */
2293 node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
2294 NEXTOPER(node) + EXTRA_STEP_2ARGS + 1, sv, l + 1);
2295 } else if (regkind[(U8)op] == CURLY && op != CURLYX) {
2296 node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
2298 } else if ( op == PLUS || op == STAR) {
2299 node = dumpuntil(start, NEXTOPER(node), NEXTOPER(node) + 1, sv, l + 1);
2300 } else if (op == ANYOF) {
2301 node = NEXTOPER(node);
2303 } else if (regkind[(U8)op] == EXACT) {
2304 /* Literal string, where present. */
2305 node += ((*OPERAND(node)) + 2 + sizeof(regnode) - 1) / sizeof(regnode);
2306 node = NEXTOPER(node);
2308 node = NEXTOPER(node);
2309 node += regarglen[(U8)op];
2311 if (op == CURLYX || op == OPEN)
2313 else if (op == WHILEM)
2316 #endif /* DEBUGGING */
2321 - regdump - dump a regexp onto Perl_debug_log in vaguely comprehensible form
2328 SV *sv = sv_newmortal();
2330 (void)dumpuntil(r->program, r->program + 1, NULL, sv, 0);
2332 /* Header fields of interest. */
2333 if (r->anchored_substr)
2334 PerlIO_printf(Perl_debug_log, "anchored `%s%s%s'%s at %d ",
2336 SvPVX(r->anchored_substr),
2338 SvTAIL(r->anchored_substr) ? "$" : "",
2339 r->anchored_offset);
2340 if (r->float_substr)
2341 PerlIO_printf(Perl_debug_log, "floating `%s%s%s'%s at %d..%u ",
2343 SvPVX(r->float_substr),
2345 SvTAIL(r->float_substr) ? "$" : "",
2346 r->float_min_offset, r->float_max_offset);
2347 if (r->check_substr)
2348 PerlIO_printf(Perl_debug_log,
2349 r->check_substr == r->float_substr
2350 ? "(checking floating" : "(checking anchored");
2351 if (r->reganch & ROPT_NOSCAN)
2352 PerlIO_printf(Perl_debug_log, " noscan");
2353 if (r->reganch & ROPT_CHECK_ALL)
2354 PerlIO_printf(Perl_debug_log, " isall");
2355 if (r->check_substr)
2356 PerlIO_printf(Perl_debug_log, ") ");
2358 if (r->regstclass) {
2359 regprop(sv, r->regstclass);
2360 PerlIO_printf(Perl_debug_log, "stclass `%s' ", SvPVX(sv));
2362 if (r->reganch & ROPT_ANCH) {
2363 PerlIO_printf(Perl_debug_log, "anchored");
2364 if (r->reganch & ROPT_ANCH_BOL)
2365 PerlIO_printf(Perl_debug_log, "(BOL)");
2366 if (r->reganch & ROPT_ANCH_MBOL)
2367 PerlIO_printf(Perl_debug_log, "(MBOL)");
2368 if (r->reganch & ROPT_ANCH_GPOS)
2369 PerlIO_printf(Perl_debug_log, "(GPOS)");
2370 PerlIO_putc(Perl_debug_log, ' ');
2372 if (r->reganch & ROPT_GPOS_SEEN)
2373 PerlIO_printf(Perl_debug_log, "GPOS ");
2374 if (r->reganch & ROPT_SKIP)
2375 PerlIO_printf(Perl_debug_log, "plus ");
2376 if (r->reganch & ROPT_IMPLICIT)
2377 PerlIO_printf(Perl_debug_log, "implicit ");
2378 PerlIO_printf(Perl_debug_log, "minlen %ld ", (long) r->minlen);
2379 if (r->reganch & ROPT_EVAL_SEEN)
2380 PerlIO_printf(Perl_debug_log, "with eval ");
2381 PerlIO_printf(Perl_debug_log, "\n");
2382 #endif /* DEBUGGING */
2386 - regprop - printable representation of opcode
2389 regprop(SV *sv, regnode *o)
2393 register char *p = 0;
2395 sv_setpvn(sv, "", 0);
2431 sv_catpvf(sv, "EXACT <%s%s%s>", PL_colors[0], OPERAND(o) + 1, PL_colors[1]);
2434 sv_catpvf(sv, "EXACTF <%s%s%s>", PL_colors[0], OPERAND(o) + 1, PL_colors[1]);
2437 sv_catpvf(sv, "EXACTFL <%s%s%s>", PL_colors[0], OPERAND(o) + 1, PL_colors[1]);
2464 sv_catpvf(sv, "CURLY {%d,%d}", ARG1(o), ARG2(o));
2467 sv_catpvf(sv, "CURLYM[%d] {%d,%d}", o->flags, ARG1(o), ARG2(o));
2470 sv_catpvf(sv, "CURLYN[%d] {%d,%d}", o->flags, ARG1(o), ARG2(o));
2473 sv_catpvf(sv, "CURLYX {%d,%d}", ARG1(o), ARG2(o));
2476 sv_catpvf(sv, "REF%d", ARG(o));
2479 sv_catpvf(sv, "REFF%d", ARG(o));
2482 sv_catpvf(sv, "REFFL%d", ARG(o));
2485 sv_catpvf(sv, "OPEN%d", ARG(o));
2488 sv_catpvf(sv, "CLOSE%d", ARG(o));
2504 sv_catpvf(sv, "UNLESSM[-%d]", o->flags);
2507 sv_catpvf(sv, "IFMATCH[-%d]", o->flags);
2558 sv_catpvf(sv, "GROUPP%d", ARG(o));
2573 FAIL("corrupted regexp opcode");
2577 #endif /* DEBUGGING */
2581 pregfree(struct regexp *r)
2584 if (!r || (--r->refcnt > 0))
2587 Safefree(r->precomp);
2589 Safefree(r->subbase);
2591 if (r->anchored_substr)
2592 SvREFCNT_dec(r->anchored_substr);
2593 if (r->float_substr)
2594 SvREFCNT_dec(r->float_substr);
2595 Safefree(r->substrs);
2598 int n = r->data->count;
2600 switch (r->data->what[n]) {
2602 SvREFCNT_dec((SV*)r->data->data[n]);
2605 op_free((OP_4tree*)r->data->data[n]);
2610 FAIL2("panic: regfree data code '%c'", r->data->what[n]);
2613 Safefree(r->data->what);
2616 Safefree(r->startp);
2622 - regnext - dig the "next" pointer out of a node
2624 * [Note, when REGALIGN is defined there are two places in regmatch()
2625 * that bypass this code for speed.]
2628 regnext(register regnode *p)
2631 register I32 offset;
2633 if (p == &PL_regdummy)
2636 offset = (reg_off_by_arg[OP(p)] ? ARG(p) : NEXT_OFF(p));
2644 re_croak2(const char* pat1,const char* pat2,...)
2647 STRLEN l1 = strlen(pat1);
2648 STRLEN l2 = strlen(pat2);
2656 Copy(pat1, buf, l1 , char);
2657 Copy(pat2, buf + l1, l2 , char);
2658 buf[l1 + l2] = '\n';
2659 buf[l1 + l2 + 1] = '\0';
2660 va_start(args, pat2);
2661 message = mess(buf, &args);
2663 l1 = strlen(message);
2666 Copy(message, buf, l1 , char);
2667 buf[l1] = '\0'; /* Overwrite \n */