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