Browse Source

strsplit()

Rusty Russell 17 years ago
parent
commit
458c48e8b2
4 changed files with 104 additions and 5 deletions
  1. 4 3
      ccan/string/_info.c
  2. 33 0
      ccan/string/string.c
  3. 33 0
      ccan/string/string.h
  4. 34 2
      ccan/string/test/run.c

+ 4 - 3
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;
 }

+ 33 - 0
ccan/string/string.c

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

+ 33 - 0
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 */

+ 34 - 2
ccan/string/test/run.c

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