1 /* Licensed under BSD-MIT - see LICENSE file for details */
13 #include <ccan/str/str.h>
14 #include <ccan/take/take.h>
16 char *tal_strdup(const tal_t *ctx, const char *p)
18 /* We have to let through NULL for take(). */
19 return tal_dup_(ctx, p, 1, p ? strlen(p) + 1: 1, 0, false,
20 TAL_LABEL(char, "[]"));
23 char *tal_strndup(const tal_t *ctx, const char *p, size_t n)
28 /* We have to let through NULL for take(). */
34 ret = tal_dup_(ctx, p, 1, len, 1, false, TAL_LABEL(char, "[]"));
40 char *tal_fmt(const tal_t *ctx, const char *fmt, ...)
46 ret = tal_vfmt(ctx, fmt, ap);
52 static bool do_vfmt(char **buf, size_t off, const char *fmt, va_list ap)
54 /* A decent guess to start. */
55 size_t max = strlen(fmt) * 2 + 1;
62 if (!tal_resize(buf, off + max)) {
68 ret = vsnprintf(*buf + off, max, fmt, ap2);
83 char *tal_vfmt(const tal_t *ctx, const char *fmt, va_list ap)
87 if (!fmt && taken(fmt))
90 /* A decent guess to start. */
91 buf = tal_arr(ctx, char, strlen(fmt) * 2);
92 if (!do_vfmt(&buf, 0, fmt, ap))
97 bool tal_append_vfmt(char **baseptr, const char *fmt, va_list ap)
99 if (!fmt && taken(fmt))
102 return do_vfmt(baseptr, strlen(*baseptr), fmt, ap);
105 bool tal_append_fmt(char **baseptr, const char *fmt, ...)
111 ret = tal_append_vfmt(baseptr, fmt, ap);
117 char *tal_strcat(const tal_t *ctx, const char *s1, const char *s2)
122 if (unlikely(!s2) && taken(s2)) {
127 /* We have to let through NULL for take(). */
128 len1 = s1 ? strlen(s1) : 0;
131 /* We use tal_dup_ here to avoid attaching a length property. */
132 ret = tal_dup_(ctx, s1, 1, len1, len2 + 1, false,
133 TAL_LABEL(char, "[]"));
135 memcpy(ret + len1, s2, len2 + 1);
142 char **tal_strsplit(const tal_t *ctx,
143 const char *string, const char *delims, enum strsplit flags)
146 size_t max = 64, num = 0;
148 parts = tal_arr(ctx, char *, max + 1);
149 if (unlikely(!parts)) {
156 str = tal_strdup(parts, string);
159 if (unlikely(!delims) && is_taken(delims))
162 if (flags == STR_NO_EMPTY)
163 str += strspn(str, delims);
165 while (*str != '\0') {
166 size_t len = strcspn(str, delims), dlen;
169 dlen = strspn(str + len, delims);
170 parts[num][len] = '\0';
171 if (flags == STR_EMPTY_OK && dlen)
174 if (++num == max && !tal_resize(&parts, max*=2 + 1))
179 /* Ensure that tal_count() is correct. */
180 if (unlikely(!tal_resize(&parts, num+1)))
194 char *tal_strjoin(const tal_t *ctx,
195 char *strings[], const char *delim, enum strjoin flags)
199 size_t totlen = 0, dlen;
201 if (unlikely(!strings) && is_taken(strings))
204 if (unlikely(!delim) && is_taken(delim))
207 dlen = strlen(delim);
208 ret = tal_arr(ctx, char, dlen*2+1);
213 for (i = 0; strings[i]; i++) {
214 size_t len = strlen(strings[i]);
216 if (flags == STR_NO_TRAIL && !strings[i+1])
218 if (!tal_resize(&ret, totlen + len + dlen + 1))
220 memcpy(ret + totlen, strings[i], len);
222 memcpy(ret + totlen, delim, dlen);
237 static size_t count_open_braces(const char *string)
240 size_t num = 0, esc = 0;
246 /* An odd number of \ means it's escaped. */
247 if (*string == '(' && (esc & 1) == 0)
255 return strcount(string, "(");
259 bool tal_strreg(const tal_t *ctx, const char *string, const char *regex, ...)
261 size_t nmatch = 1 + count_open_braces(regex);
262 regmatch_t matches[nmatch];
268 if (unlikely(!regex) && is_taken(regex))
271 if (regcomp(&r, regex, REG_EXTENDED) != 0)
274 if (unlikely(!string) && is_taken(string))
277 if (regexec(&r, string, nmatch, matches, 0) != 0)
282 for (i = 1; i < nmatch; i++) {
283 char **arg = va_arg(ap, char **);
285 /* eg. ([a-z])? can give "no match". */
286 if (matches[i].rm_so == -1)
289 *arg = tal_strndup(ctx,
290 string + matches[i].rm_so,
293 /* FIXME: If we fail, we set some and leak! */