mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-12 11:19:08 -05:00
util: Enhance Join()
This commit is contained in:
parent
fe05dd0611
commit
4c9b9a4882
1 changed files with 12 additions and 4 deletions
|
@ -30,10 +30,11 @@ NODISCARD inline std::string TrimString(const std::string& str, const std::strin
|
||||||
* @param separator The separator
|
* @param separator The separator
|
||||||
* @param unary_op Apply this operator to each item in the list
|
* @param unary_op Apply this operator to each item in the list
|
||||||
*/
|
*/
|
||||||
template <typename T, typename UnaryOp>
|
template <typename T, typename BaseType, typename UnaryOp>
|
||||||
std::string Join(const std::vector<T>& list, const std::string& separator, UnaryOp unary_op)
|
auto Join(const std::vector<T>& list, const BaseType& separator, UnaryOp unary_op)
|
||||||
|
-> decltype(unary_op(list.at(0)))
|
||||||
{
|
{
|
||||||
std::string ret;
|
decltype(unary_op(list.at(0))) ret;
|
||||||
for (size_t i = 0; i < list.size(); ++i) {
|
for (size_t i = 0; i < list.size(); ++i) {
|
||||||
if (i > 0) ret += separator;
|
if (i > 0) ret += separator;
|
||||||
ret += unary_op(list.at(i));
|
ret += unary_op(list.at(i));
|
||||||
|
@ -41,9 +42,16 @@ std::string Join(const std::vector<T>& list, const std::string& separator, Unary
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
T Join(const std::vector<T>& list, const T& separator)
|
||||||
|
{
|
||||||
|
return Join(list, separator, [](const T& i) { return i; });
|
||||||
|
}
|
||||||
|
|
||||||
|
// Explicit overload needed for c_str arguments, which would otherwise cause a substitution failure in the template above.
|
||||||
inline std::string Join(const std::vector<std::string>& list, const std::string& separator)
|
inline std::string Join(const std::vector<std::string>& list, const std::string& separator)
|
||||||
{
|
{
|
||||||
return Join(list, separator, [](const std::string& i) { return i; });
|
return Join<std::string>(list, separator);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Reference in a new issue