Bitcoin Core  27.99.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 <util/spanparsing.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 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 // NOLINTNEXTLINE(misc-no-recursion)
69 auto Join(const C& container, const S& separator, UnaryOp unary_op)
70 {
71  decltype(unary_op(*container.begin())) ret;
72  bool first{true};
73  for (const auto& item : container) {
74  if (!first) ret += separator;
75  ret += unary_op(item);
76  first = false;
77  }
78  return ret;
79 }
80 
81 template <typename C, typename S>
82 auto Join(const C& container, const S& separator)
83 {
84  return Join(container, separator, [](const auto& i) { return i; });
85 }
86 
90 inline std::string MakeUnorderedList(const std::vector<std::string>& items)
91 {
92  return Join(items, "\n", [](const std::string& item) { return "- " + item; });
93 }
94 
98 [[nodiscard]] inline bool ContainsNoNUL(std::string_view str) noexcept
99 {
100  for (auto c : str) {
101  if (c == 0) return false;
102  }
103  return true;
104 }
105 
109 template <typename T>
110 std::string ToString(const T& t)
111 {
112  std::ostringstream oss;
113  oss.imbue(std::locale::classic());
114  oss << t;
115  return oss.str();
116 }
117 
121 template <typename T1, size_t PREFIX_LEN>
122 [[nodiscard]] inline bool HasPrefix(const T1& obj,
123  const std::array<uint8_t, PREFIX_LEN>& prefix)
124 {
125  return obj.size() >= PREFIX_LEN &&
126  std::equal(std::begin(prefix), std::end(prefix), std::begin(obj));
127 }
128 
129 #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:1007
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:90
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:98
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition: string.h:110
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:122
auto Join(const C &container, const S &separator, UnaryOp unary_op)
Join all container items.
Definition: string.h:69