Introduce g_string_overwrite(_len)? for overwriting parts of strings (#368686, Samuel Cormier-Iijima)

svn path=/trunk/; revision=5563
This commit is contained in:
Mathias Hasselmann 2007-06-15 11:54:21 +00:00
parent 19086497ab
commit ed4d216d2b
5 changed files with 106 additions and 1 deletions

View File

@ -1,3 +1,9 @@
2007-06-15 Mathias Hasselmann <mathias.hasselmann@gmx.de>
* build, tests/string-test.c, glib/glib.symbols,
glib/gstring.c, glib/gstring.h: Introduce g_string_overwrite(_len)?
for overwriting parts of strings (#368686, Samuel Cormier-Iijima)
2007-06-14 Cody Russell <bratsche@gnome.org> 2007-06-14 Cody Russell <bratsche@gnome.org>
* gobject/gtype.c (g_type_class_add_private): Check for 0-sized * gobject/gtype.c (g_type_class_add_private): Check for 0-sized

View File

@ -1143,6 +1143,8 @@ g_string_insert_len
g_string_insert_unichar g_string_insert_unichar
g_string_new g_string_new
g_string_new_len g_string_new_len
g_string_overwrite
g_string_overwrite_len
g_string_prepend g_string_prepend
g_string_prepend_c g_string_prepend_c
g_string_prepend_len g_string_prepend_len

View File

@ -1022,6 +1022,76 @@ g_string_insert_unichar (GString *string,
return string; return string;
} }
/**
* g_string_overwrite:
* @string: a #GString
* @pos: the position at which to start overwriting
* @val: the string that will overwrite the @string starting at @pos
*
* Overwrites part of a string, lengthening it if necessary.
*
* Return value: @string
*
* Since: 2.14
**/
GString *
g_string_overwrite (GString *string,
gsize pos,
const gchar *val)
{
g_return_val_if_fail (val != NULL, string);
return g_string_overwrite_len (string, pos, val, strlen (val));
}
/**
* g_string_overwrite_len:
* @string: a #GString
* @pos: the position at which to start overwriting
* @val: the string that will overwrite the @string starting at @pos
* @len: the number of bytes to write from @val
*
* Overwrites part of a string, lengthening it if necessary. This function
* will work with embedded NULLs.
*
* Return value: @string
*
* Since: 2.14
**/
GString *
g_string_overwrite_len (GString *string,
gsize pos,
const gchar *val,
gssize len)
{
gsize end;
g_return_val_if_fail (string != NULL, NULL);
if (!len)
return string;
g_return_val_if_fail (val != NULL, string);
g_return_val_if_fail (pos <= string->len, string);
if (len < 0)
len = strlen (val);
end = pos + len;
if (end > string->len)
g_string_maybe_expand (string, end - string->len);
memcpy (string->str + pos, val, len);
if (end > string->len)
{
string->str[end] = '\0';
string->len = end;
}
return string;
}
/** /**
* g_string_erase: * g_string_erase:
* @string: a #GString * @string: a #GString

View File

@ -105,6 +105,13 @@ GString* g_string_insert_c (GString *string,
GString* g_string_insert_unichar (GString *string, GString* g_string_insert_unichar (GString *string,
gssize pos, gssize pos,
gunichar wc); gunichar wc);
GString* g_string_overwrite (GString *string,
gsize pos,
const gchar *val);
GString* g_string_overwrite_len (GString *string,
gsize pos,
const gchar *val,
gssize len);
GString* g_string_erase (GString *string, GString* g_string_erase (GString *string,
gssize pos, gssize pos,
gssize len); gssize len);

View File

@ -258,6 +258,26 @@ main (int argc,
g_string_free (string1, TRUE); g_string_free (string1, TRUE);
g_string_free (string2, TRUE); g_string_free (string2, TRUE);
/* overwriting functions */
string1 = g_string_new ("testing");
g_string_overwrite (string1, 4, " and expand");
g_assert (15 == string1->len);
g_assert ('\0' == string1->str[15]);
g_assert (g_str_equal ("test and expand", string1->str));
g_string_overwrite (string1, 5, "NOT-");
g_assert (15 == string1->len);
g_assert ('\0' == string1->str[15]);
g_assert (g_str_equal ("test NOT-expand", string1->str));
g_string_overwrite_len (string1, 9, "blablabla", 6);
g_assert (15 == string1->len);
g_assert ('\0' == string1->str[15]);
g_assert (g_str_equal ("test NOT-blabla", string1->str));
g_string_free (string1, TRUE);
/* Check handling of embedded ASCII 0 (NUL) characters in GString. */ /* Check handling of embedded ASCII 0 (NUL) characters in GString. */
string1 = g_string_new ("fiddle"); string1 = g_string_new ("fiddle");
string2 = g_string_new ("fiddle"); string2 = g_string_new ("fiddle");