Browse Source

strset: allow const arguments to strset_iterate().

Rusty Russell 14 years ago
parent
commit
932aeb6dcc
3 changed files with 35 additions and 5 deletions
  1. 4 4
      ccan/strset/strset.c
  2. 1 1
      ccan/strset/strset.h
  3. 30 0
      ccan/strset/test/run-iterate-const.c

+ 4 - 4
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)

+ 1 - 1
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);
 
 
 /**

+ 30 - 0
ccan/strset/test/run-iterate-const.c

@@ -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();
+}