49 #define HASH(bv, nw) (((nw) == 1) ? *(bv) : hash(bv, nw))
50 #define HIT(h,bv,ss,nw) ((ss)->hash == (h) && ((nw) == 1 || \
51 memcmp(VS(bv), VS((ss)->states), (nw)*sizeof(unsigned)) == 0))
56 #define NOPROGRESS 010
101 #define DOMALLOC ((struct smalldfa *)NULL)
126 #define VISERR(vv) ((vv)->err != 0)
127 #define ISERR() VISERR(v)
128 #define VERR(vv,e) ((vv)->err = ((vv)->err ? (vv)->err : (e)))
129 #define ERR(e) VERR(v, e)
130 #define NOERR() {if (ISERR()) return v->err;}
131 #define OFF(p) ((p) - v->start)
132 #define LOFF(p) ((long)OFF(p))
145 struct dfa *d,
struct dfa *s,
chr **coldp);
161 chr *max,
chr **coldp,
int *hitstopp);
163 struct sset **lastcss,
chr **lastcp);
170 static unsigned hash(
unsigned *uv,
int n);
195 struct vars *v = &var;
208 if (re == NULL ||
string == NULL || re->re_magic !=
REMAGIC)
210 if (re->re_csize !=
sizeof(
chr))
212 if (search_start >
len)
220 v->
g = (
struct guts *) re->re_guts;
227 if (backref && nmatch <= v->g->nsub)
247 if (nmatch > v->
g->
nsub + 1)
248 nmatch = v->
g->
nsub + 1;
275 for (
i = 0;
i < n;
i++)
288 for (
i = 0;
i < n;
i++)
297 for (
i = 0;
i < n;
i++)
317 assert(nmatch <= v->nmatch);
440 &cold, (
int *) NULL);
465 for (begin = open; begin <=
close; begin++)
467 MDEBUG((
"\nfind trying at %ld\n",
LOFF(begin)));
470 (
chr **) NULL, &hitend);
478 if (hitend && cold == NULL)
567 assert(d != NULL && s != NULL);
587 for (begin = open; begin <=
close; begin++)
589 MDEBUG((
"\ncfind trying at %ld\n",
LOFF(begin)));
597 estop, (
chr **) NULL, &hitend);
599 end =
longest(v, d, begin, estop,
606 if (hitend && cold == NULL)
651 }
while (close < v->stop);
668 for (
i = n - 1;
i > 0;
i--)
687 if ((
size_t) n < v->nmatch)
710 if ((
size_t) n >= v->
nmatch)
854 mid =
longest(v, d, begin, end, (
int *) NULL);
858 MDEBUG((
"%d: tentative midpoint %ld\n", t->
id,
LOFF(mid)));
864 if (
longest(v, d2, mid, end, (
int *) NULL) == end)
873 MDEBUG((
"%d: successful\n", t->
id));
888 MDEBUG((
"%d: no midpoint\n", t->
id));
891 mid =
longest(v, d, begin, mid - 1, (
int *) NULL);
896 MDEBUG((
"%d: failed midpoint\n", t->
id));
935 mid =
shortest(v, d, begin, begin, end, (
chr **) NULL, (
int *) NULL);
939 MDEBUG((
"%d: tentative midpoint %ld\n", t->
id,
LOFF(mid)));
945 if (
longest(v, d2, mid, end, (
int *) NULL) == end)
954 MDEBUG((
"%d: successful\n", t->
id));
969 MDEBUG((
"%d: no midpoint\n", t->
id));
972 mid =
shortest(v, d, begin, mid + 1, end, (
chr **) NULL, (
int *) NULL);
977 MDEBUG((
"%d: failed midpoint\n", t->
id));
1011 assert((
size_t) n < v->nmatch);
1013 MDEBUG((
"%d: cbrdissect %d{%d-%d} %ld-%ld\n", t->
id, n, min, max,
1017 if (v->
pmatch[n].rm_so == -1)
1029 if (begin == end && min <= max)
1031 MDEBUG((
"%d: backref matched trivially\n", t->
id));
1041 MDEBUG((
"%d: backref matched trivially\n", t->
id));
1053 if (tlen % brlen != 0)
1055 numreps = tlen / brlen;
1056 if (numreps < min || (numreps > max && max !=
DUPINF))
1061 while (numreps-- > 0)
1063 if ((*v->
g->compare) (brstring, p, brlen) != 0)
1068 MDEBUG((
"%d: backref matched\n", t->
id));
1098 if (
longest(v, d, begin, end, (
int *) NULL) == end)
1100 MDEBUG((
"%d: caltdissect matched\n", t->
id));
1148 min_matches = t->
min;
1149 if (min_matches <= 0)
1161 max_matches = end - begin;
1163 max_matches = t->
max;
1164 if (max_matches < min_matches)
1165 max_matches = min_matches;
1166 endpts = (
chr **)
MALLOC((max_matches + 1) *
sizeof(
chr *));
1197 endpts[k] =
longest(v, d, endpts[k - 1], limit, (
int *) NULL);
1203 if (endpts[k] == NULL)
1209 MDEBUG((
"%d: working endpoint %d: %ld\n",
1210 t->
id, k,
LOFF(endpts[k])));
1216 if (endpts[k] != end)
1219 if (k >= max_matches)
1227 if (endpts[k] == endpts[k - 1] &&
1228 (k >= min_matches || min_matches - k < end - endpts[k]))
1242 if (k < min_matches)
1245 MDEBUG((
"%d: verifying %d..%d\n", t->
id, nverified + 1, k));
1247 for (
i = nverified + 1;
i <= k;
i++)
1267 MDEBUG((
"%d: successful\n", t->
id));
1283 chr *prev_end = endpts[k - 1];
1285 if (endpts[k] > prev_end)
1287 limit = endpts[k] - 1;
1288 if (limit > prev_end ||
1289 (k < min_matches && min_matches - k >= end - prev_end))
1307 if (t->
min == 0 && begin == end)
1309 MDEBUG((
"%d: allowing zero matches\n", t->
id));
1348 min_matches = t->
min;
1349 if (min_matches <= 0)
1353 MDEBUG((
"%d: allowing zero matches\n", t->
id));
1368 max_matches = end - begin;
1370 max_matches = t->
max;
1371 if (max_matches < min_matches)
1372 max_matches = min_matches;
1373 endpts = (
chr **)
MALLOC((max_matches + 1) *
sizeof(
chr *));
1404 if (limit == endpts[k - 1] &&
1406 (k >= min_matches || min_matches - k < end - limit))
1410 if (k >= max_matches)
1414 endpts[k] =
shortest(v, d, endpts[k - 1], limit, end,
1415 (
chr **) NULL, (
int *) NULL);
1421 if (endpts[k] == NULL)
1427 MDEBUG((
"%d: working endpoint %d: %ld\n",
1428 t->
id, k,
LOFF(endpts[k])));
1434 if (endpts[k] != end)
1437 if (k >= max_matches)
1445 limit = endpts[k - 1];
1455 if (k < min_matches)
1458 MDEBUG((
"%d: verifying %d..%d\n", t->
id, nverified + 1, k));
1460 for (
i = nverified + 1;
i <= k;
i++)
1480 MDEBUG((
"%d: successful\n", t->
id));
1495 if (endpts[k] < end)
1497 limit = endpts[k] + 1;
static void cleanup(void)
if(TABLE==NULL||TABLE_index==NULL)
static void const char fflush(stdout)
void pg_set_regex_collation(Oid collation)
static int find(struct vars *v, struct cnfa *cnfa, struct colormap *cm)
static int citerdissect(struct vars *v, struct subre *t, chr *begin, chr *end)
static void freedfa(struct dfa *d)
static struct sset * initialize(struct vars *v, struct dfa *d, chr *start)
static struct sset * getvacant(struct vars *v, struct dfa *d, chr *cp, chr *start)
static void zapallsubs(regmatch_t *p, size_t n)
static int lacon(struct vars *v, struct cnfa *pcnfa, chr *cp, color co)
static int ccondissect(struct vars *v, struct subre *t, chr *begin, chr *end)
static int cfind(struct vars *v, struct cnfa *cnfa, struct colormap *cm)
static int creviterdissect(struct vars *v, struct subre *t, chr *begin, chr *end)
static int cfindloop(struct vars *v, struct cnfa *cnfa, struct colormap *cm, struct dfa *d, struct dfa *s, chr **coldp)
static chr * longest(struct vars *v, struct dfa *d, chr *start, chr *stop, int *hitstopp)
static unsigned hash(unsigned *uv, int n)
static int cbrdissect(struct vars *v, struct subre *t, chr *begin, chr *end)
static struct dfa * getladfa(struct vars *v, int n)
static struct dfa * getsubdfa(struct vars *v, struct subre *t)
static int crevcondissect(struct vars *v, struct subre *t, chr *begin, chr *end)
static struct sset * pickss(struct vars *v, struct dfa *d, chr *cp, chr *start)
static struct dfa * newdfa(struct vars *v, struct cnfa *cnfa, struct colormap *cm, struct smalldfa *sml)
static int cdissect(struct vars *v, struct subre *t, chr *begin, chr *end)
static chr * lastcold(struct vars *v, struct dfa *d)
static void zaptreesubs(struct vars *v, struct subre *t)
static int caltdissect(struct vars *v, struct subre *t, chr *begin, chr *end)
static chr * dfa_backref(struct vars *v, struct dfa *d, chr *start, chr *min, chr *max, bool shortest)
static void subset(struct vars *v, struct subre *sub, chr *begin, chr *end)
static chr * shortest(struct vars *v, struct dfa *d, chr *start, chr *min, chr *max, chr **coldp, int *hitstopp)
int pg_regexec(regex_t *re, const chr *string, size_t len, size_t search_start, rm_detail_t *details, size_t nmatch, regmatch_t pmatch[], int flags)
static int matchuntil(struct vars *v, struct dfa *d, chr *probe, struct sset **lastcss, chr **lastcp)
static struct sset * miss(struct vars *v, struct dfa *d, struct sset *css, color co, chr *cp, chr *start)
#define STACK_TOO_DEEP(re)
unsigned statesarea[FEWSTATES *2+WORK]
struct arcp incarea[FEWSTATES *2 *FEWCOLORS]
struct sset * outsarea[FEWSTATES *2 *FEWCOLORS]
struct sset ssets[FEWSTATES *2]