]> git.ozlabs.org Git - ccan/blobdiff - ccan/tal/str/str.c
tal/str: move tal string functions here from tal.
[ccan] / ccan / tal / str / str.c
index 09c45447fb6e3e3fa53882f4e4d829cab001aa0b..b7df54c42af704eee6e1fbd9d941205ec54a2235 100644 (file)
@@ -9,12 +9,80 @@
 #include <regex.h>
 #include <stdarg.h>
 #include <unistd.h>
+#include <stdio.h>
 #include <ccan/str/str.h>
 #include <ccan/tal/tal.h>
 #include <ccan/take/take.h>
 
-char **strsplit(const tal_t *ctx,
-               const char *string, const char *delims, enum strsplit flags)
+char *tal_strdup(const tal_t *ctx, const char *p)
+{
+       /* We have to let through NULL for take(). */
+       return tal_dup_(ctx, p, 1, p ? strlen(p) + 1: 1, 0, false,
+                       TAL_LABEL(char, "[]"));
+}
+
+char *tal_strndup(const tal_t *ctx, const char *p, size_t n)
+{
+       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
+               len = n;
+
+       ret = tal_dup_(ctx, p, 1, len, 1, false, TAL_LABEL(char, "[]"));
+       if (ret)
+               ret[len] = '\0';
+       return ret;
+}
+
+char *tal_asprintf(const tal_t *ctx, const char *fmt, ...)
+{
+       va_list ap;
+       char *ret;
+
+       va_start(ap, fmt);
+       ret = tal_vasprintf(ctx, fmt, ap);
+       va_end(ap);
+
+       return ret;
+}
+
+char *tal_vasprintf(const tal_t *ctx, 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) {
+               va_list ap2;
+
+               va_copy(ap2, ap);
+               ret = vsnprintf(buf, max, fmt, ap2);
+               va_end(ap2);
+
+               if (ret < max)
+                       break;
+               if (!tal_resize(&buf, max *= 2))
+                       buf = tal_free(buf);
+       }
+       if (taken(fmt))
+               tal_free(fmt);
+       return buf;
+}
+
+char **tal_strsplit(const tal_t *ctx,
+                   const char *string, const char *delims, enum strsplit flags)
 {
        char **parts, *str;
        size_t max = 64, num = 0;
@@ -49,6 +117,11 @@ char **strsplit(const tal_t *ctx,
                        goto fail;
        }
        parts[num] = NULL;
+
+       /* Ensure that tal_count() is correct. */
+       if (unlikely(!tal_resize(&parts, num+1)))
+               goto fail;
+
        if (taken(delims))
                tal_free(delims);
        return parts;
@@ -60,8 +133,8 @@ fail:
        return NULL;
 }
 
-char *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)
 {
        unsigned int i;
        char *ret = NULL;
@@ -103,7 +176,7 @@ fail:
        goto out;
 }
 
-bool strreg(const tal_t *ctx, const char *string, const char *regex, ...)
+bool tal_strreg(const tal_t *ctx, const char *string, const char *regex, ...)
 {
        size_t nmatch = 1 + strcount(regex, "(");
        regmatch_t matches[nmatch];