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