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