17 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
33 if (
SAFE_CHARS[rule].find(c) != std::string::npos) {
41 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
42 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
43 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
44 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
45 -1, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, -1, -1, -1, -1, -1, -1, -1, -1, -1,
46 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
47 -1, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, -1, -1, -1, -1, -1, -1, -1, -1, -1,
48 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
49 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
50 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
51 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
52 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
53 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
54 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
55 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
56 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
63bool IsHex(std::string_view str) {
69 return (str.size() > 0) && (str.size() % 2 == 0);
73 if (str.substr(0, 2) ==
"0x") {
82 return str.size() > 0;
85template <
typename Byte>
86std::optional<std::vector<Byte>>
TryParseHex(std::string_view str) {
87 std::vector<Byte> vch;
88 auto it = str.begin();
89 while (it != str.end()) {
95 if (it == str.end()) {
99 if (c1 < 0 || c2 < 0) {
102 vch.push_back(Byte(c1 << 4) | Byte(c2));
106template std::vector<std::byte>
ParseHex(std::string_view);
107template std::vector<uint8_t>
ParseHex(std::string_view);
110 std::string &hostOut) {
111 size_t colon = in.find_last_of(
':');
114 bool fHaveColon = colon != in.npos;
117 bool fBracketed = fHaveColon && (in[0] ==
'[' && in[colon - 1] ==
']');
119 fHaveColon && (in.find_last_of(
':', colon - 1) != in.npos);
120 if (fHaveColon && (colon == 0 || fBracketed || !fMultiColon)) {
123 in = in.substr(0, colon);
127 if (in.size() > 0 && in[0] ==
'[' && in[in.size() - 1] ==
']') {
128 hostOut = in.substr(1, in.size() - 2);
135 static const char *pbase64 =
136 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
139 str.reserve(((input.
size() + 2) / 3) * 4);
140 ConvertBits<8, 6, true>([&](
int v) { str += pbase64[v]; }, input.
begin(),
142 while (str.size() % 4) {
148std::optional<std::vector<uint8_t>>
DecodeBase64(std::string_view str) {
149 static const int8_t decode64_table[256] = {
150 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
151 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
152 -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57,
153 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6,
154 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
155 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
156 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1,
157 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
158 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
159 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
160 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
161 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
162 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
163 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
166 if (str.size() % 4 != 0) {
170 if (str.size() >= 1 && str.back() ==
'=') {
171 str.remove_suffix(1);
173 if (str.size() >= 1 && str.back() ==
'=') {
174 str.remove_suffix(1);
177 std::vector<uint8_t> ret;
178 ret.reserve((str.size() * 3) / 4);
179 bool valid = ConvertBits<6, 8, false>(
180 [&](uint8_t c) { ret.push_back(c); }, str.begin(), str.end(),
181 [](
char c) { return decode64_table[uint8_t(c)]; });
190 static const char *pbase32 =
"abcdefghijklmnopqrstuvwxyz234567";
193 str.reserve(((input.
size() + 4) / 5) * 8);
194 ConvertBits<8, 5, true>([&](
int v) { str += pbase32[v]; }, input.
begin(),
197 while (str.size() % 8) {
208std::optional<std::vector<uint8_t>>
DecodeBase32(std::string_view str) {
209 static const int8_t decode32_table[256] = {
210 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
211 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
212 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29,
213 30, 31, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6,
214 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
215 25, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
216 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1,
217 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
218 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
219 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
220 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
221 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
222 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
223 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
226 if (str.size() % 8 != 0) {
230 if (str.size() >= 1 && str.back() ==
'=') {
231 str.remove_suffix(1);
233 if (str.size() >= 2 && str.substr(str.size() - 2) ==
"==") {
234 str.remove_suffix(2);
236 if (str.size() >= 1 && str.back() ==
'=') {
237 str.remove_suffix(1);
239 if (str.size() >= 2 && str.substr(str.size() - 2) ==
"==") {
240 str.remove_suffix(2);
243 std::vector<uint8_t> ret;
244 ret.reserve((str.size() * 5) / 8);
245 bool valid = ConvertBits<5, 8, false>(
246 [&](uint8_t c) { ret.push_back(c); }, str.begin(), str.end(),
247 [](
char c) { return decode32_table[uint8_t(c)]; });
257template <
typename T>
bool ParseIntegral(std::string_view str, T *out) {
258 static_assert(std::is_integral<T>::value);
261 if (str.length() >= 2 && str[0] ==
'+' && str[1] ==
'-') {
264 const std::optional<T> opt_int =
265 ToIntegral<T>((!str.empty() && str[0] ==
'+') ? str.substr(1) : str);
269 if (out !=
nullptr) {
277 return ParseIntegral<int32_t>(str, out);
281 return ParseIntegral<int64_t>(str, out);
285 return ParseIntegral<uint8_t>(str, out);
289 return ParseIntegral<uint16_t>(str, out);
293 return ParseIntegral<uint32_t>(str, out);
297 return ParseIntegral<uint64_t>(str, out);
301 std::stringstream out;
304 while (ptr < in.size()) {
305 size_t lineend = in.find_first_of(
'\n', ptr);
306 if (lineend == std::string::npos) {
309 const size_t linelen = lineend - ptr;
310 const size_t rem_width = width - indented;
311 if (linelen <= rem_width) {
312 out << in.substr(ptr, linelen + 1);
316 size_t finalspace = in.find_last_of(
" \n", ptr + rem_width);
317 if (finalspace == std::string::npos || finalspace < ptr) {
319 finalspace = in.find_first_of(
"\n ", ptr);
320 if (finalspace == std::string::npos) {
322 out << in.substr(ptr);
326 out << in.substr(ptr, finalspace - ptr) <<
"\n";
327 if (in[finalspace] ==
'\n') {
330 out << std::string(indent,
' ');
333 ptr = finalspace + 1;
341 return _atoi64(str.c_str());
343 return strtoll(str.c_str(),
nullptr, 10);
347int atoi(
const std::string &str) {
348 return atoi(str.c_str());
364 int &mantissa_tzeros) {
368 for (
int i = 0; i <= mantissa_tzeros; ++i) {
375 mantissa += ch -
'0';
382 int64_t mantissa = 0;
383 int64_t exponent = 0;
384 int mantissa_tzeros = 0;
385 bool mantissa_sign =
false;
386 bool exponent_sign =
false;
388 int end = val.size();
391 if (ptr < end && val[ptr] ==
'-') {
392 mantissa_sign =
true;
396 if (val[ptr] ==
'0') {
399 }
else if (val[ptr] >=
'1' && val[ptr] <=
'9') {
400 while (ptr < end &&
IsDigit(val[ptr])) {
416 if (ptr < end && val[ptr] ==
'.') {
418 if (ptr < end &&
IsDigit(val[ptr])) {
419 while (ptr < end &&
IsDigit(val[ptr])) {
433 if (ptr < end && (val[ptr] ==
'e' || val[ptr] ==
'E')) {
435 if (ptr < end && val[ptr] ==
'+') {
437 }
else if (ptr < end && val[ptr] ==
'-') {
438 exponent_sign =
true;
441 if (ptr < end &&
IsDigit(val[ptr])) {
442 while (ptr < end &&
IsDigit(val[ptr])) {
447 exponent = exponent * 10 + val[ptr] -
'0';
461 exponent = -exponent;
463 exponent = exponent - point_ofs + mantissa_tzeros;
467 mantissa = -mantissa;
471 exponent += decimals;
476 if (exponent >= 18) {
481 for (
int i = 0; i < exponent; ++i) {
495 *amount_out = mantissa;
503 for (
auto ch : str) {
511 for (
auto ch : str) {
527using ByteAsHex = std::array<char, 2>;
529constexpr std::array<ByteAsHex, 256> CreateByteToHexMap() {
530 constexpr char hexmap[16] = {
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
531 '8',
'9',
'a',
'b',
'c',
'd',
'e',
'f'};
533 std::array<ByteAsHex, 256> byte_to_hex{};
534 for (
size_t i = 0; i < byte_to_hex.size(); ++i) {
535 byte_to_hex[i][0] = hexmap[i >> 4];
536 byte_to_hex[i][1] = hexmap[i & 15];
544 std::string rv(s.
size() * 2,
'\0');
545 static constexpr auto byte_to_hex = CreateByteToHexMap();
546 static_assert(
sizeof(byte_to_hex) == 512);
548 char *it = rv.data();
549 for (uint8_t v : s) {
550 std::memcpy(it, byte_to_hex[v].data(), 2);
554 assert(it == rv.data() + rv.size());
constexpr std::size_t size() const noexcept
constexpr C * begin() const noexcept
constexpr C * end() const noexcept
constexpr auto MakeUCharSpan(V &&v) -> decltype(UCharSpanCast(Span{std::forward< V >(v)}))
Like the Span constructor, but for (const) uint8_t member types only.
constexpr bool IsDigit(char c)
Tests if the given character is a decimal digit.
constexpr bool IsSpace(char c) noexcept
Tests if the given character is a whitespace character.
std::string Capitalize(std::string str)
Capitalizes the first character of the given string.
static const std::string SAFE_CHARS[]
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 HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
std::string EncodeBase64(Span< const uint8_t > input)
bool ParseUInt16(std::string_view str, uint16_t *out)
Convert decimal string to unsigned 16-bit integer with strict parse error feedback.
std::string ToUpper(std::string_view str)
Returns the uppercase equivalent of the given string.
const signed char p_util_hexdigit[256]
template std::vector< std::byte > ParseHex(std::string_view)
std::string EncodeBase32(Span< const uint8_t > input, bool pad)
Base32 encode.
bool ParseFixedPoint(std::string_view val, int decimals, int64_t *amount_out)
Parse number as fixed point according to JSON number syntax.
bool ParseInt64(std::string_view str, int64_t *out)
Convert string to signed 64-bit integer with strict parse error feedback.
bool ParseUInt8(std::string_view str, uint8_t *out)
Convert decimal string to unsigned 8-bit integer with strict parse error feedback.
bool ParseUInt64(std::string_view str, uint64_t *out)
Convert decimal string to unsigned 64-bit integer with strict parse error feedback.
signed char HexDigit(char c)
int atoi(const std::string &str)
static bool ProcessMantissaDigit(char ch, int64_t &mantissa, int &mantissa_tzeros)
Helper function for ParseFixedPoint.
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::string FormatParagraph(std::string_view in, size_t width, size_t indent)
Format a paragraph of text to a fixed width, adding spaces for indentation to any added line.
static const int64_t UPPER_BOUND
Upper bound for mantissa.
int64_t atoi64(const std::string &str)
std::optional< std::vector< uint8_t > > DecodeBase64(std::string_view str)
bool ParseUInt32(std::string_view str, uint32_t *out)
Convert decimal string to unsigned 32-bit integer with strict parse error feedback.
void SplitHostPort(std::string_view in, uint16_t &portOut, std::string &hostOut)
std::optional< std::vector< Byte > > TryParseHex(std::string_view str)
Parse the hex string into bytes (uint8_t or std::byte).
std::string ToLower(std::string_view str)
Returns the lowercase equivalent of the given string.
std::optional< std::vector< uint8_t > > DecodeBase32(std::string_view str)
std::string SanitizeString(std::string_view str, int rule)
Remove unsafe chars.
static const std::string CHARS_ALPHA_NUM