Skip to content

Commit f6c2872

Browse files
MarcoFalkefurszy
authored andcommitted
util: Add Join helper to join a list of strings
1 parent 32c1e42 commit f6c2872

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

src/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ BITCOIN_CORE_H = \
286286
util/memory.h \
287287
util/system.h \
288288
util/macros.h \
289+
util/string.h \
289290
util/threadnames.h \
290291
utilstrencodings.h \
291292
utilmoneystr.h \
@@ -558,6 +559,7 @@ libbitcoin_util_a_SOURCES = \
558559
utilmoneystr.cpp \
559560
util/threadnames.cpp \
560561
utilstrencodings.cpp \
562+
util/string.cpp \
561563
utiltime.cpp \
562564
$(BITCOIN_CORE_H) \
563565
$(LIBSAPLING_H)

src/test/util_tests.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "primitives/transaction.h"
1010
#include "sync.h"
1111
#include "utilstrencodings.h"
12+
#include "util/string.h"
1213
#include "utilmoneystr.h"
1314
#include "test/test_pivx.h"
1415
#include "util/vector.h"
@@ -85,6 +86,19 @@ BOOST_AUTO_TEST_CASE(util_HexStr)
8586
"04 67 8a fd b0");
8687
}
8788

89+
BOOST_AUTO_TEST_CASE(util_Join)
90+
{
91+
// Normal version
92+
BOOST_CHECK_EQUAL(Join({}, ", "), "");
93+
BOOST_CHECK_EQUAL(Join({"foo"}, ", "), "foo");
94+
BOOST_CHECK_EQUAL(Join({"foo", "bar"}, ", "), "foo, bar");
95+
96+
// Version with unary operator
97+
const auto op_upper = [](const std::string& s) { return ToUpper(s); };
98+
BOOST_CHECK_EQUAL(Join<std::string>({}, ", ", op_upper), "");
99+
BOOST_CHECK_EQUAL(Join<std::string>({"foo"}, ", ", op_upper), "FOO");
100+
BOOST_CHECK_EQUAL(Join<std::string>({"foo", "bar"}, ", ", op_upper), "FOO, BAR");
101+
}
88102

89103
BOOST_AUTO_TEST_CASE(util_FormatISO8601DateTime)
90104
{

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)