mirror of
https://github.com/libvirt/libvirt.git
synced 2025-02-25 18:55:26 -06:00
util: add virBufferTrimChars
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>
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user