3e5111
From a05097ee436b03de9280a9ae25f85a150ddaa327 Mon Sep 17 00:00:00 2001
3e5111
Message-Id: <a05097ee436b03de9280a9ae25f85a150ddaa327@dist-git>
3e5111
From: Pavel Hrdina <phrdina@redhat.com>
3e5111
Date: Tue, 16 May 2017 11:46:11 +0200
3e5111
Subject: [PATCH] util: introduce virStringMatch
3e5111
3e5111
Simply tries to match the provided regex on a string and returns
3e5111
the result.  Useful if caller don't care about the matched substring
3e5111
and want to just test if some pattern patches a string.
3e5111
3e5111
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
3e5111
(cherry picked from commit f15f155403e36a66e3568676105fdca725ad2c52)
3e5111
3e5111
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1446980
3e5111
3e5111
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
3e5111
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
3e5111
---
3e5111
 src/libvirt_private.syms |  1 +
3e5111
 src/util/virstring.c     | 32 ++++++++++++++++++++++++++++++++
3e5111
 src/util/virstring.h     |  3 +++
3e5111
 tests/virstringtest.c    | 47 +++++++++++++++++++++++++++++++++++++++++++++++
3e5111
 4 files changed, 83 insertions(+)
3e5111
3e5111
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
3e5111
index 134d4a832..06c338384 100644
3e5111
--- a/src/libvirt_private.syms
3e5111
+++ b/src/libvirt_private.syms
3e5111
@@ -2599,6 +2599,7 @@ virStringListHasString;
3e5111
 virStringListJoin;
3e5111
 virStringListLength;
3e5111
 virStringListRemove;
3e5111
+virStringMatch;
3e5111
 virStringReplace;
3e5111
 virStringSearch;
3e5111
 virStringSortCompare;
3e5111
diff --git a/src/util/virstring.c b/src/util/virstring.c
3e5111
index 69abc267b..5b2be317b 100644
3e5111
--- a/src/util/virstring.c
3e5111
+++ b/src/util/virstring.c
3e5111
@@ -979,6 +979,38 @@ virStringSearch(const char *str,
3e5111
 }
3e5111
 
3e5111
 /**
3e5111
+ * virStringMatch:
3e5111
+ * @str: string to match
3e5111
+ * @regexp: POSIX Extended regular expression pattern used for matching
3e5111
+ *
3e5111
+ * Performs a POSIX extended regex match against a string.
3e5111
+ * Returns true on match, false on error or no match.
3e5111
+ */
3e5111
+bool
3e5111
+virStringMatch(const char *str,
3e5111
+               const char *regexp)
3e5111
+{
3e5111
+    regex_t re;
3e5111
+    int rv;
3e5111
+
3e5111
+    VIR_DEBUG("match '%s' for '%s'", str, regexp);
3e5111
+
3e5111
+    if ((rv = regcomp(&re, regexp, REG_EXTENDED | REG_NOSUB)) != 0) {
3e5111
+        char error[100];
3e5111
+        regerror(rv, &re, error, sizeof(error));
3e5111
+        VIR_WARN("error while compiling regular expression '%s': %s",
3e5111
+                 regexp, error);
3e5111
+        return false;
3e5111
+    }
3e5111
+
3e5111
+    rv = regexec(&re, str, 0, NULL, 0);
3e5111
+
3e5111
+    regfree(&re);
3e5111
+
3e5111
+    return rv == 0;
3e5111
+}
3e5111
+
3e5111
+/**
3e5111
  * virStringReplace:
3e5111
  * @haystack: the source string to process
3e5111
  * @oldneedle: the substring to locate
3e5111
diff --git a/src/util/virstring.h b/src/util/virstring.h
3e5111
index a5550e30d..0df03473a 100644
3e5111
--- a/src/util/virstring.h
3e5111
+++ b/src/util/virstring.h
3e5111
@@ -274,6 +274,9 @@ ssize_t virStringSearch(const char *str,
3e5111
                         char ***matches)
3e5111
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(4);
3e5111
 
3e5111
+bool virStringMatch(const char *str,
3e5111
+                    const char *regexp);
3e5111
+
3e5111
 char *virStringReplace(const char *haystack,
3e5111
                        const char *oldneedle,
3e5111
                        const char *newneedle)
3e5111
diff --git a/tests/virstringtest.c b/tests/virstringtest.c
3e5111
index db1731f96..d27c8a5a8 100644
3e5111
--- a/tests/virstringtest.c
3e5111
+++ b/tests/virstringtest.c
3e5111
@@ -480,6 +480,38 @@ testStringSearch(const void *opaque)
3e5111
 }
3e5111
 
3e5111
 
3e5111
+struct stringMatchData {
3e5111
+    const char *str;
3e5111
+    const char *regexp;
3e5111
+    bool expectMatch;
3e5111
+};
3e5111
+
3e5111
+static int
3e5111
+testStringMatch(const void *opaque)
3e5111
+{
3e5111
+    const struct stringMatchData *data = opaque;
3e5111
+    bool match;
3e5111
+
3e5111
+    match = virStringMatch(data->str, data->regexp);
3e5111
+
3e5111
+    if (data->expectMatch) {
3e5111
+        if (!match) {
3e5111
+            fprintf(stderr, "expected match for '%s' on '%s' but got no match\n",
3e5111
+                    data->regexp, data->str);
3e5111
+            return -1;
3e5111
+        }
3e5111
+    } else {
3e5111
+        if (match) {
3e5111
+            fprintf(stderr, "expected no match for '%s' on '%s' but got match\n",
3e5111
+                    data->regexp, data->str);
3e5111
+            return -1;
3e5111
+        }
3e5111
+    }
3e5111
+
3e5111
+    return 0;
3e5111
+}
3e5111
+
3e5111
+
3e5111
 struct stringReplaceData {
3e5111
     const char *haystack;
3e5111
     const char *oldneedle;
3e5111
@@ -803,6 +835,21 @@ mymain(void)
3e5111
     const char *matches3[] = { "foo", "bar" };
3e5111
     TEST_SEARCH("1foo2bar3eek", "([a-z]+)", 2, 2, matches3, false);
3e5111
 
3e5111
+#define TEST_MATCH(s, r, m)                                                 \
3e5111
+    do {                                                                    \
3e5111
+        struct stringMatchData data = {                                     \
3e5111
+            .str = s,                                                       \
3e5111
+            .regexp = r,                                                    \
3e5111
+            .expectMatch = m,                                               \
3e5111
+        };                                                                  \
3e5111
+        if (virTestRun("virStringMatch " s, testStringMatch, &data) < 0)    \
3e5111
+            ret = -1;                                                       \
3e5111
+    } while (0)
3e5111
+
3e5111
+    TEST_MATCH("foo", "foo", true);
3e5111
+    TEST_MATCH("foobar", "f[o]+", true);
3e5111
+    TEST_MATCH("foobar", "^f[o]+$", false);
3e5111
+
3e5111
 #define TEST_REPLACE(h, o, n, r)                                             \
3e5111
     do {                                                                     \
3e5111
         struct stringReplaceData data = {                                    \
3e5111
-- 
3e5111
2.13.0
3e5111