Bitcoin Core 32.99.0
P2P Digital Currency
Loading...
Searching...
No Matches
strencodings.h
Go to the documentation of this file.
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-present The Bitcoin Core developers
3// Distributed under the MIT software license, see the accompanying
4// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
9#ifndef BITCOIN_UTIL_STRENCODINGS_H
10#define BITCOIN_UTIL_STRENCODINGS_H
11
12#include <span.h>
13#include <util/string.h>
14
15#include <array>
16#include <bit>
17#include <charconv>
18#include <cstddef>
19#include <cstdint>
20#include <functional>
21#include <limits>
22#include <optional>
23#include <span>
24#include <string>
25#include <string_view>
26#include <system_error>
27#include <type_traits>
28#include <vector>
29
38
44enum class ByteUnit : uint64_t {
45 NOOP = 1ULL,
46 k = 1000ULL,
47 K = 1024ULL,
48 m = 1'000'000ULL,
49 M = 1ULL << 20,
50 g = 1'000'000'000ULL,
51 G = 1ULL << 30,
52 t = 1'000'000'000'000ULL,
53 T = 1ULL << 40,
54};
55
63std::string SanitizeString(std::string_view str, int rule = SAFE_CHARS_DEFAULT);
65template <typename Byte = std::byte>
66std::optional<std::vector<Byte>> TryParseHex(std::string_view str);
68template <typename Byte = uint8_t>
69std::vector<Byte> ParseHex(std::string_view hex_str)
70{
71 return TryParseHex<Byte>(hex_str).value_or(std::vector<Byte>{});
72}
73/* Returns true if each character in str is a hex character, and has an even
74 * number of hex digits.*/
75bool IsHex(std::string_view str);
76std::optional<std::vector<unsigned char>> DecodeBase64(std::string_view str);
77std::string EncodeBase64(std::span<const unsigned char> input);
78inline std::string EncodeBase64(std::span<const std::byte> input) { return EncodeBase64(MakeUCharSpan(input)); }
79inline std::string EncodeBase64(std::string_view str) { return EncodeBase64(MakeUCharSpan(str)); }
80std::optional<std::vector<unsigned char>> DecodeBase32(std::string_view str);
81
87std::string EncodeBase32(std::span<const unsigned char> input, bool pad = true);
88
94std::string EncodeBase32(std::string_view str, bool pad = true);
95
105bool SplitHostPort(std::string_view in, uint16_t& portOut, std::string& hostOut);
106
107// LocaleIndependentAtoi is provided for backwards compatibility reasons.
108//
109// New code should use ToIntegral.
110//
111// The goal of LocaleIndependentAtoi is to replicate the defined behaviour of
112// std::atoi as it behaves under the "C" locale, and remove some undefined
113// behavior. If the parsed value is bigger than the integer type's maximum
114// value, or smaller than the integer type's minimum value, std::atoi has
115// undefined behavior, while this function returns the maximum or minimum
116// values, respectively.
117template <typename T>
118T LocaleIndependentAtoi(std::string_view str)
119{
120 static_assert(std::is_integral_v<T>);
121 T result;
122 // Emulate atoi(...) handling of white space and leading +/-.
123 std::string_view s = util::TrimStringView(str);
124 if (!s.empty() && s[0] == '+') {
125 if (s.length() >= 2 && s[1] == '-') {
126 return 0;
127 }
128 s = s.substr(1);
129 }
130 auto [_, error_condition] = std::from_chars(s.data(), s.data() + s.size(), result);
131 if (error_condition == std::errc::result_out_of_range) {
132 if (s.length() >= 1 && s[0] == '-') {
133 // Saturate underflow, per strtoll's behavior.
134 return std::numeric_limits<T>::min();
135 } else {
136 // Saturate overflow, per strtoll's behavior.
137 return std::numeric_limits<T>::max();
138 }
139 } else if (error_condition != std::errc{}) {
140 return 0;
141 }
142 return result;
143}
144
150constexpr bool IsDigit(char c)
151{
152 return c >= '0' && c <= '9';
153}
154
166constexpr inline bool IsSpace(char c) noexcept {
167 return c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v';
168}
169
179template <typename T>
180std::optional<T> ToIntegral(std::string_view str, size_t base = 10)
181{
182 static_assert(std::is_integral_v<T>);
183 T result;
184 const auto [first_nonmatching, error_condition] = std::from_chars(str.data(), str.data() + str.size(), result, base);
185 if (first_nonmatching != str.data() + str.size() || error_condition != std::errc{}) {
186 return std::nullopt;
187 }
188 return result;
189}
190
195std::string FormatParagraph(std::string_view in, size_t width = 79, size_t indent = 0);
196
202template <typename T>
203bool TimingResistantEqual(const T& a, const T& b)
204{
205 if (b.size() == 0) return a.size() == 0;
206 size_t accumulator = a.size() ^ b.size();
207 for (size_t i = 0; i < a.size(); i++)
208 accumulator |= size_t(a[i] ^ b[i%b.size()]);
209 return accumulator == 0;
210}
211
216[[nodiscard]] bool ParseFixedPoint(std::string_view, int decimals, int64_t *amount_out);
217
219template <int frombits, int tobits, bool pad, typename O, typename It, typename I = std::identity>
220bool ConvertBits(O outfn, It it, It end, I infn = {})
221{
222 size_t acc = 0;
223 size_t bits = 0;
224 constexpr size_t maxv = (1 << tobits) - 1;
225 constexpr size_t max_acc = (1 << (frombits + tobits - 1)) - 1;
226 while (it != end) {
227 int v = infn(*it);
228 if (v < 0) return false;
229 acc = ((acc << frombits) | v) & max_acc;
230 bits += frombits;
231 while (bits >= tobits) {
232 bits -= tobits;
233 outfn((acc >> bits) & maxv);
234 }
235 ++it;
236 }
237 if (pad) {
238 if (bits) outfn((acc << (tobits - bits)) & maxv);
239 } else if (bits >= frombits || ((acc << (tobits - bits)) & maxv)) {
240 return false;
241 }
242 return true;
243}
244
255constexpr char ToLower(char c)
256{
257 return (c >= 'A' && c <= 'Z' ? (c - 'A') + 'a' : c);
258}
259
269std::string ToLower(std::string_view str);
270
281constexpr char ToUpper(char c)
282{
283 return (c >= 'a' && c <= 'z' ? (c - 'a') + 'A' : c);
284}
285
295std::string ToUpper(std::string_view str);
296
306std::string Capitalize(std::string str);
307
319std::optional<uint64_t> ParseByteUnits(std::string_view str, ByteUnit default_multiplier);
320
327bool CaseInsensitiveEqual(std::string_view s1, std::string_view s2);
328
329namespace util {
331consteval uint8_t ConstevalHexDigit(const char c)
332{
333 if (c >= '0' && c <= '9') return c - '0';
334 if (c >= 'a' && c <= 'f') return c - 'a' + 0xa;
335
336 throw "Only lowercase hex digits are allowed, for consistency";
337}
338
339namespace detail {
340template <size_t N>
341struct Hex {
342 std::array<std::byte, N / 2> bytes{};
343 consteval Hex(const char (&hex_str)[N])
344 // 2 hex digits required per byte + implicit null terminator
345 requires(N % 2 == 1)
346 {
347 if (hex_str[N - 1]) throw "null terminator required";
348 for (std::size_t i = 0; i < bytes.size(); ++i) {
349 bytes[i] = static_cast<std::byte>(
350 (ConstevalHexDigit(hex_str[2 * i]) << 4) |
351 ConstevalHexDigit(hex_str[2 * i + 1]));
352 }
353 }
354};
355} // namespace detail
356
386inline namespace hex_literals {
387
388template <util::detail::Hex str>
389constexpr auto operator""_hex() { return str.bytes; }
390
391template <util::detail::Hex str>
392constexpr auto operator""_hex_u8() { return std::bit_cast<std::array<uint8_t, str.bytes.size()>>(str.bytes); }
393
394template <util::detail::Hex str>
395constexpr auto operator""_hex_v() { return std::vector<std::byte>{str.bytes.begin(), str.bytes.end()}; }
396
397template <util::detail::Hex str>
398inline auto operator""_hex_v_u8() { return std::vector<uint8_t>{UCharCast(str.bytes.data()), UCharCast(str.bytes.data() + str.bytes.size())}; }
399
400} // inline namespace hex_literals
401} // namespace util
402
403#endif // BITCOIN_UTIL_STRENCODINGS_H
consteval uint8_t ConstevalHexDigit(const char c)
consteval version of HexDigit() without the lookup table.
std::string_view TrimStringView(std::string_view str LIFETIMEBOUND, std::string_view pattern=" \f\n\r\t\v")
Definition string.h:163
constexpr auto MakeUCharSpan(const V &v) -> decltype(UCharSpanCast(std::span{v}))
Like the std::span constructor, but for (const) unsigned char member types only.
Definition span.h:111
unsigned char * UCharCast(char *c)
Definition span.h:95
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)
bool CaseInsensitiveEqual(std::string_view s1, std::string_view s2)
Locale-independent, ASCII-only comparator.
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.
consteval Hex(const char(&hex_str)[N])
std::array< std::byte, N/2 > bytes
consteval auto _(util::TranslatedLiteral str)
Definition translation.h:79
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition time.h:82