mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-08-10 03:04:05 +02:00
gstring: add g_string_new_take
Adds a GString constructor that takes over ownership of an existing, dynamically allocated, string.
This commit is contained in:
committed by
Philip Withnall
parent
03aa8513f8
commit
afdab4f493
@@ -2625,6 +2625,7 @@ g_str_hash
|
|||||||
<FILE>strings</FILE>
|
<FILE>strings</FILE>
|
||||||
GString
|
GString
|
||||||
g_string_new
|
g_string_new
|
||||||
|
g_string_new_take
|
||||||
g_string_new_len
|
g_string_new_len
|
||||||
g_string_sized_new
|
g_string_sized_new
|
||||||
g_string_assign
|
g_string_assign
|
||||||
|
@@ -155,6 +155,39 @@ g_string_new (const gchar *init)
|
|||||||
return string;
|
return string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* g_string_new_take: (constructor)
|
||||||
|
* @init: (nullable) (transfer full): initial text used as the string.
|
||||||
|
* Ownership of the string is transferred to the #GString.
|
||||||
|
* Passing %NULL creates an empty string.
|
||||||
|
*
|
||||||
|
* Creates a new #GString, initialized with the given string.
|
||||||
|
*
|
||||||
|
* After this call, @init belongs to the #GString and may no longer be
|
||||||
|
* modified by the caller. The memory of @data has to be dynamically
|
||||||
|
* allocated and will eventually be freed with g_free().
|
||||||
|
*
|
||||||
|
* Returns: (transfer full): the new #GString
|
||||||
|
*/
|
||||||
|
GString *
|
||||||
|
g_string_new_take (gchar *init)
|
||||||
|
{
|
||||||
|
GString *string;
|
||||||
|
|
||||||
|
if (init == NULL)
|
||||||
|
{
|
||||||
|
return g_string_new (NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
string = g_slice_new (GString);
|
||||||
|
|
||||||
|
string->str = init;
|
||||||
|
string->len = strlen (string->str);
|
||||||
|
string->allocated_len = string->len + 1;
|
||||||
|
|
||||||
|
return string;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* g_string_new_len: (constructor)
|
* g_string_new_len: (constructor)
|
||||||
* @init: initial contents of the string
|
* @init: initial contents of the string
|
||||||
|
@@ -51,6 +51,8 @@ struct _GString
|
|||||||
|
|
||||||
GLIB_AVAILABLE_IN_ALL
|
GLIB_AVAILABLE_IN_ALL
|
||||||
GString* g_string_new (const gchar *init);
|
GString* g_string_new (const gchar *init);
|
||||||
|
GLIB_AVAILABLE_IN_2_78
|
||||||
|
GString* g_string_new_take (gchar *init);
|
||||||
GLIB_AVAILABLE_IN_ALL
|
GLIB_AVAILABLE_IN_ALL
|
||||||
GString* g_string_new_len (const gchar *init,
|
GString* g_string_new_len (const gchar *init,
|
||||||
gssize len);
|
gssize len);
|
||||||
|
@@ -709,6 +709,40 @@ test_string_steal (void)
|
|||||||
g_free (str);
|
g_free (str);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
test_string_new_take (void)
|
||||||
|
{
|
||||||
|
const char *test_str_const = "test_test";
|
||||||
|
const char *replaced_str_const = "test__test";
|
||||||
|
char *test_str = malloc (10 * sizeof (*test_str_const));
|
||||||
|
GString *string;
|
||||||
|
|
||||||
|
strcpy (test_str, test_str_const);
|
||||||
|
g_assert_cmpstr (test_str, ==, test_str_const);
|
||||||
|
|
||||||
|
string = g_string_new_take (g_steal_pointer (&test_str));
|
||||||
|
g_assert_null (test_str);
|
||||||
|
g_assert_nonnull (string);
|
||||||
|
|
||||||
|
g_string_replace (string, "_", "__", 0);
|
||||||
|
g_assert_cmpstr (string->str, ==, replaced_str_const);
|
||||||
|
|
||||||
|
test_str = g_string_free_and_steal (g_steal_pointer (&string));
|
||||||
|
g_assert_cmpstr (test_str, ==, replaced_str_const);
|
||||||
|
|
||||||
|
g_free (test_str);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
test_string_new_take_null (void)
|
||||||
|
{
|
||||||
|
GString *string = g_string_new_take (NULL);
|
||||||
|
|
||||||
|
g_assert_cmpstr (string->str, ==, "");
|
||||||
|
|
||||||
|
g_string_free (g_steal_pointer (&string), TRUE);
|
||||||
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
main (int argc,
|
main (int argc,
|
||||||
char *argv[])
|
char *argv[])
|
||||||
@@ -736,6 +770,8 @@ main (int argc,
|
|||||||
g_test_add_func ("/string/test-string-to-bytes", test_string_to_bytes);
|
g_test_add_func ("/string/test-string-to-bytes", test_string_to_bytes);
|
||||||
g_test_add_func ("/string/test-string-replace", test_string_replace);
|
g_test_add_func ("/string/test-string-replace", test_string_replace);
|
||||||
g_test_add_func ("/string/test-string-steal", test_string_steal);
|
g_test_add_func ("/string/test-string-steal", test_string_steal);
|
||||||
|
g_test_add_func ("/string/test-string-new-take", test_string_new_take);
|
||||||
|
g_test_add_func ("/string/test-string-new-take/null", test_string_new_take_null);
|
||||||
|
|
||||||
return g_test_run();
|
return g_test_run();
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user