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)
209 STRLEN l = CHR_SVLEN(data->last_found);
210 STRLEN old_l = CHR_SVLEN(*data->longest);
212 if ((l >= old_l) && ((l > old_l) || (data->flags & SF_BEFORE_EOL))) {
213 sv_setsv(*data->longest, data->last_found);
214 if (*data->longest == data->longest_fixed) {
215 data->offset_fixed = l ? data->last_start_min : data->pos_min;
216 if (data->flags & SF_BEFORE_EOL)
218 |= ((data->flags & SF_BEFORE_EOL) << SF_FIX_SHIFT_EOL);
220 data->flags &= ~SF_FIX_BEFORE_EOL;
223 data->offset_float_min = l ? data->last_start_min : data->pos_min;
224 data->offset_float_max = (l
225 ? data->last_start_max
226 : data->pos_min + data->pos_delta);
227 if (data->flags & SF_BEFORE_EOL)
229 |= ((data->flags & SF_BEFORE_EOL) << SF_FL_SHIFT_EOL);
231 data->flags &= ~SF_FL_BEFORE_EOL;
234 SvCUR_set(data->last_found, 0);
236 data->flags &= ~SF_BEFORE_EOL;
239 /* Stops at toplevel WHILEM as well as at `last'. At end *scanp is set
240 to the position after last scanned or to NULL. */
243 study_chunk(regnode **scanp, I32 *deltap, regnode *last, scan_data_t *data, U32 flags)
244 /* scanp: Start here (read-write). */
245 /* deltap: Write maxlen-minlen here. */
246 /* last: Stop before this one. */
249 I32 min = 0, pars = 0, code;
250 regnode *scan = *scanp, *next;
252 int is_inf = (flags & SCF_DO_SUBSTR) && (data->flags & SF_IS_INF);
253 I32 is_par = OP(scan) == OPEN ? ARG(scan) : 0;
254 scan_data_t data_fake;
256 while (scan && OP(scan) != END && scan < last) {
257 /* Peephole optimizer: */
259 if (regkind[(U8)OP(scan)] == EXACT) {
260 regnode *n = regnext(scan);
263 regnode *stop = scan;
266 next = scan + (*OPERAND(scan) + 2 - 1)/sizeof(regnode) + 2;
267 /* Skip NOTHING, merge EXACT*. */
269 ( regkind[(U8)OP(n)] == NOTHING ||
270 (stringok && (OP(n) == OP(scan))))
272 && NEXT_OFF(scan) + NEXT_OFF(n) < I16_MAX) {
273 if (OP(n) == TAIL || n > next)
275 if (regkind[(U8)OP(n)] == NOTHING) {
276 NEXT_OFF(scan) += NEXT_OFF(n);
277 next = n + NODE_STEP_REGNODE;
285 int oldl = *OPERAND(scan);
286 regnode *nnext = regnext(n);
288 if (oldl + *OPERAND(n) > U8_MAX)
290 NEXT_OFF(scan) += NEXT_OFF(n);
291 *OPERAND(scan) += *OPERAND(n);
292 next = n + (*OPERAND(n) + 2 - 1)/sizeof(regnode) + 2;
293 /* Now we can overwrite *n : */
294 Move(OPERAND(n) + 1, OPERAND(scan) + oldl + 1,
295 *OPERAND(n) + 1, char);
305 n = scan + (*OPERAND(scan) + 2 - 1)/sizeof(regnode) + 2;
307 /* Purify reports a benign UMR here sometimes, because we
308 * don't initialize the OP() slot of a node when that node
309 * is occupied by just the trailing null of the string in
311 if (regkind[(U8)OP(n)] != NOTHING || OP(n) == NOTHING) {
320 if (OP(scan) != CURLYX) {
321 int max = (reg_off_by_arg[OP(scan)] ? I32_MAX : U16_MAX);
322 int off = (reg_off_by_arg[OP(scan)] ? ARG(scan) : NEXT_OFF(scan));
326 /* Skip NOTHING and LONGJMP. */
327 while ((n = regnext(n))
328 && ((regkind[(U8)OP(n)] == NOTHING && (noff = NEXT_OFF(n)))
329 || ((OP(n) == LONGJMP) && (noff = ARG(n))))
332 if (reg_off_by_arg[OP(scan)])
335 NEXT_OFF(scan) = off;
337 if (OP(scan) == BRANCH || OP(scan) == BRANCHJ
338 || OP(scan) == IFTHEN || OP(scan) == SUSPEND) {
339 next = regnext(scan);
342 if (OP(next) == code || code == IFTHEN || code == SUSPEND) {
343 I32 max1 = 0, min1 = I32_MAX, num = 0;
345 if (flags & SCF_DO_SUBSTR)
347 while (OP(scan) == code) {
348 I32 deltanext, minnext;
352 next = regnext(scan);
353 scan = NEXTOPER(scan);
355 scan = NEXTOPER(scan);
356 /* We suppose the run is continuous, last=next...*/
357 minnext = study_chunk(&scan, &deltanext, next,
361 if (max1 < minnext + deltanext)
362 max1 = minnext + deltanext;
363 if (deltanext == I32_MAX)
366 if (data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
368 if (data && (data_fake.flags & SF_HAS_EVAL))
369 data->flags |= SF_HAS_EVAL;
373 if (code == IFTHEN && num < 2) /* Empty ELSE branch */
375 if (flags & SCF_DO_SUBSTR) {
376 data->pos_min += min1;
377 data->pos_delta += max1 - min1;
378 if (max1 != min1 || is_inf)
379 data->longest = &(data->longest_float);
382 delta += max1 - min1;
384 else if (code == BRANCHJ) /* single branch is optimized. */
385 scan = NEXTOPER(NEXTOPER(scan));
386 else /* single branch is optimized. */
387 scan = NEXTOPER(scan);
390 else if (OP(scan) == EXACT) {
391 I32 l = *OPERAND(scan);
393 unsigned char *s = (unsigned char *)(OPERAND(scan)+1);
394 unsigned char *e = s + l;
403 if (flags & SCF_DO_SUBSTR) { /* Update longest substr. */
404 /* The code below prefers earlier match for fixed
405 offset, later match for variable offset. */
406 if (data->last_end == -1) { /* Update the start info. */
407 data->last_start_min = data->pos_min;
408 data->last_start_max = is_inf
409 ? I32_MAX : data->pos_min + data->pos_delta;
411 sv_catpvn(data->last_found, (char *)(OPERAND(scan)+1), *OPERAND(scan));
412 data->last_end = data->pos_min + l;
413 data->pos_min += l; /* As in the first entry. */
414 data->flags &= ~SF_BEFORE_EOL;
417 else if (regkind[(U8)OP(scan)] == EXACT) {
418 I32 l = *OPERAND(scan);
419 if (flags & SCF_DO_SUBSTR)
422 unsigned char *s = (unsigned char *)(OPERAND(scan)+1);
423 unsigned char *e = s + l;
432 if (data && (flags & SCF_DO_SUBSTR))
435 else if (strchr(varies,OP(scan))) {
436 I32 mincount, maxcount, minnext, deltanext, pos_before, fl;
437 regnode *oscan = scan;
439 switch (regkind[(U8)OP(scan)]) {
441 scan = NEXTOPER(scan);
444 if (flags & SCF_DO_SUBSTR) {
445 next = NEXTOPER(scan);
446 if (OP(next) == EXACT) {
448 maxcount = REG_INFTY;
449 next = regnext(scan);
450 scan = NEXTOPER(scan);
454 if (flags & SCF_DO_SUBSTR)
460 scan = regnext(scan);
461 if (flags & SCF_DO_SUBSTR) {
463 data->longest = &(data->longest_float);
465 goto optimize_curly_tail;
467 mincount = ARG1(scan);
468 maxcount = ARG2(scan);
469 next = regnext(scan);
470 scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
472 if (flags & SCF_DO_SUBSTR) {
473 if (mincount == 0) scan_commit(data);
474 pos_before = data->pos_min;
478 data->flags &= ~(SF_HAS_PAR|SF_IN_PAR|SF_HAS_EVAL);
480 data->flags |= SF_IS_INF;
482 /* This will finish on WHILEM, setting scan, or on NULL: */
483 minnext = study_chunk(&scan, &deltanext, last, data,
485 ? (flags & ~SCF_DO_SUBSTR) : flags);
486 if (!scan) /* It was not CURLYX, but CURLY. */
488 if (PL_dowarn && (minnext + deltanext == 0)
489 && !(data->flags & (SF_HAS_PAR|SF_IN_PAR))
490 && maxcount <= 10000) /* Complement check for big count */
491 warn("Strange *+?{} on zero-length expression");
492 min += minnext * mincount;
493 is_inf |= (maxcount == REG_INFTY && (minnext + deltanext) > 0
494 || deltanext == I32_MAX);
495 delta += (minnext + deltanext) * maxcount - minnext * mincount;
497 /* Try powerful optimization CURLYX => CURLYN. */
498 if ( OP(oscan) == CURLYX && data
499 && data->flags & SF_IN_PAR
500 && !(data->flags & SF_HAS_EVAL)
501 && !deltanext && minnext == 1 ) {
502 /* Try to optimize to CURLYN. */
503 regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS;
504 regnode *nxt1 = nxt, *nxt2;
508 if (!strchr(simple,OP(nxt))
509 && !(regkind[(U8)OP(nxt)] == EXACT
510 && *OPERAND(nxt) == 1))
514 if (OP(nxt) != CLOSE)
516 /* Now we know that nxt2 is the only contents: */
517 oscan->flags = ARG(nxt);
519 OP(nxt1) = NOTHING; /* was OPEN. */
521 OP(nxt1 + 1) = OPTIMIZED; /* was count. */
522 NEXT_OFF(nxt1+ 1) = 0; /* just for consistancy. */
523 NEXT_OFF(nxt2) = 0; /* just for consistancy with CURLY. */
524 OP(nxt) = OPTIMIZED; /* was CLOSE. */
525 OP(nxt + 1) = OPTIMIZED; /* was count. */
526 NEXT_OFF(nxt+ 1) = 0; /* just for consistancy. */
531 /* Try optimization CURLYX => CURLYM. */
532 if ( OP(oscan) == CURLYX && data
533 && !(data->flags & SF_HAS_PAR)
534 && !(data->flags & SF_HAS_EVAL)
536 /* XXXX How to optimize if data == 0? */
537 /* Optimize to a simpler form. */
538 regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN */
542 while ( (nxt2 = regnext(nxt)) /* skip over embedded stuff*/
543 && (OP(nxt2) != WHILEM))
545 OP(nxt2) = SUCCEED; /* Whas WHILEM */
546 /* Need to optimize away parenths. */
547 if (data->flags & SF_IN_PAR) {
548 /* Set the parenth number. */
549 regnode *nxt1 = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN*/
551 if (OP(nxt) != CLOSE)
552 FAIL("panic opt close");
553 oscan->flags = ARG(nxt);
554 OP(nxt1) = OPTIMIZED; /* was OPEN. */
555 OP(nxt) = OPTIMIZED; /* was CLOSE. */
557 OP(nxt1 + 1) = OPTIMIZED; /* was count. */
558 OP(nxt + 1) = OPTIMIZED; /* was count. */
559 NEXT_OFF(nxt1 + 1) = 0; /* just for consistancy. */
560 NEXT_OFF(nxt + 1) = 0; /* just for consistancy. */
563 while ( nxt1 && (OP(nxt1) != WHILEM)) {
564 regnode *nnxt = regnext(nxt1);
567 if (reg_off_by_arg[OP(nxt1)])
568 ARG_SET(nxt1, nxt2 - nxt1);
569 else if (nxt2 - nxt1 < U16_MAX)
570 NEXT_OFF(nxt1) = nxt2 - nxt1;
572 OP(nxt) = NOTHING; /* Cannot beautify */
577 /* Optimize again: */
578 study_chunk(&nxt1, &deltanext, nxt, NULL, 0);
583 if (data && fl & (SF_HAS_PAR|SF_IN_PAR))
585 if (flags & SCF_DO_SUBSTR) {
586 SV *last_str = Nullsv;
587 int counted = mincount != 0;
589 if (data->last_end > 0 && mincount != 0) { /* Ends with a string. */
590 I32 b = pos_before >= data->last_start_min
591 ? pos_before : data->last_start_min;
593 char *s = SvPV(data->last_found, l);
594 I32 old = b - data->last_start_min;
597 old = utf8_hop((U8*)s, old) - (U8*)s;
600 /* Get the added string: */
601 last_str = newSVpv(s + old, l);
602 if (deltanext == 0 && pos_before == b) {
603 /* What was added is a constant string */
605 SvGROW(last_str, (mincount * l) + 1);
606 repeatcpy(SvPVX(last_str) + l,
607 SvPVX(last_str), l, mincount - 1);
608 SvCUR(last_str) *= mincount;
609 /* Add additional parts. */
610 SvCUR_set(data->last_found,
611 SvCUR(data->last_found) - l);
612 sv_catsv(data->last_found, last_str);
613 data->last_end += l * (mincount - 1);
617 /* It is counted once already... */
618 data->pos_min += minnext * (mincount - counted);
619 data->pos_delta += - counted * deltanext +
620 (minnext + deltanext) * maxcount - minnext * mincount;
621 if (mincount != maxcount) {
623 if (mincount && last_str) {
624 sv_setsv(data->last_found, last_str);
625 data->last_end = data->pos_min;
626 data->last_start_min =
627 data->pos_min - CHR_SVLEN(last_str);
628 data->last_start_max = is_inf
630 : data->pos_min + data->pos_delta
631 - CHR_SVLEN(last_str);
633 data->longest = &(data->longest_float);
636 if (data && (fl & SF_HAS_EVAL))
637 data->flags |= SF_HAS_EVAL;
639 if (OP(oscan) != CURLYX) {
640 while (regkind[(U8)OP(next = regnext(oscan))] == NOTHING
642 NEXT_OFF(oscan) += NEXT_OFF(next);
645 default: /* REF only? */
646 if (flags & SCF_DO_SUBSTR) {
648 data->longest = &(data->longest_float);
654 else if (strchr(simple,OP(scan)) || regkind[(U8)OP(scan)] == ANYUTF8) {
655 if (flags & SCF_DO_SUBSTR) {
661 else if (regkind[(U8)OP(scan)] == EOL && flags & SCF_DO_SUBSTR) {
662 data->flags |= (OP(scan) == MEOL
666 else if (regkind[(U8)OP(scan)] == BRANCHJ
667 && (scan->flags || data)
668 && (OP(scan) == IFMATCH || OP(scan) == UNLESSM)) {
669 I32 deltanext, minnext;
673 next = regnext(scan);
674 nscan = NEXTOPER(NEXTOPER(scan));
675 minnext = study_chunk(&nscan, &deltanext, last, &data_fake, 0);
678 FAIL("variable length lookbehind not implemented");
680 else if (minnext > U8_MAX) {
681 FAIL2("lookbehind longer than %d not implemented", U8_MAX);
683 scan->flags = minnext;
685 if (data && data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
687 if (data && (data_fake.flags & SF_HAS_EVAL))
688 data->flags |= SF_HAS_EVAL;
690 else if (OP(scan) == OPEN) {
693 else if (OP(scan) == CLOSE && ARG(scan) == is_par) {
694 next = regnext(scan);
696 if ( next && (OP(next) != WHILEM) && next < last)
697 is_par = 0; /* Disable optimization */
699 else if (OP(scan) == EVAL) {
701 data->flags |= SF_HAS_EVAL;
703 /* Else: zero-length, ignore. */
704 scan = regnext(scan);
709 *deltap = is_inf ? I32_MAX : delta;
710 if (flags & SCF_DO_SUBSTR && is_inf)
711 data->pos_delta = I32_MAX - data->pos_min;
714 if (is_par && pars==1 && data) {
715 data->flags |= SF_IN_PAR;
716 data->flags &= ~SF_HAS_PAR;
718 else if (pars && data) {
719 data->flags |= SF_HAS_PAR;
720 data->flags &= ~SF_IN_PAR;
726 add_data(I32 n, char *s)
729 if (PL_regcomp_rx->data) {
730 Renewc(PL_regcomp_rx->data,
731 sizeof(*PL_regcomp_rx->data) + sizeof(void*) * (PL_regcomp_rx->data->count + n - 1),
732 char, struct reg_data);
733 Renew(PL_regcomp_rx->data->what, PL_regcomp_rx->data->count + n, U8);
734 PL_regcomp_rx->data->count += n;
737 Newc(1207, PL_regcomp_rx->data, sizeof(*PL_regcomp_rx->data) + sizeof(void*) * (n - 1),
738 char, struct reg_data);
739 New(1208, PL_regcomp_rx->data->what, n, U8);
740 PL_regcomp_rx->data->count = n;
742 Copy(s, PL_regcomp_rx->data->what + PL_regcomp_rx->data->count - n, n, U8);
743 return PL_regcomp_rx->data->count - n;
747 - pregcomp - compile a regular expression into internal code
749 * We can't allocate space until we know how big the compiled form will be,
750 * but we can't compile it (and thus know how big it is) until we've got a
751 * place to put the code. So we cheat: we compile it twice, once with code
752 * generation turned off and size counting turned on, and once "for real".
753 * This also means that we don't allocate space until we are sure that the
754 * thing really will compile successfully, and we never have to move the
755 * code and thus invalidate pointers into it. (Note that it has to be in
756 * one piece because free() must be able to free it all.) [NB: not true in perl]
758 * Beware that the optimization-preparation code in here knows about some
759 * of the structure of the compiled regexp. [I'll say.]
762 pregcomp(char *exp, char *xend, PMOP *pm)
777 FAIL("NULL regexp argument");
779 if (PL_curcop == &compiling ? (PL_hints & HINT_UTF8) : IN_UTF8)
780 PL_reg_flags |= RF_utf8;
784 PL_regprecomp = savepvn(exp, xend - exp);
785 DEBUG_r(PerlIO_printf(Perl_debug_log, "compiling RE `%*s'\n",
786 xend - exp, PL_regprecomp));
787 PL_regflags = pm->op_pmflags;
791 PL_seen_zerolen = *exp == '^' ? -1 : 0;
795 /* First pass: determine size, legality. */
796 PL_regcomp_parse = exp;
801 PL_regcode = &PL_regdummy;
802 regc((U8)MAGIC, (char*)PL_regcode);
803 if (reg(0, &flags) == NULL) {
804 Safefree(PL_regprecomp);
805 PL_regprecomp = Nullch;
808 DEBUG_r(PerlIO_printf(Perl_debug_log, "size %d ", PL_regsize));
813 char *s = PerlEnv_getenv("TERMCAP_COLORS");
817 PL_colors[0] = s = savepv(s);
821 FAIL("Not enough TABs in TERMCAP_COLORS");
831 PerlIO_printf(Perl_debug_log, "%s%s%s%s",
832 PL_colors[0],PL_colors[1],PL_colors[2],PL_colors[3]);
836 /* Small enough for pointer-storage convention?
837 If extralen==0, this means that we will not need long jumps. */
838 if (PL_regsize >= 0x10000L && PL_extralen)
839 PL_regsize += PL_extralen;
843 /* Allocate space and initialize. */
844 Newc(1001, r, sizeof(regexp) + (unsigned)PL_regsize * sizeof(regnode),
847 FAIL("regexp out of space");
849 r->prelen = xend - exp;
850 r->precomp = PL_regprecomp;
851 r->subbeg = r->subbase = NULL;
852 r->nparens = PL_regnpar - 1; /* set early to validate backrefs */
855 /* Second pass: emit code. */
856 PL_regcomp_parse = exp;
860 PL_regcode = r->program;
861 /* Store the count of eval-groups for security checks: */
862 PL_regcode->next_off = ((PL_seen_evals > U16_MAX) ? U16_MAX : PL_seen_evals);
863 regc((U8)MAGIC, (char*) PL_regcode++);
865 if (reg(0, &flags) == NULL)
868 /* Dig out information for optimizations. */
869 r->reganch = pm->op_pmflags & PMf_COMPILETIME;
870 pm->op_pmflags = PL_regflags;
872 r->reganch |= ROPT_UTF8;
873 r->regstclass = NULL;
874 if (PL_regnaughty >= 10) /* Probably an expensive pattern. */
875 r->reganch |= ROPT_NAUGHTY;
876 scan = r->program + 1; /* First BRANCH. */
878 /* XXXX To minimize changes to RE engine we always allocate
879 3-units-long substrs field. */
880 Newz(1004, r->substrs, 1, struct reg_substr_data);
882 if (OP(scan) != BRANCH) { /* Only one top-level choice. */
885 STRLEN longest_float_length, longest_fixed_length;
887 StructCopy(&zero_scan_data, &data, scan_data_t);
889 /* Skip introductions and multiplicators >= 1. */
890 while ((OP(first) == OPEN && (sawopen = 1)) ||
891 (OP(first) == BRANCH && OP(regnext(first)) != BRANCH) ||
892 (OP(first) == PLUS) ||
893 (OP(first) == MINMOD) ||
894 (regkind[(U8)OP(first)] == CURLY && ARG1(first) > 0) ) {
895 if (OP(first) == PLUS)
898 first += regarglen[(U8)OP(first)];
899 first = NEXTOPER(first);
902 /* Starting-point info. */
904 if (OP(first) == EXACT); /* Empty, get anchored substr later. */
905 else if (strchr(simple+4,OP(first)))
906 r->regstclass = first;
907 else if (regkind[(U8)OP(first)] == BOUND ||
908 regkind[(U8)OP(first)] == NBOUND)
909 r->regstclass = first;
910 else if (regkind[(U8)OP(first)] == BOL) {
911 r->reganch |= (OP(first) == MBOL ? ROPT_ANCH_MBOL: ROPT_ANCH_BOL);
912 first = NEXTOPER(first);
915 else if (OP(first) == GPOS) {
916 r->reganch |= ROPT_ANCH_GPOS;
917 first = NEXTOPER(first);
920 else if ((OP(first) == STAR &&
921 regkind[(U8)OP(NEXTOPER(first))] == ANY) &&
922 !(r->reganch & ROPT_ANCH) )
924 /* turn .* into ^.* with an implied $*=1 */
925 r->reganch |= ROPT_ANCH_BOL | ROPT_IMPLICIT;
926 first = NEXTOPER(first);
929 if (sawplus && (!sawopen || !PL_regsawback))
930 r->reganch |= ROPT_SKIP; /* x+ must match 1st of run */
932 /* Scan is after the zeroth branch, first is atomic matcher. */
933 DEBUG_r(PerlIO_printf(Perl_debug_log, "first at %d\n",
936 * If there's something expensive in the r.e., find the
937 * longest literal string that must appear and make it the
938 * regmust. Resolve ties in favor of later strings, since
939 * the regstart check works with the beginning of the r.e.
940 * and avoiding duplication strengthens checking. Not a
941 * strong reason, but sufficient in the absence of others.
942 * [Now we resolve ties in favor of the earlier string if
943 * it happens that c_offset_min has been invalidated, since the
944 * earlier string may buy us something the later one won't.]
948 data.longest_fixed = newSVpv("",0);
949 data.longest_float = newSVpv("",0);
950 data.last_found = newSVpv("",0);
951 data.longest = &(data.longest_fixed);
954 minlen = study_chunk(&first, &fake, scan + PL_regsize, /* Up to end */
955 &data, SCF_DO_SUBSTR);
956 if ( PL_regnpar == 1 && data.longest == &(data.longest_fixed)
957 && data.last_start_min == 0 && data.last_end > 0
959 && (!(PL_regseen & REG_SEEN_GPOS) || (r->reganch & ROPT_ANCH_GPOS)))
960 r->reganch |= ROPT_CHECK_ALL;
962 SvREFCNT_dec(data.last_found);
964 longest_float_length = CHR_SVLEN(data.longest_float);
965 if (longest_float_length
966 || (data.flags & SF_FL_BEFORE_EOL
967 && (!(data.flags & SF_FL_BEFORE_MEOL)
968 || (PL_regflags & PMf_MULTILINE)))) {
969 if (SvCUR(data.longest_fixed) /* ok to leave SvCUR */
970 && data.offset_fixed == data.offset_float_min)
971 goto remove; /* Like in (a)+. */
973 r->float_substr = data.longest_float;
974 r->float_min_offset = data.offset_float_min;
975 r->float_max_offset = data.offset_float_max;
976 fbm_compile(r->float_substr, 0);
977 BmUSEFUL(r->float_substr) = 100;
978 if (data.flags & SF_FL_BEFORE_EOL /* Cannot have SEOL and MULTI */
979 && (!(data.flags & SF_FL_BEFORE_MEOL)
980 || (PL_regflags & PMf_MULTILINE)))
981 SvTAIL_on(r->float_substr);
985 r->float_substr = Nullsv;
986 SvREFCNT_dec(data.longest_float);
987 longest_float_length = 0;
990 longest_fixed_length = CHR_SVLEN(data.longest_fixed);
991 if (longest_fixed_length
992 || (data.flags & SF_FIX_BEFORE_EOL /* Cannot have SEOL and MULTI */
993 && (!(data.flags & SF_FIX_BEFORE_MEOL)
994 || (PL_regflags & PMf_MULTILINE)))) {
995 r->anchored_substr = data.longest_fixed;
996 r->anchored_offset = data.offset_fixed;
997 fbm_compile(r->anchored_substr, 0);
998 BmUSEFUL(r->anchored_substr) = 100;
999 if (data.flags & SF_FIX_BEFORE_EOL /* Cannot have SEOL and MULTI */
1000 && (!(data.flags & SF_FIX_BEFORE_MEOL)
1001 || (PL_regflags & PMf_MULTILINE)))
1002 SvTAIL_on(r->anchored_substr);
1005 r->anchored_substr = Nullsv;
1006 SvREFCNT_dec(data.longest_fixed);
1007 longest_fixed_length = 0;
1010 /* A temporary algorithm prefers floated substr to fixed one to dig more info. */
1011 if (longest_fixed_length > longest_float_length) {
1012 r->check_substr = r->anchored_substr;
1013 r->check_offset_min = r->check_offset_max = r->anchored_offset;
1014 if (r->reganch & ROPT_ANCH_SINGLE)
1015 r->reganch |= ROPT_NOSCAN;
1018 r->check_substr = r->float_substr;
1019 r->check_offset_min = data.offset_float_min;
1020 r->check_offset_max = data.offset_float_max;
1024 /* Several toplevels. Best we can is to set minlen. */
1027 DEBUG_r(PerlIO_printf(Perl_debug_log, "\n"));
1028 scan = r->program + 1;
1029 minlen = study_chunk(&scan, &fake, scan + PL_regsize, NULL, 0);
1030 r->check_substr = r->anchored_substr = r->float_substr = Nullsv;
1034 if (PL_regseen & REG_SEEN_GPOS)
1035 r->reganch |= ROPT_GPOS_SEEN;
1036 if (PL_regseen & REG_SEEN_LOOKBEHIND)
1037 r->reganch |= ROPT_LOOKBEHIND_SEEN;
1038 if (PL_regseen & REG_SEEN_EVAL)
1039 r->reganch |= ROPT_EVAL_SEEN;
1040 Newz(1002, r->startp, PL_regnpar, char*);
1041 Newz(1002, r->endp, PL_regnpar, char*);
1042 DEBUG_r(regdump(r));
1047 - reg - regular expression, i.e. main body or parenthesized thing
1049 * Caller must absorb opening parenthesis.
1051 * Combining parenthesis handling with the base level of regular expression
1052 * is a trifle forced, but the need to tie the tails of the branches to what
1053 * follows makes it hard to avoid.
1056 reg(I32 paren, I32 *flagp)
1057 /* paren: Parenthesized? 0=top, 1=(, inside: changed to letter. */
1060 register regnode *ret; /* Will be the head of the group. */
1061 register regnode *br;
1062 register regnode *lastbr;
1063 register regnode *ender = 0;
1064 register I32 parno = 0;
1065 I32 flags, oregflags = PL_regflags, have_branch = 0, open = 0;
1068 *flagp = 0; /* Tentatively. */
1070 /* Make an OPEN node, if parenthesized. */
1072 if (*PL_regcomp_parse == '?') {
1073 U16 posflags = 0, negflags = 0;
1074 U16 *flagsp = &posflags;
1077 paren = *PL_regcomp_parse++;
1078 ret = NULL; /* For look-ahead/behind. */
1081 PL_regseen |= REG_SEEN_LOOKBEHIND;
1082 if (*PL_regcomp_parse == '!')
1084 if (*PL_regcomp_parse != '=' && *PL_regcomp_parse != '!')
1095 FAIL2("Sequence (?%c...) not implemented", (int)paren);
1098 while (*PL_regcomp_parse && *PL_regcomp_parse != ')')
1100 if (*PL_regcomp_parse != ')')
1101 FAIL("Sequence (?#... not terminated");
1108 I32 count = 1, n = 0;
1110 char *s = PL_regcomp_parse;
1112 OP_4tree *sop, *rop;
1115 PL_regseen |= REG_SEEN_EVAL;
1116 while (count && (c = *PL_regcomp_parse)) {
1117 if (c == '\\' && PL_regcomp_parse[1])
1125 if (*PL_regcomp_parse != ')')
1126 FAIL("Sequence (?{...}) not terminated or not {}-balanced");
1130 if (PL_regcomp_parse - 1 - s)
1131 sv = newSVpv(s, PL_regcomp_parse - 1 - s);
1133 sv = newSVpv("", 0);
1135 rop = sv_compile_2op(sv, &sop, "re", &av);
1137 n = add_data(3, "nso");
1138 PL_regcomp_rx->data->data[n] = (void*)rop;
1139 PL_regcomp_rx->data->data[n+1] = (void*)av;
1140 PL_regcomp_rx->data->data[n+2] = (void*)sop;
1143 else { /* First pass */
1144 if (PL_reginterp_cnt < ++PL_seen_evals && PL_curcop != &compiling)
1145 /* No compiled RE interpolated, has runtime
1146 components ===> unsafe. */
1147 FAIL("Eval-group not allowed at runtime, use re 'eval'");
1149 FAIL("Eval-group in insecure regular expression");
1153 return reganode(EVAL, n);
1157 if (PL_regcomp_parse[0] == '?') {
1158 if (PL_regcomp_parse[1] == '=' || PL_regcomp_parse[1] == '!'
1159 || PL_regcomp_parse[1] == '<'
1160 || PL_regcomp_parse[1] == '{') { /* Lookahead or eval. */
1163 ret = reg_node(LOGICAL);
1164 regtail(ret, reg(1, &flag));
1168 else if (PL_regcomp_parse[0] >= '1' && PL_regcomp_parse[0] <= '9' ) {
1169 parno = atoi(PL_regcomp_parse++);
1171 while (isDIGIT(*PL_regcomp_parse))
1173 ret = reganode(GROUPP, parno);
1174 if ((c = *nextchar()) != ')')
1175 FAIL2("Switch (?(number%c not recognized", c);
1177 regtail(ret, reganode(IFTHEN, 0));
1178 br = regbranch(&flags, 1);
1180 br = reganode(LONGJMP, 0);
1182 regtail(br, reganode(LONGJMP, 0));
1185 lastbr = reganode(IFTHEN, 0); /* Fake one for optimizer. */
1186 regbranch(&flags, 1);
1187 regtail(ret, lastbr);
1193 FAIL("Switch (?(condition)... contains too many branches");
1194 ender = reg_node(TAIL);
1197 regtail(lastbr, ender);
1198 regtail(NEXTOPER(NEXTOPER(lastbr)), ender);
1201 regtail(ret, ender);
1205 FAIL2("Unknown condition for (?(%.2s", PL_regcomp_parse);
1209 FAIL("Sequence (? incomplete");
1214 while (*PL_regcomp_parse && strchr("iogcmsx", *PL_regcomp_parse)) {
1215 if (*PL_regcomp_parse != 'o')
1216 pmflag(flagsp, *PL_regcomp_parse);
1219 if (*PL_regcomp_parse == '-') {
1224 PL_regflags |= posflags;
1225 PL_regflags &= ~negflags;
1226 if (*PL_regcomp_parse == ':') {
1232 if (*PL_regcomp_parse != ')')
1233 FAIL2("Sequence (?%c...) not recognized", *PL_regcomp_parse);
1242 ret = reganode(OPEN, parno);
1249 /* Pick up the branches, linking them together. */
1250 br = regbranch(&flags, 1);
1253 if (*PL_regcomp_parse == '|') {
1254 if (!SIZE_ONLY && PL_extralen) {
1255 reginsert(BRANCHJ, br);
1258 reginsert(BRANCH, br);
1261 PL_extralen += 1; /* For BRANCHJ-BRANCH. */
1263 else if (paren == ':') {
1264 *flagp |= flags&SIMPLE;
1266 if (open) { /* Starts with OPEN. */
1267 regtail(ret, br); /* OPEN -> first. */
1269 else if (paren != '?') /* Not Conditional */
1273 *flagp |= flags&SPSTART;
1275 while (*PL_regcomp_parse == '|') {
1276 if (!SIZE_ONLY && PL_extralen) {
1277 ender = reganode(LONGJMP,0);
1278 regtail(NEXTOPER(NEXTOPER(lastbr)), ender); /* Append to the previous. */
1281 PL_extralen += 2; /* Account for LONGJMP. */
1283 br = regbranch(&flags, 0);
1286 regtail(lastbr, br); /* BRANCH -> BRANCH. */
1290 *flagp |= flags&SPSTART;
1293 if (have_branch || paren != ':') {
1294 /* Make a closing node, and hook it on the end. */
1297 ender = reg_node(TAIL);
1300 ender = reganode(CLOSE, parno);
1306 *flagp &= ~HASWIDTH;
1309 ender = reg_node(SUCCEED);
1312 ender = reg_node(END);
1315 regtail(lastbr, ender);
1318 /* Hook the tails of the branches to the closing node. */
1319 for (br = ret; br != NULL; br = regnext(br)) {
1320 regoptail(br, ender);
1327 static char parens[] = "=!<,>";
1329 if (paren && (p = strchr(parens, paren))) {
1330 int node = ((p - parens) % 2) ? UNLESSM : IFMATCH;
1331 int flag = (p - parens) > 1;
1334 node = SUSPEND, flag = 0;
1335 reginsert(node,ret);
1337 regtail(ret, reg_node(TAIL));
1341 /* Check for proper termination. */
1342 if (paren && (PL_regcomp_parse >= PL_regxend || *nextchar() != ')')) {
1343 FAIL("unmatched () in regexp");
1345 else if (!paren && PL_regcomp_parse < PL_regxend) {
1346 if (*PL_regcomp_parse == ')') {
1347 FAIL("unmatched () in regexp");
1350 FAIL("junk on end of regexp"); /* "Can't happen". */
1354 PL_regflags = oregflags;
1361 - regbranch - one alternative of an | operator
1363 * Implements the concatenation operator.
1366 regbranch(I32 *flagp, I32 first)
1369 register regnode *ret;
1370 register regnode *chain = NULL;
1371 register regnode *latest;
1372 I32 flags = 0, c = 0;
1377 if (!SIZE_ONLY && PL_extralen)
1378 ret = reganode(BRANCHJ,0);
1380 ret = reg_node(BRANCH);
1383 if (!first && SIZE_ONLY)
1384 PL_extralen += 1; /* BRANCHJ */
1386 *flagp = WORST; /* Tentatively. */
1390 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != '|' && *PL_regcomp_parse != ')') {
1392 latest = regpiece(&flags);
1393 if (latest == NULL) {
1394 if (flags & TRYAGAIN)
1398 else if (ret == NULL)
1400 *flagp |= flags&HASWIDTH;
1401 if (chain == NULL) /* First piece. */
1402 *flagp |= flags&SPSTART;
1405 regtail(chain, latest);
1410 if (chain == NULL) { /* Loop ran zero times. */
1411 chain = reg_node(NOTHING);
1416 *flagp |= flags&SIMPLE;
1423 - regpiece - something followed by possible [*+?]
1425 * Note that the branching code sequences used for ? and the general cases
1426 * of * and + are somewhat optimized: they use the same NOTHING node as
1427 * both the endmarker for their branch list and the body of the last branch.
1428 * It might seem that this node could be dispensed with entirely, but the
1429 * endmarker role is not redundant.
1432 regpiece(I32 *flagp)
1435 register regnode *ret;
1437 register char *next;
1439 char *origparse = PL_regcomp_parse;
1442 I32 max = REG_INFTY;
1444 ret = regatom(&flags);
1446 if (flags & TRYAGAIN)
1451 op = *PL_regcomp_parse;
1453 if (op == '{' && regcurly(PL_regcomp_parse)) {
1454 next = PL_regcomp_parse + 1;
1456 while (isDIGIT(*next) || *next == ',') {
1465 if (*next == '}') { /* got one */
1469 min = atoi(PL_regcomp_parse);
1473 maxpos = PL_regcomp_parse;
1475 if (!max && *maxpos != '0')
1476 max = REG_INFTY; /* meaning "infinity" */
1477 else if (max >= REG_INFTY)
1478 FAIL2("Quantifier in {,} bigger than %d", REG_INFTY - 1);
1479 PL_regcomp_parse = next;
1483 if ((flags&SIMPLE)) {
1484 PL_regnaughty += 2 + PL_regnaughty / 2;
1485 reginsert(CURLY, ret);
1488 PL_regnaughty += 4 + PL_regnaughty; /* compound interest */
1489 regtail(ret, reg_node(WHILEM));
1490 if (!SIZE_ONLY && PL_extralen) {
1491 reginsert(LONGJMP,ret);
1492 reginsert(NOTHING,ret);
1493 NEXT_OFF(ret) = 3; /* Go over LONGJMP. */
1495 reginsert(CURLYX,ret);
1496 if (!SIZE_ONLY && PL_extralen)
1497 NEXT_OFF(ret) = 3; /* Go over NOTHING to LONGJMP. */
1498 regtail(ret, reg_node(NOTHING));
1508 if (max && max < min)
1509 FAIL("Can't do {n,m} with n > m");
1524 #if 0 /* Now runtime fix should be reliable. */
1525 if (!(flags&HASWIDTH) && op != '?')
1526 FAIL("regexp *+ operand could be empty");
1531 *flagp = (op != '+') ? (WORST|SPSTART|HASWIDTH) : (WORST|HASWIDTH);
1533 if (op == '*' && (flags&SIMPLE)) {
1534 reginsert(STAR, ret);
1538 else if (op == '*') {
1542 else if (op == '+' && (flags&SIMPLE)) {
1543 reginsert(PLUS, ret);
1547 else if (op == '+') {
1551 else if (op == '?') {
1556 if (PL_dowarn && !SIZE_ONLY && !(flags&HASWIDTH) && max > 10000) {
1557 warn("%.*s matches null string many times",
1558 PL_regcomp_parse - origparse, origparse);
1561 if (*PL_regcomp_parse == '?') {
1563 reginsert(MINMOD, ret);
1564 regtail(ret, ret + NODE_STEP_REGNODE);
1566 if (ISMULT2(PL_regcomp_parse))
1567 FAIL("nested *?+ in regexp");
1573 - regatom - the lowest level
1575 * Optimization: gobbles an entire sequence of ordinary characters so that
1576 * it can turn them into a single node, which is smaller to store and
1577 * faster to run. Backslashed characters are exceptions, each becoming a
1578 * separate node; the code is simpler that way and it's not worth fixing.
1580 * [Yes, it is worth fixing, some scripts can run twice the speed.]
1586 register regnode *ret = 0;
1589 *flagp = WORST; /* Tentatively. */
1592 switch (*PL_regcomp_parse) {
1596 if (PL_regflags & PMf_MULTILINE)
1597 ret = reg_node(MBOL);
1598 else if (PL_regflags & PMf_SINGLELINE)
1599 ret = reg_node(SBOL);
1601 ret = reg_node(BOL);
1604 if (PL_regcomp_parse[1])
1607 if (PL_regflags & PMf_MULTILINE)
1608 ret = reg_node(MEOL);
1609 else if (PL_regflags & PMf_SINGLELINE)
1610 ret = reg_node(SEOL);
1612 ret = reg_node(EOL);
1617 if (PL_regflags & PMf_SINGLELINE)
1618 ret = reg_node(SANYUTF8);
1620 ret = reg_node(ANYUTF8);
1624 if (PL_regflags & PMf_SINGLELINE)
1625 ret = reg_node(SANY);
1627 ret = reg_node(ANY);
1628 *flagp |= HASWIDTH|SIMPLE;
1634 ret = (UTF ? regclassutf8() : regclass());
1635 *flagp |= HASWIDTH|SIMPLE;
1639 ret = reg(1, &flags);
1641 if (flags & TRYAGAIN)
1645 *flagp |= flags&(HASWIDTH|SPSTART|SIMPLE);
1649 if (flags & TRYAGAIN) {
1653 FAIL2("internal urp in regexp at /%s/", PL_regcomp_parse);
1654 /* Supposed to be caught earlier. */
1657 if (!regcurly(PL_regcomp_parse)) {
1665 FAIL("?+*{} follows nothing in regexp");
1668 switch (*++PL_regcomp_parse) {
1671 ret = reg_node(SBOL);
1676 ret = reg_node(GPOS);
1677 PL_regseen |= REG_SEEN_GPOS;
1682 ret = reg_node(SEOL);
1687 ret = reg_node(EOS);
1689 PL_seen_zerolen++; /* Do not optimize RE away */
1693 ret = reg_node(SANY);
1694 *flagp |= HASWIDTH|SIMPLE;
1698 ret = reg_node(CLUMP);
1701 if (UTF && !PL_utf8_mark)
1702 is_utf8_mark("~"); /* preload table */
1707 ? (LOC ? ALNUMLUTF8 : ALNUMUTF8)
1708 : (LOC ? ALNUML : ALNUM));
1709 *flagp |= HASWIDTH|SIMPLE;
1711 if (UTF && !PL_utf8_alnum)
1712 is_utf8_alnum("a"); /* preload table */
1717 ? (LOC ? NALNUMLUTF8 : NALNUMUTF8)
1718 : (LOC ? NALNUML : NALNUM));
1719 *flagp |= HASWIDTH|SIMPLE;
1721 if (UTF && !PL_utf8_alnum)
1722 is_utf8_alnum("a"); /* preload table */
1728 ? (LOC ? BOUNDLUTF8 : BOUNDUTF8)
1729 : (LOC ? BOUNDL : BOUND));
1732 if (UTF && !PL_utf8_alnum)
1733 is_utf8_alnum("a"); /* preload table */
1739 ? (LOC ? NBOUNDLUTF8 : NBOUNDUTF8)
1740 : (LOC ? NBOUNDL : NBOUND));
1743 if (UTF && !PL_utf8_alnum)
1744 is_utf8_alnum("a"); /* preload table */
1749 ? (LOC ? SPACELUTF8 : SPACEUTF8)
1750 : (LOC ? SPACEL : SPACE));
1751 *flagp |= HASWIDTH|SIMPLE;
1753 if (UTF && !PL_utf8_space)
1754 is_utf8_space(" "); /* preload table */
1759 ? (LOC ? NSPACELUTF8 : NSPACEUTF8)
1760 : (LOC ? NSPACEL : NSPACE));
1761 *flagp |= HASWIDTH|SIMPLE;
1763 if (UTF && !PL_utf8_space)
1764 is_utf8_space(" "); /* preload table */
1767 ret = reg_node(UTF ? DIGITUTF8 : DIGIT);
1768 *flagp |= HASWIDTH|SIMPLE;
1770 if (UTF && !PL_utf8_digit)
1771 is_utf8_digit("1"); /* preload table */
1774 ret = reg_node(UTF ? NDIGITUTF8 : NDIGIT);
1775 *flagp |= HASWIDTH|SIMPLE;
1777 if (UTF && !PL_utf8_digit)
1778 is_utf8_digit("1"); /* preload table */
1790 case '1': case '2': case '3': case '4':
1791 case '5': case '6': case '7': case '8': case '9':
1793 I32 num = atoi(PL_regcomp_parse);
1795 if (num > 9 && num >= PL_regnpar)
1798 if (!SIZE_ONLY && num > PL_regcomp_rx->nparens)
1799 FAIL("reference to nonexistent group");
1802 ? (LOC ? REFFL : REFF)
1805 while (isDIGIT(*PL_regcomp_parse))
1813 if (PL_regcomp_parse >= PL_regxend)
1814 FAIL("trailing \\ in regexp");
1822 if (PL_regflags & PMf_EXTENDED) {
1823 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != '\n') PL_regcomp_parse++;
1824 if (PL_regcomp_parse < PL_regxend)
1840 ? (LOC ? EXACTFL : EXACTF)
1842 s = (char *) OPERAND(ret);
1843 regc(0, s++); /* save spot for len */
1844 for (len = 0, p = PL_regcomp_parse - 1;
1845 len < 127 && p < PL_regxend;
1850 if (PL_regflags & PMf_EXTENDED)
1851 p = regwhite(p, PL_regxend);
1903 char* e = strchr(p, '}');
1906 FAIL("Missing right brace on \\x{}");
1908 ender = scan_hex(p + 1, e - p, &numlen);
1909 if (numlen + len >= 127) { /* numlen is generous */
1916 FAIL("Can't use \\x{} without 'use utf8' declaration");
1919 ender = scan_hex(p, 2, &numlen);
1925 ender = UCHARAT(p++);
1926 ender = toCTRL(ender);
1928 case '0': case '1': case '2': case '3':case '4':
1929 case '5': case '6': case '7': case '8':case '9':
1931 (isDIGIT(p[1]) && atoi(p) >= PL_regnpar) ) {
1932 ender = scan_oct(p, 3, &numlen);
1941 if (p >= PL_regxend)
1942 FAIL("trailing \\ in regexp");
1945 goto normal_default;
1950 if ((*p & 0xc0) == 0xc0 && UTF) {
1951 ender = utf8_to_uv(p, &numlen);
1958 if (PL_regflags & PMf_EXTENDED)
1959 p = regwhite(p, PL_regxend);
1962 ender = toLOWER_LC_uni(ender);
1964 ender = toLOWER_uni(ender);
1966 if (ISMULT2(p)) { /* Back off on ?+*. */
1969 else if (ender >= 0x80 && UTF) {
1970 reguni(ender, s, &numlen);
1980 if (ender >= 0x80 && UTF) {
1981 reguni(ender, s, &numlen);
1989 PL_regcomp_parse = p - 1;
1992 FAIL("internal disaster in regexp");
1998 *OPERAND(ret) = len;
2001 PL_regsize += (len + 2 + sizeof(regnode) - 1) / sizeof(regnode);
2004 PL_regcode += (len + 2 + sizeof(regnode) - 1) / sizeof(regnode);
2014 regwhite(char *p, char *e)
2019 else if (*p == '#') {
2022 } while (p < e && *p != '\n');
2034 register char *opnd, *s;
2036 register I32 lastvalue = 1234;
2037 register I32 range = 0;
2038 register regnode *ret;
2042 s = opnd = (char *) OPERAND(PL_regcode);
2043 ret = reg_node(ANYOF);
2044 for (value = 0; value < 33; value++)
2046 if (*PL_regcomp_parse == '^') { /* Complement of range. */
2050 *opnd |= ANYOF_INVERT;
2053 PL_regcode += ANY_SKIP;
2055 *opnd |= ANYOF_FOLD;
2057 *opnd |= ANYOF_LOCALE;
2060 PL_regsize += ANY_SKIP;
2062 if (*PL_regcomp_parse == ']' || *PL_regcomp_parse == '-')
2063 goto skipcond; /* allow 1st char to be ] or - */
2064 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != ']') {
2066 value = UCHARAT(PL_regcomp_parse++);
2067 if (value == '[' && PL_regcomp_parse + 1 < PL_regxend &&
2068 /* I smell either [: or [= or [. -- POSIX has been here, right? */
2069 (*PL_regcomp_parse == ':' || *PL_regcomp_parse == '=' || *PL_regcomp_parse == '.')) {
2070 char posixccc = *PL_regcomp_parse;
2071 char* posixccs = PL_regcomp_parse++;
2073 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != posixccc)
2075 if (PL_regcomp_parse == PL_regxend)
2076 /* Grandfather lone [:, [=, [. */
2077 PL_regcomp_parse = posixccs;
2079 PL_regcomp_parse++; /* skip over the posixccc */
2080 if (*PL_regcomp_parse == ']') {
2081 /* Not Implemented Yet.
2082 * (POSIX Extended Character Classes, that is)
2083 * The text between e.g. [: and :] would start
2084 * at posixccs + 1 and stop at regcomp_parse - 2. */
2085 if (dowarn && !SIZE_ONLY)
2086 warn("Character class syntax [%c %c] is reserved for future extensions", posixccc, posixccc);
2087 PL_regcomp_parse++; /* skip over the ending ] */
2091 if (value == '\\') {
2092 value = UCHARAT(PL_regcomp_parse++);
2097 *opnd |= ANYOF_ALNUML;
2099 for (value = 0; value < 256; value++)
2101 ANYOF_SET(opnd, value);
2109 *opnd |= ANYOF_NALNUML;
2111 for (value = 0; value < 256; value++)
2112 if (!isALNUM(value))
2113 ANYOF_SET(opnd, value);
2121 *opnd |= ANYOF_SPACEL;
2123 for (value = 0; value < 256; value++)
2125 ANYOF_SET(opnd, value);
2133 *opnd |= ANYOF_NSPACEL;
2135 for (value = 0; value < 256; value++)
2136 if (!isSPACE(value))
2137 ANYOF_SET(opnd, value);
2144 for (value = '0'; value <= '9'; value++)
2145 ANYOF_SET(opnd, value);
2151 for (value = 0; value < '0'; value++)
2152 ANYOF_SET(opnd, value);
2153 for (value = '9' + 1; value < 256; value++)
2154 ANYOF_SET(opnd, value);
2180 value = scan_hex(PL_regcomp_parse, 2, &numlen);
2181 PL_regcomp_parse += numlen;
2184 value = UCHARAT(PL_regcomp_parse++);
2185 value = toCTRL(value);
2187 case '0': case '1': case '2': case '3': case '4':
2188 case '5': case '6': case '7': case '8': case '9':
2189 value = scan_oct(--PL_regcomp_parse, 3, &numlen);
2190 PL_regcomp_parse += numlen;
2195 if (lastvalue > value)
2196 FAIL("invalid [] range in regexp");
2201 if (*PL_regcomp_parse == '-' && PL_regcomp_parse+1 < PL_regxend &&
2202 PL_regcomp_parse[1] != ']') {
2205 continue; /* do it next time */
2209 for ( ; lastvalue <= value; lastvalue++)
2210 ANYOF_SET(opnd, lastvalue);
2214 if (*PL_regcomp_parse != ']')
2215 FAIL("unmatched [] in regexp");
2217 /* optimize case-insensitive simple patterns (e.g. /[a-z]/i) */
2218 if (!SIZE_ONLY && (*opnd & (0xFF ^ ANYOF_INVERT)) == ANYOF_FOLD) {
2219 for (value = 0; value < 256; ++value) {
2220 if (ANYOF_TEST(opnd, value)) {
2221 I32 cf = fold[value];
2222 ANYOF_SET(opnd, cf);
2225 *opnd &= ~ANYOF_FOLD;
2227 /* optimize inverted simple patterns (e.g. [^a-z]) */
2228 if (!SIZE_ONLY && (*opnd & 0xFF) == ANYOF_INVERT) {
2229 for (value = 0; value < 32; ++value)
2230 opnd[1 + value] ^= 0xFF;
2239 register char *opnd, *e;
2241 register U32 lastvalue = 123456;
2242 register I32 range = 0;
2243 register regnode *ret;
2249 if (*PL_regcomp_parse == '^') { /* Complement of range. */
2253 flags |= ANYOF_INVERT;
2257 flags |= ANYOF_FOLD;
2259 flags |= ANYOF_LOCALE;
2260 listsv = newSVpv("# comment\n",0);
2263 if (*PL_regcomp_parse == ']' || *PL_regcomp_parse == '-')
2264 goto skipcond; /* allow 1st char to be ] or - */
2266 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != ']') {
2268 value = utf8_to_uv(PL_regcomp_parse, &numlen);
2269 PL_regcomp_parse += numlen;
2271 if (value == '[' && PL_regcomp_parse + 1 < PL_regxend &&
2272 /* I smell either [: or [= or [. -- POSIX has been here, right? */
2273 (*PL_regcomp_parse == ':' || *PL_regcomp_parse == '=' || *PL_regcomp_parse == '.')) {
2274 char posixccc = *PL_regcomp_parse;
2275 char* posixccs = PL_regcomp_parse++;
2277 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != posixccc)
2279 if (PL_regcomp_parse == PL_regxend)
2280 /* Grandfather lone [:, [=, [. */
2281 PL_regcomp_parse = posixccs;
2283 PL_regcomp_parse++; /* skip over the posixccc */
2284 if (*PL_regcomp_parse == ']') {
2285 /* Not Implemented Yet.
2286 * (POSIX Extended Character Classes, that is)
2287 * The text between e.g. [: and :] would start
2288 * at posixccs + 1 and stop at regcomp_parse - 2. */
2289 if (dowarn && !SIZE_ONLY)
2290 warn("Character class syntax [%c %c] is reserved for future extensions", posixccc, posixccc);
2291 PL_regcomp_parse++; /* skip over the ending ] */
2296 if (value == '\\') {
2297 value = utf8_to_uv(PL_regcomp_parse, &numlen);
2298 PL_regcomp_parse += numlen;
2303 flags |= ANYOF_ALNUML;
2305 sv_catpvf(listsv, "+utf8::IsAlnum\n");
2312 flags |= ANYOF_NALNUML;
2315 "-utf8::IsAlpha\n-utf8::IsDigit\n0000\t%04x\n%04x\tffff\n",
2324 flags |= ANYOF_SPACEL;
2325 sv_catpvf(listsv, "+utf8::IsSpace\n");
2334 flags |= ANYOF_NSPACEL;
2336 "!utf8::IsSpace\n");
2344 sv_catpvf(listsv, "+utf8::IsDigit\n");
2351 "!utf8::IsDigit\n");
2357 if (*PL_regcomp_parse == '{') {
2358 e = strchr(PL_regcomp_parse++, '}');
2360 FAIL("Missing right brace on \\p{}");
2361 n = e - PL_regcomp_parse;
2364 e = PL_regcomp_parse;
2369 sv_catpvf(listsv, "+utf8::%.*s\n", n, PL_regcomp_parse);
2372 "!utf8::%.*s\n", n, PL_regcomp_parse);
2374 PL_regcomp_parse = e + 1;
2399 if (*PL_regcomp_parse == '{') {
2400 e = strchr(PL_regcomp_parse++, '}');
2402 FAIL("Missing right brace on \\x{}");
2403 value = scan_hex(PL_regcomp_parse + 1, e - PL_regcomp_parse, &numlen);
2404 PL_regcomp_parse = e + 1;
2407 value = scan_hex(PL_regcomp_parse, 2, &numlen);
2408 PL_regcomp_parse += numlen;
2412 value = UCHARAT(PL_regcomp_parse++);
2413 value = toCTRL(value);
2415 case '0': case '1': case '2': case '3': case '4':
2416 case '5': case '6': case '7': case '8': case '9':
2417 value = scan_oct(--PL_regcomp_parse, 3, &numlen);
2418 PL_regcomp_parse += numlen;
2423 if (lastvalue > value)
2424 FAIL("invalid [] range in regexp");
2426 sv_catpvf(listsv, "%04x\t%04x\n", lastvalue, value);
2432 if (*PL_regcomp_parse == '-' && PL_regcomp_parse+1 < PL_regxend &&
2433 PL_regcomp_parse[1] != ']') {
2436 continue; /* do it next time */
2439 sv_catpvf(listsv, "%04x\n", value);
2442 if (*PL_regcomp_parse != ']')
2443 FAIL("unmatched [] in regexp");
2446 ret = reganode(ANYOFUTF8, 0);
2449 SV *rv = swash_init("utf8", "", listsv, 1, 0);
2450 SvREFCNT_dec(listsv);
2451 n = add_data(1,"s");
2452 PL_regcomp_rx->data->data[n] = (void*)rv;
2453 ARG1_SET(ret, flags);
2464 char* retval = PL_regcomp_parse++;
2467 if (*PL_regcomp_parse == '(' && PL_regcomp_parse[1] == '?' &&
2468 PL_regcomp_parse[2] == '#') {
2469 while (*PL_regcomp_parse && *PL_regcomp_parse != ')')
2474 if (PL_regflags & PMf_EXTENDED) {
2475 if (isSPACE(*PL_regcomp_parse)) {
2479 else if (*PL_regcomp_parse == '#') {
2480 while (*PL_regcomp_parse && *PL_regcomp_parse != '\n')
2491 - reg_node - emit a node
2493 STATIC regnode * /* Location. */
2497 register regnode *ret;
2498 register regnode *ptr;
2502 SIZE_ALIGN(PL_regsize);
2507 NODE_ALIGN_FILL(ret);
2509 FILL_ADVANCE_NODE(ptr, op);
2516 - reganode - emit a node with an argument
2518 STATIC regnode * /* Location. */
2519 reganode(U8 op, U32 arg)
2522 register regnode *ret;
2523 register regnode *ptr;
2527 SIZE_ALIGN(PL_regsize);
2532 NODE_ALIGN_FILL(ret);
2534 FILL_ADVANCE_NODE_ARG(ptr, op, arg);
2541 - regc - emit (if appropriate) a Unicode character
2544 reguni(UV uv, char* s, I32* lenp)
2548 *lenp = uv_to_utf8(tmpbuf, uv) - tmpbuf;
2551 *lenp = uv_to_utf8(s, uv) - s;
2556 - regc - emit (if appropriate) a byte of code
2567 - reginsert - insert an operator in front of already-emitted operand
2569 * Means relocating the operand.
2572 reginsert(U8 op, regnode *opnd)
2575 register regnode *src;
2576 register regnode *dst;
2577 register regnode *place;
2578 register int offset = regarglen[(U8)op];
2580 /* (regkind[(U8)op] == CURLY ? EXTRA_STEP_2ARGS : 0); */
2583 PL_regsize += NODE_STEP_REGNODE + offset;
2588 PL_regcode += NODE_STEP_REGNODE + offset;
2591 StructCopy(--src, --dst, regnode);
2593 place = opnd; /* Op node, where operand used to be. */
2594 src = NEXTOPER(place);
2595 FILL_ADVANCE_NODE(place, op);
2596 Zero(src, offset, regnode);
2600 - regtail - set the next-pointer at the end of a node chain of p to val.
2603 regtail(regnode *p, regnode *val)
2606 register regnode *scan;
2607 register regnode *temp;
2608 register I32 offset;
2613 /* Find last node. */
2616 temp = regnext(scan);
2622 if (reg_off_by_arg[OP(scan)]) {
2623 ARG_SET(scan, val - scan);
2626 NEXT_OFF(scan) = val - scan;
2631 - regoptail - regtail on operand of first argument; nop if operandless
2634 regoptail(regnode *p, regnode *val)
2637 /* "Operandless" and "op != BRANCH" are synonymous in practice. */
2638 if (p == NULL || SIZE_ONLY)
2640 if (regkind[(U8)OP(p)] == BRANCH) {
2641 regtail(NEXTOPER(p), val);
2643 else if ( regkind[(U8)OP(p)] == BRANCHJ) {
2644 regtail(NEXTOPER(NEXTOPER(p)), val);
2651 - regcurly - a little FSA that accepts {\d+,?\d*}
2654 regcurly(register char *s)
2673 dumpuntil(regnode *start, regnode *node, regnode *last, SV* sv, I32 l)
2676 register char op = EXACT; /* Arbitrary non-END op. */
2677 register regnode *next, *onode;
2679 while (op != END && (!last || node < last)) {
2680 /* While that wasn't END last time... */
2686 next = regnext(node);
2688 if (OP(node) == OPTIMIZED)
2691 PerlIO_printf(Perl_debug_log, "%4d:%*s%s", node - start,
2692 2*l + 1, "", SvPVX(sv));
2693 if (next == NULL) /* Next ptr. */
2694 PerlIO_printf(Perl_debug_log, "(0)");
2696 PerlIO_printf(Perl_debug_log, "(%d)", next - start);
2697 (void)PerlIO_putc(Perl_debug_log, '\n');
2699 if (regkind[(U8)op] == BRANCHJ) {
2700 register regnode *nnode = (OP(next) == LONGJMP
2703 if (last && nnode > last)
2705 node = dumpuntil(start, NEXTOPER(NEXTOPER(node)), nnode, sv, l + 1);
2707 else if (regkind[(U8)op] == BRANCH) {
2708 node = dumpuntil(start, NEXTOPER(node), next, sv, l + 1);
2710 else if ( op == CURLY) { /* `next' might be very big: optimizer */
2711 node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
2712 NEXTOPER(node) + EXTRA_STEP_2ARGS + 1, sv, l + 1);
2714 else if (regkind[(U8)op] == CURLY && op != CURLYX) {
2715 node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
2718 else if ( op == PLUS || op == STAR) {
2719 node = dumpuntil(start, NEXTOPER(node), NEXTOPER(node) + 1, sv, l + 1);
2721 else if (op == ANYOF) {
2722 node = NEXTOPER(node);
2725 else if (regkind[(U8)op] == EXACT) {
2726 /* Literal string, where present. */
2727 node += ((*OPERAND(node)) + 2 + sizeof(regnode) - 1) / sizeof(regnode);
2728 node = NEXTOPER(node);
2731 node = NEXTOPER(node);
2732 node += regarglen[(U8)op];
2734 if (op == CURLYX || op == OPEN)
2736 else if (op == WHILEM)
2739 #endif /* DEBUGGING */
2744 - regdump - dump a regexp onto Perl_debug_log in vaguely comprehensible form
2751 SV *sv = sv_newmortal();
2753 (void)dumpuntil(r->program, r->program + 1, NULL, sv, 0);
2755 /* Header fields of interest. */
2756 if (r->anchored_substr)
2757 PerlIO_printf(Perl_debug_log, "anchored `%s%s%s'%s at %d ",
2759 SvPVX(r->anchored_substr),
2761 SvTAIL(r->anchored_substr) ? "$" : "",
2762 r->anchored_offset);
2763 if (r->float_substr)
2764 PerlIO_printf(Perl_debug_log, "floating `%s%s%s'%s at %d..%u ",
2766 SvPVX(r->float_substr),
2768 SvTAIL(r->float_substr) ? "$" : "",
2769 r->float_min_offset, r->float_max_offset);
2770 if (r->check_substr)
2771 PerlIO_printf(Perl_debug_log,
2772 r->check_substr == r->float_substr
2773 ? "(checking floating" : "(checking anchored");
2774 if (r->reganch & ROPT_NOSCAN)
2775 PerlIO_printf(Perl_debug_log, " noscan");
2776 if (r->reganch & ROPT_CHECK_ALL)
2777 PerlIO_printf(Perl_debug_log, " isall");
2778 if (r->check_substr)
2779 PerlIO_printf(Perl_debug_log, ") ");
2781 if (r->regstclass) {
2782 regprop(sv, r->regstclass);
2783 PerlIO_printf(Perl_debug_log, "stclass `%s' ", SvPVX(sv));
2785 if (r->reganch & ROPT_ANCH) {
2786 PerlIO_printf(Perl_debug_log, "anchored");
2787 if (r->reganch & ROPT_ANCH_BOL)
2788 PerlIO_printf(Perl_debug_log, "(BOL)");
2789 if (r->reganch & ROPT_ANCH_MBOL)
2790 PerlIO_printf(Perl_debug_log, "(MBOL)");
2791 if (r->reganch & ROPT_ANCH_GPOS)
2792 PerlIO_printf(Perl_debug_log, "(GPOS)");
2793 PerlIO_putc(Perl_debug_log, ' ');
2795 if (r->reganch & ROPT_GPOS_SEEN)
2796 PerlIO_printf(Perl_debug_log, "GPOS ");
2797 if (r->reganch & ROPT_SKIP)
2798 PerlIO_printf(Perl_debug_log, "plus ");
2799 if (r->reganch & ROPT_IMPLICIT)
2800 PerlIO_printf(Perl_debug_log, "implicit ");
2801 PerlIO_printf(Perl_debug_log, "minlen %ld ", (long) r->minlen);
2802 if (r->reganch & ROPT_EVAL_SEEN)
2803 PerlIO_printf(Perl_debug_log, "with eval ");
2804 PerlIO_printf(Perl_debug_log, "\n");
2805 #endif /* DEBUGGING */
2809 - regprop - printable representation of opcode
2812 regprop(SV *sv, regnode *o)
2816 register char *p = 0;
2818 sv_setpvn(sv, "", 0);
2863 sv_catpvf(sv, "EXACT <%s%s%s>", PL_colors[0], OPERAND(o) + 1, PL_colors[1]);
2866 sv_catpvf(sv, "EXACTF <%s%s%s>", PL_colors[0], OPERAND(o) + 1, PL_colors[1]);
2869 sv_catpvf(sv, "EXACTFL <%s%s%s>", PL_colors[0], OPERAND(o) + 1, PL_colors[1]);
2896 sv_catpvf(sv, "CURLY {%d,%d}", ARG1(o), ARG2(o));
2899 sv_catpvf(sv, "CURLYM[%d] {%d,%d}", o->flags, ARG1(o), ARG2(o));
2902 sv_catpvf(sv, "CURLYN[%d] {%d,%d}", o->flags, ARG1(o), ARG2(o));
2905 sv_catpvf(sv, "CURLYX {%d,%d}", ARG1(o), ARG2(o));
2908 sv_catpvf(sv, "REF%d", ARG(o));
2911 sv_catpvf(sv, "REFF%d", ARG(o));
2914 sv_catpvf(sv, "REFFL%d", ARG(o));
2917 sv_catpvf(sv, "OPEN%d", ARG(o));
2920 sv_catpvf(sv, "CLOSE%d", ARG(o));
2936 sv_catpvf(sv, "UNLESSM[-%d]", o->flags);
2939 sv_catpvf(sv, "IFMATCH[-%d]", o->flags);
2990 sv_catpvf(sv, "GROUPP%d", ARG(o));
3005 FAIL("corrupted regexp opcode");
3009 #endif /* DEBUGGING */
3013 pregfree(struct regexp *r)
3016 if (!r || (--r->refcnt > 0))
3019 Safefree(r->precomp);
3021 Safefree(r->subbase);
3023 if (r->anchored_substr)
3024 SvREFCNT_dec(r->anchored_substr);
3025 if (r->float_substr)
3026 SvREFCNT_dec(r->float_substr);
3027 Safefree(r->substrs);
3030 int n = r->data->count;
3032 switch (r->data->what[n]) {
3034 SvREFCNT_dec((SV*)r->data->data[n]);
3037 op_free((OP_4tree*)r->data->data[n]);
3042 FAIL2("panic: regfree data code '%c'", r->data->what[n]);
3045 Safefree(r->data->what);
3048 Safefree(r->startp);
3054 - regnext - dig the "next" pointer out of a node
3056 * [Note, when REGALIGN is defined there are two places in regmatch()
3057 * that bypass this code for speed.]
3060 regnext(register regnode *p)
3063 register I32 offset;
3065 if (p == &PL_regdummy)
3068 offset = (reg_off_by_arg[OP(p)] ? ARG(p) : NEXT_OFF(p));
3076 re_croak2(const char* pat1,const char* pat2,...)
3079 STRLEN l1 = strlen(pat1);
3080 STRLEN l2 = strlen(pat2);
3088 Copy(pat1, buf, l1 , char);
3089 Copy(pat2, buf + l1, l2 , char);
3090 buf[l1 + l2] = '\n';
3091 buf[l1 + l2 + 1] = '\0';
3092 va_start(args, pat2);
3093 message = mess(buf, &args);
3095 l1 = strlen(message);
3098 Copy(message, buf, l1 , char);
3099 buf[l1] = '\0'; /* Overwrite \n */
3103 /* XXX Here's a total kludge. But we need to re-enter for swash routines. */
3106 save_re_context(void)
3109 SAVEPPTR(PL_regprecomp); /* uncompiled string. */
3110 SAVEI32(PL_regnpar); /* () count. */
3111 SAVEI32(PL_regsize); /* Code size. */
3112 SAVEI16(PL_regflags); /* are we folding, multilining? */
3113 SAVEPPTR(PL_reginput); /* String-input pointer. */
3114 SAVEPPTR(PL_regbol); /* Beginning of input, for ^ check. */
3115 SAVEPPTR(PL_regeol); /* End of input, for $ check. */
3116 SAVESPTR(PL_regstartp); /* Pointer to startp array. */
3117 SAVESPTR(PL_regendp); /* Ditto for endp. */
3118 SAVESPTR(PL_reglastparen); /* Similarly for lastparen. */
3119 SAVEPPTR(PL_regtill); /* How far we are required to go. */
3120 SAVEI32(PL_regprev); /* char before regbol, \n if none */
3121 SAVESPTR(PL_reg_start_tmp); /* from regexec.c */
3122 PL_reg_start_tmp = 0;
3123 SAVEFREEPV(PL_reg_start_tmp);
3124 SAVEI32(PL_reg_start_tmpl); /* from regexec.c */
3125 PL_reg_start_tmpl = 0;
3126 SAVESPTR(PL_regdata);
3127 SAVEI32(PL_reg_flags); /* from regexec.c */
3128 SAVEI32(PL_reg_eval_set); /* from regexec.c */
3129 SAVEI32(PL_regnarrate); /* from regexec.c */
3130 SAVESPTR(PL_regprogram); /* from regexec.c */
3131 SAVEINT(PL_regindent); /* from regexec.c */
3132 SAVESPTR(PL_regcc); /* from regexec.c */
3133 SAVESPTR(PL_curcop);
3134 SAVESPTR(PL_regcomp_rx); /* from regcomp.c */
3135 SAVEI32(PL_regseen); /* from regcomp.c */
3136 SAVEI32(PL_regsawback); /* Did we see \1, ...? */
3137 SAVEI32(PL_regnaughty); /* How bad is this pattern? */
3138 SAVESPTR(PL_regcode); /* Code-emit pointer; ®dummy = don't */
3139 SAVEPPTR(PL_regxend); /* End of input for compile */
3140 SAVEPPTR(PL_regcomp_parse); /* Input-scan pointer. */