[libvirt] [PATCH] util: string: Introduce virStringHasChars

Peter Krempa posted 1 patch 6 years, 7 months ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/5299355362f9fd531ad81d2c355b53c62a853fee.1503058116.git.pkrempa@redhat.com
src/util/virstring.c | 23 +++++++++++++++++++----
src/util/virstring.h |  2 ++
2 files changed, 21 insertions(+), 4 deletions(-)
[libvirt] [PATCH] util: string: Introduce virStringHasChars
Posted by Peter Krempa 6 years, 7 months ago
The helper returns true if a string contains any of the given chars.
virStringHasControlChars can be reimplemented using that helper.
---

Pavel was loudly thinking about such function. It turns out that I have
it ready on one of my branches.

 src/util/virstring.c | 23 +++++++++++++++++++----
 src/util/virstring.h |  2 ++
 2 files changed, 21 insertions(+), 4 deletions(-)

diff --git a/src/util/virstring.c b/src/util/virstring.c
index 58abf9dd6..0288d1e67 100644
--- a/src/util/virstring.c
+++ b/src/util/virstring.c
@@ -1195,6 +1195,24 @@ virStringStripIPv6Brackets(char *str)
 }


+/**
+ * virStringHasChars:
+ * @str: string to look for chars in
+ * @chars: chars to find in string @str
+ *
+ * Returns true if @str contains any of the chars in @chars.
+ */
+bool
+virStringHasChars(const char *str,
+                  const char *chars)
+{
+    if (!str)
+        return false;
+
+    return str[strcspn(str, chars)] != '\0';
+}
+
+
 static const char control_chars[] =
     "\x01\x02\x03\x04\x05\x06\x07"
     "\x08" /* \t \n */ "\x0B\x0C" /* \r */ "\x0E\x0F"
@@ -1204,10 +1222,7 @@ static const char control_chars[] =
 bool
 virStringHasControlChars(const char *str)
 {
-    if (!str)
-        return false;
-
-    return str[strcspn(str, control_chars)] != '\0';
+    return virStringHasChars(str, control_chars);
 }


diff --git a/src/util/virstring.h b/src/util/virstring.h
index ff5f0148d..1290fcce1 100644
--- a/src/util/virstring.h
+++ b/src/util/virstring.h
@@ -286,6 +286,8 @@ char *virStringReplace(const char *haystack,
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3);

 void virStringStripIPv6Brackets(char *str);
+bool virStringHasChars(const char *str,
+                       const char *chars);
 bool virStringHasControlChars(const char *str);
 void virStringStripControlChars(char *str);

-- 
2.14.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH] util: string: Introduce virStringHasChars
Posted by Pavel Hrdina 6 years, 7 months ago
On Fri, Aug 18, 2017 at 02:09:57PM +0200, Peter Krempa wrote:
> The helper returns true if a string contains any of the given chars.
> virStringHasControlChars can be reimplemented using that helper.
> ---
> 
> Pavel was loudly thinking about such function. It turns out that I have
> it ready on one of my branches.
> 
>  src/util/virstring.c | 23 +++++++++++++++++++----
>  src/util/virstring.h |  2 ++
>  2 files changed, 21 insertions(+), 4 deletions(-)

Please add an entry to src/libvirt_private.sym, this helper can be used
to replace all occurrences of strspn() and strcspn().

Reviewed-by: Pavel Hrdina <phrdina@redhat.com>
--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list