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
45 # define PERL_NO_GET_CONTEXT
50 * pregcomp and pregexec -- regsub and regerror are not used in perl
52 * Copyright (c) 1986 by University of Toronto.
53 * Written by Henry Spencer. Not derived from licensed software.
55 * Permission is granted to anyone to use this software for any
56 * purpose on any computer system, and to redistribute it freely,
57 * subject to the following restrictions:
59 * 1. The author is not responsible for the consequences of use of
60 * this software, no matter how awful, even if they arise
63 * 2. The origin of this software must not be misrepresented, either
64 * by explicit claim or by omission.
66 * 3. Altered versions must be plainly marked as such, and must not
67 * be misrepresented as being the original software.
70 **** Alterations to Henry's code are...
72 **** Copyright (c) 1991-2000, Larry Wall
74 **** You may distribute under the terms of either the GNU General Public
75 **** License or the Artistic License, as specified in the README file.
78 * Beware that some of this code is subtly aware of the way operator
79 * precedence is structured in regular expressions. Serious changes in
80 * regular-expression syntax might require a total rethink.
83 #define PERL_IN_REGCOMP_C
86 #ifdef PERL_IN_XSUB_RE
87 # if defined(PERL_CAPI) || defined(PERL_OBJECT)
102 # if defined(BUGGY_MSC6)
103 /* MSC 6.00A breaks on op/regexp.t test 85 unless we turn this off */
104 # pragma optimize("a",off)
105 /* But MSC 6.00A is happy with 'w', for aliases only across function calls*/
106 # pragma optimize("w",on )
107 # endif /* BUGGY_MSC6 */
111 #define STATIC static
114 #define ISMULT1(c) ((c) == '*' || (c) == '+' || (c) == '?')
115 #define ISMULT2(s) ((*s) == '*' || (*s) == '+' || (*s) == '?' || \
116 ((*s) == '{' && regcurly(s)))
118 #define PERL_META "^$.[()|?+*\\"
120 #define META "^$.[()|?+*\\"
124 #undef SPSTART /* dratted cpp namespace... */
127 * Flags to be passed up and down.
129 #define WORST 0 /* Worst case. */
130 #define HASWIDTH 0x1 /* Known to match non-null strings. */
131 #define SIMPLE 0x2 /* Simple enough to be STAR/PLUS operand. */
132 #define SPSTART 0x4 /* Starts with * or +. */
133 #define TRYAGAIN 0x8 /* Weeded out a declaration. */
135 /* Length of a variant. */
137 typedef struct scan_data_t {
143 I32 last_end; /* min value, <0 unless valid. */
146 SV **longest; /* Either &l_fixed, or &l_float. */
150 I32 offset_float_min;
151 I32 offset_float_max;
154 struct regnode_charclass_class *start_class;
158 * Forward declarations for pregcomp()'s friends.
161 static scan_data_t zero_scan_data = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
164 #define SF_BEFORE_EOL (SF_BEFORE_SEOL|SF_BEFORE_MEOL)
165 #define SF_BEFORE_SEOL 0x1
166 #define SF_BEFORE_MEOL 0x2
167 #define SF_FIX_BEFORE_EOL (SF_FIX_BEFORE_SEOL|SF_FIX_BEFORE_MEOL)
168 #define SF_FL_BEFORE_EOL (SF_FL_BEFORE_SEOL|SF_FL_BEFORE_MEOL)
171 # define SF_FIX_SHIFT_EOL (0+2)
172 # define SF_FL_SHIFT_EOL (0+4)
174 # define SF_FIX_SHIFT_EOL (+2)
175 # define SF_FL_SHIFT_EOL (+4)
178 #define SF_FIX_BEFORE_SEOL (SF_BEFORE_SEOL << SF_FIX_SHIFT_EOL)
179 #define SF_FIX_BEFORE_MEOL (SF_BEFORE_MEOL << SF_FIX_SHIFT_EOL)
181 #define SF_FL_BEFORE_SEOL (SF_BEFORE_SEOL << SF_FL_SHIFT_EOL)
182 #define SF_FL_BEFORE_MEOL (SF_BEFORE_MEOL << SF_FL_SHIFT_EOL) /* 0x20 */
183 #define SF_IS_INF 0x40
184 #define SF_HAS_PAR 0x80
185 #define SF_IN_PAR 0x100
186 #define SF_HAS_EVAL 0x200
187 #define SCF_DO_SUBSTR 0x400
188 #define SCF_DO_STCLASS_AND 0x0800
189 #define SCF_DO_STCLASS_OR 0x1000
190 #define SCF_DO_STCLASS (SCF_DO_STCLASS_AND|SCF_DO_STCLASS_OR)
193 #define UTF (PL_reg_flags & RF_utf8)
194 #define LOC (PL_regflags & PMf_LOCALE)
195 #define FOLD (PL_regflags & PMf_FOLD)
197 #define OOB_CHAR8 1234
198 #define OOB_UTF8 123456
199 #define OOB_NAMEDCLASS -1
201 #define CHR_SVLEN(sv) (UTF ? sv_len_utf8(sv) : SvCUR(sv))
202 #define CHR_DIST(a,b) (UTF ? utf8_distance(a,b) : a - b)
205 /* length of regex to show in messages that don't mark a position within */
206 #define RegexLengthToShowInErrorMessages 127
209 * If MARKER[12] are adjusted, be sure to adjust the constants at the top
210 * of t/op/regmesg.t, the tests in t/op/re_tests, and those in
211 * op/pragma/warn/regcomp.
213 #define MARKER1 "<HERE<" /* marker as it appears in the description */
214 #define MARKER2 " <<<HERE<<< " /* marker as it appears within the regex */
216 #define REPORT_LOCATION " at " MARKER1 " mark in regex m/%.*s" MARKER2 "%s/"
219 * Calls SAVEDESTRUCTOR_X if needed, then calls Perl_croak with the given
220 * arg. Show regex, up to a maximum length. If it's too long, chop and add
225 char *elipises = ""; \
226 unsigned len = strlen(PL_regprecomp); \
229 SAVEDESTRUCTOR_X(clear_re,(void*)PL_regcomp_rx); \
231 if (len > RegexLengthToShowInErrorMessages) { \
232 /* chop 10 shorter than the max, to ensure meaning of "..." */ \
233 len = RegexLengthToShowInErrorMessages - 10; \
236 Perl_croak(aTHX_ "%s in regex m/%.*s%s/", \
237 m, len, PL_regprecomp, elipises); \
241 * Calls SAVEDESTRUCTOR_X if needed, then calls Perl_croak with the given
242 * args. Show regex, up to a maximum length. If it's too long, chop and add
245 #define FAIL2(pat,m) \
247 char *elipises = ""; \
248 unsigned len = strlen(PL_regprecomp); \
251 SAVEDESTRUCTOR_X(clear_re,(void*)PL_regcomp_rx); \
253 if (len > RegexLengthToShowInErrorMessages) { \
254 /* chop 10 shorter than the max, to ensure meaning of "..." */ \
255 len = RegexLengthToShowInErrorMessages - 10; \
258 S_re_croak2(aTHX_ pat, " in regex m/%.*s%s/", \
259 m, len, PL_regprecomp, elipises); \
264 * Simple_vFAIL -- like FAIL, but marks the current location in the scan
266 #define Simple_vFAIL(m) \
268 unsigned offset = strlen(PL_regprecomp)-(PL_regxend-PL_regcomp_parse); \
270 Perl_croak(aTHX_ "%s" REPORT_LOCATION, \
271 m, offset, PL_regprecomp, PL_regprecomp + offset); \
275 * Calls SAVEDESTRUCTOR_X if needed, then Simple_vFAIL()
280 SAVEDESTRUCTOR_X(clear_re,(void*)PL_regcomp_rx); \
285 * Like Simple_vFAIL(), but accepts two arguments.
287 #define Simple_vFAIL2(m,a1) \
289 unsigned offset = strlen(PL_regprecomp)-(PL_regxend-PL_regcomp_parse); \
291 S_re_croak2(aTHX_ m, REPORT_LOCATION, a1, \
292 offset, PL_regprecomp, PL_regprecomp + offset); \
296 * Calls SAVEDESTRUCTOR_X if needed, then Simple_vFAIL2().
298 #define vFAIL2(m,a1) \
301 SAVEDESTRUCTOR_X(clear_re,(void*)PL_regcomp_rx); \
302 Simple_vFAIL2(m, a1); \
307 * Like Simple_vFAIL(), but accepts three arguments.
309 #define Simple_vFAIL3(m, a1, a2) \
311 unsigned offset = strlen(PL_regprecomp)-(PL_regxend-PL_regcomp_parse); \
313 S_re_croak2(aTHX_ m, REPORT_LOCATION, a1, a2, \
314 offset, PL_regprecomp, PL_regprecomp + offset); \
318 * Calls SAVEDESTRUCTOR_X if needed, then Simple_vFAIL3().
320 #define vFAIL3(m,a1,a2) \
323 SAVEDESTRUCTOR_X(clear_re,(void*)PL_regcomp_rx); \
324 Simple_vFAIL3(m, a1, a2); \
328 * Like Simple_vFAIL(), but accepts four arguments.
330 #define Simple_vFAIL4(m, a1, a2, a3) \
332 unsigned offset = strlen(PL_regprecomp)-(PL_regxend-PL_regcomp_parse); \
334 S_re_croak2(aTHX_ m, REPORT_LOCATION, a1, a2, a3,\
335 offset, PL_regprecomp, PL_regprecomp + offset); \
339 * Like Simple_vFAIL(), but accepts five arguments.
341 #define Simple_vFAIL5(m, a1, a2, a3, a4) \
343 unsigned offset = strlen(PL_regprecomp)-(PL_regxend-PL_regcomp_parse); \
344 S_re_croak2(aTHX_ m, REPORT_LOCATION, a1, a2, a3, a4,\
345 offset, PL_regprecomp, PL_regprecomp + offset); \
349 #define vWARN(loc,m) \
351 unsigned offset = strlen(PL_regprecomp)-(PL_regxend-(loc)); \
352 Perl_warner(aTHX_ WARN_REGEXP, "%s" REPORT_LOCATION,\
353 m, offset, PL_regprecomp, PL_regprecomp + offset); \
357 #define vWARN2(loc, m, a1) \
359 unsigned offset = strlen(PL_regprecomp)-(PL_regxend-(loc)); \
360 Perl_warner(aTHX_ WARN_REGEXP, m REPORT_LOCATION,\
362 offset, PL_regprecomp, PL_regprecomp + offset); \
365 #define vWARN3(loc, m, a1, a2) \
367 unsigned offset = strlen(PL_regprecomp) - (PL_regxend - (loc)); \
368 Perl_warner(aTHX_ WARN_REGEXP, m REPORT_LOCATION, \
370 offset, PL_regprecomp, PL_regprecomp + offset); \
373 #define vWARN4(loc, m, a1, a2, a3) \
375 unsigned offset = strlen(PL_regprecomp)-(PL_regxend-(loc)); \
376 Perl_warner(aTHX_ WARN_REGEXP, m REPORT_LOCATION,\
378 offset, PL_regprecomp, PL_regprecomp + offset); \
383 /* Allow for side effects in s */
384 #define REGC(c,s) STMT_START { if (!SIZE_ONLY) *(s) = (c); else (s);} STMT_END
386 static void clear_re(pTHXo_ void *r);
388 /* Mark that we cannot extend a found fixed substring at this point.
389 Updata the longest found anchored substring and the longest found
390 floating substrings if needed. */
393 S_scan_commit(pTHX_ scan_data_t *data)
396 STRLEN l = CHR_SVLEN(data->last_found);
397 STRLEN old_l = CHR_SVLEN(*data->longest);
399 if ((l >= old_l) && ((l > old_l) || (data->flags & SF_BEFORE_EOL))) {
400 sv_setsv(*data->longest, data->last_found);
401 if (*data->longest == data->longest_fixed) {
402 data->offset_fixed = l ? data->last_start_min : data->pos_min;
403 if (data->flags & SF_BEFORE_EOL)
405 |= ((data->flags & SF_BEFORE_EOL) << SF_FIX_SHIFT_EOL);
407 data->flags &= ~SF_FIX_BEFORE_EOL;
410 data->offset_float_min = l ? data->last_start_min : data->pos_min;
411 data->offset_float_max = (l
412 ? data->last_start_max
413 : data->pos_min + data->pos_delta);
414 if (data->flags & SF_BEFORE_EOL)
416 |= ((data->flags & SF_BEFORE_EOL) << SF_FL_SHIFT_EOL);
418 data->flags &= ~SF_FL_BEFORE_EOL;
421 SvCUR_set(data->last_found, 0);
423 data->flags &= ~SF_BEFORE_EOL;
426 /* Can match anything (initialization) */
428 S_cl_anything(pTHX_ struct regnode_charclass_class *cl)
432 ANYOF_CLASS_ZERO(cl);
433 for (value = 0; value < 256; ++value)
434 ANYOF_BITMAP_SET(cl, value);
435 cl->flags = ANYOF_EOS;
437 cl->flags |= ANYOF_LOCALE;
440 /* Can match anything (initialization) */
442 S_cl_is_anything(pTHX_ struct regnode_charclass_class *cl)
446 for (value = 0; value < ANYOF_MAX; value += 2)
447 if (ANYOF_CLASS_TEST(cl, value) && ANYOF_CLASS_TEST(cl, value + 1))
449 for (value = 0; value < 256; ++value)
450 if (!ANYOF_BITMAP_TEST(cl, value))
455 /* Can match anything (initialization) */
457 S_cl_init(pTHX_ struct regnode_charclass_class *cl)
459 Zero(cl, 1, struct regnode_charclass_class);
465 S_cl_init_zero(pTHX_ struct regnode_charclass_class *cl)
467 Zero(cl, 1, struct regnode_charclass_class);
471 cl->flags |= ANYOF_LOCALE;
474 /* 'And' a given class with another one. Can create false positives */
475 /* We assume that cl is not inverted */
477 S_cl_and(pTHX_ struct regnode_charclass_class *cl,
478 struct regnode_charclass_class *and_with)
480 if (!(and_with->flags & ANYOF_CLASS)
481 && !(cl->flags & ANYOF_CLASS)
482 && (and_with->flags & ANYOF_LOCALE) == (cl->flags & ANYOF_LOCALE)
483 && !(and_with->flags & ANYOF_FOLD)
484 && !(cl->flags & ANYOF_FOLD)) {
487 if (and_with->flags & ANYOF_INVERT)
488 for (i = 0; i < ANYOF_BITMAP_SIZE; i++)
489 cl->bitmap[i] &= ~and_with->bitmap[i];
491 for (i = 0; i < ANYOF_BITMAP_SIZE; i++)
492 cl->bitmap[i] &= and_with->bitmap[i];
493 } /* XXXX: logic is complicated otherwise, leave it along for a moment. */
494 if (!(and_with->flags & ANYOF_EOS))
495 cl->flags &= ~ANYOF_EOS;
498 /* 'OR' a given class with another one. Can create false positives */
499 /* We assume that cl is not inverted */
501 S_cl_or(pTHX_ struct regnode_charclass_class *cl, struct regnode_charclass_class *or_with)
503 if (or_with->flags & ANYOF_INVERT) {
505 * (B1 | CL1) | (!B2 & !CL2) = (B1 | !B2 & !CL2) | (CL1 | (!B2 & !CL2))
506 * <= (B1 | !B2) | (CL1 | !CL2)
507 * which is wasteful if CL2 is small, but we ignore CL2:
508 * (B1 | CL1) | (!B2 & !CL2) <= (B1 | CL1) | !B2 = (B1 | !B2) | CL1
509 * XXXX Can we handle case-fold? Unclear:
510 * (OK1(i) | OK1(i')) | !(OK1(i) | OK1(i')) =
511 * (OK1(i) | OK1(i')) | (!OK1(i) & !OK1(i'))
513 if ( (or_with->flags & ANYOF_LOCALE) == (cl->flags & ANYOF_LOCALE)
514 && !(or_with->flags & ANYOF_FOLD)
515 && !(cl->flags & ANYOF_FOLD) ) {
518 for (i = 0; i < ANYOF_BITMAP_SIZE; i++)
519 cl->bitmap[i] |= ~or_with->bitmap[i];
520 } /* XXXX: logic is complicated otherwise */
525 /* (B1 | CL1) | (B2 | CL2) = (B1 | B2) | (CL1 | CL2)) */
526 if ( (or_with->flags & ANYOF_LOCALE) == (cl->flags & ANYOF_LOCALE)
527 && (!(or_with->flags & ANYOF_FOLD)
528 || (cl->flags & ANYOF_FOLD)) ) {
531 /* OR char bitmap and class bitmap separately */
532 for (i = 0; i < ANYOF_BITMAP_SIZE; i++)
533 cl->bitmap[i] |= or_with->bitmap[i];
534 if (or_with->flags & ANYOF_CLASS) {
535 for (i = 0; i < ANYOF_CLASSBITMAP_SIZE; i++)
536 cl->classflags[i] |= or_with->classflags[i];
537 cl->flags |= ANYOF_CLASS;
540 else { /* XXXX: logic is complicated, leave it along for a moment. */
544 if (or_with->flags & ANYOF_EOS)
545 cl->flags |= ANYOF_EOS;
548 /* REx optimizer. Converts nodes into quickier variants "in place".
549 Finds fixed substrings. */
551 /* Stops at toplevel WHILEM as well as at `last'. At end *scanp is set
552 to the position after last scanned or to NULL. */
555 S_study_chunk(pTHX_ regnode **scanp, I32 *deltap, regnode *last, scan_data_t *data, U32 flags)
556 /* scanp: Start here (read-write). */
557 /* deltap: Write maxlen-minlen here. */
558 /* last: Stop before this one. */
561 I32 min = 0, pars = 0, code;
562 regnode *scan = *scanp, *next;
564 int is_inf = (flags & SCF_DO_SUBSTR) && (data->flags & SF_IS_INF);
565 int is_inf_internal = 0; /* The studied chunk is infinite */
566 I32 is_par = OP(scan) == OPEN ? ARG(scan) : 0;
567 scan_data_t data_fake;
568 struct regnode_charclass_class and_with; /* Valid if flags & SCF_DO_STCLASS_OR */
570 while (scan && OP(scan) != END && scan < last) {
571 /* Peephole optimizer: */
573 if (PL_regkind[(U8)OP(scan)] == EXACT) {
574 /* Merge several consecutive EXACTish nodes into one. */
575 regnode *n = regnext(scan);
578 regnode *stop = scan;
581 next = scan + NODE_SZ_STR(scan);
582 /* Skip NOTHING, merge EXACT*. */
584 ( PL_regkind[(U8)OP(n)] == NOTHING ||
585 (stringok && (OP(n) == OP(scan))))
587 && NEXT_OFF(scan) + NEXT_OFF(n) < I16_MAX) {
588 if (OP(n) == TAIL || n > next)
590 if (PL_regkind[(U8)OP(n)] == NOTHING) {
591 NEXT_OFF(scan) += NEXT_OFF(n);
592 next = n + NODE_STEP_REGNODE;
600 int oldl = STR_LEN(scan);
601 regnode *nnext = regnext(n);
603 if (oldl + STR_LEN(n) > U8_MAX)
605 NEXT_OFF(scan) += NEXT_OFF(n);
606 STR_LEN(scan) += STR_LEN(n);
607 next = n + NODE_SZ_STR(n);
608 /* Now we can overwrite *n : */
609 Move(STRING(n), STRING(scan) + oldl,
620 n = scan + NODE_SZ_STR(scan);
622 if (PL_regkind[(U8)OP(n)] != NOTHING || OP(n) == NOTHING) {
630 /* Follow the next-chain of the current node and optimize
631 away all the NOTHINGs from it. */
632 if (OP(scan) != CURLYX) {
633 int max = (reg_off_by_arg[OP(scan)]
635 /* I32 may be smaller than U16 on CRAYs! */
636 : (I32_MAX < U16_MAX ? I32_MAX : U16_MAX));
637 int off = (reg_off_by_arg[OP(scan)] ? ARG(scan) : NEXT_OFF(scan));
641 /* Skip NOTHING and LONGJMP. */
642 while ((n = regnext(n))
643 && ((PL_regkind[(U8)OP(n)] == NOTHING && (noff = NEXT_OFF(n)))
644 || ((OP(n) == LONGJMP) && (noff = ARG(n))))
647 if (reg_off_by_arg[OP(scan)])
650 NEXT_OFF(scan) = off;
652 /* The principal pseudo-switch. Cannot be a switch, since we
653 look into several different things. */
654 if (OP(scan) == BRANCH || OP(scan) == BRANCHJ
655 || OP(scan) == IFTHEN || OP(scan) == SUSPEND) {
656 next = regnext(scan);
659 if (OP(next) == code || code == IFTHEN || code == SUSPEND) {
660 I32 max1 = 0, min1 = I32_MAX, num = 0;
661 struct regnode_charclass_class accum;
663 if (flags & SCF_DO_SUBSTR) /* XXXX Add !SUSPEND? */
664 scan_commit(data); /* Cannot merge strings after this. */
665 if (flags & SCF_DO_STCLASS)
666 cl_init_zero(&accum);
667 while (OP(scan) == code) {
668 I32 deltanext, minnext, f = 0;
669 struct regnode_charclass_class this_class;
674 data_fake.whilem_c = data->whilem_c;
675 next = regnext(scan);
676 scan = NEXTOPER(scan);
678 scan = NEXTOPER(scan);
679 if (flags & SCF_DO_STCLASS) {
680 cl_init(&this_class);
681 data_fake.start_class = &this_class;
682 f = SCF_DO_STCLASS_AND;
684 /* we suppose the run is continuous, last=next...*/
685 minnext = study_chunk(&scan, &deltanext, next,
689 if (max1 < minnext + deltanext)
690 max1 = minnext + deltanext;
691 if (deltanext == I32_MAX)
692 is_inf = is_inf_internal = 1;
694 if (data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
696 if (data && (data_fake.flags & SF_HAS_EVAL))
697 data->flags |= SF_HAS_EVAL;
699 data->whilem_c = data_fake.whilem_c;
700 if (flags & SCF_DO_STCLASS)
701 cl_or(&accum, &this_class);
705 if (code == IFTHEN && num < 2) /* Empty ELSE branch */
707 if (flags & SCF_DO_SUBSTR) {
708 data->pos_min += min1;
709 data->pos_delta += max1 - min1;
710 if (max1 != min1 || is_inf)
711 data->longest = &(data->longest_float);
714 delta += max1 - min1;
715 if (flags & SCF_DO_STCLASS_OR) {
716 cl_or(data->start_class, &accum);
718 cl_and(data->start_class, &and_with);
719 flags &= ~SCF_DO_STCLASS;
722 else if (flags & SCF_DO_STCLASS_AND) {
724 cl_and(data->start_class, &accum);
725 flags &= ~SCF_DO_STCLASS;
728 /* Switch to OR mode: cache the old value of
729 * data->start_class */
730 StructCopy(data->start_class, &and_with,
731 struct regnode_charclass_class);
732 flags &= ~SCF_DO_STCLASS_AND;
733 StructCopy(&accum, data->start_class,
734 struct regnode_charclass_class);
735 flags |= SCF_DO_STCLASS_OR;
736 data->start_class->flags |= ANYOF_EOS;
740 else if (code == BRANCHJ) /* single branch is optimized. */
741 scan = NEXTOPER(NEXTOPER(scan));
742 else /* single branch is optimized. */
743 scan = NEXTOPER(scan);
746 else if (OP(scan) == EXACT) {
747 I32 l = STR_LEN(scan);
749 unsigned char *s = (unsigned char *)STRING(scan);
750 unsigned char *e = s + l;
759 if (flags & SCF_DO_SUBSTR) { /* Update longest substr. */
760 /* The code below prefers earlier match for fixed
761 offset, later match for variable offset. */
762 if (data->last_end == -1) { /* Update the start info. */
763 data->last_start_min = data->pos_min;
764 data->last_start_max = is_inf
765 ? I32_MAX : data->pos_min + data->pos_delta;
767 sv_catpvn(data->last_found, STRING(scan), STR_LEN(scan));
768 data->last_end = data->pos_min + l;
769 data->pos_min += l; /* As in the first entry. */
770 data->flags &= ~SF_BEFORE_EOL;
772 if (flags & SCF_DO_STCLASS_AND) {
773 /* Check whether it is compatible with what we know already! */
776 if (!(data->start_class->flags & (ANYOF_CLASS | ANYOF_LOCALE))
777 && !ANYOF_BITMAP_TEST(data->start_class, *STRING(scan))
778 && (!(data->start_class->flags & ANYOF_FOLD)
779 || !ANYOF_BITMAP_TEST(data->start_class,
780 PL_fold[*(U8*)STRING(scan)])))
782 ANYOF_CLASS_ZERO(data->start_class);
783 ANYOF_BITMAP_ZERO(data->start_class);
785 ANYOF_BITMAP_SET(data->start_class, *STRING(scan));
786 data->start_class->flags &= ~ANYOF_EOS;
788 else if (flags & SCF_DO_STCLASS_OR) {
789 /* false positive possible if the class is case-folded */
790 ANYOF_BITMAP_SET(data->start_class, *STRING(scan));
791 data->start_class->flags &= ~ANYOF_EOS;
792 cl_and(data->start_class, &and_with);
794 flags &= ~SCF_DO_STCLASS;
796 else if (PL_regkind[(U8)OP(scan)] == EXACT) { /* But OP != EXACT! */
797 I32 l = STR_LEN(scan);
799 /* Search for fixed substrings supports EXACT only. */
800 if (flags & SCF_DO_SUBSTR)
803 unsigned char *s = (unsigned char *)STRING(scan);
804 unsigned char *e = s + l;
813 if (data && (flags & SCF_DO_SUBSTR))
815 if (flags & SCF_DO_STCLASS_AND) {
816 /* Check whether it is compatible with what we know already! */
819 if (!(data->start_class->flags & (ANYOF_CLASS | ANYOF_LOCALE))
820 && !ANYOF_BITMAP_TEST(data->start_class, *STRING(scan))
821 && !ANYOF_BITMAP_TEST(data->start_class,
822 PL_fold[*(U8*)STRING(scan)]))
824 ANYOF_CLASS_ZERO(data->start_class);
825 ANYOF_BITMAP_ZERO(data->start_class);
827 ANYOF_BITMAP_SET(data->start_class, *STRING(scan));
828 data->start_class->flags &= ~ANYOF_EOS;
829 data->start_class->flags |= ANYOF_FOLD;
830 if (OP(scan) == EXACTFL)
831 data->start_class->flags |= ANYOF_LOCALE;
834 else if (flags & SCF_DO_STCLASS_OR) {
835 if (data->start_class->flags & ANYOF_FOLD) {
836 /* false positive possible if the class is case-folded.
837 Assume that the locale settings are the same... */
838 ANYOF_BITMAP_SET(data->start_class, *STRING(scan));
839 data->start_class->flags &= ~ANYOF_EOS;
841 cl_and(data->start_class, &and_with);
843 flags &= ~SCF_DO_STCLASS;
845 else if (strchr((char*)PL_varies,OP(scan))) {
846 I32 mincount, maxcount, minnext, deltanext, pos_before, fl;
848 regnode *oscan = scan;
849 struct regnode_charclass_class this_class;
850 struct regnode_charclass_class *oclass = NULL;
852 switch (PL_regkind[(U8)OP(scan)]) {
853 case WHILEM: /* End of (?:...)* . */
854 scan = NEXTOPER(scan);
857 if (flags & (SCF_DO_SUBSTR | SCF_DO_STCLASS)) {
858 next = NEXTOPER(scan);
859 if (OP(next) == EXACT || (flags & SCF_DO_STCLASS)) {
861 maxcount = REG_INFTY;
862 next = regnext(scan);
863 scan = NEXTOPER(scan);
867 if (flags & SCF_DO_SUBSTR)
872 if (flags & SCF_DO_STCLASS) {
874 maxcount = REG_INFTY;
875 next = regnext(scan);
876 scan = NEXTOPER(scan);
879 is_inf = is_inf_internal = 1;
880 scan = regnext(scan);
881 if (flags & SCF_DO_SUBSTR) {
882 scan_commit(data); /* Cannot extend fixed substrings */
883 data->longest = &(data->longest_float);
885 goto optimize_curly_tail;
887 mincount = ARG1(scan);
888 maxcount = ARG2(scan);
889 next = regnext(scan);
890 scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
892 if (flags & SCF_DO_SUBSTR) {
893 if (mincount == 0) scan_commit(data); /* Cannot extend fixed substrings */
894 pos_before = data->pos_min;
898 data->flags &= ~(SF_HAS_PAR|SF_IN_PAR|SF_HAS_EVAL);
900 data->flags |= SF_IS_INF;
902 if (flags & SCF_DO_STCLASS) {
903 cl_init(&this_class);
904 oclass = data->start_class;
905 data->start_class = &this_class;
906 f |= SCF_DO_STCLASS_AND;
907 f &= ~SCF_DO_STCLASS_OR;
910 /* This will finish on WHILEM, setting scan, or on NULL: */
911 minnext = study_chunk(&scan, &deltanext, last, data,
913 ? (f & ~SCF_DO_SUBSTR) : f);
915 if (flags & SCF_DO_STCLASS)
916 data->start_class = oclass;
917 if (mincount == 0 || minnext == 0) {
918 if (flags & SCF_DO_STCLASS_OR) {
919 cl_or(data->start_class, &this_class);
921 else if (flags & SCF_DO_STCLASS_AND) {
922 /* Switch to OR mode: cache the old value of
923 * data->start_class */
924 StructCopy(data->start_class, &and_with,
925 struct regnode_charclass_class);
926 flags &= ~SCF_DO_STCLASS_AND;
927 StructCopy(&this_class, data->start_class,
928 struct regnode_charclass_class);
929 flags |= SCF_DO_STCLASS_OR;
930 data->start_class->flags |= ANYOF_EOS;
932 } else { /* Non-zero len */
933 if (flags & SCF_DO_STCLASS_OR) {
934 cl_or(data->start_class, &this_class);
935 cl_and(data->start_class, &and_with);
937 else if (flags & SCF_DO_STCLASS_AND)
938 cl_and(data->start_class, &this_class);
939 flags &= ~SCF_DO_STCLASS;
941 if (!scan) /* It was not CURLYX, but CURLY. */
943 if (ckWARN(WARN_REGEXP) && (minnext + deltanext == 0)
944 && !(data->flags & (SF_HAS_PAR|SF_IN_PAR))
945 && maxcount <= REG_INFTY/3) /* Complement check for big count */
947 vWARN(PL_regcomp_parse,
948 "Quantifier unexpected on zero-length expression");
951 min += minnext * mincount;
952 is_inf_internal |= ((maxcount == REG_INFTY
953 && (minnext + deltanext) > 0)
954 || deltanext == I32_MAX);
955 is_inf |= is_inf_internal;
956 delta += (minnext + deltanext) * maxcount - minnext * mincount;
958 /* Try powerful optimization CURLYX => CURLYN. */
959 if ( OP(oscan) == CURLYX && data
960 && data->flags & SF_IN_PAR
961 && !(data->flags & SF_HAS_EVAL)
962 && !deltanext && minnext == 1 ) {
963 /* Try to optimize to CURLYN. */
964 regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS;
965 regnode *nxt1 = nxt, *nxt2;
969 if (!strchr((char*)PL_simple,OP(nxt))
970 && !(PL_regkind[(U8)OP(nxt)] == EXACT
971 && STR_LEN(nxt) == 1))
975 if (OP(nxt) != CLOSE)
977 /* Now we know that nxt2 is the only contents: */
978 oscan->flags = ARG(nxt);
980 OP(nxt1) = NOTHING; /* was OPEN. */
982 OP(nxt1 + 1) = OPTIMIZED; /* was count. */
983 NEXT_OFF(nxt1+ 1) = 0; /* just for consistancy. */
984 NEXT_OFF(nxt2) = 0; /* just for consistancy with CURLY. */
985 OP(nxt) = OPTIMIZED; /* was CLOSE. */
986 OP(nxt + 1) = OPTIMIZED; /* was count. */
987 NEXT_OFF(nxt+ 1) = 0; /* just for consistancy. */
992 /* Try optimization CURLYX => CURLYM. */
993 if ( OP(oscan) == CURLYX && data
994 && !(data->flags & SF_HAS_PAR)
995 && !(data->flags & SF_HAS_EVAL)
997 /* XXXX How to optimize if data == 0? */
998 /* Optimize to a simpler form. */
999 regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN */
1003 while ( (nxt2 = regnext(nxt)) /* skip over embedded stuff*/
1004 && (OP(nxt2) != WHILEM))
1006 OP(nxt2) = SUCCEED; /* Whas WHILEM */
1007 /* Need to optimize away parenths. */
1008 if (data->flags & SF_IN_PAR) {
1009 /* Set the parenth number. */
1010 regnode *nxt1 = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN*/
1012 if (OP(nxt) != CLOSE)
1013 FAIL("Panic opt close");
1014 oscan->flags = ARG(nxt);
1015 OP(nxt1) = OPTIMIZED; /* was OPEN. */
1016 OP(nxt) = OPTIMIZED; /* was CLOSE. */
1018 OP(nxt1 + 1) = OPTIMIZED; /* was count. */
1019 OP(nxt + 1) = OPTIMIZED; /* was count. */
1020 NEXT_OFF(nxt1 + 1) = 0; /* just for consistancy. */
1021 NEXT_OFF(nxt + 1) = 0; /* just for consistancy. */
1024 while ( nxt1 && (OP(nxt1) != WHILEM)) {
1025 regnode *nnxt = regnext(nxt1);
1028 if (reg_off_by_arg[OP(nxt1)])
1029 ARG_SET(nxt1, nxt2 - nxt1);
1030 else if (nxt2 - nxt1 < U16_MAX)
1031 NEXT_OFF(nxt1) = nxt2 - nxt1;
1033 OP(nxt) = NOTHING; /* Cannot beautify */
1038 /* Optimize again: */
1039 study_chunk(&nxt1, &deltanext, nxt, NULL, 0);
1044 else if (OP(oscan) == CURLYX && data && ++data->whilem_c < 16) {
1045 /* This stays as CURLYX, and can put the count/of pair. */
1046 /* Find WHILEM (as in regexec.c) */
1047 regnode *nxt = oscan + NEXT_OFF(oscan);
1049 if (OP(PREVOPER(nxt)) == NOTHING) /* LONGJMP */
1051 PREVOPER(nxt)->flags = data->whilem_c
1052 | (PL_reg_whilem_seen << 4); /* On WHILEM */
1054 if (data && fl & (SF_HAS_PAR|SF_IN_PAR))
1056 if (flags & SCF_DO_SUBSTR) {
1057 SV *last_str = Nullsv;
1058 int counted = mincount != 0;
1060 if (data->last_end > 0 && mincount != 0) { /* Ends with a string. */
1061 I32 b = pos_before >= data->last_start_min
1062 ? pos_before : data->last_start_min;
1064 char *s = SvPV(data->last_found, l);
1065 I32 old = b - data->last_start_min;
1068 old = utf8_hop((U8*)s, old) - (U8*)s;
1071 /* Get the added string: */
1072 last_str = newSVpvn(s + old, l);
1073 if (deltanext == 0 && pos_before == b) {
1074 /* What was added is a constant string */
1076 SvGROW(last_str, (mincount * l) + 1);
1077 repeatcpy(SvPVX(last_str) + l,
1078 SvPVX(last_str), l, mincount - 1);
1079 SvCUR(last_str) *= mincount;
1080 /* Add additional parts. */
1081 SvCUR_set(data->last_found,
1082 SvCUR(data->last_found) - l);
1083 sv_catsv(data->last_found, last_str);
1084 data->last_end += l * (mincount - 1);
1087 /* start offset must point into the last copy */
1088 data->last_start_min += minnext * (mincount - 1);
1089 data->last_start_max += is_inf ? 0 : (maxcount - 1)
1090 * (minnext + data->pos_delta);
1093 /* It is counted once already... */
1094 data->pos_min += minnext * (mincount - counted);
1095 data->pos_delta += - counted * deltanext +
1096 (minnext + deltanext) * maxcount - minnext * mincount;
1097 if (mincount != maxcount) {
1098 /* Cannot extend fixed substrings found inside
1101 if (mincount && last_str) {
1102 sv_setsv(data->last_found, last_str);
1103 data->last_end = data->pos_min;
1104 data->last_start_min =
1105 data->pos_min - CHR_SVLEN(last_str);
1106 data->last_start_max = is_inf
1108 : data->pos_min + data->pos_delta
1109 - CHR_SVLEN(last_str);
1111 data->longest = &(data->longest_float);
1113 SvREFCNT_dec(last_str);
1115 if (data && (fl & SF_HAS_EVAL))
1116 data->flags |= SF_HAS_EVAL;
1117 optimize_curly_tail:
1118 if (OP(oscan) != CURLYX) {
1119 while (PL_regkind[(U8)OP(next = regnext(oscan))] == NOTHING
1121 NEXT_OFF(oscan) += NEXT_OFF(next);
1124 default: /* REF and CLUMP only? */
1125 if (flags & SCF_DO_SUBSTR) {
1126 scan_commit(data); /* Cannot expect anything... */
1127 data->longest = &(data->longest_float);
1129 is_inf = is_inf_internal = 1;
1130 if (flags & SCF_DO_STCLASS_OR)
1131 cl_anything(data->start_class);
1132 flags &= ~SCF_DO_STCLASS;
1136 else if (strchr((char*)PL_simple,OP(scan)) || PL_regkind[(U8)OP(scan)] == ANYUTF8) {
1139 if (flags & SCF_DO_SUBSTR) {
1144 if (flags & SCF_DO_STCLASS) {
1145 data->start_class->flags &= ~ANYOF_EOS; /* No match on empty */
1147 /* Some of the logic below assumes that switching
1148 locale on will only add false positives. */
1149 switch (PL_regkind[(U8)OP(scan)]) {
1166 /* Perl_croak(aTHX_ "panic: unexpected simple REx opcode %d", OP(scan)); */
1167 if (flags & SCF_DO_STCLASS_OR) /* Allow everything */
1168 cl_anything(data->start_class);
1171 if (OP(scan) == SANY)
1173 if (flags & SCF_DO_STCLASS_OR) { /* Everything but \n */
1174 value = (ANYOF_BITMAP_TEST(data->start_class,'\n')
1175 || (data->start_class->flags & ANYOF_CLASS));
1176 cl_anything(data->start_class);
1178 if (flags & SCF_DO_STCLASS_AND || !value)
1179 ANYOF_BITMAP_CLEAR(data->start_class,'\n');
1182 if (flags & SCF_DO_STCLASS_AND)
1183 cl_and(data->start_class,
1184 (struct regnode_charclass_class*)scan);
1186 cl_or(data->start_class,
1187 (struct regnode_charclass_class*)scan);
1190 if (flags & SCF_DO_STCLASS_AND) {
1191 if (!(data->start_class->flags & ANYOF_LOCALE)) {
1192 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NALNUM);
1193 for (value = 0; value < 256; value++)
1194 if (!isALNUM(value))
1195 ANYOF_BITMAP_CLEAR(data->start_class, value);
1199 if (data->start_class->flags & ANYOF_LOCALE)
1200 ANYOF_CLASS_SET(data->start_class,ANYOF_ALNUM);
1202 for (value = 0; value < 256; value++)
1204 ANYOF_BITMAP_SET(data->start_class, value);
1209 if (flags & SCF_DO_STCLASS_AND) {
1210 if (data->start_class->flags & ANYOF_LOCALE)
1211 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NALNUM);
1214 ANYOF_CLASS_SET(data->start_class,ANYOF_ALNUM);
1215 data->start_class->flags |= ANYOF_LOCALE;
1219 if (flags & SCF_DO_STCLASS_AND) {
1220 if (!(data->start_class->flags & ANYOF_LOCALE)) {
1221 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_ALNUM);
1222 for (value = 0; value < 256; value++)
1224 ANYOF_BITMAP_CLEAR(data->start_class, value);
1228 if (data->start_class->flags & ANYOF_LOCALE)
1229 ANYOF_CLASS_SET(data->start_class,ANYOF_NALNUM);
1231 for (value = 0; value < 256; value++)
1232 if (!isALNUM(value))
1233 ANYOF_BITMAP_SET(data->start_class, value);
1238 if (flags & SCF_DO_STCLASS_AND) {
1239 if (data->start_class->flags & ANYOF_LOCALE)
1240 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_ALNUM);
1243 data->start_class->flags |= ANYOF_LOCALE;
1244 ANYOF_CLASS_SET(data->start_class,ANYOF_NALNUM);
1248 if (flags & SCF_DO_STCLASS_AND) {
1249 if (!(data->start_class->flags & ANYOF_LOCALE)) {
1250 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NSPACE);
1251 for (value = 0; value < 256; value++)
1252 if (!isSPACE(value))
1253 ANYOF_BITMAP_CLEAR(data->start_class, value);
1257 if (data->start_class->flags & ANYOF_LOCALE)
1258 ANYOF_CLASS_SET(data->start_class,ANYOF_SPACE);
1260 for (value = 0; value < 256; value++)
1262 ANYOF_BITMAP_SET(data->start_class, value);
1267 if (flags & SCF_DO_STCLASS_AND) {
1268 if (data->start_class->flags & ANYOF_LOCALE)
1269 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NSPACE);
1272 data->start_class->flags |= ANYOF_LOCALE;
1273 ANYOF_CLASS_SET(data->start_class,ANYOF_SPACE);
1277 if (flags & SCF_DO_STCLASS_AND) {
1278 if (!(data->start_class->flags & ANYOF_LOCALE)) {
1279 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_SPACE);
1280 for (value = 0; value < 256; value++)
1282 ANYOF_BITMAP_CLEAR(data->start_class, value);
1286 if (data->start_class->flags & ANYOF_LOCALE)
1287 ANYOF_CLASS_SET(data->start_class,ANYOF_NSPACE);
1289 for (value = 0; value < 256; value++)
1290 if (!isSPACE(value))
1291 ANYOF_BITMAP_SET(data->start_class, value);
1296 if (flags & SCF_DO_STCLASS_AND) {
1297 if (data->start_class->flags & ANYOF_LOCALE) {
1298 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_SPACE);
1299 for (value = 0; value < 256; value++)
1300 if (!isSPACE(value))
1301 ANYOF_BITMAP_CLEAR(data->start_class, value);
1305 data->start_class->flags |= ANYOF_LOCALE;
1306 ANYOF_CLASS_SET(data->start_class,ANYOF_NSPACE);
1310 if (flags & SCF_DO_STCLASS_AND) {
1311 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NDIGIT);
1312 for (value = 0; value < 256; value++)
1313 if (!isDIGIT(value))
1314 ANYOF_BITMAP_CLEAR(data->start_class, value);
1317 if (data->start_class->flags & ANYOF_LOCALE)
1318 ANYOF_CLASS_SET(data->start_class,ANYOF_DIGIT);
1320 for (value = 0; value < 256; value++)
1322 ANYOF_BITMAP_SET(data->start_class, value);
1327 if (flags & SCF_DO_STCLASS_AND) {
1328 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_DIGIT);
1329 for (value = 0; value < 256; value++)
1331 ANYOF_BITMAP_CLEAR(data->start_class, value);
1334 if (data->start_class->flags & ANYOF_LOCALE)
1335 ANYOF_CLASS_SET(data->start_class,ANYOF_NDIGIT);
1337 for (value = 0; value < 256; value++)
1338 if (!isDIGIT(value))
1339 ANYOF_BITMAP_SET(data->start_class, value);
1344 if (flags & SCF_DO_STCLASS_OR)
1345 cl_and(data->start_class, &and_with);
1346 flags &= ~SCF_DO_STCLASS;
1349 else if (PL_regkind[(U8)OP(scan)] == EOL && flags & SCF_DO_SUBSTR) {
1350 data->flags |= (OP(scan) == MEOL
1354 else if ( PL_regkind[(U8)OP(scan)] == BRANCHJ
1355 /* Lookbehind, or need to calculate parens/evals/stclass: */
1356 && (scan->flags || data || (flags & SCF_DO_STCLASS))
1357 && (OP(scan) == IFMATCH || OP(scan) == UNLESSM)) {
1358 /* Lookahead/lookbehind */
1359 I32 deltanext, minnext;
1361 struct regnode_charclass_class intrnl;
1364 data_fake.flags = 0;
1366 data_fake.whilem_c = data->whilem_c;
1367 if ( flags & SCF_DO_STCLASS && !scan->flags
1368 && OP(scan) == IFMATCH ) { /* Lookahead */
1370 data_fake.start_class = &intrnl;
1371 f = SCF_DO_STCLASS_AND;
1373 next = regnext(scan);
1374 nscan = NEXTOPER(NEXTOPER(scan));
1375 minnext = study_chunk(&nscan, &deltanext, last, &data_fake, f);
1378 vFAIL("Variable length lookbehind not implemented");
1380 else if (minnext > U8_MAX) {
1381 vFAIL2("Lookbehind longer than %"UVuf" not implemented", (UV)U8_MAX);
1383 scan->flags = minnext;
1385 if (data && data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
1387 if (data && (data_fake.flags & SF_HAS_EVAL))
1388 data->flags |= SF_HAS_EVAL;
1390 data->whilem_c = data_fake.whilem_c;
1392 int was = (data->start_class->flags & ANYOF_EOS);
1394 cl_and(data->start_class, &intrnl);
1396 data->start_class->flags |= ANYOF_EOS;
1399 else if (OP(scan) == OPEN) {
1402 else if (OP(scan) == CLOSE && ARG(scan) == is_par) {
1403 next = regnext(scan);
1405 if ( next && (OP(next) != WHILEM) && next < last)
1406 is_par = 0; /* Disable optimization */
1408 else if (OP(scan) == EVAL) {
1410 data->flags |= SF_HAS_EVAL;
1412 else if (OP(scan) == LOGICAL && scan->flags == 2) { /* Embedded follows */
1413 if (flags & SCF_DO_SUBSTR) {
1415 data->longest = &(data->longest_float);
1417 is_inf = is_inf_internal = 1;
1418 if (flags & SCF_DO_STCLASS_OR) /* Allow everything */
1419 cl_anything(data->start_class);
1420 flags &= ~SCF_DO_STCLASS;
1422 /* Else: zero-length, ignore. */
1423 scan = regnext(scan);
1428 *deltap = is_inf_internal ? I32_MAX : delta;
1429 if (flags & SCF_DO_SUBSTR && is_inf)
1430 data->pos_delta = I32_MAX - data->pos_min;
1431 if (is_par > U8_MAX)
1433 if (is_par && pars==1 && data) {
1434 data->flags |= SF_IN_PAR;
1435 data->flags &= ~SF_HAS_PAR;
1437 else if (pars && data) {
1438 data->flags |= SF_HAS_PAR;
1439 data->flags &= ~SF_IN_PAR;
1441 if (flags & SCF_DO_STCLASS_OR)
1442 cl_and(data->start_class, &and_with);
1447 S_add_data(pTHX_ I32 n, char *s)
1450 if (PL_regcomp_rx->data) {
1451 Renewc(PL_regcomp_rx->data,
1452 sizeof(*PL_regcomp_rx->data) + sizeof(void*) * (PL_regcomp_rx->data->count + n - 1),
1453 char, struct reg_data);
1454 Renew(PL_regcomp_rx->data->what, PL_regcomp_rx->data->count + n, U8);
1455 PL_regcomp_rx->data->count += n;
1458 Newc(1207, PL_regcomp_rx->data, sizeof(*PL_regcomp_rx->data) + sizeof(void*) * (n - 1),
1459 char, struct reg_data);
1460 New(1208, PL_regcomp_rx->data->what, n, U8);
1461 PL_regcomp_rx->data->count = n;
1463 Copy(s, PL_regcomp_rx->data->what + PL_regcomp_rx->data->count - n, n, U8);
1464 return PL_regcomp_rx->data->count - n;
1468 Perl_reginitcolors(pTHX)
1472 char *s = PerlEnv_getenv("PERL_RE_COLORS");
1475 PL_colors[0] = s = savepv(s);
1477 s = strchr(s, '\t');
1483 PL_colors[i] = s = "";
1487 PL_colors[i++] = "";
1494 - pregcomp - compile a regular expression into internal code
1496 * We can't allocate space until we know how big the compiled form will be,
1497 * but we can't compile it (and thus know how big it is) until we've got a
1498 * place to put the code. So we cheat: we compile it twice, once with code
1499 * generation turned off and size counting turned on, and once "for real".
1500 * This also means that we don't allocate space until we are sure that the
1501 * thing really will compile successfully, and we never have to move the
1502 * code and thus invalidate pointers into it. (Note that it has to be in
1503 * one piece because free() must be able to free it all.) [NB: not true in perl]
1505 * Beware that the optimization-preparation code in here knows about some
1506 * of the structure of the compiled regexp. [I'll say.]
1509 Perl_pregcomp(pTHX_ char *exp, char *xend, PMOP *pm)
1522 FAIL("NULL regexp argument");
1524 if (pm->op_pmdynflags & PMdf_UTF8) {
1525 PL_reg_flags |= RF_utf8;
1530 PL_regprecomp = savepvn(exp, xend - exp);
1531 DEBUG_r(if (!PL_colorset) reginitcolors());
1532 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sCompiling REx%s `%s%*s%s'\n",
1533 PL_colors[4],PL_colors[5],PL_colors[0],
1534 (int)(xend - exp), PL_regprecomp, PL_colors[1]));
1535 PL_regflags = pm->op_pmflags;
1539 PL_seen_zerolen = *exp == '^' ? -1 : 0;
1543 /* First pass: determine size, legality. */
1544 PL_regcomp_parse = exp;
1549 PL_regcode = &PL_regdummy;
1550 PL_reg_whilem_seen = 0;
1551 #if 0 /* REGC() is (currently) a NOP at the first pass.
1552 * Clever compilers notice this and complain. --jhi */
1553 REGC((U8)REG_MAGIC, (char*)PL_regcode);
1555 if (reg(0, &flags) == NULL) {
1556 Safefree(PL_regprecomp);
1557 PL_regprecomp = Nullch;
1560 DEBUG_r(PerlIO_printf(Perl_debug_log, "size %"IVdf" ", (IV)PL_regsize));
1562 /* Small enough for pointer-storage convention?
1563 If extralen==0, this means that we will not need long jumps. */
1564 if (PL_regsize >= 0x10000L && PL_extralen)
1565 PL_regsize += PL_extralen;
1568 if (PL_reg_whilem_seen > 15)
1569 PL_reg_whilem_seen = 15;
1571 /* Allocate space and initialize. */
1572 Newc(1001, r, sizeof(regexp) + (unsigned)PL_regsize * sizeof(regnode),
1575 FAIL("Regexp out of space");
1578 /* avoid reading uninitialized memory in DEBUGGING code in study_chunk() */
1579 Zero(r, sizeof(regexp) + (unsigned)PL_regsize * sizeof(regnode), char);
1582 r->prelen = xend - exp;
1583 r->precomp = PL_regprecomp;
1585 r->reganch = pm->op_pmflags & PMf_COMPILETIME;
1586 r->nparens = PL_regnpar - 1; /* set early to validate backrefs */
1588 r->substrs = 0; /* Useful during FAIL. */
1589 r->startp = 0; /* Useful during FAIL. */
1590 r->endp = 0; /* Useful during FAIL. */
1594 /* Second pass: emit code. */
1595 PL_regcomp_parse = exp;
1599 PL_regcode = r->program;
1600 /* Store the count of eval-groups for security checks: */
1601 PL_regcode->next_off = ((PL_seen_evals > U16_MAX) ? U16_MAX : PL_seen_evals);
1602 REGC((U8)REG_MAGIC, (char*) PL_regcode++);
1604 if (reg(0, &flags) == NULL)
1607 /* Dig out information for optimizations. */
1608 r->reganch = pm->op_pmflags & PMf_COMPILETIME; /* Again? */
1609 pm->op_pmflags = PL_regflags;
1611 r->reganch |= ROPT_UTF8;
1612 r->regstclass = NULL;
1613 if (PL_regnaughty >= 10) /* Probably an expensive pattern. */
1614 r->reganch |= ROPT_NAUGHTY;
1615 scan = r->program + 1; /* First BRANCH. */
1617 /* XXXX To minimize changes to RE engine we always allocate
1618 3-units-long substrs field. */
1619 Newz(1004, r->substrs, 1, struct reg_substr_data);
1621 StructCopy(&zero_scan_data, &data, scan_data_t);
1622 /* XXXX Should not we check for something else? Usually it is OPEN1... */
1623 if (OP(scan) != BRANCH) { /* Only one top-level choice. */
1625 STRLEN longest_float_length, longest_fixed_length;
1626 struct regnode_charclass_class ch_class;
1630 /* Skip introductions and multiplicators >= 1. */
1631 while ((OP(first) == OPEN && (sawopen = 1)) ||
1632 /* An OR of *one* alternative - should not happen now. */
1633 (OP(first) == BRANCH && OP(regnext(first)) != BRANCH) ||
1634 (OP(first) == PLUS) ||
1635 (OP(first) == MINMOD) ||
1636 /* An {n,m} with n>0 */
1637 (PL_regkind[(U8)OP(first)] == CURLY && ARG1(first) > 0) ) {
1638 if (OP(first) == PLUS)
1641 first += regarglen[(U8)OP(first)];
1642 first = NEXTOPER(first);
1645 /* Starting-point info. */
1647 if (PL_regkind[(U8)OP(first)] == EXACT) {
1648 if (OP(first) == EXACT); /* Empty, get anchored substr later. */
1649 else if ((OP(first) == EXACTF || OP(first) == EXACTFL)
1651 r->regstclass = first;
1653 else if (strchr((char*)PL_simple,OP(first)))
1654 r->regstclass = first;
1655 else if (PL_regkind[(U8)OP(first)] == BOUND ||
1656 PL_regkind[(U8)OP(first)] == NBOUND)
1657 r->regstclass = first;
1658 else if (PL_regkind[(U8)OP(first)] == BOL) {
1659 r->reganch |= (OP(first) == MBOL
1661 : (OP(first) == SBOL
1664 first = NEXTOPER(first);
1667 else if (OP(first) == GPOS) {
1668 r->reganch |= ROPT_ANCH_GPOS;
1669 first = NEXTOPER(first);
1672 else if ((OP(first) == STAR &&
1673 PL_regkind[(U8)OP(NEXTOPER(first))] == REG_ANY) &&
1674 !(r->reganch & ROPT_ANCH) )
1676 /* turn .* into ^.* with an implied $*=1 */
1677 int type = OP(NEXTOPER(first));
1679 if (type == REG_ANY || type == ANYUTF8)
1680 type = ROPT_ANCH_MBOL;
1682 type = ROPT_ANCH_SBOL;
1684 r->reganch |= type | ROPT_IMPLICIT;
1685 first = NEXTOPER(first);
1688 if (sawplus && (!sawopen || !PL_regsawback)
1689 && !(PL_regseen & REG_SEEN_EVAL)) /* May examine pos and $& */
1690 /* x+ must match at the 1st pos of run of x's */
1691 r->reganch |= ROPT_SKIP;
1693 /* Scan is after the zeroth branch, first is atomic matcher. */
1694 DEBUG_r(PerlIO_printf(Perl_debug_log, "first at %"IVdf"\n",
1695 (IV)(first - scan + 1)));
1697 * If there's something expensive in the r.e., find the
1698 * longest literal string that must appear and make it the
1699 * regmust. Resolve ties in favor of later strings, since
1700 * the regstart check works with the beginning of the r.e.
1701 * and avoiding duplication strengthens checking. Not a
1702 * strong reason, but sufficient in the absence of others.
1703 * [Now we resolve ties in favor of the earlier string if
1704 * it happens that c_offset_min has been invalidated, since the
1705 * earlier string may buy us something the later one won't.]
1709 data.longest_fixed = newSVpvn("",0);
1710 data.longest_float = newSVpvn("",0);
1711 data.last_found = newSVpvn("",0);
1712 data.longest = &(data.longest_fixed);
1714 if (!r->regstclass) {
1716 data.start_class = &ch_class;
1717 stclass_flag = SCF_DO_STCLASS_AND;
1718 } else /* XXXX Check for BOUND? */
1721 minlen = study_chunk(&first, &fake, scan + PL_regsize, /* Up to end */
1722 &data, SCF_DO_SUBSTR | stclass_flag);
1723 if ( PL_regnpar == 1 && data.longest == &(data.longest_fixed)
1724 && data.last_start_min == 0 && data.last_end > 0
1726 && (!(PL_regseen & REG_SEEN_GPOS) || (r->reganch & ROPT_ANCH_GPOS)))
1727 r->reganch |= ROPT_CHECK_ALL;
1729 SvREFCNT_dec(data.last_found);
1731 longest_float_length = CHR_SVLEN(data.longest_float);
1732 if (longest_float_length
1733 || (data.flags & SF_FL_BEFORE_EOL
1734 && (!(data.flags & SF_FL_BEFORE_MEOL)
1735 || (PL_regflags & PMf_MULTILINE)))) {
1738 if (SvCUR(data.longest_fixed) /* ok to leave SvCUR */
1739 && data.offset_fixed == data.offset_float_min
1740 && SvCUR(data.longest_fixed) == SvCUR(data.longest_float))
1741 goto remove_float; /* As in (a)+. */
1743 r->float_substr = data.longest_float;
1744 r->float_min_offset = data.offset_float_min;
1745 r->float_max_offset = data.offset_float_max;
1746 t = (data.flags & SF_FL_BEFORE_EOL /* Can't have SEOL and MULTI */
1747 && (!(data.flags & SF_FL_BEFORE_MEOL)
1748 || (PL_regflags & PMf_MULTILINE)));
1749 fbm_compile(r->float_substr, t ? FBMcf_TAIL : 0);
1753 r->float_substr = Nullsv;
1754 SvREFCNT_dec(data.longest_float);
1755 longest_float_length = 0;
1758 longest_fixed_length = CHR_SVLEN(data.longest_fixed);
1759 if (longest_fixed_length
1760 || (data.flags & SF_FIX_BEFORE_EOL /* Cannot have SEOL and MULTI */
1761 && (!(data.flags & SF_FIX_BEFORE_MEOL)
1762 || (PL_regflags & PMf_MULTILINE)))) {
1765 r->anchored_substr = data.longest_fixed;
1766 r->anchored_offset = data.offset_fixed;
1767 t = (data.flags & SF_FIX_BEFORE_EOL /* Can't have SEOL and MULTI */
1768 && (!(data.flags & SF_FIX_BEFORE_MEOL)
1769 || (PL_regflags & PMf_MULTILINE)));
1770 fbm_compile(r->anchored_substr, t ? FBMcf_TAIL : 0);
1773 r->anchored_substr = Nullsv;
1774 SvREFCNT_dec(data.longest_fixed);
1775 longest_fixed_length = 0;
1778 && (OP(r->regstclass) == REG_ANY || OP(r->regstclass) == ANYUTF8
1779 || OP(r->regstclass) == SANYUTF8 || OP(r->regstclass) == SANY))
1780 r->regstclass = NULL;
1781 if ((!r->anchored_substr || r->anchored_offset) && stclass_flag
1782 && !(data.start_class->flags & ANYOF_EOS)
1783 && !cl_is_anything(data.start_class)) {
1785 I32 n = add_data(1, "f");
1787 New(1006, PL_regcomp_rx->data->data[n], 1,
1788 struct regnode_charclass_class);
1789 StructCopy(data.start_class,
1790 (struct regnode_charclass_class*)PL_regcomp_rx->data->data[n],
1791 struct regnode_charclass_class);
1792 r->regstclass = (regnode*)PL_regcomp_rx->data->data[n];
1793 r->reganch &= ~ROPT_SKIP; /* Used in find_byclass(). */
1794 DEBUG_r((sv = sv_newmortal(),
1795 regprop(sv, (regnode*)data.start_class),
1796 PerlIO_printf(Perl_debug_log, "synthetic stclass `%s'.\n",
1800 /* A temporary algorithm prefers floated substr to fixed one to dig more info. */
1801 if (longest_fixed_length > longest_float_length) {
1802 r->check_substr = r->anchored_substr;
1803 r->check_offset_min = r->check_offset_max = r->anchored_offset;
1804 if (r->reganch & ROPT_ANCH_SINGLE)
1805 r->reganch |= ROPT_NOSCAN;
1808 r->check_substr = r->float_substr;
1809 r->check_offset_min = data.offset_float_min;
1810 r->check_offset_max = data.offset_float_max;
1812 /* XXXX Currently intuiting is not compatible with ANCH_GPOS.
1813 This should be changed ASAP! */
1814 if (r->check_substr && !(r->reganch & ROPT_ANCH_GPOS)) {
1815 r->reganch |= RE_USE_INTUIT;
1816 if (SvTAIL(r->check_substr))
1817 r->reganch |= RE_INTUIT_TAIL;
1821 /* Several toplevels. Best we can is to set minlen. */
1823 struct regnode_charclass_class ch_class;
1825 DEBUG_r(PerlIO_printf(Perl_debug_log, "\n"));
1826 scan = r->program + 1;
1828 data.start_class = &ch_class;
1829 minlen = study_chunk(&scan, &fake, scan + PL_regsize, &data, SCF_DO_STCLASS_AND);
1830 r->check_substr = r->anchored_substr = r->float_substr = Nullsv;
1831 if (!(data.start_class->flags & ANYOF_EOS)
1832 && !cl_is_anything(data.start_class)) {
1834 I32 n = add_data(1, "f");
1836 New(1006, PL_regcomp_rx->data->data[n], 1,
1837 struct regnode_charclass_class);
1838 StructCopy(data.start_class,
1839 (struct regnode_charclass_class*)PL_regcomp_rx->data->data[n],
1840 struct regnode_charclass_class);
1841 r->regstclass = (regnode*)PL_regcomp_rx->data->data[n];
1842 r->reganch &= ~ROPT_SKIP; /* Used in find_byclass(). */
1843 DEBUG_r((sv = sv_newmortal(),
1844 regprop(sv, (regnode*)data.start_class),
1845 PerlIO_printf(Perl_debug_log, "synthetic stclass `%s'.\n",
1851 if (PL_regseen & REG_SEEN_GPOS)
1852 r->reganch |= ROPT_GPOS_SEEN;
1853 if (PL_regseen & REG_SEEN_LOOKBEHIND)
1854 r->reganch |= ROPT_LOOKBEHIND_SEEN;
1855 if (PL_regseen & REG_SEEN_EVAL)
1856 r->reganch |= ROPT_EVAL_SEEN;
1857 Newz(1002, r->startp, PL_regnpar, I32);
1858 Newz(1002, r->endp, PL_regnpar, I32);
1859 DEBUG_r(regdump(r));
1864 - reg - regular expression, i.e. main body or parenthesized thing
1866 * Caller must absorb opening parenthesis.
1868 * Combining parenthesis handling with the base level of regular expression
1869 * is a trifle forced, but the need to tie the tails of the branches to what
1870 * follows makes it hard to avoid.
1873 S_reg(pTHX_ I32 paren, I32 *flagp)
1874 /* paren: Parenthesized? 0=top, 1=(, inside: changed to letter. */
1877 register regnode *ret; /* Will be the head of the group. */
1878 register regnode *br;
1879 register regnode *lastbr;
1880 register regnode *ender = 0;
1881 register I32 parno = 0;
1882 I32 flags, oregflags = PL_regflags, have_branch = 0, open = 0;
1883 char *oregcomp_parse = PL_regcomp_parse;
1886 *flagp = 0; /* Tentatively. */
1888 /* Make an OPEN node, if parenthesized. */
1890 if (*PL_regcomp_parse == '?') {
1891 U16 posflags = 0, negflags = 0;
1892 U16 *flagsp = &posflags;
1894 char *seqstart = PL_regcomp_parse;
1897 paren = *PL_regcomp_parse++;
1898 ret = NULL; /* For look-ahead/behind. */
1901 PL_regseen |= REG_SEEN_LOOKBEHIND;
1902 if (*PL_regcomp_parse == '!')
1904 if (*PL_regcomp_parse != '=' && *PL_regcomp_parse != '!')
1915 vFAIL2("Sequence (?%c...) not implemented", (int)paren);
1918 while (*PL_regcomp_parse && *PL_regcomp_parse != ')')
1920 if (*PL_regcomp_parse != ')')
1921 FAIL("Sequence (?#... not terminated");
1927 vWARN(PL_regcomp_parse, "(?p{}) is deprecated - use (??{})");
1931 paren = *PL_regcomp_parse++;
1936 I32 count = 1, n = 0;
1938 char *s = PL_regcomp_parse;
1940 OP_4tree *sop, *rop;
1943 PL_regseen |= REG_SEEN_EVAL;
1944 while (count && (c = *PL_regcomp_parse)) {
1945 if (c == '\\' && PL_regcomp_parse[1])
1953 if (*PL_regcomp_parse != ')')
1955 PL_regcomp_parse = s;
1956 vFAIL("Sequence (?{...}) not terminated or not {}-balanced");
1961 if (PL_regcomp_parse - 1 - s)
1962 sv = newSVpvn(s, PL_regcomp_parse - 1 - s);
1964 sv = newSVpvn("", 0);
1966 rop = sv_compile_2op(sv, &sop, "re", &av);
1968 n = add_data(3, "nop");
1969 PL_regcomp_rx->data->data[n] = (void*)rop;
1970 PL_regcomp_rx->data->data[n+1] = (void*)sop;
1971 PL_regcomp_rx->data->data[n+2] = (void*)av;
1974 else { /* First pass */
1975 if (PL_reginterp_cnt < ++PL_seen_evals
1976 && PL_curcop != &PL_compiling)
1977 /* No compiled RE interpolated, has runtime
1978 components ===> unsafe. */
1979 FAIL("Eval-group not allowed at runtime, use re 'eval'");
1981 FAIL("Eval-group in insecure regular expression");
1986 ret = reg_node(LOGICAL);
1989 regtail(ret, reganode(EVAL, n));
1992 return reganode(EVAL, n);
1996 if (PL_regcomp_parse[0] == '?') {
1997 if (PL_regcomp_parse[1] == '=' || PL_regcomp_parse[1] == '!'
1998 || PL_regcomp_parse[1] == '<'
1999 || PL_regcomp_parse[1] == '{') { /* Lookahead or eval. */
2002 ret = reg_node(LOGICAL);
2005 regtail(ret, reg(1, &flag));
2009 else if (PL_regcomp_parse[0] >= '1' && PL_regcomp_parse[0] <= '9' ) {
2010 parno = atoi(PL_regcomp_parse++);
2012 while (isDIGIT(*PL_regcomp_parse))
2014 ret = reganode(GROUPP, parno);
2015 if ((c = *nextchar()) != ')')
2016 vFAIL("Switch condition not recognized");
2018 regtail(ret, reganode(IFTHEN, 0));
2019 br = regbranch(&flags, 1);
2021 br = reganode(LONGJMP, 0);
2023 regtail(br, reganode(LONGJMP, 0));
2028 lastbr = reganode(IFTHEN, 0); /* Fake one for optimizer. */
2029 regbranch(&flags, 1);
2030 regtail(ret, lastbr);
2038 vFAIL("Switch (?(condition)... contains too many branches");
2039 ender = reg_node(TAIL);
2042 regtail(lastbr, ender);
2043 regtail(NEXTOPER(NEXTOPER(lastbr)), ender);
2046 regtail(ret, ender);
2050 vFAIL2("Unknown switch condition (?(%.2s", PL_regcomp_parse);
2054 PL_regcomp_parse--; /* for vFAIL to print correctly */
2055 vFAIL("Sequence (? incomplete");
2060 while (*PL_regcomp_parse && strchr("iogcmsx", *PL_regcomp_parse)) {
2061 if (*PL_regcomp_parse != 'o')
2062 pmflag(flagsp, *PL_regcomp_parse);
2065 if (*PL_regcomp_parse == '-') {
2070 PL_regflags |= posflags;
2071 PL_regflags &= ~negflags;
2072 if (*PL_regcomp_parse == ':') {
2078 if (*PL_regcomp_parse != ')') {
2080 vFAIL3("Sequence (%.*s...) not recognized", PL_regcomp_parse-seqstart, seqstart);
2090 ret = reganode(OPEN, parno);
2097 /* Pick up the branches, linking them together. */
2098 br = regbranch(&flags, 1);
2101 if (*PL_regcomp_parse == '|') {
2102 if (!SIZE_ONLY && PL_extralen) {
2103 reginsert(BRANCHJ, br);
2106 reginsert(BRANCH, br);
2109 PL_extralen += 1; /* For BRANCHJ-BRANCH. */
2111 else if (paren == ':') {
2112 *flagp |= flags&SIMPLE;
2114 if (open) { /* Starts with OPEN. */
2115 regtail(ret, br); /* OPEN -> first. */
2117 else if (paren != '?') /* Not Conditional */
2121 *flagp |= flags&SPSTART;
2123 while (*PL_regcomp_parse == '|') {
2124 if (!SIZE_ONLY && PL_extralen) {
2125 ender = reganode(LONGJMP,0);
2126 regtail(NEXTOPER(NEXTOPER(lastbr)), ender); /* Append to the previous. */
2129 PL_extralen += 2; /* Account for LONGJMP. */
2131 br = regbranch(&flags, 0);
2134 regtail(lastbr, br); /* BRANCH -> BRANCH. */
2138 *flagp |= flags&SPSTART;
2141 if (have_branch || paren != ':') {
2142 /* Make a closing node, and hook it on the end. */
2145 ender = reg_node(TAIL);
2148 ender = reganode(CLOSE, parno);
2154 *flagp &= ~HASWIDTH;
2157 ender = reg_node(SUCCEED);
2160 ender = reg_node(END);
2163 regtail(lastbr, ender);
2166 /* Hook the tails of the branches to the closing node. */
2167 for (br = ret; br != NULL; br = regnext(br)) {
2168 regoptail(br, ender);
2175 static char parens[] = "=!<,>";
2177 if (paren && (p = strchr(parens, paren))) {
2178 int node = ((p - parens) % 2) ? UNLESSM : IFMATCH;
2179 int flag = (p - parens) > 1;
2182 node = SUSPEND, flag = 0;
2183 reginsert(node,ret);
2185 regtail(ret, reg_node(TAIL));
2189 /* Check for proper termination. */
2191 PL_regflags = oregflags;
2192 if (PL_regcomp_parse >= PL_regxend || *nextchar() != ')') {
2193 PL_regcomp_parse = oregcomp_parse;
2194 vFAIL("Unmatched (");
2197 else if (!paren && PL_regcomp_parse < PL_regxend) {
2198 if (*PL_regcomp_parse == ')') {
2200 vFAIL("Unmatched )");
2203 FAIL("Junk on end of regexp"); /* "Can't happen". */
2211 - regbranch - one alternative of an | operator
2213 * Implements the concatenation operator.
2216 S_regbranch(pTHX_ I32 *flagp, I32 first)
2219 register regnode *ret;
2220 register regnode *chain = NULL;
2221 register regnode *latest;
2222 I32 flags = 0, c = 0;
2227 if (!SIZE_ONLY && PL_extralen)
2228 ret = reganode(BRANCHJ,0);
2230 ret = reg_node(BRANCH);
2233 if (!first && SIZE_ONLY)
2234 PL_extralen += 1; /* BRANCHJ */
2236 *flagp = WORST; /* Tentatively. */
2240 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != '|' && *PL_regcomp_parse != ')') {
2242 latest = regpiece(&flags);
2243 if (latest == NULL) {
2244 if (flags & TRYAGAIN)
2248 else if (ret == NULL)
2250 *flagp |= flags&HASWIDTH;
2251 if (chain == NULL) /* First piece. */
2252 *flagp |= flags&SPSTART;
2255 regtail(chain, latest);
2260 if (chain == NULL) { /* Loop ran zero times. */
2261 chain = reg_node(NOTHING);
2266 *flagp |= flags&SIMPLE;
2273 - regpiece - something followed by possible [*+?]
2275 * Note that the branching code sequences used for ? and the general cases
2276 * of * and + are somewhat optimized: they use the same NOTHING node as
2277 * both the endmarker for their branch list and the body of the last branch.
2278 * It might seem that this node could be dispensed with entirely, but the
2279 * endmarker role is not redundant.
2282 S_regpiece(pTHX_ I32 *flagp)
2285 register regnode *ret;
2287 register char *next;
2289 char *origparse = PL_regcomp_parse;
2292 I32 max = REG_INFTY;
2294 ret = regatom(&flags);
2296 if (flags & TRYAGAIN)
2301 op = *PL_regcomp_parse;
2303 if (op == '{' && regcurly(PL_regcomp_parse)) {
2304 next = PL_regcomp_parse + 1;
2306 while (isDIGIT(*next) || *next == ',') {
2315 if (*next == '}') { /* got one */
2319 min = atoi(PL_regcomp_parse);
2323 maxpos = PL_regcomp_parse;
2325 if (!max && *maxpos != '0')
2326 max = REG_INFTY; /* meaning "infinity" */
2327 else if (max >= REG_INFTY)
2328 vFAIL2("Quantifier in {,} bigger than %d", REG_INFTY - 1);
2329 PL_regcomp_parse = next;
2333 if ((flags&SIMPLE)) {
2334 PL_regnaughty += 2 + PL_regnaughty / 2;
2335 reginsert(CURLY, ret);
2338 regnode *w = reg_node(WHILEM);
2342 if (!SIZE_ONLY && PL_extralen) {
2343 reginsert(LONGJMP,ret);
2344 reginsert(NOTHING,ret);
2345 NEXT_OFF(ret) = 3; /* Go over LONGJMP. */
2347 reginsert(CURLYX,ret);
2348 if (!SIZE_ONLY && PL_extralen)
2349 NEXT_OFF(ret) = 3; /* Go over NOTHING to LONGJMP. */
2350 regtail(ret, reg_node(NOTHING));
2352 PL_reg_whilem_seen++, PL_extralen += 3;
2353 PL_regnaughty += 4 + PL_regnaughty; /* compound interest */
2361 if (max && max < min)
2362 vFAIL("Can't do {n,m} with n > m");
2377 #if 0 /* Now runtime fix should be reliable. */
2379 /* if this is reinstated, don't forget to put this back into perldiag:
2381 =item Regexp *+ operand could be empty at {#} in regex m/%s/
2383 (F) The part of the regexp subject to either the * or + quantifier
2384 could match an empty string. The {#} shows in the regular
2385 expression about where the problem was discovered.
2389 if (!(flags&HASWIDTH) && op != '?')
2390 vFAIL("Regexp *+ operand could be empty");
2395 *flagp = (op != '+') ? (WORST|SPSTART|HASWIDTH) : (WORST|HASWIDTH);
2397 if (op == '*' && (flags&SIMPLE)) {
2398 reginsert(STAR, ret);
2402 else if (op == '*') {
2406 else if (op == '+' && (flags&SIMPLE)) {
2407 reginsert(PLUS, ret);
2411 else if (op == '+') {
2415 else if (op == '?') {
2420 if (ckWARN(WARN_REGEXP) && !SIZE_ONLY && !(flags&HASWIDTH) && max > REG_INFTY/3) {
2421 vWARN3(PL_regcomp_parse,
2422 "%.*s matches null string many times",
2423 PL_regcomp_parse - origparse,
2427 if (*PL_regcomp_parse == '?') {
2429 reginsert(MINMOD, ret);
2430 regtail(ret, ret + NODE_STEP_REGNODE);
2432 if (ISMULT2(PL_regcomp_parse)) {
2434 vFAIL("Nested quantifiers");
2441 - regatom - the lowest level
2443 * Optimization: gobbles an entire sequence of ordinary characters so that
2444 * it can turn them into a single node, which is smaller to store and
2445 * faster to run. Backslashed characters are exceptions, each becoming a
2446 * separate node; the code is simpler that way and it's not worth fixing.
2448 * [Yes, it is worth fixing, some scripts can run twice the speed.] */
2450 S_regatom(pTHX_ I32 *flagp)
2453 register regnode *ret = 0;
2456 *flagp = WORST; /* Tentatively. */
2459 switch (*PL_regcomp_parse) {
2463 if (PL_regflags & PMf_MULTILINE)
2464 ret = reg_node(MBOL);
2465 else if (PL_regflags & PMf_SINGLELINE)
2466 ret = reg_node(SBOL);
2468 ret = reg_node(BOL);
2471 if (PL_regcomp_parse[1])
2474 if (PL_regflags & PMf_MULTILINE)
2475 ret = reg_node(MEOL);
2476 else if (PL_regflags & PMf_SINGLELINE)
2477 ret = reg_node(SEOL);
2479 ret = reg_node(EOL);
2484 if (PL_regflags & PMf_SINGLELINE)
2485 ret = reg_node(SANYUTF8);
2487 ret = reg_node(ANYUTF8);
2491 if (PL_regflags & PMf_SINGLELINE)
2492 ret = reg_node(SANY);
2494 ret = reg_node(REG_ANY);
2495 *flagp |= HASWIDTH|SIMPLE;
2501 char *oregcomp_parse = ++PL_regcomp_parse;
2502 ret = (UTF ? regclassutf8() : regclass());
2503 if (*PL_regcomp_parse != ']') {
2504 PL_regcomp_parse = oregcomp_parse;
2505 vFAIL("Unmatched [");
2508 *flagp |= HASWIDTH|SIMPLE;
2513 ret = reg(1, &flags);
2515 if (flags & TRYAGAIN) {
2516 if (PL_regcomp_parse == PL_regxend) {
2517 /* Make parent create an empty node if needed. */
2525 *flagp |= flags&(HASWIDTH|SPSTART|SIMPLE);
2529 if (flags & TRYAGAIN) {
2533 vFAIL("Internal urp");
2534 /* Supposed to be caught earlier. */
2537 if (!regcurly(PL_regcomp_parse)) {
2546 vFAIL("Quantifier follows nothing");
2549 switch (*++PL_regcomp_parse) {
2552 ret = reg_node(SBOL);
2557 ret = reg_node(GPOS);
2558 PL_regseen |= REG_SEEN_GPOS;
2563 ret = reg_node(SEOL);
2568 ret = reg_node(EOS);
2570 PL_seen_zerolen++; /* Do not optimize RE away */
2574 ret = reg_node(SANY);
2575 *flagp |= HASWIDTH|SIMPLE;
2579 ret = reg_node(CLUMP);
2582 if (UTF && !PL_utf8_mark)
2583 is_utf8_mark((U8*)"~"); /* preload table */
2588 ? (LOC ? ALNUMLUTF8 : ALNUMUTF8)
2589 : (LOC ? ALNUML : ALNUM));
2590 *flagp |= HASWIDTH|SIMPLE;
2592 if (UTF && !PL_utf8_alnum)
2593 is_utf8_alnum((U8*)"a"); /* preload table */
2598 ? (LOC ? NALNUMLUTF8 : NALNUMUTF8)
2599 : (LOC ? NALNUML : NALNUM));
2600 *flagp |= HASWIDTH|SIMPLE;
2602 if (UTF && !PL_utf8_alnum)
2603 is_utf8_alnum((U8*)"a"); /* preload table */
2607 PL_regseen |= REG_SEEN_LOOKBEHIND;
2610 ? (LOC ? BOUNDLUTF8 : BOUNDUTF8)
2611 : (LOC ? BOUNDL : BOUND));
2614 if (UTF && !PL_utf8_alnum)
2615 is_utf8_alnum((U8*)"a"); /* preload table */
2619 PL_regseen |= REG_SEEN_LOOKBEHIND;
2622 ? (LOC ? NBOUNDLUTF8 : NBOUNDUTF8)
2623 : (LOC ? NBOUNDL : NBOUND));
2626 if (UTF && !PL_utf8_alnum)
2627 is_utf8_alnum((U8*)"a"); /* preload table */
2632 ? (LOC ? SPACELUTF8 : SPACEUTF8)
2633 : (LOC ? SPACEL : SPACE));
2634 *flagp |= HASWIDTH|SIMPLE;
2636 if (UTF && !PL_utf8_space)
2637 is_utf8_space((U8*)" "); /* preload table */
2642 ? (LOC ? NSPACELUTF8 : NSPACEUTF8)
2643 : (LOC ? NSPACEL : NSPACE));
2644 *flagp |= HASWIDTH|SIMPLE;
2646 if (UTF && !PL_utf8_space)
2647 is_utf8_space((U8*)" "); /* preload table */
2650 ret = reg_node(UTF ? DIGITUTF8 : DIGIT);
2651 *flagp |= HASWIDTH|SIMPLE;
2653 if (UTF && !PL_utf8_digit)
2654 is_utf8_digit((U8*)"1"); /* preload table */
2657 ret = reg_node(UTF ? NDIGITUTF8 : NDIGIT);
2658 *flagp |= HASWIDTH|SIMPLE;
2660 if (UTF && !PL_utf8_digit)
2661 is_utf8_digit((U8*)"1"); /* preload table */
2665 { /* a lovely hack--pretend we saw [\pX] instead */
2666 char* oldregxend = PL_regxend;
2668 if (PL_regcomp_parse[1] == '{') {
2669 PL_regxend = strchr(PL_regcomp_parse, '}');
2671 PL_regcomp_parse += 2;
2672 PL_regxend = oldregxend;
2673 vFAIL("Missing right brace on \\p{}");
2678 PL_regxend = PL_regcomp_parse + 2;
2681 ret = regclassutf8();
2683 PL_regxend = oldregxend;
2686 *flagp |= HASWIDTH|SIMPLE;
2699 case '1': case '2': case '3': case '4':
2700 case '5': case '6': case '7': case '8': case '9':
2702 I32 num = atoi(PL_regcomp_parse);
2704 if (num > 9 && num >= PL_regnpar)
2707 while (isDIGIT(*PL_regcomp_parse))
2710 if (!SIZE_ONLY && num > PL_regcomp_rx->nparens)
2711 vFAIL("Reference to nonexistent group");
2714 ? (LOC ? REFFL : REFF)
2723 if (PL_regcomp_parse >= PL_regxend)
2724 FAIL("Trailing \\");
2727 /* Do not generate `unrecognized' warnings here, we fall
2728 back into the quick-grab loop below */
2734 if (PL_regflags & PMf_EXTENDED) {
2735 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != '\n') PL_regcomp_parse++;
2736 if (PL_regcomp_parse < PL_regxend)
2752 ? (LOC ? EXACTFL : EXACTF)
2755 for (len = 0, p = PL_regcomp_parse - 1;
2756 len < 127 && p < PL_regxend;
2761 if (PL_regflags & PMf_EXTENDED)
2762 p = regwhite(p, PL_regxend);
2824 char* e = strchr(p, '}');
2827 PL_regcomp_parse = p + 1;
2828 vFAIL("Missing right brace on \\x{}");
2831 numlen = 1; /* allow underscores */
2832 ender = (UV)scan_hex(p + 1, e - p - 1, &numlen);
2833 /* numlen is generous */
2834 if (numlen + len >= 127) {
2842 PL_regcomp_parse = e + 1;
2843 vFAIL("Can't use \\x{} without 'use utf8' declaration");
2848 numlen = 0; /* disallow underscores */
2849 ender = (UV)scan_hex(p, 2, &numlen);
2855 ender = UCHARAT(p++);
2856 ender = toCTRL(ender);
2858 case '0': case '1': case '2': case '3':case '4':
2859 case '5': case '6': case '7': case '8':case '9':
2861 (isDIGIT(p[1]) && atoi(p) >= PL_regnpar) ) {
2862 numlen = 0; /* disallow underscores */
2863 ender = (UV)scan_oct(p, 3, &numlen);
2872 if (p >= PL_regxend)
2873 FAIL("Trailing \\");
2876 if (!SIZE_ONLY && ckWARN(WARN_REGEXP) && isALPHA(*p))
2877 vWARN2(p +1, "Unrecognized escape \\%c passed through", *p);
2878 goto normal_default;
2883 if ((*p & 0xc0) == 0xc0 && UTF) {
2884 ender = utf8_to_uv((U8*)p, &numlen);
2891 if (PL_regflags & PMf_EXTENDED)
2892 p = regwhite(p, PL_regxend);
2895 ender = toLOWER_LC_uni(ender);
2897 ender = toLOWER_uni(ender);
2899 if (ISMULT2(p)) { /* Back off on ?+*. */
2902 else if (ender >= 0x80 && UTF) {
2903 reguni(ender, s, &numlen);
2913 if (ender >= 0x80 && UTF) {
2914 reguni(ender, s, &numlen);
2922 PL_regcomp_parse = p - 1;
2925 vFAIL("Internal disaster");
2933 PL_regsize += STR_SZ(len);
2935 PL_regcode += STR_SZ(len);
2944 S_regwhite(pTHX_ char *p, char *e)
2949 else if (*p == '#') {
2952 } while (p < e && *p != '\n');
2960 /* Parse POSIX character classes: [[:foo:]], [[=foo=]], [[.foo.]].
2961 Character classes ([:foo:]) can also be negated ([:^foo:]).
2962 Returns a named class id (ANYOF_XXX) if successful, -1 otherwise.
2963 Equivalence classes ([=foo=]) and composites ([.foo.]) are parsed,
2964 but trigger warnings because they are currently unimplemented. */
2966 S_regpposixcc(pTHX_ I32 value)
2970 I32 namedclass = OOB_NAMEDCLASS;
2972 if (value == '[' && PL_regcomp_parse + 1 < PL_regxend &&
2973 /* I smell either [: or [= or [. -- POSIX has been here, right? */
2974 (*PL_regcomp_parse == ':' ||
2975 *PL_regcomp_parse == '=' ||
2976 *PL_regcomp_parse == '.')) {
2977 char c = *PL_regcomp_parse;
2978 char* s = PL_regcomp_parse++;
2980 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != c)
2982 if (PL_regcomp_parse == PL_regxend)
2983 /* Grandfather lone [:, [=, [. */
2984 PL_regcomp_parse = s;
2986 char* t = PL_regcomp_parse++; /* skip over the c */
2988 if (*PL_regcomp_parse == ']') {
2989 PL_regcomp_parse++; /* skip over the ending ] */
2992 I32 complement = *posixcc == '^' ? *posixcc++ : 0;
2993 I32 skip = 5; /* the most common skip */
2997 if (strnEQ(posixcc, "alnum", 5))
2999 complement ? ANYOF_NALNUMC : ANYOF_ALNUMC;
3000 else if (strnEQ(posixcc, "alpha", 5))
3002 complement ? ANYOF_NALPHA : ANYOF_ALPHA;
3003 else if (strnEQ(posixcc, "ascii", 5))
3005 complement ? ANYOF_NASCII : ANYOF_ASCII;
3008 if (strnEQ(posixcc, "cntrl", 5))
3010 complement ? ANYOF_NCNTRL : ANYOF_CNTRL;
3013 if (strnEQ(posixcc, "digit", 5))
3015 complement ? ANYOF_NDIGIT : ANYOF_DIGIT;
3018 if (strnEQ(posixcc, "graph", 5))
3020 complement ? ANYOF_NGRAPH : ANYOF_GRAPH;
3023 if (strnEQ(posixcc, "lower", 5))
3025 complement ? ANYOF_NLOWER : ANYOF_LOWER;
3028 if (strnEQ(posixcc, "print", 5))
3030 complement ? ANYOF_NPRINT : ANYOF_PRINT;
3031 else if (strnEQ(posixcc, "punct", 5))
3033 complement ? ANYOF_NPUNCT : ANYOF_PUNCT;
3036 if (strnEQ(posixcc, "space", 5))
3038 complement ? ANYOF_NSPACE : ANYOF_SPACE;
3041 if (strnEQ(posixcc, "upper", 5))
3043 complement ? ANYOF_NUPPER : ANYOF_UPPER;
3045 case 'w': /* this is not POSIX, this is the Perl \w */
3046 if (strnEQ(posixcc, "word", 4)) {
3048 complement ? ANYOF_NALNUM : ANYOF_ALNUM;
3053 if (strnEQ(posixcc, "xdigit", 6)) {
3055 complement ? ANYOF_NXDIGIT : ANYOF_XDIGIT;
3060 if (namedclass == OOB_NAMEDCLASS ||
3061 posixcc[skip] != ':' ||
3062 posixcc[skip+1] != ']')
3064 Simple_vFAIL3("POSIX class [:%.*s:] unknown",
3067 } else if (!SIZE_ONLY) {
3068 /* [[=foo=]] and [[.foo.]] are still future. */
3070 /* adjust PL_regcomp_parse so the warning shows after
3072 while (*PL_regcomp_parse && *PL_regcomp_parse != ']')
3074 Simple_vFAIL3("POSIX syntax [%c %c] is reserved for future extensions", c, c);
3077 /* Maternal grandfather:
3078 * "[:" ending in ":" but not in ":]" */
3079 PL_regcomp_parse = s;
3088 S_checkposixcc(pTHX)
3090 if (!SIZE_ONLY && ckWARN(WARN_REGEXP) &&
3091 (*PL_regcomp_parse == ':' ||
3092 *PL_regcomp_parse == '=' ||
3093 *PL_regcomp_parse == '.')) {
3094 char *s = PL_regcomp_parse;
3097 while(*s && isALNUM(*s))
3099 if (*s && c == *s && s[1] == ']') {
3100 vWARN3(s+2, "POSIX syntax [%c %c] belongs inside character classes", c, c);
3102 /* [[=foo=]] and [[.foo.]] are still future. */
3103 if (c == '=' || c == '.')
3105 /* adjust PL_regcomp_parse so the error shows after
3107 while (*PL_regcomp_parse && *PL_regcomp_parse++ != ']')
3109 Simple_vFAIL3("POSIX syntax [%c %c] is reserved for future extensions", c, c);
3120 register I32 lastvalue = OOB_CHAR8;
3121 register I32 range = 0;
3122 register regnode *ret;
3126 bool need_class = 0;
3128 ret = reg_node(ANYOF);
3130 PL_regsize += ANYOF_SKIP;
3133 ANYOF_BITMAP_ZERO(ret);
3134 PL_regcode += ANYOF_SKIP;
3136 ANYOF_FLAGS(ret) |= ANYOF_FOLD;
3138 ANYOF_FLAGS(ret) |= ANYOF_LOCALE;
3140 if (*PL_regcomp_parse == '^') { /* Complement of range. */
3144 ANYOF_FLAGS(ret) |= ANYOF_INVERT;
3147 if (!SIZE_ONLY && ckWARN(WARN_REGEXP))
3150 if (*PL_regcomp_parse == ']' || *PL_regcomp_parse == '-')
3151 goto skipcond; /* allow 1st char to be ] or - */
3152 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != ']') {
3154 namedclass = OOB_NAMEDCLASS;
3156 rangebegin = PL_regcomp_parse;
3157 value = UCHARAT(PL_regcomp_parse++);
3159 namedclass = regpposixcc(value);
3160 else if (value == '\\') {
3161 value = UCHARAT(PL_regcomp_parse++);
3162 /* Some compilers cannot handle switching on 64-bit integer
3163 * values, therefore value cannot be an UV. --jhi */
3165 case 'w': namedclass = ANYOF_ALNUM; break;
3166 case 'W': namedclass = ANYOF_NALNUM; break;
3167 case 's': namedclass = ANYOF_SPACE; break;
3168 case 'S': namedclass = ANYOF_NSPACE; break;
3169 case 'd': namedclass = ANYOF_DIGIT; break;
3170 case 'D': namedclass = ANYOF_NDIGIT; break;
3171 case 'n': value = '\n'; break;
3172 case 'r': value = '\r'; break;
3173 case 't': value = '\t'; break;
3174 case 'f': value = '\f'; break;
3175 case 'b': value = '\b'; break;
3177 case 'e': value = '\033'; break;
3178 case 'a': value = '\007'; break;
3180 case 'e': value = '\047'; break;
3181 case 'a': value = '\057'; break;
3184 numlen = 0; /* disallow underscores */
3185 value = (UV)scan_hex(PL_regcomp_parse, 2, &numlen);
3186 PL_regcomp_parse += numlen;
3189 value = UCHARAT(PL_regcomp_parse++);
3190 value = toCTRL(value);
3192 case '0': case '1': case '2': case '3': case '4':
3193 case '5': case '6': case '7': case '8': case '9':
3194 numlen = 0; /* disallow underscores */
3195 value = (UV)scan_oct(--PL_regcomp_parse, 3, &numlen);
3196 PL_regcomp_parse += numlen;
3199 if (!SIZE_ONLY && ckWARN(WARN_REGEXP) && isALPHA(value))
3201 vWARN2(PL_regcomp_parse, "Unrecognized escape \\%c in character class passed through", (int)value);
3205 if (namedclass > OOB_NAMEDCLASS) {
3206 if (!need_class && !SIZE_ONLY)
3207 ANYOF_CLASS_ZERO(ret);
3209 if (range) { /* a-\d, a-[:digit:] */
3211 if (ckWARN(WARN_REGEXP))
3212 vWARN4(PL_regcomp_parse,
3213 "False [] range \"%*.*s\"",
3214 PL_regcomp_parse - rangebegin,
3215 PL_regcomp_parse - rangebegin,
3217 ANYOF_BITMAP_SET(ret, lastvalue);
3218 ANYOF_BITMAP_SET(ret, '-');
3220 range = 0; /* this is not a true range */
3223 switch (namedclass) {
3226 ANYOF_CLASS_SET(ret, ANYOF_ALNUM);
3228 for (value = 0; value < 256; value++)
3230 ANYOF_BITMAP_SET(ret, value);
3235 ANYOF_CLASS_SET(ret, ANYOF_NALNUM);
3237 for (value = 0; value < 256; value++)
3238 if (!isALNUM(value))
3239 ANYOF_BITMAP_SET(ret, value);
3244 ANYOF_CLASS_SET(ret, ANYOF_SPACE);
3246 for (value = 0; value < 256; value++)
3248 ANYOF_BITMAP_SET(ret, value);
3253 ANYOF_CLASS_SET(ret, ANYOF_NSPACE);
3255 for (value = 0; value < 256; value++)
3256 if (!isSPACE(value))
3257 ANYOF_BITMAP_SET(ret, value);
3262 ANYOF_CLASS_SET(ret, ANYOF_DIGIT);
3264 for (value = '0'; value <= '9'; value++)
3265 ANYOF_BITMAP_SET(ret, value);
3270 ANYOF_CLASS_SET(ret, ANYOF_NDIGIT);
3272 for (value = 0; value < '0'; value++)
3273 ANYOF_BITMAP_SET(ret, value);
3274 for (value = '9' + 1; value < 256; value++)
3275 ANYOF_BITMAP_SET(ret, value);
3280 ANYOF_CLASS_SET(ret, ANYOF_NALNUMC);
3282 for (value = 0; value < 256; value++)
3283 if (!isALNUMC(value))
3284 ANYOF_BITMAP_SET(ret, value);
3289 ANYOF_CLASS_SET(ret, ANYOF_ALNUMC);
3291 for (value = 0; value < 256; value++)
3292 if (isALNUMC(value))
3293 ANYOF_BITMAP_SET(ret, value);
3298 ANYOF_CLASS_SET(ret, ANYOF_ALPHA);
3300 for (value = 0; value < 256; value++)
3302 ANYOF_BITMAP_SET(ret, value);
3307 ANYOF_CLASS_SET(ret, ANYOF_NALPHA);
3309 for (value = 0; value < 256; value++)
3310 if (!isALPHA(value))
3311 ANYOF_BITMAP_SET(ret, value);
3316 ANYOF_CLASS_SET(ret, ANYOF_ASCII);
3319 for (value = 0; value < 128; value++)
3320 ANYOF_BITMAP_SET(ret, value);
3322 for (value = 0; value < 256; value++)
3324 ANYOF_BITMAP_SET(ret, value);
3330 ANYOF_CLASS_SET(ret, ANYOF_NASCII);
3333 for (value = 128; value < 256; value++)
3334 ANYOF_BITMAP_SET(ret, value);
3336 for (value = 0; value < 256; value++)
3337 if (!isASCII(value))
3338 ANYOF_BITMAP_SET(ret, value);
3344 ANYOF_CLASS_SET(ret, ANYOF_CNTRL);
3346 for (value = 0; value < 256; value++)
3348 ANYOF_BITMAP_SET(ret, value);
3350 lastvalue = OOB_CHAR8;
3354 ANYOF_CLASS_SET(ret, ANYOF_NCNTRL);
3356 for (value = 0; value < 256; value++)
3357 if (!isCNTRL(value))
3358 ANYOF_BITMAP_SET(ret, value);
3363 ANYOF_CLASS_SET(ret, ANYOF_GRAPH);
3365 for (value = 0; value < 256; value++)
3367 ANYOF_BITMAP_SET(ret, value);
3372 ANYOF_CLASS_SET(ret, ANYOF_NGRAPH);
3374 for (value = 0; value < 256; value++)
3375 if (!isGRAPH(value))
3376 ANYOF_BITMAP_SET(ret, value);
3381 ANYOF_CLASS_SET(ret, ANYOF_LOWER);
3383 for (value = 0; value < 256; value++)
3385 ANYOF_BITMAP_SET(ret, value);
3390 ANYOF_CLASS_SET(ret, ANYOF_NLOWER);
3392 for (value = 0; value < 256; value++)
3393 if (!isLOWER(value))
3394 ANYOF_BITMAP_SET(ret, value);
3399 ANYOF_CLASS_SET(ret, ANYOF_PRINT);
3401 for (value = 0; value < 256; value++)
3403 ANYOF_BITMAP_SET(ret, value);
3408 ANYOF_CLASS_SET(ret, ANYOF_NPRINT);
3410 for (value = 0; value < 256; value++)
3411 if (!isPRINT(value))
3412 ANYOF_BITMAP_SET(ret, value);
3417 ANYOF_CLASS_SET(ret, ANYOF_PUNCT);
3419 for (value = 0; value < 256; value++)
3421 ANYOF_BITMAP_SET(ret, value);
3426 ANYOF_CLASS_SET(ret, ANYOF_NPUNCT);
3428 for (value = 0; value < 256; value++)
3429 if (!isPUNCT(value))
3430 ANYOF_BITMAP_SET(ret, value);
3435 ANYOF_CLASS_SET(ret, ANYOF_UPPER);
3437 for (value = 0; value < 256; value++)
3439 ANYOF_BITMAP_SET(ret, value);
3444 ANYOF_CLASS_SET(ret, ANYOF_NUPPER);
3446 for (value = 0; value < 256; value++)
3447 if (!isUPPER(value))
3448 ANYOF_BITMAP_SET(ret, value);
3453 ANYOF_CLASS_SET(ret, ANYOF_XDIGIT);
3455 for (value = 0; value < 256; value++)
3456 if (isXDIGIT(value))
3457 ANYOF_BITMAP_SET(ret, value);
3462 ANYOF_CLASS_SET(ret, ANYOF_NXDIGIT);
3464 for (value = 0; value < 256; value++)
3465 if (!isXDIGIT(value))
3466 ANYOF_BITMAP_SET(ret, value);
3470 vFAIL("Invalid [::] class");
3474 ANYOF_FLAGS(ret) |= ANYOF_CLASS;
3479 if (lastvalue > value) /* b-a */ {
3480 Simple_vFAIL4("Invalid [] range \"%*.*s\"",
3481 PL_regcomp_parse - rangebegin,
3482 PL_regcomp_parse - rangebegin,
3489 if (*PL_regcomp_parse == '-' && PL_regcomp_parse+1 < PL_regxend &&
3490 PL_regcomp_parse[1] != ']') {
3492 if (namedclass > OOB_NAMEDCLASS) { /* \w-, [:word:]- */
3493 if (ckWARN(WARN_REGEXP))
3494 vWARN4(PL_regcomp_parse,
3495 "False [] range \"%*.*s\"",
3496 PL_regcomp_parse - rangebegin,
3497 PL_regcomp_parse - rangebegin,
3500 ANYOF_BITMAP_SET(ret, '-');
3503 continue; /* do it next time */
3506 /* now is the next time */
3508 #ifndef ASCIIish /* EBCDIC, for example. */
3509 if ((isLOWER(lastvalue) && isLOWER(value)) ||
3510 (isUPPER(lastvalue) && isUPPER(value)))
3513 if (isLOWER(lastvalue)) {
3514 for (i = lastvalue; i <= value; i++)
3516 ANYOF_BITMAP_SET(ret, i);
3518 for (i = lastvalue; i <= value; i++)
3520 ANYOF_BITMAP_SET(ret, i);
3525 for ( ; lastvalue <= value; lastvalue++)
3526 ANYOF_BITMAP_SET(ret, lastvalue);
3532 PL_regsize += ANYOF_CLASS_ADD_SKIP;
3534 PL_regcode += ANYOF_CLASS_ADD_SKIP;
3536 /* optimize case-insensitive simple patterns (e.g. /[a-z]/i) */
3538 (ANYOF_FLAGS(ret) & (ANYOF_FLAGS_ALL ^ ANYOF_INVERT)) == ANYOF_FOLD) {
3539 for (value = 0; value < 256; ++value) {
3540 if (ANYOF_BITMAP_TEST(ret, value)) {
3541 I32 cf = PL_fold[value];
3542 ANYOF_BITMAP_SET(ret, cf);
3545 ANYOF_FLAGS(ret) &= ~ANYOF_FOLD;
3547 /* optimize inverted simple patterns (e.g. [^a-z]) */
3548 if (!SIZE_ONLY && (ANYOF_FLAGS(ret) & ANYOF_FLAGS_ALL) == ANYOF_INVERT) {
3549 for (value = 0; value < ANYOF_BITMAP_SIZE; ++value)
3550 ANYOF_BITMAP(ret)[value] ^= ANYOF_FLAGS_ALL;
3551 ANYOF_FLAGS(ret) = 0;
3557 S_regclassutf8(pTHX)
3562 register U32 lastvalue = OOB_UTF8;
3563 register I32 range = 0;
3564 register regnode *ret;
3572 if (*PL_regcomp_parse == '^') { /* Complement of range. */
3576 flags |= ANYOF_INVERT;
3580 flags |= ANYOF_FOLD;
3582 flags |= ANYOF_LOCALE;
3583 listsv = newSVpvn("# comment\n",10);
3586 if (!SIZE_ONLY && ckWARN(WARN_REGEXP))
3589 if (*PL_regcomp_parse == ']' || *PL_regcomp_parse == '-')
3590 goto skipcond; /* allow 1st char to be ] or - */
3592 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != ']') {
3594 namedclass = OOB_NAMEDCLASS;
3596 rangebegin = PL_regcomp_parse;
3597 value = utf8_to_uv((U8*)PL_regcomp_parse, &numlen);
3598 PL_regcomp_parse += numlen;
3600 namedclass = regpposixcc(value);
3601 else if (value == '\\') {
3602 value = (U32)utf8_to_uv((U8*)PL_regcomp_parse, &numlen);
3603 PL_regcomp_parse += numlen;
3604 /* Some compilers cannot handle switching on 64-bit integer
3605 * values, therefore value cannot be an UV. Yes, this will
3606 * be a problem later if we want switch on Unicode. --jhi */
3608 case 'w': namedclass = ANYOF_ALNUM; break;
3609 case 'W': namedclass = ANYOF_NALNUM; break;
3610 case 's': namedclass = ANYOF_SPACE; break;
3611 case 'S': namedclass = ANYOF_NSPACE; break;
3612 case 'd': namedclass = ANYOF_DIGIT; break;
3613 case 'D': namedclass = ANYOF_NDIGIT; break;
3616 if (*PL_regcomp_parse == '{') {
3617 e = strchr(PL_regcomp_parse++, '}');
3619 vFAIL("Missing right brace on \\p{}");
3620 n = e - PL_regcomp_parse;
3623 e = PL_regcomp_parse;
3628 Perl_sv_catpvf(aTHX_ listsv,
3629 "+utf8::%.*s\n", (int)n, PL_regcomp_parse);
3631 Perl_sv_catpvf(aTHX_ listsv,
3632 "!utf8::%.*s\n", (int)n, PL_regcomp_parse);
3634 PL_regcomp_parse = e + 1;
3635 lastvalue = OOB_UTF8;
3637 case 'n': value = '\n'; break;
3638 case 'r': value = '\r'; break;
3639 case 't': value = '\t'; break;
3640 case 'f': value = '\f'; break;
3641 case 'b': value = '\b'; break;
3643 case 'e': value = '\033'; break;
3644 case 'a': value = '\007'; break;
3646 case 'e': value = '\047'; break;
3647 case 'a': value = '\057'; break;
3650 if (*PL_regcomp_parse == '{') {
3651 e = strchr(PL_regcomp_parse++, '}');
3653 vFAIL("Missing right brace on \\x{}");
3654 numlen = 1; /* allow underscores */
3655 value = (UV)scan_hex(PL_regcomp_parse,
3656 e - PL_regcomp_parse,
3658 PL_regcomp_parse = e + 1;
3661 numlen = 0; /* disallow underscores */
3662 value = (UV)scan_hex(PL_regcomp_parse, 2, &numlen);
3663 PL_regcomp_parse += numlen;
3667 value = UCHARAT(PL_regcomp_parse++);
3668 value = toCTRL(value);
3670 case '0': case '1': case '2': case '3': case '4':
3671 case '5': case '6': case '7': case '8': case '9':
3672 numlen = 0; /* disallow underscores */
3673 value = (UV)scan_oct(--PL_regcomp_parse, 3, &numlen);
3674 PL_regcomp_parse += numlen;
3677 if (!SIZE_ONLY && ckWARN(WARN_REGEXP) && isALPHA(value))
3678 vWARN2(PL_regcomp_parse,
3679 "Unrecognized escape \\%c in character class passed through",
3684 if (namedclass > OOB_NAMEDCLASS) {
3685 if (range) { /* a-\d, a-[:digit:] */
3687 if (ckWARN(WARN_REGEXP))
3688 vWARN4(PL_regcomp_parse,
3689 "False [] range \"%*.*s\"",
3690 PL_regcomp_parse - rangebegin,
3691 PL_regcomp_parse - rangebegin,
3693 Perl_sv_catpvf(aTHX_ listsv,
3694 /* 0x002D is Unicode for '-' */
3695 "%04"UVxf"\n002D\n", (UV)lastvalue);
3700 switch (namedclass) {
3702 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsWord\n"); break;
3704 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsWord\n"); break;
3706 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsAlnum\n"); break;
3708 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsAlnum\n"); break;
3710 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsAlpha\n"); break;
3712 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsAlpha\n"); break;
3714 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsASCII\n"); break;
3716 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsASCII\n"); break;
3718 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsCntrl\n"); break;
3720 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsCntrl\n"); break;
3722 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsGraph\n"); break;
3724 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsGraph\n"); break;
3726 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsDigit\n"); break;
3728 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsDigit\n"); break;
3730 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsLower\n"); break;
3732 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsLower\n"); break;
3734 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsPrint\n"); break;
3736 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsPrint\n"); break;
3738 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsPunct\n"); break;
3740 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsPunct\n"); break;
3742 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsSpace\n"); break;
3744 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsSpace\n"); break;
3746 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsUpper\n"); break;
3748 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsUpper\n"); break;
3750 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsXDigit\n"); break;
3752 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsXDigit\n"); break;
3758 if (lastvalue > value) { /* b-a */
3759 Simple_vFAIL4("invalid [] range \"%*.*s\"",
3760 PL_regcomp_parse - rangebegin,
3761 PL_regcomp_parse - rangebegin,
3768 if (*PL_regcomp_parse == '-' && PL_regcomp_parse+1 < PL_regxend &&
3769 PL_regcomp_parse[1] != ']') {
3771 if (namedclass > OOB_NAMEDCLASS) { /* \w-, [:word:]- */
3772 if (ckWARN(WARN_REGEXP))
3773 vWARN4(PL_regcomp_parse,
3774 "False [] range \"%*.*s\"",
3775 PL_regcomp_parse - rangebegin,
3776 PL_regcomp_parse - rangebegin,
3779 Perl_sv_catpvf(aTHX_ listsv,
3780 /* 0x002D is Unicode for '-' */
3784 continue; /* do it next time */
3787 /* now is the next time */
3789 Perl_sv_catpvf(aTHX_ listsv, "%04"UVxf"\t%04"UVxf"\n",
3790 (UV)lastvalue, (UV)value);
3794 ret = reganode(ANYOFUTF8, 0);
3797 SV *rv = swash_init("utf8", "", listsv, 1, 0);
3798 SvREFCNT_dec(listsv);
3799 n = add_data(1,"s");
3800 PL_regcomp_rx->data->data[n] = (void*)rv;
3801 ARG1_SET(ret, flags);
3812 char* retval = PL_regcomp_parse++;
3815 if (*PL_regcomp_parse == '(' && PL_regcomp_parse[1] == '?' &&
3816 PL_regcomp_parse[2] == '#') {
3817 while (*PL_regcomp_parse && *PL_regcomp_parse != ')')
3822 if (PL_regflags & PMf_EXTENDED) {
3823 if (isSPACE(*PL_regcomp_parse)) {
3827 else if (*PL_regcomp_parse == '#') {
3828 while (*PL_regcomp_parse && *PL_regcomp_parse != '\n')
3839 - reg_node - emit a node
3841 STATIC regnode * /* Location. */
3842 S_reg_node(pTHX_ U8 op)
3845 register regnode *ret;
3846 register regnode *ptr;
3850 SIZE_ALIGN(PL_regsize);
3855 NODE_ALIGN_FILL(ret);
3857 FILL_ADVANCE_NODE(ptr, op);
3864 - reganode - emit a node with an argument
3866 STATIC regnode * /* Location. */
3867 S_reganode(pTHX_ U8 op, U32 arg)
3870 register regnode *ret;
3871 register regnode *ptr;
3875 SIZE_ALIGN(PL_regsize);
3880 NODE_ALIGN_FILL(ret);
3882 FILL_ADVANCE_NODE_ARG(ptr, op, arg);
3889 - reguni - emit (if appropriate) a Unicode character
3892 S_reguni(pTHX_ UV uv, char* s, I32* lenp)
3896 U8 tmpbuf[UTF8_MAXLEN];
3897 *lenp = uv_to_utf8(tmpbuf, uv) - tmpbuf;
3900 *lenp = uv_to_utf8((U8*)s, uv) - (U8*)s;
3905 - reginsert - insert an operator in front of already-emitted operand
3907 * Means relocating the operand.
3910 S_reginsert(pTHX_ U8 op, regnode *opnd)
3913 register regnode *src;
3914 register regnode *dst;
3915 register regnode *place;
3916 register int offset = regarglen[(U8)op];
3918 /* (PL_regkind[(U8)op] == CURLY ? EXTRA_STEP_2ARGS : 0); */
3921 PL_regsize += NODE_STEP_REGNODE + offset;
3926 PL_regcode += NODE_STEP_REGNODE + offset;
3929 StructCopy(--src, --dst, regnode);
3931 place = opnd; /* Op node, where operand used to be. */
3932 src = NEXTOPER(place);
3933 FILL_ADVANCE_NODE(place, op);
3934 Zero(src, offset, regnode);
3938 - regtail - set the next-pointer at the end of a node chain of p to val.
3941 S_regtail(pTHX_ regnode *p, regnode *val)
3944 register regnode *scan;
3945 register regnode *temp;
3950 /* Find last node. */
3953 temp = regnext(scan);
3959 if (reg_off_by_arg[OP(scan)]) {
3960 ARG_SET(scan, val - scan);
3963 NEXT_OFF(scan) = val - scan;
3968 - regoptail - regtail on operand of first argument; nop if operandless
3971 S_regoptail(pTHX_ regnode *p, regnode *val)
3974 /* "Operandless" and "op != BRANCH" are synonymous in practice. */
3975 if (p == NULL || SIZE_ONLY)
3977 if (PL_regkind[(U8)OP(p)] == BRANCH) {
3978 regtail(NEXTOPER(p), val);
3980 else if ( PL_regkind[(U8)OP(p)] == BRANCHJ) {
3981 regtail(NEXTOPER(NEXTOPER(p)), val);
3988 - regcurly - a little FSA that accepts {\d+,?\d*}
3991 S_regcurly(pTHX_ register char *s)
4010 S_dumpuntil(pTHX_ regnode *start, regnode *node, regnode *last, SV* sv, I32 l)
4013 register U8 op = EXACT; /* Arbitrary non-END op. */
4014 register regnode *next;
4016 while (op != END && (!last || node < last)) {
4017 /* While that wasn't END last time... */
4023 next = regnext(node);
4025 if (OP(node) == OPTIMIZED)
4028 PerlIO_printf(Perl_debug_log, "%4"IVdf":%*s%s", (IV)(node - start),
4029 (int)(2*l + 1), "", SvPVX(sv));
4030 if (next == NULL) /* Next ptr. */
4031 PerlIO_printf(Perl_debug_log, "(0)");
4033 PerlIO_printf(Perl_debug_log, "(%"IVdf")", (IV)(next - start));
4034 (void)PerlIO_putc(Perl_debug_log, '\n');
4036 if (PL_regkind[(U8)op] == BRANCHJ) {
4037 register regnode *nnode = (OP(next) == LONGJMP
4040 if (last && nnode > last)
4042 node = dumpuntil(start, NEXTOPER(NEXTOPER(node)), nnode, sv, l + 1);
4044 else if (PL_regkind[(U8)op] == BRANCH) {
4045 node = dumpuntil(start, NEXTOPER(node), next, sv, l + 1);
4047 else if ( op == CURLY) { /* `next' might be very big: optimizer */
4048 node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
4049 NEXTOPER(node) + EXTRA_STEP_2ARGS + 1, sv, l + 1);
4051 else if (PL_regkind[(U8)op] == CURLY && op != CURLYX) {
4052 node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
4055 else if ( op == PLUS || op == STAR) {
4056 node = dumpuntil(start, NEXTOPER(node), NEXTOPER(node) + 1, sv, l + 1);
4058 else if (op == ANYOF) {
4059 node = NEXTOPER(node);
4062 else if (PL_regkind[(U8)op] == EXACT) {
4063 /* Literal string, where present. */
4064 node += NODE_SZ_STR(node) - 1;
4065 node = NEXTOPER(node);
4068 node = NEXTOPER(node);
4069 node += regarglen[(U8)op];
4071 if (op == CURLYX || op == OPEN)
4073 else if (op == WHILEM)
4076 #endif /* DEBUGGING */
4081 - regdump - dump a regexp onto Perl_debug_log in vaguely comprehensible form
4084 Perl_regdump(pTHX_ regexp *r)
4088 SV *sv = sv_newmortal();
4090 (void)dumpuntil(r->program, r->program + 1, NULL, sv, 0);
4092 /* Header fields of interest. */
4093 if (r->anchored_substr)
4094 PerlIO_printf(Perl_debug_log,
4095 "anchored `%s%.*s%s'%s at %"IVdf" ",
4097 (int)(SvCUR(r->anchored_substr) - (SvTAIL(r->anchored_substr)!=0)),
4098 SvPVX(r->anchored_substr),
4100 SvTAIL(r->anchored_substr) ? "$" : "",
4101 (IV)r->anchored_offset);
4102 if (r->float_substr)
4103 PerlIO_printf(Perl_debug_log,
4104 "floating `%s%.*s%s'%s at %"IVdf"..%"UVuf" ",
4106 (int)(SvCUR(r->float_substr) - (SvTAIL(r->float_substr)!=0)),
4107 SvPVX(r->float_substr),
4109 SvTAIL(r->float_substr) ? "$" : "",
4110 (IV)r->float_min_offset, (UV)r->float_max_offset);
4111 if (r->check_substr)
4112 PerlIO_printf(Perl_debug_log,
4113 r->check_substr == r->float_substr
4114 ? "(checking floating" : "(checking anchored");
4115 if (r->reganch & ROPT_NOSCAN)
4116 PerlIO_printf(Perl_debug_log, " noscan");
4117 if (r->reganch & ROPT_CHECK_ALL)
4118 PerlIO_printf(Perl_debug_log, " isall");
4119 if (r->check_substr)
4120 PerlIO_printf(Perl_debug_log, ") ");
4122 if (r->regstclass) {
4123 regprop(sv, r->regstclass);
4124 PerlIO_printf(Perl_debug_log, "stclass `%s' ", SvPVX(sv));
4126 if (r->reganch & ROPT_ANCH) {
4127 PerlIO_printf(Perl_debug_log, "anchored");
4128 if (r->reganch & ROPT_ANCH_BOL)
4129 PerlIO_printf(Perl_debug_log, "(BOL)");
4130 if (r->reganch & ROPT_ANCH_MBOL)
4131 PerlIO_printf(Perl_debug_log, "(MBOL)");
4132 if (r->reganch & ROPT_ANCH_SBOL)
4133 PerlIO_printf(Perl_debug_log, "(SBOL)");
4134 if (r->reganch & ROPT_ANCH_GPOS)
4135 PerlIO_printf(Perl_debug_log, "(GPOS)");
4136 PerlIO_putc(Perl_debug_log, ' ');
4138 if (r->reganch & ROPT_GPOS_SEEN)
4139 PerlIO_printf(Perl_debug_log, "GPOS ");
4140 if (r->reganch & ROPT_SKIP)
4141 PerlIO_printf(Perl_debug_log, "plus ");
4142 if (r->reganch & ROPT_IMPLICIT)
4143 PerlIO_printf(Perl_debug_log, "implicit ");
4144 PerlIO_printf(Perl_debug_log, "minlen %ld ", (long) r->minlen);
4145 if (r->reganch & ROPT_EVAL_SEEN)
4146 PerlIO_printf(Perl_debug_log, "with eval ");
4147 PerlIO_printf(Perl_debug_log, "\n");
4148 #endif /* DEBUGGING */
4152 S_put_byte(pTHX_ SV *sv, int c)
4154 if (c <= ' ' || c == 127 || c == 255)
4155 Perl_sv_catpvf(aTHX_ sv, "\\%o", c);
4156 else if (c == '-' || c == ']' || c == '\\' || c == '^')
4157 Perl_sv_catpvf(aTHX_ sv, "\\%c", c);
4159 Perl_sv_catpvf(aTHX_ sv, "%c", c);
4163 - regprop - printable representation of opcode
4166 Perl_regprop(pTHX_ SV *sv, regnode *o)
4172 sv_setpvn(sv, "", 0);
4173 if (OP(o) >= reg_num) /* regnode.type is unsigned */
4174 FAIL("Corrupted regexp opcode");
4175 sv_catpv(sv, (char*)reg_name[OP(o)]); /* Take off const! */
4177 k = PL_regkind[(U8)OP(o)];
4180 Perl_sv_catpvf(aTHX_ sv, " <%s%.*s%s>", PL_colors[0],
4181 STR_LEN(o), STRING(o), PL_colors[1]);
4182 else if (k == CURLY) {
4183 if (OP(o) == CURLYM || OP(o) == CURLYN)
4184 Perl_sv_catpvf(aTHX_ sv, "[%d]", o->flags); /* Parenth number */
4185 Perl_sv_catpvf(aTHX_ sv, " {%d,%d}", ARG1(o), ARG2(o));
4187 else if (k == WHILEM && o->flags) /* Ordinal/of */
4188 Perl_sv_catpvf(aTHX_ sv, "[%d/%d]", o->flags & 0xf, o->flags>>4);
4189 else if (k == REF || k == OPEN || k == CLOSE || k == GROUPP )
4190 Perl_sv_catpvf(aTHX_ sv, "%d", (int)ARG(o)); /* Parenth number */
4191 else if (k == LOGICAL)
4192 Perl_sv_catpvf(aTHX_ sv, "[%d]", o->flags); /* 2: embedded, otherwise 1 */
4193 else if (k == ANYOF) {
4194 int i, rangestart = -1;
4195 const char * const out[] = { /* Should be syncronized with
4196 a table in regcomp.h */
4225 if (o->flags & ANYOF_LOCALE)
4226 sv_catpv(sv, "{loc}");
4227 if (o->flags & ANYOF_FOLD)
4228 sv_catpv(sv, "{i}");
4229 Perl_sv_catpvf(aTHX_ sv, "[%s", PL_colors[0]);
4230 if (o->flags & ANYOF_INVERT)
4232 for (i = 0; i <= 256; i++) {
4233 if (i < 256 && ANYOF_BITMAP_TEST(o,i)) {
4234 if (rangestart == -1)
4236 } else if (rangestart != -1) {
4237 if (i <= rangestart + 3)
4238 for (; rangestart < i; rangestart++)
4239 put_byte(sv, rangestart);
4241 put_byte(sv, rangestart);
4243 put_byte(sv, i - 1);
4248 if (o->flags & ANYOF_CLASS)
4249 for (i = 0; i < sizeof(out)/sizeof(char*); i++)
4250 if (ANYOF_CLASS_TEST(o,i))
4251 sv_catpv(sv, out[i]);
4252 Perl_sv_catpvf(aTHX_ sv, "%s]", PL_colors[1]);
4254 else if (k == BRANCHJ && (OP(o) == UNLESSM || OP(o) == IFMATCH))
4255 Perl_sv_catpvf(aTHX_ sv, "[-%d]", o->flags);
4256 #endif /* DEBUGGING */
4260 Perl_re_intuit_string(pTHX_ regexp *prog)
4261 { /* Assume that RE_INTUIT is set */
4264 char *s = SvPV(prog->check_substr,n_a);
4266 if (!PL_colorset) reginitcolors();
4267 PerlIO_printf(Perl_debug_log,
4268 "%sUsing REx substr:%s `%s%.60s%s%s'\n",
4269 PL_colors[4],PL_colors[5],PL_colors[0],
4272 (strlen(s) > 60 ? "..." : ""));
4275 return prog->check_substr;
4279 Perl_pregfree(pTHX_ struct regexp *r)
4282 DEBUG_r(if (!PL_colorset) reginitcolors());
4284 if (!r || (--r->refcnt > 0))
4286 DEBUG_r(PerlIO_printf(Perl_debug_log,
4287 "%sFreeing REx:%s `%s%.60s%s%s'\n",
4288 PL_colors[4],PL_colors[5],PL_colors[0],
4291 (strlen(r->precomp) > 60 ? "..." : "")));
4294 Safefree(r->precomp);
4295 if (RX_MATCH_COPIED(r))
4296 Safefree(r->subbeg);
4298 if (r->anchored_substr)
4299 SvREFCNT_dec(r->anchored_substr);
4300 if (r->float_substr)
4301 SvREFCNT_dec(r->float_substr);
4302 Safefree(r->substrs);
4305 int n = r->data->count;
4306 AV* new_comppad = NULL;
4311 switch (r->data->what[n]) {
4313 SvREFCNT_dec((SV*)r->data->data[n]);
4316 Safefree(r->data->data[n]);
4319 new_comppad = (AV*)r->data->data[n];
4322 if (new_comppad == NULL)
4323 Perl_croak(aTHX_ "panic: pregfree comppad");
4324 old_comppad = PL_comppad;
4325 old_curpad = PL_curpad;
4326 PL_comppad = new_comppad;
4327 PL_curpad = AvARRAY(new_comppad);
4328 op_free((OP_4tree*)r->data->data[n]);
4329 PL_comppad = old_comppad;
4330 PL_curpad = old_curpad;
4331 SvREFCNT_dec((SV*)new_comppad);
4337 FAIL2("panic: regfree data code '%c'", r->data->what[n]);
4340 Safefree(r->data->what);
4343 Safefree(r->startp);
4349 - regnext - dig the "next" pointer out of a node
4351 * [Note, when REGALIGN is defined there are two places in regmatch()
4352 * that bypass this code for speed.]
4355 Perl_regnext(pTHX_ register regnode *p)
4358 register I32 offset;
4360 if (p == &PL_regdummy)
4363 offset = (reg_off_by_arg[OP(p)] ? ARG(p) : NEXT_OFF(p));
4371 S_re_croak2(pTHX_ const char* pat1,const char* pat2,...)
4374 STRLEN l1 = strlen(pat1);
4375 STRLEN l2 = strlen(pat2);
4384 Copy(pat1, buf, l1 , char);
4385 Copy(pat2, buf + l1, l2 , char);
4386 buf[l1 + l2] = '\n';
4387 buf[l1 + l2 + 1] = '\0';
4389 /* ANSI variant takes additional second argument */
4390 va_start(args, pat2);
4394 msv = vmess(buf, &args);
4396 message = SvPV(msv,l1);
4399 Copy(message, buf, l1 , char);
4400 buf[l1] = '\0'; /* Overwrite \n */
4401 Perl_croak(aTHX_ "%s", buf);
4404 /* XXX Here's a total kludge. But we need to re-enter for swash routines. */
4407 Perl_save_re_context(pTHX)
4411 SAVEPPTR(PL_regprecomp); /* uncompiled string. */
4412 SAVEI32(PL_regnpar); /* () count. */
4413 SAVEI32(PL_regsize); /* Code size. */
4414 SAVEI16(PL_regflags); /* are we folding, multilining? */
4415 SAVEPPTR(PL_reginput); /* String-input pointer. */
4416 SAVEPPTR(PL_regbol); /* Beginning of input, for ^ check. */
4417 SAVEPPTR(PL_regeol); /* End of input, for $ check. */
4418 SAVEVPTR(PL_regstartp); /* Pointer to startp array. */
4419 SAVEVPTR(PL_regendp); /* Ditto for endp. */
4420 SAVEVPTR(PL_reglastparen); /* Similarly for lastparen. */
4421 SAVEPPTR(PL_regtill); /* How far we are required to go. */
4422 SAVEI8(PL_regprev); /* char before regbol, \n if none */
4423 SAVEVPTR(PL_reg_start_tmp); /* from regexec.c */
4424 PL_reg_start_tmp = 0;
4425 SAVEFREEPV(PL_reg_start_tmp);
4426 SAVEI32(PL_reg_start_tmpl); /* from regexec.c */
4427 PL_reg_start_tmpl = 0;
4428 SAVEVPTR(PL_regdata);
4429 SAVEI32(PL_reg_flags); /* from regexec.c */
4430 SAVEI32(PL_reg_eval_set); /* from regexec.c */
4431 SAVEI32(PL_regnarrate); /* from regexec.c */
4432 SAVEVPTR(PL_regprogram); /* from regexec.c */
4433 SAVEINT(PL_regindent); /* from regexec.c */
4434 SAVEVPTR(PL_regcc); /* from regexec.c */
4435 SAVEVPTR(PL_curcop);
4436 SAVEVPTR(PL_regcomp_rx); /* from regcomp.c */
4437 SAVEI32(PL_regseen); /* from regcomp.c */
4438 SAVEI32(PL_regsawback); /* Did we see \1, ...? */
4439 SAVEI32(PL_regnaughty); /* How bad is this pattern? */
4440 SAVEVPTR(PL_regcode); /* Code-emit pointer; ®dummy = don't */
4441 SAVEPPTR(PL_regxend); /* End of input for compile */
4442 SAVEPPTR(PL_regcomp_parse); /* Input-scan pointer. */
4443 SAVEVPTR(PL_reg_call_cc); /* from regexec.c */
4444 SAVEVPTR(PL_reg_re); /* from regexec.c */
4445 SAVEPPTR(PL_reg_ganch); /* from regexec.c */
4446 SAVESPTR(PL_reg_sv); /* from regexec.c */
4447 SAVEVPTR(PL_reg_magic); /* from regexec.c */
4448 SAVEI32(PL_reg_oldpos); /* from regexec.c */
4449 SAVEVPTR(PL_reg_oldcurpm); /* from regexec.c */
4450 SAVEVPTR(PL_reg_curpm); /* from regexec.c */
4452 SAVEPPTR(PL_reg_starttry); /* from regexec.c */
4463 clear_re(pTHXo_ void *r)
4465 ReREFCNT_dec((regexp *)r);