]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
07630cea LP |
2 | #pragma once |
3 | ||
07630cea LP |
4 | #include <string.h> |
5 | ||
efc529cf | 6 | #include "alloc-util.h" |
0c15577a DDM |
7 | #include "forward.h" |
8 | #include "string-util-fundamental.h" /* IWYU pragma: export */ | |
07630cea | 9 | |
4eb0c875 ZJS |
10 | static inline char* strstr_ptr(const char *haystack, const char *needle) { |
11 | if (!haystack || !needle) | |
12 | return NULL; | |
13 | return strstr(haystack, needle); | |
14 | } | |
15 | ||
ff3f2953 | 16 | static inline char* strstrafter(const char *haystack, const char *needle) { |
d791013f LP |
17 | char *p; |
18 | ||
19 | /* Returns NULL if not found, or pointer to first character after needle if found */ | |
20 | ||
21 | p = strstr_ptr(haystack, needle); | |
22 | if (!p) | |
23 | return NULL; | |
24 | ||
25 | return p + strlen(needle); | |
26 | } | |
27 | ||
07630cea | 28 | static inline const char* strnull(const char *s) { |
ad5d4b17 | 29 | return s ?: "(null)"; |
07630cea LP |
30 | } |
31 | ||
bfd5a068 | 32 | static inline const char* strna(const char *s) { |
ad5d4b17 | 33 | return s ?: "n/a"; |
07630cea LP |
34 | } |
35 | ||
55fced5a ZJS |
36 | static inline const char* true_false(bool b) { |
37 | return b ? "true" : "false"; | |
38 | } | |
39 | ||
fe37e5a5 ZJS |
40 | static inline const char* plus_minus(bool b) { |
41 | return b ? "+" : "-"; | |
42 | } | |
43 | ||
55fced5a ZJS |
44 | static inline const char* one_zero(bool b) { |
45 | return b ? "1" : "0"; | |
46 | } | |
47 | ||
48 | static inline const char* enable_disable(bool b) { | |
49 | return b ? "enable" : "disable"; | |
50 | } | |
51 | ||
4ed9e261 LP |
52 | static inline const char* enabled_disabled(bool b) { |
53 | return b ? "enabled" : "disabled"; | |
54 | } | |
55 | ||
ef2409cb LP |
56 | /* This macro's return pointer will have the "const" qualifier set or unset the same way as the input |
57 | * pointer. */ | |
58 | #define empty_to_null(p) \ | |
59 | ({ \ | |
60 | const char *_p = (p); \ | |
61 | (typeof(p)) (isempty(_p) ? NULL : _p); \ | |
62 | }) | |
3c6f7c34 | 63 | |
bfd5a068 | 64 | static inline const char* empty_to_na(const char *p) { |
edee65a6 ZJS |
65 | return isempty(p) ? "n/a" : p; |
66 | } | |
67 | ||
bfd5a068 | 68 | static inline const char* empty_to_dash(const char *str) { |
07b0b339 SK |
69 | return isempty(str) ? "-" : str; |
70 | } | |
71 | ||
e7b88b7b LP |
72 | static inline bool empty_or_dash(const char *str) { |
73 | return !str || | |
74 | str[0] == 0 || | |
75 | (str[0] == '-' && str[1] == 0); | |
76 | } | |
77 | ||
bfd5a068 | 78 | static inline const char* empty_or_dash_to_null(const char *p) { |
dc90e0fa LP |
79 | return empty_or_dash(p) ? NULL : p; |
80 | } | |
ef2409cb LP |
81 | #define empty_or_dash_to_null(p) \ |
82 | ({ \ | |
83 | const char *_p = (p); \ | |
84 | (typeof(p)) (empty_or_dash(_p) ? NULL : _p); \ | |
85 | }) | |
dc90e0fa | 86 | |
ff3f2953 | 87 | char* first_word(const char *s, const char *word) _pure_; |
07630cea | 88 | |
6da2ea9f | 89 | char* strextendn(char **x, const char *s, size_t l) _nonnull_if_nonzero_(2, 3); |
b40694f5 | 90 | |
34467ffa | 91 | #define strjoin(a, ...) strextend_with_separator_internal(NULL, NULL, a, __VA_ARGS__, NULL) |
07630cea LP |
92 | |
93 | #define strjoina(a, ...) \ | |
94 | ({ \ | |
95 | const char *_appendees_[] = { a, __VA_ARGS__ }; \ | |
96 | char *_d_, *_p_; \ | |
9a485918 ZJS |
97 | size_t _len_ = 0; \ |
98 | size_t _i_; \ | |
07630cea LP |
99 | for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \ |
100 | _len_ += strlen(_appendees_[_i_]); \ | |
6e9417f5 | 101 | _p_ = _d_ = newa(char, _len_ + 1); \ |
07630cea LP |
102 | for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \ |
103 | _p_ = stpcpy(_p_, _appendees_[_i_]); \ | |
104 | *_p_ = 0; \ | |
105 | _d_; \ | |
106 | }) | |
107 | ||
ff3f2953 ZJS |
108 | char* strstrip(char *s); |
109 | char* delete_chars(char *s, const char *bad); | |
110 | char* delete_trailing_chars(char *s, const char *bad); | |
111 | char* truncate_nl_full(char *s, size_t *ret_len); | |
112 | static inline char* truncate_nl(char *s) { | |
61cecfa0 | 113 | return truncate_nl_full(s, NULL); |
114 | } | |
07630cea | 115 | |
ee0373cb | 116 | static inline char* skip_leading_chars(const char *s, const char *bad) { |
7546145e LP |
117 | if (!s) |
118 | return NULL; | |
119 | ||
120 | if (!bad) | |
121 | bad = WHITESPACE; | |
122 | ||
123 | return (char*) s + strspn(s, bad); | |
124 | } | |
125 | ||
180341e7 | 126 | char ascii_tolower(char x) _const_; |
ff3f2953 ZJS |
127 | char* ascii_strlower(char *s); |
128 | char* ascii_strlower_n(char *s, size_t n); | |
07630cea | 129 | |
180341e7 | 130 | char ascii_toupper(char x) _const_; |
ff3f2953 | 131 | char* ascii_strupper(char *s); |
846b8fc3 | 132 | |
522d85ae | 133 | int ascii_strcasecmp_n(const char *a, const char *b, size_t n); |
c1749834 | 134 | int ascii_strcasecmp_nn(const char *a, size_t n, const char *b, size_t m); |
522d85ae | 135 | |
07630cea LP |
136 | bool chars_intersect(const char *a, const char *b) _pure_; |
137 | ||
f0f044a4 | 138 | static inline bool _pure_ in_charset(const char *s, const char *charset) { |
07630cea LP |
139 | assert(s); |
140 | assert(charset); | |
141 | return s[strspn(s, charset)] == '\0'; | |
142 | } | |
143 | ||
6302d386 | 144 | static inline bool char_is_cc(char p) { |
3d56acef YW |
145 | /* char is unsigned on some architectures, e.g. aarch64. So, compiler may warn the condition |
146 | * p >= 0 is always true. See #19543. Hence, let's cast to unsigned before the comparison. Note | |
147 | * that the cast in the right hand side is redundant, as according to the C standard, compilers | |
148 | * automatically cast a signed value to unsigned when comparing with an unsigned variable. Just | |
149 | * for safety and readability. */ | |
150 | return (uint8_t) p < (uint8_t) ' ' || p == 127; | |
6302d386 | 151 | } |
07630cea LP |
152 | bool string_has_cc(const char *p, const char *ok) _pure_; |
153 | ||
ff3f2953 ZJS |
154 | char* ellipsize_mem(const char *s, size_t old_length_bytes, size_t new_length_columns, unsigned percent); |
155 | static inline char* ellipsize(const char *s, size_t length, unsigned percent) { | |
ae03775f ZJS |
156 | return ellipsize_mem(s, strlen(s), length, percent); |
157 | } | |
158 | ||
ff3f2953 | 159 | char* cellescape(char *buf, size_t len, const char *s); |
07630cea | 160 | |
cca24fc3 ZJS |
161 | /* This limit is arbitrary, enough to give some idea what the string contains */ |
162 | #define CELLESCAPE_DEFAULT_LENGTH 64 | |
163 | ||
07630cea LP |
164 | char* strshorten(char *s, size_t l); |
165 | ||
2812017c DDM |
166 | int strgrowpad0(char **s, size_t l); |
167 | ||
ff3f2953 | 168 | char* strreplace(const char *text, const char *old_string, const char *new_string); |
07630cea | 169 | |
ff3f2953 | 170 | char* strip_tab_ansi(char **ibuf, size_t *_isz, size_t highlight[2]); |
07630cea | 171 | |
ff3f2953 | 172 | char* strextend_with_separator_internal(char **x, const char *separator, ...) _sentinel_; |
c2bc710b LP |
173 | #define strextend_with_separator(x, separator, ...) strextend_with_separator_internal(x, separator, __VA_ARGS__, NULL) |
174 | #define strextend(x, ...) strextend_with_separator_internal(x, NULL, __VA_ARGS__, NULL) | |
07630cea | 175 | |
6b13ca8a YW |
176 | int strextendf_with_separator(char **x, const char *separator, const char *format, ...) _printf_(3,4); |
177 | #define strextendf(x, ...) strextendf_with_separator(x, NULL, __VA_ARGS__) | |
e9b88a6d | 178 | |
d9bf8150 MY |
179 | #define strprepend_with_separator(x, separator, ...) \ |
180 | ({ \ | |
181 | char **_p_ = ASSERT_PTR(x), *_s_; \ | |
182 | _s_ = strextend_with_separator_internal(NULL, (separator), __VA_ARGS__, empty_to_null(*_p_), NULL); \ | |
183 | if (_s_) { \ | |
184 | free(*_p_); \ | |
185 | *_p_ = _s_; \ | |
186 | } \ | |
187 | _s_; \ | |
188 | }) | |
189 | #define strprepend(x, ...) strprepend_with_separator(x, NULL, __VA_ARGS__) | |
190 | ||
ff3f2953 | 191 | char* strrep(const char *s, unsigned n); |
07630cea | 192 | |
34fb408f MY |
193 | #define strrepa(s, n) \ |
194 | ({ \ | |
195 | const char *_sss_ = (s); \ | |
196 | size_t _nnn_ = (n), _len_ = strlen(_sss_); \ | |
197 | assert_se(MUL_ASSIGN_SAFE(&_len_, _nnn_)); \ | |
198 | char *_d_, *_p_; \ | |
199 | _p_ = _d_ = newa(char, _len_ + 1); \ | |
200 | for (size_t _i_ = 0; _i_ < _nnn_; _i_++) \ | |
201 | _p_ = stpcpy(_p_, _sss_); \ | |
202 | *_p_ = 0; \ | |
203 | _d_; \ | |
00614746 MY |
204 | }) |
205 | ||
ac3f3026 | 206 | int split_pair(const char *s, const char *sep, char **ret_first, char **ret_second); |
07630cea LP |
207 | |
208 | int free_and_strdup(char **p, const char *s); | |
93a1f792 | 209 | int free_and_strdup_warn(char **p, const char *s); |
6da2ea9f | 210 | int free_and_strndup(char **p, const char *s, size_t l) _nonnull_if_nonzero_(2, 3); |
07630cea | 211 | |
892c5902 ZJS |
212 | int strdup_to_full(char **ret, const char *src); |
213 | static inline int strdup_to(char **ret, const char *src) { | |
214 | int r = strdup_to_full(ASSERT_PTR(ret), src); | |
215 | return r < 0 ? r : 0; /* Suppress return value of 1. */ | |
216 | } | |
217 | ||
f3e2e81d | 218 | bool string_is_safe(const char *p) _pure_; |
e4a08721 | 219 | bool string_is_safe_ascii(const char *p) _pure_; |
7bf7ce28 | 220 | |
6695c200 ZJS |
221 | DISABLE_WARNING_STRINGOP_TRUNCATION; |
222 | static inline void strncpy_exact(char *buf, const char *src, size_t buf_len) { | |
223 | strncpy(buf, src, buf_len); | |
224 | } | |
225 | REENABLE_WARNING; | |
226 | ||
21224070 DP |
227 | /* Like startswith_no_case(), but operates on arbitrary memory blocks. |
228 | * It works only for ASCII strings. | |
229 | */ | |
f0f044a4 | 230 | static inline void* memory_startswith_no_case(const void *p, size_t sz, const char *token) { |
21224070 DP |
231 | assert(token); |
232 | ||
9a485918 | 233 | size_t n = strlen(token); |
21224070 DP |
234 | if (sz < n) |
235 | return NULL; | |
236 | ||
237 | assert(p); | |
238 | ||
9a485918 | 239 | for (size_t i = 0; i < n; i++) |
21224070 DP |
240 | if (ascii_tolower(((char *)p)[i]) != ascii_tolower(token[i])) |
241 | return NULL; | |
21224070 DP |
242 | |
243 | return (uint8_t*) p + n; | |
244 | } | |
bc28751e | 245 | |
e4a08721 | 246 | char* str_realloc(char *p); |
53caaffd | 247 | char* string_erase(char *x); |
8dd6491e LP |
248 | |
249 | int string_truncate_lines(const char *s, size_t n_lines, char **ret); | |
f6857fa6 | 250 | int string_extract_line(const char *s, size_t i, char **ret); |
46bf625a | 251 | |
f8c70079 | 252 | int string_contains_word_strv(const char *string, const char *separators, char * const *words, const char **ret_word); |
46bf625a ZJS |
253 | static inline int string_contains_word(const char *string, const char *separators, const char *word) { |
254 | return string_contains_word_strv(string, separators, STRV_MAKE(word), NULL); | |
255 | } | |
8034b42c ADT |
256 | |
257 | bool streq_skip_trailing_chars(const char *s1, const char *s2, const char *ok); | |
072f5f9b | 258 | |
ff3f2953 | 259 | char* string_replace_char(char *str, char old_char, char new_char); |
146f4482 | 260 | |
7153213e LP |
261 | typedef enum MakeCStringMode { |
262 | MAKE_CSTRING_REFUSE_TRAILING_NUL, | |
263 | MAKE_CSTRING_ALLOW_TRAILING_NUL, | |
264 | MAKE_CSTRING_REQUIRE_TRAILING_NUL, | |
265 | _MAKE_CSTRING_MODE_MAX, | |
266 | _MAKE_CSTRING_MODE_INVALID = -1, | |
267 | } MakeCStringMode; | |
268 | ||
269 | int make_cstring(const char *s, size_t n, MakeCStringMode mode, char **ret); | |
270 | ||
180341e7 | 271 | size_t strspn_from_end(const char *str, const char *accept) _pure_; |
e8bec624 | 272 | |
ff3f2953 ZJS |
273 | char* strdupspn(const char *a, const char *accept); |
274 | char* strdupcspn(const char *a, const char *reject); | |
7b82d95f | 275 | |
02207b54 DDM |
276 | /* These are like strdupa()/strndupa(), but honour ALLOCA_MAX */ |
277 | #define strdupa_safe(s) \ | |
278 | ({ \ | |
279 | const char *_t = (s); \ | |
280 | (char*) memdupa_suffix0(_t, strlen(_t)); \ | |
281 | }) | |
282 | ||
283 | #define strndupa_safe(s, n) \ | |
284 | ({ \ | |
285 | const char *_t = (s); \ | |
286 | (char*) memdupa_suffix0(_t, strnlen(_t, n)); \ | |
287 | }) | |
288 | ||
ff3f2953 | 289 | char* find_line_startswith(const char *haystack, const char *needle); |
ba2d8107 AP |
290 | char* find_line(const char *haystack, const char *needle); |
291 | char* find_line_after(const char *haystack, const char *needle); | |
70cc7ed9 | 292 | |
180341e7 MY |
293 | bool version_is_valid(const char *s) _pure_; |
294 | bool version_is_valid_versionspec(const char *s) _pure_; | |
7ef5b0a4 LP |
295 | |
296 | ssize_t strlevenshtein(const char *x, const char *y); | |
6a20a9d2 | 297 | |
180341e7 | 298 | char* strrstr(const char *haystack, const char *needle) _pure_; |
f77f363c | 299 | |
180341e7 | 300 | size_t str_common_prefix(const char *a, const char *b) _pure_; |