[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: [libvirt] [PATCH v2 1/3] util: introduce virStringMatch



On Fri, May 12, 2017 at 02:57:54PM +0200, Pavel Hrdina wrote:
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>
---

changes in v2:
   - virStringMatch returns bool instead of int

src/libvirt_private.syms |  1 +
src/util/virstring.c     | 33 +++++++++++++++++++++++++++++++++
src/util/virstring.h     |  3 +++
tests/virstringtest.c    | 47 +++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 84 insertions(+)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index afb9100c50..d32c6e7549 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2621,6 +2621,7 @@ virStringListHasString;
virStringListJoin;
virStringListLength;
virStringListRemove;
+virStringMatch;
virStringReplace;
virStringSearch;
virStringSortCompare;
diff --git a/src/util/virstring.c b/src/util/virstring.c
index 335e773d78..c8d6b0ca02 100644
--- a/src/util/virstring.c
+++ b/src/util/virstring.c
@@ -979,6 +979,39 @@ virStringSearch(const char *str,
}

/**
+ * 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));
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("error while compiling regular expression '%s': %s"),
+                       regexp, error);
+        return false;

Don't report the error, just return false.  Or do VIR_WARN() if you
want.  The conversion to bool was meant together with the fact that
lastError should not be set.

ACK with that.

Attachment: signature.asc
Description: Digital signature


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]