From fc02bf666bf6f5ac12873bf684db69f38c7e3d30 Mon Sep 17 00:00:00 2001 From: Andrew Borodin Date: Sun, 21 Apr 2024 11:37:39 +0300 Subject: [PATCH] (mc_g_string_append_c_len): new API that extends GString one. Signed-off-by: Andrew Borodin --- lib/glibcompat.c | 29 +++++++++++++++++++++++++++++ lib/glibcompat.h | 3 +++ lib/strutil/strutil.c | 11 ++--------- 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/lib/glibcompat.c b/lib/glibcompat.c index 27d81a247..8a5e2e91a 100644 --- a/lib/glibcompat.c +++ b/lib/glibcompat.c @@ -297,3 +297,32 @@ mc_g_string_dup (const GString * s) } /* --------------------------------------------------------------------------------------------- */ + +/** + * mc_g_string_append_c_len: + * @s: (not nullable): the destination #GString. + * @c: the byte to append onto the end of @s + * @len: the number of bytes @c to append onto the end of @s + * @return: @s + * + * Adds @len bytes @c onto the end of @s. + * + * There is no such API in GLib2. + */ +GString * +mc_g_string_append_c_len (GString * s, gchar c, guint len) +{ + g_return_val_if_fail (s != NULL, NULL); + + if (len != 0) + { + guint s_len = s->len; + + g_string_set_size (s, s->len + len); + memset (s->str + s_len, (unsigned char) c, len); + } + + return s; +} + +/* --------------------------------------------------------------------------------------------- */ diff --git a/lib/glibcompat.h b/lib/glibcompat.h index e2f84f5d9..9ab3b47f5 100644 --- a/lib/glibcompat.h +++ b/lib/glibcompat.h @@ -40,6 +40,9 @@ GString *mc_g_string_copy (GString * dest, const GString * src); /* There is no such API in GLib2 */ GString *mc_g_string_dup (const GString * s); +/* There is no such API in GLib2 */ +GString *mc_g_string_append_c_len (GString * s, gchar c, guint len); + /*** inline functions ****************************************************************************/ #endif /* MC_GLIBCOMPAT_H */ diff --git a/lib/strutil/strutil.c b/lib/strutil/strutil.c index 4e24e7f0a..7f0290b0a 100644 --- a/lib/strutil/strutil.c +++ b/lib/strutil/strutil.c @@ -153,9 +153,7 @@ _str_convert (GIConv coder, const char *string, int size, GString * buffer) case G_CONVERT_ERROR_NO_CONVERSION: /* Conversion between the requested character sets is not supported. */ g_free (tmp_buff); - tmp_buff = g_strnfill (strlen (string), '?'); - g_string_append (buffer, tmp_buff); - g_free (tmp_buff); + mc_g_string_append_c_len (buffer, '?', strlen (string)); return ESTR_FAILURE; case G_CONVERT_ERROR_ILLEGAL_SEQUENCE: @@ -186,12 +184,7 @@ _str_convert (GIConv coder, const char *string, int size, GString * buffer) g_string_append (buffer, tmp_buff); g_free (tmp_buff); if ((int) bytes_read < left) - { - left = left - bytes_read; - tmp_buff = g_strnfill (left, '?'); - g_string_append (buffer, tmp_buff); - g_free (tmp_buff); - } + mc_g_string_append_c_len (buffer, '?', left - bytes_read); return ESTR_PROBLEM; case G_CONVERT_ERROR_BAD_URI: /* Don't know how handle this error :( */