1
0
mirror of https://github.com/systemd/systemd.git synced 2025-03-23 10:50:16 +03:00

strv: introduce string_strv_hashmap_remove()

This commit is contained in:
Yu Watanabe 2025-01-28 08:50:14 +09:00
parent 3e1d7b6aae
commit c540875cd3
3 changed files with 37 additions and 0 deletions

View File

@ -1063,6 +1063,23 @@ int fputstrv(FILE *f, char * const *l, const char *separator, bool *space) {
return 0;
}
void string_strv_hashmap_remove(Hashmap *h, const char *key, const char *value) {
assert(key);
if (value) {
char **l = hashmap_get(h, key);
if (!l)
return;
strv_remove(l, value);
if (!strv_isempty(l))
return;
}
_unused_ _cleanup_free_ char *key_free = NULL;
strv_free(hashmap_remove2(h, key, (void**) &key_free));
}
static int string_strv_hashmap_put_internal(Hashmap *h, const char *key, const char *value) {
char **l;
int r;

View File

@ -258,6 +258,10 @@ int fputstrv(FILE *f, char * const *l, const char *separator, bool *space);
#define strv_free_and_replace(a, b) \
free_and_replace_full(a, b, strv_free)
void string_strv_hashmap_remove(Hashmap *h, const char *key, const char *value);
static inline void string_strv_ordered_hashmap_remove(OrderedHashmap *h, const char *key, const char *value) {
string_strv_hashmap_remove(PLAIN_HASHMAP(h), key, value);
}
int _string_strv_hashmap_put(Hashmap **h, const char *key, const char *value HASHMAP_DEBUG_PARAMS);
int _string_strv_ordered_hashmap_put(OrderedHashmap **h, const char *key, const char *value HASHMAP_DEBUG_PARAMS);
#define string_strv_hashmap_put(h, k, v) _string_strv_hashmap_put(h, k, v HASHMAP_DEBUG_SRC_ARGS)

View File

@ -973,6 +973,22 @@ TEST(string_strv_hashmap) {
s = hashmap_get(m, "xxx");
assert_se(strv_equal(s, STRV_MAKE("bar", "BAR")));
string_strv_hashmap_remove(m, "foo", "bar");
ASSERT_NOT_NULL(s = hashmap_get(m, "foo"));
ASSERT_TRUE(strv_equal(s, STRV_MAKE("BAR")));
string_strv_hashmap_remove(m, "foo", "BAR");
ASSERT_NULL(hashmap_get(m, "foo"));
string_strv_hashmap_remove(m, "xxx", "BAR");
ASSERT_NOT_NULL(s = hashmap_get(m, "xxx"));
ASSERT_TRUE(strv_equal(s, STRV_MAKE("bar")));
string_strv_hashmap_remove(m, "xxx", "bar");
ASSERT_NULL(hashmap_get(m, "xxx"));
ASSERT_TRUE(hashmap_isempty(m));
}
TEST(hashmap_dump_sorted) {