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, STR_LEN(n), char);
672 n = scan + NODE_SZ_STR(scan);
674 if (PL_regkind[(U8)OP(n)] != NOTHING || OP(n) == NOTHING) {
682 /* Follow the next-chain of the current node and optimize
683 away all the NOTHINGs from it. */
684 if (OP(scan) != CURLYX) {
685 int max = (reg_off_by_arg[OP(scan)]
687 /* I32 may be smaller than U16 on CRAYs! */
688 : (I32_MAX < U16_MAX ? I32_MAX : U16_MAX));
689 int off = (reg_off_by_arg[OP(scan)] ? ARG(scan) : NEXT_OFF(scan));
693 /* Skip NOTHING and LONGJMP. */
694 while ((n = regnext(n))
695 && ((PL_regkind[(U8)OP(n)] == NOTHING && (noff = NEXT_OFF(n)))
696 || ((OP(n) == LONGJMP) && (noff = ARG(n))))
699 if (reg_off_by_arg[OP(scan)])
702 NEXT_OFF(scan) = off;
704 /* The principal pseudo-switch. Cannot be a switch, since we
705 look into several different things. */
706 if (OP(scan) == BRANCH || OP(scan) == BRANCHJ
707 || OP(scan) == IFTHEN || OP(scan) == SUSPEND) {
708 next = regnext(scan);
711 if (OP(next) == code || code == IFTHEN || code == SUSPEND) {
712 I32 max1 = 0, min1 = I32_MAX, num = 0;
713 struct regnode_charclass_class accum;
715 if (flags & SCF_DO_SUBSTR) /* XXXX Add !SUSPEND? */
716 scan_commit(pRExC_state, data); /* Cannot merge strings after this. */
717 if (flags & SCF_DO_STCLASS)
718 cl_init_zero(pRExC_state, &accum);
719 while (OP(scan) == code) {
720 I32 deltanext, minnext, f = 0, fake;
721 struct regnode_charclass_class this_class;
726 data_fake.whilem_c = data->whilem_c;
727 data_fake.last_closep = data->last_closep;
730 data_fake.last_closep = &fake;
731 next = regnext(scan);
732 scan = NEXTOPER(scan);
734 scan = NEXTOPER(scan);
735 if (flags & SCF_DO_STCLASS) {
736 cl_init(pRExC_state, &this_class);
737 data_fake.start_class = &this_class;
738 f = SCF_DO_STCLASS_AND;
740 if (flags & SCF_WHILEM_VISITED_POS)
741 f |= SCF_WHILEM_VISITED_POS;
742 /* we suppose the run is continuous, last=next...*/
743 minnext = study_chunk(pRExC_state, &scan, &deltanext,
744 next, &data_fake, f);
747 if (max1 < minnext + deltanext)
748 max1 = minnext + deltanext;
749 if (deltanext == I32_MAX)
750 is_inf = is_inf_internal = 1;
752 if (data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
754 if (data && (data_fake.flags & SF_HAS_EVAL))
755 data->flags |= SF_HAS_EVAL;
757 data->whilem_c = data_fake.whilem_c;
758 if (flags & SCF_DO_STCLASS)
759 cl_or(pRExC_state, &accum, &this_class);
763 if (code == IFTHEN && num < 2) /* Empty ELSE branch */
765 if (flags & SCF_DO_SUBSTR) {
766 data->pos_min += min1;
767 data->pos_delta += max1 - min1;
768 if (max1 != min1 || is_inf)
769 data->longest = &(data->longest_float);
772 delta += max1 - min1;
773 if (flags & SCF_DO_STCLASS_OR) {
774 cl_or(pRExC_state, data->start_class, &accum);
776 cl_and(data->start_class, &and_with);
777 flags &= ~SCF_DO_STCLASS;
780 else if (flags & SCF_DO_STCLASS_AND) {
782 cl_and(data->start_class, &accum);
783 flags &= ~SCF_DO_STCLASS;
786 /* Switch to OR mode: cache the old value of
787 * data->start_class */
788 StructCopy(data->start_class, &and_with,
789 struct regnode_charclass_class);
790 flags &= ~SCF_DO_STCLASS_AND;
791 StructCopy(&accum, data->start_class,
792 struct regnode_charclass_class);
793 flags |= SCF_DO_STCLASS_OR;
794 data->start_class->flags |= ANYOF_EOS;
798 else if (code == BRANCHJ) /* single branch is optimized. */
799 scan = NEXTOPER(NEXTOPER(scan));
800 else /* single branch is optimized. */
801 scan = NEXTOPER(scan);
804 else if (OP(scan) == EXACT) {
805 I32 l = STR_LEN(scan);
806 UV uc = *((U8*)STRING(scan));
808 U8 *s = (U8*)STRING(scan);
809 l = utf8_length(s, s + l);
810 uc = utf8_to_uv_simple(s, NULL);
813 if (flags & SCF_DO_SUBSTR) { /* Update longest substr. */
814 /* The code below prefers earlier match for fixed
815 offset, later match for variable offset. */
816 if (data->last_end == -1) { /* Update the start info. */
817 data->last_start_min = data->pos_min;
818 data->last_start_max = is_inf
819 ? I32_MAX : data->pos_min + data->pos_delta;
821 sv_catpvn(data->last_found, STRING(scan), STR_LEN(scan));
822 data->last_end = data->pos_min + l;
823 data->pos_min += l; /* As in the first entry. */
824 data->flags &= ~SF_BEFORE_EOL;
826 if (flags & SCF_DO_STCLASS_AND) {
827 /* Check whether it is compatible with what we know already! */
831 !(data->start_class->flags & (ANYOF_CLASS | ANYOF_LOCALE))
832 && !ANYOF_BITMAP_TEST(data->start_class, uc)
833 && (!(data->start_class->flags & ANYOF_FOLD)
834 || !ANYOF_BITMAP_TEST(data->start_class, PL_fold[uc])))
836 ANYOF_CLASS_ZERO(data->start_class);
837 ANYOF_BITMAP_ZERO(data->start_class);
839 ANYOF_BITMAP_SET(data->start_class, uc);
840 data->start_class->flags &= ~ANYOF_EOS;
842 data->start_class->flags &= ~ANYOF_UNICODE_ALL;
844 else if (flags & SCF_DO_STCLASS_OR) {
845 /* false positive possible if the class is case-folded */
847 ANYOF_BITMAP_SET(data->start_class, uc);
849 data->start_class->flags |= ANYOF_UNICODE_ALL;
850 data->start_class->flags &= ~ANYOF_EOS;
851 cl_and(data->start_class, &and_with);
853 flags &= ~SCF_DO_STCLASS;
855 else if (PL_regkind[(U8)OP(scan)] == EXACT) { /* But OP != EXACT! */
856 I32 l = STR_LEN(scan);
857 UV uc = *((U8*)STRING(scan));
859 /* Search for fixed substrings supports EXACT only. */
860 if (flags & SCF_DO_SUBSTR)
861 scan_commit(pRExC_state, data);
863 U8 *s = (U8 *)STRING(scan);
864 l = utf8_length(s, s + l);
865 uc = utf8_to_uv_simple(s, NULL);
868 if (data && (flags & SCF_DO_SUBSTR))
870 if (flags & SCF_DO_STCLASS_AND) {
871 /* Check whether it is compatible with what we know already! */
875 !(data->start_class->flags & (ANYOF_CLASS | ANYOF_LOCALE))
876 && !ANYOF_BITMAP_TEST(data->start_class, uc)
877 && !ANYOF_BITMAP_TEST(data->start_class, PL_fold[uc]))
879 ANYOF_CLASS_ZERO(data->start_class);
880 ANYOF_BITMAP_ZERO(data->start_class);
882 ANYOF_BITMAP_SET(data->start_class, uc);
883 data->start_class->flags &= ~ANYOF_EOS;
884 data->start_class->flags |= ANYOF_FOLD;
885 if (OP(scan) == EXACTFL)
886 data->start_class->flags |= ANYOF_LOCALE;
889 else if (flags & SCF_DO_STCLASS_OR) {
890 if (data->start_class->flags & ANYOF_FOLD) {
891 /* false positive possible if the class is case-folded.
892 Assume that the locale settings are the same... */
894 ANYOF_BITMAP_SET(data->start_class, uc);
895 data->start_class->flags &= ~ANYOF_EOS;
897 cl_and(data->start_class, &and_with);
899 flags &= ~SCF_DO_STCLASS;
901 else if (strchr((char*)PL_varies,OP(scan))) {
902 I32 mincount, maxcount, minnext, deltanext, fl;
903 I32 f = flags, pos_before = 0;
904 regnode *oscan = scan;
905 struct regnode_charclass_class this_class;
906 struct regnode_charclass_class *oclass = NULL;
908 switch (PL_regkind[(U8)OP(scan)]) {
909 case WHILEM: /* End of (?:...)* . */
910 scan = NEXTOPER(scan);
913 if (flags & (SCF_DO_SUBSTR | SCF_DO_STCLASS)) {
914 next = NEXTOPER(scan);
915 if (OP(next) == EXACT || (flags & SCF_DO_STCLASS)) {
917 maxcount = REG_INFTY;
918 next = regnext(scan);
919 scan = NEXTOPER(scan);
923 if (flags & SCF_DO_SUBSTR)
928 if (flags & SCF_DO_STCLASS) {
930 maxcount = REG_INFTY;
931 next = regnext(scan);
932 scan = NEXTOPER(scan);
935 is_inf = is_inf_internal = 1;
936 scan = regnext(scan);
937 if (flags & SCF_DO_SUBSTR) {
938 scan_commit(pRExC_state, data); /* Cannot extend fixed substrings */
939 data->longest = &(data->longest_float);
941 goto optimize_curly_tail;
943 mincount = ARG1(scan);
944 maxcount = ARG2(scan);
945 next = regnext(scan);
946 if (OP(scan) == CURLYX) {
947 I32 lp = (data ? *(data->last_closep) : 0);
949 scan->flags = ((lp <= U8_MAX) ? lp : U8_MAX);
951 scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
953 if (flags & SCF_DO_SUBSTR) {
954 if (mincount == 0) scan_commit(pRExC_state,data); /* Cannot extend fixed substrings */
955 pos_before = data->pos_min;
959 data->flags &= ~(SF_HAS_PAR|SF_IN_PAR|SF_HAS_EVAL);
961 data->flags |= SF_IS_INF;
963 if (flags & SCF_DO_STCLASS) {
964 cl_init(pRExC_state, &this_class);
965 oclass = data->start_class;
966 data->start_class = &this_class;
967 f |= SCF_DO_STCLASS_AND;
968 f &= ~SCF_DO_STCLASS_OR;
970 /* These are the cases when once a subexpression
971 fails at a particular position, it cannot succeed
972 even after backtracking at the enclosing scope.
974 XXXX what if minimal match and we are at the
975 initial run of {n,m}? */
976 if ((mincount != maxcount - 1) && (maxcount != REG_INFTY))
977 f &= ~SCF_WHILEM_VISITED_POS;
979 /* This will finish on WHILEM, setting scan, or on NULL: */
980 minnext = study_chunk(pRExC_state, &scan, &deltanext, last, data,
982 ? (f & ~SCF_DO_SUBSTR) : f);
984 if (flags & SCF_DO_STCLASS)
985 data->start_class = oclass;
986 if (mincount == 0 || minnext == 0) {
987 if (flags & SCF_DO_STCLASS_OR) {
988 cl_or(pRExC_state, data->start_class, &this_class);
990 else if (flags & SCF_DO_STCLASS_AND) {
991 /* Switch to OR mode: cache the old value of
992 * data->start_class */
993 StructCopy(data->start_class, &and_with,
994 struct regnode_charclass_class);
995 flags &= ~SCF_DO_STCLASS_AND;
996 StructCopy(&this_class, data->start_class,
997 struct regnode_charclass_class);
998 flags |= SCF_DO_STCLASS_OR;
999 data->start_class->flags |= ANYOF_EOS;
1001 } else { /* Non-zero len */
1002 if (flags & SCF_DO_STCLASS_OR) {
1003 cl_or(pRExC_state, data->start_class, &this_class);
1004 cl_and(data->start_class, &and_with);
1006 else if (flags & SCF_DO_STCLASS_AND)
1007 cl_and(data->start_class, &this_class);
1008 flags &= ~SCF_DO_STCLASS;
1010 if (!scan) /* It was not CURLYX, but CURLY. */
1012 if (ckWARN(WARN_REGEXP) && (minnext + deltanext == 0)
1013 && !(data->flags & (SF_HAS_PAR|SF_IN_PAR))
1014 && maxcount <= REG_INFTY/3) /* Complement check for big count */
1017 "Quantifier unexpected on zero-length expression");
1020 min += minnext * mincount;
1021 is_inf_internal |= ((maxcount == REG_INFTY
1022 && (minnext + deltanext) > 0)
1023 || deltanext == I32_MAX);
1024 is_inf |= is_inf_internal;
1025 delta += (minnext + deltanext) * maxcount - minnext * mincount;
1027 /* Try powerful optimization CURLYX => CURLYN. */
1028 if ( OP(oscan) == CURLYX && data
1029 && data->flags & SF_IN_PAR
1030 && !(data->flags & SF_HAS_EVAL)
1031 && !deltanext && minnext == 1 ) {
1032 /* Try to optimize to CURLYN. */
1033 regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS;
1034 regnode *nxt1 = nxt, *nxt2;
1038 if (!strchr((char*)PL_simple,OP(nxt))
1039 && !(PL_regkind[(U8)OP(nxt)] == EXACT
1040 && STR_LEN(nxt) == 1))
1044 if (OP(nxt) != CLOSE)
1046 /* Now we know that nxt2 is the only contents: */
1047 oscan->flags = ARG(nxt);
1049 OP(nxt1) = NOTHING; /* was OPEN. */
1051 OP(nxt1 + 1) = OPTIMIZED; /* was count. */
1052 NEXT_OFF(nxt1+ 1) = 0; /* just for consistancy. */
1053 NEXT_OFF(nxt2) = 0; /* just for consistancy with CURLY. */
1054 OP(nxt) = OPTIMIZED; /* was CLOSE. */
1055 OP(nxt + 1) = OPTIMIZED; /* was count. */
1056 NEXT_OFF(nxt+ 1) = 0; /* just for consistancy. */
1061 /* Try optimization CURLYX => CURLYM. */
1062 if ( OP(oscan) == CURLYX && data
1063 && !(data->flags & SF_HAS_PAR)
1064 && !(data->flags & SF_HAS_EVAL)
1066 /* XXXX How to optimize if data == 0? */
1067 /* Optimize to a simpler form. */
1068 regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN */
1072 while ( (nxt2 = regnext(nxt)) /* skip over embedded stuff*/
1073 && (OP(nxt2) != WHILEM))
1075 OP(nxt2) = SUCCEED; /* Whas WHILEM */
1076 /* Need to optimize away parenths. */
1077 if (data->flags & SF_IN_PAR) {
1078 /* Set the parenth number. */
1079 regnode *nxt1 = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN*/
1081 if (OP(nxt) != CLOSE)
1082 FAIL("Panic opt close");
1083 oscan->flags = ARG(nxt);
1084 OP(nxt1) = OPTIMIZED; /* was OPEN. */
1085 OP(nxt) = OPTIMIZED; /* was CLOSE. */
1087 OP(nxt1 + 1) = OPTIMIZED; /* was count. */
1088 OP(nxt + 1) = OPTIMIZED; /* was count. */
1089 NEXT_OFF(nxt1 + 1) = 0; /* just for consistancy. */
1090 NEXT_OFF(nxt + 1) = 0; /* just for consistancy. */
1093 while ( nxt1 && (OP(nxt1) != WHILEM)) {
1094 regnode *nnxt = regnext(nxt1);
1097 if (reg_off_by_arg[OP(nxt1)])
1098 ARG_SET(nxt1, nxt2 - nxt1);
1099 else if (nxt2 - nxt1 < U16_MAX)
1100 NEXT_OFF(nxt1) = nxt2 - nxt1;
1102 OP(nxt) = NOTHING; /* Cannot beautify */
1107 /* Optimize again: */
1108 study_chunk(pRExC_state, &nxt1, &deltanext, nxt,
1114 else if ((OP(oscan) == CURLYX)
1115 && (flags & SCF_WHILEM_VISITED_POS)
1116 /* See the comment on a similar expression above.
1117 However, this time it not a subexpression
1118 we care about, but the expression itself. */
1119 && (maxcount == REG_INFTY)
1120 && data && ++data->whilem_c < 16) {
1121 /* This stays as CURLYX, we can put the count/of pair. */
1122 /* Find WHILEM (as in regexec.c) */
1123 regnode *nxt = oscan + NEXT_OFF(oscan);
1125 if (OP(PREVOPER(nxt)) == NOTHING) /* LONGJMP */
1127 PREVOPER(nxt)->flags = data->whilem_c
1128 | (RExC_whilem_seen << 4); /* On WHILEM */
1130 if (data && fl & (SF_HAS_PAR|SF_IN_PAR))
1132 if (flags & SCF_DO_SUBSTR) {
1133 SV *last_str = Nullsv;
1134 int counted = mincount != 0;
1136 if (data->last_end > 0 && mincount != 0) { /* Ends with a string. */
1137 I32 b = pos_before >= data->last_start_min
1138 ? pos_before : data->last_start_min;
1140 char *s = SvPV(data->last_found, l);
1141 I32 old = b - data->last_start_min;
1144 old = utf8_hop((U8*)s, old) - (U8*)s;
1147 /* Get the added string: */
1148 last_str = newSVpvn(s + old, l);
1149 if (deltanext == 0 && pos_before == b) {
1150 /* What was added is a constant string */
1152 SvGROW(last_str, (mincount * l) + 1);
1153 repeatcpy(SvPVX(last_str) + l,
1154 SvPVX(last_str), l, mincount - 1);
1155 SvCUR(last_str) *= mincount;
1156 /* Add additional parts. */
1157 SvCUR_set(data->last_found,
1158 SvCUR(data->last_found) - l);
1159 sv_catsv(data->last_found, last_str);
1160 data->last_end += l * (mincount - 1);
1163 /* start offset must point into the last copy */
1164 data->last_start_min += minnext * (mincount - 1);
1165 data->last_start_max += is_inf ? 0 : (maxcount - 1)
1166 * (minnext + data->pos_delta);
1169 /* It is counted once already... */
1170 data->pos_min += minnext * (mincount - counted);
1171 data->pos_delta += - counted * deltanext +
1172 (minnext + deltanext) * maxcount - minnext * mincount;
1173 if (mincount != maxcount) {
1174 /* Cannot extend fixed substrings found inside
1176 scan_commit(pRExC_state,data);
1177 if (mincount && last_str) {
1178 sv_setsv(data->last_found, last_str);
1179 data->last_end = data->pos_min;
1180 data->last_start_min =
1181 data->pos_min - CHR_SVLEN(last_str);
1182 data->last_start_max = is_inf
1184 : data->pos_min + data->pos_delta
1185 - CHR_SVLEN(last_str);
1187 data->longest = &(data->longest_float);
1189 SvREFCNT_dec(last_str);
1191 if (data && (fl & SF_HAS_EVAL))
1192 data->flags |= SF_HAS_EVAL;
1193 optimize_curly_tail:
1194 if (OP(oscan) != CURLYX) {
1195 while (PL_regkind[(U8)OP(next = regnext(oscan))] == NOTHING
1197 NEXT_OFF(oscan) += NEXT_OFF(next);
1200 default: /* REF and CLUMP only? */
1201 if (flags & SCF_DO_SUBSTR) {
1202 scan_commit(pRExC_state,data); /* Cannot expect anything... */
1203 data->longest = &(data->longest_float);
1205 is_inf = is_inf_internal = 1;
1206 if (flags & SCF_DO_STCLASS_OR)
1207 cl_anything(pRExC_state, data->start_class);
1208 flags &= ~SCF_DO_STCLASS;
1212 else if (strchr((char*)PL_simple,OP(scan))) {
1215 if (flags & SCF_DO_SUBSTR) {
1216 scan_commit(pRExC_state,data);
1220 if (flags & SCF_DO_STCLASS) {
1221 data->start_class->flags &= ~ANYOF_EOS; /* No match on empty */
1223 /* Some of the logic below assumes that switching
1224 locale on will only add false positives. */
1225 switch (PL_regkind[(U8)OP(scan)]) {
1229 /* Perl_croak(aTHX_ "panic: unexpected simple REx opcode %d", OP(scan)); */
1230 if (flags & SCF_DO_STCLASS_OR) /* Allow everything */
1231 cl_anything(pRExC_state, data->start_class);
1234 if (OP(scan) == SANY)
1236 if (flags & SCF_DO_STCLASS_OR) { /* Everything but \n */
1237 value = (ANYOF_BITMAP_TEST(data->start_class,'\n')
1238 || (data->start_class->flags & ANYOF_CLASS));
1239 cl_anything(pRExC_state, data->start_class);
1241 if (flags & SCF_DO_STCLASS_AND || !value)
1242 ANYOF_BITMAP_CLEAR(data->start_class,'\n');
1245 if (flags & SCF_DO_STCLASS_AND)
1246 cl_and(data->start_class,
1247 (struct regnode_charclass_class*)scan);
1249 cl_or(pRExC_state, data->start_class,
1250 (struct regnode_charclass_class*)scan);
1253 if (flags & SCF_DO_STCLASS_AND) {
1254 if (!(data->start_class->flags & ANYOF_LOCALE)) {
1255 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NALNUM);
1256 for (value = 0; value < 256; value++)
1257 if (!isALNUM(value))
1258 ANYOF_BITMAP_CLEAR(data->start_class, value);
1262 if (data->start_class->flags & ANYOF_LOCALE)
1263 ANYOF_CLASS_SET(data->start_class,ANYOF_ALNUM);
1265 for (value = 0; value < 256; value++)
1267 ANYOF_BITMAP_SET(data->start_class, value);
1272 if (flags & SCF_DO_STCLASS_AND) {
1273 if (data->start_class->flags & ANYOF_LOCALE)
1274 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NALNUM);
1277 ANYOF_CLASS_SET(data->start_class,ANYOF_ALNUM);
1278 data->start_class->flags |= ANYOF_LOCALE;
1282 if (flags & SCF_DO_STCLASS_AND) {
1283 if (!(data->start_class->flags & ANYOF_LOCALE)) {
1284 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_ALNUM);
1285 for (value = 0; value < 256; value++)
1287 ANYOF_BITMAP_CLEAR(data->start_class, value);
1291 if (data->start_class->flags & ANYOF_LOCALE)
1292 ANYOF_CLASS_SET(data->start_class,ANYOF_NALNUM);
1294 for (value = 0; value < 256; value++)
1295 if (!isALNUM(value))
1296 ANYOF_BITMAP_SET(data->start_class, value);
1301 if (flags & SCF_DO_STCLASS_AND) {
1302 if (data->start_class->flags & ANYOF_LOCALE)
1303 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_ALNUM);
1306 data->start_class->flags |= ANYOF_LOCALE;
1307 ANYOF_CLASS_SET(data->start_class,ANYOF_NALNUM);
1311 if (flags & SCF_DO_STCLASS_AND) {
1312 if (!(data->start_class->flags & ANYOF_LOCALE)) {
1313 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NSPACE);
1314 for (value = 0; value < 256; value++)
1315 if (!isSPACE(value))
1316 ANYOF_BITMAP_CLEAR(data->start_class, value);
1320 if (data->start_class->flags & ANYOF_LOCALE)
1321 ANYOF_CLASS_SET(data->start_class,ANYOF_SPACE);
1323 for (value = 0; value < 256; value++)
1325 ANYOF_BITMAP_SET(data->start_class, value);
1330 if (flags & SCF_DO_STCLASS_AND) {
1331 if (data->start_class->flags & ANYOF_LOCALE)
1332 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NSPACE);
1335 data->start_class->flags |= ANYOF_LOCALE;
1336 ANYOF_CLASS_SET(data->start_class,ANYOF_SPACE);
1340 if (flags & SCF_DO_STCLASS_AND) {
1341 if (!(data->start_class->flags & ANYOF_LOCALE)) {
1342 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_SPACE);
1343 for (value = 0; value < 256; value++)
1345 ANYOF_BITMAP_CLEAR(data->start_class, value);
1349 if (data->start_class->flags & ANYOF_LOCALE)
1350 ANYOF_CLASS_SET(data->start_class,ANYOF_NSPACE);
1352 for (value = 0; value < 256; value++)
1353 if (!isSPACE(value))
1354 ANYOF_BITMAP_SET(data->start_class, value);
1359 if (flags & SCF_DO_STCLASS_AND) {
1360 if (data->start_class->flags & ANYOF_LOCALE) {
1361 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_SPACE);
1362 for (value = 0; value < 256; value++)
1363 if (!isSPACE(value))
1364 ANYOF_BITMAP_CLEAR(data->start_class, value);
1368 data->start_class->flags |= ANYOF_LOCALE;
1369 ANYOF_CLASS_SET(data->start_class,ANYOF_NSPACE);
1373 if (flags & SCF_DO_STCLASS_AND) {
1374 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NDIGIT);
1375 for (value = 0; value < 256; value++)
1376 if (!isDIGIT(value))
1377 ANYOF_BITMAP_CLEAR(data->start_class, value);
1380 if (data->start_class->flags & ANYOF_LOCALE)
1381 ANYOF_CLASS_SET(data->start_class,ANYOF_DIGIT);
1383 for (value = 0; value < 256; value++)
1385 ANYOF_BITMAP_SET(data->start_class, value);
1390 if (flags & SCF_DO_STCLASS_AND) {
1391 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_DIGIT);
1392 for (value = 0; value < 256; value++)
1394 ANYOF_BITMAP_CLEAR(data->start_class, value);
1397 if (data->start_class->flags & ANYOF_LOCALE)
1398 ANYOF_CLASS_SET(data->start_class,ANYOF_NDIGIT);
1400 for (value = 0; value < 256; value++)
1401 if (!isDIGIT(value))
1402 ANYOF_BITMAP_SET(data->start_class, value);
1407 if (flags & SCF_DO_STCLASS_OR)
1408 cl_and(data->start_class, &and_with);
1409 flags &= ~SCF_DO_STCLASS;
1412 else if (PL_regkind[(U8)OP(scan)] == EOL && flags & SCF_DO_SUBSTR) {
1413 data->flags |= (OP(scan) == MEOL
1417 else if ( PL_regkind[(U8)OP(scan)] == BRANCHJ
1418 /* Lookbehind, or need to calculate parens/evals/stclass: */
1419 && (scan->flags || data || (flags & SCF_DO_STCLASS))
1420 && (OP(scan) == IFMATCH || OP(scan) == UNLESSM)) {
1421 /* Lookahead/lookbehind */
1422 I32 deltanext, minnext, fake = 0;
1424 struct regnode_charclass_class intrnl;
1427 data_fake.flags = 0;
1429 data_fake.whilem_c = data->whilem_c;
1430 data_fake.last_closep = data->last_closep;
1433 data_fake.last_closep = &fake;
1434 if ( flags & SCF_DO_STCLASS && !scan->flags
1435 && OP(scan) == IFMATCH ) { /* Lookahead */
1436 cl_init(pRExC_state, &intrnl);
1437 data_fake.start_class = &intrnl;
1438 f |= SCF_DO_STCLASS_AND;
1440 if (flags & SCF_WHILEM_VISITED_POS)
1441 f |= SCF_WHILEM_VISITED_POS;
1442 next = regnext(scan);
1443 nscan = NEXTOPER(NEXTOPER(scan));
1444 minnext = study_chunk(pRExC_state, &nscan, &deltanext, last, &data_fake, f);
1447 vFAIL("Variable length lookbehind not implemented");
1449 else if (minnext > U8_MAX) {
1450 vFAIL2("Lookbehind longer than %"UVuf" not implemented", (UV)U8_MAX);
1452 scan->flags = minnext;
1454 if (data && data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
1456 if (data && (data_fake.flags & SF_HAS_EVAL))
1457 data->flags |= SF_HAS_EVAL;
1459 data->whilem_c = data_fake.whilem_c;
1460 if (f & SCF_DO_STCLASS_AND) {
1461 int was = (data->start_class->flags & ANYOF_EOS);
1463 cl_and(data->start_class, &intrnl);
1465 data->start_class->flags |= ANYOF_EOS;
1468 else if (OP(scan) == OPEN) {
1471 else if (OP(scan) == CLOSE) {
1472 if (ARG(scan) == is_par) {
1473 next = regnext(scan);
1475 if ( next && (OP(next) != WHILEM) && next < last)
1476 is_par = 0; /* Disable optimization */
1479 *(data->last_closep) = ARG(scan);
1481 else if (OP(scan) == EVAL) {
1483 data->flags |= SF_HAS_EVAL;
1485 else if (OP(scan) == LOGICAL && scan->flags == 2) { /* Embedded follows */
1486 if (flags & SCF_DO_SUBSTR) {
1487 scan_commit(pRExC_state,data);
1488 data->longest = &(data->longest_float);
1490 is_inf = is_inf_internal = 1;
1491 if (flags & SCF_DO_STCLASS_OR) /* Allow everything */
1492 cl_anything(pRExC_state, data->start_class);
1493 flags &= ~SCF_DO_STCLASS;
1495 /* Else: zero-length, ignore. */
1496 scan = regnext(scan);
1501 *deltap = is_inf_internal ? I32_MAX : delta;
1502 if (flags & SCF_DO_SUBSTR && is_inf)
1503 data->pos_delta = I32_MAX - data->pos_min;
1504 if (is_par > U8_MAX)
1506 if (is_par && pars==1 && data) {
1507 data->flags |= SF_IN_PAR;
1508 data->flags &= ~SF_HAS_PAR;
1510 else if (pars && data) {
1511 data->flags |= SF_HAS_PAR;
1512 data->flags &= ~SF_IN_PAR;
1514 if (flags & SCF_DO_STCLASS_OR)
1515 cl_and(data->start_class, &and_with);
1520 S_add_data(pTHX_ RExC_state_t *pRExC_state, I32 n, char *s)
1522 if (RExC_rx->data) {
1523 Renewc(RExC_rx->data,
1524 sizeof(*RExC_rx->data) + sizeof(void*) * (RExC_rx->data->count + n - 1),
1525 char, struct reg_data);
1526 Renew(RExC_rx->data->what, RExC_rx->data->count + n, U8);
1527 RExC_rx->data->count += n;
1530 Newc(1207, RExC_rx->data, sizeof(*RExC_rx->data) + sizeof(void*) * (n - 1),
1531 char, struct reg_data);
1532 New(1208, RExC_rx->data->what, n, U8);
1533 RExC_rx->data->count = n;
1535 Copy(s, RExC_rx->data->what + RExC_rx->data->count - n, n, U8);
1536 return RExC_rx->data->count - n;
1540 Perl_reginitcolors(pTHX)
1543 char *s = PerlEnv_getenv("PERL_RE_COLORS");
1546 PL_colors[0] = s = savepv(s);
1548 s = strchr(s, '\t');
1554 PL_colors[i] = s = "";
1558 PL_colors[i++] = "";
1565 - pregcomp - compile a regular expression into internal code
1567 * We can't allocate space until we know how big the compiled form will be,
1568 * but we can't compile it (and thus know how big it is) until we've got a
1569 * place to put the code. So we cheat: we compile it twice, once with code
1570 * generation turned off and size counting turned on, and once "for real".
1571 * This also means that we don't allocate space until we are sure that the
1572 * thing really will compile successfully, and we never have to move the
1573 * code and thus invalidate pointers into it. (Note that it has to be in
1574 * one piece because free() must be able to free it all.) [NB: not true in perl]
1576 * Beware that the optimization-preparation code in here knows about some
1577 * of the structure of the compiled regexp. [I'll say.]
1580 Perl_pregcomp(pTHX_ char *exp, char *xend, PMOP *pm)
1590 RExC_state_t RExC_state;
1591 RExC_state_t *pRExC_state = &RExC_state;
1594 FAIL("NULL regexp argument");
1596 /* XXXX This looks very suspicious... */
1597 if (pm->op_pmdynflags & PMdf_CMP_UTF8)
1602 RExC_precomp = savepvn(exp, xend - exp);
1603 DEBUG_r(if (!PL_colorset) reginitcolors());
1604 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sCompiling REx%s `%s%*s%s'\n",
1605 PL_colors[4],PL_colors[5],PL_colors[0],
1606 (int)(xend - exp), RExC_precomp, PL_colors[1]));
1607 RExC_flags16 = pm->op_pmflags;
1611 RExC_seen_zerolen = *exp == '^' ? -1 : 0;
1612 RExC_seen_evals = 0;
1615 /* First pass: determine size, legality. */
1621 RExC_emit = &PL_regdummy;
1622 RExC_whilem_seen = 0;
1623 #if 0 /* REGC() is (currently) a NOP at the first pass.
1624 * Clever compilers notice this and complain. --jhi */
1625 REGC((U8)REG_MAGIC, (char*)RExC_emit);
1627 if (reg(pRExC_state, 0, &flags) == NULL) {
1628 Safefree(RExC_precomp);
1629 RExC_precomp = Nullch;
1632 DEBUG_r(PerlIO_printf(Perl_debug_log, "size %"IVdf" ", (IV)RExC_size));
1634 /* Small enough for pointer-storage convention?
1635 If extralen==0, this means that we will not need long jumps. */
1636 if (RExC_size >= 0x10000L && RExC_extralen)
1637 RExC_size += RExC_extralen;
1640 if (RExC_whilem_seen > 15)
1641 RExC_whilem_seen = 15;
1643 /* Allocate space and initialize. */
1644 Newc(1001, r, sizeof(regexp) + (unsigned)RExC_size * sizeof(regnode),
1647 FAIL("Regexp out of space");
1650 /* avoid reading uninitialized memory in DEBUGGING code in study_chunk() */
1651 Zero(r, sizeof(regexp) + (unsigned)RExC_size * sizeof(regnode), char);
1654 r->prelen = xend - exp;
1655 r->precomp = RExC_precomp;
1657 r->reganch = pm->op_pmflags & PMf_COMPILETIME;
1658 r->nparens = RExC_npar - 1; /* set early to validate backrefs */
1660 r->substrs = 0; /* Useful during FAIL. */
1661 r->startp = 0; /* Useful during FAIL. */
1662 r->endp = 0; /* Useful during FAIL. */
1666 /* Second pass: emit code. */
1671 RExC_emit = r->program;
1672 /* Store the count of eval-groups for security checks: */
1673 RExC_emit->next_off = ((RExC_seen_evals > U16_MAX) ? U16_MAX : RExC_seen_evals);
1674 REGC((U8)REG_MAGIC, (char*) RExC_emit++);
1676 if (reg(pRExC_state, 0, &flags) == NULL)
1679 /* Dig out information for optimizations. */
1680 r->reganch = pm->op_pmflags & PMf_COMPILETIME; /* Again? */
1681 pm->op_pmflags = RExC_flags16;
1683 r->reganch |= ROPT_UTF8;
1684 r->regstclass = NULL;
1685 if (RExC_naughty >= 10) /* Probably an expensive pattern. */
1686 r->reganch |= ROPT_NAUGHTY;
1687 scan = r->program + 1; /* First BRANCH. */
1689 /* XXXX To minimize changes to RE engine we always allocate
1690 3-units-long substrs field. */
1691 Newz(1004, r->substrs, 1, struct reg_substr_data);
1693 StructCopy(&zero_scan_data, &data, scan_data_t);
1694 /* XXXX Should not we check for something else? Usually it is OPEN1... */
1695 if (OP(scan) != BRANCH) { /* Only one top-level choice. */
1697 STRLEN longest_float_length, longest_fixed_length;
1698 struct regnode_charclass_class ch_class;
1703 /* Skip introductions and multiplicators >= 1. */
1704 while ((OP(first) == OPEN && (sawopen = 1)) ||
1705 /* An OR of *one* alternative - should not happen now. */
1706 (OP(first) == BRANCH && OP(regnext(first)) != BRANCH) ||
1707 (OP(first) == PLUS) ||
1708 (OP(first) == MINMOD) ||
1709 /* An {n,m} with n>0 */
1710 (PL_regkind[(U8)OP(first)] == CURLY && ARG1(first) > 0) ) {
1711 if (OP(first) == PLUS)
1714 first += regarglen[(U8)OP(first)];
1715 first = NEXTOPER(first);
1718 /* Starting-point info. */
1720 if (PL_regkind[(U8)OP(first)] == EXACT) {
1721 if (OP(first) == EXACT)
1722 ; /* Empty, get anchored substr later. */
1723 else if ((OP(first) == EXACTF || OP(first) == EXACTFL))
1724 r->regstclass = first;
1726 else if (strchr((char*)PL_simple,OP(first)))
1727 r->regstclass = first;
1728 else if (PL_regkind[(U8)OP(first)] == BOUND ||
1729 PL_regkind[(U8)OP(first)] == NBOUND)
1730 r->regstclass = first;
1731 else if (PL_regkind[(U8)OP(first)] == BOL) {
1732 r->reganch |= (OP(first) == MBOL
1734 : (OP(first) == SBOL
1737 first = NEXTOPER(first);
1740 else if (OP(first) == GPOS) {
1741 r->reganch |= ROPT_ANCH_GPOS;
1742 first = NEXTOPER(first);
1745 else if ((OP(first) == STAR &&
1746 PL_regkind[(U8)OP(NEXTOPER(first))] == REG_ANY) &&
1747 !(r->reganch & ROPT_ANCH) )
1749 /* turn .* into ^.* with an implied $*=1 */
1750 int type = OP(NEXTOPER(first));
1752 if (type == REG_ANY)
1753 type = ROPT_ANCH_MBOL;
1755 type = ROPT_ANCH_SBOL;
1757 r->reganch |= type | ROPT_IMPLICIT;
1758 first = NEXTOPER(first);
1761 if (sawplus && (!sawopen || !RExC_sawback)
1762 && !(RExC_seen & REG_SEEN_EVAL)) /* May examine pos and $& */
1763 /* x+ must match at the 1st pos of run of x's */
1764 r->reganch |= ROPT_SKIP;
1766 /* Scan is after the zeroth branch, first is atomic matcher. */
1767 DEBUG_r(PerlIO_printf(Perl_debug_log, "first at %"IVdf"\n",
1768 (IV)(first - scan + 1)));
1770 * If there's something expensive in the r.e., find the
1771 * longest literal string that must appear and make it the
1772 * regmust. Resolve ties in favor of later strings, since
1773 * the regstart check works with the beginning of the r.e.
1774 * and avoiding duplication strengthens checking. Not a
1775 * strong reason, but sufficient in the absence of others.
1776 * [Now we resolve ties in favor of the earlier string if
1777 * it happens that c_offset_min has been invalidated, since the
1778 * earlier string may buy us something the later one won't.]
1782 data.longest_fixed = newSVpvn("",0);
1783 data.longest_float = newSVpvn("",0);
1784 data.last_found = newSVpvn("",0);
1785 data.longest = &(data.longest_fixed);
1787 if (!r->regstclass) {
1788 cl_init(pRExC_state, &ch_class);
1789 data.start_class = &ch_class;
1790 stclass_flag = SCF_DO_STCLASS_AND;
1791 } else /* XXXX Check for BOUND? */
1793 data.last_closep = &last_close;
1795 minlen = study_chunk(pRExC_state, &first, &fake, scan + RExC_size, /* Up to end */
1796 &data, SCF_DO_SUBSTR | SCF_WHILEM_VISITED_POS | stclass_flag);
1797 if ( RExC_npar == 1 && data.longest == &(data.longest_fixed)
1798 && data.last_start_min == 0 && data.last_end > 0
1799 && !RExC_seen_zerolen
1800 && (!(RExC_seen & REG_SEEN_GPOS) || (r->reganch & ROPT_ANCH_GPOS)))
1801 r->reganch |= ROPT_CHECK_ALL;
1802 scan_commit(pRExC_state, &data);
1803 SvREFCNT_dec(data.last_found);
1805 longest_float_length = CHR_SVLEN(data.longest_float);
1806 if (longest_float_length
1807 || (data.flags & SF_FL_BEFORE_EOL
1808 && (!(data.flags & SF_FL_BEFORE_MEOL)
1809 || (RExC_flags16 & PMf_MULTILINE)))) {
1812 if (SvCUR(data.longest_fixed) /* ok to leave SvCUR */
1813 && data.offset_fixed == data.offset_float_min
1814 && SvCUR(data.longest_fixed) == SvCUR(data.longest_float))
1815 goto remove_float; /* As in (a)+. */
1817 r->float_substr = data.longest_float;
1818 r->float_min_offset = data.offset_float_min;
1819 r->float_max_offset = data.offset_float_max;
1820 t = (data.flags & SF_FL_BEFORE_EOL /* Can't have SEOL and MULTI */
1821 && (!(data.flags & SF_FL_BEFORE_MEOL)
1822 || (RExC_flags16 & PMf_MULTILINE)));
1823 fbm_compile(r->float_substr, t ? FBMcf_TAIL : 0);
1827 r->float_substr = Nullsv;
1828 SvREFCNT_dec(data.longest_float);
1829 longest_float_length = 0;
1832 longest_fixed_length = CHR_SVLEN(data.longest_fixed);
1833 if (longest_fixed_length
1834 || (data.flags & SF_FIX_BEFORE_EOL /* Cannot have SEOL and MULTI */
1835 && (!(data.flags & SF_FIX_BEFORE_MEOL)
1836 || (RExC_flags16 & PMf_MULTILINE)))) {
1839 r->anchored_substr = data.longest_fixed;
1840 r->anchored_offset = data.offset_fixed;
1841 t = (data.flags & SF_FIX_BEFORE_EOL /* Can't have SEOL and MULTI */
1842 && (!(data.flags & SF_FIX_BEFORE_MEOL)
1843 || (RExC_flags16 & PMf_MULTILINE)));
1844 fbm_compile(r->anchored_substr, t ? FBMcf_TAIL : 0);
1847 r->anchored_substr = Nullsv;
1848 SvREFCNT_dec(data.longest_fixed);
1849 longest_fixed_length = 0;
1852 && (OP(r->regstclass) == REG_ANY || OP(r->regstclass) == SANY))
1853 r->regstclass = NULL;
1854 if ((!r->anchored_substr || r->anchored_offset) && stclass_flag
1855 && !(data.start_class->flags & ANYOF_EOS)
1856 && !cl_is_anything(data.start_class)) {
1858 I32 n = add_data(pRExC_state, 1, "f");
1860 New(1006, RExC_rx->data->data[n], 1,
1861 struct regnode_charclass_class);
1862 StructCopy(data.start_class,
1863 (struct regnode_charclass_class*)RExC_rx->data->data[n],
1864 struct regnode_charclass_class);
1865 r->regstclass = (regnode*)RExC_rx->data->data[n];
1866 r->reganch &= ~ROPT_SKIP; /* Used in find_byclass(). */
1867 PL_regdata = r->data; /* for regprop() */
1868 DEBUG_r((sv = sv_newmortal(),
1869 regprop(sv, (regnode*)data.start_class),
1870 PerlIO_printf(Perl_debug_log, "synthetic stclass `%s'.\n",
1874 /* A temporary algorithm prefers floated substr to fixed one to dig more info. */
1875 if (longest_fixed_length > longest_float_length) {
1876 r->check_substr = r->anchored_substr;
1877 r->check_offset_min = r->check_offset_max = r->anchored_offset;
1878 if (r->reganch & ROPT_ANCH_SINGLE)
1879 r->reganch |= ROPT_NOSCAN;
1882 r->check_substr = r->float_substr;
1883 r->check_offset_min = data.offset_float_min;
1884 r->check_offset_max = data.offset_float_max;
1886 /* XXXX Currently intuiting is not compatible with ANCH_GPOS.
1887 This should be changed ASAP! */
1888 if (r->check_substr && !(r->reganch & ROPT_ANCH_GPOS)) {
1889 r->reganch |= RE_USE_INTUIT;
1890 if (SvTAIL(r->check_substr))
1891 r->reganch |= RE_INTUIT_TAIL;
1895 /* Several toplevels. Best we can is to set minlen. */
1897 struct regnode_charclass_class ch_class;
1900 DEBUG_r(PerlIO_printf(Perl_debug_log, "\n"));
1901 scan = r->program + 1;
1902 cl_init(pRExC_state, &ch_class);
1903 data.start_class = &ch_class;
1904 data.last_closep = &last_close;
1905 minlen = study_chunk(pRExC_state, &scan, &fake, scan + RExC_size, &data, SCF_DO_STCLASS_AND|SCF_WHILEM_VISITED_POS);
1906 r->check_substr = r->anchored_substr = r->float_substr = Nullsv;
1907 if (!(data.start_class->flags & ANYOF_EOS)
1908 && !cl_is_anything(data.start_class)) {
1910 I32 n = add_data(pRExC_state, 1, "f");
1912 New(1006, RExC_rx->data->data[n], 1,
1913 struct regnode_charclass_class);
1914 StructCopy(data.start_class,
1915 (struct regnode_charclass_class*)RExC_rx->data->data[n],
1916 struct regnode_charclass_class);
1917 r->regstclass = (regnode*)RExC_rx->data->data[n];
1918 r->reganch &= ~ROPT_SKIP; /* Used in find_byclass(). */
1919 DEBUG_r((sv = sv_newmortal(),
1920 regprop(sv, (regnode*)data.start_class),
1921 PerlIO_printf(Perl_debug_log, "synthetic stclass `%s'.\n",
1927 if (RExC_seen & REG_SEEN_GPOS)
1928 r->reganch |= ROPT_GPOS_SEEN;
1929 if (RExC_seen & REG_SEEN_LOOKBEHIND)
1930 r->reganch |= ROPT_LOOKBEHIND_SEEN;
1931 if (RExC_seen & REG_SEEN_EVAL)
1932 r->reganch |= ROPT_EVAL_SEEN;
1933 Newz(1002, r->startp, RExC_npar, I32);
1934 Newz(1002, r->endp, RExC_npar, I32);
1935 PL_regdata = r->data; /* for regprop() */
1936 DEBUG_r(regdump(r));
1941 - reg - regular expression, i.e. main body or parenthesized thing
1943 * Caller must absorb opening parenthesis.
1945 * Combining parenthesis handling with the base level of regular expression
1946 * is a trifle forced, but the need to tie the tails of the branches to what
1947 * follows makes it hard to avoid.
1950 S_reg(pTHX_ RExC_state_t *pRExC_state, I32 paren, I32 *flagp)
1951 /* paren: Parenthesized? 0=top, 1=(, inside: changed to letter. */
1953 register regnode *ret; /* Will be the head of the group. */
1954 register regnode *br;
1955 register regnode *lastbr;
1956 register regnode *ender = 0;
1957 register I32 parno = 0;
1958 I32 flags, oregflags = RExC_flags16, have_branch = 0, open = 0;
1959 char *oregcomp_parse = RExC_parse;
1962 *flagp = 0; /* Tentatively. */
1964 /* Make an OPEN node, if parenthesized. */
1966 if (*RExC_parse == '?') {
1967 U16 posflags = 0, negflags = 0;
1968 U16 *flagsp = &posflags;
1970 char *seqstart = RExC_parse;
1973 paren = *RExC_parse++;
1974 ret = NULL; /* For look-ahead/behind. */
1977 RExC_seen |= REG_SEEN_LOOKBEHIND;
1978 if (*RExC_parse == '!')
1980 if (*RExC_parse != '=' && *RExC_parse != '!')
1985 RExC_seen_zerolen++;
1991 vFAIL2("Sequence (?%c...) not implemented", (int)paren);
1994 while (*RExC_parse && *RExC_parse != ')')
1996 if (*RExC_parse != ')')
1997 FAIL("Sequence (?#... not terminated");
1998 nextchar(pRExC_state);
2003 vWARN(RExC_parse, "(?p{}) is deprecated - use (??{})");
2007 paren = *RExC_parse++;
2011 I32 count = 1, n = 0;
2013 char *s = RExC_parse;
2015 OP_4tree *sop, *rop;
2017 RExC_seen_zerolen++;
2018 RExC_seen |= REG_SEEN_EVAL;
2019 while (count && (c = *RExC_parse)) {
2020 if (c == '\\' && RExC_parse[1])
2028 if (*RExC_parse != ')')
2031 vFAIL("Sequence (?{...}) not terminated or not {}-balanced");
2036 if (RExC_parse - 1 - s)
2037 sv = newSVpvn(s, RExC_parse - 1 - s);
2039 sv = newSVpvn("", 0);
2042 Perl_save_re_context(aTHX);
2043 rop = sv_compile_2op(sv, &sop, "re", &av);
2046 n = add_data(pRExC_state, 3, "nop");
2047 RExC_rx->data->data[n] = (void*)rop;
2048 RExC_rx->data->data[n+1] = (void*)sop;
2049 RExC_rx->data->data[n+2] = (void*)av;
2052 else { /* First pass */
2053 if (PL_reginterp_cnt < ++RExC_seen_evals
2054 && PL_curcop != &PL_compiling)
2055 /* No compiled RE interpolated, has runtime
2056 components ===> unsafe. */
2057 FAIL("Eval-group not allowed at runtime, use re 'eval'");
2059 FAIL("Eval-group in insecure regular expression");
2062 nextchar(pRExC_state);
2064 ret = reg_node(pRExC_state, LOGICAL);
2067 regtail(pRExC_state, ret, reganode(pRExC_state, EVAL, n));
2070 return reganode(pRExC_state, EVAL, n);
2074 if (RExC_parse[0] == '?') {
2075 if (RExC_parse[1] == '=' || RExC_parse[1] == '!'
2076 || RExC_parse[1] == '<'
2077 || RExC_parse[1] == '{') { /* Lookahead or eval. */
2080 ret = reg_node(pRExC_state, LOGICAL);
2083 regtail(pRExC_state, ret, reg(pRExC_state, 1, &flag));
2087 else if (RExC_parse[0] >= '1' && RExC_parse[0] <= '9' ) {
2088 parno = atoi(RExC_parse++);
2090 while (isDIGIT(*RExC_parse))
2092 ret = reganode(pRExC_state, GROUPP, parno);
2093 if ((c = *nextchar(pRExC_state)) != ')')
2094 vFAIL("Switch condition not recognized");
2096 regtail(pRExC_state, ret, reganode(pRExC_state, IFTHEN, 0));
2097 br = regbranch(pRExC_state, &flags, 1);
2099 br = reganode(pRExC_state, LONGJMP, 0);
2101 regtail(pRExC_state, br, reganode(pRExC_state, LONGJMP, 0));
2102 c = *nextchar(pRExC_state);
2106 lastbr = reganode(pRExC_state, IFTHEN, 0); /* Fake one for optimizer. */
2107 regbranch(pRExC_state, &flags, 1);
2108 regtail(pRExC_state, ret, lastbr);
2111 c = *nextchar(pRExC_state);
2116 vFAIL("Switch (?(condition)... contains too many branches");
2117 ender = reg_node(pRExC_state, TAIL);
2118 regtail(pRExC_state, br, ender);
2120 regtail(pRExC_state, lastbr, ender);
2121 regtail(pRExC_state, NEXTOPER(NEXTOPER(lastbr)), ender);
2124 regtail(pRExC_state, ret, ender);
2128 vFAIL2("Unknown switch condition (?(%.2s", RExC_parse);
2132 RExC_parse--; /* for vFAIL to print correctly */
2133 vFAIL("Sequence (? incomplete");
2138 while (*RExC_parse && strchr("iogcmsx", *RExC_parse)) {
2139 if (*RExC_parse != 'o')
2140 pmflag(flagsp, *RExC_parse);
2143 if (*RExC_parse == '-') {
2148 RExC_flags16 |= posflags;
2149 RExC_flags16 &= ~negflags;
2150 if (*RExC_parse == ':') {
2156 if (*RExC_parse != ')') {
2158 vFAIL3("Sequence (%.*s...) not recognized", RExC_parse-seqstart, seqstart);
2160 nextchar(pRExC_state);
2168 ret = reganode(pRExC_state, OPEN, parno);
2175 /* Pick up the branches, linking them together. */
2176 br = regbranch(pRExC_state, &flags, 1);
2179 if (*RExC_parse == '|') {
2180 if (!SIZE_ONLY && RExC_extralen) {
2181 reginsert(pRExC_state, BRANCHJ, br);
2184 reginsert(pRExC_state, BRANCH, br);
2187 RExC_extralen += 1; /* For BRANCHJ-BRANCH. */
2189 else if (paren == ':') {
2190 *flagp |= flags&SIMPLE;
2192 if (open) { /* Starts with OPEN. */
2193 regtail(pRExC_state, ret, br); /* OPEN -> first. */
2195 else if (paren != '?') /* Not Conditional */
2199 *flagp |= flags&SPSTART;
2201 while (*RExC_parse == '|') {
2202 if (!SIZE_ONLY && RExC_extralen) {
2203 ender = reganode(pRExC_state, LONGJMP,0);
2204 regtail(pRExC_state, NEXTOPER(NEXTOPER(lastbr)), ender); /* Append to the previous. */
2207 RExC_extralen += 2; /* Account for LONGJMP. */
2208 nextchar(pRExC_state);
2209 br = regbranch(pRExC_state, &flags, 0);
2212 regtail(pRExC_state, lastbr, br); /* BRANCH -> BRANCH. */
2216 *flagp |= flags&SPSTART;
2219 if (have_branch || paren != ':') {
2220 /* Make a closing node, and hook it on the end. */
2223 ender = reg_node(pRExC_state, TAIL);
2226 ender = reganode(pRExC_state, CLOSE, parno);
2232 *flagp &= ~HASWIDTH;
2235 ender = reg_node(pRExC_state, SUCCEED);
2238 ender = reg_node(pRExC_state, END);
2241 regtail(pRExC_state, lastbr, ender);
2244 /* Hook the tails of the branches to the closing node. */
2245 for (br = ret; br != NULL; br = regnext(br)) {
2246 regoptail(pRExC_state, br, ender);
2253 static char parens[] = "=!<,>";
2255 if (paren && (p = strchr(parens, paren))) {
2256 int node = ((p - parens) % 2) ? UNLESSM : IFMATCH;
2257 int flag = (p - parens) > 1;
2260 node = SUSPEND, flag = 0;
2261 reginsert(pRExC_state, node,ret);
2263 regtail(pRExC_state, ret, reg_node(pRExC_state, TAIL));
2267 /* Check for proper termination. */
2269 RExC_flags16 = oregflags;
2270 if (RExC_parse >= RExC_end || *nextchar(pRExC_state) != ')') {
2271 RExC_parse = oregcomp_parse;
2272 vFAIL("Unmatched (");
2275 else if (!paren && RExC_parse < RExC_end) {
2276 if (*RExC_parse == ')') {
2278 vFAIL("Unmatched )");
2281 FAIL("Junk on end of regexp"); /* "Can't happen". */
2289 - regbranch - one alternative of an | operator
2291 * Implements the concatenation operator.
2294 S_regbranch(pTHX_ RExC_state_t *pRExC_state, I32 *flagp, I32 first)
2296 register regnode *ret;
2297 register regnode *chain = NULL;
2298 register regnode *latest;
2299 I32 flags = 0, c = 0;
2304 if (!SIZE_ONLY && RExC_extralen)
2305 ret = reganode(pRExC_state, BRANCHJ,0);
2307 ret = reg_node(pRExC_state, BRANCH);
2310 if (!first && SIZE_ONLY)
2311 RExC_extralen += 1; /* BRANCHJ */
2313 *flagp = WORST; /* Tentatively. */
2316 nextchar(pRExC_state);
2317 while (RExC_parse < RExC_end && *RExC_parse != '|' && *RExC_parse != ')') {
2319 latest = regpiece(pRExC_state, &flags);
2320 if (latest == NULL) {
2321 if (flags & TRYAGAIN)
2325 else if (ret == NULL)
2327 *flagp |= flags&HASWIDTH;
2328 if (chain == NULL) /* First piece. */
2329 *flagp |= flags&SPSTART;
2332 regtail(pRExC_state, chain, latest);
2337 if (chain == NULL) { /* Loop ran zero times. */
2338 chain = reg_node(pRExC_state, NOTHING);
2343 *flagp |= flags&SIMPLE;
2350 - regpiece - something followed by possible [*+?]
2352 * Note that the branching code sequences used for ? and the general cases
2353 * of * and + are somewhat optimized: they use the same NOTHING node as
2354 * both the endmarker for their branch list and the body of the last branch.
2355 * It might seem that this node could be dispensed with entirely, but the
2356 * endmarker role is not redundant.
2359 S_regpiece(pTHX_ RExC_state_t *pRExC_state, I32 *flagp)
2361 register regnode *ret;
2363 register char *next;
2365 char *origparse = RExC_parse;
2368 I32 max = REG_INFTY;
2370 ret = regatom(pRExC_state, &flags);
2372 if (flags & TRYAGAIN)
2379 if (op == '{' && regcurly(RExC_parse)) {
2380 next = RExC_parse + 1;
2382 while (isDIGIT(*next) || *next == ',') {
2391 if (*next == '}') { /* got one */
2395 min = atoi(RExC_parse);
2399 maxpos = RExC_parse;
2401 if (!max && *maxpos != '0')
2402 max = REG_INFTY; /* meaning "infinity" */
2403 else if (max >= REG_INFTY)
2404 vFAIL2("Quantifier in {,} bigger than %d", REG_INFTY - 1);
2406 nextchar(pRExC_state);
2409 if ((flags&SIMPLE)) {
2410 RExC_naughty += 2 + RExC_naughty / 2;
2411 reginsert(pRExC_state, CURLY, ret);
2414 regnode *w = reg_node(pRExC_state, WHILEM);
2417 regtail(pRExC_state, ret, w);
2418 if (!SIZE_ONLY && RExC_extralen) {
2419 reginsert(pRExC_state, LONGJMP,ret);
2420 reginsert(pRExC_state, NOTHING,ret);
2421 NEXT_OFF(ret) = 3; /* Go over LONGJMP. */
2423 reginsert(pRExC_state, CURLYX,ret);
2424 if (!SIZE_ONLY && RExC_extralen)
2425 NEXT_OFF(ret) = 3; /* Go over NOTHING to LONGJMP. */
2426 regtail(pRExC_state, ret, reg_node(pRExC_state, NOTHING));
2428 RExC_whilem_seen++, RExC_extralen += 3;
2429 RExC_naughty += 4 + RExC_naughty; /* compound interest */
2437 if (max && max < min)
2438 vFAIL("Can't do {n,m} with n > m");
2453 #if 0 /* Now runtime fix should be reliable. */
2455 /* if this is reinstated, don't forget to put this back into perldiag:
2457 =item Regexp *+ operand could be empty at {#} in regex m/%s/
2459 (F) The part of the regexp subject to either the * or + quantifier
2460 could match an empty string. The {#} shows in the regular
2461 expression about where the problem was discovered.
2465 if (!(flags&HASWIDTH) && op != '?')
2466 vFAIL("Regexp *+ operand could be empty");
2469 nextchar(pRExC_state);
2471 *flagp = (op != '+') ? (WORST|SPSTART|HASWIDTH) : (WORST|HASWIDTH);
2473 if (op == '*' && (flags&SIMPLE)) {
2474 reginsert(pRExC_state, STAR, ret);
2478 else if (op == '*') {
2482 else if (op == '+' && (flags&SIMPLE)) {
2483 reginsert(pRExC_state, PLUS, ret);
2487 else if (op == '+') {
2491 else if (op == '?') {
2496 if (ckWARN(WARN_REGEXP) && !SIZE_ONLY && !(flags&HASWIDTH) && max > REG_INFTY/3) {
2498 "%.*s matches null string many times",
2499 RExC_parse - origparse,
2503 if (*RExC_parse == '?') {
2504 nextchar(pRExC_state);
2505 reginsert(pRExC_state, MINMOD, ret);
2506 regtail(pRExC_state, ret, ret + NODE_STEP_REGNODE);
2508 if (ISMULT2(RExC_parse)) {
2510 vFAIL("Nested quantifiers");
2517 - regatom - the lowest level
2519 * Optimization: gobbles an entire sequence of ordinary characters so that
2520 * it can turn them into a single node, which is smaller to store and
2521 * faster to run. Backslashed characters are exceptions, each becoming a
2522 * separate node; the code is simpler that way and it's not worth fixing.
2524 * [Yes, it is worth fixing, some scripts can run twice the speed.] */
2526 S_regatom(pTHX_ RExC_state_t *pRExC_state, I32 *flagp)
2528 register regnode *ret = 0;
2531 *flagp = WORST; /* Tentatively. */
2534 switch (*RExC_parse) {
2536 RExC_seen_zerolen++;
2537 nextchar(pRExC_state);
2538 if (RExC_flags16 & PMf_MULTILINE)
2539 ret = reg_node(pRExC_state, MBOL);
2540 else if (RExC_flags16 & PMf_SINGLELINE)
2541 ret = reg_node(pRExC_state, SBOL);
2543 ret = reg_node(pRExC_state, BOL);
2546 nextchar(pRExC_state);
2548 RExC_seen_zerolen++;
2549 if (RExC_flags16 & PMf_MULTILINE)
2550 ret = reg_node(pRExC_state, MEOL);
2551 else if (RExC_flags16 & PMf_SINGLELINE)
2552 ret = reg_node(pRExC_state, SEOL);
2554 ret = reg_node(pRExC_state, EOL);
2557 nextchar(pRExC_state);
2558 if (RExC_flags16 & PMf_SINGLELINE)
2559 ret = reg_node(pRExC_state, SANY);
2561 ret = reg_node(pRExC_state, REG_ANY);
2562 *flagp |= HASWIDTH|SIMPLE;
2567 char *oregcomp_parse = ++RExC_parse;
2568 ret = regclass(pRExC_state);
2569 if (*RExC_parse != ']') {
2570 RExC_parse = oregcomp_parse;
2571 vFAIL("Unmatched [");
2573 nextchar(pRExC_state);
2574 *flagp |= HASWIDTH|SIMPLE;
2578 nextchar(pRExC_state);
2579 ret = reg(pRExC_state, 1, &flags);
2581 if (flags & TRYAGAIN) {
2582 if (RExC_parse == RExC_end) {
2583 /* Make parent create an empty node if needed. */
2591 *flagp |= flags&(HASWIDTH|SPSTART|SIMPLE);
2595 if (flags & TRYAGAIN) {
2599 vFAIL("Internal urp");
2600 /* Supposed to be caught earlier. */
2603 if (!regcurly(RExC_parse)) {
2612 vFAIL("Quantifier follows nothing");
2615 switch (*++RExC_parse) {
2617 RExC_seen_zerolen++;
2618 ret = reg_node(pRExC_state, SBOL);
2620 nextchar(pRExC_state);
2623 ret = reg_node(pRExC_state, GPOS);
2624 RExC_seen |= REG_SEEN_GPOS;
2626 nextchar(pRExC_state);
2629 ret = reg_node(pRExC_state, SEOL);
2631 nextchar(pRExC_state);
2634 ret = reg_node(pRExC_state, EOS);
2636 RExC_seen_zerolen++; /* Do not optimize RE away */
2637 nextchar(pRExC_state);
2640 ret = reg_node(pRExC_state, SANY);
2641 *flagp |= HASWIDTH|SIMPLE;
2642 nextchar(pRExC_state);
2645 ret = reg_node(pRExC_state, CLUMP);
2647 nextchar(pRExC_state);
2648 if (UTF && !PL_utf8_mark)
2649 is_utf8_mark((U8*)"~"); /* preload table */
2652 ret = reg_node(pRExC_state, LOC ? ALNUML : ALNUM);
2653 *flagp |= HASWIDTH|SIMPLE;
2654 nextchar(pRExC_state);
2655 if (UTF && !PL_utf8_alnum)
2656 is_utf8_alnum((U8*)"a"); /* preload table */
2659 ret = reg_node(pRExC_state, LOC ? NALNUML : NALNUM);
2660 *flagp |= HASWIDTH|SIMPLE;
2661 nextchar(pRExC_state);
2662 if (UTF && !PL_utf8_alnum)
2663 is_utf8_alnum((U8*)"a"); /* preload table */
2666 RExC_seen_zerolen++;
2667 RExC_seen |= REG_SEEN_LOOKBEHIND;
2668 ret = reg_node(pRExC_state, LOC ? BOUNDL : BOUND);
2670 nextchar(pRExC_state);
2671 if (UTF && !PL_utf8_alnum)
2672 is_utf8_alnum((U8*)"a"); /* preload table */
2675 RExC_seen_zerolen++;
2676 RExC_seen |= REG_SEEN_LOOKBEHIND;
2677 ret = reg_node(pRExC_state, LOC ? NBOUNDL : NBOUND);
2679 nextchar(pRExC_state);
2680 if (UTF && !PL_utf8_alnum)
2681 is_utf8_alnum((U8*)"a"); /* preload table */
2684 ret = reg_node(pRExC_state, LOC ? SPACEL : SPACE);
2685 *flagp |= HASWIDTH|SIMPLE;
2686 nextchar(pRExC_state);
2687 if (UTF && !PL_utf8_space)
2688 is_utf8_space((U8*)" "); /* preload table */
2691 ret = reg_node(pRExC_state, LOC ? NSPACEL : NSPACE);
2692 *flagp |= HASWIDTH|SIMPLE;
2693 nextchar(pRExC_state);
2694 if (UTF && !PL_utf8_space)
2695 is_utf8_space((U8*)" "); /* preload table */
2698 ret = reg_node(pRExC_state, DIGIT);
2699 *flagp |= HASWIDTH|SIMPLE;
2700 nextchar(pRExC_state);
2701 if (UTF && !PL_utf8_digit)
2702 is_utf8_digit((U8*)"1"); /* preload table */
2705 ret = reg_node(pRExC_state, NDIGIT);
2706 *flagp |= HASWIDTH|SIMPLE;
2707 nextchar(pRExC_state);
2708 if (UTF && !PL_utf8_digit)
2709 is_utf8_digit((U8*)"1"); /* preload table */
2713 { /* a lovely hack--pretend we saw [\pX] instead */
2714 char* oldregxend = RExC_end;
2716 if (RExC_parse[1] == '{') {
2717 RExC_end = strchr(RExC_parse, '}');
2720 RExC_end = oldregxend;
2721 vFAIL("Missing right brace on \\p{}");
2726 RExC_end = RExC_parse + 2;
2729 ret = regclass(pRExC_state);
2731 RExC_end = oldregxend;
2733 nextchar(pRExC_state);
2734 *flagp |= HASWIDTH|SIMPLE;
2747 case '1': case '2': case '3': case '4':
2748 case '5': case '6': case '7': case '8': case '9':
2750 I32 num = atoi(RExC_parse);
2752 if (num > 9 && num >= RExC_npar)
2755 while (isDIGIT(*RExC_parse))
2758 if (!SIZE_ONLY && num > RExC_rx->nparens)
2759 vFAIL("Reference to nonexistent group");
2761 ret = reganode(pRExC_state, FOLD
2762 ? (LOC ? REFFL : REFF)
2766 nextchar(pRExC_state);
2771 if (RExC_parse >= RExC_end)
2772 FAIL("Trailing \\");
2775 /* Do not generate `unrecognized' warnings here, we fall
2776 back into the quick-grab loop below */
2782 if (RExC_flags16 & PMf_EXTENDED) {
2783 while (RExC_parse < RExC_end && *RExC_parse != '\n') RExC_parse++;
2784 if (RExC_parse < RExC_end)
2790 register STRLEN len;
2799 ret = reg_node(pRExC_state, FOLD
2800 ? (LOC ? EXACTFL : EXACTF)
2803 for (len = 0, p = RExC_parse - 1;
2804 len < 127 && p < RExC_end;
2809 if (RExC_flags16 & PMf_EXTENDED)
2810 p = regwhite(p, RExC_end);
2872 char* e = strchr(p, '}');
2876 vFAIL("Missing right brace on \\x{}");
2879 numlen = 1; /* allow underscores */
2880 ender = (UV)scan_hex(p + 1, e - p - 1, &numlen);
2881 /* numlen is generous */
2882 if (numlen + len >= 127) {
2890 numlen = 0; /* disallow underscores */
2891 ender = (UV)scan_hex(p, 2, &numlen);
2897 ender = UCHARAT(p++);
2898 ender = toCTRL(ender);
2900 case '0': case '1': case '2': case '3':case '4':
2901 case '5': case '6': case '7': case '8':case '9':
2903 (isDIGIT(p[1]) && atoi(p) >= RExC_npar) ) {
2904 numlen = 0; /* disallow underscores */
2905 ender = (UV)scan_oct(p, 3, &numlen);
2915 FAIL("Trailing \\");
2918 if (!SIZE_ONLY && ckWARN(WARN_REGEXP) && isALPHA(*p))
2919 vWARN2(p +1, "Unrecognized escape \\%c passed through", *p);
2920 goto normal_default;
2925 if (UTF8_IS_START(*p) && UTF) {
2926 ender = utf8_to_uv((U8*)p, RExC_end - p,
2934 if (RExC_flags16 & PMf_EXTENDED)
2935 p = regwhite(p, RExC_end);
2938 ender = toLOWER_LC_uni(ender);
2940 ender = toLOWER_uni(ender);
2942 if (ISMULT2(p)) { /* Back off on ?+*. */
2945 /* ender is a Unicode value so it can be > 0xff --
2946 * in other words, do not use UTF8_IS_CONTINUED(). */
2947 else if (ender >= 0x80 && UTF) {
2948 reguni(pRExC_state, ender, s, &numlen);
2958 /* ender is a Unicode value so it can be > 0xff --
2959 * in other words, do not use UTF8_IS_CONTINUED(). */
2960 if (ender >= 0x80 && UTF) {
2961 reguni(pRExC_state, ender, s, &numlen);
2970 nextchar(pRExC_state);
2972 /* len is STRLEN which is unsigned, need to copy to signed */
2975 vFAIL("Internal disaster");
2984 RExC_size += STR_SZ(len);
2986 RExC_emit += STR_SZ(len);
2995 S_regwhite(pTHX_ char *p, char *e)
3000 else if (*p == '#') {
3003 } while (p < e && *p != '\n');
3011 /* Parse POSIX character classes: [[:foo:]], [[=foo=]], [[.foo.]].
3012 Character classes ([:foo:]) can also be negated ([:^foo:]).
3013 Returns a named class id (ANYOF_XXX) if successful, -1 otherwise.
3014 Equivalence classes ([=foo=]) and composites ([.foo.]) are parsed,
3015 but trigger warnings because they are currently unimplemented. */
3017 S_regpposixcc(pTHX_ RExC_state_t *pRExC_state, I32 value)
3020 I32 namedclass = OOB_NAMEDCLASS;
3022 if (value == '[' && RExC_parse + 1 < RExC_end &&
3023 /* I smell either [: or [= or [. -- POSIX has been here, right? */
3024 (*RExC_parse == ':' ||
3025 *RExC_parse == '=' ||
3026 *RExC_parse == '.')) {
3027 char c = *RExC_parse;
3028 char* s = RExC_parse++;
3030 while (RExC_parse < RExC_end && *RExC_parse != c)
3032 if (RExC_parse == RExC_end)
3033 /* Grandfather lone [:, [=, [. */
3036 char* t = RExC_parse++; /* skip over the c */
3038 if (*RExC_parse == ']') {
3039 RExC_parse++; /* skip over the ending ] */
3042 I32 complement = *posixcc == '^' ? *posixcc++ : 0;
3043 I32 skip = 5; /* the most common skip */
3047 if (strnEQ(posixcc, "alnum", 5))
3049 complement ? ANYOF_NALNUMC : ANYOF_ALNUMC;
3050 else if (strnEQ(posixcc, "alpha", 5))
3052 complement ? ANYOF_NALPHA : ANYOF_ALPHA;
3053 else if (strnEQ(posixcc, "ascii", 5))
3055 complement ? ANYOF_NASCII : ANYOF_ASCII;
3058 if (strnEQ(posixcc, "blank", 5))
3060 complement ? ANYOF_NBLANK : ANYOF_BLANK;
3063 if (strnEQ(posixcc, "cntrl", 5))
3065 complement ? ANYOF_NCNTRL : ANYOF_CNTRL;
3068 if (strnEQ(posixcc, "digit", 5))
3070 complement ? ANYOF_NDIGIT : ANYOF_DIGIT;
3073 if (strnEQ(posixcc, "graph", 5))
3075 complement ? ANYOF_NGRAPH : ANYOF_GRAPH;
3078 if (strnEQ(posixcc, "lower", 5))
3080 complement ? ANYOF_NLOWER : ANYOF_LOWER;
3083 if (strnEQ(posixcc, "print", 5))
3085 complement ? ANYOF_NPRINT : ANYOF_PRINT;
3086 else if (strnEQ(posixcc, "punct", 5))
3088 complement ? ANYOF_NPUNCT : ANYOF_PUNCT;
3091 if (strnEQ(posixcc, "space", 5))
3093 complement ? ANYOF_NPSXSPC : ANYOF_PSXSPC;
3096 if (strnEQ(posixcc, "upper", 5))
3098 complement ? ANYOF_NUPPER : ANYOF_UPPER;
3100 case 'w': /* this is not POSIX, this is the Perl \w */
3101 if (strnEQ(posixcc, "word", 4)) {
3103 complement ? ANYOF_NALNUM : ANYOF_ALNUM;
3108 if (strnEQ(posixcc, "xdigit", 6)) {
3110 complement ? ANYOF_NXDIGIT : ANYOF_XDIGIT;
3115 if (namedclass == OOB_NAMEDCLASS ||
3116 posixcc[skip] != ':' ||
3117 posixcc[skip+1] != ']')
3119 Simple_vFAIL3("POSIX class [:%.*s:] unknown",
3122 } else if (!SIZE_ONLY) {
3123 /* [[=foo=]] and [[.foo.]] are still future. */
3125 /* adjust RExC_parse so the warning shows after
3127 while (*RExC_parse && *RExC_parse != ']')
3129 Simple_vFAIL3("POSIX syntax [%c %c] is reserved for future extensions", c, c);
3132 /* Maternal grandfather:
3133 * "[:" ending in ":" but not in ":]" */
3143 S_checkposixcc(pTHX_ RExC_state_t *pRExC_state)
3145 if (!SIZE_ONLY && ckWARN(WARN_REGEXP) &&
3146 (*RExC_parse == ':' ||
3147 *RExC_parse == '=' ||
3148 *RExC_parse == '.')) {
3149 char *s = RExC_parse;
3152 while(*s && isALNUM(*s))
3154 if (*s && c == *s && s[1] == ']') {
3155 vWARN3(s+2, "POSIX syntax [%c %c] belongs inside character classes", c, c);
3157 /* [[=foo=]] and [[.foo.]] are still future. */
3158 if (c == '=' || c == '.')
3160 /* adjust RExC_parse so the error shows after
3162 while (*RExC_parse && *RExC_parse++ != ']')
3164 Simple_vFAIL3("POSIX syntax [%c %c] is reserved for future extensions", c, c);
3171 S_regclass(pTHX_ RExC_state_t *pRExC_state)
3174 register IV lastvalue = OOB_UNICODE;
3175 register IV range = 0;
3176 register regnode *ret;
3180 bool need_class = 0;
3184 bool dont_optimize_invert = FALSE;
3186 ret = reganode(pRExC_state, ANYOF, 0);
3189 ANYOF_FLAGS(ret) = 0;
3191 if (*RExC_parse == '^') { /* Complement of range. */
3195 ANYOF_FLAGS(ret) |= ANYOF_INVERT;
3199 RExC_size += ANYOF_SKIP;
3201 RExC_emit += ANYOF_SKIP;
3203 ANYOF_FLAGS(ret) |= ANYOF_FOLD;
3205 ANYOF_FLAGS(ret) |= ANYOF_LOCALE;
3206 ANYOF_BITMAP_ZERO(ret);
3207 listsv = newSVpvn("# comment\n", 10);
3210 if (!SIZE_ONLY && ckWARN(WARN_REGEXP))
3211 checkposixcc(pRExC_state);
3213 if (*RExC_parse == ']' || *RExC_parse == '-')
3214 goto charclassloop; /* allow 1st char to be ] or - */
3216 while (RExC_parse < RExC_end && *RExC_parse != ']') {
3220 namedclass = OOB_NAMEDCLASS; /* initialize as illegal */
3223 rangebegin = RExC_parse;
3225 value = utf8_to_uv((U8*)RExC_parse,
3226 RExC_end - RExC_parse,
3228 RExC_parse += numlen;
3231 value = UCHARAT(RExC_parse++);
3233 namedclass = regpposixcc(pRExC_state, value);
3234 else if (value == '\\') {
3236 value = utf8_to_uv((U8*)RExC_parse,
3237 RExC_end - RExC_parse,
3239 RExC_parse += numlen;
3242 value = UCHARAT(RExC_parse++);
3243 /* Some compilers cannot handle switching on 64-bit integer
3244 * values, therefore value cannot be an UV. Yes, this will
3245 * be a problem later if we want switch on Unicode.
3246 * A similar issue a little bit later when switching on
3247 * namedclass. --jhi */
3248 switch ((I32)value) {
3249 case 'w': namedclass = ANYOF_ALNUM; break;
3250 case 'W': namedclass = ANYOF_NALNUM; break;
3251 case 's': namedclass = ANYOF_SPACE; break;
3252 case 'S': namedclass = ANYOF_NSPACE; break;
3253 case 'd': namedclass = ANYOF_DIGIT; break;
3254 case 'D': namedclass = ANYOF_NDIGIT; break;
3257 if (*RExC_parse == '{') {
3258 e = strchr(RExC_parse++, '}');
3260 vFAIL("Missing right brace on \\p{}");
3269 Perl_sv_catpvf(aTHX_ listsv,
3270 "+utf8::%.*s\n", (int)n, RExC_parse);
3272 Perl_sv_catpvf(aTHX_ listsv,
3273 "!utf8::%.*s\n", (int)n, RExC_parse);
3276 ANYOF_FLAGS(ret) |= ANYOF_UNICODE;
3278 case 'n': value = '\n'; break;
3279 case 'r': value = '\r'; break;
3280 case 't': value = '\t'; break;
3281 case 'f': value = '\f'; break;
3282 case 'b': value = '\b'; break;
3284 case 'e': value = '\033'; break;
3285 case 'a': value = '\007'; break;
3287 case 'e': value = '\047'; break;
3288 case 'a': value = '\057'; break;
3291 if (*RExC_parse == '{') {
3292 e = strchr(RExC_parse++, '}');
3294 vFAIL("Missing right brace on \\x{}");
3295 numlen = 1; /* allow underscores */
3296 value = (UV)scan_hex(RExC_parse,
3302 numlen = 0; /* disallow underscores */
3303 value = (UV)scan_hex(RExC_parse, 2, &numlen);
3304 RExC_parse += numlen;
3308 value = UCHARAT(RExC_parse++);
3309 value = toCTRL(value);
3311 case '0': case '1': case '2': case '3': case '4':
3312 case '5': case '6': case '7': case '8': case '9':
3313 numlen = 0; /* disallow underscores */
3314 value = (UV)scan_oct(--RExC_parse, 3, &numlen);
3315 RExC_parse += numlen;
3318 if (!SIZE_ONLY && ckWARN(WARN_REGEXP) && isALPHA(value))
3320 "Unrecognized escape \\%c in character class passed through",
3324 } /* end of \blah */
3326 if (namedclass > OOB_NAMEDCLASS) { /* this is a named class \blah */
3328 if (!SIZE_ONLY && !need_class)
3329 ANYOF_CLASS_ZERO(ret);
3333 /* a bad range like a-\d, a-[:digit:] ? */
3336 if (ckWARN(WARN_REGEXP))
3338 "False [] range \"%*.*s\"",
3339 RExC_parse - rangebegin,
3340 RExC_parse - rangebegin,
3342 if (lastvalue < 256) {
3343 ANYOF_BITMAP_SET(ret, lastvalue);
3344 ANYOF_BITMAP_SET(ret, '-');
3347 ANYOF_FLAGS(ret) |= ANYOF_UNICODE;
3348 Perl_sv_catpvf(aTHX_ listsv,
3349 /* 0x002D is Unicode for '-' */
3350 "%04"UVxf"\n002D\n", (UV)lastvalue);
3354 range = 0; /* this was not a true range */
3358 /* Possible truncation here but in some 64-bit environments
3359 * the compiler gets heartburn about switch on 64-bit values.
3360 * A similar issue a little earlier when switching on value.
3362 switch ((I32)namedclass) {
3365 ANYOF_CLASS_SET(ret, ANYOF_ALNUM);
3367 for (value = 0; value < 256; value++)
3369 ANYOF_BITMAP_SET(ret, value);
3371 dont_optimize_invert = TRUE;
3372 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsWord\n");
3376 ANYOF_CLASS_SET(ret, ANYOF_NALNUM);
3378 for (value = 0; value < 256; value++)
3379 if (!isALNUM(value))
3380 ANYOF_BITMAP_SET(ret, value);
3382 dont_optimize_invert = TRUE;
3383 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsWord\n");
3387 ANYOF_CLASS_SET(ret, ANYOF_ALNUMC);
3389 for (value = 0; value < 256; value++)
3390 if (isALNUMC(value))
3391 ANYOF_BITMAP_SET(ret, value);
3393 dont_optimize_invert = TRUE;
3394 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsAlnum\n");
3398 ANYOF_CLASS_SET(ret, ANYOF_NALNUMC);
3400 for (value = 0; value < 256; value++)
3401 if (!isALNUMC(value))
3402 ANYOF_BITMAP_SET(ret, value);
3404 dont_optimize_invert = TRUE;
3405 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsAlnum\n");
3409 ANYOF_CLASS_SET(ret, ANYOF_ALPHA);
3411 for (value = 0; value < 256; value++)
3413 ANYOF_BITMAP_SET(ret, value);
3415 dont_optimize_invert = TRUE;
3416 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsAlpha\n");
3420 ANYOF_CLASS_SET(ret, ANYOF_NALPHA);
3422 for (value = 0; value < 256; value++)
3423 if (!isALPHA(value))
3424 ANYOF_BITMAP_SET(ret, value);
3426 dont_optimize_invert = TRUE;
3427 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsAlpha\n");
3431 ANYOF_CLASS_SET(ret, ANYOF_ASCII);
3434 for (value = 0; value < 128; value++)
3435 ANYOF_BITMAP_SET(ret, value);
3437 for (value = 0; value < 256; value++)
3439 ANYOF_BITMAP_SET(ret, value);
3442 dont_optimize_invert = TRUE;
3443 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsASCII\n");
3447 ANYOF_CLASS_SET(ret, ANYOF_NASCII);
3450 for (value = 128; value < 256; value++)
3451 ANYOF_BITMAP_SET(ret, value);
3453 for (value = 0; value < 256; value++)
3454 if (!isASCII(value))
3455 ANYOF_BITMAP_SET(ret, value);
3458 dont_optimize_invert = TRUE;
3459 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsASCII\n");
3463 ANYOF_CLASS_SET(ret, ANYOF_BLANK);
3465 for (value = 0; value < 256; value++)
3467 ANYOF_BITMAP_SET(ret, value);
3469 dont_optimize_invert = TRUE;
3470 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsBlank\n");
3474 ANYOF_CLASS_SET(ret, ANYOF_NBLANK);
3476 for (value = 0; value < 256; value++)
3477 if (!isBLANK(value))
3478 ANYOF_BITMAP_SET(ret, value);
3480 dont_optimize_invert = TRUE;
3481 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsBlank\n");
3485 ANYOF_CLASS_SET(ret, ANYOF_CNTRL);
3487 for (value = 0; value < 256; value++)
3489 ANYOF_BITMAP_SET(ret, value);
3491 dont_optimize_invert = TRUE;
3492 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsCntrl\n");
3496 ANYOF_CLASS_SET(ret, ANYOF_NCNTRL);
3498 for (value = 0; value < 256; value++)
3499 if (!isCNTRL(value))
3500 ANYOF_BITMAP_SET(ret, value);
3502 dont_optimize_invert = TRUE;
3503 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsCntrl\n");
3507 ANYOF_CLASS_SET(ret, ANYOF_DIGIT);
3509 /* consecutive digits assumed */
3510 for (value = '0'; value <= '9'; value++)
3511 ANYOF_BITMAP_SET(ret, value);
3513 dont_optimize_invert = TRUE;
3514 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsDigit\n");
3518 ANYOF_CLASS_SET(ret, ANYOF_NDIGIT);
3520 /* consecutive digits assumed */
3521 for (value = 0; value < '0'; value++)
3522 ANYOF_BITMAP_SET(ret, value);
3523 for (value = '9' + 1; value < 256; value++)
3524 ANYOF_BITMAP_SET(ret, value);
3526 dont_optimize_invert = TRUE;
3527 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsDigit\n");
3531 ANYOF_CLASS_SET(ret, ANYOF_GRAPH);
3533 for (value = 0; value < 256; value++)
3535 ANYOF_BITMAP_SET(ret, value);
3537 dont_optimize_invert = TRUE;
3538 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsGraph\n");
3542 ANYOF_CLASS_SET(ret, ANYOF_NGRAPH);
3544 for (value = 0; value < 256; value++)
3545 if (!isGRAPH(value))
3546 ANYOF_BITMAP_SET(ret, value);
3548 dont_optimize_invert = TRUE;
3549 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsGraph\n");
3553 ANYOF_CLASS_SET(ret, ANYOF_LOWER);
3555 for (value = 0; value < 256; value++)
3557 ANYOF_BITMAP_SET(ret, value);
3559 dont_optimize_invert = TRUE;
3560 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsLower\n");
3564 ANYOF_CLASS_SET(ret, ANYOF_NLOWER);
3566 for (value = 0; value < 256; value++)
3567 if (!isLOWER(value))
3568 ANYOF_BITMAP_SET(ret, value);
3570 dont_optimize_invert = TRUE;
3571 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsLower\n");
3575 ANYOF_CLASS_SET(ret, ANYOF_PRINT);
3577 for (value = 0; value < 256; value++)
3579 ANYOF_BITMAP_SET(ret, value);
3581 dont_optimize_invert = TRUE;
3582 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsPrint\n");
3586 ANYOF_CLASS_SET(ret, ANYOF_NPRINT);
3588 for (value = 0; value < 256; value++)
3589 if (!isPRINT(value))
3590 ANYOF_BITMAP_SET(ret, value);
3592 dont_optimize_invert = TRUE;
3593 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsPrint\n");
3597 ANYOF_CLASS_SET(ret, ANYOF_PSXSPC);
3599 for (value = 0; value < 256; value++)
3600 if (isPSXSPC(value))
3601 ANYOF_BITMAP_SET(ret, value);
3603 dont_optimize_invert = TRUE;
3604 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsSpace\n");
3608 ANYOF_CLASS_SET(ret, ANYOF_NPSXSPC);
3610 for (value = 0; value < 256; value++)
3611 if (!isPSXSPC(value))
3612 ANYOF_BITMAP_SET(ret, value);
3614 dont_optimize_invert = TRUE;
3615 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsSpace\n");
3619 ANYOF_CLASS_SET(ret, ANYOF_PUNCT);
3621 for (value = 0; value < 256; value++)
3623 ANYOF_BITMAP_SET(ret, value);
3625 dont_optimize_invert = TRUE;
3626 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsPunct\n");
3630 ANYOF_CLASS_SET(ret, ANYOF_NPUNCT);
3632 for (value = 0; value < 256; value++)
3633 if (!isPUNCT(value))
3634 ANYOF_BITMAP_SET(ret, value);
3636 dont_optimize_invert = TRUE;
3637 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsPunct\n");
3641 ANYOF_CLASS_SET(ret, ANYOF_SPACE);
3643 for (value = 0; value < 256; value++)
3645 ANYOF_BITMAP_SET(ret, value);
3647 dont_optimize_invert = TRUE;
3648 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsSpacePerl\n");
3652 ANYOF_CLASS_SET(ret, ANYOF_NSPACE);
3654 for (value = 0; value < 256; value++)
3655 if (!isSPACE(value))
3656 ANYOF_BITMAP_SET(ret, value);
3658 dont_optimize_invert = TRUE;
3659 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsSpacePerl\n");
3663 ANYOF_CLASS_SET(ret, ANYOF_UPPER);
3665 for (value = 0; value < 256; value++)
3667 ANYOF_BITMAP_SET(ret, value);
3669 dont_optimize_invert = TRUE;
3670 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsUpper\n");
3674 ANYOF_CLASS_SET(ret, ANYOF_NUPPER);
3676 for (value = 0; value < 256; value++)
3677 if (!isUPPER(value))
3678 ANYOF_BITMAP_SET(ret, value);
3680 dont_optimize_invert = TRUE;
3681 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsUpper\n");
3685 ANYOF_CLASS_SET(ret, ANYOF_XDIGIT);
3687 for (value = 0; value < 256; value++)
3688 if (isXDIGIT(value))
3689 ANYOF_BITMAP_SET(ret, value);
3691 dont_optimize_invert = TRUE;
3692 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsXDigit\n");
3696 ANYOF_CLASS_SET(ret, ANYOF_NXDIGIT);
3698 for (value = 0; value < 256; value++)
3699 if (!isXDIGIT(value))
3700 ANYOF_BITMAP_SET(ret, value);
3702 dont_optimize_invert = TRUE;
3703 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsXDigit\n");
3706 vFAIL("Invalid [::] class");
3710 ANYOF_FLAGS(ret) |= ANYOF_CLASS;
3713 } /* end of namedclass \blah */
3716 if (lastvalue > value) /* b-a */ {
3717 Simple_vFAIL4("Invalid [] range \"%*.*s\"",
3718 RExC_parse - rangebegin,
3719 RExC_parse - rangebegin,
3722 range = 0; /* not a true range */
3725 lastvalue = value; /* save the beginning of the range */
3726 if (*RExC_parse == '-' && RExC_parse+1 < RExC_end &&
3727 RExC_parse[1] != ']') {
3730 /* a bad range like \w-, [:word:]- ? */
3731 if (namedclass > OOB_NAMEDCLASS) {
3732 if (ckWARN(WARN_REGEXP))
3734 "False [] range \"%*.*s\"",
3735 RExC_parse - rangebegin,
3736 RExC_parse - rangebegin,
3739 ANYOF_BITMAP_SET(ret, '-');
3741 range = 1; /* yeah, it's a range! */
3742 continue; /* but do it the next time */
3746 /* now is the next time */
3748 if (lastvalue < 256 && value < 256) {
3749 #ifndef ASCIIish /* EBCDIC, for example. */
3750 if ((isLOWER(lastvalue) && isLOWER(value)) ||
3751 (isUPPER(lastvalue) && isUPPER(value)))
3754 if (isLOWER(lastvalue)) {
3755 for (i = lastvalue; i <= value; i++)
3757 ANYOF_BITMAP_SET(ret, i);
3759 for (i = lastvalue; i <= value; i++)
3761 ANYOF_BITMAP_SET(ret, i);
3766 for ( ; lastvalue <= value; lastvalue++)
3767 ANYOF_BITMAP_SET(ret, lastvalue);
3769 ANYOF_FLAGS(ret) |= ANYOF_UNICODE;
3770 if (lastvalue < value)
3771 Perl_sv_catpvf(aTHX_ listsv, "%04"UVxf"\t%04"UVxf"\n",
3772 (UV)lastvalue, (UV)value);
3774 Perl_sv_catpvf(aTHX_ listsv, "%04"UVxf"\n",
3779 range = 0; /* this range (if it was one) is done now */
3784 RExC_size += ANYOF_CLASS_ADD_SKIP;
3786 RExC_emit += ANYOF_CLASS_ADD_SKIP;
3789 /* optimize case-insensitive simple patterns (e.g. /[a-z]/i) */
3792 /* If the only flag is folding (plus possibly inversion). */
3793 (ANYOF_FLAGS_ALL ^ ANYOF_INVERT) == ANYOF_FOLD)) {
3794 for (value = 0; value < 256; ++value) {
3795 if (ANYOF_BITMAP_TEST(ret, value)) {
3796 IV fold = PL_fold[value];
3799 ANYOF_BITMAP_SET(ret, fold);
3802 ANYOF_FLAGS(ret) &= ~ANYOF_FOLD;
3805 /* optimize inverted simple patterns (e.g. [^a-z]) */
3806 if (!SIZE_ONLY && !dont_optimize_invert &&
3807 /* If the only flag is inversion. */
3808 (ANYOF_FLAGS(ret) & ANYOF_FLAGS_ALL) == ANYOF_INVERT) {
3809 for (value = 0; value < ANYOF_BITMAP_SIZE; ++value)
3810 ANYOF_BITMAP(ret)[value] ^= ANYOF_FLAGS_ALL;
3811 ANYOF_FLAGS(ret) = ANYOF_UNICODE_ALL;
3818 av_store(av, 0, listsv);
3819 av_store(av, 1, NULL);
3820 rv = newRV_noinc((SV*)av);
3821 n = add_data(pRExC_state, 1, "s");
3822 RExC_rx->data->data[n] = (void*)rv;
3830 S_nextchar(pTHX_ RExC_state_t *pRExC_state)
3832 char* retval = RExC_parse++;
3835 if (*RExC_parse == '(' && RExC_parse[1] == '?' &&
3836 RExC_parse[2] == '#') {
3837 while (*RExC_parse && *RExC_parse != ')')
3842 if (RExC_flags16 & PMf_EXTENDED) {
3843 if (isSPACE(*RExC_parse)) {
3847 else if (*RExC_parse == '#') {
3848 while (*RExC_parse && *RExC_parse != '\n')
3859 - reg_node - emit a node
3861 STATIC regnode * /* Location. */
3862 S_reg_node(pTHX_ RExC_state_t *pRExC_state, U8 op)
3864 register regnode *ret;
3865 register regnode *ptr;
3869 SIZE_ALIGN(RExC_size);
3874 NODE_ALIGN_FILL(ret);
3876 FILL_ADVANCE_NODE(ptr, op);
3883 - reganode - emit a node with an argument
3885 STATIC regnode * /* Location. */
3886 S_reganode(pTHX_ RExC_state_t *pRExC_state, U8 op, U32 arg)
3888 register regnode *ret;
3889 register regnode *ptr;
3893 SIZE_ALIGN(RExC_size);
3898 NODE_ALIGN_FILL(ret);
3900 FILL_ADVANCE_NODE_ARG(ptr, op, arg);
3907 - reguni - emit (if appropriate) a Unicode character
3910 S_reguni(pTHX_ RExC_state_t *pRExC_state, UV uv, char* s, STRLEN* lenp)
3912 *lenp = SIZE_ONLY ? UNISKIP(uv) : (uv_to_utf8((U8*)s, uv) - (U8*)s);
3916 - reginsert - insert an operator in front of already-emitted operand
3918 * Means relocating the operand.
3921 S_reginsert(pTHX_ RExC_state_t *pRExC_state, U8 op, regnode *opnd)
3923 register regnode *src;
3924 register regnode *dst;
3925 register regnode *place;
3926 register int offset = regarglen[(U8)op];
3928 /* (PL_regkind[(U8)op] == CURLY ? EXTRA_STEP_2ARGS : 0); */
3931 RExC_size += NODE_STEP_REGNODE + offset;
3936 RExC_emit += NODE_STEP_REGNODE + offset;
3939 StructCopy(--src, --dst, regnode);
3941 place = opnd; /* Op node, where operand used to be. */
3942 src = NEXTOPER(place);
3943 FILL_ADVANCE_NODE(place, op);
3944 Zero(src, offset, regnode);
3948 - regtail - set the next-pointer at the end of a node chain of p to val.
3951 S_regtail(pTHX_ RExC_state_t *pRExC_state, regnode *p, regnode *val)
3953 register regnode *scan;
3954 register regnode *temp;
3959 /* Find last node. */
3962 temp = regnext(scan);
3968 if (reg_off_by_arg[OP(scan)]) {
3969 ARG_SET(scan, val - scan);
3972 NEXT_OFF(scan) = val - scan;
3977 - regoptail - regtail on operand of first argument; nop if operandless
3980 S_regoptail(pTHX_ RExC_state_t *pRExC_state, regnode *p, regnode *val)
3982 /* "Operandless" and "op != BRANCH" are synonymous in practice. */
3983 if (p == NULL || SIZE_ONLY)
3985 if (PL_regkind[(U8)OP(p)] == BRANCH) {
3986 regtail(pRExC_state, NEXTOPER(p), val);
3988 else if ( PL_regkind[(U8)OP(p)] == BRANCHJ) {
3989 regtail(pRExC_state, NEXTOPER(NEXTOPER(p)), val);
3996 - regcurly - a little FSA that accepts {\d+,?\d*}
3999 S_regcurly(pTHX_ register char *s)
4018 S_dumpuntil(pTHX_ regnode *start, regnode *node, regnode *last, SV* sv, I32 l)
4021 register U8 op = EXACT; /* Arbitrary non-END op. */
4022 register regnode *next;
4024 while (op != END && (!last || node < last)) {
4025 /* While that wasn't END last time... */
4031 next = regnext(node);
4033 if (OP(node) == OPTIMIZED)
4036 PerlIO_printf(Perl_debug_log, "%4"IVdf":%*s%s", (IV)(node - start),
4037 (int)(2*l + 1), "", SvPVX(sv));
4038 if (next == NULL) /* Next ptr. */
4039 PerlIO_printf(Perl_debug_log, "(0)");
4041 PerlIO_printf(Perl_debug_log, "(%"IVdf")", (IV)(next - start));
4042 (void)PerlIO_putc(Perl_debug_log, '\n');
4044 if (PL_regkind[(U8)op] == BRANCHJ) {
4045 register regnode *nnode = (OP(next) == LONGJMP
4048 if (last && nnode > last)
4050 node = dumpuntil(start, NEXTOPER(NEXTOPER(node)), nnode, sv, l + 1);
4052 else if (PL_regkind[(U8)op] == BRANCH) {
4053 node = dumpuntil(start, NEXTOPER(node), next, sv, l + 1);
4055 else if ( op == CURLY) { /* `next' might be very big: optimizer */
4056 node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
4057 NEXTOPER(node) + EXTRA_STEP_2ARGS + 1, sv, l + 1);
4059 else if (PL_regkind[(U8)op] == CURLY && op != CURLYX) {
4060 node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
4063 else if ( op == PLUS || op == STAR) {
4064 node = dumpuntil(start, NEXTOPER(node), NEXTOPER(node) + 1, sv, l + 1);
4066 else if (op == ANYOF) {
4067 node = NEXTOPER(node);
4070 else if (PL_regkind[(U8)op] == EXACT) {
4071 /* Literal string, where present. */
4072 node += NODE_SZ_STR(node) - 1;
4073 node = NEXTOPER(node);
4076 node = NEXTOPER(node);
4077 node += regarglen[(U8)op];
4079 if (op == CURLYX || op == OPEN)
4081 else if (op == WHILEM)
4084 #endif /* DEBUGGING */
4089 - regdump - dump a regexp onto Perl_debug_log in vaguely comprehensible form
4092 Perl_regdump(pTHX_ regexp *r)
4095 SV *sv = sv_newmortal();
4097 (void)dumpuntil(r->program, r->program + 1, NULL, sv, 0);
4099 /* Header fields of interest. */
4100 if (r->anchored_substr)
4101 PerlIO_printf(Perl_debug_log,
4102 "anchored `%s%.*s%s'%s at %"IVdf" ",
4104 (int)(SvCUR(r->anchored_substr) - (SvTAIL(r->anchored_substr)!=0)),
4105 SvPVX(r->anchored_substr),
4107 SvTAIL(r->anchored_substr) ? "$" : "",
4108 (IV)r->anchored_offset);
4109 if (r->float_substr)
4110 PerlIO_printf(Perl_debug_log,
4111 "floating `%s%.*s%s'%s at %"IVdf"..%"UVuf" ",
4113 (int)(SvCUR(r->float_substr) - (SvTAIL(r->float_substr)!=0)),
4114 SvPVX(r->float_substr),
4116 SvTAIL(r->float_substr) ? "$" : "",
4117 (IV)r->float_min_offset, (UV)r->float_max_offset);
4118 if (r->check_substr)
4119 PerlIO_printf(Perl_debug_log,
4120 r->check_substr == r->float_substr
4121 ? "(checking floating" : "(checking anchored");
4122 if (r->reganch & ROPT_NOSCAN)
4123 PerlIO_printf(Perl_debug_log, " noscan");
4124 if (r->reganch & ROPT_CHECK_ALL)
4125 PerlIO_printf(Perl_debug_log, " isall");
4126 if (r->check_substr)
4127 PerlIO_printf(Perl_debug_log, ") ");
4129 if (r->regstclass) {
4130 regprop(sv, r->regstclass);
4131 PerlIO_printf(Perl_debug_log, "stclass `%s' ", SvPVX(sv));
4133 if (r->reganch & ROPT_ANCH) {
4134 PerlIO_printf(Perl_debug_log, "anchored");
4135 if (r->reganch & ROPT_ANCH_BOL)
4136 PerlIO_printf(Perl_debug_log, "(BOL)");
4137 if (r->reganch & ROPT_ANCH_MBOL)
4138 PerlIO_printf(Perl_debug_log, "(MBOL)");
4139 if (r->reganch & ROPT_ANCH_SBOL)
4140 PerlIO_printf(Perl_debug_log, "(SBOL)");
4141 if (r->reganch & ROPT_ANCH_GPOS)
4142 PerlIO_printf(Perl_debug_log, "(GPOS)");
4143 PerlIO_putc(Perl_debug_log, ' ');
4145 if (r->reganch & ROPT_GPOS_SEEN)
4146 PerlIO_printf(Perl_debug_log, "GPOS ");
4147 if (r->reganch & ROPT_SKIP)
4148 PerlIO_printf(Perl_debug_log, "plus ");
4149 if (r->reganch & ROPT_IMPLICIT)
4150 PerlIO_printf(Perl_debug_log, "implicit ");
4151 PerlIO_printf(Perl_debug_log, "minlen %ld ", (long) r->minlen);
4152 if (r->reganch & ROPT_EVAL_SEEN)
4153 PerlIO_printf(Perl_debug_log, "with eval ");
4154 PerlIO_printf(Perl_debug_log, "\n");
4155 #endif /* DEBUGGING */
4159 S_put_byte(pTHX_ SV *sv, int c)
4161 if (isCNTRL(c) || c == 127 || c == 255 || !isPRINT(c))
4162 Perl_sv_catpvf(aTHX_ sv, "\\%o", c);
4163 else if (c == '-' || c == ']' || c == '\\' || c == '^')
4164 Perl_sv_catpvf(aTHX_ sv, "\\%c", c);
4166 Perl_sv_catpvf(aTHX_ sv, "%c", c);
4170 - regprop - printable representation of opcode
4173 Perl_regprop(pTHX_ SV *sv, regnode *o)
4178 sv_setpvn(sv, "", 0);
4179 if (OP(o) >= reg_num) /* regnode.type is unsigned */
4180 /* It would be nice to FAIL() here, but this may be called from
4181 regexec.c, and it would be hard to supply pRExC_state. */
4182 Perl_croak(aTHX_ "Corrupted regexp opcode");
4183 sv_catpv(sv, (char*)reg_name[OP(o)]); /* Take off const! */
4185 k = PL_regkind[(U8)OP(o)];
4188 Perl_sv_catpvf(aTHX_ sv, " <%s%.*s%s>", PL_colors[0],
4189 STR_LEN(o), STRING(o), PL_colors[1]);
4190 else if (k == CURLY) {
4191 if (OP(o) == CURLYM || OP(o) == CURLYN || OP(o) == CURLYX)
4192 Perl_sv_catpvf(aTHX_ sv, "[%d]", o->flags); /* Parenth number */
4193 Perl_sv_catpvf(aTHX_ sv, " {%d,%d}", ARG1(o), ARG2(o));
4195 else if (k == WHILEM && o->flags) /* Ordinal/of */
4196 Perl_sv_catpvf(aTHX_ sv, "[%d/%d]", o->flags & 0xf, o->flags>>4);
4197 else if (k == REF || k == OPEN || k == CLOSE || k == GROUPP )
4198 Perl_sv_catpvf(aTHX_ sv, "%d", (int)ARG(o)); /* Parenth number */
4199 else if (k == LOGICAL)
4200 Perl_sv_catpvf(aTHX_ sv, "[%d]", o->flags); /* 2: embedded, otherwise 1 */
4201 else if (k == ANYOF) {
4202 int i, rangestart = -1;
4203 U8 flags = ANYOF_FLAGS(o);
4204 const char * const anyofs[] = { /* Should be syncronized with
4205 * ANYOF_ #xdefines in regcomp.h */
4238 if (flags & ANYOF_LOCALE)
4239 sv_catpv(sv, "{loc}");
4240 if (flags & ANYOF_FOLD)
4241 sv_catpv(sv, "{i}");
4242 Perl_sv_catpvf(aTHX_ sv, "[%s", PL_colors[0]);
4243 if (flags & ANYOF_INVERT)
4245 for (i = 0; i <= 256; i++) {
4246 if (i < 256 && ANYOF_BITMAP_TEST(o,i)) {
4247 if (rangestart == -1)
4249 } else if (rangestart != -1) {
4250 if (i <= rangestart + 3)
4251 for (; rangestart < i; rangestart++)
4252 put_byte(sv, rangestart);
4254 put_byte(sv, rangestart);
4256 put_byte(sv, i - 1);
4262 if (o->flags & ANYOF_CLASS)
4263 for (i = 0; i < sizeof(anyofs)/sizeof(char*); i++)
4264 if (ANYOF_CLASS_TEST(o,i))
4265 sv_catpv(sv, anyofs[i]);
4267 if (flags & ANYOF_UNICODE)
4268 sv_catpv(sv, "{unicode}");
4269 else if (flags & ANYOF_UNICODE_ALL)
4270 sv_catpv(sv, "{all-unicode}");
4274 SV *sw = regclass_swash(o, FALSE, &lv);
4279 U8 s[UTF8_MAXLEN+1];
4281 for (i = 0; i <= 256; i++) { /* just the first 256 */
4282 U8 *e = uv_to_utf8(s, i);
4284 if (i < 256 && swash_fetch(sw, s)) {
4285 if (rangestart == -1)
4287 } else if (rangestart != -1) {
4290 if (i <= rangestart + 3)
4291 for (; rangestart < i; rangestart++) {
4292 for(e = uv_to_utf8(s, rangestart), p = s; p < e; p++)
4296 for (e = uv_to_utf8(s, rangestart), p = s; p < e; p++)
4299 for (e = uv_to_utf8(s, i - 1), p = s; p < e; p++)
4306 sv_catpv(sv, "..."); /* et cetera */
4310 char *s = savepv(SvPVX(lv));
4313 while(*s && *s != '\n') s++;
4334 Perl_sv_catpvf(aTHX_ sv, "%s]", PL_colors[1]);
4336 else if (k == BRANCHJ && (OP(o) == UNLESSM || OP(o) == IFMATCH))
4337 Perl_sv_catpvf(aTHX_ sv, "[-%d]", o->flags);
4338 #endif /* DEBUGGING */
4342 Perl_re_intuit_string(pTHX_ regexp *prog)
4343 { /* Assume that RE_INTUIT is set */
4346 char *s = SvPV(prog->check_substr,n_a);
4348 if (!PL_colorset) reginitcolors();
4349 PerlIO_printf(Perl_debug_log,
4350 "%sUsing REx substr:%s `%s%.60s%s%s'\n",
4351 PL_colors[4],PL_colors[5],PL_colors[0],
4354 (strlen(s) > 60 ? "..." : ""));
4357 return prog->check_substr;
4361 Perl_pregfree(pTHX_ struct regexp *r)
4363 DEBUG_r(if (!PL_colorset) reginitcolors());
4365 if (!r || (--r->refcnt > 0))
4367 DEBUG_r(PerlIO_printf(Perl_debug_log,
4368 "%sFreeing REx:%s `%s%.60s%s%s'\n",
4369 PL_colors[4],PL_colors[5],PL_colors[0],
4372 (strlen(r->precomp) > 60 ? "..." : "")));
4375 Safefree(r->precomp);
4376 if (RX_MATCH_COPIED(r))
4377 Safefree(r->subbeg);
4379 if (r->anchored_substr)
4380 SvREFCNT_dec(r->anchored_substr);
4381 if (r->float_substr)
4382 SvREFCNT_dec(r->float_substr);
4383 Safefree(r->substrs);
4386 int n = r->data->count;
4387 AV* new_comppad = NULL;
4392 switch (r->data->what[n]) {
4394 SvREFCNT_dec((SV*)r->data->data[n]);
4397 Safefree(r->data->data[n]);
4400 new_comppad = (AV*)r->data->data[n];
4403 if (new_comppad == NULL)
4404 Perl_croak(aTHX_ "panic: pregfree comppad");
4405 old_comppad = PL_comppad;
4406 old_curpad = PL_curpad;
4407 /* Watch out for global destruction's random ordering. */
4408 if (SvTYPE(new_comppad) == SVt_PVAV) {
4409 PL_comppad = new_comppad;
4410 PL_curpad = AvARRAY(new_comppad);
4414 op_free((OP_4tree*)r->data->data[n]);
4415 PL_comppad = old_comppad;
4416 PL_curpad = old_curpad;
4417 SvREFCNT_dec((SV*)new_comppad);
4423 Perl_croak(aTHX_ "panic: regfree data code '%c'", r->data->what[n]);
4426 Safefree(r->data->what);
4429 Safefree(r->startp);
4435 - regnext - dig the "next" pointer out of a node
4437 * [Note, when REGALIGN is defined there are two places in regmatch()
4438 * that bypass this code for speed.]
4441 Perl_regnext(pTHX_ register regnode *p)
4443 register I32 offset;
4445 if (p == &PL_regdummy)
4448 offset = (reg_off_by_arg[OP(p)] ? ARG(p) : NEXT_OFF(p));
4456 S_re_croak2(pTHX_ const char* pat1,const char* pat2,...)
4459 STRLEN l1 = strlen(pat1);
4460 STRLEN l2 = strlen(pat2);
4469 Copy(pat1, buf, l1 , char);
4470 Copy(pat2, buf + l1, l2 , char);
4471 buf[l1 + l2] = '\n';
4472 buf[l1 + l2 + 1] = '\0';
4474 /* ANSI variant takes additional second argument */
4475 va_start(args, pat2);
4479 msv = vmess(buf, &args);
4481 message = SvPV(msv,l1);
4484 Copy(message, buf, l1 , char);
4485 buf[l1] = '\0'; /* Overwrite \n */
4486 Perl_croak(aTHX_ "%s", buf);
4489 /* XXX Here's a total kludge. But we need to re-enter for swash routines. */
4492 Perl_save_re_context(pTHX)
4495 SAVEPPTR(RExC_precomp); /* uncompiled string. */
4496 SAVEI32(RExC_npar); /* () count. */
4497 SAVEI32(RExC_size); /* Code size. */
4498 SAVEI16(RExC_flags16); /* are we folding, multilining? */
4499 SAVEVPTR(RExC_rx); /* from regcomp.c */
4500 SAVEI32(RExC_seen); /* from regcomp.c */
4501 SAVEI32(RExC_sawback); /* Did we see \1, ...? */
4502 SAVEI32(RExC_naughty); /* How bad is this pattern? */
4503 SAVEVPTR(RExC_emit); /* Code-emit pointer; ®dummy = don't */
4504 SAVEPPTR(RExC_end); /* End of input for compile */
4505 SAVEPPTR(RExC_parse); /* Input-scan pointer. */
4508 SAVEI32(PL_reg_flags); /* from regexec.c */
4510 SAVEPPTR(PL_reginput); /* String-input pointer. */
4511 SAVEPPTR(PL_regbol); /* Beginning of input, for ^ check. */
4512 SAVEPPTR(PL_regeol); /* End of input, for $ check. */
4513 SAVEVPTR(PL_regstartp); /* Pointer to startp array. */
4514 SAVEVPTR(PL_regendp); /* Ditto for endp. */
4515 SAVEVPTR(PL_reglastparen); /* Similarly for lastparen. */
4516 SAVEPPTR(PL_regtill); /* How far we are required to go. */
4517 SAVEI8(PL_regprev); /* char before regbol, \n if none */
4518 SAVEGENERICPV(PL_reg_start_tmp); /* from regexec.c */
4519 PL_reg_start_tmp = 0;
4520 SAVEI32(PL_reg_start_tmpl); /* from regexec.c */
4521 PL_reg_start_tmpl = 0;
4522 SAVEVPTR(PL_regdata);
4523 SAVEI32(PL_reg_eval_set); /* from regexec.c */
4524 SAVEI32(PL_regnarrate); /* from regexec.c */
4525 SAVEVPTR(PL_regprogram); /* from regexec.c */
4526 SAVEINT(PL_regindent); /* from regexec.c */
4527 SAVEVPTR(PL_regcc); /* from regexec.c */
4528 SAVEVPTR(PL_curcop);
4529 SAVEVPTR(PL_reg_call_cc); /* from regexec.c */
4530 SAVEVPTR(PL_reg_re); /* from regexec.c */
4531 SAVEPPTR(PL_reg_ganch); /* from regexec.c */
4532 SAVESPTR(PL_reg_sv); /* from regexec.c */
4533 SAVEVPTR(PL_reg_magic); /* from regexec.c */
4534 SAVEI32(PL_reg_oldpos); /* from regexec.c */
4535 SAVEVPTR(PL_reg_oldcurpm); /* from regexec.c */
4536 SAVEVPTR(PL_reg_curpm); /* from regexec.c */
4537 SAVEI32(PL_regnpar); /* () count. */
4539 SAVEPPTR(PL_reg_starttry); /* from regexec.c */
4550 clear_re(pTHXo_ void *r)
4552 ReREFCNT_dec((regexp *)r);