X-Git-Url: https://git.distorted.org.uk/~mdw/disorder/blobdiff_plain/a2e9d1472af7f3693cd7843eb3ff4d8b23423fc8..802bb5963b7883e640a87a11dcd7a81bf811a076:/lib/regexp.c diff --git a/lib/regexp.c b/lib/regexp.c index cb5ecc6..5199339 100644 --- a/lib/regexp.c +++ b/lib/regexp.c @@ -23,6 +23,63 @@ #include "regexp.h" #include "mem.h" +#ifdef HAVE_LIBPCRE2 + +static pcre2_general_context *genctx = 0; +static pcre2_compile_context *compctx = 0; + +static void *rxalloc(size_t sz, void attribute((unused)) *q) + { return xmalloc(sz); } +static void rxfree(void *p, void attribute((unused)) *q) + { xfree(p); } + +void regexp_setup(void) +{ + if(genctx) { + pcre2_compile_context_free(compctx); + pcre2_general_context_free(genctx); + } + genctx = pcre2_general_context_create(rxalloc, rxfree, 0); + compctx = pcre2_compile_context_create(genctx); +} + +regexp *regexp_compile(const char *pat, unsigned f, + char *errbuf, size_t errlen, size_t *erroff_out) +{ + int errcode; + PCRE2_SIZE erroff; + regexp *re; + + re = pcre2_compile((PCRE2_SPTR)pat, strlen(pat), f, + &errcode, &erroff, compctx); + if(!re) { + *erroff_out = erroff; + pcre2_get_error_message(errcode, (PCRE2_UCHAR *)errbuf, errlen); + } + return re; +} + +int regexp_match(const regexp *re, const char *s, size_t n, unsigned f, + size_t *ov, size_t on) +{ + int rc; + pcre2_match_data *m; + PCRE2_SIZE *ovp; + size_t i; + + m = pcre2_match_data_create(on, genctx); + rc = pcre2_match(re, (PCRE2_SPTR)s, n, 0, f, m, 0); + ovp = pcre2_get_ovector_pointer(m); + for(i = 0; i < on; i++) ov[i] = ovp[i]; + pcre2_match_data_free(m); + return rc; +} + +void regexp_free(regexp *re) + { pcre2_code_free(re); } + +#else + void regexp_setup(void) { pcre_malloc = xmalloc; @@ -64,6 +121,8 @@ int regexp_match(const regexp *re, const char *s, size_t n, unsigned f, void regexp_free(regexp *re) { pcre_free(re); } +#endif + /* Local Variables: c-basic-offset:2