chiark / gitweb /
configure.ac, acinclude.m4, lib/regexp.[ch]: Support `libpcre2'.
[disorder] / lib / regexp.c
index cb5ecc66e3d18ea7f574102d1daf5269dff3c55d..51993399e380dcea996f1829f7dc8fe1c0d17f2c 100644 (file)
 #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