9 #ifndef BITCOIN_UTIL_STRENCODINGS_H 10 #define BITCOIN_UTIL_STRENCODINGS_H 37 std::vector<unsigned char>
ParseHex(
const char* psz);
38 std::vector<unsigned char>
ParseHex(
const std::string& str);
42 bool IsHex(
const std::string& str);
47 std::vector<unsigned char>
DecodeBase64(
const char* p,
bool* pf_invalid =
nullptr);
48 std::string
DecodeBase64(
const std::string& str,
bool* pf_invalid =
nullptr);
51 std::vector<unsigned char>
DecodeBase32(
const char* p,
bool* pf_invalid =
nullptr);
52 std::string
DecodeBase32(
const std::string& str,
bool* pf_invalid =
nullptr);
66 std::string
EncodeBase32(
const std::string& str,
bool pad =
true);
68 void SplitHostPort(std::string in, uint16_t& portOut, std::string& hostOut);
69 int64_t
atoi64(
const std::string& str);
70 int atoi(
const std::string& str);
79 return c >=
'0' && c <=
'9';
93 constexpr
inline bool IsSpace(
char c) noexcept {
94 return c ==
' ' || c ==
'\f' || c ==
'\n' || c ==
'\r' || c ==
'\t' || c ==
'\v';
102 [[nodiscard]]
bool ParseInt32(
const std::string& str, int32_t *out);
109 [[nodiscard]]
bool ParseInt64(
const std::string& str, int64_t *out);
116 [[nodiscard]]
bool ParseUInt8(
const std::string& str, uint8_t *out);
123 [[nodiscard]]
bool ParseUInt16(
const std::string& str, uint16_t* out);
130 [[nodiscard]]
bool ParseUInt32(
const std::string& str, uint32_t *out);
137 [[nodiscard]]
bool ParseUInt64(
const std::string& str, uint64_t *out);
144 [[nodiscard]]
bool ParseDouble(
const std::string& str,
double *out);
156 std::string
FormatParagraph(
const std::string& in,
size_t width = 79,
size_t indent = 0);
163 template <
typename T>
166 if (b.size() == 0)
return a.size() == 0;
167 size_t accumulator = a.size() ^ b.size();
168 for (
size_t i = 0; i < a.size(); i++)
169 accumulator |= a[i] ^ b[i%b.size()];
170 return accumulator == 0;
178 [[nodiscard]]
bool ParseFixedPoint(
const std::string &val,
int decimals, int64_t *amount_out);
181 template<
int frombits,
int tobits,
bool pad,
typename O,
typename I>
185 constexpr
size_t maxv = (1 << tobits) - 1;
186 constexpr
size_t max_acc = (1 << (frombits + tobits - 1)) - 1;
188 acc = ((acc << frombits) | *it) & max_acc;
190 while (bits >= tobits) {
192 outfn((acc >> bits) & maxv);
197 if (bits) outfn((acc << (tobits - bits)) & maxv);
198 }
else if (bits >= frombits || ((acc << (tobits - bits)) & maxv)) {
216 return (c >=
'A' && c <=
'Z' ? (c -
'A') +
'a' : c);
228 std::string
ToLower(
const std::string& str);
242 return (c >=
'a' && c <=
'z' ? (c -
'a') +
'A' : c);
254 std::string
ToUpper(
const std::string& str);
267 #endif // BITCOIN_UTIL_STRENCODINGS_H 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.
The full set of allowed chars.
constexpr char ToUpper(char c)
Converts the given character to its uppercase equivalent.
std::string EncodeBase64(Span< const unsigned char > input)
std::vector< unsigned char > ParseHex(const char *psz)
signed char HexDigit(char c)
constexpr bool IsDigit(char c)
Tests if the given character is a decimal digit.
bool ParseInt32(const std::string &str, int32_t *out)
Convert string to signed 32-bit integer with strict parse error feedback.
bool ParseInt64(const std::string &str, int64_t *out)
Convert string to signed 64-bit integer with strict parse error feedback.
bool IsHex(const std::string &str)
std::string EncodeBase32(Span< const unsigned char > input, bool pad=true)
Base32 encode.
std::vector< unsigned char > DecodeBase64(const char *p, bool *pf_invalid=nullptr)
constexpr auto MakeUCharSpan(V &&v) -> decltype(UCharSpanCast(MakeSpan(std::forward< V >(v))))
Like MakeSpan, but for (const) unsigned char member types only.
SafeChars
Utilities for converting data from/to strings.
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
std::string Capitalize(std::string str)
Capitalizes the first character of the given string.
std::string SanitizeString(const std::string &str, int rule=SAFE_CHARS_DEFAULT)
Remove unsafe chars.
void SplitHostPort(std::string in, uint16_t &portOut, std::string &hostOut)
bool ConvertBits(const O &outfn, I it, I end)
Convert from one power-of-2 number base to another.
Chars allowed in filenames.
bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out)
Parse number as fixed point according to JSON number syntax.
bool ParseUInt16(const std::string &str, uint16_t *out)
Convert decimal string to unsigned 16-bit integer with strict parse error feedback.
constexpr bool IsSpace(char c) noexcept
Tests if the given character is a whitespace character.
bool IsHexNumber(const std::string &str)
Return true if the string is a hex number, optionally prefixed with "0x".
int atoi(const std::string &str)
int64_t atoi64(const std::string &str)
bool ParseUInt8(const std::string &str, uint8_t *out)
Convert decimal string to unsigned 8-bit integer with strict parse error feedback.
bool ParseUInt64(const std::string &str, uint64_t *out)
Convert decimal string to unsigned 64-bit integer with strict parse error feedback.
constexpr char ToLower(char c)
Converts the given character to its lowercase equivalent.
A Span is an object that can refer to a contiguous sequence of objects.
Chars allowed in URIs (RFC 3986)
bool ParseDouble(const std::string &str, double *out)
Convert string to double with strict parse error feedback.
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::vector< unsigned char > DecodeBase32(const char *p, bool *pf_invalid=nullptr)