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.
11 * @nump: optional pointer to place resulting number of lines
13 * This function splits a single string into multiple strings. The
14 * original string is untouched: an array is allocated (using talloc)
15 * pointing to copies of each substring. Multiple delimiters result
16 * in empty substrings. By definition, no delimiters will appear in
19 * The final char * in the array will be NULL, so you can use this or
20 * @nump to find the array length.
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", NULL);
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,
44 * strjoin - Join an array of substrings into one long string
45 * @ctx: the context to tallocate from (often NULL)
46 * @strings: the NULL-terminated array of strings to join
47 * @delim: the delimiter to insert between the strings
49 * This function joins an array of strings into a single string. The
50 * return value is allocated using talloc. Each string in @strings is
51 * followed by a copy of @delim.
54 * // Append the string "--EOL" to each line.
55 * static char *append_to_all_lines(const char *string)
59 * lines = strsplit(NULL, string, "\n", NULL);
60 * ret = strjoin(NULL, lines, "-- EOL\n");
65 char *strjoin(const void *ctx, char *strings[], const char *delim);
68 * strreg - match and extract from a string via (extended) regular expressions.
69 * @ctx: the context to tallocate from (often NULL)
70 * @string: the string to try to match.
71 * @regex: the regular expression to match.
72 * ...: pointers to strings to allocate for subexpressions.
74 * Returns true if we matched, in which case any parenthesized
75 * expressions in @regex are allocated and placed in the char **
76 * arguments following @regex. NULL arguments mean the match is not
77 * saved. The order of the strings is the order
78 * of opening braces in the expression: in the case of repeated
79 * expressions (eg "([a-z])*") the last one is saved, in the case of
80 * non-existent matches (eg "([a-z]*)?") the pointer is set to NULL.
82 * Allocation failures or malformed regular expressions return false.
85 * regcomp(3), regex(3).
88 * // Given 'My name is Rusty' outputs 'Hello Rusty!'
89 * // Given 'my first name is Rusty Russell' outputs 'Hello Rusty Russell!'
90 * // Given 'My name isnt Rusty Russell' outputs 'Hello there!'
91 * int main(int argc, char *argv[])
93 * char *person, *input;
95 * // Join args and trim trailing space.
96 * input = strjoin(NULL, argv+1, " ");
97 * if (strlen(input) != 0)
98 * input[strlen(input)-1] = '\0';
100 * if (strreg(NULL, input, "[Mm]y (first )?name is ([A-Za-z ]+)",
102 * printf("Hello %s!\n", person);
104 * printf("Hello there!\n");
108 bool strreg(const void *ctx, const char *string, const char *regex, ...);
109 #endif /* CCAN_STR_TALLOC_H */