• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 13297401319

12 Feb 2025 09:02PM UTC coverage: 71.784% (-0.06%) from 71.84%
13297401319

push

github

poettering
update TODO

293477 of 408836 relevant lines covered (71.78%)

714381.64 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.77
/src/basic/string-util.h
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
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
#include "string-util-fundamental.h"
11
#include "utf8.h"
12

13
/* What is interpreted as whitespace? */
14
#define WHITESPACE          " \t\n\r"
15
#define NEWLINE             "\n\r"
16
#define QUOTES              "\"\'"
17
#define COMMENTS            "#;"
18
#define GLOB_CHARS          "*?["
19
#define DIGITS              "0123456789"
20
#define LOWERCASE_LETTERS   "abcdefghijklmnopqrstuvwxyz"
21
#define UPPERCASE_LETTERS   "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
22
#define LETTERS             LOWERCASE_LETTERS UPPERCASE_LETTERS
23
#define ALPHANUMERICAL      LETTERS DIGITS
24
#define HEXDIGITS           DIGITS "abcdefABCDEF"
25
#define LOWERCASE_HEXDIGITS DIGITS "abcdef"
26
#define URI_RESERVED        ":/?#[]@!$&'()*+;="         /* [RFC3986] */
27
#define URI_UNRESERVED      ALPHANUMERICAL "-._~"       /* [RFC3986] */
28
#define URI_VALID           URI_RESERVED URI_UNRESERVED /* [RFC3986] */
29

30
static inline char* strstr_ptr(const char *haystack, const char *needle) {
726,857✔
31
        if (!haystack || !needle)
726,857✔
32
                return NULL;
33
        return strstr(haystack, needle);
726,854✔
34
}
35

36
static inline char* strstrafter(const char *haystack, const char *needle) {
726,857✔
37
        char *p;
726,857✔
38

39
        /* Returns NULL if not found, or pointer to first character after needle if found */
40

41
        p = strstr_ptr(haystack, needle);
726,857✔
42
        if (!p)
726,857✔
43
                return NULL;
44

45
        return p + strlen(needle);
32,899✔
46
}
47

48
static inline const char* strnull(const char *s) {
105,944✔
49
        return s ?: "(null)";
104,523✔
50
}
51

52
static inline const char* strna(const char *s) {
7,579,351✔
53
        return s ?: "n/a";
7,452,643✔
54
}
55

56
static inline const char* true_false(bool b) {
28✔
57
        return b ? "true" : "false";
28✔
58
}
59

60
static inline const char* plus_minus(bool b) {
46✔
61
        return b ? "+" : "-";
46✔
62
}
63

64
static inline const char* one_zero(bool b) {
23,884✔
65
        return b ? "1" : "0";
22,652✔
66
}
67

68
static inline const char* enable_disable(bool b) {
252✔
69
        return b ? "enable" : "disable";
252✔
70
}
71

72
static inline const char* enabled_disabled(bool b) {
1✔
73
        return b ? "enabled" : "disabled";
1✔
74
}
75

76
/* This macro's return pointer will have the "const" qualifier set or unset the same way as the input
77
 * pointer. */
78
#define empty_to_null(p)                                \
79
        ({                                              \
80
                const char *_p = (p);                   \
81
                (typeof(p)) (isempty(_p) ? NULL : _p);  \
82
        })
83

84
static inline const char* empty_to_na(const char *p) {
7,787✔
85
        return isempty(p) ? "n/a" : p;
15,574✔
86
}
87

88
static inline const char* empty_to_dash(const char *str) {
498✔
89
        return isempty(str) ? "-" : str;
2,857✔
90
}
91

92
static inline bool empty_or_dash(const char *str) {
266,124✔
93
        return !str ||
437,312✔
94
                str[0] == 0 ||
252,905✔
95
                (str[0] == '-' && str[1] == 0);
82,383✔
96
}
97

98
static inline const char* empty_or_dash_to_null(const char *p) {
99
        return empty_or_dash(p) ? NULL : p;
100
}
101
#define empty_or_dash_to_null(p)                                \
102
        ({                                                      \
103
                const char *_p = (p);                           \
104
                (typeof(p)) (empty_or_dash(_p) ? NULL : _p);    \
105
        })
106

107
char* first_word(const char *s, const char *word) _pure_;
108

109
char* strprepend(char **x, const char *s);
110
char* strextendn(char **x, const char *s, size_t l);
111

112
#define strjoin(a, ...) strextend_with_separator_internal(NULL, NULL, a, __VA_ARGS__, NULL)
113

