util: introduce virBufferTrimLen

Just like the existing virBufferTrim, but only
does one thing at a time.

Signed-off-by: Ján Tomko <jtomko@redhat.com>
Reviewed-by: Peter Krempa <pkrempa@redhat.com>
This commit is contained in:
Ján Tomko 2020-02-02 20:16:36 +01:00
parent e092daacee
commit 080405540a
3 changed files with 21 additions and 0 deletions

View File

@ -1646,6 +1646,7 @@ virBufferStrcat;
virBufferStrcatVArgs;
virBufferTrim;
virBufferTrimChars;
virBufferTrimLen;
virBufferURIEncodeString;
virBufferUse;
virBufferVasprintf;

View File

@ -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

View File

@ -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);