Bitcoin Core  28.1.0
P2P Digital Currency
string.h
Go to the documentation of this file.
1 // Copyright (c) 2019-2022 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 <span.h>
9 
10 #include <array>
11 #include <cstdint>
12 #include <cstring>
13 #include <locale>
14 #include <sstream>
15 #include <string> // IWYU pragma: export
16 #include <string_view> // IWYU pragma: export
17 #include <vector>
18 
19 namespace util {
20 void ReplaceAll(std::string& in_out, const std::string& search, const std::string& substitute);
21 
29 template <typename T = Span<const char>>
30 std::vector<T> Split(const Span<const char>& sp, std::string_view separators)
31 {
32  std::vector<T> ret;
33  auto it = sp.begin();
34  auto start = it;
35  while (it != sp.end()) {
36  if (separators.find(*it) != std::string::npos) {
37  ret.emplace_back(start, it);
38  start = it + 1;
39  }
40  ++it;
41  }
42  ret.emplace_back(start, it);
43  return ret;
44 }
45 
53 template <typename T = Span<const char>>
54 std::vector<T> Split(const Span<const char>& sp, char sep)
55 {
56  return Split<T>(sp, std::string_view{&sep, 1});
57 }
58 
59 [[nodiscard]] inline std::vector<std::string> SplitString(std::string_view str, char sep)
60 {
61  return Split<std::string>(str, sep);
62 }
63 
64 [[nodiscard]] inline std::vector<std::string> SplitString(std::string_view str, std::string_view separators)
65 {
66  return Split<std::string>(str, separators);
67 }
68 
69 [[nodiscard]] inline std::string_view TrimStringView(std::string_view str, std::string_view pattern = " \f\n\r\t\v")
70 {
71  std::string::size_type front = str.find_first_not_of(pattern);
72  if (front == std::string::npos) {
73  return {};
74  }
75  std::string::size_type end = str.find_last_not_of(pattern);
76  return str.substr(front, end - front + 1);
77 }
78 
79 [[nodiscard]] inline std::string TrimString(std::string_view str, std::string_view pattern = " \f\n\r\t\v")
80 {
81  return std::string(TrimStringView(str, pattern));
82 }
83 
84 [[nodiscard]] inline std::string_view RemoveSuffixView(std::string_view str, std::string_view suffix)
85 {
86  if (str.ends_with(suffix)) {
87  return str.substr(0, str.size() - suffix.size());
88  }
89  return str;
90 }
91 
92 [[nodiscard]] inline std::string_view RemovePrefixView(std::string_view str, std::string_view prefix)
93 {
94  if (str.substr(0, prefix.size()) == prefix) {
95  return str.substr(prefix.size());
96  }
97  return str;
98 }
99 
100 [[nodiscard]] inline std::string RemovePrefix(std::string_view str, std::string_view prefix)
101 {
102  return std::string(RemovePrefixView(str, prefix));
103 }
104 
113 template <typename C, typename S, typename UnaryOp>
114 // NOLINTNEXTLINE(misc-no-recursion)
115 auto Join(const C& container, const S& separator, UnaryOp unary_op)
116 {
117  decltype(unary_op(*container.begin())) ret;
118  bool first{true};
119  for (const auto& item : container) {
120  if (!first) ret += separator;
121  ret += unary_op(item);
122  first = false;
123  }
124  return ret;
125 }
126 
127 template <typename C, typename S>
128 auto Join(const C& container, const S& separator)
129 {
130  return Join(container, separator, [](const auto& i) { return i; });
131 }
132 
136 inline std::string MakeUnorderedList(const std::vector<std::string>& items)
137 {
138  return Join(items, "\n", [](const std::string& item) { return "- " + item; });
139 }
140 
144 [[nodiscard]] inline bool ContainsNoNUL(std::string_view str) noexcept
145 {
146  for (auto c : str) {
147  if (c == 0) return false;
148  }
149  return true;
150 }
151 
155 template <typename T>
156 std::string ToString(const T& t)
157 {
158  std::ostringstream oss;
159  oss.imbue(std::locale::classic());
160  oss << t;
161  return oss.str();
162 }
163 
167 template <typename T1, size_t PREFIX_LEN>
168 [[nodiscard]] inline bool HasPrefix(const T1& obj,
169  const std::array<uint8_t, PREFIX_LEN>& prefix)
170 {
171  return obj.size() >= PREFIX_LEN &&
172  std::equal(std::begin(prefix), std::end(prefix), std::begin(obj));
173 }
174 } // namespace util
175 
176 #endif // BITCOIN_UTIL_STRING_H
void ReplaceAll(std::string &in_out, const std::string &search, const std::string &substitute)
Definition: string.cpp:11
int ret
std::vector< std::string > SplitString(std::string_view str, char sep)
Definition: string.h:59
std::string TrimString(std::string_view str, std::string_view pattern=" \\\)
Definition: string.h:79
constexpr C * end() const noexcept
Definition: span.h:176
const char * prefix
Definition: rest.cpp:1007
std::string_view RemoveSuffixView(std::string_view str, std::string_view suffix)
Definition: string.h:84
std::string RemovePrefix(std::string_view str, std::string_view prefix)
Definition: string.h:100
#define S(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p)
bool ContainsNoNUL(std::string_view str) noexcept
Check if a string does not contain any embedded NUL (\0) characters.
Definition: string.h:144
std::string_view RemovePrefixView(std::string_view str, std::string_view prefix)
Definition: string.h:92
std::string_view TrimStringView(std::string_view str, std::string_view pattern=" \\\)
Definition: string.h:69
std::string MakeUnorderedList(const std::vector< std::string > &items)
Create an unordered multi-line list of items.
Definition: string.h:136
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:168
std::vector< T > Split(const Span< const char > &sp, std::string_view separators)
Split a string on any char found in separators, returning a vector.
Definition: string.h:30
constexpr C * begin() const noexcept
Definition: span.h:175
A Span is an object that can refer to a contiguous sequence of objects.
Definition: solver.h:20
auto Join(const C &container, const S &separator, UnaryOp unary_op)
Join all container items.
Definition: string.h:115
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition: string.h:156