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, "");
41 size_t totlen = 0, dlen = strlen(delim);
43 for (i = 0; strings[i]; i++) {
44 size_t len = strlen(strings[i]);
45 ret = talloc_realloc(ctx, ret, char, totlen + len + dlen + 1);
46 memcpy(ret + totlen, strings[i], len);
48 memcpy(ret + totlen, delim, dlen);
55 bool strreg(const void *ctx, const char *string, const char *regex, ...)
57 size_t nmatch = 1 + strcount(regex, "(");
58 regmatch_t *matches = talloc_array(ctx, regmatch_t, nmatch);
62 if (!matches || regcomp(&r, regex, REG_EXTENDED) != 0)
65 if (regexec(&r, string, nmatch, matches, 0) == 0) {
71 for (i = 1; i < nmatch; i++) {
73 arg = va_arg(ap, char **);
75 /* eg. ([a-z])? can give "no match". */
76 if (matches[i].rm_so == -1)
79 *arg = talloc_strndup(ctx,
80 string + matches[i].rm_so,