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 if (RExC_seen & REG_SEEN_SANY)
1934 r->reganch |= ROPT_SANY_SEEN;
1935 Newz(1002, r->startp, RExC_npar, I32);
1936 Newz(1002, r->endp, RExC_npar, I32);
1937 PL_regdata = r->data; /* for regprop() */
1938 DEBUG_r(regdump(r));
1943 - reg - regular expression, i.e. main body or parenthesized thing
1945 * Caller must absorb opening parenthesis.
1947 * Combining parenthesis handling with the base level of regular expression
1948 * is a trifle forced, but the need to tie the tails of the branches to what
1949 * follows makes it hard to avoid.
1952 S_reg(pTHX_ RExC_state_t *pRExC_state, I32 paren, I32 *flagp)
1953 /* paren: Parenthesized? 0=top, 1=(, inside: changed to letter. */
1955 register regnode *ret; /* Will be the head of the group. */
1956 register regnode *br;
1957 register regnode *lastbr;
1958 register regnode *ender = 0;
1959 register I32 parno = 0;
1960 I32 flags, oregflags = RExC_flags16, have_branch = 0, open = 0;
1961 char *oregcomp_parse = RExC_parse;
1964 *flagp = 0; /* Tentatively. */
1966 /* Make an OPEN node, if parenthesized. */
1968 if (*RExC_parse == '?') {
1969 U16 posflags = 0, negflags = 0;
1970 U16 *flagsp = &posflags;
1972 char *seqstart = RExC_parse;
1975 paren = *RExC_parse++;
1976 ret = NULL; /* For look-ahead/behind. */
1979 RExC_seen |= REG_SEEN_LOOKBEHIND;
1980 if (*RExC_parse == '!')
1982 if (*RExC_parse != '=' && *RExC_parse != '!')
1987 RExC_seen_zerolen++;
1993 vFAIL2("Sequence (?%c...) not implemented", (int)paren);
1996 while (*RExC_parse && *RExC_parse != ')')
1998 if (*RExC_parse != ')')
1999 FAIL("Sequence (?#... not terminated");
2000 nextchar(pRExC_state);
2005 vWARN(RExC_parse, "(?p{}) is deprecated - use (??{})");
2009 paren = *RExC_parse++;
2013 I32 count = 1, n = 0;
2015 char *s = RExC_parse;
2017 OP_4tree *sop, *rop;
2019 RExC_seen_zerolen++;
2020 RExC_seen |= REG_SEEN_EVAL;
2021 while (count && (c = *RExC_parse)) {
2022 if (c == '\\' && RExC_parse[1])
2030 if (*RExC_parse != ')')
2033 vFAIL("Sequence (?{...}) not terminated or not {}-balanced");
2038 if (RExC_parse - 1 - s)
2039 sv = newSVpvn(s, RExC_parse - 1 - s);
2041 sv = newSVpvn("", 0);
2044 Perl_save_re_context(aTHX);
2045 rop = sv_compile_2op(sv, &sop, "re", &av);
2048 n = add_data(pRExC_state, 3, "nop");
2049 RExC_rx->data->data[n] = (void*)rop;
2050 RExC_rx->data->data[n+1] = (void*)sop;
2051 RExC_rx->data->data[n+2] = (void*)av;
2054 else { /* First pass */
2055 if (PL_reginterp_cnt < ++RExC_seen_evals
2056 && PL_curcop != &PL_compiling)
2057 /* No compiled RE interpolated, has runtime
2058 components ===> unsafe. */
2059 FAIL("Eval-group not allowed at runtime, use re 'eval'");
2061 FAIL("Eval-group in insecure regular expression");
2064 nextchar(pRExC_state);
2066 ret = reg_node(pRExC_state, LOGICAL);
2069 regtail(pRExC_state, ret, reganode(pRExC_state, EVAL, n));
2072 return reganode(pRExC_state, EVAL, n);
2076 if (RExC_parse[0] == '?') {
2077 if (RExC_parse[1] == '=' || RExC_parse[1] == '!'
2078 || RExC_parse[1] == '<'
2079 || RExC_parse[1] == '{') { /* Lookahead or eval. */
2082 ret = reg_node(pRExC_state, LOGICAL);
2085 regtail(pRExC_state, ret, reg(pRExC_state, 1, &flag));
2089 else if (RExC_parse[0] >= '1' && RExC_parse[0] <= '9' ) {
2090 parno = atoi(RExC_parse++);
2092 while (isDIGIT(*RExC_parse))
2094 ret = reganode(pRExC_state, GROUPP, parno);
2095 if ((c = *nextchar(pRExC_state)) != ')')
2096 vFAIL("Switch condition not recognized");
2098 regtail(pRExC_state, ret, reganode(pRExC_state, IFTHEN, 0));
2099 br = regbranch(pRExC_state, &flags, 1);
2101 br = reganode(pRExC_state, LONGJMP, 0);
2103 regtail(pRExC_state, br, reganode(pRExC_state, LONGJMP, 0));
2104 c = *nextchar(pRExC_state);
2108 lastbr = reganode(pRExC_state, IFTHEN, 0); /* Fake one for optimizer. */
2109 regbranch(pRExC_state, &flags, 1);
2110 regtail(pRExC_state, ret, lastbr);
2113 c = *nextchar(pRExC_state);
2118 vFAIL("Switch (?(condition)... contains too many branches");
2119 ender = reg_node(pRExC_state, TAIL);
2120 regtail(pRExC_state, br, ender);
2122 regtail(pRExC_state, lastbr, ender);
2123 regtail(pRExC_state, NEXTOPER(NEXTOPER(lastbr)), ender);
2126 regtail(pRExC_state, ret, ender);
2130 vFAIL2("Unknown switch condition (?(%.2s", RExC_parse);
2134 RExC_parse--; /* for vFAIL to print correctly */
2135 vFAIL("Sequence (? incomplete");
2140 while (*RExC_parse && strchr("iogcmsx", *RExC_parse)) {
2141 if (*RExC_parse != 'o')
2142 pmflag(flagsp, *RExC_parse);
2145 if (*RExC_parse == '-') {
2150 RExC_flags16 |= posflags;
2151 RExC_flags16 &= ~negflags;
2152 if (*RExC_parse == ':') {
2158 if (*RExC_parse != ')') {
2160 vFAIL3("Sequence (%.*s...) not recognized", RExC_parse-seqstart, seqstart);
2162 nextchar(pRExC_state);
2170 ret = reganode(pRExC_state, OPEN, parno);
2177 /* Pick up the branches, linking them together. */
2178 br = regbranch(pRExC_state, &flags, 1);
2181 if (*RExC_parse == '|') {
2182 if (!SIZE_ONLY && RExC_extralen) {
2183 reginsert(pRExC_state, BRANCHJ, br);
2186 reginsert(pRExC_state, BRANCH, br);
2189 RExC_extralen += 1; /* For BRANCHJ-BRANCH. */
2191 else if (paren == ':') {
2192 *flagp |= flags&SIMPLE;
2194 if (open) { /* Starts with OPEN. */
2195 regtail(pRExC_state, ret, br); /* OPEN -> first. */
2197 else if (paren != '?') /* Not Conditional */
2201 *flagp |= flags&SPSTART;
2203 while (*RExC_parse == '|') {
2204 if (!SIZE_ONLY && RExC_extralen) {
2205 ender = reganode(pRExC_state, LONGJMP,0);
2206 regtail(pRExC_state, NEXTOPER(NEXTOPER(lastbr)), ender); /* Append to the previous. */
2209 RExC_extralen += 2; /* Account for LONGJMP. */
2210 nextchar(pRExC_state);
2211 br = regbranch(pRExC_state, &flags, 0);
2214 regtail(pRExC_state, lastbr, br); /* BRANCH -> BRANCH. */
2218 *flagp |= flags&SPSTART;
2221 if (have_branch || paren != ':') {
2222 /* Make a closing node, and hook it on the end. */
2225 ender = reg_node(pRExC_state, TAIL);
2228 ender = reganode(pRExC_state, CLOSE, parno);
2234 *flagp &= ~HASWIDTH;
2237 ender = reg_node(pRExC_state, SUCCEED);
2240 ender = reg_node(pRExC_state, END);
2243 regtail(pRExC_state, lastbr, ender);
2246 /* Hook the tails of the branches to the closing node. */
2247 for (br = ret; br != NULL; br = regnext(br)) {
2248 regoptail(pRExC_state, br, ender);
2255 static char parens[] = "=!<,>";
2257 if (paren && (p = strchr(parens, paren))) {
2258 int node = ((p - parens) % 2) ? UNLESSM : IFMATCH;
2259 int flag = (p - parens) > 1;
2262 node = SUSPEND, flag = 0;
2263 reginsert(pRExC_state, node,ret);
2265 regtail(pRExC_state, ret, reg_node(pRExC_state, TAIL));
2269 /* Check for proper termination. */
2271 RExC_flags16 = oregflags;
2272 if (RExC_parse >= RExC_end || *nextchar(pRExC_state) != ')') {
2273 RExC_parse = oregcomp_parse;
2274 vFAIL("Unmatched (");
2277 else if (!paren && RExC_parse < RExC_end) {
2278 if (*RExC_parse == ')') {
2280 vFAIL("Unmatched )");
2283 FAIL("Junk on end of regexp"); /* "Can't happen". */
2291 - regbranch - one alternative of an | operator
2293 * Implements the concatenation operator.
2296 S_regbranch(pTHX_ RExC_state_t *pRExC_state, I32 *flagp, I32 first)
2298 register regnode *ret;
2299 register regnode *chain = NULL;
2300 register regnode *latest;
2301 I32 flags = 0, c = 0;
2306 if (!SIZE_ONLY && RExC_extralen)
2307 ret = reganode(pRExC_state, BRANCHJ,0);
2309 ret = reg_node(pRExC_state, BRANCH);
2312 if (!first && SIZE_ONLY)
2313 RExC_extralen += 1; /* BRANCHJ */
2315 *flagp = WORST; /* Tentatively. */
2318 nextchar(pRExC_state);
2319 while (RExC_parse < RExC_end && *RExC_parse != '|' && *RExC_parse != ')') {
2321 latest = regpiece(pRExC_state, &flags);
2322 if (latest == NULL) {
2323 if (flags & TRYAGAIN)
2327 else if (ret == NULL)
2329 *flagp |= flags&HASWIDTH;
2330 if (chain == NULL) /* First piece. */
2331 *flagp |= flags&SPSTART;
2334 regtail(pRExC_state, chain, latest);
2339 if (chain == NULL) { /* Loop ran zero times. */
2340 chain = reg_node(pRExC_state, NOTHING);
2345 *flagp |= flags&SIMPLE;
2352 - regpiece - something followed by possible [*+?]
2354 * Note that the branching code sequences used for ? and the general cases
2355 * of * and + are somewhat optimized: they use the same NOTHING node as
2356 * both the endmarker for their branch list and the body of the last branch.
2357 * It might seem that this node could be dispensed with entirely, but the
2358 * endmarker role is not redundant.
2361 S_regpiece(pTHX_ RExC_state_t *pRExC_state, I32 *flagp)
2363 register regnode *ret;
2365 register char *next;
2367 char *origparse = RExC_parse;
2370 I32 max = REG_INFTY;
2372 ret = regatom(pRExC_state, &flags);
2374 if (flags & TRYAGAIN)
2381 if (op == '{' && regcurly(RExC_parse)) {
2382 next = RExC_parse + 1;
2384 while (isDIGIT(*next) || *next == ',') {
2393 if (*next == '}') { /* got one */
2397 min = atoi(RExC_parse);
2401 maxpos = RExC_parse;
2403 if (!max && *maxpos != '0')
2404 max = REG_INFTY; /* meaning "infinity" */
2405 else if (max >= REG_INFTY)
2406 vFAIL2("Quantifier in {,} bigger than %d", REG_INFTY - 1);
2408 nextchar(pRExC_state);
2411 if ((flags&SIMPLE)) {
2412 RExC_naughty += 2 + RExC_naughty / 2;
2413 reginsert(pRExC_state, CURLY, ret);
2416 regnode *w = reg_node(pRExC_state, WHILEM);
2419 regtail(pRExC_state, ret, w);
2420 if (!SIZE_ONLY && RExC_extralen) {
2421 reginsert(pRExC_state, LONGJMP,ret);
2422 reginsert(pRExC_state, NOTHING,ret);
2423 NEXT_OFF(ret) = 3; /* Go over LONGJMP. */
2425 reginsert(pRExC_state, CURLYX,ret);
2426 if (!SIZE_ONLY && RExC_extralen)
2427 NEXT_OFF(ret) = 3; /* Go over NOTHING to LONGJMP. */
2428 regtail(pRExC_state, ret, reg_node(pRExC_state, NOTHING));
2430 RExC_whilem_seen++, RExC_extralen += 3;
2431 RExC_naughty += 4 + RExC_naughty; /* compound interest */
2439 if (max && max < min)
2440 vFAIL("Can't do {n,m} with n > m");
2455 #if 0 /* Now runtime fix should be reliable. */
2457 /* if this is reinstated, don't forget to put this back into perldiag:
2459 =item Regexp *+ operand could be empty at {#} in regex m/%s/
2461 (F) The part of the regexp subject to either the * or + quantifier
2462 could match an empty string. The {#} shows in the regular
2463 expression about where the problem was discovered.
2467 if (!(flags&HASWIDTH) && op != '?')
2468 vFAIL("Regexp *+ operand could be empty");
2471 nextchar(pRExC_state);
2473 *flagp = (op != '+') ? (WORST|SPSTART|HASWIDTH) : (WORST|HASWIDTH);
2475 if (op == '*' && (flags&SIMPLE)) {
2476 reginsert(pRExC_state, STAR, ret);
2480 else if (op == '*') {
2484 else if (op == '+' && (flags&SIMPLE)) {
2485 reginsert(pRExC_state, PLUS, ret);
2489 else if (op == '+') {
2493 else if (op == '?') {
2498 if (ckWARN(WARN_REGEXP) && !SIZE_ONLY && !(flags&HASWIDTH) && max > REG_INFTY/3) {
2500 "%.*s matches null string many times",
2501 RExC_parse - origparse,
2505 if (*RExC_parse == '?') {
2506 nextchar(pRExC_state);
2507 reginsert(pRExC_state, MINMOD, ret);
2508 regtail(pRExC_state, ret, ret + NODE_STEP_REGNODE);
2510 if (ISMULT2(RExC_parse)) {
2512 vFAIL("Nested quantifiers");
2519 - regatom - the lowest level
2521 * Optimization: gobbles an entire sequence of ordinary characters so that
2522 * it can turn them into a single node, which is smaller to store and
2523 * faster to run. Backslashed characters are exceptions, each becoming a
2524 * separate node; the code is simpler that way and it's not worth fixing.
2526 * [Yes, it is worth fixing, some scripts can run twice the speed.] */
2528 S_regatom(pTHX_ RExC_state_t *pRExC_state, I32 *flagp)
2530 register regnode *ret = 0;
2533 *flagp = WORST; /* Tentatively. */
2536 switch (*RExC_parse) {
2538 RExC_seen_zerolen++;
2539 nextchar(pRExC_state);
2540 if (RExC_flags16 & PMf_MULTILINE)
2541 ret = reg_node(pRExC_state, MBOL);
2542 else if (RExC_flags16 & PMf_SINGLELINE)
2543 ret = reg_node(pRExC_state, SBOL);
2545 ret = reg_node(pRExC_state, BOL);
2548 nextchar(pRExC_state);
2550 RExC_seen_zerolen++;
2551 if (RExC_flags16 & PMf_MULTILINE)
2552 ret = reg_node(pRExC_state, MEOL);
2553 else if (RExC_flags16 & PMf_SINGLELINE)
2554 ret = reg_node(pRExC_state, SEOL);
2556 ret = reg_node(pRExC_state, EOL);
2559 nextchar(pRExC_state);
2560 if (RExC_flags16 & PMf_SINGLELINE)
2561 ret = reg_node(pRExC_state, SANY);
2563 ret = reg_node(pRExC_state, REG_ANY);
2564 *flagp |= HASWIDTH|SIMPLE;
2569 char *oregcomp_parse = ++RExC_parse;
2570 ret = regclass(pRExC_state);
2571 if (*RExC_parse != ']') {
2572 RExC_parse = oregcomp_parse;
2573 vFAIL("Unmatched [");
2575 nextchar(pRExC_state);
2576 *flagp |= HASWIDTH|SIMPLE;
2580 nextchar(pRExC_state);
2581 ret = reg(pRExC_state, 1, &flags);
2583 if (flags & TRYAGAIN) {
2584 if (RExC_parse == RExC_end) {
2585 /* Make parent create an empty node if needed. */
2593 *flagp |= flags&(HASWIDTH|SPSTART|SIMPLE);
2597 if (flags & TRYAGAIN) {
2601 vFAIL("Internal urp");
2602 /* Supposed to be caught earlier. */
2605 if (!regcurly(RExC_parse)) {
2614 vFAIL("Quantifier follows nothing");
2617 switch (*++RExC_parse) {
2619 RExC_seen_zerolen++;
2620 ret = reg_node(pRExC_state, SBOL);
2622 nextchar(pRExC_state);
2625 ret = reg_node(pRExC_state, GPOS);
2626 RExC_seen |= REG_SEEN_GPOS;
2628 nextchar(pRExC_state);
2631 ret = reg_node(pRExC_state, SEOL);
2633 nextchar(pRExC_state);
2636 ret = reg_node(pRExC_state, EOS);
2638 RExC_seen_zerolen++; /* Do not optimize RE away */
2639 nextchar(pRExC_state);
2642 ret = reg_node(pRExC_state, SANY);
2643 RExC_seen |= REG_SEEN_SANY;
2644 *flagp |= HASWIDTH|SIMPLE;
2645 nextchar(pRExC_state);
2648 ret = reg_node(pRExC_state, CLUMP);
2650 nextchar(pRExC_state);
2651 if (UTF && !PL_utf8_mark)
2652 is_utf8_mark((U8*)"~"); /* preload table */
2655 ret = reg_node(pRExC_state, LOC ? ALNUML : ALNUM);
2656 *flagp |= HASWIDTH|SIMPLE;
2657 nextchar(pRExC_state);
2658 if (UTF && !PL_utf8_alnum)
2659 is_utf8_alnum((U8*)"a"); /* preload table */
2662 ret = reg_node(pRExC_state, LOC ? NALNUML : NALNUM);
2663 *flagp |= HASWIDTH|SIMPLE;
2664 nextchar(pRExC_state);
2665 if (UTF && !PL_utf8_alnum)
2666 is_utf8_alnum((U8*)"a"); /* preload table */
2669 RExC_seen_zerolen++;
2670 RExC_seen |= REG_SEEN_LOOKBEHIND;
2671 ret = reg_node(pRExC_state, LOC ? BOUNDL : BOUND);
2673 nextchar(pRExC_state);
2674 if (UTF && !PL_utf8_alnum)
2675 is_utf8_alnum((U8*)"a"); /* preload table */
2678 RExC_seen_zerolen++;
2679 RExC_seen |= REG_SEEN_LOOKBEHIND;
2680 ret = reg_node(pRExC_state, LOC ? NBOUNDL : NBOUND);
2682 nextchar(pRExC_state);
2683 if (UTF && !PL_utf8_alnum)
2684 is_utf8_alnum((U8*)"a"); /* preload table */
2687 ret = reg_node(pRExC_state, LOC ? SPACEL : SPACE);
2688 *flagp |= HASWIDTH|SIMPLE;
2689 nextchar(pRExC_state);
2690 if (UTF && !PL_utf8_space)
2691 is_utf8_space((U8*)" "); /* preload table */
2694 ret = reg_node(pRExC_state, LOC ? NSPACEL : NSPACE);
2695 *flagp |= HASWIDTH|SIMPLE;
2696 nextchar(pRExC_state);
2697 if (UTF && !PL_utf8_space)
2698 is_utf8_space((U8*)" "); /* preload table */
2701 ret = reg_node(pRExC_state, DIGIT);
2702 *flagp |= HASWIDTH|SIMPLE;
2703 nextchar(pRExC_state);
2704 if (UTF && !PL_utf8_digit)
2705 is_utf8_digit((U8*)"1"); /* preload table */
2708 ret = reg_node(pRExC_state, NDIGIT);
2709 *flagp |= HASWIDTH|SIMPLE;
2710 nextchar(pRExC_state);
2711 if (UTF && !PL_utf8_digit)
2712 is_utf8_digit((U8*)"1"); /* preload table */
2716 { /* a lovely hack--pretend we saw [\pX] instead */
2717 char* oldregxend = RExC_end;
2719 if (RExC_parse[1] == '{') {
2720 RExC_end = strchr(RExC_parse, '}');
2723 RExC_end = oldregxend;
2724 vFAIL("Missing right brace on \\p{}");
2729 RExC_end = RExC_parse + 2;
2732 ret = regclass(pRExC_state);
2734 RExC_end = oldregxend;
2736 nextchar(pRExC_state);
2737 *flagp |= HASWIDTH|SIMPLE;
2750 case '1': case '2': case '3': case '4':
2751 case '5': case '6': case '7': case '8': case '9':
2753 I32 num = atoi(RExC_parse);
2755 if (num > 9 && num >= RExC_npar)
2758 while (isDIGIT(*RExC_parse))
2761 if (!SIZE_ONLY && num > RExC_rx->nparens)
2762 vFAIL("Reference to nonexistent group");
2764 ret = reganode(pRExC_state, FOLD
2765 ? (LOC ? REFFL : REFF)
2769 nextchar(pRExC_state);
2774 if (RExC_parse >= RExC_end)
2775 FAIL("Trailing \\");
2778 /* Do not generate `unrecognized' warnings here, we fall
2779 back into the quick-grab loop below */
2785 if (RExC_flags16 & PMf_EXTENDED) {
2786 while (RExC_parse < RExC_end && *RExC_parse != '\n') RExC_parse++;
2787 if (RExC_parse < RExC_end)
2793 register STRLEN len;
2802 ret = reg_node(pRExC_state, FOLD
2803 ? (LOC ? EXACTFL : EXACTF)
2806 for (len = 0, p = RExC_parse - 1;
2807 len < 127 && p < RExC_end;
2812 if (RExC_flags16 & PMf_EXTENDED)
2813 p = regwhite(p, RExC_end);
2875 char* e = strchr(p, '}');
2879 vFAIL("Missing right brace on \\x{}");
2882 numlen = 1; /* allow underscores */
2883 ender = (UV)scan_hex(p + 1, e - p - 1, &numlen);
2884 /* numlen is generous */
2885 if (numlen + len >= 127) {
2893 numlen = 0; /* disallow underscores */
2894 ender = (UV)scan_hex(p, 2, &numlen);
2900 ender = UCHARAT(p++);
2901 ender = toCTRL(ender);
2903 case '0': case '1': case '2': case '3':case '4':
2904 case '5': case '6': case '7': case '8':case '9':
2906 (isDIGIT(p[1]) && atoi(p) >= RExC_npar) ) {
2907 numlen = 0; /* disallow underscores */
2908 ender = (UV)scan_oct(p, 3, &numlen);
2918 FAIL("Trailing \\");
2921 if (!SIZE_ONLY && ckWARN(WARN_REGEXP) && isALPHA(*p))
2922 vWARN2(p +1, "Unrecognized escape \\%c passed through", *p);
2923 goto normal_default;
2928 if (UTF8_IS_START(*p) && UTF) {
2929 ender = utf8_to_uv((U8*)p, RExC_end - p,
2937 if (RExC_flags16 & PMf_EXTENDED)
2938 p = regwhite(p, RExC_end);
2941 ender = toLOWER_LC_uni(ender);
2943 ender = toLOWER_uni(ender);
2945 if (ISMULT2(p)) { /* Back off on ?+*. */
2948 /* ender is a Unicode value so it can be > 0xff --
2949 * in other words, do not use UTF8_IS_CONTINUED(). */
2950 else if (ender >= 0x80 && UTF) {
2951 reguni(pRExC_state, ender, s, &numlen);
2961 /* ender is a Unicode value so it can be > 0xff --
2962 * in other words, do not use UTF8_IS_CONTINUED(). */
2963 if (ender >= 0x80 && UTF) {
2964 reguni(pRExC_state, ender, s, &numlen);
2973 nextchar(pRExC_state);
2975 /* len is STRLEN which is unsigned, need to copy to signed */
2978 vFAIL("Internal disaster");
2987 RExC_size += STR_SZ(len);
2989 RExC_emit += STR_SZ(len);
2998 S_regwhite(pTHX_ char *p, char *e)
3003 else if (*p == '#') {
3006 } while (p < e && *p != '\n');
3014 /* Parse POSIX character classes: [[:foo:]], [[=foo=]], [[.foo.]].
3015 Character classes ([:foo:]) can also be negated ([:^foo:]).
3016 Returns a named class id (ANYOF_XXX) if successful, -1 otherwise.
3017 Equivalence classes ([=foo=]) and composites ([.foo.]) are parsed,
3018 but trigger warnings because they are currently unimplemented. */
3020 S_regpposixcc(pTHX_ RExC_state_t *pRExC_state, I32 value)
3023 I32 namedclass = OOB_NAMEDCLASS;
3025 if (value == '[' && RExC_parse + 1 < RExC_end &&
3026 /* I smell either [: or [= or [. -- POSIX has been here, right? */
3027 (*RExC_parse == ':' ||
3028 *RExC_parse == '=' ||
3029 *RExC_parse == '.')) {
3030 char c = *RExC_parse;
3031 char* s = RExC_parse++;
3033 while (RExC_parse < RExC_end && *RExC_parse != c)
3035 if (RExC_parse == RExC_end)
3036 /* Grandfather lone [:, [=, [. */
3039 char* t = RExC_parse++; /* skip over the c */
3041 if (*RExC_parse == ']') {
3042 RExC_parse++; /* skip over the ending ] */
3045 I32 complement = *posixcc == '^' ? *posixcc++ : 0;
3046 I32 skip = 5; /* the most common skip */
3050 if (strnEQ(posixcc, "alnum", 5))
3052 complement ? ANYOF_NALNUMC : ANYOF_ALNUMC;
3053 else if (strnEQ(posixcc, "alpha", 5))
3055 complement ? ANYOF_NALPHA : ANYOF_ALPHA;
3056 else if (strnEQ(posixcc, "ascii", 5))
3058 complement ? ANYOF_NASCII : ANYOF_ASCII;
3061 if (strnEQ(posixcc, "blank", 5))
3063 complement ? ANYOF_NBLANK : ANYOF_BLANK;
3066 if (strnEQ(posixcc, "cntrl", 5))
3068 complement ? ANYOF_NCNTRL : ANYOF_CNTRL;
3071 if (strnEQ(posixcc, "digit", 5))
3073 complement ? ANYOF_NDIGIT : ANYOF_DIGIT;
3076 if (strnEQ(posixcc, "graph", 5))
3078 complement ? ANYOF_NGRAPH : ANYOF_GRAPH;
3081 if (strnEQ(posixcc, "lower", 5))
3083 complement ? ANYOF_NLOWER : ANYOF_LOWER;
3086 if (strnEQ(posixcc, "print", 5))
3088 complement ? ANYOF_NPRINT : ANYOF_PRINT;
3089 else if (strnEQ(posixcc, "punct", 5))
3091 complement ? ANYOF_NPUNCT : ANYOF_PUNCT;
3094 if (strnEQ(posixcc, "space", 5))
3096 complement ? ANYOF_NPSXSPC : ANYOF_PSXSPC;
3099 if (strnEQ(posixcc, "upper", 5))
3101 complement ? ANYOF_NUPPER : ANYOF_UPPER;
3103 case 'w': /* this is not POSIX, this is the Perl \w */
3104 if (strnEQ(posixcc, "word", 4)) {
3106 complement ? ANYOF_NALNUM : ANYOF_ALNUM;
3111 if (strnEQ(posixcc, "xdigit", 6)) {
3113 complement ? ANYOF_NXDIGIT : ANYOF_XDIGIT;
3118 if (namedclass == OOB_NAMEDCLASS ||
3119 posixcc[skip] != ':' ||
3120 posixcc[skip+1] != ']')
3122 Simple_vFAIL3("POSIX class [:%.*s:] unknown",
3125 } else if (!SIZE_ONLY) {
3126 /* [[=foo=]] and [[.foo.]] are still future. */
3128 /* adjust RExC_parse so the warning shows after
3130 while (*RExC_parse && *RExC_parse != ']')
3132 Simple_vFAIL3("POSIX syntax [%c %c] is reserved for future extensions", c, c);
3135 /* Maternal grandfather:
3136 * "[:" ending in ":" but not in ":]" */
3146 S_checkposixcc(pTHX_ RExC_state_t *pRExC_state)
3148 if (!SIZE_ONLY && ckWARN(WARN_REGEXP) &&
3149 (*RExC_parse == ':' ||
3150 *RExC_parse == '=' ||
3151 *RExC_parse == '.')) {
3152 char *s = RExC_parse;
3155 while(*s && isALNUM(*s))
3157 if (*s && c == *s && s[1] == ']') {
3158 vWARN3(s+2, "POSIX syntax [%c %c] belongs inside character classes", c, c);
3160 /* [[=foo=]] and [[.foo.]] are still future. */
3161 if (c == '=' || c == '.')
3163 /* adjust RExC_parse so the error shows after
3165 while (*RExC_parse && *RExC_parse++ != ']')
3167 Simple_vFAIL3("POSIX syntax [%c %c] is reserved for future extensions", c, c);
3174 S_regclass(pTHX_ RExC_state_t *pRExC_state)
3177 register IV lastvalue = OOB_UNICODE;
3178 register IV range = 0;
3179 register regnode *ret;
3183 bool need_class = 0;
3187 bool dont_optimize_invert = FALSE;
3189 ret = reganode(pRExC_state, ANYOF, 0);
3192 ANYOF_FLAGS(ret) = 0;
3194 if (*RExC_parse == '^') { /* Complement of range. */
3198 ANYOF_FLAGS(ret) |= ANYOF_INVERT;
3202 RExC_size += ANYOF_SKIP;
3204 RExC_emit += ANYOF_SKIP;
3206 ANYOF_FLAGS(ret) |= ANYOF_FOLD;
3208 ANYOF_FLAGS(ret) |= ANYOF_LOCALE;
3209 ANYOF_BITMAP_ZERO(ret);
3210 listsv = newSVpvn("# comment\n", 10);
3213 if (!SIZE_ONLY && ckWARN(WARN_REGEXP))
3214 checkposixcc(pRExC_state);
3216 if (*RExC_parse == ']' || *RExC_parse == '-')
3217 goto charclassloop; /* allow 1st char to be ] or - */
3219 while (RExC_parse < RExC_end && *RExC_parse != ']') {
3223 namedclass = OOB_NAMEDCLASS; /* initialize as illegal */
3226 rangebegin = RExC_parse;
3228 value = utf8_to_uv((U8*)RExC_parse,
3229 RExC_end - RExC_parse,
3231 RExC_parse += numlen;
3234 value = UCHARAT(RExC_parse++);
3236 namedclass = regpposixcc(pRExC_state, value);
3237 else if (value == '\\') {
3239 value = utf8_to_uv((U8*)RExC_parse,
3240 RExC_end - RExC_parse,
3242 RExC_parse += numlen;
3245 value = UCHARAT(RExC_parse++);
3246 /* Some compilers cannot handle switching on 64-bit integer
3247 * values, therefore value cannot be an UV. Yes, this will
3248 * be a problem later if we want switch on Unicode.
3249 * A similar issue a little bit later when switching on
3250 * namedclass. --jhi */
3251 switch ((I32)value) {
3252 case 'w': namedclass = ANYOF_ALNUM; break;
3253 case 'W': namedclass = ANYOF_NALNUM; break;
3254 case 's': namedclass = ANYOF_SPACE; break;
3255 case 'S': namedclass = ANYOF_NSPACE; break;
3256 case 'd': namedclass = ANYOF_DIGIT; break;
3257 case 'D': namedclass = ANYOF_NDIGIT; break;
3260 if (*RExC_parse == '{') {
3261 e = strchr(RExC_parse++, '}');
3263 vFAIL("Missing right brace on \\p{}");
3272 Perl_sv_catpvf(aTHX_ listsv,
3273 "+utf8::%.*s\n", (int)n, RExC_parse);
3275 Perl_sv_catpvf(aTHX_ listsv,
3276 "!utf8::%.*s\n", (int)n, RExC_parse);
3279 ANYOF_FLAGS(ret) |= ANYOF_UNICODE;
3281 case 'n': value = '\n'; break;
3282 case 'r': value = '\r'; break;
3283 case 't': value = '\t'; break;
3284 case 'f': value = '\f'; break;
3285 case 'b': value = '\b'; break;
3287 case 'e': value = '\033'; break;
3288 case 'a': value = '\007'; break;
3290 case 'e': value = '\047'; break;
3291 case 'a': value = '\057'; break;
3294 if (*RExC_parse == '{') {
3295 e = strchr(RExC_parse++, '}');
3297 vFAIL("Missing right brace on \\x{}");
3298 numlen = 1; /* allow underscores */
3299 value = (UV)scan_hex(RExC_parse,
3305 numlen = 0; /* disallow underscores */
3306 value = (UV)scan_hex(RExC_parse, 2, &numlen);
3307 RExC_parse += numlen;
3311 value = UCHARAT(RExC_parse++);
3312 value = toCTRL(value);
3314 case '0': case '1': case '2': case '3': case '4':
3315 case '5': case '6': case '7': case '8': case '9':
3316 numlen = 0; /* disallow underscores */
3317 value = (UV)scan_oct(--RExC_parse, 3, &numlen);
3318 RExC_parse += numlen;
3321 if (!SIZE_ONLY && ckWARN(WARN_REGEXP) && isALPHA(value))
3323 "Unrecognized escape \\%c in character class passed through",
3327 } /* end of \blah */
3329 if (namedclass > OOB_NAMEDCLASS) { /* this is a named class \blah */
3331 if (!SIZE_ONLY && !need_class)
3332 ANYOF_CLASS_ZERO(ret);
3336 /* a bad range like a-\d, a-[:digit:] ? */
3339 if (ckWARN(WARN_REGEXP))
3341 "False [] range \"%*.*s\"",
3342 RExC_parse - rangebegin,
3343 RExC_parse - rangebegin,
3345 if (lastvalue < 256) {
3346 ANYOF_BITMAP_SET(ret, lastvalue);
3347 ANYOF_BITMAP_SET(ret, '-');
3350 ANYOF_FLAGS(ret) |= ANYOF_UNICODE;
3351 Perl_sv_catpvf(aTHX_ listsv,
3352 /* 0x002D is Unicode for '-' */
3353 "%04"UVxf"\n002D\n", (UV)lastvalue);
3357 range = 0; /* this was not a true range */
3361 /* Possible truncation here but in some 64-bit environments
3362 * the compiler gets heartburn about switch on 64-bit values.
3363 * A similar issue a little earlier when switching on value.
3365 switch ((I32)namedclass) {
3368 ANYOF_CLASS_SET(ret, ANYOF_ALNUM);
3370 for (value = 0; value < 256; value++)
3372 ANYOF_BITMAP_SET(ret, value);
3374 dont_optimize_invert = TRUE;
3375 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsWord\n");
3379 ANYOF_CLASS_SET(ret, ANYOF_NALNUM);
3381 for (value = 0; value < 256; value++)
3382 if (!isALNUM(value))
3383 ANYOF_BITMAP_SET(ret, value);
3385 dont_optimize_invert = TRUE;
3386 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsWord\n");
3390 ANYOF_CLASS_SET(ret, ANYOF_ALNUMC);
3392 for (value = 0; value < 256; value++)
3393 if (isALNUMC(value))
3394 ANYOF_BITMAP_SET(ret, value);
3396 dont_optimize_invert = TRUE;
3397 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsAlnum\n");
3401 ANYOF_CLASS_SET(ret, ANYOF_NALNUMC);
3403 for (value = 0; value < 256; value++)
3404 if (!isALNUMC(value))
3405 ANYOF_BITMAP_SET(ret, value);
3407 dont_optimize_invert = TRUE;
3408 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsAlnum\n");
3412 ANYOF_CLASS_SET(ret, ANYOF_ALPHA);
3414 for (value = 0; value < 256; value++)
3416 ANYOF_BITMAP_SET(ret, value);
3418 dont_optimize_invert = TRUE;
3419 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsAlpha\n");
3423 ANYOF_CLASS_SET(ret, ANYOF_NALPHA);
3425 for (value = 0; value < 256; value++)
3426 if (!isALPHA(value))
3427 ANYOF_BITMAP_SET(ret, value);
3429 dont_optimize_invert = TRUE;
3430 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsAlpha\n");
3434 ANYOF_CLASS_SET(ret, ANYOF_ASCII);
3437 for (value = 0; value < 128; value++)
3438 ANYOF_BITMAP_SET(ret, value);
3440 for (value = 0; value < 256; value++)
3442 ANYOF_BITMAP_SET(ret, value);
3445 dont_optimize_invert = TRUE;
3446 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsASCII\n");
3450 ANYOF_CLASS_SET(ret, ANYOF_NASCII);
3453 for (value = 128; value < 256; value++)
3454 ANYOF_BITMAP_SET(ret, value);
3456 for (value = 0; value < 256; value++)
3457 if (!isASCII(value))
3458 ANYOF_BITMAP_SET(ret, value);
3461 dont_optimize_invert = TRUE;
3462 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsASCII\n");
3466 ANYOF_CLASS_SET(ret, ANYOF_BLANK);
3468 for (value = 0; value < 256; value++)
3470 ANYOF_BITMAP_SET(ret, value);
3472 dont_optimize_invert = TRUE;
3473 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsBlank\n");
3477 ANYOF_CLASS_SET(ret, ANYOF_NBLANK);
3479 for (value = 0; value < 256; value++)
3480 if (!isBLANK(value))
3481 ANYOF_BITMAP_SET(ret, value);
3483 dont_optimize_invert = TRUE;
3484 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsBlank\n");
3488 ANYOF_CLASS_SET(ret, ANYOF_CNTRL);
3490 for (value = 0; value < 256; value++)
3492 ANYOF_BITMAP_SET(ret, value);
3494 dont_optimize_invert = TRUE;
3495 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsCntrl\n");
3499 ANYOF_CLASS_SET(ret, ANYOF_NCNTRL);
3501 for (value = 0; value < 256; value++)
3502 if (!isCNTRL(value))
3503 ANYOF_BITMAP_SET(ret, value);
3505 dont_optimize_invert = TRUE;
3506 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsCntrl\n");
3510 ANYOF_CLASS_SET(ret, ANYOF_DIGIT);
3512 /* consecutive digits assumed */
3513 for (value = '0'; value <= '9'; value++)
3514 ANYOF_BITMAP_SET(ret, value);
3516 dont_optimize_invert = TRUE;
3517 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsDigit\n");
3521 ANYOF_CLASS_SET(ret, ANYOF_NDIGIT);
3523 /* consecutive digits assumed */
3524 for (value = 0; value < '0'; value++)
3525 ANYOF_BITMAP_SET(ret, value);
3526 for (value = '9' + 1; value < 256; value++)
3527 ANYOF_BITMAP_SET(ret, value);
3529 dont_optimize_invert = TRUE;
3530 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsDigit\n");
3534 ANYOF_CLASS_SET(ret, ANYOF_GRAPH);
3536 for (value = 0; value < 256; value++)
3538 ANYOF_BITMAP_SET(ret, value);
3540 dont_optimize_invert = TRUE;
3541 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsGraph\n");
3545 ANYOF_CLASS_SET(ret, ANYOF_NGRAPH);
3547 for (value = 0; value < 256; value++)
3548 if (!isGRAPH(value))
3549 ANYOF_BITMAP_SET(ret, value);
3551 dont_optimize_invert = TRUE;
3552 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsGraph\n");
3556 ANYOF_CLASS_SET(ret, ANYOF_LOWER);
3558 for (value = 0; value < 256; value++)
3560 ANYOF_BITMAP_SET(ret, value);
3562 dont_optimize_invert = TRUE;
3563 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsLower\n");
3567 ANYOF_CLASS_SET(ret, ANYOF_NLOWER);
3569 for (value = 0; value < 256; value++)
3570 if (!isLOWER(value))
3571 ANYOF_BITMAP_SET(ret, value);
3573 dont_optimize_invert = TRUE;
3574 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsLower\n");
3578 ANYOF_CLASS_SET(ret, ANYOF_PRINT);
3580 for (value = 0; value < 256; value++)
3582 ANYOF_BITMAP_SET(ret, value);
3584 dont_optimize_invert = TRUE;
3585 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsPrint\n");
3589 ANYOF_CLASS_SET(ret, ANYOF_NPRINT);
3591 for (value = 0; value < 256; value++)
3592 if (!isPRINT(value))
3593 ANYOF_BITMAP_SET(ret, value);
3595 dont_optimize_invert = TRUE;
3596 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsPrint\n");
3600 ANYOF_CLASS_SET(ret, ANYOF_PSXSPC);
3602 for (value = 0; value < 256; value++)
3603 if (isPSXSPC(value))
3604 ANYOF_BITMAP_SET(ret, value);
3606 dont_optimize_invert = TRUE;
3607 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsSpace\n");
3611 ANYOF_CLASS_SET(ret, ANYOF_NPSXSPC);
3613 for (value = 0; value < 256; value++)
3614 if (!isPSXSPC(value))
3615 ANYOF_BITMAP_SET(ret, value);
3617 dont_optimize_invert = TRUE;
3618 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsSpace\n");
3622 ANYOF_CLASS_SET(ret, ANYOF_PUNCT);
3624 for (value = 0; value < 256; value++)
3626 ANYOF_BITMAP_SET(ret, value);
3628 dont_optimize_invert = TRUE;
3629 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsPunct\n");
3633 ANYOF_CLASS_SET(ret, ANYOF_NPUNCT);
3635 for (value = 0; value < 256; value++)
3636 if (!isPUNCT(value))
3637 ANYOF_BITMAP_SET(ret, value);
3639 dont_optimize_invert = TRUE;
3640 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsPunct\n");
3644 ANYOF_CLASS_SET(ret, ANYOF_SPACE);
3646 for (value = 0; value < 256; value++)
3648 ANYOF_BITMAP_SET(ret, value);
3650 dont_optimize_invert = TRUE;
3651 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsSpacePerl\n");
3655 ANYOF_CLASS_SET(ret, ANYOF_NSPACE);
3657 for (value = 0; value < 256; value++)
3658 if (!isSPACE(value))
3659 ANYOF_BITMAP_SET(ret, value);
3661 dont_optimize_invert = TRUE;
3662 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsSpacePerl\n");
3666 ANYOF_CLASS_SET(ret, ANYOF_UPPER);
3668 for (value = 0; value < 256; value++)
3670 ANYOF_BITMAP_SET(ret, value);
3672 dont_optimize_invert = TRUE;
3673 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsUpper\n");
3677 ANYOF_CLASS_SET(ret, ANYOF_NUPPER);
3679 for (value = 0; value < 256; value++)
3680 if (!isUPPER(value))
3681 ANYOF_BITMAP_SET(ret, value);
3683 dont_optimize_invert = TRUE;
3684 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsUpper\n");
3688 ANYOF_CLASS_SET(ret, ANYOF_XDIGIT);
3690 for (value = 0; value < 256; value++)
3691 if (isXDIGIT(value))
3692 ANYOF_BITMAP_SET(ret, value);
3694 dont_optimize_invert = TRUE;
3695 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsXDigit\n");
3699 ANYOF_CLASS_SET(ret, ANYOF_NXDIGIT);
3701 for (value = 0; value < 256; value++)
3702 if (!isXDIGIT(value))
3703 ANYOF_BITMAP_SET(ret, value);
3705 dont_optimize_invert = TRUE;
3706 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsXDigit\n");
3709 vFAIL("Invalid [::] class");
3713 ANYOF_FLAGS(ret) |= ANYOF_CLASS;
3716 } /* end of namedclass \blah */
3719 if (lastvalue > value) /* b-a */ {
3720 Simple_vFAIL4("Invalid [] range \"%*.*s\"",
3721 RExC_parse - rangebegin,
3722 RExC_parse - rangebegin,
3725 range = 0; /* not a true range */
3728 lastvalue = value; /* save the beginning of the range */
3729 if (*RExC_parse == '-' && RExC_parse+1 < RExC_end &&
3730 RExC_parse[1] != ']') {
3733 /* a bad range like \w-, [:word:]- ? */
3734 if (namedclass > OOB_NAMEDCLASS) {
3735 if (ckWARN(WARN_REGEXP))
3737 "False [] range \"%*.*s\"",
3738 RExC_parse - rangebegin,
3739 RExC_parse - rangebegin,
3742 ANYOF_BITMAP_SET(ret, '-');
3744 range = 1; /* yeah, it's a range! */
3745 continue; /* but do it the next time */
3749 /* now is the next time */
3751 if (lastvalue < 256 && value < 256) {
3752 #ifndef ASCIIish /* EBCDIC, for example. */
3753 if ((isLOWER(lastvalue) && isLOWER(value)) ||
3754 (isUPPER(lastvalue) && isUPPER(value)))
3757 if (isLOWER(lastvalue)) {
3758 for (i = lastvalue; i <= value; i++)
3760 ANYOF_BITMAP_SET(ret, i);
3762 for (i = lastvalue; i <= value; i++)
3764 ANYOF_BITMAP_SET(ret, i);
3769 for ( ; lastvalue <= value; lastvalue++)
3770 ANYOF_BITMAP_SET(ret, lastvalue);
3772 ANYOF_FLAGS(ret) |= ANYOF_UNICODE;
3773 if (lastvalue < value)
3774 Perl_sv_catpvf(aTHX_ listsv, "%04"UVxf"\t%04"UVxf"\n",
3775 (UV)lastvalue, (UV)value);
3777 Perl_sv_catpvf(aTHX_ listsv, "%04"UVxf"\n",
3782 range = 0; /* this range (if it was one) is done now */
3787 RExC_size += ANYOF_CLASS_ADD_SKIP;
3789 RExC_emit += ANYOF_CLASS_ADD_SKIP;
3792 /* optimize case-insensitive simple patterns (e.g. /[a-z]/i) */
3795 /* If the only flag is folding (plus possibly inversion). */
3796 (ANYOF_FLAGS_ALL ^ ANYOF_INVERT) == ANYOF_FOLD)) {
3797 for (value = 0; value < 256; ++value) {
3798 if (ANYOF_BITMAP_TEST(ret, value)) {
3799 IV fold = PL_fold[value];
3802 ANYOF_BITMAP_SET(ret, fold);
3805 ANYOF_FLAGS(ret) &= ~ANYOF_FOLD;
3808 /* optimize inverted simple patterns (e.g. [^a-z]) */
3809 if (!SIZE_ONLY && !dont_optimize_invert &&
3810 /* If the only flag is inversion. */
3811 (ANYOF_FLAGS(ret) & ANYOF_FLAGS_ALL) == ANYOF_INVERT) {
3812 for (value = 0; value < ANYOF_BITMAP_SIZE; ++value)
3813 ANYOF_BITMAP(ret)[value] ^= ANYOF_FLAGS_ALL;
3814 ANYOF_FLAGS(ret) = ANYOF_UNICODE_ALL;
3821 av_store(av, 0, listsv);
3822 av_store(av, 1, NULL);
3823 rv = newRV_noinc((SV*)av);
3824 n = add_data(pRExC_state, 1, "s");
3825 RExC_rx->data->data[n] = (void*)rv;
3833 S_nextchar(pTHX_ RExC_state_t *pRExC_state)
3835 char* retval = RExC_parse++;
3838 if (*RExC_parse == '(' && RExC_parse[1] == '?' &&
3839 RExC_parse[2] == '#') {
3840 while (*RExC_parse && *RExC_parse != ')')
3845 if (RExC_flags16 & PMf_EXTENDED) {
3846 if (isSPACE(*RExC_parse)) {
3850 else if (*RExC_parse == '#') {
3851 while (*RExC_parse && *RExC_parse != '\n')
3862 - reg_node - emit a node
3864 STATIC regnode * /* Location. */
3865 S_reg_node(pTHX_ RExC_state_t *pRExC_state, U8 op)
3867 register regnode *ret;
3868 register regnode *ptr;
3872 SIZE_ALIGN(RExC_size);
3877 NODE_ALIGN_FILL(ret);
3879 FILL_ADVANCE_NODE(ptr, op);
3886 - reganode - emit a node with an argument
3888 STATIC regnode * /* Location. */
3889 S_reganode(pTHX_ RExC_state_t *pRExC_state, U8 op, U32 arg)
3891 register regnode *ret;
3892 register regnode *ptr;
3896 SIZE_ALIGN(RExC_size);
3901 NODE_ALIGN_FILL(ret);
3903 FILL_ADVANCE_NODE_ARG(ptr, op, arg);
3910 - reguni - emit (if appropriate) a Unicode character
3913 S_reguni(pTHX_ RExC_state_t *pRExC_state, UV uv, char* s, STRLEN* lenp)
3915 *lenp = SIZE_ONLY ? UNISKIP(uv) : (uv_to_utf8((U8*)s, uv) - (U8*)s);
3919 - reginsert - insert an operator in front of already-emitted operand
3921 * Means relocating the operand.
3924 S_reginsert(pTHX_ RExC_state_t *pRExC_state, U8 op, regnode *opnd)
3926 register regnode *src;
3927 register regnode *dst;
3928 register regnode *place;
3929 register int offset = regarglen[(U8)op];
3931 /* (PL_regkind[(U8)op] == CURLY ? EXTRA_STEP_2ARGS : 0); */
3934 RExC_size += NODE_STEP_REGNODE + offset;
3939 RExC_emit += NODE_STEP_REGNODE + offset;
3942 StructCopy(--src, --dst, regnode);
3944 place = opnd; /* Op node, where operand used to be. */
3945 src = NEXTOPER(place);
3946 FILL_ADVANCE_NODE(place, op);
3947 Zero(src, offset, regnode);
3951 - regtail - set the next-pointer at the end of a node chain of p to val.
3954 S_regtail(pTHX_ RExC_state_t *pRExC_state, regnode *p, regnode *val)
3956 register regnode *scan;
3957 register regnode *temp;
3962 /* Find last node. */
3965 temp = regnext(scan);
3971 if (reg_off_by_arg[OP(scan)]) {
3972 ARG_SET(scan, val - scan);
3975 NEXT_OFF(scan) = val - scan;
3980 - regoptail - regtail on operand of first argument; nop if operandless
3983 S_regoptail(pTHX_ RExC_state_t *pRExC_state, regnode *p, regnode *val)
3985 /* "Operandless" and "op != BRANCH" are synonymous in practice. */
3986 if (p == NULL || SIZE_ONLY)
3988 if (PL_regkind[(U8)OP(p)] == BRANCH) {
3989 regtail(pRExC_state, NEXTOPER(p), val);
3991 else if ( PL_regkind[(U8)OP(p)] == BRANCHJ) {
3992 regtail(pRExC_state, NEXTOPER(NEXTOPER(p)), val);
3999 - regcurly - a little FSA that accepts {\d+,?\d*}
4002 S_regcurly(pTHX_ register char *s)
4021 S_dumpuntil(pTHX_ regnode *start, regnode *node, regnode *last, SV* sv, I32 l)
4024 register U8 op = EXACT; /* Arbitrary non-END op. */
4025 register regnode *next;
4027 while (op != END && (!last || node < last)) {
4028 /* While that wasn't END last time... */
4034 next = regnext(node);
4036 if (OP(node) == OPTIMIZED)
4039 PerlIO_printf(Perl_debug_log, "%4"IVdf":%*s%s", (IV)(node - start),
4040 (int)(2*l + 1), "", SvPVX(sv));
4041 if (next == NULL) /* Next ptr. */
4042 PerlIO_printf(Perl_debug_log, "(0)");
4044 PerlIO_printf(Perl_debug_log, "(%"IVdf")", (IV)(next - start));
4045 (void)PerlIO_putc(Perl_debug_log, '\n');
4047 if (PL_regkind[(U8)op] == BRANCHJ) {
4048 register regnode *nnode = (OP(next) == LONGJMP
4051 if (last && nnode > last)
4053 node = dumpuntil(start, NEXTOPER(NEXTOPER(node)), nnode, sv, l + 1);
4055 else if (PL_regkind[(U8)op] == BRANCH) {
4056 node = dumpuntil(start, NEXTOPER(node), next, sv, l + 1);
4058 else if ( op == CURLY) { /* `next' might be very big: optimizer */
4059 node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
4060 NEXTOPER(node) + EXTRA_STEP_2ARGS + 1, sv, l + 1);
4062 else if (PL_regkind[(U8)op] == CURLY && op != CURLYX) {
4063 node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
4066 else if ( op == PLUS || op == STAR) {
4067 node = dumpuntil(start, NEXTOPER(node), NEXTOPER(node) + 1, sv, l + 1);
4069 else if (op == ANYOF) {
4070 node = NEXTOPER(node);
4073 else if (PL_regkind[(U8)op] == EXACT) {
4074 /* Literal string, where present. */
4075 node += NODE_SZ_STR(node) - 1;
4076 node = NEXTOPER(node);
4079 node = NEXTOPER(node);
4080 node += regarglen[(U8)op];
4082 if (op == CURLYX || op == OPEN)
4084 else if (op == WHILEM)
4087 #endif /* DEBUGGING */
4092 - regdump - dump a regexp onto Perl_debug_log in vaguely comprehensible form
4095 Perl_regdump(pTHX_ regexp *r)
4098 SV *sv = sv_newmortal();
4100 (void)dumpuntil(r->program, r->program + 1, NULL, sv, 0);
4102 /* Header fields of interest. */
4103 if (r->anchored_substr)
4104 PerlIO_printf(Perl_debug_log,
4105 "anchored `%s%.*s%s'%s at %"IVdf" ",
4107 (int)(SvCUR(r->anchored_substr) - (SvTAIL(r->anchored_substr)!=0)),
4108 SvPVX(r->anchored_substr),
4110 SvTAIL(r->anchored_substr) ? "$" : "",
4111 (IV)r->anchored_offset);
4112 if (r->float_substr)
4113 PerlIO_printf(Perl_debug_log,
4114 "floating `%s%.*s%s'%s at %"IVdf"..%"UVuf" ",
4116 (int)(SvCUR(r->float_substr) - (SvTAIL(r->float_substr)!=0)),
4117 SvPVX(r->float_substr),
4119 SvTAIL(r->float_substr) ? "$" : "",
4120 (IV)r->float_min_offset, (UV)r->float_max_offset);
4121 if (r->check_substr)
4122 PerlIO_printf(Perl_debug_log,
4123 r->check_substr == r->float_substr
4124 ? "(checking floating" : "(checking anchored");
4125 if (r->reganch & ROPT_NOSCAN)
4126 PerlIO_printf(Perl_debug_log, " noscan");
4127 if (r->reganch & ROPT_CHECK_ALL)
4128 PerlIO_printf(Perl_debug_log, " isall");
4129 if (r->check_substr)
4130 PerlIO_printf(Perl_debug_log, ") ");
4132 if (r->regstclass) {
4133 regprop(sv, r->regstclass);
4134 PerlIO_printf(Perl_debug_log, "stclass `%s' ", SvPVX(sv));
4136 if (r->reganch & ROPT_ANCH) {
4137 PerlIO_printf(Perl_debug_log, "anchored");
4138 if (r->reganch & ROPT_ANCH_BOL)
4139 PerlIO_printf(Perl_debug_log, "(BOL)");
4140 if (r->reganch & ROPT_ANCH_MBOL)
4141 PerlIO_printf(Perl_debug_log, "(MBOL)");
4142 if (r->reganch & ROPT_ANCH_SBOL)
4143 PerlIO_printf(Perl_debug_log, "(SBOL)");
4144 if (r->reganch & ROPT_ANCH_GPOS)
4145 PerlIO_printf(Perl_debug_log, "(GPOS)");
4146 PerlIO_putc(Perl_debug_log, ' ');
4148 if (r->reganch & ROPT_GPOS_SEEN)
4149 PerlIO_printf(Perl_debug_log, "GPOS ");
4150 if (r->reganch & ROPT_SKIP)
4151 PerlIO_printf(Perl_debug_log, "plus ");
4152 if (r->reganch & ROPT_IMPLICIT)
4153 PerlIO_printf(Perl_debug_log, "implicit ");
4154 PerlIO_printf(Perl_debug_log, "minlen %ld ", (long) r->minlen);
4155 if (r->reganch & ROPT_EVAL_SEEN)
4156 PerlIO_printf(Perl_debug_log, "with eval ");
4157 PerlIO_printf(Perl_debug_log, "\n");
4158 #endif /* DEBUGGING */
4162 S_put_byte(pTHX_ SV *sv, int c)
4164 if (isCNTRL(c) || c == 127 || c == 255 || !isPRINT(c))
4165 Perl_sv_catpvf(aTHX_ sv, "\\%o", c);
4166 else if (c == '-' || c == ']' || c == '\\' || c == '^')
4167 Perl_sv_catpvf(aTHX_ sv, "\\%c", c);
4169 Perl_sv_catpvf(aTHX_ sv, "%c", c);
4173 - regprop - printable representation of opcode
4176 Perl_regprop(pTHX_ SV *sv, regnode *o)
4181 sv_setpvn(sv, "", 0);
4182 if (OP(o) >= reg_num) /* regnode.type is unsigned */
4183 /* It would be nice to FAIL() here, but this may be called from
4184 regexec.c, and it would be hard to supply pRExC_state. */
4185 Perl_croak(aTHX_ "Corrupted regexp opcode");
4186 sv_catpv(sv, (char*)reg_name[OP(o)]); /* Take off const! */
4188 k = PL_regkind[(U8)OP(o)];
4191 Perl_sv_catpvf(aTHX_ sv, " <%s%.*s%s>", PL_colors[0],
4192 STR_LEN(o), STRING(o), PL_colors[1]);
4193 else if (k == CURLY) {
4194 if (OP(o) == CURLYM || OP(o) == CURLYN || OP(o) == CURLYX)
4195 Perl_sv_catpvf(aTHX_ sv, "[%d]", o->flags); /* Parenth number */
4196 Perl_sv_catpvf(aTHX_ sv, " {%d,%d}", ARG1(o), ARG2(o));
4198 else if (k == WHILEM && o->flags) /* Ordinal/of */
4199 Perl_sv_catpvf(aTHX_ sv, "[%d/%d]", o->flags & 0xf, o->flags>>4);
4200 else if (k == REF || k == OPEN || k == CLOSE || k == GROUPP )
4201 Perl_sv_catpvf(aTHX_ sv, "%d", (int)ARG(o)); /* Parenth number */
4202 else if (k == LOGICAL)
4203 Perl_sv_catpvf(aTHX_ sv, "[%d]", o->flags); /* 2: embedded, otherwise 1 */
4204 else if (k == ANYOF) {
4205 int i, rangestart = -1;
4206 U8 flags = ANYOF_FLAGS(o);
4207 const char * const anyofs[] = { /* Should be syncronized with
4208 * ANYOF_ #xdefines in regcomp.h */
4241 if (flags & ANYOF_LOCALE)
4242 sv_catpv(sv, "{loc}");
4243 if (flags & ANYOF_FOLD)
4244 sv_catpv(sv, "{i}");
4245 Perl_sv_catpvf(aTHX_ sv, "[%s", PL_colors[0]);
4246 if (flags & ANYOF_INVERT)
4248 for (i = 0; i <= 256; i++) {
4249 if (i < 256 && ANYOF_BITMAP_TEST(o,i)) {
4250 if (rangestart == -1)
4252 } else if (rangestart != -1) {
4253 if (i <= rangestart + 3)
4254 for (; rangestart < i; rangestart++)
4255 put_byte(sv, rangestart);
4257 put_byte(sv, rangestart);
4259 put_byte(sv, i - 1);
4265 if (o->flags & ANYOF_CLASS)
4266 for (i = 0; i < sizeof(anyofs)/sizeof(char*); i++)
4267 if (ANYOF_CLASS_TEST(o,i))
4268 sv_catpv(sv, anyofs[i]);
4270 if (flags & ANYOF_UNICODE)
4271 sv_catpv(sv, "{unicode}");
4272 else if (flags & ANYOF_UNICODE_ALL)
4273 sv_catpv(sv, "{all-unicode}");
4277 SV *sw = regclass_swash(o, FALSE, &lv);
4282 U8 s[UTF8_MAXLEN+1];
4284 for (i = 0; i <= 256; i++) { /* just the first 256 */
4285 U8 *e = uv_to_utf8(s, i);
4287 if (i < 256 && swash_fetch(sw, s)) {
4288 if (rangestart == -1)
4290 } else if (rangestart != -1) {
4293 if (i <= rangestart + 3)
4294 for (; rangestart < i; rangestart++) {
4295 for(e = uv_to_utf8(s, rangestart), p = s; p < e; p++)
4299 for (e = uv_to_utf8(s, rangestart), p = s; p < e; p++)
4302 for (e = uv_to_utf8(s, i - 1), p = s; p < e; p++)
4309 sv_catpv(sv, "..."); /* et cetera */
4313 char *s = savepv(SvPVX(lv));
4316 while(*s && *s != '\n') s++;
4337 Perl_sv_catpvf(aTHX_ sv, "%s]", PL_colors[1]);
4339 else if (k == BRANCHJ && (OP(o) == UNLESSM || OP(o) == IFMATCH))
4340 Perl_sv_catpvf(aTHX_ sv, "[-%d]", o->flags);
4341 #endif /* DEBUGGING */
4345 Perl_re_intuit_string(pTHX_ regexp *prog)
4346 { /* Assume that RE_INTUIT is set */
4349 char *s = SvPV(prog->check_substr,n_a);
4351 if (!PL_colorset) reginitcolors();
4352 PerlIO_printf(Perl_debug_log,
4353 "%sUsing REx substr:%s `%s%.60s%s%s'\n",
4354 PL_colors[4],PL_colors[5],PL_colors[0],
4357 (strlen(s) > 60 ? "..." : ""));
4360 return prog->check_substr;
4364 Perl_pregfree(pTHX_ struct regexp *r)
4366 DEBUG_r(if (!PL_colorset) reginitcolors());
4368 if (!r || (--r->refcnt > 0))
4370 DEBUG_r(PerlIO_printf(Perl_debug_log,
4371 "%sFreeing REx:%s `%s%.60s%s%s'\n",
4372 PL_colors[4],PL_colors[5],PL_colors[0],
4375 (strlen(r->precomp) > 60 ? "..." : "")));
4378 Safefree(r->precomp);
4379 if (RX_MATCH_COPIED(r))
4380 Safefree(r->subbeg);
4382 if (r->anchored_substr)
4383 SvREFCNT_dec(r->anchored_substr);
4384 if (r->float_substr)
4385 SvREFCNT_dec(r->float_substr);
4386 Safefree(r->substrs);
4389 int n = r->data->count;
4390 AV* new_comppad = NULL;
4395 switch (r->data->what[n]) {
4397 SvREFCNT_dec((SV*)r->data->data[n]);
4400 Safefree(r->data->data[n]);
4403 new_comppad = (AV*)r->data->data[n];
4406 if (new_comppad == NULL)
4407 Perl_croak(aTHX_ "panic: pregfree comppad");
4408 old_comppad = PL_comppad;
4409 old_curpad = PL_curpad;
4410 /* Watch out for global destruction's random ordering. */
4411 if (SvTYPE(new_comppad) == SVt_PVAV) {
4412 PL_comppad = new_comppad;
4413 PL_curpad = AvARRAY(new_comppad);
4417 op_free((OP_4tree*)r->data->data[n]);
4418 PL_comppad = old_comppad;
4419 PL_curpad = old_curpad;
4420 SvREFCNT_dec((SV*)new_comppad);
4426 Perl_croak(aTHX_ "panic: regfree data code '%c'", r->data->what[n]);
4429 Safefree(r->data->what);
4432 Safefree(r->startp);
4438 - regnext - dig the "next" pointer out of a node
4440 * [Note, when REGALIGN is defined there are two places in regmatch()
4441 * that bypass this code for speed.]
4444 Perl_regnext(pTHX_ register regnode *p)
4446 register I32 offset;
4448 if (p == &PL_regdummy)
4451 offset = (reg_off_by_arg[OP(p)] ? ARG(p) : NEXT_OFF(p));
4459 S_re_croak2(pTHX_ const char* pat1,const char* pat2,...)
4462 STRLEN l1 = strlen(pat1);
4463 STRLEN l2 = strlen(pat2);
4472 Copy(pat1, buf, l1 , char);
4473 Copy(pat2, buf + l1, l2 , char);
4474 buf[l1 + l2] = '\n';
4475 buf[l1 + l2 + 1] = '\0';
4477 /* ANSI variant takes additional second argument */
4478 va_start(args, pat2);
4482 msv = vmess(buf, &args);
4484 message = SvPV(msv,l1);
4487 Copy(message, buf, l1 , char);
4488 buf[l1] = '\0'; /* Overwrite \n */
4489 Perl_croak(aTHX_ "%s", buf);
4492 /* XXX Here's a total kludge. But we need to re-enter for swash routines. */
4495 Perl_save_re_context(pTHX)
4498 SAVEPPTR(RExC_precomp); /* uncompiled string. */
4499 SAVEI32(RExC_npar); /* () count. */
4500 SAVEI32(RExC_size); /* Code size. */
4501 SAVEI16(RExC_flags16); /* are we folding, multilining? */
4502 SAVEVPTR(RExC_rx); /* from regcomp.c */
4503 SAVEI32(RExC_seen); /* from regcomp.c */
4504 SAVEI32(RExC_sawback); /* Did we see \1, ...? */
4505 SAVEI32(RExC_naughty); /* How bad is this pattern? */
4506 SAVEVPTR(RExC_emit); /* Code-emit pointer; ®dummy = don't */
4507 SAVEPPTR(RExC_end); /* End of input for compile */
4508 SAVEPPTR(RExC_parse); /* Input-scan pointer. */
4511 SAVEI32(PL_reg_flags); /* from regexec.c */
4513 SAVEPPTR(PL_reginput); /* String-input pointer. */
4514 SAVEPPTR(PL_regbol); /* Beginning of input, for ^ check. */
4515 SAVEPPTR(PL_regeol); /* End of input, for $ check. */
4516 SAVEVPTR(PL_regstartp); /* Pointer to startp array. */
4517 SAVEVPTR(PL_regendp); /* Ditto for endp. */
4518 SAVEVPTR(PL_reglastparen); /* Similarly for lastparen. */
4519 SAVEPPTR(PL_regtill); /* How far we are required to go. */
4520 SAVEI8(PL_regprev); /* char before regbol, \n if none */
4521 SAVEGENERICPV(PL_reg_start_tmp); /* from regexec.c */
4522 PL_reg_start_tmp = 0;
4523 SAVEI32(PL_reg_start_tmpl); /* from regexec.c */
4524 PL_reg_start_tmpl = 0;
4525 SAVEVPTR(PL_regdata);
4526 SAVEI32(PL_reg_eval_set); /* from regexec.c */
4527 SAVEI32(PL_regnarrate); /* from regexec.c */
4528 SAVEVPTR(PL_regprogram); /* from regexec.c */
4529 SAVEINT(PL_regindent); /* from regexec.c */
4530 SAVEVPTR(PL_regcc); /* from regexec.c */
4531 SAVEVPTR(PL_curcop);
4532 SAVEVPTR(PL_reg_call_cc); /* from regexec.c */
4533 SAVEVPTR(PL_reg_re); /* from regexec.c */
4534 SAVEPPTR(PL_reg_ganch); /* from regexec.c */
4535 SAVESPTR(PL_reg_sv); /* from regexec.c */
4536 SAVEVPTR(PL_reg_magic); /* from regexec.c */
4537 SAVEI32(PL_reg_oldpos); /* from regexec.c */
4538 SAVEVPTR(PL_reg_oldcurpm); /* from regexec.c */
4539 SAVEVPTR(PL_reg_curpm); /* from regexec.c */
4540 SAVEI32(PL_regnpar); /* () count. */
4542 SAVEPPTR(PL_reg_starttry); /* from regexec.c */
4553 clear_re(pTHXo_ void *r)
4555 ReREFCNT_dec((regexp *)r);