From nobody Thu Jul 3 16:03:15 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1494517809909625.9105011241131; Thu, 11 May 2017 08:50:09 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id BE1AE8048A; Thu, 11 May 2017 15:50:02 +0000 (UTC) Received: from colo-mx.corp.redhat.com (unknown [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 26F5571C37; Thu, 11 May 2017 15:50:02 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 5A5615EC63; Thu, 11 May 2017 15:50:01 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v4BFnxiI011193 for ; Thu, 11 May 2017 11:49:59 -0400 Received: by smtp.corp.redhat.com (Postfix) id 7639896557; Thu, 11 May 2017 15:49:59 +0000 (UTC) Received: from antique-work.brq.redhat.com (dhcp129-230.brq.redhat.com [10.34.129.230]) by smtp.corp.redhat.com (Postfix) with ESMTP id F0AC69654B for ; Thu, 11 May 2017 15:49:58 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com BE1AE8048A Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=libvir-list-bounces@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com BE1AE8048A From: Pavel Hrdina To: libvir-list@redhat.com Date: Thu, 11 May 2017 17:49:53 +0200 Message-Id: <5d047461de2fece49dd74df523189d39483c672e.1494517711.git.phrdina@redhat.com> In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 1/2] util: introduce virStringMatch X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Thu, 11 May 2017 15:50:09 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" 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 --- 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, } =20 /** + * 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 =3D -1; + int rv; + + VIR_DEBUG("match '%s' for '%s'", str, regexp); + + if ((rv =3D regcomp(&re, regexp, REG_EXTENDED | REG_NOSUB)) !=3D 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 =3D regexec(&re, str, 0, NULL, 0)) !=3D 0) + ret =3D 1; + + regfree(&re); + return ret; +} + +/** * virStringReplace: * @haystack: the source string to process * @oldneedle: the substring to locate diff --git a/src/util/virstring.h b/src/util/virstring.h index c545ca3f0d..3221d99752 100644 --- a/src/util/virstring.h +++ b/src/util/virstring.h @@ -274,6 +274,9 @@ ssize_t virStringSearch(const char *str, char ***matches) ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(4); =20 +int virStringMatch(const char *str, + const char *regexp); + char *virStringReplace(const char *haystack, const char *oldneedle, const char *newneedle) diff --git a/tests/virstringtest.c b/tests/virstringtest.c index 96bc79f832..f8402cd163 100644 --- a/tests/virstringtest.c +++ b/tests/virstringtest.c @@ -480,6 +480,38 @@ testStringSearch(const void *opaque) } =20 =20 +struct stringMatchData { + const char *str; + const char *regexp; + bool expectMatch; +}; + +static int +testStringMatch(const void *opaque) +{ + const struct stringMatchData *data =3D opaque; + int rv; + + rv =3D virStringMatch(data->str, data->regexp); + + if (data->expectMatch) { + if (rv !=3D 0) { + fprintf(stderr, "expected match for '%s' on '%s' but got no ma= tch\n", + data->regexp, data->str); + return -1; + } + } else { + if (rv !=3D 1) { + fprintf(stderr, "expected no match for '%s' on '%s' but got ma= tch\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[] =3D { "foo", "bar" }; TEST_SEARCH("1foo2bar3eek", "([a-z]+)", 2, 2, matches3, false); =20 +#define TEST_MATCH(s, r, m) = \ + do { = \ + struct stringMatchData data =3D { = \ + .str =3D s, = \ + .regexp =3D r, = \ + .expectMatch =3D m, = \ + }; = \ + if (virTestRun("virStringMatch " s, testStringMatch, &data) < 0) = \ + ret =3D -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 =3D { = \ --=20 2.12.2 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list