5#ifndef BITCOIN_UTIL_STRING_H
6#define BITCOIN_UTIL_STRING_H
19void ReplaceAll(std::string &in_out,
const std::string &search,
20 const std::string &substitute);
22[[nodiscard]]
inline std::vector<std::string>
SplitString(std::string_view str,
24 return spanparsing::Split<std::string>(str, sep);
27[[nodiscard]]
inline std::string_view
28TrimStringView(std::string_view str, std::string_view pattern =
" \f\n\r\t\v") {
29 std::string::size_type front = str.find_first_not_of(pattern);
30 if (front == std::string::npos) {
33 std::string::size_type end = str.find_last_not_of(pattern);
34 return str.substr(front, end - front + 1);
37[[nodiscard]]
inline std::string
38TrimString(std::string_view str, std::string_view pattern =
" \f\n\r\t\v") {
42[[nodiscard]]
inline std::string_view
45 return str.substr(
prefix.size());
62template <
typename T,
typename BaseType,
typename UnaryOp>
63auto Join(
const std::vector<T> &list,
const BaseType &separator,
64 UnaryOp unary_op) ->
decltype(unary_op(list.at(0))) {
65 decltype(unary_op(list.at(0))) ret;
66 for (
size_t i = 0; i < list.size(); ++i) {
70 ret += unary_op(list.at(i));
75template <
typename T,
typename T2>
76T
Join(
const std::vector<T> &list,
const T2 &separator) {
77 return Join(list, separator, [](
const T &i) {
return i; });
82inline std::string
Join(
const std::vector<std::string> &list,
83 std::string_view separator) {
84 return Join<std::string>(list, separator);
92 if (c == 0)
return false;
100template <
typename T> std::string
ToString(
const T &t) {
101 std::ostringstream oss;
102 oss.imbue(std::locale::classic());
110template <
typename T1,
size_t PREFIX_LEN>
111[[nodiscard]]
inline bool
113 return obj.size() >= PREFIX_LEN &&
114 std::equal(std::begin(
prefix), std::end(
prefix), std::begin(obj));
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 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)
std::vector< std::string > SplitString(std::string_view str, char sep)
void ReplaceAll(std::string &in_out, const std::string &search, const std::string &substitute)
std::string_view RemovePrefixView(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::string ToString(const T &t)
Locale-independent version of std::to_string.
std::string_view TrimStringView(std::string_view str, std::string_view pattern=" \f\n\r\t\v")
bool HasPrefix(const T1 &obj, const std::array< uint8_t, PREFIX_LEN > &prefix)
Check whether a container begins with the given prefix.