10Sstevel@tonic-gate /* regcomp.c
20Sstevel@tonic-gate */
30Sstevel@tonic-gate
40Sstevel@tonic-gate /*
50Sstevel@tonic-gate * "A fair jaw-cracker dwarf-language must be." --Samwise Gamgee
60Sstevel@tonic-gate */
70Sstevel@tonic-gate
80Sstevel@tonic-gate /* NOTE: this is derived from Henry Spencer's regexp code, and should not
90Sstevel@tonic-gate * confused with the original package (see point 3 below). Thanks, Henry!
100Sstevel@tonic-gate */
110Sstevel@tonic-gate
120Sstevel@tonic-gate /* Additional note: this code is very heavily munged from Henry's version
130Sstevel@tonic-gate * in places. In some spots I've traded clarity for efficiency, so don't
140Sstevel@tonic-gate * blame Henry for some of the lack of readability.
150Sstevel@tonic-gate */
160Sstevel@tonic-gate
170Sstevel@tonic-gate /* The names of the functions have been changed from regcomp and
180Sstevel@tonic-gate * regexec to pregcomp and pregexec in order to avoid conflicts
190Sstevel@tonic-gate * with the POSIX routines of the same names.
200Sstevel@tonic-gate */
210Sstevel@tonic-gate
220Sstevel@tonic-gate #ifdef PERL_EXT_RE_BUILD
230Sstevel@tonic-gate /* need to replace pregcomp et al, so enable that */
240Sstevel@tonic-gate # ifndef PERL_IN_XSUB_RE
250Sstevel@tonic-gate # define PERL_IN_XSUB_RE
260Sstevel@tonic-gate # endif
270Sstevel@tonic-gate /* need access to debugger hooks */
280Sstevel@tonic-gate # if defined(PERL_EXT_RE_DEBUG) && !defined(DEBUGGING)
290Sstevel@tonic-gate # define DEBUGGING
300Sstevel@tonic-gate # endif
310Sstevel@tonic-gate #endif
320Sstevel@tonic-gate
330Sstevel@tonic-gate #ifdef PERL_IN_XSUB_RE
340Sstevel@tonic-gate /* We *really* need to overwrite these symbols: */
350Sstevel@tonic-gate # define Perl_pregcomp my_regcomp
360Sstevel@tonic-gate # define Perl_regdump my_regdump
370Sstevel@tonic-gate # define Perl_regprop my_regprop
380Sstevel@tonic-gate # define Perl_pregfree my_regfree
390Sstevel@tonic-gate # define Perl_re_intuit_string my_re_intuit_string
400Sstevel@tonic-gate /* *These* symbols are masked to allow static link. */
410Sstevel@tonic-gate # define Perl_regnext my_regnext
420Sstevel@tonic-gate # define Perl_save_re_context my_save_re_context
430Sstevel@tonic-gate # define Perl_reginitcolors my_reginitcolors
440Sstevel@tonic-gate
450Sstevel@tonic-gate # define PERL_NO_GET_CONTEXT
460Sstevel@tonic-gate #endif
470Sstevel@tonic-gate
480Sstevel@tonic-gate /*SUPPRESS 112*/
490Sstevel@tonic-gate /*
500Sstevel@tonic-gate * pregcomp and pregexec -- regsub and regerror are not used in perl
510Sstevel@tonic-gate *
520Sstevel@tonic-gate * Copyright (c) 1986 by University of Toronto.
530Sstevel@tonic-gate * Written by Henry Spencer. Not derived from licensed software.
540Sstevel@tonic-gate *
550Sstevel@tonic-gate * Permission is granted to anyone to use this software for any
560Sstevel@tonic-gate * purpose on any computer system, and to redistribute it freely,
570Sstevel@tonic-gate * subject to the following restrictions:
580Sstevel@tonic-gate *
590Sstevel@tonic-gate * 1. The author is not responsible for the consequences of use of
600Sstevel@tonic-gate * this software, no matter how awful, even if they arise
610Sstevel@tonic-gate * from defects in it.
620Sstevel@tonic-gate *
630Sstevel@tonic-gate * 2. The origin of this software must not be misrepresented, either
640Sstevel@tonic-gate * by explicit claim or by omission.
650Sstevel@tonic-gate *
660Sstevel@tonic-gate * 3. Altered versions must be plainly marked as such, and must not
670Sstevel@tonic-gate * be misrepresented as being the original software.
680Sstevel@tonic-gate *
690Sstevel@tonic-gate *
700Sstevel@tonic-gate **** Alterations to Henry's code are...
710Sstevel@tonic-gate ****
720Sstevel@tonic-gate **** Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
730Sstevel@tonic-gate **** 2000, 2001, 2002, 2003, by Larry Wall and others
740Sstevel@tonic-gate ****
750Sstevel@tonic-gate **** You may distribute under the terms of either the GNU General Public
760Sstevel@tonic-gate **** License or the Artistic License, as specified in the README file.
770Sstevel@tonic-gate
780Sstevel@tonic-gate *
790Sstevel@tonic-gate * Beware that some of this code is subtly aware of the way operator
800Sstevel@tonic-gate * precedence is structured in regular expressions. Serious changes in
810Sstevel@tonic-gate * regular-expression syntax might require a total rethink.
820Sstevel@tonic-gate */
830Sstevel@tonic-gate #include "EXTERN.h"
840Sstevel@tonic-gate #define PERL_IN_REGCOMP_C
850Sstevel@tonic-gate #include "perl.h"
860Sstevel@tonic-gate
870Sstevel@tonic-gate #ifndef PERL_IN_XSUB_RE
880Sstevel@tonic-gate # include "INTERN.h"
890Sstevel@tonic-gate #endif
900Sstevel@tonic-gate
910Sstevel@tonic-gate #define REG_COMP_C
920Sstevel@tonic-gate #include "regcomp.h"
930Sstevel@tonic-gate
940Sstevel@tonic-gate #ifdef op
950Sstevel@tonic-gate #undef op
960Sstevel@tonic-gate #endif /* op */
970Sstevel@tonic-gate
980Sstevel@tonic-gate #ifdef MSDOS
990Sstevel@tonic-gate # if defined(BUGGY_MSC6)
1000Sstevel@tonic-gate /* MSC 6.00A breaks on op/regexp.t test 85 unless we turn this off */
1010Sstevel@tonic-gate # pragma optimize("a",off)
1020Sstevel@tonic-gate /* But MSC 6.00A is happy with 'w', for aliases only across function calls*/
1030Sstevel@tonic-gate # pragma optimize("w",on )
1040Sstevel@tonic-gate # endif /* BUGGY_MSC6 */
1050Sstevel@tonic-gate #endif /* MSDOS */
1060Sstevel@tonic-gate
1070Sstevel@tonic-gate #ifndef STATIC
1080Sstevel@tonic-gate #define STATIC static
1090Sstevel@tonic-gate #endif
1100Sstevel@tonic-gate
1110Sstevel@tonic-gate typedef struct RExC_state_t {
1120Sstevel@tonic-gate U32 flags; /* are we folding, multilining? */
1130Sstevel@tonic-gate char *precomp; /* uncompiled string. */
1140Sstevel@tonic-gate regexp *rx;
1150Sstevel@tonic-gate char *start; /* Start of input for compile */
1160Sstevel@tonic-gate char *end; /* End of input for compile */
1170Sstevel@tonic-gate char *parse; /* Input-scan pointer. */
1180Sstevel@tonic-gate I32 whilem_seen; /* number of WHILEM in this expr */
1190Sstevel@tonic-gate regnode *emit_start; /* Start of emitted-code area */
1200Sstevel@tonic-gate regnode *emit; /* Code-emit pointer; ®dummy = don't = compiling */
1210Sstevel@tonic-gate I32 naughty; /* How bad is this pattern? */
1220Sstevel@tonic-gate I32 sawback; /* Did we see \1, ...? */
1230Sstevel@tonic-gate U32 seen;
1240Sstevel@tonic-gate I32 size; /* Code size. */
1250Sstevel@tonic-gate I32 npar; /* () count. */
1260Sstevel@tonic-gate I32 extralen;
1270Sstevel@tonic-gate I32 seen_zerolen;
1280Sstevel@tonic-gate I32 seen_evals;
129*6696Svm156888 I32 utf8; /* whether the pattern is utf8 or not */
130*6696Svm156888 I32 orig_utf8; /* whether the pattern was originally in utf8 */
131*6696Svm156888 /* XXX use this for future optimisation of case
132*6696Svm156888 * where pattern must be upgraded to utf8. */
1330Sstevel@tonic-gate #if ADD_TO_REGEXEC
1340Sstevel@tonic-gate char *starttry; /* -Dr: where regtry was called. */
1350Sstevel@tonic-gate #define RExC_starttry (pRExC_state->starttry)
1360Sstevel@tonic-gate #endif
1370Sstevel@tonic-gate } RExC_state_t;
1380Sstevel@tonic-gate
1390Sstevel@tonic-gate #define RExC_flags (pRExC_state->flags)
1400Sstevel@tonic-gate #define RExC_precomp (pRExC_state->precomp)
1410Sstevel@tonic-gate #define RExC_rx (pRExC_state->rx)
1420Sstevel@tonic-gate #define RExC_start (pRExC_state->start)
1430Sstevel@tonic-gate #define RExC_end (pRExC_state->end)
1440Sstevel@tonic-gate #define RExC_parse (pRExC_state->parse)
1450Sstevel@tonic-gate #define RExC_whilem_seen (pRExC_state->whilem_seen)
1460Sstevel@tonic-gate #define RExC_offsets (pRExC_state->rx->offsets) /* I am not like the others */
1470Sstevel@tonic-gate #define RExC_emit (pRExC_state->emit)
1480Sstevel@tonic-gate #define RExC_emit_start (pRExC_state->emit_start)
1490Sstevel@tonic-gate #define RExC_naughty (pRExC_state->naughty)
1500Sstevel@tonic-gate #define RExC_sawback (pRExC_state->sawback)
1510Sstevel@tonic-gate #define RExC_seen (pRExC_state->seen)
1520Sstevel@tonic-gate #define RExC_size (pRExC_state->size)
1530Sstevel@tonic-gate #define RExC_npar (pRExC_state->npar)
1540Sstevel@tonic-gate #define RExC_extralen (pRExC_state->extralen)
1550Sstevel@tonic-gate #define RExC_seen_zerolen (pRExC_state->seen_zerolen)
1560Sstevel@tonic-gate #define RExC_seen_evals (pRExC_state->seen_evals)
1570Sstevel@tonic-gate #define RExC_utf8 (pRExC_state->utf8)
158*6696Svm156888 #define RExC_orig_utf8 (pRExC_state->orig_utf8)
1590Sstevel@tonic-gate
1600Sstevel@tonic-gate #define ISMULT1(c) ((c) == '*' || (c) == '+' || (c) == '?')
1610Sstevel@tonic-gate #define ISMULT2(s) ((*s) == '*' || (*s) == '+' || (*s) == '?' || \
1620Sstevel@tonic-gate ((*s) == '{' && regcurly(s)))
1630Sstevel@tonic-gate
1640Sstevel@tonic-gate #ifdef SPSTART
1650Sstevel@tonic-gate #undef SPSTART /* dratted cpp namespace... */
1660Sstevel@tonic-gate #endif
1670Sstevel@tonic-gate /*
1680Sstevel@tonic-gate * Flags to be passed up and down.
1690Sstevel@tonic-gate */
1700Sstevel@tonic-gate #define WORST 0 /* Worst case. */
1710Sstevel@tonic-gate #define HASWIDTH 0x1 /* Known to match non-null strings. */
1720Sstevel@tonic-gate #define SIMPLE 0x2 /* Simple enough to be STAR/PLUS operand. */
1730Sstevel@tonic-gate #define SPSTART 0x4 /* Starts with * or +. */
1740Sstevel@tonic-gate #define TRYAGAIN 0x8 /* Weeded out a declaration. */
1750Sstevel@tonic-gate
1760Sstevel@tonic-gate /* Length of a variant. */
1770Sstevel@tonic-gate
1780Sstevel@tonic-gate typedef struct scan_data_t {
1790Sstevel@tonic-gate I32 len_min;
1800Sstevel@tonic-gate I32 len_delta;
1810Sstevel@tonic-gate I32 pos_min;
1820Sstevel@tonic-gate I32 pos_delta;
1830Sstevel@tonic-gate SV *last_found;
1840Sstevel@tonic-gate I32 last_end; /* min value, <0 unless valid. */
1850Sstevel@tonic-gate I32 last_start_min;
1860Sstevel@tonic-gate I32 last_start_max;
1870Sstevel@tonic-gate SV **longest; /* Either &l_fixed, or &l_float. */
1880Sstevel@tonic-gate SV *longest_fixed;
1890Sstevel@tonic-gate I32 offset_fixed;
1900Sstevel@tonic-gate SV *longest_float;
1910Sstevel@tonic-gate I32 offset_float_min;
1920Sstevel@tonic-gate I32 offset_float_max;
1930Sstevel@tonic-gate I32 flags;
1940Sstevel@tonic-gate I32 whilem_c;
1950Sstevel@tonic-gate I32 *last_closep;
1960Sstevel@tonic-gate struct regnode_charclass_class *start_class;
1970Sstevel@tonic-gate } scan_data_t;
1980Sstevel@tonic-gate
1990Sstevel@tonic-gate /*
2000Sstevel@tonic-gate * Forward declarations for pregcomp()'s friends.
2010Sstevel@tonic-gate */
2020Sstevel@tonic-gate
2030Sstevel@tonic-gate static scan_data_t zero_scan_data = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2040Sstevel@tonic-gate 0, 0, 0, 0, 0, 0};
2050Sstevel@tonic-gate
2060Sstevel@tonic-gate #define SF_BEFORE_EOL (SF_BEFORE_SEOL|SF_BEFORE_MEOL)
2070Sstevel@tonic-gate #define SF_BEFORE_SEOL 0x1
2080Sstevel@tonic-gate #define SF_BEFORE_MEOL 0x2
2090Sstevel@tonic-gate #define SF_FIX_BEFORE_EOL (SF_FIX_BEFORE_SEOL|SF_FIX_BEFORE_MEOL)
2100Sstevel@tonic-gate #define SF_FL_BEFORE_EOL (SF_FL_BEFORE_SEOL|SF_FL_BEFORE_MEOL)
2110Sstevel@tonic-gate
2120Sstevel@tonic-gate #ifdef NO_UNARY_PLUS
2130Sstevel@tonic-gate # define SF_FIX_SHIFT_EOL (0+2)
2140Sstevel@tonic-gate # define SF_FL_SHIFT_EOL (0+4)
2150Sstevel@tonic-gate #else
2160Sstevel@tonic-gate # define SF_FIX_SHIFT_EOL (+2)
2170Sstevel@tonic-gate # define SF_FL_SHIFT_EOL (+4)
2180Sstevel@tonic-gate #endif
2190Sstevel@tonic-gate
2200Sstevel@tonic-gate #define SF_FIX_BEFORE_SEOL (SF_BEFORE_SEOL << SF_FIX_SHIFT_EOL)
2210Sstevel@tonic-gate #define SF_FIX_BEFORE_MEOL (SF_BEFORE_MEOL << SF_FIX_SHIFT_EOL)
2220Sstevel@tonic-gate
2230Sstevel@tonic-gate #define SF_FL_BEFORE_SEOL (SF_BEFORE_SEOL << SF_FL_SHIFT_EOL)
2240Sstevel@tonic-gate #define SF_FL_BEFORE_MEOL (SF_BEFORE_MEOL << SF_FL_SHIFT_EOL) /* 0x20 */
2250Sstevel@tonic-gate #define SF_IS_INF 0x40
2260Sstevel@tonic-gate #define SF_HAS_PAR 0x80
2270Sstevel@tonic-gate #define SF_IN_PAR 0x100
2280Sstevel@tonic-gate #define SF_HAS_EVAL 0x200
2290Sstevel@tonic-gate #define SCF_DO_SUBSTR 0x400
2300Sstevel@tonic-gate #define SCF_DO_STCLASS_AND 0x0800
2310Sstevel@tonic-gate #define SCF_DO_STCLASS_OR 0x1000
2320Sstevel@tonic-gate #define SCF_DO_STCLASS (SCF_DO_STCLASS_AND|SCF_DO_STCLASS_OR)
2330Sstevel@tonic-gate #define SCF_WHILEM_VISITED_POS 0x2000
2340Sstevel@tonic-gate
2350Sstevel@tonic-gate #define UTF (RExC_utf8 != 0)
2360Sstevel@tonic-gate #define LOC ((RExC_flags & PMf_LOCALE) != 0)
2370Sstevel@tonic-gate #define FOLD ((RExC_flags & PMf_FOLD) != 0)
2380Sstevel@tonic-gate
2390Sstevel@tonic-gate #define OOB_UNICODE 12345678
2400Sstevel@tonic-gate #define OOB_NAMEDCLASS -1
2410Sstevel@tonic-gate
2420Sstevel@tonic-gate #define CHR_SVLEN(sv) (UTF ? sv_len_utf8(sv) : SvCUR(sv))
2430Sstevel@tonic-gate #define CHR_DIST(a,b) (UTF ? utf8_distance(a,b) : a - b)
2440Sstevel@tonic-gate
2450Sstevel@tonic-gate
2460Sstevel@tonic-gate /* length of regex to show in messages that don't mark a position within */
2470Sstevel@tonic-gate #define RegexLengthToShowInErrorMessages 127
2480Sstevel@tonic-gate
2490Sstevel@tonic-gate /*
2500Sstevel@tonic-gate * If MARKER[12] are adjusted, be sure to adjust the constants at the top
2510Sstevel@tonic-gate * of t/op/regmesg.t, the tests in t/op/re_tests, and those in
2520Sstevel@tonic-gate * op/pragma/warn/regcomp.
2530Sstevel@tonic-gate */
2540Sstevel@tonic-gate #define MARKER1 "<-- HERE" /* marker as it appears in the description */
2550Sstevel@tonic-gate #define MARKER2 " <-- HERE " /* marker as it appears within the regex */
2560Sstevel@tonic-gate
2570Sstevel@tonic-gate #define REPORT_LOCATION " in regex; marked by " MARKER1 " in m/%.*s" MARKER2 "%s/"
2580Sstevel@tonic-gate
2590Sstevel@tonic-gate /*
2600Sstevel@tonic-gate * Calls SAVEDESTRUCTOR_X if needed, then calls Perl_croak with the given
2610Sstevel@tonic-gate * arg. Show regex, up to a maximum length. If it's too long, chop and add
2620Sstevel@tonic-gate * "...".
2630Sstevel@tonic-gate */
2640Sstevel@tonic-gate #define FAIL(msg) STMT_START { \
2650Sstevel@tonic-gate char *ellipses = ""; \
2660Sstevel@tonic-gate IV len = RExC_end - RExC_precomp; \
2670Sstevel@tonic-gate \
2680Sstevel@tonic-gate if (!SIZE_ONLY) \
2690Sstevel@tonic-gate SAVEDESTRUCTOR_X(clear_re,(void*)RExC_rx); \
2700Sstevel@tonic-gate if (len > RegexLengthToShowInErrorMessages) { \
2710Sstevel@tonic-gate /* chop 10 shorter than the max, to ensure meaning of "..." */ \
2720Sstevel@tonic-gate len = RegexLengthToShowInErrorMessages - 10; \
2730Sstevel@tonic-gate ellipses = "..."; \
2740Sstevel@tonic-gate } \
2750Sstevel@tonic-gate Perl_croak(aTHX_ "%s in regex m/%.*s%s/", \
2760Sstevel@tonic-gate msg, (int)len, RExC_precomp, ellipses); \
2770Sstevel@tonic-gate } STMT_END
2780Sstevel@tonic-gate
2790Sstevel@tonic-gate /*
2800Sstevel@tonic-gate * Calls SAVEDESTRUCTOR_X if needed, then calls Perl_croak with the given
2810Sstevel@tonic-gate * args. Show regex, up to a maximum length. If it's too long, chop and add
2820Sstevel@tonic-gate * "...".
2830Sstevel@tonic-gate */
2840Sstevel@tonic-gate #define FAIL2(pat,msg) STMT_START { \
2850Sstevel@tonic-gate char *ellipses = ""; \
2860Sstevel@tonic-gate IV len = RExC_end - RExC_precomp; \
2870Sstevel@tonic-gate \
2880Sstevel@tonic-gate if (!SIZE_ONLY) \
2890Sstevel@tonic-gate SAVEDESTRUCTOR_X(clear_re,(void*)RExC_rx); \
2900Sstevel@tonic-gate if (len > RegexLengthToShowInErrorMessages) { \
2910Sstevel@tonic-gate /* chop 10 shorter than the max, to ensure meaning of "..." */ \
2920Sstevel@tonic-gate len = RegexLengthToShowInErrorMessages - 10; \
2930Sstevel@tonic-gate ellipses = "..."; \
2940Sstevel@tonic-gate } \
2950Sstevel@tonic-gate S_re_croak2(aTHX_ pat, " in regex m/%.*s%s/", \
2960Sstevel@tonic-gate msg, (int)len, RExC_precomp, ellipses); \
2970Sstevel@tonic-gate } STMT_END
2980Sstevel@tonic-gate
2990Sstevel@tonic-gate
3000Sstevel@tonic-gate /*
3010Sstevel@tonic-gate * Simple_vFAIL -- like FAIL, but marks the current location in the scan
3020Sstevel@tonic-gate */
3030Sstevel@tonic-gate #define Simple_vFAIL(m) STMT_START { \
3040Sstevel@tonic-gate IV offset = RExC_parse - RExC_precomp; \
3050Sstevel@tonic-gate Perl_croak(aTHX_ "%s" REPORT_LOCATION, \
3060Sstevel@tonic-gate m, (int)offset, RExC_precomp, RExC_precomp + offset); \
3070Sstevel@tonic-gate } STMT_END
3080Sstevel@tonic-gate
3090Sstevel@tonic-gate /*
3100Sstevel@tonic-gate * Calls SAVEDESTRUCTOR_X if needed, then Simple_vFAIL()
3110Sstevel@tonic-gate */
3120Sstevel@tonic-gate #define vFAIL(m) STMT_START { \
3130Sstevel@tonic-gate if (!SIZE_ONLY) \
3140Sstevel@tonic-gate SAVEDESTRUCTOR_X(clear_re,(void*)RExC_rx); \
3150Sstevel@tonic-gate Simple_vFAIL(m); \
3160Sstevel@tonic-gate } STMT_END
3170Sstevel@tonic-gate
3180Sstevel@tonic-gate /*
3190Sstevel@tonic-gate * Like Simple_vFAIL(), but accepts two arguments.
3200Sstevel@tonic-gate */
3210Sstevel@tonic-gate #define Simple_vFAIL2(m,a1) STMT_START { \
3220Sstevel@tonic-gate IV offset = RExC_parse - RExC_precomp; \
3230Sstevel@tonic-gate S_re_croak2(aTHX_ m, REPORT_LOCATION, a1, \
3240Sstevel@tonic-gate (int)offset, RExC_precomp, RExC_precomp + offset); \
3250Sstevel@tonic-gate } STMT_END
3260Sstevel@tonic-gate
3270Sstevel@tonic-gate /*
3280Sstevel@tonic-gate * Calls SAVEDESTRUCTOR_X if needed, then Simple_vFAIL2().
3290Sstevel@tonic-gate */
3300Sstevel@tonic-gate #define vFAIL2(m,a1) STMT_START { \
3310Sstevel@tonic-gate if (!SIZE_ONLY) \
3320Sstevel@tonic-gate SAVEDESTRUCTOR_X(clear_re,(void*)RExC_rx); \
3330Sstevel@tonic-gate Simple_vFAIL2(m, a1); \
3340Sstevel@tonic-gate } STMT_END
3350Sstevel@tonic-gate
3360Sstevel@tonic-gate
3370Sstevel@tonic-gate /*
3380Sstevel@tonic-gate * Like Simple_vFAIL(), but accepts three arguments.
3390Sstevel@tonic-gate */
3400Sstevel@tonic-gate #define Simple_vFAIL3(m, a1, a2) STMT_START { \
3410Sstevel@tonic-gate IV offset = RExC_parse - RExC_precomp; \
3420Sstevel@tonic-gate S_re_croak2(aTHX_ m, REPORT_LOCATION, a1, a2, \
3430Sstevel@tonic-gate (int)offset, RExC_precomp, RExC_precomp + offset); \
3440Sstevel@tonic-gate } STMT_END
3450Sstevel@tonic-gate
3460Sstevel@tonic-gate /*
3470Sstevel@tonic-gate * Calls SAVEDESTRUCTOR_X if needed, then Simple_vFAIL3().
3480Sstevel@tonic-gate */
3490Sstevel@tonic-gate #define vFAIL3(m,a1,a2) STMT_START { \
3500Sstevel@tonic-gate if (!SIZE_ONLY) \
3510Sstevel@tonic-gate SAVEDESTRUCTOR_X(clear_re,(void*)RExC_rx); \
3520Sstevel@tonic-gate Simple_vFAIL3(m, a1, a2); \
3530Sstevel@tonic-gate } STMT_END
3540Sstevel@tonic-gate
3550Sstevel@tonic-gate /*
3560Sstevel@tonic-gate * Like Simple_vFAIL(), but accepts four arguments.
3570Sstevel@tonic-gate */
3580Sstevel@tonic-gate #define Simple_vFAIL4(m, a1, a2, a3) STMT_START { \
3590Sstevel@tonic-gate IV offset = RExC_parse - RExC_precomp; \
3600Sstevel@tonic-gate S_re_croak2(aTHX_ m, REPORT_LOCATION, a1, a2, a3, \
3610Sstevel@tonic-gate (int)offset, RExC_precomp, RExC_precomp + offset); \
3620Sstevel@tonic-gate } STMT_END
3630Sstevel@tonic-gate
3640Sstevel@tonic-gate /*
3650Sstevel@tonic-gate * Like Simple_vFAIL(), but accepts five arguments.
3660Sstevel@tonic-gate */
3670Sstevel@tonic-gate #define Simple_vFAIL5(m, a1, a2, a3, a4) STMT_START { \
3680Sstevel@tonic-gate IV offset = RExC_parse - RExC_precomp; \
3690Sstevel@tonic-gate S_re_croak2(aTHX_ m, REPORT_LOCATION, a1, a2, a3, a4, \
3700Sstevel@tonic-gate (int)offset, RExC_precomp, RExC_precomp + offset); \
3710Sstevel@tonic-gate } STMT_END
3720Sstevel@tonic-gate
3730Sstevel@tonic-gate
3740Sstevel@tonic-gate #define vWARN(loc,m) STMT_START { \
3750Sstevel@tonic-gate IV offset = loc - RExC_precomp; \
3760Sstevel@tonic-gate Perl_warner(aTHX_ packWARN(WARN_REGEXP), "%s" REPORT_LOCATION, \
3770Sstevel@tonic-gate m, (int)offset, RExC_precomp, RExC_precomp + offset); \
3780Sstevel@tonic-gate } STMT_END
3790Sstevel@tonic-gate
3800Sstevel@tonic-gate #define vWARNdep(loc,m) STMT_START { \
3810Sstevel@tonic-gate IV offset = loc - RExC_precomp; \
3820Sstevel@tonic-gate Perl_warner(aTHX_ packWARN2(WARN_DEPRECATED, WARN_REGEXP), \
3830Sstevel@tonic-gate "%s" REPORT_LOCATION, \
3840Sstevel@tonic-gate m, (int)offset, RExC_precomp, RExC_precomp + offset); \
3850Sstevel@tonic-gate } STMT_END
3860Sstevel@tonic-gate
3870Sstevel@tonic-gate
3880Sstevel@tonic-gate #define vWARN2(loc, m, a1) STMT_START { \
3890Sstevel@tonic-gate IV offset = loc - RExC_precomp; \
3900Sstevel@tonic-gate Perl_warner(aTHX_ packWARN(WARN_REGEXP), m REPORT_LOCATION, \
3910Sstevel@tonic-gate a1, (int)offset, RExC_precomp, RExC_precomp + offset); \
3920Sstevel@tonic-gate } STMT_END
3930Sstevel@tonic-gate
3940Sstevel@tonic-gate #define vWARN3(loc, m, a1, a2) STMT_START { \
3950Sstevel@tonic-gate IV offset = loc - RExC_precomp; \
3960Sstevel@tonic-gate Perl_warner(aTHX_ packWARN(WARN_REGEXP), m REPORT_LOCATION, \
3970Sstevel@tonic-gate a1, a2, (int)offset, RExC_precomp, RExC_precomp + offset); \
3980Sstevel@tonic-gate } STMT_END
3990Sstevel@tonic-gate
4000Sstevel@tonic-gate #define vWARN4(loc, m, a1, a2, a3) STMT_START { \
4010Sstevel@tonic-gate IV offset = loc - RExC_precomp; \
4020Sstevel@tonic-gate Perl_warner(aTHX_ packWARN(WARN_REGEXP), m REPORT_LOCATION, \
4030Sstevel@tonic-gate a1, a2, a3, (int)offset, RExC_precomp, RExC_precomp + offset); \
4040Sstevel@tonic-gate } STMT_END
4050Sstevel@tonic-gate
4060Sstevel@tonic-gate #define vWARN5(loc, m, a1, a2, a3, a4) STMT_START { \
4070Sstevel@tonic-gate IV offset = loc - RExC_precomp; \
4080Sstevel@tonic-gate Perl_warner(aTHX_ packWARN(WARN_REGEXP), m REPORT_LOCATION, \
4090Sstevel@tonic-gate a1, a2, a3, a4, (int)offset, RExC_precomp, RExC_precomp + offset); \
4100Sstevel@tonic-gate } STMT_END
4110Sstevel@tonic-gate
4120Sstevel@tonic-gate
4130Sstevel@tonic-gate /* Allow for side effects in s */
4140Sstevel@tonic-gate #define REGC(c,s) STMT_START { \
4150Sstevel@tonic-gate if (!SIZE_ONLY) *(s) = (c); else (void)(s); \
4160Sstevel@tonic-gate } STMT_END
4170Sstevel@tonic-gate
4180Sstevel@tonic-gate /* Macros for recording node offsets. 20001227 mjd@plover.com
4190Sstevel@tonic-gate * Nodes are numbered 1, 2, 3, 4. Node #n's position is recorded in
4200Sstevel@tonic-gate * element 2*n-1 of the array. Element #2n holds the byte length node #n.
4210Sstevel@tonic-gate * Element 0 holds the number n.
4220Sstevel@tonic-gate */
4230Sstevel@tonic-gate
4240Sstevel@tonic-gate #define MJD_OFFSET_DEBUG(x)
4250Sstevel@tonic-gate /* #define MJD_OFFSET_DEBUG(x) Perl_warn_nocontext x */
4260Sstevel@tonic-gate
4270Sstevel@tonic-gate
4280Sstevel@tonic-gate #define Set_Node_Offset_To_R(node,byte) STMT_START { \
4290Sstevel@tonic-gate if (! SIZE_ONLY) { \
4300Sstevel@tonic-gate MJD_OFFSET_DEBUG(("** (%d) offset of node %d is %d.\n", \
4310Sstevel@tonic-gate __LINE__, (node), (byte))); \
4320Sstevel@tonic-gate if((node) < 0) { \
4330Sstevel@tonic-gate Perl_croak(aTHX_ "value of node is %d in Offset macro", node); \
4340Sstevel@tonic-gate } else { \
4350Sstevel@tonic-gate RExC_offsets[2*(node)-1] = (byte); \
4360Sstevel@tonic-gate } \
4370Sstevel@tonic-gate } \
4380Sstevel@tonic-gate } STMT_END
4390Sstevel@tonic-gate
4400Sstevel@tonic-gate #define Set_Node_Offset(node,byte) \
4410Sstevel@tonic-gate Set_Node_Offset_To_R((node)-RExC_emit_start, (byte)-RExC_start)
4420Sstevel@tonic-gate #define Set_Cur_Node_Offset Set_Node_Offset(RExC_emit, RExC_parse)
4430Sstevel@tonic-gate
4440Sstevel@tonic-gate #define Set_Node_Length_To_R(node,len) STMT_START { \
4450Sstevel@tonic-gate if (! SIZE_ONLY) { \
4460Sstevel@tonic-gate MJD_OFFSET_DEBUG(("** (%d) size of node %d is %d.\n", \
4470Sstevel@tonic-gate __LINE__, (node), (len))); \
4480Sstevel@tonic-gate if((node) < 0) { \
4490Sstevel@tonic-gate Perl_croak(aTHX_ "value of node is %d in Length macro", node); \
4500Sstevel@tonic-gate } else { \
4510Sstevel@tonic-gate RExC_offsets[2*(node)] = (len); \
4520Sstevel@tonic-gate } \
4530Sstevel@tonic-gate } \
4540Sstevel@tonic-gate } STMT_END
4550Sstevel@tonic-gate
4560Sstevel@tonic-gate #define Set_Node_Length(node,len) \
4570Sstevel@tonic-gate Set_Node_Length_To_R((node)-RExC_emit_start, len)
4580Sstevel@tonic-gate #define Set_Cur_Node_Length(len) Set_Node_Length(RExC_emit, len)
4590Sstevel@tonic-gate #define Set_Node_Cur_Length(node) \
4600Sstevel@tonic-gate Set_Node_Length(node, RExC_parse - parse_start)
4610Sstevel@tonic-gate
4620Sstevel@tonic-gate /* Get offsets and lengths */
4630Sstevel@tonic-gate #define Node_Offset(n) (RExC_offsets[2*((n)-RExC_emit_start)-1])
4640Sstevel@tonic-gate #define Node_Length(n) (RExC_offsets[2*((n)-RExC_emit_start)])
4650Sstevel@tonic-gate
4660Sstevel@tonic-gate static void clear_re(pTHX_ void *r);
4670Sstevel@tonic-gate
4680Sstevel@tonic-gate /* Mark that we cannot extend a found fixed substring at this point.
4690Sstevel@tonic-gate Updata the longest found anchored substring and the longest found
4700Sstevel@tonic-gate floating substrings if needed. */
4710Sstevel@tonic-gate
4720Sstevel@tonic-gate STATIC void
S_scan_commit(pTHX_ RExC_state_t * pRExC_state,scan_data_t * data)4730Sstevel@tonic-gate S_scan_commit(pTHX_ RExC_state_t *pRExC_state, scan_data_t *data)
4740Sstevel@tonic-gate {
4750Sstevel@tonic-gate STRLEN l = CHR_SVLEN(data->last_found);
4760Sstevel@tonic-gate STRLEN old_l = CHR_SVLEN(*data->longest);
4770Sstevel@tonic-gate
4780Sstevel@tonic-gate if ((l >= old_l) && ((l > old_l) || (data->flags & SF_BEFORE_EOL))) {
4790Sstevel@tonic-gate SvSetMagicSV(*data->longest, data->last_found);
4800Sstevel@tonic-gate if (*data->longest == data->longest_fixed) {
4810Sstevel@tonic-gate data->offset_fixed = l ? data->last_start_min : data->pos_min;
4820Sstevel@tonic-gate if (data->flags & SF_BEFORE_EOL)
4830Sstevel@tonic-gate data->flags
4840Sstevel@tonic-gate |= ((data->flags & SF_BEFORE_EOL) << SF_FIX_SHIFT_EOL);
4850Sstevel@tonic-gate else
4860Sstevel@tonic-gate data->flags &= ~SF_FIX_BEFORE_EOL;
4870Sstevel@tonic-gate }
4880Sstevel@tonic-gate else {
4890Sstevel@tonic-gate data->offset_float_min = l ? data->last_start_min : data->pos_min;
4900Sstevel@tonic-gate data->offset_float_max = (l
4910Sstevel@tonic-gate ? data->last_start_max
4920Sstevel@tonic-gate : data->pos_min + data->pos_delta);
4930Sstevel@tonic-gate if ((U32)data->offset_float_max > (U32)I32_MAX)
4940Sstevel@tonic-gate data->offset_float_max = I32_MAX;
4950Sstevel@tonic-gate if (data->flags & SF_BEFORE_EOL)
4960Sstevel@tonic-gate data->flags
4970Sstevel@tonic-gate |= ((data->flags & SF_BEFORE_EOL) << SF_FL_SHIFT_EOL);
4980Sstevel@tonic-gate else
4990Sstevel@tonic-gate data->flags &= ~SF_FL_BEFORE_EOL;
5000Sstevel@tonic-gate }
5010Sstevel@tonic-gate }
5020Sstevel@tonic-gate SvCUR_set(data->last_found, 0);
5030Sstevel@tonic-gate {
5040Sstevel@tonic-gate SV * sv = data->last_found;
5050Sstevel@tonic-gate MAGIC *mg =
5060Sstevel@tonic-gate SvUTF8(sv) && SvMAGICAL(sv) ? mg_find(sv, PERL_MAGIC_utf8) : NULL;
5070Sstevel@tonic-gate if (mg && mg->mg_len > 0)
5080Sstevel@tonic-gate mg->mg_len = 0;
5090Sstevel@tonic-gate }
5100Sstevel@tonic-gate data->last_end = -1;
5110Sstevel@tonic-gate data->flags &= ~SF_BEFORE_EOL;
5120Sstevel@tonic-gate }
5130Sstevel@tonic-gate
5140Sstevel@tonic-gate /* Can match anything (initialization) */
5150Sstevel@tonic-gate STATIC void
S_cl_anything(pTHX_ RExC_state_t * pRExC_state,struct regnode_charclass_class * cl)5160Sstevel@tonic-gate S_cl_anything(pTHX_ RExC_state_t *pRExC_state, struct regnode_charclass_class *cl)
5170Sstevel@tonic-gate {
5180Sstevel@tonic-gate ANYOF_CLASS_ZERO(cl);
5190Sstevel@tonic-gate ANYOF_BITMAP_SETALL(cl);
5200Sstevel@tonic-gate cl->flags = ANYOF_EOS|ANYOF_UNICODE_ALL;
5210Sstevel@tonic-gate if (LOC)
5220Sstevel@tonic-gate cl->flags |= ANYOF_LOCALE;
5230Sstevel@tonic-gate }
5240Sstevel@tonic-gate
5250Sstevel@tonic-gate /* Can match anything (initialization) */
5260Sstevel@tonic-gate STATIC int
S_cl_is_anything(pTHX_ struct regnode_charclass_class * cl)5270Sstevel@tonic-gate S_cl_is_anything(pTHX_ struct regnode_charclass_class *cl)
5280Sstevel@tonic-gate {
5290Sstevel@tonic-gate int value;
5300Sstevel@tonic-gate
5310Sstevel@tonic-gate for (value = 0; value <= ANYOF_MAX; value += 2)
5320Sstevel@tonic-gate if (ANYOF_CLASS_TEST(cl, value) && ANYOF_CLASS_TEST(cl, value + 1))
5330Sstevel@tonic-gate return 1;
5340Sstevel@tonic-gate if (!(cl->flags & ANYOF_UNICODE_ALL))
5350Sstevel@tonic-gate return 0;
5360Sstevel@tonic-gate if (!ANYOF_BITMAP_TESTALLSET(cl))
5370Sstevel@tonic-gate return 0;
5380Sstevel@tonic-gate return 1;
5390Sstevel@tonic-gate }
5400Sstevel@tonic-gate
5410Sstevel@tonic-gate /* Can match anything (initialization) */
5420Sstevel@tonic-gate STATIC void
S_cl_init(pTHX_ RExC_state_t * pRExC_state,struct regnode_charclass_class * cl)5430Sstevel@tonic-gate S_cl_init(pTHX_ RExC_state_t *pRExC_state, struct regnode_charclass_class *cl)
5440Sstevel@tonic-gate {
5450Sstevel@tonic-gate Zero(cl, 1, struct regnode_charclass_class);
5460Sstevel@tonic-gate cl->type = ANYOF;
5470Sstevel@tonic-gate cl_anything(pRExC_state, cl);
5480Sstevel@tonic-gate }
5490Sstevel@tonic-gate
5500Sstevel@tonic-gate STATIC void
S_cl_init_zero(pTHX_ RExC_state_t * pRExC_state,struct regnode_charclass_class * cl)5510Sstevel@tonic-gate S_cl_init_zero(pTHX_ RExC_state_t *pRExC_state, struct regnode_charclass_class *cl)
5520Sstevel@tonic-gate {
5530Sstevel@tonic-gate Zero(cl, 1, struct regnode_charclass_class);
5540Sstevel@tonic-gate cl->type = ANYOF;
5550Sstevel@tonic-gate cl_anything(pRExC_state, cl);
5560Sstevel@tonic-gate if (LOC)
5570Sstevel@tonic-gate cl->flags |= ANYOF_LOCALE;
5580Sstevel@tonic-gate }
5590Sstevel@tonic-gate
5600Sstevel@tonic-gate /* 'And' a given class with another one. Can create false positives */
5610Sstevel@tonic-gate /* We assume that cl is not inverted */
5620Sstevel@tonic-gate STATIC void
S_cl_and(pTHX_ struct regnode_charclass_class * cl,struct regnode_charclass_class * and_with)5630Sstevel@tonic-gate S_cl_and(pTHX_ struct regnode_charclass_class *cl,
5640Sstevel@tonic-gate struct regnode_charclass_class *and_with)
5650Sstevel@tonic-gate {
5660Sstevel@tonic-gate if (!(and_with->flags & ANYOF_CLASS)
5670Sstevel@tonic-gate && !(cl->flags & ANYOF_CLASS)
5680Sstevel@tonic-gate && (and_with->flags & ANYOF_LOCALE) == (cl->flags & ANYOF_LOCALE)
5690Sstevel@tonic-gate && !(and_with->flags & ANYOF_FOLD)
5700Sstevel@tonic-gate && !(cl->flags & ANYOF_FOLD)) {
5710Sstevel@tonic-gate int i;
5720Sstevel@tonic-gate
5730Sstevel@tonic-gate if (and_with->flags & ANYOF_INVERT)
5740Sstevel@tonic-gate for (i = 0; i < ANYOF_BITMAP_SIZE; i++)
5750Sstevel@tonic-gate cl->bitmap[i] &= ~and_with->bitmap[i];
5760Sstevel@tonic-gate else
5770Sstevel@tonic-gate for (i = 0; i < ANYOF_BITMAP_SIZE; i++)
5780Sstevel@tonic-gate cl->bitmap[i] &= and_with->bitmap[i];
5790Sstevel@tonic-gate } /* XXXX: logic is complicated otherwise, leave it along for a moment. */
5800Sstevel@tonic-gate if (!(and_with->flags & ANYOF_EOS))
5810Sstevel@tonic-gate cl->flags &= ~ANYOF_EOS;
5820Sstevel@tonic-gate
5830Sstevel@tonic-gate if (cl->flags & ANYOF_UNICODE_ALL && and_with->flags & ANYOF_UNICODE &&
5840Sstevel@tonic-gate !(and_with->flags & ANYOF_INVERT)) {
5850Sstevel@tonic-gate cl->flags &= ~ANYOF_UNICODE_ALL;
5860Sstevel@tonic-gate cl->flags |= ANYOF_UNICODE;
5870Sstevel@tonic-gate ARG_SET(cl, ARG(and_with));
5880Sstevel@tonic-gate }
5890Sstevel@tonic-gate if (!(and_with->flags & ANYOF_UNICODE_ALL) &&
5900Sstevel@tonic-gate !(and_with->flags & ANYOF_INVERT))
5910Sstevel@tonic-gate cl->flags &= ~ANYOF_UNICODE_ALL;
5920Sstevel@tonic-gate if (!(and_with->flags & (ANYOF_UNICODE|ANYOF_UNICODE_ALL)) &&
5930Sstevel@tonic-gate !(and_with->flags & ANYOF_INVERT))
5940Sstevel@tonic-gate cl->flags &= ~ANYOF_UNICODE;
5950Sstevel@tonic-gate }
5960Sstevel@tonic-gate
5970Sstevel@tonic-gate /* 'OR' a given class with another one. Can create false positives */
5980Sstevel@tonic-gate /* We assume that cl is not inverted */
5990Sstevel@tonic-gate STATIC void
S_cl_or(pTHX_ RExC_state_t * pRExC_state,struct regnode_charclass_class * cl,struct regnode_charclass_class * or_with)6000Sstevel@tonic-gate S_cl_or(pTHX_ RExC_state_t *pRExC_state, struct regnode_charclass_class *cl, struct regnode_charclass_class *or_with)
6010Sstevel@tonic-gate {
6020Sstevel@tonic-gate if (or_with->flags & ANYOF_INVERT) {
6030Sstevel@tonic-gate /* We do not use
6040Sstevel@tonic-gate * (B1 | CL1) | (!B2 & !CL2) = (B1 | !B2 & !CL2) | (CL1 | (!B2 & !CL2))
6050Sstevel@tonic-gate * <= (B1 | !B2) | (CL1 | !CL2)
6060Sstevel@tonic-gate * which is wasteful if CL2 is small, but we ignore CL2:
6070Sstevel@tonic-gate * (B1 | CL1) | (!B2 & !CL2) <= (B1 | CL1) | !B2 = (B1 | !B2) | CL1
6080Sstevel@tonic-gate * XXXX Can we handle case-fold? Unclear:
6090Sstevel@tonic-gate * (OK1(i) | OK1(i')) | !(OK1(i) | OK1(i')) =
6100Sstevel@tonic-gate * (OK1(i) | OK1(i')) | (!OK1(i) & !OK1(i'))
6110Sstevel@tonic-gate */
6120Sstevel@tonic-gate if ( (or_with->flags & ANYOF_LOCALE) == (cl->flags & ANYOF_LOCALE)
6130Sstevel@tonic-gate && !(or_with->flags & ANYOF_FOLD)
6140Sstevel@tonic-gate && !(cl->flags & ANYOF_FOLD) ) {
6150Sstevel@tonic-gate int i;
6160Sstevel@tonic-gate
6170Sstevel@tonic-gate for (i = 0; i < ANYOF_BITMAP_SIZE; i++)
6180Sstevel@tonic-gate cl->bitmap[i] |= ~or_with->bitmap[i];
6190Sstevel@tonic-gate } /* XXXX: logic is complicated otherwise */
6200Sstevel@tonic-gate else {
6210Sstevel@tonic-gate cl_anything(pRExC_state, cl);
6220Sstevel@tonic-gate }
6230Sstevel@tonic-gate } else {
6240Sstevel@tonic-gate /* (B1 | CL1) | (B2 | CL2) = (B1 | B2) | (CL1 | CL2)) */
6250Sstevel@tonic-gate if ( (or_with->flags & ANYOF_LOCALE) == (cl->flags & ANYOF_LOCALE)
6260Sstevel@tonic-gate && (!(or_with->flags & ANYOF_FOLD)
6270Sstevel@tonic-gate || (cl->flags & ANYOF_FOLD)) ) {
6280Sstevel@tonic-gate int i;
6290Sstevel@tonic-gate
6300Sstevel@tonic-gate /* OR char bitmap and class bitmap separately */
6310Sstevel@tonic-gate for (i = 0; i < ANYOF_BITMAP_SIZE; i++)
6320Sstevel@tonic-gate cl->bitmap[i] |= or_with->bitmap[i];
6330Sstevel@tonic-gate if (or_with->flags & ANYOF_CLASS) {
6340Sstevel@tonic-gate for (i = 0; i < ANYOF_CLASSBITMAP_SIZE; i++)
6350Sstevel@tonic-gate cl->classflags[i] |= or_with->classflags[i];
6360Sstevel@tonic-gate cl->flags |= ANYOF_CLASS;
6370Sstevel@tonic-gate }
6380Sstevel@tonic-gate }
6390Sstevel@tonic-gate else { /* XXXX: logic is complicated, leave it along for a moment. */
6400Sstevel@tonic-gate cl_anything(pRExC_state, cl);
6410Sstevel@tonic-gate }
6420Sstevel@tonic-gate }
6430Sstevel@tonic-gate if (or_with->flags & ANYOF_EOS)
6440Sstevel@tonic-gate cl->flags |= ANYOF_EOS;
6450Sstevel@tonic-gate
6460Sstevel@tonic-gate if (cl->flags & ANYOF_UNICODE && or_with->flags & ANYOF_UNICODE &&
6470Sstevel@tonic-gate ARG(cl) != ARG(or_with)) {
6480Sstevel@tonic-gate cl->flags |= ANYOF_UNICODE_ALL;
6490Sstevel@tonic-gate cl->flags &= ~ANYOF_UNICODE;
6500Sstevel@tonic-gate }
6510Sstevel@tonic-gate if (or_with->flags & ANYOF_UNICODE_ALL) {
6520Sstevel@tonic-gate cl->flags |= ANYOF_UNICODE_ALL;
6530Sstevel@tonic-gate cl->flags &= ~ANYOF_UNICODE;
6540Sstevel@tonic-gate }
6550Sstevel@tonic-gate }
6560Sstevel@tonic-gate
6570Sstevel@tonic-gate /*
6580Sstevel@tonic-gate * There are strange code-generation bugs caused on sparc64 by gcc-2.95.2.
6590Sstevel@tonic-gate * These need to be revisited when a newer toolchain becomes available.
6600Sstevel@tonic-gate */
6610Sstevel@tonic-gate #if defined(__sparc64__) && defined(__GNUC__)
6620Sstevel@tonic-gate # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 96)
6630Sstevel@tonic-gate # undef SPARC64_GCC_WORKAROUND
6640Sstevel@tonic-gate # define SPARC64_GCC_WORKAROUND 1
6650Sstevel@tonic-gate # endif
6660Sstevel@tonic-gate #endif
6670Sstevel@tonic-gate
6680Sstevel@tonic-gate /* REx optimizer. Converts nodes into quickier variants "in place".
6690Sstevel@tonic-gate Finds fixed substrings. */
6700Sstevel@tonic-gate
6710Sstevel@tonic-gate /* Stops at toplevel WHILEM as well as at `last'. At end *scanp is set
6720Sstevel@tonic-gate to the position after last scanned or to NULL. */
6730Sstevel@tonic-gate
6740Sstevel@tonic-gate STATIC I32
S_study_chunk(pTHX_ RExC_state_t * pRExC_state,regnode ** scanp,I32 * deltap,regnode * last,scan_data_t * data,U32 flags)6750Sstevel@tonic-gate S_study_chunk(pTHX_ RExC_state_t *pRExC_state, regnode **scanp, I32 *deltap, regnode *last, scan_data_t *data, U32 flags)
6760Sstevel@tonic-gate /* scanp: Start here (read-write). */
6770Sstevel@tonic-gate /* deltap: Write maxlen-minlen here. */
6780Sstevel@tonic-gate /* last: Stop before this one. */
6790Sstevel@tonic-gate {
6800Sstevel@tonic-gate I32 min = 0, pars = 0, code;
6810Sstevel@tonic-gate regnode *scan = *scanp, *next;
6820Sstevel@tonic-gate I32 delta = 0;
6830Sstevel@tonic-gate int is_inf = (flags & SCF_DO_SUBSTR) && (data->flags & SF_IS_INF);
6840Sstevel@tonic-gate int is_inf_internal = 0; /* The studied chunk is infinite */
6850Sstevel@tonic-gate I32 is_par = OP(scan) == OPEN ? ARG(scan) : 0;
6860Sstevel@tonic-gate scan_data_t data_fake;
6870Sstevel@tonic-gate struct regnode_charclass_class and_with; /* Valid if flags & SCF_DO_STCLASS_OR */
6880Sstevel@tonic-gate
6890Sstevel@tonic-gate while (scan && OP(scan) != END && scan < last) {
6900Sstevel@tonic-gate /* Peephole optimizer: */
6910Sstevel@tonic-gate
6920Sstevel@tonic-gate if (PL_regkind[(U8)OP(scan)] == EXACT) {
6930Sstevel@tonic-gate /* Merge several consecutive EXACTish nodes into one. */
6940Sstevel@tonic-gate regnode *n = regnext(scan);
6950Sstevel@tonic-gate U32 stringok = 1;
6960Sstevel@tonic-gate #ifdef DEBUGGING
6970Sstevel@tonic-gate regnode *stop = scan;
6980Sstevel@tonic-gate #endif
6990Sstevel@tonic-gate
7000Sstevel@tonic-gate next = scan + NODE_SZ_STR(scan);
7010Sstevel@tonic-gate /* Skip NOTHING, merge EXACT*. */
7020Sstevel@tonic-gate while (n &&
7030Sstevel@tonic-gate ( PL_regkind[(U8)OP(n)] == NOTHING ||
7040Sstevel@tonic-gate (stringok && (OP(n) == OP(scan))))
7050Sstevel@tonic-gate && NEXT_OFF(n)
7060Sstevel@tonic-gate && NEXT_OFF(scan) + NEXT_OFF(n) < I16_MAX) {
7070Sstevel@tonic-gate if (OP(n) == TAIL || n > next)
7080Sstevel@tonic-gate stringok = 0;
7090Sstevel@tonic-gate if (PL_regkind[(U8)OP(n)] == NOTHING) {
7100Sstevel@tonic-gate NEXT_OFF(scan) += NEXT_OFF(n);
7110Sstevel@tonic-gate next = n + NODE_STEP_REGNODE;
7120Sstevel@tonic-gate #ifdef DEBUGGING
7130Sstevel@tonic-gate if (stringok)
7140Sstevel@tonic-gate stop = n;
7150Sstevel@tonic-gate #endif
7160Sstevel@tonic-gate n = regnext(n);
7170Sstevel@tonic-gate }
7180Sstevel@tonic-gate else if (stringok) {
7190Sstevel@tonic-gate int oldl = STR_LEN(scan);
7200Sstevel@tonic-gate regnode *nnext = regnext(n);
7210Sstevel@tonic-gate
7220Sstevel@tonic-gate if (oldl + STR_LEN(n) > U8_MAX)
7230Sstevel@tonic-gate break;
7240Sstevel@tonic-gate NEXT_OFF(scan) += NEXT_OFF(n);
7250Sstevel@tonic-gate STR_LEN(scan) += STR_LEN(n);
7260Sstevel@tonic-gate next = n + NODE_SZ_STR(n);
7270Sstevel@tonic-gate /* Now we can overwrite *n : */
7280Sstevel@tonic-gate Move(STRING(n), STRING(scan) + oldl, STR_LEN(n), char);
7290Sstevel@tonic-gate #ifdef DEBUGGING
7300Sstevel@tonic-gate stop = next - 1;
7310Sstevel@tonic-gate #endif
7320Sstevel@tonic-gate n = nnext;
7330Sstevel@tonic-gate }
7340Sstevel@tonic-gate }
7350Sstevel@tonic-gate
7360Sstevel@tonic-gate if (UTF && OP(scan) == EXACTF && STR_LEN(scan) >= 6) {
7370Sstevel@tonic-gate /*
7380Sstevel@tonic-gate Two problematic code points in Unicode casefolding of EXACT nodes:
7390Sstevel@tonic-gate
7400Sstevel@tonic-gate U+0390 - GREEK SMALL LETTER IOTA WITH DIALYTIKA AND TONOS
7410Sstevel@tonic-gate U+03B0 - GREEK SMALL LETTER UPSILON WITH DIALYTIKA AND TONOS
7420Sstevel@tonic-gate
7430Sstevel@tonic-gate which casefold to
7440Sstevel@tonic-gate
7450Sstevel@tonic-gate Unicode UTF-8
7460Sstevel@tonic-gate
7470Sstevel@tonic-gate U+03B9 U+0308 U+0301 0xCE 0xB9 0xCC 0x88 0xCC 0x81
7480Sstevel@tonic-gate U+03C5 U+0308 U+0301 0xCF 0x85 0xCC 0x88 0xCC 0x81
7490Sstevel@tonic-gate
7500Sstevel@tonic-gate This means that in case-insensitive matching (or "loose matching",
7510Sstevel@tonic-gate as Unicode calls it), an EXACTF of length six (the UTF-8 encoded byte
7520Sstevel@tonic-gate length of the above casefolded versions) can match a target string
7530Sstevel@tonic-gate of length two (the byte length of UTF-8 encoded U+0390 or U+03B0).
7540Sstevel@tonic-gate This would rather mess up the minimum length computation.
7550Sstevel@tonic-gate
7560Sstevel@tonic-gate What we'll do is to look for the tail four bytes, and then peek
7570Sstevel@tonic-gate at the preceding two bytes to see whether we need to decrease
7580Sstevel@tonic-gate the minimum length by four (six minus two).
7590Sstevel@tonic-gate
7600Sstevel@tonic-gate Thanks to the design of UTF-8, there cannot be false matches:
7610Sstevel@tonic-gate A sequence of valid UTF-8 bytes cannot be a subsequence of
7620Sstevel@tonic-gate another valid sequence of UTF-8 bytes.
7630Sstevel@tonic-gate
7640Sstevel@tonic-gate */
7650Sstevel@tonic-gate char *s0 = STRING(scan), *s, *t;
7660Sstevel@tonic-gate char *s1 = s0 + STR_LEN(scan) - 1, *s2 = s1 - 4;
7670Sstevel@tonic-gate char *t0 = "\xcc\x88\xcc\x81";
7680Sstevel@tonic-gate char *t1 = t0 + 3;
7690Sstevel@tonic-gate
7700Sstevel@tonic-gate for (s = s0 + 2;
7710Sstevel@tonic-gate s < s2 && (t = ninstr(s, s1, t0, t1));
7720Sstevel@tonic-gate s = t + 4) {
7730Sstevel@tonic-gate if (((U8)t[-1] == 0xB9 && (U8)t[-2] == 0xCE) ||
7740Sstevel@tonic-gate ((U8)t[-1] == 0x85 && (U8)t[-2] == 0xCF))
7750Sstevel@tonic-gate min -= 4;
7760Sstevel@tonic-gate }
7770Sstevel@tonic-gate }
7780Sstevel@tonic-gate
7790Sstevel@tonic-gate #ifdef DEBUGGING
7800Sstevel@tonic-gate /* Allow dumping */
7810Sstevel@tonic-gate n = scan + NODE_SZ_STR(scan);
7820Sstevel@tonic-gate while (n <= stop) {
7830Sstevel@tonic-gate if (PL_regkind[(U8)OP(n)] != NOTHING || OP(n) == NOTHING) {
7840Sstevel@tonic-gate OP(n) = OPTIMIZED;
7850Sstevel@tonic-gate NEXT_OFF(n) = 0;
7860Sstevel@tonic-gate }
7870Sstevel@tonic-gate n++;
7880Sstevel@tonic-gate }
7890Sstevel@tonic-gate #endif
7900Sstevel@tonic-gate }
7910Sstevel@tonic-gate /* Follow the next-chain of the current node and optimize
7920Sstevel@tonic-gate away all the NOTHINGs from it. */
7930Sstevel@tonic-gate if (OP(scan) != CURLYX) {
7940Sstevel@tonic-gate int max = (reg_off_by_arg[OP(scan)]
7950Sstevel@tonic-gate ? I32_MAX
7960Sstevel@tonic-gate /* I32 may be smaller than U16 on CRAYs! */
7970Sstevel@tonic-gate : (I32_MAX < U16_MAX ? I32_MAX : U16_MAX));
7980Sstevel@tonic-gate int off = (reg_off_by_arg[OP(scan)] ? ARG(scan) : NEXT_OFF(scan));
7990Sstevel@tonic-gate int noff;
8000Sstevel@tonic-gate regnode *n = scan;
8010Sstevel@tonic-gate
8020Sstevel@tonic-gate /* Skip NOTHING and LONGJMP. */
8030Sstevel@tonic-gate while ((n = regnext(n))
8040Sstevel@tonic-gate && ((PL_regkind[(U8)OP(n)] == NOTHING && (noff = NEXT_OFF(n)))
8050Sstevel@tonic-gate || ((OP(n) == LONGJMP) && (noff = ARG(n))))
8060Sstevel@tonic-gate && off + noff < max)
8070Sstevel@tonic-gate off += noff;
8080Sstevel@tonic-gate if (reg_off_by_arg[OP(scan)])
8090Sstevel@tonic-gate ARG(scan) = off;
8100Sstevel@tonic-gate else
8110Sstevel@tonic-gate NEXT_OFF(scan) = off;
8120Sstevel@tonic-gate }
8130Sstevel@tonic-gate /* The principal pseudo-switch. Cannot be a switch, since we
8140Sstevel@tonic-gate look into several different things. */
8150Sstevel@tonic-gate if (OP(scan) == BRANCH || OP(scan) == BRANCHJ
8160Sstevel@tonic-gate || OP(scan) == IFTHEN || OP(scan) == SUSPEND) {
8170Sstevel@tonic-gate next = regnext(scan);
8180Sstevel@tonic-gate code = OP(scan);
8190Sstevel@tonic-gate
8200Sstevel@tonic-gate if (OP(next) == code || code == IFTHEN || code == SUSPEND) {
8210Sstevel@tonic-gate I32 max1 = 0, min1 = I32_MAX, num = 0;
8220Sstevel@tonic-gate struct regnode_charclass_class accum;
8230Sstevel@tonic-gate
8240Sstevel@tonic-gate if (flags & SCF_DO_SUBSTR) /* XXXX Add !SUSPEND? */
8250Sstevel@tonic-gate scan_commit(pRExC_state, data); /* Cannot merge strings after this. */
8260Sstevel@tonic-gate if (flags & SCF_DO_STCLASS)
8270Sstevel@tonic-gate cl_init_zero(pRExC_state, &accum);
8280Sstevel@tonic-gate while (OP(scan) == code) {
8290Sstevel@tonic-gate I32 deltanext, minnext, f = 0, fake;
8300Sstevel@tonic-gate struct regnode_charclass_class this_class;
8310Sstevel@tonic-gate
8320Sstevel@tonic-gate num++;
8330Sstevel@tonic-gate data_fake.flags = 0;
8340Sstevel@tonic-gate if (data) {
8350Sstevel@tonic-gate data_fake.whilem_c = data->whilem_c;
8360Sstevel@tonic-gate data_fake.last_closep = data->last_closep;
8370Sstevel@tonic-gate }
8380Sstevel@tonic-gate else
8390Sstevel@tonic-gate data_fake.last_closep = &fake;
8400Sstevel@tonic-gate next = regnext(scan);
8410Sstevel@tonic-gate scan = NEXTOPER(scan);
8420Sstevel@tonic-gate if (code != BRANCH)
8430Sstevel@tonic-gate scan = NEXTOPER(scan);
8440Sstevel@tonic-gate if (flags & SCF_DO_STCLASS) {
8450Sstevel@tonic-gate cl_init(pRExC_state, &this_class);
8460Sstevel@tonic-gate data_fake.start_class = &this_class;
8470Sstevel@tonic-gate f = SCF_DO_STCLASS_AND;
8480Sstevel@tonic-gate }
8490Sstevel@tonic-gate if (flags & SCF_WHILEM_VISITED_POS)
8500Sstevel@tonic-gate f |= SCF_WHILEM_VISITED_POS;
8510Sstevel@tonic-gate /* we suppose the run is continuous, last=next...*/
8520Sstevel@tonic-gate minnext = study_chunk(pRExC_state, &scan, &deltanext,
8530Sstevel@tonic-gate next, &data_fake, f);
8540Sstevel@tonic-gate if (min1 > minnext)
8550Sstevel@tonic-gate min1 = minnext;
8560Sstevel@tonic-gate if (max1 < minnext + deltanext)
8570Sstevel@tonic-gate max1 = minnext + deltanext;
8580Sstevel@tonic-gate if (deltanext == I32_MAX)
8590Sstevel@tonic-gate is_inf = is_inf_internal = 1;
8600Sstevel@tonic-gate scan = next;
8610Sstevel@tonic-gate if (data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
8620Sstevel@tonic-gate pars++;
8630Sstevel@tonic-gate if (data && (data_fake.flags & SF_HAS_EVAL))
8640Sstevel@tonic-gate data->flags |= SF_HAS_EVAL;
8650Sstevel@tonic-gate if (data)
8660Sstevel@tonic-gate data->whilem_c = data_fake.whilem_c;
8670Sstevel@tonic-gate if (flags & SCF_DO_STCLASS)
8680Sstevel@tonic-gate cl_or(pRExC_state, &accum, &this_class);
8690Sstevel@tonic-gate if (code == SUSPEND)
8700Sstevel@tonic-gate break;
8710Sstevel@tonic-gate }
8720Sstevel@tonic-gate if (code == IFTHEN && num < 2) /* Empty ELSE branch */
8730Sstevel@tonic-gate min1 = 0;
8740Sstevel@tonic-gate if (flags & SCF_DO_SUBSTR) {
8750Sstevel@tonic-gate data->pos_min += min1;
8760Sstevel@tonic-gate data->pos_delta += max1 - min1;
8770Sstevel@tonic-gate if (max1 != min1 || is_inf)
8780Sstevel@tonic-gate data->longest = &(data->longest_float);
8790Sstevel@tonic-gate }
8800Sstevel@tonic-gate min += min1;
8810Sstevel@tonic-gate delta += max1 - min1;
8820Sstevel@tonic-gate if (flags & SCF_DO_STCLASS_OR) {
8830Sstevel@tonic-gate cl_or(pRExC_state, data->start_class, &accum);
8840Sstevel@tonic-gate if (min1) {
8850Sstevel@tonic-gate cl_and(data->start_class, &and_with);
8860Sstevel@tonic-gate flags &= ~SCF_DO_STCLASS;
8870Sstevel@tonic-gate }
8880Sstevel@tonic-gate }
8890Sstevel@tonic-gate else if (flags & SCF_DO_STCLASS_AND) {
8900Sstevel@tonic-gate if (min1) {
8910Sstevel@tonic-gate cl_and(data->start_class, &accum);
8920Sstevel@tonic-gate flags &= ~SCF_DO_STCLASS;
8930Sstevel@tonic-gate }
8940Sstevel@tonic-gate else {
8950Sstevel@tonic-gate /* Switch to OR mode: cache the old value of
8960Sstevel@tonic-gate * data->start_class */
8970Sstevel@tonic-gate StructCopy(data->start_class, &and_with,
8980Sstevel@tonic-gate struct regnode_charclass_class);
8990Sstevel@tonic-gate flags &= ~SCF_DO_STCLASS_AND;
9000Sstevel@tonic-gate StructCopy(&accum, data->start_class,
9010Sstevel@tonic-gate struct regnode_charclass_class);
9020Sstevel@tonic-gate flags |= SCF_DO_STCLASS_OR;
9030Sstevel@tonic-gate data->start_class->flags |= ANYOF_EOS;
9040Sstevel@tonic-gate }
9050Sstevel@tonic-gate }
9060Sstevel@tonic-gate }
9070Sstevel@tonic-gate else if (code == BRANCHJ) /* single branch is optimized. */
9080Sstevel@tonic-gate scan = NEXTOPER(NEXTOPER(scan));
9090Sstevel@tonic-gate else /* single branch is optimized. */
9100Sstevel@tonic-gate scan = NEXTOPER(scan);
9110Sstevel@tonic-gate continue;
9120Sstevel@tonic-gate }
9130Sstevel@tonic-gate else if (OP(scan) == EXACT) {
9140Sstevel@tonic-gate I32 l = STR_LEN(scan);
9150Sstevel@tonic-gate UV uc = *((U8*)STRING(scan));
9160Sstevel@tonic-gate if (UTF) {
9170Sstevel@tonic-gate U8 *s = (U8*)STRING(scan);
9180Sstevel@tonic-gate l = utf8_length(s, s + l);
9190Sstevel@tonic-gate uc = utf8_to_uvchr(s, NULL);
9200Sstevel@tonic-gate }
9210Sstevel@tonic-gate min += l;
9220Sstevel@tonic-gate if (flags & SCF_DO_SUBSTR) { /* Update longest substr. */
9230Sstevel@tonic-gate /* The code below prefers earlier match for fixed
9240Sstevel@tonic-gate offset, later match for variable offset. */
9250Sstevel@tonic-gate if (data->last_end == -1) { /* Update the start info. */
9260Sstevel@tonic-gate data->last_start_min = data->pos_min;
9270Sstevel@tonic-gate data->last_start_max = is_inf
9280Sstevel@tonic-gate ? I32_MAX : data->pos_min + data->pos_delta;
9290Sstevel@tonic-gate }
9300Sstevel@tonic-gate sv_catpvn(data->last_found, STRING(scan), STR_LEN(scan));
9310Sstevel@tonic-gate {
9320Sstevel@tonic-gate SV * sv = data->last_found;
9330Sstevel@tonic-gate MAGIC *mg = SvUTF8(sv) && SvMAGICAL(sv) ?
9340Sstevel@tonic-gate mg_find(sv, PERL_MAGIC_utf8) : NULL;
9350Sstevel@tonic-gate if (mg && mg->mg_len >= 0)
9360Sstevel@tonic-gate mg->mg_len += utf8_length((U8*)STRING(scan),
9370Sstevel@tonic-gate (U8*)STRING(scan)+STR_LEN(scan));
9380Sstevel@tonic-gate }
9390Sstevel@tonic-gate if (UTF)
9400Sstevel@tonic-gate SvUTF8_on(data->last_found);
9410Sstevel@tonic-gate data->last_end = data->pos_min + l;
9420Sstevel@tonic-gate data->pos_min += l; /* As in the first entry. */
9430Sstevel@tonic-gate data->flags &= ~SF_BEFORE_EOL;
9440Sstevel@tonic-gate }
9450Sstevel@tonic-gate if (flags & SCF_DO_STCLASS_AND) {
9460Sstevel@tonic-gate /* Check whether it is compatible with what we know already! */
9470Sstevel@tonic-gate int compat = 1;
9480Sstevel@tonic-gate
9490Sstevel@tonic-gate if (uc >= 0x100 ||
9500Sstevel@tonic-gate (!(data->start_class->flags & (ANYOF_CLASS | ANYOF_LOCALE))
9510Sstevel@tonic-gate && !ANYOF_BITMAP_TEST(data->start_class, uc)
9520Sstevel@tonic-gate && (!(data->start_class->flags & ANYOF_FOLD)
9530Sstevel@tonic-gate || !ANYOF_BITMAP_TEST(data->start_class, PL_fold[uc])))
9540Sstevel@tonic-gate )
9550Sstevel@tonic-gate compat = 0;
9560Sstevel@tonic-gate ANYOF_CLASS_ZERO(data->start_class);
9570Sstevel@tonic-gate ANYOF_BITMAP_ZERO(data->start_class);
9580Sstevel@tonic-gate if (compat)
9590Sstevel@tonic-gate ANYOF_BITMAP_SET(data->start_class, uc);
9600Sstevel@tonic-gate data->start_class->flags &= ~ANYOF_EOS;
9610Sstevel@tonic-gate if (uc < 0x100)
9620Sstevel@tonic-gate data->start_class->flags &= ~ANYOF_UNICODE_ALL;
9630Sstevel@tonic-gate }
9640Sstevel@tonic-gate else if (flags & SCF_DO_STCLASS_OR) {
9650Sstevel@tonic-gate /* false positive possible if the class is case-folded */
9660Sstevel@tonic-gate if (uc < 0x100)
9670Sstevel@tonic-gate ANYOF_BITMAP_SET(data->start_class, uc);
9680Sstevel@tonic-gate else
9690Sstevel@tonic-gate data->start_class->flags |= ANYOF_UNICODE_ALL;
9700Sstevel@tonic-gate data->start_class->flags &= ~ANYOF_EOS;
9710Sstevel@tonic-gate cl_and(data->start_class, &and_with);
9720Sstevel@tonic-gate }
9730Sstevel@tonic-gate flags &= ~SCF_DO_STCLASS;
9740Sstevel@tonic-gate }
9750Sstevel@tonic-gate else if (PL_regkind[(U8)OP(scan)] == EXACT) { /* But OP != EXACT! */
9760Sstevel@tonic-gate I32 l = STR_LEN(scan);
9770Sstevel@tonic-gate UV uc = *((U8*)STRING(scan));
9780Sstevel@tonic-gate
9790Sstevel@tonic-gate /* Search for fixed substrings supports EXACT only. */
9800Sstevel@tonic-gate if (flags & SCF_DO_SUBSTR)
9810Sstevel@tonic-gate scan_commit(pRExC_state, data);
9820Sstevel@tonic-gate if (UTF) {
9830Sstevel@tonic-gate U8 *s = (U8 *)STRING(scan);
9840Sstevel@tonic-gate l = utf8_length(s, s + l);
9850Sstevel@tonic-gate uc = utf8_to_uvchr(s, NULL);
9860Sstevel@tonic-gate }
9870Sstevel@tonic-gate min += l;
9880Sstevel@tonic-gate if (data && (flags & SCF_DO_SUBSTR))
9890Sstevel@tonic-gate data->pos_min += l;
9900Sstevel@tonic-gate if (flags & SCF_DO_STCLASS_AND) {
9910Sstevel@tonic-gate /* Check whether it is compatible with what we know already! */
9920Sstevel@tonic-gate int compat = 1;
9930Sstevel@tonic-gate
9940Sstevel@tonic-gate if (uc >= 0x100 ||
9950Sstevel@tonic-gate (!(data->start_class->flags & (ANYOF_CLASS | ANYOF_LOCALE))
9960Sstevel@tonic-gate && !ANYOF_BITMAP_TEST(data->start_class, uc)
9970Sstevel@tonic-gate && !ANYOF_BITMAP_TEST(data->start_class, PL_fold[uc])))
9980Sstevel@tonic-gate compat = 0;
9990Sstevel@tonic-gate ANYOF_CLASS_ZERO(data->start_class);
10000Sstevel@tonic-gate ANYOF_BITMAP_ZERO(data->start_class);
10010Sstevel@tonic-gate if (compat) {
10020Sstevel@tonic-gate ANYOF_BITMAP_SET(data->start_class, uc);
10030Sstevel@tonic-gate data->start_class->flags &= ~ANYOF_EOS;
10040Sstevel@tonic-gate data->start_class->flags |= ANYOF_FOLD;
10050Sstevel@tonic-gate if (OP(scan) == EXACTFL)
10060Sstevel@tonic-gate data->start_class->flags |= ANYOF_LOCALE;
10070Sstevel@tonic-gate }
10080Sstevel@tonic-gate }
10090Sstevel@tonic-gate else if (flags & SCF_DO_STCLASS_OR) {
10100Sstevel@tonic-gate if (data->start_class->flags & ANYOF_FOLD) {
10110Sstevel@tonic-gate /* false positive possible if the class is case-folded.
10120Sstevel@tonic-gate Assume that the locale settings are the same... */
10130Sstevel@tonic-gate if (uc < 0x100)
10140Sstevel@tonic-gate ANYOF_BITMAP_SET(data->start_class, uc);
10150Sstevel@tonic-gate data->start_class->flags &= ~ANYOF_EOS;
10160Sstevel@tonic-gate }
10170Sstevel@tonic-gate cl_and(data->start_class, &and_with);
10180Sstevel@tonic-gate }
10190Sstevel@tonic-gate flags &= ~SCF_DO_STCLASS;
10200Sstevel@tonic-gate }
10210Sstevel@tonic-gate else if (strchr((char*)PL_varies,OP(scan))) {
10220Sstevel@tonic-gate I32 mincount, maxcount, minnext, deltanext, fl = 0;
10230Sstevel@tonic-gate I32 f = flags, pos_before = 0;
10240Sstevel@tonic-gate regnode *oscan = scan;
10250Sstevel@tonic-gate struct regnode_charclass_class this_class;
10260Sstevel@tonic-gate struct regnode_charclass_class *oclass = NULL;
10270Sstevel@tonic-gate I32 next_is_eval = 0;
10280Sstevel@tonic-gate
10290Sstevel@tonic-gate switch (PL_regkind[(U8)OP(scan)]) {
10300Sstevel@tonic-gate case WHILEM: /* End of (?:...)* . */
10310Sstevel@tonic-gate scan = NEXTOPER(scan);
10320Sstevel@tonic-gate goto finish;
10330Sstevel@tonic-gate case PLUS:
10340Sstevel@tonic-gate if (flags & (SCF_DO_SUBSTR | SCF_DO_STCLASS)) {
10350Sstevel@tonic-gate next = NEXTOPER(scan);
10360Sstevel@tonic-gate if (OP(next) == EXACT || (flags & SCF_DO_STCLASS)) {
10370Sstevel@tonic-gate mincount = 1;
10380Sstevel@tonic-gate maxcount = REG_INFTY;
10390Sstevel@tonic-gate next = regnext(scan);
10400Sstevel@tonic-gate scan = NEXTOPER(scan);
10410Sstevel@tonic-gate goto do_curly;
10420Sstevel@tonic-gate }
10430Sstevel@tonic-gate }
10440Sstevel@tonic-gate if (flags & SCF_DO_SUBSTR)
10450Sstevel@tonic-gate data->pos_min++;
10460Sstevel@tonic-gate min++;
10470Sstevel@tonic-gate /* Fall through. */
10480Sstevel@tonic-gate case STAR:
10490Sstevel@tonic-gate if (flags & SCF_DO_STCLASS) {
10500Sstevel@tonic-gate mincount = 0;
10510Sstevel@tonic-gate maxcount = REG_INFTY;
10520Sstevel@tonic-gate next = regnext(scan);
10530Sstevel@tonic-gate scan = NEXTOPER(scan);
10540Sstevel@tonic-gate goto do_curly;
10550Sstevel@tonic-gate }
10560Sstevel@tonic-gate is_inf = is_inf_internal = 1;
10570Sstevel@tonic-gate scan = regnext(scan);
10580Sstevel@tonic-gate if (flags & SCF_DO_SUBSTR) {
10590Sstevel@tonic-gate scan_commit(pRExC_state, data); /* Cannot extend fixed substrings */
10600Sstevel@tonic-gate data->longest = &(data->longest_float);
10610Sstevel@tonic-gate }
10620Sstevel@tonic-gate goto optimize_curly_tail;
10630Sstevel@tonic-gate case CURLY:
10640Sstevel@tonic-gate mincount = ARG1(scan);
10650Sstevel@tonic-gate maxcount = ARG2(scan);
10660Sstevel@tonic-gate next = regnext(scan);
10670Sstevel@tonic-gate if (OP(scan) == CURLYX) {
10680Sstevel@tonic-gate I32 lp = (data ? *(data->last_closep) : 0);
10690Sstevel@tonic-gate
10700Sstevel@tonic-gate scan->flags = ((lp <= U8_MAX) ? lp : U8_MAX);
10710Sstevel@tonic-gate }
10720Sstevel@tonic-gate scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
10730Sstevel@tonic-gate next_is_eval = (OP(scan) == EVAL);
10740Sstevel@tonic-gate do_curly:
10750Sstevel@tonic-gate if (flags & SCF_DO_SUBSTR) {
10760Sstevel@tonic-gate if (mincount == 0) scan_commit(pRExC_state,data); /* Cannot extend fixed substrings */
10770Sstevel@tonic-gate pos_before = data->pos_min;
10780Sstevel@tonic-gate }
10790Sstevel@tonic-gate if (data) {
10800Sstevel@tonic-gate fl = data->flags;
10810Sstevel@tonic-gate data->flags &= ~(SF_HAS_PAR|SF_IN_PAR|SF_HAS_EVAL);
10820Sstevel@tonic-gate if (is_inf)
10830Sstevel@tonic-gate data->flags |= SF_IS_INF;
10840Sstevel@tonic-gate }
10850Sstevel@tonic-gate if (flags & SCF_DO_STCLASS) {
10860Sstevel@tonic-gate cl_init(pRExC_state, &this_class);
10870Sstevel@tonic-gate oclass = data->start_class;
10880Sstevel@tonic-gate data->start_class = &this_class;
10890Sstevel@tonic-gate f |= SCF_DO_STCLASS_AND;
10900Sstevel@tonic-gate f &= ~SCF_DO_STCLASS_OR;
10910Sstevel@tonic-gate }
10920Sstevel@tonic-gate /* These are the cases when once a subexpression
10930Sstevel@tonic-gate fails at a particular position, it cannot succeed
10940Sstevel@tonic-gate even after backtracking at the enclosing scope.
10950Sstevel@tonic-gate
10960Sstevel@tonic-gate XXXX what if minimal match and we are at the
10970Sstevel@tonic-gate initial run of {n,m}? */
10980Sstevel@tonic-gate if ((mincount != maxcount - 1) && (maxcount != REG_INFTY))
10990Sstevel@tonic-gate f &= ~SCF_WHILEM_VISITED_POS;
11000Sstevel@tonic-gate
11010Sstevel@tonic-gate /* This will finish on WHILEM, setting scan, or on NULL: */
11020Sstevel@tonic-gate minnext = study_chunk(pRExC_state, &scan, &deltanext, last, data,
11030Sstevel@tonic-gate mincount == 0
11040Sstevel@tonic-gate ? (f & ~SCF_DO_SUBSTR) : f);
11050Sstevel@tonic-gate
11060Sstevel@tonic-gate if (flags & SCF_DO_STCLASS)
11070Sstevel@tonic-gate data->start_class = oclass;
11080Sstevel@tonic-gate if (mincount == 0 || minnext == 0) {
11090Sstevel@tonic-gate if (flags & SCF_DO_STCLASS_OR) {
11100Sstevel@tonic-gate cl_or(pRExC_state, data->start_class, &this_class);
11110Sstevel@tonic-gate }
11120Sstevel@tonic-gate else if (flags & SCF_DO_STCLASS_AND) {
11130Sstevel@tonic-gate /* Switch to OR mode: cache the old value of
11140Sstevel@tonic-gate * data->start_class */
11150Sstevel@tonic-gate StructCopy(data->start_class, &and_with,
11160Sstevel@tonic-gate struct regnode_charclass_class);
11170Sstevel@tonic-gate flags &= ~SCF_DO_STCLASS_AND;
11180Sstevel@tonic-gate StructCopy(&this_class, data->start_class,
11190Sstevel@tonic-gate struct regnode_charclass_class);
11200Sstevel@tonic-gate flags |= SCF_DO_STCLASS_OR;
11210Sstevel@tonic-gate data->start_class->flags |= ANYOF_EOS;
11220Sstevel@tonic-gate }
11230Sstevel@tonic-gate } else { /* Non-zero len */
11240Sstevel@tonic-gate if (flags & SCF_DO_STCLASS_OR) {
11250Sstevel@tonic-gate cl_or(pRExC_state, data->start_class, &this_class);
11260Sstevel@tonic-gate cl_and(data->start_class, &and_with);
11270Sstevel@tonic-gate }
11280Sstevel@tonic-gate else if (flags & SCF_DO_STCLASS_AND)
11290Sstevel@tonic-gate cl_and(data->start_class, &this_class);
11300Sstevel@tonic-gate flags &= ~SCF_DO_STCLASS;
11310Sstevel@tonic-gate }
11320Sstevel@tonic-gate if (!scan) /* It was not CURLYX, but CURLY. */
11330Sstevel@tonic-gate scan = next;
11340Sstevel@tonic-gate if (ckWARN(WARN_REGEXP)
11350Sstevel@tonic-gate /* ? quantifier ok, except for (?{ ... }) */
11360Sstevel@tonic-gate && (next_is_eval || !(mincount == 0 && maxcount == 1))
11370Sstevel@tonic-gate && (minnext == 0) && (deltanext == 0)
11380Sstevel@tonic-gate && data && !(data->flags & (SF_HAS_PAR|SF_IN_PAR))
11390Sstevel@tonic-gate && maxcount <= REG_INFTY/3) /* Complement check for big count */
11400Sstevel@tonic-gate {
11410Sstevel@tonic-gate vWARN(RExC_parse,
11420Sstevel@tonic-gate "Quantifier unexpected on zero-length expression");
11430Sstevel@tonic-gate }
11440Sstevel@tonic-gate
11450Sstevel@tonic-gate min += minnext * mincount;
11460Sstevel@tonic-gate is_inf_internal |= ((maxcount == REG_INFTY
11470Sstevel@tonic-gate && (minnext + deltanext) > 0)
11480Sstevel@tonic-gate || deltanext == I32_MAX);
11490Sstevel@tonic-gate is_inf |= is_inf_internal;
11500Sstevel@tonic-gate delta += (minnext + deltanext) * maxcount - minnext * mincount;
11510Sstevel@tonic-gate
11520Sstevel@tonic-gate /* Try powerful optimization CURLYX => CURLYN. */
11530Sstevel@tonic-gate if ( OP(oscan) == CURLYX && data
11540Sstevel@tonic-gate && data->flags & SF_IN_PAR
11550Sstevel@tonic-gate && !(data->flags & SF_HAS_EVAL)
11560Sstevel@tonic-gate && !deltanext && minnext == 1 ) {
11570Sstevel@tonic-gate /* Try to optimize to CURLYN. */
11580Sstevel@tonic-gate regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS;
11590Sstevel@tonic-gate regnode *nxt1 = nxt;
11600Sstevel@tonic-gate #ifdef DEBUGGING
11610Sstevel@tonic-gate regnode *nxt2;
11620Sstevel@tonic-gate #endif
11630Sstevel@tonic-gate
11640Sstevel@tonic-gate /* Skip open. */
11650Sstevel@tonic-gate nxt = regnext(nxt);
11660Sstevel@tonic-gate if (!strchr((char*)PL_simple,OP(nxt))
11670Sstevel@tonic-gate && !(PL_regkind[(U8)OP(nxt)] == EXACT
11680Sstevel@tonic-gate && STR_LEN(nxt) == 1))
11690Sstevel@tonic-gate goto nogo;
11700Sstevel@tonic-gate #ifdef DEBUGGING
11710Sstevel@tonic-gate nxt2 = nxt;
11720Sstevel@tonic-gate #endif
11730Sstevel@tonic-gate nxt = regnext(nxt);
11740Sstevel@tonic-gate if (OP(nxt) != CLOSE)
11750Sstevel@tonic-gate goto nogo;
11760Sstevel@tonic-gate /* Now we know that nxt2 is the only contents: */
11770Sstevel@tonic-gate oscan->flags = (U8)ARG(nxt);
11780Sstevel@tonic-gate OP(oscan) = CURLYN;
11790Sstevel@tonic-gate OP(nxt1) = NOTHING; /* was OPEN. */
11800Sstevel@tonic-gate #ifdef DEBUGGING
11810Sstevel@tonic-gate OP(nxt1 + 1) = OPTIMIZED; /* was count. */
11820Sstevel@tonic-gate NEXT_OFF(nxt1+ 1) = 0; /* just for consistancy. */
11830Sstevel@tonic-gate NEXT_OFF(nxt2) = 0; /* just for consistancy with CURLY. */
11840Sstevel@tonic-gate OP(nxt) = OPTIMIZED; /* was CLOSE. */
11850Sstevel@tonic-gate OP(nxt + 1) = OPTIMIZED; /* was count. */
11860Sstevel@tonic-gate NEXT_OFF(nxt+ 1) = 0; /* just for consistancy. */
11870Sstevel@tonic-gate #endif
11880Sstevel@tonic-gate }
11890Sstevel@tonic-gate nogo:
11900Sstevel@tonic-gate
11910Sstevel@tonic-gate /* Try optimization CURLYX => CURLYM. */
11920Sstevel@tonic-gate if ( OP(oscan) == CURLYX && data
11930Sstevel@tonic-gate && !(data->flags & SF_HAS_PAR)
11940Sstevel@tonic-gate && !(data->flags & SF_HAS_EVAL)
11950Sstevel@tonic-gate && !deltanext ) {
11960Sstevel@tonic-gate /* XXXX How to optimize if data == 0? */
11970Sstevel@tonic-gate /* Optimize to a simpler form. */
11980Sstevel@tonic-gate regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN */
11990Sstevel@tonic-gate regnode *nxt2;
12000Sstevel@tonic-gate
12010Sstevel@tonic-gate OP(oscan) = CURLYM;
12020Sstevel@tonic-gate while ( (nxt2 = regnext(nxt)) /* skip over embedded stuff*/
12030Sstevel@tonic-gate && (OP(nxt2) != WHILEM))
12040Sstevel@tonic-gate nxt = nxt2;
12050Sstevel@tonic-gate OP(nxt2) = SUCCEED; /* Whas WHILEM */
12060Sstevel@tonic-gate /* Need to optimize away parenths. */
12070Sstevel@tonic-gate if (data->flags & SF_IN_PAR) {
12080Sstevel@tonic-gate /* Set the parenth number. */
12090Sstevel@tonic-gate regnode *nxt1 = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN*/
12100Sstevel@tonic-gate
12110Sstevel@tonic-gate if (OP(nxt) != CLOSE)
12120Sstevel@tonic-gate FAIL("Panic opt close");
12130Sstevel@tonic-gate oscan->flags = (U8)ARG(nxt);
12140Sstevel@tonic-gate OP(nxt1) = OPTIMIZED; /* was OPEN. */
12150Sstevel@tonic-gate OP(nxt) = OPTIMIZED; /* was CLOSE. */
12160Sstevel@tonic-gate #ifdef DEBUGGING
12170Sstevel@tonic-gate OP(nxt1 + 1) = OPTIMIZED; /* was count. */
12180Sstevel@tonic-gate OP(nxt + 1) = OPTIMIZED; /* was count. */
12190Sstevel@tonic-gate NEXT_OFF(nxt1 + 1) = 0; /* just for consistancy. */
12200Sstevel@tonic-gate NEXT_OFF(nxt + 1) = 0; /* just for consistancy. */
12210Sstevel@tonic-gate #endif
12220Sstevel@tonic-gate #if 0
12230Sstevel@tonic-gate while ( nxt1 && (OP(nxt1) != WHILEM)) {
12240Sstevel@tonic-gate regnode *nnxt = regnext(nxt1);
12250Sstevel@tonic-gate
12260Sstevel@tonic-gate if (nnxt == nxt) {
12270Sstevel@tonic-gate if (reg_off_by_arg[OP(nxt1)])
12280Sstevel@tonic-gate ARG_SET(nxt1, nxt2 - nxt1);
12290Sstevel@tonic-gate else if (nxt2 - nxt1 < U16_MAX)
12300Sstevel@tonic-gate NEXT_OFF(nxt1) = nxt2 - nxt1;
12310Sstevel@tonic-gate else
12320Sstevel@tonic-gate OP(nxt) = NOTHING; /* Cannot beautify */
12330Sstevel@tonic-gate }
12340Sstevel@tonic-gate nxt1 = nnxt;
12350Sstevel@tonic-gate }
12360Sstevel@tonic-gate #endif
12370Sstevel@tonic-gate /* Optimize again: */
12380Sstevel@tonic-gate study_chunk(pRExC_state, &nxt1, &deltanext, nxt,
12390Sstevel@tonic-gate NULL, 0);
12400Sstevel@tonic-gate }
12410Sstevel@tonic-gate else
12420Sstevel@tonic-gate oscan->flags = 0;
12430Sstevel@tonic-gate }
12440Sstevel@tonic-gate else if ((OP(oscan) == CURLYX)
12450Sstevel@tonic-gate && (flags & SCF_WHILEM_VISITED_POS)
12460Sstevel@tonic-gate /* See the comment on a similar expression above.
12470Sstevel@tonic-gate However, this time it not a subexpression
12480Sstevel@tonic-gate we care about, but the expression itself. */
12490Sstevel@tonic-gate && (maxcount == REG_INFTY)
12500Sstevel@tonic-gate && data && ++data->whilem_c < 16) {
12510Sstevel@tonic-gate /* This stays as CURLYX, we can put the count/of pair. */
12520Sstevel@tonic-gate /* Find WHILEM (as in regexec.c) */
12530Sstevel@tonic-gate regnode *nxt = oscan + NEXT_OFF(oscan);
12540Sstevel@tonic-gate
12550Sstevel@tonic-gate if (OP(PREVOPER(nxt)) == NOTHING) /* LONGJMP */
12560Sstevel@tonic-gate nxt += ARG(nxt);
12570Sstevel@tonic-gate PREVOPER(nxt)->flags = (U8)(data->whilem_c
12580Sstevel@tonic-gate | (RExC_whilem_seen << 4)); /* On WHILEM */
12590Sstevel@tonic-gate }
12600Sstevel@tonic-gate if (data && fl & (SF_HAS_PAR|SF_IN_PAR))
12610Sstevel@tonic-gate pars++;
12620Sstevel@tonic-gate if (flags & SCF_DO_SUBSTR) {
12630Sstevel@tonic-gate SV *last_str = Nullsv;
12640Sstevel@tonic-gate int counted = mincount != 0;
12650Sstevel@tonic-gate
12660Sstevel@tonic-gate if (data->last_end > 0 && mincount != 0) { /* Ends with a string. */
12670Sstevel@tonic-gate #if defined(SPARC64_GCC_WORKAROUND)
12680Sstevel@tonic-gate I32 b = 0;
12690Sstevel@tonic-gate STRLEN l = 0;
12700Sstevel@tonic-gate char *s = NULL;
12710Sstevel@tonic-gate I32 old = 0;
12720Sstevel@tonic-gate
12730Sstevel@tonic-gate if (pos_before >= data->last_start_min)
12740Sstevel@tonic-gate b = pos_before;
12750Sstevel@tonic-gate else
12760Sstevel@tonic-gate b = data->last_start_min;
12770Sstevel@tonic-gate
12780Sstevel@tonic-gate l = 0;
12790Sstevel@tonic-gate s = SvPV(data->last_found, l);
12800Sstevel@tonic-gate old = b - data->last_start_min;
12810Sstevel@tonic-gate
12820Sstevel@tonic-gate #else
12830Sstevel@tonic-gate I32 b = pos_before >= data->last_start_min
12840Sstevel@tonic-gate ? pos_before : data->last_start_min;
12850Sstevel@tonic-gate STRLEN l;
12860Sstevel@tonic-gate char *s = SvPV(data->last_found, l);
12870Sstevel@tonic-gate I32 old = b - data->last_start_min;
12880Sstevel@tonic-gate #endif
12890Sstevel@tonic-gate
12900Sstevel@tonic-gate if (UTF)
12910Sstevel@tonic-gate old = utf8_hop((U8*)s, old) - (U8*)s;
12920Sstevel@tonic-gate
12930Sstevel@tonic-gate l -= old;
12940Sstevel@tonic-gate /* Get the added string: */
12950Sstevel@tonic-gate last_str = newSVpvn(s + old, l);
12960Sstevel@tonic-gate if (UTF)
12970Sstevel@tonic-gate SvUTF8_on(last_str);
12980Sstevel@tonic-gate if (deltanext == 0 && pos_before == b) {
12990Sstevel@tonic-gate /* What was added is a constant string */
13000Sstevel@tonic-gate if (mincount > 1) {
13010Sstevel@tonic-gate SvGROW(last_str, (mincount * l) + 1);
13020Sstevel@tonic-gate repeatcpy(SvPVX(last_str) + l,
13030Sstevel@tonic-gate SvPVX(last_str), l, mincount - 1);
13040Sstevel@tonic-gate SvCUR(last_str) *= mincount;
13050Sstevel@tonic-gate /* Add additional parts. */
13060Sstevel@tonic-gate SvCUR_set(data->last_found,
13070Sstevel@tonic-gate SvCUR(data->last_found) - l);
13080Sstevel@tonic-gate sv_catsv(data->last_found, last_str);
13090Sstevel@tonic-gate {
13100Sstevel@tonic-gate SV * sv = data->last_found;
13110Sstevel@tonic-gate MAGIC *mg =
13120Sstevel@tonic-gate SvUTF8(sv) && SvMAGICAL(sv) ?
13130Sstevel@tonic-gate mg_find(sv, PERL_MAGIC_utf8) : NULL;
13140Sstevel@tonic-gate if (mg && mg->mg_len >= 0)
13150Sstevel@tonic-gate mg->mg_len += CHR_SVLEN(last_str);
13160Sstevel@tonic-gate }
13170Sstevel@tonic-gate data->last_end += l * (mincount - 1);
13180Sstevel@tonic-gate }
13190Sstevel@tonic-gate } else {
13200Sstevel@tonic-gate /* start offset must point into the last copy */
13210Sstevel@tonic-gate data->last_start_min += minnext * (mincount - 1);
13220Sstevel@tonic-gate data->last_start_max += is_inf ? I32_MAX
13230Sstevel@tonic-gate : (maxcount - 1) * (minnext + data->pos_delta);
13240Sstevel@tonic-gate }
13250Sstevel@tonic-gate }
13260Sstevel@tonic-gate /* It is counted once already... */
13270Sstevel@tonic-gate data->pos_min += minnext * (mincount - counted);
13280Sstevel@tonic-gate data->pos_delta += - counted * deltanext +
13290Sstevel@tonic-gate (minnext + deltanext) * maxcount - minnext * mincount;
13300Sstevel@tonic-gate if (mincount != maxcount) {
13310Sstevel@tonic-gate /* Cannot extend fixed substrings found inside
13320Sstevel@tonic-gate the group. */
13330Sstevel@tonic-gate scan_commit(pRExC_state,data);
13340Sstevel@tonic-gate if (mincount && last_str) {
13350Sstevel@tonic-gate sv_setsv(data->last_found, last_str);
13360Sstevel@tonic-gate data->last_end = data->pos_min;
13370Sstevel@tonic-gate data->last_start_min =
13380Sstevel@tonic-gate data->pos_min - CHR_SVLEN(last_str);
13390Sstevel@tonic-gate data->last_start_max = is_inf
13400Sstevel@tonic-gate ? I32_MAX
13410Sstevel@tonic-gate : data->pos_min + data->pos_delta
13420Sstevel@tonic-gate - CHR_SVLEN(last_str);
13430Sstevel@tonic-gate }
13440Sstevel@tonic-gate data->longest = &(data->longest_float);
13450Sstevel@tonic-gate }
13460Sstevel@tonic-gate SvREFCNT_dec(last_str);
13470Sstevel@tonic-gate }
13480Sstevel@tonic-gate if (data && (fl & SF_HAS_EVAL))
13490Sstevel@tonic-gate data->flags |= SF_HAS_EVAL;
13500Sstevel@tonic-gate optimize_curly_tail:
13510Sstevel@tonic-gate if (OP(oscan) != CURLYX) {
13520Sstevel@tonic-gate while (PL_regkind[(U8)OP(next = regnext(oscan))] == NOTHING
13530Sstevel@tonic-gate && NEXT_OFF(next))
13540Sstevel@tonic-gate NEXT_OFF(oscan) += NEXT_OFF(next);
13550Sstevel@tonic-gate }
13560Sstevel@tonic-gate continue;
13570Sstevel@tonic-gate default: /* REF and CLUMP only? */
13580Sstevel@tonic-gate if (flags & SCF_DO_SUBSTR) {
13590Sstevel@tonic-gate scan_commit(pRExC_state,data); /* Cannot expect anything... */
13600Sstevel@tonic-gate data->longest = &(data->longest_float);
13610Sstevel@tonic-gate }
13620Sstevel@tonic-gate is_inf = is_inf_internal = 1;
13630Sstevel@tonic-gate if (flags & SCF_DO_STCLASS_OR)
13640Sstevel@tonic-gate cl_anything(pRExC_state, data->start_class);
13650Sstevel@tonic-gate flags &= ~SCF_DO_STCLASS;
13660Sstevel@tonic-gate break;
13670Sstevel@tonic-gate }
13680Sstevel@tonic-gate }
13690Sstevel@tonic-gate else if (strchr((char*)PL_simple,OP(scan))) {
13700Sstevel@tonic-gate int value = 0;
13710Sstevel@tonic-gate
13720Sstevel@tonic-gate if (flags & SCF_DO_SUBSTR) {
13730Sstevel@tonic-gate scan_commit(pRExC_state,data);
13740Sstevel@tonic-gate data->pos_min++;
13750Sstevel@tonic-gate }
13760Sstevel@tonic-gate min++;
13770Sstevel@tonic-gate if (flags & SCF_DO_STCLASS) {
13780Sstevel@tonic-gate data->start_class->flags &= ~ANYOF_EOS; /* No match on empty */
13790Sstevel@tonic-gate
13800Sstevel@tonic-gate /* Some of the logic below assumes that switching
13810Sstevel@tonic-gate locale on will only add false positives. */
13820Sstevel@tonic-gate switch (PL_regkind[(U8)OP(scan)]) {
13830Sstevel@tonic-gate case SANY:
13840Sstevel@tonic-gate default:
13850Sstevel@tonic-gate do_default:
13860Sstevel@tonic-gate /* Perl_croak(aTHX_ "panic: unexpected simple REx opcode %d", OP(scan)); */
13870Sstevel@tonic-gate if (flags & SCF_DO_STCLASS_OR) /* Allow everything */
13880Sstevel@tonic-gate cl_anything(pRExC_state, data->start_class);
13890Sstevel@tonic-gate break;
13900Sstevel@tonic-gate case REG_ANY:
13910Sstevel@tonic-gate if (OP(scan) == SANY)
13920Sstevel@tonic-gate goto do_default;
13930Sstevel@tonic-gate if (flags & SCF_DO_STCLASS_OR) { /* Everything but \n */
13940Sstevel@tonic-gate value = (ANYOF_BITMAP_TEST(data->start_class,'\n')
13950Sstevel@tonic-gate || (data->start_class->flags & ANYOF_CLASS));
13960Sstevel@tonic-gate cl_anything(pRExC_state, data->start_class);
13970Sstevel@tonic-gate }
13980Sstevel@tonic-gate if (flags & SCF_DO_STCLASS_AND || !value)
13990Sstevel@tonic-gate ANYOF_BITMAP_CLEAR(data->start_class,'\n');
14000Sstevel@tonic-gate break;
14010Sstevel@tonic-gate case ANYOF:
14020Sstevel@tonic-gate if (flags & SCF_DO_STCLASS_AND)
14030Sstevel@tonic-gate cl_and(data->start_class,
14040Sstevel@tonic-gate (struct regnode_charclass_class*)scan);
14050Sstevel@tonic-gate else
14060Sstevel@tonic-gate cl_or(pRExC_state, data->start_class,
14070Sstevel@tonic-gate (struct regnode_charclass_class*)scan);
14080Sstevel@tonic-gate break;
14090Sstevel@tonic-gate case ALNUM:
14100Sstevel@tonic-gate if (flags & SCF_DO_STCLASS_AND) {
14110Sstevel@tonic-gate if (!(data->start_class->flags & ANYOF_LOCALE)) {
14120Sstevel@tonic-gate ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NALNUM);
14130Sstevel@tonic-gate for (value = 0; value < 256; value++)
14140Sstevel@tonic-gate if (!isALNUM(value))
14150Sstevel@tonic-gate ANYOF_BITMAP_CLEAR(data->start_class, value);
14160Sstevel@tonic-gate }
14170Sstevel@tonic-gate }
14180Sstevel@tonic-gate else {
14190Sstevel@tonic-gate if (data->start_class->flags & ANYOF_LOCALE)
14200Sstevel@tonic-gate ANYOF_CLASS_SET(data->start_class,ANYOF_ALNUM);
14210Sstevel@tonic-gate else {
14220Sstevel@tonic-gate for (value = 0; value < 256; value++)
14230Sstevel@tonic-gate if (isALNUM(value))
14240Sstevel@tonic-gate ANYOF_BITMAP_SET(data->start_class, value);
14250Sstevel@tonic-gate }
14260Sstevel@tonic-gate }
14270Sstevel@tonic-gate break;
14280Sstevel@tonic-gate case ALNUML:
14290Sstevel@tonic-gate if (flags & SCF_DO_STCLASS_AND) {
14300Sstevel@tonic-gate if (data->start_class->flags & ANYOF_LOCALE)
14310Sstevel@tonic-gate ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NALNUM);
14320Sstevel@tonic-gate }
14330Sstevel@tonic-gate else {
14340Sstevel@tonic-gate ANYOF_CLASS_SET(data->start_class,ANYOF_ALNUM);
14350Sstevel@tonic-gate data->start_class->flags |= ANYOF_LOCALE;
14360Sstevel@tonic-gate }
14370Sstevel@tonic-gate break;
14380Sstevel@tonic-gate case NALNUM:
14390Sstevel@tonic-gate if (flags & SCF_DO_STCLASS_AND) {
14400Sstevel@tonic-gate if (!(data->start_class->flags & ANYOF_LOCALE)) {
14410Sstevel@tonic-gate ANYOF_CLASS_CLEAR(data->start_class,ANYOF_ALNUM);
14420Sstevel@tonic-gate for (value = 0; value < 256; value++)
14430Sstevel@tonic-gate if (isALNUM(value))
14440Sstevel@tonic-gate ANYOF_BITMAP_CLEAR(data->start_class, value);
14450Sstevel@tonic-gate }
14460Sstevel@tonic-gate }
14470Sstevel@tonic-gate else {
14480Sstevel@tonic-gate if (data->start_class->flags & ANYOF_LOCALE)
14490Sstevel@tonic-gate ANYOF_CLASS_SET(data->start_class,ANYOF_NALNUM);
14500Sstevel@tonic-gate else {
14510Sstevel@tonic-gate for (value = 0; value < 256; value++)
14520Sstevel@tonic-gate if (!isALNUM(value))
14530Sstevel@tonic-gate ANYOF_BITMAP_SET(data->start_class, value);
14540Sstevel@tonic-gate }
14550Sstevel@tonic-gate }
14560Sstevel@tonic-gate break;
14570Sstevel@tonic-gate case NALNUML:
14580Sstevel@tonic-gate if (flags & SCF_DO_STCLASS_AND) {
14590Sstevel@tonic-gate if (data->start_class->flags & ANYOF_LOCALE)
14600Sstevel@tonic-gate ANYOF_CLASS_CLEAR(data->start_class,ANYOF_ALNUM);
14610Sstevel@tonic-gate }
14620Sstevel@tonic-gate else {
14630Sstevel@tonic-gate data->start_class->flags |= ANYOF_LOCALE;
14640Sstevel@tonic-gate ANYOF_CLASS_SET(data->start_class,ANYOF_NALNUM);
14650Sstevel@tonic-gate }
14660Sstevel@tonic-gate break;
14670Sstevel@tonic-gate case SPACE:
14680Sstevel@tonic-gate if (flags & SCF_DO_STCLASS_AND) {
14690Sstevel@tonic-gate if (!(data->start_class->flags & ANYOF_LOCALE)) {
14700Sstevel@tonic-gate ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NSPACE);
14710Sstevel@tonic-gate for (value = 0; value < 256; value++)
14720Sstevel@tonic-gate if (!isSPACE(value))
14730Sstevel@tonic-gate ANYOF_BITMAP_CLEAR(data->start_class, value);
14740Sstevel@tonic-gate }
14750Sstevel@tonic-gate }
14760Sstevel@tonic-gate else {
14770Sstevel@tonic-gate if (data->start_class->flags & ANYOF_LOCALE)
14780Sstevel@tonic-gate ANYOF_CLASS_SET(data->start_class,ANYOF_SPACE);
14790Sstevel@tonic-gate else {
14800Sstevel@tonic-gate for (value = 0; value < 256; value++)
14810Sstevel@tonic-gate if (isSPACE(value))
14820Sstevel@tonic-gate ANYOF_BITMAP_SET(data->start_class, value);
14830Sstevel@tonic-gate }
14840Sstevel@tonic-gate }
14850Sstevel@tonic-gate break;
14860Sstevel@tonic-gate case SPACEL:
14870Sstevel@tonic-gate if (flags & SCF_DO_STCLASS_AND) {
14880Sstevel@tonic-gate if (data->start_class->flags & ANYOF_LOCALE)
14890Sstevel@tonic-gate ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NSPACE);
14900Sstevel@tonic-gate }
14910Sstevel@tonic-gate else {
14920Sstevel@tonic-gate data->start_class->flags |= ANYOF_LOCALE;
14930Sstevel@tonic-gate ANYOF_CLASS_SET(data->start_class,ANYOF_SPACE);
14940Sstevel@tonic-gate }
14950Sstevel@tonic-gate break;
14960Sstevel@tonic-gate case NSPACE:
14970Sstevel@tonic-gate if (flags & SCF_DO_STCLASS_AND) {
14980Sstevel@tonic-gate if (!(data->start_class->flags & ANYOF_LOCALE)) {
14990Sstevel@tonic-gate ANYOF_CLASS_CLEAR(data->start_class,ANYOF_SPACE);
15000Sstevel@tonic-gate for (value = 0; value < 256; value++)
15010Sstevel@tonic-gate if (isSPACE(value))
15020Sstevel@tonic-gate ANYOF_BITMAP_CLEAR(data->start_class, value);
15030Sstevel@tonic-gate }
15040Sstevel@tonic-gate }
15050Sstevel@tonic-gate else {
15060Sstevel@tonic-gate if (data->start_class->flags & ANYOF_LOCALE)
15070Sstevel@tonic-gate ANYOF_CLASS_SET(data->start_class,ANYOF_NSPACE);
15080Sstevel@tonic-gate else {
15090Sstevel@tonic-gate for (value = 0; value < 256; value++)
15100Sstevel@tonic-gate if (!isSPACE(value))
15110Sstevel@tonic-gate ANYOF_BITMAP_SET(data->start_class, value);
15120Sstevel@tonic-gate }
15130Sstevel@tonic-gate }
15140Sstevel@tonic-gate break;
15150Sstevel@tonic-gate case NSPACEL:
15160Sstevel@tonic-gate if (flags & SCF_DO_STCLASS_AND) {
15170Sstevel@tonic-gate if (data->start_class->flags & ANYOF_LOCALE) {
15180Sstevel@tonic-gate ANYOF_CLASS_CLEAR(data->start_class,ANYOF_SPACE);
15190Sstevel@tonic-gate for (value = 0; value < 256; value++)
15200Sstevel@tonic-gate if (!isSPACE(value))
15210Sstevel@tonic-gate ANYOF_BITMAP_CLEAR(data->start_class, value);
15220Sstevel@tonic-gate }
15230Sstevel@tonic-gate }
15240Sstevel@tonic-gate else {
15250Sstevel@tonic-gate data->start_class->flags |= ANYOF_LOCALE;
15260Sstevel@tonic-gate ANYOF_CLASS_SET(data->start_class,ANYOF_NSPACE);
15270Sstevel@tonic-gate }
15280Sstevel@tonic-gate break;
15290Sstevel@tonic-gate case DIGIT:
15300Sstevel@tonic-gate if (flags & SCF_DO_STCLASS_AND) {
15310Sstevel@tonic-gate ANYOF_CLASS_CLEAR(data->start_class,ANYOF_NDIGIT);
15320Sstevel@tonic-gate for (value = 0; value < 256; value++)
15330Sstevel@tonic-gate if (!isDIGIT(value))
15340Sstevel@tonic-gate ANYOF_BITMAP_CLEAR(data->start_class, value);
15350Sstevel@tonic-gate }
15360Sstevel@tonic-gate else {
15370Sstevel@tonic-gate if (data->start_class->flags & ANYOF_LOCALE)
15380Sstevel@tonic-gate ANYOF_CLASS_SET(data->start_class,ANYOF_DIGIT);
15390Sstevel@tonic-gate else {
15400Sstevel@tonic-gate for (value = 0; value < 256; value++)
15410Sstevel@tonic-gate if (isDIGIT(value))
15420Sstevel@tonic-gate ANYOF_BITMAP_SET(data->start_class, value);
15430Sstevel@tonic-gate }
15440Sstevel@tonic-gate }
15450Sstevel@tonic-gate break;
15460Sstevel@tonic-gate case NDIGIT:
15470Sstevel@tonic-gate if (flags & SCF_DO_STCLASS_AND) {
15480Sstevel@tonic-gate ANYOF_CLASS_CLEAR(data->start_class,ANYOF_DIGIT);
15490Sstevel@tonic-gate for (value = 0; value < 256; value++)
15500Sstevel@tonic-gate if (isDIGIT(value))
15510Sstevel@tonic-gate ANYOF_BITMAP_CLEAR(data->start_class, value);
15520Sstevel@tonic-gate }
15530Sstevel@tonic-gate else {
15540Sstevel@tonic-gate if (data->start_class->flags & ANYOF_LOCALE)
15550Sstevel@tonic-gate ANYOF_CLASS_SET(data->start_class,ANYOF_NDIGIT);
15560Sstevel@tonic-gate else {
15570Sstevel@tonic-gate for (value = 0; value < 256; value++)
15580Sstevel@tonic-gate if (!isDIGIT(value))
15590Sstevel@tonic-gate ANYOF_BITMAP_SET(data->start_class, value);
15600Sstevel@tonic-gate }
15610Sstevel@tonic-gate }
15620Sstevel@tonic-gate break;
15630Sstevel@tonic-gate }
15640Sstevel@tonic-gate if (flags & SCF_DO_STCLASS_OR)
15650Sstevel@tonic-gate cl_and(data->start_class, &and_with);
15660Sstevel@tonic-gate flags &= ~SCF_DO_STCLASS;
15670Sstevel@tonic-gate }
15680Sstevel@tonic-gate }
15690Sstevel@tonic-gate else if (PL_regkind[(U8)OP(scan)] == EOL && flags & SCF_DO_SUBSTR) {
15700Sstevel@tonic-gate data->flags |= (OP(scan) == MEOL
15710Sstevel@tonic-gate ? SF_BEFORE_MEOL
15720Sstevel@tonic-gate : SF_BEFORE_SEOL);
15730Sstevel@tonic-gate }
15740Sstevel@tonic-gate else if ( PL_regkind[(U8)OP(scan)] == BRANCHJ
15750Sstevel@tonic-gate /* Lookbehind, or need to calculate parens/evals/stclass: */
15760Sstevel@tonic-gate && (scan->flags || data || (flags & SCF_DO_STCLASS))
15770Sstevel@tonic-gate && (OP(scan) == IFMATCH || OP(scan) == UNLESSM)) {
15780Sstevel@tonic-gate /* Lookahead/lookbehind */
15790Sstevel@tonic-gate I32 deltanext, minnext, fake = 0;
15800Sstevel@tonic-gate regnode *nscan;
15810Sstevel@tonic-gate struct regnode_charclass_class intrnl;
15820Sstevel@tonic-gate int f = 0;
15830Sstevel@tonic-gate
15840Sstevel@tonic-gate data_fake.flags = 0;
15850Sstevel@tonic-gate if (data) {
15860Sstevel@tonic-gate data_fake.whilem_c = data->whilem_c;
15870Sstevel@tonic-gate data_fake.last_closep = data->last_closep;
15880Sstevel@tonic-gate }
15890Sstevel@tonic-gate else
15900Sstevel@tonic-gate data_fake.last_closep = &fake;
15910Sstevel@tonic-gate if ( flags & SCF_DO_STCLASS && !scan->flags
15920Sstevel@tonic-gate && OP(scan) == IFMATCH ) { /* Lookahead */
15930Sstevel@tonic-gate cl_init(pRExC_state, &intrnl);
15940Sstevel@tonic-gate data_fake.start_class = &intrnl;
15950Sstevel@tonic-gate f |= SCF_DO_STCLASS_AND;
15960Sstevel@tonic-gate }
15970Sstevel@tonic-gate if (flags & SCF_WHILEM_VISITED_POS)
15980Sstevel@tonic-gate f |= SCF_WHILEM_VISITED_POS;
15990Sstevel@tonic-gate next = regnext(scan);
16000Sstevel@tonic-gate nscan = NEXTOPER(NEXTOPER(scan));
16010Sstevel@tonic-gate minnext = study_chunk(pRExC_state, &nscan, &deltanext, last, &data_fake, f);
16020Sstevel@tonic-gate if (scan->flags) {
16030Sstevel@tonic-gate if (deltanext) {
16040Sstevel@tonic-gate vFAIL("Variable length lookbehind not implemented");
16050Sstevel@tonic-gate }
16060Sstevel@tonic-gate else if (minnext > U8_MAX) {
16070Sstevel@tonic-gate vFAIL2("Lookbehind longer than %"UVuf" not implemented", (UV)U8_MAX);
16080Sstevel@tonic-gate }
16090Sstevel@tonic-gate scan->flags = (U8)minnext;
16100Sstevel@tonic-gate }
16110Sstevel@tonic-gate if (data && data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
16120Sstevel@tonic-gate pars++;
16130Sstevel@tonic-gate if (data && (data_fake.flags & SF_HAS_EVAL))
16140Sstevel@tonic-gate data->flags |= SF_HAS_EVAL;
16150Sstevel@tonic-gate if (data)
16160Sstevel@tonic-gate data->whilem_c = data_fake.whilem_c;
16170Sstevel@tonic-gate if (f & SCF_DO_STCLASS_AND) {
16180Sstevel@tonic-gate int was = (data->start_class->flags & ANYOF_EOS);
16190Sstevel@tonic-gate
16200Sstevel@tonic-gate cl_and(data->start_class, &intrnl);
16210Sstevel@tonic-gate if (was)
16220Sstevel@tonic-gate data->start_class->flags |= ANYOF_EOS;
16230Sstevel@tonic-gate }
16240Sstevel@tonic-gate }
16250Sstevel@tonic-gate else if (OP(scan) == OPEN) {
16260Sstevel@tonic-gate pars++;
16270Sstevel@tonic-gate }
16280Sstevel@tonic-gate else if (OP(scan) == CLOSE) {
16290Sstevel@tonic-gate if ((I32)ARG(scan) == is_par) {
16300Sstevel@tonic-gate next = regnext(scan);
16310Sstevel@tonic-gate
16320Sstevel@tonic-gate if ( next && (OP(next) != WHILEM) && next < last)
16330Sstevel@tonic-gate is_par = 0; /* Disable optimization */
16340Sstevel@tonic-gate }
16350Sstevel@tonic-gate if (data)
16360Sstevel@tonic-gate *(data->last_closep) = ARG(scan);
16370Sstevel@tonic-gate }
16380Sstevel@tonic-gate else if (OP(scan) == EVAL) {
16390Sstevel@tonic-gate if (data)
16400Sstevel@tonic-gate data->flags |= SF_HAS_EVAL;
16410Sstevel@tonic-gate }
16420Sstevel@tonic-gate else if (OP(scan) == LOGICAL && scan->flags == 2) { /* Embedded follows */
16430Sstevel@tonic-gate if (flags & SCF_DO_SUBSTR) {
16440Sstevel@tonic-gate scan_commit(pRExC_state,data);
16450Sstevel@tonic-gate data->longest = &(data->longest_float);
16460Sstevel@tonic-gate }
16470Sstevel@tonic-gate is_inf = is_inf_internal = 1;
16480Sstevel@tonic-gate if (flags & SCF_DO_STCLASS_OR) /* Allow everything */
16490Sstevel@tonic-gate cl_anything(pRExC_state, data->start_class);
16500Sstevel@tonic-gate flags &= ~SCF_DO_STCLASS;
16510Sstevel@tonic-gate }
16520Sstevel@tonic-gate /* Else: zero-length, ignore. */
16530Sstevel@tonic-gate scan = regnext(scan);
16540Sstevel@tonic-gate }
16550Sstevel@tonic-gate
16560Sstevel@tonic-gate finish:
16570Sstevel@tonic-gate *scanp = scan;
16580Sstevel@tonic-gate *deltap = is_inf_internal ? I32_MAX : delta;
16590Sstevel@tonic-gate if (flags & SCF_DO_SUBSTR && is_inf)
16600Sstevel@tonic-gate data->pos_delta = I32_MAX - data->pos_min;
16610Sstevel@tonic-gate if (is_par > U8_MAX)
16620Sstevel@tonic-gate is_par = 0;
16630Sstevel@tonic-gate if (is_par && pars==1 && data) {
16640Sstevel@tonic-gate data->flags |= SF_IN_PAR;
16650Sstevel@tonic-gate data->flags &= ~SF_HAS_PAR;
16660Sstevel@tonic-gate }
16670Sstevel@tonic-gate else if (pars && data) {
16680Sstevel@tonic-gate data->flags |= SF_HAS_PAR;
16690Sstevel@tonic-gate data->flags &= ~SF_IN_PAR;
16700Sstevel@tonic-gate }
16710Sstevel@tonic-gate if (flags & SCF_DO_STCLASS_OR)
16720Sstevel@tonic-gate cl_and(data->start_class, &and_with);
16730Sstevel@tonic-gate return min;
16740Sstevel@tonic-gate }
16750Sstevel@tonic-gate
16760Sstevel@tonic-gate STATIC I32
S_add_data(pTHX_ RExC_state_t * pRExC_state,I32 n,char * s)16770Sstevel@tonic-gate S_add_data(pTHX_ RExC_state_t *pRExC_state, I32 n, char *s)
16780Sstevel@tonic-gate {
16790Sstevel@tonic-gate if (RExC_rx->data) {
16800Sstevel@tonic-gate Renewc(RExC_rx->data,
16810Sstevel@tonic-gate sizeof(*RExC_rx->data) + sizeof(void*) * (RExC_rx->data->count + n - 1),
16820Sstevel@tonic-gate char, struct reg_data);
16830Sstevel@tonic-gate Renew(RExC_rx->data->what, RExC_rx->data->count + n, U8);
16840Sstevel@tonic-gate RExC_rx->data->count += n;
16850Sstevel@tonic-gate }
16860Sstevel@tonic-gate else {
16870Sstevel@tonic-gate Newc(1207, RExC_rx->data, sizeof(*RExC_rx->data) + sizeof(void*) * (n - 1),
16880Sstevel@tonic-gate char, struct reg_data);
16890Sstevel@tonic-gate New(1208, RExC_rx->data->what, n, U8);
16900Sstevel@tonic-gate RExC_rx->data->count = n;
16910Sstevel@tonic-gate }
16920Sstevel@tonic-gate Copy(s, RExC_rx->data->what + RExC_rx->data->count - n, n, U8);
16930Sstevel@tonic-gate return RExC_rx->data->count - n;
16940Sstevel@tonic-gate }
16950Sstevel@tonic-gate
16960Sstevel@tonic-gate void
Perl_reginitcolors(pTHX)16970Sstevel@tonic-gate Perl_reginitcolors(pTHX)
16980Sstevel@tonic-gate {
16990Sstevel@tonic-gate int i = 0;
17000Sstevel@tonic-gate char *s = PerlEnv_getenv("PERL_RE_COLORS");
17010Sstevel@tonic-gate
17020Sstevel@tonic-gate if (s) {
17030Sstevel@tonic-gate PL_colors[0] = s = savepv(s);
17040Sstevel@tonic-gate while (++i < 6) {
17050Sstevel@tonic-gate s = strchr(s, '\t');
17060Sstevel@tonic-gate if (s) {
17070Sstevel@tonic-gate *s = '\0';
17080Sstevel@tonic-gate PL_colors[i] = ++s;
17090Sstevel@tonic-gate }
17100Sstevel@tonic-gate else
17110Sstevel@tonic-gate PL_colors[i] = s = "";
17120Sstevel@tonic-gate }
17130Sstevel@tonic-gate } else {
17140Sstevel@tonic-gate while (i < 6)
17150Sstevel@tonic-gate PL_colors[i++] = "";
17160Sstevel@tonic-gate }
17170Sstevel@tonic-gate PL_colorset = 1;
17180Sstevel@tonic-gate }
17190Sstevel@tonic-gate
17200Sstevel@tonic-gate
17210Sstevel@tonic-gate /*
17220Sstevel@tonic-gate - pregcomp - compile a regular expression into internal code
17230Sstevel@tonic-gate *
17240Sstevel@tonic-gate * We can't allocate space until we know how big the compiled form will be,
17250Sstevel@tonic-gate * but we can't compile it (and thus know how big it is) until we've got a
17260Sstevel@tonic-gate * place to put the code. So we cheat: we compile it twice, once with code
17270Sstevel@tonic-gate * generation turned off and size counting turned on, and once "for real".
17280Sstevel@tonic-gate * This also means that we don't allocate space until we are sure that the
17290Sstevel@tonic-gate * thing really will compile successfully, and we never have to move the
17300Sstevel@tonic-gate * code and thus invalidate pointers into it. (Note that it has to be in
17310Sstevel@tonic-gate * one piece because free() must be able to free it all.) [NB: not true in perl]
17320Sstevel@tonic-gate *
17330Sstevel@tonic-gate * Beware that the optimization-preparation code in here knows about some
17340Sstevel@tonic-gate * of the structure of the compiled regexp. [I'll say.]
17350Sstevel@tonic-gate */
17360Sstevel@tonic-gate regexp *
Perl_pregcomp(pTHX_ char * exp,char * xend,PMOP * pm)17370Sstevel@tonic-gate Perl_pregcomp(pTHX_ char *exp, char *xend, PMOP *pm)
17380Sstevel@tonic-gate {
17390Sstevel@tonic-gate register regexp *r;
17400Sstevel@tonic-gate regnode *scan;
17410Sstevel@tonic-gate regnode *first;
17420Sstevel@tonic-gate I32 flags;
17430Sstevel@tonic-gate I32 minlen = 0;
17440Sstevel@tonic-gate I32 sawplus = 0;
17450Sstevel@tonic-gate I32 sawopen = 0;
17460Sstevel@tonic-gate scan_data_t data;
17470Sstevel@tonic-gate RExC_state_t RExC_state;
17480Sstevel@tonic-gate RExC_state_t *pRExC_state = &RExC_state;
17490Sstevel@tonic-gate
17500Sstevel@tonic-gate if (exp == NULL)
17510Sstevel@tonic-gate FAIL("NULL regexp argument");
17520Sstevel@tonic-gate
1753*6696Svm156888 RExC_utf8 = RExC_orig_utf8 = pm->op_pmdynflags & PMdf_CMP_UTF8;
1754*6696Svm156888
17550Sstevel@tonic-gate DEBUG_r({
17560Sstevel@tonic-gate if (!PL_colorset) reginitcolors();
17570Sstevel@tonic-gate PerlIO_printf(Perl_debug_log, "%sCompiling REx%s `%s%*s%s'\n",
17580Sstevel@tonic-gate PL_colors[4],PL_colors[5],PL_colors[0],
1759*6696Svm156888 (int)(xend - exp), exp, PL_colors[1]);
17600Sstevel@tonic-gate });
1761*6696Svm156888
1762*6696Svm156888 redo_first_pass:
1763*6696Svm156888 RExC_precomp = exp;
17640Sstevel@tonic-gate RExC_flags = pm->op_pmflags;
17650Sstevel@tonic-gate RExC_sawback = 0;
17660Sstevel@tonic-gate
17670Sstevel@tonic-gate RExC_seen = 0;
17680Sstevel@tonic-gate RExC_seen_zerolen = *exp == '^' ? -1 : 0;
17690Sstevel@tonic-gate RExC_seen_evals = 0;
17700Sstevel@tonic-gate RExC_extralen = 0;
17710Sstevel@tonic-gate
17720Sstevel@tonic-gate /* First pass: determine size, legality. */
17730Sstevel@tonic-gate RExC_parse = exp;
17740Sstevel@tonic-gate RExC_start = exp;
17750Sstevel@tonic-gate RExC_end = xend;
17760Sstevel@tonic-gate RExC_naughty = 0;
17770Sstevel@tonic-gate RExC_npar = 1;
17780Sstevel@tonic-gate RExC_size = 0L;
17790Sstevel@tonic-gate RExC_emit = &PL_regdummy;
17800Sstevel@tonic-gate RExC_whilem_seen = 0;
17810Sstevel@tonic-gate #if 0 /* REGC() is (currently) a NOP at the first pass.
17820Sstevel@tonic-gate * Clever compilers notice this and complain. --jhi */
17830Sstevel@tonic-gate REGC((U8)REG_MAGIC, (char*)RExC_emit);
17840Sstevel@tonic-gate #endif
17850Sstevel@tonic-gate if (reg(pRExC_state, 0, &flags) == NULL) {
17860Sstevel@tonic-gate RExC_precomp = Nullch;
17870Sstevel@tonic-gate return(NULL);
17880Sstevel@tonic-gate }
1789*6696Svm156888
1790*6696Svm156888 if (RExC_utf8 && !RExC_orig_utf8) {
1791*6696Svm156888 /* It's possible to write a regexp in ascii that represents unicode
1792*6696Svm156888 codepoints outside of the byte range, such as via \x{100}. If we
1793*6696Svm156888 detect such a sequence we have to convert the entire pattern to utf8
1794*6696Svm156888 and then recompile, as our sizing calculation will have been based
1795*6696Svm156888 on 1 byte == 1 character, but we will need to use utf8 to encode
1796*6696Svm156888 at least some part of the pattern, and therefore must convert the whole
1797*6696Svm156888 thing.
1798*6696Svm156888 XXX: somehow figure out how to make this less expensive...
1799*6696Svm156888 -- dmq */
1800*6696Svm156888 STRLEN len = xend-exp;
1801*6696Svm156888 DEBUG_r(PerlIO_printf(Perl_debug_log,
1802*6696Svm156888 "UTF8 mismatch! Converting to utf8 for resizing and compile\n"));
1803*6696Svm156888 exp = (char*)Perl_bytes_to_utf8(aTHX_ (U8*)exp, &len);
1804*6696Svm156888 xend = exp + len;
1805*6696Svm156888 RExC_orig_utf8 = RExC_utf8;
1806*6696Svm156888 SAVEFREEPV(exp);
1807*6696Svm156888 goto redo_first_pass;
1808*6696Svm156888 }
18090Sstevel@tonic-gate DEBUG_r(PerlIO_printf(Perl_debug_log, "size %"IVdf" ", (IV)RExC_size));
18100Sstevel@tonic-gate
18110Sstevel@tonic-gate /* Small enough for pointer-storage convention?
18120Sstevel@tonic-gate If extralen==0, this means that we will not need long jumps. */
18130Sstevel@tonic-gate if (RExC_size >= 0x10000L && RExC_extralen)
18140Sstevel@tonic-gate RExC_size += RExC_extralen;
18150Sstevel@tonic-gate else
18160Sstevel@tonic-gate RExC_extralen = 0;
18170Sstevel@tonic-gate if (RExC_whilem_seen > 15)
18180Sstevel@tonic-gate RExC_whilem_seen = 15;
18190Sstevel@tonic-gate
18200Sstevel@tonic-gate /* Allocate space and initialize. */
18210Sstevel@tonic-gate Newc(1001, r, sizeof(regexp) + (unsigned)RExC_size * sizeof(regnode),
18220Sstevel@tonic-gate char, regexp);
18230Sstevel@tonic-gate if (r == NULL)
18240Sstevel@tonic-gate FAIL("Regexp out of space");
18250Sstevel@tonic-gate
18260Sstevel@tonic-gate #ifdef DEBUGGING
18270Sstevel@tonic-gate /* avoid reading uninitialized memory in DEBUGGING code in study_chunk() */
18280Sstevel@tonic-gate Zero(r, sizeof(regexp) + (unsigned)RExC_size * sizeof(regnode), char);
18290Sstevel@tonic-gate #endif
18300Sstevel@tonic-gate r->refcnt = 1;
18310Sstevel@tonic-gate r->prelen = xend - exp;
18320Sstevel@tonic-gate r->precomp = savepvn(RExC_precomp, r->prelen);
18330Sstevel@tonic-gate r->subbeg = NULL;
18340Sstevel@tonic-gate r->reganch = pm->op_pmflags & PMf_COMPILETIME;
18350Sstevel@tonic-gate r->nparens = RExC_npar - 1; /* set early to validate backrefs */
18360Sstevel@tonic-gate
18370Sstevel@tonic-gate r->substrs = 0; /* Useful during FAIL. */
18380Sstevel@tonic-gate r->startp = 0; /* Useful during FAIL. */
18390Sstevel@tonic-gate r->endp = 0; /* Useful during FAIL. */
18400Sstevel@tonic-gate
18410Sstevel@tonic-gate Newz(1304, r->offsets, 2*RExC_size+1, U32); /* MJD 20001228 */
18420Sstevel@tonic-gate if (r->offsets) {
18430Sstevel@tonic-gate r->offsets[0] = RExC_size;
18440Sstevel@tonic-gate }
18450Sstevel@tonic-gate DEBUG_r(PerlIO_printf(Perl_debug_log,
18460Sstevel@tonic-gate "%s %"UVuf" bytes for offset annotations.\n",
18470Sstevel@tonic-gate r->offsets ? "Got" : "Couldn't get",
18480Sstevel@tonic-gate (UV)((2*RExC_size+1) * sizeof(U32))));
18490Sstevel@tonic-gate
18500Sstevel@tonic-gate RExC_rx = r;
18510Sstevel@tonic-gate
18520Sstevel@tonic-gate /* Second pass: emit code. */
18530Sstevel@tonic-gate RExC_flags = pm->op_pmflags; /* don't let top level (?i) bleed */
18540Sstevel@tonic-gate RExC_parse = exp;
18550Sstevel@tonic-gate RExC_end = xend;
18560Sstevel@tonic-gate RExC_naughty = 0;
18570Sstevel@tonic-gate RExC_npar = 1;
18580Sstevel@tonic-gate RExC_emit_start = r->program;
18590Sstevel@tonic-gate RExC_emit = r->program;
18600Sstevel@tonic-gate /* Store the count of eval-groups for security checks: */
18610Sstevel@tonic-gate RExC_emit->next_off = (U16)((RExC_seen_evals > U16_MAX) ? U16_MAX : RExC_seen_evals);
18620Sstevel@tonic-gate REGC((U8)REG_MAGIC, (char*) RExC_emit++);
18630Sstevel@tonic-gate r->data = 0;
18640Sstevel@tonic-gate if (reg(pRExC_state, 0, &flags) == NULL)
18650Sstevel@tonic-gate return(NULL);
18660Sstevel@tonic-gate
18670Sstevel@tonic-gate /* Dig out information for optimizations. */
18680Sstevel@tonic-gate r->reganch = pm->op_pmflags & PMf_COMPILETIME; /* Again? */
18690Sstevel@tonic-gate pm->op_pmflags = RExC_flags;
18700Sstevel@tonic-gate if (UTF)
18710Sstevel@tonic-gate r->reganch |= ROPT_UTF8; /* Unicode in it? */
18720Sstevel@tonic-gate r->regstclass = NULL;
18730Sstevel@tonic-gate if (RExC_naughty >= 10) /* Probably an expensive pattern. */
18740Sstevel@tonic-gate r->reganch |= ROPT_NAUGHTY;
18750Sstevel@tonic-gate scan = r->program + 1; /* First BRANCH. */
18760Sstevel@tonic-gate
18770Sstevel@tonic-gate /* XXXX To minimize changes to RE engine we always allocate
18780Sstevel@tonic-gate 3-units-long substrs field. */
18790Sstevel@tonic-gate Newz(1004, r->substrs, 1, struct reg_substr_data);
18800Sstevel@tonic-gate
18810Sstevel@tonic-gate StructCopy(&zero_scan_data, &data, scan_data_t);
18820Sstevel@tonic-gate /* XXXX Should not we check for something else? Usually it is OPEN1... */
18830Sstevel@tonic-gate if (OP(scan) != BRANCH) { /* Only one top-level choice. */
18840Sstevel@tonic-gate I32 fake;
18850Sstevel@tonic-gate STRLEN longest_float_length, longest_fixed_length;
18860Sstevel@tonic-gate struct regnode_charclass_class ch_class;
18870Sstevel@tonic-gate int stclass_flag;
18880Sstevel@tonic-gate I32 last_close = 0;
18890Sstevel@tonic-gate
18900Sstevel@tonic-gate first = scan;
18910Sstevel@tonic-gate /* Skip introductions and multiplicators >= 1. */
18920Sstevel@tonic-gate while ((OP(first) == OPEN && (sawopen = 1)) ||
18930Sstevel@tonic-gate /* An OR of *one* alternative - should not happen now. */
18940Sstevel@tonic-gate (OP(first) == BRANCH && OP(regnext(first)) != BRANCH) ||
18950Sstevel@tonic-gate (OP(first) == PLUS) ||
18960Sstevel@tonic-gate (OP(first) == MINMOD) ||
18970Sstevel@tonic-gate /* An {n,m} with n>0 */
18980Sstevel@tonic-gate (PL_regkind[(U8)OP(first)] == CURLY && ARG1(first) > 0) ) {
18990Sstevel@tonic-gate if (OP(first) == PLUS)
19000Sstevel@tonic-gate sawplus = 1;
19010Sstevel@tonic-gate else
19020Sstevel@tonic-gate first += regarglen[(U8)OP(first)];
19030Sstevel@tonic-gate first = NEXTOPER(first);
19040Sstevel@tonic-gate }
19050Sstevel@tonic-gate
19060Sstevel@tonic-gate /* Starting-point info. */
19070Sstevel@tonic-gate again:
19080Sstevel@tonic-gate if (PL_regkind[(U8)OP(first)] == EXACT) {
19090Sstevel@tonic-gate if (OP(first) == EXACT)
19100Sstevel@tonic-gate ; /* Empty, get anchored substr later. */
19110Sstevel@tonic-gate else if ((OP(first) == EXACTF || OP(first) == EXACTFL))
19120Sstevel@tonic-gate r->regstclass = first;
19130Sstevel@tonic-gate }
19140Sstevel@tonic-gate else if (strchr((char*)PL_simple,OP(first)))
19150Sstevel@tonic-gate r->regstclass = first;
19160Sstevel@tonic-gate else if (PL_regkind[(U8)OP(first)] == BOUND ||
19170Sstevel@tonic-gate PL_regkind[(U8)OP(first)] == NBOUND)
19180Sstevel@tonic-gate r->regstclass = first;
19190Sstevel@tonic-gate else if (PL_regkind[(U8)OP(first)] == BOL) {
19200Sstevel@tonic-gate r->reganch |= (OP(first) == MBOL
19210Sstevel@tonic-gate ? ROPT_ANCH_MBOL
19220Sstevel@tonic-gate : (OP(first) == SBOL
19230Sstevel@tonic-gate ? ROPT_ANCH_SBOL
19240Sstevel@tonic-gate : ROPT_ANCH_BOL));
19250Sstevel@tonic-gate first = NEXTOPER(first);
19260Sstevel@tonic-gate goto again;
19270Sstevel@tonic-gate }
19280Sstevel@tonic-gate else if (OP(first) == GPOS) {
19290Sstevel@tonic-gate r->reganch |= ROPT_ANCH_GPOS;
19300Sstevel@tonic-gate first = NEXTOPER(first);
19310Sstevel@tonic-gate goto again;
19320Sstevel@tonic-gate }
19330Sstevel@tonic-gate else if (!sawopen && (OP(first) == STAR &&
19340Sstevel@tonic-gate PL_regkind[(U8)OP(NEXTOPER(first))] == REG_ANY) &&
19350Sstevel@tonic-gate !(r->reganch & ROPT_ANCH) )
19360Sstevel@tonic-gate {
19370Sstevel@tonic-gate /* turn .* into ^.* with an implied $*=1 */
19380Sstevel@tonic-gate int type = OP(NEXTOPER(first));
19390Sstevel@tonic-gate
19400Sstevel@tonic-gate if (type == REG_ANY)
19410Sstevel@tonic-gate type = ROPT_ANCH_MBOL;
19420Sstevel@tonic-gate else
19430Sstevel@tonic-gate type = ROPT_ANCH_SBOL;
19440Sstevel@tonic-gate
19450Sstevel@tonic-gate r->reganch |= type | ROPT_IMPLICIT;
19460Sstevel@tonic-gate first = NEXTOPER(first);
19470Sstevel@tonic-gate goto again;
19480Sstevel@tonic-gate }
19490Sstevel@tonic-gate if (sawplus && (!sawopen || !RExC_sawback)
19500Sstevel@tonic-gate && !(RExC_seen & REG_SEEN_EVAL)) /* May examine pos and $& */
19510Sstevel@tonic-gate /* x+ must match at the 1st pos of run of x's */
19520Sstevel@tonic-gate r->reganch |= ROPT_SKIP;
19530Sstevel@tonic-gate
19540Sstevel@tonic-gate /* Scan is after the zeroth branch, first is atomic matcher. */
19550Sstevel@tonic-gate DEBUG_r(PerlIO_printf(Perl_debug_log, "first at %"IVdf"\n",
19560Sstevel@tonic-gate (IV)(first - scan + 1)));
19570Sstevel@tonic-gate /*
19580Sstevel@tonic-gate * If there's something expensive in the r.e., find the
19590Sstevel@tonic-gate * longest literal string that must appear and make it the
19600Sstevel@tonic-gate * regmust. Resolve ties in favor of later strings, since
19610Sstevel@tonic-gate * the regstart check works with the beginning of the r.e.
19620Sstevel@tonic-gate * and avoiding duplication strengthens checking. Not a
19630Sstevel@tonic-gate * strong reason, but sufficient in the absence of others.
19640Sstevel@tonic-gate * [Now we resolve ties in favor of the earlier string if
19650Sstevel@tonic-gate * it happens that c_offset_min has been invalidated, since the
19660Sstevel@tonic-gate * earlier string may buy us something the later one won't.]
19670Sstevel@tonic-gate */
19680Sstevel@tonic-gate minlen = 0;
19690Sstevel@tonic-gate
19700Sstevel@tonic-gate data.longest_fixed = newSVpvn("",0);
19710Sstevel@tonic-gate data.longest_float = newSVpvn("",0);
19720Sstevel@tonic-gate data.last_found = newSVpvn("",0);
19730Sstevel@tonic-gate data.longest = &(data.longest_fixed);
19740Sstevel@tonic-gate first = scan;
19750Sstevel@tonic-gate if (!r->regstclass) {
19760Sstevel@tonic-gate cl_init(pRExC_state, &ch_class);
19770Sstevel@tonic-gate data.start_class = &ch_class;
19780Sstevel@tonic-gate stclass_flag = SCF_DO_STCLASS_AND;
19790Sstevel@tonic-gate } else /* XXXX Check for BOUND? */
19800Sstevel@tonic-gate stclass_flag = 0;
19810Sstevel@tonic-gate data.last_closep = &last_close;
19820Sstevel@tonic-gate
19830Sstevel@tonic-gate minlen = study_chunk(pRExC_state, &first, &fake, scan + RExC_size, /* Up to end */
19840Sstevel@tonic-gate &data, SCF_DO_SUBSTR | SCF_WHILEM_VISITED_POS | stclass_flag);
19850Sstevel@tonic-gate if ( RExC_npar == 1 && data.longest == &(data.longest_fixed)
19860Sstevel@tonic-gate && data.last_start_min == 0 && data.last_end > 0
19870Sstevel@tonic-gate && !RExC_seen_zerolen
19880Sstevel@tonic-gate && (!(RExC_seen & REG_SEEN_GPOS) || (r->reganch & ROPT_ANCH_GPOS)))
19890Sstevel@tonic-gate r->reganch |= ROPT_CHECK_ALL;
19900Sstevel@tonic-gate scan_commit(pRExC_state, &data);
19910Sstevel@tonic-gate SvREFCNT_dec(data.last_found);
19920Sstevel@tonic-gate
19930Sstevel@tonic-gate longest_float_length = CHR_SVLEN(data.longest_float);
19940Sstevel@tonic-gate if (longest_float_length
19950Sstevel@tonic-gate || (data.flags & SF_FL_BEFORE_EOL
19960Sstevel@tonic-gate && (!(data.flags & SF_FL_BEFORE_MEOL)
19970Sstevel@tonic-gate || (RExC_flags & PMf_MULTILINE)))) {
19980Sstevel@tonic-gate int t;
19990Sstevel@tonic-gate
20000Sstevel@tonic-gate if (SvCUR(data.longest_fixed) /* ok to leave SvCUR */
20010Sstevel@tonic-gate && data.offset_fixed == data.offset_float_min
20020Sstevel@tonic-gate && SvCUR(data.longest_fixed) == SvCUR(data.longest_float))
20030Sstevel@tonic-gate goto remove_float; /* As in (a)+. */
20040Sstevel@tonic-gate
20050Sstevel@tonic-gate if (SvUTF8(data.longest_float)) {
20060Sstevel@tonic-gate r->float_utf8 = data.longest_float;
20070Sstevel@tonic-gate r->float_substr = Nullsv;
20080Sstevel@tonic-gate } else {
20090Sstevel@tonic-gate r->float_substr = data.longest_float;
20100Sstevel@tonic-gate r->float_utf8 = Nullsv;
20110Sstevel@tonic-gate }
20120Sstevel@tonic-gate r->float_min_offset = data.offset_float_min;
20130Sstevel@tonic-gate r->float_max_offset = data.offset_float_max;
20140Sstevel@tonic-gate t = (data.flags & SF_FL_BEFORE_EOL /* Can't have SEOL and MULTI */
20150Sstevel@tonic-gate && (!(data.flags & SF_FL_BEFORE_MEOL)
20160Sstevel@tonic-gate || (RExC_flags & PMf_MULTILINE)));
20170Sstevel@tonic-gate fbm_compile(data.longest_float, t ? FBMcf_TAIL : 0);
20180Sstevel@tonic-gate }
20190Sstevel@tonic-gate else {
20200Sstevel@tonic-gate remove_float:
20210Sstevel@tonic-gate r->float_substr = r->float_utf8 = Nullsv;
20220Sstevel@tonic-gate SvREFCNT_dec(data.longest_float);
20230Sstevel@tonic-gate longest_float_length = 0;
20240Sstevel@tonic-gate }
20250Sstevel@tonic-gate
20260Sstevel@tonic-gate longest_fixed_length = CHR_SVLEN(data.longest_fixed);
20270Sstevel@tonic-gate if (longest_fixed_length
20280Sstevel@tonic-gate || (data.flags & SF_FIX_BEFORE_EOL /* Cannot have SEOL and MULTI */
20290Sstevel@tonic-gate && (!(data.flags & SF_FIX_BEFORE_MEOL)
20300Sstevel@tonic-gate || (RExC_flags & PMf_MULTILINE)))) {
20310Sstevel@tonic-gate int t;
20320Sstevel@tonic-gate
20330Sstevel@tonic-gate if (SvUTF8(data.longest_fixed)) {
20340Sstevel@tonic-gate r->anchored_utf8 = data.longest_fixed;
20350Sstevel@tonic-gate r->anchored_substr = Nullsv;
20360Sstevel@tonic-gate } else {
20370Sstevel@tonic-gate r->anchored_substr = data.longest_fixed;
20380Sstevel@tonic-gate r->anchored_utf8 = Nullsv;
20390Sstevel@tonic-gate }
20400Sstevel@tonic-gate r->anchored_offset = data.offset_fixed;
20410Sstevel@tonic-gate t = (data.flags & SF_FIX_BEFORE_EOL /* Can't have SEOL and MULTI */
20420Sstevel@tonic-gate && (!(data.flags & SF_FIX_BEFORE_MEOL)
20430Sstevel@tonic-gate || (RExC_flags & PMf_MULTILINE)));
20440Sstevel@tonic-gate fbm_compile(data.longest_fixed, t ? FBMcf_TAIL : 0);
20450Sstevel@tonic-gate }
20460Sstevel@tonic-gate else {
20470Sstevel@tonic-gate r->anchored_substr = r->anchored_utf8 = Nullsv;
20480Sstevel@tonic-gate SvREFCNT_dec(data.longest_fixed);
20490Sstevel@tonic-gate longest_fixed_length = 0;
20500Sstevel@tonic-gate }
20510Sstevel@tonic-gate if (r->regstclass
20520Sstevel@tonic-gate && (OP(r->regstclass) == REG_ANY || OP(r->regstclass) == SANY))
20530Sstevel@tonic-gate r->regstclass = NULL;
20540Sstevel@tonic-gate if ((!(r->anchored_substr || r->anchored_utf8) || r->anchored_offset)
20550Sstevel@tonic-gate && stclass_flag
20560Sstevel@tonic-gate && !(data.start_class->flags & ANYOF_EOS)
20570Sstevel@tonic-gate && !cl_is_anything(data.start_class))
20580Sstevel@tonic-gate {
20590Sstevel@tonic-gate I32 n = add_data(pRExC_state, 1, "f");
20600Sstevel@tonic-gate
20610Sstevel@tonic-gate New(1006, RExC_rx->data->data[n], 1,
20620Sstevel@tonic-gate struct regnode_charclass_class);
20630Sstevel@tonic-gate StructCopy(data.start_class,
20640Sstevel@tonic-gate (struct regnode_charclass_class*)RExC_rx->data->data[n],
20650Sstevel@tonic-gate struct regnode_charclass_class);
20660Sstevel@tonic-gate r->regstclass = (regnode*)RExC_rx->data->data[n];
20670Sstevel@tonic-gate r->reganch &= ~ROPT_SKIP; /* Used in find_byclass(). */
20680Sstevel@tonic-gate PL_regdata = r->data; /* for regprop() */
20690Sstevel@tonic-gate DEBUG_r({ SV *sv = sv_newmortal();
20700Sstevel@tonic-gate regprop(sv, (regnode*)data.start_class);
20710Sstevel@tonic-gate PerlIO_printf(Perl_debug_log,
20720Sstevel@tonic-gate "synthetic stclass `%s'.\n",
20730Sstevel@tonic-gate SvPVX(sv));});
20740Sstevel@tonic-gate }
20750Sstevel@tonic-gate
20760Sstevel@tonic-gate /* A temporary algorithm prefers floated substr to fixed one to dig more info. */
20770Sstevel@tonic-gate if (longest_fixed_length > longest_float_length) {
20780Sstevel@tonic-gate r->check_substr = r->anchored_substr;
20790Sstevel@tonic-gate r->check_utf8 = r->anchored_utf8;
20800Sstevel@tonic-gate r->check_offset_min = r->check_offset_max = r->anchored_offset;
20810Sstevel@tonic-gate if (r->reganch & ROPT_ANCH_SINGLE)
20820Sstevel@tonic-gate r->reganch |= ROPT_NOSCAN;
20830Sstevel@tonic-gate }
20840Sstevel@tonic-gate else {
20850Sstevel@tonic-gate r->check_substr = r->float_substr;
20860Sstevel@tonic-gate r->check_utf8 = r->float_utf8;
20870Sstevel@tonic-gate r->check_offset_min = data.offset_float_min;
20880Sstevel@tonic-gate r->check_offset_max = data.offset_float_max;
20890Sstevel@tonic-gate }
20900Sstevel@tonic-gate /* XXXX Currently intuiting is not compatible with ANCH_GPOS.
20910Sstevel@tonic-gate This should be changed ASAP! */
20920Sstevel@tonic-gate if ((r->check_substr || r->check_utf8) && !(r->reganch & ROPT_ANCH_GPOS)) {
20930Sstevel@tonic-gate r->reganch |= RE_USE_INTUIT;
20940Sstevel@tonic-gate if (SvTAIL(r->check_substr ? r->check_substr : r->check_utf8))
20950Sstevel@tonic-gate r->reganch |= RE_INTUIT_TAIL;
20960Sstevel@tonic-gate }
20970Sstevel@tonic-gate }
20980Sstevel@tonic-gate else {
20990Sstevel@tonic-gate /* Several toplevels. Best we can is to set minlen. */
21000Sstevel@tonic-gate I32 fake;
21010Sstevel@tonic-gate struct regnode_charclass_class ch_class;
21020Sstevel@tonic-gate I32 last_close = 0;
21030Sstevel@tonic-gate
21040Sstevel@tonic-gate DEBUG_r(PerlIO_printf(Perl_debug_log, "\n"));
21050Sstevel@tonic-gate scan = r->program + 1;
21060Sstevel@tonic-gate cl_init(pRExC_state, &ch_class);
21070Sstevel@tonic-gate data.start_class = &ch_class;
21080Sstevel@tonic-gate data.last_closep = &last_close;
21090Sstevel@tonic-gate minlen = study_chunk(pRExC_state, &scan, &fake, scan + RExC_size, &data, SCF_DO_STCLASS_AND|SCF_WHILEM_VISITED_POS);
21100Sstevel@tonic-gate r->check_substr = r->check_utf8 = r->anchored_substr = r->anchored_utf8
21110Sstevel@tonic-gate = r->float_substr = r->float_utf8 = Nullsv;
21120Sstevel@tonic-gate if (!(data.start_class->flags & ANYOF_EOS)
21130Sstevel@tonic-gate && !cl_is_anything(data.start_class))
21140Sstevel@tonic-gate {
21150Sstevel@tonic-gate I32 n = add_data(pRExC_state, 1, "f");
21160Sstevel@tonic-gate
21170Sstevel@tonic-gate New(1006, RExC_rx->data->data[n], 1,
21180Sstevel@tonic-gate struct regnode_charclass_class);
21190Sstevel@tonic-gate StructCopy(data.start_class,
21200Sstevel@tonic-gate (struct regnode_charclass_class*)RExC_rx->data->data[n],
21210Sstevel@tonic-gate struct regnode_charclass_class);
21220Sstevel@tonic-gate r->regstclass = (regnode*)RExC_rx->data->data[n];
21230Sstevel@tonic-gate r->reganch &= ~ROPT_SKIP; /* Used in find_byclass(). */
21240Sstevel@tonic-gate DEBUG_r({ SV* sv = sv_newmortal();
21250Sstevel@tonic-gate regprop(sv, (regnode*)data.start_class);
21260Sstevel@tonic-gate PerlIO_printf(Perl_debug_log,
21270Sstevel@tonic-gate "synthetic stclass `%s'.\n",
21280Sstevel@tonic-gate SvPVX(sv));});
21290Sstevel@tonic-gate }
21300Sstevel@tonic-gate }
21310Sstevel@tonic-gate
21320Sstevel@tonic-gate r->minlen = minlen;
21330Sstevel@tonic-gate if (RExC_seen & REG_SEEN_GPOS)
21340Sstevel@tonic-gate r->reganch |= ROPT_GPOS_SEEN;
21350Sstevel@tonic-gate if (RExC_seen & REG_SEEN_LOOKBEHIND)
21360Sstevel@tonic-gate r->reganch |= ROPT_LOOKBEHIND_SEEN;
21370Sstevel@tonic-gate if (RExC_seen & REG_SEEN_EVAL)
21380Sstevel@tonic-gate r->reganch |= ROPT_EVAL_SEEN;
21390Sstevel@tonic-gate if (RExC_seen & REG_SEEN_CANY)
21400Sstevel@tonic-gate r->reganch |= ROPT_CANY_SEEN;
21410Sstevel@tonic-gate Newz(1002, r->startp, RExC_npar, I32);
21420Sstevel@tonic-gate Newz(1002, r->endp, RExC_npar, I32);
21430Sstevel@tonic-gate PL_regdata = r->data; /* for regprop() */
21440Sstevel@tonic-gate DEBUG_r(regdump(r));
21450Sstevel@tonic-gate return(r);
21460Sstevel@tonic-gate }
21470Sstevel@tonic-gate
21480Sstevel@tonic-gate /*
21490Sstevel@tonic-gate - reg - regular expression, i.e. main body or parenthesized thing
21500Sstevel@tonic-gate *
21510Sstevel@tonic-gate * Caller must absorb opening parenthesis.
21520Sstevel@tonic-gate *
21530Sstevel@tonic-gate * Combining parenthesis handling with the base level of regular expression
21540Sstevel@tonic-gate * is a trifle forced, but the need to tie the tails of the branches to what
21550Sstevel@tonic-gate * follows makes it hard to avoid.
21560Sstevel@tonic-gate */
21570Sstevel@tonic-gate STATIC regnode *
S_reg(pTHX_ RExC_state_t * pRExC_state,I32 paren,I32 * flagp)21580Sstevel@tonic-gate S_reg(pTHX_ RExC_state_t *pRExC_state, I32 paren, I32 *flagp)
21590Sstevel@tonic-gate /* paren: Parenthesized? 0=top, 1=(, inside: changed to letter. */
21600Sstevel@tonic-gate {
21610Sstevel@tonic-gate register regnode *ret; /* Will be the head of the group. */
21620Sstevel@tonic-gate register regnode *br;
21630Sstevel@tonic-gate register regnode *lastbr;
21640Sstevel@tonic-gate register regnode *ender = 0;
21650Sstevel@tonic-gate register I32 parno = 0;
21660Sstevel@tonic-gate I32 flags, oregflags = RExC_flags, have_branch = 0, open = 0;
21670Sstevel@tonic-gate
21680Sstevel@tonic-gate /* for (?g), (?gc), and (?o) warnings; warning
21690Sstevel@tonic-gate about (?c) will warn about (?g) -- japhy */
21700Sstevel@tonic-gate
21710Sstevel@tonic-gate I32 wastedflags = 0x00,
21720Sstevel@tonic-gate wasted_o = 0x01,
21730Sstevel@tonic-gate wasted_g = 0x02,
21740Sstevel@tonic-gate wasted_gc = 0x02 | 0x04,
21750Sstevel@tonic-gate wasted_c = 0x04;
21760Sstevel@tonic-gate
21770Sstevel@tonic-gate char * parse_start = RExC_parse; /* MJD */
21780Sstevel@tonic-gate char *oregcomp_parse = RExC_parse;
21790Sstevel@tonic-gate char c;
21800Sstevel@tonic-gate
21810Sstevel@tonic-gate *flagp = 0; /* Tentatively. */
21820Sstevel@tonic-gate
21830Sstevel@tonic-gate
21840Sstevel@tonic-gate /* Make an OPEN node, if parenthesized. */
21850Sstevel@tonic-gate if (paren) {
21860Sstevel@tonic-gate if (*RExC_parse == '?') { /* (?...) */
21870Sstevel@tonic-gate U32 posflags = 0, negflags = 0;
21880Sstevel@tonic-gate U32 *flagsp = &posflags;
21890Sstevel@tonic-gate int logical = 0;
21900Sstevel@tonic-gate char *seqstart = RExC_parse;
21910Sstevel@tonic-gate
21920Sstevel@tonic-gate RExC_parse++;
21930Sstevel@tonic-gate paren = *RExC_parse++;
21940Sstevel@tonic-gate ret = NULL; /* For look-ahead/behind. */
21950Sstevel@tonic-gate switch (paren) {
21960Sstevel@tonic-gate case '<': /* (?<...) */
21970Sstevel@tonic-gate RExC_seen |= REG_SEEN_LOOKBEHIND;
21980Sstevel@tonic-gate if (*RExC_parse == '!')
21990Sstevel@tonic-gate paren = ',';
22000Sstevel@tonic-gate if (*RExC_parse != '=' && *RExC_parse != '!')
22010Sstevel@tonic-gate goto unknown;
22020Sstevel@tonic-gate RExC_parse++;
22030Sstevel@tonic-gate case '=': /* (?=...) */
22040Sstevel@tonic-gate case '!': /* (?!...) */
22050Sstevel@tonic-gate RExC_seen_zerolen++;
22060Sstevel@tonic-gate case ':': /* (?:...) */
22070Sstevel@tonic-gate case '>': /* (?>...) */
22080Sstevel@tonic-gate break;
22090Sstevel@tonic-gate case '$': /* (?$...) */
22100Sstevel@tonic-gate case '@': /* (?@...) */
22110Sstevel@tonic-gate vFAIL2("Sequence (?%c...) not implemented", (int)paren);
22120Sstevel@tonic-gate break;
22130Sstevel@tonic-gate case '#': /* (?#...) */
22140Sstevel@tonic-gate while (*RExC_parse && *RExC_parse != ')')
22150Sstevel@tonic-gate RExC_parse++;
22160Sstevel@tonic-gate if (*RExC_parse != ')')
22170Sstevel@tonic-gate FAIL("Sequence (?#... not terminated");
22180Sstevel@tonic-gate nextchar(pRExC_state);
22190Sstevel@tonic-gate *flagp = TRYAGAIN;
22200Sstevel@tonic-gate return NULL;
22210Sstevel@tonic-gate case 'p': /* (?p...) */
22220Sstevel@tonic-gate if (SIZE_ONLY && ckWARN2(WARN_DEPRECATED, WARN_REGEXP))
22230Sstevel@tonic-gate vWARNdep(RExC_parse, "(?p{}) is deprecated - use (??{})");
22240Sstevel@tonic-gate /* FALL THROUGH*/
22250Sstevel@tonic-gate case '?': /* (??...) */
22260Sstevel@tonic-gate logical = 1;
22270Sstevel@tonic-gate if (*RExC_parse != '{')
22280Sstevel@tonic-gate goto unknown;
22290Sstevel@tonic-gate paren = *RExC_parse++;
22300Sstevel@tonic-gate /* FALL THROUGH */
22310Sstevel@tonic-gate case '{': /* (?{...}) */
22320Sstevel@tonic-gate {
22330Sstevel@tonic-gate I32 count = 1, n = 0;
22340Sstevel@tonic-gate char c;
22350Sstevel@tonic-gate char *s = RExC_parse;
22360Sstevel@tonic-gate SV *sv;
22370Sstevel@tonic-gate OP_4tree *sop, *rop;
22380Sstevel@tonic-gate
22390Sstevel@tonic-gate RExC_seen_zerolen++;
22400Sstevel@tonic-gate RExC_seen |= REG_SEEN_EVAL;
22410Sstevel@tonic-gate while (count && (c = *RExC_parse)) {
22420Sstevel@tonic-gate if (c == '\\' && RExC_parse[1])
22430Sstevel@tonic-gate RExC_parse++;
22440Sstevel@tonic-gate else if (c == '{')
22450Sstevel@tonic-gate count++;
22460Sstevel@tonic-gate else if (c == '}')
22470Sstevel@tonic-gate count--;
22480Sstevel@tonic-gate RExC_parse++;
22490Sstevel@tonic-gate }
22500Sstevel@tonic-gate if (*RExC_parse != ')')
22510Sstevel@tonic-gate {
22520Sstevel@tonic-gate RExC_parse = s;
22530Sstevel@tonic-gate vFAIL("Sequence (?{...}) not terminated or not {}-balanced");
22540Sstevel@tonic-gate }
22550Sstevel@tonic-gate if (!SIZE_ONLY) {
22560Sstevel@tonic-gate PAD *pad;
22570Sstevel@tonic-gate
22580Sstevel@tonic-gate if (RExC_parse - 1 - s)
22590Sstevel@tonic-gate sv = newSVpvn(s, RExC_parse - 1 - s);
22600Sstevel@tonic-gate else
22610Sstevel@tonic-gate sv = newSVpvn("", 0);
22620Sstevel@tonic-gate
22630Sstevel@tonic-gate ENTER;
22640Sstevel@tonic-gate Perl_save_re_context(aTHX);
22650Sstevel@tonic-gate rop = sv_compile_2op(sv, &sop, "re", &pad);
22660Sstevel@tonic-gate sop->op_private |= OPpREFCOUNTED;
22670Sstevel@tonic-gate /* re_dup will OpREFCNT_inc */
22680Sstevel@tonic-gate OpREFCNT_set(sop, 1);
22690Sstevel@tonic-gate LEAVE;
22700Sstevel@tonic-gate
22710Sstevel@tonic-gate n = add_data(pRExC_state, 3, "nop");
22720Sstevel@tonic-gate RExC_rx->data->data[n] = (void*)rop;
22730Sstevel@tonic-gate RExC_rx->data->data[n+1] = (void*)sop;
22740Sstevel@tonic-gate RExC_rx->data->data[n+2] = (void*)pad;
22750Sstevel@tonic-gate SvREFCNT_dec(sv);
22760Sstevel@tonic-gate }
22770Sstevel@tonic-gate else { /* First pass */
22780Sstevel@tonic-gate if (PL_reginterp_cnt < ++RExC_seen_evals
22790Sstevel@tonic-gate && IN_PERL_RUNTIME)
22800Sstevel@tonic-gate /* No compiled RE interpolated, has runtime
22810Sstevel@tonic-gate components ===> unsafe. */
22820Sstevel@tonic-gate FAIL("Eval-group not allowed at runtime, use re 'eval'");
22830Sstevel@tonic-gate if (PL_tainting && PL_tainted)
22840Sstevel@tonic-gate FAIL("Eval-group in insecure regular expression");
22850Sstevel@tonic-gate }
22860Sstevel@tonic-gate
22870Sstevel@tonic-gate nextchar(pRExC_state);
22880Sstevel@tonic-gate if (logical) {
22890Sstevel@tonic-gate ret = reg_node(pRExC_state, LOGICAL);
22900Sstevel@tonic-gate if (!SIZE_ONLY)
22910Sstevel@tonic-gate ret->flags = 2;
22920Sstevel@tonic-gate regtail(pRExC_state, ret, reganode(pRExC_state, EVAL, n));
22930Sstevel@tonic-gate /* deal with the length of this later - MJD */
22940Sstevel@tonic-gate return ret;
22950Sstevel@tonic-gate }
22960Sstevel@tonic-gate ret = reganode(pRExC_state, EVAL, n);
22970Sstevel@tonic-gate Set_Node_Length(ret, RExC_parse - parse_start + 1);
22980Sstevel@tonic-gate Set_Node_Offset(ret, parse_start);
22990Sstevel@tonic-gate return ret;
23000Sstevel@tonic-gate }
23010Sstevel@tonic-gate case '(': /* (?(?{...})...) and (?(?=...)...) */
23020Sstevel@tonic-gate {
23030Sstevel@tonic-gate if (RExC_parse[0] == '?') { /* (?(?...)) */
23040Sstevel@tonic-gate if (RExC_parse[1] == '=' || RExC_parse[1] == '!'
23050Sstevel@tonic-gate || RExC_parse[1] == '<'
23060Sstevel@tonic-gate || RExC_parse[1] == '{') { /* Lookahead or eval. */
23070Sstevel@tonic-gate I32 flag;
23080Sstevel@tonic-gate
23090Sstevel@tonic-gate ret = reg_node(pRExC_state, LOGICAL);
23100Sstevel@tonic-gate if (!SIZE_ONLY)
23110Sstevel@tonic-gate ret->flags = 1;
23120Sstevel@tonic-gate regtail(pRExC_state, ret, reg(pRExC_state, 1, &flag));
23130Sstevel@tonic-gate goto insert_if;
23140Sstevel@tonic-gate }
23150Sstevel@tonic-gate }
23160Sstevel@tonic-gate else if (RExC_parse[0] >= '1' && RExC_parse[0] <= '9' ) {
23170Sstevel@tonic-gate /* (?(1)...) */
23180Sstevel@tonic-gate parno = atoi(RExC_parse++);
23190Sstevel@tonic-gate
23200Sstevel@tonic-gate while (isDIGIT(*RExC_parse))
23210Sstevel@tonic-gate RExC_parse++;
23220Sstevel@tonic-gate ret = reganode(pRExC_state, GROUPP, parno);
23230Sstevel@tonic-gate
23240Sstevel@tonic-gate if ((c = *nextchar(pRExC_state)) != ')')
23250Sstevel@tonic-gate vFAIL("Switch condition not recognized");
23260Sstevel@tonic-gate insert_if:
23270Sstevel@tonic-gate regtail(pRExC_state, ret, reganode(pRExC_state, IFTHEN, 0));
23280Sstevel@tonic-gate br = regbranch(pRExC_state, &flags, 1);
23290Sstevel@tonic-gate if (br == NULL)
23300Sstevel@tonic-gate br = reganode(pRExC_state, LONGJMP, 0);
23310Sstevel@tonic-gate else
23320Sstevel@tonic-gate regtail(pRExC_state, br, reganode(pRExC_state, LONGJMP, 0));
23330Sstevel@tonic-gate c = *nextchar(pRExC_state);
23340Sstevel@tonic-gate if (flags&HASWIDTH)
23350Sstevel@tonic-gate *flagp |= HASWIDTH;
23360Sstevel@tonic-gate if (c == '|') {
23370Sstevel@tonic-gate lastbr = reganode(pRExC_state, IFTHEN, 0); /* Fake one for optimizer. */
23380Sstevel@tonic-gate regbranch(pRExC_state, &flags, 1);
23390Sstevel@tonic-gate regtail(pRExC_state, ret, lastbr);
23400Sstevel@tonic-gate if (flags&HASWIDTH)
23410Sstevel@tonic-gate *flagp |= HASWIDTH;
23420Sstevel@tonic-gate c = *nextchar(pRExC_state);
23430Sstevel@tonic-gate }
23440Sstevel@tonic-gate else
23450Sstevel@tonic-gate lastbr = NULL;
23460Sstevel@tonic-gate if (c != ')')
23470Sstevel@tonic-gate vFAIL("Switch (?(condition)... contains too many branches");
23480Sstevel@tonic-gate ender = reg_node(pRExC_state, TAIL);
23490Sstevel@tonic-gate regtail(pRExC_state, br, ender);
23500Sstevel@tonic-gate if (lastbr) {
23510Sstevel@tonic-gate regtail(pRExC_state, lastbr, ender);
23520Sstevel@tonic-gate regtail(pRExC_state, NEXTOPER(NEXTOPER(lastbr)), ender);
23530Sstevel@tonic-gate }
23540Sstevel@tonic-gate else
23550Sstevel@tonic-gate regtail(pRExC_state, ret, ender);
23560Sstevel@tonic-gate return ret;
23570Sstevel@tonic-gate }
23580Sstevel@tonic-gate else {
23590Sstevel@tonic-gate vFAIL2("Unknown switch condition (?(%.2s", RExC_parse);
23600Sstevel@tonic-gate }
23610Sstevel@tonic-gate }
23620Sstevel@tonic-gate case 0:
23630Sstevel@tonic-gate RExC_parse--; /* for vFAIL to print correctly */
23640Sstevel@tonic-gate vFAIL("Sequence (? incomplete");
23650Sstevel@tonic-gate break;
23660Sstevel@tonic-gate default:
23670Sstevel@tonic-gate --RExC_parse;
23680Sstevel@tonic-gate parse_flags: /* (?i) */
23690Sstevel@tonic-gate while (*RExC_parse && strchr("iogcmsx", *RExC_parse)) {
23700Sstevel@tonic-gate /* (?g), (?gc) and (?o) are useless here
23710Sstevel@tonic-gate and must be globally applied -- japhy */
23720Sstevel@tonic-gate
23730Sstevel@tonic-gate if (*RExC_parse == 'o' || *RExC_parse == 'g') {
23740Sstevel@tonic-gate if (SIZE_ONLY && ckWARN(WARN_REGEXP)) {
23750Sstevel@tonic-gate I32 wflagbit = *RExC_parse == 'o' ? wasted_o : wasted_g;
23760Sstevel@tonic-gate if (! (wastedflags & wflagbit) ) {
23770Sstevel@tonic-gate wastedflags |= wflagbit;
23780Sstevel@tonic-gate vWARN5(
23790Sstevel@tonic-gate RExC_parse + 1,
23800Sstevel@tonic-gate "Useless (%s%c) - %suse /%c modifier",
23810Sstevel@tonic-gate flagsp == &negflags ? "?-" : "?",
23820Sstevel@tonic-gate *RExC_parse,
23830Sstevel@tonic-gate flagsp == &negflags ? "don't " : "",
23840Sstevel@tonic-gate *RExC_parse
23850Sstevel@tonic-gate );
23860Sstevel@tonic-gate }
23870Sstevel@tonic-gate }
23880Sstevel@tonic-gate }
23890Sstevel@tonic-gate else if (*RExC_parse == 'c') {
23900Sstevel@tonic-gate if (SIZE_ONLY && ckWARN(WARN_REGEXP)) {
23910Sstevel@tonic-gate if (! (wastedflags & wasted_c) ) {
23920Sstevel@tonic-gate wastedflags |= wasted_gc;
23930Sstevel@tonic-gate vWARN3(
23940Sstevel@tonic-gate RExC_parse + 1,
23950Sstevel@tonic-gate "Useless (%sc) - %suse /gc modifier",
23960Sstevel@tonic-gate flagsp == &negflags ? "?-" : "?",
23970Sstevel@tonic-gate flagsp == &negflags ? "don't " : ""
23980Sstevel@tonic-gate );
23990Sstevel@tonic-gate }
24000Sstevel@tonic-gate }
24010Sstevel@tonic-gate }
24020Sstevel@tonic-gate else { pmflag(flagsp, *RExC_parse); }
24030Sstevel@tonic-gate
24040Sstevel@tonic-gate ++RExC_parse;
24050Sstevel@tonic-gate }
24060Sstevel@tonic-gate if (*RExC_parse == '-') {
24070Sstevel@tonic-gate flagsp = &negflags;
24080Sstevel@tonic-gate wastedflags = 0; /* reset so (?g-c) warns twice */
24090Sstevel@tonic-gate ++RExC_parse;
24100Sstevel@tonic-gate goto parse_flags;
24110Sstevel@tonic-gate }
24120Sstevel@tonic-gate RExC_flags |= posflags;
24130Sstevel@tonic-gate RExC_flags &= ~negflags;
24140Sstevel@tonic-gate if (*RExC_parse == ':') {
24150Sstevel@tonic-gate RExC_parse++;
24160Sstevel@tonic-gate paren = ':';
24170Sstevel@tonic-gate break;
24180Sstevel@tonic-gate }
24190Sstevel@tonic-gate unknown:
24200Sstevel@tonic-gate if (*RExC_parse != ')') {
24210Sstevel@tonic-gate RExC_parse++;
24220Sstevel@tonic-gate vFAIL3("Sequence (%.*s...) not recognized", RExC_parse-seqstart, seqstart);
24230Sstevel@tonic-gate }
24240Sstevel@tonic-gate nextchar(pRExC_state);
24250Sstevel@tonic-gate *flagp = TRYAGAIN;
24260Sstevel@tonic-gate return NULL;
24270Sstevel@tonic-gate }
24280Sstevel@tonic-gate }
24290Sstevel@tonic-gate else { /* (...) */
24300Sstevel@tonic-gate parno = RExC_npar;
24310Sstevel@tonic-gate RExC_npar++;
24320Sstevel@tonic-gate ret = reganode(pRExC_state, OPEN, parno);
24330Sstevel@tonic-gate Set_Node_Length(ret, 1); /* MJD */
24340Sstevel@tonic-gate Set_Node_Offset(ret, RExC_parse); /* MJD */
24350Sstevel@tonic-gate open = 1;
24360Sstevel@tonic-gate }
24370Sstevel@tonic-gate }
24380Sstevel@tonic-gate else /* ! paren */
24390Sstevel@tonic-gate ret = NULL;
24400Sstevel@tonic-gate
24410Sstevel@tonic-gate /* Pick up the branches, linking them together. */
24420Sstevel@tonic-gate parse_start = RExC_parse; /* MJD */
24430Sstevel@tonic-gate br = regbranch(pRExC_state, &flags, 1);
24440Sstevel@tonic-gate /* branch_len = (paren != 0); */
24450Sstevel@tonic-gate
24460Sstevel@tonic-gate if (br == NULL)
24470Sstevel@tonic-gate return(NULL);
24480Sstevel@tonic-gate if (*RExC_parse == '|') {
24490Sstevel@tonic-gate if (!SIZE_ONLY && RExC_extralen) {
24500Sstevel@tonic-gate reginsert(pRExC_state, BRANCHJ, br);
24510Sstevel@tonic-gate }
24520Sstevel@tonic-gate else { /* MJD */
24530Sstevel@tonic-gate reginsert(pRExC_state, BRANCH, br);
24540Sstevel@tonic-gate Set_Node_Length(br, paren != 0);
24550Sstevel@tonic-gate Set_Node_Offset_To_R(br-RExC_emit_start, parse_start-RExC_start);
24560Sstevel@tonic-gate }
24570Sstevel@tonic-gate have_branch = 1;
24580Sstevel@tonic-gate if (SIZE_ONLY)
24590Sstevel@tonic-gate RExC_extralen += 1; /* For BRANCHJ-BRANCH. */
24600Sstevel@tonic-gate }
24610Sstevel@tonic-gate else if (paren == ':') {
24620Sstevel@tonic-gate *flagp |= flags&SIMPLE;
24630Sstevel@tonic-gate }
24640Sstevel@tonic-gate if (open) { /* Starts with OPEN. */
24650Sstevel@tonic-gate regtail(pRExC_state, ret, br); /* OPEN -> first. */
24660Sstevel@tonic-gate }
24670Sstevel@tonic-gate else if (paren != '?') /* Not Conditional */
24680Sstevel@tonic-gate ret = br;
24690Sstevel@tonic-gate *flagp |= flags & (SPSTART | HASWIDTH);
24700Sstevel@tonic-gate lastbr = br;
24710Sstevel@tonic-gate while (*RExC_parse == '|') {
24720Sstevel@tonic-gate if (!SIZE_ONLY && RExC_extralen) {
24730Sstevel@tonic-gate ender = reganode(pRExC_state, LONGJMP,0);
24740Sstevel@tonic-gate regtail(pRExC_state, NEXTOPER(NEXTOPER(lastbr)), ender); /* Append to the previous. */
24750Sstevel@tonic-gate }
24760Sstevel@tonic-gate if (SIZE_ONLY)
24770Sstevel@tonic-gate RExC_extralen += 2; /* Account for LONGJMP. */
24780Sstevel@tonic-gate nextchar(pRExC_state);
24790Sstevel@tonic-gate br = regbranch(pRExC_state, &flags, 0);
24800Sstevel@tonic-gate
24810Sstevel@tonic-gate if (br == NULL)
24820Sstevel@tonic-gate return(NULL);
24830Sstevel@tonic-gate regtail(pRExC_state, lastbr, br); /* BRANCH -> BRANCH. */
24840Sstevel@tonic-gate lastbr = br;
24850Sstevel@tonic-gate if (flags&HASWIDTH)
24860Sstevel@tonic-gate *flagp |= HASWIDTH;
24870Sstevel@tonic-gate *flagp |= flags&SPSTART;
24880Sstevel@tonic-gate }
24890Sstevel@tonic-gate
24900Sstevel@tonic-gate if (have_branch || paren != ':') {
24910Sstevel@tonic-gate /* Make a closing node, and hook it on the end. */
24920Sstevel@tonic-gate switch (paren) {
24930Sstevel@tonic-gate case ':':
24940Sstevel@tonic-gate ender = reg_node(pRExC_state, TAIL);
24950Sstevel@tonic-gate break;
24960Sstevel@tonic-gate case 1:
24970Sstevel@tonic-gate ender = reganode(pRExC_state, CLOSE, parno);
24980Sstevel@tonic-gate Set_Node_Offset(ender,RExC_parse+1); /* MJD */
24990Sstevel@tonic-gate Set_Node_Length(ender,1); /* MJD */
25000Sstevel@tonic-gate break;
25010Sstevel@tonic-gate case '<':
25020Sstevel@tonic-gate case ',':
25030Sstevel@tonic-gate case '=':
25040Sstevel@tonic-gate case '!':
25050Sstevel@tonic-gate *flagp &= ~HASWIDTH;
25060Sstevel@tonic-gate /* FALL THROUGH */
25070Sstevel@tonic-gate case '>':
25080Sstevel@tonic-gate ender = reg_node(pRExC_state, SUCCEED);
25090Sstevel@tonic-gate break;
25100Sstevel@tonic-gate case 0:
25110Sstevel@tonic-gate ender = reg_node(pRExC_state, END);
25120Sstevel@tonic-gate break;
25130Sstevel@tonic-gate }
25140Sstevel@tonic-gate regtail(pRExC_state, lastbr, ender);
25150Sstevel@tonic-gate
25160Sstevel@tonic-gate if (have_branch) {
25170Sstevel@tonic-gate /* Hook the tails of the branches to the closing node. */
25180Sstevel@tonic-gate for (br = ret; br != NULL; br = regnext(br)) {
25190Sstevel@tonic-gate regoptail(pRExC_state, br, ender);
25200Sstevel@tonic-gate }
25210Sstevel@tonic-gate }
25220Sstevel@tonic-gate }
25230Sstevel@tonic-gate
25240Sstevel@tonic-gate {
25250Sstevel@tonic-gate char *p;
25260Sstevel@tonic-gate static char parens[] = "=!<,>";
25270Sstevel@tonic-gate
25280Sstevel@tonic-gate if (paren && (p = strchr(parens, paren))) {
25290Sstevel@tonic-gate U8 node = ((p - parens) % 2) ? UNLESSM : IFMATCH;
25300Sstevel@tonic-gate int flag = (p - parens) > 1;
25310Sstevel@tonic-gate
25320Sstevel@tonic-gate if (paren == '>')
25330Sstevel@tonic-gate node = SUSPEND, flag = 0;
25340Sstevel@tonic-gate reginsert(pRExC_state, node,ret);
25350Sstevel@tonic-gate Set_Node_Cur_Length(ret);
25360Sstevel@tonic-gate Set_Node_Offset(ret, parse_start + 1);
25370Sstevel@tonic-gate ret->flags = flag;
25380Sstevel@tonic-gate regtail(pRExC_state, ret, reg_node(pRExC_state, TAIL));
25390Sstevel@tonic-gate }
25400Sstevel@tonic-gate }
25410Sstevel@tonic-gate
25420Sstevel@tonic-gate /* Check for proper termination. */
25430Sstevel@tonic-gate if (paren) {
25440Sstevel@tonic-gate RExC_flags = oregflags;
25450Sstevel@tonic-gate if (RExC_parse >= RExC_end || *nextchar(pRExC_state) != ')') {
25460Sstevel@tonic-gate RExC_parse = oregcomp_parse;
25470Sstevel@tonic-gate vFAIL("Unmatched (");
25480Sstevel@tonic-gate }
25490Sstevel@tonic-gate }
25500Sstevel@tonic-gate else if (!paren && RExC_parse < RExC_end) {
25510Sstevel@tonic-gate if (*RExC_parse == ')') {
25520Sstevel@tonic-gate RExC_parse++;
25530Sstevel@tonic-gate vFAIL("Unmatched )");
25540Sstevel@tonic-gate }
25550Sstevel@tonic-gate else
25560Sstevel@tonic-gate FAIL("Junk on end of regexp"); /* "Can't happen". */
25570Sstevel@tonic-gate /* NOTREACHED */
25580Sstevel@tonic-gate }
25590Sstevel@tonic-gate
25600Sstevel@tonic-gate return(ret);
25610Sstevel@tonic-gate }
25620Sstevel@tonic-gate
25630Sstevel@tonic-gate /*
25640Sstevel@tonic-gate - regbranch - one alternative of an | operator
25650Sstevel@tonic-gate *
25660Sstevel@tonic-gate * Implements the concatenation operator.
25670Sstevel@tonic-gate */
25680Sstevel@tonic-gate STATIC regnode *
S_regbranch(pTHX_ RExC_state_t * pRExC_state,I32 * flagp,I32 first)25690Sstevel@tonic-gate S_regbranch(pTHX_ RExC_state_t *pRExC_state, I32 *flagp, I32 first)
25700Sstevel@tonic-gate {
25710Sstevel@tonic-gate register regnode *ret;
25720Sstevel@tonic-gate register regnode *chain = NULL;
25730Sstevel@tonic-gate register regnode *latest;
25740Sstevel@tonic-gate I32 flags = 0, c = 0;
25750Sstevel@tonic-gate
25760Sstevel@tonic-gate if (first)
25770Sstevel@tonic-gate ret = NULL;
25780Sstevel@tonic-gate else {
25790Sstevel@tonic-gate if (!SIZE_ONLY && RExC_extralen)
25800Sstevel@tonic-gate ret = reganode(pRExC_state, BRANCHJ,0);
25810Sstevel@tonic-gate else {
25820Sstevel@tonic-gate ret = reg_node(pRExC_state, BRANCH);
25830Sstevel@tonic-gate Set_Node_Length(ret, 1);
25840Sstevel@tonic-gate }
25850Sstevel@tonic-gate }
25860Sstevel@tonic-gate
25870Sstevel@tonic-gate if (!first && SIZE_ONLY)
25880Sstevel@tonic-gate RExC_extralen += 1; /* BRANCHJ */
25890Sstevel@tonic-gate
25900Sstevel@tonic-gate *flagp = WORST; /* Tentatively. */
25910Sstevel@tonic-gate
25920Sstevel@tonic-gate RExC_parse--;
25930Sstevel@tonic-gate nextchar(pRExC_state);
25940Sstevel@tonic-gate while (RExC_parse < RExC_end && *RExC_parse != '|' && *RExC_parse != ')') {
25950Sstevel@tonic-gate flags &= ~TRYAGAIN;
25960Sstevel@tonic-gate latest = regpiece(pRExC_state, &flags);
25970Sstevel@tonic-gate if (latest == NULL) {
25980Sstevel@tonic-gate if (flags & TRYAGAIN)
25990Sstevel@tonic-gate continue;
26000Sstevel@tonic-gate return(NULL);
26010Sstevel@tonic-gate }
26020Sstevel@tonic-gate else if (ret == NULL)
26030Sstevel@tonic-gate ret = latest;
26040Sstevel@tonic-gate *flagp |= flags&HASWIDTH;
26050Sstevel@tonic-gate if (chain == NULL) /* First piece. */
26060Sstevel@tonic-gate *flagp |= flags&SPSTART;
26070Sstevel@tonic-gate else {
26080Sstevel@tonic-gate RExC_naughty++;
26090Sstevel@tonic-gate regtail(pRExC_state, chain, latest);
26100Sstevel@tonic-gate }
26110Sstevel@tonic-gate chain = latest;
26120Sstevel@tonic-gate c++;
26130Sstevel@tonic-gate }
26140Sstevel@tonic-gate if (chain == NULL) { /* Loop ran zero times. */
26150Sstevel@tonic-gate chain = reg_node(pRExC_state, NOTHING);
26160Sstevel@tonic-gate if (ret == NULL)
26170Sstevel@tonic-gate ret = chain;
26180Sstevel@tonic-gate }
26190Sstevel@tonic-gate if (c == 1) {
26200Sstevel@tonic-gate *flagp |= flags&SIMPLE;
26210Sstevel@tonic-gate }
26220Sstevel@tonic-gate
26230Sstevel@tonic-gate return(ret);
26240Sstevel@tonic-gate }
26250Sstevel@tonic-gate
26260Sstevel@tonic-gate /*
26270Sstevel@tonic-gate - regpiece - something followed by possible [*+?]
26280Sstevel@tonic-gate *
26290Sstevel@tonic-gate * Note that the branching code sequences used for ? and the general cases
26300Sstevel@tonic-gate * of * and + are somewhat optimized: they use the same NOTHING node as
26310Sstevel@tonic-gate * both the endmarker for their branch list and the body of the last branch.
26320Sstevel@tonic-gate * It might seem that this node could be dispensed with entirely, but the
26330Sstevel@tonic-gate * endmarker role is not redundant.
26340Sstevel@tonic-gate */
26350Sstevel@tonic-gate STATIC regnode *
S_regpiece(pTHX_ RExC_state_t * pRExC_state,I32 * flagp)26360Sstevel@tonic-gate S_regpiece(pTHX_ RExC_state_t *pRExC_state, I32 *flagp)
26370Sstevel@tonic-gate {
26380Sstevel@tonic-gate register regnode *ret;
26390Sstevel@tonic-gate register char op;
26400Sstevel@tonic-gate register char *next;
26410Sstevel@tonic-gate I32 flags;
26420Sstevel@tonic-gate char *origparse = RExC_parse;
26430Sstevel@tonic-gate char *maxpos;
26440Sstevel@tonic-gate I32 min;
26450Sstevel@tonic-gate I32 max = REG_INFTY;
26460Sstevel@tonic-gate char *parse_start;
26470Sstevel@tonic-gate
26480Sstevel@tonic-gate ret = regatom(pRExC_state, &flags);
26490Sstevel@tonic-gate if (ret == NULL) {
26500Sstevel@tonic-gate if (flags & TRYAGAIN)
26510Sstevel@tonic-gate *flagp |= TRYAGAIN;
26520Sstevel@tonic-gate return(NULL);
26530Sstevel@tonic-gate }
26540Sstevel@tonic-gate
26550Sstevel@tonic-gate op = *RExC_parse;
26560Sstevel@tonic-gate
26570Sstevel@tonic-gate if (op == '{' && regcurly(RExC_parse)) {
26580Sstevel@tonic-gate parse_start = RExC_parse; /* MJD */
26590Sstevel@tonic-gate next = RExC_parse + 1;
26600Sstevel@tonic-gate maxpos = Nullch;
26610Sstevel@tonic-gate while (isDIGIT(*next) || *next == ',') {
26620Sstevel@tonic-gate if (*next == ',') {
26630Sstevel@tonic-gate if (maxpos)
26640Sstevel@tonic-gate break;
26650Sstevel@tonic-gate else
26660Sstevel@tonic-gate maxpos = next;
26670Sstevel@tonic-gate }
26680Sstevel@tonic-gate next++;
26690Sstevel@tonic-gate }
26700Sstevel@tonic-gate if (*next == '}') { /* got one */
26710Sstevel@tonic-gate if (!maxpos)
26720Sstevel@tonic-gate maxpos = next;
26730Sstevel@tonic-gate RExC_parse++;
26740Sstevel@tonic-gate min = atoi(RExC_parse);
26750Sstevel@tonic-gate if (*maxpos == ',')
26760Sstevel@tonic-gate maxpos++;
26770Sstevel@tonic-gate else
26780Sstevel@tonic-gate maxpos = RExC_parse;
26790Sstevel@tonic-gate max = atoi(maxpos);
26800Sstevel@tonic-gate if (!max && *maxpos != '0')
26810Sstevel@tonic-gate max = REG_INFTY; /* meaning "infinity" */
26820Sstevel@tonic-gate else if (max >= REG_INFTY)
26830Sstevel@tonic-gate vFAIL2("Quantifier in {,} bigger than %d", REG_INFTY - 1);
26840Sstevel@tonic-gate RExC_parse = next;
26850Sstevel@tonic-gate nextchar(pRExC_state);
26860Sstevel@tonic-gate
26870Sstevel@tonic-gate do_curly:
26880Sstevel@tonic-gate if ((flags&SIMPLE)) {
26890Sstevel@tonic-gate RExC_naughty += 2 + RExC_naughty / 2;
26900Sstevel@tonic-gate reginsert(pRExC_state, CURLY, ret);
26910Sstevel@tonic-gate Set_Node_Offset(ret, parse_start+1); /* MJD */
26920Sstevel@tonic-gate Set_Node_Cur_Length(ret);
26930Sstevel@tonic-gate }
26940Sstevel@tonic-gate else {
26950Sstevel@tonic-gate regnode *w = reg_node(pRExC_state, WHILEM);
26960Sstevel@tonic-gate
26970Sstevel@tonic-gate w->flags = 0;
26980Sstevel@tonic-gate regtail(pRExC_state, ret, w);
26990Sstevel@tonic-gate if (!SIZE_ONLY && RExC_extralen) {
27000Sstevel@tonic-gate reginsert(pRExC_state, LONGJMP,ret);
27010Sstevel@tonic-gate reginsert(pRExC_state, NOTHING,ret);
27020Sstevel@tonic-gate NEXT_OFF(ret) = 3; /* Go over LONGJMP. */
27030Sstevel@tonic-gate }
27040Sstevel@tonic-gate reginsert(pRExC_state, CURLYX,ret);
27050Sstevel@tonic-gate /* MJD hk */
27060Sstevel@tonic-gate Set_Node_Offset(ret, parse_start+1);
27070Sstevel@tonic-gate Set_Node_Length(ret,
27080Sstevel@tonic-gate op == '{' ? (RExC_parse - parse_start) : 1);
27090Sstevel@tonic-gate
27100Sstevel@tonic-gate if (!SIZE_ONLY && RExC_extralen)
27110Sstevel@tonic-gate NEXT_OFF(ret) = 3; /* Go over NOTHING to LONGJMP. */
27120Sstevel@tonic-gate regtail(pRExC_state, ret, reg_node(pRExC_state, NOTHING));
27130Sstevel@tonic-gate if (SIZE_ONLY)
27140Sstevel@tonic-gate RExC_whilem_seen++, RExC_extralen += 3;
27150Sstevel@tonic-gate RExC_naughty += 4 + RExC_naughty; /* compound interest */
27160Sstevel@tonic-gate }
27170Sstevel@tonic-gate ret->flags = 0;
27180Sstevel@tonic-gate
27190Sstevel@tonic-gate if (min > 0)
27200Sstevel@tonic-gate *flagp = WORST;
27210Sstevel@tonic-gate if (max > 0)
27220Sstevel@tonic-gate *flagp |= HASWIDTH;
27230Sstevel@tonic-gate if (max && max < min)
27240Sstevel@tonic-gate vFAIL("Can't do {n,m} with n > m");
27250Sstevel@tonic-gate if (!SIZE_ONLY) {
27260Sstevel@tonic-gate ARG1_SET(ret, (U16)min);
27270Sstevel@tonic-gate ARG2_SET(ret, (U16)max);
27280Sstevel@tonic-gate }
27290Sstevel@tonic-gate
27300Sstevel@tonic-gate goto nest_check;
27310Sstevel@tonic-gate }
27320Sstevel@tonic-gate }
27330Sstevel@tonic-gate
27340Sstevel@tonic-gate if (!ISMULT1(op)) {
27350Sstevel@tonic-gate *flagp = flags;
27360Sstevel@tonic-gate return(ret);
27370Sstevel@tonic-gate }
27380Sstevel@tonic-gate
27390Sstevel@tonic-gate #if 0 /* Now runtime fix should be reliable. */
27400Sstevel@tonic-gate
27410Sstevel@tonic-gate /* if this is reinstated, don't forget to put this back into perldiag:
27420Sstevel@tonic-gate
27430Sstevel@tonic-gate =item Regexp *+ operand could be empty at {#} in regex m/%s/
27440Sstevel@tonic-gate
27450Sstevel@tonic-gate (F) The part of the regexp subject to either the * or + quantifier
27460Sstevel@tonic-gate could match an empty string. The {#} shows in the regular
27470Sstevel@tonic-gate expression about where the problem was discovered.
27480Sstevel@tonic-gate
27490Sstevel@tonic-gate */
27500Sstevel@tonic-gate
27510Sstevel@tonic-gate if (!(flags&HASWIDTH) && op != '?')
27520Sstevel@tonic-gate vFAIL("Regexp *+ operand could be empty");
27530Sstevel@tonic-gate #endif
27540Sstevel@tonic-gate
27550Sstevel@tonic-gate parse_start = RExC_parse;
27560Sstevel@tonic-gate nextchar(pRExC_state);
27570Sstevel@tonic-gate
27580Sstevel@tonic-gate *flagp = (op != '+') ? (WORST|SPSTART|HASWIDTH) : (WORST|HASWIDTH);
27590Sstevel@tonic-gate
27600Sstevel@tonic-gate if (op == '*' && (flags&SIMPLE)) {
27610Sstevel@tonic-gate reginsert(pRExC_state, STAR, ret);
27620Sstevel@tonic-gate ret->flags = 0;
27630Sstevel@tonic-gate RExC_naughty += 4;
27640Sstevel@tonic-gate }
27650Sstevel@tonic-gate else if (op == '*') {
27660Sstevel@tonic-gate min = 0;
27670Sstevel@tonic-gate goto do_curly;
27680Sstevel@tonic-gate }
27690Sstevel@tonic-gate else if (op == '+' && (flags&SIMPLE)) {
27700Sstevel@tonic-gate reginsert(pRExC_state, PLUS, ret);
27710Sstevel@tonic-gate ret->flags = 0;
27720Sstevel@tonic-gate RExC_naughty += 3;
27730Sstevel@tonic-gate }
27740Sstevel@tonic-gate else if (op == '+') {
27750Sstevel@tonic-gate min = 1;
27760Sstevel@tonic-gate goto do_curly;
27770Sstevel@tonic-gate }
27780Sstevel@tonic-gate else if (op == '?') {
27790Sstevel@tonic-gate min = 0; max = 1;
27800Sstevel@tonic-gate goto do_curly;
27810Sstevel@tonic-gate }
27820Sstevel@tonic-gate nest_check:
27830Sstevel@tonic-gate if (ckWARN(WARN_REGEXP) && !SIZE_ONLY && !(flags&HASWIDTH) && max > REG_INFTY/3) {
27840Sstevel@tonic-gate vWARN3(RExC_parse,
27850Sstevel@tonic-gate "%.*s matches null string many times",
27860Sstevel@tonic-gate RExC_parse - origparse,
27870Sstevel@tonic-gate origparse);
27880Sstevel@tonic-gate }
27890Sstevel@tonic-gate
27900Sstevel@tonic-gate if (*RExC_parse == '?') {
27910Sstevel@tonic-gate nextchar(pRExC_state);
27920Sstevel@tonic-gate reginsert(pRExC_state, MINMOD, ret);
27930Sstevel@tonic-gate regtail(pRExC_state, ret, ret + NODE_STEP_REGNODE);
27940Sstevel@tonic-gate }
27950Sstevel@tonic-gate if (ISMULT2(RExC_parse)) {
27960Sstevel@tonic-gate RExC_parse++;
27970Sstevel@tonic-gate vFAIL("Nested quantifiers");
27980Sstevel@tonic-gate }
27990Sstevel@tonic-gate
28000Sstevel@tonic-gate return(ret);
28010Sstevel@tonic-gate }
28020Sstevel@tonic-gate
28030Sstevel@tonic-gate /*
28040Sstevel@tonic-gate - regatom - the lowest level
28050Sstevel@tonic-gate *
28060Sstevel@tonic-gate * Optimization: gobbles an entire sequence of ordinary characters so that
28070Sstevel@tonic-gate * it can turn them into a single node, which is smaller to store and
28080Sstevel@tonic-gate * faster to run. Backslashed characters are exceptions, each becoming a
28090Sstevel@tonic-gate * separate node; the code is simpler that way and it's not worth fixing.
28100Sstevel@tonic-gate *
28110Sstevel@tonic-gate * [Yes, it is worth fixing, some scripts can run twice the speed.] */
28120Sstevel@tonic-gate STATIC regnode *
S_regatom(pTHX_ RExC_state_t * pRExC_state,I32 * flagp)28130Sstevel@tonic-gate S_regatom(pTHX_ RExC_state_t *pRExC_state, I32 *flagp)
28140Sstevel@tonic-gate {
28150Sstevel@tonic-gate register regnode *ret = 0;
28160Sstevel@tonic-gate I32 flags;
28170Sstevel@tonic-gate char *parse_start = RExC_parse;
28180Sstevel@tonic-gate
28190Sstevel@tonic-gate *flagp = WORST; /* Tentatively. */
28200Sstevel@tonic-gate
28210Sstevel@tonic-gate tryagain:
28220Sstevel@tonic-gate switch (*RExC_parse) {
28230Sstevel@tonic-gate case '^':
28240Sstevel@tonic-gate RExC_seen_zerolen++;
28250Sstevel@tonic-gate nextchar(pRExC_state);
28260Sstevel@tonic-gate if (RExC_flags & PMf_MULTILINE)
28270Sstevel@tonic-gate ret = reg_node(pRExC_state, MBOL);
28280Sstevel@tonic-gate else if (RExC_flags & PMf_SINGLELINE)
28290Sstevel@tonic-gate ret = reg_node(pRExC_state, SBOL);
28300Sstevel@tonic-gate else
28310Sstevel@tonic-gate ret = reg_node(pRExC_state, BOL);
28320Sstevel@tonic-gate Set_Node_Length(ret, 1); /* MJD */
28330Sstevel@tonic-gate break;
28340Sstevel@tonic-gate case '$':
28350Sstevel@tonic-gate nextchar(pRExC_state);
28360Sstevel@tonic-gate if (*RExC_parse)
28370Sstevel@tonic-gate RExC_seen_zerolen++;
28380Sstevel@tonic-gate if (RExC_flags & PMf_MULTILINE)
28390Sstevel@tonic-gate ret = reg_node(pRExC_state, MEOL);
28400Sstevel@tonic-gate else if (RExC_flags & PMf_SINGLELINE)
28410Sstevel@tonic-gate ret = reg_node(pRExC_state, SEOL);
28420Sstevel@tonic-gate else
28430Sstevel@tonic-gate ret = reg_node(pRExC_state, EOL);
28440Sstevel@tonic-gate Set_Node_Length(ret, 1); /* MJD */
28450Sstevel@tonic-gate break;
28460Sstevel@tonic-gate case '.':
28470Sstevel@tonic-gate nextchar(pRExC_state);
28480Sstevel@tonic-gate if (RExC_flags & PMf_SINGLELINE)
28490Sstevel@tonic-gate ret = reg_node(pRExC_state, SANY);
28500Sstevel@tonic-gate else
28510Sstevel@tonic-gate ret = reg_node(pRExC_state, REG_ANY);
28520Sstevel@tonic-gate *flagp |= HASWIDTH|SIMPLE;
28530Sstevel@tonic-gate RExC_naughty++;
28540Sstevel@tonic-gate Set_Node_Length(ret, 1); /* MJD */
28550Sstevel@tonic-gate break;
28560Sstevel@tonic-gate case '[':
28570Sstevel@tonic-gate {
28580Sstevel@tonic-gate char *oregcomp_parse = ++RExC_parse;
28590Sstevel@tonic-gate ret = regclass(pRExC_state);
28600Sstevel@tonic-gate if (*RExC_parse != ']') {
28610Sstevel@tonic-gate RExC_parse = oregcomp_parse;
28620Sstevel@tonic-gate vFAIL("Unmatched [");
28630Sstevel@tonic-gate }
28640Sstevel@tonic-gate nextchar(pRExC_state);
28650Sstevel@tonic-gate *flagp |= HASWIDTH|SIMPLE;
28660Sstevel@tonic-gate Set_Node_Length(ret, RExC_parse - oregcomp_parse + 1); /* MJD */
28670Sstevel@tonic-gate break;
28680Sstevel@tonic-gate }
28690Sstevel@tonic-gate case '(':
28700Sstevel@tonic-gate nextchar(pRExC_state);
28710Sstevel@tonic-gate ret = reg(pRExC_state, 1, &flags);
28720Sstevel@tonic-gate if (ret == NULL) {
28730Sstevel@tonic-gate if (flags & TRYAGAIN) {
28740Sstevel@tonic-gate if (RExC_parse == RExC_end) {
28750Sstevel@tonic-gate /* Make parent create an empty node if needed. */
28760Sstevel@tonic-gate *flagp |= TRYAGAIN;
28770Sstevel@tonic-gate return(NULL);
28780Sstevel@tonic-gate }
28790Sstevel@tonic-gate goto tryagain;
28800Sstevel@tonic-gate }
28810Sstevel@tonic-gate return(NULL);
28820Sstevel@tonic-gate }
28830Sstevel@tonic-gate *flagp |= flags&(HASWIDTH|SPSTART|SIMPLE);
28840Sstevel@tonic-gate break;
28850Sstevel@tonic-gate case '|':
28860Sstevel@tonic-gate case ')':
28870Sstevel@tonic-gate if (flags & TRYAGAIN) {
28880Sstevel@tonic-gate *flagp |= TRYAGAIN;
28890Sstevel@tonic-gate return NULL;
28900Sstevel@tonic-gate }
28910Sstevel@tonic-gate vFAIL("Internal urp");
28920Sstevel@tonic-gate /* Supposed to be caught earlier. */
28930Sstevel@tonic-gate break;
28940Sstevel@tonic-gate case '{':
28950Sstevel@tonic-gate if (!regcurly(RExC_parse)) {
28960Sstevel@tonic-gate RExC_parse++;
28970Sstevel@tonic-gate goto defchar;
28980Sstevel@tonic-gate }
28990Sstevel@tonic-gate /* FALL THROUGH */
29000Sstevel@tonic-gate case '?':
29010Sstevel@tonic-gate case '+':
29020Sstevel@tonic-gate case '*':
29030Sstevel@tonic-gate RExC_parse++;
29040Sstevel@tonic-gate vFAIL("Quantifier follows nothing");
29050Sstevel@tonic-gate break;
29060Sstevel@tonic-gate case '\\':
29070Sstevel@tonic-gate switch (*++RExC_parse) {
29080Sstevel@tonic-gate case 'A':
29090Sstevel@tonic-gate RExC_seen_zerolen++;
29100Sstevel@tonic-gate ret = reg_node(pRExC_state, SBOL);
29110Sstevel@tonic-gate *flagp |= SIMPLE;
29120Sstevel@tonic-gate nextchar(pRExC_state);
29130Sstevel@tonic-gate Set_Node_Length(ret, 2); /* MJD */
29140Sstevel@tonic-gate break;
29150Sstevel@tonic-gate case 'G':
29160Sstevel@tonic-gate ret = reg_node(pRExC_state, GPOS);
29170Sstevel@tonic-gate RExC_seen |= REG_SEEN_GPOS;
29180Sstevel@tonic-gate *flagp |= SIMPLE;
29190Sstevel@tonic-gate nextchar(pRExC_state);
29200Sstevel@tonic-gate Set_Node_Length(ret, 2); /* MJD */
29210Sstevel@tonic-gate break;
29220Sstevel@tonic-gate case 'Z':
29230Sstevel@tonic-gate ret = reg_node(pRExC_state, SEOL);
29240Sstevel@tonic-gate *flagp |= SIMPLE;
29250Sstevel@tonic-gate RExC_seen_zerolen++; /* Do not optimize RE away */
29260Sstevel@tonic-gate nextchar(pRExC_state);
29270Sstevel@tonic-gate break;
29280Sstevel@tonic-gate case 'z':
29290Sstevel@tonic-gate ret = reg_node(pRExC_state, EOS);
29300Sstevel@tonic-gate *flagp |= SIMPLE;
29310Sstevel@tonic-gate RExC_seen_zerolen++; /* Do not optimize RE away */
29320Sstevel@tonic-gate nextchar(pRExC_state);
29330Sstevel@tonic-gate Set_Node_Length(ret, 2); /* MJD */
29340Sstevel@tonic-gate break;
29350Sstevel@tonic-gate case 'C':
29360Sstevel@tonic-gate ret = reg_node(pRExC_state, CANY);
29370Sstevel@tonic-gate RExC_seen |= REG_SEEN_CANY;
29380Sstevel@tonic-gate *flagp |= HASWIDTH|SIMPLE;
29390Sstevel@tonic-gate nextchar(pRExC_state);
29400Sstevel@tonic-gate Set_Node_Length(ret, 2); /* MJD */
29410Sstevel@tonic-gate break;
29420Sstevel@tonic-gate case 'X':
29430Sstevel@tonic-gate ret = reg_node(pRExC_state, CLUMP);
29440Sstevel@tonic-gate *flagp |= HASWIDTH;
29450Sstevel@tonic-gate nextchar(pRExC_state);
29460Sstevel@tonic-gate Set_Node_Length(ret, 2); /* MJD */
29470Sstevel@tonic-gate break;
29480Sstevel@tonic-gate case 'w':
29490Sstevel@tonic-gate ret = reg_node(pRExC_state, (U8)(LOC ? ALNUML : ALNUM));
29500Sstevel@tonic-gate *flagp |= HASWIDTH|SIMPLE;
29510Sstevel@tonic-gate nextchar(pRExC_state);
29520Sstevel@tonic-gate Set_Node_Length(ret, 2); /* MJD */
29530Sstevel@tonic-gate break;
29540Sstevel@tonic-gate case 'W':
29550Sstevel@tonic-gate ret = reg_node(pRExC_state, (U8)(LOC ? NALNUML : NALNUM));
29560Sstevel@tonic-gate *flagp |= HASWIDTH|SIMPLE;
29570Sstevel@tonic-gate nextchar(pRExC_state);
29580Sstevel@tonic-gate Set_Node_Length(ret, 2); /* MJD */
29590Sstevel@tonic-gate break;
29600Sstevel@tonic-gate case 'b':
29610Sstevel@tonic-gate RExC_seen_zerolen++;
29620Sstevel@tonic-gate RExC_seen |= REG_SEEN_LOOKBEHIND;
29630Sstevel@tonic-gate ret = reg_node(pRExC_state, (U8)(LOC ? BOUNDL : BOUND));
29640Sstevel@tonic-gate *flagp |= SIMPLE;
29650Sstevel@tonic-gate nextchar(pRExC_state);
29660Sstevel@tonic-gate Set_Node_Length(ret, 2); /* MJD */
29670Sstevel@tonic-gate break;
29680Sstevel@tonic-gate case 'B':
29690Sstevel@tonic-gate RExC_seen_zerolen++;
29700Sstevel@tonic-gate RExC_seen |= REG_SEEN_LOOKBEHIND;
29710Sstevel@tonic-gate ret = reg_node(pRExC_state, (U8)(LOC ? NBOUNDL : NBOUND));
29720Sstevel@tonic-gate *flagp |= SIMPLE;
29730Sstevel@tonic-gate nextchar(pRExC_state);
29740Sstevel@tonic-gate Set_Node_Length(ret, 2); /* MJD */
29750Sstevel@tonic-gate break;
29760Sstevel@tonic-gate case 's':
29770Sstevel@tonic-gate ret = reg_node(pRExC_state, (U8)(LOC ? SPACEL : SPACE));
29780Sstevel@tonic-gate *flagp |= HASWIDTH|SIMPLE;
29790Sstevel@tonic-gate nextchar(pRExC_state);
29800Sstevel@tonic-gate Set_Node_Length(ret, 2); /* MJD */
29810Sstevel@tonic-gate break;
29820Sstevel@tonic-gate case 'S':
29830Sstevel@tonic-gate ret = reg_node(pRExC_state, (U8)(LOC ? NSPACEL : NSPACE));
29840Sstevel@tonic-gate *flagp |= HASWIDTH|SIMPLE;
29850Sstevel@tonic-gate nextchar(pRExC_state);
29860Sstevel@tonic-gate Set_Node_Length(ret, 2); /* MJD */
29870Sstevel@tonic-gate break;
29880Sstevel@tonic-gate case 'd':
29890Sstevel@tonic-gate ret = reg_node(pRExC_state, DIGIT);
29900Sstevel@tonic-gate *flagp |= HASWIDTH|SIMPLE;
29910Sstevel@tonic-gate nextchar(pRExC_state);
29920Sstevel@tonic-gate Set_Node_Length(ret, 2); /* MJD */
29930Sstevel@tonic-gate break;
29940Sstevel@tonic-gate case 'D':
29950Sstevel@tonic-gate ret = reg_node(pRExC_state, NDIGIT);
29960Sstevel@tonic-gate *flagp |= HASWIDTH|SIMPLE;
29970Sstevel@tonic-gate nextchar(pRExC_state);
29980Sstevel@tonic-gate Set_Node_Length(ret, 2); /* MJD */
29990Sstevel@tonic-gate break;
30000Sstevel@tonic-gate case 'p':
30010Sstevel@tonic-gate case 'P':
30020Sstevel@tonic-gate {
30030Sstevel@tonic-gate char* oldregxend = RExC_end;
30040Sstevel@tonic-gate char* parse_start = RExC_parse - 2;
30050Sstevel@tonic-gate
30060Sstevel@tonic-gate if (RExC_parse[1] == '{') {
30070Sstevel@tonic-gate /* a lovely hack--pretend we saw [\pX] instead */
30080Sstevel@tonic-gate RExC_end = strchr(RExC_parse, '}');
30090Sstevel@tonic-gate if (!RExC_end) {
30100Sstevel@tonic-gate U8 c = (U8)*RExC_parse;
30110Sstevel@tonic-gate RExC_parse += 2;
30120Sstevel@tonic-gate RExC_end = oldregxend;
30130Sstevel@tonic-gate vFAIL2("Missing right brace on \\%c{}", c);
30140Sstevel@tonic-gate }
30150Sstevel@tonic-gate RExC_end++;
30160Sstevel@tonic-gate }
30170Sstevel@tonic-gate else {
30180Sstevel@tonic-gate RExC_end = RExC_parse + 2;
30190Sstevel@tonic-gate if (RExC_end > oldregxend)
30200Sstevel@tonic-gate RExC_end = oldregxend;
30210Sstevel@tonic-gate }
30220Sstevel@tonic-gate RExC_parse--;
30230Sstevel@tonic-gate
30240Sstevel@tonic-gate ret = regclass(pRExC_state);
30250Sstevel@tonic-gate
30260Sstevel@tonic-gate RExC_end = oldregxend;
30270Sstevel@tonic-gate RExC_parse--;
30280Sstevel@tonic-gate
30290Sstevel@tonic-gate Set_Node_Offset(ret, parse_start + 2);
30300Sstevel@tonic-gate Set_Node_Cur_Length(ret);
30310Sstevel@tonic-gate nextchar(pRExC_state);
30320Sstevel@tonic-gate *flagp |= HASWIDTH|SIMPLE;
30330Sstevel@tonic-gate }
30340Sstevel@tonic-gate break;
30350Sstevel@tonic-gate case 'n':
30360Sstevel@tonic-gate case 'r':
30370Sstevel@tonic-gate case 't':
30380Sstevel@tonic-gate case 'f':
30390Sstevel@tonic-gate case 'e':
30400Sstevel@tonic-gate case 'a':
30410Sstevel@tonic-gate case 'x':
30420Sstevel@tonic-gate case 'c':
30430Sstevel@tonic-gate case '0':
30440Sstevel@tonic-gate goto defchar;
30450Sstevel@tonic-gate case '1': case '2': case '3': case '4':
30460Sstevel@tonic-gate case '5': case '6': case '7': case '8': case '9':
30470Sstevel@tonic-gate {
30480Sstevel@tonic-gate I32 num = atoi(RExC_parse);
30490Sstevel@tonic-gate
30500Sstevel@tonic-gate if (num > 9 && num >= RExC_npar)
30510Sstevel@tonic-gate goto defchar;
30520Sstevel@tonic-gate else {
30530Sstevel@tonic-gate char * parse_start = RExC_parse - 1; /* MJD */
30540Sstevel@tonic-gate while (isDIGIT(*RExC_parse))
30550Sstevel@tonic-gate RExC_parse++;
30560Sstevel@tonic-gate
30570Sstevel@tonic-gate if (!SIZE_ONLY && num > (I32)RExC_rx->nparens)
30580Sstevel@tonic-gate vFAIL("Reference to nonexistent group");
30590Sstevel@tonic-gate RExC_sawback = 1;
30600Sstevel@tonic-gate ret = reganode(pRExC_state,
30610Sstevel@tonic-gate (U8)(FOLD ? (LOC ? REFFL : REFF) : REF),
30620Sstevel@tonic-gate num);
30630Sstevel@tonic-gate *flagp |= HASWIDTH;
30640Sstevel@tonic-gate
30650Sstevel@tonic-gate /* override incorrect value set in reganode MJD */
30660Sstevel@tonic-gate Set_Node_Offset(ret, parse_start+1);
30670Sstevel@tonic-gate Set_Node_Cur_Length(ret); /* MJD */
30680Sstevel@tonic-gate RExC_parse--;
30690Sstevel@tonic-gate nextchar(pRExC_state);
30700Sstevel@tonic-gate }
30710Sstevel@tonic-gate }
30720Sstevel@tonic-gate break;
30730Sstevel@tonic-gate case '\0':
30740Sstevel@tonic-gate if (RExC_parse >= RExC_end)
30750Sstevel@tonic-gate FAIL("Trailing \\");
30760Sstevel@tonic-gate /* FALL THROUGH */
30770Sstevel@tonic-gate default:
30780Sstevel@tonic-gate /* Do not generate `unrecognized' warnings here, we fall
30790Sstevel@tonic-gate back into the quick-grab loop below */
30800Sstevel@tonic-gate parse_start--;
30810Sstevel@tonic-gate goto defchar;
30820Sstevel@tonic-gate }
30830Sstevel@tonic-gate break;
30840Sstevel@tonic-gate
30850Sstevel@tonic-gate case '#':
30860Sstevel@tonic-gate if (RExC_flags & PMf_EXTENDED) {
30870Sstevel@tonic-gate while (RExC_parse < RExC_end && *RExC_parse != '\n') RExC_parse++;
30880Sstevel@tonic-gate if (RExC_parse < RExC_end)
30890Sstevel@tonic-gate goto tryagain;
30900Sstevel@tonic-gate }
30910Sstevel@tonic-gate /* FALL THROUGH */
30920Sstevel@tonic-gate
30930Sstevel@tonic-gate default: {
30940Sstevel@tonic-gate register STRLEN len;
30950Sstevel@tonic-gate register UV ender;
30960Sstevel@tonic-gate register char *p;
30970Sstevel@tonic-gate char *oldp, *s;
30980Sstevel@tonic-gate STRLEN numlen;
30990Sstevel@tonic-gate STRLEN foldlen;
31000Sstevel@tonic-gate U8 tmpbuf[UTF8_MAXLEN_FOLD+1], *foldbuf;
31010Sstevel@tonic-gate
31020Sstevel@tonic-gate parse_start = RExC_parse - 1;
31030Sstevel@tonic-gate
31040Sstevel@tonic-gate RExC_parse++;
31050Sstevel@tonic-gate
31060Sstevel@tonic-gate defchar:
31070Sstevel@tonic-gate ender = 0;
31080Sstevel@tonic-gate ret = reg_node(pRExC_state,
31090Sstevel@tonic-gate (U8)(FOLD ? (LOC ? EXACTFL : EXACTF) : EXACT));
31100Sstevel@tonic-gate s = STRING(ret);
31110Sstevel@tonic-gate for (len = 0, p = RExC_parse - 1;
31120Sstevel@tonic-gate len < 127 && p < RExC_end;
31130Sstevel@tonic-gate len++)
31140Sstevel@tonic-gate {
31150Sstevel@tonic-gate oldp = p;
31160Sstevel@tonic-gate
31170Sstevel@tonic-gate if (RExC_flags & PMf_EXTENDED)
31180Sstevel@tonic-gate p = regwhite(p, RExC_end);
31190Sstevel@tonic-gate switch (*p) {
31200Sstevel@tonic-gate case '^':
31210Sstevel@tonic-gate case '$':
31220Sstevel@tonic-gate case '.':
31230Sstevel@tonic-gate case '[':
31240Sstevel@tonic-gate case '(':
31250Sstevel@tonic-gate case ')':
31260Sstevel@tonic-gate case '|':
31270Sstevel@tonic-gate goto loopdone;
31280Sstevel@tonic-gate case '\\':
31290Sstevel@tonic-gate switch (*++p) {
31300Sstevel@tonic-gate case 'A':
31310Sstevel@tonic-gate case 'C':
31320Sstevel@tonic-gate case 'X':
31330Sstevel@tonic-gate case 'G':
31340Sstevel@tonic-gate case 'Z':
31350Sstevel@tonic-gate case 'z':
31360Sstevel@tonic-gate case 'w':
31370Sstevel@tonic-gate case 'W':
31380Sstevel@tonic-gate case 'b':
31390Sstevel@tonic-gate case 'B':
31400Sstevel@tonic-gate case 's':
31410Sstevel@tonic-gate case 'S':
31420Sstevel@tonic-gate case 'd':
31430Sstevel@tonic-gate case 'D':
31440Sstevel@tonic-gate case 'p':
31450Sstevel@tonic-gate case 'P':
31460Sstevel@tonic-gate --p;
31470Sstevel@tonic-gate goto loopdone;
31480Sstevel@tonic-gate case 'n':
31490Sstevel@tonic-gate ender = '\n';
31500Sstevel@tonic-gate p++;
31510Sstevel@tonic-gate break;
31520Sstevel@tonic-gate case 'r':
31530Sstevel@tonic-gate ender = '\r';
31540Sstevel@tonic-gate p++;
31550Sstevel@tonic-gate break;
31560Sstevel@tonic-gate case 't':
31570Sstevel@tonic-gate ender = '\t';
31580Sstevel@tonic-gate p++;
31590Sstevel@tonic-gate break;
31600Sstevel@tonic-gate case 'f':
31610Sstevel@tonic-gate ender = '\f';
31620Sstevel@tonic-gate p++;
31630Sstevel@tonic-gate break;
31640Sstevel@tonic-gate case 'e':
31650Sstevel@tonic-gate ender = ASCII_TO_NATIVE('\033');
31660Sstevel@tonic-gate p++;
31670Sstevel@tonic-gate break;
31680Sstevel@tonic-gate case 'a':
31690Sstevel@tonic-gate ender = ASCII_TO_NATIVE('\007');
31700Sstevel@tonic-gate p++;
31710Sstevel@tonic-gate break;
31720Sstevel@tonic-gate case 'x':
31730Sstevel@tonic-gate if (*++p == '{') {
31740Sstevel@tonic-gate char* e = strchr(p, '}');
31750Sstevel@tonic-gate
31760Sstevel@tonic-gate if (!e) {
31770Sstevel@tonic-gate RExC_parse = p + 1;
31780Sstevel@tonic-gate vFAIL("Missing right brace on \\x{}");
31790Sstevel@tonic-gate }
31800Sstevel@tonic-gate else {
31810Sstevel@tonic-gate I32 flags = PERL_SCAN_ALLOW_UNDERSCORES
31820Sstevel@tonic-gate | PERL_SCAN_DISALLOW_PREFIX;
31830Sstevel@tonic-gate numlen = e - p - 1;
31840Sstevel@tonic-gate ender = grok_hex(p + 1, &numlen, &flags, NULL);
31850Sstevel@tonic-gate if (ender > 0xff)
31860Sstevel@tonic-gate RExC_utf8 = 1;
31870Sstevel@tonic-gate p = e + 1;
31880Sstevel@tonic-gate }
31890Sstevel@tonic-gate }
31900Sstevel@tonic-gate else {
31910Sstevel@tonic-gate I32 flags = PERL_SCAN_DISALLOW_PREFIX;
31920Sstevel@tonic-gate numlen = 2;
31930Sstevel@tonic-gate ender = grok_hex(p, &numlen, &flags, NULL);
31940Sstevel@tonic-gate p += numlen;
31950Sstevel@tonic-gate }
31960Sstevel@tonic-gate break;
31970Sstevel@tonic-gate case 'c':
31980Sstevel@tonic-gate p++;
31990Sstevel@tonic-gate ender = UCHARAT(p++);
32000Sstevel@tonic-gate ender = toCTRL(ender);
32010Sstevel@tonic-gate break;
32020Sstevel@tonic-gate case '0': case '1': case '2': case '3':case '4':
32030Sstevel@tonic-gate case '5': case '6': case '7': case '8':case '9':
32040Sstevel@tonic-gate if (*p == '0' ||
32050Sstevel@tonic-gate (isDIGIT(p[1]) && atoi(p) >= RExC_npar) ) {
32060Sstevel@tonic-gate I32 flags = 0;
32070Sstevel@tonic-gate numlen = 3;
32080Sstevel@tonic-gate ender = grok_oct(p, &numlen, &flags, NULL);
32090Sstevel@tonic-gate p += numlen;
32100Sstevel@tonic-gate }
32110Sstevel@tonic-gate else {
32120Sstevel@tonic-gate --p;
32130Sstevel@tonic-gate goto loopdone;
32140Sstevel@tonic-gate }
32150Sstevel@tonic-gate break;
32160Sstevel@tonic-gate case '\0':
32170Sstevel@tonic-gate if (p >= RExC_end)
32180Sstevel@tonic-gate FAIL("Trailing \\");
32190Sstevel@tonic-gate /* FALL THROUGH */
32200Sstevel@tonic-gate default:
32210Sstevel@tonic-gate if (!SIZE_ONLY && ckWARN(WARN_REGEXP) && isALPHA(*p))
32220Sstevel@tonic-gate vWARN2(p + 1, "Unrecognized escape \\%c passed through", UCHARAT(p));
32230Sstevel@tonic-gate goto normal_default;
32240Sstevel@tonic-gate }
32250Sstevel@tonic-gate break;
32260Sstevel@tonic-gate default:
32270Sstevel@tonic-gate normal_default:
32280Sstevel@tonic-gate if (UTF8_IS_START(*p) && UTF) {
32290Sstevel@tonic-gate ender = utf8n_to_uvchr((U8*)p, RExC_end - p,
32300Sstevel@tonic-gate &numlen, 0);
32310Sstevel@tonic-gate p += numlen;
32320Sstevel@tonic-gate }
32330Sstevel@tonic-gate else
32340Sstevel@tonic-gate ender = *p++;
32350Sstevel@tonic-gate break;
32360Sstevel@tonic-gate }
32370Sstevel@tonic-gate if (RExC_flags & PMf_EXTENDED)
32380Sstevel@tonic-gate p = regwhite(p, RExC_end);
32390Sstevel@tonic-gate if (UTF && FOLD) {
32400Sstevel@tonic-gate /* Prime the casefolded buffer. */
32410Sstevel@tonic-gate ender = toFOLD_uni(ender, tmpbuf, &foldlen);
32420Sstevel@tonic-gate }
32430Sstevel@tonic-gate if (ISMULT2(p)) { /* Back off on ?+*. */
32440Sstevel@tonic-gate if (len)
32450Sstevel@tonic-gate p = oldp;
32460Sstevel@tonic-gate else if (UTF) {
32470Sstevel@tonic-gate STRLEN unilen;
32480Sstevel@tonic-gate
32490Sstevel@tonic-gate if (FOLD) {
32500Sstevel@tonic-gate /* Emit all the Unicode characters. */
32510Sstevel@tonic-gate for (foldbuf = tmpbuf;
32520Sstevel@tonic-gate foldlen;
32530Sstevel@tonic-gate foldlen -= numlen) {
32540Sstevel@tonic-gate ender = utf8_to_uvchr(foldbuf, &numlen);
32550Sstevel@tonic-gate if (numlen > 0) {
32560Sstevel@tonic-gate reguni(pRExC_state, ender, s, &unilen);
32570Sstevel@tonic-gate s += unilen;
32580Sstevel@tonic-gate len += unilen;
32590Sstevel@tonic-gate /* In EBCDIC the numlen
32600Sstevel@tonic-gate * and unilen can differ. */
32610Sstevel@tonic-gate foldbuf += numlen;
32620Sstevel@tonic-gate if (numlen >= foldlen)
32630Sstevel@tonic-gate break;
32640Sstevel@tonic-gate }
32650Sstevel@tonic-gate else
32660Sstevel@tonic-gate break; /* "Can't happen." */
32670Sstevel@tonic-gate }
32680Sstevel@tonic-gate }
32690Sstevel@tonic-gate else {
32700Sstevel@tonic-gate reguni(pRExC_state, ender, s, &unilen);
32710Sstevel@tonic-gate if (unilen > 0) {
32720Sstevel@tonic-gate s += unilen;
32730Sstevel@tonic-gate len += unilen;
32740Sstevel@tonic-gate }
32750Sstevel@tonic-gate }
32760Sstevel@tonic-gate }
32770Sstevel@tonic-gate else {
32780Sstevel@tonic-gate len++;
32790Sstevel@tonic-gate REGC((char)ender, s++);
32800Sstevel@tonic-gate }
32810Sstevel@tonic-gate break;
32820Sstevel@tonic-gate }
32830Sstevel@tonic-gate if (UTF) {
32840Sstevel@tonic-gate STRLEN unilen;
32850Sstevel@tonic-gate
32860Sstevel@tonic-gate if (FOLD) {
32870Sstevel@tonic-gate /* Emit all the Unicode characters. */
32880Sstevel@tonic-gate for (foldbuf = tmpbuf;
32890Sstevel@tonic-gate foldlen;
32900Sstevel@tonic-gate foldlen -= numlen) {
32910Sstevel@tonic-gate ender = utf8_to_uvchr(foldbuf, &numlen);
32920Sstevel@tonic-gate if (numlen > 0) {
32930Sstevel@tonic-gate reguni(pRExC_state, ender, s, &unilen);
32940Sstevel@tonic-gate len += unilen;
32950Sstevel@tonic-gate s += unilen;
32960Sstevel@tonic-gate /* In EBCDIC the numlen
32970Sstevel@tonic-gate * and unilen can differ. */
32980Sstevel@tonic-gate foldbuf += numlen;
32990Sstevel@tonic-gate if (numlen >= foldlen)
33000Sstevel@tonic-gate break;
33010Sstevel@tonic-gate }
33020Sstevel@tonic-gate else
33030Sstevel@tonic-gate break;
33040Sstevel@tonic-gate }
33050Sstevel@tonic-gate }
33060Sstevel@tonic-gate else {
33070Sstevel@tonic-gate reguni(pRExC_state, ender, s, &unilen);
33080Sstevel@tonic-gate if (unilen > 0) {
33090Sstevel@tonic-gate s += unilen;
33100Sstevel@tonic-gate len += unilen;
33110Sstevel@tonic-gate }
33120Sstevel@tonic-gate }
33130Sstevel@tonic-gate len--;
33140Sstevel@tonic-gate }
33150Sstevel@tonic-gate else
33160Sstevel@tonic-gate REGC((char)ender, s++);
33170Sstevel@tonic-gate }
33180Sstevel@tonic-gate loopdone:
33190Sstevel@tonic-gate RExC_parse = p - 1;
33200Sstevel@tonic-gate Set_Node_Cur_Length(ret); /* MJD */
33210Sstevel@tonic-gate nextchar(pRExC_state);
33220Sstevel@tonic-gate {
33230Sstevel@tonic-gate /* len is STRLEN which is unsigned, need to copy to signed */
33240Sstevel@tonic-gate IV iv = len;
33250Sstevel@tonic-gate if (iv < 0)
33260Sstevel@tonic-gate vFAIL("Internal disaster");
33270Sstevel@tonic-gate }
33280Sstevel@tonic-gate if (len > 0)
33290Sstevel@tonic-gate *flagp |= HASWIDTH;
33300Sstevel@tonic-gate if (len == 1 && UNI_IS_INVARIANT(ender))
33310Sstevel@tonic-gate *flagp |= SIMPLE;
33320Sstevel@tonic-gate if (!SIZE_ONLY)
33330Sstevel@tonic-gate STR_LEN(ret) = len;
33340Sstevel@tonic-gate if (SIZE_ONLY)
33350Sstevel@tonic-gate RExC_size += STR_SZ(len);
33360Sstevel@tonic-gate else
33370Sstevel@tonic-gate RExC_emit += STR_SZ(len);
33380Sstevel@tonic-gate }
33390Sstevel@tonic-gate break;
33400Sstevel@tonic-gate }
33410Sstevel@tonic-gate
33420Sstevel@tonic-gate /* If the encoding pragma is in effect recode the text of
33430Sstevel@tonic-gate * any EXACT-kind nodes. */
33440Sstevel@tonic-gate if (PL_encoding && PL_regkind[(U8)OP(ret)] == EXACT) {
33450Sstevel@tonic-gate STRLEN oldlen = STR_LEN(ret);
33460Sstevel@tonic-gate SV *sv = sv_2mortal(newSVpvn(STRING(ret), oldlen));
33470Sstevel@tonic-gate
33480Sstevel@tonic-gate if (RExC_utf8)
33490Sstevel@tonic-gate SvUTF8_on(sv);
33500Sstevel@tonic-gate if (sv_utf8_downgrade(sv, TRUE)) {
33510Sstevel@tonic-gate char *s = sv_recode_to_utf8(sv, PL_encoding);
33520Sstevel@tonic-gate STRLEN newlen = SvCUR(sv);
33530Sstevel@tonic-gate
33540Sstevel@tonic-gate if (SvUTF8(sv))
33550Sstevel@tonic-gate RExC_utf8 = 1;
33560Sstevel@tonic-gate if (!SIZE_ONLY) {
33570Sstevel@tonic-gate DEBUG_r(PerlIO_printf(Perl_debug_log, "recode %*s to %*s\n",
33580Sstevel@tonic-gate (int)oldlen, STRING(ret),
33590Sstevel@tonic-gate (int)newlen, s));
33600Sstevel@tonic-gate Copy(s, STRING(ret), newlen, char);
33610Sstevel@tonic-gate STR_LEN(ret) += newlen - oldlen;
33620Sstevel@tonic-gate RExC_emit += STR_SZ(newlen) - STR_SZ(oldlen);
33630Sstevel@tonic-gate } else
33640Sstevel@tonic-gate RExC_size += STR_SZ(newlen) - STR_SZ(oldlen);
33650Sstevel@tonic-gate }
33660Sstevel@tonic-gate }
33670Sstevel@tonic-gate
33680Sstevel@tonic-gate return(ret);
33690Sstevel@tonic-gate }
33700Sstevel@tonic-gate
33710Sstevel@tonic-gate STATIC char *
S_regwhite(pTHX_ char * p,char * e)33720Sstevel@tonic-gate S_regwhite(pTHX_ char *p, char *e)
33730Sstevel@tonic-gate {
33740Sstevel@tonic-gate while (p < e) {
33750Sstevel@tonic-gate if (isSPACE(*p))
33760Sstevel@tonic-gate ++p;
33770Sstevel@tonic-gate else if (*p == '#') {
33780Sstevel@tonic-gate do {
33790Sstevel@tonic-gate p++;
33800Sstevel@tonic-gate } while (p < e && *p != '\n');
33810Sstevel@tonic-gate }
33820Sstevel@tonic-gate else
33830Sstevel@tonic-gate break;
33840Sstevel@tonic-gate }
33850Sstevel@tonic-gate return p;
33860Sstevel@tonic-gate }
33870Sstevel@tonic-gate
33880Sstevel@tonic-gate /* Parse POSIX character classes: [[:foo:]], [[=foo=]], [[.foo.]].
33890Sstevel@tonic-gate Character classes ([:foo:]) can also be negated ([:^foo:]).
33900Sstevel@tonic-gate Returns a named class id (ANYOF_XXX) if successful, -1 otherwise.
33910Sstevel@tonic-gate Equivalence classes ([=foo=]) and composites ([.foo.]) are parsed,
33920Sstevel@tonic-gate but trigger failures because they are currently unimplemented. */
33930Sstevel@tonic-gate
33940Sstevel@tonic-gate #define POSIXCC_DONE(c) ((c) == ':')
33950Sstevel@tonic-gate #define POSIXCC_NOTYET(c) ((c) == '=' || (c) == '.')
33960Sstevel@tonic-gate #define POSIXCC(c) (POSIXCC_DONE(c) || POSIXCC_NOTYET(c))
33970Sstevel@tonic-gate
33980Sstevel@tonic-gate STATIC I32
S_regpposixcc(pTHX_ RExC_state_t * pRExC_state,I32 value)33990Sstevel@tonic-gate S_regpposixcc(pTHX_ RExC_state_t *pRExC_state, I32 value)
34000Sstevel@tonic-gate {
34010Sstevel@tonic-gate char *posixcc = 0;
34020Sstevel@tonic-gate I32 namedclass = OOB_NAMEDCLASS;
34030Sstevel@tonic-gate
34040Sstevel@tonic-gate if (value == '[' && RExC_parse + 1 < RExC_end &&
34050Sstevel@tonic-gate /* I smell either [: or [= or [. -- POSIX has been here, right? */
34060Sstevel@tonic-gate POSIXCC(UCHARAT(RExC_parse))) {
34070Sstevel@tonic-gate char c = UCHARAT(RExC_parse);
34080Sstevel@tonic-gate char* s = RExC_parse++;
34090Sstevel@tonic-gate
34100Sstevel@tonic-gate while (RExC_parse < RExC_end && UCHARAT(RExC_parse) != c)
34110Sstevel@tonic-gate RExC_parse++;
34120Sstevel@tonic-gate if (RExC_parse == RExC_end)
34130Sstevel@tonic-gate /* Grandfather lone [:, [=, [. */
34140Sstevel@tonic-gate RExC_parse = s;
34150Sstevel@tonic-gate else {
34160Sstevel@tonic-gate char* t = RExC_parse++; /* skip over the c */
34170Sstevel@tonic-gate
34180Sstevel@tonic-gate if (UCHARAT(RExC_parse) == ']') {
34190Sstevel@tonic-gate RExC_parse++; /* skip over the ending ] */
34200Sstevel@tonic-gate posixcc = s + 1;
34210Sstevel@tonic-gate if (*s == ':') {
34220Sstevel@tonic-gate I32 complement = *posixcc == '^' ? *posixcc++ : 0;
34230Sstevel@tonic-gate I32 skip = 5; /* the most common skip */
34240Sstevel@tonic-gate
34250Sstevel@tonic-gate switch (*posixcc) {
34260Sstevel@tonic-gate case 'a':
34270Sstevel@tonic-gate if (strnEQ(posixcc, "alnum", 5))
34280Sstevel@tonic-gate namedclass =
34290Sstevel@tonic-gate complement ? ANYOF_NALNUMC : ANYOF_ALNUMC;
34300Sstevel@tonic-gate else if (strnEQ(posixcc, "alpha", 5))
34310Sstevel@tonic-gate namedclass =
34320Sstevel@tonic-gate complement ? ANYOF_NALPHA : ANYOF_ALPHA;
34330Sstevel@tonic-gate else if (strnEQ(posixcc, "ascii", 5))
34340Sstevel@tonic-gate namedclass =
34350Sstevel@tonic-gate complement ? ANYOF_NASCII : ANYOF_ASCII;
34360Sstevel@tonic-gate break;
34370Sstevel@tonic-gate case 'b':
34380Sstevel@tonic-gate if (strnEQ(posixcc, "blank", 5))
34390Sstevel@tonic-gate namedclass =
34400Sstevel@tonic-gate complement ? ANYOF_NBLANK : ANYOF_BLANK;
34410Sstevel@tonic-gate break;
34420Sstevel@tonic-gate case 'c':
34430Sstevel@tonic-gate if (strnEQ(posixcc, "cntrl", 5))
34440Sstevel@tonic-gate namedclass =
34450Sstevel@tonic-gate complement ? ANYOF_NCNTRL : ANYOF_CNTRL;
34460Sstevel@tonic-gate break;
34470Sstevel@tonic-gate case 'd':
34480Sstevel@tonic-gate if (strnEQ(posixcc, "digit", 5))
34490Sstevel@tonic-gate namedclass =
34500Sstevel@tonic-gate complement ? ANYOF_NDIGIT : ANYOF_DIGIT;
34510Sstevel@tonic-gate break;
34520Sstevel@tonic-gate case 'g':
34530Sstevel@tonic-gate if (strnEQ(posixcc, "graph", 5))
34540Sstevel@tonic-gate namedclass =
34550Sstevel@tonic-gate complement ? ANYOF_NGRAPH : ANYOF_GRAPH;
34560Sstevel@tonic-gate break;
34570Sstevel@tonic-gate case 'l':
34580Sstevel@tonic-gate if (strnEQ(posixcc, "lower", 5))
34590Sstevel@tonic-gate namedclass =
34600Sstevel@tonic-gate complement ? ANYOF_NLOWER : ANYOF_LOWER;
34610Sstevel@tonic-gate break;
34620Sstevel@tonic-gate case 'p':
34630Sstevel@tonic-gate if (strnEQ(posixcc, "print", 5))
34640Sstevel@tonic-gate namedclass =
34650Sstevel@tonic-gate complement ? ANYOF_NPRINT : ANYOF_PRINT;
34660Sstevel@tonic-gate else if (strnEQ(posixcc, "punct", 5))
34670Sstevel@tonic-gate namedclass =
34680Sstevel@tonic-gate complement ? ANYOF_NPUNCT : ANYOF_PUNCT;
34690Sstevel@tonic-gate break;
34700Sstevel@tonic-gate case 's':
34710Sstevel@tonic-gate if (strnEQ(posixcc, "space", 5))
34720Sstevel@tonic-gate namedclass =
34730Sstevel@tonic-gate complement ? ANYOF_NPSXSPC : ANYOF_PSXSPC;
34740Sstevel@tonic-gate break;
34750Sstevel@tonic-gate case 'u':
34760Sstevel@tonic-gate if (strnEQ(posixcc, "upper", 5))
34770Sstevel@tonic-gate namedclass =
34780Sstevel@tonic-gate complement ? ANYOF_NUPPER : ANYOF_UPPER;
34790Sstevel@tonic-gate break;
34800Sstevel@tonic-gate case 'w': /* this is not POSIX, this is the Perl \w */
34810Sstevel@tonic-gate if (strnEQ(posixcc, "word", 4)) {
34820Sstevel@tonic-gate namedclass =
34830Sstevel@tonic-gate complement ? ANYOF_NALNUM : ANYOF_ALNUM;
34840Sstevel@tonic-gate skip = 4;
34850Sstevel@tonic-gate }
34860Sstevel@tonic-gate break;
34870Sstevel@tonic-gate case 'x':
34880Sstevel@tonic-gate if (strnEQ(posixcc, "xdigit", 6)) {
34890Sstevel@tonic-gate namedclass =
34900Sstevel@tonic-gate complement ? ANYOF_NXDIGIT : ANYOF_XDIGIT;
34910Sstevel@tonic-gate skip = 6;
34920Sstevel@tonic-gate }
34930Sstevel@tonic-gate break;
34940Sstevel@tonic-gate }
34950Sstevel@tonic-gate if (namedclass == OOB_NAMEDCLASS ||
34960Sstevel@tonic-gate posixcc[skip] != ':' ||
34970Sstevel@tonic-gate posixcc[skip+1] != ']')
34980Sstevel@tonic-gate {
34990Sstevel@tonic-gate Simple_vFAIL3("POSIX class [:%.*s:] unknown",
35000Sstevel@tonic-gate t - s - 1, s + 1);
35010Sstevel@tonic-gate }
35020Sstevel@tonic-gate } else if (!SIZE_ONLY) {
35030Sstevel@tonic-gate /* [[=foo=]] and [[.foo.]] are still future. */
35040Sstevel@tonic-gate
35050Sstevel@tonic-gate /* adjust RExC_parse so the warning shows after
35060Sstevel@tonic-gate the class closes */
35070Sstevel@tonic-gate while (UCHARAT(RExC_parse) && UCHARAT(RExC_parse) != ']')
35080Sstevel@tonic-gate RExC_parse++;
35090Sstevel@tonic-gate Simple_vFAIL3("POSIX syntax [%c %c] is reserved for future extensions", c, c);
35100Sstevel@tonic-gate }
35110Sstevel@tonic-gate } else {
35120Sstevel@tonic-gate /* Maternal grandfather:
35130Sstevel@tonic-gate * "[:" ending in ":" but not in ":]" */
35140Sstevel@tonic-gate RExC_parse = s;
35150Sstevel@tonic-gate }
35160Sstevel@tonic-gate }
35170Sstevel@tonic-gate }
35180Sstevel@tonic-gate
35190Sstevel@tonic-gate return namedclass;
35200Sstevel@tonic-gate }
35210Sstevel@tonic-gate
35220Sstevel@tonic-gate STATIC void
S_checkposixcc(pTHX_ RExC_state_t * pRExC_state)35230Sstevel@tonic-gate S_checkposixcc(pTHX_ RExC_state_t *pRExC_state)
35240Sstevel@tonic-gate {
35250Sstevel@tonic-gate if (!SIZE_ONLY && POSIXCC(UCHARAT(RExC_parse))) {
35260Sstevel@tonic-gate char *s = RExC_parse;
35270Sstevel@tonic-gate char c = *s++;
35280Sstevel@tonic-gate
35290Sstevel@tonic-gate while(*s && isALNUM(*s))
35300Sstevel@tonic-gate s++;
35310Sstevel@tonic-gate if (*s && c == *s && s[1] == ']') {
35320Sstevel@tonic-gate if (ckWARN(WARN_REGEXP))
35330Sstevel@tonic-gate vWARN3(s+2,
35340Sstevel@tonic-gate "POSIX syntax [%c %c] belongs inside character classes",
35350Sstevel@tonic-gate c, c);
35360Sstevel@tonic-gate
35370Sstevel@tonic-gate /* [[=foo=]] and [[.foo.]] are still future. */
35380Sstevel@tonic-gate if (POSIXCC_NOTYET(c)) {
35390Sstevel@tonic-gate /* adjust RExC_parse so the error shows after
35400Sstevel@tonic-gate the class closes */
35410Sstevel@tonic-gate while (UCHARAT(RExC_parse) && UCHARAT(RExC_parse++) != ']')
35420Sstevel@tonic-gate ;
35430Sstevel@tonic-gate Simple_vFAIL3("POSIX syntax [%c %c] is reserved for future extensions", c, c);
35440Sstevel@tonic-gate }
35450Sstevel@tonic-gate }
35460Sstevel@tonic-gate }
35470Sstevel@tonic-gate }
35480Sstevel@tonic-gate
35490Sstevel@tonic-gate STATIC regnode *
S_regclass(pTHX_ RExC_state_t * pRExC_state)35500Sstevel@tonic-gate S_regclass(pTHX_ RExC_state_t *pRExC_state)
35510Sstevel@tonic-gate {
35520Sstevel@tonic-gate register UV value;
35530Sstevel@tonic-gate register UV nextvalue;
35540Sstevel@tonic-gate register IV prevvalue = OOB_UNICODE;
35550Sstevel@tonic-gate register IV range = 0;
35560Sstevel@tonic-gate register regnode *ret;
35570Sstevel@tonic-gate STRLEN numlen;
35580Sstevel@tonic-gate IV namedclass;
35590Sstevel@tonic-gate char *rangebegin = 0;
35600Sstevel@tonic-gate bool need_class = 0;
35610Sstevel@tonic-gate SV *listsv = Nullsv;
35620Sstevel@tonic-gate register char *e;
35630Sstevel@tonic-gate UV n;
35640Sstevel@tonic-gate bool optimize_invert = TRUE;
35650Sstevel@tonic-gate AV* unicode_alternate = 0;
35660Sstevel@tonic-gate #ifdef EBCDIC
35670Sstevel@tonic-gate UV literal_endpoint = 0;
35680Sstevel@tonic-gate #endif
35690Sstevel@tonic-gate
35700Sstevel@tonic-gate ret = reganode(pRExC_state, ANYOF, 0);
35710Sstevel@tonic-gate
35720Sstevel@tonic-gate if (!SIZE_ONLY)
35730Sstevel@tonic-gate ANYOF_FLAGS(ret) = 0;
35740Sstevel@tonic-gate
35750Sstevel@tonic-gate if (UCHARAT(RExC_parse) == '^') { /* Complement of range. */
35760Sstevel@tonic-gate RExC_naughty++;
35770Sstevel@tonic-gate RExC_parse++;
35780Sstevel@tonic-gate if (!SIZE_ONLY)
35790Sstevel@tonic-gate ANYOF_FLAGS(ret) |= ANYOF_INVERT;
35800Sstevel@tonic-gate }
35810Sstevel@tonic-gate
35820Sstevel@tonic-gate if (SIZE_ONLY)
35830Sstevel@tonic-gate RExC_size += ANYOF_SKIP;
35840Sstevel@tonic-gate else {
35850Sstevel@tonic-gate RExC_emit += ANYOF_SKIP;
35860Sstevel@tonic-gate if (FOLD)
35870Sstevel@tonic-gate ANYOF_FLAGS(ret) |= ANYOF_FOLD;
35880Sstevel@tonic-gate if (LOC)
35890Sstevel@tonic-gate ANYOF_FLAGS(ret) |= ANYOF_LOCALE;
35900Sstevel@tonic-gate ANYOF_BITMAP_ZERO(ret);
35910Sstevel@tonic-gate listsv = newSVpvn("# comment\n", 10);
35920Sstevel@tonic-gate }
35930Sstevel@tonic-gate
35940Sstevel@tonic-gate nextvalue = RExC_parse < RExC_end ? UCHARAT(RExC_parse) : 0;
35950Sstevel@tonic-gate
35960Sstevel@tonic-gate if (!SIZE_ONLY && POSIXCC(nextvalue))
35970Sstevel@tonic-gate checkposixcc(pRExC_state);
35980Sstevel@tonic-gate
35990Sstevel@tonic-gate /* allow 1st char to be ] (allowing it to be - is dealt with later) */
36000Sstevel@tonic-gate if (UCHARAT(RExC_parse) == ']')
36010Sstevel@tonic-gate goto charclassloop;
36020Sstevel@tonic-gate
36030Sstevel@tonic-gate while (RExC_parse < RExC_end && UCHARAT(RExC_parse) != ']') {
36040Sstevel@tonic-gate
36050Sstevel@tonic-gate charclassloop:
36060Sstevel@tonic-gate
36070Sstevel@tonic-gate namedclass = OOB_NAMEDCLASS; /* initialize as illegal */
36080Sstevel@tonic-gate
36090Sstevel@tonic-gate if (!range)
36100Sstevel@tonic-gate rangebegin = RExC_parse;
36110Sstevel@tonic-gate if (UTF) {
36120Sstevel@tonic-gate value = utf8n_to_uvchr((U8*)RExC_parse,
36130Sstevel@tonic-gate RExC_end - RExC_parse,
36140Sstevel@tonic-gate &numlen, 0);
36150Sstevel@tonic-gate RExC_parse += numlen;
36160Sstevel@tonic-gate }
36170Sstevel@tonic-gate else
36180Sstevel@tonic-gate value = UCHARAT(RExC_parse++);
36190Sstevel@tonic-gate nextvalue = RExC_parse < RExC_end ? UCHARAT(RExC_parse) : 0;
36200Sstevel@tonic-gate if (value == '[' && POSIXCC(nextvalue))
36210Sstevel@tonic-gate namedclass = regpposixcc(pRExC_state, value);
36220Sstevel@tonic-gate else if (value == '\\') {
36230Sstevel@tonic-gate if (UTF) {
36240Sstevel@tonic-gate value = utf8n_to_uvchr((U8*)RExC_parse,
36250Sstevel@tonic-gate RExC_end - RExC_parse,
36260Sstevel@tonic-gate &numlen, 0);
36270Sstevel@tonic-gate RExC_parse += numlen;
36280Sstevel@tonic-gate }
36290Sstevel@tonic-gate else
36300Sstevel@tonic-gate value = UCHARAT(RExC_parse++);
36310Sstevel@tonic-gate /* Some compilers cannot handle switching on 64-bit integer
36320Sstevel@tonic-gate * values, therefore value cannot be an UV. Yes, this will
36330Sstevel@tonic-gate * be a problem later if we want switch on Unicode.
36340Sstevel@tonic-gate * A similar issue a little bit later when switching on
36350Sstevel@tonic-gate * namedclass. --jhi */
36360Sstevel@tonic-gate switch ((I32)value) {
36370Sstevel@tonic-gate case 'w': namedclass = ANYOF_ALNUM; break;
36380Sstevel@tonic-gate case 'W': namedclass = ANYOF_NALNUM; break;
36390Sstevel@tonic-gate case 's': namedclass = ANYOF_SPACE; break;
36400Sstevel@tonic-gate case 'S': namedclass = ANYOF_NSPACE; break;
36410Sstevel@tonic-gate case 'd': namedclass = ANYOF_DIGIT; break;
36420Sstevel@tonic-gate case 'D': namedclass = ANYOF_NDIGIT; break;
36430Sstevel@tonic-gate case 'p':
36440Sstevel@tonic-gate case 'P':
36450Sstevel@tonic-gate if (RExC_parse >= RExC_end)
36460Sstevel@tonic-gate vFAIL2("Empty \\%c{}", (U8)value);
36470Sstevel@tonic-gate if (*RExC_parse == '{') {
36480Sstevel@tonic-gate U8 c = (U8)value;
36490Sstevel@tonic-gate e = strchr(RExC_parse++, '}');
36500Sstevel@tonic-gate if (!e)
36510Sstevel@tonic-gate vFAIL2("Missing right brace on \\%c{}", c);
36520Sstevel@tonic-gate while (isSPACE(UCHARAT(RExC_parse)))
36530Sstevel@tonic-gate RExC_parse++;
36540Sstevel@tonic-gate if (e == RExC_parse)
36550Sstevel@tonic-gate vFAIL2("Empty \\%c{}", c);
36560Sstevel@tonic-gate n = e - RExC_parse;
36570Sstevel@tonic-gate while (isSPACE(UCHARAT(RExC_parse + n - 1)))
36580Sstevel@tonic-gate n--;
36590Sstevel@tonic-gate }
36600Sstevel@tonic-gate else {
36610Sstevel@tonic-gate e = RExC_parse;
36620Sstevel@tonic-gate n = 1;
36630Sstevel@tonic-gate }
36640Sstevel@tonic-gate if (!SIZE_ONLY) {
36650Sstevel@tonic-gate if (UCHARAT(RExC_parse) == '^') {
36660Sstevel@tonic-gate RExC_parse++;
36670Sstevel@tonic-gate n--;
36680Sstevel@tonic-gate value = value == 'p' ? 'P' : 'p'; /* toggle */
36690Sstevel@tonic-gate while (isSPACE(UCHARAT(RExC_parse))) {
36700Sstevel@tonic-gate RExC_parse++;
36710Sstevel@tonic-gate n--;
36720Sstevel@tonic-gate }
36730Sstevel@tonic-gate }
36740Sstevel@tonic-gate if (value == 'p')
36750Sstevel@tonic-gate Perl_sv_catpvf(aTHX_ listsv,
36760Sstevel@tonic-gate "+utf8::%.*s\n", (int)n, RExC_parse);
36770Sstevel@tonic-gate else
36780Sstevel@tonic-gate Perl_sv_catpvf(aTHX_ listsv,
36790Sstevel@tonic-gate "!utf8::%.*s\n", (int)n, RExC_parse);
36800Sstevel@tonic-gate }
36810Sstevel@tonic-gate RExC_parse = e + 1;
36820Sstevel@tonic-gate ANYOF_FLAGS(ret) |= ANYOF_UNICODE;
36830Sstevel@tonic-gate continue;
36840Sstevel@tonic-gate case 'n': value = '\n'; break;
36850Sstevel@tonic-gate case 'r': value = '\r'; break;
36860Sstevel@tonic-gate case 't': value = '\t'; break;
36870Sstevel@tonic-gate case 'f': value = '\f'; break;
36880Sstevel@tonic-gate case 'b': value = '\b'; break;
36890Sstevel@tonic-gate case 'e': value = ASCII_TO_NATIVE('\033');break;
36900Sstevel@tonic-gate case 'a': value = ASCII_TO_NATIVE('\007');break;
36910Sstevel@tonic-gate case 'x':
36920Sstevel@tonic-gate if (*RExC_parse == '{') {
36930Sstevel@tonic-gate I32 flags = PERL_SCAN_ALLOW_UNDERSCORES
36940Sstevel@tonic-gate | PERL_SCAN_DISALLOW_PREFIX;
36950Sstevel@tonic-gate e = strchr(RExC_parse++, '}');
36960Sstevel@tonic-gate if (!e)
36970Sstevel@tonic-gate vFAIL("Missing right brace on \\x{}");
36980Sstevel@tonic-gate
36990Sstevel@tonic-gate numlen = e - RExC_parse;
37000Sstevel@tonic-gate value = grok_hex(RExC_parse, &numlen, &flags, NULL);
37010Sstevel@tonic-gate RExC_parse = e + 1;
37020Sstevel@tonic-gate }
37030Sstevel@tonic-gate else {
37040Sstevel@tonic-gate I32 flags = PERL_SCAN_DISALLOW_PREFIX;
37050Sstevel@tonic-gate numlen = 2;
37060Sstevel@tonic-gate value = grok_hex(RExC_parse, &numlen, &flags, NULL);
37070Sstevel@tonic-gate RExC_parse += numlen;
37080Sstevel@tonic-gate }
37090Sstevel@tonic-gate break;
37100Sstevel@tonic-gate case 'c':
37110Sstevel@tonic-gate value = UCHARAT(RExC_parse++);
37120Sstevel@tonic-gate value = toCTRL(value);
37130Sstevel@tonic-gate break;
37140Sstevel@tonic-gate case '0': case '1': case '2': case '3': case '4':
37150Sstevel@tonic-gate case '5': case '6': case '7': case '8': case '9':
37160Sstevel@tonic-gate {
37170Sstevel@tonic-gate I32 flags = 0;
37180Sstevel@tonic-gate numlen = 3;
37190Sstevel@tonic-gate value = grok_oct(--RExC_parse, &numlen, &flags, NULL);
37200Sstevel@tonic-gate RExC_parse += numlen;
37210Sstevel@tonic-gate break;
37220Sstevel@tonic-gate }
37230Sstevel@tonic-gate default:
37240Sstevel@tonic-gate if (!SIZE_ONLY && ckWARN(WARN_REGEXP) && isALPHA(value))
37250Sstevel@tonic-gate vWARN2(RExC_parse,
37260Sstevel@tonic-gate "Unrecognized escape \\%c in character class passed through",
37270Sstevel@tonic-gate (int)value);
37280Sstevel@tonic-gate break;
37290Sstevel@tonic-gate }
37300Sstevel@tonic-gate } /* end of \blah */
37310Sstevel@tonic-gate #ifdef EBCDIC
37320Sstevel@tonic-gate else
37330Sstevel@tonic-gate literal_endpoint++;
37340Sstevel@tonic-gate #endif
37350Sstevel@tonic-gate
37360Sstevel@tonic-gate if (namedclass > OOB_NAMEDCLASS) { /* this is a named class \blah */
37370Sstevel@tonic-gate
37380Sstevel@tonic-gate if (!SIZE_ONLY && !need_class)
37390Sstevel@tonic-gate ANYOF_CLASS_ZERO(ret);
37400Sstevel@tonic-gate
37410Sstevel@tonic-gate need_class = 1;
37420Sstevel@tonic-gate
37430Sstevel@tonic-gate /* a bad range like a-\d, a-[:digit:] ? */
37440Sstevel@tonic-gate if (range) {
37450Sstevel@tonic-gate if (!SIZE_ONLY) {
37460Sstevel@tonic-gate if (ckWARN(WARN_REGEXP))
37470Sstevel@tonic-gate vWARN4(RExC_parse,
37480Sstevel@tonic-gate "False [] range \"%*.*s\"",
37490Sstevel@tonic-gate RExC_parse - rangebegin,
37500Sstevel@tonic-gate RExC_parse - rangebegin,
37510Sstevel@tonic-gate rangebegin);
37520Sstevel@tonic-gate if (prevvalue < 256) {
37530Sstevel@tonic-gate ANYOF_BITMAP_SET(ret, prevvalue);
37540Sstevel@tonic-gate ANYOF_BITMAP_SET(ret, '-');
37550Sstevel@tonic-gate }
37560Sstevel@tonic-gate else {
37570Sstevel@tonic-gate ANYOF_FLAGS(ret) |= ANYOF_UNICODE;
37580Sstevel@tonic-gate Perl_sv_catpvf(aTHX_ listsv,
37590Sstevel@tonic-gate "%04"UVxf"\n%04"UVxf"\n", (UV)prevvalue, (UV) '-');
37600Sstevel@tonic-gate }
37610Sstevel@tonic-gate }
37620Sstevel@tonic-gate
37630Sstevel@tonic-gate range = 0; /* this was not a true range */
37640Sstevel@tonic-gate }
37650Sstevel@tonic-gate
37660Sstevel@tonic-gate if (!SIZE_ONLY) {
37670Sstevel@tonic-gate if (namedclass > OOB_NAMEDCLASS)
37680Sstevel@tonic-gate optimize_invert = FALSE;
37690Sstevel@tonic-gate /* Possible truncation here but in some 64-bit environments
37700Sstevel@tonic-gate * the compiler gets heartburn about switch on 64-bit values.
37710Sstevel@tonic-gate * A similar issue a little earlier when switching on value.
37720Sstevel@tonic-gate * --jhi */
37730Sstevel@tonic-gate switch ((I32)namedclass) {
37740Sstevel@tonic-gate case ANYOF_ALNUM:
37750Sstevel@tonic-gate if (LOC)
37760Sstevel@tonic-gate ANYOF_CLASS_SET(ret, ANYOF_ALNUM);
37770Sstevel@tonic-gate else {
37780Sstevel@tonic-gate for (value = 0; value < 256; value++)
37790Sstevel@tonic-gate if (isALNUM(value))
37800Sstevel@tonic-gate ANYOF_BITMAP_SET(ret, value);
37810Sstevel@tonic-gate }
37820Sstevel@tonic-gate Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsWord\n");
37830Sstevel@tonic-gate break;
37840Sstevel@tonic-gate case ANYOF_NALNUM:
37850Sstevel@tonic-gate if (LOC)
37860Sstevel@tonic-gate ANYOF_CLASS_SET(ret, ANYOF_NALNUM);
37870Sstevel@tonic-gate else {
37880Sstevel@tonic-gate for (value = 0; value < 256; value++)
37890Sstevel@tonic-gate if (!isALNUM(value))
37900Sstevel@tonic-gate ANYOF_BITMAP_SET(ret, value);
37910Sstevel@tonic-gate }
37920Sstevel@tonic-gate Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsWord\n");
37930Sstevel@tonic-gate break;
37940Sstevel@tonic-gate case ANYOF_ALNUMC:
37950Sstevel@tonic-gate if (LOC)
37960Sstevel@tonic-gate ANYOF_CLASS_SET(ret, ANYOF_ALNUMC);
37970Sstevel@tonic-gate else {
37980Sstevel@tonic-gate for (value = 0; value < 256; value++)
37990Sstevel@tonic-gate if (isALNUMC(value))
38000Sstevel@tonic-gate ANYOF_BITMAP_SET(ret, value);
38010Sstevel@tonic-gate }
38020Sstevel@tonic-gate Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsAlnum\n");
38030Sstevel@tonic-gate break;
38040Sstevel@tonic-gate case ANYOF_NALNUMC:
38050Sstevel@tonic-gate if (LOC)
38060Sstevel@tonic-gate ANYOF_CLASS_SET(ret, ANYOF_NALNUMC);
38070Sstevel@tonic-gate else {
38080Sstevel@tonic-gate for (value = 0; value < 256; value++)
38090Sstevel@tonic-gate if (!isALNUMC(value))
38100Sstevel@tonic-gate ANYOF_BITMAP_SET(ret, value);
38110Sstevel@tonic-gate }
38120Sstevel@tonic-gate Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsAlnum\n");
38130Sstevel@tonic-gate break;
38140Sstevel@tonic-gate case ANYOF_ALPHA:
38150Sstevel@tonic-gate if (LOC)
38160Sstevel@tonic-gate ANYOF_CLASS_SET(ret, ANYOF_ALPHA);
38170Sstevel@tonic-gate else {
38180Sstevel@tonic-gate for (value = 0; value < 256; value++)
38190Sstevel@tonic-gate if (isALPHA(value))
38200Sstevel@tonic-gate ANYOF_BITMAP_SET(ret, value);
38210Sstevel@tonic-gate }
38220Sstevel@tonic-gate Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsAlpha\n");
38230Sstevel@tonic-gate break;
38240Sstevel@tonic-gate case ANYOF_NALPHA:
38250Sstevel@tonic-gate if (LOC)
38260Sstevel@tonic-gate ANYOF_CLASS_SET(ret, ANYOF_NALPHA);
38270Sstevel@tonic-gate else {
38280Sstevel@tonic-gate for (value = 0; value < 256; value++)
38290Sstevel@tonic-gate if (!isALPHA(value))
38300Sstevel@tonic-gate ANYOF_BITMAP_SET(ret, value);
38310Sstevel@tonic-gate }
38320Sstevel@tonic-gate Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsAlpha\n");
38330Sstevel@tonic-gate break;
38340Sstevel@tonic-gate case ANYOF_ASCII:
38350Sstevel@tonic-gate if (LOC)
38360Sstevel@tonic-gate ANYOF_CLASS_SET(ret, ANYOF_ASCII);
38370Sstevel@tonic-gate else {
38380Sstevel@tonic-gate #ifndef EBCDIC
38390Sstevel@tonic-gate for (value = 0; value < 128; value++)
38400Sstevel@tonic-gate ANYOF_BITMAP_SET(ret, value);
38410Sstevel@tonic-gate #else /* EBCDIC */
38420Sstevel@tonic-gate for (value = 0; value < 256; value++) {
38430Sstevel@tonic-gate if (isASCII(value))
38440Sstevel@tonic-gate ANYOF_BITMAP_SET(ret, value);
38450Sstevel@tonic-gate }
38460Sstevel@tonic-gate #endif /* EBCDIC */
38470Sstevel@tonic-gate }
38480Sstevel@tonic-gate Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsASCII\n");
38490Sstevel@tonic-gate break;
38500Sstevel@tonic-gate case ANYOF_NASCII:
38510Sstevel@tonic-gate if (LOC)
38520Sstevel@tonic-gate ANYOF_CLASS_SET(ret, ANYOF_NASCII);
38530Sstevel@tonic-gate else {
38540Sstevel@tonic-gate #ifndef EBCDIC
38550Sstevel@tonic-gate for (value = 128; value < 256; value++)
38560Sstevel@tonic-gate ANYOF_BITMAP_SET(ret, value);
38570Sstevel@tonic-gate #else /* EBCDIC */
38580Sstevel@tonic-gate for (value = 0; value < 256; value++) {
38590Sstevel@tonic-gate if (!isASCII(value))
38600Sstevel@tonic-gate ANYOF_BITMAP_SET(ret, value);
38610Sstevel@tonic-gate }
38620Sstevel@tonic-gate #endif /* EBCDIC */
38630Sstevel@tonic-gate }
38640Sstevel@tonic-gate Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsASCII\n");
38650Sstevel@tonic-gate break;
38660Sstevel@tonic-gate case ANYOF_BLANK:
38670Sstevel@tonic-gate if (LOC)
38680Sstevel@tonic-gate ANYOF_CLASS_SET(ret, ANYOF_BLANK);
38690Sstevel@tonic-gate else {
38700Sstevel@tonic-gate for (value = 0; value < 256; value++)
38710Sstevel@tonic-gate if (isBLANK(value))
38720Sstevel@tonic-gate ANYOF_BITMAP_SET(ret, value);
38730Sstevel@tonic-gate }
38740Sstevel@tonic-gate Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsBlank\n");
38750Sstevel@tonic-gate break;
38760Sstevel@tonic-gate case ANYOF_NBLANK:
38770Sstevel@tonic-gate if (LOC)
38780Sstevel@tonic-gate ANYOF_CLASS_SET(ret, ANYOF_NBLANK);
38790Sstevel@tonic-gate else {
38800Sstevel@tonic-gate for (value = 0; value < 256; value++)
38810Sstevel@tonic-gate if (!isBLANK(value))
38820Sstevel@tonic-gate ANYOF_BITMAP_SET(ret, value);
38830Sstevel@tonic-gate }
38840Sstevel@tonic-gate Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsBlank\n");
38850Sstevel@tonic-gate break;
38860Sstevel@tonic-gate case ANYOF_CNTRL:
38870Sstevel@tonic-gate if (LOC)
38880Sstevel@tonic-gate ANYOF_CLASS_SET(ret, ANYOF_CNTRL);
38890Sstevel@tonic-gate else {
38900Sstevel@tonic-gate for (value = 0; value < 256; value++)
38910Sstevel@tonic-gate if (isCNTRL(value))
38920Sstevel@tonic-gate ANYOF_BITMAP_SET(ret, value);
38930Sstevel@tonic-gate }
38940Sstevel@tonic-gate Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsCntrl\n");
38950Sstevel@tonic-gate break;
38960Sstevel@tonic-gate case ANYOF_NCNTRL:
38970Sstevel@tonic-gate if (LOC)
38980Sstevel@tonic-gate ANYOF_CLASS_SET(ret, ANYOF_NCNTRL);
38990Sstevel@tonic-gate else {
39000Sstevel@tonic-gate for (value = 0; value < 256; value++)
39010Sstevel@tonic-gate if (!isCNTRL(value))
39020Sstevel@tonic-gate ANYOF_BITMAP_SET(ret, value);
39030Sstevel@tonic-gate }
39040Sstevel@tonic-gate Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsCntrl\n");
39050Sstevel@tonic-gate break;
39060Sstevel@tonic-gate case ANYOF_DIGIT:
39070Sstevel@tonic-gate if (LOC)
39080Sstevel@tonic-gate ANYOF_CLASS_SET(ret, ANYOF_DIGIT);
39090Sstevel@tonic-gate else {
39100Sstevel@tonic-gate /* consecutive digits assumed */
39110Sstevel@tonic-gate for (value = '0'; value <= '9'; value++)
39120Sstevel@tonic-gate ANYOF_BITMAP_SET(ret, value);
39130Sstevel@tonic-gate }
39140Sstevel@tonic-gate Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsDigit\n");
39150Sstevel@tonic-gate break;
39160Sstevel@tonic-gate case ANYOF_NDIGIT:
39170Sstevel@tonic-gate if (LOC)
39180Sstevel@tonic-gate ANYOF_CLASS_SET(ret, ANYOF_NDIGIT);
39190Sstevel@tonic-gate else {
39200Sstevel@tonic-gate /* consecutive digits assumed */
39210Sstevel@tonic-gate for (value = 0; value < '0'; value++)
39220Sstevel@tonic-gate ANYOF_BITMAP_SET(ret, value);
39230Sstevel@tonic-gate for (value = '9' + 1; value < 256; value++)
39240Sstevel@tonic-gate ANYOF_BITMAP_SET(ret, value);
39250Sstevel@tonic-gate }
39260Sstevel@tonic-gate Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsDigit\n");
39270Sstevel@tonic-gate break;
39280Sstevel@tonic-gate case ANYOF_GRAPH:
39290Sstevel@tonic-gate if (LOC)
39300Sstevel@tonic-gate ANYOF_CLASS_SET(ret, ANYOF_GRAPH);
39310Sstevel@tonic-gate else {
39320Sstevel@tonic-gate for (value = 0; value < 256; value++)
39330Sstevel@tonic-gate if (isGRAPH(value))
39340Sstevel@tonic-gate ANYOF_BITMAP_SET(ret, value);
39350Sstevel@tonic-gate }
39360Sstevel@tonic-gate Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsGraph\n");
39370Sstevel@tonic-gate break;
39380Sstevel@tonic-gate case ANYOF_NGRAPH:
39390Sstevel@tonic-gate if (LOC)
39400Sstevel@tonic-gate ANYOF_CLASS_SET(ret, ANYOF_NGRAPH);
39410Sstevel@tonic-gate else {
39420Sstevel@tonic-gate for (value = 0; value < 256; value++)
39430Sstevel@tonic-gate if (!isGRAPH(value))
39440Sstevel@tonic-gate ANYOF_BITMAP_SET(ret, value);
39450Sstevel@tonic-gate }
39460Sstevel@tonic-gate Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsGraph\n");
39470Sstevel@tonic-gate break;
39480Sstevel@tonic-gate case ANYOF_LOWER:
39490Sstevel@tonic-gate if (LOC)
39500Sstevel@tonic-gate ANYOF_CLASS_SET(ret, ANYOF_LOWER);
39510Sstevel@tonic-gate else {
39520Sstevel@tonic-gate for (value = 0; value < 256; value++)
39530Sstevel@tonic-gate if (isLOWER(value))
39540Sstevel@tonic-gate ANYOF_BITMAP_SET(ret, value);
39550Sstevel@tonic-gate }
39560Sstevel@tonic-gate Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsLower\n");
39570Sstevel@tonic-gate break;
39580Sstevel@tonic-gate case ANYOF_NLOWER:
39590Sstevel@tonic-gate if (LOC)
39600Sstevel@tonic-gate ANYOF_CLASS_SET(ret, ANYOF_NLOWER);
39610Sstevel@tonic-gate else {
39620Sstevel@tonic-gate for (value = 0; value < 256; value++)
39630Sstevel@tonic-gate if (!isLOWER(value))
39640Sstevel@tonic-gate ANYOF_BITMAP_SET(ret, value);
39650Sstevel@tonic-gate }
39660Sstevel@tonic-gate Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsLower\n");
39670Sstevel@tonic-gate break;
39680Sstevel@tonic-gate case ANYOF_PRINT:
39690Sstevel@tonic-gate if (LOC)
39700Sstevel@tonic-gate ANYOF_CLASS_SET(ret, ANYOF_PRINT);
39710Sstevel@tonic-gate else {
39720Sstevel@tonic-gate for (value = 0; value < 256; value++)
39730Sstevel@tonic-gate if (isPRINT(value))
39740Sstevel@tonic-gate ANYOF_BITMAP_SET(ret, value);
39750Sstevel@tonic-gate }
39760Sstevel@tonic-gate Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsPrint\n");
39770Sstevel@tonic-gate break;
39780Sstevel@tonic-gate case ANYOF_NPRINT:
39790Sstevel@tonic-gate if (LOC)
39800Sstevel@tonic-gate ANYOF_CLASS_SET(ret, ANYOF_NPRINT);
39810Sstevel@tonic-gate else {
39820Sstevel@tonic-gate for (value = 0; value < 256; value++)
39830Sstevel@tonic-gate if (!isPRINT(value))
39840Sstevel@tonic-gate ANYOF_BITMAP_SET(ret, value);
39850Sstevel@tonic-gate }
39860Sstevel@tonic-gate Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsPrint\n");
39870Sstevel@tonic-gate break;
39880Sstevel@tonic-gate case ANYOF_PSXSPC:
39890Sstevel@tonic-gate if (LOC)
39900Sstevel@tonic-gate ANYOF_CLASS_SET(ret, ANYOF_PSXSPC);
39910Sstevel@tonic-gate else {
39920Sstevel@tonic-gate for (value = 0; value < 256; value++)
39930Sstevel@tonic-gate if (isPSXSPC(value))
39940Sstevel@tonic-gate ANYOF_BITMAP_SET(ret, value);
39950Sstevel@tonic-gate }
39960Sstevel@tonic-gate Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsSpace\n");
39970Sstevel@tonic-gate break;
39980Sstevel@tonic-gate case ANYOF_NPSXSPC:
39990Sstevel@tonic-gate if (LOC)
40000Sstevel@tonic-gate ANYOF_CLASS_SET(ret, ANYOF_NPSXSPC);
40010Sstevel@tonic-gate else {
40020Sstevel@tonic-gate for (value = 0; value < 256; value++)
40030Sstevel@tonic-gate if (!isPSXSPC(value))
40040Sstevel@tonic-gate ANYOF_BITMAP_SET(ret, value);
40050Sstevel@tonic-gate }
40060Sstevel@tonic-gate Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsSpace\n");
40070Sstevel@tonic-gate break;
40080Sstevel@tonic-gate case ANYOF_PUNCT:
40090Sstevel@tonic-gate if (LOC)
40100Sstevel@tonic-gate ANYOF_CLASS_SET(ret, ANYOF_PUNCT);
40110Sstevel@tonic-gate else {
40120Sstevel@tonic-gate for (value = 0; value < 256; value++)
40130Sstevel@tonic-gate if (isPUNCT(value))
40140Sstevel@tonic-gate ANYOF_BITMAP_SET(ret, value);
40150Sstevel@tonic-gate }
40160Sstevel@tonic-gate Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsPunct\n");
40170Sstevel@tonic-gate break;
40180Sstevel@tonic-gate case ANYOF_NPUNCT:
40190Sstevel@tonic-gate if (LOC)
40200Sstevel@tonic-gate ANYOF_CLASS_SET(ret, ANYOF_NPUNCT);
40210Sstevel@tonic-gate else {
40220Sstevel@tonic-gate for (value = 0; value < 256; value++)
40230Sstevel@tonic-gate if (!isPUNCT(value))
40240Sstevel@tonic-gate ANYOF_BITMAP_SET(ret, value);
40250Sstevel@tonic-gate }
40260Sstevel@tonic-gate Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsPunct\n");
40270Sstevel@tonic-gate break;
40280Sstevel@tonic-gate case ANYOF_SPACE:
40290Sstevel@tonic-gate if (LOC)
40300Sstevel@tonic-gate ANYOF_CLASS_SET(ret, ANYOF_SPACE);
40310Sstevel@tonic-gate else {
40320Sstevel@tonic-gate for (value = 0; value < 256; value++)
40330Sstevel@tonic-gate if (isSPACE(value))
40340Sstevel@tonic-gate ANYOF_BITMAP_SET(ret, value);
40350Sstevel@tonic-gate }
40360Sstevel@tonic-gate Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsSpacePerl\n");
40370Sstevel@tonic-gate break;
40380Sstevel@tonic-gate case ANYOF_NSPACE:
40390Sstevel@tonic-gate if (LOC)
40400Sstevel@tonic-gate ANYOF_CLASS_SET(ret, ANYOF_NSPACE);
40410Sstevel@tonic-gate else {
40420Sstevel@tonic-gate for (value = 0; value < 256; value++)
40430Sstevel@tonic-gate if (!isSPACE(value))
40440Sstevel@tonic-gate ANYOF_BITMAP_SET(ret, value);
40450Sstevel@tonic-gate }
40460Sstevel@tonic-gate Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsSpacePerl\n");
40470Sstevel@tonic-gate break;
40480Sstevel@tonic-gate case ANYOF_UPPER:
40490Sstevel@tonic-gate if (LOC)
40500Sstevel@tonic-gate ANYOF_CLASS_SET(ret, ANYOF_UPPER);
40510Sstevel@tonic-gate else {
40520Sstevel@tonic-gate for (value = 0; value < 256; value++)
40530Sstevel@tonic-gate if (isUPPER(value))
40540Sstevel@tonic-gate ANYOF_BITMAP_SET(ret, value);
40550Sstevel@tonic-gate }
40560Sstevel@tonic-gate Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsUpper\n");
40570Sstevel@tonic-gate break;
40580Sstevel@tonic-gate case ANYOF_NUPPER:
40590Sstevel@tonic-gate if (LOC)
40600Sstevel@tonic-gate ANYOF_CLASS_SET(ret, ANYOF_NUPPER);
40610Sstevel@tonic-gate else {
40620Sstevel@tonic-gate for (value = 0; value < 256; value++)
40630Sstevel@tonic-gate if (!isUPPER(value))
40640Sstevel@tonic-gate ANYOF_BITMAP_SET(ret, value);
40650Sstevel@tonic-gate }
40660Sstevel@tonic-gate Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsUpper\n");
40670Sstevel@tonic-gate break;
40680Sstevel@tonic-gate case ANYOF_XDIGIT:
40690Sstevel@tonic-gate if (LOC)
40700Sstevel@tonic-gate ANYOF_CLASS_SET(ret, ANYOF_XDIGIT);
40710Sstevel@tonic-gate else {
40720Sstevel@tonic-gate for (value = 0; value < 256; value++)
40730Sstevel@tonic-gate if (isXDIGIT(value))
40740Sstevel@tonic-gate ANYOF_BITMAP_SET(ret, value);
40750Sstevel@tonic-gate }
40760Sstevel@tonic-gate Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsXDigit\n");
40770Sstevel@tonic-gate break;
40780Sstevel@tonic-gate case ANYOF_NXDIGIT:
40790Sstevel@tonic-gate if (LOC)
40800Sstevel@tonic-gate ANYOF_CLASS_SET(ret, ANYOF_NXDIGIT);
40810Sstevel@tonic-gate else {
40820Sstevel@tonic-gate for (value = 0; value < 256; value++)
40830Sstevel@tonic-gate if (!isXDIGIT(value))
40840Sstevel@tonic-gate ANYOF_BITMAP_SET(ret, value);
40850Sstevel@tonic-gate }
40860Sstevel@tonic-gate Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsXDigit\n");
40870Sstevel@tonic-gate break;
40880Sstevel@tonic-gate default:
40890Sstevel@tonic-gate vFAIL("Invalid [::] class");
40900Sstevel@tonic-gate break;
40910Sstevel@tonic-gate }
40920Sstevel@tonic-gate if (LOC)
40930Sstevel@tonic-gate ANYOF_FLAGS(ret) |= ANYOF_CLASS;
40940Sstevel@tonic-gate continue;
40950Sstevel@tonic-gate }
40960Sstevel@tonic-gate } /* end of namedclass \blah */
40970Sstevel@tonic-gate
40980Sstevel@tonic-gate if (range) {
40990Sstevel@tonic-gate if (prevvalue > (IV)value) /* b-a */ {
41000Sstevel@tonic-gate Simple_vFAIL4("Invalid [] range \"%*.*s\"",
41010Sstevel@tonic-gate RExC_parse - rangebegin,
41020Sstevel@tonic-gate RExC_parse - rangebegin,
41030Sstevel@tonic-gate rangebegin);
41040Sstevel@tonic-gate range = 0; /* not a valid range */
41050Sstevel@tonic-gate }
41060Sstevel@tonic-gate }
41070Sstevel@tonic-gate else {
41080Sstevel@tonic-gate prevvalue = value; /* save the beginning of the range */
41090Sstevel@tonic-gate if (*RExC_parse == '-' && RExC_parse+1 < RExC_end &&
41100Sstevel@tonic-gate RExC_parse[1] != ']') {
41110Sstevel@tonic-gate RExC_parse++;
41120Sstevel@tonic-gate
41130Sstevel@tonic-gate /* a bad range like \w-, [:word:]- ? */
41140Sstevel@tonic-gate if (namedclass > OOB_NAMEDCLASS) {
41150Sstevel@tonic-gate if (ckWARN(WARN_REGEXP))
41160Sstevel@tonic-gate vWARN4(RExC_parse,
41170Sstevel@tonic-gate "False [] range \"%*.*s\"",
41180Sstevel@tonic-gate RExC_parse - rangebegin,
41190Sstevel@tonic-gate RExC_parse - rangebegin,
41200Sstevel@tonic-gate rangebegin);
41210Sstevel@tonic-gate if (!SIZE_ONLY)
41220Sstevel@tonic-gate ANYOF_BITMAP_SET(ret, '-');
41230Sstevel@tonic-gate } else
41240Sstevel@tonic-gate range = 1; /* yeah, it's a range! */
41250Sstevel@tonic-gate continue; /* but do it the next time */
41260Sstevel@tonic-gate }
41270Sstevel@tonic-gate }
41280Sstevel@tonic-gate
41290Sstevel@tonic-gate /* now is the next time */
41300Sstevel@tonic-gate if (!SIZE_ONLY) {
41310Sstevel@tonic-gate IV i;
41320Sstevel@tonic-gate
41330Sstevel@tonic-gate if (prevvalue < 256) {
41340Sstevel@tonic-gate IV ceilvalue = value < 256 ? value : 255;
41350Sstevel@tonic-gate
41360Sstevel@tonic-gate #ifdef EBCDIC
41370Sstevel@tonic-gate /* In EBCDIC [\x89-\x91] should include
41380Sstevel@tonic-gate * the \x8e but [i-j] should not. */
41390Sstevel@tonic-gate if (literal_endpoint == 2 &&
41400Sstevel@tonic-gate ((isLOWER(prevvalue) && isLOWER(ceilvalue)) ||
41410Sstevel@tonic-gate (isUPPER(prevvalue) && isUPPER(ceilvalue))))
41420Sstevel@tonic-gate {
41430Sstevel@tonic-gate if (isLOWER(prevvalue)) {
41440Sstevel@tonic-gate for (i = prevvalue; i <= ceilvalue; i++)
41450Sstevel@tonic-gate if (isLOWER(i))
41460Sstevel@tonic-gate ANYOF_BITMAP_SET(ret, i);
41470Sstevel@tonic-gate } else {
41480Sstevel@tonic-gate for (i = prevvalue; i <= ceilvalue; i++)
41490Sstevel@tonic-gate if (isUPPER(i))
41500Sstevel@tonic-gate ANYOF_BITMAP_SET(ret, i);
41510Sstevel@tonic-gate }
41520Sstevel@tonic-gate }
41530Sstevel@tonic-gate else
41540Sstevel@tonic-gate #endif
41550Sstevel@tonic-gate for (i = prevvalue; i <= ceilvalue; i++)
41560Sstevel@tonic-gate ANYOF_BITMAP_SET(ret, i);
41570Sstevel@tonic-gate }
41580Sstevel@tonic-gate if (value > 255 || UTF) {
41590Sstevel@tonic-gate UV prevnatvalue = NATIVE_TO_UNI(prevvalue);
41600Sstevel@tonic-gate UV natvalue = NATIVE_TO_UNI(value);
41610Sstevel@tonic-gate
41620Sstevel@tonic-gate ANYOF_FLAGS(ret) |= ANYOF_UNICODE;
41630Sstevel@tonic-gate if (prevnatvalue < natvalue) { /* what about > ? */
41640Sstevel@tonic-gate Perl_sv_catpvf(aTHX_ listsv, "%04"UVxf"\t%04"UVxf"\n",
41650Sstevel@tonic-gate prevnatvalue, natvalue);
41660Sstevel@tonic-gate }
41670Sstevel@tonic-gate else if (prevnatvalue == natvalue) {
41680Sstevel@tonic-gate Perl_sv_catpvf(aTHX_ listsv, "%04"UVxf"\n", natvalue);
41690Sstevel@tonic-gate if (FOLD) {
41700Sstevel@tonic-gate U8 foldbuf[UTF8_MAXLEN_FOLD+1];
41710Sstevel@tonic-gate STRLEN foldlen;
41720Sstevel@tonic-gate UV f = to_uni_fold(natvalue, foldbuf, &foldlen);
41730Sstevel@tonic-gate
41740Sstevel@tonic-gate /* If folding and foldable and a single
41750Sstevel@tonic-gate * character, insert also the folded version
41760Sstevel@tonic-gate * to the charclass. */
41770Sstevel@tonic-gate if (f != value) {
41780Sstevel@tonic-gate if (foldlen == (STRLEN)UNISKIP(f))
41790Sstevel@tonic-gate Perl_sv_catpvf(aTHX_ listsv,
41800Sstevel@tonic-gate "%04"UVxf"\n", f);
41810Sstevel@tonic-gate else {
41820Sstevel@tonic-gate /* Any multicharacter foldings
41830Sstevel@tonic-gate * require the following transform:
41840Sstevel@tonic-gate * [ABCDEF] -> (?:[ABCabcDEFd]|pq|rst)
41850Sstevel@tonic-gate * where E folds into "pq" and F folds
41860Sstevel@tonic-gate * into "rst", all other characters
41870Sstevel@tonic-gate * fold to single characters. We save
41880Sstevel@tonic-gate * away these multicharacter foldings,
41890Sstevel@tonic-gate * to be later saved as part of the
41900Sstevel@tonic-gate * additional "s" data. */
41910Sstevel@tonic-gate SV *sv;
41920Sstevel@tonic-gate
41930Sstevel@tonic-gate if (!unicode_alternate)
41940Sstevel@tonic-gate unicode_alternate = newAV();
41950Sstevel@tonic-gate sv = newSVpvn((char*)foldbuf, foldlen);
41960Sstevel@tonic-gate SvUTF8_on(sv);
41970Sstevel@tonic-gate av_push(unicode_alternate, sv);
41980Sstevel@tonic-gate }
41990Sstevel@tonic-gate }
42000Sstevel@tonic-gate
42010Sstevel@tonic-gate /* If folding and the value is one of the Greek
42020Sstevel@tonic-gate * sigmas insert a few more sigmas to make the
42030Sstevel@tonic-gate * folding rules of the sigmas to work right.
42040Sstevel@tonic-gate * Note that not all the possible combinations
42050Sstevel@tonic-gate * are handled here: some of them are handled
42060Sstevel@tonic-gate * by the standard folding rules, and some of
42070Sstevel@tonic-gate * them (literal or EXACTF cases) are handled
42080Sstevel@tonic-gate * during runtime in regexec.c:S_find_byclass(). */
42090Sstevel@tonic-gate if (value == UNICODE_GREEK_SMALL_LETTER_FINAL_SIGMA) {
42100Sstevel@tonic-gate Perl_sv_catpvf(aTHX_ listsv, "%04"UVxf"\n",
42110Sstevel@tonic-gate (UV)UNICODE_GREEK_CAPITAL_LETTER_SIGMA);
42120Sstevel@tonic-gate Perl_sv_catpvf(aTHX_ listsv, "%04"UVxf"\n",
42130Sstevel@tonic-gate (UV)UNICODE_GREEK_SMALL_LETTER_SIGMA);
42140Sstevel@tonic-gate }
42150Sstevel@tonic-gate else if (value == UNICODE_GREEK_CAPITAL_LETTER_SIGMA)
42160Sstevel@tonic-gate Perl_sv_catpvf(aTHX_ listsv, "%04"UVxf"\n",
42170Sstevel@tonic-gate (UV)UNICODE_GREEK_SMALL_LETTER_SIGMA);
42180Sstevel@tonic-gate }
42190Sstevel@tonic-gate }
42200Sstevel@tonic-gate }
42210Sstevel@tonic-gate #ifdef EBCDIC
42220Sstevel@tonic-gate literal_endpoint = 0;
42230Sstevel@tonic-gate #endif
42240Sstevel@tonic-gate }
42250Sstevel@tonic-gate
42260Sstevel@tonic-gate range = 0; /* this range (if it was one) is done now */
42270Sstevel@tonic-gate }
42280Sstevel@tonic-gate
42290Sstevel@tonic-gate if (need_class) {
42300Sstevel@tonic-gate ANYOF_FLAGS(ret) |= ANYOF_LARGE;
42310Sstevel@tonic-gate if (SIZE_ONLY)
42320Sstevel@tonic-gate RExC_size += ANYOF_CLASS_ADD_SKIP;
42330Sstevel@tonic-gate else
42340Sstevel@tonic-gate RExC_emit += ANYOF_CLASS_ADD_SKIP;
42350Sstevel@tonic-gate }
42360Sstevel@tonic-gate
42370Sstevel@tonic-gate /* optimize case-insensitive simple patterns (e.g. /[a-z]/i) */
42380Sstevel@tonic-gate if (!SIZE_ONLY &&
42390Sstevel@tonic-gate /* If the only flag is folding (plus possibly inversion). */
42400Sstevel@tonic-gate ((ANYOF_FLAGS(ret) & (ANYOF_FLAGS_ALL ^ ANYOF_INVERT)) == ANYOF_FOLD)
42410Sstevel@tonic-gate ) {
42420Sstevel@tonic-gate for (value = 0; value < 256; ++value) {
42430Sstevel@tonic-gate if (ANYOF_BITMAP_TEST(ret, value)) {
42440Sstevel@tonic-gate UV fold = PL_fold[value];
42450Sstevel@tonic-gate
42460Sstevel@tonic-gate if (fold != value)
42470Sstevel@tonic-gate ANYOF_BITMAP_SET(ret, fold);
42480Sstevel@tonic-gate }
42490Sstevel@tonic-gate }
42500Sstevel@tonic-gate ANYOF_FLAGS(ret) &= ~ANYOF_FOLD;
42510Sstevel@tonic-gate }
42520Sstevel@tonic-gate
42530Sstevel@tonic-gate /* optimize inverted simple patterns (e.g. [^a-z]) */
42540Sstevel@tonic-gate if (!SIZE_ONLY && optimize_invert &&
42550Sstevel@tonic-gate /* If the only flag is inversion. */
42560Sstevel@tonic-gate (ANYOF_FLAGS(ret) & ANYOF_FLAGS_ALL) == ANYOF_INVERT) {
42570Sstevel@tonic-gate for (value = 0; value < ANYOF_BITMAP_SIZE; ++value)
42580Sstevel@tonic-gate ANYOF_BITMAP(ret)[value] ^= ANYOF_FLAGS_ALL;
42590Sstevel@tonic-gate ANYOF_FLAGS(ret) = ANYOF_UNICODE_ALL;
42600Sstevel@tonic-gate }
42610Sstevel@tonic-gate
42620Sstevel@tonic-gate if (!SIZE_ONLY) {
42630Sstevel@tonic-gate AV *av = newAV();
42640Sstevel@tonic-gate SV *rv;
42650Sstevel@tonic-gate
42660Sstevel@tonic-gate /* The 0th element stores the character class description
42670Sstevel@tonic-gate * in its textual form: used later (regexec.c:Perl_regclass_swash())
42680Sstevel@tonic-gate * to initialize the appropriate swash (which gets stored in
42690Sstevel@tonic-gate * the 1st element), and also useful for dumping the regnode.
42700Sstevel@tonic-gate * The 2nd element stores the multicharacter foldings,
42710Sstevel@tonic-gate * used later (regexec.c:S_reginclass()). */
42720Sstevel@tonic-gate av_store(av, 0, listsv);
42730Sstevel@tonic-gate av_store(av, 1, NULL);
42740Sstevel@tonic-gate av_store(av, 2, (SV*)unicode_alternate);
42750Sstevel@tonic-gate rv = newRV_noinc((SV*)av);
42760Sstevel@tonic-gate n = add_data(pRExC_state, 1, "s");
42770Sstevel@tonic-gate RExC_rx->data->data[n] = (void*)rv;
42780Sstevel@tonic-gate ARG_SET(ret, n);
42790Sstevel@tonic-gate }
42800Sstevel@tonic-gate
42810Sstevel@tonic-gate return ret;
42820Sstevel@tonic-gate }
42830Sstevel@tonic-gate
42840Sstevel@tonic-gate STATIC char*
S_nextchar(pTHX_ RExC_state_t * pRExC_state)42850Sstevel@tonic-gate S_nextchar(pTHX_ RExC_state_t *pRExC_state)
42860Sstevel@tonic-gate {
42870Sstevel@tonic-gate char* retval = RExC_parse++;
42880Sstevel@tonic-gate
42890Sstevel@tonic-gate for (;;) {
42900Sstevel@tonic-gate if (*RExC_parse == '(' && RExC_parse[1] == '?' &&
42910Sstevel@tonic-gate RExC_parse[2] == '#') {
42920Sstevel@tonic-gate while (*RExC_parse != ')') {
42930Sstevel@tonic-gate if (RExC_parse == RExC_end)
42940Sstevel@tonic-gate FAIL("Sequence (?#... not terminated");
42950Sstevel@tonic-gate RExC_parse++;
42960Sstevel@tonic-gate }
42970Sstevel@tonic-gate RExC_parse++;
42980Sstevel@tonic-gate continue;
42990Sstevel@tonic-gate }
43000Sstevel@tonic-gate if (RExC_flags & PMf_EXTENDED) {
43010Sstevel@tonic-gate if (isSPACE(*RExC_parse)) {
43020Sstevel@tonic-gate RExC_parse++;
43030Sstevel@tonic-gate continue;
43040Sstevel@tonic-gate }
43050Sstevel@tonic-gate else if (*RExC_parse == '#') {
43060Sstevel@tonic-gate while (RExC_parse < RExC_end)
43070Sstevel@tonic-gate if (*RExC_parse++ == '\n') break;
43080Sstevel@tonic-gate continue;
43090Sstevel@tonic-gate }
43100Sstevel@tonic-gate }
43110Sstevel@tonic-gate return retval;
43120Sstevel@tonic-gate }
43130Sstevel@tonic-gate }
43140Sstevel@tonic-gate
43150Sstevel@tonic-gate /*
43160Sstevel@tonic-gate - reg_node - emit a node
43170Sstevel@tonic-gate */
43180Sstevel@tonic-gate STATIC regnode * /* Location. */
S_reg_node(pTHX_ RExC_state_t * pRExC_state,U8 op)43190Sstevel@tonic-gate S_reg_node(pTHX_ RExC_state_t *pRExC_state, U8 op)
43200Sstevel@tonic-gate {
43210Sstevel@tonic-gate register regnode *ret;
43220Sstevel@tonic-gate register regnode *ptr;
43230Sstevel@tonic-gate
43240Sstevel@tonic-gate ret = RExC_emit;
43250Sstevel@tonic-gate if (SIZE_ONLY) {
43260Sstevel@tonic-gate SIZE_ALIGN(RExC_size);
43270Sstevel@tonic-gate RExC_size += 1;
43280Sstevel@tonic-gate return(ret);
43290Sstevel@tonic-gate }
43300Sstevel@tonic-gate
43310Sstevel@tonic-gate NODE_ALIGN_FILL(ret);
43320Sstevel@tonic-gate ptr = ret;
43330Sstevel@tonic-gate FILL_ADVANCE_NODE(ptr, op);
43340Sstevel@tonic-gate if (RExC_offsets) { /* MJD */
43350Sstevel@tonic-gate MJD_OFFSET_DEBUG(("%s:%u: (op %s) %s %u <- %u (len %u) (max %u).\n",
43360Sstevel@tonic-gate "reg_node", __LINE__,
43370Sstevel@tonic-gate reg_name[op],
43380Sstevel@tonic-gate RExC_emit - RExC_emit_start > RExC_offsets[0]
43390Sstevel@tonic-gate ? "Overwriting end of array!\n" : "OK",
43400Sstevel@tonic-gate RExC_emit - RExC_emit_start,
43410Sstevel@tonic-gate RExC_parse - RExC_start,
43420Sstevel@tonic-gate RExC_offsets[0]));
43430Sstevel@tonic-gate Set_Node_Offset(RExC_emit, RExC_parse + (op == END));
43440Sstevel@tonic-gate }
43450Sstevel@tonic-gate
43460Sstevel@tonic-gate RExC_emit = ptr;
43470Sstevel@tonic-gate
43480Sstevel@tonic-gate return(ret);
43490Sstevel@tonic-gate }
43500Sstevel@tonic-gate
43510Sstevel@tonic-gate /*
43520Sstevel@tonic-gate - reganode - emit a node with an argument
43530Sstevel@tonic-gate */
43540Sstevel@tonic-gate STATIC regnode * /* Location. */
S_reganode(pTHX_ RExC_state_t * pRExC_state,U8 op,U32 arg)43550Sstevel@tonic-gate S_reganode(pTHX_ RExC_state_t *pRExC_state, U8 op, U32 arg)
43560Sstevel@tonic-gate {
43570Sstevel@tonic-gate register regnode *ret;
43580Sstevel@tonic-gate register regnode *ptr;
43590Sstevel@tonic-gate
43600Sstevel@tonic-gate ret = RExC_emit;
43610Sstevel@tonic-gate if (SIZE_ONLY) {
43620Sstevel@tonic-gate SIZE_ALIGN(RExC_size);
43630Sstevel@tonic-gate RExC_size += 2;
43640Sstevel@tonic-gate return(ret);
43650Sstevel@tonic-gate }
43660Sstevel@tonic-gate
43670Sstevel@tonic-gate NODE_ALIGN_FILL(ret);
43680Sstevel@tonic-gate ptr = ret;
43690Sstevel@tonic-gate FILL_ADVANCE_NODE_ARG(ptr, op, arg);
43700Sstevel@tonic-gate if (RExC_offsets) { /* MJD */
43710Sstevel@tonic-gate MJD_OFFSET_DEBUG(("%s(%d): (op %s) %s %u <- %u (max %u).\n",
43720Sstevel@tonic-gate "reganode",
43730Sstevel@tonic-gate __LINE__,
43740Sstevel@tonic-gate reg_name[op],
43750Sstevel@tonic-gate RExC_emit - RExC_emit_start > RExC_offsets[0] ?
43760Sstevel@tonic-gate "Overwriting end of array!\n" : "OK",
43770Sstevel@tonic-gate RExC_emit - RExC_emit_start,
43780Sstevel@tonic-gate RExC_parse - RExC_start,
43790Sstevel@tonic-gate RExC_offsets[0]));
43800Sstevel@tonic-gate Set_Cur_Node_Offset;
43810Sstevel@tonic-gate }
43820Sstevel@tonic-gate
43830Sstevel@tonic-gate RExC_emit = ptr;
43840Sstevel@tonic-gate
43850Sstevel@tonic-gate return(ret);
43860Sstevel@tonic-gate }
43870Sstevel@tonic-gate
43880Sstevel@tonic-gate /*
43890Sstevel@tonic-gate - reguni - emit (if appropriate) a Unicode character
43900Sstevel@tonic-gate */
43910Sstevel@tonic-gate STATIC void
S_reguni(pTHX_ RExC_state_t * pRExC_state,UV uv,char * s,STRLEN * lenp)43920Sstevel@tonic-gate S_reguni(pTHX_ RExC_state_t *pRExC_state, UV uv, char* s, STRLEN* lenp)
43930Sstevel@tonic-gate {
43940Sstevel@tonic-gate *lenp = SIZE_ONLY ? UNISKIP(uv) : (uvchr_to_utf8((U8*)s, uv) - (U8*)s);
43950Sstevel@tonic-gate }
43960Sstevel@tonic-gate
43970Sstevel@tonic-gate /*
43980Sstevel@tonic-gate - reginsert - insert an operator in front of already-emitted operand
43990Sstevel@tonic-gate *
44000Sstevel@tonic-gate * Means relocating the operand.
44010Sstevel@tonic-gate */
44020Sstevel@tonic-gate STATIC void
S_reginsert(pTHX_ RExC_state_t * pRExC_state,U8 op,regnode * opnd)44030Sstevel@tonic-gate S_reginsert(pTHX_ RExC_state_t *pRExC_state, U8 op, regnode *opnd)
44040Sstevel@tonic-gate {
44050Sstevel@tonic-gate register regnode *src;
44060Sstevel@tonic-gate register regnode *dst;
44070Sstevel@tonic-gate register regnode *place;
44080Sstevel@tonic-gate register int offset = regarglen[(U8)op];
44090Sstevel@tonic-gate
44100Sstevel@tonic-gate /* (PL_regkind[(U8)op] == CURLY ? EXTRA_STEP_2ARGS : 0); */
44110Sstevel@tonic-gate
44120Sstevel@tonic-gate if (SIZE_ONLY) {
44130Sstevel@tonic-gate RExC_size += NODE_STEP_REGNODE + offset;
44140Sstevel@tonic-gate return;
44150Sstevel@tonic-gate }
44160Sstevel@tonic-gate
44170Sstevel@tonic-gate src = RExC_emit;
44180Sstevel@tonic-gate RExC_emit += NODE_STEP_REGNODE + offset;
44190Sstevel@tonic-gate dst = RExC_emit;
44200Sstevel@tonic-gate while (src > opnd) {
44210Sstevel@tonic-gate StructCopy(--src, --dst, regnode);
44220Sstevel@tonic-gate if (RExC_offsets) { /* MJD 20010112 */
44230Sstevel@tonic-gate MJD_OFFSET_DEBUG(("%s(%d): (op %s) %s copy %u -> %u (max %u).\n",
44240Sstevel@tonic-gate "reg_insert",
44250Sstevel@tonic-gate __LINE__,
44260Sstevel@tonic-gate reg_name[op],
44270Sstevel@tonic-gate dst - RExC_emit_start > RExC_offsets[0]
44280Sstevel@tonic-gate ? "Overwriting end of array!\n" : "OK",
44290Sstevel@tonic-gate src - RExC_emit_start,
44300Sstevel@tonic-gate dst - RExC_emit_start,
44310Sstevel@tonic-gate RExC_offsets[0]));
44320Sstevel@tonic-gate Set_Node_Offset_To_R(dst-RExC_emit_start, Node_Offset(src));
44330Sstevel@tonic-gate Set_Node_Length_To_R(dst-RExC_emit_start, Node_Length(src));
44340Sstevel@tonic-gate }
44350Sstevel@tonic-gate }
44360Sstevel@tonic-gate
44370Sstevel@tonic-gate
44380Sstevel@tonic-gate place = opnd; /* Op node, where operand used to be. */
44390Sstevel@tonic-gate if (RExC_offsets) { /* MJD */
44400Sstevel@tonic-gate MJD_OFFSET_DEBUG(("%s(%d): (op %s) %s %u <- %u (max %u).\n",
44410Sstevel@tonic-gate "reginsert",
44420Sstevel@tonic-gate __LINE__,
44430Sstevel@tonic-gate reg_name[op],
44440Sstevel@tonic-gate place - RExC_emit_start > RExC_offsets[0]
44450Sstevel@tonic-gate ? "Overwriting end of array!\n" : "OK",
44460Sstevel@tonic-gate place - RExC_emit_start,
44470Sstevel@tonic-gate RExC_parse - RExC_start,
44480Sstevel@tonic-gate RExC_offsets[0]));
44490Sstevel@tonic-gate Set_Node_Offset(place, RExC_parse);
44500Sstevel@tonic-gate Set_Node_Length(place, 1);
44510Sstevel@tonic-gate }
44520Sstevel@tonic-gate src = NEXTOPER(place);
44530Sstevel@tonic-gate FILL_ADVANCE_NODE(place, op);
44540Sstevel@tonic-gate Zero(src, offset, regnode);
44550Sstevel@tonic-gate }
44560Sstevel@tonic-gate
44570Sstevel@tonic-gate /*
44580Sstevel@tonic-gate - regtail - set the next-pointer at the end of a node chain of p to val.
44590Sstevel@tonic-gate */
44600Sstevel@tonic-gate STATIC void
S_regtail(pTHX_ RExC_state_t * pRExC_state,regnode * p,regnode * val)44610Sstevel@tonic-gate S_regtail(pTHX_ RExC_state_t *pRExC_state, regnode *p, regnode *val)
44620Sstevel@tonic-gate {
44630Sstevel@tonic-gate register regnode *scan;
44640Sstevel@tonic-gate register regnode *temp;
44650Sstevel@tonic-gate
44660Sstevel@tonic-gate if (SIZE_ONLY)
44670Sstevel@tonic-gate return;
44680Sstevel@tonic-gate
44690Sstevel@tonic-gate /* Find last node. */
44700Sstevel@tonic-gate scan = p;
44710Sstevel@tonic-gate for (;;) {
44720Sstevel@tonic-gate temp = regnext(scan);
44730Sstevel@tonic-gate if (temp == NULL)
44740Sstevel@tonic-gate break;
44750Sstevel@tonic-gate scan = temp;
44760Sstevel@tonic-gate }
44770Sstevel@tonic-gate
44780Sstevel@tonic-gate if (reg_off_by_arg[OP(scan)]) {
44790Sstevel@tonic-gate ARG_SET(scan, val - scan);
44800Sstevel@tonic-gate }
44810Sstevel@tonic-gate else {
44820Sstevel@tonic-gate NEXT_OFF(scan) = val - scan;
44830Sstevel@tonic-gate }
44840Sstevel@tonic-gate }
44850Sstevel@tonic-gate
44860Sstevel@tonic-gate /*
44870Sstevel@tonic-gate - regoptail - regtail on operand of first argument; nop if operandless
44880Sstevel@tonic-gate */
44890Sstevel@tonic-gate STATIC void
S_regoptail(pTHX_ RExC_state_t * pRExC_state,regnode * p,regnode * val)44900Sstevel@tonic-gate S_regoptail(pTHX_ RExC_state_t *pRExC_state, regnode *p, regnode *val)
44910Sstevel@tonic-gate {
44920Sstevel@tonic-gate /* "Operandless" and "op != BRANCH" are synonymous in practice. */
44930Sstevel@tonic-gate if (p == NULL || SIZE_ONLY)
44940Sstevel@tonic-gate return;
44950Sstevel@tonic-gate if (PL_regkind[(U8)OP(p)] == BRANCH) {
44960Sstevel@tonic-gate regtail(pRExC_state, NEXTOPER(p), val);
44970Sstevel@tonic-gate }
44980Sstevel@tonic-gate else if ( PL_regkind[(U8)OP(p)] == BRANCHJ) {
44990Sstevel@tonic-gate regtail(pRExC_state, NEXTOPER(NEXTOPER(p)), val);
45000Sstevel@tonic-gate }
45010Sstevel@tonic-gate else
45020Sstevel@tonic-gate return;
45030Sstevel@tonic-gate }
45040Sstevel@tonic-gate
45050Sstevel@tonic-gate /*
45060Sstevel@tonic-gate - regcurly - a little FSA that accepts {\d+,?\d*}
45070Sstevel@tonic-gate */
45080Sstevel@tonic-gate STATIC I32
S_regcurly(pTHX_ register char * s)45090Sstevel@tonic-gate S_regcurly(pTHX_ register char *s)
45100Sstevel@tonic-gate {
45110Sstevel@tonic-gate if (*s++ != '{')
45120Sstevel@tonic-gate return FALSE;
45130Sstevel@tonic-gate if (!isDIGIT(*s))
45140Sstevel@tonic-gate return FALSE;
45150Sstevel@tonic-gate while (isDIGIT(*s))
45160Sstevel@tonic-gate s++;
45170Sstevel@tonic-gate if (*s == ',')
45180Sstevel@tonic-gate s++;
45190Sstevel@tonic-gate while (isDIGIT(*s))
45200Sstevel@tonic-gate s++;
45210Sstevel@tonic-gate if (*s != '}')
45220Sstevel@tonic-gate return FALSE;
45230Sstevel@tonic-gate return TRUE;
45240Sstevel@tonic-gate }
45250Sstevel@tonic-gate
45260Sstevel@tonic-gate
45270Sstevel@tonic-gate #ifdef DEBUGGING
45280Sstevel@tonic-gate
45290Sstevel@tonic-gate STATIC regnode *
S_dumpuntil(pTHX_ regnode * start,regnode * node,regnode * last,SV * sv,I32 l)45300Sstevel@tonic-gate S_dumpuntil(pTHX_ regnode *start, regnode *node, regnode *last, SV* sv, I32 l)
45310Sstevel@tonic-gate {
45320Sstevel@tonic-gate register U8 op = EXACT; /* Arbitrary non-END op. */
45330Sstevel@tonic-gate register regnode *next;
45340Sstevel@tonic-gate
45350Sstevel@tonic-gate while (op != END && (!last || node < last)) {
45360Sstevel@tonic-gate /* While that wasn't END last time... */
45370Sstevel@tonic-gate
45380Sstevel@tonic-gate NODE_ALIGN(node);
45390Sstevel@tonic-gate op = OP(node);
45400Sstevel@tonic-gate if (op == CLOSE)
45410Sstevel@tonic-gate l--;
45420Sstevel@tonic-gate next = regnext(node);
45430Sstevel@tonic-gate /* Where, what. */
45440Sstevel@tonic-gate if (OP(node) == OPTIMIZED)
45450Sstevel@tonic-gate goto after_print;
45460Sstevel@tonic-gate regprop(sv, node);
45470Sstevel@tonic-gate PerlIO_printf(Perl_debug_log, "%4"IVdf":%*s%s", (IV)(node - start),
45480Sstevel@tonic-gate (int)(2*l + 1), "", SvPVX(sv));
45490Sstevel@tonic-gate if (next == NULL) /* Next ptr. */
45500Sstevel@tonic-gate PerlIO_printf(Perl_debug_log, "(0)");
45510Sstevel@tonic-gate else
45520Sstevel@tonic-gate PerlIO_printf(Perl_debug_log, "(%"IVdf")", (IV)(next - start));
45530Sstevel@tonic-gate (void)PerlIO_putc(Perl_debug_log, '\n');
45540Sstevel@tonic-gate after_print:
45550Sstevel@tonic-gate if (PL_regkind[(U8)op] == BRANCHJ) {
45560Sstevel@tonic-gate register regnode *nnode = (OP(next) == LONGJMP
45570Sstevel@tonic-gate ? regnext(next)
45580Sstevel@tonic-gate : next);
45590Sstevel@tonic-gate if (last && nnode > last)
45600Sstevel@tonic-gate nnode = last;
45610Sstevel@tonic-gate node = dumpuntil(start, NEXTOPER(NEXTOPER(node)), nnode, sv, l + 1);
45620Sstevel@tonic-gate }
45630Sstevel@tonic-gate else if (PL_regkind[(U8)op] == BRANCH) {
45640Sstevel@tonic-gate node = dumpuntil(start, NEXTOPER(node), next, sv, l + 1);
45650Sstevel@tonic-gate }
45660Sstevel@tonic-gate else if ( op == CURLY) { /* `next' might be very big: optimizer */
45670Sstevel@tonic-gate node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
45680Sstevel@tonic-gate NEXTOPER(node) + EXTRA_STEP_2ARGS + 1, sv, l + 1);
45690Sstevel@tonic-gate }
45700Sstevel@tonic-gate else if (PL_regkind[(U8)op] == CURLY && op != CURLYX) {
45710Sstevel@tonic-gate node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
45720Sstevel@tonic-gate next, sv, l + 1);
45730Sstevel@tonic-gate }
45740Sstevel@tonic-gate else if ( op == PLUS || op == STAR) {
45750Sstevel@tonic-gate node = dumpuntil(start, NEXTOPER(node), NEXTOPER(node) + 1, sv, l + 1);
45760Sstevel@tonic-gate }
45770Sstevel@tonic-gate else if (op == ANYOF) {
45780Sstevel@tonic-gate /* arglen 1 + class block */
45790Sstevel@tonic-gate node += 1 + ((ANYOF_FLAGS(node) & ANYOF_LARGE)
45800Sstevel@tonic-gate ? ANYOF_CLASS_SKIP : ANYOF_SKIP);
45810Sstevel@tonic-gate node = NEXTOPER(node);
45820Sstevel@tonic-gate }
45830Sstevel@tonic-gate else if (PL_regkind[(U8)op] == EXACT) {
45840Sstevel@tonic-gate /* Literal string, where present. */
45850Sstevel@tonic-gate node += NODE_SZ_STR(node) - 1;
45860Sstevel@tonic-gate node = NEXTOPER(node);
45870Sstevel@tonic-gate }
45880Sstevel@tonic-gate else {
45890Sstevel@tonic-gate node = NEXTOPER(node);
45900Sstevel@tonic-gate node += regarglen[(U8)op];
45910Sstevel@tonic-gate }
45920Sstevel@tonic-gate if (op == CURLYX || op == OPEN)
45930Sstevel@tonic-gate l++;
45940Sstevel@tonic-gate else if (op == WHILEM)
45950Sstevel@tonic-gate l--;
45960Sstevel@tonic-gate }
45970Sstevel@tonic-gate return node;
45980Sstevel@tonic-gate }
45990Sstevel@tonic-gate
46000Sstevel@tonic-gate #endif /* DEBUGGING */
46010Sstevel@tonic-gate
46020Sstevel@tonic-gate /*
46030Sstevel@tonic-gate - regdump - dump a regexp onto Perl_debug_log in vaguely comprehensible form
46040Sstevel@tonic-gate */
46050Sstevel@tonic-gate void
Perl_regdump(pTHX_ regexp * r)46060Sstevel@tonic-gate Perl_regdump(pTHX_ regexp *r)
46070Sstevel@tonic-gate {
46080Sstevel@tonic-gate #ifdef DEBUGGING
46090Sstevel@tonic-gate SV *sv = sv_newmortal();
46100Sstevel@tonic-gate
46110Sstevel@tonic-gate (void)dumpuntil(r->program, r->program + 1, NULL, sv, 0);
46120Sstevel@tonic-gate
46130Sstevel@tonic-gate /* Header fields of interest. */
46140Sstevel@tonic-gate if (r->anchored_substr)
46150Sstevel@tonic-gate PerlIO_printf(Perl_debug_log,
46160Sstevel@tonic-gate "anchored `%s%.*s%s'%s at %"IVdf" ",
46170Sstevel@tonic-gate PL_colors[0],
46180Sstevel@tonic-gate (int)(SvCUR(r->anchored_substr) - (SvTAIL(r->anchored_substr)!=0)),
46190Sstevel@tonic-gate SvPVX(r->anchored_substr),
46200Sstevel@tonic-gate PL_colors[1],
46210Sstevel@tonic-gate SvTAIL(r->anchored_substr) ? "$" : "",
46220Sstevel@tonic-gate (IV)r->anchored_offset);
46230Sstevel@tonic-gate else if (r->anchored_utf8)
46240Sstevel@tonic-gate PerlIO_printf(Perl_debug_log,
46250Sstevel@tonic-gate "anchored utf8 `%s%.*s%s'%s at %"IVdf" ",
46260Sstevel@tonic-gate PL_colors[0],
46270Sstevel@tonic-gate (int)(SvCUR(r->anchored_utf8) - (SvTAIL(r->anchored_utf8)!=0)),
46280Sstevel@tonic-gate SvPVX(r->anchored_utf8),
46290Sstevel@tonic-gate PL_colors[1],
46300Sstevel@tonic-gate SvTAIL(r->anchored_utf8) ? "$" : "",
46310Sstevel@tonic-gate (IV)r->anchored_offset);
46320Sstevel@tonic-gate if (r->float_substr)
46330Sstevel@tonic-gate PerlIO_printf(Perl_debug_log,
46340Sstevel@tonic-gate "floating `%s%.*s%s'%s at %"IVdf"..%"UVuf" ",
46350Sstevel@tonic-gate PL_colors[0],
46360Sstevel@tonic-gate (int)(SvCUR(r->float_substr) - (SvTAIL(r->float_substr)!=0)),
46370Sstevel@tonic-gate SvPVX(r->float_substr),
46380Sstevel@tonic-gate PL_colors[1],
46390Sstevel@tonic-gate SvTAIL(r->float_substr) ? "$" : "",
46400Sstevel@tonic-gate (IV)r->float_min_offset, (UV)r->float_max_offset);
46410Sstevel@tonic-gate else if (r->float_utf8)
46420Sstevel@tonic-gate PerlIO_printf(Perl_debug_log,
46430Sstevel@tonic-gate "floating utf8 `%s%.*s%s'%s at %"IVdf"..%"UVuf" ",
46440Sstevel@tonic-gate PL_colors[0],
46450Sstevel@tonic-gate (int)(SvCUR(r->float_utf8) - (SvTAIL(r->float_utf8)!=0)),
46460Sstevel@tonic-gate SvPVX(r->float_utf8),
46470Sstevel@tonic-gate PL_colors[1],
46480Sstevel@tonic-gate SvTAIL(r->float_utf8) ? "$" : "",
46490Sstevel@tonic-gate (IV)r->float_min_offset, (UV)r->float_max_offset);
46500Sstevel@tonic-gate if (r->check_substr || r->check_utf8)
46510Sstevel@tonic-gate PerlIO_printf(Perl_debug_log,
46520Sstevel@tonic-gate r->check_substr == r->float_substr
46530Sstevel@tonic-gate && r->check_utf8 == r->float_utf8
46540Sstevel@tonic-gate ? "(checking floating" : "(checking anchored");
46550Sstevel@tonic-gate if (r->reganch & ROPT_NOSCAN)
46560Sstevel@tonic-gate PerlIO_printf(Perl_debug_log, " noscan");
46570Sstevel@tonic-gate if (r->reganch & ROPT_CHECK_ALL)
46580Sstevel@tonic-gate PerlIO_printf(Perl_debug_log, " isall");
46590Sstevel@tonic-gate if (r->check_substr || r->check_utf8)
46600Sstevel@tonic-gate PerlIO_printf(Perl_debug_log, ") ");
46610Sstevel@tonic-gate
46620Sstevel@tonic-gate if (r->regstclass) {
46630Sstevel@tonic-gate regprop(sv, r->regstclass);
46640Sstevel@tonic-gate PerlIO_printf(Perl_debug_log, "stclass `%s' ", SvPVX(sv));
46650Sstevel@tonic-gate }
46660Sstevel@tonic-gate if (r->reganch & ROPT_ANCH) {
46670Sstevel@tonic-gate PerlIO_printf(Perl_debug_log, "anchored");
46680Sstevel@tonic-gate if (r->reganch & ROPT_ANCH_BOL)
46690Sstevel@tonic-gate PerlIO_printf(Perl_debug_log, "(BOL)");
46700Sstevel@tonic-gate if (r->reganch & ROPT_ANCH_MBOL)
46710Sstevel@tonic-gate PerlIO_printf(Perl_debug_log, "(MBOL)");
46720Sstevel@tonic-gate if (r->reganch & ROPT_ANCH_SBOL)
46730Sstevel@tonic-gate PerlIO_printf(Perl_debug_log, "(SBOL)");
46740Sstevel@tonic-gate if (r->reganch & ROPT_ANCH_GPOS)
46750Sstevel@tonic-gate PerlIO_printf(Perl_debug_log, "(GPOS)");
46760Sstevel@tonic-gate PerlIO_putc(Perl_debug_log, ' ');
46770Sstevel@tonic-gate }
46780Sstevel@tonic-gate if (r->reganch & ROPT_GPOS_SEEN)
46790Sstevel@tonic-gate PerlIO_printf(Perl_debug_log, "GPOS ");
46800Sstevel@tonic-gate if (r->reganch & ROPT_SKIP)
46810Sstevel@tonic-gate PerlIO_printf(Perl_debug_log, "plus ");
46820Sstevel@tonic-gate if (r->reganch & ROPT_IMPLICIT)
46830Sstevel@tonic-gate PerlIO_printf(Perl_debug_log, "implicit ");
46840Sstevel@tonic-gate PerlIO_printf(Perl_debug_log, "minlen %ld ", (long) r->minlen);
46850Sstevel@tonic-gate if (r->reganch & ROPT_EVAL_SEEN)
46860Sstevel@tonic-gate PerlIO_printf(Perl_debug_log, "with eval ");
46870Sstevel@tonic-gate PerlIO_printf(Perl_debug_log, "\n");
46880Sstevel@tonic-gate if (r->offsets) {
46890Sstevel@tonic-gate U32 i;
46900Sstevel@tonic-gate U32 len = r->offsets[0];
46910Sstevel@tonic-gate PerlIO_printf(Perl_debug_log, "Offsets: [%"UVuf"]\n\t", (UV)r->offsets[0]);
46920Sstevel@tonic-gate for (i = 1; i <= len; i++)
46930Sstevel@tonic-gate PerlIO_printf(Perl_debug_log, "%"UVuf"[%"UVuf"] ",
46940Sstevel@tonic-gate (UV)r->offsets[i*2-1],
46950Sstevel@tonic-gate (UV)r->offsets[i*2]);
46960Sstevel@tonic-gate PerlIO_printf(Perl_debug_log, "\n");
46970Sstevel@tonic-gate }
46980Sstevel@tonic-gate #endif /* DEBUGGING */
46990Sstevel@tonic-gate }
47000Sstevel@tonic-gate
47010Sstevel@tonic-gate #ifdef DEBUGGING
47020Sstevel@tonic-gate
47030Sstevel@tonic-gate STATIC void
S_put_byte(pTHX_ SV * sv,int c)47040Sstevel@tonic-gate S_put_byte(pTHX_ SV *sv, int c)
47050Sstevel@tonic-gate {
47060Sstevel@tonic-gate if (isCNTRL(c) || c == 255 || !isPRINT(c))
47070Sstevel@tonic-gate Perl_sv_catpvf(aTHX_ sv, "\\%o", c);
47080Sstevel@tonic-gate else if (c == '-' || c == ']' || c == '\\' || c == '^')
47090Sstevel@tonic-gate Perl_sv_catpvf(aTHX_ sv, "\\%c", c);
47100Sstevel@tonic-gate else
47110Sstevel@tonic-gate Perl_sv_catpvf(aTHX_ sv, "%c", c);
47120Sstevel@tonic-gate }
47130Sstevel@tonic-gate
47140Sstevel@tonic-gate #endif /* DEBUGGING */
47150Sstevel@tonic-gate
47160Sstevel@tonic-gate /*
47170Sstevel@tonic-gate - regprop - printable representation of opcode
47180Sstevel@tonic-gate */
47190Sstevel@tonic-gate void
Perl_regprop(pTHX_ SV * sv,regnode * o)47200Sstevel@tonic-gate Perl_regprop(pTHX_ SV *sv, regnode *o)
47210Sstevel@tonic-gate {
47220Sstevel@tonic-gate #ifdef DEBUGGING
47230Sstevel@tonic-gate register int k;
47240Sstevel@tonic-gate
47250Sstevel@tonic-gate sv_setpvn(sv, "", 0);
47260Sstevel@tonic-gate if (OP(o) >= reg_num) /* regnode.type is unsigned */
47270Sstevel@tonic-gate /* It would be nice to FAIL() here, but this may be called from
47280Sstevel@tonic-gate regexec.c, and it would be hard to supply pRExC_state. */
47290Sstevel@tonic-gate Perl_croak(aTHX_ "Corrupted regexp opcode");
47300Sstevel@tonic-gate sv_catpv(sv, (char*)reg_name[OP(o)]); /* Take off const! */
47310Sstevel@tonic-gate
47320Sstevel@tonic-gate k = PL_regkind[(U8)OP(o)];
47330Sstevel@tonic-gate
47340Sstevel@tonic-gate if (k == EXACT) {
47350Sstevel@tonic-gate SV *dsv = sv_2mortal(newSVpvn("", 0));
47360Sstevel@tonic-gate /* Using is_utf8_string() is a crude hack but it may
47370Sstevel@tonic-gate * be the best for now since we have no flag "this EXACTish
47380Sstevel@tonic-gate * node was UTF-8" --jhi */
47390Sstevel@tonic-gate bool do_utf8 = is_utf8_string((U8*)STRING(o), STR_LEN(o));
47400Sstevel@tonic-gate char *s = do_utf8 ?
47410Sstevel@tonic-gate pv_uni_display(dsv, (U8*)STRING(o), STR_LEN(o), 60,
47420Sstevel@tonic-gate UNI_DISPLAY_REGEX) :
47430Sstevel@tonic-gate STRING(o);
47440Sstevel@tonic-gate int len = do_utf8 ?
47450Sstevel@tonic-gate strlen(s) :
47460Sstevel@tonic-gate STR_LEN(o);
47470Sstevel@tonic-gate Perl_sv_catpvf(aTHX_ sv, " <%s%.*s%s>",
47480Sstevel@tonic-gate PL_colors[0],
47490Sstevel@tonic-gate len, s,
47500Sstevel@tonic-gate PL_colors[1]);
47510Sstevel@tonic-gate }
47520Sstevel@tonic-gate else if (k == CURLY) {
47530Sstevel@tonic-gate if (OP(o) == CURLYM || OP(o) == CURLYN || OP(o) == CURLYX)
47540Sstevel@tonic-gate Perl_sv_catpvf(aTHX_ sv, "[%d]", o->flags); /* Parenth number */
47550Sstevel@tonic-gate Perl_sv_catpvf(aTHX_ sv, " {%d,%d}", ARG1(o), ARG2(o));
47560Sstevel@tonic-gate }
47570Sstevel@tonic-gate else if (k == WHILEM && o->flags) /* Ordinal/of */
47580Sstevel@tonic-gate Perl_sv_catpvf(aTHX_ sv, "[%d/%d]", o->flags & 0xf, o->flags>>4);
47590Sstevel@tonic-gate else if (k == REF || k == OPEN || k == CLOSE || k == GROUPP )
47600Sstevel@tonic-gate Perl_sv_catpvf(aTHX_ sv, "%d", (int)ARG(o)); /* Parenth number */
47610Sstevel@tonic-gate else if (k == LOGICAL)
47620Sstevel@tonic-gate Perl_sv_catpvf(aTHX_ sv, "[%d]", o->flags); /* 2: embedded, otherwise 1 */
47630Sstevel@tonic-gate else if (k == ANYOF) {
47640Sstevel@tonic-gate int i, rangestart = -1;
47650Sstevel@tonic-gate U8 flags = ANYOF_FLAGS(o);
47660Sstevel@tonic-gate const char * const anyofs[] = { /* Should be synchronized with
47670Sstevel@tonic-gate * ANYOF_ #xdefines in regcomp.h */
47680Sstevel@tonic-gate "\\w",
47690Sstevel@tonic-gate "\\W",
47700Sstevel@tonic-gate "\\s",
47710Sstevel@tonic-gate "\\S",
47720Sstevel@tonic-gate "\\d",
47730Sstevel@tonic-gate "\\D",
47740Sstevel@tonic-gate "[:alnum:]",
47750Sstevel@tonic-gate "[:^alnum:]",
47760Sstevel@tonic-gate "[:alpha:]",
47770Sstevel@tonic-gate "[:^alpha:]",
47780Sstevel@tonic-gate "[:ascii:]",
47790Sstevel@tonic-gate "[:^ascii:]",
47800Sstevel@tonic-gate "[:ctrl:]",
47810Sstevel@tonic-gate "[:^ctrl:]",
47820Sstevel@tonic-gate "[:graph:]",
47830Sstevel@tonic-gate "[:^graph:]",
47840Sstevel@tonic-gate "[:lower:]",
47850Sstevel@tonic-gate "[:^lower:]",
47860Sstevel@tonic-gate "[:print:]",
47870Sstevel@tonic-gate "[:^print:]",
47880Sstevel@tonic-gate "[:punct:]",
47890Sstevel@tonic-gate "[:^punct:]",
47900Sstevel@tonic-gate "[:upper:]",
47910Sstevel@tonic-gate "[:^upper:]",
47920Sstevel@tonic-gate "[:xdigit:]",
47930Sstevel@tonic-gate "[:^xdigit:]",
47940Sstevel@tonic-gate "[:space:]",
47950Sstevel@tonic-gate "[:^space:]",
47960Sstevel@tonic-gate "[:blank:]",
47970Sstevel@tonic-gate "[:^blank:]"
47980Sstevel@tonic-gate };
47990Sstevel@tonic-gate
48000Sstevel@tonic-gate if (flags & ANYOF_LOCALE)
48010Sstevel@tonic-gate sv_catpv(sv, "{loc}");
48020Sstevel@tonic-gate if (flags & ANYOF_FOLD)
48030Sstevel@tonic-gate sv_catpv(sv, "{i}");
48040Sstevel@tonic-gate Perl_sv_catpvf(aTHX_ sv, "[%s", PL_colors[0]);
48050Sstevel@tonic-gate if (flags & ANYOF_INVERT)
48060Sstevel@tonic-gate sv_catpv(sv, "^");
48070Sstevel@tonic-gate for (i = 0; i <= 256; i++) {
48080Sstevel@tonic-gate if (i < 256 && ANYOF_BITMAP_TEST(o,i)) {
48090Sstevel@tonic-gate if (rangestart == -1)
48100Sstevel@tonic-gate rangestart = i;
48110Sstevel@tonic-gate } else if (rangestart != -1) {
48120Sstevel@tonic-gate if (i <= rangestart + 3)
48130Sstevel@tonic-gate for (; rangestart < i; rangestart++)
48140Sstevel@tonic-gate put_byte(sv, rangestart);
48150Sstevel@tonic-gate else {
48160Sstevel@tonic-gate put_byte(sv, rangestart);
48170Sstevel@tonic-gate sv_catpv(sv, "-");
48180Sstevel@tonic-gate put_byte(sv, i - 1);
48190Sstevel@tonic-gate }
48200Sstevel@tonic-gate rangestart = -1;
48210Sstevel@tonic-gate }
48220Sstevel@tonic-gate }
48230Sstevel@tonic-gate
48240Sstevel@tonic-gate if (o->flags & ANYOF_CLASS)
48250Sstevel@tonic-gate for (i = 0; i < sizeof(anyofs)/sizeof(char*); i++)
48260Sstevel@tonic-gate if (ANYOF_CLASS_TEST(o,i))
48270Sstevel@tonic-gate sv_catpv(sv, anyofs[i]);
48280Sstevel@tonic-gate
48290Sstevel@tonic-gate if (flags & ANYOF_UNICODE)
48300Sstevel@tonic-gate sv_catpv(sv, "{unicode}");
48310Sstevel@tonic-gate else if (flags & ANYOF_UNICODE_ALL)
48320Sstevel@tonic-gate sv_catpv(sv, "{unicode_all}");
48330Sstevel@tonic-gate
48340Sstevel@tonic-gate {
48350Sstevel@tonic-gate SV *lv;
48360Sstevel@tonic-gate SV *sw = regclass_swash(o, FALSE, &lv, 0);
48370Sstevel@tonic-gate
48380Sstevel@tonic-gate if (lv) {
48390Sstevel@tonic-gate if (sw) {
48400Sstevel@tonic-gate U8 s[UTF8_MAXLEN+1];
48410Sstevel@tonic-gate
48420Sstevel@tonic-gate for (i = 0; i <= 256; i++) { /* just the first 256 */
48430Sstevel@tonic-gate U8 *e = uvchr_to_utf8(s, i);
48440Sstevel@tonic-gate
48450Sstevel@tonic-gate if (i < 256 && swash_fetch(sw, s, TRUE)) {
48460Sstevel@tonic-gate if (rangestart == -1)
48470Sstevel@tonic-gate rangestart = i;
48480Sstevel@tonic-gate } else if (rangestart != -1) {
48490Sstevel@tonic-gate U8 *p;
48500Sstevel@tonic-gate
48510Sstevel@tonic-gate if (i <= rangestart + 3)
48520Sstevel@tonic-gate for (; rangestart < i; rangestart++) {
48530Sstevel@tonic-gate for(e = uvchr_to_utf8(s, rangestart), p = s; p < e; p++)
48540Sstevel@tonic-gate put_byte(sv, *p);
48550Sstevel@tonic-gate }
48560Sstevel@tonic-gate else {
48570Sstevel@tonic-gate for (e = uvchr_to_utf8(s, rangestart), p = s; p < e; p++)
48580Sstevel@tonic-gate put_byte(sv, *p);
48590Sstevel@tonic-gate sv_catpv(sv, "-");
48600Sstevel@tonic-gate for (e = uvchr_to_utf8(s, i - 1), p = s; p < e; p++)
48610Sstevel@tonic-gate put_byte(sv, *p);
48620Sstevel@tonic-gate }
48630Sstevel@tonic-gate rangestart = -1;
48640Sstevel@tonic-gate }
48650Sstevel@tonic-gate }
48660Sstevel@tonic-gate
48670Sstevel@tonic-gate sv_catpv(sv, "..."); /* et cetera */
48680Sstevel@tonic-gate }
48690Sstevel@tonic-gate
48700Sstevel@tonic-gate {
48710Sstevel@tonic-gate char *s = savepv(SvPVX(lv));
48720Sstevel@tonic-gate char *origs = s;
48730Sstevel@tonic-gate
48740Sstevel@tonic-gate while(*s && *s != '\n') s++;
48750Sstevel@tonic-gate
48760Sstevel@tonic-gate if (*s == '\n') {
48770Sstevel@tonic-gate char *t = ++s;
48780Sstevel@tonic-gate
48790Sstevel@tonic-gate while (*s) {
48800Sstevel@tonic-gate if (*s == '\n')
48810Sstevel@tonic-gate *s = ' ';
48820Sstevel@tonic-gate s++;
48830Sstevel@tonic-gate }
48840Sstevel@tonic-gate if (s[-1] == ' ')
48850Sstevel@tonic-gate s[-1] = 0;
48860Sstevel@tonic-gate
48870Sstevel@tonic-gate sv_catpv(sv, t);
48880Sstevel@tonic-gate }
48890Sstevel@tonic-gate
48900Sstevel@tonic-gate Safefree(origs);
48910Sstevel@tonic-gate }
48920Sstevel@tonic-gate }
48930Sstevel@tonic-gate }
48940Sstevel@tonic-gate
48950Sstevel@tonic-gate Perl_sv_catpvf(aTHX_ sv, "%s]", PL_colors[1]);
48960Sstevel@tonic-gate }
48970Sstevel@tonic-gate else if (k == BRANCHJ && (OP(o) == UNLESSM || OP(o) == IFMATCH))
48980Sstevel@tonic-gate Perl_sv_catpvf(aTHX_ sv, "[-%d]", o->flags);
48990Sstevel@tonic-gate #endif /* DEBUGGING */
49000Sstevel@tonic-gate }
49010Sstevel@tonic-gate
49020Sstevel@tonic-gate SV *
Perl_re_intuit_string(pTHX_ regexp * prog)49030Sstevel@tonic-gate Perl_re_intuit_string(pTHX_ regexp *prog)
49040Sstevel@tonic-gate { /* Assume that RE_INTUIT is set */
49050Sstevel@tonic-gate DEBUG_r(
49060Sstevel@tonic-gate { STRLEN n_a;
49070Sstevel@tonic-gate char *s = SvPV(prog->check_substr
49080Sstevel@tonic-gate ? prog->check_substr : prog->check_utf8, n_a);
49090Sstevel@tonic-gate
49100Sstevel@tonic-gate if (!PL_colorset) reginitcolors();
49110Sstevel@tonic-gate PerlIO_printf(Perl_debug_log,
49120Sstevel@tonic-gate "%sUsing REx %ssubstr:%s `%s%.60s%s%s'\n",
49130Sstevel@tonic-gate PL_colors[4],
49140Sstevel@tonic-gate prog->check_substr ? "" : "utf8 ",
49150Sstevel@tonic-gate PL_colors[5],PL_colors[0],
49160Sstevel@tonic-gate s,
49170Sstevel@tonic-gate PL_colors[1],
49180Sstevel@tonic-gate (strlen(s) > 60 ? "..." : ""));
49190Sstevel@tonic-gate } );
49200Sstevel@tonic-gate
49210Sstevel@tonic-gate return prog->check_substr ? prog->check_substr : prog->check_utf8;
49220Sstevel@tonic-gate }
49230Sstevel@tonic-gate
49240Sstevel@tonic-gate void
Perl_pregfree(pTHX_ struct regexp * r)49250Sstevel@tonic-gate Perl_pregfree(pTHX_ struct regexp *r)
49260Sstevel@tonic-gate {
49270Sstevel@tonic-gate #ifdef DEBUGGING
49280Sstevel@tonic-gate SV *dsv = PERL_DEBUG_PAD_ZERO(0);
49290Sstevel@tonic-gate #endif
49300Sstevel@tonic-gate
49310Sstevel@tonic-gate if (!r || (--r->refcnt > 0))
49320Sstevel@tonic-gate return;
49330Sstevel@tonic-gate DEBUG_r({
49340Sstevel@tonic-gate int len;
49350Sstevel@tonic-gate char *s;
49360Sstevel@tonic-gate
49370Sstevel@tonic-gate s = (r->reganch & ROPT_UTF8) ? pv_uni_display(dsv, (U8*)r->precomp,
49380Sstevel@tonic-gate r->prelen, 60, UNI_DISPLAY_REGEX)
49390Sstevel@tonic-gate : pv_display(dsv, r->precomp, r->prelen, 0, 60);
49400Sstevel@tonic-gate len = SvCUR(dsv);
49410Sstevel@tonic-gate if (!PL_colorset)
49420Sstevel@tonic-gate reginitcolors();
49430Sstevel@tonic-gate PerlIO_printf(Perl_debug_log,
49440Sstevel@tonic-gate "%sFreeing REx:%s `%s%*.*s%s%s'\n",
49450Sstevel@tonic-gate PL_colors[4],PL_colors[5],PL_colors[0],
49460Sstevel@tonic-gate len, len, s,
49470Sstevel@tonic-gate PL_colors[1],
49480Sstevel@tonic-gate len > 60 ? "..." : "");
49490Sstevel@tonic-gate });
49500Sstevel@tonic-gate
49510Sstevel@tonic-gate if (r->precomp)
49520Sstevel@tonic-gate Safefree(r->precomp);
49530Sstevel@tonic-gate if (r->offsets) /* 20010421 MJD */
49540Sstevel@tonic-gate Safefree(r->offsets);
49550Sstevel@tonic-gate if (RX_MATCH_COPIED(r))
49560Sstevel@tonic-gate Safefree(r->subbeg);
49570Sstevel@tonic-gate if (r->substrs) {
49580Sstevel@tonic-gate if (r->anchored_substr)
49590Sstevel@tonic-gate SvREFCNT_dec(r->anchored_substr);
49600Sstevel@tonic-gate if (r->anchored_utf8)
49610Sstevel@tonic-gate SvREFCNT_dec(r->anchored_utf8);
49620Sstevel@tonic-gate if (r->float_substr)
49630Sstevel@tonic-gate SvREFCNT_dec(r->float_substr);
49640Sstevel@tonic-gate if (r->float_utf8)
49650Sstevel@tonic-gate SvREFCNT_dec(r->float_utf8);
49660Sstevel@tonic-gate Safefree(r->substrs);
49670Sstevel@tonic-gate }
49680Sstevel@tonic-gate if (r->data) {
49690Sstevel@tonic-gate int n = r->data->count;
49700Sstevel@tonic-gate PAD* new_comppad = NULL;
49710Sstevel@tonic-gate PAD* old_comppad;
49720Sstevel@tonic-gate
49730Sstevel@tonic-gate while (--n >= 0) {
49740Sstevel@tonic-gate /* If you add a ->what type here, update the comment in regcomp.h */
49750Sstevel@tonic-gate switch (r->data->what[n]) {
49760Sstevel@tonic-gate case 's':
49770Sstevel@tonic-gate SvREFCNT_dec((SV*)r->data->data[n]);
49780Sstevel@tonic-gate break;
49790Sstevel@tonic-gate case 'f':
49800Sstevel@tonic-gate Safefree(r->data->data[n]);
49810Sstevel@tonic-gate break;
49820Sstevel@tonic-gate case 'p':
49830Sstevel@tonic-gate new_comppad = (AV*)r->data->data[n];
49840Sstevel@tonic-gate break;
49850Sstevel@tonic-gate case 'o':
49860Sstevel@tonic-gate if (new_comppad == NULL)
49870Sstevel@tonic-gate Perl_croak(aTHX_ "panic: pregfree comppad");
49880Sstevel@tonic-gate PAD_SAVE_LOCAL(old_comppad,
49890Sstevel@tonic-gate /* Watch out for global destruction's random ordering. */
49900Sstevel@tonic-gate (SvTYPE(new_comppad) == SVt_PVAV) ?
49910Sstevel@tonic-gate new_comppad : Null(PAD *)
49920Sstevel@tonic-gate );
49930Sstevel@tonic-gate if (!OpREFCNT_dec((OP_4tree*)r->data->data[n])) {
49940Sstevel@tonic-gate op_free((OP_4tree*)r->data->data[n]);
49950Sstevel@tonic-gate }
49960Sstevel@tonic-gate
49970Sstevel@tonic-gate PAD_RESTORE_LOCAL(old_comppad);
49980Sstevel@tonic-gate SvREFCNT_dec((SV*)new_comppad);
49990Sstevel@tonic-gate new_comppad = NULL;
50000Sstevel@tonic-gate break;
50010Sstevel@tonic-gate case 'n':
50020Sstevel@tonic-gate break;
50030Sstevel@tonic-gate default:
50040Sstevel@tonic-gate Perl_croak(aTHX_ "panic: regfree data code '%c'", r->data->what[n]);
50050Sstevel@tonic-gate }
50060Sstevel@tonic-gate }
50070Sstevel@tonic-gate Safefree(r->data->what);
50080Sstevel@tonic-gate Safefree(r->data);
50090Sstevel@tonic-gate }
50100Sstevel@tonic-gate Safefree(r->startp);
50110Sstevel@tonic-gate Safefree(r->endp);
50120Sstevel@tonic-gate Safefree(r);
50130Sstevel@tonic-gate }
50140Sstevel@tonic-gate
50150Sstevel@tonic-gate /*
50160Sstevel@tonic-gate - regnext - dig the "next" pointer out of a node
50170Sstevel@tonic-gate *
50180Sstevel@tonic-gate * [Note, when REGALIGN is defined there are two places in regmatch()
50190Sstevel@tonic-gate * that bypass this code for speed.]
50200Sstevel@tonic-gate */
50210Sstevel@tonic-gate regnode *
Perl_regnext(pTHX_ register regnode * p)50220Sstevel@tonic-gate Perl_regnext(pTHX_ register regnode *p)
50230Sstevel@tonic-gate {
50240Sstevel@tonic-gate register I32 offset;
50250Sstevel@tonic-gate
50260Sstevel@tonic-gate if (p == &PL_regdummy)
50270Sstevel@tonic-gate return(NULL);
50280Sstevel@tonic-gate
50290Sstevel@tonic-gate offset = (reg_off_by_arg[OP(p)] ? ARG(p) : NEXT_OFF(p));
50300Sstevel@tonic-gate if (offset == 0)
50310Sstevel@tonic-gate return(NULL);
50320Sstevel@tonic-gate
50330Sstevel@tonic-gate return(p+offset);
50340Sstevel@tonic-gate }
50350Sstevel@tonic-gate
50360Sstevel@tonic-gate STATIC void
S_re_croak2(pTHX_ const char * pat1,const char * pat2,...)50370Sstevel@tonic-gate S_re_croak2(pTHX_ const char* pat1,const char* pat2,...)
50380Sstevel@tonic-gate {
50390Sstevel@tonic-gate va_list args;
50400Sstevel@tonic-gate STRLEN l1 = strlen(pat1);
50410Sstevel@tonic-gate STRLEN l2 = strlen(pat2);
50420Sstevel@tonic-gate char buf[512];
50430Sstevel@tonic-gate SV *msv;
50440Sstevel@tonic-gate char *message;
50450Sstevel@tonic-gate
50460Sstevel@tonic-gate if (l1 > 510)
50470Sstevel@tonic-gate l1 = 510;
50480Sstevel@tonic-gate if (l1 + l2 > 510)
50490Sstevel@tonic-gate l2 = 510 - l1;
50500Sstevel@tonic-gate Copy(pat1, buf, l1 , char);
50510Sstevel@tonic-gate Copy(pat2, buf + l1, l2 , char);
50520Sstevel@tonic-gate buf[l1 + l2] = '\n';
50530Sstevel@tonic-gate buf[l1 + l2 + 1] = '\0';
50540Sstevel@tonic-gate #ifdef I_STDARG
50550Sstevel@tonic-gate /* ANSI variant takes additional second argument */
50560Sstevel@tonic-gate va_start(args, pat2);
50570Sstevel@tonic-gate #else
50580Sstevel@tonic-gate va_start(args);
50590Sstevel@tonic-gate #endif
50600Sstevel@tonic-gate msv = vmess(buf, &args);
50610Sstevel@tonic-gate va_end(args);
50620Sstevel@tonic-gate message = SvPV(msv,l1);
50630Sstevel@tonic-gate if (l1 > 512)
50640Sstevel@tonic-gate l1 = 512;
50650Sstevel@tonic-gate Copy(message, buf, l1 , char);
50660Sstevel@tonic-gate buf[l1-1] = '\0'; /* Overwrite \n */
50670Sstevel@tonic-gate Perl_croak(aTHX_ "%s", buf);
50680Sstevel@tonic-gate }
50690Sstevel@tonic-gate
50700Sstevel@tonic-gate /* XXX Here's a total kludge. But we need to re-enter for swash routines. */
50710Sstevel@tonic-gate
50720Sstevel@tonic-gate void
Perl_save_re_context(pTHX)50730Sstevel@tonic-gate Perl_save_re_context(pTHX)
50740Sstevel@tonic-gate {
50750Sstevel@tonic-gate SAVEI32(PL_reg_flags); /* from regexec.c */
50760Sstevel@tonic-gate SAVEPPTR(PL_bostr);
50770Sstevel@tonic-gate SAVEPPTR(PL_reginput); /* String-input pointer. */
50780Sstevel@tonic-gate SAVEPPTR(PL_regbol); /* Beginning of input, for ^ check. */
50790Sstevel@tonic-gate SAVEPPTR(PL_regeol); /* End of input, for $ check. */
50800Sstevel@tonic-gate SAVEVPTR(PL_regstartp); /* Pointer to startp array. */
50810Sstevel@tonic-gate SAVEVPTR(PL_regendp); /* Ditto for endp. */
50820Sstevel@tonic-gate SAVEVPTR(PL_reglastparen); /* Similarly for lastparen. */
50830Sstevel@tonic-gate SAVEVPTR(PL_reglastcloseparen); /* Similarly for lastcloseparen. */
50840Sstevel@tonic-gate SAVEPPTR(PL_regtill); /* How far we are required to go. */
50850Sstevel@tonic-gate SAVEGENERICPV(PL_reg_start_tmp); /* from regexec.c */
50860Sstevel@tonic-gate PL_reg_start_tmp = 0;
50870Sstevel@tonic-gate SAVEI32(PL_reg_start_tmpl); /* from regexec.c */
50880Sstevel@tonic-gate PL_reg_start_tmpl = 0;
50890Sstevel@tonic-gate SAVEVPTR(PL_regdata);
50900Sstevel@tonic-gate SAVEI32(PL_reg_eval_set); /* from regexec.c */
50910Sstevel@tonic-gate SAVEI32(PL_regnarrate); /* from regexec.c */
50920Sstevel@tonic-gate SAVEVPTR(PL_regprogram); /* from regexec.c */
50930Sstevel@tonic-gate SAVEINT(PL_regindent); /* from regexec.c */
50940Sstevel@tonic-gate SAVEVPTR(PL_regcc); /* from regexec.c */
50950Sstevel@tonic-gate SAVEVPTR(PL_curcop);
50960Sstevel@tonic-gate SAVEVPTR(PL_reg_call_cc); /* from regexec.c */
50970Sstevel@tonic-gate SAVEVPTR(PL_reg_re); /* from regexec.c */
50980Sstevel@tonic-gate SAVEPPTR(PL_reg_ganch); /* from regexec.c */
50990Sstevel@tonic-gate SAVESPTR(PL_reg_sv); /* from regexec.c */
51000Sstevel@tonic-gate SAVEBOOL(PL_reg_match_utf8); /* from regexec.c */
51010Sstevel@tonic-gate SAVEVPTR(PL_reg_magic); /* from regexec.c */
51020Sstevel@tonic-gate SAVEI32(PL_reg_oldpos); /* from regexec.c */
51030Sstevel@tonic-gate SAVEVPTR(PL_reg_oldcurpm); /* from regexec.c */
51040Sstevel@tonic-gate SAVEVPTR(PL_reg_curpm); /* from regexec.c */
51050Sstevel@tonic-gate SAVEPPTR(PL_reg_oldsaved); /* old saved substr during match */
51060Sstevel@tonic-gate PL_reg_oldsaved = Nullch;
51070Sstevel@tonic-gate SAVEI32(PL_reg_oldsavedlen); /* old length of saved substr during match */
51080Sstevel@tonic-gate PL_reg_oldsavedlen = 0;
51090Sstevel@tonic-gate SAVEI32(PL_reg_maxiter); /* max wait until caching pos */
51100Sstevel@tonic-gate PL_reg_maxiter = 0;
51110Sstevel@tonic-gate SAVEI32(PL_reg_leftiter); /* wait until caching pos */
51120Sstevel@tonic-gate PL_reg_leftiter = 0;
51130Sstevel@tonic-gate SAVEGENERICPV(PL_reg_poscache); /* cache of pos of WHILEM */
51140Sstevel@tonic-gate PL_reg_poscache = Nullch;
51150Sstevel@tonic-gate SAVEI32(PL_reg_poscache_size); /* size of pos cache of WHILEM */
51160Sstevel@tonic-gate PL_reg_poscache_size = 0;
51170Sstevel@tonic-gate SAVEPPTR(PL_regprecomp); /* uncompiled string. */
51180Sstevel@tonic-gate SAVEI32(PL_regnpar); /* () count. */
51190Sstevel@tonic-gate SAVEI32(PL_regsize); /* from regexec.c */
51200Sstevel@tonic-gate
51210Sstevel@tonic-gate {
51220Sstevel@tonic-gate /* Save $1..$n (#18107: UTF-8 s/(\w+)/uc($1)/e); AMS 20021106. */
51230Sstevel@tonic-gate U32 i;
51240Sstevel@tonic-gate GV *mgv;
51250Sstevel@tonic-gate REGEXP *rx;
51260Sstevel@tonic-gate char digits[16];
51270Sstevel@tonic-gate
51280Sstevel@tonic-gate if (PL_curpm && (rx = PM_GETRE(PL_curpm))) {
51290Sstevel@tonic-gate for (i = 1; i <= rx->nparens; i++) {
51300Sstevel@tonic-gate sprintf(digits, "%lu", (long)i);
51310Sstevel@tonic-gate if ((mgv = gv_fetchpv(digits, FALSE, SVt_PV)))
51320Sstevel@tonic-gate save_scalar(mgv);
51330Sstevel@tonic-gate }
51340Sstevel@tonic-gate }
51350Sstevel@tonic-gate }
51360Sstevel@tonic-gate
51370Sstevel@tonic-gate #ifdef DEBUGGING
51380Sstevel@tonic-gate SAVEPPTR(PL_reg_starttry); /* from regexec.c */
51390Sstevel@tonic-gate #endif
51400Sstevel@tonic-gate }
51410Sstevel@tonic-gate
51420Sstevel@tonic-gate static void
clear_re(pTHX_ void * r)51430Sstevel@tonic-gate clear_re(pTHX_ void *r)
51440Sstevel@tonic-gate {
51450Sstevel@tonic-gate ReREFCNT_dec((regexp *)r);
51460Sstevel@tonic-gate }
51470Sstevel@tonic-gate
5148