strv-util: add strv_filter_prefix() helper

This commit is contained in:
Lennart Poettering
2025-02-17 15:00:00 +01:00
parent f77f363c95
commit 428146dc89
3 changed files with 42 additions and 0 deletions
+21
View File
@@ -1238,3 +1238,24 @@ int strv_rebreak_lines(char **l, size_t width, char ***ret) {
*ret = TAKE_PTR(broken);
return 0;
}
char** strv_filter_prefix(char *const*l, const char *prefix) {
_cleanup_strv_free_ char **f = NULL;
/* Allocates a copy of 'l', but only copies over entries starting with 'prefix' */
if (isempty(prefix))
return strv_copy(l);
size_t sz = 0;
STRV_FOREACH(i, l) {
if (!startswith(*i, prefix))
continue;
if (strv_extend_with_size(&f, &sz, *i) < 0)
return NULL;
}
return TAKE_PTR(f);
}
+2
View File
@@ -268,3 +268,5 @@ int _string_strv_ordered_hashmap_put(OrderedHashmap **h, const char *key, const
#define string_strv_ordered_hashmap_put(h, k, v) _string_strv_ordered_hashmap_put(h, k, v HASHMAP_DEBUG_SRC_ARGS)
int strv_rebreak_lines(char **l, size_t width, char ***ret);
char** strv_filter_prefix(char *const*l, const char *prefix);