1 /* SPDX-License-Identifier: LGPL-2.1+ */
9 #include "alloc-util.h"
11 #include "extract-word.h"
13 #include "memory-util.h"
14 #include "nulstr-util.h"
15 #include "sort-util.h"
16 #include "string-util.h"
19 char *strv_find(char * const *l
, const char *name
) {
31 char *strv_find_case(char * const *l
, const char *name
) {
37 if (strcaseeq(*i
, name
))
43 char *strv_find_prefix(char * const *l
, const char *name
) {
49 if (startswith(*i
, name
))
55 char *strv_find_startswith(char * const *l
, const char *name
) {
60 /* Like strv_find_prefix, but actually returns only the
61 * suffix, not the whole item */
64 e
= startswith(*i
, name
);
72 char **strv_free(char **l
) {
84 char **strv_free_erase(char **l
) {
93 char **strv_copy(char * const *l
) {
96 k
= r
= new(char*, strv_length(l
) + 1);
101 for (; *l
; k
++, l
++) {
113 size_t strv_length(char * const *l
) {
125 char **strv_new_ap(const char *x
, va_list ap
) {
127 _cleanup_strv_free_
char **a
= NULL
;
131 /* As a special trick we ignore all listed strings that equal
132 * STRV_IGNORE. This is supposed to be used with the
133 * STRV_IFNOTNULL() macro to include possibly NULL strings in
134 * the string list. */
137 n
= x
== STRV_IGNORE
? 0 : 1;
140 while ((s
= va_arg(aq
, const char*))) {
141 if (s
== STRV_IGNORE
)
155 if (x
!= STRV_IGNORE
) {
162 while ((s
= va_arg(ap
, const char*))) {
164 if (s
== STRV_IGNORE
)
180 char **strv_new_internal(const char *x
, ...) {
185 r
= strv_new_ap(x
, ap
);
191 int strv_extend_strv(char ***a
, char * const *b
, bool filter_duplicates
) {
192 char * const *s
, **t
;
193 size_t p
, q
, i
= 0, j
;
203 if (p
>= SIZE_MAX
- q
)
206 t
= reallocarray(*a
, GREEDY_ALLOC_ROUND_UP(p
+ q
+ 1), sizeof(char *));
215 if (filter_duplicates
&& strv_contains(t
, *s
))
231 for (j
= 0; j
< i
; j
++)
238 int strv_extend_strv_concat(char ***a
, char * const *b
, const char *suffix
) {
245 v
= strjoin(*s
, suffix
);
259 char **strv_split_full(const char *s
, const char *separator
, SplitFlags flags
) {
260 const char *word
, *state
;
268 separator
= WHITESPACE
;
270 s
+= strspn(s
, separator
);
272 return new0(char*, 1);
275 _FOREACH_WORD(word
, l
, s
, separator
, flags
, state
)
283 _FOREACH_WORD(word
, l
, s
, separator
, flags
, state
) {
284 r
[i
] = strndup(word
, l
);
297 char **strv_split_newlines(const char *s
) {
303 /* Special version of strv_split() that splits on newlines and
304 * suppresses an empty string at the end */
306 l
= strv_split(s
, NEWLINE
);
314 if (isempty(l
[n
- 1]))
315 l
[n
- 1] = mfree(l
[n
- 1]);
320 int strv_split_extract(char ***t
, const char *s
, const char *separators
, ExtractFlags flags
) {
321 _cleanup_strv_free_
char **l
= NULL
;
322 size_t n
= 0, allocated
= 0;
329 _cleanup_free_
char *word
= NULL
;
331 r
= extract_first_word(&s
, &word
, separators
, flags
);
337 if (!GREEDY_REALLOC(l
, allocated
, n
+ 2))
340 l
[n
++] = TAKE_PTR(word
);
356 char *strv_join_prefix(char * const *l
, const char *separator
, const char *prefix
) {
364 k
= strlen(separator
);
365 m
= strlen_ptr(prefix
);
381 e
= stpcpy(e
, separator
);
384 e
= stpcpy(e
, prefix
);
394 int strv_push(char ***l
, char *value
) {
403 /* Check for overflow */
407 c
= reallocarray(*l
, GREEDY_ALLOC_ROUND_UP(n
+ 2), sizeof(char*));
418 int strv_push_pair(char ***l
, char *a
, char *b
) {
427 /* Check for overflow */
431 /* increase and check for overflow */
432 c
= reallocarray(*l
, GREEDY_ALLOC_ROUND_UP(n
+ !!a
+ !!b
+ 1), sizeof(char*));
446 int strv_insert(char ***l
, size_t position
, char *value
) {
454 position
= MIN(position
, n
);
456 /* increase and check for overflow */
465 for (i
= 0; i
< position
; i
++)
468 for (i
= position
; i
< n
; i
++)
479 int strv_consume(char ***l
, char *value
) {
482 r
= strv_push(l
, value
);
489 int strv_consume_pair(char ***l
, char *a
, char *b
) {
492 r
= strv_push_pair(l
, a
, b
);
501 int strv_consume_prepend(char ***l
, char *value
) {
504 r
= strv_push_prepend(l
, value
);
511 int strv_extend(char ***l
, const char *value
) {
521 return strv_consume(l
, v
);
524 int strv_extend_front(char ***l
, const char *value
) {
530 /* Like strv_extend(), but prepends rather than appends the new entry */
537 /* Increase and overflow check. */
546 c
= reallocarray(*l
, m
, sizeof(char*));
552 memmove(c
+1, c
, n
* sizeof(char*));
560 char **strv_uniq(char **l
) {
563 /* Drops duplicate entries. The first identical string will be
564 * kept, the others dropped */
567 strv_remove(i
+1, *i
);
572 bool strv_is_uniq(char * const *l
) {
576 if (strv_find(i
+1, *i
))
582 char **strv_remove(char **l
, const char *s
) {
590 /* Drops every occurrence of s in the string list, edits
593 for (f
= t
= l
; *f
; f
++)
603 char **strv_parse_nulstr(const char *s
, size_t l
) {
604 /* l is the length of the input data, which will be split at NULs into
605 * elements of the resulting strv. Hence, the number of items in the resulting strv
606 * will be equal to one plus the number of NUL bytes in the l bytes starting at s,
607 * unless s[l-1] is NUL, in which case the final empty string is not stored in
608 * the resulting strv, and length is equal to the number of NUL bytes.
610 * Note that contrary to a normal nulstr which cannot contain empty strings, because
611 * the input data is terminated by any two consequent NUL bytes, this parser accepts
612 * empty strings in s.
622 return new0(char*, 1);
624 for (p
= s
; p
< s
+ l
; p
++)
631 v
= new0(char*, c
+1);
639 e
= memchr(p
, 0, s
+ l
- p
);
641 v
[i
] = strndup(p
, e
? e
- p
: s
+ l
- p
);
660 char **strv_split_nulstr(const char *s
) {
665 if (strv_extend(&r
, i
) < 0) {
671 return strv_new(NULL
);
676 int strv_make_nulstr(char * const *l
, char **ret
, size_t *ret_size
) {
677 /* A valid nulstr with two NULs at the end will be created, but
678 * q will be the length without the two trailing NULs. Thus the output
679 * string is a valid nulstr and can be iterated over using NULSTR_FOREACH,
680 * and can also be parsed by strv_parse_nulstr as long as the length
681 * is provided separately.
684 size_t n_allocated
= 0, n
= 0;
685 _cleanup_free_
char *m
= NULL
;
696 if (!GREEDY_REALLOC(m
, n_allocated
, n
+ z
+ 2))
699 memcpy(m
+ n
, *i
, z
+ 1);
709 /* make sure there is a second extra NUL at the end of resulting nulstr */
721 bool strv_overlap(char * const *a
, char * const *b
) {
725 if (strv_contains(b
, *i
))
731 static int str_compare(char * const *a
, char * const *b
) {
732 return strcmp(*a
, *b
);
735 char **strv_sort(char **l
) {
736 typesafe_qsort(l
, strv_length(l
), str_compare
);
740 int strv_compare(char * const *a
, char * const *b
) {
743 if (strv_isempty(a
)) {
753 for ( ; *a
|| *b
; ++a
, ++b
) {
754 r
= strcmp_ptr(*a
, *b
);
762 void strv_print(char * const *l
) {
769 int strv_extendf(char ***l
, const char *format
, ...) {
774 va_start(ap
, format
);
775 r
= vasprintf(&x
, format
, ap
);
781 return strv_consume(l
, x
);
784 char **strv_reverse(char **l
) {
791 for (i
= 0; i
< n
/ 2; i
++)
792 SWAP_TWO(l
[i
], l
[n
-1-i
]);
797 char **strv_shell_escape(char **l
, const char *bad
) {
800 /* Escapes every character in every string in l that is in bad,
801 * edits in-place, does not roll-back on error. */
806 v
= shell_escape(*s
, bad
);
817 bool strv_fnmatch_full(char* const* patterns
, const char *s
, int flags
, size_t *matched_pos
) {
818 for (size_t i
= 0; patterns
&& patterns
[i
]; i
++)
819 if (fnmatch(patterns
[i
], s
, flags
) == 0) {
828 char ***strv_free_free(char ***l
) {
840 char **strv_skip(char **l
, size_t n
) {
852 int strv_extend_n(char ***l
, const char *value
, size_t n
) {
863 /* Adds the value n times to l */
866 if (n
>= SIZE_MAX
- k
)
869 nl
= reallocarray(*l
, GREEDY_ALLOC_ROUND_UP(k
+ n
+ 1), sizeof(char *));
875 for (i
= k
; i
< k
+ n
; i
++) {
876 nl
[i
] = strdup(value
);
885 for (j
= k
; j
< i
; j
++)
892 int fputstrv(FILE *f
, char * const *l
, const char *separator
, bool *space
) {
897 /* Like fputs(), but for strv, and with a less stupid argument order */
903 r
= fputs_with_space(f
, *s
, separator
, space
);
911 static int string_strv_hashmap_put_internal(Hashmap
*h
, const char *key
, const char *value
) {
915 l
= hashmap_get(h
, key
);
917 /* A list for this key already exists, let's append to it if it is not listed yet */
918 if (strv_contains(l
, value
))
921 r
= strv_extend(&l
, value
);
925 assert_se(hashmap_update(h
, key
, l
) >= 0);
927 /* No list for this key exists yet, create one */
928 _cleanup_strv_free_
char **l2
= NULL
;
929 _cleanup_free_
char *t
= NULL
;
935 r
= strv_extend(&l2
, value
);
939 r
= hashmap_put(h
, t
, l2
);
949 int _string_strv_hashmap_put(Hashmap
**h
, const char *key
, const char *value HASHMAP_DEBUG_PARAMS
) {
952 r
= _hashmap_ensure_allocated(h
, &string_strv_hash_ops HASHMAP_DEBUG_PASS_ARGS
);
956 return string_strv_hashmap_put_internal(*h
, key
, value
);
959 int _string_strv_ordered_hashmap_put(OrderedHashmap
**h
, const char *key
, const char *value HASHMAP_DEBUG_PARAMS
) {
962 r
= _ordered_hashmap_ensure_allocated(h
, &string_strv_hash_ops HASHMAP_DEBUG_PASS_ARGS
);
966 return string_strv_hashmap_put_internal(PLAIN_HASHMAP(*h
), key
, value
);
969 DEFINE_HASH_OPS_FULL(string_strv_hash_ops
, char, string_hash_func
, string_compare_func
, free
, char*, strv_free
);