/**
* 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:
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;
}
--- /dev/null
+#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;
+}
+
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 */
+#include "string/string.h"
#include <stdlib.h>
#include <stdio.h>
-#include "string/string.h"
+#include "string/string.c"
#include "tap/tap.h"
/* FIXME: ccanize */
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;
}
}
- 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;
}
}
}
+
+ 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();
}