diff options
author | Yu Watanabe <watanabe.yu+github@gmail.com> | 2020-12-06 20:10:48 +0900 |
---|---|---|
committer | Yu Watanabe <watanabe.yu+github@gmail.com> | 2020-12-08 12:28:54 +0900 |
commit | 4dbce717873000cff7b56f89266d1d2fe53f9284 (patch) | |
tree | 70054e296c38e7d8a433c2757c14388504a7a4ea /src/basic/hashmap.c | |
parent | 77613416e0c79050b46bd728608e2c9e05e216d9 (diff) | |
download | systemd-4dbce717873000cff7b56f89266d1d2fe53f9284.tar.gz |
set: introduce set_strjoin()
Diffstat (limited to 'src/basic/hashmap.c')
-rw-r--r-- | src/basic/hashmap.c | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/src/basic/hashmap.c b/src/basic/hashmap.c index dd1b18c878..e38e580530 100644 --- a/src/basic/hashmap.c +++ b/src/basic/hashmap.c @@ -1976,3 +1976,38 @@ IteratedCache* iterated_cache_free(IteratedCache *cache) { return mfree(cache); } + +int set_strjoin(Set *s, const char *separator, char **ret) { + size_t separator_len, allocated = 0, len = 0; + _cleanup_free_ char *str = NULL; + const char *value; + bool first = true; + + assert(ret); + + separator_len = strlen_ptr(separator); + + SET_FOREACH(value, s) { + size_t l = strlen_ptr(value); + + if (l == 0) + continue; + + if (!GREEDY_REALLOC(str, allocated, len + l + (first ? 0 : separator_len) + 1)) + return -ENOMEM; + + if (separator_len > 0 && !first) { + memcpy(str + len, separator, separator_len); + len += separator_len; + } + + memcpy(str + len, value, l); + len += l; + first = false; + } + if (str) + str[len] = '\0'; + + *ret = TAKE_PTR(str); + return 0; +} |