strv-util: add strv_filter_prefix() helper
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user