5#ifndef BITCOIN_UTIL_STRING_H
6#define BITCOIN_UTIL_STRING_H
12#include <initializer_list>
25template <
unsigned num_params>
28 unsigned count_normal{0};
29 unsigned count_pos{0};
30 for (
auto it{str}; *it !=
'\0'; ++it) {
31 if (*it !=
'%' || *++it ==
'%')
continue;
34 unsigned maybe_num{0};
35 while (
'0' <= *it && *it <=
'9') {
37 maybe_num += *it -
'0';
44 if (maybe_num == 0)
throw "Positional format specifier must have position of at least 1";
45 count_pos = std::max(count_pos, maybe_num);
56 while (*it ==
'#' || *it ==
'0' || *it ==
'-' || *it ==
' ' || *it ==
'+') ++it;
58 auto parse_size = [&] {
63 while (
'0' <= *it && *it <=
'9') ++it;
76 if (*it ==
'\0')
throw "Format specifier incorrectly terminated by end of string";
81 if (count_normal && count_pos)
throw "Format specifiers must be all positional or all non-positional!";
82 unsigned count{count_normal | count_pos};
83 if (num_params !=
count)
throw "Format specifier count must match the argument count!";
95template <
unsigned num_params>
97 const char*
const fmt;
102void ReplaceAll(std::string& in_out, std::string_view search, std::string_view substitute);
119template <
typename T = std::span<const
char>>
120std::vector<T>
Split(std::span<const char> sp
LIFETIMEBOUND, std::string_view separators,
bool include_sep =
false)
123 auto it = sp.begin();
125 while (it != sp.end()) {
126 if (separators.find(*it) != std::string::npos) {
128 ret.emplace_back(start, it + 1);
130 ret.emplace_back(start, it);
136 ret.emplace_back(start, it);
147template <
typename T = std::span<const
char>>
150 return Split<T>(sp, std::string_view{&sep, 1}, include_sep);
153[[nodiscard]]
inline std::vector<std::string>
SplitString(std::string_view str,
char sep)
155 return Split<std::string>(str, sep);
158[[nodiscard]]
inline std::vector<std::string>
SplitString(std::string_view str, std::string_view separators)
160 return Split<std::string>(str, separators);
165 std::string::size_type front = str.find_first_not_of(pattern);
166 if (front == std::string::npos) {
169 std::string::size_type end = str.find_last_not_of(pattern);
170 return str.substr(front, end - front + 1);
173[[nodiscard]]
inline std::string
TrimString(std::string_view str, std::string_view pattern =
" \f\n\r\t\v")
180 if (str.ends_with(suffix)) {
181 return str.substr(0, str.size() - suffix.size());
188 if (str.starts_with(
prefix)) {
189 return str.substr(
prefix.size());
207template <
typename C,
typename S,
typename UnaryOp>
209auto Join(
const C& container,
const S& separator, UnaryOp unary_op)
211 decltype(unary_op(*container.begin()))
ret;
213 for (
const auto& item : container) {
214 if (!first)
ret += separator;
215 ret += unary_op(item);
221template <
typename C,
typename S>
222auto Join(
const C& container,
const S& separator)
224 return Join(container, separator, [](
const auto& i) {
return i; });
232 return Join(items,
"\n", [](
const std::string& item) {
return "- " + item; });
238[[nodiscard]]
inline bool ContainsNUL(std::string_view str)
noexcept
241 if (c == 0)
return true;
252 std::ostringstream oss;
253 oss.imbue(std::locale::classic());
261template <
typename T1,
size_t PREFIX_LEN>
263 const std::array<uint8_t, PREFIX_LEN>&
prefix)
265 return obj.size() >= PREFIX_LEN &&
266 std::equal(std::begin(
prefix), std::end(
prefix), std::begin(obj));
273 std::string_view::iterator
m_it;
const size_t m_max_line_length
size_t Consumed() const
Returns number of bytes already read from buffer.
std::optional< std::string_view > ReadLine() LIFETIMEBOUND
Returns a string from current iterator position up to (but not including) next and advances iterator...
size_t Remaining() const
Returns remaining size of bytes in buffer.
const std::string_view m_str
LineReader(std::string_view str LIFETIMEBOUND, size_t max_line_length)
std::string_view ReadLength(size_t len) LIFETIMEBOUND
Returns string from current iterator position of specified length if possible and advances iterator o...
std::string_view::iterator m_it
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 MakeUnorderedList(const std::vector< std::string > &items)
Create an unordered multi-line list of items.
std::string_view RemoveSuffixView(std::string_view str LIFETIMEBOUND, std::string_view suffix)
std::vector< T > Split(std::span< const char > sp LIFETIMEBOUND, std::string_view separators, bool include_sep=false)
Split a string on any char found in separators, returning a vector.
std::string_view TrimStringView(std::string_view str LIFETIMEBOUND, std::string_view pattern=" \f\n\r\t\v")
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)
auto Join(const C &container, const S &separator, UnaryOp unary_op)
Join all container items.
bool ContainsNUL(std::string_view str) noexcept
Check if a string contains any embedded NUL (\0) characters.
std::string_view RemovePrefixView(std::string_view str LIFETIMEBOUND, std::string_view prefix)
#define S(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p)