/* *longest-longest-preferredmatchingengine * *Onsuccess,returnsmatchendpointaddress.ReturnsNULLonnomatch. *InternalerrorsalsoreturnNULL,withv->errset.
*/ static chr *
longest(struct vars *v, struct dfa *d,
chr *start, /* where the match should start */
chr *stop, /* match must end at or before here */ int *hitstopp) /* record whether hit v->stop, if non-NULL */
{
chr *cp;
chr *realstop = (stop == v->stop) ? stop : stop + 1;
color co; struct sset *css; struct sset *ss;
chr *post; int i; struct colormap *cm = d->cm;
/* if this is a backref to a known string, just match against that */ if (d->backno >= 0)
{
assert((size_t) d->backno < v->nmatch); if (v->pmatch[d->backno].rm_so >= 0)
{
cp = dfa_backref(v, d, start, start, stop, false); if (cp == v->stop && stop == v->stop && hitstopp != NULL)
*hitstopp = 1; return cp;
}
}
/* fast path for matchall NFAs */ if (d->cnfa->flags & MATCHALL)
{
size_t nchr = stop - start;
size_t maxmatchall = d->cnfa->maxmatchall;
/* *Thisisthemaintext-scanningloop.Itseemsworthhavingtwocopies *toavoidtheoverheadofREG_FTRACEtestshere,eveninREG_DEBUG *builds,whenyou'renotactivelytracing.
*/ #ifdef REG_DEBUG if (v->eflags & REG_FTRACE)
{ while (cp < realstop)
{
FDEBUG(("+++ at c%d +++\n", (int) (css - d->ssets)));
co = GETCOLOR(cm, *cp);
FDEBUG(("char %c, color %ld\n", (char) *cp, (long) co));
ss = css->outs[co]; if (ss == NULL)
{
ss = miss(v, d, css, co, cp + 1, start); if (ss == NULL) break; /* NOTE BREAK OUT */
}
cp++;
ss->lastseen = cp;
css = ss;
}
} else #endif
{ while (cp < realstop)
{
co = GETCOLOR(cm, *cp);
ss = css->outs[co]; if (ss == NULL)
{
ss = miss(v, d, css, co, cp + 1, start); if (ss == NULL) break; /* NOTE BREAK OUT */
}
cp++;
ss->lastseen = cp;
css = ss;
}
}
if (ISERR()) return NULL;
/* shutdown */
FDEBUG(("+++ shutdown at c%d +++\n", (int) (css - d->ssets))); if (cp == v->stop && stop == v->stop)
{ if (hitstopp != NULL)
*hitstopp = 1;
co = d->cnfa->eos[(v->eflags & REG_NOTEOL) ? 0 : 1];
FDEBUG(("color %ld\n", (long) co));
ss = miss(v, d, css, co, cp, start); if (ISERR()) return NULL; /* special case: match ended at eol? */ if (ss != NULL && (ss->flags & POSTSTATE)) return cp; elseif (ss != NULL)
ss->lastseen = cp; /* to be tidy */
}
/* find last match, if any */
post = d->lastpost; for (ss = d->ssets, i = d->nssused; i > 0; ss++, i--) if ((ss->flags & POSTSTATE) && post != ss->lastseen &&
(post == NULL || post < ss->lastseen))
post = ss->lastseen; if (post != NULL) /* found one */ return post - 1;
return NULL;
}
/* *shortest-shortest-preferredmatchingengine * *Onsuccess,returnsmatchendpointaddress.ReturnsNULLonnomatch. *InternalerrorsalsoreturnNULL,withv->errset.
*/ static chr *
shortest(struct vars *v, struct dfa *d,
chr *start, /* where the match should start */
chr *min, /* match must end at or after here */
chr *max, /* match must end at or before here */
chr **coldp, /* store coldstart pointer here, if non-NULL */ int *hitstopp) /* record whether hit v->stop, if non-NULL */
{
chr *cp;
chr *realmin = (min == v->stop) ? min : min + 1;
chr *realmax = (max == v->stop) ? max : max + 1;
color co; struct sset *css; struct sset *ss; struct colormap *cm = d->cm;
/* prevent "uninitialized variable" warnings */ if (coldp != NULL)
*coldp = NULL; if (hitstopp != NULL)
*hitstopp = 0;
/* if this is a backref to a known string, just match against that */ if (d->backno >= 0)
{
assert((size_t) d->backno < v->nmatch); if (v->pmatch[d->backno].rm_so >= 0)
{
cp = dfa_backref(v, d, start, min, max, true); if (cp != NULL && coldp != NULL)
*coldp = start; /* there is no case where we should set *hitstopp */ return cp;
}
}
/* fast path for matchall NFAs */ if (d->cnfa->flags & MATCHALL)
{
size_t nchr = min - start;
if (d->cnfa->maxmatchall != DUPINF &&
nchr > d->cnfa->maxmatchall) return NULL; if ((max - start) < d->cnfa->minmatchall) return NULL; if (nchr < d->cnfa->minmatchall)
min = start + d->cnfa->minmatchall; if (coldp != NULL)
*coldp = start; /* there is no case where we should set *hitstopp */ return min;
}
css = miss(v, d, css, co, cp, v->start); if (css == NULL) return0;
css->lastseen = cp;
} elseif (css == NULL)
{ /* we previously found that no match is possible beyond *lastcp */ return0;
}
ss = css;
/* *Thisisthemaintext-scanningloop.Itseemsworthhavingtwocopies *toavoidtheoverheadofREG_FTRACEtestshere,eveninREG_DEBUG *builds,whenyou'renotactivelytracing.
*/ #ifdef REG_DEBUG if (v->eflags & REG_FTRACE)
{ while (cp < probe)
{
FDEBUG((">>> at c%d >>>\n", (int) (css - d->ssets)));
co = GETCOLOR(cm, *cp);
FDEBUG(("char %c, color %ld\n", (char) *cp, (long) co));
ss = css->outs[co]; if (ss == NULL)
{
ss = miss(v, d, css, co, cp + 1, v->start); if (ss == NULL) break; /* NOTE BREAK OUT */
}
cp++;
ss->lastseen = cp;
css = ss;
}
} else #endif
{ while (cp < probe)
{
co = GETCOLOR(cm, *cp);
ss = css->outs[co]; if (ss == NULL)
{
ss = miss(v, d, css, co, cp + 1, v->start); if (ss == NULL) break; /* NOTE BREAK OUT */
}
cp++;
ss->lastseen = cp;
css = ss;
}
}
*lastcss = ss;
*lastcp = cp;
if (ss == NULL) return0; /* impossible match, or internal error */
/* We need to process one more chr, or the EOS symbol, to check match */ if (cp < v->stop)
{
FDEBUG((">>> at c%d >>>\n", (int) (css - d->ssets)));
co = GETCOLOR(cm, *cp);
FDEBUG(("char %c, color %ld\n", (char) *cp, (long) co));
ss = css->outs[co]; if (ss == NULL)
ss = miss(v, d, css, co, cp + 1, v->start);
} else
{
assert(cp == v->stop);
co = d->cnfa->eos[(v->eflags & REG_NOTEOL) ? 0 : 1];
FDEBUG(("color %ld\n", (long) co));
ss = miss(v, d, css, co, cp, v->start);
}
if (ss == NULL || !(ss->flags & POSTSTATE)) return0;
return1;
}
/* *dfa_backref-findbestmatchlengthforaknownbackrefstring * *Whenthebackref'sreferentisalreadyavailable,wecandeliveranexact *answerwithconsiderablylessworkthanrunningthebackrefnode'sNFA. * *Returnmatchendpointforlongestorshortestvalidrepeatedmatch, *orNULLifthereisnovalidmatch. * *Shouldbeinsyncwithcbrdissect(),althoughthathasthedifferenttask *ofcheckingamatchtoapredeterminedsectionofthestring.
*/ static chr *
dfa_backref(struct vars *v, struct dfa *d,
chr *start, /* where the match should start */
chr *min, /* match must end at or after here */
chr *max, /* match must end at or before here */ bool shortest)
{ int n = d->backno; int backmin = d->backmin; int backmax = d->backmax;
size_t numreps;
size_t minreps;
size_t maxreps;
size_t brlen;
chr *brstring;
chr *p;
/* get the backreferenced string (caller should have checked this) */ if (v->pmatch[n].rm_so == -1) return NULL;
brstring = v->start + v->pmatch[n].rm_so;
brlen = v->pmatch[n].rm_eo - v->pmatch[n].rm_so;
/* special-case zero-length backreference to avoid divide by zero */ if (brlen == 0)
{ /* *matchesonlyazero-lengthstring,butanynumberofrepetitions *canbeconsideredtobepresent
*/ if (min == start && backmin <= backmax) return start; return NULL;
}
/* apply bounds, then see if there is any allowed match length */ if (minreps < backmin)
minreps = backmin; if (backmax != DUPINF && maxreps > backmax)
maxreps = backmax; if (maxreps < minreps) return NULL;
/* quick exit if zero-repetitions match is valid and preferred */ if (shortest && minreps == 0) return start;
/* okay, compare the actual string contents */
p = start;
numreps = 0; while (numreps < maxreps)
{ if ((*v->g->compare) (brstring, p, brlen) != 0) break;
p += brlen;
numreps++; if (shortest && numreps >= minreps) break;
}
if (numreps >= minreps) return p; return NULL;
}
/* *lastcold-determinelastpointatwhichnoprogresshadbeenmade
*/ static chr * /* endpoint, or NULL */
lastcold(struct vars *v, struct dfa *d)
{ struct sset *ss;
chr *nopr; int i;
nopr = d->lastnopr; if (nopr == NULL)
nopr = v->start; for (ss = d->ssets, i = d->nssused; i > 0; ss++, i--) if ((ss->flags & NOPROGRESS) && nopr < ss->lastseen)
nopr = ss->lastseen; return nopr;
}
/* initialization of sset fields is done as needed */
return d;
}
/* *freedfa-freeaDFA
*/ staticvoid
freedfa(struct dfa *d)
{ if (d->arraysmalloced)
{ if (d->ssets != NULL)
FREE(d->ssets); if (d->statesarea != NULL)
FREE(d->statesarea); if (d->outsarea != NULL)
FREE(d->outsarea); if (d->incarea != NULL)
FREE(d->incarea);
}
if (d->ismalloced)
FREE(d);
}
/* *hash-constructahashcodeforabitvector * *Thereareprobablybetterways,butthey'remoreexpensive.
*/ staticunsigned
hash(unsigned *uv, int n)
{ int i; unsigned h;
h = 0; for (i = 0; i < n; i++)
h ^= uv[i]; return h;
}
/* *initialize-hand-craftacacheentryforstartup,otherwisegetready
*/ staticstruct sset *
initialize(struct vars *v, struct dfa *d,
chr *start)
{ struct sset *ss; int i;
/* is previous one still there? */ if (d->nssused > 0 && (d->ssets[0].flags & STARTER))
ss = &d->ssets[0]; else
{ /* no, must (re)build it */
ss = getvacant(v, d, start, start); if (ss == NULL) return NULL; for (i = 0; i < d->wordsper; i++)
ss->states[i] = 0;
BSET(ss->states, d->cnfa->pre);
ss->hash = HASH(ss->states, d->wordsper);
assert(d->cnfa->pre != d->cnfa->post);
ss->flags = STARTER | LOCKED | NOPROGRESS; /* lastseen dealt with below */
}
for (i = 0; i < d->nssused; i++)
d->ssets[i].lastseen = NULL;
ss->lastseen = start; /* maybe untrue, but harmless */
d->lastpost = NULL;
d->lastnopr = NULL; return ss;
}
/* *miss-handleastatesetcachemiss * *cssisthecurrentstateset,coisthecolorofthecurrentinputcharacter, *cppointstothecharacterafterthat(whichiswherewemayneedtotest *LACONs).startdoesnotaffectmatchingbehaviorbutisneededforpickss' *heuristicsaboutwhichstatesetcacheentrytoreplace. * *Ordinarily,returnstheaddressofthenextstateset(theonethatis *validafterconsumingtheinputcharacter).ReturnsNULLifnovalid *NFAstatesremain,iewehaveacertainmatchfailure. *InternalerrorsalsoreturnNULL,withv->errset.
*/ staticstruct sset *
miss(struct vars *v, struct dfa *d, struct sset *css,
color co,
chr *cp, /* next chr */
chr *start) /* where the attempt got started */
{ struct cnfa *cnfa = d->cnfa; int i; unsigned h; struct carc *ca; struct sset *p; int ispseudocolor; int ispost; int noprogress; int gotstate; int dolacons; int sawlacons;
/* for convenience, we can be called even if it might not be a miss */ if (css->outs[co] != NULL)
{
FDEBUG(("hit\n")); return css->outs[co];
}
FDEBUG(("miss\n"));
/* *Whatsetofstateswouldweendupinafterconsumingthecocharacter? *WefirstconsiderPLAINarcsthatconsumethecharacter,andthenlook *toseewhatLACONarcscouldbetraversedafterconsumingit.
*/ for (i = 0; i < d->wordsper; i++)
d->work[i] = 0; /* build new stateset bitmap in d->work */
ispseudocolor = d->cm->cd[co].flags & PSEUDO;
ispost = 0;
noprogress = 1;
gotstate = 0; for (i = 0; i < d->nstates; i++) if (ISBSET(css->states, i)) for (ca = cnfa->states[i]; ca->co != COLORLESS; ca++) if (ca->co == co ||
(ca->co == RAINBOW && !ispseudocolor))
{
BSET(d->work, ca->to);
gotstate = 1; if (ca->to == cnfa->post)
ispost = 1; if (!(cnfa->stflags[ca->to] & CNFA_NOPROGRESS))
noprogress = 0;
FDEBUG(("%d -> %d\n", i, ca->to));
} if (!gotstate) return NULL; /* character cannot reach any new state */
dolacons = (cnfa->flags & HASLACONS);
sawlacons = 0; /* outer loop handles transitive closure of reachable-by-LACON states */ while (dolacons)
{
dolacons = 0; for (i = 0; i < d->nstates; i++) if (ISBSET(d->work, i)) for (ca = cnfa->states[i]; ca->co != COLORLESS; ca++)
{ if (ca->co < cnfa->ncolors) continue; /* not a LACON arc */ if (ISBSET(d->work, ca->to)) continue; /* arc would be a no-op anyway */
sawlacons = 1; /* this LACON affects our result */ if (!lacon(v, cnfa, cp, ca->co))
{ if (ISERR()) return NULL; continue; /* LACON arc cannot be traversed */
} if (ISERR()) return NULL;
BSET(d->work, ca->to);
dolacons = 1; if (ca->to == cnfa->post)
ispost = 1; if (!(cnfa->stflags[ca->to] & CNFA_NOPROGRESS))
noprogress = 0;
FDEBUG(("%d :> %d\n", i, ca->to));
}
}
h = HASH(d->work, d->wordsper);
/* Is this stateset already in the cache? */ for (p = d->ssets, i = d->nssused; i > 0; p++, i--) if (HIT(h, d->work, p, d->wordsper))
{
FDEBUG(("cached c%d\n", (int) (p - d->ssets))); break; /* NOTE BREAK OUT */
} if (i == 0)
{ /* nope, need a new cache entry */
p = getvacant(v, d, cp, start); if (p == NULL) return NULL;
assert(p != css); for (i = 0; i < d->wordsper; i++)
p->states[i] = d->work[i];
p->hash = h;
p->flags = (ispost) ? POSTSTATE : 0; if (noprogress)
p->flags |= NOPROGRESS; /* lastseen to be dealt with by caller */
}
ss = pickss(v, d, cp, start); if (ss == NULL) return NULL;
assert(!(ss->flags & LOCKED));
/* clear out its inarcs, including self-referential ones */
ap = ss->ins; while ((p = ap.ss) != NULL)
{
co = ap.co;
FDEBUG(("zapping c%d's %ld outarc\n", (int) (p - d->ssets), (long) co));
p->outs[co] = NULL;
ap = p->inchain[co];
p->inchain[co].ss = NULL; /* paranoia */
}
ss->ins.ss = NULL;
/* take it off the inarc chains of the ssets reached by its outarcs */ for (i = 0; i < d->ncolors; i++)
{
p = ss->outs[i];
assert(p != ss); /* not self-referential */ if (p == NULL) continue; /* NOTE CONTINUE */
FDEBUG(("del outarc %d from c%d's in chn\n", i, (int) (p - d->ssets))); if (p->ins.ss == ss && p->ins.co == i)
p->ins = ss->inchain[i]; else
{ struct arcp lastap = {NULL, 0};
assert(p->ins.ss != NULL); for (ap = p->ins; ap.ss != NULL &&
!(ap.ss == ss && ap.co == i);
ap = ap.ss->inchain[ap.co])
lastap = ap;
assert(ap.ss != NULL);
lastap.ss->inchain[lastap.co] = ss->inchain[i];
}
ss->outs[i] = NULL;
ss->inchain[i].ss = NULL;
}
/* if ss was a success state, may need to remember location */ if ((ss->flags & POSTSTATE) && ss->lastseen != d->lastpost &&
(d->lastpost == NULL || d->lastpost < ss->lastseen))
d->lastpost = ss->lastseen;
/* likewise for a no-progress state */ if ((ss->flags & NOPROGRESS) && ss->lastseen != d->lastnopr &&
(d->lastnopr == NULL || d->lastnopr < ss->lastseen))
d->lastnopr = ss->lastseen;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.