* with the POSIX routines of the same names.
*/
+#ifdef PERL_EXT_RE_BUILD
+/* need to replace pregcomp et al, so enable that */
+# ifndef PERL_IN_XSUB_RE
+# define PERL_IN_XSUB_RE
+# endif
+/* need access to debugger hooks */
+# ifndef DEBUGGING
+# define DEBUGGING
+# endif
+#endif
+
+#ifdef PERL_IN_XSUB_RE
+/* We *really* need to overwrite these symbols: */
+# define Perl_pregcomp my_regcomp
+# define Perl_regdump my_regdump
+# define Perl_regprop my_regprop
+/* *These* symbols are masked to allow static link. */
+# define Perl_pregfree my_regfree
+# define Perl_regnext my_regnext
+# define Perl_save_re_context my_save_re_context
+# define Perl_reginitcolors my_reginitcolors
+#endif
+
/*SUPPRESS 112*/
/*
* pregcomp and pregexec -- regsub and regerror are not used in perl
*
**** Alterations to Henry's code are...
****
- **** Copyright (c) 1991-1997, Larry Wall
+ **** Copyright (c) 1991-1998, Larry Wall
****
**** You may distribute under the terms of either the GNU General Public
**** License or the Artistic License, as specified in the README file.
*/
#include "EXTERN.h"
#include "perl.h"
-#include "INTERN.h"
+
+#ifndef PERL_IN_XSUB_RE
+# include "INTERN.h"
+#endif
#define REG_COMP_C
#include "regcomp.h"
* Flags to be passed up and down.
*/
#define WORST 0 /* Worst case. */
-#define HASWIDTH 0x1 /* Known never to match null string. */
+#define HASWIDTH 0x1 /* Known to match non-null strings. */
#define SIMPLE 0x2 /* Simple enough to be STAR/PLUS operand. */
#define SPSTART 0x4 /* Starts with * or +. */
#define TRYAGAIN 0x8 /* Weeded out a declaration. */
static regnode *regatom _((I32 *));
static regnode *regbranch _((I32 *, I32));
static void regc _((U8, char *));
+static void reguni _((UV, char *, I32*));
static regnode *regclass _((void));
+static regnode *regclassutf8 _((void));
STATIC I32 regcurly _((char *));
static regnode *reg_node _((U8));
static regnode *regpiece _((I32 *));
typedef struct {
I32 len_min;
I32 len_delta;
- I32 pos_min;
- I32 pos_delta;
+ I32 pos_min; /* CC */
+ I32 pos_delta; /* CC */
SV *last_found;
I32 last_end; /* min value, <0 unless valid. */
- I32 last_start_min;
- I32 last_start_max;
+ I32 last_start_min; /* CC */
+ I32 last_start_max; /* CC */
SV **longest; /* Either &l_fixed, or &l_float. */
SV *longest_fixed;
- I32 offset_fixed;
+ I32 offset_fixed; /* CC */
SV *longest_float;
- I32 offset_float_min;
- I32 offset_float_max;
+ I32 offset_float_min; /* CC */
+ I32 offset_float_max; /* CC */
I32 flags;
} scan_data_t;
#endif
#define SF_HAS_EVAL 0x200
#define SCF_DO_SUBSTR 0x400
+#define RF_utf8 8
+#define UTF (PL_reg_flags & RF_utf8)
+#define LOC (PL_regflags & PMf_LOCALE)
+#define FOLD (PL_regflags & PMf_FOLD)
+
+#define CHR_SVLEN(sv) (UTF ? sv_len_utf8(sv) : SvCUR(sv))
+#define CHR_DIST(a,b) (UTF ? utf8_distance(a,b) : a - b)
+
STATIC void
scan_commit(scan_data_t *data)
{
- STRLEN l = SvCUR(data->last_found);
- STRLEN old_l = SvCUR(*data->longest);
+ dTHR;
+ STRLEN l = CHR_SVLEN(data->last_found);
+ STRLEN old_l = CHR_SVLEN(*data->longest);
if ((l >= old_l) && ((l > old_l) || (data->flags & SF_BEFORE_EOL))) {
sv_setsv(*data->longest, data->last_found);
|= ((data->flags & SF_BEFORE_EOL) << SF_FIX_SHIFT_EOL);
else
data->flags &= ~SF_FIX_BEFORE_EOL;
- } else {
+ }
+ else {
data->offset_float_min = l ? data->last_start_min : data->pos_min;
data->offset_float_max = (l
? data->last_start_max
/* deltap: Write maxlen-minlen here. */
/* last: Stop before this one. */
{
+ dTHR;
I32 min = 0, pars = 0, code;
regnode *scan = *scanp, *next;
I32 delta = 0;
int is_inf = (flags & SCF_DO_SUBSTR) && (data->flags & SF_IS_INF);
+ int is_inf_internal = 0; /* The studied chunk is infinite */
I32 is_par = OP(scan) == OPEN ? ARG(scan) : 0;
scan_data_t data_fake;
while (scan && OP(scan) != END && scan < last) {
/* Peephole optimizer: */
- if (regkind[(U8)OP(scan)] == EXACT) {
+ if (PL_regkind[(U8)OP(scan)] == EXACT) {
regnode *n = regnext(scan);
U32 stringok = 1;
#ifdef DEBUGGING
next = scan + (*OPERAND(scan) + 2 - 1)/sizeof(regnode) + 2;
/* Skip NOTHING, merge EXACT*. */
while (n &&
- ( regkind[(U8)OP(n)] == NOTHING ||
+ ( PL_regkind[(U8)OP(n)] == NOTHING ||
(stringok && (OP(n) == OP(scan))))
&& NEXT_OFF(n)
&& NEXT_OFF(scan) + NEXT_OFF(n) < I16_MAX) {
if (OP(n) == TAIL || n > next)
stringok = 0;
- if (regkind[(U8)OP(n)] == NOTHING) {
+ if (PL_regkind[(U8)OP(n)] == NOTHING) {
NEXT_OFF(scan) += NEXT_OFF(n);
next = n + NODE_STEP_REGNODE;
#ifdef DEBUGGING
stop = n;
#endif
n = regnext(n);
- } else {
+ }
+ else {
int oldl = *OPERAND(scan);
regnode *nnext = regnext(n);
/* Allow dumping */
n = scan + (*OPERAND(scan) + 2 - 1)/sizeof(regnode) + 2;
while (n <= stop) {
- if (regkind[(U8)OP(n)] != NOTHING || OP(n) == NOTHING) {
+ /* Purify reports a benign UMR here sometimes, because we
+ * don't initialize the OP() slot of a node when that node
+ * is occupied by just the trailing null of the string in
+ * an EXACT node */
+ if (PL_regkind[(U8)OP(n)] != NOTHING || OP(n) == NOTHING) {
OP(n) = OPTIMIZED;
NEXT_OFF(n) = 0;
}
}
if (OP(scan) != CURLYX) {
- int max = (reg_off_by_arg[OP(scan)] ? I32_MAX : U16_MAX);
+ int max = (reg_off_by_arg[OP(scan)]
+ ? I32_MAX
+ /* I32 may be smaller than U16 on CRAYs! */
+ : (I32_MAX < U16_MAX ? I32_MAX : U16_MAX));
int off = (reg_off_by_arg[OP(scan)] ? ARG(scan) : NEXT_OFF(scan));
int noff;
regnode *n = scan;
/* Skip NOTHING and LONGJMP. */
while ((n = regnext(n))
- && ((regkind[(U8)OP(n)] == NOTHING && (noff = NEXT_OFF(n)))
+ && ((PL_regkind[(U8)OP(n)] == NOTHING && (noff = NEXT_OFF(n)))
|| ((OP(n) == LONGJMP) && (noff = ARG(n))))
&& off + noff < max)
off += noff;
if (max1 < minnext + deltanext)
max1 = minnext + deltanext;
if (deltanext == I32_MAX)
- is_inf = 1;
+ is_inf = is_inf_internal = 1;
scan = next;
if (data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
pars++;
}
min += min1;
delta += max1 - min1;
- } else if (code == BRANCHJ) /* single branch is optimized. */
+ }
+ else if (code == BRANCHJ) /* single branch is optimized. */
scan = NEXTOPER(NEXTOPER(scan));
else /* single branch is optimized. */
scan = NEXTOPER(scan);
continue;
- } else if (OP(scan) == EXACT) {
- min += *OPERAND(scan);
+ }
+ else if (OP(scan) == EXACT) {
+ I32 l = *OPERAND(scan);
+ if (UTF) {
+ unsigned char *s = (unsigned char *)(OPERAND(scan)+1);
+ unsigned char *e = s + l;
+ I32 newl = 0;
+ while (s < e) {
+ newl++;
+ s += UTF8SKIP(s);
+ }
+ l = newl;
+ }
+ min += l;
if (flags & SCF_DO_SUBSTR) { /* Update longest substr. */
- I32 l = *OPERAND(scan);
-
/* The code below prefers earlier match for fixed
offset, later match for variable offset. */
if (data->last_end == -1) { /* Update the start info. */
data->last_start_max = is_inf
? I32_MAX : data->pos_min + data->pos_delta;
}
- sv_catpvn(data->last_found, (char *)(OPERAND(scan)+1), l);
+ sv_catpvn(data->last_found, (char *)(OPERAND(scan)+1), *OPERAND(scan));
data->last_end = data->pos_min + l;
data->pos_min += l; /* As in the first entry. */
data->flags &= ~SF_BEFORE_EOL;
}
- } else if (regkind[(U8)OP(scan)] == EXACT) {
+ }
+ else if (PL_regkind[(U8)OP(scan)] == EXACT) {
+ I32 l = *OPERAND(scan);
if (flags & SCF_DO_SUBSTR)
scan_commit(data);
- min += *OPERAND(scan);
+ if (UTF) {
+ unsigned char *s = (unsigned char *)(OPERAND(scan)+1);
+ unsigned char *e = s + l;
+ I32 newl = 0;
+ while (s < e) {
+ newl++;
+ s += UTF8SKIP(s);
+ }
+ l = newl;
+ }
+ min += l;
if (data && (flags & SCF_DO_SUBSTR))
- data->pos_min += *OPERAND(scan);
- } else if (strchr(varies,OP(scan))) {
+ data->pos_min += l;
+ }
+ else if (strchr(PL_varies,OP(scan))) {
I32 mincount, maxcount, minnext, deltanext, pos_before, fl;
regnode *oscan = scan;
- switch (regkind[(U8)OP(scan)]) {
+ switch (PL_regkind[(U8)OP(scan)]) {
case WHILEM:
scan = NEXTOPER(scan);
goto finish;
min++;
/* Fall through. */
case STAR:
- is_inf = 1;
+ is_inf = is_inf_internal = 1;
scan = regnext(scan);
if (flags & SCF_DO_SUBSTR) {
scan_commit(data);
? (flags & ~SCF_DO_SUBSTR) : flags);
if (!scan) /* It was not CURLYX, but CURLY. */
scan = next;
- if (dowarn && (minnext + deltanext == 0)
- && !(data->flags & (SF_HAS_PAR|SF_IN_PAR)))
- warn("Strange *+?{} on zero-length expression");
+ if (ckWARN(WARN_UNSAFE) && (minnext + deltanext == 0)
+ && !(data->flags & (SF_HAS_PAR|SF_IN_PAR))
+ && maxcount <= REG_INFTY/3) /* Complement check for big count */
+ warner(WARN_UNSAFE, "Strange *+?{} on zero-length expression");
min += minnext * mincount;
- is_inf |= (maxcount == REG_INFTY && (minnext + deltanext) > 0
- || deltanext == I32_MAX);
+ is_inf_internal |= (maxcount == REG_INFTY
+ && (minnext + deltanext) > 0
+ || deltanext == I32_MAX);
+ is_inf |= is_inf_internal;
delta += (minnext + deltanext) * maxcount - minnext * mincount;
/* Try powerful optimization CURLYX => CURLYN. */
-#ifdef REGALIGN_STRUCT
if ( OP(oscan) == CURLYX && data
&& data->flags & SF_IN_PAR
&& !(data->flags & SF_HAS_EVAL)
/* Skip open. */
nxt = regnext(nxt);
- if (!strchr(simple,OP(nxt))
- && !(regkind[(U8)OP(nxt)] == EXACT
+ if (!strchr(PL_simple,OP(nxt))
+ && !(PL_regkind[(U8)OP(nxt)] == EXACT
&& *OPERAND(nxt) == 1))
goto nogo;
nxt2 = nxt;
NEXT_OFF(nxt+ 1) = 0; /* just for consistancy. */
#endif
}
-#endif
nogo:
/* Try optimization CURLYX => CURLYM. */
if ( OP(oscan) == CURLYX && data
-#ifdef REGALIGN_STRUCT
&& !(data->flags & SF_HAS_PAR)
-#else
- && !(data->flags & (SF_HAS_PAR|SF_IN_PAR))
-#endif
&& !(data->flags & SF_HAS_EVAL)
&& !deltanext ) {
/* XXXX How to optimize if data == 0? */
&& (OP(nxt2) != WHILEM))
nxt = nxt2;
OP(nxt2) = SUCCEED; /* Whas WHILEM */
-#ifdef REGALIGN_STRUCT
/* Need to optimize away parenths. */
if (data->flags & SF_IN_PAR) {
/* Set the parenth number. */
#endif
/* Optimize again: */
study_chunk(&nxt1, &deltanext, nxt, NULL, 0);
- } else
+ }
+ else
oscan->flags = 0;
-#endif
}
if (data && fl & (SF_HAS_PAR|SF_IN_PAR))
pars++;
? pos_before : data->last_start_min;
STRLEN l;
char *s = SvPV(data->last_found, l);
+ I32 old = b - data->last_start_min;
+
+ if (UTF)
+ old = utf8_hop((U8*)s, old) - (U8*)s;
- l -= b - data->last_start_min;
+ l -= old;
/* Get the added string: */
- last_str = newSVpv(s + b - data->last_start_min, l);
+ last_str = newSVpv(s + old, l);
if (deltanext == 0 && pos_before == b) {
/* What was added is a constant string */
if (mincount > 1) {
sv_setsv(data->last_found, last_str);
data->last_end = data->pos_min;
data->last_start_min =
- data->pos_min - SvCUR(last_str);
+ data->pos_min - CHR_SVLEN(last_str);
data->last_start_max = is_inf
? I32_MAX
: data->pos_min + data->pos_delta
- - SvCUR(last_str);
+ - CHR_SVLEN(last_str);
}
data->longest = &(data->longest_float);
}
+ SvREFCNT_dec(last_str);
}
if (data && (fl & SF_HAS_EVAL))
data->flags |= SF_HAS_EVAL;
optimize_curly_tail:
-#ifdef REGALIGN
if (OP(oscan) != CURLYX) {
- while (regkind[(U8)OP(next = regnext(oscan))] == NOTHING
+ while (PL_regkind[(U8)OP(next = regnext(oscan))] == NOTHING
&& NEXT_OFF(next))
NEXT_OFF(oscan) += NEXT_OFF(next);
}
-#endif
continue;
default: /* REF only? */
if (flags & SCF_DO_SUBSTR) {
scan_commit(data);
data->longest = &(data->longest_float);
}
- is_inf = 1;
+ is_inf = is_inf_internal = 1;
break;
}
- } else if (strchr(simple,OP(scan))) {
+ }
+ else if (strchr(PL_simple,OP(scan)) || PL_regkind[(U8)OP(scan)] == ANYUTF8) {
if (flags & SCF_DO_SUBSTR) {
scan_commit(data);
data->pos_min++;
}
min++;
- } else if (regkind[(U8)OP(scan)] == EOL && flags & SCF_DO_SUBSTR) {
+ }
+ else if (PL_regkind[(U8)OP(scan)] == EOL && flags & SCF_DO_SUBSTR) {
data->flags |= (OP(scan) == MEOL
? SF_BEFORE_MEOL
: SF_BEFORE_SEOL);
- } else if (regkind[(U8)OP(scan)] == BRANCHJ
+ }
+ else if (PL_regkind[(U8)OP(scan)] == BRANCHJ
&& (scan->flags || data)
&& (OP(scan) == IFMATCH || OP(scan) == UNLESSM)) {
I32 deltanext, minnext;
if (scan->flags) {
if (deltanext) {
FAIL("variable length lookbehind not implemented");
- } else if (minnext > U8_MAX) {
+ }
+ else if (minnext > U8_MAX) {
FAIL2("lookbehind longer than %d not implemented", U8_MAX);
}
scan->flags = minnext;
pars++;
if (data && (data_fake.flags & SF_HAS_EVAL))
data->flags |= SF_HAS_EVAL;
- } else if (OP(scan) == OPEN) {
+ }
+ else if (OP(scan) == OPEN) {
pars++;
- } else if (OP(scan) == CLOSE && ARG(scan) == is_par) {
-#ifdef REGALIGN_STRUCT
+ }
+ else if (OP(scan) == CLOSE && ARG(scan) == is_par) {
next = regnext(scan);
if ( next && (OP(next) != WHILEM) && next < last)
-#endif
is_par = 0; /* Disable optimization */
- } else if (OP(scan) == EVAL) {
+ }
+ else if (OP(scan) == EVAL) {
if (data)
data->flags |= SF_HAS_EVAL;
}
+ else if (OP(scan) == LOGICAL && scan->flags == 2) { /* Embedded */
+ if (flags & SCF_DO_SUBSTR) {
+ scan_commit(data);
+ data->longest = &(data->longest_float);
+ }
+ is_inf = is_inf_internal = 1;
+ }
/* Else: zero-length, ignore. */
scan = regnext(scan);
}
finish:
*scanp = scan;
- *deltap = is_inf ? I32_MAX : delta;
+ *deltap = is_inf_internal ? I32_MAX : delta;
if (flags & SCF_DO_SUBSTR && is_inf)
data->pos_delta = I32_MAX - data->pos_min;
if (is_par > U8_MAX)
if (is_par && pars==1 && data) {
data->flags |= SF_IN_PAR;
data->flags &= ~SF_HAS_PAR;
- } else if (pars && data) {
+ }
+ else if (pars && data) {
data->flags |= SF_HAS_PAR;
data->flags &= ~SF_IN_PAR;
}
STATIC I32
add_data(I32 n, char *s)
{
- if (rx->data) {
- Renewc(rx->data,
- sizeof(*rx->data) + sizeof(void*) * (rx->data->count + n - 1),
+ dTHR;
+ if (PL_regcomp_rx->data) {
+ Renewc(PL_regcomp_rx->data,
+ sizeof(*PL_regcomp_rx->data) + sizeof(void*) * (PL_regcomp_rx->data->count + n - 1),
char, struct reg_data);
- Renew(rx->data->what, rx->data->count + n, U8);
- rx->data->count += n;
- } else {
- Newc(1207, rx->data, sizeof(*rx->data) + sizeof(void*) * (n - 1),
+ Renew(PL_regcomp_rx->data->what, PL_regcomp_rx->data->count + n, U8);
+ PL_regcomp_rx->data->count += n;
+ }
+ else {
+ Newc(1207, PL_regcomp_rx->data, sizeof(*PL_regcomp_rx->data) + sizeof(void*) * (n - 1),
char, struct reg_data);
- New(1208, rx->data->what, n, U8);
- rx->data->count = n;
+ New(1208, PL_regcomp_rx->data->what, n, U8);
+ PL_regcomp_rx->data->count = n;
}
- Copy(s, rx->data->what + rx->data->count - n, n, U8);
- return rx->data->count - n;
+ Copy(s, PL_regcomp_rx->data->what + PL_regcomp_rx->data->count - n, n, U8);
+ return PL_regcomp_rx->data->count - n;
+}
+
+void
+reginitcolors(void)
+{
+ dTHR;
+ int i = 0;
+ char *s = PerlEnv_getenv("PERL_RE_COLORS");
+
+ if (s) {
+ PL_colors[0] = s = savepv(s);
+ while (++i < 6) {
+ s = strchr(s, '\t');
+ if (s) {
+ *s = '\0';
+ PL_colors[i] = ++s;
+ }
+ else
+ PL_colors[i] = "";
+ }
+ } else {
+ while (i < 6)
+ PL_colors[i++] = "";
+ }
+ PL_colorset = 1;
}
/*
regexp *
pregcomp(char *exp, char *xend, PMOP *pm)
{
+ dTHR;
register regexp *r;
regnode *scan;
SV **longest;
if (exp == NULL)
FAIL("NULL regexp argument");
- regprecomp = savepvn(exp, xend - exp);
- DEBUG_r(PerlIO_printf(Perl_debug_log, "compiling RE `%*s'\n",
- xend - exp, regprecomp));
- regflags = pm->op_pmflags;
- regsawback = 0;
+ if (PL_curcop == &PL_compiling ? (PL_hints & HINT_UTF8) : IN_UTF8)
+ PL_reg_flags |= RF_utf8;
+ else
+ PL_reg_flags = 0;
+
+ PL_regprecomp = savepvn(exp, xend - exp);
+ DEBUG_r(if (!PL_colorset) reginitcolors());
+ DEBUG_r(PerlIO_printf(Perl_debug_log, "%sCompiling%s RE `%s%*s%s'\n",
+ PL_colors[4],PL_colors[5],PL_colors[0],
+ xend - exp, PL_regprecomp, PL_colors[1]));
+ PL_regflags = pm->op_pmflags;
+ PL_regsawback = 0;
- regseen = 0;
- seen_zerolen = *exp == '^' ? -1 : 0;
- extralen = 0;
+ PL_regseen = 0;
+ PL_seen_zerolen = *exp == '^' ? -1 : 0;
+ PL_seen_evals = 0;
+ PL_extralen = 0;
/* First pass: determine size, legality. */
- regparse = exp;
- regxend = xend;
- regnaughty = 0;
- regnpar = 1;
- regsize = 0L;
- regcode = ®dummy;
- regc((U8)MAGIC, (char*)regcode);
+ PL_regcomp_parse = exp;
+ PL_regxend = xend;
+ PL_regnaughty = 0;
+ PL_regnpar = 1;
+ PL_regsize = 0L;
+ PL_regcode = &PL_regdummy;
+ regc((U8)REG_MAGIC, (char*)PL_regcode);
if (reg(0, &flags) == NULL) {
- Safefree(regprecomp);
- regprecomp = Nullch;
+ Safefree(PL_regprecomp);
+ PL_regprecomp = Nullch;
return(NULL);
}
- DEBUG_r(PerlIO_printf(Perl_debug_log, "size %d ", regsize));
-
- DEBUG_r(
- if (!colorset) {
- int i = 0;
- char *s = PerlEnv_getenv("TERMCAP_COLORS");
-
- colorset = 1;
- if (s) {
- colors[0] = s = savepv(s);
- while (++i < 4) {
- s = strchr(s, '\t');
- if (!s)
- FAIL("Not enough TABs in TERMCAP_COLORS");
- *s = '\0';
- colors[i] = ++s;
- }
- } else {
- while (i < 4)
- colors[i++] = "";
- }
- /* Reset colors: */
- PerlIO_printf(Perl_debug_log, "%s%s%s%s",
- colors[0],colors[1],colors[2],colors[3]);
- }
- );
+ DEBUG_r(PerlIO_printf(Perl_debug_log, "size %d ", PL_regsize));
/* Small enough for pointer-storage convention?
If extralen==0, this means that we will not need long jumps. */
-#ifndef REGALIGN_STRUCT
- if (regsize >= 0x10000L && extralen)
- FAIL("regexp too big");
-#else
- if (regsize >= 0x10000L && extralen)
- regsize += extralen;
+ if (PL_regsize >= 0x10000L && PL_extralen)
+ PL_regsize += PL_extralen;
else
- extralen = 0;
-#endif
+ PL_extralen = 0;
/* Allocate space and initialize. */
- Newc(1001, r, sizeof(regexp) + (unsigned)regsize * sizeof(regnode),
+ Newc(1001, r, sizeof(regexp) + (unsigned)PL_regsize * sizeof(regnode),
char, regexp);
if (r == NULL)
FAIL("regexp out of space");
r->refcnt = 1;
r->prelen = xend - exp;
- r->precomp = regprecomp;
+ r->precomp = PL_regprecomp;
r->subbeg = r->subbase = NULL;
- rx = r;
+ r->nparens = PL_regnpar - 1; /* set early to validate backrefs */
+ PL_regcomp_rx = r;
/* Second pass: emit code. */
- regparse = exp;
- regxend = xend;
- regnaughty = 0;
- regnpar = 1;
- regcode = r->program;
- regc((U8)MAGIC, (char*) regcode++);
+ PL_regcomp_parse = exp;
+ PL_regxend = xend;
+ PL_regnaughty = 0;
+ PL_regnpar = 1;
+ PL_regcode = r->program;
+ /* Store the count of eval-groups for security checks: */
+ PL_regcode->next_off = ((PL_seen_evals > U16_MAX) ? U16_MAX : PL_seen_evals);
+ regc((U8)REG_MAGIC, (char*) PL_regcode++);
r->data = 0;
if (reg(0, &flags) == NULL)
return(NULL);
/* Dig out information for optimizations. */
- pm->op_pmflags = regflags;
- r->reganch = 0;
+ r->reganch = pm->op_pmflags & PMf_COMPILETIME;
+ pm->op_pmflags = PL_regflags;
+ if (UTF)
+ r->reganch |= ROPT_UTF8;
r->regstclass = NULL;
- r->naughty = regnaughty >= 10; /* Probably an expensive pattern. */
+ if (PL_regnaughty >= 10) /* Probably an expensive pattern. */
+ r->reganch |= ROPT_NAUGHTY;
scan = r->program + 1; /* First BRANCH. */
/* XXXX To minimize changes to RE engine we always allocate
(OP(first) == BRANCH && OP(regnext(first)) != BRANCH) ||
(OP(first) == PLUS) ||
(OP(first) == MINMOD) ||
- (regkind[(U8)OP(first)] == CURLY && ARG1(first) > 0) ) {
+ (PL_regkind[(U8)OP(first)] == CURLY && ARG1(first) > 0) ) {
if (OP(first) == PLUS)
sawplus = 1;
else
/* Starting-point info. */
again:
if (OP(first) == EXACT); /* Empty, get anchored substr later. */
- else if (strchr(simple+2,OP(first)))
+ else if (strchr(PL_simple+4,OP(first)))
r->regstclass = first;
- else if (regkind[(U8)OP(first)] == BOUND ||
- regkind[(U8)OP(first)] == NBOUND)
+ else if (PL_regkind[(U8)OP(first)] == BOUND ||
+ PL_regkind[(U8)OP(first)] == NBOUND)
r->regstclass = first;
- else if (regkind[(U8)OP(first)] == BOL) {
+ else if (PL_regkind[(U8)OP(first)] == BOL) {
r->reganch |= (OP(first) == MBOL ? ROPT_ANCH_MBOL: ROPT_ANCH_BOL);
first = NEXTOPER(first);
goto again;
goto again;
}
else if ((OP(first) == STAR &&
- regkind[(U8)OP(NEXTOPER(first))] == ANY) &&
+ PL_regkind[(U8)OP(NEXTOPER(first))] == REG_ANY) &&
!(r->reganch & ROPT_ANCH) )
{
/* turn .* into ^.* with an implied $*=1 */
first = NEXTOPER(first);
goto again;
}
- if (sawplus && (!sawopen || !regsawback))
+ if (sawplus && (!sawopen || !PL_regsawback))
r->reganch |= ROPT_SKIP; /* x+ must match 1st of run */
/* Scan is after the zeroth branch, first is atomic matcher. */
data.longest = &(data.longest_fixed);
first = scan;
- minlen = study_chunk(&first, &fake, scan + regsize, /* Up to end */
+ minlen = study_chunk(&first, &fake, scan + PL_regsize, /* Up to end */
&data, SCF_DO_SUBSTR);
- if ( regnpar == 1 && data.longest == &(data.longest_fixed)
+ if ( PL_regnpar == 1 && data.longest == &(data.longest_fixed)
&& data.last_start_min == 0 && data.last_end > 0
- && !seen_zerolen
- && (!(regseen & REG_SEEN_GPOS) || (r->reganch & ROPT_ANCH_GPOS)))
+ && !PL_seen_zerolen
+ && (!(PL_regseen & REG_SEEN_GPOS) || (r->reganch & ROPT_ANCH_GPOS)))
r->reganch |= ROPT_CHECK_ALL;
scan_commit(&data);
SvREFCNT_dec(data.last_found);
- longest_float_length = SvCUR(data.longest_float);
+ longest_float_length = CHR_SVLEN(data.longest_float);
if (longest_float_length
|| (data.flags & SF_FL_BEFORE_EOL
&& (!(data.flags & SF_FL_BEFORE_MEOL)
- || (regflags & PMf_MULTILINE)))) {
- if (SvCUR(data.longest_fixed)
- && data.offset_fixed == data.offset_float_min)
- goto remove; /* Like in (a)+. */
-
+ || (PL_regflags & PMf_MULTILINE)))) {
+ if (SvCUR(data.longest_fixed) /* ok to leave SvCUR */
+ && data.offset_fixed == data.offset_float_min
+ && SvCUR(data.longest_fixed) == SvCUR(data.longest_float))
+ goto remove_float; /* As in (a)+. */
+
r->float_substr = data.longest_float;
r->float_min_offset = data.offset_float_min;
r->float_max_offset = data.offset_float_max;
BmUSEFUL(r->float_substr) = 100;
if (data.flags & SF_FL_BEFORE_EOL /* Cannot have SEOL and MULTI */
&& (!(data.flags & SF_FL_BEFORE_MEOL)
- || (regflags & PMf_MULTILINE)))
+ || (PL_regflags & PMf_MULTILINE)))
SvTAIL_on(r->float_substr);
- } else {
- remove:
+ }
+ else {
+ remove_float:
r->float_substr = Nullsv;
SvREFCNT_dec(data.longest_float);
longest_float_length = 0;
}
- longest_fixed_length = SvCUR(data.longest_fixed);
+ longest_fixed_length = CHR_SVLEN(data.longest_fixed);
if (longest_fixed_length
|| (data.flags & SF_FIX_BEFORE_EOL /* Cannot have SEOL and MULTI */
&& (!(data.flags & SF_FIX_BEFORE_MEOL)
- || (regflags & PMf_MULTILINE)))) {
+ || (PL_regflags & PMf_MULTILINE)))) {
r->anchored_substr = data.longest_fixed;
r->anchored_offset = data.offset_fixed;
fbm_compile(r->anchored_substr, 0);
BmUSEFUL(r->anchored_substr) = 100;
if (data.flags & SF_FIX_BEFORE_EOL /* Cannot have SEOL and MULTI */
&& (!(data.flags & SF_FIX_BEFORE_MEOL)
- || (regflags & PMf_MULTILINE)))
+ || (PL_regflags & PMf_MULTILINE)))
SvTAIL_on(r->anchored_substr);
- } else {
+ }
+ else {
r->anchored_substr = Nullsv;
SvREFCNT_dec(data.longest_fixed);
longest_fixed_length = 0;
r->check_offset_min = r->check_offset_max = r->anchored_offset;
if (r->reganch & ROPT_ANCH_SINGLE)
r->reganch |= ROPT_NOSCAN;
- } else {
+ }
+ else {
r->check_substr = r->float_substr;
r->check_offset_min = data.offset_float_min;
r->check_offset_max = data.offset_float_max;
}
- } else {
+ }
+ else {
/* Several toplevels. Best we can is to set minlen. */
I32 fake;
DEBUG_r(PerlIO_printf(Perl_debug_log, "\n"));
scan = r->program + 1;
- minlen = study_chunk(&scan, &fake, scan + regsize, NULL, 0);
+ minlen = study_chunk(&scan, &fake, scan + PL_regsize, NULL, 0);
r->check_substr = r->anchored_substr = r->float_substr = Nullsv;
}
- r->nparens = regnpar - 1;
r->minlen = minlen;
- if (regseen & REG_SEEN_GPOS)
+ if (PL_regseen & REG_SEEN_GPOS)
r->reganch |= ROPT_GPOS_SEEN;
- if (regseen & REG_SEEN_LOOKBEHIND)
+ if (PL_regseen & REG_SEEN_LOOKBEHIND)
r->reganch |= ROPT_LOOKBEHIND_SEEN;
- Newz(1002, r->startp, regnpar, char*);
- Newz(1002, r->endp, regnpar, char*);
+ if (PL_regseen & REG_SEEN_EVAL)
+ r->reganch |= ROPT_EVAL_SEEN;
+ Newz(1002, r->startp, PL_regnpar, char*);
+ Newz(1002, r->endp, PL_regnpar, char*);
DEBUG_r(regdump(r));
return(r);
}
reg(I32 paren, I32 *flagp)
/* paren: Parenthesized? 0=top, 1=(, inside: changed to letter. */
{
+ dTHR;
register regnode *ret; /* Will be the head of the group. */
register regnode *br;
register regnode *lastbr;
register regnode *ender = 0;
register I32 parno = 0;
- I32 flags, oregflags = regflags, have_branch = 0, open = 0;
+ I32 flags, oregflags = PL_regflags, have_branch = 0, open = 0;
char c;
- *flagp = HASWIDTH; /* Tentatively. */
+ *flagp = 0; /* Tentatively. */
/* Make an OPEN node, if parenthesized. */
if (paren) {
- if (*regparse == '?') {
- regparse++;
- paren = *regparse++;
+ if (*PL_regcomp_parse == '?') {
+ U16 posflags = 0, negflags = 0;
+ U16 *flagsp = &posflags;
+ int logical = 0;
+
+ PL_regcomp_parse++;
+ paren = *PL_regcomp_parse++;
ret = NULL; /* For look-ahead/behind. */
switch (paren) {
case '<':
-#ifndef REGALIGN_STRUCT
- FAIL("lookbehind non-implemented without REGALIGN_STRUCT");
-#endif
- regseen |= REG_SEEN_LOOKBEHIND;
- if (*regparse == '!')
+ PL_regseen |= REG_SEEN_LOOKBEHIND;
+ if (*PL_regcomp_parse == '!')
paren = ',';
- if (*regparse != '=' && *regparse != '!')
+ if (*PL_regcomp_parse != '=' && *PL_regcomp_parse != '!')
goto unknown;
- regparse++;
+ PL_regcomp_parse++;
case '=':
case '!':
- seen_zerolen++;
+ PL_seen_zerolen++;
case ':':
case '>':
break;
FAIL2("Sequence (?%c...) not implemented", (int)paren);
break;
case '#':
- while (*regparse && *regparse != ')')
- regparse++;
- if (*regparse != ')')
+ while (*PL_regcomp_parse && *PL_regcomp_parse != ')')
+ PL_regcomp_parse++;
+ if (*PL_regcomp_parse != ')')
FAIL("Sequence (?#... not terminated");
nextchar();
*flagp = TRYAGAIN;
return NULL;
+ case 'p':
+ logical = 1;
+ paren = *PL_regcomp_parse++;
+ /* FALL THROUGH */
case '{':
{
dTHR;
I32 count = 1, n = 0;
char c;
- char *s = regparse;
+ char *s = PL_regcomp_parse;
SV *sv;
OP_4tree *sop, *rop;
- seen_zerolen++;
- while (count && (c = *regparse)) {
- if (c == '\\' && regparse[1])
- regparse++;
+ PL_seen_zerolen++;
+ PL_regseen |= REG_SEEN_EVAL;
+ while (count && (c = *PL_regcomp_parse)) {
+ if (c == '\\' && PL_regcomp_parse[1])
+ PL_regcomp_parse++;
else if (c == '{')
count++;
else if (c == '}')
count--;
- regparse++;
+ PL_regcomp_parse++;
}
- if (*regparse != ')')
+ if (*PL_regcomp_parse != ')')
FAIL("Sequence (?{...}) not terminated or not {}-balanced");
if (!SIZE_ONLY) {
AV *av;
- if (regparse - 1 - s)
- sv = newSVpv(s, regparse - 1 - s);
+ if (PL_regcomp_parse - 1 - s)
+ sv = newSVpv(s, PL_regcomp_parse - 1 - s);
else
sv = newSVpv("", 0);
rop = sv_compile_2op(sv, &sop, "re", &av);
n = add_data(3, "nso");
- rx->data->data[n] = (void*)rop;
- rx->data->data[n+1] = (void*)av;
- rx->data->data[n+2] = (void*)sop;
+ PL_regcomp_rx->data->data[n] = (void*)rop;
+ PL_regcomp_rx->data->data[n+1] = (void*)av;
+ PL_regcomp_rx->data->data[n+2] = (void*)sop;
SvREFCNT_dec(sv);
- } else { /* First pass */
- if (tainted)
+ }
+ else { /* First pass */
+ if (PL_reginterp_cnt < ++PL_seen_evals
+ && PL_curcop != &PL_compiling)
+ /* No compiled RE interpolated, has runtime
+ components ===> unsafe. */
+ FAIL("Eval-group not allowed at runtime, use re 'eval'");
+ if (PL_tainted)
FAIL("Eval-group in insecure regular expression");
}
nextchar();
+ if (logical) {
+ ret = reg_node(LOGICAL);
+ if (!SIZE_ONLY)
+ ret->flags = 2;
+ regtail(ret, reganode(EVAL, n));
+ return ret;
+ }
return reganode(EVAL, n);
}
case '(':
{
- if (regparse[0] == '?') {
- if (regparse[1] == '=' || regparse[1] == '!'
- || regparse[1] == '<'
- || regparse[1] == '{') { /* Lookahead or eval. */
+ if (PL_regcomp_parse[0] == '?') {
+ if (PL_regcomp_parse[1] == '=' || PL_regcomp_parse[1] == '!'
+ || PL_regcomp_parse[1] == '<'
+ || PL_regcomp_parse[1] == '{') { /* Lookahead or eval. */
I32 flag;
ret = reg_node(LOGICAL);
+ if (!SIZE_ONLY)
+ ret->flags = 1;
regtail(ret, reg(1, &flag));
goto insert_if;
}
- } else if (regparse[0] >= '1' && regparse[0] <= '9' ) {
- parno = atoi(regparse++);
+ }
+ else if (PL_regcomp_parse[0] >= '1' && PL_regcomp_parse[0] <= '9' ) {
+ parno = atoi(PL_regcomp_parse++);
- while (isDIGIT(*regparse))
- regparse++;
+ while (isDIGIT(*PL_regcomp_parse))
+ PL_regcomp_parse++;
ret = reganode(GROUPP, parno);
if ((c = *nextchar()) != ')')
FAIL2("Switch (?(number%c not recognized", c);
else
regtail(br, reganode(LONGJMP, 0));
c = *nextchar();
+ if (flags&HASWIDTH)
+ *flagp |= HASWIDTH;
if (c == '|') {
lastbr = reganode(IFTHEN, 0); /* Fake one for optimizer. */
regbranch(&flags, 1);
regtail(ret, lastbr);
+ if (flags&HASWIDTH)
+ *flagp |= HASWIDTH;
c = *nextchar();
- } else
+ }
+ else
lastbr = NULL;
if (c != ')')
FAIL("Switch (?(condition)... contains too many branches");
if (lastbr) {
regtail(lastbr, ender);
regtail(NEXTOPER(NEXTOPER(lastbr)), ender);
- } else
+ }
+ else
regtail(ret, ender);
return ret;
- } else {
- FAIL2("Unknown condition for (?(%.2s", regparse);
+ }
+ else {
+ FAIL2("Unknown condition for (?(%.2s", PL_regcomp_parse);
}
}
case 0:
FAIL("Sequence (? incomplete");
break;
default:
- --regparse;
- while (*regparse && strchr("iogcmsx", *regparse)) {
- if (*regparse != 'o')
- pmflag(®flags, *regparse);
- ++regparse;
+ --PL_regcomp_parse;
+ parse_flags:
+ while (*PL_regcomp_parse && strchr("iogcmsx", *PL_regcomp_parse)) {
+ if (*PL_regcomp_parse != 'o')
+ pmflag(flagsp, *PL_regcomp_parse);
+ ++PL_regcomp_parse;
}
+ if (*PL_regcomp_parse == '-') {
+ flagsp = &negflags;
+ ++PL_regcomp_parse;
+ goto parse_flags;
+ }
+ PL_regflags |= posflags;
+ PL_regflags &= ~negflags;
+ if (*PL_regcomp_parse == ':') {
+ PL_regcomp_parse++;
+ paren = ':';
+ break;
+ }
unknown:
- if (*regparse != ')')
- FAIL2("Sequence (?%c...) not recognized", *regparse);
+ if (*PL_regcomp_parse != ')')
+ FAIL2("Sequence (?%c...) not recognized", *PL_regcomp_parse);
nextchar();
*flagp = TRYAGAIN;
return NULL;
}
}
else {
- parno = regnpar;
- regnpar++;
+ parno = PL_regnpar;
+ PL_regnpar++;
ret = reganode(OPEN, parno);
open = 1;
}
- } else
+ }
+ else
ret = NULL;
/* Pick up the branches, linking them together. */
br = regbranch(&flags, 1);
if (br == NULL)
return(NULL);
- if (*regparse == '|') {
- if (!SIZE_ONLY && extralen) {
+ if (*PL_regcomp_parse == '|') {
+ if (!SIZE_ONLY && PL_extralen) {
reginsert(BRANCHJ, br);
- } else
+ }
+ else
reginsert(BRANCH, br);
have_branch = 1;
if (SIZE_ONLY)
- extralen += 1; /* For BRANCHJ-BRANCH. */
- } else if (paren == ':') {
+ PL_extralen += 1; /* For BRANCHJ-BRANCH. */
+ }
+ else if (paren == ':') {
*flagp |= flags&SIMPLE;
}
if (open) { /* Starts with OPEN. */
regtail(ret, br); /* OPEN -> first. */
- } else if (paren != '?') /* Not Conditional */
+ }
+ else if (paren != '?') /* Not Conditional */
ret = br;
- if (!(flags&HASWIDTH))
- *flagp &= ~HASWIDTH;
+ if (flags&HASWIDTH)
+ *flagp |= HASWIDTH;
*flagp |= flags&SPSTART;
lastbr = br;
- while (*regparse == '|') {
- if (!SIZE_ONLY && extralen) {
+ while (*PL_regcomp_parse == '|') {
+ if (!SIZE_ONLY && PL_extralen) {
ender = reganode(LONGJMP,0);
regtail(NEXTOPER(NEXTOPER(lastbr)), ender); /* Append to the previous. */
}
if (SIZE_ONLY)
- extralen += 2; /* Account for LONGJMP. */
+ PL_extralen += 2; /* Account for LONGJMP. */
nextchar();
br = regbranch(&flags, 0);
if (br == NULL)
return(NULL);
regtail(lastbr, br); /* BRANCH -> BRANCH. */
lastbr = br;
- if (!(flags&HASWIDTH))
- *flagp &= ~HASWIDTH;
+ if (flags&HASWIDTH)
+ *flagp |= HASWIDTH;
*flagp |= flags&SPSTART;
}
ender = reganode(CLOSE, parno);
break;
case '<':
- case '>':
case ',':
case '=':
case '!':
- ender = reg_node(SUCCEED);
*flagp &= ~HASWIDTH;
+ /* FALL THROUGH */
+ case '>':
+ ender = reg_node(SUCCEED);
break;
case 0:
ender = reg_node(END);
if (paren == '>')
node = SUSPEND, flag = 0;
reginsert(node,ret);
-#ifdef REGALIGN_STRUCT
ret->flags = flag;
-#endif
regtail(ret, reg_node(TAIL));
}
}
/* Check for proper termination. */
- if (paren && (regparse >= regxend || *nextchar() != ')')) {
+ if (paren && (PL_regcomp_parse >= PL_regxend || *nextchar() != ')')) {
FAIL("unmatched () in regexp");
- } else if (!paren && regparse < regxend) {
- if (*regparse == ')') {
+ }
+ else if (!paren && PL_regcomp_parse < PL_regxend) {
+ if (*PL_regcomp_parse == ')') {
FAIL("unmatched () in regexp");
- } else
+ }
+ else
FAIL("junk on end of regexp"); /* "Can't happen". */
/* NOTREACHED */
}
if (paren != 0) {
- regflags = oregflags;
+ PL_regflags = oregflags;
}
return(ret);
STATIC regnode *
regbranch(I32 *flagp, I32 first)
{
+ dTHR;
register regnode *ret;
register regnode *chain = NULL;
register regnode *latest;
if (first)
ret = NULL;
else {
- if (!SIZE_ONLY && extralen)
+ if (!SIZE_ONLY && PL_extralen)
ret = reganode(BRANCHJ,0);
else
ret = reg_node(BRANCH);
}
if (!first && SIZE_ONLY)
- extralen += 1; /* BRANCHJ */
+ PL_extralen += 1; /* BRANCHJ */
*flagp = WORST; /* Tentatively. */
- regparse--;
+ PL_regcomp_parse--;
nextchar();
- while (regparse < regxend && *regparse != '|' && *regparse != ')') {
+ while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != '|' && *PL_regcomp_parse != ')') {
flags &= ~TRYAGAIN;
latest = regpiece(&flags);
if (latest == NULL) {
if (flags & TRYAGAIN)
continue;
return(NULL);
- } else if (ret == NULL)
+ }
+ else if (ret == NULL)
ret = latest;
*flagp |= flags&HASWIDTH;
if (chain == NULL) /* First piece. */
*flagp |= flags&SPSTART;
else {
- regnaughty++;
+ PL_regnaughty++;
regtail(chain, latest);
}
chain = latest;
STATIC regnode *
regpiece(I32 *flagp)
{
+ dTHR;
register regnode *ret;
register char op;
register char *next;
I32 flags;
- char *origparse = regparse;
+ char *origparse = PL_regcomp_parse;
char *maxpos;
I32 min;
I32 max = REG_INFTY;
return(NULL);
}
- op = *regparse;
+ op = *PL_regcomp_parse;
- if (op == '{' && regcurly(regparse)) {
- next = regparse + 1;
+ if (op == '{' && regcurly(PL_regcomp_parse)) {
+ next = PL_regcomp_parse + 1;
maxpos = Nullch;
while (isDIGIT(*next) || *next == ',') {
if (*next == ',') {
if (*next == '}') { /* got one */
if (!maxpos)
maxpos = next;
- regparse++;
- min = atoi(regparse);
+ PL_regcomp_parse++;
+ min = atoi(PL_regcomp_parse);
if (*maxpos == ',')
maxpos++;
else
- maxpos = regparse;
+ maxpos = PL_regcomp_parse;
max = atoi(maxpos);
if (!max && *maxpos != '0')
max = REG_INFTY; /* meaning "infinity" */
else if (max >= REG_INFTY)
FAIL2("Quantifier in {,} bigger than %d", REG_INFTY - 1);
- regparse = next;
+ PL_regcomp_parse = next;
nextchar();
do_curly:
if ((flags&SIMPLE)) {
- regnaughty += 2 + regnaughty / 2;
+ PL_regnaughty += 2 + PL_regnaughty / 2;
reginsert(CURLY, ret);
}
else {
- regnaughty += 4 + regnaughty; /* compound interest */
+ PL_regnaughty += 4 + PL_regnaughty; /* compound interest */
regtail(ret, reg_node(WHILEM));
- if (!SIZE_ONLY && extralen) {
+ if (!SIZE_ONLY && PL_extralen) {
reginsert(LONGJMP,ret);
reginsert(NOTHING,ret);
NEXT_OFF(ret) = 3; /* Go over LONGJMP. */
}
reginsert(CURLYX,ret);
- if (!SIZE_ONLY && extralen)
+ if (!SIZE_ONLY && PL_extralen)
NEXT_OFF(ret) = 3; /* Go over NOTHING to LONGJMP. */
regtail(ret, reg_node(NOTHING));
if (SIZE_ONLY)
- extralen += 3;
+ PL_extralen += 3;
}
-#ifdef REGALIGN_STRUCT
ret->flags = 0;
-#endif
if (min > 0)
- *flagp = (WORST|HASWIDTH);
+ *flagp = WORST;
+ if (max > 0)
+ *flagp |= HASWIDTH;
if (max && max < min)
FAIL("Can't do {n,m} with n > m");
if (!SIZE_ONLY) {
nextchar();
- *flagp = (op != '+') ? (WORST|SPSTART) : (WORST|HASWIDTH);
+ *flagp = (op != '+') ? (WORST|SPSTART|HASWIDTH) : (WORST|HASWIDTH);
if (op == '*' && (flags&SIMPLE)) {
reginsert(STAR, ret);
-#ifdef REGALIGN_STRUCT
ret->flags = 0;
-#endif
- regnaughty += 4;
+ PL_regnaughty += 4;
}
else if (op == '*') {
min = 0;
goto do_curly;
- } else if (op == '+' && (flags&SIMPLE)) {
+ }
+ else if (op == '+' && (flags&SIMPLE)) {
reginsert(PLUS, ret);
-#ifdef REGALIGN_STRUCT
ret->flags = 0;
-#endif
- regnaughty += 3;
+ PL_regnaughty += 3;
}
else if (op == '+') {
min = 1;
goto do_curly;
- } else if (op == '?') {
+ }
+ else if (op == '?') {
min = 0; max = 1;
goto do_curly;
}
nest_check:
- if (dowarn && !SIZE_ONLY && !(flags&HASWIDTH) && max > 10000) {
- warn("%.*s matches null string many times",
- regparse - origparse, origparse);
+ if (ckWARN(WARN_UNSAFE) && !SIZE_ONLY && !(flags&HASWIDTH) && max > REG_INFTY/3) {
+ warner(WARN_UNSAFE, "%.*s matches null string many times",
+ PL_regcomp_parse - origparse, origparse);
}
- if (*regparse == '?') {
+ if (*PL_regcomp_parse == '?') {
nextchar();
reginsert(MINMOD, ret);
-#ifdef REGALIGN
regtail(ret, ret + NODE_STEP_REGNODE);
-#else
- regtail(ret, ret + 3);
-#endif
}
- if (ISMULT2(regparse))
+ if (ISMULT2(PL_regcomp_parse))
FAIL("nested *?+ in regexp");
return(ret);
STATIC regnode *
regatom(I32 *flagp)
{
+ dTHR;
register regnode *ret = 0;
I32 flags;
*flagp = WORST; /* Tentatively. */
tryagain:
- switch (*regparse) {
+ switch (*PL_regcomp_parse) {
case '^':
- seen_zerolen++;
+ PL_seen_zerolen++;
nextchar();
- if (regflags & PMf_MULTILINE)
+ if (PL_regflags & PMf_MULTILINE)
ret = reg_node(MBOL);
- else if (regflags & PMf_SINGLELINE)
+ else if (PL_regflags & PMf_SINGLELINE)
ret = reg_node(SBOL);
else
ret = reg_node(BOL);
break;
case '$':
- if (regparse[1])
- seen_zerolen++;
+ if (PL_regcomp_parse[1])
+ PL_seen_zerolen++;
nextchar();
- if (regflags & PMf_MULTILINE)
+ if (PL_regflags & PMf_MULTILINE)
ret = reg_node(MEOL);
- else if (regflags & PMf_SINGLELINE)
+ else if (PL_regflags & PMf_SINGLELINE)
ret = reg_node(SEOL);
else
ret = reg_node(EOL);
break;
case '.':
nextchar();
- if (regflags & PMf_SINGLELINE)
- ret = reg_node(SANY);
- else
- ret = reg_node(ANY);
- regnaughty++;
- *flagp |= HASWIDTH|SIMPLE;
+ if (UTF) {
+ if (PL_regflags & PMf_SINGLELINE)
+ ret = reg_node(SANYUTF8);
+ else
+ ret = reg_node(ANYUTF8);
+ *flagp |= HASWIDTH;
+ }
+ else {
+ if (PL_regflags & PMf_SINGLELINE)
+ ret = reg_node(SANY);
+ else
+ ret = reg_node(REG_ANY);
+ *flagp |= HASWIDTH|SIMPLE;
+ }
+ PL_regnaughty++;
break;
case '[':
- regparse++;
- ret = regclass();
+ PL_regcomp_parse++;
+ ret = (UTF ? regclassutf8() : regclass());
+ if (*PL_regcomp_parse != ']')
+ FAIL("unmatched [] in regexp");
+ nextchar();
*flagp |= HASWIDTH|SIMPLE;
break;
case '(':
*flagp |= TRYAGAIN;
return NULL;
}
- FAIL2("internal urp in regexp at /%s/", regparse);
+ FAIL2("internal urp in regexp at /%s/", PL_regcomp_parse);
/* Supposed to be caught earlier. */
break;
case '{':
- if (!regcurly(regparse)) {
- regparse++;
+ if (!regcurly(PL_regcomp_parse)) {
+ PL_regcomp_parse++;
goto defchar;
}
/* FALL THROUGH */
FAIL("?+*{} follows nothing in regexp");
break;
case '\\':
- switch (*++regparse) {
+ switch (*++PL_regcomp_parse) {
case 'A':
- seen_zerolen++;
+ PL_seen_zerolen++;
ret = reg_node(SBOL);
*flagp |= SIMPLE;
nextchar();
break;
case 'G':
ret = reg_node(GPOS);
- regseen |= REG_SEEN_GPOS;
+ PL_regseen |= REG_SEEN_GPOS;
*flagp |= SIMPLE;
nextchar();
break;
*flagp |= SIMPLE;
nextchar();
break;
+ case 'z':
+ ret = reg_node(EOS);
+ *flagp |= SIMPLE;
+ PL_seen_zerolen++; /* Do not optimize RE away */
+ nextchar();
+ break;
+ case 'C':
+ ret = reg_node(SANY);
+ *flagp |= HASWIDTH|SIMPLE;
+ nextchar();
+ break;
+ case 'X':
+ ret = reg_node(CLUMP);
+ *flagp |= HASWIDTH;
+ nextchar();
+ if (UTF && !PL_utf8_mark)
+ is_utf8_mark((U8*)"~"); /* preload table */
+ break;
case 'w':
- ret = reg_node((regflags & PMf_LOCALE) ? ALNUML : ALNUM);
+ ret = reg_node(
+ UTF
+ ? (LOC ? ALNUMLUTF8 : ALNUMUTF8)
+ : (LOC ? ALNUML : ALNUM));
*flagp |= HASWIDTH|SIMPLE;
nextchar();
+ if (UTF && !PL_utf8_alnum)
+ is_utf8_alnum((U8*)"a"); /* preload table */
break;
case 'W':
- ret = reg_node((regflags & PMf_LOCALE) ? NALNUML : NALNUM);
+ ret = reg_node(
+ UTF
+ ? (LOC ? NALNUMLUTF8 : NALNUMUTF8)
+ : (LOC ? NALNUML : NALNUM));
*flagp |= HASWIDTH|SIMPLE;
nextchar();
+ if (UTF && !PL_utf8_alnum)
+ is_utf8_alnum((U8*)"a"); /* preload table */
break;
case 'b':
- seen_zerolen++;
- ret = reg_node((regflags & PMf_LOCALE) ? BOUNDL : BOUND);
+ PL_seen_zerolen++;
+ ret = reg_node(
+ UTF
+ ? (LOC ? BOUNDLUTF8 : BOUNDUTF8)
+ : (LOC ? BOUNDL : BOUND));
*flagp |= SIMPLE;
nextchar();
+ if (UTF && !PL_utf8_alnum)
+ is_utf8_alnum((U8*)"a"); /* preload table */
break;
case 'B':
- seen_zerolen++;
- ret = reg_node((regflags & PMf_LOCALE) ? NBOUNDL : NBOUND);
+ PL_seen_zerolen++;
+ ret = reg_node(
+ UTF
+ ? (LOC ? NBOUNDLUTF8 : NBOUNDUTF8)
+ : (LOC ? NBOUNDL : NBOUND));
*flagp |= SIMPLE;
nextchar();
+ if (UTF && !PL_utf8_alnum)
+ is_utf8_alnum((U8*)"a"); /* preload table */
break;
case 's':
- ret = reg_node((regflags & PMf_LOCALE) ? SPACEL : SPACE);
+ ret = reg_node(
+ UTF
+ ? (LOC ? SPACELUTF8 : SPACEUTF8)
+ : (LOC ? SPACEL : SPACE));
*flagp |= HASWIDTH|SIMPLE;
nextchar();
+ if (UTF && !PL_utf8_space)
+ is_utf8_space((U8*)" "); /* preload table */
break;
case 'S':
- ret = reg_node((regflags & PMf_LOCALE) ? NSPACEL : NSPACE);
+ ret = reg_node(
+ UTF
+ ? (LOC ? NSPACELUTF8 : NSPACEUTF8)
+ : (LOC ? NSPACEL : NSPACE));
*flagp |= HASWIDTH|SIMPLE;
nextchar();
+ if (UTF && !PL_utf8_space)
+ is_utf8_space((U8*)" "); /* preload table */
break;
case 'd':
- ret = reg_node(DIGIT);
+ ret = reg_node(UTF ? DIGITUTF8 : DIGIT);
*flagp |= HASWIDTH|SIMPLE;
nextchar();
+ if (UTF && !PL_utf8_digit)
+ is_utf8_digit((U8*)"1"); /* preload table */
break;
case 'D':
- ret = reg_node(NDIGIT);
+ ret = reg_node(UTF ? NDIGITUTF8 : NDIGIT);
*flagp |= HASWIDTH|SIMPLE;
nextchar();
+ if (UTF && !PL_utf8_digit)
+ is_utf8_digit((U8*)"1"); /* preload table */
+ break;
+ case 'p':
+ case 'P':
+ { /* a lovely hack--pretend we saw [\pX] instead */
+ char* oldregxend = PL_regxend;
+
+ if (PL_regcomp_parse[1] == '{') {
+ PL_regxend = strchr(PL_regcomp_parse, '}');
+ if (!PL_regxend)
+ FAIL("Missing right brace on \\p{}");
+ PL_regxend++;
+ }
+ else
+ PL_regxend = PL_regcomp_parse + 2;
+ PL_regcomp_parse--;
+
+ ret = regclassutf8();
+
+ PL_regxend = oldregxend;
+ PL_regcomp_parse--;
+ nextchar();
+ *flagp |= HASWIDTH|SIMPLE;
+ }
break;
case 'n':
case 'r':
case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
{
- I32 num = atoi(regparse);
+ I32 num = atoi(PL_regcomp_parse);
- if (num > 9 && num >= regnpar)
+ if (num > 9 && num >= PL_regnpar)
goto defchar;
else {
- regsawback = 1;
- ret = reganode((regflags & PMf_FOLD)
- ? ((regflags & PMf_LOCALE) ? REFFL : REFF)
+ if (!SIZE_ONLY && num > PL_regcomp_rx->nparens)
+ FAIL("reference to nonexistent group");
+ PL_regsawback = 1;
+ ret = reganode(FOLD
+ ? (LOC ? REFFL : REFF)
: REF, num);
*flagp |= HASWIDTH;
- while (isDIGIT(*regparse))
- regparse++;
- regparse--;
+ while (isDIGIT(*PL_regcomp_parse))
+ PL_regcomp_parse++;
+ PL_regcomp_parse--;
nextchar();
}
}
break;
case '\0':
- if (regparse >= regxend)
+ if (PL_regcomp_parse >= PL_regxend)
FAIL("trailing \\ in regexp");
/* FALL THROUGH */
default:
break;
case '#':
- if (regflags & PMf_EXTENDED) {
- while (regparse < regxend && *regparse != '\n') regparse++;
- if (regparse < regxend)
+ if (PL_regflags & PMf_EXTENDED) {
+ while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != '\n') PL_regcomp_parse++;
+ if (PL_regcomp_parse < PL_regxend)
goto tryagain;
}
/* FALL THROUGH */
default: {
register I32 len;
- register U8 ender;
+ register UV ender;
register char *p;
char *oldp, *s;
I32 numlen;
- regparse++;
+ PL_regcomp_parse++;
defchar:
- ret = reg_node((regflags & PMf_FOLD)
- ? ((regflags & PMf_LOCALE) ? EXACTFL : EXACTF)
+ ret = reg_node(FOLD
+ ? (LOC ? EXACTFL : EXACTF)
: EXACT);
s = (char *) OPERAND(ret);
regc(0, s++); /* save spot for len */
- for (len = 0, p = regparse - 1;
- len < 127 && p < regxend;
+ for (len = 0, p = PL_regcomp_parse - 1;
+ len < 127 && p < PL_regxend;
len++)
{
oldp = p;
- if (regflags & PMf_EXTENDED)
- p = regwhite(p, regxend);
+ if (PL_regflags & PMf_EXTENDED)
+ p = regwhite(p, PL_regxend);
switch (*p) {
case '^':
case '$':
case 'A':
case 'G':
case 'Z':
+ case 'z':
case 'w':
case 'W':
case 'b':
case 'S':
case 'd':
case 'D':
+ case 'p':
+ case 'P':
--p;
goto loopdone;
case 'n':
p++;
break;
case 'x':
- ender = scan_hex(++p, 2, &numlen);
- p += numlen;
+ if (*++p == '{') {
+ char* e = strchr(p, '}');
+
+ if (!e)
+ FAIL("Missing right brace on \\x{}");
+ else if (UTF) {
+ ender = scan_hex(p + 1, e - p, &numlen);
+ if (numlen + len >= 127) { /* numlen is generous */
+ p--;
+ goto loopdone;
+ }
+ p = e + 1;
+ }
+ else
+ FAIL("Can't use \\x{} without 'use utf8' declaration");
+ }
+ else {
+ ender = scan_hex(p, 2, &numlen);
+ p += numlen;
+ }
break;
case 'c':
p++;
case '0': case '1': case '2': case '3':case '4':
case '5': case '6': case '7': case '8':case '9':
if (*p == '0' ||
- (isDIGIT(p[1]) && atoi(p) >= regnpar) ) {
+ (isDIGIT(p[1]) && atoi(p) >= PL_regnpar) ) {
ender = scan_oct(p, 3, &numlen);
p += numlen;
}
}
break;
case '\0':
- if (p >= regxend)
+ if (p >= PL_regxend)
FAIL("trailing \\ in regexp");
/* FALL THROUGH */
default:
- ender = *p++;
- break;
+ goto normal_default;
}
break;
default:
- ender = *p++;
+ normal_default:
+ if ((*p & 0xc0) == 0xc0 && UTF) {
+ ender = utf8_to_uv((U8*)p, &numlen);
+ p += numlen;
+ }
+ else
+ ender = *p++;
break;
}
- if (regflags & PMf_EXTENDED)
- p = regwhite(p, regxend);
+ if (PL_regflags & PMf_EXTENDED)
+ p = regwhite(p, PL_regxend);
+ if (UTF && FOLD) {
+ if (LOC)
+ ender = toLOWER_LC_uni(ender);
+ else
+ ender = toLOWER_uni(ender);
+ }
if (ISMULT2(p)) { /* Back off on ?+*. */
if (len)
p = oldp;
+ else if (ender >= 0x80 && UTF) {
+ reguni(ender, s, &numlen);
+ s += numlen;
+ len += numlen;
+ }
else {
len++;
regc(ender, s++);
}
break;
}
- regc(ender, s++);
+ if (ender >= 0x80 && UTF) {
+ reguni(ender, s, &numlen);
+ s += numlen;
+ len += numlen - 1;
+ }
+ else
+ regc(ender, s++);
}
loopdone:
- regparse = p - 1;
+ PL_regcomp_parse = p - 1;
nextchar();
if (len < 0)
FAIL("internal disaster in regexp");
*OPERAND(ret) = len;
regc('\0', s++);
if (SIZE_ONLY) {
-#ifdef REGALIGN_STRUCT
- regsize += (len + 2 + sizeof(regnode) - 1) / sizeof(regnode);
-#endif
- } else {
- regcode += (len + 2 + sizeof(regnode) - 1) / sizeof(regnode);
+ PL_regsize += (len + 2 + sizeof(regnode) - 1) / sizeof(regnode);
+ }
+ else {
+ PL_regcode += (len + 2 + sizeof(regnode) - 1) / sizeof(regnode);
}
}
break;
return p;
}
+/* parse POSIX character classes like [[:foo:]] */
+STATIC char*
+regpposixcc(I32 value)
+{
+ char *posixcc = 0;
+
+ if (value == '[' && PL_regcomp_parse + 1 < PL_regxend &&
+ /* I smell either [: or [= or [. -- POSIX has been here, right? */
+ (*PL_regcomp_parse == ':' ||
+ *PL_regcomp_parse == '=' ||
+ *PL_regcomp_parse == '.')) {
+ char c = *PL_regcomp_parse;
+ char* s = PL_regcomp_parse++;
+
+ while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != c)
+ PL_regcomp_parse++;
+ if (PL_regcomp_parse == PL_regxend)
+ /* Grandfather lone [:, [=, [. */
+ PL_regcomp_parse = s;
+ else {
+ PL_regcomp_parse++; /* skip over the c */
+ if (*PL_regcomp_parse == ']') {
+ /* Not Implemented Yet.
+ * (POSIX Extended Character Classes, that is)
+ * The text between e.g. [: and :] would start
+ * at s + 1 and stop at regcomp_parse - 2. */
+ if (ckWARN(WARN_UNSAFE) && !SIZE_ONLY)
+ warner(WARN_UNSAFE,
+ "Character class syntax [%c %c] is reserved for future extensions", c, c);
+ PL_regcomp_parse++; /* skip over the ending ] */
+ posixcc = s + 1;
+ }
+ }
+ }
+
+ return posixcc;
+}
+
STATIC regnode *
regclass(void)
{
+ dTHR;
register char *opnd, *s;
- register I32 Class;
- register I32 lastclass = 1234;
+ register I32 value;
+ register I32 lastvalue = 1234;
register I32 range = 0;
register regnode *ret;
register I32 def;
I32 numlen;
- s = opnd = (char *) OPERAND(regcode);
+ s = opnd = (char *) OPERAND(PL_regcode);
ret = reg_node(ANYOF);
- for (Class = 0; Class < 33; Class++)
+ for (value = 0; value < 33; value++)
regc(0, s++);
- if (*regparse == '^') { /* Complement of range. */
- regnaughty++;
- regparse++;
+ if (*PL_regcomp_parse == '^') { /* Complement of range. */
+ PL_regnaughty++;
+ PL_regcomp_parse++;
if (!SIZE_ONLY)
*opnd |= ANYOF_INVERT;
}
if (!SIZE_ONLY) {
- regcode += ANY_SKIP;
- if (regflags & PMf_FOLD)
+ PL_regcode += ANY_SKIP;
+ if (FOLD)
*opnd |= ANYOF_FOLD;
- if (regflags & PMf_LOCALE)
+ if (LOC)
*opnd |= ANYOF_LOCALE;
- } else {
- regsize += ANY_SKIP;
}
- if (*regparse == ']' || *regparse == '-')
+ else {
+ PL_regsize += ANY_SKIP;
+ }
+ if (*PL_regcomp_parse == ']' || *PL_regcomp_parse == '-')
goto skipcond; /* allow 1st char to be ] or - */
- while (regparse < regxend && *regparse != ']') {
+ while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != ']') {
skipcond:
- Class = UCHARAT(regparse++);
- if (Class == '[' && regparse + 1 < regxend &&
- /* I smell either [: or [= or [. -- POSIX has been here, right? */
- (*regparse == ':' || *regparse == '=' || *regparse == '.')) {
- char posixccc = *regparse;
- char* posixccs = regparse++;
-
- while (regparse < regxend && *regparse != posixccc)
- regparse++;
- if (regparse == regxend)
- /* Grandfather lone [:, [=, [. */
- regparse = posixccs;
- else {
- regparse++; /* skip over the posixccc */
- if (*regparse == ']') {
- /* Not Implemented Yet.
- * (POSIX Extended Character Classes, that is)
- * The text between e.g. [: and :] would start
- * at posixccs + 1 and stop at regparse - 2. */
- if (dowarn && !SIZE_ONLY)
- warn("Character class syntax [%c %c] is reserved for future extensions", posixccc, posixccc);
- regparse++; /* skip over the ending ] */
- }
- }
- }
- if (Class == '\\') {
- Class = UCHARAT(regparse++);
- switch (Class) {
+ value = UCHARAT(PL_regcomp_parse++);
+ if (value == '[')
+ (void)regpposixcc(value); /* ignore the return value for now */
+ else if (value == '\\') {
+ value = UCHARAT(PL_regcomp_parse++);
+ switch (value) {
case 'w':
if (!SIZE_ONLY) {
- if (regflags & PMf_LOCALE)
+ if (LOC)
*opnd |= ANYOF_ALNUML;
else {
- for (Class = 0; Class < 256; Class++)
- if (isALNUM(Class))
- ANYOF_SET(opnd, Class);
+ for (value = 0; value < 256; value++)
+ if (isALNUM(value))
+ ANYOF_SET(opnd, value);
}
}
- lastclass = 1234;
+ lastvalue = 1234;
continue;
case 'W':
if (!SIZE_ONLY) {
- if (regflags & PMf_LOCALE)
+ if (LOC)
*opnd |= ANYOF_NALNUML;
else {
- for (Class = 0; Class < 256; Class++)
- if (!isALNUM(Class))
- ANYOF_SET(opnd, Class);
+ for (value = 0; value < 256; value++)
+ if (!isALNUM(value))
+ ANYOF_SET(opnd, value);
}
}
- lastclass = 1234;
+ lastvalue = 1234;
continue;
case 's':
if (!SIZE_ONLY) {
- if (regflags & PMf_LOCALE)
+ if (LOC)
*opnd |= ANYOF_SPACEL;
else {
- for (Class = 0; Class < 256; Class++)
- if (isSPACE(Class))
- ANYOF_SET(opnd, Class);
+ for (value = 0; value < 256; value++)
+ if (isSPACE(value))
+ ANYOF_SET(opnd, value);
}
}
- lastclass = 1234;
+ lastvalue = 1234;
continue;
case 'S':
if (!SIZE_ONLY) {
- if (regflags & PMf_LOCALE)
+ if (LOC)
*opnd |= ANYOF_NSPACEL;
else {
- for (Class = 0; Class < 256; Class++)
- if (!isSPACE(Class))
- ANYOF_SET(opnd, Class);
+ for (value = 0; value < 256; value++)
+ if (!isSPACE(value))
+ ANYOF_SET(opnd, value);
}
}
- lastclass = 1234;
+ lastvalue = 1234;
continue;
case 'd':
if (!SIZE_ONLY) {
- for (Class = '0'; Class <= '9'; Class++)
- ANYOF_SET(opnd, Class);
+ for (value = '0'; value <= '9'; value++)
+ ANYOF_SET(opnd, value);
}
- lastclass = 1234;
+ lastvalue = 1234;
continue;
case 'D':
if (!SIZE_ONLY) {
- for (Class = 0; Class < '0'; Class++)
- ANYOF_SET(opnd, Class);
- for (Class = '9' + 1; Class < 256; Class++)
- ANYOF_SET(opnd, Class);
+ for (value = 0; value < '0'; value++)
+ ANYOF_SET(opnd, value);
+ for (value = '9' + 1; value < 256; value++)
+ ANYOF_SET(opnd, value);
}
- lastclass = 1234;
+ lastvalue = 1234;
continue;
case 'n':
- Class = '\n';
+ value = '\n';
break;
case 'r':
- Class = '\r';
+ value = '\r';
break;
case 't':
- Class = '\t';
+ value = '\t';
break;
case 'f':
- Class = '\f';
+ value = '\f';
break;
case 'b':
- Class = '\b';
+ value = '\b';
break;
case 'e':
- Class = '\033';
+ value = '\033';
break;
case 'a':
- Class = '\007';
+ value = '\007';
break;
case 'x':
- Class = scan_hex(regparse, 2, &numlen);
- regparse += numlen;
+ value = scan_hex(PL_regcomp_parse, 2, &numlen);
+ PL_regcomp_parse += numlen;
break;
case 'c':
- Class = UCHARAT(regparse++);
- Class = toCTRL(Class);
+ value = UCHARAT(PL_regcomp_parse++);
+ value = toCTRL(value);
break;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
- Class = scan_oct(--regparse, 3, &numlen);
- regparse += numlen;
+ value = scan_oct(--PL_regcomp_parse, 3, &numlen);
+ PL_regcomp_parse += numlen;
break;
}
}
if (range) {
- if (lastclass > Class)
+ if (lastvalue > value)
FAIL("invalid [] range in regexp");
range = 0;
}
else {
- lastclass = Class;
- if (*regparse == '-' && regparse+1 < regxend &&
- regparse[1] != ']') {
- regparse++;
+ lastvalue = value;
+ if (*PL_regcomp_parse == '-' && PL_regcomp_parse+1 < PL_regxend &&
+ PL_regcomp_parse[1] != ']') {
+ PL_regcomp_parse++;
range = 1;
continue; /* do it next time */
}
}
if (!SIZE_ONLY) {
- for ( ; lastclass <= Class; lastclass++)
- ANYOF_SET(opnd, lastclass);
- }
- lastclass = Class;
+#ifndef ASCIIish
+ if ((isLOWER(lastvalue) && isLOWER(value)) ||
+ (isUPPER(lastvalue) && isUPPER(value)))
+ {
+ I32 i;
+ if (isLOWER(lastvalue)) {
+ for (i = lastvalue; i <= value; i++)
+ if (isLOWER(i))
+ ANYOF_SET(opnd, i);
+ } else {
+ for (i = lastvalue; i <= value; i++)
+ if (isUPPER(i))
+ ANYOF_SET(opnd, i);
+ }
+ }
+ else
+#endif
+ for ( ; lastvalue <= value; lastvalue++)
+ ANYOF_SET(opnd, lastvalue);
+ }
+ lastvalue = value;
}
- if (*regparse != ']')
- FAIL("unmatched [] in regexp");
- nextchar();
/* optimize case-insensitive simple patterns (e.g. /[a-z]/i) */
if (!SIZE_ONLY && (*opnd & (0xFF ^ ANYOF_INVERT)) == ANYOF_FOLD) {
- for (Class = 0; Class < 256; ++Class) {
- if (ANYOF_TEST(opnd, Class)) {
- I32 cf = fold[Class];
+ for (value = 0; value < 256; ++value) {
+ if (ANYOF_TEST(opnd, value)) {
+ I32 cf = PL_fold[value];
ANYOF_SET(opnd, cf);
}
}
}
/* optimize inverted simple patterns (e.g. [^a-z]) */
if (!SIZE_ONLY && (*opnd & 0xFF) == ANYOF_INVERT) {
- for (Class = 0; Class < 32; ++Class)
- opnd[1 + Class] ^= 0xFF;
+ for (value = 0; value < 32; ++value)
+ opnd[1 + value] ^= 0xFF;
*opnd = 0;
}
return ret;
}
+STATIC regnode *
+regclassutf8(void)
+{
+ register char *opnd, *e;
+ register U32 value;
+ register U32 lastvalue = 123456;
+ register I32 range = 0;
+ register regnode *ret;
+ I32 numlen;
+ I32 n;
+ SV *listsv;
+ U8 flags = 0;
+ dTHR;
+
+ if (*PL_regcomp_parse == '^') { /* Complement of range. */
+ PL_regnaughty++;
+ PL_regcomp_parse++;
+ if (!SIZE_ONLY)
+ flags |= ANYOF_INVERT;
+ }
+ if (!SIZE_ONLY) {
+ if (FOLD)
+ flags |= ANYOF_FOLD;
+ if (LOC)
+ flags |= ANYOF_LOCALE;
+ listsv = newSVpv("# comment\n",0);
+ }
+
+ if (*PL_regcomp_parse == ']' || *PL_regcomp_parse == '-')
+ goto skipcond; /* allow 1st char to be ] or - */
+
+ while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != ']') {
+ skipcond:
+ value = utf8_to_uv((U8*)PL_regcomp_parse, &numlen);
+ PL_regcomp_parse += numlen;
+
+ if (value == '[')
+ (void)regpposixcc(value); /* ignore the return value for now */
+ else if (value == '\\') {
+ value = utf8_to_uv((U8*)PL_regcomp_parse, &numlen);
+ PL_regcomp_parse += numlen;
+ switch (value) {
+ case 'w':
+ if (!SIZE_ONLY) {
+ if (LOC)
+ flags |= ANYOF_ALNUML;
+
+ sv_catpvf(listsv, "+utf8::IsAlnum\n");
+ }
+ lastvalue = 123456;
+ continue;
+ case 'W':
+ if (!SIZE_ONLY) {
+ if (LOC)
+ flags |= ANYOF_NALNUML;
+
+ sv_catpvf(listsv,
+ "-utf8::IsAlpha\n-utf8::IsDigit\n0000\t%04x\n%04x\tffff\n",
+ '_' - 1,
+ '_' + 1);
+ }
+ lastvalue = 123456;
+ continue;
+ case 's':
+ if (!SIZE_ONLY) {
+ if (LOC)
+ flags |= ANYOF_SPACEL;
+ sv_catpvf(listsv, "+utf8::IsSpace\n");
+ if (!PL_utf8_space)
+ is_utf8_space((U8*)" ");
+ }
+ lastvalue = 123456;
+ continue;
+ case 'S':
+ if (!SIZE_ONLY) {
+ if (LOC)
+ flags |= ANYOF_NSPACEL;
+ sv_catpvf(listsv,
+ "!utf8::IsSpace\n");
+ if (!PL_utf8_space)
+ is_utf8_space((U8*)" ");
+ }
+ lastvalue = 123456;
+ continue;
+ case 'd':
+ if (!SIZE_ONLY) {
+ sv_catpvf(listsv, "+utf8::IsDigit\n");
+ }
+ lastvalue = 123456;
+ continue;
+ case 'D':
+ if (!SIZE_ONLY) {
+ sv_catpvf(listsv,
+ "!utf8::IsDigit\n");
+ }
+ lastvalue = 123456;
+ continue;
+ case 'p':
+ case 'P':
+ if (*PL_regcomp_parse == '{') {
+ e = strchr(PL_regcomp_parse++, '}');
+ if (!e)
+ FAIL("Missing right brace on \\p{}");
+ n = e - PL_regcomp_parse;
+ }
+ else {
+ e = PL_regcomp_parse;
+ n = 1;
+ }
+ if (!SIZE_ONLY) {
+ if (value == 'p')
+ sv_catpvf(listsv, "+utf8::%.*s\n", n, PL_regcomp_parse);
+ else
+ sv_catpvf(listsv,
+ "!utf8::%.*s\n", n, PL_regcomp_parse);
+ }
+ PL_regcomp_parse = e + 1;
+ lastvalue = 123456;
+ continue;
+ case 'n':
+ value = '\n';
+ break;
+ case 'r':
+ value = '\r';
+ break;
+ case 't':
+ value = '\t';
+ break;
+ case 'f':
+ value = '\f';
+ break;
+ case 'b':
+ value = '\b';
+ break;
+ case 'e':
+ value = '\033';
+ break;
+ case 'a':
+ value = '\007';
+ break;
+ case 'x':
+ if (*PL_regcomp_parse == '{') {
+ e = strchr(PL_regcomp_parse++, '}');
+ if (!e)
+ FAIL("Missing right brace on \\x{}");
+ value = scan_hex(PL_regcomp_parse + 1, e - PL_regcomp_parse, &numlen);
+ PL_regcomp_parse = e + 1;
+ }
+ else {
+ value = scan_hex(PL_regcomp_parse, 2, &numlen);
+ PL_regcomp_parse += numlen;
+ }
+ break;
+ case 'c':
+ value = UCHARAT(PL_regcomp_parse++);
+ value = toCTRL(value);
+ break;
+ case '0': case '1': case '2': case '3': case '4':
+ case '5': case '6': case '7': case '8': case '9':
+ value = scan_oct(--PL_regcomp_parse, 3, &numlen);
+ PL_regcomp_parse += numlen;
+ break;
+ }
+ }
+ if (range) {
+ if (lastvalue > value)
+ FAIL("invalid [] range in regexp");
+ if (!SIZE_ONLY)
+ sv_catpvf(listsv, "%04x\t%04x\n", lastvalue, value);
+ lastvalue = value;
+ range = 0;
+ }
+ else {
+ lastvalue = value;
+ if (*PL_regcomp_parse == '-' && PL_regcomp_parse+1 < PL_regxend &&
+ PL_regcomp_parse[1] != ']') {
+ PL_regcomp_parse++;
+ range = 1;
+ continue; /* do it next time */
+ }
+ if (!SIZE_ONLY)
+ sv_catpvf(listsv, "%04x\n", value);
+ }
+ }
+
+ ret = reganode(ANYOFUTF8, 0);
+
+ if (!SIZE_ONLY) {
+ SV *rv = swash_init("utf8", "", listsv, 1, 0);
+ SvREFCNT_dec(listsv);
+ n = add_data(1,"s");
+ PL_regcomp_rx->data->data[n] = (void*)rv;
+ ARG1_SET(ret, flags);
+ ARG2_SET(ret, n);
+ }
+
+ return ret;
+}
+
STATIC char*
nextchar(void)
{
- char* retval = regparse++;
+ dTHR;
+ char* retval = PL_regcomp_parse++;
for (;;) {
- if (*regparse == '(' && regparse[1] == '?' &&
- regparse[2] == '#') {
- while (*regparse && *regparse != ')')
- regparse++;
- regparse++;
+ if (*PL_regcomp_parse == '(' && PL_regcomp_parse[1] == '?' &&
+ PL_regcomp_parse[2] == '#') {
+ while (*PL_regcomp_parse && *PL_regcomp_parse != ')')
+ PL_regcomp_parse++;
+ PL_regcomp_parse++;
continue;
}
- if (regflags & PMf_EXTENDED) {
- if (isSPACE(*regparse)) {
- regparse++;
+ if (PL_regflags & PMf_EXTENDED) {
+ if (isSPACE(*PL_regcomp_parse)) {
+ PL_regcomp_parse++;
continue;
}
- else if (*regparse == '#') {
- while (*regparse && *regparse != '\n')
- regparse++;
- regparse++;
+ else if (*PL_regcomp_parse == '#') {
+ while (*PL_regcomp_parse && *PL_regcomp_parse != '\n')
+ PL_regcomp_parse++;
+ PL_regcomp_parse++;
continue;
}
}
STATIC regnode * /* Location. */
reg_node(U8 op)
{
+ dTHR;
register regnode *ret;
register regnode *ptr;
- ret = regcode;
+ ret = PL_regcode;
if (SIZE_ONLY) {
- SIZE_ALIGN(regsize);
-#ifdef REGALIGN_STRUCT
- regsize += 1;
-#else
- regsize += 3;
-#endif
+ SIZE_ALIGN(PL_regsize);
+ PL_regsize += 1;
return(ret);
}
NODE_ALIGN_FILL(ret);
ptr = ret;
FILL_ADVANCE_NODE(ptr, op);
- regcode = ptr;
+ PL_regcode = ptr;
return(ret);
}
STATIC regnode * /* Location. */
reganode(U8 op, U32 arg)
{
+ dTHR;
register regnode *ret;
register regnode *ptr;
- ret = regcode;
+ ret = PL_regcode;
if (SIZE_ONLY) {
- SIZE_ALIGN(regsize);
-#ifdef REGALIGN
- regsize += 2;
-#else
- regsize += 5;
-#endif
+ SIZE_ALIGN(PL_regsize);
+ PL_regsize += 2;
return(ret);
}
NODE_ALIGN_FILL(ret);
ptr = ret;
FILL_ADVANCE_NODE_ARG(ptr, op, arg);
- regcode = ptr;
+ PL_regcode = ptr;
return(ret);
}
/*
+- regc - emit (if appropriate) a Unicode character
+*/
+STATIC void
+reguni(UV uv, char* s, I32* lenp)
+{
+ dTHR;
+ if (SIZE_ONLY) {
+ U8 tmpbuf[10];
+ *lenp = uv_to_utf8(tmpbuf, uv) - tmpbuf;
+ }
+ else
+ *lenp = uv_to_utf8((U8*)s, uv) - (U8*)s;
+
+}
+
+/*
- regc - emit (if appropriate) a byte of code
*/
STATIC void
regc(U8 b, char* s)
{
+ dTHR;
if (!SIZE_ONLY)
*s = b;
}
STATIC void
reginsert(U8 op, regnode *opnd)
{
+ dTHR;
register regnode *src;
register regnode *dst;
register regnode *place;
register int offset = regarglen[(U8)op];
-/* (regkind[(U8)op] == CURLY ? EXTRA_STEP_2ARGS : 0); */
+/* (PL_regkind[(U8)op] == CURLY ? EXTRA_STEP_2ARGS : 0); */
if (SIZE_ONLY) {
- regsize += NODE_STEP_REGNODE + offset;
+ PL_regsize += NODE_STEP_REGNODE + offset;
return;
}
- src = regcode;
- regcode += NODE_STEP_REGNODE + offset;
- dst = regcode;
+ src = PL_regcode;
+ PL_regcode += NODE_STEP_REGNODE + offset;
+ dst = PL_regcode;
while (src > opnd)
StructCopy(--src, --dst, regnode);
src = NEXTOPER(place);
FILL_ADVANCE_NODE(place, op);
Zero(src, offset, regnode);
-#if defined(REGALIGN) && !defined(REGALIGN_STRUCT)
- src[offset + 1] = '\177';
-#endif
}
/*
STATIC void
regtail(regnode *p, regnode *val)
{
+ dTHR;
register regnode *scan;
register regnode *temp;
register I32 offset;
scan = temp;
}
-#ifdef REGALIGN
-# ifdef REGALIGN_STRUCT
if (reg_off_by_arg[OP(scan)]) {
ARG_SET(scan, val - scan);
- } else {
+ }
+ else {
NEXT_OFF(scan) = val - scan;
}
-# else
- offset = val - scan;
-# ifndef lint
- *(short*)(scan+1) = offset;
-# endif
-#endif
-#else
- if (OP(scan) == BACK)
- offset = scan - val;
- else
- offset = val - scan;
- *(scan+1) = (offset>>8)&0377;
- *(scan+2) = offset&0377;
-#endif
}
/*
STATIC void
regoptail(regnode *p, regnode *val)
{
+ dTHR;
/* "Operandless" and "op != BRANCH" are synonymous in practice. */
if (p == NULL || SIZE_ONLY)
return;
- if (regkind[(U8)OP(p)] == BRANCH) {
+ if (PL_regkind[(U8)OP(p)] == BRANCH) {
regtail(NEXTOPER(p), val);
- } else if ( regkind[(U8)OP(p)] == BRANCHJ) {
+ }
+ else if ( PL_regkind[(U8)OP(p)] == BRANCHJ) {
regtail(NEXTOPER(NEXTOPER(p)), val);
- } else
+ }
+ else
return;
}
if (OP(node) == OPTIMIZED)
goto after_print;
regprop(sv, node);
- PerlIO_printf(Perl_debug_log, "%4d%*s%s", node - start,
+ PerlIO_printf(Perl_debug_log, "%4d:%*s%s", node - start,
2*l + 1, "", SvPVX(sv));
if (next == NULL) /* Next ptr. */
PerlIO_printf(Perl_debug_log, "(0)");
PerlIO_printf(Perl_debug_log, "(%d)", next - start);
(void)PerlIO_putc(Perl_debug_log, '\n');
after_print:
- if (regkind[(U8)op] == BRANCHJ) {
+ if (PL_regkind[(U8)op] == BRANCHJ) {
register regnode *nnode = (OP(next) == LONGJMP
? regnext(next)
: next);
if (last && nnode > last)
nnode = last;
node = dumpuntil(start, NEXTOPER(NEXTOPER(node)), nnode, sv, l + 1);
- } else if (regkind[(U8)op] == BRANCH) {
+ }
+ else if (PL_regkind[(U8)op] == BRANCH) {
node = dumpuntil(start, NEXTOPER(node), next, sv, l + 1);
- } else if ( op == CURLY) { /* `next' might be very big: optimizer */
+ }
+ else if ( op == CURLY) { /* `next' might be very big: optimizer */
node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
NEXTOPER(node) + EXTRA_STEP_2ARGS + 1, sv, l + 1);
- } else if (regkind[(U8)op] == CURLY && op != CURLYX) {
+ }
+ else if (PL_regkind[(U8)op] == CURLY && op != CURLYX) {
node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
next, sv, l + 1);
- } else if ( op == PLUS || op == STAR) {
+ }
+ else if ( op == PLUS || op == STAR) {
node = dumpuntil(start, NEXTOPER(node), NEXTOPER(node) + 1, sv, l + 1);
- } else if (op == ANYOF) {
+ }
+ else if (op == ANYOF) {
node = NEXTOPER(node);
node += ANY_SKIP;
- } else if (regkind[(U8)op] == EXACT) {
+ }
+ else if (PL_regkind[(U8)op] == EXACT) {
/* Literal string, where present. */
node += ((*OPERAND(node)) + 2 + sizeof(regnode) - 1) / sizeof(regnode);
node = NEXTOPER(node);
- } else {
+ }
+ else {
node = NEXTOPER(node);
node += regarglen[(U8)op];
}
else if (op == WHILEM)
l--;
}
-#endif /* DEBUGGING */
+#endif /* DEBUGGING */
return node;
}
regdump(regexp *r)
{
#ifdef DEBUGGING
+ dTHR;
SV *sv = sv_newmortal();
(void)dumpuntil(r->program, r->program + 1, NULL, sv, 0);
/* Header fields of interest. */
if (r->anchored_substr)
PerlIO_printf(Perl_debug_log, "anchored `%s%s%s'%s at %d ",
- colors[0],
+ PL_colors[0],
SvPVX(r->anchored_substr),
- colors[1],
+ PL_colors[1],
SvTAIL(r->anchored_substr) ? "$" : "",
r->anchored_offset);
if (r->float_substr)
PerlIO_printf(Perl_debug_log, "floating `%s%s%s'%s at %d..%u ",
- colors[0],
+ PL_colors[0],
SvPVX(r->float_substr),
- colors[1],
+ PL_colors[1],
SvTAIL(r->float_substr) ? "$" : "",
r->float_min_offset, r->float_max_offset);
if (r->check_substr)
if (r->reganch & ROPT_IMPLICIT)
PerlIO_printf(Perl_debug_log, "implicit ");
PerlIO_printf(Perl_debug_log, "minlen %ld ", (long) r->minlen);
+ if (r->reganch & ROPT_EVAL_SEEN)
+ PerlIO_printf(Perl_debug_log, "with eval ");
PerlIO_printf(Perl_debug_log, "\n");
-#endif /* DEBUGGING */
+#endif /* DEBUGGING */
}
/*
regprop(SV *sv, regnode *o)
{
#ifdef DEBUGGING
+ dTHR;
register char *p = 0;
- sv_setpv(sv, ":");
+ sv_setpvn(sv, "", 0);
switch (OP(o)) {
case BOL:
p = "BOL";
case EOL:
p = "EOL";
break;
+ case EOS:
+ p = "EOS";
+ break;
case MEOL:
p = "MEOL";
break;
case SEOL:
p = "SEOL";
break;
- case ANY:
+ case REG_ANY:
p = "ANY";
break;
case SANY:
p = "SANY";
break;
+ case ANYUTF8:
+ p = "ANYUTF8";
+ break;
+ case SANYUTF8:
+ p = "SANYUTF8";
+ break;
+ case ANYOFUTF8:
+ p = "ANYOFUTF8";
+ break;
case ANYOF:
p = "ANYOF";
break;
p = "BRANCH";
break;
case EXACT:
- sv_catpvf(sv, "EXACT <%s%s%s>", colors[0], OPERAND(o) + 1, colors[1]);
+ sv_catpvf(sv, "EXACT <%s%s%s>", PL_colors[0], OPERAND(o) + 1, PL_colors[1]);
break;
case EXACTF:
- sv_catpvf(sv, "EXACTF <%s%s%s>", colors[0], OPERAND(o) + 1, colors[1]);
+ sv_catpvf(sv, "EXACTF <%s%s%s>", PL_colors[0], OPERAND(o) + 1, PL_colors[1]);
break;
case EXACTFL:
- sv_catpvf(sv, "EXACTFL <%s%s%s>", colors[0], OPERAND(o) + 1, colors[1]);
+ sv_catpvf(sv, "EXACTFL <%s%s%s>", PL_colors[0], OPERAND(o) + 1, PL_colors[1]);
break;
case NOTHING:
p = "NOTHING";
sv_catpvf(sv, "CURLY {%d,%d}", ARG1(o), ARG2(o));
break;
case CURLYM:
-#ifdef REGALIGN_STRUCT
sv_catpvf(sv, "CURLYM[%d] {%d,%d}", o->flags, ARG1(o), ARG2(o));
-#else
- sv_catpvf(sv, "CURLYM {%d,%d}", ARG1(o), ARG2(o));
-#endif
break;
case CURLYN:
-#ifdef REGALIGN_STRUCT
sv_catpvf(sv, "CURLYN[%d] {%d,%d}", o->flags, ARG1(o), ARG2(o));
-#else
- sv_catpvf(sv, "CURLYN {%d,%d}", ARG1(o), ARG2(o));
-#endif
break;
case CURLYX:
sv_catpvf(sv, "CURLYX {%d,%d}", ARG1(o), ARG2(o));
p = "GPOS";
break;
case UNLESSM:
-#ifdef REGALIGN_STRUCT
sv_catpvf(sv, "UNLESSM[-%d]", o->flags);
-#else
- p = "UNLESSM";
-#endif
break;
case IFMATCH:
-#ifdef REGALIGN_STRUCT
sv_catpvf(sv, "IFMATCH[-%d]", o->flags);
-#else
- p = "IFMATCH";
-#endif
break;
case SUCCEED:
p = "SUCCEED";
sv_catpvf(sv, "GROUPP%d", ARG(o));
break;
case LOGICAL:
- p = "LOGICAL";
+ sv_catpvf(sv, "LOGICAL[%d]", o->flags);
break;
case SUSPEND:
p = "SUSPEND";
}
if (p)
sv_catpv(sv, p);
-#endif /* DEBUGGING */
+#endif /* DEBUGGING */
}
void
pregfree(struct regexp *r)
{
+ dTHR;
if (!r || (--r->refcnt > 0))
return;
if (r->precomp)
regnode *
regnext(register regnode *p)
{
+ dTHR;
register I32 offset;
- if (p == ®dummy)
+ if (p == &PL_regdummy)
return(NULL);
offset = (reg_off_by_arg[OP(p)] ? ARG(p) : NEXT_OFF(p));
if (offset == 0)
return(NULL);
-#ifdef REGALIGN
return(p+offset);
-#else
- if (OP(p) == BACK)
- return(p-offset);
- else
- return(p+offset);
-#endif
}
-#ifdef I_STDARG
STATIC void
re_croak2(const char* pat1,const char* pat2,...)
-#else
-/*VARARGS0*/
-static void
-re_croak2(const char* pat1,const char* pat2, va_alist)
- const char* pat1;
- const char* pat2;
- va_dcl
-#endif
{
va_list args;
STRLEN l1 = strlen(pat1);
l2 = 510 - l1;
Copy(pat1, buf, l1 , char);
Copy(pat2, buf + l1, l2 , char);
- buf[l1 + l2 + 1] = '\n';
- buf[l1 + l2 + 2] = '\0';
-#ifdef I_STDARG
+ buf[l1 + l2] = '\n';
+ buf[l1 + l2 + 1] = '\0';
va_start(args, pat2);
-#else
- va_start(args);
-#endif
message = mess(buf, &args);
va_end(args);
l1 = strlen(message);
buf[l1] = '\0'; /* Overwrite \n */
croak("%s", buf);
}
+
+/* XXX Here's a total kludge. But we need to re-enter for swash routines. */
+
+void
+save_re_context(void)
+{
+ dTHR;
+ SAVEPPTR(PL_bostr);
+ SAVEPPTR(PL_regprecomp); /* uncompiled string. */
+ SAVEI32(PL_regnpar); /* () count. */
+ SAVEI32(PL_regsize); /* Code size. */
+ SAVEI16(PL_regflags); /* are we folding, multilining? */
+ SAVEPPTR(PL_reginput); /* String-input pointer. */
+ SAVEPPTR(PL_regbol); /* Beginning of input, for ^ check. */
+ SAVEPPTR(PL_regeol); /* End of input, for $ check. */
+ SAVESPTR(PL_regstartp); /* Pointer to startp array. */
+ SAVESPTR(PL_regendp); /* Ditto for endp. */
+ SAVESPTR(PL_reglastparen); /* Similarly for lastparen. */
+ SAVEPPTR(PL_regtill); /* How far we are required to go. */
+ SAVEI32(PL_regprev); /* char before regbol, \n if none */
+ SAVESPTR(PL_reg_start_tmp); /* from regexec.c */
+ PL_reg_start_tmp = 0;
+ SAVEFREEPV(PL_reg_start_tmp);
+ SAVEI32(PL_reg_start_tmpl); /* from regexec.c */
+ PL_reg_start_tmpl = 0;
+ SAVESPTR(PL_regdata);
+ SAVEI32(PL_reg_flags); /* from regexec.c */
+ SAVEI32(PL_reg_eval_set); /* from regexec.c */
+ SAVEI32(PL_regnarrate); /* from regexec.c */
+ SAVESPTR(PL_regprogram); /* from regexec.c */
+ SAVEINT(PL_regindent); /* from regexec.c */
+ SAVESPTR(PL_regcc); /* from regexec.c */
+ SAVESPTR(PL_curcop);
+ SAVESPTR(PL_regcomp_rx); /* from regcomp.c */
+ SAVEI32(PL_regseen); /* from regcomp.c */
+ SAVEI32(PL_regsawback); /* Did we see \1, ...? */
+ SAVEI32(PL_regnaughty); /* How bad is this pattern? */
+ SAVESPTR(PL_regcode); /* Code-emit pointer; ®dummy = don't */
+ SAVEPPTR(PL_regxend); /* End of input for compile */
+ SAVEPPTR(PL_regcomp_parse); /* Input-scan pointer. */
+}