mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-03-15 20:25:12 +01:00
Merge branch 'ebassi/strvbuilder-util' into 'main'
Add unref-to-strv to GStrvBuilder See merge request GNOME/glib!3952
This commit is contained in:
commit
63548f63ec
@ -37,7 +37,10 @@
|
||||
* g_autoptr(GStrvBuilder) builder = g_strv_builder_new ();
|
||||
* g_strv_builder_add (builder, "hello");
|
||||
* g_strv_builder_add (builder, "world");
|
||||
*
|
||||
* g_auto(GStrv) array = g_strv_builder_end (builder);
|
||||
*
|
||||
* g_assert_true (g_strv_equal (array, (const char *[]) { "hello", "world", NULL }));
|
||||
* ```
|
||||
*
|
||||
* Since: 2.68
|
||||
@ -81,6 +84,43 @@ g_strv_builder_unref (GStrvBuilder *builder)
|
||||
g_ptr_array_unref (&builder->array);
|
||||
}
|
||||
|
||||
/**
|
||||
* g_strv_builder_unref_to_strv:
|
||||
* @builder: (transfer full): a #GStrvBuilder
|
||||
*
|
||||
* Decreases the reference count on the string vector builder, and returns
|
||||
* its contents as a `NULL`-terminated string array.
|
||||
*
|
||||
* This function is especially useful for cases where it's not possible
|
||||
* to use `g_autoptr()`.
|
||||
*
|
||||
* ```c
|
||||
* GStrvBuilder *builder = g_strv_builder_new ();
|
||||
* g_strv_builder_add (builder, "hello");
|
||||
* g_strv_builder_add (builder, "world");
|
||||
*
|
||||
* GStrv array = g_strv_builder_unref_to_strv (builder);
|
||||
*
|
||||
* g_assert_true (g_strv_equal (array, (const char *[]) { "hello", "world", NULL }));
|
||||
*
|
||||
* g_strfreev (array);
|
||||
* ```
|
||||
*
|
||||
* Returns: (transfer full) (array zero-terminated=1): the constructed string
|
||||
* array
|
||||
*
|
||||
* Since: 2.82
|
||||
*/
|
||||
GStrv
|
||||
g_strv_builder_unref_to_strv (GStrvBuilder *builder)
|
||||
{
|
||||
GStrv res = g_strv_builder_end (builder);
|
||||
|
||||
g_strv_builder_unref (builder);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* g_strv_builder_ref:
|
||||
* @builder: (transfer none): a #GStrvBuilder
|
||||
|
@ -38,6 +38,9 @@ GStrvBuilder *g_strv_builder_new (void);
|
||||
GLIB_AVAILABLE_IN_2_68
|
||||
void g_strv_builder_unref (GStrvBuilder *builder);
|
||||
|
||||
GLIB_AVAILABLE_IN_2_82
|
||||
GStrv g_strv_builder_unref_to_strv (GStrvBuilder *builder);
|
||||
|
||||
GLIB_AVAILABLE_IN_2_68
|
||||
GStrvBuilder *g_strv_builder_ref (GStrvBuilder *builder);
|
||||
|
||||
|
@ -122,6 +122,30 @@ test_strvbuilder_ref (void)
|
||||
g_strv_builder_unref (builder);
|
||||
}
|
||||
|
||||
static void
|
||||
test_strvbuilder_unref_to_strv (void)
|
||||
{
|
||||
GStrvBuilder *builder = g_strv_builder_new ();
|
||||
GStrv result;
|
||||
|
||||
g_strv_builder_add_many (builder, "hello", "world", NULL);
|
||||
result = g_strv_builder_unref_to_strv (builder);
|
||||
|
||||
g_assert_true (g_strv_equal ((const char * const *) result,
|
||||
(const char *[]) {
|
||||
"hello",
|
||||
"world",
|
||||
NULL,
|
||||
}));
|
||||
|
||||
g_strfreev (result);
|
||||
|
||||
builder = g_strv_builder_new ();
|
||||
result = g_strv_builder_unref_to_strv (builder);
|
||||
g_assert_null (result[0]);
|
||||
g_strfreev (result);
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc,
|
||||
char *argv[])
|
||||
@ -134,6 +158,7 @@ main (int argc,
|
||||
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);
|
||||
g_test_add_func ("/strvbuilder/unref_to_strv", test_strvbuilder_unref_to_strv);
|
||||
|
||||
return g_test_run ();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user