string: Add an inline g_string_truncate too

This is another very commonly used
GString function.
This commit is contained in:
Matthias Clasen 2023-01-14 09:43:10 -05:00
parent 84780536a4
commit c191c99748
2 changed files with 14 additions and 2 deletions

View File

@ -358,7 +358,7 @@ g_string_assign (GString *string,
* Returns: (transfer none): @string
*/
GString *
g_string_truncate (GString *string,
(g_string_truncate) (GString *string,
gsize len)
{
g_return_val_if_fail (string != NULL, NULL);

View File

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