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