5#ifndef BITCOIN_UTIL_STRING_H
6#define BITCOIN_UTIL_STRING_H
22template <
unsigned num_params>
27 for (
auto it{str}; *it !=
'\0'; ++it) {
28 if (*it !=
'%' || *++it ==
'%')
continue;
32 while (
'0' <= *it && *it <=
'9') {
41 if (
maybe_num == 0)
throw "Positional format specifier must have position of at least 1";
53 while (*it ==
'#' || *it ==
'0' || *it ==
'-' || *it ==
' ' || *it ==
'+') ++it;
60 while (
'0' <= *it && *it <=
'9') ++it;
73 if (*it ==
'\0')
throw "Format specifier incorrectly terminated by end of string";
78 if (
count_normal &&
count_pos)
throw "Format specifiers must be all positional or all non-positional!";
80 if (
num_params !=
count)
throw "Format specifier count must match the argument count!";
92template <
unsigned num_params>
94 const char*
const fmt;
115template <
typename T = std::span<const
char>>
119 auto it =
sp.begin();
121 while (it !=
sp.end()) {
122 if (
separators.find(*it) != std::string::npos) {
124 ret.emplace_back(start, it + 1);
126 ret.emplace_back(start, it);
132 ret.emplace_back(start, it);
143template <
typename T = std::span<const
char>>
161 std::string::size_type front = str.find_first_not_of(
pattern);
162 if (front == std::string::npos) {
165 std::string::size_type end = str.find_last_not_of(
pattern);
166 return str.substr(front, end - front + 1);
176 if (str.ends_with(
suffix)) {
177 return str.substr(0, str.size() -
suffix.size());
184 if (str.starts_with(
prefix)) {
185 return str.substr(
prefix.size());
203template <
typename C,
typename S,
typename UnaryOp>
217template <
typename C,
typename S>
237 if (
c == 0)
return false;
248 std::ostringstream
oss;
249 oss.imbue(std::locale::classic());
257template <
typename T1,
size_t PREFIX_LEN>
259 const std::array<uint8_t, PREFIX_LEN>&
prefix)
266 const std::span<const std::byte>::iterator
start;
267 const std::span<const std::byte>::iterator
end;
269 std::span<const std::byte>::iterator
it;
281 std::optional<std::string>
ReadLine();
#define T(expected, seed, data)
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.
void ReplaceAll(std::string &in_out, const std::string &search, const std::string &substitute)
std::vector< T > Split(const std::span< const char > &sp, std::string_view separators, bool include_sep=false)
Split a string on any char found in separators, returning a vector.
#define S(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p)
size_t Remaining() const
Returns remaining size of bytes in buffer.
std::span< conststd::byte >::iterator it
LineReader(std::span< const std::byte > buffer, size_t max_line_length)
const std::span< conststd::byte >::iterator end
std::string ReadLength(size_t len)
Returns string from current iterator position of specified length if possible and advances iterator o...
const size_t max_line_length
const std::span< conststd::byte >::iterator start
std::optional< std::string > ReadLine()
Returns a string from current iterator position up to (but not including) next and advances iterator...
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.