PIVX Core  5.6.99
P2P Digital Currency
string.h
Go to the documentation of this file.
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 PIVX_UTIL_STRING_H
6 #define PIVX_UTIL_STRING_H
7 
8 #include "attributes.h"
9 
10 #include <algorithm>
11 #include <array>
12 #include <cstdint>
13 #include <cstring>
14 #include <functional>
15 #include <string>
16 #include <vector>
17 
25 template <typename T, typename UnaryOp>
26 std::string Join(const std::vector<T>& list, const std::string& separator, UnaryOp unary_op)
27 {
28  std::string ret;
29  for (size_t i = 0; i < list.size(); ++i) {
30  if (i > 0) ret += separator;
31  ret += unary_op(list.at(i));
32  }
33  return ret;
34 }
35 
36 inline std::string Join(const std::vector<std::string>& list, const std::string& separator)
37 {
38  return Join(list, separator, [](const std::string& i) { return i; });
39 }
40 
44 inline bool ValidAsCString(const std::string& str) noexcept
45 {
46  return str.size() == strlen(str.c_str());
47 }
48 
52 template <typename T1, size_t PREFIX_LEN>
53 NODISCARD inline bool HasPrefix(const T1& obj,
54  const std::array<uint8_t, PREFIX_LEN>& prefix)
55 {
56  return obj.size() >= PREFIX_LEN &&
57  std::equal(std::begin(prefix), std::end(prefix), std::begin(obj));
58 }
59 
60 #endif // PIVX_UTIL_STRING_H
#define NODISCARD
Definition: attributes.h:18
const char * prefix
Definition: rest.cpp:564
NODISCARD bool HasPrefix(const T1 &obj, const std::array< uint8_t, PREFIX_LEN > &prefix)
Check whether a container begins with the given prefix.
Definition: string.h:53
bool ValidAsCString(const std::string &str) noexcept
Check if a string does not contain any embedded NUL (\0) characters.
Definition: string.h:44
std::string Join(const std::vector< T > &list, const std::string &separator, UnaryOp unary_op)
Join a list of items.
Definition: string.h:26