forked from dashpay/dash
-
Notifications
You must be signed in to change notification settings - Fork 714
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
util: Add Join helper to join a list of strings
- Loading branch information
MarcoFalke
committed
Aug 20, 2019
1 parent
e00ecb3
commit fa8cd6f
Showing
4 changed files
with
58 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// Copyright (c) 2019 The Bitcoin Core developers | ||
// Distributed under the MIT software license, see the accompanying | ||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
||
#include <util/string.h> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright (c) 2019 The Bitcoin Core developers | ||
// Distributed under the MIT software license, see the accompanying | ||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
||
#ifndef BITCOIN_UTIL_STRING_H | ||
#define BITCOIN_UTIL_STRING_H | ||
|
||
#include <functional> | ||
#include <string> | ||
#include <vector> | ||
|
||
/** | ||
* Join a list of items | ||
* | ||
* @param list The list to join | ||
* @param separator The separator | ||
* @param unary_op Apply this operator to each item in the list | ||
*/ | ||
template <typename T, typename UnaryOp> | ||
std::string Join(const std::vector<T>& list, const std::string& separator, UnaryOp unary_op) | ||
{ | ||
std::string ret; | ||
for (size_t i = 0; i < list.size(); ++i) { | ||
if (i > 0) ret += separator; | ||
ret += unary_op(list.at(i)); | ||
} | ||
return ret; | ||
} | ||
|
||
inline std::string Join(const std::vector<std::string>& list, const std::string& separator) | ||
{ | ||
return Join(list, separator, [](const std::string& i) { return i; }); | ||
} | ||
|
||
#endif // BITCOIN_UTIL_STRENCODINGS_H |