Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[glibutil] Added gutil_ptrv_length()
  • Loading branch information
monich committed Oct 28, 2020
1 parent 027811e commit c188559
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 13 deletions.
8 changes: 6 additions & 2 deletions include/gutil_misc.h
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2016-2019 Jolla Ltd.
* Copyright (C) 2016-2019 Slava Monich <slava.monich@jolla.com>
* Copyright (C) 2016-2020 Jolla Ltd.
* Copyright (C) 2016-2020 Slava Monich <slava.monich@jolla.com>
*
* You may use this file under the terms of BSD license as follows:
*
Expand Down Expand Up @@ -117,6 +117,10 @@ gutil_bytes_equal_data(
GBytes* bytes,
const GUtilData* data); /* Since 1.0.41 */

gsize
gutil_ptrv_length(
gconstpointer ptrv); /* Since 1.0.50 */

G_END_DECLS

#endif /* GUTIL_MISC_H */
Expand Down
20 changes: 18 additions & 2 deletions src/gutil_misc.c
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2016-2019 Jolla Ltd.
* Copyright (C) 2016-2019 Slava Monich <slava.monich@jolla.com>
* Copyright (C) 2016-2020 Jolla Ltd.
* Copyright (C) 2016-2020 Slava Monich <slava.monich@jolla.com>
*
* You may use this file under the terms of BSD license as follows:
*
Expand Down Expand Up @@ -415,6 +415,22 @@ gutil_bytes_equal_data(
}
}

/* Calculates the length of NULL-terminated array of pointers */
gsize
gutil_ptrv_length(
gconstpointer ptrv) /* Since 1.0.50 */
{
if (G_LIKELY(ptrv)) {
gsize len = 0;
const gconstpointer* ptr = ptrv;

while (*ptr++) len++;
return len;
} else {
return 0;
}
}

/*
* Local Variables:
* mode: C
Expand Down
9 changes: 2 additions & 7 deletions src/gutil_strv.c
Expand Up @@ -31,6 +31,7 @@
*/

#include "gutil_strv.h"
#include "gutil_misc.h"

#include <stdlib.h>

Expand All @@ -41,13 +42,7 @@ guint
gutil_strv_length(
const GStrV* sv)
{
if (G_LIKELY(sv)) {
guint i = 0;
while (sv[i]) i++;
return i;
} else {
return 0;
}
return (guint) gutil_ptrv_length(sv);
}

/**
Expand Down
22 changes: 20 additions & 2 deletions test/test_misc/test_misc.c
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2016-2019 Jolla Ltd.
* Copyright (C) 2016-2019 Slava Monich <slava.monich@jolla.com>
* Copyright (C) 2016-2020 Jolla Ltd.
* Copyright (C) 2016-2020 Slava Monich <slava.monich@jolla.com>
*
* You may use this file under the terms of BSD license as follows:
*
Expand Down Expand Up @@ -484,6 +484,23 @@ test_bytes_equal(
g_bytes_unref(bytes);
}

/*==========================================================================*
* ptrv
*==========================================================================*/

static
void
test_ptrv(
void)
{
static const gconstpointer ptrv0[] = { NULL };
static const gconstpointer ptrv1[] = { ptrv0, NULL };

g_assert_cmpuint(gutil_ptrv_length(NULL), == ,0);
g_assert_cmpuint(gutil_ptrv_length(ptrv0), == ,0);
g_assert_cmpuint(gutil_ptrv_length(ptrv1), == ,1);
}

/*==========================================================================*
* Common
*==========================================================================*/
Expand Down Expand Up @@ -513,6 +530,7 @@ int main(int argc, char* argv[])
g_test_add_func(TEST_("bytes_concat"), test_bytes_concat);
g_test_add_func(TEST_("bytes_xor"), test_bytes_xor);
g_test_add_func(TEST_("bytes_equal"), test_bytes_equal);
g_test_add_func(TEST_("ptrv"), test_ptrv);
test_init(&test_opt, argc, argv);
return g_test_run();
}
Expand Down

0 comments on commit c188559

Please sign in to comment.