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 #define tal_strdup(ctx, p) tal_strdup_(ctx, p, TAL_LABEL(char, "[]"))
18 char *tal_strdup_(const tal_t *ctx, const char *p TAKES, const char *label);
21 * tal_strndup - duplicate a limited amount of a string.
22 * @ctx: NULL, or tal allocated object to be parent.
23 * @p: the string to copy (can be take()).
24 * @n: the maximum length to copy.
26 * Always gives a nul-terminated string, with strlen() <= @n.
28 #define tal_strndup(ctx, p, n) tal_strndup_(ctx, p, n, TAL_LABEL(char, "[]"))
29 char *tal_strndup_(const tal_t *ctx, const char *p TAKES, size_t n,
33 * tal_fmt - allocate a formatted string
34 * @ctx: NULL, or tal allocated object to be parent.
35 * @fmt: the printf-style format (can be take()).
37 #define tal_fmt(ctx, ...) \
38 tal_fmt_(ctx, TAL_LABEL(char, "[]"), __VA_ARGS__)
39 char *tal_fmt_(const tal_t *ctx, const char *label, const char *fmt TAKES,
43 * tal_vfmt - allocate a formatted string (va_list version)
44 * @ctx: NULL, or tal allocated object to be parent.
45 * @fmt: the printf-style format (can be take()).
46 * @va: the va_list containing the format args.
48 #define tal_vfmt(ctx, fmt, va) \
49 tal_vfmt_(ctx, fmt, va, TAL_LABEL(char, "[]"))
50 char *tal_vfmt_(const tal_t *ctx, const char *fmt TAKES, va_list ap,
55 * tal_append_fmt - append a formatted string to a talloc string.
56 * @baseptr: a pointer to the tal string to be appended to.
57 * @fmt: the printf-style format (can be take()).
59 * Returns false on allocation failure.
61 bool tal_append_fmt(char **baseptr, const char *fmt TAKES, ...) PRINTF_FMT(2,3);
64 * tal_append_vfmt - append a formatted string to a talloc string (va_list)
65 * @baseptr: a pointer to the tal string to be appended to.
66 * @fmt: the printf-style format (can be take()).
67 * @va: the va_list containing the format args.
69 * Returns false on allocation failure.
71 bool tal_append_vfmt(char **baseptr, const char *fmt TAKES, va_list ap);
74 * tal_strcat - join two strings together
75 * @ctx: NULL, or tal allocated object to be parent.
76 * @s1: the first string (can be take()).
77 * @s2: the second string (can be take()).
79 #define tal_strcat(ctx, s1, s2) tal_strcat_(ctx, s1, s2, TAL_LABEL(char, "[]"))
80 char *tal_strcat_(const tal_t *ctx, const char *s1 TAKES, const char *s2 TAKES,
89 * tal_strsplit - Split string into an array of substrings
90 * @ctx: the context to tal from (often NULL).
91 * @string: the string to split (can be take()).
92 * @delims: delimiters where lines should be split (can be take()).
93 * @flags: whether to include empty substrings.
95 * This function splits a single string into multiple strings.
97 * If @string is take(), the returned array will point into the
100 * Multiple delimiters result in empty substrings. By definition, no
101 * delimiters will appear in the substrings.
103 * The final char * in the array will be NULL, and tal_count() will
104 * return the number of elements plus 1 (for that NULL).
107 * #include <ccan/tal/str/str.h>
109 * static unsigned int count_long_lines(const char *string)
112 * unsigned int i, long_lines = 0;
114 * // Can only fail on out-of-memory.
115 * lines = tal_strsplit(NULL, string, "\n", STR_NO_EMPTY);
116 * for (i = 0; lines[i] != NULL; i++)
117 * if (strlen(lines[i]) > 80)
123 #define tal_strsplit(ctx, string, delims, flag) \
124 tal_strsplit_(ctx, string, delims, flag, TAL_LABEL(char *, "[]"))
125 char **tal_strsplit_(const tal_t *ctx,
126 const char *string TAKES,
127 const char *delims TAKES,
137 * tal_strjoin - Join an array of substrings into one long string
138 * @ctx: the context to tal from (often NULL).
139 * @strings: the NULL-terminated array of strings to join (can be take())
140 * @delim: the delimiter to insert between the strings (can be take())
141 * @flags: whether to add a delimieter to the end
143 * This function joins an array of strings into a single string. The
144 * return value is allocated using tal. Each string in @strings is
145 * followed by a copy of @delim.
148 * // Append the string "--EOL" to each line.
149 * static char *append_to_all_lines(const char *string)
151 * char **lines, *ret;
153 * lines = tal_strsplit(NULL, string, "\n", STR_EMPTY_OK);
154 * ret = tal_strjoin(NULL, lines, "-- EOL\n", STR_TRAIL);
159 #define tal_strjoin(ctx, strings, delim, flags) \
160 tal_strjoin_(ctx, strings, delim, flags, TAL_LABEL(char, "[]"))
161 char *tal_strjoin_(const void *ctx,
162 char *strings[] TAKES,
163 const char *delim TAKES,
168 * tal_strreg - match/extract from a string via (extended) regular expressions.
169 * @ctx: the context to tal from (often NULL)
170 * @string: the string to try to match (can be take())
171 * @regex: the regular expression to match (can be take())
172 * ...: pointers to strings to allocate for subexpressions.
174 * Returns true if we matched, in which case any parenthesized
175 * expressions in @regex are allocated and placed in the char **
176 * arguments following @regex. NULL arguments mean the match is not
177 * saved. The order of the strings is the order
178 * of opening braces in the expression: in the case of repeated
179 * expressions (eg "([a-z])*") the last one is saved, in the case of
180 * non-existent matches (eg "([a-z]*)?") the pointer is set to NULL.
182 * Allocation failures or malformed regular expressions return false.
185 * regcomp(3), regex(3).
188 * // Given "My name is Rusty" outputs "Hello Rusty!\n"
189 * // Given "my first name is Rusty Russell" outputs "Hello Rusty Russell!\n"
190 * // Given "My name isnt Rusty Russell" outputs "Hello there!\n"
191 * int main(int argc, char *argv[])
193 * char *person, *input;
196 * // Join args and trim trailing space.
197 * input = tal_strjoin(NULL, argv+1, " ", STR_NO_TRAIL);
198 * if (tal_strreg(NULL, input,
199 * "[Mm]y (first )?name is ([A-Za-z ]+)",
201 * printf("Hello %s!\n", person);
203 * printf("Hello there!\n");
207 #define tal_strreg(ctx, string, ...) \
208 tal_strreg_(ctx, string, TAL_LABEL(char, "[]"), __VA_ARGS__)
209 bool tal_strreg_(const void *ctx, const char *string TAKES,
210 const char *label, const char *regex, ...);
211 #endif /* CCAN_STR_TAL_H */