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)] ? I32_MAX : U16_MAX);
323 int off = (reg_off_by_arg[OP(scan)] ? ARG(scan) : NEXT_OFF(scan));
327 /* Skip NOTHING and LONGJMP. */
328 while ((n = regnext(n))
329 && ((regkind[(U8)OP(n)] == NOTHING && (noff = NEXT_OFF(n)))
330 || ((OP(n) == LONGJMP) && (noff = ARG(n))))
333 if (reg_off_by_arg[OP(scan)])
336 NEXT_OFF(scan) = off;
338 if (OP(scan) == BRANCH || OP(scan) == BRANCHJ
339 || OP(scan) == IFTHEN || OP(scan) == SUSPEND) {
340 next = regnext(scan);
343 if (OP(next) == code || code == IFTHEN || code == SUSPEND) {
344 I32 max1 = 0, min1 = I32_MAX, num = 0;
346 if (flags & SCF_DO_SUBSTR)
348 while (OP(scan) == code) {
349 I32 deltanext, minnext;
353 next = regnext(scan);
354 scan = NEXTOPER(scan);
356 scan = NEXTOPER(scan);
357 /* We suppose the run is continuous, last=next...*/
358 minnext = study_chunk(&scan, &deltanext, next,
362 if (max1 < minnext + deltanext)
363 max1 = minnext + deltanext;
364 if (deltanext == I32_MAX)
367 if (data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
369 if (data && (data_fake.flags & SF_HAS_EVAL))
370 data->flags |= SF_HAS_EVAL;
374 if (code == IFTHEN && num < 2) /* Empty ELSE branch */
376 if (flags & SCF_DO_SUBSTR) {
377 data->pos_min += min1;
378 data->pos_delta += max1 - min1;
379 if (max1 != min1 || is_inf)
380 data->longest = &(data->longest_float);
383 delta += max1 - min1;
385 else if (code == BRANCHJ) /* single branch is optimized. */
386 scan = NEXTOPER(NEXTOPER(scan));
387 else /* single branch is optimized. */
388 scan = NEXTOPER(scan);
391 else if (OP(scan) == EXACT) {
392 I32 l = *OPERAND(scan);
394 unsigned char *s = (unsigned char *)(OPERAND(scan)+1);
395 unsigned char *e = s + l;
404 if (flags & SCF_DO_SUBSTR) { /* Update longest substr. */
405 /* The code below prefers earlier match for fixed
406 offset, later match for variable offset. */
407 if (data->last_end == -1) { /* Update the start info. */
408 data->last_start_min = data->pos_min;
409 data->last_start_max = is_inf
410 ? I32_MAX : data->pos_min + data->pos_delta;
412 sv_catpvn(data->last_found, (char *)(OPERAND(scan)+1), *OPERAND(scan));
413 data->last_end = data->pos_min + l;
414 data->pos_min += l; /* As in the first entry. */
415 data->flags &= ~SF_BEFORE_EOL;
418 else if (regkind[(U8)OP(scan)] == EXACT) {
419 I32 l = *OPERAND(scan);
420 if (flags & SCF_DO_SUBSTR)
423 unsigned char *s = (unsigned char *)(OPERAND(scan)+1);
424 unsigned char *e = s + l;
433 if (data && (flags & SCF_DO_SUBSTR))
436 else if (strchr(varies,OP(scan))) {
437 I32 mincount, maxcount, minnext, deltanext, pos_before, fl;
438 regnode *oscan = scan;
440 switch (regkind[(U8)OP(scan)]) {
442 scan = NEXTOPER(scan);
445 if (flags & SCF_DO_SUBSTR) {
446 next = NEXTOPER(scan);
447 if (OP(next) == EXACT) {
449 maxcount = REG_INFTY;
450 next = regnext(scan);
451 scan = NEXTOPER(scan);
455 if (flags & SCF_DO_SUBSTR)
461 scan = regnext(scan);
462 if (flags & SCF_DO_SUBSTR) {
464 data->longest = &(data->longest_float);
466 goto optimize_curly_tail;
468 mincount = ARG1(scan);
469 maxcount = ARG2(scan);
470 next = regnext(scan);
471 scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
473 if (flags & SCF_DO_SUBSTR) {
474 if (mincount == 0) scan_commit(data);
475 pos_before = data->pos_min;
479 data->flags &= ~(SF_HAS_PAR|SF_IN_PAR|SF_HAS_EVAL);
481 data->flags |= SF_IS_INF;
483 /* This will finish on WHILEM, setting scan, or on NULL: */
484 minnext = study_chunk(&scan, &deltanext, last, data,
486 ? (flags & ~SCF_DO_SUBSTR) : flags);
487 if (!scan) /* It was not CURLYX, but CURLY. */
489 if (PL_dowarn && (minnext + deltanext == 0)
490 && !(data->flags & (SF_HAS_PAR|SF_IN_PAR))
491 && maxcount <= 10000) /* Complement check for big count */
492 warn("Strange *+?{} on zero-length expression");
493 min += minnext * mincount;
494 is_inf |= (maxcount == REG_INFTY && (minnext + deltanext) > 0
495 || deltanext == I32_MAX);
496 delta += (minnext + deltanext) * maxcount - minnext * mincount;
498 /* Try powerful optimization CURLYX => CURLYN. */
499 if ( OP(oscan) == CURLYX && data
500 && data->flags & SF_IN_PAR
501 && !(data->flags & SF_HAS_EVAL)
502 && !deltanext && minnext == 1 ) {
503 /* Try to optimize to CURLYN. */
504 regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS;
505 regnode *nxt1 = nxt, *nxt2;
509 if (!strchr(simple,OP(nxt))
510 && !(regkind[(U8)OP(nxt)] == EXACT
511 && *OPERAND(nxt) == 1))
515 if (OP(nxt) != CLOSE)
517 /* Now we know that nxt2 is the only contents: */
518 oscan->flags = ARG(nxt);
520 OP(nxt1) = NOTHING; /* was OPEN. */
522 OP(nxt1 + 1) = OPTIMIZED; /* was count. */
523 NEXT_OFF(nxt1+ 1) = 0; /* just for consistancy. */
524 NEXT_OFF(nxt2) = 0; /* just for consistancy with CURLY. */
525 OP(nxt) = OPTIMIZED; /* was CLOSE. */
526 OP(nxt + 1) = OPTIMIZED; /* was count. */
527 NEXT_OFF(nxt+ 1) = 0; /* just for consistancy. */
532 /* Try optimization CURLYX => CURLYM. */
533 if ( OP(oscan) == CURLYX && data
534 && !(data->flags & SF_HAS_PAR)
535 && !(data->flags & SF_HAS_EVAL)
537 /* XXXX How to optimize if data == 0? */
538 /* Optimize to a simpler form. */
539 regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN */
543 while ( (nxt2 = regnext(nxt)) /* skip over embedded stuff*/
544 && (OP(nxt2) != WHILEM))
546 OP(nxt2) = SUCCEED; /* Whas WHILEM */
547 /* Need to optimize away parenths. */
548 if (data->flags & SF_IN_PAR) {
549 /* Set the parenth number. */
550 regnode *nxt1 = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN*/
552 if (OP(nxt) != CLOSE)
553 FAIL("panic opt close");
554 oscan->flags = ARG(nxt);
555 OP(nxt1) = OPTIMIZED; /* was OPEN. */
556 OP(nxt) = OPTIMIZED; /* was CLOSE. */
558 OP(nxt1 + 1) = OPTIMIZED; /* was count. */
559 OP(nxt + 1) = OPTIMIZED; /* was count. */
560 NEXT_OFF(nxt1 + 1) = 0; /* just for consistancy. */
561 NEXT_OFF(nxt + 1) = 0; /* just for consistancy. */
564 while ( nxt1 && (OP(nxt1) != WHILEM)) {
565 regnode *nnxt = regnext(nxt1);
568 if (reg_off_by_arg[OP(nxt1)])
569 ARG_SET(nxt1, nxt2 - nxt1);
570 else if (nxt2 - nxt1 < U16_MAX)
571 NEXT_OFF(nxt1) = nxt2 - nxt1;
573 OP(nxt) = NOTHING; /* Cannot beautify */
578 /* Optimize again: */
579 study_chunk(&nxt1, &deltanext, nxt, NULL, 0);
584 if (data && fl & (SF_HAS_PAR|SF_IN_PAR))
586 if (flags & SCF_DO_SUBSTR) {
587 SV *last_str = Nullsv;
588 int counted = mincount != 0;
590 if (data->last_end > 0 && mincount != 0) { /* Ends with a string. */
591 I32 b = pos_before >= data->last_start_min
592 ? pos_before : data->last_start_min;
594 char *s = SvPV(data->last_found, l);
595 I32 old = b - data->last_start_min;
598 old = utf8_hop((U8*)s, old) - (U8*)s;
601 /* Get the added string: */
602 last_str = newSVpv(s + old, l);
603 if (deltanext == 0 && pos_before == b) {
604 /* What was added is a constant string */
606 SvGROW(last_str, (mincount * l) + 1);
607 repeatcpy(SvPVX(last_str) + l,
608 SvPVX(last_str), l, mincount - 1);
609 SvCUR(last_str) *= mincount;
610 /* Add additional parts. */
611 SvCUR_set(data->last_found,
612 SvCUR(data->last_found) - l);
613 sv_catsv(data->last_found, last_str);
614 data->last_end += l * (mincount - 1);
618 /* It is counted once already... */
619 data->pos_min += minnext * (mincount - counted);
620 data->pos_delta += - counted * deltanext +
621 (minnext + deltanext) * maxcount - minnext * mincount;
622 if (mincount != maxcount) {
624 if (mincount && last_str) {
625 sv_setsv(data->last_found, last_str);
626 data->last_end = data->pos_min;
627 data->last_start_min =
628 data->pos_min - CHR_SVLEN(last_str);
629 data->last_start_max = is_inf
631 : data->pos_min + data->pos_delta
632 - CHR_SVLEN(last_str);
634 data->longest = &(data->longest_float);
637 if (data && (fl & SF_HAS_EVAL))
638 data->flags |= SF_HAS_EVAL;
640 if (OP(oscan) != CURLYX) {
641 while (regkind[(U8)OP(next = regnext(oscan))] == NOTHING
643 NEXT_OFF(oscan) += NEXT_OFF(next);
646 default: /* REF only? */
647 if (flags & SCF_DO_SUBSTR) {
649 data->longest = &(data->longest_float);
655 else if (strchr(simple,OP(scan)) || regkind[(U8)OP(scan)] == ANYUTF8) {
656 if (flags & SCF_DO_SUBSTR) {
662 else if (regkind[(U8)OP(scan)] == EOL && flags & SCF_DO_SUBSTR) {
663 data->flags |= (OP(scan) == MEOL
667 else if (regkind[(U8)OP(scan)] == BRANCHJ
668 && (scan->flags || data)
669 && (OP(scan) == IFMATCH || OP(scan) == UNLESSM)) {
670 I32 deltanext, minnext;
674 next = regnext(scan);
675 nscan = NEXTOPER(NEXTOPER(scan));
676 minnext = study_chunk(&nscan, &deltanext, last, &data_fake, 0);
679 FAIL("variable length lookbehind not implemented");
681 else if (minnext > U8_MAX) {
682 FAIL2("lookbehind longer than %d not implemented", U8_MAX);
684 scan->flags = minnext;
686 if (data && data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
688 if (data && (data_fake.flags & SF_HAS_EVAL))
689 data->flags |= SF_HAS_EVAL;
691 else if (OP(scan) == OPEN) {
694 else if (OP(scan) == CLOSE && ARG(scan) == is_par) {
695 next = regnext(scan);
697 if ( next && (OP(next) != WHILEM) && next < last)
698 is_par = 0; /* Disable optimization */
700 else if (OP(scan) == EVAL) {
702 data->flags |= SF_HAS_EVAL;
704 /* Else: zero-length, ignore. */
705 scan = regnext(scan);
710 *deltap = is_inf ? I32_MAX : delta;
711 if (flags & SCF_DO_SUBSTR && is_inf)
712 data->pos_delta = I32_MAX - data->pos_min;
715 if (is_par && pars==1 && data) {
716 data->flags |= SF_IN_PAR;
717 data->flags &= ~SF_HAS_PAR;
719 else if (pars && data) {
720 data->flags |= SF_HAS_PAR;
721 data->flags &= ~SF_IN_PAR;
727 add_data(I32 n, char *s)
730 if (PL_regcomp_rx->data) {
731 Renewc(PL_regcomp_rx->data,
732 sizeof(*PL_regcomp_rx->data) + sizeof(void*) * (PL_regcomp_rx->data->count + n - 1),
733 char, struct reg_data);
734 Renew(PL_regcomp_rx->data->what, PL_regcomp_rx->data->count + n, U8);
735 PL_regcomp_rx->data->count += n;
738 Newc(1207, PL_regcomp_rx->data, sizeof(*PL_regcomp_rx->data) + sizeof(void*) * (n - 1),
739 char, struct reg_data);
740 New(1208, PL_regcomp_rx->data->what, n, U8);
741 PL_regcomp_rx->data->count = n;
743 Copy(s, PL_regcomp_rx->data->what + PL_regcomp_rx->data->count - n, n, U8);
744 return PL_regcomp_rx->data->count - n;
748 - pregcomp - compile a regular expression into internal code
750 * We can't allocate space until we know how big the compiled form will be,
751 * but we can't compile it (and thus know how big it is) until we've got a
752 * place to put the code. So we cheat: we compile it twice, once with code
753 * generation turned off and size counting turned on, and once "for real".
754 * This also means that we don't allocate space until we are sure that the
755 * thing really will compile successfully, and we never have to move the
756 * code and thus invalidate pointers into it. (Note that it has to be in
757 * one piece because free() must be able to free it all.) [NB: not true in perl]
759 * Beware that the optimization-preparation code in here knows about some
760 * of the structure of the compiled regexp. [I'll say.]
763 pregcomp(char *exp, char *xend, PMOP *pm)
778 FAIL("NULL regexp argument");
780 if (PL_curcop == &compiling ? (PL_hints & HINT_UTF8) : IN_UTF8)
781 PL_reg_flags |= RF_utf8;
785 PL_regprecomp = savepvn(exp, xend - exp);
786 DEBUG_r(PerlIO_printf(Perl_debug_log, "compiling RE `%*s'\n",
787 xend - exp, PL_regprecomp));
788 PL_regflags = pm->op_pmflags;
792 PL_seen_zerolen = *exp == '^' ? -1 : 0;
796 /* First pass: determine size, legality. */
797 PL_regcomp_parse = exp;
802 PL_regcode = &PL_regdummy;
803 regc((U8)MAGIC, (char*)PL_regcode);
804 if (reg(0, &flags) == NULL) {
805 Safefree(PL_regprecomp);
806 PL_regprecomp = Nullch;
809 DEBUG_r(PerlIO_printf(Perl_debug_log, "size %d ", PL_regsize));
814 char *s = PerlEnv_getenv("TERMCAP_COLORS");
818 PL_colors[0] = s = savepv(s);
822 FAIL("Not enough TABs in TERMCAP_COLORS");
832 PerlIO_printf(Perl_debug_log, "%s%s%s%s",
833 PL_colors[0],PL_colors[1],PL_colors[2],PL_colors[3]);
837 /* Small enough for pointer-storage convention?
838 If extralen==0, this means that we will not need long jumps. */
839 if (PL_regsize >= 0x10000L && PL_extralen)
840 PL_regsize += PL_extralen;
844 /* Allocate space and initialize. */
845 Newc(1001, r, sizeof(regexp) + (unsigned)PL_regsize * sizeof(regnode),
848 FAIL("regexp out of space");
850 r->prelen = xend - exp;
851 r->precomp = PL_regprecomp;
852 r->subbeg = r->subbase = NULL;
853 r->nparens = PL_regnpar - 1; /* set early to validate backrefs */
856 /* Second pass: emit code. */
857 PL_regcomp_parse = exp;
861 PL_regcode = r->program;
862 /* Store the count of eval-groups for security checks: */
863 PL_regcode->next_off = ((PL_seen_evals > U16_MAX) ? U16_MAX : PL_seen_evals);
864 regc((U8)MAGIC, (char*) PL_regcode++);
866 if (reg(0, &flags) == NULL)
869 /* Dig out information for optimizations. */
870 r->reganch = pm->op_pmflags & PMf_COMPILETIME;
871 pm->op_pmflags = PL_regflags;
873 r->reganch |= ROPT_UTF8;
874 r->regstclass = NULL;
875 if (PL_regnaughty >= 10) /* Probably an expensive pattern. */
876 r->reganch |= ROPT_NAUGHTY;
877 scan = r->program + 1; /* First BRANCH. */
879 /* XXXX To minimize changes to RE engine we always allocate
880 3-units-long substrs field. */
881 Newz(1004, r->substrs, 1, struct reg_substr_data);
883 if (OP(scan) != BRANCH) { /* Only one top-level choice. */
886 STRLEN longest_float_length, longest_fixed_length;
888 StructCopy(&zero_scan_data, &data, scan_data_t);
890 /* Skip introductions and multiplicators >= 1. */
891 while ((OP(first) == OPEN && (sawopen = 1)) ||
892 (OP(first) == BRANCH && OP(regnext(first)) != BRANCH) ||
893 (OP(first) == PLUS) ||
894 (OP(first) == MINMOD) ||
895 (regkind[(U8)OP(first)] == CURLY && ARG1(first) > 0) ) {
896 if (OP(first) == PLUS)
899 first += regarglen[(U8)OP(first)];
900 first = NEXTOPER(first);
903 /* Starting-point info. */
905 if (OP(first) == EXACT); /* Empty, get anchored substr later. */
906 else if (strchr(simple+4,OP(first)))
907 r->regstclass = first;
908 else if (regkind[(U8)OP(first)] == BOUND ||
909 regkind[(U8)OP(first)] == NBOUND)
910 r->regstclass = first;
911 else if (regkind[(U8)OP(first)] == BOL) {
912 r->reganch |= (OP(first) == MBOL ? ROPT_ANCH_MBOL: ROPT_ANCH_BOL);
913 first = NEXTOPER(first);
916 else if (OP(first) == GPOS) {
917 r->reganch |= ROPT_ANCH_GPOS;
918 first = NEXTOPER(first);
921 else if ((OP(first) == STAR &&
922 regkind[(U8)OP(NEXTOPER(first))] == ANY) &&
923 !(r->reganch & ROPT_ANCH) )
925 /* turn .* into ^.* with an implied $*=1 */
926 r->reganch |= ROPT_ANCH_BOL | ROPT_IMPLICIT;
927 first = NEXTOPER(first);
930 if (sawplus && (!sawopen || !PL_regsawback))
931 r->reganch |= ROPT_SKIP; /* x+ must match 1st of run */
933 /* Scan is after the zeroth branch, first is atomic matcher. */
934 DEBUG_r(PerlIO_printf(Perl_debug_log, "first at %d\n",
937 * If there's something expensive in the r.e., find the
938 * longest literal string that must appear and make it the
939 * regmust. Resolve ties in favor of later strings, since
940 * the regstart check works with the beginning of the r.e.
941 * and avoiding duplication strengthens checking. Not a
942 * strong reason, but sufficient in the absence of others.
943 * [Now we resolve ties in favor of the earlier string if
944 * it happens that c_offset_min has been invalidated, since the
945 * earlier string may buy us something the later one won't.]
949 data.longest_fixed = newSVpv("",0);
950 data.longest_float = newSVpv("",0);
951 data.last_found = newSVpv("",0);
952 data.longest = &(data.longest_fixed);
955 minlen = study_chunk(&first, &fake, scan + PL_regsize, /* Up to end */
956 &data, SCF_DO_SUBSTR);
957 if ( PL_regnpar == 1 && data.longest == &(data.longest_fixed)
958 && data.last_start_min == 0 && data.last_end > 0
960 && (!(PL_regseen & REG_SEEN_GPOS) || (r->reganch & ROPT_ANCH_GPOS)))
961 r->reganch |= ROPT_CHECK_ALL;
963 SvREFCNT_dec(data.last_found);
965 longest_float_length = CHR_SVLEN(data.longest_float);
966 if (longest_float_length
967 || (data.flags & SF_FL_BEFORE_EOL
968 && (!(data.flags & SF_FL_BEFORE_MEOL)
969 || (PL_regflags & PMf_MULTILINE)))) {
970 if (SvCUR(data.longest_fixed) /* ok to leave SvCUR */
971 && data.offset_fixed == data.offset_float_min)
972 goto remove; /* Like in (a)+. */
974 r->float_substr = data.longest_float;
975 r->float_min_offset = data.offset_float_min;
976 r->float_max_offset = data.offset_float_max;
977 fbm_compile(r->float_substr, 0);
978 BmUSEFUL(r->float_substr) = 100;
979 if (data.flags & SF_FL_BEFORE_EOL /* Cannot have SEOL and MULTI */
980 && (!(data.flags & SF_FL_BEFORE_MEOL)
981 || (PL_regflags & PMf_MULTILINE)))
982 SvTAIL_on(r->float_substr);
986 r->float_substr = Nullsv;
987 SvREFCNT_dec(data.longest_float);
988 longest_float_length = 0;
991 longest_fixed_length = CHR_SVLEN(data.longest_fixed);
992 if (longest_fixed_length
993 || (data.flags & SF_FIX_BEFORE_EOL /* Cannot have SEOL and MULTI */
994 && (!(data.flags & SF_FIX_BEFORE_MEOL)
995 || (PL_regflags & PMf_MULTILINE)))) {
996 r->anchored_substr = data.longest_fixed;
997 r->anchored_offset = data.offset_fixed;
998 fbm_compile(r->anchored_substr, 0);
999 BmUSEFUL(r->anchored_substr) = 100;
1000 if (data.flags & SF_FIX_BEFORE_EOL /* Cannot have SEOL and MULTI */
1001 && (!(data.flags & SF_FIX_BEFORE_MEOL)
1002 || (PL_regflags & PMf_MULTILINE)))
1003 SvTAIL_on(r->anchored_substr);
1006 r->anchored_substr = Nullsv;
1007 SvREFCNT_dec(data.longest_fixed);
1008 longest_fixed_length = 0;
1011 /* A temporary algorithm prefers floated substr to fixed one to dig more info. */
1012 if (longest_fixed_length > longest_float_length) {
1013 r->check_substr = r->anchored_substr;
1014 r->check_offset_min = r->check_offset_max = r->anchored_offset;
1015 if (r->reganch & ROPT_ANCH_SINGLE)
1016 r->reganch |= ROPT_NOSCAN;
1019 r->check_substr = r->float_substr;
1020 r->check_offset_min = data.offset_float_min;
1021 r->check_offset_max = data.offset_float_max;
1025 /* Several toplevels. Best we can is to set minlen. */
1028 DEBUG_r(PerlIO_printf(Perl_debug_log, "\n"));
1029 scan = r->program + 1;
1030 minlen = study_chunk(&scan, &fake, scan + PL_regsize, NULL, 0);
1031 r->check_substr = r->anchored_substr = r->float_substr = Nullsv;
1035 if (PL_regseen & REG_SEEN_GPOS)
1036 r->reganch |= ROPT_GPOS_SEEN;
1037 if (PL_regseen & REG_SEEN_LOOKBEHIND)
1038 r->reganch |= ROPT_LOOKBEHIND_SEEN;
1039 if (PL_regseen & REG_SEEN_EVAL)
1040 r->reganch |= ROPT_EVAL_SEEN;
1041 Newz(1002, r->startp, PL_regnpar, char*);
1042 Newz(1002, r->endp, PL_regnpar, char*);
1043 DEBUG_r(regdump(r));
1048 - reg - regular expression, i.e. main body or parenthesized thing
1050 * Caller must absorb opening parenthesis.
1052 * Combining parenthesis handling with the base level of regular expression
1053 * is a trifle forced, but the need to tie the tails of the branches to what
1054 * follows makes it hard to avoid.
1057 reg(I32 paren, I32 *flagp)
1058 /* paren: Parenthesized? 0=top, 1=(, inside: changed to letter. */
1061 register regnode *ret; /* Will be the head of the group. */
1062 register regnode *br;
1063 register regnode *lastbr;
1064 register regnode *ender = 0;
1065 register I32 parno = 0;
1066 I32 flags, oregflags = PL_regflags, have_branch = 0, open = 0;
1069 *flagp = 0; /* Tentatively. */
1071 /* Make an OPEN node, if parenthesized. */
1073 if (*PL_regcomp_parse == '?') {
1074 U16 posflags = 0, negflags = 0;
1075 U16 *flagsp = &posflags;
1078 paren = *PL_regcomp_parse++;
1079 ret = NULL; /* For look-ahead/behind. */
1082 PL_regseen |= REG_SEEN_LOOKBEHIND;
1083 if (*PL_regcomp_parse == '!')
1085 if (*PL_regcomp_parse != '=' && *PL_regcomp_parse != '!')
1096 FAIL2("Sequence (?%c...) not implemented", (int)paren);
1099 while (*PL_regcomp_parse && *PL_regcomp_parse != ')')
1101 if (*PL_regcomp_parse != ')')
1102 FAIL("Sequence (?#... not terminated");
1109 I32 count = 1, n = 0;
1111 char *s = PL_regcomp_parse;
1113 OP_4tree *sop, *rop;
1116 PL_regseen |= REG_SEEN_EVAL;
1117 while (count && (c = *PL_regcomp_parse)) {
1118 if (c == '\\' && PL_regcomp_parse[1])
1126 if (*PL_regcomp_parse != ')')
1127 FAIL("Sequence (?{...}) not terminated or not {}-balanced");
1131 if (PL_regcomp_parse - 1 - s)
1132 sv = newSVpv(s, PL_regcomp_parse - 1 - s);
1134 sv = newSVpv("", 0);
1136 rop = sv_compile_2op(sv, &sop, "re", &av);
1138 n = add_data(3, "nso");
1139 PL_regcomp_rx->data->data[n] = (void*)rop;
1140 PL_regcomp_rx->data->data[n+1] = (void*)av;
1141 PL_regcomp_rx->data->data[n+2] = (void*)sop;
1144 else { /* First pass */
1145 if (PL_reginterp_cnt < ++PL_seen_evals && PL_curcop != &compiling)
1146 /* No compiled RE interpolated, has runtime
1147 components ===> unsafe. */
1148 FAIL("Eval-group not allowed at runtime, use re 'eval'");
1150 FAIL("Eval-group in insecure regular expression");
1154 return reganode(EVAL, n);
1158 if (PL_regcomp_parse[0] == '?') {
1159 if (PL_regcomp_parse[1] == '=' || PL_regcomp_parse[1] == '!'
1160 || PL_regcomp_parse[1] == '<'
1161 || PL_regcomp_parse[1] == '{') { /* Lookahead or eval. */
1164 ret = reg_node(LOGICAL);
1165 regtail(ret, reg(1, &flag));
1169 else if (PL_regcomp_parse[0] >= '1' && PL_regcomp_parse[0] <= '9' ) {
1170 parno = atoi(PL_regcomp_parse++);
1172 while (isDIGIT(*PL_regcomp_parse))
1174 ret = reganode(GROUPP, parno);
1175 if ((c = *nextchar()) != ')')
1176 FAIL2("Switch (?(number%c not recognized", c);
1178 regtail(ret, reganode(IFTHEN, 0));
1179 br = regbranch(&flags, 1);
1181 br = reganode(LONGJMP, 0);
1183 regtail(br, reganode(LONGJMP, 0));
1186 lastbr = reganode(IFTHEN, 0); /* Fake one for optimizer. */
1187 regbranch(&flags, 1);
1188 regtail(ret, lastbr);
1194 FAIL("Switch (?(condition)... contains too many branches");
1195 ender = reg_node(TAIL);
1198 regtail(lastbr, ender);
1199 regtail(NEXTOPER(NEXTOPER(lastbr)), ender);
1202 regtail(ret, ender);
1206 FAIL2("Unknown condition for (?(%.2s", PL_regcomp_parse);
1210 FAIL("Sequence (? incomplete");
1215 while (*PL_regcomp_parse && strchr("iogcmsx", *PL_regcomp_parse)) {
1216 if (*PL_regcomp_parse != 'o')
1217 pmflag(flagsp, *PL_regcomp_parse);
1220 if (*PL_regcomp_parse == '-') {
1225 PL_regflags |= posflags;
1226 PL_regflags &= ~negflags;
1227 if (*PL_regcomp_parse == ':') {
1233 if (*PL_regcomp_parse != ')')
1234 FAIL2("Sequence (?%c...) not recognized", *PL_regcomp_parse);
1243 ret = reganode(OPEN, parno);
1250 /* Pick up the branches, linking them together. */
1251 br = regbranch(&flags, 1);
1254 if (*PL_regcomp_parse == '|') {
1255 if (!SIZE_ONLY && PL_extralen) {
1256 reginsert(BRANCHJ, br);
1259 reginsert(BRANCH, br);
1262 PL_extralen += 1; /* For BRANCHJ-BRANCH. */
1264 else if (paren == ':') {
1265 *flagp |= flags&SIMPLE;
1267 if (open) { /* Starts with OPEN. */
1268 regtail(ret, br); /* OPEN -> first. */
1270 else if (paren != '?') /* Not Conditional */
1274 *flagp |= flags&SPSTART;
1276 while (*PL_regcomp_parse == '|') {
1277 if (!SIZE_ONLY && PL_extralen) {
1278 ender = reganode(LONGJMP,0);
1279 regtail(NEXTOPER(NEXTOPER(lastbr)), ender); /* Append to the previous. */
1282 PL_extralen += 2; /* Account for LONGJMP. */
1284 br = regbranch(&flags, 0);
1287 regtail(lastbr, br); /* BRANCH -> BRANCH. */
1291 *flagp |= flags&SPSTART;
1294 if (have_branch || paren != ':') {
1295 /* Make a closing node, and hook it on the end. */
1298 ender = reg_node(TAIL);
1301 ender = reganode(CLOSE, parno);
1307 *flagp &= ~HASWIDTH;
1310 ender = reg_node(SUCCEED);
1313 ender = reg_node(END);
1316 regtail(lastbr, ender);
1319 /* Hook the tails of the branches to the closing node. */
1320 for (br = ret; br != NULL; br = regnext(br)) {
1321 regoptail(br, ender);
1328 static char parens[] = "=!<,>";
1330 if (paren && (p = strchr(parens, paren))) {
1331 int node = ((p - parens) % 2) ? UNLESSM : IFMATCH;
1332 int flag = (p - parens) > 1;
1335 node = SUSPEND, flag = 0;
1336 reginsert(node,ret);
1338 regtail(ret, reg_node(TAIL));
1342 /* Check for proper termination. */
1343 if (paren && (PL_regcomp_parse >= PL_regxend || *nextchar() != ')')) {
1344 FAIL("unmatched () in regexp");
1346 else if (!paren && PL_regcomp_parse < PL_regxend) {
1347 if (*PL_regcomp_parse == ')') {
1348 FAIL("unmatched () in regexp");
1351 FAIL("junk on end of regexp"); /* "Can't happen". */
1355 PL_regflags = oregflags;
1362 - regbranch - one alternative of an | operator
1364 * Implements the concatenation operator.
1367 regbranch(I32 *flagp, I32 first)
1370 register regnode *ret;
1371 register regnode *chain = NULL;
1372 register regnode *latest;
1373 I32 flags = 0, c = 0;
1378 if (!SIZE_ONLY && PL_extralen)
1379 ret = reganode(BRANCHJ,0);
1381 ret = reg_node(BRANCH);
1384 if (!first && SIZE_ONLY)
1385 PL_extralen += 1; /* BRANCHJ */
1387 *flagp = WORST; /* Tentatively. */
1391 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != '|' && *PL_regcomp_parse != ')') {
1393 latest = regpiece(&flags);
1394 if (latest == NULL) {
1395 if (flags & TRYAGAIN)
1399 else if (ret == NULL)
1401 *flagp |= flags&HASWIDTH;
1402 if (chain == NULL) /* First piece. */
1403 *flagp |= flags&SPSTART;
1406 regtail(chain, latest);
1411 if (chain == NULL) { /* Loop ran zero times. */
1412 chain = reg_node(NOTHING);
1417 *flagp |= flags&SIMPLE;
1424 - regpiece - something followed by possible [*+?]
1426 * Note that the branching code sequences used for ? and the general cases
1427 * of * and + are somewhat optimized: they use the same NOTHING node as
1428 * both the endmarker for their branch list and the body of the last branch.
1429 * It might seem that this node could be dispensed with entirely, but the
1430 * endmarker role is not redundant.
1433 regpiece(I32 *flagp)
1436 register regnode *ret;
1438 register char *next;
1440 char *origparse = PL_regcomp_parse;
1443 I32 max = REG_INFTY;
1445 ret = regatom(&flags);
1447 if (flags & TRYAGAIN)
1452 op = *PL_regcomp_parse;
1454 if (op == '{' && regcurly(PL_regcomp_parse)) {
1455 next = PL_regcomp_parse + 1;
1457 while (isDIGIT(*next) || *next == ',') {
1466 if (*next == '}') { /* got one */
1470 min = atoi(PL_regcomp_parse);
1474 maxpos = PL_regcomp_parse;
1476 if (!max && *maxpos != '0')
1477 max = REG_INFTY; /* meaning "infinity" */
1478 else if (max >= REG_INFTY)
1479 FAIL2("Quantifier in {,} bigger than %d", REG_INFTY - 1);
1480 PL_regcomp_parse = next;
1484 if ((flags&SIMPLE)) {
1485 PL_regnaughty += 2 + PL_regnaughty / 2;
1486 reginsert(CURLY, ret);
1489 PL_regnaughty += 4 + PL_regnaughty; /* compound interest */
1490 regtail(ret, reg_node(WHILEM));
1491 if (!SIZE_ONLY && PL_extralen) {
1492 reginsert(LONGJMP,ret);
1493 reginsert(NOTHING,ret);
1494 NEXT_OFF(ret) = 3; /* Go over LONGJMP. */
1496 reginsert(CURLYX,ret);
1497 if (!SIZE_ONLY && PL_extralen)
1498 NEXT_OFF(ret) = 3; /* Go over NOTHING to LONGJMP. */
1499 regtail(ret, reg_node(NOTHING));
1509 if (max && max < min)
1510 FAIL("Can't do {n,m} with n > m");
1525 #if 0 /* Now runtime fix should be reliable. */
1526 if (!(flags&HASWIDTH) && op != '?')
1527 FAIL("regexp *+ operand could be empty");
1532 *flagp = (op != '+') ? (WORST|SPSTART|HASWIDTH) : (WORST|HASWIDTH);
1534 if (op == '*' && (flags&SIMPLE)) {
1535 reginsert(STAR, ret);
1539 else if (op == '*') {
1543 else if (op == '+' && (flags&SIMPLE)) {
1544 reginsert(PLUS, ret);
1548 else if (op == '+') {
1552 else if (op == '?') {
1557 if (PL_dowarn && !SIZE_ONLY && !(flags&HASWIDTH) && max > 10000) {
1558 warn("%.*s matches null string many times",
1559 PL_regcomp_parse - origparse, origparse);
1562 if (*PL_regcomp_parse == '?') {
1564 reginsert(MINMOD, ret);
1565 regtail(ret, ret + NODE_STEP_REGNODE);
1567 if (ISMULT2(PL_regcomp_parse))
1568 FAIL("nested *?+ in regexp");
1574 - regatom - the lowest level
1576 * Optimization: gobbles an entire sequence of ordinary characters so that
1577 * it can turn them into a single node, which is smaller to store and
1578 * faster to run. Backslashed characters are exceptions, each becoming a
1579 * separate node; the code is simpler that way and it's not worth fixing.
1581 * [Yes, it is worth fixing, some scripts can run twice the speed.]
1587 register regnode *ret = 0;
1590 *flagp = WORST; /* Tentatively. */
1593 switch (*PL_regcomp_parse) {
1597 if (PL_regflags & PMf_MULTILINE)
1598 ret = reg_node(MBOL);
1599 else if (PL_regflags & PMf_SINGLELINE)
1600 ret = reg_node(SBOL);
1602 ret = reg_node(BOL);
1605 if (PL_regcomp_parse[1])
1608 if (PL_regflags & PMf_MULTILINE)
1609 ret = reg_node(MEOL);
1610 else if (PL_regflags & PMf_SINGLELINE)
1611 ret = reg_node(SEOL);
1613 ret = reg_node(EOL);
1618 if (PL_regflags & PMf_SINGLELINE)
1619 ret = reg_node(SANYUTF8);
1621 ret = reg_node(ANYUTF8);
1625 if (PL_regflags & PMf_SINGLELINE)
1626 ret = reg_node(SANY);
1628 ret = reg_node(ANY);
1629 *flagp |= HASWIDTH|SIMPLE;
1635 ret = (UTF ? regclassutf8() : regclass());
1636 *flagp |= HASWIDTH|SIMPLE;
1640 ret = reg(1, &flags);
1642 if (flags & TRYAGAIN)
1646 *flagp |= flags&(HASWIDTH|SPSTART|SIMPLE);
1650 if (flags & TRYAGAIN) {
1654 FAIL2("internal urp in regexp at /%s/", PL_regcomp_parse);
1655 /* Supposed to be caught earlier. */
1658 if (!regcurly(PL_regcomp_parse)) {
1666 FAIL("?+*{} follows nothing in regexp");
1669 switch (*++PL_regcomp_parse) {
1672 ret = reg_node(SBOL);
1677 ret = reg_node(GPOS);
1678 PL_regseen |= REG_SEEN_GPOS;
1683 ret = reg_node(SEOL);
1688 ret = reg_node(EOS);
1690 PL_seen_zerolen++; /* Do not optimize RE away */
1694 ret = reg_node(SANY);
1695 *flagp |= HASWIDTH|SIMPLE;
1699 ret = reg_node(CLUMP);
1702 if (UTF && !PL_utf8_mark)
1703 is_utf8_mark("~"); /* preload table */
1708 ? (LOC ? ALNUMLUTF8 : ALNUMUTF8)
1709 : (LOC ? ALNUML : ALNUM));
1710 *flagp |= HASWIDTH|SIMPLE;
1712 if (UTF && !PL_utf8_alnum)
1713 is_utf8_alnum("a"); /* preload table */
1718 ? (LOC ? NALNUMLUTF8 : NALNUMUTF8)
1719 : (LOC ? NALNUML : NALNUM));
1720 *flagp |= HASWIDTH|SIMPLE;
1722 if (UTF && !PL_utf8_alnum)
1723 is_utf8_alnum("a"); /* preload table */
1729 ? (LOC ? BOUNDLUTF8 : BOUNDUTF8)
1730 : (LOC ? BOUNDL : BOUND));
1733 if (UTF && !PL_utf8_alnum)
1734 is_utf8_alnum("a"); /* preload table */
1740 ? (LOC ? NBOUNDLUTF8 : NBOUNDUTF8)
1741 : (LOC ? NBOUNDL : NBOUND));
1744 if (UTF && !PL_utf8_alnum)
1745 is_utf8_alnum("a"); /* preload table */
1750 ? (LOC ? SPACELUTF8 : SPACEUTF8)
1751 : (LOC ? SPACEL : SPACE));
1752 *flagp |= HASWIDTH|SIMPLE;
1754 if (UTF && !PL_utf8_space)
1755 is_utf8_space(" "); /* preload table */
1760 ? (LOC ? NSPACELUTF8 : NSPACEUTF8)
1761 : (LOC ? NSPACEL : NSPACE));
1762 *flagp |= HASWIDTH|SIMPLE;
1764 if (UTF && !PL_utf8_space)
1765 is_utf8_space(" "); /* preload table */
1768 ret = reg_node(UTF ? DIGITUTF8 : DIGIT);
1769 *flagp |= HASWIDTH|SIMPLE;
1771 if (UTF && !PL_utf8_digit)
1772 is_utf8_digit("1"); /* preload table */
1775 ret = reg_node(UTF ? NDIGITUTF8 : NDIGIT);
1776 *flagp |= HASWIDTH|SIMPLE;
1778 if (UTF && !PL_utf8_digit)
1779 is_utf8_digit("1"); /* preload table */
1791 case '1': case '2': case '3': case '4':
1792 case '5': case '6': case '7': case '8': case '9':
1794 I32 num = atoi(PL_regcomp_parse);
1796 if (num > 9 && num >= PL_regnpar)
1799 if (!SIZE_ONLY && num > PL_regcomp_rx->nparens)
1800 FAIL("reference to nonexistent group");
1803 ? (LOC ? REFFL : REFF)
1806 while (isDIGIT(*PL_regcomp_parse))
1814 if (PL_regcomp_parse >= PL_regxend)
1815 FAIL("trailing \\ in regexp");
1823 if (PL_regflags & PMf_EXTENDED) {
1824 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != '\n') PL_regcomp_parse++;
1825 if (PL_regcomp_parse < PL_regxend)
1841 ? (LOC ? EXACTFL : EXACTF)
1843 s = (char *) OPERAND(ret);
1844 regc(0, s++); /* save spot for len */
1845 for (len = 0, p = PL_regcomp_parse - 1;
1846 len < 127 && p < PL_regxend;
1851 if (PL_regflags & PMf_EXTENDED)
1852 p = regwhite(p, PL_regxend);
1904 char* e = strchr(p, '}');
1907 FAIL("Missing right brace on \\x{}");
1909 ender = scan_hex(p + 1, e - p, &numlen);
1910 if (numlen + len >= 127) { /* numlen is generous */
1917 FAIL("Can't use \\x{} without 'use utf8' declaration");
1920 ender = scan_hex(p, 2, &numlen);
1926 ender = UCHARAT(p++);
1927 ender = toCTRL(ender);
1929 case '0': case '1': case '2': case '3':case '4':
1930 case '5': case '6': case '7': case '8':case '9':
1932 (isDIGIT(p[1]) && atoi(p) >= PL_regnpar) ) {
1933 ender = scan_oct(p, 3, &numlen);
1942 if (p >= PL_regxend)
1943 FAIL("trailing \\ in regexp");
1946 goto normal_default;
1951 if ((*p & 0xc0) == 0xc0 && UTF) {
1952 ender = utf8_to_uv(p, &numlen);
1959 if (PL_regflags & PMf_EXTENDED)
1960 p = regwhite(p, PL_regxend);
1963 ender = toLOWER_LC_uni(ender);
1965 ender = toLOWER_uni(ender);
1967 if (ISMULT2(p)) { /* Back off on ?+*. */
1970 else if (ender >= 0x80 && UTF) {
1971 reguni(ender, s, &numlen);
1981 if (ender >= 0x80 && UTF) {
1982 reguni(ender, s, &numlen);
1990 PL_regcomp_parse = p - 1;
1993 FAIL("internal disaster in regexp");
1999 *OPERAND(ret) = len;
2002 PL_regsize += (len + 2 + sizeof(regnode) - 1) / sizeof(regnode);
2005 PL_regcode += (len + 2 + sizeof(regnode) - 1) / sizeof(regnode);
2015 regwhite(char *p, char *e)
2020 else if (*p == '#') {
2023 } while (p < e && *p != '\n');
2035 register char *opnd, *s;
2037 register I32 lastvalue = 1234;
2038 register I32 range = 0;
2039 register regnode *ret;
2043 s = opnd = (char *) OPERAND(PL_regcode);
2044 ret = reg_node(ANYOF);
2045 for (value = 0; value < 33; value++)
2047 if (*PL_regcomp_parse == '^') { /* Complement of range. */
2051 *opnd |= ANYOF_INVERT;
2054 PL_regcode += ANY_SKIP;
2056 *opnd |= ANYOF_FOLD;
2058 *opnd |= ANYOF_LOCALE;
2061 PL_regsize += ANY_SKIP;
2063 if (*PL_regcomp_parse == ']' || *PL_regcomp_parse == '-')
2064 goto skipcond; /* allow 1st char to be ] or - */
2065 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != ']') {
2067 value = UCHARAT(PL_regcomp_parse++);
2068 if (value == '[' && PL_regcomp_parse + 1 < PL_regxend &&
2069 /* I smell either [: or [= or [. -- POSIX has been here, right? */
2070 (*PL_regcomp_parse == ':' || *PL_regcomp_parse == '=' || *PL_regcomp_parse == '.')) {
2071 char posixccc = *PL_regcomp_parse;
2072 char* posixccs = PL_regcomp_parse++;
2074 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != posixccc)
2076 if (PL_regcomp_parse == PL_regxend)
2077 /* Grandfather lone [:, [=, [. */
2078 PL_regcomp_parse = posixccs;
2080 PL_regcomp_parse++; /* skip over the posixccc */
2081 if (*PL_regcomp_parse == ']') {
2082 /* Not Implemented Yet.
2083 * (POSIX Extended Character Classes, that is)
2084 * The text between e.g. [: and :] would start
2085 * at posixccs + 1 and stop at regcomp_parse - 2. */
2086 if (dowarn && !SIZE_ONLY)
2087 warn("Character class syntax [%c %c] is reserved for future extensions", posixccc, posixccc);
2088 PL_regcomp_parse++; /* skip over the ending ] */
2092 if (value == '\\') {
2093 value = UCHARAT(PL_regcomp_parse++);
2098 *opnd |= ANYOF_ALNUML;
2100 for (value = 0; value < 256; value++)
2102 ANYOF_SET(opnd, value);
2110 *opnd |= ANYOF_NALNUML;
2112 for (value = 0; value < 256; value++)
2113 if (!isALNUM(value))
2114 ANYOF_SET(opnd, value);
2122 *opnd |= ANYOF_SPACEL;
2124 for (value = 0; value < 256; value++)
2126 ANYOF_SET(opnd, value);
2134 *opnd |= ANYOF_NSPACEL;
2136 for (value = 0; value < 256; value++)
2137 if (!isSPACE(value))
2138 ANYOF_SET(opnd, value);
2145 for (value = '0'; value <= '9'; value++)
2146 ANYOF_SET(opnd, value);
2152 for (value = 0; value < '0'; value++)
2153 ANYOF_SET(opnd, value);
2154 for (value = '9' + 1; value < 256; value++)
2155 ANYOF_SET(opnd, value);
2181 value = scan_hex(PL_regcomp_parse, 2, &numlen);
2182 PL_regcomp_parse += numlen;
2185 value = UCHARAT(PL_regcomp_parse++);
2186 value = toCTRL(value);
2188 case '0': case '1': case '2': case '3': case '4':
2189 case '5': case '6': case '7': case '8': case '9':
2190 value = scan_oct(--PL_regcomp_parse, 3, &numlen);
2191 PL_regcomp_parse += numlen;
2196 if (lastvalue > value)
2197 FAIL("invalid [] range in regexp");
2202 if (*PL_regcomp_parse == '-' && PL_regcomp_parse+1 < PL_regxend &&
2203 PL_regcomp_parse[1] != ']') {
2206 continue; /* do it next time */
2210 for ( ; lastvalue <= value; lastvalue++)
2211 ANYOF_SET(opnd, lastvalue);
2215 if (*PL_regcomp_parse != ']')
2216 FAIL("unmatched [] in regexp");
2218 /* optimize case-insensitive simple patterns (e.g. /[a-z]/i) */
2219 if (!SIZE_ONLY && (*opnd & (0xFF ^ ANYOF_INVERT)) == ANYOF_FOLD) {
2220 for (value = 0; value < 256; ++value) {
2221 if (ANYOF_TEST(opnd, value)) {
2222 I32 cf = fold[value];
2223 ANYOF_SET(opnd, cf);
2226 *opnd &= ~ANYOF_FOLD;
2228 /* optimize inverted simple patterns (e.g. [^a-z]) */
2229 if (!SIZE_ONLY && (*opnd & 0xFF) == ANYOF_INVERT) {
2230 for (value = 0; value < 32; ++value)
2231 opnd[1 + value] ^= 0xFF;
2240 register char *opnd, *e;
2242 register U32 lastvalue = 123456;
2243 register I32 range = 0;
2244 register regnode *ret;
2251 if (*PL_regcomp_parse == '^') { /* Complement of range. */
2255 flags |= ANYOF_INVERT;
2259 flags |= ANYOF_FOLD;
2261 flags |= ANYOF_LOCALE;
2262 listsv = newSVpv("# comment\n",0);
2265 if (*PL_regcomp_parse == ']' || *PL_regcomp_parse == '-')
2266 goto skipcond; /* allow 1st char to be ] or - */
2268 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != ']') {
2270 value = utf8_to_uv(PL_regcomp_parse, &numlen);
2271 PL_regcomp_parse += numlen;
2273 if (value == '[' && PL_regcomp_parse + 1 < PL_regxend &&
2274 /* I smell either [: or [= or [. -- POSIX has been here, right? */
2275 (*PL_regcomp_parse == ':' || *PL_regcomp_parse == '=' || *PL_regcomp_parse == '.')) {
2276 char posixccc = *PL_regcomp_parse;
2277 char* posixccs = PL_regcomp_parse++;
2279 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != posixccc)
2281 if (PL_regcomp_parse == PL_regxend)
2282 /* Grandfather lone [:, [=, [. */
2283 PL_regcomp_parse = posixccs;
2285 PL_regcomp_parse++; /* skip over the posixccc */
2286 if (*PL_regcomp_parse == ']') {
2287 /* Not Implemented Yet.
2288 * (POSIX Extended Character Classes, that is)
2289 * The text between e.g. [: and :] would start
2290 * at posixccs + 1 and stop at regcomp_parse - 2. */
2291 if (dowarn && !SIZE_ONLY)
2292 warn("Character class syntax [%c %c] is reserved for future extensions", posixccc, posixccc);
2293 PL_regcomp_parse++; /* skip over the ending ] */
2298 if (value == '\\') {
2299 value = utf8_to_uv(PL_regcomp_parse, &numlen);
2300 PL_regcomp_parse += numlen;
2305 flags |= ANYOF_ALNUML;
2307 sv_catpvf(listsv, "+utf8::IsAlnum\n");
2314 flags |= ANYOF_NALNUML;
2317 "-utf8::IsAlpha\n-utf8::IsDigit\n0000\t%04x\n%04x\tffff\n",
2326 flags |= ANYOF_SPACEL;
2327 sv_catpvf(listsv, "+utf8::IsSpace\n");
2336 flags |= ANYOF_NSPACEL;
2338 "!utf8::IsSpace\n");
2346 sv_catpvf(listsv, "+utf8::IsDigit\n");
2353 "!utf8::IsDigit\n");
2359 if (*PL_regcomp_parse == '{') {
2360 e = strchr(PL_regcomp_parse++, '}');
2362 FAIL("Missing right brace on \\p{}");
2363 n = e - PL_regcomp_parse;
2366 e = PL_regcomp_parse;
2371 sv_catpvf(listsv, "+utf8::%.*s\n", n, PL_regcomp_parse);
2374 "!utf8::%.*s\n", n, PL_regcomp_parse);
2376 PL_regcomp_parse = e + 1;
2401 if (*PL_regcomp_parse == '{') {
2402 e = strchr(PL_regcomp_parse++, '}');
2404 FAIL("Missing right brace on \\x{}");
2405 value = scan_hex(PL_regcomp_parse + 1, e - PL_regcomp_parse, &numlen);
2406 PL_regcomp_parse = e + 1;
2409 value = scan_hex(PL_regcomp_parse, 2, &numlen);
2410 PL_regcomp_parse += numlen;
2414 value = UCHARAT(PL_regcomp_parse++);
2415 value = toCTRL(value);
2417 case '0': case '1': case '2': case '3': case '4':
2418 case '5': case '6': case '7': case '8': case '9':
2419 value = scan_oct(--PL_regcomp_parse, 3, &numlen);
2420 PL_regcomp_parse += numlen;
2425 if (lastvalue > value)
2426 FAIL("invalid [] range in regexp");
2428 sv_catpvf(listsv, "%04x\t%04x\n", lastvalue, value);
2434 if (*PL_regcomp_parse == '-' && PL_regcomp_parse+1 < PL_regxend &&
2435 PL_regcomp_parse[1] != ']') {
2438 continue; /* do it next time */
2441 sv_catpvf(listsv, "%04x\n", value);
2444 if (*PL_regcomp_parse != ']')
2445 FAIL("unmatched [] in regexp");
2448 ret = reganode(ANYOFUTF8, 0);
2451 SV *rv = swash_init("utf8", "", listsv, 1, 0);
2452 SvREFCNT_dec(listsv);
2453 n = add_data(1,"s");
2454 PL_regcomp_rx->data->data[n] = (void*)rv;
2455 ARG1_SET(ret, flags);
2466 char* retval = PL_regcomp_parse++;
2469 if (*PL_regcomp_parse == '(' && PL_regcomp_parse[1] == '?' &&
2470 PL_regcomp_parse[2] == '#') {
2471 while (*PL_regcomp_parse && *PL_regcomp_parse != ')')
2476 if (PL_regflags & PMf_EXTENDED) {
2477 if (isSPACE(*PL_regcomp_parse)) {
2481 else if (*PL_regcomp_parse == '#') {
2482 while (*PL_regcomp_parse && *PL_regcomp_parse != '\n')
2493 - reg_node - emit a node
2495 STATIC regnode * /* Location. */
2499 register regnode *ret;
2500 register regnode *ptr;
2504 SIZE_ALIGN(PL_regsize);
2509 NODE_ALIGN_FILL(ret);
2511 FILL_ADVANCE_NODE(ptr, op);
2518 - reganode - emit a node with an argument
2520 STATIC regnode * /* Location. */
2521 reganode(U8 op, U32 arg)
2524 register regnode *ret;
2525 register regnode *ptr;
2529 SIZE_ALIGN(PL_regsize);
2534 NODE_ALIGN_FILL(ret);
2536 FILL_ADVANCE_NODE_ARG(ptr, op, arg);
2543 - regc - emit (if appropriate) a Unicode character
2546 reguni(UV uv, char* s, I32* lenp)
2551 *lenp = uv_to_utf8(tmpbuf, uv) - tmpbuf;
2554 *lenp = uv_to_utf8(s, uv) - s;
2559 - regc - emit (if appropriate) a byte of code
2570 - reginsert - insert an operator in front of already-emitted operand
2572 * Means relocating the operand.
2575 reginsert(U8 op, regnode *opnd)
2578 register regnode *src;
2579 register regnode *dst;
2580 register regnode *place;
2581 register int offset = regarglen[(U8)op];
2583 /* (regkind[(U8)op] == CURLY ? EXTRA_STEP_2ARGS : 0); */
2586 PL_regsize += NODE_STEP_REGNODE + offset;
2591 PL_regcode += NODE_STEP_REGNODE + offset;
2594 StructCopy(--src, --dst, regnode);
2596 place = opnd; /* Op node, where operand used to be. */
2597 src = NEXTOPER(place);
2598 FILL_ADVANCE_NODE(place, op);
2599 Zero(src, offset, regnode);
2603 - regtail - set the next-pointer at the end of a node chain of p to val.
2606 regtail(regnode *p, regnode *val)
2609 register regnode *scan;
2610 register regnode *temp;
2611 register I32 offset;
2616 /* Find last node. */
2619 temp = regnext(scan);
2625 if (reg_off_by_arg[OP(scan)]) {
2626 ARG_SET(scan, val - scan);
2629 NEXT_OFF(scan) = val - scan;
2634 - regoptail - regtail on operand of first argument; nop if operandless
2637 regoptail(regnode *p, regnode *val)
2640 /* "Operandless" and "op != BRANCH" are synonymous in practice. */
2641 if (p == NULL || SIZE_ONLY)
2643 if (regkind[(U8)OP(p)] == BRANCH) {
2644 regtail(NEXTOPER(p), val);
2646 else if ( regkind[(U8)OP(p)] == BRANCHJ) {
2647 regtail(NEXTOPER(NEXTOPER(p)), val);
2654 - regcurly - a little FSA that accepts {\d+,?\d*}
2657 regcurly(register char *s)
2676 dumpuntil(regnode *start, regnode *node, regnode *last, SV* sv, I32 l)
2679 register char op = EXACT; /* Arbitrary non-END op. */
2680 register regnode *next, *onode;
2682 while (op != END && (!last || node < last)) {
2683 /* While that wasn't END last time... */
2689 next = regnext(node);
2691 if (OP(node) == OPTIMIZED)
2694 PerlIO_printf(Perl_debug_log, "%4d:%*s%s", node - start,
2695 2*l + 1, "", SvPVX(sv));
2696 if (next == NULL) /* Next ptr. */
2697 PerlIO_printf(Perl_debug_log, "(0)");
2699 PerlIO_printf(Perl_debug_log, "(%d)", next - start);
2700 (void)PerlIO_putc(Perl_debug_log, '\n');
2702 if (regkind[(U8)op] == BRANCHJ) {
2703 register regnode *nnode = (OP(next) == LONGJMP
2706 if (last && nnode > last)
2708 node = dumpuntil(start, NEXTOPER(NEXTOPER(node)), nnode, sv, l + 1);
2710 else if (regkind[(U8)op] == BRANCH) {
2711 node = dumpuntil(start, NEXTOPER(node), next, sv, l + 1);
2713 else if ( op == CURLY) { /* `next' might be very big: optimizer */
2714 node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
2715 NEXTOPER(node) + EXTRA_STEP_2ARGS + 1, sv, l + 1);
2717 else if (regkind[(U8)op] == CURLY && op != CURLYX) {
2718 node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
2721 else if ( op == PLUS || op == STAR) {
2722 node = dumpuntil(start, NEXTOPER(node), NEXTOPER(node) + 1, sv, l + 1);
2724 else if (op == ANYOF) {
2725 node = NEXTOPER(node);
2728 else if (regkind[(U8)op] == EXACT) {
2729 /* Literal string, where present. */
2730 node += ((*OPERAND(node)) + 2 + sizeof(regnode) - 1) / sizeof(regnode);
2731 node = NEXTOPER(node);
2734 node = NEXTOPER(node);
2735 node += regarglen[(U8)op];
2737 if (op == CURLYX || op == OPEN)
2739 else if (op == WHILEM)
2742 #endif /* DEBUGGING */
2747 - regdump - dump a regexp onto Perl_debug_log in vaguely comprehensible form
2754 SV *sv = sv_newmortal();
2756 (void)dumpuntil(r->program, r->program + 1, NULL, sv, 0);
2758 /* Header fields of interest. */
2759 if (r->anchored_substr)
2760 PerlIO_printf(Perl_debug_log, "anchored `%s%s%s'%s at %d ",
2762 SvPVX(r->anchored_substr),
2764 SvTAIL(r->anchored_substr) ? "$" : "",
2765 r->anchored_offset);
2766 if (r->float_substr)
2767 PerlIO_printf(Perl_debug_log, "floating `%s%s%s'%s at %d..%u ",
2769 SvPVX(r->float_substr),
2771 SvTAIL(r->float_substr) ? "$" : "",
2772 r->float_min_offset, r->float_max_offset);
2773 if (r->check_substr)
2774 PerlIO_printf(Perl_debug_log,
2775 r->check_substr == r->float_substr
2776 ? "(checking floating" : "(checking anchored");
2777 if (r->reganch & ROPT_NOSCAN)
2778 PerlIO_printf(Perl_debug_log, " noscan");
2779 if (r->reganch & ROPT_CHECK_ALL)
2780 PerlIO_printf(Perl_debug_log, " isall");
2781 if (r->check_substr)
2782 PerlIO_printf(Perl_debug_log, ") ");
2784 if (r->regstclass) {
2785 regprop(sv, r->regstclass);
2786 PerlIO_printf(Perl_debug_log, "stclass `%s' ", SvPVX(sv));
2788 if (r->reganch & ROPT_ANCH) {
2789 PerlIO_printf(Perl_debug_log, "anchored");
2790 if (r->reganch & ROPT_ANCH_BOL)
2791 PerlIO_printf(Perl_debug_log, "(BOL)");
2792 if (r->reganch & ROPT_ANCH_MBOL)
2793 PerlIO_printf(Perl_debug_log, "(MBOL)");
2794 if (r->reganch & ROPT_ANCH_GPOS)
2795 PerlIO_printf(Perl_debug_log, "(GPOS)");
2796 PerlIO_putc(Perl_debug_log, ' ');
2798 if (r->reganch & ROPT_GPOS_SEEN)
2799 PerlIO_printf(Perl_debug_log, "GPOS ");
2800 if (r->reganch & ROPT_SKIP)
2801 PerlIO_printf(Perl_debug_log, "plus ");
2802 if (r->reganch & ROPT_IMPLICIT)
2803 PerlIO_printf(Perl_debug_log, "implicit ");
2804 PerlIO_printf(Perl_debug_log, "minlen %ld ", (long) r->minlen);
2805 if (r->reganch & ROPT_EVAL_SEEN)
2806 PerlIO_printf(Perl_debug_log, "with eval ");
2807 PerlIO_printf(Perl_debug_log, "\n");
2808 #endif /* DEBUGGING */
2812 - regprop - printable representation of opcode
2815 regprop(SV *sv, regnode *o)
2819 register char *p = 0;
2821 sv_setpvn(sv, "", 0);
2866 sv_catpvf(sv, "EXACT <%s%s%s>", PL_colors[0], OPERAND(o) + 1, PL_colors[1]);
2869 sv_catpvf(sv, "EXACTF <%s%s%s>", PL_colors[0], OPERAND(o) + 1, PL_colors[1]);
2872 sv_catpvf(sv, "EXACTFL <%s%s%s>", PL_colors[0], OPERAND(o) + 1, PL_colors[1]);
2899 sv_catpvf(sv, "CURLY {%d,%d}", ARG1(o), ARG2(o));
2902 sv_catpvf(sv, "CURLYM[%d] {%d,%d}", o->flags, ARG1(o), ARG2(o));
2905 sv_catpvf(sv, "CURLYN[%d] {%d,%d}", o->flags, ARG1(o), ARG2(o));
2908 sv_catpvf(sv, "CURLYX {%d,%d}", ARG1(o), ARG2(o));
2911 sv_catpvf(sv, "REF%d", ARG(o));
2914 sv_catpvf(sv, "REFF%d", ARG(o));
2917 sv_catpvf(sv, "REFFL%d", ARG(o));
2920 sv_catpvf(sv, "OPEN%d", ARG(o));
2923 sv_catpvf(sv, "CLOSE%d", ARG(o));
2939 sv_catpvf(sv, "UNLESSM[-%d]", o->flags);
2942 sv_catpvf(sv, "IFMATCH[-%d]", o->flags);
2993 sv_catpvf(sv, "GROUPP%d", ARG(o));
3008 FAIL("corrupted regexp opcode");
3012 #endif /* DEBUGGING */
3016 pregfree(struct regexp *r)
3019 if (!r || (--r->refcnt > 0))
3022 Safefree(r->precomp);
3024 Safefree(r->subbase);
3026 if (r->anchored_substr)
3027 SvREFCNT_dec(r->anchored_substr);
3028 if (r->float_substr)
3029 SvREFCNT_dec(r->float_substr);
3030 Safefree(r->substrs);
3033 int n = r->data->count;
3035 switch (r->data->what[n]) {
3037 SvREFCNT_dec((SV*)r->data->data[n]);
3040 op_free((OP_4tree*)r->data->data[n]);
3045 FAIL2("panic: regfree data code '%c'", r->data->what[n]);
3048 Safefree(r->data->what);
3051 Safefree(r->startp);
3057 - regnext - dig the "next" pointer out of a node
3059 * [Note, when REGALIGN is defined there are two places in regmatch()
3060 * that bypass this code for speed.]
3063 regnext(register regnode *p)
3066 register I32 offset;
3068 if (p == &PL_regdummy)
3071 offset = (reg_off_by_arg[OP(p)] ? ARG(p) : NEXT_OFF(p));
3079 re_croak2(const char* pat1,const char* pat2,...)
3082 STRLEN l1 = strlen(pat1);
3083 STRLEN l2 = strlen(pat2);
3091 Copy(pat1, buf, l1 , char);
3092 Copy(pat2, buf + l1, l2 , char);
3093 buf[l1 + l2] = '\n';
3094 buf[l1 + l2 + 1] = '\0';
3095 va_start(args, pat2);
3096 message = mess(buf, &args);
3098 l1 = strlen(message);
3101 Copy(message, buf, l1 , char);
3102 buf[l1] = '\0'; /* Overwrite \n */
3106 /* XXX Here's a total kludge. But we need to re-enter for swash routines. */
3109 save_re_context(void)
3113 SAVEPPTR(PL_regprecomp); /* uncompiled string. */
3114 SAVEI32(PL_regnpar); /* () count. */
3115 SAVEI32(PL_regsize); /* Code size. */
3116 SAVEI16(PL_regflags); /* are we folding, multilining? */
3117 SAVEPPTR(PL_reginput); /* String-input pointer. */
3118 SAVEPPTR(PL_regbol); /* Beginning of input, for ^ check. */
3119 SAVEPPTR(PL_regeol); /* End of input, for $ check. */
3120 SAVESPTR(PL_regstartp); /* Pointer to startp array. */
3121 SAVESPTR(PL_regendp); /* Ditto for endp. */
3122 SAVESPTR(PL_reglastparen); /* Similarly for lastparen. */
3123 SAVEPPTR(PL_regtill); /* How far we are required to go. */
3124 SAVEI32(PL_regprev); /* char before regbol, \n if none */
3125 SAVESPTR(PL_reg_start_tmp); /* from regexec.c */
3126 PL_reg_start_tmp = 0;
3127 SAVEFREEPV(PL_reg_start_tmp);
3128 SAVEI32(PL_reg_start_tmpl); /* from regexec.c */
3129 PL_reg_start_tmpl = 0;
3130 SAVESPTR(PL_regdata);
3131 SAVEI32(PL_reg_flags); /* from regexec.c */
3132 SAVEI32(PL_reg_eval_set); /* from regexec.c */
3133 SAVEI32(PL_regnarrate); /* from regexec.c */
3134 SAVESPTR(PL_regprogram); /* from regexec.c */
3135 SAVEINT(PL_regindent); /* from regexec.c */
3136 SAVESPTR(PL_regcc); /* from regexec.c */
3137 SAVESPTR(PL_curcop);
3138 SAVESPTR(PL_regcomp_rx); /* from regcomp.c */
3139 SAVEI32(PL_regseen); /* from regcomp.c */
3140 SAVEI32(PL_regsawback); /* Did we see \1, ...? */
3141 SAVEI32(PL_regnaughty); /* How bad is this pattern? */
3142 SAVESPTR(PL_regcode); /* Code-emit pointer; ®dummy = don't */
3143 SAVEPPTR(PL_regxend); /* End of input for compile */
3144 SAVEPPTR(PL_regcomp_parse); /* Input-scan pointer. */