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 I32 is_par = OP(scan) == OPEN ? ARG(scan) : 0;
256 scan_data_t data_fake;
258 while (scan && OP(scan) != END && scan < last) {
259 /* Peephole optimizer: */
261 if (regkind[(U8)OP(scan)] == EXACT) {
262 regnode *n = regnext(scan);
265 regnode *stop = scan;
268 next = scan + (*OPERAND(scan) + 2 - 1)/sizeof(regnode) + 2;
269 /* Skip NOTHING, merge EXACT*. */
271 ( regkind[(U8)OP(n)] == NOTHING ||
272 (stringok && (OP(n) == OP(scan))))
274 && NEXT_OFF(scan) + NEXT_OFF(n) < I16_MAX) {
275 if (OP(n) == TAIL || n > next)
277 if (regkind[(U8)OP(n)] == NOTHING) {
278 NEXT_OFF(scan) += NEXT_OFF(n);
279 next = n + NODE_STEP_REGNODE;
287 int oldl = *OPERAND(scan);
288 regnode *nnext = regnext(n);
290 if (oldl + *OPERAND(n) > U8_MAX)
292 NEXT_OFF(scan) += NEXT_OFF(n);
293 *OPERAND(scan) += *OPERAND(n);
294 next = n + (*OPERAND(n) + 2 - 1)/sizeof(regnode) + 2;
295 /* Now we can overwrite *n : */
296 Move(OPERAND(n) + 1, OPERAND(scan) + oldl + 1,
297 *OPERAND(n) + 1, char);
307 n = scan + (*OPERAND(scan) + 2 - 1)/sizeof(regnode) + 2;
309 /* Purify reports a benign UMR here sometimes, because we
310 * don't initialize the OP() slot of a node when that node
311 * is occupied by just the trailing null of the string in
313 if (regkind[(U8)OP(n)] != NOTHING || OP(n) == NOTHING) {
322 if (OP(scan) != CURLYX) {
323 int max = (reg_off_by_arg[OP(scan)]
325 /* I32 may be smaller than U16 on CRAYs! */
326 : (I32_MAX < U16_MAX ? I32_MAX : U16_MAX));
327 int off = (reg_off_by_arg[OP(scan)] ? ARG(scan) : NEXT_OFF(scan));
331 /* Skip NOTHING and LONGJMP. */
332 while ((n = regnext(n))
333 && ((regkind[(U8)OP(n)] == NOTHING && (noff = NEXT_OFF(n)))
334 || ((OP(n) == LONGJMP) && (noff = ARG(n))))
337 if (reg_off_by_arg[OP(scan)])
340 NEXT_OFF(scan) = off;
342 if (OP(scan) == BRANCH || OP(scan) == BRANCHJ
343 || OP(scan) == IFTHEN || OP(scan) == SUSPEND) {
344 next = regnext(scan);
347 if (OP(next) == code || code == IFTHEN || code == SUSPEND) {
348 I32 max1 = 0, min1 = I32_MAX, num = 0;
350 if (flags & SCF_DO_SUBSTR)
352 while (OP(scan) == code) {
353 I32 deltanext, minnext;
357 next = regnext(scan);
358 scan = NEXTOPER(scan);
360 scan = NEXTOPER(scan);
361 /* We suppose the run is continuous, last=next...*/
362 minnext = study_chunk(&scan, &deltanext, next,
366 if (max1 < minnext + deltanext)
367 max1 = minnext + deltanext;
368 if (deltanext == I32_MAX)
371 if (data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
373 if (data && (data_fake.flags & SF_HAS_EVAL))
374 data->flags |= SF_HAS_EVAL;
378 if (code == IFTHEN && num < 2) /* Empty ELSE branch */
380 if (flags & SCF_DO_SUBSTR) {
381 data->pos_min += min1;
382 data->pos_delta += max1 - min1;
383 if (max1 != min1 || is_inf)
384 data->longest = &(data->longest_float);
387 delta += max1 - min1;
389 else if (code == BRANCHJ) /* single branch is optimized. */
390 scan = NEXTOPER(NEXTOPER(scan));
391 else /* single branch is optimized. */
392 scan = NEXTOPER(scan);
395 else if (OP(scan) == EXACT) {
396 I32 l = *OPERAND(scan);
398 unsigned char *s = (unsigned char *)(OPERAND(scan)+1);
399 unsigned char *e = s + l;
408 if (flags & SCF_DO_SUBSTR) { /* Update longest substr. */
409 /* The code below prefers earlier match for fixed
410 offset, later match for variable offset. */
411 if (data->last_end == -1) { /* Update the start info. */
412 data->last_start_min = data->pos_min;
413 data->last_start_max = is_inf
414 ? I32_MAX : data->pos_min + data->pos_delta;
416 sv_catpvn(data->last_found, (char *)(OPERAND(scan)+1), *OPERAND(scan));
417 data->last_end = data->pos_min + l;
418 data->pos_min += l; /* As in the first entry. */
419 data->flags &= ~SF_BEFORE_EOL;
422 else if (regkind[(U8)OP(scan)] == EXACT) {
423 I32 l = *OPERAND(scan);
424 if (flags & SCF_DO_SUBSTR)
427 unsigned char *s = (unsigned char *)(OPERAND(scan)+1);
428 unsigned char *e = s + l;
437 if (data && (flags & SCF_DO_SUBSTR))
440 else if (strchr(varies,OP(scan))) {
441 I32 mincount, maxcount, minnext, deltanext, pos_before, fl;
442 regnode *oscan = scan;
444 switch (regkind[(U8)OP(scan)]) {
446 scan = NEXTOPER(scan);
449 if (flags & SCF_DO_SUBSTR) {
450 next = NEXTOPER(scan);
451 if (OP(next) == EXACT) {
453 maxcount = REG_INFTY;
454 next = regnext(scan);
455 scan = NEXTOPER(scan);
459 if (flags & SCF_DO_SUBSTR)
465 scan = regnext(scan);
466 if (flags & SCF_DO_SUBSTR) {
468 data->longest = &(data->longest_float);
470 goto optimize_curly_tail;
472 mincount = ARG1(scan);
473 maxcount = ARG2(scan);
474 next = regnext(scan);
475 scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
477 if (flags & SCF_DO_SUBSTR) {
478 if (mincount == 0) scan_commit(data);
479 pos_before = data->pos_min;
483 data->flags &= ~(SF_HAS_PAR|SF_IN_PAR|SF_HAS_EVAL);
485 data->flags |= SF_IS_INF;
487 /* This will finish on WHILEM, setting scan, or on NULL: */
488 minnext = study_chunk(&scan, &deltanext, last, data,
490 ? (flags & ~SCF_DO_SUBSTR) : flags);
491 if (!scan) /* It was not CURLYX, but CURLY. */
493 if (ckWARN(WARN_UNSAFE) && (minnext + deltanext == 0)
494 && !(data->flags & (SF_HAS_PAR|SF_IN_PAR))
495 && maxcount <= 10000) /* Complement check for big count */
496 warner(WARN_UNSAFE, "Strange *+?{} on zero-length expression");
497 min += minnext * mincount;
498 is_inf |= (maxcount == REG_INFTY && (minnext + deltanext) > 0
499 || deltanext == I32_MAX);
500 delta += (minnext + deltanext) * maxcount - minnext * mincount;
502 /* Try powerful optimization CURLYX => CURLYN. */
503 if ( OP(oscan) == CURLYX && data
504 && data->flags & SF_IN_PAR
505 && !(data->flags & SF_HAS_EVAL)
506 && !deltanext && minnext == 1 ) {
507 /* Try to optimize to CURLYN. */
508 regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS;
509 regnode *nxt1 = nxt, *nxt2;
513 if (!strchr(simple,OP(nxt))
514 && !(regkind[(U8)OP(nxt)] == EXACT
515 && *OPERAND(nxt) == 1))
519 if (OP(nxt) != CLOSE)
521 /* Now we know that nxt2 is the only contents: */
522 oscan->flags = ARG(nxt);
524 OP(nxt1) = NOTHING; /* was OPEN. */
526 OP(nxt1 + 1) = OPTIMIZED; /* was count. */
527 NEXT_OFF(nxt1+ 1) = 0; /* just for consistancy. */
528 NEXT_OFF(nxt2) = 0; /* just for consistancy with CURLY. */
529 OP(nxt) = OPTIMIZED; /* was CLOSE. */
530 OP(nxt + 1) = OPTIMIZED; /* was count. */
531 NEXT_OFF(nxt+ 1) = 0; /* just for consistancy. */
536 /* Try optimization CURLYX => CURLYM. */
537 if ( OP(oscan) == CURLYX && data
538 && !(data->flags & SF_HAS_PAR)
539 && !(data->flags & SF_HAS_EVAL)
541 /* XXXX How to optimize if data == 0? */
542 /* Optimize to a simpler form. */
543 regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN */
547 while ( (nxt2 = regnext(nxt)) /* skip over embedded stuff*/
548 && (OP(nxt2) != WHILEM))
550 OP(nxt2) = SUCCEED; /* Whas WHILEM */
551 /* Need to optimize away parenths. */
552 if (data->flags & SF_IN_PAR) {
553 /* Set the parenth number. */
554 regnode *nxt1 = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN*/
556 if (OP(nxt) != CLOSE)
557 FAIL("panic opt close");
558 oscan->flags = ARG(nxt);
559 OP(nxt1) = OPTIMIZED; /* was OPEN. */
560 OP(nxt) = OPTIMIZED; /* was CLOSE. */
562 OP(nxt1 + 1) = OPTIMIZED; /* was count. */
563 OP(nxt + 1) = OPTIMIZED; /* was count. */
564 NEXT_OFF(nxt1 + 1) = 0; /* just for consistancy. */
565 NEXT_OFF(nxt + 1) = 0; /* just for consistancy. */
568 while ( nxt1 && (OP(nxt1) != WHILEM)) {
569 regnode *nnxt = regnext(nxt1);
572 if (reg_off_by_arg[OP(nxt1)])
573 ARG_SET(nxt1, nxt2 - nxt1);
574 else if (nxt2 - nxt1 < U16_MAX)
575 NEXT_OFF(nxt1) = nxt2 - nxt1;
577 OP(nxt) = NOTHING; /* Cannot beautify */
582 /* Optimize again: */
583 study_chunk(&nxt1, &deltanext, nxt, NULL, 0);
588 if (data && fl & (SF_HAS_PAR|SF_IN_PAR))
590 if (flags & SCF_DO_SUBSTR) {
591 SV *last_str = Nullsv;
592 int counted = mincount != 0;
594 if (data->last_end > 0 && mincount != 0) { /* Ends with a string. */
595 I32 b = pos_before >= data->last_start_min
596 ? pos_before : data->last_start_min;
598 char *s = SvPV(data->last_found, l);
599 I32 old = b - data->last_start_min;
602 old = utf8_hop((U8*)s, old) - (U8*)s;
605 /* Get the added string: */
606 last_str = newSVpv(s + old, l);
607 if (deltanext == 0 && pos_before == b) {
608 /* What was added is a constant string */
610 SvGROW(last_str, (mincount * l) + 1);
611 repeatcpy(SvPVX(last_str) + l,
612 SvPVX(last_str), l, mincount - 1);
613 SvCUR(last_str) *= mincount;
614 /* Add additional parts. */
615 SvCUR_set(data->last_found,
616 SvCUR(data->last_found) - l);
617 sv_catsv(data->last_found, last_str);
618 data->last_end += l * (mincount - 1);
622 /* It is counted once already... */
623 data->pos_min += minnext * (mincount - counted);
624 data->pos_delta += - counted * deltanext +
625 (minnext + deltanext) * maxcount - minnext * mincount;
626 if (mincount != maxcount) {
628 if (mincount && last_str) {
629 sv_setsv(data->last_found, last_str);
630 data->last_end = data->pos_min;
631 data->last_start_min =
632 data->pos_min - CHR_SVLEN(last_str);
633 data->last_start_max = is_inf
635 : data->pos_min + data->pos_delta
636 - CHR_SVLEN(last_str);
638 data->longest = &(data->longest_float);
641 if (data && (fl & SF_HAS_EVAL))
642 data->flags |= SF_HAS_EVAL;
644 if (OP(oscan) != CURLYX) {
645 while (regkind[(U8)OP(next = regnext(oscan))] == NOTHING
647 NEXT_OFF(oscan) += NEXT_OFF(next);
650 default: /* REF only? */
651 if (flags & SCF_DO_SUBSTR) {
653 data->longest = &(data->longest_float);
659 else if (strchr(simple,OP(scan)) || regkind[(U8)OP(scan)] == ANYUTF8) {
660 if (flags & SCF_DO_SUBSTR) {
666 else if (regkind[(U8)OP(scan)] == EOL && flags & SCF_DO_SUBSTR) {
667 data->flags |= (OP(scan) == MEOL
671 else if (regkind[(U8)OP(scan)] == BRANCHJ
672 && (scan->flags || data)
673 && (OP(scan) == IFMATCH || OP(scan) == UNLESSM)) {
674 I32 deltanext, minnext;
678 next = regnext(scan);
679 nscan = NEXTOPER(NEXTOPER(scan));
680 minnext = study_chunk(&nscan, &deltanext, last, &data_fake, 0);
683 FAIL("variable length lookbehind not implemented");
685 else if (minnext > U8_MAX) {
686 FAIL2("lookbehind longer than %d not implemented", U8_MAX);
688 scan->flags = minnext;
690 if (data && data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
692 if (data && (data_fake.flags & SF_HAS_EVAL))
693 data->flags |= SF_HAS_EVAL;
695 else if (OP(scan) == OPEN) {
698 else if (OP(scan) == CLOSE && ARG(scan) == is_par) {
699 next = regnext(scan);
701 if ( next && (OP(next) != WHILEM) && next < last)
702 is_par = 0; /* Disable optimization */
704 else if (OP(scan) == EVAL) {
706 data->flags |= SF_HAS_EVAL;
708 /* Else: zero-length, ignore. */
709 scan = regnext(scan);
714 *deltap = is_inf ? I32_MAX : delta;
715 if (flags & SCF_DO_SUBSTR && is_inf)
716 data->pos_delta = I32_MAX - data->pos_min;
719 if (is_par && pars==1 && data) {
720 data->flags |= SF_IN_PAR;
721 data->flags &= ~SF_HAS_PAR;
723 else if (pars && data) {
724 data->flags |= SF_HAS_PAR;
725 data->flags &= ~SF_IN_PAR;
731 add_data(I32 n, char *s)
734 if (PL_regcomp_rx->data) {
735 Renewc(PL_regcomp_rx->data,
736 sizeof(*PL_regcomp_rx->data) + sizeof(void*) * (PL_regcomp_rx->data->count + n - 1),
737 char, struct reg_data);
738 Renew(PL_regcomp_rx->data->what, PL_regcomp_rx->data->count + n, U8);
739 PL_regcomp_rx->data->count += n;
742 Newc(1207, PL_regcomp_rx->data, sizeof(*PL_regcomp_rx->data) + sizeof(void*) * (n - 1),
743 char, struct reg_data);
744 New(1208, PL_regcomp_rx->data->what, n, U8);
745 PL_regcomp_rx->data->count = n;
747 Copy(s, PL_regcomp_rx->data->what + PL_regcomp_rx->data->count - n, n, U8);
748 return PL_regcomp_rx->data->count - n;
752 - pregcomp - compile a regular expression into internal code
754 * We can't allocate space until we know how big the compiled form will be,
755 * but we can't compile it (and thus know how big it is) until we've got a
756 * place to put the code. So we cheat: we compile it twice, once with code
757 * generation turned off and size counting turned on, and once "for real".
758 * This also means that we don't allocate space until we are sure that the
759 * thing really will compile successfully, and we never have to move the
760 * code and thus invalidate pointers into it. (Note that it has to be in
761 * one piece because free() must be able to free it all.) [NB: not true in perl]
763 * Beware that the optimization-preparation code in here knows about some
764 * of the structure of the compiled regexp. [I'll say.]
767 pregcomp(char *exp, char *xend, PMOP *pm)
782 FAIL("NULL regexp argument");
784 if (PL_curcop == &compiling ? (PL_hints & HINT_UTF8) : IN_UTF8)
785 PL_reg_flags |= RF_utf8;
789 PL_regprecomp = savepvn(exp, xend - exp);
790 DEBUG_r(PerlIO_printf(Perl_debug_log, "compiling RE `%*s'\n",
791 xend - exp, PL_regprecomp));
792 PL_regflags = pm->op_pmflags;
796 PL_seen_zerolen = *exp == '^' ? -1 : 0;
800 /* First pass: determine size, legality. */
801 PL_regcomp_parse = exp;
806 PL_regcode = &PL_regdummy;
807 regc((U8)MAGIC, (char*)PL_regcode);
808 if (reg(0, &flags) == NULL) {
809 Safefree(PL_regprecomp);
810 PL_regprecomp = Nullch;
813 DEBUG_r(PerlIO_printf(Perl_debug_log, "size %d ", PL_regsize));
818 char *s = PerlEnv_getenv("TERMCAP_COLORS");
822 PL_colors[0] = s = savepv(s);
826 FAIL("Not enough TABs in TERMCAP_COLORS");
836 PerlIO_printf(Perl_debug_log, "%s%s%s%s",
837 PL_colors[0],PL_colors[1],PL_colors[2],PL_colors[3]);
841 /* Small enough for pointer-storage convention?
842 If extralen==0, this means that we will not need long jumps. */
843 if (PL_regsize >= 0x10000L && PL_extralen)
844 PL_regsize += PL_extralen;
848 /* Allocate space and initialize. */
849 Newc(1001, r, sizeof(regexp) + (unsigned)PL_regsize * sizeof(regnode),
852 FAIL("regexp out of space");
854 r->prelen = xend - exp;
855 r->precomp = PL_regprecomp;
856 r->subbeg = r->subbase = NULL;
857 r->nparens = PL_regnpar - 1; /* set early to validate backrefs */
860 /* Second pass: emit code. */
861 PL_regcomp_parse = exp;
865 PL_regcode = r->program;
866 /* Store the count of eval-groups for security checks: */
867 PL_regcode->next_off = ((PL_seen_evals > U16_MAX) ? U16_MAX : PL_seen_evals);
868 regc((U8)MAGIC, (char*) PL_regcode++);
870 if (reg(0, &flags) == NULL)
873 /* Dig out information for optimizations. */
874 r->reganch = pm->op_pmflags & PMf_COMPILETIME;
875 pm->op_pmflags = PL_regflags;
877 r->reganch |= ROPT_UTF8;
878 r->regstclass = NULL;
879 if (PL_regnaughty >= 10) /* Probably an expensive pattern. */
880 r->reganch |= ROPT_NAUGHTY;
881 scan = r->program + 1; /* First BRANCH. */
883 /* XXXX To minimize changes to RE engine we always allocate
884 3-units-long substrs field. */
885 Newz(1004, r->substrs, 1, struct reg_substr_data);
887 if (OP(scan) != BRANCH) { /* Only one top-level choice. */
890 STRLEN longest_float_length, longest_fixed_length;
892 StructCopy(&zero_scan_data, &data, scan_data_t);
894 /* Skip introductions and multiplicators >= 1. */
895 while ((OP(first) == OPEN && (sawopen = 1)) ||
896 (OP(first) == BRANCH && OP(regnext(first)) != BRANCH) ||
897 (OP(first) == PLUS) ||
898 (OP(first) == MINMOD) ||
899 (regkind[(U8)OP(first)] == CURLY && ARG1(first) > 0) ) {
900 if (OP(first) == PLUS)
903 first += regarglen[(U8)OP(first)];
904 first = NEXTOPER(first);
907 /* Starting-point info. */
909 if (OP(first) == EXACT); /* Empty, get anchored substr later. */
910 else if (strchr(simple+4,OP(first)))
911 r->regstclass = first;
912 else if (regkind[(U8)OP(first)] == BOUND ||
913 regkind[(U8)OP(first)] == NBOUND)
914 r->regstclass = first;
915 else if (regkind[(U8)OP(first)] == BOL) {
916 r->reganch |= (OP(first) == MBOL ? ROPT_ANCH_MBOL: ROPT_ANCH_BOL);
917 first = NEXTOPER(first);
920 else if (OP(first) == GPOS) {
921 r->reganch |= ROPT_ANCH_GPOS;
922 first = NEXTOPER(first);
925 else if ((OP(first) == STAR &&
926 regkind[(U8)OP(NEXTOPER(first))] == ANY) &&
927 !(r->reganch & ROPT_ANCH) )
929 /* turn .* into ^.* with an implied $*=1 */
930 r->reganch |= ROPT_ANCH_BOL | ROPT_IMPLICIT;
931 first = NEXTOPER(first);
934 if (sawplus && (!sawopen || !PL_regsawback))
935 r->reganch |= ROPT_SKIP; /* x+ must match 1st of run */
937 /* Scan is after the zeroth branch, first is atomic matcher. */
938 DEBUG_r(PerlIO_printf(Perl_debug_log, "first at %d\n",
941 * If there's something expensive in the r.e., find the
942 * longest literal string that must appear and make it the
943 * regmust. Resolve ties in favor of later strings, since
944 * the regstart check works with the beginning of the r.e.
945 * and avoiding duplication strengthens checking. Not a
946 * strong reason, but sufficient in the absence of others.
947 * [Now we resolve ties in favor of the earlier string if
948 * it happens that c_offset_min has been invalidated, since the
949 * earlier string may buy us something the later one won't.]
953 data.longest_fixed = newSVpv("",0);
954 data.longest_float = newSVpv("",0);
955 data.last_found = newSVpv("",0);
956 data.longest = &(data.longest_fixed);
959 minlen = study_chunk(&first, &fake, scan + PL_regsize, /* Up to end */
960 &data, SCF_DO_SUBSTR);
961 if ( PL_regnpar == 1 && data.longest == &(data.longest_fixed)
962 && data.last_start_min == 0 && data.last_end > 0
964 && (!(PL_regseen & REG_SEEN_GPOS) || (r->reganch & ROPT_ANCH_GPOS)))
965 r->reganch |= ROPT_CHECK_ALL;
967 SvREFCNT_dec(data.last_found);
969 longest_float_length = CHR_SVLEN(data.longest_float);
970 if (longest_float_length
971 || (data.flags & SF_FL_BEFORE_EOL
972 && (!(data.flags & SF_FL_BEFORE_MEOL)
973 || (PL_regflags & PMf_MULTILINE)))) {
974 if (SvCUR(data.longest_fixed) /* ok to leave SvCUR */
975 && data.offset_fixed == data.offset_float_min)
976 goto remove; /* Like in (a)+. */
978 r->float_substr = data.longest_float;
979 r->float_min_offset = data.offset_float_min;
980 r->float_max_offset = data.offset_float_max;
981 fbm_compile(r->float_substr, 0);
982 BmUSEFUL(r->float_substr) = 100;
983 if (data.flags & SF_FL_BEFORE_EOL /* Cannot have SEOL and MULTI */
984 && (!(data.flags & SF_FL_BEFORE_MEOL)
985 || (PL_regflags & PMf_MULTILINE)))
986 SvTAIL_on(r->float_substr);
990 r->float_substr = Nullsv;
991 SvREFCNT_dec(data.longest_float);
992 longest_float_length = 0;
995 longest_fixed_length = CHR_SVLEN(data.longest_fixed);
996 if (longest_fixed_length
997 || (data.flags & SF_FIX_BEFORE_EOL /* Cannot have SEOL and MULTI */
998 && (!(data.flags & SF_FIX_BEFORE_MEOL)
999 || (PL_regflags & PMf_MULTILINE)))) {
1000 r->anchored_substr = data.longest_fixed;
1001 r->anchored_offset = data.offset_fixed;
1002 fbm_compile(r->anchored_substr, 0);
1003 BmUSEFUL(r->anchored_substr) = 100;
1004 if (data.flags & SF_FIX_BEFORE_EOL /* Cannot have SEOL and MULTI */
1005 && (!(data.flags & SF_FIX_BEFORE_MEOL)
1006 || (PL_regflags & PMf_MULTILINE)))
1007 SvTAIL_on(r->anchored_substr);
1010 r->anchored_substr = Nullsv;
1011 SvREFCNT_dec(data.longest_fixed);
1012 longest_fixed_length = 0;
1015 /* A temporary algorithm prefers floated substr to fixed one to dig more info. */
1016 if (longest_fixed_length > longest_float_length) {
1017 r->check_substr = r->anchored_substr;
1018 r->check_offset_min = r->check_offset_max = r->anchored_offset;
1019 if (r->reganch & ROPT_ANCH_SINGLE)
1020 r->reganch |= ROPT_NOSCAN;
1023 r->check_substr = r->float_substr;
1024 r->check_offset_min = data.offset_float_min;
1025 r->check_offset_max = data.offset_float_max;
1029 /* Several toplevels. Best we can is to set minlen. */
1032 DEBUG_r(PerlIO_printf(Perl_debug_log, "\n"));
1033 scan = r->program + 1;
1034 minlen = study_chunk(&scan, &fake, scan + PL_regsize, NULL, 0);
1035 r->check_substr = r->anchored_substr = r->float_substr = Nullsv;
1039 if (PL_regseen & REG_SEEN_GPOS)
1040 r->reganch |= ROPT_GPOS_SEEN;
1041 if (PL_regseen & REG_SEEN_LOOKBEHIND)
1042 r->reganch |= ROPT_LOOKBEHIND_SEEN;
1043 if (PL_regseen & REG_SEEN_EVAL)
1044 r->reganch |= ROPT_EVAL_SEEN;
1045 Newz(1002, r->startp, PL_regnpar, char*);
1046 Newz(1002, r->endp, PL_regnpar, char*);
1047 DEBUG_r(regdump(r));
1052 - reg - regular expression, i.e. main body or parenthesized thing
1054 * Caller must absorb opening parenthesis.
1056 * Combining parenthesis handling with the base level of regular expression
1057 * is a trifle forced, but the need to tie the tails of the branches to what
1058 * follows makes it hard to avoid.
1061 reg(I32 paren, I32 *flagp)
1062 /* paren: Parenthesized? 0=top, 1=(, inside: changed to letter. */
1065 register regnode *ret; /* Will be the head of the group. */
1066 register regnode *br;
1067 register regnode *lastbr;
1068 register regnode *ender = 0;
1069 register I32 parno = 0;
1070 I32 flags, oregflags = PL_regflags, have_branch = 0, open = 0;
1073 *flagp = 0; /* Tentatively. */
1075 /* Make an OPEN node, if parenthesized. */
1077 if (*PL_regcomp_parse == '?') {
1078 U16 posflags = 0, negflags = 0;
1079 U16 *flagsp = &posflags;
1082 paren = *PL_regcomp_parse++;
1083 ret = NULL; /* For look-ahead/behind. */
1086 PL_regseen |= REG_SEEN_LOOKBEHIND;
1087 if (*PL_regcomp_parse == '!')
1089 if (*PL_regcomp_parse != '=' && *PL_regcomp_parse != '!')
1100 FAIL2("Sequence (?%c...) not implemented", (int)paren);
1103 while (*PL_regcomp_parse && *PL_regcomp_parse != ')')
1105 if (*PL_regcomp_parse != ')')
1106 FAIL("Sequence (?#... not terminated");
1113 I32 count = 1, n = 0;
1115 char *s = PL_regcomp_parse;
1117 OP_4tree *sop, *rop;
1120 PL_regseen |= REG_SEEN_EVAL;
1121 while (count && (c = *PL_regcomp_parse)) {
1122 if (c == '\\' && PL_regcomp_parse[1])
1130 if (*PL_regcomp_parse != ')')
1131 FAIL("Sequence (?{...}) not terminated or not {}-balanced");
1135 if (PL_regcomp_parse - 1 - s)
1136 sv = newSVpv(s, PL_regcomp_parse - 1 - s);
1138 sv = newSVpv("", 0);
1140 rop = sv_compile_2op(sv, &sop, "re", &av);
1142 n = add_data(3, "nso");
1143 PL_regcomp_rx->data->data[n] = (void*)rop;
1144 PL_regcomp_rx->data->data[n+1] = (void*)av;
1145 PL_regcomp_rx->data->data[n+2] = (void*)sop;
1148 else { /* First pass */
1149 if (PL_reginterp_cnt < ++PL_seen_evals && PL_curcop != &compiling)
1150 /* No compiled RE interpolated, has runtime
1151 components ===> unsafe. */
1152 FAIL("Eval-group not allowed at runtime, use re 'eval'");
1154 FAIL("Eval-group in insecure regular expression");
1158 return reganode(EVAL, n);
1162 if (PL_regcomp_parse[0] == '?') {
1163 if (PL_regcomp_parse[1] == '=' || PL_regcomp_parse[1] == '!'
1164 || PL_regcomp_parse[1] == '<'
1165 || PL_regcomp_parse[1] == '{') { /* Lookahead or eval. */
1168 ret = reg_node(LOGICAL);
1169 regtail(ret, reg(1, &flag));
1173 else if (PL_regcomp_parse[0] >= '1' && PL_regcomp_parse[0] <= '9' ) {
1174 parno = atoi(PL_regcomp_parse++);
1176 while (isDIGIT(*PL_regcomp_parse))
1178 ret = reganode(GROUPP, parno);
1179 if ((c = *nextchar()) != ')')
1180 FAIL2("Switch (?(number%c not recognized", c);
1182 regtail(ret, reganode(IFTHEN, 0));
1183 br = regbranch(&flags, 1);
1185 br = reganode(LONGJMP, 0);
1187 regtail(br, reganode(LONGJMP, 0));
1190 lastbr = reganode(IFTHEN, 0); /* Fake one for optimizer. */
1191 regbranch(&flags, 1);
1192 regtail(ret, lastbr);
1198 FAIL("Switch (?(condition)... contains too many branches");
1199 ender = reg_node(TAIL);
1202 regtail(lastbr, ender);
1203 regtail(NEXTOPER(NEXTOPER(lastbr)), ender);
1206 regtail(ret, ender);
1210 FAIL2("Unknown condition for (?(%.2s", PL_regcomp_parse);
1214 FAIL("Sequence (? incomplete");
1219 while (*PL_regcomp_parse && strchr("iogcmsx", *PL_regcomp_parse)) {
1220 if (*PL_regcomp_parse != 'o')
1221 pmflag(flagsp, *PL_regcomp_parse);
1224 if (*PL_regcomp_parse == '-') {
1229 PL_regflags |= posflags;
1230 PL_regflags &= ~negflags;
1231 if (*PL_regcomp_parse == ':') {
1237 if (*PL_regcomp_parse != ')')
1238 FAIL2("Sequence (?%c...) not recognized", *PL_regcomp_parse);
1247 ret = reganode(OPEN, parno);
1254 /* Pick up the branches, linking them together. */
1255 br = regbranch(&flags, 1);
1258 if (*PL_regcomp_parse == '|') {
1259 if (!SIZE_ONLY && PL_extralen) {
1260 reginsert(BRANCHJ, br);
1263 reginsert(BRANCH, br);
1266 PL_extralen += 1; /* For BRANCHJ-BRANCH. */
1268 else if (paren == ':') {
1269 *flagp |= flags&SIMPLE;
1271 if (open) { /* Starts with OPEN. */
1272 regtail(ret, br); /* OPEN -> first. */
1274 else if (paren != '?') /* Not Conditional */
1278 *flagp |= flags&SPSTART;
1280 while (*PL_regcomp_parse == '|') {
1281 if (!SIZE_ONLY && PL_extralen) {
1282 ender = reganode(LONGJMP,0);
1283 regtail(NEXTOPER(NEXTOPER(lastbr)), ender); /* Append to the previous. */
1286 PL_extralen += 2; /* Account for LONGJMP. */
1288 br = regbranch(&flags, 0);
1291 regtail(lastbr, br); /* BRANCH -> BRANCH. */
1295 *flagp |= flags&SPSTART;
1298 if (have_branch || paren != ':') {
1299 /* Make a closing node, and hook it on the end. */
1302 ender = reg_node(TAIL);
1305 ender = reganode(CLOSE, parno);
1311 *flagp &= ~HASWIDTH;
1314 ender = reg_node(SUCCEED);
1317 ender = reg_node(END);
1320 regtail(lastbr, ender);
1323 /* Hook the tails of the branches to the closing node. */
1324 for (br = ret; br != NULL; br = regnext(br)) {
1325 regoptail(br, ender);
1332 static char parens[] = "=!<,>";
1334 if (paren && (p = strchr(parens, paren))) {
1335 int node = ((p - parens) % 2) ? UNLESSM : IFMATCH;
1336 int flag = (p - parens) > 1;
1339 node = SUSPEND, flag = 0;
1340 reginsert(node,ret);
1342 regtail(ret, reg_node(TAIL));
1346 /* Check for proper termination. */
1347 if (paren && (PL_regcomp_parse >= PL_regxend || *nextchar() != ')')) {
1348 FAIL("unmatched () in regexp");
1350 else if (!paren && PL_regcomp_parse < PL_regxend) {
1351 if (*PL_regcomp_parse == ')') {
1352 FAIL("unmatched () in regexp");
1355 FAIL("junk on end of regexp"); /* "Can't happen". */
1359 PL_regflags = oregflags;
1366 - regbranch - one alternative of an | operator
1368 * Implements the concatenation operator.
1371 regbranch(I32 *flagp, I32 first)
1374 register regnode *ret;
1375 register regnode *chain = NULL;
1376 register regnode *latest;
1377 I32 flags = 0, c = 0;
1382 if (!SIZE_ONLY && PL_extralen)
1383 ret = reganode(BRANCHJ,0);
1385 ret = reg_node(BRANCH);
1388 if (!first && SIZE_ONLY)
1389 PL_extralen += 1; /* BRANCHJ */
1391 *flagp = WORST; /* Tentatively. */
1395 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != '|' && *PL_regcomp_parse != ')') {
1397 latest = regpiece(&flags);
1398 if (latest == NULL) {
1399 if (flags & TRYAGAIN)
1403 else if (ret == NULL)
1405 *flagp |= flags&HASWIDTH;
1406 if (chain == NULL) /* First piece. */
1407 *flagp |= flags&SPSTART;
1410 regtail(chain, latest);
1415 if (chain == NULL) { /* Loop ran zero times. */
1416 chain = reg_node(NOTHING);
1421 *flagp |= flags&SIMPLE;
1428 - regpiece - something followed by possible [*+?]
1430 * Note that the branching code sequences used for ? and the general cases
1431 * of * and + are somewhat optimized: they use the same NOTHING node as
1432 * both the endmarker for their branch list and the body of the last branch.
1433 * It might seem that this node could be dispensed with entirely, but the
1434 * endmarker role is not redundant.
1437 regpiece(I32 *flagp)
1440 register regnode *ret;
1442 register char *next;
1444 char *origparse = PL_regcomp_parse;
1447 I32 max = REG_INFTY;
1449 ret = regatom(&flags);
1451 if (flags & TRYAGAIN)
1456 op = *PL_regcomp_parse;
1458 if (op == '{' && regcurly(PL_regcomp_parse)) {
1459 next = PL_regcomp_parse + 1;
1461 while (isDIGIT(*next) || *next == ',') {
1470 if (*next == '}') { /* got one */
1474 min = atoi(PL_regcomp_parse);
1478 maxpos = PL_regcomp_parse;
1480 if (!max && *maxpos != '0')
1481 max = REG_INFTY; /* meaning "infinity" */
1482 else if (max >= REG_INFTY)
1483 FAIL2("Quantifier in {,} bigger than %d", REG_INFTY - 1);
1484 PL_regcomp_parse = next;
1488 if ((flags&SIMPLE)) {
1489 PL_regnaughty += 2 + PL_regnaughty / 2;
1490 reginsert(CURLY, ret);
1493 PL_regnaughty += 4 + PL_regnaughty; /* compound interest */
1494 regtail(ret, reg_node(WHILEM));
1495 if (!SIZE_ONLY && PL_extralen) {
1496 reginsert(LONGJMP,ret);
1497 reginsert(NOTHING,ret);
1498 NEXT_OFF(ret) = 3; /* Go over LONGJMP. */
1500 reginsert(CURLYX,ret);
1501 if (!SIZE_ONLY && PL_extralen)
1502 NEXT_OFF(ret) = 3; /* Go over NOTHING to LONGJMP. */
1503 regtail(ret, reg_node(NOTHING));
1513 if (max && max < min)
1514 FAIL("Can't do {n,m} with n > m");
1529 #if 0 /* Now runtime fix should be reliable. */
1530 if (!(flags&HASWIDTH) && op != '?')
1531 FAIL("regexp *+ operand could be empty");
1536 *flagp = (op != '+') ? (WORST|SPSTART|HASWIDTH) : (WORST|HASWIDTH);
1538 if (op == '*' && (flags&SIMPLE)) {
1539 reginsert(STAR, ret);
1543 else if (op == '*') {
1547 else if (op == '+' && (flags&SIMPLE)) {
1548 reginsert(PLUS, ret);
1552 else if (op == '+') {
1556 else if (op == '?') {
1561 if (ckWARN(WARN_UNSAFE) && !SIZE_ONLY && !(flags&HASWIDTH) && max > 10000) {
1562 warner(WARN_UNSAFE, "%.*s matches null string many times",
1563 PL_regcomp_parse - origparse, origparse);
1566 if (*PL_regcomp_parse == '?') {
1568 reginsert(MINMOD, ret);
1569 regtail(ret, ret + NODE_STEP_REGNODE);
1571 if (ISMULT2(PL_regcomp_parse))
1572 FAIL("nested *?+ in regexp");
1578 - regatom - the lowest level
1580 * Optimization: gobbles an entire sequence of ordinary characters so that
1581 * it can turn them into a single node, which is smaller to store and
1582 * faster to run. Backslashed characters are exceptions, each becoming a
1583 * separate node; the code is simpler that way and it's not worth fixing.
1585 * [Yes, it is worth fixing, some scripts can run twice the speed.]
1591 register regnode *ret = 0;
1594 *flagp = WORST; /* Tentatively. */
1597 switch (*PL_regcomp_parse) {
1601 if (PL_regflags & PMf_MULTILINE)
1602 ret = reg_node(MBOL);
1603 else if (PL_regflags & PMf_SINGLELINE)
1604 ret = reg_node(SBOL);
1606 ret = reg_node(BOL);
1609 if (PL_regcomp_parse[1])
1612 if (PL_regflags & PMf_MULTILINE)
1613 ret = reg_node(MEOL);
1614 else if (PL_regflags & PMf_SINGLELINE)
1615 ret = reg_node(SEOL);
1617 ret = reg_node(EOL);
1622 if (PL_regflags & PMf_SINGLELINE)
1623 ret = reg_node(SANYUTF8);
1625 ret = reg_node(ANYUTF8);
1629 if (PL_regflags & PMf_SINGLELINE)
1630 ret = reg_node(SANY);
1632 ret = reg_node(ANY);
1633 *flagp |= HASWIDTH|SIMPLE;
1639 ret = (UTF ? regclassutf8() : regclass());
1640 if (*PL_regcomp_parse != ']')
1641 FAIL("unmatched [] in regexp");
1643 *flagp |= HASWIDTH|SIMPLE;
1647 ret = reg(1, &flags);
1649 if (flags & TRYAGAIN)
1653 *flagp |= flags&(HASWIDTH|SPSTART|SIMPLE);
1657 if (flags & TRYAGAIN) {
1661 FAIL2("internal urp in regexp at /%s/", PL_regcomp_parse);
1662 /* Supposed to be caught earlier. */
1665 if (!regcurly(PL_regcomp_parse)) {
1673 FAIL("?+*{} follows nothing in regexp");
1676 switch (*++PL_regcomp_parse) {
1679 ret = reg_node(SBOL);
1684 ret = reg_node(GPOS);
1685 PL_regseen |= REG_SEEN_GPOS;
1690 ret = reg_node(SEOL);
1695 ret = reg_node(EOS);
1697 PL_seen_zerolen++; /* Do not optimize RE away */
1701 ret = reg_node(SANY);
1702 *flagp |= HASWIDTH|SIMPLE;
1706 ret = reg_node(CLUMP);
1709 if (UTF && !PL_utf8_mark)
1710 is_utf8_mark("~"); /* preload table */
1715 ? (LOC ? ALNUMLUTF8 : ALNUMUTF8)
1716 : (LOC ? ALNUML : ALNUM));
1717 *flagp |= HASWIDTH|SIMPLE;
1719 if (UTF && !PL_utf8_alnum)
1720 is_utf8_alnum("a"); /* preload table */
1725 ? (LOC ? NALNUMLUTF8 : NALNUMUTF8)
1726 : (LOC ? NALNUML : NALNUM));
1727 *flagp |= HASWIDTH|SIMPLE;
1729 if (UTF && !PL_utf8_alnum)
1730 is_utf8_alnum("a"); /* preload table */
1736 ? (LOC ? BOUNDLUTF8 : BOUNDUTF8)
1737 : (LOC ? BOUNDL : BOUND));
1740 if (UTF && !PL_utf8_alnum)
1741 is_utf8_alnum("a"); /* preload table */
1747 ? (LOC ? NBOUNDLUTF8 : NBOUNDUTF8)
1748 : (LOC ? NBOUNDL : NBOUND));
1751 if (UTF && !PL_utf8_alnum)
1752 is_utf8_alnum("a"); /* preload table */
1757 ? (LOC ? SPACELUTF8 : SPACEUTF8)
1758 : (LOC ? SPACEL : SPACE));
1759 *flagp |= HASWIDTH|SIMPLE;
1761 if (UTF && !PL_utf8_space)
1762 is_utf8_space(" "); /* preload table */
1767 ? (LOC ? NSPACELUTF8 : NSPACEUTF8)
1768 : (LOC ? NSPACEL : NSPACE));
1769 *flagp |= HASWIDTH|SIMPLE;
1771 if (UTF && !PL_utf8_space)
1772 is_utf8_space(" "); /* preload table */
1775 ret = reg_node(UTF ? DIGITUTF8 : DIGIT);
1776 *flagp |= HASWIDTH|SIMPLE;
1778 if (UTF && !PL_utf8_digit)
1779 is_utf8_digit("1"); /* preload table */
1782 ret = reg_node(UTF ? NDIGITUTF8 : NDIGIT);
1783 *flagp |= HASWIDTH|SIMPLE;
1785 if (UTF && !PL_utf8_digit)
1786 is_utf8_digit("1"); /* preload table */
1790 { /* a lovely hack--pretend we saw [\pX] instead */
1791 char* oldregxend = PL_regxend;
1793 if (PL_regcomp_parse[1] == '{') {
1794 PL_regxend = strchr(PL_regcomp_parse, '}');
1796 FAIL("Missing right brace on \\p{}");
1800 PL_regxend = PL_regcomp_parse + 2;
1803 ret = regclassutf8();
1805 PL_regxend = oldregxend;
1808 *flagp |= HASWIDTH|SIMPLE;
1821 case '1': case '2': case '3': case '4':
1822 case '5': case '6': case '7': case '8': case '9':
1824 I32 num = atoi(PL_regcomp_parse);
1826 if (num > 9 && num >= PL_regnpar)
1829 if (!SIZE_ONLY && num > PL_regcomp_rx->nparens)
1830 FAIL("reference to nonexistent group");
1833 ? (LOC ? REFFL : REFF)
1836 while (isDIGIT(*PL_regcomp_parse))
1844 if (PL_regcomp_parse >= PL_regxend)
1845 FAIL("trailing \\ in regexp");
1853 if (PL_regflags & PMf_EXTENDED) {
1854 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != '\n') PL_regcomp_parse++;
1855 if (PL_regcomp_parse < PL_regxend)
1871 ? (LOC ? EXACTFL : EXACTF)
1873 s = (char *) OPERAND(ret);
1874 regc(0, s++); /* save spot for len */
1875 for (len = 0, p = PL_regcomp_parse - 1;
1876 len < 127 && p < PL_regxend;
1881 if (PL_regflags & PMf_EXTENDED)
1882 p = regwhite(p, PL_regxend);
1936 char* e = strchr(p, '}');
1939 FAIL("Missing right brace on \\x{}");
1941 ender = scan_hex(p + 1, e - p, &numlen);
1942 if (numlen + len >= 127) { /* numlen is generous */
1949 FAIL("Can't use \\x{} without 'use utf8' declaration");
1952 ender = scan_hex(p, 2, &numlen);
1958 ender = UCHARAT(p++);
1959 ender = toCTRL(ender);
1961 case '0': case '1': case '2': case '3':case '4':
1962 case '5': case '6': case '7': case '8':case '9':
1964 (isDIGIT(p[1]) && atoi(p) >= PL_regnpar) ) {
1965 ender = scan_oct(p, 3, &numlen);
1974 if (p >= PL_regxend)
1975 FAIL("trailing \\ in regexp");
1978 goto normal_default;
1983 if ((*p & 0xc0) == 0xc0 && UTF) {
1984 ender = utf8_to_uv(p, &numlen);
1991 if (PL_regflags & PMf_EXTENDED)
1992 p = regwhite(p, PL_regxend);
1995 ender = toLOWER_LC_uni(ender);
1997 ender = toLOWER_uni(ender);
1999 if (ISMULT2(p)) { /* Back off on ?+*. */
2002 else if (ender >= 0x80 && UTF) {
2003 reguni(ender, s, &numlen);
2013 if (ender >= 0x80 && UTF) {
2014 reguni(ender, s, &numlen);
2022 PL_regcomp_parse = p - 1;
2025 FAIL("internal disaster in regexp");
2031 *OPERAND(ret) = len;
2034 PL_regsize += (len + 2 + sizeof(regnode) - 1) / sizeof(regnode);
2037 PL_regcode += (len + 2 + sizeof(regnode) - 1) / sizeof(regnode);
2047 regwhite(char *p, char *e)
2052 else if (*p == '#') {
2055 } while (p < e && *p != '\n');
2067 register char *opnd, *s;
2069 register I32 lastvalue = 1234;
2070 register I32 range = 0;
2071 register regnode *ret;
2075 s = opnd = (char *) OPERAND(PL_regcode);
2076 ret = reg_node(ANYOF);
2077 for (value = 0; value < 33; value++)
2079 if (*PL_regcomp_parse == '^') { /* Complement of range. */
2083 *opnd |= ANYOF_INVERT;
2086 PL_regcode += ANY_SKIP;
2088 *opnd |= ANYOF_FOLD;
2090 *opnd |= ANYOF_LOCALE;
2093 PL_regsize += ANY_SKIP;
2095 if (*PL_regcomp_parse == ']' || *PL_regcomp_parse == '-')
2096 goto skipcond; /* allow 1st char to be ] or - */
2097 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != ']') {
2099 value = UCHARAT(PL_regcomp_parse++);
2100 if (value == '[' && PL_regcomp_parse + 1 < PL_regxend &&
2101 /* I smell either [: or [= or [. -- POSIX has been here, right? */
2102 (*PL_regcomp_parse == ':' || *PL_regcomp_parse == '=' || *PL_regcomp_parse == '.')) {
2103 char posixccc = *PL_regcomp_parse;
2104 char* posixccs = PL_regcomp_parse++;
2106 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != posixccc)
2108 if (PL_regcomp_parse == PL_regxend)
2109 /* Grandfather lone [:, [=, [. */
2110 PL_regcomp_parse = posixccs;
2112 PL_regcomp_parse++; /* skip over the posixccc */
2113 if (*PL_regcomp_parse == ']') {
2114 /* Not Implemented Yet.
2115 * (POSIX Extended Character Classes, that is)
2116 * The text between e.g. [: and :] would start
2117 * at posixccs + 1 and stop at regcomp_parse - 2. */
2118 if (ckWARN(WARN_UNSAFE) && !SIZE_ONLY)
2120 "Character class syntax [%c %c] is reserved for future extensions", posixccc, posixccc);
2121 PL_regcomp_parse++; /* skip over the ending ] */
2125 if (value == '\\') {
2126 value = UCHARAT(PL_regcomp_parse++);
2131 *opnd |= ANYOF_ALNUML;
2133 for (value = 0; value < 256; value++)
2135 ANYOF_SET(opnd, value);
2143 *opnd |= ANYOF_NALNUML;
2145 for (value = 0; value < 256; value++)
2146 if (!isALNUM(value))
2147 ANYOF_SET(opnd, value);
2155 *opnd |= ANYOF_SPACEL;
2157 for (value = 0; value < 256; value++)
2159 ANYOF_SET(opnd, value);
2167 *opnd |= ANYOF_NSPACEL;
2169 for (value = 0; value < 256; value++)
2170 if (!isSPACE(value))
2171 ANYOF_SET(opnd, value);
2178 for (value = '0'; value <= '9'; value++)
2179 ANYOF_SET(opnd, value);
2185 for (value = 0; value < '0'; value++)
2186 ANYOF_SET(opnd, value);
2187 for (value = '9' + 1; value < 256; value++)
2188 ANYOF_SET(opnd, value);
2214 value = scan_hex(PL_regcomp_parse, 2, &numlen);
2215 PL_regcomp_parse += numlen;
2218 value = UCHARAT(PL_regcomp_parse++);
2219 value = toCTRL(value);
2221 case '0': case '1': case '2': case '3': case '4':
2222 case '5': case '6': case '7': case '8': case '9':
2223 value = scan_oct(--PL_regcomp_parse, 3, &numlen);
2224 PL_regcomp_parse += numlen;
2229 if (lastvalue > value)
2230 FAIL("invalid [] range in regexp");
2235 if (*PL_regcomp_parse == '-' && PL_regcomp_parse+1 < PL_regxend &&
2236 PL_regcomp_parse[1] != ']') {
2239 continue; /* do it next time */
2243 for ( ; lastvalue <= value; lastvalue++)
2244 ANYOF_SET(opnd, lastvalue);
2248 /* optimize case-insensitive simple patterns (e.g. /[a-z]/i) */
2249 if (!SIZE_ONLY && (*opnd & (0xFF ^ ANYOF_INVERT)) == ANYOF_FOLD) {
2250 for (value = 0; value < 256; ++value) {
2251 if (ANYOF_TEST(opnd, value)) {
2252 I32 cf = fold[value];
2253 ANYOF_SET(opnd, cf);
2256 *opnd &= ~ANYOF_FOLD;
2258 /* optimize inverted simple patterns (e.g. [^a-z]) */
2259 if (!SIZE_ONLY && (*opnd & 0xFF) == ANYOF_INVERT) {
2260 for (value = 0; value < 32; ++value)
2261 opnd[1 + value] ^= 0xFF;
2270 register char *opnd, *e;
2272 register U32 lastvalue = 123456;
2273 register I32 range = 0;
2274 register regnode *ret;
2281 if (*PL_regcomp_parse == '^') { /* Complement of range. */
2285 flags |= ANYOF_INVERT;
2289 flags |= ANYOF_FOLD;
2291 flags |= ANYOF_LOCALE;
2292 listsv = newSVpv("# comment\n",0);
2295 if (*PL_regcomp_parse == ']' || *PL_regcomp_parse == '-')
2296 goto skipcond; /* allow 1st char to be ] or - */
2298 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != ']') {
2300 value = utf8_to_uv(PL_regcomp_parse, &numlen);
2301 PL_regcomp_parse += numlen;
2303 if (value == '[' && PL_regcomp_parse + 1 < PL_regxend &&
2304 /* I smell either [: or [= or [. -- POSIX has been here, right? */
2305 (*PL_regcomp_parse == ':' || *PL_regcomp_parse == '=' || *PL_regcomp_parse == '.')) {
2306 char posixccc = *PL_regcomp_parse;
2307 char* posixccs = PL_regcomp_parse++;
2309 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != posixccc)
2311 if (PL_regcomp_parse == PL_regxend)
2312 /* Grandfather lone [:, [=, [. */
2313 PL_regcomp_parse = posixccs;
2315 PL_regcomp_parse++; /* skip over the posixccc */
2316 if (*PL_regcomp_parse == ']') {
2317 /* Not Implemented Yet.
2318 * (POSIX Extended Character Classes, that is)
2319 * The text between e.g. [: and :] would start
2320 * at posixccs + 1 and stop at regcomp_parse - 2. */
2321 if (ckWARN(WARN_UNSAFE) && !SIZE_ONLY)
2323 "Character class syntax [%c %c] is reserved for future extensions", posixccc, posixccc);
2324 PL_regcomp_parse++; /* skip over the ending ] */
2329 if (value == '\\') {
2330 value = utf8_to_uv(PL_regcomp_parse, &numlen);
2331 PL_regcomp_parse += numlen;
2336 flags |= ANYOF_ALNUML;
2338 sv_catpvf(listsv, "+utf8::IsAlnum\n");
2345 flags |= ANYOF_NALNUML;
2348 "-utf8::IsAlpha\n-utf8::IsDigit\n0000\t%04x\n%04x\tffff\n",
2357 flags |= ANYOF_SPACEL;
2358 sv_catpvf(listsv, "+utf8::IsSpace\n");
2367 flags |= ANYOF_NSPACEL;
2369 "!utf8::IsSpace\n");
2377 sv_catpvf(listsv, "+utf8::IsDigit\n");
2384 "!utf8::IsDigit\n");
2390 if (*PL_regcomp_parse == '{') {
2391 e = strchr(PL_regcomp_parse++, '}');
2393 FAIL("Missing right brace on \\p{}");
2394 n = e - PL_regcomp_parse;
2397 e = PL_regcomp_parse;
2402 sv_catpvf(listsv, "+utf8::%.*s\n", n, PL_regcomp_parse);
2405 "!utf8::%.*s\n", n, PL_regcomp_parse);
2407 PL_regcomp_parse = e + 1;
2432 if (*PL_regcomp_parse == '{') {
2433 e = strchr(PL_regcomp_parse++, '}');
2435 FAIL("Missing right brace on \\x{}");
2436 value = scan_hex(PL_regcomp_parse + 1, e - PL_regcomp_parse, &numlen);
2437 PL_regcomp_parse = e + 1;
2440 value = scan_hex(PL_regcomp_parse, 2, &numlen);
2441 PL_regcomp_parse += numlen;
2445 value = UCHARAT(PL_regcomp_parse++);
2446 value = toCTRL(value);
2448 case '0': case '1': case '2': case '3': case '4':
2449 case '5': case '6': case '7': case '8': case '9':
2450 value = scan_oct(--PL_regcomp_parse, 3, &numlen);
2451 PL_regcomp_parse += numlen;
2456 if (lastvalue > value)
2457 FAIL("invalid [] range in regexp");
2459 sv_catpvf(listsv, "%04x\t%04x\n", lastvalue, value);
2465 if (*PL_regcomp_parse == '-' && PL_regcomp_parse+1 < PL_regxend &&
2466 PL_regcomp_parse[1] != ']') {
2469 continue; /* do it next time */
2472 sv_catpvf(listsv, "%04x\n", value);
2476 ret = reganode(ANYOFUTF8, 0);
2479 SV *rv = swash_init("utf8", "", listsv, 1, 0);
2480 SvREFCNT_dec(listsv);
2481 n = add_data(1,"s");
2482 PL_regcomp_rx->data->data[n] = (void*)rv;
2483 ARG1_SET(ret, flags);
2494 char* retval = PL_regcomp_parse++;
2497 if (*PL_regcomp_parse == '(' && PL_regcomp_parse[1] == '?' &&
2498 PL_regcomp_parse[2] == '#') {
2499 while (*PL_regcomp_parse && *PL_regcomp_parse != ')')
2504 if (PL_regflags & PMf_EXTENDED) {
2505 if (isSPACE(*PL_regcomp_parse)) {
2509 else if (*PL_regcomp_parse == '#') {
2510 while (*PL_regcomp_parse && *PL_regcomp_parse != '\n')
2521 - reg_node - emit a node
2523 STATIC regnode * /* Location. */
2527 register regnode *ret;
2528 register regnode *ptr;
2532 SIZE_ALIGN(PL_regsize);
2537 NODE_ALIGN_FILL(ret);
2539 FILL_ADVANCE_NODE(ptr, op);
2546 - reganode - emit a node with an argument
2548 STATIC regnode * /* Location. */
2549 reganode(U8 op, U32 arg)
2552 register regnode *ret;
2553 register regnode *ptr;
2557 SIZE_ALIGN(PL_regsize);
2562 NODE_ALIGN_FILL(ret);
2564 FILL_ADVANCE_NODE_ARG(ptr, op, arg);
2571 - regc - emit (if appropriate) a Unicode character
2574 reguni(UV uv, char* s, I32* lenp)
2579 *lenp = uv_to_utf8(tmpbuf, uv) - tmpbuf;
2582 *lenp = uv_to_utf8(s, uv) - s;
2587 - regc - emit (if appropriate) a byte of code
2598 - reginsert - insert an operator in front of already-emitted operand
2600 * Means relocating the operand.
2603 reginsert(U8 op, regnode *opnd)
2606 register regnode *src;
2607 register regnode *dst;
2608 register regnode *place;
2609 register int offset = regarglen[(U8)op];
2611 /* (regkind[(U8)op] == CURLY ? EXTRA_STEP_2ARGS : 0); */
2614 PL_regsize += NODE_STEP_REGNODE + offset;
2619 PL_regcode += NODE_STEP_REGNODE + offset;
2622 StructCopy(--src, --dst, regnode);
2624 place = opnd; /* Op node, where operand used to be. */
2625 src = NEXTOPER(place);
2626 FILL_ADVANCE_NODE(place, op);
2627 Zero(src, offset, regnode);
2631 - regtail - set the next-pointer at the end of a node chain of p to val.
2634 regtail(regnode *p, regnode *val)
2637 register regnode *scan;
2638 register regnode *temp;
2639 register I32 offset;
2644 /* Find last node. */
2647 temp = regnext(scan);
2653 if (reg_off_by_arg[OP(scan)]) {
2654 ARG_SET(scan, val - scan);
2657 NEXT_OFF(scan) = val - scan;
2662 - regoptail - regtail on operand of first argument; nop if operandless
2665 regoptail(regnode *p, regnode *val)
2668 /* "Operandless" and "op != BRANCH" are synonymous in practice. */
2669 if (p == NULL || SIZE_ONLY)
2671 if (regkind[(U8)OP(p)] == BRANCH) {
2672 regtail(NEXTOPER(p), val);
2674 else if ( regkind[(U8)OP(p)] == BRANCHJ) {
2675 regtail(NEXTOPER(NEXTOPER(p)), val);
2682 - regcurly - a little FSA that accepts {\d+,?\d*}
2685 regcurly(register char *s)
2704 dumpuntil(regnode *start, regnode *node, regnode *last, SV* sv, I32 l)
2707 register char op = EXACT; /* Arbitrary non-END op. */
2708 register regnode *next, *onode;
2710 while (op != END && (!last || node < last)) {
2711 /* While that wasn't END last time... */
2717 next = regnext(node);
2719 if (OP(node) == OPTIMIZED)
2722 PerlIO_printf(Perl_debug_log, "%4d:%*s%s", node - start,
2723 2*l + 1, "", SvPVX(sv));
2724 if (next == NULL) /* Next ptr. */
2725 PerlIO_printf(Perl_debug_log, "(0)");
2727 PerlIO_printf(Perl_debug_log, "(%d)", next - start);
2728 (void)PerlIO_putc(Perl_debug_log, '\n');
2730 if (regkind[(U8)op] == BRANCHJ) {
2731 register regnode *nnode = (OP(next) == LONGJMP
2734 if (last && nnode > last)
2736 node = dumpuntil(start, NEXTOPER(NEXTOPER(node)), nnode, sv, l + 1);
2738 else if (regkind[(U8)op] == BRANCH) {
2739 node = dumpuntil(start, NEXTOPER(node), next, sv, l + 1);
2741 else if ( op == CURLY) { /* `next' might be very big: optimizer */
2742 node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
2743 NEXTOPER(node) + EXTRA_STEP_2ARGS + 1, sv, l + 1);
2745 else if (regkind[(U8)op] == CURLY && op != CURLYX) {
2746 node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
2749 else if ( op == PLUS || op == STAR) {
2750 node = dumpuntil(start, NEXTOPER(node), NEXTOPER(node) + 1, sv, l + 1);
2752 else if (op == ANYOF) {
2753 node = NEXTOPER(node);
2756 else if (regkind[(U8)op] == EXACT) {
2757 /* Literal string, where present. */
2758 node += ((*OPERAND(node)) + 2 + sizeof(regnode) - 1) / sizeof(regnode);
2759 node = NEXTOPER(node);
2762 node = NEXTOPER(node);
2763 node += regarglen[(U8)op];
2765 if (op == CURLYX || op == OPEN)
2767 else if (op == WHILEM)
2770 #endif /* DEBUGGING */
2775 - regdump - dump a regexp onto Perl_debug_log in vaguely comprehensible form
2782 SV *sv = sv_newmortal();
2784 (void)dumpuntil(r->program, r->program + 1, NULL, sv, 0);
2786 /* Header fields of interest. */
2787 if (r->anchored_substr)
2788 PerlIO_printf(Perl_debug_log, "anchored `%s%s%s'%s at %d ",
2790 SvPVX(r->anchored_substr),
2792 SvTAIL(r->anchored_substr) ? "$" : "",
2793 r->anchored_offset);
2794 if (r->float_substr)
2795 PerlIO_printf(Perl_debug_log, "floating `%s%s%s'%s at %d..%u ",
2797 SvPVX(r->float_substr),
2799 SvTAIL(r->float_substr) ? "$" : "",
2800 r->float_min_offset, r->float_max_offset);
2801 if (r->check_substr)
2802 PerlIO_printf(Perl_debug_log,
2803 r->check_substr == r->float_substr
2804 ? "(checking floating" : "(checking anchored");
2805 if (r->reganch & ROPT_NOSCAN)
2806 PerlIO_printf(Perl_debug_log, " noscan");
2807 if (r->reganch & ROPT_CHECK_ALL)
2808 PerlIO_printf(Perl_debug_log, " isall");
2809 if (r->check_substr)
2810 PerlIO_printf(Perl_debug_log, ") ");
2812 if (r->regstclass) {
2813 regprop(sv, r->regstclass);
2814 PerlIO_printf(Perl_debug_log, "stclass `%s' ", SvPVX(sv));
2816 if (r->reganch & ROPT_ANCH) {
2817 PerlIO_printf(Perl_debug_log, "anchored");
2818 if (r->reganch & ROPT_ANCH_BOL)
2819 PerlIO_printf(Perl_debug_log, "(BOL)");
2820 if (r->reganch & ROPT_ANCH_MBOL)
2821 PerlIO_printf(Perl_debug_log, "(MBOL)");
2822 if (r->reganch & ROPT_ANCH_GPOS)
2823 PerlIO_printf(Perl_debug_log, "(GPOS)");
2824 PerlIO_putc(Perl_debug_log, ' ');
2826 if (r->reganch & ROPT_GPOS_SEEN)
2827 PerlIO_printf(Perl_debug_log, "GPOS ");
2828 if (r->reganch & ROPT_SKIP)
2829 PerlIO_printf(Perl_debug_log, "plus ");
2830 if (r->reganch & ROPT_IMPLICIT)
2831 PerlIO_printf(Perl_debug_log, "implicit ");
2832 PerlIO_printf(Perl_debug_log, "minlen %ld ", (long) r->minlen);
2833 if (r->reganch & ROPT_EVAL_SEEN)
2834 PerlIO_printf(Perl_debug_log, "with eval ");
2835 PerlIO_printf(Perl_debug_log, "\n");
2836 #endif /* DEBUGGING */
2840 - regprop - printable representation of opcode
2843 regprop(SV *sv, regnode *o)
2847 register char *p = 0;
2849 sv_setpvn(sv, "", 0);
2894 sv_catpvf(sv, "EXACT <%s%s%s>", PL_colors[0], OPERAND(o) + 1, PL_colors[1]);
2897 sv_catpvf(sv, "EXACTF <%s%s%s>", PL_colors[0], OPERAND(o) + 1, PL_colors[1]);
2900 sv_catpvf(sv, "EXACTFL <%s%s%s>", PL_colors[0], OPERAND(o) + 1, PL_colors[1]);
2927 sv_catpvf(sv, "CURLY {%d,%d}", ARG1(o), ARG2(o));
2930 sv_catpvf(sv, "CURLYM[%d] {%d,%d}", o->flags, ARG1(o), ARG2(o));
2933 sv_catpvf(sv, "CURLYN[%d] {%d,%d}", o->flags, ARG1(o), ARG2(o));
2936 sv_catpvf(sv, "CURLYX {%d,%d}", ARG1(o), ARG2(o));
2939 sv_catpvf(sv, "REF%d", ARG(o));
2942 sv_catpvf(sv, "REFF%d", ARG(o));
2945 sv_catpvf(sv, "REFFL%d", ARG(o));
2948 sv_catpvf(sv, "OPEN%d", ARG(o));
2951 sv_catpvf(sv, "CLOSE%d", ARG(o));
2967 sv_catpvf(sv, "UNLESSM[-%d]", o->flags);
2970 sv_catpvf(sv, "IFMATCH[-%d]", o->flags);
3021 sv_catpvf(sv, "GROUPP%d", ARG(o));
3036 FAIL("corrupted regexp opcode");
3040 #endif /* DEBUGGING */
3044 pregfree(struct regexp *r)
3047 if (!r || (--r->refcnt > 0))
3050 Safefree(r->precomp);
3052 Safefree(r->subbase);
3054 if (r->anchored_substr)
3055 SvREFCNT_dec(r->anchored_substr);
3056 if (r->float_substr)
3057 SvREFCNT_dec(r->float_substr);
3058 Safefree(r->substrs);
3061 int n = r->data->count;
3063 switch (r->data->what[n]) {
3065 SvREFCNT_dec((SV*)r->data->data[n]);
3068 op_free((OP_4tree*)r->data->data[n]);
3073 FAIL2("panic: regfree data code '%c'", r->data->what[n]);
3076 Safefree(r->data->what);
3079 Safefree(r->startp);
3085 - regnext - dig the "next" pointer out of a node
3087 * [Note, when REGALIGN is defined there are two places in regmatch()
3088 * that bypass this code for speed.]
3091 regnext(register regnode *p)
3094 register I32 offset;
3096 if (p == &PL_regdummy)
3099 offset = (reg_off_by_arg[OP(p)] ? ARG(p) : NEXT_OFF(p));
3107 re_croak2(const char* pat1,const char* pat2,...)
3110 STRLEN l1 = strlen(pat1);
3111 STRLEN l2 = strlen(pat2);
3119 Copy(pat1, buf, l1 , char);
3120 Copy(pat2, buf + l1, l2 , char);
3121 buf[l1 + l2] = '\n';
3122 buf[l1 + l2 + 1] = '\0';
3123 va_start(args, pat2);
3124 message = mess(buf, &args);
3126 l1 = strlen(message);
3129 Copy(message, buf, l1 , char);
3130 buf[l1] = '\0'; /* Overwrite \n */
3134 /* XXX Here's a total kludge. But we need to re-enter for swash routines. */
3137 save_re_context(void)
3141 SAVEPPTR(PL_regprecomp); /* uncompiled string. */
3142 SAVEI32(PL_regnpar); /* () count. */
3143 SAVEI32(PL_regsize); /* Code size. */
3144 SAVEI16(PL_regflags); /* are we folding, multilining? */
3145 SAVEPPTR(PL_reginput); /* String-input pointer. */
3146 SAVEPPTR(PL_regbol); /* Beginning of input, for ^ check. */
3147 SAVEPPTR(PL_regeol); /* End of input, for $ check. */
3148 SAVESPTR(PL_regstartp); /* Pointer to startp array. */
3149 SAVESPTR(PL_regendp); /* Ditto for endp. */
3150 SAVESPTR(PL_reglastparen); /* Similarly for lastparen. */
3151 SAVEPPTR(PL_regtill); /* How far we are required to go. */
3152 SAVEI32(PL_regprev); /* char before regbol, \n if none */
3153 SAVESPTR(PL_reg_start_tmp); /* from regexec.c */
3154 PL_reg_start_tmp = 0;
3155 SAVEFREEPV(PL_reg_start_tmp);
3156 SAVEI32(PL_reg_start_tmpl); /* from regexec.c */
3157 PL_reg_start_tmpl = 0;
3158 SAVESPTR(PL_regdata);
3159 SAVEI32(PL_reg_flags); /* from regexec.c */
3160 SAVEI32(PL_reg_eval_set); /* from regexec.c */
3161 SAVEI32(PL_regnarrate); /* from regexec.c */
3162 SAVESPTR(PL_regprogram); /* from regexec.c */
3163 SAVEINT(PL_regindent); /* from regexec.c */
3164 SAVESPTR(PL_regcc); /* from regexec.c */
3165 SAVESPTR(PL_curcop);
3166 SAVESPTR(PL_regcomp_rx); /* from regcomp.c */
3167 SAVEI32(PL_regseen); /* from regcomp.c */
3168 SAVEI32(PL_regsawback); /* Did we see \1, ...? */
3169 SAVEI32(PL_regnaughty); /* How bad is this pattern? */
3170 SAVESPTR(PL_regcode); /* Code-emit pointer; ®dummy = don't */
3171 SAVEPPTR(PL_regxend); /* End of input for compile */
3172 SAVEPPTR(PL_regcomp_parse); /* Input-scan pointer. */