mirror of
https://github.com/systemd/systemd.git
synced 2024-12-25 01:34:28 +03:00
set: introduce set_strjoin()
This commit is contained in:
parent
77613416e0
commit
4dbce71787
@ -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;
|
||||
}
|
||||
|
@ -150,3 +150,5 @@ DEFINE_TRIVIAL_CLEANUP_FUNC(Set*, set_free_free);
|
||||
|
||||
#define _cleanup_set_free_ _cleanup_(set_freep)
|
||||
#define _cleanup_set_free_free_ _cleanup_(set_free_freep)
|
||||
|
||||
int set_strjoin(Set *s, const char *separator, char **ret);
|
||||
|
@ -150,6 +150,56 @@ static void test_set_ensure_consume(void) {
|
||||
assert_se(set_size(m) == 2);
|
||||
}
|
||||
|
||||
static void test_set_strjoin(void) {
|
||||
_cleanup_set_free_ Set *m = NULL;
|
||||
_cleanup_free_ char *joined = NULL;
|
||||
|
||||
assert_se(set_strjoin(m, NULL, &joined) >= 0);
|
||||
assert_se(!joined);
|
||||
assert_se(set_strjoin(m, "", &joined) >= 0);
|
||||
assert_se(!joined);
|
||||
assert_se(set_strjoin(m, " ", &joined) >= 0);
|
||||
assert_se(!joined);
|
||||
assert_se(set_strjoin(m, "xxx", &joined) >= 0);
|
||||
assert_se(!joined);
|
||||
|
||||
assert_se(set_put_strdup(&m, "aaa") == 1);
|
||||
|
||||
assert_se(set_strjoin(m, NULL, &joined) >= 0);
|
||||
assert_se(streq(joined, "aaa"));
|
||||
|
||||
joined = mfree(joined);
|
||||
assert_se(set_strjoin(m, "", &joined) >= 0);
|
||||
assert_se(streq(joined, "aaa"));
|
||||
|
||||
joined = mfree(joined);
|
||||
assert_se(set_strjoin(m, " ", &joined) >= 0);
|
||||
assert_se(streq(joined, "aaa"));
|
||||
|
||||
joined = mfree(joined);
|
||||
assert_se(set_strjoin(m, "xxx", &joined) >= 0);
|
||||
assert_se(streq(joined, "aaa"));
|
||||
|
||||
assert_se(set_put_strdup(&m, "bbb") == 1);
|
||||
assert_se(set_put_strdup(&m, "aaa") == 0);
|
||||
|
||||
joined = mfree(joined);
|
||||
assert_se(set_strjoin(m, NULL, &joined) >= 0);
|
||||
assert_se(STR_IN_SET(joined, "aaabbb", "bbbaaa"));
|
||||
|
||||
joined = mfree(joined);
|
||||
assert_se(set_strjoin(m, "", &joined) >= 0);
|
||||
assert_se(STR_IN_SET(joined, "aaabbb", "bbbaaa"));
|
||||
|
||||
joined = mfree(joined);
|
||||
assert_se(set_strjoin(m, " ", &joined) >= 0);
|
||||
assert_se(STR_IN_SET(joined, "aaa bbb", "bbb aaa"));
|
||||
|
||||
joined = mfree(joined);
|
||||
assert_se(set_strjoin(m, "xxx", &joined) >= 0);
|
||||
assert_se(STR_IN_SET(joined, "aaaxxxbbb", "bbbxxxaaa"));
|
||||
}
|
||||
|
||||
int main(int argc, const char *argv[]) {
|
||||
test_set_steal_first();
|
||||
test_set_free_with_destructor();
|
||||
@ -160,6 +210,7 @@ int main(int argc, const char *argv[]) {
|
||||
test_set_ensure_allocated();
|
||||
test_set_ensure_put();
|
||||
test_set_ensure_consume();
|
||||
test_set_strjoin();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user