]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/string-util.c
man/systemd-sysext: list ephemeral/ephemeral-import in the list of options
[thirdparty/systemd.git] / src / basic / string-util.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
07630cea 2
11c3a366 3#include <stdio.h>
11c3a366 4
b5efdb8a 5#include "alloc-util.h"
8409f688 6#include "escape.h"
53cd7f33 7#include "extract-word.h"
d723363a 8#include "glyph-util.h"
07630cea 9#include "gunicode.h"
c30a49b2 10#include "locale-util.h"
93a1f792 11#include "log.h"
090a9c1e 12#include "memory-util.h"
2485b7e2 13#include "memstream-util.h"
f5c6b4f4 14#include "path-util.h"
b11d6a7b 15#include "string-util.h"
46bf625a 16#include "strv.h"
b4766d5f 17#include "terminal-util.h"
07630cea 18#include "utf8.h"
07630cea 19
07630cea 20char* first_word(const char *s, const char *word) {
07630cea
LP
21 assert(s);
22 assert(word);
23
c1bf0571
MY
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. */
07630cea 26
c1bf0571 27 if (isempty(word))
07630cea
LP
28 return (char*) s;
29
c1bf0571
MY
30 const char *p = startswith(s, word);
31 if (!p)
07630cea 32 return NULL;
c1bf0571 33 if (*p == '\0')
07630cea
LP
34 return (char*) p;
35
c1bf0571
MY
36 const char *nw = skip_leading_chars(p, WHITESPACE);
37 if (p == nw)
07630cea
LP
38 return NULL;
39
c1bf0571 40 return (char*) nw;
07630cea
LP
41}
42
63ed6115
MY
43char* 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
ff3f2953 67char* strstrip(char *s) {
7546145e
LP
68 if (!s)
69 return NULL;
70
0a6ffc5c 71 /* Drops trailing whitespace. Modifies the string in place. Returns pointer to first non-space character */
07630cea 72
0a6ffc5c 73 return delete_trailing_chars(skip_leading_chars(s, WHITESPACE), WHITESPACE);
07630cea
LP
74}
75
ff3f2953 76char* delete_chars(char *s, const char *bad) {
07630cea
LP
77 char *f, *t;
78
7546145e
LP
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;
07630cea
LP
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
ff3f2953 99char* delete_trailing_chars(char *s, const char *bad) {
a01080ce 100 char *c = s;
7546145e
LP
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
a01080ce 110 for (char *p = s; *p; p++)
7546145e
LP
111 if (!strchr(bad, *p))
112 c = p + 1;
113
114 *c = 0;
115
116 return s;
117}
118
ff3f2953 119char* truncate_nl_full(char *s, size_t *ret_len) {
61cecfa0 120 size_t n;
121
07630cea
LP
122 assert(s);
123
61cecfa0 124 n = strcspn(s, NEWLINE);
125 s[n] = '\0';
126 if (ret_len)
127 *ret_len = n;
07630cea
LP
128 return s;
129}
130
b577e3d5
LP
131char ascii_tolower(char x) {
132
133 if (x >= 'A' && x <= 'Z')
134 return x - 'A' + 'a';
135
136 return x;
137}
138
846b8fc3
LP
139char ascii_toupper(char x) {
140
141 if (x >= 'a' && x <= 'z')
142 return x - 'a' + 'A';
143
144 return x;
145}
146
ff3f2953 147char* ascii_strlower(char *t) {
07630cea
LP
148 assert(t);
149
a01080ce 150 for (char *p = t; *p; p++)
b577e3d5
LP
151 *p = ascii_tolower(*p);
152
153 return t;
154}
155
ff3f2953 156char* ascii_strupper(char *t) {
846b8fc3
LP
157 assert(t);
158
a01080ce 159 for (char *p = t; *p; p++)
846b8fc3
LP
160 *p = ascii_toupper(*p);
161
162 return t;
163}
164
ff3f2953 165char* ascii_strlower_n(char *t, size_t n) {
b577e3d5
LP
166 if (n <= 0)
167 return t;
168
a01080ce 169 for (size_t i = 0; i < n; i++)
b577e3d5 170 t[i] = ascii_tolower(t[i]);
07630cea
LP
171
172 return t;
173}
522d85ae
LP
174
175int ascii_strcasecmp_n(const char *a, const char *b, size_t n) {
176
177 for (; n > 0; a++, b++, n--) {
178 int x, y;
179
180 x = (int) (uint8_t) ascii_tolower(*a);
181 y = (int) (uint8_t) ascii_tolower(*b);
182
183 if (x != y)
184 return x - y;
185 }
186
187 return 0;
188}
c1749834
LP
189
190int ascii_strcasecmp_nn(const char *a, size_t n, const char *b, size_t m) {
191 int r;
192
193 r = ascii_strcasecmp_n(a, b, MIN(n, m));
194 if (r != 0)
195 return r;
196
6dd91b36 197 return CMP(n, m);
c1749834 198}
07630cea
LP
199
200bool chars_intersect(const char *a, const char *b) {
07630cea 201 /* Returns true if any of the chars in a are in b. */
a01080ce 202 for (const char *p = a; *p; p++)
07630cea
LP
203 if (strchr(b, *p))
204 return true;
205
206 return false;
207}
208
209bool string_has_cc(const char *p, const char *ok) {
07630cea
LP
210 assert(p);
211
212 /*
213 * Check if a string contains control characters. If 'ok' is
214 * non-NULL it may be a string containing additional CCs to be
215 * considered OK.
216 */
217
a01080ce 218 for (const char *t = p; *t; t++) {
07630cea
LP
219 if (ok && strchr(ok, *t))
220 continue;
221
6302d386 222 if (char_is_cc(*t))
07630cea
LP
223 return true;
224 }
225
226 return false;
227}
228
8409f688 229static int write_ellipsis(char *buf, bool unicode) {
1ae9b0cf 230 const char *s = glyph_full(GLYPH_ELLIPSIS, unicode);
d723363a
YW
231 assert(strlen(s) == 3);
232 memcpy(buf, s, 3);
8409f688
ZJS
233 return 3;
234}
235
cb558ab2
ZJS
236static size_t ansi_sequence_length(const char *s, size_t len) {
237 assert(s);
238
239 if (len < 2)
240 return 0;
241
242 if (s[0] != 0x1B) /* ASCII 27, aka ESC, aka Ctrl-[ */
243 return 0; /* Not the start of a sequence */
244
245 if (s[1] == 0x5B) { /* [, start of CSI sequence */
246 size_t i = 2;
247
248 if (i == len)
249 return 0;
250
251 while (s[i] >= 0x30 && s[i] <= 0x3F) /* Parameter bytes */
252 if (++i == len)
253 return 0;
254 while (s[i] >= 0x20 && s[i] <= 0x2F) /* Intermediate bytes */
255 if (++i == len)
256 return 0;
257 if (s[i] >= 0x40 && s[i] <= 0x7E) /* Final byte */
258 return i + 1;
259 return 0; /* Bad sequence */
260
261 } else if (s[1] >= 0x40 && s[1] <= 0x5F) /* other non-CSI Fe sequence */
262 return 2;
263
264 return 0; /* Bad escape? */
265}
266
267static bool string_has_ansi_sequence(const char *s, size_t len) {
268 const char *t = s;
269
270 while ((t = memchr(s, 0x1B, len - (t - s))))
271 if (ansi_sequence_length(t, len - (t - s)) > 0)
272 return true;
273 return false;
274}
275
276static size_t previous_ansi_sequence(const char *s, size_t length, const char **ret_where) {
277 /* Locate the previous ANSI sequence and save its start in *ret_where and return length. */
278
279 for (size_t i = length - 2; i > 0; i--) { /* -2 because at least two bytes are needed */
280 size_t slen = ansi_sequence_length(s + (i - 1), length - (i - 1));
281 if (slen == 0)
282 continue;
283
284 *ret_where = s + (i - 1);
285 return slen;
286 }
287
288 *ret_where = NULL;
289 return 0;
290}
291
07630cea 292static char *ascii_ellipsize_mem(const char *s, size_t old_length, size_t new_length, unsigned percent) {
9924aef6
ZJS
293 size_t x, need_space, suffix_len;
294 char *t;
07630cea
LP
295
296 assert(s);
297 assert(percent <= 100);
f5fbe71d 298 assert(new_length != SIZE_MAX);
07630cea 299
c30a49b2 300 if (old_length <= new_length)
07630cea
LP
301 return strndup(s, old_length);
302
c30a49b2
LP
303 /* Special case short ellipsations */
304 switch (new_length) {
305
306 case 0:
307 return strdup("");
308
309 case 1:
310 if (is_locale_utf8())
311 return strdup("…");
312 else
313 return strdup(".");
314
315 case 2:
316 if (!is_locale_utf8())
317 return strdup("..");
c30a49b2
LP
318 break;
319 }
320
321 /* Calculate how much space the ellipsis will take up. If we are in UTF-8 mode we only need space for one
322 * character ("…"), otherwise for three characters ("..."). Note that in both cases we need 3 bytes of storage,
323 * either for the UTF-8 encoded character or for three ASCII characters. */
324 need_space = is_locale_utf8() ? 1 : 3;
325
9924aef6
ZJS
326 t = new(char, new_length+3);
327 if (!t)
07630cea
LP
328 return NULL;
329
c30a49b2 330 assert(new_length >= need_space);
07630cea 331
c30a49b2
LP
332 x = ((new_length - need_space) * percent + 50) / 100;
333 assert(x <= new_length - need_space);
07630cea 334
c498b2f7 335 write_ellipsis(mempcpy(t, s, x), /* unicode = */ false);
9924aef6
ZJS
336 suffix_len = new_length - x - need_space;
337 memcpy(t + x + 3, s + old_length - suffix_len, suffix_len);
338 *(t + x + 3 + suffix_len) = '\0';
07630cea 339
9924aef6 340 return t;
07630cea
LP
341}
342
ff3f2953 343char* ellipsize_mem(const char *s, size_t old_length, size_t new_length, unsigned percent) {
c30a49b2 344 size_t x, k, len, len2;
07630cea 345 const char *i, *j;
c932fb71 346 int r;
07630cea 347
c30a49b2
LP
348 /* Note that 'old_length' refers to bytes in the string, while 'new_length' refers to character cells taken up
349 * on screen. This distinction doesn't matter for ASCII strings, but it does matter for non-ASCII UTF-8
350 * strings.
351 *
352 * Ellipsation is done in a locale-dependent way:
353 * 1. If the string passed in is fully ASCII and the current locale is not UTF-8, three dots are used ("...")
354 * 2. Otherwise, a unicode ellipsis is used ("…")
355 *
356 * In other words: you'll get a unicode ellipsis as soon as either the string contains non-ASCII characters or
357 * the current locale is UTF-8.
358 */
359
07630cea
LP
360 assert(s);
361 assert(percent <= 100);
ddbc9319 362
f5fbe71d 363 if (new_length == SIZE_MAX)
ddbc9319
LP
364 return strndup(s, old_length);
365
c30a49b2
LP
366 if (new_length == 0)
367 return strdup("");
07630cea 368
cb558ab2
ZJS
369 bool has_ansi_seq = string_has_ansi_sequence(s, old_length);
370
371 /* If no multibyte characters or ANSI sequences, use ascii_ellipsize_mem for speed */
372 if (!has_ansi_seq && ascii_is_valid_n(s, old_length))
07630cea
LP
373 return ascii_ellipsize_mem(s, old_length, new_length, percent);
374
cb558ab2 375 x = (new_length - 1) * percent / 100;
c30a49b2 376 assert(x <= new_length - 1);
07630cea
LP
377
378 k = 0;
cb558ab2
ZJS
379 for (i = s; i < s + old_length; ) {
380 size_t slen = has_ansi_seq ? ansi_sequence_length(i, old_length - (i - s)) : 0;
381 if (slen > 0) {
382 i += slen;
383 continue; /* ANSI sequences don't take up any space in output */
384 }
07630cea 385
cb558ab2 386 char32_t c;
c932fb71
SL
387 r = utf8_encoded_to_unichar(i, &c);
388 if (r < 0)
07630cea 389 return NULL;
07630cea 390
cb558ab2
ZJS
391 int w = unichar_iswide(c) ? 2 : 1;
392 if (k + w > x)
9924aef6 393 break;
cb558ab2
ZJS
394
395 k += w;
396 i += r;
9924aef6 397 }
07630cea 398
cb558ab2
ZJS
399 const char *ansi_start = s + old_length;
400 size_t ansi_len = 0;
401
402 for (const char *t = j = s + old_length; t > i && k < new_length; ) {
c932fb71 403 char32_t c;
9924aef6 404 int w;
cb558ab2
ZJS
405 const char *tt;
406
407 if (has_ansi_seq && ansi_start >= t)
408 /* Figure out the previous ANSI sequence, if any */
409 ansi_len = previous_ansi_sequence(s, t - s, &ansi_start);
07630cea 410
cb558ab2
ZJS
411 /* If the sequence extends all the way to the current position, skip it. */
412 if (has_ansi_seq && ansi_len > 0 && ansi_start + ansi_len == t) {
413 t = ansi_start;
414 continue;
415 }
416
417 tt = utf8_prev_char(t);
418 r = utf8_encoded_to_unichar(tt, &c);
c932fb71 419 if (r < 0)
07630cea 420 return NULL;
9924aef6
ZJS
421
422 w = unichar_iswide(c) ? 2 : 1;
cb558ab2 423 if (k + w > new_length)
9924aef6 424 break;
cb558ab2
ZJS
425
426 k += w;
427 j = t = tt; /* j should always point to the first "real" character */
07630cea 428 }
07630cea 429
cb558ab2
ZJS
430 /* We don't actually need to ellipsize */
431 if (i >= j)
9924aef6 432 return memdup_suffix0(s, old_length);
07630cea 433
cb558ab2
ZJS
434 if (k >= new_length) {
435 /* Make space for ellipsis, if required and possible. We know that the edge character is not
436 * part of an ANSI sequence (because then we'd skip it). If the last character we looked at
437 * was wide, we don't need to make space. */
438 if (j < s + old_length)
439 j = utf8_next_char(j);
440 else if (i > s)
441 i = utf8_prev_char(i);
442 }
07630cea
LP
443
444 len = i - s;
445 len2 = s + old_length - j;
cb558ab2
ZJS
446
447 /* If we have ANSI, allow the same length as the source string + ellipsis. It'd be too involved to
448 * figure out what exact space is needed. Strings with ANSI sequences are most likely to be fairly
449 * short anyway. */
450 size_t alloc_len = has_ansi_seq ? old_length + 3 + 1 : len + 3 + len2 + 1;
451
452 char *e = new(char, alloc_len);
07630cea
LP
453 if (!e)
454 return NULL;
455
cb558ab2 456 memcpy_safe(e, s, len);
d723363a 457 write_ellipsis(e + len, /* unicode = */ true);
cb558ab2
ZJS
458
459 char *dst = e + len + 3;
460
461 if (has_ansi_seq)
462 /* Copy over any ANSI sequences in full */
463 for (const char *p = s + len; p < j; ) {
464 size_t slen = ansi_sequence_length(p, j - p);
465 if (slen > 0) {
be492020 466 dst = mempcpy(dst, p, slen);
cb558ab2
ZJS
467 p += slen;
468 } else
469 p = utf8_next_char(p);
470 }
471
472 memcpy_safe(dst, j, len2);
473 dst[len2] = '\0';
07630cea
LP
474
475 return e;
476}
477
ff3f2953 478char* cellescape(char *buf, size_t len, const char *s) {
8409f688
ZJS
479 /* Escape and ellipsize s into buffer buf of size len. Only non-control ASCII
480 * characters are copied as they are, everything else is escaped. The result
481 * is different then if escaping and ellipsization was performed in two
482 * separate steps, because each sequence is either stored in full or skipped.
483 *
484 * This function should be used for logging about strings which expected to
485 * be plain ASCII in a safe way.
486 *
487 * An ellipsis will be used if s is too long. It was always placed at the
488 * very end.
489 */
490
a01080ce 491 size_t i = 0, last_char_width[4] = {}, k = 0;
61f6e276 492
596c9e67 493 assert(buf);
61f6e276 494 assert(len > 0); /* at least a terminating NUL */
596c9e67 495 assert(s);
8409f688 496
61f6e276
LP
497 for (;;) {
498 char four[4];
499 int w;
8409f688 500
61f6e276 501 if (*s == 0) /* terminating NUL detected? then we are done! */
8409f688 502 goto done;
61f6e276
LP
503
504 w = cescape_char(*s, four);
505 if (i + w + 1 > len) /* This character doesn't fit into the buffer anymore? In that case let's
506 * ellipsize at the previous location */
507 break;
508
509 /* OK, there was space, let's add this escaped character to the buffer */
510 memcpy(buf + i, four, w);
511 i += w;
512
513 /* And remember its width in the ring buffer */
514 last_char_width[k] = w;
515 k = (k + 1) % 4;
516
517 s++;
8409f688
ZJS
518 }
519
61f6e276
LP
520 /* Ellipsation is necessary. This means we might need to truncate the string again to make space for 4
521 * characters ideally, but the buffer is shorter than that in the first place take what we can get */
a01080ce 522 for (size_t j = 0; j < ELEMENTSOF(last_char_width); j++) {
61f6e276
LP
523
524 if (i + 4 <= len) /* nice, we reached our space goal */
525 break;
526
527 k = k == 0 ? 3 : k - 1;
528 if (last_char_width[k] == 0) /* bummer, we reached the beginning of the strings */
529 break;
530
531 assert(i >= last_char_width[k]);
532 i -= last_char_width[k];
8409f688
ZJS
533 }
534
61f6e276 535 if (i + 4 <= len) /* yay, enough space */
d723363a 536 i += write_ellipsis(buf + i, /* unicode = */ false);
61f6e276
LP
537 else if (i + 3 <= len) { /* only space for ".." */
538 buf[i++] = '.';
539 buf[i++] = '.';
540 } else if (i + 2 <= len) /* only space for a single "." */
541 buf[i++] = '.';
542 else
543 assert(i + 1 <= len);
544
596c9e67 545done:
8409f688
ZJS
546 buf[i] = '\0';
547 return buf;
548}
549
07630cea
LP
550char* strshorten(char *s, size_t l) {
551 assert(s);
552
d49dc7bb
LP
553 if (l >= SIZE_MAX-1) /* Would not change anything */
554 return s;
555
47b33c7d 556 if (strnlen(s, l+1) > l)
07630cea
LP
557 s[l] = 0;
558
559 return s;
560}
561
2812017c 562int strgrowpad0(char **s, size_t l) {
8e479584
LP
563 size_t sz;
564
2812017c
DDM
565 assert(s);
566
8e479584
LP
567 if (*s) {
568 sz = strlen(*s) + 1;
569 if (sz >= l) /* never shrink */
570 return 0;
571 } else
572 sz = 0;
573
2812017c
DDM
574 char *q = realloc(*s, l);
575 if (!q)
576 return -ENOMEM;
8e479584 577
2812017c
DDM
578 *s = q;
579
2812017c
DDM
580 memzero(*s + sz, l - sz);
581 return 0;
582}
583
ff3f2953 584char* strreplace(const char *text, const char *old_string, const char *new_string) {
319a4f4b 585 size_t l, old_len, new_len;
9d73565a 586 char *t, *ret = NULL;
07630cea 587 const char *f;
07630cea 588
07630cea
LP
589 assert(old_string);
590 assert(new_string);
591
9d73565a
LP
592 if (!text)
593 return NULL;
594
07630cea
LP
595 old_len = strlen(old_string);
596 new_len = strlen(new_string);
597
598 l = strlen(text);
319a4f4b 599 if (!GREEDY_REALLOC(ret, l+1))
07630cea
LP
600 return NULL;
601
602 f = text;
9d73565a 603 t = ret;
07630cea 604 while (*f) {
07630cea
LP
605 size_t d, nl;
606
607 if (!startswith(f, old_string)) {
608 *(t++) = *(f++);
609 continue;
610 }
611
9d73565a 612 d = t - ret;
07630cea 613 nl = l - old_len + new_len;
9d73565a 614
319a4f4b 615 if (!GREEDY_REALLOC(ret, nl + 1))
9d73565a 616 return mfree(ret);
07630cea
LP
617
618 l = nl;
9d73565a 619 t = ret + d;
07630cea
LP
620
621 t = stpcpy(t, new_string);
622 f += old_len;
623 }
624
625 *t = 0;
9d73565a 626 return ret;
07630cea
LP
627}
628
6fb05690
LP
629static void advance_offsets(
630 ssize_t diff,
631 size_t offsets[2], /* note: we can't use [static 2] here, since this may be NULL */
632 size_t shift[static 2],
633 size_t size) {
634
b4766d5f
ZJS
635 if (!offsets)
636 return;
637
6fb05690
LP
638 assert(shift);
639
b4766d5f
ZJS
640 if ((size_t) diff < offsets[0])
641 shift[0] += size;
642 if ((size_t) diff < offsets[1])
643 shift[1] += size;
644}
645
ff3f2953 646char* strip_tab_ansi(char **ibuf, size_t *_isz, size_t highlight[2]) {
62a3fc6d 647 const char *begin = NULL;
07630cea
LP
648 enum {
649 STATE_OTHER,
650 STATE_ESCAPE,
695a944c 651 STATE_CSI,
e65b0904 652 STATE_OSC,
a39c5179 653 STATE_OSC_CLOSING,
07630cea 654 } state = STATE_OTHER;
2485b7e2
YW
655 _cleanup_(memstream_done) MemStream m = {};
656 size_t isz, shift[2] = {}, n_carriage_returns = 0;
657 FILE *f;
07630cea
LP
658
659 assert(ibuf);
660 assert(*ibuf);
661
695a944c
LP
662 /* This does three things:
663 *
664 * 1. Replaces TABs by 8 spaces
665 * 2. Strips ANSI color sequences (a subset of CSI), i.e. ESC '[' … 'm' sequences
a39c5179 666 * 3. Strips ANSI operating system sequences (OSC), i.e. ESC ']' … ST sequences
62a3fc6d
ZJS
667 * 4. Strip trailing \r characters (since they would "move the cursor", but have no
668 * other effect).
695a944c 669 *
2fe21124
ZJS
670 * Everything else will be left as it is. In particular other ANSI sequences are left as they are, as
671 * are any other special characters. Truncated ANSI sequences are left-as is too. This call is
672 * supposed to suppress the most basic formatting noise, but nothing else.
695a944c 673 *
e65b0904 674 * Why care for OSC sequences? Well, to undo what terminal_urlify() and friends generate. */
07630cea
LP
675
676 isz = _isz ? *_isz : strlen(*ibuf);
677
2fe21124
ZJS
678 /* Note we turn off internal locking on f for performance reasons. It's safe to do so since we
679 * created f here and it doesn't leave our scope. */
2485b7e2 680 f = memstream_init(&m);
07630cea
LP
681 if (!f)
682 return NULL;
683
62a3fc6d 684 for (const char *i = *ibuf; i < *ibuf + isz + 1; i++) {
07630cea 685
f79562aa
LP
686 bool eot = i >= *ibuf + isz;
687
07630cea
LP
688 switch (state) {
689
690 case STATE_OTHER:
f79562aa 691 if (eot)
07630cea 692 break;
62a3fc6d
ZJS
693
694 if (*i == '\r') {
695 n_carriage_returns++;
696 break;
697 } else if (*i == '\n')
698 /* Ignore carriage returns before new line */
699 n_carriage_returns = 0;
700 for (; n_carriage_returns > 0; n_carriage_returns--)
701 fputc('\r', f);
702
703 if (*i == '\x1B')
07630cea 704 state = STATE_ESCAPE;
b4766d5f 705 else if (*i == '\t') {
0d536673 706 fputs(" ", f);
b4766d5f
ZJS
707 advance_offsets(i - *ibuf, highlight, shift, 7);
708 } else
0d536673 709 fputc(*i, f);
b4766d5f 710
07630cea
LP
711 break;
712
713 case STATE_ESCAPE:
62a3fc6d
ZJS
714 assert(n_carriage_returns == 0);
715
f79562aa 716 if (eot) {
0d536673 717 fputc('\x1B', f);
b4766d5f 718 advance_offsets(i - *ibuf, highlight, shift, 1);
07630cea 719 break;
695a944c
LP
720 } else if (*i == '[') { /* ANSI CSI */
721 state = STATE_CSI;
722 begin = i + 1;
e65b0904
LP
723 } else if (*i == ']') { /* ANSI OSC */
724 state = STATE_OSC;
07630cea
LP
725 begin = i + 1;
726 } else {
0d536673
LP
727 fputc('\x1B', f);
728 fputc(*i, f);
b4766d5f 729 advance_offsets(i - *ibuf, highlight, shift, 1);
07630cea
LP
730 state = STATE_OTHER;
731 }
732
733 break;
734
695a944c 735 case STATE_CSI:
62a3fc6d 736 assert(n_carriage_returns == 0);
07630cea 737
f79562aa 738 if (eot || !strchr("01234567890;m", *i)) { /* EOT or invalid chars in sequence */
0d536673
LP
739 fputc('\x1B', f);
740 fputc('[', f);
b4766d5f 741 advance_offsets(i - *ibuf, highlight, shift, 2);
07630cea
LP
742 state = STATE_OTHER;
743 i = begin-1;
744 } else if (*i == 'm')
745 state = STATE_OTHER;
695a944c
LP
746
747 break;
748
e65b0904 749 case STATE_OSC:
62a3fc6d 750 assert(n_carriage_returns == 0);
695a944c 751
a39c5179
LP
752 /* There are three kinds of OSC terminators: \x07, \x1b\x5c or \x9c. We only support
753 * the first two, because the last one is a valid UTF-8 codepoint and hence creates
754 * an ambiguity (many Terminal emulators refuse to support it as well). */
0823d96a 755 if (eot || (!IN_SET(*i, '\x07', '\x1b') && !osc_char_is_valid(*i))) { /* EOT or invalid chars in sequence */
695a944c
LP
756 fputc('\x1B', f);
757 fputc(']', f);
758 advance_offsets(i - *ibuf, highlight, shift, 2);
759 state = STATE_OTHER;
760 i = begin-1;
a39c5179
LP
761 } else if (*i == '\x07') /* Single character ST */
762 state = STATE_OTHER;
763 else if (*i == '\x1B')
764 state = STATE_OSC_CLOSING;
765
766 break;
767
768 case STATE_OSC_CLOSING:
f79562aa 769 if (eot || *i != '\x5c') { /* EOT or incomplete two-byte ST in sequence */
a39c5179
LP
770 fputc('\x1B', f);
771 fputc(']', f);
772 advance_offsets(i - *ibuf, highlight, shift, 2);
773 state = STATE_OTHER;
774 i = begin-1;
775 } else if (*i == '\x5c')
695a944c
LP
776 state = STATE_OTHER;
777
07630cea
LP
778 break;
779 }
780 }
781
2485b7e2
YW
782 char *obuf;
783 if (memstream_finalize(&m, &obuf, _isz) < 0)
f392dfb5 784 return NULL;
07630cea 785
6fb05690 786 free_and_replace(*ibuf, obuf);
07630cea 787
b4766d5f
ZJS
788 if (highlight) {
789 highlight[0] += shift[0];
790 highlight[1] += shift[1];
791 }
792
6fb05690 793 return *ibuf;
07630cea
LP
794}
795
ff3f2953 796char* strextend_with_separator_internal(char **x, const char *separator, ...) {
34467ffa 797 _cleanup_free_ char *buffer = NULL;
bb8ad9ea 798 size_t f, l, l_separator;
c2bc710b
LP
799 bool need_separator;
800 char *nr, *p;
bb8ad9ea 801 va_list ap;
07630cea 802
34467ffa
LP
803 if (!x)
804 x = &buffer;
07630cea 805
7bf7ce28 806 l = f = strlen_ptr(*x);
07630cea 807
bb8ad9ea
LP
808 need_separator = !isempty(*x);
809 l_separator = strlen_ptr(separator);
810
811 va_start(ap, separator);
34467ffa 812 for (const char *t;;) {
07630cea
LP
813 size_t n;
814
815 t = va_arg(ap, const char *);
816 if (!t)
817 break;
fd3b7cf7
LP
818 if (t == POINTER_MAX)
819 continue;
07630cea
LP
820
821 n = strlen(t);
bb8ad9ea
LP
822
823 if (need_separator)
824 n += l_separator;
825
c2bc710b 826 if (n >= SIZE_MAX - l) {
07630cea
LP
827 va_end(ap);
828 return NULL;
829 }
830
831 l += n;
bb8ad9ea 832 need_separator = true;
07630cea
LP
833 }
834 va_end(ap);
835
bb8ad9ea
LP
836 need_separator = !isempty(*x);
837
2a4e1fd0 838 nr = realloc(*x, GREEDY_ALLOC_ROUND_UP(l+1));
c2bc710b 839 if (!nr)
07630cea
LP
840 return NULL;
841
c2bc710b
LP
842 *x = nr;
843 p = nr + f;
07630cea 844
bb8ad9ea 845 va_start(ap, separator);
07630cea
LP
846 for (;;) {
847 const char *t;
848
849 t = va_arg(ap, const char *);
850 if (!t)
851 break;
fd3b7cf7
LP
852 if (t == POINTER_MAX)
853 continue;
07630cea 854
bb8ad9ea
LP
855 if (need_separator && separator)
856 p = stpcpy(p, separator);
857
07630cea 858 p = stpcpy(p, t);
bb8ad9ea
LP
859
860 need_separator = true;
07630cea
LP
861 }
862 va_end(ap);
863
c2bc710b 864 assert(p == nr + l);
07630cea 865 *p = 0;
07630cea 866
34467ffa
LP
867 /* If no buffer to extend was passed in return the start of the buffer */
868 if (buffer)
869 return TAKE_PTR(buffer);
870
871 /* Otherwise we extended the buffer: return the end */
c2bc710b 872 return p;
07630cea
LP
873}
874
6b13ca8a
YW
875int strextendf_with_separator(char **x, const char *separator, const char *format, ...) {
876 size_t m, a, l_separator;
e9b88a6d
LP
877 va_list ap;
878 int l;
879
880 /* Appends a formatted string to the specified string. Don't use this in inner loops, since then
881 * we'll spend a tonload of time in determining the length of the string passed in, over and over
882 * again. */
883
884 assert(x);
885 assert(format);
886
6b13ca8a
YW
887 l_separator = isempty(*x) ? 0 : strlen_ptr(separator);
888
e9b88a6d
LP
889 /* Let's try to use the allocated buffer, if there's room at the end still. Otherwise let's extend by 64 chars. */
890 if (*x) {
891 m = strlen(*x);
6df28e1f 892 a = MALLOC_SIZEOF_SAFE(*x);
e9b88a6d
LP
893 assert(a >= m + 1);
894 } else
895 m = a = 0;
896
6b13ca8a 897 if (a - m < 17 + l_separator) { /* if there's less than 16 chars space, then enlarge the buffer first */
e9b88a6d
LP
898 char *n;
899
6b13ca8a
YW
900 if (_unlikely_(l_separator > SIZE_MAX - 64)) /* overflow check #1 */
901 return -ENOMEM;
902 if (_unlikely_(m > SIZE_MAX - 64 - l_separator)) /* overflow check #2 */
e9b88a6d
LP
903 return -ENOMEM;
904
6b13ca8a 905 n = realloc(*x, m + 64 + l_separator);
e9b88a6d
LP
906 if (!n)
907 return -ENOMEM;
908
909 *x = n;
6df28e1f 910 a = MALLOC_SIZEOF_SAFE(*x);
e9b88a6d
LP
911 }
912
913 /* Now, let's try to format the string into it */
6b13ca8a 914 memcpy_safe(*x + m, separator, l_separator);
e9b88a6d 915 va_start(ap, format);
6b13ca8a 916 l = vsnprintf(*x + m + l_separator, a - m - l_separator, format, ap);
e9b88a6d
LP
917 va_end(ap);
918
919 assert(l >= 0);
920
6b13ca8a 921 if ((size_t) l < a - m - l_separator) {
e9b88a6d
LP
922 char *n;
923
924 /* Nice! This worked. We are done. But first, let's return the extra space we don't
925 * need. This should be a cheap operation, since we only lower the allocation size here,
926 * never increase. */
6b13ca8a 927 n = realloc(*x, m + (size_t) l + l_separator + 1);
e9b88a6d
LP
928 if (n)
929 *x = n;
930 } else {
931 char *n;
932
933 /* Wasn't enough. Then let's allocate exactly what we need. */
934
6b13ca8a 935 if (_unlikely_((size_t) l > SIZE_MAX - (l_separator + 1))) /* overflow check #1 */
e9b88a6d 936 goto oom;
6b13ca8a 937 if (_unlikely_(m > SIZE_MAX - ((size_t) l + l_separator + 1))) /* overflow check #2 */
e9b88a6d
LP
938 goto oom;
939
6b13ca8a 940 a = m + (size_t) l + l_separator + 1;
e9b88a6d
LP
941 n = realloc(*x, a);
942 if (!n)
943 goto oom;
944 *x = n;
945
946 va_start(ap, format);
6b13ca8a 947 l = vsnprintf(*x + m + l_separator, a - m - l_separator, format, ap);
e9b88a6d
LP
948 va_end(ap);
949
6b13ca8a 950 assert((size_t) l < a - m - l_separator);
e9b88a6d
LP
951 }
952
953 return 0;
954
955oom:
dbbc86ff 956 /* truncate the bytes added after memcpy_safe() again */
e9b88a6d
LP
957 (*x)[m] = 0;
958 return -ENOMEM;
959}
960
ff3f2953 961char* strrep(const char *s, unsigned n) {
07630cea 962 char *r, *p;
fe96c0f8 963 size_t l;
07630cea
LP
964
965 assert(s);
966
967 l = strlen(s);
968 p = r = malloc(l * n + 1);
969 if (!r)
970 return NULL;
971
fe96c0f8 972 for (unsigned i = 0; i < n; i++)
07630cea
LP
973 p = stpcpy(p, s);
974
975 *p = 0;
976 return r;
977}
978
ac3f3026 979int split_pair(const char *s, const char *sep, char **ret_first, char **ret_second) {
07630cea 980 assert(s);
ac3f3026
YW
981 assert(!isempty(sep));
982 assert(ret_first);
983 assert(ret_second);
07630cea 984
ac3f3026 985 const char *x = strstr(s, sep);
07630cea
LP
986 if (!x)
987 return -EINVAL;
988
ac3f3026 989 _cleanup_free_ char *a = strndup(s, x - s);
07630cea
LP
990 if (!a)
991 return -ENOMEM;
992
ac3f3026
YW
993 _cleanup_free_ char *b = strdup(x + strlen(sep));
994 if (!b)
07630cea 995 return -ENOMEM;
07630cea 996
ac3f3026
YW
997 *ret_first = TAKE_PTR(a);
998 *ret_second = TAKE_PTR(b);
07630cea
LP
999 return 0;
1000}
1001
1002int free_and_strdup(char **p, const char *s) {
1003 char *t;
1004
1005 assert(p);
1006
7f546026 1007 /* Replaces a string pointer with a strdup()ed new string,
07630cea
LP
1008 * possibly freeing the old one. */
1009
1010 if (streq_ptr(*p, s))
1011 return 0;
1012
1013 if (s) {
1014 t = strdup(s);
1015 if (!t)
1016 return -ENOMEM;
1017 } else
1018 t = NULL;
1019
d6f2cd67 1020 free_and_replace(*p, t);
07630cea
LP
1021
1022 return 1;
1023}
1024
93a1f792
DDM
1025int free_and_strdup_warn(char **p, const char *s) {
1026 int r;
1027
1028 r = free_and_strdup(p, s);
1029 if (r < 0)
1030 return log_oom();
1031 return r;
1032}
1033
7f546026
ZJS
1034int free_and_strndup(char **p, const char *s, size_t l) {
1035 char *t;
1036
1037 assert(p);
1038 assert(s || l == 0);
1039
1040 /* Replaces a string pointer with a strndup()ed new string,
1041 * freeing the old one. */
1042
1043 if (!*p && !s)
1044 return 0;
1045
1046 if (*p && s && strneq(*p, s, l) && (l > strlen(*p) || (*p)[l] == '\0'))
1047 return 0;
1048
1049 if (s) {
1050 t = strndup(s, l);
1051 if (!t)
1052 return -ENOMEM;
1053 } else
1054 t = NULL;
1055
1056 free_and_replace(*p, t);
1057 return 1;
1058}
1059
892c5902
ZJS
1060int strdup_to_full(char **ret, const char *src) {
1061 if (!src) {
1062 if (ret)
1063 *ret = NULL;
1064
1065 return 0;
1066 } else {
1067 if (ret) {
1068 char *t = strdup(src);
1069 if (!t)
1070 return -ENOMEM;
1071 *ret = t;
1072 }
1073
1074 return 1;
1075 }
1076};
1077
f3e2e81d 1078bool string_is_safe(const char *p) {
f3e2e81d
LP
1079 if (!p)
1080 return false;
1081
839d1b20
LP
1082 /* Checks if the specified string contains no quotes or control characters */
1083
a01080ce 1084 for (const char *t = p; *t; t++) {
f3e2e81d
LP
1085 if (*t > 0 && *t < ' ') /* no control characters */
1086 return false;
1087
1088 if (strchr(QUOTES "\\\x7f", *t))
1089 return false;
1090 }
1091
1092 return true;
1093}
53caaffd 1094
e4a08721
DDM
1095bool string_is_safe_ascii(const char *p) {
1096 return ascii_is_valid(p) && string_is_safe(p);
1097}
1098
1099char* str_realloc(char *p) {
1100 /* Reallocate *p to actual size. Ignore failure, and return the original string on error. */
1101
1102 if (!p)
1103 return NULL;
1104
1105 return realloc(p, strlen(p) + 1) ?: p;
1106}
1107
53caaffd
LP
1108char* string_erase(char *x) {
1109 if (!x)
1110 return NULL;
1111
1112 /* A delicious drop of snake-oil! To be called on memory where we stored passphrases or so, after we
1113 * used them. */
1114 explicit_bzero_safe(x, strlen(x));
1115 return x;
1116}
8dd6491e
LP
1117
1118int string_truncate_lines(const char *s, size_t n_lines, char **ret) {
1119 const char *p = s, *e = s;
1120 bool truncation_applied = false;
1121 char *copy;
1122 size_t n = 0;
1123
1124 assert(s);
1125
1126 /* Truncate after the specified number of lines. Returns > 0 if a truncation was applied or == 0 if
1127 * there were fewer lines in the string anyway. Trailing newlines on input are ignored, and not
1128 * generated either. */
1129
1130 for (;;) {
1131 size_t k;
1132
1133 k = strcspn(p, "\n");
1134
1135 if (p[k] == 0) {
1136 if (k == 0) /* final empty line */
1137 break;
1138
1139 if (n >= n_lines) /* above threshold */
1140 break;
1141
1142 e = p + k; /* last line to include */
1143 break;
1144 }
1145
1146 assert(p[k] == '\n');
1147
1148 if (n >= n_lines)
1149 break;
1150
1151 if (k > 0)
1152 e = p + k;
1153
1154 p += k + 1;
1155 n++;
1156 }
1157
1158 /* e points after the last character we want to keep */
1159 if (isempty(e))
1160 copy = strdup(s);
1161 else {
1162 if (!in_charset(e, "\n")) /* We only consider things truncated if we remove something that
1163 * isn't a new-line or a series of them */
1164 truncation_applied = true;
1165
1166 copy = strndup(s, e - s);
1167 }
1168 if (!copy)
1169 return -ENOMEM;
1170
1171 *ret = copy;
1172 return truncation_applied;
1173}
f6857fa6
LP
1174
1175int string_extract_line(const char *s, size_t i, char **ret) {
1176 const char *p = s;
1177 size_t c = 0;
1178
1179 /* Extract the i'nth line from the specified string. Returns > 0 if there are more lines after that,
1180 * and == 0 if we are looking at the last line or already beyond the last line. As special
1181 * optimization, if the first line is requested and the string only consists of one line we return
1182 * NULL, indicating the input string should be used as is, and avoid a memory allocation for a very
1183 * common case. */
1184
1185 for (;;) {
1186 const char *q;
1187
1188 q = strchr(p, '\n');
1189 if (i == c) {
1190 /* The line we are looking for! */
1191
1192 if (q) {
1193 char *m;
1194
1195 m = strndup(p, q - p);
1196 if (!m)
1197 return -ENOMEM;
1198
1199 *ret = m;
f174b294
ZJS
1200 return !isempty(q + 1); /* More coming? */
1201 } else
1202 /* Tell the caller to use the input string if equal */
1203 return strdup_to(ret, p != s ? p : NULL);
f6857fa6
LP
1204 }
1205
f174b294 1206 if (!q)
f6857fa6 1207 /* No more lines, return empty line */
f174b294 1208 return strdup_to(ret, "");
f6857fa6
LP
1209
1210 p = q + 1;
1211 c++;
1212 }
1213}
53cd7f33 1214
f8c70079
MY
1215int string_contains_word_strv(const char *string, const char *separators, char * const *words, const char **ret_word) {
1216 /* In the default mode with no separators specified, we split on whitespace and coalesce separators. */
53cd7f33 1217 const ExtractFlags flags = separators ? EXTRACT_DONT_COALESCE_SEPARATORS : 0;
46bf625a 1218 const char *found = NULL;
f8c70079 1219 int r;
46bf625a 1220
f8c70079 1221 for (;;) {
53cd7f33 1222 _cleanup_free_ char *w = NULL;
53cd7f33 1223
f8c70079 1224 r = extract_first_word(&string, &w, separators, flags);
53cd7f33
ZJS
1225 if (r < 0)
1226 return r;
1227 if (r == 0)
46bf625a
ZJS
1228 break;
1229
1230 found = strv_find(words, w);
1231 if (found)
1232 break;
53cd7f33 1233 }
46bf625a
ZJS
1234
1235 if (ret_word)
1236 *ret_word = found;
1237 return !!found;
53cd7f33 1238}
8034b42c
ADT
1239
1240bool streq_skip_trailing_chars(const char *s1, const char *s2, const char *ok) {
1241 if (!s1 && !s2)
1242 return true;
1243 if (!s1 || !s2)
1244 return false;
1245
1246 if (!ok)
1247 ok = WHITESPACE;
1248
1249 for (; *s1 && *s2; s1++, s2++)
1250 if (*s1 != *s2)
1251 break;
1252
1253 return in_charset(s1, ok) && in_charset(s2, ok);
1254}
072f5f9b 1255
ff3f2953 1256char* string_replace_char(char *str, char old_char, char new_char) {
072f5f9b
YW
1257 assert(str);
1258 assert(old_char != '\0');
1259 assert(new_char != '\0');
1260 assert(old_char != new_char);
1261
1262 for (char *p = strchr(str, old_char); p; p = strchr(p + 1, old_char))
1263 *p = new_char;
1264
1265 return str;
1266}
146f4482 1267
7153213e
LP
1268int make_cstring(const char *s, size_t n, MakeCStringMode mode, char **ret) {
1269 char *b;
1270
1271 assert(s || n == 0);
1272 assert(mode >= 0);
1273 assert(mode < _MAKE_CSTRING_MODE_MAX);
1274
1275 /* Converts a sized character buffer into a NUL-terminated NUL string, refusing if there are embedded
1276 * NUL bytes. Whether to expect a trailing NUL byte can be specified via 'mode' */
1277
1278 if (n == 0) {
1279 if (mode == MAKE_CSTRING_REQUIRE_TRAILING_NUL)
1280 return -EINVAL;
1281
1282 if (!ret)
1283 return 0;
1284
1285 b = new0(char, 1);
1286 } else {
1287 const char *nul;
1288
1289 nul = memchr(s, 0, n);
1290 if (nul) {
1291 if (nul < s + n - 1 || /* embedded NUL? */
1292 mode == MAKE_CSTRING_REFUSE_TRAILING_NUL)
1293 return -EINVAL;
1294
1295 n--;
1296 } else if (mode == MAKE_CSTRING_REQUIRE_TRAILING_NUL)
1297 return -EINVAL;
1298
1299 if (!ret)
1300 return 0;
1301
1302 b = memdup_suffix0(s, n);
1303 }
1304 if (!b)
1305 return -ENOMEM;
1306
1307 *ret = b;
1308 return 0;
1309}
1310
146f4482
YW
1311size_t strspn_from_end(const char *str, const char *accept) {
1312 size_t n = 0;
1313
1314 if (isempty(str))
1315 return 0;
1316
1317 if (isempty(accept))
1318 return 0;
1319
1320 for (const char *p = str + strlen(str); p > str && strchr(accept, p[-1]); p--)
1321 n++;
1322
1323 return n;
1324}
e8bec624 1325
ff3f2953 1326char* strdupspn(const char *a, const char *accept) {
e8bec624
LP
1327 if (isempty(a) || isempty(accept))
1328 return strdup("");
1329
1330 return strndup(a, strspn(a, accept));
1331}
1332
ff3f2953 1333char* strdupcspn(const char *a, const char *reject) {
e8bec624
LP
1334 if (isempty(a))
1335 return strdup("");
1336 if (isempty(reject))
1337 return strdup(a);
1338
1339 return strndup(a, strcspn(a, reject));
1340}
7b82d95f 1341
ff3f2953 1342char* find_line_startswith(const char *haystack, const char *needle) {
7b82d95f
LP
1343 char *p;
1344
1345 assert(haystack);
1346 assert(needle);
1347
1348 /* Finds the first line in 'haystack' that starts with the specified string. Returns a pointer to the
1349 * first character after it */
1350
1351 p = strstr(haystack, needle);
1352 if (!p)
1353 return NULL;
1354
1355 if (p > haystack)
1356 while (p[-1] != '\n') {
1357 p = strstr(p + 1, needle);
1358 if (!p)
1359 return NULL;
1360 }
1361
1362 return p + strlen(needle);
1363}
70cc7ed9 1364
ba2d8107
AP
1365char* find_line(const char *haystack, const char *needle) {
1366 char *p;
1367
1368 assert(haystack);
1369 assert(needle);
1370
1371 /* Finds the first line in 'haystack' that match the specified string. Returns a pointer to the
1372 * beginning of the line */
1373
1374 p = find_line_startswith(haystack, needle);
1375 if (!p)
1376 return NULL;
1377
1378 if (*p == 0 || strchr(NEWLINE, *p))
1379 return p - strlen(needle);
1380
1381 return NULL;
1382}
1383
1384char* find_line_after(const char *haystack, const char *needle) {
1385 char *p;
1386
1387 assert(haystack);
1388 assert(needle);
1389
1390 /* Finds the first line in 'haystack' that match the specified string. Returns a pointer to the
1391 * next line after it */
1392
1393 p = find_line_startswith(haystack, needle);
1394 if (!p)
1395 return NULL;
1396
1397 if (*p == 0)
1398 return p;
1399 if (strchr(NEWLINE, *p))
1400 return p + 1;
1401
1402 return NULL;
1403}
1404
f5c6b4f4
LP
1405bool version_is_valid(const char *s) {
1406 if (isempty(s))
1407 return false;
1408
1409 if (!filename_part_is_valid(s))
1410 return false;
1411
1412 /* This is a superset of the characters used by semver. We additionally allow "," and "_". */
1413 if (!in_charset(s, ALPHANUMERICAL ".,_-+"))
1414 return false;
1415
1416 return true;
1417}
c46f5680
JB
1418
1419bool version_is_valid_versionspec(const char *s) {
1420 if (!filename_part_is_valid(s))
1421 return false;
1422
1423 if (!in_charset(s, ALPHANUMERICAL "-.~^"))
1424 return false;
1425
1426 return true;
1427}
7ef5b0a4
LP
1428
1429ssize_t strlevenshtein(const char *x, const char *y) {
1430 _cleanup_free_ size_t *t0 = NULL, *t1 = NULL, *t2 = NULL;
1431 size_t xl, yl;
1432
1433 /* This is inspired from the Linux kernel's Levenshtein implementation */
1434
1435 if (streq_ptr(x, y))
1436 return 0;
1437
1438 xl = strlen_ptr(x);
1439 if (xl > SSIZE_MAX)
1440 return -E2BIG;
1441
1442 yl = strlen_ptr(y);
1443 if (yl > SSIZE_MAX)
1444 return -E2BIG;
1445
1446 if (isempty(x))
1447 return yl;
1448 if (isempty(y))
1449 return xl;
1450
1451 t0 = new0(size_t, yl + 1);
1452 if (!t0)
1453 return -ENOMEM;
1454 t1 = new0(size_t, yl + 1);
1455 if (!t1)
1456 return -ENOMEM;
1457 t2 = new0(size_t, yl + 1);
1458 if (!t2)
1459 return -ENOMEM;
1460
1461 for (size_t i = 0; i <= yl; i++)
1462 t1[i] = i;
1463
1464 for (size_t i = 0; i < xl; i++) {
1465 t2[0] = i + 1;
1466
1467 for (size_t j = 0; j < yl; j++) {
1468 /* Substitution */
1469 t2[j+1] = t1[j] + (x[i] != y[j]);
1470
1471 /* Swap */
1472 if (i > 0 && j > 0 && x[i-1] == y[j] && x[i] == y[j-1] && t2[j+1] > t0[j-1] + 1)
1473 t2[j+1] = t0[j-1] + 1;
1474
1475 /* Deletion */
1476 if (t2[j+1] > t1[j+1] + 1)
1477 t2[j+1] = t1[j+1] + 1;
1478
1479 /* Insertion */
1480 if (t2[j+1] > t2[j] + 1)
1481 t2[j+1] = t2[j] + 1;
1482 }
1483
1484 size_t *dummy = t0;
1485 t0 = t1;
1486 t1 = t2;
1487 t2 = dummy;
1488 }
1489
1490 return t1[yl];
1491}
63566c6b 1492
ff3f2953 1493char* strrstr(const char *haystack, const char *needle) {
9e44842a 1494 /* Like strstr() but returns the last rather than the first occurrence of "needle" in "haystack". */
63566c6b
LP
1495
1496 if (!haystack || !needle)
1497 return NULL;
1498
9e44842a 1499 /* Special case: for the empty string we return the very last possible occurrence, i.e. *after* the
63566c6b 1500 * last char, not before. */
aa9ff6c2 1501 if (*needle == 0)
63566c6b
LP
1502 return strchr(haystack, 0);
1503
aa9ff6c2
R
1504 for (const char *p = strstr(haystack, needle), *q; p; p = q) {
1505 q = strstr(p + 1, needle);
1506 if (!q)
1507 return (char *) p;
1508 }
1509 return NULL;
63566c6b 1510}
f77f363c
LP
1511
1512size_t str_common_prefix(const char *a, const char *b) {
1513 assert(a);
1514 assert(b);
1515
1516 /* Returns the length of the common prefix of the two specified strings, or SIZE_MAX in case the
1517 * strings are fully identical. */
1518
1519 for (size_t n = 0;; n++) {
1520 char c = a[n];
1521 if (c != b[n])
1522 return n;
1523 if (c == 0)
1524 return SIZE_MAX;
1525 }
1526}