]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/string-util.h
Merge pull request #25168 from valentindavid/valentindavid/umount-move-recursive...
[thirdparty/systemd.git] / src / basic / string-util.h
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
07630cea
LP
2#pragma once
3
07630cea 4#include <stdbool.h>
11c3a366 5#include <stddef.h>
07630cea
LP
6#include <string.h>
7
6e9417f5 8#include "alloc-util.h"
07630cea 9#include "macro.h"
e5bc5f1f 10#include "string-util-fundamental.h"
07630cea 11
b11d6a7b 12/* What is interpreted as whitespace? */
1d0727e7
MS
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
23#define HEXDIGITS DIGITS "abcdefABCDEF"
24#define LOWERCASE_HEXDIGITS DIGITS "abcdef"
b11d6a7b 25
4eb0c875
ZJS
26static inline char* strstr_ptr(const char *haystack, const char *needle) {
27 if (!haystack || !needle)
28 return NULL;
29 return strstr(haystack, needle);
30}
31
07630cea 32static inline const char* strnull(const char *s) {
ad5d4b17 33 return s ?: "(null)";
07630cea
LP
34}
35
36static inline const char *strna(const char *s) {
ad5d4b17 37 return s ?: "n/a";
07630cea
LP
38}
39
55fced5a
ZJS
40static inline const char* true_false(bool b) {
41 return b ? "true" : "false";
42}
43
fe37e5a5
ZJS
44static inline const char* plus_minus(bool b) {
45 return b ? "+" : "-";
46}
47
55fced5a
ZJS
48static inline const char* one_zero(bool b) {
49 return b ? "1" : "0";
50}
51
52static inline const char* enable_disable(bool b) {
53 return b ? "enable" : "disable";
54}
55
ef2409cb
LP
56/* This macro's return pointer will have the "const" qualifier set or unset the same way as the input
57 * pointer. */
58#define empty_to_null(p) \
59 ({ \
60 const char *_p = (p); \
61 (typeof(p)) (isempty(_p) ? NULL : _p); \
62 })
3c6f7c34 63
edee65a6
ZJS
64static inline const char *empty_to_na(const char *p) {
65 return isempty(p) ? "n/a" : p;
66}
67
c5984fe1 68static inline const char *empty_to_dash(const char *str) {
07b0b339
SK
69 return isempty(str) ? "-" : str;
70}
71
e7b88b7b
LP
72static inline bool empty_or_dash(const char *str) {
73 return !str ||
74 str[0] == 0 ||
75 (str[0] == '-' && str[1] == 0);
76}
77
dc90e0fa
LP
78static inline const char *empty_or_dash_to_null(const char *p) {
79 return empty_or_dash(p) ? NULL : p;
80}
ef2409cb
LP
81#define empty_or_dash_to_null(p) \
82 ({ \
83 const char *_p = (p); \
84 (typeof(p)) (empty_or_dash(_p) ? NULL : _p); \
85 })
dc90e0fa 86
07630cea
LP
87char *first_word(const char *s, const char *word) _pure_;
88
07630cea
LP
89char *strnappend(const char *s, const char *suffix, size_t length);
90
605405c6
ZJS
91char *strjoin_real(const char *x, ...) _sentinel_;
92#define strjoin(a, ...) strjoin_real((a), __VA_ARGS__, NULL)
07630cea
LP
93
94#define strjoina(a, ...) \
95 ({ \
96 const char *_appendees_[] = { a, __VA_ARGS__ }; \
97 char *_d_, *_p_; \
9a485918
ZJS
98 size_t _len_ = 0; \
99 size_t _i_; \
07630cea
LP
100 for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \
101 _len_ += strlen(_appendees_[_i_]); \
6e9417f5 102 _p_ = _d_ = newa(char, _len_ + 1); \
07630cea
LP
103 for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \
104 _p_ = stpcpy(_p_, _appendees_[_i_]); \
105 *_p_ = 0; \
106 _d_; \
107 })
108
109char *strstrip(char *s);
110char *delete_chars(char *s, const char *bad);
7546145e 111char *delete_trailing_chars(char *s, const char *bad);
07630cea
LP
112char *truncate_nl(char *s);
113
7546145e 114static inline char *skip_leading_chars(const char *s, const char *bad) {
7546145e
LP
115 if (!s)
116 return NULL;
117
118 if (!bad)
119 bad = WHITESPACE;
120
121 return (char*) s + strspn(s, bad);
122}
123
b577e3d5
LP
124char ascii_tolower(char x);
125char *ascii_strlower(char *s);
126char *ascii_strlower_n(char *s, size_t n);
07630cea 127
846b8fc3
LP
128char ascii_toupper(char x);
129char *ascii_strupper(char *s);
130
522d85ae 131int ascii_strcasecmp_n(const char *a, const char *b, size_t n);
c1749834 132int ascii_strcasecmp_nn(const char *a, size_t n, const char *b, size_t m);
522d85ae 133
07630cea
LP
134bool chars_intersect(const char *a, const char *b) _pure_;
135
136static inline bool _pure_ in_charset(const char *s, const char* charset) {
137 assert(s);
138 assert(charset);
139 return s[strspn(s, charset)] == '\0';
140}
141
6302d386 142static inline bool char_is_cc(char p) {
3d56acef
YW
143 /* char is unsigned on some architectures, e.g. aarch64. So, compiler may warn the condition
144 * p >= 0 is always true. See #19543. Hence, let's cast to unsigned before the comparison. Note
145 * that the cast in the right hand side is redundant, as according to the C standard, compilers
146 * automatically cast a signed value to unsigned when comparing with an unsigned variable. Just
147 * for safety and readability. */
148 return (uint8_t) p < (uint8_t) ' ' || p == 127;
6302d386 149}
07630cea
LP
150bool string_has_cc(const char *p, const char *ok) _pure_;
151
152char *ellipsize_mem(const char *s, size_t old_length_bytes, size_t new_length_columns, unsigned percent);
ae03775f
ZJS
153static inline char *ellipsize(const char *s, size_t length, unsigned percent) {
154 return ellipsize_mem(s, strlen(s), length, percent);
155}
156
8409f688 157char *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
07630cea
LP
162char* strshorten(char *s, size_t l);
163
2812017c
DDM
164int strgrowpad0(char **s, size_t l);
165
07630cea
LP
166char *strreplace(const char *text, const char *old_string, const char *new_string);
167
b4766d5f 168char *strip_tab_ansi(char **ibuf, size_t *_isz, size_t highlight[2]);
07630cea 169
c2bc710b 170char *strextend_with_separator_internal(char **x, const char *separator, ...) _sentinel_;
c2bc710b
LP
171#define strextend_with_separator(x, separator, ...) strextend_with_separator_internal(x, separator, __VA_ARGS__, NULL)
172#define strextend(x, ...) strextend_with_separator_internal(x, NULL, __VA_ARGS__, NULL)
07630cea 173
6b13ca8a
YW
174int strextendf_with_separator(char **x, const char *separator, const char *format, ...) _printf_(3,4);
175#define strextendf(x, ...) strextendf_with_separator(x, NULL, __VA_ARGS__)
e9b88a6d 176
07630cea
LP
177char *strrep(const char *s, unsigned n);
178
179int split_pair(const char *s, const char *sep, char **l, char **r);
180
181int free_and_strdup(char **p, const char *s);
f1531db5 182static inline int free_and_strdup_warn(char **p, const char *s) {
a56dd158
YW
183 int r;
184
185 r = free_and_strdup(p, s);
186 if (r < 0)
f1531db5 187 return log_oom();
a56dd158 188 return r;
f1531db5 189}
7f546026 190int free_and_strndup(char **p, const char *s, size_t l);
07630cea 191
f3e2e81d 192bool string_is_safe(const char *p) _pure_;
7bf7ce28 193
6695c200
ZJS
194DISABLE_WARNING_STRINGOP_TRUNCATION;
195static inline void strncpy_exact(char *buf, const char *src, size_t buf_len) {
196 strncpy(buf, src, buf_len);
197}
198REENABLE_WARNING;
199
21224070
DP
200/* Like startswith_no_case(), but operates on arbitrary memory blocks.
201 * It works only for ASCII strings.
202 */
203static inline void *memory_startswith_no_case(const void *p, size_t sz, const char *token) {
21224070
DP
204 assert(token);
205
9a485918 206 size_t n = strlen(token);
21224070
DP
207 if (sz < n)
208 return NULL;
209
210 assert(p);
211
9a485918 212 for (size_t i = 0; i < n; i++)
21224070
DP
213 if (ascii_tolower(((char *)p)[i]) != ascii_tolower(token[i]))
214 return NULL;
21224070
DP
215
216 return (uint8_t*) p + n;
217}
bc28751e 218
523e1b14
ZJS
219static inline char* str_realloc(char *p) {
220 /* Reallocate *p to actual size. Ignore failure, and return the original string on error. */
bc28751e 221
523e1b14 222 if (!p)
bc28751e
ZJS
223 return NULL;
224
523e1b14 225 return realloc(p, strlen(p) + 1) ?: p;
bc28751e 226}
53caaffd
LP
227
228char* string_erase(char *x);
8dd6491e
LP
229
230int string_truncate_lines(const char *s, size_t n_lines, char **ret);
f6857fa6 231int string_extract_line(const char *s, size_t i, char **ret);
46bf625a
ZJS
232
233int string_contains_word_strv(const char *string, const char *separators, char **words, const char **ret_word);
234static inline int string_contains_word(const char *string, const char *separators, const char *word) {
235 return string_contains_word_strv(string, separators, STRV_MAKE(word), NULL);
236}
8034b42c
ADT
237
238bool streq_skip_trailing_chars(const char *s1, const char *s2, const char *ok);
072f5f9b
YW
239
240char *string_replace_char(char *str, char old_char, char new_char);
146f4482 241
7153213e
LP
242typedef enum MakeCStringMode {
243 MAKE_CSTRING_REFUSE_TRAILING_NUL,
244 MAKE_CSTRING_ALLOW_TRAILING_NUL,
245 MAKE_CSTRING_REQUIRE_TRAILING_NUL,
246 _MAKE_CSTRING_MODE_MAX,
247 _MAKE_CSTRING_MODE_INVALID = -1,
248} MakeCStringMode;
249
250int make_cstring(const char *s, size_t n, MakeCStringMode mode, char **ret);
251
146f4482 252size_t strspn_from_end(const char *str, const char *accept);
e8bec624
LP
253
254char *strdupspn(const char *a, const char *accept);
255char *strdupcspn(const char *a, const char *reject);