]> git.ozlabs.org Git - ccan/commitdiff
strset: allow const arguments to strset_iterate().
authorRusty Russell <rusty@rustcorp.com.au>
Thu, 1 Dec 2011 06:10:51 +0000 (16:40 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Thu, 1 Dec 2011 06:10:51 +0000 (16:40 +1030)
ccan/strset/strset.c
ccan/strset/strset.h
ccan/strset/test/run-iterate-const.c [new file with mode: 0644]

index a5441d4462e27da84ca1cf01ccfc897564c0bb98..8b38ba2978f617dc4d018c045ab4c8b236bedb87 100644 (file)
@@ -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)
index 250ce7189614a357b89cb2bc7620283a2254caaa..9352fe9fb2e2918aa2af8715a64b376765b61cb6 100644 (file)
@@ -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 (file)
index 0000000..42a1d73
--- /dev/null
@@ -0,0 +1,30 @@
+#include <ccan/strset/strset.h>
+#include <ccan/strset/strset.c>
+#include <ccan/tap/tap.h>
+
+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();
+}