114
#define strjoina(a, ...)                                                \
115
        ({                                                              \
116
                const char *_appendees_[] = { a, __VA_ARGS__ };         \
117
                char *_d_, *_p_;                                        \
118
                size_t _len_ = 0;                                       \
119
                size_t _i_;                                             \
120
                for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \
121
                        _len_ += strlen(_appendees_[_i_]);              \
122
                _p_ = _d_ = newa(char, _len_ + 1);                      \
123
                for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \
124
                        _p_ = stpcpy(_p_, _appendees_[_i_]);            \
125
                *_p_ = 0;                                               \
126
                _d_;                                                    \
127
        })
128

129
char* strstrip(char *s);
130
char* delete_chars(char *s, const char *bad);
131
char* delete_trailing_chars(char *s, const char *bad);
132
char* truncate_nl_full(char *s, size_t *ret_len);
133
static inline char* truncate_nl(char *s) {
3,176,841✔
134
        return truncate_nl_full(s, NULL);
3,176,841✔
135
}
136

137
static inline char* skip_leading_chars(const char *s, const char *bad) {
9,082,007✔
138
        if (!s)
9,082,007✔
139
                return NULL;
140

141
        if (!bad)
9,268,972✔
142
                bad = WHITESPACE;
1,082,600✔
143

144
        return (char*) s + strspn(s, bad);
9,268,952✔
145
}
146

147
char ascii_tolower(char x);
148
char* ascii_strlower(char *s);
149
char* ascii_strlower_n(char *s, size_t n);
150

151
char ascii_toupper(char x);
152
char* ascii_strupper(char *s);
153

154
int ascii_strcasecmp_n(const char *a, const char *b, size_t n);
155
int ascii_strcasecmp_nn(const char *a, size_t n, const char *b, size_t m);
156

157
bool chars_intersect(const char *a, const char *b) _pure_;
158

159
static inline bool _pure_ in_charset(const char *s, const char *charset) {
13,786,223✔
160
        assert(s);
13,786,223✔
161
        assert(charset);
13,786,223✔
162
        return s[strspn(s, charset)] == '\0';
13,786,223✔
163
}
164

165
static inline bool char_is_cc(char p) {
2,973,367✔
166
        /* char is unsigned on some architectures, e.g. aarch64. So, compiler may warn the condition
167
         * p >= 0 is always true. See #19543. Hence, let's cast to unsigned before the comparison. Note
168
         * that the cast in the right hand side is redundant, as according to the C standard, compilers
169
         * automatically cast a signed value to unsigned when comparing with an unsigned variable. Just
170
         * for safety and readability. */
171
        return (uint8_t) p < (uint8_t) ' ' || p == 127;
2,973,367✔
172
}
173
bool string_has_cc(const char *p, const char *ok) _pure_;
174

175
char* ellipsize_mem(const char *s, size_t old_length_bytes, size_t new_length_columns, unsigned percent);
176
static inline char* ellipsize(const char *s, size_t length, unsigned percent) {
1,259✔
177
        return ellipsize_mem(s, strlen(s), length, percent);
1,259✔
178
}
179

180
char* cellescape(char *buf, size_t len, const char *s);
181

182
/* This limit is arbitrary, enough to give some idea what the string contains */
183
#define CELLESCAPE_DEFAULT_LENGTH 64
184

185
char* strshorten(char *s, size_t l);
186

187
int strgrowpad0(char **s, size_t l);
188

189
char* strreplace(const char *text, const char *old_string, const char *new_string);
190

191
char* strip_tab_ansi(char **ibuf, size_t *_isz, size_t highlight[2]);
192

193
char* strextend_with_separator_internal(char **x, const char *separator, ...) _sentinel_;
194
#define strextend_with_separator(x, separator, ...) strextend_with_separator_internal(x, separator, __VA_ARGS__, NULL)
195
#define strextend(x, ...) strextend_with_separator_internal(x, NULL, __VA_ARGS__, NULL)
196

197
int strextendf_with_separator(char **x, const char *separator, const char *format, ...) _printf_(3,4);
198
#define strextendf(x, ...) strextendf_with_separator(x, NULL, __VA_ARGS__)
199

200
char* strrep(const char *s, unsigned n);
201

