]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/string-util.c
2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 #include "alloc-util.h"
29 #include "string-util.h"
33 int strcmp_ptr(const char *a
, const char *b
) {
35 /* Like strcmp(), but tries to make sense of NULL pointers */
48 char* endswith(const char *s
, const char *postfix
) {
58 return (char*) s
+ sl
;
63 if (memcmp(s
+ sl
- pl
, postfix
, pl
) != 0)
66 return (char*) s
+ sl
- pl
;
69 char* endswith_no_case(const char *s
, const char *postfix
) {
79 return (char*) s
+ sl
;
84 if (strcasecmp(s
+ sl
- pl
, postfix
) != 0)
87 return (char*) s
+ sl
- pl
;
90 char* first_word(const char *s
, const char *word
) {
97 /* Checks if the string starts with the specified word, either
98 * followed by NUL or by whitespace. Returns a pointer to the
99 * NUL or the first character after the whitespace. */
110 if (memcmp(s
, word
, wl
) != 0)
117 if (!strchr(WHITESPACE
, *p
))
120 p
+= strspn(p
, WHITESPACE
);
124 static size_t strcspn_escaped(const char *s
, const char *reject
) {
125 bool escaped
= false;
128 for (n
=0; s
[n
]; n
++) {
131 else if (s
[n
] == '\\')
133 else if (strchr(reject
, s
[n
]))
137 /* if s ends in \, return index of previous char */
141 /* Split a string into words. */
142 const char* split(const char **state
, size_t *l
, const char *separator
, bool quoted
) {
148 assert(**state
== '\0');
152 current
+= strspn(current
, separator
);
158 if (quoted
&& strchr("\'\"", *current
)) {
159 char quotechars
[2] = {*current
, '\0'};
161 *l
= strcspn_escaped(current
+ 1, quotechars
);
162 if (current
[*l
+ 1] == '\0' || current
[*l
+ 1] != quotechars
[0] ||
163 (current
[*l
+ 2] && !strchr(separator
, current
[*l
+ 2]))) {
164 /* right quote missing or garbage at the end */
168 *state
= current
++ + *l
+ 2;
170 *l
= strcspn_escaped(current
, separator
);
171 if (current
[*l
] && !strchr(separator
, current
[*l
])) {
172 /* unfinished escape */
176 *state
= current
+ *l
;
178 *l
= strcspn(current
, separator
);
179 *state
= current
+ *l
;
185 char *strnappend(const char *s
, const char *suffix
, size_t b
) {
193 return strndup(suffix
, b
);
202 if (b
> ((size_t) -1) - a
)
205 r
= new(char, a
+b
+1);
210 memcpy(r
+a
, suffix
, b
);
216 char *strappend(const char *s
, const char *suffix
) {
217 return strnappend(s
, suffix
, suffix
? strlen(suffix
) : 0);
220 char *strjoin(const char *x
, ...) {
234 t
= va_arg(ap
, const char *);
239 if (n
> ((size_t) -1) - l
) {
263 t
= va_arg(ap
, const char *);
277 char *strstrip(char *s
) {
280 /* Drops trailing whitespace. Modifies the string in
281 * place. Returns pointer to first non-space character */
283 s
+= strspn(s
, WHITESPACE
);
285 for (e
= strchr(s
, 0); e
> s
; e
--)
286 if (!strchr(WHITESPACE
, e
[-1]))
294 char *delete_chars(char *s
, const char *bad
) {
297 /* Drops all whitespace, regardless where in the string */
299 for (f
= s
, t
= s
; *f
; f
++) {
311 char *truncate_nl(char *s
) {
314 s
[strcspn(s
, NEWLINE
)] = 0;
318 char ascii_tolower(char x
) {
320 if (x
>= 'A' && x
<= 'Z')
321 return x
- 'A' + 'a';
326 char *ascii_strlower(char *t
) {
332 *p
= ascii_tolower(*p
);
337 char *ascii_strlower_n(char *t
, size_t n
) {
343 for (i
= 0; i
< n
; i
++)
344 t
[i
] = ascii_tolower(t
[i
]);
349 int ascii_strcasecmp_n(const char *a
, const char *b
, size_t n
) {
351 for (; n
> 0; a
++, b
++, n
--) {
354 x
= (int) (uint8_t) ascii_tolower(*a
);
355 y
= (int) (uint8_t) ascii_tolower(*b
);
364 int ascii_strcasecmp_nn(const char *a
, size_t n
, const char *b
, size_t m
) {
367 r
= ascii_strcasecmp_n(a
, b
, MIN(n
, m
));
379 bool chars_intersect(const char *a
, const char *b
) {
382 /* Returns true if any of the chars in a are in b. */
390 bool string_has_cc(const char *p
, const char *ok
) {
396 * Check if a string contains control characters. If 'ok' is
397 * non-NULL it may be a string containing additional CCs to be
401 for (t
= p
; *t
; t
++) {
402 if (ok
&& strchr(ok
, *t
))
405 if (*t
> 0 && *t
< ' ')
415 static char *ascii_ellipsize_mem(const char *s
, size_t old_length
, size_t new_length
, unsigned percent
) {
420 assert(percent
<= 100);
421 assert(new_length
>= 3);
423 if (old_length
<= 3 || old_length
<= new_length
)
424 return strndup(s
, old_length
);
426 r
= new0(char, new_length
+1);
430 x
= (new_length
* percent
) / 100;
432 if (x
> new_length
- 3)
440 s
+ old_length
- (new_length
- x
- 3),
446 char *ellipsize_mem(const char *s
, size_t old_length
, size_t new_length
, unsigned percent
) {
450 unsigned k
, len
, len2
;
454 assert(percent
<= 100);
455 assert(new_length
>= 3);
457 /* if no multibyte characters use ascii_ellipsize_mem for speed */
458 if (ascii_is_valid(s
))
459 return ascii_ellipsize_mem(s
, old_length
, new_length
, percent
);
461 if (old_length
<= 3 || old_length
<= new_length
)
462 return strndup(s
, old_length
);
464 x
= (new_length
* percent
) / 100;
466 if (x
> new_length
- 3)
470 for (i
= s
; k
< x
&& i
< s
+ old_length
; i
= utf8_next_char(i
)) {
473 r
= utf8_encoded_to_unichar(i
, &c
);
476 k
+= unichar_iswide(c
) ? 2 : 1;
479 if (k
> x
) /* last character was wide and went over quota */
482 for (j
= s
+ old_length
; k
< new_length
&& j
> i
; ) {
485 j
= utf8_prev_char(j
);
486 r
= utf8_encoded_to_unichar(j
, &c
);
489 k
+= unichar_iswide(c
) ? 2 : 1;
493 /* we don't actually need to ellipsize */
495 return memdup(s
, old_length
+ 1);
497 /* make space for ellipsis */
498 j
= utf8_next_char(j
);
501 len2
= s
+ old_length
- j
;
502 e
= new(char, len
+ 3 + len2
+ 1);
507 printf("old_length=%zu new_length=%zu x=%zu len=%u len2=%u k=%u\n",
508 old_length, new_length, x, len, len2, k);
512 e
[len
] = 0xe2; /* tri-dot ellipsis: … */
516 memcpy(e
+ len
+ 3, j
, len2
+ 1);
521 char *ellipsize(const char *s
, size_t length
, unsigned percent
) {
522 return ellipsize_mem(s
, strlen(s
), length
, percent
);
525 bool nulstr_contains(const char*nulstr
, const char *needle
) {
531 NULSTR_FOREACH(i
, nulstr
)
532 if (streq(i
, needle
))
538 char* strshorten(char *s
, size_t l
) {
547 char *strreplace(const char *text
, const char *old_string
, const char *new_string
) {
550 size_t l
, old_len
, new_len
;
556 old_len
= strlen(old_string
);
557 new_len
= strlen(new_string
);
570 if (!startswith(f
, old_string
)) {
576 nl
= l
- old_len
+ new_len
;
577 a
= realloc(r
, nl
+ 1);
585 t
= stpcpy(t
, new_string
);
597 char *strip_tab_ansi(char **ibuf
, size_t *_isz
) {
598 const char *i
, *begin
= NULL
;
603 } state
= STATE_OTHER
;
611 /* Strips ANSI color and replaces TABs by 8 spaces */
613 isz
= _isz
? *_isz
: strlen(*ibuf
);
615 f
= open_memstream(&obuf
, &osz
);
619 for (i
= *ibuf
; i
< *ibuf
+ isz
+ 1; i
++) {
624 if (i
>= *ibuf
+ isz
) /* EOT */
626 else if (*i
== '\x1B')
627 state
= STATE_ESCAPE
;
635 if (i
>= *ibuf
+ isz
) { /* EOT */
638 } else if (*i
== '[') {
639 state
= STATE_BRACKET
;
651 if (i
>= *ibuf
+ isz
|| /* EOT */
652 (!(*i
>= '0' && *i
<= '9') && *i
!= ';' && *i
!= 'm')) {
657 } else if (*i
== 'm')
680 char *strextend(char **x
, ...) {
687 l
= f
= *x
? strlen(*x
) : 0;
694 t
= va_arg(ap
, const char *);
699 if (n
> ((size_t) -1) - l
) {
708 r
= realloc(*x
, l
+1);
718 t
= va_arg(ap
, const char *);
732 char *strrep(const char *s
, unsigned n
) {
740 p
= r
= malloc(l
* n
+ 1);
744 for (i
= 0; i
< n
; i
++)
751 int split_pair(const char *s
, const char *sep
, char **l
, char **r
) {
766 a
= strndup(s
, x
- s
);
770 b
= strdup(x
+ strlen(sep
));
782 int free_and_strdup(char **p
, const char *s
) {
787 /* Replaces a string pointer with an strdup()ed new string,
788 * possibly freeing the old one. */
790 if (streq_ptr(*p
, s
))
806 #pragma GCC push_options
807 #pragma GCC optimize("O0")
809 void* memory_erase(void *p
, size_t l
) {
810 volatile uint8_t* x
= (volatile uint8_t*) p
;
812 /* This basically does what memset() does, but hopefully isn't
813 * optimized away by the compiler. One of those days, when
814 * glibc learns memset_s() we should replace this call by
815 * memset_s(), but until then this has to do. */
823 #pragma GCC pop_options
825 char* string_erase(char *x
) {
830 /* A delicious drop of snake-oil! To be called on memory where
831 * we stored passphrases or so, after we used them. */
833 return memory_erase(x
, strlen(x
));
836 char *string_free_erase(char *s
) {
837 return mfree(string_erase(s
));
840 bool string_is_safe(const char *p
) {
846 for (t
= p
; *t
; t
++) {
847 if (*t
> 0 && *t
< ' ') /* no control characters */
850 if (strchr(QUOTES
"\\\x7f", *t
))