9 #ifndef BITCOIN_UTIL_STRENCODINGS_H 10 #define BITCOIN_UTIL_STRENCODINGS_H 21 #include <string_view> 22 #include <system_error> 23 #include <type_traits> 48 t = 1'000
'000'000
'000ULL, 59 std::string SanitizeString(std::string_view str, int rule = SAFE_CHARS_DEFAULT); 61 template <typename Byte = uint8_t> 62 std::vector<Byte> ParseHex(std::string_view str); 63 signed char HexDigit(char c); 64 /* Returns true if each character in str is a hex character, and has an even 65 * number of hex digits.*/ 66 bool IsHex(std::string_view str); 70 bool IsHexNumber(std::string_view str); 71 std::optional<std::vector<unsigned char>> DecodeBase64(std::string_view str); 72 std::string EncodeBase64(Span<const unsigned char> input); 73 inline std::string EncodeBase64(Span<const std::byte> input) { return EncodeBase64(MakeUCharSpan(input)); } 74 inline std::string EncodeBase64(std::string_view str) { return EncodeBase64(MakeUCharSpan(str)); } 75 std::optional<std::vector<unsigned char>> DecodeBase32(std::string_view str); 82 std::string EncodeBase32(Span<const unsigned char> input, bool pad = true); 89 std::string EncodeBase32(std::string_view str, bool pad = true); 91 void SplitHostPort(std::string_view in, uint16_t& portOut, std::string& hostOut); 93 // LocaleIndependentAtoi is provided for backwards compatibility reasons. 95 // New code should use ToIntegral or the ParseInt* functions 96 // which provide parse error feedback. 98 // The goal of LocaleIndependentAtoi is to replicate the defined behaviour of 99 // std::atoi as it behaves under the "C" locale, and remove some undefined 100 // behavior. If the parsed value is bigger than the integer type's maximum
104 template <
typename T>
107 static_assert(std::is_integral<T>::value);
111 if (!s.empty() && s[0] ==
'+') {
112 if (s.length() >= 2 && s[1] ==
'-') {
117 auto [
_, error_condition] = std::from_chars(s.data(), s.data() + s.size(), result);
118 if (error_condition == std::errc::result_out_of_range) {
119 if (s.length() >= 1 && s[0] ==
'-') {
121 return std::numeric_limits<T>::min();
124 return std::numeric_limits<T>::max();
126 }
else if (error_condition != std::errc{}) {
139 return c >=
'0' && c <=
'9';
153 constexpr
inline bool IsSpace(
char c) noexcept {
154 return c ==
' ' || c ==
'\f' || c ==
'\n' || c ==
'\r' || c ==
'\t' || c ==
'\v';
165 template <
typename T>
168 static_assert(std::is_integral<T>::value);
170 const auto [first_nonmatching, error_condition] = std::from_chars(str.data(), str.data() + str.size(), result);
171 if (first_nonmatching != str.data() + str.size() || error_condition != std::errc{}) {
182 [[nodiscard]]
bool ParseInt32(std::string_view str, int32_t *out);
189 [[nodiscard]]
bool ParseInt64(std::string_view str, int64_t *out);
196 [[nodiscard]]
bool ParseUInt8(std::string_view str, uint8_t *out);
203 [[nodiscard]]
bool ParseUInt16(std::string_view str, uint16_t* out);
210 [[nodiscard]]
bool ParseUInt32(std::string_view str, uint32_t *out);
217 [[nodiscard]]
bool ParseUInt64(std::string_view str, uint64_t *out);
230 std::string
FormatParagraph(std::string_view in,
size_t width = 79,
size_t indent = 0);
237 template <
typename T>
240 if (b.size() == 0)
return a.size() == 0;
241 size_t accumulator = a.size() ^ b.size();
242 for (
size_t i = 0; i < a.size(); i++)
243 accumulator |=
size_t(a[i] ^ b[i%b.size()]);
244 return accumulator == 0;
252 [[nodiscard]]
bool ParseFixedPoint(std::string_view,
int decimals, int64_t *amount_out);
258 [[maybe_unused]]
int operator()(
int x)
const {
return x; }
264 template<
int frombits,
int tobits,
bool pad,
typename O,
typename It,
typename I = IntIdentity>
268 constexpr
size_t maxv = (1 << tobits) - 1;
269 constexpr
size_t max_acc = (1 << (frombits + tobits - 1)) - 1;
272 if (v < 0)
return false;
273 acc = ((acc << frombits) | v) & max_acc;
275 while (bits >= tobits) {
277 outfn((acc >> bits) & maxv);
282 if (bits) outfn((acc << (tobits - bits)) & maxv);
283 }
else if (bits >= frombits || ((acc << (tobits - bits)) & maxv)) {
301 return (c >=
'A' && c <=
'Z' ? (c -
'A') +
'a' : c);
313 std::string
ToLower(std::string_view str);
327 return (c >=
'a' && c <=
'z' ? (c -
'a') +
'A' : c);
339 std::string
ToUpper(std::string_view str);
365 #endif // BITCOIN_UTIL_STRENCODINGS_H 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.
T LocaleIndependentAtoi(std::string_view str)
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].
bool ParseUInt64(std::string_view str, uint64_t *out)
Convert decimal string to unsigned 64-bit integer with strict parse error feedback.
bool ParseUInt16(std::string_view str, uint16_t *out)
Convert decimal string to unsigned 16-bit integer with strict parse error feedback.
bool ParseFixedPoint(std::string_view, int decimals, int64_t *amount_out)
Parse number as fixed point according to JSON number syntax.
constexpr bool IsDigit(char c)
Tests if the given character is a decimal digit.
std::string_view TrimStringView(std::string_view str, std::string_view pattern=" \\\)
bool ParseUInt32(std::string_view str, uint32_t *out)
Convert decimal string to unsigned 32-bit integer with strict parse error feedback.
bool ParseInt32(std::string_view str, int32_t *out)
Convert string to signed 32-bit integer with strict parse error feedback.
bilingual_str _(const char *psz)
Translation function.
SafeChars
Utilities for converting data from/to strings.
bool ParseUInt8(std::string_view str, uint8_t *out)
Convert decimal string to unsigned 8-bit integer with strict parse error feedback.
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.
ByteUnit
Used by ParseByteUnits() Lowercase base 1000 Uppercase base 1024.
Chars allowed in filenames.
constexpr bool IsSpace(char c) noexcept
Tests if the given character is a whitespace character.
std::optional< T > ToIntegral(std::string_view str)
Convert string to integral type T.
constexpr auto MakeUCharSpan(V &&v) -> decltype(UCharSpanCast(Span
Like the Span constructor, but for (const) unsigned char member types only.
bool ParseInt64(std::string_view str, int64_t *out)
Convert string to signed 64-bit integer with strict parse error feedback.
constexpr char ToLower(char c)
Converts the given character to its lowercase equivalent.
bool ConvertBits(O outfn, It it, It end, I infn={})
Convert from one power-of-2 number base to another.
A Span is an object that can refer to a contiguous sequence of objects.
Chars allowed in URIs (RFC 3986)
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...