5#ifndef BITCOIN_UTIL_STRING_H
6#define BITCOIN_UTIL_STRING_H
21template <
unsigned num_params>
24 unsigned count_normal{0};
25 unsigned count_pos{0};
26 for (
auto it{str}; *it !=
'\0'; ++it) {
27 if (*it !=
'%' || *++it ==
'%')
continue;
30 unsigned maybe_num{0};
31 while (
'0' <= *it && *it <=
'9') {
33 maybe_num += *it -
'0';
40 if (maybe_num == 0)
throw "Positional format specifier must have position of at least 1";
41 count_pos = std::max(count_pos, maybe_num);
52 while (*it ==
'#' || *it ==
'0' || *it ==
'-' || *it ==
' ' || *it ==
'+') ++it;
54 auto parse_size = [&] {
59 while (
'0' <= *it && *it <=
'9') ++it;
72 if (*it ==
'\0')
throw "Format specifier incorrectly terminated by end of string";
77 if (count_normal && count_pos)
throw "Format specifiers must be all positional or all non-positional!";
78 unsigned count{count_normal | count_pos};
79 if (num_params !=
count)
throw "Format specifier count must match the argument count!";
91template <
unsigned num_params>
93 const char*
const fmt;
97void ReplaceAll(std::string& in_out,
const std::string& search,
const std::string& substitute);
106template <
typename T = Span<const
char>>
110 auto it = sp.
begin();
112 while (it != sp.
end()) {
113 if (separators.find(*it) != std::string::npos) {
114 ret.emplace_back(start, it);
119 ret.emplace_back(start, it);
130template <
typename T = Span<const
char>>
133 return Split<T>(sp, std::string_view{&sep, 1});
136[[nodiscard]]
inline std::vector<std::string>
SplitString(std::string_view str,
char sep)
138 return Split<std::string>(str, sep);
141[[nodiscard]]
inline std::vector<std::string>
SplitString(std::string_view str, std::string_view separators)
143 return Split<std::string>(str, separators);
146[[nodiscard]]
inline std::string_view
TrimStringView(std::string_view str, std::string_view pattern =
" \f\n\r\t\v")
148 std::string::size_type front = str.find_first_not_of(pattern);
149 if (front == std::string::npos) {
152 std::string::size_type end = str.find_last_not_of(pattern);
153 return str.substr(front, end - front + 1);
156[[nodiscard]]
inline std::string
TrimString(std::string_view str, std::string_view pattern =
" \f\n\r\t\v")
161[[nodiscard]]
inline std::string_view
RemoveSuffixView(std::string_view str, std::string_view suffix)
163 if (str.ends_with(suffix)) {
164 return str.substr(0, str.size() - suffix.size());
172 return str.substr(
prefix.size());
190template <
typename C,
typename S,
typename UnaryOp>
192auto Join(
const C& container,
const S& separator, UnaryOp unary_op)
194 decltype(unary_op(*container.begin()))
ret;
196 for (
const auto& item : container) {
197 if (!first)
ret += separator;
198 ret += unary_op(item);
204template <
typename C,
typename S>
205auto Join(
const C& container,
const S& separator)
207 return Join(container, separator, [](
const auto& i) {
return i; });
215 return Join(items,
"\n", [](
const std::string& item) {
return "- " + item; });
224 if (c == 0)
return false;
235 std::ostringstream oss;
236 oss.imbue(std::locale::classic());
244template <
typename T1,
size_t PREFIX_LEN>
246 const std::array<uint8_t, PREFIX_LEN>&
prefix)
248 return obj.size() >= PREFIX_LEN &&
249 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
static constexpr void CheckNumFormatSpecifiers(const char *str)
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.
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 RemoveSuffixView(std::string_view str, std::string_view suffix)
std::string_view RemovePrefixView(std::string_view str, std::string_view prefix)
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)
auto Join(const C &container, const S &separator, UnaryOp unary_op)
Join all container items.
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.
void ReplaceAll(std::string &in_out, const std::string &search, const std::string &substitute)
#define S(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p)