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