mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-01-11 12:22:39 -03:00
Make Join() util work with any container type
Also, remove helper that is only used in tests.
This commit is contained in:
parent
faf8da3c8d
commit
fa1c716955
2 changed files with 19 additions and 16 deletions
|
@ -244,9 +244,9 @@ BOOST_AUTO_TEST_CASE(util_Join)
|
|||
|
||||
// Version with unary operator
|
||||
const auto op_upper = [](const std::string& s) { return ToUpper(s); };
|
||||
BOOST_CHECK_EQUAL(Join<std::string>({}, ", ", op_upper), "");
|
||||
BOOST_CHECK_EQUAL(Join<std::string>({"foo"}, ", ", op_upper), "FOO");
|
||||
BOOST_CHECK_EQUAL(Join<std::string>({"foo", "bar"}, ", ", op_upper), "FOO, BAR");
|
||||
BOOST_CHECK_EQUAL(Join(std::list<std::string>{}, ", ", op_upper), "");
|
||||
BOOST_CHECK_EQUAL(Join(std::list<std::string>{"foo"}, ", ", op_upper), "FOO");
|
||||
BOOST_CHECK_EQUAL(Join(std::list<std::string>{"foo", "bar"}, ", ", op_upper), "FOO, BAR");
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(util_ReplaceAll)
|
||||
|
|
|
@ -58,27 +58,30 @@ void ReplaceAll(std::string& in_out, const std::string& search, const std::strin
|
|||
}
|
||||
|
||||
/**
|
||||
* Join a list of items
|
||||
* Join all container items. Typically used to concatenate strings but accepts
|
||||
* containers with elements of any type.
|
||||
*
|
||||
* @param list The list to join
|
||||
* @param separator The separator
|
||||
* @param unary_op Apply this operator to each item in the list
|
||||
* @param container The items to join
|
||||
* @param separator The separator
|
||||
* @param unary_op Apply this operator to each item
|
||||
*/
|
||||
template <typename T, typename BaseType, typename UnaryOp>
|
||||
auto Join(const std::vector<T>& list, const BaseType& separator, UnaryOp unary_op)
|
||||
template <typename C, typename S, typename UnaryOp>
|
||||
auto Join(const C& container, const S& separator, UnaryOp unary_op)
|
||||
{
|
||||
decltype(unary_op(list.at(0))) ret;
|
||||
for (size_t i = 0; i < list.size(); ++i) {
|
||||
if (i > 0) ret += separator;
|
||||
ret += unary_op(list.at(i));
|
||||
decltype(unary_op(*container.begin())) ret;
|
||||
bool first{true};
|
||||
for (const auto& item : container) {
|
||||
if (!first) ret += separator;
|
||||
ret += unary_op(item);
|
||||
first = false;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
template <typename T, typename T2>
|
||||
T Join(const std::vector<T>& list, const T2& separator)
|
||||
template <typename C, typename S>
|
||||
auto Join(const C& container, const S& separator)
|
||||
{
|
||||
return Join(list, separator, [](const T& i) { return i; });
|
||||
return Join(container, separator, [](const auto& i) { return i; });
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue