X-Git-Url: http://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Fstr_talloc%2Fstr_talloc.h;h=c6e6a84bb00d64a496973d5a756ac31d993c9261;hp=9828cd85d00aa2fc27f695786bfe03cfdabeb02a;hb=c438ec17d7b2efe76e56e5fc5ab88bd4a02735e8;hpb=9965fc25fcc92dc76d1cd4cf9595dc3dc9ebc586 diff --git a/ccan/str_talloc/str_talloc.h b/ccan/str_talloc/str_talloc.h index 9828cd85..c6e6a84b 100644 --- a/ccan/str_talloc/str_talloc.h +++ b/ccan/str_talloc/str_talloc.h @@ -1,3 +1,4 @@ +/* Licensed under LGPLv2.1+ - see LICENSE file for details */ #ifndef CCAN_STR_TALLOC_H #define CCAN_STR_TALLOC_H #include @@ -8,7 +9,6 @@ * @ctx: the context to tallocate from (often NULL) * @string: the string to split * @delims: delimiters where lines should be split. - * @nump: optional pointer to place resulting number of lines * * This function splits a single string into multiple strings. The * original string is untouched: an array is allocated (using talloc) @@ -16,17 +16,21 @@ * in empty substrings. By definition, no delimiters will appear in * the substrings. * - * The final char * in the array will be NULL, so you can use this or - * @nump to find the array length. + * The final char * in the array will be NULL, talloc_array_length() of the + * returned value is 1 greater than the number of valid elements in + * the array. * * Example: - * unsigned int count_long_lines(const char *text) + * #include + * #include + * ... + * static unsigned int count_long_lines(const char *string) * { * char **lines; * unsigned int i, long_lines = 0; * * // Can only fail on out-of-memory. - * lines = strsplit(NULL, string, "\n", NULL); + * lines = strsplit(NULL, string, "\n"); * for (i = 0; lines[i] != NULL; i++) * if (strlen(lines[i]) > 80) * long_lines++; @@ -34,8 +38,7 @@ * return long_lines; * } */ -char **strsplit(const void *ctx, const char *string, const char *delims, - unsigned int *nump); +char **strsplit(const void *ctx, const char *string, const char *delims); /** * strjoin - Join an array of substrings into one long string @@ -49,16 +52,58 @@ char **strsplit(const void *ctx, const char *string, const char *delims, * * Example: * // Append the string "--EOL" to each line. - * char *append_to_all_lines(const char *string) + * static char *append_to_all_lines(const char *string) * { * char **lines, *ret; - * unsigned int i, num, newnum; * - * lines = strsplit(NULL, string, "\n", NULL); + * lines = strsplit(NULL, string, "\n"); * ret = strjoin(NULL, lines, "-- EOL\n"); * talloc_free(lines); * return ret; * } */ char *strjoin(const void *ctx, char *strings[], const char *delim); + +/** + * strreg - match and extract from a string via (extended) regular expressions. + * @ctx: the context to tallocate from (often NULL) + * @string: the string to try to match. + * @regex: the regular expression to match. + * ...: pointers to strings to allocate for subexpressions. + * + * Returns true if we matched, in which case any parenthesized + * expressions in @regex are allocated and placed in the char ** + * arguments following @regex. NULL arguments mean the match is not + * saved. The order of the strings is the order + * of opening braces in the expression: in the case of repeated + * expressions (eg "([a-z])*") the last one is saved, in the case of + * non-existent matches (eg "([a-z]*)?") the pointer is set to NULL. + * + * Allocation failures or malformed regular expressions return false. + * + * See Also: + * regcomp(3), regex(3). + * + * Example: + * // Given 'My name is Rusty' outputs 'Hello Rusty!' + * // Given 'my first name is Rusty Russell' outputs 'Hello Rusty Russell!' + * // Given 'My name isnt Rusty Russell' outputs 'Hello there!' + * int main(int argc, char *argv[]) + * { + * char *person, *input; + * + * // Join args and trim trailing space. + * input = strjoin(NULL, argv+1, " "); + * if (strlen(input) != 0) + * input[strlen(input)-1] = '\0'; + * + * if (strreg(NULL, input, "[Mm]y (first )?name is ([A-Za-z ]+)", + * NULL, &person)) + * printf("Hello %s!\n", person); + * else + * printf("Hello there!\n"); + * return 0; + * } + */ +bool strreg(const void *ctx, const char *string, const char *regex, ...); #endif /* CCAN_STR_TALLOC_H */