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 parent to tal from (often NULL)
17 * @string: the string to split
18 * @delims: delimiters where lines should be split.
19 * @flags: whether to include empty substrings.
21 * This function splits a single string into multiple strings. The
22 * original string is untouched: an array is allocated (using tal)
23 * pointing to copies of each substring. Multiple delimiters result
24 * in empty substrings. By definition, no delimiters will appear in
27 * The final char * in the array will be NULL.
30 * #include <ccan/tal/str/str.h>
32 * static unsigned int count_long_lines(const char *string)
35 * unsigned int i, long_lines = 0;
37 * // Can only fail on out-of-memory.
38 * lines = strsplit(NULL, string, "\n", STR_NO_EMPTY);
39 * for (i = 0; lines[i] != NULL; i++)
40 * if (strlen(lines[i]) > 80)
46 char **strsplit(const void *ctx, const char *string, const char *delims,
55 * strjoin - Join an array of substrings into one long string
56 * @ctx: the context to tal from (often NULL)
57 * @strings: the NULL-terminated array of strings to join
58 * @delim: the delimiter to insert between the strings
59 * @flags: whether to add a delimieter to the end
61 * This function joins an array of strings into a single string. The
62 * return value is allocated using tal. Each string in @strings is
63 * followed by a copy of @delim.
66 * // Append the string "--EOL" to each line.
67 * static char *append_to_all_lines(const char *string)
71 * lines = strsplit(NULL, string, "\n", STR_EMPTY_OK);
72 * ret = strjoin(NULL, lines, "-- EOL\n", STR_TRAIL);
77 char *strjoin(const void *ctx, char *strings[], const char *delim,
81 * strreg - match and extract from a string via (extended) regular expressions.
82 * @ctx: the context to tal from (often NULL)
83 * @string: the string to try to match.
84 * @regex: the regular expression to match.
85 * ...: pointers to strings to allocate for subexpressions.
87 * Returns true if we matched, in which case any parenthesized
88 * expressions in @regex are allocated and placed in the char **
89 * arguments following @regex. NULL arguments mean the match is not
90 * saved. The order of the strings is the order
91 * of opening braces in the expression: in the case of repeated
92 * expressions (eg "([a-z])*") the last one is saved, in the case of
93 * non-existent matches (eg "([a-z]*)?") the pointer is set to NULL.
95 * Allocation failures or malformed regular expressions return false.
98 * regcomp(3), regex(3).
101 * // Given 'My name is Rusty' outputs 'Hello Rusty!'
102 * // Given 'my first name is Rusty Russell' outputs 'Hello Rusty Russell!'
103 * // Given 'My name isnt Rusty Russell' outputs 'Hello there!'
104 * int main(int argc, char *argv[])
106 * char *person, *input;
108 * // Join args and trim trailing space.
109 * input = strjoin(NULL, argv+1, " ", STR_NO_TRAIL);
110 * if (strreg(NULL, input, "[Mm]y (first )?name is ([A-Za-z ]+)",
112 * printf("Hello %s!\n", person);
114 * printf("Hello there!\n");
118 bool strreg(const void *ctx, const char *string, const char *regex, ...);
119 #endif /* CCAN_STR_TAL_H */