]> git.sur5r.net Git - i3/i3/blobdiff - src/regex.c
Merge branch 'release-4.16.1'
[i3/i3] / src / regex.c
index da8f91d8a67fbd4f09e35fc0a512598d0c0ccd11..8f039157f6f7547d57e66733aa4bffdb23f72345 100644 (file)
@@ -2,11 +2,11 @@
  * vim:ts=4:sw=4:expandtab
  *
  * i3 - an improved dynamic tiling window manager
- * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
+ * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
  *
+ * regex.c: Interface to libPCRE (perl compatible regular expressions).
  *
  */
-
 #include "all.h"
 
 /*
@@ -23,11 +23,12 @@ struct regex *regex_new(const char *pattern) {
     const char *error;
     int errorcode, offset;
 
-    struct regex *re = scalloc(sizeof(struct regex));
+    struct regex *re = scalloc(1, sizeof(struct regex));
     re->pattern = sstrdup(pattern);
+    int options = PCRE_UTF8;
     /* We use PCRE_UCP so that \B, \b, \D, \d, \S, \s, \W, \w and some POSIX
      * character classes play nicely with Unicode */
-    int options = PCRE_UCP | PCRE_UTF8;
+    options |= PCRE_UCP;
     while (!(re->regex = pcre_compile2(pattern, options, &errorcode, &error, &offset, NULL))) {
         /* If the error is that PCRE was not compiled with UTF-8 support we
          * disable it and try again */
@@ -37,6 +38,7 @@ struct regex *regex_new(const char *pattern) {
         }
         ELOG("PCRE regular expression compilation failed at %d: %s\n",
              offset, error);
+        regex_free(re);
         return NULL;
     }
     re->extra = pcre_study(re->regex, 0, &error);
@@ -49,10 +51,23 @@ struct regex *regex_new(const char *pattern) {
     return re;
 }
 
+/*
+ * Frees the given regular expression. It must not be used afterwards!
+ *
+ */
+void regex_free(struct regex *regex) {
+    if (!regex)
+        return;
+    FREE(regex->pattern);
+    FREE(regex->regex);
+    FREE(regex->extra);
+    FREE(regex);
+}
+
 /*
  * Checks if the given regular expression matches the given input and returns
  * true if it does. In either case, it logs the outcome using LOG(), so it will
- * be visible without any debug loglevel.
+ * be visible without debug logging.
  *
  */
 bool regex_matches(struct regex *regex, const char *input) {