diff options
author | Ben Pfaff <blp@ovn.org> | 2019-09-18 07:32:51 -0700 |
---|---|---|
committer | Ben Pfaff <blp@ovn.org> | 2019-09-18 08:42:30 -0700 |
commit | 3ae9a07ae7738bc14d632ee9edfc696fa8b198dd (patch) | |
tree | 9d50e4cd9304d4a757818d3e6de1b9af788e2ede /lib | |
parent | 334eec6a8ece99a0145165352ad38636f697c548 (diff) | |
download | openvswitch-3ae9a07ae7738bc14d632ee9edfc696fa8b198dd.tar.gz |
sset: New function sset_join().
This will acquire its first user in an upcoming commit.
This function follows the pattern set by svec_join().
Reviewed-by: Yifeng Sun <pkusunyifeng@gmail.com>
Signed-off-by: Ben Pfaff <blp@ovn.org>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/sset.c | 31 | ||||
-rw-r--r-- | lib/sset.h | 3 |
2 files changed, 34 insertions, 0 deletions
diff --git a/lib/sset.c b/lib/sset.c index 3deb1f9de..b2e3f43ec 100644 --- a/lib/sset.c +++ b/lib/sset.c @@ -18,6 +18,7 @@ #include "sset.h" +#include "openvswitch/dynamic-string.h" #include "hash.h" static uint32_t @@ -118,6 +119,36 @@ sset_from_delimited_string(struct sset *set, const char *s_, free(s); } +/* Returns a malloc()'d string that consists of the concatenation of all of the + * strings in 'sset' in lexicographic order, each separated from the next by + * 'delimiter' and followed by 'terminator'. For example: + * + * sset_join(("a", "b", "c"), ", ", ".") -> "a, b, c." + * sset_join(("xyzzy"), ", ", ".") -> "xyzzy." + * sset_join((""), ", ", ".") -> "." + * + * The caller is responsible for freeing the returned string (with free()). + */ +char * +sset_join(const struct sset *sset, + const char *delimiter, const char *terminator) +{ + struct ds s = DS_EMPTY_INITIALIZER; + + const char **names = sset_sort(sset); + for (size_t i = 0; i < sset_count(sset); i++) { + if (i) { + ds_put_cstr(&s, delimiter); + } + ds_put_cstr(&s, names[i]); + } + free(names); + + ds_put_cstr(&s, terminator); + + return ds_steal_cstr(&s); +} + /* Returns true if 'set' contains no strings, false if it contains at least one * string. */ bool diff --git a/lib/sset.h b/lib/sset.h index 768d0cf0a..f0bb8b534 100644 --- a/lib/sset.h +++ b/lib/sset.h @@ -43,8 +43,11 @@ void sset_clone(struct sset *, const struct sset *); void sset_swap(struct sset *, struct sset *); void sset_moved(struct sset *); +/* String parsing and formatting. */ void sset_from_delimited_string(struct sset *, const char *s, const char *delimiters); +char *sset_join(const struct sset *, + const char *delimiter, const char *terminator); /* Count. */ bool sset_is_empty(const struct sset *); |