]> git.ozlabs.org Git - ccan/blob - ccan/tal/str/str.h
bdcb27ea3f8e18b398e81ba2f876154d6118b092
[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 #ifdef TAL_USE_TALLOC
5 #include <ccan/tal/talloc/talloc.h>
6 #else
7 #include <ccan/tal/tal.h>
8 #endif
9 #include <string.h>
10 #include <stdbool.h>
11
12 /**
13  * tal_strdup - duplicate a string
14  * @ctx: NULL, or tal allocated object to be parent.
15  * @p: the string to copy (can be take()).
16  */
17 #define tal_strdup(ctx, p) tal_strdup_(ctx, p, TAL_LABEL(char, "[]"))
18 char *tal_strdup_(const tal_t *ctx, const char *p TAKES, const char *label);
19
20 /**
21  * tal_strndup - duplicate a limited amount of a string.
22  * @ctx: NULL, or tal allocated object to be parent.
23  * @p: the string to copy (can be take()).
24  * @n: the maximum length to copy.
25  *
26  * Always gives a nul-terminated string, with strlen() <= @n.
27  */
28 #define tal_strndup(ctx, p, n) tal_strndup_(ctx, p, n, TAL_LABEL(char, "[]"))
29 char *tal_strndup_(const tal_t *ctx, const char *p TAKES, size_t n,
30                    const char *label);
31
32 /**
33  * tal_fmt - allocate a formatted string
34  * @ctx: NULL, or tal allocated object to be parent.
35  * @fmt: the printf-style format (can be take()).
36  */
37 #define tal_fmt(ctx, ...)                                \
38         tal_fmt_(ctx, TAL_LABEL(char, "[]"), __VA_ARGS__)
39 char *tal_fmt_(const tal_t *ctx, const char *label, const char *fmt TAKES,
40                 ...) PRINTF_FMT(3,4);
41
42 /**
43  * tal_vfmt - allocate a formatted string (va_list version)
44  * @ctx: NULL, or tal allocated object to be parent.
45  * @fmt: the printf-style format (can be take()).
46  * @va: the va_list containing the format args.
47  */
48 #define tal_vfmt(ctx, fmt, va)                          \
49         tal_vfmt_(ctx, fmt, va, TAL_LABEL(char, "[]"))
50 char *tal_vfmt_(const tal_t *ctx, const char *fmt TAKES, va_list ap,
51                 const char *label)
52         PRINTF_FMT(2,0);
53
54 /**
55  * tal_append_fmt - append a formatted string to a talloc string.
56  * @baseptr: a pointer to the tal string to be appended to.
57  * @fmt: the printf-style format (can be take()).
58  *
59  * Returns false on allocation failure.
60  */
61 bool tal_append_fmt(char **baseptr, const char *fmt TAKES, ...) PRINTF_FMT(2,3);
62
63 /**
64  * tal_append_vfmt - append a formatted string to a talloc string (va_list)
65  * @baseptr: a pointer to the tal string to be appended to.
66  * @fmt: the printf-style format (can be take()).
67  * @va: the va_list containing the format args.
68  *
69  * Returns false on allocation failure.
70  */
71 bool tal_append_vfmt(char **baseptr, const char *fmt TAKES, va_list ap);
72
73 /**
74  * tal_strcat - join two strings together
75  * @ctx: NULL, or tal allocated object to be parent.
76  * @s1: the first string (can be take()).
77  * @s2: the second string (can be take()).
78  */
79 #define tal_strcat(ctx, s1, s2) tal_strcat_(ctx, s1, s2, TAL_LABEL(char, "[]"))
80 char *tal_strcat_(const tal_t *ctx, const char *s1 TAKES, const char *s2 TAKES,
81                   const char *label);
82
83 enum strsplit {
84         STR_EMPTY_OK,
85         STR_NO_EMPTY
86 };
87
88 /**
89  * tal_strsplit - Split string into an array of substrings
90  * @ctx: the context to tal from (often NULL).
91  * @string: the string to split (can be take()).
92  * @delims: delimiters where lines should be split (can be take()).
93  * @flags: whether to include empty substrings.
94  *
95  * This function splits a single string into multiple strings.
96  *
97  * If @string is take(), the returned array will point into the
98  * mangled @string.
99  *
100  * Multiple delimiters result in empty substrings.  By definition, no
101  * delimiters will appear in the substrings.
102  *
103  * The final char * in the array will be NULL, and tal_count() will
104  * return the number of elements plus 1 (for that NULL).
105  *
106  * Example:
107  *      #include <ccan/tal/str/str.h>
108  *      ...
109  *      static unsigned int count_long_lines(const char *string)
110  *      {
111  *              char **lines;
112  *              unsigned int i, long_lines = 0;
113  *
114  *              // Can only fail on out-of-memory.
115  *              lines = tal_strsplit(NULL, string, "\n", STR_NO_EMPTY);
116  *              for (i = 0; lines[i] != NULL; i++)
117  *                      if (strlen(lines[i]) > 80)
118  *                              long_lines++;
119  *              tal_free(lines);
120  *              return long_lines;
121  *      }
122  */
123 #define tal_strsplit(ctx, string, delims, flag) \
124         tal_strsplit_(ctx, string, delims, flag, TAL_LABEL(char *, "[]"))
125 char **tal_strsplit_(const tal_t *ctx,
126                      const char *string TAKES,
127                      const char *delims TAKES,
128                      enum strsplit flag,
129                      const char *label);
130
131 enum strjoin {
132         STR_TRAIL,
133         STR_NO_TRAIL
134 };
135
136 /**
137  * tal_strjoin - Join an array of substrings into one long string
138  * @ctx: the context to tal from (often NULL).
139  * @strings: the NULL-terminated array of strings to join (can be take())
140  * @delim: the delimiter to insert between the strings (can be take())
141  * @flags: whether to add a delimieter to the end
142  *
143  * This function joins an array of strings into a single string.  The
144  * return value is allocated using tal.  Each string in @strings is
145  * followed by a copy of @delim.
146  *
147  * Example:
148  *      // Append the string "--EOL" to each line.
149  *      static char *append_to_all_lines(const char *string)
150  *      {
151  *              char **lines, *ret;
152  *
153  *              lines = tal_strsplit(NULL, string, "\n", STR_EMPTY_OK);
154  *              ret = tal_strjoin(NULL, lines, "-- EOL\n", STR_TRAIL);
155  *              tal_free(lines);
156  *              return ret;
157  *      }
158  */
159 #define tal_strjoin(ctx, strings, delim, flags)                         \
160         tal_strjoin_(ctx, strings, delim, flags, TAL_LABEL(char, "[]"))
161 char *tal_strjoin_(const void *ctx,
162                    char *strings[] TAKES,
163                    const char *delim TAKES,
164                    enum strjoin flags,
165                    const char *label);
166
167 /**
168  * tal_strreg - match/extract from a string via (extended) regular expressions.
169  * @ctx: the context to tal from (often NULL)
170  * @string: the string to try to match (can be take())
171  * @regex: the regular expression to match (can be take())
172  * ...: pointers to strings to allocate for subexpressions.
173  *
174  * Returns true if we matched, in which case any parenthesized
175  * expressions in @regex are allocated and placed in the char **
176  * arguments following @regex.  NULL arguments mean the match is not
177  * saved.  The order of the strings is the order
178  * of opening braces in the expression: in the case of repeated
179  * expressions (eg "([a-z])*") the last one is saved, in the case of
180  * non-existent matches (eg "([a-z]*)?") the pointer is set to NULL.
181  *
182  * Allocation failures or malformed regular expressions return false.
183  *
184  * See Also:
185  *      regcomp(3), regex(3).
186  *
187  * Example:
188  *      // Given "My name is Rusty" outputs "Hello Rusty!\n"
189  *      // Given "my first name is Rusty Russell" outputs "Hello Rusty Russell!\n"
190  *      // Given "My name isnt Rusty Russell" outputs "Hello there!\n"
191  *      int main(int argc, char *argv[])
192  *      {
193  *              char *person, *input;
194  *
195  *              (void)argc;
196  *              // Join args and trim trailing space.
197  *              input = tal_strjoin(NULL, argv+1, " ", STR_NO_TRAIL);
198  *              if (tal_strreg(NULL, input,
199  *                             "[Mm]y (first )?name is ([A-Za-z ]+)",
200  *                             NULL, &person))
201  *                      printf("Hello %s!\n", person);
202  *              else
203  *                      printf("Hello there!\n");
204  *              return 0;
205  *      }
206  */
207 #define tal_strreg(ctx, string, ...)                                    \
208         tal_strreg_(ctx, string, TAL_LABEL(char, "[]"), __VA_ARGS__)
209 bool tal_strreg_(const void *ctx, const char *string TAKES,
210                  const char *label, const char *regex, ...);
211 #endif /* CCAN_STR_TAL_H */