5#ifndef BITCOIN_UTIL_STRING_H
6#define BITCOIN_UTIL_STRING_H
24void ReplaceAll(std::string &in_out, std::string_view search,
25 std::string_view substitute);
35template <
typename T = Span<const
char>>
40 while (it != sp.
end()) {
41 if (separators.find(*it) != std::string::npos) {
42 ret.emplace_back(start, it);
47 ret.emplace_back(start, it);
58template <
typename T = Span<const
char>>
60 return Split<T>(sp, std::string_view{&sep, 1});
63[[nodiscard]]
inline std::vector<std::string>
SplitString(std::string_view str,
65 return Split<std::string>(str, sep);
68[[nodiscard]]
inline std::vector<std::string>
69SplitString(std::string_view str, std::string_view separators) {
70 return Split<std::string>(str, separators);
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) {
79 std::string::size_type end = str.find_last_not_of(pattern);
80 return str.substr(front, end - front + 1);
83[[nodiscard]]
inline std::string
84TrimString(std::string_view str, std::string_view pattern =
" \f\n\r\t\v") {
88[[nodiscard]]
inline std::string_view
91 return str.substr(
prefix.size());
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) {
116 ret += unary_op(list.at(i));
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; });
128inline std::string
Join(
const std::vector<std::string> &list,
129 std::string_view separator) {
130 return Join<std::string>(list, separator);
137 return Join(items,
"\n",
138 [](
const std::string &item) {
return "- " + item; });
156template <
typename T> std::string
ToString(
const T &t) {
157 std::ostringstream oss;
158 oss.imbue(std::locale::classic());
166template <
typename T1,
size_t PREFIX_LEN>
167[[nodiscard]]
inline bool
169 return obj.size() >= PREFIX_LEN &&
170 std::equal(std::begin(
prefix), std::end(
prefix), std::begin(obj));
A Span is an object that can refer to a contiguous sequence of objects.
constexpr C * begin() const noexcept
constexpr C * end() const noexcept
std::vector< std::string > SplitString(std::string_view str, char sep)
bool HasPrefix(const T1 &obj, const std::array< uint8_t, PREFIX_LEN > &prefix)
Check whether a container begins with the given prefix.
auto Join(const std::vector< T > &list, const BaseType &separator, UnaryOp unary_op) -> decltype(unary_op(list.at(0)))
Join a list of items.
std::string_view TrimStringView(std::string_view str, std::string_view pattern=" \f\n\r\t\v")
std::string MakeUnorderedList(const std::vector< std::string > &items)
Create an unordered multi-line list of items.
std::string_view RemovePrefixView(std::string_view str, std::string_view prefix)
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...
std::string ToString(const T &t)
Locale-independent version of std::to_string.
std::string TrimString(std::string_view str, std::string_view pattern=" \f\n\r\t\v")
std::string RemovePrefix(std::string_view str, std::string_view prefix)
bool ContainsNoNUL(std::string_view str) noexcept
Check if a string does not contain any embedded NUL (\0) characters.
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.