]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
07630cea LP |
2 | #pragma once |
3 | ||
07630cea | 4 | #include <stdbool.h> |
11c3a366 | 5 | #include <stddef.h> |
07630cea LP |
6 | #include <string.h> |
7 | ||
6e9417f5 | 8 | #include "alloc-util.h" |
07630cea LP |
9 | #include "macro.h" |
10 | ||
b11d6a7b LP |
11 | /* What is interpreted as whitespace? */ |
12 | #define WHITESPACE " \t\n\r" | |
13 | #define NEWLINE "\n\r" | |
14 | #define QUOTES "\"\'" | |
15 | #define COMMENTS "#;" | |
16 | #define GLOB_CHARS "*?[" | |
17 | #define DIGITS "0123456789" | |
18 | #define LOWERCASE_LETTERS "abcdefghijklmnopqrstuvwxyz" | |
19 | #define UPPERCASE_LETTERS "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
20 | #define LETTERS LOWERCASE_LETTERS UPPERCASE_LETTERS | |
21 | #define ALPHANUMERICAL LETTERS DIGITS | |
1a7906ae | 22 | #define HEXDIGITS DIGITS "abcdefABCDEF" |
b11d6a7b | 23 | |
07630cea LP |
24 | #define streq(a,b) (strcmp((a),(b)) == 0) |
25 | #define strneq(a, b, n) (strncmp((a), (b), (n)) == 0) | |
26 | #define strcaseeq(a,b) (strcasecmp((a),(b)) == 0) | |
27 | #define strncaseeq(a, b, n) (strncasecmp((a), (b), (n)) == 0) | |
28 | ||
29 | int strcmp_ptr(const char *a, const char *b) _pure_; | |
6cf3011c | 30 | int strcasecmp_ptr(const char *a, const char *b) _pure_; |
07630cea LP |
31 | |
32 | static inline bool streq_ptr(const char *a, const char *b) { | |
33 | return strcmp_ptr(a, b) == 0; | |
34 | } | |
35 | ||
36 | static inline const char* strempty(const char *s) { | |
ad5d4b17 | 37 | return s ?: ""; |
07630cea LP |
38 | } |
39 | ||
40 | static inline const char* strnull(const char *s) { | |
ad5d4b17 | 41 | return s ?: "(null)"; |
07630cea LP |
42 | } |
43 | ||
44 | static inline const char *strna(const char *s) { | |
ad5d4b17 | 45 | return s ?: "n/a"; |
07630cea LP |
46 | } |
47 | ||
55fced5a ZJS |
48 | static inline const char* yes_no(bool b) { |
49 | return b ? "yes" : "no"; | |
50 | } | |
51 | ||
52 | static inline const char* true_false(bool b) { | |
53 | return b ? "true" : "false"; | |
54 | } | |
55 | ||
56 | static inline const char* one_zero(bool b) { | |
57 | return b ? "1" : "0"; | |
58 | } | |
59 | ||
60 | static inline const char* enable_disable(bool b) { | |
61 | return b ? "enable" : "disable"; | |
62 | } | |
63 | ||
07630cea LP |
64 | static inline bool isempty(const char *p) { |
65 | return !p || !p[0]; | |
66 | } | |
67 | ||
3c6f7c34 LP |
68 | static inline const char *empty_to_null(const char *p) { |
69 | return isempty(p) ? NULL : p; | |
70 | } | |
71 | ||
c5984fe1 | 72 | static inline const char *empty_to_dash(const char *str) { |
07b0b339 SK |
73 | return isempty(str) ? "-" : str; |
74 | } | |
75 | ||
e7b88b7b LP |
76 | static inline bool empty_or_dash(const char *str) { |
77 | return !str || | |
78 | str[0] == 0 || | |
79 | (str[0] == '-' && str[1] == 0); | |
80 | } | |
81 | ||
dc90e0fa LP |
82 | static inline const char *empty_or_dash_to_null(const char *p) { |
83 | return empty_or_dash(p) ? NULL : p; | |
84 | } | |
85 | ||
07630cea LP |
86 | static inline char *startswith(const char *s, const char *prefix) { |
87 | size_t l; | |
88 | ||
89 | l = strlen(prefix); | |
90 | if (strncmp(s, prefix, l) == 0) | |
91 | return (char*) s + l; | |
92 | ||
93 | return NULL; | |
94 | } | |
95 | ||
96 | static inline char *startswith_no_case(const char *s, const char *prefix) { | |
97 | size_t l; | |
98 | ||
99 | l = strlen(prefix); | |
100 | if (strncasecmp(s, prefix, l) == 0) | |
101 | return (char*) s + l; | |
102 | ||
103 | return NULL; | |
104 | } | |
105 | ||
106 | char *endswith(const char *s, const char *postfix) _pure_; | |
107 | char *endswith_no_case(const char *s, const char *postfix) _pure_; | |
108 | ||
109 | char *first_word(const char *s, const char *word) _pure_; | |
110 | ||
8059aa9c YW |
111 | typedef enum SplitFlags { |
112 | SPLIT_QUOTES = 0x01 << 0, | |
113 | SPLIT_RELAX = 0x01 << 1, | |
114 | } SplitFlags; | |
115 | ||
82aa14e2 | 116 | /* Smelly. Do not use this anymore. Use extract_first_word() instead! */ |
8059aa9c | 117 | const char* split(const char **state, size_t *l, const char *separator, SplitFlags flags); |
07630cea | 118 | |
82aa14e2 | 119 | /* Similar, don't use this anymore */ |
07630cea | 120 | #define FOREACH_WORD(word, length, s, state) \ |
8059aa9c | 121 | _FOREACH_WORD(word, length, s, WHITESPACE, 0, state) |
07630cea LP |
122 | |
123 | #define FOREACH_WORD_SEPARATOR(word, length, s, separator, state) \ | |
8059aa9c | 124 | _FOREACH_WORD(word, length, s, separator, 0, state) |
07630cea | 125 | |
8059aa9c YW |
126 | #define _FOREACH_WORD(word, length, s, separator, flags, state) \ |
127 | for ((state) = (s), (word) = split(&(state), &(length), (separator), (flags)); (word); (word) = split(&(state), &(length), (separator), (flags))) | |
07630cea | 128 | |
07630cea LP |
129 | char *strnappend(const char *s, const char *suffix, size_t length); |
130 | ||
605405c6 ZJS |
131 | char *strjoin_real(const char *x, ...) _sentinel_; |
132 | #define strjoin(a, ...) strjoin_real((a), __VA_ARGS__, NULL) | |
07630cea LP |
133 | |
134 | #define strjoina(a, ...) \ | |
135 | ({ \ | |
136 | const char *_appendees_[] = { a, __VA_ARGS__ }; \ | |
137 | char *_d_, *_p_; \ | |
35207e25 | 138 | size_t _len_ = 0; \ |
da6053d0 | 139 | size_t _i_; \ |
07630cea LP |
140 | for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \ |
141 | _len_ += strlen(_appendees_[_i_]); \ | |
6e9417f5 | 142 | _p_ = _d_ = newa(char, _len_ + 1); \ |
07630cea LP |
143 | for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \ |
144 | _p_ = stpcpy(_p_, _appendees_[_i_]); \ | |
145 | *_p_ = 0; \ | |
146 | _d_; \ | |
147 | }) | |
148 | ||
149 | char *strstrip(char *s); | |
150 | char *delete_chars(char *s, const char *bad); | |
7546145e | 151 | char *delete_trailing_chars(char *s, const char *bad); |
07630cea LP |
152 | char *truncate_nl(char *s); |
153 | ||
7546145e LP |
154 | static inline char *skip_leading_chars(const char *s, const char *bad) { |
155 | ||
156 | if (!s) | |
157 | return NULL; | |
158 | ||
159 | if (!bad) | |
160 | bad = WHITESPACE; | |
161 | ||
162 | return (char*) s + strspn(s, bad); | |
163 | } | |
164 | ||
b577e3d5 LP |
165 | char ascii_tolower(char x); |
166 | char *ascii_strlower(char *s); | |
167 | char *ascii_strlower_n(char *s, size_t n); | |
07630cea | 168 | |
846b8fc3 LP |
169 | char ascii_toupper(char x); |
170 | char *ascii_strupper(char *s); | |
171 | ||
522d85ae | 172 | int ascii_strcasecmp_n(const char *a, const char *b, size_t n); |
c1749834 | 173 | int ascii_strcasecmp_nn(const char *a, size_t n, const char *b, size_t m); |
522d85ae | 174 | |
07630cea LP |
175 | bool chars_intersect(const char *a, const char *b) _pure_; |
176 | ||
177 | static inline bool _pure_ in_charset(const char *s, const char* charset) { | |
178 | assert(s); | |
179 | assert(charset); | |
180 | return s[strspn(s, charset)] == '\0'; | |
181 | } | |
182 | ||
183 | bool string_has_cc(const char *p, const char *ok) _pure_; | |
184 | ||
185 | char *ellipsize_mem(const char *s, size_t old_length_bytes, size_t new_length_columns, unsigned percent); | |
ae03775f ZJS |
186 | static inline char *ellipsize(const char *s, size_t length, unsigned percent) { |
187 | return ellipsize_mem(s, strlen(s), length, percent); | |
188 | } | |
189 | ||
8409f688 | 190 | char *cellescape(char *buf, size_t len, const char *s); |
07630cea | 191 | |
cca24fc3 ZJS |
192 | /* This limit is arbitrary, enough to give some idea what the string contains */ |
193 | #define CELLESCAPE_DEFAULT_LENGTH 64 | |
194 | ||
07630cea LP |
195 | char* strshorten(char *s, size_t l); |
196 | ||
197 | char *strreplace(const char *text, const char *old_string, const char *new_string); | |
198 | ||
b4766d5f | 199 | char *strip_tab_ansi(char **ibuf, size_t *_isz, size_t highlight[2]); |
07630cea | 200 | |
bb8ad9ea LP |
201 | char *strextend_with_separator(char **x, const char *separator, ...) _sentinel_; |
202 | ||
203 | #define strextend(x, ...) strextend_with_separator(x, NULL, __VA_ARGS__) | |
07630cea LP |
204 | |
205 | char *strrep(const char *s, unsigned n); | |
206 | ||
207 | int split_pair(const char *s, const char *sep, char **l, char **r); | |
208 | ||
209 | int free_and_strdup(char **p, const char *s); | |
f1531db5 ZJS |
210 | static inline int free_and_strdup_warn(char **p, const char *s) { |
211 | if (free_and_strdup(p, s) < 0) | |
212 | return log_oom(); | |
213 | return 0; | |
214 | } | |
7f546026 | 215 | int free_and_strndup(char **p, const char *s, size_t l); |
07630cea | 216 | |
f3e2e81d | 217 | bool string_is_safe(const char *p) _pure_; |
7bf7ce28 LP |
218 | |
219 | static inline size_t strlen_ptr(const char *s) { | |
220 | if (!s) | |
221 | return 0; | |
222 | ||
223 | return strlen(s); | |
224 | } | |
9b8ff183 | 225 | |
6695c200 ZJS |
226 | DISABLE_WARNING_STRINGOP_TRUNCATION; |
227 | static inline void strncpy_exact(char *buf, const char *src, size_t buf_len) { | |
228 | strncpy(buf, src, buf_len); | |
229 | } | |
230 | REENABLE_WARNING; | |
231 | ||
9b8ff183 LP |
232 | /* Like startswith(), but operates on arbitrary memory blocks */ |
233 | static inline void *memory_startswith(const void *p, size_t sz, const char *token) { | |
234 | size_t n; | |
235 | ||
236 | assert(token); | |
237 | ||
238 | n = strlen(token); | |
239 | if (sz < n) | |
240 | return NULL; | |
241 | ||
242 | assert(p); | |
243 | ||
244 | if (memcmp(p, token, n) != 0) | |
245 | return NULL; | |
246 | ||
247 | return (uint8_t*) p + n; | |
248 | } | |
21224070 DP |
249 | |
250 | /* Like startswith_no_case(), but operates on arbitrary memory blocks. | |
251 | * It works only for ASCII strings. | |
252 | */ | |
253 | static inline void *memory_startswith_no_case(const void *p, size_t sz, const char *token) { | |
254 | size_t n, i; | |
255 | ||
256 | assert(token); | |
257 | ||
258 | n = strlen(token); | |
259 | if (sz < n) | |
260 | return NULL; | |
261 | ||
262 | assert(p); | |
263 | ||
264 | for (i = 0; i < n; i++) { | |
265 | if (ascii_tolower(((char *)p)[i]) != ascii_tolower(token[i])) | |
266 | return NULL; | |
267 | } | |
268 | ||
269 | return (uint8_t*) p + n; | |
270 | } | |
bc28751e ZJS |
271 | |
272 | static inline char* str_realloc(char **p) { | |
273 | /* Reallocate *p to actual size */ | |
274 | ||
275 | if (!*p) | |
276 | return NULL; | |
277 | ||
278 | char *t = realloc(*p, strlen(*p) + 1); | |
279 | if (!t) | |
280 | return NULL; | |
281 | ||
282 | return (*p = t); | |
283 | } | |
53caaffd LP |
284 | |
285 | char* string_erase(char *x); | |
8dd6491e LP |
286 | |
287 | int string_truncate_lines(const char *s, size_t n_lines, char **ret); | |
f6857fa6 | 288 | int string_extract_line(const char *s, size_t i, char **ret); |