1 /* Licensed under BSD-MIT - see LICENSE file for details */
4 #include <ccan/tal/tal.h>
5 #include <ccan/tal/tal.h>
15 * strsplit - Split string into an array of substrings
16 * @ctx: the context to tal from (often NULL).
17 * @string: the string to split (can be take()).
18 * @delims: delimiters where lines should be split (can be take()).
19 * @flags: whether to include empty substrings.
21 * This function splits a single string into multiple strings.
23 * If @string is take(), the returned array will point into the
26 * Multiple delimiters result in empty substrings. By definition, no
27 * delimiters will appear in the substrings.
29 * The final char * in the array will be NULL, and tal_count() will
30 * return the number of elements plus 1 (for that NULL).
33 * #include <ccan/tal/str/str.h>
35 * static unsigned int count_long_lines(const char *string)
38 * unsigned int i, long_lines = 0;
40 * // Can only fail on out-of-memory.
41 * lines = strsplit(NULL, string, "\n", STR_NO_EMPTY);
42 * for (i = 0; lines[i] != NULL; i++)
43 * if (strlen(lines[i]) > 80)
49 char **strsplit(const tal_t *ctx,
50 const char *string, const char *delims, enum strsplit flags);
58 * strjoin - Join an array of substrings into one long string
59 * @ctx: the context to tal from (often NULL).
60 * @strings: the NULL-terminated array of strings to join (can be take())
61 * @delim: the delimiter to insert between the strings (can be take())
62 * @flags: whether to add a delimieter to the end
64 * This function joins an array of strings into a single string. The
65 * return value is allocated using tal. Each string in @strings is
66 * followed by a copy of @delim.
69 * // Append the string "--EOL" to each line.
70 * static char *append_to_all_lines(const char *string)
74 * lines = strsplit(NULL, string, "\n", STR_EMPTY_OK);
75 * ret = strjoin(NULL, lines, "-- EOL\n", STR_TRAIL);
80 char *strjoin(const void *ctx, char *strings[], const char *delim,
84 * strreg - match and extract from a string via (extended) regular expressions.
85 * @ctx: the context to tal from (often NULL)
86 * @string: the string to try to match (can be take())
87 * @regex: the regular expression to match (can be take())
88 * ...: pointers to strings to allocate for subexpressions.
90 * Returns true if we matched, in which case any parenthesized
91 * expressions in @regex are allocated and placed in the char **
92 * arguments following @regex. NULL arguments mean the match is not
93 * saved. The order of the strings is the order
94 * of opening braces in the expression: in the case of repeated
95 * expressions (eg "([a-z])*") the last one is saved, in the case of
96 * non-existent matches (eg "([a-z]*)?") the pointer is set to NULL.
98 * Allocation failures or malformed regular expressions return false.
101 * regcomp(3), regex(3).
104 * // Given 'My name is Rusty' outputs 'Hello Rusty!'
105 * // Given 'my first name is Rusty Russell' outputs 'Hello Rusty Russell!'
106 * // Given 'My name isnt Rusty Russell' outputs 'Hello there!'
107 * int main(int argc, char *argv[])
109 * char *person, *input;
111 * // Join args and trim trailing space.
112 * input = strjoin(NULL, argv+1, " ", STR_NO_TRAIL);
113 * if (strreg(NULL, input, "[Mm]y (first )?name is ([A-Za-z ]+)",
115 * printf("Hello %s!\n", person);
117 * printf("Hello there!\n");
121 bool strreg(const void *ctx, const char *string, const char *regex, ...);
122 #endif /* CCAN_STR_TAL_H */