5 * "A fair jaw-cracker dwarf-language must be." --Samwise Gamgee
8 /* NOTE: this is derived from Henry Spencer's regexp code, and should not
9 * confused with the original package (see point 3 below). Thanks, Henry!
12 /* Additional note: this code is very heavily munged from Henry's version
13 * in places. In some spots I've traded clarity for efficiency, so don't
14 * blame Henry for some of the lack of readability.
17 /* The names of the functions have been changed from regcomp and
18 * regexec to pregcomp and pregexec in order to avoid conflicts
19 * with the POSIX routines of the same names.
22 #ifdef PERL_EXT_RE_BUILD
23 /* need to replace pregcomp et al, so enable that */
24 # ifndef PERL_IN_XSUB_RE
25 # define PERL_IN_XSUB_RE
27 /* need access to debugger hooks */
28 # if defined(PERL_EXT_RE_DEBUG) && !defined(DEBUGGING)
33 #ifdef PERL_IN_XSUB_RE
34 /* We *really* need to overwrite these symbols: */
35 # define Perl_pregcomp my_regcomp
36 # define Perl_regdump my_regdump
37 # define Perl_regprop my_regprop
38 # define Perl_pregfree my_regfree
39 # define Perl_re_intuit_string my_re_intuit_string
40 /* *These* symbols are masked to allow static link. */
41 # define Perl_regnext my_regnext
42 # define Perl_save_re_context my_save_re_context
43 # define Perl_reginitcolors my_reginitcolors
45 # define PERL_NO_GET_CONTEXT
50 * pregcomp and pregexec -- regsub and regerror are not used in perl
52 * Copyright (c) 1986 by University of Toronto.
53 * Written by Henry Spencer. Not derived from licensed software.
55 * Permission is granted to anyone to use this software for any
56 * purpose on any computer system, and to redistribute it freely,
57 * subject to the following restrictions:
59 * 1. The author is not responsible for the consequences of use of
60 * this software, no matter how awful, even if they arise
63 * 2. The origin of this software must not be misrepresented, either
64 * by explicit claim or by omission.
66 * 3. Altered versions must be plainly marked as such, and must not
67 * be misrepresented as being the original software.
70 **** Alterations to Henry's code are...
72 **** Copyright (c) 1991-2000, Larry Wall
74 **** You may distribute under the terms of either the GNU General Public
75 **** License or the Artistic License, as specified in the README file.
78 * Beware that some of this code is subtly aware of the way operator
79 * precedence is structured in regular expressions. Serious changes in
80 * regular-expression syntax might require a total rethink.
83 #define PERL_IN_REGCOMP_C
86 #ifdef PERL_IN_XSUB_RE
87 # if defined(PERL_CAPI) || defined(PERL_OBJECT)
102 # if defined(BUGGY_MSC6)
103 /* MSC 6.00A breaks on op/regexp.t test 85 unless we turn this off */
104 # pragma optimize("a",off)
105 /* But MSC 6.00A is happy with 'w', for aliases only across function calls*/
106 # pragma optimize("w",on )
107 # endif /* BUGGY_MSC6 */
111 #define STATIC static
114 typedef struct RExC_state_t {
115 U16 flags16; /* are we folding, multilining? */
116 char *precomp; /* uncompiled string. */
118 char *end; /* End of input for compile */
119 char *parse; /* Input-scan pointer. */
120 I32 whilem_seen; /* number of WHILEM in this expr */
121 regnode *emit; /* Code-emit pointer; ®dummy = don't */
122 I32 naughty; /* How bad is this pattern? */
123 I32 sawback; /* Did we see \1, ...? */
125 I32 size; /* Code size. */
126 I32 npar; /* () count. */
131 char *starttry; /* -Dr: where regtry was called. */
132 #define RExC_starttry (pRExC_state->starttry)
136 #define RExC_flags16 (pRExC_state->flags16)
137 #define RExC_precomp (pRExC_state->precomp)
138 #define RExC_rx (pRExC_state->rx)
139 #define RExC_end (pRExC_state->end)
140 #define RExC_parse (pRExC_state->parse)
141 #define RExC_whilem_seen (pRExC_state->whilem_seen)
142 #define RExC_emit (pRExC_state->emit)
143 #define RExC_naughty (pRExC_state->naughty)
144 #define RExC_sawback (pRExC_state->sawback)
145 #define RExC_seen (pRExC_state->seen)
146 #define RExC_size (pRExC_state->size)
147 #define RExC_npar (pRExC_state->npar)
148 #define RExC_extralen (pRExC_state->extralen)
149 #define RExC_seen_zerolen (pRExC_state->seen_zerolen)
150 #define RExC_seen_evals (pRExC_state->seen_evals)
152 #define ISMULT1(c) ((c) == '*' || (c) == '+' || (c) == '?')
153 #define ISMULT2(s) ((*s) == '*' || (*s) == '+' || (*s) == '?' || \
154 ((*s) == '{' && regcurly(s)))
156 #define PERL_META "^$.[()|?+*\\"
158 #define META "^$.[()|?+*\\"
162 #undef SPSTART /* dratted cpp namespace... */
165 * Flags to be passed up and down.
167 #define WORST 0 /* Worst case. */
168 #define HASWIDTH 0x1 /* Known to match non-null strings. */
169 #define SIMPLE 0x2 /* Simple enough to be STAR/PLUS operand. */
170 #define SPSTART 0x4 /* Starts with * or +. */
171 #define TRYAGAIN 0x8 /* Weeded out a declaration. */
173 /* Length of a variant. */
175 typedef struct scan_data_t {
181 I32 last_end; /* min value, <0 unless valid. */
184 SV **longest; /* Either &l_fixed, or &l_float. */
188 I32 offset_float_min;
189 I32 offset_float_max;
193 struct regnode_charclass_class *start_class;
197 * Forward declarations for pregcomp()'s friends.
200 static scan_data_t zero_scan_data = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
203 #define SF_BEFORE_EOL (SF_BEFORE_SEOL|SF_BEFORE_MEOL)
204 #define SF_BEFORE_SEOL 0x1
205 #define SF_BEFORE_MEOL 0x2
206 #define SF_FIX_BEFORE_EOL (SF_FIX_BEFORE_SEOL|SF_FIX_BEFORE_MEOL)
207 #define SF_FL_BEFORE_EOL (SF_FL_BEFORE_SEOL|SF_FL_BEFORE_MEOL)
210 # define SF_FIX_SHIFT_EOL (0+2)
211 # define SF_FL_SHIFT_EOL (0+4)
213 # define SF_FIX_SHIFT_EOL (+2)
214 # define SF_FL_SHIFT_EOL (+4)
217 #define SF_FIX_BEFORE_SEOL (SF_BEFORE_SEOL << SF_FIX_SHIFT_EOL)
218 #define SF_FIX_BEFORE_MEOL (SF_BEFORE_MEOL << SF_FIX_SHIFT_EOL)
220 #define SF_FL_BEFORE_SEOL (SF_BEFORE_SEOL << SF_FL_SHIFT_EOL)
221 #define SF_FL_BEFORE_MEOL (SF_BEFORE_MEOL << SF_FL_SHIFT_EOL) /* 0x20 */
222 #define SF_IS_INF 0x40
223 #define SF_HAS_PAR 0x80
224 #define SF_IN_PAR 0x100
225 #define SF_HAS_EVAL 0x200
226 #define SCF_DO_SUBSTR 0x400
227 #define SCF_DO_STCLASS_AND 0x0800
228 #define SCF_DO_STCLASS_OR 0x1000
229 #define SCF_DO_STCLASS (SCF_DO_STCLASS_AND|SCF_DO_STCLASS_OR)
230 #define SCF_WHILEM_VISITED_POS 0x2000
233 #define UTF (PL_reg_flags & RF_utf8)
234 #define LOC (RExC_flags16 & PMf_LOCALE)
235 #define FOLD (RExC_flags16 & PMf_FOLD)
237 #define OOB_CHAR8 1234
238 #define OOB_UTF8 123456
239 #define OOB_NAMEDCLASS -1
241 #define CHR_SVLEN(sv) (UTF ? sv_len_utf8(sv) : SvCUR(sv))
242 #define CHR_DIST(a,b) (UTF ? utf8_distance(a,b) : a - b)
245 /* length of regex to show in messages that don't mark a position within */
246 #define RegexLengthToShowInErrorMessages 127
249 * If MARKER[12] are adjusted, be sure to adjust the constants at the top
250 * of t/op/regmesg.t, the tests in t/op/re_tests, and those in
251 * op/pragma/warn/regcomp.
253 #define MARKER1 "HERE" /* marker as it appears in the description */
254 #define MARKER2 " << HERE " /* marker as it appears within the regex */
256 #define REPORT_LOCATION " before " MARKER1 " mark in regex m/%.*s" MARKER2 "%s/"
259 * Calls SAVEDESTRUCTOR_X if needed, then calls Perl_croak with the given
260 * arg. Show regex, up to a maximum length. If it's too long, chop and add
265 char *ellipses = ""; \
266 unsigned len = strlen(RExC_precomp); \
269 SAVEDESTRUCTOR_X(clear_re,(void*)RExC_rx); \
271 if (len > RegexLengthToShowInErrorMessages) { \
272 /* chop 10 shorter than the max, to ensure meaning of "..." */ \
273 len = RegexLengthToShowInErrorMessages - 10; \
276 Perl_croak(aTHX_ "%s in regex m/%.*s%s/", \
277 msg, (int)len, RExC_precomp, ellipses); \
281 * Calls SAVEDESTRUCTOR_X if needed, then calls Perl_croak with the given
282 * args. Show regex, up to a maximum length. If it's too long, chop and add
285 #define FAIL2(pat,msg) \
287 char *ellipses = ""; \
288 unsigned len = strlen(RExC_precomp); \
291 SAVEDESTRUCTOR_X(clear_re,(void*)RExC_rx); \
293 if (len > RegexLengthToShowInErrorMessages) { \
294 /* chop 10 shorter than the max, to ensure meaning of "..." */ \
295 len = RegexLengthToShowInErrorMessages - 10; \
298 S_re_croak2(aTHX_ pat, " in regex m/%.*s%s/", \
299 msg, (int)len, RExC_precomp, ellipses); \
304 * Simple_vFAIL -- like FAIL, but marks the current location in the scan
306 #define Simple_vFAIL(m) \
308 unsigned offset = strlen(RExC_precomp)-(RExC_end-RExC_parse); \
310 Perl_croak(aTHX_ "%s" REPORT_LOCATION, \
311 m, (int)offset, RExC_precomp, RExC_precomp + offset); \
315 * Calls SAVEDESTRUCTOR_X if needed, then Simple_vFAIL()
320 SAVEDESTRUCTOR_X(clear_re,(void*)RExC_rx); \
325 * Like Simple_vFAIL(), but accepts two arguments.
327 #define Simple_vFAIL2(m,a1) \
329 unsigned offset = strlen(RExC_precomp)-(RExC_end-RExC_parse); \
331 S_re_croak2(aTHX_ m, REPORT_LOCATION, a1, \
332 (int)offset, RExC_precomp, RExC_precomp + offset); \
336 * Calls SAVEDESTRUCTOR_X if needed, then Simple_vFAIL2().
338 #define vFAIL2(m,a1) \
341 SAVEDESTRUCTOR_X(clear_re,(void*)RExC_rx); \
342 Simple_vFAIL2(m, a1); \
347 * Like Simple_vFAIL(), but accepts three arguments.
349 #define Simple_vFAIL3(m, a1, a2) \
351 unsigned offset = strlen(RExC_precomp)-(RExC_end-RExC_parse); \
353 S_re_croak2(aTHX_ m, REPORT_LOCATION, a1, a2, \
354 (int)offset, RExC_precomp, RExC_precomp + offset); \
358 * Calls SAVEDESTRUCTOR_X if needed, then Simple_vFAIL3().
360 #define vFAIL3(m,a1,a2) \
363 SAVEDESTRUCTOR_X(clear_re,(void*)RExC_rx); \
364 Simple_vFAIL3(m, a1, a2); \
368 * Like Simple_vFAIL(), but accepts four arguments.
370 #define Simple_vFAIL4(m, a1, a2, a3) \
372 unsigned offset = strlen(RExC_precomp)-(RExC_end-RExC_parse); \
374 S_re_croak2(aTHX_ m, REPORT_LOCATION, a1, a2, a3,\
375 (int)offset, RExC_precomp, RExC_precomp + offset); \
379 * Like Simple_vFAIL(), but accepts five arguments.
381 #define Simple_vFAIL5(m, a1, a2, a3, a4) \
383 unsigned offset = strlen(RExC_precomp)-(RExC_end-RExC_parse); \
384 S_re_croak2(aTHX_ m, REPORT_LOCATION, a1, a2, a3, a4,\
385 (int)offset, RExC_precomp, RExC_precomp + offset); \
389 #define vWARN(loc,m) \
391 unsigned offset = strlen(RExC_precomp)-(RExC_end-(loc)); \
392 Perl_warner(aTHX_ WARN_REGEXP, "%s" REPORT_LOCATION,\
393 m, (int)offset, RExC_precomp, RExC_precomp + offset); \
397 #define vWARN2(loc, m, a1) \
399 unsigned offset = strlen(RExC_precomp)-(RExC_end-(loc)); \
400 Perl_warner(aTHX_ WARN_REGEXP, m REPORT_LOCATION,\
402 (int)offset, RExC_precomp, RExC_precomp + offset); \
405 #define vWARN3(loc, m, a1, a2) \
407 unsigned offset = strlen(RExC_precomp) - (RExC_end - (loc)); \
408 Perl_warner(aTHX_ WARN_REGEXP, m REPORT_LOCATION, \
410 (int)offset, RExC_precomp, RExC_precomp + offset); \
413 #define vWARN4(loc, m, a1, a2, a3) \
415 unsigned offset = strlen(RExC_precomp)-(RExC_end-(loc)); \
416 Perl_warner(aTHX_ WARN_REGEXP, m REPORT_LOCATION,\
418 (int)offset, RExC_precomp, RExC_precomp + offset); \
422 /* Allow for side effects in s */
423 #define REGC(c,s) STMT_START { if (!SIZE_ONLY) *(s) = (c); else (s);} STMT_END
425 static void clear_re(pTHXo_ void *r);
427 /* Mark that we cannot extend a found fixed substring at this point.
428 Updata the longest found anchored substring and the longest found
429 floating substrings if needed. */
432 S_scan_commit(pTHX_ RExC_state_t *pRExC_state, scan_data_t *data)
434 STRLEN l = CHR_SVLEN(data->last_found);
435 STRLEN old_l = CHR_SVLEN(*data->longest);
437 if ((l >= old_l) && ((l > old_l) || (data->flags & SF_BEFORE_EOL))) {
438 sv_setsv(*data->longest, data->last_found);
439 if (*data->longest == data->longest_fixed) {
440 data->offset_fixed = l ? data->last_start_min : data->pos_min;
441 if (data->flags & SF_BEFORE_EOL)
443 |= ((data->flags & SF_BEFORE_EOL) << SF_FIX_SHIFT_EOL);
445 data->flags &= ~SF_FIX_BEFORE_EOL;
448 data->offset_float_min = l ? data->last_start_min : data->pos_min;
449 data->offset_float_max = (l
450 ? data->last_start_max
451 : data->pos_min + data->pos_delta);
452 if (data->flags & SF_BEFORE_EOL)
454 |= ((data->flags & SF_BEFORE_EOL) << SF_FL_SHIFT_EOL);
456 data->flags &= ~SF_FL_BEFORE_EOL;
459 SvCUR_set(data->last_found, 0);
461 data->flags &= ~SF_BEFORE_EOL;
464 /* Can match anything (initialization) */
466 S_cl_anything(pTHX_ RExC_state_t *pRExC_state, struct regnode_charclass_class *cl)
470 ANYOF_CLASS_ZERO(cl);
471 for (value = 0; value < 256; ++value)
472 ANYOF_BITMAP_SET(cl, value);
473 cl->flags = ANYOF_EOS;
475 cl->flags |= ANYOF_LOCALE;
478 /* Can match anything (initialization) */
480 S_cl_is_anything(pTHX_ struct regnode_charclass_class *cl)
484 for (value = 0; value <= ANYOF_MAX; value += 2)
485 if (ANYOF_CLASS_TEST(cl, value) && ANYOF_CLASS_TEST(cl, value + 1))
487 for (value = 0; value < 256; ++value)
488 if (!ANYOF_BITMAP_TEST(cl, value))
493 /* Can match anything (initialization) */
495 S_cl_init(pTHX_ RExC_state_t *pRExC_state, struct regnode_charclass_class *cl)
497 Zero(cl, 1, struct regnode_charclass_class);
499 cl_anything(pRExC_state, cl);
503 S_cl_init_zero(pTHX_ RExC_state_t *pRExC_state, struct regnode_charclass_class *cl)
505 Zero(cl, 1, struct regnode_charclass_class);
507 cl_anything(pRExC_state, cl);
509 cl->flags |= ANYOF_LOCALE;
512 /* 'And' a given class with another one. Can create false positives */
513 /* We assume that cl is not inverted */
515 S_cl_and(pTHX_ struct regnode_charclass_class *cl,
516 struct regnode_charclass_class *and_with)
518 if (!(and_with->flags & ANYOF_CLASS)
519 && !(cl->flags & ANYOF_CLASS)
520 && (and_with->flags & ANYOF_LOCALE) == (cl->flags & ANYOF_LOCALE)
521 && !(and_with->flags & ANYOF_FOLD)
522 && !(cl->flags & ANYOF_FOLD)) {
525 if (and_with->flags & ANYOF_INVERT)
526 for (i = 0; i < ANYOF_BITMAP_SIZE; i++)
527 cl->bitmap[i] &= ~and_with->bitmap[i];
529 for (i = 0; i < ANYOF_BITMAP_SIZE; i++)
530 cl->bitmap[i] &= and_with->bitmap[i];
531 } /* XXXX: logic is complicated otherwise, leave it along for a moment. */
532 if (!(and_with->flags & ANYOF_EOS))
533 cl->flags &= ~ANYOF_EOS;
536 /* 'OR' a given class with another one. Can create false positives */
537 /* We assume that cl is not inverted */
539 S_cl_or(pTHX_ RExC_state_t *pRExC_state, struct regnode_charclass_class *cl, struct regnode_charclass_class *or_with)
541 if (or_with->flags & ANYOF_INVERT) {
543 * (B1 | CL1) | (!B2 & !CL2) = (B1 | !B2 & !CL2) | (CL1 | (!B2 & !CL2))
544 * <= (B1 | !B2) | (CL1 | !CL2)
545 * which is wasteful if CL2 is small, but we ignore CL2:
546 * (B1 | CL1) | (!B2 & !CL2) <= (B1 | CL1) | !B2 = (B1 | !B2) | CL1
547 * XXXX Can we handle case-fold? Unclear:
548 * (OK1(i) | OK1(i')) | !(OK1(i) | OK1(i')) =
549 * (OK1(i) | OK1(i')) | (!OK1(i) & !OK1(i'))
551 if ( (or_with->flags & ANYOF_LOCALE) == (cl->flags & ANYOF_LOCALE)
552 && !(or_with->flags & ANYOF_FOLD)
553 && !(cl->flags & ANYOF_FOLD) ) {
556 for (i = 0; i < ANYOF_BITMAP_SIZE; i++)
557 cl->bitmap[i] |= ~or_with->bitmap[i];
558 } /* XXXX: logic is complicated otherwise */
560 cl_anything(pRExC_state, cl);
563 /* (B1 | CL1) | (B2 | CL2) = (B1 | B2) | (CL1 | CL2)) */
564 if ( (or_with->flags & ANYOF_LOCALE) == (cl->flags & ANYOF_LOCALE)
565 && (!(or_with->flags & ANYOF_FOLD)
566 || (cl->flags & ANYOF_FOLD)) ) {
569 /* OR char bitmap and class bitmap separately */
570 for (i = 0; i < ANYOF_BITMAP_SIZE; i++)
571 cl->bitmap[i] |= or_with->bitmap[i];
572 if (or_with->flags & ANYOF_CLASS) {
573 for (i = 0; i < ANYOF_CLASSBITMAP_SIZE; i++)
574 cl->classflags[i] |= or_with->classflags[i];
575 cl->flags |= ANYOF_CLASS;
578 else { /* XXXX: logic is complicated, leave it along for a moment. */
579 cl_anything(pRExC_state, cl);
582 if (or_with->flags & ANYOF_EOS)
583 cl->flags |= ANYOF_EOS;
586 /* REx optimizer. Converts nodes into quickier variants "in place".
587 Finds fixed substrings. */
589 /* Stops at toplevel WHILEM as well as at `last'. At end *scanp is set
590 to the position after last scanned or to NULL. */
593 S_study_chunk(pTHX_ RExC_state_t *pRExC_state, regnode **scanp, I32 *deltap, regnode *last, scan_data_t *data, U32 flags)
594 /* scanp: Start here (read-write). */
595 /* deltap: Write maxlen-minlen here. */
596 /* last: Stop before this one. */
598 I32 min = 0, pars = 0, code;
599 regnode *scan = *scanp, *next;
601 int is_inf = (flags & SCF_DO_SUBSTR) && (data->flags & SF_IS_INF);
602 int is_inf_internal = 0; /* The studied chunk is infinite */
603 I32 is_par = OP(scan) == OPEN ? ARG(scan) : 0;
604 scan_data_t data_fake;
605 struct regnode_charclass_class and_with; /* Valid if flags & SCF_DO_STCLASS_OR */
607 while (scan && OP(scan) != END && scan < last) {
608 /* Peephole optimizer: */
610 if (PL_regkind[(U8)OP(scan)] == EXACT) {
611 /* Merge several consecutive EXACTish nodes into one. */
612 regnode *n = regnext(scan);
615 regnode *stop = scan;
618 next = scan + NODE_SZ_STR(scan);
619 /* Skip NOTHING, merge EXACT*. */
621 ( PL_regkind[(U8)OP(n)] == NOTHING ||
622 (stringok && (OP(n) == OP(scan))))
624 && NEXT_OFF(scan) + NEXT_OFF(n) < I16_MAX) {
625 if (OP(n) == TAIL || n > next)
627 if (PL_regkind[(U8)OP(n)] == NOTHING) {
628 NEXT_OFF(scan) += NEXT_OFF(n);
629 next = n + NODE_STEP_REGNODE;
637 int oldl = STR_LEN(scan);
638 regnode *nnext = regnext(n);
640 if (oldl + STR_LEN(n) > U8_MAX)
642 NEXT_OFF(scan) += NEXT_OFF(n);
643 STR_LEN(scan) += STR_LEN(n);
644 next = n + NODE_SZ_STR(n);
645 /* Now we can overwrite *n : */
646 Move(STRING(n), STRING(scan) + oldl,
657 n = scan + NODE_SZ_STR(scan);
659 if (PL_regkind[(U8)OP(n)] != NOTHING || OP(n) == NOTHING) {
667 /* Follow the next-chain of the current node and optimize
668 away all the NOTHINGs from it. */
669 if (OP(scan) != CURLYX) {
670 int max = (reg_off_by_arg[OP(scan)]
672 /* I32 may be smaller than U16 on CRAYs! */
673 : (I32_MAX < U16_MAX ? I32_MAX : U16_MAX));
674 int off = (reg_off_by_arg[OP(scan)] ? ARG(scan) : NEXT_OFF(scan));
678 /* Skip NOTHING and LONGJMP. */
679 while ((n = regnext(n))
680 && ((PL_regkind[(U8)OP(n)] == NOTHING && (noff = NEXT_OFF(n)))
681 || ((OP(n) == LONGJMP) && (noff = ARG(n))))
684 if (reg_off_by_arg[OP(scan)])
687 NEXT_OFF(scan) = off;
689 /* The principal pseudo-switch. Cannot be a switch, since we
690 look into several different things. */
691 if (OP(scan) == BRANCH || OP(scan) == BRANCHJ
692 || OP(scan) == IFTHEN || OP(scan) == SUSPEND) {
693 next = regnext(scan);
696 if (OP(next) == code || code == IFTHEN || code == SUSPEND) {
697 I32 max1 = 0, min1 = I32_MAX, num = 0;
698 struct regnode_charclass_class accum;
700 if (flags & SCF_DO_SUBSTR) /* XXXX Add !SUSPEND? */
701 scan_commit(pRExC_state, data); /* Cannot merge strings after this. */
702 if (flags & SCF_DO_STCLASS)
703 cl_init_zero(pRExC_state, &accum);
704 while (OP(scan) == code) {
705 I32 deltanext, minnext, f = 0, fake;
706 struct regnode_charclass_class this_class;
711 data_fake.whilem_c = data->whilem_c;
712 data_fake.last_closep = data->last_closep;
715 data_fake.last_closep = &fake;
716 next = regnext(scan);
717 scan = NEXTOPER(scan);
719 scan = NEXTOPER(scan);
720 if (flags & SCF_DO_STCLASS) {
721 cl_init(pRExC_state, &this_class);
722 data_fake.start_class = &this_class;
723 f = SCF_DO_STCLASS_AND;
725 if (flags & SCF_WHILEM_VISITED_POS)
726 f |= SCF_WHILEM_VISITED_POS;
727 /* we suppose the run is continuous, last=next...*/
728 minnext = study_chunk(pRExC_state, &scan, &deltanext,
729 next, &data_fake, f);
732 if (max1 < minnext + deltanext)
733 max1 = minnext + deltanext;
734 if (deltanext == I32_MAX)
735 is_inf = is_inf_internal = 1;
737 if (data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
739 if (data && (data_fake.flags & SF_HAS_EVAL))
740 data->flags |= SF_HAS_EVAL;
742 data->whilem_c = data_fake.whilem_c;
743 if (flags & SCF_DO_STCLASS)
744 cl_or(pRExC_state, &accum, &this_class);
748 if (code == IFTHEN && num < 2) /* Empty ELSE branch */
750 if (flags & SCF_DO_SUBSTR) {
751 data->pos_min += min1;
752 data->pos_delta += max1 - min1;
753 if (max1 != min1 || is_inf)
754 data->longest = &(data->longest_float);
757 delta += max1 - min1;
758 if (flags & SCF_DO_STCLASS_OR) {
759 cl_or(pRExC_state, data->start_class, &accum);
761 cl_and(data->start_class, &and_with);
762 flags &= ~SCF_DO_STCLASS;
765 else if (flags & SCF_DO_STCLASS_AND) {
767 cl_and(data->start_class, &accum);
768 flags &= ~SCF_DO_STCLASS;
771 /* Switch to OR mode: cache the old value of
772 * data->start_class */
773 StructCopy(data->start_class, &and_with,
774 struct regnode_charclass_class);
775 flags &= ~SCF_DO_STCLASS_AND;
776 StructCopy(&accum, data->start_class,
777 struct regnode_charclass_class);
778 flags |= SCF_DO_STCLASS_OR;
779 data->start_class->flags |= ANYOF_EOS;
783 else if (code == BRANCHJ) /* single branch is optimized. */
784 scan = NEXTOPER(NEXTOPER(scan));
785 else /* single branch is optimized. */
786 scan = NEXTOPER(scan);
789 else if (OP(scan) == EXACT) {
790 I32 l = STR_LEN(scan);
792 unsigned char *s = (unsigned char *)STRING(scan);
793 unsigned char *e = s + l;
802 if (flags & SCF_DO_SUBSTR) { /* Update longest substr. */
803 /* The code below prefers earlier match for fixed
804 offset, later match for variable offset. */
805 if (data->last_end == -1) { /* Update the start info. */
806 data->last_start_min = data->pos_min;
807 data->last_start_max = is_inf
808 ? I32_MAX : data->pos_min + data->pos_delta;
810 sv_catpvn(data->last_found, STRING(scan), STR_LEN(scan));
811 data->last_end = data->pos_min + l;
812 data->pos_min += l; /* As in the first entry. */
813 data->flags &= ~SF_BEFORE_EOL;
815 if (flags & SCF_DO_STCLASS_AND) {
816 /* Check whether it is compatible with what we know already! */
819 if (!(data->start_class->flags & (ANYOF_CLASS | ANYOF_LOCALE))
820 && !ANYOF_BITMAP_TEST(data->start_class, *STRING(scan))
821 && (!(data->start_class->flags & ANYOF_FOLD)
822 || !ANYOF_BITMAP_TEST(data->start_class,
823 PL_fold[*(U8*)STRING(scan)])))
825 ANYOF_CLASS_ZERO(data->start_class);
826 ANYOF_BITMAP_ZERO(data->start_class);
828 ANYOF_BITMAP_SET(data->start_class, *STRING(scan));
829 data->start_class->flags &= ~ANYOF_EOS;
831 else if (flags & SCF_DO_STCLASS_OR) {
832 /* false positive possible if the class is case-folded */
833 ANYOF_BITMAP_SET(data->start_class, *STRING(scan));
834 data->start_class->flags &= ~ANYOF_EOS;
835 cl_and(data->start_class, &and_with);
837 flags &= ~SCF_DO_STCLASS;
839 else if (PL_regkind[(U8)OP(scan)] == EXACT) { /* But OP != EXACT! */
840 I32 l = STR_LEN(scan);
842 /* Search for fixed substrings supports EXACT only. */
843 if (flags & SCF_DO_SUBSTR)
844 scan_commit(pRExC_state, data);
846 unsigned char *s = (unsigned char *)STRING(scan);
847 unsigned char *e = s + l;
856 if (data && (flags & SCF_DO_SUBSTR))
858 if (flags & SCF_DO_STCLASS_AND) {
859 /* Check whether it is compatible with what we know already! */
862 if (!(data->start_class->flags & (ANYOF_CLASS | ANYOF_LOCALE))
863 && !ANYOF_BITMAP_TEST(data->start_class, *STRING(scan))
864 && !ANYOF_BITMAP_TEST(data->start_class,
865 PL_fold[*(U8*)STRING(scan)]))
867 ANYOF_CLASS_ZERO(data->start_class);
868 ANYOF_BITMAP_ZERO(data->start_class);
870 ANYOF_BITMAP_SET(data->start_class, *STRING(scan));
871 data->start_class->flags &= ~ANYOF_EOS;
872 data->start_class->flags |= ANYOF_FOLD;
873 if (OP(scan) == EXACTFL)
874 data->start_class->flags |= ANYOF_LOCALE;
877 else if (flags & SCF_DO_STCLASS_OR) {
878 if (data->start_class->flags & ANYOF_FOLD) {
879 /* false positive possible if the class is case-folded.
880 Assume that the locale settings are the same... */
881 ANYOF_BITMAP_SET(data->start_class, *STRING(scan));
882 data->start_class->flags &= ~ANYOF_EOS;
884 cl_and(data->start_class, &and_with);
886 flags &= ~SCF_DO_STCLASS;
888 else if (strchr((char*)PL_varies,OP(scan))) {
889 I32 mincount, maxcount, minnext, deltanext, fl;
890 I32 f = flags, pos_before = 0;
891 regnode *oscan = scan;
892 struct regnode_charclass_class this_class;
893 struct regnode_charclass_class *oclass = NULL;
895 switch (PL_regkind[(U8)OP(scan)]) {
896 case WHILEM: /* End of (?:...)* . */
897 scan = NEXTOPER(scan);
900 if (flags & (SCF_DO_SUBSTR | SCF_DO_STCLASS)) {
901 next = NEXTOPER(scan);
902 if (OP(next) == EXACT || (flags & SCF_DO_STCLASS)) {
904 maxcount = REG_INFTY;
905 next = regnext(scan);
906 scan = NEXTOPER(scan);
910 if (flags & SCF_DO_SUBSTR)
915 if (flags & SCF_DO_STCLASS) {
917 maxcount = REG_INFTY;
918 next = regnext(scan);
919 scan = NEXTOPER(scan);
922 is_inf = is_inf_internal = 1;
923 scan = regnext(scan);
924 if (flags & SCF_DO_SUBSTR) {
925 scan_commit(pRExC_state, data); /* Cannot extend fixed substrings */
926 data->longest = &(data->longest_float);
928 goto optimize_curly_tail;
930 mincount = ARG1(scan);
931 maxcount = ARG2(scan);
932 next = regnext(scan);
933 if (OP(scan) == CURLYX) {
934 I32 lp = (data ? *(data->last_closep) : 0);
936 scan->flags = ((lp <= U8_MAX) ? lp : U8_MAX);
938 scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
940 if (flags & SCF_DO_SUBSTR) {
941 if (mincount == 0) scan_commit(pRExC_state,data); /* Cannot extend fixed substrings */
942 pos_before = data->pos_min;
946 data->flags &= ~(SF_HAS_PAR|SF_IN_PAR|SF_HAS_EVAL);
948 data->flags |= SF_IS_INF;
950 if (flags & SCF_DO_STCLASS) {
951 cl_init(pRExC_state, &this_class);
952 oclass = data->start_class;
953 data->start_class = &this_class;
954 f |= SCF_DO_STCLASS_AND;
955 f &= ~SCF_DO_STCLASS_OR;
957 /* These are the cases when once a subexpression
958 fails at a particular position, it cannot succeed
959 even after backtracking at the enclosing scope.
961 XXXX what if minimal match and we are at the
962 initial run of {n,m}? */
963 if ((mincount != maxcount - 1) && (maxcount != REG_INFTY))
964 f &= ~SCF_WHILEM_VISITED_POS;
966 /* This will finish on WHILEM, setting scan, or on NULL: */
967 minnext = study_chunk(pRExC_state, &scan, &deltanext, last, data,
969 ? (f & ~SCF_DO_SUBSTR) : f);
971 if (flags & SCF_DO_STCLASS)
972 data->start_class = oclass;
973 if (mincount == 0 || minnext == 0) {
974 if (flags & SCF_DO_STCLASS_OR) {
975 cl_or(pRExC_state, data->start_class, &this_class);
977 else if (flags & SCF_DO_STCLASS_AND) {
978 /* Switch to OR mode: cache the old value of
979 * data->start_class */
980 StructCopy(data->start_class, &and_with,
981 struct regnode_charclass_class);
982 flags &= ~SCF_DO_STCLASS_AND;
983 StructCopy(&this_class, data->start_class,
984 struct regnode_charclass_class);
985 flags |= SCF_DO_STCLASS_OR;
986 data->start_class->flags |= ANYOF_EOS;
988 } else { /* Non-zero len */
989 if (flags & SCF_DO_STCLASS_OR) {
990 cl_or(pRExC_state, data->start_class, &this_class);
991 cl_and(data->start_class, &and_with);
993 else if (flags & SCF_DO_STCLASS_AND)
994 cl_and(data->start_class, &this_class);
995 flags &= ~SCF_DO_STCLASS;
997 if (!scan) /* It was not CURLYX, but CURLY. */
999 if (ckWARN(WARN_REGEXP) && (minnext + deltanext == 0)
1000 && !(data->flags & (SF_HAS_PAR|SF_IN_PAR))
1001 && maxcount <= REG_INFTY/3) /* Complement check for big count */
1004 "Quantifier unexpected on zero-length expression");
1007 min += minnext * mincount;
1008 is_inf_internal |= ((maxcount == REG_INFTY
1009 && (minnext + deltanext) > 0)
1010 || deltanext == I32_MAX);
1011 is_inf |= is_inf_internal;
1012 delta += (minnext + deltanext) * maxcount - minnext * mincount;
1014 /* Try powerful optimization CURLYX => CURLYN. */
1015 if ( OP(oscan) == CURLYX && data
1016 && data->flags & SF_IN_PAR
1017 && !(data->flags & SF_HAS_EVAL)
1018 && !deltanext && minnext == 1 ) {
1019 /* Try to optimize to CURLYN. */
1020 regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS;
1021 regnode *nxt1 = nxt, *nxt2;
1025 if (!strchr((char*)PL_simple,OP(nxt))
1026 && !(PL_regkind[(U8)OP(nxt)] == EXACT
1027 && STR_LEN(nxt) == 1))
1031 if (OP(nxt) != CLOSE)
1033 /* Now we know that nxt2 is the only contents: */
1034 oscan->flags = ARG(nxt);
1036 OP(nxt1) = NOTHING; /* was OPEN. */
1038 OP(nxt1 + 1) = OPTIMIZED; /* was count. */
1039 NEXT_OFF(nxt1+ 1) = 0; /* just for consistancy. */
1040 NEXT_OFF(nxt2) = 0; /* just for consistancy with CURLY. */
1041 OP(nxt) = OPTIMIZED; /* was CLOSE. */
1042 OP(nxt + 1) = OPTIMIZED; /* was count. */
1043 NEXT_OFF(nxt+ 1) = 0; /* just for consistancy. */
1048 /* Try optimization CURLYX => CURLYM. */
1049 if ( OP(oscan) == CURLYX && data
1050 && !(data->flags & SF_HAS_PAR)
1051 && !(data->flags & SF_HAS_EVAL)
1053 /* XXXX How to optimize if data == 0? */
1054 /* Optimize to a simpler form. */
1055 regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN */
1059 while ( (nxt2 = regnext(nxt)) /* skip over embedded stuff*/
1060 && (OP(nxt2) != WHILEM))
1062 OP(nxt2) = SUCCEED; /* Whas WHILEM */
1063 /* Need to optimize away parenths. */
1064 if (data->flags & SF_IN_PAR) {
1065 /* Set the parenth number. */
1066 regnode *nxt1 = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN*/
1068 if (OP(nxt) != CLOSE)
1069 FAIL("Panic opt close");
1070 oscan->flags = ARG(nxt);
1071 OP(nxt1) = OPTIMIZED; /* was OPEN. */
1072 OP(nxt) = OPTIMIZED; /* was CLOSE. */
1074 OP(nxt1 + 1) = OPTIMIZED; /* was count. */
1075 OP(nxt + 1) = OPTIMIZED; /* was count. */
1076 NEXT_OFF(nxt1 + 1) = 0; /* just for consistancy. */
1077 NEXT_OFF(nxt + 1) = 0; /* just for consistancy. */
1080 while ( nxt1 && (OP(nxt1) != WHILEM)) {
1081 regnode *nnxt = regnext(nxt1);
1084 if (reg_off_by_arg[OP(nxt1)])
1085 ARG_SET(nxt1, nxt2 - nxt1);
1086 else if (nxt2 - nxt1 < U16_MAX)
1087 NEXT_OFF(nxt1) = nxt2 - nxt1;
1089 OP(nxt) = NOTHING; /* Cannot beautify */
1094 /* Optimize again: */
1095 study_chunk(pRExC_state, &nxt1, &deltanext, nxt,
1101 else if ((OP(oscan) == CURLYX)
1102 && (flags & SCF_WHILEM_VISITED_POS)
1103 /* See the comment on a similar expression above.
1104 However, this time it not a subexpression
1105 we care about, but the expression itself. */
1106 && (maxcount == REG_INFTY)
1107 && data && ++data->whilem_c < 16) {
1108 /* This stays as CURLYX, we can put the count/of pair. */
1109 /* Find WHILEM (as in regexec.c) */
1110 regnode *nxt = oscan + NEXT_OFF(oscan);
1112 if (OP(PREVOPER(nxt)) == NOTHING) /* LONGJMP */
1114 PREVOPER(nxt)->flags = data->whilem_c
1115 | (RExC_whilem_seen << 4); /* On WHILEM */
1117 if (data && fl & (SF_HAS_PAR|SF_IN_PAR))
1119 if (flags & SCF_DO_SUBSTR) {
1120 SV *last_str = Nullsv;
1121 int counted = mincount != 0;
1123 if (data->last_end > 0 && mincount != 0) { /* Ends with a string. */
1124 I32 b = pos_before >= data->last_start_min
1125 ? pos_before : data->last_start_min;
1127 char *s = SvPV(data->last_found, l);
1128 I32 old = b - data->last_start_min;
1131 old = utf8_hop((U8*)s, old) - (U8*)s;
1134 /* Get the added string: */
1135 last_str = newSVpvn(s + old, l);
1136 if (deltanext == 0 && pos_before == b) {
1137 /* What was added is a constant string */
1139 SvGROW(last_str, (mincount * l) + 1);
1140 repeatcpy(SvPVX(last_str) + l,
1141 SvPVX(last_str), l, mincount - 1);
1142 SvCUR(last_str) *= mincount;
1143 /* Add additional parts. */
1144 SvCUR_set(data->last_found,
1145 SvCUR(data->last_found) - l);
1146 sv_catsv(data->last_found, last_str);
1147 data->last_end += l * (mincount - 1);
1150 /* start offset must point into the last copy */
1151 data->last_start_min += minnext * (mincount - 1);
1152 data->last_start_max += is_inf ? 0 : (maxcount - 1)
1153 * (minnext + data->pos_delta);
1156 /* It is counted once already... */
1157 data->pos_min += minnext * (mincount - counted);
1158 data->pos_delta += - counted * deltanext +
1159 (minnext + deltanext) * maxcount - minnext * mincount;
1160 if (mincount != maxcount) {
1161 /* Cannot extend fixed substrings found inside
1163 scan_commit(pRExC_state,data);
1164 if (mincount && last_str) {
1165 sv_setsv(data->last_found, last_str);
1166 data->last_end = data->pos_min;
1167 data->last_start_min =
1168 data->pos_min - CHR_SVLEN(last_str);
1169 data->last_start_max = is_inf
1171 : data->pos_min + data->pos_delta
1172 - CHR_SVLEN(last_str);
1174 data->longest = &(data->longest_float);
1176 SvREFCNT_dec(last_str);
1178 if (data && (fl & SF_HAS_EVAL))
1179 data->flags |= SF_HAS_EVAL;
1180 optimize_curly_tail:
1181 if (OP(oscan) != CURLYX) {
1182 while (PL_regkind[(U8)OP(next = regnext(oscan))] == NOTHING
1184 NEXT_OFF(oscan) += NEXT_OFF(next);
1187 default: /* REF and CLUMP only? */
1188 if (flags & SCF_DO_SUBSTR) {
1189 scan_commit(pRExC_state,data); /* Cannot expect anything... */
1190 data->longest = &(data->longest_float);
1192 is_inf = is_inf_internal = 1;
1193 if (flags & SCF_DO_STCLASS_OR)
1194 cl_anything(pRExC_state, data->start_class);
1195 flags &= ~SCF_DO_STCLASS;
1199 else if (strchr((char*)PL_simple,OP(scan)) || PL_regkind[(U8)OP(scan)] == ANYUTF8) {
1202 if (flags & SCF_DO_SUBSTR) {
1203 scan_commit(pRExC_state,data);
1207 if (flags & SCF_DO_STCLASS) {
1208 data->start_class->flags &= ~ANYOF_EOS; /* No match on empty */
1210 /* Some of the logic below assumes that switching
1211 locale on will only add false positives. */
1212 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_UTF8) {
1598 PL_reg_flags |= RF_utf8;
1603 RExC_precomp = savepvn(exp, xend - exp);
1604 DEBUG_r(if (!PL_colorset) reginitcolors());
1605 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sCompiling REx%s `%s%*s%s'\n",
1606 PL_colors[4],PL_colors[5],PL_colors[0],
1607 (int)(xend - exp), RExC_precomp, PL_colors[1]));
1608 RExC_flags16 = pm->op_pmflags;
1612 RExC_seen_zerolen = *exp == '^' ? -1 : 0;
1613 RExC_seen_evals = 0;
1616 /* First pass: determine size, legality. */
1622 RExC_emit = &PL_regdummy;
1623 RExC_whilem_seen = 0;
1624 #if 0 /* REGC() is (currently) a NOP at the first pass.
1625 * Clever compilers notice this and complain. --jhi */
1626 REGC((U8)REG_MAGIC, (char*)RExC_emit);
1628 if (reg(pRExC_state, 0, &flags) == NULL) {
1629 Safefree(RExC_precomp);
1630 RExC_precomp = Nullch;
1633 DEBUG_r(PerlIO_printf(Perl_debug_log, "size %"IVdf" ", (IV)RExC_size));
1635 /* Small enough for pointer-storage convention?
1636 If extralen==0, this means that we will not need long jumps. */
1637 if (RExC_size >= 0x10000L && RExC_extralen)
1638 RExC_size += RExC_extralen;
1641 if (RExC_whilem_seen > 15)
1642 RExC_whilem_seen = 15;
1644 /* Allocate space and initialize. */
1645 Newc(1001, r, sizeof(regexp) + (unsigned)RExC_size * sizeof(regnode),
1648 FAIL("Regexp out of space");
1651 /* avoid reading uninitialized memory in DEBUGGING code in study_chunk() */
1652 Zero(r, sizeof(regexp) + (unsigned)RExC_size * sizeof(regnode), char);
1655 r->prelen = xend - exp;
1656 r->precomp = RExC_precomp;
1658 r->reganch = pm->op_pmflags & PMf_COMPILETIME;
1659 r->nparens = RExC_npar - 1; /* set early to validate backrefs */
1661 r->substrs = 0; /* Useful during FAIL. */
1662 r->startp = 0; /* Useful during FAIL. */
1663 r->endp = 0; /* Useful during FAIL. */
1667 /* Second pass: emit code. */
1672 RExC_emit = r->program;
1673 /* Store the count of eval-groups for security checks: */
1674 RExC_emit->next_off = ((RExC_seen_evals > U16_MAX) ? U16_MAX : RExC_seen_evals);
1675 REGC((U8)REG_MAGIC, (char*) RExC_emit++);
1677 if (reg(pRExC_state, 0, &flags) == NULL)
1680 /* Dig out information for optimizations. */
1681 r->reganch = pm->op_pmflags & PMf_COMPILETIME; /* Again? */
1682 pm->op_pmflags = RExC_flags16;
1684 r->reganch |= ROPT_UTF8;
1685 r->regstclass = NULL;
1686 if (RExC_naughty >= 10) /* Probably an expensive pattern. */
1687 r->reganch |= ROPT_NAUGHTY;
1688 scan = r->program + 1; /* First BRANCH. */
1690 /* XXXX To minimize changes to RE engine we always allocate
1691 3-units-long substrs field. */
1692 Newz(1004, r->substrs, 1, struct reg_substr_data);
1694 StructCopy(&zero_scan_data, &data, scan_data_t);
1695 /* XXXX Should not we check for something else? Usually it is OPEN1... */
1696 if (OP(scan) != BRANCH) { /* Only one top-level choice. */
1698 STRLEN longest_float_length, longest_fixed_length;
1699 struct regnode_charclass_class ch_class;
1704 /* Skip introductions and multiplicators >= 1. */
1705 while ((OP(first) == OPEN && (sawopen = 1)) ||
1706 /* An OR of *one* alternative - should not happen now. */
1707 (OP(first) == BRANCH && OP(regnext(first)) != BRANCH) ||
1708 (OP(first) == PLUS) ||
1709 (OP(first) == MINMOD) ||
1710 /* An {n,m} with n>0 */
1711 (PL_regkind[(U8)OP(first)] == CURLY && ARG1(first) > 0) ) {
1712 if (OP(first) == PLUS)
1715 first += regarglen[(U8)OP(first)];
1716 first = NEXTOPER(first);
1719 /* Starting-point info. */
1721 if (PL_regkind[(U8)OP(first)] == EXACT) {
1722 if (OP(first) == EXACT); /* Empty, get anchored substr later. */
1723 else if ((OP(first) == EXACTF || OP(first) == EXACTFL)
1725 r->regstclass = first;
1727 else if (strchr((char*)PL_simple,OP(first)))
1728 r->regstclass = first;
1729 else if (PL_regkind[(U8)OP(first)] == BOUND ||
1730 PL_regkind[(U8)OP(first)] == NBOUND)
1731 r->regstclass = first;
1732 else if (PL_regkind[(U8)OP(first)] == BOL) {
1733 r->reganch |= (OP(first) == MBOL
1735 : (OP(first) == SBOL
1738 first = NEXTOPER(first);
1741 else if (OP(first) == GPOS) {
1742 r->reganch |= ROPT_ANCH_GPOS;
1743 first = NEXTOPER(first);
1746 else if ((OP(first) == STAR &&
1747 PL_regkind[(U8)OP(NEXTOPER(first))] == REG_ANY) &&
1748 !(r->reganch & ROPT_ANCH) )
1750 /* turn .* into ^.* with an implied $*=1 */
1751 int type = OP(NEXTOPER(first));
1753 if (type == REG_ANY || type == ANYUTF8)
1754 type = ROPT_ANCH_MBOL;
1756 type = ROPT_ANCH_SBOL;
1758 r->reganch |= type | ROPT_IMPLICIT;
1759 first = NEXTOPER(first);
1762 if (sawplus && (!sawopen || !RExC_sawback)
1763 && !(RExC_seen & REG_SEEN_EVAL)) /* May examine pos and $& */
1764 /* x+ must match at the 1st pos of run of x's */
1765 r->reganch |= ROPT_SKIP;
1767 /* Scan is after the zeroth branch, first is atomic matcher. */
1768 DEBUG_r(PerlIO_printf(Perl_debug_log, "first at %"IVdf"\n",
1769 (IV)(first - scan + 1)));
1771 * If there's something expensive in the r.e., find the
1772 * longest literal string that must appear and make it the
1773 * regmust. Resolve ties in favor of later strings, since
1774 * the regstart check works with the beginning of the r.e.
1775 * and avoiding duplication strengthens checking. Not a
1776 * strong reason, but sufficient in the absence of others.
1777 * [Now we resolve ties in favor of the earlier string if
1778 * it happens that c_offset_min has been invalidated, since the
1779 * earlier string may buy us something the later one won't.]
1783 data.longest_fixed = newSVpvn("",0);
1784 data.longest_float = newSVpvn("",0);
1785 data.last_found = newSVpvn("",0);
1786 data.longest = &(data.longest_fixed);
1788 if (!r->regstclass) {
1789 cl_init(pRExC_state, &ch_class);
1790 data.start_class = &ch_class;
1791 stclass_flag = SCF_DO_STCLASS_AND;
1792 } else /* XXXX Check for BOUND? */
1794 data.last_closep = &last_close;
1796 minlen = study_chunk(pRExC_state, &first, &fake, scan + RExC_size, /* Up to end */
1797 &data, SCF_DO_SUBSTR | SCF_WHILEM_VISITED_POS | stclass_flag);
1798 if ( RExC_npar == 1 && data.longest == &(data.longest_fixed)
1799 && data.last_start_min == 0 && data.last_end > 0
1800 && !RExC_seen_zerolen
1801 && (!(RExC_seen & REG_SEEN_GPOS) || (r->reganch & ROPT_ANCH_GPOS)))
1802 r->reganch |= ROPT_CHECK_ALL;
1803 scan_commit(pRExC_state, &data);
1804 SvREFCNT_dec(data.last_found);
1806 longest_float_length = CHR_SVLEN(data.longest_float);
1807 if (longest_float_length
1808 || (data.flags & SF_FL_BEFORE_EOL
1809 && (!(data.flags & SF_FL_BEFORE_MEOL)
1810 || (RExC_flags16 & PMf_MULTILINE)))) {
1813 if (SvCUR(data.longest_fixed) /* ok to leave SvCUR */
1814 && data.offset_fixed == data.offset_float_min
1815 && SvCUR(data.longest_fixed) == SvCUR(data.longest_float))
1816 goto remove_float; /* As in (a)+. */
1818 r->float_substr = data.longest_float;
1819 r->float_min_offset = data.offset_float_min;
1820 r->float_max_offset = data.offset_float_max;
1821 t = (data.flags & SF_FL_BEFORE_EOL /* Can't have SEOL and MULTI */
1822 && (!(data.flags & SF_FL_BEFORE_MEOL)
1823 || (RExC_flags16 & PMf_MULTILINE)));
1824 fbm_compile(r->float_substr, t ? FBMcf_TAIL : 0);
1828 r->float_substr = Nullsv;
1829 SvREFCNT_dec(data.longest_float);
1830 longest_float_length = 0;
1833 longest_fixed_length = CHR_SVLEN(data.longest_fixed);
1834 if (longest_fixed_length
1835 || (data.flags & SF_FIX_BEFORE_EOL /* Cannot have SEOL and MULTI */
1836 && (!(data.flags & SF_FIX_BEFORE_MEOL)
1837 || (RExC_flags16 & PMf_MULTILINE)))) {
1840 r->anchored_substr = data.longest_fixed;
1841 r->anchored_offset = data.offset_fixed;
1842 t = (data.flags & SF_FIX_BEFORE_EOL /* Can't have SEOL and MULTI */
1843 && (!(data.flags & SF_FIX_BEFORE_MEOL)
1844 || (RExC_flags16 & PMf_MULTILINE)));
1845 fbm_compile(r->anchored_substr, t ? FBMcf_TAIL : 0);
1848 r->anchored_substr = Nullsv;
1849 SvREFCNT_dec(data.longest_fixed);
1850 longest_fixed_length = 0;
1853 && (OP(r->regstclass) == REG_ANY || OP(r->regstclass) == ANYUTF8
1854 || OP(r->regstclass) == SANYUTF8 || OP(r->regstclass) == SANY))
1855 r->regstclass = NULL;
1856 if ((!r->anchored_substr || r->anchored_offset) && stclass_flag
1857 && !(data.start_class->flags & ANYOF_EOS)
1858 && !cl_is_anything(data.start_class)) {
1860 I32 n = add_data(pRExC_state, 1, "f");
1862 New(1006, RExC_rx->data->data[n], 1,
1863 struct regnode_charclass_class);
1864 StructCopy(data.start_class,
1865 (struct regnode_charclass_class*)RExC_rx->data->data[n],
1866 struct regnode_charclass_class);
1867 r->regstclass = (regnode*)RExC_rx->data->data[n];
1868 r->reganch &= ~ROPT_SKIP; /* Used in find_byclass(). */
1869 DEBUG_r((sv = sv_newmortal(),
1870 regprop(sv, (regnode*)data.start_class),
1871 PerlIO_printf(Perl_debug_log, "synthetic stclass `%s'.\n",
1875 /* A temporary algorithm prefers floated substr to fixed one to dig more info. */
1876 if (longest_fixed_length > longest_float_length) {
1877 r->check_substr = r->anchored_substr;
1878 r->check_offset_min = r->check_offset_max = r->anchored_offset;
1879 if (r->reganch & ROPT_ANCH_SINGLE)
1880 r->reganch |= ROPT_NOSCAN;
1883 r->check_substr = r->float_substr;
1884 r->check_offset_min = data.offset_float_min;
1885 r->check_offset_max = data.offset_float_max;
1887 /* XXXX Currently intuiting is not compatible with ANCH_GPOS.
1888 This should be changed ASAP! */
1889 if (r->check_substr && !(r->reganch & ROPT_ANCH_GPOS)) {
1890 r->reganch |= RE_USE_INTUIT;
1891 if (SvTAIL(r->check_substr))
1892 r->reganch |= RE_INTUIT_TAIL;
1896 /* Several toplevels. Best we can is to set minlen. */
1898 struct regnode_charclass_class ch_class;
1901 DEBUG_r(PerlIO_printf(Perl_debug_log, "\n"));
1902 scan = r->program + 1;
1903 cl_init(pRExC_state, &ch_class);
1904 data.start_class = &ch_class;
1905 data.last_closep = &last_close;
1906 minlen = study_chunk(pRExC_state, &scan, &fake, scan + RExC_size, &data, SCF_DO_STCLASS_AND|SCF_WHILEM_VISITED_POS);
1907 r->check_substr = r->anchored_substr = r->float_substr = Nullsv;
1908 if (!(data.start_class->flags & ANYOF_EOS)
1909 && !cl_is_anything(data.start_class)) {
1911 I32 n = add_data(pRExC_state, 1, "f");
1913 New(1006, RExC_rx->data->data[n], 1,
1914 struct regnode_charclass_class);
1915 StructCopy(data.start_class,
1916 (struct regnode_charclass_class*)RExC_rx->data->data[n],
1917 struct regnode_charclass_class);
1918 r->regstclass = (regnode*)RExC_rx->data->data[n];
1919 r->reganch &= ~ROPT_SKIP; /* Used in find_byclass(). */
1920 DEBUG_r((sv = sv_newmortal(),
1921 regprop(sv, (regnode*)data.start_class),
1922 PerlIO_printf(Perl_debug_log, "synthetic stclass `%s'.\n",
1928 if (RExC_seen & REG_SEEN_GPOS)
1929 r->reganch |= ROPT_GPOS_SEEN;
1930 if (RExC_seen & REG_SEEN_LOOKBEHIND)
1931 r->reganch |= ROPT_LOOKBEHIND_SEEN;
1932 if (RExC_seen & REG_SEEN_EVAL)
1933 r->reganch |= ROPT_EVAL_SEEN;
1934 Newz(1002, r->startp, RExC_npar, I32);
1935 Newz(1002, r->endp, RExC_npar, I32);
1936 PL_regdata = r->data; /* for regprop() ANYOFUTF8 */
1937 DEBUG_r(regdump(r));
1942 - reg - regular expression, i.e. main body or parenthesized thing
1944 * Caller must absorb opening parenthesis.
1946 * Combining parenthesis handling with the base level of regular expression
1947 * is a trifle forced, but the need to tie the tails of the branches to what
1948 * follows makes it hard to avoid.
1951 S_reg(pTHX_ RExC_state_t *pRExC_state, I32 paren, I32 *flagp)
1952 /* paren: Parenthesized? 0=top, 1=(, inside: changed to letter. */
1954 register regnode *ret; /* Will be the head of the group. */
1955 register regnode *br;
1956 register regnode *lastbr;
1957 register regnode *ender = 0;
1958 register I32 parno = 0;
1959 I32 flags, oregflags = RExC_flags16, have_branch = 0, open = 0;
1960 char *oregcomp_parse = RExC_parse;
1963 *flagp = 0; /* Tentatively. */
1965 /* Make an OPEN node, if parenthesized. */
1967 if (*RExC_parse == '?') {
1968 U16 posflags = 0, negflags = 0;
1969 U16 *flagsp = &posflags;
1971 char *seqstart = RExC_parse;
1974 paren = *RExC_parse++;
1975 ret = NULL; /* For look-ahead/behind. */
1978 RExC_seen |= REG_SEEN_LOOKBEHIND;
1979 if (*RExC_parse == '!')
1981 if (*RExC_parse != '=' && *RExC_parse != '!')
1986 RExC_seen_zerolen++;
1992 vFAIL2("Sequence (?%c...) not implemented", (int)paren);
1995 while (*RExC_parse && *RExC_parse != ')')
1997 if (*RExC_parse != ')')
1998 FAIL("Sequence (?#... not terminated");
1999 nextchar(pRExC_state);
2004 vWARN(RExC_parse, "(?p{}) is deprecated - use (??{})");
2008 paren = *RExC_parse++;
2012 I32 count = 1, n = 0;
2014 char *s = RExC_parse;
2016 OP_4tree *sop, *rop;
2018 RExC_seen_zerolen++;
2019 RExC_seen |= REG_SEEN_EVAL;
2020 while (count && (c = *RExC_parse)) {
2021 if (c == '\\' && RExC_parse[1])
2029 if (*RExC_parse != ')')
2032 vFAIL("Sequence (?{...}) not terminated or not {}-balanced");
2037 if (RExC_parse - 1 - s)
2038 sv = newSVpvn(s, RExC_parse - 1 - s);
2040 sv = newSVpvn("", 0);
2043 Perl_save_re_context(aTHX);
2044 rop = sv_compile_2op(sv, &sop, "re", &av);
2047 n = add_data(pRExC_state, 3, "nop");
2048 RExC_rx->data->data[n] = (void*)rop;
2049 RExC_rx->data->data[n+1] = (void*)sop;
2050 RExC_rx->data->data[n+2] = (void*)av;
2053 else { /* First pass */
2054 if (PL_reginterp_cnt < ++RExC_seen_evals
2055 && PL_curcop != &PL_compiling)
2056 /* No compiled RE interpolated, has runtime
2057 components ===> unsafe. */
2058 FAIL("Eval-group not allowed at runtime, use re 'eval'");
2060 FAIL("Eval-group in insecure regular expression");
2063 nextchar(pRExC_state);
2065 ret = reg_node(pRExC_state, LOGICAL);
2068 regtail(pRExC_state, ret, reganode(pRExC_state, EVAL, n));
2071 return reganode(pRExC_state, EVAL, n);
2075 if (RExC_parse[0] == '?') {
2076 if (RExC_parse[1] == '=' || RExC_parse[1] == '!'
2077 || RExC_parse[1] == '<'
2078 || RExC_parse[1] == '{') { /* Lookahead or eval. */
2081 ret = reg_node(pRExC_state, LOGICAL);
2084 regtail(pRExC_state, ret, reg(pRExC_state, 1, &flag));
2088 else if (RExC_parse[0] >= '1' && RExC_parse[0] <= '9' ) {
2089 parno = atoi(RExC_parse++);
2091 while (isDIGIT(*RExC_parse))
2093 ret = reganode(pRExC_state, GROUPP, parno);
2094 if ((c = *nextchar(pRExC_state)) != ')')
2095 vFAIL("Switch condition not recognized");
2097 regtail(pRExC_state, ret, reganode(pRExC_state, IFTHEN, 0));
2098 br = regbranch(pRExC_state, &flags, 1);
2100 br = reganode(pRExC_state, LONGJMP, 0);
2102 regtail(pRExC_state, br, reganode(pRExC_state, LONGJMP, 0));
2103 c = *nextchar(pRExC_state);
2107 lastbr = reganode(pRExC_state, IFTHEN, 0); /* Fake one for optimizer. */
2108 regbranch(pRExC_state, &flags, 1);
2109 regtail(pRExC_state, ret, lastbr);
2112 c = *nextchar(pRExC_state);
2117 vFAIL("Switch (?(condition)... contains too many branches");
2118 ender = reg_node(pRExC_state, TAIL);
2119 regtail(pRExC_state, br, ender);
2121 regtail(pRExC_state, lastbr, ender);
2122 regtail(pRExC_state, NEXTOPER(NEXTOPER(lastbr)), ender);
2125 regtail(pRExC_state, ret, ender);
2129 vFAIL2("Unknown switch condition (?(%.2s", RExC_parse);
2133 RExC_parse--; /* for vFAIL to print correctly */
2134 vFAIL("Sequence (? incomplete");
2139 while (*RExC_parse && strchr("iogcmsx", *RExC_parse)) {
2140 if (*RExC_parse != 'o')
2141 pmflag(flagsp, *RExC_parse);
2144 if (*RExC_parse == '-') {
2149 RExC_flags16 |= posflags;
2150 RExC_flags16 &= ~negflags;
2151 if (*RExC_parse == ':') {
2157 if (*RExC_parse != ')') {
2159 vFAIL3("Sequence (%.*s...) not recognized", RExC_parse-seqstart, seqstart);
2161 nextchar(pRExC_state);
2169 ret = reganode(pRExC_state, OPEN, parno);
2176 /* Pick up the branches, linking them together. */
2177 br = regbranch(pRExC_state, &flags, 1);
2180 if (*RExC_parse == '|') {
2181 if (!SIZE_ONLY && RExC_extralen) {
2182 reginsert(pRExC_state, BRANCHJ, br);
2185 reginsert(pRExC_state, BRANCH, br);
2188 RExC_extralen += 1; /* For BRANCHJ-BRANCH. */
2190 else if (paren == ':') {
2191 *flagp |= flags&SIMPLE;
2193 if (open) { /* Starts with OPEN. */
2194 regtail(pRExC_state, ret, br); /* OPEN -> first. */
2196 else if (paren != '?') /* Not Conditional */
2200 *flagp |= flags&SPSTART;
2202 while (*RExC_parse == '|') {
2203 if (!SIZE_ONLY && RExC_extralen) {
2204 ender = reganode(pRExC_state, LONGJMP,0);
2205 regtail(pRExC_state, NEXTOPER(NEXTOPER(lastbr)), ender); /* Append to the previous. */
2208 RExC_extralen += 2; /* Account for LONGJMP. */
2209 nextchar(pRExC_state);
2210 br = regbranch(pRExC_state, &flags, 0);
2213 regtail(pRExC_state, lastbr, br); /* BRANCH -> BRANCH. */
2217 *flagp |= flags&SPSTART;
2220 if (have_branch || paren != ':') {
2221 /* Make a closing node, and hook it on the end. */
2224 ender = reg_node(pRExC_state, TAIL);
2227 ender = reganode(pRExC_state, CLOSE, parno);
2233 *flagp &= ~HASWIDTH;
2236 ender = reg_node(pRExC_state, SUCCEED);
2239 ender = reg_node(pRExC_state, END);
2242 regtail(pRExC_state, lastbr, ender);
2245 /* Hook the tails of the branches to the closing node. */
2246 for (br = ret; br != NULL; br = regnext(br)) {
2247 regoptail(pRExC_state, br, ender);
2254 static char parens[] = "=!<,>";
2256 if (paren && (p = strchr(parens, paren))) {
2257 int node = ((p - parens) % 2) ? UNLESSM : IFMATCH;
2258 int flag = (p - parens) > 1;
2261 node = SUSPEND, flag = 0;
2262 reginsert(pRExC_state, node,ret);
2264 regtail(pRExC_state, ret, reg_node(pRExC_state, TAIL));
2268 /* Check for proper termination. */
2270 RExC_flags16 = oregflags;
2271 if (RExC_parse >= RExC_end || *nextchar(pRExC_state) != ')') {
2272 RExC_parse = oregcomp_parse;
2273 vFAIL("Unmatched (");
2276 else if (!paren && RExC_parse < RExC_end) {
2277 if (*RExC_parse == ')') {
2279 vFAIL("Unmatched )");
2282 FAIL("Junk on end of regexp"); /* "Can't happen". */
2290 - regbranch - one alternative of an | operator
2292 * Implements the concatenation operator.
2295 S_regbranch(pTHX_ RExC_state_t *pRExC_state, I32 *flagp, I32 first)
2297 register regnode *ret;
2298 register regnode *chain = NULL;
2299 register regnode *latest;
2300 I32 flags = 0, c = 0;
2305 if (!SIZE_ONLY && RExC_extralen)
2306 ret = reganode(pRExC_state, BRANCHJ,0);
2308 ret = reg_node(pRExC_state, BRANCH);
2311 if (!first && SIZE_ONLY)
2312 RExC_extralen += 1; /* BRANCHJ */
2314 *flagp = WORST; /* Tentatively. */
2317 nextchar(pRExC_state);
2318 while (RExC_parse < RExC_end && *RExC_parse != '|' && *RExC_parse != ')') {
2320 latest = regpiece(pRExC_state, &flags);
2321 if (latest == NULL) {
2322 if (flags & TRYAGAIN)
2326 else if (ret == NULL)
2328 *flagp |= flags&HASWIDTH;
2329 if (chain == NULL) /* First piece. */
2330 *flagp |= flags&SPSTART;
2333 regtail(pRExC_state, chain, latest);
2338 if (chain == NULL) { /* Loop ran zero times. */
2339 chain = reg_node(pRExC_state, NOTHING);
2344 *flagp |= flags&SIMPLE;
2351 - regpiece - something followed by possible [*+?]
2353 * Note that the branching code sequences used for ? and the general cases
2354 * of * and + are somewhat optimized: they use the same NOTHING node as
2355 * both the endmarker for their branch list and the body of the last branch.
2356 * It might seem that this node could be dispensed with entirely, but the
2357 * endmarker role is not redundant.
2360 S_regpiece(pTHX_ RExC_state_t *pRExC_state, I32 *flagp)
2362 register regnode *ret;
2364 register char *next;
2366 char *origparse = RExC_parse;
2369 I32 max = REG_INFTY;
2371 ret = regatom(pRExC_state, &flags);
2373 if (flags & TRYAGAIN)
2380 if (op == '{' && regcurly(RExC_parse)) {
2381 next = RExC_parse + 1;
2383 while (isDIGIT(*next) || *next == ',') {
2392 if (*next == '}') { /* got one */
2396 min = atoi(RExC_parse);
2400 maxpos = RExC_parse;
2402 if (!max && *maxpos != '0')
2403 max = REG_INFTY; /* meaning "infinity" */
2404 else if (max >= REG_INFTY)
2405 vFAIL2("Quantifier in {,} bigger than %d", REG_INFTY - 1);
2407 nextchar(pRExC_state);
2410 if ((flags&SIMPLE)) {
2411 RExC_naughty += 2 + RExC_naughty / 2;
2412 reginsert(pRExC_state, CURLY, ret);
2415 regnode *w = reg_node(pRExC_state, WHILEM);
2418 regtail(pRExC_state, ret, w);
2419 if (!SIZE_ONLY && RExC_extralen) {
2420 reginsert(pRExC_state, LONGJMP,ret);
2421 reginsert(pRExC_state, NOTHING,ret);
2422 NEXT_OFF(ret) = 3; /* Go over LONGJMP. */
2424 reginsert(pRExC_state, CURLYX,ret);
2425 if (!SIZE_ONLY && RExC_extralen)
2426 NEXT_OFF(ret) = 3; /* Go over NOTHING to LONGJMP. */
2427 regtail(pRExC_state, ret, reg_node(pRExC_state, NOTHING));
2429 RExC_whilem_seen++, RExC_extralen += 3;
2430 RExC_naughty += 4 + RExC_naughty; /* compound interest */
2438 if (max && max < min)
2439 vFAIL("Can't do {n,m} with n > m");
2454 #if 0 /* Now runtime fix should be reliable. */
2456 /* if this is reinstated, don't forget to put this back into perldiag:
2458 =item Regexp *+ operand could be empty at {#} in regex m/%s/
2460 (F) The part of the regexp subject to either the * or + quantifier
2461 could match an empty string. The {#} shows in the regular
2462 expression about where the problem was discovered.
2466 if (!(flags&HASWIDTH) && op != '?')
2467 vFAIL("Regexp *+ operand could be empty");
2470 nextchar(pRExC_state);
2472 *flagp = (op != '+') ? (WORST|SPSTART|HASWIDTH) : (WORST|HASWIDTH);
2474 if (op == '*' && (flags&SIMPLE)) {
2475 reginsert(pRExC_state, STAR, ret);
2479 else if (op == '*') {
2483 else if (op == '+' && (flags&SIMPLE)) {
2484 reginsert(pRExC_state, PLUS, ret);
2488 else if (op == '+') {
2492 else if (op == '?') {
2497 if (ckWARN(WARN_REGEXP) && !SIZE_ONLY && !(flags&HASWIDTH) && max > REG_INFTY/3) {
2499 "%.*s matches null string many times",
2500 RExC_parse - origparse,
2504 if (*RExC_parse == '?') {
2505 nextchar(pRExC_state);
2506 reginsert(pRExC_state, MINMOD, ret);
2507 regtail(pRExC_state, ret, ret + NODE_STEP_REGNODE);
2509 if (ISMULT2(RExC_parse)) {
2511 vFAIL("Nested quantifiers");
2518 - regatom - the lowest level
2520 * Optimization: gobbles an entire sequence of ordinary characters so that
2521 * it can turn them into a single node, which is smaller to store and
2522 * faster to run. Backslashed characters are exceptions, each becoming a
2523 * separate node; the code is simpler that way and it's not worth fixing.
2525 * [Yes, it is worth fixing, some scripts can run twice the speed.] */
2527 S_regatom(pTHX_ RExC_state_t *pRExC_state, I32 *flagp)
2529 register regnode *ret = 0;
2532 *flagp = WORST; /* Tentatively. */
2535 switch (*RExC_parse) {
2537 RExC_seen_zerolen++;
2538 nextchar(pRExC_state);
2539 if (RExC_flags16 & PMf_MULTILINE)
2540 ret = reg_node(pRExC_state, MBOL);
2541 else if (RExC_flags16 & PMf_SINGLELINE)
2542 ret = reg_node(pRExC_state, SBOL);
2544 ret = reg_node(pRExC_state, BOL);
2547 nextchar(pRExC_state);
2549 RExC_seen_zerolen++;
2550 if (RExC_flags16 & PMf_MULTILINE)
2551 ret = reg_node(pRExC_state, MEOL);
2552 else if (RExC_flags16 & PMf_SINGLELINE)
2553 ret = reg_node(pRExC_state, SEOL);
2555 ret = reg_node(pRExC_state, EOL);
2558 nextchar(pRExC_state);
2560 if (RExC_flags16 & PMf_SINGLELINE)
2561 ret = reg_node(pRExC_state, SANYUTF8);
2563 ret = reg_node(pRExC_state, ANYUTF8);
2567 if (RExC_flags16 & PMf_SINGLELINE)
2568 ret = reg_node(pRExC_state, SANY);
2570 ret = reg_node(pRExC_state, REG_ANY);
2571 *flagp |= HASWIDTH|SIMPLE;
2577 char *oregcomp_parse = ++RExC_parse;
2578 ret = (UTF ? regclassutf8(pRExC_state) : regclass(pRExC_state));
2579 if (*RExC_parse != ']') {
2580 RExC_parse = oregcomp_parse;
2581 vFAIL("Unmatched [");
2583 nextchar(pRExC_state);
2584 *flagp |= HASWIDTH|SIMPLE;
2588 nextchar(pRExC_state);
2589 ret = reg(pRExC_state, 1, &flags);
2591 if (flags & TRYAGAIN) {
2592 if (RExC_parse == RExC_end) {
2593 /* Make parent create an empty node if needed. */
2601 *flagp |= flags&(HASWIDTH|SPSTART|SIMPLE);
2605 if (flags & TRYAGAIN) {
2609 vFAIL("Internal urp");
2610 /* Supposed to be caught earlier. */
2613 if (!regcurly(RExC_parse)) {
2622 vFAIL("Quantifier follows nothing");
2625 switch (*++RExC_parse) {
2627 RExC_seen_zerolen++;
2628 ret = reg_node(pRExC_state, SBOL);
2630 nextchar(pRExC_state);
2633 ret = reg_node(pRExC_state, GPOS);
2634 RExC_seen |= REG_SEEN_GPOS;
2636 nextchar(pRExC_state);
2639 ret = reg_node(pRExC_state, SEOL);
2641 nextchar(pRExC_state);
2644 ret = reg_node(pRExC_state, EOS);
2646 RExC_seen_zerolen++; /* Do not optimize RE away */
2647 nextchar(pRExC_state);
2650 ret = reg_node(pRExC_state, SANY);
2651 *flagp |= HASWIDTH|SIMPLE;
2652 nextchar(pRExC_state);
2655 ret = reg_node(pRExC_state, CLUMP);
2657 nextchar(pRExC_state);
2658 if (UTF && !PL_utf8_mark)
2659 is_utf8_mark((U8*)"~"); /* preload table */
2662 ret = reg_node(pRExC_state,
2664 ? (LOC ? ALNUMLUTF8 : ALNUMUTF8)
2665 : (LOC ? ALNUML : ALNUM));
2666 *flagp |= HASWIDTH|SIMPLE;
2667 nextchar(pRExC_state);
2668 if (UTF && !PL_utf8_alnum)
2669 is_utf8_alnum((U8*)"a"); /* preload table */
2672 ret = reg_node(pRExC_state,
2674 ? (LOC ? NALNUMLUTF8 : NALNUMUTF8)
2675 : (LOC ? NALNUML : NALNUM));
2676 *flagp |= HASWIDTH|SIMPLE;
2677 nextchar(pRExC_state);
2678 if (UTF && !PL_utf8_alnum)
2679 is_utf8_alnum((U8*)"a"); /* preload table */
2682 RExC_seen_zerolen++;
2683 RExC_seen |= REG_SEEN_LOOKBEHIND;
2684 ret = reg_node(pRExC_state,
2686 ? (LOC ? BOUNDLUTF8 : BOUNDUTF8)
2687 : (LOC ? BOUNDL : BOUND));
2689 nextchar(pRExC_state);
2690 if (UTF && !PL_utf8_alnum)
2691 is_utf8_alnum((U8*)"a"); /* preload table */
2694 RExC_seen_zerolen++;
2695 RExC_seen |= REG_SEEN_LOOKBEHIND;
2696 ret = reg_node(pRExC_state,
2698 ? (LOC ? NBOUNDLUTF8 : NBOUNDUTF8)
2699 : (LOC ? NBOUNDL : NBOUND));
2701 nextchar(pRExC_state);
2702 if (UTF && !PL_utf8_alnum)
2703 is_utf8_alnum((U8*)"a"); /* preload table */
2706 ret = reg_node(pRExC_state,
2708 ? (LOC ? SPACELUTF8 : SPACEUTF8)
2709 : (LOC ? SPACEL : SPACE));
2710 *flagp |= HASWIDTH|SIMPLE;
2711 nextchar(pRExC_state);
2712 if (UTF && !PL_utf8_space)
2713 is_utf8_space((U8*)" "); /* preload table */
2716 ret = reg_node(pRExC_state,
2718 ? (LOC ? NSPACELUTF8 : NSPACEUTF8)
2719 : (LOC ? NSPACEL : NSPACE));
2720 *flagp |= HASWIDTH|SIMPLE;
2721 nextchar(pRExC_state);
2722 if (UTF && !PL_utf8_space)
2723 is_utf8_space((U8*)" "); /* preload table */
2726 ret = reg_node(pRExC_state, UTF ? DIGITUTF8 : DIGIT);
2727 *flagp |= HASWIDTH|SIMPLE;
2728 nextchar(pRExC_state);
2729 if (UTF && !PL_utf8_digit)
2730 is_utf8_digit((U8*)"1"); /* preload table */
2733 ret = reg_node(pRExC_state, UTF ? NDIGITUTF8 : NDIGIT);
2734 *flagp |= HASWIDTH|SIMPLE;
2735 nextchar(pRExC_state);
2736 if (UTF && !PL_utf8_digit)
2737 is_utf8_digit((U8*)"1"); /* preload table */
2741 { /* a lovely hack--pretend we saw [\pX] instead */
2742 char* oldregxend = RExC_end;
2744 if (RExC_parse[1] == '{') {
2745 RExC_end = strchr(RExC_parse, '}');
2748 RExC_end = oldregxend;
2749 vFAIL("Missing right brace on \\p{}");
2754 RExC_end = RExC_parse + 2;
2757 ret = regclassutf8(pRExC_state);
2759 RExC_end = oldregxend;
2761 nextchar(pRExC_state);
2762 *flagp |= HASWIDTH|SIMPLE;
2775 case '1': case '2': case '3': case '4':
2776 case '5': case '6': case '7': case '8': case '9':
2778 I32 num = atoi(RExC_parse);
2780 if (num > 9 && num >= RExC_npar)
2783 while (isDIGIT(*RExC_parse))
2786 if (!SIZE_ONLY && num > RExC_rx->nparens)
2787 vFAIL("Reference to nonexistent group");
2789 ret = reganode(pRExC_state, FOLD
2790 ? (LOC ? REFFL : REFF)
2794 nextchar(pRExC_state);
2799 if (RExC_parse >= RExC_end)
2800 FAIL("Trailing \\");
2803 /* Do not generate `unrecognized' warnings here, we fall
2804 back into the quick-grab loop below */
2810 if (RExC_flags16 & PMf_EXTENDED) {
2811 while (RExC_parse < RExC_end && *RExC_parse != '\n') RExC_parse++;
2812 if (RExC_parse < RExC_end)
2818 register STRLEN len;
2827 ret = reg_node(pRExC_state, FOLD
2828 ? (LOC ? EXACTFL : EXACTF)
2831 for (len = 0, p = RExC_parse - 1;
2832 len < 127 && p < RExC_end;
2837 if (RExC_flags16 & PMf_EXTENDED)
2838 p = regwhite(p, RExC_end);
2900 char* e = strchr(p, '}');
2904 vFAIL("Missing right brace on \\x{}");
2907 numlen = 1; /* allow underscores */
2908 ender = (UV)scan_hex(p + 1, e - p - 1, &numlen);
2909 /* numlen is generous */
2910 if (numlen + len >= 127) {
2918 numlen = 0; /* disallow underscores */
2919 ender = (UV)scan_hex(p, 2, &numlen);
2925 ender = UCHARAT(p++);
2926 ender = toCTRL(ender);
2928 case '0': case '1': case '2': case '3':case '4':
2929 case '5': case '6': case '7': case '8':case '9':
2931 (isDIGIT(p[1]) && atoi(p) >= RExC_npar) ) {
2932 numlen = 0; /* disallow underscores */
2933 ender = (UV)scan_oct(p, 3, &numlen);
2943 FAIL("Trailing \\");
2946 if (!SIZE_ONLY && ckWARN(WARN_REGEXP) && isALPHA(*p))
2947 vWARN2(p +1, "Unrecognized escape \\%c passed through", *p);
2948 goto normal_default;
2953 if ((*p & 0xc0) == 0xc0 && UTF) {
2954 ender = utf8_to_uv((U8*)p, RExC_end - p,
2962 if (RExC_flags16 & PMf_EXTENDED)
2963 p = regwhite(p, RExC_end);
2966 ender = toLOWER_LC_uni(ender);
2968 ender = toLOWER_uni(ender);
2970 if (ISMULT2(p)) { /* Back off on ?+*. */
2973 else if (ender >= 0x80 && UTF) {
2974 reguni(pRExC_state, ender, s, &numlen);
2984 if (ender >= 0x80 && UTF) {
2985 reguni(pRExC_state, ender, s, &numlen);
2994 nextchar(pRExC_state);
2996 /* len is STRLEN which is unsigned, need to copy to signed */
2999 vFAIL("Internal disaster");
3008 RExC_size += STR_SZ(len);
3010 RExC_emit += STR_SZ(len);
3019 S_regwhite(pTHX_ char *p, char *e)
3024 else if (*p == '#') {
3027 } while (p < e && *p != '\n');
3035 /* Parse POSIX character classes: [[:foo:]], [[=foo=]], [[.foo.]].
3036 Character classes ([:foo:]) can also be negated ([:^foo:]).
3037 Returns a named class id (ANYOF_XXX) if successful, -1 otherwise.
3038 Equivalence classes ([=foo=]) and composites ([.foo.]) are parsed,
3039 but trigger warnings because they are currently unimplemented. */
3041 S_regpposixcc(pTHX_ RExC_state_t *pRExC_state, I32 value)
3044 I32 namedclass = OOB_NAMEDCLASS;
3046 if (value == '[' && RExC_parse + 1 < RExC_end &&
3047 /* I smell either [: or [= or [. -- POSIX has been here, right? */
3048 (*RExC_parse == ':' ||
3049 *RExC_parse == '=' ||
3050 *RExC_parse == '.')) {
3051 char c = *RExC_parse;
3052 char* s = RExC_parse++;
3054 while (RExC_parse < RExC_end && *RExC_parse != c)
3056 if (RExC_parse == RExC_end)
3057 /* Grandfather lone [:, [=, [. */
3060 char* t = RExC_parse++; /* skip over the c */
3062 if (*RExC_parse == ']') {
3063 RExC_parse++; /* skip over the ending ] */
3066 I32 complement = *posixcc == '^' ? *posixcc++ : 0;
3067 I32 skip = 5; /* the most common skip */
3071 if (strnEQ(posixcc, "alnum", 5))
3073 complement ? ANYOF_NALNUMC : ANYOF_ALNUMC;
3074 else if (strnEQ(posixcc, "alpha", 5))
3076 complement ? ANYOF_NALPHA : ANYOF_ALPHA;
3077 else if (strnEQ(posixcc, "ascii", 5))
3079 complement ? ANYOF_NASCII : ANYOF_ASCII;
3082 if (strnEQ(posixcc, "blank", 5))
3084 complement ? ANYOF_NBLANK : ANYOF_BLANK;
3087 if (strnEQ(posixcc, "cntrl", 5))
3089 complement ? ANYOF_NCNTRL : ANYOF_CNTRL;
3092 if (strnEQ(posixcc, "digit", 5))
3094 complement ? ANYOF_NDIGIT : ANYOF_DIGIT;
3097 if (strnEQ(posixcc, "graph", 5))
3099 complement ? ANYOF_NGRAPH : ANYOF_GRAPH;
3102 if (strnEQ(posixcc, "lower", 5))
3104 complement ? ANYOF_NLOWER : ANYOF_LOWER;
3107 if (strnEQ(posixcc, "print", 5))
3109 complement ? ANYOF_NPRINT : ANYOF_PRINT;
3110 else if (strnEQ(posixcc, "punct", 5))
3112 complement ? ANYOF_NPUNCT : ANYOF_PUNCT;
3115 if (strnEQ(posixcc, "space", 5))
3117 complement ? ANYOF_NPSXSPC : ANYOF_PSXSPC;
3120 if (strnEQ(posixcc, "upper", 5))
3122 complement ? ANYOF_NUPPER : ANYOF_UPPER;
3124 case 'w': /* this is not POSIX, this is the Perl \w */
3125 if (strnEQ(posixcc, "word", 4)) {
3127 complement ? ANYOF_NALNUM : ANYOF_ALNUM;
3132 if (strnEQ(posixcc, "xdigit", 6)) {
3134 complement ? ANYOF_NXDIGIT : ANYOF_XDIGIT;
3139 if (namedclass == OOB_NAMEDCLASS ||
3140 posixcc[skip] != ':' ||
3141 posixcc[skip+1] != ']')
3143 Simple_vFAIL3("POSIX class [:%.*s:] unknown",
3146 } else if (!SIZE_ONLY) {
3147 /* [[=foo=]] and [[.foo.]] are still future. */
3149 /* adjust RExC_parse so the warning shows after
3151 while (*RExC_parse && *RExC_parse != ']')
3153 Simple_vFAIL3("POSIX syntax [%c %c] is reserved for future extensions", c, c);
3156 /* Maternal grandfather:
3157 * "[:" ending in ":" but not in ":]" */
3167 S_checkposixcc(pTHX_ RExC_state_t *pRExC_state)
3169 if (!SIZE_ONLY && ckWARN(WARN_REGEXP) &&
3170 (*RExC_parse == ':' ||
3171 *RExC_parse == '=' ||
3172 *RExC_parse == '.')) {
3173 char *s = RExC_parse;
3176 while(*s && isALNUM(*s))
3178 if (*s && c == *s && s[1] == ']') {
3179 vWARN3(s+2, "POSIX syntax [%c %c] belongs inside character classes", c, c);
3181 /* [[=foo=]] and [[.foo.]] are still future. */
3182 if (c == '=' || c == '.')
3184 /* adjust RExC_parse so the error shows after
3186 while (*RExC_parse && *RExC_parse++ != ']')
3188 Simple_vFAIL3("POSIX syntax [%c %c] is reserved for future extensions", c, c);
3195 S_regclass(pTHX_ RExC_state_t *pRExC_state)
3198 register I32 lastvalue = OOB_CHAR8;
3199 register I32 range = 0;
3200 register regnode *ret;
3204 bool need_class = 0;
3206 ret = reg_node(pRExC_state, ANYOF);
3208 RExC_size += ANYOF_SKIP;
3211 ANYOF_BITMAP_ZERO(ret);
3212 RExC_emit += ANYOF_SKIP;
3214 ANYOF_FLAGS(ret) |= ANYOF_FOLD;
3216 ANYOF_FLAGS(ret) |= ANYOF_LOCALE;
3218 if (*RExC_parse == '^') { /* Complement of range. */
3222 ANYOF_FLAGS(ret) |= ANYOF_INVERT;
3225 if (!SIZE_ONLY && ckWARN(WARN_REGEXP))
3226 checkposixcc(pRExC_state);
3228 if (*RExC_parse == ']' || *RExC_parse == '-')
3229 goto skipcond; /* allow 1st char to be ] or - */
3230 while (RExC_parse < RExC_end && *RExC_parse != ']') {
3232 namedclass = OOB_NAMEDCLASS;
3234 rangebegin = RExC_parse;
3235 value = UCHARAT(RExC_parse++);
3237 namedclass = regpposixcc(pRExC_state, value);
3238 else if (value == '\\') {
3239 value = UCHARAT(RExC_parse++);
3240 /* Some compilers cannot handle switching on 64-bit integer
3241 * values, therefore the 'value' cannot be an UV. --jhi */
3243 case 'w': namedclass = ANYOF_ALNUM; break;
3244 case 'W': namedclass = ANYOF_NALNUM; break;
3245 case 's': namedclass = ANYOF_SPACE; break;
3246 case 'S': namedclass = ANYOF_NSPACE; break;
3247 case 'd': namedclass = ANYOF_DIGIT; break;
3248 case 'D': namedclass = ANYOF_NDIGIT; break;
3249 case 'n': value = '\n'; break;
3250 case 'r': value = '\r'; break;
3251 case 't': value = '\t'; break;
3252 case 'f': value = '\f'; break;
3253 case 'b': value = '\b'; break;
3255 case 'e': value = '\033'; break;
3256 case 'a': value = '\007'; break;
3258 case 'e': value = '\047'; break;
3259 case 'a': value = '\057'; break;
3262 numlen = 0; /* disallow underscores */
3263 value = (UV)scan_hex(RExC_parse, 2, &numlen);
3264 RExC_parse += numlen;
3267 value = UCHARAT(RExC_parse++);
3268 value = toCTRL(value);
3270 case '0': case '1': case '2': case '3': case '4':
3271 case '5': case '6': case '7': case '8': case '9':
3272 numlen = 0; /* disallow underscores */
3273 value = (UV)scan_oct(--RExC_parse, 3, &numlen);
3274 RExC_parse += numlen;
3277 if (!SIZE_ONLY && ckWARN(WARN_REGEXP) && isALPHA(value))
3279 vWARN2(RExC_parse, "Unrecognized escape \\%c in character class passed through", (int)value);
3283 if (namedclass > OOB_NAMEDCLASS) {
3284 if (!need_class && !SIZE_ONLY)
3285 ANYOF_CLASS_ZERO(ret);
3287 if (range) { /* a-\d, a-[:digit:] */
3289 if (ckWARN(WARN_REGEXP))
3291 "False [] range \"%*.*s\"",
3292 RExC_parse - rangebegin,
3293 RExC_parse - rangebegin,
3295 ANYOF_BITMAP_SET(ret, lastvalue);
3296 ANYOF_BITMAP_SET(ret, '-');
3298 range = 0; /* this is not a true range */
3301 switch (namedclass) {
3304 ANYOF_CLASS_SET(ret, ANYOF_ALNUM);
3306 for (value = 0; value < 256; value++)
3308 ANYOF_BITMAP_SET(ret, value);
3313 ANYOF_CLASS_SET(ret, ANYOF_NALNUM);
3315 for (value = 0; value < 256; value++)
3316 if (!isALNUM(value))
3317 ANYOF_BITMAP_SET(ret, value);
3322 ANYOF_CLASS_SET(ret, ANYOF_SPACE);
3324 for (value = 0; value < 256; value++)
3326 ANYOF_BITMAP_SET(ret, value);
3331 ANYOF_CLASS_SET(ret, ANYOF_NSPACE);
3333 for (value = 0; value < 256; value++)
3334 if (!isSPACE(value))
3335 ANYOF_BITMAP_SET(ret, value);
3340 ANYOF_CLASS_SET(ret, ANYOF_DIGIT);
3342 for (value = '0'; value <= '9'; value++)
3343 ANYOF_BITMAP_SET(ret, value);
3348 ANYOF_CLASS_SET(ret, ANYOF_NDIGIT);
3350 for (value = 0; value < '0'; value++)
3351 ANYOF_BITMAP_SET(ret, value);
3352 for (value = '9' + 1; value < 256; value++)
3353 ANYOF_BITMAP_SET(ret, value);
3358 ANYOF_CLASS_SET(ret, ANYOF_NALNUMC);
3360 for (value = 0; value < 256; value++)
3361 if (!isALNUMC(value))
3362 ANYOF_BITMAP_SET(ret, value);
3367 ANYOF_CLASS_SET(ret, ANYOF_ALNUMC);
3369 for (value = 0; value < 256; value++)
3370 if (isALNUMC(value))
3371 ANYOF_BITMAP_SET(ret, value);
3376 ANYOF_CLASS_SET(ret, ANYOF_ALPHA);
3378 for (value = 0; value < 256; value++)
3380 ANYOF_BITMAP_SET(ret, value);
3385 ANYOF_CLASS_SET(ret, ANYOF_NALPHA);
3387 for (value = 0; value < 256; value++)
3388 if (!isALPHA(value))
3389 ANYOF_BITMAP_SET(ret, value);
3394 ANYOF_CLASS_SET(ret, ANYOF_ASCII);
3397 for (value = 0; value < 128; value++)
3398 ANYOF_BITMAP_SET(ret, value);
3400 for (value = 0; value < 256; value++)
3402 ANYOF_BITMAP_SET(ret, value);
3408 ANYOF_CLASS_SET(ret, ANYOF_NASCII);
3411 for (value = 128; value < 256; value++)
3412 ANYOF_BITMAP_SET(ret, value);
3414 for (value = 0; value < 256; value++)
3415 if (!isASCII(value))
3416 ANYOF_BITMAP_SET(ret, value);
3422 ANYOF_CLASS_SET(ret, ANYOF_BLANK);
3424 for (value = 0; value < 256; value++)
3426 ANYOF_BITMAP_SET(ret, value);
3431 ANYOF_CLASS_SET(ret, ANYOF_NBLANK);
3433 for (value = 0; value < 256; value++)
3434 if (!isBLANK(value))
3435 ANYOF_BITMAP_SET(ret, value);
3440 ANYOF_CLASS_SET(ret, ANYOF_CNTRL);
3442 for (value = 0; value < 256; value++)
3444 ANYOF_BITMAP_SET(ret, value);
3446 lastvalue = OOB_CHAR8;
3450 ANYOF_CLASS_SET(ret, ANYOF_NCNTRL);
3452 for (value = 0; value < 256; value++)
3453 if (!isCNTRL(value))
3454 ANYOF_BITMAP_SET(ret, value);
3459 ANYOF_CLASS_SET(ret, ANYOF_GRAPH);
3461 for (value = 0; value < 256; value++)
3463 ANYOF_BITMAP_SET(ret, value);
3468 ANYOF_CLASS_SET(ret, ANYOF_NGRAPH);
3470 for (value = 0; value < 256; value++)
3471 if (!isGRAPH(value))
3472 ANYOF_BITMAP_SET(ret, value);
3477 ANYOF_CLASS_SET(ret, ANYOF_LOWER);
3479 for (value = 0; value < 256; value++)
3481 ANYOF_BITMAP_SET(ret, value);
3486 ANYOF_CLASS_SET(ret, ANYOF_NLOWER);
3488 for (value = 0; value < 256; value++)
3489 if (!isLOWER(value))
3490 ANYOF_BITMAP_SET(ret, value);
3495 ANYOF_CLASS_SET(ret, ANYOF_PRINT);
3497 for (value = 0; value < 256; value++)
3499 ANYOF_BITMAP_SET(ret, value);
3504 ANYOF_CLASS_SET(ret, ANYOF_NPRINT);
3506 for (value = 0; value < 256; value++)
3507 if (!isPRINT(value))
3508 ANYOF_BITMAP_SET(ret, value);
3513 ANYOF_CLASS_SET(ret, ANYOF_PSXSPC);
3515 for (value = 0; value < 256; value++)
3516 if (isPSXSPC(value))
3517 ANYOF_BITMAP_SET(ret, value);
3522 ANYOF_CLASS_SET(ret, ANYOF_NPSXSPC);
3524 for (value = 0; value < 256; value++)
3525 if (!isPSXSPC(value))
3526 ANYOF_BITMAP_SET(ret, value);
3531 ANYOF_CLASS_SET(ret, ANYOF_PUNCT);
3533 for (value = 0; value < 256; value++)
3535 ANYOF_BITMAP_SET(ret, value);
3540 ANYOF_CLASS_SET(ret, ANYOF_NPUNCT);
3542 for (value = 0; value < 256; value++)
3543 if (!isPUNCT(value))
3544 ANYOF_BITMAP_SET(ret, value);
3549 ANYOF_CLASS_SET(ret, ANYOF_UPPER);
3551 for (value = 0; value < 256; value++)
3553 ANYOF_BITMAP_SET(ret, value);
3558 ANYOF_CLASS_SET(ret, ANYOF_NUPPER);
3560 for (value = 0; value < 256; value++)
3561 if (!isUPPER(value))
3562 ANYOF_BITMAP_SET(ret, value);
3567 ANYOF_CLASS_SET(ret, ANYOF_XDIGIT);
3569 for (value = 0; value < 256; value++)
3570 if (isXDIGIT(value))
3571 ANYOF_BITMAP_SET(ret, value);
3576 ANYOF_CLASS_SET(ret, ANYOF_NXDIGIT);
3578 for (value = 0; value < 256; value++)
3579 if (!isXDIGIT(value))
3580 ANYOF_BITMAP_SET(ret, value);
3584 vFAIL("Invalid [::] class");
3588 ANYOF_FLAGS(ret) |= ANYOF_CLASS;
3593 if (lastvalue > value) /* b-a */ {
3594 Simple_vFAIL4("Invalid [] range \"%*.*s\"",
3595 RExC_parse - rangebegin,
3596 RExC_parse - rangebegin,
3603 if (*RExC_parse == '-' && RExC_parse+1 < RExC_end &&
3604 RExC_parse[1] != ']') {
3606 if (namedclass > OOB_NAMEDCLASS) { /* \w-, [:word:]- */
3607 if (ckWARN(WARN_REGEXP))
3609 "False [] range \"%*.*s\"",
3610 RExC_parse - rangebegin,
3611 RExC_parse - rangebegin,
3614 ANYOF_BITMAP_SET(ret, '-');
3617 continue; /* do it next time */
3620 /* now is the next time */
3622 #ifndef ASCIIish /* EBCDIC, for example. */
3623 if ((isLOWER(lastvalue) && isLOWER(value)) ||
3624 (isUPPER(lastvalue) && isUPPER(value)))
3627 if (isLOWER(lastvalue)) {
3628 for (i = lastvalue; i <= value; i++)
3630 ANYOF_BITMAP_SET(ret, i);
3632 for (i = lastvalue; i <= value; i++)
3634 ANYOF_BITMAP_SET(ret, i);
3639 for ( ; lastvalue <= value; lastvalue++)
3640 ANYOF_BITMAP_SET(ret, lastvalue);
3646 RExC_size += ANYOF_CLASS_ADD_SKIP;
3648 RExC_emit += ANYOF_CLASS_ADD_SKIP;
3650 /* optimize case-insensitive simple patterns (e.g. /[a-z]/i) */
3652 (ANYOF_FLAGS(ret) & (ANYOF_FLAGS_ALL ^ ANYOF_INVERT)) == ANYOF_FOLD) {
3653 for (value = 0; value < 256; ++value) {
3654 if (ANYOF_BITMAP_TEST(ret, value)) {
3655 I32 cf = PL_fold[value];
3656 ANYOF_BITMAP_SET(ret, cf);
3659 ANYOF_FLAGS(ret) &= ~ANYOF_FOLD;
3661 /* optimize inverted simple patterns (e.g. [^a-z]) */
3662 if (!SIZE_ONLY && (ANYOF_FLAGS(ret) & ANYOF_FLAGS_ALL) == ANYOF_INVERT) {
3663 for (value = 0; value < ANYOF_BITMAP_SIZE; ++value)
3664 ANYOF_BITMAP(ret)[value] ^= ANYOF_FLAGS_ALL;
3665 ANYOF_FLAGS(ret) = 0;
3671 S_regclassutf8(pTHX_ RExC_state_t *pRExC_state)
3675 register U32 lastvalue = OOB_UTF8;
3676 register I32 range = 0;
3677 register regnode *ret;
3685 if (*RExC_parse == '^') { /* Complement of range. */
3689 flags |= ANYOF_INVERT;
3693 flags |= ANYOF_FOLD;
3695 flags |= ANYOF_LOCALE;
3696 listsv = newSVpvn("# comment\n", 10);
3699 if (!SIZE_ONLY && ckWARN(WARN_REGEXP))
3700 checkposixcc(pRExC_state);
3702 if (*RExC_parse == ']' || *RExC_parse == '-')
3703 goto skipcond; /* allow 1st char to be ] or - */
3705 while (RExC_parse < RExC_end && *RExC_parse != ']') {
3707 namedclass = OOB_NAMEDCLASS;
3709 rangebegin = RExC_parse;
3710 value = utf8_to_uv((U8*)RExC_parse,
3711 RExC_end - RExC_parse,
3713 RExC_parse += numlen;
3715 namedclass = regpposixcc(pRExC_state, value);
3716 else if (value == '\\') {
3717 value = (U32)utf8_to_uv((U8*)RExC_parse,
3718 RExC_end - RExC_parse,
3720 RExC_parse += numlen;
3721 /* Some compilers cannot handle switching on 64-bit integer
3722 * values, therefore value cannot be an UV. Yes, this will
3723 * be a problem later if we want switch on Unicode. --jhi */
3725 case 'w': namedclass = ANYOF_ALNUM; break;
3726 case 'W': namedclass = ANYOF_NALNUM; break;
3727 case 's': namedclass = ANYOF_SPACE; break;
3728 case 'S': namedclass = ANYOF_NSPACE; break;
3729 case 'd': namedclass = ANYOF_DIGIT; break;
3730 case 'D': namedclass = ANYOF_NDIGIT; break;
3733 if (*RExC_parse == '{') {
3734 e = strchr(RExC_parse++, '}');
3736 vFAIL("Missing right brace on \\p{}");
3745 Perl_sv_catpvf(aTHX_ listsv,
3746 "+utf8::%.*s\n", (int)n, RExC_parse);
3748 Perl_sv_catpvf(aTHX_ listsv,
3749 "!utf8::%.*s\n", (int)n, RExC_parse);
3752 lastvalue = OOB_UTF8;
3754 case 'n': value = '\n'; break;
3755 case 'r': value = '\r'; break;
3756 case 't': value = '\t'; break;
3757 case 'f': value = '\f'; break;
3758 case 'b': value = '\b'; break;
3760 case 'e': value = '\033'; break;
3761 case 'a': value = '\007'; break;
3763 case 'e': value = '\047'; break;
3764 case 'a': value = '\057'; break;
3767 if (*RExC_parse == '{') {
3768 e = strchr(RExC_parse++, '}');
3770 vFAIL("Missing right brace on \\x{}");
3771 numlen = 1; /* allow underscores */
3772 value = (UV)scan_hex(RExC_parse,
3778 numlen = 0; /* disallow underscores */
3779 value = (UV)scan_hex(RExC_parse, 2, &numlen);
3780 RExC_parse += numlen;
3784 value = UCHARAT(RExC_parse++);
3785 value = toCTRL(value);
3787 case '0': case '1': case '2': case '3': case '4':
3788 case '5': case '6': case '7': case '8': case '9':
3789 numlen = 0; /* disallow underscores */
3790 value = (UV)scan_oct(--RExC_parse, 3, &numlen);
3791 RExC_parse += numlen;
3794 if (!SIZE_ONLY && ckWARN(WARN_REGEXP) && isALPHA(value))
3796 "Unrecognized escape \\%c in character class passed through",
3801 if (namedclass > OOB_NAMEDCLASS) {
3802 if (range) { /* a-\d, a-[:digit:] */
3804 if (ckWARN(WARN_REGEXP))
3806 "False [] range \"%*.*s\"",
3807 RExC_parse - rangebegin,
3808 RExC_parse - rangebegin,
3810 Perl_sv_catpvf(aTHX_ listsv,
3811 /* 0x002D is Unicode for '-' */
3812 "%04"UVxf"\n002D\n", (UV)lastvalue);
3817 switch (namedclass) {
3819 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsWord\n"); break;
3821 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsWord\n"); break;
3823 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsAlnum\n"); break;
3825 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsAlnum\n"); break;
3827 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsAlpha\n"); break;
3829 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsAlpha\n"); break;
3831 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsASCII\n"); break;
3833 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsASCII\n"); break;
3835 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsCntrl\n"); break;
3837 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsCntrl\n"); break;
3839 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsGraph\n"); break;
3841 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsGraph\n"); break;
3843 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsDigit\n"); break;
3845 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsDigit\n"); break;
3847 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsLower\n"); break;
3849 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsLower\n"); break;
3851 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsPrint\n"); break;
3853 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsPrint\n"); break;
3855 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsPunct\n"); break;
3857 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsPunct\n"); break;
3859 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsSpacePerl\n");break;
3861 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsSpacePerl\n");break;
3863 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsBlank\n"); break;
3865 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsBlank\n"); break;
3867 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsSpace\n"); break;
3869 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsSpace\n"); break;
3871 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsUpper\n"); break;
3873 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsUpper\n"); break;
3875 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsXDigit\n"); break;
3877 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsXDigit\n"); break;
3883 if (lastvalue > value) { /* b-a */
3884 Simple_vFAIL4("Invalid [] range \"%*.*s\"",
3885 RExC_parse - rangebegin,
3886 RExC_parse - rangebegin,
3893 if (*RExC_parse == '-' && RExC_parse+1 < RExC_end &&
3894 RExC_parse[1] != ']') {
3896 if (namedclass > OOB_NAMEDCLASS) { /* \w-, [:word:]- */
3897 if (ckWARN(WARN_REGEXP))
3899 "False [] range \"%*.*s\"",
3900 RExC_parse - rangebegin,
3901 RExC_parse - rangebegin,
3904 Perl_sv_catpvf(aTHX_ listsv,
3905 /* 0x002D is Unicode for '-' */
3909 continue; /* do it next time */
3912 /* now is the next time */
3914 Perl_sv_catpvf(aTHX_ listsv, "%04"UVxf"\t%04"UVxf"\n",
3915 (UV)lastvalue, (UV)value);
3919 ret = reganode(pRExC_state, ANYOFUTF8, 0);
3922 SV *rv = swash_init("utf8", "", listsv, 1, 0);
3926 av_push(av, listsv);
3927 rv = newRV_inc((SV*)av);
3929 SvREFCNT_dec(listsv);
3931 n = add_data(pRExC_state, 1, "s");
3932 RExC_rx->data->data[n] = (void*)rv;
3933 ARG1_SET(ret, flags);
3941 S_nextchar(pTHX_ RExC_state_t *pRExC_state)
3943 char* retval = RExC_parse++;
3946 if (*RExC_parse == '(' && RExC_parse[1] == '?' &&
3947 RExC_parse[2] == '#') {
3948 while (*RExC_parse && *RExC_parse != ')')
3953 if (RExC_flags16 & PMf_EXTENDED) {
3954 if (isSPACE(*RExC_parse)) {
3958 else if (*RExC_parse == '#') {
3959 while (*RExC_parse && *RExC_parse != '\n')
3970 - reg_node - emit a node
3972 STATIC regnode * /* Location. */
3973 S_reg_node(pTHX_ RExC_state_t *pRExC_state, U8 op)
3975 register regnode *ret;
3976 register regnode *ptr;
3980 SIZE_ALIGN(RExC_size);
3985 NODE_ALIGN_FILL(ret);
3987 FILL_ADVANCE_NODE(ptr, op);
3994 - reganode - emit a node with an argument
3996 STATIC regnode * /* Location. */
3997 S_reganode(pTHX_ RExC_state_t *pRExC_state, U8 op, U32 arg)
3999 register regnode *ret;
4000 register regnode *ptr;
4004 SIZE_ALIGN(RExC_size);
4009 NODE_ALIGN_FILL(ret);
4011 FILL_ADVANCE_NODE_ARG(ptr, op, arg);
4018 - reguni - emit (if appropriate) a Unicode character
4021 S_reguni(pTHX_ RExC_state_t *pRExC_state, UV uv, char* s, STRLEN* lenp)
4023 *lenp = SIZE_ONLY ? UNISKIP(uv) : (uv_to_utf8((U8*)s, uv) - (U8*)s);
4027 - reginsert - insert an operator in front of already-emitted operand
4029 * Means relocating the operand.
4032 S_reginsert(pTHX_ RExC_state_t *pRExC_state, U8 op, regnode *opnd)
4034 register regnode *src;
4035 register regnode *dst;
4036 register regnode *place;
4037 register int offset = regarglen[(U8)op];
4039 /* (PL_regkind[(U8)op] == CURLY ? EXTRA_STEP_2ARGS : 0); */
4042 RExC_size += NODE_STEP_REGNODE + offset;
4047 RExC_emit += NODE_STEP_REGNODE + offset;
4050 StructCopy(--src, --dst, regnode);
4052 place = opnd; /* Op node, where operand used to be. */
4053 src = NEXTOPER(place);
4054 FILL_ADVANCE_NODE(place, op);
4055 Zero(src, offset, regnode);
4059 - regtail - set the next-pointer at the end of a node chain of p to val.
4062 S_regtail(pTHX_ RExC_state_t *pRExC_state, regnode *p, regnode *val)
4064 register regnode *scan;
4065 register regnode *temp;
4070 /* Find last node. */
4073 temp = regnext(scan);
4079 if (reg_off_by_arg[OP(scan)]) {
4080 ARG_SET(scan, val - scan);
4083 NEXT_OFF(scan) = val - scan;
4088 - regoptail - regtail on operand of first argument; nop if operandless
4091 S_regoptail(pTHX_ RExC_state_t *pRExC_state, regnode *p, regnode *val)
4093 /* "Operandless" and "op != BRANCH" are synonymous in practice. */
4094 if (p == NULL || SIZE_ONLY)
4096 if (PL_regkind[(U8)OP(p)] == BRANCH) {
4097 regtail(pRExC_state, NEXTOPER(p), val);
4099 else if ( PL_regkind[(U8)OP(p)] == BRANCHJ) {
4100 regtail(pRExC_state, NEXTOPER(NEXTOPER(p)), val);
4107 - regcurly - a little FSA that accepts {\d+,?\d*}
4110 S_regcurly(pTHX_ register char *s)
4129 S_dumpuntil(pTHX_ regnode *start, regnode *node, regnode *last, SV* sv, I32 l)
4132 register U8 op = EXACT; /* Arbitrary non-END op. */
4133 register regnode *next;
4135 while (op != END && (!last || node < last)) {
4136 /* While that wasn't END last time... */
4142 next = regnext(node);
4144 if (OP(node) == OPTIMIZED)
4147 PerlIO_printf(Perl_debug_log, "%4"IVdf":%*s%s", (IV)(node - start),
4148 (int)(2*l + 1), "", SvPVX(sv));
4149 if (next == NULL) /* Next ptr. */
4150 PerlIO_printf(Perl_debug_log, "(0)");
4152 PerlIO_printf(Perl_debug_log, "(%"IVdf")", (IV)(next - start));
4153 (void)PerlIO_putc(Perl_debug_log, '\n');
4155 if (PL_regkind[(U8)op] == BRANCHJ) {
4156 register regnode *nnode = (OP(next) == LONGJMP
4159 if (last && nnode > last)
4161 node = dumpuntil(start, NEXTOPER(NEXTOPER(node)), nnode, sv, l + 1);
4163 else if (PL_regkind[(U8)op] == BRANCH) {
4164 node = dumpuntil(start, NEXTOPER(node), next, sv, l + 1);
4166 else if ( op == CURLY) { /* `next' might be very big: optimizer */
4167 node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
4168 NEXTOPER(node) + EXTRA_STEP_2ARGS + 1, sv, l + 1);
4170 else if (PL_regkind[(U8)op] == CURLY && op != CURLYX) {
4171 node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
4174 else if ( op == PLUS || op == STAR) {
4175 node = dumpuntil(start, NEXTOPER(node), NEXTOPER(node) + 1, sv, l + 1);
4177 else if (op == ANYOF) {
4178 node = NEXTOPER(node);
4181 else if (PL_regkind[(U8)op] == EXACT) {
4182 /* Literal string, where present. */
4183 node += NODE_SZ_STR(node) - 1;
4184 node = NEXTOPER(node);
4187 node = NEXTOPER(node);
4188 node += regarglen[(U8)op];
4190 if (op == CURLYX || op == OPEN)
4192 else if (op == WHILEM)
4195 #endif /* DEBUGGING */
4200 - regdump - dump a regexp onto Perl_debug_log in vaguely comprehensible form
4203 Perl_regdump(pTHX_ regexp *r)
4206 SV *sv = sv_newmortal();
4208 (void)dumpuntil(r->program, r->program + 1, NULL, sv, 0);
4210 /* Header fields of interest. */
4211 if (r->anchored_substr)
4212 PerlIO_printf(Perl_debug_log,
4213 "anchored `%s%.*s%s'%s at %"IVdf" ",
4215 (int)(SvCUR(r->anchored_substr) - (SvTAIL(r->anchored_substr)!=0)),
4216 SvPVX(r->anchored_substr),
4218 SvTAIL(r->anchored_substr) ? "$" : "",
4219 (IV)r->anchored_offset);
4220 if (r->float_substr)
4221 PerlIO_printf(Perl_debug_log,
4222 "floating `%s%.*s%s'%s at %"IVdf"..%"UVuf" ",
4224 (int)(SvCUR(r->float_substr) - (SvTAIL(r->float_substr)!=0)),
4225 SvPVX(r->float_substr),
4227 SvTAIL(r->float_substr) ? "$" : "",
4228 (IV)r->float_min_offset, (UV)r->float_max_offset);
4229 if (r->check_substr)
4230 PerlIO_printf(Perl_debug_log,
4231 r->check_substr == r->float_substr
4232 ? "(checking floating" : "(checking anchored");
4233 if (r->reganch & ROPT_NOSCAN)
4234 PerlIO_printf(Perl_debug_log, " noscan");
4235 if (r->reganch & ROPT_CHECK_ALL)
4236 PerlIO_printf(Perl_debug_log, " isall");
4237 if (r->check_substr)
4238 PerlIO_printf(Perl_debug_log, ") ");
4240 if (r->regstclass) {
4241 regprop(sv, r->regstclass);
4242 PerlIO_printf(Perl_debug_log, "stclass `%s' ", SvPVX(sv));
4244 if (r->reganch & ROPT_ANCH) {
4245 PerlIO_printf(Perl_debug_log, "anchored");
4246 if (r->reganch & ROPT_ANCH_BOL)
4247 PerlIO_printf(Perl_debug_log, "(BOL)");
4248 if (r->reganch & ROPT_ANCH_MBOL)
4249 PerlIO_printf(Perl_debug_log, "(MBOL)");
4250 if (r->reganch & ROPT_ANCH_SBOL)
4251 PerlIO_printf(Perl_debug_log, "(SBOL)");
4252 if (r->reganch & ROPT_ANCH_GPOS)
4253 PerlIO_printf(Perl_debug_log, "(GPOS)");
4254 PerlIO_putc(Perl_debug_log, ' ');
4256 if (r->reganch & ROPT_GPOS_SEEN)
4257 PerlIO_printf(Perl_debug_log, "GPOS ");
4258 if (r->reganch & ROPT_SKIP)
4259 PerlIO_printf(Perl_debug_log, "plus ");
4260 if (r->reganch & ROPT_IMPLICIT)
4261 PerlIO_printf(Perl_debug_log, "implicit ");
4262 PerlIO_printf(Perl_debug_log, "minlen %ld ", (long) r->minlen);
4263 if (r->reganch & ROPT_EVAL_SEEN)
4264 PerlIO_printf(Perl_debug_log, "with eval ");
4265 PerlIO_printf(Perl_debug_log, "\n");
4266 #endif /* DEBUGGING */
4270 S_put_byte(pTHX_ SV *sv, int c)
4272 if (isCNTRL(c) || c == 127 || c == 255)
4273 Perl_sv_catpvf(aTHX_ sv, "\\%o", c);
4274 else if (c == '-' || c == ']' || c == '\\' || c == '^')
4275 Perl_sv_catpvf(aTHX_ sv, "\\%c", c);
4277 Perl_sv_catpvf(aTHX_ sv, "%c", c);
4281 - regprop - printable representation of opcode
4284 Perl_regprop(pTHX_ SV *sv, regnode *o)
4289 sv_setpvn(sv, "", 0);
4290 if (OP(o) >= reg_num) /* regnode.type is unsigned */
4291 /* It would be nice to FAIL() here, but this may be called from
4292 regexec.c, and it would be hard to supply pRExC_state. */
4293 Perl_croak(aTHX_ "Corrupted regexp opcode");
4294 sv_catpv(sv, (char*)reg_name[OP(o)]); /* Take off const! */
4296 k = PL_regkind[(U8)OP(o)];
4299 Perl_sv_catpvf(aTHX_ sv, " <%s%.*s%s>", PL_colors[0],
4300 STR_LEN(o), STRING(o), PL_colors[1]);
4301 else if (k == CURLY) {
4302 if (OP(o) == CURLYM || OP(o) == CURLYN || OP(o) == CURLYX)
4303 Perl_sv_catpvf(aTHX_ sv, "[%d]", o->flags); /* Parenth number */
4304 Perl_sv_catpvf(aTHX_ sv, " {%d,%d}", ARG1(o), ARG2(o));
4306 else if (k == WHILEM && o->flags) /* Ordinal/of */
4307 Perl_sv_catpvf(aTHX_ sv, "[%d/%d]", o->flags & 0xf, o->flags>>4);
4308 else if (k == REF || k == OPEN || k == CLOSE || k == GROUPP )
4309 Perl_sv_catpvf(aTHX_ sv, "%d", (int)ARG(o)); /* Parenth number */
4310 else if (k == LOGICAL)
4311 Perl_sv_catpvf(aTHX_ sv, "[%d]", o->flags); /* 2: embedded, otherwise 1 */
4312 else if (k == ANYOF) {
4313 int i, rangestart = -1;
4314 bool anyofutf8 = OP(o) == ANYOFUTF8;
4315 U8 flags = anyofutf8 ? ARG1(o) : o->flags;
4316 const char * const anyofs[] = { /* Should be syncronized with
4317 * ANYOF_ #xdefines in regcomp.h */
4350 if (flags & ANYOF_LOCALE)
4351 sv_catpv(sv, "{loc}");
4352 if (flags & ANYOF_FOLD)
4353 sv_catpv(sv, "{i}");
4354 Perl_sv_catpvf(aTHX_ sv, "[%s", PL_colors[0]);
4355 if (flags & ANYOF_INVERT)
4357 if (OP(o) == ANYOF) {
4358 for (i = 0; i <= 256; i++) {
4359 if (i < 256 && ANYOF_BITMAP_TEST(o,i)) {
4360 if (rangestart == -1)
4362 } else if (rangestart != -1) {
4363 if (i <= rangestart + 3)
4364 for (; rangestart < i; rangestart++)
4365 put_byte(sv, rangestart);
4367 put_byte(sv, rangestart);
4369 put_byte(sv, i - 1);
4374 if (o->flags & ANYOF_CLASS)
4375 for (i = 0; i < sizeof(anyofs)/sizeof(char*); i++)
4376 if (ANYOF_CLASS_TEST(o,i))
4377 sv_catpv(sv, anyofs[i]);
4380 SV *rv = (SV*)PL_regdata->data[ARG2(o)];
4381 AV *av = (AV*)SvRV((SV*)rv);
4382 SV *sw = *av_fetch(av, 0, FALSE);
4383 SV *lv = *av_fetch(av, 1, FALSE);
4385 U8 s[UTF8_MAXLEN+1];
4386 for (i = 0; i <= 256; i++) { /* just the first 256 */
4387 U8 *e = uv_to_utf8(s, i);
4388 if (i < 256 && swash_fetch(sw, s)) {
4389 if (rangestart == -1)
4391 } else if (rangestart != -1) {
4394 if (i <= rangestart + 3)
4395 for (; rangestart < i; rangestart++) {
4396 for(e = uv_to_utf8(s, rangestart), p = s; p < e; p++)
4400 for (e = uv_to_utf8(s, rangestart), p = s; p < e; p++)
4403 for (e = uv_to_utf8(s, i - 1), p = s; p < e; p++)
4409 sv_catpv(sv, "...");
4411 char *s = savepv(SvPVX(lv));
4413 while(*s && *s != '\n') s++;
4429 Perl_sv_catpvf(aTHX_ sv, "%s]", PL_colors[1]);
4431 else if (k == BRANCHJ && (OP(o) == UNLESSM || OP(o) == IFMATCH))
4432 Perl_sv_catpvf(aTHX_ sv, "[-%d]", o->flags);
4433 #endif /* DEBUGGING */
4437 Perl_re_intuit_string(pTHX_ regexp *prog)
4438 { /* Assume that RE_INTUIT is set */
4441 char *s = SvPV(prog->check_substr,n_a);
4443 if (!PL_colorset) reginitcolors();
4444 PerlIO_printf(Perl_debug_log,
4445 "%sUsing REx substr:%s `%s%.60s%s%s'\n",
4446 PL_colors[4],PL_colors[5],PL_colors[0],
4449 (strlen(s) > 60 ? "..." : ""));
4452 return prog->check_substr;
4456 Perl_pregfree(pTHX_ struct regexp *r)
4458 DEBUG_r(if (!PL_colorset) reginitcolors());
4460 if (!r || (--r->refcnt > 0))
4462 DEBUG_r(PerlIO_printf(Perl_debug_log,
4463 "%sFreeing REx:%s `%s%.60s%s%s'\n",
4464 PL_colors[4],PL_colors[5],PL_colors[0],
4467 (strlen(r->precomp) > 60 ? "..." : "")));
4470 Safefree(r->precomp);
4471 if (RX_MATCH_COPIED(r))
4472 Safefree(r->subbeg);
4474 if (r->anchored_substr)
4475 SvREFCNT_dec(r->anchored_substr);
4476 if (r->float_substr)
4477 SvREFCNT_dec(r->float_substr);
4478 Safefree(r->substrs);
4481 int n = r->data->count;
4482 AV* new_comppad = NULL;
4487 switch (r->data->what[n]) {
4491 SV *rv = (SV*)r->data->data[n];
4492 AV *av = (AV*)SvRV((SV*)rv);
4493 SV *sw = *av_fetch(av, 0, FALSE);
4494 SV *lv = *av_fetch(av, 1, FALSE);
4499 SvREFCNT_dec((SV*)r->data->data[n]);
4502 Safefree(r->data->data[n]);
4505 new_comppad = (AV*)r->data->data[n];
4508 if (new_comppad == NULL)
4509 Perl_croak(aTHX_ "panic: pregfree comppad");
4510 old_comppad = PL_comppad;
4511 old_curpad = PL_curpad;
4512 /* Watch out for global destruction's random ordering. */
4513 if (SvTYPE(new_comppad) == SVt_PVAV) {
4514 PL_comppad = new_comppad;
4515 PL_curpad = AvARRAY(new_comppad);
4519 op_free((OP_4tree*)r->data->data[n]);
4520 PL_comppad = old_comppad;
4521 PL_curpad = old_curpad;
4522 SvREFCNT_dec((SV*)new_comppad);
4528 Perl_croak(aTHX_ "panic: regfree data code '%c'", r->data->what[n]);
4531 Safefree(r->data->what);
4534 Safefree(r->startp);
4540 - regnext - dig the "next" pointer out of a node
4542 * [Note, when REGALIGN is defined there are two places in regmatch()
4543 * that bypass this code for speed.]
4546 Perl_regnext(pTHX_ register regnode *p)
4548 register I32 offset;
4550 if (p == &PL_regdummy)
4553 offset = (reg_off_by_arg[OP(p)] ? ARG(p) : NEXT_OFF(p));
4561 S_re_croak2(pTHX_ const char* pat1,const char* pat2,...)
4564 STRLEN l1 = strlen(pat1);
4565 STRLEN l2 = strlen(pat2);
4574 Copy(pat1, buf, l1 , char);
4575 Copy(pat2, buf + l1, l2 , char);
4576 buf[l1 + l2] = '\n';
4577 buf[l1 + l2 + 1] = '\0';
4579 /* ANSI variant takes additional second argument */
4580 va_start(args, pat2);
4584 msv = vmess(buf, &args);
4586 message = SvPV(msv,l1);
4589 Copy(message, buf, l1 , char);
4590 buf[l1] = '\0'; /* Overwrite \n */
4591 Perl_croak(aTHX_ "%s", buf);
4594 /* XXX Here's a total kludge. But we need to re-enter for swash routines. */
4597 Perl_save_re_context(pTHX)
4600 SAVEPPTR(RExC_precomp); /* uncompiled string. */
4601 SAVEI32(RExC_npar); /* () count. */
4602 SAVEI32(RExC_size); /* Code size. */
4603 SAVEI16(RExC_flags16); /* are we folding, multilining? */
4604 SAVEVPTR(RExC_rx); /* from regcomp.c */
4605 SAVEI32(RExC_seen); /* from regcomp.c */
4606 SAVEI32(RExC_sawback); /* Did we see \1, ...? */
4607 SAVEI32(RExC_naughty); /* How bad is this pattern? */
4608 SAVEVPTR(RExC_emit); /* Code-emit pointer; ®dummy = don't */
4609 SAVEPPTR(RExC_end); /* End of input for compile */
4610 SAVEPPTR(RExC_parse); /* Input-scan pointer. */
4613 SAVEI32(PL_reg_flags); /* from regexec.c */
4615 SAVEPPTR(PL_reginput); /* String-input pointer. */
4616 SAVEPPTR(PL_regbol); /* Beginning of input, for ^ check. */
4617 SAVEPPTR(PL_regeol); /* End of input, for $ check. */
4618 SAVEVPTR(PL_regstartp); /* Pointer to startp array. */
4619 SAVEVPTR(PL_regendp); /* Ditto for endp. */
4620 SAVEVPTR(PL_reglastparen); /* Similarly for lastparen. */
4621 SAVEPPTR(PL_regtill); /* How far we are required to go. */
4622 SAVEI8(PL_regprev); /* char before regbol, \n if none */
4623 SAVEVPTR(PL_reg_start_tmp); /* from regexec.c */
4624 PL_reg_start_tmp = 0;
4625 SAVEFREEPV(PL_reg_start_tmp);
4626 SAVEI32(PL_reg_start_tmpl); /* from regexec.c */
4627 PL_reg_start_tmpl = 0;
4628 SAVEVPTR(PL_regdata);
4629 SAVEI32(PL_reg_eval_set); /* from regexec.c */
4630 SAVEI32(PL_regnarrate); /* from regexec.c */
4631 SAVEVPTR(PL_regprogram); /* from regexec.c */
4632 SAVEINT(PL_regindent); /* from regexec.c */
4633 SAVEVPTR(PL_regcc); /* from regexec.c */
4634 SAVEVPTR(PL_curcop);
4635 SAVEVPTR(PL_reg_call_cc); /* from regexec.c */
4636 SAVEVPTR(PL_reg_re); /* from regexec.c */
4637 SAVEPPTR(PL_reg_ganch); /* from regexec.c */
4638 SAVESPTR(PL_reg_sv); /* from regexec.c */
4639 SAVEVPTR(PL_reg_magic); /* from regexec.c */
4640 SAVEI32(PL_reg_oldpos); /* from regexec.c */
4641 SAVEVPTR(PL_reg_oldcurpm); /* from regexec.c */
4642 SAVEVPTR(PL_reg_curpm); /* from regexec.c */
4643 SAVEI32(PL_regnpar); /* () count. */
4645 SAVEPPTR(PL_reg_starttry); /* from regexec.c */
4656 clear_re(pTHXo_ void *r)
4658 ReREFCNT_dec((regexp *)r);