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