X-Git-Url: http://git.ozlabs.org/?a=blobdiff_plain;f=ccan%2Ftal%2Fstr%2Fstr.h;h=0c1821331e6a733b7a43fe3491e19b9065947eb8;hb=eedf1079f38efb2b8dc4fd3f516cce8ac1272c06;hp=f30911e2df6f246cb39897fe11f00af0c61d0918;hpb=15ed4f453713f1a7bdeec22b53704819fa3d447f;p=ccan diff --git a/ccan/tal/str/str.h b/ccan/tal/str/str.h index f30911e2..0c182133 100644 --- a/ccan/tal/str/str.h +++ b/ccan/tal/str/str.h @@ -1,11 +1,74 @@ /* Licensed under BSD-MIT - see LICENSE file for details */ #ifndef CCAN_STR_TAL_H #define CCAN_STR_TAL_H +#ifdef TAL_USE_TALLOC +#include +#else #include -#include +#endif #include #include +/** + * tal_strdup - duplicate a string + * @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); + +/** + * tal_strndup - duplicate a limited amount of a string. + * @ctx: NULL, or tal allocated object to be parent. + * @p: the string to copy (can be take()). + * @n: the maximum length to copy. + * + * Always gives a nul-terminated string, with strlen() <= @n. + */ +char *tal_strndup(const tal_t *ctx, const char *p, 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); + +/** + * tal_vfmt - allocate a formatted string (va_list version) + * @ctx: NULL, or tal allocated object to be parent. + * @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) + 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, ...) 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, 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); + enum strsplit { STR_EMPTY_OK, STR_NO_EMPTY @@ -101,9 +164,9 @@ 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;