summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRusty Russell <rusty@rustcorp.com.au>2012-12-03 19:29:39 +1030
committerRusty Russell <rusty@rustcorp.com.au>2012-12-03 19:29:39 +1030
commit737dcc0fe959d67eb29f57df37b0d7f188e0213d (patch)
treeee009b080a5617d4db6f15ccf94e4bd4356f4a75
parent83c75170a2be2e3fa58a139f866e957aa6b82995 (diff)
tal/str: make tal_count() work for strsplit.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
-rw-r--r--ccan/tal/str/str.c5
-rw-r--r--ccan/tal/str/str.h3
-rw-r--r--ccan/tal/str/test/run.c6
3 files changed, 12 insertions, 2 deletions
diff --git a/ccan/tal/str/str.c b/ccan/tal/str/str.c
index 09c45447..7d5b103a 100644
--- a/ccan/tal/str/str.c
+++ b/ccan/tal/str/str.c
@@ -49,6 +49,11 @@ char **strsplit(const tal_t *ctx,
goto fail;
}
parts[num] = NULL;
+
+ /* Ensure that tal_count() is correct. */
+ if (unlikely(!tal_resize(&parts, num+1)))
+ goto fail;
+
if (taken(delims))
tal_free(delims);
return parts;
diff --git a/ccan/tal/str/str.h b/ccan/tal/str/str.h
index 7d0a9454..295c3a6b 100644
--- a/ccan/tal/str/str.h
+++ b/ccan/tal/str/str.h
@@ -26,7 +26,8 @@ enum strsplit {
* Multiple delimiters result in empty substrings. By definition, no
* delimiters will appear in the substrings.
*
- * The final char * in the array will be NULL.
+ * The final char * in the array will be NULL, and tal_count() will
+ * return the number of elements plus 1 (for that NULL).
*
* Example:
* #include <ccan/tal/str/str.h>
diff --git a/ccan/tal/str/test/run.c b/ccan/tal/str/test/run.c
index 70ebbc92..303a0fd2 100644
--- a/ccan/tal/str/test/run.c
+++ b/ccan/tal/str/test/run.c
@@ -14,24 +14,27 @@ int main(int argc, char *argv[])
char **split, *str;
void *ctx;
- plan_tests(65);
+ plan_tests(69);
split = strsplit(NULL, "hello world", " ", STR_EMPTY_OK);
ok1(!strcmp(split[0], "hello"));
ok1(!strcmp(split[1], ""));
ok1(!strcmp(split[2], "world"));
ok1(split[3] == NULL);
+ ok1(tal_count(split) == 4);
tal_free(split);
split = strsplit(NULL, "hello world", " ", STR_NO_EMPTY);
ok1(!strcmp(split[0], "hello"));
ok1(!strcmp(split[1], "world"));
ok1(split[2] == NULL);
+ ok1(tal_count(split) == 3);
tal_free(split);
split = strsplit(NULL, " hello world", " ", STR_NO_EMPTY);
ok1(!strcmp(split[0], "hello"));
ok1(!strcmp(split[1], "world"));
ok1(split[2] == NULL);
+ ok1(tal_count(split) == 3);
tal_free(split);
split = strsplit(NULL, "hello world", "o ", STR_EMPTY_OK);
@@ -41,6 +44,7 @@ int main(int argc, char *argv[])
ok1(!strcmp(split[3], "w"));
ok1(!strcmp(split[4], "rld"));
ok1(split[5] == NULL);
+ ok1(tal_count(split) == 6);
ctx = split;
split = strsplit(ctx, "hello world", "o ", STR_EMPTY_OK);