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::vector<Byte>
ParseHex(std::string_view str)
83 std::vector<Byte> vch;
84 auto it = str.begin();
85 while (it != str.end() && it + 1 != str.end()) {
92 if (c1 < 0 || c2 < 0)
break;
93 vch.push_back(Byte(c1 << 4) | Byte(c2));
97 template std::vector<std::byte>
ParseHex(std::string_view);
98 template std::vector<uint8_t>
ParseHex(std::string_view);
100 void SplitHostPort(std::string_view in, uint16_t& portOut, std::string& hostOut)
102 size_t colon = in.find_last_of(
':');
104 bool fHaveColon = colon != in.npos;
105 bool fBracketed = fHaveColon && (in[0] ==
'[' && in[colon - 1] ==
']');
106 bool fMultiColon{fHaveColon && colon != 0 && (in.find_last_of(
':', colon - 1) != in.npos)};
107 if (fHaveColon && (colon == 0 || fBracketed || !fMultiColon)) {
110 in = in.substr(0, colon);
114 if (in.size() > 0 && in[0] ==
'[' && in[in.size() - 1] ==
']') {
115 hostOut = in.substr(1, in.size() - 2);
123 static const char *pbase64 =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
126 str.reserve(((input.
size() + 2) / 3) * 4);
127 ConvertBits<8, 6, true>([&](
int v) { str += pbase64[v]; }, input.
begin(), input.
end());
128 while (str.size() % 4) str +=
'=';
132 std::optional<std::vector<unsigned char>>
DecodeBase64(std::string_view str)
134 static const int8_t decode64_table[256]{
135 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
136 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
137 -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1,
138 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
139 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28,
140 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
141 49, 50, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
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, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
145 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
146 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
147 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
150 if (str.size() % 4 != 0)
return {};
152 if (str.size() >= 1 && str.back() ==
'=') str.remove_suffix(1);
153 if (str.size() >= 1 && str.back() ==
'=') str.remove_suffix(1);
155 std::vector<unsigned char>
ret;
156 ret.reserve((str.size() * 3) / 4);
157 bool valid = ConvertBits<6, 8, false>(
158 [&](
unsigned char c) {
ret.push_back(c); },
159 str.begin(), str.end(),
160 [](
char c) {
return decode64_table[uint8_t(c)]; }
162 if (!valid)
return {};
169 static const char *pbase32 =
"abcdefghijklmnopqrstuvwxyz234567";
172 str.reserve(((input.
size() + 4) / 5) * 8);
173 ConvertBits<8, 5, true>([&](
int v) { str += pbase32[v]; }, input.
begin(), input.
end());
175 while (str.size() % 8) {
187 std::optional<std::vector<unsigned char>>
DecodeBase32(std::string_view str)
189 static const int8_t decode32_table[256]{
190 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
191 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
192 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, -1, -1, -1, -1,
193 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
194 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 0, 1, 2,
195 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
196 23, 24, 25, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
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, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
200 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
201 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
202 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
205 if (str.size() % 8 != 0)
return {};
207 if (str.size() >= 1 && str.back() ==
'=') str.remove_suffix(1);
208 if (str.size() >= 2 && str.substr(str.size() - 2) ==
"==") str.remove_suffix(2);
209 if (str.size() >= 1 && str.back() ==
'=') str.remove_suffix(1);
210 if (str.size() >= 2 && str.substr(str.size() - 2) ==
"==") str.remove_suffix(2);
212 std::vector<unsigned char>
ret;
213 ret.reserve((str.size() * 5) / 8);
214 bool valid = ConvertBits<5, 8, false>(
215 [&](
unsigned char c) {
ret.push_back(c); },
216 str.begin(), str.end(),
217 [](
char c) {
return decode32_table[uint8_t(c)]; }
220 if (!valid)
return {};
226 template <
typename T>
227 bool ParseIntegral(std::string_view str, T* out)
229 static_assert(std::is_integral<T>::value);
232 if (str.length() >= 2 && str[0] ==
'+' && str[1] ==
'-') {
235 const std::optional<T> opt_int = ToIntegral<T>((!str.empty() && str[0] ==
'+') ? str.substr(1) : str);
239 if (out !=
nullptr) {
248 return ParseIntegral<int32_t>(str, out);
253 return ParseIntegral<int64_t>(str, out);
258 return ParseIntegral<uint8_t>(str, out);
263 return ParseIntegral<uint16_t>(str, out);
268 return ParseIntegral<uint32_t>(str, out);
273 return ParseIntegral<uint64_t>(str, out);
279 std::stringstream out;
282 while (ptr < in.size())
284 size_t lineend = in.find_first_of(
'\n', ptr);
285 if (lineend == std::string::npos) {
288 const size_t linelen = lineend - ptr;
289 const size_t rem_width = width - indented;
290 if (linelen <= rem_width) {
291 out << in.substr(ptr, linelen + 1);
295 size_t finalspace = in.find_last_of(
" \n", ptr + rem_width);
296 if (finalspace == std::string::npos || finalspace < ptr) {
298 finalspace = in.find_first_of(
"\n ", ptr);
299 if (finalspace == std::string::npos) {
301 out << in.substr(ptr);
305 out << in.substr(ptr, finalspace - ptr) <<
"\n";
306 if (in[finalspace] ==
'\n') {
309 out << std::string(indent,
' ');
312 ptr = finalspace + 1;
334 for (
int i=0; i<=mantissa_tzeros; ++i) {
339 mantissa += ch -
'0';
347 int64_t mantissa = 0;
348 int64_t exponent = 0;
349 int mantissa_tzeros = 0;
350 bool mantissa_sign =
false;
351 bool exponent_sign =
false;
353 int end = val.size();
356 if (ptr < end && val[ptr] ==
'-') {
357 mantissa_sign =
true;
362 if (val[ptr] ==
'0') {
365 }
else if (val[ptr] >=
'1' && val[ptr] <=
'9') {
366 while (ptr < end &&
IsDigit(val[ptr])) {
373 if (ptr < end && val[ptr] ==
'.')
376 if (ptr < end &&
IsDigit(val[ptr]))
378 while (ptr < end &&
IsDigit(val[ptr])) {
386 if (ptr < end && (val[ptr] ==
'e' || val[ptr] ==
'E'))
389 if (ptr < end && val[ptr] ==
'+')
391 else if (ptr < end && val[ptr] ==
'-') {
392 exponent_sign =
true;
395 if (ptr < end &&
IsDigit(val[ptr])) {
396 while (ptr < end &&
IsDigit(val[ptr])) {
399 exponent = exponent * 10 + val[ptr] -
'0';
409 exponent = -exponent;
410 exponent = exponent - point_ofs + mantissa_tzeros;
414 mantissa = -mantissa;
417 exponent += decimals;
423 for (
int i=0; i < exponent; ++i) {
432 *amount_out = mantissa;
440 for (
auto ch : str) r +=
ToLower(ch);
447 for (
auto ch : str) r +=
ToUpper(ch);
453 if (str.empty())
return str;
460 using ByteAsHex = std::array<char, 2>;
462 constexpr std::array<ByteAsHex, 256> CreateByteToHexMap()
464 constexpr
char hexmap[16] = {
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'a',
'b',
'c',
'd',
'e',
'f'};
466 std::array<ByteAsHex, 256> byte_to_hex{};
467 for (
size_t i = 0; i < byte_to_hex.size(); ++i) {
468 byte_to_hex[i][0] = hexmap[i >> 4];
469 byte_to_hex[i][1] = hexmap[i & 15];
478 std::string rv(s.
size() * 2,
'\0');
479 static constexpr
auto byte_to_hex = CreateByteToHexMap();
480 static_assert(
sizeof(byte_to_hex) == 512);
482 char* it = rv.data();
483 for (uint8_t v : s) {
484 std::memcpy(it, byte_to_hex[v].data(), 2);
488 assert(it == rv.data() + rv.size());
497 auto multiplier = default_multiplier;
498 char unit = str.back();
529 uint64_t unit_amount =
static_cast<uint64_t
>(multiplier);
530 auto parsed_num = ToIntegral<uint64_t>(unit ? str.substr(0, str.size() - 1) : str);
531 if (!parsed_num || parsed_num > std::numeric_limits<uint64_t>::max() / unit_amount) {
534 return *parsed_num * unit_amount;
signed char HexDigit(char c)
static const std::string SAFE_CHARS[]
constexpr C * end() const noexcept
bool IsHexNumber(std::string_view str)
Return true if the string is a hex number, optionally prefixed with "0x".
bool IsHex(std::string_view str)
constexpr std::size_t size() const noexcept
const signed char p_util_hexdigit[256]
constexpr bool IsDigit(char c)
Tests if the given character is a decimal digit.
bool ParseUInt64(std::string_view str, uint64_t *out)
Convert decimal string to unsigned 64-bit integer with strict parse error feedback.
std::string EncodeBase64(Span< const unsigned char > input)
static const int64_t UPPER_BOUND
Upper bound for mantissa.
std::string SanitizeString(std::string_view str, int rule)
Remove unsafe chars.
bool ParseUInt32(std::string_view str, uint32_t *out)
Convert decimal string to unsigned 32-bit integer with strict parse error feedback.
std::vector< Byte > ParseHex(std::string_view str)
Parse the hex string into bytes (uint8_t or std::byte).
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
std::optional< std::vector< unsigned char > > DecodeBase64(std::string_view str)
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].
bool ParseUInt8(std::string_view str, uint8_t *out)
Convert decimal string to unsigned 8-bit integer with strict parse error feedback.
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 std::string CHARS_ALPHA_NUM
ByteUnit
Used by ParseByteUnits() Lowercase base 1000 Uppercase base 1024.
bool ParseFixedPoint(std::string_view val, int decimals, int64_t *amount_out)
Parse number as fixed point according to JSON number syntax.
std::string ToLower(std::string_view str)
Returns the lowercase equivalent of the given string.
bool ParseInt32(std::string_view str, int32_t *out)
Convert string to signed 32-bit integer with strict parse error feedback.
constexpr C * begin() const noexcept
bool ParseInt64(std::string_view str, int64_t *out)
Convert string to signed 64-bit integer with strict parse error feedback.
constexpr bool IsSpace(char c) noexcept
Tests if the given character is a whitespace character.
std::optional< std::vector< unsigned char > > DecodeBase32(std::string_view str)
constexpr auto MakeUCharSpan(V &&v) -> decltype(UCharSpanCast(Span
Like the Span constructor, but for (const) unsigned char member types only.
static bool ProcessMantissaDigit(char ch, int64_t &mantissa, int &mantissa_tzeros)
Helper function for ParseFixedPoint.
std::string Capitalize(std::string str)
Capitalizes the first character of the given string.
std::string EncodeBase32(Span< const unsigned char > input, bool pad)
Base32 encode.
std::string ToUpper(std::string_view str)
Returns the uppercase equivalent of the given string.
void SplitHostPort(std::string_view in, uint16_t &portOut, std::string &hostOut)