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.
32 * #include <ccan/tal/str/str.h>
34 * static unsigned int count_long_lines(const char *string)
37 * unsigned int i, long_lines = 0;
39 * // Can only fail on out-of-memory.
40 * lines = strsplit(NULL, string, "\n", STR_NO_EMPTY);
41 * for (i = 0; lines[i] != NULL; i++)
42 * if (strlen(lines[i]) > 80)
48 char **strsplit(const tal_t *ctx,
49 const char *string, const char *delims, enum strsplit flags);
57 * strjoin - Join an array of substrings into one long string
58 * @ctx: the context to tal from (often NULL).
59 * @strings: the NULL-terminated array of strings to join (can be take())
60 * @delim: the delimiter to insert between the strings (can be take())
61 * @flags: whether to add a delimieter to the end
63 * This function joins an array of strings into a single string. The
64 * return value is allocated using tal. Each string in @strings is
65 * followed by a copy of @delim.
68 * // Append the string "--EOL" to each line.
69 * static char *append_to_all_lines(const char *string)
73 * lines = strsplit(NULL, string, "\n", STR_EMPTY_OK);
74 * ret = strjoin(NULL, lines, "-- EOL\n", STR_TRAIL);
79 char *strjoin(const void *ctx, char *strings[], const char *delim,
83 * strreg - match and extract from a string via (extended) regular expressions.
84 * @ctx: the context to tal from (often NULL)
85 * @string: the string to try to match (can be take())
86 * @regex: the regular expression to match (can be take())
87 * ...: pointers to strings to allocate for subexpressions.
89 * Returns true if we matched, in which case any parenthesized
90 * expressions in @regex are allocated and placed in the char **
91 * arguments following @regex. NULL arguments mean the match is not
92 * saved. The order of the strings is the order
93 * of opening braces in the expression: in the case of repeated
94 * expressions (eg "([a-z])*") the last one is saved, in the case of
95 * non-existent matches (eg "([a-z]*)?") the pointer is set to NULL.
97 * Allocation failures or malformed regular expressions return false.
100 * regcomp(3), regex(3).
103 * // Given 'My name is Rusty' outputs 'Hello Rusty!'
104 * // Given 'my first name is Rusty Russell' outputs 'Hello Rusty Russell!'
105 * // Given 'My name isnt Rusty Russell' outputs 'Hello there!'
106 * int main(int argc, char *argv[])
108 * char *person, *input;
110 * // Join args and trim trailing space.
111 * input = strjoin(NULL, argv+1, " ", STR_NO_TRAIL);
112 * if (strreg(NULL, input, "[Mm]y (first )?name is ([A-Za-z ]+)",
114 * printf("Hello %s!\n", person);
116 * printf("Hello there!\n");
120 bool strreg(const void *ctx, const char *string, const char *regex, ...);
121 #endif /* CCAN_STR_TAL_H */