X-Git-Url: https://git.ozlabs.org/?a=blobdiff_plain;f=ccan%2Ftal%2Fstr%2Fstr.c;h=7adb9ef5aefbf990067a1dc80569dbba6f679587;hb=64e9e7145aac9502655c5799ab711b9766c1da57;hp=b7df54c42af704eee6e1fbd9d941205ec54a2235;hpb=5582b011948769779b0d839d35873c2bc557f9cb;p=ccan diff --git a/ccan/tal/str/str.c b/ccan/tal/str/str.c index b7df54c4..7adb9ef5 100644 --- a/ccan/tal/str/str.c +++ b/ccan/tal/str/str.c @@ -11,7 +11,6 @@ #include #include #include -#include #include char *tal_strdup(const tal_t *ctx, const char *p) @@ -27,11 +26,9 @@ char *tal_strndup(const tal_t *ctx, const char *p, size_t n) char *ret; /* We have to let through NULL for take(). */ - if (likely(p)) { - len = strlen(p); - if (len > n) - len = n; - } else + if (likely(p)) + len = strnlen(p, n); + else len = n; ret = tal_dup_(ctx, p, 1, len, 1, false, TAL_LABEL(char, "[]")); @@ -40,47 +37,108 @@ char *tal_strndup(const tal_t *ctx, const char *p, size_t n) return ret; } -char *tal_asprintf(const tal_t *ctx, const char *fmt, ...) +char *tal_fmt(const tal_t *ctx, const char *fmt, ...) { va_list ap; char *ret; va_start(ap, fmt); - ret = tal_vasprintf(ctx, fmt, ap); + ret = tal_vfmt(ctx, fmt, ap); va_end(ap); return ret; } -char *tal_vasprintf(const tal_t *ctx, const char *fmt, va_list ap) +static bool do_vfmt(char **buf, size_t off, const char *fmt, va_list ap) { - size_t max; - char *buf; - int ret; - - if (!fmt && taken(fmt)) - return NULL; - /* A decent guess to start. */ - max = strlen(fmt) * 2; - buf = tal_arr(ctx, char, max); - while (buf) { + size_t max = strlen(fmt) * 2 + 1; + bool ok; + + for (;;) { va_list ap2; + int ret; + + if (!tal_resize(buf, off + max)) { + ok = false; + break; + } va_copy(ap2, ap); - ret = vsnprintf(buf, max, fmt, ap2); + ret = vsnprintf(*buf + off, max, fmt, ap2); va_end(ap2); - if (ret < max) + if (ret < max) { + ok = true; break; - if (!tal_resize(&buf, max *= 2)) - buf = tal_free(buf); + } + max *= 2; } + if (taken(fmt)) tal_free(fmt); + return ok; +} + +char *tal_vfmt(const tal_t *ctx, const char *fmt, va_list ap) +{ + char *buf; + + if (!fmt && taken(fmt)) + return NULL; + + /* A decent guess to start. */ + buf = tal_arr(ctx, char, strlen(fmt) * 2); + if (!do_vfmt(&buf, 0, fmt, ap)) + buf = tal_free(buf); return buf; } +bool tal_append_vfmt(char **baseptr, const char *fmt, va_list ap) +{ + if (!fmt && taken(fmt)) + return false; + + return do_vfmt(baseptr, strlen(*baseptr), fmt, ap); +} + +bool tal_append_fmt(char **baseptr, const char *fmt, ...) +{ + va_list ap; + bool ret; + + va_start(ap, fmt); + ret = tal_append_vfmt(baseptr, fmt, ap); + va_end(ap); + + return ret; +} + +char *tal_strcat(const tal_t *ctx, const char *s1, const char *s2) +{ + size_t len1, len2; + char *ret; + + if (unlikely(!s2) && taken(s2)) { + if (taken(s1)) + tal_free(s1); + return NULL; + } + /* We have to let through NULL for take(). */ + len1 = s1 ? strlen(s1) : 0; + len2 = strlen(s2); + + /* We use tal_dup_ here to avoid attaching a length property. */ + ret = tal_dup_(ctx, s1, 1, len1, len2 + 1, false, + TAL_LABEL(char, "[]")); + if (likely(ret)) + memcpy(ret + len1, s2, len2 + 1); + + if (taken(s2)) + tal_free(s2); + return ret; +} + char **tal_strsplit(const tal_t *ctx, const char *string, const char *delims, enum strsplit flags) { @@ -176,9 +234,31 @@ fail: goto out; } +static size_t count_open_braces(const char *string) +{ +#if 1 + size_t num = 0, esc = 0; + + while (*string) { + if (*string == '\\') + esc++; + else { + /* An odd number of \ means it's escaped. */ + if (*string == '(' && (esc & 1) == 0) + num++; + esc = 0; + } + string++; + } + return num; +#else + return strcount(string, "("); +#endif +} + bool tal_strreg(const tal_t *ctx, const char *string, const char *regex, ...) { - size_t nmatch = 1 + strcount(regex, "("); + size_t nmatch = 1 + count_open_braces(regex); regmatch_t matches[nmatch]; regex_t r; bool ret = false;