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:
Peter Eisenmann 2023-05-02 13:07:13 +02:00 committed by Philip Withnall
parent 03aa8513f8
commit afdab4f493
4 changed files with 72 additions and 0 deletions

View File

@ -2625,6 +2625,7 @@ g_str_hash
<FILE>strings</FILE>
GString
g_string_new
g_string_new_take
g_string_new_len
g_string_sized_new
g_string_assign

View File

@ -155,6 +155,39 @@ g_string_new (const gchar *init)
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)
* @init: initial contents of the string

View File

@ -51,6 +51,8 @@ struct _GString
GLIB_AVAILABLE_IN_ALL
GString* g_string_new (const gchar *init);
GLIB_AVAILABLE_IN_2_78
GString* g_string_new_take (gchar *init);
GLIB_AVAILABLE_IN_ALL
GString* g_string_new_len (const gchar *init,
gssize len);

View File

@ -709,6 +709,40 @@ test_string_steal (void)
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
main (int argc,
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-replace", test_string_replace);
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();
}