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
195 #define OOB_NAMEDCLASS -1
197 #define CHR_SVLEN(sv) (UTF ? sv_len_utf8(sv) : SvCUR(sv))
198 #define CHR_DIST(a,b) (UTF ? utf8_distance(a,b) : a - b)
200 /* Allow for side effects in s */
201 #define REGC(c,s) STMT_START { if (!SIZE_ONLY) *(s) = (c); else (s);} STMT_END
203 static void clear_re(pTHXo_ void *r);
206 S_scan_commit(pTHX_ scan_data_t *data)
209 STRLEN l = CHR_SVLEN(data->last_found);
210 STRLEN old_l = CHR_SVLEN(*data->longest);
212 if ((l >= old_l) && ((l > old_l) || (data->flags & SF_BEFORE_EOL))) {
213 sv_setsv(*data->longest, data->last_found);
214 if (*data->longest == data->longest_fixed) {
215 data->offset_fixed = l ? data->last_start_min : data->pos_min;
216 if (data->flags & SF_BEFORE_EOL)
218 |= ((data->flags & SF_BEFORE_EOL) << SF_FIX_SHIFT_EOL);
220 data->flags &= ~SF_FIX_BEFORE_EOL;
223 data->offset_float_min = l ? data->last_start_min : data->pos_min;
224 data->offset_float_max = (l
225 ? data->last_start_max
226 : data->pos_min + data->pos_delta);
227 if (data->flags & SF_BEFORE_EOL)
229 |= ((data->flags & SF_BEFORE_EOL) << SF_FL_SHIFT_EOL);
231 data->flags &= ~SF_FL_BEFORE_EOL;
234 SvCUR_set(data->last_found, 0);
236 data->flags &= ~SF_BEFORE_EOL;
239 /* Stops at toplevel WHILEM as well as at `last'. At end *scanp is set
240 to the position after last scanned or to NULL. */
243 S_study_chunk(pTHX_ regnode **scanp, I32 *deltap, regnode *last, scan_data_t *data, U32 flags)
244 /* scanp: Start here (read-write). */
245 /* deltap: Write maxlen-minlen here. */
246 /* last: Stop before this one. */
249 I32 min = 0, pars = 0, code;
250 regnode *scan = *scanp, *next;
252 int is_inf = (flags & SCF_DO_SUBSTR) && (data->flags & SF_IS_INF);
253 int is_inf_internal = 0; /* The studied chunk is infinite */
254 I32 is_par = OP(scan) == OPEN ? ARG(scan) : 0;
255 scan_data_t data_fake;
257 while (scan && OP(scan) != END && scan < last) {
258 /* Peephole optimizer: */
260 if (PL_regkind[(U8)OP(scan)] == EXACT) {
261 regnode *n = regnext(scan);
264 regnode *stop = scan;
267 next = scan + NODE_SZ_STR(scan);
268 /* Skip NOTHING, merge EXACT*. */
270 ( PL_regkind[(U8)OP(n)] == NOTHING ||
271 (stringok && (OP(n) == OP(scan))))
273 && NEXT_OFF(scan) + NEXT_OFF(n) < I16_MAX) {
274 if (OP(n) == TAIL || n > next)
276 if (PL_regkind[(U8)OP(n)] == NOTHING) {
277 NEXT_OFF(scan) += NEXT_OFF(n);
278 next = n + NODE_STEP_REGNODE;
286 int oldl = STR_LEN(scan);
287 regnode *nnext = regnext(n);
289 if (oldl + STR_LEN(n) > U8_MAX)
291 NEXT_OFF(scan) += NEXT_OFF(n);
292 STR_LEN(scan) += STR_LEN(n);
293 next = n + NODE_SZ_STR(n);
294 /* Now we can overwrite *n : */
295 Move(STRING(n), STRING(scan) + oldl,
306 n = scan + NODE_SZ_STR(scan);
308 /* Purify reports a benign UMR here sometimes, because we
309 * don't initialize the OP() slot of a node when that node
310 * is occupied by just the trailing null of the string in
312 if (PL_regkind[(U8)OP(n)] != NOTHING || OP(n) == NOTHING) {
321 if (OP(scan) != CURLYX) {
322 int max = (reg_off_by_arg[OP(scan)]
324 /* I32 may be smaller than U16 on CRAYs! */
325 : (I32_MAX < U16_MAX ? I32_MAX : U16_MAX));
326 int off = (reg_off_by_arg[OP(scan)] ? ARG(scan) : NEXT_OFF(scan));
330 /* Skip NOTHING and LONGJMP. */
331 while ((n = regnext(n))
332 && ((PL_regkind[(U8)OP(n)] == NOTHING && (noff = NEXT_OFF(n)))
333 || ((OP(n) == LONGJMP) && (noff = ARG(n))))
336 if (reg_off_by_arg[OP(scan)])
339 NEXT_OFF(scan) = off;
341 if (OP(scan) == BRANCH || OP(scan) == BRANCHJ
342 || OP(scan) == IFTHEN || OP(scan) == SUSPEND) {
343 next = regnext(scan);
346 if (OP(next) == code || code == IFTHEN || code == SUSPEND) {
347 I32 max1 = 0, min1 = I32_MAX, num = 0;
349 if (flags & SCF_DO_SUBSTR)
351 while (OP(scan) == code) {
352 I32 deltanext, minnext;
357 data_fake.whilem_c = data->whilem_c;
358 next = regnext(scan);
359 scan = NEXTOPER(scan);
361 scan = NEXTOPER(scan);
362 /* We suppose the run is continuous, last=next...*/
363 minnext = study_chunk(&scan, &deltanext, next,
367 if (max1 < minnext + deltanext)
368 max1 = minnext + deltanext;
369 if (deltanext == I32_MAX)
370 is_inf = is_inf_internal = 1;
372 if (data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
374 if (data && (data_fake.flags & SF_HAS_EVAL))
375 data->flags |= SF_HAS_EVAL;
377 data->whilem_c = data_fake.whilem_c;
381 if (code == IFTHEN && num < 2) /* Empty ELSE branch */
383 if (flags & SCF_DO_SUBSTR) {
384 data->pos_min += min1;
385 data->pos_delta += max1 - min1;
386 if (max1 != min1 || is_inf)
387 data->longest = &(data->longest_float);
390 delta += max1 - min1;
392 else if (code == BRANCHJ) /* single branch is optimized. */
393 scan = NEXTOPER(NEXTOPER(scan));
394 else /* single branch is optimized. */
395 scan = NEXTOPER(scan);
398 else if (OP(scan) == EXACT) {
399 I32 l = STR_LEN(scan);
401 unsigned char *s = (unsigned char *)STRING(scan);
402 unsigned char *e = s + l;
411 if (flags & SCF_DO_SUBSTR) { /* Update longest substr. */
412 /* The code below prefers earlier match for fixed
413 offset, later match for variable offset. */
414 if (data->last_end == -1) { /* Update the start info. */
415 data->last_start_min = data->pos_min;
416 data->last_start_max = is_inf
417 ? I32_MAX : data->pos_min + data->pos_delta;
419 sv_catpvn(data->last_found, STRING(scan), STR_LEN(scan));
420 data->last_end = data->pos_min + l;
421 data->pos_min += l; /* As in the first entry. */
422 data->flags &= ~SF_BEFORE_EOL;
425 else if (PL_regkind[(U8)OP(scan)] == EXACT) {
426 I32 l = STR_LEN(scan);
427 if (flags & SCF_DO_SUBSTR)
430 unsigned char *s = (unsigned char *)STRING(scan);
431 unsigned char *e = s + l;
440 if (data && (flags & SCF_DO_SUBSTR))
443 else if (strchr((char*)PL_varies,OP(scan))) {
444 I32 mincount, maxcount, minnext, deltanext, pos_before, fl;
445 regnode *oscan = scan;
447 switch (PL_regkind[(U8)OP(scan)]) {
449 scan = NEXTOPER(scan);
452 if (flags & SCF_DO_SUBSTR) {
453 next = NEXTOPER(scan);
454 if (OP(next) == EXACT) {
456 maxcount = REG_INFTY;
457 next = regnext(scan);
458 scan = NEXTOPER(scan);
462 if (flags & SCF_DO_SUBSTR)
467 is_inf = is_inf_internal = 1;
468 scan = regnext(scan);
469 if (flags & SCF_DO_SUBSTR) {
471 data->longest = &(data->longest_float);
473 goto optimize_curly_tail;
475 mincount = ARG1(scan);
476 maxcount = ARG2(scan);
477 next = regnext(scan);
478 scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
480 if (flags & SCF_DO_SUBSTR) {
481 if (mincount == 0) scan_commit(data);
482 pos_before = data->pos_min;
486 data->flags &= ~(SF_HAS_PAR|SF_IN_PAR|SF_HAS_EVAL);
488 data->flags |= SF_IS_INF;
490 /* This will finish on WHILEM, setting scan, or on NULL: */
491 minnext = study_chunk(&scan, &deltanext, last, data,
493 ? (flags & ~SCF_DO_SUBSTR) : flags);
494 if (!scan) /* It was not CURLYX, but CURLY. */
496 if (ckWARN(WARN_UNSAFE) && (minnext + deltanext == 0)
497 && !(data->flags & (SF_HAS_PAR|SF_IN_PAR))
498 && maxcount <= REG_INFTY/3) /* Complement check for big count */
499 Perl_warner(aTHX_ WARN_UNSAFE,
500 "Strange *+?{} on zero-length expression");
501 min += minnext * mincount;
502 is_inf_internal |= (maxcount == REG_INFTY
503 && (minnext + deltanext) > 0
504 || deltanext == I32_MAX);
505 is_inf |= is_inf_internal;
506 delta += (minnext + deltanext) * maxcount - minnext * mincount;
508 /* Try powerful optimization CURLYX => CURLYN. */
509 if ( OP(oscan) == CURLYX && data
510 && data->flags & SF_IN_PAR
511 && !(data->flags & SF_HAS_EVAL)
512 && !deltanext && minnext == 1 ) {
513 /* Try to optimize to CURLYN. */
514 regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS;
515 regnode *nxt1 = nxt, *nxt2;
519 if (!strchr((char*)PL_simple,OP(nxt))
520 && !(PL_regkind[(U8)OP(nxt)] == EXACT
521 && STR_LEN(nxt) == 1))
525 if (OP(nxt) != CLOSE)
527 /* Now we know that nxt2 is the only contents: */
528 oscan->flags = ARG(nxt);
530 OP(nxt1) = NOTHING; /* was OPEN. */
532 OP(nxt1 + 1) = OPTIMIZED; /* was count. */
533 NEXT_OFF(nxt1+ 1) = 0; /* just for consistancy. */
534 NEXT_OFF(nxt2) = 0; /* just for consistancy with CURLY. */
535 OP(nxt) = OPTIMIZED; /* was CLOSE. */
536 OP(nxt + 1) = OPTIMIZED; /* was count. */
537 NEXT_OFF(nxt+ 1) = 0; /* just for consistancy. */
542 /* Try optimization CURLYX => CURLYM. */
543 if ( OP(oscan) == CURLYX && data
544 && !(data->flags & SF_HAS_PAR)
545 && !(data->flags & SF_HAS_EVAL)
547 /* XXXX How to optimize if data == 0? */
548 /* Optimize to a simpler form. */
549 regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN */
553 while ( (nxt2 = regnext(nxt)) /* skip over embedded stuff*/
554 && (OP(nxt2) != WHILEM))
556 OP(nxt2) = SUCCEED; /* Whas WHILEM */
557 /* Need to optimize away parenths. */
558 if (data->flags & SF_IN_PAR) {
559 /* Set the parenth number. */
560 regnode *nxt1 = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN*/
562 if (OP(nxt) != CLOSE)
563 FAIL("panic opt close");
564 oscan->flags = ARG(nxt);
565 OP(nxt1) = OPTIMIZED; /* was OPEN. */
566 OP(nxt) = OPTIMIZED; /* was CLOSE. */
568 OP(nxt1 + 1) = OPTIMIZED; /* was count. */
569 OP(nxt + 1) = OPTIMIZED; /* was count. */
570 NEXT_OFF(nxt1 + 1) = 0; /* just for consistancy. */
571 NEXT_OFF(nxt + 1) = 0; /* just for consistancy. */
574 while ( nxt1 && (OP(nxt1) != WHILEM)) {
575 regnode *nnxt = regnext(nxt1);
578 if (reg_off_by_arg[OP(nxt1)])
579 ARG_SET(nxt1, nxt2 - nxt1);
580 else if (nxt2 - nxt1 < U16_MAX)
581 NEXT_OFF(nxt1) = nxt2 - nxt1;
583 OP(nxt) = NOTHING; /* Cannot beautify */
588 /* Optimize again: */
589 study_chunk(&nxt1, &deltanext, nxt, NULL, 0);
594 else if (OP(oscan) == CURLYX && data && ++data->whilem_c < 16) {
595 /* This stays as CURLYX, and can put the count/of pair. */
596 /* Find WHILEM (as in regexec.c) */
597 regnode *nxt = oscan + NEXT_OFF(oscan);
599 if (OP(PREVOPER(nxt)) == NOTHING) /* LONGJMP */
601 PREVOPER(nxt)->flags = data->whilem_c
602 | (PL_reg_whilem_seen << 4); /* On WHILEM */
604 if (data && fl & (SF_HAS_PAR|SF_IN_PAR))
606 if (flags & SCF_DO_SUBSTR) {
607 SV *last_str = Nullsv;
608 int counted = mincount != 0;
610 if (data->last_end > 0 && mincount != 0) { /* Ends with a string. */
611 I32 b = pos_before >= data->last_start_min
612 ? pos_before : data->last_start_min;
614 char *s = SvPV(data->last_found, l);
615 I32 old = b - data->last_start_min;
618 old = utf8_hop((U8*)s, old) - (U8*)s;
621 /* Get the added string: */
622 last_str = newSVpvn(s + old, l);
623 if (deltanext == 0 && pos_before == b) {
624 /* What was added is a constant string */
626 SvGROW(last_str, (mincount * l) + 1);
627 repeatcpy(SvPVX(last_str) + l,
628 SvPVX(last_str), l, mincount - 1);
629 SvCUR(last_str) *= mincount;
630 /* Add additional parts. */
631 SvCUR_set(data->last_found,
632 SvCUR(data->last_found) - l);
633 sv_catsv(data->last_found, last_str);
634 data->last_end += l * (mincount - 1);
638 /* It is counted once already... */
639 data->pos_min += minnext * (mincount - counted);
640 data->pos_delta += - counted * deltanext +
641 (minnext + deltanext) * maxcount - minnext * mincount;
642 if (mincount != maxcount) {
644 if (mincount && last_str) {
645 sv_setsv(data->last_found, last_str);
646 data->last_end = data->pos_min;
647 data->last_start_min =
648 data->pos_min - CHR_SVLEN(last_str);
649 data->last_start_max = is_inf
651 : data->pos_min + data->pos_delta
652 - CHR_SVLEN(last_str);
654 data->longest = &(data->longest_float);
656 SvREFCNT_dec(last_str);
658 if (data && (fl & SF_HAS_EVAL))
659 data->flags |= SF_HAS_EVAL;
661 if (OP(oscan) != CURLYX) {
662 while (PL_regkind[(U8)OP(next = regnext(oscan))] == NOTHING
664 NEXT_OFF(oscan) += NEXT_OFF(next);
667 default: /* REF only? */
668 if (flags & SCF_DO_SUBSTR) {
670 data->longest = &(data->longest_float);
672 is_inf = is_inf_internal = 1;
676 else if (strchr((char*)PL_simple,OP(scan)) || PL_regkind[(U8)OP(scan)] == ANYUTF8) {
677 if (flags & SCF_DO_SUBSTR) {
683 else if (PL_regkind[(U8)OP(scan)] == EOL && flags & SCF_DO_SUBSTR) {
684 data->flags |= (OP(scan) == MEOL
688 else if (PL_regkind[(U8)OP(scan)] == BRANCHJ
689 && (scan->flags || data)
690 && (OP(scan) == IFMATCH || OP(scan) == UNLESSM)) {
691 I32 deltanext, minnext;
696 data_fake.whilem_c = data->whilem_c;
697 next = regnext(scan);
698 nscan = NEXTOPER(NEXTOPER(scan));
699 minnext = study_chunk(&nscan, &deltanext, last, &data_fake, 0);
702 FAIL("variable length lookbehind not implemented");
704 else if (minnext > U8_MAX) {
706 FAIL2("lookbehind longer than %" PERL_PRIu64 " not implemented", (UV)U8_MAX);
708 FAIL2("lookbehind longer than %d not implemented", U8_MAX);
711 scan->flags = minnext;
713 if (data && data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
715 if (data && (data_fake.flags & SF_HAS_EVAL))
716 data->flags |= SF_HAS_EVAL;
718 data->whilem_c = data_fake.whilem_c;
720 else if (OP(scan) == OPEN) {
723 else if (OP(scan) == CLOSE && ARG(scan) == is_par) {
724 next = regnext(scan);
726 if ( next && (OP(next) != WHILEM) && next < last)
727 is_par = 0; /* Disable optimization */
729 else if (OP(scan) == EVAL) {
731 data->flags |= SF_HAS_EVAL;
733 else if (OP(scan) == LOGICAL && scan->flags == 2) { /* Embedded */
734 if (flags & SCF_DO_SUBSTR) {
736 data->longest = &(data->longest_float);
738 is_inf = is_inf_internal = 1;
740 /* Else: zero-length, ignore. */
741 scan = regnext(scan);
746 *deltap = is_inf_internal ? I32_MAX : delta;
747 if (flags & SCF_DO_SUBSTR && is_inf)
748 data->pos_delta = I32_MAX - data->pos_min;
751 if (is_par && pars==1 && data) {
752 data->flags |= SF_IN_PAR;
753 data->flags &= ~SF_HAS_PAR;
755 else if (pars && data) {
756 data->flags |= SF_HAS_PAR;
757 data->flags &= ~SF_IN_PAR;
763 S_add_data(pTHX_ I32 n, char *s)
766 if (PL_regcomp_rx->data) {
767 Renewc(PL_regcomp_rx->data,
768 sizeof(*PL_regcomp_rx->data) + sizeof(void*) * (PL_regcomp_rx->data->count + n - 1),
769 char, struct reg_data);
770 Renew(PL_regcomp_rx->data->what, PL_regcomp_rx->data->count + n, U8);
771 PL_regcomp_rx->data->count += n;
774 Newc(1207, PL_regcomp_rx->data, sizeof(*PL_regcomp_rx->data) + sizeof(void*) * (n - 1),
775 char, struct reg_data);
776 New(1208, PL_regcomp_rx->data->what, n, U8);
777 PL_regcomp_rx->data->count = n;
779 Copy(s, PL_regcomp_rx->data->what + PL_regcomp_rx->data->count - n, n, U8);
780 return PL_regcomp_rx->data->count - n;
784 Perl_reginitcolors(pTHX)
788 char *s = PerlEnv_getenv("PERL_RE_COLORS");
791 PL_colors[0] = s = savepv(s);
799 PL_colors[i] = s = "";
809 - pregcomp - compile a regular expression into internal code
811 * We can't allocate space until we know how big the compiled form will be,
812 * but we can't compile it (and thus know how big it is) until we've got a
813 * place to put the code. So we cheat: we compile it twice, once with code
814 * generation turned off and size counting turned on, and once "for real".
815 * This also means that we don't allocate space until we are sure that the
816 * thing really will compile successfully, and we never have to move the
817 * code and thus invalidate pointers into it. (Note that it has to be in
818 * one piece because free() must be able to free it all.) [NB: not true in perl]
820 * Beware that the optimization-preparation code in here knows about some
821 * of the structure of the compiled regexp. [I'll say.]
824 Perl_pregcomp(pTHX_ char *exp, char *xend, PMOP *pm)
840 FAIL("NULL regexp argument");
842 if (PL_curcop == &PL_compiling ? (PL_hints & HINT_UTF8) : IN_UTF8)
843 PL_reg_flags |= RF_utf8;
847 PL_regprecomp = savepvn(exp, xend - exp);
848 DEBUG_r(if (!PL_colorset) reginitcolors());
849 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sCompiling REx%s `%s%*s%s'\n",
850 PL_colors[4],PL_colors[5],PL_colors[0],
851 xend - exp, PL_regprecomp, PL_colors[1]));
852 PL_regflags = pm->op_pmflags;
856 PL_seen_zerolen = *exp == '^' ? -1 : 0;
860 /* First pass: determine size, legality. */
861 PL_regcomp_parse = exp;
866 PL_regcode = &PL_regdummy;
867 PL_reg_whilem_seen = 0;
868 REGC((U8)REG_MAGIC, (char*)PL_regcode);
869 if (reg(0, &flags) == NULL) {
870 Safefree(PL_regprecomp);
871 PL_regprecomp = Nullch;
874 DEBUG_r(PerlIO_printf(Perl_debug_log, "size %d ", PL_regsize));
876 /* Small enough for pointer-storage convention?
877 If extralen==0, this means that we will not need long jumps. */
878 if (PL_regsize >= 0x10000L && PL_extralen)
879 PL_regsize += PL_extralen;
882 if (PL_reg_whilem_seen > 15)
883 PL_reg_whilem_seen = 15;
885 /* Allocate space and initialize. */
886 Newc(1001, r, sizeof(regexp) + (unsigned)PL_regsize * sizeof(regnode),
889 FAIL("regexp out of space");
891 r->prelen = xend - exp;
892 r->precomp = PL_regprecomp;
894 r->reganch = pm->op_pmflags & PMf_COMPILETIME;
895 r->nparens = PL_regnpar - 1; /* set early to validate backrefs */
897 r->substrs = 0; /* Useful during FAIL. */
898 r->startp = 0; /* Useful during FAIL. */
899 r->endp = 0; /* Useful during FAIL. */
903 /* Second pass: emit code. */
904 PL_regcomp_parse = exp;
908 PL_regcode = r->program;
909 /* Store the count of eval-groups for security checks: */
910 PL_regcode->next_off = ((PL_seen_evals > U16_MAX) ? U16_MAX : PL_seen_evals);
911 REGC((U8)REG_MAGIC, (char*) PL_regcode++);
913 if (reg(0, &flags) == NULL)
916 /* Dig out information for optimizations. */
917 r->reganch = pm->op_pmflags & PMf_COMPILETIME; /* Again? */
918 pm->op_pmflags = PL_regflags;
920 r->reganch |= ROPT_UTF8;
921 r->regstclass = NULL;
922 if (PL_regnaughty >= 10) /* Probably an expensive pattern. */
923 r->reganch |= ROPT_NAUGHTY;
924 scan = r->program + 1; /* First BRANCH. */
926 /* XXXX To minimize changes to RE engine we always allocate
927 3-units-long substrs field. */
928 Newz(1004, r->substrs, 1, struct reg_substr_data);
930 StructCopy(&zero_scan_data, &data, scan_data_t);
931 if (OP(scan) != BRANCH) { /* Only one top-level choice. */
933 STRLEN longest_float_length, longest_fixed_length;
936 /* Skip introductions and multiplicators >= 1. */
937 while ((OP(first) == OPEN && (sawopen = 1)) ||
938 (OP(first) == BRANCH && OP(regnext(first)) != BRANCH) ||
939 (OP(first) == PLUS) ||
940 (OP(first) == MINMOD) ||
941 (PL_regkind[(U8)OP(first)] == CURLY && ARG1(first) > 0) ) {
942 if (OP(first) == PLUS)
945 first += regarglen[(U8)OP(first)];
946 first = NEXTOPER(first);
949 /* Starting-point info. */
951 if (OP(first) == EXACT); /* Empty, get anchored substr later. */
952 else if (strchr((char*)PL_simple+4,OP(first)))
953 r->regstclass = first;
954 else if (PL_regkind[(U8)OP(first)] == BOUND ||
955 PL_regkind[(U8)OP(first)] == NBOUND)
956 r->regstclass = first;
957 else if (PL_regkind[(U8)OP(first)] == BOL) {
958 r->reganch |= (OP(first) == MBOL
963 first = NEXTOPER(first);
966 else if (OP(first) == GPOS) {
967 r->reganch |= ROPT_ANCH_GPOS;
968 first = NEXTOPER(first);
971 else if ((OP(first) == STAR &&
972 PL_regkind[(U8)OP(NEXTOPER(first))] == REG_ANY) &&
973 !(r->reganch & ROPT_ANCH) )
975 /* turn .* into ^.* with an implied $*=1 */
976 int type = OP(NEXTOPER(first));
978 if (type == REG_ANY || type == ANYUTF8)
979 type = ROPT_ANCH_MBOL;
981 type = ROPT_ANCH_SBOL;
983 r->reganch |= type | ROPT_IMPLICIT;
984 first = NEXTOPER(first);
987 if (sawplus && (!sawopen || !PL_regsawback)
988 && !(PL_regseen & REG_SEEN_EVAL)) /* May examine pos and $& */
989 /* x+ must match at the 1st pos of run of x's */
990 r->reganch |= ROPT_SKIP;
992 /* Scan is after the zeroth branch, first is atomic matcher. */
993 DEBUG_r(PerlIO_printf(Perl_debug_log, "first at %d\n",
996 * If there's something expensive in the r.e., find the
997 * longest literal string that must appear and make it the
998 * regmust. Resolve ties in favor of later strings, since
999 * the regstart check works with the beginning of the r.e.
1000 * and avoiding duplication strengthens checking. Not a
1001 * strong reason, but sufficient in the absence of others.
1002 * [Now we resolve ties in favor of the earlier string if
1003 * it happens that c_offset_min has been invalidated, since the
1004 * earlier string may buy us something the later one won't.]
1008 data.longest_fixed = newSVpvn("",0);
1009 data.longest_float = newSVpvn("",0);
1010 data.last_found = newSVpvn("",0);
1011 data.longest = &(data.longest_fixed);
1014 minlen = study_chunk(&first, &fake, scan + PL_regsize, /* Up to end */
1015 &data, SCF_DO_SUBSTR);
1016 if ( PL_regnpar == 1 && data.longest == &(data.longest_fixed)
1017 && data.last_start_min == 0 && data.last_end > 0
1019 && (!(PL_regseen & REG_SEEN_GPOS) || (r->reganch & ROPT_ANCH_GPOS)))
1020 r->reganch |= ROPT_CHECK_ALL;
1022 SvREFCNT_dec(data.last_found);
1024 longest_float_length = CHR_SVLEN(data.longest_float);
1025 if (longest_float_length
1026 || (data.flags & SF_FL_BEFORE_EOL
1027 && (!(data.flags & SF_FL_BEFORE_MEOL)
1028 || (PL_regflags & PMf_MULTILINE)))) {
1031 if (SvCUR(data.longest_fixed) /* ok to leave SvCUR */
1032 && data.offset_fixed == data.offset_float_min
1033 && SvCUR(data.longest_fixed) == SvCUR(data.longest_float))
1034 goto remove_float; /* As in (a)+. */
1036 r->float_substr = data.longest_float;
1037 r->float_min_offset = data.offset_float_min;
1038 r->float_max_offset = data.offset_float_max;
1039 t = (data.flags & SF_FL_BEFORE_EOL /* Can't have SEOL and MULTI */
1040 && (!(data.flags & SF_FL_BEFORE_MEOL)
1041 || (PL_regflags & PMf_MULTILINE)));
1042 fbm_compile(r->float_substr, t ? FBMcf_TAIL : 0);
1046 r->float_substr = Nullsv;
1047 SvREFCNT_dec(data.longest_float);
1048 longest_float_length = 0;
1051 longest_fixed_length = CHR_SVLEN(data.longest_fixed);
1052 if (longest_fixed_length
1053 || (data.flags & SF_FIX_BEFORE_EOL /* Cannot have SEOL and MULTI */
1054 && (!(data.flags & SF_FIX_BEFORE_MEOL)
1055 || (PL_regflags & PMf_MULTILINE)))) {
1058 r->anchored_substr = data.longest_fixed;
1059 r->anchored_offset = data.offset_fixed;
1060 t = (data.flags & SF_FIX_BEFORE_EOL /* Can't have SEOL and MULTI */
1061 && (!(data.flags & SF_FIX_BEFORE_MEOL)
1062 || (PL_regflags & PMf_MULTILINE)));
1063 fbm_compile(r->anchored_substr, t ? FBMcf_TAIL : 0);
1066 r->anchored_substr = Nullsv;
1067 SvREFCNT_dec(data.longest_fixed);
1068 longest_fixed_length = 0;
1071 /* A temporary algorithm prefers floated substr to fixed one to dig more info. */
1072 if (longest_fixed_length > longest_float_length) {
1073 r->check_substr = r->anchored_substr;
1074 r->check_offset_min = r->check_offset_max = r->anchored_offset;
1075 if (r->reganch & ROPT_ANCH_SINGLE)
1076 r->reganch |= ROPT_NOSCAN;
1079 r->check_substr = r->float_substr;
1080 r->check_offset_min = data.offset_float_min;
1081 r->check_offset_max = data.offset_float_max;
1083 if (r->check_substr) {
1084 r->reganch |= RE_USE_INTUIT;
1085 if (SvTAIL(r->check_substr))
1086 r->reganch |= RE_INTUIT_TAIL;
1090 /* Several toplevels. Best we can is to set minlen. */
1093 DEBUG_r(PerlIO_printf(Perl_debug_log, "\n"));
1094 scan = r->program + 1;
1095 minlen = study_chunk(&scan, &fake, scan + PL_regsize, &data, 0);
1096 r->check_substr = r->anchored_substr = r->float_substr = Nullsv;
1100 if (PL_regseen & REG_SEEN_GPOS)
1101 r->reganch |= ROPT_GPOS_SEEN;
1102 if (PL_regseen & REG_SEEN_LOOKBEHIND)
1103 r->reganch |= ROPT_LOOKBEHIND_SEEN;
1104 if (PL_regseen & REG_SEEN_EVAL)
1105 r->reganch |= ROPT_EVAL_SEEN;
1106 Newz(1002, r->startp, PL_regnpar, I32);
1107 Newz(1002, r->endp, PL_regnpar, I32);
1108 DEBUG_r(regdump(r));
1113 - reg - regular expression, i.e. main body or parenthesized thing
1115 * Caller must absorb opening parenthesis.
1117 * Combining parenthesis handling with the base level of regular expression
1118 * is a trifle forced, but the need to tie the tails of the branches to what
1119 * follows makes it hard to avoid.
1122 S_reg(pTHX_ I32 paren, I32 *flagp)
1123 /* paren: Parenthesized? 0=top, 1=(, inside: changed to letter. */
1126 register regnode *ret; /* Will be the head of the group. */
1127 register regnode *br;
1128 register regnode *lastbr;
1129 register regnode *ender = 0;
1130 register I32 parno = 0;
1131 I32 flags, oregflags = PL_regflags, have_branch = 0, open = 0;
1134 *flagp = 0; /* Tentatively. */
1136 /* Make an OPEN node, if parenthesized. */
1138 if (*PL_regcomp_parse == '?') {
1139 U16 posflags = 0, negflags = 0;
1140 U16 *flagsp = &posflags;
1144 paren = *PL_regcomp_parse++;
1145 ret = NULL; /* For look-ahead/behind. */
1148 PL_regseen |= REG_SEEN_LOOKBEHIND;
1149 if (*PL_regcomp_parse == '!')
1151 if (*PL_regcomp_parse != '=' && *PL_regcomp_parse != '!')
1162 FAIL2("Sequence (?%c...) not implemented", (int)paren);
1165 while (*PL_regcomp_parse && *PL_regcomp_parse != ')')
1167 if (*PL_regcomp_parse != ')')
1168 FAIL("Sequence (?#... not terminated");
1174 paren = *PL_regcomp_parse++;
1179 I32 count = 1, n = 0;
1181 char *s = PL_regcomp_parse;
1183 OP_4tree *sop, *rop;
1186 PL_regseen |= REG_SEEN_EVAL;
1187 while (count && (c = *PL_regcomp_parse)) {
1188 if (c == '\\' && PL_regcomp_parse[1])
1196 if (*PL_regcomp_parse != ')')
1197 FAIL("Sequence (?{...}) not terminated or not {}-balanced");
1201 if (PL_regcomp_parse - 1 - s)
1202 sv = newSVpvn(s, PL_regcomp_parse - 1 - s);
1204 sv = newSVpvn("", 0);
1206 rop = sv_compile_2op(sv, &sop, "re", &av);
1208 n = add_data(3, "nop");
1209 PL_regcomp_rx->data->data[n] = (void*)rop;
1210 PL_regcomp_rx->data->data[n+1] = (void*)sop;
1211 PL_regcomp_rx->data->data[n+2] = (void*)av;
1214 else { /* First pass */
1215 if (PL_reginterp_cnt < ++PL_seen_evals
1216 && PL_curcop != &PL_compiling)
1217 /* No compiled RE interpolated, has runtime
1218 components ===> unsafe. */
1219 FAIL("Eval-group not allowed at runtime, use re 'eval'");
1221 FAIL("Eval-group in insecure regular expression");
1226 ret = reg_node(LOGICAL);
1229 regtail(ret, reganode(EVAL, n));
1232 return reganode(EVAL, n);
1236 if (PL_regcomp_parse[0] == '?') {
1237 if (PL_regcomp_parse[1] == '=' || PL_regcomp_parse[1] == '!'
1238 || PL_regcomp_parse[1] == '<'
1239 || PL_regcomp_parse[1] == '{') { /* Lookahead or eval. */
1242 ret = reg_node(LOGICAL);
1245 regtail(ret, reg(1, &flag));
1249 else if (PL_regcomp_parse[0] >= '1' && PL_regcomp_parse[0] <= '9' ) {
1250 parno = atoi(PL_regcomp_parse++);
1252 while (isDIGIT(*PL_regcomp_parse))
1254 ret = reganode(GROUPP, parno);
1255 if ((c = *nextchar()) != ')')
1256 FAIL2("Switch (?(number%c not recognized", c);
1258 regtail(ret, reganode(IFTHEN, 0));
1259 br = regbranch(&flags, 1);
1261 br = reganode(LONGJMP, 0);
1263 regtail(br, reganode(LONGJMP, 0));
1268 lastbr = reganode(IFTHEN, 0); /* Fake one for optimizer. */
1269 regbranch(&flags, 1);
1270 regtail(ret, lastbr);
1278 FAIL("Switch (?(condition)... contains too many branches");
1279 ender = reg_node(TAIL);
1282 regtail(lastbr, ender);
1283 regtail(NEXTOPER(NEXTOPER(lastbr)), ender);
1286 regtail(ret, ender);
1290 FAIL2("Unknown condition for (?(%.2s", PL_regcomp_parse);
1294 FAIL("Sequence (? incomplete");
1299 while (*PL_regcomp_parse && strchr("iogcmsx", *PL_regcomp_parse)) {
1300 if (*PL_regcomp_parse != 'o')
1301 pmflag(flagsp, *PL_regcomp_parse);
1304 if (*PL_regcomp_parse == '-') {
1309 PL_regflags |= posflags;
1310 PL_regflags &= ~negflags;
1311 if (*PL_regcomp_parse == ':') {
1317 if (*PL_regcomp_parse != ')')
1318 FAIL2("Sequence (?%c...) not recognized", *PL_regcomp_parse);
1327 ret = reganode(OPEN, parno);
1334 /* Pick up the branches, linking them together. */
1335 br = regbranch(&flags, 1);
1338 if (*PL_regcomp_parse == '|') {
1339 if (!SIZE_ONLY && PL_extralen) {
1340 reginsert(BRANCHJ, br);
1343 reginsert(BRANCH, br);
1346 PL_extralen += 1; /* For BRANCHJ-BRANCH. */
1348 else if (paren == ':') {
1349 *flagp |= flags&SIMPLE;
1351 if (open) { /* Starts with OPEN. */
1352 regtail(ret, br); /* OPEN -> first. */
1354 else if (paren != '?') /* Not Conditional */
1358 *flagp |= flags&SPSTART;
1360 while (*PL_regcomp_parse == '|') {
1361 if (!SIZE_ONLY && PL_extralen) {
1362 ender = reganode(LONGJMP,0);
1363 regtail(NEXTOPER(NEXTOPER(lastbr)), ender); /* Append to the previous. */
1366 PL_extralen += 2; /* Account for LONGJMP. */
1368 br = regbranch(&flags, 0);
1371 regtail(lastbr, br); /* BRANCH -> BRANCH. */
1375 *flagp |= flags&SPSTART;
1378 if (have_branch || paren != ':') {
1379 /* Make a closing node, and hook it on the end. */
1382 ender = reg_node(TAIL);
1385 ender = reganode(CLOSE, parno);
1391 *flagp &= ~HASWIDTH;
1394 ender = reg_node(SUCCEED);
1397 ender = reg_node(END);
1400 regtail(lastbr, ender);
1403 /* Hook the tails of the branches to the closing node. */
1404 for (br = ret; br != NULL; br = regnext(br)) {
1405 regoptail(br, ender);
1412 static char parens[] = "=!<,>";
1414 if (paren && (p = strchr(parens, paren))) {
1415 int node = ((p - parens) % 2) ? UNLESSM : IFMATCH;
1416 int flag = (p - parens) > 1;
1419 node = SUSPEND, flag = 0;
1420 reginsert(node,ret);
1422 regtail(ret, reg_node(TAIL));
1426 /* Check for proper termination. */
1428 PL_regflags = oregflags;
1429 if (PL_regcomp_parse >= PL_regxend || *nextchar() != ')') {
1430 FAIL("unmatched () in regexp");
1433 else if (!paren && PL_regcomp_parse < PL_regxend) {
1434 if (*PL_regcomp_parse == ')') {
1435 FAIL("unmatched () in regexp");
1438 FAIL("junk on end of regexp"); /* "Can't happen". */
1446 - regbranch - one alternative of an | operator
1448 * Implements the concatenation operator.
1451 S_regbranch(pTHX_ I32 *flagp, I32 first)
1454 register regnode *ret;
1455 register regnode *chain = NULL;
1456 register regnode *latest;
1457 I32 flags = 0, c = 0;
1462 if (!SIZE_ONLY && PL_extralen)
1463 ret = reganode(BRANCHJ,0);
1465 ret = reg_node(BRANCH);
1468 if (!first && SIZE_ONLY)
1469 PL_extralen += 1; /* BRANCHJ */
1471 *flagp = WORST; /* Tentatively. */
1475 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != '|' && *PL_regcomp_parse != ')') {
1477 latest = regpiece(&flags);
1478 if (latest == NULL) {
1479 if (flags & TRYAGAIN)
1483 else if (ret == NULL)
1485 *flagp |= flags&HASWIDTH;
1486 if (chain == NULL) /* First piece. */
1487 *flagp |= flags&SPSTART;
1490 regtail(chain, latest);
1495 if (chain == NULL) { /* Loop ran zero times. */
1496 chain = reg_node(NOTHING);
1501 *flagp |= flags&SIMPLE;
1508 - regpiece - something followed by possible [*+?]
1510 * Note that the branching code sequences used for ? and the general cases
1511 * of * and + are somewhat optimized: they use the same NOTHING node as
1512 * both the endmarker for their branch list and the body of the last branch.
1513 * It might seem that this node could be dispensed with entirely, but the
1514 * endmarker role is not redundant.
1517 S_regpiece(pTHX_ I32 *flagp)
1520 register regnode *ret;
1522 register char *next;
1524 char *origparse = PL_regcomp_parse;
1527 I32 max = REG_INFTY;
1529 ret = regatom(&flags);
1531 if (flags & TRYAGAIN)
1536 op = *PL_regcomp_parse;
1538 if (op == '{' && regcurly(PL_regcomp_parse)) {
1539 next = PL_regcomp_parse + 1;
1541 while (isDIGIT(*next) || *next == ',') {
1550 if (*next == '}') { /* got one */
1554 min = atoi(PL_regcomp_parse);
1558 maxpos = PL_regcomp_parse;
1560 if (!max && *maxpos != '0')
1561 max = REG_INFTY; /* meaning "infinity" */
1562 else if (max >= REG_INFTY)
1563 FAIL2("Quantifier in {,} bigger than %d", REG_INFTY - 1);
1564 PL_regcomp_parse = next;
1568 if ((flags&SIMPLE)) {
1569 PL_regnaughty += 2 + PL_regnaughty / 2;
1570 reginsert(CURLY, ret);
1573 regnode *w = reg_node(WHILEM);
1577 if (!SIZE_ONLY && PL_extralen) {
1578 reginsert(LONGJMP,ret);
1579 reginsert(NOTHING,ret);
1580 NEXT_OFF(ret) = 3; /* Go over LONGJMP. */
1582 reginsert(CURLYX,ret);
1583 if (!SIZE_ONLY && PL_extralen)
1584 NEXT_OFF(ret) = 3; /* Go over NOTHING to LONGJMP. */
1585 regtail(ret, reg_node(NOTHING));
1587 PL_reg_whilem_seen++, PL_extralen += 3;
1588 PL_regnaughty += 4 + PL_regnaughty; /* compound interest */
1596 if (max && max < min)
1597 FAIL("Can't do {n,m} with n > m");
1612 #if 0 /* Now runtime fix should be reliable. */
1613 if (!(flags&HASWIDTH) && op != '?')
1614 FAIL("regexp *+ operand could be empty");
1619 *flagp = (op != '+') ? (WORST|SPSTART|HASWIDTH) : (WORST|HASWIDTH);
1621 if (op == '*' && (flags&SIMPLE)) {
1622 reginsert(STAR, ret);
1626 else if (op == '*') {
1630 else if (op == '+' && (flags&SIMPLE)) {
1631 reginsert(PLUS, ret);
1635 else if (op == '+') {
1639 else if (op == '?') {
1644 if (ckWARN(WARN_UNSAFE) && !SIZE_ONLY && !(flags&HASWIDTH) && max > REG_INFTY/3) {
1645 Perl_warner(aTHX_ WARN_UNSAFE, "%.*s matches null string many times",
1646 PL_regcomp_parse - origparse, origparse);
1649 if (*PL_regcomp_parse == '?') {
1651 reginsert(MINMOD, ret);
1652 regtail(ret, ret + NODE_STEP_REGNODE);
1654 if (ISMULT2(PL_regcomp_parse))
1655 FAIL("nested *?+ in regexp");
1661 - regatom - the lowest level
1663 * Optimization: gobbles an entire sequence of ordinary characters so that
1664 * it can turn them into a single node, which is smaller to store and
1665 * faster to run. Backslashed characters are exceptions, each becoming a
1666 * separate node; the code is simpler that way and it's not worth fixing.
1668 * [Yes, it is worth fixing, some scripts can run twice the speed.]
1671 S_regatom(pTHX_ I32 *flagp)
1674 register regnode *ret = 0;
1677 *flagp = WORST; /* Tentatively. */
1680 switch (*PL_regcomp_parse) {
1684 if (PL_regflags & PMf_MULTILINE)
1685 ret = reg_node(MBOL);
1686 else if (PL_regflags & PMf_SINGLELINE)
1687 ret = reg_node(SBOL);
1689 ret = reg_node(BOL);
1692 if (PL_regcomp_parse[1])
1695 if (PL_regflags & PMf_MULTILINE)
1696 ret = reg_node(MEOL);
1697 else if (PL_regflags & PMf_SINGLELINE)
1698 ret = reg_node(SEOL);
1700 ret = reg_node(EOL);
1705 if (PL_regflags & PMf_SINGLELINE)
1706 ret = reg_node(SANYUTF8);
1708 ret = reg_node(ANYUTF8);
1712 if (PL_regflags & PMf_SINGLELINE)
1713 ret = reg_node(SANY);
1715 ret = reg_node(REG_ANY);
1716 *flagp |= HASWIDTH|SIMPLE;
1722 ret = (UTF ? regclassutf8() : regclass());
1723 if (*PL_regcomp_parse != ']')
1724 FAIL("unmatched [] in regexp");
1726 *flagp |= HASWIDTH|SIMPLE;
1730 ret = reg(1, &flags);
1732 if (flags & TRYAGAIN)
1736 *flagp |= flags&(HASWIDTH|SPSTART|SIMPLE);
1740 if (flags & TRYAGAIN) {
1744 FAIL2("internal urp in regexp at /%s/", PL_regcomp_parse);
1745 /* Supposed to be caught earlier. */
1748 if (!regcurly(PL_regcomp_parse)) {
1756 FAIL("?+*{} follows nothing in regexp");
1759 switch (*++PL_regcomp_parse) {
1762 ret = reg_node(SBOL);
1767 ret = reg_node(GPOS);
1768 PL_regseen |= REG_SEEN_GPOS;
1773 ret = reg_node(SEOL);
1778 ret = reg_node(EOS);
1780 PL_seen_zerolen++; /* Do not optimize RE away */
1784 ret = reg_node(SANY);
1785 *flagp |= HASWIDTH|SIMPLE;
1789 ret = reg_node(CLUMP);
1792 if (UTF && !PL_utf8_mark)
1793 is_utf8_mark((U8*)"~"); /* preload table */
1798 ? (LOC ? ALNUMLUTF8 : ALNUMUTF8)
1799 : (LOC ? ALNUML : ALNUM));
1800 *flagp |= HASWIDTH|SIMPLE;
1802 if (UTF && !PL_utf8_alnum)
1803 is_utf8_alnum((U8*)"a"); /* preload table */
1808 ? (LOC ? NALNUMLUTF8 : NALNUMUTF8)
1809 : (LOC ? NALNUML : NALNUM));
1810 *flagp |= HASWIDTH|SIMPLE;
1812 if (UTF && !PL_utf8_alnum)
1813 is_utf8_alnum((U8*)"a"); /* preload table */
1817 PL_regseen |= REG_SEEN_LOOKBEHIND;
1820 ? (LOC ? BOUNDLUTF8 : BOUNDUTF8)
1821 : (LOC ? BOUNDL : BOUND));
1824 if (UTF && !PL_utf8_alnum)
1825 is_utf8_alnum((U8*)"a"); /* preload table */
1829 PL_regseen |= REG_SEEN_LOOKBEHIND;
1832 ? (LOC ? NBOUNDLUTF8 : NBOUNDUTF8)
1833 : (LOC ? NBOUNDL : NBOUND));
1836 if (UTF && !PL_utf8_alnum)
1837 is_utf8_alnum((U8*)"a"); /* preload table */
1842 ? (LOC ? SPACELUTF8 : SPACEUTF8)
1843 : (LOC ? SPACEL : SPACE));
1844 *flagp |= HASWIDTH|SIMPLE;
1846 if (UTF && !PL_utf8_space)
1847 is_utf8_space((U8*)" "); /* preload table */
1852 ? (LOC ? NSPACELUTF8 : NSPACEUTF8)
1853 : (LOC ? NSPACEL : NSPACE));
1854 *flagp |= HASWIDTH|SIMPLE;
1856 if (UTF && !PL_utf8_space)
1857 is_utf8_space((U8*)" "); /* preload table */
1860 ret = reg_node(UTF ? DIGITUTF8 : DIGIT);
1861 *flagp |= HASWIDTH|SIMPLE;
1863 if (UTF && !PL_utf8_digit)
1864 is_utf8_digit((U8*)"1"); /* preload table */
1867 ret = reg_node(UTF ? NDIGITUTF8 : NDIGIT);
1868 *flagp |= HASWIDTH|SIMPLE;
1870 if (UTF && !PL_utf8_digit)
1871 is_utf8_digit((U8*)"1"); /* preload table */
1875 { /* a lovely hack--pretend we saw [\pX] instead */
1876 char* oldregxend = PL_regxend;
1878 if (PL_regcomp_parse[1] == '{') {
1879 PL_regxend = strchr(PL_regcomp_parse, '}');
1881 FAIL("Missing right brace on \\p{}");
1885 PL_regxend = PL_regcomp_parse + 2;
1888 ret = regclassutf8();
1890 PL_regxend = oldregxend;
1893 *flagp |= HASWIDTH|SIMPLE;
1906 case '1': case '2': case '3': case '4':
1907 case '5': case '6': case '7': case '8': case '9':
1909 I32 num = atoi(PL_regcomp_parse);
1911 if (num > 9 && num >= PL_regnpar)
1914 if (!SIZE_ONLY && num > PL_regcomp_rx->nparens)
1915 FAIL("reference to nonexistent group");
1918 ? (LOC ? REFFL : REFF)
1921 while (isDIGIT(*PL_regcomp_parse))
1929 if (PL_regcomp_parse >= PL_regxend)
1930 FAIL("trailing \\ in regexp");
1933 /* Do not generate `unrecognized' warnings here, we fall
1934 back into the quick-grab loop below */
1940 if (PL_regflags & PMf_EXTENDED) {
1941 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != '\n') PL_regcomp_parse++;
1942 if (PL_regcomp_parse < PL_regxend)
1958 ? (LOC ? EXACTFL : EXACTF)
1961 for (len = 0, p = PL_regcomp_parse - 1;
1962 len < 127 && p < PL_regxend;
1967 if (PL_regflags & PMf_EXTENDED)
1968 p = regwhite(p, PL_regxend);
2022 char* e = strchr(p, '}');
2025 FAIL("Missing right brace on \\x{}");
2027 ender = scan_hex(p + 1, e - p, &numlen);
2028 if (numlen + len >= 127) { /* numlen is generous */
2035 FAIL("Can't use \\x{} without 'use utf8' declaration");
2038 ender = scan_hex(p, 2, &numlen);
2044 ender = UCHARAT(p++);
2045 ender = toCTRL(ender);
2047 case '0': case '1': case '2': case '3':case '4':
2048 case '5': case '6': case '7': case '8':case '9':
2050 (isDIGIT(p[1]) && atoi(p) >= PL_regnpar) ) {
2051 ender = scan_oct(p, 3, &numlen);
2060 if (p >= PL_regxend)
2061 FAIL("trailing \\ in regexp");
2064 if (!SIZE_ONLY && ckWARN(WARN_UNSAFE) && isALPHA(*p))
2065 Perl_warner(aTHX_ WARN_UNSAFE,
2066 "/%.127s/: Unrecognized escape \\%c passed through",
2069 goto normal_default;
2074 if ((*p & 0xc0) == 0xc0 && UTF) {
2075 ender = utf8_to_uv((U8*)p, &numlen);
2082 if (PL_regflags & PMf_EXTENDED)
2083 p = regwhite(p, PL_regxend);
2086 ender = toLOWER_LC_uni(ender);
2088 ender = toLOWER_uni(ender);
2090 if (ISMULT2(p)) { /* Back off on ?+*. */
2093 else if (ender >= 0x80 && UTF) {
2094 reguni(ender, s, &numlen);
2104 if (ender >= 0x80 && UTF) {
2105 reguni(ender, s, &numlen);
2113 PL_regcomp_parse = p - 1;
2116 FAIL("internal disaster in regexp");
2124 PL_regsize += STR_SZ(len);
2126 PL_regcode += STR_SZ(len);
2135 S_regwhite(pTHX_ char *p, char *e)
2140 else if (*p == '#') {
2143 } while (p < e && *p != '\n');
2151 /* Parse POSIX character classes: [[:foo:]], [[=foo=]], [[.foo.]].
2152 Character classes ([:foo:]) can also be negated ([:^foo:]).
2153 Returns a named class id (ANYOF_XXX) if successful, -1 otherwise.
2154 Equivalence classes ([=foo=]) and composites ([.foo.]) are parsed,
2155 but trigger warnings because they are currently unimplemented. */
2157 S_regpposixcc(pTHX_ I32 value)
2161 I32 namedclass = -1;
2163 if (value == '[' && PL_regcomp_parse + 1 < PL_regxend &&
2164 /* I smell either [: or [= or [. -- POSIX has been here, right? */
2165 (*PL_regcomp_parse == ':' ||
2166 *PL_regcomp_parse == '=' ||
2167 *PL_regcomp_parse == '.')) {
2168 char c = *PL_regcomp_parse;
2169 char* s = PL_regcomp_parse++;
2171 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != c)
2173 if (PL_regcomp_parse == PL_regxend)
2174 /* Grandfather lone [:, [=, [. */
2175 PL_regcomp_parse = s;
2177 char* t = PL_regcomp_parse++; /* skip over the c */
2179 if (*PL_regcomp_parse == ']') {
2180 PL_regcomp_parse++; /* skip over the ending ] */
2183 I32 complement = *posixcc == '^' ? *posixcc++ : 0;
2184 I32 skip = 5; /* the most common skip */
2188 if (strnEQ(posixcc, "alnum", 5))
2190 complement ? ANYOF_NALNUMC : ANYOF_ALNUMC;
2191 else if (strnEQ(posixcc, "alpha", 5))
2193 complement ? ANYOF_NALPHA : ANYOF_ALPHA;
2194 else if (strnEQ(posixcc, "ascii", 5))
2196 complement ? ANYOF_NASCII : ANYOF_ASCII;
2199 if (strnEQ(posixcc, "cntrl", 5))
2201 complement ? ANYOF_NCNTRL : ANYOF_CNTRL;
2204 if (strnEQ(posixcc, "digit", 5))
2206 complement ? ANYOF_NDIGIT : ANYOF_DIGIT;
2209 if (strnEQ(posixcc, "graph", 5))
2211 complement ? ANYOF_NGRAPH : ANYOF_GRAPH;
2214 if (strnEQ(posixcc, "lower", 5))
2216 complement ? ANYOF_NLOWER : ANYOF_LOWER;
2219 if (strnEQ(posixcc, "print", 5))
2221 complement ? ANYOF_NPRINT : ANYOF_PRINT;
2222 else if (strnEQ(posixcc, "punct", 5))
2224 complement ? ANYOF_NPUNCT : ANYOF_PUNCT;
2227 if (strnEQ(posixcc, "space", 5))
2229 complement ? ANYOF_NSPACE : ANYOF_SPACE;
2231 if (strnEQ(posixcc, "upper", 5))
2233 complement ? ANYOF_NUPPER : ANYOF_UPPER;
2235 case 'w': /* this is not POSIX, this is the Perl \w */
2236 if (strnEQ(posixcc, "word", 4)) {
2238 complement ? ANYOF_NALNUM : ANYOF_ALNUM;
2243 if (strnEQ(posixcc, "xdigit", 6)) {
2245 complement ? ANYOF_NXDIGIT : ANYOF_XDIGIT;
2250 if ((namedclass == OOB_NAMEDCLASS ||
2251 !(posixcc + skip + 2 < PL_regxend &&
2252 (posixcc[skip] == ':' &&
2253 posixcc[skip + 1] == ']'))))
2254 Perl_croak(aTHX_ "Character class [:%.*s:] unknown",
2256 } else if (ckWARN(WARN_UNSAFE) && !SIZE_ONLY)
2257 /* [[=foo=]] and [[.foo.]] are still future. */
2258 Perl_warner(aTHX_ WARN_UNSAFE,
2259 "Character class syntax [%c %c] is reserved for future extensions", c, c);
2261 /* Maternal grandfather:
2262 * "[:" ending in ":" but not in ":]" */
2263 PL_regcomp_parse = s;
2272 S_checkposixcc(pTHX)
2274 if (ckWARN(WARN_UNSAFE) && !SIZE_ONLY &&
2275 (*PL_regcomp_parse == ':' ||
2276 *PL_regcomp_parse == '=' ||
2277 *PL_regcomp_parse == '.')) {
2278 char *s = PL_regcomp_parse;
2281 while(*s && isALNUM(*s))
2283 if (*s && c == *s && s[1] == ']') {
2284 Perl_warner(aTHX_ WARN_UNSAFE,
2285 "Character class syntax [%c %c] belongs inside character classes", c, c);
2286 if (c == '=' || c == '.')
2287 Perl_warner(aTHX_ WARN_UNSAFE,
2288 "Character class syntax [%c %c] is reserved for future extensions", c, c);
2297 register char *opnd, *s;
2299 register I32 lastvalue = OOB_CHAR8;
2300 register I32 range = 0;
2301 register regnode *ret;
2306 s = opnd = MASK(PL_regcode);
2307 ret = reg_node(ANYOF);
2308 for (value = 0; value < ANYOF_SIZE; value++)
2310 if (*PL_regcomp_parse == '^') { /* Complement of range. */
2314 ANYOF_FLAGS(opnd) |= ANYOF_INVERT;
2317 PL_regcode += ANY_SKIP;
2319 ANYOF_FLAGS(opnd) |= ANYOF_FOLD;
2321 ANYOF_FLAGS(opnd) |= ANYOF_LOCALE;
2324 PL_regsize += ANY_SKIP;
2329 if (*PL_regcomp_parse == ']' || *PL_regcomp_parse == '-')
2330 goto skipcond; /* allow 1st char to be ] or - */
2331 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != ']') {
2333 namedclass = OOB_NAMEDCLASS;
2334 value = UCHARAT(PL_regcomp_parse++);
2336 namedclass = regpposixcc(value);
2337 else if (value == '\\') {
2338 value = UCHARAT(PL_regcomp_parse++);
2340 case 'w': namedclass = ANYOF_ALNUM; break;
2341 case 'W': namedclass = ANYOF_NALNUM; break;
2342 case 's': namedclass = ANYOF_SPACE; break;
2343 case 'S': namedclass = ANYOF_NSPACE; break;
2344 case 'd': namedclass = ANYOF_DIGIT; break;
2345 case 'D': namedclass = ANYOF_NDIGIT; break;
2346 case 'n': value = '\n'; break;
2347 case 'r': value = '\r'; break;
2348 case 't': value = '\t'; break;
2349 case 'f': value = '\f'; break;
2350 case 'b': value = '\b'; break;
2351 case 'e': value = '\033'; break;
2352 case 'a': value = '\007'; break;
2354 value = scan_hex(PL_regcomp_parse, 2, &numlen);
2355 PL_regcomp_parse += numlen;
2358 value = UCHARAT(PL_regcomp_parse++);
2359 value = toCTRL(value);
2361 case '0': case '1': case '2': case '3': case '4':
2362 case '5': case '6': case '7': case '8': case '9':
2363 value = scan_oct(--PL_regcomp_parse, 3, &numlen);
2364 PL_regcomp_parse += numlen;
2368 if (!SIZE_ONLY && namedclass > OOB_NAMEDCLASS) {
2370 FAIL("invalid [] range in regexp"); /* [a-\w], [a-[:word:]] */
2371 switch (namedclass) {
2374 ANYOF_CLASS_SET(opnd, ANYOF_ALNUM);
2376 for (value = 0; value < 256; value++)
2378 ANYOF_BITMAP_SET(opnd, value);
2383 ANYOF_CLASS_SET(opnd, ANYOF_NALNUM);
2385 for (value = 0; value < 256; value++)
2386 if (!isALNUM(value))
2387 ANYOF_BITMAP_SET(opnd, value);
2392 ANYOF_CLASS_SET(opnd, ANYOF_SPACE);
2394 for (value = 0; value < 256; value++)
2396 ANYOF_BITMAP_SET(opnd, value);
2401 ANYOF_CLASS_SET(opnd, ANYOF_NSPACE);
2403 for (value = 0; value < 256; value++)
2404 if (!isSPACE(value))
2405 ANYOF_BITMAP_SET(opnd, value);
2410 ANYOF_CLASS_SET(opnd, ANYOF_DIGIT);
2412 for (value = '0'; value <= '9'; value++)
2413 ANYOF_BITMAP_SET(opnd, value);
2418 ANYOF_CLASS_SET(opnd, ANYOF_NDIGIT);
2420 for (value = 0; value < '0'; value++)
2421 ANYOF_BITMAP_SET(opnd, value);
2422 for (value = '9' + 1; value < 256; value++)
2423 ANYOF_BITMAP_SET(opnd, value);
2428 ANYOF_CLASS_SET(opnd, ANYOF_NALNUMC);
2430 for (value = 0; value < 256; value++)
2431 if (!isALNUMC(value))
2432 ANYOF_BITMAP_SET(opnd, value);
2437 ANYOF_CLASS_SET(opnd, ANYOF_ALNUMC);
2439 for (value = 0; value < 256; value++)
2440 if (isALNUMC(value))
2441 ANYOF_BITMAP_SET(opnd, value);
2446 ANYOF_CLASS_SET(opnd, ANYOF_ALPHA);
2448 for (value = 0; value < 256; value++)
2450 ANYOF_BITMAP_SET(opnd, value);
2455 ANYOF_CLASS_SET(opnd, ANYOF_NALPHA);
2457 for (value = 0; value < 256; value++)
2458 if (!isALPHA(value))
2459 ANYOF_BITMAP_SET(opnd, value);
2464 ANYOF_CLASS_SET(opnd, ANYOF_ASCII);
2466 for (value = 0; value < 128; value++)
2467 ANYOF_BITMAP_SET(opnd, value);
2472 ANYOF_CLASS_SET(opnd, ANYOF_NASCII);
2474 for (value = 128; value < 256; value++)
2475 ANYOF_BITMAP_SET(opnd, value);
2480 ANYOF_CLASS_SET(opnd, ANYOF_CNTRL);
2482 for (value = 0; value < 256; value++)
2484 ANYOF_BITMAP_SET(opnd, value);
2486 lastvalue = OOB_CHAR8;
2490 ANYOF_CLASS_SET(opnd, ANYOF_NCNTRL);
2492 for (value = 0; value < 256; value++)
2493 if (!isCNTRL(value))
2494 ANYOF_BITMAP_SET(opnd, value);
2499 ANYOF_CLASS_SET(opnd, ANYOF_GRAPH);
2501 for (value = 0; value < 256; value++)
2503 ANYOF_BITMAP_SET(opnd, value);
2508 ANYOF_CLASS_SET(opnd, ANYOF_NGRAPH);
2510 for (value = 0; value < 256; value++)
2511 if (!isGRAPH(value))
2512 ANYOF_BITMAP_SET(opnd, value);
2517 ANYOF_CLASS_SET(opnd, ANYOF_LOWER);
2519 for (value = 0; value < 256; value++)
2521 ANYOF_BITMAP_SET(opnd, value);
2526 ANYOF_CLASS_SET(opnd, ANYOF_NLOWER);
2528 for (value = 0; value < 256; value++)
2529 if (!isLOWER(value))
2530 ANYOF_BITMAP_SET(opnd, value);
2535 ANYOF_CLASS_SET(opnd, ANYOF_PRINT);
2537 for (value = 0; value < 256; value++)
2539 ANYOF_BITMAP_SET(opnd, value);
2544 ANYOF_CLASS_SET(opnd, ANYOF_NPRINT);
2546 for (value = 0; value < 256; value++)
2547 if (!isPRINT(value))
2548 ANYOF_BITMAP_SET(opnd, value);
2553 ANYOF_CLASS_SET(opnd, ANYOF_PUNCT);
2555 for (value = 0; value < 256; value++)
2557 ANYOF_BITMAP_SET(opnd, value);
2562 ANYOF_CLASS_SET(opnd, ANYOF_NPUNCT);
2564 for (value = 0; value < 256; value++)
2565 if (!isPUNCT(value))
2566 ANYOF_BITMAP_SET(opnd, value);
2571 ANYOF_CLASS_SET(opnd, ANYOF_UPPER);
2573 for (value = 0; value < 256; value++)
2575 ANYOF_BITMAP_SET(opnd, value);
2580 ANYOF_CLASS_SET(opnd, ANYOF_NUPPER);
2582 for (value = 0; value < 256; value++)
2583 if (!isUPPER(value))
2584 ANYOF_BITMAP_SET(opnd, value);
2589 ANYOF_CLASS_SET(opnd, ANYOF_XDIGIT);
2591 for (value = 0; value < 256; value++)
2592 if (isXDIGIT(value))
2593 ANYOF_BITMAP_SET(opnd, value);
2598 ANYOF_CLASS_SET(opnd, ANYOF_NXDIGIT);
2600 for (value = 0; value < 256; value++)
2601 if (!isXDIGIT(value))
2602 ANYOF_BITMAP_SET(opnd, value);
2606 FAIL("invalid [::] class in regexp");
2610 ANYOF_FLAGS(opnd) |= ANYOF_CLASS;
2614 if (lastvalue > value)
2615 FAIL("invalid [] range in regexp"); /* [b-a] */
2620 if (*PL_regcomp_parse == '-' && PL_regcomp_parse+1 < PL_regxend &&
2621 PL_regcomp_parse[1] != ']') {
2622 if (namedclass > OOB_NAMEDCLASS)
2623 FAIL("invalid [] range in regexp"); /* [\w-a] */
2626 continue; /* do it next time */
2629 /* now is the next time */
2631 #ifndef ASCIIish /* EBCDIC, for example. */
2632 if ((isLOWER(lastvalue) && isLOWER(value)) ||
2633 (isUPPER(lastvalue) && isUPPER(value)))
2636 if (isLOWER(lastvalue)) {
2637 for (i = lastvalue; i <= value; i++)
2639 ANYOF_BITMAP_SET(opnd, i);
2641 for (i = lastvalue; i <= value; i++)
2643 ANYOF_BITMAP_SET(opnd, i);
2648 for ( ; lastvalue <= value; lastvalue++)
2649 ANYOF_BITMAP_SET(opnd, lastvalue);
2653 /* optimize case-insensitive simple patterns (e.g. /[a-z]/i) */
2655 (ANYOF_FLAGS(opnd) & (ANYOF_FLAGS_ALL ^ ANYOF_INVERT)) == ANYOF_FOLD) {
2656 for (value = 0; value < 256; ++value) {
2657 if (ANYOF_BITMAP_TEST(opnd, value)) {
2658 I32 cf = PL_fold[value];
2659 ANYOF_BITMAP_SET(opnd, cf);
2662 ANYOF_FLAGS(opnd) &= ~ANYOF_FOLD;
2664 /* optimize inverted simple patterns (e.g. [^a-z]) */
2665 if (!SIZE_ONLY && (ANYOF_FLAGS(opnd) & ANYOF_FLAGS_ALL) == ANYOF_INVERT) {
2666 for (value = 0; value < ANYOF_BITMAP_SIZE; ++value)
2667 opnd[ANYOF_BITMAP_OFFSET + value] ^= ANYOF_FLAGS_ALL;
2668 ANYOF_FLAGS(opnd) = 0;
2674 S_regclassutf8(pTHX)
2677 register char *opnd, *e;
2679 register U32 lastvalue = OOB_UTF8;
2680 register I32 range = 0;
2681 register regnode *ret;
2688 if (*PL_regcomp_parse == '^') { /* Complement of range. */
2692 flags |= ANYOF_INVERT;
2696 flags |= ANYOF_FOLD;
2698 flags |= ANYOF_LOCALE;
2699 listsv = newSVpvn("# comment\n",10);
2704 if (*PL_regcomp_parse == ']' || *PL_regcomp_parse == '-')
2705 goto skipcond; /* allow 1st char to be ] or - */
2707 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != ']') {
2709 namedclass = OOB_NAMEDCLASS;
2710 value = utf8_to_uv((U8*)PL_regcomp_parse, &numlen);
2711 PL_regcomp_parse += numlen;
2714 namedclass = regpposixcc(value);
2715 else if (value == '\\') {
2716 value = utf8_to_uv((U8*)PL_regcomp_parse, &numlen);
2717 PL_regcomp_parse += numlen;
2719 case 'w': namedclass = ANYOF_ALNUM; break;
2720 case 'W': namedclass = ANYOF_NALNUM; break;
2721 case 's': namedclass = ANYOF_SPACE; break;
2722 case 'S': namedclass = ANYOF_NSPACE; break;
2723 case 'd': namedclass = ANYOF_DIGIT; break;
2724 case 'D': namedclass = ANYOF_NDIGIT; break;
2727 if (*PL_regcomp_parse == '{') {
2728 e = strchr(PL_regcomp_parse++, '}');
2730 FAIL("Missing right brace on \\p{}");
2731 n = e - PL_regcomp_parse;
2734 e = PL_regcomp_parse;
2739 Perl_sv_catpvf(aTHX_ listsv,
2740 "+utf8::%.*s\n", n, PL_regcomp_parse);
2742 Perl_sv_catpvf(aTHX_ listsv,
2743 "!utf8::%.*s\n", n, PL_regcomp_parse);
2745 PL_regcomp_parse = e + 1;
2746 lastvalue = OOB_UTF8;
2748 case 'n': value = '\n'; break;
2749 case 'r': value = '\r'; break;
2750 case 't': value = '\t'; break;
2751 case 'f': value = '\f'; break;
2752 case 'b': value = '\b'; break;
2753 case 'e': value = '\033'; break;
2754 case 'a': value = '\007'; break;
2756 if (*PL_regcomp_parse == '{') {
2757 e = strchr(PL_regcomp_parse++, '}');
2759 FAIL("Missing right brace on \\x{}");
2760 value = scan_hex(PL_regcomp_parse,
2761 e - PL_regcomp_parse,
2763 PL_regcomp_parse = e + 1;
2766 value = scan_hex(PL_regcomp_parse, 2, &numlen);
2767 PL_regcomp_parse += numlen;
2771 value = UCHARAT(PL_regcomp_parse++);
2772 value = toCTRL(value);
2774 case '0': case '1': case '2': case '3': case '4':
2775 case '5': case '6': case '7': case '8': case '9':
2776 value = scan_oct(--PL_regcomp_parse, 3, &numlen);
2777 PL_regcomp_parse += numlen;
2781 if (!SIZE_ONLY && namedclass > OOB_NAMEDCLASS) {
2783 FAIL("invalid [] range in regexp"); /* [a-\w], [a-[:word:]] */
2784 switch (namedclass) {
2786 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsWord\n"); break;
2788 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsWord\n"); break;
2790 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsAlnum\n"); break;
2792 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsAlnum\n"); break;
2794 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsAlpha\n"); break;
2796 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsAlpha\n"); break;
2798 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsASCII\n"); break;
2800 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsASCII\n"); break;
2802 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsCntrl\n"); break;
2804 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsCntrl\n"); break;
2806 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsGraph\n"); break;
2808 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsGraph\n"); break;
2810 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsDigit\n"); break;
2812 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsDigit\n"); break;
2814 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsLower\n"); break;
2816 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsLower\n"); break;
2818 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsPrint\n"); break;
2820 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsPrint\n"); break;
2822 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsPunct\n"); break;
2824 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsPunct\n"); break;
2826 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsSpace\n"); break;
2828 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsSpace\n"); break;
2830 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsUpper\n"); break;
2832 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsUpper\n"); break;
2834 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsXDigit\n"); break;
2836 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsXDigit\n"); break;
2841 if (lastvalue > value)
2842 FAIL("invalid [] range in regexp"); /* [b-a] */
2845 Perl_sv_catpvf(aTHX_ listsv, "%04" PERL_PRIx64 "\t%04" PERL_PRIx64 "\n", (UV)lastvalue, (UV)value);
2848 Perl_sv_catpvf(aTHX_ listsv, "%04x\t%04x\n", lastvalue, value);
2854 if (*PL_regcomp_parse == '-' && PL_regcomp_parse+1 < PL_regxend &&
2855 PL_regcomp_parse[1] != ']') {
2856 if (namedclass > OOB_NAMEDCLASS)
2857 FAIL("invalid [] range in regexp"); /* [\w-a] */
2860 continue; /* do it next time */
2863 /* now is the next time */
2866 Perl_sv_catpvf(aTHX_ listsv, "%04" PERL_PRIx64 "\n", (UV)value);
2869 Perl_sv_catpvf(aTHX_ listsv, "%04x\n", value);
2874 ret = reganode(ANYOFUTF8, 0);
2877 SV *rv = swash_init("utf8", "", listsv, 1, 0);
2878 SvREFCNT_dec(listsv);
2879 n = add_data(1,"s");
2880 PL_regcomp_rx->data->data[n] = (void*)rv;
2881 ARG1_SET(ret, flags);
2892 char* retval = PL_regcomp_parse++;
2895 if (*PL_regcomp_parse == '(' && PL_regcomp_parse[1] == '?' &&
2896 PL_regcomp_parse[2] == '#') {
2897 while (*PL_regcomp_parse && *PL_regcomp_parse != ')')
2902 if (PL_regflags & PMf_EXTENDED) {
2903 if (isSPACE(*PL_regcomp_parse)) {
2907 else if (*PL_regcomp_parse == '#') {
2908 while (*PL_regcomp_parse && *PL_regcomp_parse != '\n')
2919 - reg_node - emit a node
2921 STATIC regnode * /* Location. */
2922 S_reg_node(pTHX_ U8 op)
2925 register regnode *ret;
2926 register regnode *ptr;
2930 SIZE_ALIGN(PL_regsize);
2935 NODE_ALIGN_FILL(ret);
2937 FILL_ADVANCE_NODE(ptr, op);
2944 - reganode - emit a node with an argument
2946 STATIC regnode * /* Location. */
2947 S_reganode(pTHX_ U8 op, U32 arg)
2950 register regnode *ret;
2951 register regnode *ptr;
2955 SIZE_ALIGN(PL_regsize);
2960 NODE_ALIGN_FILL(ret);
2962 FILL_ADVANCE_NODE_ARG(ptr, op, arg);
2969 - reguni - emit (if appropriate) a Unicode character
2972 S_reguni(pTHX_ UV uv, char* s, I32* lenp)
2977 *lenp = uv_to_utf8(tmpbuf, uv) - tmpbuf;
2980 *lenp = uv_to_utf8((U8*)s, uv) - (U8*)s;
2985 - reginsert - insert an operator in front of already-emitted operand
2987 * Means relocating the operand.
2990 S_reginsert(pTHX_ U8 op, regnode *opnd)
2993 register regnode *src;
2994 register regnode *dst;
2995 register regnode *place;
2996 register int offset = regarglen[(U8)op];
2998 /* (PL_regkind[(U8)op] == CURLY ? EXTRA_STEP_2ARGS : 0); */
3001 PL_regsize += NODE_STEP_REGNODE + offset;
3006 PL_regcode += NODE_STEP_REGNODE + offset;
3009 StructCopy(--src, --dst, regnode);
3011 place = opnd; /* Op node, where operand used to be. */
3012 src = NEXTOPER(place);
3013 FILL_ADVANCE_NODE(place, op);
3014 Zero(src, offset, regnode);
3018 - regtail - set the next-pointer at the end of a node chain of p to val.
3021 S_regtail(pTHX_ regnode *p, regnode *val)
3024 register regnode *scan;
3025 register regnode *temp;
3026 register I32 offset;
3031 /* Find last node. */
3034 temp = regnext(scan);
3040 if (reg_off_by_arg[OP(scan)]) {
3041 ARG_SET(scan, val - scan);
3044 NEXT_OFF(scan) = val - scan;
3049 - regoptail - regtail on operand of first argument; nop if operandless
3052 S_regoptail(pTHX_ regnode *p, regnode *val)
3055 /* "Operandless" and "op != BRANCH" are synonymous in practice. */
3056 if (p == NULL || SIZE_ONLY)
3058 if (PL_regkind[(U8)OP(p)] == BRANCH) {
3059 regtail(NEXTOPER(p), val);
3061 else if ( PL_regkind[(U8)OP(p)] == BRANCHJ) {
3062 regtail(NEXTOPER(NEXTOPER(p)), val);
3069 - regcurly - a little FSA that accepts {\d+,?\d*}
3072 S_regcurly(pTHX_ register char *s)
3091 S_dumpuntil(pTHX_ regnode *start, regnode *node, regnode *last, SV* sv, I32 l)
3094 register U8 op = EXACT; /* Arbitrary non-END op. */
3095 register regnode *next, *onode;
3097 while (op != END && (!last || node < last)) {
3098 /* While that wasn't END last time... */
3104 next = regnext(node);
3106 if (OP(node) == OPTIMIZED)
3109 PerlIO_printf(Perl_debug_log, "%4d:%*s%s", node - start,
3110 2*l + 1, "", SvPVX(sv));
3111 if (next == NULL) /* Next ptr. */
3112 PerlIO_printf(Perl_debug_log, "(0)");
3114 PerlIO_printf(Perl_debug_log, "(%d)", next - start);
3115 (void)PerlIO_putc(Perl_debug_log, '\n');
3117 if (PL_regkind[(U8)op] == BRANCHJ) {
3118 register regnode *nnode = (OP(next) == LONGJMP
3121 if (last && nnode > last)
3123 node = dumpuntil(start, NEXTOPER(NEXTOPER(node)), nnode, sv, l + 1);
3125 else if (PL_regkind[(U8)op] == BRANCH) {
3126 node = dumpuntil(start, NEXTOPER(node), next, sv, l + 1);
3128 else if ( op == CURLY) { /* `next' might be very big: optimizer */
3129 node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
3130 NEXTOPER(node) + EXTRA_STEP_2ARGS + 1, sv, l + 1);
3132 else if (PL_regkind[(U8)op] == CURLY && op != CURLYX) {
3133 node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
3136 else if ( op == PLUS || op == STAR) {
3137 node = dumpuntil(start, NEXTOPER(node), NEXTOPER(node) + 1, sv, l + 1);
3139 else if (op == ANYOF) {
3140 node = NEXTOPER(node);
3143 else if (PL_regkind[(U8)op] == EXACT) {
3144 /* Literal string, where present. */
3145 node += NODE_SZ_STR(node) - 1;
3146 node = NEXTOPER(node);
3149 node = NEXTOPER(node);
3150 node += regarglen[(U8)op];
3152 if (op == CURLYX || op == OPEN)
3154 else if (op == WHILEM)
3157 #endif /* DEBUGGING */
3162 - regdump - dump a regexp onto Perl_debug_log in vaguely comprehensible form
3165 Perl_regdump(pTHX_ regexp *r)
3169 SV *sv = sv_newmortal();
3171 (void)dumpuntil(r->program, r->program + 1, NULL, sv, 0);
3173 /* Header fields of interest. */
3174 if (r->anchored_substr)
3175 PerlIO_printf(Perl_debug_log, "anchored `%s%.*s%s'%s at %d ",
3177 SvCUR(r->anchored_substr) - (SvTAIL(r->anchored_substr)!=0),
3178 SvPVX(r->anchored_substr),
3180 SvTAIL(r->anchored_substr) ? "$" : "",
3181 r->anchored_offset);
3182 if (r->float_substr)
3183 PerlIO_printf(Perl_debug_log, "floating `%s%.*s%s'%s at %d..%u ",
3185 SvCUR(r->float_substr) - (SvTAIL(r->float_substr)!=0),
3186 SvPVX(r->float_substr),
3188 SvTAIL(r->float_substr) ? "$" : "",
3189 r->float_min_offset, r->float_max_offset);
3190 if (r->check_substr)
3191 PerlIO_printf(Perl_debug_log,
3192 r->check_substr == r->float_substr
3193 ? "(checking floating" : "(checking anchored");
3194 if (r->reganch & ROPT_NOSCAN)
3195 PerlIO_printf(Perl_debug_log, " noscan");
3196 if (r->reganch & ROPT_CHECK_ALL)
3197 PerlIO_printf(Perl_debug_log, " isall");
3198 if (r->check_substr)
3199 PerlIO_printf(Perl_debug_log, ") ");
3201 if (r->regstclass) {
3202 regprop(sv, r->regstclass);
3203 PerlIO_printf(Perl_debug_log, "stclass `%s' ", SvPVX(sv));
3205 if (r->reganch & ROPT_ANCH) {
3206 PerlIO_printf(Perl_debug_log, "anchored");
3207 if (r->reganch & ROPT_ANCH_BOL)
3208 PerlIO_printf(Perl_debug_log, "(BOL)");
3209 if (r->reganch & ROPT_ANCH_MBOL)
3210 PerlIO_printf(Perl_debug_log, "(MBOL)");
3211 if (r->reganch & ROPT_ANCH_SBOL)
3212 PerlIO_printf(Perl_debug_log, "(SBOL)");
3213 if (r->reganch & ROPT_ANCH_GPOS)
3214 PerlIO_printf(Perl_debug_log, "(GPOS)");
3215 PerlIO_putc(Perl_debug_log, ' ');
3217 if (r->reganch & ROPT_GPOS_SEEN)
3218 PerlIO_printf(Perl_debug_log, "GPOS ");
3219 if (r->reganch & ROPT_SKIP)
3220 PerlIO_printf(Perl_debug_log, "plus ");
3221 if (r->reganch & ROPT_IMPLICIT)
3222 PerlIO_printf(Perl_debug_log, "implicit ");
3223 PerlIO_printf(Perl_debug_log, "minlen %ld ", (long) r->minlen);
3224 if (r->reganch & ROPT_EVAL_SEEN)
3225 PerlIO_printf(Perl_debug_log, "with eval ");
3226 PerlIO_printf(Perl_debug_log, "\n");
3227 #endif /* DEBUGGING */
3231 - regprop - printable representation of opcode
3234 Perl_regprop(pTHX_ SV *sv, regnode *o)
3240 sv_setpvn(sv, "", 0);
3241 if (OP(o) >= reg_num) /* regnode.type is unsigned */
3242 FAIL("corrupted regexp opcode");
3243 sv_catpv(sv, (char*)reg_name[OP(o)]); /* Take off const! */
3245 k = PL_regkind[(U8)OP(o)];
3248 Perl_sv_catpvf(aTHX_ sv, " <%s%.*s%s>", PL_colors[0],
3249 STR_LEN(o), STRING(o), PL_colors[1]);
3250 else if (k == CURLY) {
3251 if (OP(o) == CURLYM || OP(o) == CURLYN)
3252 Perl_sv_catpvf(aTHX_ sv, "[%d]", o->flags); /* Parenth number */
3253 Perl_sv_catpvf(aTHX_ sv, " {%d,%d}", ARG1(o), ARG2(o));
3255 else if (k == WHILEM && o->flags) /* Ordinal/of */
3256 Perl_sv_catpvf(aTHX_ sv, "[%d/%d]", o->flags & 0xf, o->flags>>4);
3257 else if (k == REF || k == OPEN || k == CLOSE || k == GROUPP )
3258 Perl_sv_catpvf(aTHX_ sv, "%d", ARG(o)); /* Parenth number */
3259 else if (k == LOGICAL)
3260 Perl_sv_catpvf(aTHX_ sv, "[%d]", o->flags); /* 2: embedded, otherwise 1 */
3261 else if (k == BRANCHJ && (OP(o) == UNLESSM || OP(o) == IFMATCH))
3262 Perl_sv_catpvf(aTHX_ sv, "[-%d]", o->flags);
3263 #endif /* DEBUGGING */
3267 Perl_re_intuit_string(pTHX_ regexp *prog)
3268 { /* Assume that RE_INTUIT is set */
3271 char *s = SvPV(prog->check_substr,n_a);
3273 if (!PL_colorset) reginitcolors();
3274 PerlIO_printf(Perl_debug_log,
3275 "%sUsing REx substr:%s `%s%.60s%s%s'\n",
3276 PL_colors[4],PL_colors[5],PL_colors[0],
3279 (strlen(s) > 60 ? "..." : ""));
3282 return prog->check_substr;
3286 Perl_pregfree(pTHX_ struct regexp *r)
3289 DEBUG_r(if (!PL_colorset) reginitcolors());
3291 if (!r || (--r->refcnt > 0))
3293 DEBUG_r(PerlIO_printf(Perl_debug_log,
3294 "%sFreeing REx:%s `%s%.60s%s%s'\n",
3295 PL_colors[4],PL_colors[5],PL_colors[0],
3298 (strlen(r->precomp) > 60 ? "..." : "")));
3301 Safefree(r->precomp);
3302 if (RX_MATCH_COPIED(r))
3303 Safefree(r->subbeg);
3305 if (r->anchored_substr)
3306 SvREFCNT_dec(r->anchored_substr);
3307 if (r->float_substr)
3308 SvREFCNT_dec(r->float_substr);
3309 Safefree(r->substrs);
3312 int n = r->data->count;
3313 AV* new_comppad = NULL;
3318 switch (r->data->what[n]) {
3320 SvREFCNT_dec((SV*)r->data->data[n]);
3323 new_comppad = (AV*)r->data->data[n];
3326 if (new_comppad == NULL)
3327 Perl_croak(aTHX_ "panic: pregfree comppad");
3328 old_comppad = PL_comppad;
3329 old_curpad = PL_curpad;
3330 PL_comppad = new_comppad;
3331 PL_curpad = AvARRAY(new_comppad);
3332 op_free((OP_4tree*)r->data->data[n]);
3333 PL_comppad = old_comppad;
3334 PL_curpad = old_curpad;
3335 SvREFCNT_dec((SV*)new_comppad);
3341 FAIL2("panic: regfree data code '%c'", r->data->what[n]);
3344 Safefree(r->data->what);
3347 Safefree(r->startp);
3353 - regnext - dig the "next" pointer out of a node
3355 * [Note, when REGALIGN is defined there are two places in regmatch()
3356 * that bypass this code for speed.]
3359 Perl_regnext(pTHX_ register regnode *p)
3362 register I32 offset;
3364 if (p == &PL_regdummy)
3367 offset = (reg_off_by_arg[OP(p)] ? ARG(p) : NEXT_OFF(p));
3375 S_re_croak2(pTHX_ const char* pat1,const char* pat2,...)
3378 STRLEN l1 = strlen(pat1);
3379 STRLEN l2 = strlen(pat2);
3388 Copy(pat1, buf, l1 , char);
3389 Copy(pat2, buf + l1, l2 , char);
3390 buf[l1 + l2] = '\n';
3391 buf[l1 + l2 + 1] = '\0';
3393 /* ANSI variant takes additional second argument */
3394 va_start(args, pat2);
3398 msv = vmess(buf, &args);
3400 message = SvPV(msv,l1);
3403 Copy(message, buf, l1 , char);
3404 buf[l1] = '\0'; /* Overwrite \n */
3405 Perl_croak(aTHX_ "%s", buf);
3408 /* XXX Here's a total kludge. But we need to re-enter for swash routines. */
3411 Perl_save_re_context(pTHX)
3415 SAVEPPTR(PL_regprecomp); /* uncompiled string. */
3416 SAVEI32(PL_regnpar); /* () count. */
3417 SAVEI32(PL_regsize); /* Code size. */
3418 SAVEI16(PL_regflags); /* are we folding, multilining? */
3419 SAVEPPTR(PL_reginput); /* String-input pointer. */
3420 SAVEPPTR(PL_regbol); /* Beginning of input, for ^ check. */
3421 SAVEPPTR(PL_regeol); /* End of input, for $ check. */
3422 SAVESPTR(PL_regstartp); /* Pointer to startp array. */
3423 SAVESPTR(PL_regendp); /* Ditto for endp. */
3424 SAVESPTR(PL_reglastparen); /* Similarly for lastparen. */
3425 SAVEPPTR(PL_regtill); /* How far we are required to go. */
3426 SAVEI32(PL_regprev); /* char before regbol, \n if none */
3427 SAVESPTR(PL_reg_start_tmp); /* from regexec.c */
3428 PL_reg_start_tmp = 0;
3429 SAVEFREEPV(PL_reg_start_tmp);
3430 SAVEI32(PL_reg_start_tmpl); /* from regexec.c */
3431 PL_reg_start_tmpl = 0;
3432 SAVESPTR(PL_regdata);
3433 SAVEI32(PL_reg_flags); /* from regexec.c */
3434 SAVEI32(PL_reg_eval_set); /* from regexec.c */
3435 SAVEI32(PL_regnarrate); /* from regexec.c */
3436 SAVESPTR(PL_regprogram); /* from regexec.c */
3437 SAVEINT(PL_regindent); /* from regexec.c */
3438 SAVESPTR(PL_regcc); /* from regexec.c */
3439 SAVESPTR(PL_curcop);
3440 SAVESPTR(PL_regcomp_rx); /* from regcomp.c */
3441 SAVEI32(PL_regseen); /* from regcomp.c */
3442 SAVEI32(PL_regsawback); /* Did we see \1, ...? */
3443 SAVEI32(PL_regnaughty); /* How bad is this pattern? */
3444 SAVESPTR(PL_regcode); /* Code-emit pointer; ®dummy = don't */
3445 SAVEPPTR(PL_regxend); /* End of input for compile */
3446 SAVEPPTR(PL_regcomp_parse); /* Input-scan pointer. */
3447 SAVESPTR(PL_reg_call_cc); /* from regexec.c */
3448 SAVESPTR(PL_reg_re); /* from regexec.c */
3449 SAVEPPTR(PL_reg_ganch); /* from regexec.c */
3450 SAVESPTR(PL_reg_sv); /* from regexec.c */
3451 SAVESPTR(PL_reg_magic); /* from regexec.c */
3452 SAVEI32(PL_reg_oldpos); /* from regexec.c */
3453 SAVESPTR(PL_reg_oldcurpm); /* from regexec.c */
3454 SAVESPTR(PL_reg_curpm); /* from regexec.c */
3456 SAVEPPTR(PL_reg_starttry); /* from regexec.c */
3468 clear_re(pTHXo_ void *r)
3470 ReREFCNT_dec((regexp *)r);