aboutsummaryrefslogtreecommitdiff
path: root/src/util/string.h
diff options
context:
space:
mode:
authorMarcoFalke <falke.marco@gmail.com>2019-08-20 14:51:43 -0400
committerMarcoFalke <falke.marco@gmail.com>2019-08-20 16:51:41 -0400
commitfa8cd6f9c13319baca467864661982a3dfb2320c (patch)
tree1d92c6fde4e276efa705802bfc57a944f13b5b3a /src/util/string.h
parente00ecb3d7aaee463643e486ca03c318e192b8058 (diff)
util: Add Join helper to join a list of strings
Diffstat (limited to 'src/util/string.h')
-rw-r--r--src/util/string.h35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/util/string.h b/src/util/string.h
new file mode 100644
index 0000000000..dec0c19b08
--- /dev/null
+++ b/src/util/string.h
@@ -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