render / rpms / libvirt

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