5 * "A fair jaw-cracker dwarf-language must be." --Samwise Gamgee
8 /* NOTE: this is derived from Henry Spencer's regexp code, and should not
9 * confused with the original package (see point 3 below). Thanks, Henry!
12 /* Additional note: this code is very heavily munged from Henry's version
13 * in places. In some spots I've traded clarity for efficiency, so don't
14 * blame Henry for some of the lack of readability.
17 /* The names of the functions have been changed from regcomp and
18 * regexec to pregcomp and pregexec in order to avoid conflicts
19 * with the POSIX routines of the same names.
22 #ifdef PERL_EXT_RE_BUILD
23 /* need to replace pregcomp et al, so enable that */
24 # ifndef PERL_IN_XSUB_RE
25 # define PERL_IN_XSUB_RE
27 /* need access to debugger hooks */
33 #ifdef PERL_IN_XSUB_RE
34 /* We *really* need to overwrite these symbols: */
35 # define Perl_pregcomp my_regcomp
36 # define Perl_regdump my_regdump
37 # define Perl_regprop my_regprop
38 /* *These* symbols are masked to allow static link. */
39 # define Perl_pregfree my_regfree
40 # define Perl_regnext my_regnext
45 * pregcomp and pregexec -- regsub and regerror are not used in perl
47 * Copyright (c) 1986 by University of Toronto.
48 * Written by Henry Spencer. Not derived from licensed software.
50 * Permission is granted to anyone to use this software for any
51 * purpose on any computer system, and to redistribute it freely,
52 * subject to the following restrictions:
54 * 1. The author is not responsible for the consequences of use of
55 * this software, no matter how awful, even if they arise
58 * 2. The origin of this software must not be misrepresented, either
59 * by explicit claim or by omission.
61 * 3. Altered versions must be plainly marked as such, and must not
62 * be misrepresented as being the original software.
65 **** Alterations to Henry's code are...
67 **** Copyright (c) 1991-1998, Larry Wall
69 **** You may distribute under the terms of either the GNU General Public
70 **** License or the Artistic License, as specified in the README file.
73 * Beware that some of this code is subtly aware of the way operator
74 * precedence is structured in regular expressions. Serious changes in
75 * regular-expression syntax might require a total rethink.
80 #ifndef PERL_IN_XSUB_RE
92 # if defined(BUGGY_MSC6)
93 /* MSC 6.00A breaks on op/regexp.t test 85 unless we turn this off */
94 # pragma optimize("a",off)
95 /* But MSC 6.00A is happy with 'w', for aliases only across function calls*/
96 # pragma optimize("w",on )
97 # endif /* BUGGY_MSC6 */
101 #define STATIC static
104 #define ISMULT1(c) ((c) == '*' || (c) == '+' || (c) == '?')
105 #define ISMULT2(s) ((*s) == '*' || (*s) == '+' || (*s) == '?' || \
106 ((*s) == '{' && regcurly(s)))
108 #define PERL_META "^$.[()|?+*\\"
110 #define META "^$.[()|?+*\\"
114 #undef SPSTART /* dratted cpp namespace... */
117 * Flags to be passed up and down.
119 #define WORST 0 /* Worst case. */
120 #define HASWIDTH 0x1 /* Known to match non-null strings. */
121 #define SIMPLE 0x2 /* Simple enough to be STAR/PLUS operand. */
122 #define SPSTART 0x4 /* Starts with * or +. */
123 #define TRYAGAIN 0x8 /* Weeded out a declaration. */
126 * Forward declarations for pregcomp()'s friends.
130 static regnode *reg _((I32, I32 *));
131 static regnode *reganode _((U8, U32));
132 static regnode *regatom _((I32 *));
133 static regnode *regbranch _((I32 *, I32));
134 static void regc _((U8, char *));
135 static void reguni _((UV, char *, I32*));
136 static regnode *regclass _((void));
137 static regnode *regclassutf8 _((void));
138 STATIC I32 regcurly _((char *));
139 static regnode *reg_node _((U8));
140 static regnode *regpiece _((I32 *));
141 static void reginsert _((U8, regnode *));
142 static void regoptail _((regnode *, regnode *));
143 static void regtail _((regnode *, regnode *));
144 static char* regwhite _((char *, char *));
145 static char* nextchar _((void));
146 static void re_croak2 _((const char* pat1,const char* pat2,...)) __attribute__((noreturn));
149 /* Length of a variant. */
155 I32 pos_min; /* CC */
156 I32 pos_delta; /* CC */
158 I32 last_end; /* min value, <0 unless valid. */
159 I32 last_start_min; /* CC */
160 I32 last_start_max; /* CC */
161 SV **longest; /* Either &l_fixed, or &l_float. */
163 I32 offset_fixed; /* CC */
165 I32 offset_float_min; /* CC */
166 I32 offset_float_max; /* CC */
171 static scan_data_t zero_scan_data = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
173 #define SF_BEFORE_EOL (SF_BEFORE_SEOL|SF_BEFORE_MEOL)
174 #define SF_BEFORE_SEOL 0x1
175 #define SF_BEFORE_MEOL 0x2
176 #define SF_FIX_BEFORE_EOL (SF_FIX_BEFORE_SEOL|SF_FIX_BEFORE_MEOL)
177 #define SF_FL_BEFORE_EOL (SF_FL_BEFORE_SEOL|SF_FL_BEFORE_MEOL)
180 # define SF_FIX_SHIFT_EOL (0+2)
181 # define SF_FL_SHIFT_EOL (0+4)
183 # define SF_FIX_SHIFT_EOL (+2)
184 # define SF_FL_SHIFT_EOL (+4)
187 #define SF_FIX_BEFORE_SEOL (SF_BEFORE_SEOL << SF_FIX_SHIFT_EOL)
188 #define SF_FIX_BEFORE_MEOL (SF_BEFORE_MEOL << SF_FIX_SHIFT_EOL)
190 #define SF_FL_BEFORE_SEOL (SF_BEFORE_SEOL << SF_FL_SHIFT_EOL)
191 #define SF_FL_BEFORE_MEOL (SF_BEFORE_MEOL << SF_FL_SHIFT_EOL) /* 0x20 */
192 #define SF_IS_INF 0x40
193 #define SF_HAS_PAR 0x80
194 #define SF_IN_PAR 0x100
195 #define SF_HAS_EVAL 0x200
196 #define SCF_DO_SUBSTR 0x400
199 #define UTF (PL_reg_flags & RF_utf8)
200 #define LOC (PL_regflags & PMf_LOCALE)
201 #define FOLD (PL_regflags & PMf_FOLD)
203 #define CHR_SVLEN(sv) (UTF ? sv_len_utf8(sv) : SvCUR(sv))
204 #define CHR_DIST(a,b) (UTF ? utf8_distance(a,b) : a - b)
207 scan_commit(scan_data_t *data)
210 STRLEN l = CHR_SVLEN(data->last_found);
211 STRLEN old_l = CHR_SVLEN(*data->longest);
213 if ((l >= old_l) && ((l > old_l) || (data->flags & SF_BEFORE_EOL))) {
214 sv_setsv(*data->longest, data->last_found);
215 if (*data->longest == data->longest_fixed) {
216 data->offset_fixed = l ? data->last_start_min : data->pos_min;
217 if (data->flags & SF_BEFORE_EOL)
219 |= ((data->flags & SF_BEFORE_EOL) << SF_FIX_SHIFT_EOL);
221 data->flags &= ~SF_FIX_BEFORE_EOL;
224 data->offset_float_min = l ? data->last_start_min : data->pos_min;
225 data->offset_float_max = (l
226 ? data->last_start_max
227 : data->pos_min + data->pos_delta);
228 if (data->flags & SF_BEFORE_EOL)
230 |= ((data->flags & SF_BEFORE_EOL) << SF_FL_SHIFT_EOL);
232 data->flags &= ~SF_FL_BEFORE_EOL;
235 SvCUR_set(data->last_found, 0);
237 data->flags &= ~SF_BEFORE_EOL;
240 /* Stops at toplevel WHILEM as well as at `last'. At end *scanp is set
241 to the position after last scanned or to NULL. */
244 study_chunk(regnode **scanp, I32 *deltap, regnode *last, scan_data_t *data, U32 flags)
245 /* scanp: Start here (read-write). */
246 /* deltap: Write maxlen-minlen here. */
247 /* last: Stop before this one. */
250 I32 min = 0, pars = 0, code;
251 regnode *scan = *scanp, *next;
253 int is_inf = (flags & SCF_DO_SUBSTR) && (data->flags & SF_IS_INF);
254 I32 is_par = OP(scan) == OPEN ? ARG(scan) : 0;
255 scan_data_t data_fake;
257 while (scan && OP(scan) != END && scan < last) {
258 /* Peephole optimizer: */
260 if (regkind[(U8)OP(scan)] == EXACT) {
261 regnode *n = regnext(scan);
264 regnode *stop = scan;
267 next = scan + (*OPERAND(scan) + 2 - 1)/sizeof(regnode) + 2;
268 /* Skip NOTHING, merge EXACT*. */
270 ( regkind[(U8)OP(n)] == NOTHING ||
271 (stringok && (OP(n) == OP(scan))))
273 && NEXT_OFF(scan) + NEXT_OFF(n) < I16_MAX) {
274 if (OP(n) == TAIL || n > next)
276 if (regkind[(U8)OP(n)] == NOTHING) {
277 NEXT_OFF(scan) += NEXT_OFF(n);
278 next = n + NODE_STEP_REGNODE;
286 int oldl = *OPERAND(scan);
287 regnode *nnext = regnext(n);
289 if (oldl + *OPERAND(n) > U8_MAX)
291 NEXT_OFF(scan) += NEXT_OFF(n);
292 *OPERAND(scan) += *OPERAND(n);
293 next = n + (*OPERAND(n) + 2 - 1)/sizeof(regnode) + 2;
294 /* Now we can overwrite *n : */
295 Move(OPERAND(n) + 1, OPERAND(scan) + oldl + 1,
296 *OPERAND(n) + 1, char);
306 n = scan + (*OPERAND(scan) + 2 - 1)/sizeof(regnode) + 2;
308 /* Purify reports a benign UMR here sometimes, because we
309 * don't initialize the OP() slot of a node when that node
310 * is occupied by just the trailing null of the string in
312 if (regkind[(U8)OP(n)] != NOTHING || OP(n) == NOTHING) {
321 if (OP(scan) != CURLYX) {
322 int max = (reg_off_by_arg[OP(scan)]
324 /* I32 may be smaller than U16 on CRAYs! */
325 : (I32_MAX < U16_MAX ? I32_MAX : U16_MAX));
326 int off = (reg_off_by_arg[OP(scan)] ? ARG(scan) : NEXT_OFF(scan));
330 /* Skip NOTHING and LONGJMP. */
331 while ((n = regnext(n))
332 && ((regkind[(U8)OP(n)] == NOTHING && (noff = NEXT_OFF(n)))
333 || ((OP(n) == LONGJMP) && (noff = ARG(n))))
336 if (reg_off_by_arg[OP(scan)])
339 NEXT_OFF(scan) = off;
341 if (OP(scan) == BRANCH || OP(scan) == BRANCHJ
342 || OP(scan) == IFTHEN || OP(scan) == SUSPEND) {
343 next = regnext(scan);
346 if (OP(next) == code || code == IFTHEN || code == SUSPEND) {
347 I32 max1 = 0, min1 = I32_MAX, num = 0;
349 if (flags & SCF_DO_SUBSTR)
351 while (OP(scan) == code) {
352 I32 deltanext, minnext;
356 next = regnext(scan);
357 scan = NEXTOPER(scan);
359 scan = NEXTOPER(scan);
360 /* We suppose the run is continuous, last=next...*/
361 minnext = study_chunk(&scan, &deltanext, next,
365 if (max1 < minnext + deltanext)
366 max1 = minnext + deltanext;
367 if (deltanext == I32_MAX)
370 if (data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
372 if (data && (data_fake.flags & SF_HAS_EVAL))
373 data->flags |= SF_HAS_EVAL;
377 if (code == IFTHEN && num < 2) /* Empty ELSE branch */
379 if (flags & SCF_DO_SUBSTR) {
380 data->pos_min += min1;
381 data->pos_delta += max1 - min1;
382 if (max1 != min1 || is_inf)
383 data->longest = &(data->longest_float);
386 delta += max1 - min1;
388 else if (code == BRANCHJ) /* single branch is optimized. */
389 scan = NEXTOPER(NEXTOPER(scan));
390 else /* single branch is optimized. */
391 scan = NEXTOPER(scan);
394 else if (OP(scan) == EXACT) {
395 I32 l = *OPERAND(scan);
397 unsigned char *s = (unsigned char *)(OPERAND(scan)+1);
398 unsigned char *e = s + l;
407 if (flags & SCF_DO_SUBSTR) { /* Update longest substr. */
408 /* The code below prefers earlier match for fixed
409 offset, later match for variable offset. */
410 if (data->last_end == -1) { /* Update the start info. */
411 data->last_start_min = data->pos_min;
412 data->last_start_max = is_inf
413 ? I32_MAX : data->pos_min + data->pos_delta;
415 sv_catpvn(data->last_found, (char *)(OPERAND(scan)+1), *OPERAND(scan));
416 data->last_end = data->pos_min + l;
417 data->pos_min += l; /* As in the first entry. */
418 data->flags &= ~SF_BEFORE_EOL;
421 else if (regkind[(U8)OP(scan)] == EXACT) {
422 I32 l = *OPERAND(scan);
423 if (flags & SCF_DO_SUBSTR)
426 unsigned char *s = (unsigned char *)(OPERAND(scan)+1);
427 unsigned char *e = s + l;
436 if (data && (flags & SCF_DO_SUBSTR))
439 else if (strchr(varies,OP(scan))) {
440 I32 mincount, maxcount, minnext, deltanext, pos_before, fl;
441 regnode *oscan = scan;
443 switch (regkind[(U8)OP(scan)]) {
445 scan = NEXTOPER(scan);
448 if (flags & SCF_DO_SUBSTR) {
449 next = NEXTOPER(scan);
450 if (OP(next) == EXACT) {
452 maxcount = REG_INFTY;
453 next = regnext(scan);
454 scan = NEXTOPER(scan);
458 if (flags & SCF_DO_SUBSTR)
464 scan = regnext(scan);
465 if (flags & SCF_DO_SUBSTR) {
467 data->longest = &(data->longest_float);
469 goto optimize_curly_tail;
471 mincount = ARG1(scan);
472 maxcount = ARG2(scan);
473 next = regnext(scan);
474 scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
476 if (flags & SCF_DO_SUBSTR) {
477 if (mincount == 0) scan_commit(data);
478 pos_before = data->pos_min;
482 data->flags &= ~(SF_HAS_PAR|SF_IN_PAR|SF_HAS_EVAL);
484 data->flags |= SF_IS_INF;
486 /* This will finish on WHILEM, setting scan, or on NULL: */
487 minnext = study_chunk(&scan, &deltanext, last, data,
489 ? (flags & ~SCF_DO_SUBSTR) : flags);
490 if (!scan) /* It was not CURLYX, but CURLY. */
492 if (PL_dowarn && (minnext + deltanext == 0)
493 && !(data->flags & (SF_HAS_PAR|SF_IN_PAR))
494 && maxcount <= 10000) /* Complement check for big count */
495 warn("Strange *+?{} on zero-length expression");
496 min += minnext * mincount;
497 is_inf |= (maxcount == REG_INFTY && (minnext + deltanext) > 0
498 || deltanext == I32_MAX);
499 delta += (minnext + deltanext) * maxcount - minnext * mincount;
501 /* Try powerful optimization CURLYX => CURLYN. */
502 if ( OP(oscan) == CURLYX && data
503 && data->flags & SF_IN_PAR
504 && !(data->flags & SF_HAS_EVAL)
505 && !deltanext && minnext == 1 ) {
506 /* Try to optimize to CURLYN. */
507 regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS;
508 regnode *nxt1 = nxt, *nxt2;
512 if (!strchr(simple,OP(nxt))
513 && !(regkind[(U8)OP(nxt)] == EXACT
514 && *OPERAND(nxt) == 1))
518 if (OP(nxt) != CLOSE)
520 /* Now we know that nxt2 is the only contents: */
521 oscan->flags = ARG(nxt);
523 OP(nxt1) = NOTHING; /* was OPEN. */
525 OP(nxt1 + 1) = OPTIMIZED; /* was count. */
526 NEXT_OFF(nxt1+ 1) = 0; /* just for consistancy. */
527 NEXT_OFF(nxt2) = 0; /* just for consistancy with CURLY. */
528 OP(nxt) = OPTIMIZED; /* was CLOSE. */
529 OP(nxt + 1) = OPTIMIZED; /* was count. */
530 NEXT_OFF(nxt+ 1) = 0; /* just for consistancy. */
535 /* Try optimization CURLYX => CURLYM. */
536 if ( OP(oscan) == CURLYX && data
537 && !(data->flags & SF_HAS_PAR)
538 && !(data->flags & SF_HAS_EVAL)
540 /* XXXX How to optimize if data == 0? */
541 /* Optimize to a simpler form. */
542 regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN */
546 while ( (nxt2 = regnext(nxt)) /* skip over embedded stuff*/
547 && (OP(nxt2) != WHILEM))
549 OP(nxt2) = SUCCEED; /* Whas WHILEM */
550 /* Need to optimize away parenths. */
551 if (data->flags & SF_IN_PAR) {
552 /* Set the parenth number. */
553 regnode *nxt1 = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN*/
555 if (OP(nxt) != CLOSE)
556 FAIL("panic opt close");
557 oscan->flags = ARG(nxt);
558 OP(nxt1) = OPTIMIZED; /* was OPEN. */
559 OP(nxt) = OPTIMIZED; /* was CLOSE. */
561 OP(nxt1 + 1) = OPTIMIZED; /* was count. */
562 OP(nxt + 1) = OPTIMIZED; /* was count. */
563 NEXT_OFF(nxt1 + 1) = 0; /* just for consistancy. */
564 NEXT_OFF(nxt + 1) = 0; /* just for consistancy. */
567 while ( nxt1 && (OP(nxt1) != WHILEM)) {
568 regnode *nnxt = regnext(nxt1);
571 if (reg_off_by_arg[OP(nxt1)])
572 ARG_SET(nxt1, nxt2 - nxt1);
573 else if (nxt2 - nxt1 < U16_MAX)
574 NEXT_OFF(nxt1) = nxt2 - nxt1;
576 OP(nxt) = NOTHING; /* Cannot beautify */
581 /* Optimize again: */
582 study_chunk(&nxt1, &deltanext, nxt, NULL, 0);
587 if (data && fl & (SF_HAS_PAR|SF_IN_PAR))
589 if (flags & SCF_DO_SUBSTR) {
590 SV *last_str = Nullsv;
591 int counted = mincount != 0;
593 if (data->last_end > 0 && mincount != 0) { /* Ends with a string. */
594 I32 b = pos_before >= data->last_start_min
595 ? pos_before : data->last_start_min;
597 char *s = SvPV(data->last_found, l);
598 I32 old = b - data->last_start_min;
601 old = utf8_hop((U8*)s, old) - (U8*)s;
604 /* Get the added string: */
605 last_str = newSVpv(s + old, l);
606 if (deltanext == 0 && pos_before == b) {
607 /* What was added is a constant string */
609 SvGROW(last_str, (mincount * l) + 1);
610 repeatcpy(SvPVX(last_str) + l,
611 SvPVX(last_str), l, mincount - 1);
612 SvCUR(last_str) *= mincount;
613 /* Add additional parts. */
614 SvCUR_set(data->last_found,
615 SvCUR(data->last_found) - l);
616 sv_catsv(data->last_found, last_str);
617 data->last_end += l * (mincount - 1);
621 /* It is counted once already... */
622 data->pos_min += minnext * (mincount - counted);
623 data->pos_delta += - counted * deltanext +
624 (minnext + deltanext) * maxcount - minnext * mincount;
625 if (mincount != maxcount) {
627 if (mincount && last_str) {
628 sv_setsv(data->last_found, last_str);
629 data->last_end = data->pos_min;
630 data->last_start_min =
631 data->pos_min - CHR_SVLEN(last_str);
632 data->last_start_max = is_inf
634 : data->pos_min + data->pos_delta
635 - CHR_SVLEN(last_str);
637 data->longest = &(data->longest_float);
640 if (data && (fl & SF_HAS_EVAL))
641 data->flags |= SF_HAS_EVAL;
643 if (OP(oscan) != CURLYX) {
644 while (regkind[(U8)OP(next = regnext(oscan))] == NOTHING
646 NEXT_OFF(oscan) += NEXT_OFF(next);
649 default: /* REF only? */
650 if (flags & SCF_DO_SUBSTR) {
652 data->longest = &(data->longest_float);
658 else if (strchr(simple,OP(scan)) || regkind[(U8)OP(scan)] == ANYUTF8) {
659 if (flags & SCF_DO_SUBSTR) {
665 else if (regkind[(U8)OP(scan)] == EOL && flags & SCF_DO_SUBSTR) {
666 data->flags |= (OP(scan) == MEOL
670 else if (regkind[(U8)OP(scan)] == BRANCHJ
671 && (scan->flags || data)
672 && (OP(scan) == IFMATCH || OP(scan) == UNLESSM)) {
673 I32 deltanext, minnext;
677 next = regnext(scan);
678 nscan = NEXTOPER(NEXTOPER(scan));
679 minnext = study_chunk(&nscan, &deltanext, last, &data_fake, 0);
682 FAIL("variable length lookbehind not implemented");
684 else if (minnext > U8_MAX) {
685 FAIL2("lookbehind longer than %d not implemented", U8_MAX);
687 scan->flags = minnext;
689 if (data && data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
691 if (data && (data_fake.flags & SF_HAS_EVAL))
692 data->flags |= SF_HAS_EVAL;
694 else if (OP(scan) == OPEN) {
697 else if (OP(scan) == CLOSE && ARG(scan) == is_par) {
698 next = regnext(scan);
700 if ( next && (OP(next) != WHILEM) && next < last)
701 is_par = 0; /* Disable optimization */
703 else if (OP(scan) == EVAL) {
705 data->flags |= SF_HAS_EVAL;
707 /* Else: zero-length, ignore. */
708 scan = regnext(scan);
713 *deltap = is_inf ? I32_MAX : delta;
714 if (flags & SCF_DO_SUBSTR && is_inf)
715 data->pos_delta = I32_MAX - data->pos_min;
718 if (is_par && pars==1 && data) {
719 data->flags |= SF_IN_PAR;
720 data->flags &= ~SF_HAS_PAR;
722 else if (pars && data) {
723 data->flags |= SF_HAS_PAR;
724 data->flags &= ~SF_IN_PAR;
730 add_data(I32 n, char *s)
733 if (PL_regcomp_rx->data) {
734 Renewc(PL_regcomp_rx->data,
735 sizeof(*PL_regcomp_rx->data) + sizeof(void*) * (PL_regcomp_rx->data->count + n - 1),
736 char, struct reg_data);
737 Renew(PL_regcomp_rx->data->what, PL_regcomp_rx->data->count + n, U8);
738 PL_regcomp_rx->data->count += n;
741 Newc(1207, PL_regcomp_rx->data, sizeof(*PL_regcomp_rx->data) + sizeof(void*) * (n - 1),
742 char, struct reg_data);
743 New(1208, PL_regcomp_rx->data->what, n, U8);
744 PL_regcomp_rx->data->count = n;
746 Copy(s, PL_regcomp_rx->data->what + PL_regcomp_rx->data->count - n, n, U8);
747 return PL_regcomp_rx->data->count - n;
751 - pregcomp - compile a regular expression into internal code
753 * We can't allocate space until we know how big the compiled form will be,
754 * but we can't compile it (and thus know how big it is) until we've got a
755 * place to put the code. So we cheat: we compile it twice, once with code
756 * generation turned off and size counting turned on, and once "for real".
757 * This also means that we don't allocate space until we are sure that the
758 * thing really will compile successfully, and we never have to move the
759 * code and thus invalidate pointers into it. (Note that it has to be in
760 * one piece because free() must be able to free it all.) [NB: not true in perl]
762 * Beware that the optimization-preparation code in here knows about some
763 * of the structure of the compiled regexp. [I'll say.]
766 pregcomp(char *exp, char *xend, PMOP *pm)
781 FAIL("NULL regexp argument");
783 if (PL_curcop == &compiling ? (PL_hints & HINT_UTF8) : IN_UTF8)
784 PL_reg_flags |= RF_utf8;
788 PL_regprecomp = savepvn(exp, xend - exp);
789 DEBUG_r(PerlIO_printf(Perl_debug_log, "compiling RE `%*s'\n",
790 xend - exp, PL_regprecomp));
791 PL_regflags = pm->op_pmflags;
795 PL_seen_zerolen = *exp == '^' ? -1 : 0;
799 /* First pass: determine size, legality. */
800 PL_regcomp_parse = exp;
805 PL_regcode = &PL_regdummy;
806 regc((U8)MAGIC, (char*)PL_regcode);
807 if (reg(0, &flags) == NULL) {
808 Safefree(PL_regprecomp);
809 PL_regprecomp = Nullch;
812 DEBUG_r(PerlIO_printf(Perl_debug_log, "size %d ", PL_regsize));
817 char *s = PerlEnv_getenv("TERMCAP_COLORS");
821 PL_colors[0] = s = savepv(s);
825 FAIL("Not enough TABs in TERMCAP_COLORS");
835 PerlIO_printf(Perl_debug_log, "%s%s%s%s",
836 PL_colors[0],PL_colors[1],PL_colors[2],PL_colors[3]);
840 /* Small enough for pointer-storage convention?
841 If extralen==0, this means that we will not need long jumps. */
842 if (PL_regsize >= 0x10000L && PL_extralen)
843 PL_regsize += PL_extralen;
847 /* Allocate space and initialize. */
848 Newc(1001, r, sizeof(regexp) + (unsigned)PL_regsize * sizeof(regnode),
851 FAIL("regexp out of space");
853 r->prelen = xend - exp;
854 r->precomp = PL_regprecomp;
855 r->subbeg = r->subbase = NULL;
856 r->nparens = PL_regnpar - 1; /* set early to validate backrefs */
859 /* Second pass: emit code. */
860 PL_regcomp_parse = exp;
864 PL_regcode = r->program;
865 /* Store the count of eval-groups for security checks: */
866 PL_regcode->next_off = ((PL_seen_evals > U16_MAX) ? U16_MAX : PL_seen_evals);
867 regc((U8)MAGIC, (char*) PL_regcode++);
869 if (reg(0, &flags) == NULL)
872 /* Dig out information for optimizations. */
873 r->reganch = pm->op_pmflags & PMf_COMPILETIME;
874 pm->op_pmflags = PL_regflags;
876 r->reganch |= ROPT_UTF8;
877 r->regstclass = NULL;
878 if (PL_regnaughty >= 10) /* Probably an expensive pattern. */
879 r->reganch |= ROPT_NAUGHTY;
880 scan = r->program + 1; /* First BRANCH. */
882 /* XXXX To minimize changes to RE engine we always allocate
883 3-units-long substrs field. */
884 Newz(1004, r->substrs, 1, struct reg_substr_data);
886 if (OP(scan) != BRANCH) { /* Only one top-level choice. */
889 STRLEN longest_float_length, longest_fixed_length;
891 StructCopy(&zero_scan_data, &data, scan_data_t);
893 /* Skip introductions and multiplicators >= 1. */
894 while ((OP(first) == OPEN && (sawopen = 1)) ||
895 (OP(first) == BRANCH && OP(regnext(first)) != BRANCH) ||
896 (OP(first) == PLUS) ||
897 (OP(first) == MINMOD) ||
898 (regkind[(U8)OP(first)] == CURLY && ARG1(first) > 0) ) {
899 if (OP(first) == PLUS)
902 first += regarglen[(U8)OP(first)];
903 first = NEXTOPER(first);
906 /* Starting-point info. */
908 if (OP(first) == EXACT); /* Empty, get anchored substr later. */
909 else if (strchr(simple+4,OP(first)))
910 r->regstclass = first;
911 else if (regkind[(U8)OP(first)] == BOUND ||
912 regkind[(U8)OP(first)] == NBOUND)
913 r->regstclass = first;
914 else if (regkind[(U8)OP(first)] == BOL) {
915 r->reganch |= (OP(first) == MBOL ? ROPT_ANCH_MBOL: ROPT_ANCH_BOL);
916 first = NEXTOPER(first);
919 else if (OP(first) == GPOS) {
920 r->reganch |= ROPT_ANCH_GPOS;
921 first = NEXTOPER(first);
924 else if ((OP(first) == STAR &&
925 regkind[(U8)OP(NEXTOPER(first))] == ANY) &&
926 !(r->reganch & ROPT_ANCH) )
928 /* turn .* into ^.* with an implied $*=1 */
929 r->reganch |= ROPT_ANCH_BOL | ROPT_IMPLICIT;
930 first = NEXTOPER(first);
933 if (sawplus && (!sawopen || !PL_regsawback))
934 r->reganch |= ROPT_SKIP; /* x+ must match 1st of run */
936 /* Scan is after the zeroth branch, first is atomic matcher. */
937 DEBUG_r(PerlIO_printf(Perl_debug_log, "first at %d\n",
940 * If there's something expensive in the r.e., find the
941 * longest literal string that must appear and make it the
942 * regmust. Resolve ties in favor of later strings, since
943 * the regstart check works with the beginning of the r.e.
944 * and avoiding duplication strengthens checking. Not a
945 * strong reason, but sufficient in the absence of others.
946 * [Now we resolve ties in favor of the earlier string if
947 * it happens that c_offset_min has been invalidated, since the
948 * earlier string may buy us something the later one won't.]
952 data.longest_fixed = newSVpv("",0);
953 data.longest_float = newSVpv("",0);
954 data.last_found = newSVpv("",0);
955 data.longest = &(data.longest_fixed);
958 minlen = study_chunk(&first, &fake, scan + PL_regsize, /* Up to end */
959 &data, SCF_DO_SUBSTR);
960 if ( PL_regnpar == 1 && data.longest == &(data.longest_fixed)
961 && data.last_start_min == 0 && data.last_end > 0
963 && (!(PL_regseen & REG_SEEN_GPOS) || (r->reganch & ROPT_ANCH_GPOS)))
964 r->reganch |= ROPT_CHECK_ALL;
966 SvREFCNT_dec(data.last_found);
968 longest_float_length = CHR_SVLEN(data.longest_float);
969 if (longest_float_length
970 || (data.flags & SF_FL_BEFORE_EOL
971 && (!(data.flags & SF_FL_BEFORE_MEOL)
972 || (PL_regflags & PMf_MULTILINE)))) {
973 if (SvCUR(data.longest_fixed) /* ok to leave SvCUR */
974 && data.offset_fixed == data.offset_float_min)
975 goto remove; /* Like in (a)+. */
977 r->float_substr = data.longest_float;
978 r->float_min_offset = data.offset_float_min;
979 r->float_max_offset = data.offset_float_max;
980 fbm_compile(r->float_substr, 0);
981 BmUSEFUL(r->float_substr) = 100;
982 if (data.flags & SF_FL_BEFORE_EOL /* Cannot have SEOL and MULTI */
983 && (!(data.flags & SF_FL_BEFORE_MEOL)
984 || (PL_regflags & PMf_MULTILINE)))
985 SvTAIL_on(r->float_substr);
989 r->float_substr = Nullsv;
990 SvREFCNT_dec(data.longest_float);
991 longest_float_length = 0;
994 longest_fixed_length = CHR_SVLEN(data.longest_fixed);
995 if (longest_fixed_length
996 || (data.flags & SF_FIX_BEFORE_EOL /* Cannot have SEOL and MULTI */
997 && (!(data.flags & SF_FIX_BEFORE_MEOL)
998 || (PL_regflags & PMf_MULTILINE)))) {
999 r->anchored_substr = data.longest_fixed;
1000 r->anchored_offset = data.offset_fixed;
1001 fbm_compile(r->anchored_substr, 0);
1002 BmUSEFUL(r->anchored_substr) = 100;
1003 if (data.flags & SF_FIX_BEFORE_EOL /* Cannot have SEOL and MULTI */
1004 && (!(data.flags & SF_FIX_BEFORE_MEOL)
1005 || (PL_regflags & PMf_MULTILINE)))
1006 SvTAIL_on(r->anchored_substr);
1009 r->anchored_substr = Nullsv;
1010 SvREFCNT_dec(data.longest_fixed);
1011 longest_fixed_length = 0;
1014 /* A temporary algorithm prefers floated substr to fixed one to dig more info. */
1015 if (longest_fixed_length > longest_float_length) {
1016 r->check_substr = r->anchored_substr;
1017 r->check_offset_min = r->check_offset_max = r->anchored_offset;
1018 if (r->reganch & ROPT_ANCH_SINGLE)
1019 r->reganch |= ROPT_NOSCAN;
1022 r->check_substr = r->float_substr;
1023 r->check_offset_min = data.offset_float_min;
1024 r->check_offset_max = data.offset_float_max;
1028 /* Several toplevels. Best we can is to set minlen. */
1031 DEBUG_r(PerlIO_printf(Perl_debug_log, "\n"));
1032 scan = r->program + 1;
1033 minlen = study_chunk(&scan, &fake, scan + PL_regsize, NULL, 0);
1034 r->check_substr = r->anchored_substr = r->float_substr = Nullsv;
1038 if (PL_regseen & REG_SEEN_GPOS)
1039 r->reganch |= ROPT_GPOS_SEEN;
1040 if (PL_regseen & REG_SEEN_LOOKBEHIND)
1041 r->reganch |= ROPT_LOOKBEHIND_SEEN;
1042 if (PL_regseen & REG_SEEN_EVAL)
1043 r->reganch |= ROPT_EVAL_SEEN;
1044 Newz(1002, r->startp, PL_regnpar, char*);
1045 Newz(1002, r->endp, PL_regnpar, char*);
1046 DEBUG_r(regdump(r));
1051 - reg - regular expression, i.e. main body or parenthesized thing
1053 * Caller must absorb opening parenthesis.
1055 * Combining parenthesis handling with the base level of regular expression
1056 * is a trifle forced, but the need to tie the tails of the branches to what
1057 * follows makes it hard to avoid.
1060 reg(I32 paren, I32 *flagp)
1061 /* paren: Parenthesized? 0=top, 1=(, inside: changed to letter. */
1064 register regnode *ret; /* Will be the head of the group. */
1065 register regnode *br;
1066 register regnode *lastbr;
1067 register regnode *ender = 0;
1068 register I32 parno = 0;
1069 I32 flags, oregflags = PL_regflags, have_branch = 0, open = 0;
1072 *flagp = 0; /* Tentatively. */
1074 /* Make an OPEN node, if parenthesized. */
1076 if (*PL_regcomp_parse == '?') {
1077 U16 posflags = 0, negflags = 0;
1078 U16 *flagsp = &posflags;
1081 paren = *PL_regcomp_parse++;
1082 ret = NULL; /* For look-ahead/behind. */
1085 PL_regseen |= REG_SEEN_LOOKBEHIND;
1086 if (*PL_regcomp_parse == '!')
1088 if (*PL_regcomp_parse != '=' && *PL_regcomp_parse != '!')
1099 FAIL2("Sequence (?%c...) not implemented", (int)paren);
1102 while (*PL_regcomp_parse && *PL_regcomp_parse != ')')
1104 if (*PL_regcomp_parse != ')')
1105 FAIL("Sequence (?#... not terminated");
1112 I32 count = 1, n = 0;
1114 char *s = PL_regcomp_parse;
1116 OP_4tree *sop, *rop;
1119 PL_regseen |= REG_SEEN_EVAL;
1120 while (count && (c = *PL_regcomp_parse)) {
1121 if (c == '\\' && PL_regcomp_parse[1])
1129 if (*PL_regcomp_parse != ')')
1130 FAIL("Sequence (?{...}) not terminated or not {}-balanced");
1134 if (PL_regcomp_parse - 1 - s)
1135 sv = newSVpv(s, PL_regcomp_parse - 1 - s);
1137 sv = newSVpv("", 0);
1139 rop = sv_compile_2op(sv, &sop, "re", &av);
1141 n = add_data(3, "nso");
1142 PL_regcomp_rx->data->data[n] = (void*)rop;
1143 PL_regcomp_rx->data->data[n+1] = (void*)av;
1144 PL_regcomp_rx->data->data[n+2] = (void*)sop;
1147 else { /* First pass */
1148 if (PL_reginterp_cnt < ++PL_seen_evals && PL_curcop != &compiling)
1149 /* No compiled RE interpolated, has runtime
1150 components ===> unsafe. */
1151 FAIL("Eval-group not allowed at runtime, use re 'eval'");
1153 FAIL("Eval-group in insecure regular expression");
1157 return reganode(EVAL, n);
1161 if (PL_regcomp_parse[0] == '?') {
1162 if (PL_regcomp_parse[1] == '=' || PL_regcomp_parse[1] == '!'
1163 || PL_regcomp_parse[1] == '<'
1164 || PL_regcomp_parse[1] == '{') { /* Lookahead or eval. */
1167 ret = reg_node(LOGICAL);
1168 regtail(ret, reg(1, &flag));
1172 else if (PL_regcomp_parse[0] >= '1' && PL_regcomp_parse[0] <= '9' ) {
1173 parno = atoi(PL_regcomp_parse++);
1175 while (isDIGIT(*PL_regcomp_parse))
1177 ret = reganode(GROUPP, parno);
1178 if ((c = *nextchar()) != ')')
1179 FAIL2("Switch (?(number%c not recognized", c);
1181 regtail(ret, reganode(IFTHEN, 0));
1182 br = regbranch(&flags, 1);
1184 br = reganode(LONGJMP, 0);
1186 regtail(br, reganode(LONGJMP, 0));
1189 lastbr = reganode(IFTHEN, 0); /* Fake one for optimizer. */
1190 regbranch(&flags, 1);
1191 regtail(ret, lastbr);
1197 FAIL("Switch (?(condition)... contains too many branches");
1198 ender = reg_node(TAIL);
1201 regtail(lastbr, ender);
1202 regtail(NEXTOPER(NEXTOPER(lastbr)), ender);
1205 regtail(ret, ender);
1209 FAIL2("Unknown condition for (?(%.2s", PL_regcomp_parse);
1213 FAIL("Sequence (? incomplete");
1218 while (*PL_regcomp_parse && strchr("iogcmsx", *PL_regcomp_parse)) {
1219 if (*PL_regcomp_parse != 'o')
1220 pmflag(flagsp, *PL_regcomp_parse);
1223 if (*PL_regcomp_parse == '-') {
1228 PL_regflags |= posflags;
1229 PL_regflags &= ~negflags;
1230 if (*PL_regcomp_parse == ':') {
1236 if (*PL_regcomp_parse != ')')
1237 FAIL2("Sequence (?%c...) not recognized", *PL_regcomp_parse);
1246 ret = reganode(OPEN, parno);
1253 /* Pick up the branches, linking them together. */
1254 br = regbranch(&flags, 1);
1257 if (*PL_regcomp_parse == '|') {
1258 if (!SIZE_ONLY && PL_extralen) {
1259 reginsert(BRANCHJ, br);
1262 reginsert(BRANCH, br);
1265 PL_extralen += 1; /* For BRANCHJ-BRANCH. */
1267 else if (paren == ':') {
1268 *flagp |= flags&SIMPLE;
1270 if (open) { /* Starts with OPEN. */
1271 regtail(ret, br); /* OPEN -> first. */
1273 else if (paren != '?') /* Not Conditional */
1277 *flagp |= flags&SPSTART;
1279 while (*PL_regcomp_parse == '|') {
1280 if (!SIZE_ONLY && PL_extralen) {
1281 ender = reganode(LONGJMP,0);
1282 regtail(NEXTOPER(NEXTOPER(lastbr)), ender); /* Append to the previous. */
1285 PL_extralen += 2; /* Account for LONGJMP. */
1287 br = regbranch(&flags, 0);
1290 regtail(lastbr, br); /* BRANCH -> BRANCH. */
1294 *flagp |= flags&SPSTART;
1297 if (have_branch || paren != ':') {
1298 /* Make a closing node, and hook it on the end. */
1301 ender = reg_node(TAIL);
1304 ender = reganode(CLOSE, parno);
1310 *flagp &= ~HASWIDTH;
1313 ender = reg_node(SUCCEED);
1316 ender = reg_node(END);
1319 regtail(lastbr, ender);
1322 /* Hook the tails of the branches to the closing node. */
1323 for (br = ret; br != NULL; br = regnext(br)) {
1324 regoptail(br, ender);
1331 static char parens[] = "=!<,>";
1333 if (paren && (p = strchr(parens, paren))) {
1334 int node = ((p - parens) % 2) ? UNLESSM : IFMATCH;
1335 int flag = (p - parens) > 1;
1338 node = SUSPEND, flag = 0;
1339 reginsert(node,ret);
1341 regtail(ret, reg_node(TAIL));
1345 /* Check for proper termination. */
1346 if (paren && (PL_regcomp_parse >= PL_regxend || *nextchar() != ')')) {
1347 FAIL("unmatched () in regexp");
1349 else if (!paren && PL_regcomp_parse < PL_regxend) {
1350 if (*PL_regcomp_parse == ')') {
1351 FAIL("unmatched () in regexp");
1354 FAIL("junk on end of regexp"); /* "Can't happen". */
1358 PL_regflags = oregflags;
1365 - regbranch - one alternative of an | operator
1367 * Implements the concatenation operator.
1370 regbranch(I32 *flagp, I32 first)
1373 register regnode *ret;
1374 register regnode *chain = NULL;
1375 register regnode *latest;
1376 I32 flags = 0, c = 0;
1381 if (!SIZE_ONLY && PL_extralen)
1382 ret = reganode(BRANCHJ,0);
1384 ret = reg_node(BRANCH);
1387 if (!first && SIZE_ONLY)
1388 PL_extralen += 1; /* BRANCHJ */
1390 *flagp = WORST; /* Tentatively. */
1394 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != '|' && *PL_regcomp_parse != ')') {
1396 latest = regpiece(&flags);
1397 if (latest == NULL) {
1398 if (flags & TRYAGAIN)
1402 else if (ret == NULL)
1404 *flagp |= flags&HASWIDTH;
1405 if (chain == NULL) /* First piece. */
1406 *flagp |= flags&SPSTART;
1409 regtail(chain, latest);
1414 if (chain == NULL) { /* Loop ran zero times. */
1415 chain = reg_node(NOTHING);
1420 *flagp |= flags&SIMPLE;
1427 - regpiece - something followed by possible [*+?]
1429 * Note that the branching code sequences used for ? and the general cases
1430 * of * and + are somewhat optimized: they use the same NOTHING node as
1431 * both the endmarker for their branch list and the body of the last branch.
1432 * It might seem that this node could be dispensed with entirely, but the
1433 * endmarker role is not redundant.
1436 regpiece(I32 *flagp)
1439 register regnode *ret;
1441 register char *next;
1443 char *origparse = PL_regcomp_parse;
1446 I32 max = REG_INFTY;
1448 ret = regatom(&flags);
1450 if (flags & TRYAGAIN)
1455 op = *PL_regcomp_parse;
1457 if (op == '{' && regcurly(PL_regcomp_parse)) {
1458 next = PL_regcomp_parse + 1;
1460 while (isDIGIT(*next) || *next == ',') {
1469 if (*next == '}') { /* got one */
1473 min = atoi(PL_regcomp_parse);
1477 maxpos = PL_regcomp_parse;
1479 if (!max && *maxpos != '0')
1480 max = REG_INFTY; /* meaning "infinity" */
1481 else if (max >= REG_INFTY)
1482 FAIL2("Quantifier in {,} bigger than %d", REG_INFTY - 1);
1483 PL_regcomp_parse = next;
1487 if ((flags&SIMPLE)) {
1488 PL_regnaughty += 2 + PL_regnaughty / 2;
1489 reginsert(CURLY, ret);
1492 PL_regnaughty += 4 + PL_regnaughty; /* compound interest */
1493 regtail(ret, reg_node(WHILEM));
1494 if (!SIZE_ONLY && PL_extralen) {
1495 reginsert(LONGJMP,ret);
1496 reginsert(NOTHING,ret);
1497 NEXT_OFF(ret) = 3; /* Go over LONGJMP. */
1499 reginsert(CURLYX,ret);
1500 if (!SIZE_ONLY && PL_extralen)
1501 NEXT_OFF(ret) = 3; /* Go over NOTHING to LONGJMP. */
1502 regtail(ret, reg_node(NOTHING));
1512 if (max && max < min)
1513 FAIL("Can't do {n,m} with n > m");
1528 #if 0 /* Now runtime fix should be reliable. */
1529 if (!(flags&HASWIDTH) && op != '?')
1530 FAIL("regexp *+ operand could be empty");
1535 *flagp = (op != '+') ? (WORST|SPSTART|HASWIDTH) : (WORST|HASWIDTH);
1537 if (op == '*' && (flags&SIMPLE)) {
1538 reginsert(STAR, ret);
1542 else if (op == '*') {
1546 else if (op == '+' && (flags&SIMPLE)) {
1547 reginsert(PLUS, ret);
1551 else if (op == '+') {
1555 else if (op == '?') {
1560 if (PL_dowarn && !SIZE_ONLY && !(flags&HASWIDTH) && max > 10000) {
1561 warn("%.*s matches null string many times",
1562 PL_regcomp_parse - origparse, origparse);
1565 if (*PL_regcomp_parse == '?') {
1567 reginsert(MINMOD, ret);
1568 regtail(ret, ret + NODE_STEP_REGNODE);
1570 if (ISMULT2(PL_regcomp_parse))
1571 FAIL("nested *?+ in regexp");
1577 - regatom - the lowest level
1579 * Optimization: gobbles an entire sequence of ordinary characters so that
1580 * it can turn them into a single node, which is smaller to store and
1581 * faster to run. Backslashed characters are exceptions, each becoming a
1582 * separate node; the code is simpler that way and it's not worth fixing.
1584 * [Yes, it is worth fixing, some scripts can run twice the speed.]
1590 register regnode *ret = 0;
1593 *flagp = WORST; /* Tentatively. */
1596 switch (*PL_regcomp_parse) {
1600 if (PL_regflags & PMf_MULTILINE)
1601 ret = reg_node(MBOL);
1602 else if (PL_regflags & PMf_SINGLELINE)
1603 ret = reg_node(SBOL);
1605 ret = reg_node(BOL);
1608 if (PL_regcomp_parse[1])
1611 if (PL_regflags & PMf_MULTILINE)
1612 ret = reg_node(MEOL);
1613 else if (PL_regflags & PMf_SINGLELINE)
1614 ret = reg_node(SEOL);
1616 ret = reg_node(EOL);
1621 if (PL_regflags & PMf_SINGLELINE)
1622 ret = reg_node(SANYUTF8);
1624 ret = reg_node(ANYUTF8);
1628 if (PL_regflags & PMf_SINGLELINE)
1629 ret = reg_node(SANY);
1631 ret = reg_node(ANY);
1632 *flagp |= HASWIDTH|SIMPLE;
1638 ret = (UTF ? regclassutf8() : regclass());
1639 *flagp |= HASWIDTH|SIMPLE;
1643 ret = reg(1, &flags);
1645 if (flags & TRYAGAIN)
1649 *flagp |= flags&(HASWIDTH|SPSTART|SIMPLE);
1653 if (flags & TRYAGAIN) {
1657 FAIL2("internal urp in regexp at /%s/", PL_regcomp_parse);
1658 /* Supposed to be caught earlier. */
1661 if (!regcurly(PL_regcomp_parse)) {
1669 FAIL("?+*{} follows nothing in regexp");
1672 switch (*++PL_regcomp_parse) {
1675 ret = reg_node(SBOL);
1680 ret = reg_node(GPOS);
1681 PL_regseen |= REG_SEEN_GPOS;
1686 ret = reg_node(SEOL);
1691 ret = reg_node(EOS);
1693 PL_seen_zerolen++; /* Do not optimize RE away */
1697 ret = reg_node(SANY);
1698 *flagp |= HASWIDTH|SIMPLE;
1702 ret = reg_node(CLUMP);
1705 if (UTF && !PL_utf8_mark)
1706 is_utf8_mark("~"); /* preload table */
1711 ? (LOC ? ALNUMLUTF8 : ALNUMUTF8)
1712 : (LOC ? ALNUML : ALNUM));
1713 *flagp |= HASWIDTH|SIMPLE;
1715 if (UTF && !PL_utf8_alnum)
1716 is_utf8_alnum("a"); /* preload table */
1721 ? (LOC ? NALNUMLUTF8 : NALNUMUTF8)
1722 : (LOC ? NALNUML : NALNUM));
1723 *flagp |= HASWIDTH|SIMPLE;
1725 if (UTF && !PL_utf8_alnum)
1726 is_utf8_alnum("a"); /* preload table */
1732 ? (LOC ? BOUNDLUTF8 : BOUNDUTF8)
1733 : (LOC ? BOUNDL : BOUND));
1736 if (UTF && !PL_utf8_alnum)
1737 is_utf8_alnum("a"); /* preload table */
1743 ? (LOC ? NBOUNDLUTF8 : NBOUNDUTF8)
1744 : (LOC ? NBOUNDL : NBOUND));
1747 if (UTF && !PL_utf8_alnum)
1748 is_utf8_alnum("a"); /* preload table */
1753 ? (LOC ? SPACELUTF8 : SPACEUTF8)
1754 : (LOC ? SPACEL : SPACE));
1755 *flagp |= HASWIDTH|SIMPLE;
1757 if (UTF && !PL_utf8_space)
1758 is_utf8_space(" "); /* preload table */
1763 ? (LOC ? NSPACELUTF8 : NSPACEUTF8)
1764 : (LOC ? NSPACEL : NSPACE));
1765 *flagp |= HASWIDTH|SIMPLE;
1767 if (UTF && !PL_utf8_space)
1768 is_utf8_space(" "); /* preload table */
1771 ret = reg_node(UTF ? DIGITUTF8 : DIGIT);
1772 *flagp |= HASWIDTH|SIMPLE;
1774 if (UTF && !PL_utf8_digit)
1775 is_utf8_digit("1"); /* preload table */
1778 ret = reg_node(UTF ? NDIGITUTF8 : NDIGIT);
1779 *flagp |= HASWIDTH|SIMPLE;
1781 if (UTF && !PL_utf8_digit)
1782 is_utf8_digit("1"); /* preload table */
1794 case '1': case '2': case '3': case '4':
1795 case '5': case '6': case '7': case '8': case '9':
1797 I32 num = atoi(PL_regcomp_parse);
1799 if (num > 9 && num >= PL_regnpar)
1802 if (!SIZE_ONLY && num > PL_regcomp_rx->nparens)
1803 FAIL("reference to nonexistent group");
1806 ? (LOC ? REFFL : REFF)
1809 while (isDIGIT(*PL_regcomp_parse))
1817 if (PL_regcomp_parse >= PL_regxend)
1818 FAIL("trailing \\ in regexp");
1826 if (PL_regflags & PMf_EXTENDED) {
1827 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != '\n') PL_regcomp_parse++;
1828 if (PL_regcomp_parse < PL_regxend)
1844 ? (LOC ? EXACTFL : EXACTF)
1846 s = (char *) OPERAND(ret);
1847 regc(0, s++); /* save spot for len */
1848 for (len = 0, p = PL_regcomp_parse - 1;
1849 len < 127 && p < PL_regxend;
1854 if (PL_regflags & PMf_EXTENDED)
1855 p = regwhite(p, PL_regxend);
1907 char* e = strchr(p, '}');
1910 FAIL("Missing right brace on \\x{}");
1912 ender = scan_hex(p + 1, e - p, &numlen);
1913 if (numlen + len >= 127) { /* numlen is generous */
1920 FAIL("Can't use \\x{} without 'use utf8' declaration");
1923 ender = scan_hex(p, 2, &numlen);
1929 ender = UCHARAT(p++);
1930 ender = toCTRL(ender);
1932 case '0': case '1': case '2': case '3':case '4':
1933 case '5': case '6': case '7': case '8':case '9':
1935 (isDIGIT(p[1]) && atoi(p) >= PL_regnpar) ) {
1936 ender = scan_oct(p, 3, &numlen);
1945 if (p >= PL_regxend)
1946 FAIL("trailing \\ in regexp");
1949 goto normal_default;
1954 if ((*p & 0xc0) == 0xc0 && UTF) {
1955 ender = utf8_to_uv(p, &numlen);
1962 if (PL_regflags & PMf_EXTENDED)
1963 p = regwhite(p, PL_regxend);
1966 ender = toLOWER_LC_uni(ender);
1968 ender = toLOWER_uni(ender);
1970 if (ISMULT2(p)) { /* Back off on ?+*. */
1973 else if (ender >= 0x80 && UTF) {
1974 reguni(ender, s, &numlen);
1984 if (ender >= 0x80 && UTF) {
1985 reguni(ender, s, &numlen);
1993 PL_regcomp_parse = p - 1;
1996 FAIL("internal disaster in regexp");
2002 *OPERAND(ret) = len;
2005 PL_regsize += (len + 2 + sizeof(regnode) - 1) / sizeof(regnode);
2008 PL_regcode += (len + 2 + sizeof(regnode) - 1) / sizeof(regnode);
2018 regwhite(char *p, char *e)
2023 else if (*p == '#') {
2026 } while (p < e && *p != '\n');
2038 register char *opnd, *s;
2040 register I32 lastvalue = 1234;
2041 register I32 range = 0;
2042 register regnode *ret;
2046 s = opnd = (char *) OPERAND(PL_regcode);
2047 ret = reg_node(ANYOF);
2048 for (value = 0; value < 33; value++)
2050 if (*PL_regcomp_parse == '^') { /* Complement of range. */
2054 *opnd |= ANYOF_INVERT;
2057 PL_regcode += ANY_SKIP;
2059 *opnd |= ANYOF_FOLD;
2061 *opnd |= ANYOF_LOCALE;
2064 PL_regsize += ANY_SKIP;
2066 if (*PL_regcomp_parse == ']' || *PL_regcomp_parse == '-')
2067 goto skipcond; /* allow 1st char to be ] or - */
2068 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != ']') {
2070 value = UCHARAT(PL_regcomp_parse++);
2071 if (value == '[' && PL_regcomp_parse + 1 < PL_regxend &&
2072 /* I smell either [: or [= or [. -- POSIX has been here, right? */
2073 (*PL_regcomp_parse == ':' || *PL_regcomp_parse == '=' || *PL_regcomp_parse == '.')) {
2074 char posixccc = *PL_regcomp_parse;
2075 char* posixccs = PL_regcomp_parse++;
2077 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != posixccc)
2079 if (PL_regcomp_parse == PL_regxend)
2080 /* Grandfather lone [:, [=, [. */
2081 PL_regcomp_parse = posixccs;
2083 PL_regcomp_parse++; /* skip over the posixccc */
2084 if (*PL_regcomp_parse == ']') {
2085 /* Not Implemented Yet.
2086 * (POSIX Extended Character Classes, that is)
2087 * The text between e.g. [: and :] would start
2088 * at posixccs + 1 and stop at regcomp_parse - 2. */
2089 if (dowarn && !SIZE_ONLY)
2090 warn("Character class syntax [%c %c] is reserved for future extensions", posixccc, posixccc);
2091 PL_regcomp_parse++; /* skip over the ending ] */
2095 if (value == '\\') {
2096 value = UCHARAT(PL_regcomp_parse++);
2101 *opnd |= ANYOF_ALNUML;
2103 for (value = 0; value < 256; value++)
2105 ANYOF_SET(opnd, value);
2113 *opnd |= ANYOF_NALNUML;
2115 for (value = 0; value < 256; value++)
2116 if (!isALNUM(value))
2117 ANYOF_SET(opnd, value);
2125 *opnd |= ANYOF_SPACEL;
2127 for (value = 0; value < 256; value++)
2129 ANYOF_SET(opnd, value);
2137 *opnd |= ANYOF_NSPACEL;
2139 for (value = 0; value < 256; value++)
2140 if (!isSPACE(value))
2141 ANYOF_SET(opnd, value);
2148 for (value = '0'; value <= '9'; value++)
2149 ANYOF_SET(opnd, value);
2155 for (value = 0; value < '0'; value++)
2156 ANYOF_SET(opnd, value);
2157 for (value = '9' + 1; value < 256; value++)
2158 ANYOF_SET(opnd, value);
2184 value = scan_hex(PL_regcomp_parse, 2, &numlen);
2185 PL_regcomp_parse += numlen;
2188 value = UCHARAT(PL_regcomp_parse++);
2189 value = toCTRL(value);
2191 case '0': case '1': case '2': case '3': case '4':
2192 case '5': case '6': case '7': case '8': case '9':
2193 value = scan_oct(--PL_regcomp_parse, 3, &numlen);
2194 PL_regcomp_parse += numlen;
2199 if (lastvalue > value)
2200 FAIL("invalid [] range in regexp");
2205 if (*PL_regcomp_parse == '-' && PL_regcomp_parse+1 < PL_regxend &&
2206 PL_regcomp_parse[1] != ']') {
2209 continue; /* do it next time */
2213 for ( ; lastvalue <= value; lastvalue++)
2214 ANYOF_SET(opnd, lastvalue);
2218 if (*PL_regcomp_parse != ']')
2219 FAIL("unmatched [] in regexp");
2221 /* optimize case-insensitive simple patterns (e.g. /[a-z]/i) */
2222 if (!SIZE_ONLY && (*opnd & (0xFF ^ ANYOF_INVERT)) == ANYOF_FOLD) {
2223 for (value = 0; value < 256; ++value) {
2224 if (ANYOF_TEST(opnd, value)) {
2225 I32 cf = fold[value];
2226 ANYOF_SET(opnd, cf);
2229 *opnd &= ~ANYOF_FOLD;
2231 /* optimize inverted simple patterns (e.g. [^a-z]) */
2232 if (!SIZE_ONLY && (*opnd & 0xFF) == ANYOF_INVERT) {
2233 for (value = 0; value < 32; ++value)
2234 opnd[1 + value] ^= 0xFF;
2243 register char *opnd, *e;
2245 register U32 lastvalue = 123456;
2246 register I32 range = 0;
2247 register regnode *ret;
2254 if (*PL_regcomp_parse == '^') { /* Complement of range. */
2258 flags |= ANYOF_INVERT;
2262 flags |= ANYOF_FOLD;
2264 flags |= ANYOF_LOCALE;
2265 listsv = newSVpv("# comment\n",0);
2268 if (*PL_regcomp_parse == ']' || *PL_regcomp_parse == '-')
2269 goto skipcond; /* allow 1st char to be ] or - */
2271 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != ']') {
2273 value = utf8_to_uv(PL_regcomp_parse, &numlen);
2274 PL_regcomp_parse += numlen;
2276 if (value == '[' && PL_regcomp_parse + 1 < PL_regxend &&
2277 /* I smell either [: or [= or [. -- POSIX has been here, right? */
2278 (*PL_regcomp_parse == ':' || *PL_regcomp_parse == '=' || *PL_regcomp_parse == '.')) {
2279 char posixccc = *PL_regcomp_parse;
2280 char* posixccs = PL_regcomp_parse++;
2282 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != posixccc)
2284 if (PL_regcomp_parse == PL_regxend)
2285 /* Grandfather lone [:, [=, [. */
2286 PL_regcomp_parse = posixccs;
2288 PL_regcomp_parse++; /* skip over the posixccc */
2289 if (*PL_regcomp_parse == ']') {
2290 /* Not Implemented Yet.
2291 * (POSIX Extended Character Classes, that is)
2292 * The text between e.g. [: and :] would start
2293 * at posixccs + 1 and stop at regcomp_parse - 2. */
2294 if (dowarn && !SIZE_ONLY)
2295 warn("Character class syntax [%c %c] is reserved for future extensions", posixccc, posixccc);
2296 PL_regcomp_parse++; /* skip over the ending ] */
2301 if (value == '\\') {
2302 value = utf8_to_uv(PL_regcomp_parse, &numlen);
2303 PL_regcomp_parse += numlen;
2308 flags |= ANYOF_ALNUML;
2310 sv_catpvf(listsv, "+utf8::IsAlnum\n");
2317 flags |= ANYOF_NALNUML;
2320 "-utf8::IsAlpha\n-utf8::IsDigit\n0000\t%04x\n%04x\tffff\n",
2329 flags |= ANYOF_SPACEL;
2330 sv_catpvf(listsv, "+utf8::IsSpace\n");
2339 flags |= ANYOF_NSPACEL;
2341 "!utf8::IsSpace\n");
2349 sv_catpvf(listsv, "+utf8::IsDigit\n");
2356 "!utf8::IsDigit\n");
2362 if (*PL_regcomp_parse == '{') {
2363 e = strchr(PL_regcomp_parse++, '}');
2365 FAIL("Missing right brace on \\p{}");
2366 n = e - PL_regcomp_parse;
2369 e = PL_regcomp_parse;
2374 sv_catpvf(listsv, "+utf8::%.*s\n", n, PL_regcomp_parse);
2377 "!utf8::%.*s\n", n, PL_regcomp_parse);
2379 PL_regcomp_parse = e + 1;
2404 if (*PL_regcomp_parse == '{') {
2405 e = strchr(PL_regcomp_parse++, '}');
2407 FAIL("Missing right brace on \\x{}");
2408 value = scan_hex(PL_regcomp_parse + 1, e - PL_regcomp_parse, &numlen);
2409 PL_regcomp_parse = e + 1;
2412 value = scan_hex(PL_regcomp_parse, 2, &numlen);
2413 PL_regcomp_parse += numlen;
2417 value = UCHARAT(PL_regcomp_parse++);
2418 value = toCTRL(value);
2420 case '0': case '1': case '2': case '3': case '4':
2421 case '5': case '6': case '7': case '8': case '9':
2422 value = scan_oct(--PL_regcomp_parse, 3, &numlen);
2423 PL_regcomp_parse += numlen;
2428 if (lastvalue > value)
2429 FAIL("invalid [] range in regexp");
2431 sv_catpvf(listsv, "%04x\t%04x\n", lastvalue, value);
2437 if (*PL_regcomp_parse == '-' && PL_regcomp_parse+1 < PL_regxend &&
2438 PL_regcomp_parse[1] != ']') {
2441 continue; /* do it next time */
2444 sv_catpvf(listsv, "%04x\n", value);
2447 if (*PL_regcomp_parse != ']')
2448 FAIL("unmatched [] in regexp");
2451 ret = reganode(ANYOFUTF8, 0);
2454 SV *rv = swash_init("utf8", "", listsv, 1, 0);
2455 SvREFCNT_dec(listsv);
2456 n = add_data(1,"s");
2457 PL_regcomp_rx->data->data[n] = (void*)rv;
2458 ARG1_SET(ret, flags);
2469 char* retval = PL_regcomp_parse++;
2472 if (*PL_regcomp_parse == '(' && PL_regcomp_parse[1] == '?' &&
2473 PL_regcomp_parse[2] == '#') {
2474 while (*PL_regcomp_parse && *PL_regcomp_parse != ')')
2479 if (PL_regflags & PMf_EXTENDED) {
2480 if (isSPACE(*PL_regcomp_parse)) {
2484 else if (*PL_regcomp_parse == '#') {
2485 while (*PL_regcomp_parse && *PL_regcomp_parse != '\n')
2496 - reg_node - emit a node
2498 STATIC regnode * /* Location. */
2502 register regnode *ret;
2503 register regnode *ptr;
2507 SIZE_ALIGN(PL_regsize);
2512 NODE_ALIGN_FILL(ret);
2514 FILL_ADVANCE_NODE(ptr, op);
2521 - reganode - emit a node with an argument
2523 STATIC regnode * /* Location. */
2524 reganode(U8 op, U32 arg)
2527 register regnode *ret;
2528 register regnode *ptr;
2532 SIZE_ALIGN(PL_regsize);
2537 NODE_ALIGN_FILL(ret);
2539 FILL_ADVANCE_NODE_ARG(ptr, op, arg);
2546 - regc - emit (if appropriate) a Unicode character
2549 reguni(UV uv, char* s, I32* lenp)
2554 *lenp = uv_to_utf8(tmpbuf, uv) - tmpbuf;
2557 *lenp = uv_to_utf8(s, uv) - s;
2562 - regc - emit (if appropriate) a byte of code
2573 - reginsert - insert an operator in front of already-emitted operand
2575 * Means relocating the operand.
2578 reginsert(U8 op, regnode *opnd)
2581 register regnode *src;
2582 register regnode *dst;
2583 register regnode *place;
2584 register int offset = regarglen[(U8)op];
2586 /* (regkind[(U8)op] == CURLY ? EXTRA_STEP_2ARGS : 0); */
2589 PL_regsize += NODE_STEP_REGNODE + offset;
2594 PL_regcode += NODE_STEP_REGNODE + offset;
2597 StructCopy(--src, --dst, regnode);
2599 place = opnd; /* Op node, where operand used to be. */
2600 src = NEXTOPER(place);
2601 FILL_ADVANCE_NODE(place, op);
2602 Zero(src, offset, regnode);
2606 - regtail - set the next-pointer at the end of a node chain of p to val.
2609 regtail(regnode *p, regnode *val)
2612 register regnode *scan;
2613 register regnode *temp;
2614 register I32 offset;
2619 /* Find last node. */
2622 temp = regnext(scan);
2628 if (reg_off_by_arg[OP(scan)]) {
2629 ARG_SET(scan, val - scan);
2632 NEXT_OFF(scan) = val - scan;
2637 - regoptail - regtail on operand of first argument; nop if operandless
2640 regoptail(regnode *p, regnode *val)
2643 /* "Operandless" and "op != BRANCH" are synonymous in practice. */
2644 if (p == NULL || SIZE_ONLY)
2646 if (regkind[(U8)OP(p)] == BRANCH) {
2647 regtail(NEXTOPER(p), val);
2649 else if ( regkind[(U8)OP(p)] == BRANCHJ) {
2650 regtail(NEXTOPER(NEXTOPER(p)), val);
2657 - regcurly - a little FSA that accepts {\d+,?\d*}
2660 regcurly(register char *s)
2679 dumpuntil(regnode *start, regnode *node, regnode *last, SV* sv, I32 l)
2682 register char op = EXACT; /* Arbitrary non-END op. */
2683 register regnode *next, *onode;
2685 while (op != END && (!last || node < last)) {
2686 /* While that wasn't END last time... */
2692 next = regnext(node);
2694 if (OP(node) == OPTIMIZED)
2697 PerlIO_printf(Perl_debug_log, "%4d:%*s%s", node - start,
2698 2*l + 1, "", SvPVX(sv));
2699 if (next == NULL) /* Next ptr. */
2700 PerlIO_printf(Perl_debug_log, "(0)");
2702 PerlIO_printf(Perl_debug_log, "(%d)", next - start);
2703 (void)PerlIO_putc(Perl_debug_log, '\n');
2705 if (regkind[(U8)op] == BRANCHJ) {
2706 register regnode *nnode = (OP(next) == LONGJMP
2709 if (last && nnode > last)
2711 node = dumpuntil(start, NEXTOPER(NEXTOPER(node)), nnode, sv, l + 1);
2713 else if (regkind[(U8)op] == BRANCH) {
2714 node = dumpuntil(start, NEXTOPER(node), next, sv, l + 1);
2716 else if ( op == CURLY) { /* `next' might be very big: optimizer */
2717 node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
2718 NEXTOPER(node) + EXTRA_STEP_2ARGS + 1, sv, l + 1);
2720 else if (regkind[(U8)op] == CURLY && op != CURLYX) {
2721 node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
2724 else if ( op == PLUS || op == STAR) {
2725 node = dumpuntil(start, NEXTOPER(node), NEXTOPER(node) + 1, sv, l + 1);
2727 else if (op == ANYOF) {
2728 node = NEXTOPER(node);
2731 else if (regkind[(U8)op] == EXACT) {
2732 /* Literal string, where present. */
2733 node += ((*OPERAND(node)) + 2 + sizeof(regnode) - 1) / sizeof(regnode);
2734 node = NEXTOPER(node);
2737 node = NEXTOPER(node);
2738 node += regarglen[(U8)op];
2740 if (op == CURLYX || op == OPEN)
2742 else if (op == WHILEM)
2745 #endif /* DEBUGGING */
2750 - regdump - dump a regexp onto Perl_debug_log in vaguely comprehensible form
2757 SV *sv = sv_newmortal();
2759 (void)dumpuntil(r->program, r->program + 1, NULL, sv, 0);
2761 /* Header fields of interest. */
2762 if (r->anchored_substr)
2763 PerlIO_printf(Perl_debug_log, "anchored `%s%s%s'%s at %d ",
2765 SvPVX(r->anchored_substr),
2767 SvTAIL(r->anchored_substr) ? "$" : "",
2768 r->anchored_offset);
2769 if (r->float_substr)
2770 PerlIO_printf(Perl_debug_log, "floating `%s%s%s'%s at %d..%u ",
2772 SvPVX(r->float_substr),
2774 SvTAIL(r->float_substr) ? "$" : "",
2775 r->float_min_offset, r->float_max_offset);
2776 if (r->check_substr)
2777 PerlIO_printf(Perl_debug_log,
2778 r->check_substr == r->float_substr
2779 ? "(checking floating" : "(checking anchored");
2780 if (r->reganch & ROPT_NOSCAN)
2781 PerlIO_printf(Perl_debug_log, " noscan");
2782 if (r->reganch & ROPT_CHECK_ALL)
2783 PerlIO_printf(Perl_debug_log, " isall");
2784 if (r->check_substr)
2785 PerlIO_printf(Perl_debug_log, ") ");
2787 if (r->regstclass) {
2788 regprop(sv, r->regstclass);
2789 PerlIO_printf(Perl_debug_log, "stclass `%s' ", SvPVX(sv));
2791 if (r->reganch & ROPT_ANCH) {
2792 PerlIO_printf(Perl_debug_log, "anchored");
2793 if (r->reganch & ROPT_ANCH_BOL)
2794 PerlIO_printf(Perl_debug_log, "(BOL)");
2795 if (r->reganch & ROPT_ANCH_MBOL)
2796 PerlIO_printf(Perl_debug_log, "(MBOL)");
2797 if (r->reganch & ROPT_ANCH_GPOS)
2798 PerlIO_printf(Perl_debug_log, "(GPOS)");
2799 PerlIO_putc(Perl_debug_log, ' ');
2801 if (r->reganch & ROPT_GPOS_SEEN)
2802 PerlIO_printf(Perl_debug_log, "GPOS ");
2803 if (r->reganch & ROPT_SKIP)
2804 PerlIO_printf(Perl_debug_log, "plus ");
2805 if (r->reganch & ROPT_IMPLICIT)
2806 PerlIO_printf(Perl_debug_log, "implicit ");
2807 PerlIO_printf(Perl_debug_log, "minlen %ld ", (long) r->minlen);
2808 if (r->reganch & ROPT_EVAL_SEEN)
2809 PerlIO_printf(Perl_debug_log, "with eval ");
2810 PerlIO_printf(Perl_debug_log, "\n");
2811 #endif /* DEBUGGING */
2815 - regprop - printable representation of opcode
2818 regprop(SV *sv, regnode *o)
2822 register char *p = 0;
2824 sv_setpvn(sv, "", 0);
2869 sv_catpvf(sv, "EXACT <%s%s%s>", PL_colors[0], OPERAND(o) + 1, PL_colors[1]);
2872 sv_catpvf(sv, "EXACTF <%s%s%s>", PL_colors[0], OPERAND(o) + 1, PL_colors[1]);
2875 sv_catpvf(sv, "EXACTFL <%s%s%s>", PL_colors[0], OPERAND(o) + 1, PL_colors[1]);
2902 sv_catpvf(sv, "CURLY {%d,%d}", ARG1(o), ARG2(o));
2905 sv_catpvf(sv, "CURLYM[%d] {%d,%d}", o->flags, ARG1(o), ARG2(o));
2908 sv_catpvf(sv, "CURLYN[%d] {%d,%d}", o->flags, ARG1(o), ARG2(o));
2911 sv_catpvf(sv, "CURLYX {%d,%d}", ARG1(o), ARG2(o));
2914 sv_catpvf(sv, "REF%d", ARG(o));
2917 sv_catpvf(sv, "REFF%d", ARG(o));
2920 sv_catpvf(sv, "REFFL%d", ARG(o));
2923 sv_catpvf(sv, "OPEN%d", ARG(o));
2926 sv_catpvf(sv, "CLOSE%d", ARG(o));
2942 sv_catpvf(sv, "UNLESSM[-%d]", o->flags);
2945 sv_catpvf(sv, "IFMATCH[-%d]", o->flags);
2996 sv_catpvf(sv, "GROUPP%d", ARG(o));
3011 FAIL("corrupted regexp opcode");
3015 #endif /* DEBUGGING */
3019 pregfree(struct regexp *r)
3022 if (!r || (--r->refcnt > 0))
3025 Safefree(r->precomp);
3027 Safefree(r->subbase);
3029 if (r->anchored_substr)
3030 SvREFCNT_dec(r->anchored_substr);
3031 if (r->float_substr)
3032 SvREFCNT_dec(r->float_substr);
3033 Safefree(r->substrs);
3036 int n = r->data->count;
3038 switch (r->data->what[n]) {
3040 SvREFCNT_dec((SV*)r->data->data[n]);
3043 op_free((OP_4tree*)r->data->data[n]);
3048 FAIL2("panic: regfree data code '%c'", r->data->what[n]);
3051 Safefree(r->data->what);
3054 Safefree(r->startp);
3060 - regnext - dig the "next" pointer out of a node
3062 * [Note, when REGALIGN is defined there are two places in regmatch()
3063 * that bypass this code for speed.]
3066 regnext(register regnode *p)
3069 register I32 offset;
3071 if (p == &PL_regdummy)
3074 offset = (reg_off_by_arg[OP(p)] ? ARG(p) : NEXT_OFF(p));
3082 re_croak2(const char* pat1,const char* pat2,...)
3085 STRLEN l1 = strlen(pat1);
3086 STRLEN l2 = strlen(pat2);
3094 Copy(pat1, buf, l1 , char);
3095 Copy(pat2, buf + l1, l2 , char);
3096 buf[l1 + l2] = '\n';
3097 buf[l1 + l2 + 1] = '\0';
3098 va_start(args, pat2);
3099 message = mess(buf, &args);
3101 l1 = strlen(message);
3104 Copy(message, buf, l1 , char);
3105 buf[l1] = '\0'; /* Overwrite \n */
3109 /* XXX Here's a total kludge. But we need to re-enter for swash routines. */
3112 save_re_context(void)
3116 SAVEPPTR(PL_regprecomp); /* uncompiled string. */
3117 SAVEI32(PL_regnpar); /* () count. */
3118 SAVEI32(PL_regsize); /* Code size. */
3119 SAVEI16(PL_regflags); /* are we folding, multilining? */
3120 SAVEPPTR(PL_reginput); /* String-input pointer. */
3121 SAVEPPTR(PL_regbol); /* Beginning of input, for ^ check. */
3122 SAVEPPTR(PL_regeol); /* End of input, for $ check. */
3123 SAVESPTR(PL_regstartp); /* Pointer to startp array. */
3124 SAVESPTR(PL_regendp); /* Ditto for endp. */
3125 SAVESPTR(PL_reglastparen); /* Similarly for lastparen. */
3126 SAVEPPTR(PL_regtill); /* How far we are required to go. */
3127 SAVEI32(PL_regprev); /* char before regbol, \n if none */
3128 SAVESPTR(PL_reg_start_tmp); /* from regexec.c */
3129 PL_reg_start_tmp = 0;
3130 SAVEFREEPV(PL_reg_start_tmp);
3131 SAVEI32(PL_reg_start_tmpl); /* from regexec.c */
3132 PL_reg_start_tmpl = 0;
3133 SAVESPTR(PL_regdata);
3134 SAVEI32(PL_reg_flags); /* from regexec.c */
3135 SAVEI32(PL_reg_eval_set); /* from regexec.c */
3136 SAVEI32(PL_regnarrate); /* from regexec.c */
3137 SAVESPTR(PL_regprogram); /* from regexec.c */
3138 SAVEINT(PL_regindent); /* from regexec.c */
3139 SAVESPTR(PL_regcc); /* from regexec.c */
3140 SAVESPTR(PL_curcop);
3141 SAVESPTR(PL_regcomp_rx); /* from regcomp.c */
3142 SAVEI32(PL_regseen); /* from regcomp.c */
3143 SAVEI32(PL_regsawback); /* Did we see \1, ...? */
3144 SAVEI32(PL_regnaughty); /* How bad is this pattern? */
3145 SAVESPTR(PL_regcode); /* Code-emit pointer; ®dummy = don't */
3146 SAVEPPTR(PL_regxend); /* End of input for compile */
3147 SAVEPPTR(PL_regcomp_parse); /* Input-scan pointer. */