From fa8cd6f9c13319baca467864661982a3dfb2320c Mon Sep 17 00:00:00 2001 From: MarcoFalke Date: Tue, 20 Aug 2019 14:51:43 -0400 Subject: [PATCH] util: Add Join helper to join a list of strings --- src/Makefile.am | 2 ++ src/test/util_tests.cpp | 18 ++++++++++++++++-- src/util/string.cpp | 5 +++++ src/util/string.h | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 src/util/string.cpp create mode 100644 src/util/string.h diff --git a/src/Makefile.am b/src/Makefile.am index 141d8e68ea2..477b1300bc6 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -211,6 +211,7 @@ BITCOIN_CORE_H = \ util/memory.h \ util/moneystr.h \ util/rbf.h \ + util/string.h \ util/threadnames.h \ util/time.h \ util/translation.h \ @@ -501,6 +502,7 @@ libbitcoin_util_a_SOURCES = \ util/rbf.cpp \ util/threadnames.cpp \ util/strencodings.cpp \ + util/string.cpp \ util/time.cpp \ util/url.cpp \ util/validation.cpp \ diff --git a/src/test/util_tests.cpp b/src/test/util_tests.cpp index 7119f56fc37..65cb956fbe5 100644 --- a/src/test/util_tests.cpp +++ b/src/test/util_tests.cpp @@ -6,11 +6,12 @@ #include #include +#include #include -#include #include +#include +#include #include -#include #include #include @@ -123,6 +124,19 @@ BOOST_AUTO_TEST_CASE(util_HexStr) ); } +BOOST_AUTO_TEST_CASE(util_Join) +{ + // Normal version + BOOST_CHECK_EQUAL(Join({}, ", "), ""); + BOOST_CHECK_EQUAL(Join({"foo"}, ", "), "foo"); + BOOST_CHECK_EQUAL(Join({"foo", "bar"}, ", "), "foo, bar"); + + // Version with unary operator + const auto op_upper = [](const std::string& s) { return ToUpper(s); }; + BOOST_CHECK_EQUAL(Join({}, ", ", op_upper), ""); + BOOST_CHECK_EQUAL(Join({"foo"}, ", ", op_upper), "FOO"); + BOOST_CHECK_EQUAL(Join({"foo", "bar"}, ", ", op_upper), "FOO, BAR"); +} BOOST_AUTO_TEST_CASE(util_FormatISO8601DateTime) { diff --git a/src/util/string.cpp b/src/util/string.cpp new file mode 100644 index 00000000000..8ea3a1afc6a --- /dev/null +++ b/src/util/string.cpp @@ -0,0 +1,5 @@ +// Copyright (c) 2019 The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#include diff --git a/src/util/string.h b/src/util/string.h new file mode 100644 index 00000000000..dec0c19b082 --- /dev/null +++ b/src/util/string.h @@ -0,0 +1,35 @@ +// Copyright (c) 2019 The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#ifndef BITCOIN_UTIL_STRING_H +#define BITCOIN_UTIL_STRING_H + +#include +#include +#include + +/** + * Join a list of items + * + * @param list The list to join + * @param separator The separator + * @param unary_op Apply this operator to each item in the list + */ +template +std::string Join(const std::vector& list, const std::string& separator, UnaryOp unary_op) +{ + std::string ret; + for (size_t i = 0; i < list.size(); ++i) { + if (i > 0) ret += separator; + ret += unary_op(list.at(i)); + } + return ret; +} + +inline std::string Join(const std::vector& list, const std::string& separator) +{ + return Join(list, separator, [](const std::string& i) { return i; }); +} + +#endif // BITCOIN_UTIL_STRENCODINGS_H