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