Merge branch 'g-strv-builder-take' into 'main'

gstrvbuilder: Add g_strv_builder_take

See merge request GNOME/glib!3637
This commit is contained in:
Philip Withnall 2023-10-16 08:21:34 +00:00
commit 0a0f1f662c
4 changed files with 43 additions and 0 deletions

View File

@ -1604,6 +1604,7 @@ g_strv_builder_unref
g_strv_builder_add
g_strv_builder_addv
g_strv_builder_add_many
g_strv_builder_take
g_strv_builder_end
<SUBSECTION>

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

View File

@ -53,6 +53,10 @@ GLIB_AVAILABLE_IN_2_70
void g_strv_builder_add_many (GStrvBuilder *builder,
...) G_GNUC_NULL_TERMINATED;
GLIB_AVAILABLE_IN_2_80
void g_strv_builder_take (GStrvBuilder *builder,
char *value);
GLIB_AVAILABLE_IN_2_68
GStrv g_strv_builder_end (GStrvBuilder *builder);

View File

@ -92,6 +92,25 @@ test_strvbuilder_add_many (void)
g_strv_builder_unref (builder);
}
static void
test_strvbuilder_take (void)
{
GStrvBuilder *builder;
GStrv result;
const gchar *expected[] = { "one", "two", "three", NULL };
builder = g_strv_builder_new ();
g_strv_builder_take (builder, g_strdup ("one"));
g_strv_builder_add (builder, "two");
g_strv_builder_take (builder, g_strdup ("three"));
result = g_strv_builder_end (builder);
g_assert_nonnull (result);
g_assert_true (g_strv_equal ((const gchar *const *) result, expected));
g_strfreev (result);
g_strv_builder_unref (builder);
}
static void
test_strvbuilder_ref (void)
{
@ -113,6 +132,7 @@ main (int argc,
g_test_add_func ("/strvbuilder/add", test_strvbuilder_add);
g_test_add_func ("/strvbuilder/addv", test_strvbuilder_addv);
g_test_add_func ("/strvbuilder/add_many", test_strvbuilder_add_many);
g_test_add_func ("/strvbuilder/take", test_strvbuilder_take);
g_test_add_func ("/strvbuilder/ref", test_strvbuilder_ref);
return g_test_run ();