| 1 |
/* |
| 2 |
* re_*exec and friends - match REs |
| 3 |
* |
| 4 |
* Copyright (c) 1998, 1999 Henry Spencer. All rights reserved. |
| 5 |
* |
| 6 |
* Development of this software was funded, in part, by Cray Research Inc., |
| 7 |
* UUNET Communications Services Inc., Sun Microsystems Inc., and Scriptics |
| 8 |
* Corporation, none of whom are responsible for the results. The author |
| 9 |
* thanks all of them. |
| 10 |
* |
| 11 |
* Redistribution and use in source and binary forms -- with or without |
| 12 |
* modification -- are permitted for any purpose, provided that |
| 13 |
* redistributions in source form retain this entire copyright notice and |
| 14 |
* indicate the origin and nature of any modifications. |
| 15 |
* |
| 16 |
* I'd appreciate being given credit for this package in the documentation of |
| 17 |
* software which uses it, but that is not a requirement. |
| 18 |
* |
| 19 |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 20 |
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY |
| 21 |
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 22 |
* HENRY SPENCER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 23 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 24 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 25 |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 26 |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 27 |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 28 |
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 |
*/ |
| 30 |
|
| 31 |
#include "regguts.h" |
| 32 |
|
| 33 |
/* |
| 34 |
* Lazy-DFA representation. |
| 35 |
*/ |
| 36 |
|
| 37 |
struct arcp { /* "pointer" to an outarc */ |
| 38 |
struct sset *ss; |
| 39 |
color co; |
| 40 |
}; |
| 41 |
|
| 42 |
struct sset { /* state set */ |
| 43 |
unsigned *states; /* pointer to bitvector */ |
| 44 |
unsigned hash; /* hash of bitvector */ |
| 45 |
#define HASH(bv, nw) (((nw) == 1) ? *(bv) : hash(bv, nw)) |
| 46 |
#define HIT(h,bv,ss,nw) ((ss)->hash == (h) && ((nw) == 1 || \ |
| 47 |
memcmp(VS(bv), VS((ss)->states), (nw)*sizeof(unsigned)) == 0)) |
| 48 |
int flags; |
| 49 |
#define STARTER 01 /* the initial state set */ |
| 50 |
#define POSTSTATE 02 /* includes the goal state */ |
| 51 |
#define LOCKED 04 /* locked in cache */ |
| 52 |
#define NOPROGRESS 010 /* zero-progress state set */ |
| 53 |
struct arcp ins; /* chain of inarcs pointing here */ |
| 54 |
chr *lastseen; /* last entered on arrival here */ |
| 55 |
struct sset **outs; /* outarc vector indexed by color */ |
| 56 |
struct arcp *inchain; /* chain-pointer vector for outarcs */ |
| 57 |
}; |
| 58 |
|
| 59 |
struct dfa { |
| 60 |
int nssets; /* size of cache */ |
| 61 |
int nssused; /* how many entries occupied yet */ |
| 62 |
int nstates; /* number of states */ |
| 63 |
int ncolors; /* length of outarc and inchain vectors */ |
| 64 |
int wordsper; /* length of state-set bitvectors */ |
| 65 |
struct sset *ssets; /* state-set cache */ |
| 66 |
unsigned *statesarea; /* bitvector storage */ |
| 67 |
unsigned *work; /* pointer to work area within statesarea */ |
| 68 |
struct sset **outsarea; /* outarc-vector storage */ |
| 69 |
struct arcp *incarea; /* inchain storage */ |
| 70 |
struct cnfa *cnfa; |
| 71 |
struct colormap *cm; |
| 72 |
chr *lastpost; /* location of last cache-flushed success */ |
| 73 |
chr *lastnopr; /* location of last cache-flushed NOPROGRESS */ |
| 74 |
struct sset *search; /* replacement-search-pointer memory */ |
| 75 |
int cptsmalloced; /* were the areas individually malloced? */ |
| 76 |
char *mallocarea; /* self, or malloced area, or NULL */ |
| 77 |
}; |
| 78 |
|
| 79 |
#define WORK 1 /* number of work bitvectors needed */ |
| 80 |
|
| 81 |
/* |
| 82 |
* Setup for non-malloc allocation for small cases. |
| 83 |
*/ |
| 84 |
|
| 85 |
#define FEWSTATES 20 /* must be less than UBITS */ |
| 86 |
#define FEWCOLORS 15 |
| 87 |
struct smalldfa { |
| 88 |
struct dfa dfa; |
| 89 |
struct sset ssets[FEWSTATES*2]; |
| 90 |
unsigned statesarea[FEWSTATES*2 + WORK]; |
| 91 |
struct sset *outsarea[FEWSTATES*2 * FEWCOLORS]; |
| 92 |
struct arcp incarea[FEWSTATES*2 * FEWCOLORS]; |
| 93 |
}; |
| 94 |
#define DOMALLOC ((struct smalldfa *)NULL) /* force malloc */ |
| 95 |
|
| 96 |
/* |
| 97 |
* Internal variables, bundled for easy passing around. |
| 98 |
*/ |
| 99 |
|
| 100 |
struct vars { |
| 101 |
regex_t *re; |
| 102 |
struct guts *g; |
| 103 |
int eflags; /* copies of arguments */ |
| 104 |
size_t nmatch; |
| 105 |
regmatch_t *pmatch; |
| 106 |
rm_detail_t *details; |
| 107 |
chr *start; /* start of string */ |
| 108 |
chr *stop; /* just past end of string */ |
| 109 |
int err; /* error code if any (0 none) */ |
| 110 |
struct dfa **subdfas; /* per-subre DFAs */ |
| 111 |
struct smalldfa dfa1; |
| 112 |
struct smalldfa dfa2; |
| 113 |
}; |
| 114 |
#define VISERR(vv) ((vv)->err != 0) /* have we seen an error yet? */ |
| 115 |
#define ISERR() VISERR(v) |
| 116 |
#define VERR(vv,e) ((vv)->err = ((vv)->err ? (vv)->err : (e))) |
| 117 |
#define ERR(e) VERR(v, e) /* record an error */ |
| 118 |
#define NOERR() {if (ISERR()) return v->err;} /* if error seen, return it */ |
| 119 |
#define OFF(p) ((p) - v->start) |
| 120 |
#define LOFF(p) ((long)OFF(p)) |
| 121 |
|
| 122 |
/* |
| 123 |
* forward declarations |
| 124 |
*/ |
| 125 |
/* =====^!^===== begin forwards =====^!^===== */ |
| 126 |
/* automatically gathered by fwd; do not hand-edit */ |
| 127 |
/* === regexec.c === */ |
| 128 |
int exec(regex_t *, const chr *, size_t, rm_detail_t *, size_t, regmatch_t [], int); |
| 129 |
static struct dfa *getsubdfa(struct vars *, struct subre *); |
| 130 |
static int simpleFind(struct vars *const, struct cnfa *const, struct colormap *const); |
| 131 |
static int complicatedFind(struct vars *const, struct cnfa *const, struct colormap *const); |
| 132 |
static int complicatedFindLoop(struct vars *const, struct dfa *const, struct dfa *const, chr **const); |
| 133 |
static void zapallsubs(regmatch_t *const, const size_t); |
| 134 |
static void zaptreesubs(struct vars *const, struct subre *const); |
| 135 |
static void subset(struct vars *const, struct subre *const, chr *const, chr *const); |
| 136 |
static int cdissect(struct vars *, struct subre *, chr *, chr *); |
| 137 |
static int ccondissect(struct vars *, struct subre *, chr *, chr *); |
| 138 |
static int crevcondissect(struct vars *, struct subre *, chr *, chr *); |
| 139 |
static int cbrdissect(struct vars *, struct subre *, chr *, chr *); |
| 140 |
static int caltdissect(struct vars *, struct subre *, chr *, chr *); |
| 141 |
static int citerdissect(struct vars *, struct subre *, chr *, chr *); |
| 142 |
static int creviterdissect(struct vars *, struct subre *, chr *, chr *); |
| 143 |
/* === rege_dfa.c === */ |
| 144 |
static chr *longest(struct vars *const, struct dfa *const, chr *const, chr *const, int *const); |
| 145 |
static chr *shortest(struct vars *const, struct dfa *const, chr *const, chr *const, chr *const, chr **const, int *const); |
| 146 |
static chr *lastCold(struct vars *const, struct dfa *const); |
| 147 |
static struct dfa *newDFA(struct vars *const, struct cnfa *const, struct colormap *const, struct smalldfa *); |
| 148 |
static void freeDFA(struct dfa *const); |
| 149 |
static unsigned hash(unsigned *const, const int); |
| 150 |
static struct sset *initialize(struct vars *const, struct dfa *const, chr *const); |
| 151 |
static struct sset *miss(struct vars *const, struct dfa *const, struct sset *const, const pcolor, chr *const, chr *const); |
| 152 |
static int checkLAConstraint(struct vars *const, struct cnfa *const, chr *const, const pcolor); |
| 153 |
static struct sset *getVacantSS(struct vars *const, struct dfa *const, chr *const, chr *const); |
| 154 |
static struct sset *pickNextSS(struct vars *const, struct dfa *const, chr *const, chr *const); |
| 155 |
/* automatically gathered by fwd; do not hand-edit */ |
| 156 |
/* =====^!^===== end forwards =====^!^===== */ |
| 157 |
|
| 158 |
/* |
| 159 |
- exec - match regular expression |
| 160 |
^ int exec(regex_t *, const chr *, size_t, rm_detail_t *, |
| 161 |
^ size_t, regmatch_t [], int); |
| 162 |
*/ |
| 163 |
int |
| 164 |
exec( |
| 165 |
regex_t *re, |
| 166 |
const chr *string, |
| 167 |
size_t len, |
| 168 |
rm_detail_t *details, |
| 169 |
size_t nmatch, |
| 170 |
regmatch_t pmatch[], |
| 171 |
int flags) |
| 172 |
{ |
| 173 |
AllocVars(v); |
| 174 |
int st, backref; |
| 175 |
size_t n; |
| 176 |
size_t i; |
| 177 |
#define LOCALMAT 20 |
| 178 |
regmatch_t mat[LOCALMAT]; |
| 179 |
#define LOCALDFAS 40 |
| 180 |
struct dfa *subdfas[LOCALDFAS]; |
| 181 |
|
| 182 |
/* |
| 183 |
* Sanity checks. |
| 184 |
*/ |
| 185 |
|
| 186 |
if (re == NULL || string == NULL || re->re_magic != REMAGIC) { |
| 187 |
FreeVars(v); |
| 188 |
return REG_INVARG; |
| 189 |
} |
| 190 |
if (re->re_csize != sizeof(chr)) { |
| 191 |
FreeVars(v); |
| 192 |
return REG_MIXED; |
| 193 |
} |
| 194 |
|
| 195 |
/* |
| 196 |
* Setup. |
| 197 |
*/ |
| 198 |
|
| 199 |
v->re = re; |
| 200 |
v->g = (struct guts *)re->re_guts; |
| 201 |
if ((v->g->cflags®_EXPECT) && details == NULL) { |
| 202 |
FreeVars(v); |
| 203 |
return REG_INVARG; |
| 204 |
} |
| 205 |
if (v->g->info®_UIMPOSSIBLE) { |
| 206 |
FreeVars(v); |
| 207 |
return REG_NOMATCH; |
| 208 |
} |
| 209 |
backref = (v->g->info®_UBACKREF) ? 1 : 0; |
| 210 |
v->eflags = flags; |
| 211 |
if (v->g->cflags®_NOSUB) { |
| 212 |
nmatch = 0; /* override client */ |
| 213 |
} |
| 214 |
v->nmatch = nmatch; |
| 215 |
if (backref) { |
| 216 |
/* |
| 217 |
* Need work area. |
| 218 |
*/ |
| 219 |
|
| 220 |
if (v->g->nsub + 1 <= LOCALMAT) { |
| 221 |
v->pmatch = mat; |
| 222 |
} else { |
| 223 |
v->pmatch = (regmatch_t *) |
| 224 |
MALLOC((v->g->nsub + 1) * sizeof(regmatch_t)); |
| 225 |
} |
| 226 |
if (v->pmatch == NULL) { |
| 227 |
FreeVars(v); |
| 228 |
return REG_ESPACE; |
| 229 |
} |
| 230 |
v->nmatch = v->g->nsub + 1; |
| 231 |
} else { |
| 232 |
v->pmatch = pmatch; |
| 233 |
} |
| 234 |
v->details = details; |
| 235 |
v->start = (chr *)string; |
| 236 |
v->stop = (chr *)string + len; |
| 237 |
v->err = 0; |
| 238 |
assert(v->g->ntree >= 0); |
| 239 |
n = (size_t) v->g->ntree; |
| 240 |
if (n <= LOCALDFAS) { |
| 241 |
v->subdfas = subdfas; |
| 242 |
} else { |
| 243 |
v->subdfas = (struct dfa **) MALLOC(n * sizeof(struct dfa *)); |
| 244 |
} |
| 245 |
if (v->subdfas == NULL) { |
| 246 |
if (v->pmatch != pmatch && v->pmatch != mat) |
| 247 |
FREE(v->pmatch); |
| 248 |
FreeVars(v); |
| 249 |
return REG_ESPACE; |
| 250 |
} |
| 251 |
for (i = 0; i < n; i++) |
| 252 |
v->subdfas[i] = NULL; |
| 253 |
|
| 254 |
/* |
| 255 |
* Do it. |
| 256 |
*/ |
| 257 |
|
| 258 |
assert(v->g->tree != NULL); |
| 259 |
if (backref) { |
| 260 |
st = complicatedFind(v, &v->g->tree->cnfa, &v->g->cmap); |
| 261 |
} else { |
| 262 |
st = simpleFind(v, &v->g->tree->cnfa, &v->g->cmap); |
| 263 |
} |
| 264 |
|
| 265 |
/* |
| 266 |
* Copy (portion of) match vector over if necessary. |
| 267 |
*/ |
| 268 |
|
| 269 |
if (st == REG_OKAY && v->pmatch != pmatch && nmatch > 0) { |
| 270 |
zapallsubs(pmatch, nmatch); |
| 271 |
n = (nmatch < v->nmatch) ? nmatch : v->nmatch; |
| 272 |
memcpy(VS(pmatch), VS(v->pmatch), n*sizeof(regmatch_t)); |
| 273 |
} |
| 274 |
|
| 275 |
/* |
| 276 |
* Clean up. |
| 277 |
*/ |
| 278 |
|
| 279 |
if (v->pmatch != pmatch && v->pmatch != mat) { |
| 280 |
FREE(v->pmatch); |
| 281 |
} |
| 282 |
n = (size_t) v->g->ntree; |
| 283 |
for (i = 0; i < n; i++) { |
| 284 |
if (v->subdfas[i] != NULL) |
| 285 |
freeDFA(v->subdfas[i]); |
| 286 |
} |
| 287 |
if (v->subdfas != subdfas) |
| 288 |
FREE(v->subdfas); |
| 289 |
FreeVars(v); |
| 290 |
return st; |
| 291 |
} |
| 292 |
|
| 293 |
/* |
| 294 |
- getsubdfa - create or re-fetch the DFA for a subre node |
| 295 |
* We only need to create the DFA once per overall regex execution. |
| 296 |
* The DFA will be freed by the cleanup step in exec(). |
| 297 |
*/ |
| 298 |
static struct dfa * |
| 299 |
getsubdfa(struct vars * v, |
| 300 |
struct subre * t) |
| 301 |
{ |
| 302 |
if (v->subdfas[t->id] == NULL) { |
| 303 |
v->subdfas[t->id] = newDFA(v, &t->cnfa, &v->g->cmap, DOMALLOC); |
| 304 |
if (ISERR()) |
| 305 |
return NULL; |
| 306 |
} |
| 307 |
return v->subdfas[t->id]; |
| 308 |
} |
| 309 |
|
| 310 |
/* |
| 311 |
- simpleFind - find a match for the main NFA (no-complications case) |
| 312 |
^ static int simpleFind(struct vars *, struct cnfa *, struct colormap *); |
| 313 |
*/ |
| 314 |
static int |
| 315 |
simpleFind( |
| 316 |
struct vars *const v, |
| 317 |
struct cnfa *const cnfa, |
| 318 |
struct colormap *const cm) |
| 319 |
{ |
| 320 |
struct dfa *s, *d; |
| 321 |
chr *begin, *end = NULL; |
| 322 |
chr *cold; |
| 323 |
chr *open, *close; /* Open and close of range of possible |
| 324 |
* starts */ |
| 325 |
int hitend; |
| 326 |
int shorter = (v->g->tree->flags&SHORTER) ? 1 : 0; |
| 327 |
|
| 328 |
/* |
| 329 |
* First, a shot with the search RE. |
| 330 |
*/ |
| 331 |
|
| 332 |
s = newDFA(v, &v->g->search, cm, &v->dfa1); |
| 333 |
assert(!(ISERR() && s != NULL)); |
| 334 |
NOERR(); |
| 335 |
MDEBUG(("\nsearch at %ld\n", LOFF(v->start))); |
| 336 |
cold = NULL; |
| 337 |
close = shortest(v, s, v->start, v->start, v->stop, &cold, NULL); |
| 338 |
freeDFA(s); |
| 339 |
NOERR(); |
| 340 |
if (v->g->cflags®_EXPECT) { |
| 341 |
assert(v->details != NULL); |
| 342 |
if (cold != NULL) { |
| 343 |
v->details->rm_extend.rm_so = OFF(cold); |
| 344 |
} else { |
| 345 |
v->details->rm_extend.rm_so = OFF(v->stop); |
| 346 |
} |
| 347 |
v->details->rm_extend.rm_eo = OFF(v->stop); /* unknown */ |
| 348 |
} |
| 349 |
if (close == NULL) { /* not found */ |
| 350 |
return REG_NOMATCH; |
| 351 |
} |
| 352 |
if (v->nmatch == 0) { /* found, don't need exact location */ |
| 353 |
return REG_OKAY; |
| 354 |
} |
| 355 |
|
| 356 |
/* |
| 357 |
* Find starting point and match. |
| 358 |
*/ |
| 359 |
|
| 360 |
assert(cold != NULL); |
| 361 |
open = cold; |
| 362 |
cold = NULL; |
| 363 |
MDEBUG(("between %ld and %ld\n", LOFF(open), LOFF(close))); |
| 364 |
d = newDFA(v, cnfa, cm, &v->dfa1); |
| 365 |
assert(!(ISERR() && d != NULL)); |
| 366 |
NOERR(); |
| 367 |
for (begin = open; begin <= close; begin++) { |
| 368 |
MDEBUG(("\nfind trying at %ld\n", LOFF(begin))); |
| 369 |
if (shorter) { |
| 370 |
end = shortest(v, d, begin, begin, v->stop, NULL, &hitend); |
| 371 |
} else { |
| 372 |
end = longest(v, d, begin, v->stop, &hitend); |
| 373 |
} |
| 374 |
if (ISERR()) { |
| 375 |
freeDFA(d); |
| 376 |
return v->err; |
| 377 |
} |
| 378 |
if (hitend && cold == NULL) { |
| 379 |
cold = begin; |
| 380 |
} |
| 381 |
if (end != NULL) { |
| 382 |
break; /* NOTE BREAK OUT */ |
| 383 |
} |
| 384 |
} |
| 385 |
assert(end != NULL); /* search RE succeeded so loop should */ |
| 386 |
freeDFA(d); |
| 387 |
|
| 388 |
/* |
| 389 |
* And pin down details. |
| 390 |
*/ |
| 391 |
|
| 392 |
assert(v->nmatch > 0); |
| 393 |
v->pmatch[0].rm_so = OFF(begin); |
| 394 |
v->pmatch[0].rm_eo = OFF(end); |
| 395 |
if (v->g->cflags®_EXPECT) { |
| 396 |
if (cold != NULL) { |
| 397 |
v->details->rm_extend.rm_so = OFF(cold); |
| 398 |
} else { |
| 399 |
v->details->rm_extend.rm_so = OFF(v->stop); |
| 400 |
} |
| 401 |
v->details->rm_extend.rm_eo = OFF(v->stop); /* unknown */ |
| 402 |
} |
| 403 |
if (v->nmatch == 1) { /* no need for submatches */ |
| 404 |
return REG_OKAY; |
| 405 |
} |
| 406 |
|
| 407 |
/* |
| 408 |
* Find submatches. |
| 409 |
*/ |
| 410 |
|
| 411 |
zapallsubs(v->pmatch, v->nmatch); |
| 412 |
return cdissect(v, v->g->tree, begin, end); |
| 413 |
} |
| 414 |
|
| 415 |
/* |
| 416 |
- complicatedFind - find a match for the main NFA (with complications) |
| 417 |
^ static int complicatedFind(struct vars *, struct cnfa *, struct colormap *); |
| 418 |
*/ |
| 419 |
static int |
| 420 |
complicatedFind( |
| 421 |
struct vars *const v, |
| 422 |
struct cnfa *const cnfa, |
| 423 |
struct colormap *const cm) |
| 424 |
{ |
| 425 |
struct dfa *s, *d; |
| 426 |
chr *cold = NULL; /* silence gcc 4 warning */ |
| 427 |
int ret; |
| 428 |
|
| 429 |
s = newDFA(v, &v->g->search, cm, &v->dfa1); |
| 430 |
NOERR(); |
| 431 |
d = newDFA(v, cnfa, cm, &v->dfa2); |
| 432 |
if (ISERR()) { |
| 433 |
assert(d == NULL); |
| 434 |
freeDFA(s); |
| 435 |
return v->err; |
| 436 |
} |
| 437 |
|
| 438 |
ret = complicatedFindLoop(v, d, s, &cold); |
| 439 |
|
| 440 |
freeDFA(d); |
| 441 |
freeDFA(s); |
| 442 |
NOERR(); |
| 443 |
if (v->g->cflags®_EXPECT) { |
| 444 |
assert(v->details != NULL); |
| 445 |
if (cold != NULL) { |
| 446 |
v->details->rm_extend.rm_so = OFF(cold); |
| 447 |
} else { |
| 448 |
v->details->rm_extend.rm_so = OFF(v->stop); |
| 449 |
} |
| 450 |
v->details->rm_extend.rm_eo = OFF(v->stop); /* unknown */ |
| 451 |
} |
| 452 |
return ret; |
| 453 |
} |
| 454 |
|
| 455 |
/* |
| 456 |
- complicatedFindLoop - the heart of complicatedFind |
| 457 |
^ static int complicatedFindLoop(struct vars *, |
| 458 |
^ struct dfa *, struct dfa *, chr **); |
| 459 |
*/ |
| 460 |
static int |
| 461 |
complicatedFindLoop( |
| 462 |
struct vars *const v, |
| 463 |
struct dfa *const d, |
| 464 |
struct dfa *const s, |
| 465 |
chr **const coldp) /* where to put coldstart pointer */ |
| 466 |
{ |
| 467 |
chr *begin, *end; |
| 468 |
chr *cold; |
| 469 |
chr *open, *close; /* Open and close of range of possible |
| 470 |
* starts */ |
| 471 |
chr *estart, *estop; |
| 472 |
int er, hitend; |
| 473 |
int shorter = v->g->tree->flags&SHORTER; |
| 474 |
|
| 475 |
assert(d != NULL && s != NULL); |
| 476 |
cold = NULL; |
| 477 |
close = v->start; |
| 478 |
do { |
| 479 |
MDEBUG(("\ncsearch at %ld\n", LOFF(close))); |
| 480 |
close = shortest(v, s, close, close, v->stop, &cold, NULL); |
| 481 |
if (close == NULL) { |
| 482 |
break; /* NOTE BREAK */ |
| 483 |
} |
| 484 |
assert(cold != NULL); |
| 485 |
open = cold; |
| 486 |
cold = NULL; |
| 487 |
MDEBUG(("cbetween %ld and %ld\n", LOFF(open), LOFF(close))); |
| 488 |
for (begin = open; begin <= close; begin++) { |
| 489 |
MDEBUG(("\ncomplicatedFind trying at %ld\n", LOFF(begin))); |
| 490 |
estart = begin; |
| 491 |
estop = v->stop; |
| 492 |
for (;;) { |
| 493 |
if (shorter) { |
| 494 |
end = shortest(v, d, begin, estart, estop, NULL, &hitend); |
| 495 |
} else { |
| 496 |
end = longest(v, d, begin, estop, &hitend); |
| 497 |
} |
| 498 |
if (hitend && cold == NULL) { |
| 499 |
cold = begin; |
| 500 |
} |
| 501 |
if (end == NULL) { |
| 502 |
break; /* NOTE BREAK OUT */ |
| 503 |
} |
| 504 |
|
| 505 |
MDEBUG(("tentative end %ld\n", LOFF(end))); |
| 506 |
zapallsubs(v->pmatch, v->nmatch); |
| 507 |
er = cdissect(v, v->g->tree, begin, end); |
| 508 |
if (er == REG_OKAY) { |
| 509 |
if (v->nmatch > 0) { |
| 510 |
v->pmatch[0].rm_so = OFF(begin); |
| 511 |
v->pmatch[0].rm_eo = OFF(end); |
| 512 |
} |
| 513 |
*coldp = cold; |
| 514 |
return REG_OKAY; |
| 515 |
} |
| 516 |
if (er != REG_NOMATCH) { |
| 517 |
ERR(er); |
| 518 |
*coldp = cold; |
| 519 |
return er; |
| 520 |
} |
| 521 |
if ((shorter) ? end == estop : end == begin) { |
| 522 |
break; |
| 523 |
} |
| 524 |
|
| 525 |
/* |
| 526 |
* Go around and try again |
| 527 |
*/ |
| 528 |
|
| 529 |
if (shorter) { |
| 530 |
estart = end + 1; |
| 531 |
} else { |
| 532 |
estop = end - 1; |
| 533 |
} |
| 534 |
} |
| 535 |
} |
| 536 |
} while (close < v->stop); |
| 537 |
|
| 538 |
*coldp = cold; |
| 539 |
return REG_NOMATCH; |
| 540 |
} |
| 541 |
|
| 542 |
/* |
| 543 |
- zapallsubs - initialize all subexpression matches to "no match" |
| 544 |
^ static void zapallsubs(regmatch_t *, size_t); |
| 545 |
*/ |
| 546 |
static void |
| 547 |
zapallsubs( |
| 548 |
regmatch_t *const p, |
| 549 |
const size_t n) |
| 550 |
{ |
| 551 |
size_t i; |
| 552 |
|
| 553 |
for (i = n-1; i > 0; i--) { |
| 554 |
p[i].rm_so = -1; |
| 555 |
p[i].rm_eo = -1; |
| 556 |
} |
| 557 |
} |
| 558 |
|
| 559 |
/* |
| 560 |
- zaptreesubs - initialize subexpressions within subtree to "no match" |
| 561 |
^ static void zaptreesubs(struct vars *, struct subre *); |
| 562 |
*/ |
| 563 |
static void |
| 564 |
zaptreesubs( |
| 565 |
struct vars *const v, |
| 566 |
struct subre *const t) |
| 567 |
{ |
| 568 |
if (t->op == '(') { |
| 569 |
int n = t->subno; |
| 570 |
assert(n > 0); |
| 571 |
if ((size_t) n < v->nmatch) { |
| 572 |
v->pmatch[n].rm_so = -1; |
| 573 |
v->pmatch[n].rm_eo = -1; |
| 574 |
} |
| 575 |
} |
| 576 |
|
| 577 |
if (t->left != NULL) { |
| 578 |
zaptreesubs(v, t->left); |
| 579 |
} |
| 580 |
if (t->right != NULL) { |
| 581 |
zaptreesubs(v, t->right); |
| 582 |
} |
| 583 |
} |
| 584 |
|
| 585 |
/* |
| 586 |
- subset - set subexpression match data for a successful subre |
| 587 |
^ static void subset(struct vars *, struct subre *, chr *, chr *); |
| 588 |
*/ |
| 589 |
static void |
| 590 |
subset( |
| 591 |
struct vars *const v, |
| 592 |
struct subre *const sub, |
| 593 |
chr *const begin, |
| 594 |
chr *const end) |
| 595 |
{ |
| 596 |
int n = sub->subno; |
| 597 |
|
| 598 |
assert(n > 0); |
| 599 |
if ((size_t)n >= v->nmatch) { |
| 600 |
return; |
| 601 |
} |
| 602 |
|
| 603 |
MDEBUG(("setting %d\n", n)); |
| 604 |
v->pmatch[n].rm_so = OFF(begin); |
| 605 |
v->pmatch[n].rm_eo = OFF(end); |
| 606 |
} |
| 607 |
|
| 608 |
/* |
| 609 |
- cdissect - check backrefs and determine subexpression matches |
| 610 |
* cdissect recursively processes a subre tree to check matching of backrefs |
| 611 |
* and/or identify submatch boundaries for capture nodes. The proposed match |
| 612 |
* runs from "begin" to "end" (not including "end"), and we are basically |
| 613 |
* "dissecting" it to see where the submatches are. |
| 614 |
* Before calling any level of cdissect, the caller must have run the node's |
| 615 |
* DFA and found that the proposed substring satisfies the DFA. (We make |
| 616 |
* the caller do that because in concatenation and iteration nodes, it's |
| 617 |
* much faster to check all the substrings against the child DFAs before we |
| 618 |
* recurse.) Also, caller must have cleared subexpression match data via |
| 619 |
* zaptreesubs (or zapallsubs at the top level). |
| 620 |
^ static int cdissect(struct vars *, struct subre *, chr *, chr *); |
| 621 |
*/ |
| 622 |
static int /* regexec return code */ |
| 623 |
cdissect( |
| 624 |
struct vars *v, |
| 625 |
struct subre *t, |
| 626 |
chr *begin, /* beginning of relevant substring */ |
| 627 |
chr *end) /* end of same */ |
| 628 |
{ |
| 629 |
int er; |
| 630 |
|
| 631 |
assert(t != NULL); |
| 632 |
MDEBUG(("cdissect %ld-%ld %c\n", LOFF(begin), LOFF(end), t->op)); |
| 633 |
|
| 634 |
switch (t->op) { |
| 635 |
case '=': /* terminal node */ |
| 636 |
assert(t->left == NULL && t->right == NULL); |
| 637 |
er = REG_OKAY; /* no action, parent did the work */ |
| 638 |
break; |
| 639 |
case 'b': /* back reference */ |
| 640 |
assert(t->left == NULL && t->right == NULL); |
| 641 |
er = cbrdissect(v, t, begin, end); |
| 642 |
break; |
| 643 |
case '.': /* concatenation */ |
| 644 |
assert(t->left != NULL && t->right != NULL); |
| 645 |
if (t->left->flags & SHORTER) {/* reverse scan */ |
| 646 |
er = crevcondissect(v, t, begin, end); |
| 647 |
} else { |
| 648 |
er = ccondissect(v, t, begin, end); |
| 649 |
} |
| 650 |
break; |
| 651 |
case '|': /* alternation */ |
| 652 |
assert(t->left != NULL); |
| 653 |
er = caltdissect(v, t, begin, end); |
| 654 |
break; |
| 655 |
case '*': /* iteration */ |
| 656 |
assert(t->left != NULL); |
| 657 |
if (t->left->flags & SHORTER) {/* reverse scan */ |
| 658 |
er = creviterdissect(v, t, begin, end); |
| 659 |
} else { |
| 660 |
er = citerdissect(v, t, begin, end); |
| 661 |
} |
| 662 |
break; |
| 663 |
case '(': /* capturing */ |
| 664 |
assert(t->left != NULL && t->right == NULL); |
| 665 |
assert(t->subno > 0); |
| 666 |
er = cdissect(v, t->left, begin, end); |
| 667 |
if (er == REG_OKAY) { |
| 668 |
subset(v, t, begin, end); |
| 669 |
} |
| 670 |
break; |
| 671 |
default: |
| 672 |
er = REG_ASSERT; |
| 673 |
break; |
| 674 |
} |
| 675 |
|
| 676 |
/* |
| 677 |
* We should never have a match failure unless backrefs lurk below; |
| 678 |
* otherwise, either caller failed to check the DFA, or there's some |
| 679 |
* inconsistency between the DFA and the node's innards. |
| 680 |
*/ |
| 681 |
assert(er != REG_NOMATCH || (t->flags & BACKR)); |
| 682 |
|
| 683 |
return er; |
| 684 |
} |
| 685 |
|
| 686 |
/* |
| 687 |
- ccondissect - dissect match for concatenation node |
| 688 |
^ static int ccondissect(struct vars *, struct subre *, chr *, chr *); |
| 689 |
*/ |
| 690 |
static int /* regexec return code */ |
| 691 |
ccondissect( |
| 692 |
struct vars *v, |
| 693 |
struct subre *t, |
| 694 |
chr *begin, /* beginning of relevant substring */ |
| 695 |
chr *end) /* end of same */ |
| 696 |
{ |
| 697 |
struct dfa *d, *d2; |
| 698 |
chr *mid; |
| 699 |
|
| 700 |
assert(t->op == '.'); |
| 701 |
assert(t->left != NULL && t->left->cnfa.nstates > 0); |
| 702 |
assert(t->right != NULL && t->right->cnfa.nstates > 0); |
| 703 |
assert(!(t->left->flags & SHORTER)); |
| 704 |
|
| 705 |
d = getsubdfa(v, t->left); |
| 706 |
NOERR(); |
| 707 |
d2 = getsubdfa(v, t->right); |
| 708 |
NOERR(); |
| 709 |
|
| 710 |
MDEBUG(("cConcat %d\n", t->id)); |
| 711 |
|
| 712 |
/* |
| 713 |
* Pick a tentative midpoint. |
| 714 |
*/ |
| 715 |
mid = longest(v, d, begin, end, (int *) NULL); |
| 716 |
if (mid == NULL) { |
| 717 |
return REG_NOMATCH; |
| 718 |
} |
| 719 |
MDEBUG(("tentative midpoint %ld\n", LOFF(mid))); |
| 720 |
|
| 721 |
/* |
| 722 |
* Iterate until satisfaction or failure. |
| 723 |
*/ |
| 724 |
|
| 725 |
for (;;) { |
| 726 |
/* |
| 727 |
* Try this midpoint on for size. |
| 728 |
*/ |
| 729 |
|
| 730 |
if (longest(v, d2, mid, end, NULL) == end) { |
| 731 |
int er = cdissect(v, t->left, begin, mid); |
| 732 |
|
| 733 |
if (er == REG_OKAY) { |
| 734 |
er = cdissect(v, t->right, mid, end); |
| 735 |
if (er == REG_OKAY) { |
| 736 |
/* |
| 737 |
* Satisfaction. |
| 738 |
*/ |
| 739 |
|
| 740 |
MDEBUG(("successful\n")); |
| 741 |
return REG_OKAY; |
| 742 |
} |
| 743 |
} |
| 744 |
if (er != REG_NOMATCH) { |
| 745 |
return er; |
| 746 |
} |
| 747 |
} |
| 748 |
|
| 749 |
/* |
| 750 |
* That midpoint didn't work, find a new one. |
| 751 |
*/ |
| 752 |
|
| 753 |
if (mid == begin) { |
| 754 |
/* |
| 755 |
* All possibilities exhausted. |
| 756 |
*/ |
| 757 |
|
| 758 |
MDEBUG(("%d no midpoint\n", t->id)); |
| 759 |
return REG_NOMATCH; |
| 760 |
} |
| 761 |
mid = longest(v, d, begin, mid-1, NULL); |
| 762 |
if (mid == NULL) { |
| 763 |
/* |
| 764 |
* Failed to find a new one. |
| 765 |
*/ |
| 766 |
|
| 767 |
MDEBUG(("%d failed midpoint\n", t->id)); |
| 768 |
return REG_NOMATCH; |
| 769 |
} |
| 770 |
MDEBUG(("%d: new midpoint %ld\n", t->id, LOFF(mid))); |
| 771 |
zaptreesubs(v, t->left); |
| 772 |
zaptreesubs(v, t->right); |
| 773 |
} |
| 774 |
} |
| 775 |
|
| 776 |
/* |
| 777 |
- crevcondissect - dissect match for concatenation node, shortest-first |
| 778 |
^ static int crevcondissect(struct vars *, struct subre *, chr *, chr *); |
| 779 |
*/ |
| 780 |
static int /* regexec return code */ |
| 781 |
crevcondissect( |
| 782 |
struct vars *v, |
| 783 |
struct subre *t, |
| 784 |
chr *begin, /* beginning of relevant substring */ |
| 785 |
chr *end) /* end of same */ |
| 786 |
{ |
| 787 |
struct dfa *d, *d2; |
| 788 |
chr *mid; |
| 789 |
|
| 790 |
assert(t->op == '.'); |
| 791 |
assert(t->left != NULL && t->left->cnfa.nstates > 0); |
| 792 |
assert(t->right != NULL && t->right->cnfa.nstates > 0); |
| 793 |
assert(t->left->flags&SHORTER); |
| 794 |
|
| 795 |
d = getsubdfa(v, t->left); |
| 796 |
NOERR(); |
| 797 |
d2 = getsubdfa(v, t->right); |
| 798 |
NOERR(); |
| 799 |
|
| 800 |
MDEBUG(("crevcon %d\n", t->id)); |
| 801 |
|
| 802 |
/* |
| 803 |
* Pick a tentative midpoint. |
| 804 |
*/ |
| 805 |
|
| 806 |
mid = shortest(v, d, begin, begin, end, (chr **) NULL, (int *) NULL); |
| 807 |
if (mid == NULL) { |
| 808 |
return REG_NOMATCH; |
| 809 |
} |
| 810 |
MDEBUG(("tentative midpoint %ld\n", LOFF(mid))); |
| 811 |
|
| 812 |
/* |
| 813 |
* Iterate until satisfaction or failure. |
| 814 |
*/ |
| 815 |
|
| 816 |
for (;;) { |
| 817 |
/* |
| 818 |
* Try this midpoint on for size. |
| 819 |
*/ |
| 820 |
|
| 821 |
if (longest(v, d2, mid, end, NULL) == end) { |
| 822 |
int er = cdissect(v, t->left, begin, mid); |
| 823 |
|
| 824 |
if (er == REG_OKAY) { |
| 825 |
er = cdissect(v, t->right, mid, end); |
| 826 |
if (er == REG_OKAY) { |
| 827 |
/* |
| 828 |
* Satisfaction. |
| 829 |
*/ |
| 830 |
|
| 831 |
MDEBUG(("successful\n")); |
| 832 |
return REG_OKAY; |
| 833 |
} |
| 834 |
} |
| 835 |
if (er != REG_NOMATCH) { |
| 836 |
return er; |
| 837 |
} |
| 838 |
} |
| 839 |
|
| 840 |
/* |
| 841 |
* That midpoint didn't work, find a new one. |
| 842 |
*/ |
| 843 |
|
| 844 |
if (mid == end) { |
| 845 |
/* |
| 846 |
* All possibilities exhausted. |
| 847 |
*/ |
| 848 |
|
| 849 |
MDEBUG(("%d no midpoint\n", t->id)); |
| 850 |
return REG_NOMATCH; |
| 851 |
} |
| 852 |
mid = shortest(v, d, begin, mid+1, end, NULL, NULL); |
| 853 |
if (mid == NULL) { |
| 854 |
/* |
| 855 |
* Failed to find a new one. |
| 856 |
*/ |
| 857 |
|
| 858 |
MDEBUG(("%d failed midpoint\n", t->id)); |
| 859 |
return REG_NOMATCH; |
| 860 |
} |
| 861 |
MDEBUG(("%d: new midpoint %ld\n", t->id, LOFF(mid))); |
| 862 |
zaptreesubs(v, t->left); |
| 863 |
zaptreesubs(v, t->right); |
| 864 |
} |
| 865 |
} |
| 866 |
|
| 867 |
/* |
| 868 |
- cbrdissect - dissect match for backref node |
| 869 |
^ static int cbrdissect(struct vars *, struct subre *, chr *, chr *); |
| 870 |
*/ |
| 871 |
static int /* regexec return code */ |
| 872 |
cbrdissect( |
| 873 |
struct vars *v, |
| 874 |
struct subre *t, |
| 875 |
chr *begin, /* beginning of relevant substring */ |
| 876 |
chr *end) /* end of same */ |
| 877 |
{ |
| 878 |
int n = t->subno, min = t->min, max = t->max; |
| 879 |
size_t numreps; |
| 880 |
size_t tlen; |
| 881 |
size_t brlen; |
| 882 |
chr *brstring; |
| 883 |
chr *p; |
| 884 |
|
| 885 |
assert(t != NULL); |
| 886 |
assert(t->op == 'b'); |
| 887 |
assert(n >= 0); |
| 888 |
assert((size_t)n < v->nmatch); |
| 889 |
|
| 890 |
MDEBUG(("cbackref n%d %d{%d-%d}\n", t->id, n, min, max)); |
| 891 |
|
| 892 |
/* get the backreferenced string */ |
| 893 |
if (v->pmatch[n].rm_so == -1) { |
| 894 |
return REG_NOMATCH; |
| 895 |
} |
| 896 |
brstring = v->start + v->pmatch[n].rm_so; |
| 897 |
brlen = v->pmatch[n].rm_eo - v->pmatch[n].rm_so; |
| 898 |
|
| 899 |
/* special cases for zero-length strings */ |
| 900 |
if (brlen == 0) { |
| 901 |
/* |
| 902 |
* matches only if target is zero length, but any number of |
| 903 |
* repetitions can be considered to be present |
| 904 |
*/ |
| 905 |
if (begin == end && min <= max) { |
| 906 |
MDEBUG(("cbackref matched trivially\n")); |
| 907 |
return REG_OKAY; |
| 908 |
} |
| 909 |
return REG_NOMATCH; |
| 910 |
} |
| 911 |
if (begin == end) { |
| 912 |
/* matches only if zero repetitions are okay */ |
| 913 |
if (min == 0) { |
| 914 |
MDEBUG(("cbackref matched trivially\n")); |
| 915 |
return REG_OKAY; |
| 916 |
} |
| 917 |
return REG_NOMATCH; |
| 918 |
} |
| 919 |
|
| 920 |
/* |
| 921 |
* check target length to see if it could possibly be an allowed number of |
| 922 |
* repetitions of brstring |
| 923 |
*/ |
| 924 |
|
| 925 |
assert(end > begin); |
| 926 |
tlen = end - begin; |
| 927 |
if (tlen % brlen != 0) |
| 928 |
return REG_NOMATCH; |
| 929 |
numreps = tlen / brlen; |
| 930 |
if (numreps < (size_t)min || (numreps > (size_t)max && max != DUPINF)) |
| 931 |
return REG_NOMATCH; |
| 932 |
|
| 933 |
/* okay, compare the actual string contents */ |
| 934 |
p = begin; |
| 935 |
while (numreps-- > 0) { |
| 936 |
if ((*v->g->compare) (brstring, p, brlen) != 0) |
| 937 |
return REG_NOMATCH; |
| 938 |
p += brlen; |
| 939 |
} |
| 940 |
|
| 941 |
MDEBUG(("cbackref matched\n")); |
| 942 |
return REG_OKAY; |
| 943 |
} |
| 944 |
|
| 945 |
/* |
| 946 |
- caltdissect - dissect match for alternation node |
| 947 |
^ static int caltdissect(struct vars *, struct subre *, chr *, chr *); |
| 948 |
*/ |
| 949 |
static int /* regexec return code */ |
| 950 |
caltdissect( |
| 951 |
struct vars *v, |
| 952 |
struct subre *t, |
| 953 |
chr *begin, /* beginning of relevant substring */ |
| 954 |
chr *end) /* end of same */ |
| 955 |
{ |
| 956 |
struct dfa *d; |
| 957 |
int er; |
| 958 |
|
| 959 |
/* We loop, rather than tail-recurse, to handle a chain of alternatives */ |
| 960 |
while (t != NULL) { |
| 961 |
assert(t->op == '|'); |
| 962 |
assert(t->left != NULL && t->left->cnfa.nstates > 0); |
| 963 |
|
| 964 |
MDEBUG(("calt n%d\n", t->id)); |
| 965 |
|
| 966 |
d = getsubdfa(v, t->left); |
| 967 |
NOERR(); |
| 968 |
if (longest(v, d, begin, end, (int *) NULL) == end) { |
| 969 |
MDEBUG(("calt matched\n")); |
| 970 |
er = cdissect(v, t->left, begin, end); |
| 971 |
if (er != REG_NOMATCH) { |
| 972 |
return er; |
| 973 |
} |
| 974 |
} |
| 975 |
|
| 976 |
t = t->right; |
| 977 |
} |
| 978 |
|
| 979 |
return REG_NOMATCH; |
| 980 |
} |
| 981 |
|
| 982 |
/* |
| 983 |
- citerdissect - dissect match for iteration node |
| 984 |
^ static int citerdissect(struct vars *, struct subre *, chr *, chr *); |
| 985 |
*/ |
| 986 |
static int /* regexec return code */ |
| 987 |
citerdissect(struct vars * v, |
| 988 |
struct subre * t, |
| 989 |
chr *begin, /* beginning of relevant substring */ |
| 990 |
chr *end) /* end of same */ |
| 991 |
{ |
| 992 |
struct dfa *d; |
| 993 |
chr **endpts; |
| 994 |
chr *limit; |
| 995 |
int min_matches; |
| 996 |
size_t max_matches; |
| 997 |
int nverified; |
| 998 |
int k; |
| 999 |
int i; |
| 1000 |
int er; |
| 1001 |
|
| 1002 |
assert(t->op == '*'); |
| 1003 |
assert(t->left != NULL && t->left->cnfa.nstates > 0); |
| 1004 |
assert(!(t->left->flags & SHORTER)); |
| 1005 |
assert(begin <= end); |
| 1006 |
|
| 1007 |
/* |
| 1008 |
* If zero matches are allowed, and target string is empty, just declare |
| 1009 |
* victory. OTOH, if target string isn't empty, zero matches can't work |
| 1010 |
* so we pretend the min is 1. |
| 1011 |
*/ |
| 1012 |
min_matches = t->min; |
| 1013 |
if (min_matches <= 0) { |
| 1014 |
if (begin == end) |
| 1015 |
return REG_OKAY; |
| 1016 |
min_matches = 1; |
| 1017 |
} |
| 1018 |
|
| 1019 |
/* |
| 1020 |
* We need workspace to track the endpoints of each sub-match. Normally |
| 1021 |
* we consider only nonzero-length sub-matches, so there can be at most |
| 1022 |
* end-begin of them. However, if min is larger than that, we will also |
| 1023 |
* consider zero-length sub-matches in order to find enough matches. |
| 1024 |
* |
| 1025 |
* For convenience, endpts[0] contains the "begin" pointer and we store |
| 1026 |
* sub-match endpoints in endpts[1..max_matches]. |
| 1027 |
*/ |
| 1028 |
max_matches = end - begin; |
| 1029 |
if (max_matches > (size_t)t->max && t->max != DUPINF) |
| 1030 |
max_matches = t->max; |
| 1031 |
if (max_matches < (size_t)min_matches) |
| 1032 |
max_matches = min_matches; |
| 1033 |
endpts = (chr **) MALLOC((max_matches + 1) * sizeof(chr *)); |
| 1034 |
if (endpts == NULL) |
| 1035 |
return REG_ESPACE; |
| 1036 |
endpts[0] = begin; |
| 1037 |
|
| 1038 |
d = getsubdfa(v, t->left); |
| 1039 |
if (ISERR()) { |
| 1040 |
FREE(endpts); |
| 1041 |
return v->err; |
| 1042 |
} |
| 1043 |
MDEBUG(("citer %d\n", t->id)); |
| 1044 |
|
| 1045 |
/* |
| 1046 |
* Our strategy is to first find a set of sub-match endpoints that are |
| 1047 |
* valid according to the child node's DFA, and then recursively dissect |
| 1048 |
* each sub-match to confirm validity. If any validity check fails, |
| 1049 |
* backtrack the last sub-match and try again. And, when we next try for |
| 1050 |
* a validity check, we need not recheck any successfully verified |
| 1051 |
* sub-matches that we didn't move the endpoints of. nverified remembers |
| 1052 |
* how many sub-matches are currently known okay. |
| 1053 |
*/ |
| 1054 |
|
| 1055 |
/* initialize to consider first sub-match */ |
| 1056 |
nverified = 0; |
| 1057 |
k = 1; |
| 1058 |
limit = end; |
| 1059 |
|
| 1060 |
/* iterate until satisfaction or failure */ |
| 1061 |
while (k > 0) { |
| 1062 |
/* try to find an endpoint for the k'th sub-match */ |
| 1063 |
endpts[k] = longest(v, d, endpts[k - 1], limit, (int *) NULL); |
| 1064 |
if (endpts[k] == NULL) { |
| 1065 |
/* no match possible, so see if we can shorten previous one */ |
| 1066 |
k--; |
| 1067 |
goto backtrack; |
| 1068 |
} |
| 1069 |
MDEBUG(("%d: working endpoint %d: %ld\n", |
| 1070 |
t->id, k, LOFF(endpts[k]))); |
| 1071 |
|
| 1072 |
/* k'th sub-match can no longer be considered verified */ |
| 1073 |
if (nverified >= k) |
| 1074 |
nverified = k - 1; |
| 1075 |
|
| 1076 |
if (endpts[k] != end) { |
| 1077 |
/* haven't reached end yet, try another iteration if allowed */ |
| 1078 |
if ((size_t)k >= max_matches) { |
| 1079 |
/* must try to shorten some previous match */ |
| 1080 |
k--; |
| 1081 |
goto backtrack; |
| 1082 |
} |
| 1083 |
|
| 1084 |
/* reject zero-length match unless necessary to achieve min */ |
| 1085 |
if (endpts[k] == endpts[k - 1] && |
| 1086 |
(k >= min_matches || min_matches - k < end - endpts[k])) |
| 1087 |
goto backtrack; |
| 1088 |
|
| 1089 |
k++; |
| 1090 |
limit = end; |
| 1091 |
continue; |
| 1092 |
} |
| 1093 |
|
| 1094 |
/* |
| 1095 |
* We've identified a way to divide the string into k sub-matches |
| 1096 |
* that works so far as the child DFA can tell. If k is an allowed |
| 1097 |
* number of matches, start the slow part: recurse to verify each |
| 1098 |
* sub-match. We always have k <= max_matches, needn't check that. |
| 1099 |
*/ |
| 1100 |
if (k < min_matches) |
| 1101 |
goto backtrack; |
| 1102 |
|
| 1103 |
MDEBUG(("%d: verifying %d..%d\n", t->id, nverified + 1, k)); |
| 1104 |
|
| 1105 |
for (i = nverified + 1; i <= k; i++) { |
| 1106 |
zaptreesubs(v, t->left); |
| 1107 |
er = cdissect(v, t->left, endpts[i - 1], endpts[i]); |
| 1108 |
if (er == REG_OKAY) { |
| 1109 |
nverified = i; |
| 1110 |
continue; |
| 1111 |
} |
| 1112 |
if (er == REG_NOMATCH) |
| 1113 |
break; |
| 1114 |
/* oops, something failed */ |
| 1115 |
FREE(endpts); |
| 1116 |
return er; |
| 1117 |
} |
| 1118 |
|
| 1119 |
if (i > k) { |
| 1120 |
/* satisfaction */ |
| 1121 |
MDEBUG(("%d successful\n", t->id)); |
| 1122 |
FREE(endpts); |
| 1123 |
return REG_OKAY; |
| 1124 |
} |
| 1125 |
|
| 1126 |
/* match failed to verify, so backtrack */ |
| 1127 |
|
| 1128 |
backtrack: |
| 1129 |
/* |
| 1130 |
* Must consider shorter versions of the current sub-match. However, |
| 1131 |
* we'll only ask for a zero-length match if necessary. |
| 1132 |
*/ |
| 1133 |
while (k > 0) { |
| 1134 |
chr *prev_end = endpts[k - 1]; |
| 1135 |
|
| 1136 |
if (endpts[k] > prev_end) { |
| 1137 |
limit = endpts[k] - 1; |
| 1138 |
if (limit > prev_end || |
| 1139 |
(k < min_matches && min_matches - k >= end - prev_end)) { |
| 1140 |
/* break out of backtrack loop, continue the outer one */ |
| 1141 |
break; |
| 1142 |
} |
| 1143 |
} |
| 1144 |
/* can't shorten k'th sub-match any more, consider previous one */ |
| 1145 |
k--; |
| 1146 |
} |
| 1147 |
} |
| 1148 |
|
| 1149 |
/* all possibilities exhausted */ |
| 1150 |
MDEBUG(("%d failed\n", t->id)); |
| 1151 |
FREE(endpts); |
| 1152 |
return REG_NOMATCH; |
| 1153 |
} |
| 1154 |
|
| 1155 |
/* |
| 1156 |
- creviterdissect - dissect match for iteration node, shortest-first |
| 1157 |
^ static int creviterdissect(struct vars *, struct subre *, chr *, chr *); |
| 1158 |
*/ |
| 1159 |
static int /* regexec return code */ |
| 1160 |
creviterdissect(struct vars * v, |
| 1161 |
struct subre * t, |
| 1162 |
chr *begin, /* beginning of relevant substring */ |
| 1163 |
chr *end) /* end of same */ |
| 1164 |
{ |
| 1165 |
struct dfa *d; |
| 1166 |
chr **endpts; |
| 1167 |
chr *limit; |
| 1168 |
int min_matches; |
| 1169 |
size_t max_matches; |
| 1170 |
int nverified; |
| 1171 |
int k; |
| 1172 |
int i; |
| 1173 |
int er; |
| 1174 |
|
| 1175 |
assert(t->op == '*'); |
| 1176 |
assert(t->left != NULL && t->left->cnfa.nstates > 0); |
| 1177 |
assert(t->left->flags & SHORTER); |
| 1178 |
assert(begin <= end); |
| 1179 |
|
| 1180 |
/* |
| 1181 |
* If zero matches are allowed, and target string is empty, just declare |
| 1182 |
* victory. OTOH, if target string isn't empty, zero matches can't work |
| 1183 |
* so we pretend the min is 1. |
| 1184 |
*/ |
| 1185 |
min_matches = t->min; |
| 1186 |
if (min_matches <= 0) { |
| 1187 |
if (begin == end) |
| 1188 |
return REG_OKAY; |
| 1189 |
min_matches = 1; |
| 1190 |
} |
| 1191 |
|
| 1192 |
/* |
| 1193 |
* We need workspace to track the endpoints of each sub-match. Normally |
| 1194 |
* we consider only nonzero-length sub-matches, so there can be at most |
| 1195 |
* end-begin of them. However, if min is larger than that, we will also |
| 1196 |
* consider zero-length sub-matches in order to find enough matches. |
| 1197 |
* |
| 1198 |
* For convenience, endpts[0] contains the "begin" pointer and we store |
| 1199 |
* sub-match endpoints in endpts[1..max_matches]. |
| 1200 |
*/ |
| 1201 |
max_matches = end - begin; |
| 1202 |
if (max_matches > (size_t)t->max && t->max != DUPINF) |
| 1203 |
max_matches = t->max; |
| 1204 |
if (max_matches < (size_t)min_matches) |
| 1205 |
max_matches = min_matches; |
| 1206 |
endpts = (chr **) MALLOC((max_matches + 1) * sizeof(chr *)); |
| 1207 |
if (endpts == NULL) |
| 1208 |
return REG_ESPACE; |
| 1209 |
endpts[0] = begin; |
| 1210 |
|
| 1211 |
d = getsubdfa(v, t->left); |
| 1212 |
if (ISERR()) { |
| 1213 |
FREE(endpts); |
| 1214 |
return v->err; |
| 1215 |
} |
| 1216 |
MDEBUG(("creviter %d\n", t->id)); |
| 1217 |
|
| 1218 |
/* |
| 1219 |
* Our strategy is to first find a set of sub-match endpoints that are |
| 1220 |
* valid according to the child node's DFA, and then recursively dissect |
| 1221 |
* each sub-match to confirm validity. If any validity check fails, |
| 1222 |
* backtrack the last sub-match and try again. And, when we next try for |
| 1223 |
* a validity check, we need not recheck any successfully verified |
| 1224 |
* sub-matches that we didn't move the endpoints of. nverified remembers |
| 1225 |
* how many sub-matches are currently known okay. |
| 1226 |
*/ |
| 1227 |
|
| 1228 |
/* initialize to consider first sub-match */ |
| 1229 |
nverified = 0; |
| 1230 |
k = 1; |
| 1231 |
limit = begin; |
| 1232 |
|
| 1233 |
/* iterate until satisfaction or failure */ |
| 1234 |
while (k > 0) { |
| 1235 |
/* disallow zero-length match unless necessary to achieve min */ |
| 1236 |
if (limit == endpts[k - 1] && |
| 1237 |
limit != end && |
| 1238 |
(k >= min_matches || min_matches - k < end - limit)) |
| 1239 |
limit++; |
| 1240 |
|
| 1241 |
/* if this is the last allowed sub-match, it must reach to the end */ |
| 1242 |
if ((size_t)k >= max_matches) |
| 1243 |
limit = end; |
| 1244 |
|
| 1245 |
/* try to find an endpoint for the k'th sub-match */ |
| 1246 |
endpts[k] = shortest(v, d, endpts[k - 1], limit, end, |
| 1247 |
(chr **) NULL, (int *) NULL); |
| 1248 |
if (endpts[k] == NULL) { |
| 1249 |
/* no match possible, so see if we can lengthen previous one */ |
| 1250 |
k--; |
| 1251 |
goto backtrack; |
| 1252 |
} |
| 1253 |
MDEBUG(("%d: working endpoint %d: %ld\n", |
| 1254 |
t->id, k, LOFF(endpts[k]))); |
| 1255 |
|
| 1256 |
/* k'th sub-match can no longer be considered verified */ |
| 1257 |
if (nverified >= k) |
| 1258 |
nverified = k - 1; |
| 1259 |
|
| 1260 |
if (endpts[k] != end) { |
| 1261 |
/* haven't reached end yet, try another iteration if allowed */ |
| 1262 |
if ((size_t)k >= max_matches) { |
| 1263 |
/* must try to lengthen some previous match */ |
| 1264 |
k--; |
| 1265 |
goto backtrack; |
| 1266 |
} |
| 1267 |
|
| 1268 |
k++; |
| 1269 |
limit = endpts[k - 1]; |
| 1270 |
continue; |
| 1271 |
} |
| 1272 |
|
| 1273 |
/* |
| 1274 |
* We've identified a way to divide the string into k sub-matches |
| 1275 |
* that works so far as the child DFA can tell. If k is an allowed |
| 1276 |
* number of matches, start the slow part: recurse to verify each |
| 1277 |
* sub-match. We always have k <= max_matches, needn't check that. |
| 1278 |
*/ |
| 1279 |
if (k < min_matches) |
| 1280 |
goto backtrack; |
| 1281 |
|
| 1282 |
MDEBUG(("%d: verifying %d..%d\n", t->id, nverified + 1, k)); |
| 1283 |
|
| 1284 |
for (i = nverified + 1; i <= k; i++) { |
| 1285 |
zaptreesubs(v, t->left); |
| 1286 |
er = cdissect(v, t->left, endpts[i - 1], endpts[i]); |
| 1287 |
if (er == REG_OKAY) { |
| 1288 |
nverified = i; |
| 1289 |
continue; |
| 1290 |
} |
| 1291 |
if (er == REG_NOMATCH) |
| 1292 |
break; |
| 1293 |
/* oops, something failed */ |
| 1294 |
FREE(endpts); |
| 1295 |
return er; |
| 1296 |
} |
| 1297 |
|
| 1298 |
if (i > k) { |
| 1299 |
/* satisfaction */ |
| 1300 |
MDEBUG(("%d successful\n", t->id)); |
| 1301 |
FREE(endpts); |
| 1302 |
return REG_OKAY; |
| 1303 |
} |
| 1304 |
|
| 1305 |
/* match failed to verify, so backtrack */ |
| 1306 |
|
| 1307 |
backtrack: |
| 1308 |
/* |
| 1309 |
* Must consider longer versions of the current sub-match. |
| 1310 |
*/ |
| 1311 |
while (k > 0) { |
| 1312 |
if (endpts[k] < end) { |
| 1313 |
limit = endpts[k] + 1; |
| 1314 |
/* break out of backtrack loop, continue the outer one */ |
| 1315 |
break; |
| 1316 |
} |
| 1317 |
/* can't lengthen k'th sub-match any more, consider previous one */ |
| 1318 |
k--; |
| 1319 |
} |
| 1320 |
} |
| 1321 |
|
| 1322 |
/* all possibilities exhausted */ |
| 1323 |
MDEBUG(("%d failed\n", t->id)); |
| 1324 |
FREE(endpts); |
| 1325 |
return REG_NOMATCH; |
| 1326 |
} |
| 1327 |
|
| 1328 |
#include "rege_dfa.c" |
| 1329 |
|
| 1330 |
/* |
| 1331 |
* Local Variables: |
| 1332 |
* mode: c |
| 1333 |
* c-basic-offset: 4 |
| 1334 |
* fill-column: 78 |
| 1335 |
* End: |
| 1336 |
*/ |