6 #include "str_talloc.h"
11 #include <ccan/talloc/talloc.h>
12 #include <ccan/str/str.h>
14 char **strsplit(const void *ctx, const char *string, const char *delims)
17 unsigned int max = 64, num = 0;
19 lines = talloc_array(ctx, char *, max+1);
21 while (*string != '\0') {
22 unsigned int len = strcspn(string, delims);
23 lines[num] = talloc_array(lines, char, len + 1);
24 memcpy(lines[num], string, len);
25 lines[num][len] = '\0';
27 string += strspn(string, delims) ? 1 : 0;
29 lines = talloc_realloc(ctx, lines, char *, max*=2 + 1);
33 /* Shrink, so talloc_get_size works */
34 return talloc_realloc(ctx, lines, char *, num+1);
37 char *strjoin(const void *ctx, char *strings[], const char *delim)
40 char *ret = talloc_strdup(ctx, "");
42 for (i = 0; strings[i]; i++) {
43 ret = talloc_append_string(ret, strings[i]);
44 ret = talloc_append_string(ret, delim);
49 bool strreg(const void *ctx, const char *string, const char *regex, ...)
51 size_t nmatch = 1 + strcount(regex, "(");
52 regmatch_t *matches = talloc_array(ctx, regmatch_t, nmatch);
56 if (!matches || regcomp(&r, regex, REG_EXTENDED) != 0)
59 if (regexec(&r, string, nmatch, matches, 0) == 0) {
65 for (i = 1; i < nmatch; i++) {
67 arg = va_arg(ap, char **);
69 /* eg. ([a-z])? can give "no match". */
70 if (matches[i].rm_so == -1)
73 *arg = talloc_strndup(ctx,
74 string + matches[i].rm_so,