From 932aeb6dcc4a6df2d08755f743659451c9721447 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Thu, 1 Dec 2011 16:40:51 +1030 Subject: [PATCH] strset: allow const arguments to strset_iterate(). --- ccan/strset/strset.c | 8 ++++---- ccan/strset/strset.h | 2 +- ccan/strset/test/run-iterate-const.c | 30 ++++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 ccan/strset/test/run-iterate-const.c diff --git a/ccan/strset/strset.c b/ccan/strset/strset.c index a5441d44..8b38ba29 100644 --- a/ccan/strset/strset.c +++ b/ccan/strset/strset.c @@ -229,19 +229,19 @@ char *strset_clear(struct strset *set, const char *member) } static bool iterate(struct strset n, - bool (*handle)(const char *, void *), void *data) + bool (*handle)(const char *, void *), const void *data) { if (n.u.s[0]) - return handle(n.u.s, data); + return handle(n.u.s, (void *)data); if (unlikely(n.u.n->byte_num == (size_t)-1)) - return handle(n.u.n->child[0].u.s, data); + return handle(n.u.n->child[0].u.s, (void *)data); return iterate(n.u.n->child[0], handle, data) || iterate(n.u.n->child[1], handle, data); } void strset_iterate_(const struct strset *set, - bool (*handle)(const char *, void *), void *data) + bool (*handle)(const char *, void *), const void *data) { /* Empty set? */ if (!set->u.n) diff --git a/ccan/strset/strset.h b/ccan/strset/strset.h index 250ce718..9352fe9f 100644 --- a/ccan/strset/strset.h +++ b/ccan/strset/strset.h @@ -139,7 +139,7 @@ void strset_destroy(struct strset *set); const char *), \ (arg)) void strset_iterate_(const struct strset *set, - bool (*handle)(const char *, void *), void *data); + bool (*handle)(const char *, void *), const void *data); /** diff --git a/ccan/strset/test/run-iterate-const.c b/ccan/strset/test/run-iterate-const.c new file mode 100644 index 00000000..42a1d735 --- /dev/null +++ b/ccan/strset/test/run-iterate-const.c @@ -0,0 +1,30 @@ +#include +#include +#include + +static bool found = false; + +/* Make sure const args work. */ +static bool find_string(const char *str, const char *cmp) +{ + if (strcmp(str, cmp) == 0) + found = true; + return false; +} + +int main(void) +{ + struct strset set; + + plan_tests(3); + + strset_init(&set); + ok1(strset_set(&set, "hello")); + ok1(strset_set(&set, "world")); + strset_iterate(&set, find_string, (const char *)"hello"); + ok1(found); + strset_destroy(&set); + + /* This exits depending on whether all tests passed */ + return exit_status(); +} -- 2.39.2