From c191c997483c542df2676d258ce2c7b714113804 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Sat, 14 Jan 2023 09:43:10 -0500 Subject: [PATCH] string: Add an inline g_string_truncate too This is another very commonly used GString function. --- glib/gstring.c | 4 ++-- glib/gstring.h | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/glib/gstring.c b/glib/gstring.c index ed6f03123..8967ce12b 100644 --- a/glib/gstring.c +++ b/glib/gstring.c @@ -358,8 +358,8 @@ g_string_assign (GString *string, * Returns: (transfer none): @string */ GString * -g_string_truncate (GString *string, - gsize len) +(g_string_truncate) (GString *string, + gsize len) { g_return_val_if_fail (string != NULL, NULL); diff --git a/glib/gstring.h b/glib/gstring.h index 70888217c..00dea325b 100644 --- a/glib/gstring.h +++ b/glib/gstring.h @@ -204,9 +204,21 @@ g_string_append_len_inline (GString *gstring, } #define g_string_append_len(gstr,val,len) g_string_append_len_inline (gstr, val, len) +static inline GString * +g_string_truncate_inline (GString *gstring, + gsize len) +{ + gstring->len = MIN (len, gstring->len); + gstring->str[gstring->len] = '\0'; + return gstring; +} + +#define g_string_truncate(gstr,len) g_string_truncate_inline (gstr, len) + #if G_GNUC_CHECK_VERSION (2, 0) #define g_string_append(gstr,val) g_string_append_len (gstr, val, __builtin_constant_p (val) ? (gssize) strlen (val) : (gssize) -1) + #endif #endif /* G_CAN_INLINE */