1 /* Licensed under BSD-MIT - see LICENSE file for details */
12 #include <ccan/tal/tal.h>
13 #include <ccan/str/str.h>
15 char **strsplit(const void *ctx, const char *string, const char *delims,
19 size_t max = 64, num = 0;
21 lines = tal_arr(ctx, char *, max+1);
23 if (flags == STR_NO_EMPTY)
24 string += strspn(string, delims);
26 while (*string != '\0') {
27 size_t len = strcspn(string, delims), dlen;
29 lines[num] = tal_arr(lines, char, len + 1);
30 memcpy(lines[num], string, len);
31 lines[num][len] = '\0';
33 dlen = strspn(string, delims);
34 if (flags == STR_EMPTY_OK && dlen)
38 tal_resize(&lines, max*=2 + 1);
44 char *strjoin(const void *ctx, char *strings[], const char *delim,
48 char *ret = tal_strdup(ctx, "");
49 size_t totlen = 0, dlen = strlen(delim);
51 for (i = 0; strings[i]; i++) {
52 size_t len = strlen(strings[i]);
53 if (flags == STR_NO_TRAIL && !strings[i+1])
55 tal_resize(&ret, totlen + len + dlen + 1);
56 memcpy(ret + totlen, strings[i], len);
58 memcpy(ret + totlen, delim, dlen);
65 bool strreg(const void *ctx, const char *string, const char *regex, ...)
67 size_t nmatch = 1 + strcount(regex, "(");
68 regmatch_t *matches = tal_arr(ctx, regmatch_t, nmatch);
72 if (!matches || regcomp(&r, regex, REG_EXTENDED) != 0)
75 if (regexec(&r, string, nmatch, matches, 0) == 0) {
81 for (i = 1; i < nmatch; i++) {
83 arg = va_arg(ap, char **);
85 /* eg. ([a-z])? can give "no match". */
86 if (matches[i].rm_so == -1)
89 *arg = tal_strndup(ctx,
90 string + matches[i].rm_so,