From 458c48e8b27a2eff90b51610e86a870e103a28ad Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Mon, 28 Jul 2008 13:54:36 +1000 Subject: [PATCH] strsplit() --- ccan/string/_info.c | 7 ++++--- ccan/string/string.c | 33 +++++++++++++++++++++++++++++++++ ccan/string/string.h | 33 +++++++++++++++++++++++++++++++++ ccan/string/test/run.c | 36 ++++++++++++++++++++++++++++++++++-- 4 files changed, 104 insertions(+), 5 deletions(-) create mode 100644 ccan/string/string.c diff --git a/ccan/string/_info.c b/ccan/string/_info.c index 9cb691ce..37c6dbf5 100644 --- a/ccan/string/_info.c +++ b/ccan/string/_info.c @@ -5,7 +5,7 @@ /** * string - string helper routines * - * This is a grab bag of modules for string comparisons, designed to enhance + * This is a grab bag of modules for string operations, designed to enhance * the standard string.h. * * Example: @@ -27,9 +27,10 @@ int main(int argc, char *argv[]) if (argc != 2) return 1; - if (strcmp(argv[1], "depends") == 0) - /* Nothing. */ + if (strcmp(argv[1], "depends") == 0) { + printf("ccan/talloc\n"); return 0; + } return 1; } diff --git a/ccan/string/string.c b/ccan/string/string.c new file mode 100644 index 00000000..d3d17bbd --- /dev/null +++ b/ccan/string/string.c @@ -0,0 +1,33 @@ +#include +#include +#include +#include +#include +#include +#include "string.h" +#include "talloc/talloc.h" + +char **strsplit(const void *ctx, const char *string, const char *delims, + unsigned int *nump) +{ + char **lines = NULL; + unsigned int max = 64, num = 0; + + lines = talloc_array(ctx, char *, max+1); + + while (*string != '\0') { + unsigned int len = strcspn(string, delims); + lines[num] = talloc_array(lines, char, len + 1); + memcpy(lines[num], string, len); + lines[num][len] = '\0'; + string += len; + string += strspn(string, delims) ? 1 : 0; + if (++num == max) + lines = talloc_realloc(ctx, lines, char *, max*=2 + 1); + } + lines[num] = NULL; + if (nump) + *nump = num; + return lines; +} + diff --git a/ccan/string/string.h b/ccan/string/string.h index f4997c69..3b636747 100644 --- a/ccan/string/string.h +++ b/ccan/string/string.h @@ -43,4 +43,37 @@ static inline bool strends(const char *str, const char *postfix) return streq(str + strlen(str) - strlen(postfix), postfix); } + +/** + * strsplit - Split string into an array of substrings + * @ctx: the context to tallocate from (often NULL) + * @string: the string to split + * @delims: delimiters where lines should be split. + * @nump: optional pointer to place resulting number of lines + * + * This function splits a single string into multiple strings. The + * original string is untouched: an array is allocated (using talloc) + * pointing to copies of each substring. Multiple delimiters result + * in empty substrings. + * + * The final char * in the array will be NULL, so you can use this or + * @nump to find the array length. + * + * Example: + * unsigned int count_long_lines(const char *text) + * { + * char **lines; + * unsigned int i, long_lines = 0; + * + * // Can only fail on out-of-memory. + * lines = strsplit(NULL, string, "\n", NULL); + * for (i = 0; lines[i] != NULL; i++) + * if (strlen(lines[i]) > 80) + * long_lines++; + * talloc_free(lines); + * return long_lines; + * } + */ +char **strsplit(const void *ctx, const char *string, const char *delims, + unsigned int *nump); #endif /* CCAN_STRING_H */ diff --git a/ccan/string/test/run.c b/ccan/string/test/run.c index fded0e49..089bfd3e 100644 --- a/ccan/string/test/run.c +++ b/ccan/string/test/run.c @@ -1,6 +1,7 @@ +#include "string/string.h" #include #include -#include "string/string.h" +#include "string/string.c" #include "tap/tap.h" /* FIXME: ccanize */ @@ -21,6 +22,8 @@ static char *strdup_rev(const char *s) int main(int argc, char *argv[]) { unsigned int i, j, n; + char **split; + void *ctx; char *strings[ARRAY_SIZE(substrings) * ARRAY_SIZE(substrings)]; n = 0; @@ -33,7 +36,7 @@ int main(int argc, char *argv[]) } } - plan_tests(n * n * 5); + plan_tests(n * n * 5 + 16); for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { unsigned int k, identical = 0; @@ -73,5 +76,34 @@ int main(int argc, char *argv[]) } } } + + split = strsplit(NULL, "hello world", " ", &n); + ok1(n == 3); + ok1(streq(split[0], "hello")); + ok1(streq(split[1], "")); + ok1(streq(split[2], "world")); + ok1(split[3] == NULL); + talloc_free(split); + + split = strsplit(NULL, "hello world", " ", NULL); + ok1(streq(split[0], "hello")); + ok1(streq(split[1], "")); + ok1(streq(split[2], "world")); + ok1(split[3] == NULL); + talloc_free(split); + + split = strsplit(NULL, "hello world", "o ", NULL); + ok1(streq(split[0], "hell")); + ok1(streq(split[1], "")); + ok1(streq(split[2], "")); + ok1(streq(split[3], "w")); + ok1(streq(split[4], "rld")); + ok1(split[5] == NULL); + + ctx = split; + split = strsplit(ctx, "hello world", "o ", NULL); + ok1(talloc_parent(split) == ctx); + talloc_free(ctx); + return exit_status(); } -- 2.39.2