5 * "A fair jaw-cracker dwarf-language must be." --Samwise Gamgee
8 /* NOTE: this is derived from Henry Spencer's regexp code, and should not
9 * confused with the original package (see point 3 below). Thanks, Henry!
12 /* Additional note: this code is very heavily munged from Henry's version
13 * in places. In some spots I've traded clarity for efficiency, so don't
14 * blame Henry for some of the lack of readability.
17 /* The names of the functions have been changed from regcomp and
18 * regexec to pregcomp and pregexec in order to avoid conflicts
19 * with the POSIX routines of the same names.
22 #ifdef PERL_EXT_RE_BUILD
23 /* need to replace pregcomp et al, so enable that */
24 # ifndef PERL_IN_XSUB_RE
25 # define PERL_IN_XSUB_RE
27 /* need access to debugger hooks */
28 # if defined(PERL_EXT_RE_DEBUG) && !defined(DEBUGGING)
33 #ifdef PERL_IN_XSUB_RE
34 /* We *really* need to overwrite these symbols: */
35 # define Perl_pregcomp my_regcomp
36 # define Perl_regdump my_regdump
37 # define Perl_regprop my_regprop
38 # define Perl_pregfree my_regfree
39 # define Perl_re_intuit_string my_re_intuit_string
40 /* *These* symbols are masked to allow static link. */
41 # define Perl_regnext my_regnext
42 # define Perl_save_re_context my_save_re_context
43 # define Perl_reginitcolors my_reginitcolors
45 # define PERL_NO_GET_CONTEXT
50 * pregcomp and pregexec -- regsub and regerror are not used in perl
52 * Copyright (c) 1986 by University of Toronto.
53 * Written by Henry Spencer. Not derived from licensed software.
55 * Permission is granted to anyone to use this software for any
56 * purpose on any computer system, and to redistribute it freely,
57 * subject to the following restrictions:
59 * 1. The author is not responsible for the consequences of use of
60 * this software, no matter how awful, even if they arise
63 * 2. The origin of this software must not be misrepresented, either
64 * by explicit claim or by omission.
66 * 3. Altered versions must be plainly marked as such, and must not
67 * be misrepresented as being the original software.
70 **** Alterations to Henry's code are...
72 **** Copyright (c) 1991-2001, Larry Wall
74 **** You may distribute under the terms of either the GNU General Public
75 **** License or the Artistic License, as specified in the README file.
78 * Beware that some of this code is subtly aware of the way operator
79 * precedence is structured in regular expressions. Serious changes in
80 * regular-expression syntax might require a total rethink.
83 #define PERL_IN_REGCOMP_C
86 #ifndef PERL_IN_XSUB_RE
98 # if defined(BUGGY_MSC6)
99 /* MSC 6.00A breaks on op/regexp.t test 85 unless we turn this off */
100 # pragma optimize("a",off)
101 /* But MSC 6.00A is happy with 'w', for aliases only across function calls*/
102 # pragma optimize("w",on )
103 # endif /* BUGGY_MSC6 */
107 #define STATIC static
110 typedef struct RExC_state_t {
111 U16 flags16; /* are we folding, multilining? */
112 char *precomp; /* uncompiled string. */
114 char *start; /* Start of input for compile */
115 char *end; /* End of input for compile */
116 char *parse; /* Input-scan pointer. */
117 I32 whilem_seen; /* number of WHILEM in this expr */
118 regnode *emit_start; /* Start of emitted-code area */
119 regnode *emit; /* Code-emit pointer; ®dummy = don't = compiling */
120 I32 naughty; /* How bad is this pattern? */
121 I32 sawback; /* Did we see \1, ...? */
123 I32 size; /* Code size. */
124 I32 npar; /* () count. */
130 char *starttry; /* -Dr: where regtry was called. */
131 #define RExC_starttry (pRExC_state->starttry)
135 #define RExC_flags16 (pRExC_state->flags16)
136 #define RExC_precomp (pRExC_state->precomp)
137 #define RExC_rx (pRExC_state->rx)
138 #define RExC_start (pRExC_state->start)
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_offsets (pRExC_state->rx->offsets) /* I am not like the others */
143 #define RExC_emit (pRExC_state->emit)
144 #define RExC_emit_start (pRExC_state->emit_start)
145 #define RExC_naughty (pRExC_state->naughty)
146 #define RExC_sawback (pRExC_state->sawback)
147 #define RExC_seen (pRExC_state->seen)
148 #define RExC_size (pRExC_state->size)
149 #define RExC_npar (pRExC_state->npar)
150 #define RExC_extralen (pRExC_state->extralen)
151 #define RExC_seen_zerolen (pRExC_state->seen_zerolen)
152 #define RExC_seen_evals (pRExC_state->seen_evals)
153 #define RExC_utf8 (pRExC_state->utf8)
155 #define ISMULT1(c) ((c) == '*' || (c) == '+' || (c) == '?')
156 #define ISMULT2(s) ((*s) == '*' || (*s) == '+' || (*s) == '?' || \
157 ((*s) == '{' && regcurly(s)))
160 #undef SPSTART /* dratted cpp namespace... */
163 * Flags to be passed up and down.
165 #define WORST 0 /* Worst case. */
166 #define HASWIDTH 0x1 /* Known to match non-null strings. */
167 #define SIMPLE 0x2 /* Simple enough to be STAR/PLUS operand. */
168 #define SPSTART 0x4 /* Starts with * or +. */
169 #define TRYAGAIN 0x8 /* Weeded out a declaration. */
171 /* Length of a variant. */
173 typedef struct scan_data_t {
179 I32 last_end; /* min value, <0 unless valid. */
182 SV **longest; /* Either &l_fixed, or &l_float. */
186 I32 offset_float_min;
187 I32 offset_float_max;
191 struct regnode_charclass_class *start_class;
195 * Forward declarations for pregcomp()'s friends.
198 static scan_data_t zero_scan_data = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
201 #define SF_BEFORE_EOL (SF_BEFORE_SEOL|SF_BEFORE_MEOL)
202 #define SF_BEFORE_SEOL 0x1
203 #define SF_BEFORE_MEOL 0x2
204 #define SF_FIX_BEFORE_EOL (SF_FIX_BEFORE_SEOL|SF_FIX_BEFORE_MEOL)
205 #define SF_FL_BEFORE_EOL (SF_FL_BEFORE_SEOL|SF_FL_BEFORE_MEOL)
208 # define SF_FIX_SHIFT_EOL (0+2)
209 # define SF_FL_SHIFT_EOL (0+4)
211 # define SF_FIX_SHIFT_EOL (+2)
212 # define SF_FL_SHIFT_EOL (+4)
215 #define SF_FIX_BEFORE_SEOL (SF_BEFORE_SEOL << SF_FIX_SHIFT_EOL)
216 #define SF_FIX_BEFORE_MEOL (SF_BEFORE_MEOL << SF_FIX_SHIFT_EOL)
218 #define SF_FL_BEFORE_SEOL (SF_BEFORE_SEOL << SF_FL_SHIFT_EOL)
219 #define SF_FL_BEFORE_MEOL (SF_BEFORE_MEOL << SF_FL_SHIFT_EOL) /* 0x20 */
220 #define SF_IS_INF 0x40
221 #define SF_HAS_PAR 0x80
222 #define SF_IN_PAR 0x100
223 #define SF_HAS_EVAL 0x200
224 #define SCF_DO_SUBSTR 0x400
225 #define SCF_DO_STCLASS_AND 0x0800
226 #define SCF_DO_STCLASS_OR 0x1000
227 #define SCF_DO_STCLASS (SCF_DO_STCLASS_AND|SCF_DO_STCLASS_OR)
228 #define SCF_WHILEM_VISITED_POS 0x2000
230 #define UTF RExC_utf8
231 #define LOC (RExC_flags16 & PMf_LOCALE)
232 #define FOLD (RExC_flags16 & PMf_FOLD)
234 #define OOB_UNICODE 12345678
235 #define OOB_NAMEDCLASS -1
237 #define CHR_SVLEN(sv) (UTF ? sv_len_utf8(sv) : SvCUR(sv))
238 #define CHR_DIST(a,b) (UTF ? utf8_distance(a,b) : a - b)
241 /* length of regex to show in messages that don't mark a position within */
242 #define RegexLengthToShowInErrorMessages 127
245 * If MARKER[12] are adjusted, be sure to adjust the constants at the top
246 * of t/op/regmesg.t, the tests in t/op/re_tests, and those in
247 * op/pragma/warn/regcomp.
249 #define MARKER1 "<-- HERE" /* marker as it appears in the description */
250 #define MARKER2 " <-- HERE " /* marker as it appears within the regex */
252 #define REPORT_LOCATION " in regex; marked by " MARKER1 " in m/%.*s" MARKER2 "%s/"
255 * Calls SAVEDESTRUCTOR_X if needed, then calls Perl_croak with the given
256 * arg. Show regex, up to a maximum length. If it's too long, chop and add
261 char *ellipses = ""; \
262 IV len = RExC_end - RExC_precomp; \
265 SAVEDESTRUCTOR_X(clear_re,(void*)RExC_rx); \
267 if (len > RegexLengthToShowInErrorMessages) { \
268 /* chop 10 shorter than the max, to ensure meaning of "..." */ \
269 len = RegexLengthToShowInErrorMessages - 10; \
272 Perl_croak(aTHX_ "%s in regex m/%.*s%s/", \
273 msg, (int)len, RExC_precomp, ellipses); \
277 * Calls SAVEDESTRUCTOR_X if needed, then calls Perl_croak with the given
278 * args. Show regex, up to a maximum length. If it's too long, chop and add
281 #define FAIL2(pat,msg) \
283 char *ellipses = ""; \
284 IV len = RExC_end - RExC_precomp; \
287 SAVEDESTRUCTOR_X(clear_re,(void*)RExC_rx); \
289 if (len > RegexLengthToShowInErrorMessages) { \
290 /* chop 10 shorter than the max, to ensure meaning of "..." */ \
291 len = RegexLengthToShowInErrorMessages - 10; \
294 S_re_croak2(aTHX_ pat, " in regex m/%.*s%s/", \
295 msg, (int)len, RExC_precomp, ellipses); \
300 * Simple_vFAIL -- like FAIL, but marks the current location in the scan
302 #define Simple_vFAIL(m) \
304 IV offset = RExC_parse - RExC_precomp; \
306 Perl_croak(aTHX_ "%s" REPORT_LOCATION, \
307 m, (int)offset, RExC_precomp, RExC_precomp + offset); \
311 * Calls SAVEDESTRUCTOR_X if needed, then Simple_vFAIL()
316 SAVEDESTRUCTOR_X(clear_re,(void*)RExC_rx); \
321 * Like Simple_vFAIL(), but accepts two arguments.
323 #define Simple_vFAIL2(m,a1) \
325 IV offset = RExC_parse - RExC_precomp; \
327 S_re_croak2(aTHX_ m, REPORT_LOCATION, a1, \
328 (int)offset, RExC_precomp, RExC_precomp + offset); \
332 * Calls SAVEDESTRUCTOR_X if needed, then Simple_vFAIL2().
334 #define vFAIL2(m,a1) \
337 SAVEDESTRUCTOR_X(clear_re,(void*)RExC_rx); \
338 Simple_vFAIL2(m, a1); \
343 * Like Simple_vFAIL(), but accepts three arguments.
345 #define Simple_vFAIL3(m, a1, a2) \
347 IV offset = RExC_parse - RExC_precomp; \
349 S_re_croak2(aTHX_ m, REPORT_LOCATION, a1, a2, \
350 (int)offset, RExC_precomp, RExC_precomp + offset); \
354 * Calls SAVEDESTRUCTOR_X if needed, then Simple_vFAIL3().
356 #define vFAIL3(m,a1,a2) \
359 SAVEDESTRUCTOR_X(clear_re,(void*)RExC_rx); \
360 Simple_vFAIL3(m, a1, a2); \
364 * Like Simple_vFAIL(), but accepts four arguments.
366 #define Simple_vFAIL4(m, a1, a2, a3) \
368 IV offset = RExC_parse - RExC_precomp; \
370 S_re_croak2(aTHX_ m, REPORT_LOCATION, a1, a2, a3,\
371 (int)offset, RExC_precomp, RExC_precomp + offset); \
375 * Like Simple_vFAIL(), but accepts five arguments.
377 #define Simple_vFAIL5(m, a1, a2, a3, a4) \
379 IV offset = RExC_parse - RExC_precomp; \
380 S_re_croak2(aTHX_ m, REPORT_LOCATION, a1, a2, a3, a4,\
381 (int)offset, RExC_precomp, RExC_precomp + offset); \
385 #define vWARN(loc,m) \
387 IV offset = loc - RExC_precomp; \
388 Perl_warner(aTHX_ WARN_REGEXP, "%s" REPORT_LOCATION,\
389 m, (int)offset, RExC_precomp, RExC_precomp + offset); \
392 #define vWARNdep(loc,m) \
394 IV offset = loc - RExC_precomp; \
395 int warn_cat = ckWARN(WARN_REGEXP) ? WARN_REGEXP : WARN_DEPRECATED; \
396 Perl_warner(aTHX_ warn_cat, "%s" REPORT_LOCATION,\
397 m, (int)offset, RExC_precomp, RExC_precomp + offset); \
401 #define vWARN2(loc, m, a1) \
403 IV offset = loc - RExC_precomp; \
404 Perl_warner(aTHX_ WARN_REGEXP, m REPORT_LOCATION,\
406 (int)offset, RExC_precomp, RExC_precomp + offset); \
409 #define vWARN3(loc, m, a1, a2) \
411 IV offset = loc - RExC_precomp; \
412 Perl_warner(aTHX_ WARN_REGEXP, m REPORT_LOCATION, \
414 (int)offset, RExC_precomp, RExC_precomp + offset); \
417 #define vWARN4(loc, m, a1, a2, a3) \
419 IV offset = loc - RExC_precomp; \
420 Perl_warner(aTHX_ WARN_REGEXP, m REPORT_LOCATION,\
422 (int)offset, RExC_precomp, RExC_precomp + offset); \
425 /* used for the parse_flags section for (?c) -- japhy */
426 #define vWARN5(loc, m, a1, a2, a3, a4) \
428 IV offset = loc - RExC_precomp; \
429 Perl_warner(aTHX_ WARN_REGEXP, m REPORT_LOCATION, \
431 (int)offset, RExC_precomp, RExC_precomp + offset); \
435 /* Allow for side effects in s */
436 #define REGC(c,s) STMT_START { if (!SIZE_ONLY) *(s) = (c); else (void)(s);} STMT_END
438 /* Macros for recording node offsets. 20001227 mjd@plover.com
439 * Nodes are numbered 1, 2, 3, 4. Node #n's position is recorded in
440 * element 2*n-1 of the array. Element #2n holds the byte length node #n.
441 * Element 0 holds the number n.
444 #define MJD_OFFSET_DEBUG(x)
445 /* #define MJD_OFFSET_DEBUG(x) fprintf x */
448 # define Set_Node_Offset_To_R(node,byte) \
452 Perl_croak(aTHX_ "value of node is %d in Offset macro", node); \
454 RExC_offsets[2*(node)-1] = (byte); \
459 # define Set_Node_Offset(node,byte) Set_Node_Offset_To_R((node)-RExC_emit_start, (byte)-RExC_start)
460 # define Set_Cur_Node_Offset Set_Node_Offset(RExC_emit, RExC_parse)
462 # define Set_Node_Length_To_R(node,len) \
465 MJD_OFFSET_DEBUG((stderr, "** (%d) size of node %d is %d.\n", __LINE__, (node), (len))); \
467 Perl_croak(aTHX_ "value of node is %d in Length macro", node); \
469 RExC_offsets[2*(node)] = (len); \
474 # define Set_Node_Length(node,len) Set_Node_Length_To_R((node)-RExC_emit_start, len)
475 # define Set_Cur_Node_Length(len) Set_Node_Length(RExC_emit, len)
476 # define Set_Node_Cur_Length(node) Set_Node_Length(node, RExC_parse - parse_start)
478 /* Get offsets and lengths */
479 #define Node_Offset(n) (RExC_offsets[2*((n)-RExC_emit_start)-1])
480 #define Node_Length(n) (RExC_offsets[2*((n)-RExC_emit_start)])
482 static void clear_re(pTHX_ void *r);
484 /* Mark that we cannot extend a found fixed substring at this point.
485 Updata the longest found anchored substring and the longest found
486 floating substrings if needed. */
489 S_scan_commit(pTHX_ RExC_state_t *pRExC_state, scan_data_t *data)
491 STRLEN l = CHR_SVLEN(data->last_found);
492 STRLEN old_l = CHR_SVLEN(*data->longest);
494 if ((l >= old_l) && ((l > old_l) || (data->flags & SF_BEFORE_EOL))) {
495 sv_setsv(*data->longest, data->last_found);
496 if (*data->longest == data->longest_fixed) {
497 data->offset_fixed = l ? data->last_start_min : data->pos_min;
498 if (data->flags & SF_BEFORE_EOL)
500 |= ((data->flags & SF_BEFORE_EOL) << SF_FIX_SHIFT_EOL);
502 data->flags &= ~SF_FIX_BEFORE_EOL;
505 data->offset_float_min = l ? data->last_start_min : data->pos_min;
506 data->offset_float_max = (l
507 ? data->last_start_max
508 : data->pos_min + data->pos_delta);
509 if (data->flags & SF_BEFORE_EOL)
511 |= ((data->flags & SF_BEFORE_EOL) << SF_FL_SHIFT_EOL);
513 data->flags &= ~SF_FL_BEFORE_EOL;
516 SvCUR_set(data->last_found, 0);
518 data->flags &= ~SF_BEFORE_EOL;
521 /* Can match anything (initialization) */
523 S_cl_anything(pTHX_ RExC_state_t *pRExC_state, struct regnode_charclass_class *cl)
527 ANYOF_CLASS_ZERO(cl);
528 for (value = 0; value < 256; ++value)
529 ANYOF_BITMAP_SET(cl, value);
530 cl->flags = ANYOF_EOS|ANYOF_UNICODE_ALL;
532 cl->flags |= ANYOF_LOCALE;
535 /* Can match anything (initialization) */
537 S_cl_is_anything(pTHX_ struct regnode_charclass_class *cl)
541 for (value = 0; value <= ANYOF_MAX; value += 2)
542 if (ANYOF_CLASS_TEST(cl, value) && ANYOF_CLASS_TEST(cl, value + 1))
544 if (!(cl->flags & ANYOF_UNICODE_ALL))
546 for (value = 0; value < 256; ++value)
547 if (!ANYOF_BITMAP_TEST(cl, value))
552 /* Can match anything (initialization) */
554 S_cl_init(pTHX_ RExC_state_t *pRExC_state, struct regnode_charclass_class *cl)
556 Zero(cl, 1, struct regnode_charclass_class);
558 cl_anything(pRExC_state, cl);
562 S_cl_init_zero(pTHX_ RExC_state_t *pRExC_state, struct regnode_charclass_class *cl)
564 Zero(cl, 1, struct regnode_charclass_class);
566 cl_anything(pRExC_state, cl);
568 cl->flags |= ANYOF_LOCALE;
571 /* 'And' a given class with another one. Can create false positives */
572 /* We assume that cl is not inverted */
574 S_cl_and(pTHX_ struct regnode_charclass_class *cl,
575 struct regnode_charclass_class *and_with)
577 if (!(and_with->flags & ANYOF_CLASS)
578 && !(cl->flags & ANYOF_CLASS)
579 && (and_with->flags & ANYOF_LOCALE) == (cl->flags & ANYOF_LOCALE)
580 && !(and_with->flags & ANYOF_FOLD)
581 && !(cl->flags & ANYOF_FOLD)) {
584 if (and_with->flags & ANYOF_INVERT)
585 for (i = 0; i < ANYOF_BITMAP_SIZE; i++)
586 cl->bitmap[i] &= ~and_with->bitmap[i];
588 for (i = 0; i < ANYOF_BITMAP_SIZE; i++)
589 cl->bitmap[i] &= and_with->bitmap[i];
590 } /* XXXX: logic is complicated otherwise, leave it along for a moment. */
591 if (!(and_with->flags & ANYOF_EOS))
592 cl->flags &= ~ANYOF_EOS;
594 if (cl->flags & ANYOF_UNICODE_ALL && and_with->flags & ANYOF_UNICODE) {
595 cl->flags &= ~ANYOF_UNICODE_ALL;
596 cl->flags |= ANYOF_UNICODE;
597 ARG_SET(cl, ARG(and_with));
599 if (!(and_with->flags & ANYOF_UNICODE_ALL))
600 cl->flags &= ~ANYOF_UNICODE_ALL;
601 if (!(and_with->flags & (ANYOF_UNICODE|ANYOF_UNICODE_ALL)))
602 cl->flags &= ~ANYOF_UNICODE;
605 /* 'OR' a given class with another one. Can create false positives */
606 /* We assume that cl is not inverted */
608 S_cl_or(pTHX_ RExC_state_t *pRExC_state, struct regnode_charclass_class *cl, struct regnode_charclass_class *or_with)
610 if (or_with->flags & ANYOF_INVERT) {
612 * (B1 | CL1) | (!B2 & !CL2) = (B1 | !B2 & !CL2) | (CL1 | (!B2 & !CL2))
613 * <= (B1 | !B2) | (CL1 | !CL2)
614 * which is wasteful if CL2 is small, but we ignore CL2:
615 * (B1 | CL1) | (!B2 & !CL2) <= (B1 | CL1) | !B2 = (B1 | !B2) | CL1
616 * XXXX Can we handle case-fold? Unclear:
617 * (OK1(i) | OK1(i')) | !(OK1(i) | OK1(i')) =
618 * (OK1(i) | OK1(i')) | (!OK1(i) & !OK1(i'))
620 if ( (or_with->flags & ANYOF_LOCALE) == (cl->flags & ANYOF_LOCALE)
621 && !(or_with->flags & ANYOF_FOLD)
622 && !(cl->flags & ANYOF_FOLD) ) {
625 for (i = 0; i < ANYOF_BITMAP_SIZE; i++)
626 cl->bitmap[i] |= ~or_with->bitmap[i];
627 } /* XXXX: logic is complicated otherwise */
629 cl_anything(pRExC_state, cl);
632 /* (B1 | CL1) | (B2 | CL2) = (B1 | B2) | (CL1 | CL2)) */
633 if ( (or_with->flags & ANYOF_LOCALE) == (cl->flags & ANYOF_LOCALE)
634 && (!(or_with->flags & ANYOF_FOLD)
635 || (cl->flags & ANYOF_FOLD)) ) {
638 /* OR char bitmap and class bitmap separately */
639 for (i = 0; i < ANYOF_BITMAP_SIZE; i++)
640 cl->bitmap[i] |= or_with->bitmap[i];
641 if (or_with->flags & ANYOF_CLASS) {
642 for (i = 0; i < ANYOF_CLASSBITMAP_SIZE; i++)
643 cl->classflags[i] |= or_with->classflags[i];
644 cl->flags |= ANYOF_CLASS;
647 else { /* XXXX: logic is complicated, leave it along for a moment. */
648 cl_anything(pRExC_state, cl);
651 if (or_with->flags & ANYOF_EOS)
652 cl->flags |= ANYOF_EOS;
654 if (cl->flags & ANYOF_UNICODE && or_with->flags & ANYOF_UNICODE &&
655 ARG(cl) != ARG(or_with)) {
656 cl->flags |= ANYOF_UNICODE_ALL;
657 cl->flags &= ~ANYOF_UNICODE;
659 if (or_with->flags & ANYOF_UNICODE_ALL) {
660 cl->flags |= ANYOF_UNICODE_ALL;
661 cl->flags &= ~ANYOF_UNICODE;
665 /* REx optimizer. Converts nodes into quickier variants "in place".
666 Finds fixed substrings. */
668 /* Stops at toplevel WHILEM as well as at `last'. At end *scanp is set
669 to the position after last scanned or to NULL. */
672 S_study_chunk(pTHX_ RExC_state_t *pRExC_state, regnode **scanp, I32 *deltap, regnode *last, scan_data_t *data, U32 flags)
673 /* scanp: Start here (read-write). */
674 /* deltap: Write maxlen-minlen here. */
675 /* last: Stop before this one. */
677 I32 min = 0, pars = 0, code;
678 regnode *scan = *scanp, *next;
680 int is_inf = (flags & SCF_DO_SUBSTR) && (data->flags & SF_IS_INF);
681 int is_inf_internal = 0; /* The studied chunk is infinite */
682 I32 is_par = OP(scan) == OPEN ? ARG(scan) : 0;
683 scan_data_t data_fake;
684 struct regnode_charclass_class and_with; /* Valid if flags & SCF_DO_STCLASS_OR */
686 while (scan && OP(scan) != END && scan < last) {
687 /* Peephole optimizer: */
689 if (PL_regkind[(U8)OP(scan)] == EXACT) {
690 /* Merge several consecutive EXACTish nodes into one. */
691 regnode *n = regnext(scan);
694 regnode *stop = scan;
697 next = scan + NODE_SZ_STR(scan);
698 /* Skip NOTHING, merge EXACT*. */
700 ( PL_regkind[(U8)OP(n)] == NOTHING ||
701 (stringok && (OP(n) == OP(scan))))
703 && NEXT_OFF(scan) + NEXT_OFF(n) < I16_MAX) {
704 if (OP(n) == TAIL || n > next)
706 if (PL_regkind[(U8)OP(n)] == NOTHING) {
707 NEXT_OFF(scan) += NEXT_OFF(n);
708 next = n + NODE_STEP_REGNODE;
716 int oldl = STR_LEN(scan);
717 regnode *nnext = regnext(n);
719 if (oldl + STR_LEN(n) > U8_MAX)
721 NEXT_OFF(scan) += NEXT_OFF(n);
722 STR_LEN(scan) += STR_LEN(n);
723 next = n + NODE_SZ_STR(n);
724 /* Now we can overwrite *n : */
725 Move(STRING(n), STRING(scan) + oldl, STR_LEN(n), char);
734 n = scan + NODE_SZ_STR(scan);
736 if (PL_regkind[(U8)OP(n)] != NOTHING || OP(n) == NOTHING) {
744 /* Follow the next-chain of the current node and optimize
745 away all the NOTHINGs from it. */
746 if (OP(scan) != CURLYX) {
747 int max = (reg_off_by_arg[OP(scan)]
749 /* I32 may be smaller than U16 on CRAYs! */
750 : (I32_MAX < U16_MAX ? I32_MAX : U16_MAX));
751 int off = (reg_off_by_arg[OP(scan)] ? ARG(scan) : NEXT_OFF(scan));
755 /* Skip NOTHING and LONGJMP. */
756 while ((n = regnext(n))
757 && ((PL_regkind[(U8)OP(n)] == NOTHING && (noff = NEXT_OFF(n)))
758 || ((OP(n) == LONGJMP) && (noff = ARG(n))))
761 if (reg_off_by_arg[OP(scan)])
764 NEXT_OFF(scan) = off;
766 /* The principal pseudo-switch. Cannot be a switch, since we
767 look into several different things. */
768 if (OP(scan) == BRANCH || OP(scan) == BRANCHJ
769 || OP(scan) == IFTHEN || OP(scan) == SUSPEND) {
770 next = regnext(scan);
773 if (OP(next) == code || code == IFTHEN || code == SUSPEND) {
774 I32 max1 = 0, min1 = I32_MAX, num = 0;
775 struct regnode_charclass_class accum;
777 if (flags & SCF_DO_SUBSTR) /* XXXX Add !SUSPEND? */
778 scan_commit(pRExC_state, data); /* Cannot merge strings after this. */
779 if (flags & SCF_DO_STCLASS)
780 cl_init_zero(pRExC_state, &accum);
781 while (OP(scan) == code) {
782 I32 deltanext, minnext, f = 0, fake;
783 struct regnode_charclass_class this_class;
788 data_fake.whilem_c = data->whilem_c;
789 data_fake.last_closep = data->last_closep;
792 data_fake.last_closep = &fake;
793 next = regnext(scan);
794 scan = NEXTOPER(scan);
796 scan = NEXTOPER(scan);
797 if (flags & SCF_DO_STCLASS) {
798 cl_init(pRExC_state, &this_class);
799 data_fake.start_class = &this_class;
800 f = SCF_DO_STCLASS_AND;
802 if (flags & SCF_WHILEM_VISITED_POS)
803 f |= SCF_WHILEM_VISITED_POS;
804 /* we suppose the run is continuous, last=next...*/
805 minnext = study_chunk(pRExC_state, &scan, &deltanext,
806 next, &data_fake, f);
809 if (max1 < minnext + deltanext)
810 max1 = minnext + deltanext;
811 if (deltanext == I32_MAX)
812 is_inf = is_inf_internal = 1;
814 if (data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
816 if (data && (data_fake.flags & SF_HAS_EVAL))
817 data->flags |= SF_HAS_EVAL;
819 data->whilem_c = data_fake.whilem_c;
820 if (flags & SCF_DO_STCLASS)
821 cl_or(pRExC_state, &accum, &this_class);
825 if (code == IFTHEN && num < 2) /* Empty ELSE branch */
827 if (flags & SCF_DO_SUBSTR) {
828 data->pos_min += min1;
829 data->pos_delta += max1 - min1;
830 if (max1 != min1 || is_inf)
831 data->longest = &(data->longest_float);
834 delta += max1 - min1;
835 if (flags & SCF_DO_STCLASS_OR) {
836 cl_or(pRExC_state, data->start_class, &accum);
838 cl_and(data->start_class, &and_with);
839 flags &= ~SCF_DO_STCLASS;
842 else if (flags & SCF_DO_STCLASS_AND) {
844 cl_and(data->start_class, &accum);
845 flags &= ~SCF_DO_STCLASS;
848 /* Switch to OR mode: cache the old value of
849 * data->start_class */
850 StructCopy(data->start_class, &and_with,
851 struct regnode_charclass_class);
852 flags &= ~SCF_DO_STCLASS_AND;
853 StructCopy(&accum, data->start_class,
854 struct regnode_charclass_class);
855 flags |= SCF_DO_STCLASS_OR;
856 data->start_class->flags |= ANYOF_EOS;
860 else if (code == BRANCHJ) /* single branch is optimized. */
861 scan = NEXTOPER(NEXTOPER(scan));
862 else /* single branch is optimized. */
863 scan = NEXTOPER(scan);
866 else if (OP(scan) == EXACT) {
867 I32 l = STR_LEN(scan);
868 UV uc = *((U8*)STRING(scan));
870 U8 *s = (U8*)STRING(scan);
871 l = utf8_length(s, s + l);
872 uc = utf8_to_uvchr(s, NULL);
875 if (flags & SCF_DO_SUBSTR) { /* Update longest substr. */
876 /* The code below prefers earlier match for fixed
877 offset, later match for variable offset. */
878 if (data->last_end == -1) { /* Update the start info. */
879 data->last_start_min = data->pos_min;
880 data->last_start_max = is_inf
881 ? I32_MAX : data->pos_min + data->pos_delta;
883 sv_catpvn(data->last_found, STRING(scan), STR_LEN(scan));
884 data->last_end = data->pos_min + l;
885 data->pos_min += l; /* As in the first entry. */
886 data->flags &= ~SF_BEFORE_EOL;
888 if (flags & SCF_DO_STCLASS_AND) {
889 /* Check whether it is compatible with what we know already! */
893 (!(data->start_class->flags & (ANYOF_CLASS | ANYOF_LOCALE))
894 && !ANYOF_BITMAP_TEST(data->start_class, uc)
895 && (!(data->start_class->flags & ANYOF_FOLD)
896 || !ANYOF_BITMAP_TEST(data->start_class, PL_fold[uc])))
899 ANYOF_CLASS_ZERO(data->start_class);
900 ANYOF_BITMAP_ZERO(data->start_class);
902 ANYOF_BITMAP_SET(data->start_class, uc);
903 data->start_class->flags &= ~ANYOF_EOS;
905 data->start_class->flags &= ~ANYOF_UNICODE_ALL;
907 else if (flags & SCF_DO_STCLASS_OR) {
908 /* false positive possible if the class is case-folded */
910 ANYOF_BITMAP_SET(data->start_class, uc);
912 data->start_class->flags |= ANYOF_UNICODE_ALL;
913 data->start_class->flags &= ~ANYOF_EOS;
914 cl_and(data->start_class, &and_with);
916 flags &= ~SCF_DO_STCLASS;
918 else if (PL_regkind[(U8)OP(scan)] == EXACT) { /* But OP != EXACT! */
919 I32 l = STR_LEN(scan);
920 UV uc = *((U8*)STRING(scan));
922 /* Search for fixed substrings supports EXACT only. */
923 if (flags & SCF_DO_SUBSTR)
924 scan_commit(pRExC_state, data);
926 U8 *s = (U8 *)STRING(scan);
927 l = utf8_length(s, s + l);
928 uc = utf8_to_uvchr(s, NULL);
931 if (data && (flags & SCF_DO_SUBSTR))
933 if (flags & SCF_DO_STCLASS_AND) {
934 /* Check whether it is compatible with what we know already! */
938 (!(data->start_class->flags & (ANYOF_CLASS | ANYOF_LOCALE))
939 && !ANYOF_BITMAP_TEST(data->start_class, uc)
940 && !ANYOF_BITMAP_TEST(data->start_class, PL_fold[uc])))
942 ANYOF_CLASS_ZERO(data->start_class);
943 ANYOF_BITMAP_ZERO(data->start_class);
945 ANYOF_BITMAP_SET(data->start_class, uc);
946 data->start_class->flags &= ~ANYOF_EOS;
947 data->start_class->flags |= ANYOF_FOLD;
948 if (OP(scan) == EXACTFL)
949 data->start_class->flags |= ANYOF_LOCALE;
952 else if (flags & SCF_DO_STCLASS_OR) {
953 if (data->start_class->flags & ANYOF_FOLD) {
954 /* false positive possible if the class is case-folded.
955 Assume that the locale settings are the same... */
957 ANYOF_BITMAP_SET(data->start_class, uc);
958 data->start_class->flags &= ~ANYOF_EOS;
960 cl_and(data->start_class, &and_with);
962 flags &= ~SCF_DO_STCLASS;
964 else if (strchr((char*)PL_varies,OP(scan))) {
965 I32 mincount, maxcount, minnext, deltanext, fl = 0;
966 I32 f = flags, pos_before = 0;
967 regnode *oscan = scan;
968 struct regnode_charclass_class this_class;
969 struct regnode_charclass_class *oclass = NULL;
970 I32 next_is_eval = 0;
972 switch (PL_regkind[(U8)OP(scan)]) {
973 case WHILEM: /* End of (?:...)* . */
974 scan = NEXTOPER(scan);
977 if (flags & (SCF_DO_SUBSTR | SCF_DO_STCLASS)) {
978 next = NEXTOPER(scan);
979 if (OP(next) == EXACT || (flags & SCF_DO_STCLASS)) {
981 maxcount = REG_INFTY;
982 next = regnext(scan);
983 scan = NEXTOPER(scan);
987 if (flags & SCF_DO_SUBSTR)
992 if (flags & SCF_DO_STCLASS) {
994 maxcount = REG_INFTY;
995 next = regnext(scan);
996 scan = NEXTOPER(scan);
999 is_inf = is_inf_internal = 1;
1000 scan = regnext(scan);
1001 if (flags & SCF_DO_SUBSTR) {
1002 scan_commit(pRExC_state, data); /* Cannot extend fixed substrings */
1003 data->longest = &(data->longest_float);
1005 goto optimize_curly_tail;
1007 mincount = ARG1(scan);
1008 maxcount = ARG2(scan);
1009 next = regnext(scan);
1010 if (OP(scan) == CURLYX) {
1011 I32 lp = (data ? *(data->last_closep) : 0);
1013 scan->flags = ((lp <= U8_MAX) ? lp : U8_MAX);
1015 scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
1016 next_is_eval = (OP(scan) == EVAL);
1018 if (flags & SCF_DO_SUBSTR) {
1019 if (mincount == 0) scan_commit(pRExC_state,data); /* Cannot extend fixed substrings */
1020 pos_before = data->pos_min;
1024 data->flags &= ~(SF_HAS_PAR|SF_IN_PAR|SF_HAS_EVAL);
1026 data->flags |= SF_IS_INF;
1028 if (flags & SCF_DO_STCLASS) {
1029 cl_init(pRExC_state, &this_class);
1030 oclass = data->start_class;
1031 data->start_class = &this_class;
1032 f |= SCF_DO_STCLASS_AND;
1033 f &= ~SCF_DO_STCLASS_OR;
1035 /* These are the cases when once a subexpression
1036 fails at a particular position, it cannot succeed
1037 even after backtracking at the enclosing scope.
1039 XXXX what if minimal match and we are at the
1040 initial run of {n,m}? */
1041 if ((mincount != maxcount - 1) && (maxcount != REG_INFTY))
1042 f &= ~SCF_WHILEM_VISITED_POS;
1044 /* This will finish on WHILEM, setting scan, or on NULL: */
1045 minnext = study_chunk(pRExC_state, &scan, &deltanext, last, data,
1047 ? (f & ~SCF_DO_SUBSTR) : f);
1049 if (flags & SCF_DO_STCLASS)
1050 data->start_class = oclass;
1051 if (mincount == 0 || minnext == 0) {
1052 if (flags & SCF_DO_STCLASS_OR) {
1053 cl_or(pRExC_state, data->start_class, &this_class);
1055 else if (flags & SCF_DO_STCLASS_AND) {
1056 /* Switch to OR mode: cache the old value of
1057 * data->start_class */
1058 StructCopy(data->start_class, &and_with,
1059 struct regnode_charclass_class);
1060 flags &= ~SCF_DO_STCLASS_AND;
1061 StructCopy(&this_class, data->start_class,
1062 struct regnode_charclass_class);
1063 flags |= SCF_DO_STCLASS_OR;
1064 data->start_class->flags |= ANYOF_EOS;
1066 } else { /* Non-zero len */
1067 if (flags & SCF_DO_STCLASS_OR) {
1068 cl_or(pRExC_state, data->start_class, &this_class);
1069 cl_and(data->start_class, &and_with);
1071 else if (flags & SCF_DO_STCLASS_AND)
1072 cl_and(data->start_class, &this_class);
1073 flags &= ~SCF_DO_STCLASS;
1075 if (!scan) /* It was not CURLYX, but CURLY. */
1077 if (ckWARN(WARN_REGEXP)
1078 /* ? quantifier ok, except for (?{ ... }) */
1079 && (next_is_eval || !(mincount == 0 && maxcount == 1))
1080 && (minnext == 0) && (deltanext == 0)
1081 && data && !(data->flags & (SF_HAS_PAR|SF_IN_PAR))
1082 && maxcount <= REG_INFTY/3) /* Complement check for big count */
1085 "Quantifier unexpected on zero-length expression");
1088 min += minnext * mincount;
1089 is_inf_internal |= ((maxcount == REG_INFTY
1090 && (minnext + deltanext) > 0)
1091 || deltanext == I32_MAX);
1092 is_inf |= is_inf_internal;
1093 delta += (minnext + deltanext) * maxcount - minnext * mincount;
1095 /* Try powerful optimization CURLYX => CURLYN. */
1096 if ( OP(oscan) == CURLYX && data
1097 && data->flags & SF_IN_PAR
1098 && !(data->flags & SF_HAS_EVAL)
1099 && !deltanext && minnext == 1 ) {
1100 /* Try to optimize to CURLYN. */
1101 regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS;
1102 regnode *nxt1 = nxt;
1109 if (!strchr((char*)PL_simple,OP(nxt))
1110 && !(PL_regkind[(U8)OP(nxt)] == EXACT
1111 && STR_LEN(nxt) == 1))
1117 if (OP(nxt) != CLOSE)
1119 /* Now we know that nxt2 is the only contents: */
1120 oscan->flags = ARG(nxt);
1122 OP(nxt1) = NOTHING; /* was OPEN. */
1124 OP(nxt1 + 1) = OPTIMIZED; /* was count. */
1125 NEXT_OFF(nxt1+ 1) = 0; /* just for consistancy. */
1126 NEXT_OFF(nxt2) = 0; /* just for consistancy with CURLY. */
1127 OP(nxt) = OPTIMIZED; /* was CLOSE. */
1128 OP(nxt + 1) = OPTIMIZED; /* was count. */
1129 NEXT_OFF(nxt+ 1) = 0; /* just for consistancy. */
1134 /* Try optimization CURLYX => CURLYM. */
1135 if ( OP(oscan) == CURLYX && data
1136 && !(data->flags & SF_HAS_PAR)
1137 && !(data->flags & SF_HAS_EVAL)
1139 /* XXXX How to optimize if data == 0? */
1140 /* Optimize to a simpler form. */
1141 regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN */
1145 while ( (nxt2 = regnext(nxt)) /* skip over embedded stuff*/
1146 && (OP(nxt2) != WHILEM))
1148 OP(nxt2) = SUCCEED; /* Whas WHILEM */
1149 /* Need to optimize away parenths. */
1150 if (data->flags & SF_IN_PAR) {
1151 /* Set the parenth number. */
1152 regnode *nxt1 = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN*/
1154 if (OP(nxt) != CLOSE)
1155 FAIL("Panic opt close");
1156 oscan->flags = ARG(nxt);
1157 OP(nxt1) = OPTIMIZED; /* was OPEN. */
1158 OP(nxt) = OPTIMIZED; /* was CLOSE. */
1160 OP(nxt1 + 1) = OPTIMIZED; /* was count. */
1161 OP(nxt + 1) = OPTIMIZED; /* was count. */
1162 NEXT_OFF(nxt1 + 1) = 0; /* just for consistancy. */
1163 NEXT_OFF(nxt + 1) = 0; /* just for consistancy. */
1166 while ( nxt1 && (OP(nxt1) != WHILEM)) {
1167 regnode *nnxt = regnext(nxt1);
1170 if (reg_off_by_arg[OP(nxt1)])
1171 ARG_SET(nxt1, nxt2 - nxt1);
1172 else if (nxt2 - nxt1 < U16_MAX)
1173 NEXT_OFF(nxt1) = nxt2 - nxt1;
1175 OP(nxt) = NOTHING; /* Cannot beautify */
1180 /* Optimize again: */
1181 study_chunk(pRExC_state, &nxt1, &deltanext, nxt,
1187 else if ((OP(oscan) == CURLYX)
1188 && (flags & SCF_WHILEM_VISITED_POS)
1189 /* See the comment on a similar expression above.
1190 However, this time it not a subexpression
1191 we care about, but the expression itself. */
1192 && (maxcount == REG_INFTY)
1193 && data && ++data->whilem_c < 16) {
1194 /* This stays as CURLYX, we can put the count/of pair. */
1195 /* Find WHILEM (as in regexec.c) */
1196 regnode *nxt = oscan + NEXT_OFF(oscan);
1198 if (OP(PREVOPER(nxt)) == NOTHING) /* LONGJMP */
1200 PREVOPER(nxt)->flags = data->whilem_c
1201 | (RExC_whilem_seen << 4); /* On WHILEM */
1203 if (data && fl & (SF_HAS_PAR|SF_IN_PAR))
1205 if (flags & SCF_DO_SUBSTR) {
1206 SV *last_str = Nullsv;
1207 int counted = mincount != 0;
1209 if (data->last_end > 0 && mincount != 0) { /* Ends with a string. */
1210 I32 b = pos_before >= data->last_start_min
1211 ? pos_before : data->last_start_min;
1213 char *s = SvPV(data->last_found, l);
1214 I32 old = b - data->last_start_min;
1217 old = utf8_hop((U8*)s, old) - (U8*)s;
1220 /* Get the added string: */
1221 last_str = newSVpvn(s + old, l);
1222 if (deltanext == 0 && pos_before == b) {
1223 /* What was added is a constant string */
1225 SvGROW(last_str, (mincount * l) + 1);
1226 repeatcpy(SvPVX(last_str) + l,
1227 SvPVX(last_str), l, mincount - 1);
1228 SvCUR(last_str) *= mincount;
1229 /* Add additional parts. */
1230 SvCUR_set(data->last_found,
1231 SvCUR(data->last_found) - l);
1232 sv_catsv(data->last_found, last_str);
1233 data->last_end += l * (mincount - 1);
1236 /* start offset must point into the last copy */
1237 data->last_start_min += minnext * (mincount - 1);
1238 data->last_start_max += is_inf ? 0 : (maxcount - 1)
1239 * (minnext + data->pos_delta);
1242 /* It is counted once already... */
1243 data->pos_min += minnext * (mincount - counted);
1244 data->pos_delta += - counted * deltanext +
1245 (minnext + deltanext) * maxcount - minnext * mincount;
1246 if (mincount != maxcount) {
1247 /* Cannot extend fixed substrings found inside
1249 scan_commit(pRExC_state,data);
1250 if (mincount && last_str) {
1251 sv_setsv(data->last_found, last_str);
1252 data->last_end = data->pos_min;
1253 data->last_start_min =
1254 data->pos_min - CHR_SVLEN(last_str);
1255 data->last_start_max = is_inf
1257 : data->pos_min + data->pos_delta
1258 - CHR_SVLEN(last_str);
1260 data->longest = &(data->longest_float);
1262 SvREFCNT_dec(last_str);
1264 if (data && (fl & SF_HAS_EVAL))
1265 data->flags |= SF_HAS_EVAL;
1266 optimize_curly_tail:
1267 if (OP(oscan) != CURLYX) {
1268 while (PL_regkind[(U8)OP(next = regnext(oscan))] == NOTHING
1270 NEXT_OFF(oscan) += NEXT_OFF(next);
1273 default: /* REF and CLUMP only? */
1274 if (flags & SCF_DO_SUBSTR) {
1275 scan_commit(pRExC_state,data); /* Cannot expect anything... */
1276 data->longest = &(data->longest_float);
1278 is_inf = is_inf_internal = 1;
1279 if (flags & SCF_DO_STCLASS_OR)
1280 cl_anything(pRExC_state, data->start_class);
1281 flags &= ~SCF_DO_STCLASS;
1285 else if (strchr((char*)PL_simple,OP(scan))) {
1288 if (flags & SCF_DO_SUBSTR) {
1289 scan_commit(pRExC_state,data);
1293 if (flags & SCF_DO_STCLASS) {
1294 data->start_class->flags &= ~ANYOF_EOS; /* No match on empty */
1296 /* Some of the logic below assumes that switching
1297 locale on will only add false positives. */
1298 switch (PL_regkind[(U8)OP(scan)]) {
1302 /* Perl_croak(aTHX_ "panic: unexpected simple REx opcode %d", OP(scan)); */
1303 if (flags & SCF_DO_STCLASS_OR) /* Allow everything */
1304 cl_anything(pRExC_state, data->start_class);
1307 if (OP(scan) == SANY)
1309 if (flags & SCF_DO_STCLASS_OR) { /* Everything but \n */
1310 value = (ANYOF_BITMAP_TEST(data->start_class,'\n')
1311 || (data->start_class->flags & ANYOF_CLASS));
1312 cl_anything(pRExC_state, data->start_class);
1314 if (flags & SCF_DO_STCLASS_AND || !value)
1315 ANYOF_BITMAP_CLEAR(data->start_class,'\n');
1318 if (flags & SCF_DO_STCLASS_AND)
1319 cl_and(data->start_class,
1320 (struct regnode_charclass_class*)scan);
1322 cl_or(pRExC_state, data->start_class,
1323 (struct regnode_charclass_class*)scan);
1326 if (flags & SCF_DO_STCLASS_AND) {
1327 if (!(data->start_class->flags & ANYOF_LOCALE)) {
1328 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NALNUM);
1329 for (value = 0; value < 256; value++)
1330 if (!isALNUM(value))
1331 ANYOF_BITMAP_CLEAR(data->start_class, value);
1335 if (data->start_class->flags & ANYOF_LOCALE)
1336 ANYOF_CLASS_SET(data->start_class,ANYOF_ALNUM);
1338 for (value = 0; value < 256; value++)
1340 ANYOF_BITMAP_SET(data->start_class, value);
1345 if (flags & SCF_DO_STCLASS_AND) {
1346 if (data->start_class->flags & ANYOF_LOCALE)
1347 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NALNUM);
1350 ANYOF_CLASS_SET(data->start_class,ANYOF_ALNUM);
1351 data->start_class->flags |= ANYOF_LOCALE;
1355 if (flags & SCF_DO_STCLASS_AND) {
1356 if (!(data->start_class->flags & ANYOF_LOCALE)) {
1357 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_ALNUM);
1358 for (value = 0; value < 256; value++)
1360 ANYOF_BITMAP_CLEAR(data->start_class, value);
1364 if (data->start_class->flags & ANYOF_LOCALE)
1365 ANYOF_CLASS_SET(data->start_class,ANYOF_NALNUM);
1367 for (value = 0; value < 256; value++)
1368 if (!isALNUM(value))
1369 ANYOF_BITMAP_SET(data->start_class, value);
1374 if (flags & SCF_DO_STCLASS_AND) {
1375 if (data->start_class->flags & ANYOF_LOCALE)
1376 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_ALNUM);
1379 data->start_class->flags |= ANYOF_LOCALE;
1380 ANYOF_CLASS_SET(data->start_class,ANYOF_NALNUM);
1384 if (flags & SCF_DO_STCLASS_AND) {
1385 if (!(data->start_class->flags & ANYOF_LOCALE)) {
1386 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NSPACE);
1387 for (value = 0; value < 256; value++)
1388 if (!isSPACE(value))
1389 ANYOF_BITMAP_CLEAR(data->start_class, value);
1393 if (data->start_class->flags & ANYOF_LOCALE)
1394 ANYOF_CLASS_SET(data->start_class,ANYOF_SPACE);
1396 for (value = 0; value < 256; value++)
1398 ANYOF_BITMAP_SET(data->start_class, value);
1403 if (flags & SCF_DO_STCLASS_AND) {
1404 if (data->start_class->flags & ANYOF_LOCALE)
1405 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NSPACE);
1408 data->start_class->flags |= ANYOF_LOCALE;
1409 ANYOF_CLASS_SET(data->start_class,ANYOF_SPACE);
1413 if (flags & SCF_DO_STCLASS_AND) {
1414 if (!(data->start_class->flags & ANYOF_LOCALE)) {
1415 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_SPACE);
1416 for (value = 0; value < 256; value++)
1418 ANYOF_BITMAP_CLEAR(data->start_class, value);
1422 if (data->start_class->flags & ANYOF_LOCALE)
1423 ANYOF_CLASS_SET(data->start_class,ANYOF_NSPACE);
1425 for (value = 0; value < 256; value++)
1426 if (!isSPACE(value))
1427 ANYOF_BITMAP_SET(data->start_class, value);
1432 if (flags & SCF_DO_STCLASS_AND) {
1433 if (data->start_class->flags & ANYOF_LOCALE) {
1434 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_SPACE);
1435 for (value = 0; value < 256; value++)
1436 if (!isSPACE(value))
1437 ANYOF_BITMAP_CLEAR(data->start_class, value);
1441 data->start_class->flags |= ANYOF_LOCALE;
1442 ANYOF_CLASS_SET(data->start_class,ANYOF_NSPACE);
1446 if (flags & SCF_DO_STCLASS_AND) {
1447 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NDIGIT);
1448 for (value = 0; value < 256; value++)
1449 if (!isDIGIT(value))
1450 ANYOF_BITMAP_CLEAR(data->start_class, value);
1453 if (data->start_class->flags & ANYOF_LOCALE)
1454 ANYOF_CLASS_SET(data->start_class,ANYOF_DIGIT);
1456 for (value = 0; value < 256; value++)
1458 ANYOF_BITMAP_SET(data->start_class, value);
1463 if (flags & SCF_DO_STCLASS_AND) {
1464 ANYOF_CLASS_CLEAR(data->start_class,ANYOF_DIGIT);
1465 for (value = 0; value < 256; value++)
1467 ANYOF_BITMAP_CLEAR(data->start_class, value);
1470 if (data->start_class->flags & ANYOF_LOCALE)
1471 ANYOF_CLASS_SET(data->start_class,ANYOF_NDIGIT);
1473 for (value = 0; value < 256; value++)
1474 if (!isDIGIT(value))
1475 ANYOF_BITMAP_SET(data->start_class, value);
1480 if (flags & SCF_DO_STCLASS_OR)
1481 cl_and(data->start_class, &and_with);
1482 flags &= ~SCF_DO_STCLASS;
1485 else if (PL_regkind[(U8)OP(scan)] == EOL && flags & SCF_DO_SUBSTR) {
1486 data->flags |= (OP(scan) == MEOL
1490 else if ( PL_regkind[(U8)OP(scan)] == BRANCHJ
1491 /* Lookbehind, or need to calculate parens/evals/stclass: */
1492 && (scan->flags || data || (flags & SCF_DO_STCLASS))
1493 && (OP(scan) == IFMATCH || OP(scan) == UNLESSM)) {
1494 /* Lookahead/lookbehind */
1495 I32 deltanext, minnext, fake = 0;
1497 struct regnode_charclass_class intrnl;
1500 data_fake.flags = 0;
1502 data_fake.whilem_c = data->whilem_c;
1503 data_fake.last_closep = data->last_closep;
1506 data_fake.last_closep = &fake;
1507 if ( flags & SCF_DO_STCLASS && !scan->flags
1508 && OP(scan) == IFMATCH ) { /* Lookahead */
1509 cl_init(pRExC_state, &intrnl);
1510 data_fake.start_class = &intrnl;
1511 f |= SCF_DO_STCLASS_AND;
1513 if (flags & SCF_WHILEM_VISITED_POS)
1514 f |= SCF_WHILEM_VISITED_POS;
1515 next = regnext(scan);
1516 nscan = NEXTOPER(NEXTOPER(scan));
1517 minnext = study_chunk(pRExC_state, &nscan, &deltanext, last, &data_fake, f);
1520 vFAIL("Variable length lookbehind not implemented");
1522 else if (minnext > U8_MAX) {
1523 vFAIL2("Lookbehind longer than %"UVuf" not implemented", (UV)U8_MAX);
1525 scan->flags = minnext;
1527 if (data && data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
1529 if (data && (data_fake.flags & SF_HAS_EVAL))
1530 data->flags |= SF_HAS_EVAL;
1532 data->whilem_c = data_fake.whilem_c;
1533 if (f & SCF_DO_STCLASS_AND) {
1534 int was = (data->start_class->flags & ANYOF_EOS);
1536 cl_and(data->start_class, &intrnl);
1538 data->start_class->flags |= ANYOF_EOS;
1541 else if (OP(scan) == OPEN) {
1544 else if (OP(scan) == CLOSE) {
1545 if (ARG(scan) == is_par) {
1546 next = regnext(scan);
1548 if ( next && (OP(next) != WHILEM) && next < last)
1549 is_par = 0; /* Disable optimization */
1552 *(data->last_closep) = ARG(scan);
1554 else if (OP(scan) == EVAL) {
1556 data->flags |= SF_HAS_EVAL;
1558 else if (OP(scan) == LOGICAL && scan->flags == 2) { /* Embedded follows */
1559 if (flags & SCF_DO_SUBSTR) {
1560 scan_commit(pRExC_state,data);
1561 data->longest = &(data->longest_float);
1563 is_inf = is_inf_internal = 1;
1564 if (flags & SCF_DO_STCLASS_OR) /* Allow everything */
1565 cl_anything(pRExC_state, data->start_class);
1566 flags &= ~SCF_DO_STCLASS;
1568 /* Else: zero-length, ignore. */
1569 scan = regnext(scan);
1574 *deltap = is_inf_internal ? I32_MAX : delta;
1575 if (flags & SCF_DO_SUBSTR && is_inf)
1576 data->pos_delta = I32_MAX - data->pos_min;
1577 if (is_par > U8_MAX)
1579 if (is_par && pars==1 && data) {
1580 data->flags |= SF_IN_PAR;
1581 data->flags &= ~SF_HAS_PAR;
1583 else if (pars && data) {
1584 data->flags |= SF_HAS_PAR;
1585 data->flags &= ~SF_IN_PAR;
1587 if (flags & SCF_DO_STCLASS_OR)
1588 cl_and(data->start_class, &and_with);
1593 S_add_data(pTHX_ RExC_state_t *pRExC_state, I32 n, char *s)
1595 if (RExC_rx->data) {
1596 Renewc(RExC_rx->data,
1597 sizeof(*RExC_rx->data) + sizeof(void*) * (RExC_rx->data->count + n - 1),
1598 char, struct reg_data);
1599 Renew(RExC_rx->data->what, RExC_rx->data->count + n, U8);
1600 RExC_rx->data->count += n;
1603 Newc(1207, RExC_rx->data, sizeof(*RExC_rx->data) + sizeof(void*) * (n - 1),
1604 char, struct reg_data);
1605 New(1208, RExC_rx->data->what, n, U8);
1606 RExC_rx->data->count = n;
1608 Copy(s, RExC_rx->data->what + RExC_rx->data->count - n, n, U8);
1609 return RExC_rx->data->count - n;
1613 Perl_reginitcolors(pTHX)
1616 char *s = PerlEnv_getenv("PERL_RE_COLORS");
1619 PL_colors[0] = s = savepv(s);
1621 s = strchr(s, '\t');
1627 PL_colors[i] = s = "";
1631 PL_colors[i++] = "";
1638 - pregcomp - compile a regular expression into internal code
1640 * We can't allocate space until we know how big the compiled form will be,
1641 * but we can't compile it (and thus know how big it is) until we've got a
1642 * place to put the code. So we cheat: we compile it twice, once with code
1643 * generation turned off and size counting turned on, and once "for real".
1644 * This also means that we don't allocate space until we are sure that the
1645 * thing really will compile successfully, and we never have to move the
1646 * code and thus invalidate pointers into it. (Note that it has to be in
1647 * one piece because free() must be able to free it all.) [NB: not true in perl]
1649 * Beware that the optimization-preparation code in here knows about some
1650 * of the structure of the compiled regexp. [I'll say.]
1653 Perl_pregcomp(pTHX_ char *exp, char *xend, PMOP *pm)
1663 RExC_state_t RExC_state;
1664 RExC_state_t *pRExC_state = &RExC_state;
1667 FAIL("NULL regexp argument");
1669 /* XXXX This looks very suspicious... */
1670 if (pm->op_pmdynflags & PMdf_CMP_UTF8)
1676 DEBUG_r(if (!PL_colorset) reginitcolors());
1677 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sCompiling REx%s `%s%*s%s'\n",
1678 PL_colors[4],PL_colors[5],PL_colors[0],
1679 (int)(xend - exp), RExC_precomp, PL_colors[1]));
1680 RExC_flags16 = pm->op_pmflags;
1684 RExC_seen_zerolen = *exp == '^' ? -1 : 0;
1685 RExC_seen_evals = 0;
1688 /* First pass: determine size, legality. */
1695 RExC_emit = &PL_regdummy;
1696 RExC_whilem_seen = 0;
1697 #if 0 /* REGC() is (currently) a NOP at the first pass.
1698 * Clever compilers notice this and complain. --jhi */
1699 REGC((U8)REG_MAGIC, (char*)RExC_emit);
1701 if (reg(pRExC_state, 0, &flags) == NULL) {
1702 RExC_precomp = Nullch;
1705 DEBUG_r(PerlIO_printf(Perl_debug_log, "size %"IVdf" ", (IV)RExC_size));
1707 /* Small enough for pointer-storage convention?
1708 If extralen==0, this means that we will not need long jumps. */
1709 if (RExC_size >= 0x10000L && RExC_extralen)
1710 RExC_size += RExC_extralen;
1713 if (RExC_whilem_seen > 15)
1714 RExC_whilem_seen = 15;
1716 /* Allocate space and initialize. */
1717 Newc(1001, r, sizeof(regexp) + (unsigned)RExC_size * sizeof(regnode),
1720 FAIL("Regexp out of space");
1723 /* avoid reading uninitialized memory in DEBUGGING code in study_chunk() */
1724 Zero(r, sizeof(regexp) + (unsigned)RExC_size * sizeof(regnode), char);
1727 r->prelen = xend - exp;
1728 r->precomp = savepvn(RExC_precomp, r->prelen);
1730 r->reganch = pm->op_pmflags & PMf_COMPILETIME;
1731 r->nparens = RExC_npar - 1; /* set early to validate backrefs */
1733 r->substrs = 0; /* Useful during FAIL. */
1734 r->startp = 0; /* Useful during FAIL. */
1735 r->endp = 0; /* Useful during FAIL. */
1737 Newz(1304, r->offsets, 2*RExC_size+1, U32); /* MJD 20001228 */
1739 r->offsets[0] = RExC_size;
1741 DEBUG_r(PerlIO_printf(Perl_debug_log,
1742 "%s %"UVuf" bytes for offset annotations.\n",
1743 r->offsets ? "Got" : "Couldn't get",
1744 (UV)((2*RExC_size+1) * sizeof(U32))));
1748 /* Second pass: emit code. */
1749 RExC_flags16 = pm->op_pmflags; /* don't let top level (?i) bleed */
1754 RExC_emit_start = r->program;
1755 RExC_emit = r->program;
1756 /* Store the count of eval-groups for security checks: */
1757 RExC_emit->next_off = ((RExC_seen_evals > U16_MAX) ? U16_MAX : RExC_seen_evals);
1758 REGC((U8)REG_MAGIC, (char*) RExC_emit++);
1760 if (reg(pRExC_state, 0, &flags) == NULL)
1763 /* Dig out information for optimizations. */
1764 r->reganch = pm->op_pmflags & PMf_COMPILETIME; /* Again? */
1765 pm->op_pmflags = RExC_flags16;
1767 r->reganch |= ROPT_UTF8; /* Unicode in it? */
1768 r->regstclass = NULL;
1769 if (RExC_naughty >= 10) /* Probably an expensive pattern. */
1770 r->reganch |= ROPT_NAUGHTY;
1771 scan = r->program + 1; /* First BRANCH. */
1773 /* XXXX To minimize changes to RE engine we always allocate
1774 3-units-long substrs field. */
1775 Newz(1004, r->substrs, 1, struct reg_substr_data);
1777 StructCopy(&zero_scan_data, &data, scan_data_t);
1778 /* XXXX Should not we check for something else? Usually it is OPEN1... */
1779 if (OP(scan) != BRANCH) { /* Only one top-level choice. */
1781 STRLEN longest_float_length, longest_fixed_length;
1782 struct regnode_charclass_class ch_class;
1787 /* Skip introductions and multiplicators >= 1. */
1788 while ((OP(first) == OPEN && (sawopen = 1)) ||
1789 /* An OR of *one* alternative - should not happen now. */
1790 (OP(first) == BRANCH && OP(regnext(first)) != BRANCH) ||
1791 (OP(first) == PLUS) ||
1792 (OP(first) == MINMOD) ||
1793 /* An {n,m} with n>0 */
1794 (PL_regkind[(U8)OP(first)] == CURLY && ARG1(first) > 0) ) {
1795 if (OP(first) == PLUS)
1798 first += regarglen[(U8)OP(first)];
1799 first = NEXTOPER(first);
1802 /* Starting-point info. */
1804 if (PL_regkind[(U8)OP(first)] == EXACT) {
1805 if (OP(first) == EXACT)
1806 ; /* Empty, get anchored substr later. */
1807 else if ((OP(first) == EXACTF || OP(first) == EXACTFL))
1808 r->regstclass = first;
1810 else if (strchr((char*)PL_simple,OP(first)))
1811 r->regstclass = first;
1812 else if (PL_regkind[(U8)OP(first)] == BOUND ||
1813 PL_regkind[(U8)OP(first)] == NBOUND)
1814 r->regstclass = first;
1815 else if (PL_regkind[(U8)OP(first)] == BOL) {
1816 r->reganch |= (OP(first) == MBOL
1818 : (OP(first) == SBOL
1821 first = NEXTOPER(first);
1824 else if (OP(first) == GPOS) {
1825 r->reganch |= ROPT_ANCH_GPOS;
1826 first = NEXTOPER(first);
1829 else if ((OP(first) == STAR &&
1830 PL_regkind[(U8)OP(NEXTOPER(first))] == REG_ANY) &&
1831 !(r->reganch & ROPT_ANCH) )
1833 /* turn .* into ^.* with an implied $*=1 */
1834 int type = OP(NEXTOPER(first));
1836 if (type == REG_ANY)
1837 type = ROPT_ANCH_MBOL;
1839 type = ROPT_ANCH_SBOL;
1841 r->reganch |= type | ROPT_IMPLICIT;
1842 first = NEXTOPER(first);
1845 if (sawplus && (!sawopen || !RExC_sawback)
1846 && !(RExC_seen & REG_SEEN_EVAL)) /* May examine pos and $& */
1847 /* x+ must match at the 1st pos of run of x's */
1848 r->reganch |= ROPT_SKIP;
1850 /* Scan is after the zeroth branch, first is atomic matcher. */
1851 DEBUG_r(PerlIO_printf(Perl_debug_log, "first at %"IVdf"\n",
1852 (IV)(first - scan + 1)));
1854 * If there's something expensive in the r.e., find the
1855 * longest literal string that must appear and make it the
1856 * regmust. Resolve ties in favor of later strings, since
1857 * the regstart check works with the beginning of the r.e.
1858 * and avoiding duplication strengthens checking. Not a
1859 * strong reason, but sufficient in the absence of others.
1860 * [Now we resolve ties in favor of the earlier string if
1861 * it happens that c_offset_min has been invalidated, since the
1862 * earlier string may buy us something the later one won't.]
1866 data.longest_fixed = newSVpvn("",0);
1867 data.longest_float = newSVpvn("",0);
1868 data.last_found = newSVpvn("",0);
1869 data.longest = &(data.longest_fixed);
1871 if (!r->regstclass) {
1872 cl_init(pRExC_state, &ch_class);
1873 data.start_class = &ch_class;
1874 stclass_flag = SCF_DO_STCLASS_AND;
1875 } else /* XXXX Check for BOUND? */
1877 data.last_closep = &last_close;
1879 minlen = study_chunk(pRExC_state, &first, &fake, scan + RExC_size, /* Up to end */
1880 &data, SCF_DO_SUBSTR | SCF_WHILEM_VISITED_POS | stclass_flag);
1881 if ( RExC_npar == 1 && data.longest == &(data.longest_fixed)
1882 && data.last_start_min == 0 && data.last_end > 0
1883 && !RExC_seen_zerolen
1884 && (!(RExC_seen & REG_SEEN_GPOS) || (r->reganch & ROPT_ANCH_GPOS)))
1885 r->reganch |= ROPT_CHECK_ALL;
1886 scan_commit(pRExC_state, &data);
1887 SvREFCNT_dec(data.last_found);
1889 longest_float_length = CHR_SVLEN(data.longest_float);
1890 if (longest_float_length
1891 || (data.flags & SF_FL_BEFORE_EOL
1892 && (!(data.flags & SF_FL_BEFORE_MEOL)
1893 || (RExC_flags16 & PMf_MULTILINE)))) {
1896 if (SvCUR(data.longest_fixed) /* ok to leave SvCUR */
1897 && data.offset_fixed == data.offset_float_min
1898 && SvCUR(data.longest_fixed) == SvCUR(data.longest_float))
1899 goto remove_float; /* As in (a)+. */
1901 r->float_substr = data.longest_float;
1902 r->float_min_offset = data.offset_float_min;
1903 r->float_max_offset = data.offset_float_max;
1904 t = (data.flags & SF_FL_BEFORE_EOL /* Can't have SEOL and MULTI */
1905 && (!(data.flags & SF_FL_BEFORE_MEOL)
1906 || (RExC_flags16 & PMf_MULTILINE)));
1907 fbm_compile(r->float_substr, t ? FBMcf_TAIL : 0);
1911 r->float_substr = Nullsv;
1912 SvREFCNT_dec(data.longest_float);
1913 longest_float_length = 0;
1916 longest_fixed_length = CHR_SVLEN(data.longest_fixed);
1917 if (longest_fixed_length
1918 || (data.flags & SF_FIX_BEFORE_EOL /* Cannot have SEOL and MULTI */
1919 && (!(data.flags & SF_FIX_BEFORE_MEOL)
1920 || (RExC_flags16 & PMf_MULTILINE)))) {
1923 r->anchored_substr = data.longest_fixed;
1924 r->anchored_offset = data.offset_fixed;
1925 t = (data.flags & SF_FIX_BEFORE_EOL /* Can't have SEOL and MULTI */
1926 && (!(data.flags & SF_FIX_BEFORE_MEOL)
1927 || (RExC_flags16 & PMf_MULTILINE)));
1928 fbm_compile(r->anchored_substr, t ? FBMcf_TAIL : 0);
1931 r->anchored_substr = Nullsv;
1932 SvREFCNT_dec(data.longest_fixed);
1933 longest_fixed_length = 0;
1936 && (OP(r->regstclass) == REG_ANY || OP(r->regstclass) == SANY))
1937 r->regstclass = NULL;
1938 if ((!r->anchored_substr || r->anchored_offset) && stclass_flag
1939 && !(data.start_class->flags & ANYOF_EOS)
1940 && !cl_is_anything(data.start_class)) {
1941 I32 n = add_data(pRExC_state, 1, "f");
1943 New(1006, RExC_rx->data->data[n], 1,
1944 struct regnode_charclass_class);
1945 StructCopy(data.start_class,
1946 (struct regnode_charclass_class*)RExC_rx->data->data[n],
1947 struct regnode_charclass_class);
1948 r->regstclass = (regnode*)RExC_rx->data->data[n];
1949 r->reganch &= ~ROPT_SKIP; /* Used in find_byclass(). */
1950 PL_regdata = r->data; /* for regprop() */
1951 DEBUG_r({ SV *sv = sv_newmortal();
1952 regprop(sv, (regnode*)data.start_class);
1953 PerlIO_printf(Perl_debug_log,
1954 "synthetic stclass `%s'.\n",
1958 /* A temporary algorithm prefers floated substr to fixed one to dig more info. */
1959 if (longest_fixed_length > longest_float_length) {
1960 r->check_substr = r->anchored_substr;
1961 r->check_offset_min = r->check_offset_max = r->anchored_offset;
1962 if (r->reganch & ROPT_ANCH_SINGLE)
1963 r->reganch |= ROPT_NOSCAN;
1966 r->check_substr = r->float_substr;
1967 r->check_offset_min = data.offset_float_min;
1968 r->check_offset_max = data.offset_float_max;
1970 /* XXXX Currently intuiting is not compatible with ANCH_GPOS.
1971 This should be changed ASAP! */
1972 if (r->check_substr && !(r->reganch & ROPT_ANCH_GPOS)) {
1973 r->reganch |= RE_USE_INTUIT;
1974 if (SvTAIL(r->check_substr))
1975 r->reganch |= RE_INTUIT_TAIL;
1979 /* Several toplevels. Best we can is to set minlen. */
1981 struct regnode_charclass_class ch_class;
1984 DEBUG_r(PerlIO_printf(Perl_debug_log, "\n"));
1985 scan = r->program + 1;
1986 cl_init(pRExC_state, &ch_class);
1987 data.start_class = &ch_class;
1988 data.last_closep = &last_close;
1989 minlen = study_chunk(pRExC_state, &scan, &fake, scan + RExC_size, &data, SCF_DO_STCLASS_AND|SCF_WHILEM_VISITED_POS);
1990 r->check_substr = r->anchored_substr = r->float_substr = Nullsv;
1991 if (!(data.start_class->flags & ANYOF_EOS)
1992 && !cl_is_anything(data.start_class)) {
1993 I32 n = add_data(pRExC_state, 1, "f");
1995 New(1006, RExC_rx->data->data[n], 1,
1996 struct regnode_charclass_class);
1997 StructCopy(data.start_class,
1998 (struct regnode_charclass_class*)RExC_rx->data->data[n],
1999 struct regnode_charclass_class);
2000 r->regstclass = (regnode*)RExC_rx->data->data[n];
2001 r->reganch &= ~ROPT_SKIP; /* Used in find_byclass(). */
2002 DEBUG_r({ SV* sv = sv_newmortal();
2003 regprop(sv, (regnode*)data.start_class);
2004 PerlIO_printf(Perl_debug_log,
2005 "synthetic stclass `%s'.\n",
2011 if (RExC_seen & REG_SEEN_GPOS)
2012 r->reganch |= ROPT_GPOS_SEEN;
2013 if (RExC_seen & REG_SEEN_LOOKBEHIND)
2014 r->reganch |= ROPT_LOOKBEHIND_SEEN;
2015 if (RExC_seen & REG_SEEN_EVAL)
2016 r->reganch |= ROPT_EVAL_SEEN;
2017 if (RExC_seen & REG_SEEN_CANY)
2018 r->reganch |= ROPT_CANY_SEEN;
2019 Newz(1002, r->startp, RExC_npar, I32);
2020 Newz(1002, r->endp, RExC_npar, I32);
2021 PL_regdata = r->data; /* for regprop() */
2022 DEBUG_r(regdump(r));
2027 - reg - regular expression, i.e. main body or parenthesized thing
2029 * Caller must absorb opening parenthesis.
2031 * Combining parenthesis handling with the base level of regular expression
2032 * is a trifle forced, but the need to tie the tails of the branches to what
2033 * follows makes it hard to avoid.
2036 S_reg(pTHX_ RExC_state_t *pRExC_state, I32 paren, I32 *flagp)
2037 /* paren: Parenthesized? 0=top, 1=(, inside: changed to letter. */
2039 register regnode *ret; /* Will be the head of the group. */
2040 register regnode *br;
2041 register regnode *lastbr;
2042 register regnode *ender = 0;
2043 register I32 parno = 0;
2044 I32 flags, oregflags = RExC_flags16, have_branch = 0, open = 0;
2046 /* for (?g), (?gc), and (?o) warnings; warning
2047 about (?c) will warn about (?g) -- japhy */
2049 I32 wastedflags = 0x00,
2052 wasted_gc = 0x02 | 0x04,
2055 char * parse_start = RExC_parse; /* MJD */
2056 char *oregcomp_parse = RExC_parse;
2059 *flagp = 0; /* Tentatively. */
2062 /* Make an OPEN node, if parenthesized. */
2064 if (*RExC_parse == '?') { /* (?...) */
2065 U16 posflags = 0, negflags = 0;
2066 U16 *flagsp = &posflags;
2068 char *seqstart = RExC_parse;
2071 paren = *RExC_parse++;
2072 ret = NULL; /* For look-ahead/behind. */
2074 case '<': /* (?<...) */
2075 RExC_seen |= REG_SEEN_LOOKBEHIND;
2076 if (*RExC_parse == '!')
2078 if (*RExC_parse != '=' && *RExC_parse != '!')
2081 case '=': /* (?=...) */
2082 case '!': /* (?!...) */
2083 RExC_seen_zerolen++;
2084 case ':': /* (?:...) */
2085 case '>': /* (?>...) */
2087 case '$': /* (?$...) */
2088 case '@': /* (?@...) */
2089 vFAIL2("Sequence (?%c...) not implemented", (int)paren);
2091 case '#': /* (?#...) */
2092 while (*RExC_parse && *RExC_parse != ')')
2094 if (*RExC_parse != ')')
2095 FAIL("Sequence (?#... not terminated");
2096 nextchar(pRExC_state);
2099 case 'p': /* (?p...) */
2100 if (SIZE_ONLY && ckWARN2(WARN_DEPRECATED, WARN_REGEXP))
2101 vWARNdep(RExC_parse, "(?p{}) is deprecated - use (??{})");
2103 case '?': /* (??...) */
2105 paren = *RExC_parse++;
2107 case '{': /* (?{...}) */
2109 I32 count = 1, n = 0;
2111 char *s = RExC_parse;
2113 OP_4tree *sop, *rop;
2115 RExC_seen_zerolen++;
2116 RExC_seen |= REG_SEEN_EVAL;
2117 while (count && (c = *RExC_parse)) {
2118 if (c == '\\' && RExC_parse[1])
2126 if (*RExC_parse != ')')
2129 vFAIL("Sequence (?{...}) not terminated or not {}-balanced");
2134 if (RExC_parse - 1 - s)
2135 sv = newSVpvn(s, RExC_parse - 1 - s);
2137 sv = newSVpvn("", 0);
2140 Perl_save_re_context(aTHX);
2141 rop = sv_compile_2op(sv, &sop, "re", &av);
2142 sop->op_private |= OPpREFCOUNTED;
2143 /* re_dup will OpREFCNT_inc */
2144 OpREFCNT_set(sop, 1);
2147 n = add_data(pRExC_state, 3, "nop");
2148 RExC_rx->data->data[n] = (void*)rop;
2149 RExC_rx->data->data[n+1] = (void*)sop;
2150 RExC_rx->data->data[n+2] = (void*)av;
2153 else { /* First pass */
2154 if (PL_reginterp_cnt < ++RExC_seen_evals
2155 && PL_curcop != &PL_compiling)
2156 /* No compiled RE interpolated, has runtime
2157 components ===> unsafe. */
2158 FAIL("Eval-group not allowed at runtime, use re 'eval'");
2159 if (PL_tainting && PL_tainted)
2160 FAIL("Eval-group in insecure regular expression");
2163 nextchar(pRExC_state);
2165 ret = reg_node(pRExC_state, LOGICAL);
2168 regtail(pRExC_state, ret, reganode(pRExC_state, EVAL, n));
2169 /* deal with the length of this later - MJD */
2172 return reganode(pRExC_state, EVAL, n);
2174 case '(': /* (?(?{...})...) and (?(?=...)...) */
2176 if (RExC_parse[0] == '?') { /* (?(?...)) */
2177 if (RExC_parse[1] == '=' || RExC_parse[1] == '!'
2178 || RExC_parse[1] == '<'
2179 || RExC_parse[1] == '{') { /* Lookahead or eval. */
2182 ret = reg_node(pRExC_state, LOGICAL);
2185 regtail(pRExC_state, ret, reg(pRExC_state, 1, &flag));
2189 else if (RExC_parse[0] >= '1' && RExC_parse[0] <= '9' ) {
2191 parno = atoi(RExC_parse++);
2193 while (isDIGIT(*RExC_parse))
2195 ret = reganode(pRExC_state, GROUPP, parno);
2197 if ((c = *nextchar(pRExC_state)) != ')')
2198 vFAIL("Switch condition not recognized");
2200 regtail(pRExC_state, ret, reganode(pRExC_state, IFTHEN, 0));
2201 br = regbranch(pRExC_state, &flags, 1);
2203 br = reganode(pRExC_state, LONGJMP, 0);
2205 regtail(pRExC_state, br, reganode(pRExC_state, LONGJMP, 0));
2206 c = *nextchar(pRExC_state);
2210 lastbr = reganode(pRExC_state, IFTHEN, 0); /* Fake one for optimizer. */
2211 regbranch(pRExC_state, &flags, 1);
2212 regtail(pRExC_state, ret, lastbr);
2215 c = *nextchar(pRExC_state);
2220 vFAIL("Switch (?(condition)... contains too many branches");
2221 ender = reg_node(pRExC_state, TAIL);
2222 regtail(pRExC_state, br, ender);
2224 regtail(pRExC_state, lastbr, ender);
2225 regtail(pRExC_state, NEXTOPER(NEXTOPER(lastbr)), ender);
2228 regtail(pRExC_state, ret, ender);
2232 vFAIL2("Unknown switch condition (?(%.2s", RExC_parse);
2236 RExC_parse--; /* for vFAIL to print correctly */
2237 vFAIL("Sequence (? incomplete");
2241 parse_flags: /* (?i) */
2242 while (*RExC_parse && strchr("iogcmsx", *RExC_parse)) {
2243 /* (?g), (?gc) and (?o) are useless here
2244 and must be globally applied -- japhy */
2246 if (*RExC_parse == 'o' || *RExC_parse == 'g') {
2247 if (SIZE_ONLY && ckWARN(WARN_REGEXP)) {
2248 I32 wflagbit = *RExC_parse == 'o' ? wasted_o : wasted_g;
2249 if (! (wastedflags & wflagbit) ) {
2250 wastedflags |= wflagbit;
2253 "Useless (%s%c) - %suse /%c modifier",
2254 flagsp == &negflags ? "?-" : "?",
2256 flagsp == &negflags ? "don't " : "",
2262 else if (*RExC_parse == 'c') {
2263 if (SIZE_ONLY && ckWARN(WARN_REGEXP)) {
2264 if (! (wastedflags & wasted_c) ) {
2265 wastedflags |= wasted_gc;
2268 "Useless (%sc) - %suse /gc modifier",
2269 flagsp == &negflags ? "?-" : "?",
2270 flagsp == &negflags ? "don't " : ""
2275 else { pmflag(flagsp, *RExC_parse); }
2279 if (*RExC_parse == '-') {
2281 wastedflags = 0; /* reset so (?g-c) warns twice */
2285 RExC_flags16 |= posflags;
2286 RExC_flags16 &= ~negflags;
2287 if (*RExC_parse == ':') {
2293 if (*RExC_parse != ')') {
2295 vFAIL3("Sequence (%.*s...) not recognized", RExC_parse-seqstart, seqstart);
2297 nextchar(pRExC_state);
2305 ret = reganode(pRExC_state, OPEN, parno);
2306 Set_Node_Length(ret, 1); /* MJD */
2307 Set_Node_Offset(ret, RExC_parse); /* MJD */
2314 /* Pick up the branches, linking them together. */
2315 parse_start = RExC_parse; /* MJD */
2316 br = regbranch(pRExC_state, &flags, 1);
2317 /* branch_len = (paren != 0); */
2321 if (*RExC_parse == '|') {
2322 if (!SIZE_ONLY && RExC_extralen) {
2323 reginsert(pRExC_state, BRANCHJ, br);
2326 reginsert(pRExC_state, BRANCH, br);
2327 Set_Node_Length(br, paren != 0);
2328 Set_Node_Offset_To_R(br-RExC_emit_start, parse_start-RExC_start);
2332 RExC_extralen += 1; /* For BRANCHJ-BRANCH. */
2334 else if (paren == ':') {
2335 *flagp |= flags&SIMPLE;
2337 if (open) { /* Starts with OPEN. */
2338 regtail(pRExC_state, ret, br); /* OPEN -> first. */
2340 else if (paren != '?') /* Not Conditional */
2344 *flagp |= flags&SPSTART;
2346 while (*RExC_parse == '|') {
2347 if (!SIZE_ONLY && RExC_extralen) {
2348 ender = reganode(pRExC_state, LONGJMP,0);
2349 regtail(pRExC_state, NEXTOPER(NEXTOPER(lastbr)), ender); /* Append to the previous. */
2352 RExC_extralen += 2; /* Account for LONGJMP. */
2353 nextchar(pRExC_state);
2354 br = regbranch(pRExC_state, &flags, 0);
2358 regtail(pRExC_state, lastbr, br); /* BRANCH -> BRANCH. */
2362 *flagp |= flags&SPSTART;
2365 if (have_branch || paren != ':') {
2366 /* Make a closing node, and hook it on the end. */
2369 ender = reg_node(pRExC_state, TAIL);
2372 ender = reganode(pRExC_state, CLOSE, parno);
2373 Set_Node_Offset(ender,RExC_parse+1); /* MJD */
2374 Set_Node_Length(ender,1); /* MJD */
2380 *flagp &= ~HASWIDTH;
2383 ender = reg_node(pRExC_state, SUCCEED);
2386 ender = reg_node(pRExC_state, END);
2389 regtail(pRExC_state, lastbr, ender);
2392 /* Hook the tails of the branches to the closing node. */
2393 for (br = ret; br != NULL; br = regnext(br)) {
2394 regoptail(pRExC_state, br, ender);
2401 static char parens[] = "=!<,>";
2403 if (paren && (p = strchr(parens, paren))) {
2404 int node = ((p - parens) % 2) ? UNLESSM : IFMATCH;
2405 int flag = (p - parens) > 1;
2408 node = SUSPEND, flag = 0;
2409 reginsert(pRExC_state, node,ret);
2411 regtail(pRExC_state, ret, reg_node(pRExC_state, TAIL));
2415 /* Check for proper termination. */
2417 RExC_flags16 = oregflags;
2418 if (RExC_parse >= RExC_end || *nextchar(pRExC_state) != ')') {
2419 RExC_parse = oregcomp_parse;
2420 vFAIL("Unmatched (");
2423 else if (!paren && RExC_parse < RExC_end) {
2424 if (*RExC_parse == ')') {
2426 vFAIL("Unmatched )");
2429 FAIL("Junk on end of regexp"); /* "Can't happen". */
2437 - regbranch - one alternative of an | operator
2439 * Implements the concatenation operator.
2442 S_regbranch(pTHX_ RExC_state_t *pRExC_state, I32 *flagp, I32 first)
2444 register regnode *ret;
2445 register regnode *chain = NULL;
2446 register regnode *latest;
2447 I32 flags = 0, c = 0;
2452 if (!SIZE_ONLY && RExC_extralen)
2453 ret = reganode(pRExC_state, BRANCHJ,0);
2455 ret = reg_node(pRExC_state, BRANCH);
2456 Set_Node_Length(ret, 1);
2460 if (!first && SIZE_ONLY)
2461 RExC_extralen += 1; /* BRANCHJ */
2463 *flagp = WORST; /* Tentatively. */
2466 nextchar(pRExC_state);
2467 while (RExC_parse < RExC_end && *RExC_parse != '|' && *RExC_parse != ')') {
2469 latest = regpiece(pRExC_state, &flags);
2470 if (latest == NULL) {
2471 if (flags & TRYAGAIN)
2475 else if (ret == NULL)
2477 *flagp |= flags&HASWIDTH;
2478 if (chain == NULL) /* First piece. */
2479 *flagp |= flags&SPSTART;
2482 regtail(pRExC_state, chain, latest);
2487 if (chain == NULL) { /* Loop ran zero times. */
2488 chain = reg_node(pRExC_state, NOTHING);
2493 *flagp |= flags&SIMPLE;
2500 - regpiece - something followed by possible [*+?]
2502 * Note that the branching code sequences used for ? and the general cases
2503 * of * and + are somewhat optimized: they use the same NOTHING node as
2504 * both the endmarker for their branch list and the body of the last branch.
2505 * It might seem that this node could be dispensed with entirely, but the
2506 * endmarker role is not redundant.
2509 S_regpiece(pTHX_ RExC_state_t *pRExC_state, I32 *flagp)
2511 register regnode *ret;
2513 register char *next;
2515 char *origparse = RExC_parse;
2518 I32 max = REG_INFTY;
2521 ret = regatom(pRExC_state, &flags);
2523 if (flags & TRYAGAIN)
2530 if (op == '{' && regcurly(RExC_parse)) {
2531 parse_start = RExC_parse; /* MJD */
2532 next = RExC_parse + 1;
2534 while (isDIGIT(*next) || *next == ',') {
2543 if (*next == '}') { /* got one */
2547 min = atoi(RExC_parse);
2551 maxpos = RExC_parse;
2553 if (!max && *maxpos != '0')
2554 max = REG_INFTY; /* meaning "infinity" */
2555 else if (max >= REG_INFTY)
2556 vFAIL2("Quantifier in {,} bigger than %d", REG_INFTY - 1);
2558 nextchar(pRExC_state);
2561 if ((flags&SIMPLE)) {
2562 RExC_naughty += 2 + RExC_naughty / 2;
2563 reginsert(pRExC_state, CURLY, ret);
2564 Set_Node_Offset(ret, parse_start+1); /* MJD */
2565 Set_Node_Cur_Length(ret);
2568 regnode *w = reg_node(pRExC_state, WHILEM);
2571 regtail(pRExC_state, ret, w);
2572 if (!SIZE_ONLY && RExC_extralen) {
2573 reginsert(pRExC_state, LONGJMP,ret);
2574 reginsert(pRExC_state, NOTHING,ret);
2575 NEXT_OFF(ret) = 3; /* Go over LONGJMP. */
2577 reginsert(pRExC_state, CURLYX,ret);
2579 Set_Node_Offset(ret, parse_start+1);
2580 Set_Node_Length(ret,
2581 op == '{' ? (RExC_parse - parse_start) : 1);
2583 if (!SIZE_ONLY && RExC_extralen)
2584 NEXT_OFF(ret) = 3; /* Go over NOTHING to LONGJMP. */
2585 regtail(pRExC_state, ret, reg_node(pRExC_state, NOTHING));
2587 RExC_whilem_seen++, RExC_extralen += 3;
2588 RExC_naughty += 4 + RExC_naughty; /* compound interest */
2596 if (max && max < min)
2597 vFAIL("Can't do {n,m} with n > m");
2612 #if 0 /* Now runtime fix should be reliable. */
2614 /* if this is reinstated, don't forget to put this back into perldiag:
2616 =item Regexp *+ operand could be empty at {#} in regex m/%s/
2618 (F) The part of the regexp subject to either the * or + quantifier
2619 could match an empty string. The {#} shows in the regular
2620 expression about where the problem was discovered.
2624 if (!(flags&HASWIDTH) && op != '?')
2625 vFAIL("Regexp *+ operand could be empty");
2628 parse_start = RExC_parse;
2629 nextchar(pRExC_state);
2631 *flagp = (op != '+') ? (WORST|SPSTART|HASWIDTH) : (WORST|HASWIDTH);
2633 if (op == '*' && (flags&SIMPLE)) {
2634 reginsert(pRExC_state, STAR, ret);
2638 else if (op == '*') {
2642 else if (op == '+' && (flags&SIMPLE)) {
2643 reginsert(pRExC_state, PLUS, ret);
2647 else if (op == '+') {
2651 else if (op == '?') {
2656 if (ckWARN(WARN_REGEXP) && !SIZE_ONLY && !(flags&HASWIDTH) && max > REG_INFTY/3) {
2658 "%.*s matches null string many times",
2659 RExC_parse - origparse,
2663 if (*RExC_parse == '?') {
2664 nextchar(pRExC_state);
2665 reginsert(pRExC_state, MINMOD, ret);
2666 regtail(pRExC_state, ret, ret + NODE_STEP_REGNODE);
2668 if (ISMULT2(RExC_parse)) {
2670 vFAIL("Nested quantifiers");
2677 - regatom - the lowest level
2679 * Optimization: gobbles an entire sequence of ordinary characters so that
2680 * it can turn them into a single node, which is smaller to store and
2681 * faster to run. Backslashed characters are exceptions, each becoming a
2682 * separate node; the code is simpler that way and it's not worth fixing.
2684 * [Yes, it is worth fixing, some scripts can run twice the speed.] */
2686 S_regatom(pTHX_ RExC_state_t *pRExC_state, I32 *flagp)
2688 register regnode *ret = 0;
2690 char *parse_start = 0;
2692 *flagp = WORST; /* Tentatively. */
2695 switch (*RExC_parse) {
2697 RExC_seen_zerolen++;
2698 nextchar(pRExC_state);
2699 if (RExC_flags16 & PMf_MULTILINE)
2700 ret = reg_node(pRExC_state, MBOL);
2701 else if (RExC_flags16 & PMf_SINGLELINE)
2702 ret = reg_node(pRExC_state, SBOL);
2704 ret = reg_node(pRExC_state, BOL);
2705 Set_Node_Length(ret, 1); /* MJD */
2708 nextchar(pRExC_state);
2710 RExC_seen_zerolen++;
2711 if (RExC_flags16 & PMf_MULTILINE)
2712 ret = reg_node(pRExC_state, MEOL);
2713 else if (RExC_flags16 & PMf_SINGLELINE)
2714 ret = reg_node(pRExC_state, SEOL);
2716 ret = reg_node(pRExC_state, EOL);
2717 Set_Node_Length(ret, 1); /* MJD */
2720 nextchar(pRExC_state);
2721 if (RExC_flags16 & PMf_SINGLELINE)
2722 ret = reg_node(pRExC_state, SANY);
2724 ret = reg_node(pRExC_state, REG_ANY);
2725 *flagp |= HASWIDTH|SIMPLE;
2727 Set_Node_Length(ret, 1); /* MJD */
2731 char *oregcomp_parse = ++RExC_parse;
2732 ret = regclass(pRExC_state);
2733 if (*RExC_parse != ']') {
2734 RExC_parse = oregcomp_parse;
2735 vFAIL("Unmatched [");
2737 nextchar(pRExC_state);
2738 *flagp |= HASWIDTH|SIMPLE;
2739 Set_Node_Length(ret, RExC_parse - oregcomp_parse + 1); /* MJD */
2743 nextchar(pRExC_state);
2744 ret = reg(pRExC_state, 1, &flags);
2746 if (flags & TRYAGAIN) {
2747 if (RExC_parse == RExC_end) {
2748 /* Make parent create an empty node if needed. */
2756 *flagp |= flags&(HASWIDTH|SPSTART|SIMPLE);
2760 if (flags & TRYAGAIN) {
2764 vFAIL("Internal urp");
2765 /* Supposed to be caught earlier. */
2768 if (!regcurly(RExC_parse)) {
2777 vFAIL("Quantifier follows nothing");
2780 switch (*++RExC_parse) {
2782 RExC_seen_zerolen++;
2783 ret = reg_node(pRExC_state, SBOL);
2785 nextchar(pRExC_state);
2786 Set_Node_Length(ret, 2); /* MJD */
2789 ret = reg_node(pRExC_state, GPOS);
2790 RExC_seen |= REG_SEEN_GPOS;
2792 nextchar(pRExC_state);
2793 Set_Node_Length(ret, 2); /* MJD */
2796 ret = reg_node(pRExC_state, SEOL);
2798 nextchar(pRExC_state);
2801 ret = reg_node(pRExC_state, EOS);
2803 RExC_seen_zerolen++; /* Do not optimize RE away */
2804 nextchar(pRExC_state);
2805 Set_Node_Length(ret, 2); /* MJD */
2808 ret = reg_node(pRExC_state, CANY);
2809 RExC_seen |= REG_SEEN_CANY;
2810 *flagp |= HASWIDTH|SIMPLE;
2811 nextchar(pRExC_state);
2812 Set_Node_Length(ret, 2); /* MJD */
2815 ret = reg_node(pRExC_state, CLUMP);
2817 nextchar(pRExC_state);
2818 Set_Node_Length(ret, 2); /* MJD */
2821 ret = reg_node(pRExC_state, LOC ? ALNUML : ALNUM);
2822 *flagp |= HASWIDTH|SIMPLE;
2823 nextchar(pRExC_state);
2824 Set_Node_Length(ret, 2); /* MJD */
2827 ret = reg_node(pRExC_state, LOC ? NALNUML : NALNUM);
2828 *flagp |= HASWIDTH|SIMPLE;
2829 nextchar(pRExC_state);
2830 Set_Node_Length(ret, 2); /* MJD */
2833 RExC_seen_zerolen++;
2834 RExC_seen |= REG_SEEN_LOOKBEHIND;
2835 ret = reg_node(pRExC_state, LOC ? BOUNDL : BOUND);
2837 nextchar(pRExC_state);
2838 Set_Node_Length(ret, 2); /* MJD */
2841 RExC_seen_zerolen++;
2842 RExC_seen |= REG_SEEN_LOOKBEHIND;
2843 ret = reg_node(pRExC_state, LOC ? NBOUNDL : NBOUND);
2845 nextchar(pRExC_state);
2846 Set_Node_Length(ret, 2); /* MJD */
2849 ret = reg_node(pRExC_state, LOC ? SPACEL : SPACE);
2850 *flagp |= HASWIDTH|SIMPLE;
2851 nextchar(pRExC_state);
2852 Set_Node_Length(ret, 2); /* MJD */
2855 ret = reg_node(pRExC_state, LOC ? NSPACEL : NSPACE);
2856 *flagp |= HASWIDTH|SIMPLE;
2857 nextchar(pRExC_state);
2858 Set_Node_Length(ret, 2); /* MJD */
2861 ret = reg_node(pRExC_state, DIGIT);
2862 *flagp |= HASWIDTH|SIMPLE;
2863 nextchar(pRExC_state);
2864 Set_Node_Length(ret, 2); /* MJD */
2867 ret = reg_node(pRExC_state, NDIGIT);
2868 *flagp |= HASWIDTH|SIMPLE;
2869 nextchar(pRExC_state);
2870 Set_Node_Length(ret, 2); /* MJD */
2875 char* oldregxend = RExC_end;
2876 char* parse_start = RExC_parse;
2878 if (RExC_parse[1] == '{') {
2879 /* a lovely hack--pretend we saw [\pX] instead */
2880 RExC_end = strchr(RExC_parse, '}');
2882 U8 c = (U8)*RExC_parse;
2884 RExC_end = oldregxend;
2885 vFAIL2("Missing right brace on \\%c{}", c);
2890 RExC_end = RExC_parse + 2;
2893 ret = regclass(pRExC_state);
2895 RExC_end = oldregxend;
2897 Set_Node_Cur_Length(ret); /* MJD */
2898 nextchar(pRExC_state);
2899 *flagp |= HASWIDTH|SIMPLE;
2912 case '1': case '2': case '3': case '4':
2913 case '5': case '6': case '7': case '8': case '9':
2915 I32 num = atoi(RExC_parse);
2917 if (num > 9 && num >= RExC_npar)
2920 char * parse_start = RExC_parse - 1; /* MJD */
2921 while (isDIGIT(*RExC_parse))
2924 if (!SIZE_ONLY && num > RExC_rx->nparens)
2925 vFAIL("Reference to nonexistent group");
2927 ret = reganode(pRExC_state, FOLD
2928 ? (LOC ? REFFL : REFF)
2932 /* override incorrect value set in reganode MJD */
2933 Set_Node_Offset(ret, parse_start+1);
2934 Set_Node_Cur_Length(ret); /* MJD */
2936 nextchar(pRExC_state);
2941 if (RExC_parse >= RExC_end)
2942 FAIL("Trailing \\");
2945 /* Do not generate `unrecognized' warnings here, we fall
2946 back into the quick-grab loop below */
2952 if (RExC_flags16 & PMf_EXTENDED) {
2953 while (RExC_parse < RExC_end && *RExC_parse != '\n') RExC_parse++;
2954 if (RExC_parse < RExC_end)
2960 register STRLEN len;
2966 U8 tmpbuf[UTF8_MAXLEN*2+1];
2968 parse_start = RExC_parse - 1;
2973 ret = reg_node(pRExC_state, FOLD
2974 ? (LOC ? EXACTFL : EXACTF)
2977 for (len = 0, p = RExC_parse - 1;
2978 len < 127 && p < RExC_end;
2983 if (RExC_flags16 & PMf_EXTENDED)
2984 p = regwhite(p, RExC_end);
3029 ender = ASCII_TO_NATIVE('\033');
3033 ender = ASCII_TO_NATIVE('\007');
3038 char* e = strchr(p, '}');
3042 vFAIL("Missing right brace on \\x{}");
3045 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES
3046 | PERL_SCAN_DISALLOW_PREFIX;
3048 ender = grok_hex(p + 1, &numlen, &flags, NULL);
3051 /* numlen is generous */
3052 if (numlen + len >= 127) {
3060 I32 flags = PERL_SCAN_DISALLOW_PREFIX;
3062 ender = grok_hex(p, &numlen, &flags, NULL);
3068 ender = UCHARAT(p++);
3069 ender = toCTRL(ender);
3071 case '0': case '1': case '2': case '3':case '4':
3072 case '5': case '6': case '7': case '8':case '9':
3074 (isDIGIT(p[1]) && atoi(p) >= RExC_npar) ) {
3077 ender = grok_oct(p, &numlen, &flags, NULL);
3087 FAIL("Trailing \\");
3090 if (!SIZE_ONLY && ckWARN(WARN_REGEXP) && isALPHA(*p))
3091 vWARN2(p + 1, "Unrecognized escape \\%c passed through", UCHARAT(p));
3092 goto normal_default;
3097 if (UTF8_IS_START(*p) && UTF) {
3098 ender = utf8n_to_uvchr((U8*)p, RExC_end - p,
3106 if (RExC_flags16 & PMf_EXTENDED)
3107 p = regwhite(p, RExC_end);
3109 toLOWER_uni(ender, tmpbuf, &ulen);
3110 ender = utf8_to_uvchr(tmpbuf, 0);
3112 if (ISMULT2(p)) { /* Back off on ?+*. */
3115 else if (!UNI_IS_INVARIANT(NATIVE_TO_UNI(ender)) && UTF) {
3116 reguni(pRExC_state, ender, s, &numlen);
3126 if (!UNI_IS_INVARIANT(NATIVE_TO_UNI(ender)) && UTF) {
3127 reguni(pRExC_state, ender, s, &numlen);
3136 Set_Node_Cur_Length(ret); /* MJD */
3137 nextchar(pRExC_state);
3139 /* len is STRLEN which is unsigned, need to copy to signed */
3142 vFAIL("Internal disaster");
3151 RExC_size += STR_SZ(len);
3153 RExC_emit += STR_SZ(len);
3158 if (PL_encoding && PL_regkind[(U8)OP(ret)] == EXACT && !RExC_utf8) {
3159 STRLEN oldlen = STR_LEN(ret);
3160 SV *sv = sv_2mortal(newSVpvn(STRING(ret), oldlen));
3161 char *s = Perl_sv_recode_to_utf8(aTHX_ sv, PL_encoding);
3162 STRLEN newlen = SvCUR(sv);
3164 DEBUG_r(PerlIO_printf(Perl_debug_log, "recode %*s to %*s\n",
3165 oldlen, STRING(ret), newlen, s));
3166 Copy(s, STRING(ret), newlen, char);
3167 STR_LEN(ret) += newlen - oldlen;
3168 RExC_emit += STR_SZ(newlen) - STR_SZ(oldlen);
3170 RExC_size += STR_SZ(newlen) - STR_SZ(oldlen);
3178 S_regwhite(pTHX_ char *p, char *e)
3183 else if (*p == '#') {
3186 } while (p < e && *p != '\n');
3194 /* Parse POSIX character classes: [[:foo:]], [[=foo=]], [[.foo.]].
3195 Character classes ([:foo:]) can also be negated ([:^foo:]).
3196 Returns a named class id (ANYOF_XXX) if successful, -1 otherwise.
3197 Equivalence classes ([=foo=]) and composites ([.foo.]) are parsed,
3198 but trigger failures because they are currently unimplemented. */
3200 #define POSIXCC_DONE(c) ((c) == ':')
3201 #define POSIXCC_NOTYET(c) ((c) == '=' || (c) == '.')
3202 #define POSIXCC(c) (POSIXCC_DONE(c) || POSIXCC_NOTYET(c))
3205 S_regpposixcc(pTHX_ RExC_state_t *pRExC_state, I32 value)
3208 I32 namedclass = OOB_NAMEDCLASS;
3210 if (value == '[' && RExC_parse + 1 < RExC_end &&
3211 /* I smell either [: or [= or [. -- POSIX has been here, right? */
3212 POSIXCC(UCHARAT(RExC_parse))) {
3213 char c = UCHARAT(RExC_parse);
3214 char* s = RExC_parse++;
3216 while (RExC_parse < RExC_end && UCHARAT(RExC_parse) != c)
3218 if (RExC_parse == RExC_end)
3219 /* Grandfather lone [:, [=, [. */
3222 char* t = RExC_parse++; /* skip over the c */
3224 if (UCHARAT(RExC_parse) == ']') {
3225 RExC_parse++; /* skip over the ending ] */
3228 I32 complement = *posixcc == '^' ? *posixcc++ : 0;
3229 I32 skip = 5; /* the most common skip */
3233 if (strnEQ(posixcc, "alnum", 5))
3235 complement ? ANYOF_NALNUMC : ANYOF_ALNUMC;
3236 else if (strnEQ(posixcc, "alpha", 5))
3238 complement ? ANYOF_NALPHA : ANYOF_ALPHA;
3239 else if (strnEQ(posixcc, "ascii", 5))
3241 complement ? ANYOF_NASCII : ANYOF_ASCII;
3244 if (strnEQ(posixcc, "blank", 5))
3246 complement ? ANYOF_NBLANK : ANYOF_BLANK;
3249 if (strnEQ(posixcc, "cntrl", 5))
3251 complement ? ANYOF_NCNTRL : ANYOF_CNTRL;
3254 if (strnEQ(posixcc, "digit", 5))
3256 complement ? ANYOF_NDIGIT : ANYOF_DIGIT;
3259 if (strnEQ(posixcc, "graph", 5))
3261 complement ? ANYOF_NGRAPH : ANYOF_GRAPH;
3264 if (strnEQ(posixcc, "lower", 5))
3266 complement ? ANYOF_NLOWER : ANYOF_LOWER;
3269 if (strnEQ(posixcc, "print", 5))
3271 complement ? ANYOF_NPRINT : ANYOF_PRINT;
3272 else if (strnEQ(posixcc, "punct", 5))
3274 complement ? ANYOF_NPUNCT : ANYOF_PUNCT;
3277 if (strnEQ(posixcc, "space", 5))
3279 complement ? ANYOF_NPSXSPC : ANYOF_PSXSPC;
3282 if (strnEQ(posixcc, "upper", 5))
3284 complement ? ANYOF_NUPPER : ANYOF_UPPER;
3286 case 'w': /* this is not POSIX, this is the Perl \w */
3287 if (strnEQ(posixcc, "word", 4)) {
3289 complement ? ANYOF_NALNUM : ANYOF_ALNUM;
3294 if (strnEQ(posixcc, "xdigit", 6)) {
3296 complement ? ANYOF_NXDIGIT : ANYOF_XDIGIT;
3301 if (namedclass == OOB_NAMEDCLASS ||
3302 posixcc[skip] != ':' ||
3303 posixcc[skip+1] != ']')
3305 Simple_vFAIL3("POSIX class [:%.*s:] unknown",
3308 } else if (!SIZE_ONLY) {
3309 /* [[=foo=]] and [[.foo.]] are still future. */
3311 /* adjust RExC_parse so the warning shows after
3313 while (UCHARAT(RExC_parse) && UCHARAT(RExC_parse) != ']')
3315 Simple_vFAIL3("POSIX syntax [%c %c] is reserved for future extensions", c, c);
3318 /* Maternal grandfather:
3319 * "[:" ending in ":" but not in ":]" */
3329 S_checkposixcc(pTHX_ RExC_state_t *pRExC_state)
3331 if (!SIZE_ONLY && ckWARN(WARN_REGEXP) &&
3332 POSIXCC(UCHARAT(RExC_parse))) {
3333 char *s = RExC_parse;
3336 while(*s && isALNUM(*s))
3338 if (*s && c == *s && s[1] == ']') {
3339 vWARN3(s+2, "POSIX syntax [%c %c] belongs inside character classes", c, c);
3341 /* [[=foo=]] and [[.foo.]] are still future. */
3342 if (POSIXCC_NOTYET(c)) {
3343 /* adjust RExC_parse so the error shows after
3345 while (UCHARAT(RExC_parse) && UCHARAT(RExC_parse++) != ']')
3347 Simple_vFAIL3("POSIX syntax [%c %c] is reserved for future extensions", c, c);
3354 S_regclass(pTHX_ RExC_state_t *pRExC_state)
3357 register UV nextvalue;
3358 register IV prevvalue = OOB_UNICODE;
3359 register IV range = 0;
3360 register regnode *ret;
3363 char *rangebegin = 0;
3364 bool need_class = 0;
3365 SV *listsv = Nullsv;
3368 bool optimize_invert = TRUE;
3370 ret = reganode(pRExC_state, ANYOF, 0);
3373 ANYOF_FLAGS(ret) = 0;
3375 if (UCHARAT(RExC_parse) == '^') { /* Complement of range. */
3379 ANYOF_FLAGS(ret) |= ANYOF_INVERT;
3383 RExC_size += ANYOF_SKIP;
3385 RExC_emit += ANYOF_SKIP;
3387 ANYOF_FLAGS(ret) |= ANYOF_FOLD;
3389 ANYOF_FLAGS(ret) |= ANYOF_LOCALE;
3390 ANYOF_BITMAP_ZERO(ret);
3391 listsv = newSVpvn("# comment\n", 10);
3394 nextvalue = RExC_parse < RExC_end ? UCHARAT(RExC_parse) : 0;
3396 if (!SIZE_ONLY && ckWARN(WARN_REGEXP) && POSIXCC(nextvalue))
3397 checkposixcc(pRExC_state);
3399 if (UCHARAT(RExC_parse) == ']' || UCHARAT(RExC_parse) == '-')
3400 goto charclassloop; /* allow 1st char to be ] or - */
3402 while (RExC_parse < RExC_end && UCHARAT(RExC_parse) != ']') {
3406 namedclass = OOB_NAMEDCLASS; /* initialize as illegal */
3409 rangebegin = RExC_parse;
3411 value = utf8n_to_uvchr((U8*)RExC_parse,
3412 RExC_end - RExC_parse,
3414 RExC_parse += numlen;
3417 value = UCHARAT(RExC_parse++);
3418 nextvalue = RExC_parse < RExC_end ? UCHARAT(RExC_parse) : 0;
3419 if (value == '[' && POSIXCC(nextvalue))
3420 namedclass = regpposixcc(pRExC_state, value);
3421 else if (value == '\\') {
3423 value = utf8n_to_uvchr((U8*)RExC_parse,
3424 RExC_end - RExC_parse,
3426 RExC_parse += numlen;
3429 value = UCHARAT(RExC_parse++);
3430 /* Some compilers cannot handle switching on 64-bit integer
3431 * values, therefore value cannot be an UV. Yes, this will
3432 * be a problem later if we want switch on Unicode.
3433 * A similar issue a little bit later when switching on
3434 * namedclass. --jhi */
3435 switch ((I32)value) {
3436 case 'w': namedclass = ANYOF_ALNUM; break;
3437 case 'W': namedclass = ANYOF_NALNUM; break;
3438 case 's': namedclass = ANYOF_SPACE; break;
3439 case 'S': namedclass = ANYOF_NSPACE; break;
3440 case 'd': namedclass = ANYOF_DIGIT; break;
3441 case 'D': namedclass = ANYOF_NDIGIT; break;
3444 if (*RExC_parse == '{') {
3446 e = strchr(RExC_parse++, '}');
3448 vFAIL2("Missing right brace on \\%c{}", c);
3449 while (isSPACE(UCHARAT(RExC_parse)))
3451 if (e == RExC_parse)
3452 vFAIL2("Empty \\%c{}", c);
3454 while (isSPACE(UCHARAT(RExC_parse + n - 1)))
3462 if (UCHARAT(RExC_parse) == '^') {
3465 value = value == 'p' ? 'P' : 'p'; /* toggle */
3466 while (isSPACE(UCHARAT(RExC_parse))) {
3472 Perl_sv_catpvf(aTHX_ listsv,
3473 "+utf8::%.*s\n", (int)n, RExC_parse);
3475 Perl_sv_catpvf(aTHX_ listsv,
3476 "!utf8::%.*s\n", (int)n, RExC_parse);
3479 ANYOF_FLAGS(ret) |= ANYOF_UNICODE;
3481 case 'n': value = '\n'; break;
3482 case 'r': value = '\r'; break;
3483 case 't': value = '\t'; break;
3484 case 'f': value = '\f'; break;
3485 case 'b': value = '\b'; break;
3486 case 'e': value = ASCII_TO_NATIVE('\033');break;
3487 case 'a': value = ASCII_TO_NATIVE('\007');break;
3489 if (*RExC_parse == '{') {
3490 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES
3491 | PERL_SCAN_DISALLOW_PREFIX;
3492 e = strchr(RExC_parse++, '}');
3494 vFAIL("Missing right brace on \\x{}");
3496 numlen = e - RExC_parse;
3497 value = grok_hex(RExC_parse, &numlen, &flags, NULL);
3501 I32 flags = PERL_SCAN_DISALLOW_PREFIX;
3503 value = grok_hex(RExC_parse, &numlen, &flags, NULL);
3504 RExC_parse += numlen;
3508 value = UCHARAT(RExC_parse++);
3509 value = toCTRL(value);
3511 case '0': case '1': case '2': case '3': case '4':
3512 case '5': case '6': case '7': case '8': case '9':
3516 value = grok_oct(--RExC_parse, &numlen, &flags, NULL);
3517 RExC_parse += numlen;
3521 if (!SIZE_ONLY && ckWARN(WARN_REGEXP) && isALPHA(value))
3523 "Unrecognized escape \\%c in character class passed through",
3527 } /* end of \blah */
3529 if (namedclass > OOB_NAMEDCLASS) { /* this is a named class \blah */
3531 if (!SIZE_ONLY && !need_class)
3532 ANYOF_CLASS_ZERO(ret);
3536 /* a bad range like a-\d, a-[:digit:] ? */
3539 if (ckWARN(WARN_REGEXP))
3541 "False [] range \"%*.*s\"",
3542 RExC_parse - rangebegin,
3543 RExC_parse - rangebegin,
3545 if (prevvalue < 256) {
3546 ANYOF_BITMAP_SET(ret, prevvalue);
3547 ANYOF_BITMAP_SET(ret, '-');
3550 ANYOF_FLAGS(ret) |= ANYOF_UNICODE;
3551 Perl_sv_catpvf(aTHX_ listsv,
3552 "%04"UVxf"\n%04"UVxf"\n", (UV)prevvalue, (UV) '-');
3556 range = 0; /* this was not a true range */
3560 if (namedclass > OOB_NAMEDCLASS)
3561 optimize_invert = FALSE;
3562 /* Possible truncation here but in some 64-bit environments
3563 * the compiler gets heartburn about switch on 64-bit values.
3564 * A similar issue a little earlier when switching on value.
3566 switch ((I32)namedclass) {
3569 ANYOF_CLASS_SET(ret, ANYOF_ALNUM);
3571 for (value = 0; value < 256; value++)
3573 ANYOF_BITMAP_SET(ret, value);
3575 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsWord\n");
3579 ANYOF_CLASS_SET(ret, ANYOF_NALNUM);
3581 for (value = 0; value < 256; value++)
3582 if (!isALNUM(value))
3583 ANYOF_BITMAP_SET(ret, value);
3585 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsWord\n");
3589 ANYOF_CLASS_SET(ret, ANYOF_ALNUMC);
3591 for (value = 0; value < 256; value++)
3592 if (isALNUMC(value))
3593 ANYOF_BITMAP_SET(ret, value);
3595 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsAlnum\n");
3599 ANYOF_CLASS_SET(ret, ANYOF_NALNUMC);
3601 for (value = 0; value < 256; value++)
3602 if (!isALNUMC(value))
3603 ANYOF_BITMAP_SET(ret, value);
3605 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsAlnum\n");
3609 ANYOF_CLASS_SET(ret, ANYOF_ALPHA);
3611 for (value = 0; value < 256; value++)
3613 ANYOF_BITMAP_SET(ret, value);
3615 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsAlpha\n");
3619 ANYOF_CLASS_SET(ret, ANYOF_NALPHA);
3621 for (value = 0; value < 256; value++)
3622 if (!isALPHA(value))
3623 ANYOF_BITMAP_SET(ret, value);
3625 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsAlpha\n");
3629 ANYOF_CLASS_SET(ret, ANYOF_ASCII);
3632 for (value = 0; value < 128; value++)
3633 ANYOF_BITMAP_SET(ret, value);
3635 for (value = 0; value < 256; value++) {
3637 ANYOF_BITMAP_SET(ret, value);
3641 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsASCII\n");
3645 ANYOF_CLASS_SET(ret, ANYOF_NASCII);
3648 for (value = 128; value < 256; value++)
3649 ANYOF_BITMAP_SET(ret, value);
3651 for (value = 0; value < 256; value++) {
3652 if (!isASCII(value))
3653 ANYOF_BITMAP_SET(ret, value);
3657 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsASCII\n");
3661 ANYOF_CLASS_SET(ret, ANYOF_BLANK);
3663 for (value = 0; value < 256; value++)
3665 ANYOF_BITMAP_SET(ret, value);
3667 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsBlank\n");
3671 ANYOF_CLASS_SET(ret, ANYOF_NBLANK);
3673 for (value = 0; value < 256; value++)
3674 if (!isBLANK(value))
3675 ANYOF_BITMAP_SET(ret, value);
3677 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsBlank\n");
3681 ANYOF_CLASS_SET(ret, ANYOF_CNTRL);
3683 for (value = 0; value < 256; value++)
3685 ANYOF_BITMAP_SET(ret, value);
3687 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsCntrl\n");
3691 ANYOF_CLASS_SET(ret, ANYOF_NCNTRL);
3693 for (value = 0; value < 256; value++)
3694 if (!isCNTRL(value))
3695 ANYOF_BITMAP_SET(ret, value);
3697 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsCntrl\n");
3701 ANYOF_CLASS_SET(ret, ANYOF_DIGIT);
3703 /* consecutive digits assumed */
3704 for (value = '0'; value <= '9'; value++)
3705 ANYOF_BITMAP_SET(ret, value);
3707 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsDigit\n");
3711 ANYOF_CLASS_SET(ret, ANYOF_NDIGIT);
3713 /* consecutive digits assumed */
3714 for (value = 0; value < '0'; value++)
3715 ANYOF_BITMAP_SET(ret, value);
3716 for (value = '9' + 1; value < 256; value++)
3717 ANYOF_BITMAP_SET(ret, value);
3719 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsDigit\n");
3723 ANYOF_CLASS_SET(ret, ANYOF_GRAPH);
3725 for (value = 0; value < 256; value++)
3727 ANYOF_BITMAP_SET(ret, value);
3729 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsGraph\n");
3733 ANYOF_CLASS_SET(ret, ANYOF_NGRAPH);
3735 for (value = 0; value < 256; value++)
3736 if (!isGRAPH(value))
3737 ANYOF_BITMAP_SET(ret, value);
3739 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsGraph\n");
3743 ANYOF_CLASS_SET(ret, ANYOF_LOWER);
3745 for (value = 0; value < 256; value++)
3747 ANYOF_BITMAP_SET(ret, value);
3749 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsLower\n");
3753 ANYOF_CLASS_SET(ret, ANYOF_NLOWER);
3755 for (value = 0; value < 256; value++)
3756 if (!isLOWER(value))
3757 ANYOF_BITMAP_SET(ret, value);
3759 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsLower\n");
3763 ANYOF_CLASS_SET(ret, ANYOF_PRINT);
3765 for (value = 0; value < 256; value++)
3767 ANYOF_BITMAP_SET(ret, value);
3769 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsPrint\n");
3773 ANYOF_CLASS_SET(ret, ANYOF_NPRINT);
3775 for (value = 0; value < 256; value++)
3776 if (!isPRINT(value))
3777 ANYOF_BITMAP_SET(ret, value);
3779 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsPrint\n");
3783 ANYOF_CLASS_SET(ret, ANYOF_PSXSPC);
3785 for (value = 0; value < 256; value++)
3786 if (isPSXSPC(value))
3787 ANYOF_BITMAP_SET(ret, value);
3789 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsSpace\n");
3793 ANYOF_CLASS_SET(ret, ANYOF_NPSXSPC);
3795 for (value = 0; value < 256; value++)
3796 if (!isPSXSPC(value))
3797 ANYOF_BITMAP_SET(ret, value);
3799 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsSpace\n");
3803 ANYOF_CLASS_SET(ret, ANYOF_PUNCT);
3805 for (value = 0; value < 256; value++)
3807 ANYOF_BITMAP_SET(ret, value);
3809 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsPunct\n");
3813 ANYOF_CLASS_SET(ret, ANYOF_NPUNCT);
3815 for (value = 0; value < 256; value++)
3816 if (!isPUNCT(value))
3817 ANYOF_BITMAP_SET(ret, value);
3819 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsPunct\n");
3823 ANYOF_CLASS_SET(ret, ANYOF_SPACE);
3825 for (value = 0; value < 256; value++)
3827 ANYOF_BITMAP_SET(ret, value);
3829 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsSpacePerl\n");
3833 ANYOF_CLASS_SET(ret, ANYOF_NSPACE);
3835 for (value = 0; value < 256; value++)
3836 if (!isSPACE(value))
3837 ANYOF_BITMAP_SET(ret, value);
3839 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsSpacePerl\n");
3843 ANYOF_CLASS_SET(ret, ANYOF_UPPER);
3845 for (value = 0; value < 256; value++)
3847 ANYOF_BITMAP_SET(ret, value);
3849 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsUpper\n");
3853 ANYOF_CLASS_SET(ret, ANYOF_NUPPER);
3855 for (value = 0; value < 256; value++)
3856 if (!isUPPER(value))
3857 ANYOF_BITMAP_SET(ret, value);
3859 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsUpper\n");
3863 ANYOF_CLASS_SET(ret, ANYOF_XDIGIT);
3865 for (value = 0; value < 256; value++)
3866 if (isXDIGIT(value))
3867 ANYOF_BITMAP_SET(ret, value);
3869 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsXDigit\n");
3873 ANYOF_CLASS_SET(ret, ANYOF_NXDIGIT);
3875 for (value = 0; value < 256; value++)
3876 if (!isXDIGIT(value))
3877 ANYOF_BITMAP_SET(ret, value);
3879 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsXDigit\n");
3882 vFAIL("Invalid [::] class");
3886 ANYOF_FLAGS(ret) |= ANYOF_CLASS;
3889 } /* end of namedclass \blah */
3892 if (prevvalue > value) /* b-a */ {
3893 Simple_vFAIL4("Invalid [] range \"%*.*s\"",
3894 RExC_parse - rangebegin,
3895 RExC_parse - rangebegin,
3897 range = 0; /* not a valid range */
3901 prevvalue = value; /* save the beginning of the range */
3902 if (*RExC_parse == '-' && RExC_parse+1 < RExC_end &&
3903 RExC_parse[1] != ']') {
3906 /* a bad range like \w-, [:word:]- ? */
3907 if (namedclass > OOB_NAMEDCLASS) {
3908 if (ckWARN(WARN_REGEXP))
3910 "False [] range \"%*.*s\"",
3911 RExC_parse - rangebegin,
3912 RExC_parse - rangebegin,
3915 ANYOF_BITMAP_SET(ret, '-');
3917 range = 1; /* yeah, it's a range! */
3918 continue; /* but do it the next time */
3922 /* now is the next time */
3926 if (prevvalue < 256) {
3927 IV ceilvalue = value < 256 ? value : 255;
3930 if ((isLOWER(prevvalue) && isLOWER(ceilvalue)) ||
3931 (isUPPER(prevvalue) && isUPPER(ceilvalue)))
3933 if (isLOWER(prevvalue)) {
3934 for (i = prevvalue; i <= ceilvalue; i++)
3936 ANYOF_BITMAP_SET(ret, i);
3938 for (i = prevvalue; i <= ceilvalue; i++)
3940 ANYOF_BITMAP_SET(ret, i);
3945 for (i = prevvalue; i <= ceilvalue; i++)
3946 ANYOF_BITMAP_SET(ret, i);
3949 ANYOF_FLAGS(ret) |= ANYOF_UNICODE;
3950 if (prevvalue < value)
3951 Perl_sv_catpvf(aTHX_ listsv, "%04"UVxf"\t%04"UVxf"\n",
3952 (UV)prevvalue, (UV)value);
3953 else if (prevvalue == value)
3954 Perl_sv_catpvf(aTHX_ listsv, "%04"UVxf"\n",
3959 range = 0; /* this range (if it was one) is done now */
3963 ANYOF_FLAGS(ret) |= ANYOF_LARGE;
3965 RExC_size += ANYOF_CLASS_ADD_SKIP;
3967 RExC_emit += ANYOF_CLASS_ADD_SKIP;
3970 /* optimize case-insensitive simple patterns (e.g. /[a-z]/i) */
3972 /* If the only flag is folding (plus possibly inversion). */
3973 ((ANYOF_FLAGS(ret) & (ANYOF_FLAGS_ALL ^ ANYOF_INVERT)) == ANYOF_FOLD)
3975 for (value = 0; value < 256; ++value) {
3976 if (ANYOF_BITMAP_TEST(ret, value)) {
3977 IV fold = PL_fold[value];
3980 ANYOF_BITMAP_SET(ret, fold);
3983 ANYOF_FLAGS(ret) &= ~ANYOF_FOLD;
3986 /* optimize inverted simple patterns (e.g. [^a-z]) */
3987 if (!SIZE_ONLY && optimize_invert &&
3988 /* If the only flag is inversion. */
3989 (ANYOF_FLAGS(ret) & ANYOF_FLAGS_ALL) == ANYOF_INVERT) {
3990 for (value = 0; value < ANYOF_BITMAP_SIZE; ++value)
3991 ANYOF_BITMAP(ret)[value] ^= ANYOF_FLAGS_ALL;
3992 ANYOF_FLAGS(ret) = ANYOF_UNICODE_ALL;
3999 av_store(av, 0, listsv);
4000 av_store(av, 1, NULL);
4001 rv = newRV_noinc((SV*)av);
4002 n = add_data(pRExC_state, 1, "s");
4003 RExC_rx->data->data[n] = (void*)rv;
4011 S_nextchar(pTHX_ RExC_state_t *pRExC_state)
4013 char* retval = RExC_parse++;
4016 if (*RExC_parse == '(' && RExC_parse[1] == '?' &&
4017 RExC_parse[2] == '#') {
4018 while (*RExC_parse && *RExC_parse != ')')
4023 if (RExC_flags16 & PMf_EXTENDED) {
4024 if (isSPACE(*RExC_parse)) {
4028 else if (*RExC_parse == '#') {
4029 while (*RExC_parse && *RExC_parse != '\n')
4040 - reg_node - emit a node
4042 STATIC regnode * /* Location. */
4043 S_reg_node(pTHX_ RExC_state_t *pRExC_state, U8 op)
4045 register regnode *ret;
4046 register regnode *ptr;
4050 SIZE_ALIGN(RExC_size);
4055 NODE_ALIGN_FILL(ret);
4057 FILL_ADVANCE_NODE(ptr, op);
4058 if (RExC_offsets) { /* MJD */
4059 MJD_OFFSET_DEBUG((stderr, "%s:%u: (op %s) %s %u <- %u (len %u) (max %u).\n",
4060 "reg_node", __LINE__,
4062 RExC_emit - RExC_emit_start > RExC_offsets[0]
4063 ? "Overwriting end of array!\n" : "OK",
4064 RExC_emit - RExC_emit_start,
4065 RExC_parse - RExC_start,
4067 Set_Node_Offset(RExC_emit, RExC_parse + (op == END));
4076 - reganode - emit a node with an argument
4078 STATIC regnode * /* Location. */
4079 S_reganode(pTHX_ RExC_state_t *pRExC_state, U8 op, U32 arg)
4081 register regnode *ret;
4082 register regnode *ptr;
4086 SIZE_ALIGN(RExC_size);
4091 NODE_ALIGN_FILL(ret);
4093 FILL_ADVANCE_NODE_ARG(ptr, op, arg);
4094 if (RExC_offsets) { /* MJD */
4095 MJD_OFFSET_DEBUG((stderr, "%s: %s %u <- %u (max %u).\n",
4097 RExC_emit - RExC_emit_start > RExC_offsets[0] ?
4098 "Overwriting end of array!\n" : "OK",
4099 RExC_emit - RExC_emit_start,
4100 RExC_parse - RExC_start,
4102 Set_Cur_Node_Offset;
4111 - reguni - emit (if appropriate) a Unicode character
4114 S_reguni(pTHX_ RExC_state_t *pRExC_state, UV uv, char* s, STRLEN* lenp)
4116 *lenp = SIZE_ONLY ? UNISKIP(uv) : (uvchr_to_utf8((U8*)s, uv) - (U8*)s);
4120 - reginsert - insert an operator in front of already-emitted operand
4122 * Means relocating the operand.
4125 S_reginsert(pTHX_ RExC_state_t *pRExC_state, U8 op, regnode *opnd)
4127 register regnode *src;
4128 register regnode *dst;
4129 register regnode *place;
4130 register int offset = regarglen[(U8)op];
4132 /* (PL_regkind[(U8)op] == CURLY ? EXTRA_STEP_2ARGS : 0); */
4135 RExC_size += NODE_STEP_REGNODE + offset;
4140 RExC_emit += NODE_STEP_REGNODE + offset;
4142 while (src > opnd) {
4143 StructCopy(--src, --dst, regnode);
4144 if (RExC_offsets) { /* MJD 20010112 */
4145 MJD_OFFSET_DEBUG((stderr, "%s: %s copy %u -> %u (max %u).\n",
4147 dst - RExC_emit_start > RExC_offsets[0]
4148 ? "Overwriting end of array!\n" : "OK",
4149 src - RExC_emit_start,
4150 dst - RExC_emit_start,
4152 Set_Node_Offset_To_R(dst-RExC_emit_start, Node_Offset(src));
4153 Set_Node_Length_To_R(dst-RExC_emit_start, Node_Length(src));
4158 place = opnd; /* Op node, where operand used to be. */
4159 if (RExC_offsets) { /* MJD */
4160 MJD_OFFSET_DEBUG((stderr, "%s: %s %u <- %u (max %u).\n",
4162 place - RExC_emit_start > RExC_offsets[0]
4163 ? "Overwriting end of array!\n" : "OK",
4164 place - RExC_emit_start,
4165 RExC_parse - RExC_start,
4167 Set_Node_Offset(place, RExC_parse);
4169 src = NEXTOPER(place);
4170 FILL_ADVANCE_NODE(place, op);
4171 Zero(src, offset, regnode);
4175 - regtail - set the next-pointer at the end of a node chain of p to val.
4178 S_regtail(pTHX_ RExC_state_t *pRExC_state, regnode *p, regnode *val)
4180 register regnode *scan;
4181 register regnode *temp;
4186 /* Find last node. */
4189 temp = regnext(scan);
4195 if (reg_off_by_arg[OP(scan)]) {
4196 ARG_SET(scan, val - scan);
4199 NEXT_OFF(scan) = val - scan;
4204 - regoptail - regtail on operand of first argument; nop if operandless
4207 S_regoptail(pTHX_ RExC_state_t *pRExC_state, regnode *p, regnode *val)
4209 /* "Operandless" and "op != BRANCH" are synonymous in practice. */
4210 if (p == NULL || SIZE_ONLY)
4212 if (PL_regkind[(U8)OP(p)] == BRANCH) {
4213 regtail(pRExC_state, NEXTOPER(p), val);
4215 else if ( PL_regkind[(U8)OP(p)] == BRANCHJ) {
4216 regtail(pRExC_state, NEXTOPER(NEXTOPER(p)), val);
4223 - regcurly - a little FSA that accepts {\d+,?\d*}
4226 S_regcurly(pTHX_ register char *s)
4247 S_dumpuntil(pTHX_ regnode *start, regnode *node, regnode *last, SV* sv, I32 l)
4249 register U8 op = EXACT; /* Arbitrary non-END op. */
4250 register regnode *next;
4252 while (op != END && (!last || node < last)) {
4253 /* While that wasn't END last time... */
4259 next = regnext(node);
4261 if (OP(node) == OPTIMIZED)
4264 PerlIO_printf(Perl_debug_log, "%4"IVdf":%*s%s", (IV)(node - start),
4265 (int)(2*l + 1), "", SvPVX(sv));
4266 if (next == NULL) /* Next ptr. */
4267 PerlIO_printf(Perl_debug_log, "(0)");
4269 PerlIO_printf(Perl_debug_log, "(%"IVdf")", (IV)(next - start));
4270 (void)PerlIO_putc(Perl_debug_log, '\n');
4272 if (PL_regkind[(U8)op] == BRANCHJ) {
4273 register regnode *nnode = (OP(next) == LONGJMP
4276 if (last && nnode > last)
4278 node = dumpuntil(start, NEXTOPER(NEXTOPER(node)), nnode, sv, l + 1);
4280 else if (PL_regkind[(U8)op] == BRANCH) {
4281 node = dumpuntil(start, NEXTOPER(node), next, sv, l + 1);
4283 else if ( op == CURLY) { /* `next' might be very big: optimizer */
4284 node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
4285 NEXTOPER(node) + EXTRA_STEP_2ARGS + 1, sv, l + 1);
4287 else if (PL_regkind[(U8)op] == CURLY && op != CURLYX) {
4288 node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
4291 else if ( op == PLUS || op == STAR) {
4292 node = dumpuntil(start, NEXTOPER(node), NEXTOPER(node) + 1, sv, l + 1);
4294 else if (op == ANYOF) {
4295 /* arglen 1 + class block */
4296 node += 1 + ((ANYOF_FLAGS(node) & ANYOF_LARGE)
4297 ? ANYOF_CLASS_SKIP : ANYOF_SKIP);
4298 node = NEXTOPER(node);
4300 else if (PL_regkind[(U8)op] == EXACT) {
4301 /* Literal string, where present. */
4302 node += NODE_SZ_STR(node) - 1;
4303 node = NEXTOPER(node);
4306 node = NEXTOPER(node);
4307 node += regarglen[(U8)op];
4309 if (op == CURLYX || op == OPEN)
4311 else if (op == WHILEM)
4317 #endif /* DEBUGGING */
4320 - regdump - dump a regexp onto Perl_debug_log in vaguely comprehensible form
4323 Perl_regdump(pTHX_ regexp *r)
4326 SV *sv = sv_newmortal();
4328 (void)dumpuntil(r->program, r->program + 1, NULL, sv, 0);
4330 /* Header fields of interest. */
4331 if (r->anchored_substr)
4332 PerlIO_printf(Perl_debug_log,
4333 "anchored `%s%.*s%s'%s at %"IVdf" ",
4335 (int)(SvCUR(r->anchored_substr) - (SvTAIL(r->anchored_substr)!=0)),
4336 SvPVX(r->anchored_substr),
4338 SvTAIL(r->anchored_substr) ? "$" : "",
4339 (IV)r->anchored_offset);
4340 if (r->float_substr)
4341 PerlIO_printf(Perl_debug_log,
4342 "floating `%s%.*s%s'%s at %"IVdf"..%"UVuf" ",
4344 (int)(SvCUR(r->float_substr) - (SvTAIL(r->float_substr)!=0)),
4345 SvPVX(r->float_substr),
4347 SvTAIL(r->float_substr) ? "$" : "",
4348 (IV)r->float_min_offset, (UV)r->float_max_offset);
4349 if (r->check_substr)
4350 PerlIO_printf(Perl_debug_log,
4351 r->check_substr == r->float_substr
4352 ? "(checking floating" : "(checking anchored");
4353 if (r->reganch & ROPT_NOSCAN)
4354 PerlIO_printf(Perl_debug_log, " noscan");
4355 if (r->reganch & ROPT_CHECK_ALL)
4356 PerlIO_printf(Perl_debug_log, " isall");
4357 if (r->check_substr)
4358 PerlIO_printf(Perl_debug_log, ") ");
4360 if (r->regstclass) {
4361 regprop(sv, r->regstclass);
4362 PerlIO_printf(Perl_debug_log, "stclass `%s' ", SvPVX(sv));
4364 if (r->reganch & ROPT_ANCH) {
4365 PerlIO_printf(Perl_debug_log, "anchored");
4366 if (r->reganch & ROPT_ANCH_BOL)
4367 PerlIO_printf(Perl_debug_log, "(BOL)");
4368 if (r->reganch & ROPT_ANCH_MBOL)
4369 PerlIO_printf(Perl_debug_log, "(MBOL)");
4370 if (r->reganch & ROPT_ANCH_SBOL)
4371 PerlIO_printf(Perl_debug_log, "(SBOL)");
4372 if (r->reganch & ROPT_ANCH_GPOS)
4373 PerlIO_printf(Perl_debug_log, "(GPOS)");
4374 PerlIO_putc(Perl_debug_log, ' ');
4376 if (r->reganch & ROPT_GPOS_SEEN)
4377 PerlIO_printf(Perl_debug_log, "GPOS ");
4378 if (r->reganch & ROPT_SKIP)
4379 PerlIO_printf(Perl_debug_log, "plus ");
4380 if (r->reganch & ROPT_IMPLICIT)
4381 PerlIO_printf(Perl_debug_log, "implicit ");
4382 PerlIO_printf(Perl_debug_log, "minlen %ld ", (long) r->minlen);
4383 if (r->reganch & ROPT_EVAL_SEEN)
4384 PerlIO_printf(Perl_debug_log, "with eval ");
4385 PerlIO_printf(Perl_debug_log, "\n");
4388 U32 len = r->offsets[0];
4389 PerlIO_printf(Perl_debug_log, "Offsets: [%"UVuf"]\n\t", (UV)r->offsets[0]);
4390 for (i = 1; i <= len; i++)
4391 PerlIO_printf(Perl_debug_log, "%"UVuf"[%"UVuf"] ",
4392 (UV)r->offsets[i*2-1],
4393 (UV)r->offsets[i*2]);
4394 PerlIO_printf(Perl_debug_log, "\n");
4396 #endif /* DEBUGGING */
4402 S_put_byte(pTHX_ SV *sv, int c)
4404 if (isCNTRL(c) || c == 255 || !isPRINT(c))
4405 Perl_sv_catpvf(aTHX_ sv, "\\%o", c);
4406 else if (c == '-' || c == ']' || c == '\\' || c == '^')
4407 Perl_sv_catpvf(aTHX_ sv, "\\%c", c);
4409 Perl_sv_catpvf(aTHX_ sv, "%c", c);
4412 #endif /* DEBUGGING */
4415 - regprop - printable representation of opcode
4418 Perl_regprop(pTHX_ SV *sv, regnode *o)
4423 sv_setpvn(sv, "", 0);
4424 if (OP(o) >= reg_num) /* regnode.type is unsigned */
4425 /* It would be nice to FAIL() here, but this may be called from
4426 regexec.c, and it would be hard to supply pRExC_state. */
4427 Perl_croak(aTHX_ "Corrupted regexp opcode");
4428 sv_catpv(sv, (char*)reg_name[OP(o)]); /* Take off const! */
4430 k = PL_regkind[(U8)OP(o)];
4433 SV *dsv = sv_2mortal(newSVpvn("", 0));
4434 bool do_utf8 = DO_UTF8(sv);
4436 pv_uni_display(dsv, (U8*)STRING(o), STR_LEN(o), 60, 0) :
4441 Perl_sv_catpvf(aTHX_ sv, " <%s%.*s%s>",
4446 else if (k == CURLY) {
4447 if (OP(o) == CURLYM || OP(o) == CURLYN || OP(o) == CURLYX)
4448 Perl_sv_catpvf(aTHX_ sv, "[%d]", o->flags); /* Parenth number */
4449 Perl_sv_catpvf(aTHX_ sv, " {%d,%d}", ARG1(o), ARG2(o));
4451 else if (k == WHILEM && o->flags) /* Ordinal/of */
4452 Perl_sv_catpvf(aTHX_ sv, "[%d/%d]", o->flags & 0xf, o->flags>>4);
4453 else if (k == REF || k == OPEN || k == CLOSE || k == GROUPP )
4454 Perl_sv_catpvf(aTHX_ sv, "%d", (int)ARG(o)); /* Parenth number */
4455 else if (k == LOGICAL)
4456 Perl_sv_catpvf(aTHX_ sv, "[%d]", o->flags); /* 2: embedded, otherwise 1 */
4457 else if (k == ANYOF) {
4458 int i, rangestart = -1;
4459 U8 flags = ANYOF_FLAGS(o);
4460 const char * const anyofs[] = { /* Should be syncronized with
4461 * ANYOF_ #xdefines in regcomp.h */
4494 if (flags & ANYOF_LOCALE)
4495 sv_catpv(sv, "{loc}");
4496 if (flags & ANYOF_FOLD)
4497 sv_catpv(sv, "{i}");
4498 Perl_sv_catpvf(aTHX_ sv, "[%s", PL_colors[0]);
4499 if (flags & ANYOF_INVERT)
4501 for (i = 0; i <= 256; i++) {
4502 if (i < 256 && ANYOF_BITMAP_TEST(o,i)) {
4503 if (rangestart == -1)
4505 } else if (rangestart != -1) {
4506 if (i <= rangestart + 3)
4507 for (; rangestart < i; rangestart++)
4508 put_byte(sv, rangestart);
4510 put_byte(sv, rangestart);
4512 put_byte(sv, i - 1);
4518 if (o->flags & ANYOF_CLASS)
4519 for (i = 0; i < sizeof(anyofs)/sizeof(char*); i++)
4520 if (ANYOF_CLASS_TEST(o,i))
4521 sv_catpv(sv, anyofs[i]);
4523 if (flags & ANYOF_UNICODE)
4524 sv_catpv(sv, "{unicode}");
4525 else if (flags & ANYOF_UNICODE_ALL)
4526 sv_catpv(sv, "{unicode_all}");
4530 SV *sw = regclass_swash(o, FALSE, &lv);
4535 U8 s[UTF8_MAXLEN+1];
4537 for (i = 0; i <= 256; i++) { /* just the first 256 */
4538 U8 *e = uvchr_to_utf8(s, i);
4540 if (i < 256 && swash_fetch(sw, s, TRUE)) {
4541 if (rangestart == -1)
4543 } else if (rangestart != -1) {
4546 if (i <= rangestart + 3)
4547 for (; rangestart < i; rangestart++) {
4548 for(e = uvchr_to_utf8(s, rangestart), p = s; p < e; p++)
4552 for (e = uvchr_to_utf8(s, rangestart), p = s; p < e; p++)
4555 for (e = uvchr_to_utf8(s, i - 1), p = s; p < e; p++)
4562 sv_catpv(sv, "..."); /* et cetera */
4566 char *s = savepv(SvPVX(lv));
4569 while(*s && *s != '\n') s++;
4590 Perl_sv_catpvf(aTHX_ sv, "%s]", PL_colors[1]);
4592 else if (k == BRANCHJ && (OP(o) == UNLESSM || OP(o) == IFMATCH))
4593 Perl_sv_catpvf(aTHX_ sv, "[-%d]", o->flags);
4594 #endif /* DEBUGGING */
4598 Perl_re_intuit_string(pTHX_ regexp *prog)
4599 { /* Assume that RE_INTUIT is set */
4602 char *s = SvPV(prog->check_substr,n_a);
4604 if (!PL_colorset) reginitcolors();
4605 PerlIO_printf(Perl_debug_log,
4606 "%sUsing REx substr:%s `%s%.60s%s%s'\n",
4607 PL_colors[4],PL_colors[5],PL_colors[0],
4610 (strlen(s) > 60 ? "..." : ""));
4613 return prog->check_substr;
4617 Perl_pregfree(pTHX_ struct regexp *r)
4619 DEBUG_r(if (!PL_colorset) reginitcolors());
4621 if (!r || (--r->refcnt > 0))
4623 DEBUG_r(PerlIO_printf(Perl_debug_log,
4624 "%sFreeing REx:%s `%s%.60s%s%s'\n",
4625 PL_colors[4],PL_colors[5],PL_colors[0],
4628 (strlen(r->precomp) > 60 ? "..." : "")));
4631 Safefree(r->precomp);
4632 if (r->offsets) /* 20010421 MJD */
4633 Safefree(r->offsets);
4634 if (RX_MATCH_COPIED(r))
4635 Safefree(r->subbeg);
4637 if (r->anchored_substr)
4638 SvREFCNT_dec(r->anchored_substr);
4639 if (r->float_substr)
4640 SvREFCNT_dec(r->float_substr);
4641 Safefree(r->substrs);
4644 int n = r->data->count;
4645 AV* new_comppad = NULL;
4650 /* If you add a ->what type here, update the comment in regcomp.h */
4651 switch (r->data->what[n]) {
4653 SvREFCNT_dec((SV*)r->data->data[n]);
4656 Safefree(r->data->data[n]);
4659 new_comppad = (AV*)r->data->data[n];
4662 if (new_comppad == NULL)
4663 Perl_croak(aTHX_ "panic: pregfree comppad");
4664 old_comppad = PL_comppad;
4665 old_curpad = PL_curpad;
4666 /* Watch out for global destruction's random ordering. */
4667 if (SvTYPE(new_comppad) == SVt_PVAV) {
4668 PL_comppad = new_comppad;
4669 PL_curpad = AvARRAY(new_comppad);
4674 if (!OpREFCNT_dec((OP_4tree*)r->data->data[n])) {
4675 op_free((OP_4tree*)r->data->data[n]);
4678 PL_comppad = old_comppad;
4679 PL_curpad = old_curpad;
4680 SvREFCNT_dec((SV*)new_comppad);
4686 Perl_croak(aTHX_ "panic: regfree data code '%c'", r->data->what[n]);
4689 Safefree(r->data->what);
4692 Safefree(r->startp);
4698 - regnext - dig the "next" pointer out of a node
4700 * [Note, when REGALIGN is defined there are two places in regmatch()
4701 * that bypass this code for speed.]
4704 Perl_regnext(pTHX_ register regnode *p)
4706 register I32 offset;
4708 if (p == &PL_regdummy)
4711 offset = (reg_off_by_arg[OP(p)] ? ARG(p) : NEXT_OFF(p));
4719 S_re_croak2(pTHX_ const char* pat1,const char* pat2,...)
4722 STRLEN l1 = strlen(pat1);
4723 STRLEN l2 = strlen(pat2);
4732 Copy(pat1, buf, l1 , char);
4733 Copy(pat2, buf + l1, l2 , char);
4734 buf[l1 + l2] = '\n';
4735 buf[l1 + l2 + 1] = '\0';
4737 /* ANSI variant takes additional second argument */
4738 va_start(args, pat2);
4742 msv = vmess(buf, &args);
4744 message = SvPV(msv,l1);
4747 Copy(message, buf, l1 , char);
4748 buf[l1] = '\0'; /* Overwrite \n */
4749 Perl_croak(aTHX_ "%s", buf);
4752 /* XXX Here's a total kludge. But we need to re-enter for swash routines. */
4755 Perl_save_re_context(pTHX)
4758 SAVEPPTR(RExC_precomp); /* uncompiled string. */
4759 SAVEI32(RExC_npar); /* () count. */
4760 SAVEI32(RExC_size); /* Code size. */
4761 SAVEI16(RExC_flags16); /* are we folding, multilining? */
4762 SAVEVPTR(RExC_rx); /* from regcomp.c */
4763 SAVEI32(RExC_seen); /* from regcomp.c */
4764 SAVEI32(RExC_sawback); /* Did we see \1, ...? */
4765 SAVEI32(RExC_naughty); /* How bad is this pattern? */
4766 SAVEVPTR(RExC_emit); /* Code-emit pointer; ®dummy = don't */
4767 SAVEPPTR(RExC_end); /* End of input for compile */
4768 SAVEPPTR(RExC_parse); /* Input-scan pointer. */
4771 SAVEI32(PL_reg_flags); /* from regexec.c */
4773 SAVEPPTR(PL_reginput); /* String-input pointer. */
4774 SAVEPPTR(PL_regbol); /* Beginning of input, for ^ check. */
4775 SAVEPPTR(PL_regeol); /* End of input, for $ check. */
4776 SAVEVPTR(PL_regstartp); /* Pointer to startp array. */
4777 SAVEVPTR(PL_regendp); /* Ditto for endp. */
4778 SAVEVPTR(PL_reglastparen); /* Similarly for lastparen. */
4779 SAVEPPTR(PL_regtill); /* How far we are required to go. */
4780 SAVEGENERICPV(PL_reg_start_tmp); /* from regexec.c */
4781 PL_reg_start_tmp = 0;
4782 SAVEI32(PL_reg_start_tmpl); /* from regexec.c */
4783 PL_reg_start_tmpl = 0;
4784 SAVEVPTR(PL_regdata);
4785 SAVEI32(PL_reg_eval_set); /* from regexec.c */
4786 SAVEI32(PL_regnarrate); /* from regexec.c */
4787 SAVEVPTR(PL_regprogram); /* from regexec.c */
4788 SAVEINT(PL_regindent); /* from regexec.c */
4789 SAVEVPTR(PL_regcc); /* from regexec.c */
4790 SAVEVPTR(PL_curcop);
4791 SAVEVPTR(PL_reg_call_cc); /* from regexec.c */
4792 SAVEVPTR(PL_reg_re); /* from regexec.c */
4793 SAVEPPTR(PL_reg_ganch); /* from regexec.c */
4794 SAVESPTR(PL_reg_sv); /* from regexec.c */
4795 SAVEI8(PL_reg_match_utf8); /* from regexec.c */
4796 SAVEVPTR(PL_reg_magic); /* from regexec.c */
4797 SAVEI32(PL_reg_oldpos); /* from regexec.c */
4798 SAVEVPTR(PL_reg_oldcurpm); /* from regexec.c */
4799 SAVEVPTR(PL_reg_curpm); /* from regexec.c */
4800 SAVEI32(PL_regnpar); /* () count. */
4801 SAVEI32(PL_regsize); /* from regexec.c */
4803 SAVEPPTR(PL_reg_starttry); /* from regexec.c */
4808 clear_re(pTHX_ void *r)
4810 ReREFCNT_dec((regexp *)r);