]> git.ozlabs.org Git - ccan/commitdiff
strsplit()
authorRusty Russell <rusty@rustcorp.com.au>
Mon, 28 Jul 2008 03:54:36 +0000 (13:54 +1000)
committerRusty Russell <rusty@rustcorp.com.au>
Mon, 28 Jul 2008 03:54:36 +0000 (13:54 +1000)
ccan/string/_info.c
ccan/string/string.c [new file with mode: 0644]
ccan/string/string.h
ccan/string/test/run.c

index 9cb691ce64f5701cf181fccd9d04d6bc42e4874d..37c6dbf5940ca707dafa180bc71ae55865f89bf0 100644 (file)
@@ -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 (file)
index 0000000..d3d17bb
--- /dev/null
@@ -0,0 +1,33 @@
+#include <unistd.h>
+#include <stdint.h>
+#include <string.h>
+#include <limits.h>
+#include <assert.h>
+#include <stdlib.h>
+#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;
+}
+       
index f4997c69adbef7c08561040c64586235c2892ec2..3b6367479783aba3c0dfa78965872af7d192bb72 100644 (file)
@@ -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 */
index fded0e49a3ebde3044ea9270cd02688055a7f0c3..089bfd3e4fda6a72be3f157b3a1ca60617b71d0c 100644 (file)
@@ -1,6 +1,7 @@
+#include "string/string.h"
 #include <stdlib.h>
 #include <stdio.h>
-#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();
 }