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 */
28 # if defined(PERL_EXT_RE_DEBUG) && !defined(DEBUGGING)
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 # define Perl_pregfree my_regfree
39 # define Perl_re_intuit_string my_re_intuit_string
40 /* *These* symbols are masked to allow static link. */
41 # define Perl_regnext my_regnext
42 # define Perl_save_re_context my_save_re_context
43 # define Perl_reginitcolors my_reginitcolors
48 * pregcomp and pregexec -- regsub and regerror are not used in perl
50 * Copyright (c) 1986 by University of Toronto.
51 * Written by Henry Spencer. Not derived from licensed software.
53 * Permission is granted to anyone to use this software for any
54 * purpose on any computer system, and to redistribute it freely,
55 * subject to the following restrictions:
57 * 1. The author is not responsible for the consequences of use of
58 * this software, no matter how awful, even if they arise
61 * 2. The origin of this software must not be misrepresented, either
62 * by explicit claim or by omission.
64 * 3. Altered versions must be plainly marked as such, and must not
65 * be misrepresented as being the original software.
68 **** Alterations to Henry's code are...
70 **** Copyright (c) 1991-1999, Larry Wall
72 **** You may distribute under the terms of either the GNU General Public
73 **** License or the Artistic License, as specified in the README file.
76 * Beware that some of this code is subtly aware of the way operator
77 * precedence is structured in regular expressions. Serious changes in
78 * regular-expression syntax might require a total rethink.
81 #define PERL_IN_REGCOMP_C
84 #ifndef PERL_IN_XSUB_RE
96 # if defined(BUGGY_MSC6)
97 /* MSC 6.00A breaks on op/regexp.t test 85 unless we turn this off */
98 # pragma optimize("a",off)
99 /* But MSC 6.00A is happy with 'w', for aliases only across function calls*/
100 # pragma optimize("w",on )
101 # endif /* BUGGY_MSC6 */
105 #define STATIC static
108 #define ISMULT1(c) ((c) == '*' || (c) == '+' || (c) == '?')
109 #define ISMULT2(s) ((*s) == '*' || (*s) == '+' || (*s) == '?' || \
110 ((*s) == '{' && regcurly(s)))
112 #define PERL_META "^$.[()|?+*\\"
114 #define META "^$.[()|?+*\\"
118 #undef SPSTART /* dratted cpp namespace... */
121 * Flags to be passed up and down.
123 #define WORST 0 /* Worst case. */
124 #define HASWIDTH 0x1 /* Known to match non-null strings. */
125 #define SIMPLE 0x2 /* Simple enough to be STAR/PLUS operand. */
126 #define SPSTART 0x4 /* Starts with * or +. */
127 #define TRYAGAIN 0x8 /* Weeded out a declaration. */
130 * Forward declarations for pregcomp()'s friends.
133 static scan_data_t zero_scan_data = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
136 #define SF_BEFORE_EOL (SF_BEFORE_SEOL|SF_BEFORE_MEOL)
137 #define SF_BEFORE_SEOL 0x1
138 #define SF_BEFORE_MEOL 0x2
139 #define SF_FIX_BEFORE_EOL (SF_FIX_BEFORE_SEOL|SF_FIX_BEFORE_MEOL)
140 #define SF_FL_BEFORE_EOL (SF_FL_BEFORE_SEOL|SF_FL_BEFORE_MEOL)
143 # define SF_FIX_SHIFT_EOL (0+2)
144 # define SF_FL_SHIFT_EOL (0+4)
146 # define SF_FIX_SHIFT_EOL (+2)
147 # define SF_FL_SHIFT_EOL (+4)
150 #define SF_FIX_BEFORE_SEOL (SF_BEFORE_SEOL << SF_FIX_SHIFT_EOL)
151 #define SF_FIX_BEFORE_MEOL (SF_BEFORE_MEOL << SF_FIX_SHIFT_EOL)
153 #define SF_FL_BEFORE_SEOL (SF_BEFORE_SEOL << SF_FL_SHIFT_EOL)
154 #define SF_FL_BEFORE_MEOL (SF_BEFORE_MEOL << SF_FL_SHIFT_EOL) /* 0x20 */
155 #define SF_IS_INF 0x40
156 #define SF_HAS_PAR 0x80
157 #define SF_IN_PAR 0x100
158 #define SF_HAS_EVAL 0x200
159 #define SCF_DO_SUBSTR 0x400
162 #define UTF (PL_reg_flags & RF_utf8)
163 #define LOC (PL_regflags & PMf_LOCALE)
164 #define FOLD (PL_regflags & PMf_FOLD)
166 #define CHR_SVLEN(sv) (UTF ? sv_len_utf8(sv) : SvCUR(sv))
167 #define CHR_DIST(a,b) (UTF ? utf8_distance(a,b) : a - b)
170 S_clear_re(pTHX_ void *r)
172 ReREFCNT_dec((regexp *)r);
176 S_scan_commit(pTHX_ scan_data_t *data)
179 STRLEN l = CHR_SVLEN(data->last_found);
180 STRLEN old_l = CHR_SVLEN(*data->longest);
182 if ((l >= old_l) && ((l > old_l) || (data->flags & SF_BEFORE_EOL))) {
183 sv_setsv(*data->longest, data->last_found);
184 if (*data->longest == data->longest_fixed) {
185 data->offset_fixed = l ? data->last_start_min : data->pos_min;
186 if (data->flags & SF_BEFORE_EOL)
188 |= ((data->flags & SF_BEFORE_EOL) << SF_FIX_SHIFT_EOL);
190 data->flags &= ~SF_FIX_BEFORE_EOL;
193 data->offset_float_min = l ? data->last_start_min : data->pos_min;
194 data->offset_float_max = (l
195 ? data->last_start_max
196 : data->pos_min + data->pos_delta);
197 if (data->flags & SF_BEFORE_EOL)
199 |= ((data->flags & SF_BEFORE_EOL) << SF_FL_SHIFT_EOL);
201 data->flags &= ~SF_FL_BEFORE_EOL;
204 SvCUR_set(data->last_found, 0);
206 data->flags &= ~SF_BEFORE_EOL;
209 /* Stops at toplevel WHILEM as well as at `last'. At end *scanp is set
210 to the position after last scanned or to NULL. */
213 S_study_chunk(pTHX_ regnode **scanp, I32 *deltap, regnode *last, scan_data_t *data, U32 flags)
214 /* scanp: Start here (read-write). */
215 /* deltap: Write maxlen-minlen here. */
216 /* last: Stop before this one. */
219 I32 min = 0, pars = 0, code;
220 regnode *scan = *scanp, *next;
222 int is_inf = (flags & SCF_DO_SUBSTR) && (data->flags & SF_IS_INF);
223 int is_inf_internal = 0; /* The studied chunk is infinite */
224 I32 is_par = OP(scan) == OPEN ? ARG(scan) : 0;
225 scan_data_t data_fake;
227 while (scan && OP(scan) != END && scan < last) {
228 /* Peephole optimizer: */
230 if (PL_regkind[(U8)OP(scan)] == EXACT) {
231 regnode *n = regnext(scan);
234 regnode *stop = scan;
237 next = scan + (*OPERAND(scan) + 2 - 1)/sizeof(regnode) + 2;
238 /* Skip NOTHING, merge EXACT*. */
240 ( PL_regkind[(U8)OP(n)] == NOTHING ||
241 (stringok && (OP(n) == OP(scan))))
243 && NEXT_OFF(scan) + NEXT_OFF(n) < I16_MAX) {
244 if (OP(n) == TAIL || n > next)
246 if (PL_regkind[(U8)OP(n)] == NOTHING) {
247 NEXT_OFF(scan) += NEXT_OFF(n);
248 next = n + NODE_STEP_REGNODE;
256 int oldl = *OPERAND(scan);
257 regnode *nnext = regnext(n);
259 if (oldl + *OPERAND(n) > U8_MAX)
261 NEXT_OFF(scan) += NEXT_OFF(n);
262 *OPERAND(scan) += *OPERAND(n);
263 next = n + (*OPERAND(n) + 2 - 1)/sizeof(regnode) + 2;
264 /* Now we can overwrite *n : */
265 Move(OPERAND(n) + 1, OPERAND(scan) + oldl + 1,
266 *OPERAND(n) + 1, char);
276 n = scan + (*OPERAND(scan) + 2 - 1)/sizeof(regnode) + 2;
278 /* Purify reports a benign UMR here sometimes, because we
279 * don't initialize the OP() slot of a node when that node
280 * is occupied by just the trailing null of the string in
282 if (PL_regkind[(U8)OP(n)] != NOTHING || OP(n) == NOTHING) {
291 if (OP(scan) != CURLYX) {
292 int max = (reg_off_by_arg[OP(scan)]
294 /* I32 may be smaller than U16 on CRAYs! */
295 : (I32_MAX < U16_MAX ? I32_MAX : U16_MAX));
296 int off = (reg_off_by_arg[OP(scan)] ? ARG(scan) : NEXT_OFF(scan));
300 /* Skip NOTHING and LONGJMP. */
301 while ((n = regnext(n))
302 && ((PL_regkind[(U8)OP(n)] == NOTHING && (noff = NEXT_OFF(n)))
303 || ((OP(n) == LONGJMP) && (noff = ARG(n))))
306 if (reg_off_by_arg[OP(scan)])
309 NEXT_OFF(scan) = off;
311 if (OP(scan) == BRANCH || OP(scan) == BRANCHJ
312 || OP(scan) == IFTHEN || OP(scan) == SUSPEND) {
313 next = regnext(scan);
316 if (OP(next) == code || code == IFTHEN || code == SUSPEND) {
317 I32 max1 = 0, min1 = I32_MAX, num = 0;
319 if (flags & SCF_DO_SUBSTR)
321 while (OP(scan) == code) {
322 I32 deltanext, minnext;
326 next = regnext(scan);
327 scan = NEXTOPER(scan);
329 scan = NEXTOPER(scan);
330 /* We suppose the run is continuous, last=next...*/
331 minnext = study_chunk(&scan, &deltanext, next,
335 if (max1 < minnext + deltanext)
336 max1 = minnext + deltanext;
337 if (deltanext == I32_MAX)
338 is_inf = is_inf_internal = 1;
340 if (data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
342 if (data && (data_fake.flags & SF_HAS_EVAL))
343 data->flags |= SF_HAS_EVAL;
347 if (code == IFTHEN && num < 2) /* Empty ELSE branch */
349 if (flags & SCF_DO_SUBSTR) {
350 data->pos_min += min1;
351 data->pos_delta += max1 - min1;
352 if (max1 != min1 || is_inf)
353 data->longest = &(data->longest_float);
356 delta += max1 - min1;
358 else if (code == BRANCHJ) /* single branch is optimized. */
359 scan = NEXTOPER(NEXTOPER(scan));
360 else /* single branch is optimized. */
361 scan = NEXTOPER(scan);
364 else if (OP(scan) == EXACT) {
365 I32 l = *OPERAND(scan);
367 unsigned char *s = (unsigned char *)(OPERAND(scan)+1);
368 unsigned char *e = s + l;
377 if (flags & SCF_DO_SUBSTR) { /* Update longest substr. */
378 /* The code below prefers earlier match for fixed
379 offset, later match for variable offset. */
380 if (data->last_end == -1) { /* Update the start info. */
381 data->last_start_min = data->pos_min;
382 data->last_start_max = is_inf
383 ? I32_MAX : data->pos_min + data->pos_delta;
385 sv_catpvn(data->last_found, (char *)(OPERAND(scan)+1), *OPERAND(scan));
386 data->last_end = data->pos_min + l;
387 data->pos_min += l; /* As in the first entry. */
388 data->flags &= ~SF_BEFORE_EOL;
391 else if (PL_regkind[(U8)OP(scan)] == EXACT) {
392 I32 l = *OPERAND(scan);
393 if (flags & SCF_DO_SUBSTR)
396 unsigned char *s = (unsigned char *)(OPERAND(scan)+1);
397 unsigned char *e = s + l;
406 if (data && (flags & SCF_DO_SUBSTR))
409 else if (strchr(PL_varies,OP(scan))) {
410 I32 mincount, maxcount, minnext, deltanext, pos_before, fl;
411 regnode *oscan = scan;
413 switch (PL_regkind[(U8)OP(scan)]) {
415 scan = NEXTOPER(scan);
418 if (flags & SCF_DO_SUBSTR) {
419 next = NEXTOPER(scan);
420 if (OP(next) == EXACT) {
422 maxcount = REG_INFTY;
423 next = regnext(scan);
424 scan = NEXTOPER(scan);
428 if (flags & SCF_DO_SUBSTR)
433 is_inf = is_inf_internal = 1;
434 scan = regnext(scan);
435 if (flags & SCF_DO_SUBSTR) {
437 data->longest = &(data->longest_float);
439 goto optimize_curly_tail;
441 mincount = ARG1(scan);
442 maxcount = ARG2(scan);
443 next = regnext(scan);
444 scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
446 if (flags & SCF_DO_SUBSTR) {
447 if (mincount == 0) scan_commit(data);
448 pos_before = data->pos_min;
452 data->flags &= ~(SF_HAS_PAR|SF_IN_PAR|SF_HAS_EVAL);
454 data->flags |= SF_IS_INF;
456 /* This will finish on WHILEM, setting scan, or on NULL: */
457 minnext = study_chunk(&scan, &deltanext, last, data,
459 ? (flags & ~SCF_DO_SUBSTR) : flags);
460 if (!scan) /* It was not CURLYX, but CURLY. */
462 if (ckWARN(WARN_UNSAFE) && (minnext + deltanext == 0)
463 && !(data->flags & (SF_HAS_PAR|SF_IN_PAR))
464 && maxcount <= REG_INFTY/3) /* Complement check for big count */
465 Perl_warner(aTHX_ WARN_UNSAFE, "Strange *+?{} on zero-length expression");
466 min += minnext * mincount;
467 is_inf_internal |= (maxcount == REG_INFTY
468 && (minnext + deltanext) > 0
469 || deltanext == I32_MAX);
470 is_inf |= is_inf_internal;
471 delta += (minnext + deltanext) * maxcount - minnext * mincount;
473 /* Try powerful optimization CURLYX => CURLYN. */
474 if ( OP(oscan) == CURLYX && data
475 && data->flags & SF_IN_PAR
476 && !(data->flags & SF_HAS_EVAL)
477 && !deltanext && minnext == 1 ) {
478 /* Try to optimize to CURLYN. */
479 regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS;
480 regnode *nxt1 = nxt, *nxt2;
484 if (!strchr(PL_simple,OP(nxt))
485 && !(PL_regkind[(U8)OP(nxt)] == EXACT
486 && *OPERAND(nxt) == 1))
490 if (OP(nxt) != CLOSE)
492 /* Now we know that nxt2 is the only contents: */
493 oscan->flags = ARG(nxt);
495 OP(nxt1) = NOTHING; /* was OPEN. */
497 OP(nxt1 + 1) = OPTIMIZED; /* was count. */
498 NEXT_OFF(nxt1+ 1) = 0; /* just for consistancy. */
499 NEXT_OFF(nxt2) = 0; /* just for consistancy with CURLY. */
500 OP(nxt) = OPTIMIZED; /* was CLOSE. */
501 OP(nxt + 1) = OPTIMIZED; /* was count. */
502 NEXT_OFF(nxt+ 1) = 0; /* just for consistancy. */
507 /* Try optimization CURLYX => CURLYM. */
508 if ( OP(oscan) == CURLYX && data
509 && !(data->flags & SF_HAS_PAR)
510 && !(data->flags & SF_HAS_EVAL)
512 /* XXXX How to optimize if data == 0? */
513 /* Optimize to a simpler form. */
514 regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN */
518 while ( (nxt2 = regnext(nxt)) /* skip over embedded stuff*/
519 && (OP(nxt2) != WHILEM))
521 OP(nxt2) = SUCCEED; /* Whas WHILEM */
522 /* Need to optimize away parenths. */
523 if (data->flags & SF_IN_PAR) {
524 /* Set the parenth number. */
525 regnode *nxt1 = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN*/
527 if (OP(nxt) != CLOSE)
528 FAIL("panic opt close");
529 oscan->flags = ARG(nxt);
530 OP(nxt1) = OPTIMIZED; /* was OPEN. */
531 OP(nxt) = OPTIMIZED; /* was CLOSE. */
533 OP(nxt1 + 1) = OPTIMIZED; /* was count. */
534 OP(nxt + 1) = OPTIMIZED; /* was count. */
535 NEXT_OFF(nxt1 + 1) = 0; /* just for consistancy. */
536 NEXT_OFF(nxt + 1) = 0; /* just for consistancy. */
539 while ( nxt1 && (OP(nxt1) != WHILEM)) {
540 regnode *nnxt = regnext(nxt1);
543 if (reg_off_by_arg[OP(nxt1)])
544 ARG_SET(nxt1, nxt2 - nxt1);
545 else if (nxt2 - nxt1 < U16_MAX)
546 NEXT_OFF(nxt1) = nxt2 - nxt1;
548 OP(nxt) = NOTHING; /* Cannot beautify */
553 /* Optimize again: */
554 study_chunk(&nxt1, &deltanext, nxt, NULL, 0);
559 if (data && fl & (SF_HAS_PAR|SF_IN_PAR))
561 if (flags & SCF_DO_SUBSTR) {
562 SV *last_str = Nullsv;
563 int counted = mincount != 0;
565 if (data->last_end > 0 && mincount != 0) { /* Ends with a string. */
566 I32 b = pos_before >= data->last_start_min
567 ? pos_before : data->last_start_min;
569 char *s = SvPV(data->last_found, l);
570 I32 old = b - data->last_start_min;
573 old = utf8_hop((U8*)s, old) - (U8*)s;
576 /* Get the added string: */
577 last_str = newSVpvn(s + old, l);
578 if (deltanext == 0 && pos_before == b) {
579 /* What was added is a constant string */
581 SvGROW(last_str, (mincount * l) + 1);
582 repeatcpy(SvPVX(last_str) + l,
583 SvPVX(last_str), l, mincount - 1);
584 SvCUR(last_str) *= mincount;
585 /* Add additional parts. */
586 SvCUR_set(data->last_found,
587 SvCUR(data->last_found) - l);
588 sv_catsv(data->last_found, last_str);
589 data->last_end += l * (mincount - 1);
593 /* It is counted once already... */
594 data->pos_min += minnext * (mincount - counted);
595 data->pos_delta += - counted * deltanext +
596 (minnext + deltanext) * maxcount - minnext * mincount;
597 if (mincount != maxcount) {
599 if (mincount && last_str) {
600 sv_setsv(data->last_found, last_str);
601 data->last_end = data->pos_min;
602 data->last_start_min =
603 data->pos_min - CHR_SVLEN(last_str);
604 data->last_start_max = is_inf
606 : data->pos_min + data->pos_delta
607 - CHR_SVLEN(last_str);
609 data->longest = &(data->longest_float);
611 SvREFCNT_dec(last_str);
613 if (data && (fl & SF_HAS_EVAL))
614 data->flags |= SF_HAS_EVAL;
616 if (OP(oscan) != CURLYX) {
617 while (PL_regkind[(U8)OP(next = regnext(oscan))] == NOTHING
619 NEXT_OFF(oscan) += NEXT_OFF(next);
622 default: /* REF only? */
623 if (flags & SCF_DO_SUBSTR) {
625 data->longest = &(data->longest_float);
627 is_inf = is_inf_internal = 1;
631 else if (strchr(PL_simple,OP(scan)) || PL_regkind[(U8)OP(scan)] == ANYUTF8) {
632 if (flags & SCF_DO_SUBSTR) {
638 else if (PL_regkind[(U8)OP(scan)] == EOL && flags & SCF_DO_SUBSTR) {
639 data->flags |= (OP(scan) == MEOL
643 else if (PL_regkind[(U8)OP(scan)] == BRANCHJ
644 && (scan->flags || data)
645 && (OP(scan) == IFMATCH || OP(scan) == UNLESSM)) {
646 I32 deltanext, minnext;
650 next = regnext(scan);
651 nscan = NEXTOPER(NEXTOPER(scan));
652 minnext = study_chunk(&nscan, &deltanext, last, &data_fake, 0);
655 FAIL("variable length lookbehind not implemented");
657 else if (minnext > U8_MAX) {
658 FAIL2("lookbehind longer than %d not implemented", U8_MAX);
660 scan->flags = minnext;
662 if (data && data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
664 if (data && (data_fake.flags & SF_HAS_EVAL))
665 data->flags |= SF_HAS_EVAL;
667 else if (OP(scan) == OPEN) {
670 else if (OP(scan) == CLOSE && ARG(scan) == is_par) {
671 next = regnext(scan);
673 if ( next && (OP(next) != WHILEM) && next < last)
674 is_par = 0; /* Disable optimization */
676 else if (OP(scan) == EVAL) {
678 data->flags |= SF_HAS_EVAL;
680 else if (OP(scan) == LOGICAL && scan->flags == 2) { /* Embedded */
681 if (flags & SCF_DO_SUBSTR) {
683 data->longest = &(data->longest_float);
685 is_inf = is_inf_internal = 1;
687 /* Else: zero-length, ignore. */
688 scan = regnext(scan);
693 *deltap = is_inf_internal ? I32_MAX : delta;
694 if (flags & SCF_DO_SUBSTR && is_inf)
695 data->pos_delta = I32_MAX - data->pos_min;
698 if (is_par && pars==1 && data) {
699 data->flags |= SF_IN_PAR;
700 data->flags &= ~SF_HAS_PAR;
702 else if (pars && data) {
703 data->flags |= SF_HAS_PAR;
704 data->flags &= ~SF_IN_PAR;
710 S_add_data(pTHX_ I32 n, char *s)
713 if (PL_regcomp_rx->data) {
714 Renewc(PL_regcomp_rx->data,
715 sizeof(*PL_regcomp_rx->data) + sizeof(void*) * (PL_regcomp_rx->data->count + n - 1),
716 char, struct reg_data);
717 Renew(PL_regcomp_rx->data->what, PL_regcomp_rx->data->count + n, U8);
718 PL_regcomp_rx->data->count += n;
721 Newc(1207, PL_regcomp_rx->data, sizeof(*PL_regcomp_rx->data) + sizeof(void*) * (n - 1),
722 char, struct reg_data);
723 New(1208, PL_regcomp_rx->data->what, n, U8);
724 PL_regcomp_rx->data->count = n;
726 Copy(s, PL_regcomp_rx->data->what + PL_regcomp_rx->data->count - n, n, U8);
727 return PL_regcomp_rx->data->count - n;
731 Perl_reginitcolors(pTHX)
735 char *s = PerlEnv_getenv("PERL_RE_COLORS");
738 PL_colors[0] = s = savepv(s);
746 PL_colors[i] = s = "";
756 - pregcomp - compile a regular expression into internal code
758 * We can't allocate space until we know how big the compiled form will be,
759 * but we can't compile it (and thus know how big it is) until we've got a
760 * place to put the code. So we cheat: we compile it twice, once with code
761 * generation turned off and size counting turned on, and once "for real".
762 * This also means that we don't allocate space until we are sure that the
763 * thing really will compile successfully, and we never have to move the
764 * code and thus invalidate pointers into it. (Note that it has to be in
765 * one piece because free() must be able to free it all.) [NB: not true in perl]
767 * Beware that the optimization-preparation code in here knows about some
768 * of the structure of the compiled regexp. [I'll say.]
771 Perl_pregcomp(pTHX_ char *exp, char *xend, PMOP *pm)
786 FAIL("NULL regexp argument");
788 if (PL_curcop == &PL_compiling ? (PL_hints & HINT_UTF8) : IN_UTF8)
789 PL_reg_flags |= RF_utf8;
793 PL_regprecomp = savepvn(exp, xend - exp);
794 DEBUG_r(if (!PL_colorset) reginitcolors());
795 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sCompiling%s RE `%s%*s%s'\n",
796 PL_colors[4],PL_colors[5],PL_colors[0],
797 xend - exp, PL_regprecomp, PL_colors[1]));
798 PL_regflags = pm->op_pmflags;
802 PL_seen_zerolen = *exp == '^' ? -1 : 0;
806 /* First pass: determine size, legality. */
807 PL_regcomp_parse = exp;
812 PL_regcode = &PL_regdummy;
813 regc((U8)REG_MAGIC, (char*)PL_regcode);
814 if (reg(0, &flags) == NULL) {
815 Safefree(PL_regprecomp);
816 PL_regprecomp = Nullch;
819 DEBUG_r(PerlIO_printf(Perl_debug_log, "size %d ", PL_regsize));
821 /* Small enough for pointer-storage convention?
822 If extralen==0, this means that we will not need long jumps. */
823 if (PL_regsize >= 0x10000L && PL_extralen)
824 PL_regsize += PL_extralen;
828 /* Allocate space and initialize. */
829 Newc(1001, r, sizeof(regexp) + (unsigned)PL_regsize * sizeof(regnode),
832 FAIL("regexp out of space");
834 r->prelen = xend - exp;
835 r->precomp = PL_regprecomp;
837 r->reganch = pm->op_pmflags & PMf_COMPILETIME;
838 r->nparens = PL_regnpar - 1; /* set early to validate backrefs */
840 r->substrs = 0; /* Useful during FAIL. */
841 r->startp = 0; /* Useful during FAIL. */
842 r->endp = 0; /* Useful during FAIL. */
846 /* Second pass: emit code. */
847 PL_regcomp_parse = exp;
851 PL_regcode = r->program;
852 /* Store the count of eval-groups for security checks: */
853 PL_regcode->next_off = ((PL_seen_evals > U16_MAX) ? U16_MAX : PL_seen_evals);
854 regc((U8)REG_MAGIC, (char*) PL_regcode++);
856 if (reg(0, &flags) == NULL)
859 /* Dig out information for optimizations. */
860 r->reganch = pm->op_pmflags & PMf_COMPILETIME; /* Again? */
861 pm->op_pmflags = PL_regflags;
863 r->reganch |= ROPT_UTF8;
864 r->regstclass = NULL;
865 if (PL_regnaughty >= 10) /* Probably an expensive pattern. */
866 r->reganch |= ROPT_NAUGHTY;
867 scan = r->program + 1; /* First BRANCH. */
869 /* XXXX To minimize changes to RE engine we always allocate
870 3-units-long substrs field. */
871 Newz(1004, r->substrs, 1, struct reg_substr_data);
873 if (OP(scan) != BRANCH) { /* Only one top-level choice. */
876 STRLEN longest_float_length, longest_fixed_length;
878 StructCopy(&zero_scan_data, &data, scan_data_t);
880 /* Skip introductions and multiplicators >= 1. */
881 while ((OP(first) == OPEN && (sawopen = 1)) ||
882 (OP(first) == BRANCH && OP(regnext(first)) != BRANCH) ||
883 (OP(first) == PLUS) ||
884 (OP(first) == MINMOD) ||
885 (PL_regkind[(U8)OP(first)] == CURLY && ARG1(first) > 0) ) {
886 if (OP(first) == PLUS)
889 first += regarglen[(U8)OP(first)];
890 first = NEXTOPER(first);
893 /* Starting-point info. */
895 if (OP(first) == EXACT); /* Empty, get anchored substr later. */
896 else if (strchr(PL_simple+4,OP(first)))
897 r->regstclass = first;
898 else if (PL_regkind[(U8)OP(first)] == BOUND ||
899 PL_regkind[(U8)OP(first)] == NBOUND)
900 r->regstclass = first;
901 else if (PL_regkind[(U8)OP(first)] == BOL) {
902 r->reganch |= (OP(first) == MBOL
907 first = NEXTOPER(first);
910 else if (OP(first) == GPOS) {
911 r->reganch |= ROPT_ANCH_GPOS;
912 first = NEXTOPER(first);
915 else if ((OP(first) == STAR &&
916 PL_regkind[(U8)OP(NEXTOPER(first))] == REG_ANY) &&
917 !(r->reganch & ROPT_ANCH) )
919 /* turn .* into ^.* with an implied $*=1 */
920 int type = OP(NEXTOPER(first));
922 if (type == REG_ANY || type == ANYUTF8)
923 type = ROPT_ANCH_MBOL;
925 type = ROPT_ANCH_SBOL;
927 r->reganch |= type | ROPT_IMPLICIT;
928 first = NEXTOPER(first);
931 if (sawplus && (!sawopen || !PL_regsawback)
932 && !(PL_regseen & REG_SEEN_EVAL)) /* May examine pos and $& */
933 /* x+ must match at the 1st pos of run of x's */
934 r->reganch |= ROPT_SKIP;
936 /* Scan is after the zeroth branch, first is atomic matcher. */
937 DEBUG_r(PerlIO_printf(Perl_debug_log, "first at %d\n",
940 * If there's something expensive in the r.e., find the
941 * longest literal string that must appear and make it the
942 * regmust. Resolve ties in favor of later strings, since
943 * the regstart check works with the beginning of the r.e.
944 * and avoiding duplication strengthens checking. Not a
945 * strong reason, but sufficient in the absence of others.
946 * [Now we resolve ties in favor of the earlier string if
947 * it happens that c_offset_min has been invalidated, since the
948 * earlier string may buy us something the later one won't.]
952 data.longest_fixed = newSVpvn("",0);
953 data.longest_float = newSVpvn("",0);
954 data.last_found = newSVpvn("",0);
955 data.longest = &(data.longest_fixed);
958 minlen = study_chunk(&first, &fake, scan + PL_regsize, /* Up to end */
959 &data, SCF_DO_SUBSTR);
960 if ( PL_regnpar == 1 && data.longest == &(data.longest_fixed)
961 && data.last_start_min == 0 && data.last_end > 0
963 && (!(PL_regseen & REG_SEEN_GPOS) || (r->reganch & ROPT_ANCH_GPOS)))
964 r->reganch |= ROPT_CHECK_ALL;
966 SvREFCNT_dec(data.last_found);
968 longest_float_length = CHR_SVLEN(data.longest_float);
969 if (longest_float_length
970 || (data.flags & SF_FL_BEFORE_EOL
971 && (!(data.flags & SF_FL_BEFORE_MEOL)
972 || (PL_regflags & PMf_MULTILINE)))) {
975 if (SvCUR(data.longest_fixed) /* ok to leave SvCUR */
976 && data.offset_fixed == data.offset_float_min
977 && SvCUR(data.longest_fixed) == SvCUR(data.longest_float))
978 goto remove_float; /* As in (a)+. */
980 r->float_substr = data.longest_float;
981 r->float_min_offset = data.offset_float_min;
982 r->float_max_offset = data.offset_float_max;
983 t = (data.flags & SF_FL_BEFORE_EOL /* Can't have SEOL and MULTI */
984 && (!(data.flags & SF_FL_BEFORE_MEOL)
985 || (PL_regflags & PMf_MULTILINE)));
986 fbm_compile(r->float_substr, t ? FBMcf_TAIL : 0);
990 r->float_substr = Nullsv;
991 SvREFCNT_dec(data.longest_float);
992 longest_float_length = 0;
995 longest_fixed_length = CHR_SVLEN(data.longest_fixed);
996 if (longest_fixed_length
997 || (data.flags & SF_FIX_BEFORE_EOL /* Cannot have SEOL and MULTI */
998 && (!(data.flags & SF_FIX_BEFORE_MEOL)
999 || (PL_regflags & PMf_MULTILINE)))) {
1002 r->anchored_substr = data.longest_fixed;
1003 r->anchored_offset = data.offset_fixed;
1004 t = (data.flags & SF_FIX_BEFORE_EOL /* Can't have SEOL and MULTI */
1005 && (!(data.flags & SF_FIX_BEFORE_MEOL)
1006 || (PL_regflags & PMf_MULTILINE)));
1007 fbm_compile(r->anchored_substr, t ? FBMcf_TAIL : 0);
1010 r->anchored_substr = Nullsv;
1011 SvREFCNT_dec(data.longest_fixed);
1012 longest_fixed_length = 0;
1015 /* A temporary algorithm prefers floated substr to fixed one to dig more info. */
1016 if (longest_fixed_length > longest_float_length) {
1017 r->check_substr = r->anchored_substr;
1018 r->check_offset_min = r->check_offset_max = r->anchored_offset;
1019 if (r->reganch & ROPT_ANCH_SINGLE)
1020 r->reganch |= ROPT_NOSCAN;
1023 r->check_substr = r->float_substr;
1024 r->check_offset_min = data.offset_float_min;
1025 r->check_offset_max = data.offset_float_max;
1027 if (r->check_substr) {
1028 r->reganch |= RE_USE_INTUIT;
1029 if (SvTAIL(r->check_substr))
1030 r->reganch |= RE_INTUIT_TAIL;
1034 /* Several toplevels. Best we can is to set minlen. */
1037 DEBUG_r(PerlIO_printf(Perl_debug_log, "\n"));
1038 scan = r->program + 1;
1039 minlen = study_chunk(&scan, &fake, scan + PL_regsize, NULL, 0);
1040 r->check_substr = r->anchored_substr = r->float_substr = Nullsv;
1044 if (PL_regseen & REG_SEEN_GPOS)
1045 r->reganch |= ROPT_GPOS_SEEN;
1046 if (PL_regseen & REG_SEEN_LOOKBEHIND)
1047 r->reganch |= ROPT_LOOKBEHIND_SEEN;
1048 if (PL_regseen & REG_SEEN_EVAL)
1049 r->reganch |= ROPT_EVAL_SEEN;
1050 Newz(1002, r->startp, PL_regnpar, I32);
1051 Newz(1002, r->endp, PL_regnpar, I32);
1052 DEBUG_r(regdump(r));
1057 - reg - regular expression, i.e. main body or parenthesized thing
1059 * Caller must absorb opening parenthesis.
1061 * Combining parenthesis handling with the base level of regular expression
1062 * is a trifle forced, but the need to tie the tails of the branches to what
1063 * follows makes it hard to avoid.
1066 S_reg(pTHX_ I32 paren, I32 *flagp)
1067 /* paren: Parenthesized? 0=top, 1=(, inside: changed to letter. */
1070 register regnode *ret; /* Will be the head of the group. */
1071 register regnode *br;
1072 register regnode *lastbr;
1073 register regnode *ender = 0;
1074 register I32 parno = 0;
1075 I32 flags, oregflags = PL_regflags, have_branch = 0, open = 0;
1078 *flagp = 0; /* Tentatively. */
1080 /* Make an OPEN node, if parenthesized. */
1082 if (*PL_regcomp_parse == '?') {
1083 U16 posflags = 0, negflags = 0;
1084 U16 *flagsp = &posflags;
1088 paren = *PL_regcomp_parse++;
1089 ret = NULL; /* For look-ahead/behind. */
1092 PL_regseen |= REG_SEEN_LOOKBEHIND;
1093 if (*PL_regcomp_parse == '!')
1095 if (*PL_regcomp_parse != '=' && *PL_regcomp_parse != '!')
1106 FAIL2("Sequence (?%c...) not implemented", (int)paren);
1109 while (*PL_regcomp_parse && *PL_regcomp_parse != ')')
1111 if (*PL_regcomp_parse != ')')
1112 FAIL("Sequence (?#... not terminated");
1118 paren = *PL_regcomp_parse++;
1123 I32 count = 1, n = 0;
1125 char *s = PL_regcomp_parse;
1127 OP_4tree *sop, *rop;
1130 PL_regseen |= REG_SEEN_EVAL;
1131 while (count && (c = *PL_regcomp_parse)) {
1132 if (c == '\\' && PL_regcomp_parse[1])
1140 if (*PL_regcomp_parse != ')')
1141 FAIL("Sequence (?{...}) not terminated or not {}-balanced");
1145 if (PL_regcomp_parse - 1 - s)
1146 sv = newSVpvn(s, PL_regcomp_parse - 1 - s);
1148 sv = newSVpvn("", 0);
1150 rop = sv_compile_2op(sv, &sop, "re", &av);
1152 n = add_data(3, "nop");
1153 PL_regcomp_rx->data->data[n] = (void*)rop;
1154 PL_regcomp_rx->data->data[n+1] = (void*)sop;
1155 PL_regcomp_rx->data->data[n+2] = (void*)av;
1158 else { /* First pass */
1159 if (PL_reginterp_cnt < ++PL_seen_evals
1160 && PL_curcop != &PL_compiling)
1161 /* No compiled RE interpolated, has runtime
1162 components ===> unsafe. */
1163 FAIL("Eval-group not allowed at runtime, use re 'eval'");
1165 FAIL("Eval-group in insecure regular expression");
1170 ret = reg_node(LOGICAL);
1173 regtail(ret, reganode(EVAL, n));
1176 return reganode(EVAL, n);
1180 if (PL_regcomp_parse[0] == '?') {
1181 if (PL_regcomp_parse[1] == '=' || PL_regcomp_parse[1] == '!'
1182 || PL_regcomp_parse[1] == '<'
1183 || PL_regcomp_parse[1] == '{') { /* Lookahead or eval. */
1186 ret = reg_node(LOGICAL);
1189 regtail(ret, reg(1, &flag));
1193 else if (PL_regcomp_parse[0] >= '1' && PL_regcomp_parse[0] <= '9' ) {
1194 parno = atoi(PL_regcomp_parse++);
1196 while (isDIGIT(*PL_regcomp_parse))
1198 ret = reganode(GROUPP, parno);
1199 if ((c = *nextchar()) != ')')
1200 FAIL2("Switch (?(number%c not recognized", c);
1202 regtail(ret, reganode(IFTHEN, 0));
1203 br = regbranch(&flags, 1);
1205 br = reganode(LONGJMP, 0);
1207 regtail(br, reganode(LONGJMP, 0));
1212 lastbr = reganode(IFTHEN, 0); /* Fake one for optimizer. */
1213 regbranch(&flags, 1);
1214 regtail(ret, lastbr);
1222 FAIL("Switch (?(condition)... contains too many branches");
1223 ender = reg_node(TAIL);
1226 regtail(lastbr, ender);
1227 regtail(NEXTOPER(NEXTOPER(lastbr)), ender);
1230 regtail(ret, ender);
1234 FAIL2("Unknown condition for (?(%.2s", PL_regcomp_parse);
1238 FAIL("Sequence (? incomplete");
1243 while (*PL_regcomp_parse && strchr("iogcmsx", *PL_regcomp_parse)) {
1244 if (*PL_regcomp_parse != 'o')
1245 pmflag(flagsp, *PL_regcomp_parse);
1248 if (*PL_regcomp_parse == '-') {
1253 PL_regflags |= posflags;
1254 PL_regflags &= ~negflags;
1255 if (*PL_regcomp_parse == ':') {
1261 if (*PL_regcomp_parse != ')')
1262 FAIL2("Sequence (?%c...) not recognized", *PL_regcomp_parse);
1271 ret = reganode(OPEN, parno);
1278 /* Pick up the branches, linking them together. */
1279 br = regbranch(&flags, 1);
1282 if (*PL_regcomp_parse == '|') {
1283 if (!SIZE_ONLY && PL_extralen) {
1284 reginsert(BRANCHJ, br);
1287 reginsert(BRANCH, br);
1290 PL_extralen += 1; /* For BRANCHJ-BRANCH. */
1292 else if (paren == ':') {
1293 *flagp |= flags&SIMPLE;
1295 if (open) { /* Starts with OPEN. */
1296 regtail(ret, br); /* OPEN -> first. */
1298 else if (paren != '?') /* Not Conditional */
1302 *flagp |= flags&SPSTART;
1304 while (*PL_regcomp_parse == '|') {
1305 if (!SIZE_ONLY && PL_extralen) {
1306 ender = reganode(LONGJMP,0);
1307 regtail(NEXTOPER(NEXTOPER(lastbr)), ender); /* Append to the previous. */
1310 PL_extralen += 2; /* Account for LONGJMP. */
1312 br = regbranch(&flags, 0);
1315 regtail(lastbr, br); /* BRANCH -> BRANCH. */
1319 *flagp |= flags&SPSTART;
1322 if (have_branch || paren != ':') {
1323 /* Make a closing node, and hook it on the end. */
1326 ender = reg_node(TAIL);
1329 ender = reganode(CLOSE, parno);
1335 *flagp &= ~HASWIDTH;
1338 ender = reg_node(SUCCEED);
1341 ender = reg_node(END);
1344 regtail(lastbr, ender);
1347 /* Hook the tails of the branches to the closing node. */
1348 for (br = ret; br != NULL; br = regnext(br)) {
1349 regoptail(br, ender);
1356 static char parens[] = "=!<,>";
1358 if (paren && (p = strchr(parens, paren))) {
1359 int node = ((p - parens) % 2) ? UNLESSM : IFMATCH;
1360 int flag = (p - parens) > 1;
1363 node = SUSPEND, flag = 0;
1364 reginsert(node,ret);
1366 regtail(ret, reg_node(TAIL));
1370 /* Check for proper termination. */
1372 PL_regflags = oregflags;
1373 if (PL_regcomp_parse >= PL_regxend || *nextchar() != ')') {
1374 FAIL("unmatched () in regexp");
1377 else if (!paren && PL_regcomp_parse < PL_regxend) {
1378 if (*PL_regcomp_parse == ')') {
1379 FAIL("unmatched () in regexp");
1382 FAIL("junk on end of regexp"); /* "Can't happen". */
1390 - regbranch - one alternative of an | operator
1392 * Implements the concatenation operator.
1395 S_regbranch(pTHX_ I32 *flagp, I32 first)
1398 register regnode *ret;
1399 register regnode *chain = NULL;
1400 register regnode *latest;
1401 I32 flags = 0, c = 0;
1406 if (!SIZE_ONLY && PL_extralen)
1407 ret = reganode(BRANCHJ,0);
1409 ret = reg_node(BRANCH);
1412 if (!first && SIZE_ONLY)
1413 PL_extralen += 1; /* BRANCHJ */
1415 *flagp = WORST; /* Tentatively. */
1419 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != '|' && *PL_regcomp_parse != ')') {
1421 latest = regpiece(&flags);
1422 if (latest == NULL) {
1423 if (flags & TRYAGAIN)
1427 else if (ret == NULL)
1429 *flagp |= flags&HASWIDTH;
1430 if (chain == NULL) /* First piece. */
1431 *flagp |= flags&SPSTART;
1434 regtail(chain, latest);
1439 if (chain == NULL) { /* Loop ran zero times. */
1440 chain = reg_node(NOTHING);
1445 *flagp |= flags&SIMPLE;
1452 - regpiece - something followed by possible [*+?]
1454 * Note that the branching code sequences used for ? and the general cases
1455 * of * and + are somewhat optimized: they use the same NOTHING node as
1456 * both the endmarker for their branch list and the body of the last branch.
1457 * It might seem that this node could be dispensed with entirely, but the
1458 * endmarker role is not redundant.
1461 S_regpiece(pTHX_ I32 *flagp)
1464 register regnode *ret;
1466 register char *next;
1468 char *origparse = PL_regcomp_parse;
1471 I32 max = REG_INFTY;
1473 ret = regatom(&flags);
1475 if (flags & TRYAGAIN)
1480 op = *PL_regcomp_parse;
1482 if (op == '{' && regcurly(PL_regcomp_parse)) {
1483 next = PL_regcomp_parse + 1;
1485 while (isDIGIT(*next) || *next == ',') {
1494 if (*next == '}') { /* got one */
1498 min = atoi(PL_regcomp_parse);
1502 maxpos = PL_regcomp_parse;
1504 if (!max && *maxpos != '0')
1505 max = REG_INFTY; /* meaning "infinity" */
1506 else if (max >= REG_INFTY)
1507 FAIL2("Quantifier in {,} bigger than %d", REG_INFTY - 1);
1508 PL_regcomp_parse = next;
1512 if ((flags&SIMPLE)) {
1513 PL_regnaughty += 2 + PL_regnaughty / 2;
1514 reginsert(CURLY, ret);
1517 PL_regnaughty += 4 + PL_regnaughty; /* compound interest */
1518 regtail(ret, reg_node(WHILEM));
1519 if (!SIZE_ONLY && PL_extralen) {
1520 reginsert(LONGJMP,ret);
1521 reginsert(NOTHING,ret);
1522 NEXT_OFF(ret) = 3; /* Go over LONGJMP. */
1524 reginsert(CURLYX,ret);
1525 if (!SIZE_ONLY && PL_extralen)
1526 NEXT_OFF(ret) = 3; /* Go over NOTHING to LONGJMP. */
1527 regtail(ret, reg_node(NOTHING));
1537 if (max && max < min)
1538 FAIL("Can't do {n,m} with n > m");
1553 #if 0 /* Now runtime fix should be reliable. */
1554 if (!(flags&HASWIDTH) && op != '?')
1555 FAIL("regexp *+ operand could be empty");
1560 *flagp = (op != '+') ? (WORST|SPSTART|HASWIDTH) : (WORST|HASWIDTH);
1562 if (op == '*' && (flags&SIMPLE)) {
1563 reginsert(STAR, ret);
1567 else if (op == '*') {
1571 else if (op == '+' && (flags&SIMPLE)) {
1572 reginsert(PLUS, ret);
1576 else if (op == '+') {
1580 else if (op == '?') {
1585 if (ckWARN(WARN_UNSAFE) && !SIZE_ONLY && !(flags&HASWIDTH) && max > REG_INFTY/3) {
1586 Perl_warner(aTHX_ WARN_UNSAFE, "%.*s matches null string many times",
1587 PL_regcomp_parse - origparse, origparse);
1590 if (*PL_regcomp_parse == '?') {
1592 reginsert(MINMOD, ret);
1593 regtail(ret, ret + NODE_STEP_REGNODE);
1595 if (ISMULT2(PL_regcomp_parse))
1596 FAIL("nested *?+ in regexp");
1602 - regatom - the lowest level
1604 * Optimization: gobbles an entire sequence of ordinary characters so that
1605 * it can turn them into a single node, which is smaller to store and
1606 * faster to run. Backslashed characters are exceptions, each becoming a
1607 * separate node; the code is simpler that way and it's not worth fixing.
1609 * [Yes, it is worth fixing, some scripts can run twice the speed.]
1612 S_regatom(pTHX_ I32 *flagp)
1615 register regnode *ret = 0;
1618 *flagp = WORST; /* Tentatively. */
1621 switch (*PL_regcomp_parse) {
1625 if (PL_regflags & PMf_MULTILINE)
1626 ret = reg_node(MBOL);
1627 else if (PL_regflags & PMf_SINGLELINE)
1628 ret = reg_node(SBOL);
1630 ret = reg_node(BOL);
1633 if (PL_regcomp_parse[1])
1636 if (PL_regflags & PMf_MULTILINE)
1637 ret = reg_node(MEOL);
1638 else if (PL_regflags & PMf_SINGLELINE)
1639 ret = reg_node(SEOL);
1641 ret = reg_node(EOL);
1646 if (PL_regflags & PMf_SINGLELINE)
1647 ret = reg_node(SANYUTF8);
1649 ret = reg_node(ANYUTF8);
1653 if (PL_regflags & PMf_SINGLELINE)
1654 ret = reg_node(SANY);
1656 ret = reg_node(REG_ANY);
1657 *flagp |= HASWIDTH|SIMPLE;
1663 ret = (UTF ? regclassutf8() : regclass());
1664 if (*PL_regcomp_parse != ']')
1665 FAIL("unmatched [] in regexp");
1667 *flagp |= HASWIDTH|SIMPLE;
1671 ret = reg(1, &flags);
1673 if (flags & TRYAGAIN)
1677 *flagp |= flags&(HASWIDTH|SPSTART|SIMPLE);
1681 if (flags & TRYAGAIN) {
1685 FAIL2("internal urp in regexp at /%s/", PL_regcomp_parse);
1686 /* Supposed to be caught earlier. */
1689 if (!regcurly(PL_regcomp_parse)) {
1697 FAIL("?+*{} follows nothing in regexp");
1700 switch (*++PL_regcomp_parse) {
1703 ret = reg_node(SBOL);
1708 ret = reg_node(GPOS);
1709 PL_regseen |= REG_SEEN_GPOS;
1714 ret = reg_node(SEOL);
1719 ret = reg_node(EOS);
1721 PL_seen_zerolen++; /* Do not optimize RE away */
1725 ret = reg_node(SANY);
1726 *flagp |= HASWIDTH|SIMPLE;
1730 ret = reg_node(CLUMP);
1733 if (UTF && !PL_utf8_mark)
1734 is_utf8_mark((U8*)"~"); /* preload table */
1739 ? (LOC ? ALNUMLUTF8 : ALNUMUTF8)
1740 : (LOC ? ALNUML : ALNUM));
1741 *flagp |= HASWIDTH|SIMPLE;
1743 if (UTF && !PL_utf8_alnum)
1744 is_utf8_alnum((U8*)"a"); /* preload table */
1749 ? (LOC ? NALNUMLUTF8 : NALNUMUTF8)
1750 : (LOC ? NALNUML : NALNUM));
1751 *flagp |= HASWIDTH|SIMPLE;
1753 if (UTF && !PL_utf8_alnum)
1754 is_utf8_alnum((U8*)"a"); /* preload table */
1758 PL_regseen |= REG_SEEN_LOOKBEHIND;
1761 ? (LOC ? BOUNDLUTF8 : BOUNDUTF8)
1762 : (LOC ? BOUNDL : BOUND));
1765 if (UTF && !PL_utf8_alnum)
1766 is_utf8_alnum((U8*)"a"); /* preload table */
1770 PL_regseen |= REG_SEEN_LOOKBEHIND;
1773 ? (LOC ? NBOUNDLUTF8 : NBOUNDUTF8)
1774 : (LOC ? NBOUNDL : NBOUND));
1777 if (UTF && !PL_utf8_alnum)
1778 is_utf8_alnum((U8*)"a"); /* preload table */
1783 ? (LOC ? SPACELUTF8 : SPACEUTF8)
1784 : (LOC ? SPACEL : SPACE));
1785 *flagp |= HASWIDTH|SIMPLE;
1787 if (UTF && !PL_utf8_space)
1788 is_utf8_space((U8*)" "); /* preload table */
1793 ? (LOC ? NSPACELUTF8 : NSPACEUTF8)
1794 : (LOC ? NSPACEL : NSPACE));
1795 *flagp |= HASWIDTH|SIMPLE;
1797 if (UTF && !PL_utf8_space)
1798 is_utf8_space((U8*)" "); /* preload table */
1801 ret = reg_node(UTF ? DIGITUTF8 : DIGIT);
1802 *flagp |= HASWIDTH|SIMPLE;
1804 if (UTF && !PL_utf8_digit)
1805 is_utf8_digit((U8*)"1"); /* preload table */
1808 ret = reg_node(UTF ? NDIGITUTF8 : NDIGIT);
1809 *flagp |= HASWIDTH|SIMPLE;
1811 if (UTF && !PL_utf8_digit)
1812 is_utf8_digit((U8*)"1"); /* preload table */
1816 { /* a lovely hack--pretend we saw [\pX] instead */
1817 char* oldregxend = PL_regxend;
1819 if (PL_regcomp_parse[1] == '{') {
1820 PL_regxend = strchr(PL_regcomp_parse, '}');
1822 FAIL("Missing right brace on \\p{}");
1826 PL_regxend = PL_regcomp_parse + 2;
1829 ret = regclassutf8();
1831 PL_regxend = oldregxend;
1834 *flagp |= HASWIDTH|SIMPLE;
1847 case '1': case '2': case '3': case '4':
1848 case '5': case '6': case '7': case '8': case '9':
1850 I32 num = atoi(PL_regcomp_parse);
1852 if (num > 9 && num >= PL_regnpar)
1855 if (!SIZE_ONLY && num > PL_regcomp_rx->nparens)
1856 FAIL("reference to nonexistent group");
1859 ? (LOC ? REFFL : REFF)
1862 while (isDIGIT(*PL_regcomp_parse))
1870 if (PL_regcomp_parse >= PL_regxend)
1871 FAIL("trailing \\ in regexp");
1874 /* Do not generate `unrecognized' warnings here, we fall
1875 back into the quick-grab loop below */
1881 if (PL_regflags & PMf_EXTENDED) {
1882 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != '\n') PL_regcomp_parse++;
1883 if (PL_regcomp_parse < PL_regxend)
1899 ? (LOC ? EXACTFL : EXACTF)
1901 s = (char *) OPERAND(ret);
1902 regc(0, s++); /* save spot for len */
1903 for (len = 0, p = PL_regcomp_parse - 1;
1904 len < 127 && p < PL_regxend;
1909 if (PL_regflags & PMf_EXTENDED)
1910 p = regwhite(p, PL_regxend);
1964 char* e = strchr(p, '}');
1967 FAIL("Missing right brace on \\x{}");
1969 ender = scan_hex(p + 1, e - p, &numlen);
1970 if (numlen + len >= 127) { /* numlen is generous */
1977 FAIL("Can't use \\x{} without 'use utf8' declaration");
1980 ender = scan_hex(p, 2, &numlen);
1986 ender = UCHARAT(p++);
1987 ender = toCTRL(ender);
1989 case '0': case '1': case '2': case '3':case '4':
1990 case '5': case '6': case '7': case '8':case '9':
1992 (isDIGIT(p[1]) && atoi(p) >= PL_regnpar) ) {
1993 ender = scan_oct(p, 3, &numlen);
2002 if (p >= PL_regxend)
2003 FAIL("trailing \\ in regexp");
2006 if (!SIZE_ONLY && ckWARN(WARN_UNSAFE) && isALPHA(*p))
2007 Perl_warner(aTHX_ WARN_UNSAFE,
2008 "/%.127s/: Unrecognized escape \\%c passed through",
2011 goto normal_default;
2016 if ((*p & 0xc0) == 0xc0 && UTF) {
2017 ender = utf8_to_uv((U8*)p, &numlen);
2024 if (PL_regflags & PMf_EXTENDED)
2025 p = regwhite(p, PL_regxend);
2028 ender = toLOWER_LC_uni(ender);
2030 ender = toLOWER_uni(ender);
2032 if (ISMULT2(p)) { /* Back off on ?+*. */
2035 else if (ender >= 0x80 && UTF) {
2036 reguni(ender, s, &numlen);
2046 if (ender >= 0x80 && UTF) {
2047 reguni(ender, s, &numlen);
2055 PL_regcomp_parse = p - 1;
2058 FAIL("internal disaster in regexp");
2064 *OPERAND(ret) = len;
2067 PL_regsize += (len + 2 + sizeof(regnode) - 1) / sizeof(regnode);
2070 PL_regcode += (len + 2 + sizeof(regnode) - 1) / sizeof(regnode);
2080 S_regwhite(pTHX_ char *p, char *e)
2085 else if (*p == '#') {
2088 } while (p < e && *p != '\n');
2096 /* parse POSIX character classes like [[:foo:]] */
2098 S_regpposixcc(pTHX_ I32 value)
2103 if (value == '[' && PL_regcomp_parse + 1 < PL_regxend &&
2104 /* I smell either [: or [= or [. -- POSIX has been here, right? */
2105 (*PL_regcomp_parse == ':' ||
2106 *PL_regcomp_parse == '=' ||
2107 *PL_regcomp_parse == '.')) {
2108 char c = *PL_regcomp_parse;
2109 char* s = PL_regcomp_parse++;
2111 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != c)
2113 if (PL_regcomp_parse == PL_regxend)
2114 /* Grandfather lone [:, [=, [. */
2115 PL_regcomp_parse = s;
2117 PL_regcomp_parse++; /* skip over the c */
2118 if (*PL_regcomp_parse == ']') {
2119 /* Not Implemented Yet.
2120 * (POSIX Extended Character Classes, that is)
2121 * The text between e.g. [: and :] would start
2122 * at s + 1 and stop at regcomp_parse - 2. */
2123 if (ckWARN(WARN_UNSAFE) && !SIZE_ONLY)
2124 Perl_warner(aTHX_ WARN_UNSAFE,
2125 "Character class syntax [%c %c] is reserved for future extensions", c, c);
2126 PL_regcomp_parse++; /* skip over the ending ] */
2130 /* maternal grandfather */
2131 PL_regcomp_parse = s;
2143 register char *opnd, *s;
2145 register I32 lastvalue = 1234;
2146 register I32 range = 0;
2147 register regnode *ret;
2151 s = opnd = (char *) OPERAND(PL_regcode);
2152 ret = reg_node(ANYOF);
2153 for (value = 0; value < 33; value++)
2155 if (*PL_regcomp_parse == '^') { /* Complement of range. */
2159 *opnd |= ANYOF_INVERT;
2162 PL_regcode += ANY_SKIP;
2164 *opnd |= ANYOF_FOLD;
2166 *opnd |= ANYOF_LOCALE;
2169 PL_regsize += ANY_SKIP;
2171 if (*PL_regcomp_parse == ']' || *PL_regcomp_parse == '-')
2172 goto skipcond; /* allow 1st char to be ] or - */
2173 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != ']') {
2175 value = UCHARAT(PL_regcomp_parse++);
2177 (void)regpposixcc(value); /* ignore the return value for now */
2178 else if (value == '\\') {
2179 value = UCHARAT(PL_regcomp_parse++);
2184 *opnd |= ANYOF_ALNUML;
2186 for (value = 0; value < 256; value++)
2188 ANYOF_SET(opnd, value);
2196 *opnd |= ANYOF_NALNUML;
2198 for (value = 0; value < 256; value++)
2199 if (!isALNUM(value))
2200 ANYOF_SET(opnd, value);
2208 *opnd |= ANYOF_SPACEL;
2210 for (value = 0; value < 256; value++)
2212 ANYOF_SET(opnd, value);
2220 *opnd |= ANYOF_NSPACEL;
2222 for (value = 0; value < 256; value++)
2223 if (!isSPACE(value))
2224 ANYOF_SET(opnd, value);
2231 for (value = '0'; value <= '9'; value++)
2232 ANYOF_SET(opnd, value);
2238 for (value = 0; value < '0'; value++)
2239 ANYOF_SET(opnd, value);
2240 for (value = '9' + 1; value < 256; value++)
2241 ANYOF_SET(opnd, value);
2267 value = scan_hex(PL_regcomp_parse, 2, &numlen);
2268 PL_regcomp_parse += numlen;
2271 value = UCHARAT(PL_regcomp_parse++);
2272 value = toCTRL(value);
2274 case '0': case '1': case '2': case '3': case '4':
2275 case '5': case '6': case '7': case '8': case '9':
2276 value = scan_oct(--PL_regcomp_parse, 3, &numlen);
2277 PL_regcomp_parse += numlen;
2282 if (lastvalue > value)
2283 FAIL("invalid [] range in regexp");
2288 if (*PL_regcomp_parse == '-' && PL_regcomp_parse+1 < PL_regxend &&
2289 PL_regcomp_parse[1] != ']') {
2292 continue; /* do it next time */
2297 if ((isLOWER(lastvalue) && isLOWER(value)) ||
2298 (isUPPER(lastvalue) && isUPPER(value)))
2301 if (isLOWER(lastvalue)) {
2302 for (i = lastvalue; i <= value; i++)
2306 for (i = lastvalue; i <= value; i++)
2313 for ( ; lastvalue <= value; lastvalue++)
2314 ANYOF_SET(opnd, lastvalue);
2318 /* optimize case-insensitive simple patterns (e.g. /[a-z]/i) */
2319 if (!SIZE_ONLY && (*opnd & (0xFF ^ ANYOF_INVERT)) == ANYOF_FOLD) {
2320 for (value = 0; value < 256; ++value) {
2321 if (ANYOF_TEST(opnd, value)) {
2322 I32 cf = PL_fold[value];
2323 ANYOF_SET(opnd, cf);
2326 *opnd &= ~ANYOF_FOLD;
2328 /* optimize inverted simple patterns (e.g. [^a-z]) */
2329 if (!SIZE_ONLY && (*opnd & 0xFF) == ANYOF_INVERT) {
2330 for (value = 0; value < 32; ++value)
2331 opnd[1 + value] ^= 0xFF;
2338 S_regclassutf8(pTHX)
2340 register char *opnd, *e;
2342 register U32 lastvalue = 123456;
2343 register I32 range = 0;
2344 register regnode *ret;
2351 if (*PL_regcomp_parse == '^') { /* Complement of range. */
2355 flags |= ANYOF_INVERT;
2359 flags |= ANYOF_FOLD;
2361 flags |= ANYOF_LOCALE;
2362 listsv = newSVpvn("# comment\n",10);
2365 if (*PL_regcomp_parse == ']' || *PL_regcomp_parse == '-')
2366 goto skipcond; /* allow 1st char to be ] or - */
2368 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != ']') {
2370 value = utf8_to_uv((U8*)PL_regcomp_parse, &numlen);
2371 PL_regcomp_parse += numlen;
2374 (void)regpposixcc(value); /* ignore the return value for now */
2375 else if (value == '\\') {
2376 value = utf8_to_uv((U8*)PL_regcomp_parse, &numlen);
2377 PL_regcomp_parse += numlen;
2382 flags |= ANYOF_ALNUML;
2384 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsAlnum\n");
2391 flags |= ANYOF_NALNUML;
2393 Perl_sv_catpvf(aTHX_ listsv,
2394 "-utf8::IsAlpha\n-utf8::IsDigit\n0000\t%04x\n%04x\tffff\n",
2403 flags |= ANYOF_SPACEL;
2404 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsSpace\n");
2406 is_utf8_space((U8*)" ");
2413 flags |= ANYOF_NSPACEL;
2414 Perl_sv_catpvf(aTHX_ listsv,
2415 "!utf8::IsSpace\n");
2417 is_utf8_space((U8*)" ");
2423 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsDigit\n");
2429 Perl_sv_catpvf(aTHX_ listsv,
2430 "!utf8::IsDigit\n");
2436 if (*PL_regcomp_parse == '{') {
2437 e = strchr(PL_regcomp_parse++, '}');
2439 FAIL("Missing right brace on \\p{}");
2440 n = e - PL_regcomp_parse;
2443 e = PL_regcomp_parse;
2448 Perl_sv_catpvf(aTHX_ listsv, "+utf8::%.*s\n", n, PL_regcomp_parse);
2450 Perl_sv_catpvf(aTHX_ listsv,
2451 "!utf8::%.*s\n", n, PL_regcomp_parse);
2453 PL_regcomp_parse = e + 1;
2478 if (*PL_regcomp_parse == '{') {
2479 e = strchr(PL_regcomp_parse++, '}');
2481 FAIL("Missing right brace on \\x{}");
2482 value = scan_hex(PL_regcomp_parse, e - PL_regcomp_parse, &numlen);
2483 PL_regcomp_parse = e + 1;
2486 value = scan_hex(PL_regcomp_parse, 2, &numlen);
2487 PL_regcomp_parse += numlen;
2491 value = UCHARAT(PL_regcomp_parse++);
2492 value = toCTRL(value);
2494 case '0': case '1': case '2': case '3': case '4':
2495 case '5': case '6': case '7': case '8': case '9':
2496 value = scan_oct(--PL_regcomp_parse, 3, &numlen);
2497 PL_regcomp_parse += numlen;
2502 if (lastvalue > value)
2503 FAIL("invalid [] range in regexp");
2505 Perl_sv_catpvf(aTHX_ listsv, "%04x\t%04x\n", lastvalue, value);
2511 if (*PL_regcomp_parse == '-' && PL_regcomp_parse+1 < PL_regxend &&
2512 PL_regcomp_parse[1] != ']') {
2515 continue; /* do it next time */
2518 Perl_sv_catpvf(aTHX_ listsv, "%04x\n", value);
2522 ret = reganode(ANYOFUTF8, 0);
2525 SV *rv = swash_init("utf8", "", listsv, 1, 0);
2526 SvREFCNT_dec(listsv);
2527 n = add_data(1,"s");
2528 PL_regcomp_rx->data->data[n] = (void*)rv;
2529 ARG1_SET(ret, flags);
2540 char* retval = PL_regcomp_parse++;
2543 if (*PL_regcomp_parse == '(' && PL_regcomp_parse[1] == '?' &&
2544 PL_regcomp_parse[2] == '#') {
2545 while (*PL_regcomp_parse && *PL_regcomp_parse != ')')
2550 if (PL_regflags & PMf_EXTENDED) {
2551 if (isSPACE(*PL_regcomp_parse)) {
2555 else if (*PL_regcomp_parse == '#') {
2556 while (*PL_regcomp_parse && *PL_regcomp_parse != '\n')
2567 - reg_node - emit a node
2569 STATIC regnode * /* Location. */
2570 S_reg_node(pTHX_ U8 op)
2573 register regnode *ret;
2574 register regnode *ptr;
2578 SIZE_ALIGN(PL_regsize);
2583 NODE_ALIGN_FILL(ret);
2585 FILL_ADVANCE_NODE(ptr, op);
2592 - reganode - emit a node with an argument
2594 STATIC regnode * /* Location. */
2595 S_reganode(pTHX_ U8 op, U32 arg)
2598 register regnode *ret;
2599 register regnode *ptr;
2603 SIZE_ALIGN(PL_regsize);
2608 NODE_ALIGN_FILL(ret);
2610 FILL_ADVANCE_NODE_ARG(ptr, op, arg);
2617 - regc - emit (if appropriate) a Unicode character
2620 S_reguni(pTHX_ UV uv, char* s, I32* lenp)
2625 *lenp = uv_to_utf8(tmpbuf, uv) - tmpbuf;
2628 *lenp = uv_to_utf8((U8*)s, uv) - (U8*)s;
2633 - regc - emit (if appropriate) a byte of code
2636 S_regc(pTHX_ U8 b, char* s)
2644 - reginsert - insert an operator in front of already-emitted operand
2646 * Means relocating the operand.
2649 S_reginsert(pTHX_ U8 op, regnode *opnd)
2652 register regnode *src;
2653 register regnode *dst;
2654 register regnode *place;
2655 register int offset = regarglen[(U8)op];
2657 /* (PL_regkind[(U8)op] == CURLY ? EXTRA_STEP_2ARGS : 0); */
2660 PL_regsize += NODE_STEP_REGNODE + offset;
2665 PL_regcode += NODE_STEP_REGNODE + offset;
2668 StructCopy(--src, --dst, regnode);
2670 place = opnd; /* Op node, where operand used to be. */
2671 src = NEXTOPER(place);
2672 FILL_ADVANCE_NODE(place, op);
2673 Zero(src, offset, regnode);
2677 - regtail - set the next-pointer at the end of a node chain of p to val.
2680 S_regtail(pTHX_ regnode *p, regnode *val)
2683 register regnode *scan;
2684 register regnode *temp;
2685 register I32 offset;
2690 /* Find last node. */
2693 temp = regnext(scan);
2699 if (reg_off_by_arg[OP(scan)]) {
2700 ARG_SET(scan, val - scan);
2703 NEXT_OFF(scan) = val - scan;
2708 - regoptail - regtail on operand of first argument; nop if operandless
2711 S_regoptail(pTHX_ regnode *p, regnode *val)
2714 /* "Operandless" and "op != BRANCH" are synonymous in practice. */
2715 if (p == NULL || SIZE_ONLY)
2717 if (PL_regkind[(U8)OP(p)] == BRANCH) {
2718 regtail(NEXTOPER(p), val);
2720 else if ( PL_regkind[(U8)OP(p)] == BRANCHJ) {
2721 regtail(NEXTOPER(NEXTOPER(p)), val);
2728 - regcurly - a little FSA that accepts {\d+,?\d*}
2731 S_regcurly(pTHX_ register char *s)
2750 S_dumpuntil(pTHX_ regnode *start, regnode *node, regnode *last, SV* sv, I32 l)
2753 register char op = EXACT; /* Arbitrary non-END op. */
2754 register regnode *next, *onode;
2756 while (op != END && (!last || node < last)) {
2757 /* While that wasn't END last time... */
2763 next = regnext(node);
2765 if (OP(node) == OPTIMIZED)
2768 PerlIO_printf(Perl_debug_log, "%4d:%*s%s", node - start,
2769 2*l + 1, "", SvPVX(sv));
2770 if (next == NULL) /* Next ptr. */
2771 PerlIO_printf(Perl_debug_log, "(0)");
2773 PerlIO_printf(Perl_debug_log, "(%d)", next - start);
2774 (void)PerlIO_putc(Perl_debug_log, '\n');
2776 if (PL_regkind[(U8)op] == BRANCHJ) {
2777 register regnode *nnode = (OP(next) == LONGJMP
2780 if (last && nnode > last)
2782 node = dumpuntil(start, NEXTOPER(NEXTOPER(node)), nnode, sv, l + 1);
2784 else if (PL_regkind[(U8)op] == BRANCH) {
2785 node = dumpuntil(start, NEXTOPER(node), next, sv, l + 1);
2787 else if ( op == CURLY) { /* `next' might be very big: optimizer */
2788 node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
2789 NEXTOPER(node) + EXTRA_STEP_2ARGS + 1, sv, l + 1);
2791 else if (PL_regkind[(U8)op] == CURLY && op != CURLYX) {
2792 node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
2795 else if ( op == PLUS || op == STAR) {
2796 node = dumpuntil(start, NEXTOPER(node), NEXTOPER(node) + 1, sv, l + 1);
2798 else if (op == ANYOF) {
2799 node = NEXTOPER(node);
2802 else if (PL_regkind[(U8)op] == EXACT) {
2803 /* Literal string, where present. */
2804 node += ((*OPERAND(node)) + 2 + sizeof(regnode) - 1) / sizeof(regnode);
2805 node = NEXTOPER(node);
2808 node = NEXTOPER(node);
2809 node += regarglen[(U8)op];
2811 if (op == CURLYX || op == OPEN)
2813 else if (op == WHILEM)
2816 #endif /* DEBUGGING */
2821 - regdump - dump a regexp onto Perl_debug_log in vaguely comprehensible form
2824 Perl_regdump(pTHX_ regexp *r)
2828 SV *sv = sv_newmortal();
2830 (void)dumpuntil(r->program, r->program + 1, NULL, sv, 0);
2832 /* Header fields of interest. */
2833 if (r->anchored_substr)
2834 PerlIO_printf(Perl_debug_log, "anchored `%s%s%s'%s at %d ",
2836 SvPVX(r->anchored_substr),
2838 SvTAIL(r->anchored_substr) ? "$" : "",
2839 r->anchored_offset);
2840 if (r->float_substr)
2841 PerlIO_printf(Perl_debug_log, "floating `%s%s%s'%s at %d..%u ",
2843 SvPVX(r->float_substr),
2845 SvTAIL(r->float_substr) ? "$" : "",
2846 r->float_min_offset, r->float_max_offset);
2847 if (r->check_substr)
2848 PerlIO_printf(Perl_debug_log,
2849 r->check_substr == r->float_substr
2850 ? "(checking floating" : "(checking anchored");
2851 if (r->reganch & ROPT_NOSCAN)
2852 PerlIO_printf(Perl_debug_log, " noscan");
2853 if (r->reganch & ROPT_CHECK_ALL)
2854 PerlIO_printf(Perl_debug_log, " isall");
2855 if (r->check_substr)
2856 PerlIO_printf(Perl_debug_log, ") ");
2858 if (r->regstclass) {
2859 regprop(sv, r->regstclass);
2860 PerlIO_printf(Perl_debug_log, "stclass `%s' ", SvPVX(sv));
2862 if (r->reganch & ROPT_ANCH) {
2863 PerlIO_printf(Perl_debug_log, "anchored");
2864 if (r->reganch & ROPT_ANCH_BOL)
2865 PerlIO_printf(Perl_debug_log, "(BOL)");
2866 if (r->reganch & ROPT_ANCH_MBOL)
2867 PerlIO_printf(Perl_debug_log, "(MBOL)");
2868 if (r->reganch & ROPT_ANCH_SBOL)
2869 PerlIO_printf(Perl_debug_log, "(SBOL)");
2870 if (r->reganch & ROPT_ANCH_GPOS)
2871 PerlIO_printf(Perl_debug_log, "(GPOS)");
2872 PerlIO_putc(Perl_debug_log, ' ');
2874 if (r->reganch & ROPT_GPOS_SEEN)
2875 PerlIO_printf(Perl_debug_log, "GPOS ");
2876 if (r->reganch & ROPT_SKIP)
2877 PerlIO_printf(Perl_debug_log, "plus ");
2878 if (r->reganch & ROPT_IMPLICIT)
2879 PerlIO_printf(Perl_debug_log, "implicit ");
2880 PerlIO_printf(Perl_debug_log, "minlen %ld ", (long) r->minlen);
2881 if (r->reganch & ROPT_EVAL_SEEN)
2882 PerlIO_printf(Perl_debug_log, "with eval ");
2883 PerlIO_printf(Perl_debug_log, "\n");
2884 #endif /* DEBUGGING */
2888 - regprop - printable representation of opcode
2891 Perl_regprop(pTHX_ SV *sv, regnode *o)
2897 sv_setpvn(sv, "", 0);
2898 if (OP(o) >= reg_num) /* regnode.type is unsigned */
2899 FAIL("corrupted regexp opcode");
2900 sv_catpv(sv, (char*)reg_name[OP(o)]); /* Take off const! */
2902 k = PL_regkind[(U8)OP(o)];
2905 Perl_sv_catpvf(aTHX_ sv, " <%s%s%s>", PL_colors[0], OPERAND(o) + 1, PL_colors[1]);
2906 else if (k == CURLY) {
2907 if (OP(o) == CURLYM || OP(o) == CURLYN)
2908 Perl_sv_catpvf(aTHX_ sv, "[%d]", o->flags); /* Parenth number */
2909 Perl_sv_catpvf(aTHX_ sv, " {%d,%d}", ARG1(o), ARG2(o));
2911 else if (k == REF || k == OPEN || k == CLOSE || k == GROUPP )
2912 Perl_sv_catpvf(aTHX_ sv, "%d", ARG(o)); /* Parenth number */
2913 else if (k == LOGICAL)
2914 Perl_sv_catpvf(aTHX_ sv, "[%d]", ARG(o)); /* 2: embedded, otherwise 1 */
2915 else if (k == BRANCHJ && (OP(o) == UNLESSM || OP(o) == IFMATCH))
2916 Perl_sv_catpvf(aTHX_ sv, "[-%d]", o->flags);
2917 #endif /* DEBUGGING */
2921 Perl_re_intuit_string(pTHX_ regexp *prog)
2922 { /* Assume that RE_INTUIT is set */
2925 char *s = SvPV(prog->check_substr,n_a);
2927 if (!PL_colorset) reginitcolors();
2928 PerlIO_printf(Perl_debug_log,
2929 "%sUsing REx substr:%s `%s%.60s%s%s'\n",
2930 PL_colors[4],PL_colors[5],PL_colors[0],
2933 (strlen(s) > 60 ? "..." : ""));
2936 return prog->check_substr;
2940 Perl_pregfree(pTHX_ struct regexp *r)
2943 DEBUG_r(PerlIO_printf(Perl_debug_log,
2944 "%sFreeing REx:%s `%s%.60s%s%s'\n",
2945 PL_colors[4],PL_colors[5],PL_colors[0],
2948 (strlen(r->precomp) > 60 ? "..." : "")));
2951 if (!r || (--r->refcnt > 0))
2954 Safefree(r->precomp);
2955 if (RX_MATCH_COPIED(r))
2956 Safefree(r->subbeg);
2958 if (r->anchored_substr)
2959 SvREFCNT_dec(r->anchored_substr);
2960 if (r->float_substr)
2961 SvREFCNT_dec(r->float_substr);
2962 Safefree(r->substrs);
2965 int n = r->data->count;
2966 AV* new_comppad = NULL;
2971 switch (r->data->what[n]) {
2973 SvREFCNT_dec((SV*)r->data->data[n]);
2976 new_comppad = (AV*)r->data->data[n];
2979 if (new_comppad == NULL)
2980 Perl_croak(aTHX_ "panic: pregfree comppad");
2981 old_comppad = PL_comppad;
2982 old_curpad = PL_curpad;
2983 PL_comppad = new_comppad;
2984 PL_curpad = AvARRAY(new_comppad);
2985 op_free((OP_4tree*)r->data->data[n]);
2986 PL_comppad = old_comppad;
2987 PL_curpad = old_curpad;
2988 SvREFCNT_dec((SV*)new_comppad);
2994 FAIL2("panic: regfree data code '%c'", r->data->what[n]);
2997 Safefree(r->data->what);
3000 Safefree(r->startp);
3006 - regnext - dig the "next" pointer out of a node
3008 * [Note, when REGALIGN is defined there are two places in regmatch()
3009 * that bypass this code for speed.]
3012 Perl_regnext(pTHX_ register regnode *p)
3015 register I32 offset;
3017 if (p == &PL_regdummy)
3020 offset = (reg_off_by_arg[OP(p)] ? ARG(p) : NEXT_OFF(p));
3028 S_re_croak2(pTHX_ const char* pat1,const char* pat2,...)
3031 STRLEN l1 = strlen(pat1);
3032 STRLEN l2 = strlen(pat2);
3041 Copy(pat1, buf, l1 , char);
3042 Copy(pat2, buf + l1, l2 , char);
3043 buf[l1 + l2] = '\n';
3044 buf[l1 + l2 + 1] = '\0';
3046 /* ANSI variant takes additional second argument */
3047 va_start(args, pat2);
3051 msv = mess(buf, &args);
3053 message = SvPV(msv,l1);
3056 Copy(message, buf, l1 , char);
3057 buf[l1] = '\0'; /* Overwrite \n */
3058 Perl_croak(aTHX_ "%s", buf);
3061 /* XXX Here's a total kludge. But we need to re-enter for swash routines. */
3064 Perl_save_re_context(pTHX)
3068 SAVEPPTR(PL_regprecomp); /* uncompiled string. */
3069 SAVEI32(PL_regnpar); /* () count. */
3070 SAVEI32(PL_regsize); /* Code size. */
3071 SAVEI16(PL_regflags); /* are we folding, multilining? */
3072 SAVEPPTR(PL_reginput); /* String-input pointer. */
3073 SAVEPPTR(PL_regbol); /* Beginning of input, for ^ check. */
3074 SAVEPPTR(PL_regeol); /* End of input, for $ check. */
3075 SAVESPTR(PL_regstartp); /* Pointer to startp array. */
3076 SAVESPTR(PL_regendp); /* Ditto for endp. */
3077 SAVESPTR(PL_reglastparen); /* Similarly for lastparen. */
3078 SAVEPPTR(PL_regtill); /* How far we are required to go. */
3079 SAVEI32(PL_regprev); /* char before regbol, \n if none */
3080 SAVESPTR(PL_reg_start_tmp); /* from regexec.c */
3081 PL_reg_start_tmp = 0;
3082 SAVEFREEPV(PL_reg_start_tmp);
3083 SAVEI32(PL_reg_start_tmpl); /* from regexec.c */
3084 PL_reg_start_tmpl = 0;
3085 SAVESPTR(PL_regdata);
3086 SAVEI32(PL_reg_flags); /* from regexec.c */
3087 SAVEI32(PL_reg_eval_set); /* from regexec.c */
3088 SAVEI32(PL_regnarrate); /* from regexec.c */
3089 SAVESPTR(PL_regprogram); /* from regexec.c */
3090 SAVEINT(PL_regindent); /* from regexec.c */
3091 SAVESPTR(PL_regcc); /* from regexec.c */
3092 SAVESPTR(PL_curcop);
3093 SAVESPTR(PL_regcomp_rx); /* from regcomp.c */
3094 SAVEI32(PL_regseen); /* from regcomp.c */
3095 SAVEI32(PL_regsawback); /* Did we see \1, ...? */
3096 SAVEI32(PL_regnaughty); /* How bad is this pattern? */
3097 SAVESPTR(PL_regcode); /* Code-emit pointer; ®dummy = don't */
3098 SAVEPPTR(PL_regxend); /* End of input for compile */
3099 SAVEPPTR(PL_regcomp_parse); /* Input-scan pointer. */
3100 SAVESPTR(PL_reg_call_cc); /* from regexec.c */
3101 SAVESPTR(PL_reg_re); /* from regexec.c */
3102 SAVEPPTR(PL_reg_ganch); /* from regexec.c */
3103 SAVESPTR(PL_reg_sv); /* from regexec.c */
3104 SAVESPTR(PL_reg_magic); /* from regexec.c */
3105 SAVEI32(PL_reg_oldpos); /* from regexec.c */
3106 SAVESPTR(PL_reg_oldcurpm); /* from regexec.c */
3107 SAVESPTR(PL_reg_curpm); /* from regexec.c */
3109 SAVEPPTR(PL_reg_starttry); /* from regexec.c */