5 * "A fair jaw-cracker dwarf-language must be." --Samwise Gamgee
8 /* NOTE: this is derived from Henry Spencer's regexp code, and should not
9 * confused with the original package (see point 3 below). Thanks, Henry!
12 /* Additional note: this code is very heavily munged from Henry's version
13 * in places. In some spots I've traded clarity for efficiency, so don't
14 * blame Henry for some of the lack of readability.
17 /* The names of the functions have been changed from regcomp and
18 * regexec to pregcomp and pregexec in order to avoid conflicts
19 * with the POSIX routines of the same names.
22 #ifdef PERL_EXT_RE_BUILD
23 /* need to replace pregcomp et al, so enable that */
24 # ifndef PERL_IN_XSUB_RE
25 # define PERL_IN_XSUB_RE
27 /* need access to debugger hooks */
33 #ifdef PERL_IN_XSUB_RE
34 /* We *really* need to overwrite these symbols: */
35 # define Perl_pregcomp my_regcomp
36 # define Perl_regdump my_regdump
37 # define Perl_regprop my_regprop
38 /* *These* symbols are masked to allow static link. */
39 # define Perl_pregfree my_regfree
40 # define Perl_regnext my_regnext
41 # define Perl_save_re_context my_save_re_context
42 # define Perl_reginitcolors my_reginitcolors
47 * pregcomp and pregexec -- regsub and regerror are not used in perl
49 * Copyright (c) 1986 by University of Toronto.
50 * Written by Henry Spencer. Not derived from licensed software.
52 * Permission is granted to anyone to use this software for any
53 * purpose on any computer system, and to redistribute it freely,
54 * subject to the following restrictions:
56 * 1. The author is not responsible for the consequences of use of
57 * this software, no matter how awful, even if they arise
60 * 2. The origin of this software must not be misrepresented, either
61 * by explicit claim or by omission.
63 * 3. Altered versions must be plainly marked as such, and must not
64 * be misrepresented as being the original software.
67 **** Alterations to Henry's code are...
69 **** Copyright (c) 1991-1999, Larry Wall
71 **** You may distribute under the terms of either the GNU General Public
72 **** License or the Artistic License, as specified in the README file.
75 * Beware that some of this code is subtly aware of the way operator
76 * precedence is structured in regular expressions. Serious changes in
77 * regular-expression syntax might require a total rethink.
80 #define PERL_IN_REGCOMP_C
83 #ifndef PERL_IN_XSUB_RE
95 # if defined(BUGGY_MSC6)
96 /* MSC 6.00A breaks on op/regexp.t test 85 unless we turn this off */
97 # pragma optimize("a",off)
98 /* But MSC 6.00A is happy with 'w', for aliases only across function calls*/
99 # pragma optimize("w",on )
100 # endif /* BUGGY_MSC6 */
104 #define STATIC static
107 #define ISMULT1(c) ((c) == '*' || (c) == '+' || (c) == '?')
108 #define ISMULT2(s) ((*s) == '*' || (*s) == '+' || (*s) == '?' || \
109 ((*s) == '{' && regcurly(s)))
111 #define PERL_META "^$.[()|?+*\\"
113 #define META "^$.[()|?+*\\"
117 #undef SPSTART /* dratted cpp namespace... */
120 * Flags to be passed up and down.
122 #define WORST 0 /* Worst case. */
123 #define HASWIDTH 0x1 /* Known to match non-null strings. */
124 #define SIMPLE 0x2 /* Simple enough to be STAR/PLUS operand. */
125 #define SPSTART 0x4 /* Starts with * or +. */
126 #define TRYAGAIN 0x8 /* Weeded out a declaration. */
129 * Forward declarations for pregcomp()'s friends.
132 static scan_data_t zero_scan_data = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
135 #define SF_BEFORE_EOL (SF_BEFORE_SEOL|SF_BEFORE_MEOL)
136 #define SF_BEFORE_SEOL 0x1
137 #define SF_BEFORE_MEOL 0x2
138 #define SF_FIX_BEFORE_EOL (SF_FIX_BEFORE_SEOL|SF_FIX_BEFORE_MEOL)
139 #define SF_FL_BEFORE_EOL (SF_FL_BEFORE_SEOL|SF_FL_BEFORE_MEOL)
142 # define SF_FIX_SHIFT_EOL (0+2)
143 # define SF_FL_SHIFT_EOL (0+4)
145 # define SF_FIX_SHIFT_EOL (+2)
146 # define SF_FL_SHIFT_EOL (+4)
149 #define SF_FIX_BEFORE_SEOL (SF_BEFORE_SEOL << SF_FIX_SHIFT_EOL)
150 #define SF_FIX_BEFORE_MEOL (SF_BEFORE_MEOL << SF_FIX_SHIFT_EOL)
152 #define SF_FL_BEFORE_SEOL (SF_BEFORE_SEOL << SF_FL_SHIFT_EOL)
153 #define SF_FL_BEFORE_MEOL (SF_BEFORE_MEOL << SF_FL_SHIFT_EOL) /* 0x20 */
154 #define SF_IS_INF 0x40
155 #define SF_HAS_PAR 0x80
156 #define SF_IN_PAR 0x100
157 #define SF_HAS_EVAL 0x200
158 #define SCF_DO_SUBSTR 0x400
161 #define UTF (PL_reg_flags & RF_utf8)
162 #define LOC (PL_regflags & PMf_LOCALE)
163 #define FOLD (PL_regflags & PMf_FOLD)
165 #define CHR_SVLEN(sv) (UTF ? sv_len_utf8(sv) : SvCUR(sv))
166 #define CHR_DIST(a,b) (UTF ? utf8_distance(a,b) : a - b)
169 S_clear_re(pTHX_ void *r)
171 ReREFCNT_dec((regexp *)r);
175 S_scan_commit(pTHX_ scan_data_t *data)
178 STRLEN l = CHR_SVLEN(data->last_found);
179 STRLEN old_l = CHR_SVLEN(*data->longest);
181 if ((l >= old_l) && ((l > old_l) || (data->flags & SF_BEFORE_EOL))) {
182 sv_setsv(*data->longest, data->last_found);
183 if (*data->longest == data->longest_fixed) {
184 data->offset_fixed = l ? data->last_start_min : data->pos_min;
185 if (data->flags & SF_BEFORE_EOL)
187 |= ((data->flags & SF_BEFORE_EOL) << SF_FIX_SHIFT_EOL);
189 data->flags &= ~SF_FIX_BEFORE_EOL;
192 data->offset_float_min = l ? data->last_start_min : data->pos_min;
193 data->offset_float_max = (l
194 ? data->last_start_max
195 : data->pos_min + data->pos_delta);
196 if (data->flags & SF_BEFORE_EOL)
198 |= ((data->flags & SF_BEFORE_EOL) << SF_FL_SHIFT_EOL);
200 data->flags &= ~SF_FL_BEFORE_EOL;
203 SvCUR_set(data->last_found, 0);
205 data->flags &= ~SF_BEFORE_EOL;
208 /* Stops at toplevel WHILEM as well as at `last'. At end *scanp is set
209 to the position after last scanned or to NULL. */
212 S_study_chunk(pTHX_ regnode **scanp, I32 *deltap, regnode *last, scan_data_t *data, U32 flags)
213 /* scanp: Start here (read-write). */
214 /* deltap: Write maxlen-minlen here. */
215 /* last: Stop before this one. */
218 I32 min = 0, pars = 0, code;
219 regnode *scan = *scanp, *next;
221 int is_inf = (flags & SCF_DO_SUBSTR) && (data->flags & SF_IS_INF);
222 int is_inf_internal = 0; /* The studied chunk is infinite */
223 I32 is_par = OP(scan) == OPEN ? ARG(scan) : 0;
224 scan_data_t data_fake;
226 while (scan && OP(scan) != END && scan < last) {
227 /* Peephole optimizer: */
229 if (PL_regkind[(U8)OP(scan)] == EXACT) {
230 regnode *n = regnext(scan);
233 regnode *stop = scan;
236 next = scan + (*OPERAND(scan) + 2 - 1)/sizeof(regnode) + 2;
237 /* Skip NOTHING, merge EXACT*. */
239 ( PL_regkind[(U8)OP(n)] == NOTHING ||
240 (stringok && (OP(n) == OP(scan))))
242 && NEXT_OFF(scan) + NEXT_OFF(n) < I16_MAX) {
243 if (OP(n) == TAIL || n > next)
245 if (PL_regkind[(U8)OP(n)] == NOTHING) {
246 NEXT_OFF(scan) += NEXT_OFF(n);
247 next = n + NODE_STEP_REGNODE;
255 int oldl = *OPERAND(scan);
256 regnode *nnext = regnext(n);
258 if (oldl + *OPERAND(n) > U8_MAX)
260 NEXT_OFF(scan) += NEXT_OFF(n);
261 *OPERAND(scan) += *OPERAND(n);
262 next = n + (*OPERAND(n) + 2 - 1)/sizeof(regnode) + 2;
263 /* Now we can overwrite *n : */
264 Move(OPERAND(n) + 1, OPERAND(scan) + oldl + 1,
265 *OPERAND(n) + 1, char);
275 n = scan + (*OPERAND(scan) + 2 - 1)/sizeof(regnode) + 2;
277 /* Purify reports a benign UMR here sometimes, because we
278 * don't initialize the OP() slot of a node when that node
279 * is occupied by just the trailing null of the string in
281 if (PL_regkind[(U8)OP(n)] != NOTHING || OP(n) == NOTHING) {
290 if (OP(scan) != CURLYX) {
291 int max = (reg_off_by_arg[OP(scan)]
293 /* I32 may be smaller than U16 on CRAYs! */
294 : (I32_MAX < U16_MAX ? I32_MAX : U16_MAX));
295 int off = (reg_off_by_arg[OP(scan)] ? ARG(scan) : NEXT_OFF(scan));
299 /* Skip NOTHING and LONGJMP. */
300 while ((n = regnext(n))
301 && ((PL_regkind[(U8)OP(n)] == NOTHING && (noff = NEXT_OFF(n)))
302 || ((OP(n) == LONGJMP) && (noff = ARG(n))))
305 if (reg_off_by_arg[OP(scan)])
308 NEXT_OFF(scan) = off;
310 if (OP(scan) == BRANCH || OP(scan) == BRANCHJ
311 || OP(scan) == IFTHEN || OP(scan) == SUSPEND) {
312 next = regnext(scan);
315 if (OP(next) == code || code == IFTHEN || code == SUSPEND) {
316 I32 max1 = 0, min1 = I32_MAX, num = 0;
318 if (flags & SCF_DO_SUBSTR)
320 while (OP(scan) == code) {
321 I32 deltanext, minnext;
325 next = regnext(scan);
326 scan = NEXTOPER(scan);
328 scan = NEXTOPER(scan);
329 /* We suppose the run is continuous, last=next...*/
330 minnext = study_chunk(&scan, &deltanext, next,
334 if (max1 < minnext + deltanext)
335 max1 = minnext + deltanext;
336 if (deltanext == I32_MAX)
337 is_inf = is_inf_internal = 1;
339 if (data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
341 if (data && (data_fake.flags & SF_HAS_EVAL))
342 data->flags |= SF_HAS_EVAL;
346 if (code == IFTHEN && num < 2) /* Empty ELSE branch */
348 if (flags & SCF_DO_SUBSTR) {
349 data->pos_min += min1;
350 data->pos_delta += max1 - min1;
351 if (max1 != min1 || is_inf)
352 data->longest = &(data->longest_float);
355 delta += max1 - min1;
357 else if (code == BRANCHJ) /* single branch is optimized. */
358 scan = NEXTOPER(NEXTOPER(scan));
359 else /* single branch is optimized. */
360 scan = NEXTOPER(scan);
363 else if (OP(scan) == EXACT) {
364 I32 l = *OPERAND(scan);
366 unsigned char *s = (unsigned char *)(OPERAND(scan)+1);
367 unsigned char *e = s + l;
376 if (flags & SCF_DO_SUBSTR) { /* Update longest substr. */
377 /* The code below prefers earlier match for fixed
378 offset, later match for variable offset. */
379 if (data->last_end == -1) { /* Update the start info. */
380 data->last_start_min = data->pos_min;
381 data->last_start_max = is_inf
382 ? I32_MAX : data->pos_min + data->pos_delta;
384 sv_catpvn(data->last_found, (char *)(OPERAND(scan)+1), *OPERAND(scan));
385 data->last_end = data->pos_min + l;
386 data->pos_min += l; /* As in the first entry. */
387 data->flags &= ~SF_BEFORE_EOL;
390 else if (PL_regkind[(U8)OP(scan)] == EXACT) {
391 I32 l = *OPERAND(scan);
392 if (flags & SCF_DO_SUBSTR)
395 unsigned char *s = (unsigned char *)(OPERAND(scan)+1);
396 unsigned char *e = s + l;
405 if (data && (flags & SCF_DO_SUBSTR))
408 else if (strchr(PL_varies,OP(scan))) {
409 I32 mincount, maxcount, minnext, deltanext, pos_before, fl;
410 regnode *oscan = scan;
412 switch (PL_regkind[(U8)OP(scan)]) {
414 scan = NEXTOPER(scan);
417 if (flags & SCF_DO_SUBSTR) {
418 next = NEXTOPER(scan);
419 if (OP(next) == EXACT) {
421 maxcount = REG_INFTY;
422 next = regnext(scan);
423 scan = NEXTOPER(scan);
427 if (flags & SCF_DO_SUBSTR)
432 is_inf = is_inf_internal = 1;
433 scan = regnext(scan);
434 if (flags & SCF_DO_SUBSTR) {
436 data->longest = &(data->longest_float);
438 goto optimize_curly_tail;
440 mincount = ARG1(scan);
441 maxcount = ARG2(scan);
442 next = regnext(scan);
443 scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
445 if (flags & SCF_DO_SUBSTR) {
446 if (mincount == 0) scan_commit(data);
447 pos_before = data->pos_min;
451 data->flags &= ~(SF_HAS_PAR|SF_IN_PAR|SF_HAS_EVAL);
453 data->flags |= SF_IS_INF;
455 /* This will finish on WHILEM, setting scan, or on NULL: */
456 minnext = study_chunk(&scan, &deltanext, last, data,
458 ? (flags & ~SCF_DO_SUBSTR) : flags);
459 if (!scan) /* It was not CURLYX, but CURLY. */
461 if (ckWARN(WARN_UNSAFE) && (minnext + deltanext == 0)
462 && !(data->flags & (SF_HAS_PAR|SF_IN_PAR))
463 && maxcount <= REG_INFTY/3) /* Complement check for big count */
464 Perl_warner(aTHX_ WARN_UNSAFE, "Strange *+?{} on zero-length expression");
465 min += minnext * mincount;
466 is_inf_internal |= (maxcount == REG_INFTY
467 && (minnext + deltanext) > 0
468 || deltanext == I32_MAX);
469 is_inf |= is_inf_internal;
470 delta += (minnext + deltanext) * maxcount - minnext * mincount;
472 /* Try powerful optimization CURLYX => CURLYN. */
473 if ( OP(oscan) == CURLYX && data
474 && data->flags & SF_IN_PAR
475 && !(data->flags & SF_HAS_EVAL)
476 && !deltanext && minnext == 1 ) {
477 /* Try to optimize to CURLYN. */
478 regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS;
479 regnode *nxt1 = nxt, *nxt2;
483 if (!strchr(PL_simple,OP(nxt))
484 && !(PL_regkind[(U8)OP(nxt)] == EXACT
485 && *OPERAND(nxt) == 1))
489 if (OP(nxt) != CLOSE)
491 /* Now we know that nxt2 is the only contents: */
492 oscan->flags = ARG(nxt);
494 OP(nxt1) = NOTHING; /* was OPEN. */
496 OP(nxt1 + 1) = OPTIMIZED; /* was count. */
497 NEXT_OFF(nxt1+ 1) = 0; /* just for consistancy. */
498 NEXT_OFF(nxt2) = 0; /* just for consistancy with CURLY. */
499 OP(nxt) = OPTIMIZED; /* was CLOSE. */
500 OP(nxt + 1) = OPTIMIZED; /* was count. */
501 NEXT_OFF(nxt+ 1) = 0; /* just for consistancy. */
506 /* Try optimization CURLYX => CURLYM. */
507 if ( OP(oscan) == CURLYX && data
508 && !(data->flags & SF_HAS_PAR)
509 && !(data->flags & SF_HAS_EVAL)
511 /* XXXX How to optimize if data == 0? */
512 /* Optimize to a simpler form. */
513 regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN */
517 while ( (nxt2 = regnext(nxt)) /* skip over embedded stuff*/
518 && (OP(nxt2) != WHILEM))
520 OP(nxt2) = SUCCEED; /* Whas WHILEM */
521 /* Need to optimize away parenths. */
522 if (data->flags & SF_IN_PAR) {
523 /* Set the parenth number. */
524 regnode *nxt1 = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN*/
526 if (OP(nxt) != CLOSE)
527 FAIL("panic opt close");
528 oscan->flags = ARG(nxt);
529 OP(nxt1) = OPTIMIZED; /* was OPEN. */
530 OP(nxt) = OPTIMIZED; /* was CLOSE. */
532 OP(nxt1 + 1) = OPTIMIZED; /* was count. */
533 OP(nxt + 1) = OPTIMIZED; /* was count. */
534 NEXT_OFF(nxt1 + 1) = 0; /* just for consistancy. */
535 NEXT_OFF(nxt + 1) = 0; /* just for consistancy. */
538 while ( nxt1 && (OP(nxt1) != WHILEM)) {
539 regnode *nnxt = regnext(nxt1);
542 if (reg_off_by_arg[OP(nxt1)])
543 ARG_SET(nxt1, nxt2 - nxt1);
544 else if (nxt2 - nxt1 < U16_MAX)
545 NEXT_OFF(nxt1) = nxt2 - nxt1;
547 OP(nxt) = NOTHING; /* Cannot beautify */
552 /* Optimize again: */
553 study_chunk(&nxt1, &deltanext, nxt, NULL, 0);
558 if (data && fl & (SF_HAS_PAR|SF_IN_PAR))
560 if (flags & SCF_DO_SUBSTR) {
561 SV *last_str = Nullsv;
562 int counted = mincount != 0;
564 if (data->last_end > 0 && mincount != 0) { /* Ends with a string. */
565 I32 b = pos_before >= data->last_start_min
566 ? pos_before : data->last_start_min;
568 char *s = SvPV(data->last_found, l);
569 I32 old = b - data->last_start_min;
572 old = utf8_hop((U8*)s, old) - (U8*)s;
575 /* Get the added string: */
576 last_str = newSVpvn(s + old, l);
577 if (deltanext == 0 && pos_before == b) {
578 /* What was added is a constant string */
580 SvGROW(last_str, (mincount * l) + 1);
581 repeatcpy(SvPVX(last_str) + l,
582 SvPVX(last_str), l, mincount - 1);
583 SvCUR(last_str) *= mincount;
584 /* Add additional parts. */
585 SvCUR_set(data->last_found,
586 SvCUR(data->last_found) - l);
587 sv_catsv(data->last_found, last_str);
588 data->last_end += l * (mincount - 1);
592 /* It is counted once already... */
593 data->pos_min += minnext * (mincount - counted);
594 data->pos_delta += - counted * deltanext +
595 (minnext + deltanext) * maxcount - minnext * mincount;
596 if (mincount != maxcount) {
598 if (mincount && last_str) {
599 sv_setsv(data->last_found, last_str);
600 data->last_end = data->pos_min;
601 data->last_start_min =
602 data->pos_min - CHR_SVLEN(last_str);
603 data->last_start_max = is_inf
605 : data->pos_min + data->pos_delta
606 - CHR_SVLEN(last_str);
608 data->longest = &(data->longest_float);
610 SvREFCNT_dec(last_str);
612 if (data && (fl & SF_HAS_EVAL))
613 data->flags |= SF_HAS_EVAL;
615 if (OP(oscan) != CURLYX) {
616 while (PL_regkind[(U8)OP(next = regnext(oscan))] == NOTHING
618 NEXT_OFF(oscan) += NEXT_OFF(next);
621 default: /* REF only? */
622 if (flags & SCF_DO_SUBSTR) {
624 data->longest = &(data->longest_float);
626 is_inf = is_inf_internal = 1;
630 else if (strchr(PL_simple,OP(scan)) || PL_regkind[(U8)OP(scan)] == ANYUTF8) {
631 if (flags & SCF_DO_SUBSTR) {
637 else if (PL_regkind[(U8)OP(scan)] == EOL && flags & SCF_DO_SUBSTR) {
638 data->flags |= (OP(scan) == MEOL
642 else if (PL_regkind[(U8)OP(scan)] == BRANCHJ
643 && (scan->flags || data)
644 && (OP(scan) == IFMATCH || OP(scan) == UNLESSM)) {
645 I32 deltanext, minnext;
649 next = regnext(scan);
650 nscan = NEXTOPER(NEXTOPER(scan));
651 minnext = study_chunk(&nscan, &deltanext, last, &data_fake, 0);
654 FAIL("variable length lookbehind not implemented");
656 else if (minnext > U8_MAX) {
657 FAIL2("lookbehind longer than %d not implemented", U8_MAX);
659 scan->flags = minnext;
661 if (data && data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
663 if (data && (data_fake.flags & SF_HAS_EVAL))
664 data->flags |= SF_HAS_EVAL;
666 else if (OP(scan) == OPEN) {
669 else if (OP(scan) == CLOSE && ARG(scan) == is_par) {
670 next = regnext(scan);
672 if ( next && (OP(next) != WHILEM) && next < last)
673 is_par = 0; /* Disable optimization */
675 else if (OP(scan) == EVAL) {
677 data->flags |= SF_HAS_EVAL;
679 else if (OP(scan) == LOGICAL && scan->flags == 2) { /* Embedded */
680 if (flags & SCF_DO_SUBSTR) {
682 data->longest = &(data->longest_float);
684 is_inf = is_inf_internal = 1;
686 /* Else: zero-length, ignore. */
687 scan = regnext(scan);
692 *deltap = is_inf_internal ? I32_MAX : delta;
693 if (flags & SCF_DO_SUBSTR && is_inf)
694 data->pos_delta = I32_MAX - data->pos_min;
697 if (is_par && pars==1 && data) {
698 data->flags |= SF_IN_PAR;
699 data->flags &= ~SF_HAS_PAR;
701 else if (pars && data) {
702 data->flags |= SF_HAS_PAR;
703 data->flags &= ~SF_IN_PAR;
709 S_add_data(pTHX_ I32 n, char *s)
712 if (PL_regcomp_rx->data) {
713 Renewc(PL_regcomp_rx->data,
714 sizeof(*PL_regcomp_rx->data) + sizeof(void*) * (PL_regcomp_rx->data->count + n - 1),
715 char, struct reg_data);
716 Renew(PL_regcomp_rx->data->what, PL_regcomp_rx->data->count + n, U8);
717 PL_regcomp_rx->data->count += n;
720 Newc(1207, PL_regcomp_rx->data, sizeof(*PL_regcomp_rx->data) + sizeof(void*) * (n - 1),
721 char, struct reg_data);
722 New(1208, PL_regcomp_rx->data->what, n, U8);
723 PL_regcomp_rx->data->count = n;
725 Copy(s, PL_regcomp_rx->data->what + PL_regcomp_rx->data->count - n, n, U8);
726 return PL_regcomp_rx->data->count - n;
730 Perl_reginitcolors(pTHX)
734 char *s = PerlEnv_getenv("PERL_RE_COLORS");
737 PL_colors[0] = s = savepv(s);
745 PL_colors[i] = s = "";
755 - pregcomp - compile a regular expression into internal code
757 * We can't allocate space until we know how big the compiled form will be,
758 * but we can't compile it (and thus know how big it is) until we've got a
759 * place to put the code. So we cheat: we compile it twice, once with code
760 * generation turned off and size counting turned on, and once "for real".
761 * This also means that we don't allocate space until we are sure that the
762 * thing really will compile successfully, and we never have to move the
763 * code and thus invalidate pointers into it. (Note that it has to be in
764 * one piece because free() must be able to free it all.) [NB: not true in perl]
766 * Beware that the optimization-preparation code in here knows about some
767 * of the structure of the compiled regexp. [I'll say.]
770 Perl_pregcomp(pTHX_ char *exp, char *xend, PMOP *pm)
785 FAIL("NULL regexp argument");
787 if (PL_curcop == &PL_compiling ? (PL_hints & HINT_UTF8) : IN_UTF8)
788 PL_reg_flags |= RF_utf8;
792 PL_regprecomp = savepvn(exp, xend - exp);
793 DEBUG_r(if (!PL_colorset) reginitcolors());
794 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sCompiling%s RE `%s%*s%s'\n",
795 PL_colors[4],PL_colors[5],PL_colors[0],
796 xend - exp, PL_regprecomp, PL_colors[1]));
797 PL_regflags = pm->op_pmflags;
801 PL_seen_zerolen = *exp == '^' ? -1 : 0;
805 /* First pass: determine size, legality. */
806 PL_regcomp_parse = exp;
811 PL_regcode = &PL_regdummy;
812 regc((U8)REG_MAGIC, (char*)PL_regcode);
813 if (reg(0, &flags) == NULL) {
814 Safefree(PL_regprecomp);
815 PL_regprecomp = Nullch;
818 DEBUG_r(PerlIO_printf(Perl_debug_log, "size %d ", PL_regsize));
820 /* Small enough for pointer-storage convention?
821 If extralen==0, this means that we will not need long jumps. */
822 if (PL_regsize >= 0x10000L && PL_extralen)
823 PL_regsize += PL_extralen;
827 /* Allocate space and initialize. */
828 Newc(1001, r, sizeof(regexp) + (unsigned)PL_regsize * sizeof(regnode),
831 FAIL("regexp out of space");
833 r->prelen = xend - exp;
834 r->precomp = PL_regprecomp;
836 r->reganch = pm->op_pmflags & PMf_COMPILETIME;
837 r->nparens = PL_regnpar - 1; /* set early to validate backrefs */
839 r->substrs = 0; /* Useful during FAIL. */
840 r->startp = 0; /* Useful during FAIL. */
841 r->endp = 0; /* Useful during FAIL. */
845 /* Second pass: emit code. */
846 PL_regcomp_parse = exp;
850 PL_regcode = r->program;
851 /* Store the count of eval-groups for security checks: */
852 PL_regcode->next_off = ((PL_seen_evals > U16_MAX) ? U16_MAX : PL_seen_evals);
853 regc((U8)REG_MAGIC, (char*) PL_regcode++);
855 if (reg(0, &flags) == NULL)
858 /* Dig out information for optimizations. */
859 r->reganch = pm->op_pmflags & PMf_COMPILETIME; /* Again? */
860 pm->op_pmflags = PL_regflags;
862 r->reganch |= ROPT_UTF8;
863 r->regstclass = NULL;
864 if (PL_regnaughty >= 10) /* Probably an expensive pattern. */
865 r->reganch |= ROPT_NAUGHTY;
866 scan = r->program + 1; /* First BRANCH. */
868 /* XXXX To minimize changes to RE engine we always allocate
869 3-units-long substrs field. */
870 Newz(1004, r->substrs, 1, struct reg_substr_data);
872 if (OP(scan) != BRANCH) { /* Only one top-level choice. */
875 STRLEN longest_float_length, longest_fixed_length;
877 StructCopy(&zero_scan_data, &data, scan_data_t);
879 /* Skip introductions and multiplicators >= 1. */
880 while ((OP(first) == OPEN && (sawopen = 1)) ||
881 (OP(first) == BRANCH && OP(regnext(first)) != BRANCH) ||
882 (OP(first) == PLUS) ||
883 (OP(first) == MINMOD) ||
884 (PL_regkind[(U8)OP(first)] == CURLY && ARG1(first) > 0) ) {
885 if (OP(first) == PLUS)
888 first += regarglen[(U8)OP(first)];
889 first = NEXTOPER(first);
892 /* Starting-point info. */
894 if (OP(first) == EXACT); /* Empty, get anchored substr later. */
895 else if (strchr(PL_simple+4,OP(first)))
896 r->regstclass = first;
897 else if (PL_regkind[(U8)OP(first)] == BOUND ||
898 PL_regkind[(U8)OP(first)] == NBOUND)
899 r->regstclass = first;
900 else if (PL_regkind[(U8)OP(first)] == BOL) {
901 r->reganch |= (OP(first) == MBOL ? ROPT_ANCH_MBOL: ROPT_ANCH_BOL);
902 first = NEXTOPER(first);
905 else if (OP(first) == GPOS) {
906 r->reganch |= ROPT_ANCH_GPOS;
907 first = NEXTOPER(first);
910 else if ((OP(first) == STAR &&
911 PL_regkind[(U8)OP(NEXTOPER(first))] == REG_ANY) &&
912 !(r->reganch & ROPT_ANCH) )
914 /* turn .* into ^.* with an implied $*=1 */
915 r->reganch |= ROPT_ANCH_BOL | ROPT_IMPLICIT;
916 first = NEXTOPER(first);
919 if (sawplus && (!sawopen || !PL_regsawback))
920 r->reganch |= ROPT_SKIP; /* x+ must match 1st of run */
922 /* Scan is after the zeroth branch, first is atomic matcher. */
923 DEBUG_r(PerlIO_printf(Perl_debug_log, "first at %d\n",
926 * If there's something expensive in the r.e., find the
927 * longest literal string that must appear and make it the
928 * regmust. Resolve ties in favor of later strings, since
929 * the regstart check works with the beginning of the r.e.
930 * and avoiding duplication strengthens checking. Not a
931 * strong reason, but sufficient in the absence of others.
932 * [Now we resolve ties in favor of the earlier string if
933 * it happens that c_offset_min has been invalidated, since the
934 * earlier string may buy us something the later one won't.]
938 data.longest_fixed = newSVpvn("",0);
939 data.longest_float = newSVpvn("",0);
940 data.last_found = newSVpvn("",0);
941 data.longest = &(data.longest_fixed);
944 minlen = study_chunk(&first, &fake, scan + PL_regsize, /* Up to end */
945 &data, SCF_DO_SUBSTR);
946 if ( PL_regnpar == 1 && data.longest == &(data.longest_fixed)
947 && data.last_start_min == 0 && data.last_end > 0
949 && (!(PL_regseen & REG_SEEN_GPOS) || (r->reganch & ROPT_ANCH_GPOS)))
950 r->reganch |= ROPT_CHECK_ALL;
952 SvREFCNT_dec(data.last_found);
954 longest_float_length = CHR_SVLEN(data.longest_float);
955 if (longest_float_length
956 || (data.flags & SF_FL_BEFORE_EOL
957 && (!(data.flags & SF_FL_BEFORE_MEOL)
958 || (PL_regflags & PMf_MULTILINE)))) {
961 if (SvCUR(data.longest_fixed) /* ok to leave SvCUR */
962 && data.offset_fixed == data.offset_float_min
963 && SvCUR(data.longest_fixed) == SvCUR(data.longest_float))
964 goto remove_float; /* As in (a)+. */
966 r->float_substr = data.longest_float;
967 r->float_min_offset = data.offset_float_min;
968 r->float_max_offset = data.offset_float_max;
969 t = (data.flags & SF_FL_BEFORE_EOL /* Can't have SEOL and MULTI */
970 && (!(data.flags & SF_FL_BEFORE_MEOL)
971 || (PL_regflags & PMf_MULTILINE)));
972 fbm_compile(r->float_substr, t ? FBMcf_TAIL : 0);
976 r->float_substr = Nullsv;
977 SvREFCNT_dec(data.longest_float);
978 longest_float_length = 0;
981 longest_fixed_length = CHR_SVLEN(data.longest_fixed);
982 if (longest_fixed_length
983 || (data.flags & SF_FIX_BEFORE_EOL /* Cannot have SEOL and MULTI */
984 && (!(data.flags & SF_FIX_BEFORE_MEOL)
985 || (PL_regflags & PMf_MULTILINE)))) {
988 r->anchored_substr = data.longest_fixed;
989 r->anchored_offset = data.offset_fixed;
990 t = (data.flags & SF_FIX_BEFORE_EOL /* Can't have SEOL and MULTI */
991 && (!(data.flags & SF_FIX_BEFORE_MEOL)
992 || (PL_regflags & PMf_MULTILINE)));
993 fbm_compile(r->anchored_substr, t ? FBMcf_TAIL : 0);
996 r->anchored_substr = Nullsv;
997 SvREFCNT_dec(data.longest_fixed);
998 longest_fixed_length = 0;
1001 /* A temporary algorithm prefers floated substr to fixed one to dig more info. */
1002 if (longest_fixed_length > longest_float_length) {
1003 r->check_substr = r->anchored_substr;
1004 r->check_offset_min = r->check_offset_max = r->anchored_offset;
1005 if (r->reganch & ROPT_ANCH_SINGLE)
1006 r->reganch |= ROPT_NOSCAN;
1009 r->check_substr = r->float_substr;
1010 r->check_offset_min = data.offset_float_min;
1011 r->check_offset_max = data.offset_float_max;
1015 /* Several toplevels. Best we can is to set minlen. */
1018 DEBUG_r(PerlIO_printf(Perl_debug_log, "\n"));
1019 scan = r->program + 1;
1020 minlen = study_chunk(&scan, &fake, scan + PL_regsize, NULL, 0);
1021 r->check_substr = r->anchored_substr = r->float_substr = Nullsv;
1025 if (PL_regseen & REG_SEEN_GPOS)
1026 r->reganch |= ROPT_GPOS_SEEN;
1027 if (PL_regseen & REG_SEEN_LOOKBEHIND)
1028 r->reganch |= ROPT_LOOKBEHIND_SEEN;
1029 if (PL_regseen & REG_SEEN_EVAL)
1030 r->reganch |= ROPT_EVAL_SEEN;
1031 Newz(1002, r->startp, PL_regnpar, I32);
1032 Newz(1002, r->endp, PL_regnpar, I32);
1033 DEBUG_r(regdump(r));
1038 - reg - regular expression, i.e. main body or parenthesized thing
1040 * Caller must absorb opening parenthesis.
1042 * Combining parenthesis handling with the base level of regular expression
1043 * is a trifle forced, but the need to tie the tails of the branches to what
1044 * follows makes it hard to avoid.
1047 S_reg(pTHX_ I32 paren, I32 *flagp)
1048 /* paren: Parenthesized? 0=top, 1=(, inside: changed to letter. */
1051 register regnode *ret; /* Will be the head of the group. */
1052 register regnode *br;
1053 register regnode *lastbr;
1054 register regnode *ender = 0;
1055 register I32 parno = 0;
1056 I32 flags, oregflags = PL_regflags, have_branch = 0, open = 0;
1059 *flagp = 0; /* Tentatively. */
1061 /* Make an OPEN node, if parenthesized. */
1063 if (*PL_regcomp_parse == '?') {
1064 U16 posflags = 0, negflags = 0;
1065 U16 *flagsp = &posflags;
1069 paren = *PL_regcomp_parse++;
1070 ret = NULL; /* For look-ahead/behind. */
1073 PL_regseen |= REG_SEEN_LOOKBEHIND;
1074 if (*PL_regcomp_parse == '!')
1076 if (*PL_regcomp_parse != '=' && *PL_regcomp_parse != '!')
1087 FAIL2("Sequence (?%c...) not implemented", (int)paren);
1090 while (*PL_regcomp_parse && *PL_regcomp_parse != ')')
1092 if (*PL_regcomp_parse != ')')
1093 FAIL("Sequence (?#... not terminated");
1099 paren = *PL_regcomp_parse++;
1104 I32 count = 1, n = 0;
1106 char *s = PL_regcomp_parse;
1108 OP_4tree *sop, *rop;
1111 PL_regseen |= REG_SEEN_EVAL;
1112 while (count && (c = *PL_regcomp_parse)) {
1113 if (c == '\\' && PL_regcomp_parse[1])
1121 if (*PL_regcomp_parse != ')')
1122 FAIL("Sequence (?{...}) not terminated or not {}-balanced");
1126 if (PL_regcomp_parse - 1 - s)
1127 sv = newSVpvn(s, PL_regcomp_parse - 1 - s);
1129 sv = newSVpvn("", 0);
1131 rop = sv_compile_2op(sv, &sop, "re", &av);
1133 n = add_data(3, "nop");
1134 PL_regcomp_rx->data->data[n] = (void*)rop;
1135 PL_regcomp_rx->data->data[n+1] = (void*)sop;
1136 PL_regcomp_rx->data->data[n+2] = (void*)av;
1139 else { /* First pass */
1140 if (PL_reginterp_cnt < ++PL_seen_evals
1141 && PL_curcop != &PL_compiling)
1142 /* No compiled RE interpolated, has runtime
1143 components ===> unsafe. */
1144 FAIL("Eval-group not allowed at runtime, use re 'eval'");
1146 FAIL("Eval-group in insecure regular expression");
1151 ret = reg_node(LOGICAL);
1154 regtail(ret, reganode(EVAL, n));
1157 return reganode(EVAL, n);
1161 if (PL_regcomp_parse[0] == '?') {
1162 if (PL_regcomp_parse[1] == '=' || PL_regcomp_parse[1] == '!'
1163 || PL_regcomp_parse[1] == '<'
1164 || PL_regcomp_parse[1] == '{') { /* Lookahead or eval. */
1167 ret = reg_node(LOGICAL);
1170 regtail(ret, reg(1, &flag));
1174 else if (PL_regcomp_parse[0] >= '1' && PL_regcomp_parse[0] <= '9' ) {
1175 parno = atoi(PL_regcomp_parse++);
1177 while (isDIGIT(*PL_regcomp_parse))
1179 ret = reganode(GROUPP, parno);
1180 if ((c = *nextchar()) != ')')
1181 FAIL2("Switch (?(number%c not recognized", c);
1183 regtail(ret, reganode(IFTHEN, 0));
1184 br = regbranch(&flags, 1);
1186 br = reganode(LONGJMP, 0);
1188 regtail(br, reganode(LONGJMP, 0));
1193 lastbr = reganode(IFTHEN, 0); /* Fake one for optimizer. */
1194 regbranch(&flags, 1);
1195 regtail(ret, lastbr);
1203 FAIL("Switch (?(condition)... contains too many branches");
1204 ender = reg_node(TAIL);
1207 regtail(lastbr, ender);
1208 regtail(NEXTOPER(NEXTOPER(lastbr)), ender);
1211 regtail(ret, ender);
1215 FAIL2("Unknown condition for (?(%.2s", PL_regcomp_parse);
1219 FAIL("Sequence (? incomplete");
1224 while (*PL_regcomp_parse && strchr("iogcmsx", *PL_regcomp_parse)) {
1225 if (*PL_regcomp_parse != 'o')
1226 pmflag(flagsp, *PL_regcomp_parse);
1229 if (*PL_regcomp_parse == '-') {
1234 PL_regflags |= posflags;
1235 PL_regflags &= ~negflags;
1236 if (*PL_regcomp_parse == ':') {
1242 if (*PL_regcomp_parse != ')')
1243 FAIL2("Sequence (?%c...) not recognized", *PL_regcomp_parse);
1252 ret = reganode(OPEN, parno);
1259 /* Pick up the branches, linking them together. */
1260 br = regbranch(&flags, 1);
1263 if (*PL_regcomp_parse == '|') {
1264 if (!SIZE_ONLY && PL_extralen) {
1265 reginsert(BRANCHJ, br);
1268 reginsert(BRANCH, br);
1271 PL_extralen += 1; /* For BRANCHJ-BRANCH. */
1273 else if (paren == ':') {
1274 *flagp |= flags&SIMPLE;
1276 if (open) { /* Starts with OPEN. */
1277 regtail(ret, br); /* OPEN -> first. */
1279 else if (paren != '?') /* Not Conditional */
1283 *flagp |= flags&SPSTART;
1285 while (*PL_regcomp_parse == '|') {
1286 if (!SIZE_ONLY && PL_extralen) {
1287 ender = reganode(LONGJMP,0);
1288 regtail(NEXTOPER(NEXTOPER(lastbr)), ender); /* Append to the previous. */
1291 PL_extralen += 2; /* Account for LONGJMP. */
1293 br = regbranch(&flags, 0);
1296 regtail(lastbr, br); /* BRANCH -> BRANCH. */
1300 *flagp |= flags&SPSTART;
1303 if (have_branch || paren != ':') {
1304 /* Make a closing node, and hook it on the end. */
1307 ender = reg_node(TAIL);
1310 ender = reganode(CLOSE, parno);
1316 *flagp &= ~HASWIDTH;
1319 ender = reg_node(SUCCEED);
1322 ender = reg_node(END);
1325 regtail(lastbr, ender);
1328 /* Hook the tails of the branches to the closing node. */
1329 for (br = ret; br != NULL; br = regnext(br)) {
1330 regoptail(br, ender);
1337 static char parens[] = "=!<,>";
1339 if (paren && (p = strchr(parens, paren))) {
1340 int node = ((p - parens) % 2) ? UNLESSM : IFMATCH;
1341 int flag = (p - parens) > 1;
1344 node = SUSPEND, flag = 0;
1345 reginsert(node,ret);
1347 regtail(ret, reg_node(TAIL));
1351 /* Check for proper termination. */
1353 PL_regflags = oregflags;
1354 if (PL_regcomp_parse >= PL_regxend || *nextchar() != ')') {
1355 FAIL("unmatched () in regexp");
1358 else if (!paren && PL_regcomp_parse < PL_regxend) {
1359 if (*PL_regcomp_parse == ')') {
1360 FAIL("unmatched () in regexp");
1363 FAIL("junk on end of regexp"); /* "Can't happen". */
1371 - regbranch - one alternative of an | operator
1373 * Implements the concatenation operator.
1376 S_regbranch(pTHX_ I32 *flagp, I32 first)
1379 register regnode *ret;
1380 register regnode *chain = NULL;
1381 register regnode *latest;
1382 I32 flags = 0, c = 0;
1387 if (!SIZE_ONLY && PL_extralen)
1388 ret = reganode(BRANCHJ,0);
1390 ret = reg_node(BRANCH);
1393 if (!first && SIZE_ONLY)
1394 PL_extralen += 1; /* BRANCHJ */
1396 *flagp = WORST; /* Tentatively. */
1400 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != '|' && *PL_regcomp_parse != ')') {
1402 latest = regpiece(&flags);
1403 if (latest == NULL) {
1404 if (flags & TRYAGAIN)
1408 else if (ret == NULL)
1410 *flagp |= flags&HASWIDTH;
1411 if (chain == NULL) /* First piece. */
1412 *flagp |= flags&SPSTART;
1415 regtail(chain, latest);
1420 if (chain == NULL) { /* Loop ran zero times. */
1421 chain = reg_node(NOTHING);
1426 *flagp |= flags&SIMPLE;
1433 - regpiece - something followed by possible [*+?]
1435 * Note that the branching code sequences used for ? and the general cases
1436 * of * and + are somewhat optimized: they use the same NOTHING node as
1437 * both the endmarker for their branch list and the body of the last branch.
1438 * It might seem that this node could be dispensed with entirely, but the
1439 * endmarker role is not redundant.
1442 S_regpiece(pTHX_ I32 *flagp)
1445 register regnode *ret;
1447 register char *next;
1449 char *origparse = PL_regcomp_parse;
1452 I32 max = REG_INFTY;
1454 ret = regatom(&flags);
1456 if (flags & TRYAGAIN)
1461 op = *PL_regcomp_parse;
1463 if (op == '{' && regcurly(PL_regcomp_parse)) {
1464 next = PL_regcomp_parse + 1;
1466 while (isDIGIT(*next) || *next == ',') {
1475 if (*next == '}') { /* got one */
1479 min = atoi(PL_regcomp_parse);
1483 maxpos = PL_regcomp_parse;
1485 if (!max && *maxpos != '0')
1486 max = REG_INFTY; /* meaning "infinity" */
1487 else if (max >= REG_INFTY)
1488 FAIL2("Quantifier in {,} bigger than %d", REG_INFTY - 1);
1489 PL_regcomp_parse = next;
1493 if ((flags&SIMPLE)) {
1494 PL_regnaughty += 2 + PL_regnaughty / 2;
1495 reginsert(CURLY, ret);
1498 PL_regnaughty += 4 + PL_regnaughty; /* compound interest */
1499 regtail(ret, reg_node(WHILEM));
1500 if (!SIZE_ONLY && PL_extralen) {
1501 reginsert(LONGJMP,ret);
1502 reginsert(NOTHING,ret);
1503 NEXT_OFF(ret) = 3; /* Go over LONGJMP. */
1505 reginsert(CURLYX,ret);
1506 if (!SIZE_ONLY && PL_extralen)
1507 NEXT_OFF(ret) = 3; /* Go over NOTHING to LONGJMP. */
1508 regtail(ret, reg_node(NOTHING));
1518 if (max && max < min)
1519 FAIL("Can't do {n,m} with n > m");
1534 #if 0 /* Now runtime fix should be reliable. */
1535 if (!(flags&HASWIDTH) && op != '?')
1536 FAIL("regexp *+ operand could be empty");
1541 *flagp = (op != '+') ? (WORST|SPSTART|HASWIDTH) : (WORST|HASWIDTH);
1543 if (op == '*' && (flags&SIMPLE)) {
1544 reginsert(STAR, ret);
1548 else if (op == '*') {
1552 else if (op == '+' && (flags&SIMPLE)) {
1553 reginsert(PLUS, ret);
1557 else if (op == '+') {
1561 else if (op == '?') {
1566 if (ckWARN(WARN_UNSAFE) && !SIZE_ONLY && !(flags&HASWIDTH) && max > REG_INFTY/3) {
1567 Perl_warner(aTHX_ WARN_UNSAFE, "%.*s matches null string many times",
1568 PL_regcomp_parse - origparse, origparse);
1571 if (*PL_regcomp_parse == '?') {
1573 reginsert(MINMOD, ret);
1574 regtail(ret, ret + NODE_STEP_REGNODE);
1576 if (ISMULT2(PL_regcomp_parse))
1577 FAIL("nested *?+ in regexp");
1583 - regatom - the lowest level
1585 * Optimization: gobbles an entire sequence of ordinary characters so that
1586 * it can turn them into a single node, which is smaller to store and
1587 * faster to run. Backslashed characters are exceptions, each becoming a
1588 * separate node; the code is simpler that way and it's not worth fixing.
1590 * [Yes, it is worth fixing, some scripts can run twice the speed.]
1593 S_regatom(pTHX_ I32 *flagp)
1596 register regnode *ret = 0;
1599 *flagp = WORST; /* Tentatively. */
1602 switch (*PL_regcomp_parse) {
1606 if (PL_regflags & PMf_MULTILINE)
1607 ret = reg_node(MBOL);
1608 else if (PL_regflags & PMf_SINGLELINE)
1609 ret = reg_node(SBOL);
1611 ret = reg_node(BOL);
1614 if (PL_regcomp_parse[1])
1617 if (PL_regflags & PMf_MULTILINE)
1618 ret = reg_node(MEOL);
1619 else if (PL_regflags & PMf_SINGLELINE)
1620 ret = reg_node(SEOL);
1622 ret = reg_node(EOL);
1627 if (PL_regflags & PMf_SINGLELINE)
1628 ret = reg_node(SANYUTF8);
1630 ret = reg_node(ANYUTF8);
1634 if (PL_regflags & PMf_SINGLELINE)
1635 ret = reg_node(SANY);
1637 ret = reg_node(REG_ANY);
1638 *flagp |= HASWIDTH|SIMPLE;
1644 ret = (UTF ? regclassutf8() : regclass());
1645 if (*PL_regcomp_parse != ']')
1646 FAIL("unmatched [] in regexp");
1648 *flagp |= HASWIDTH|SIMPLE;
1652 ret = reg(1, &flags);
1654 if (flags & TRYAGAIN)
1658 *flagp |= flags&(HASWIDTH|SPSTART|SIMPLE);
1662 if (flags & TRYAGAIN) {
1666 FAIL2("internal urp in regexp at /%s/", PL_regcomp_parse);
1667 /* Supposed to be caught earlier. */
1670 if (!regcurly(PL_regcomp_parse)) {
1678 FAIL("?+*{} follows nothing in regexp");
1681 switch (*++PL_regcomp_parse) {
1684 ret = reg_node(SBOL);
1689 ret = reg_node(GPOS);
1690 PL_regseen |= REG_SEEN_GPOS;
1695 ret = reg_node(SEOL);
1700 ret = reg_node(EOS);
1702 PL_seen_zerolen++; /* Do not optimize RE away */
1706 ret = reg_node(SANY);
1707 *flagp |= HASWIDTH|SIMPLE;
1711 ret = reg_node(CLUMP);
1714 if (UTF && !PL_utf8_mark)
1715 is_utf8_mark((U8*)"~"); /* preload table */
1720 ? (LOC ? ALNUMLUTF8 : ALNUMUTF8)
1721 : (LOC ? ALNUML : ALNUM));
1722 *flagp |= HASWIDTH|SIMPLE;
1724 if (UTF && !PL_utf8_alnum)
1725 is_utf8_alnum((U8*)"a"); /* preload table */
1730 ? (LOC ? NALNUMLUTF8 : NALNUMUTF8)
1731 : (LOC ? NALNUML : NALNUM));
1732 *flagp |= HASWIDTH|SIMPLE;
1734 if (UTF && !PL_utf8_alnum)
1735 is_utf8_alnum((U8*)"a"); /* preload table */
1739 PL_regseen |= REG_SEEN_LOOKBEHIND;
1742 ? (LOC ? BOUNDLUTF8 : BOUNDUTF8)
1743 : (LOC ? BOUNDL : BOUND));
1746 if (UTF && !PL_utf8_alnum)
1747 is_utf8_alnum((U8*)"a"); /* preload table */
1751 PL_regseen |= REG_SEEN_LOOKBEHIND;
1754 ? (LOC ? NBOUNDLUTF8 : NBOUNDUTF8)
1755 : (LOC ? NBOUNDL : NBOUND));
1758 if (UTF && !PL_utf8_alnum)
1759 is_utf8_alnum((U8*)"a"); /* preload table */
1764 ? (LOC ? SPACELUTF8 : SPACEUTF8)
1765 : (LOC ? SPACEL : SPACE));
1766 *flagp |= HASWIDTH|SIMPLE;
1768 if (UTF && !PL_utf8_space)
1769 is_utf8_space((U8*)" "); /* preload table */
1774 ? (LOC ? NSPACELUTF8 : NSPACEUTF8)
1775 : (LOC ? NSPACEL : NSPACE));
1776 *flagp |= HASWIDTH|SIMPLE;
1778 if (UTF && !PL_utf8_space)
1779 is_utf8_space((U8*)" "); /* preload table */
1782 ret = reg_node(UTF ? DIGITUTF8 : DIGIT);
1783 *flagp |= HASWIDTH|SIMPLE;
1785 if (UTF && !PL_utf8_digit)
1786 is_utf8_digit((U8*)"1"); /* preload table */
1789 ret = reg_node(UTF ? NDIGITUTF8 : NDIGIT);
1790 *flagp |= HASWIDTH|SIMPLE;
1792 if (UTF && !PL_utf8_digit)
1793 is_utf8_digit((U8*)"1"); /* preload table */
1797 { /* a lovely hack--pretend we saw [\pX] instead */
1798 char* oldregxend = PL_regxend;
1800 if (PL_regcomp_parse[1] == '{') {
1801 PL_regxend = strchr(PL_regcomp_parse, '}');
1803 FAIL("Missing right brace on \\p{}");
1807 PL_regxend = PL_regcomp_parse + 2;
1810 ret = regclassutf8();
1812 PL_regxend = oldregxend;
1815 *flagp |= HASWIDTH|SIMPLE;
1828 case '1': case '2': case '3': case '4':
1829 case '5': case '6': case '7': case '8': case '9':
1831 I32 num = atoi(PL_regcomp_parse);
1833 if (num > 9 && num >= PL_regnpar)
1836 if (!SIZE_ONLY && num > PL_regcomp_rx->nparens)
1837 FAIL("reference to nonexistent group");
1840 ? (LOC ? REFFL : REFF)
1843 while (isDIGIT(*PL_regcomp_parse))
1851 if (PL_regcomp_parse >= PL_regxend)
1852 FAIL("trailing \\ in regexp");
1855 /* Do not generate `unrecognized' warnings here, we fall
1856 back into the quick-grab loop below */
1862 if (PL_regflags & PMf_EXTENDED) {
1863 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != '\n') PL_regcomp_parse++;
1864 if (PL_regcomp_parse < PL_regxend)
1880 ? (LOC ? EXACTFL : EXACTF)
1882 s = (char *) OPERAND(ret);
1883 regc(0, s++); /* save spot for len */
1884 for (len = 0, p = PL_regcomp_parse - 1;
1885 len < 127 && p < PL_regxend;
1890 if (PL_regflags & PMf_EXTENDED)
1891 p = regwhite(p, PL_regxend);
1945 char* e = strchr(p, '}');
1948 FAIL("Missing right brace on \\x{}");
1950 ender = scan_hex(p + 1, e - p, &numlen);
1951 if (numlen + len >= 127) { /* numlen is generous */
1958 FAIL("Can't use \\x{} without 'use utf8' declaration");
1961 ender = scan_hex(p, 2, &numlen);
1967 ender = UCHARAT(p++);
1968 ender = toCTRL(ender);
1970 case '0': case '1': case '2': case '3':case '4':
1971 case '5': case '6': case '7': case '8':case '9':
1973 (isDIGIT(p[1]) && atoi(p) >= PL_regnpar) ) {
1974 ender = scan_oct(p, 3, &numlen);
1983 if (p >= PL_regxend)
1984 FAIL("trailing \\ in regexp");
1987 if (!SIZE_ONLY && ckWARN(WARN_UNSAFE) && isALPHA(*p))
1988 Perl_warner(aTHX_ WARN_UNSAFE,
1989 "/%.127s/: Unrecognized escape \\%c passed through",
1992 goto normal_default;
1997 if ((*p & 0xc0) == 0xc0 && UTF) {
1998 ender = utf8_to_uv((U8*)p, &numlen);
2005 if (PL_regflags & PMf_EXTENDED)
2006 p = regwhite(p, PL_regxend);
2009 ender = toLOWER_LC_uni(ender);
2011 ender = toLOWER_uni(ender);
2013 if (ISMULT2(p)) { /* Back off on ?+*. */
2016 else if (ender >= 0x80 && UTF) {
2017 reguni(ender, s, &numlen);
2027 if (ender >= 0x80 && UTF) {
2028 reguni(ender, s, &numlen);
2036 PL_regcomp_parse = p - 1;
2039 FAIL("internal disaster in regexp");
2045 *OPERAND(ret) = len;
2048 PL_regsize += (len + 2 + sizeof(regnode) - 1) / sizeof(regnode);
2051 PL_regcode += (len + 2 + sizeof(regnode) - 1) / sizeof(regnode);
2061 S_regwhite(pTHX_ char *p, char *e)
2066 else if (*p == '#') {
2069 } while (p < e && *p != '\n');
2077 /* parse POSIX character classes like [[:foo:]] */
2079 S_regpposixcc(pTHX_ I32 value)
2084 if (value == '[' && PL_regcomp_parse + 1 < PL_regxend &&
2085 /* I smell either [: or [= or [. -- POSIX has been here, right? */
2086 (*PL_regcomp_parse == ':' ||
2087 *PL_regcomp_parse == '=' ||
2088 *PL_regcomp_parse == '.')) {
2089 char c = *PL_regcomp_parse;
2090 char* s = PL_regcomp_parse++;
2092 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != c)
2094 if (PL_regcomp_parse == PL_regxend)
2095 /* Grandfather lone [:, [=, [. */
2096 PL_regcomp_parse = s;
2098 PL_regcomp_parse++; /* skip over the c */
2099 if (*PL_regcomp_parse == ']') {
2100 /* Not Implemented Yet.
2101 * (POSIX Extended Character Classes, that is)
2102 * The text between e.g. [: and :] would start
2103 * at s + 1 and stop at regcomp_parse - 2. */
2104 if (ckWARN(WARN_UNSAFE) && !SIZE_ONLY)
2105 Perl_warner(aTHX_ WARN_UNSAFE,
2106 "Character class syntax [%c %c] is reserved for future extensions", c, c);
2107 PL_regcomp_parse++; /* skip over the ending ] */
2111 /* maternal grandfather */
2112 PL_regcomp_parse = s;
2124 register char *opnd, *s;
2126 register I32 lastvalue = 1234;
2127 register I32 range = 0;
2128 register regnode *ret;
2132 s = opnd = (char *) OPERAND(PL_regcode);
2133 ret = reg_node(ANYOF);
2134 for (value = 0; value < 33; value++)
2136 if (*PL_regcomp_parse == '^') { /* Complement of range. */
2140 *opnd |= ANYOF_INVERT;
2143 PL_regcode += ANY_SKIP;
2145 *opnd |= ANYOF_FOLD;
2147 *opnd |= ANYOF_LOCALE;
2150 PL_regsize += ANY_SKIP;
2152 if (*PL_regcomp_parse == ']' || *PL_regcomp_parse == '-')
2153 goto skipcond; /* allow 1st char to be ] or - */
2154 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != ']') {
2156 value = UCHARAT(PL_regcomp_parse++);
2158 (void)regpposixcc(value); /* ignore the return value for now */
2159 else if (value == '\\') {
2160 value = UCHARAT(PL_regcomp_parse++);
2165 *opnd |= ANYOF_ALNUML;
2167 for (value = 0; value < 256; value++)
2169 ANYOF_SET(opnd, value);
2177 *opnd |= ANYOF_NALNUML;
2179 for (value = 0; value < 256; value++)
2180 if (!isALNUM(value))
2181 ANYOF_SET(opnd, value);
2189 *opnd |= ANYOF_SPACEL;
2191 for (value = 0; value < 256; value++)
2193 ANYOF_SET(opnd, value);
2201 *opnd |= ANYOF_NSPACEL;
2203 for (value = 0; value < 256; value++)
2204 if (!isSPACE(value))
2205 ANYOF_SET(opnd, value);
2212 for (value = '0'; value <= '9'; value++)
2213 ANYOF_SET(opnd, value);
2219 for (value = 0; value < '0'; value++)
2220 ANYOF_SET(opnd, value);
2221 for (value = '9' + 1; value < 256; value++)
2222 ANYOF_SET(opnd, value);
2248 value = scan_hex(PL_regcomp_parse, 2, &numlen);
2249 PL_regcomp_parse += numlen;
2252 value = UCHARAT(PL_regcomp_parse++);
2253 value = toCTRL(value);
2255 case '0': case '1': case '2': case '3': case '4':
2256 case '5': case '6': case '7': case '8': case '9':
2257 value = scan_oct(--PL_regcomp_parse, 3, &numlen);
2258 PL_regcomp_parse += numlen;
2263 if (lastvalue > value)
2264 FAIL("invalid [] range in regexp");
2269 if (*PL_regcomp_parse == '-' && PL_regcomp_parse+1 < PL_regxend &&
2270 PL_regcomp_parse[1] != ']') {
2273 continue; /* do it next time */
2278 if ((isLOWER(lastvalue) && isLOWER(value)) ||
2279 (isUPPER(lastvalue) && isUPPER(value)))
2282 if (isLOWER(lastvalue)) {
2283 for (i = lastvalue; i <= value; i++)
2287 for (i = lastvalue; i <= value; i++)
2294 for ( ; lastvalue <= value; lastvalue++)
2295 ANYOF_SET(opnd, lastvalue);
2299 /* optimize case-insensitive simple patterns (e.g. /[a-z]/i) */
2300 if (!SIZE_ONLY && (*opnd & (0xFF ^ ANYOF_INVERT)) == ANYOF_FOLD) {
2301 for (value = 0; value < 256; ++value) {
2302 if (ANYOF_TEST(opnd, value)) {
2303 I32 cf = PL_fold[value];
2304 ANYOF_SET(opnd, cf);
2307 *opnd &= ~ANYOF_FOLD;
2309 /* optimize inverted simple patterns (e.g. [^a-z]) */
2310 if (!SIZE_ONLY && (*opnd & 0xFF) == ANYOF_INVERT) {
2311 for (value = 0; value < 32; ++value)
2312 opnd[1 + value] ^= 0xFF;
2319 S_regclassutf8(pTHX)
2321 register char *opnd, *e;
2323 register U32 lastvalue = 123456;
2324 register I32 range = 0;
2325 register regnode *ret;
2332 if (*PL_regcomp_parse == '^') { /* Complement of range. */
2336 flags |= ANYOF_INVERT;
2340 flags |= ANYOF_FOLD;
2342 flags |= ANYOF_LOCALE;
2343 listsv = newSVpvn("# comment\n",10);
2346 if (*PL_regcomp_parse == ']' || *PL_regcomp_parse == '-')
2347 goto skipcond; /* allow 1st char to be ] or - */
2349 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != ']') {
2351 value = utf8_to_uv((U8*)PL_regcomp_parse, &numlen);
2352 PL_regcomp_parse += numlen;
2355 (void)regpposixcc(value); /* ignore the return value for now */
2356 else if (value == '\\') {
2357 value = utf8_to_uv((U8*)PL_regcomp_parse, &numlen);
2358 PL_regcomp_parse += numlen;
2363 flags |= ANYOF_ALNUML;
2365 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsAlnum\n");
2372 flags |= ANYOF_NALNUML;
2374 Perl_sv_catpvf(aTHX_ listsv,
2375 "-utf8::IsAlpha\n-utf8::IsDigit\n0000\t%04x\n%04x\tffff\n",
2384 flags |= ANYOF_SPACEL;
2385 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsSpace\n");
2387 is_utf8_space((U8*)" ");
2394 flags |= ANYOF_NSPACEL;
2395 Perl_sv_catpvf(aTHX_ listsv,
2396 "!utf8::IsSpace\n");
2398 is_utf8_space((U8*)" ");
2404 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsDigit\n");
2410 Perl_sv_catpvf(aTHX_ listsv,
2411 "!utf8::IsDigit\n");
2417 if (*PL_regcomp_parse == '{') {
2418 e = strchr(PL_regcomp_parse++, '}');
2420 FAIL("Missing right brace on \\p{}");
2421 n = e - PL_regcomp_parse;
2424 e = PL_regcomp_parse;
2429 Perl_sv_catpvf(aTHX_ listsv, "+utf8::%.*s\n", n, PL_regcomp_parse);
2431 Perl_sv_catpvf(aTHX_ listsv,
2432 "!utf8::%.*s\n", n, PL_regcomp_parse);
2434 PL_regcomp_parse = e + 1;
2459 if (*PL_regcomp_parse == '{') {
2460 e = strchr(PL_regcomp_parse++, '}');
2462 FAIL("Missing right brace on \\x{}");
2463 value = scan_hex(PL_regcomp_parse, e - PL_regcomp_parse, &numlen);
2464 PL_regcomp_parse = e + 1;
2467 value = scan_hex(PL_regcomp_parse, 2, &numlen);
2468 PL_regcomp_parse += numlen;
2472 value = UCHARAT(PL_regcomp_parse++);
2473 value = toCTRL(value);
2475 case '0': case '1': case '2': case '3': case '4':
2476 case '5': case '6': case '7': case '8': case '9':
2477 value = scan_oct(--PL_regcomp_parse, 3, &numlen);
2478 PL_regcomp_parse += numlen;
2483 if (lastvalue > value)
2484 FAIL("invalid [] range in regexp");
2486 Perl_sv_catpvf(aTHX_ listsv, "%04x\t%04x\n", lastvalue, value);
2492 if (*PL_regcomp_parse == '-' && PL_regcomp_parse+1 < PL_regxend &&
2493 PL_regcomp_parse[1] != ']') {
2496 continue; /* do it next time */
2499 Perl_sv_catpvf(aTHX_ listsv, "%04x\n", value);
2503 ret = reganode(ANYOFUTF8, 0);
2506 SV *rv = swash_init("utf8", "", listsv, 1, 0);
2507 SvREFCNT_dec(listsv);
2508 n = add_data(1,"s");
2509 PL_regcomp_rx->data->data[n] = (void*)rv;
2510 ARG1_SET(ret, flags);
2521 char* retval = PL_regcomp_parse++;
2524 if (*PL_regcomp_parse == '(' && PL_regcomp_parse[1] == '?' &&
2525 PL_regcomp_parse[2] == '#') {
2526 while (*PL_regcomp_parse && *PL_regcomp_parse != ')')
2531 if (PL_regflags & PMf_EXTENDED) {
2532 if (isSPACE(*PL_regcomp_parse)) {
2536 else if (*PL_regcomp_parse == '#') {
2537 while (*PL_regcomp_parse && *PL_regcomp_parse != '\n')
2548 - reg_node - emit a node
2550 STATIC regnode * /* Location. */
2551 S_reg_node(pTHX_ U8 op)
2554 register regnode *ret;
2555 register regnode *ptr;
2559 SIZE_ALIGN(PL_regsize);
2564 NODE_ALIGN_FILL(ret);
2566 FILL_ADVANCE_NODE(ptr, op);
2573 - reganode - emit a node with an argument
2575 STATIC regnode * /* Location. */
2576 S_reganode(pTHX_ U8 op, U32 arg)
2579 register regnode *ret;
2580 register regnode *ptr;
2584 SIZE_ALIGN(PL_regsize);
2589 NODE_ALIGN_FILL(ret);
2591 FILL_ADVANCE_NODE_ARG(ptr, op, arg);
2598 - regc - emit (if appropriate) a Unicode character
2601 S_reguni(pTHX_ UV uv, char* s, I32* lenp)
2606 *lenp = uv_to_utf8(tmpbuf, uv) - tmpbuf;
2609 *lenp = uv_to_utf8((U8*)s, uv) - (U8*)s;
2614 - regc - emit (if appropriate) a byte of code
2617 S_regc(pTHX_ U8 b, char* s)
2625 - reginsert - insert an operator in front of already-emitted operand
2627 * Means relocating the operand.
2630 S_reginsert(pTHX_ U8 op, regnode *opnd)
2633 register regnode *src;
2634 register regnode *dst;
2635 register regnode *place;
2636 register int offset = regarglen[(U8)op];
2638 /* (PL_regkind[(U8)op] == CURLY ? EXTRA_STEP_2ARGS : 0); */
2641 PL_regsize += NODE_STEP_REGNODE + offset;
2646 PL_regcode += NODE_STEP_REGNODE + offset;
2649 StructCopy(--src, --dst, regnode);
2651 place = opnd; /* Op node, where operand used to be. */
2652 src = NEXTOPER(place);
2653 FILL_ADVANCE_NODE(place, op);
2654 Zero(src, offset, regnode);
2658 - regtail - set the next-pointer at the end of a node chain of p to val.
2661 S_regtail(pTHX_ regnode *p, regnode *val)
2664 register regnode *scan;
2665 register regnode *temp;
2666 register I32 offset;
2671 /* Find last node. */
2674 temp = regnext(scan);
2680 if (reg_off_by_arg[OP(scan)]) {
2681 ARG_SET(scan, val - scan);
2684 NEXT_OFF(scan) = val - scan;
2689 - regoptail - regtail on operand of first argument; nop if operandless
2692 S_regoptail(pTHX_ regnode *p, regnode *val)
2695 /* "Operandless" and "op != BRANCH" are synonymous in practice. */
2696 if (p == NULL || SIZE_ONLY)
2698 if (PL_regkind[(U8)OP(p)] == BRANCH) {
2699 regtail(NEXTOPER(p), val);
2701 else if ( PL_regkind[(U8)OP(p)] == BRANCHJ) {
2702 regtail(NEXTOPER(NEXTOPER(p)), val);
2709 - regcurly - a little FSA that accepts {\d+,?\d*}
2712 S_regcurly(pTHX_ register char *s)
2731 S_dumpuntil(pTHX_ regnode *start, regnode *node, regnode *last, SV* sv, I32 l)
2734 register char op = EXACT; /* Arbitrary non-END op. */
2735 register regnode *next, *onode;
2737 while (op != END && (!last || node < last)) {
2738 /* While that wasn't END last time... */
2744 next = regnext(node);
2746 if (OP(node) == OPTIMIZED)
2749 PerlIO_printf(Perl_debug_log, "%4d:%*s%s", node - start,
2750 2*l + 1, "", SvPVX(sv));
2751 if (next == NULL) /* Next ptr. */
2752 PerlIO_printf(Perl_debug_log, "(0)");
2754 PerlIO_printf(Perl_debug_log, "(%d)", next - start);
2755 (void)PerlIO_putc(Perl_debug_log, '\n');
2757 if (PL_regkind[(U8)op] == BRANCHJ) {
2758 register regnode *nnode = (OP(next) == LONGJMP
2761 if (last && nnode > last)
2763 node = dumpuntil(start, NEXTOPER(NEXTOPER(node)), nnode, sv, l + 1);
2765 else if (PL_regkind[(U8)op] == BRANCH) {
2766 node = dumpuntil(start, NEXTOPER(node), next, sv, l + 1);
2768 else if ( op == CURLY) { /* `next' might be very big: optimizer */
2769 node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
2770 NEXTOPER(node) + EXTRA_STEP_2ARGS + 1, sv, l + 1);
2772 else if (PL_regkind[(U8)op] == CURLY && op != CURLYX) {
2773 node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
2776 else if ( op == PLUS || op == STAR) {
2777 node = dumpuntil(start, NEXTOPER(node), NEXTOPER(node) + 1, sv, l + 1);
2779 else if (op == ANYOF) {
2780 node = NEXTOPER(node);
2783 else if (PL_regkind[(U8)op] == EXACT) {
2784 /* Literal string, where present. */
2785 node += ((*OPERAND(node)) + 2 + sizeof(regnode) - 1) / sizeof(regnode);
2786 node = NEXTOPER(node);
2789 node = NEXTOPER(node);
2790 node += regarglen[(U8)op];
2792 if (op == CURLYX || op == OPEN)
2794 else if (op == WHILEM)
2797 #endif /* DEBUGGING */
2802 - regdump - dump a regexp onto Perl_debug_log in vaguely comprehensible form
2805 Perl_regdump(pTHX_ regexp *r)
2809 SV *sv = sv_newmortal();
2811 (void)dumpuntil(r->program, r->program + 1, NULL, sv, 0);
2813 /* Header fields of interest. */
2814 if (r->anchored_substr)
2815 PerlIO_printf(Perl_debug_log, "anchored `%s%s%s'%s at %d ",
2817 SvPVX(r->anchored_substr),
2819 SvTAIL(r->anchored_substr) ? "$" : "",
2820 r->anchored_offset);
2821 if (r->float_substr)
2822 PerlIO_printf(Perl_debug_log, "floating `%s%s%s'%s at %d..%u ",
2824 SvPVX(r->float_substr),
2826 SvTAIL(r->float_substr) ? "$" : "",
2827 r->float_min_offset, r->float_max_offset);
2828 if (r->check_substr)
2829 PerlIO_printf(Perl_debug_log,
2830 r->check_substr == r->float_substr
2831 ? "(checking floating" : "(checking anchored");
2832 if (r->reganch & ROPT_NOSCAN)
2833 PerlIO_printf(Perl_debug_log, " noscan");
2834 if (r->reganch & ROPT_CHECK_ALL)
2835 PerlIO_printf(Perl_debug_log, " isall");
2836 if (r->check_substr)
2837 PerlIO_printf(Perl_debug_log, ") ");
2839 if (r->regstclass) {
2840 regprop(sv, r->regstclass);
2841 PerlIO_printf(Perl_debug_log, "stclass `%s' ", SvPVX(sv));
2843 if (r->reganch & ROPT_ANCH) {
2844 PerlIO_printf(Perl_debug_log, "anchored");
2845 if (r->reganch & ROPT_ANCH_BOL)
2846 PerlIO_printf(Perl_debug_log, "(BOL)");
2847 if (r->reganch & ROPT_ANCH_MBOL)
2848 PerlIO_printf(Perl_debug_log, "(MBOL)");
2849 if (r->reganch & ROPT_ANCH_GPOS)
2850 PerlIO_printf(Perl_debug_log, "(GPOS)");
2851 PerlIO_putc(Perl_debug_log, ' ');
2853 if (r->reganch & ROPT_GPOS_SEEN)
2854 PerlIO_printf(Perl_debug_log, "GPOS ");
2855 if (r->reganch & ROPT_SKIP)
2856 PerlIO_printf(Perl_debug_log, "plus ");
2857 if (r->reganch & ROPT_IMPLICIT)
2858 PerlIO_printf(Perl_debug_log, "implicit ");
2859 PerlIO_printf(Perl_debug_log, "minlen %ld ", (long) r->minlen);
2860 if (r->reganch & ROPT_EVAL_SEEN)
2861 PerlIO_printf(Perl_debug_log, "with eval ");
2862 PerlIO_printf(Perl_debug_log, "\n");
2863 #endif /* DEBUGGING */
2867 - regprop - printable representation of opcode
2870 Perl_regprop(pTHX_ SV *sv, regnode *o)
2876 sv_setpvn(sv, "", 0);
2877 if (OP(o) >= reg_num) /* regnode.type is unsigned */
2878 FAIL("corrupted regexp opcode");
2879 sv_catpv(sv, (char*)reg_name[OP(o)]); /* Take off const! */
2881 k = PL_regkind[(U8)OP(o)];
2884 Perl_sv_catpvf(aTHX_ sv, " <%s%s%s>", PL_colors[0], OPERAND(o) + 1, PL_colors[1]);
2885 else if (k == CURLY) {
2886 if (OP(o) == CURLYM || OP(o) == CURLYN)
2887 Perl_sv_catpvf(aTHX_ sv, "[%d]", o->flags); /* Parenth number */
2888 Perl_sv_catpvf(aTHX_ sv, " {%d,%d}", ARG1(o), ARG2(o));
2890 else if (k == REF || k == OPEN || k == CLOSE || k == GROUPP )
2891 Perl_sv_catpvf(aTHX_ sv, "%d", ARG(o)); /* Parenth number */
2892 else if (k == LOGICAL)
2893 Perl_sv_catpvf(aTHX_ sv, "[%d]", ARG(o)); /* 2: embedded, otherwise 1 */
2894 else if (k == BRANCHJ && (OP(o) == UNLESSM || OP(o) == IFMATCH))
2895 Perl_sv_catpvf(aTHX_ sv, "[-%d]", o->flags);
2896 #endif /* DEBUGGING */
2900 Perl_pregfree(pTHX_ struct regexp *r)
2903 if (!r || (--r->refcnt > 0))
2906 Safefree(r->precomp);
2907 if (RX_MATCH_COPIED(r))
2908 Safefree(r->subbeg);
2910 if (r->anchored_substr)
2911 SvREFCNT_dec(r->anchored_substr);
2912 if (r->float_substr)
2913 SvREFCNT_dec(r->float_substr);
2914 Safefree(r->substrs);
2917 int n = r->data->count;
2918 AV* new_comppad = NULL;
2923 switch (r->data->what[n]) {
2925 SvREFCNT_dec((SV*)r->data->data[n]);
2928 new_comppad = (AV*)r->data->data[n];
2931 if (new_comppad == NULL)
2932 Perl_croak(aTHX_ "panic: pregfree comppad");
2933 old_comppad = PL_comppad;
2934 old_curpad = PL_curpad;
2935 PL_comppad = new_comppad;
2936 PL_curpad = AvARRAY(new_comppad);
2937 op_free((OP_4tree*)r->data->data[n]);
2938 PL_comppad = old_comppad;
2939 PL_curpad = old_curpad;
2940 SvREFCNT_dec((SV*)new_comppad);
2946 FAIL2("panic: regfree data code '%c'", r->data->what[n]);
2949 Safefree(r->data->what);
2952 Safefree(r->startp);
2958 - regnext - dig the "next" pointer out of a node
2960 * [Note, when REGALIGN is defined there are two places in regmatch()
2961 * that bypass this code for speed.]
2964 Perl_regnext(pTHX_ register regnode *p)
2967 register I32 offset;
2969 if (p == &PL_regdummy)
2972 offset = (reg_off_by_arg[OP(p)] ? ARG(p) : NEXT_OFF(p));
2980 S_re_croak2(pTHX_ const char* pat1,const char* pat2,...)
2983 STRLEN l1 = strlen(pat1);
2984 STRLEN l2 = strlen(pat2);
2993 Copy(pat1, buf, l1 , char);
2994 Copy(pat2, buf + l1, l2 , char);
2995 buf[l1 + l2] = '\n';
2996 buf[l1 + l2 + 1] = '\0';
2998 /* ANSI variant takes additional second argument */
2999 va_start(args, pat2);
3003 msv = mess(buf, &args);
3005 message = SvPV(msv,l1);
3008 Copy(message, buf, l1 , char);
3009 buf[l1] = '\0'; /* Overwrite \n */
3010 Perl_croak(aTHX_ "%s", buf);
3013 /* XXX Here's a total kludge. But we need to re-enter for swash routines. */
3016 Perl_save_re_context(pTHX)
3020 SAVEPPTR(PL_regprecomp); /* uncompiled string. */
3021 SAVEI32(PL_regnpar); /* () count. */
3022 SAVEI32(PL_regsize); /* Code size. */
3023 SAVEI16(PL_regflags); /* are we folding, multilining? */
3024 SAVEPPTR(PL_reginput); /* String-input pointer. */
3025 SAVEPPTR(PL_regbol); /* Beginning of input, for ^ check. */
3026 SAVEPPTR(PL_regeol); /* End of input, for $ check. */
3027 SAVESPTR(PL_regstartp); /* Pointer to startp array. */
3028 SAVESPTR(PL_regendp); /* Ditto for endp. */
3029 SAVESPTR(PL_reglastparen); /* Similarly for lastparen. */
3030 SAVEPPTR(PL_regtill); /* How far we are required to go. */
3031 SAVEI32(PL_regprev); /* char before regbol, \n if none */
3032 SAVESPTR(PL_reg_start_tmp); /* from regexec.c */
3033 PL_reg_start_tmp = 0;
3034 SAVEFREEPV(PL_reg_start_tmp);
3035 SAVEI32(PL_reg_start_tmpl); /* from regexec.c */
3036 PL_reg_start_tmpl = 0;
3037 SAVESPTR(PL_regdata);
3038 SAVEI32(PL_reg_flags); /* from regexec.c */
3039 SAVEI32(PL_reg_eval_set); /* from regexec.c */
3040 SAVEI32(PL_regnarrate); /* from regexec.c */
3041 SAVESPTR(PL_regprogram); /* from regexec.c */
3042 SAVEINT(PL_regindent); /* from regexec.c */
3043 SAVESPTR(PL_regcc); /* from regexec.c */
3044 SAVESPTR(PL_curcop);
3045 SAVESPTR(PL_regcomp_rx); /* from regcomp.c */
3046 SAVEI32(PL_regseen); /* from regcomp.c */
3047 SAVEI32(PL_regsawback); /* Did we see \1, ...? */
3048 SAVEI32(PL_regnaughty); /* How bad is this pattern? */
3049 SAVESPTR(PL_regcode); /* Code-emit pointer; ®dummy = don't */
3050 SAVEPPTR(PL_regxend); /* End of input for compile */
3051 SAVEPPTR(PL_regcomp_parse); /* Input-scan pointer. */
3052 SAVESPTR(PL_reg_call_cc); /* from regexec.c */
3053 SAVESPTR(PL_reg_re); /* from regexec.c */
3054 SAVEPPTR(PL_reg_ganch); /* from regexec.c */
3055 SAVESPTR(PL_reg_sv); /* from regexec.c */
3056 SAVESPTR(PL_reg_magic); /* from regexec.c */
3057 SAVEI32(PL_reg_oldpos); /* from regexec.c */
3058 SAVESPTR(PL_reg_oldcurpm); /* from regexec.c */
3059 SAVESPTR(PL_reg_curpm); /* from regexec.c */
3061 SAVEPPTR(PL_reg_starttry); /* from regexec.c */