1 /* Licensed under BSD-MIT - see LICENSE file for details */
5 #include <ccan/tal/talloc/talloc.h>
7 #include <ccan/tal/tal.h>
13 * tal_strdup - duplicate a string
14 * @ctx: NULL, or tal allocated object to be parent.
15 * @p: the string to copy (can be take()).
17 * The returned string will have tal_count() == strlen() + 1.
19 #define tal_strdup(ctx, p) tal_strdup_(ctx, p, TAL_LABEL(char, "[]"))
20 char *tal_strdup_(const tal_t *ctx, const char *p TAKES, const char *label);
23 * tal_strndup - duplicate a limited amount of a string.
24 * @ctx: NULL, or tal allocated object to be parent.
25 * @p: the string to copy (can be take()).
26 * @n: the maximum length to copy.
28 * Always gives a nul-terminated string, with strlen() <= @n.
29 * The returned string will have tal_count() == strlen() + 1.
31 #define tal_strndup(ctx, p, n) tal_strndup_(ctx, p, n, TAL_LABEL(char, "[]"))
32 char *tal_strndup_(const tal_t *ctx, const char *p TAKES, size_t n,
36 * tal_fmt - allocate a formatted string
37 * @ctx: NULL, or tal allocated object to be parent.
38 * @fmt: the printf-style format (can be take()).
40 * The returned string will have tal_count() == strlen() + 1.
42 #define tal_fmt(ctx, ...) \
43 tal_fmt_(ctx, TAL_LABEL(char, "[]"), __VA_ARGS__)
44 char *tal_fmt_(const tal_t *ctx, const char *label, const char *fmt TAKES,
48 * tal_vfmt - allocate a formatted string (va_list version)
49 * @ctx: NULL, or tal allocated object to be parent.
50 * @fmt: the printf-style format (can be take()).
51 * @va: the va_list containing the format args.
53 * The returned string will have tal_count() == strlen() + 1.
55 #define tal_vfmt(ctx, fmt, va) \
56 tal_vfmt_(ctx, fmt, va, TAL_LABEL(char, "[]"))
57 char *tal_vfmt_(const tal_t *ctx, const char *fmt TAKES, va_list ap,
62 * tal_append_fmt - append a formatted string to a talloc string.
63 * @baseptr: a pointer to the tal string to be appended to.
64 * @fmt: the printf-style format (can be take()).
66 * Returns false on allocation failure.
67 * Otherwise tal_count(*@baseptr) == strlen(*@baseptr) + 1.
69 bool tal_append_fmt(char **baseptr, const char *fmt TAKES, ...) PRINTF_FMT(2,3);
72 * tal_append_vfmt - append a formatted string to a talloc string (va_list)
73 * @baseptr: a pointer to the tal string to be appended to.
74 * @fmt: the printf-style format (can be take()).
75 * @va: the va_list containing the format args.
77 * Returns false on allocation failure.
78 * Otherwise tal_count(*@baseptr) == strlen(*@baseptr) + 1.
80 bool tal_append_vfmt(char **baseptr, const char *fmt TAKES, va_list ap);
83 * tal_strcat - join two strings together
84 * @ctx: NULL, or tal allocated object to be parent.
85 * @s1: the first string (can be take()).
86 * @s2: the second string (can be take()).
88 * The returned string will have tal_count() == strlen() + 1.
90 #define tal_strcat(ctx, s1, s2) tal_strcat_(ctx, s1, s2, TAL_LABEL(char, "[]"))
91 char *tal_strcat_(const tal_t *ctx, const char *s1 TAKES, const char *s2 TAKES,
100 * tal_strsplit - Split string into an array of substrings
101 * @ctx: the context to tal from (often NULL).
102 * @string: the string to split (can be take()).
103 * @delims: delimiters where lines should be split (can be take()).
104 * @flags: whether to include empty substrings.
106 * This function splits a single string into multiple strings.
108 * If @string is take(), the returned array will point into the
111 * Multiple delimiters result in empty substrings. By definition, no
112 * delimiters will appear in the substrings.
114 * The final char * in the array will be NULL, and tal_count() will
115 * return the number of elements plus 1 (for that NULL).
118 * #include <ccan/tal/str/str.h>
120 * static unsigned int count_long_lines(const char *string)
123 * unsigned int i, long_lines = 0;
125 * // Can only fail on out-of-memory.
126 * lines = tal_strsplit(NULL, string, "\n", STR_NO_EMPTY);
127 * for (i = 0; lines[i] != NULL; i++)
128 * if (strlen(lines[i]) > 80)
134 #define tal_strsplit(ctx, string, delims, flag) \
135 tal_strsplit_(ctx, string, delims, flag, TAL_LABEL(char *, "[]"))
136 char **tal_strsplit_(const tal_t *ctx,
137 const char *string TAKES,
138 const char *delims TAKES,
148 * tal_strjoin - Join an array of substrings into one long string
149 * @ctx: the context to tal from (often NULL).
150 * @strings: the NULL-terminated array of strings to join (can be take())
151 * @delim: the delimiter to insert between the strings (can be take())
152 * @flags: whether to add a delimieter to the end
154 * This function joins an array of strings into a single string. The
155 * return value is allocated using tal. Each string in @strings is
156 * followed by a copy of @delim.
158 * The returned string will have tal_count() == strlen() + 1.
161 * // Append the string "--EOL" to each line.
162 * static char *append_to_all_lines(const char *string)
164 * char **lines, *ret;
166 * lines = tal_strsplit(NULL, string, "\n", STR_EMPTY_OK);
167 * ret = tal_strjoin(NULL, lines, "-- EOL\n", STR_TRAIL);
172 #define tal_strjoin(ctx, strings, delim, flags) \
173 tal_strjoin_(ctx, strings, delim, flags, TAL_LABEL(char, "[]"))
174 char *tal_strjoin_(const void *ctx,
175 char *strings[] TAKES,
176 const char *delim TAKES,
181 * tal_strreg - match/extract from a string via (extended) regular expressions.
182 * @ctx: the context to tal from (often NULL)
183 * @string: the string to try to match (can be take())
184 * @regex: the regular expression to match (can be take())
185 * ...: pointers to strings to allocate for subexpressions.
187 * Returns true if we matched, in which case any parenthesized
188 * expressions in @regex are allocated and placed in the char **
189 * arguments following @regex. NULL arguments mean the match is not
190 * saved. The order of the strings is the order
191 * of opening braces in the expression: in the case of repeated
192 * expressions (eg "([a-z])*") the last one is saved, in the case of
193 * non-existent matches (eg "([a-z]*)?") the pointer is set to NULL.
195 * Allocation failures or malformed regular expressions return false.
196 * The allocated strings will have tal_count() == strlen() + 1.
199 * regcomp(3), regex(3).
202 * // Given "My name is Rusty" outputs "Hello Rusty!\n"
203 * // Given "my first name is Rusty Russell" outputs "Hello Rusty Russell!\n"
204 * // Given "My name isnt Rusty Russell" outputs "Hello there!\n"
205 * int main(int argc, char *argv[])
207 * char *person, *input;
210 * // Join args and trim trailing space.
211 * input = tal_strjoin(NULL, argv+1, " ", STR_NO_TRAIL);
212 * if (tal_strreg(NULL, input,
213 * "[Mm]y (first )?name is ([A-Za-z ]+)",
215 * printf("Hello %s!\n", person);
217 * printf("Hello there!\n");
221 #define tal_strreg(ctx, string, ...) \
222 tal_strreg_(ctx, string, TAL_LABEL(char, "[]"), __VA_ARGS__)
223 bool tal_strreg_(const void *ctx, const char *string TAKES,
224 const char *label, const char *regex, ...);
225 #endif /* CCAN_STR_TAL_H */