1 #ifndef CCAN_STR_TALLOC_H
2 #define CCAN_STR_TALLOC_H
7 * strsplit - Split string into an array of substrings
8 * @ctx: the context to tallocate from (often NULL)
9 * @string: the string to split
10 * @delims: delimiters where lines should be split.
12 * This function splits a single string into multiple strings. The
13 * original string is untouched: an array is allocated (using talloc)
14 * pointing to copies of each substring. Multiple delimiters result
15 * in empty substrings. By definition, no delimiters will appear in
18 * The final char * in the array will be NULL, talloc_array_length() of the
19 * returned value is 1 greater than the number of valid elements in
23 * #include <ccan/talloc/talloc.h>
24 * #include <ccan/str_talloc/str_talloc.h>
26 * static unsigned int count_long_lines(const char *string)
29 * unsigned int i, long_lines = 0;
31 * // Can only fail on out-of-memory.
32 * lines = strsplit(NULL, string, "\n");
33 * for (i = 0; lines[i] != NULL; i++)
34 * if (strlen(lines[i]) > 80)
40 char **strsplit(const void *ctx, const char *string, const char *delims);
43 * strjoin - Join an array of substrings into one long string
44 * @ctx: the context to tallocate from (often NULL)
45 * @strings: the NULL-terminated array of strings to join
46 * @delim: the delimiter to insert between the strings
48 * This function joins an array of strings into a single string. The
49 * return value is allocated using talloc. Each string in @strings is
50 * followed by a copy of @delim.
53 * // Append the string "--EOL" to each line.
54 * static char *append_to_all_lines(const char *string)
58 * lines = strsplit(NULL, string, "\n");
59 * ret = strjoin(NULL, lines, "-- EOL\n");
64 char *strjoin(const void *ctx, char *strings[], const char *delim);
67 * strreg - match and extract from a string via (extended) regular expressions.
68 * @ctx: the context to tallocate from (often NULL)
69 * @string: the string to try to match.
70 * @regex: the regular expression to match.
71 * ...: pointers to strings to allocate for subexpressions.
73 * Returns true if we matched, in which case any parenthesized
74 * expressions in @regex are allocated and placed in the char **
75 * arguments following @regex. NULL arguments mean the match is not
76 * saved. The order of the strings is the order
77 * of opening braces in the expression: in the case of repeated
78 * expressions (eg "([a-z])*") the last one is saved, in the case of
79 * non-existent matches (eg "([a-z]*)?") the pointer is set to NULL.
81 * Allocation failures or malformed regular expressions return false.
84 * regcomp(3), regex(3).
87 * // Given 'My name is Rusty' outputs 'Hello Rusty!'
88 * // Given 'my first name is Rusty Russell' outputs 'Hello Rusty Russell!'
89 * // Given 'My name isnt Rusty Russell' outputs 'Hello there!'
90 * int main(int argc, char *argv[])
92 * char *person, *input;
94 * // Join args and trim trailing space.
95 * input = strjoin(NULL, argv+1, " ");
96 * if (strlen(input) != 0)
97 * input[strlen(input)-1] = '\0';
99 * if (strreg(NULL, input, "[Mm]y (first )?name is ([A-Za-z ]+)",
101 * printf("Hello %s!\n", person);
103 * printf("Hello there!\n");
107 bool strreg(const void *ctx, const char *string, const char *regex, ...);
108 #endif /* CCAN_STR_TALLOC_H */