]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/string-util.h
ethtool: add several new link modes
[thirdparty/systemd.git] / src / basic / string-util.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 #pragma once
3
4 #include <stdbool.h>
5 #include <stddef.h>
6 #include <string.h>
7
8 #include "alloc-util.h"
9 #include "macro.h"
10
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
22 #define HEXDIGITS DIGITS "abcdefABCDEF"
23
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 int strcasecmp_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) {
37 return s ?: "";
38 }
39
40 static inline const char* strnull(const char *s) {
41 return s ?: "(null)";
42 }
43
44 static inline const char *strna(const char *s) {
45 return s ?: "n/a";
46 }
47
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
64 static inline bool isempty(const char *p) {
65 return !p || !p[0];
66 }
67
68 static inline const char *empty_to_null(const char *p) {
69 return isempty(p) ? NULL : p;
70 }
71
72 static inline const char *empty_to_dash(const char *str) {
73 return isempty(str) ? "-" : str;
74 }
75
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
82 static inline const char *empty_or_dash_to_null(const char *p) {
83 return empty_or_dash(p) ? NULL : p;
84 }
85
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
111 char *strnappend(const char *s, const char *suffix, size_t length);
112
113 char *strjoin_real(const char *x, ...) _sentinel_;
114 #define strjoin(a, ...) strjoin_real((a), __VA_ARGS__, NULL)
115
116 #define strjoina(a, ...) \
117 ({ \
118 const char *_appendees_[] = { a, __VA_ARGS__ }; \
119 char *_d_, *_p_; \
120 size_t _len_ = 0; \
121 size_t _i_; \
122 for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \
123 _len_ += strlen(_appendees_[_i_]); \
124 _p_ = _d_ = newa(char, _len_ + 1); \
125 for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \
126 _p_ = stpcpy(_p_, _appendees_[_i_]); \
127 *_p_ = 0; \
128 _d_; \
129 })
130
131 char *strstrip(char *s);
132 char *delete_chars(char *s, const char *bad);
133 char *delete_trailing_chars(char *s, const char *bad);
134 char *truncate_nl(char *s);
135
136 static inline char *skip_leading_chars(const char *s, const char *bad) {
137 if (!s)
138 return NULL;
139
140 if (!bad)
141 bad = WHITESPACE;
142
143 return (char*) s + strspn(s, bad);
144 }
145
146 char ascii_tolower(char x);
147 char *ascii_strlower(char *s);
148 char *ascii_strlower_n(char *s, size_t n);
149
150 char ascii_toupper(char x);
151 char *ascii_strupper(char *s);
152
153 int ascii_strcasecmp_n(const char *a, const char *b, size_t n);
154 int ascii_strcasecmp_nn(const char *a, size_t n, const char *b, size_t m);
155
156 bool chars_intersect(const char *a, const char *b) _pure_;
157
158 static inline bool _pure_ in_charset(const char *s, const char* charset) {
159 assert(s);
160 assert(charset);
161 return s[strspn(s, charset)] == '\0';
162 }
163
164 bool string_has_cc(const char *p, const char *ok) _pure_;
165
166 char *ellipsize_mem(const char *s, size_t old_length_bytes, size_t new_length_columns, unsigned percent);
167 static inline char *ellipsize(const char *s, size_t length, unsigned percent) {
168 return ellipsize_mem(s, strlen(s), length, percent);
169 }
170
171 char *cellescape(char *buf, size_t len, const char *s);
172
173 /* This limit is arbitrary, enough to give some idea what the string contains */
174 #define CELLESCAPE_DEFAULT_LENGTH 64
175
176 char* strshorten(char *s, size_t l);
177
178 char *strreplace(const char *text, const char *old_string, const char *new_string);
179
180 char *strip_tab_ansi(char **ibuf, size_t *_isz, size_t highlight[2]);
181
182 char *strextend_with_separator(char **x, const char *separator, ...) _sentinel_;
183
184 #define strextend(x, ...) strextend_with_separator(x, NULL, __VA_ARGS__)
185
186 char *strrep(const char *s, unsigned n);
187
188 int split_pair(const char *s, const char *sep, char **l, char **r);
189
190 int free_and_strdup(char **p, const char *s);
191 static inline int free_and_strdup_warn(char **p, const char *s) {
192 if (free_and_strdup(p, s) < 0)
193 return log_oom();
194 return 0;
195 }
196 int free_and_strndup(char **p, const char *s, size_t l);
197
198 bool string_is_safe(const char *p) _pure_;
199
200 static inline size_t strlen_ptr(const char *s) {
201 if (!s)
202 return 0;
203
204 return strlen(s);
205 }
206
207 DISABLE_WARNING_STRINGOP_TRUNCATION;
208 static inline void strncpy_exact(char *buf, const char *src, size_t buf_len) {
209 strncpy(buf, src, buf_len);
210 }
211 REENABLE_WARNING;
212
213 /* Like startswith(), but operates on arbitrary memory blocks */
214 static inline void *memory_startswith(const void *p, size_t sz, const char *token) {
215 assert(token);
216
217 size_t n = strlen(token);
218 if (sz < n)
219 return NULL;
220
221 assert(p);
222
223 if (memcmp(p, token, n) != 0)
224 return NULL;
225
226 return (uint8_t*) p + n;
227 }
228
229 /* Like startswith_no_case(), but operates on arbitrary memory blocks.
230 * It works only for ASCII strings.
231 */
232 static inline void *memory_startswith_no_case(const void *p, size_t sz, const char *token) {
233 assert(token);
234
235 size_t n = strlen(token);
236 if (sz < n)
237 return NULL;
238
239 assert(p);
240
241 for (size_t i = 0; i < n; i++)
242 if (ascii_tolower(((char *)p)[i]) != ascii_tolower(token[i]))
243 return NULL;
244
245 return (uint8_t*) p + n;
246 }
247
248 static inline char* str_realloc(char **p) {
249 /* Reallocate *p to actual size */
250
251 if (!*p)
252 return NULL;
253
254 char *t = realloc(*p, strlen(*p) + 1);
255 if (!t)
256 return NULL;
257
258 return (*p = t);
259 }
260
261 char* string_erase(char *x);
262
263 int string_truncate_lines(const char *s, size_t n_lines, char **ret);
264 int string_extract_line(const char *s, size_t i, char **ret);
265
266 int string_contains_word_strv(const char *string, const char *separators, char **words, const char **ret_word);
267 static inline int string_contains_word(const char *string, const char *separators, const char *word) {
268 return string_contains_word_strv(string, separators, STRV_MAKE(word), NULL);
269 }