]> git.ozlabs.org Git - ccan/blobdiff - ccan/tal/str/str.h
take, tal, tal/path, tal/str, tal/talloc: annotate APIs with TAKES.
[ccan] / ccan / tal / str / str.h
index 4a6c474a4428a3e79998d1168b09e8c28d2f57a9..ec853defccf9804b42a02879070c12faa493d06b 100644 (file)
@@ -1,8 +1,11 @@
 /* Licensed under BSD-MIT - see LICENSE file for details */
 #ifndef CCAN_STR_TAL_H
 #define CCAN_STR_TAL_H
+#ifdef TAL_USE_TALLOC
+#include <ccan/tal/talloc/talloc.h>
+#else
 #include <ccan/tal/tal.h>
-#include <ccan/tal/tal.h>
+#endif
 #include <string.h>
 #include <stdbool.h>
 
@@ -11,7 +14,7 @@
  * @ctx: NULL, or tal allocated object to be parent.
  * @p: the string to copy (can be take()).
  */
-char *tal_strdup(const tal_t *ctx, const char *p);
+char *tal_strdup(const tal_t *ctx, const char *p TAKES);
 
 /**
  * tal_strndup - duplicate a limited amount of a string.
@@ -21,14 +24,14 @@ char *tal_strdup(const tal_t *ctx, const char *p);
  *
  * Always gives a nul-terminated string, with strlen() <= @n.
  */
-char *tal_strndup(const tal_t *ctx, const char *p, size_t n);
+char *tal_strndup(const tal_t *ctx, const char *p TAKES, size_t n);
 
 /**
  * tal_fmt - allocate a formatted string
  * @ctx: NULL, or tal allocated object to be parent.
  * @fmt: the printf-style format (can be take()).
  */
-char *tal_fmt(const tal_t *ctx, const char *fmt, ...) PRINTF_FMT(2,3);
+char *tal_fmt(const tal_t *ctx, const char *fmt TAKES, ...) PRINTF_FMT(2,3);
 
 /**
  * tal_vfmt - allocate a formatted string (va_list version)
@@ -36,16 +39,35 @@ char *tal_fmt(const tal_t *ctx, const char *fmt, ...) PRINTF_FMT(2,3);
  * @fmt: the printf-style format (can be take()).
  * @va: the va_list containing the format args.
  */
-char *tal_vfmt(const tal_t *ctx, const char *fmt, va_list ap)
+char *tal_vfmt(const tal_t *ctx, const char *fmt TAKES, va_list ap)
        PRINTF_FMT(2,0);
 
+/**
+ * tal_append_fmt - append a formatted string to a talloc string.
+ * @baseptr: a pointer to the tal string to be appended to.
+ * @fmt: the printf-style format (can be take()).
+ *
+ * Returns false on allocation failure.
+ */
+bool tal_append_fmt(char **baseptr, const char *fmt TAKES, ...) PRINTF_FMT(2,3);
+
+/**
+ * tal_append_vfmt - append a formatted string to a talloc string (va_list)
+ * @baseptr: a pointer to the tal string to be appended to.
+ * @fmt: the printf-style format (can be take()).
+ * @va: the va_list containing the format args.
+ *
+ * Returns false on allocation failure.
+ */
+bool tal_append_vfmt(char **baseptr, const char *fmt TAKES, va_list ap);
+
 /**
  * tal_strcat - join two strings together
  * @ctx: NULL, or tal allocated object to be parent.
  * @s1: the first string (can be take()).
  * @s2: the second string (can be take()).
  */
-char *tal_strcat(const tal_t *ctx, const char *s1, const char *s2);
+char *tal_strcat(const tal_t *ctx, const char *s1 TAKES, const char *s2 TAKES);
 
 enum strsplit {
        STR_EMPTY_OK,
@@ -88,7 +110,9 @@ enum strsplit {
  *     }
  */
 char **tal_strsplit(const tal_t *ctx,
-                   const char *string, const char *delims, enum strsplit flag);
+                   const char *string TAKES,
+                   const char *delims TAKES,
+                   enum strsplit flag);
 
 enum strjoin {
        STR_TRAIL,
@@ -118,7 +142,9 @@ enum strjoin {
  *             return ret;
  *     }
  */
-char *tal_strjoin(const void *ctx, char *strings[], const char *delim,
+char *tal_strjoin(const void *ctx,
+                 char *strings[] TAKES,
+                 const char *delim TAKES,
                  enum strjoin flags);
 
 /**
@@ -142,13 +168,14 @@ char *tal_strjoin(const void *ctx, char *strings[], const char *delim,
  *     regcomp(3), regex(3).
  *
  * Example:
- *     // Given 'My name is Rusty' outputs 'Hello Rusty!'
- *     // Given 'my first name is Rusty Russell' outputs 'Hello Rusty Russell!'
- *     // Given 'My name isnt Rusty Russell' outputs 'Hello there!'
+ *     // Given "My name is Rusty" outputs "Hello Rusty!\n"
+ *     // Given "my first name is Rusty Russell" outputs "Hello Rusty Russell!\n"
+ *     // Given "My name isnt Rusty Russell" outputs "Hello there!\n"
  *     int main(int argc, char *argv[])
  *     {
  *             char *person, *input;
  *
+ *             (void)argc;
  *             // Join args and trim trailing space.
  *             input = tal_strjoin(NULL, argv+1, " ", STR_NO_TRAIL);
  *             if (tal_strreg(NULL, input,
@@ -160,5 +187,6 @@ char *tal_strjoin(const void *ctx, char *strings[], const char *delim,
  *             return 0;
  *     }
  */
-bool tal_strreg(const void *ctx, const char *string, const char *regex, ...);
+bool tal_strreg(const void *ctx, const char *string TAKES,
+               const char *regex TAKES, ...);
 #endif /* CCAN_STR_TAL_H */