1
0
mirror of https://github.com/systemd/systemd.git synced 2024-10-30 06:25:37 +03:00

strv: add strv_copy_n() helper for copying part of a n strv

This commit is contained in:
Lennart Poettering 2023-02-16 15:41:55 +01:00
parent f4ff3e7149
commit 4ea517a6e0
3 changed files with 52 additions and 3 deletions

View File

@ -77,20 +77,26 @@ char** strv_free_erase(char **l) {
return mfree(l);
}
char** strv_copy(char * const *l) {
char** strv_copy_n(char * const *l, size_t m) {
_cleanup_strv_free_ char **result = NULL;
char **k;
result = new(char*, strv_length(l) + 1);
result = new(char*, MIN(strv_length(l), m) + 1);
if (!result)
return NULL;
k = result;
STRV_FOREACH(i, l) {
if (m == 0)
break;
*k = strdup(*i);
if (!*k)
return NULL;
k++;
if (m != SIZE_MAX)
m--;
}
*k = NULL;

View File

@ -29,7 +29,10 @@ char** strv_free_erase(char **l);
DEFINE_TRIVIAL_CLEANUP_FUNC(char**, strv_free_erase);
#define _cleanup_strv_free_erase_ _cleanup_(strv_free_erasep)
char** strv_copy(char * const *l);
char** strv_copy_n(char * const *l, size_t n);
static inline char** strv_copy(char * const *l) {
return strv_copy_n(l, SIZE_MAX);
}
size_t strv_length(char * const *l) _pure_;
int strv_extend_strv(char ***a, char * const *b, bool filter_duplicates);

View File

@ -954,4 +954,44 @@ TEST(strv_extend_join) {
assert_se(streq(v[1], "ABC=QER"));
}
TEST(strv_copy_n) {
char **x = STRV_MAKE("a", "b", "c", "d", "e");
_cleanup_strv_free_ char **l = NULL;
l = strv_copy_n(x, 0);
assert_se(strv_equal(l, NULL));
strv_free(l);
l = strv_copy_n(x, 0);
assert_se(strv_equal(l, (char**) { NULL }));
strv_free(l);
l = strv_copy_n(x, 1);
assert_se(strv_equal(l, STRV_MAKE("a")));
strv_free(l);
l = strv_copy_n(x, 2);
assert_se(strv_equal(l, STRV_MAKE("a", "b")));
strv_free(l);
l = strv_copy_n(x, 3);
assert_se(strv_equal(l, STRV_MAKE("a", "b", "c")));
strv_free(l);
l = strv_copy_n(x, 4);
assert_se(strv_equal(l, STRV_MAKE("a", "b", "c", "d")));
strv_free(l);
l = strv_copy_n(x, 5);
assert_se(strv_equal(l, STRV_MAKE("a", "b", "c", "d", "e")));
strv_free(l);
l = strv_copy_n(x, 6);
assert_se(strv_equal(l, STRV_MAKE("a", "b", "c", "d", "e")));
strv_free(l);
l = strv_copy_n(x, SIZE_MAX);
assert_se(strv_equal(l, STRV_MAKE("a", "b", "c", "d", "e")));
}
DEFINE_TEST_MAIN(LOG_INFO);