202
#define strrepa(s, n)                                                   \
203
        ({                                                              \
204
                const char *_sss_ = (s);                                \
205
                size_t _nnn_ = (n), _len_ = strlen(_sss_);              \
206
                assert_se(MUL_ASSIGN_SAFE(&_len_, _nnn_));              \
207
                char *_d_, *_p_;                                        \
208
                _p_ = _d_ = newa(char, _len_ + 1);                      \
209
                for (size_t _i_ = 0; _i_ < _nnn_; _i_++)                \
210
                        _p_ = stpcpy(_p_, _sss_);                       \
211
                *_p_ = 0;                                               \
212
                _d_;                                                    \
213
        })
214

215
int split_pair(const char *s, const char *sep, char **ret_first, char **ret_second);
216

217
int free_and_strdup(char **p, const char *s);
218
static inline int free_and_strdup_warn(char **p, const char *s) {
52,161✔
219
        int r;
52,161✔
220

221
        r = free_and_strdup(p, s);
52,161✔
222
        if (r < 0)
52,161✔
223
                return log_oom();
×
224
        return r;
225
}
226
int free_and_strndup(char **p, const char *s, size_t l);
227

228
int strdup_to_full(char **ret, const char *src);
229
static inline int strdup_to(char **ret, const char *src) {
534,901✔
230
        int r = strdup_to_full(ASSERT_PTR(ret), src);
534,901✔
231
        return r < 0 ? r : 0;  /* Suppress return value of 1. */
533,883✔
232
}
233

234
bool string_is_safe(const char *p) _pure_;
235
static inline bool string_is_safe_ascii(const char *p) {
×
236
        return ascii_is_valid(p) && string_is_safe(p);
×
237
}
238

239
DISABLE_WARNING_STRINGOP_TRUNCATION;
240
static inline void strncpy_exact(char *buf, const char *src, size_t buf_len) {
150✔
241
        strncpy(buf, src, buf_len);
150✔
242
}
145✔
243
REENABLE_WARNING;
244

245
/* Like startswith_no_case(), but operates on arbitrary memory blocks.
246
 * It works only for ASCII strings.
247
 */
248
static inline void* memory_startswith_no_case(const void *p, size_t sz, const char *token) {
19✔
249
        assert(token);
19✔
250

251
        size_t n = strlen(token);
19✔
252
        if (sz < n)
19✔
253
                return NULL;
254

255
        assert(p);
18✔
256

257
        for (size_t i = 0; i < n; i++)
44✔
258
                if (ascii_tolower(((char *)p)[i]) != ascii_tolower(token[i]))
27✔
259
                        return NULL;
260

261
        return (uint8_t*) p + n;
17✔
262
}
263

264
static inline char* str_realloc(char *p) {
31,674✔
265
        /* Reallocate *p to actual size. Ignore failure, and return the original string on error. */
266

267
        if (!p)
31,674✔
268
                return NULL;
269

270
        return realloc(p, strlen(p) + 1) ?: p;
31,524✔
271
}
272

273
char* string_erase(char *x);
274

275
int string_truncate_lines(const char *s, size_t n_lines, char **ret);
276
int string_extract_line(const char *s, size_t i, char **ret);
277

278
int string_contains_word_strv(const char *string, const char *separators, char * const *words, const char **ret_word);
279
static inline int string_contains_word(const char *string, const char *separators, const char *word) {
63✔
280
        return string_contains_word_strv(string, separators, STRV_MAKE(word), NULL);
13✔
281
}
282

283
bool streq_skip_trailing_chars(const char *s1, const char *s2, const char *ok);
284

285
char* string_replace_char(char *str, char old_char, char new_char);
286

287
typedef enum MakeCStringMode {
288
        MAKE_CSTRING_REFUSE_TRAILING_NUL,
289
        MAKE_CSTRING_ALLOW_TRAILING_NUL,
290
        MAKE_CSTRING_REQUIRE_TRAILING_NUL,
291
        _MAKE_CSTRING_MODE_MAX,
292
        _MAKE_CSTRING_MODE_INVALID = -1,
293
} MakeCStringMode;
294

295
int make_cstring(const char *s, size_t n, MakeCStringMode mode, char **ret);
296

297
size_t strspn_from_end(const char *str, const char *accept);
298

299
char* strdupspn(const char *a, const char *accept);
300
char* strdupcspn(const char *a, const char *reject);
301

302
char* find_line_startswith(const char *haystack, const char *needle);
303

304
bool version_is_valid(const char *s);
305

306
bool version_is_valid_versionspec(const char *s);
307

308
ssize_t strlevenshtein(const char *x, const char *y);
309

310
char* strrstr(const char *haystack, const char *needle);
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc