From 080405540aff52389923ecf2827989b991e94942 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1n=20Tomko?= Date: Sun, 2 Feb 2020 20:16:36 +0100 Subject: [PATCH] util: introduce virBufferTrimLen MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Just like the existing virBufferTrim, but only does one thing at a time. Signed-off-by: Ján Tomko Reviewed-by: Peter Krempa --- src/libvirt_private.syms | 1 + src/util/virbuffer.c | 19 +++++++++++++++++++ src/util/virbuffer.h | 1 + 3 files changed, 21 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index ebf830791e..7dfb2458eb 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1646,6 +1646,7 @@ virBufferStrcat; virBufferStrcatVArgs; virBufferTrim; virBufferTrimChars; +virBufferTrimLen; virBufferURIEncodeString; virBufferUse; virBufferVasprintf; diff --git a/src/util/virbuffer.c b/src/util/virbuffer.c index 914c386b18..b76d99b56f 100644 --- a/src/util/virbuffer.c +++ b/src/util/virbuffer.c @@ -700,6 +700,25 @@ virBufferTrimChars(virBufferPtr buf, const char *trim) g_string_truncate(buf->str, i + 1); } +/** + * virBufferTrimLen: + * @buf: the buffer to trim + * @len: the number of bytes to trim + * + * Trim the tail of a buffer. + */ +void +virBufferTrimLen(virBufferPtr buf, int len) +{ + if (!buf || !buf->str) + return; + + if (len > buf->str->len) + return; + + g_string_truncate(buf->str, buf->str->len - len); +} + /** * virBufferAddStr: * @buf: the buffer to append to diff --git a/src/util/virbuffer.h b/src/util/virbuffer.h index 183f78f279..7b068075b2 100644 --- a/src/util/virbuffer.h +++ b/src/util/virbuffer.h @@ -93,4 +93,5 @@ size_t virBufferGetEffectiveIndent(const virBuffer *buf); void virBufferTrim(virBufferPtr buf, const char *trim, int len); void virBufferTrimChars(virBufferPtr buf, const char *trim); +void virBufferTrimLen(virBufferPtr buf, int len); void virBufferAddStr(virBufferPtr buf, const char *str); -- GitLab