9#ifndef BITCOIN_UTIL_STRENCODINGS_H
10#define BITCOIN_UTIL_STRENCODINGS_H
25#include <system_error>
51 t = 1'000'000'000'000ULL,
64template <
typename Byte = std::
byte>
65std::optional<std::vector<Byte>>
TryParseHex(std::string_view str);
67template <
typename Byte = u
int8_t>
68std::vector<Byte>
ParseHex(std::string_view hex_str)
70 return TryParseHex<Byte>(hex_str).value_or(std::vector<Byte>{});
74bool IsHex(std::string_view str);
75std::optional<std::vector<unsigned char>>
DecodeBase64(std::string_view str);
76std::string
EncodeBase64(std::span<const unsigned char> input);
79std::optional<std::vector<unsigned char>>
DecodeBase32(std::string_view str);
86std::string
EncodeBase32(std::span<const unsigned char> input,
bool pad =
true);
93std::string
EncodeBase32(std::string_view str,
bool pad =
true);
104bool SplitHostPort(std::string_view in, uint16_t& portOut, std::string& hostOut);
119 static_assert(std::is_integral_v<T>);
123 if (!
s.empty() &&
s[0] ==
'+') {
124 if (
s.length() >= 2 &&
s[1] ==
'-') {
129 auto [
_, error_condition] = std::from_chars(
s.data(),
s.data() +
s.size(), result);
130 if (error_condition == std::errc::result_out_of_range) {
131 if (
s.length() >= 1 &&
s[0] ==
'-') {
133 return std::numeric_limits<T>::min();
136 return std::numeric_limits<T>::max();
138 }
else if (error_condition != std::errc{}) {
151 return c >=
'0' && c <=
'9';
165constexpr inline bool IsSpace(
char c)
noexcept {
166 return c ==
' ' || c ==
'\f' || c ==
'\n' || c ==
'\r' || c ==
'\t' || c ==
'\v';
179std::optional<T>
ToIntegral(std::string_view str,
size_t base = 10)
181 static_assert(std::is_integral_v<T>);
183 const auto [first_nonmatching, error_condition] = std::from_chars(str.data(), str.data() + str.size(), result, base);
184 if (first_nonmatching != str.data() + str.size() || error_condition != std::errc{}) {
194std::string
FormatParagraph(std::string_view in,
size_t width = 79,
size_t indent = 0);
204 if (b.size() == 0)
return a.size() == 0;
205 size_t accumulator = a.size() ^ b.size();
206 for (
size_t i = 0; i < a.size(); i++)
207 accumulator |=
size_t(a[i] ^ b[i%b.size()]);
208 return accumulator == 0;
215[[nodiscard]]
bool ParseFixedPoint(std::string_view,
int decimals, int64_t *amount_out);
221 [[maybe_unused]]
int operator()(
int x)
const {
return x; }
227template<
int frombits,
int tobits,
bool pad,
typename O,
typename It,
typename I = IntIdentity>
231 constexpr size_t maxv = (1 << tobits) - 1;
232 constexpr size_t max_acc = (1 << (frombits + tobits - 1)) - 1;
235 if (v < 0)
return false;
236 acc = ((acc << frombits) | v) & max_acc;
238 while (bits >= tobits) {
240 outfn((acc >> bits) & maxv);
245 if (bits) outfn((acc << (tobits - bits)) & maxv);
246 }
else if (bits >= frombits || ((acc << (tobits - bits)) & maxv)) {
264 return (c >=
'A' && c <=
'Z' ? (c -
'A') +
'a' : c);
276std::string
ToLower(std::string_view str);
290 return (c >=
'a' && c <=
'z' ? (c -
'a') +
'A' : c);
302std::string
ToUpper(std::string_view str);
332 if (c >=
'0' && c <=
'9')
return c -
'0';
333 if (c >=
'a' && c <=
'f')
return c -
'a' + 0xa;
335 throw "Only lowercase hex digits are allowed, for consistency";
341 std::array<std::byte, N / 2>
bytes{};
342 consteval Hex(
const char (&hex_str)[N])
346 if (hex_str[N - 1])
throw "null terminator required";
347 for (std::size_t i = 0; i <
bytes.size(); ++i) {
348 bytes[i] =
static_cast<std::byte
>(
357 bool operator()(std::string_view s1, std::string_view s2)
const
366 return std::hash<std::string>{}(
ToLower(
s));
399inline namespace hex_literals {
401template <util::detail::Hex str>
402constexpr auto operator""_hex() {
return str.bytes; }
404template <util::detail::Hex str>
405constexpr auto operator""_hex_u8() {
return std::bit_cast<std::array<uint8_t, str.bytes.size()>>(str.bytes); }
407template <util::detail::Hex str>
408constexpr auto operator""_hex_v() {
return std::vector<std::byte>{str.bytes.begin(), str.bytes.end()}; }
410template <util::detail::Hex str>
411inline auto operator""_hex_v_u8() {
return std::vector<uint8_t>{
UCharCast(str.bytes.data()),
UCharCast(str.bytes.data() + str.bytes.size())}; }
#define T(expected, seed, data)
consteval uint8_t ConstevalHexDigit(const char c)
consteval version of HexDigit() without the lookup table.
std::string_view TrimStringView(std::string_view str, std::string_view pattern=" \f\n\r\t\v")
constexpr auto MakeUCharSpan(const V &v) -> decltype(UCharSpanCast(std::span{v}))
Like the std::span constructor, but for (const) unsigned char member types only.
unsigned char * UCharCast(char *c)
std::string Capitalize(std::string str)
Capitalizes the first character of the given string.
constexpr char ToLower(char c)
Converts the given character to its lowercase equivalent.
constexpr bool IsDigit(char c)
Tests if the given character is a decimal digit.
constexpr char ToUpper(char c)
Converts the given character to its uppercase equivalent.
std::string EncodeBase32(std::span< const unsigned char > input, bool pad=true)
Base32 encode.
std::optional< std::vector< unsigned char > > DecodeBase32(std::string_view str)
T LocaleIndependentAtoi(std::string_view str)
ByteUnit
Used by ParseByteUnits() Lowercase base 1000 Uppercase base 1024.
bool TimingResistantEqual(const T &a, const T &b)
Timing-attack-resistant comparison.
std::vector< Byte > ParseHex(std::string_view hex_str)
Like TryParseHex, but returns an empty vector on invalid input.
std::optional< T > ToIntegral(std::string_view str, size_t base=10)
Convert string to integral type T.
bool ParseFixedPoint(std::string_view, int decimals, int64_t *amount_out)
Parse number as fixed point according to JSON number syntax.
constexpr bool IsSpace(char c) noexcept
Tests if the given character is a whitespace character.
bool IsHex(std::string_view str)
std::optional< std::vector< unsigned char > > DecodeBase64(std::string_view str)
bool SplitHostPort(std::string_view in, uint16_t &portOut, std::string &hostOut)
Splits socket address string into host string and port value.
bool ConvertBits(O outfn, It it, It end, I infn={})
Convert from one power-of-2 number base to another.
std::string EncodeBase64(std::span< const unsigned char > input)
std::string FormatParagraph(std::string_view in, size_t width=79, size_t indent=0)
Format a paragraph of text to a fixed width, adding spaces for indentation to any added line.
std::string SanitizeString(std::string_view str, int rule=SAFE_CHARS_DEFAULT)
Remove unsafe chars.
std::optional< std::vector< Byte > > TryParseHex(std::string_view str)
Parse the hex string into bytes (uint8_t or std::byte).
std::optional< uint64_t > ParseByteUnits(std::string_view str, ByteUnit default_multiplier)
Parse a string with suffix unit [k|K|m|M|g|G|t|T].
SafeChars
Utilities for converting data from/to strings.
@ SAFE_CHARS_DEFAULT
The full set of allowed chars.
@ SAFE_CHARS_UA_COMMENT
BIP-0014 subset.
@ SAFE_CHARS_URI
Chars allowed in URIs (RFC 3986)
@ SAFE_CHARS_FILENAME
Chars allowed in filenames.
size_t operator()(std::string_view s) const
bool operator()(std::string_view s1, std::string_view s2) const
consteval Hex(const char(&hex_str)[N])
std::array< std::byte, N/2 > bytes
consteval auto _(util::TranslatedLiteral str)