9#ifndef BITCOIN_UTIL_STRENCODINGS_H
10#define BITCOIN_UTIL_STRENCODINGS_H
48template <
typename Byte = std::
byte>
49std::optional<std::vector<Byte>>
TryParseHex(std::string_view str);
52template <
typename Byte = u
int8_t>
53std::vector<Byte>
ParseHex(std::string_view hex_str) {
54 return TryParseHex<Byte>(hex_str).value_or(std::vector<Byte>{});
60bool IsHex(std::string_view str);
65std::optional<std::vector<uint8_t>>
DecodeBase64(std::string_view str);
73std::optional<std::vector<uint8_t>>
DecodeBase32(std::string_view str);
87std::string
EncodeBase32(std::string_view str,
bool pad =
true);
100 std::string &hostOut);
110 static_assert(std::is_integral<T>::value);
114 if (!s.empty() && s[0] ==
'+') {
115 if (s.length() >= 2 && s[1] ==
'-') {
120 auto [
_, error_condition] =
121 std::from_chars(s.data(), s.data() + s.size(), result);
122 if (error_condition != std::errc{}) {
134 return c >=
'0' && c <=
'9';
149constexpr inline bool IsSpace(
char c)
noexcept {
150 return c ==
' ' || c ==
'\f' || c ==
'\n' || c ==
'\r' || c ==
'\t' ||
162template <
typename T> std::optional<T>
ToIntegral(std::string_view str) {
163 static_assert(std::is_integral<T>::value);
165 const auto [first_nonmatching, error_condition] =
166 std::from_chars(str.data(), str.data() + str.size(), result);
167 if (first_nonmatching != str.data() + str.size() ||
168 error_condition != std::errc{}) {
179[[nodiscard]]
bool ParseInt32(std::string_view str, int32_t *out);
186[[nodiscard]]
bool ParseInt64(std::string_view str, int64_t *out);
195[[nodiscard]]
bool ParseUInt8(std::string_view str, uint8_t *out);
204[[nodiscard]]
bool ParseUInt16(std::string_view str, uint16_t *out);
212[[nodiscard]]
bool ParseUInt32(std::string_view str, uint32_t *out);
220[[nodiscard]]
bool ParseUInt64(std::string_view str, uint64_t *out);
235 return a.size() == 0;
237 size_t accumulator = a.size() ^ b.size();
238 for (
size_t i = 0; i < a.size(); i++) {
239 accumulator |= size_t(a[i] ^ b[i % b.size()]);
241 return accumulator == 0;
252 int64_t *amount_out);
260 [[maybe_unused]]
int operator()(
int x)
const {
return x; }
271template <
int frombits,
int tobits,
bool pad,
typename O,
typename It,
272 typename I = IntIdentity>
276 constexpr size_t maxv = (1 << tobits) - 1;
277 constexpr size_t max_acc = (1 << (frombits + tobits - 1)) - 1;
283 acc = ((acc << frombits) | v) & max_acc;
285 while (bits >= tobits) {
287 outfn((acc >> bits) & maxv);
294 outfn((acc << (tobits - bits)) & maxv);
296 }
else if (bits >= frombits || ((acc << (tobits - bits)) & maxv)) {
314 return (c >=
'A' && c <=
'Z' ? (c -
'A') +
'a' : c);
326std::string
ToLower(std::string_view str);
339 return (c >=
'a' && c <=
'z' ? (c -
'a') +
'A' : c);
351std::string
ToUpper(std::string_view str);
std::string TrimString(std::string_view str, std::string_view pattern=" \f\n\r\t\v")
constexpr auto MakeUCharSpan(V &&v) -> decltype(UCharSpanCast(Span{std::forward< V >(v)}))
Like the Span constructor, but for (const) uint8_t member types only.
std::string Capitalize(std::string str)
Capitalizes the first character of the given string.
bool IsHexNumber(std::string_view str)
Return true if the string is a hex number, optionally prefixed with "0x".
bool ParseInt32(std::string_view str, int32_t *out)
Convert string to signed 32-bit integer with strict parse error feedback.
std::string EncodeBase64(Span< const uint8_t > input)
std::string EncodeBase32(Span< const uint8_t > input, bool pad=true)
Base32 encode.
bool ParseUInt16(std::string_view str, uint16_t *out)
Convert decimal string to unsigned 16-bit integer with strict parse error feedback.
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.
T LocaleIndependentAtoi(const std::string &str)
bool ParseInt64(std::string_view str, int64_t *out)
Convert string to signed 64-bit integer with strict parse error feedback.
bool TimingResistantEqual(const T &a, const T &b)
Timing-attack-resistant comparison.
bool ParseUInt8(std::string_view str, uint8_t *out)
Convert decimal string to unsigned 8-bit integer with strict parse error feedback.
std::vector< Byte > ParseHex(std::string_view hex_str)
Like TryParseHex, but returns an empty vector on invalid input.
bool ParseFixedPoint(std::string_view, int decimals, int64_t *amount_out)
Parse number as fixed point according to JSON number syntax.
bool ParseUInt64(std::string_view str, uint64_t *out)
Convert decimal string to unsigned 64-bit integer with strict parse error feedback.
constexpr bool IsSpace(char c) noexcept
Tests if the given character is a whitespace character.
bool IsHex(std::string_view str)
Returns true if each character in str is a hex character, and has an even number of hex digits.
std::optional< T > ToIntegral(std::string_view str)
Convert string to integral type T.
std::optional< std::vector< uint8_t > > 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 ParseUInt32(std::string_view str, uint32_t *out)
Convert decimal string to unsigned 32-bit integer with strict parse error feedback.
bool ConvertBits(O outfn, It it, It end, I infn={})
Convert from one power-of-2 number base to another.
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).
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.
std::optional< std::vector< uint8_t > > DecodeBase32(std::string_view str)
bilingual_str _(const char *psz)
Translation function.