Bitcoin Core  24.99.0
P2P Digital Currency
string.h
Go to the documentation of this file.
1 // Copyright (c) 2019-2021 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 BITCOIN_UTIL_STRING_H
6 #define BITCOIN_UTIL_STRING_H
7 
8 #include <util/spanparsing.h>
9 
10 #include <array>
11 #include <cstdint>
12 #include <cstring>
13 #include <locale>
14 #include <sstream>
15 #include <string>
16 #include <string_view>
17 #include <vector>
18 
19 void ReplaceAll(std::string& in_out, const std::string& search, const std::string& substitute);
20 
21 [[nodiscard]] inline std::vector<std::string> SplitString(std::string_view str, char sep)
22 {
23  return spanparsing::Split<std::string>(str, sep);
24 }
25 
26 [[nodiscard]] inline std::vector<std::string> SplitString(std::string_view str, std::string_view separators)
27 {
28  return spanparsing::Split<std::string>(str, separators);
29 }
30 
31 [[nodiscard]] inline std::string_view TrimStringView(std::string_view str, std::string_view pattern = " \f\n\r\t\v")
32 {
33  std::string::size_type front = str.find_first_not_of(pattern);
34  if (front == std::string::npos) {
35  return {};
36  }
37  std::string::size_type end = str.find_last_not_of(pattern);
38  return str.substr(front, end - front + 1);
39 }
40 
41 [[nodiscard]] inline std::string TrimString(std::string_view str, std::string_view pattern = " \f\n\r\t\v")
42 {
43  return std::string(TrimStringView(str, pattern));
44 }
45 
46 [[nodiscard]] inline std::string_view RemovePrefixView(std::string_view str, std::string_view prefix)
47 {
48  if (str.substr(0, prefix.size()) == prefix) {
49  return str.substr(prefix.size());
50  }
51  return str;
52 }
53 
54 [[nodiscard]] inline std::string RemovePrefix(std::string_view str, std::string_view prefix)
55 {
56  return std::string(RemovePrefixView(str, prefix));
57 }
58 
67 template <typename C, typename S, typename UnaryOp>
68 auto Join(const C& container, const S& separator, UnaryOp unary_op)
69 {
70  decltype(unary_op(*container.begin())) ret;
71  bool first{true};
72  for (const auto& item : container) {
73  if (!first) ret += separator;
74  ret += unary_op(item);
75  first = false;
76  }
77  return ret;
78 }
79 
80 template <typename C, typename S>
81 auto Join(const C& container, const S& separator)
82 {
83  return Join(container, separator, [](const auto& i) { return i; });
84 }
85 
89 inline std::string MakeUnorderedList(const std::vector<std::string>& items)
90 {
91  return Join(items, "\n", [](const std::string& item) { return "- " + item; });
92 }
93 
97 [[nodiscard]] inline bool ContainsNoNUL(std::string_view str) noexcept
98 {
99  for (auto c : str) {
100  if (c == 0) return false;
101  }
102  return true;
103 }
104 
108 template <typename T>
109 std::string ToString(const T& t)
110 {
111  std::ostringstream oss;
112  oss.imbue(std::locale::classic());
113  oss << t;
114  return oss.str();
115 }
116 
120 template <typename T1, size_t PREFIX_LEN>
121 [[nodiscard]] inline bool HasPrefix(const T1& obj,
122  const std::array<uint8_t, PREFIX_LEN>& prefix)
123 {
124  return obj.size() >= PREFIX_LEN &&
125  std::equal(std::begin(prefix), std::end(prefix), std::begin(obj));
126 }
127 
128 #endif // BITCOIN_UTIL_STRING_H
int ret
#define S(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p)
const char * prefix
Definition: rest.cpp:972
std::string TrimString(std::string_view str, std::string_view pattern=" \f\n\r\t\v")
Definition: string.h:41
std::string RemovePrefix(std::string_view str, std::string_view prefix)
Definition: string.h:54
std::string MakeUnorderedList(const std::vector< std::string > &items)
Create an unordered multi-line list of items.
Definition: string.h:89
std::vector< std::string > SplitString(std::string_view str, char sep)
Definition: string.h:21
void ReplaceAll(std::string &in_out, const std::string &search, const std::string &substitute)
Definition: string.cpp:10
std::string_view RemovePrefixView(std::string_view str, std::string_view prefix)
Definition: string.h:46
bool ContainsNoNUL(std::string_view str) noexcept
Check if a string does not contain any embedded NUL (\0) characters.
Definition: string.h:97
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition: string.h:109
std::string_view TrimStringView(std::string_view str, std::string_view pattern=" \f\n\r\t\v")
Definition: string.h:31
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:121
auto Join(const C &container, const S &separator, UnaryOp unary_op)
Join all container items.
Definition: string.h:68