5#ifndef BITCOIN_UTIL_STRING_H
6#define BITCOIN_UTIL_STRING_H
12#include <initializer_list>
25template <
unsigned num_params>
30 for (
auto it{str}; *it !=
'\0'; ++it) {
31 if (*it !=
'%' || *++it ==
'%')
continue;
35 while (
'0' <= *it && *it <=
'9') {
44 if (
maybe_num == 0)
throw "Positional format specifier must have position of at least 1";
56 while (*it ==
'#' || *it ==
'0' || *it ==
'-' || *it ==
' ' || *it ==
'+') ++it;
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!";
83 if (
num_params !=
count)
throw "Format specifier count must match the argument count!";
95template <
unsigned num_params>
97 const char*
const fmt;
119template <
typename T = std::span<const
char>>
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>>
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);
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>
221template <
typename C,
typename S>
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)
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
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
#define T(expected, seed, data)
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")
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)
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.