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,
18 unsigned int max = 64, num = 0;
20 lines = talloc_array(ctx, char *, max+1);
22 while (*string != '\0') {
23 unsigned int len = strcspn(string, delims);
24 lines[num] = talloc_array(lines, char, len + 1);
25 memcpy(lines[num], string, len);
26 lines[num][len] = '\0';
28 string += strspn(string, delims) ? 1 : 0;
30 lines = talloc_realloc(ctx, lines, char *, max*=2 + 1);
38 char *strjoin(const void *ctx, char *strings[], const char *delim)
41 char *ret = talloc_strdup(ctx, "");
43 for (i = 0; strings[i]; i++) {
44 ret = talloc_append_string(ret, strings[i]);
45 ret = talloc_append_string(ret, delim);
50 bool strreg(const void *ctx, const char *string, const char *regex, ...)
52 size_t nmatch = 1 + strcount(regex, "(");
53 regmatch_t *matches = talloc_array(ctx, regmatch_t, nmatch);
57 if (!matches || regcomp(&r, regex, REG_EXTENDED) != 0)
60 if (regexec(&r, string, nmatch, matches, 0) == 0) {
66 for (i = 1; i < nmatch; i++) {
68 arg = va_arg(ap, char **);
70 /* eg. ([a-z])? can give "no match". */
71 if (matches[i].rm_so == -1)
74 *arg = talloc_strndup(ctx,
75 string + matches[i].rm_so,