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
41 # define save_re_context my_save_re_context
46 * pregcomp and pregexec -- regsub and regerror are not used in perl
48 * Copyright (c) 1986 by University of Toronto.
49 * Written by Henry Spencer. Not derived from licensed software.
51 * Permission is granted to anyone to use this software for any
52 * purpose on any computer system, and to redistribute it freely,
53 * subject to the following restrictions:
55 * 1. The author is not responsible for the consequences of use of
56 * this software, no matter how awful, even if they arise
59 * 2. The origin of this software must not be misrepresented, either
60 * by explicit claim or by omission.
62 * 3. Altered versions must be plainly marked as such, and must not
63 * be misrepresented as being the original software.
66 **** Alterations to Henry's code are...
68 **** Copyright (c) 1991-1998, Larry Wall
70 **** You may distribute under the terms of either the GNU General Public
71 **** License or the Artistic License, as specified in the README file.
74 * Beware that some of this code is subtly aware of the way operator
75 * precedence is structured in regular expressions. Serious changes in
76 * regular-expression syntax might require a total rethink.
81 #ifndef PERL_IN_XSUB_RE
93 # if defined(BUGGY_MSC6)
94 /* MSC 6.00A breaks on op/regexp.t test 85 unless we turn this off */
95 # pragma optimize("a",off)
96 /* But MSC 6.00A is happy with 'w', for aliases only across function calls*/
97 # pragma optimize("w",on )
98 # endif /* BUGGY_MSC6 */
102 #define STATIC static
105 #define ISMULT1(c) ((c) == '*' || (c) == '+' || (c) == '?')
106 #define ISMULT2(s) ((*s) == '*' || (*s) == '+' || (*s) == '?' || \
107 ((*s) == '{' && regcurly(s)))
109 #define PERL_META "^$.[()|?+*\\"
111 #define META "^$.[()|?+*\\"
115 #undef SPSTART /* dratted cpp namespace... */
118 * Flags to be passed up and down.
120 #define WORST 0 /* Worst case. */
121 #define HASWIDTH 0x1 /* Known to match non-null strings. */
122 #define SIMPLE 0x2 /* Simple enough to be STAR/PLUS operand. */
123 #define SPSTART 0x4 /* Starts with * or +. */
124 #define TRYAGAIN 0x8 /* Weeded out a declaration. */
127 * Forward declarations for pregcomp()'s friends.
131 static regnode *reg _((I32, I32 *));
132 static regnode *reganode _((U8, U32));
133 static regnode *regatom _((I32 *));
134 static regnode *regbranch _((I32 *, I32));
135 static void regc _((U8, char *));
136 static void reguni _((UV, char *, I32*));
137 static regnode *regclass _((void));
138 static regnode *regclassutf8 _((void));
139 STATIC I32 regcurly _((char *));
140 static regnode *reg_node _((U8));
141 static regnode *regpiece _((I32 *));
142 static void reginsert _((U8, regnode *));
143 static void regoptail _((regnode *, regnode *));
144 static void regtail _((regnode *, regnode *));
145 static char* regwhite _((char *, char *));
146 static char* nextchar _((void));
147 static void re_croak2 _((const char* pat1,const char* pat2,...)) __attribute__((noreturn));
150 /* Length of a variant. */
156 I32 pos_min; /* CC */
157 I32 pos_delta; /* CC */
159 I32 last_end; /* min value, <0 unless valid. */
160 I32 last_start_min; /* CC */
161 I32 last_start_max; /* CC */
162 SV **longest; /* Either &l_fixed, or &l_float. */
164 I32 offset_fixed; /* CC */
166 I32 offset_float_min; /* CC */
167 I32 offset_float_max; /* CC */
172 static scan_data_t zero_scan_data = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
174 #define SF_BEFORE_EOL (SF_BEFORE_SEOL|SF_BEFORE_MEOL)
175 #define SF_BEFORE_SEOL 0x1
176 #define SF_BEFORE_MEOL 0x2
177 #define SF_FIX_BEFORE_EOL (SF_FIX_BEFORE_SEOL|SF_FIX_BEFORE_MEOL)
178 #define SF_FL_BEFORE_EOL (SF_FL_BEFORE_SEOL|SF_FL_BEFORE_MEOL)
181 # define SF_FIX_SHIFT_EOL (0+2)
182 # define SF_FL_SHIFT_EOL (0+4)
184 # define SF_FIX_SHIFT_EOL (+2)
185 # define SF_FL_SHIFT_EOL (+4)
188 #define SF_FIX_BEFORE_SEOL (SF_BEFORE_SEOL << SF_FIX_SHIFT_EOL)
189 #define SF_FIX_BEFORE_MEOL (SF_BEFORE_MEOL << SF_FIX_SHIFT_EOL)
191 #define SF_FL_BEFORE_SEOL (SF_BEFORE_SEOL << SF_FL_SHIFT_EOL)
192 #define SF_FL_BEFORE_MEOL (SF_BEFORE_MEOL << SF_FL_SHIFT_EOL) /* 0x20 */
193 #define SF_IS_INF 0x40
194 #define SF_HAS_PAR 0x80
195 #define SF_IN_PAR 0x100
196 #define SF_HAS_EVAL 0x200
197 #define SCF_DO_SUBSTR 0x400
200 #define UTF (PL_reg_flags & RF_utf8)
201 #define LOC (PL_regflags & PMf_LOCALE)
202 #define FOLD (PL_regflags & PMf_FOLD)
204 #define CHR_SVLEN(sv) (UTF ? sv_len_utf8(sv) : SvCUR(sv))
205 #define CHR_DIST(a,b) (UTF ? utf8_distance(a,b) : a - b)
208 scan_commit(scan_data_t *data)
211 STRLEN l = CHR_SVLEN(data->last_found);
212 STRLEN old_l = CHR_SVLEN(*data->longest);
214 if ((l >= old_l) && ((l > old_l) || (data->flags & SF_BEFORE_EOL))) {
215 sv_setsv(*data->longest, data->last_found);
216 if (*data->longest == data->longest_fixed) {
217 data->offset_fixed = l ? data->last_start_min : data->pos_min;
218 if (data->flags & SF_BEFORE_EOL)
220 |= ((data->flags & SF_BEFORE_EOL) << SF_FIX_SHIFT_EOL);
222 data->flags &= ~SF_FIX_BEFORE_EOL;
225 data->offset_float_min = l ? data->last_start_min : data->pos_min;
226 data->offset_float_max = (l
227 ? data->last_start_max
228 : data->pos_min + data->pos_delta);
229 if (data->flags & SF_BEFORE_EOL)
231 |= ((data->flags & SF_BEFORE_EOL) << SF_FL_SHIFT_EOL);
233 data->flags &= ~SF_FL_BEFORE_EOL;
236 SvCUR_set(data->last_found, 0);
238 data->flags &= ~SF_BEFORE_EOL;
241 /* Stops at toplevel WHILEM as well as at `last'. At end *scanp is set
242 to the position after last scanned or to NULL. */
245 study_chunk(regnode **scanp, I32 *deltap, regnode *last, scan_data_t *data, U32 flags)
246 /* scanp: Start here (read-write). */
247 /* deltap: Write maxlen-minlen here. */
248 /* last: Stop before this one. */
251 I32 min = 0, pars = 0, code;
252 regnode *scan = *scanp, *next;
254 int is_inf = (flags & SCF_DO_SUBSTR) && (data->flags & SF_IS_INF);
255 int is_inf_internal = 0; /* The studied chunk is infinite */
256 I32 is_par = OP(scan) == OPEN ? ARG(scan) : 0;
257 scan_data_t data_fake;
259 while (scan && OP(scan) != END && scan < last) {
260 /* Peephole optimizer: */
262 if (regkind[(U8)OP(scan)] == EXACT) {
263 regnode *n = regnext(scan);
266 regnode *stop = scan;
269 next = scan + (*OPERAND(scan) + 2 - 1)/sizeof(regnode) + 2;
270 /* Skip NOTHING, merge EXACT*. */
272 ( regkind[(U8)OP(n)] == NOTHING ||
273 (stringok && (OP(n) == OP(scan))))
275 && NEXT_OFF(scan) + NEXT_OFF(n) < I16_MAX) {
276 if (OP(n) == TAIL || n > next)
278 if (regkind[(U8)OP(n)] == NOTHING) {
279 NEXT_OFF(scan) += NEXT_OFF(n);
280 next = n + NODE_STEP_REGNODE;
288 int oldl = *OPERAND(scan);
289 regnode *nnext = regnext(n);
291 if (oldl + *OPERAND(n) > U8_MAX)
293 NEXT_OFF(scan) += NEXT_OFF(n);
294 *OPERAND(scan) += *OPERAND(n);
295 next = n + (*OPERAND(n) + 2 - 1)/sizeof(regnode) + 2;
296 /* Now we can overwrite *n : */
297 Move(OPERAND(n) + 1, OPERAND(scan) + oldl + 1,
298 *OPERAND(n) + 1, char);
308 n = scan + (*OPERAND(scan) + 2 - 1)/sizeof(regnode) + 2;
310 /* Purify reports a benign UMR here sometimes, because we
311 * don't initialize the OP() slot of a node when that node
312 * is occupied by just the trailing null of the string in
314 if (regkind[(U8)OP(n)] != NOTHING || OP(n) == NOTHING) {
323 if (OP(scan) != CURLYX) {
324 int max = (reg_off_by_arg[OP(scan)]
326 /* I32 may be smaller than U16 on CRAYs! */
327 : (I32_MAX < U16_MAX ? I32_MAX : U16_MAX));
328 int off = (reg_off_by_arg[OP(scan)] ? ARG(scan) : NEXT_OFF(scan));
332 /* Skip NOTHING and LONGJMP. */
333 while ((n = regnext(n))
334 && ((regkind[(U8)OP(n)] == NOTHING && (noff = NEXT_OFF(n)))
335 || ((OP(n) == LONGJMP) && (noff = ARG(n))))
338 if (reg_off_by_arg[OP(scan)])
341 NEXT_OFF(scan) = off;
343 if (OP(scan) == BRANCH || OP(scan) == BRANCHJ
344 || OP(scan) == IFTHEN || OP(scan) == SUSPEND) {
345 next = regnext(scan);
348 if (OP(next) == code || code == IFTHEN || code == SUSPEND) {
349 I32 max1 = 0, min1 = I32_MAX, num = 0;
351 if (flags & SCF_DO_SUBSTR)
353 while (OP(scan) == code) {
354 I32 deltanext, minnext;
358 next = regnext(scan);
359 scan = NEXTOPER(scan);
361 scan = NEXTOPER(scan);
362 /* We suppose the run is continuous, last=next...*/
363 minnext = study_chunk(&scan, &deltanext, next,
367 if (max1 < minnext + deltanext)
368 max1 = minnext + deltanext;
369 if (deltanext == I32_MAX)
370 is_inf = is_inf_internal = 1;
372 if (data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
374 if (data && (data_fake.flags & SF_HAS_EVAL))
375 data->flags |= SF_HAS_EVAL;
379 if (code == IFTHEN && num < 2) /* Empty ELSE branch */
381 if (flags & SCF_DO_SUBSTR) {
382 data->pos_min += min1;
383 data->pos_delta += max1 - min1;
384 if (max1 != min1 || is_inf)
385 data->longest = &(data->longest_float);
388 delta += max1 - min1;
390 else if (code == BRANCHJ) /* single branch is optimized. */
391 scan = NEXTOPER(NEXTOPER(scan));
392 else /* single branch is optimized. */
393 scan = NEXTOPER(scan);
396 else if (OP(scan) == EXACT) {
397 I32 l = *OPERAND(scan);
399 unsigned char *s = (unsigned char *)(OPERAND(scan)+1);
400 unsigned char *e = s + l;
409 if (flags & SCF_DO_SUBSTR) { /* Update longest substr. */
410 /* The code below prefers earlier match for fixed
411 offset, later match for variable offset. */
412 if (data->last_end == -1) { /* Update the start info. */
413 data->last_start_min = data->pos_min;
414 data->last_start_max = is_inf
415 ? I32_MAX : data->pos_min + data->pos_delta;
417 sv_catpvn(data->last_found, (char *)(OPERAND(scan)+1), *OPERAND(scan));
418 data->last_end = data->pos_min + l;
419 data->pos_min += l; /* As in the first entry. */
420 data->flags &= ~SF_BEFORE_EOL;
423 else if (regkind[(U8)OP(scan)] == EXACT) {
424 I32 l = *OPERAND(scan);
425 if (flags & SCF_DO_SUBSTR)
428 unsigned char *s = (unsigned char *)(OPERAND(scan)+1);
429 unsigned char *e = s + l;
438 if (data && (flags & SCF_DO_SUBSTR))
441 else if (strchr(varies,OP(scan))) {
442 I32 mincount, maxcount, minnext, deltanext, pos_before, fl;
443 regnode *oscan = scan;
445 switch (regkind[(U8)OP(scan)]) {
447 scan = NEXTOPER(scan);
450 if (flags & SCF_DO_SUBSTR) {
451 next = NEXTOPER(scan);
452 if (OP(next) == EXACT) {
454 maxcount = REG_INFTY;
455 next = regnext(scan);
456 scan = NEXTOPER(scan);
460 if (flags & SCF_DO_SUBSTR)
465 is_inf = is_inf_internal = 1;
466 scan = regnext(scan);
467 if (flags & SCF_DO_SUBSTR) {
469 data->longest = &(data->longest_float);
471 goto optimize_curly_tail;
473 mincount = ARG1(scan);
474 maxcount = ARG2(scan);
475 next = regnext(scan);
476 scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
478 if (flags & SCF_DO_SUBSTR) {
479 if (mincount == 0) scan_commit(data);
480 pos_before = data->pos_min;
484 data->flags &= ~(SF_HAS_PAR|SF_IN_PAR|SF_HAS_EVAL);
486 data->flags |= SF_IS_INF;
488 /* This will finish on WHILEM, setting scan, or on NULL: */
489 minnext = study_chunk(&scan, &deltanext, last, data,
491 ? (flags & ~SCF_DO_SUBSTR) : flags);
492 if (!scan) /* It was not CURLYX, but CURLY. */
494 if (ckWARN(WARN_UNSAFE) && (minnext + deltanext == 0)
495 && !(data->flags & (SF_HAS_PAR|SF_IN_PAR))
496 && maxcount <= 10000) /* Complement check for big count */
497 warner(WARN_UNSAFE, "Strange *+?{} on zero-length expression");
498 min += minnext * mincount;
499 is_inf_internal |= (maxcount == REG_INFTY
500 && (minnext + deltanext) > 0
501 || deltanext == I32_MAX);
502 is_inf |= is_inf_internal;
503 delta += (minnext + deltanext) * maxcount - minnext * mincount;
505 /* Try powerful optimization CURLYX => CURLYN. */
506 if ( OP(oscan) == CURLYX && data
507 && data->flags & SF_IN_PAR
508 && !(data->flags & SF_HAS_EVAL)
509 && !deltanext && minnext == 1 ) {
510 /* Try to optimize to CURLYN. */
511 regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS;
512 regnode *nxt1 = nxt, *nxt2;
516 if (!strchr(simple,OP(nxt))
517 && !(regkind[(U8)OP(nxt)] == EXACT
518 && *OPERAND(nxt) == 1))
522 if (OP(nxt) != CLOSE)
524 /* Now we know that nxt2 is the only contents: */
525 oscan->flags = ARG(nxt);
527 OP(nxt1) = NOTHING; /* was OPEN. */
529 OP(nxt1 + 1) = OPTIMIZED; /* was count. */
530 NEXT_OFF(nxt1+ 1) = 0; /* just for consistancy. */
531 NEXT_OFF(nxt2) = 0; /* just for consistancy with CURLY. */
532 OP(nxt) = OPTIMIZED; /* was CLOSE. */
533 OP(nxt + 1) = OPTIMIZED; /* was count. */
534 NEXT_OFF(nxt+ 1) = 0; /* just for consistancy. */
539 /* Try optimization CURLYX => CURLYM. */
540 if ( OP(oscan) == CURLYX && data
541 && !(data->flags & SF_HAS_PAR)
542 && !(data->flags & SF_HAS_EVAL)
544 /* XXXX How to optimize if data == 0? */
545 /* Optimize to a simpler form. */
546 regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN */
550 while ( (nxt2 = regnext(nxt)) /* skip over embedded stuff*/
551 && (OP(nxt2) != WHILEM))
553 OP(nxt2) = SUCCEED; /* Whas WHILEM */
554 /* Need to optimize away parenths. */
555 if (data->flags & SF_IN_PAR) {
556 /* Set the parenth number. */
557 regnode *nxt1 = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN*/
559 if (OP(nxt) != CLOSE)
560 FAIL("panic opt close");
561 oscan->flags = ARG(nxt);
562 OP(nxt1) = OPTIMIZED; /* was OPEN. */
563 OP(nxt) = OPTIMIZED; /* was CLOSE. */
565 OP(nxt1 + 1) = OPTIMIZED; /* was count. */
566 OP(nxt + 1) = OPTIMIZED; /* was count. */
567 NEXT_OFF(nxt1 + 1) = 0; /* just for consistancy. */
568 NEXT_OFF(nxt + 1) = 0; /* just for consistancy. */
571 while ( nxt1 && (OP(nxt1) != WHILEM)) {
572 regnode *nnxt = regnext(nxt1);
575 if (reg_off_by_arg[OP(nxt1)])
576 ARG_SET(nxt1, nxt2 - nxt1);
577 else if (nxt2 - nxt1 < U16_MAX)
578 NEXT_OFF(nxt1) = nxt2 - nxt1;
580 OP(nxt) = NOTHING; /* Cannot beautify */
585 /* Optimize again: */
586 study_chunk(&nxt1, &deltanext, nxt, NULL, 0);
591 if (data && fl & (SF_HAS_PAR|SF_IN_PAR))
593 if (flags & SCF_DO_SUBSTR) {
594 SV *last_str = Nullsv;
595 int counted = mincount != 0;
597 if (data->last_end > 0 && mincount != 0) { /* Ends with a string. */
598 I32 b = pos_before >= data->last_start_min
599 ? pos_before : data->last_start_min;
601 char *s = SvPV(data->last_found, l);
602 I32 old = b - data->last_start_min;
605 old = utf8_hop((U8*)s, old) - (U8*)s;
608 /* Get the added string: */
609 last_str = newSVpv(s + old, l);
610 if (deltanext == 0 && pos_before == b) {
611 /* What was added is a constant string */
613 SvGROW(last_str, (mincount * l) + 1);
614 repeatcpy(SvPVX(last_str) + l,
615 SvPVX(last_str), l, mincount - 1);
616 SvCUR(last_str) *= mincount;
617 /* Add additional parts. */
618 SvCUR_set(data->last_found,
619 SvCUR(data->last_found) - l);
620 sv_catsv(data->last_found, last_str);
621 data->last_end += l * (mincount - 1);
625 /* It is counted once already... */
626 data->pos_min += minnext * (mincount - counted);
627 data->pos_delta += - counted * deltanext +
628 (minnext + deltanext) * maxcount - minnext * mincount;
629 if (mincount != maxcount) {
631 if (mincount && last_str) {
632 sv_setsv(data->last_found, last_str);
633 data->last_end = data->pos_min;
634 data->last_start_min =
635 data->pos_min - CHR_SVLEN(last_str);
636 data->last_start_max = is_inf
638 : data->pos_min + data->pos_delta
639 - CHR_SVLEN(last_str);
641 data->longest = &(data->longest_float);
643 SvREFCNT_dec(last_str);
645 if (data && (fl & SF_HAS_EVAL))
646 data->flags |= SF_HAS_EVAL;
648 if (OP(oscan) != CURLYX) {
649 while (regkind[(U8)OP(next = regnext(oscan))] == NOTHING
651 NEXT_OFF(oscan) += NEXT_OFF(next);
654 default: /* REF only? */
655 if (flags & SCF_DO_SUBSTR) {
657 data->longest = &(data->longest_float);
659 is_inf = is_inf_internal = 1;
663 else if (strchr(simple,OP(scan)) || regkind[(U8)OP(scan)] == ANYUTF8) {
664 if (flags & SCF_DO_SUBSTR) {
670 else if (regkind[(U8)OP(scan)] == EOL && flags & SCF_DO_SUBSTR) {
671 data->flags |= (OP(scan) == MEOL
675 else if (regkind[(U8)OP(scan)] == BRANCHJ
676 && (scan->flags || data)
677 && (OP(scan) == IFMATCH || OP(scan) == UNLESSM)) {
678 I32 deltanext, minnext;
682 next = regnext(scan);
683 nscan = NEXTOPER(NEXTOPER(scan));
684 minnext = study_chunk(&nscan, &deltanext, last, &data_fake, 0);
687 FAIL("variable length lookbehind not implemented");
689 else if (minnext > U8_MAX) {
690 FAIL2("lookbehind longer than %d not implemented", U8_MAX);
692 scan->flags = minnext;
694 if (data && data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
696 if (data && (data_fake.flags & SF_HAS_EVAL))
697 data->flags |= SF_HAS_EVAL;
699 else if (OP(scan) == OPEN) {
702 else if (OP(scan) == CLOSE && ARG(scan) == is_par) {
703 next = regnext(scan);
705 if ( next && (OP(next) != WHILEM) && next < last)
706 is_par = 0; /* Disable optimization */
708 else if (OP(scan) == EVAL) {
710 data->flags |= SF_HAS_EVAL;
712 else if (OP(scan) == LOGICAL && scan->flags == 2) { /* Embedded */
713 if (flags & SCF_DO_SUBSTR) {
715 data->longest = &(data->longest_float);
717 is_inf = is_inf_internal = 1;
719 /* Else: zero-length, ignore. */
720 scan = regnext(scan);
725 *deltap = is_inf_internal ? I32_MAX : delta;
726 if (flags & SCF_DO_SUBSTR && is_inf)
727 data->pos_delta = I32_MAX - data->pos_min;
730 if (is_par && pars==1 && data) {
731 data->flags |= SF_IN_PAR;
732 data->flags &= ~SF_HAS_PAR;
734 else if (pars && data) {
735 data->flags |= SF_HAS_PAR;
736 data->flags &= ~SF_IN_PAR;
742 add_data(I32 n, char *s)
745 if (PL_regcomp_rx->data) {
746 Renewc(PL_regcomp_rx->data,
747 sizeof(*PL_regcomp_rx->data) + sizeof(void*) * (PL_regcomp_rx->data->count + n - 1),
748 char, struct reg_data);
749 Renew(PL_regcomp_rx->data->what, PL_regcomp_rx->data->count + n, U8);
750 PL_regcomp_rx->data->count += n;
753 Newc(1207, PL_regcomp_rx->data, sizeof(*PL_regcomp_rx->data) + sizeof(void*) * (n - 1),
754 char, struct reg_data);
755 New(1208, PL_regcomp_rx->data->what, n, U8);
756 PL_regcomp_rx->data->count = n;
758 Copy(s, PL_regcomp_rx->data->what + PL_regcomp_rx->data->count - n, n, U8);
759 return PL_regcomp_rx->data->count - n;
763 - pregcomp - compile a regular expression into internal code
765 * We can't allocate space until we know how big the compiled form will be,
766 * but we can't compile it (and thus know how big it is) until we've got a
767 * place to put the code. So we cheat: we compile it twice, once with code
768 * generation turned off and size counting turned on, and once "for real".
769 * This also means that we don't allocate space until we are sure that the
770 * thing really will compile successfully, and we never have to move the
771 * code and thus invalidate pointers into it. (Note that it has to be in
772 * one piece because free() must be able to free it all.) [NB: not true in perl]
774 * Beware that the optimization-preparation code in here knows about some
775 * of the structure of the compiled regexp. [I'll say.]
778 pregcomp(char *exp, char *xend, PMOP *pm)
793 FAIL("NULL regexp argument");
795 if (PL_curcop == &compiling ? (PL_hints & HINT_UTF8) : IN_UTF8)
796 PL_reg_flags |= RF_utf8;
800 PL_regprecomp = savepvn(exp, xend - exp);
801 DEBUG_r(PerlIO_printf(Perl_debug_log, "compiling RE `%*s'\n",
802 xend - exp, PL_regprecomp));
803 PL_regflags = pm->op_pmflags;
807 PL_seen_zerolen = *exp == '^' ? -1 : 0;
811 /* First pass: determine size, legality. */
812 PL_regcomp_parse = exp;
817 PL_regcode = &PL_regdummy;
818 regc((U8)MAGIC, (char*)PL_regcode);
819 if (reg(0, &flags) == NULL) {
820 Safefree(PL_regprecomp);
821 PL_regprecomp = Nullch;
824 DEBUG_r(PerlIO_printf(Perl_debug_log, "size %d ", PL_regsize));
829 char *s = PerlEnv_getenv("TERMCAP_COLORS");
833 PL_colors[0] = s = savepv(s);
837 FAIL("Not enough TABs in TERMCAP_COLORS");
847 PerlIO_printf(Perl_debug_log, "%s%s%s%s",
848 PL_colors[0],PL_colors[1],PL_colors[2],PL_colors[3]);
852 /* Small enough for pointer-storage convention?
853 If extralen==0, this means that we will not need long jumps. */
854 if (PL_regsize >= 0x10000L && PL_extralen)
855 PL_regsize += PL_extralen;
859 /* Allocate space and initialize. */
860 Newc(1001, r, sizeof(regexp) + (unsigned)PL_regsize * sizeof(regnode),
863 FAIL("regexp out of space");
865 r->prelen = xend - exp;
866 r->precomp = PL_regprecomp;
867 r->subbeg = r->subbase = NULL;
868 r->nparens = PL_regnpar - 1; /* set early to validate backrefs */
871 /* Second pass: emit code. */
872 PL_regcomp_parse = exp;
876 PL_regcode = r->program;
877 /* Store the count of eval-groups for security checks: */
878 PL_regcode->next_off = ((PL_seen_evals > U16_MAX) ? U16_MAX : PL_seen_evals);
879 regc((U8)MAGIC, (char*) PL_regcode++);
881 if (reg(0, &flags) == NULL)
884 /* Dig out information for optimizations. */
885 r->reganch = pm->op_pmflags & PMf_COMPILETIME;
886 pm->op_pmflags = PL_regflags;
888 r->reganch |= ROPT_UTF8;
889 r->regstclass = NULL;
890 if (PL_regnaughty >= 10) /* Probably an expensive pattern. */
891 r->reganch |= ROPT_NAUGHTY;
892 scan = r->program + 1; /* First BRANCH. */
894 /* XXXX To minimize changes to RE engine we always allocate
895 3-units-long substrs field. */
896 Newz(1004, r->substrs, 1, struct reg_substr_data);
898 if (OP(scan) != BRANCH) { /* Only one top-level choice. */
901 STRLEN longest_float_length, longest_fixed_length;
903 StructCopy(&zero_scan_data, &data, scan_data_t);
905 /* Skip introductions and multiplicators >= 1. */
906 while ((OP(first) == OPEN && (sawopen = 1)) ||
907 (OP(first) == BRANCH && OP(regnext(first)) != BRANCH) ||
908 (OP(first) == PLUS) ||
909 (OP(first) == MINMOD) ||
910 (regkind[(U8)OP(first)] == CURLY && ARG1(first) > 0) ) {
911 if (OP(first) == PLUS)
914 first += regarglen[(U8)OP(first)];
915 first = NEXTOPER(first);
918 /* Starting-point info. */
920 if (OP(first) == EXACT); /* Empty, get anchored substr later. */
921 else if (strchr(simple+4,OP(first)))
922 r->regstclass = first;
923 else if (regkind[(U8)OP(first)] == BOUND ||
924 regkind[(U8)OP(first)] == NBOUND)
925 r->regstclass = first;
926 else if (regkind[(U8)OP(first)] == BOL) {
927 r->reganch |= (OP(first) == MBOL ? ROPT_ANCH_MBOL: ROPT_ANCH_BOL);
928 first = NEXTOPER(first);
931 else if (OP(first) == GPOS) {
932 r->reganch |= ROPT_ANCH_GPOS;
933 first = NEXTOPER(first);
936 else if ((OP(first) == STAR &&
937 regkind[(U8)OP(NEXTOPER(first))] == ANY) &&
938 !(r->reganch & ROPT_ANCH) )
940 /* turn .* into ^.* with an implied $*=1 */
941 r->reganch |= ROPT_ANCH_BOL | ROPT_IMPLICIT;
942 first = NEXTOPER(first);
945 if (sawplus && (!sawopen || !PL_regsawback))
946 r->reganch |= ROPT_SKIP; /* x+ must match 1st of run */
948 /* Scan is after the zeroth branch, first is atomic matcher. */
949 DEBUG_r(PerlIO_printf(Perl_debug_log, "first at %d\n",
952 * If there's something expensive in the r.e., find the
953 * longest literal string that must appear and make it the
954 * regmust. Resolve ties in favor of later strings, since
955 * the regstart check works with the beginning of the r.e.
956 * and avoiding duplication strengthens checking. Not a
957 * strong reason, but sufficient in the absence of others.
958 * [Now we resolve ties in favor of the earlier string if
959 * it happens that c_offset_min has been invalidated, since the
960 * earlier string may buy us something the later one won't.]
964 data.longest_fixed = newSVpv("",0);
965 data.longest_float = newSVpv("",0);
966 data.last_found = newSVpv("",0);
967 data.longest = &(data.longest_fixed);
970 minlen = study_chunk(&first, &fake, scan + PL_regsize, /* Up to end */
971 &data, SCF_DO_SUBSTR);
972 if ( PL_regnpar == 1 && data.longest == &(data.longest_fixed)
973 && data.last_start_min == 0 && data.last_end > 0
975 && (!(PL_regseen & REG_SEEN_GPOS) || (r->reganch & ROPT_ANCH_GPOS)))
976 r->reganch |= ROPT_CHECK_ALL;
978 SvREFCNT_dec(data.last_found);
980 longest_float_length = CHR_SVLEN(data.longest_float);
981 if (longest_float_length
982 || (data.flags & SF_FL_BEFORE_EOL
983 && (!(data.flags & SF_FL_BEFORE_MEOL)
984 || (PL_regflags & PMf_MULTILINE)))) {
985 if (SvCUR(data.longest_fixed) /* ok to leave SvCUR */
986 && data.offset_fixed == data.offset_float_min
987 && SvCUR(data.longest_fixed) == SvCUR(data.longest_float))
988 goto remove_float; /* As in (a)+. */
990 r->float_substr = data.longest_float;
991 r->float_min_offset = data.offset_float_min;
992 r->float_max_offset = data.offset_float_max;
993 fbm_compile(r->float_substr, 0);
994 BmUSEFUL(r->float_substr) = 100;
995 if (data.flags & SF_FL_BEFORE_EOL /* Cannot have SEOL and MULTI */
996 && (!(data.flags & SF_FL_BEFORE_MEOL)
997 || (PL_regflags & PMf_MULTILINE)))
998 SvTAIL_on(r->float_substr);
1002 r->float_substr = Nullsv;
1003 SvREFCNT_dec(data.longest_float);
1004 longest_float_length = 0;
1007 longest_fixed_length = CHR_SVLEN(data.longest_fixed);
1008 if (longest_fixed_length
1009 || (data.flags & SF_FIX_BEFORE_EOL /* Cannot have SEOL and MULTI */
1010 && (!(data.flags & SF_FIX_BEFORE_MEOL)
1011 || (PL_regflags & PMf_MULTILINE)))) {
1012 r->anchored_substr = data.longest_fixed;
1013 r->anchored_offset = data.offset_fixed;
1014 fbm_compile(r->anchored_substr, 0);
1015 BmUSEFUL(r->anchored_substr) = 100;
1016 if (data.flags & SF_FIX_BEFORE_EOL /* Cannot have SEOL and MULTI */
1017 && (!(data.flags & SF_FIX_BEFORE_MEOL)
1018 || (PL_regflags & PMf_MULTILINE)))
1019 SvTAIL_on(r->anchored_substr);
1022 r->anchored_substr = Nullsv;
1023 SvREFCNT_dec(data.longest_fixed);
1024 longest_fixed_length = 0;
1027 /* A temporary algorithm prefers floated substr to fixed one to dig more info. */
1028 if (longest_fixed_length > longest_float_length) {
1029 r->check_substr = r->anchored_substr;
1030 r->check_offset_min = r->check_offset_max = r->anchored_offset;
1031 if (r->reganch & ROPT_ANCH_SINGLE)
1032 r->reganch |= ROPT_NOSCAN;
1035 r->check_substr = r->float_substr;
1036 r->check_offset_min = data.offset_float_min;
1037 r->check_offset_max = data.offset_float_max;
1041 /* Several toplevels. Best we can is to set minlen. */
1044 DEBUG_r(PerlIO_printf(Perl_debug_log, "\n"));
1045 scan = r->program + 1;
1046 minlen = study_chunk(&scan, &fake, scan + PL_regsize, NULL, 0);
1047 r->check_substr = r->anchored_substr = r->float_substr = Nullsv;
1051 if (PL_regseen & REG_SEEN_GPOS)
1052 r->reganch |= ROPT_GPOS_SEEN;
1053 if (PL_regseen & REG_SEEN_LOOKBEHIND)
1054 r->reganch |= ROPT_LOOKBEHIND_SEEN;
1055 if (PL_regseen & REG_SEEN_EVAL)
1056 r->reganch |= ROPT_EVAL_SEEN;
1057 Newz(1002, r->startp, PL_regnpar, char*);
1058 Newz(1002, r->endp, PL_regnpar, char*);
1059 DEBUG_r(regdump(r));
1064 - reg - regular expression, i.e. main body or parenthesized thing
1066 * Caller must absorb opening parenthesis.
1068 * Combining parenthesis handling with the base level of regular expression
1069 * is a trifle forced, but the need to tie the tails of the branches to what
1070 * follows makes it hard to avoid.
1073 reg(I32 paren, I32 *flagp)
1074 /* paren: Parenthesized? 0=top, 1=(, inside: changed to letter. */
1077 register regnode *ret; /* Will be the head of the group. */
1078 register regnode *br;
1079 register regnode *lastbr;
1080 register regnode *ender = 0;
1081 register I32 parno = 0;
1082 I32 flags, oregflags = PL_regflags, have_branch = 0, open = 0;
1085 *flagp = 0; /* Tentatively. */
1087 /* Make an OPEN node, if parenthesized. */
1089 if (*PL_regcomp_parse == '?') {
1090 U16 posflags = 0, negflags = 0;
1091 U16 *flagsp = &posflags;
1095 paren = *PL_regcomp_parse++;
1096 ret = NULL; /* For look-ahead/behind. */
1099 PL_regseen |= REG_SEEN_LOOKBEHIND;
1100 if (*PL_regcomp_parse == '!')
1102 if (*PL_regcomp_parse != '=' && *PL_regcomp_parse != '!')
1113 FAIL2("Sequence (?%c...) not implemented", (int)paren);
1116 while (*PL_regcomp_parse && *PL_regcomp_parse != ')')
1118 if (*PL_regcomp_parse != ')')
1119 FAIL("Sequence (?#... not terminated");
1125 paren = *PL_regcomp_parse++;
1130 I32 count = 1, n = 0;
1132 char *s = PL_regcomp_parse;
1134 OP_4tree *sop, *rop;
1137 PL_regseen |= REG_SEEN_EVAL;
1138 while (count && (c = *PL_regcomp_parse)) {
1139 if (c == '\\' && PL_regcomp_parse[1])
1147 if (*PL_regcomp_parse != ')')
1148 FAIL("Sequence (?{...}) not terminated or not {}-balanced");
1152 if (PL_regcomp_parse - 1 - s)
1153 sv = newSVpv(s, PL_regcomp_parse - 1 - s);
1155 sv = newSVpv("", 0);
1157 rop = sv_compile_2op(sv, &sop, "re", &av);
1159 n = add_data(3, "nso");
1160 PL_regcomp_rx->data->data[n] = (void*)rop;
1161 PL_regcomp_rx->data->data[n+1] = (void*)av;
1162 PL_regcomp_rx->data->data[n+2] = (void*)sop;
1165 else { /* First pass */
1166 if (PL_reginterp_cnt < ++PL_seen_evals && PL_curcop != &compiling)
1167 /* No compiled RE interpolated, has runtime
1168 components ===> unsafe. */
1169 FAIL("Eval-group not allowed at runtime, use re 'eval'");
1171 FAIL("Eval-group in insecure regular expression");
1176 ret = reg_node(LOGICAL);
1179 regtail(ret, reganode(EVAL, n));
1182 return reganode(EVAL, n);
1186 if (PL_regcomp_parse[0] == '?') {
1187 if (PL_regcomp_parse[1] == '=' || PL_regcomp_parse[1] == '!'
1188 || PL_regcomp_parse[1] == '<'
1189 || PL_regcomp_parse[1] == '{') { /* Lookahead or eval. */
1192 ret = reg_node(LOGICAL);
1195 regtail(ret, reg(1, &flag));
1199 else if (PL_regcomp_parse[0] >= '1' && PL_regcomp_parse[0] <= '9' ) {
1200 parno = atoi(PL_regcomp_parse++);
1202 while (isDIGIT(*PL_regcomp_parse))
1204 ret = reganode(GROUPP, parno);
1205 if ((c = *nextchar()) != ')')
1206 FAIL2("Switch (?(number%c not recognized", c);
1208 regtail(ret, reganode(IFTHEN, 0));
1209 br = regbranch(&flags, 1);
1211 br = reganode(LONGJMP, 0);
1213 regtail(br, reganode(LONGJMP, 0));
1216 lastbr = reganode(IFTHEN, 0); /* Fake one for optimizer. */
1217 regbranch(&flags, 1);
1218 regtail(ret, lastbr);
1224 FAIL("Switch (?(condition)... contains too many branches");
1225 ender = reg_node(TAIL);
1228 regtail(lastbr, ender);
1229 regtail(NEXTOPER(NEXTOPER(lastbr)), ender);
1232 regtail(ret, ender);
1236 FAIL2("Unknown condition for (?(%.2s", PL_regcomp_parse);
1240 FAIL("Sequence (? incomplete");
1245 while (*PL_regcomp_parse && strchr("iogcmsx", *PL_regcomp_parse)) {
1246 if (*PL_regcomp_parse != 'o')
1247 pmflag(flagsp, *PL_regcomp_parse);
1250 if (*PL_regcomp_parse == '-') {
1255 PL_regflags |= posflags;
1256 PL_regflags &= ~negflags;
1257 if (*PL_regcomp_parse == ':') {
1263 if (*PL_regcomp_parse != ')')
1264 FAIL2("Sequence (?%c...) not recognized", *PL_regcomp_parse);
1273 ret = reganode(OPEN, parno);
1280 /* Pick up the branches, linking them together. */
1281 br = regbranch(&flags, 1);
1284 if (*PL_regcomp_parse == '|') {
1285 if (!SIZE_ONLY && PL_extralen) {
1286 reginsert(BRANCHJ, br);
1289 reginsert(BRANCH, br);
1292 PL_extralen += 1; /* For BRANCHJ-BRANCH. */
1294 else if (paren == ':') {
1295 *flagp |= flags&SIMPLE;
1297 if (open) { /* Starts with OPEN. */
1298 regtail(ret, br); /* OPEN -> first. */
1300 else if (paren != '?') /* Not Conditional */
1304 *flagp |= flags&SPSTART;
1306 while (*PL_regcomp_parse == '|') {
1307 if (!SIZE_ONLY && PL_extralen) {
1308 ender = reganode(LONGJMP,0);
1309 regtail(NEXTOPER(NEXTOPER(lastbr)), ender); /* Append to the previous. */
1312 PL_extralen += 2; /* Account for LONGJMP. */
1314 br = regbranch(&flags, 0);
1317 regtail(lastbr, br); /* BRANCH -> BRANCH. */
1321 *flagp |= flags&SPSTART;
1324 if (have_branch || paren != ':') {
1325 /* Make a closing node, and hook it on the end. */
1328 ender = reg_node(TAIL);
1331 ender = reganode(CLOSE, parno);
1337 *flagp &= ~HASWIDTH;
1340 ender = reg_node(SUCCEED);
1343 ender = reg_node(END);
1346 regtail(lastbr, ender);
1349 /* Hook the tails of the branches to the closing node. */
1350 for (br = ret; br != NULL; br = regnext(br)) {
1351 regoptail(br, ender);
1358 static char parens[] = "=!<,>";
1360 if (paren && (p = strchr(parens, paren))) {
1361 int node = ((p - parens) % 2) ? UNLESSM : IFMATCH;
1362 int flag = (p - parens) > 1;
1365 node = SUSPEND, flag = 0;
1366 reginsert(node,ret);
1368 regtail(ret, reg_node(TAIL));
1372 /* Check for proper termination. */
1373 if (paren && (PL_regcomp_parse >= PL_regxend || *nextchar() != ')')) {
1374 FAIL("unmatched () in regexp");
1376 else if (!paren && PL_regcomp_parse < PL_regxend) {
1377 if (*PL_regcomp_parse == ')') {
1378 FAIL("unmatched () in regexp");
1381 FAIL("junk on end of regexp"); /* "Can't happen". */
1385 PL_regflags = oregflags;
1392 - regbranch - one alternative of an | operator
1394 * Implements the concatenation operator.
1397 regbranch(I32 *flagp, I32 first)
1400 register regnode *ret;
1401 register regnode *chain = NULL;
1402 register regnode *latest;
1403 I32 flags = 0, c = 0;
1408 if (!SIZE_ONLY && PL_extralen)
1409 ret = reganode(BRANCHJ,0);
1411 ret = reg_node(BRANCH);
1414 if (!first && SIZE_ONLY)
1415 PL_extralen += 1; /* BRANCHJ */
1417 *flagp = WORST; /* Tentatively. */
1421 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != '|' && *PL_regcomp_parse != ')') {
1423 latest = regpiece(&flags);
1424 if (latest == NULL) {
1425 if (flags & TRYAGAIN)
1429 else if (ret == NULL)
1431 *flagp |= flags&HASWIDTH;
1432 if (chain == NULL) /* First piece. */
1433 *flagp |= flags&SPSTART;
1436 regtail(chain, latest);
1441 if (chain == NULL) { /* Loop ran zero times. */
1442 chain = reg_node(NOTHING);
1447 *flagp |= flags&SIMPLE;
1454 - regpiece - something followed by possible [*+?]
1456 * Note that the branching code sequences used for ? and the general cases
1457 * of * and + are somewhat optimized: they use the same NOTHING node as
1458 * both the endmarker for their branch list and the body of the last branch.
1459 * It might seem that this node could be dispensed with entirely, but the
1460 * endmarker role is not redundant.
1463 regpiece(I32 *flagp)
1466 register regnode *ret;
1468 register char *next;
1470 char *origparse = PL_regcomp_parse;
1473 I32 max = REG_INFTY;
1475 ret = regatom(&flags);
1477 if (flags & TRYAGAIN)
1482 op = *PL_regcomp_parse;
1484 if (op == '{' && regcurly(PL_regcomp_parse)) {
1485 next = PL_regcomp_parse + 1;
1487 while (isDIGIT(*next) || *next == ',') {
1496 if (*next == '}') { /* got one */
1500 min = atoi(PL_regcomp_parse);
1504 maxpos = PL_regcomp_parse;
1506 if (!max && *maxpos != '0')
1507 max = REG_INFTY; /* meaning "infinity" */
1508 else if (max >= REG_INFTY)
1509 FAIL2("Quantifier in {,} bigger than %d", REG_INFTY - 1);
1510 PL_regcomp_parse = next;
1514 if ((flags&SIMPLE)) {
1515 PL_regnaughty += 2 + PL_regnaughty / 2;
1516 reginsert(CURLY, ret);
1519 PL_regnaughty += 4 + PL_regnaughty; /* compound interest */
1520 regtail(ret, reg_node(WHILEM));
1521 if (!SIZE_ONLY && PL_extralen) {
1522 reginsert(LONGJMP,ret);
1523 reginsert(NOTHING,ret);
1524 NEXT_OFF(ret) = 3; /* Go over LONGJMP. */
1526 reginsert(CURLYX,ret);
1527 if (!SIZE_ONLY && PL_extralen)
1528 NEXT_OFF(ret) = 3; /* Go over NOTHING to LONGJMP. */
1529 regtail(ret, reg_node(NOTHING));
1539 if (max && max < min)
1540 FAIL("Can't do {n,m} with n > m");
1555 #if 0 /* Now runtime fix should be reliable. */
1556 if (!(flags&HASWIDTH) && op != '?')
1557 FAIL("regexp *+ operand could be empty");
1562 *flagp = (op != '+') ? (WORST|SPSTART|HASWIDTH) : (WORST|HASWIDTH);
1564 if (op == '*' && (flags&SIMPLE)) {
1565 reginsert(STAR, ret);
1569 else if (op == '*') {
1573 else if (op == '+' && (flags&SIMPLE)) {
1574 reginsert(PLUS, ret);
1578 else if (op == '+') {
1582 else if (op == '?') {
1587 if (ckWARN(WARN_UNSAFE) && !SIZE_ONLY && !(flags&HASWIDTH) && max > 10000) {
1588 warner(WARN_UNSAFE, "%.*s matches null string many times",
1589 PL_regcomp_parse - origparse, origparse);
1592 if (*PL_regcomp_parse == '?') {
1594 reginsert(MINMOD, ret);
1595 regtail(ret, ret + NODE_STEP_REGNODE);
1597 if (ISMULT2(PL_regcomp_parse))
1598 FAIL("nested *?+ in regexp");
1604 - regatom - the lowest level
1606 * Optimization: gobbles an entire sequence of ordinary characters so that
1607 * it can turn them into a single node, which is smaller to store and
1608 * faster to run. Backslashed characters are exceptions, each becoming a
1609 * separate node; the code is simpler that way and it's not worth fixing.
1611 * [Yes, it is worth fixing, some scripts can run twice the speed.]
1617 register regnode *ret = 0;
1620 *flagp = WORST; /* Tentatively. */
1623 switch (*PL_regcomp_parse) {
1627 if (PL_regflags & PMf_MULTILINE)
1628 ret = reg_node(MBOL);
1629 else if (PL_regflags & PMf_SINGLELINE)
1630 ret = reg_node(SBOL);
1632 ret = reg_node(BOL);
1635 if (PL_regcomp_parse[1])
1638 if (PL_regflags & PMf_MULTILINE)
1639 ret = reg_node(MEOL);
1640 else if (PL_regflags & PMf_SINGLELINE)
1641 ret = reg_node(SEOL);
1643 ret = reg_node(EOL);
1648 if (PL_regflags & PMf_SINGLELINE)
1649 ret = reg_node(SANYUTF8);
1651 ret = reg_node(ANYUTF8);
1655 if (PL_regflags & PMf_SINGLELINE)
1656 ret = reg_node(SANY);
1658 ret = reg_node(ANY);
1659 *flagp |= HASWIDTH|SIMPLE;
1665 ret = (UTF ? regclassutf8() : regclass());
1666 if (*PL_regcomp_parse != ']')
1667 FAIL("unmatched [] in regexp");
1669 *flagp |= HASWIDTH|SIMPLE;
1673 ret = reg(1, &flags);
1675 if (flags & TRYAGAIN)
1679 *flagp |= flags&(HASWIDTH|SPSTART|SIMPLE);
1683 if (flags & TRYAGAIN) {
1687 FAIL2("internal urp in regexp at /%s/", PL_regcomp_parse);
1688 /* Supposed to be caught earlier. */
1691 if (!regcurly(PL_regcomp_parse)) {
1699 FAIL("?+*{} follows nothing in regexp");
1702 switch (*++PL_regcomp_parse) {
1705 ret = reg_node(SBOL);
1710 ret = reg_node(GPOS);
1711 PL_regseen |= REG_SEEN_GPOS;
1716 ret = reg_node(SEOL);
1721 ret = reg_node(EOS);
1723 PL_seen_zerolen++; /* Do not optimize RE away */
1727 ret = reg_node(SANY);
1728 *flagp |= HASWIDTH|SIMPLE;
1732 ret = reg_node(CLUMP);
1735 if (UTF && !PL_utf8_mark)
1736 is_utf8_mark((U8*)"~"); /* preload table */
1741 ? (LOC ? ALNUMLUTF8 : ALNUMUTF8)
1742 : (LOC ? ALNUML : ALNUM));
1743 *flagp |= HASWIDTH|SIMPLE;
1745 if (UTF && !PL_utf8_alnum)
1746 is_utf8_alnum((U8*)"a"); /* preload table */
1751 ? (LOC ? NALNUMLUTF8 : NALNUMUTF8)
1752 : (LOC ? NALNUML : NALNUM));
1753 *flagp |= HASWIDTH|SIMPLE;
1755 if (UTF && !PL_utf8_alnum)
1756 is_utf8_alnum((U8*)"a"); /* preload table */
1762 ? (LOC ? BOUNDLUTF8 : BOUNDUTF8)
1763 : (LOC ? BOUNDL : BOUND));
1766 if (UTF && !PL_utf8_alnum)
1767 is_utf8_alnum((U8*)"a"); /* preload table */
1773 ? (LOC ? NBOUNDLUTF8 : NBOUNDUTF8)
1774 : (LOC ? NBOUNDL : NBOUND));
1777 if (UTF && !PL_utf8_alnum)
1778 is_utf8_alnum((U8*)"a"); /* preload table */
1783 ? (LOC ? SPACELUTF8 : SPACEUTF8)
1784 : (LOC ? SPACEL : SPACE));
1785 *flagp |= HASWIDTH|SIMPLE;
1787 if (UTF && !PL_utf8_space)
1788 is_utf8_space((U8*)" "); /* preload table */
1793 ? (LOC ? NSPACELUTF8 : NSPACEUTF8)
1794 : (LOC ? NSPACEL : NSPACE));
1795 *flagp |= HASWIDTH|SIMPLE;
1797 if (UTF && !PL_utf8_space)
1798 is_utf8_space((U8*)" "); /* preload table */
1801 ret = reg_node(UTF ? DIGITUTF8 : DIGIT);
1802 *flagp |= HASWIDTH|SIMPLE;
1804 if (UTF && !PL_utf8_digit)
1805 is_utf8_digit((U8*)"1"); /* preload table */
1808 ret = reg_node(UTF ? NDIGITUTF8 : NDIGIT);
1809 *flagp |= HASWIDTH|SIMPLE;
1811 if (UTF && !PL_utf8_digit)
1812 is_utf8_digit((U8*)"1"); /* preload table */
1816 { /* a lovely hack--pretend we saw [\pX] instead */
1817 char* oldregxend = PL_regxend;
1819 if (PL_regcomp_parse[1] == '{') {
1820 PL_regxend = strchr(PL_regcomp_parse, '}');
1822 FAIL("Missing right brace on \\p{}");
1826 PL_regxend = PL_regcomp_parse + 2;
1829 ret = regclassutf8();
1831 PL_regxend = oldregxend;
1834 *flagp |= HASWIDTH|SIMPLE;
1847 case '1': case '2': case '3': case '4':
1848 case '5': case '6': case '7': case '8': case '9':
1850 I32 num = atoi(PL_regcomp_parse);
1852 if (num > 9 && num >= PL_regnpar)
1855 if (!SIZE_ONLY && num > PL_regcomp_rx->nparens)
1856 FAIL("reference to nonexistent group");
1859 ? (LOC ? REFFL : REFF)
1862 while (isDIGIT(*PL_regcomp_parse))
1870 if (PL_regcomp_parse >= PL_regxend)
1871 FAIL("trailing \\ in regexp");
1879 if (PL_regflags & PMf_EXTENDED) {
1880 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != '\n') PL_regcomp_parse++;
1881 if (PL_regcomp_parse < PL_regxend)
1897 ? (LOC ? EXACTFL : EXACTF)
1899 s = (char *) OPERAND(ret);
1900 regc(0, s++); /* save spot for len */
1901 for (len = 0, p = PL_regcomp_parse - 1;
1902 len < 127 && p < PL_regxend;
1907 if (PL_regflags & PMf_EXTENDED)
1908 p = regwhite(p, PL_regxend);
1962 char* e = strchr(p, '}');
1965 FAIL("Missing right brace on \\x{}");
1967 ender = scan_hex(p + 1, e - p, &numlen);
1968 if (numlen + len >= 127) { /* numlen is generous */
1975 FAIL("Can't use \\x{} without 'use utf8' declaration");
1978 ender = scan_hex(p, 2, &numlen);
1984 ender = UCHARAT(p++);
1985 ender = toCTRL(ender);
1987 case '0': case '1': case '2': case '3':case '4':
1988 case '5': case '6': case '7': case '8':case '9':
1990 (isDIGIT(p[1]) && atoi(p) >= PL_regnpar) ) {
1991 ender = scan_oct(p, 3, &numlen);
2000 if (p >= PL_regxend)
2001 FAIL("trailing \\ in regexp");
2004 goto normal_default;
2009 if ((*p & 0xc0) == 0xc0 && UTF) {
2010 ender = utf8_to_uv((U8*)p, &numlen);
2017 if (PL_regflags & PMf_EXTENDED)
2018 p = regwhite(p, PL_regxend);
2021 ender = toLOWER_LC_uni(ender);
2023 ender = toLOWER_uni(ender);
2025 if (ISMULT2(p)) { /* Back off on ?+*. */
2028 else if (ender >= 0x80 && UTF) {
2029 reguni(ender, s, &numlen);
2039 if (ender >= 0x80 && UTF) {
2040 reguni(ender, s, &numlen);
2048 PL_regcomp_parse = p - 1;
2051 FAIL("internal disaster in regexp");
2057 *OPERAND(ret) = len;
2060 PL_regsize += (len + 2 + sizeof(regnode) - 1) / sizeof(regnode);
2063 PL_regcode += (len + 2 + sizeof(regnode) - 1) / sizeof(regnode);
2073 regwhite(char *p, char *e)
2078 else if (*p == '#') {
2081 } while (p < e && *p != '\n');
2093 register char *opnd, *s;
2095 register I32 lastvalue = 1234;
2096 register I32 range = 0;
2097 register regnode *ret;
2101 s = opnd = (char *) OPERAND(PL_regcode);
2102 ret = reg_node(ANYOF);
2103 for (value = 0; value < 33; value++)
2105 if (*PL_regcomp_parse == '^') { /* Complement of range. */
2109 *opnd |= ANYOF_INVERT;
2112 PL_regcode += ANY_SKIP;
2114 *opnd |= ANYOF_FOLD;
2116 *opnd |= ANYOF_LOCALE;
2119 PL_regsize += ANY_SKIP;
2121 if (*PL_regcomp_parse == ']' || *PL_regcomp_parse == '-')
2122 goto skipcond; /* allow 1st char to be ] or - */
2123 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != ']') {
2125 value = UCHARAT(PL_regcomp_parse++);
2126 if (value == '[' && PL_regcomp_parse + 1 < PL_regxend &&
2127 /* I smell either [: or [= or [. -- POSIX has been here, right? */
2128 (*PL_regcomp_parse == ':' || *PL_regcomp_parse == '=' || *PL_regcomp_parse == '.')) {
2129 char posixccc = *PL_regcomp_parse;
2130 char* posixccs = PL_regcomp_parse++;
2132 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != posixccc)
2134 if (PL_regcomp_parse == PL_regxend)
2135 /* Grandfather lone [:, [=, [. */
2136 PL_regcomp_parse = posixccs;
2138 PL_regcomp_parse++; /* skip over the posixccc */
2139 if (*PL_regcomp_parse == ']') {
2140 /* Not Implemented Yet.
2141 * (POSIX Extended Character Classes, that is)
2142 * The text between e.g. [: and :] would start
2143 * at posixccs + 1 and stop at regcomp_parse - 2. */
2144 if (ckWARN(WARN_UNSAFE) && !SIZE_ONLY)
2146 "Character class syntax [%c %c] is reserved for future extensions", posixccc, posixccc);
2147 PL_regcomp_parse++; /* skip over the ending ] */
2151 if (value == '\\') {
2152 value = UCHARAT(PL_regcomp_parse++);
2157 *opnd |= ANYOF_ALNUML;
2159 for (value = 0; value < 256; value++)
2161 ANYOF_SET(opnd, value);
2169 *opnd |= ANYOF_NALNUML;
2171 for (value = 0; value < 256; value++)
2172 if (!isALNUM(value))
2173 ANYOF_SET(opnd, value);
2181 *opnd |= ANYOF_SPACEL;
2183 for (value = 0; value < 256; value++)
2185 ANYOF_SET(opnd, value);
2193 *opnd |= ANYOF_NSPACEL;
2195 for (value = 0; value < 256; value++)
2196 if (!isSPACE(value))
2197 ANYOF_SET(opnd, value);
2204 for (value = '0'; value <= '9'; value++)
2205 ANYOF_SET(opnd, value);
2211 for (value = 0; value < '0'; value++)
2212 ANYOF_SET(opnd, value);
2213 for (value = '9' + 1; value < 256; value++)
2214 ANYOF_SET(opnd, value);
2240 value = scan_hex(PL_regcomp_parse, 2, &numlen);
2241 PL_regcomp_parse += numlen;
2244 value = UCHARAT(PL_regcomp_parse++);
2245 value = toCTRL(value);
2247 case '0': case '1': case '2': case '3': case '4':
2248 case '5': case '6': case '7': case '8': case '9':
2249 value = scan_oct(--PL_regcomp_parse, 3, &numlen);
2250 PL_regcomp_parse += numlen;
2255 if (lastvalue > value)
2256 FAIL("invalid [] range in regexp");
2261 if (*PL_regcomp_parse == '-' && PL_regcomp_parse+1 < PL_regxend &&
2262 PL_regcomp_parse[1] != ']') {
2265 continue; /* do it next time */
2270 if ((isLOWER(lastvalue) && isLOWER(value)) ||
2271 (isUPPER(lastvalue) && isUPPER(value))) {
2272 if (isLOWER(lastvalue)) {
2273 for (i = lastvalue; i <= value; i++)
2277 for (i = lastvalue; i <= value; i++)
2284 for ( ; lastvalue <= value; lastvalue++)
2285 ANYOF_SET(opnd, lastvalue);
2289 /* optimize case-insensitive simple patterns (e.g. /[a-z]/i) */
2290 if (!SIZE_ONLY && (*opnd & (0xFF ^ ANYOF_INVERT)) == ANYOF_FOLD) {
2291 for (value = 0; value < 256; ++value) {
2292 if (ANYOF_TEST(opnd, value)) {
2293 I32 cf = fold[value];
2294 ANYOF_SET(opnd, cf);
2297 *opnd &= ~ANYOF_FOLD;
2299 /* optimize inverted simple patterns (e.g. [^a-z]) */
2300 if (!SIZE_ONLY && (*opnd & 0xFF) == ANYOF_INVERT) {
2301 for (value = 0; value < 32; ++value)
2302 opnd[1 + value] ^= 0xFF;
2311 register char *opnd, *e;
2313 register U32 lastvalue = 123456;
2314 register I32 range = 0;
2315 register regnode *ret;
2322 if (*PL_regcomp_parse == '^') { /* Complement of range. */
2326 flags |= ANYOF_INVERT;
2330 flags |= ANYOF_FOLD;
2332 flags |= ANYOF_LOCALE;
2333 listsv = newSVpv("# comment\n",0);
2336 if (*PL_regcomp_parse == ']' || *PL_regcomp_parse == '-')
2337 goto skipcond; /* allow 1st char to be ] or - */
2339 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != ']') {
2341 value = utf8_to_uv((U8*)PL_regcomp_parse, &numlen);
2342 PL_regcomp_parse += numlen;
2344 if (value == '[' && PL_regcomp_parse + 1 < PL_regxend &&
2345 /* I smell either [: or [= or [. -- POSIX has been here, right? */
2346 (*PL_regcomp_parse == ':' || *PL_regcomp_parse == '=' || *PL_regcomp_parse == '.')) {
2347 char posixccc = *PL_regcomp_parse;
2348 char* posixccs = PL_regcomp_parse++;
2350 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != posixccc)
2352 if (PL_regcomp_parse == PL_regxend)
2353 /* Grandfather lone [:, [=, [. */
2354 PL_regcomp_parse = posixccs;
2356 PL_regcomp_parse++; /* skip over the posixccc */
2357 if (*PL_regcomp_parse == ']') {
2358 /* Not Implemented Yet.
2359 * (POSIX Extended Character Classes, that is)
2360 * The text between e.g. [: and :] would start
2361 * at posixccs + 1 and stop at regcomp_parse - 2. */
2362 if (ckWARN(WARN_UNSAFE) && !SIZE_ONLY)
2364 "Character class syntax [%c %c] is reserved for future extensions", posixccc, posixccc);
2365 PL_regcomp_parse++; /* skip over the ending ] */
2370 if (value == '\\') {
2371 value = utf8_to_uv((U8*)PL_regcomp_parse, &numlen);
2372 PL_regcomp_parse += numlen;
2377 flags |= ANYOF_ALNUML;
2379 sv_catpvf(listsv, "+utf8::IsAlnum\n");
2386 flags |= ANYOF_NALNUML;
2389 "-utf8::IsAlpha\n-utf8::IsDigit\n0000\t%04x\n%04x\tffff\n",
2398 flags |= ANYOF_SPACEL;
2399 sv_catpvf(listsv, "+utf8::IsSpace\n");
2401 is_utf8_space((U8*)" ");
2408 flags |= ANYOF_NSPACEL;
2410 "!utf8::IsSpace\n");
2412 is_utf8_space((U8*)" ");
2418 sv_catpvf(listsv, "+utf8::IsDigit\n");
2425 "!utf8::IsDigit\n");
2431 if (*PL_regcomp_parse == '{') {
2432 e = strchr(PL_regcomp_parse++, '}');
2434 FAIL("Missing right brace on \\p{}");
2435 n = e - PL_regcomp_parse;
2438 e = PL_regcomp_parse;
2443 sv_catpvf(listsv, "+utf8::%.*s\n", n, PL_regcomp_parse);
2446 "!utf8::%.*s\n", n, PL_regcomp_parse);
2448 PL_regcomp_parse = e + 1;
2473 if (*PL_regcomp_parse == '{') {
2474 e = strchr(PL_regcomp_parse++, '}');
2476 FAIL("Missing right brace on \\x{}");
2477 value = scan_hex(PL_regcomp_parse + 1, e - PL_regcomp_parse, &numlen);
2478 PL_regcomp_parse = e + 1;
2481 value = scan_hex(PL_regcomp_parse, 2, &numlen);
2482 PL_regcomp_parse += numlen;
2486 value = UCHARAT(PL_regcomp_parse++);
2487 value = toCTRL(value);
2489 case '0': case '1': case '2': case '3': case '4':
2490 case '5': case '6': case '7': case '8': case '9':
2491 value = scan_oct(--PL_regcomp_parse, 3, &numlen);
2492 PL_regcomp_parse += numlen;
2497 if (lastvalue > value)
2498 FAIL("invalid [] range in regexp");
2500 sv_catpvf(listsv, "%04x\t%04x\n", lastvalue, value);
2506 if (*PL_regcomp_parse == '-' && PL_regcomp_parse+1 < PL_regxend &&
2507 PL_regcomp_parse[1] != ']') {
2510 continue; /* do it next time */
2513 sv_catpvf(listsv, "%04x\n", value);
2517 ret = reganode(ANYOFUTF8, 0);
2520 SV *rv = swash_init("utf8", "", listsv, 1, 0);
2521 SvREFCNT_dec(listsv);
2522 n = add_data(1,"s");
2523 PL_regcomp_rx->data->data[n] = (void*)rv;
2524 ARG1_SET(ret, flags);
2535 char* retval = PL_regcomp_parse++;
2538 if (*PL_regcomp_parse == '(' && PL_regcomp_parse[1] == '?' &&
2539 PL_regcomp_parse[2] == '#') {
2540 while (*PL_regcomp_parse && *PL_regcomp_parse != ')')
2545 if (PL_regflags & PMf_EXTENDED) {
2546 if (isSPACE(*PL_regcomp_parse)) {
2550 else if (*PL_regcomp_parse == '#') {
2551 while (*PL_regcomp_parse && *PL_regcomp_parse != '\n')
2562 - reg_node - emit a node
2564 STATIC regnode * /* Location. */
2568 register regnode *ret;
2569 register regnode *ptr;
2573 SIZE_ALIGN(PL_regsize);
2578 NODE_ALIGN_FILL(ret);
2580 FILL_ADVANCE_NODE(ptr, op);
2587 - reganode - emit a node with an argument
2589 STATIC regnode * /* Location. */
2590 reganode(U8 op, U32 arg)
2593 register regnode *ret;
2594 register regnode *ptr;
2598 SIZE_ALIGN(PL_regsize);
2603 NODE_ALIGN_FILL(ret);
2605 FILL_ADVANCE_NODE_ARG(ptr, op, arg);
2612 - regc - emit (if appropriate) a Unicode character
2615 reguni(UV uv, char* s, I32* lenp)
2620 *lenp = uv_to_utf8(tmpbuf, uv) - tmpbuf;
2623 *lenp = uv_to_utf8((U8*)s, uv) - (U8*)s;
2628 - regc - emit (if appropriate) a byte of code
2639 - reginsert - insert an operator in front of already-emitted operand
2641 * Means relocating the operand.
2644 reginsert(U8 op, regnode *opnd)
2647 register regnode *src;
2648 register regnode *dst;
2649 register regnode *place;
2650 register int offset = regarglen[(U8)op];
2652 /* (regkind[(U8)op] == CURLY ? EXTRA_STEP_2ARGS : 0); */
2655 PL_regsize += NODE_STEP_REGNODE + offset;
2660 PL_regcode += NODE_STEP_REGNODE + offset;
2663 StructCopy(--src, --dst, regnode);
2665 place = opnd; /* Op node, where operand used to be. */
2666 src = NEXTOPER(place);
2667 FILL_ADVANCE_NODE(place, op);
2668 Zero(src, offset, regnode);
2672 - regtail - set the next-pointer at the end of a node chain of p to val.
2675 regtail(regnode *p, regnode *val)
2678 register regnode *scan;
2679 register regnode *temp;
2680 register I32 offset;
2685 /* Find last node. */
2688 temp = regnext(scan);
2694 if (reg_off_by_arg[OP(scan)]) {
2695 ARG_SET(scan, val - scan);
2698 NEXT_OFF(scan) = val - scan;
2703 - regoptail - regtail on operand of first argument; nop if operandless
2706 regoptail(regnode *p, regnode *val)
2709 /* "Operandless" and "op != BRANCH" are synonymous in practice. */
2710 if (p == NULL || SIZE_ONLY)
2712 if (regkind[(U8)OP(p)] == BRANCH) {
2713 regtail(NEXTOPER(p), val);
2715 else if ( regkind[(U8)OP(p)] == BRANCHJ) {
2716 regtail(NEXTOPER(NEXTOPER(p)), val);
2723 - regcurly - a little FSA that accepts {\d+,?\d*}
2726 regcurly(register char *s)
2745 dumpuntil(regnode *start, regnode *node, regnode *last, SV* sv, I32 l)
2748 register char op = EXACT; /* Arbitrary non-END op. */
2749 register regnode *next, *onode;
2751 while (op != END && (!last || node < last)) {
2752 /* While that wasn't END last time... */
2758 next = regnext(node);
2760 if (OP(node) == OPTIMIZED)
2763 PerlIO_printf(Perl_debug_log, "%4d:%*s%s", node - start,
2764 2*l + 1, "", SvPVX(sv));
2765 if (next == NULL) /* Next ptr. */
2766 PerlIO_printf(Perl_debug_log, "(0)");
2768 PerlIO_printf(Perl_debug_log, "(%d)", next - start);
2769 (void)PerlIO_putc(Perl_debug_log, '\n');
2771 if (regkind[(U8)op] == BRANCHJ) {
2772 register regnode *nnode = (OP(next) == LONGJMP
2775 if (last && nnode > last)
2777 node = dumpuntil(start, NEXTOPER(NEXTOPER(node)), nnode, sv, l + 1);
2779 else if (regkind[(U8)op] == BRANCH) {
2780 node = dumpuntil(start, NEXTOPER(node), next, sv, l + 1);
2782 else if ( op == CURLY) { /* `next' might be very big: optimizer */
2783 node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
2784 NEXTOPER(node) + EXTRA_STEP_2ARGS + 1, sv, l + 1);
2786 else if (regkind[(U8)op] == CURLY && op != CURLYX) {
2787 node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
2790 else if ( op == PLUS || op == STAR) {
2791 node = dumpuntil(start, NEXTOPER(node), NEXTOPER(node) + 1, sv, l + 1);
2793 else if (op == ANYOF) {
2794 node = NEXTOPER(node);
2797 else if (regkind[(U8)op] == EXACT) {
2798 /* Literal string, where present. */
2799 node += ((*OPERAND(node)) + 2 + sizeof(regnode) - 1) / sizeof(regnode);
2800 node = NEXTOPER(node);
2803 node = NEXTOPER(node);
2804 node += regarglen[(U8)op];
2806 if (op == CURLYX || op == OPEN)
2808 else if (op == WHILEM)
2811 #endif /* DEBUGGING */
2816 - regdump - dump a regexp onto Perl_debug_log in vaguely comprehensible form
2823 SV *sv = sv_newmortal();
2825 (void)dumpuntil(r->program, r->program + 1, NULL, sv, 0);
2827 /* Header fields of interest. */
2828 if (r->anchored_substr)
2829 PerlIO_printf(Perl_debug_log, "anchored `%s%s%s'%s at %d ",
2831 SvPVX(r->anchored_substr),
2833 SvTAIL(r->anchored_substr) ? "$" : "",
2834 r->anchored_offset);
2835 if (r->float_substr)
2836 PerlIO_printf(Perl_debug_log, "floating `%s%s%s'%s at %d..%u ",
2838 SvPVX(r->float_substr),
2840 SvTAIL(r->float_substr) ? "$" : "",
2841 r->float_min_offset, r->float_max_offset);
2842 if (r->check_substr)
2843 PerlIO_printf(Perl_debug_log,
2844 r->check_substr == r->float_substr
2845 ? "(checking floating" : "(checking anchored");
2846 if (r->reganch & ROPT_NOSCAN)
2847 PerlIO_printf(Perl_debug_log, " noscan");
2848 if (r->reganch & ROPT_CHECK_ALL)
2849 PerlIO_printf(Perl_debug_log, " isall");
2850 if (r->check_substr)
2851 PerlIO_printf(Perl_debug_log, ") ");
2853 if (r->regstclass) {
2854 regprop(sv, r->regstclass);
2855 PerlIO_printf(Perl_debug_log, "stclass `%s' ", SvPVX(sv));
2857 if (r->reganch & ROPT_ANCH) {
2858 PerlIO_printf(Perl_debug_log, "anchored");
2859 if (r->reganch & ROPT_ANCH_BOL)
2860 PerlIO_printf(Perl_debug_log, "(BOL)");
2861 if (r->reganch & ROPT_ANCH_MBOL)
2862 PerlIO_printf(Perl_debug_log, "(MBOL)");
2863 if (r->reganch & ROPT_ANCH_GPOS)
2864 PerlIO_printf(Perl_debug_log, "(GPOS)");
2865 PerlIO_putc(Perl_debug_log, ' ');
2867 if (r->reganch & ROPT_GPOS_SEEN)
2868 PerlIO_printf(Perl_debug_log, "GPOS ");
2869 if (r->reganch & ROPT_SKIP)
2870 PerlIO_printf(Perl_debug_log, "plus ");
2871 if (r->reganch & ROPT_IMPLICIT)
2872 PerlIO_printf(Perl_debug_log, "implicit ");
2873 PerlIO_printf(Perl_debug_log, "minlen %ld ", (long) r->minlen);
2874 if (r->reganch & ROPT_EVAL_SEEN)
2875 PerlIO_printf(Perl_debug_log, "with eval ");
2876 PerlIO_printf(Perl_debug_log, "\n");
2877 #endif /* DEBUGGING */
2881 - regprop - printable representation of opcode
2884 regprop(SV *sv, regnode *o)
2888 register char *p = 0;
2890 sv_setpvn(sv, "", 0);
2935 sv_catpvf(sv, "EXACT <%s%s%s>", PL_colors[0], OPERAND(o) + 1, PL_colors[1]);
2938 sv_catpvf(sv, "EXACTF <%s%s%s>", PL_colors[0], OPERAND(o) + 1, PL_colors[1]);
2941 sv_catpvf(sv, "EXACTFL <%s%s%s>", PL_colors[0], OPERAND(o) + 1, PL_colors[1]);
2968 sv_catpvf(sv, "CURLY {%d,%d}", ARG1(o), ARG2(o));
2971 sv_catpvf(sv, "CURLYM[%d] {%d,%d}", o->flags, ARG1(o), ARG2(o));
2974 sv_catpvf(sv, "CURLYN[%d] {%d,%d}", o->flags, ARG1(o), ARG2(o));
2977 sv_catpvf(sv, "CURLYX {%d,%d}", ARG1(o), ARG2(o));
2980 sv_catpvf(sv, "REF%d", ARG(o));
2983 sv_catpvf(sv, "REFF%d", ARG(o));
2986 sv_catpvf(sv, "REFFL%d", ARG(o));
2989 sv_catpvf(sv, "OPEN%d", ARG(o));
2992 sv_catpvf(sv, "CLOSE%d", ARG(o));
3008 sv_catpvf(sv, "UNLESSM[-%d]", o->flags);
3011 sv_catpvf(sv, "IFMATCH[-%d]", o->flags);
3062 sv_catpvf(sv, "GROUPP%d", ARG(o));
3065 sv_catpvf(sv, "LOGICAL[%d]", o->flags);
3077 FAIL("corrupted regexp opcode");
3081 #endif /* DEBUGGING */
3085 pregfree(struct regexp *r)
3088 if (!r || (--r->refcnt > 0))
3091 Safefree(r->precomp);
3093 Safefree(r->subbase);
3095 if (r->anchored_substr)
3096 SvREFCNT_dec(r->anchored_substr);
3097 if (r->float_substr)
3098 SvREFCNT_dec(r->float_substr);
3099 Safefree(r->substrs);
3102 int n = r->data->count;
3104 switch (r->data->what[n]) {
3106 SvREFCNT_dec((SV*)r->data->data[n]);
3109 op_free((OP_4tree*)r->data->data[n]);
3114 FAIL2("panic: regfree data code '%c'", r->data->what[n]);
3117 Safefree(r->data->what);
3120 Safefree(r->startp);
3126 - regnext - dig the "next" pointer out of a node
3128 * [Note, when REGALIGN is defined there are two places in regmatch()
3129 * that bypass this code for speed.]
3132 regnext(register regnode *p)
3135 register I32 offset;
3137 if (p == &PL_regdummy)
3140 offset = (reg_off_by_arg[OP(p)] ? ARG(p) : NEXT_OFF(p));
3148 re_croak2(const char* pat1,const char* pat2,...)
3151 STRLEN l1 = strlen(pat1);
3152 STRLEN l2 = strlen(pat2);
3160 Copy(pat1, buf, l1 , char);
3161 Copy(pat2, buf + l1, l2 , char);
3162 buf[l1 + l2] = '\n';
3163 buf[l1 + l2 + 1] = '\0';
3164 va_start(args, pat2);
3165 message = mess(buf, &args);
3167 l1 = strlen(message);
3170 Copy(message, buf, l1 , char);
3171 buf[l1] = '\0'; /* Overwrite \n */
3175 /* XXX Here's a total kludge. But we need to re-enter for swash routines. */
3178 save_re_context(void)
3182 SAVEPPTR(PL_regprecomp); /* uncompiled string. */
3183 SAVEI32(PL_regnpar); /* () count. */
3184 SAVEI32(PL_regsize); /* Code size. */
3185 SAVEI16(PL_regflags); /* are we folding, multilining? */
3186 SAVEPPTR(PL_reginput); /* String-input pointer. */
3187 SAVEPPTR(PL_regbol); /* Beginning of input, for ^ check. */
3188 SAVEPPTR(PL_regeol); /* End of input, for $ check. */
3189 SAVESPTR(PL_regstartp); /* Pointer to startp array. */
3190 SAVESPTR(PL_regendp); /* Ditto for endp. */
3191 SAVESPTR(PL_reglastparen); /* Similarly for lastparen. */
3192 SAVEPPTR(PL_regtill); /* How far we are required to go. */
3193 SAVEI32(PL_regprev); /* char before regbol, \n if none */
3194 SAVESPTR(PL_reg_start_tmp); /* from regexec.c */
3195 PL_reg_start_tmp = 0;
3196 SAVEFREEPV(PL_reg_start_tmp);
3197 SAVEI32(PL_reg_start_tmpl); /* from regexec.c */
3198 PL_reg_start_tmpl = 0;
3199 SAVESPTR(PL_regdata);
3200 SAVEI32(PL_reg_flags); /* from regexec.c */
3201 SAVEI32(PL_reg_eval_set); /* from regexec.c */
3202 SAVEI32(PL_regnarrate); /* from regexec.c */
3203 SAVESPTR(PL_regprogram); /* from regexec.c */
3204 SAVEINT(PL_regindent); /* from regexec.c */
3205 SAVESPTR(PL_regcc); /* from regexec.c */
3206 SAVESPTR(PL_curcop);
3207 SAVESPTR(PL_regcomp_rx); /* from regcomp.c */
3208 SAVEI32(PL_regseen); /* from regcomp.c */
3209 SAVEI32(PL_regsawback); /* Did we see \1, ...? */
3210 SAVEI32(PL_regnaughty); /* How bad is this pattern? */
3211 SAVESPTR(PL_regcode); /* Code-emit pointer; ®dummy = don't */
3212 SAVEPPTR(PL_regxend); /* End of input for compile */
3213 SAVEPPTR(PL_regcomp_parse); /* Input-scan pointer. */