(mc_g_string_append_c_len): new API that extends GString one.

Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
This commit is contained in:
Andrew Borodin 2024-04-21 11:37:39 +03:00
parent 822ef80f5b
commit fc02bf666b
3 changed files with 34 additions and 9 deletions

View File

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

View File

@ -40,6 +40,9 @@ GString *mc_g_string_copy (GString * dest, const GString * src);
/* There is no such API in GLib2 */ /* There is no such API in GLib2 */
GString *mc_g_string_dup (const GString * s); 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 ****************************************************************************/ /*** inline functions ****************************************************************************/
#endif /* MC_GLIBCOMPAT_H */ #endif /* MC_GLIBCOMPAT_H */

View File

@ -153,9 +153,7 @@ _str_convert (GIConv coder, const char *string, int size, GString * buffer)
case G_CONVERT_ERROR_NO_CONVERSION: case G_CONVERT_ERROR_NO_CONVERSION:
/* Conversion between the requested character sets is not supported. */ /* Conversion between the requested character sets is not supported. */
g_free (tmp_buff); g_free (tmp_buff);
tmp_buff = g_strnfill (strlen (string), '?'); mc_g_string_append_c_len (buffer, '?', strlen (string));
g_string_append (buffer, tmp_buff);
g_free (tmp_buff);
return ESTR_FAILURE; return ESTR_FAILURE;
case G_CONVERT_ERROR_ILLEGAL_SEQUENCE: 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_string_append (buffer, tmp_buff);
g_free (tmp_buff); g_free (tmp_buff);
if ((int) bytes_read < left) if ((int) bytes_read < left)
{ mc_g_string_append_c_len (buffer, '?', left - bytes_read);
left = left - bytes_read;
tmp_buff = g_strnfill (left, '?');
g_string_append (buffer, tmp_buff);
g_free (tmp_buff);
}
return ESTR_PROBLEM; return ESTR_PROBLEM;
case G_CONVERT_ERROR_BAD_URI: /* Don't know how handle this error :( */ case G_CONVERT_ERROR_BAD_URI: /* Don't know how handle this error :( */