/*
* This file shows how an interface can be built to cache regexp patterns
* and ultimately provide a more streamlined interface to the regexp kfuns.
*
* Note that since regexp_match() severely depends on the return result from
* regexp_compile() being unaltered, it is a good idea to provide an
* interface like this, and also to mask the regexp_match() kfun from the
* auto object.
*/
# define CACHE_SIZE 10
private mapping cache;
private string *list;
private string last_pattern;
static
void create(void)
{
cache = ([ ]);
list = ({ });
}
int *match(string subject, string pattern)
{
string *buffer;
if ((buffer = cache[pattern]) == 0)
{
buffer = regexp_compile(pattern);
if (sizeof(list) >= CACHE_SIZE)
{
cache[list[0]] = 0;
list = list[1 ..] + ({ pattern });
}
else
list += ({ pattern });
cache[pattern] = buffer;
}
else if (pattern != last_pattern)
{
list = list - ({ pattern }) + ({ pattern });
last_pattern = pattern;
}
return regexp_match(buffer, subject);
}