/*
* regexp.c
*
* Return results of a regular expression
* NB! Needs the regexp kfun package for DGD.
*
* (C) Frank Schmidt, Jesus@NorseMUD
*
*/
#ifndef __AUTO /* In the regexp object (simul_efun) */
/* how to init us */
#define INIT_REGEXP() init_regexp()
/* define a default cache size to use for compiled regular expressions */
#ifndef REGEXP_CACHE_SIZE
# define REGEXP_CACHE_SIZE 100 /* default regexp cache size */
#endif
/* cache to store repeated patterns in, speeds up process */
private mapping regexp_cache;
/* initialize cache and list */
static void init_regexp() {
regexp_cache = ([ ]);
}
/* return the regular expression, use caching of patterns */
varargs int *regexp(string subject, string pattern, int incase, int reverse) {
string *buffer, cachepatt;
/* add info about if the search should be case-insensitive.
(This info tag will be added no matter what, so it will never
influence the resulting pattern, by neither look-up nor store. */
cachepatt = pattern + "@" + (string)incase;
/* compile regexp pattern or get it from cache */
if ((buffer = regexp_cache[cachepatt]) == 0) {
/* compile... */
buffer = ::regexp_compile(pattern, incase);
while (map_sizeof(regexp_cache) >= REGEXP_CACHE_SIZE) {
/* exceeding cache, delete an element */
map_delete(regexp_cache, map_indices(regexp_cache)[random(REGEXP_CACHE_SIZE)]);
}
/* add current buffer to cache */
regexp_cache[cachepatt] = buffer;
}
/* return final result */
return ::regexp_match(buffer, subject, reverse);
}
#else /* In the auto object */
/* define the regexp object (this object) */
#define REGEXP_OB SIMUL_EFUN
/* regexp sfun */
static varargs int *regexp(string subject, string pattern, int incase, int reverse) {
return REGEXP_OB->regexp(subject, pattern, incase, reverse);
}
/* override kfuns that (maybe) shouldn't be accessed? */
#if 0
private varargs string *regexp_compile(string pattern, int case_insensitive) {}
private varargs int *regexp_match(string *pattern, string subject, int reverse) {}
#endif
#endif