1 /* Licensed under LGPLv2.1+ - see LICENSE file for details */
7 #include "str_talloc.h"
12 #include <ccan/talloc/talloc.h>
13 #include <ccan/str/str.h>
15 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);
34 /* Shrink, so talloc_get_size works */
35 return talloc_realloc(ctx, lines, char *, num+1);
38 char *strjoin(const void *ctx, char *strings[], const char *delim)
41 char *ret = talloc_strdup(ctx, "");
42 size_t totlen = 0, dlen = strlen(delim);
44 for (i = 0; strings[i]; i++) {
45 size_t len = strlen(strings[i]);
46 ret = talloc_realloc(ctx, ret, char, totlen + len + dlen + 1);
47 memcpy(ret + totlen, strings[i], len);
49 memcpy(ret + totlen, delim, dlen);
56 bool strreg(const void *ctx, const char *string, const char *regex, ...)
58 size_t nmatch = 1 + strcount(regex, "(");
59 regmatch_t *matches = talloc_array(ctx, regmatch_t, nmatch);
63 if (!matches || regcomp(&r, regex, REG_EXTENDED) != 0)
66 if (regexec(&r, string, nmatch, matches, 0) == 0) {
72 for (i = 1; i < nmatch; i++) {
74 arg = va_arg(ap, char **);
76 /* eg. ([a-z])? can give "no match". */
77 if (matches[i].rm_so == -1)
80 *arg = talloc_strndup(ctx,
81 string + matches[i].rm_so,