#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <sys/types.h>
#include "system.h"
#include "kwset.h"
#include "obstack.h"
#ifdef GREP
extern char *xmalloc();
# undef malloc
# define malloc xmalloc
#endif
#define NCHAR (UCHAR_MAX + 1)
#define obstack_chunk_alloc malloc
#define obstack_chunk_free free
struct tree
{
struct tree *llink;
struct tree *rlink;
struct trie *trie;
unsigned char label;
char balance;
};
struct trie
{
unsigned int accepting;
struct tree *links;
struct trie *parent;
struct trie *next;
struct trie *fail;
int depth;
int shift;
int maxshift;
};
struct kwset
{
struct obstack obstack;
int words;
struct trie *trie;
int mind;
int maxd;
unsigned char delta[NCHAR];
struct trie *next[NCHAR];
char *target;
int mind2;
char *trans;
};
static void enqueue PARAMS((struct tree *, struct trie **));
static void treefails PARAMS((register struct tree *, struct trie *, struct trie *));
static void treedelta PARAMS((register struct tree *,register unsigned int, unsigned char *));
static int hasevery PARAMS((register struct tree *, register struct tree *));
static void treenext PARAMS((struct tree *, struct trie **));
static char * bmexec PARAMS((kwset_t, char *, size_t));
static char * cwexec PARAMS((kwset_t, char *, size_t, struct kwsmatch *));
kwset_t
kwsalloc (char *trans)
{
struct kwset *kwset;
kwset = (struct kwset *) malloc(sizeof (struct kwset));
if (!kwset)
return 0;
obstack_init(&kwset->obstack);
kwset->words = 0;
kwset->trie
= (struct trie *) obstack_alloc(&kwset->obstack, sizeof (struct trie));
if (!kwset->trie)
{
kwsfree((kwset_t) kwset);
return 0;
}
kwset->trie->accepting = 0;
kwset->trie->links = 0;
kwset->trie->parent = 0;
kwset->trie->next = 0;
kwset->trie->fail = 0;
kwset->trie->depth = 0;
kwset->trie->shift = 0;
kwset->mind = INT_MAX;
kwset->maxd = -1;
kwset->target = 0;
kwset->trans = trans;
return (kwset_t) kwset;
}
char *
kwsincr (kwset_t kws, char *text, size_t len)
{
struct kwset *kwset;
register struct trie *trie;
register unsigned char label;
register struct tree *link;
register int depth;
struct tree *links[12];
enum { L, R } dirs[12];
struct tree *t, *r, *l, *rl, *lr;
kwset = (struct kwset *) kws;
trie = kwset->trie;
text += len;
while (len--)
{
label = kwset->trans ? kwset->trans[(unsigned char) *--text] : *--text;
link = trie->links;
links[0] = (struct tree *) &trie->links;
dirs[0] = L;
depth = 1;
while (link && label != link->label)
{
links[depth] = link;
if (label < link->label)
dirs[depth++] = L, link = link->llink;
else
dirs[depth++] = R, link = link->rlink;
}
if (!link)
{
link = (struct tree *) obstack_alloc(&kwset->obstack,
sizeof (struct tree));
if (!link)
return _("memory exhausted");
link->llink = 0;
link->rlink = 0;
link->trie = (struct trie *) obstack_alloc(&kwset->obstack,
sizeof (struct trie));
if (!link->trie)
return _("memory exhausted");
link->trie->accepting = 0;
link->trie->links = 0;
link->trie->parent = trie;
link->trie->next = 0;
link->trie->fail = 0;
link->trie->depth = trie->depth + 1;
link->trie->shift = 0;
link->label = label;
link->balance = 0;
if (dirs[--depth] == L)
links[depth]->llink = link;
else
links[depth]->rlink = link;
while (depth && !links[depth]->balance)
{
if (dirs[depth] == L)
--links[depth]->balance;
else
++links[depth]->balance;
--depth;
}
if (depth && ((dirs[depth] == L && --links[depth]->balance)
|| (dirs[depth] == R && ++links[depth]->balance)))
{
switch (links[depth]->balance)
{
case (char) -2:
switch (dirs[depth + 1])
{
case L:
r = links[depth], t = r->llink, rl = t->rlink;
t->rlink = r, r->llink = rl;
t->balance = r->balance = 0;
break;
case R:
r = links[depth], l = r->llink, t = l->rlink;
rl = t->rlink, lr = t->llink;
t->llink = l, l->rlink = lr, t->rlink = r, r->llink = rl;
l->balance = t->balance != 1 ? 0 : -1;
r->balance = t->balance != (char) -1 ? 0 : 1;
t->balance = 0;
break;
default:
abort ();
}
break;
case 2:
switch (dirs[depth + 1])
{
case R:
l = links[depth], t = l->rlink, lr = t->llink;
t->llink = l, l->rlink = lr;
t->balance = l->balance = 0;
break;
case L:
l = links[depth], r = l->rlink, t = r->llink;
lr = t->llink, rl = t->rlink;
t->llink = l, l->rlink = lr, t->rlink = r, r->llink = rl;
l->balance = t->balance != 1 ? 0 : -1;
r->balance = t->balance != (char) -1 ? 0 : 1;
t->balance = 0;
break;
default:
abort ();
}
break;
default:
abort ();
}
if (dirs[depth - 1] == L)
links[depth - 1]->llink = t;
else
links[depth - 1]->rlink = t;
}
}
trie = link->trie;
}
if (!trie->accepting)
trie->accepting = 1 + 2 * kwset->words;
++kwset->words;
if (trie->depth < kwset->mind)
kwset->mind = trie->depth;
if (trie->depth > kwset->maxd)
kwset->maxd = trie->depth;
return 0;
}
static void
enqueue (struct tree *tree, struct trie **last)
{
if (!tree)
return;
enqueue(tree->llink, last);
enqueue(tree->rlink, last);
(*last) = (*last)->next = tree->trie;
}
static void
treefails (register struct tree *tree, struct trie *fail, struct trie *recourse)
{
register struct tree *link;
if (!tree)
return;
treefails(tree->llink, fail, recourse);
treefails(tree->rlink, fail, recourse);
while (fail)
{
link = fail->links;
while (link && tree->label != link->label)
if (tree->label < link->label)
link = link->llink;
else
link = link->rlink;
if (link)
{
tree->trie->fail = link->trie;
return;
}
fail = fail->fail;
}
tree->trie->fail = recourse;
}
static void
treedelta (register struct tree *tree,
register unsigned int depth,
unsigned char delta[])
{
if (!tree)
return;
treedelta(tree->llink, depth, delta);
treedelta(tree->rlink, depth, delta);
if (depth < delta[tree->label])
delta[tree->label] = depth;
}
static int
hasevery (register struct tree *a, register struct tree *b)
{
if (!b)
return 1;
if (!hasevery(a, b->llink))
return 0;
if (!hasevery(a, b->rlink))
return 0;
while (a && b->label != a->label)
if (b->label < a->label)
a = a->llink;
else
a = a->rlink;
return !!a;
}
static void
treenext (struct tree *tree, struct trie *next[])
{
if (!tree)
return;
treenext(tree->llink, next);
treenext(tree->rlink, next);
next[tree->label] = tree->trie;
}
char *
kwsprep (kwset_t kws)
{
register struct kwset *kwset;
register int i;
register struct trie *curr, *fail;
register char *trans;
unsigned char delta[NCHAR];
struct trie *last, *next[NCHAR];
kwset = (struct kwset *) kws;
if (kwset->mind < 256)
for (i = 0; i < NCHAR; ++i)
delta[i] = kwset->mind;
else
for (i = 0; i < NCHAR; ++i)
delta[i] = 255;
if (kwset->words == 1 && kwset->trans == 0)
{
kwset->target = obstack_alloc(&kwset->obstack, kwset->mind);
for (i = kwset->mind - 1, curr = kwset->trie; i >= 0; --i)
{
kwset->target[i] = curr->links->label;
curr = curr->links->trie;
}
for (i = 0; i < kwset->mind; ++i)
delta[(unsigned char) kwset->target[i]] = kwset->mind - (i + 1);
kwset->mind2 = kwset->mind;
for (i = 0; i < kwset->mind - 1; ++i)
if (kwset->target[i] == kwset->target[kwset->mind - 1])
kwset->mind2 = kwset->mind - (i + 1);
}
else
{
for (curr = last = kwset->trie; curr; curr = curr->next)
{
enqueue(curr->links, &last);
curr->shift = kwset->mind;
curr->maxshift = kwset->mind;
treedelta(curr->links, curr->depth, delta);
treefails(curr->links, curr->fail, kwset->trie);
for (fail = curr->fail; fail; fail = fail->fail)
{
if (!hasevery(fail->links, curr->links))
if (curr->depth - fail->depth < fail->shift)
fail->shift = curr->depth - fail->depth;
if (curr->accepting && fail->maxshift > curr->depth - fail->depth)
fail->maxshift = curr->depth - fail->depth;
}
}
for (curr = kwset->trie->next; curr; curr = curr->next)
{
if (curr->maxshift > curr->parent->maxshift)
curr->maxshift = curr->parent->maxshift;
if (curr->shift > curr->maxshift)
curr->shift = curr->maxshift;
}
for (i = 0; i < NCHAR; ++i)
next[i] = 0;
treenext(kwset->trie->links, next);
if ((trans = kwset->trans) != 0)
for (i = 0; i < NCHAR; ++i)
kwset->next[i] = next[(unsigned char) trans[i]];
else
for (i = 0; i < NCHAR; ++i)
kwset->next[i] = next[i];
}
if ((trans = kwset->trans) != 0)
for (i = 0; i < NCHAR; ++i)
kwset->delta[i] = delta[(unsigned char) trans[i]];
else
for (i = 0; i < NCHAR; ++i)
kwset->delta[i] = delta[i];
return 0;
}
#define U(C) ((unsigned char) (C))
static char *
bmexec (kwset_t kws, char *text, size_t size)
{
struct kwset *kwset;
register unsigned char *d1;
register char *ep, *sp, *tp;
register int d, gc, i, len, md2;
kwset = (struct kwset *) kws;
len = kwset->mind;
if (len == 0)
return text;
if (len > size)
return 0;
if (len == 1)
return memchr(text, kwset->target[0], size);
d1 = kwset->delta;
sp = kwset->target + len;
gc = U(sp[-2]);
md2 = kwset->mind2;
tp = text + len;
if (size > 12 * len)
for (ep = text + size - 11 * len;;)
{
while (tp <= ep)
{
d = d1[U(tp[-1])], tp += d;
d = d1[U(tp[-1])], tp += d;
if (d == 0)
goto found;
d = d1[U(tp[-1])], tp += d;
d = d1[U(tp[-1])], tp += d;
d = d1[U(tp[-1])], tp += d;
if (d == 0)
goto found;
d = d1[U(tp[-1])], tp += d;
d = d1[U(tp[-1])], tp += d;
d = d1[U(tp[-1])], tp += d;
if (d == 0)
goto found;
d = d1[U(tp[-1])], tp += d;
d = d1[U(tp[-1])], tp += d;
}
break;
found:
if (U(tp[-2]) == gc)
{
for (i = 3; i <= len && U(tp[-i]) == U(sp[-i]); ++i)
;
if (i > len)
return tp - len;
}
tp += md2;
}
ep = text + size;
d = d1[U(tp[-1])];
while (d <= ep - tp)
{
d = d1[U((tp += d)[-1])];
if (d != 0)
continue;
if (U(tp[-2]) == gc)
{
for (i = 3; i <= len && U(tp[-i]) == U(sp[-i]); ++i)
;
if (i > len)
return tp - len;
}
d = md2;
}
return 0;
}
static char *
cwexec (kwset_t kws, char *text, size_t len, struct kwsmatch *kwsmatch)
{
struct kwset *kwset;
struct trie **next, *trie, *accept;
char *beg, *lim, *mch, *lmch;
register unsigned char c, *delta;
register int d;
register char *end, *qlim;
register struct tree *tree;
register char *trans;
#ifdef lint
accept = NULL;
#endif
kwset = (struct kwset *) kws;
if (len < kwset->mind)
return 0;
next = kwset->next;
delta = kwset->delta;
trans = kwset->trans;
lim = text + len;
end = text;
if ((d = kwset->mind) != 0)
mch = 0;
else
{
mch = text, accept = kwset->trie;
goto match;
}
if (len >= 4 * kwset->mind)
qlim = lim - 4 * kwset->mind;
else
qlim = 0;
while (lim - end >= d)
{
if (qlim && end <= qlim)
{
end += d - 1;
while ((d = delta[c = *end]) && end < qlim)
{
end += d;
end += delta[(unsigned char) *end];
end += delta[(unsigned char) *end];
}
++end;
}
else
d = delta[c = (end += d)[-1]];
if (d)
continue;
beg = end - 1;
trie = next[c];
if (trie->accepting)
{
mch = beg;
accept = trie;
}
d = trie->shift;
while (beg > text)
{
c = trans ? trans[(unsigned char) *--beg] : *--beg;
tree = trie->links;
while (tree && c != tree->label)
if (c < tree->label)
tree = tree->llink;
else
tree = tree->rlink;
if (tree)
{
trie = tree->trie;
if (trie->accepting)
{
mch = beg;
accept = trie;
}
}
else
break;
d = trie->shift;
}
if (mch)
goto match;
}
return 0;
match:
if (lim - mch > kwset->maxd)
lim = mch + kwset->maxd;
lmch = 0;
d = 1;
while (lim - end >= d)
{
if ((d = delta[c = (end += d)[-1]]) != 0)
continue;
beg = end - 1;
if (!(trie = next[c]))
{
d = 1;
continue;
}
if (trie->accepting && beg <= mch)
{
lmch = beg;
accept = trie;
}
d = trie->shift;
while (beg > text)
{
c = trans ? trans[(unsigned char) *--beg] : *--beg;
tree = trie->links;
while (tree && c != tree->label)
if (c < tree->label)
tree = tree->llink;
else
tree = tree->rlink;
if (tree)
{
trie = tree->trie;
if (trie->accepting && beg <= mch)
{
lmch = beg;
accept = trie;
}
}
else
break;
d = trie->shift;
}
if (lmch)
{
mch = lmch;
goto match;
}
if (!d)
d = 1;
}
if (kwsmatch)
{
kwsmatch->index = accept->accepting / 2;
kwsmatch->beg[0] = mch;
kwsmatch->size[0] = accept->depth;
}
return mch;
}
char *
kwsexec (kwset_t kws, char *text, size_t size, struct kwsmatch *kwsmatch)
{
struct kwset *kwset;
char *ret;
kwset = (struct kwset *) kws;
if (kwset->words == 1 && kwset->trans == 0)
{
ret = bmexec(kws, text, size);
if (kwsmatch != 0 && ret != 0)
{
kwsmatch->index = 0;
kwsmatch->beg[0] = ret;
kwsmatch->size[0] = kwset->mind;
}
return ret;
}
else
return cwexec(kws, text, size, kwsmatch);
}
void
kwsfree (kwset_t kws)
{
struct kwset *kwset;
kwset = (struct kwset *) kws;
obstack_free(&kwset->obstack, 0);
free(kws);
}