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