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-2001, 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. */
132 char *starttry; /* -Dr: where regtry was called. */
133 #define RExC_starttry (pRExC_state->starttry)
137 #define RExC_flags16 (pRExC_state->flags16)
138 #define RExC_precomp (pRExC_state->precomp)
139 #define RExC_rx (pRExC_state->rx)
140 #define RExC_end (pRExC_state->end)
141 #define RExC_parse (pRExC_state->parse)
142 #define RExC_whilem_seen (pRExC_state->whilem_seen)
143 #define RExC_emit (pRExC_state->emit)
144 #define RExC_naughty (pRExC_state->naughty)
145 #define RExC_sawback (pRExC_state->sawback)
146 #define RExC_seen (pRExC_state->seen)
147 #define RExC_size (pRExC_state->size)
148 #define RExC_npar (pRExC_state->npar)
149 #define RExC_extralen (pRExC_state->extralen)
150 #define RExC_seen_zerolen (pRExC_state->seen_zerolen)
151 #define RExC_seen_evals (pRExC_state->seen_evals)
152 #define RExC_utf8 (pRExC_state->utf8)
154 #define ISMULT1(c) ((c) == '*' || (c) == '+' || (c) == '?')
155 #define ISMULT2(s) ((*s) == '*' || (*s) == '+' || (*s) == '?' || \
156 ((*s) == '{' && regcurly(s)))
159 #undef SPSTART /* dratted cpp namespace... */
162 * Flags to be passed up and down.
164 #define WORST 0 /* Worst case. */
165 #define HASWIDTH 0x1 /* Known to match non-null strings. */
166 #define SIMPLE 0x2 /* Simple enough to be STAR/PLUS operand. */
167 #define SPSTART 0x4 /* Starts with * or +. */
168 #define TRYAGAIN 0x8 /* Weeded out a declaration. */
170 /* Length of a variant. */
172 typedef struct scan_data_t {
178 I32 last_end; /* min value, <0 unless valid. */
181 SV **longest; /* Either &l_fixed, or &l_float. */
185 I32 offset_float_min;
186 I32 offset_float_max;
190 struct regnode_charclass_class *start_class;
194 * Forward declarations for pregcomp()'s friends.
197 static scan_data_t zero_scan_data = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
200 #define SF_BEFORE_EOL (SF_BEFORE_SEOL|SF_BEFORE_MEOL)
201 #define SF_BEFORE_SEOL 0x1
202 #define SF_BEFORE_MEOL 0x2
203 #define SF_FIX_BEFORE_EOL (SF_FIX_BEFORE_SEOL|SF_FIX_BEFORE_MEOL)
204 #define SF_FL_BEFORE_EOL (SF_FL_BEFORE_SEOL|SF_FL_BEFORE_MEOL)
207 # define SF_FIX_SHIFT_EOL (0+2)
208 # define SF_FL_SHIFT_EOL (0+4)
210 # define SF_FIX_SHIFT_EOL (+2)
211 # define SF_FL_SHIFT_EOL (+4)
214 #define SF_FIX_BEFORE_SEOL (SF_BEFORE_SEOL << SF_FIX_SHIFT_EOL)
215 #define SF_FIX_BEFORE_MEOL (SF_BEFORE_MEOL << SF_FIX_SHIFT_EOL)
217 #define SF_FL_BEFORE_SEOL (SF_BEFORE_SEOL << SF_FL_SHIFT_EOL)
218 #define SF_FL_BEFORE_MEOL (SF_BEFORE_MEOL << SF_FL_SHIFT_EOL) /* 0x20 */
219 #define SF_IS_INF 0x40
220 #define SF_HAS_PAR 0x80
221 #define SF_IN_PAR 0x100
222 #define SF_HAS_EVAL 0x200
223 #define SCF_DO_SUBSTR 0x400
224 #define SCF_DO_STCLASS_AND 0x0800
225 #define SCF_DO_STCLASS_OR 0x1000
226 #define SCF_DO_STCLASS (SCF_DO_STCLASS_AND|SCF_DO_STCLASS_OR)
227 #define SCF_WHILEM_VISITED_POS 0x2000
229 #define UTF RExC_utf8
230 #define LOC (RExC_flags16 & PMf_LOCALE)
231 #define FOLD (RExC_flags16 & PMf_FOLD)
233 #define OOB_UNICODE 12345678
234 #define OOB_NAMEDCLASS -1
236 #define CHR_SVLEN(sv) (UTF ? sv_len_utf8(sv) : SvCUR(sv))
237 #define CHR_DIST(a,b) (UTF ? utf8_distance(a,b) : a - b)
240 /* length of regex to show in messages that don't mark a position within */
241 #define RegexLengthToShowInErrorMessages 127
244 * If MARKER[12] are adjusted, be sure to adjust the constants at the top
245 * of t/op/regmesg.t, the tests in t/op/re_tests, and those in
246 * op/pragma/warn/regcomp.
248 #define MARKER1 "HERE" /* marker as it appears in the description */
249 #define MARKER2 " << HERE " /* marker as it appears within the regex */
251 #define REPORT_LOCATION " before " MARKER1 " mark in regex m/%.*s" MARKER2 "%s/"
254 * Calls SAVEDESTRUCTOR_X if needed, then calls Perl_croak with the given
255 * arg. Show regex, up to a maximum length. If it's too long, chop and add
260 char *ellipses = ""; \
261 unsigned len = strlen(RExC_precomp); \
264 SAVEDESTRUCTOR_X(clear_re,(void*)RExC_rx); \
266 if (len > RegexLengthToShowInErrorMessages) { \
267 /* chop 10 shorter than the max, to ensure meaning of "..." */ \
268 len = RegexLengthToShowInErrorMessages - 10; \
271 Perl_croak(aTHX_ "%s in regex m/%.*s%s/", \
272 msg, (int)len, RExC_precomp, ellipses); \
276 * Calls SAVEDESTRUCTOR_X if needed, then calls Perl_croak with the given
277 * args. Show regex, up to a maximum length. If it's too long, chop and add
280 #define FAIL2(pat,msg) \
282 char *ellipses = ""; \
283 unsigned len = strlen(RExC_precomp); \
286 SAVEDESTRUCTOR_X(clear_re,(void*)RExC_rx); \
288 if (len > RegexLengthToShowInErrorMessages) { \
289 /* chop 10 shorter than the max, to ensure meaning of "..." */ \
290 len = RegexLengthToShowInErrorMessages - 10; \
293 S_re_croak2(aTHX_ pat, " in regex m/%.*s%s/", \
294 msg, (int)len, RExC_precomp, ellipses); \
299 * Simple_vFAIL -- like FAIL, but marks the current location in the scan
301 #define Simple_vFAIL(m) \
303 unsigned offset = strlen(RExC_precomp)-(RExC_end-RExC_parse); \
305 Perl_croak(aTHX_ "%s" REPORT_LOCATION, \
306 m, (int)offset, RExC_precomp, RExC_precomp + offset); \
310 * Calls SAVEDESTRUCTOR_X if needed, then Simple_vFAIL()
315 SAVEDESTRUCTOR_X(clear_re,(void*)RExC_rx); \
320 * Like Simple_vFAIL(), but accepts two arguments.
322 #define Simple_vFAIL2(m,a1) \
324 unsigned offset = strlen(RExC_precomp)-(RExC_end-RExC_parse); \
326 S_re_croak2(aTHX_ m, REPORT_LOCATION, a1, \
327 (int)offset, RExC_precomp, RExC_precomp + offset); \
331 * Calls SAVEDESTRUCTOR_X if needed, then Simple_vFAIL2().
333 #define vFAIL2(m,a1) \
336 SAVEDESTRUCTOR_X(clear_re,(void*)RExC_rx); \
337 Simple_vFAIL2(m, a1); \
342 * Like Simple_vFAIL(), but accepts three arguments.
344 #define Simple_vFAIL3(m, a1, a2) \
346 unsigned offset = strlen(RExC_precomp)-(RExC_end-RExC_parse); \
348 S_re_croak2(aTHX_ m, REPORT_LOCATION, a1, a2, \
349 (int)offset, RExC_precomp, RExC_precomp + offset); \
353 * Calls SAVEDESTRUCTOR_X if needed, then Simple_vFAIL3().
355 #define vFAIL3(m,a1,a2) \
358 SAVEDESTRUCTOR_X(clear_re,(void*)RExC_rx); \
359 Simple_vFAIL3(m, a1, a2); \
363 * Like Simple_vFAIL(), but accepts four arguments.
365 #define Simple_vFAIL4(m, a1, a2, a3) \
367 unsigned offset = strlen(RExC_precomp)-(RExC_end-RExC_parse); \
369 S_re_croak2(aTHX_ m, REPORT_LOCATION, a1, a2, a3,\
370 (int)offset, RExC_precomp, RExC_precomp + offset); \
374 * Like Simple_vFAIL(), but accepts five arguments.
376 #define Simple_vFAIL5(m, a1, a2, a3, a4) \
378 unsigned offset = strlen(RExC_precomp)-(RExC_end-RExC_parse); \
379 S_re_croak2(aTHX_ m, REPORT_LOCATION, a1, a2, a3, a4,\
380 (int)offset, RExC_precomp, RExC_precomp + offset); \
384 #define vWARN(loc,m) \
386 unsigned offset = strlen(RExC_precomp)-(RExC_end-(loc)); \
387 Perl_warner(aTHX_ WARN_REGEXP, "%s" REPORT_LOCATION,\
388 m, (int)offset, RExC_precomp, RExC_precomp + offset); \
392 #define vWARN2(loc, m, a1) \
394 unsigned offset = strlen(RExC_precomp)-(RExC_end-(loc)); \
395 Perl_warner(aTHX_ WARN_REGEXP, m REPORT_LOCATION,\
397 (int)offset, RExC_precomp, RExC_precomp + offset); \
400 #define vWARN3(loc, m, a1, a2) \
402 unsigned offset = strlen(RExC_precomp) - (RExC_end - (loc)); \
403 Perl_warner(aTHX_ WARN_REGEXP, m REPORT_LOCATION, \
405 (int)offset, RExC_precomp, RExC_precomp + offset); \
408 #define vWARN4(loc, m, a1, a2, a3) \
410 unsigned offset = strlen(RExC_precomp)-(RExC_end-(loc)); \
411 Perl_warner(aTHX_ WARN_REGEXP, m REPORT_LOCATION,\
413 (int)offset, RExC_precomp, RExC_precomp + offset); \
417 /* Allow for side effects in s */
418 #define REGC(c,s) STMT_START { if (!SIZE_ONLY) *(s) = (c); else (s);} STMT_END
420 static void clear_re(pTHXo_ void *r);
422 /* Mark that we cannot extend a found fixed substring at this point.
423 Updata the longest found anchored substring and the longest found
424 floating substrings if needed. */
427 S_scan_commit(pTHX_ RExC_state_t *pRExC_state, scan_data_t *data)
429 STRLEN l = CHR_SVLEN(data->last_found);
430 STRLEN old_l = CHR_SVLEN(*data->longest);
432 if ((l >= old_l) && ((l > old_l) || (data->flags & SF_BEFORE_EOL))) {
433 sv_setsv(*data->longest, data->last_found);
434 if (*data->longest == data->longest_fixed) {
435 data->offset_fixed = l ? data->last_start_min : data->pos_min;
436 if (data->flags & SF_BEFORE_EOL)
438 |= ((data->flags & SF_BEFORE_EOL) << SF_FIX_SHIFT_EOL);
440 data->flags &= ~SF_FIX_BEFORE_EOL;
443 data->offset_float_min = l ? data->last_start_min : data->pos_min;
444 data->offset_float_max = (l
445 ? data->last_start_max
446 : data->pos_min + data->pos_delta);
447 if (data->flags & SF_BEFORE_EOL)
449 |= ((data->flags & SF_BEFORE_EOL) << SF_FL_SHIFT_EOL);
451 data->flags &= ~SF_FL_BEFORE_EOL;
454 SvCUR_set(data->last_found, 0);
456 data->flags &= ~SF_BEFORE_EOL;
459 /* Can match anything (initialization) */
461 S_cl_anything(pTHX_ RExC_state_t *pRExC_state, struct regnode_charclass_class *cl)
465 ANYOF_CLASS_ZERO(cl);
466 for (value = 0; value < 256; ++value)
467 ANYOF_BITMAP_SET(cl, value);
468 cl->flags = ANYOF_EOS|ANYOF_UNICODE_ALL;
470 cl->flags |= ANYOF_LOCALE;
473 /* Can match anything (initialization) */
475 S_cl_is_anything(pTHX_ struct regnode_charclass_class *cl)
479 for (value = 0; value <= ANYOF_MAX; value += 2)
480 if (ANYOF_CLASS_TEST(cl, value) && ANYOF_CLASS_TEST(cl, value + 1))
482 if (!(cl->flags & ANYOF_UNICODE_ALL))
484 for (value = 0; value < 256; ++value)
485 if (!ANYOF_BITMAP_TEST(cl, value))
490 /* Can match anything (initialization) */
492 S_cl_init(pTHX_ RExC_state_t *pRExC_state, struct regnode_charclass_class *cl)
494 Zero(cl, 1, struct regnode_charclass_class);
496 cl_anything(pRExC_state, cl);
500 S_cl_init_zero(pTHX_ RExC_state_t *pRExC_state, struct regnode_charclass_class *cl)
502 Zero(cl, 1, struct regnode_charclass_class);
504 cl_anything(pRExC_state, cl);
506 cl->flags |= ANYOF_LOCALE;
509 /* 'And' a given class with another one. Can create false positives */
510 /* We assume that cl is not inverted */
512 S_cl_and(pTHX_ struct regnode_charclass_class *cl,
513 struct regnode_charclass_class *and_with)
515 if (!(and_with->flags & ANYOF_CLASS)
516 && !(cl->flags & ANYOF_CLASS)
517 && (and_with->flags & ANYOF_LOCALE) == (cl->flags & ANYOF_LOCALE)
518 && !(and_with->flags & ANYOF_FOLD)
519 && !(cl->flags & ANYOF_FOLD)) {
522 if (and_with->flags & ANYOF_INVERT)
523 for (i = 0; i < ANYOF_BITMAP_SIZE; i++)
524 cl->bitmap[i] &= ~and_with->bitmap[i];
526 for (i = 0; i < ANYOF_BITMAP_SIZE; i++)
527 cl->bitmap[i] &= and_with->bitmap[i];
528 } /* XXXX: logic is complicated otherwise, leave it along for a moment. */
529 if (!(and_with->flags & ANYOF_EOS))
530 cl->flags &= ~ANYOF_EOS;
532 if (cl->flags & ANYOF_UNICODE_ALL && and_with->flags & ANYOF_UNICODE) {
533 cl->flags &= ~ANYOF_UNICODE_ALL;
534 cl->flags |= ANYOF_UNICODE;
535 ARG_SET(cl, ARG(and_with));
537 if (!(and_with->flags & ANYOF_UNICODE_ALL))
538 cl->flags &= ~ANYOF_UNICODE_ALL;
539 if (!(and_with->flags & (ANYOF_UNICODE|ANYOF_UNICODE_ALL)))
540 cl->flags &= ~ANYOF_UNICODE;
543 /* 'OR' a given class with another one. Can create false positives */
544 /* We assume that cl is not inverted */
546 S_cl_or(pTHX_ RExC_state_t *pRExC_state, struct regnode_charclass_class *cl, struct regnode_charclass_class *or_with)
548 if (or_with->flags & ANYOF_INVERT) {
550 * (B1 | CL1) | (!B2 & !CL2) = (B1 | !B2 & !CL2) | (CL1 | (!B2 & !CL2))
551 * <= (B1 | !B2) | (CL1 | !CL2)
552 * which is wasteful if CL2 is small, but we ignore CL2:
553 * (B1 | CL1) | (!B2 & !CL2) <= (B1 | CL1) | !B2 = (B1 | !B2) | CL1
554 * XXXX Can we handle case-fold? Unclear:
555 * (OK1(i) | OK1(i')) | !(OK1(i) | OK1(i')) =
556 * (OK1(i) | OK1(i')) | (!OK1(i) & !OK1(i'))
558 if ( (or_with->flags & ANYOF_LOCALE) == (cl->flags & ANYOF_LOCALE)
559 && !(or_with->flags & ANYOF_FOLD)
560 && !(cl->flags & ANYOF_FOLD) ) {
563 for (i = 0; i < ANYOF_BITMAP_SIZE; i++)
564 cl->bitmap[i] |= ~or_with->bitmap[i];
565 } /* XXXX: logic is complicated otherwise */
567 cl_anything(pRExC_state, cl);
570 /* (B1 | CL1) | (B2 | CL2) = (B1 | B2) | (CL1 | CL2)) */
571 if ( (or_with->flags & ANYOF_LOCALE) == (cl->flags & ANYOF_LOCALE)
572 && (!(or_with->flags & ANYOF_FOLD)
573 || (cl->flags & ANYOF_FOLD)) ) {
576 /* OR char bitmap and class bitmap separately */
577 for (i = 0; i < ANYOF_BITMAP_SIZE; i++)
578 cl->bitmap[i] |= or_with->bitmap[i];
579 if (or_with->flags & ANYOF_CLASS) {
580 for (i = 0; i < ANYOF_CLASSBITMAP_SIZE; i++)
581 cl->classflags[i] |= or_with->classflags[i];
582 cl->flags |= ANYOF_CLASS;
585 else { /* XXXX: logic is complicated, leave it along for a moment. */
586 cl_anything(pRExC_state, cl);
589 if (or_with->flags & ANYOF_EOS)
590 cl->flags |= ANYOF_EOS;
592 if (cl->flags & ANYOF_UNICODE && or_with->flags & ANYOF_UNICODE &&
593 ARG(cl) != ARG(or_with)) {
594 cl->flags |= ANYOF_UNICODE_ALL;
595 cl->flags &= ~ANYOF_UNICODE;
597 if (or_with->flags & ANYOF_UNICODE_ALL) {
598 cl->flags |= ANYOF_UNICODE_ALL;
599 cl->flags &= ~ANYOF_UNICODE;
603 /* REx optimizer. Converts nodes into quickier variants "in place".
604 Finds fixed substrings. */
606 /* Stops at toplevel WHILEM as well as at `last'. At end *scanp is set
607 to the position after last scanned or to NULL. */
610 S_study_chunk(pTHX_ RExC_state_t *pRExC_state, regnode **scanp, I32 *deltap, regnode *last, scan_data_t *data, U32 flags)
611 /* scanp: Start here (read-write). */
612 /* deltap: Write maxlen-minlen here. */
613 /* last: Stop before this one. */
615 I32 min = 0, pars = 0, code;
616 regnode *scan = *scanp, *next;
618 int is_inf = (flags & SCF_DO_SUBSTR) && (data->flags & SF_IS_INF);
619 int is_inf_internal = 0; /* The studied chunk is infinite */
620 I32 is_par = OP(scan) == OPEN ? ARG(scan) : 0;
621 scan_data_t data_fake;
622 struct regnode_charclass_class and_with; /* Valid if flags & SCF_DO_STCLASS_OR */
624 while (scan && OP(scan) != END && scan < last) {
625 /* Peephole optimizer: */
627 if (PL_regkind[(U8)OP(scan)] == EXACT) {
628 /* Merge several consecutive EXACTish nodes into one. */
629 regnode *n = regnext(scan);
632 regnode *stop = scan;
635 next = scan + NODE_SZ_STR(scan);
636 /* Skip NOTHING, merge EXACT*. */
638 ( PL_regkind[(U8)OP(n)] == NOTHING ||
639 (stringok && (OP(n) == OP(scan))))
641 && NEXT_OFF(scan) + NEXT_OFF(n) < I16_MAX) {
642 if (OP(n) == TAIL || n > next)
644 if (PL_regkind[(U8)OP(n)] == NOTHING) {
645 NEXT_OFF(scan) += NEXT_OFF(n);
646 next = n + NODE_STEP_REGNODE;
654 int oldl = STR_LEN(scan);
655 regnode *nnext = regnext(n);
657 if (oldl + STR_LEN(n) > U8_MAX)
659 NEXT_OFF(scan) += NEXT_OFF(n);
660 STR_LEN(scan) += STR_LEN(n);
661 next = n + NODE_SZ_STR(n);
662 /* Now we can overwrite *n : */
663 Move(STRING(n), STRING(scan) + oldl,
674 n = scan + NODE_SZ_STR(scan);
676 if (PL_regkind[(U8)OP(n)] != NOTHING || OP(n) == NOTHING) {
684 /* Follow the next-chain of the current node and optimize
685 away all the NOTHINGs from it. */
686 if (OP(scan) != CURLYX) {
687 int max = (reg_off_by_arg[OP(scan)]
689 /* I32 may be smaller than U16 on CRAYs! */
690 : (I32_MAX < U16_MAX ? I32_MAX : U16_MAX));
691 int off = (reg_off_by_arg[OP(scan)] ? ARG(scan) : NEXT_OFF(scan));
695 /* Skip NOTHING and LONGJMP. */
696 while ((n = regnext(n))
697 && ((PL_regkind[(U8)OP(n)] == NOTHING && (noff = NEXT_OFF(n)))
698 || ((OP(n) == LONGJMP) && (noff = ARG(n))))
701 if (reg_off_by_arg[OP(scan)])
704 NEXT_OFF(scan) = off;
706 /* The principal pseudo-switch. Cannot be a switch, since we
707 look into several different things. */
708 if (OP(scan) == BRANCH || OP(scan) == BRANCHJ
709 || OP(scan) == IFTHEN || OP(scan) == SUSPEND) {
710 next = regnext(scan);
713 if (OP(next) == code || code == IFTHEN || code == SUSPEND) {
714 I32 max1 = 0, min1 = I32_MAX, num = 0;
715 struct regnode_charclass_class accum;
717 if (flags & SCF_DO_SUBSTR) /* XXXX Add !SUSPEND? */
718 scan_commit(pRExC_state, data); /* Cannot merge strings after this. */
719 if (flags & SCF_DO_STCLASS)
720 cl_init_zero(pRExC_state, &accum);
721 while (OP(scan) == code) {
722 I32 deltanext, minnext, f = 0, fake;
723 struct regnode_charclass_class this_class;
728 data_fake.whilem_c = data->whilem_c;
729 data_fake.last_closep = data->last_closep;
732 data_fake.last_closep = &fake;
733 next = regnext(scan);
734 scan = NEXTOPER(scan);
736 scan = NEXTOPER(scan);
737 if (flags & SCF_DO_STCLASS) {
738 cl_init(pRExC_state, &this_class);
739 data_fake.start_class = &this_class;
740 f = SCF_DO_STCLASS_AND;
742 if (flags & SCF_WHILEM_VISITED_POS)
743 f |= SCF_WHILEM_VISITED_POS;
744 /* we suppose the run is continuous, last=next...*/
745 minnext = study_chunk(pRExC_state, &scan, &deltanext,
746 next, &data_fake, f);
749 if (max1 < minnext + deltanext)
750 max1 = minnext + deltanext;
751 if (deltanext == I32_MAX)
752 is_inf = is_inf_internal = 1;
754 if (data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
756 if (data && (data_fake.flags & SF_HAS_EVAL))
757 data->flags |= SF_HAS_EVAL;
759 data->whilem_c = data_fake.whilem_c;
760 if (flags & SCF_DO_STCLASS)
761 cl_or(pRExC_state, &accum, &this_class);
765 if (code == IFTHEN && num < 2) /* Empty ELSE branch */
767 if (flags & SCF_DO_SUBSTR) {
768 data->pos_min += min1;
769 data->pos_delta += max1 - min1;
770 if (max1 != min1 || is_inf)
771 data->longest = &(data->longest_float);
774 delta += max1 - min1;
775 if (flags & SCF_DO_STCLASS_OR) {
776 cl_or(pRExC_state, data->start_class, &accum);
778 cl_and(data->start_class, &and_with);
779 flags &= ~SCF_DO_STCLASS;
782 else if (flags & SCF_DO_STCLASS_AND) {
784 cl_and(data->start_class, &accum);
785 flags &= ~SCF_DO_STCLASS;
788 /* Switch to OR mode: cache the old value of
789 * data->start_class */
790 StructCopy(data->start_class, &and_with,
791 struct regnode_charclass_class);
792 flags &= ~SCF_DO_STCLASS_AND;
793 StructCopy(&accum, data->start_class,
794 struct regnode_charclass_class);
795 flags |= SCF_DO_STCLASS_OR;
796 data->start_class->flags |= ANYOF_EOS;
800 else if (code == BRANCHJ) /* single branch is optimized. */
801 scan = NEXTOPER(NEXTOPER(scan));
802 else /* single branch is optimized. */
803 scan = NEXTOPER(scan);
806 else if (OP(scan) == EXACT) {
807 I32 l = STR_LEN(scan);
808 UV uc = *((U8*)STRING(scan));
810 U8 *s = (U8*)STRING(scan);
811 l = utf8_length(s, s + l);
812 uc = utf8_to_uv_simple(s, NULL);
815 if (flags & SCF_DO_SUBSTR) { /* Update longest substr. */
816 /* The code below prefers earlier match for fixed
817 offset, later match for variable offset. */
818 if (data->last_end == -1) { /* Update the start info. */
819 data->last_start_min = data->pos_min;
820 data->last_start_max = is_inf
821 ? I32_MAX : data->pos_min + data->pos_delta;
823 sv_catpvn(data->last_found, STRING(scan), STR_LEN(scan));
824 data->last_end = data->pos_min + l;
825 data->pos_min += l; /* As in the first entry. */
826 data->flags &= ~SF_BEFORE_EOL;
828 if (flags & SCF_DO_STCLASS_AND) {
829 /* Check whether it is compatible with what we know already! */
833 !(data->start_class->flags & (ANYOF_CLASS | ANYOF_LOCALE))
834 && !ANYOF_BITMAP_TEST(data->start_class, uc)
835 && (!(data->start_class->flags & ANYOF_FOLD)
836 || !ANYOF_BITMAP_TEST(data->start_class, PL_fold[uc])))
838 ANYOF_CLASS_ZERO(data->start_class);
839 ANYOF_BITMAP_ZERO(data->start_class);
841 ANYOF_BITMAP_SET(data->start_class, uc);
842 data->start_class->flags &= ~ANYOF_EOS;
844 data->start_class->flags &= ~ANYOF_UNICODE_ALL;
846 else if (flags & SCF_DO_STCLASS_OR) {
847 /* false positive possible if the class is case-folded */
849 ANYOF_BITMAP_SET(data->start_class, uc);
851 data->start_class->flags |= ANYOF_UNICODE_ALL;
852 data->start_class->flags &= ~ANYOF_EOS;
853 cl_and(data->start_class, &and_with);
855 flags &= ~SCF_DO_STCLASS;
857 else if (PL_regkind[(U8)OP(scan)] == EXACT) { /* But OP != EXACT! */
858 I32 l = STR_LEN(scan);
859 UV uc = *((U8*)STRING(scan));
861 /* Search for fixed substrings supports EXACT only. */
862 if (flags & SCF_DO_SUBSTR)
863 scan_commit(pRExC_state, data);
865 U8 *s = (U8 *)STRING(scan);
866 l = utf8_length(s, s + l);
867 uc = utf8_to_uv_simple(s, NULL);
870 if (data && (flags & SCF_DO_SUBSTR))
872 if (flags & SCF_DO_STCLASS_AND) {
873 /* Check whether it is compatible with what we know already! */
877 !(data->start_class->flags & (ANYOF_CLASS | ANYOF_LOCALE))
878 && !ANYOF_BITMAP_TEST(data->start_class, uc)
879 && !ANYOF_BITMAP_TEST(data->start_class, PL_fold[uc]))
881 ANYOF_CLASS_ZERO(data->start_class);
882 ANYOF_BITMAP_ZERO(data->start_class);
884 ANYOF_BITMAP_SET(data->start_class, uc);
885 data->start_class->flags &= ~ANYOF_EOS;
886 data->start_class->flags |= ANYOF_FOLD;
887 if (OP(scan) == EXACTFL)
888 data->start_class->flags |= ANYOF_LOCALE;
891 else if (flags & SCF_DO_STCLASS_OR) {
892 if (data->start_class->flags & ANYOF_FOLD) {
893 /* false positive possible if the class is case-folded.
894 Assume that the locale settings are the same... */
896 ANYOF_BITMAP_SET(data->start_class, uc);
897 data->start_class->flags &= ~ANYOF_EOS;
899 cl_and(data->start_class, &and_with);
901 flags &= ~SCF_DO_STCLASS;
903 else if (strchr((char*)PL_varies,OP(scan))) {
904 I32 mincount, maxcount, minnext, deltanext, fl;
905 I32 f = flags, pos_before = 0;
906 regnode *oscan = scan;
907 struct regnode_charclass_class this_class;
908 struct regnode_charclass_class *oclass = NULL;
910 switch (PL_regkind[(U8)OP(scan)]) {
911 case WHILEM: /* End of (?:...)* . */
912 scan = NEXTOPER(scan);
915 if (flags & (SCF_DO_SUBSTR | SCF_DO_STCLASS)) {
916 next = NEXTOPER(scan);
917 if (OP(next) == EXACT || (flags & SCF_DO_STCLASS)) {
919 maxcount = REG_INFTY;
920 next = regnext(scan);
921 scan = NEXTOPER(scan);
925 if (flags & SCF_DO_SUBSTR)
930 if (flags & SCF_DO_STCLASS) {
932 maxcount = REG_INFTY;
933 next = regnext(scan);
934 scan = NEXTOPER(scan);
937 is_inf = is_inf_internal = 1;
938 scan = regnext(scan);
939 if (flags & SCF_DO_SUBSTR) {
940 scan_commit(pRExC_state, data); /* Cannot extend fixed substrings */
941 data->longest = &(data->longest_float);
943 goto optimize_curly_tail;
945 mincount = ARG1(scan);
946 maxcount = ARG2(scan);
947 next = regnext(scan);
948 if (OP(scan) == CURLYX) {
949 I32 lp = (data ? *(data->last_closep) : 0);
951 scan->flags = ((lp <= U8_MAX) ? lp : U8_MAX);
953 scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
955 if (flags & SCF_DO_SUBSTR) {
956 if (mincount == 0) scan_commit(pRExC_state,data); /* Cannot extend fixed substrings */
957 pos_before = data->pos_min;
961 data->flags &= ~(SF_HAS_PAR|SF_IN_PAR|SF_HAS_EVAL);
963 data->flags |= SF_IS_INF;
965 if (flags & SCF_DO_STCLASS) {
966 cl_init(pRExC_state, &this_class);
967 oclass = data->start_class;
968 data->start_class = &this_class;
969 f |= SCF_DO_STCLASS_AND;
970 f &= ~SCF_DO_STCLASS_OR;
972 /* These are the cases when once a subexpression
973 fails at a particular position, it cannot succeed
974 even after backtracking at the enclosing scope.
976 XXXX what if minimal match and we are at the
977 initial run of {n,m}? */
978 if ((mincount != maxcount - 1) && (maxcount != REG_INFTY))
979 f &= ~SCF_WHILEM_VISITED_POS;
981 /* This will finish on WHILEM, setting scan, or on NULL: */
982 minnext = study_chunk(pRExC_state, &scan, &deltanext, last, data,
984 ? (f & ~SCF_DO_SUBSTR) : f);
986 if (flags & SCF_DO_STCLASS)
987 data->start_class = oclass;
988 if (mincount == 0 || minnext == 0) {
989 if (flags & SCF_DO_STCLASS_OR) {
990 cl_or(pRExC_state, data->start_class, &this_class);
992 else if (flags & SCF_DO_STCLASS_AND) {
993 /* Switch to OR mode: cache the old value of
994 * data->start_class */
995 StructCopy(data->start_class, &and_with,
996 struct regnode_charclass_class);
997 flags &= ~SCF_DO_STCLASS_AND;
998 StructCopy(&this_class, data->start_class,
999 struct regnode_charclass_class);
1000 flags |= SCF_DO_STCLASS_OR;
1001 data->start_class->flags |= ANYOF_EOS;
1003 } else { /* Non-zero len */
1004 if (flags & SCF_DO_STCLASS_OR) {
1005 cl_or(pRExC_state, data->start_class, &this_class);
1006 cl_and(data->start_class, &and_with);
1008 else if (flags & SCF_DO_STCLASS_AND)
1009 cl_and(data->start_class, &this_class);
1010 flags &= ~SCF_DO_STCLASS;
1012 if (!scan) /* It was not CURLYX, but CURLY. */
1014 if (ckWARN(WARN_REGEXP) && (minnext + deltanext == 0)
1015 && !(data->flags & (SF_HAS_PAR|SF_IN_PAR))
1016 && maxcount <= REG_INFTY/3) /* Complement check for big count */
1019 "Quantifier unexpected on zero-length expression");
1022 min += minnext * mincount;
1023 is_inf_internal |= ((maxcount == REG_INFTY
1024 && (minnext + deltanext) > 0)
1025 || deltanext == I32_MAX);
1026 is_inf |= is_inf_internal;
1027 delta += (minnext + deltanext) * maxcount - minnext * mincount;
1029 /* Try powerful optimization CURLYX => CURLYN. */
1030 if ( OP(oscan) == CURLYX && data
1031 && data->flags & SF_IN_PAR
1032 && !(data->flags & SF_HAS_EVAL)
1033 && !deltanext && minnext == 1 ) {
1034 /* Try to optimize to CURLYN. */
1035 regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS;
1036 regnode *nxt1 = nxt, *nxt2;
1040 if (!strchr((char*)PL_simple,OP(nxt))
1041 && !(PL_regkind[(U8)OP(nxt)] == EXACT
1042 && STR_LEN(nxt) == 1))
1046 if (OP(nxt) != CLOSE)
1048 /* Now we know that nxt2 is the only contents: */
1049 oscan->flags = ARG(nxt);
1051 OP(nxt1) = NOTHING; /* was OPEN. */
1053 OP(nxt1 + 1) = OPTIMIZED; /* was count. */
1054 NEXT_OFF(nxt1+ 1) = 0; /* just for consistancy. */
1055 NEXT_OFF(nxt2) = 0; /* just for consistancy with CURLY. */
1056 OP(nxt) = OPTIMIZED; /* was CLOSE. */
1057 OP(nxt + 1) = OPTIMIZED; /* was count. */
1058 NEXT_OFF(nxt+ 1) = 0; /* just for consistancy. */
1063 /* Try optimization CURLYX => CURLYM. */
1064 if ( OP(oscan) == CURLYX && data
1065 && !(data->flags & SF_HAS_PAR)
1066 && !(data->flags & SF_HAS_EVAL)
1068 /* XXXX How to optimize if data == 0? */
1069 /* Optimize to a simpler form. */
1070 regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN */
1074 while ( (nxt2 = regnext(nxt)) /* skip over embedded stuff*/
1075 && (OP(nxt2) != WHILEM))
1077 OP(nxt2) = SUCCEED; /* Whas WHILEM */
1078 /* Need to optimize away parenths. */
1079 if (data->flags & SF_IN_PAR) {
1080 /* Set the parenth number. */
1081 regnode *nxt1 = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN*/
1083 if (OP(nxt) != CLOSE)
1084 FAIL("Panic opt close");
1085 oscan->flags = ARG(nxt);
1086 OP(nxt1) = OPTIMIZED; /* was OPEN. */
1087 OP(nxt) = OPTIMIZED; /* was CLOSE. */
1089 OP(nxt1 + 1) = OPTIMIZED; /* was count. */
1090 OP(nxt + 1) = OPTIMIZED; /* was count. */
1091 NEXT_OFF(nxt1 + 1) = 0; /* just for consistancy. */
1092 NEXT_OFF(nxt + 1) = 0; /* just for consistancy. */
1095 while ( nxt1 && (OP(nxt1) != WHILEM)) {
1096 regnode *nnxt = regnext(nxt1);
1099 if (reg_off_by_arg[OP(nxt1)])
1100 ARG_SET(nxt1, nxt2 - nxt1);
1101 else if (nxt2 - nxt1 < U16_MAX)
1102 NEXT_OFF(nxt1) = nxt2 - nxt1;
1104 OP(nxt) = NOTHING; /* Cannot beautify */
1109 /* Optimize again: */
1110 study_chunk(pRExC_state, &nxt1, &deltanext, nxt,
1116 else if ((OP(oscan) == CURLYX)
1117 && (flags & SCF_WHILEM_VISITED_POS)
1118 /* See the comment on a similar expression above.
1119 However, this time it not a subexpression
1120 we care about, but the expression itself. */
1121 && (maxcount == REG_INFTY)
1122 && data && ++data->whilem_c < 16) {
1123 /* This stays as CURLYX, we can put the count/of pair. */
1124 /* Find WHILEM (as in regexec.c) */
1125 regnode *nxt = oscan + NEXT_OFF(oscan);
1127 if (OP(PREVOPER(nxt)) == NOTHING) /* LONGJMP */
1129 PREVOPER(nxt)->flags = data->whilem_c
1130 | (RExC_whilem_seen << 4); /* On WHILEM */
1132 if (data && fl & (SF_HAS_PAR|SF_IN_PAR))
1134 if (flags & SCF_DO_SUBSTR) {
1135 SV *last_str = Nullsv;
1136 int counted = mincount != 0;
1138 if (data->last_end > 0 && mincount != 0) { /* Ends with a string. */
1139 I32 b = pos_before >= data->last_start_min
1140 ? pos_before : data->last_start_min;
1142 char *s = SvPV(data->last_found, l);
1143 I32 old = b - data->last_start_min;
1146 old = utf8_hop((U8*)s, old) - (U8*)s;
1149 /* Get the added string: */
1150 last_str = newSVpvn(s + old, l);
1151 if (deltanext == 0 && pos_before == b) {
1152 /* What was added is a constant string */
1154 SvGROW(last_str, (mincount * l) + 1);
1155 repeatcpy(SvPVX(last_str) + l,
1156 SvPVX(last_str), l, mincount - 1);
1157 SvCUR(last_str) *= mincount;
1158 /* Add additional parts. */
1159 SvCUR_set(data->last_found,
1160 SvCUR(data->last_found) - l);
1161 sv_catsv(data->last_found, last_str);
1162 data->last_end += l * (mincount - 1);
1165 /* start offset must point into the last copy */
1166 data->last_start_min += minnext * (mincount - 1);
1167 data->last_start_max += is_inf ? 0 : (maxcount - 1)
1168 * (minnext + data->pos_delta);
1171 /* It is counted once already... */
1172 data->pos_min += minnext * (mincount - counted);
1173 data->pos_delta += - counted * deltanext +
1174 (minnext + deltanext) * maxcount - minnext * mincount;
1175 if (mincount != maxcount) {
1176 /* Cannot extend fixed substrings found inside
1178 scan_commit(pRExC_state,data);
1179 if (mincount && last_str) {
1180 sv_setsv(data->last_found, last_str);
1181 data->last_end = data->pos_min;
1182 data->last_start_min =
1183 data->pos_min - CHR_SVLEN(last_str);
1184 data->last_start_max = is_inf
1186 : data->pos_min + data->pos_delta
1187 - CHR_SVLEN(last_str);
1189 data->longest = &(data->longest_float);
1191 SvREFCNT_dec(last_str);
1193 if (data && (fl & SF_HAS_EVAL))
1194 data->flags |= SF_HAS_EVAL;
1195 optimize_curly_tail:
1196 if (OP(oscan) != CURLYX) {
1197 while (PL_regkind[(U8)OP(next = regnext(oscan))] == NOTHING
1199 NEXT_OFF(oscan) += NEXT_OFF(next);
1202 default: /* REF and CLUMP only? */
1203 if (flags & SCF_DO_SUBSTR) {
1204 scan_commit(pRExC_state,data); /* Cannot expect anything... */
1205 data->longest = &(data->longest_float);
1207 is_inf = is_inf_internal = 1;
1208 if (flags & SCF_DO_STCLASS_OR)
1209 cl_anything(pRExC_state, data->start_class);
1210 flags &= ~SCF_DO_STCLASS;
1214 else if (strchr((char*)PL_simple,OP(scan))) {
1217 if (flags & SCF_DO_SUBSTR) {
1218 scan_commit(pRExC_state,data);
1222 if (flags & SCF_DO_STCLASS) {
1223 data->start_class->flags &= ~ANYOF_EOS; /* No match on empty */
1225 /* Some of the logic below assumes that switching
1226 locale on will only add false positives. */
1227 switch (PL_regkind[(U8)OP(scan)]) {
1231 /* Perl_croak(aTHX_ "panic: unexpected simple REx opcode %d", OP(scan)); */
1232 if (flags & SCF_DO_STCLASS_OR) /* Allow everything */
1233 cl_anything(pRExC_state, data->start_class);
1236 if (OP(scan) == SANY)
1238 if (flags & SCF_DO_STCLASS_OR) { /* Everything but \n */
1239 value = (ANYOF_BITMAP_TEST(data->start_class,'\n')
1240 || (data->start_class->flags & ANYOF_CLASS));
1241 cl_anything(pRExC_state, data->start_class);
1243 if (flags & SCF_DO_STCLASS_AND || !value)
1244 ANYOF_BITMAP_CLEAR(data->start_class,'\n');
1247 if (flags & SCF_DO_STCLASS_AND)
1248 cl_and(data->start_class,
1249 (struct regnode_charclass_class*)scan);
1251 cl_or(pRExC_state, data->start_class,
1252 (struct regnode_charclass_class*)scan);
1255 if (flags & SCF_DO_STCLASS_AND) {
1256 if (!(data->start_class->flags & ANYOF_LOCALE)) {
1257 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NALNUM);
1258 for (value = 0; value < 256; value++)
1259 if (!isALNUM(value))
1260 ANYOF_BITMAP_CLEAR(data->start_class, value);
1264 if (data->start_class->flags & ANYOF_LOCALE)
1265 ANYOF_CLASS_SET(data->start_class,ANYOF_ALNUM);
1267 for (value = 0; value < 256; value++)
1269 ANYOF_BITMAP_SET(data->start_class, value);
1274 if (flags & SCF_DO_STCLASS_AND) {
1275 if (data->start_class->flags & ANYOF_LOCALE)
1276 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NALNUM);
1279 ANYOF_CLASS_SET(data->start_class,ANYOF_ALNUM);
1280 data->start_class->flags |= ANYOF_LOCALE;
1284 if (flags & SCF_DO_STCLASS_AND) {
1285 if (!(data->start_class->flags & ANYOF_LOCALE)) {
1286 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_ALNUM);
1287 for (value = 0; value < 256; value++)
1289 ANYOF_BITMAP_CLEAR(data->start_class, value);
1293 if (data->start_class->flags & ANYOF_LOCALE)
1294 ANYOF_CLASS_SET(data->start_class,ANYOF_NALNUM);
1296 for (value = 0; value < 256; value++)
1297 if (!isALNUM(value))
1298 ANYOF_BITMAP_SET(data->start_class, value);
1303 if (flags & SCF_DO_STCLASS_AND) {
1304 if (data->start_class->flags & ANYOF_LOCALE)
1305 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_ALNUM);
1308 data->start_class->flags |= ANYOF_LOCALE;
1309 ANYOF_CLASS_SET(data->start_class,ANYOF_NALNUM);
1313 if (flags & SCF_DO_STCLASS_AND) {
1314 if (!(data->start_class->flags & ANYOF_LOCALE)) {
1315 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NSPACE);
1316 for (value = 0; value < 256; value++)
1317 if (!isSPACE(value))
1318 ANYOF_BITMAP_CLEAR(data->start_class, value);
1322 if (data->start_class->flags & ANYOF_LOCALE)
1323 ANYOF_CLASS_SET(data->start_class,ANYOF_SPACE);
1325 for (value = 0; value < 256; value++)
1327 ANYOF_BITMAP_SET(data->start_class, value);
1332 if (flags & SCF_DO_STCLASS_AND) {
1333 if (data->start_class->flags & ANYOF_LOCALE)
1334 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NSPACE);
1337 data->start_class->flags |= ANYOF_LOCALE;
1338 ANYOF_CLASS_SET(data->start_class,ANYOF_SPACE);
1342 if (flags & SCF_DO_STCLASS_AND) {
1343 if (!(data->start_class->flags & ANYOF_LOCALE)) {
1344 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_SPACE);
1345 for (value = 0; value < 256; value++)
1347 ANYOF_BITMAP_CLEAR(data->start_class, value);
1351 if (data->start_class->flags & ANYOF_LOCALE)
1352 ANYOF_CLASS_SET(data->start_class,ANYOF_NSPACE);
1354 for (value = 0; value < 256; value++)
1355 if (!isSPACE(value))
1356 ANYOF_BITMAP_SET(data->start_class, value);
1361 if (flags & SCF_DO_STCLASS_AND) {
1362 if (data->start_class->flags & ANYOF_LOCALE) {
1363 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_SPACE);
1364 for (value = 0; value < 256; value++)
1365 if (!isSPACE(value))
1366 ANYOF_BITMAP_CLEAR(data->start_class, value);
1370 data->start_class->flags |= ANYOF_LOCALE;
1371 ANYOF_CLASS_SET(data->start_class,ANYOF_NSPACE);
1375 if (flags & SCF_DO_STCLASS_AND) {
1376 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NDIGIT);
1377 for (value = 0; value < 256; value++)
1378 if (!isDIGIT(value))
1379 ANYOF_BITMAP_CLEAR(data->start_class, value);
1382 if (data->start_class->flags & ANYOF_LOCALE)
1383 ANYOF_CLASS_SET(data->start_class,ANYOF_DIGIT);
1385 for (value = 0; value < 256; value++)
1387 ANYOF_BITMAP_SET(data->start_class, value);
1392 if (flags & SCF_DO_STCLASS_AND) {
1393 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_DIGIT);
1394 for (value = 0; value < 256; value++)
1396 ANYOF_BITMAP_CLEAR(data->start_class, value);
1399 if (data->start_class->flags & ANYOF_LOCALE)
1400 ANYOF_CLASS_SET(data->start_class,ANYOF_NDIGIT);
1402 for (value = 0; value < 256; value++)
1403 if (!isDIGIT(value))
1404 ANYOF_BITMAP_SET(data->start_class, value);
1409 if (flags & SCF_DO_STCLASS_OR)
1410 cl_and(data->start_class, &and_with);
1411 flags &= ~SCF_DO_STCLASS;
1414 else if (PL_regkind[(U8)OP(scan)] == EOL && flags & SCF_DO_SUBSTR) {
1415 data->flags |= (OP(scan) == MEOL
1419 else if ( PL_regkind[(U8)OP(scan)] == BRANCHJ
1420 /* Lookbehind, or need to calculate parens/evals/stclass: */
1421 && (scan->flags || data || (flags & SCF_DO_STCLASS))
1422 && (OP(scan) == IFMATCH || OP(scan) == UNLESSM)) {
1423 /* Lookahead/lookbehind */
1424 I32 deltanext, minnext, fake = 0;
1426 struct regnode_charclass_class intrnl;
1429 data_fake.flags = 0;
1431 data_fake.whilem_c = data->whilem_c;
1432 data_fake.last_closep = data->last_closep;
1435 data_fake.last_closep = &fake;
1436 if ( flags & SCF_DO_STCLASS && !scan->flags
1437 && OP(scan) == IFMATCH ) { /* Lookahead */
1438 cl_init(pRExC_state, &intrnl);
1439 data_fake.start_class = &intrnl;
1440 f |= SCF_DO_STCLASS_AND;
1442 if (flags & SCF_WHILEM_VISITED_POS)
1443 f |= SCF_WHILEM_VISITED_POS;
1444 next = regnext(scan);
1445 nscan = NEXTOPER(NEXTOPER(scan));
1446 minnext = study_chunk(pRExC_state, &nscan, &deltanext, last, &data_fake, f);
1449 vFAIL("Variable length lookbehind not implemented");
1451 else if (minnext > U8_MAX) {
1452 vFAIL2("Lookbehind longer than %"UVuf" not implemented", (UV)U8_MAX);
1454 scan->flags = minnext;
1456 if (data && data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
1458 if (data && (data_fake.flags & SF_HAS_EVAL))
1459 data->flags |= SF_HAS_EVAL;
1461 data->whilem_c = data_fake.whilem_c;
1462 if (f & SCF_DO_STCLASS_AND) {
1463 int was = (data->start_class->flags & ANYOF_EOS);
1465 cl_and(data->start_class, &intrnl);
1467 data->start_class->flags |= ANYOF_EOS;
1470 else if (OP(scan) == OPEN) {
1473 else if (OP(scan) == CLOSE) {
1474 if (ARG(scan) == is_par) {
1475 next = regnext(scan);
1477 if ( next && (OP(next) != WHILEM) && next < last)
1478 is_par = 0; /* Disable optimization */
1481 *(data->last_closep) = ARG(scan);
1483 else if (OP(scan) == EVAL) {
1485 data->flags |= SF_HAS_EVAL;
1487 else if (OP(scan) == LOGICAL && scan->flags == 2) { /* Embedded follows */
1488 if (flags & SCF_DO_SUBSTR) {
1489 scan_commit(pRExC_state,data);
1490 data->longest = &(data->longest_float);
1492 is_inf = is_inf_internal = 1;
1493 if (flags & SCF_DO_STCLASS_OR) /* Allow everything */
1494 cl_anything(pRExC_state, data->start_class);
1495 flags &= ~SCF_DO_STCLASS;
1497 /* Else: zero-length, ignore. */
1498 scan = regnext(scan);
1503 *deltap = is_inf_internal ? I32_MAX : delta;
1504 if (flags & SCF_DO_SUBSTR && is_inf)
1505 data->pos_delta = I32_MAX - data->pos_min;
1506 if (is_par > U8_MAX)
1508 if (is_par && pars==1 && data) {
1509 data->flags |= SF_IN_PAR;
1510 data->flags &= ~SF_HAS_PAR;
1512 else if (pars && data) {
1513 data->flags |= SF_HAS_PAR;
1514 data->flags &= ~SF_IN_PAR;
1516 if (flags & SCF_DO_STCLASS_OR)
1517 cl_and(data->start_class, &and_with);
1522 S_add_data(pTHX_ RExC_state_t *pRExC_state, I32 n, char *s)
1524 if (RExC_rx->data) {
1525 Renewc(RExC_rx->data,
1526 sizeof(*RExC_rx->data) + sizeof(void*) * (RExC_rx->data->count + n - 1),
1527 char, struct reg_data);
1528 Renew(RExC_rx->data->what, RExC_rx->data->count + n, U8);
1529 RExC_rx->data->count += n;
1532 Newc(1207, RExC_rx->data, sizeof(*RExC_rx->data) + sizeof(void*) * (n - 1),
1533 char, struct reg_data);
1534 New(1208, RExC_rx->data->what, n, U8);
1535 RExC_rx->data->count = n;
1537 Copy(s, RExC_rx->data->what + RExC_rx->data->count - n, n, U8);
1538 return RExC_rx->data->count - n;
1542 Perl_reginitcolors(pTHX)
1545 char *s = PerlEnv_getenv("PERL_RE_COLORS");
1548 PL_colors[0] = s = savepv(s);
1550 s = strchr(s, '\t');
1556 PL_colors[i] = s = "";
1560 PL_colors[i++] = "";
1567 - pregcomp - compile a regular expression into internal code
1569 * We can't allocate space until we know how big the compiled form will be,
1570 * but we can't compile it (and thus know how big it is) until we've got a
1571 * place to put the code. So we cheat: we compile it twice, once with code
1572 * generation turned off and size counting turned on, and once "for real".
1573 * This also means that we don't allocate space until we are sure that the
1574 * thing really will compile successfully, and we never have to move the
1575 * code and thus invalidate pointers into it. (Note that it has to be in
1576 * one piece because free() must be able to free it all.) [NB: not true in perl]
1578 * Beware that the optimization-preparation code in here knows about some
1579 * of the structure of the compiled regexp. [I'll say.]
1582 Perl_pregcomp(pTHX_ char *exp, char *xend, PMOP *pm)
1592 RExC_state_t RExC_state;
1593 RExC_state_t *pRExC_state = &RExC_state;
1596 FAIL("NULL regexp argument");
1598 /* XXXX This looks very suspicious... */
1599 if (pm->op_pmdynflags & PMdf_CMP_UTF8)
1604 RExC_precomp = savepvn(exp, xend - exp);
1605 DEBUG_r(if (!PL_colorset) reginitcolors());
1606 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sCompiling REx%s `%s%*s%s'\n",
1607 PL_colors[4],PL_colors[5],PL_colors[0],
1608 (int)(xend - exp), RExC_precomp, PL_colors[1]));
1609 RExC_flags16 = pm->op_pmflags;
1613 RExC_seen_zerolen = *exp == '^' ? -1 : 0;
1614 RExC_seen_evals = 0;
1617 /* First pass: determine size, legality. */
1623 RExC_emit = &PL_regdummy;
1624 RExC_whilem_seen = 0;
1625 #if 0 /* REGC() is (currently) a NOP at the first pass.
1626 * Clever compilers notice this and complain. --jhi */
1627 REGC((U8)REG_MAGIC, (char*)RExC_emit);
1629 if (reg(pRExC_state, 0, &flags) == NULL) {
1630 Safefree(RExC_precomp);
1631 RExC_precomp = Nullch;
1634 DEBUG_r(PerlIO_printf(Perl_debug_log, "size %"IVdf" ", (IV)RExC_size));
1636 /* Small enough for pointer-storage convention?
1637 If extralen==0, this means that we will not need long jumps. */
1638 if (RExC_size >= 0x10000L && RExC_extralen)
1639 RExC_size += RExC_extralen;
1642 if (RExC_whilem_seen > 15)
1643 RExC_whilem_seen = 15;
1645 /* Allocate space and initialize. */
1646 Newc(1001, r, sizeof(regexp) + (unsigned)RExC_size * sizeof(regnode),
1649 FAIL("Regexp out of space");
1652 /* avoid reading uninitialized memory in DEBUGGING code in study_chunk() */
1653 Zero(r, sizeof(regexp) + (unsigned)RExC_size * sizeof(regnode), char);
1656 r->prelen = xend - exp;
1657 r->precomp = RExC_precomp;
1659 r->reganch = pm->op_pmflags & PMf_COMPILETIME;
1660 r->nparens = RExC_npar - 1; /* set early to validate backrefs */
1662 r->substrs = 0; /* Useful during FAIL. */
1663 r->startp = 0; /* Useful during FAIL. */
1664 r->endp = 0; /* Useful during FAIL. */
1668 /* Second pass: emit code. */
1673 RExC_emit = r->program;
1674 /* Store the count of eval-groups for security checks: */
1675 RExC_emit->next_off = ((RExC_seen_evals > U16_MAX) ? U16_MAX : RExC_seen_evals);
1676 REGC((U8)REG_MAGIC, (char*) RExC_emit++);
1678 if (reg(pRExC_state, 0, &flags) == NULL)
1681 /* Dig out information for optimizations. */
1682 r->reganch = pm->op_pmflags & PMf_COMPILETIME; /* Again? */
1683 pm->op_pmflags = RExC_flags16;
1685 r->reganch |= ROPT_UTF8;
1686 r->regstclass = NULL;
1687 if (RExC_naughty >= 10) /* Probably an expensive pattern. */
1688 r->reganch |= ROPT_NAUGHTY;
1689 scan = r->program + 1; /* First BRANCH. */
1691 /* XXXX To minimize changes to RE engine we always allocate
1692 3-units-long substrs field. */
1693 Newz(1004, r->substrs, 1, struct reg_substr_data);
1695 StructCopy(&zero_scan_data, &data, scan_data_t);
1696 /* XXXX Should not we check for something else? Usually it is OPEN1... */
1697 if (OP(scan) != BRANCH) { /* Only one top-level choice. */
1699 STRLEN longest_float_length, longest_fixed_length;
1700 struct regnode_charclass_class ch_class;
1705 /* Skip introductions and multiplicators >= 1. */
1706 while ((OP(first) == OPEN && (sawopen = 1)) ||
1707 /* An OR of *one* alternative - should not happen now. */
1708 (OP(first) == BRANCH && OP(regnext(first)) != BRANCH) ||
1709 (OP(first) == PLUS) ||
1710 (OP(first) == MINMOD) ||
1711 /* An {n,m} with n>0 */
1712 (PL_regkind[(U8)OP(first)] == CURLY && ARG1(first) > 0) ) {
1713 if (OP(first) == PLUS)
1716 first += regarglen[(U8)OP(first)];
1717 first = NEXTOPER(first);
1720 /* Starting-point info. */
1722 if (PL_regkind[(U8)OP(first)] == EXACT) {
1723 if (OP(first) == EXACT)
1724 ; /* Empty, get anchored substr later. */
1725 else if ((OP(first) == EXACTF || OP(first) == EXACTFL))
1726 r->regstclass = first;
1728 else if (strchr((char*)PL_simple,OP(first)))
1729 r->regstclass = first;
1730 else if (PL_regkind[(U8)OP(first)] == BOUND ||
1731 PL_regkind[(U8)OP(first)] == NBOUND)
1732 r->regstclass = first;
1733 else if (PL_regkind[(U8)OP(first)] == BOL) {
1734 r->reganch |= (OP(first) == MBOL
1736 : (OP(first) == SBOL
1739 first = NEXTOPER(first);
1742 else if (OP(first) == GPOS) {
1743 r->reganch |= ROPT_ANCH_GPOS;
1744 first = NEXTOPER(first);
1747 else if ((OP(first) == STAR &&
1748 PL_regkind[(U8)OP(NEXTOPER(first))] == REG_ANY) &&
1749 !(r->reganch & ROPT_ANCH) )
1751 /* turn .* into ^.* with an implied $*=1 */
1752 int type = OP(NEXTOPER(first));
1754 if (type == REG_ANY)
1755 type = ROPT_ANCH_MBOL;
1757 type = ROPT_ANCH_SBOL;
1759 r->reganch |= type | ROPT_IMPLICIT;
1760 first = NEXTOPER(first);
1763 if (sawplus && (!sawopen || !RExC_sawback)
1764 && !(RExC_seen & REG_SEEN_EVAL)) /* May examine pos and $& */
1765 /* x+ must match at the 1st pos of run of x's */
1766 r->reganch |= ROPT_SKIP;
1768 /* Scan is after the zeroth branch, first is atomic matcher. */
1769 DEBUG_r(PerlIO_printf(Perl_debug_log, "first at %"IVdf"\n",
1770 (IV)(first - scan + 1)));
1772 * If there's something expensive in the r.e., find the
1773 * longest literal string that must appear and make it the
1774 * regmust. Resolve ties in favor of later strings, since
1775 * the regstart check works with the beginning of the r.e.
1776 * and avoiding duplication strengthens checking. Not a
1777 * strong reason, but sufficient in the absence of others.
1778 * [Now we resolve ties in favor of the earlier string if
1779 * it happens that c_offset_min has been invalidated, since the
1780 * earlier string may buy us something the later one won't.]
1784 data.longest_fixed = newSVpvn("",0);
1785 data.longest_float = newSVpvn("",0);
1786 data.last_found = newSVpvn("",0);
1787 data.longest = &(data.longest_fixed);
1789 if (!r->regstclass) {
1790 cl_init(pRExC_state, &ch_class);
1791 data.start_class = &ch_class;
1792 stclass_flag = SCF_DO_STCLASS_AND;
1793 } else /* XXXX Check for BOUND? */
1795 data.last_closep = &last_close;
1797 minlen = study_chunk(pRExC_state, &first, &fake, scan + RExC_size, /* Up to end */
1798 &data, SCF_DO_SUBSTR | SCF_WHILEM_VISITED_POS | stclass_flag);
1799 if ( RExC_npar == 1 && data.longest == &(data.longest_fixed)
1800 && data.last_start_min == 0 && data.last_end > 0
1801 && !RExC_seen_zerolen
1802 && (!(RExC_seen & REG_SEEN_GPOS) || (r->reganch & ROPT_ANCH_GPOS)))
1803 r->reganch |= ROPT_CHECK_ALL;
1804 scan_commit(pRExC_state, &data);
1805 SvREFCNT_dec(data.last_found);
1807 longest_float_length = CHR_SVLEN(data.longest_float);
1808 if (longest_float_length
1809 || (data.flags & SF_FL_BEFORE_EOL
1810 && (!(data.flags & SF_FL_BEFORE_MEOL)
1811 || (RExC_flags16 & PMf_MULTILINE)))) {
1814 if (SvCUR(data.longest_fixed) /* ok to leave SvCUR */
1815 && data.offset_fixed == data.offset_float_min
1816 && SvCUR(data.longest_fixed) == SvCUR(data.longest_float))
1817 goto remove_float; /* As in (a)+. */
1819 r->float_substr = data.longest_float;
1820 r->float_min_offset = data.offset_float_min;
1821 r->float_max_offset = data.offset_float_max;
1822 t = (data.flags & SF_FL_BEFORE_EOL /* Can't have SEOL and MULTI */
1823 && (!(data.flags & SF_FL_BEFORE_MEOL)
1824 || (RExC_flags16 & PMf_MULTILINE)));
1825 fbm_compile(r->float_substr, t ? FBMcf_TAIL : 0);
1829 r->float_substr = Nullsv;
1830 SvREFCNT_dec(data.longest_float);
1831 longest_float_length = 0;
1834 longest_fixed_length = CHR_SVLEN(data.longest_fixed);
1835 if (longest_fixed_length
1836 || (data.flags & SF_FIX_BEFORE_EOL /* Cannot have SEOL and MULTI */
1837 && (!(data.flags & SF_FIX_BEFORE_MEOL)
1838 || (RExC_flags16 & PMf_MULTILINE)))) {
1841 r->anchored_substr = data.longest_fixed;
1842 r->anchored_offset = data.offset_fixed;
1843 t = (data.flags & SF_FIX_BEFORE_EOL /* Can't have SEOL and MULTI */
1844 && (!(data.flags & SF_FIX_BEFORE_MEOL)
1845 || (RExC_flags16 & PMf_MULTILINE)));
1846 fbm_compile(r->anchored_substr, t ? FBMcf_TAIL : 0);
1849 r->anchored_substr = Nullsv;
1850 SvREFCNT_dec(data.longest_fixed);
1851 longest_fixed_length = 0;
1854 && (OP(r->regstclass) == REG_ANY || OP(r->regstclass) == SANY))
1855 r->regstclass = NULL;
1856 if ((!r->anchored_substr || r->anchored_offset) && stclass_flag
1857 && !(data.start_class->flags & ANYOF_EOS)
1858 && !cl_is_anything(data.start_class)) {
1860 I32 n = add_data(pRExC_state, 1, "f");
1862 New(1006, RExC_rx->data->data[n], 1,
1863 struct regnode_charclass_class);
1864 StructCopy(data.start_class,
1865 (struct regnode_charclass_class*)RExC_rx->data->data[n],
1866 struct regnode_charclass_class);
1867 r->regstclass = (regnode*)RExC_rx->data->data[n];
1868 r->reganch &= ~ROPT_SKIP; /* Used in find_byclass(). */
1869 PL_regdata = r->data; /* for regprop() */
1870 DEBUG_r((sv = sv_newmortal(),
1871 regprop(sv, (regnode*)data.start_class),
1872 PerlIO_printf(Perl_debug_log, "synthetic stclass `%s'.\n",
1876 /* A temporary algorithm prefers floated substr to fixed one to dig more info. */
1877 if (longest_fixed_length > longest_float_length) {
1878 r->check_substr = r->anchored_substr;
1879 r->check_offset_min = r->check_offset_max = r->anchored_offset;
1880 if (r->reganch & ROPT_ANCH_SINGLE)
1881 r->reganch |= ROPT_NOSCAN;
1884 r->check_substr = r->float_substr;
1885 r->check_offset_min = data.offset_float_min;
1886 r->check_offset_max = data.offset_float_max;
1888 /* XXXX Currently intuiting is not compatible with ANCH_GPOS.
1889 This should be changed ASAP! */
1890 if (r->check_substr && !(r->reganch & ROPT_ANCH_GPOS)) {
1891 r->reganch |= RE_USE_INTUIT;
1892 if (SvTAIL(r->check_substr))
1893 r->reganch |= RE_INTUIT_TAIL;
1897 /* Several toplevels. Best we can is to set minlen. */
1899 struct regnode_charclass_class ch_class;
1902 DEBUG_r(PerlIO_printf(Perl_debug_log, "\n"));
1903 scan = r->program + 1;
1904 cl_init(pRExC_state, &ch_class);
1905 data.start_class = &ch_class;
1906 data.last_closep = &last_close;
1907 minlen = study_chunk(pRExC_state, &scan, &fake, scan + RExC_size, &data, SCF_DO_STCLASS_AND|SCF_WHILEM_VISITED_POS);
1908 r->check_substr = r->anchored_substr = r->float_substr = Nullsv;
1909 if (!(data.start_class->flags & ANYOF_EOS)
1910 && !cl_is_anything(data.start_class)) {
1912 I32 n = add_data(pRExC_state, 1, "f");
1914 New(1006, RExC_rx->data->data[n], 1,
1915 struct regnode_charclass_class);
1916 StructCopy(data.start_class,
1917 (struct regnode_charclass_class*)RExC_rx->data->data[n],
1918 struct regnode_charclass_class);
1919 r->regstclass = (regnode*)RExC_rx->data->data[n];
1920 r->reganch &= ~ROPT_SKIP; /* Used in find_byclass(). */
1921 DEBUG_r((sv = sv_newmortal(),
1922 regprop(sv, (regnode*)data.start_class),
1923 PerlIO_printf(Perl_debug_log, "synthetic stclass `%s'.\n",
1929 if (RExC_seen & REG_SEEN_GPOS)
1930 r->reganch |= ROPT_GPOS_SEEN;
1931 if (RExC_seen & REG_SEEN_LOOKBEHIND)
1932 r->reganch |= ROPT_LOOKBEHIND_SEEN;
1933 if (RExC_seen & REG_SEEN_EVAL)
1934 r->reganch |= ROPT_EVAL_SEEN;
1935 Newz(1002, r->startp, RExC_npar, I32);
1936 Newz(1002, r->endp, RExC_npar, I32);
1937 PL_regdata = r->data; /* for regprop() */
1938 DEBUG_r(regdump(r));
1943 - reg - regular expression, i.e. main body or parenthesized thing
1945 * Caller must absorb opening parenthesis.
1947 * Combining parenthesis handling with the base level of regular expression
1948 * is a trifle forced, but the need to tie the tails of the branches to what
1949 * follows makes it hard to avoid.
1952 S_reg(pTHX_ RExC_state_t *pRExC_state, I32 paren, I32 *flagp)
1953 /* paren: Parenthesized? 0=top, 1=(, inside: changed to letter. */
1955 register regnode *ret; /* Will be the head of the group. */
1956 register regnode *br;
1957 register regnode *lastbr;
1958 register regnode *ender = 0;
1959 register I32 parno = 0;
1960 I32 flags, oregflags = RExC_flags16, have_branch = 0, open = 0;
1961 char *oregcomp_parse = RExC_parse;
1964 *flagp = 0; /* Tentatively. */
1966 /* Make an OPEN node, if parenthesized. */
1968 if (*RExC_parse == '?') {
1969 U16 posflags = 0, negflags = 0;
1970 U16 *flagsp = &posflags;
1972 char *seqstart = RExC_parse;
1975 paren = *RExC_parse++;
1976 ret = NULL; /* For look-ahead/behind. */
1979 RExC_seen |= REG_SEEN_LOOKBEHIND;
1980 if (*RExC_parse == '!')
1982 if (*RExC_parse != '=' && *RExC_parse != '!')
1987 RExC_seen_zerolen++;
1993 vFAIL2("Sequence (?%c...) not implemented", (int)paren);
1996 while (*RExC_parse && *RExC_parse != ')')
1998 if (*RExC_parse != ')')
1999 FAIL("Sequence (?#... not terminated");
2000 nextchar(pRExC_state);
2005 vWARN(RExC_parse, "(?p{}) is deprecated - use (??{})");
2009 paren = *RExC_parse++;
2013 I32 count = 1, n = 0;
2015 char *s = RExC_parse;
2017 OP_4tree *sop, *rop;
2019 RExC_seen_zerolen++;
2020 RExC_seen |= REG_SEEN_EVAL;
2021 while (count && (c = *RExC_parse)) {
2022 if (c == '\\' && RExC_parse[1])
2030 if (*RExC_parse != ')')
2033 vFAIL("Sequence (?{...}) not terminated or not {}-balanced");
2038 if (RExC_parse - 1 - s)
2039 sv = newSVpvn(s, RExC_parse - 1 - s);
2041 sv = newSVpvn("", 0);
2044 Perl_save_re_context(aTHX);
2045 rop = sv_compile_2op(sv, &sop, "re", &av);
2048 n = add_data(pRExC_state, 3, "nop");
2049 RExC_rx->data->data[n] = (void*)rop;
2050 RExC_rx->data->data[n+1] = (void*)sop;
2051 RExC_rx->data->data[n+2] = (void*)av;
2054 else { /* First pass */
2055 if (PL_reginterp_cnt < ++RExC_seen_evals
2056 && PL_curcop != &PL_compiling)
2057 /* No compiled RE interpolated, has runtime
2058 components ===> unsafe. */
2059 FAIL("Eval-group not allowed at runtime, use re 'eval'");
2061 FAIL("Eval-group in insecure regular expression");
2064 nextchar(pRExC_state);
2066 ret = reg_node(pRExC_state, LOGICAL);
2069 regtail(pRExC_state, ret, reganode(pRExC_state, EVAL, n));
2072 return reganode(pRExC_state, EVAL, n);
2076 if (RExC_parse[0] == '?') {
2077 if (RExC_parse[1] == '=' || RExC_parse[1] == '!'
2078 || RExC_parse[1] == '<'
2079 || RExC_parse[1] == '{') { /* Lookahead or eval. */
2082 ret = reg_node(pRExC_state, LOGICAL);
2085 regtail(pRExC_state, ret, reg(pRExC_state, 1, &flag));
2089 else if (RExC_parse[0] >= '1' && RExC_parse[0] <= '9' ) {
2090 parno = atoi(RExC_parse++);
2092 while (isDIGIT(*RExC_parse))
2094 ret = reganode(pRExC_state, GROUPP, parno);
2095 if ((c = *nextchar(pRExC_state)) != ')')
2096 vFAIL("Switch condition not recognized");
2098 regtail(pRExC_state, ret, reganode(pRExC_state, IFTHEN, 0));
2099 br = regbranch(pRExC_state, &flags, 1);
2101 br = reganode(pRExC_state, LONGJMP, 0);
2103 regtail(pRExC_state, br, reganode(pRExC_state, LONGJMP, 0));
2104 c = *nextchar(pRExC_state);
2108 lastbr = reganode(pRExC_state, IFTHEN, 0); /* Fake one for optimizer. */
2109 regbranch(pRExC_state, &flags, 1);
2110 regtail(pRExC_state, ret, lastbr);
2113 c = *nextchar(pRExC_state);
2118 vFAIL("Switch (?(condition)... contains too many branches");
2119 ender = reg_node(pRExC_state, TAIL);
2120 regtail(pRExC_state, br, ender);
2122 regtail(pRExC_state, lastbr, ender);
2123 regtail(pRExC_state, NEXTOPER(NEXTOPER(lastbr)), ender);
2126 regtail(pRExC_state, ret, ender);
2130 vFAIL2("Unknown switch condition (?(%.2s", RExC_parse);
2134 RExC_parse--; /* for vFAIL to print correctly */
2135 vFAIL("Sequence (? incomplete");
2140 while (*RExC_parse && strchr("iogcmsx", *RExC_parse)) {
2141 if (*RExC_parse != 'o')
2142 pmflag(flagsp, *RExC_parse);
2145 if (*RExC_parse == '-') {
2150 RExC_flags16 |= posflags;
2151 RExC_flags16 &= ~negflags;
2152 if (*RExC_parse == ':') {
2158 if (*RExC_parse != ')') {
2160 vFAIL3("Sequence (%.*s...) not recognized", RExC_parse-seqstart, seqstart);
2162 nextchar(pRExC_state);
2170 ret = reganode(pRExC_state, OPEN, parno);
2177 /* Pick up the branches, linking them together. */
2178 br = regbranch(pRExC_state, &flags, 1);
2181 if (*RExC_parse == '|') {
2182 if (!SIZE_ONLY && RExC_extralen) {
2183 reginsert(pRExC_state, BRANCHJ, br);
2186 reginsert(pRExC_state, BRANCH, br);
2189 RExC_extralen += 1; /* For BRANCHJ-BRANCH. */
2191 else if (paren == ':') {
2192 *flagp |= flags&SIMPLE;
2194 if (open) { /* Starts with OPEN. */
2195 regtail(pRExC_state, ret, br); /* OPEN -> first. */
2197 else if (paren != '?') /* Not Conditional */
2201 *flagp |= flags&SPSTART;
2203 while (*RExC_parse == '|') {
2204 if (!SIZE_ONLY && RExC_extralen) {
2205 ender = reganode(pRExC_state, LONGJMP,0);
2206 regtail(pRExC_state, NEXTOPER(NEXTOPER(lastbr)), ender); /* Append to the previous. */
2209 RExC_extralen += 2; /* Account for LONGJMP. */
2210 nextchar(pRExC_state);
2211 br = regbranch(pRExC_state, &flags, 0);
2214 regtail(pRExC_state, lastbr, br); /* BRANCH -> BRANCH. */
2218 *flagp |= flags&SPSTART;
2221 if (have_branch || paren != ':') {
2222 /* Make a closing node, and hook it on the end. */
2225 ender = reg_node(pRExC_state, TAIL);
2228 ender = reganode(pRExC_state, CLOSE, parno);
2234 *flagp &= ~HASWIDTH;
2237 ender = reg_node(pRExC_state, SUCCEED);
2240 ender = reg_node(pRExC_state, END);
2243 regtail(pRExC_state, lastbr, ender);
2246 /* Hook the tails of the branches to the closing node. */
2247 for (br = ret; br != NULL; br = regnext(br)) {
2248 regoptail(pRExC_state, br, ender);
2255 static char parens[] = "=!<,>";
2257 if (paren && (p = strchr(parens, paren))) {
2258 int node = ((p - parens) % 2) ? UNLESSM : IFMATCH;
2259 int flag = (p - parens) > 1;
2262 node = SUSPEND, flag = 0;
2263 reginsert(pRExC_state, node,ret);
2265 regtail(pRExC_state, ret, reg_node(pRExC_state, TAIL));
2269 /* Check for proper termination. */
2271 RExC_flags16 = oregflags;
2272 if (RExC_parse >= RExC_end || *nextchar(pRExC_state) != ')') {
2273 RExC_parse = oregcomp_parse;
2274 vFAIL("Unmatched (");
2277 else if (!paren && RExC_parse < RExC_end) {
2278 if (*RExC_parse == ')') {
2280 vFAIL("Unmatched )");
2283 FAIL("Junk on end of regexp"); /* "Can't happen". */
2291 - regbranch - one alternative of an | operator
2293 * Implements the concatenation operator.
2296 S_regbranch(pTHX_ RExC_state_t *pRExC_state, I32 *flagp, I32 first)
2298 register regnode *ret;
2299 register regnode *chain = NULL;
2300 register regnode *latest;
2301 I32 flags = 0, c = 0;
2306 if (!SIZE_ONLY && RExC_extralen)
2307 ret = reganode(pRExC_state, BRANCHJ,0);
2309 ret = reg_node(pRExC_state, BRANCH);
2312 if (!first && SIZE_ONLY)
2313 RExC_extralen += 1; /* BRANCHJ */
2315 *flagp = WORST; /* Tentatively. */
2318 nextchar(pRExC_state);
2319 while (RExC_parse < RExC_end && *RExC_parse != '|' && *RExC_parse != ')') {
2321 latest = regpiece(pRExC_state, &flags);
2322 if (latest == NULL) {
2323 if (flags & TRYAGAIN)
2327 else if (ret == NULL)
2329 *flagp |= flags&HASWIDTH;
2330 if (chain == NULL) /* First piece. */
2331 *flagp |= flags&SPSTART;
2334 regtail(pRExC_state, chain, latest);
2339 if (chain == NULL) { /* Loop ran zero times. */
2340 chain = reg_node(pRExC_state, NOTHING);
2345 *flagp |= flags&SIMPLE;
2352 - regpiece - something followed by possible [*+?]
2354 * Note that the branching code sequences used for ? and the general cases
2355 * of * and + are somewhat optimized: they use the same NOTHING node as
2356 * both the endmarker for their branch list and the body of the last branch.
2357 * It might seem that this node could be dispensed with entirely, but the
2358 * endmarker role is not redundant.
2361 S_regpiece(pTHX_ RExC_state_t *pRExC_state, I32 *flagp)
2363 register regnode *ret;
2365 register char *next;
2367 char *origparse = RExC_parse;
2370 I32 max = REG_INFTY;
2372 ret = regatom(pRExC_state, &flags);
2374 if (flags & TRYAGAIN)
2381 if (op == '{' && regcurly(RExC_parse)) {
2382 next = RExC_parse + 1;
2384 while (isDIGIT(*next) || *next == ',') {
2393 if (*next == '}') { /* got one */
2397 min = atoi(RExC_parse);
2401 maxpos = RExC_parse;
2403 if (!max && *maxpos != '0')
2404 max = REG_INFTY; /* meaning "infinity" */
2405 else if (max >= REG_INFTY)
2406 vFAIL2("Quantifier in {,} bigger than %d", REG_INFTY - 1);
2408 nextchar(pRExC_state);
2411 if ((flags&SIMPLE)) {
2412 RExC_naughty += 2 + RExC_naughty / 2;
2413 reginsert(pRExC_state, CURLY, ret);
2416 regnode *w = reg_node(pRExC_state, WHILEM);
2419 regtail(pRExC_state, ret, w);
2420 if (!SIZE_ONLY && RExC_extralen) {
2421 reginsert(pRExC_state, LONGJMP,ret);
2422 reginsert(pRExC_state, NOTHING,ret);
2423 NEXT_OFF(ret) = 3; /* Go over LONGJMP. */
2425 reginsert(pRExC_state, CURLYX,ret);
2426 if (!SIZE_ONLY && RExC_extralen)
2427 NEXT_OFF(ret) = 3; /* Go over NOTHING to LONGJMP. */
2428 regtail(pRExC_state, ret, reg_node(pRExC_state, NOTHING));
2430 RExC_whilem_seen++, RExC_extralen += 3;
2431 RExC_naughty += 4 + RExC_naughty; /* compound interest */
2439 if (max && max < min)
2440 vFAIL("Can't do {n,m} with n > m");
2455 #if 0 /* Now runtime fix should be reliable. */
2457 /* if this is reinstated, don't forget to put this back into perldiag:
2459 =item Regexp *+ operand could be empty at {#} in regex m/%s/
2461 (F) The part of the regexp subject to either the * or + quantifier
2462 could match an empty string. The {#} shows in the regular
2463 expression about where the problem was discovered.
2467 if (!(flags&HASWIDTH) && op != '?')
2468 vFAIL("Regexp *+ operand could be empty");
2471 nextchar(pRExC_state);
2473 *flagp = (op != '+') ? (WORST|SPSTART|HASWIDTH) : (WORST|HASWIDTH);
2475 if (op == '*' && (flags&SIMPLE)) {
2476 reginsert(pRExC_state, STAR, ret);
2480 else if (op == '*') {
2484 else if (op == '+' && (flags&SIMPLE)) {
2485 reginsert(pRExC_state, PLUS, ret);
2489 else if (op == '+') {
2493 else if (op == '?') {
2498 if (ckWARN(WARN_REGEXP) && !SIZE_ONLY && !(flags&HASWIDTH) && max > REG_INFTY/3) {
2500 "%.*s matches null string many times",
2501 RExC_parse - origparse,
2505 if (*RExC_parse == '?') {
2506 nextchar(pRExC_state);
2507 reginsert(pRExC_state, MINMOD, ret);
2508 regtail(pRExC_state, ret, ret + NODE_STEP_REGNODE);
2510 if (ISMULT2(RExC_parse)) {
2512 vFAIL("Nested quantifiers");
2519 - regatom - the lowest level
2521 * Optimization: gobbles an entire sequence of ordinary characters so that
2522 * it can turn them into a single node, which is smaller to store and
2523 * faster to run. Backslashed characters are exceptions, each becoming a
2524 * separate node; the code is simpler that way and it's not worth fixing.
2526 * [Yes, it is worth fixing, some scripts can run twice the speed.] */
2528 S_regatom(pTHX_ RExC_state_t *pRExC_state, I32 *flagp)
2530 register regnode *ret = 0;
2533 *flagp = WORST; /* Tentatively. */
2536 switch (*RExC_parse) {
2538 RExC_seen_zerolen++;
2539 nextchar(pRExC_state);
2540 if (RExC_flags16 & PMf_MULTILINE)
2541 ret = reg_node(pRExC_state, MBOL);
2542 else if (RExC_flags16 & PMf_SINGLELINE)
2543 ret = reg_node(pRExC_state, SBOL);
2545 ret = reg_node(pRExC_state, BOL);
2548 nextchar(pRExC_state);
2550 RExC_seen_zerolen++;
2551 if (RExC_flags16 & PMf_MULTILINE)
2552 ret = reg_node(pRExC_state, MEOL);
2553 else if (RExC_flags16 & PMf_SINGLELINE)
2554 ret = reg_node(pRExC_state, SEOL);
2556 ret = reg_node(pRExC_state, EOL);
2559 nextchar(pRExC_state);
2560 if (RExC_flags16 & PMf_SINGLELINE)
2561 ret = reg_node(pRExC_state, SANY);
2563 ret = reg_node(pRExC_state, REG_ANY);
2564 *flagp |= HASWIDTH|SIMPLE;
2569 char *oregcomp_parse = ++RExC_parse;
2570 ret = regclass(pRExC_state);
2571 if (*RExC_parse != ']') {
2572 RExC_parse = oregcomp_parse;
2573 vFAIL("Unmatched [");
2575 nextchar(pRExC_state);
2576 *flagp |= HASWIDTH|SIMPLE;
2580 nextchar(pRExC_state);
2581 ret = reg(pRExC_state, 1, &flags);
2583 if (flags & TRYAGAIN) {
2584 if (RExC_parse == RExC_end) {
2585 /* Make parent create an empty node if needed. */
2593 *flagp |= flags&(HASWIDTH|SPSTART|SIMPLE);
2597 if (flags & TRYAGAIN) {
2601 vFAIL("Internal urp");
2602 /* Supposed to be caught earlier. */
2605 if (!regcurly(RExC_parse)) {
2614 vFAIL("Quantifier follows nothing");
2617 switch (*++RExC_parse) {
2619 RExC_seen_zerolen++;
2620 ret = reg_node(pRExC_state, SBOL);
2622 nextchar(pRExC_state);
2625 ret = reg_node(pRExC_state, GPOS);
2626 RExC_seen |= REG_SEEN_GPOS;
2628 nextchar(pRExC_state);
2631 ret = reg_node(pRExC_state, SEOL);
2633 nextchar(pRExC_state);
2636 ret = reg_node(pRExC_state, EOS);
2638 RExC_seen_zerolen++; /* Do not optimize RE away */
2639 nextchar(pRExC_state);
2642 ret = reg_node(pRExC_state, SANY);
2643 *flagp |= HASWIDTH|SIMPLE;
2644 nextchar(pRExC_state);
2647 ret = reg_node(pRExC_state, CLUMP);
2649 nextchar(pRExC_state);
2650 if (UTF && !PL_utf8_mark)
2651 is_utf8_mark((U8*)"~"); /* preload table */
2654 ret = reg_node(pRExC_state, LOC ? ALNUML : ALNUM);
2655 *flagp |= HASWIDTH|SIMPLE;
2656 nextchar(pRExC_state);
2657 if (UTF && !PL_utf8_alnum)
2658 is_utf8_alnum((U8*)"a"); /* preload table */
2661 ret = reg_node(pRExC_state, LOC ? NALNUML : NALNUM);
2662 *flagp |= HASWIDTH|SIMPLE;
2663 nextchar(pRExC_state);
2664 if (UTF && !PL_utf8_alnum)
2665 is_utf8_alnum((U8*)"a"); /* preload table */
2668 RExC_seen_zerolen++;
2669 RExC_seen |= REG_SEEN_LOOKBEHIND;
2670 ret = reg_node(pRExC_state, LOC ? BOUNDL : BOUND);
2672 nextchar(pRExC_state);
2673 if (UTF && !PL_utf8_alnum)
2674 is_utf8_alnum((U8*)"a"); /* preload table */
2677 RExC_seen_zerolen++;
2678 RExC_seen |= REG_SEEN_LOOKBEHIND;
2679 ret = reg_node(pRExC_state, LOC ? NBOUNDL : NBOUND);
2681 nextchar(pRExC_state);
2682 if (UTF && !PL_utf8_alnum)
2683 is_utf8_alnum((U8*)"a"); /* preload table */
2686 ret = reg_node(pRExC_state, LOC ? SPACEL : SPACE);
2687 *flagp |= HASWIDTH|SIMPLE;
2688 nextchar(pRExC_state);
2689 if (UTF && !PL_utf8_space)
2690 is_utf8_space((U8*)" "); /* preload table */
2693 ret = reg_node(pRExC_state, LOC ? NSPACEL : NSPACE);
2694 *flagp |= HASWIDTH|SIMPLE;
2695 nextchar(pRExC_state);
2696 if (UTF && !PL_utf8_space)
2697 is_utf8_space((U8*)" "); /* preload table */
2700 ret = reg_node(pRExC_state, DIGIT);
2701 *flagp |= HASWIDTH|SIMPLE;
2702 nextchar(pRExC_state);
2703 if (UTF && !PL_utf8_digit)
2704 is_utf8_digit((U8*)"1"); /* preload table */
2707 ret = reg_node(pRExC_state, NDIGIT);
2708 *flagp |= HASWIDTH|SIMPLE;
2709 nextchar(pRExC_state);
2710 if (UTF && !PL_utf8_digit)
2711 is_utf8_digit((U8*)"1"); /* preload table */
2715 { /* a lovely hack--pretend we saw [\pX] instead */
2716 char* oldregxend = RExC_end;
2718 if (RExC_parse[1] == '{') {
2719 RExC_end = strchr(RExC_parse, '}');
2722 RExC_end = oldregxend;
2723 vFAIL("Missing right brace on \\p{}");
2728 RExC_end = RExC_parse + 2;
2731 ret = regclass(pRExC_state);
2733 RExC_end = oldregxend;
2735 nextchar(pRExC_state);
2736 *flagp |= HASWIDTH|SIMPLE;
2749 case '1': case '2': case '3': case '4':
2750 case '5': case '6': case '7': case '8': case '9':
2752 I32 num = atoi(RExC_parse);
2754 if (num > 9 && num >= RExC_npar)
2757 while (isDIGIT(*RExC_parse))
2760 if (!SIZE_ONLY && num > RExC_rx->nparens)
2761 vFAIL("Reference to nonexistent group");
2763 ret = reganode(pRExC_state, FOLD
2764 ? (LOC ? REFFL : REFF)
2768 nextchar(pRExC_state);
2773 if (RExC_parse >= RExC_end)
2774 FAIL("Trailing \\");
2777 /* Do not generate `unrecognized' warnings here, we fall
2778 back into the quick-grab loop below */
2784 if (RExC_flags16 & PMf_EXTENDED) {
2785 while (RExC_parse < RExC_end && *RExC_parse != '\n') RExC_parse++;
2786 if (RExC_parse < RExC_end)
2792 register STRLEN len;
2801 ret = reg_node(pRExC_state, FOLD
2802 ? (LOC ? EXACTFL : EXACTF)
2805 for (len = 0, p = RExC_parse - 1;
2806 len < 127 && p < RExC_end;
2811 if (RExC_flags16 & PMf_EXTENDED)
2812 p = regwhite(p, RExC_end);
2874 char* e = strchr(p, '}');
2878 vFAIL("Missing right brace on \\x{}");
2881 numlen = 1; /* allow underscores */
2882 ender = (UV)scan_hex(p + 1, e - p - 1, &numlen);
2883 /* numlen is generous */
2884 if (numlen + len >= 127) {
2892 numlen = 0; /* disallow underscores */
2893 ender = (UV)scan_hex(p, 2, &numlen);
2899 ender = UCHARAT(p++);
2900 ender = toCTRL(ender);
2902 case '0': case '1': case '2': case '3':case '4':
2903 case '5': case '6': case '7': case '8':case '9':
2905 (isDIGIT(p[1]) && atoi(p) >= RExC_npar) ) {
2906 numlen = 0; /* disallow underscores */
2907 ender = (UV)scan_oct(p, 3, &numlen);
2917 FAIL("Trailing \\");
2920 if (!SIZE_ONLY && ckWARN(WARN_REGEXP) && isALPHA(*p))
2921 vWARN2(p +1, "Unrecognized escape \\%c passed through", *p);
2922 goto normal_default;
2927 if (UTF8_IS_START(*p) && UTF) {
2928 ender = utf8_to_uv((U8*)p, RExC_end - p,
2936 if (RExC_flags16 & PMf_EXTENDED)
2937 p = regwhite(p, RExC_end);
2940 ender = toLOWER_LC_uni(ender);
2942 ender = toLOWER_uni(ender);
2944 if (ISMULT2(p)) { /* Back off on ?+*. */
2947 /* ender is a Unicode value so it can be > 0xff --
2948 * in other words, do not use UTF8_IS_CONTINUED(). */
2949 else if (ender >= 0x80 && UTF) {
2950 reguni(pRExC_state, ender, s, &numlen);
2960 /* ender is a Unicode value so it can be > 0xff --
2961 * in other words, do not use UTF8_IS_CONTINUED(). */
2962 if (ender >= 0x80 && UTF) {
2963 reguni(pRExC_state, ender, s, &numlen);
2972 nextchar(pRExC_state);
2974 /* len is STRLEN which is unsigned, need to copy to signed */
2977 vFAIL("Internal disaster");
2986 RExC_size += STR_SZ(len);
2988 RExC_emit += STR_SZ(len);
2997 S_regwhite(pTHX_ char *p, char *e)
3002 else if (*p == '#') {
3005 } while (p < e && *p != '\n');
3013 /* Parse POSIX character classes: [[:foo:]], [[=foo=]], [[.foo.]].
3014 Character classes ([:foo:]) can also be negated ([:^foo:]).
3015 Returns a named class id (ANYOF_XXX) if successful, -1 otherwise.
3016 Equivalence classes ([=foo=]) and composites ([.foo.]) are parsed,
3017 but trigger warnings because they are currently unimplemented. */
3019 S_regpposixcc(pTHX_ RExC_state_t *pRExC_state, I32 value)
3022 I32 namedclass = OOB_NAMEDCLASS;
3024 if (value == '[' && RExC_parse + 1 < RExC_end &&
3025 /* I smell either [: or [= or [. -- POSIX has been here, right? */
3026 (*RExC_parse == ':' ||
3027 *RExC_parse == '=' ||
3028 *RExC_parse == '.')) {
3029 char c = *RExC_parse;
3030 char* s = RExC_parse++;
3032 while (RExC_parse < RExC_end && *RExC_parse != c)
3034 if (RExC_parse == RExC_end)
3035 /* Grandfather lone [:, [=, [. */
3038 char* t = RExC_parse++; /* skip over the c */
3040 if (*RExC_parse == ']') {
3041 RExC_parse++; /* skip over the ending ] */
3044 I32 complement = *posixcc == '^' ? *posixcc++ : 0;
3045 I32 skip = 5; /* the most common skip */
3049 if (strnEQ(posixcc, "alnum", 5))
3051 complement ? ANYOF_NALNUMC : ANYOF_ALNUMC;
3052 else if (strnEQ(posixcc, "alpha", 5))
3054 complement ? ANYOF_NALPHA : ANYOF_ALPHA;
3055 else if (strnEQ(posixcc, "ascii", 5))
3057 complement ? ANYOF_NASCII : ANYOF_ASCII;
3060 if (strnEQ(posixcc, "blank", 5))
3062 complement ? ANYOF_NBLANK : ANYOF_BLANK;
3065 if (strnEQ(posixcc, "cntrl", 5))
3067 complement ? ANYOF_NCNTRL : ANYOF_CNTRL;
3070 if (strnEQ(posixcc, "digit", 5))
3072 complement ? ANYOF_NDIGIT : ANYOF_DIGIT;
3075 if (strnEQ(posixcc, "graph", 5))
3077 complement ? ANYOF_NGRAPH : ANYOF_GRAPH;
3080 if (strnEQ(posixcc, "lower", 5))
3082 complement ? ANYOF_NLOWER : ANYOF_LOWER;
3085 if (strnEQ(posixcc, "print", 5))
3087 complement ? ANYOF_NPRINT : ANYOF_PRINT;
3088 else if (strnEQ(posixcc, "punct", 5))
3090 complement ? ANYOF_NPUNCT : ANYOF_PUNCT;
3093 if (strnEQ(posixcc, "space", 5))
3095 complement ? ANYOF_NPSXSPC : ANYOF_PSXSPC;
3098 if (strnEQ(posixcc, "upper", 5))
3100 complement ? ANYOF_NUPPER : ANYOF_UPPER;
3102 case 'w': /* this is not POSIX, this is the Perl \w */
3103 if (strnEQ(posixcc, "word", 4)) {
3105 complement ? ANYOF_NALNUM : ANYOF_ALNUM;
3110 if (strnEQ(posixcc, "xdigit", 6)) {
3112 complement ? ANYOF_NXDIGIT : ANYOF_XDIGIT;
3117 if (namedclass == OOB_NAMEDCLASS ||
3118 posixcc[skip] != ':' ||
3119 posixcc[skip+1] != ']')
3121 Simple_vFAIL3("POSIX class [:%.*s:] unknown",
3124 } else if (!SIZE_ONLY) {
3125 /* [[=foo=]] and [[.foo.]] are still future. */
3127 /* adjust RExC_parse so the warning shows after
3129 while (*RExC_parse && *RExC_parse != ']')
3131 Simple_vFAIL3("POSIX syntax [%c %c] is reserved for future extensions", c, c);
3134 /* Maternal grandfather:
3135 * "[:" ending in ":" but not in ":]" */
3145 S_checkposixcc(pTHX_ RExC_state_t *pRExC_state)
3147 if (!SIZE_ONLY && ckWARN(WARN_REGEXP) &&
3148 (*RExC_parse == ':' ||
3149 *RExC_parse == '=' ||
3150 *RExC_parse == '.')) {
3151 char *s = RExC_parse;
3154 while(*s && isALNUM(*s))
3156 if (*s && c == *s && s[1] == ']') {
3157 vWARN3(s+2, "POSIX syntax [%c %c] belongs inside character classes", c, c);
3159 /* [[=foo=]] and [[.foo.]] are still future. */
3160 if (c == '=' || c == '.')
3162 /* adjust RExC_parse so the error shows after
3164 while (*RExC_parse && *RExC_parse++ != ']')
3166 Simple_vFAIL3("POSIX syntax [%c %c] is reserved for future extensions", c, c);
3173 S_regclass(pTHX_ RExC_state_t *pRExC_state)
3176 register IV lastvalue = OOB_UNICODE;
3177 register IV range = 0;
3178 register regnode *ret;
3182 bool need_class = 0;
3186 bool dont_optimize_invert = FALSE;
3188 ret = reganode(pRExC_state, ANYOF, 0);
3191 ANYOF_FLAGS(ret) = 0;
3193 if (*RExC_parse == '^') { /* Complement of range. */
3197 ANYOF_FLAGS(ret) |= ANYOF_INVERT;
3201 RExC_size += ANYOF_SKIP;
3203 RExC_emit += ANYOF_SKIP;
3205 ANYOF_FLAGS(ret) |= ANYOF_FOLD;
3207 ANYOF_FLAGS(ret) |= ANYOF_LOCALE;
3208 ANYOF_BITMAP_ZERO(ret);
3209 listsv = newSVpvn("# comment\n", 10);
3212 if (!SIZE_ONLY && ckWARN(WARN_REGEXP))
3213 checkposixcc(pRExC_state);
3215 if (*RExC_parse == ']' || *RExC_parse == '-')
3216 goto charclassloop; /* allow 1st char to be ] or - */
3218 while (RExC_parse < RExC_end && *RExC_parse != ']') {
3222 namedclass = OOB_NAMEDCLASS; /* initialize as illegal */
3225 rangebegin = RExC_parse;
3227 value = utf8_to_uv((U8*)RExC_parse,
3228 RExC_end - RExC_parse,
3230 RExC_parse += numlen;
3233 value = UCHARAT(RExC_parse++);
3235 namedclass = regpposixcc(pRExC_state, value);
3236 else if (value == '\\') {
3238 value = utf8_to_uv((U8*)RExC_parse,
3239 RExC_end - RExC_parse,
3241 RExC_parse += numlen;
3244 value = UCHARAT(RExC_parse++);
3245 /* Some compilers cannot handle switching on 64-bit integer
3246 * values, therefore value cannot be an UV. Yes, this will
3247 * be a problem later if we want switch on Unicode.
3248 * A similar issue a little bit later when switching on
3249 * namedclass. --jhi */
3250 switch ((I32)value) {
3251 case 'w': namedclass = ANYOF_ALNUM; break;
3252 case 'W': namedclass = ANYOF_NALNUM; break;
3253 case 's': namedclass = ANYOF_SPACE; break;
3254 case 'S': namedclass = ANYOF_NSPACE; break;
3255 case 'd': namedclass = ANYOF_DIGIT; break;
3256 case 'D': namedclass = ANYOF_NDIGIT; break;
3259 if (*RExC_parse == '{') {
3260 e = strchr(RExC_parse++, '}');
3262 vFAIL("Missing right brace on \\p{}");
3271 Perl_sv_catpvf(aTHX_ listsv,
3272 "+utf8::%.*s\n", (int)n, RExC_parse);
3274 Perl_sv_catpvf(aTHX_ listsv,
3275 "!utf8::%.*s\n", (int)n, RExC_parse);
3278 ANYOF_FLAGS(ret) |= ANYOF_UNICODE;
3280 case 'n': value = '\n'; break;
3281 case 'r': value = '\r'; break;
3282 case 't': value = '\t'; break;
3283 case 'f': value = '\f'; break;
3284 case 'b': value = '\b'; break;
3286 case 'e': value = '\033'; break;
3287 case 'a': value = '\007'; break;
3289 case 'e': value = '\047'; break;
3290 case 'a': value = '\057'; break;
3293 if (*RExC_parse == '{') {
3294 e = strchr(RExC_parse++, '}');
3296 vFAIL("Missing right brace on \\x{}");
3297 numlen = 1; /* allow underscores */
3298 value = (UV)scan_hex(RExC_parse,
3304 numlen = 0; /* disallow underscores */
3305 value = (UV)scan_hex(RExC_parse, 2, &numlen);
3306 RExC_parse += numlen;
3310 value = UCHARAT(RExC_parse++);
3311 value = toCTRL(value);
3313 case '0': case '1': case '2': case '3': case '4':
3314 case '5': case '6': case '7': case '8': case '9':
3315 numlen = 0; /* disallow underscores */
3316 value = (UV)scan_oct(--RExC_parse, 3, &numlen);
3317 RExC_parse += numlen;
3320 if (!SIZE_ONLY && ckWARN(WARN_REGEXP) && isALPHA(value))
3322 "Unrecognized escape \\%c in character class passed through",
3326 } /* end of \blah */
3328 if (namedclass > OOB_NAMEDCLASS) { /* this is a named class \blah */
3330 if (!SIZE_ONLY && !need_class)
3331 ANYOF_CLASS_ZERO(ret);
3335 /* a bad range like a-\d, a-[:digit:] ? */
3338 if (ckWARN(WARN_REGEXP))
3340 "False [] range \"%*.*s\"",
3341 RExC_parse - rangebegin,
3342 RExC_parse - rangebegin,
3344 if (lastvalue < 256) {
3345 ANYOF_BITMAP_SET(ret, lastvalue);
3346 ANYOF_BITMAP_SET(ret, '-');
3349 ANYOF_FLAGS(ret) |= ANYOF_UNICODE;
3350 Perl_sv_catpvf(aTHX_ listsv,
3351 /* 0x002D is Unicode for '-' */
3352 "%04"UVxf"\n002D\n", (UV)lastvalue);
3356 range = 0; /* this was not a true range */
3360 /* Possible truncation here but in some 64-bit environments
3361 * the compiler gets heartburn about switch on 64-bit values.
3362 * A similar issue a little earlier when switching on value.
3364 switch ((I32)namedclass) {
3367 ANYOF_CLASS_SET(ret, ANYOF_ALNUM);
3369 for (value = 0; value < 256; value++)
3371 ANYOF_BITMAP_SET(ret, value);
3373 dont_optimize_invert = TRUE;
3374 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsWord\n");
3378 ANYOF_CLASS_SET(ret, ANYOF_NALNUM);
3380 for (value = 0; value < 256; value++)
3381 if (!isALNUM(value))
3382 ANYOF_BITMAP_SET(ret, value);
3384 dont_optimize_invert = TRUE;
3385 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsWord\n");
3389 ANYOF_CLASS_SET(ret, ANYOF_ALNUMC);
3391 for (value = 0; value < 256; value++)
3392 if (isALNUMC(value))
3393 ANYOF_BITMAP_SET(ret, value);
3395 dont_optimize_invert = TRUE;
3396 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsAlnum\n");
3400 ANYOF_CLASS_SET(ret, ANYOF_NALNUMC);
3402 for (value = 0; value < 256; value++)
3403 if (!isALNUMC(value))
3404 ANYOF_BITMAP_SET(ret, value);
3406 dont_optimize_invert = TRUE;
3407 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsAlnum\n");
3411 ANYOF_CLASS_SET(ret, ANYOF_ALPHA);
3413 for (value = 0; value < 256; value++)
3415 ANYOF_BITMAP_SET(ret, value);
3417 dont_optimize_invert = TRUE;
3418 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsAlpha\n");
3422 ANYOF_CLASS_SET(ret, ANYOF_NALPHA);
3424 for (value = 0; value < 256; value++)
3425 if (!isALPHA(value))
3426 ANYOF_BITMAP_SET(ret, value);
3428 dont_optimize_invert = TRUE;
3429 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsAlpha\n");
3433 ANYOF_CLASS_SET(ret, ANYOF_ASCII);
3436 for (value = 0; value < 128; value++)
3437 ANYOF_BITMAP_SET(ret, value);
3439 for (value = 0; value < 256; value++)
3441 ANYOF_BITMAP_SET(ret, value);
3444 dont_optimize_invert = TRUE;
3445 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsASCII\n");
3449 ANYOF_CLASS_SET(ret, ANYOF_NASCII);
3452 for (value = 128; value < 256; value++)
3453 ANYOF_BITMAP_SET(ret, value);
3455 for (value = 0; value < 256; value++)
3456 if (!isASCII(value))
3457 ANYOF_BITMAP_SET(ret, value);
3460 dont_optimize_invert = TRUE;
3461 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsASCII\n");
3465 ANYOF_CLASS_SET(ret, ANYOF_BLANK);
3467 for (value = 0; value < 256; value++)
3469 ANYOF_BITMAP_SET(ret, value);
3471 dont_optimize_invert = TRUE;
3472 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsBlank\n");
3476 ANYOF_CLASS_SET(ret, ANYOF_NBLANK);
3478 for (value = 0; value < 256; value++)
3479 if (!isBLANK(value))
3480 ANYOF_BITMAP_SET(ret, value);
3482 dont_optimize_invert = TRUE;
3483 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsBlank\n");
3487 ANYOF_CLASS_SET(ret, ANYOF_CNTRL);
3489 for (value = 0; value < 256; value++)
3491 ANYOF_BITMAP_SET(ret, value);
3493 dont_optimize_invert = TRUE;
3494 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsCntrl\n");
3498 ANYOF_CLASS_SET(ret, ANYOF_NCNTRL);
3500 for (value = 0; value < 256; value++)
3501 if (!isCNTRL(value))
3502 ANYOF_BITMAP_SET(ret, value);
3504 dont_optimize_invert = TRUE;
3505 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsCntrl\n");
3509 ANYOF_CLASS_SET(ret, ANYOF_DIGIT);
3511 /* consecutive digits assumed */
3512 for (value = '0'; value <= '9'; value++)
3513 ANYOF_BITMAP_SET(ret, value);
3515 dont_optimize_invert = TRUE;
3516 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsDigit\n");
3520 ANYOF_CLASS_SET(ret, ANYOF_NDIGIT);
3522 /* consecutive digits assumed */
3523 for (value = 0; value < '0'; value++)
3524 ANYOF_BITMAP_SET(ret, value);
3525 for (value = '9' + 1; value < 256; value++)
3526 ANYOF_BITMAP_SET(ret, value);
3528 dont_optimize_invert = TRUE;
3529 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsDigit\n");
3533 ANYOF_CLASS_SET(ret, ANYOF_GRAPH);
3535 for (value = 0; value < 256; value++)
3537 ANYOF_BITMAP_SET(ret, value);
3539 dont_optimize_invert = TRUE;
3540 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsGraph\n");
3544 ANYOF_CLASS_SET(ret, ANYOF_NGRAPH);
3546 for (value = 0; value < 256; value++)
3547 if (!isGRAPH(value))
3548 ANYOF_BITMAP_SET(ret, value);
3550 dont_optimize_invert = TRUE;
3551 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsGraph\n");
3555 ANYOF_CLASS_SET(ret, ANYOF_LOWER);
3557 for (value = 0; value < 256; value++)
3559 ANYOF_BITMAP_SET(ret, value);
3561 dont_optimize_invert = TRUE;
3562 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsLower\n");
3566 ANYOF_CLASS_SET(ret, ANYOF_NLOWER);
3568 for (value = 0; value < 256; value++)
3569 if (!isLOWER(value))
3570 ANYOF_BITMAP_SET(ret, value);
3572 dont_optimize_invert = TRUE;
3573 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsLower\n");
3577 ANYOF_CLASS_SET(ret, ANYOF_PRINT);
3579 for (value = 0; value < 256; value++)
3581 ANYOF_BITMAP_SET(ret, value);
3583 dont_optimize_invert = TRUE;
3584 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsPrint\n");
3588 ANYOF_CLASS_SET(ret, ANYOF_NPRINT);
3590 for (value = 0; value < 256; value++)
3591 if (!isPRINT(value))
3592 ANYOF_BITMAP_SET(ret, value);
3594 dont_optimize_invert = TRUE;
3595 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsPrint\n");
3599 ANYOF_CLASS_SET(ret, ANYOF_PSXSPC);
3601 for (value = 0; value < 256; value++)
3602 if (isPSXSPC(value))
3603 ANYOF_BITMAP_SET(ret, value);
3605 dont_optimize_invert = TRUE;
3606 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsSpace\n");
3610 ANYOF_CLASS_SET(ret, ANYOF_NPSXSPC);
3612 for (value = 0; value < 256; value++)
3613 if (!isPSXSPC(value))
3614 ANYOF_BITMAP_SET(ret, value);
3616 dont_optimize_invert = TRUE;
3617 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsSpace\n");
3621 ANYOF_CLASS_SET(ret, ANYOF_PUNCT);
3623 for (value = 0; value < 256; value++)
3625 ANYOF_BITMAP_SET(ret, value);
3627 dont_optimize_invert = TRUE;
3628 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsPunct\n");
3632 ANYOF_CLASS_SET(ret, ANYOF_NPUNCT);
3634 for (value = 0; value < 256; value++)
3635 if (!isPUNCT(value))
3636 ANYOF_BITMAP_SET(ret, value);
3638 dont_optimize_invert = TRUE;
3639 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsPunct\n");
3643 ANYOF_CLASS_SET(ret, ANYOF_SPACE);
3645 for (value = 0; value < 256; value++)
3647 ANYOF_BITMAP_SET(ret, value);
3649 dont_optimize_invert = TRUE;
3650 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsSpacePerl\n");
3654 ANYOF_CLASS_SET(ret, ANYOF_NSPACE);
3656 for (value = 0; value < 256; value++)
3657 if (!isSPACE(value))
3658 ANYOF_BITMAP_SET(ret, value);
3660 dont_optimize_invert = TRUE;
3661 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsSpacePerl\n");
3665 ANYOF_CLASS_SET(ret, ANYOF_UPPER);
3667 for (value = 0; value < 256; value++)
3669 ANYOF_BITMAP_SET(ret, value);
3671 dont_optimize_invert = TRUE;
3672 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsUpper\n");
3676 ANYOF_CLASS_SET(ret, ANYOF_NUPPER);
3678 for (value = 0; value < 256; value++)
3679 if (!isUPPER(value))
3680 ANYOF_BITMAP_SET(ret, value);
3682 dont_optimize_invert = TRUE;
3683 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsUpper\n");
3687 ANYOF_CLASS_SET(ret, ANYOF_XDIGIT);
3689 for (value = 0; value < 256; value++)
3690 if (isXDIGIT(value))
3691 ANYOF_BITMAP_SET(ret, value);
3693 dont_optimize_invert = TRUE;
3694 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsXDigit\n");
3698 ANYOF_CLASS_SET(ret, ANYOF_NXDIGIT);
3700 for (value = 0; value < 256; value++)
3701 if (!isXDIGIT(value))
3702 ANYOF_BITMAP_SET(ret, value);
3704 dont_optimize_invert = TRUE;
3705 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsXDigit\n");
3708 vFAIL("Invalid [::] class");
3712 ANYOF_FLAGS(ret) |= ANYOF_CLASS;
3715 } /* end of namedclass \blah */
3718 if (lastvalue > value) /* b-a */ {
3719 Simple_vFAIL4("Invalid [] range \"%*.*s\"",
3720 RExC_parse - rangebegin,
3721 RExC_parse - rangebegin,
3724 range = 0; /* not a true range */
3727 lastvalue = value; /* save the beginning of the range */
3728 if (*RExC_parse == '-' && RExC_parse+1 < RExC_end &&
3729 RExC_parse[1] != ']') {
3732 /* a bad range like \w-, [:word:]- ? */
3733 if (namedclass > OOB_NAMEDCLASS) {
3734 if (ckWARN(WARN_REGEXP))
3736 "False [] range \"%*.*s\"",
3737 RExC_parse - rangebegin,
3738 RExC_parse - rangebegin,
3741 ANYOF_BITMAP_SET(ret, '-');
3743 range = 1; /* yeah, it's a range! */
3744 continue; /* but do it the next time */
3748 /* now is the next time */
3750 if (lastvalue < 256 && value < 256) {
3751 #ifndef ASCIIish /* EBCDIC, for example. */
3752 if ((isLOWER(lastvalue) && isLOWER(value)) ||
3753 (isUPPER(lastvalue) && isUPPER(value)))
3756 if (isLOWER(lastvalue)) {
3757 for (i = lastvalue; i <= value; i++)
3759 ANYOF_BITMAP_SET(ret, i);
3761 for (i = lastvalue; i <= value; i++)
3763 ANYOF_BITMAP_SET(ret, i);
3768 for ( ; lastvalue <= value; lastvalue++)
3769 ANYOF_BITMAP_SET(ret, lastvalue);
3771 ANYOF_FLAGS(ret) |= ANYOF_UNICODE;
3772 if (lastvalue < value)
3773 Perl_sv_catpvf(aTHX_ listsv, "%04"UVxf"\t%04"UVxf"\n",
3774 (UV)lastvalue, (UV)value);
3776 Perl_sv_catpvf(aTHX_ listsv, "%04"UVxf"\n",
3781 range = 0; /* this range (if it was one) is done now */
3786 RExC_size += ANYOF_CLASS_ADD_SKIP;
3788 RExC_emit += ANYOF_CLASS_ADD_SKIP;
3791 /* optimize case-insensitive simple patterns (e.g. /[a-z]/i) */
3794 /* If the only flag is folding (plus possibly inversion). */
3795 (ANYOF_FLAGS_ALL ^ ANYOF_INVERT) == ANYOF_FOLD)) {
3796 for (value = 0; value < 256; ++value) {
3797 if (ANYOF_BITMAP_TEST(ret, value)) {
3798 IV fold = PL_fold[value];
3801 ANYOF_BITMAP_SET(ret, fold);
3804 ANYOF_FLAGS(ret) &= ~ANYOF_FOLD;
3807 /* optimize inverted simple patterns (e.g. [^a-z]) */
3808 if (!SIZE_ONLY && !dont_optimize_invert &&
3809 /* If the only flag is inversion. */
3810 (ANYOF_FLAGS(ret) & ANYOF_FLAGS_ALL) == ANYOF_INVERT) {
3811 for (value = 0; value < ANYOF_BITMAP_SIZE; ++value)
3812 ANYOF_BITMAP(ret)[value] ^= ANYOF_FLAGS_ALL;
3813 ANYOF_FLAGS(ret) = ANYOF_UNICODE_ALL;
3820 av_store(av, 0, listsv);
3821 av_store(av, 1, NULL);
3822 rv = newRV_noinc((SV*)av);
3823 n = add_data(pRExC_state, 1, "s");
3824 RExC_rx->data->data[n] = (void*)rv;
3832 S_nextchar(pTHX_ RExC_state_t *pRExC_state)
3834 char* retval = RExC_parse++;
3837 if (*RExC_parse == '(' && RExC_parse[1] == '?' &&
3838 RExC_parse[2] == '#') {
3839 while (*RExC_parse && *RExC_parse != ')')
3844 if (RExC_flags16 & PMf_EXTENDED) {
3845 if (isSPACE(*RExC_parse)) {
3849 else if (*RExC_parse == '#') {
3850 while (*RExC_parse && *RExC_parse != '\n')
3861 - reg_node - emit a node
3863 STATIC regnode * /* Location. */
3864 S_reg_node(pTHX_ RExC_state_t *pRExC_state, U8 op)
3866 register regnode *ret;
3867 register regnode *ptr;
3871 SIZE_ALIGN(RExC_size);
3876 NODE_ALIGN_FILL(ret);
3878 FILL_ADVANCE_NODE(ptr, op);
3885 - reganode - emit a node with an argument
3887 STATIC regnode * /* Location. */
3888 S_reganode(pTHX_ RExC_state_t *pRExC_state, U8 op, U32 arg)
3890 register regnode *ret;
3891 register regnode *ptr;
3895 SIZE_ALIGN(RExC_size);
3900 NODE_ALIGN_FILL(ret);
3902 FILL_ADVANCE_NODE_ARG(ptr, op, arg);
3909 - reguni - emit (if appropriate) a Unicode character
3912 S_reguni(pTHX_ RExC_state_t *pRExC_state, UV uv, char* s, STRLEN* lenp)
3914 *lenp = SIZE_ONLY ? UNISKIP(uv) : (uv_to_utf8((U8*)s, uv) - (U8*)s);
3918 - reginsert - insert an operator in front of already-emitted operand
3920 * Means relocating the operand.
3923 S_reginsert(pTHX_ RExC_state_t *pRExC_state, U8 op, regnode *opnd)
3925 register regnode *src;
3926 register regnode *dst;
3927 register regnode *place;
3928 register int offset = regarglen[(U8)op];
3930 /* (PL_regkind[(U8)op] == CURLY ? EXTRA_STEP_2ARGS : 0); */
3933 RExC_size += NODE_STEP_REGNODE + offset;
3938 RExC_emit += NODE_STEP_REGNODE + offset;
3941 StructCopy(--src, --dst, regnode);
3943 place = opnd; /* Op node, where operand used to be. */
3944 src = NEXTOPER(place);
3945 FILL_ADVANCE_NODE(place, op);
3946 Zero(src, offset, regnode);
3950 - regtail - set the next-pointer at the end of a node chain of p to val.
3953 S_regtail(pTHX_ RExC_state_t *pRExC_state, regnode *p, regnode *val)
3955 register regnode *scan;
3956 register regnode *temp;
3961 /* Find last node. */
3964 temp = regnext(scan);
3970 if (reg_off_by_arg[OP(scan)]) {
3971 ARG_SET(scan, val - scan);
3974 NEXT_OFF(scan) = val - scan;
3979 - regoptail - regtail on operand of first argument; nop if operandless
3982 S_regoptail(pTHX_ RExC_state_t *pRExC_state, regnode *p, regnode *val)
3984 /* "Operandless" and "op != BRANCH" are synonymous in practice. */
3985 if (p == NULL || SIZE_ONLY)
3987 if (PL_regkind[(U8)OP(p)] == BRANCH) {
3988 regtail(pRExC_state, NEXTOPER(p), val);
3990 else if ( PL_regkind[(U8)OP(p)] == BRANCHJ) {
3991 regtail(pRExC_state, NEXTOPER(NEXTOPER(p)), val);
3998 - regcurly - a little FSA that accepts {\d+,?\d*}
4001 S_regcurly(pTHX_ register char *s)
4020 S_dumpuntil(pTHX_ regnode *start, regnode *node, regnode *last, SV* sv, I32 l)
4023 register U8 op = EXACT; /* Arbitrary non-END op. */
4024 register regnode *next;
4026 while (op != END && (!last || node < last)) {
4027 /* While that wasn't END last time... */
4033 next = regnext(node);
4035 if (OP(node) == OPTIMIZED)
4038 PerlIO_printf(Perl_debug_log, "%4"IVdf":%*s%s", (IV)(node - start),
4039 (int)(2*l + 1), "", SvPVX(sv));
4040 if (next == NULL) /* Next ptr. */
4041 PerlIO_printf(Perl_debug_log, "(0)");
4043 PerlIO_printf(Perl_debug_log, "(%"IVdf")", (IV)(next - start));
4044 (void)PerlIO_putc(Perl_debug_log, '\n');
4046 if (PL_regkind[(U8)op] == BRANCHJ) {
4047 register regnode *nnode = (OP(next) == LONGJMP
4050 if (last && nnode > last)
4052 node = dumpuntil(start, NEXTOPER(NEXTOPER(node)), nnode, sv, l + 1);
4054 else if (PL_regkind[(U8)op] == BRANCH) {
4055 node = dumpuntil(start, NEXTOPER(node), next, sv, l + 1);
4057 else if ( op == CURLY) { /* `next' might be very big: optimizer */
4058 node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
4059 NEXTOPER(node) + EXTRA_STEP_2ARGS + 1, sv, l + 1);
4061 else if (PL_regkind[(U8)op] == CURLY && op != CURLYX) {
4062 node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
4065 else if ( op == PLUS || op == STAR) {
4066 node = dumpuntil(start, NEXTOPER(node), NEXTOPER(node) + 1, sv, l + 1);
4068 else if (op == ANYOF) {
4069 node = NEXTOPER(node);
4072 else if (PL_regkind[(U8)op] == EXACT) {
4073 /* Literal string, where present. */
4074 node += NODE_SZ_STR(node) - 1;
4075 node = NEXTOPER(node);
4078 node = NEXTOPER(node);
4079 node += regarglen[(U8)op];
4081 if (op == CURLYX || op == OPEN)
4083 else if (op == WHILEM)
4086 #endif /* DEBUGGING */
4091 - regdump - dump a regexp onto Perl_debug_log in vaguely comprehensible form
4094 Perl_regdump(pTHX_ regexp *r)
4097 SV *sv = sv_newmortal();
4099 (void)dumpuntil(r->program, r->program + 1, NULL, sv, 0);
4101 /* Header fields of interest. */
4102 if (r->anchored_substr)
4103 PerlIO_printf(Perl_debug_log,
4104 "anchored `%s%.*s%s'%s at %"IVdf" ",
4106 (int)(SvCUR(r->anchored_substr) - (SvTAIL(r->anchored_substr)!=0)),
4107 SvPVX(r->anchored_substr),
4109 SvTAIL(r->anchored_substr) ? "$" : "",
4110 (IV)r->anchored_offset);
4111 if (r->float_substr)
4112 PerlIO_printf(Perl_debug_log,
4113 "floating `%s%.*s%s'%s at %"IVdf"..%"UVuf" ",
4115 (int)(SvCUR(r->float_substr) - (SvTAIL(r->float_substr)!=0)),
4116 SvPVX(r->float_substr),
4118 SvTAIL(r->float_substr) ? "$" : "",
4119 (IV)r->float_min_offset, (UV)r->float_max_offset);
4120 if (r->check_substr)
4121 PerlIO_printf(Perl_debug_log,
4122 r->check_substr == r->float_substr
4123 ? "(checking floating" : "(checking anchored");
4124 if (r->reganch & ROPT_NOSCAN)
4125 PerlIO_printf(Perl_debug_log, " noscan");
4126 if (r->reganch & ROPT_CHECK_ALL)
4127 PerlIO_printf(Perl_debug_log, " isall");
4128 if (r->check_substr)
4129 PerlIO_printf(Perl_debug_log, ") ");
4131 if (r->regstclass) {
4132 regprop(sv, r->regstclass);
4133 PerlIO_printf(Perl_debug_log, "stclass `%s' ", SvPVX(sv));
4135 if (r->reganch & ROPT_ANCH) {
4136 PerlIO_printf(Perl_debug_log, "anchored");
4137 if (r->reganch & ROPT_ANCH_BOL)
4138 PerlIO_printf(Perl_debug_log, "(BOL)");
4139 if (r->reganch & ROPT_ANCH_MBOL)
4140 PerlIO_printf(Perl_debug_log, "(MBOL)");
4141 if (r->reganch & ROPT_ANCH_SBOL)
4142 PerlIO_printf(Perl_debug_log, "(SBOL)");
4143 if (r->reganch & ROPT_ANCH_GPOS)
4144 PerlIO_printf(Perl_debug_log, "(GPOS)");
4145 PerlIO_putc(Perl_debug_log, ' ');
4147 if (r->reganch & ROPT_GPOS_SEEN)
4148 PerlIO_printf(Perl_debug_log, "GPOS ");
4149 if (r->reganch & ROPT_SKIP)
4150 PerlIO_printf(Perl_debug_log, "plus ");
4151 if (r->reganch & ROPT_IMPLICIT)
4152 PerlIO_printf(Perl_debug_log, "implicit ");
4153 PerlIO_printf(Perl_debug_log, "minlen %ld ", (long) r->minlen);
4154 if (r->reganch & ROPT_EVAL_SEEN)
4155 PerlIO_printf(Perl_debug_log, "with eval ");
4156 PerlIO_printf(Perl_debug_log, "\n");
4157 #endif /* DEBUGGING */
4161 S_put_byte(pTHX_ SV *sv, int c)
4163 if (isCNTRL(c) || c == 127 || c == 255 || !isPRINT(c))
4164 Perl_sv_catpvf(aTHX_ sv, "\\%o", c);
4165 else if (c == '-' || c == ']' || c == '\\' || c == '^')
4166 Perl_sv_catpvf(aTHX_ sv, "\\%c", c);
4168 Perl_sv_catpvf(aTHX_ sv, "%c", c);
4172 - regprop - printable representation of opcode
4175 Perl_regprop(pTHX_ SV *sv, regnode *o)
4180 sv_setpvn(sv, "", 0);
4181 if (OP(o) >= reg_num) /* regnode.type is unsigned */
4182 /* It would be nice to FAIL() here, but this may be called from
4183 regexec.c, and it would be hard to supply pRExC_state. */
4184 Perl_croak(aTHX_ "Corrupted regexp opcode");
4185 sv_catpv(sv, (char*)reg_name[OP(o)]); /* Take off const! */
4187 k = PL_regkind[(U8)OP(o)];
4190 Perl_sv_catpvf(aTHX_ sv, " <%s%.*s%s>", PL_colors[0],
4191 STR_LEN(o), STRING(o), PL_colors[1]);
4192 else if (k == CURLY) {
4193 if (OP(o) == CURLYM || OP(o) == CURLYN || OP(o) == CURLYX)
4194 Perl_sv_catpvf(aTHX_ sv, "[%d]", o->flags); /* Parenth number */
4195 Perl_sv_catpvf(aTHX_ sv, " {%d,%d}", ARG1(o), ARG2(o));
4197 else if (k == WHILEM && o->flags) /* Ordinal/of */
4198 Perl_sv_catpvf(aTHX_ sv, "[%d/%d]", o->flags & 0xf, o->flags>>4);
4199 else if (k == REF || k == OPEN || k == CLOSE || k == GROUPP )
4200 Perl_sv_catpvf(aTHX_ sv, "%d", (int)ARG(o)); /* Parenth number */
4201 else if (k == LOGICAL)
4202 Perl_sv_catpvf(aTHX_ sv, "[%d]", o->flags); /* 2: embedded, otherwise 1 */
4203 else if (k == ANYOF) {
4204 int i, rangestart = -1;
4205 U8 flags = ANYOF_FLAGS(o);
4206 const char * const anyofs[] = { /* Should be syncronized with
4207 * ANYOF_ #xdefines in regcomp.h */
4240 if (flags & ANYOF_LOCALE)
4241 sv_catpv(sv, "{loc}");
4242 if (flags & ANYOF_FOLD)
4243 sv_catpv(sv, "{i}");
4244 Perl_sv_catpvf(aTHX_ sv, "[%s", PL_colors[0]);
4245 if (flags & ANYOF_INVERT)
4247 for (i = 0; i <= 256; i++) {
4248 if (i < 256 && ANYOF_BITMAP_TEST(o,i)) {
4249 if (rangestart == -1)
4251 } else if (rangestart != -1) {
4252 if (i <= rangestart + 3)
4253 for (; rangestart < i; rangestart++)
4254 put_byte(sv, rangestart);
4256 put_byte(sv, rangestart);
4258 put_byte(sv, i - 1);
4264 if (o->flags & ANYOF_CLASS)
4265 for (i = 0; i < sizeof(anyofs)/sizeof(char*); i++)
4266 if (ANYOF_CLASS_TEST(o,i))
4267 sv_catpv(sv, anyofs[i]);
4269 if (flags & ANYOF_UNICODE)
4270 sv_catpv(sv, "{unicode}");
4271 else if (flags & ANYOF_UNICODE_ALL)
4272 sv_catpv(sv, "{all-unicode}");
4276 SV *sw = regclass_swash(o, FALSE, &lv);
4281 U8 s[UTF8_MAXLEN+1];
4283 for (i = 0; i <= 256; i++) { /* just the first 256 */
4284 U8 *e = uv_to_utf8(s, i);
4286 if (i < 256 && swash_fetch(sw, s)) {
4287 if (rangestart == -1)
4289 } else if (rangestart != -1) {
4292 if (i <= rangestart + 3)
4293 for (; rangestart < i; rangestart++) {
4294 for(e = uv_to_utf8(s, rangestart), p = s; p < e; p++)
4298 for (e = uv_to_utf8(s, rangestart), p = s; p < e; p++)
4301 for (e = uv_to_utf8(s, i - 1), p = s; p < e; p++)
4308 sv_catpv(sv, "..."); /* et cetera */
4312 char *s = savepv(SvPVX(lv));
4315 while(*s && *s != '\n') s++;
4336 Perl_sv_catpvf(aTHX_ sv, "%s]", PL_colors[1]);
4338 else if (k == BRANCHJ && (OP(o) == UNLESSM || OP(o) == IFMATCH))
4339 Perl_sv_catpvf(aTHX_ sv, "[-%d]", o->flags);
4340 #endif /* DEBUGGING */
4344 Perl_re_intuit_string(pTHX_ regexp *prog)
4345 { /* Assume that RE_INTUIT is set */
4348 char *s = SvPV(prog->check_substr,n_a);
4350 if (!PL_colorset) reginitcolors();
4351 PerlIO_printf(Perl_debug_log,
4352 "%sUsing REx substr:%s `%s%.60s%s%s'\n",
4353 PL_colors[4],PL_colors[5],PL_colors[0],
4356 (strlen(s) > 60 ? "..." : ""));
4359 return prog->check_substr;
4363 Perl_pregfree(pTHX_ struct regexp *r)
4365 DEBUG_r(if (!PL_colorset) reginitcolors());
4367 if (!r || (--r->refcnt > 0))
4369 DEBUG_r(PerlIO_printf(Perl_debug_log,
4370 "%sFreeing REx:%s `%s%.60s%s%s'\n",
4371 PL_colors[4],PL_colors[5],PL_colors[0],
4374 (strlen(r->precomp) > 60 ? "..." : "")));
4377 Safefree(r->precomp);
4378 if (RX_MATCH_COPIED(r))
4379 Safefree(r->subbeg);
4381 if (r->anchored_substr)
4382 SvREFCNT_dec(r->anchored_substr);
4383 if (r->float_substr)
4384 SvREFCNT_dec(r->float_substr);
4385 Safefree(r->substrs);
4388 int n = r->data->count;
4389 AV* new_comppad = NULL;
4394 switch (r->data->what[n]) {
4396 SvREFCNT_dec((SV*)r->data->data[n]);
4399 Safefree(r->data->data[n]);
4402 new_comppad = (AV*)r->data->data[n];
4405 if (new_comppad == NULL)
4406 Perl_croak(aTHX_ "panic: pregfree comppad");
4407 old_comppad = PL_comppad;
4408 old_curpad = PL_curpad;
4409 /* Watch out for global destruction's random ordering. */
4410 if (SvTYPE(new_comppad) == SVt_PVAV) {
4411 PL_comppad = new_comppad;
4412 PL_curpad = AvARRAY(new_comppad);
4416 op_free((OP_4tree*)r->data->data[n]);
4417 PL_comppad = old_comppad;
4418 PL_curpad = old_curpad;
4419 SvREFCNT_dec((SV*)new_comppad);
4425 Perl_croak(aTHX_ "panic: regfree data code '%c'", r->data->what[n]);
4428 Safefree(r->data->what);
4431 Safefree(r->startp);
4437 - regnext - dig the "next" pointer out of a node
4439 * [Note, when REGALIGN is defined there are two places in regmatch()
4440 * that bypass this code for speed.]
4443 Perl_regnext(pTHX_ register regnode *p)
4445 register I32 offset;
4447 if (p == &PL_regdummy)
4450 offset = (reg_off_by_arg[OP(p)] ? ARG(p) : NEXT_OFF(p));
4458 S_re_croak2(pTHX_ const char* pat1,const char* pat2,...)
4461 STRLEN l1 = strlen(pat1);
4462 STRLEN l2 = strlen(pat2);
4471 Copy(pat1, buf, l1 , char);
4472 Copy(pat2, buf + l1, l2 , char);
4473 buf[l1 + l2] = '\n';
4474 buf[l1 + l2 + 1] = '\0';
4476 /* ANSI variant takes additional second argument */
4477 va_start(args, pat2);
4481 msv = vmess(buf, &args);
4483 message = SvPV(msv,l1);
4486 Copy(message, buf, l1 , char);
4487 buf[l1] = '\0'; /* Overwrite \n */
4488 Perl_croak(aTHX_ "%s", buf);
4491 /* XXX Here's a total kludge. But we need to re-enter for swash routines. */
4494 Perl_save_re_context(pTHX)
4497 SAVEPPTR(RExC_precomp); /* uncompiled string. */
4498 SAVEI32(RExC_npar); /* () count. */
4499 SAVEI32(RExC_size); /* Code size. */
4500 SAVEI16(RExC_flags16); /* are we folding, multilining? */
4501 SAVEVPTR(RExC_rx); /* from regcomp.c */
4502 SAVEI32(RExC_seen); /* from regcomp.c */
4503 SAVEI32(RExC_sawback); /* Did we see \1, ...? */
4504 SAVEI32(RExC_naughty); /* How bad is this pattern? */
4505 SAVEVPTR(RExC_emit); /* Code-emit pointer; ®dummy = don't */
4506 SAVEPPTR(RExC_end); /* End of input for compile */
4507 SAVEPPTR(RExC_parse); /* Input-scan pointer. */
4510 SAVEI32(PL_reg_flags); /* from regexec.c */
4512 SAVEPPTR(PL_reginput); /* String-input pointer. */
4513 SAVEPPTR(PL_regbol); /* Beginning of input, for ^ check. */
4514 SAVEPPTR(PL_regeol); /* End of input, for $ check. */
4515 SAVEVPTR(PL_regstartp); /* Pointer to startp array. */
4516 SAVEVPTR(PL_regendp); /* Ditto for endp. */
4517 SAVEVPTR(PL_reglastparen); /* Similarly for lastparen. */
4518 SAVEPPTR(PL_regtill); /* How far we are required to go. */
4519 SAVEI8(PL_regprev); /* char before regbol, \n if none */
4520 SAVEGENERICPV(PL_reg_start_tmp); /* from regexec.c */
4521 PL_reg_start_tmp = 0;
4522 SAVEI32(PL_reg_start_tmpl); /* from regexec.c */
4523 PL_reg_start_tmpl = 0;
4524 SAVEVPTR(PL_regdata);
4525 SAVEI32(PL_reg_eval_set); /* from regexec.c */
4526 SAVEI32(PL_regnarrate); /* from regexec.c */
4527 SAVEVPTR(PL_regprogram); /* from regexec.c */
4528 SAVEINT(PL_regindent); /* from regexec.c */
4529 SAVEVPTR(PL_regcc); /* from regexec.c */
4530 SAVEVPTR(PL_curcop);
4531 SAVEVPTR(PL_reg_call_cc); /* from regexec.c */
4532 SAVEVPTR(PL_reg_re); /* from regexec.c */
4533 SAVEPPTR(PL_reg_ganch); /* from regexec.c */
4534 SAVESPTR(PL_reg_sv); /* from regexec.c */
4535 SAVEVPTR(PL_reg_magic); /* from regexec.c */
4536 SAVEI32(PL_reg_oldpos); /* from regexec.c */
4537 SAVEVPTR(PL_reg_oldcurpm); /* from regexec.c */
4538 SAVEVPTR(PL_reg_curpm); /* from regexec.c */
4539 SAVEI32(PL_regnpar); /* () count. */
4541 SAVEPPTR(PL_reg_starttry); /* from regexec.c */
4552 clear_re(pTHXo_ void *r)
4554 ReREFCNT_dec((regexp *)r);