regexec.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/* regexec.c
*/
/*
* "One Ring to rule them all, One Ring to find them..."
*/
/* NOTE: this is derived from Henry Spencer's regexp code, and should not
* confused with the original package (see point 3 below). Thanks, Henry!
*/
/* Additional note: this code is very heavily munged from Henry's version
* in places. In some spots I've traded clarity for efficiency, so don't
* blame Henry for some of the lack of readability.
*/
/* The names of the functions have been changed from regcomp and
* regexec to pregcomp and pregexec in order to avoid conflicts
* 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 */
# if defined(PERL_EXT_RE_DEBUG) && !defined(DEBUGGING)
# define DEBUGGING
# endif
#endif
#ifdef PERL_IN_XSUB_RE
/* We *really* need to overwrite these symbols: */
# define Perl_regexec_flags my_regexec
# define Perl_regdump my_regdump
# define Perl_regprop my_regprop
# define Perl_re_intuit_start my_re_intuit_start
/* *These* symbols are masked to allow static link. */
# define Perl_pregexec my_pregexec
# define Perl_reginitcolors my_reginitcolors
# define PERL_NO_GET_CONTEXT
#endif
/*SUPPRESS 112*/
/*
* pregcomp and pregexec -- regsub and regerror are not used in perl
*
* Copyright (c) 1986 by University of Toronto.
* Written by Henry Spencer. Not derived from licensed software.
*
* Permission is granted to anyone to use this software for any
* purpose on any computer system, and to redistribute it freely,
* subject to the following restrictions:
*
* 1. The author is not responsible for the consequences of use of
* this software, no matter how awful, even if they arise
* from defects in it.
*
* 2. The origin of this software must not be misrepresented, either
* by explicit claim or by omission.
*
* 3. Altered versions must be plainly marked as such, and must not
* be misrepresented as being the original software.
*
**** Alterations to Henry's code are...
****
**** Copyright (c) 1991-2001, 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.
*
* Beware that some of this code is subtly aware of the way operator
* precedence is structured in regular expressions. Serious changes in
* regular-expression syntax might require a total rethink.
*/
#include "EXTERN.h"
#define PERL_IN_REGEXEC_C
#include "perl.h"
#ifdef PERL_IN_XSUB_RE
# if defined(PERL_CAPI) || defined(PERL_OBJECT)
# include "XSUB.h"
# endif
#endif
#include "regcomp.h"
#ifndef STATIC
#define STATIC static
#endif
/*
* Forwards.
*/
#ifdef DEBUGGING
# define REGINCLASSUTF8(f,p) (ARG1(f) ? reginclassutf8(f,p) : swash_fetch(*av_fetch((AV*)SvRV((SV*)PL_regdata->data[ARG2(f)]),0,FALSE),p))
#else
# define REGINCLASSUTF8(f,p) (ARG1(f) ? reginclassutf8(f,p) : swash_fetch((SV*)PL_regdata->data[ARG2(f)],p))
#endif
{
int retval = PL_savestack_ix;
#define REGCP_PAREN_ELEMS 4
int p;
#define REGCP_OTHER_ELEMS 5
for (p = PL_regsize; p > parenfloor; p--) {
/* REGCP_PARENS_ELEMS are pushed per pairs of parentheses. */
SSPUSHINT(PL_regendp[p]);
SSPUSHINT(PL_regstartp[p]);
SSPUSHINT(p);
}
/* REGCP_OTHER_ELEMS are pushed in any case, parentheses or no. */
#define REGCP_FRAME_ELEMS 2
/* REGCP_FRAME_ELEMS are part of the REGCP_OTHER_ELEMS and
* are needed for the regexp context stack bookkeeping. */
return retval;
}
/* These are needed since we do not localize EVAL nodes: */
STATIC char *
{
I32 i;
char *input;
/* Pop REGCP_OTHER_ELEMS before the parentheses loop starts. */
i = SSPOPINT;
i = SSPOPINT; /* Parentheses elements to pop. */
/* Now restore the parentheses context. */
for (i -= (REGCP_OTHER_ELEMS - REGCP_FRAME_ELEMS);
i > 0; i -= REGCP_PAREN_ELEMS) {
if (paren <= *PL_reglastparen)
);
}
}
);
#if 1
/* It would seem that the similar code in regtry()
* already takes care of this, and in fact it is in
* a better location to since this code can #if 0-ed out
* but the code in regtry() is needed or otherwise tests
* (as of patchlevel 7877) will fail. Then again,
* this code seems to be necessary or otherwise
* building DynaLoader will fail:
* "Error: '*' not in typemap in DynaLoader.xs, line 164"
* --jhi */
if (paren > PL_regsize)
}
#endif
return input;
}
STATIC char *
{
regcppop();
return Nullch;
}
typedef struct re_cc_state
{
struct re_cc_state *prev;
} re_cc_state;
if (paren) { \
if (n) { \
} \
else \
} \
sayYES; \
if (paren && n) \
}
/*
* pregexec and friends
*/
/*
- pregexec - match a regexp against a string
*/
/* strend: pointer to null at end of string */
/* strbeg: real beginning of string */
/* minend: end of match must be >=minend after stringarg. */
/* nosave: For optimizations. */
{
return
nosave ? 0 : REXEC_COPY_STR);
}
STATIC void
{
#ifdef DEBUGGING
#endif
}
/*
* Need to implement the following flags for reg_anch:
*
* USE_INTUIT_NOML - Useful to call re_intuit_start() first
* USE_INTUIT_ML
* INTUIT_AUTORITATIVE_NOML - Can trust a positive answer
* INTUIT_AUTORITATIVE_ML
* INTUIT_ONCE_NOML - Intuit can match in one location only.
* INTUIT_ONCE_ML
*
* Another flag for this function: SECOND_TIME (so that float substrs
* with giant delta may be not rechecked).
*/
/* Assumptions: if ANCH_GPOS, then strpos is anchored. XXXX Check GPOS logic */
/* If SCREAM, then SvPVX(sv) should be compatible with strpos and strend.
Otherwise, only SvCUR(sv) is used to get strbeg. */
/* XXXX We assume that strpos is strbeg unless sv. */
/* XXXX Some places assume that there is a fixed substring.
An update may be needed if optimizer marks as "INTUITable"
RExen without fixed substrings. Similarly, it is assumed that
lengths of all the strings are no more than minlen, thus they
cannot come from lookahead.
(Or minlen should take into account lookahead.) */
/* A failure to find a constant substring means that there is no need to make
an expensive call to REx engine, thus we celebrate a failure. Similarly,
finding a substring too deep into the string means that less calls to
regtry() should be needed.
REx compiler's optimizer found 4 possible hints:
a) Anchored substring;
b) Fixed substring;
c) Whether we are anchored (beginning-of-line or \G);
d) First node (of those at offset 0) which may distingush positions;
We use a)b)d) and multiline-part of c), and try to find a position in the
string which does not contradict any of them.
*/
/* Most of decisions we do here should have been done at compile time.
The nodes of the REx which we used for the search should have been
deleted from the finite automaton. */
char *
{
register I32 start_shift;
/* Should be nonnegative! */
register char *s;
char *strbeg;
char *t;
char *tmp;
char *check_at; /* check substr found at this pos */
#ifdef DEBUGGING
#endif
"%sGuessing start of match, REx%s `%s%.60s%s%s' against `%s%.*s%s%s'...\n",
PL_colors[1],
PL_colors[0],
);
goto fail;
}
&& !PL_multiline ) ); /* Check after \n? */
if (!ml_anch) {
/* SvCUR is not set on references: SvRV and SvPVX overlap */
goto fail;
}
/* Substring at constant offset from beg-of-str... */
goto fail_finish;
}
/* Now should match s[0..slen-2] */
slen--;
|| (slen > 1
goto fail_finish;
}
}
goto report_neq;
goto success_at_start;
}
}
/* Match is anchored, but substr is not anchored wrt beg-of-str. */
s = strpos;
if (!ml_anch) {
}
}
else { /* Can match at random position */
ml_anch = 0;
s = strpos;
/* Should be nonnegative! */
}
#ifdef DEBUGGING /* 7/99: reports of failure (with the older version) */
if (end_shift < 0)
#endif
other_last = Nullch;
/* Find a possible match in the region s..strend by looking for
if (flags & REXEC_SCREAM) {
else
goto fail_finish;
if (data)
*data->scream_olds = s;
}
else
s = fbm_instr((unsigned char*)s + start_shift,
/* Update the count-of-usability, remove useless subpatterns,
unshift s. */
(s ? "Found" : "Did not find"),
PL_colors[0],
(s ? " at offset " : "...\n") ) );
if (!s)
goto fail_finish;
check_at = s;
/* Finish the diagnostic message */
/* Got a candidate. Check MBOL anchoring, and the *other* substr.
Start with the other substr.
XXXX no SCREAM optimization yet - and a very coarse implementation
XXXX /ttx+/ results in anchored=`ttx', floating=`x'. floating will
*always* match. Probably should be marked during compile...
Probably it is right to do no SCREAM here...
*/
/* Take into account the "other" substring. */
/* XXXX May be hopelessly wrong for UTF... */
if (!other_last)
other_last = strpos;
{
char *s1 = s;
t = s - prog->check_offset_max;
&& t > strpos)))
/* EMPTY */;
else
t = strpos;
t += prog->anchored_offset;
if (t < other_last) /* These positions already checked */
t = other_last;
/* XXXX It is not documented what units *_offsets are in. Assume bytes. */
/* On end-of-str: see comment below. */
s = fbm_instr((unsigned char*)t,
(s ? "Found" : "Contradicts"),
PL_colors[0],
if (!s) {
", giving up...\n"));
goto fail_finish;
}
", trying floating at offset %ld...\n",
goto restart;
}
else {
(long)(s - i_strpos)));
t = s - prog->anchored_offset;
other_last = s + 1;
s = s1;
if (t == strpos)
goto try_at_start;
goto try_at_offset;
}
}
}
else { /* Take into account the floating substring. */
char *s1 = s;
t = s - start_shift;
s = t + prog->float_min_offset;
if (s < other_last)
s = other_last;
/* XXXX It is not documented what units *_offsets are in. Assume bytes. */
/* fbm_instr() takes into account exact value of end-of-str
if the check is SvTAIL(ed). Since false positives are OK,
and end-of-str is not later than strend we are OK. */
s = fbm_instr((unsigned char*)s,
(s ? "Found" : "Contradicts"),
PL_colors[0],
if (!s) {
", giving up...\n"));
goto fail_finish;
}
", trying anchored starting at offset %ld...\n",
other_last = last;
s = HOPc(t, 1);
goto restart;
}
else {
(long)(s - i_strpos)));
other_last = s; /* Fix this later. --Hugo */
s = s1;
if (t == strpos)
goto try_at_start;
goto try_at_offset;
}
}
}
t = s - prog->check_offset_max;
&& t > strpos)))) {
/* Fixed substring is found far enough so that the match
cannot start at strpos. */
/* Eventually fbm_*() should handle this, but often
anchored_offset is not 0, so this check will not be wasted. */
/* XXXX In the code below we prefer to look for "^" even in
presence of anchored substrings. And we search even
beyond the found float position. These pessimizations
are historical artefacts only. */
if (*t == '\n') {
if (prog->anchored_substr) {
/* Since we moved from the found position,
we definitely contradict the found anchored
substr. Due to the above check we do not
contradict "check" substr.
Thus we can arrive here only if check substr
is float. Redo checking for "other"=="fixed".
*/
strpos = t + 1;
DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld, rescanning for anchored from offset %ld...\n",
PL_colors[0],PL_colors[1], (long)(strpos - i_strpos), (long)(strpos - i_strpos + prog->anchored_offset)));
goto do_other_anchored;
}
/* We don't contradict the found floating substring. */
/* XXXX Why not check for STCLASS? */
s = t + 1;
goto set_useful;
}
/* Position contradicts check-string */
/* XXXX probably better to look for check-string
than for "\n", so one should lower the limit for t? */
DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m, restarting lookup for check-string at offset %ld...\n",
goto restart;
}
t++;
}
goto fail_finish;
}
else {
}
s = t;
}
else {
/* The found string does not prohibit matching at strpos,
- no optimization of calling REx engine can be performed,
unless it was an MBOL and we are not after MBOL,
or a future STCLASS check will fail this. */
/* Even in this situation we may use MBOL flag if strpos is offset
wrt the start of the string. */
/* May be due to an implicit anchor of m{.*foo} */
{
t = strpos;
goto find_anchor;
}
);
{
/* If flags & SOMETHING - do not do it many times on the same match */
s = strpos;
/* XXXX This is a remnant of the old implementation. It
looks wasteful, since now INTUIT can use many
other heuristics. */
}
else
s = strpos;
}
/* Last resort... */
/* XXXX BmUSEFUL already changed, maybe multiple change is meaningful... */
if (prog->regstclass) {
/* minlen == 0 is possible if regstclass is \b or \B,
and the fixed substr is ''$.
Since minlen is already taken into account, s+1 is before strend;
accidentally, minlen >= 1 guaranties no false positives at s + 1
even for \b or \B. But (minlen? 1 : 0) below assumes that
regstclass does not come from lookahead... */
/* If regstclass takes bytelength more than 1: If charlength==1, OK.
This leaves EXACTF only, which is dealt with in find_byclass(). */
: 1);
: strend) ;
t = s;
}
if (!s) {
#ifdef DEBUGGING
char *what;
#endif
"Could not match STCLASS...\n") );
goto fail;
}
"This position contradicts STCLASS...\n") );
goto fail;
/* Contradict one of substrings */
if (prog->anchored_substr) {
s = HOPc(t, 1);
/* XXXX Should be taken into account earlier? */
"Could not match STCLASS...\n") );
goto fail;
}
if (!check)
goto giveup;
"Looking for %s substr starting at offset %ld...\n",
goto restart;
}
/* Have both, check_string is floating */
goto retry_floating_check;
/* Recheck anchored substring, but not floating... */
s = check_at;
if (!check)
goto giveup;
"Looking for anchored substr starting at offset %ld...\n",
(long)(other_last - i_strpos)) );
goto do_other_anchored;
}
/* Another way we could have checked stclass at the
current position only: */
if (ml_anch) {
s = t = t + 1;
if (!check)
goto giveup;
"Looking for /%s^%s/m starting at offset %ld...\n",
goto try_at_offset;
}
goto fail;
/* Check is floating subtring. */
t = check_at - start_shift;
goto hop_and_restart;
}
DEBUG_r( if (t != s)
"By STCLASS: moving %ld --> %ld\n",
else
"Does not contradict STCLASS...\n") );
}
return s;
fail_finish: /* Substring not found */
fail:
return Nullch;
}
/* We know what class REx starts with. Try to find this position... */
STATIC char *
{
char *m;
unsigned int c1;
unsigned int c2;
char *e;
/* We know what class it must start with. */
switch (OP(c)) {
case ANYOFUTF8:
while (s < strend) {
if (REGINCLASSUTF8(c, (U8*)s)) {
goto got_it;
else
}
else
tmp = 1;
s += UTF8SKIP(s);
}
break;
case ANYOF:
while (s < strend) {
if (REGINCLASS(c, *(U8*)s)) {
goto got_it;
else
}
else
tmp = 1;
s++;
}
break;
case EXACTF:
m = STRING(c);
goto do_exactf;
case EXACTFL:
m = STRING(c);
if (norun && e < s)
e = s; /* Due to minlen logic of intuit() */
/* Here it is NOT UTF! */
while (s <= e) {
: ibcmp_locale(s, m, ln)))
goto got_it;
s++;
}
} else {
while (s <= e) {
: ibcmp_locale(s, m, ln)))
goto got_it;
s++;
}
}
break;
case BOUNDL:
/* FALL THROUGH */
case BOUND:
while (s < strend) {
goto got_it;
}
s++;
}
goto got_it;
break;
case BOUNDLUTF8:
/* FALL THROUGH */
case BOUNDUTF8:
if (s == startpos)
tmp = '\n';
else {
}
while (s < strend) {
isALNUM_LC_utf8((U8*)s)))
{
goto got_it;
}
s += UTF8SKIP(s);
}
goto got_it;
break;
case NBOUNDL:
/* FALL THROUGH */
case NBOUND:
while (s < strend) {
goto got_it;
s++;
}
goto got_it;
break;
case NBOUNDLUTF8:
/* FALL THROUGH */
case NBOUNDUTF8:
if (s == startpos)
tmp = '\n';
else {
}
while (s < strend) {
isALNUM_LC_utf8((U8*)s)))
goto got_it;
s += UTF8SKIP(s);
}
goto got_it;
break;
case ALNUM:
while (s < strend) {
if (isALNUM(*s)) {
goto got_it;
else
}
else
tmp = 1;
s++;
}
break;
case ALNUMUTF8:
while (s < strend) {
goto got_it;
else
}
else
tmp = 1;
s += UTF8SKIP(s);
}
break;
case ALNUML:
while (s < strend) {
if (isALNUM_LC(*s)) {
goto got_it;
else
}
else
tmp = 1;
s++;
}
break;
case ALNUMLUTF8:
while (s < strend) {
if (isALNUM_LC_utf8((U8*)s)) {
goto got_it;
else
}
else
tmp = 1;
s += UTF8SKIP(s);
}
break;
case NALNUM:
while (s < strend) {
if (!isALNUM(*s)) {
goto got_it;
else
}
else
tmp = 1;
s++;
}
break;
case NALNUMUTF8:
while (s < strend) {
goto got_it;
else
}
else
tmp = 1;
s += UTF8SKIP(s);
}
break;
case NALNUML:
while (s < strend) {
if (!isALNUM_LC(*s)) {
goto got_it;
else
}
else
tmp = 1;
s++;
}
break;
case NALNUMLUTF8:
while (s < strend) {
if (!isALNUM_LC_utf8((U8*)s)) {
goto got_it;
else
}
else
tmp = 1;
s += UTF8SKIP(s);
}
break;
case SPACE:
while (s < strend) {
if (isSPACE(*s)) {
goto got_it;
else
}
else
tmp = 1;
s++;
}
break;
case SPACEUTF8:
while (s < strend) {
goto got_it;
else
}
else
tmp = 1;
s += UTF8SKIP(s);
}
break;
case SPACEL:
while (s < strend) {
if (isSPACE_LC(*s)) {
goto got_it;
else
}
else
tmp = 1;
s++;
}
break;
case SPACELUTF8:
while (s < strend) {
goto got_it;
else
}
else
tmp = 1;
s += UTF8SKIP(s);
}
break;
case NSPACE:
while (s < strend) {
if (!isSPACE(*s)) {
goto got_it;
else
}
else
tmp = 1;
s++;
}
break;
case NSPACEUTF8:
while (s < strend) {
goto got_it;
else
}
else
tmp = 1;
s += UTF8SKIP(s);
}
break;
case NSPACEL:
while (s < strend) {
if (!isSPACE_LC(*s)) {
goto got_it;
else
}
else
tmp = 1;
s++;
}
break;
case NSPACELUTF8:
while (s < strend) {
goto got_it;
else
}
else
tmp = 1;
s += UTF8SKIP(s);
}
break;
case DIGIT:
while (s < strend) {
if (isDIGIT(*s)) {
goto got_it;
else
}
else
tmp = 1;
s++;
}
break;
case DIGITUTF8:
while (s < strend) {
goto got_it;
else
}
else
tmp = 1;
s += UTF8SKIP(s);
}
break;
case DIGITL:
while (s < strend) {
if (isDIGIT_LC(*s)) {
goto got_it;
else
}
else
tmp = 1;
s++;
}
break;
case DIGITLUTF8:
while (s < strend) {
if (isDIGIT_LC_utf8((U8*)s)) {
goto got_it;
else
}
else
tmp = 1;
s += UTF8SKIP(s);
}
break;
case NDIGIT:
while (s < strend) {
if (!isDIGIT(*s)) {
goto got_it;
else
}
else
tmp = 1;
s++;
}
break;
case NDIGITUTF8:
while (s < strend) {
goto got_it;
else
}
else
tmp = 1;
s += UTF8SKIP(s);
}
break;
case NDIGITL:
while (s < strend) {
if (!isDIGIT_LC(*s)) {
goto got_it;
else
}
else
tmp = 1;
s++;
}
break;
case NDIGITLUTF8:
while (s < strend) {
if (!isDIGIT_LC_utf8((U8*)s)) {
goto got_it;
else
}
else
tmp = 1;
s += UTF8SKIP(s);
}
break;
default:
break;
}
return 0;
return s;
}
/*
- regexec_flags - match a regexp against a string
*/
/* strend: pointer to null at end of string */
/* strbeg: real beginning of string */
/* minend: end of match must be >=minend after stringarg. */
/* data: May be used for some additional optimizations. */
/* nosave: For optimizations. */
{
register char *s;
register regnode *c;
/* I32 start_shift = 0; */ /* Offset of the start to find
constant substr. */ /* CC */
char *scream_olds;
PL_regcc = 0;
#ifdef DEBUGGING
#endif
/* Be paranoid... */
return 0;
}
PL_regprev = '\n';
else {
}
/* Check validity of program. */
}
PL_reg_flags = 0;
PL_reg_eval_set = 0;
PL_reg_maxiter = 0;
PL_reg_flags |= RF_utf8;
/* Mark beginning of line for ^ and lookbehind. */
/* Mark end of line for $ (and such) */
/* see how far we have to get to not match where we matched before */
/* We start without call_cc context. */
PL_reg_call_cc = 0;
/* If there is a "must appear" string, look for it. */
s = startpos;
if (s > PL_reg_ganch)
goto phooey;
s = PL_reg_ganch;
}
}
else /* pos() not defined */
}
d.scream_olds = &scream_olds;
d.scream_pos = &scream_pos;
if (!s)
goto phooey; /* not present */
}
"%sMatching REx%s `%s%.60s%s%s' against `%s%.*s%s%s'\n",
PL_colors[1],
PL_colors[0],
);
/* Simplest case: anchored match need be tried only once. */
/* [unless only anchor is BOL and multiline is set] */
goto got_it;
{
char *end;
if (minlen)
/* for multiline we only have to try after newlines */
if (prog->check_substr) {
if (s == startpos)
goto after_try;
while (1) {
goto got_it;
if (s >= end)
goto phooey;
if (!s)
goto phooey;
}
else
s++;
}
} else {
if (s > startpos)
s--;
while (s < end) {
if (*s++ == '\n') { /* don't need PL_utf8skip here */
goto got_it;
}
}
}
}
goto phooey;
goto got_it;
goto phooey;
}
/* Messy cases: unanchored match. */
/* we have /x+whatever/ */
/* it must be a one character string (XXXX Except UTF?) */
#ifdef DEBUGGING
int did_match = 0;
#endif
if (UTF) {
while (s < strend) {
if (*s == ch) {
s += UTF8SKIP(s);
s += UTF8SKIP(s);
}
s += UTF8SKIP(s);
}
}
else {
while (s < strend) {
if (*s == ch) {
s++;
s++;
}
s++;
}
}
"Did not find anchored character...\n"));
}
/*SUPPRESS 560*/
char *last1; /* Last position checked before */
#ifdef DEBUGGING
int did_match = 0;
#endif
if (s > PL_bostr)
else
/* XXXX check_substr already used to find `s', can optimize if
check_substr==must. */
scream_pos = -1;
while ( (s <= last) &&
((flags & REXEC_SCREAM)
end_shift, &scream_pos, 0))
PL_multiline ? FBMrf_MULTILINE : 0))) ) {
}
else {
s = t;
}
if (UTF) {
while (s <= last1) {
goto got_it;
s += UTF8SKIP(s);
}
}
else {
while (s <= last1) {
goto got_it;
s++;
}
}
}
? "anchored" : "floating"),
PL_colors[0],
goto phooey;
}
else if ((c = prog->regstclass)) {
/* don't bother with what can't match */
goto got_it;
}
else {
dontbother = 0;
char *last;
if (flags & REXEC_SCREAM) {
if (!last)
}
else {
else if (!PL_multiline)
else
goto find_last;
} else {
if (len)
else
}
}
"%sCan't trim the tail, match fails (should not happen)%s\n",
goto phooey; /* Should not happen! */
}
}
/* We don't know much -- general case. */
if (UTF) {
for (;;) {
goto got_it;
if (s >= strend)
break;
s += UTF8SKIP(s);
};
}
else {
do {
goto got_it;
} while (s++ < strend);
}
}
/* Failure. */
goto phooey;
if (PL_reg_eval_set) {
/* Preserve the current value of $^R */
restored, the value remains
the same. */
restore_pos(aTHXo_ 0);
}
/* make sure $`, $&, $', and $digit will work later */
if ( !(flags & REXEC_NOT_FIRST) ) {
if (RX_MATCH_COPIED(prog)) {
}
if (flags & REXEC_COPY_STR) {
}
else {
}
}
return 1;
if (PL_reg_eval_set)
restore_pos(aTHXo_ 0);
return 0;
}
/*
- regtry - try match at specific point
*/
{
register I32 i;
#ifdef DEBUGGING
PL_regindent = 0; /* XXXX Not good when matches are reenterable... */
#endif
));
/* Otherwise OP_NEXTSTATE will free whatever on stack now. */
/* Apparently this is not needed, judging by wantarray. */
/* SAVEI8(cxstack[cxstack_ix].blk_gimme);
cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
if (PL_reg_sv) {
/* Make $_ available to executed code. */
/* SAVE_DEFSV does *not* suffice here for USE_THREADS */
}
/* prepare for quick setting of pos */
}
PL_reg_magic = mg;
}
if (!PL_reg_curpm)
if (RX_MATCH_COPIED(prog)) {
/* Here is a serious problem: we cannot rewrite subbeg,
since it may be needed if this match fails. Thus
$` inside (?{}) could fail... */
}
else
}
PL_regsize = 0;
if(PL_reg_start_tmp)
else
}
/* XXXX What this code is doing here?!!! There should be no need
to do this again and again, PL_reglastparen should take care of
this! --ilya*/
* Actually, the code in regcppop() (which Ilya may be meaning by
* PL_reglastparen), is not needed at all by the test suite
* enough, for building DynaLoader, or otherwise this
* "Error: '*' not in typemap in DynaLoader.xs, line 164"
* will happen. Meanwhile, this code *is* needed for the
* above-mentioned test suite tests to succeed. The common theme
* on those tests seems to be returning null fields from matches.
* --jhi */
#if 1
*++sp = -1;
*++ep = -1;
}
}
#endif
return 1;
}
return 0;
}
#define RE_UNWIND_BRANCH 1
#define RE_UNWIND_BRANCHJ 2
union re_unwind_t;
typedef struct { /* XX: makes sense to enlarge it... */
typedef struct {
char *locinput;
#ifdef DEBUGGING
int regindent;
#endif
typedef union re_unwind_t {
} re_unwind_t;
/*
- regmatch - main matching routine
*
* Conceptually the strategy is simple: check to see whether the current
* node matches, call self recursively to see whether the rest matches,
* and then act accordingly. In practice we make some effort to avoid
* recursion, in particular by going through "ordinary" nodes (that don't
* need to know whether the rest of the match failed) by a loop instead of
* by recursion.
*/
/* [lwall] I've hoisted the register declarations to the outer block in order to
* maybe save a little bit of pushing and popping on the stack. It also takes
* advantage of machines that use a register save mask on subroutine entry.
*/
{
function of same name */
register I32 n; /* no or next */
register char *s; /* operand or save */
register char *locinput = PL_reginput;
#ifdef DEBUGGING
PL_regindent++;
#endif
/* Note that nextchr is a byte even in UTF */
#if 1
# define sayYES_FINAL goto yes_final
# define sayYES_LOUD goto yes_loud
# define sayNO_FINAL goto no_final
# define sayNO_SILENT goto do_no
# define REPORT_CODE_OFF 24
#else
# define sayYES return 1
# define sayNO return 0
# define sayYES_FINAL return 1
# define sayYES_LOUD return 1
# define sayNO_FINAL return 0
# define sayNO_SILENT return 0
# define saySAME(x) return x
#endif
DEBUG_r( {
/* The part of the string before starttry has one color
(pref0_len chars), between starttry and current
position another one (pref_len - pref0_len chars),
after the current position the third one.
We assume that pref0_len <= pref_len, otherwise we
decrease pref0_len. */
if (pref0_len < 0)
pref0_len = 0;
"",
} );
case BOL:
? PL_regprev == '\n'
: (PL_multiline &&
{
/* regtill = regbol; */
break;
}
case MBOL:
? PL_regprev == '\n'
{
break;
}
case SBOL:
break;
case GPOS:
if (locinput == PL_reg_ganch)
break;
case EOL:
if (PL_multiline)
goto meol;
else
goto seol;
case MEOL:
meol:
break;
case SEOL:
seol:
break;
case EOS:
break;
case SANYUTF8:
if (nextchr & 0x80) {
break;
}
break;
case SANY:
break;
case ANYUTF8:
if (nextchr & 0x80) {
break;
}
break;
case REG_ANY:
break;
case EXACT:
/* Inline the first character, for speed. */
break;
case EXACTFL:
/* FALL THROUGH */
case EXACTF:
if (UTF) {
char *l = locinput;
char *e = s + ln;
while (s < e) {
if (l >= PL_regeol)
if (utf8_to_uv((U8*)s, e - s, 0, 0) !=
(c1 ?
toLOWER_utf8((U8*)l) :
toLOWER_LC_utf8((U8*)l)))
{
}
s += UTF8SKIP(s);
l += UTF8SKIP(l);
}
locinput = l;
break;
}
/* Inline the first character, for speed. */
break;
case ANYOFUTF8:
break;
case ANYOF:
if (nextchr < 0)
break;
case ALNUML:
/* FALL THROUGH */
case ALNUM:
if (!nextchr)
break;
case ALNUMLUTF8:
/* FALL THROUGH */
case ALNUMUTF8:
if (!nextchr)
if (nextchr & 0x80) {
{
}
break;
}
break;
case NALNUML:
/* FALL THROUGH */
case NALNUM:
break;
case NALNUMLUTF8:
/* FALL THROUGH */
case NALNUMUTF8:
if (nextchr & 0x80) {
{
}
break;
}
break;
case BOUNDL:
case NBOUNDL:
/* FALL THROUGH */
case BOUND:
case NBOUND:
/* was last char in word? */
}
else {
n = isALNUM_LC(nextchr);
}
break;
case BOUNDLUTF8:
case NBOUNDLUTF8:
/* FALL THROUGH */
case BOUNDUTF8:
case NBOUNDUTF8:
/* was last char in word? */
ln = PL_regprev;
else {
ln = utf8_to_uv(r, s - (char*)r, 0, 0);
}
}
else {
}
break;
case SPACEL:
/* FALL THROUGH */
case SPACE:
if (!nextchr)
break;
case SPACELUTF8:
/* FALL THROUGH */
case SPACEUTF8:
if (!nextchr)
if (nextchr & 0x80) {
{
}
break;
}
break;
case NSPACEL:
/* FALL THROUGH */
case NSPACE:
break;
case NSPACELUTF8:
/* FALL THROUGH */
case NSPACEUTF8:
if (nextchr & 0x80) {
{
}
break;
}
break;
case DIGITL:
/* FALL THROUGH */
case DIGIT:
if (!nextchr)
break;
case DIGITLUTF8:
/* FALL THROUGH */
case DIGITUTF8:
if (!nextchr)
if (nextchr & 0x80) {
{
}
break;
}
break;
case NDIGITL:
/* FALL THROUGH */
case NDIGIT:
break;
case NDIGITLUTF8:
/* FALL THROUGH */
case NDIGITUTF8:
if (nextchr & 0x80) {
{
}
break;
}
break;
case CLUMP:
break;
case REFFL:
/* FALL THROUGH */
case REF:
case REFF:
ln = PL_regstartp[n];
sayNO; /* Do not match unless seen CLOSEn. */
if (ln == PL_regendp[n])
break;
char *l = locinput;
char *e = PL_bostr + PL_regendp[n];
/*
* Note that we can't do the "other character" lookup trick as
* in the 8-bit case (no pun intended) because in Unicode we
* have to map both upper and title case to lower case.
*/
while (s < e) {
if (l >= PL_regeol)
s += UTF8SKIP(s);
l += UTF8SKIP(l);
}
}
else {
while (s < e) {
if (l >= PL_regeol)
s += UTF8SKIP(s);
l += UTF8SKIP(l);
}
}
locinput = l;
break;
}
/* Inline the first character, for speed. */
break;
case NOTHING:
case TAIL:
break;
case BACK:
break;
case EVAL:
{
dSP;
if (logical) {
}
if (mg) {
(void)ReREFCNT_inc(re);
}
else {
char *oprecomp = PL_regprecomp;
pm.op_pmflags = 0;
PL_regsize = osize;
PL_regnpar = onpar;
}
"Entering embedded `%s%.60s%s%s'\n",
PL_colors[0],
PL_colors[1],
);
PL_regcc = 0;
*PL_reglastparen = 0;
PL_reg_call_cc = &state;
/* XXXX This is too dramatic a measure... */
PL_reg_maxiter = 0;
/* Even though we succeeded, we need to restore
global variables, since we may be wrapped inside
SUSPEND, thus the match may be not finished yet. */
/* XXXX Do this only if SUSPENDed? */
/* XXXX This is too dramatic a measure... */
PL_reg_maxiter = 0;
/* These are needed even if not SUSPEND. */
}
regcppop();
/* XXXX This is too dramatic a measure... */
PL_reg_maxiter = 0;
}
logical = 0;
}
else
break;
}
case OPEN:
PL_reg_start_tmp[n] = locinput;
if (n > PL_regsize)
PL_regsize = n;
break;
case CLOSE:
if (n > *PL_reglastparen)
*PL_reglastparen = n;
break;
case GROUPP:
break;
case IFTHEN:
if (sw)
else {
}
break;
case LOGICAL:
break;
/*******************************************************************
PL_regcc contains infoblock about the innermost (...)* loop, and
a pointer to the next outer infoblock.
1) After matching X, regnode for CURLYX is processed;
2) This regnode creates infoblock on the stack, and calls
regmatch() recursively with the starting point at WHILEM node;
3) Each hit of WHILEM node tries to match A and Z (in the order
greediness). The information about where are nodes for "A"
and "Z" is read from the infoblock, as is info on how many times "A"
was already matched, and greediness.
4) After A matches, the same WHILEM node is hit again.
5) Each time WHILEM is hit, PL_regcc is the infoblock created by CURLYX
of the same pair. Thus when WHILEM tries to match Z, it temporarily
resets PL_regcc, since this Y(A)*Z can be a part of some other loop:
as in (Y(A)*Z)*. If Z matches, the automaton will hit the WHILEM node
of the external loop.
Currently present infoblocks form a tree with a stem formed by PL_curcc
and whatever it mentions via ->next, and additional attached trees
corresponding to temporarily unset infoblocks as in "5" above.
In the following picture infoblocks for outer loop of
(Y(A)*?Z)*?T are denoted O, for inner I. NULL starting block
is denoted by x. The matched string is YAAZYAZT. Temporarily postponed
infoblocks are drawn below the "reset" infoblock.
In fact in the picture below we do not show failed matches for Z and T
by WHILEM blocks. [We illustrate minimal matches, since for them it is
more obvious *why* one needs to *temporary* unset infoblocks.]
Matched REx position InfoBlocks Comment
(Y(A)*?Z)*?T x
Y(A)*?Z)*?T x <- O
Y (A)*?Z)*?T x <- O
Y A)*?Z)*?T x <- O <- I
YA )*?Z)*?T x <- O <- I
YA A)*?Z)*?T x <- O <- I
YAA )*?Z)*?T x <- O <- I
YAA Z)*?T x <- O # Temporary unset I
I
YAAZ Y(A)*?Z)*?T x <- O
I
YAAZY (A)*?Z)*?T x <- O
I
YAAZY A)*?Z)*?T x <- O <- I
I
YAAZYA )*?Z)*?T x <- O <- I
I
YAAZYA Z)*?T x <- O # Temporary unset I
I,I
YAAZYAZ )*?T x <- O
I,I
YAAZYAZ T x # Temporary unset O
O
I,I
YAAZYAZT x
O
I,I
*******************************************************************/
case CURLYX: {
/* XXXX Probably it is better to teach regpush to support
parenfloor > PL_regsize... */
if (parenfloor > *PL_reglastparen)
saySAME(n);
}
/* NOT REACHED */
case WHILEM: {
/*
* This is really hard to understand, because after we match
* what we're trying to match, we must make sure the rest of
* the REx is going to match for sure, and to do that we have
* to go back UP the parse tree by recursing ever deeper. And
* if it fails, we have to reset our parent's current state
* that we can try again after backing off.
*/
"%*s %ld out of %ld..%ld cc=%lx\n",
);
/* If degenerate scan matches "", assume scan done. */
if (PL_regcc)
"%*s empty match detected, try continuation...\n",
);
if (PL_regcc)
}
/* First just match a string of min scans. */
}
/* Check whether we already were at this position.
Postpone detection until we know the match is not
*that* much linear. */
if (!PL_reg_maxiter) {
}
if (PL_reg_leftiter-- == 0) {
if (PL_reg_poscache) {
if (PL_reg_poscache_size < size) {
}
}
else {
}
"%sDetected a super-linear match, switching on caching%s...\n",
);
}
if (PL_reg_leftiter < 0) {
b = o % 8;
o /= 8;
if (PL_reg_poscache[o] & (1<<b)) {
"%*s already tried at this position...\n",
);
}
PL_reg_poscache[o] |= (1<<b);
}
}
/* Prefer next over scan for minimal matching. */
if (PL_regcc)
sayYES; /* All done. */
}
regcppop();
if (PL_regcc)
&& !(PL_reg_flags & RF_warned)) {
"Complex regular subexpression recursion",
REG_INFTY - 1);
}
}
"%*s trying longer...\n",
);
/* Try scanning more and see if it helps. */
}
regcppop();
}
/* Prefer scan over next for maximal matching. */
}
regcppop(); /* Restore some previous $<digit>s? */
"%*s failed, try continuation...\n",
);
}
&& !(PL_reg_flags & RF_warned)) {
"Complex regular subexpression recursion",
REG_INFTY - 1);
}
/* Failed deeper matches of scan, so see if this one works. */
if (PL_regcc)
if (PL_regcc)
}
/* NOT REACHED */
case BRANCHJ:
goto do_branch;
case BRANCH:
{
else {
/* Put unwinding data on stack */
#ifdef DEBUGGING
#endif
/* Now go into the first branch */
}
}
break;
case MINMOD:
minmod = 1;
break;
case CURLYM:
{
I32 l = 0;
/* We suppose that the next guy does not need
backtracking: in particular, it is of constant length,
and has no parenths to influence future backrefs. */
if (paren) {
if (paren > PL_regsize)
PL_regsize = paren;
if (paren > *PL_reglastparen)
*PL_reglastparen = paren;
}
if (paren)
if (minmod) {
minmod = 0;
/* In fact, this is tricky. If paren, then the
future execution. */
ln = n;
else
}
else
/* This may be improved if l == 0. */
/* If it could work, try it. */
if (c1 == -1000 ||
{
if (paren) {
if (n) {
}
else
}
}
/* Couldn't or didn't -- move forward. */
ln++;
}
else
}
}
else {
n = regrepeat_hard(scan, n, &l);
if (n != 0 && l == 0
/* In fact, this is tricky. If paren, then the
future execution. */
ln = n;
);
if (n >= ln) {
else
}
else
}
while (n >= ln) {
/* If it could work, try it. */
if (c1 == -1000 ||
{
);
if (paren) {
if (n) {
}
else
}
}
/* Couldn't or didn't -- back up. */
n--;
}
}
break;
}
case CURLYN:
if (paren > PL_regsize)
PL_regsize = paren;
if (paren > *PL_reglastparen)
*PL_reglastparen = paren;
goto repeat;
case CURLY:
paren = 0;
goto repeat;
case STAR:
ln = 0;
n = REG_INFTY;
paren = 0;
goto repeat;
case PLUS:
ln = 1;
n = REG_INFTY;
paren = 0;
/*
* Lookahead to avoid useless match attempts
* when we know what character comes next.
*/
else
}
else
if (minmod) {
minmod = 0;
if (c1 != -1000) {
e = PL_regeol - 1;
while (1) {
/* Find place 'next' could work */
locinput++;
} else {
while (locinput <= e
locinput++;
}
if (locinput > e)
/* PL_reginput == old now */
}
/* PL_reginput == locinput now */
/* Couldn't or didn't -- move forward. */
}
}
else
/* If it could work, try it. */
if (c1 == -1000 ||
{
}
/* Couldn't or didn't -- move forward. */
ln++;
}
else
}
}
else {
ln = n; /* why back off? */
/* ...because $ and \Z can match before *and* after
newline at the end. Consider "\n\n" =~ /\n+\Z\n/.
We should back off by one in this case. */
ln--;
}
if (paren) {
while (n >= ln) {
/* If it could work, try it. */
if (c1 == -1000 ||
{
}
/* Couldn't or didn't -- back up. */
n--;
}
}
else {
while (n >= ln) {
/* If it could work, try it. */
if (c1 == -1000 ||
{
}
/* Couldn't or didn't -- back up. */
n--;
}
}
}
break;
case END:
if (PL_reg_call_cc) {
the caller. */
the callcc. */
}
regcppop();
"%*s continuation failed...\n",
);
}
if (locinput < PL_regtill) {
"%sMatch possible, but length=%ld is smaller than requested=%ld, failing!%s\n",
PL_colors[4],
(long)(locinput - PL_reg_starttry),
(long)(PL_regtill - PL_reg_starttry),
PL_colors[5]));
sayNO_FINAL; /* Cannot match: too short. */
}
sayYES_FINAL; /* Success! */
case SUCCEED:
sayYES_LOUD; /* Success! */
case SUSPEND:
n = 1;
goto do_ifmatch;
case UNLESSM:
n = 0;
if (UTF) { /* XXXX This is absolutely
broken, we read before
start of string. */
if (!s)
goto say_yes;
PL_reginput = s;
}
else {
goto say_yes;
goto do_ifmatch;
}
}
else
goto do_ifmatch;
case IFMATCH:
n = 1;
if (UTF) { /* XXXX This is absolutely
broken, we read before
start of string. */
if (!s || s < PL_bostr)
goto say_no;
PL_reginput = s;
}
else {
goto say_no;
goto do_ifmatch;
}
}
else
if (logical) {
logical = 0;
sw = 0;
goto do_longjump;
}
else
}
if (logical) {
logical = 0;
sw = 1;
}
}
/* FALL THROUGH. */
case LONGJMP:
break;
default:
}
}
/*
* We get here only if there's trouble -- normally "case END" is
* the terminating point.
*/
/*NOTREACHED*/
"%*s %scould match...%s\n",
);
goto yes;
yes:
#ifdef DEBUGGING
PL_regindent--;
#endif
#if 0 /* Breaks $^R */
if (unwind)
#endif
return 1;
no:
"%*s %sfailed...%s\n",
);
goto do_no;
if (unwind) {
case RE_UNWIND_BRANCH:
case RE_UNWIND_BRANCHJ:
{
for (n = *PL_reglastparen; n > lastparen; n--)
PL_regendp[n] = -1;
*PL_reglastparen = n;
if ( !scan ||
#ifdef DEBUGGING
PL_regindent--;
#endif
goto do_no;
}
/* Have more choice yet. Reuse the same uwb. */
/*SUPPRESS 560*/
next += n;
else
#ifdef DEBUGGING
#endif
goto reenter;
}
/* NOT REACHED */
default:
}
/* NOT REACHED */
}
#ifdef DEBUGGING
PL_regindent--;
#endif
return 0;
}
/*
- regrepeat - repeatedly match something simple, report how many
*/
/*
* [This routine now assumes that it will only match on things of length 1.
* That was true before, but now we assume scan - reginput is the count,
* rather than incrementing count on every character. [Er, except utf8.]]
*/
{
register char *scan;
register I32 c;
scan = PL_reginput;
switch (OP(p)) {
case REG_ANY:
scan++;
break;
case SANY:
break;
case ANYUTF8:
hardcount++;
}
break;
case SANYUTF8:
hardcount++;
}
break;
case EXACT: /* length of string is 1 */
scan++;
break;
case EXACTF: /* length of string is 1 */
scan++;
break;
case EXACTFL: /* length of string is 1 */
scan++;
break;
case ANYOFUTF8:
hardcount++;
}
break;
case ANYOF:
scan++;
break;
case ALNUM:
scan++;
break;
case ALNUMUTF8:
hardcount++;
}
break;
case ALNUML:
scan++;
break;
case ALNUMLUTF8:
hardcount++;
}
break;
break;
case NALNUM:
scan++;
break;
case NALNUMUTF8:
hardcount++;
}
break;
case NALNUML:
scan++;
break;
case NALNUMLUTF8:
hardcount++;
}
break;
case SPACE:
scan++;
break;
case SPACEUTF8:
hardcount++;
}
break;
case SPACEL:
scan++;
break;
case SPACELUTF8:
hardcount++;
}
break;
case NSPACE:
scan++;
break;
case NSPACEUTF8:
hardcount++;
}
break;
case NSPACEL:
scan++;
break;
case NSPACELUTF8:
hardcount++;
}
break;
case DIGIT:
scan++;
break;
case DIGITUTF8:
hardcount++;
}
break;
break;
case NDIGIT:
scan++;
break;
case NDIGITUTF8:
hardcount++;
}
break;
default: /* Called on something of 0 width. */
break; /* So match right here or not at all. */
}
if (hardcount)
c = hardcount;
else
c = scan - PL_reginput;
PL_reginput = scan;
{
});
return(c);
}
/*
- regrepeat_hard - repeatedly match something, report total lenth and length
*
* The repeater is supposed to have constant length.
*/
{
register char *scan;
register char *start;
I32 l = 0;
if (!max)
return 0;
start = PL_reginput;
if (UTF) {
if (!count++) {
l = 0;
while (start < PL_reginput) {
l++;
}
*lp = l;
if (l == 0)
return max;
}
return count;
}
}
else {
if (!count++) {
if (l == 0)
return max;
}
}
}
if (!res)
PL_reginput = scan;
return count;
}
/*
- reginclass - determine if a character falls into a character class
*/
STATIC bool
{
char flags = ANYOF_FLAGS(p);
c &= 0xFF;
if (ANYOF_BITMAP_TEST(p, c))
else if (flags & ANYOF_FOLD) {
if (flags & ANYOF_LOCALE) {
cf = PL_fold_locale[c];
}
else
if (ANYOF_BITMAP_TEST(p, cf))
}
if (
) /* How's that for a conditional? */
{
}
}
}
STATIC bool
{
#ifdef DEBUGGING
#else
#endif
if (swash_fetch(sw, p))
else if (flags & ANYOF_FOLD) {
if (flags & ANYOF_LOCALE) {
}
else
}
/* UTF8 combined with ANYOF_CLASS is ill-defined. */
}
{
if (off >= 0) {
s += UTF8SKIP(s);
}
else {
while (off++) {
s--;
if (*s & 0x80) {
s--;
} /* XXX could check well-formedness here */
}
}
}
return s;
}
{
if (off >= 0) {
s += UTF8SKIP(s);
if (off >= 0)
return 0;
}
else {
while (off++) {
s--;
if (*s & 0x80) {
s--;
} /* XXX could check well-formedness here */
}
else
break;
}
if (off <= 0)
return 0;
}
return s;
}
#ifdef PERL_OBJECT
#include "XSUB.h"
#endif
static void
{
if (PL_reg_eval_set) {
if (PL_reg_oldsaved) {
}
PL_reg_eval_set = 0;
}
}