Debian Codegraph @

src/basic/string-util.c

raw
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) {
21 assert(s);
22 assert(word);
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))
28 return (char*) s;
29
30 const char *p = startswith(s, word);
31 if (!p)
32 return NULL;
33 if (*p == '\0')
34 return (char*) p;
35
36 const char *nw = skip_leading_chars(p, WHITESPACE);
37 if (p == nw)
38 return NULL;
39
40 return (char*) nw;
41 }
42
43 char* strextendn(char **x, const char *s, size_t l) {
44 assert(x);
45 assert(s || l == 0);
46
47 if (l > 0)
48 l = strnlen(s, l); /* ignore trailing noise */
49
50 if (l > 0 || !*x) {
51 size_t q;
52 char *m;
53
54 q = strlen_ptr(*x);
55 m = realloc(*x, q + l + 1);
56 if (!m)
57 return NULL;
58
59 *mempcpy_typesafe(m + q, s, l) = 0;
60
61 *x = m;
62 }
63
64 return *x;
65 }
66
67 char* strstrip(char *s) {
68 if (!s)
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);
74 }
75
76 char* delete_chars(char *s, const char *bad) {
77 char *f, *t;
78
79 /* Drops all specified bad characters, regardless where in the string */
80
81 if (!s)
82 return NULL;
83
84 if (!bad)
85 bad = WHITESPACE;
86
87 for (f = s, t = s; *f; f++) {
88 if (strchr(bad, *f))
89 continue;
90
91 *(t++) = *f;
92 }
93
94 *t = 0;
95
96 return s;
97 }
98
99 char* delete_trailing_chars(char *s, const char *bad) {
100 char *c = s;
101
102 /* Drops all specified bad characters, at the end of the string */
103
104 if (!s)
105 return NULL;
106
107 if (!bad)
108 bad = WHITESPACE;
109
110 for (char *p = s; *p; p++)
111 if (!strchr(bad, *p))
112 c = p + 1;
113
114 *c = 0;
115
116 return s;
117 }
118
119 char* truncate_nl_full(char *s, size_t *ret_len) {
120 size_t n;
121
122 assert(s);
123
124 n = strcspn(s, NEWLINE);
125 s[n] = '\0';
126 if (ret_len)
127 *ret_len = n;
128 return s;
129 }
130
131 char ascii_tolower(char x) {
132
133 if (x >= 'A' && x <= 'Z')
134 return x - 'A' + 'a';
135
136 return x;
137 }
138
139 char ascii_toupper(char x) {
140
141 if (x >= 'a' && x <= 'z')
142 return x - 'a' + 'A';
143
144 return x;
145 }
146
147 char* ascii_strlower(char *s) {
148 assert(s);
149
150 for (char *p = s; *p; p++)
151 *p = ascii_tolower(*p);
152
153 return s;
154 }
155
156 char* ascii_strupper(char *s) {
157 assert(s);
158
159 for (char *p = s; *p; p++)
160 *p = ascii_toupper(*p);
161
162 return s;
163 }
164
165 char* ascii_strlower_n(char *s, size_t n) {
166 if (n <= 0)
167 return s;
168
169 for (size_t i = 0; i < n; i++)
170 s[i] = ascii_tolower(s[i]);
171
172 return s;
173 }
174
175 int ascii_strcasecmp_n(const char *a, const char *b, size_t n) {
176
177 assert(a);
178 assert(b);
179
180 for (; n > 0; a++, b++, n--) {
181 int x, y;
182
183 x = (int) (uint8_t) ascii_tolower(*a);
184 y = (int) (uint8_t) ascii_tolower(*b);
185
186 if (x != y)
187 return x - y;
188 }
189
190 return 0;
191 }
192
193 int ascii_strcasecmp_nn(const char *a, size_t n, const char *b, size_t m) {
194 int r;
195
196 r = ascii_strcasecmp_n(a, b, MIN(n, m));
197 if (r != 0)
198 return r;
199
200 return CMP(n, m);
201 }
202
203 bool chars_intersect(const char *a, const char *b) {
204 /* Returns true if any of the chars in a are in b. */
205 for (const char *p = a; *p; p++)
206 if (strchr(b, *p))
207 return true;
208
209 return false;
210 }
211
212 bool string_has_cc(const char *p, const char *ok) {
213 assert(p);
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++) {
222 if (ok && strchr(ok, *t))
223 continue;
224
225 if (char_is_cc(*t))
226 return true;
227 }
228
229 return false;
230 }
231
232 static int write_ellipsis(char *buf, bool unicode) {
233 const char *s = glyph_full(GLYPH_ELLIPSIS, unicode);
234 assert(strlen(s) == 3);
235 memcpy(buf, s, 3);
236 return 3;
237 }
238
239 static size_t ansi_sequence_length(const char *s, size_t len) {
240 assert(s);
241
242 if (len < 2)
243 return 0;
244
245 if (s[0] != 0x1B) /* ASCII 27, aka ESC, aka Ctrl-[ */
246 return 0; /* Not the start of a sequence */
247
248 if (s[1] == 0x5B) { /* [, start of CSI sequence */
249 size_t i = 2;
250
251 if (i == len)
252 return 0;
253
254 while (s[i] >= 0x30 && s[i] <= 0x3F) /* Parameter bytes */
255 if (++i == len)
256 return 0;
257 while (s[i] >= 0x20 && s[i] <= 0x2F) /* Intermediate bytes */
258 if (++i == len)
259 return 0;
260 if (s[i] >= 0x40 && s[i] <= 0x7E) /* Final byte */
261 return i + 1;
262 return 0; /* Bad sequence */
263
264 } else if (s[1] >= 0x40 && s[1] <= 0x5F) /* other non-CSI Fe sequence */
265 return 2;
266
267 return 0; /* Bad escape? */
268 }
269
270 static bool string_has_ansi_sequence(const char *s, size_t len) {
271 const char *t = s;
272
273 while ((t = memchr(t, 0x1B, len - (t - s)))) {
274 if (ansi_sequence_length(t, len - (t - s)) > 0)
275 return true;
276 t++;
277 }
278 return false;
279 }
280
281 static size_t previous_ansi_sequence(const char *s, size_t length, const char **ret_where) {
282
283 assert(s);
284 assert(ret_where);
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 */
289 size_t slen = ansi_sequence_length(s + (i - 1), length - (i - 1));
290 if (slen == 0)
291 continue;
292
293 *ret_where = s + (i - 1);
294 return slen;
295 }
296
297 *ret_where = NULL;
298 return 0;
299 }
300
301 static char *ascii_ellipsize_mem(const char *s, size_t old_length, size_t new_length, unsigned percent) {
302 size_t x, need_space, suffix_len;
303 char *t;
304
305 assert(s);
306 assert(percent <= 100);
307 assert(new_length != SIZE_MAX);
308
309 if (old_length <= new_length)
310 return strndup(s, old_length);
311
312 /* Special case short ellipsations */
313 switch (new_length) {
314
315 case 0:
316 return strdup("");
317
318 case 1:
319 if (is_locale_utf8())
320 return strdup("…");
321 else
322 return strdup(".");
323
324 case 2:
325 if (!is_locale_utf8())
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;
334
335 t = new(char, new_length+3);
336 if (!t)
337 return NULL;
338
339 assert(new_length >= need_space);
340
341 x = ((new_length - need_space) * percent + 50) / 100;
342 assert(x <= new_length - need_space);
343
344 write_ellipsis(mempcpy(t, s, x), /* unicode= */ false);
345 suffix_len = new_length - x - need_space;
346 memcpy(t + x + 3, s + old_length - suffix_len, suffix_len);
347 *(t + x + 3 + suffix_len) = '\0';
348
349 return t;
350 }
351
352 char* ellipsize_mem(const char *s, size_t old_length, size_t new_length, unsigned percent) {
353 size_t x, k, len, len2;
354 const char *i, *j;
355 int r;
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);
370 assert(percent <= 100);
371
372 if (new_length == SIZE_MAX)
373 return strndup(s, old_length);
374
375 if (new_length == 0)
376 return strdup("");
377
378 bool has_ansi_seq = string_has_ansi_sequence(s, old_length);
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))
382 return ascii_ellipsize_mem(s, old_length, new_length, percent);
383
384 x = (new_length - 1) * percent / 100;
385 assert(x <= new_length - 1);
386
387 k = 0;
388 for (i = s; i < s + old_length; ) {
389 size_t slen = has_ansi_seq ? ansi_sequence_length(i, old_length - (i - s)) : 0;
390 if (slen > 0) {
391 i += slen;
392 continue; /* ANSI sequences don't take up any space in output */
393 }
394
395 char32_t c;
396 r = utf8_encoded_to_unichar(i, &c);
397 if (r < 0)
398 return NULL;
399
400 int w = unichar_iswide(c) ? 2 : 1;
401 if (k + w > x)
402 break;
403
404 k += w;
405 i += r;
406 }
407
408 const char *ansi_start = s + old_length;
409 size_t ansi_len = 0;
410
411 for (const char *t = j = s + old_length; t > i && k < new_length; ) {
412 char32_t c;
413 int w;
414 const char *tt;
415
416 if (has_ansi_seq && ansi_start >= t)
417 /* Figure out the previous ANSI sequence, if any */
418 ansi_len = previous_ansi_sequence(s, t - s, &ansi_start);
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) {
422 t = ansi_start;
423 continue;
424 }
425
426 tt = utf8_prev_char(t);
427 r = utf8_encoded_to_unichar(tt, &c);
428 if (r < 0)
429 return NULL;
430
431 w = unichar_iswide(c) ? 2 : 1;
432 if (k + w > new_length)
433 break;
434
435 k += w;
436 j = t = tt; /* j should always point to the first "real" character */
437 }
438
439 /* We don't actually need to ellipsize */
440 if (i >= j)
441 return memdup_suffix0(s, old_length);
442
443 if (k >= new_length) {
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)
448 j = utf8_next_char(j);
449 else if (i > s)
450 i = utf8_prev_char(i);
451 }
452
453 len = i - s;
454 len2 = s + old_length - j;
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;
460
461 char *e = new(char, alloc_len);
462 if (!e)
463 return NULL;
464
465 memcpy_safe(e, s, len);
466 write_ellipsis(e + len, /* unicode= */ true);
467
468 char *dst = e + len + 3;
469
470 if (has_ansi_seq)
471 /* Copy over any ANSI sequences in full */
472 for (const char *p = s + len; p < j; ) {
473 size_t slen = ansi_sequence_length(p, j - p);
474 if (slen > 0) {
475 dst = mempcpy(dst, p, slen);
476 p += slen;
477 } else
478 p = utf8_next_char(p);
479 }
480
481 memcpy_safe(dst, j, len2);
482 dst[len2] = '\0';
483
484 return e;
485 }
486
487 char* cellescape(char *buf, size_t len, const char *s) {
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;
501
502 assert(buf);
503 assert(len > 0); /* at least a terminating NUL */
504 assert(s);
505
506 for (;;) {
507 char four[4];
508 int w;
509
510 if (*s == 0) /* terminating NUL detected? then we are done! */
511 goto done;
512
513 w = cescape_char(*s, four);
514 if (i + w + 1 > len) /* This character doesn't fit into the buffer anymore? In that case let's
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);
520 i += w;
521
522 /* And remember its width in the ring buffer */
523 last_char_width[k] = w;
524 k = (k + 1) % 4;
525
526 s++;
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++) {
532
533 if (i + 4 <= len) /* nice, we reached our space goal */
534 break;
535
536 k = k == 0 ? 3 : k - 1;
537 if (last_char_width[k] == 0) /* bummer, we reached the beginning of the strings */
538 break;
539
540 assert(i >= last_char_width[k]);
541 i -= last_char_width[k];
542 }
543
544 if (i + 4 <= len) /* yay, enough space */
545 i += write_ellipsis(buf + i, /* unicode= */ false);
546 else if (i + 3 <= len) { /* only space for ".." */
547 buf[i++] = '.';
548 buf[i++] = '.';
549 } else if (i + 2 <= len) /* only space for a single "." */
550 buf[i++] = '.';
551 else
552 assert(i + 1 <= len);
553
554 done:
555 buf[i] = '\0';
556 return buf;
557 }
558
559 char* strshorten(char *s, size_t l) {
560 assert(s);
561
562 if (l >= SIZE_MAX-1) /* Would not change anything */
563 return s;
564
565 if (strnlen(s, l+1) > l)
566 s[l] = 0;
567
568 return s;
569 }
570
571 int strgrowpad0(char **s, size_t l) {
572 size_t sz;
573
574 assert(s);
575
576 if (*s) {
577 sz = strlen(*s) + 1;
578 if (sz >= l) /* never shrink */
579 return 0;
580 } else
581 sz = 0;
582
583 char *q = realloc(*s, l);
584 if (!q)
585 return -ENOMEM;
586
587 *s = q;
588
589 memzero(*s + sz, l - sz);
590 return 0;
591 }
592
593 char* strreplace(const char *text, const char *old_string, const char *new_string) {
594 size_t l, old_len, new_len;
595 char *t, *ret = NULL;
596 const char *f;
597
598 assert(old_string);
599 assert(new_string);
600
601 if (!text)
602 return NULL;
603
604 old_len = strlen(old_string);
605 new_len = strlen(new_string);
606
607 l = strlen(text);
608 if (!GREEDY_REALLOC(ret, l+1))
609 return NULL;
610
611 f = text;
612 t = ret;
613 while (*f) {
614 size_t d, nl;
615
616 if (!startswith(f, old_string)) {
617 *(t++) = *(f++);
618 continue;
619 }
620
621 d = t - ret;
622 nl = l - old_len + new_len;
623
624 if (!GREEDY_REALLOC(ret, nl + 1))
625 return mfree(ret);
626
627 l = nl;
628 t = ret + d;
629
630 t = stpcpy(t, new_string);
631 f += old_len;
632 }
633
634 *t = 0;
635 return ret;
636 }
637
638 static void advance_offsets(
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)
645 return;
646
647 assert(shift);
648
649 if ((size_t) diff < offsets[0])
650 shift[0] += size;
651 if ((size_t) diff < offsets[1])
652 shift[1] += size;
653 }
654
655 char* strip_tab_ansi(char **ibuf, size_t *_isz, size_t highlight[2]) {
656 const char *begin = NULL;
657 enum {
658 STATE_OTHER,
659 STATE_ESCAPE,
660 STATE_CSI,
661 STATE_OSC,
662 STATE_OSC_CLOSING,
663 } state = STATE_OTHER;
664 _cleanup_(memstream_done) MemStream m = {};
665 size_t isz, shift[2] = {}, n_carriage_returns = 0;
666 FILE *f;
667
668 assert(ibuf);
669 assert(*ibuf);
670 POINTER_MAY_BE_NULL(_isz);
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);
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);
691 if (!f)
692 return NULL;
693
694 for (const char *i = *ibuf; i < *ibuf + isz + 1; i++) {
695
696 bool eot = i >= *ibuf + isz;
697
698 switch (state) {
699
700 case STATE_OTHER:
701 if (eot)
702 break;
703
704 if (*i == '\r') {
705 n_carriage_returns++;
706 break;
707 } else if (*i == '\n')
708 /* Ignore carriage returns before new line */
709 n_carriage_returns = 0;
710 for (; n_carriage_returns > 0; n_carriage_returns--)
711 fputc('\r', f);
712
713 if (*i == '\x1B')
714 state = STATE_ESCAPE;
715 else if (*i == '\t') {
716 fputs(" ", f);
717 advance_offsets(i - *ibuf, highlight, shift, 7);
718 } else
719 fputc(*i, f);
720
721 break;
722
723 case STATE_ESCAPE:
724 assert(n_carriage_returns == 0);
725
726 if (eot) {
727 fputc('\x1B', f);
728 advance_offsets(i - *ibuf, highlight, shift, 1);
729 break;
730 } else if (*i == '[') { /* ANSI CSI */
731 state = STATE_CSI;
732 begin = i + 1;
733 } else if (*i == ']') { /* ANSI OSC */
734 state = STATE_OSC;
735 begin = i + 1;
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:
746 assert(n_carriage_returns == 0);
747
748 if (eot || !strchr(DIGITS ";:m", *i)) { /* EOT or invalid chars in sequence */
749 fputc('\x1B', f);
750 fputc('[', f);
751 advance_offsets(i - *ibuf, highlight, shift, 2);
752 state = STATE_OTHER;
753 i = begin-1;
754 } else if (*i == 'm')
755 state = STATE_OTHER;
756
757 break;
758
759 case STATE_OSC:
760 assert(n_carriage_returns == 0);
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 */
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 */
772 state = STATE_OTHER;
773 else if (*i == '\x1B')
774 state = STATE_OSC_CLOSING;
775
776 break;
777
778 case STATE_OSC_CLOSING:
779 if (eot || *i != '\x5c') { /* EOT or incomplete two-byte ST in sequence */
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;
793 if (memstream_finalize(&m, &obuf, _isz) < 0)
794 return NULL;
795
796 free_and_replace(*ibuf, obuf);
797
798 if (highlight) {
799 highlight[0] += shift[0];
800 highlight[1] += shift[1];
801 }
802
803 return *ibuf;
804 }
805
806 char* strextendv_with_separator(char **x, const char *separator, va_list ap) {
807 _cleanup_free_ char *buffer = NULL;
808 size_t f, l, l_separator;
809 bool need_separator;
810 char *nr, *p;
811
812 if (!x)
813 x = &buffer;
814
815 l = f = strlen_ptr(*x);
816
817 need_separator = !isempty(*x);
818 l_separator = strlen_ptr(separator);
819
820 va_list aq;
821 va_copy(aq, ap);
822 for (const char *t;;) {
823 size_t n;
824
825 t = va_arg(aq, const char *);
826 if (!t)
827 break;
828 if (t == POINTER_MAX)
829 continue;
830
831 n = strlen(t);
832
833 if (need_separator)
834 n += l_separator;
835
836 if (n >= SIZE_MAX - l) {
837 va_end(aq);
838 return NULL;
839 }
840
841 l += n;
842 need_separator = true;
843 }
844 va_end(aq);
845
846 need_separator = !isempty(*x);
847
848 nr = realloc(*x, GREEDY_ALLOC_ROUND_UP(l+1));
849 if (!nr)
850 return NULL;
851
852 *x = nr;
853 p = nr + f;
854
855 for (;;) {
856 const char *t;
857
858 t = va_arg(ap, const char *);
859 if (!t)
860 break;
861 if (t == POINTER_MAX)
862 continue;
863
864 if (need_separator && separator)
865 p = stpcpy(p, separator);
866
867 p = stpcpy(p, t);
868
869 need_separator = true;
870 }
871
872 assert(p == nr + l);
873 *p = 0;
874
875 /* If no buffer to extend was passed in return the start of the buffer */
876 if (buffer)
877 return TAKE_PTR(buffer);
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, ...) {
884 va_list ap;
885 char *ret;
886
887 va_start(ap, separator);
888 ret = strextendv_with_separator(x, separator, ap);
889 va_end(ap);
890
891 return ret;
892 }
893
894 int strextendf_with_separator(char **x, const char *separator, const char *format, ...) {
895 size_t m, a, l_separator;
896 va_list ap;
897 int l;
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);
904 assert(format);
905
906 l_separator = isempty(*x) ? 0 : strlen_ptr(separator);
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) {
910 m = strlen(*x);
911 a = MALLOC_SIZEOF_SAFE(*x);
912 assert(a >= m + 1);
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 */
917 char *n;
918
919 if (_unlikely_(l_separator > SIZE_MAX - 64)) /* overflow check #1 */
920 return -ENOMEM;
921 if (_unlikely_(m > SIZE_MAX - 64 - l_separator)) /* overflow check #2 */
922 return -ENOMEM;
923
924 n = realloc(*x, m + 64 + l_separator);
925 if (!n)
926 return -ENOMEM;
927
928 *x = n;
929 a = MALLOC_SIZEOF_SAFE(*x);
930 }
931
932 /* Now, let's try to format the string into it */
933 memcpy_safe(*x + m, separator, l_separator);
934 va_start(ap, format);
935 l = vsnprintf(*x + m + l_separator, a - m - l_separator, format, ap);
936 va_end(ap);
937
938 assert(l >= 0);
939
940 if ((size_t) l < a - m - l_separator) {
941 char *n;
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);
947 if (n)
948 *x = n;
949 } else {
950 char *n;
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 */
955 goto oom;
956 if (_unlikely_(m > SIZE_MAX - ((size_t) l + l_separator + 1))) /* overflow check #2 */
957 goto oom;
958
959 a = m + (size_t) l + l_separator + 1;
960 n = realloc(*x, a);
961 if (!n)
962 goto oom;
963 *x = n;
964
965 va_start(ap, format);
966 l = vsnprintf(*x + m + l_separator, a - m - l_separator, format, ap);
967 va_end(ap);
968
969 assert((size_t) l < a - m - l_separator);
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, size_t n) {
981 char *ret, *p;
982 size_t l;
983
984 assert(s);
985
986 l = strlen(s);
987 if (!MUL_ASSIGN_SAFE(&l, n))
988 return NULL;
989 if (!INC_SAFE(&l, 1))
990 return NULL;
991
992 p = ret = malloc(l);
993 if (!ret)
994 return NULL;
995
996 for (size_t i = 0; i < n; i++)
997 p = stpcpy(p, s);
998
999 *p = 0;
1000 return ret;
1001 }
1002
1003 int split_pair(const char *s, const char *sep, char **ret_first, char **ret_second) {
1004 assert(s);
1005 assert(!isempty(sep));
1006 assert(ret_first);
1007 assert(ret_second);
1008
1009 const char *x = strstr(s, sep);
1010 if (!x)
1011 return -EINVAL;
1012
1013 _cleanup_free_ char *a = strndup(s, x - s);
1014 if (!a)
1015 return -ENOMEM;
1016
1017 _cleanup_free_ char *b = strdup(x + strlen(sep));
1018 if (!b)
1019 return -ENOMEM;
1020
1021 *ret_first = TAKE_PTR(a);
1022 *ret_second = TAKE_PTR(b);
1023 return 0;
1024 }
1025
1026 int free_and_strdup(char **p, const char *s) {
1027 char *t;
1028
1029 assert(p);
1030
1031 /* Replaces a string pointer with a strdup()ed new string,
1032 * possibly freeing the old one. */
1033
1034 if (streq_ptr(*p, s))
1035 return 0;
1036
1037 if (s) {
1038 t = strdup(s);
1039 if (!t)
1040 return -ENOMEM;
1041 } else
1042 t = NULL;
1043
1044 free_and_replace(*p, t);
1045
1046 return 1;
1047 }
1048
1049 int free_and_strdup_warn(char **p, const char *s) {
1050 int r;
1051
1052 r = free_and_strdup(p, s);
1053 if (r < 0)
1054 return log_oom();
1055 return r;
1056 }
1057
1058 int free_and_strndup(char **p, const char *s, size_t l) {
1059 char *t;
1060
1061 assert(p);
1062 assert(s || l == 0);
1063
1064 /* Replaces a string pointer with a strndup()ed new string,
1065 * freeing the old one. */
1066
1067 if (!*p && !s)
1068 return 0;
1069
1070 if (*p && s && strneq(*p, s, l) && (l > strlen(*p) || (*p)[l] == '\0'))
1071 return 0;
1072
1073 if (s) {
1074 t = strndup(s, l);
1075 if (!t)
1076 return -ENOMEM;
1077 } else
1078 t = NULL;
1079
1080 free_and_replace(*p, t);
1081 return 1;
1082 }
1083
1084 int strdup_to_full(char **ret, const char *src) {
1085 if (!src) {
1086 if (ret)
1087 *ret = NULL;
1088
1089 return 0;
1090 } else {
1091 if (ret) {
1092 char *t = strdup(src);
1093 if (!t)
1094 return -ENOMEM;
1095 *ret = t;
1096 }
1097
1098 return 1;
1099 }
1100 };
1101
1102 bool string_is_safe(const char *p, StringSafeFlags flags) {
1103
1104 /* Baseline checks are:
1105 * • No control characters (i.e. 0…31 + 127)
1106 * • UTF-8 valid (well, technically we skip this test if STRING_ASCII is set, since that is a tighter test)
1107 */
1108
1109 if (FLAGS_SET(flags, STRING_ALLOW_EMPTY) ? !p : isempty(p))
1110 return false;
1111
1112 if (!FLAGS_SET(flags, STRING_ASCII) && !utf8_is_valid(p))
1113 return false;
1114
1115 for (const char *t = p; *t; t++) {
1116 /* never allow control characters, except for new line */
1117 if ((*t > 0 && *t < ' ' && *t != '\n') || *t == 0x7f)
1118 return false;
1119
1120 if (!FLAGS_SET(flags, STRING_ALLOW_NEWLINES) && *t == '\n')
1121 return false;
1122
1123 if (!FLAGS_SET(flags, STRING_ALLOW_BACKSLASHES) && *t == '\\')
1124 return false;
1125
1126 if (!FLAGS_SET(flags, STRING_ALLOW_QUOTES) && strchr(QUOTES, *t))
1127 return false;
1128
1129 if (!FLAGS_SET(flags, STRING_ALLOW_GLOBS) && strchr(GLOB_CHARS, *t))
1130 return false;
1131
1132 if (FLAGS_SET(flags, STRING_ASCII) && (uint8_t) *t >= 0x80)
1133 return false;
1134 }
1135
1136 if (FLAGS_SET(flags, STRING_FILENAME) && !filename_is_valid(p))
1137 return false;
1138
1139 return true;
1140 }
1141
1142 char* str_realloc(char *p) {
1143 /* Reallocate *p to actual size. Ignore failure, and return the original string on error. */
1144
1145 if (!p)
1146 return NULL;
1147
1148 return realloc(p, strlen(p) + 1) ?: p;
1149 }
1150
1151 char* string_erase(char *x) {
1152 if (!x)
1153 return NULL;
1154
1155 /* A delicious drop of snake-oil! To be called on memory where we stored passphrases or so, after we
1156 * used them. */
1157 explicit_bzero_safe(x, strlen(x));
1158 return x;
1159 }
1160
1161 int string_truncate_lines(const char *s, size_t n_lines, char **ret) {
1162 const char *p = s, *e = s;
1163 bool truncation_applied = false;
1164 char *copy;
1165 size_t n = 0;
1166
1167 assert(s);
1168 assert(ret);
1169
1170 /* Truncate after the specified number of lines. Returns > 0 if a truncation was applied or == 0 if
1171 * there were fewer lines in the string anyway. Trailing newlines on input are ignored, and not
1172 * generated either. */
1173
1174 for (;;) {
1175 size_t k;
1176
1177 k = strcspn(p, "\n");
1178
1179 if (p[k] == 0) {
1180 if (k == 0) /* final empty line */
1181 break;
1182
1183 if (n >= n_lines) /* above threshold */
1184 break;
1185
1186 e = p + k; /* last line to include */
1187 break;
1188 }
1189
1190 assert(p[k] == '\n');
1191
1192 if (n >= n_lines)
1193 break;
1194
1195 if (k > 0)
1196 e = p + k;
1197
1198 p += k + 1;
1199 n++;
1200 }
1201
1202 /* e points after the last character we want to keep */
1203 if (isempty(e))
1204 copy = strdup(s);
1205 else {
1206 if (!in_charset(e, "\n")) /* We only consider things truncated if we remove something that
1207 * isn't a new-line or a series of them */
1208 truncation_applied = true;
1209
1210 copy = strndup(s, e - s);
1211 }
1212 if (!copy)
1213 return -ENOMEM;
1214
1215 *ret = copy;
1216 return truncation_applied;
1217 }
1218
1219 int string_extract_line(const char *s, size_t i, char **ret) {
1220 const char *p = s;
1221 size_t c = 0;
1222
1223 assert(ret);
1224
1225 /* Extract the i'nth line from the specified string. Returns > 0 if there are more lines after that,
1226 * and == 0 if we are looking at the last line or already beyond the last line. As special
1227 * optimization, if the first line is requested and the string only consists of one line we return
1228 * NULL, indicating the input string should be used as is, and avoid a memory allocation for a very
1229 * common case. */
1230
1231 for (;;) {
1232 const char *q;
1233
1234 q = strchr(p, '\n');
1235 if (i == c) {
1236 /* The line we are looking for! */
1237
1238 if (q) {
1239 char *m;
1240
1241 m = strndup(p, q - p);
1242 if (!m)
1243 return -ENOMEM;
1244
1245 *ret = m;
1246 return !isempty(q + 1); /* More coming? */
1247 } else
1248 /* Tell the caller to use the input string if equal */
1249 return strdup_to(ret, p != s ? p : NULL);
1250 }
1251
1252 if (!q)
1253 /* No more lines, return empty line */
1254 return strdup_to(ret, "");
1255
1256 p = q + 1;
1257 c++;
1258 }
1259 }
1260
1261 int string_contains_word_strv(const char *string, const char *separators, char * const *words, const char **ret_word) {
1262 /* In the default mode with no separators specified, we split on whitespace and coalesce separators. */
1263 const ExtractFlags flags = separators ? EXTRACT_DONT_COALESCE_SEPARATORS : 0;
1264 const char *found = NULL;
1265 int r;
1266
1267 for (;;) {
1268 _cleanup_free_ char *w = NULL;
1269
1270 r = extract_first_word(&string, &w, separators, flags);
1271 if (r < 0)
1272 return r;
1273 if (r == 0)
1274 break;
1275
1276 found = strv_find(words, w);
1277 if (found)
1278 break;
1279 }
1280
1281 if (ret_word)
1282 *ret_word = found;
1283 return !!found;
1284 }
1285
1286 bool streq_skip_trailing_chars(const char *s1, const char *s2, const char *ok) {
1287 if (!s1 && !s2)
1288 return true;
1289 if (!s1 || !s2)
1290 return false;
1291
1292 if (!ok)
1293 ok = WHITESPACE;
1294
1295 for (; *s1 && *s2; s1++, s2++)
1296 if (*s1 != *s2)
1297 break;
1298
1299 return in_charset(s1, ok) && in_charset(s2, ok);
1300 }
1301
1302 char* string_replace_char(char *str, char old_char, char new_char) {
1303 assert(str);
1304 assert(old_char != '\0');
1305 assert(new_char != '\0');
1306 assert(old_char != new_char);
1307
1308 for (char *p = strchr(str, old_char); p; p = strchr(p + 1, old_char))
1309 *p = new_char;
1310
1311 return str;
1312 }
1313
1314 int make_cstring(const void *s, size_t n, MakeCStringMode mode, char **ret) {
1315 char *b;
1316
1317 assert(s || n == 0);
1318 assert(mode >= 0);
1319 assert(mode < _MAKE_CSTRING_MODE_MAX);
1320
1321 /* Converts a sized character buffer into a NUL-terminated NUL string, refusing if there are embedded
1322 * NUL bytes. Whether to expect a trailing NUL byte can be specified via 'mode' */
1323
1324 if (n == 0) {
1325 if (mode == MAKE_CSTRING_REQUIRE_TRAILING_NUL)
1326 return -EINVAL;
1327
1328 if (!ret)
1329 return 0;
1330
1331 b = new0(char, 1);
1332 } else {
1333 const uint8_t *nul;
1334
1335 nul = memchr(s, 0, n);
1336 if (nul) {
1337 if (nul < (const uint8_t*) s + n - 1 || /* embedded NUL? */
1338 mode == MAKE_CSTRING_REFUSE_TRAILING_NUL)
1339 return -EINVAL;
1340
1341 n--;
1342 } else if (mode == MAKE_CSTRING_REQUIRE_TRAILING_NUL)
1343 return -EINVAL;
1344
1345 if (!ret)
1346 return 0;
1347
1348 b = memdup_suffix0(s, n);
1349 }
1350 if (!b)
1351 return -ENOMEM;
1352
1353 *ret = b;
1354 return 0;
1355 }
1356
1357 size_t strspn_from_end(const char *str, const char *accept) {
1358 size_t n = 0;
1359
1360 if (isempty(str))
1361 return 0;
1362
1363 if (isempty(accept))
1364 return 0;
1365
1366 for (const char *p = str + strlen(str); p > str && strchr(accept, p[-1]); p--)
1367 n++;
1368
1369 return n;
1370 }
1371
1372 char* strdupspn(const char *a, const char *accept) {
1373 if (isempty(a) || isempty(accept))
1374 return strdup("");
1375
1376 return strndup(a, strspn(a, accept));
1377 }
1378
1379 char* strdupcspn(const char *a, const char *reject) {
1380 if (isempty(a))
1381 return strdup("");
1382 if (isempty(reject))
1383 return strdup(a);
1384
1385 return strndup(a, strcspn(a, reject));
1386 }
1387
1388 char* find_line_startswith_internal(const char *haystack, const char *needle) {
1389 assert(haystack);
1390 assert(needle);
1391
1392 /* Finds the first line in 'haystack' that starts with the specified string. Returns a pointer to the
1393 * first character after it */
1394
1395 char *p = (char*) strstr(haystack, needle);
1396 if (!p)
1397 return NULL;
1398
1399 if (p > haystack)
1400 while (p[-1] != '\n') {
1401 p = strstr(p + 1, needle);
1402 if (!p)
1403 return NULL;
1404 }
1405
1406 return p + strlen(needle);
1407 }
1408
1409 char* find_line_internal(const char *haystack, const char *needle) {
1410 assert(haystack);
1411 assert(needle);
1412
1413 /* Finds the first line in 'haystack' that match the specified string. Returns a pointer to the
1414 * beginning of the line */
1415
1416 char *p = (char*) find_line_startswith(haystack, needle);
1417 if (!p)
1418 return NULL;
1419
1420 if (*p == 0 || strchr(NEWLINE, *p))
1421 return p - strlen(needle);
1422
1423 return NULL;
1424 }
1425
1426 char* find_line_after_internal(const char *haystack, const char *needle) {
1427 assert(haystack);
1428 assert(needle);
1429
1430 /* Finds the first line in 'haystack' that match the specified string. Returns a pointer to the
1431 * next line after it */
1432
1433 char *p = (char*) find_line_startswith(haystack, needle);
1434 if (!p)
1435 return NULL;
1436
1437 if (*p == 0)
1438 return p;
1439 if (strchr(NEWLINE, *p))
1440 return p + 1;
1441
1442 return NULL;
1443 }
1444
1445 bool version_is_valid(const char *s) {
1446 if (isempty(s))
1447 return false;
1448
1449 if (!filename_part_is_valid(s))
1450 return false;
1451
1452 /* This is a superset of the characters used by semver. We additionally allow "," and "_". */
1453 if (!in_charset(s, ALPHANUMERICAL ".,_-+"))
1454 return false;
1455
1456 return true;
1457 }
1458
1459 bool version_is_valid_versionspec(const char *s) {
1460 if (!filename_part_is_valid(s))
1461 return false;
1462
1463 if (!in_charset(s, ALPHANUMERICAL "-.~^"))
1464 return false;
1465
1466 return true;
1467 }
1468
1469 ssize_t strlevenshtein(const char *x, const char *y) {
1470 _cleanup_free_ size_t *t0 = NULL, *t1 = NULL, *t2 = NULL;
1471 size_t xl, yl;
1472
1473 /* This is inspired from the Linux kernel's Levenshtein implementation */
1474
1475 if (streq_ptr(x, y))
1476 return 0;
1477
1478 xl = strlen_ptr(x);
1479 if (xl > SSIZE_MAX)
1480 return -E2BIG;
1481
1482 yl = strlen_ptr(y);
1483 if (yl > SSIZE_MAX)
1484 return -E2BIG;
1485
1486 if (isempty(x))
1487 return yl;
1488 if (isempty(y))
1489 return xl;
1490
1491 t0 = new0(size_t, yl + 1);
1492 if (!t0)
1493 return -ENOMEM;
1494 t1 = new0(size_t, yl + 1);
1495 if (!t1)
1496 return -ENOMEM;
1497 t2 = new0(size_t, yl + 1);
1498 if (!t2)
1499 return -ENOMEM;
1500
1501 for (size_t i = 0; i <= yl; i++)
1502 t1[i] = i;
1503
1504 for (size_t i = 0; i < xl; i++) {
1505 t2[0] = i + 1;
1506
1507 for (size_t j = 0; j < yl; j++) {
1508 /* Substitution */
1509 t2[j+1] = t1[j] + (x[i] != y[j]);
1510
1511 /* Swap */
1512 if (i > 0 && j > 0 && x[i-1] == y[j] && x[i] == y[j-1] && t2[j+1] > t0[j-1] + 1)
1513 t2[j+1] = t0[j-1] + 1;
1514
1515 /* Deletion */
1516 if (t2[j+1] > t1[j+1] + 1)
1517 t2[j+1] = t1[j+1] + 1;
1518
1519 /* Insertion */
1520 if (t2[j+1] > t2[j] + 1)
1521 t2[j+1] = t2[j] + 1;
1522 }
1523
1524 size_t *dummy = t0;
1525 t0 = t1;
1526 t1 = t2;
1527 t2 = dummy;
1528 }
1529
1530 return t1[yl];
1531 }
1532
1533 char* strrstr_internal(const char *haystack, const char *needle) {
1534 /* Like strstr() but returns the last rather than the first occurrence of "needle" in "haystack". */
1535
1536 if (!haystack || !needle)
1537 return NULL;
1538
1539 /* Special case: for the empty string we return the very last possible occurrence, i.e. *after* the
1540 * last char, not before. */
1541 if (needle[0] == 0)
1542 return (char*) strchr(haystack, 0);
1543
1544 /* Special case: for single character strings, just use optimized strrchr() */
1545 if (needle[1] == 0)
1546 return (char*) strrchr(haystack, needle[0]);
1547
1548 for (const char *p = strstr(haystack, needle), *q; p; p = q) {
1549 q = strstr(p + 1, needle);
1550 if (!q)
1551 return (char*) p;
1552 }
1553 return NULL;
1554 }
1555
1556 char* strrstr_no_case_internal(const char *haystack, const char *needle) {
1557 if (!haystack || !needle)
1558 return NULL;
1559
1560 for (const char *p = strchr(haystack, 0); p > haystack; p--)
1561 if (startswith_no_case(p, needle))
1562 return (char*) p;
1563
1564 return startswith_no_case(haystack, needle) ? (char*) haystack : NULL;
1565 }
1566
1567 size_t str_common_prefix(const char *a, const char *b) {
1568 assert(a);
1569 assert(b);
1570
1571 /* Returns the length of the common prefix of the two specified strings, or SIZE_MAX in case the
1572 * strings are fully identical. */
1573
1574 for (size_t n = 0;; n++) {
1575 char c = a[n];
1576 if (c != b[n])
1577 return n;
1578 if (c == 0)
1579 return SIZE_MAX;
1580 }
1581 }