X-Git-Url: http://git.ozlabs.org/?a=blobdiff_plain;f=ccan%2Ftal%2Fstr%2Fstr.c;h=7adb9ef5aefbf990067a1dc80569dbba6f679587;hb=68c74ffd4356e26ba25b4fbbf9a8f832dcb8a0b8;hp=15aa0c046883633d956eb8abbf50c48e9e362c5e;hpb=e7ae27d64226dda9865f1e4b5d9a55adcee04694;p=ccan diff --git a/ccan/tal/str/str.c b/ccan/tal/str/str.c index 15aa0c04..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, "[]")); @@ -55,7 +52,7 @@ char *tal_fmt(const tal_t *ctx, const char *fmt, ...) static bool do_vfmt(char **buf, size_t off, const char *fmt, va_list ap) { /* A decent guess to start. */ - size_t max = strlen(fmt) * 2; + size_t max = strlen(fmt) * 2 + 1; bool ok; for (;;) { @@ -237,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;