]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
util: add virBufferTrimChars
authorJán Tomko <jtomko@redhat.com>
Tue, 14 Jan 2020 07:04:14 +0000 (08:04 +0100)
committerJán Tomko <jtomko@redhat.com>
Wed, 15 Jan 2020 16:10:20 +0000 (17:10 +0100)
A new helper for trimming combinations of specified characters from
the tail of the buffer.

Signed-off-by: Ján Tomko <jtomko@redhat.com>
Reviewed-by: Erik Skultety <eskultet@redhat.com>
src/libvirt_private.syms
src/util/virbuffer.c
src/util/virbuffer.h
tests/virbuftest.c

index b97906b852389baebca4ef0e3cf49e3bd6081fe8..f46ed29eaced2c3fa5a36fadec6372b4b79f1396 100644 (file)
@@ -1642,6 +1642,7 @@ virBufferSetIndent;
 virBufferStrcat;
 virBufferStrcatVArgs;
 virBufferTrim;
+virBufferTrimChars;
 virBufferURIEncodeString;
 virBufferUse;
 virBufferVasprintf;
index 1b931109194468f8ce8adfe9374880df66411d4d..914c386b187b61a66a8833e00206f611f55d605e 100644 (file)
@@ -673,6 +673,32 @@ virBufferTrim(virBufferPtr buf, const char *str, int len)
     g_string_truncate(buf->str, buf->str->len - len);
 }
 
+/**
+ * virBufferTrimChars:
+ * @buf: the buffer to trim
+ * @trim: the characters to be trimmed
+ *
+ * Trim the tail of the buffer. The longest string that can be formed with
+ * the characters from @trim is trimmed.
+ */
+void
+virBufferTrimChars(virBufferPtr buf, const char *trim)
+{
+    ssize_t i;
+
+    if (!buf || !buf->str)
+        return;
+
+    if (!trim)
+        return;
+
+    for (i = buf->str->len - 1; i > 0; i--) {
+        if (!strchr(trim, buf->str->str[i]))
+            break;
+    }
+
+    g_string_truncate(buf->str, i + 1);
+}
 
 /**
  * virBufferAddStr:
index 38758a9125998cfa90ffb0f9aa4f5b5e51d4c241..183f78f279359c4a29f1378997ba1a2a01d59cd4 100644 (file)
@@ -92,4 +92,5 @@ size_t virBufferGetIndent(const virBuffer *buf);
 size_t virBufferGetEffectiveIndent(const virBuffer *buf);
 
 void virBufferTrim(virBufferPtr buf, const char *trim, int len);
+void virBufferTrimChars(virBufferPtr buf, const char *trim);
 void virBufferAddStr(virBufferPtr buf, const char *str);
index 1780b62bf4ac1eee201378870f682cbdd1232b90..79190750007e25ae21c07721401c2657934c59c7 100644 (file)
@@ -12,6 +12,7 @@
 struct testBufAddStrData {
     const char *data;
     const char *expect;
+    const char *arg;
 };
 
 static int testBufAutoIndent(const void *data G_GNUC_UNUSED)
@@ -130,6 +131,30 @@ static int testBufTrim(const void *data G_GNUC_UNUSED)
     return ret;
 }
 
+static int
+testBufTrimChars(const void *opaque)
+{
+    const struct testBufAddStrData *data = opaque;
+    virBuffer buf = VIR_BUFFER_INITIALIZER;
+    g_autofree char *actual = NULL;
+
+    virBufferAddStr(&buf, data->data);
+    virBufferTrimChars(&buf, data->arg);
+
+    if (!(actual = virBufferContentAndReset(&buf))) {
+        VIR_TEST_DEBUG("buf is empty");
+        return -1;
+    }
+
+    if (STRNEQ_NULLABLE(actual, data->expect)) {
+        VIR_TEST_DEBUG("testBufEscapeStr(): Strings don't match:");
+        virTestDifference(stderr, data->expect, actual);
+        return -1;
+    }
+
+    return 0;
+}
+
 static int testBufAddBuffer(const void *data G_GNUC_UNUSED)
 {
     virBuffer buf1 = VIR_BUFFER_INITIALIZER;
@@ -411,6 +436,17 @@ mymain(void)
     DO_TEST_ESCAPE_REGEX("^$.|?*+()[]{}\\",
                          "\\^\\$\\.\\|\\?\\*\\+\\(\\)\\[\\]\\{\\}\\\\");
 
+#define DO_TEST_TRIM_CHARS(_data, _arg, _expect) \
+    do { \
+        struct testBufAddStrData info = { .data = _data, .expect = _expect, .arg = _arg }; \
+        if (virTestRun("Buf: Trim: " #_data, testBufTrimChars, &info) < 0) \
+            ret = -1; \
+    } while (0)
+
+    DO_TEST_TRIM_CHARS("Trimmm", "m", "Tri");
+    DO_TEST_TRIM_CHARS("-abcd-efgh--", "-", "-abcd-efgh");
+    DO_TEST_TRIM_CHARS("-hABC-efgh--", "-h", "-hABC-efg");
+
     return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
 }