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-1999, Larry Wall
74 **** You may distribute under the terms of either the GNU General Public
75 **** License or the Artistic License, as specified in the README file.
78 * Beware that some of this code is subtly aware of the way operator
79 * precedence is structured in regular expressions. Serious changes in
80 * regular-expression syntax might require a total rethink.
83 #define PERL_IN_REGCOMP_C
86 #ifdef PERL_IN_XSUB_RE
87 # if defined(PERL_CAPI) || defined(PERL_OBJECT)
102 # if defined(BUGGY_MSC6)
103 /* MSC 6.00A breaks on op/regexp.t test 85 unless we turn this off */
104 # pragma optimize("a",off)
105 /* But MSC 6.00A is happy with 'w', for aliases only across function calls*/
106 # pragma optimize("w",on )
107 # endif /* BUGGY_MSC6 */
111 #define STATIC static
114 #define ISMULT1(c) ((c) == '*' || (c) == '+' || (c) == '?')
115 #define ISMULT2(s) ((*s) == '*' || (*s) == '+' || (*s) == '?' || \
116 ((*s) == '{' && regcurly(s)))
118 #define PERL_META "^$.[()|?+*\\"
120 #define META "^$.[()|?+*\\"
124 #undef SPSTART /* dratted cpp namespace... */
127 * Flags to be passed up and down.
129 #define WORST 0 /* Worst case. */
130 #define HASWIDTH 0x1 /* Known to match non-null strings. */
131 #define SIMPLE 0x2 /* Simple enough to be STAR/PLUS operand. */
132 #define SPSTART 0x4 /* Starts with * or +. */
133 #define TRYAGAIN 0x8 /* Weeded out a declaration. */
135 /* Length of a variant. */
137 typedef struct scan_data_t {
143 I32 last_end; /* min value, <0 unless valid. */
146 SV **longest; /* Either &l_fixed, or &l_float. */
150 I32 offset_float_min;
151 I32 offset_float_max;
157 * Forward declarations for pregcomp()'s friends.
160 static scan_data_t zero_scan_data = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
163 #define SF_BEFORE_EOL (SF_BEFORE_SEOL|SF_BEFORE_MEOL)
164 #define SF_BEFORE_SEOL 0x1
165 #define SF_BEFORE_MEOL 0x2
166 #define SF_FIX_BEFORE_EOL (SF_FIX_BEFORE_SEOL|SF_FIX_BEFORE_MEOL)
167 #define SF_FL_BEFORE_EOL (SF_FL_BEFORE_SEOL|SF_FL_BEFORE_MEOL)
170 # define SF_FIX_SHIFT_EOL (0+2)
171 # define SF_FL_SHIFT_EOL (0+4)
173 # define SF_FIX_SHIFT_EOL (+2)
174 # define SF_FL_SHIFT_EOL (+4)
177 #define SF_FIX_BEFORE_SEOL (SF_BEFORE_SEOL << SF_FIX_SHIFT_EOL)
178 #define SF_FIX_BEFORE_MEOL (SF_BEFORE_MEOL << SF_FIX_SHIFT_EOL)
180 #define SF_FL_BEFORE_SEOL (SF_BEFORE_SEOL << SF_FL_SHIFT_EOL)
181 #define SF_FL_BEFORE_MEOL (SF_BEFORE_MEOL << SF_FL_SHIFT_EOL) /* 0x20 */
182 #define SF_IS_INF 0x40
183 #define SF_HAS_PAR 0x80
184 #define SF_IN_PAR 0x100
185 #define SF_HAS_EVAL 0x200
186 #define SCF_DO_SUBSTR 0x400
189 #define UTF (PL_reg_flags & RF_utf8)
190 #define LOC (PL_regflags & PMf_LOCALE)
191 #define FOLD (PL_regflags & PMf_FOLD)
193 #define OOB_CHAR8 1234
194 #define OOB_UTF8 123456
196 #define CHR_SVLEN(sv) (UTF ? sv_len_utf8(sv) : SvCUR(sv))
197 #define CHR_DIST(a,b) (UTF ? utf8_distance(a,b) : a - b)
199 static void clear_re(pTHXo_ void *r);
202 S_scan_commit(pTHX_ scan_data_t *data)
205 STRLEN l = CHR_SVLEN(data->last_found);
206 STRLEN old_l = CHR_SVLEN(*data->longest);
208 if ((l >= old_l) && ((l > old_l) || (data->flags & SF_BEFORE_EOL))) {
209 sv_setsv(*data->longest, data->last_found);
210 if (*data->longest == data->longest_fixed) {
211 data->offset_fixed = l ? data->last_start_min : data->pos_min;
212 if (data->flags & SF_BEFORE_EOL)
214 |= ((data->flags & SF_BEFORE_EOL) << SF_FIX_SHIFT_EOL);
216 data->flags &= ~SF_FIX_BEFORE_EOL;
219 data->offset_float_min = l ? data->last_start_min : data->pos_min;
220 data->offset_float_max = (l
221 ? data->last_start_max
222 : data->pos_min + data->pos_delta);
223 if (data->flags & SF_BEFORE_EOL)
225 |= ((data->flags & SF_BEFORE_EOL) << SF_FL_SHIFT_EOL);
227 data->flags &= ~SF_FL_BEFORE_EOL;
230 SvCUR_set(data->last_found, 0);
232 data->flags &= ~SF_BEFORE_EOL;
235 /* Stops at toplevel WHILEM as well as at `last'. At end *scanp is set
236 to the position after last scanned or to NULL. */
239 S_study_chunk(pTHX_ regnode **scanp, I32 *deltap, regnode *last, scan_data_t *data, U32 flags)
240 /* scanp: Start here (read-write). */
241 /* deltap: Write maxlen-minlen here. */
242 /* last: Stop before this one. */
245 I32 min = 0, pars = 0, code;
246 regnode *scan = *scanp, *next;
248 int is_inf = (flags & SCF_DO_SUBSTR) && (data->flags & SF_IS_INF);
249 int is_inf_internal = 0; /* The studied chunk is infinite */
250 I32 is_par = OP(scan) == OPEN ? ARG(scan) : 0;
251 scan_data_t data_fake;
253 while (scan && OP(scan) != END && scan < last) {
254 /* Peephole optimizer: */
256 if (PL_regkind[(U8)OP(scan)] == EXACT) {
257 regnode *n = regnext(scan);
260 regnode *stop = scan;
263 next = scan + (*OPERAND(scan) + 2 - 1)/sizeof(regnode) + 2;
264 /* Skip NOTHING, merge EXACT*. */
266 ( PL_regkind[(U8)OP(n)] == NOTHING ||
267 (stringok && (OP(n) == OP(scan))))
269 && NEXT_OFF(scan) + NEXT_OFF(n) < I16_MAX) {
270 if (OP(n) == TAIL || n > next)
272 if (PL_regkind[(U8)OP(n)] == NOTHING) {
273 NEXT_OFF(scan) += NEXT_OFF(n);
274 next = n + NODE_STEP_REGNODE;
282 int oldl = *OPERAND(scan);
283 regnode *nnext = regnext(n);
285 if (oldl + *OPERAND(n) > U8_MAX)
287 NEXT_OFF(scan) += NEXT_OFF(n);
288 *OPERAND(scan) += *OPERAND(n);
289 next = n + (*OPERAND(n) + 2 - 1)/sizeof(regnode) + 2;
290 /* Now we can overwrite *n : */
291 Move(OPERAND(n) + 1, OPERAND(scan) + oldl + 1,
292 *OPERAND(n) + 1, char);
302 n = scan + (*OPERAND(scan) + 2 - 1)/sizeof(regnode) + 2;
304 /* Purify reports a benign UMR here sometimes, because we
305 * don't initialize the OP() slot of a node when that node
306 * is occupied by just the trailing null of the string in
308 if (PL_regkind[(U8)OP(n)] != NOTHING || OP(n) == NOTHING) {
317 if (OP(scan) != CURLYX) {
318 int max = (reg_off_by_arg[OP(scan)]
320 /* I32 may be smaller than U16 on CRAYs! */
321 : (I32_MAX < U16_MAX ? I32_MAX : U16_MAX));
322 int off = (reg_off_by_arg[OP(scan)] ? ARG(scan) : NEXT_OFF(scan));
326 /* Skip NOTHING and LONGJMP. */
327 while ((n = regnext(n))
328 && ((PL_regkind[(U8)OP(n)] == NOTHING && (noff = NEXT_OFF(n)))
329 || ((OP(n) == LONGJMP) && (noff = ARG(n))))
332 if (reg_off_by_arg[OP(scan)])
335 NEXT_OFF(scan) = off;
337 if (OP(scan) == BRANCH || OP(scan) == BRANCHJ
338 || OP(scan) == IFTHEN || OP(scan) == SUSPEND) {
339 next = regnext(scan);
342 if (OP(next) == code || code == IFTHEN || code == SUSPEND) {
343 I32 max1 = 0, min1 = I32_MAX, num = 0;
345 if (flags & SCF_DO_SUBSTR)
347 while (OP(scan) == code) {
348 I32 deltanext, minnext;
353 data_fake.whilem_c = data->whilem_c;
354 next = regnext(scan);
355 scan = NEXTOPER(scan);
357 scan = NEXTOPER(scan);
358 /* We suppose the run is continuous, last=next...*/
359 minnext = study_chunk(&scan, &deltanext, next,
363 if (max1 < minnext + deltanext)
364 max1 = minnext + deltanext;
365 if (deltanext == I32_MAX)
366 is_inf = is_inf_internal = 1;
368 if (data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
370 if (data && (data_fake.flags & SF_HAS_EVAL))
371 data->flags |= SF_HAS_EVAL;
373 data->whilem_c = data_fake.whilem_c;
377 if (code == IFTHEN && num < 2) /* Empty ELSE branch */
379 if (flags & SCF_DO_SUBSTR) {
380 data->pos_min += min1;
381 data->pos_delta += max1 - min1;
382 if (max1 != min1 || is_inf)
383 data->longest = &(data->longest_float);
386 delta += max1 - min1;
388 else if (code == BRANCHJ) /* single branch is optimized. */
389 scan = NEXTOPER(NEXTOPER(scan));
390 else /* single branch is optimized. */
391 scan = NEXTOPER(scan);
394 else if (OP(scan) == EXACT) {
395 I32 l = *OPERAND(scan);
397 unsigned char *s = (unsigned char *)(OPERAND(scan)+1);
398 unsigned char *e = s + l;
407 if (flags & SCF_DO_SUBSTR) { /* Update longest substr. */
408 /* The code below prefers earlier match for fixed
409 offset, later match for variable offset. */
410 if (data->last_end == -1) { /* Update the start info. */
411 data->last_start_min = data->pos_min;
412 data->last_start_max = is_inf
413 ? I32_MAX : data->pos_min + data->pos_delta;
415 sv_catpvn(data->last_found, (char *)(OPERAND(scan)+1), *OPERAND(scan));
416 data->last_end = data->pos_min + l;
417 data->pos_min += l; /* As in the first entry. */
418 data->flags &= ~SF_BEFORE_EOL;
421 else if (PL_regkind[(U8)OP(scan)] == EXACT) {
422 I32 l = *OPERAND(scan);
423 if (flags & SCF_DO_SUBSTR)
426 unsigned char *s = (unsigned char *)(OPERAND(scan)+1);
427 unsigned char *e = s + l;
436 if (data && (flags & SCF_DO_SUBSTR))
439 else if (strchr((char*)PL_varies,OP(scan))) {
440 I32 mincount, maxcount, minnext, deltanext, pos_before, fl;
441 regnode *oscan = scan;
443 switch (PL_regkind[(U8)OP(scan)]) {
445 scan = NEXTOPER(scan);
448 if (flags & SCF_DO_SUBSTR) {
449 next = NEXTOPER(scan);
450 if (OP(next) == EXACT) {
452 maxcount = REG_INFTY;
453 next = regnext(scan);
454 scan = NEXTOPER(scan);
458 if (flags & SCF_DO_SUBSTR)
463 is_inf = is_inf_internal = 1;
464 scan = regnext(scan);
465 if (flags & SCF_DO_SUBSTR) {
467 data->longest = &(data->longest_float);
469 goto optimize_curly_tail;
471 mincount = ARG1(scan);
472 maxcount = ARG2(scan);
473 next = regnext(scan);
474 scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
476 if (flags & SCF_DO_SUBSTR) {
477 if (mincount == 0) scan_commit(data);
478 pos_before = data->pos_min;
482 data->flags &= ~(SF_HAS_PAR|SF_IN_PAR|SF_HAS_EVAL);
484 data->flags |= SF_IS_INF;
486 /* This will finish on WHILEM, setting scan, or on NULL: */
487 minnext = study_chunk(&scan, &deltanext, last, data,
489 ? (flags & ~SCF_DO_SUBSTR) : flags);
490 if (!scan) /* It was not CURLYX, but CURLY. */
492 if (ckWARN(WARN_UNSAFE) && (minnext + deltanext == 0)
493 && !(data->flags & (SF_HAS_PAR|SF_IN_PAR))
494 && maxcount <= REG_INFTY/3) /* Complement check for big count */
495 Perl_warner(aTHX_ WARN_UNSAFE,
496 "Strange *+?{} on zero-length expression");
497 min += minnext * mincount;
498 is_inf_internal |= (maxcount == REG_INFTY
499 && (minnext + deltanext) > 0
500 || deltanext == I32_MAX);
501 is_inf |= is_inf_internal;
502 delta += (minnext + deltanext) * maxcount - minnext * mincount;
504 /* Try powerful optimization CURLYX => CURLYN. */
505 if ( OP(oscan) == CURLYX && data
506 && data->flags & SF_IN_PAR
507 && !(data->flags & SF_HAS_EVAL)
508 && !deltanext && minnext == 1 ) {
509 /* Try to optimize to CURLYN. */
510 regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS;
511 regnode *nxt1 = nxt, *nxt2;
515 if (!strchr((char*)PL_simple,OP(nxt))
516 && !(PL_regkind[(U8)OP(nxt)] == EXACT
517 && *OPERAND(nxt) == 1))
521 if (OP(nxt) != CLOSE)
523 /* Now we know that nxt2 is the only contents: */
524 oscan->flags = ARG(nxt);
526 OP(nxt1) = NOTHING; /* was OPEN. */
528 OP(nxt1 + 1) = OPTIMIZED; /* was count. */
529 NEXT_OFF(nxt1+ 1) = 0; /* just for consistancy. */
530 NEXT_OFF(nxt2) = 0; /* just for consistancy with CURLY. */
531 OP(nxt) = OPTIMIZED; /* was CLOSE. */
532 OP(nxt + 1) = OPTIMIZED; /* was count. */
533 NEXT_OFF(nxt+ 1) = 0; /* just for consistancy. */
538 /* Try optimization CURLYX => CURLYM. */
539 if ( OP(oscan) == CURLYX && data
540 && !(data->flags & SF_HAS_PAR)
541 && !(data->flags & SF_HAS_EVAL)
543 /* XXXX How to optimize if data == 0? */
544 /* Optimize to a simpler form. */
545 regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN */
549 while ( (nxt2 = regnext(nxt)) /* skip over embedded stuff*/
550 && (OP(nxt2) != WHILEM))
552 OP(nxt2) = SUCCEED; /* Whas WHILEM */
553 /* Need to optimize away parenths. */
554 if (data->flags & SF_IN_PAR) {
555 /* Set the parenth number. */
556 regnode *nxt1 = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN*/
558 if (OP(nxt) != CLOSE)
559 FAIL("panic opt close");
560 oscan->flags = ARG(nxt);
561 OP(nxt1) = OPTIMIZED; /* was OPEN. */
562 OP(nxt) = OPTIMIZED; /* was CLOSE. */
564 OP(nxt1 + 1) = OPTIMIZED; /* was count. */
565 OP(nxt + 1) = OPTIMIZED; /* was count. */
566 NEXT_OFF(nxt1 + 1) = 0; /* just for consistancy. */
567 NEXT_OFF(nxt + 1) = 0; /* just for consistancy. */
570 while ( nxt1 && (OP(nxt1) != WHILEM)) {
571 regnode *nnxt = regnext(nxt1);
574 if (reg_off_by_arg[OP(nxt1)])
575 ARG_SET(nxt1, nxt2 - nxt1);
576 else if (nxt2 - nxt1 < U16_MAX)
577 NEXT_OFF(nxt1) = nxt2 - nxt1;
579 OP(nxt) = NOTHING; /* Cannot beautify */
584 /* Optimize again: */
585 study_chunk(&nxt1, &deltanext, nxt, NULL, 0);
590 else if (OP(oscan) == CURLYX && data && ++data->whilem_c < 16) {
591 /* This stays as CURLYX, and can put the count/of pair. */
592 /* Find WHILEM (as in regexec.c) */
593 regnode *nxt = oscan + NEXT_OFF(oscan);
595 if (OP(PREVOPER(nxt)) == NOTHING) /* LONGJMP */
597 PREVOPER(nxt)->flags = data->whilem_c
598 | (PL_reg_whilem_seen << 4); /* On WHILEM */
600 if (data && fl & (SF_HAS_PAR|SF_IN_PAR))
602 if (flags & SCF_DO_SUBSTR) {
603 SV *last_str = Nullsv;
604 int counted = mincount != 0;
606 if (data->last_end > 0 && mincount != 0) { /* Ends with a string. */
607 I32 b = pos_before >= data->last_start_min
608 ? pos_before : data->last_start_min;
610 char *s = SvPV(data->last_found, l);
611 I32 old = b - data->last_start_min;
614 old = utf8_hop((U8*)s, old) - (U8*)s;
617 /* Get the added string: */
618 last_str = newSVpvn(s + old, l);
619 if (deltanext == 0 && pos_before == b) {
620 /* What was added is a constant string */
622 SvGROW(last_str, (mincount * l) + 1);
623 repeatcpy(SvPVX(last_str) + l,
624 SvPVX(last_str), l, mincount - 1);
625 SvCUR(last_str) *= mincount;
626 /* Add additional parts. */
627 SvCUR_set(data->last_found,
628 SvCUR(data->last_found) - l);
629 sv_catsv(data->last_found, last_str);
630 data->last_end += l * (mincount - 1);
634 /* It is counted once already... */
635 data->pos_min += minnext * (mincount - counted);
636 data->pos_delta += - counted * deltanext +
637 (minnext + deltanext) * maxcount - minnext * mincount;
638 if (mincount != maxcount) {
640 if (mincount && last_str) {
641 sv_setsv(data->last_found, last_str);
642 data->last_end = data->pos_min;
643 data->last_start_min =
644 data->pos_min - CHR_SVLEN(last_str);
645 data->last_start_max = is_inf
647 : data->pos_min + data->pos_delta
648 - CHR_SVLEN(last_str);
650 data->longest = &(data->longest_float);
652 SvREFCNT_dec(last_str);
654 if (data && (fl & SF_HAS_EVAL))
655 data->flags |= SF_HAS_EVAL;
657 if (OP(oscan) != CURLYX) {
658 while (PL_regkind[(U8)OP(next = regnext(oscan))] == NOTHING
660 NEXT_OFF(oscan) += NEXT_OFF(next);
663 default: /* REF only? */
664 if (flags & SCF_DO_SUBSTR) {
666 data->longest = &(data->longest_float);
668 is_inf = is_inf_internal = 1;
672 else if (strchr((char*)PL_simple,OP(scan)) || PL_regkind[(U8)OP(scan)] == ANYUTF8) {
673 if (flags & SCF_DO_SUBSTR) {
679 else if (PL_regkind[(U8)OP(scan)] == EOL && flags & SCF_DO_SUBSTR) {
680 data->flags |= (OP(scan) == MEOL
684 else if (PL_regkind[(U8)OP(scan)] == BRANCHJ
685 && (scan->flags || data)
686 && (OP(scan) == IFMATCH || OP(scan) == UNLESSM)) {
687 I32 deltanext, minnext;
692 data_fake.whilem_c = data->whilem_c;
693 next = regnext(scan);
694 nscan = NEXTOPER(NEXTOPER(scan));
695 minnext = study_chunk(&nscan, &deltanext, last, &data_fake, 0);
698 FAIL("variable length lookbehind not implemented");
700 else if (minnext > U8_MAX) {
701 FAIL2("lookbehind longer than %d not implemented", U8_MAX);
703 scan->flags = minnext;
705 if (data && data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
707 if (data && (data_fake.flags & SF_HAS_EVAL))
708 data->flags |= SF_HAS_EVAL;
710 data->whilem_c = data_fake.whilem_c;
712 else if (OP(scan) == OPEN) {
715 else if (OP(scan) == CLOSE && ARG(scan) == is_par) {
716 next = regnext(scan);
718 if ( next && (OP(next) != WHILEM) && next < last)
719 is_par = 0; /* Disable optimization */
721 else if (OP(scan) == EVAL) {
723 data->flags |= SF_HAS_EVAL;
725 else if (OP(scan) == LOGICAL && scan->flags == 2) { /* Embedded */
726 if (flags & SCF_DO_SUBSTR) {
728 data->longest = &(data->longest_float);
730 is_inf = is_inf_internal = 1;
732 /* Else: zero-length, ignore. */
733 scan = regnext(scan);
738 *deltap = is_inf_internal ? I32_MAX : delta;
739 if (flags & SCF_DO_SUBSTR && is_inf)
740 data->pos_delta = I32_MAX - data->pos_min;
743 if (is_par && pars==1 && data) {
744 data->flags |= SF_IN_PAR;
745 data->flags &= ~SF_HAS_PAR;
747 else if (pars && data) {
748 data->flags |= SF_HAS_PAR;
749 data->flags &= ~SF_IN_PAR;
755 S_add_data(pTHX_ I32 n, char *s)
758 if (PL_regcomp_rx->data) {
759 Renewc(PL_regcomp_rx->data,
760 sizeof(*PL_regcomp_rx->data) + sizeof(void*) * (PL_regcomp_rx->data->count + n - 1),
761 char, struct reg_data);
762 Renew(PL_regcomp_rx->data->what, PL_regcomp_rx->data->count + n, U8);
763 PL_regcomp_rx->data->count += n;
766 Newc(1207, PL_regcomp_rx->data, sizeof(*PL_regcomp_rx->data) + sizeof(void*) * (n - 1),
767 char, struct reg_data);
768 New(1208, PL_regcomp_rx->data->what, n, U8);
769 PL_regcomp_rx->data->count = n;
771 Copy(s, PL_regcomp_rx->data->what + PL_regcomp_rx->data->count - n, n, U8);
772 return PL_regcomp_rx->data->count - n;
776 Perl_reginitcolors(pTHX)
780 char *s = PerlEnv_getenv("PERL_RE_COLORS");
783 PL_colors[0] = s = savepv(s);
791 PL_colors[i] = s = "";
801 - pregcomp - compile a regular expression into internal code
803 * We can't allocate space until we know how big the compiled form will be,
804 * but we can't compile it (and thus know how big it is) until we've got a
805 * place to put the code. So we cheat: we compile it twice, once with code
806 * generation turned off and size counting turned on, and once "for real".
807 * This also means that we don't allocate space until we are sure that the
808 * thing really will compile successfully, and we never have to move the
809 * code and thus invalidate pointers into it. (Note that it has to be in
810 * one piece because free() must be able to free it all.) [NB: not true in perl]
812 * Beware that the optimization-preparation code in here knows about some
813 * of the structure of the compiled regexp. [I'll say.]
816 Perl_pregcomp(pTHX_ char *exp, char *xend, PMOP *pm)
832 FAIL("NULL regexp argument");
834 if (PL_curcop == &PL_compiling ? (PL_hints & HINT_UTF8) : IN_UTF8)
835 PL_reg_flags |= RF_utf8;
839 PL_regprecomp = savepvn(exp, xend - exp);
840 DEBUG_r(if (!PL_colorset) reginitcolors());
841 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sCompiling REx%s `%s%*s%s'\n",
842 PL_colors[4],PL_colors[5],PL_colors[0],
843 xend - exp, PL_regprecomp, PL_colors[1]));
844 PL_regflags = pm->op_pmflags;
848 PL_seen_zerolen = *exp == '^' ? -1 : 0;
852 /* First pass: determine size, legality. */
853 PL_regcomp_parse = exp;
858 PL_regcode = &PL_regdummy;
859 PL_reg_whilem_seen = 0;
860 regc((U8)REG_MAGIC, (char*)PL_regcode);
861 if (reg(0, &flags) == NULL) {
862 Safefree(PL_regprecomp);
863 PL_regprecomp = Nullch;
866 DEBUG_r(PerlIO_printf(Perl_debug_log, "size %d ", PL_regsize));
868 /* Small enough for pointer-storage convention?
869 If extralen==0, this means that we will not need long jumps. */
870 if (PL_regsize >= 0x10000L && PL_extralen)
871 PL_regsize += PL_extralen;
874 if (PL_reg_whilem_seen > 15)
875 PL_reg_whilem_seen = 15;
877 /* Allocate space and initialize. */
878 Newc(1001, r, sizeof(regexp) + (unsigned)PL_regsize * sizeof(regnode),
881 FAIL("regexp out of space");
883 r->prelen = xend - exp;
884 r->precomp = PL_regprecomp;
886 r->reganch = pm->op_pmflags & PMf_COMPILETIME;
887 r->nparens = PL_regnpar - 1; /* set early to validate backrefs */
889 r->substrs = 0; /* Useful during FAIL. */
890 r->startp = 0; /* Useful during FAIL. */
891 r->endp = 0; /* Useful during FAIL. */
895 /* Second pass: emit code. */
896 PL_regcomp_parse = exp;
900 PL_regcode = r->program;
901 /* Store the count of eval-groups for security checks: */
902 PL_regcode->next_off = ((PL_seen_evals > U16_MAX) ? U16_MAX : PL_seen_evals);
903 regc((U8)REG_MAGIC, (char*) PL_regcode++);
905 if (reg(0, &flags) == NULL)
908 /* Dig out information for optimizations. */
909 r->reganch = pm->op_pmflags & PMf_COMPILETIME; /* Again? */
910 pm->op_pmflags = PL_regflags;
912 r->reganch |= ROPT_UTF8;
913 r->regstclass = NULL;
914 if (PL_regnaughty >= 10) /* Probably an expensive pattern. */
915 r->reganch |= ROPT_NAUGHTY;
916 scan = r->program + 1; /* First BRANCH. */
918 /* XXXX To minimize changes to RE engine we always allocate
919 3-units-long substrs field. */
920 Newz(1004, r->substrs, 1, struct reg_substr_data);
922 StructCopy(&zero_scan_data, &data, scan_data_t);
923 if (OP(scan) != BRANCH) { /* Only one top-level choice. */
925 STRLEN longest_float_length, longest_fixed_length;
928 /* Skip introductions and multiplicators >= 1. */
929 while ((OP(first) == OPEN && (sawopen = 1)) ||
930 (OP(first) == BRANCH && OP(regnext(first)) != BRANCH) ||
931 (OP(first) == PLUS) ||
932 (OP(first) == MINMOD) ||
933 (PL_regkind[(U8)OP(first)] == CURLY && ARG1(first) > 0) ) {
934 if (OP(first) == PLUS)
937 first += regarglen[(U8)OP(first)];
938 first = NEXTOPER(first);
941 /* Starting-point info. */
943 if (OP(first) == EXACT); /* Empty, get anchored substr later. */
944 else if (strchr((char*)PL_simple+4,OP(first)))
945 r->regstclass = first;
946 else if (PL_regkind[(U8)OP(first)] == BOUND ||
947 PL_regkind[(U8)OP(first)] == NBOUND)
948 r->regstclass = first;
949 else if (PL_regkind[(U8)OP(first)] == BOL) {
950 r->reganch |= (OP(first) == MBOL
955 first = NEXTOPER(first);
958 else if (OP(first) == GPOS) {
959 r->reganch |= ROPT_ANCH_GPOS;
960 first = NEXTOPER(first);
963 else if ((OP(first) == STAR &&
964 PL_regkind[(U8)OP(NEXTOPER(first))] == REG_ANY) &&
965 !(r->reganch & ROPT_ANCH) )
967 /* turn .* into ^.* with an implied $*=1 */
968 int type = OP(NEXTOPER(first));
970 if (type == REG_ANY || type == ANYUTF8)
971 type = ROPT_ANCH_MBOL;
973 type = ROPT_ANCH_SBOL;
975 r->reganch |= type | ROPT_IMPLICIT;
976 first = NEXTOPER(first);
979 if (sawplus && (!sawopen || !PL_regsawback)
980 && !(PL_regseen & REG_SEEN_EVAL)) /* May examine pos and $& */
981 /* x+ must match at the 1st pos of run of x's */
982 r->reganch |= ROPT_SKIP;
984 /* Scan is after the zeroth branch, first is atomic matcher. */
985 DEBUG_r(PerlIO_printf(Perl_debug_log, "first at %d\n",
988 * If there's something expensive in the r.e., find the
989 * longest literal string that must appear and make it the
990 * regmust. Resolve ties in favor of later strings, since
991 * the regstart check works with the beginning of the r.e.
992 * and avoiding duplication strengthens checking. Not a
993 * strong reason, but sufficient in the absence of others.
994 * [Now we resolve ties in favor of the earlier string if
995 * it happens that c_offset_min has been invalidated, since the
996 * earlier string may buy us something the later one won't.]
1000 data.longest_fixed = newSVpvn("",0);
1001 data.longest_float = newSVpvn("",0);
1002 data.last_found = newSVpvn("",0);
1003 data.longest = &(data.longest_fixed);
1006 minlen = study_chunk(&first, &fake, scan + PL_regsize, /* Up to end */
1007 &data, SCF_DO_SUBSTR);
1008 if ( PL_regnpar == 1 && data.longest == &(data.longest_fixed)
1009 && data.last_start_min == 0 && data.last_end > 0
1011 && (!(PL_regseen & REG_SEEN_GPOS) || (r->reganch & ROPT_ANCH_GPOS)))
1012 r->reganch |= ROPT_CHECK_ALL;
1014 SvREFCNT_dec(data.last_found);
1016 longest_float_length = CHR_SVLEN(data.longest_float);
1017 if (longest_float_length
1018 || (data.flags & SF_FL_BEFORE_EOL
1019 && (!(data.flags & SF_FL_BEFORE_MEOL)
1020 || (PL_regflags & PMf_MULTILINE)))) {
1023 if (SvCUR(data.longest_fixed) /* ok to leave SvCUR */
1024 && data.offset_fixed == data.offset_float_min
1025 && SvCUR(data.longest_fixed) == SvCUR(data.longest_float))
1026 goto remove_float; /* As in (a)+. */
1028 r->float_substr = data.longest_float;
1029 r->float_min_offset = data.offset_float_min;
1030 r->float_max_offset = data.offset_float_max;
1031 t = (data.flags & SF_FL_BEFORE_EOL /* Can't have SEOL and MULTI */
1032 && (!(data.flags & SF_FL_BEFORE_MEOL)
1033 || (PL_regflags & PMf_MULTILINE)));
1034 fbm_compile(r->float_substr, t ? FBMcf_TAIL : 0);
1038 r->float_substr = Nullsv;
1039 SvREFCNT_dec(data.longest_float);
1040 longest_float_length = 0;
1043 longest_fixed_length = CHR_SVLEN(data.longest_fixed);
1044 if (longest_fixed_length
1045 || (data.flags & SF_FIX_BEFORE_EOL /* Cannot have SEOL and MULTI */
1046 && (!(data.flags & SF_FIX_BEFORE_MEOL)
1047 || (PL_regflags & PMf_MULTILINE)))) {
1050 r->anchored_substr = data.longest_fixed;
1051 r->anchored_offset = data.offset_fixed;
1052 t = (data.flags & SF_FIX_BEFORE_EOL /* Can't have SEOL and MULTI */
1053 && (!(data.flags & SF_FIX_BEFORE_MEOL)
1054 || (PL_regflags & PMf_MULTILINE)));
1055 fbm_compile(r->anchored_substr, t ? FBMcf_TAIL : 0);
1058 r->anchored_substr = Nullsv;
1059 SvREFCNT_dec(data.longest_fixed);
1060 longest_fixed_length = 0;
1063 /* A temporary algorithm prefers floated substr to fixed one to dig more info. */
1064 if (longest_fixed_length > longest_float_length) {
1065 r->check_substr = r->anchored_substr;
1066 r->check_offset_min = r->check_offset_max = r->anchored_offset;
1067 if (r->reganch & ROPT_ANCH_SINGLE)
1068 r->reganch |= ROPT_NOSCAN;
1071 r->check_substr = r->float_substr;
1072 r->check_offset_min = data.offset_float_min;
1073 r->check_offset_max = data.offset_float_max;
1075 if (r->check_substr) {
1076 r->reganch |= RE_USE_INTUIT;
1077 if (SvTAIL(r->check_substr))
1078 r->reganch |= RE_INTUIT_TAIL;
1082 /* Several toplevels. Best we can is to set minlen. */
1085 DEBUG_r(PerlIO_printf(Perl_debug_log, "\n"));
1086 scan = r->program + 1;
1087 minlen = study_chunk(&scan, &fake, scan + PL_regsize, &data, 0);
1088 r->check_substr = r->anchored_substr = r->float_substr = Nullsv;
1092 if (PL_regseen & REG_SEEN_GPOS)
1093 r->reganch |= ROPT_GPOS_SEEN;
1094 if (PL_regseen & REG_SEEN_LOOKBEHIND)
1095 r->reganch |= ROPT_LOOKBEHIND_SEEN;
1096 if (PL_regseen & REG_SEEN_EVAL)
1097 r->reganch |= ROPT_EVAL_SEEN;
1098 Newz(1002, r->startp, PL_regnpar, I32);
1099 Newz(1002, r->endp, PL_regnpar, I32);
1100 DEBUG_r(regdump(r));
1105 - reg - regular expression, i.e. main body or parenthesized thing
1107 * Caller must absorb opening parenthesis.
1109 * Combining parenthesis handling with the base level of regular expression
1110 * is a trifle forced, but the need to tie the tails of the branches to what
1111 * follows makes it hard to avoid.
1114 S_reg(pTHX_ I32 paren, I32 *flagp)
1115 /* paren: Parenthesized? 0=top, 1=(, inside: changed to letter. */
1118 register regnode *ret; /* Will be the head of the group. */
1119 register regnode *br;
1120 register regnode *lastbr;
1121 register regnode *ender = 0;
1122 register I32 parno = 0;
1123 I32 flags, oregflags = PL_regflags, have_branch = 0, open = 0;
1126 *flagp = 0; /* Tentatively. */
1128 /* Make an OPEN node, if parenthesized. */
1130 if (*PL_regcomp_parse == '?') {
1131 U16 posflags = 0, negflags = 0;
1132 U16 *flagsp = &posflags;
1136 paren = *PL_regcomp_parse++;
1137 ret = NULL; /* For look-ahead/behind. */
1140 PL_regseen |= REG_SEEN_LOOKBEHIND;
1141 if (*PL_regcomp_parse == '!')
1143 if (*PL_regcomp_parse != '=' && *PL_regcomp_parse != '!')
1154 FAIL2("Sequence (?%c...) not implemented", (int)paren);
1157 while (*PL_regcomp_parse && *PL_regcomp_parse != ')')
1159 if (*PL_regcomp_parse != ')')
1160 FAIL("Sequence (?#... not terminated");
1166 paren = *PL_regcomp_parse++;
1171 I32 count = 1, n = 0;
1173 char *s = PL_regcomp_parse;
1175 OP_4tree *sop, *rop;
1178 PL_regseen |= REG_SEEN_EVAL;
1179 while (count && (c = *PL_regcomp_parse)) {
1180 if (c == '\\' && PL_regcomp_parse[1])
1188 if (*PL_regcomp_parse != ')')
1189 FAIL("Sequence (?{...}) not terminated or not {}-balanced");
1193 if (PL_regcomp_parse - 1 - s)
1194 sv = newSVpvn(s, PL_regcomp_parse - 1 - s);
1196 sv = newSVpvn("", 0);
1198 rop = sv_compile_2op(sv, &sop, "re", &av);
1200 n = add_data(3, "nop");
1201 PL_regcomp_rx->data->data[n] = (void*)rop;
1202 PL_regcomp_rx->data->data[n+1] = (void*)sop;
1203 PL_regcomp_rx->data->data[n+2] = (void*)av;
1206 else { /* First pass */
1207 if (PL_reginterp_cnt < ++PL_seen_evals
1208 && PL_curcop != &PL_compiling)
1209 /* No compiled RE interpolated, has runtime
1210 components ===> unsafe. */
1211 FAIL("Eval-group not allowed at runtime, use re 'eval'");
1213 FAIL("Eval-group in insecure regular expression");
1218 ret = reg_node(LOGICAL);
1221 regtail(ret, reganode(EVAL, n));
1224 return reganode(EVAL, n);
1228 if (PL_regcomp_parse[0] == '?') {
1229 if (PL_regcomp_parse[1] == '=' || PL_regcomp_parse[1] == '!'
1230 || PL_regcomp_parse[1] == '<'
1231 || PL_regcomp_parse[1] == '{') { /* Lookahead or eval. */
1234 ret = reg_node(LOGICAL);
1237 regtail(ret, reg(1, &flag));
1241 else if (PL_regcomp_parse[0] >= '1' && PL_regcomp_parse[0] <= '9' ) {
1242 parno = atoi(PL_regcomp_parse++);
1244 while (isDIGIT(*PL_regcomp_parse))
1246 ret = reganode(GROUPP, parno);
1247 if ((c = *nextchar()) != ')')
1248 FAIL2("Switch (?(number%c not recognized", c);
1250 regtail(ret, reganode(IFTHEN, 0));
1251 br = regbranch(&flags, 1);
1253 br = reganode(LONGJMP, 0);
1255 regtail(br, reganode(LONGJMP, 0));
1260 lastbr = reganode(IFTHEN, 0); /* Fake one for optimizer. */
1261 regbranch(&flags, 1);
1262 regtail(ret, lastbr);
1270 FAIL("Switch (?(condition)... contains too many branches");
1271 ender = reg_node(TAIL);
1274 regtail(lastbr, ender);
1275 regtail(NEXTOPER(NEXTOPER(lastbr)), ender);
1278 regtail(ret, ender);
1282 FAIL2("Unknown condition for (?(%.2s", PL_regcomp_parse);
1286 FAIL("Sequence (? incomplete");
1291 while (*PL_regcomp_parse && strchr("iogcmsx", *PL_regcomp_parse)) {
1292 if (*PL_regcomp_parse != 'o')
1293 pmflag(flagsp, *PL_regcomp_parse);
1296 if (*PL_regcomp_parse == '-') {
1301 PL_regflags |= posflags;
1302 PL_regflags &= ~negflags;
1303 if (*PL_regcomp_parse == ':') {
1309 if (*PL_regcomp_parse != ')')
1310 FAIL2("Sequence (?%c...) not recognized", *PL_regcomp_parse);
1319 ret = reganode(OPEN, parno);
1326 /* Pick up the branches, linking them together. */
1327 br = regbranch(&flags, 1);
1330 if (*PL_regcomp_parse == '|') {
1331 if (!SIZE_ONLY && PL_extralen) {
1332 reginsert(BRANCHJ, br);
1335 reginsert(BRANCH, br);
1338 PL_extralen += 1; /* For BRANCHJ-BRANCH. */
1340 else if (paren == ':') {
1341 *flagp |= flags&SIMPLE;
1343 if (open) { /* Starts with OPEN. */
1344 regtail(ret, br); /* OPEN -> first. */
1346 else if (paren != '?') /* Not Conditional */
1350 *flagp |= flags&SPSTART;
1352 while (*PL_regcomp_parse == '|') {
1353 if (!SIZE_ONLY && PL_extralen) {
1354 ender = reganode(LONGJMP,0);
1355 regtail(NEXTOPER(NEXTOPER(lastbr)), ender); /* Append to the previous. */
1358 PL_extralen += 2; /* Account for LONGJMP. */
1360 br = regbranch(&flags, 0);
1363 regtail(lastbr, br); /* BRANCH -> BRANCH. */
1367 *flagp |= flags&SPSTART;
1370 if (have_branch || paren != ':') {
1371 /* Make a closing node, and hook it on the end. */
1374 ender = reg_node(TAIL);
1377 ender = reganode(CLOSE, parno);
1383 *flagp &= ~HASWIDTH;
1386 ender = reg_node(SUCCEED);
1389 ender = reg_node(END);
1392 regtail(lastbr, ender);
1395 /* Hook the tails of the branches to the closing node. */
1396 for (br = ret; br != NULL; br = regnext(br)) {
1397 regoptail(br, ender);
1404 static char parens[] = "=!<,>";
1406 if (paren && (p = strchr(parens, paren))) {
1407 int node = ((p - parens) % 2) ? UNLESSM : IFMATCH;
1408 int flag = (p - parens) > 1;
1411 node = SUSPEND, flag = 0;
1412 reginsert(node,ret);
1414 regtail(ret, reg_node(TAIL));
1418 /* Check for proper termination. */
1420 PL_regflags = oregflags;
1421 if (PL_regcomp_parse >= PL_regxend || *nextchar() != ')') {
1422 FAIL("unmatched () in regexp");
1425 else if (!paren && PL_regcomp_parse < PL_regxend) {
1426 if (*PL_regcomp_parse == ')') {
1427 FAIL("unmatched () in regexp");
1430 FAIL("junk on end of regexp"); /* "Can't happen". */
1438 - regbranch - one alternative of an | operator
1440 * Implements the concatenation operator.
1443 S_regbranch(pTHX_ I32 *flagp, I32 first)
1446 register regnode *ret;
1447 register regnode *chain = NULL;
1448 register regnode *latest;
1449 I32 flags = 0, c = 0;
1454 if (!SIZE_ONLY && PL_extralen)
1455 ret = reganode(BRANCHJ,0);
1457 ret = reg_node(BRANCH);
1460 if (!first && SIZE_ONLY)
1461 PL_extralen += 1; /* BRANCHJ */
1463 *flagp = WORST; /* Tentatively. */
1467 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != '|' && *PL_regcomp_parse != ')') {
1469 latest = regpiece(&flags);
1470 if (latest == NULL) {
1471 if (flags & TRYAGAIN)
1475 else if (ret == NULL)
1477 *flagp |= flags&HASWIDTH;
1478 if (chain == NULL) /* First piece. */
1479 *flagp |= flags&SPSTART;
1482 regtail(chain, latest);
1487 if (chain == NULL) { /* Loop ran zero times. */
1488 chain = reg_node(NOTHING);
1493 *flagp |= flags&SIMPLE;
1500 - regpiece - something followed by possible [*+?]
1502 * Note that the branching code sequences used for ? and the general cases
1503 * of * and + are somewhat optimized: they use the same NOTHING node as
1504 * both the endmarker for their branch list and the body of the last branch.
1505 * It might seem that this node could be dispensed with entirely, but the
1506 * endmarker role is not redundant.
1509 S_regpiece(pTHX_ I32 *flagp)
1512 register regnode *ret;
1514 register char *next;
1516 char *origparse = PL_regcomp_parse;
1519 I32 max = REG_INFTY;
1521 ret = regatom(&flags);
1523 if (flags & TRYAGAIN)
1528 op = *PL_regcomp_parse;
1530 if (op == '{' && regcurly(PL_regcomp_parse)) {
1531 next = PL_regcomp_parse + 1;
1533 while (isDIGIT(*next) || *next == ',') {
1542 if (*next == '}') { /* got one */
1546 min = atoi(PL_regcomp_parse);
1550 maxpos = PL_regcomp_parse;
1552 if (!max && *maxpos != '0')
1553 max = REG_INFTY; /* meaning "infinity" */
1554 else if (max >= REG_INFTY)
1555 FAIL2("Quantifier in {,} bigger than %d", REG_INFTY - 1);
1556 PL_regcomp_parse = next;
1560 if ((flags&SIMPLE)) {
1561 PL_regnaughty += 2 + PL_regnaughty / 2;
1562 reginsert(CURLY, ret);
1565 regnode *w = reg_node(WHILEM);
1569 if (!SIZE_ONLY && PL_extralen) {
1570 reginsert(LONGJMP,ret);
1571 reginsert(NOTHING,ret);
1572 NEXT_OFF(ret) = 3; /* Go over LONGJMP. */
1574 reginsert(CURLYX,ret);
1575 if (!SIZE_ONLY && PL_extralen)
1576 NEXT_OFF(ret) = 3; /* Go over NOTHING to LONGJMP. */
1577 regtail(ret, reg_node(NOTHING));
1579 PL_reg_whilem_seen++, PL_extralen += 3;
1580 PL_regnaughty += 4 + PL_regnaughty; /* compound interest */
1588 if (max && max < min)
1589 FAIL("Can't do {n,m} with n > m");
1604 #if 0 /* Now runtime fix should be reliable. */
1605 if (!(flags&HASWIDTH) && op != '?')
1606 FAIL("regexp *+ operand could be empty");
1611 *flagp = (op != '+') ? (WORST|SPSTART|HASWIDTH) : (WORST|HASWIDTH);
1613 if (op == '*' && (flags&SIMPLE)) {
1614 reginsert(STAR, ret);
1618 else if (op == '*') {
1622 else if (op == '+' && (flags&SIMPLE)) {
1623 reginsert(PLUS, ret);
1627 else if (op == '+') {
1631 else if (op == '?') {
1636 if (ckWARN(WARN_UNSAFE) && !SIZE_ONLY && !(flags&HASWIDTH) && max > REG_INFTY/3) {
1637 Perl_warner(aTHX_ WARN_UNSAFE, "%.*s matches null string many times",
1638 PL_regcomp_parse - origparse, origparse);
1641 if (*PL_regcomp_parse == '?') {
1643 reginsert(MINMOD, ret);
1644 regtail(ret, ret + NODE_STEP_REGNODE);
1646 if (ISMULT2(PL_regcomp_parse))
1647 FAIL("nested *?+ in regexp");
1653 - regatom - the lowest level
1655 * Optimization: gobbles an entire sequence of ordinary characters so that
1656 * it can turn them into a single node, which is smaller to store and
1657 * faster to run. Backslashed characters are exceptions, each becoming a
1658 * separate node; the code is simpler that way and it's not worth fixing.
1660 * [Yes, it is worth fixing, some scripts can run twice the speed.]
1663 S_regatom(pTHX_ I32 *flagp)
1666 register regnode *ret = 0;
1669 *flagp = WORST; /* Tentatively. */
1672 switch (*PL_regcomp_parse) {
1676 if (PL_regflags & PMf_MULTILINE)
1677 ret = reg_node(MBOL);
1678 else if (PL_regflags & PMf_SINGLELINE)
1679 ret = reg_node(SBOL);
1681 ret = reg_node(BOL);
1684 if (PL_regcomp_parse[1])
1687 if (PL_regflags & PMf_MULTILINE)
1688 ret = reg_node(MEOL);
1689 else if (PL_regflags & PMf_SINGLELINE)
1690 ret = reg_node(SEOL);
1692 ret = reg_node(EOL);
1697 if (PL_regflags & PMf_SINGLELINE)
1698 ret = reg_node(SANYUTF8);
1700 ret = reg_node(ANYUTF8);
1704 if (PL_regflags & PMf_SINGLELINE)
1705 ret = reg_node(SANY);
1707 ret = reg_node(REG_ANY);
1708 *flagp |= HASWIDTH|SIMPLE;
1714 ret = (UTF ? regclassutf8() : regclass());
1715 if (*PL_regcomp_parse != ']')
1716 FAIL("unmatched [] in regexp");
1718 *flagp |= HASWIDTH|SIMPLE;
1722 ret = reg(1, &flags);
1724 if (flags & TRYAGAIN)
1728 *flagp |= flags&(HASWIDTH|SPSTART|SIMPLE);
1732 if (flags & TRYAGAIN) {
1736 FAIL2("internal urp in regexp at /%s/", PL_regcomp_parse);
1737 /* Supposed to be caught earlier. */
1740 if (!regcurly(PL_regcomp_parse)) {
1748 FAIL("?+*{} follows nothing in regexp");
1751 switch (*++PL_regcomp_parse) {
1754 ret = reg_node(SBOL);
1759 ret = reg_node(GPOS);
1760 PL_regseen |= REG_SEEN_GPOS;
1765 ret = reg_node(SEOL);
1770 ret = reg_node(EOS);
1772 PL_seen_zerolen++; /* Do not optimize RE away */
1776 ret = reg_node(SANY);
1777 *flagp |= HASWIDTH|SIMPLE;
1781 ret = reg_node(CLUMP);
1784 if (UTF && !PL_utf8_mark)
1785 is_utf8_mark((U8*)"~"); /* preload table */
1790 ? (LOC ? ALNUMLUTF8 : ALNUMUTF8)
1791 : (LOC ? ALNUML : ALNUM));
1792 *flagp |= HASWIDTH|SIMPLE;
1794 if (UTF && !PL_utf8_alnum)
1795 is_utf8_alnum((U8*)"a"); /* preload table */
1800 ? (LOC ? NALNUMLUTF8 : NALNUMUTF8)
1801 : (LOC ? NALNUML : NALNUM));
1802 *flagp |= HASWIDTH|SIMPLE;
1804 if (UTF && !PL_utf8_alnum)
1805 is_utf8_alnum((U8*)"a"); /* preload table */
1809 PL_regseen |= REG_SEEN_LOOKBEHIND;
1812 ? (LOC ? BOUNDLUTF8 : BOUNDUTF8)
1813 : (LOC ? BOUNDL : BOUND));
1816 if (UTF && !PL_utf8_alnum)
1817 is_utf8_alnum((U8*)"a"); /* preload table */
1821 PL_regseen |= REG_SEEN_LOOKBEHIND;
1824 ? (LOC ? NBOUNDLUTF8 : NBOUNDUTF8)
1825 : (LOC ? NBOUNDL : NBOUND));
1828 if (UTF && !PL_utf8_alnum)
1829 is_utf8_alnum((U8*)"a"); /* preload table */
1834 ? (LOC ? SPACELUTF8 : SPACEUTF8)
1835 : (LOC ? SPACEL : SPACE));
1836 *flagp |= HASWIDTH|SIMPLE;
1838 if (UTF && !PL_utf8_space)
1839 is_utf8_space((U8*)" "); /* preload table */
1844 ? (LOC ? NSPACELUTF8 : NSPACEUTF8)
1845 : (LOC ? NSPACEL : NSPACE));
1846 *flagp |= HASWIDTH|SIMPLE;
1848 if (UTF && !PL_utf8_space)
1849 is_utf8_space((U8*)" "); /* preload table */
1852 ret = reg_node(UTF ? DIGITUTF8 : DIGIT);
1853 *flagp |= HASWIDTH|SIMPLE;
1855 if (UTF && !PL_utf8_digit)
1856 is_utf8_digit((U8*)"1"); /* preload table */
1859 ret = reg_node(UTF ? NDIGITUTF8 : NDIGIT);
1860 *flagp |= HASWIDTH|SIMPLE;
1862 if (UTF && !PL_utf8_digit)
1863 is_utf8_digit((U8*)"1"); /* preload table */
1867 { /* a lovely hack--pretend we saw [\pX] instead */
1868 char* oldregxend = PL_regxend;
1870 if (PL_regcomp_parse[1] == '{') {
1871 PL_regxend = strchr(PL_regcomp_parse, '}');
1873 FAIL("Missing right brace on \\p{}");
1877 PL_regxend = PL_regcomp_parse + 2;
1880 ret = regclassutf8();
1882 PL_regxend = oldregxend;
1885 *flagp |= HASWIDTH|SIMPLE;
1898 case '1': case '2': case '3': case '4':
1899 case '5': case '6': case '7': case '8': case '9':
1901 I32 num = atoi(PL_regcomp_parse);
1903 if (num > 9 && num >= PL_regnpar)
1906 if (!SIZE_ONLY && num > PL_regcomp_rx->nparens)
1907 FAIL("reference to nonexistent group");
1910 ? (LOC ? REFFL : REFF)
1913 while (isDIGIT(*PL_regcomp_parse))
1921 if (PL_regcomp_parse >= PL_regxend)
1922 FAIL("trailing \\ in regexp");
1925 /* Do not generate `unrecognized' warnings here, we fall
1926 back into the quick-grab loop below */
1932 if (PL_regflags & PMf_EXTENDED) {
1933 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != '\n') PL_regcomp_parse++;
1934 if (PL_regcomp_parse < PL_regxend)
1950 ? (LOC ? EXACTFL : EXACTF)
1952 s = (char *) OPERAND(ret);
1953 regc(0, s++); /* save spot for len */
1954 for (len = 0, p = PL_regcomp_parse - 1;
1955 len < 127 && p < PL_regxend;
1960 if (PL_regflags & PMf_EXTENDED)
1961 p = regwhite(p, PL_regxend);
2015 char* e = strchr(p, '}');
2018 FAIL("Missing right brace on \\x{}");
2020 ender = scan_hex(p + 1, e - p, &numlen);
2021 if (numlen + len >= 127) { /* numlen is generous */
2028 FAIL("Can't use \\x{} without 'use utf8' declaration");
2031 ender = scan_hex(p, 2, &numlen);
2037 ender = UCHARAT(p++);
2038 ender = toCTRL(ender);
2040 case '0': case '1': case '2': case '3':case '4':
2041 case '5': case '6': case '7': case '8':case '9':
2043 (isDIGIT(p[1]) && atoi(p) >= PL_regnpar) ) {
2044 ender = scan_oct(p, 3, &numlen);
2053 if (p >= PL_regxend)
2054 FAIL("trailing \\ in regexp");
2057 if (!SIZE_ONLY && ckWARN(WARN_UNSAFE) && isALPHA(*p))
2058 Perl_warner(aTHX_ WARN_UNSAFE,
2059 "/%.127s/: Unrecognized escape \\%c passed through",
2062 goto normal_default;
2067 if ((*p & 0xc0) == 0xc0 && UTF) {
2068 ender = utf8_to_uv((U8*)p, &numlen);
2075 if (PL_regflags & PMf_EXTENDED)
2076 p = regwhite(p, PL_regxend);
2079 ender = toLOWER_LC_uni(ender);
2081 ender = toLOWER_uni(ender);
2083 if (ISMULT2(p)) { /* Back off on ?+*. */
2086 else if (ender >= 0x80 && UTF) {
2087 reguni(ender, s, &numlen);
2097 if (ender >= 0x80 && UTF) {
2098 reguni(ender, s, &numlen);
2106 PL_regcomp_parse = p - 1;
2109 FAIL("internal disaster in regexp");
2115 *OPERAND(ret) = len;
2118 PL_regsize += (len + 2 + sizeof(regnode) - 1) / sizeof(regnode);
2121 PL_regcode += (len + 2 + sizeof(regnode) - 1) / sizeof(regnode);
2131 S_regwhite(pTHX_ char *p, char *e)
2136 else if (*p == '#') {
2139 } while (p < e && *p != '\n');
2147 /* Parse POSIX character classes: [[:foo:]], [[=foo=]], [[.foo.]].
2148 Character classes ([:foo:]) can also be negated ([:^foo:]).
2149 Returns a named class id (ANYOF_XXX) if successful, -1 otherwise.
2150 Equivalence classes ([=foo=]) and composites ([.foo.]) are parsed,
2151 but trigger warnings because they are currently unimplemented. */
2153 S_regpposixcc(pTHX_ I32 value)
2157 I32 namedclass = -1;
2159 if (value == '[' && PL_regcomp_parse + 1 < PL_regxend &&
2160 /* I smell either [: or [= or [. -- POSIX has been here, right? */
2161 (*PL_regcomp_parse == ':' ||
2162 *PL_regcomp_parse == '=' ||
2163 *PL_regcomp_parse == '.')) {
2164 char c = *PL_regcomp_parse;
2165 char* s = PL_regcomp_parse++;
2167 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != c)
2169 if (PL_regcomp_parse == PL_regxend)
2170 /* Grandfather lone [:, [=, [. */
2171 PL_regcomp_parse = s;
2173 char* t = PL_regcomp_parse++; /* skip over the c */
2175 if (*PL_regcomp_parse == ']') {
2176 PL_regcomp_parse++; /* skip over the ending ] */
2179 I32 complement = *posixcc == '^' ? *posixcc++ : 0;
2180 I32 skip = 5; /* the most common skip */
2184 if (strnEQ(posixcc, "alnum", 5))
2186 complement ? ANYOF_NALNUMC : ANYOF_ALNUMC;
2187 else if (strnEQ(posixcc, "alpha", 5))
2189 complement ? ANYOF_NALPHA : ANYOF_ALPHA;
2190 else if (strnEQ(posixcc, "ascii", 5))
2192 complement ? ANYOF_NASCII : ANYOF_ASCII;
2195 if (strnEQ(posixcc, "cntrl", 5))
2197 complement ? ANYOF_NCNTRL : ANYOF_CNTRL;
2200 if (strnEQ(posixcc, "digit", 5))
2202 complement ? ANYOF_NDIGIT : ANYOF_DIGIT;
2205 if (strnEQ(posixcc, "graph", 5))
2207 complement ? ANYOF_NGRAPH : ANYOF_GRAPH;
2210 if (strnEQ(posixcc, "lower", 5))
2212 complement ? ANYOF_NLOWER : ANYOF_LOWER;
2215 if (strnEQ(posixcc, "print", 5))
2217 complement ? ANYOF_NPRINT : ANYOF_PRINT;
2218 else if (strnEQ(posixcc, "punct", 5))
2220 complement ? ANYOF_NPUNCT : ANYOF_PUNCT;
2223 if (strnEQ(posixcc, "space", 5))
2225 complement ? ANYOF_NSPACE : ANYOF_SPACE;
2227 if (strnEQ(posixcc, "upper", 5))
2229 complement ? ANYOF_NUPPER : ANYOF_UPPER;
2231 case 'w': /* this is not POSIX, this is the Perl \w */
2232 if (strnEQ(posixcc, "word", 4)) {
2234 complement ? ANYOF_NALNUM : ANYOF_ALNUM;
2239 if (strnEQ(posixcc, "xdigit", 6)) {
2241 complement ? ANYOF_NXDIGIT : ANYOF_XDIGIT;
2246 if ((namedclass == -1 ||
2247 !(posixcc + skip + 2 < PL_regxend &&
2248 (posixcc[skip] == ':' &&
2249 posixcc[skip + 1] == ']'))))
2250 Perl_croak(aTHX_ "Character class [:%.*s:] unknown",
2252 } else if (ckWARN(WARN_UNSAFE) && !SIZE_ONLY)
2253 /* [[=foo=]] and [[.foo.]] are still future. */
2254 Perl_warner(aTHX_ WARN_UNSAFE,
2255 "Character class syntax [%c %c] is reserved for future extensions", c, c);
2257 /* Maternal grandfather:
2258 * "[:" ending in ":" but not in ":]" */
2259 PL_regcomp_parse = s;
2268 S_checkposixcc(pTHX)
2270 if (ckWARN(WARN_UNSAFE) && !SIZE_ONLY &&
2271 (*PL_regcomp_parse == ':' ||
2272 *PL_regcomp_parse == '=' ||
2273 *PL_regcomp_parse == '.')) {
2274 char *s = PL_regcomp_parse;
2277 while(*s && isALNUM(*s))
2279 if (*s && c == *s && s[1] == ']') {
2280 Perl_warner(aTHX_ WARN_UNSAFE,
2281 "Character class syntax [%c %c] belongs inside character classes", c, c);
2282 if (c == '=' || c == '.')
2283 Perl_warner(aTHX_ WARN_UNSAFE,
2284 "Character class syntax [%c %c] is reserved for future extensions", c, c);
2293 register char *opnd, *s;
2295 register I32 lastvalue = OOB_CHAR8;
2296 register I32 range = 0;
2297 register regnode *ret;
2302 s = opnd = (char *) OPERAND(PL_regcode);
2303 ret = reg_node(ANYOF);
2304 for (value = 0; value < ANYOF_SIZE; value++)
2306 if (*PL_regcomp_parse == '^') { /* Complement of range. */
2310 ANYOF_FLAGS(opnd) |= ANYOF_INVERT;
2313 PL_regcode += ANY_SKIP;
2315 ANYOF_FLAGS(opnd) |= ANYOF_FOLD;
2317 ANYOF_FLAGS(opnd) |= ANYOF_LOCALE;
2320 PL_regsize += ANY_SKIP;
2325 if (*PL_regcomp_parse == ']' || *PL_regcomp_parse == '-')
2326 goto skipcond; /* allow 1st char to be ] or - */
2327 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != ']') {
2330 value = UCHARAT(PL_regcomp_parse++);
2332 namedclass = regpposixcc(value);
2333 else if (value == '\\') {
2334 value = UCHARAT(PL_regcomp_parse++);
2336 case 'w': namedclass = ANYOF_ALNUM; break;
2337 case 'W': namedclass = ANYOF_NALNUM; break;
2338 case 's': namedclass = ANYOF_SPACE; break;
2339 case 'S': namedclass = ANYOF_NSPACE; break;
2340 case 'd': namedclass = ANYOF_DIGIT; break;
2341 case 'D': namedclass = ANYOF_NDIGIT; break;
2342 case 'n': value = '\n'; break;
2343 case 'r': value = '\r'; break;
2344 case 't': value = '\t'; break;
2345 case 'f': value = '\f'; break;
2346 case 'b': value = '\b'; break;
2347 case 'e': value = '\033'; break;
2348 case 'a': value = '\007'; break;
2350 value = scan_hex(PL_regcomp_parse, 2, &numlen);
2351 PL_regcomp_parse += numlen;
2354 value = UCHARAT(PL_regcomp_parse++);
2355 value = toCTRL(value);
2357 case '0': case '1': case '2': case '3': case '4':
2358 case '5': case '6': case '7': case '8': case '9':
2359 value = scan_oct(--PL_regcomp_parse, 3, &numlen);
2360 PL_regcomp_parse += numlen;
2364 if (!SIZE_ONLY && namedclass > -1) {
2365 switch (namedclass) {
2368 ANYOF_CLASS_SET(opnd, ANYOF_ALNUM);
2370 for (value = 0; value < 256; value++)
2372 ANYOF_BITMAP_SET(opnd, value);
2377 ANYOF_CLASS_SET(opnd, ANYOF_NALNUM);
2379 for (value = 0; value < 256; value++)
2380 if (!isALNUM(value))
2381 ANYOF_BITMAP_SET(opnd, value);
2386 ANYOF_CLASS_SET(opnd, ANYOF_SPACE);
2388 for (value = 0; value < 256; value++)
2390 ANYOF_BITMAP_SET(opnd, value);
2395 ANYOF_CLASS_SET(opnd, ANYOF_NSPACE);
2397 for (value = 0; value < 256; value++)
2398 if (!isSPACE(value))
2399 ANYOF_BITMAP_SET(opnd, value);
2404 ANYOF_CLASS_SET(opnd, ANYOF_DIGIT);
2406 for (value = '0'; value <= '9'; value++)
2407 ANYOF_BITMAP_SET(opnd, value);
2412 ANYOF_CLASS_SET(opnd, ANYOF_NDIGIT);
2414 for (value = 0; value < '0'; value++)
2415 ANYOF_BITMAP_SET(opnd, value);
2416 for (value = '9' + 1; value < 256; value++)
2417 ANYOF_BITMAP_SET(opnd, value);
2422 ANYOF_CLASS_SET(opnd, ANYOF_NALNUMC);
2424 for (value = 0; value < 256; value++)
2425 if (!isALNUMC(value))
2426 ANYOF_BITMAP_SET(opnd, value);
2431 ANYOF_CLASS_SET(opnd, ANYOF_ALNUMC);
2433 for (value = 0; value < 256; value++)
2434 if (isALNUMC(value))
2435 ANYOF_BITMAP_SET(opnd, value);
2440 ANYOF_CLASS_SET(opnd, ANYOF_ALPHA);
2442 for (value = 0; value < 256; value++)
2444 ANYOF_BITMAP_SET(opnd, value);
2449 ANYOF_CLASS_SET(opnd, ANYOF_NALPHA);
2451 for (value = 0; value < 256; value++)
2452 if (!isALPHA(value))
2453 ANYOF_BITMAP_SET(opnd, value);
2458 ANYOF_CLASS_SET(opnd, ANYOF_ASCII);
2460 for (value = 0; value < 128; value++)
2461 ANYOF_BITMAP_SET(opnd, value);
2466 ANYOF_CLASS_SET(opnd, ANYOF_NASCII);
2468 for (value = 128; value < 256; value++)
2469 ANYOF_BITMAP_SET(opnd, value);
2474 ANYOF_CLASS_SET(opnd, ANYOF_CNTRL);
2476 for (value = 0; value < 256; value++)
2478 ANYOF_BITMAP_SET(opnd, value);
2480 lastvalue = OOB_CHAR8;
2484 ANYOF_CLASS_SET(opnd, ANYOF_NCNTRL);
2486 for (value = 0; value < 256; value++)
2487 if (!isCNTRL(value))
2488 ANYOF_BITMAP_SET(opnd, value);
2493 ANYOF_CLASS_SET(opnd, ANYOF_GRAPH);
2495 for (value = 0; value < 256; value++)
2497 ANYOF_BITMAP_SET(opnd, value);
2502 ANYOF_CLASS_SET(opnd, ANYOF_NGRAPH);
2504 for (value = 0; value < 256; value++)
2505 if (!isGRAPH(value))
2506 ANYOF_BITMAP_SET(opnd, value);
2511 ANYOF_CLASS_SET(opnd, ANYOF_LOWER);
2513 for (value = 0; value < 256; value++)
2515 ANYOF_BITMAP_SET(opnd, value);
2520 ANYOF_CLASS_SET(opnd, ANYOF_NLOWER);
2522 for (value = 0; value < 256; value++)
2523 if (!isLOWER(value))
2524 ANYOF_BITMAP_SET(opnd, value);
2529 ANYOF_CLASS_SET(opnd, ANYOF_PRINT);
2531 for (value = 0; value < 256; value++)
2533 ANYOF_BITMAP_SET(opnd, value);
2538 ANYOF_CLASS_SET(opnd, ANYOF_NPRINT);
2540 for (value = 0; value < 256; value++)
2541 if (!isPRINT(value))
2542 ANYOF_BITMAP_SET(opnd, value);
2547 ANYOF_CLASS_SET(opnd, ANYOF_PUNCT);
2549 for (value = 0; value < 256; value++)
2551 ANYOF_BITMAP_SET(opnd, value);
2556 ANYOF_CLASS_SET(opnd, ANYOF_NPUNCT);
2558 for (value = 0; value < 256; value++)
2559 if (!isPUNCT(value))
2560 ANYOF_BITMAP_SET(opnd, value);
2565 ANYOF_CLASS_SET(opnd, ANYOF_UPPER);
2567 for (value = 0; value < 256; value++)
2569 ANYOF_BITMAP_SET(opnd, value);
2574 ANYOF_CLASS_SET(opnd, ANYOF_NUPPER);
2576 for (value = 0; value < 256; value++)
2577 if (!isUPPER(value))
2578 ANYOF_BITMAP_SET(opnd, value);
2583 ANYOF_CLASS_SET(opnd, ANYOF_XDIGIT);
2585 for (value = 0; value < 256; value++)
2586 if (isXDIGIT(value))
2587 ANYOF_BITMAP_SET(opnd, value);
2592 ANYOF_CLASS_SET(opnd, ANYOF_NXDIGIT);
2594 for (value = 0; value < 256; value++)
2595 if (!isXDIGIT(value))
2596 ANYOF_BITMAP_SET(opnd, value);
2600 FAIL("invalid [::] class in regexp");
2604 ANYOF_FLAGS(opnd) |= ANYOF_CLASS;
2605 lastvalue = OOB_CHAR8;
2609 if (lastvalue > value)
2610 FAIL("invalid [] range in regexp");
2615 if (*PL_regcomp_parse == '-' && PL_regcomp_parse+1 < PL_regxend &&
2616 PL_regcomp_parse[1] != ']') {
2619 continue; /* do it next time */
2624 if ((isLOWER(lastvalue) && isLOWER(value)) ||
2625 (isUPPER(lastvalue) && isUPPER(value)))
2628 if (isLOWER(lastvalue)) {
2629 for (i = lastvalue; i <= value; i++)
2631 ANYOF_BITMAP_SET(opnd, i);
2633 for (i = lastvalue; i <= value; i++)
2635 ANYOF_BITMAP_SET(opnd, i);
2640 for ( ; lastvalue <= value; lastvalue++)
2641 ANYOF_BITMAP_SET(opnd, lastvalue);
2645 /* optimize case-insensitive simple patterns (e.g. /[a-z]/i) */
2647 (ANYOF_FLAGS(opnd) & (ANYOF_FLAGS_ALL ^ ANYOF_INVERT)) == ANYOF_FOLD) {
2648 for (value = 0; value < 256; ++value) {
2649 if (ANYOF_BITMAP_TEST(opnd, value)) {
2650 I32 cf = PL_fold[value];
2651 ANYOF_BITMAP_SET(opnd, cf);
2654 ANYOF_FLAGS(opnd) &= ~ANYOF_FOLD;
2656 /* optimize inverted simple patterns (e.g. [^a-z]) */
2657 if (!SIZE_ONLY && (ANYOF_FLAGS(opnd) & ANYOF_FLAGS_ALL) == ANYOF_INVERT) {
2658 for (value = 0; value < ANYOF_BITMAP_SIZE; ++value)
2659 opnd[ANYOF_BITMAP_OFFSET + value] ^= ANYOF_FLAGS_ALL;
2660 ANYOF_FLAGS(opnd) = 0;
2666 S_regclassutf8(pTHX)
2669 register char *opnd, *e;
2671 register U32 lastvalue = OOB_UTF8;
2672 register I32 range = 0;
2673 register regnode *ret;
2680 if (*PL_regcomp_parse == '^') { /* Complement of range. */
2684 flags |= ANYOF_INVERT;
2688 flags |= ANYOF_FOLD;
2690 flags |= ANYOF_LOCALE;
2691 listsv = newSVpvn("# comment\n",10);
2696 if (*PL_regcomp_parse == ']' || *PL_regcomp_parse == '-')
2697 goto skipcond; /* allow 1st char to be ] or - */
2699 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != ']') {
2702 value = utf8_to_uv((U8*)PL_regcomp_parse, &numlen);
2703 PL_regcomp_parse += numlen;
2706 namedclass = regpposixcc(value);
2707 else if (value == '\\') {
2708 value = utf8_to_uv((U8*)PL_regcomp_parse, &numlen);
2709 PL_regcomp_parse += numlen;
2711 case 'w': namedclass = ANYOF_ALNUM; break;
2712 case 'W': namedclass = ANYOF_NALNUM; break;
2713 case 's': namedclass = ANYOF_SPACE; break;
2714 case 'S': namedclass = ANYOF_NSPACE; break;
2715 case 'd': namedclass = ANYOF_DIGIT; break;
2716 case 'D': namedclass = ANYOF_NDIGIT; break;
2719 if (*PL_regcomp_parse == '{') {
2720 e = strchr(PL_regcomp_parse++, '}');
2722 FAIL("Missing right brace on \\p{}");
2723 n = e - PL_regcomp_parse;
2726 e = PL_regcomp_parse;
2731 Perl_sv_catpvf(aTHX_ listsv,
2732 "+utf8::%.*s\n", n, PL_regcomp_parse);
2734 Perl_sv_catpvf(aTHX_ listsv,
2735 "!utf8::%.*s\n", n, PL_regcomp_parse);
2737 PL_regcomp_parse = e + 1;
2738 lastvalue = OOB_UTF8;
2740 case 'n': value = '\n'; break;
2741 case 'r': value = '\r'; break;
2742 case 't': value = '\t'; break;
2743 case 'f': value = '\f'; break;
2744 case 'b': value = '\b'; break;
2745 case 'e': value = '\033'; break;
2746 case 'a': value = '\007'; break;
2748 if (*PL_regcomp_parse == '{') {
2749 e = strchr(PL_regcomp_parse++, '}');
2751 FAIL("Missing right brace on \\x{}");
2752 value = scan_hex(PL_regcomp_parse,
2753 e - PL_regcomp_parse,
2755 PL_regcomp_parse = e + 1;
2758 value = scan_hex(PL_regcomp_parse, 2, &numlen);
2759 PL_regcomp_parse += numlen;
2763 value = UCHARAT(PL_regcomp_parse++);
2764 value = toCTRL(value);
2766 case '0': case '1': case '2': case '3': case '4':
2767 case '5': case '6': case '7': case '8': case '9':
2768 value = scan_oct(--PL_regcomp_parse, 3, &numlen);
2769 PL_regcomp_parse += numlen;
2773 if (!SIZE_ONLY && namedclass > -1) {
2774 switch (namedclass) {
2776 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsWord\n"); break;
2778 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsWord\n"); break;
2780 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsAlnum\n"); break;
2782 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsAlnum\n"); break;
2784 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsAlpha\n"); break;
2786 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsAlpha\n"); break;
2788 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsASCII\n"); break;
2790 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsASCII\n"); break;
2792 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsCntrl\n"); break;
2794 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsCntrl\n"); break;
2796 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsGraph\n"); break;
2798 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsGraph\n"); break;
2800 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsDigit\n"); break;
2802 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsDigit\n"); break;
2804 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsLower\n"); break;
2806 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsLower\n"); break;
2808 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsPrint\n"); break;
2810 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsPrint\n"); break;
2812 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsPunct\n"); break;
2814 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsPunct\n"); break;
2816 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsSpace\n"); break;
2818 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsSpace\n"); break;
2820 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsUpper\n"); break;
2822 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsUpper\n"); break;
2824 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsXDigit\n"); break;
2826 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsXDigit\n"); break;
2831 if (lastvalue > value)
2832 FAIL("invalid [] range in regexp");
2834 Perl_sv_catpvf(aTHX_ listsv, "%04x\t%04x\n", lastvalue, value);
2840 if (*PL_regcomp_parse == '-' && PL_regcomp_parse+1 < PL_regxend &&
2841 PL_regcomp_parse[1] != ']') {
2844 continue; /* do it next time */
2847 Perl_sv_catpvf(aTHX_ listsv, "%04x\n", value);
2851 ret = reganode(ANYOFUTF8, 0);
2854 SV *rv = swash_init("utf8", "", listsv, 1, 0);
2855 SvREFCNT_dec(listsv);
2856 n = add_data(1,"s");
2857 PL_regcomp_rx->data->data[n] = (void*)rv;
2858 ARG1_SET(ret, flags);
2869 char* retval = PL_regcomp_parse++;
2872 if (*PL_regcomp_parse == '(' && PL_regcomp_parse[1] == '?' &&
2873 PL_regcomp_parse[2] == '#') {
2874 while (*PL_regcomp_parse && *PL_regcomp_parse != ')')
2879 if (PL_regflags & PMf_EXTENDED) {
2880 if (isSPACE(*PL_regcomp_parse)) {
2884 else if (*PL_regcomp_parse == '#') {
2885 while (*PL_regcomp_parse && *PL_regcomp_parse != '\n')
2896 - reg_node - emit a node
2898 STATIC regnode * /* Location. */
2899 S_reg_node(pTHX_ U8 op)
2902 register regnode *ret;
2903 register regnode *ptr;
2907 SIZE_ALIGN(PL_regsize);
2912 NODE_ALIGN_FILL(ret);
2914 FILL_ADVANCE_NODE(ptr, op);
2921 - reganode - emit a node with an argument
2923 STATIC regnode * /* Location. */
2924 S_reganode(pTHX_ U8 op, U32 arg)
2927 register regnode *ret;
2928 register regnode *ptr;
2932 SIZE_ALIGN(PL_regsize);
2937 NODE_ALIGN_FILL(ret);
2939 FILL_ADVANCE_NODE_ARG(ptr, op, arg);
2946 - regc - emit (if appropriate) a Unicode character
2949 S_reguni(pTHX_ UV uv, char* s, I32* lenp)
2954 *lenp = uv_to_utf8(tmpbuf, uv) - tmpbuf;
2957 *lenp = uv_to_utf8((U8*)s, uv) - (U8*)s;
2962 - regc - emit (if appropriate) a byte of code
2965 S_regc(pTHX_ U8 b, char* s)
2973 - reginsert - insert an operator in front of already-emitted operand
2975 * Means relocating the operand.
2978 S_reginsert(pTHX_ U8 op, regnode *opnd)
2981 register regnode *src;
2982 register regnode *dst;
2983 register regnode *place;
2984 register int offset = regarglen[(U8)op];
2986 /* (PL_regkind[(U8)op] == CURLY ? EXTRA_STEP_2ARGS : 0); */
2989 PL_regsize += NODE_STEP_REGNODE + offset;
2994 PL_regcode += NODE_STEP_REGNODE + offset;
2997 StructCopy(--src, --dst, regnode);
2999 place = opnd; /* Op node, where operand used to be. */
3000 src = NEXTOPER(place);
3001 FILL_ADVANCE_NODE(place, op);
3002 Zero(src, offset, regnode);
3006 - regtail - set the next-pointer at the end of a node chain of p to val.
3009 S_regtail(pTHX_ regnode *p, regnode *val)
3012 register regnode *scan;
3013 register regnode *temp;
3014 register I32 offset;
3019 /* Find last node. */
3022 temp = regnext(scan);
3028 if (reg_off_by_arg[OP(scan)]) {
3029 ARG_SET(scan, val - scan);
3032 NEXT_OFF(scan) = val - scan;
3037 - regoptail - regtail on operand of first argument; nop if operandless
3040 S_regoptail(pTHX_ regnode *p, regnode *val)
3043 /* "Operandless" and "op != BRANCH" are synonymous in practice. */
3044 if (p == NULL || SIZE_ONLY)
3046 if (PL_regkind[(U8)OP(p)] == BRANCH) {
3047 regtail(NEXTOPER(p), val);
3049 else if ( PL_regkind[(U8)OP(p)] == BRANCHJ) {
3050 regtail(NEXTOPER(NEXTOPER(p)), val);
3057 - regcurly - a little FSA that accepts {\d+,?\d*}
3060 S_regcurly(pTHX_ register char *s)
3079 S_dumpuntil(pTHX_ regnode *start, regnode *node, regnode *last, SV* sv, I32 l)
3082 register U8 op = EXACT; /* Arbitrary non-END op. */
3083 register regnode *next, *onode;
3085 while (op != END && (!last || node < last)) {
3086 /* While that wasn't END last time... */
3092 next = regnext(node);
3094 if (OP(node) == OPTIMIZED)
3097 PerlIO_printf(Perl_debug_log, "%4d:%*s%s", node - start,
3098 2*l + 1, "", SvPVX(sv));
3099 if (next == NULL) /* Next ptr. */
3100 PerlIO_printf(Perl_debug_log, "(0)");
3102 PerlIO_printf(Perl_debug_log, "(%d)", next - start);
3103 (void)PerlIO_putc(Perl_debug_log, '\n');
3105 if (PL_regkind[(U8)op] == BRANCHJ) {
3106 register regnode *nnode = (OP(next) == LONGJMP
3109 if (last && nnode > last)
3111 node = dumpuntil(start, NEXTOPER(NEXTOPER(node)), nnode, sv, l + 1);
3113 else if (PL_regkind[(U8)op] == BRANCH) {
3114 node = dumpuntil(start, NEXTOPER(node), next, sv, l + 1);
3116 else if ( op == CURLY) { /* `next' might be very big: optimizer */
3117 node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
3118 NEXTOPER(node) + EXTRA_STEP_2ARGS + 1, sv, l + 1);
3120 else if (PL_regkind[(U8)op] == CURLY && op != CURLYX) {
3121 node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
3124 else if ( op == PLUS || op == STAR) {
3125 node = dumpuntil(start, NEXTOPER(node), NEXTOPER(node) + 1, sv, l + 1);
3127 else if (op == ANYOF) {
3128 node = NEXTOPER(node);
3131 else if (PL_regkind[(U8)op] == EXACT) {
3132 /* Literal string, where present. */
3133 node += ((*OPERAND(node)) + 2 + sizeof(regnode) - 1) / sizeof(regnode);
3134 node = NEXTOPER(node);
3137 node = NEXTOPER(node);
3138 node += regarglen[(U8)op];
3140 if (op == CURLYX || op == OPEN)
3142 else if (op == WHILEM)
3145 #endif /* DEBUGGING */
3150 - regdump - dump a regexp onto Perl_debug_log in vaguely comprehensible form
3153 Perl_regdump(pTHX_ regexp *r)
3157 SV *sv = sv_newmortal();
3159 (void)dumpuntil(r->program, r->program + 1, NULL, sv, 0);
3161 /* Header fields of interest. */
3162 if (r->anchored_substr)
3163 PerlIO_printf(Perl_debug_log, "anchored `%s%.*s%s'%s at %d ",
3165 SvCUR(r->anchored_substr) - (SvTAIL(r->anchored_substr)!=0),
3166 SvPVX(r->anchored_substr),
3168 SvTAIL(r->anchored_substr) ? "$" : "",
3169 r->anchored_offset);
3170 if (r->float_substr)
3171 PerlIO_printf(Perl_debug_log, "floating `%s%.*s%s'%s at %d..%u ",
3173 SvCUR(r->float_substr) - (SvTAIL(r->float_substr)!=0),
3174 SvPVX(r->float_substr),
3176 SvTAIL(r->float_substr) ? "$" : "",
3177 r->float_min_offset, r->float_max_offset);
3178 if (r->check_substr)
3179 PerlIO_printf(Perl_debug_log,
3180 r->check_substr == r->float_substr
3181 ? "(checking floating" : "(checking anchored");
3182 if (r->reganch & ROPT_NOSCAN)
3183 PerlIO_printf(Perl_debug_log, " noscan");
3184 if (r->reganch & ROPT_CHECK_ALL)
3185 PerlIO_printf(Perl_debug_log, " isall");
3186 if (r->check_substr)
3187 PerlIO_printf(Perl_debug_log, ") ");
3189 if (r->regstclass) {
3190 regprop(sv, r->regstclass);
3191 PerlIO_printf(Perl_debug_log, "stclass `%s' ", SvPVX(sv));
3193 if (r->reganch & ROPT_ANCH) {
3194 PerlIO_printf(Perl_debug_log, "anchored");
3195 if (r->reganch & ROPT_ANCH_BOL)
3196 PerlIO_printf(Perl_debug_log, "(BOL)");
3197 if (r->reganch & ROPT_ANCH_MBOL)
3198 PerlIO_printf(Perl_debug_log, "(MBOL)");
3199 if (r->reganch & ROPT_ANCH_SBOL)
3200 PerlIO_printf(Perl_debug_log, "(SBOL)");
3201 if (r->reganch & ROPT_ANCH_GPOS)
3202 PerlIO_printf(Perl_debug_log, "(GPOS)");
3203 PerlIO_putc(Perl_debug_log, ' ');
3205 if (r->reganch & ROPT_GPOS_SEEN)
3206 PerlIO_printf(Perl_debug_log, "GPOS ");
3207 if (r->reganch & ROPT_SKIP)
3208 PerlIO_printf(Perl_debug_log, "plus ");
3209 if (r->reganch & ROPT_IMPLICIT)
3210 PerlIO_printf(Perl_debug_log, "implicit ");
3211 PerlIO_printf(Perl_debug_log, "minlen %ld ", (long) r->minlen);
3212 if (r->reganch & ROPT_EVAL_SEEN)
3213 PerlIO_printf(Perl_debug_log, "with eval ");
3214 PerlIO_printf(Perl_debug_log, "\n");
3215 #endif /* DEBUGGING */
3219 - regprop - printable representation of opcode
3222 Perl_regprop(pTHX_ SV *sv, regnode *o)
3228 sv_setpvn(sv, "", 0);
3229 if (OP(o) >= reg_num) /* regnode.type is unsigned */
3230 FAIL("corrupted regexp opcode");
3231 sv_catpv(sv, (char*)reg_name[OP(o)]); /* Take off const! */
3233 k = PL_regkind[(U8)OP(o)];
3236 Perl_sv_catpvf(aTHX_ sv, " <%s%s%s>", PL_colors[0], OPERAND(o) + 1, PL_colors[1]);
3237 else if (k == CURLY) {
3238 if (OP(o) == CURLYM || OP(o) == CURLYN)
3239 Perl_sv_catpvf(aTHX_ sv, "[%d]", o->flags); /* Parenth number */
3240 Perl_sv_catpvf(aTHX_ sv, " {%d,%d}", ARG1(o), ARG2(o));
3242 else if (k == WHILEM && o->flags) /* Ordinal/of */
3243 Perl_sv_catpvf(aTHX_ sv, "[%d/%d]", o->flags & 0xf, o->flags>>4);
3244 else if (k == REF || k == OPEN || k == CLOSE || k == GROUPP )
3245 Perl_sv_catpvf(aTHX_ sv, "%d", ARG(o)); /* Parenth number */
3246 else if (k == LOGICAL)
3247 Perl_sv_catpvf(aTHX_ sv, "[%d]", ARG(o)); /* 2: embedded, otherwise 1 */
3248 else if (k == BRANCHJ && (OP(o) == UNLESSM || OP(o) == IFMATCH))
3249 Perl_sv_catpvf(aTHX_ sv, "[-%d]", o->flags);
3250 #endif /* DEBUGGING */
3254 Perl_re_intuit_string(pTHX_ regexp *prog)
3255 { /* Assume that RE_INTUIT is set */
3258 char *s = SvPV(prog->check_substr,n_a);
3260 if (!PL_colorset) reginitcolors();
3261 PerlIO_printf(Perl_debug_log,
3262 "%sUsing REx substr:%s `%s%.60s%s%s'\n",
3263 PL_colors[4],PL_colors[5],PL_colors[0],
3266 (strlen(s) > 60 ? "..." : ""));
3269 return prog->check_substr;
3273 Perl_pregfree(pTHX_ struct regexp *r)
3276 DEBUG_r(if (!PL_colorset) reginitcolors());
3277 DEBUG_r(PerlIO_printf(Perl_debug_log,
3278 "%sFreeing REx:%s `%s%.60s%s%s'\n",
3279 PL_colors[4],PL_colors[5],PL_colors[0],
3282 (strlen(r->precomp) > 60 ? "..." : "")));
3285 if (!r || (--r->refcnt > 0))
3288 Safefree(r->precomp);
3289 if (RX_MATCH_COPIED(r))
3290 Safefree(r->subbeg);
3292 if (r->anchored_substr)
3293 SvREFCNT_dec(r->anchored_substr);
3294 if (r->float_substr)
3295 SvREFCNT_dec(r->float_substr);
3296 Safefree(r->substrs);
3299 int n = r->data->count;
3300 AV* new_comppad = NULL;
3305 switch (r->data->what[n]) {
3307 SvREFCNT_dec((SV*)r->data->data[n]);
3310 new_comppad = (AV*)r->data->data[n];
3313 if (new_comppad == NULL)
3314 Perl_croak(aTHX_ "panic: pregfree comppad");
3315 old_comppad = PL_comppad;
3316 old_curpad = PL_curpad;
3317 PL_comppad = new_comppad;
3318 PL_curpad = AvARRAY(new_comppad);
3319 op_free((OP_4tree*)r->data->data[n]);
3320 PL_comppad = old_comppad;
3321 PL_curpad = old_curpad;
3322 SvREFCNT_dec((SV*)new_comppad);
3328 FAIL2("panic: regfree data code '%c'", r->data->what[n]);
3331 Safefree(r->data->what);
3334 Safefree(r->startp);
3340 - regnext - dig the "next" pointer out of a node
3342 * [Note, when REGALIGN is defined there are two places in regmatch()
3343 * that bypass this code for speed.]
3346 Perl_regnext(pTHX_ register regnode *p)
3349 register I32 offset;
3351 if (p == &PL_regdummy)
3354 offset = (reg_off_by_arg[OP(p)] ? ARG(p) : NEXT_OFF(p));
3362 S_re_croak2(pTHX_ const char* pat1,const char* pat2,...)
3365 STRLEN l1 = strlen(pat1);
3366 STRLEN l2 = strlen(pat2);
3375 Copy(pat1, buf, l1 , char);
3376 Copy(pat2, buf + l1, l2 , char);
3377 buf[l1 + l2] = '\n';
3378 buf[l1 + l2 + 1] = '\0';
3380 /* ANSI variant takes additional second argument */
3381 va_start(args, pat2);
3385 msv = mess(buf, &args);
3387 message = SvPV(msv,l1);
3390 Copy(message, buf, l1 , char);
3391 buf[l1] = '\0'; /* Overwrite \n */
3392 Perl_croak(aTHX_ "%s", buf);
3395 /* XXX Here's a total kludge. But we need to re-enter for swash routines. */
3398 Perl_save_re_context(pTHX)
3402 SAVEPPTR(PL_regprecomp); /* uncompiled string. */
3403 SAVEI32(PL_regnpar); /* () count. */
3404 SAVEI32(PL_regsize); /* Code size. */
3405 SAVEI16(PL_regflags); /* are we folding, multilining? */
3406 SAVEPPTR(PL_reginput); /* String-input pointer. */
3407 SAVEPPTR(PL_regbol); /* Beginning of input, for ^ check. */
3408 SAVEPPTR(PL_regeol); /* End of input, for $ check. */
3409 SAVESPTR(PL_regstartp); /* Pointer to startp array. */
3410 SAVESPTR(PL_regendp); /* Ditto for endp. */
3411 SAVESPTR(PL_reglastparen); /* Similarly for lastparen. */
3412 SAVEPPTR(PL_regtill); /* How far we are required to go. */
3413 SAVEI32(PL_regprev); /* char before regbol, \n if none */
3414 SAVESPTR(PL_reg_start_tmp); /* from regexec.c */
3415 PL_reg_start_tmp = 0;
3416 SAVEFREEPV(PL_reg_start_tmp);
3417 SAVEI32(PL_reg_start_tmpl); /* from regexec.c */
3418 PL_reg_start_tmpl = 0;
3419 SAVESPTR(PL_regdata);
3420 SAVEI32(PL_reg_flags); /* from regexec.c */
3421 SAVEI32(PL_reg_eval_set); /* from regexec.c */
3422 SAVEI32(PL_regnarrate); /* from regexec.c */
3423 SAVESPTR(PL_regprogram); /* from regexec.c */
3424 SAVEINT(PL_regindent); /* from regexec.c */
3425 SAVESPTR(PL_regcc); /* from regexec.c */
3426 SAVESPTR(PL_curcop);
3427 SAVESPTR(PL_regcomp_rx); /* from regcomp.c */
3428 SAVEI32(PL_regseen); /* from regcomp.c */
3429 SAVEI32(PL_regsawback); /* Did we see \1, ...? */
3430 SAVEI32(PL_regnaughty); /* How bad is this pattern? */
3431 SAVESPTR(PL_regcode); /* Code-emit pointer; ®dummy = don't */
3432 SAVEPPTR(PL_regxend); /* End of input for compile */
3433 SAVEPPTR(PL_regcomp_parse); /* Input-scan pointer. */
3434 SAVESPTR(PL_reg_call_cc); /* from regexec.c */
3435 SAVESPTR(PL_reg_re); /* from regexec.c */
3436 SAVEPPTR(PL_reg_ganch); /* from regexec.c */
3437 SAVESPTR(PL_reg_sv); /* from regexec.c */
3438 SAVESPTR(PL_reg_magic); /* from regexec.c */
3439 SAVEI32(PL_reg_oldpos); /* from regexec.c */
3440 SAVESPTR(PL_reg_oldcurpm); /* from regexec.c */
3441 SAVESPTR(PL_reg_curpm); /* from regexec.c */
3443 SAVEPPTR(PL_reg_starttry); /* from regexec.c */
3455 clear_re(pTHXo_ void *r)
3457 ReREFCNT_dec((regexp *)r);