1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include "nulstr-util.h"
4 #include "string-util.h"
7 char** strv_parse_nulstr_full(const char *s
, size_t l
, bool drop_trailing_nuls
) {
8 /* l is the length of the input data, which will be split at NULs into elements of the resulting
9 * strv. Hence, the number of items in the resulting strv will be equal to one plus the number of NUL
10 * bytes in the l bytes starting at s, unless s[l-1] is NUL, in which case the final empty string is
11 * not stored in the resulting strv, and length is equal to the number of NUL bytes.
13 * Note that contrary to a normal nulstr which cannot contain empty strings, because the input data
14 * is terminated by any two consequent NUL bytes, this parser accepts empty strings in s. */
16 _cleanup_strv_free_
char **v
= NULL
;
21 if (drop_trailing_nuls
)
22 while (l
> 0 && s
[l
-1] == '\0')
26 return new0(char*, 1);
28 for (const char *p
= s
; p
< s
+ l
; p
++)
39 for (const char *p
= s
; p
< s
+ l
; ) {
42 e
= memchr(p
, 0, s
+ l
- p
);
44 v
[i
] = memdup_suffix0(p
, e
? e
- p
: s
+ l
- p
);
61 char** strv_split_nulstr(const char *s
) {
62 _cleanup_strv_free_
char **l
= NULL
;
64 /* This parses a nulstr, without specification of size, and stops at an empty string. This cannot
65 * parse nulstrs with embedded empty strings hence, as an empty string is an end marker. Use
66 * strv_parse_nulstr() above to parse a nulstr with embedded empty strings (which however requires a
67 * size to be specified) */
70 if (strv_extend(&l
, i
) < 0)
73 return l
? TAKE_PTR(l
) : strv_new(NULL
);
76 int strv_make_nulstr(char * const *l
, char **ret
, size_t *ret_size
) {
77 /* Builds a nulstr and returns it together with the size. An extra NUL byte will be appended (⚠️ but
78 * not included in the size! ⚠️). This is done so that the nulstr can be used both in
79 * strv_parse_nulstr() and in NULSTR_FOREACH()/strv_split_nulstr() contexts, i.e. with and without a
80 * size parameter. In the former case we can include empty strings, in the latter case we cannot (as
81 * that is the end marker).
83 * When NULSTR_FOREACH()/strv_split_nulstr() is used it is often assumed that the nulstr ends in two
84 * NUL bytes (which it will, if not empty). To ensure that this assumption *always* holds, we'll
85 * return a buffer with two NUL bytes in that case, but return a size of zero. */
87 _cleanup_free_
char *m
= NULL
;
97 if (!GREEDY_REALLOC(m
, n
+ z
+ 2))
100 memcpy(m
+ n
, *i
, z
+ 1);
105 /* return a buffer with an extra NUL, so that the assumption that we always have two trailing NULs holds */
112 /* Make sure there is a second extra NUL at the end of resulting nulstr (not counted in return size) */
122 int set_make_nulstr(Set
*s
, char **ret
, size_t *ret_size
) {
123 /* Use _cleanup_free_ instead of _cleanup_strv_free_ because we need to clean the strv only, not
124 * the strings owned by the set. */
125 _cleanup_free_
char **strv
= NULL
;
129 strv
= set_get_strv(s
);
133 return strv_make_nulstr(strv
, ret
, ret_size
);
136 const char* nulstr_get(const char *nulstr
, const char *needle
) {
140 NULSTR_FOREACH(i
, nulstr
)
141 if (streq(i
, needle
))