mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-02-03 01:36:17 +01:00
Introduce g_string_overwrite(_len)? for overwriting parts of strings (#368686, Samuel Cormier-Iijima)
svn path=/trunk/; revision=5563
This commit is contained in:
parent
19086497ab
commit
ed4d216d2b
@ -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>
|
||||
|
||||
* gobject/gtype.c (g_type_class_add_private): Check for 0-sized
|
||||
|
@ -1143,6 +1143,8 @@ g_string_insert_len
|
||||
g_string_insert_unichar
|
||||
g_string_new
|
||||
g_string_new_len
|
||||
g_string_overwrite
|
||||
g_string_overwrite_len
|
||||
g_string_prepend
|
||||
g_string_prepend_c
|
||||
g_string_prepend_len
|
||||
|
@ -1022,6 +1022,76 @@ g_string_insert_unichar (GString *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:
|
||||
* @string: a #GString
|
||||
|
@ -105,6 +105,13 @@ GString* g_string_insert_c (GString *string,
|
||||
GString* g_string_insert_unichar (GString *string,
|
||||
gssize pos,
|
||||
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,
|
||||
gssize pos,
|
||||
gssize len);
|
||||
|
@ -257,7 +257,27 @@ main (int argc,
|
||||
g_assert (g_string_equal(string1, string2));
|
||||
g_string_free (string1, 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. */
|
||||
string1 = g_string_new ("fiddle");
|
||||
string2 = g_string_new ("fiddle");
|
||||
|
Loading…
Reference in New Issue
Block a user