Bitcoin ABC  0.26.3
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 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,
20  const std::string &substitute);
21 
22 [[nodiscard]] inline std::vector<std::string> SplitString(std::string_view str,
23  char sep) {
24  return spanparsing::Split<std::string>(str, sep);
25 }
26 
27 [[nodiscard]] inline std::string
28 TrimString(const std::string &str, const std::string &pattern = " \f\n\r\t\v") {
29  std::string::size_type front = str.find_first_not_of(pattern);
30  if (front == std::string::npos) {
31  return std::string();
32  }
33  std::string::size_type end = str.find_last_not_of(pattern);
34  return str.substr(front, end - front + 1);
35 }
36 
37 [[nodiscard]] inline std::string RemovePrefix(const std::string &str,
38  const std::string &prefix) {
39  if (str.substr(0, prefix.size()) == prefix) {
40  return str.substr(prefix.size());
41  }
42  return str;
43 }
44 
52 template <typename T, typename BaseType, typename UnaryOp>
53 auto Join(const std::vector<T> &list, const BaseType &separator,
54  UnaryOp unary_op) -> decltype(unary_op(list.at(0))) {
55  decltype(unary_op(list.at(0))) ret;
56  for (size_t i = 0; i < list.size(); ++i) {
57  if (i > 0) {
58  ret += separator;
59  }
60  ret += unary_op(list.at(i));
61  }
62  return ret;
63 }
64 
65 template <typename T> T Join(const std::vector<T> &list, const T &separator) {
66  return Join(list, separator, [](const T &i) { return i; });
67 }
68 
69 // Explicit overload needed for c_str arguments, which would otherwise cause a
70 // substitution failure in the template above.
71 inline std::string Join(const std::vector<std::string> &list,
72  const std::string &separator) {
73  return Join<std::string>(list, separator);
74 }
75 
79 [[nodiscard]] inline bool ValidAsCString(const std::string &str) noexcept {
80  return str.size() == strlen(str.c_str());
81 }
82 
86 template <typename T> std::string ToString(const T &t) {
87  std::ostringstream oss;
88  oss.imbue(std::locale::classic());
89  oss << t;
90  return oss.str();
91 }
92 
96 template <typename T1, size_t PREFIX_LEN>
97 [[nodiscard]] inline bool
98 HasPrefix(const T1 &obj, const std::array<uint8_t, PREFIX_LEN> &prefix) {
99  return obj.size() >= PREFIX_LEN &&
100  std::equal(std::begin(prefix), std::end(prefix), std::begin(obj));
101 }
102 
103 #endif // BITCOIN_UTIL_STRING_H
const char * prefix
Definition: rest.cpp:817
auto Join(const std::vector< T > &list, const BaseType &separator, UnaryOp unary_op) -> decltype(unary_op(list.at(0)))
Join a list of items.
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:79
std::string RemovePrefix(const std::string &str, const std::string &prefix)
Definition: string.h:37
std::vector< std::string > SplitString(std::string_view str, char sep)
Definition: string.h:22
void ReplaceAll(std::string &in_out, const std::string &search, const std::string &substitute)
Definition: string.cpp:10
std::string TrimString(const std::string &str, const std::string &pattern=" \f\n\r\t\v")
Definition: string.h:28
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition: string.h:86
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:98