20static const std::string
CHARS_ALPHA_NUM =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
34 if (
SAFE_CHARS[rule].find(c) != std::string::npos) {
46 return (str.size() > 0) && (str.size()%2 == 0);
49template <
typename Byte>
50std::optional<std::vector<Byte>>
TryParseHex(std::string_view str)
52 std::vector<Byte> vch;
53 vch.reserve(str.size() / 2);
55 auto it = str.begin();
56 while (it != str.end()) {
62 if (it == str.end())
return std::nullopt;
64 if (c1 < 0 || c2 < 0)
return std::nullopt;
65 vch.push_back(Byte(c1 << 4) | Byte(c2));
69template std::optional<std::vector<std::byte>>
TryParseHex(std::string_view);
70template std::optional<std::vector<uint8_t>>
TryParseHex(std::string_view);
72bool SplitHostPort(std::string_view in, uint16_t& portOut, std::string& hostOut)
75 size_t colon = in.find_last_of(
':');
77 bool fHaveColon = colon != in.npos;
78 bool fBracketed = fHaveColon && (in[0] ==
'[' && in[colon - 1] ==
']');
79 bool fMultiColon{fHaveColon && colon != 0 && (in.find_last_of(
':', colon - 1) != in.npos)};
80 if (fHaveColon && (colon == 0 || fBracketed || !fMultiColon)) {
83 in = in.substr(0, colon);
85 valid = (portOut != 0);
90 if (in.size() > 0 && in[0] ==
'[' && in[in.size() - 1] ==
']') {
91 hostOut = in.substr(1, in.size() - 2);
101 static const char *pbase64 =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
104 str.reserve(((input.
size() + 2) / 3) * 4);
105 ConvertBits<8, 6, true>([&](
int v) { str += pbase64[v]; }, input.
begin(), input.
end());
106 while (str.size() % 4) str +=
'=';
110std::optional<std::vector<unsigned char>>
DecodeBase64(std::string_view str)
112 static const int8_t decode64_table[256]{
113 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
114 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
115 -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1,
116 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
117 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28,
118 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
119 49, 50, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
120 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
121 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
122 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
123 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
124 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
125 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
128 if (str.size() % 4 != 0)
return {};
130 if (str.size() >= 1 && str.back() ==
'=') str.remove_suffix(1);
131 if (str.size() >= 1 && str.back() ==
'=') str.remove_suffix(1);
133 std::vector<unsigned char>
ret;
134 ret.reserve((str.size() * 3) / 4);
135 bool valid = ConvertBits<6, 8, false>(
136 [&](
unsigned char c) {
ret.push_back(c); },
137 str.begin(), str.end(),
138 [](
char c) { return decode64_table[uint8_t(c)]; }
140 if (!valid)
return {};
147 static const char *pbase32 =
"abcdefghijklmnopqrstuvwxyz234567";
150 str.reserve(((input.
size() + 4) / 5) * 8);
151 ConvertBits<8, 5, true>([&](
int v) { str += pbase32[v]; }, input.
begin(), input.
end());
153 while (str.size() % 8) {
165std::optional<std::vector<unsigned char>>
DecodeBase32(std::string_view str)
167 static const int8_t decode32_table[256]{
168 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
169 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
170 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, -1, -1, -1, -1,
171 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
172 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 0, 1, 2,
173 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
174 23, 24, 25, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
175 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
176 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
177 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
178 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
179 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
180 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
183 if (str.size() % 8 != 0)
return {};
185 if (str.size() >= 1 && str.back() ==
'=') str.remove_suffix(1);
186 if (str.size() >= 2 && str.substr(str.size() - 2) ==
"==") str.remove_suffix(2);
187 if (str.size() >= 1 && str.back() ==
'=') str.remove_suffix(1);
188 if (str.size() >= 2 && str.substr(str.size() - 2) ==
"==") str.remove_suffix(2);
190 std::vector<unsigned char>
ret;
191 ret.reserve((str.size() * 5) / 8);
192 bool valid = ConvertBits<5, 8, false>(
193 [&](
unsigned char c) {
ret.push_back(c); },
194 str.begin(), str.end(),
195 [](
char c) { return decode32_table[uint8_t(c)]; }
198 if (!valid)
return {};
205bool ParseIntegral(std::string_view str, T*
out)
207 static_assert(std::is_integral<T>::value);
210 if (str.length() >= 2 && str[0] ==
'+' && str[1] ==
'-') {
213 const std::optional<T> opt_int = ToIntegral<T>((!str.empty() && str[0] ==
'+') ? str.substr(1) : str);
217 if (
out !=
nullptr) {
226 return ParseIntegral<int32_t>(str,
out);
231 return ParseIntegral<int64_t>(str,
out);
236 return ParseIntegral<uint8_t>(str,
out);
241 return ParseIntegral<uint16_t>(str,
out);
246 return ParseIntegral<uint32_t>(str,
out);
251 return ParseIntegral<uint64_t>(str,
out);
257 std::stringstream
out;
260 while (ptr < in.size())
262 size_t lineend = in.find_first_of(
'\n', ptr);
263 if (lineend == std::string::npos) {
266 const size_t linelen = lineend - ptr;
267 const size_t rem_width = width - indented;
268 if (linelen <= rem_width) {
269 out << in.substr(ptr, linelen + 1);
273 size_t finalspace = in.find_last_of(
" \n", ptr + rem_width);
274 if (finalspace == std::string::npos || finalspace < ptr) {
276 finalspace = in.find_first_of(
"\n ", ptr);
277 if (finalspace == std::string::npos) {
279 out << in.substr(ptr);
283 out << in.substr(ptr, finalspace - ptr) <<
"\n";
284 if (in[finalspace] ==
'\n') {
287 out << std::string(indent,
' ');
290 ptr = finalspace + 1;
312 for (
int i=0; i<=mantissa_tzeros; ++i) {
317 mantissa += ch -
'0';
325 int64_t mantissa = 0;
326 int64_t exponent = 0;
327 int mantissa_tzeros = 0;
328 bool mantissa_sign =
false;
329 bool exponent_sign =
false;
331 int end = val.size();
334 if (ptr < end && val[ptr] ==
'-') {
335 mantissa_sign =
true;
340 if (val[ptr] ==
'0') {
343 }
else if (val[ptr] >=
'1' && val[ptr] <=
'9') {
344 while (ptr < end &&
IsDigit(val[ptr])) {
351 if (ptr < end && val[ptr] ==
'.')
354 if (ptr < end &&
IsDigit(val[ptr]))
356 while (ptr < end &&
IsDigit(val[ptr])) {
364 if (ptr < end && (val[ptr] ==
'e' || val[ptr] ==
'E'))
367 if (ptr < end && val[ptr] ==
'+')
369 else if (ptr < end && val[ptr] ==
'-') {
370 exponent_sign =
true;
373 if (ptr < end &&
IsDigit(val[ptr])) {
374 while (ptr < end &&
IsDigit(val[ptr])) {
377 exponent = exponent * 10 + val[ptr] -
'0';
387 exponent = -exponent;
388 exponent = exponent - point_ofs + mantissa_tzeros;
392 mantissa = -mantissa;
395 exponent += decimals;
401 for (
int i=0; i < exponent; ++i) {
410 *amount_out = mantissa;
418 r.reserve(str.size());
419 for (
auto ch : str) r +=
ToLower(ch);
426 r.reserve(str.size());
427 for (
auto ch : str) r +=
ToUpper(ch);
433 if (str.empty())
return str;
443 auto multiplier = default_multiplier;
444 char unit = str.back();
475 uint64_t unit_amount =
static_cast<uint64_t
>(multiplier);
476 auto parsed_num = ToIntegral<uint64_t>(unit ? str.substr(0, str.size() - 1) : str);
477 if (!parsed_num || parsed_num > std::numeric_limits<uint64_t>::max() / unit_amount) {
480 return *parsed_num * unit_amount;
constexpr std::size_t size() const noexcept
constexpr C * begin() const noexcept
constexpr C * end() const noexcept
signed char HexDigit(char c)
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 ParseInt32(std::string_view str, int32_t *out)
Convert string to signed 32-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.
std::string ToUpper(std::string_view str)
Returns the uppercase equivalent of the given string.
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.
std::optional< std::vector< unsigned char > > DecodeBase32(std::string_view str)
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.
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::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 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