]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
util: introduce virStringMatch
authorPavel Hrdina <phrdina@redhat.com>
Thu, 11 May 2017 15:16:13 +0000 (17:16 +0200)
committerPavel Hrdina <phrdina@redhat.com>
Fri, 12 May 2017 14:51:18 +0000 (16:51 +0200)
Simply tries to match the provided regex on a string and returns
the result.  Useful if caller don't care about the matched substring
and want to just test if some pattern patches a string.

Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
src/libvirt_private.syms
src/util/virstring.c
src/util/virstring.h
tests/virstringtest.c

index afb9100c50756975dfbd3b2f1f7224634c0d4f2c..d32c6e75491a36ae742a9701f7ec7cddabf344cb 100644 (file)
@@ -2621,6 +2621,7 @@ virStringListHasString;
 virStringListJoin;
 virStringListLength;
 virStringListRemove;
+virStringMatch;
 virStringReplace;
 virStringSearch;
 virStringSortCompare;
index 335e773d78ef7d84dee2d83ae27158b016ffb738..089b53946a212ece919689f6d6a550d157c715f2 100644 (file)
@@ -978,6 +978,38 @@ virStringSearch(const char *str,
     return ret;
 }
 
+/**
+ * virStringMatch:
+ * @str: string to match
+ * @regexp: POSIX Extended regular expression pattern used for matching
+ *
+ * Performs a POSIX extended regex match against a string.
+ * Returns true on match, false on error or no match.
+ */
+bool
+virStringMatch(const char *str,
+               const char *regexp)
+{
+    regex_t re;
+    int rv;
+
+    VIR_DEBUG("match '%s' for '%s'", str, regexp);
+
+    if ((rv = regcomp(&re, regexp, REG_EXTENDED | REG_NOSUB)) != 0) {
+        char error[100];
+        regerror(rv, &re, error, sizeof(error));
+        VIR_WARN("error while compiling regular expression '%s': %s",
+                 regexp, error);
+        return false;
+    }
+
+    rv = regexec(&re, str, 0, NULL, 0);
+
+    regfree(&re);
+
+    return rv == 0;
+}
+
 /**
  * virStringReplace:
  * @haystack: the source string to process
index c545ca3f0dc13189447820db2b2667c60b177aff..0038a405b121eb9d2bd3e09c6118aa613a85a6d4 100644 (file)
@@ -274,6 +274,9 @@ ssize_t virStringSearch(const char *str,
                         char ***matches)
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(4);
 
+bool virStringMatch(const char *str,
+                    const char *regexp);
+
 char *virStringReplace(const char *haystack,
                        const char *oldneedle,
                        const char *newneedle)
index 96bc79f8325cc34032d89d43f376bad2d36eca42..97c6e76dcbfb3da0a75fa66d09eebbfd4bc21d46 100644 (file)
@@ -480,6 +480,38 @@ testStringSearch(const void *opaque)
 }
 
 
+struct stringMatchData {
+    const char *str;
+    const char *regexp;
+    bool expectMatch;
+};
+
+static int
+testStringMatch(const void *opaque)
+{
+    const struct stringMatchData *data = opaque;
+    bool match;
+
+    match = virStringMatch(data->str, data->regexp);
+
+    if (data->expectMatch) {
+        if (!match) {
+            fprintf(stderr, "expected match for '%s' on '%s' but got no match\n",
+                    data->regexp, data->str);
+            return -1;
+        }
+    } else {
+        if (match) {
+            fprintf(stderr, "expected no match for '%s' on '%s' but got match\n",
+                    data->regexp, data->str);
+            return -1;
+        }
+    }
+
+    return 0;
+}
+
+
 struct stringReplaceData {
     const char *haystack;
     const char *oldneedle;
@@ -803,6 +835,21 @@ mymain(void)
     const char *matches3[] = { "foo", "bar" };
     TEST_SEARCH("1foo2bar3eek", "([a-z]+)", 2, 2, matches3, false);
 
+#define TEST_MATCH(s, r, m)                                                 \
+    do {                                                                    \
+        struct stringMatchData data = {                                     \
+            .str = s,                                                       \
+            .regexp = r,                                                    \
+            .expectMatch = m,                                               \
+        };                                                                  \
+        if (virTestRun("virStringMatch " s, testStringMatch, &data) < 0)    \
+            ret = -1;                                                       \
+    } while (0)
+
+    TEST_MATCH("foo", "foo", true);
+    TEST_MATCH("foobar", "f[o]+", true);
+    TEST_MATCH("foobar", "^f[o]+$", false);
+
 #define TEST_REPLACE(h, o, n, r)                                             \
     do {                                                                     \
         struct stringReplaceData data = {                                    \