Debian Codegraph @

src/basic/strv.h

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include "basic-forward.h"
5
6 #include "../fundamental/strv.h" /* IWYU pragma: export */
7
8 char* strv_find(char * const *l, const char *name) _pure_;
9 char* strv_find_case(char * const *l, const char *name) _pure_;
10 char* strv_find_prefix(char * const *l, const char *name) _pure_;
11 char* strv_find_startswith(char * const *l, const char *name) _pure_;
12 char* strv_find_closest(char * const *l, const char *name) _pure_;
13 /* Given two vectors, the first a list of keys and the second a list of key-value pairs, returns the value
14 * of the first key from the first vector that is found in the second vector. */
15 char* strv_find_first_field(char * const *needles, char * const *haystack) _pure_;
16
17 #define strv_contains(l, s) (!!strv_find((l), (s)))
18 #define strv_contains_case(l, s) (!!strv_find_case((l), (s)))
19
20 char** strv_free(char **l);
21 DEFINE_TRIVIAL_CLEANUP_FUNC(char**, strv_free);
22 #define _cleanup_strv_free_ _cleanup_(strv_freep)
23
24 char** strv_free_erase(char **l);
25 DEFINE_TRIVIAL_CLEANUP_FUNC(char**, strv_free_erase);
26 #define _cleanup_strv_free_erase_ _cleanup_(strv_free_erasep)
27
28 void strv_free_many(char ***strvs, size_t n) _nonnull_if_nonzero_(1, 2);
29
30 char** strv_copy_n(char * const *l, size_t n);
31 static inline char** strv_copy(char * const *l) {
32 return strv_copy_n(l, SIZE_MAX);
33 }
34 int strv_copy_unless_empty(char * const *l, char ***ret);
35
36 size_t strv_length(char * const *l) _pure_;
37
38 int strv_extend_strv(char ***a, char * const *b, bool filter_duplicates);
39 int strv_extend_strv_consume(char ***a, char **b, bool filter_duplicates);
40
41 int strv_extend_strv_biconcat(char ***a, const char *prefix, const char* const *b, const char *suffix);
42 static inline int strv_extend_strv_concat(char ***a, const char* const *b, const char *suffix) {
43 return strv_extend_strv_biconcat(a, NULL, b, suffix);
44 }
45
46 int strv_prepend(char ***l, const char *value);
47
48 /* _with_size() are lower-level functions where the size can be provided externally,
49 * which allows us to skip iterating over the strv to find the end, which saves
50 * a bit of time and reduces the complexity of appending from O(n²) to O(n). */
51
52 int strv_extend_with_size(char ***l, size_t *n, const char *value);
53 static inline int strv_extend(char ***l, const char *value) {
54 return strv_extend_with_size(l, NULL, value);
55 }
56
57 int strv_extend_many_internal(char ***l, const char *value, ...);
58 #define strv_extend_many(l, ...) strv_extend_many_internal(l, __VA_ARGS__, POINTER_MAX)
59
60 int strv_extendf_with_size(char ***l, size_t *n, const char *format, ...) _printf_(3,4);
61 #define strv_extendf(l, ...) strv_extendf_with_size(l, NULL, __VA_ARGS__)
62
63 int strv_extend_joined_with_size_sentinel(char ***l, size_t *n, ...) _sentinel_;
64 #define strv_extend_joined_with_size(l, n, ...) strv_extend_joined_with_size_sentinel(l, n, __VA_ARGS__, NULL)
65 #define strv_extend_joined(l, ...) strv_extend_joined_with_size(l, NULL, __VA_ARGS__)
66
67 int strv_push_with_size(char ***l, size_t *n, char *value);
68 static inline int strv_push(char ***l, char *value) {
69 return strv_push_with_size(l, NULL, value);
70 }
71 int strv_push_pair(char ***l, char *a, char *b);
72
73 int strv_insert(char ***l, size_t position, char *value);
74
75 static inline int strv_push_prepend(char ***l, char *value) {
76 return strv_insert(l, 0, value);
77 }
78
79 int strv_consume_with_size(char ***l, size_t *n, char *value);
80 static inline int strv_consume(char ***l, char *value) {
81 return strv_consume_with_size(l, NULL, value);
82 }
83
84 int strv_consume_pair(char ***l, char *a, char *b);
85 int strv_consume_prepend(char ***l, char *value);
86
87 char** strv_remove(char **l, const char *s);
88 char** strv_uniq(char **l);
89 bool strv_is_uniq(char * const *l) _pure_;
90
91 int strv_compare(char * const *a, char * const *b) _pure_;
92 static inline bool strv_equal(char * const *a, char * const *b) {
93 return strv_compare(a, b) == 0;
94 }
95
96 bool strv_equal_ignore_order(char * const *a, char * const *b) _pure_;
97
98 char** strv_new_internal(const char *x, ...) _sentinel_;
99 char** strv_new_ap(const char *x, va_list ap);
100 #define strv_new(...) strv_new_internal(__VA_ARGS__, NULL)
101
102 #define STRV_IGNORE ((const char *) POINTER_MAX)
103
104 static inline const char* STRV_IFNOTNULL(const char *x) {
105 return x ?: STRV_IGNORE;
106 }
107
108 int strv_split_full(char ***t, const char *s, const char *separators, ExtractFlags flags);
109 char** strv_split(const char *s, const char *separators);
110
111 int strv_split_and_extend_full(char ***t, const char *s, const char *separators, bool filter_duplicates, ExtractFlags flags);
112 int strv_split_and_extend(char ***t, const char *s, const char *separators, bool filter_duplicates);
113
114 int strv_split_newlines_full(char ***ret, const char *s, ExtractFlags flags);
115 char** strv_split_newlines(const char *s);
116
117 /* Given a string containing white-space separated tuples of words themselves separated by ':',
118 * returns a vector of strings. If the second element in a tuple is missing, the corresponding
119 * string in the vector is an empty string. */
120 int strv_split_colon_pairs(char ***t, const char *s);
121
122 char* strv_join_full(char * const *l, const char *separator, const char *prefix, bool escape_separator);
123 static inline char *strv_join(char * const *l, const char *separator) {
124 return strv_join_full(l, separator, NULL, false);
125 }
126
127 bool strv_overlap(char * const *a, char * const *b) _pure_;
128
129 #define _STRV_FOREACH_BACKWARDS(s, l, h, i) \
130 for (typeof(*(l)) *s, *h = (l), *i = ({ \
131 size_t _len = strv_length(h); \
132 _len > 0 ? h + _len - 1 : NULL; \
133 }); \
134 (s = i); \
135 i = PTR_SUB1(i, h))
136
137 #define STRV_FOREACH_BACKWARDS(s, l) \
138 _STRV_FOREACH_BACKWARDS(s, l, UNIQ_T(h, UNIQ), UNIQ_T(i, UNIQ))
139
140 #define _STRV_FOREACH_PAIR(x, y, l, i) \
141 for (typeof(*(l)) *x, *y, *i = (l); \
142 i && *(x = i) && *(y = i + 1); \
143 i += 2)
144
145 #define STRV_FOREACH_PAIR(x, y, l) \
146 _STRV_FOREACH_PAIR(x, y, l, UNIQ_T(i, UNIQ))
147
148 char** strv_sort(char **l);
149 char** strv_sort_uniq(char **l);
150 void strv_print_full(char * const *l, const char *prefix);
151 static inline void strv_print(char * const *l) {
152 strv_print_full(l, NULL);
153 }
154
155 char* startswith_strv_internal(const char *s, char * const *l);
156 #define startswith_strv(s, l) const_generic(s, startswith_strv_internal(s, l))
157
158 #define STARTSWITH_SET(p, ...) \
159 startswith_strv(p, STRV_MAKE(__VA_ARGS__))
160
161 char* endswith_strv_internal(const char *s, char * const *l);
162 #define endswith_strv(s, l) const_generic(s, endswith_strv_internal(s, l))
163
164 #define ENDSWITH_SET(p, ...) \
165 endswith_strv(p, STRV_MAKE(__VA_ARGS__))
166
167 #define STR_IN_SET(x, ...) strv_contains(STRV_MAKE(__VA_ARGS__), x)
168 #define STRPTR_IN_SET(x, ...) \
169 ({ \
170 const char* _x = (x); \
171 _x && strv_contains(STRV_MAKE(__VA_ARGS__), _x); \
172 })
173
174 #define STRCASE_IN_SET(x, ...) strv_contains_case(STRV_MAKE(__VA_ARGS__), x)
175 #define STRCASEPTR_IN_SET(x, ...) \
176 ({ \
177 const char* _x = (x); \
178 _x && strv_contains_case(STRV_MAKE(__VA_ARGS__), _x); \
179 })
180
181 #define _FOREACH_STRING(uniq, x, y, ...) \
182 for (const char *x, * const*UNIQ_T(l, uniq) = STRV_MAKE_CONST(({ x = y; }), ##__VA_ARGS__); \
183 x; \
184 x = *(++UNIQ_T(l, uniq)))
185
186 #define FOREACH_STRING(x, y, ...) \
187 _FOREACH_STRING(UNIQ, x, y, ##__VA_ARGS__)
188
189 char** strv_reverse(char **l);
190 char** strv_shell_escape(char **l, const char *bad);
191
192 bool strv_fnmatch_full(char* const* patterns, const char *s, int flags, size_t *ret_matched_pos);
193 static inline bool strv_fnmatch(char* const* patterns, const char *s) {
194 return strv_fnmatch_full(patterns, s, 0, NULL);
195 }
196 static inline bool strv_fnmatch_or_empty(char* const* patterns, const char *s, int flags) {
197 assert(s);
198 return strv_isempty(patterns) ||
199 strv_fnmatch_full(patterns, s, flags, NULL);
200 }
201
202 char** strv_skip(char **l, size_t n);
203
204 int strv_extend_n(char ***l, const char *value, size_t n);
205
206 int strv_extend_assignment(char ***l, const char *lhs, const char *rhs);
207
208 int fputstrv(FILE *f, char * const *l, const char *separator, bool *space);
209
210 #define strv_free_and_replace(a, b) \
211 free_and_replace_full(a, b, strv_free)
212
213 void string_strv_hashmap_remove(Hashmap *h, const char *key, const char *value);
214 void string_strv_ordered_hashmap_remove(OrderedHashmap *h, const char *key, const char *value);
215 int string_strv_hashmap_put(Hashmap **h, const char *key, const char *value);
216 int string_strv_ordered_hashmap_put(OrderedHashmap **h, const char *key, const char *value);
217
218 int strv_rebreak_lines(char **l, size_t width, char ***ret);
219
220 char** strv_filter_prefix(char * const *l, const char *prefix);
221
222 /* whenever we need to initialize something with a constant non-NULL, but empty strv, we can use this shared
223 * one */
224 extern const char* const strv_empty[1];
225
226 #define STRV_EMPTY ((char**) strv_empty)