[libvirt] [PATCH 1/2] util: introduce virStringMatch

Martin Kletzander mkletzan at redhat.com
Fri May 12 09:03:39 UTC 2017


On Thu, May 11, 2017 at 05:49:53PM +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 at redhat.com>
>---
> src/libvirt_private.syms |  1 +
> src/util/virstring.c     | 34 ++++++++++++++++++++++++++++++++++
> src/util/virstring.h     |  3 +++
> tests/virstringtest.c    | 47 +++++++++++++++++++++++++++++++++++++++++++++++
> 4 files changed, 85 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..b95a8926bd 100644
>--- a/src/util/virstring.c
>+++ b/src/util/virstring.c
>@@ -979,6 +979,40 @@ virStringSearch(const char *str,
> }
>
> /**
>+ * virStringMatch:
>+ * @str: string to match
>+ * @regexp: POSIX Extended regular expression pattern used for matching
>+ *
>+ * Performs a POSIX extended regex search against a string.
>+ * Returns 0 on match, -1 on error, 1 on no match.
>+ */
>+int
>+virStringMatch(const char *str,
>+               const char *regexp)
>+{
>+    regex_t re;
>+    int ret = -1;
>+    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 -1;
>+    }
>+
>+    if ((ret = regexec(&re, str, 0, NULL, 0)) != 0)
>+        ret = 1;

This could be made easier if you just retuned bool true on match and
false on no match/error.  Even though that would ignore one (very
unlikely) error, it works much more nicely with any usage this might get
in libvirt.

ACK with that changed.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: Digital signature
URL: <http://listman.redhat.com/archives/libvir-list/attachments/20170512/5103e632/attachment-0001.sig>


More information about the libvir-list mailing list