Bitcoin ABC 0.34.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 <span.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
19namespace util {
24void ReplaceAll(std::string &in_out, std::string_view search,
25 std::string_view substitute);
26
35template <typename T = Span<const char>>
36std::vector<T> Split(const Span<const char> &sp, std::string_view separators) {
37 std::vector<T> ret;
38 auto it = sp.begin();
39 auto start = it;
40 while (it != sp.end()) {
41 if (separators.find(*it) != std::string::npos) {
42 ret.emplace_back(start, it);
43 start = it + 1;
44 }
45 ++it;
46 }
47 ret.emplace_back(start, it);
48 return ret;
49}
50
58template <typename T = Span<const char>>
59std::vector<T> Split(const Span<const char> &sp, char sep) {
60 return Split<T>(sp, std::string_view{&sep, 1});
61}
62
63[[nodiscard]] inline std::vector<std::string> SplitString(std::string_view str,
64 char sep) {
65 return Split<std::string>(str, sep);
66}
67
68[[nodiscard]] inline std::vector<std::string>
69SplitString(std::string_view str, std::string_view separators) {
70 return Split<std::string>(str, separators);
71}
72
73[[nodiscard]] inline std::string_view
74TrimStringView(std::string_view str, std::string_view pattern = " \f\n\r\t\v") {
75 std::string::size_type front = str.find_first_not_of(pattern);
76 if (front == std::string::npos) {
77 return {};
78 }
79 std::string::size_type end = str.find_last_not_of(pattern);
80 return str.substr(front, end - front + 1);
81}
82
83[[nodiscard]] inline std::string
84TrimString(std::string_view str, std::string_view pattern = " \f\n\r\t\v") {
85 return std::string(TrimStringView(str, pattern));
86}
87
88[[nodiscard]] inline std::string_view
89RemovePrefixView(std::string_view str, std::string_view prefix) {
90 if (str.substr(0, prefix.size()) == prefix) {
91 return str.substr(prefix.size());
92 }
93 return str;
94}
95
96[[nodiscard]] inline std::string RemovePrefix(std::string_view str,
97 std::string_view prefix) {
98 return std::string(RemovePrefixView(str, prefix));
99}
100
108template <typename T, typename BaseType, typename UnaryOp>
109auto Join(const std::vector<T> &list, const BaseType &separator,
110 UnaryOp unary_op) -> decltype(unary_op(list.at(0))) {
111 decltype(unary_op(list.at(0))) ret;
112 for (size_t i = 0; i < list.size(); ++i) {
113 if (i > 0) {
114 ret += separator;
115 }
116 ret += unary_op(list.at(i));
117 }
118 return ret;
119}
120
121template <typename T, typename T2>
122T Join(const std::vector<T> &list, const T2 &separator) {
123 return Join(list, separator, [](const T &i) { return i; });
124}
125
126// Explicit overload needed for c_str arguments, which would otherwise cause a
127// substitution failure in the template above.
128inline std::string Join(const std::vector<std::string> &list,
129 std::string_view separator) {
130 return Join<std::string>(list, separator);
131}
132
136inline std::string MakeUnorderedList(const std::vector<std::string> &items) {
137 return Join(items, "\n",
138 [](const std::string &item) { return "- " + item; });
139}
140
144[[nodiscard]] inline bool ContainsNoNUL(std::string_view str) noexcept {
145 for (auto c : str) {
146 if (c == 0) {
147 return false;
148 }
149 }
150 return true;
151}
152
156template <typename T> std::string ToString(const T &t) {
157 std::ostringstream oss;
158 oss.imbue(std::locale::classic());
159 oss << t;
160 return oss.str();
161}
162
166template <typename T1, size_t PREFIX_LEN>
167[[nodiscard]] inline bool
168HasPrefix(const T1 &obj, const std::array<uint8_t, PREFIX_LEN> &prefix) {
169 return obj.size() >= PREFIX_LEN &&
170 std::equal(std::begin(prefix), std::end(prefix), std::begin(obj));
171}
172} // namespace util
173
174#endif // BITCOIN_UTIL_STRING_H
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:94
constexpr C * begin() const noexcept
Definition: span.h:200
constexpr C * end() const noexcept
Definition: span.h:201
std::vector< std::string > SplitString(std::string_view str, char sep)
Definition: string.h:63
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:168
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:109
std::string_view TrimStringView(std::string_view str, std::string_view pattern=" \f\n\r\t\v")
Definition: string.h:74
std::string MakeUnorderedList(const std::vector< std::string > &items)
Create an unordered multi-line list of items.
Definition: string.h:136
std::string_view RemovePrefixView(std::string_view str, std::string_view prefix)
Definition: string.h:89
void ReplaceAll(std::string &in_out, std::string_view search, std::string_view substitute)
Replace every non-overlapping occurrence of search with substitute, treating both literally; the repl...
Definition: string.cpp:11
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition: string.h:156
std::string TrimString(std::string_view str, std::string_view pattern=" \f\n\r\t\v")
Definition: string.h:84
std::string RemovePrefix(std::string_view str, std::string_view prefix)
Definition: string.h:96
bool ContainsNoNUL(std::string_view str) noexcept
Check if a string does not contain any embedded NUL (\0) characters.
Definition: string.h:144
std::vector< T > Split(const Span< const char > &sp, std::string_view separators)
Split a string on any char found in separators, returning a vector.
Definition: string.h:36
const char * prefix
Definition: rest.cpp:1078