diff options
author | Philip Withnall <philip@tecnocode.co.uk> | 2023-05-16 12:04:37 +0000 |
---|---|---|
committer | Philip Withnall <philip@tecnocode.co.uk> | 2023-05-16 12:04:37 +0000 |
commit | ec8856d7d69be85bc1ad3c64a801460c8c692231 (patch) | |
tree | b220a46f844450a687d9f772967a779bbb01f9ff /glib/gstring.c | |
parent | 90c3438373645033782eb9a326d4417f6c4f0888 (diff) | |
parent | ac4d1e2686fa6580f070b4aba9cd6423b691c7e3 (diff) | |
download | glib-ec8856d7d69be85bc1ad3c64a801460c8c692231.tar.gz |
Merge branch 'wip/p3732/g-string-new-steal' into 'main'
gstring: add g_string_new_take
See merge request GNOME/glib!3423
Diffstat (limited to 'glib/gstring.c')
-rw-r--r-- | glib/gstring.c | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/glib/gstring.c b/glib/gstring.c index ad6b92231..77448f2f1 100644 --- a/glib/gstring.c +++ b/glib/gstring.c @@ -156,6 +156,39 @@ g_string_new (const gchar *init) } /** + * 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 * @len: length of @init to use |