]> git.ozlabs.org Git - ccan/blob - ccan/str_talloc/str_talloc.h
337422dee3e9af037051890e6db3f5ef02306b6f
[ccan] / ccan / str_talloc / str_talloc.h
1 #ifndef CCAN_STR_TALLOC_H
2 #define CCAN_STR_TALLOC_H
3 #include <string.h>
4 #include <stdbool.h>
5
6 /**
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  *
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
16  * the substrings.
17  *
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
20  * the array.
21  *
22  * Example:
23  *      #include <ccan/talloc/talloc.h>
24  *      #include <ccan/str_talloc/str_talloc.h>
25  *      ...
26  *      static unsigned int count_long_lines(const char *string)
27  *      {
28  *              char **lines;
29  *              unsigned int i, long_lines = 0;
30  *
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)
35  *                              long_lines++;
36  *              talloc_free(lines);
37  *              return long_lines;
38  *      }
39  */
40 char **strsplit(const void *ctx, const char *string, const char *delims);
41
42 /**
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
47  *
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.
51  *
52  * Example:
53  *      // Append the string "--EOL" to each line.
54  *      static char *append_to_all_lines(const char *string)
55  *      {
56  *              char **lines, *ret;
57  *
58  *              lines = strsplit(NULL, string, "\n");
59  *              ret = strjoin(NULL, lines, "-- EOL\n");
60  *              talloc_free(lines);
61  *              return ret;
62  *      }
63  */
64 char *strjoin(const void *ctx, char *strings[], const char *delim);
65
66 /**
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.
72  *
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.
80  *
81  * Allocation failures or malformed regular expressions return false.
82  *
83  * See Also:
84  *      regcomp(3), regex(3).
85  *
86  * Example:
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[])
91  *      {
92  *              char *person, *input;
93  *
94  *              // Join args and trim trailing space.
95  *              input = strjoin(NULL, argv+1, " ");
96  *              if (strlen(input) != 0)
97  *                      input[strlen(input)-1] = '\0';
98  *
99  *              if (strreg(NULL, input, "[Mm]y (first )?name is ([A-Za-z ]+)",
100  *                         NULL, &person))
101  *                      printf("Hello %s!\n", person);
102  *              else
103  *                      printf("Hello there!\n");
104  *              return 0;
105  *      }
106  */
107 bool strreg(const void *ctx, const char *string, const char *regex, ...);
108 #endif /* CCAN_STR_TALLOC_H */