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

systemd / systemd / 24373971178

14 Apr 2026 12:08AM UTC coverage: 72.291% (+0.2%) from 72.107%
24373971178

push

github

web-flow
hwdb: Add extended SteelSeries Arctis headset device support (#41628)

Add USB device IDs for additional SteelSeries Arctis headset models to
the sound card hardware database.

Newly added device IDs:

- Arctis Nova 7x v2 (22AD)
- Arctis Nova 7 Diablo IV (22A9)
- Arctis Nova 7X (22A4)
- Arctis Nova 7X (22A5)
- Arctis Nova 7P V2 (22A7)

321138 of 444232 relevant lines covered (72.29%)

1277257.29 hits per line

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

91.44
/src/basic/string-util.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <stdio.h>
4

5
#include "alloc-util.h"
6
#include "escape.h"
7
#include "extract-word.h"
8
#include "glyph-util.h"
9
#include "gunicode.h"
10
#include "locale-util.h"
11
#include "log.h"
12
#include "memory-util.h"
13
#include "memstream-util.h"
14
#include "path-util.h"
15
#include "string-util.h"
16
#include "strv.h"
17
#include "terminal-util.h"
18
#include "utf8.h"
19

20
char* first_word(const char *s, const char *word) {
3,382,940✔
21
        assert(s);
3,382,940✔
22
        assert(word);
3,382,940✔
23

24
        /* Checks if the string starts with the specified word, either followed by NUL or by whitespace.
25
         * Returns a pointer to the NUL or the first character after the whitespace. */
26

27
        if (isempty(word))
3,382,940✔
28
                return (char*) s;
29

30
        const char *p = startswith(s, word);
3,382,939✔
31
        if (!p)
3,382,939✔
32
                return NULL;
33
        if (*p == '\0')
118,475✔
34
                return (char*) p;
35

36
        const char *nw = skip_leading_chars(p, WHITESPACE);
118,474✔
37
        if (p == nw)
118,474✔
38
                return NULL;
1✔
39

40
        return (char*) nw;
41
}
42

43
char* strextendn(char **x, const char *s, size_t l) {
29,200✔
44
        assert(x);
29,200✔
45
        assert(s || l == 0);
29,200✔
46

47
        if (l > 0)
29,200✔
48
                l = strnlen(s, l); /* ignore trailing noise */
28,874✔
49

50
        if (l > 0 || !*x) {
29,200✔
51
                size_t q;
29,044✔
52
                char *m;
29,044✔
53

54
                q = strlen_ptr(*x);
29,044✔
55
                m = realloc(*x, q + l + 1);
29,044✔
56
                if (!m)
29,044✔
57
                        return NULL;
58

59
                *mempcpy_typesafe(m + q, s, l) = 0;
29,044✔
60

61
                *x = m;
29,044✔
62
        }
63

64
        return *x;
29,200✔
65
}
66

67
char* strstrip(char *s) {
6,553,062✔
68
        if (!s)
6,553,062✔
69
                return NULL;
70

71
        /* Drops trailing whitespace. Modifies the string in place. Returns pointer to first non-space character */
72

73
        return delete_trailing_chars(skip_leading_chars(s, WHITESPACE), WHITESPACE);
6,549,720✔
74
}
75

76
char* delete_chars(char *s, const char *bad) {
9✔
77
        char *f, *t;
9✔
78

79
        /* Drops all specified bad characters, regardless where in the string */
80

81
        if (!s)
9✔
82
                return NULL;
83

84
        if (!bad)
9✔
85
                bad = WHITESPACE;
×
86

87
        for (f = s, t = s; *f; f++) {
131✔
88
                if (strchr(bad, *f))
122✔
89
                        continue;
67✔
90

91
                *(t++) = *f;
55✔
92
        }
93

94
        *t = 0;
9✔
95

96
        return s;
9✔
97
}
98

99
char* delete_trailing_chars(char *s, const char *bad) {
8,167,575✔
100
        char *c = s;
8,167,575✔
101

102
        /* Drops all specified bad characters, at the end of the string */
103

104
        if (!s)
8,167,575✔
105
                return NULL;
106

107
        if (!bad)
8,167,575✔
108
                bad = WHITESPACE;
87,023✔
109

110
        for (char *p = s; *p; p++)
292,153,366✔
111
                if (!strchr(bad, *p))
283,985,791✔
112
                        c = p + 1;
272,169,757✔
113

114
        *c = 0;
8,167,575✔
115

116
        return s;
8,167,575✔
117
}
118

119
char* truncate_nl_full(char *s, size_t *ret_len) {
2,880,908✔
120
        size_t n;
2,880,908✔
121

122
        assert(s);
2,880,908✔
123

124
        n = strcspn(s, NEWLINE);
2,880,908✔
125
        s[n] = '\0';
2,880,908✔
126
        if (ret_len)
2,880,908✔
127
                *ret_len = n;
6✔
128
        return s;
2,880,908✔
129
}
130

131
char ascii_tolower(char x) {
52,530,062✔
132

133
        if (x >= 'A' && x <= 'Z')
52,530,062✔
134
                return x - 'A' + 'a';
3,608,047✔
135

136
        return x;
137
}
138

139
char ascii_toupper(char x) {
332,487✔
140

141
        if (x >= 'a' && x <= 'z')
332,487✔
142
                return x - 'a' + 'A';
203,102✔
143

144
        return x;
145
}
146

147
char* ascii_strlower(char *s) {
28,913✔
148
        assert(s);
28,913✔
149

150
        for (char *p = s; *p; p++)
185,114✔
151
                *p = ascii_tolower(*p);
156,201✔
152

153
        return s;
28,913✔
154
}
155

156
char* ascii_strupper(char *s) {
23,868✔
157
        assert(s);
23,868✔
158

159
        for (char *p = s; *p; p++)
356,225✔
160
                *p = ascii_toupper(*p);
332,357✔
161

162
        return s;
23,868✔
163
}
164

165
char* ascii_strlower_n(char *s, size_t n) {
1,819,540✔
166
        if (n <= 0)
1,819,540✔
167
                return s;
168

169
        for (size_t i = 0; i < n; i++)
15,535,179✔
170
                s[i] = ascii_tolower(s[i]);
13,715,974✔
171

172
        return s;
173
}
174

175
int ascii_strcasecmp_n(const char *a, const char *b, size_t n) {
2,563,195✔
176

177
        assert(a);
2,563,195✔
178
        assert(b);
2,563,195✔
179

180
        for (; n > 0; a++, b++, n--) {
21,287,227✔
181
                int x, y;
19,324,478✔
182

183
                x = (int) (uint8_t) ascii_tolower(*a);
19,324,478✔
184
                y = (int) (uint8_t) ascii_tolower(*b);
19,324,478✔
185

186
                if (x != y)
19,324,478✔
187
                        return x - y;
600,446✔
188
        }
189

190
        return 0;
191
}
192

193
int ascii_strcasecmp_nn(const char *a, size_t n, const char *b, size_t m) {
1,090,100✔
194
        int r;
1,090,100✔
195

196
        r = ascii_strcasecmp_n(a, b, MIN(n, m));
1,090,100✔
197
        if (r != 0)
1,090,100✔
198
                return r;
199

200
        return CMP(n, m);
959,190✔
201
}
202

203
bool chars_intersect(const char *a, const char *b) {
11,648✔
204
        /* Returns true if any of the chars in a are in b. */
205
        for (const char *p = a; *p; p++)
167,978✔
206
                if (strchr(b, *p))
156,343✔
207
                        return true;
208

209
        return false;
210
}
211

212
bool string_has_cc(const char *p, const char *ok) {
13,956,826✔
213
        assert(p);
13,956,826✔
214

215
        /*
216
         * Check if a string contains control characters. If 'ok' is
217
         * non-NULL it may be a string containing additional CCs to be
218
         * considered OK.
219
         */
220

221
        for (const char *t = p; *t; t++) {
252,708,104✔
222
                if (ok && strchr(ok, *t))
238,751,300✔
223
                        continue;
8✔
224

225
                if (char_is_cc(*t))
238,751,292✔
226
                        return true;
227
        }
228

229
        return false;
230
}
231

232
static int write_ellipsis(char *buf, bool unicode) {
4,457✔
233
        const char *s = glyph_full(GLYPH_ELLIPSIS, unicode);
4,457✔
234
        assert(strlen(s) == 3);
4,457✔
235
        memcpy(buf, s, 3);
4,457✔
236
        return 3;
4,457✔
237
}
238

239
static size_t ansi_sequence_length(const char *s, size_t len) {
6,334✔
240
        assert(s);
6,334✔
241

242
        if (len < 2)
6,334✔
243
                return 0;
244

245
        if (s[0] != 0x1B)  /* ASCII 27, aka ESC, aka Ctrl-[ */
6,242✔
246
                return 0;  /* Not the start of a sequence */
247

248
        if (s[1] == 0x5B) { /* [, start of CSI sequence */
903✔
249
                size_t i = 2;
902✔
250

251
                if (i == len)
902✔
252
                        return 0;
253

254
                while (s[i] >= 0x30 && s[i] <= 0x3F) /* Parameter bytes */
9,245✔
255
                        if (++i == len)
8,343✔
256
                                return 0;
257
                while (s[i] >= 0x20 && s[i] <= 0x2F) /* Intermediate bytes */
902✔
258
                        if (++i == len)
×
259
                                return 0;
260
                if (s[i] >= 0x40 && s[i] <= 0x7E) /* Final byte */
902✔
261
                        return i + 1;
902✔
262
                return 0;  /* Bad sequence */
263

264
        } else if (s[1] >= 0x40 && s[1] <= 0x5F) /* other non-CSI Fe sequence */
1✔
265
                return 2;
×
266

267
        return 0;  /* Bad escape? */
268
}
269

270
static bool string_has_ansi_sequence(const char *s, size_t len) {
7,985✔
271
        const char *t = s;
7,985✔
272

273
        while ((t = memchr(t, 0x1B, len - (t - s)))) {
7,986✔
274
                if (ansi_sequence_length(t, len - (t - s)) > 0)
154✔
275
                        return true;
276
                t++;
1✔
277
        }
278
        return false;
279
}
280

281
static size_t previous_ansi_sequence(const char *s, size_t length, const char **ret_where) {
265✔
282

283
        assert(s);
265✔
284
        assert(ret_where);
265✔
285

286
        /* Locate the previous ANSI sequence and save its start in *ret_where and return length. */
287

288
        for (size_t i = length - 2; i > 0; i--) {  /* -2 because at least two bytes are needed */
3,442✔
289
                size_t slen = ansi_sequence_length(s + (i - 1), length - (i - 1));
3,440✔
290
                if (slen == 0)
3,440✔
291
                        continue;
3,177✔
292

293
                *ret_where = s + (i - 1);
263✔
294
                return slen;
263✔
295
        }
296

297
        *ret_where = NULL;
2✔
298
        return 0;
2✔
299
}
300

301
static char *ascii_ellipsize_mem(const char *s, size_t old_length, size_t new_length, unsigned percent) {
4,432✔
302
        size_t x, need_space, suffix_len;
4,432✔
303
        char *t;
4,432✔
304

305
        assert(s);
4,432✔
306
        assert(percent <= 100);
4,432✔
307
        assert(new_length != SIZE_MAX);
4,432✔
308

309
        if (old_length <= new_length)
4,432✔
310
                return strndup(s, old_length);
1,767✔
311

312
        /* Special case short ellipsations */
313
        switch (new_length) {
2,665✔
314

315
        case 0:
×
316
                return strdup("");
×
317

318
        case 1:
65✔
319
                if (is_locale_utf8())
65✔
320
                        return strdup("…");
65✔
321
                else
322
                        return strdup(".");
×
323

324
        case 2:
35✔
325
                if (!is_locale_utf8())
35✔
326
                        return strdup("..");
×
327
                break;
328
        }
329

330
        /* Calculate how much space the ellipsis will take up. If we are in UTF-8 mode we only need space for one
331
         * character ("…"), otherwise for three characters ("..."). Note that in both cases we need 3 bytes of storage,
332
         * either for the UTF-8 encoded character or for three ASCII characters. */
333
        need_space = is_locale_utf8() ? 1 : 3;
2,600✔
334

335
        t = new(char, new_length+3);
2,600✔
336
        if (!t)
2,600✔
337
                return NULL;
338

339
        assert(new_length >= need_space);
2,600✔
340

341
        x = ((new_length - need_space) * percent + 50) / 100;
2,600✔
342
        assert(x <= new_length - need_space);
2,600✔
343

344
        write_ellipsis(mempcpy(t, s, x), /* unicode= */ false);
2,600✔
345
        suffix_len = new_length - x - need_space;
2,600✔
346
        memcpy(t + x + 3, s + old_length - suffix_len, suffix_len);
2,600✔
347
        *(t + x + 3 + suffix_len) = '\0';
2,600✔
348

349
        return t;
2,600✔
350
}
351

352
char* ellipsize_mem(const char *s, size_t old_length, size_t new_length, unsigned percent) {
8,156✔
353
        size_t x, k, len, len2;
8,156✔
354
        const char *i, *j;
8,156✔
355
        int r;
8,156✔
356

357
        /* Note that 'old_length' refers to bytes in the string, while 'new_length' refers to character cells taken up
358
         * on screen. This distinction doesn't matter for ASCII strings, but it does matter for non-ASCII UTF-8
359
         * strings.
360
         *
361
         * Ellipsation is done in a locale-dependent way:
362
         * 1. If the string passed in is fully ASCII and the current locale is not UTF-8, three dots are used ("...")
363
         * 2. Otherwise, a unicode ellipsis is used ("…")
364
         *
365
         * In other words: you'll get a unicode ellipsis as soon as either the string contains non-ASCII characters or
366
         * the current locale is UTF-8.
367
         */
368

369
        assert(s);
8,156✔
370
        assert(percent <= 100);
8,156✔
371

372
        if (new_length == SIZE_MAX)
8,156✔
373
                return strndup(s, old_length);
×
374

375
        if (new_length == 0)
8,156✔
376
                return strdup("");
171✔
377

378
        bool has_ansi_seq = string_has_ansi_sequence(s, old_length);
7,985✔
379

380
        /* If no multibyte characters or ANSI sequences, use ascii_ellipsize_mem for speed */
381
        if (!has_ansi_seq && ascii_is_valid_n(s, old_length))
7,985✔
382
                return ascii_ellipsize_mem(s, old_length, new_length, percent);
4,432✔
383

384
        x = (new_length - 1) * percent / 100;
3,553✔
385
        assert(x <= new_length - 1);
3,553✔
386

387
        k = 0;
388
        for (i = s; i < s + old_length; ) {
49,919✔
389
                size_t slen = has_ansi_seq ? ansi_sequence_length(i, old_length - (i - s)) : 0;
48,727✔
390
                if (slen > 0) {
1,011✔
391
                        i += slen;
224✔
392
                        continue;  /* ANSI sequences don't take up any space in output */
224✔
393
                }
394

395
                char32_t c;
48,503✔
396
                r = utf8_encoded_to_unichar(i, &c);
48,503✔
397
                if (r < 0)
48,503✔
398
                        return NULL;
×
399

400
                int w = unichar_iswide(c) ? 2 : 1;
48,503✔
401
                if (k + w > x)
48,503✔
402
                        break;
403

404
                k += w;
46,142✔
405
                i += r;
46,142✔
406
        }
407

408
        const char *ansi_start = s + old_length;
3,553✔
409
        size_t ansi_len = 0;
3,553✔
410

411
        for (const char *t = j = s + old_length; t > i && k < new_length; ) {
13,240✔
412
                char32_t c;
9,773✔
413
                int w;
9,773✔
414
                const char *tt;
9,773✔
415

416
                if (has_ansi_seq && ansi_start >= t)
9,773✔
417
                        /* Figure out the previous ANSI sequence, if any */
418
                        ansi_len = previous_ansi_sequence(s, t - s, &ansi_start);
265✔
419

420
                /* If the sequence extends all the way to the current position, skip it. */
421
                if (has_ansi_seq && ansi_len > 0 && ansi_start + ansi_len == t) {
9,773✔
422
                        t = ansi_start;
112✔
423
                        continue;
112✔
424
                }
425

426
                tt = utf8_prev_char(t);
9,661✔
427
                r = utf8_encoded_to_unichar(tt, &c);
9,661✔
428
                if (r < 0)
9,661✔
429
                        return NULL;
×
430

431
                w = unichar_iswide(c) ? 2 : 1;
9,661✔
432
                if (k + w > new_length)
9,661✔
433
                        break;
434

435
                k += w;
9,575✔
436
                j = t = tt;  /* j should always point to the first "real" character */
9,575✔
437
        }
438

439
        /* We don't actually need to ellipsize */
440
        if (i >= j)
3,553✔
441
                return memdup_suffix0(s, old_length);
1,720✔
442

443
        if (k >= new_length) {
1,833✔
444
                /* Make space for ellipsis, if required and possible. We know that the edge character is not
445
                 * part of an ANSI sequence (because then we'd skip it). If the last character we looked at
446
                 * was wide, we don't need to make space. */
447
                if (j < s + old_length)
1,747✔
448
                        j = utf8_next_char(j);
1,747✔
449
                else if (i > s)
×
450
                        i = utf8_prev_char(i);
×
451
        }
452

453
        len = i - s;
1,833✔
454
        len2 = s + old_length - j;
1,833✔
455

456
        /* If we have ANSI, allow the same length as the source string + ellipsis. It'd be too involved to
457
         * figure out what exact space is needed. Strings with ANSI sequences are most likely to be fairly
458
         * short anyway. */
459
        size_t alloc_len = has_ansi_seq ? old_length + 3 + 1 : len + 3 + len2 + 1;
1,833✔
460

461
        char *e = new(char, alloc_len);
1,833✔
462
        if (!e)
1,833✔
463
                return NULL;
464

465
        memcpy_safe(e, s, len);
1,833✔
466
        write_ellipsis(e + len, /* unicode= */ true);
1,833✔
467

468
        char *dst = e + len + 3;
1,833✔
469

470
        if (has_ansi_seq)
1,833✔
471
                /* Copy over any ANSI sequences in full */
472
                for (const char *p = s + len; p < j; ) {
1,864✔
473
                        size_t slen = ansi_sequence_length(p, j - p);
1,729✔
474
                        if (slen > 0) {
1,729✔
475
                                dst = mempcpy(dst, p, slen);
262✔
476
                                p += slen;
262✔
477
                        } else
478
                                p = utf8_next_char(p);
1,467✔
479
                }
480

481
        memcpy_safe(dst, j, len2);
1,833✔
482
        dst[len2] = '\0';
1,833✔
483

484
        return e;
1,833✔
485
}
486

487
char* cellescape(char *buf, size_t len, const char *s) {
42,848✔
488
        /* Escape and ellipsize s into buffer buf of size len. Only non-control ASCII
489
         * characters are copied as they are, everything else is escaped. The result
490
         * is different then if escaping and ellipsization was performed in two
491
         * separate steps, because each sequence is either stored in full or skipped.
492
         *
493
         * This function should be used for logging about strings which expected to
494
         * be plain ASCII in a safe way.
495
         *
496
         * An ellipsis will be used if s is too long. It was always placed at the
497
         * very end.
498
         */
499

500
        size_t i = 0, last_char_width[4] = {}, k = 0;
42,848✔
501

502
        assert(buf);
42,848✔
503
        assert(len > 0); /* at least a terminating NUL */
42,848✔
504
        assert(s);
42,848✔
505

506
        for (;;) {
944,558✔
507
                char four[4];
493,703✔
508
                int w;
493,703✔
509

510
                if (*s == 0) /* terminating NUL detected? then we are done! */
493,703✔
511
                        goto done;
42,812✔
512

513
                w = cescape_char(*s, four);
450,891✔
514
                if (i + w + 1 > len) /* This character doesn't fit into the buffer anymore? In that case let's
450,891✔
515
                                      * ellipsize at the previous location */
516
                        break;
517

518
                /* OK, there was space, let's add this escaped character to the buffer */
519
                memcpy(buf + i, four, w);
450,855✔
520
                i += w;
450,855✔
521

522
                /* And remember its width in the ring buffer */
523
                last_char_width[k] = w;
450,855✔
524
                k = (k + 1) % 4;
450,855✔
525

526
                s++;
450,855✔
527
        }
528

529
        /* Ellipsation is necessary. This means we might need to truncate the string again to make space for 4
530
         * characters ideally, but the buffer is shorter than that in the first place take what we can get */
531
        for (size_t j = 0; j < ELEMENTSOF(last_char_width); j++) {
81✔
532

533
                if (i + 4 <= len) /* nice, we reached our space goal */
81✔
534
                        break;
535

536
                k = k == 0 ? 3 : k - 1;
57✔
537
                if (last_char_width[k] == 0) /* bummer, we reached the beginning of the strings */
57✔
538
                        break;
539

540
                assert(i >= last_char_width[k]);
45✔
541
                i -= last_char_width[k];
45✔
542
        }
543

544
        if (i + 4 <= len) /* yay, enough space */
36✔
545
                i += write_ellipsis(buf + i, /* unicode= */ false);
24✔
546
        else if (i + 3 <= len) { /* only space for ".." */
12✔
547
                buf[i++] = '.';
4✔
548
                buf[i++] = '.';
4✔
549
        } else if (i + 2 <= len) /* only space for a single "." */
8✔
550
                buf[i++] = '.';
4✔
551
        else
552
                assert(i + 1 <= len);
4✔
553

554
done:
4✔
555
        buf[i] = '\0';
42,848✔
556
        return buf;
42,848✔
557
}
558

559
char* strshorten(char *s, size_t l) {
360,801✔
560
        assert(s);
360,801✔
561

562
        if (l >= SIZE_MAX-1) /* Would not change anything */
360,801✔
563
                return s;
564

565
        if (strnlen(s, l+1) > l)
360,799✔
566
                s[l] = 0;
108✔
567

568
        return s;
569
}
570

571
int strgrowpad0(char **s, size_t l) {
26✔
572
        size_t sz;
26✔
573

574
        assert(s);
26✔
575

576
        if (*s) {
26✔
577
                sz = strlen(*s) + 1;
26✔
578
                if (sz >= l) /* never shrink */
26✔
579
                        return 0;
580
        } else
581
                sz = 0;
582

583
        char *q = realloc(*s, l);
26✔
584
        if (!q)
26✔
585
                return -ENOMEM;
586

587
        *s = q;
26✔
588

589
        memzero(*s + sz, l - sz);
26✔
590
        return 0;
591
}
592

593
char* strreplace(const char *text, const char *old_string, const char *new_string) {
4,595✔
594
        size_t l, old_len, new_len;
4,595✔
595
        char *t, *ret = NULL;
4,595✔
596
        const char *f;
4,595✔
597

598
        assert(old_string);
4,595✔
599
        assert(new_string);
4,595✔
600

601
        if (!text)
4,595✔
602
                return NULL;
4,595✔
603

604
        old_len = strlen(old_string);
4,594✔
605
        new_len = strlen(new_string);
4,594✔
606

607
        l = strlen(text);
4,594✔
608
        if (!GREEDY_REALLOC(ret, l+1))
4,594✔
609
                return NULL;
610

611
        f = text;
4,594✔
612
        t = ret;
4,594✔
613
        while (*f) {
160,905✔
614
                size_t d, nl;
156,311✔
615

616
                if (!startswith(f, old_string)) {
156,311✔
617
                        *(t++) = *(f++);
154,107✔
618
                        continue;
154,107✔
619
                }
620

621
                d = t - ret;
2,204✔
622
                nl = l - old_len + new_len;
2,204✔
623

624
                if (!GREEDY_REALLOC(ret, nl + 1))
2,204✔
625
                        return mfree(ret);
×
626

627
                l = nl;
2,204✔
628
                t = ret + d;
2,204✔
629

630
                t = stpcpy(t, new_string);
2,204✔
631
                f += old_len;
2,204✔
632
        }
633

634
        *t = 0;
4,594✔
635
        return ret;
4,594✔
636
}
637

638
static void advance_offsets(
406✔
639
                ssize_t diff,
640
                size_t offsets[2], /* note: we can't use [static 2] here, since this may be NULL */
641
                size_t shift[static 2],
642
                size_t size) {
643

644
        if (!offsets)
406✔
645
                return;
646

647
        assert(shift);
396✔
648

649
        if ((size_t) diff < offsets[0])
396✔
650
                shift[0] += size;
×
651
        if ((size_t) diff < offsets[1])
396✔
652
                shift[1] += size;
×
653
}
654

655
char* strip_tab_ansi(char **ibuf, size_t *_isz, size_t highlight[2]) {
180,351✔
656
        const char *begin = NULL;
180,351✔
657
        enum {
180,351✔
658
                STATE_OTHER,
659
                STATE_ESCAPE,
660
                STATE_CSI,
661
                STATE_OSC,
662
                STATE_OSC_CLOSING,
663
        } state = STATE_OTHER;
180,351✔
664
        _cleanup_(memstream_done) MemStream m = {};
180,351✔
665
        size_t isz, shift[2] = {}, n_carriage_returns = 0;
180,351✔
666
        FILE *f;
180,351✔
667

668
        assert(ibuf);
180,351✔
669
        assert(*ibuf);
180,351✔
670
        POINTER_MAY_BE_NULL(_isz);
180,351✔
671

672
        /* This does three things:
673
         *
674
         * 1. Replaces TABs by 8 spaces
675
         * 2. Strips ANSI color sequences (a subset of CSI), i.e. ESC '[' … 'm' sequences
676
         * 3. Strips ANSI operating system sequences (OSC), i.e. ESC ']' … ST sequences
677
         * 4. Strip trailing \r characters (since they would "move the cursor", but have no
678
         *    other effect).
679
         *
680
         * Everything else will be left as it is. In particular other ANSI sequences are left as they are, as
681
         * are any other special characters. Truncated ANSI sequences are left-as is too. This call is
682
         * supposed to suppress the most basic formatting noise, but nothing else.
683
         *
684
         * Why care for OSC sequences? Well, to undo what terminal_urlify() and friends generate. */
685

686
        isz = _isz ? *_isz : strlen(*ibuf);
180,351✔
687

688
        /* Note we turn off internal locking on f for performance reasons. It's safe to do so since we
689
         * created f here and it doesn't leave our scope. */
690
        f = memstream_init(&m);
180,351✔
691
        if (!f)
180,351✔
692
                return NULL;
693

694
        for (const char *i = *ibuf; i < *ibuf + isz + 1; i++) {
21,553,476✔
695

696
                bool eot = i >= *ibuf + isz;
21,373,125✔
697

698
                switch (state) {
21,373,125✔
699

700
                case STATE_OTHER:
21,372,874✔
701
                        if (eot)
21,372,874✔
702
                                break;
703

704
                        if (*i == '\r') {
21,192,523✔
705
                                n_carriage_returns++;
7✔
706
                                break;
7✔
707
                        } else if (*i == '\n')
21,192,516✔
708
                                /* Ignore carriage returns before new line */
709
                                n_carriage_returns = 0;
38✔
710
                        for (; n_carriage_returns > 0; n_carriage_returns--)
21,192,518✔
711
                                fputc('\r', f);
2✔
712

713
                        if (*i == '\x1B')
21,192,516✔
714
                                state = STATE_ESCAPE;
715
                        else if (*i == '\t') {
21,192,482✔
716
                                fputs("        ", f);
401✔
717
                                advance_offsets(i - *ibuf, highlight, shift, 7);
401✔
718
                        } else
719
                                fputc(*i, f);
21,192,081✔
720

721
                        break;
722

723
                case STATE_ESCAPE:
34✔
724
                        assert(n_carriage_returns == 0);
34✔
725

726
                        if (eot) {
34✔
727
                                fputc('\x1B', f);
×
728
                                advance_offsets(i - *ibuf, highlight, shift, 1);
×
729
                                break;
730
                        } else if (*i == '[') { /* ANSI CSI */
34✔
731
                                state = STATE_CSI;
31✔
732
                                begin = i + 1;
31✔
733
                        } else if (*i == ']') { /* ANSI OSC */
3✔
734
                                state = STATE_OSC;
3✔
735
                                begin = i + 1;
3✔
736
                        } else {
737
                                fputc('\x1B', f);
×
738
                                fputc(*i, f);
×
739
                                advance_offsets(i - *ibuf, highlight, shift, 1);
×
740
                                state = STATE_OTHER;
×
741
                        }
742

743
                        break;
744

745
                case STATE_CSI:
191✔
746
                        assert(n_carriage_returns == 0);
191✔
747

748
                        if (eot || !strchr(DIGITS ";:m", *i)) { /* EOT or invalid chars in sequence */
191✔
749
                                fputc('\x1B', f);
5✔
750
                                fputc('[', f);
5✔
751
                                advance_offsets(i - *ibuf, highlight, shift, 2);
5✔
752
                                state = STATE_OTHER;
5✔
753
                                i = begin-1;
5✔
754
                        } else if (*i == 'm')
186✔
755
                                state = STATE_OTHER;
26✔
756

757
                        break;
758

759
                case STATE_OSC:
24✔
760
                        assert(n_carriage_returns == 0);
24✔
761

762
                        /* There are three kinds of OSC terminators: \x07, \x1b\x5c or \x9c. We only support
763
                         * the first two, because the last one is a valid UTF-8 codepoint and hence creates
764
                         * an ambiguity (many Terminal emulators refuse to support it as well). */
765
                        if (eot || (!IN_SET(*i, '\x07', '\x1b') && !osc_char_is_valid(*i))) { /* EOT or invalid chars in sequence */
24✔
766
                                fputc('\x1B', f);
×
767
                                fputc(']', f);
×
768
                                advance_offsets(i - *ibuf, highlight, shift, 2);
×
769
                                state = STATE_OTHER;
×
770
                                i = begin-1;
×
771
                        } else if (*i == '\x07') /* Single character ST */
24✔
772
                                state = STATE_OTHER;
773
                        else if (*i == '\x1B')
23✔
774
                                state = STATE_OSC_CLOSING;
2✔
775

776
                        break;
777

778
                case STATE_OSC_CLOSING:
2✔
779
                        if (eot || *i != '\x5c') { /* EOT or incomplete two-byte ST in sequence */
2✔
780
                                fputc('\x1B', f);
×
781
                                fputc(']', f);
×
782
                                advance_offsets(i - *ibuf, highlight, shift, 2);
×
783
                                state = STATE_OTHER;
×
784
                                i = begin-1;
×
785
                        } else if (*i == '\x5c')
786
                                state = STATE_OTHER;
787

788
                        break;
789
                }
790
        }
791

792
        char *obuf;
180,351✔
793
        if (memstream_finalize(&m, &obuf, _isz) < 0)
180,351✔
794
                return NULL;
795

796
        free_and_replace(*ibuf, obuf);
180,351✔
797

798
        if (highlight) {
180,351✔
799
                highlight[0] += shift[0];
180,324✔
800
                highlight[1] += shift[1];
180,324✔
801
        }
802

803
        return *ibuf;
180,351✔
804
}
805

806
char* strextendv_with_separator(char **x, const char *separator, va_list ap) {
23,449,628✔
807
        _cleanup_free_ char *buffer = NULL;
23,449,628✔
808
        size_t f, l, l_separator;
23,449,628✔
809
        bool need_separator;
23,449,628✔
810
        char *nr, *p;
23,449,628✔
811

812
        if (!x)
23,449,628✔
813
                x = &buffer;
22,124,363✔
814

815
        l = f = strlen_ptr(*x);
23,449,628✔
816

817
        need_separator = !isempty(*x);
23,449,628✔
818
        l_separator = strlen_ptr(separator);
23,449,628✔
819

820
        va_list aq;
23,449,628✔
821
        va_copy(aq, ap);
23,449,628✔
822
        for (const char *t;;) {
89,108,308✔
823
                size_t n;
89,108,308✔
824

825
                t = va_arg(aq, const char *);
89,108,308✔
826
                if (!t)
89,108,308✔
827
                        break;
828
                if (t == POINTER_MAX)
65,658,680✔
829
                        continue;
3✔
830

831
                n = strlen(t);
65,658,677✔
832

833
                if (need_separator)
65,658,677✔
834
                        n += l_separator;
42,960,241✔
835

836
                if (n >= SIZE_MAX - l) {
65,658,677✔
837
                        va_end(aq);
×
838
                        return NULL;
×
839
                }
840

841
                l += n;
65,658,677✔
842
                need_separator = true;
65,658,677✔
843
        }
844
        va_end(aq);
23,449,628✔
845

846
        need_separator = !isempty(*x);
23,449,628✔
847

848
        nr = realloc(*x, GREEDY_ALLOC_ROUND_UP(l+1));
23,449,628✔
849
        if (!nr)
23,449,628✔
850
                return NULL;
851

852
        *x = nr;
23,449,628✔
853
        p = nr + f;
23,449,628✔
854

855
        for (;;) {
89,108,308✔
856
                const char *t;
89,108,308✔
857

858
                t = va_arg(ap, const char *);
89,108,308✔
859
                if (!t)
89,108,308✔
860
                        break;
861
                if (t == POINTER_MAX)
65,658,680✔
862
                        continue;
3✔
863

864
                if (need_separator && separator)
65,658,677✔
865
                        p = stpcpy(p, separator);
160,023✔
866

867
                p = stpcpy(p, t);
65,658,677✔
868

869
                need_separator = true;
65,658,677✔
870
        }
871

872
        assert(p == nr + l);
23,449,628✔
873
        *p = 0;
23,449,628✔
874

875
        /* If no buffer to extend was passed in return the start of the buffer */
876
        if (buffer)
23,449,628✔
877
                return TAKE_PTR(buffer);
22,124,363✔
878

879
        /* Otherwise we extended the buffer: return the end */
880
        return p;
881
}
882

883
char* strextend_with_separator_internal(char **x, const char *separator, ...) {
23,397,983✔
884
        va_list ap;
23,397,983✔
885
        char *ret;
23,397,983✔
886

887
        va_start(ap, separator);
23,397,983✔
888
        ret = strextendv_with_separator(x, separator, ap);
23,397,983✔
889
        va_end(ap);
23,397,983✔
890

891
        return ret;
23,397,983✔
892
}
893

894
int strextendf_with_separator(char **x, const char *separator, const char *format, ...) {
17,875✔
895
        size_t m, a, l_separator;
17,875✔
896
        va_list ap;
17,875✔
897
        int l;
17,875✔
898

899
        /* Appends a formatted string to the specified string. Don't use this in inner loops, since then
900
         * we'll spend a tonload of time in determining the length of the string passed in, over and over
901
         * again. */
902

903
        assert(x);
17,875✔
904
        assert(format);
17,875✔
905

906
        l_separator = isempty(*x) ? 0 : strlen_ptr(separator);
31,111✔
907

908
        /* Let's try to use the allocated buffer, if there's room at the end still. Otherwise let's extend by 64 chars. */
909
        if (*x) {
17,875✔
910
                m = strlen(*x);
13,236✔
911
                a = MALLOC_SIZEOF_SAFE(*x);
13,236✔
912
                assert(a >= m + 1);
13,236✔
913
        } else
914
                m = a = 0;
915

916
        if (a - m < 17 + l_separator) { /* if there's less than 16 chars space, then enlarge the buffer first */
17,875✔
917
                char *n;
11,001✔
918

919
                if (_unlikely_(l_separator > SIZE_MAX - 64)) /* overflow check #1 */
11,001✔
920
                        return -ENOMEM;
17,875✔
921
                if (_unlikely_(m > SIZE_MAX - 64 - l_separator)) /* overflow check #2 */
11,001✔
922
                        return -ENOMEM;
923

924
                n = realloc(*x, m + 64 + l_separator);
11,001✔
925
                if (!n)
11,001✔
926
                        return -ENOMEM;
927

928
                *x = n;
11,001✔
929
                a = MALLOC_SIZEOF_SAFE(*x);
11,001✔
930
        }
931

932
        /* Now, let's try to format the string into it */
933
        memcpy_safe(*x + m, separator, l_separator);
17,875✔
934
        va_start(ap, format);
17,875✔
935
        l = vsnprintf(*x + m + l_separator, a - m - l_separator, format, ap);
17,875✔
936
        va_end(ap);
17,875✔
937

938
        assert(l >= 0);
17,875✔
939

940
        if ((size_t) l < a - m - l_separator) {
17,875✔
941
                char *n;
17,378✔
942

943
                /* Nice! This worked. We are done. But first, let's return the extra space we don't
944
                 * need. This should be a cheap operation, since we only lower the allocation size here,
945
                 * never increase. */
946
                n = realloc(*x, m + (size_t) l + l_separator + 1);
17,378✔
947
                if (n)
17,378✔
948
                        *x = n;
17,378✔
949
        } else {
950
                char *n;
497✔
951

952
                /* Wasn't enough. Then let's allocate exactly what we need. */
953

954
                if (_unlikely_((size_t) l > SIZE_MAX - (l_separator + 1))) /* overflow check #1 */
497✔
955
                        goto oom;
×
956
                if (_unlikely_(m > SIZE_MAX - ((size_t) l + l_separator + 1))) /* overflow check #2 */
497✔
957
                        goto oom;
×
958

959
                a = m + (size_t) l + l_separator + 1;
497✔
960
                n = realloc(*x, a);
497✔
961
                if (!n)
497✔
962
                        goto oom;
×
963
                *x = n;
497✔
964

965
                va_start(ap, format);
497✔
966
                l = vsnprintf(*x + m + l_separator, a - m - l_separator, format, ap);
497✔
967
                va_end(ap);
497✔
968

969
                assert((size_t) l < a - m - l_separator);
497✔
970
        }
971

972
        return 0;
973

974
oom:
×
975
        /* truncate the bytes added after memcpy_safe() again */
976
        (*x)[m] = 0;
×
977
        return -ENOMEM;
×
978
}
979

980
char* strrep(const char *s, unsigned n) {
11✔
981
        char *r, *p;
11✔
982
        size_t l;
11✔
983

984
        assert(s);
11✔
985

986
        l = strlen(s);
11✔
987
        p = r = malloc(l * n + 1);
11✔
988
        if (!r)
11✔
989
                return NULL;
990

991
        for (unsigned i = 0; i < n; i++)
40,337✔
992
                p = stpcpy(p, s);
40,326✔
993

994
        *p = 0;
11✔
995
        return r;
11✔
996
}
997

998
int split_pair(const char *s, const char *sep, char **ret_first, char **ret_second) {
1,564✔
999
        assert(s);
1,564✔
1000
        assert(!isempty(sep));
1,564✔
1001
        assert(ret_first);
1,564✔
1002
        assert(ret_second);
1,564✔
1003

1004
        const char *x = strstr(s, sep);
1,564✔
1005
        if (!x)
1,564✔
1006
                return -EINVAL;
1,564✔
1007

1008
        _cleanup_free_ char *a = strndup(s, x - s);
262✔
1009
        if (!a)
262✔
1010
                return -ENOMEM;
1011

1012
        _cleanup_free_ char *b = strdup(x + strlen(sep));
262✔
1013
        if (!b)
262✔
1014
                return -ENOMEM;
1015

1016
        *ret_first = TAKE_PTR(a);
262✔
1017
        *ret_second = TAKE_PTR(b);
262✔
1018
        return 0;
262✔
1019
}
1020

1021
int free_and_strdup(char **p, const char *s) {
2,154,818✔
1022
        char *t;
2,154,818✔
1023

1024
        assert(p);
2,154,818✔
1025

1026
        /* Replaces a string pointer with a strdup()ed new string,
1027
         * possibly freeing the old one. */
1028

1029
        if (streq_ptr(*p, s))
2,154,818✔
1030
                return 0;
2,154,818✔
1031

1032
        if (s) {
1,706,261✔
1033
                t = strdup(s);
1,698,200✔
1034
                if (!t)
1,698,200✔
1035
                        return -ENOMEM;
1036
        } else
1037
                t = NULL;
1038

1039
        free_and_replace(*p, t);
1,706,261✔
1040

1041
        return 1;
1,706,261✔
1042
}
1043

1044
int free_and_strdup_warn(char **p, const char *s) {
62,739✔
1045
        int r;
62,739✔
1046

1047
        r = free_and_strdup(p, s);
62,739✔
1048
        if (r < 0)
62,739✔
1049
                return log_oom();
×
1050
        return r;
1051
}
1052

1053
int free_and_strndup(char **p, const char *s, size_t l) {
70,009✔
1054
        char *t;
70,009✔
1055

1056
        assert(p);
70,009✔
1057
        assert(s || l == 0);
70,009✔
1058

1059
        /* Replaces a string pointer with a strndup()ed new string,
1060
         * freeing the old one. */
1061

1062
        if (!*p && !s)
70,009✔
1063
                return 0;
70,009✔
1064

1065
        if (*p && s && strneq(*p, s, l) && (l > strlen(*p) || (*p)[l] == '\0'))
70,009✔
1066
                return 0;
1067

1068
        if (s) {
32,659✔
1069
                t = strndup(s, l);
32,718✔
1070
                if (!t)
32,718✔
1071
                        return -ENOMEM;
1072
        } else
1073
                t = NULL;
1074

1075
        free_and_replace(*p, t);
32,719✔
1076
        return 1;
32,719✔
1077
}
1078

1079
int strdup_to_full(char **ret, const char *src) {
807,396✔
1080
        if (!src) {
807,396✔
1081
                if (ret)
279,714✔
1082
                        *ret = NULL;
279,713✔
1083

1084
                return 0;
279,714✔
1085
        } else {
1086
                if (ret) {
527,682✔
1087
                        char *t = strdup(src);
527,680✔
1088
                        if (!t)
527,680✔
1089
                                return -ENOMEM;
1090
                        *ret = t;
527,680✔
1091
                }
1092

1093
                return 1;
527,682✔
1094
        }
1095
};
1096

1097
bool string_is_safe(const char *p) {
197,363✔
1098
        if (!p)
197,363✔
1099
                return false;
1100

1101
        /* Checks if the specified string contains no quotes or control characters */
1102

1103
        for (const char *t = p; *t; t++) {
2,308,060✔
1104
                if (*t > 0 && *t < ' ') /* no control characters */
2,110,710✔
1105
                        return false;
1106

1107
                if (strchr(QUOTES "\\\x7f", *t))
2,110,702✔
1108
                        return false;
1109
        }
1110

1111
        return true;
1112
}
1113

1114
bool string_is_safe_ascii(const char *p) {
×
1115
        return ascii_is_valid(p) && string_is_safe(p);
×
1116
}
1117

1118
char* str_realloc(char *p) {
42,423✔
1119
        /* Reallocate *p to actual size. Ignore failure, and return the original string on error. */
1120

1121
        if (!p)
42,423✔
1122
                return NULL;
1123

1124
        return realloc(p, strlen(p) + 1) ?: p;
42,423✔
1125
}
1126

1127
char* string_erase(char *x) {
119✔
1128
        if (!x)
119✔
1129
                return NULL;
1130

1131
        /* A delicious drop of snake-oil! To be called on memory where we stored passphrases or so, after we
1132
         * used them. */
1133
        explicit_bzero_safe(x, strlen(x));
119✔
1134
        return x;
119✔
1135
}
1136

1137
int string_truncate_lines(const char *s, size_t n_lines, char **ret) {
300✔
1138
        const char *p = s, *e = s;
300✔
1139
        bool truncation_applied = false;
300✔
1140
        char *copy;
300✔
1141
        size_t n = 0;
300✔
1142

1143
        assert(s);
300✔
1144
        assert(ret);
300✔
1145

1146
        /* Truncate after the specified number of lines. Returns > 0 if a truncation was applied or == 0 if
1147
         * there were fewer lines in the string anyway. Trailing newlines on input are ignored, and not
1148
         * generated either. */
1149

1150
        for (;;) {
530✔
1151
                size_t k;
415✔
1152

1153
                k = strcspn(p, "\n");
415✔
1154

1155
                if (p[k] == 0) {
415✔
1156
                        if (k == 0) /* final empty line */
268✔
1157
                                break;
1158

1159
                        if (n >= n_lines) /* above threshold */
243✔
1160
                                break;
1161

1162
                        e = p + k; /* last line to include */
226✔
1163
                        break;
226✔
1164
                }
1165

1166
                assert(p[k] == '\n');
147✔
1167

1168
                if (n >= n_lines)
147✔
1169
                        break;
1170

1171
                if (k > 0)
115✔
1172
                        e = p + k;
92✔
1173

1174
                p += k + 1;
115✔
1175
                n++;
115✔
1176
        }
1177

1178
        /* e points after the last character we want to keep */
1179
        if (isempty(e))
300✔
1180
                copy = strdup(s);
230✔
1181
        else {
1182
                if (!in_charset(e, "\n")) /* We only consider things truncated if we remove something that
70✔
1183
                                           * isn't a new-line or a series of them */
1184
                        truncation_applied = true;
42✔
1185

1186
                copy = strndup(s, e - s);
70✔
1187
        }
1188
        if (!copy)
300✔
1189
                return -ENOMEM;
1190

1191
        *ret = copy;
300✔
1192
        return truncation_applied;
300✔
1193
}
1194

1195
int string_extract_line(const char *s, size_t i, char **ret) {
211,195✔
1196
        const char *p = s;
211,195✔
1197
        size_t c = 0;
211,195✔
1198

1199
        assert(ret);
211,195✔
1200

1201
        /* Extract the i'nth line from the specified string. Returns > 0 if there are more lines after that,
1202
         * and == 0 if we are looking at the last line or already beyond the last line. As special
1203
         * optimization, if the first line is requested and the string only consists of one line we return
1204
         * NULL, indicating the input string should be used as is, and avoid a memory allocation for a very
1205
         * common case. */
1206

1207
        for (;;) {
398,421✔
1208
                const char *q;
304,808✔
1209

1210
                q = strchr(p, '\n');
304,808✔
1211
                if (i == c) {
304,808✔
1212
                        /* The line we are looking for! */
1213

1214
                        if (q) {
207,634✔
1215
                                char *m;
3,695✔
1216

1217
                                m = strndup(p, q - p);
3,695✔
1218
                                if (!m)
3,695✔
1219
                                        return -ENOMEM;
1220

1221
                                *ret = m;
3,695✔
1222
                                return !isempty(q + 1); /* More coming? */
7,390✔
1223
                        } else
1224
                                /* Tell the caller to use the input string if equal */
1225
                                return strdup_to(ret, p != s ? p : NULL);
406,230✔
1226
                }
1227

1228
                if (!q)
97,174✔
1229
                        /* No more lines, return empty line */
1230
                        return strdup_to(ret, "");
3,561✔
1231

1232
                p = q + 1;
93,613✔
1233
                c++;
93,613✔
1234
        }
1235
}
1236

1237
int string_contains_word_strv(const char *string, const char *separators, char * const *words, const char **ret_word) {
71✔
1238
        /* In the default mode with no separators specified, we split on whitespace and coalesce separators. */
1239
        const ExtractFlags flags = separators ? EXTRACT_DONT_COALESCE_SEPARATORS : 0;
71✔
1240
        const char *found = NULL;
71✔
1241
        int r;
196✔
1242

1243
        for (;;) {
321✔
1244
                _cleanup_free_ char *w = NULL;
125✔
1245

1246
                r = extract_first_word(&string, &w, separators, flags);
196✔
1247
                if (r < 0)
196✔
1248
                        return r;
×
1249
                if (r == 0)
196✔
1250
                        break;
1251

1252
                found = strv_find(words, w);
146✔
1253
                if (found)
146✔
1254
                        break;
1255
        }
1256

1257
        if (ret_word)
71✔
1258
                *ret_word = found;
7✔
1259
        return !!found;
71✔
1260
}
1261

1262
bool streq_skip_trailing_chars(const char *s1, const char *s2, const char *ok) {
8,808✔
1263
        if (!s1 && !s2)
8,808✔
1264
                return true;
1265
        if (!s1 || !s2)
8,807✔
1266
                return false;
1267

1268
        if (!ok)
8,805✔
1269
                ok = WHITESPACE;
13✔
1270

1271
        for (; *s1 && *s2; s1++, s2++)
16,665✔
1272
                if (*s1 != *s2)
10,052✔
1273
                        break;
1274

1275
        return in_charset(s1, ok) && in_charset(s2, ok);
11,003✔
1276
}
1277

1278
char* string_replace_char(char *str, char old_char, char new_char) {
674,556✔
1279
        assert(str);
674,556✔
1280
        assert(old_char != '\0');
674,556✔
1281
        assert(new_char != '\0');
674,556✔
1282
        assert(old_char != new_char);
674,556✔
1283

1284
        for (char *p = strchr(str, old_char); p; p = strchr(p + 1, old_char))
679,755✔
1285
                *p = new_char;
5,199✔
1286

1287
        return str;
674,556✔
1288
}
1289

1290
int make_cstring(const char *s, size_t n, MakeCStringMode mode, char **ret) {
2,418✔
1291
        char *b;
2,418✔
1292

1293
        assert(s || n == 0);
2,418✔
1294
        assert(mode >= 0);
2,418✔
1295
        assert(mode < _MAKE_CSTRING_MODE_MAX);
2,418✔
1296

1297
        /* Converts a sized character buffer into a NUL-terminated NUL string, refusing if there are embedded
1298
         * NUL bytes. Whether to expect a trailing NUL byte can be specified via 'mode' */
1299

1300
        if (n == 0) {
2,418✔
1301
                if (mode == MAKE_CSTRING_REQUIRE_TRAILING_NUL)
20✔
1302
                        return -EINVAL;
1303

1304
                if (!ret)
19✔
1305
                        return 0;
1306

1307
                b = new0(char, 1);
19✔
1308
        } else {
1309
                const char *nul;
2,398✔
1310

1311
                nul = memchr(s, 0, n);
2,398✔
1312
                if (nul) {
2,398✔
1313
                        if (nul < s + n - 1 || /* embedded NUL? */
15✔
1314
                            mode == MAKE_CSTRING_REFUSE_TRAILING_NUL)
1315
                                return -EINVAL;
1316

1317
                        n--;
1318
                } else if (mode == MAKE_CSTRING_REQUIRE_TRAILING_NUL)
2,383✔
1319
                        return -EINVAL;
1320

1321
                if (!ret)
2,387✔
1322
                        return 0;
1323

1324
                b = memdup_suffix0(s, n);
2,387✔
1325
        }
1326
        if (!b)
2,406✔
1327
                return -ENOMEM;
1328

1329
        *ret = b;
2,406✔
1330
        return 0;
2,406✔
1331
}
1332

1333
size_t strspn_from_end(const char *str, const char *accept) {
638,850✔
1334
        size_t n = 0;
638,850✔
1335

1336
        if (isempty(str))
638,850✔
1337
                return 0;
1338

1339
        if (isempty(accept))
638,847✔
1340
                return 0;
1341

1342
        for (const char *p = str + strlen(str); p > str && strchr(accept, p[-1]); p--)
1,253,938✔
1343
                n++;
615,092✔
1344

1345
        return n;
1346
}
1347

1348
char* strdupspn(const char *a, const char *accept) {
×
1349
        if (isempty(a) || isempty(accept))
×
1350
                return strdup("");
×
1351

1352
        return strndup(a, strspn(a, accept));
×
1353
}
1354

1355
char* strdupcspn(const char *a, const char *reject) {
37,918✔
1356
        if (isempty(a))
37,918✔
1357
                return strdup("");
×
1358
        if (isempty(reject))
37,918✔
1359
                return strdup(a);
×
1360

1361
        return strndup(a, strcspn(a, reject));
37,918✔
1362
}
1363

1364
char* find_line_startswith_internal(const char *haystack, const char *needle) {
2,432✔
1365
        assert(haystack);
2,432✔
1366
        assert(needle);
2,432✔
1367

1368
        /* Finds the first line in 'haystack' that starts with the specified string. Returns a pointer to the
1369
         * first character after it */
1370

1371
        char *p = (char*) strstr(haystack, needle);
2,432✔
1372
        if (!p)
2,432✔
1373
                return NULL;
1374

1375
        if (p > haystack)
517✔
1376
                while (p[-1] != '\n') {
38✔
1377
                        p = strstr(p + 1, needle);
9✔
1378
                        if (!p)
9✔
1379
                                return NULL;
1380
                }
1381

1382
        return p + strlen(needle);
516✔
1383
}
1384

1385
char* find_line_internal(const char *haystack, const char *needle) {
×
1386
        assert(haystack);
×
1387
        assert(needle);
×
1388

1389
        /* Finds the first line in 'haystack' that match the specified string. Returns a pointer to the
1390
         * beginning of the line */
1391

1392
        char *p = (char*) find_line_startswith(haystack, needle);
×
1393
        if (!p)
×
1394
                return NULL;
1395

1396
        if (*p == 0 || strchr(NEWLINE, *p))
×
1397
                return p - strlen(needle);
×
1398

1399
        return NULL;
1400
}
1401

1402
char* find_line_after_internal(const char *haystack, const char *needle) {
×
1403
        assert(haystack);
×
1404
        assert(needle);
×
1405

1406
        /* Finds the first line in 'haystack' that match the specified string. Returns a pointer to the
1407
         * next line after it */
1408

1409
        char *p = (char*) find_line_startswith(haystack, needle);
×
1410
        if (!p)
×
1411
                return NULL;
1412

1413
        if (*p == 0)
×
1414
                return p;
1415
        if (strchr(NEWLINE, *p))
×
1416
                return p + 1;
×
1417

1418
        return NULL;
1419
}
1420

1421
bool version_is_valid(const char *s) {
19,087✔
1422
        if (isempty(s))
19,087✔
1423
                return false;
1424

1425
        if (!filename_part_is_valid(s))
19,085✔
1426
                return false;
1427

1428
        /* This is a superset of the characters used by semver. We additionally allow "," and "_". */
1429
        if (!in_charset(s, ALPHANUMERICAL ".,_-+"))
19,085✔
1430
                return false;
×
1431

1432
        return true;
1433
}
1434

1435
bool version_is_valid_versionspec(const char *s) {
94✔
1436
        if (!filename_part_is_valid(s))
94✔
1437
                return false;
1438

1439
        if (!in_charset(s, ALPHANUMERICAL "-.~^"))
94✔
1440
                return false;
×
1441

1442
        return true;
1443
}
1444

1445
ssize_t strlevenshtein(const char *x, const char *y) {
114✔
1446
        _cleanup_free_ size_t *t0 = NULL, *t1 = NULL, *t2 = NULL;
228✔
1447
        size_t xl, yl;
114✔
1448

1449
        /* This is inspired from the Linux kernel's Levenshtein implementation */
1450

1451
        if (streq_ptr(x, y))
114✔
1452
                return 0;
1453

1454
        xl = strlen_ptr(x);
110✔
1455
        if (xl > SSIZE_MAX)
109✔
1456
                return -E2BIG;
1457

1458
        yl = strlen_ptr(y);
110✔
1459
        if (yl > SSIZE_MAX)
109✔
1460
                return -E2BIG;
1461

1462
        if (isempty(x))
110✔
1463
                return yl;
3✔
1464
        if (isempty(y))
107✔
1465
                return xl;
1✔
1466

1467
        t0 = new0(size_t, yl + 1);
106✔
1468
        if (!t0)
106✔
1469
                return -ENOMEM;
1470
        t1 = new0(size_t, yl + 1);
106✔
1471
        if (!t1)
106✔
1472
                return -ENOMEM;
1473
        t2 = new0(size_t, yl + 1);
106✔
1474
        if (!t2)
106✔
1475
                return -ENOMEM;
1476

1477
        for (size_t i = 0; i <= yl; i++)
1,113✔
1478
                t1[i] = i;
1,007✔
1479

1480
        for (size_t i = 0; i < xl; i++) {
627✔
1481
                t2[0] = i + 1;
521✔
1482

1483
                for (size_t j = 0; j < yl; j++) {
5,378✔
1484
                        /* Substitution */
1485
                        t2[j+1] = t1[j] + (x[i] != y[j]);
4,857✔
1486

1487
                        /* Swap */
1488
                        if (i > 0 && j > 0 && x[i-1] == y[j] && x[i] == y[j-1] && t2[j+1] > t0[j-1] + 1)
4,857✔
1489
                                t2[j+1] = t0[j-1] + 1;
10✔
1490

1491
                        /* Deletion */
1492
                        if (t2[j+1] > t1[j+1] + 1)
4,857✔
1493
                                t2[j+1] = t1[j+1] + 1;
176✔
1494

1495
                        /* Insertion */
1496
                        if (t2[j+1] > t2[j] + 1)
4,857✔
1497
                                t2[j+1] = t2[j] + 1;
687✔
1498
                }
1499

1500
                size_t *dummy = t0;
1501
                t0 = t1;
1502
                t1 = t2;
1503
                t2 = dummy;
1504
        }
1505

1506
        return t1[yl];
106✔
1507
}
1508

1509
char* strrstr_internal(const char *haystack, const char *needle) {
25,251✔
1510
        /* Like strstr() but returns the last rather than the first occurrence of "needle" in "haystack". */
1511

1512
        if (!haystack || !needle)
25,251✔
1513
                return NULL;
1514

1515
        /* Special case: for the empty string we return the very last possible occurrence, i.e. *after* the
1516
         * last char, not before. */
1517
        if (*needle == 0)
25,248✔
1518
                return (char*) strchr(haystack, 0);
2✔
1519

1520
        for (const char *p = strstr(haystack, needle), *q; p; p = q) {
25,256✔
1521
                q = strstr(p + 1, needle);
25✔
1522
                if (!q)
25✔
1523
                        return (char *) p;
1524
        }
1525
        return NULL;
1526
}
1527

1528
size_t str_common_prefix(const char *a, const char *b) {
16✔
1529
        assert(a);
16✔
1530
        assert(b);
16✔
1531

1532
        /* Returns the length of the common prefix of the two specified strings, or SIZE_MAX in case the
1533
         * strings are fully identical. */
1534

1535
        for (size_t n = 0;; n++) {
39✔
1536
                char c = a[n];
55✔
1537
                if (c != b[n])
55✔
1538
                        return n;
1539
                if (c == 0)
42✔
1540
                        return SIZE_MAX;
1541
        }
1542
}
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