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 Perl_save_re_context my_save_re_context
42 # define Perl_reginitcolors my_reginitcolors
47 * pregcomp and pregexec -- regsub and regerror are not used in perl
49 * Copyright (c) 1986 by University of Toronto.
50 * Written by Henry Spencer. Not derived from licensed software.
52 * Permission is granted to anyone to use this software for any
53 * purpose on any computer system, and to redistribute it freely,
54 * subject to the following restrictions:
56 * 1. The author is not responsible for the consequences of use of
57 * this software, no matter how awful, even if they arise
60 * 2. The origin of this software must not be misrepresented, either
61 * by explicit claim or by omission.
63 * 3. Altered versions must be plainly marked as such, and must not
64 * be misrepresented as being the original software.
67 **** Alterations to Henry's code are...
69 **** Copyright (c) 1991-1998, Larry Wall
71 **** You may distribute under the terms of either the GNU General Public
72 **** License or the Artistic License, as specified in the README file.
75 * Beware that some of this code is subtly aware of the way operator
76 * precedence is structured in regular expressions. Serious changes in
77 * regular-expression syntax might require a total rethink.
82 #ifndef PERL_IN_XSUB_RE
94 # if defined(BUGGY_MSC6)
95 /* MSC 6.00A breaks on op/regexp.t test 85 unless we turn this off */
96 # pragma optimize("a",off)
97 /* But MSC 6.00A is happy with 'w', for aliases only across function calls*/
98 # pragma optimize("w",on )
99 # endif /* BUGGY_MSC6 */
103 #define STATIC static
106 #define ISMULT1(c) ((c) == '*' || (c) == '+' || (c) == '?')
107 #define ISMULT2(s) ((*s) == '*' || (*s) == '+' || (*s) == '?' || \
108 ((*s) == '{' && regcurly(s)))
110 #define PERL_META "^$.[()|?+*\\"
112 #define META "^$.[()|?+*\\"
116 #undef SPSTART /* dratted cpp namespace... */
119 * Flags to be passed up and down.
121 #define WORST 0 /* Worst case. */
122 #define HASWIDTH 0x1 /* Known to match non-null strings. */
123 #define SIMPLE 0x2 /* Simple enough to be STAR/PLUS operand. */
124 #define SPSTART 0x4 /* Starts with * or +. */
125 #define TRYAGAIN 0x8 /* Weeded out a declaration. */
128 * Forward declarations for pregcomp()'s friends.
132 static regnode *reg _((I32, I32 *));
133 static regnode *reganode _((U8, U32));
134 static regnode *regatom _((I32 *));
135 static regnode *regbranch _((I32 *, I32));
136 static void regc _((U8, char *));
137 static void reguni _((UV, char *, I32*));
138 static regnode *regclass _((void));
139 static regnode *regclassutf8 _((void));
140 STATIC I32 regcurly _((char *));
141 static regnode *reg_node _((U8));
142 static regnode *regpiece _((I32 *));
143 static void reginsert _((U8, regnode *));
144 static void regoptail _((regnode *, regnode *));
145 static void regtail _((regnode *, regnode *));
146 static char* regwhite _((char *, char *));
147 static char* nextchar _((void));
148 static void re_croak2 _((const char* pat1,const char* pat2,...)) __attribute__((noreturn));
149 static char* regpposixcc _((I32 value));
150 static void clear_re _((void *r));
153 /* Length of a variant. */
159 I32 pos_min; /* CC */
160 I32 pos_delta; /* CC */
162 I32 last_end; /* min value, <0 unless valid. */
163 I32 last_start_min; /* CC */
164 I32 last_start_max; /* CC */
165 SV **longest; /* Either &l_fixed, or &l_float. */
167 I32 offset_fixed; /* CC */
169 I32 offset_float_min; /* CC */
170 I32 offset_float_max; /* CC */
175 static scan_data_t zero_scan_data = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
178 #define SF_BEFORE_EOL (SF_BEFORE_SEOL|SF_BEFORE_MEOL)
179 #define SF_BEFORE_SEOL 0x1
180 #define SF_BEFORE_MEOL 0x2
181 #define SF_FIX_BEFORE_EOL (SF_FIX_BEFORE_SEOL|SF_FIX_BEFORE_MEOL)
182 #define SF_FL_BEFORE_EOL (SF_FL_BEFORE_SEOL|SF_FL_BEFORE_MEOL)
185 # define SF_FIX_SHIFT_EOL (0+2)
186 # define SF_FL_SHIFT_EOL (0+4)
188 # define SF_FIX_SHIFT_EOL (+2)
189 # define SF_FL_SHIFT_EOL (+4)
192 #define SF_FIX_BEFORE_SEOL (SF_BEFORE_SEOL << SF_FIX_SHIFT_EOL)
193 #define SF_FIX_BEFORE_MEOL (SF_BEFORE_MEOL << SF_FIX_SHIFT_EOL)
195 #define SF_FL_BEFORE_SEOL (SF_BEFORE_SEOL << SF_FL_SHIFT_EOL)
196 #define SF_FL_BEFORE_MEOL (SF_BEFORE_MEOL << SF_FL_SHIFT_EOL) /* 0x20 */
197 #define SF_IS_INF 0x40
198 #define SF_HAS_PAR 0x80
199 #define SF_IN_PAR 0x100
200 #define SF_HAS_EVAL 0x200
201 #define SCF_DO_SUBSTR 0x400
204 #define UTF (PL_reg_flags & RF_utf8)
205 #define LOC (PL_regflags & PMf_LOCALE)
206 #define FOLD (PL_regflags & PMf_FOLD)
208 #define CHR_SVLEN(sv) (UTF ? sv_len_utf8(sv) : SvCUR(sv))
209 #define CHR_DIST(a,b) (UTF ? utf8_distance(a,b) : a - b)
214 ReREFCNT_dec((regexp *)r);
218 scan_commit(scan_data_t *data)
221 STRLEN l = CHR_SVLEN(data->last_found);
222 STRLEN old_l = CHR_SVLEN(*data->longest);
224 if ((l >= old_l) && ((l > old_l) || (data->flags & SF_BEFORE_EOL))) {
225 sv_setsv(*data->longest, data->last_found);
226 if (*data->longest == data->longest_fixed) {
227 data->offset_fixed = l ? data->last_start_min : data->pos_min;
228 if (data->flags & SF_BEFORE_EOL)
230 |= ((data->flags & SF_BEFORE_EOL) << SF_FIX_SHIFT_EOL);
232 data->flags &= ~SF_FIX_BEFORE_EOL;
235 data->offset_float_min = l ? data->last_start_min : data->pos_min;
236 data->offset_float_max = (l
237 ? data->last_start_max
238 : data->pos_min + data->pos_delta);
239 if (data->flags & SF_BEFORE_EOL)
241 |= ((data->flags & SF_BEFORE_EOL) << SF_FL_SHIFT_EOL);
243 data->flags &= ~SF_FL_BEFORE_EOL;
246 SvCUR_set(data->last_found, 0);
248 data->flags &= ~SF_BEFORE_EOL;
251 /* Stops at toplevel WHILEM as well as at `last'. At end *scanp is set
252 to the position after last scanned or to NULL. */
255 study_chunk(regnode **scanp, I32 *deltap, regnode *last, scan_data_t *data, U32 flags)
256 /* scanp: Start here (read-write). */
257 /* deltap: Write maxlen-minlen here. */
258 /* last: Stop before this one. */
261 I32 min = 0, pars = 0, code;
262 regnode *scan = *scanp, *next;
264 int is_inf = (flags & SCF_DO_SUBSTR) && (data->flags & SF_IS_INF);
265 int is_inf_internal = 0; /* The studied chunk is infinite */
266 I32 is_par = OP(scan) == OPEN ? ARG(scan) : 0;
267 scan_data_t data_fake;
269 while (scan && OP(scan) != END && scan < last) {
270 /* Peephole optimizer: */
272 if (PL_regkind[(U8)OP(scan)] == EXACT) {
273 regnode *n = regnext(scan);
276 regnode *stop = scan;
279 next = scan + (*OPERAND(scan) + 2 - 1)/sizeof(regnode) + 2;
280 /* Skip NOTHING, merge EXACT*. */
282 ( PL_regkind[(U8)OP(n)] == NOTHING ||
283 (stringok && (OP(n) == OP(scan))))
285 && NEXT_OFF(scan) + NEXT_OFF(n) < I16_MAX) {
286 if (OP(n) == TAIL || n > next)
288 if (PL_regkind[(U8)OP(n)] == NOTHING) {
289 NEXT_OFF(scan) += NEXT_OFF(n);
290 next = n + NODE_STEP_REGNODE;
298 int oldl = *OPERAND(scan);
299 regnode *nnext = regnext(n);
301 if (oldl + *OPERAND(n) > U8_MAX)
303 NEXT_OFF(scan) += NEXT_OFF(n);
304 *OPERAND(scan) += *OPERAND(n);
305 next = n + (*OPERAND(n) + 2 - 1)/sizeof(regnode) + 2;
306 /* Now we can overwrite *n : */
307 Move(OPERAND(n) + 1, OPERAND(scan) + oldl + 1,
308 *OPERAND(n) + 1, char);
318 n = scan + (*OPERAND(scan) + 2 - 1)/sizeof(regnode) + 2;
320 /* Purify reports a benign UMR here sometimes, because we
321 * don't initialize the OP() slot of a node when that node
322 * is occupied by just the trailing null of the string in
324 if (PL_regkind[(U8)OP(n)] != NOTHING || OP(n) == NOTHING) {
333 if (OP(scan) != CURLYX) {
334 int max = (reg_off_by_arg[OP(scan)]
336 /* I32 may be smaller than U16 on CRAYs! */
337 : (I32_MAX < U16_MAX ? I32_MAX : U16_MAX));
338 int off = (reg_off_by_arg[OP(scan)] ? ARG(scan) : NEXT_OFF(scan));
342 /* Skip NOTHING and LONGJMP. */
343 while ((n = regnext(n))
344 && ((PL_regkind[(U8)OP(n)] == NOTHING && (noff = NEXT_OFF(n)))
345 || ((OP(n) == LONGJMP) && (noff = ARG(n))))
348 if (reg_off_by_arg[OP(scan)])
351 NEXT_OFF(scan) = off;
353 if (OP(scan) == BRANCH || OP(scan) == BRANCHJ
354 || OP(scan) == IFTHEN || OP(scan) == SUSPEND) {
355 next = regnext(scan);
358 if (OP(next) == code || code == IFTHEN || code == SUSPEND) {
359 I32 max1 = 0, min1 = I32_MAX, num = 0;
361 if (flags & SCF_DO_SUBSTR)
363 while (OP(scan) == code) {
364 I32 deltanext, minnext;
368 next = regnext(scan);
369 scan = NEXTOPER(scan);
371 scan = NEXTOPER(scan);
372 /* We suppose the run is continuous, last=next...*/
373 minnext = study_chunk(&scan, &deltanext, next,
377 if (max1 < minnext + deltanext)
378 max1 = minnext + deltanext;
379 if (deltanext == I32_MAX)
380 is_inf = is_inf_internal = 1;
382 if (data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
384 if (data && (data_fake.flags & SF_HAS_EVAL))
385 data->flags |= SF_HAS_EVAL;
389 if (code == IFTHEN && num < 2) /* Empty ELSE branch */
391 if (flags & SCF_DO_SUBSTR) {
392 data->pos_min += min1;
393 data->pos_delta += max1 - min1;
394 if (max1 != min1 || is_inf)
395 data->longest = &(data->longest_float);
398 delta += max1 - min1;
400 else if (code == BRANCHJ) /* single branch is optimized. */
401 scan = NEXTOPER(NEXTOPER(scan));
402 else /* single branch is optimized. */
403 scan = NEXTOPER(scan);
406 else if (OP(scan) == EXACT) {
407 I32 l = *OPERAND(scan);
409 unsigned char *s = (unsigned char *)(OPERAND(scan)+1);
410 unsigned char *e = s + l;
419 if (flags & SCF_DO_SUBSTR) { /* Update longest substr. */
420 /* The code below prefers earlier match for fixed
421 offset, later match for variable offset. */
422 if (data->last_end == -1) { /* Update the start info. */
423 data->last_start_min = data->pos_min;
424 data->last_start_max = is_inf
425 ? I32_MAX : data->pos_min + data->pos_delta;
427 sv_catpvn(data->last_found, (char *)(OPERAND(scan)+1), *OPERAND(scan));
428 data->last_end = data->pos_min + l;
429 data->pos_min += l; /* As in the first entry. */
430 data->flags &= ~SF_BEFORE_EOL;
433 else if (PL_regkind[(U8)OP(scan)] == EXACT) {
434 I32 l = *OPERAND(scan);
435 if (flags & SCF_DO_SUBSTR)
438 unsigned char *s = (unsigned char *)(OPERAND(scan)+1);
439 unsigned char *e = s + l;
448 if (data && (flags & SCF_DO_SUBSTR))
451 else if (strchr(PL_varies,OP(scan))) {
452 I32 mincount, maxcount, minnext, deltanext, pos_before, fl;
453 regnode *oscan = scan;
455 switch (PL_regkind[(U8)OP(scan)]) {
457 scan = NEXTOPER(scan);
460 if (flags & SCF_DO_SUBSTR) {
461 next = NEXTOPER(scan);
462 if (OP(next) == EXACT) {
464 maxcount = REG_INFTY;
465 next = regnext(scan);
466 scan = NEXTOPER(scan);
470 if (flags & SCF_DO_SUBSTR)
475 is_inf = is_inf_internal = 1;
476 scan = regnext(scan);
477 if (flags & SCF_DO_SUBSTR) {
479 data->longest = &(data->longest_float);
481 goto optimize_curly_tail;
483 mincount = ARG1(scan);
484 maxcount = ARG2(scan);
485 next = regnext(scan);
486 scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
488 if (flags & SCF_DO_SUBSTR) {
489 if (mincount == 0) scan_commit(data);
490 pos_before = data->pos_min;
494 data->flags &= ~(SF_HAS_PAR|SF_IN_PAR|SF_HAS_EVAL);
496 data->flags |= SF_IS_INF;
498 /* This will finish on WHILEM, setting scan, or on NULL: */
499 minnext = study_chunk(&scan, &deltanext, last, data,
501 ? (flags & ~SCF_DO_SUBSTR) : flags);
502 if (!scan) /* It was not CURLYX, but CURLY. */
504 if (ckWARN(WARN_UNSAFE) && (minnext + deltanext == 0)
505 && !(data->flags & (SF_HAS_PAR|SF_IN_PAR))
506 && maxcount <= REG_INFTY/3) /* Complement check for big count */
507 warner(WARN_UNSAFE, "Strange *+?{} on zero-length expression");
508 min += minnext * mincount;
509 is_inf_internal |= (maxcount == REG_INFTY
510 && (minnext + deltanext) > 0
511 || deltanext == I32_MAX);
512 is_inf |= is_inf_internal;
513 delta += (minnext + deltanext) * maxcount - minnext * mincount;
515 /* Try powerful optimization CURLYX => CURLYN. */
516 if ( OP(oscan) == CURLYX && data
517 && data->flags & SF_IN_PAR
518 && !(data->flags & SF_HAS_EVAL)
519 && !deltanext && minnext == 1 ) {
520 /* Try to optimize to CURLYN. */
521 regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS;
522 regnode *nxt1 = nxt, *nxt2;
526 if (!strchr(PL_simple,OP(nxt))
527 && !(PL_regkind[(U8)OP(nxt)] == EXACT
528 && *OPERAND(nxt) == 1))
532 if (OP(nxt) != CLOSE)
534 /* Now we know that nxt2 is the only contents: */
535 oscan->flags = ARG(nxt);
537 OP(nxt1) = NOTHING; /* was OPEN. */
539 OP(nxt1 + 1) = OPTIMIZED; /* was count. */
540 NEXT_OFF(nxt1+ 1) = 0; /* just for consistancy. */
541 NEXT_OFF(nxt2) = 0; /* just for consistancy with CURLY. */
542 OP(nxt) = OPTIMIZED; /* was CLOSE. */
543 OP(nxt + 1) = OPTIMIZED; /* was count. */
544 NEXT_OFF(nxt+ 1) = 0; /* just for consistancy. */
549 /* Try optimization CURLYX => CURLYM. */
550 if ( OP(oscan) == CURLYX && data
551 && !(data->flags & SF_HAS_PAR)
552 && !(data->flags & SF_HAS_EVAL)
554 /* XXXX How to optimize if data == 0? */
555 /* Optimize to a simpler form. */
556 regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN */
560 while ( (nxt2 = regnext(nxt)) /* skip over embedded stuff*/
561 && (OP(nxt2) != WHILEM))
563 OP(nxt2) = SUCCEED; /* Whas WHILEM */
564 /* Need to optimize away parenths. */
565 if (data->flags & SF_IN_PAR) {
566 /* Set the parenth number. */
567 regnode *nxt1 = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN*/
569 if (OP(nxt) != CLOSE)
570 FAIL("panic opt close");
571 oscan->flags = ARG(nxt);
572 OP(nxt1) = OPTIMIZED; /* was OPEN. */
573 OP(nxt) = OPTIMIZED; /* was CLOSE. */
575 OP(nxt1 + 1) = OPTIMIZED; /* was count. */
576 OP(nxt + 1) = OPTIMIZED; /* was count. */
577 NEXT_OFF(nxt1 + 1) = 0; /* just for consistancy. */
578 NEXT_OFF(nxt + 1) = 0; /* just for consistancy. */
581 while ( nxt1 && (OP(nxt1) != WHILEM)) {
582 regnode *nnxt = regnext(nxt1);
585 if (reg_off_by_arg[OP(nxt1)])
586 ARG_SET(nxt1, nxt2 - nxt1);
587 else if (nxt2 - nxt1 < U16_MAX)
588 NEXT_OFF(nxt1) = nxt2 - nxt1;
590 OP(nxt) = NOTHING; /* Cannot beautify */
595 /* Optimize again: */
596 study_chunk(&nxt1, &deltanext, nxt, NULL, 0);
601 if (data && fl & (SF_HAS_PAR|SF_IN_PAR))
603 if (flags & SCF_DO_SUBSTR) {
604 SV *last_str = Nullsv;
605 int counted = mincount != 0;
607 if (data->last_end > 0 && mincount != 0) { /* Ends with a string. */
608 I32 b = pos_before >= data->last_start_min
609 ? pos_before : data->last_start_min;
611 char *s = SvPV(data->last_found, l);
612 I32 old = b - data->last_start_min;
615 old = utf8_hop((U8*)s, old) - (U8*)s;
618 /* Get the added string: */
619 last_str = newSVpv(s + old, l);
620 if (deltanext == 0 && pos_before == b) {
621 /* What was added is a constant string */
623 SvGROW(last_str, (mincount * l) + 1);
624 repeatcpy(SvPVX(last_str) + l,
625 SvPVX(last_str), l, mincount - 1);
626 SvCUR(last_str) *= mincount;
627 /* Add additional parts. */
628 SvCUR_set(data->last_found,
629 SvCUR(data->last_found) - l);
630 sv_catsv(data->last_found, last_str);
631 data->last_end += l * (mincount - 1);
635 /* It is counted once already... */
636 data->pos_min += minnext * (mincount - counted);
637 data->pos_delta += - counted * deltanext +
638 (minnext + deltanext) * maxcount - minnext * mincount;
639 if (mincount != maxcount) {
641 if (mincount && last_str) {
642 sv_setsv(data->last_found, last_str);
643 data->last_end = data->pos_min;
644 data->last_start_min =
645 data->pos_min - CHR_SVLEN(last_str);
646 data->last_start_max = is_inf
648 : data->pos_min + data->pos_delta
649 - CHR_SVLEN(last_str);
651 data->longest = &(data->longest_float);
653 SvREFCNT_dec(last_str);
655 if (data && (fl & SF_HAS_EVAL))
656 data->flags |= SF_HAS_EVAL;
658 if (OP(oscan) != CURLYX) {
659 while (PL_regkind[(U8)OP(next = regnext(oscan))] == NOTHING
661 NEXT_OFF(oscan) += NEXT_OFF(next);
664 default: /* REF only? */
665 if (flags & SCF_DO_SUBSTR) {
667 data->longest = &(data->longest_float);
669 is_inf = is_inf_internal = 1;
673 else if (strchr(PL_simple,OP(scan)) || PL_regkind[(U8)OP(scan)] == ANYUTF8) {
674 if (flags & SCF_DO_SUBSTR) {
680 else if (PL_regkind[(U8)OP(scan)] == EOL && flags & SCF_DO_SUBSTR) {
681 data->flags |= (OP(scan) == MEOL
685 else if (PL_regkind[(U8)OP(scan)] == BRANCHJ
686 && (scan->flags || data)
687 && (OP(scan) == IFMATCH || OP(scan) == UNLESSM)) {
688 I32 deltanext, minnext;
692 next = regnext(scan);
693 nscan = NEXTOPER(NEXTOPER(scan));
694 minnext = study_chunk(&nscan, &deltanext, last, &data_fake, 0);
697 FAIL("variable length lookbehind not implemented");
699 else if (minnext > U8_MAX) {
700 FAIL2("lookbehind longer than %d not implemented", U8_MAX);
702 scan->flags = minnext;
704 if (data && data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
706 if (data && (data_fake.flags & SF_HAS_EVAL))
707 data->flags |= SF_HAS_EVAL;
709 else if (OP(scan) == OPEN) {
712 else if (OP(scan) == CLOSE && ARG(scan) == is_par) {
713 next = regnext(scan);
715 if ( next && (OP(next) != WHILEM) && next < last)
716 is_par = 0; /* Disable optimization */
718 else if (OP(scan) == EVAL) {
720 data->flags |= SF_HAS_EVAL;
722 else if (OP(scan) == LOGICAL && scan->flags == 2) { /* Embedded */
723 if (flags & SCF_DO_SUBSTR) {
725 data->longest = &(data->longest_float);
727 is_inf = is_inf_internal = 1;
729 /* Else: zero-length, ignore. */
730 scan = regnext(scan);
735 *deltap = is_inf_internal ? I32_MAX : delta;
736 if (flags & SCF_DO_SUBSTR && is_inf)
737 data->pos_delta = I32_MAX - data->pos_min;
740 if (is_par && pars==1 && data) {
741 data->flags |= SF_IN_PAR;
742 data->flags &= ~SF_HAS_PAR;
744 else if (pars && data) {
745 data->flags |= SF_HAS_PAR;
746 data->flags &= ~SF_IN_PAR;
752 add_data(I32 n, char *s)
755 if (PL_regcomp_rx->data) {
756 Renewc(PL_regcomp_rx->data,
757 sizeof(*PL_regcomp_rx->data) + sizeof(void*) * (PL_regcomp_rx->data->count + n - 1),
758 char, struct reg_data);
759 Renew(PL_regcomp_rx->data->what, PL_regcomp_rx->data->count + n, U8);
760 PL_regcomp_rx->data->count += n;
763 Newc(1207, PL_regcomp_rx->data, sizeof(*PL_regcomp_rx->data) + sizeof(void*) * (n - 1),
764 char, struct reg_data);
765 New(1208, PL_regcomp_rx->data->what, n, U8);
766 PL_regcomp_rx->data->count = n;
768 Copy(s, PL_regcomp_rx->data->what + PL_regcomp_rx->data->count - n, n, U8);
769 return PL_regcomp_rx->data->count - n;
777 char *s = PerlEnv_getenv("PERL_RE_COLORS");
780 PL_colors[0] = s = savepv(s);
788 PL_colors[i] = s = "";
798 - pregcomp - compile a regular expression into internal code
800 * We can't allocate space until we know how big the compiled form will be,
801 * but we can't compile it (and thus know how big it is) until we've got a
802 * place to put the code. So we cheat: we compile it twice, once with code
803 * generation turned off and size counting turned on, and once "for real".
804 * This also means that we don't allocate space until we are sure that the
805 * thing really will compile successfully, and we never have to move the
806 * code and thus invalidate pointers into it. (Note that it has to be in
807 * one piece because free() must be able to free it all.) [NB: not true in perl]
809 * Beware that the optimization-preparation code in here knows about some
810 * of the structure of the compiled regexp. [I'll say.]
813 pregcomp(char *exp, char *xend, PMOP *pm)
828 FAIL("NULL regexp argument");
830 if (PL_curcop == &PL_compiling ? (PL_hints & HINT_UTF8) : IN_UTF8)
831 PL_reg_flags |= RF_utf8;
835 PL_regprecomp = savepvn(exp, xend - exp);
836 DEBUG_r(if (!PL_colorset) reginitcolors());
837 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sCompiling%s RE `%s%*s%s'\n",
838 PL_colors[4],PL_colors[5],PL_colors[0],
839 xend - exp, PL_regprecomp, PL_colors[1]));
840 PL_regflags = pm->op_pmflags;
844 PL_seen_zerolen = *exp == '^' ? -1 : 0;
848 /* First pass: determine size, legality. */
849 PL_regcomp_parse = exp;
854 PL_regcode = &PL_regdummy;
855 regc((U8)REG_MAGIC, (char*)PL_regcode);
856 if (reg(0, &flags) == NULL) {
857 Safefree(PL_regprecomp);
858 PL_regprecomp = Nullch;
861 DEBUG_r(PerlIO_printf(Perl_debug_log, "size %d ", PL_regsize));
863 /* Small enough for pointer-storage convention?
864 If extralen==0, this means that we will not need long jumps. */
865 if (PL_regsize >= 0x10000L && PL_extralen)
866 PL_regsize += PL_extralen;
870 /* Allocate space and initialize. */
871 Newc(1001, r, sizeof(regexp) + (unsigned)PL_regsize * sizeof(regnode),
874 FAIL("regexp out of space");
876 r->prelen = xend - exp;
877 r->precomp = PL_regprecomp;
878 r->subbeg = r->subbase = NULL;
879 r->nparens = PL_regnpar - 1; /* set early to validate backrefs */
881 r->substrs = 0; /* Useful during FAIL. */
882 r->startp = 0; /* Useful during FAIL. */
883 r->endp = 0; /* Useful during FAIL. */
887 /* Second pass: emit code. */
888 PL_regcomp_parse = exp;
892 PL_regcode = r->program;
893 /* Store the count of eval-groups for security checks: */
894 PL_regcode->next_off = ((PL_seen_evals > U16_MAX) ? U16_MAX : PL_seen_evals);
895 regc((U8)REG_MAGIC, (char*) PL_regcode++);
897 if (reg(0, &flags) == NULL)
900 /* Dig out information for optimizations. */
901 r->reganch = pm->op_pmflags & PMf_COMPILETIME;
902 pm->op_pmflags = PL_regflags;
904 r->reganch |= ROPT_UTF8;
905 r->regstclass = NULL;
906 if (PL_regnaughty >= 10) /* Probably an expensive pattern. */
907 r->reganch |= ROPT_NAUGHTY;
908 scan = r->program + 1; /* First BRANCH. */
910 /* XXXX To minimize changes to RE engine we always allocate
911 3-units-long substrs field. */
912 Newz(1004, r->substrs, 1, struct reg_substr_data);
914 if (OP(scan) != BRANCH) { /* Only one top-level choice. */
917 STRLEN longest_float_length, longest_fixed_length;
919 StructCopy(&zero_scan_data, &data, scan_data_t);
921 /* Skip introductions and multiplicators >= 1. */
922 while ((OP(first) == OPEN && (sawopen = 1)) ||
923 (OP(first) == BRANCH && OP(regnext(first)) != BRANCH) ||
924 (OP(first) == PLUS) ||
925 (OP(first) == MINMOD) ||
926 (PL_regkind[(U8)OP(first)] == CURLY && ARG1(first) > 0) ) {
927 if (OP(first) == PLUS)
930 first += regarglen[(U8)OP(first)];
931 first = NEXTOPER(first);
934 /* Starting-point info. */
936 if (OP(first) == EXACT); /* Empty, get anchored substr later. */
937 else if (strchr(PL_simple+4,OP(first)))
938 r->regstclass = first;
939 else if (PL_regkind[(U8)OP(first)] == BOUND ||
940 PL_regkind[(U8)OP(first)] == NBOUND)
941 r->regstclass = first;
942 else if (PL_regkind[(U8)OP(first)] == BOL) {
943 r->reganch |= (OP(first) == MBOL ? ROPT_ANCH_MBOL: ROPT_ANCH_BOL);
944 first = NEXTOPER(first);
947 else if (OP(first) == GPOS) {
948 r->reganch |= ROPT_ANCH_GPOS;
949 first = NEXTOPER(first);
952 else if ((OP(first) == STAR &&
953 PL_regkind[(U8)OP(NEXTOPER(first))] == REG_ANY) &&
954 !(r->reganch & ROPT_ANCH) )
956 /* turn .* into ^.* with an implied $*=1 */
957 r->reganch |= ROPT_ANCH_BOL | ROPT_IMPLICIT;
958 first = NEXTOPER(first);
961 if (sawplus && (!sawopen || !PL_regsawback))
962 r->reganch |= ROPT_SKIP; /* x+ must match 1st of run */
964 /* Scan is after the zeroth branch, first is atomic matcher. */
965 DEBUG_r(PerlIO_printf(Perl_debug_log, "first at %d\n",
968 * If there's something expensive in the r.e., find the
969 * longest literal string that must appear and make it the
970 * regmust. Resolve ties in favor of later strings, since
971 * the regstart check works with the beginning of the r.e.
972 * and avoiding duplication strengthens checking. Not a
973 * strong reason, but sufficient in the absence of others.
974 * [Now we resolve ties in favor of the earlier string if
975 * it happens that c_offset_min has been invalidated, since the
976 * earlier string may buy us something the later one won't.]
980 data.longest_fixed = newSVpv("",0);
981 data.longest_float = newSVpv("",0);
982 data.last_found = newSVpv("",0);
983 data.longest = &(data.longest_fixed);
986 minlen = study_chunk(&first, &fake, scan + PL_regsize, /* Up to end */
987 &data, SCF_DO_SUBSTR);
988 if ( PL_regnpar == 1 && data.longest == &(data.longest_fixed)
989 && data.last_start_min == 0 && data.last_end > 0
991 && (!(PL_regseen & REG_SEEN_GPOS) || (r->reganch & ROPT_ANCH_GPOS)))
992 r->reganch |= ROPT_CHECK_ALL;
994 SvREFCNT_dec(data.last_found);
996 longest_float_length = CHR_SVLEN(data.longest_float);
997 if (longest_float_length
998 || (data.flags & SF_FL_BEFORE_EOL
999 && (!(data.flags & SF_FL_BEFORE_MEOL)
1000 || (PL_regflags & PMf_MULTILINE)))) {
1001 if (SvCUR(data.longest_fixed) /* ok to leave SvCUR */
1002 && data.offset_fixed == data.offset_float_min
1003 && SvCUR(data.longest_fixed) == SvCUR(data.longest_float))
1004 goto remove_float; /* As in (a)+. */
1006 r->float_substr = data.longest_float;
1007 r->float_min_offset = data.offset_float_min;
1008 r->float_max_offset = data.offset_float_max;
1009 fbm_compile(r->float_substr, 0);
1010 BmUSEFUL(r->float_substr) = 100;
1011 if (data.flags & SF_FL_BEFORE_EOL /* Cannot have SEOL and MULTI */
1012 && (!(data.flags & SF_FL_BEFORE_MEOL)
1013 || (PL_regflags & PMf_MULTILINE)))
1014 SvTAIL_on(r->float_substr);
1018 r->float_substr = Nullsv;
1019 SvREFCNT_dec(data.longest_float);
1020 longest_float_length = 0;
1023 longest_fixed_length = CHR_SVLEN(data.longest_fixed);
1024 if (longest_fixed_length
1025 || (data.flags & SF_FIX_BEFORE_EOL /* Cannot have SEOL and MULTI */
1026 && (!(data.flags & SF_FIX_BEFORE_MEOL)
1027 || (PL_regflags & PMf_MULTILINE)))) {
1028 r->anchored_substr = data.longest_fixed;
1029 r->anchored_offset = data.offset_fixed;
1030 fbm_compile(r->anchored_substr, 0);
1031 BmUSEFUL(r->anchored_substr) = 100;
1032 if (data.flags & SF_FIX_BEFORE_EOL /* Cannot have SEOL and MULTI */
1033 && (!(data.flags & SF_FIX_BEFORE_MEOL)
1034 || (PL_regflags & PMf_MULTILINE)))
1035 SvTAIL_on(r->anchored_substr);
1038 r->anchored_substr = Nullsv;
1039 SvREFCNT_dec(data.longest_fixed);
1040 longest_fixed_length = 0;
1043 /* A temporary algorithm prefers floated substr to fixed one to dig more info. */
1044 if (longest_fixed_length > longest_float_length) {
1045 r->check_substr = r->anchored_substr;
1046 r->check_offset_min = r->check_offset_max = r->anchored_offset;
1047 if (r->reganch & ROPT_ANCH_SINGLE)
1048 r->reganch |= ROPT_NOSCAN;
1051 r->check_substr = r->float_substr;
1052 r->check_offset_min = data.offset_float_min;
1053 r->check_offset_max = data.offset_float_max;
1057 /* Several toplevels. Best we can is to set minlen. */
1060 DEBUG_r(PerlIO_printf(Perl_debug_log, "\n"));
1061 scan = r->program + 1;
1062 minlen = study_chunk(&scan, &fake, scan + PL_regsize, NULL, 0);
1063 r->check_substr = r->anchored_substr = r->float_substr = Nullsv;
1067 if (PL_regseen & REG_SEEN_GPOS)
1068 r->reganch |= ROPT_GPOS_SEEN;
1069 if (PL_regseen & REG_SEEN_LOOKBEHIND)
1070 r->reganch |= ROPT_LOOKBEHIND_SEEN;
1071 if (PL_regseen & REG_SEEN_EVAL)
1072 r->reganch |= ROPT_EVAL_SEEN;
1073 Newz(1002, r->startp, PL_regnpar, char*);
1074 Newz(1002, r->endp, PL_regnpar, char*);
1075 DEBUG_r(regdump(r));
1080 - reg - regular expression, i.e. main body or parenthesized thing
1082 * Caller must absorb opening parenthesis.
1084 * Combining parenthesis handling with the base level of regular expression
1085 * is a trifle forced, but the need to tie the tails of the branches to what
1086 * follows makes it hard to avoid.
1089 reg(I32 paren, I32 *flagp)
1090 /* paren: Parenthesized? 0=top, 1=(, inside: changed to letter. */
1093 register regnode *ret; /* Will be the head of the group. */
1094 register regnode *br;
1095 register regnode *lastbr;
1096 register regnode *ender = 0;
1097 register I32 parno = 0;
1098 I32 flags, oregflags = PL_regflags, have_branch = 0, open = 0;
1101 *flagp = 0; /* Tentatively. */
1103 /* Make an OPEN node, if parenthesized. */
1105 if (*PL_regcomp_parse == '?') {
1106 U16 posflags = 0, negflags = 0;
1107 U16 *flagsp = &posflags;
1111 paren = *PL_regcomp_parse++;
1112 ret = NULL; /* For look-ahead/behind. */
1115 PL_regseen |= REG_SEEN_LOOKBEHIND;
1116 if (*PL_regcomp_parse == '!')
1118 if (*PL_regcomp_parse != '=' && *PL_regcomp_parse != '!')
1129 FAIL2("Sequence (?%c...) not implemented", (int)paren);
1132 while (*PL_regcomp_parse && *PL_regcomp_parse != ')')
1134 if (*PL_regcomp_parse != ')')
1135 FAIL("Sequence (?#... not terminated");
1141 paren = *PL_regcomp_parse++;
1146 I32 count = 1, n = 0;
1148 char *s = PL_regcomp_parse;
1150 OP_4tree *sop, *rop;
1153 PL_regseen |= REG_SEEN_EVAL;
1154 while (count && (c = *PL_regcomp_parse)) {
1155 if (c == '\\' && PL_regcomp_parse[1])
1163 if (*PL_regcomp_parse != ')')
1164 FAIL("Sequence (?{...}) not terminated or not {}-balanced");
1168 if (PL_regcomp_parse - 1 - s)
1169 sv = newSVpv(s, PL_regcomp_parse - 1 - s);
1171 sv = newSVpv("", 0);
1173 rop = sv_compile_2op(sv, &sop, "re", &av);
1175 n = add_data(3, "nop");
1176 PL_regcomp_rx->data->data[n] = (void*)rop;
1177 PL_regcomp_rx->data->data[n+1] = (void*)sop;
1178 PL_regcomp_rx->data->data[n+2] = (void*)av;
1181 else { /* First pass */
1182 if (PL_reginterp_cnt < ++PL_seen_evals
1183 && PL_curcop != &PL_compiling)
1184 /* No compiled RE interpolated, has runtime
1185 components ===> unsafe. */
1186 FAIL("Eval-group not allowed at runtime, use re 'eval'");
1188 FAIL("Eval-group in insecure regular expression");
1193 ret = reg_node(LOGICAL);
1196 regtail(ret, reganode(EVAL, n));
1199 return reganode(EVAL, n);
1203 if (PL_regcomp_parse[0] == '?') {
1204 if (PL_regcomp_parse[1] == '=' || PL_regcomp_parse[1] == '!'
1205 || PL_regcomp_parse[1] == '<'
1206 || PL_regcomp_parse[1] == '{') { /* Lookahead or eval. */
1209 ret = reg_node(LOGICAL);
1212 regtail(ret, reg(1, &flag));
1216 else if (PL_regcomp_parse[0] >= '1' && PL_regcomp_parse[0] <= '9' ) {
1217 parno = atoi(PL_regcomp_parse++);
1219 while (isDIGIT(*PL_regcomp_parse))
1221 ret = reganode(GROUPP, parno);
1222 if ((c = *nextchar()) != ')')
1223 FAIL2("Switch (?(number%c not recognized", c);
1225 regtail(ret, reganode(IFTHEN, 0));
1226 br = regbranch(&flags, 1);
1228 br = reganode(LONGJMP, 0);
1230 regtail(br, reganode(LONGJMP, 0));
1235 lastbr = reganode(IFTHEN, 0); /* Fake one for optimizer. */
1236 regbranch(&flags, 1);
1237 regtail(ret, lastbr);
1245 FAIL("Switch (?(condition)... contains too many branches");
1246 ender = reg_node(TAIL);
1249 regtail(lastbr, ender);
1250 regtail(NEXTOPER(NEXTOPER(lastbr)), ender);
1253 regtail(ret, ender);
1257 FAIL2("Unknown condition for (?(%.2s", PL_regcomp_parse);
1261 FAIL("Sequence (? incomplete");
1266 while (*PL_regcomp_parse && strchr("iogcmsx", *PL_regcomp_parse)) {
1267 if (*PL_regcomp_parse != 'o')
1268 pmflag(flagsp, *PL_regcomp_parse);
1271 if (*PL_regcomp_parse == '-') {
1276 PL_regflags |= posflags;
1277 PL_regflags &= ~negflags;
1278 if (*PL_regcomp_parse == ':') {
1284 if (*PL_regcomp_parse != ')')
1285 FAIL2("Sequence (?%c...) not recognized", *PL_regcomp_parse);
1294 ret = reganode(OPEN, parno);
1301 /* Pick up the branches, linking them together. */
1302 br = regbranch(&flags, 1);
1305 if (*PL_regcomp_parse == '|') {
1306 if (!SIZE_ONLY && PL_extralen) {
1307 reginsert(BRANCHJ, br);
1310 reginsert(BRANCH, br);
1313 PL_extralen += 1; /* For BRANCHJ-BRANCH. */
1315 else if (paren == ':') {
1316 *flagp |= flags&SIMPLE;
1318 if (open) { /* Starts with OPEN. */
1319 regtail(ret, br); /* OPEN -> first. */
1321 else if (paren != '?') /* Not Conditional */
1325 *flagp |= flags&SPSTART;
1327 while (*PL_regcomp_parse == '|') {
1328 if (!SIZE_ONLY && PL_extralen) {
1329 ender = reganode(LONGJMP,0);
1330 regtail(NEXTOPER(NEXTOPER(lastbr)), ender); /* Append to the previous. */
1333 PL_extralen += 2; /* Account for LONGJMP. */
1335 br = regbranch(&flags, 0);
1338 regtail(lastbr, br); /* BRANCH -> BRANCH. */
1342 *flagp |= flags&SPSTART;
1345 if (have_branch || paren != ':') {
1346 /* Make a closing node, and hook it on the end. */
1349 ender = reg_node(TAIL);
1352 ender = reganode(CLOSE, parno);
1358 *flagp &= ~HASWIDTH;
1361 ender = reg_node(SUCCEED);
1364 ender = reg_node(END);
1367 regtail(lastbr, ender);
1370 /* Hook the tails of the branches to the closing node. */
1371 for (br = ret; br != NULL; br = regnext(br)) {
1372 regoptail(br, ender);
1379 static char parens[] = "=!<,>";
1381 if (paren && (p = strchr(parens, paren))) {
1382 int node = ((p - parens) % 2) ? UNLESSM : IFMATCH;
1383 int flag = (p - parens) > 1;
1386 node = SUSPEND, flag = 0;
1387 reginsert(node,ret);
1389 regtail(ret, reg_node(TAIL));
1393 /* Check for proper termination. */
1395 PL_regflags = oregflags;
1396 if (PL_regcomp_parse >= PL_regxend || *nextchar() != ')') {
1397 FAIL("unmatched () in regexp");
1400 else if (!paren && PL_regcomp_parse < PL_regxend) {
1401 if (*PL_regcomp_parse == ')') {
1402 FAIL("unmatched () in regexp");
1405 FAIL("junk on end of regexp"); /* "Can't happen". */
1413 - regbranch - one alternative of an | operator
1415 * Implements the concatenation operator.
1418 regbranch(I32 *flagp, I32 first)
1421 register regnode *ret;
1422 register regnode *chain = NULL;
1423 register regnode *latest;
1424 I32 flags = 0, c = 0;
1429 if (!SIZE_ONLY && PL_extralen)
1430 ret = reganode(BRANCHJ,0);
1432 ret = reg_node(BRANCH);
1435 if (!first && SIZE_ONLY)
1436 PL_extralen += 1; /* BRANCHJ */
1438 *flagp = WORST; /* Tentatively. */
1442 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != '|' && *PL_regcomp_parse != ')') {
1444 latest = regpiece(&flags);
1445 if (latest == NULL) {
1446 if (flags & TRYAGAIN)
1450 else if (ret == NULL)
1452 *flagp |= flags&HASWIDTH;
1453 if (chain == NULL) /* First piece. */
1454 *flagp |= flags&SPSTART;
1457 regtail(chain, latest);
1462 if (chain == NULL) { /* Loop ran zero times. */
1463 chain = reg_node(NOTHING);
1468 *flagp |= flags&SIMPLE;
1475 - regpiece - something followed by possible [*+?]
1477 * Note that the branching code sequences used for ? and the general cases
1478 * of * and + are somewhat optimized: they use the same NOTHING node as
1479 * both the endmarker for their branch list and the body of the last branch.
1480 * It might seem that this node could be dispensed with entirely, but the
1481 * endmarker role is not redundant.
1484 regpiece(I32 *flagp)
1487 register regnode *ret;
1489 register char *next;
1491 char *origparse = PL_regcomp_parse;
1494 I32 max = REG_INFTY;
1496 ret = regatom(&flags);
1498 if (flags & TRYAGAIN)
1503 op = *PL_regcomp_parse;
1505 if (op == '{' && regcurly(PL_regcomp_parse)) {
1506 next = PL_regcomp_parse + 1;
1508 while (isDIGIT(*next) || *next == ',') {
1517 if (*next == '}') { /* got one */
1521 min = atoi(PL_regcomp_parse);
1525 maxpos = PL_regcomp_parse;
1527 if (!max && *maxpos != '0')
1528 max = REG_INFTY; /* meaning "infinity" */
1529 else if (max >= REG_INFTY)
1530 FAIL2("Quantifier in {,} bigger than %d", REG_INFTY - 1);
1531 PL_regcomp_parse = next;
1535 if ((flags&SIMPLE)) {
1536 PL_regnaughty += 2 + PL_regnaughty / 2;
1537 reginsert(CURLY, ret);
1540 PL_regnaughty += 4 + PL_regnaughty; /* compound interest */
1541 regtail(ret, reg_node(WHILEM));
1542 if (!SIZE_ONLY && PL_extralen) {
1543 reginsert(LONGJMP,ret);
1544 reginsert(NOTHING,ret);
1545 NEXT_OFF(ret) = 3; /* Go over LONGJMP. */
1547 reginsert(CURLYX,ret);
1548 if (!SIZE_ONLY && PL_extralen)
1549 NEXT_OFF(ret) = 3; /* Go over NOTHING to LONGJMP. */
1550 regtail(ret, reg_node(NOTHING));
1560 if (max && max < min)
1561 FAIL("Can't do {n,m} with n > m");
1576 #if 0 /* Now runtime fix should be reliable. */
1577 if (!(flags&HASWIDTH) && op != '?')
1578 FAIL("regexp *+ operand could be empty");
1583 *flagp = (op != '+') ? (WORST|SPSTART|HASWIDTH) : (WORST|HASWIDTH);
1585 if (op == '*' && (flags&SIMPLE)) {
1586 reginsert(STAR, ret);
1590 else if (op == '*') {
1594 else if (op == '+' && (flags&SIMPLE)) {
1595 reginsert(PLUS, ret);
1599 else if (op == '+') {
1603 else if (op == '?') {
1608 if (ckWARN(WARN_UNSAFE) && !SIZE_ONLY && !(flags&HASWIDTH) && max > REG_INFTY/3) {
1609 warner(WARN_UNSAFE, "%.*s matches null string many times",
1610 PL_regcomp_parse - origparse, origparse);
1613 if (*PL_regcomp_parse == '?') {
1615 reginsert(MINMOD, ret);
1616 regtail(ret, ret + NODE_STEP_REGNODE);
1618 if (ISMULT2(PL_regcomp_parse))
1619 FAIL("nested *?+ in regexp");
1625 - regatom - the lowest level
1627 * Optimization: gobbles an entire sequence of ordinary characters so that
1628 * it can turn them into a single node, which is smaller to store and
1629 * faster to run. Backslashed characters are exceptions, each becoming a
1630 * separate node; the code is simpler that way and it's not worth fixing.
1632 * [Yes, it is worth fixing, some scripts can run twice the speed.]
1638 register regnode *ret = 0;
1641 *flagp = WORST; /* Tentatively. */
1644 switch (*PL_regcomp_parse) {
1648 if (PL_regflags & PMf_MULTILINE)
1649 ret = reg_node(MBOL);
1650 else if (PL_regflags & PMf_SINGLELINE)
1651 ret = reg_node(SBOL);
1653 ret = reg_node(BOL);
1656 if (PL_regcomp_parse[1])
1659 if (PL_regflags & PMf_MULTILINE)
1660 ret = reg_node(MEOL);
1661 else if (PL_regflags & PMf_SINGLELINE)
1662 ret = reg_node(SEOL);
1664 ret = reg_node(EOL);
1669 if (PL_regflags & PMf_SINGLELINE)
1670 ret = reg_node(SANYUTF8);
1672 ret = reg_node(ANYUTF8);
1676 if (PL_regflags & PMf_SINGLELINE)
1677 ret = reg_node(SANY);
1679 ret = reg_node(REG_ANY);
1680 *flagp |= HASWIDTH|SIMPLE;
1686 ret = (UTF ? regclassutf8() : regclass());
1687 if (*PL_regcomp_parse != ']')
1688 FAIL("unmatched [] in regexp");
1690 *flagp |= HASWIDTH|SIMPLE;
1694 ret = reg(1, &flags);
1696 if (flags & TRYAGAIN)
1700 *flagp |= flags&(HASWIDTH|SPSTART|SIMPLE);
1704 if (flags & TRYAGAIN) {
1708 FAIL2("internal urp in regexp at /%s/", PL_regcomp_parse);
1709 /* Supposed to be caught earlier. */
1712 if (!regcurly(PL_regcomp_parse)) {
1720 FAIL("?+*{} follows nothing in regexp");
1723 switch (*++PL_regcomp_parse) {
1726 ret = reg_node(SBOL);
1731 ret = reg_node(GPOS);
1732 PL_regseen |= REG_SEEN_GPOS;
1737 ret = reg_node(SEOL);
1742 ret = reg_node(EOS);
1744 PL_seen_zerolen++; /* Do not optimize RE away */
1748 ret = reg_node(SANY);
1749 *flagp |= HASWIDTH|SIMPLE;
1753 ret = reg_node(CLUMP);
1756 if (UTF && !PL_utf8_mark)
1757 is_utf8_mark((U8*)"~"); /* preload table */
1762 ? (LOC ? ALNUMLUTF8 : ALNUMUTF8)
1763 : (LOC ? ALNUML : ALNUM));
1764 *flagp |= HASWIDTH|SIMPLE;
1766 if (UTF && !PL_utf8_alnum)
1767 is_utf8_alnum((U8*)"a"); /* preload table */
1772 ? (LOC ? NALNUMLUTF8 : NALNUMUTF8)
1773 : (LOC ? NALNUML : NALNUM));
1774 *flagp |= HASWIDTH|SIMPLE;
1776 if (UTF && !PL_utf8_alnum)
1777 is_utf8_alnum((U8*)"a"); /* preload table */
1781 PL_regseen |= REG_SEEN_LOOKBEHIND;
1784 ? (LOC ? BOUNDLUTF8 : BOUNDUTF8)
1785 : (LOC ? BOUNDL : BOUND));
1788 if (UTF && !PL_utf8_alnum)
1789 is_utf8_alnum((U8*)"a"); /* preload table */
1793 PL_regseen |= REG_SEEN_LOOKBEHIND;
1796 ? (LOC ? NBOUNDLUTF8 : NBOUNDUTF8)
1797 : (LOC ? NBOUNDL : NBOUND));
1800 if (UTF && !PL_utf8_alnum)
1801 is_utf8_alnum((U8*)"a"); /* preload table */
1806 ? (LOC ? SPACELUTF8 : SPACEUTF8)
1807 : (LOC ? SPACEL : SPACE));
1808 *flagp |= HASWIDTH|SIMPLE;
1810 if (UTF && !PL_utf8_space)
1811 is_utf8_space((U8*)" "); /* preload table */
1816 ? (LOC ? NSPACELUTF8 : NSPACEUTF8)
1817 : (LOC ? NSPACEL : NSPACE));
1818 *flagp |= HASWIDTH|SIMPLE;
1820 if (UTF && !PL_utf8_space)
1821 is_utf8_space((U8*)" "); /* preload table */
1824 ret = reg_node(UTF ? DIGITUTF8 : DIGIT);
1825 *flagp |= HASWIDTH|SIMPLE;
1827 if (UTF && !PL_utf8_digit)
1828 is_utf8_digit((U8*)"1"); /* preload table */
1831 ret = reg_node(UTF ? NDIGITUTF8 : NDIGIT);
1832 *flagp |= HASWIDTH|SIMPLE;
1834 if (UTF && !PL_utf8_digit)
1835 is_utf8_digit((U8*)"1"); /* preload table */
1839 { /* a lovely hack--pretend we saw [\pX] instead */
1840 char* oldregxend = PL_regxend;
1842 if (PL_regcomp_parse[1] == '{') {
1843 PL_regxend = strchr(PL_regcomp_parse, '}');
1845 FAIL("Missing right brace on \\p{}");
1849 PL_regxend = PL_regcomp_parse + 2;
1852 ret = regclassutf8();
1854 PL_regxend = oldregxend;
1857 *flagp |= HASWIDTH|SIMPLE;
1870 case '1': case '2': case '3': case '4':
1871 case '5': case '6': case '7': case '8': case '9':
1873 I32 num = atoi(PL_regcomp_parse);
1875 if (num > 9 && num >= PL_regnpar)
1878 if (!SIZE_ONLY && num > PL_regcomp_rx->nparens)
1879 FAIL("reference to nonexistent group");
1882 ? (LOC ? REFFL : REFF)
1885 while (isDIGIT(*PL_regcomp_parse))
1893 if (PL_regcomp_parse >= PL_regxend)
1894 FAIL("trailing \\ in regexp");
1897 /* Do not generate `unrecognized' warnings here, we fall
1898 back into the quick-grab loop below */
1904 if (PL_regflags & PMf_EXTENDED) {
1905 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != '\n') PL_regcomp_parse++;
1906 if (PL_regcomp_parse < PL_regxend)
1922 ? (LOC ? EXACTFL : EXACTF)
1924 s = (char *) OPERAND(ret);
1925 regc(0, s++); /* save spot for len */
1926 for (len = 0, p = PL_regcomp_parse - 1;
1927 len < 127 && p < PL_regxend;
1932 if (PL_regflags & PMf_EXTENDED)
1933 p = regwhite(p, PL_regxend);
1987 char* e = strchr(p, '}');
1990 FAIL("Missing right brace on \\x{}");
1992 ender = scan_hex(p + 1, e - p, &numlen);
1993 if (numlen + len >= 127) { /* numlen is generous */
2000 FAIL("Can't use \\x{} without 'use utf8' declaration");
2003 ender = scan_hex(p, 2, &numlen);
2009 ender = UCHARAT(p++);
2010 ender = toCTRL(ender);
2012 case '0': case '1': case '2': case '3':case '4':
2013 case '5': case '6': case '7': case '8':case '9':
2015 (isDIGIT(p[1]) && atoi(p) >= PL_regnpar) ) {
2016 ender = scan_oct(p, 3, &numlen);
2025 if (p >= PL_regxend)
2026 FAIL("trailing \\ in regexp");
2029 if (!SIZE_ONLY && ckWARN(WARN_UNSAFE) && isALPHA(*p))
2031 "/%.127s/: Unrecognized escape \\%c passed through",
2034 goto normal_default;
2039 if ((*p & 0xc0) == 0xc0 && UTF) {
2040 ender = utf8_to_uv((U8*)p, &numlen);
2047 if (PL_regflags & PMf_EXTENDED)
2048 p = regwhite(p, PL_regxend);
2051 ender = toLOWER_LC_uni(ender);
2053 ender = toLOWER_uni(ender);
2055 if (ISMULT2(p)) { /* Back off on ?+*. */
2058 else if (ender >= 0x80 && UTF) {
2059 reguni(ender, s, &numlen);
2069 if (ender >= 0x80 && UTF) {
2070 reguni(ender, s, &numlen);
2078 PL_regcomp_parse = p - 1;
2081 FAIL("internal disaster in regexp");
2087 *OPERAND(ret) = len;
2090 PL_regsize += (len + 2 + sizeof(regnode) - 1) / sizeof(regnode);
2093 PL_regcode += (len + 2 + sizeof(regnode) - 1) / sizeof(regnode);
2103 regwhite(char *p, char *e)
2108 else if (*p == '#') {
2111 } while (p < e && *p != '\n');
2119 /* parse POSIX character classes like [[:foo:]] */
2121 regpposixcc(I32 value)
2126 if (value == '[' && PL_regcomp_parse + 1 < PL_regxend &&
2127 /* I smell either [: or [= or [. -- POSIX has been here, right? */
2128 (*PL_regcomp_parse == ':' ||
2129 *PL_regcomp_parse == '=' ||
2130 *PL_regcomp_parse == '.')) {
2131 char c = *PL_regcomp_parse;
2132 char* s = PL_regcomp_parse++;
2134 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != c)
2136 if (PL_regcomp_parse == PL_regxend)
2137 /* Grandfather lone [:, [=, [. */
2138 PL_regcomp_parse = s;
2140 PL_regcomp_parse++; /* skip over the c */
2141 if (*PL_regcomp_parse == ']') {
2142 /* Not Implemented Yet.
2143 * (POSIX Extended Character Classes, that is)
2144 * The text between e.g. [: and :] would start
2145 * at s + 1 and stop at regcomp_parse - 2. */
2146 if (ckWARN(WARN_UNSAFE) && !SIZE_ONLY)
2148 "Character class syntax [%c %c] is reserved for future extensions", c, c);
2149 PL_regcomp_parse++; /* skip over the ending ] */
2162 register char *opnd, *s;
2164 register I32 lastvalue = 1234;
2165 register I32 range = 0;
2166 register regnode *ret;
2170 s = opnd = (char *) OPERAND(PL_regcode);
2171 ret = reg_node(ANYOF);
2172 for (value = 0; value < 33; value++)
2174 if (*PL_regcomp_parse == '^') { /* Complement of range. */
2178 *opnd |= ANYOF_INVERT;
2181 PL_regcode += ANY_SKIP;
2183 *opnd |= ANYOF_FOLD;
2185 *opnd |= ANYOF_LOCALE;
2188 PL_regsize += ANY_SKIP;
2190 if (*PL_regcomp_parse == ']' || *PL_regcomp_parse == '-')
2191 goto skipcond; /* allow 1st char to be ] or - */
2192 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != ']') {
2194 value = UCHARAT(PL_regcomp_parse++);
2196 (void)regpposixcc(value); /* ignore the return value for now */
2197 else if (value == '\\') {
2198 value = UCHARAT(PL_regcomp_parse++);
2203 *opnd |= ANYOF_ALNUML;
2205 for (value = 0; value < 256; value++)
2207 ANYOF_SET(opnd, value);
2215 *opnd |= ANYOF_NALNUML;
2217 for (value = 0; value < 256; value++)
2218 if (!isALNUM(value))
2219 ANYOF_SET(opnd, value);
2227 *opnd |= ANYOF_SPACEL;
2229 for (value = 0; value < 256; value++)
2231 ANYOF_SET(opnd, value);
2239 *opnd |= ANYOF_NSPACEL;
2241 for (value = 0; value < 256; value++)
2242 if (!isSPACE(value))
2243 ANYOF_SET(opnd, value);
2250 for (value = '0'; value <= '9'; value++)
2251 ANYOF_SET(opnd, value);
2257 for (value = 0; value < '0'; value++)
2258 ANYOF_SET(opnd, value);
2259 for (value = '9' + 1; value < 256; value++)
2260 ANYOF_SET(opnd, value);
2286 value = scan_hex(PL_regcomp_parse, 2, &numlen);
2287 PL_regcomp_parse += numlen;
2290 value = UCHARAT(PL_regcomp_parse++);
2291 value = toCTRL(value);
2293 case '0': case '1': case '2': case '3': case '4':
2294 case '5': case '6': case '7': case '8': case '9':
2295 value = scan_oct(--PL_regcomp_parse, 3, &numlen);
2296 PL_regcomp_parse += numlen;
2301 if (lastvalue > value)
2302 FAIL("invalid [] range in regexp");
2307 if (*PL_regcomp_parse == '-' && PL_regcomp_parse+1 < PL_regxend &&
2308 PL_regcomp_parse[1] != ']') {
2311 continue; /* do it next time */
2316 if ((isLOWER(lastvalue) && isLOWER(value)) ||
2317 (isUPPER(lastvalue) && isUPPER(value)))
2320 if (isLOWER(lastvalue)) {
2321 for (i = lastvalue; i <= value; i++)
2325 for (i = lastvalue; i <= value; i++)
2332 for ( ; lastvalue <= value; lastvalue++)
2333 ANYOF_SET(opnd, lastvalue);
2337 /* optimize case-insensitive simple patterns (e.g. /[a-z]/i) */
2338 if (!SIZE_ONLY && (*opnd & (0xFF ^ ANYOF_INVERT)) == ANYOF_FOLD) {
2339 for (value = 0; value < 256; ++value) {
2340 if (ANYOF_TEST(opnd, value)) {
2341 I32 cf = PL_fold[value];
2342 ANYOF_SET(opnd, cf);
2345 *opnd &= ~ANYOF_FOLD;
2347 /* optimize inverted simple patterns (e.g. [^a-z]) */
2348 if (!SIZE_ONLY && (*opnd & 0xFF) == ANYOF_INVERT) {
2349 for (value = 0; value < 32; ++value)
2350 opnd[1 + value] ^= 0xFF;
2359 register char *opnd, *e;
2361 register U32 lastvalue = 123456;
2362 register I32 range = 0;
2363 register regnode *ret;
2370 if (*PL_regcomp_parse == '^') { /* Complement of range. */
2374 flags |= ANYOF_INVERT;
2378 flags |= ANYOF_FOLD;
2380 flags |= ANYOF_LOCALE;
2381 listsv = newSVpv("# comment\n",0);
2384 if (*PL_regcomp_parse == ']' || *PL_regcomp_parse == '-')
2385 goto skipcond; /* allow 1st char to be ] or - */
2387 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != ']') {
2389 value = utf8_to_uv((U8*)PL_regcomp_parse, &numlen);
2390 PL_regcomp_parse += numlen;
2393 (void)regpposixcc(value); /* ignore the return value for now */
2394 else if (value == '\\') {
2395 value = utf8_to_uv((U8*)PL_regcomp_parse, &numlen);
2396 PL_regcomp_parse += numlen;
2401 flags |= ANYOF_ALNUML;
2403 sv_catpvf(listsv, "+utf8::IsAlnum\n");
2410 flags |= ANYOF_NALNUML;
2413 "-utf8::IsAlpha\n-utf8::IsDigit\n0000\t%04x\n%04x\tffff\n",
2422 flags |= ANYOF_SPACEL;
2423 sv_catpvf(listsv, "+utf8::IsSpace\n");
2425 is_utf8_space((U8*)" ");
2432 flags |= ANYOF_NSPACEL;
2434 "!utf8::IsSpace\n");
2436 is_utf8_space((U8*)" ");
2442 sv_catpvf(listsv, "+utf8::IsDigit\n");
2449 "!utf8::IsDigit\n");
2455 if (*PL_regcomp_parse == '{') {
2456 e = strchr(PL_regcomp_parse++, '}');
2458 FAIL("Missing right brace on \\p{}");
2459 n = e - PL_regcomp_parse;
2462 e = PL_regcomp_parse;
2467 sv_catpvf(listsv, "+utf8::%.*s\n", n, PL_regcomp_parse);
2470 "!utf8::%.*s\n", n, PL_regcomp_parse);
2472 PL_regcomp_parse = e + 1;
2497 if (*PL_regcomp_parse == '{') {
2498 e = strchr(PL_regcomp_parse++, '}');
2500 FAIL("Missing right brace on \\x{}");
2501 value = scan_hex(PL_regcomp_parse + 1, e - PL_regcomp_parse, &numlen);
2502 PL_regcomp_parse = e + 1;
2505 value = scan_hex(PL_regcomp_parse, 2, &numlen);
2506 PL_regcomp_parse += numlen;
2510 value = UCHARAT(PL_regcomp_parse++);
2511 value = toCTRL(value);
2513 case '0': case '1': case '2': case '3': case '4':
2514 case '5': case '6': case '7': case '8': case '9':
2515 value = scan_oct(--PL_regcomp_parse, 3, &numlen);
2516 PL_regcomp_parse += numlen;
2521 if (lastvalue > value)
2522 FAIL("invalid [] range in regexp");
2524 sv_catpvf(listsv, "%04x\t%04x\n", lastvalue, value);
2530 if (*PL_regcomp_parse == '-' && PL_regcomp_parse+1 < PL_regxend &&
2531 PL_regcomp_parse[1] != ']') {
2534 continue; /* do it next time */
2537 sv_catpvf(listsv, "%04x\n", value);
2541 ret = reganode(ANYOFUTF8, 0);
2544 SV *rv = swash_init("utf8", "", listsv, 1, 0);
2545 SvREFCNT_dec(listsv);
2546 n = add_data(1,"s");
2547 PL_regcomp_rx->data->data[n] = (void*)rv;
2548 ARG1_SET(ret, flags);
2559 char* retval = PL_regcomp_parse++;
2562 if (*PL_regcomp_parse == '(' && PL_regcomp_parse[1] == '?' &&
2563 PL_regcomp_parse[2] == '#') {
2564 while (*PL_regcomp_parse && *PL_regcomp_parse != ')')
2569 if (PL_regflags & PMf_EXTENDED) {
2570 if (isSPACE(*PL_regcomp_parse)) {
2574 else if (*PL_regcomp_parse == '#') {
2575 while (*PL_regcomp_parse && *PL_regcomp_parse != '\n')
2586 - reg_node - emit a node
2588 STATIC regnode * /* Location. */
2592 register regnode *ret;
2593 register regnode *ptr;
2597 SIZE_ALIGN(PL_regsize);
2602 NODE_ALIGN_FILL(ret);
2604 FILL_ADVANCE_NODE(ptr, op);
2611 - reganode - emit a node with an argument
2613 STATIC regnode * /* Location. */
2614 reganode(U8 op, U32 arg)
2617 register regnode *ret;
2618 register regnode *ptr;
2622 SIZE_ALIGN(PL_regsize);
2627 NODE_ALIGN_FILL(ret);
2629 FILL_ADVANCE_NODE_ARG(ptr, op, arg);
2636 - regc - emit (if appropriate) a Unicode character
2639 reguni(UV uv, char* s, I32* lenp)
2644 *lenp = uv_to_utf8(tmpbuf, uv) - tmpbuf;
2647 *lenp = uv_to_utf8((U8*)s, uv) - (U8*)s;
2652 - regc - emit (if appropriate) a byte of code
2663 - reginsert - insert an operator in front of already-emitted operand
2665 * Means relocating the operand.
2668 reginsert(U8 op, regnode *opnd)
2671 register regnode *src;
2672 register regnode *dst;
2673 register regnode *place;
2674 register int offset = regarglen[(U8)op];
2676 /* (PL_regkind[(U8)op] == CURLY ? EXTRA_STEP_2ARGS : 0); */
2679 PL_regsize += NODE_STEP_REGNODE + offset;
2684 PL_regcode += NODE_STEP_REGNODE + offset;
2687 StructCopy(--src, --dst, regnode);
2689 place = opnd; /* Op node, where operand used to be. */
2690 src = NEXTOPER(place);
2691 FILL_ADVANCE_NODE(place, op);
2692 Zero(src, offset, regnode);
2696 - regtail - set the next-pointer at the end of a node chain of p to val.
2699 regtail(regnode *p, regnode *val)
2702 register regnode *scan;
2703 register regnode *temp;
2704 register I32 offset;
2709 /* Find last node. */
2712 temp = regnext(scan);
2718 if (reg_off_by_arg[OP(scan)]) {
2719 ARG_SET(scan, val - scan);
2722 NEXT_OFF(scan) = val - scan;
2727 - regoptail - regtail on operand of first argument; nop if operandless
2730 regoptail(regnode *p, regnode *val)
2733 /* "Operandless" and "op != BRANCH" are synonymous in practice. */
2734 if (p == NULL || SIZE_ONLY)
2736 if (PL_regkind[(U8)OP(p)] == BRANCH) {
2737 regtail(NEXTOPER(p), val);
2739 else if ( PL_regkind[(U8)OP(p)] == BRANCHJ) {
2740 regtail(NEXTOPER(NEXTOPER(p)), val);
2747 - regcurly - a little FSA that accepts {\d+,?\d*}
2750 regcurly(register char *s)
2769 dumpuntil(regnode *start, regnode *node, regnode *last, SV* sv, I32 l)
2772 register char op = EXACT; /* Arbitrary non-END op. */
2773 register regnode *next, *onode;
2775 while (op != END && (!last || node < last)) {
2776 /* While that wasn't END last time... */
2782 next = regnext(node);
2784 if (OP(node) == OPTIMIZED)
2787 PerlIO_printf(Perl_debug_log, "%4d:%*s%s", node - start,
2788 2*l + 1, "", SvPVX(sv));
2789 if (next == NULL) /* Next ptr. */
2790 PerlIO_printf(Perl_debug_log, "(0)");
2792 PerlIO_printf(Perl_debug_log, "(%d)", next - start);
2793 (void)PerlIO_putc(Perl_debug_log, '\n');
2795 if (PL_regkind[(U8)op] == BRANCHJ) {
2796 register regnode *nnode = (OP(next) == LONGJMP
2799 if (last && nnode > last)
2801 node = dumpuntil(start, NEXTOPER(NEXTOPER(node)), nnode, sv, l + 1);
2803 else if (PL_regkind[(U8)op] == BRANCH) {
2804 node = dumpuntil(start, NEXTOPER(node), next, sv, l + 1);
2806 else if ( op == CURLY) { /* `next' might be very big: optimizer */
2807 node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
2808 NEXTOPER(node) + EXTRA_STEP_2ARGS + 1, sv, l + 1);
2810 else if (PL_regkind[(U8)op] == CURLY && op != CURLYX) {
2811 node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
2814 else if ( op == PLUS || op == STAR) {
2815 node = dumpuntil(start, NEXTOPER(node), NEXTOPER(node) + 1, sv, l + 1);
2817 else if (op == ANYOF) {
2818 node = NEXTOPER(node);
2821 else if (PL_regkind[(U8)op] == EXACT) {
2822 /* Literal string, where present. */
2823 node += ((*OPERAND(node)) + 2 + sizeof(regnode) - 1) / sizeof(regnode);
2824 node = NEXTOPER(node);
2827 node = NEXTOPER(node);
2828 node += regarglen[(U8)op];
2830 if (op == CURLYX || op == OPEN)
2832 else if (op == WHILEM)
2835 #endif /* DEBUGGING */
2840 - regdump - dump a regexp onto Perl_debug_log in vaguely comprehensible form
2847 SV *sv = sv_newmortal();
2849 (void)dumpuntil(r->program, r->program + 1, NULL, sv, 0);
2851 /* Header fields of interest. */
2852 if (r->anchored_substr)
2853 PerlIO_printf(Perl_debug_log, "anchored `%s%s%s'%s at %d ",
2855 SvPVX(r->anchored_substr),
2857 SvTAIL(r->anchored_substr) ? "$" : "",
2858 r->anchored_offset);
2859 if (r->float_substr)
2860 PerlIO_printf(Perl_debug_log, "floating `%s%s%s'%s at %d..%u ",
2862 SvPVX(r->float_substr),
2864 SvTAIL(r->float_substr) ? "$" : "",
2865 r->float_min_offset, r->float_max_offset);
2866 if (r->check_substr)
2867 PerlIO_printf(Perl_debug_log,
2868 r->check_substr == r->float_substr
2869 ? "(checking floating" : "(checking anchored");
2870 if (r->reganch & ROPT_NOSCAN)
2871 PerlIO_printf(Perl_debug_log, " noscan");
2872 if (r->reganch & ROPT_CHECK_ALL)
2873 PerlIO_printf(Perl_debug_log, " isall");
2874 if (r->check_substr)
2875 PerlIO_printf(Perl_debug_log, ") ");
2877 if (r->regstclass) {
2878 regprop(sv, r->regstclass);
2879 PerlIO_printf(Perl_debug_log, "stclass `%s' ", SvPVX(sv));
2881 if (r->reganch & ROPT_ANCH) {
2882 PerlIO_printf(Perl_debug_log, "anchored");
2883 if (r->reganch & ROPT_ANCH_BOL)
2884 PerlIO_printf(Perl_debug_log, "(BOL)");
2885 if (r->reganch & ROPT_ANCH_MBOL)
2886 PerlIO_printf(Perl_debug_log, "(MBOL)");
2887 if (r->reganch & ROPT_ANCH_GPOS)
2888 PerlIO_printf(Perl_debug_log, "(GPOS)");
2889 PerlIO_putc(Perl_debug_log, ' ');
2891 if (r->reganch & ROPT_GPOS_SEEN)
2892 PerlIO_printf(Perl_debug_log, "GPOS ");
2893 if (r->reganch & ROPT_SKIP)
2894 PerlIO_printf(Perl_debug_log, "plus ");
2895 if (r->reganch & ROPT_IMPLICIT)
2896 PerlIO_printf(Perl_debug_log, "implicit ");
2897 PerlIO_printf(Perl_debug_log, "minlen %ld ", (long) r->minlen);
2898 if (r->reganch & ROPT_EVAL_SEEN)
2899 PerlIO_printf(Perl_debug_log, "with eval ");
2900 PerlIO_printf(Perl_debug_log, "\n");
2901 #endif /* DEBUGGING */
2905 - regprop - printable representation of opcode
2908 regprop(SV *sv, regnode *o)
2912 register char *p = 0;
2914 sv_setpvn(sv, "", 0);
2959 sv_catpvf(sv, "EXACT <%s%s%s>", PL_colors[0], OPERAND(o) + 1, PL_colors[1]);
2962 sv_catpvf(sv, "EXACTF <%s%s%s>", PL_colors[0], OPERAND(o) + 1, PL_colors[1]);
2965 sv_catpvf(sv, "EXACTFL <%s%s%s>", PL_colors[0], OPERAND(o) + 1, PL_colors[1]);
2992 sv_catpvf(sv, "CURLY {%d,%d}", ARG1(o), ARG2(o));
2995 sv_catpvf(sv, "CURLYM[%d] {%d,%d}", o->flags, ARG1(o), ARG2(o));
2998 sv_catpvf(sv, "CURLYN[%d] {%d,%d}", o->flags, ARG1(o), ARG2(o));
3001 sv_catpvf(sv, "CURLYX {%d,%d}", ARG1(o), ARG2(o));
3004 sv_catpvf(sv, "REF%d", ARG(o));
3007 sv_catpvf(sv, "REFF%d", ARG(o));
3010 sv_catpvf(sv, "REFFL%d", ARG(o));
3013 sv_catpvf(sv, "OPEN%d", ARG(o));
3016 sv_catpvf(sv, "CLOSE%d", ARG(o));
3032 sv_catpvf(sv, "UNLESSM[-%d]", o->flags);
3035 sv_catpvf(sv, "IFMATCH[-%d]", o->flags);
3086 sv_catpvf(sv, "GROUPP%d", ARG(o));
3089 sv_catpvf(sv, "LOGICAL[%d]", o->flags);
3101 FAIL("corrupted regexp opcode");
3105 #endif /* DEBUGGING */
3109 pregfree(struct regexp *r)
3112 if (!r || (--r->refcnt > 0))
3115 Safefree(r->precomp);
3117 Safefree(r->subbase);
3119 if (r->anchored_substr)
3120 SvREFCNT_dec(r->anchored_substr);
3121 if (r->float_substr)
3122 SvREFCNT_dec(r->float_substr);
3123 Safefree(r->substrs);
3126 int n = r->data->count;
3127 AV* new_comppad = NULL;
3132 switch (r->data->what[n]) {
3134 SvREFCNT_dec((SV*)r->data->data[n]);
3137 new_comppad = (AV*)r->data->data[n];
3140 if (new_comppad == NULL)
3141 croak("panic: pregfree comppad");
3142 old_comppad = PL_comppad;
3143 old_curpad = PL_curpad;
3144 PL_comppad = new_comppad;
3145 PL_curpad = AvARRAY(new_comppad);
3146 op_free((OP_4tree*)r->data->data[n]);
3147 PL_comppad = old_comppad;
3148 PL_curpad = old_curpad;
3149 SvREFCNT_dec((SV*)new_comppad);
3155 FAIL2("panic: regfree data code '%c'", r->data->what[n]);
3158 Safefree(r->data->what);
3161 Safefree(r->startp);
3167 - regnext - dig the "next" pointer out of a node
3169 * [Note, when REGALIGN is defined there are two places in regmatch()
3170 * that bypass this code for speed.]
3173 regnext(register regnode *p)
3176 register I32 offset;
3178 if (p == &PL_regdummy)
3181 offset = (reg_off_by_arg[OP(p)] ? ARG(p) : NEXT_OFF(p));
3189 re_croak2(const char* pat1,const char* pat2,...)
3192 STRLEN l1 = strlen(pat1);
3193 STRLEN l2 = strlen(pat2);
3201 Copy(pat1, buf, l1 , char);
3202 Copy(pat2, buf + l1, l2 , char);
3203 buf[l1 + l2] = '\n';
3204 buf[l1 + l2 + 1] = '\0';
3205 va_start(args, pat2);
3206 message = mess(buf, &args);
3208 l1 = strlen(message);
3211 Copy(message, buf, l1 , char);
3212 buf[l1] = '\0'; /* Overwrite \n */
3216 /* XXX Here's a total kludge. But we need to re-enter for swash routines. */
3219 save_re_context(void)
3223 SAVEPPTR(PL_regprecomp); /* uncompiled string. */
3224 SAVEI32(PL_regnpar); /* () count. */
3225 SAVEI32(PL_regsize); /* Code size. */
3226 SAVEI16(PL_regflags); /* are we folding, multilining? */
3227 SAVEPPTR(PL_reginput); /* String-input pointer. */
3228 SAVEPPTR(PL_regbol); /* Beginning of input, for ^ check. */
3229 SAVEPPTR(PL_regeol); /* End of input, for $ check. */
3230 SAVESPTR(PL_regstartp); /* Pointer to startp array. */
3231 SAVESPTR(PL_regendp); /* Ditto for endp. */
3232 SAVESPTR(PL_reglastparen); /* Similarly for lastparen. */
3233 SAVEPPTR(PL_regtill); /* How far we are required to go. */
3234 SAVEI32(PL_regprev); /* char before regbol, \n if none */
3235 SAVESPTR(PL_reg_start_tmp); /* from regexec.c */
3236 PL_reg_start_tmp = 0;
3237 SAVEFREEPV(PL_reg_start_tmp);
3238 SAVEI32(PL_reg_start_tmpl); /* from regexec.c */
3239 PL_reg_start_tmpl = 0;
3240 SAVESPTR(PL_regdata);
3241 SAVEI32(PL_reg_flags); /* from regexec.c */
3242 SAVEI32(PL_reg_eval_set); /* from regexec.c */
3243 SAVEI32(PL_regnarrate); /* from regexec.c */
3244 SAVESPTR(PL_regprogram); /* from regexec.c */
3245 SAVEINT(PL_regindent); /* from regexec.c */
3246 SAVESPTR(PL_regcc); /* from regexec.c */
3247 SAVESPTR(PL_curcop);
3248 SAVESPTR(PL_regcomp_rx); /* from regcomp.c */
3249 SAVEI32(PL_regseen); /* from regcomp.c */
3250 SAVEI32(PL_regsawback); /* Did we see \1, ...? */
3251 SAVEI32(PL_regnaughty); /* How bad is this pattern? */
3252 SAVESPTR(PL_regcode); /* Code-emit pointer; ®dummy = don't */
3253 SAVEPPTR(PL_regxend); /* End of input for compile */
3254 SAVEPPTR(PL_regcomp_parse); /* Input-scan pointer. */
3255 SAVESPTR(PL_reg_call_cc); /* from regexec.c */
3256 SAVESPTR(PL_reg_re); /* from regexec.c */
3257 SAVEPPTR(PL_reg_ganch); /* from regexec.c */
3258 SAVESPTR(PL_reg_sv); /* from regexec.c */
3259 SAVESPTR(PL_reg_magic); /* from regexec.c */
3260 SAVEI32(PL_reg_oldpos); /* from regexec.c */
3261 SAVESPTR(PL_reg_oldcurpm); /* from regexec.c */
3262 SAVESPTR(PL_reg_curpm); /* from regexec.c */
3264 SAVEPPTR(PL_reg_starttry); /* from regexec.c */