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 <attributes.h>
9 
10 #include <algorithm>
11 #include <array>
12 #include <cstring>
13 #include <locale>
14 #include <sstream>
15 #include <string>
16 #include <vector>
17 
18 [[nodiscard]] inline std::string
19 TrimString(const std::string &str, const std::string &pattern = " \f\n\r\t\v") {
20  std::string::size_type front = str.find_first_not_of(pattern);
21  if (front == std::string::npos) {
22  return std::string();
23  }
24  std::string::size_type end = str.find_last_not_of(pattern);
25  return str.substr(front, end - front + 1);
26 }
27 
28 [[nodiscard]] inline std::string RemovePrefix(const std::string &str,
29  const std::string &prefix) {
30  if (str.substr(0, prefix.size()) == prefix) {
31  return str.substr(prefix.size());
32  }
33  return str;
34 }
35 
43 template <typename T, typename BaseType, typename UnaryOp>
44 auto Join(const std::vector<T> &list, const BaseType &separator,
45  UnaryOp unary_op) -> decltype(unary_op(list.at(0))) {
46  decltype(unary_op(list.at(0))) ret;
47  for (size_t i = 0; i < list.size(); ++i) {
48  if (i > 0) {
49  ret += separator;
50  }
51  ret += unary_op(list.at(i));
52  }
53  return ret;
54 }
55 
56 template <typename T> T Join(const std::vector<T> &list, const T &separator) {
57  return Join(list, separator, [](const T &i) { return i; });
58 }
59 
60 // Explicit overload needed for c_str arguments, which would otherwise cause a
61 // substitution failure in the template above.
62 inline std::string Join(const std::vector<std::string> &list,
63  const std::string &separator) {
64  return Join<std::string>(list, separator);
65 }
66 
70 [[nodiscard]] inline bool ValidAsCString(const std::string &str) noexcept {
71  return str.size() == strlen(str.c_str());
72 }
73 
77 template <typename T> std::string ToString(const T &t) {
78  std::ostringstream oss;
79  oss.imbue(std::locale::classic());
80  oss << t;
81  return oss.str();
82 }
83 
87 template <typename T1, size_t PREFIX_LEN>
88 [[nodiscard]] inline bool
89 HasPrefix(const T1 &obj, const std::array<uint8_t, PREFIX_LEN> &prefix) {
90  return obj.size() >= PREFIX_LEN &&
91  std::equal(std::begin(prefix), std::end(prefix), std::begin(obj));
92 }
93 
94 #endif // BITCOIN_UTIL_STRING_H
const char * prefix
Definition: rest.cpp:823
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:44
bool ValidAsCString(const std::string &str) noexcept
Check if a string does not contain any embedded NUL (\0) characters.
Definition: string.h:70
std::string RemovePrefix(const std::string &str, const std::string &prefix)
Definition: string.h:28
std::string TrimString(const std::string &str, const std::string &pattern=" \f\n\r\t\v")
Definition: string.h:19
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition: string.h:77
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:89