]>
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 | ||
07630cea LP |
60 | static inline char *startswith(const char *s, const char *prefix) { |
61 | size_t l; | |
62 | ||
63 | l = strlen(prefix); | |
64 | if (strncmp(s, prefix, l) == 0) | |
65 | return (char*) s + l; | |
66 | ||
67 | return NULL; | |
68 | } | |
69 | ||
70 | static inline char *startswith_no_case(const char *s, const char *prefix) { | |
71 | size_t l; | |
72 | ||
73 | l = strlen(prefix); | |
74 | if (strncasecmp(s, prefix, l) == 0) | |
75 | return (char*) s + l; | |
76 | ||
77 | return NULL; | |
78 | } | |
79 | ||
80 | char *endswith(const char *s, const char *postfix) _pure_; | |
81 | char *endswith_no_case(const char *s, const char *postfix) _pure_; | |
82 | ||
83 | char *first_word(const char *s, const char *word) _pure_; | |
84 | ||
8059aa9c YW |
85 | typedef enum SplitFlags { |
86 | SPLIT_QUOTES = 0x01 << 0, | |
87 | SPLIT_RELAX = 0x01 << 1, | |
88 | } SplitFlags; | |
89 | ||
90 | const char* split(const char **state, size_t *l, const char *separator, SplitFlags flags); | |
07630cea LP |
91 | |
92 | #define FOREACH_WORD(word, length, s, state) \ | |
8059aa9c | 93 | _FOREACH_WORD(word, length, s, WHITESPACE, 0, state) |
07630cea LP |
94 | |
95 | #define FOREACH_WORD_SEPARATOR(word, length, s, separator, state) \ | |
8059aa9c | 96 | _FOREACH_WORD(word, length, s, separator, 0, state) |
07630cea | 97 | |
8059aa9c YW |
98 | #define _FOREACH_WORD(word, length, s, separator, flags, state) \ |
99 | for ((state) = (s), (word) = split(&(state), &(length), (separator), (flags)); (word); (word) = split(&(state), &(length), (separator), (flags))) | |
07630cea LP |
100 | |
101 | char *strappend(const char *s, const char *suffix); | |
102 | char *strnappend(const char *s, const char *suffix, size_t length); | |
103 | ||
605405c6 ZJS |
104 | char *strjoin_real(const char *x, ...) _sentinel_; |
105 | #define strjoin(a, ...) strjoin_real((a), __VA_ARGS__, NULL) | |
07630cea LP |
106 | |
107 | #define strjoina(a, ...) \ | |
108 | ({ \ | |
109 | const char *_appendees_[] = { a, __VA_ARGS__ }; \ | |
110 | char *_d_, *_p_; \ | |
35207e25 | 111 | size_t _len_ = 0; \ |
da6053d0 | 112 | size_t _i_; \ |
07630cea LP |
113 | for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \ |
114 | _len_ += strlen(_appendees_[_i_]); \ | |
6e9417f5 | 115 | _p_ = _d_ = newa(char, _len_ + 1); \ |
07630cea LP |
116 | for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \ |
117 | _p_ = stpcpy(_p_, _appendees_[_i_]); \ | |
118 | *_p_ = 0; \ | |
119 | _d_; \ | |
120 | }) | |
121 | ||
122 | char *strstrip(char *s); | |
123 | char *delete_chars(char *s, const char *bad); | |
7546145e | 124 | char *delete_trailing_chars(char *s, const char *bad); |
07630cea LP |
125 | char *truncate_nl(char *s); |
126 | ||
7546145e LP |
127 | static inline char *skip_leading_chars(const char *s, const char *bad) { |
128 | ||
129 | if (!s) | |
130 | return NULL; | |
131 | ||
132 | if (!bad) | |
133 | bad = WHITESPACE; | |
134 | ||
135 | return (char*) s + strspn(s, bad); | |
136 | } | |
137 | ||
b577e3d5 LP |
138 | char ascii_tolower(char x); |
139 | char *ascii_strlower(char *s); | |
140 | char *ascii_strlower_n(char *s, size_t n); | |
07630cea | 141 | |
846b8fc3 LP |
142 | char ascii_toupper(char x); |
143 | char *ascii_strupper(char *s); | |
144 | ||
522d85ae | 145 | int ascii_strcasecmp_n(const char *a, const char *b, size_t n); |
c1749834 | 146 | int ascii_strcasecmp_nn(const char *a, size_t n, const char *b, size_t m); |
522d85ae | 147 | |
07630cea LP |
148 | bool chars_intersect(const char *a, const char *b) _pure_; |
149 | ||
150 | static inline bool _pure_ in_charset(const char *s, const char* charset) { | |
151 | assert(s); | |
152 | assert(charset); | |
153 | return s[strspn(s, charset)] == '\0'; | |
154 | } | |
155 | ||
156 | bool string_has_cc(const char *p, const char *ok) _pure_; | |
157 | ||
158 | char *ellipsize_mem(const char *s, size_t old_length_bytes, size_t new_length_columns, unsigned percent); | |
ae03775f ZJS |
159 | static inline char *ellipsize(const char *s, size_t length, unsigned percent) { |
160 | return ellipsize_mem(s, strlen(s), length, percent); | |
161 | } | |
162 | ||
8409f688 | 163 | char *cellescape(char *buf, size_t len, const char *s); |
07630cea | 164 | |
cca24fc3 ZJS |
165 | /* This limit is arbitrary, enough to give some idea what the string contains */ |
166 | #define CELLESCAPE_DEFAULT_LENGTH 64 | |
167 | ||
07630cea LP |
168 | char* strshorten(char *s, size_t l); |
169 | ||
170 | char *strreplace(const char *text, const char *old_string, const char *new_string); | |
171 | ||
b4766d5f | 172 | char *strip_tab_ansi(char **ibuf, size_t *_isz, size_t highlight[2]); |
07630cea | 173 | |
bb8ad9ea LP |
174 | char *strextend_with_separator(char **x, const char *separator, ...) _sentinel_; |
175 | ||
176 | #define strextend(x, ...) strextend_with_separator(x, NULL, __VA_ARGS__) | |
07630cea LP |
177 | |
178 | char *strrep(const char *s, unsigned n); | |
179 | ||
180 | int split_pair(const char *s, const char *sep, char **l, char **r); | |
181 | ||
182 | int free_and_strdup(char **p, const char *s); | |
f1531db5 ZJS |
183 | static inline int free_and_strdup_warn(char **p, const char *s) { |
184 | if (free_and_strdup(p, s) < 0) | |
185 | return log_oom(); | |
186 | return 0; | |
187 | } | |
7f546026 | 188 | int free_and_strndup(char **p, const char *s, size_t l); |
07630cea | 189 | |
9fe4ea21 | 190 | char *string_erase(char *x); |
07630cea LP |
191 | |
192 | char *string_free_erase(char *s); | |
193 | DEFINE_TRIVIAL_CLEANUP_FUNC(char *, string_free_erase); | |
194 | #define _cleanup_string_free_erase_ _cleanup_(string_free_erasep) | |
f3e2e81d LP |
195 | |
196 | bool string_is_safe(const char *p) _pure_; | |
7bf7ce28 LP |
197 | |
198 | static inline size_t strlen_ptr(const char *s) { | |
199 | if (!s) | |
200 | return 0; | |
201 | ||
202 | return strlen(s); | |
203 | } | |
9b8ff183 LP |
204 | |
205 | /* Like startswith(), but operates on arbitrary memory blocks */ | |
206 | static inline void *memory_startswith(const void *p, size_t sz, const char *token) { | |
207 | size_t n; | |
208 | ||
209 | assert(token); | |
210 | ||
211 | n = strlen(token); | |
212 | if (sz < n) | |
213 | return NULL; | |
214 | ||
215 | assert(p); | |
216 | ||
217 | if (memcmp(p, token, n) != 0) | |
218 | return NULL; | |
219 | ||
220 | return (uint8_t*) p + n; | |
221 | } | |
21224070 DP |
222 | |
223 | /* Like startswith_no_case(), but operates on arbitrary memory blocks. | |
224 | * It works only for ASCII strings. | |
225 | */ | |
226 | static inline void *memory_startswith_no_case(const void *p, size_t sz, const char *token) { | |
227 | size_t n, i; | |
228 | ||
229 | assert(token); | |
230 | ||
231 | n = strlen(token); | |
232 | if (sz < n) | |
233 | return NULL; | |
234 | ||
235 | assert(p); | |
236 | ||
237 | for (i = 0; i < n; i++) { | |
238 | if (ascii_tolower(((char *)p)[i]) != ascii_tolower(token[i])) | |
239 | return NULL; | |
240 | } | |
241 | ||
242 | return (uint8_t*) p + n; | |
243 | } |