]>
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 | ||
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_; | |
30 | ||
31 | static inline bool streq_ptr(const char *a, const char *b) { | |
32 | return strcmp_ptr(a, b) == 0; | |
33 | } | |
34 | ||
35 | static inline const char* strempty(const char *s) { | |
ad5d4b17 | 36 | return s ?: ""; |
07630cea LP |
37 | } |
38 | ||
39 | static inline const char* strnull(const char *s) { | |
ad5d4b17 | 40 | return s ?: "(null)"; |
07630cea LP |
41 | } |
42 | ||
43 | static inline const char *strna(const char *s) { | |
ad5d4b17 | 44 | return s ?: "n/a"; |
07630cea LP |
45 | } |
46 | ||
47 | static inline bool isempty(const char *p) { | |
48 | return !p || !p[0]; | |
49 | } | |
50 | ||
3c6f7c34 LP |
51 | static inline const char *empty_to_null(const char *p) { |
52 | return isempty(p) ? NULL : p; | |
53 | } | |
54 | ||
c5984fe1 | 55 | static inline const char *empty_to_dash(const char *str) { |
07b0b339 SK |
56 | return isempty(str) ? "-" : str; |
57 | } | |
58 | ||
07630cea LP |
59 | static inline char *startswith(const char *s, const char *prefix) { |
60 | size_t l; | |
61 | ||
62 | l = strlen(prefix); | |
63 | if (strncmp(s, prefix, l) == 0) | |
64 | return (char*) s + l; | |
65 | ||
66 | return NULL; | |
67 | } | |
68 | ||
69 | static inline char *startswith_no_case(const char *s, const char *prefix) { | |
70 | size_t l; | |
71 | ||
72 | l = strlen(prefix); | |
73 | if (strncasecmp(s, prefix, l) == 0) | |
74 | return (char*) s + l; | |
75 | ||
76 | return NULL; | |
77 | } | |
78 | ||
79 | char *endswith(const char *s, const char *postfix) _pure_; | |
80 | char *endswith_no_case(const char *s, const char *postfix) _pure_; | |
81 | ||
82 | char *first_word(const char *s, const char *word) _pure_; | |
83 | ||
84 | const char* split(const char **state, size_t *l, const char *separator, bool quoted); | |
85 | ||
86 | #define FOREACH_WORD(word, length, s, state) \ | |
87 | _FOREACH_WORD(word, length, s, WHITESPACE, false, state) | |
88 | ||
89 | #define FOREACH_WORD_SEPARATOR(word, length, s, separator, state) \ | |
90 | _FOREACH_WORD(word, length, s, separator, false, state) | |
91 | ||
07630cea LP |
92 | #define _FOREACH_WORD(word, length, s, separator, quoted, state) \ |
93 | for ((state) = (s), (word) = split(&(state), &(length), (separator), (quoted)); (word); (word) = split(&(state), &(length), (separator), (quoted))) | |
94 | ||
95 | char *strappend(const char *s, const char *suffix); | |
96 | char *strnappend(const char *s, const char *suffix, size_t length); | |
97 | ||
605405c6 ZJS |
98 | char *strjoin_real(const char *x, ...) _sentinel_; |
99 | #define strjoin(a, ...) strjoin_real((a), __VA_ARGS__, NULL) | |
07630cea LP |
100 | |
101 | #define strjoina(a, ...) \ | |
102 | ({ \ | |
103 | const char *_appendees_[] = { a, __VA_ARGS__ }; \ | |
104 | char *_d_, *_p_; \ | |
35207e25 | 105 | size_t _len_ = 0; \ |
da6053d0 | 106 | size_t _i_; \ |
07630cea LP |
107 | for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \ |
108 | _len_ += strlen(_appendees_[_i_]); \ | |
109 | _p_ = _d_ = alloca(_len_ + 1); \ | |
110 | for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \ | |
111 | _p_ = stpcpy(_p_, _appendees_[_i_]); \ | |
112 | *_p_ = 0; \ | |
113 | _d_; \ | |
114 | }) | |
115 | ||
116 | char *strstrip(char *s); | |
117 | char *delete_chars(char *s, const char *bad); | |
7546145e | 118 | char *delete_trailing_chars(char *s, const char *bad); |
07630cea LP |
119 | char *truncate_nl(char *s); |
120 | ||
7546145e LP |
121 | static inline char *skip_leading_chars(const char *s, const char *bad) { |
122 | ||
123 | if (!s) | |
124 | return NULL; | |
125 | ||
126 | if (!bad) | |
127 | bad = WHITESPACE; | |
128 | ||
129 | return (char*) s + strspn(s, bad); | |
130 | } | |
131 | ||
b577e3d5 LP |
132 | char ascii_tolower(char x); |
133 | char *ascii_strlower(char *s); | |
134 | char *ascii_strlower_n(char *s, size_t n); | |
07630cea | 135 | |
846b8fc3 LP |
136 | char ascii_toupper(char x); |
137 | char *ascii_strupper(char *s); | |
138 | ||
522d85ae | 139 | int ascii_strcasecmp_n(const char *a, const char *b, size_t n); |
c1749834 | 140 | int ascii_strcasecmp_nn(const char *a, size_t n, const char *b, size_t m); |
522d85ae | 141 | |
07630cea LP |
142 | bool chars_intersect(const char *a, const char *b) _pure_; |
143 | ||
144 | static inline bool _pure_ in_charset(const char *s, const char* charset) { | |
145 | assert(s); | |
146 | assert(charset); | |
147 | return s[strspn(s, charset)] == '\0'; | |
148 | } | |
149 | ||
150 | bool string_has_cc(const char *p, const char *ok) _pure_; | |
151 | ||
152 | char *ellipsize_mem(const char *s, size_t old_length_bytes, size_t new_length_columns, unsigned percent); | |
ae03775f ZJS |
153 | static inline char *ellipsize(const char *s, size_t length, unsigned percent) { |
154 | return ellipsize_mem(s, strlen(s), length, percent); | |
155 | } | |
156 | ||
8409f688 | 157 | char *cellescape(char *buf, size_t len, const char *s); |
07630cea | 158 | |
cca24fc3 ZJS |
159 | /* This limit is arbitrary, enough to give some idea what the string contains */ |
160 | #define CELLESCAPE_DEFAULT_LENGTH 64 | |
161 | ||
2d5dece8 | 162 | bool nulstr_contains(const char *nulstr, const char *needle); |
07630cea LP |
163 | |
164 | char* strshorten(char *s, size_t l); | |
165 | ||
166 | char *strreplace(const char *text, const char *old_string, const char *new_string); | |
167 | ||
b4766d5f | 168 | char *strip_tab_ansi(char **ibuf, size_t *_isz, size_t highlight[2]); |
07630cea | 169 | |
bb8ad9ea LP |
170 | char *strextend_with_separator(char **x, const char *separator, ...) _sentinel_; |
171 | ||
172 | #define strextend(x, ...) strextend_with_separator(x, NULL, __VA_ARGS__) | |
07630cea LP |
173 | |
174 | char *strrep(const char *s, unsigned n); | |
175 | ||
176 | int split_pair(const char *s, const char *sep, char **l, char **r); | |
177 | ||
178 | int free_and_strdup(char **p, const char *s); | |
179 | ||
180 | /* Normal memmem() requires haystack to be nonnull, which is annoying for zero-length buffers */ | |
181 | static inline void *memmem_safe(const void *haystack, size_t haystacklen, const void *needle, size_t needlelen) { | |
182 | ||
183 | if (needlelen <= 0) | |
184 | return (void*) haystack; | |
185 | ||
186 | if (haystacklen < needlelen) | |
187 | return NULL; | |
188 | ||
189 | assert(haystack); | |
190 | assert(needle); | |
191 | ||
192 | return memmem(haystack, haystacklen, needle, needlelen); | |
193 | } | |
194 | ||
4b9545f1 | 195 | #if !HAVE_EXPLICIT_BZERO |
2d26d8e0 ZJS |
196 | void explicit_bzero(void *p, size_t l); |
197 | #endif | |
198 | ||
9fe4ea21 | 199 | char *string_erase(char *x); |
07630cea LP |
200 | |
201 | char *string_free_erase(char *s); | |
202 | DEFINE_TRIVIAL_CLEANUP_FUNC(char *, string_free_erase); | |
203 | #define _cleanup_string_free_erase_ _cleanup_(string_free_erasep) | |
f3e2e81d LP |
204 | |
205 | bool string_is_safe(const char *p) _pure_; | |
7bf7ce28 LP |
206 | |
207 | static inline size_t strlen_ptr(const char *s) { | |
208 | if (!s) | |
209 | return 0; | |
210 | ||
211 | return strlen(s); | |
212 | } | |
9b8ff183 LP |
213 | |
214 | /* Like startswith(), but operates on arbitrary memory blocks */ | |
215 | static inline void *memory_startswith(const void *p, size_t sz, const char *token) { | |
216 | size_t n; | |
217 | ||
218 | assert(token); | |
219 | ||
220 | n = strlen(token); | |
221 | if (sz < n) | |
222 | return NULL; | |
223 | ||
224 | assert(p); | |
225 | ||
226 | if (memcmp(p, token, n) != 0) | |
227 | return NULL; | |
228 | ||
229 | return (uint8_t*) p + n; | |
230 | } |