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