Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[glibutil] Added gutil_strv_addv()
  • Loading branch information
monich committed Jul 28, 2020
1 parent 549849d commit ef3534e
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 18 deletions.
21 changes: 17 additions & 4 deletions include/gutil_strv.h
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2014-2019 Jolla Ltd.
* Copyright (C) 2014-2019 Slava Monich <slava.monich@jolla.com>
* Copyright (C) 2014-2020 Jolla Ltd.
* Copyright (C) 2014-2020 Slava Monich <slava.monich@jolla.com>
*
* You may use this file under the terms of BSD license as follows:
*
Expand Down Expand Up @@ -82,7 +82,19 @@ gutil_strv_contains(
GStrV*
gutil_strv_add(
GStrV* sv,
const char* s);
const char* s)
G_GNUC_WARN_UNUSED_RESULT;

/**
* Appends new string(s) to the array.
*/
GStrV*
gutil_strv_addv(
GStrV* sv,
const char* s,
...) /* Since 1.0.47 */
G_GNUC_WARN_UNUSED_RESULT
G_GNUC_NULL_TERMINATED;

/**
* Removes the string from the specified position in the array.
Expand All @@ -91,7 +103,8 @@ GStrV*
gutil_strv_remove_at(
GStrV* sv,
int pos,
gboolean free_string);
gboolean free_string)
G_GNUC_WARN_UNUSED_RESULT;

/**
* Checks two string arrays for equality.
Expand Down
46 changes: 37 additions & 9 deletions src/gutil_strv.c
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2014-2019 Jolla Ltd.
* Copyright (C) 2014-2019 Slava Monich <slava.monich@jolla.com>
* Copyright (C) 2014-2020 Jolla Ltd.
* Copyright (C) 2014-2020 Slava Monich <slava.monich@jolla.com>
*
* You may use this file under the terms of BSD license as follows:
*
Expand Down Expand Up @@ -125,14 +125,42 @@ gutil_strv_add(
const char* s)
{
if (s) {
const guint len = gutil_strv_length(sv);
GStrV* newsv = g_realloc(sv, sizeof(char*)*(len+2));
newsv[len] = g_strdup(s);
newsv[len+1] = NULL;
return newsv;
} else {
return sv;
guint len = gutil_strv_length(sv);

sv = g_renew(char*, sv, len + 2);
sv[len++] = g_strdup(s);
sv[len] = NULL;
}
return sv;
}

/**
* Appends new strings to the array.
*/
GStrV*
gutil_strv_addv(
GStrV* sv,
const char* s,
...)
{
if (s) {
va_list va;
const char* s1;
guint len, i, n;

va_start(va, s);
for (n = 1; (s1 = va_arg(va, char*)) != NULL; n++);
va_end(va);

len = gutil_strv_length(sv);
sv = g_renew(gchar*, sv, len + n + 1);
sv[len++] = g_strdup(s);
va_start(va, s);
for (i = 1; i < n; i++) sv[len++] = g_strdup(va_arg(va, char*));
va_end(va);
sv[len] = NULL;
}
return sv;
}

/**
Expand Down
39 changes: 34 additions & 5 deletions test/test_strv/test_strv.c
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2015-2019 Jolla Ltd.
* Copyright (C) 2015-2019 Slava Monich <slava.monich@jolla.com>
* Copyright (C) 2015-2020 Jolla Ltd.
* Copyright (C) 2015-2020 Slava Monich <slava.monich@jolla.com>
*
* You may use this file under the terms of BSD license as follows:
*
Expand Down Expand Up @@ -83,6 +83,35 @@ test_last(
g_strfreev(ab);
}

/*==========================================================================*
* Add
*==========================================================================*/

static
void
test_addv(
void)
{
char** sv1;
char** sv2;

g_assert(!gutil_strv_add(NULL, NULL));
g_assert(!gutil_strv_addv(NULL, NULL, NULL));

sv1 = gutil_strv_add(NULL, "a");
g_assert(gutil_strv_addv(sv1, NULL, NULL) == sv1);
sv2 = gutil_strv_addv(NULL, "a", NULL);
g_assert(gutil_strv_equal(sv1, sv2));

g_strfreev(sv1);
sv1 = g_strsplit("a,bb,ccc", ",", 0);
sv2 = gutil_strv_addv(sv2, "bb", "ccc", NULL);
g_assert(gutil_strv_equal(sv1, sv2));

g_strfreev(sv1);
g_strfreev(sv2);
}

/*==========================================================================*
* Equal
*==========================================================================*/
Expand All @@ -92,15 +121,14 @@ void
test_equal(
void)
{
/* gutil_strv_add(NULL, NULL) is a nop */
char** sv1 = gutil_strv_add(gutil_strv_add(gutil_strv_add(gutil_strv_add(
gutil_strv_add(gutil_strv_add(NULL, NULL), "a"), "b"), "c"), " "), "");
char** sv1 = gutil_strv_addv(NULL, "a", "b", "c", " ", "", NULL);
char** sv2 = g_strsplit("a,b,c, ,", ",", 0);
char** sv3 = g_strsplit("a,a,a, ,", ",", 0);
char** sv4 = g_strsplit("a,b,c,,", ",", 0);
char** sv5 = g_strsplit("a,b,c,", ",", 0);
char* empty = NULL;

g_assert(!gutil_strv_add(NULL, NULL));
g_assert(gutil_strv_equal(NULL, NULL));
g_assert(gutil_strv_equal(NULL, &empty));
g_assert(gutil_strv_equal(&empty, NULL));
Expand Down Expand Up @@ -247,6 +275,7 @@ int main(int argc, char* argv[])
{
g_test_init(&argc, &argv, NULL);
g_test_add_func(TEST_PREFIX "basic", test_basic);
g_test_add_func(TEST_PREFIX "addv", test_addv);
g_test_add_func(TEST_PREFIX "last", test_last);
g_test_add_func(TEST_PREFIX "equal", test_equal);
g_test_add_func(TEST_PREFIX "find", test_find);
Expand Down

0 comments on commit ef3534e

Please sign in to comment.