]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
07630cea LP |
2 | #pragma once |
3 | ||
4 | /*** | |
07630cea | 5 | Copyright 2010 Lennart Poettering |
07630cea LP |
6 | ***/ |
7 | ||
11c3a366 | 8 | #include <alloca.h> |
07630cea | 9 | #include <stdbool.h> |
11c3a366 | 10 | #include <stddef.h> |
07630cea LP |
11 | #include <string.h> |
12 | ||
13 | #include "macro.h" | |
14 | ||
b11d6a7b LP |
15 | /* What is interpreted as whitespace? */ |
16 | #define WHITESPACE " \t\n\r" | |
17 | #define NEWLINE "\n\r" | |
18 | #define QUOTES "\"\'" | |
19 | #define COMMENTS "#;" | |
20 | #define GLOB_CHARS "*?[" | |
21 | #define DIGITS "0123456789" | |
22 | #define LOWERCASE_LETTERS "abcdefghijklmnopqrstuvwxyz" | |
23 | #define UPPERCASE_LETTERS "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
24 | #define LETTERS LOWERCASE_LETTERS UPPERCASE_LETTERS | |
25 | #define ALPHANUMERICAL LETTERS DIGITS | |
1a7906ae | 26 | #define HEXDIGITS DIGITS "abcdefABCDEF" |
b11d6a7b | 27 | |
07630cea LP |
28 | #define streq(a,b) (strcmp((a),(b)) == 0) |
29 | #define strneq(a, b, n) (strncmp((a), (b), (n)) == 0) | |
30 | #define strcaseeq(a,b) (strcasecmp((a),(b)) == 0) | |
31 | #define strncaseeq(a, b, n) (strncasecmp((a), (b), (n)) == 0) | |
32 | ||
33 | int strcmp_ptr(const char *a, const char *b) _pure_; | |
34 | ||
35 | static inline bool streq_ptr(const char *a, const char *b) { | |
36 | return strcmp_ptr(a, b) == 0; | |
37 | } | |
38 | ||
39 | static inline const char* strempty(const char *s) { | |
ad5d4b17 | 40 | return s ?: ""; |
07630cea LP |
41 | } |
42 | ||
43 | static inline const char* strnull(const char *s) { | |
ad5d4b17 | 44 | return s ?: "(null)"; |
07630cea LP |
45 | } |
46 | ||
47 | static inline const char *strna(const char *s) { | |
ad5d4b17 | 48 | return s ?: "n/a"; |
07630cea LP |
49 | } |
50 | ||
51 | static inline bool isempty(const char *p) { | |
52 | return !p || !p[0]; | |
53 | } | |
54 | ||
3c6f7c34 LP |
55 | static inline const char *empty_to_null(const char *p) { |
56 | return isempty(p) ? NULL : p; | |
57 | } | |
58 | ||
c5984fe1 | 59 | static inline const char *empty_to_dash(const char *str) { |
07b0b339 SK |
60 | return isempty(str) ? "-" : str; |
61 | } | |
62 | ||
07630cea LP |
63 | static inline char *startswith(const char *s, const char *prefix) { |
64 | size_t l; | |
65 | ||
66 | l = strlen(prefix); | |
67 | if (strncmp(s, prefix, l) == 0) | |
68 | return (char*) s + l; | |
69 | ||
70 | return NULL; | |
71 | } | |
72 | ||
73 | static inline char *startswith_no_case(const char *s, const char *prefix) { | |
74 | size_t l; | |
75 | ||
76 | l = strlen(prefix); | |
77 | if (strncasecmp(s, prefix, l) == 0) | |
78 | return (char*) s + l; | |
79 | ||
80 | return NULL; | |
81 | } | |
82 | ||
83 | char *endswith(const char *s, const char *postfix) _pure_; | |
84 | char *endswith_no_case(const char *s, const char *postfix) _pure_; | |
85 | ||
86 | char *first_word(const char *s, const char *word) _pure_; | |
87 | ||
88 | const char* split(const char **state, size_t *l, const char *separator, bool quoted); | |
89 | ||
90 | #define FOREACH_WORD(word, length, s, state) \ | |
91 | _FOREACH_WORD(word, length, s, WHITESPACE, false, state) | |
92 | ||
93 | #define FOREACH_WORD_SEPARATOR(word, length, s, separator, state) \ | |
94 | _FOREACH_WORD(word, length, s, separator, false, state) | |
95 | ||
07630cea LP |
96 | #define _FOREACH_WORD(word, length, s, separator, quoted, state) \ |
97 | for ((state) = (s), (word) = split(&(state), &(length), (separator), (quoted)); (word); (word) = split(&(state), &(length), (separator), (quoted))) | |
98 | ||
99 | char *strappend(const char *s, const char *suffix); | |
100 | char *strnappend(const char *s, const char *suffix, size_t length); | |
101 | ||
605405c6 ZJS |
102 | char *strjoin_real(const char *x, ...) _sentinel_; |
103 | #define strjoin(a, ...) strjoin_real((a), __VA_ARGS__, NULL) | |
07630cea LP |
104 | |
105 | #define strjoina(a, ...) \ | |
106 | ({ \ | |
107 | const char *_appendees_[] = { a, __VA_ARGS__ }; \ | |
108 | char *_d_, *_p_; \ | |
35207e25 | 109 | size_t _len_ = 0; \ |
da6053d0 | 110 | size_t _i_; \ |
07630cea LP |
111 | for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \ |
112 | _len_ += strlen(_appendees_[_i_]); \ | |
113 | _p_ = _d_ = alloca(_len_ + 1); \ | |
114 | for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \ | |
115 | _p_ = stpcpy(_p_, _appendees_[_i_]); \ | |
116 | *_p_ = 0; \ | |
117 | _d_; \ | |
118 | }) | |
119 | ||
120 | char *strstrip(char *s); | |
121 | char *delete_chars(char *s, const char *bad); | |
7546145e | 122 | char *delete_trailing_chars(char *s, const char *bad); |
07630cea LP |
123 | char *truncate_nl(char *s); |
124 | ||
7546145e LP |
125 | static inline char *skip_leading_chars(const char *s, const char *bad) { |
126 | ||
127 | if (!s) | |
128 | return NULL; | |
129 | ||
130 | if (!bad) | |
131 | bad = WHITESPACE; | |
132 | ||
133 | return (char*) s + strspn(s, bad); | |
134 | } | |
135 | ||
b577e3d5 LP |
136 | char ascii_tolower(char x); |
137 | char *ascii_strlower(char *s); | |
138 | char *ascii_strlower_n(char *s, size_t n); | |
07630cea | 139 | |
846b8fc3 LP |
140 | char ascii_toupper(char x); |
141 | char *ascii_strupper(char *s); | |
142 | ||
522d85ae | 143 | int ascii_strcasecmp_n(const char *a, const char *b, size_t n); |
c1749834 | 144 | int ascii_strcasecmp_nn(const char *a, size_t n, const char *b, size_t m); |
522d85ae | 145 | |
07630cea LP |
146 | bool chars_intersect(const char *a, const char *b) _pure_; |
147 | ||
148 | static inline bool _pure_ in_charset(const char *s, const char* charset) { | |
149 | assert(s); | |
150 | assert(charset); | |
151 | return s[strspn(s, charset)] == '\0'; | |
152 | } | |
153 | ||
154 | bool string_has_cc(const char *p, const char *ok) _pure_; | |
155 | ||
156 | char *ellipsize_mem(const char *s, size_t old_length_bytes, size_t new_length_columns, unsigned percent); | |
ae03775f ZJS |
157 | static inline char *ellipsize(const char *s, size_t length, unsigned percent) { |
158 | return ellipsize_mem(s, strlen(s), length, percent); | |
159 | } | |
160 | ||
8409f688 | 161 | char *cellescape(char *buf, size_t len, const char *s); |
07630cea | 162 | |
cca24fc3 ZJS |
163 | /* This limit is arbitrary, enough to give some idea what the string contains */ |
164 | #define CELLESCAPE_DEFAULT_LENGTH 64 | |
165 | ||
2d5dece8 | 166 | bool nulstr_contains(const char *nulstr, const char *needle); |
07630cea LP |
167 | |
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); | |
183 | ||
184 | /* Normal memmem() requires haystack to be nonnull, which is annoying for zero-length buffers */ | |
185 | static inline void *memmem_safe(const void *haystack, size_t haystacklen, const void *needle, size_t needlelen) { | |
186 | ||
187 | if (needlelen <= 0) | |
188 | return (void*) haystack; | |
189 | ||
190 | if (haystacklen < needlelen) | |
191 | return NULL; | |
192 | ||
193 | assert(haystack); | |
194 | assert(needle); | |
195 | ||
196 | return memmem(haystack, haystacklen, needle, needlelen); | |
197 | } | |
198 | ||
4b9545f1 | 199 | #if !HAVE_EXPLICIT_BZERO |
2d26d8e0 ZJS |
200 | void explicit_bzero(void *p, size_t l); |
201 | #endif | |
202 | ||
9fe4ea21 | 203 | char *string_erase(char *x); |
07630cea LP |
204 | |
205 | char *string_free_erase(char *s); | |
206 | DEFINE_TRIVIAL_CLEANUP_FUNC(char *, string_free_erase); | |
207 | #define _cleanup_string_free_erase_ _cleanup_(string_free_erasep) | |
f3e2e81d LP |
208 | |
209 | bool string_is_safe(const char *p) _pure_; | |
7bf7ce28 LP |
210 | |
211 | static inline size_t strlen_ptr(const char *s) { | |
212 | if (!s) | |
213 | return 0; | |
214 | ||
215 | return strlen(s); | |
216 | } | |
9b8ff183 LP |
217 | |
218 | /* Like startswith(), but operates on arbitrary memory blocks */ | |
219 | static inline void *memory_startswith(const void *p, size_t sz, const char *token) { | |
220 | size_t n; | |
221 | ||
222 | assert(token); | |
223 | ||
224 | n = strlen(token); | |
225 | if (sz < n) | |
226 | return NULL; | |
227 | ||
228 | assert(p); | |
229 | ||
230 | if (memcmp(p, token, n) != 0) | |
231 | return NULL; | |
232 | ||
233 | return (uint8_t*) p + n; | |
234 | } |