]> git.ozlabs.org Git - ccan/blob - ccan/tal/str/str.h
f30911e2df6f246cb39897fe11f00af0c61d0918
[ccan] / ccan / tal / str / str.h
1 /* Licensed under BSD-MIT - see LICENSE file for details */
2 #ifndef CCAN_STR_TAL_H
3 #define CCAN_STR_TAL_H
4 #include <ccan/tal/tal.h>
5 #include <ccan/tal/tal.h>
6 #include <string.h>
7 #include <stdbool.h>
8
9 enum strsplit {
10         STR_EMPTY_OK,
11         STR_NO_EMPTY
12 };
13
14 /**
15  * tal_strsplit - Split string into an array of substrings
16  * @ctx: the context to tal from (often NULL).
17  * @string: the string to split (can be take()).
18  * @delims: delimiters where lines should be split (can be take()).
19  * @flags: whether to include empty substrings.
20  *
21  * This function splits a single string into multiple strings.
22  *
23  * If @string is take(), the returned array will point into the
24  * mangled @string.
25  *
26  * Multiple delimiters result in empty substrings.  By definition, no
27  * delimiters will appear in the substrings.
28  *
29  * The final char * in the array will be NULL, and tal_count() will
30  * return the number of elements plus 1 (for that NULL).
31  *
32  * Example:
33  *      #include <ccan/tal/str/str.h>
34  *      ...
35  *      static unsigned int count_long_lines(const char *string)
36  *      {
37  *              char **lines;
38  *              unsigned int i, long_lines = 0;
39  *
40  *              // Can only fail on out-of-memory.
41  *              lines = tal_strsplit(NULL, string, "\n", STR_NO_EMPTY);
42  *              for (i = 0; lines[i] != NULL; i++)
43  *                      if (strlen(lines[i]) > 80)
44  *                              long_lines++;
45  *              tal_free(lines);
46  *              return long_lines;
47  *      }
48  */
49 char **tal_strsplit(const tal_t *ctx,
50                     const char *string, const char *delims, enum strsplit flag);
51
52 enum strjoin {
53         STR_TRAIL,
54         STR_NO_TRAIL
55 };
56
57 /**
58  * tal_strjoin - Join an array of substrings into one long string
59  * @ctx: the context to tal from (often NULL).
60  * @strings: the NULL-terminated array of strings to join (can be take())
61  * @delim: the delimiter to insert between the strings (can be take())
62  * @flags: whether to add a delimieter to the end
63  *
64  * This function joins an array of strings into a single string.  The
65  * return value is allocated using tal.  Each string in @strings is
66  * followed by a copy of @delim.
67  *
68  * Example:
69  *      // Append the string "--EOL" to each line.
70  *      static char *append_to_all_lines(const char *string)
71  *      {
72  *              char **lines, *ret;
73  *
74  *              lines = tal_strsplit(NULL, string, "\n", STR_EMPTY_OK);
75  *              ret = tal_strjoin(NULL, lines, "-- EOL\n", STR_TRAIL);
76  *              tal_free(lines);
77  *              return ret;
78  *      }
79  */
80 char *tal_strjoin(const void *ctx, char *strings[], const char *delim,
81                   enum strjoin flags);
82
83 /**
84  * tal_strreg - match/extract from a string via (extended) regular expressions.
85  * @ctx: the context to tal from (often NULL)
86  * @string: the string to try to match (can be take())
87  * @regex: the regular expression to match (can be take())
88  * ...: pointers to strings to allocate for subexpressions.
89  *
90  * Returns true if we matched, in which case any parenthesized
91  * expressions in @regex are allocated and placed in the char **
92  * arguments following @regex.  NULL arguments mean the match is not
93  * saved.  The order of the strings is the order
94  * of opening braces in the expression: in the case of repeated
95  * expressions (eg "([a-z])*") the last one is saved, in the case of
96  * non-existent matches (eg "([a-z]*)?") the pointer is set to NULL.
97  *
98  * Allocation failures or malformed regular expressions return false.
99  *
100  * See Also:
101  *      regcomp(3), regex(3).
102  *
103  * Example:
104  *      // Given 'My name is Rusty' outputs 'Hello Rusty!'
105  *      // Given 'my first name is Rusty Russell' outputs 'Hello Rusty Russell!'
106  *      // Given 'My name isnt Rusty Russell' outputs 'Hello there!'
107  *      int main(int argc, char *argv[])
108  *      {
109  *              char *person, *input;
110  *
111  *              // Join args and trim trailing space.
112  *              input = tal_strjoin(NULL, argv+1, " ", STR_NO_TRAIL);
113  *              if (tal_strreg(NULL, input,
114  *                             "[Mm]y (first )?name is ([A-Za-z ]+)",
115  *                             NULL, &person))
116  *                      printf("Hello %s!\n", person);
117  *              else
118  *                      printf("Hello there!\n");
119  *              return 0;
120  *      }
121  */
122 bool tal_strreg(const void *ctx, const char *string, const char *regex, ...);
123 #endif /* CCAN_STR_TAL_H */