From 5058bd7e1f0ae226d835253fcf333da3ab8d2806 Mon Sep 17 00:00:00 2001 From: Ludwig Nussel Date: Tue, 9 Jan 2024 17:31:01 +0100 Subject: [PATCH] strv: introduce strv_copy_unless_empty() --- src/basic/strv.c | 16 ++++++++++++++++ src/basic/strv.h | 2 ++ 2 files changed, 18 insertions(+) diff --git a/src/basic/strv.c b/src/basic/strv.c index 908e9e2513..fa17631aa0 100644 --- a/src/basic/strv.c +++ b/src/basic/strv.c @@ -123,6 +123,22 @@ char** strv_copy_n(char * const *l, size_t m) { return TAKE_PTR(result); } +int strv_copy_unless_empty(char * const *l, char ***ret) { + assert(ret); + + if (strv_isempty(l)) { + *ret = NULL; + return 0; + } + + char **copy = strv_copy(l); + if (!copy) + return -ENOMEM; + + *ret = TAKE_PTR(copy); + return 1; +} + size_t strv_length(char * const *l) { size_t n = 0; diff --git a/src/basic/strv.h b/src/basic/strv.h index f1a8bc4910..7361faed84 100644 --- a/src/basic/strv.h +++ b/src/basic/strv.h @@ -38,6 +38,8 @@ 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); } +int strv_copy_unless_empty(char * const *l, char ***ret); + size_t strv_length(char * const *l) _pure_; int strv_extend_strv(char ***a, char * const *b, bool filter_duplicates);