render / rpms / libvirt

Forked from rpms/libvirt 10 months ago
Clone
d76c62
From 494b2a24ad03653b4a2658a741669943555744bf Mon Sep 17 00:00:00 2001
d76c62
Message-Id: <494b2a24ad03653b4a2658a741669943555744bf@dist-git>
d76c62
From: =?UTF-8?q?J=C3=A1n=20Tomko?= <jtomko@redhat.com>
d76c62
Date: Fri, 13 Mar 2020 13:08:07 +0100
d76c62
Subject: [PATCH] util: add virBufferTrimChars
d76c62
MIME-Version: 1.0
d76c62
Content-Type: text/plain; charset=UTF-8
d76c62
Content-Transfer-Encoding: 8bit
d76c62
d76c62
A new helper for trimming combinations of specified characters from
d76c62
the tail of the buffer.
d76c62
d76c62
Signed-off-by: Ján Tomko <jtomko@redhat.com>
d76c62
Reviewed-by: Erik Skultety <eskultet@redhat.com>
d76c62
(cherry picked from commit fdd48f5b737c09a0581bf666d1578f5bd5d0de12)
d76c62
d76c62
Prerequisite for: https://bugzilla.redhat.com/show_bug.cgi?id=1808499
d76c62
d76c62
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
d76c62
Message-Id: <6c9d9490de405d56f3fd787dd5d02d3fb4943bb4.1584101247.git.mprivozn@redhat.com>
d76c62
Reviewed-by: Ján Tomko <jtomko@redhat.com>
d76c62
---
d76c62
 src/libvirt_private.syms |  1 +
d76c62
 src/util/virbuffer.c     | 26 ++++++++++++++++++++++++++
d76c62
 src/util/virbuffer.h     |  1 +
d76c62
 tests/virbuftest.c       | 36 ++++++++++++++++++++++++++++++++++++
d76c62
 4 files changed, 64 insertions(+)
d76c62
d76c62
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
d76c62
index 1f97879faa..dbbec0d567 100644
d76c62
--- a/src/libvirt_private.syms
d76c62
+++ b/src/libvirt_private.syms
d76c62
@@ -1642,6 +1642,7 @@ virBufferSetIndent;
d76c62
 virBufferStrcat;
d76c62
 virBufferStrcatVArgs;
d76c62
 virBufferTrim;
d76c62
+virBufferTrimChars;
d76c62
 virBufferURIEncodeString;
d76c62
 virBufferUse;
d76c62
 virBufferVasprintf;
d76c62
diff --git a/src/util/virbuffer.c b/src/util/virbuffer.c
d76c62
index 1b93110919..914c386b18 100644
d76c62
--- a/src/util/virbuffer.c
d76c62
+++ b/src/util/virbuffer.c
d76c62
@@ -673,6 +673,32 @@ virBufferTrim(virBufferPtr buf, const char *str, int len)
d76c62
     g_string_truncate(buf->str, buf->str->len - len);
d76c62
 }
d76c62
 
d76c62
+/**
d76c62
+ * virBufferTrimChars:
d76c62
+ * @buf: the buffer to trim
d76c62
+ * @trim: the characters to be trimmed
d76c62
+ *
d76c62
+ * Trim the tail of the buffer. The longest string that can be formed with
d76c62
+ * the characters from @trim is trimmed.
d76c62
+ */
d76c62
+void
d76c62
+virBufferTrimChars(virBufferPtr buf, const char *trim)
d76c62
+{
d76c62
+    ssize_t i;
d76c62
+
d76c62
+    if (!buf || !buf->str)
d76c62
+        return;
d76c62
+
d76c62
+    if (!trim)
d76c62
+        return;
d76c62
+
d76c62
+    for (i = buf->str->len - 1; i > 0; i--) {
d76c62
+        if (!strchr(trim, buf->str->str[i]))
d76c62
+            break;
d76c62
+    }
d76c62
+
d76c62
+    g_string_truncate(buf->str, i + 1);
d76c62
+}
d76c62
 
