Bitcoin ABC  0.24.7
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 
35 template <typename T, typename BaseType, typename UnaryOp>
36 auto Join(const std::vector<T> &list, const BaseType &separator,
37  UnaryOp unary_op) -> decltype(unary_op(list.at(0))) {
38  decltype(unary_op(list.at(0))) ret;
39  for (size_t i = 0; i < list.size(); ++i) {
40  if (i > 0) {
41  ret += separator;
42  }
43  ret += unary_op(list.at(i));
44  }
45  return ret;
46 }
47 
48 template <typename T> T Join(const std::vector<T> &list, const T &separator) {
49  return Join(list, separator, [](const T &i) { return i; });
50 }
51 
52 // Explicit overload needed for c_str arguments, which would otherwise cause a
53 // substitution failure in the template above.
54 inline std::string Join(const std::vector<std::string> &list,
55  const std::string &separator) {
56  return Join<std::string>(list, separator);
57 }
58 
62 NODISCARD inline bool ValidAsCString(const std::string &str) noexcept {
63  return str.size() == strlen(str.c_str());
64 }
65 
69 template <typename T> std::string ToString(const T &t) {
70  std::ostringstream oss;
71  oss.imbue(std::locale::classic());
72  oss << t;
73  return oss.str();
74 }
75 
79 template <typename T1, size_t PREFIX_LEN>
80 NODISCARD inline bool HasPrefix(const T1 &obj,
81  const std::array<uint8_t, PREFIX_LEN> &prefix) {
82  return obj.size() >= PREFIX_LEN &&
83  std::equal(std::begin(prefix), std::end(prefix), std::begin(obj));
84 }
85 
86 #endif // BITCOIN_UTIL_STRING_H
ToString
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition: string.h:69
NODISCARD
#define NODISCARD
Definition: attributes.h:18
prefix
const char * prefix
Definition: rest.cpp:772
HasPrefix
NODISCARD 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:80
ValidAsCString
NODISCARD bool ValidAsCString(const std::string &str) noexcept
Check if a string does not contain any embedded NUL (\0) characters.
Definition: string.h:62
attributes.h
Join
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:36
TrimString
NODISCARD std::string TrimString(const std::string &str, const std::string &pattern=" \f\n\r\t\v")
Definition: string.h:19