Skip to content

Commit 52b9797

Browse files
author
MarcoFalke
committed
Merge bitcoin#16670: util: Add Join helper to join a list of strings
faebf62 rpc: Use Join helper in rpc/util (MarcoFalke) fa8cd6f util: Add Join helper to join a list of strings (MarcoFalke) Pull request description: We have a lot of enumerations in the code and sometimes those enumerations need to be mentioned in the RPC or command line documentation. Previously, each caller would have a couple of lines inline to join the strings or the joined string is hardcoded in the documentation. A helper to join strings would make code such as bitcoin#16629 (comment) less verbose and easier to read. Also, warnings commonly accumulate in complex RPCs, since a warning doesn't lead to an early return. A helper to join those warnings would make code such as https://github.com/bitcoin/bitcoin/pull/16394/files#r309324997 less verbose and easier to read. ACKs for top commit: practicalswift: ACK faebf62 Tree-SHA512: 80f2db86a05c63b686f510585c1c631250271a8958fd71fafaac91559ffd2ec25d609bf7d53412ba27f87eff5893ac9dd9c2f296fc0c73581556e1d6a734a36f
2 parents 868a8ce + faebf62 commit 52b9797

File tree

5 files changed

+61
-8
lines changed

5 files changed

+61
-8
lines changed

src/Makefile.am

+2
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ BITCOIN_CORE_H = \
211211
util/memory.h \
212212
util/moneystr.h \
213213
util/rbf.h \
214+
util/string.h \
214215
util/threadnames.h \
215216
util/time.h \
216217
util/translation.h \
@@ -501,6 +502,7 @@ libbitcoin_util_a_SOURCES = \
501502
util/rbf.cpp \
502503
util/threadnames.cpp \
503504
util/strencodings.cpp \
505+
util/string.cpp \
504506
util/time.cpp \
505507
util/url.cpp \
506508
util/validation.cpp \

src/rpc/util.cpp

+3-6
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44

55
#include <key_io.h>
66
#include <outputtype.h>
7-
#include <script/signingprovider.h>
87
#include <rpc/util.h>
98
#include <script/descriptor.h>
9+
#include <script/signingprovider.h>
1010
#include <tinyformat.h>
1111
#include <util/strencodings.h>
12+
#include <util/string.h>
1213

1314
#include <tuple>
1415

@@ -645,11 +646,7 @@ std::string RPCArg::ToString(const bool oneline) const
645646
}
646647
case Type::OBJ:
647648
case Type::OBJ_USER_KEYS: {
648-
std::string res;
649-
for (size_t i = 0; i < m_inner.size();) {
650-
res += m_inner[i].ToStringObj(oneline);
651-
if (++i < m_inner.size()) res += ",";
652-
}
649+
const std::string res = Join(m_inner, ",", [&](const RPCArg& i) { return i.ToStringObj(oneline); });
653650
if (m_type == Type::OBJ) {
654651
return "{" + res + "}";
655652
} else {

src/test/util_tests.cpp

+16-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66

77
#include <clientversion.h>
88
#include <sync.h>
9+
#include <test/setup_common.h>
910
#include <test/util.h>
10-
#include <util/strencodings.h>
1111
#include <util/moneystr.h>
12+
#include <util/strencodings.h>
13+
#include <util/string.h>
1214
#include <util/time.h>
13-
#include <test/setup_common.h>
1415

1516
#include <stdint.h>
1617
#include <thread>
@@ -123,6 +124,19 @@ BOOST_AUTO_TEST_CASE(util_HexStr)
123124
);
124125
}
125126

127+
BOOST_AUTO_TEST_CASE(util_Join)
128+
{
129+
// Normal version
130+
BOOST_CHECK_EQUAL(Join({}, ", "), "");
131+
BOOST_CHECK_EQUAL(Join({"foo"}, ", "), "foo");
132+
BOOST_CHECK_EQUAL(Join({"foo", "bar"}, ", "), "foo, bar");
133+
134+
// Version with unary operator
135+
const auto op_upper = [](const std::string& s) { return ToUpper(s); };
136+
BOOST_CHECK_EQUAL(Join<std::string>({}, ", ", op_upper), "");
137+
BOOST_CHECK_EQUAL(Join<std::string>({"foo"}, ", ", op_upper), "FOO");
138+
BOOST_CHECK_EQUAL(Join<std::string>({"foo", "bar"}, ", ", op_upper), "FOO, BAR");
139+
}
126140

127141
BOOST_AUTO_TEST_CASE(util_FormatISO8601DateTime)
128142
{

src/util/string.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Copyright (c) 2019 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <util/string.h>

src/util/string.h

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (c) 2019 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#ifndef BITCOIN_UTIL_STRING_H
6+
#define BITCOIN_UTIL_STRING_H
7+
8+
#include <functional>
9+
#include <string>
10+
#include <vector>
11+
12+
/**
13+
* Join a list of items
14+
*
15+
* @param list The list to join
16+
* @param separator The separator
17+
* @param unary_op Apply this operator to each item in the list
18+
*/
19+
template <typename T, typename UnaryOp>
20+
std::string Join(const std::vector<T>& list, const std::string& separator, UnaryOp unary_op)
21+
{
22+
std::string ret;
23+
for (size_t i = 0; i < list.size(); ++i) {
24+
if (i > 0) ret += separator;
25+
ret += unary_op(list.at(i));
26+
}
27+
return ret;
28+
}
29+
30+
inline std::string Join(const std::vector<std::string>& list, const std::string& separator)
31+
{
32+
return Join(list, separator, [](const std::string& i) { return i; });
33+
}
34+
35+
#endif // BITCOIN_UTIL_STRENCODINGS_H

0 commit comments

Comments
 (0)