diff options
author | Matthias Clasen <mclasen@redhat.com> | 2023-01-14 09:43:10 -0500 |
---|---|---|
committer | Matthias Clasen <mclasen@redhat.com> | 2023-01-14 09:44:22 -0500 |
commit | c191c997483c542df2676d258ce2c7b714113804 (patch) | |
tree | 491c66fb1efc792b51493ca4e485d281b3d0bd4b | |
parent | 84780536a4d611297e3fae6b5a8702c4022677e0 (diff) | |
download | glib-c191c997483c542df2676d258ce2c7b714113804.tar.gz |
string: Add an inline g_string_truncate too
This is another very commonly used
GString function.
-rw-r--r-- | glib/gstring.c | 4 | ||||
-rw-r--r-- | 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 */ |