Merge branch 'strv_builder_add_many' into 'master'

gstrvbuilder: add addv and add_many to the API

See merge request GNOME/glib!2112
This commit is contained in:
Philip Withnall 2021-05-26 12:41:19 +00:00
commit 4575c70a6b
4 changed files with 93 additions and 1 deletions

View File

@ -1865,6 +1865,8 @@ g_strv_builder_new
g_strv_builder_ref
g_strv_builder_unref
g_strv_builder_add
g_strv_builder_addv
g_strv_builder_add_many
g_strv_builder_end
<SUBSECTION>

View File

@ -1,5 +1,6 @@
/*
* Copyright © 2020 Canonical Ltd.
* Copyright © 2021 Alexandros Theodotou
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@ -21,6 +22,7 @@
#include "garray.h"
#include "gmem.h"
#include "gmessages.h"
/**
* SECTION:gstrvbuilder
@ -113,6 +115,48 @@ g_strv_builder_add (GStrvBuilder *builder,
g_ptr_array_add (&builder->array, g_strdup (value));
}
/**
* g_strv_builder_addv:
* @builder: a #GStrvBuilder
* @value: (array zero-terminated=1): the vector of strings to add
*
* Appends all the strings in the given vector to the builder.
*
* Since 2.70
*/
void
g_strv_builder_addv (GStrvBuilder *builder,
const char **value)
{
gsize i = 0;
g_return_if_fail (builder != NULL);
g_return_if_fail (value != NULL);
for (i = 0; value[i] != NULL; i++)
g_strv_builder_add (builder, value[i]);
}
/**
* g_strv_builder_add_many:
* @builder: a #GStrvBuilder
* @...: one or more strings followed by %NULL
*
* Appends all the given strings to the builder.
*
* Since 2.70
*/
void
g_strv_builder_add_many (GStrvBuilder *builder,
...)
{
va_list var_args;
const gchar *str;
g_return_if_fail (builder != NULL);
va_start (var_args, builder);
while ((str = va_arg (var_args, gchar *)) != NULL)
g_strv_builder_add (builder, str);
va_end (var_args);
}
/**
* g_strv_builder_end:
* @builder: a #GStrvBuilder

View File

@ -1,5 +1,6 @@
/*
* Copyright © 2020 Canonical Ltd.
* Copyright © 2021 Alexandros Theodotou
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@ -29,7 +30,7 @@ G_BEGIN_DECLS
/**
* GStrvBuilder:
*
*
* A helper object to build a %NULL-terminated string array
* by appending. See g_strv_builder_new().
*
@ -50,6 +51,14 @@ GLIB_AVAILABLE_IN_2_68
void g_strv_builder_add (GStrvBuilder *builder,
const char *value);
GLIB_AVAILABLE_IN_2_70
void g_strv_builder_addv (GStrvBuilder *builder,
const char **value);
GLIB_AVAILABLE_IN_2_70
void g_strv_builder_add_many (GStrvBuilder *builder,
...) G_GNUC_NULL_TERMINATED;
GLIB_AVAILABLE_IN_2_68
GStrv g_strv_builder_end (GStrvBuilder *builder);

View File

@ -1,5 +1,6 @@
/*
* Copyright © 2020 Canonical Ltd.
* Copyright © 2021 Alexandros Theodotou
*
* This work is provided "as is"; redistribution and modification
* in whole or in part, in any medium, physical or electronic is
@ -55,6 +56,40 @@ test_strvbuilder_add (void)
g_strv_builder_unref (builder);
}
static void
test_strvbuilder_addv (void)
{
GStrvBuilder *builder;
GStrv result;
const gchar *expected[] = { "one", "two", "three", NULL };
builder = g_strv_builder_new ();
g_strv_builder_addv (builder, expected);
result = g_strv_builder_end (builder);
g_assert_nonnull (result);
g_assert_cmpstrv ((const gchar *const *) result, expected);
g_strfreev (result);
g_strv_builder_unref (builder);
}
static void
test_strvbuilder_add_many (void)
{
GStrvBuilder *builder;
GStrv result;
const gchar *expected[] = { "one", "two", "three", NULL };
builder = g_strv_builder_new ();
g_strv_builder_add_many (builder, "one", "two", "three", NULL);
result = g_strv_builder_end (builder);
g_assert_nonnull (result);
g_assert_cmpstrv ((const gchar *const *) result, expected);
g_strfreev (result);
g_strv_builder_unref (builder);
}
static void
test_strvbuilder_ref (void)
{
@ -74,6 +109,8 @@ main (int argc,
g_test_add_func ("/strvbuilder/empty", test_strvbuilder_empty);
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/ref", test_strvbuilder_ref);
return g_test_run ();