18 static const std::string
CHARS_ALPHA_NUM =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
32 if (
SAFE_CHARS[rule].find(c) != std::string::npos) {
40 { -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
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 0,1,2,3,4,5,6,7,8,9,-1,-1,-1,-1,-1,-1,
44 -1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1,
45 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
46 -1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1,
47 -1,-1,-1,-1,-1,-1,-1,-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, };
62 bool IsHex(std::string_view str)
67 return (str.size() > 0) && (str.size()%2 == 0);
72 if (str.substr(0, 2) ==
"0x") str.remove_prefix(2);
77 return str.size() > 0;
80 template <
typename Byte>
81 std::optional<std::vector<Byte>>
TryParseHex(std::string_view str)
83 std::vector<Byte> vch;
84 auto it = str.begin();
85 while (it != str.end()) {
91 if (it == str.end())
return std::nullopt;
93 if (c1 < 0 || c2 < 0)
return std::nullopt;
94 vch.push_back(Byte(c1 << 4) | Byte(c2));
98 template std::optional<std::vector<std::byte>>
TryParseHex(std::string_view);
99 template std::optional<std::vector<uint8_t>>
TryParseHex(std::string_view);
101 bool SplitHostPort(std::string_view in, uint16_t& portOut, std::string& hostOut)
104 size_t colon = in.find_last_of(
':');
106 bool fHaveColon = colon != in.npos;
107 bool fBracketed = fHaveColon && (in[0] ==
'[' && in[colon - 1] ==
']');
108 bool fMultiColon{fHaveColon && colon != 0 && (in.find_last_of(
':', colon - 1) != in.npos)};
109 if (fHaveColon && (colon == 0 || fBracketed || !fMultiColon)) {
112 in = in.substr(0, colon);
114 valid = (portOut != 0);
119 if (in.size() > 0 && in[0] ==
'[' && in[in.size() - 1] ==
']') {
120 hostOut = in.substr(1, in.size() - 2);
130 static const char *pbase64 =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
133 str.reserve(((input.
size() + 2) / 3) * 4);
134 ConvertBits<8, 6, true>([&](
int v) { str += pbase64[v]; }, input.
begin(), input.
end());
135 while (str.size() % 4) str +=
'=';
139 std::optional<std::vector<unsigned char>>
DecodeBase64(std::string_view str)
141 static const int8_t decode64_table[256]{
142 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
143 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
144 -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1,
145 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
146 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28,
147 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
148 49, 50, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
149 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
150 -1, -1, -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, -1, -1,
152 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
153 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
154 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
157 if (str.size() % 4 != 0)
return {};
159 if (str.size() >= 1 && str.back() ==
'=') str.remove_suffix(1);
160 if (str.size() >= 1 && str.back() ==
'=') str.remove_suffix(1);
162 std::vector<unsigned char>
ret;
163 ret.reserve((str.size() * 3) / 4);
164 bool valid = ConvertBits<6, 8, false>(
165 [&](
unsigned char c) {
ret.push_back(c); },
166 str.begin(), str.end(),
167 [](
char c) { return decode64_table[uint8_t(c)]; }
169 if (!valid)
return {};
176 static const char *pbase32 =
"abcdefghijklmnopqrstuvwxyz234567";
179 str.reserve(((input.
size() + 4) / 5) * 8);
180 ConvertBits<8, 5, true>([&](
int v) { str += pbase32[v]; }, input.
begin(), input.
end());
182 while (str.size() % 8) {
194 std::optional<std::vector<unsigned char>>
DecodeBase32(std::string_view str)
196 static const int8_t decode32_table[256]{
197 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
198 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
199 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, -1, -1, -1, -1,
200 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
201 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 0, 1, 2,
202 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
203 23, 24, 25, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
204 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
205 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
206 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
207 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
208 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
209 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
212 if (str.size() % 8 != 0)
return {};
214 if (str.size() >= 1 && str.back() ==
'=') str.remove_suffix(1);
215 if (str.size() >= 2 && str.substr(str.size() - 2) ==
"==") str.remove_suffix(2);
216 if (str.size() >= 1 && str.back() ==
'=') str.remove_suffix(1);
217 if (str.size() >= 2 && str.substr(str.size() - 2) ==
"==") str.remove_suffix(2);
219 std::vector<unsigned char>
ret;
220 ret.reserve((str.size() * 5) / 8);
221 bool valid = ConvertBits<5, 8, false>(
222 [&](
unsigned char c) {
ret.push_back(c); },
223 str.begin(), str.end(),
224 [](
char c) { return decode32_table[uint8_t(c)]; }
227 if (!valid)
return {};
233 template <
typename T>
234 bool ParseIntegral(std::string_view str, T*
out)
236 static_assert(std::is_integral<T>::value);
239 if (str.length() >= 2 && str[0] ==
'+' && str[1] ==
'-') {
242 const std::optional<T> opt_int = ToIntegral<T>((!str.empty() && str[0] ==
'+') ? str.substr(1) : str);
246 if (
out !=
nullptr) {
255 return ParseIntegral<int32_t>(str,
out);
260 return ParseIntegral<int64_t>(str,
out);
265 return ParseIntegral<uint8_t>(str,
out);
270 return ParseIntegral<uint16_t>(str,
out);
275 return ParseIntegral<uint32_t>(str,
out);
280 return ParseIntegral<uint64_t>(str,
out);
286 std::stringstream
out;
289 while (ptr < in.size())
291 size_t lineend = in.find_first_of(
'\n', ptr);
292 if (lineend == std::string::npos) {
295 const size_t linelen = lineend - ptr;
296 const size_t rem_width = width - indented;
297 if (linelen <= rem_width) {
298 out << in.substr(ptr, linelen + 1);
302 size_t finalspace = in.find_last_of(
" \n", ptr + rem_width);
303 if (finalspace == std::string::npos || finalspace < ptr) {
305 finalspace = in.find_first_of(
"\n ", ptr);
306 if (finalspace == std::string::npos) {
308 out << in.substr(ptr);
312 out << in.substr(ptr, finalspace - ptr) <<
"\n";
313 if (in[finalspace] ==
'\n') {
316 out << std::string(indent,
' ');
319 ptr = finalspace + 1;
341 for (
int i=0; i<=mantissa_tzeros; ++i) {
346 mantissa += ch -
'0';
354 int64_t mantissa = 0;
355 int64_t exponent = 0;
356 int mantissa_tzeros = 0;
357 bool mantissa_sign =
false;
358 bool exponent_sign =
false;
360 int end = val.size();
363 if (ptr < end && val[ptr] ==
'-') {
364 mantissa_sign =
true;
369 if (val[ptr] ==
'0') {
372 }
else if (val[ptr] >=
'1' && val[ptr] <=
'9') {
373 while (ptr < end &&
IsDigit(val[ptr])) {
380 if (ptr < end && val[ptr] ==
'.')
383 if (ptr < end &&
IsDigit(val[ptr]))
385 while (ptr < end &&
IsDigit(val[ptr])) {
393 if (ptr < end && (val[ptr] ==
'e' || val[ptr] ==
'E'))
396 if (ptr < end && val[ptr] ==
'+')
398 else if (ptr < end && val[ptr] ==
'-') {
399 exponent_sign =
true;
402 if (ptr < end &&
IsDigit(val[ptr])) {
403 while (ptr < end &&
IsDigit(val[ptr])) {
406 exponent = exponent * 10 + val[ptr] -
'0';
416 exponent = -exponent;
417 exponent = exponent - point_ofs + mantissa_tzeros;
421 mantissa = -mantissa;
424 exponent += decimals;
430 for (
int i=0; i < exponent; ++i) {
439 *amount_out = mantissa;
447 for (
auto ch : str) r +=
ToLower(ch);
454 for (
auto ch : str) r +=
ToUpper(ch);
460 if (str.empty())
return str;
467 using ByteAsHex = std::array<char, 2>;
469 constexpr std::array<ByteAsHex, 256> CreateByteToHexMap()
471 constexpr
char hexmap[16] = {
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'a',
'b',
'c',
'd',
'e',
'f'};
473 std::array<ByteAsHex, 256> byte_to_hex{};
474 for (
size_t i = 0; i < byte_to_hex.size(); ++i) {
475 byte_to_hex[i][0] = hexmap[i >> 4];
476 byte_to_hex[i][1] = hexmap[i & 15];
485 std::string rv(s.
size() * 2,
'\0');
486 static constexpr
auto byte_to_hex = CreateByteToHexMap();
487 static_assert(
sizeof(byte_to_hex) == 512);
489 char* it = rv.data();
490 for (uint8_t v : s) {
491 std::memcpy(it, byte_to_hex[v].data(), 2);
495 assert(it == rv.data() + rv.size());
504 auto multiplier = default_multiplier;
505 char unit = str.back();
536 uint64_t unit_amount =
static_cast<uint64_t
>(multiplier);
537 auto parsed_num = ToIntegral<uint64_t>(unit ? str.substr(0, str.size() - 1) : str);
538 if (!parsed_num || parsed_num > std::numeric_limits<uint64_t>::max() / unit_amount) {
541 return *parsed_num * unit_amount;
constexpr std::size_t size() const noexcept
constexpr C * end() const noexcept
constexpr C * begin() const noexcept
constexpr auto MakeUCharSpan(V &&v) -> decltype(UCharSpanCast(Span{std::forward< V >(v)}))
Like the Span constructor, but for (const) unsigned char member types only.
constexpr bool IsDigit(char c)
Tests if the given character is a decimal digit.
ByteUnit
Used by ParseByteUnits() Lowercase base 1000 Uppercase base 1024.
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.
bool ParseUInt16(std::string_view str, uint16_t *out)
Convert decimal string to unsigned 16-bit integer with strict parse error feedback.
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].
std::string ToUpper(std::string_view str)
Returns the uppercase equivalent of the given string.
const signed char p_util_hexdigit[256]
std::string EncodeBase64(Span< const unsigned char > input)
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.
std::string EncodeBase32(Span< const unsigned char > input, bool pad)
Base32 encode.
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)
static bool ProcessMantissaDigit(char ch, int64_t &mantissa, int &mantissa_tzeros)
Helper function for ParseFixedPoint.
bool IsHex(std::string_view str)
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.
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 ParseUInt32(std::string_view str, uint32_t *out)
Convert decimal string to unsigned 32-bit integer with strict parse error feedback.
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::string SanitizeString(std::string_view str, int rule)
Remove unsafe chars.
static const std::string CHARS_ALPHA_NUM
std::optional< std::vector< unsigned char > > DecodeBase32(std::string_view str)