gstrvbuilder: Add g_strv_builder_take

g_strv_builder_take() allows to transfer ownership of the passed in
string.

This can be useful to avoid additional allocations when using functions
that transfer ownership to the caller like g_strdup_printf().

The testcase uses g_strv_builder_take and g_strv_builder_add to demo
that calls can be mixed.
This commit is contained in:
Guido Günther
2023-10-15 15:30:52 +02:00
parent 046f70cc0d
commit bc4ba05d34
4 changed files with 43 additions and 0 deletions

View File

@@ -156,6 +156,24 @@ g_strv_builder_add_many (GStrvBuilder *builder,
va_end (var_args);
}
/**
* g_strv_builder_take:
* @builder: a #GStrvBuilder
* @value: (transfer full): a string.
* Ownership of the string is transferred to the #GStrvBuilder
*
* Add a string to the end of the array. After @value belongs to the
* #GStrvBuilder and may no longer be modified by the caller.
*
* Since 2.80
*/
void
g_strv_builder_take (GStrvBuilder *builder,
char *value)
{
g_ptr_array_add (&builder->array, value);
}
/**
* g_strv_builder_end:
* @builder: a #GStrvBuilder