d76c62
 /**
d76c62
  * virBufferAddStr:
d76c62
diff --git a/src/util/virbuffer.h b/src/util/virbuffer.h
d76c62
index 38758a9125..183f78f279 100644
d76c62
--- a/src/util/virbuffer.h
d76c62
+++ b/src/util/virbuffer.h
d76c62
@@ -92,4 +92,5 @@ size_t virBufferGetIndent(const virBuffer *buf);
d76c62
 size_t virBufferGetEffectiveIndent(const virBuffer *buf);
d76c62
 
d76c62
 void virBufferTrim(virBufferPtr buf, const char *trim, int len);
d76c62
+void virBufferTrimChars(virBufferPtr buf, const char *trim);
d76c62
 void virBufferAddStr(virBufferPtr buf, const char *str);
d76c62
diff --git a/tests/virbuftest.c b/tests/virbuftest.c
d76c62
index 1780b62bf4..7919075000 100644
d76c62
--- a/tests/virbuftest.c
d76c62
+++ b/tests/virbuftest.c
d76c62
@@ -12,6 +12,7 @@
d76c62
 struct testBufAddStrData {
d76c62
     const char *data;
d76c62
     const char *expect;
d76c62
+    const char *arg;
d76c62
 };
d76c62
 
d76c62
 static int testBufAutoIndent(const void *data G_GNUC_UNUSED)
d76c62
@@ -130,6 +131,30 @@ static int testBufTrim(const void *data G_GNUC_UNUSED)
d76c62
     return ret;
d76c62
 }
d76c62
 
d76c62
+static int
d76c62
+testBufTrimChars(const void *opaque)
d76c62
+{
d76c62
+    const struct testBufAddStrData *data = opaque;
d76c62
+    virBuffer buf = VIR_BUFFER_INITIALIZER;
d76c62
+    g_autofree char *actual = NULL;
d76c62
+
d76c62
+    virBufferAddStr(&buf, data->data);
d76c62
+    virBufferTrimChars(&buf, data->arg);
d76c62
+
d76c62
+    if (!(actual = virBufferContentAndReset(&buf))) {
d76c62
+        VIR_TEST_DEBUG("buf is empty");
d76c62
+        return -1;
d76c62
+    }
d76c62
+
d76c62
+    if (STRNEQ_NULLABLE(actual, data->expect)) {
d76c62
+        VIR_TEST_DEBUG("testBufEscapeStr(): Strings don't match:");
d76c62
+        virTestDifference(stderr, data->expect, actual);
d76c62
+        return -1;
d76c62
+    }
d76c62
+
d76c62
+    return 0;
d76c62
+}
d76c62
+
d76c62
 static int testBufAddBuffer(const void *data G_GNUC_UNUSED)
d76c62
 {
d76c62
     virBuffer buf1 = VIR_BUFFER_INITIALIZER;
d76c62
@@ -411,6 +436,17 @@ mymain(void)
d76c62
     DO_TEST_ESCAPE_REGEX("^$.|?*+()[]{}\\",
d76c62
                          "\\^\\$\\.\\|\\?\\*\\+\\(\\)\\[\\]\\{\\}\\\\");
d76c62
 
d76c62
+#define DO_TEST_TRIM_CHARS(_data, _arg, _expect) \
d76c62
+    do { \
d76c62
+        struct testBufAddStrData info = { .data = _data, .expect = _expect, .arg = _arg }; \
d76c62
+        if (virTestRun("Buf: Trim: " #_data, testBufTrimChars, &info) < 0) \
d76c62
+            ret = -1; \
d76c62
+    } while (0)
d76c62
+
d76c62
+    DO_TEST_TRIM_CHARS("Trimmm", "m", "Tri");
d76c62
+    DO_TEST_TRIM_CHARS("-abcd-efgh--", "-", "-abcd-efgh");
d76c62
+    DO_TEST_TRIM_CHARS("-hABC-efgh--", "-h", "-hABC-efg");
d76c62
+
d76c62
     return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
d76c62
 }
d76c62
 
d76c62
-- 
d76c62
2.25.1
d76c62