9#ifndef BITCOIN_UTIL_STRENCODINGS_H
10#define BITCOIN_UTIL_STRENCODINGS_H
40std::vector<unsigned char>
ParseHex(
const char* psz);
41std::vector<unsigned char>
ParseHex(
const std::string& str);
45bool IsHex(
const std::string& str);
50std::vector<unsigned char>
DecodeBase64(
const char* p,
bool* pf_invalid =
nullptr);
51std::string
DecodeBase64(
const std::string& str,
bool* pf_invalid =
nullptr);
54std::vector<unsigned char>
DecodeBase32(
const char* p,
bool* pf_invalid =
nullptr);
55std::string
DecodeBase32(
const std::string& str,
bool* pf_invalid =
nullptr);
69std::string
EncodeBase32(
const std::string& str,
bool pad =
true);
71void SplitHostPort(std::string in, uint16_t& portOut, std::string& hostOut);
83 static_assert(std::is_integral<T>::value);
87 if (!s.empty() && s[0] ==
'+') {
88 if (s.length() >= 2 && s[1] ==
'-') {
93 auto [
_, error_condition] = std::from_chars(s.data(), s.data() + s.size(), result);
94 if (error_condition != std::errc{}) {
107 return c >=
'0' && c <=
'9';
121constexpr inline bool IsSpace(
char c)
noexcept {
122 return c ==
' ' || c ==
'\f' || c ==
'\n' || c ==
'\r' || c ==
'\t' || c ==
'\v';
136 static_assert(std::is_integral<T>::value);
138 const auto [first_nonmatching, error_condition] = std::from_chars(str.data(), str.data() + str.size(), result);
139 if (first_nonmatching != str.data() + str.size() || error_condition != std::errc{}) {
150[[nodiscard]]
bool ParseInt32(
const std::string& str, int32_t *out);
157[[nodiscard]]
bool ParseInt64(
const std::string& str, int64_t *out);
164[[nodiscard]]
bool ParseUInt8(
const std::string& str, uint8_t *out);
171[[nodiscard]]
bool ParseUInt16(
const std::string& str, uint16_t* out);
178[[nodiscard]]
bool ParseUInt32(
const std::string& str, uint32_t *out);
185[[nodiscard]]
bool ParseUInt64(
const std::string& str, uint64_t *out);
197std::string
FormatParagraph(
const std::string& in,
size_t width = 79,
size_t indent = 0);
207 if (b.size() == 0)
return a.size() == 0;
208 size_t accumulator = a.size() ^ b.size();
209 for (
size_t i = 0; i < a.size(); i++)
210 accumulator |= a[i] ^ b[i%b.size()];
211 return accumulator == 0;
219[[nodiscard]]
bool ParseFixedPoint(
const std::string &val,
int decimals, int64_t *amount_out);
222template<
int frombits,
int tobits,
bool pad,
typename O,
typename I>
226 constexpr size_t maxv = (1 << tobits) - 1;
227 constexpr size_t max_acc = (1 << (frombits + tobits - 1)) - 1;
229 acc = ((acc << frombits) | *it) & max_acc;
231 while (bits >= tobits) {
233 outfn((acc >> bits) & maxv);
238 if (bits) outfn((acc << (tobits - bits)) & maxv);
239 }
else if (bits >= frombits || ((acc << (tobits - bits)) & maxv)) {
257 return (c >=
'A' && c <=
'Z' ? (c -
'A') +
'a' : c);
269std::string
ToLower(
const std::string& str);
283 return (c >=
'a' && c <=
'z' ? (c -
'a') +
'A' : c);
295std::string
ToUpper(
const std::string& str);
A Span is an object that can refer to a contiguous sequence of objects.
#define T(expected, seed, data)
constexpr auto MakeUCharSpan(V &&v) -> decltype(UCharSpanCast(MakeSpan(std::forward< V >(v))))
Like MakeSpan, but for (const) unsigned char member types only.
std::string EncodeBase32(Span< const unsigned char > input, bool pad=true)
Base32 encode.
std::string FormatParagraph(const std::string &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 Capitalize(std::string str)
Capitalizes the first character of the given string.
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
constexpr char ToLower(char c)
Converts the given character to its lowercase equivalent.
bool ParseUInt16(const std::string &str, uint16_t *out)
Convert decimal string to unsigned 16-bit integer with strict parse error feedback.
constexpr bool IsDigit(char c)
Tests if the given character is a decimal digit.
bool ConvertBits(const O &outfn, I it, I end)
Convert from one power-of-2 number base to another.
std::vector< unsigned char > DecodeBase32(const char *p, bool *pf_invalid=nullptr)
std::string EncodeBase64(Span< const unsigned char > input)
std::string SanitizeString(const std::string &str, int rule=SAFE_CHARS_DEFAULT)
Remove unsafe chars.
constexpr char ToUpper(char c)
Converts the given character to its uppercase equivalent.
T LocaleIndependentAtoi(const std::string &str)
bool ParseUInt64(const std::string &str, uint64_t *out)
Convert decimal string to unsigned 64-bit integer with strict parse error feedback.
std::vector< unsigned char > ParseHex(const char *psz)
bool ParseUInt32(const std::string &str, uint32_t *out)
Convert decimal string to unsigned 32-bit integer with strict parse error feedback.
bool TimingResistantEqual(const T &a, const T &b)
Timing-attack-resistant comparison.
std::optional< T > ToIntegral(const std::string &str)
Convert string to integral type T.
bool ParseInt32(const std::string &str, int32_t *out)
Convert string to signed 32-bit integer with strict parse error feedback.
bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out)
Parse number as fixed point according to JSON number syntax.
bool IsHex(const std::string &str)
constexpr bool IsSpace(char c) noexcept
Tests if the given character is a whitespace character.
signed char HexDigit(char c)
bool IsHexNumber(const std::string &str)
Return true if the string is a hex number, optionally prefixed with "0x".
bool ParseInt64(const std::string &str, int64_t *out)
Convert string to signed 64-bit integer with strict parse error feedback.
void SplitHostPort(std::string in, uint16_t &portOut, std::string &hostOut)
std::vector< unsigned char > DecodeBase64(const char *p, bool *pf_invalid=nullptr)
bool ParseUInt8(const std::string &str, uint8_t *out)
Convert decimal string to unsigned 8-bit integer with strict parse error feedback.
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::string TrimString(const std::string &str, const std::string &pattern=" \f\n\r\t\v")
bilingual_str _(const char *psz)
Translation function.