From patchwork Wed Sep 18 14:32:51 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ben Pfaff X-Patchwork-Id: 1164081 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=openvswitch.org (client-ip=140.211.169.12; helo=mail.linuxfoundation.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=ovn.org Received: from mail.linuxfoundation.org (mail.linuxfoundation.org [140.211.169.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 46YQTy16Grz9sNf for ; Thu, 19 Sep 2019 02:31:32 +1000 (AEST) Received: from mail.linux-foundation.org (localhost [127.0.0.1]) by mail.linuxfoundation.org (Postfix) with ESMTP id F0BF7C64; Wed, 18 Sep 2019 16:31:28 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@mail.linuxfoundation.org Received: from smtp1.linuxfoundation.org (smtp1.linux-foundation.org [172.17.192.35]) by mail.linuxfoundation.org (Postfix) with ESMTPS id 08CA9C3A for ; Wed, 18 Sep 2019 16:31:28 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from relay7-d.mail.gandi.net (relay7-d.mail.gandi.net [217.70.183.200]) by smtp1.linuxfoundation.org (Postfix) with ESMTPS id 16BC876D for ; Wed, 18 Sep 2019 16:31:26 +0000 (UTC) X-Originating-IP: 66.170.99.95 Received: from localhost.localdomain (unknown [66.170.99.95]) (Authenticated sender: blp@ovn.org) by relay7-d.mail.gandi.net (Postfix) with ESMTPSA id 4B8B420008; Wed, 18 Sep 2019 16:31:23 +0000 (UTC) From: Ben Pfaff To: dev@openvswitch.org Date: Wed, 18 Sep 2019 07:32:51 -0700 Message-Id: <20190918143252.3317-1-blp@ovn.org> X-Mailer: git-send-email 2.21.0 MIME-Version: 1.0 X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on smtp1.linux-foundation.org Cc: Ben Pfaff Subject: [ovs-dev] [PATCH 1/2] sset: New function sset_join(). X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: ovs-dev-bounces@openvswitch.org Errors-To: ovs-dev-bounces@openvswitch.org This will acquire its first user in an upcoming commit. This function follows the pattern set by svec_join(). Signed-off-by: Ben Pfaff Reviewed-by: Yifeng Sun --- lib/sset.c | 31 +++++++++++++++++++++++++++++++ lib/sset.h | 3 +++ 2 files changed, 34 insertions(+) diff --git a/lib/sset.c b/lib/sset.c index 3deb1f9de9be..b2e3f43ec91b 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 768d0cf0a1f3..f0bb8b534496 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 *);