]> git.proxmox.com Git - systemd.git/blame - src/basic/string-util.h
New upstream version 249~rc1
[systemd.git] / src / basic / string-util.h
CommitLineData
a032b68d 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
db2df898
MP
2#pragma once
3
db2df898 4#include <stdbool.h>
4c89c718 5#include <stddef.h>
db2df898
MP
6#include <string.h>
7
7c20daf6 8#include "alloc-util.h"
db2df898 9#include "macro.h"
3a6ce677 10#include "string-util-fundamental.h"
db2df898
MP
11
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
aa27b158 23#define HEXDIGITS DIGITS "abcdefABCDEF"
db2df898 24
a032b68d
MB
25static inline char* strstr_ptr(const char *haystack, const char *needle) {
26 if (!haystack || !needle)
27 return NULL;
28 return strstr(haystack, needle);
29}
30
db2df898 31static inline const char* strempty(const char *s) {
1d42b86d 32 return s ?: "";
db2df898
MP
33}
34
35static inline const char* strnull(const char *s) {
1d42b86d 36 return s ?: "(null)";
db2df898
MP
37}
38
39static inline const char *strna(const char *s) {
1d42b86d 40 return s ?: "n/a";
db2df898
MP
41}
42
e1f67bc7
MB
43static inline const char* true_false(bool b) {
44 return b ? "true" : "false";
45}
46
a032b68d
MB
47static inline const char* plus_minus(bool b) {
48 return b ? "+" : "-";
49}
50
e1f67bc7
MB
51static inline const char* one_zero(bool b) {
52 return b ? "1" : "0";
53}
54
55static inline const char* enable_disable(bool b) {
56 return b ? "enable" : "disable";
57}
58
5a920b42
MP
59static inline const char *empty_to_null(const char *p) {
60 return isempty(p) ? NULL : p;
61}
62
b012e921 63static inline const char *empty_to_dash(const char *str) {
8a584da2
MP
64 return isempty(str) ? "-" : str;
65}
66
bb4f798a
MB
67static inline bool empty_or_dash(const char *str) {
68 return !str ||
69 str[0] == 0 ||
70 (str[0] == '-' && str[1] == 0);
71}
72
73static inline const char *empty_or_dash_to_null(const char *p) {
74 return empty_or_dash(p) ? NULL : p;
75}
76
db2df898
MP
77char *first_word(const char *s, const char *word) _pure_;
78
db2df898
MP
79char *strnappend(const char *s, const char *suffix, size_t length);
80
2897b343
MP
81char *strjoin_real(const char *x, ...) _sentinel_;
82#define strjoin(a, ...) strjoin_real((a), __VA_ARGS__, NULL)
db2df898
MP
83
84#define strjoina(a, ...) \
85 ({ \
86 const char *_appendees_[] = { a, __VA_ARGS__ }; \
87 char *_d_, *_p_; \
a032b68d
MB
88 size_t _len_ = 0; \
89 size_t _i_; \
db2df898
MP
90 for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \
91 _len_ += strlen(_appendees_[_i_]); \
7c20daf6 92 _p_ = _d_ = newa(char, _len_ + 1); \
db2df898
MP
93 for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \
94 _p_ = stpcpy(_p_, _appendees_[_i_]); \
95 *_p_ = 0; \
96 _d_; \
97 })
98
99char *strstrip(char *s);
100char *delete_chars(char *s, const char *bad);
52ad194e 101char *delete_trailing_chars(char *s, const char *bad);
db2df898
MP
102char *truncate_nl(char *s);
103
52ad194e 104static inline char *skip_leading_chars(const char *s, const char *bad) {
52ad194e
MB
105 if (!s)
106 return NULL;
107
108 if (!bad)
109 bad = WHITESPACE;
110
111 return (char*) s + strspn(s, bad);
112}
113
4c89c718
MP
114char ascii_tolower(char x);
115char *ascii_strlower(char *s);
116char *ascii_strlower_n(char *s, size_t n);
117
8a584da2
MP
118char ascii_toupper(char x);
119char *ascii_strupper(char *s);
120
4c89c718
MP
121int ascii_strcasecmp_n(const char *a, const char *b, size_t n);
122int ascii_strcasecmp_nn(const char *a, size_t n, const char *b, size_t m);
db2df898
MP
123
124bool chars_intersect(const char *a, const char *b) _pure_;
125
126static inline bool _pure_ in_charset(const char *s, const char* charset) {
127 assert(s);
128 assert(charset);
129 return s[strspn(s, charset)] == '\0';
130}
131
8b3d4ff0
MB
132static inline bool char_is_cc(char p) {
133 /* char is unsigned on some architectures, e.g. aarch64. So, compiler may warn the condition
134 * p >= 0 is always true. See #19543. Hence, let's cast to unsigned before the comparison. Note
135 * that the cast in the right hand side is redundant, as according to the C standard, compilers
136 * automatically cast a signed value to unsigned when comparing with an unsigned variable. Just
137 * for safety and readability. */
138 return (uint8_t) p < (uint8_t) ' ' || p == 127;
139}
db2df898
MP
140bool string_has_cc(const char *p, const char *ok) _pure_;
141
142char *ellipsize_mem(const char *s, size_t old_length_bytes, size_t new_length_columns, unsigned percent);
b012e921
MB
143static inline char *ellipsize(const char *s, size_t length, unsigned percent) {
144 return ellipsize_mem(s, strlen(s), length, percent);
145}
146
147char *cellescape(char *buf, size_t len, const char *s);
148
149/* This limit is arbitrary, enough to give some idea what the string contains */
150#define CELLESCAPE_DEFAULT_LENGTH 64
db2df898 151
db2df898
MP
152char* strshorten(char *s, size_t l);
153
154char *strreplace(const char *text, const char *old_string, const char *new_string);
155
1d42b86d 156char *strip_tab_ansi(char **ibuf, size_t *_isz, size_t highlight[2]);
db2df898 157
3a6ce677 158char *strextend_with_separator_internal(char **x, const char *separator, ...) _sentinel_;
3a6ce677
BR
159#define strextend_with_separator(x, separator, ...) strextend_with_separator_internal(x, separator, __VA_ARGS__, NULL)
160#define strextend(x, ...) strextend_with_separator_internal(x, NULL, __VA_ARGS__, NULL)
db2df898 161
8b3d4ff0
MB
162int strextendf_with_separator(char **x, const char *separator, const char *format, ...) _printf_(3,4);
163#define strextendf(x, ...) strextendf_with_separator(x, NULL, __VA_ARGS__)
164
db2df898
MP
165char *strrep(const char *s, unsigned n);
166
167int split_pair(const char *s, const char *sep, char **l, char **r);
168
169int free_and_strdup(char **p, const char *s);
bb4f798a
MB
170static inline int free_and_strdup_warn(char **p, const char *s) {
171 if (free_and_strdup(p, s) < 0)
172 return log_oom();
173 return 0;
db2df898 174}
bb4f798a 175int free_and_strndup(char **p, const char *s, size_t l);
2897b343 176
db2df898 177bool string_is_safe(const char *p) _pure_;
f5e65279
MB
178
179static inline size_t strlen_ptr(const char *s) {
180 if (!s)
181 return 0;
182
183 return strlen(s);
184}
b012e921 185
f2dec872
BR
186DISABLE_WARNING_STRINGOP_TRUNCATION;
187static inline void strncpy_exact(char *buf, const char *src, size_t buf_len) {
188 strncpy(buf, src, buf_len);
189}
190REENABLE_WARNING;
191
b012e921
MB
192/* Like startswith(), but operates on arbitrary memory blocks */
193static inline void *memory_startswith(const void *p, size_t sz, const char *token) {
b012e921
MB
194 assert(token);
195
a032b68d 196 size_t n = strlen(token);
b012e921
MB
197 if (sz < n)
198 return NULL;
199
200 assert(p);
201
202 if (memcmp(p, token, n) != 0)
203 return NULL;
204
205 return (uint8_t*) p + n;
206}
6e866b33
MB
207
208/* Like startswith_no_case(), but operates on arbitrary memory blocks.
209 * It works only for ASCII strings.
210 */
211static inline void *memory_startswith_no_case(const void *p, size_t sz, const char *token) {
6e866b33
MB
212 assert(token);
213
a032b68d 214 size_t n = strlen(token);
6e866b33
MB
215 if (sz < n)
216 return NULL;
217
218 assert(p);
219
a032b68d 220 for (size_t i = 0; i < n; i++)
6e866b33
MB
221 if (ascii_tolower(((char *)p)[i]) != ascii_tolower(token[i]))
222 return NULL;
6e866b33
MB
223
224 return (uint8_t*) p + n;
225}
f2dec872 226
8b3d4ff0
MB
227static inline char* str_realloc(char *p) {
228 /* Reallocate *p to actual size. Ignore failure, and return the original string on error. */
f2dec872 229
8b3d4ff0 230 if (!p)
f2dec872
BR
231 return NULL;
232
8b3d4ff0 233 return realloc(p, strlen(p) + 1) ?: p;
f2dec872 234}
46cdbd49
BR
235
236char* string_erase(char *x);
237
238int string_truncate_lines(const char *s, size_t n_lines, char **ret);
239int string_extract_line(const char *s, size_t i, char **ret);
a032b68d
MB
240
241int string_contains_word_strv(const char *string, const char *separators, char **words, const char **ret_word);
242static inline int string_contains_word(const char *string, const char *separators, const char *word) {
243 return string_contains_word_strv(string, separators, STRV_MAKE(word), NULL);
244}