Skip to content

Commit fa8cd6f

Browse files
author
MarcoFalke
committed
util: Add Join helper to join a list of strings
1 parent e00ecb3 commit fa8cd6f

File tree

4 files changed

+58
-2
lines changed

4 files changed

+58
-2
lines changed

src/Makefile.am

Lines changed: 2 additions & 0 deletions
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/test/util_tests.cpp

Lines changed: 16 additions & 2 deletions
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

Lines changed: 5 additions & 0 deletions
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

Lines changed: 35 additions & 0 deletions
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)