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

systemd / systemd / 14554080340

19 Apr 2025 11:46AM UTC coverage: 72.101% (-0.03%) from 72.13%
14554080340

push

github

web-flow
Add two new paragraphs to coding style about header files (#37188)

296880 of 411754 relevant lines covered (72.1%)

687547.52 hits per line

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

96.97
/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) {
501,211✔
31
        if (!haystack || !needle)
501,211✔
32
                return NULL;
33
        return strstr(haystack, needle);
501,208✔
34
}
35

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

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

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

45
        return p + strlen(needle);
36,836✔
46
}
47

48
static inline const char* strnull(const char *s) {
79,562✔
49
        return s ?: "(null)";
78,080✔
50
}
51

52
static inline const char* strna(const char *s) {
6,986,404✔
53
        return s ?: "n/a";
6,859,801✔
54
}
55

56
static inline const char* true_false(bool b) {
26✔
57
        return b ? "true" : "false";
26✔
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) {
19,245✔
65
        return b ? "1" : "0";
18,074✔
66
}
67

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

72
static inline const char* enabled_disabled(bool b) {
7✔
73
        return b ? "enabled" : "disabled";
7✔
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) {
3,382✔
85
        return isempty(p) ? "n/a" : p;
6,764✔
86
}
87

88
static inline const char* empty_to_dash(const char *str) {
43✔
89
        return isempty(str) ? "-" : str;
58✔
90
}
91

92
static inline bool empty_or_dash(const char *str) {
164,106✔
93
        return !str ||
265,559✔
94
                str[0] == 0 ||
154,145✔
95
                (str[0] == '-' && str[1] == 0);
48,315✔
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) _nonnull_if_nonzero_(2, 3);
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) {
2,616,432✔
134
        return truncate_nl_full(s, NULL);
2,616,432✔
135
}
136

137
static inline char* skip_leading_chars(const char *s, const char *bad) {
7,841,808✔
138
        if (!s)
7,841,808✔
139
                return NULL;
140

141
        if (!bad)
7,986,320✔
142
                bad = WHITESPACE;
883,153✔
143

144
        return (char*) s + strspn(s, bad);
7,986,300✔
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) {
14,939,011✔
160
        assert(s);
14,939,011✔
161
        assert(charset);
14,939,011✔
162
        return s[strspn(s, charset)] == '\0';
14,939,011✔
163
}
164

165
static inline bool char_is_cc(char p) {
2,790,054✔
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,790,054✔
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) {
979✔
177
        return ellipsize_mem(s, strlen(s), length, percent);
979✔
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
int free_and_strdup_warn(char **p, const char *s);
219
int free_and_strndup(char **p, const char *s, size_t l) _nonnull_if_nonzero_(2, 3);
220

221
int strdup_to_full(char **ret, const char *src);
222
static inline int strdup_to(char **ret, const char *src) {
627,081✔
223
        int r = strdup_to_full(ASSERT_PTR(ret), src);
627,081✔
224
        return r < 0 ? r : 0;  /* Suppress return value of 1. */
626,050✔
225
}
226

227
bool string_is_safe(const char *p) _pure_;
228
static inline bool string_is_safe_ascii(const char *p) {
×
229
        return ascii_is_valid(p) && string_is_safe(p);
×
230
}
231

232
DISABLE_WARNING_STRINGOP_TRUNCATION;
233
static inline void strncpy_exact(char *buf, const char *src, size_t buf_len) {
154✔
234
        strncpy(buf, src, buf_len);
154✔
235
}
149✔
236
REENABLE_WARNING;
237

238
/* Like startswith_no_case(), but operates on arbitrary memory blocks.
239
 * It works only for ASCII strings.
240
 */
241
static inline void* memory_startswith_no_case(const void *p, size_t sz, const char *token) {
19✔
242
        assert(token);
19✔
243

244
        size_t n = strlen(token);
19✔
245
        if (sz < n)
19✔
246
                return NULL;
247

248
        assert(p);
18✔
249

250
        for (size_t i = 0; i < n; i++)
44✔
251
                if (ascii_tolower(((char *)p)[i]) != ascii_tolower(token[i]))
27✔
252
                        return NULL;
253

254
        return (uint8_t*) p + n;
17✔
255
}
256

257
static inline char* str_realloc(char *p) {
28,129✔
258
        /* Reallocate *p to actual size. Ignore failure, and return the original string on error. */
259

260
        if (!p)
28,129✔
261
                return NULL;
262

263
        return realloc(p, strlen(p) + 1) ?: p;
28,129✔
264
}
265

266
char* string_erase(char *x);
267

268
int string_truncate_lines(const char *s, size_t n_lines, char **ret);
269
int string_extract_line(const char *s, size_t i, char **ret);
270

271
int string_contains_word_strv(const char *string, const char *separators, char * const *words, const char **ret_word);
272
static inline int string_contains_word(const char *string, const char *separators, const char *word) {
63✔
273
        return string_contains_word_strv(string, separators, STRV_MAKE(word), NULL);
13✔
274
}
275

276
bool streq_skip_trailing_chars(const char *s1, const char *s2, const char *ok);
277

278
char* string_replace_char(char *str, char old_char, char new_char);
279

280
typedef enum MakeCStringMode {
281
        MAKE_CSTRING_REFUSE_TRAILING_NUL,
282
        MAKE_CSTRING_ALLOW_TRAILING_NUL,
283
        MAKE_CSTRING_REQUIRE_TRAILING_NUL,
284
        _MAKE_CSTRING_MODE_MAX,
285
        _MAKE_CSTRING_MODE_INVALID = -1,
286
} MakeCStringMode;
287

288
int make_cstring(const char *s, size_t n, MakeCStringMode mode, char **ret);
289

290
size_t strspn_from_end(const char *str, const char *accept);
291

292
char* strdupspn(const char *a, const char *accept);
293
char* strdupcspn(const char *a, const char *reject);
294

295
char* find_line_startswith(const char *haystack, const char *needle);
296

297
bool version_is_valid(const char *s);
298

299
bool version_is_valid_versionspec(const char *s);
300

301
ssize_t strlevenshtein(const char *x, const char *y);
302

303
char* strrstr(const char *haystack, const char *needle);
304

305
size_t str_common_prefix(const char *a, const char *b);
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