X-Git-Url: http://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Ftal%2Fstr%2Fstr.c;h=cec31c752bbe104b3a457ec25f62da7df4d172ef;hp=4606b47e3356955f7989389b9729db33c5b3b71f;hb=55d814230f7fb628bb5303cd53498209c7928040;hpb=a5ebf3146a57b7db05f6b6e2de0f2689a1b06751 diff --git a/ccan/tal/str/str.c b/ccan/tal/str/str.c index 4606b47e..cec31c75 100644 --- a/ccan/tal/str/str.c +++ b/ccan/tal/str/str.c @@ -11,77 +11,111 @@ #include #include #include -#include -#include -char *tal_strdup(const tal_t *ctx, const char *p) +char *tal_strdup_(const tal_t *ctx, const char *p, const char *label) { /* We have to let through NULL for take(). */ - return tal_dup_(ctx, p, 1, p ? strlen(p) + 1: 1, 0, false, - TAL_LABEL(char, "[]")); + return tal_dup_arr_label(ctx, char, p, p ? strlen(p) + 1: 1, 0, label); } -char *tal_strndup(const tal_t *ctx, const char *p, size_t n) +char *tal_strndup_(const tal_t *ctx, const char *p, size_t n, const char *label) { size_t len; 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, "[]")); + ret = tal_dup_arr_label(ctx, char, p, len, 1, label); if (ret) ret[len] = '\0'; return ret; } -char *tal_asprintf(const tal_t *ctx, const char *fmt, ...) +char *tal_fmt_(const tal_t *ctx, const char *label, const char *fmt, ...) { va_list ap; char *ret; va_start(ap, fmt); - ret = tal_vasprintf(ctx, fmt, ap); + ret = tal_vfmt_(ctx, fmt, ap, label); 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; + /* Make sure tal_count() is correct! */ + tal_resize(buf, off + ret + 1); 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, const char *label) +{ + char *buf; + + if (!fmt && taken(fmt)) + return NULL; + + /* A decent guess to start. */ + buf = tal_arr_label(ctx, char, strlen(fmt) * 2, label); + if (!do_vfmt(&buf, 0, fmt, ap)) + buf = tal_free(buf); return buf; } -char *tal_strcat(const tal_t *ctx, const char *s1, const char *s2) +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, + const char *label) { size_t len1, len2; char *ret; @@ -95,9 +129,7 @@ char *tal_strcat(const tal_t *ctx, const char *s1, const char *s2) 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, "[]")); + ret = tal_dup_arr_label(ctx, char, s1, len1, len2 + 1, label); if (likely(ret)) memcpy(ret + len1, s2, len2 + 1); @@ -106,8 +138,9 @@ char *tal_strcat(const tal_t *ctx, const char *s1, const char *s2) return ret; } -char **tal_strsplit(const tal_t *ctx, - const char *string, const char *delims, enum strsplit flags) +char **tal_strsplit_(const tal_t *ctx, + const char *string, const char *delims, enum strsplit flags, + const char *label) { char **parts, *str; size_t max = 64, num = 0; @@ -158,8 +191,9 @@ fail: return NULL; } -char *tal_strjoin(const tal_t *ctx, - char *strings[], const char *delim, enum strjoin flags) +char *tal_strjoin_(const tal_t *ctx, + char *strings[], const char *delim, enum strjoin flags, + const char *label) { unsigned int i; char *ret = NULL; @@ -172,7 +206,7 @@ char *tal_strjoin(const tal_t *ctx, goto fail; dlen = strlen(delim); - ret = tal_arr(ctx, char, dlen*2+1); + ret = tal_arr_label(ctx, char, dlen*2+1, label); if (!ret) goto fail; @@ -190,6 +224,8 @@ char *tal_strjoin(const tal_t *ctx, totlen += dlen; } ret[totlen] = '\0'; + /* Make sure tal_count() is correct! */ + tal_resize(&ret, totlen+1); out: if (taken(strings)) tal_free(strings); @@ -201,9 +237,32 @@ fail: goto out; } -bool tal_strreg(const tal_t *ctx, const char *string, const char *regex, ...) +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 *label, + 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; @@ -231,10 +290,11 @@ bool tal_strreg(const tal_t *ctx, const char *string, const char *regex, ...) if (matches[i].rm_so == -1) *arg = NULL; else { - *arg = tal_strndup(ctx, - string + matches[i].rm_so, - matches[i].rm_eo - - matches[i].rm_so); + *arg = tal_strndup_(ctx, + string + matches[i].rm_so, + matches[i].rm_eo + - matches[i].rm_so, + label); /* FIXME: If we fail, we set some and leak! */ if (!*arg) { ret = false;