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 typedef struct RExC_state_t {
115 U16 flags16; /* are we folding, multilining? */
116 char *precomp; /* uncompiled string. */
118 char *end; /* End of input for compile */
119 char *parse; /* Input-scan pointer. */
120 I32 whilem_seen; /* number of WHILEM in this expr */
121 regnode *emit; /* Code-emit pointer; ®dummy = don't = compiling */
122 I32 naughty; /* How bad is this pattern? */
123 I32 sawback; /* Did we see \1, ...? */
125 I32 size; /* Code size. */
126 I32 npar; /* () count. */
131 char *starttry; /* -Dr: where regtry was called. */
132 #define RExC_starttry (pRExC_state->starttry)
136 #define RExC_flags16 (pRExC_state->flags16)
137 #define RExC_precomp (pRExC_state->precomp)
138 #define RExC_rx (pRExC_state->rx)
139 #define RExC_end (pRExC_state->end)
140 #define RExC_parse (pRExC_state->parse)
141 #define RExC_whilem_seen (pRExC_state->whilem_seen)
142 #define RExC_emit (pRExC_state->emit)
143 #define RExC_naughty (pRExC_state->naughty)
144 #define RExC_sawback (pRExC_state->sawback)
145 #define RExC_seen (pRExC_state->seen)
146 #define RExC_size (pRExC_state->size)
147 #define RExC_npar (pRExC_state->npar)
148 #define RExC_extralen (pRExC_state->extralen)
149 #define RExC_seen_zerolen (pRExC_state->seen_zerolen)
150 #define RExC_seen_evals (pRExC_state->seen_evals)
152 #define ISMULT1(c) ((c) == '*' || (c) == '+' || (c) == '?')
153 #define ISMULT2(s) ((*s) == '*' || (*s) == '+' || (*s) == '?' || \
154 ((*s) == '{' && regcurly(s)))
156 #define PERL_META "^$.[()|?+*\\"
158 #define META "^$.[()|?+*\\"
162 #undef SPSTART /* dratted cpp namespace... */
165 * Flags to be passed up and down.
167 #define WORST 0 /* Worst case. */
168 #define HASWIDTH 0x1 /* Known to match non-null strings. */
169 #define SIMPLE 0x2 /* Simple enough to be STAR/PLUS operand. */
170 #define SPSTART 0x4 /* Starts with * or +. */
171 #define TRYAGAIN 0x8 /* Weeded out a declaration. */
173 /* Length of a variant. */
175 typedef struct scan_data_t {
181 I32 last_end; /* min value, <0 unless valid. */
184 SV **longest; /* Either &l_fixed, or &l_float. */
188 I32 offset_float_min;
189 I32 offset_float_max;
193 struct regnode_charclass_class *start_class;
197 * Forward declarations for pregcomp()'s friends.
200 static scan_data_t zero_scan_data = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
203 #define SF_BEFORE_EOL (SF_BEFORE_SEOL|SF_BEFORE_MEOL)
204 #define SF_BEFORE_SEOL 0x1
205 #define SF_BEFORE_MEOL 0x2
206 #define SF_FIX_BEFORE_EOL (SF_FIX_BEFORE_SEOL|SF_FIX_BEFORE_MEOL)
207 #define SF_FL_BEFORE_EOL (SF_FL_BEFORE_SEOL|SF_FL_BEFORE_MEOL)
210 # define SF_FIX_SHIFT_EOL (0+2)
211 # define SF_FL_SHIFT_EOL (0+4)
213 # define SF_FIX_SHIFT_EOL (+2)
214 # define SF_FL_SHIFT_EOL (+4)
217 #define SF_FIX_BEFORE_SEOL (SF_BEFORE_SEOL << SF_FIX_SHIFT_EOL)
218 #define SF_FIX_BEFORE_MEOL (SF_BEFORE_MEOL << SF_FIX_SHIFT_EOL)
220 #define SF_FL_BEFORE_SEOL (SF_BEFORE_SEOL << SF_FL_SHIFT_EOL)
221 #define SF_FL_BEFORE_MEOL (SF_BEFORE_MEOL << SF_FL_SHIFT_EOL) /* 0x20 */
222 #define SF_IS_INF 0x40
223 #define SF_HAS_PAR 0x80
224 #define SF_IN_PAR 0x100
225 #define SF_HAS_EVAL 0x200
226 #define SCF_DO_SUBSTR 0x400
227 #define SCF_DO_STCLASS_AND 0x0800
228 #define SCF_DO_STCLASS_OR 0x1000
229 #define SCF_DO_STCLASS (SCF_DO_STCLASS_AND|SCF_DO_STCLASS_OR)
230 #define SCF_WHILEM_VISITED_POS 0x2000
233 #define UTF (PL_reg_flags & RF_utf8)
234 #define LOC (RExC_flags16 & PMf_LOCALE)
235 #define FOLD (RExC_flags16 & PMf_FOLD)
237 #define OOB_UNICODE 12345678
238 #define OOB_NAMEDCLASS -1
240 #define CHR_SVLEN(sv) (UTF ? sv_len_utf8(sv) : SvCUR(sv))
241 #define CHR_DIST(a,b) (UTF ? utf8_distance(a,b) : a - b)
244 /* length of regex to show in messages that don't mark a position within */
245 #define RegexLengthToShowInErrorMessages 127
248 * If MARKER[12] are adjusted, be sure to adjust the constants at the top
249 * of t/op/regmesg.t, the tests in t/op/re_tests, and those in
250 * op/pragma/warn/regcomp.
252 #define MARKER1 "HERE" /* marker as it appears in the description */
253 #define MARKER2 " << HERE " /* marker as it appears within the regex */
255 #define REPORT_LOCATION " before " MARKER1 " mark in regex m/%.*s" MARKER2 "%s/"
258 * Calls SAVEDESTRUCTOR_X if needed, then calls Perl_croak with the given
259 * arg. Show regex, up to a maximum length. If it's too long, chop and add
264 char *ellipses = ""; \
265 unsigned len = strlen(RExC_precomp); \
268 SAVEDESTRUCTOR_X(clear_re,(void*)RExC_rx); \
270 if (len > RegexLengthToShowInErrorMessages) { \
271 /* chop 10 shorter than the max, to ensure meaning of "..." */ \
272 len = RegexLengthToShowInErrorMessages - 10; \
275 Perl_croak(aTHX_ "%s in regex m/%.*s%s/", \
276 msg, (int)len, RExC_precomp, ellipses); \
280 * Calls SAVEDESTRUCTOR_X if needed, then calls Perl_croak with the given
281 * args. Show regex, up to a maximum length. If it's too long, chop and add
284 #define FAIL2(pat,msg) \
286 char *ellipses = ""; \
287 unsigned len = strlen(RExC_precomp); \
290 SAVEDESTRUCTOR_X(clear_re,(void*)RExC_rx); \
292 if (len > RegexLengthToShowInErrorMessages) { \
293 /* chop 10 shorter than the max, to ensure meaning of "..." */ \
294 len = RegexLengthToShowInErrorMessages - 10; \
297 S_re_croak2(aTHX_ pat, " in regex m/%.*s%s/", \
298 msg, (int)len, RExC_precomp, ellipses); \
303 * Simple_vFAIL -- like FAIL, but marks the current location in the scan
305 #define Simple_vFAIL(m) \
307 unsigned offset = strlen(RExC_precomp)-(RExC_end-RExC_parse); \
309 Perl_croak(aTHX_ "%s" REPORT_LOCATION, \
310 m, (int)offset, RExC_precomp, RExC_precomp + offset); \
314 * Calls SAVEDESTRUCTOR_X if needed, then Simple_vFAIL()
319 SAVEDESTRUCTOR_X(clear_re,(void*)RExC_rx); \
324 * Like Simple_vFAIL(), but accepts two arguments.
326 #define Simple_vFAIL2(m,a1) \
328 unsigned offset = strlen(RExC_precomp)-(RExC_end-RExC_parse); \
330 S_re_croak2(aTHX_ m, REPORT_LOCATION, a1, \
331 (int)offset, RExC_precomp, RExC_precomp + offset); \
335 * Calls SAVEDESTRUCTOR_X if needed, then Simple_vFAIL2().
337 #define vFAIL2(m,a1) \
340 SAVEDESTRUCTOR_X(clear_re,(void*)RExC_rx); \
341 Simple_vFAIL2(m, a1); \
346 * Like Simple_vFAIL(), but accepts three arguments.
348 #define Simple_vFAIL3(m, a1, a2) \
350 unsigned offset = strlen(RExC_precomp)-(RExC_end-RExC_parse); \
352 S_re_croak2(aTHX_ m, REPORT_LOCATION, a1, a2, \
353 (int)offset, RExC_precomp, RExC_precomp + offset); \
357 * Calls SAVEDESTRUCTOR_X if needed, then Simple_vFAIL3().
359 #define vFAIL3(m,a1,a2) \
362 SAVEDESTRUCTOR_X(clear_re,(void*)RExC_rx); \
363 Simple_vFAIL3(m, a1, a2); \
367 * Like Simple_vFAIL(), but accepts four arguments.
369 #define Simple_vFAIL4(m, a1, a2, a3) \
371 unsigned offset = strlen(RExC_precomp)-(RExC_end-RExC_parse); \
373 S_re_croak2(aTHX_ m, REPORT_LOCATION, a1, a2, a3,\
374 (int)offset, RExC_precomp, RExC_precomp + offset); \
378 * Like Simple_vFAIL(), but accepts five arguments.
380 #define Simple_vFAIL5(m, a1, a2, a3, a4) \
382 unsigned offset = strlen(RExC_precomp)-(RExC_end-RExC_parse); \
383 S_re_croak2(aTHX_ m, REPORT_LOCATION, a1, a2, a3, a4,\
384 (int)offset, RExC_precomp, RExC_precomp + offset); \
388 #define vWARN(loc,m) \
390 unsigned offset = strlen(RExC_precomp)-(RExC_end-(loc)); \
391 Perl_warner(aTHX_ WARN_REGEXP, "%s" REPORT_LOCATION,\
392 m, (int)offset, RExC_precomp, RExC_precomp + offset); \
396 #define vWARN2(loc, m, a1) \
398 unsigned offset = strlen(RExC_precomp)-(RExC_end-(loc)); \
399 Perl_warner(aTHX_ WARN_REGEXP, m REPORT_LOCATION,\
401 (int)offset, RExC_precomp, RExC_precomp + offset); \
404 #define vWARN3(loc, m, a1, a2) \
406 unsigned offset = strlen(RExC_precomp) - (RExC_end - (loc)); \
407 Perl_warner(aTHX_ WARN_REGEXP, m REPORT_LOCATION, \
409 (int)offset, RExC_precomp, RExC_precomp + offset); \
412 #define vWARN4(loc, m, a1, a2, a3) \
414 unsigned offset = strlen(RExC_precomp)-(RExC_end-(loc)); \
415 Perl_warner(aTHX_ WARN_REGEXP, m REPORT_LOCATION,\
417 (int)offset, RExC_precomp, RExC_precomp + offset); \
421 /* Allow for side effects in s */
422 #define REGC(c,s) STMT_START { if (!SIZE_ONLY) *(s) = (c); else (s);} STMT_END
424 static void clear_re(pTHXo_ void *r);
426 /* Mark that we cannot extend a found fixed substring at this point.
427 Updata the longest found anchored substring and the longest found
428 floating substrings if needed. */
431 S_scan_commit(pTHX_ RExC_state_t *pRExC_state, scan_data_t *data)
433 STRLEN l = CHR_SVLEN(data->last_found);
434 STRLEN old_l = CHR_SVLEN(*data->longest);
436 if ((l >= old_l) && ((l > old_l) || (data->flags & SF_BEFORE_EOL))) {
437 sv_setsv(*data->longest, data->last_found);
438 if (*data->longest == data->longest_fixed) {
439 data->offset_fixed = l ? data->last_start_min : data->pos_min;
440 if (data->flags & SF_BEFORE_EOL)
442 |= ((data->flags & SF_BEFORE_EOL) << SF_FIX_SHIFT_EOL);
444 data->flags &= ~SF_FIX_BEFORE_EOL;
447 data->offset_float_min = l ? data->last_start_min : data->pos_min;
448 data->offset_float_max = (l
449 ? data->last_start_max
450 : data->pos_min + data->pos_delta);
451 if (data->flags & SF_BEFORE_EOL)
453 |= ((data->flags & SF_BEFORE_EOL) << SF_FL_SHIFT_EOL);
455 data->flags &= ~SF_FL_BEFORE_EOL;
458 SvCUR_set(data->last_found, 0);
460 data->flags &= ~SF_BEFORE_EOL;
463 /* Can match anything (initialization) */
465 S_cl_anything(pTHX_ RExC_state_t *pRExC_state, struct regnode_charclass_class *cl)
469 ANYOF_CLASS_ZERO(cl);
470 for (value = 0; value < 256; ++value)
471 ANYOF_BITMAP_SET(cl, value);
472 cl->flags = ANYOF_EOS;
474 cl->flags |= ANYOF_LOCALE;
477 /* Can match anything (initialization) */
479 S_cl_is_anything(pTHX_ struct regnode_charclass_class *cl)
483 for (value = 0; value <= ANYOF_MAX; value += 2)
484 if (ANYOF_CLASS_TEST(cl, value) && ANYOF_CLASS_TEST(cl, value + 1))
486 for (value = 0; value < 256; ++value)
487 if (!ANYOF_BITMAP_TEST(cl, value))
492 /* Can match anything (initialization) */
494 S_cl_init(pTHX_ RExC_state_t *pRExC_state, struct regnode_charclass_class *cl)
496 Zero(cl, 1, struct regnode_charclass_class);
498 cl_anything(pRExC_state, cl);
502 S_cl_init_zero(pTHX_ RExC_state_t *pRExC_state, struct regnode_charclass_class *cl)
504 Zero(cl, 1, struct regnode_charclass_class);
506 cl_anything(pRExC_state, cl);
508 cl->flags |= ANYOF_LOCALE;
511 /* 'And' a given class with another one. Can create false positives */
512 /* We assume that cl is not inverted */
514 S_cl_and(pTHX_ struct regnode_charclass_class *cl,
515 struct regnode_charclass_class *and_with)
517 if (!(and_with->flags & ANYOF_CLASS)
518 && !(cl->flags & ANYOF_CLASS)
519 && (and_with->flags & ANYOF_LOCALE) == (cl->flags & ANYOF_LOCALE)
520 && !(and_with->flags & ANYOF_FOLD)
521 && !(cl->flags & ANYOF_FOLD)) {
524 if (and_with->flags & ANYOF_INVERT)
525 for (i = 0; i < ANYOF_BITMAP_SIZE; i++)
526 cl->bitmap[i] &= ~and_with->bitmap[i];
528 for (i = 0; i < ANYOF_BITMAP_SIZE; i++)
529 cl->bitmap[i] &= and_with->bitmap[i];
530 } /* XXXX: logic is complicated otherwise, leave it along for a moment. */
531 if (!(and_with->flags & ANYOF_EOS))
532 cl->flags &= ~ANYOF_EOS;
535 /* 'OR' a given class with another one. Can create false positives */
536 /* We assume that cl is not inverted */
538 S_cl_or(pTHX_ RExC_state_t *pRExC_state, struct regnode_charclass_class *cl, struct regnode_charclass_class *or_with)
540 if (or_with->flags & ANYOF_INVERT) {
542 * (B1 | CL1) | (!B2 & !CL2) = (B1 | !B2 & !CL2) | (CL1 | (!B2 & !CL2))
543 * <= (B1 | !B2) | (CL1 | !CL2)
544 * which is wasteful if CL2 is small, but we ignore CL2:
545 * (B1 | CL1) | (!B2 & !CL2) <= (B1 | CL1) | !B2 = (B1 | !B2) | CL1
546 * XXXX Can we handle case-fold? Unclear:
547 * (OK1(i) | OK1(i')) | !(OK1(i) | OK1(i')) =
548 * (OK1(i) | OK1(i')) | (!OK1(i) & !OK1(i'))
550 if ( (or_with->flags & ANYOF_LOCALE) == (cl->flags & ANYOF_LOCALE)
551 && !(or_with->flags & ANYOF_FOLD)
552 && !(cl->flags & ANYOF_FOLD) ) {
555 for (i = 0; i < ANYOF_BITMAP_SIZE; i++)
556 cl->bitmap[i] |= ~or_with->bitmap[i];
557 } /* XXXX: logic is complicated otherwise */
559 cl_anything(pRExC_state, cl);
562 /* (B1 | CL1) | (B2 | CL2) = (B1 | B2) | (CL1 | CL2)) */
563 if ( (or_with->flags & ANYOF_LOCALE) == (cl->flags & ANYOF_LOCALE)
564 && (!(or_with->flags & ANYOF_FOLD)
565 || (cl->flags & ANYOF_FOLD)) ) {
568 /* OR char bitmap and class bitmap separately */
569 for (i = 0; i < ANYOF_BITMAP_SIZE; i++)
570 cl->bitmap[i] |= or_with->bitmap[i];
571 if (or_with->flags & ANYOF_CLASS) {
572 for (i = 0; i < ANYOF_CLASSBITMAP_SIZE; i++)
573 cl->classflags[i] |= or_with->classflags[i];
574 cl->flags |= ANYOF_CLASS;
577 else { /* XXXX: logic is complicated, leave it along for a moment. */
578 cl_anything(pRExC_state, cl);
581 if (or_with->flags & ANYOF_EOS)
582 cl->flags |= ANYOF_EOS;
585 /* REx optimizer. Converts nodes into quickier variants "in place".
586 Finds fixed substrings. */
588 /* Stops at toplevel WHILEM as well as at `last'. At end *scanp is set
589 to the position after last scanned or to NULL. */
592 S_study_chunk(pTHX_ RExC_state_t *pRExC_state, regnode **scanp, I32 *deltap, regnode *last, scan_data_t *data, U32 flags)
593 /* scanp: Start here (read-write). */
594 /* deltap: Write maxlen-minlen here. */
595 /* last: Stop before this one. */
597 I32 min = 0, pars = 0, code;
598 regnode *scan = *scanp, *next;
600 int is_inf = (flags & SCF_DO_SUBSTR) && (data->flags & SF_IS_INF);
601 int is_inf_internal = 0; /* The studied chunk is infinite */
602 I32 is_par = OP(scan) == OPEN ? ARG(scan) : 0;
603 scan_data_t data_fake;
604 struct regnode_charclass_class and_with; /* Valid if flags & SCF_DO_STCLASS_OR */
606 while (scan && OP(scan) != END && scan < last) {
607 /* Peephole optimizer: */
609 if (PL_regkind[(U8)OP(scan)] == EXACT) {
610 /* Merge several consecutive EXACTish nodes into one. */
611 regnode *n = regnext(scan);
614 regnode *stop = scan;
617 next = scan + NODE_SZ_STR(scan);
618 /* Skip NOTHING, merge EXACT*. */
620 ( PL_regkind[(U8)OP(n)] == NOTHING ||
621 (stringok && (OP(n) == OP(scan))))
623 && NEXT_OFF(scan) + NEXT_OFF(n) < I16_MAX) {
624 if (OP(n) == TAIL || n > next)
626 if (PL_regkind[(U8)OP(n)] == NOTHING) {
627 NEXT_OFF(scan) += NEXT_OFF(n);
628 next = n + NODE_STEP_REGNODE;
636 int oldl = STR_LEN(scan);
637 regnode *nnext = regnext(n);
639 if (oldl + STR_LEN(n) > U8_MAX)
641 NEXT_OFF(scan) += NEXT_OFF(n);
642 STR_LEN(scan) += STR_LEN(n);
643 next = n + NODE_SZ_STR(n);
644 /* Now we can overwrite *n : */
645 Move(STRING(n), STRING(scan) + oldl,
656 n = scan + NODE_SZ_STR(scan);
658 if (PL_regkind[(U8)OP(n)] != NOTHING || OP(n) == NOTHING) {
666 /* Follow the next-chain of the current node and optimize
667 away all the NOTHINGs from it. */
668 if (OP(scan) != CURLYX) {
669 int max = (reg_off_by_arg[OP(scan)]
671 /* I32 may be smaller than U16 on CRAYs! */
672 : (I32_MAX < U16_MAX ? I32_MAX : U16_MAX));
673 int off = (reg_off_by_arg[OP(scan)] ? ARG(scan) : NEXT_OFF(scan));
677 /* Skip NOTHING and LONGJMP. */
678 while ((n = regnext(n))
679 && ((PL_regkind[(U8)OP(n)] == NOTHING && (noff = NEXT_OFF(n)))
680 || ((OP(n) == LONGJMP) && (noff = ARG(n))))
683 if (reg_off_by_arg[OP(scan)])
686 NEXT_OFF(scan) = off;
688 /* The principal pseudo-switch. Cannot be a switch, since we
689 look into several different things. */
690 if (OP(scan) == BRANCH || OP(scan) == BRANCHJ
691 || OP(scan) == IFTHEN || OP(scan) == SUSPEND) {
692 next = regnext(scan);
695 if (OP(next) == code || code == IFTHEN || code == SUSPEND) {
696 I32 max1 = 0, min1 = I32_MAX, num = 0;
697 struct regnode_charclass_class accum;
699 if (flags & SCF_DO_SUBSTR) /* XXXX Add !SUSPEND? */
700 scan_commit(pRExC_state, data); /* Cannot merge strings after this. */
701 if (flags & SCF_DO_STCLASS)
702 cl_init_zero(pRExC_state, &accum);
703 while (OP(scan) == code) {
704 I32 deltanext, minnext, f = 0, fake;
705 struct regnode_charclass_class this_class;
710 data_fake.whilem_c = data->whilem_c;
711 data_fake.last_closep = data->last_closep;
714 data_fake.last_closep = &fake;
715 next = regnext(scan);
716 scan = NEXTOPER(scan);
718 scan = NEXTOPER(scan);
719 if (flags & SCF_DO_STCLASS) {
720 cl_init(pRExC_state, &this_class);
721 data_fake.start_class = &this_class;
722 f = SCF_DO_STCLASS_AND;
724 if (flags & SCF_WHILEM_VISITED_POS)
725 f |= SCF_WHILEM_VISITED_POS;
726 /* we suppose the run is continuous, last=next...*/
727 minnext = study_chunk(pRExC_state, &scan, &deltanext,
728 next, &data_fake, f);
731 if (max1 < minnext + deltanext)
732 max1 = minnext + deltanext;
733 if (deltanext == I32_MAX)
734 is_inf = is_inf_internal = 1;
736 if (data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
738 if (data && (data_fake.flags & SF_HAS_EVAL))
739 data->flags |= SF_HAS_EVAL;
741 data->whilem_c = data_fake.whilem_c;
742 if (flags & SCF_DO_STCLASS)
743 cl_or(pRExC_state, &accum, &this_class);
747 if (code == IFTHEN && num < 2) /* Empty ELSE branch */
749 if (flags & SCF_DO_SUBSTR) {
750 data->pos_min += min1;
751 data->pos_delta += max1 - min1;
752 if (max1 != min1 || is_inf)
753 data->longest = &(data->longest_float);
756 delta += max1 - min1;
757 if (flags & SCF_DO_STCLASS_OR) {
758 cl_or(pRExC_state, data->start_class, &accum);
760 cl_and(data->start_class, &and_with);
761 flags &= ~SCF_DO_STCLASS;
764 else if (flags & SCF_DO_STCLASS_AND) {
766 cl_and(data->start_class, &accum);
767 flags &= ~SCF_DO_STCLASS;
770 /* Switch to OR mode: cache the old value of
771 * data->start_class */
772 StructCopy(data->start_class, &and_with,
773 struct regnode_charclass_class);
774 flags &= ~SCF_DO_STCLASS_AND;
775 StructCopy(&accum, data->start_class,
776 struct regnode_charclass_class);
777 flags |= SCF_DO_STCLASS_OR;
778 data->start_class->flags |= ANYOF_EOS;
782 else if (code == BRANCHJ) /* single branch is optimized. */
783 scan = NEXTOPER(NEXTOPER(scan));
784 else /* single branch is optimized. */
785 scan = NEXTOPER(scan);
788 else if (OP(scan) == EXACT) {
789 I32 l = STR_LEN(scan);
791 unsigned char *s = (unsigned char *)STRING(scan);
792 unsigned char *e = s + l;
801 if (flags & SCF_DO_SUBSTR) { /* Update longest substr. */
802 /* The code below prefers earlier match for fixed
803 offset, later match for variable offset. */
804 if (data->last_end == -1) { /* Update the start info. */
805 data->last_start_min = data->pos_min;
806 data->last_start_max = is_inf
807 ? I32_MAX : data->pos_min + data->pos_delta;
809 sv_catpvn(data->last_found, STRING(scan), STR_LEN(scan));
810 data->last_end = data->pos_min + l;
811 data->pos_min += l; /* As in the first entry. */
812 data->flags &= ~SF_BEFORE_EOL;
814 if (flags & SCF_DO_STCLASS_AND) {
815 /* Check whether it is compatible with what we know already! */
818 if (!(data->start_class->flags & (ANYOF_CLASS | ANYOF_LOCALE))
819 && !ANYOF_BITMAP_TEST(data->start_class, *STRING(scan))
820 && (!(data->start_class->flags & ANYOF_FOLD)
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;
830 else if (flags & SCF_DO_STCLASS_OR) {
831 /* false positive possible if the class is case-folded */
832 ANYOF_BITMAP_SET(data->start_class, *STRING(scan));
833 data->start_class->flags &= ~ANYOF_EOS;
834 cl_and(data->start_class, &and_with);
836 flags &= ~SCF_DO_STCLASS;
838 else if (PL_regkind[(U8)OP(scan)] == EXACT) { /* But OP != EXACT! */
839 I32 l = STR_LEN(scan);
841 /* Search for fixed substrings supports EXACT only. */
842 if (flags & SCF_DO_SUBSTR)
843 scan_commit(pRExC_state, data);
845 unsigned char *s = (unsigned char *)STRING(scan);
846 unsigned char *e = s + l;
855 if (data && (flags & SCF_DO_SUBSTR))
857 if (flags & SCF_DO_STCLASS_AND) {
858 /* Check whether it is compatible with what we know already! */
861 if (!(data->start_class->flags & (ANYOF_CLASS | ANYOF_LOCALE))
862 && !ANYOF_BITMAP_TEST(data->start_class, *STRING(scan))
863 && !ANYOF_BITMAP_TEST(data->start_class,
864 PL_fold[*(U8*)STRING(scan)]))
866 ANYOF_CLASS_ZERO(data->start_class);
867 ANYOF_BITMAP_ZERO(data->start_class);
869 ANYOF_BITMAP_SET(data->start_class, *STRING(scan));
870 data->start_class->flags &= ~ANYOF_EOS;
871 data->start_class->flags |= ANYOF_FOLD;
872 if (OP(scan) == EXACTFL)
873 data->start_class->flags |= ANYOF_LOCALE;
876 else if (flags & SCF_DO_STCLASS_OR) {
877 if (data->start_class->flags & ANYOF_FOLD) {
878 /* false positive possible if the class is case-folded.
879 Assume that the locale settings are the same... */
880 ANYOF_BITMAP_SET(data->start_class, *STRING(scan));
881 data->start_class->flags &= ~ANYOF_EOS;
883 cl_and(data->start_class, &and_with);
885 flags &= ~SCF_DO_STCLASS;
887 else if (strchr((char*)PL_varies,OP(scan))) {
888 I32 mincount, maxcount, minnext, deltanext, fl;
889 I32 f = flags, pos_before = 0;
890 regnode *oscan = scan;
891 struct regnode_charclass_class this_class;
892 struct regnode_charclass_class *oclass = NULL;
894 switch (PL_regkind[(U8)OP(scan)]) {
895 case WHILEM: /* End of (?:...)* . */
896 scan = NEXTOPER(scan);
899 if (flags & (SCF_DO_SUBSTR | SCF_DO_STCLASS)) {
900 next = NEXTOPER(scan);
901 if (OP(next) == EXACT || (flags & SCF_DO_STCLASS)) {
903 maxcount = REG_INFTY;
904 next = regnext(scan);
905 scan = NEXTOPER(scan);
909 if (flags & SCF_DO_SUBSTR)
914 if (flags & SCF_DO_STCLASS) {
916 maxcount = REG_INFTY;
917 next = regnext(scan);
918 scan = NEXTOPER(scan);
921 is_inf = is_inf_internal = 1;
922 scan = regnext(scan);
923 if (flags & SCF_DO_SUBSTR) {
924 scan_commit(pRExC_state, data); /* Cannot extend fixed substrings */
925 data->longest = &(data->longest_float);
927 goto optimize_curly_tail;
929 mincount = ARG1(scan);
930 maxcount = ARG2(scan);
931 next = regnext(scan);
932 if (OP(scan) == CURLYX) {
933 I32 lp = (data ? *(data->last_closep) : 0);
935 scan->flags = ((lp <= U8_MAX) ? lp : U8_MAX);
937 scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
939 if (flags & SCF_DO_SUBSTR) {
940 if (mincount == 0) scan_commit(pRExC_state,data); /* Cannot extend fixed substrings */
941 pos_before = data->pos_min;
945 data->flags &= ~(SF_HAS_PAR|SF_IN_PAR|SF_HAS_EVAL);
947 data->flags |= SF_IS_INF;
949 if (flags & SCF_DO_STCLASS) {
950 cl_init(pRExC_state, &this_class);
951 oclass = data->start_class;
952 data->start_class = &this_class;
953 f |= SCF_DO_STCLASS_AND;
954 f &= ~SCF_DO_STCLASS_OR;
956 /* These are the cases when once a subexpression
957 fails at a particular position, it cannot succeed
958 even after backtracking at the enclosing scope.
960 XXXX what if minimal match and we are at the
961 initial run of {n,m}? */
962 if ((mincount != maxcount - 1) && (maxcount != REG_INFTY))
963 f &= ~SCF_WHILEM_VISITED_POS;
965 /* This will finish on WHILEM, setting scan, or on NULL: */
966 minnext = study_chunk(pRExC_state, &scan, &deltanext, last, data,
968 ? (f & ~SCF_DO_SUBSTR) : f);
970 if (flags & SCF_DO_STCLASS)
971 data->start_class = oclass;
972 if (mincount == 0 || minnext == 0) {
973 if (flags & SCF_DO_STCLASS_OR) {
974 cl_or(pRExC_state, data->start_class, &this_class);
976 else if (flags & SCF_DO_STCLASS_AND) {
977 /* Switch to OR mode: cache the old value of
978 * data->start_class */
979 StructCopy(data->start_class, &and_with,
980 struct regnode_charclass_class);
981 flags &= ~SCF_DO_STCLASS_AND;
982 StructCopy(&this_class, data->start_class,
983 struct regnode_charclass_class);
984 flags |= SCF_DO_STCLASS_OR;
985 data->start_class->flags |= ANYOF_EOS;
987 } else { /* Non-zero len */
988 if (flags & SCF_DO_STCLASS_OR) {
989 cl_or(pRExC_state, data->start_class, &this_class);
990 cl_and(data->start_class, &and_with);
992 else if (flags & SCF_DO_STCLASS_AND)
993 cl_and(data->start_class, &this_class);
994 flags &= ~SCF_DO_STCLASS;
996 if (!scan) /* It was not CURLYX, but CURLY. */
998 if (ckWARN(WARN_REGEXP) && (minnext + deltanext == 0)
999 && !(data->flags & (SF_HAS_PAR|SF_IN_PAR))
1000 && maxcount <= REG_INFTY/3) /* Complement check for big count */
1003 "Quantifier unexpected on zero-length expression");
1006 min += minnext * mincount;
1007 is_inf_internal |= ((maxcount == REG_INFTY
1008 && (minnext + deltanext) > 0)
1009 || deltanext == I32_MAX);
1010 is_inf |= is_inf_internal;
1011 delta += (minnext + deltanext) * maxcount - minnext * mincount;
1013 /* Try powerful optimization CURLYX => CURLYN. */
1014 if ( OP(oscan) == CURLYX && data
1015 && data->flags & SF_IN_PAR
1016 && !(data->flags & SF_HAS_EVAL)
1017 && !deltanext && minnext == 1 ) {
1018 /* Try to optimize to CURLYN. */
1019 regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS;
1020 regnode *nxt1 = nxt, *nxt2;
1024 if (!strchr((char*)PL_simple,OP(nxt))
1025 && !(PL_regkind[(U8)OP(nxt)] == EXACT
1026 && STR_LEN(nxt) == 1))
1030 if (OP(nxt) != CLOSE)
1032 /* Now we know that nxt2 is the only contents: */
1033 oscan->flags = ARG(nxt);
1035 OP(nxt1) = NOTHING; /* was OPEN. */
1037 OP(nxt1 + 1) = OPTIMIZED; /* was count. */
1038 NEXT_OFF(nxt1+ 1) = 0; /* just for consistancy. */
1039 NEXT_OFF(nxt2) = 0; /* just for consistancy with CURLY. */
1040 OP(nxt) = OPTIMIZED; /* was CLOSE. */
1041 OP(nxt + 1) = OPTIMIZED; /* was count. */
1042 NEXT_OFF(nxt+ 1) = 0; /* just for consistancy. */
1047 /* Try optimization CURLYX => CURLYM. */
1048 if ( OP(oscan) == CURLYX && data
1049 && !(data->flags & SF_HAS_PAR)
1050 && !(data->flags & SF_HAS_EVAL)
1052 /* XXXX How to optimize if data == 0? */
1053 /* Optimize to a simpler form. */
1054 regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN */
1058 while ( (nxt2 = regnext(nxt)) /* skip over embedded stuff*/
1059 && (OP(nxt2) != WHILEM))
1061 OP(nxt2) = SUCCEED; /* Whas WHILEM */
1062 /* Need to optimize away parenths. */
1063 if (data->flags & SF_IN_PAR) {
1064 /* Set the parenth number. */
1065 regnode *nxt1 = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN*/
1067 if (OP(nxt) != CLOSE)
1068 FAIL("Panic opt close");
1069 oscan->flags = ARG(nxt);
1070 OP(nxt1) = OPTIMIZED; /* was OPEN. */
1071 OP(nxt) = OPTIMIZED; /* was CLOSE. */
1073 OP(nxt1 + 1) = OPTIMIZED; /* was count. */
1074 OP(nxt + 1) = OPTIMIZED; /* was count. */
1075 NEXT_OFF(nxt1 + 1) = 0; /* just for consistancy. */
1076 NEXT_OFF(nxt + 1) = 0; /* just for consistancy. */
1079 while ( nxt1 && (OP(nxt1) != WHILEM)) {
1080 regnode *nnxt = regnext(nxt1);
1083 if (reg_off_by_arg[OP(nxt1)])
1084 ARG_SET(nxt1, nxt2 - nxt1);
1085 else if (nxt2 - nxt1 < U16_MAX)
1086 NEXT_OFF(nxt1) = nxt2 - nxt1;
1088 OP(nxt) = NOTHING; /* Cannot beautify */
1093 /* Optimize again: */
1094 study_chunk(pRExC_state, &nxt1, &deltanext, nxt,
1100 else if ((OP(oscan) == CURLYX)
1101 && (flags & SCF_WHILEM_VISITED_POS)
1102 /* See the comment on a similar expression above.
1103 However, this time it not a subexpression
1104 we care about, but the expression itself. */
1105 && (maxcount == REG_INFTY)
1106 && data && ++data->whilem_c < 16) {
1107 /* This stays as CURLYX, we can put the count/of pair. */
1108 /* Find WHILEM (as in regexec.c) */
1109 regnode *nxt = oscan + NEXT_OFF(oscan);
1111 if (OP(PREVOPER(nxt)) == NOTHING) /* LONGJMP */
1113 PREVOPER(nxt)->flags = data->whilem_c
1114 | (RExC_whilem_seen << 4); /* On WHILEM */
1116 if (data && fl & (SF_HAS_PAR|SF_IN_PAR))
1118 if (flags & SCF_DO_SUBSTR) {
1119 SV *last_str = Nullsv;
1120 int counted = mincount != 0;
1122 if (data->last_end > 0 && mincount != 0) { /* Ends with a string. */
1123 I32 b = pos_before >= data->last_start_min
1124 ? pos_before : data->last_start_min;
1126 char *s = SvPV(data->last_found, l);
1127 I32 old = b - data->last_start_min;
1130 old = utf8_hop((U8*)s, old) - (U8*)s;
1133 /* Get the added string: */
1134 last_str = newSVpvn(s + old, l);
1135 if (deltanext == 0 && pos_before == b) {
1136 /* What was added is a constant string */
1138 SvGROW(last_str, (mincount * l) + 1);
1139 repeatcpy(SvPVX(last_str) + l,
1140 SvPVX(last_str), l, mincount - 1);
1141 SvCUR(last_str) *= mincount;
1142 /* Add additional parts. */
1143 SvCUR_set(data->last_found,
1144 SvCUR(data->last_found) - l);
1145 sv_catsv(data->last_found, last_str);
1146 data->last_end += l * (mincount - 1);
1149 /* start offset must point into the last copy */
1150 data->last_start_min += minnext * (mincount - 1);
1151 data->last_start_max += is_inf ? 0 : (maxcount - 1)
1152 * (minnext + data->pos_delta);
1155 /* It is counted once already... */
1156 data->pos_min += minnext * (mincount - counted);
1157 data->pos_delta += - counted * deltanext +
1158 (minnext + deltanext) * maxcount - minnext * mincount;
1159 if (mincount != maxcount) {
1160 /* Cannot extend fixed substrings found inside
1162 scan_commit(pRExC_state,data);
1163 if (mincount && last_str) {
1164 sv_setsv(data->last_found, last_str);
1165 data->last_end = data->pos_min;
1166 data->last_start_min =
1167 data->pos_min - CHR_SVLEN(last_str);
1168 data->last_start_max = is_inf
1170 : data->pos_min + data->pos_delta
1171 - CHR_SVLEN(last_str);
1173 data->longest = &(data->longest_float);
1175 SvREFCNT_dec(last_str);
1177 if (data && (fl & SF_HAS_EVAL))
1178 data->flags |= SF_HAS_EVAL;
1179 optimize_curly_tail:
1180 if (OP(oscan) != CURLYX) {
1181 while (PL_regkind[(U8)OP(next = regnext(oscan))] == NOTHING
1183 NEXT_OFF(oscan) += NEXT_OFF(next);
1186 default: /* REF and CLUMP only? */
1187 if (flags & SCF_DO_SUBSTR) {
1188 scan_commit(pRExC_state,data); /* Cannot expect anything... */
1189 data->longest = &(data->longest_float);
1191 is_inf = is_inf_internal = 1;
1192 if (flags & SCF_DO_STCLASS_OR)
1193 cl_anything(pRExC_state, data->start_class);
1194 flags &= ~SCF_DO_STCLASS;
1198 else if (strchr((char*)PL_simple,OP(scan))) {
1201 if (flags & SCF_DO_SUBSTR) {
1202 scan_commit(pRExC_state,data);
1206 if (flags & SCF_DO_STCLASS) {
1207 data->start_class->flags &= ~ANYOF_EOS; /* No match on empty */
1209 /* Some of the logic below assumes that switching
1210 locale on will only add false positives. */
1211 switch (PL_regkind[(U8)OP(scan)]) {
1215 /* Perl_croak(aTHX_ "panic: unexpected simple REx opcode %d", OP(scan)); */
1216 if (flags & SCF_DO_STCLASS_OR) /* Allow everything */
1217 cl_anything(pRExC_state, data->start_class);
1220 if (OP(scan) == SANY)
1222 if (flags & SCF_DO_STCLASS_OR) { /* Everything but \n */
1223 value = (ANYOF_BITMAP_TEST(data->start_class,'\n')
1224 || (data->start_class->flags & ANYOF_CLASS));
1225 cl_anything(pRExC_state, data->start_class);
1227 if (flags & SCF_DO_STCLASS_AND || !value)
1228 ANYOF_BITMAP_CLEAR(data->start_class,'\n');
1231 if (flags & SCF_DO_STCLASS_AND)
1232 cl_and(data->start_class,
1233 (struct regnode_charclass_class*)scan);
1235 cl_or(pRExC_state, data->start_class,
1236 (struct regnode_charclass_class*)scan);
1239 if (flags & SCF_DO_STCLASS_AND) {
1240 if (!(data->start_class->flags & ANYOF_LOCALE)) {
1241 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NALNUM);
1242 for (value = 0; value < 256; value++)
1243 if (!isALNUM(value))
1244 ANYOF_BITMAP_CLEAR(data->start_class, value);
1248 if (data->start_class->flags & ANYOF_LOCALE)
1249 ANYOF_CLASS_SET(data->start_class,ANYOF_ALNUM);
1251 for (value = 0; value < 256; value++)
1253 ANYOF_BITMAP_SET(data->start_class, value);
1258 if (flags & SCF_DO_STCLASS_AND) {
1259 if (data->start_class->flags & ANYOF_LOCALE)
1260 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NALNUM);
1263 ANYOF_CLASS_SET(data->start_class,ANYOF_ALNUM);
1264 data->start_class->flags |= ANYOF_LOCALE;
1268 if (flags & SCF_DO_STCLASS_AND) {
1269 if (!(data->start_class->flags & ANYOF_LOCALE)) {
1270 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_ALNUM);
1271 for (value = 0; value < 256; value++)
1273 ANYOF_BITMAP_CLEAR(data->start_class, value);
1277 if (data->start_class->flags & ANYOF_LOCALE)
1278 ANYOF_CLASS_SET(data->start_class,ANYOF_NALNUM);
1280 for (value = 0; value < 256; value++)
1281 if (!isALNUM(value))
1282 ANYOF_BITMAP_SET(data->start_class, value);
1287 if (flags & SCF_DO_STCLASS_AND) {
1288 if (data->start_class->flags & ANYOF_LOCALE)
1289 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_ALNUM);
1292 data->start_class->flags |= ANYOF_LOCALE;
1293 ANYOF_CLASS_SET(data->start_class,ANYOF_NALNUM);
1297 if (flags & SCF_DO_STCLASS_AND) {
1298 if (!(data->start_class->flags & ANYOF_LOCALE)) {
1299 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NSPACE);
1300 for (value = 0; value < 256; value++)
1301 if (!isSPACE(value))
1302 ANYOF_BITMAP_CLEAR(data->start_class, value);
1306 if (data->start_class->flags & ANYOF_LOCALE)
1307 ANYOF_CLASS_SET(data->start_class,ANYOF_SPACE);
1309 for (value = 0; value < 256; value++)
1311 ANYOF_BITMAP_SET(data->start_class, value);
1316 if (flags & SCF_DO_STCLASS_AND) {
1317 if (data->start_class->flags & ANYOF_LOCALE)
1318 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NSPACE);
1321 data->start_class->flags |= ANYOF_LOCALE;
1322 ANYOF_CLASS_SET(data->start_class,ANYOF_SPACE);
1326 if (flags & SCF_DO_STCLASS_AND) {
1327 if (!(data->start_class->flags & ANYOF_LOCALE)) {
1328 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_SPACE);
1329 for (value = 0; value < 256; value++)
1331 ANYOF_BITMAP_CLEAR(data->start_class, value);
1335 if (data->start_class->flags & ANYOF_LOCALE)
1336 ANYOF_CLASS_SET(data->start_class,ANYOF_NSPACE);
1338 for (value = 0; value < 256; value++)
1339 if (!isSPACE(value))
1340 ANYOF_BITMAP_SET(data->start_class, value);
1345 if (flags & SCF_DO_STCLASS_AND) {
1346 if (data->start_class->flags & ANYOF_LOCALE) {
1347 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_SPACE);
1348 for (value = 0; value < 256; value++)
1349 if (!isSPACE(value))
1350 ANYOF_BITMAP_CLEAR(data->start_class, value);
1354 data->start_class->flags |= ANYOF_LOCALE;
1355 ANYOF_CLASS_SET(data->start_class,ANYOF_NSPACE);
1359 if (flags & SCF_DO_STCLASS_AND) {
1360 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NDIGIT);
1361 for (value = 0; value < 256; value++)
1362 if (!isDIGIT(value))
1363 ANYOF_BITMAP_CLEAR(data->start_class, value);
1366 if (data->start_class->flags & ANYOF_LOCALE)
1367 ANYOF_CLASS_SET(data->start_class,ANYOF_DIGIT);
1369 for (value = 0; value < 256; value++)
1371 ANYOF_BITMAP_SET(data->start_class, value);
1376 if (flags & SCF_DO_STCLASS_AND) {
1377 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_DIGIT);
1378 for (value = 0; value < 256; value++)
1380 ANYOF_BITMAP_CLEAR(data->start_class, value);
1383 if (data->start_class->flags & ANYOF_LOCALE)
1384 ANYOF_CLASS_SET(data->start_class,ANYOF_NDIGIT);
1386 for (value = 0; value < 256; value++)
1387 if (!isDIGIT(value))
1388 ANYOF_BITMAP_SET(data->start_class, value);
1393 if (flags & SCF_DO_STCLASS_OR)
1394 cl_and(data->start_class, &and_with);
1395 flags &= ~SCF_DO_STCLASS;
1398 else if (PL_regkind[(U8)OP(scan)] == EOL && flags & SCF_DO_SUBSTR) {
1399 data->flags |= (OP(scan) == MEOL
1403 else if ( PL_regkind[(U8)OP(scan)] == BRANCHJ
1404 /* Lookbehind, or need to calculate parens/evals/stclass: */
1405 && (scan->flags || data || (flags & SCF_DO_STCLASS))
1406 && (OP(scan) == IFMATCH || OP(scan) == UNLESSM)) {
1407 /* Lookahead/lookbehind */
1408 I32 deltanext, minnext, fake = 0;
1410 struct regnode_charclass_class intrnl;
1413 data_fake.flags = 0;
1415 data_fake.whilem_c = data->whilem_c;
1416 data_fake.last_closep = data->last_closep;
1419 data_fake.last_closep = &fake;
1420 if ( flags & SCF_DO_STCLASS && !scan->flags
1421 && OP(scan) == IFMATCH ) { /* Lookahead */
1422 cl_init(pRExC_state, &intrnl);
1423 data_fake.start_class = &intrnl;
1424 f |= SCF_DO_STCLASS_AND;
1426 if (flags & SCF_WHILEM_VISITED_POS)
1427 f |= SCF_WHILEM_VISITED_POS;
1428 next = regnext(scan);
1429 nscan = NEXTOPER(NEXTOPER(scan));
1430 minnext = study_chunk(pRExC_state, &nscan, &deltanext, last, &data_fake, f);
1433 vFAIL("Variable length lookbehind not implemented");
1435 else if (minnext > U8_MAX) {
1436 vFAIL2("Lookbehind longer than %"UVuf" not implemented", (UV)U8_MAX);
1438 scan->flags = minnext;
1440 if (data && data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
1442 if (data && (data_fake.flags & SF_HAS_EVAL))
1443 data->flags |= SF_HAS_EVAL;
1445 data->whilem_c = data_fake.whilem_c;
1446 if (f & SCF_DO_STCLASS_AND) {
1447 int was = (data->start_class->flags & ANYOF_EOS);
1449 cl_and(data->start_class, &intrnl);
1451 data->start_class->flags |= ANYOF_EOS;
1454 else if (OP(scan) == OPEN) {
1457 else if (OP(scan) == CLOSE) {
1458 if (ARG(scan) == is_par) {
1459 next = regnext(scan);
1461 if ( next && (OP(next) != WHILEM) && next < last)
1462 is_par = 0; /* Disable optimization */
1465 *(data->last_closep) = ARG(scan);
1467 else if (OP(scan) == EVAL) {
1469 data->flags |= SF_HAS_EVAL;
1471 else if (OP(scan) == LOGICAL && scan->flags == 2) { /* Embedded follows */
1472 if (flags & SCF_DO_SUBSTR) {
1473 scan_commit(pRExC_state,data);
1474 data->longest = &(data->longest_float);
1476 is_inf = is_inf_internal = 1;
1477 if (flags & SCF_DO_STCLASS_OR) /* Allow everything */
1478 cl_anything(pRExC_state, data->start_class);
1479 flags &= ~SCF_DO_STCLASS;
1481 /* Else: zero-length, ignore. */
1482 scan = regnext(scan);
1487 *deltap = is_inf_internal ? I32_MAX : delta;
1488 if (flags & SCF_DO_SUBSTR && is_inf)
1489 data->pos_delta = I32_MAX - data->pos_min;
1490 if (is_par > U8_MAX)
1492 if (is_par && pars==1 && data) {
1493 data->flags |= SF_IN_PAR;
1494 data->flags &= ~SF_HAS_PAR;
1496 else if (pars && data) {
1497 data->flags |= SF_HAS_PAR;
1498 data->flags &= ~SF_IN_PAR;
1500 if (flags & SCF_DO_STCLASS_OR)
1501 cl_and(data->start_class, &and_with);
1506 S_add_data(pTHX_ RExC_state_t *pRExC_state, I32 n, char *s)
1508 if (RExC_rx->data) {
1509 Renewc(RExC_rx->data,
1510 sizeof(*RExC_rx->data) + sizeof(void*) * (RExC_rx->data->count + n - 1),
1511 char, struct reg_data);
1512 Renew(RExC_rx->data->what, RExC_rx->data->count + n, U8);
1513 RExC_rx->data->count += n;
1516 Newc(1207, RExC_rx->data, sizeof(*RExC_rx->data) + sizeof(void*) * (n - 1),
1517 char, struct reg_data);
1518 New(1208, RExC_rx->data->what, n, U8);
1519 RExC_rx->data->count = n;
1521 Copy(s, RExC_rx->data->what + RExC_rx->data->count - n, n, U8);
1522 return RExC_rx->data->count - n;
1526 Perl_reginitcolors(pTHX)
1529 char *s = PerlEnv_getenv("PERL_RE_COLORS");
1532 PL_colors[0] = s = savepv(s);
1534 s = strchr(s, '\t');
1540 PL_colors[i] = s = "";
1544 PL_colors[i++] = "";
1551 - pregcomp - compile a regular expression into internal code
1553 * We can't allocate space until we know how big the compiled form will be,
1554 * but we can't compile it (and thus know how big it is) until we've got a
1555 * place to put the code. So we cheat: we compile it twice, once with code
1556 * generation turned off and size counting turned on, and once "for real".
1557 * This also means that we don't allocate space until we are sure that the
1558 * thing really will compile successfully, and we never have to move the
1559 * code and thus invalidate pointers into it. (Note that it has to be in
1560 * one piece because free() must be able to free it all.) [NB: not true in perl]
1562 * Beware that the optimization-preparation code in here knows about some
1563 * of the structure of the compiled regexp. [I'll say.]
1566 Perl_pregcomp(pTHX_ char *exp, char *xend, PMOP *pm)
1576 RExC_state_t RExC_state;
1577 RExC_state_t *pRExC_state = &RExC_state;
1580 FAIL("NULL regexp argument");
1582 /* XXXX This looks very suspicious... */
1583 if (pm->op_pmdynflags & PMdf_UTF8) {
1584 PL_reg_flags |= RF_utf8;
1589 RExC_precomp = savepvn(exp, xend - exp);
1590 DEBUG_r(if (!PL_colorset) reginitcolors());
1591 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sCompiling REx%s `%s%*s%s'\n",
1592 PL_colors[4],PL_colors[5],PL_colors[0],
1593 (int)(xend - exp), RExC_precomp, PL_colors[1]));
1594 RExC_flags16 = pm->op_pmflags;
1598 RExC_seen_zerolen = *exp == '^' ? -1 : 0;
1599 RExC_seen_evals = 0;
1602 /* First pass: determine size, legality. */
1608 RExC_emit = &PL_regdummy;
1609 RExC_whilem_seen = 0;
1610 #if 0 /* REGC() is (currently) a NOP at the first pass.
1611 * Clever compilers notice this and complain. --jhi */
1612 REGC((U8)REG_MAGIC, (char*)RExC_emit);
1614 if (reg(pRExC_state, 0, &flags) == NULL) {
1615 Safefree(RExC_precomp);
1616 RExC_precomp = Nullch;
1619 DEBUG_r(PerlIO_printf(Perl_debug_log, "size %"IVdf" ", (IV)RExC_size));
1621 /* Small enough for pointer-storage convention?
1622 If extralen==0, this means that we will not need long jumps. */
1623 if (RExC_size >= 0x10000L && RExC_extralen)
1624 RExC_size += RExC_extralen;
1627 if (RExC_whilem_seen > 15)
1628 RExC_whilem_seen = 15;
1630 /* Allocate space and initialize. */
1631 Newc(1001, r, sizeof(regexp) + (unsigned)RExC_size * sizeof(regnode),
1634 FAIL("Regexp out of space");
1637 /* avoid reading uninitialized memory in DEBUGGING code in study_chunk() */
1638 Zero(r, sizeof(regexp) + (unsigned)RExC_size * sizeof(regnode), char);
1641 r->prelen = xend - exp;
1642 r->precomp = RExC_precomp;
1644 r->reganch = pm->op_pmflags & PMf_COMPILETIME;
1645 r->nparens = RExC_npar - 1; /* set early to validate backrefs */
1647 r->substrs = 0; /* Useful during FAIL. */
1648 r->startp = 0; /* Useful during FAIL. */
1649 r->endp = 0; /* Useful during FAIL. */
1653 /* Second pass: emit code. */
1658 RExC_emit = r->program;
1659 /* Store the count of eval-groups for security checks: */
1660 RExC_emit->next_off = ((RExC_seen_evals > U16_MAX) ? U16_MAX : RExC_seen_evals);
1661 REGC((U8)REG_MAGIC, (char*) RExC_emit++);
1663 if (reg(pRExC_state, 0, &flags) == NULL)
1666 /* Dig out information for optimizations. */
1667 r->reganch = pm->op_pmflags & PMf_COMPILETIME; /* Again? */
1668 pm->op_pmflags = RExC_flags16;
1670 r->reganch |= ROPT_UTF8;
1671 r->regstclass = NULL;
1672 if (RExC_naughty >= 10) /* Probably an expensive pattern. */
1673 r->reganch |= ROPT_NAUGHTY;
1674 scan = r->program + 1; /* First BRANCH. */
1676 /* XXXX To minimize changes to RE engine we always allocate
1677 3-units-long substrs field. */
1678 Newz(1004, r->substrs, 1, struct reg_substr_data);
1680 StructCopy(&zero_scan_data, &data, scan_data_t);
1681 /* XXXX Should not we check for something else? Usually it is OPEN1... */
1682 if (OP(scan) != BRANCH) { /* Only one top-level choice. */
1684 STRLEN longest_float_length, longest_fixed_length;
1685 struct regnode_charclass_class ch_class;
1690 /* Skip introductions and multiplicators >= 1. */
1691 while ((OP(first) == OPEN && (sawopen = 1)) ||
1692 /* An OR of *one* alternative - should not happen now. */
1693 (OP(first) == BRANCH && OP(regnext(first)) != BRANCH) ||
1694 (OP(first) == PLUS) ||
1695 (OP(first) == MINMOD) ||
1696 /* An {n,m} with n>0 */
1697 (PL_regkind[(U8)OP(first)] == CURLY && ARG1(first) > 0) ) {
1698 if (OP(first) == PLUS)
1701 first += regarglen[(U8)OP(first)];
1702 first = NEXTOPER(first);
1705 /* Starting-point info. */
1707 if (PL_regkind[(U8)OP(first)] == EXACT) {
1708 if (OP(first) == EXACT); /* Empty, get anchored substr later. */
1709 else if ((OP(first) == EXACTF || OP(first) == EXACTFL)
1711 r->regstclass = first;
1713 else if (strchr((char*)PL_simple,OP(first)))
1714 r->regstclass = first;
1715 else if (PL_regkind[(U8)OP(first)] == BOUND ||
1716 PL_regkind[(U8)OP(first)] == NBOUND)
1717 r->regstclass = first;
1718 else if (PL_regkind[(U8)OP(first)] == BOL) {
1719 r->reganch |= (OP(first) == MBOL
1721 : (OP(first) == SBOL
1724 first = NEXTOPER(first);
1727 else if (OP(first) == GPOS) {
1728 r->reganch |= ROPT_ANCH_GPOS;
1729 first = NEXTOPER(first);
1732 else if ((OP(first) == STAR &&
1733 PL_regkind[(U8)OP(NEXTOPER(first))] == REG_ANY) &&
1734 !(r->reganch & ROPT_ANCH) )
1736 /* turn .* into ^.* with an implied $*=1 */
1737 int type = OP(NEXTOPER(first));
1739 if (type == REG_ANY)
1740 type = ROPT_ANCH_MBOL;
1742 type = ROPT_ANCH_SBOL;
1744 r->reganch |= type | ROPT_IMPLICIT;
1745 first = NEXTOPER(first);
1748 if (sawplus && (!sawopen || !RExC_sawback)
1749 && !(RExC_seen & REG_SEEN_EVAL)) /* May examine pos and $& */
1750 /* x+ must match at the 1st pos of run of x's */
1751 r->reganch |= ROPT_SKIP;
1753 /* Scan is after the zeroth branch, first is atomic matcher. */
1754 DEBUG_r(PerlIO_printf(Perl_debug_log, "first at %"IVdf"\n",
1755 (IV)(first - scan + 1)));
1757 * If there's something expensive in the r.e., find the
1758 * longest literal string that must appear and make it the
1759 * regmust. Resolve ties in favor of later strings, since
1760 * the regstart check works with the beginning of the r.e.
1761 * and avoiding duplication strengthens checking. Not a
1762 * strong reason, but sufficient in the absence of others.
1763 * [Now we resolve ties in favor of the earlier string if
1764 * it happens that c_offset_min has been invalidated, since the
1765 * earlier string may buy us something the later one won't.]
1769 data.longest_fixed = newSVpvn("",0);
1770 data.longest_float = newSVpvn("",0);
1771 data.last_found = newSVpvn("",0);
1772 data.longest = &(data.longest_fixed);
1774 if (!r->regstclass) {
1775 cl_init(pRExC_state, &ch_class);
1776 data.start_class = &ch_class;
1777 stclass_flag = SCF_DO_STCLASS_AND;
1778 } else /* XXXX Check for BOUND? */
1780 data.last_closep = &last_close;
1782 minlen = study_chunk(pRExC_state, &first, &fake, scan + RExC_size, /* Up to end */
1783 &data, SCF_DO_SUBSTR | SCF_WHILEM_VISITED_POS | stclass_flag);
1784 if ( RExC_npar == 1 && data.longest == &(data.longest_fixed)
1785 && data.last_start_min == 0 && data.last_end > 0
1786 && !RExC_seen_zerolen
1787 && (!(RExC_seen & REG_SEEN_GPOS) || (r->reganch & ROPT_ANCH_GPOS)))
1788 r->reganch |= ROPT_CHECK_ALL;
1789 scan_commit(pRExC_state, &data);
1790 SvREFCNT_dec(data.last_found);
1792 longest_float_length = CHR_SVLEN(data.longest_float);
1793 if (longest_float_length
1794 || (data.flags & SF_FL_BEFORE_EOL
1795 && (!(data.flags & SF_FL_BEFORE_MEOL)
1796 || (RExC_flags16 & PMf_MULTILINE)))) {
1799 if (SvCUR(data.longest_fixed) /* ok to leave SvCUR */
1800 && data.offset_fixed == data.offset_float_min
1801 && SvCUR(data.longest_fixed) == SvCUR(data.longest_float))
1802 goto remove_float; /* As in (a)+. */
1804 r->float_substr = data.longest_float;
1805 r->float_min_offset = data.offset_float_min;
1806 r->float_max_offset = data.offset_float_max;
1807 t = (data.flags & SF_FL_BEFORE_EOL /* Can't have SEOL and MULTI */
1808 && (!(data.flags & SF_FL_BEFORE_MEOL)
1809 || (RExC_flags16 & PMf_MULTILINE)));
1810 fbm_compile(r->float_substr, t ? FBMcf_TAIL : 0);
1814 r->float_substr = Nullsv;
1815 SvREFCNT_dec(data.longest_float);
1816 longest_float_length = 0;
1819 longest_fixed_length = CHR_SVLEN(data.longest_fixed);
1820 if (longest_fixed_length
1821 || (data.flags & SF_FIX_BEFORE_EOL /* Cannot have SEOL and MULTI */
1822 && (!(data.flags & SF_FIX_BEFORE_MEOL)
1823 || (RExC_flags16 & PMf_MULTILINE)))) {
1826 r->anchored_substr = data.longest_fixed;
1827 r->anchored_offset = data.offset_fixed;
1828 t = (data.flags & SF_FIX_BEFORE_EOL /* Can't have SEOL and MULTI */
1829 && (!(data.flags & SF_FIX_BEFORE_MEOL)
1830 || (RExC_flags16 & PMf_MULTILINE)));
1831 fbm_compile(r->anchored_substr, t ? FBMcf_TAIL : 0);
1834 r->anchored_substr = Nullsv;
1835 SvREFCNT_dec(data.longest_fixed);
1836 longest_fixed_length = 0;
1839 && (OP(r->regstclass) == REG_ANY || OP(r->regstclass) == SANY))
1840 r->regstclass = NULL;
1841 if ((!r->anchored_substr || r->anchored_offset) && stclass_flag
1842 && !(data.start_class->flags & ANYOF_EOS)
1843 && !cl_is_anything(data.start_class)) {
1845 I32 n = add_data(pRExC_state, 1, "f");
1847 New(1006, RExC_rx->data->data[n], 1,
1848 struct regnode_charclass_class);
1849 StructCopy(data.start_class,
1850 (struct regnode_charclass_class*)RExC_rx->data->data[n],
1851 struct regnode_charclass_class);
1852 r->regstclass = (regnode*)RExC_rx->data->data[n];
1853 r->reganch &= ~ROPT_SKIP; /* Used in find_byclass(). */
1854 PL_regdata = r->data; /* for regprop() */
1855 DEBUG_r((sv = sv_newmortal(),
1856 regprop(sv, (regnode*)data.start_class),
1857 PerlIO_printf(Perl_debug_log, "synthetic stclass `%s'.\n",
1861 /* A temporary algorithm prefers floated substr to fixed one to dig more info. */
1862 if (longest_fixed_length > longest_float_length) {
1863 r->check_substr = r->anchored_substr;
1864 r->check_offset_min = r->check_offset_max = r->anchored_offset;
1865 if (r->reganch & ROPT_ANCH_SINGLE)
1866 r->reganch |= ROPT_NOSCAN;
1869 r->check_substr = r->float_substr;
1870 r->check_offset_min = data.offset_float_min;
1871 r->check_offset_max = data.offset_float_max;
1873 /* XXXX Currently intuiting is not compatible with ANCH_GPOS.
1874 This should be changed ASAP! */
1875 if (r->check_substr && !(r->reganch & ROPT_ANCH_GPOS)) {
1876 r->reganch |= RE_USE_INTUIT;
1877 if (SvTAIL(r->check_substr))
1878 r->reganch |= RE_INTUIT_TAIL;
1882 /* Several toplevels. Best we can is to set minlen. */
1884 struct regnode_charclass_class ch_class;
1887 DEBUG_r(PerlIO_printf(Perl_debug_log, "\n"));
1888 scan = r->program + 1;
1889 cl_init(pRExC_state, &ch_class);
1890 data.start_class = &ch_class;
1891 data.last_closep = &last_close;
1892 minlen = study_chunk(pRExC_state, &scan, &fake, scan + RExC_size, &data, SCF_DO_STCLASS_AND|SCF_WHILEM_VISITED_POS);
1893 r->check_substr = r->anchored_substr = r->float_substr = Nullsv;
1894 if (!(data.start_class->flags & ANYOF_EOS)
1895 && !cl_is_anything(data.start_class)) {
1897 I32 n = add_data(pRExC_state, 1, "f");
1899 New(1006, RExC_rx->data->data[n], 1,
1900 struct regnode_charclass_class);
1901 StructCopy(data.start_class,
1902 (struct regnode_charclass_class*)RExC_rx->data->data[n],
1903 struct regnode_charclass_class);
1904 r->regstclass = (regnode*)RExC_rx->data->data[n];
1905 r->reganch &= ~ROPT_SKIP; /* Used in find_byclass(). */
1906 DEBUG_r((sv = sv_newmortal(),
1907 regprop(sv, (regnode*)data.start_class),
1908 PerlIO_printf(Perl_debug_log, "synthetic stclass `%s'.\n",
1914 if (RExC_seen & REG_SEEN_GPOS)
1915 r->reganch |= ROPT_GPOS_SEEN;
1916 if (RExC_seen & REG_SEEN_LOOKBEHIND)
1917 r->reganch |= ROPT_LOOKBEHIND_SEEN;
1918 if (RExC_seen & REG_SEEN_EVAL)
1919 r->reganch |= ROPT_EVAL_SEEN;
1920 Newz(1002, r->startp, RExC_npar, I32);
1921 Newz(1002, r->endp, RExC_npar, I32);
1922 PL_regdata = r->data; /* for regprop() */
1923 DEBUG_r(regdump(r));
1928 - reg - regular expression, i.e. main body or parenthesized thing
1930 * Caller must absorb opening parenthesis.
1932 * Combining parenthesis handling with the base level of regular expression
1933 * is a trifle forced, but the need to tie the tails of the branches to what
1934 * follows makes it hard to avoid.
1937 S_reg(pTHX_ RExC_state_t *pRExC_state, I32 paren, I32 *flagp)
1938 /* paren: Parenthesized? 0=top, 1=(, inside: changed to letter. */
1940 register regnode *ret; /* Will be the head of the group. */
1941 register regnode *br;
1942 register regnode *lastbr;
1943 register regnode *ender = 0;
1944 register I32 parno = 0;
1945 I32 flags, oregflags = RExC_flags16, have_branch = 0, open = 0;
1946 char *oregcomp_parse = RExC_parse;
1949 *flagp = 0; /* Tentatively. */
1951 /* Make an OPEN node, if parenthesized. */
1953 if (*RExC_parse == '?') {
1954 U16 posflags = 0, negflags = 0;
1955 U16 *flagsp = &posflags;
1957 char *seqstart = RExC_parse;
1960 paren = *RExC_parse++;
1961 ret = NULL; /* For look-ahead/behind. */
1964 RExC_seen |= REG_SEEN_LOOKBEHIND;
1965 if (*RExC_parse == '!')
1967 if (*RExC_parse != '=' && *RExC_parse != '!')
1972 RExC_seen_zerolen++;
1978 vFAIL2("Sequence (?%c...) not implemented", (int)paren);
1981 while (*RExC_parse && *RExC_parse != ')')
1983 if (*RExC_parse != ')')
1984 FAIL("Sequence (?#... not terminated");
1985 nextchar(pRExC_state);
1990 vWARN(RExC_parse, "(?p{}) is deprecated - use (??{})");
1994 paren = *RExC_parse++;
1998 I32 count = 1, n = 0;
2000 char *s = RExC_parse;
2002 OP_4tree *sop, *rop;
2004 RExC_seen_zerolen++;
2005 RExC_seen |= REG_SEEN_EVAL;
2006 while (count && (c = *RExC_parse)) {
2007 if (c == '\\' && RExC_parse[1])
2015 if (*RExC_parse != ')')
2018 vFAIL("Sequence (?{...}) not terminated or not {}-balanced");
2023 if (RExC_parse - 1 - s)
2024 sv = newSVpvn(s, RExC_parse - 1 - s);
2026 sv = newSVpvn("", 0);
2029 Perl_save_re_context(aTHX);
2030 rop = sv_compile_2op(sv, &sop, "re", &av);
2033 n = add_data(pRExC_state, 3, "nop");
2034 RExC_rx->data->data[n] = (void*)rop;
2035 RExC_rx->data->data[n+1] = (void*)sop;
2036 RExC_rx->data->data[n+2] = (void*)av;
2039 else { /* First pass */
2040 if (PL_reginterp_cnt < ++RExC_seen_evals
2041 && PL_curcop != &PL_compiling)
2042 /* No compiled RE interpolated, has runtime
2043 components ===> unsafe. */
2044 FAIL("Eval-group not allowed at runtime, use re 'eval'");
2046 FAIL("Eval-group in insecure regular expression");
2049 nextchar(pRExC_state);
2051 ret = reg_node(pRExC_state, LOGICAL);
2054 regtail(pRExC_state, ret, reganode(pRExC_state, EVAL, n));
2057 return reganode(pRExC_state, EVAL, n);
2061 if (RExC_parse[0] == '?') {
2062 if (RExC_parse[1] == '=' || RExC_parse[1] == '!'
2063 || RExC_parse[1] == '<'
2064 || RExC_parse[1] == '{') { /* Lookahead or eval. */
2067 ret = reg_node(pRExC_state, LOGICAL);
2070 regtail(pRExC_state, ret, reg(pRExC_state, 1, &flag));
2074 else if (RExC_parse[0] >= '1' && RExC_parse[0] <= '9' ) {
2075 parno = atoi(RExC_parse++);
2077 while (isDIGIT(*RExC_parse))
2079 ret = reganode(pRExC_state, GROUPP, parno);
2080 if ((c = *nextchar(pRExC_state)) != ')')
2081 vFAIL("Switch condition not recognized");
2083 regtail(pRExC_state, ret, reganode(pRExC_state, IFTHEN, 0));
2084 br = regbranch(pRExC_state, &flags, 1);
2086 br = reganode(pRExC_state, LONGJMP, 0);
2088 regtail(pRExC_state, br, reganode(pRExC_state, LONGJMP, 0));
2089 c = *nextchar(pRExC_state);
2093 lastbr = reganode(pRExC_state, IFTHEN, 0); /* Fake one for optimizer. */
2094 regbranch(pRExC_state, &flags, 1);
2095 regtail(pRExC_state, ret, lastbr);
2098 c = *nextchar(pRExC_state);
2103 vFAIL("Switch (?(condition)... contains too many branches");
2104 ender = reg_node(pRExC_state, TAIL);
2105 regtail(pRExC_state, br, ender);
2107 regtail(pRExC_state, lastbr, ender);
2108 regtail(pRExC_state, NEXTOPER(NEXTOPER(lastbr)), ender);
2111 regtail(pRExC_state, ret, ender);
2115 vFAIL2("Unknown switch condition (?(%.2s", RExC_parse);
2119 RExC_parse--; /* for vFAIL to print correctly */
2120 vFAIL("Sequence (? incomplete");
2125 while (*RExC_parse && strchr("iogcmsx", *RExC_parse)) {
2126 if (*RExC_parse != 'o')
2127 pmflag(flagsp, *RExC_parse);
2130 if (*RExC_parse == '-') {
2135 RExC_flags16 |= posflags;
2136 RExC_flags16 &= ~negflags;
2137 if (*RExC_parse == ':') {
2143 if (*RExC_parse != ')') {
2145 vFAIL3("Sequence (%.*s...) not recognized", RExC_parse-seqstart, seqstart);
2147 nextchar(pRExC_state);
2155 ret = reganode(pRExC_state, OPEN, parno);
2162 /* Pick up the branches, linking them together. */
2163 br = regbranch(pRExC_state, &flags, 1);
2166 if (*RExC_parse == '|') {
2167 if (!SIZE_ONLY && RExC_extralen) {
2168 reginsert(pRExC_state, BRANCHJ, br);
2171 reginsert(pRExC_state, BRANCH, br);
2174 RExC_extralen += 1; /* For BRANCHJ-BRANCH. */
2176 else if (paren == ':') {
2177 *flagp |= flags&SIMPLE;
2179 if (open) { /* Starts with OPEN. */
2180 regtail(pRExC_state, ret, br); /* OPEN -> first. */
2182 else if (paren != '?') /* Not Conditional */
2186 *flagp |= flags&SPSTART;
2188 while (*RExC_parse == '|') {
2189 if (!SIZE_ONLY && RExC_extralen) {
2190 ender = reganode(pRExC_state, LONGJMP,0);
2191 regtail(pRExC_state, NEXTOPER(NEXTOPER(lastbr)), ender); /* Append to the previous. */
2194 RExC_extralen += 2; /* Account for LONGJMP. */
2195 nextchar(pRExC_state);
2196 br = regbranch(pRExC_state, &flags, 0);
2199 regtail(pRExC_state, lastbr, br); /* BRANCH -> BRANCH. */
2203 *flagp |= flags&SPSTART;
2206 if (have_branch || paren != ':') {
2207 /* Make a closing node, and hook it on the end. */
2210 ender = reg_node(pRExC_state, TAIL);
2213 ender = reganode(pRExC_state, CLOSE, parno);
2219 *flagp &= ~HASWIDTH;
2222 ender = reg_node(pRExC_state, SUCCEED);
2225 ender = reg_node(pRExC_state, END);
2228 regtail(pRExC_state, lastbr, ender);
2231 /* Hook the tails of the branches to the closing node. */
2232 for (br = ret; br != NULL; br = regnext(br)) {
2233 regoptail(pRExC_state, br, ender);
2240 static char parens[] = "=!<,>";
2242 if (paren && (p = strchr(parens, paren))) {
2243 int node = ((p - parens) % 2) ? UNLESSM : IFMATCH;
2244 int flag = (p - parens) > 1;
2247 node = SUSPEND, flag = 0;
2248 reginsert(pRExC_state, node,ret);
2250 regtail(pRExC_state, ret, reg_node(pRExC_state, TAIL));
2254 /* Check for proper termination. */
2256 RExC_flags16 = oregflags;
2257 if (RExC_parse >= RExC_end || *nextchar(pRExC_state) != ')') {
2258 RExC_parse = oregcomp_parse;
2259 vFAIL("Unmatched (");
2262 else if (!paren && RExC_parse < RExC_end) {
2263 if (*RExC_parse == ')') {
2265 vFAIL("Unmatched )");
2268 FAIL("Junk on end of regexp"); /* "Can't happen". */
2276 - regbranch - one alternative of an | operator
2278 * Implements the concatenation operator.
2281 S_regbranch(pTHX_ RExC_state_t *pRExC_state, I32 *flagp, I32 first)
2283 register regnode *ret;
2284 register regnode *chain = NULL;
2285 register regnode *latest;
2286 I32 flags = 0, c = 0;
2291 if (!SIZE_ONLY && RExC_extralen)
2292 ret = reganode(pRExC_state, BRANCHJ,0);
2294 ret = reg_node(pRExC_state, BRANCH);
2297 if (!first && SIZE_ONLY)
2298 RExC_extralen += 1; /* BRANCHJ */
2300 *flagp = WORST; /* Tentatively. */
2303 nextchar(pRExC_state);
2304 while (RExC_parse < RExC_end && *RExC_parse != '|' && *RExC_parse != ')') {
2306 latest = regpiece(pRExC_state, &flags);
2307 if (latest == NULL) {
2308 if (flags & TRYAGAIN)
2312 else if (ret == NULL)
2314 *flagp |= flags&HASWIDTH;
2315 if (chain == NULL) /* First piece. */
2316 *flagp |= flags&SPSTART;
2319 regtail(pRExC_state, chain, latest);
2324 if (chain == NULL) { /* Loop ran zero times. */
2325 chain = reg_node(pRExC_state, NOTHING);
2330 *flagp |= flags&SIMPLE;
2337 - regpiece - something followed by possible [*+?]
2339 * Note that the branching code sequences used for ? and the general cases
2340 * of * and + are somewhat optimized: they use the same NOTHING node as
2341 * both the endmarker for their branch list and the body of the last branch.
2342 * It might seem that this node could be dispensed with entirely, but the
2343 * endmarker role is not redundant.
2346 S_regpiece(pTHX_ RExC_state_t *pRExC_state, I32 *flagp)
2348 register regnode *ret;
2350 register char *next;
2352 char *origparse = RExC_parse;
2355 I32 max = REG_INFTY;
2357 ret = regatom(pRExC_state, &flags);
2359 if (flags & TRYAGAIN)
2366 if (op == '{' && regcurly(RExC_parse)) {
2367 next = RExC_parse + 1;
2369 while (isDIGIT(*next) || *next == ',') {
2378 if (*next == '}') { /* got one */
2382 min = atoi(RExC_parse);
2386 maxpos = RExC_parse;
2388 if (!max && *maxpos != '0')
2389 max = REG_INFTY; /* meaning "infinity" */
2390 else if (max >= REG_INFTY)
2391 vFAIL2("Quantifier in {,} bigger than %d", REG_INFTY - 1);
2393 nextchar(pRExC_state);
2396 if ((flags&SIMPLE)) {
2397 RExC_naughty += 2 + RExC_naughty / 2;
2398 reginsert(pRExC_state, CURLY, ret);
2401 regnode *w = reg_node(pRExC_state, WHILEM);
2404 regtail(pRExC_state, ret, w);
2405 if (!SIZE_ONLY && RExC_extralen) {
2406 reginsert(pRExC_state, LONGJMP,ret);
2407 reginsert(pRExC_state, NOTHING,ret);
2408 NEXT_OFF(ret) = 3; /* Go over LONGJMP. */
2410 reginsert(pRExC_state, CURLYX,ret);
2411 if (!SIZE_ONLY && RExC_extralen)
2412 NEXT_OFF(ret) = 3; /* Go over NOTHING to LONGJMP. */
2413 regtail(pRExC_state, ret, reg_node(pRExC_state, NOTHING));
2415 RExC_whilem_seen++, RExC_extralen += 3;
2416 RExC_naughty += 4 + RExC_naughty; /* compound interest */
2424 if (max && max < min)
2425 vFAIL("Can't do {n,m} with n > m");
2440 #if 0 /* Now runtime fix should be reliable. */
2442 /* if this is reinstated, don't forget to put this back into perldiag:
2444 =item Regexp *+ operand could be empty at {#} in regex m/%s/
2446 (F) The part of the regexp subject to either the * or + quantifier
2447 could match an empty string. The {#} shows in the regular
2448 expression about where the problem was discovered.
2452 if (!(flags&HASWIDTH) && op != '?')
2453 vFAIL("Regexp *+ operand could be empty");
2456 nextchar(pRExC_state);
2458 *flagp = (op != '+') ? (WORST|SPSTART|HASWIDTH) : (WORST|HASWIDTH);
2460 if (op == '*' && (flags&SIMPLE)) {
2461 reginsert(pRExC_state, STAR, ret);
2465 else if (op == '*') {
2469 else if (op == '+' && (flags&SIMPLE)) {
2470 reginsert(pRExC_state, PLUS, ret);
2474 else if (op == '+') {
2478 else if (op == '?') {
2483 if (ckWARN(WARN_REGEXP) && !SIZE_ONLY && !(flags&HASWIDTH) && max > REG_INFTY/3) {
2485 "%.*s matches null string many times",
2486 RExC_parse - origparse,
2490 if (*RExC_parse == '?') {
2491 nextchar(pRExC_state);
2492 reginsert(pRExC_state, MINMOD, ret);
2493 regtail(pRExC_state, ret, ret + NODE_STEP_REGNODE);
2495 if (ISMULT2(RExC_parse)) {
2497 vFAIL("Nested quantifiers");
2504 - regatom - the lowest level
2506 * Optimization: gobbles an entire sequence of ordinary characters so that
2507 * it can turn them into a single node, which is smaller to store and
2508 * faster to run. Backslashed characters are exceptions, each becoming a
2509 * separate node; the code is simpler that way and it's not worth fixing.
2511 * [Yes, it is worth fixing, some scripts can run twice the speed.] */
2513 S_regatom(pTHX_ RExC_state_t *pRExC_state, I32 *flagp)
2515 register regnode *ret = 0;
2518 *flagp = WORST; /* Tentatively. */
2521 switch (*RExC_parse) {
2523 RExC_seen_zerolen++;
2524 nextchar(pRExC_state);
2525 if (RExC_flags16 & PMf_MULTILINE)
2526 ret = reg_node(pRExC_state, MBOL);
2527 else if (RExC_flags16 & PMf_SINGLELINE)
2528 ret = reg_node(pRExC_state, SBOL);
2530 ret = reg_node(pRExC_state, BOL);
2533 nextchar(pRExC_state);
2535 RExC_seen_zerolen++;
2536 if (RExC_flags16 & PMf_MULTILINE)
2537 ret = reg_node(pRExC_state, MEOL);
2538 else if (RExC_flags16 & PMf_SINGLELINE)
2539 ret = reg_node(pRExC_state, SEOL);
2541 ret = reg_node(pRExC_state, EOL);
2544 nextchar(pRExC_state);
2545 if (RExC_flags16 & PMf_SINGLELINE)
2546 ret = reg_node(pRExC_state, SANY);
2548 ret = reg_node(pRExC_state, REG_ANY);
2549 *flagp |= HASWIDTH|SIMPLE;
2554 char *oregcomp_parse = ++RExC_parse;
2555 ret = regclass(pRExC_state);
2556 if (*RExC_parse != ']') {
2557 RExC_parse = oregcomp_parse;
2558 vFAIL("Unmatched [");
2560 nextchar(pRExC_state);
2561 *flagp |= HASWIDTH|SIMPLE;
2565 nextchar(pRExC_state);
2566 ret = reg(pRExC_state, 1, &flags);
2568 if (flags & TRYAGAIN) {
2569 if (RExC_parse == RExC_end) {
2570 /* Make parent create an empty node if needed. */
2578 *flagp |= flags&(HASWIDTH|SPSTART|SIMPLE);
2582 if (flags & TRYAGAIN) {
2586 vFAIL("Internal urp");
2587 /* Supposed to be caught earlier. */
2590 if (!regcurly(RExC_parse)) {
2599 vFAIL("Quantifier follows nothing");
2602 switch (*++RExC_parse) {
2604 RExC_seen_zerolen++;
2605 ret = reg_node(pRExC_state, SBOL);
2607 nextchar(pRExC_state);
2610 ret = reg_node(pRExC_state, GPOS);
2611 RExC_seen |= REG_SEEN_GPOS;
2613 nextchar(pRExC_state);
2616 ret = reg_node(pRExC_state, SEOL);
2618 nextchar(pRExC_state);
2621 ret = reg_node(pRExC_state, EOS);
2623 RExC_seen_zerolen++; /* Do not optimize RE away */
2624 nextchar(pRExC_state);
2627 ret = reg_node(pRExC_state, SANY);
2628 *flagp |= HASWIDTH|SIMPLE;
2629 nextchar(pRExC_state);
2632 ret = reg_node(pRExC_state, CLUMP);
2634 nextchar(pRExC_state);
2635 if (UTF && !PL_utf8_mark)
2636 is_utf8_mark((U8*)"~"); /* preload table */
2639 ret = reg_node(pRExC_state, LOC ? ALNUML : ALNUM);
2640 *flagp |= HASWIDTH|SIMPLE;
2641 nextchar(pRExC_state);
2642 if (UTF && !PL_utf8_alnum)
2643 is_utf8_alnum((U8*)"a"); /* preload table */
2646 ret = reg_node(pRExC_state, LOC ? NALNUML : NALNUM);
2647 *flagp |= HASWIDTH|SIMPLE;
2648 nextchar(pRExC_state);
2649 if (UTF && !PL_utf8_alnum)
2650 is_utf8_alnum((U8*)"a"); /* preload table */
2653 RExC_seen_zerolen++;
2654 RExC_seen |= REG_SEEN_LOOKBEHIND;
2655 ret = reg_node(pRExC_state, LOC ? BOUNDL : BOUND);
2657 nextchar(pRExC_state);
2658 if (UTF && !PL_utf8_alnum)
2659 is_utf8_alnum((U8*)"a"); /* preload table */
2662 RExC_seen_zerolen++;
2663 RExC_seen |= REG_SEEN_LOOKBEHIND;
2664 ret = reg_node(pRExC_state, LOC ? NBOUNDL : NBOUND);
2666 nextchar(pRExC_state);
2667 if (UTF && !PL_utf8_alnum)
2668 is_utf8_alnum((U8*)"a"); /* preload table */
2671 ret = reg_node(pRExC_state, LOC ? SPACEL : SPACE);
2672 *flagp |= HASWIDTH|SIMPLE;
2673 nextchar(pRExC_state);
2674 if (UTF && !PL_utf8_space)
2675 is_utf8_space((U8*)" "); /* preload table */
2678 ret = reg_node(pRExC_state, LOC ? NSPACEL : NSPACE);
2679 *flagp |= HASWIDTH|SIMPLE;
2680 nextchar(pRExC_state);
2681 if (UTF && !PL_utf8_space)
2682 is_utf8_space((U8*)" "); /* preload table */
2685 ret = reg_node(pRExC_state, DIGIT);
2686 *flagp |= HASWIDTH|SIMPLE;
2687 nextchar(pRExC_state);
2688 if (UTF && !PL_utf8_digit)
2689 is_utf8_digit((U8*)"1"); /* preload table */
2692 ret = reg_node(pRExC_state, NDIGIT);
2693 *flagp |= HASWIDTH|SIMPLE;
2694 nextchar(pRExC_state);
2695 if (UTF && !PL_utf8_digit)
2696 is_utf8_digit((U8*)"1"); /* preload table */
2700 { /* a lovely hack--pretend we saw [\pX] instead */
2701 char* oldregxend = RExC_end;
2703 if (RExC_parse[1] == '{') {
2704 RExC_end = strchr(RExC_parse, '}');
2707 RExC_end = oldregxend;
2708 vFAIL("Missing right brace on \\p{}");
2713 RExC_end = RExC_parse + 2;
2716 ret = regclass(pRExC_state);
2718 RExC_end = oldregxend;
2720 nextchar(pRExC_state);
2721 *flagp |= HASWIDTH|SIMPLE;
2734 case '1': case '2': case '3': case '4':
2735 case '5': case '6': case '7': case '8': case '9':
2737 I32 num = atoi(RExC_parse);
2739 if (num > 9 && num >= RExC_npar)
2742 while (isDIGIT(*RExC_parse))
2745 if (!SIZE_ONLY && num > RExC_rx->nparens)
2746 vFAIL("Reference to nonexistent group");
2748 ret = reganode(pRExC_state, FOLD
2749 ? (LOC ? REFFL : REFF)
2753 nextchar(pRExC_state);
2758 if (RExC_parse >= RExC_end)
2759 FAIL("Trailing \\");
2762 /* Do not generate `unrecognized' warnings here, we fall
2763 back into the quick-grab loop below */
2769 if (RExC_flags16 & PMf_EXTENDED) {
2770 while (RExC_parse < RExC_end && *RExC_parse != '\n') RExC_parse++;
2771 if (RExC_parse < RExC_end)
2777 register STRLEN len;
2786 ret = reg_node(pRExC_state, FOLD
2787 ? (LOC ? EXACTFL : EXACTF)
2790 for (len = 0, p = RExC_parse - 1;
2791 len < 127 && p < RExC_end;
2796 if (RExC_flags16 & PMf_EXTENDED)
2797 p = regwhite(p, RExC_end);
2859 char* e = strchr(p, '}');
2863 vFAIL("Missing right brace on \\x{}");
2866 numlen = 1; /* allow underscores */
2867 ender = (UV)scan_hex(p + 1, e - p - 1, &numlen);
2868 /* numlen is generous */
2869 if (numlen + len >= 127) {
2877 numlen = 0; /* disallow underscores */
2878 ender = (UV)scan_hex(p, 2, &numlen);
2884 ender = UCHARAT(p++);
2885 ender = toCTRL(ender);
2887 case '0': case '1': case '2': case '3':case '4':
2888 case '5': case '6': case '7': case '8':case '9':
2890 (isDIGIT(p[1]) && atoi(p) >= RExC_npar) ) {
2891 numlen = 0; /* disallow underscores */
2892 ender = (UV)scan_oct(p, 3, &numlen);
2902 FAIL("Trailing \\");
2905 if (!SIZE_ONLY && ckWARN(WARN_REGEXP) && isALPHA(*p))
2906 vWARN2(p +1, "Unrecognized escape \\%c passed through", *p);
2907 goto normal_default;
2912 if ((*p & 0xc0) == 0xc0 && UTF) {
2913 ender = utf8_to_uv((U8*)p, RExC_end - p,
2921 if (RExC_flags16 & PMf_EXTENDED)
2922 p = regwhite(p, RExC_end);
2925 ender = toLOWER_LC_uni(ender);
2927 ender = toLOWER_uni(ender);
2929 if (ISMULT2(p)) { /* Back off on ?+*. */
2932 else if (ender >= 0x80 && UTF) {
2933 reguni(pRExC_state, ender, s, &numlen);
2943 if (ender >= 0x80 && UTF) {
2944 reguni(pRExC_state, ender, s, &numlen);
2953 nextchar(pRExC_state);
2955 /* len is STRLEN which is unsigned, need to copy to signed */
2958 vFAIL("Internal disaster");
2967 RExC_size += STR_SZ(len);
2969 RExC_emit += STR_SZ(len);
2978 S_regwhite(pTHX_ char *p, char *e)
2983 else if (*p == '#') {
2986 } while (p < e && *p != '\n');
2994 /* Parse POSIX character classes: [[:foo:]], [[=foo=]], [[.foo.]].
2995 Character classes ([:foo:]) can also be negated ([:^foo:]).
2996 Returns a named class id (ANYOF_XXX) if successful, -1 otherwise.
2997 Equivalence classes ([=foo=]) and composites ([.foo.]) are parsed,
2998 but trigger warnings because they are currently unimplemented. */
3000 S_regpposixcc(pTHX_ RExC_state_t *pRExC_state, I32 value)
3003 I32 namedclass = OOB_NAMEDCLASS;
3005 if (value == '[' && RExC_parse + 1 < RExC_end &&
3006 /* I smell either [: or [= or [. -- POSIX has been here, right? */
3007 (*RExC_parse == ':' ||
3008 *RExC_parse == '=' ||
3009 *RExC_parse == '.')) {
3010 char c = *RExC_parse;
3011 char* s = RExC_parse++;
3013 while (RExC_parse < RExC_end && *RExC_parse != c)
3015 if (RExC_parse == RExC_end)
3016 /* Grandfather lone [:, [=, [. */
3019 char* t = RExC_parse++; /* skip over the c */
3021 if (*RExC_parse == ']') {
3022 RExC_parse++; /* skip over the ending ] */
3025 I32 complement = *posixcc == '^' ? *posixcc++ : 0;
3026 I32 skip = 5; /* the most common skip */
3030 if (strnEQ(posixcc, "alnum", 5))
3032 complement ? ANYOF_NALNUMC : ANYOF_ALNUMC;
3033 else if (strnEQ(posixcc, "alpha", 5))
3035 complement ? ANYOF_NALPHA : ANYOF_ALPHA;
3036 else if (strnEQ(posixcc, "ascii", 5))
3038 complement ? ANYOF_NASCII : ANYOF_ASCII;
3041 if (strnEQ(posixcc, "blank", 5))
3043 complement ? ANYOF_NBLANK : ANYOF_BLANK;
3046 if (strnEQ(posixcc, "cntrl", 5))
3048 complement ? ANYOF_NCNTRL : ANYOF_CNTRL;
3051 if (strnEQ(posixcc, "digit", 5))
3053 complement ? ANYOF_NDIGIT : ANYOF_DIGIT;
3056 if (strnEQ(posixcc, "graph", 5))
3058 complement ? ANYOF_NGRAPH : ANYOF_GRAPH;
3061 if (strnEQ(posixcc, "lower", 5))
3063 complement ? ANYOF_NLOWER : ANYOF_LOWER;
3066 if (strnEQ(posixcc, "print", 5))
3068 complement ? ANYOF_NPRINT : ANYOF_PRINT;
3069 else if (strnEQ(posixcc, "punct", 5))
3071 complement ? ANYOF_NPUNCT : ANYOF_PUNCT;
3074 if (strnEQ(posixcc, "space", 5))
3076 complement ? ANYOF_NPSXSPC : ANYOF_PSXSPC;
3079 if (strnEQ(posixcc, "upper", 5))
3081 complement ? ANYOF_NUPPER : ANYOF_UPPER;
3083 case 'w': /* this is not POSIX, this is the Perl \w */
3084 if (strnEQ(posixcc, "word", 4)) {
3086 complement ? ANYOF_NALNUM : ANYOF_ALNUM;
3091 if (strnEQ(posixcc, "xdigit", 6)) {
3093 complement ? ANYOF_NXDIGIT : ANYOF_XDIGIT;
3098 if (namedclass == OOB_NAMEDCLASS ||
3099 posixcc[skip] != ':' ||
3100 posixcc[skip+1] != ']')
3102 Simple_vFAIL3("POSIX class [:%.*s:] unknown",
3105 } else if (!SIZE_ONLY) {
3106 /* [[=foo=]] and [[.foo.]] are still future. */
3108 /* adjust RExC_parse so the warning shows after
3110 while (*RExC_parse && *RExC_parse != ']')
3112 Simple_vFAIL3("POSIX syntax [%c %c] is reserved for future extensions", c, c);
3115 /* Maternal grandfather:
3116 * "[:" ending in ":" but not in ":]" */
3126 S_checkposixcc(pTHX_ RExC_state_t *pRExC_state)
3128 if (!SIZE_ONLY && ckWARN(WARN_REGEXP) &&
3129 (*RExC_parse == ':' ||
3130 *RExC_parse == '=' ||
3131 *RExC_parse == '.')) {
3132 char *s = RExC_parse;
3135 while(*s && isALNUM(*s))
3137 if (*s && c == *s && s[1] == ']') {
3138 vWARN3(s+2, "POSIX syntax [%c %c] belongs inside character classes", c, c);
3140 /* [[=foo=]] and [[.foo.]] are still future. */
3141 if (c == '=' || c == '.')
3143 /* adjust RExC_parse so the error shows after
3145 while (*RExC_parse && *RExC_parse++ != ']')
3147 Simple_vFAIL3("POSIX syntax [%c %c] is reserved for future extensions", c, c);
3154 S_regclass(pTHX_ RExC_state_t *pRExC_state)
3157 register IV lastvalue = OOB_UNICODE;
3158 register IV range = 0;
3159 register regnode *ret;
3163 bool need_class = 0;
3168 ret = reganode(pRExC_state, ANYOF, 0);
3171 ANYOF_FLAGS(ret) = 0;
3173 if (*RExC_parse == '^') { /* Complement of range. */
3177 ANYOF_FLAGS(ret) |= ANYOF_INVERT;
3181 RExC_size += ANYOF_SKIP;
3183 RExC_emit += ANYOF_SKIP;
3185 ANYOF_FLAGS(ret) |= ANYOF_FOLD;
3187 ANYOF_FLAGS(ret) |= ANYOF_LOCALE;
3188 ANYOF_BITMAP_ZERO(ret);
3189 listsv = newSVpvn("# comment\n", 10);
3192 if (!SIZE_ONLY && ckWARN(WARN_REGEXP))
3193 checkposixcc(pRExC_state);
3195 if (*RExC_parse == ']' || *RExC_parse == '-')
3196 goto charclassloop; /* allow 1st char to be ] or - */
3198 while (RExC_parse < RExC_end && *RExC_parse != ']') {
3202 namedclass = OOB_NAMEDCLASS; /* initialize as illegal */
3205 rangebegin = RExC_parse;
3207 value = utf8_to_uv((U8*)RExC_parse,
3208 RExC_end - RExC_parse,
3210 RExC_parse += numlen;
3213 value = UCHARAT(RExC_parse++);
3215 namedclass = regpposixcc(pRExC_state, value);
3216 else if (value == '\\') {
3218 value = utf8_to_uv((U8*)RExC_parse,
3219 RExC_end - RExC_parse,
3221 RExC_parse += numlen;
3224 value = UCHARAT(RExC_parse++);
3225 /* Some compilers cannot handle switching on 64-bit integer
3226 * values, therefore value cannot be an UV. Yes, this will
3227 * be a problem later if we want switch on Unicode.
3228 * A similar issue a little bit later when switching on
3229 * namedclass. --jhi */
3230 switch ((I32)value) {
3231 case 'w': namedclass = ANYOF_ALNUM; break;
3232 case 'W': namedclass = ANYOF_NALNUM; break;
3233 case 's': namedclass = ANYOF_SPACE; break;
3234 case 'S': namedclass = ANYOF_NSPACE; break;
3235 case 'd': namedclass = ANYOF_DIGIT; break;
3236 case 'D': namedclass = ANYOF_NDIGIT; break;
3239 if (*RExC_parse == '{') {
3240 e = strchr(RExC_parse++, '}');
3242 vFAIL("Missing right brace on \\p{}");
3251 Perl_sv_catpvf(aTHX_ listsv,
3252 "+utf8::%.*s\n", (int)n, RExC_parse);
3254 Perl_sv_catpvf(aTHX_ listsv,
3255 "!utf8::%.*s\n", (int)n, RExC_parse);
3258 ANYOF_FLAGS(ret) |= ANYOF_UNICODE;
3260 case 'n': value = '\n'; break;
3261 case 'r': value = '\r'; break;
3262 case 't': value = '\t'; break;
3263 case 'f': value = '\f'; break;
3264 case 'b': value = '\b'; break;
3266 case 'e': value = '\033'; break;
3267 case 'a': value = '\007'; break;
3269 case 'e': value = '\047'; break;
3270 case 'a': value = '\057'; break;
3273 if (*RExC_parse == '{') {
3274 e = strchr(RExC_parse++, '}');
3276 vFAIL("Missing right brace on \\x{}");
3277 numlen = 1; /* allow underscores */
3278 value = (UV)scan_hex(RExC_parse,
3284 numlen = 0; /* disallow underscores */
3285 value = (UV)scan_hex(RExC_parse, 2, &numlen);
3286 RExC_parse += numlen;
3290 value = UCHARAT(RExC_parse++);
3291 value = toCTRL(value);
3293 case '0': case '1': case '2': case '3': case '4':
3294 case '5': case '6': case '7': case '8': case '9':
3295 numlen = 0; /* disallow underscores */
3296 value = (UV)scan_oct(--RExC_parse, 3, &numlen);
3297 RExC_parse += numlen;
3300 if (!SIZE_ONLY && ckWARN(WARN_REGEXP) && isALPHA(value))
3302 "Unrecognized escape \\%c in character class passed through",
3306 } /* end of \blah */
3308 if (namedclass > OOB_NAMEDCLASS) { /* this is a named class \blah */
3310 if (!SIZE_ONLY && !need_class)
3311 ANYOF_CLASS_ZERO(ret);
3315 /* a bad range like a-\d, a-[:digit:] ? */
3318 if (ckWARN(WARN_REGEXP))
3320 "False [] range \"%*.*s\"",
3321 RExC_parse - rangebegin,
3322 RExC_parse - rangebegin,
3324 if (lastvalue < 256) {
3325 ANYOF_BITMAP_SET(ret, lastvalue);
3326 ANYOF_BITMAP_SET(ret, '-');
3329 ANYOF_FLAGS(ret) |= ANYOF_UNICODE;
3330 Perl_sv_catpvf(aTHX_ listsv,
3331 /* 0x002D is Unicode for '-' */
3332 "%04"UVxf"\n002D\n", (UV)lastvalue);
3336 range = 0; /* this was not a true range */
3340 /* Possible truncation here but in some 64-bit environments
3341 * the compiler gets heartburn about switch on 64-bit values.
3342 * A similar issue a little earlier when switching on value.
3344 switch ((I32)namedclass) {
3347 ANYOF_CLASS_SET(ret, ANYOF_ALNUM);
3349 for (value = 0; value < 256; value++)
3351 ANYOF_BITMAP_SET(ret, value);
3353 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsWord\n");
3357 ANYOF_CLASS_SET(ret, ANYOF_NALNUM);
3359 for (value = 0; value < 256; value++)
3360 if (!isALNUM(value))
3361 ANYOF_BITMAP_SET(ret, value);
3363 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsWord\n");
3367 ANYOF_CLASS_SET(ret, ANYOF_ALNUMC);
3369 for (value = 0; value < 256; value++)
3370 if (isALNUMC(value))
3371 ANYOF_BITMAP_SET(ret, value);
3373 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsAlnum\n");
3377 ANYOF_CLASS_SET(ret, ANYOF_NALNUMC);
3379 for (value = 0; value < 256; value++)
3380 if (!isALNUMC(value))
3381 ANYOF_BITMAP_SET(ret, value);
3383 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsAlnum\n");
3387 ANYOF_CLASS_SET(ret, ANYOF_ALPHA);
3389 for (value = 0; value < 256; value++)
3391 ANYOF_BITMAP_SET(ret, value);
3393 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsAlpha\n");
3397 ANYOF_CLASS_SET(ret, ANYOF_NALPHA);
3399 for (value = 0; value < 256; value++)
3400 if (!isALPHA(value))
3401 ANYOF_BITMAP_SET(ret, value);
3403 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsAlpha\n");
3407 ANYOF_CLASS_SET(ret, ANYOF_ASCII);
3410 for (value = 0; value < 128; value++)
3411 ANYOF_BITMAP_SET(ret, value);
3413 for (value = 0; value < 256; value++)
3415 ANYOF_BITMAP_SET(ret, value);
3418 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsASCII\n");
3422 ANYOF_CLASS_SET(ret, ANYOF_NASCII);
3425 for (value = 128; value < 256; value++)
3426 ANYOF_BITMAP_SET(ret, value);
3428 for (value = 0; value < 256; value++)
3429 if (!isASCII(value))
3430 ANYOF_BITMAP_SET(ret, value);
3433 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsASCII\n");
3437 ANYOF_CLASS_SET(ret, ANYOF_BLANK);
3439 for (value = 0; value < 256; value++)
3441 ANYOF_BITMAP_SET(ret, value);
3443 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsBlank\n");
3447 ANYOF_CLASS_SET(ret, ANYOF_NBLANK);
3449 for (value = 0; value < 256; value++)
3450 if (!isBLANK(value))
3451 ANYOF_BITMAP_SET(ret, value);
3453 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsBlank\n");
3457 ANYOF_CLASS_SET(ret, ANYOF_CNTRL);
3459 for (value = 0; value < 256; value++)
3461 ANYOF_BITMAP_SET(ret, value);
3463 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsCntrl\n");
3467 ANYOF_CLASS_SET(ret, ANYOF_NCNTRL);
3469 for (value = 0; value < 256; value++)
3470 if (!isCNTRL(value))
3471 ANYOF_BITMAP_SET(ret, value);
3473 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsCntrl\n");
3477 ANYOF_CLASS_SET(ret, ANYOF_DIGIT);
3479 /* consecutive digits assumed */
3480 for (value = '0'; value <= '9'; value++)
3481 ANYOF_BITMAP_SET(ret, value);
3483 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsDigit\n");
3487 ANYOF_CLASS_SET(ret, ANYOF_NDIGIT);
3489 /* consecutive digits assumed */
3490 for (value = 0; value < '0'; value++)
3491 ANYOF_BITMAP_SET(ret, value);
3492 for (value = '9' + 1; value < 256; value++)
3493 ANYOF_BITMAP_SET(ret, value);
3495 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsDigit\n");
3499 ANYOF_CLASS_SET(ret, ANYOF_GRAPH);
3501 for (value = 0; value < 256; value++)
3503 ANYOF_BITMAP_SET(ret, value);
3505 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsGraph\n");
3509 ANYOF_CLASS_SET(ret, ANYOF_NGRAPH);
3511 for (value = 0; value < 256; value++)
3512 if (!isGRAPH(value))
3513 ANYOF_BITMAP_SET(ret, value);
3515 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsGraph\n");
3519 ANYOF_CLASS_SET(ret, ANYOF_LOWER);
3521 for (value = 0; value < 256; value++)
3523 ANYOF_BITMAP_SET(ret, value);
3525 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsLower\n");
3529 ANYOF_CLASS_SET(ret, ANYOF_NLOWER);
3531 for (value = 0; value < 256; value++)
3532 if (!isLOWER(value))
3533 ANYOF_BITMAP_SET(ret, value);
3535 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsLower\n");
3539 ANYOF_CLASS_SET(ret, ANYOF_PRINT);
3541 for (value = 0; value < 256; value++)
3543 ANYOF_BITMAP_SET(ret, value);
3545 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsPrint\n");
3549 ANYOF_CLASS_SET(ret, ANYOF_NPRINT);
3551 for (value = 0; value < 256; value++)
3552 if (!isPRINT(value))
3553 ANYOF_BITMAP_SET(ret, value);
3555 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsPrint\n");
3559 ANYOF_CLASS_SET(ret, ANYOF_PSXSPC);
3561 for (value = 0; value < 256; value++)
3562 if (isPSXSPC(value))
3563 ANYOF_BITMAP_SET(ret, value);
3565 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsSpace\n");
3569 ANYOF_CLASS_SET(ret, ANYOF_NPSXSPC);
3571 for (value = 0; value < 256; value++)
3572 if (!isPSXSPC(value))
3573 ANYOF_BITMAP_SET(ret, value);
3575 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsSpace\n");
3579 ANYOF_CLASS_SET(ret, ANYOF_PUNCT);
3581 for (value = 0; value < 256; value++)
3583 ANYOF_BITMAP_SET(ret, value);
3585 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsPunct\n");
3589 ANYOF_CLASS_SET(ret, ANYOF_NPUNCT);
3591 for (value = 0; value < 256; value++)
3592 if (!isPUNCT(value))
3593 ANYOF_BITMAP_SET(ret, value);
3595 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsPunct\n");
3599 ANYOF_CLASS_SET(ret, ANYOF_SPACE);
3601 for (value = 0; value < 256; value++)
3603 ANYOF_BITMAP_SET(ret, value);
3605 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsSpacePerl\n");
3609 ANYOF_CLASS_SET(ret, ANYOF_NSPACE);
3611 for (value = 0; value < 256; value++)
3612 if (!isSPACE(value))
3613 ANYOF_BITMAP_SET(ret, value);
3615 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsSpacePerl\n");
3619 ANYOF_CLASS_SET(ret, ANYOF_UPPER);
3621 for (value = 0; value < 256; value++)
3623 ANYOF_BITMAP_SET(ret, value);
3625 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsUpper\n");
3629 ANYOF_CLASS_SET(ret, ANYOF_NUPPER);
3631 for (value = 0; value < 256; value++)
3632 if (!isUPPER(value))
3633 ANYOF_BITMAP_SET(ret, value);
3635 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsUpper\n");
3639 ANYOF_CLASS_SET(ret, ANYOF_XDIGIT);
3641 for (value = 0; value < 256; value++)
3642 if (isXDIGIT(value))
3643 ANYOF_BITMAP_SET(ret, value);
3645 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsXDigit\n");
3649 ANYOF_CLASS_SET(ret, ANYOF_NXDIGIT);
3651 for (value = 0; value < 256; value++)
3652 if (!isXDIGIT(value))
3653 ANYOF_BITMAP_SET(ret, value);
3655 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsXDigit\n");
3658 vFAIL("Invalid [::] class");
3662 ANYOF_FLAGS(ret) |= ANYOF_CLASS;
3665 } /* end of namedclass \blah */
3668 if (lastvalue > value) /* b-a */ {
3669 Simple_vFAIL4("Invalid [] range \"%*.*s\"",
3670 RExC_parse - rangebegin,
3671 RExC_parse - rangebegin,
3674 range = 0; /* not a true range */
3677 lastvalue = value; /* save the beginning of the range */
3678 if (*RExC_parse == '-' && RExC_parse+1 < RExC_end &&
3679 RExC_parse[1] != ']') {
3682 /* a bad range like \w-, [:word:]- ? */
3683 if (namedclass > OOB_NAMEDCLASS) {
3684 if (ckWARN(WARN_REGEXP))
3686 "False [] range \"%*.*s\"",
3687 RExC_parse - rangebegin,
3688 RExC_parse - rangebegin,
3691 ANYOF_BITMAP_SET(ret, '-');
3693 range = 1; /* yeah, it's a range! */
3694 continue; /* but do it the next time */
3698 /* now is the next time */
3700 if (lastvalue < 256 && value < 256) {
3701 #ifndef ASCIIish /* EBCDIC, for example. */
3702 if ((isLOWER(lastvalue) && isLOWER(value)) ||
3703 (isUPPER(lastvalue) && isUPPER(value)))
3706 if (isLOWER(lastvalue)) {
3707 for (i = lastvalue; i <= value; i++)
3709 ANYOF_BITMAP_SET(ret, i);
3711 for (i = lastvalue; i <= value; i++)
3713 ANYOF_BITMAP_SET(ret, i);
3718 for ( ; lastvalue <= value; lastvalue++)
3719 ANYOF_BITMAP_SET(ret, lastvalue);
3721 ANYOF_FLAGS(ret) |= ANYOF_UNICODE;
3722 if (lastvalue < value)
3723 Perl_sv_catpvf(aTHX_ listsv, "%04"UVxf"\t%04"UVxf"\n",
3724 (UV)lastvalue, (UV)value);
3726 Perl_sv_catpvf(aTHX_ listsv, "%04"UVxf"\n",
3731 range = 0; /* this range (if it was one) is done now */
3736 RExC_size += ANYOF_CLASS_ADD_SKIP;
3738 RExC_emit += ANYOF_CLASS_ADD_SKIP;
3741 /* optimize case-insensitive simple patterns (e.g. /[a-z]/i) */
3744 /* If the only flag is folding (plus possibly inversion). */
3745 (ANYOF_FLAGS_ALL ^ ANYOF_INVERT) == ANYOF_FOLD)) {
3746 for (value = 0; value < 256; ++value) {
3747 if (ANYOF_BITMAP_TEST(ret, value)) {
3748 IV fold = PL_fold[value];
3751 ANYOF_BITMAP_SET(ret, fold);
3754 ANYOF_FLAGS(ret) &= ~ANYOF_FOLD;
3757 /* optimize inverted simple patterns (e.g. [^a-z]) */
3759 /* If the only flag is inversion. */
3760 (ANYOF_FLAGS(ret) & ANYOF_FLAGS_ALL) == ANYOF_INVERT) {
3761 for (value = 0; value < ANYOF_BITMAP_SIZE; ++value)
3762 ANYOF_BITMAP(ret)[value] ^= ANYOF_FLAGS_ALL;
3763 ANYOF_FLAGS(ret) = 0;
3770 av_store(av, 0, listsv);
3771 av_store(av, 1, NULL);
3772 rv = newRV_noinc((SV*)av);
3773 n = add_data(pRExC_state, 1, "s");
3774 RExC_rx->data->data[n] = (void*)rv;
3782 S_nextchar(pTHX_ RExC_state_t *pRExC_state)
3784 char* retval = RExC_parse++;
3787 if (*RExC_parse == '(' && RExC_parse[1] == '?' &&
3788 RExC_parse[2] == '#') {
3789 while (*RExC_parse && *RExC_parse != ')')
3794 if (RExC_flags16 & PMf_EXTENDED) {
3795 if (isSPACE(*RExC_parse)) {
3799 else if (*RExC_parse == '#') {
3800 while (*RExC_parse && *RExC_parse != '\n')
3811 - reg_node - emit a node
3813 STATIC regnode * /* Location. */
3814 S_reg_node(pTHX_ RExC_state_t *pRExC_state, U8 op)
3816 register regnode *ret;
3817 register regnode *ptr;
3821 SIZE_ALIGN(RExC_size);
3826 NODE_ALIGN_FILL(ret);
3828 FILL_ADVANCE_NODE(ptr, op);
3835 - reganode - emit a node with an argument
3837 STATIC regnode * /* Location. */
3838 S_reganode(pTHX_ RExC_state_t *pRExC_state, U8 op, U32 arg)
3840 register regnode *ret;
3841 register regnode *ptr;
3845 SIZE_ALIGN(RExC_size);
3850 NODE_ALIGN_FILL(ret);
3852 FILL_ADVANCE_NODE_ARG(ptr, op, arg);
3859 - reguni - emit (if appropriate) a Unicode character
3862 S_reguni(pTHX_ RExC_state_t *pRExC_state, UV uv, char* s, STRLEN* lenp)
3864 *lenp = SIZE_ONLY ? UNISKIP(uv) : (uv_to_utf8((U8*)s, uv) - (U8*)s);
3868 - reginsert - insert an operator in front of already-emitted operand
3870 * Means relocating the operand.
3873 S_reginsert(pTHX_ RExC_state_t *pRExC_state, U8 op, regnode *opnd)
3875 register regnode *src;
3876 register regnode *dst;
3877 register regnode *place;
3878 register int offset = regarglen[(U8)op];
3880 /* (PL_regkind[(U8)op] == CURLY ? EXTRA_STEP_2ARGS : 0); */
3883 RExC_size += NODE_STEP_REGNODE + offset;
3888 RExC_emit += NODE_STEP_REGNODE + offset;
3891 StructCopy(--src, --dst, regnode);
3893 place = opnd; /* Op node, where operand used to be. */
3894 src = NEXTOPER(place);
3895 FILL_ADVANCE_NODE(place, op);
3896 Zero(src, offset, regnode);
3900 - regtail - set the next-pointer at the end of a node chain of p to val.
3903 S_regtail(pTHX_ RExC_state_t *pRExC_state, regnode *p, regnode *val)
3905 register regnode *scan;
3906 register regnode *temp;
3911 /* Find last node. */
3914 temp = regnext(scan);
3920 if (reg_off_by_arg[OP(scan)]) {
3921 ARG_SET(scan, val - scan);
3924 NEXT_OFF(scan) = val - scan;
3929 - regoptail - regtail on operand of first argument; nop if operandless
3932 S_regoptail(pTHX_ RExC_state_t *pRExC_state, regnode *p, regnode *val)
3934 /* "Operandless" and "op != BRANCH" are synonymous in practice. */
3935 if (p == NULL || SIZE_ONLY)
3937 if (PL_regkind[(U8)OP(p)] == BRANCH) {
3938 regtail(pRExC_state, NEXTOPER(p), val);
3940 else if ( PL_regkind[(U8)OP(p)] == BRANCHJ) {
3941 regtail(pRExC_state, NEXTOPER(NEXTOPER(p)), val);
3948 - regcurly - a little FSA that accepts {\d+,?\d*}
3951 S_regcurly(pTHX_ register char *s)
3970 S_dumpuntil(pTHX_ regnode *start, regnode *node, regnode *last, SV* sv, I32 l)
3973 register U8 op = EXACT; /* Arbitrary non-END op. */
3974 register regnode *next;
3976 while (op != END && (!last || node < last)) {
3977 /* While that wasn't END last time... */
3983 next = regnext(node);
3985 if (OP(node) == OPTIMIZED)
3988 PerlIO_printf(Perl_debug_log, "%4"IVdf":%*s%s", (IV)(node - start),
3989 (int)(2*l + 1), "", SvPVX(sv));
3990 if (next == NULL) /* Next ptr. */
3991 PerlIO_printf(Perl_debug_log, "(0)");
3993 PerlIO_printf(Perl_debug_log, "(%"IVdf")", (IV)(next - start));
3994 (void)PerlIO_putc(Perl_debug_log, '\n');
3996 if (PL_regkind[(U8)op] == BRANCHJ) {
3997 register regnode *nnode = (OP(next) == LONGJMP
4000 if (last && nnode > last)
4002 node = dumpuntil(start, NEXTOPER(NEXTOPER(node)), nnode, sv, l + 1);
4004 else if (PL_regkind[(U8)op] == BRANCH) {
4005 node = dumpuntil(start, NEXTOPER(node), next, sv, l + 1);
4007 else if ( op == CURLY) { /* `next' might be very big: optimizer */
4008 node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
4009 NEXTOPER(node) + EXTRA_STEP_2ARGS + 1, sv, l + 1);
4011 else if (PL_regkind[(U8)op] == CURLY && op != CURLYX) {
4012 node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
4015 else if ( op == PLUS || op == STAR) {
4016 node = dumpuntil(start, NEXTOPER(node), NEXTOPER(node) + 1, sv, l + 1);
4018 else if (op == ANYOF) {
4019 node = NEXTOPER(node);
4022 else if (PL_regkind[(U8)op] == EXACT) {
4023 /* Literal string, where present. */
4024 node += NODE_SZ_STR(node) - 1;
4025 node = NEXTOPER(node);
4028 node = NEXTOPER(node);
4029 node += regarglen[(U8)op];
4031 if (op == CURLYX || op == OPEN)
4033 else if (op == WHILEM)
4036 #endif /* DEBUGGING */
4041 - regdump - dump a regexp onto Perl_debug_log in vaguely comprehensible form
4044 Perl_regdump(pTHX_ regexp *r)
4047 SV *sv = sv_newmortal();
4049 (void)dumpuntil(r->program, r->program + 1, NULL, sv, 0);
4051 /* Header fields of interest. */
4052 if (r->anchored_substr)
4053 PerlIO_printf(Perl_debug_log,
4054 "anchored `%s%.*s%s'%s at %"IVdf" ",
4056 (int)(SvCUR(r->anchored_substr) - (SvTAIL(r->anchored_substr)!=0)),
4057 SvPVX(r->anchored_substr),
4059 SvTAIL(r->anchored_substr) ? "$" : "",
4060 (IV)r->anchored_offset);
4061 if (r->float_substr)
4062 PerlIO_printf(Perl_debug_log,
4063 "floating `%s%.*s%s'%s at %"IVdf"..%"UVuf" ",
4065 (int)(SvCUR(r->float_substr) - (SvTAIL(r->float_substr)!=0)),
4066 SvPVX(r->float_substr),
4068 SvTAIL(r->float_substr) ? "$" : "",
4069 (IV)r->float_min_offset, (UV)r->float_max_offset);
4070 if (r->check_substr)
4071 PerlIO_printf(Perl_debug_log,
4072 r->check_substr == r->float_substr
4073 ? "(checking floating" : "(checking anchored");
4074 if (r->reganch & ROPT_NOSCAN)
4075 PerlIO_printf(Perl_debug_log, " noscan");
4076 if (r->reganch & ROPT_CHECK_ALL)
4077 PerlIO_printf(Perl_debug_log, " isall");
4078 if (r->check_substr)
4079 PerlIO_printf(Perl_debug_log, ") ");
4081 if (r->regstclass) {
4082 regprop(sv, r->regstclass);
4083 PerlIO_printf(Perl_debug_log, "stclass `%s' ", SvPVX(sv));
4085 if (r->reganch & ROPT_ANCH) {
4086 PerlIO_printf(Perl_debug_log, "anchored");
4087 if (r->reganch & ROPT_ANCH_BOL)
4088 PerlIO_printf(Perl_debug_log, "(BOL)");
4089 if (r->reganch & ROPT_ANCH_MBOL)
4090 PerlIO_printf(Perl_debug_log, "(MBOL)");
4091 if (r->reganch & ROPT_ANCH_SBOL)
4092 PerlIO_printf(Perl_debug_log, "(SBOL)");
4093 if (r->reganch & ROPT_ANCH_GPOS)
4094 PerlIO_printf(Perl_debug_log, "(GPOS)");
4095 PerlIO_putc(Perl_debug_log, ' ');
4097 if (r->reganch & ROPT_GPOS_SEEN)
4098 PerlIO_printf(Perl_debug_log, "GPOS ");
4099 if (r->reganch & ROPT_SKIP)
4100 PerlIO_printf(Perl_debug_log, "plus ");
4101 if (r->reganch & ROPT_IMPLICIT)
4102 PerlIO_printf(Perl_debug_log, "implicit ");
4103 PerlIO_printf(Perl_debug_log, "minlen %ld ", (long) r->minlen);
4104 if (r->reganch & ROPT_EVAL_SEEN)
4105 PerlIO_printf(Perl_debug_log, "with eval ");
4106 PerlIO_printf(Perl_debug_log, "\n");
4107 #endif /* DEBUGGING */
4111 S_put_byte(pTHX_ SV *sv, int c)
4113 if (isCNTRL(c) || c == 127 || c == 255 || !isPRINT(c))
4114 Perl_sv_catpvf(aTHX_ sv, "\\%o", c);
4115 else if (c == '-' || c == ']' || c == '\\' || c == '^')
4116 Perl_sv_catpvf(aTHX_ sv, "\\%c", c);
4118 Perl_sv_catpvf(aTHX_ sv, "%c", c);
4122 - regprop - printable representation of opcode
4125 Perl_regprop(pTHX_ SV *sv, regnode *o)
4130 sv_setpvn(sv, "", 0);
4131 if (OP(o) >= reg_num) /* regnode.type is unsigned */
4132 /* It would be nice to FAIL() here, but this may be called from
4133 regexec.c, and it would be hard to supply pRExC_state. */
4134 Perl_croak(aTHX_ "Corrupted regexp opcode");
4135 sv_catpv(sv, (char*)reg_name[OP(o)]); /* Take off const! */
4137 k = PL_regkind[(U8)OP(o)];
4140 Perl_sv_catpvf(aTHX_ sv, " <%s%.*s%s>", PL_colors[0],
4141 STR_LEN(o), STRING(o), PL_colors[1]);
4142 else if (k == CURLY) {
4143 if (OP(o) == CURLYM || OP(o) == CURLYN || OP(o) == CURLYX)
4144 Perl_sv_catpvf(aTHX_ sv, "[%d]", o->flags); /* Parenth number */
4145 Perl_sv_catpvf(aTHX_ sv, " {%d,%d}", ARG1(o), ARG2(o));
4147 else if (k == WHILEM && o->flags) /* Ordinal/of */
4148 Perl_sv_catpvf(aTHX_ sv, "[%d/%d]", o->flags & 0xf, o->flags>>4);
4149 else if (k == REF || k == OPEN || k == CLOSE || k == GROUPP )
4150 Perl_sv_catpvf(aTHX_ sv, "%d", (int)ARG(o)); /* Parenth number */
4151 else if (k == LOGICAL)
4152 Perl_sv_catpvf(aTHX_ sv, "[%d]", o->flags); /* 2: embedded, otherwise 1 */
4153 else if (k == ANYOF) {
4154 int i, rangestart = -1;
4155 U8 flags = ANYOF_FLAGS(o);
4156 const char * const anyofs[] = { /* Should be syncronized with
4157 * ANYOF_ #xdefines in regcomp.h */
4190 if (flags & ANYOF_LOCALE)
4191 sv_catpv(sv, "{loc}");
4192 if (flags & ANYOF_FOLD)
4193 sv_catpv(sv, "{i}");
4194 Perl_sv_catpvf(aTHX_ sv, "[%s", PL_colors[0]);
4195 if (flags & ANYOF_INVERT)
4197 for (i = 0; i <= 256; i++) {
4198 if (i < 256 && ANYOF_BITMAP_TEST(o,i)) {
4199 if (rangestart == -1)
4201 } else if (rangestart != -1) {
4202 if (i <= rangestart + 3)
4203 for (; rangestart < i; rangestart++)
4204 put_byte(sv, rangestart);
4206 put_byte(sv, rangestart);
4208 put_byte(sv, i - 1);
4214 if (o->flags & ANYOF_CLASS)
4215 for (i = 0; i < sizeof(anyofs)/sizeof(char*); i++)
4216 if (ANYOF_CLASS_TEST(o,i))
4217 sv_catpv(sv, anyofs[i]);
4219 if (flags & ANYOF_UNICODE)
4220 sv_catpv(sv, "{unicode}");
4224 SV *sw = regclass_swash(o, FALSE, &lv);
4229 U8 s[UTF8_MAXLEN+1];
4231 for (i = 0; i <= 256; i++) { /* just the first 256 */
4232 U8 *e = uv_to_utf8(s, i);
4234 if (i < 256 && swash_fetch(sw, s)) {
4235 if (rangestart == -1)
4237 } else if (rangestart != -1) {
4240 if (i <= rangestart + 3)
4241 for (; rangestart < i; rangestart++) {
4242 for(e = uv_to_utf8(s, rangestart), p = s; p < e; p++)
4246 for (e = uv_to_utf8(s, rangestart), p = s; p < e; p++)
4249 for (e = uv_to_utf8(s, i - 1), p = s; p < e; p++)
4256 sv_catpv(sv, "..."); /* et cetera */
4260 char *s = savepv(SvPVX(lv));
4263 while(*s && *s != '\n') s++;
4284 Perl_sv_catpvf(aTHX_ sv, "%s]", PL_colors[1]);
4286 else if (k == BRANCHJ && (OP(o) == UNLESSM || OP(o) == IFMATCH))
4287 Perl_sv_catpvf(aTHX_ sv, "[-%d]", o->flags);
4288 #endif /* DEBUGGING */
4292 Perl_re_intuit_string(pTHX_ regexp *prog)
4293 { /* Assume that RE_INTUIT is set */
4296 char *s = SvPV(prog->check_substr,n_a);
4298 if (!PL_colorset) reginitcolors();
4299 PerlIO_printf(Perl_debug_log,
4300 "%sUsing REx substr:%s `%s%.60s%s%s'\n",
4301 PL_colors[4],PL_colors[5],PL_colors[0],
4304 (strlen(s) > 60 ? "..." : ""));
4307 return prog->check_substr;
4311 Perl_pregfree(pTHX_ struct regexp *r)
4313 DEBUG_r(if (!PL_colorset) reginitcolors());
4315 if (!r || (--r->refcnt > 0))
4317 DEBUG_r(PerlIO_printf(Perl_debug_log,
4318 "%sFreeing REx:%s `%s%.60s%s%s'\n",
4319 PL_colors[4],PL_colors[5],PL_colors[0],
4322 (strlen(r->precomp) > 60 ? "..." : "")));
4325 Safefree(r->precomp);
4326 if (RX_MATCH_COPIED(r))
4327 Safefree(r->subbeg);
4329 if (r->anchored_substr)
4330 SvREFCNT_dec(r->anchored_substr);
4331 if (r->float_substr)
4332 SvREFCNT_dec(r->float_substr);
4333 Safefree(r->substrs);
4336 int n = r->data->count;
4337 AV* new_comppad = NULL;
4342 switch (r->data->what[n]) {
4344 SvREFCNT_dec((SV*)r->data->data[n]);
4347 Safefree(r->data->data[n]);
4350 new_comppad = (AV*)r->data->data[n];
4353 if (new_comppad == NULL)
4354 Perl_croak(aTHX_ "panic: pregfree comppad");
4355 old_comppad = PL_comppad;
4356 old_curpad = PL_curpad;
4357 /* Watch out for global destruction's random ordering. */
4358 if (SvTYPE(new_comppad) == SVt_PVAV) {
4359 PL_comppad = new_comppad;
4360 PL_curpad = AvARRAY(new_comppad);
4364 op_free((OP_4tree*)r->data->data[n]);
4365 PL_comppad = old_comppad;
4366 PL_curpad = old_curpad;
4367 SvREFCNT_dec((SV*)new_comppad);
4373 Perl_croak(aTHX_ "panic: regfree data code '%c'", r->data->what[n]);
4376 Safefree(r->data->what);
4379 Safefree(r->startp);
4385 - regnext - dig the "next" pointer out of a node
4387 * [Note, when REGALIGN is defined there are two places in regmatch()
4388 * that bypass this code for speed.]
4391 Perl_regnext(pTHX_ register regnode *p)
4393 register I32 offset;
4395 if (p == &PL_regdummy)
4398 offset = (reg_off_by_arg[OP(p)] ? ARG(p) : NEXT_OFF(p));
4406 S_re_croak2(pTHX_ const char* pat1,const char* pat2,...)
4409 STRLEN l1 = strlen(pat1);
4410 STRLEN l2 = strlen(pat2);
4419 Copy(pat1, buf, l1 , char);
4420 Copy(pat2, buf + l1, l2 , char);
4421 buf[l1 + l2] = '\n';
4422 buf[l1 + l2 + 1] = '\0';
4424 /* ANSI variant takes additional second argument */
4425 va_start(args, pat2);
4429 msv = vmess(buf, &args);
4431 message = SvPV(msv,l1);
4434 Copy(message, buf, l1 , char);
4435 buf[l1] = '\0'; /* Overwrite \n */
4436 Perl_croak(aTHX_ "%s", buf);
4439 /* XXX Here's a total kludge. But we need to re-enter for swash routines. */
4442 Perl_save_re_context(pTHX)
4445 SAVEPPTR(RExC_precomp); /* uncompiled string. */
4446 SAVEI32(RExC_npar); /* () count. */
4447 SAVEI32(RExC_size); /* Code size. */
4448 SAVEI16(RExC_flags16); /* are we folding, multilining? */
4449 SAVEVPTR(RExC_rx); /* from regcomp.c */
4450 SAVEI32(RExC_seen); /* from regcomp.c */
4451 SAVEI32(RExC_sawback); /* Did we see \1, ...? */
4452 SAVEI32(RExC_naughty); /* How bad is this pattern? */
4453 SAVEVPTR(RExC_emit); /* Code-emit pointer; ®dummy = don't */
4454 SAVEPPTR(RExC_end); /* End of input for compile */
4455 SAVEPPTR(RExC_parse); /* Input-scan pointer. */
4458 SAVEI32(PL_reg_flags); /* from regexec.c */
4460 SAVEPPTR(PL_reginput); /* String-input pointer. */
4461 SAVEPPTR(PL_regbol); /* Beginning of input, for ^ check. */
4462 SAVEPPTR(PL_regeol); /* End of input, for $ check. */
4463 SAVEVPTR(PL_regstartp); /* Pointer to startp array. */
4464 SAVEVPTR(PL_regendp); /* Ditto for endp. */
4465 SAVEVPTR(PL_reglastparen); /* Similarly for lastparen. */
4466 SAVEPPTR(PL_regtill); /* How far we are required to go. */
4467 SAVEI8(PL_regprev); /* char before regbol, \n if none */
4468 SAVEVPTR(PL_reg_start_tmp); /* from regexec.c */
4469 PL_reg_start_tmp = 0;
4470 SAVEFREEPV(PL_reg_start_tmp);
4471 SAVEI32(PL_reg_start_tmpl); /* from regexec.c */
4472 PL_reg_start_tmpl = 0;
4473 SAVEVPTR(PL_regdata);
4474 SAVEI32(PL_reg_eval_set); /* from regexec.c */
4475 SAVEI32(PL_regnarrate); /* from regexec.c */
4476 SAVEVPTR(PL_regprogram); /* from regexec.c */
4477 SAVEINT(PL_regindent); /* from regexec.c */
4478 SAVEVPTR(PL_regcc); /* from regexec.c */
4479 SAVEVPTR(PL_curcop);
4480 SAVEVPTR(PL_reg_call_cc); /* from regexec.c */
4481 SAVEVPTR(PL_reg_re); /* from regexec.c */
4482 SAVEPPTR(PL_reg_ganch); /* from regexec.c */
4483 SAVESPTR(PL_reg_sv); /* from regexec.c */
4484 SAVEVPTR(PL_reg_magic); /* from regexec.c */
4485 SAVEI32(PL_reg_oldpos); /* from regexec.c */
4486 SAVEVPTR(PL_reg_oldcurpm); /* from regexec.c */
4487 SAVEVPTR(PL_reg_curpm); /* from regexec.c */
4488 SAVEI32(PL_regnpar); /* () count. */
4490 SAVEPPTR(PL_reg_starttry); /* from regexec.c */
4501 clear_re(pTHXo_ void *r)
4503 ReREFCNT_dec((regexp *)r);