20 static const std::string
CHARS_ALPHA_NUM =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
34 if (
SAFE_CHARS[rule].find(c) != std::string::npos) {
41 bool IsHex(std::string_view str)
46 return (str.size() > 0) && (str.size()%2 == 0);
51 if (str.substr(0, 2) ==
"0x") str.remove_prefix(2);
56 return str.size() > 0;
59 template <
typename Byte>
60 std::optional<std::vector<Byte>>
TryParseHex(std::string_view str)
62 std::vector<Byte> vch;
63 vch.reserve(str.size() / 2);
65 auto it = str.begin();
66 while (it != str.end()) {
72 if (it == str.end())
return std::nullopt;
74 if (c1 < 0 || c2 < 0)
return std::nullopt;
75 vch.push_back(Byte(c1 << 4) | Byte(c2));
79 template std::optional<std::vector<std::byte>>
TryParseHex(std::string_view);
80 template std::optional<std::vector<uint8_t>>
TryParseHex(std::string_view);
82 bool SplitHostPort(std::string_view in, uint16_t& portOut, std::string& hostOut)
85 size_t colon = in.find_last_of(
':');
87 bool fHaveColon = colon != in.npos;
88 bool fBracketed = fHaveColon && (in[0] ==
'[' && in[colon - 1] ==
']');
89 bool fMultiColon{fHaveColon && colon != 0 && (in.find_last_of(
':', colon - 1) != in.npos)};
90 if (fHaveColon && (colon == 0 || fBracketed || !fMultiColon)) {
93 in = in.substr(0, colon);
95 valid = (portOut != 0);
100 if (in.size() > 0 && in[0] ==
'[' && in[in.size() - 1] ==
']') {
101 hostOut = in.substr(1, in.size() - 2);
111 static const char *pbase64 =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
114 str.reserve(((input.
size() + 2) / 3) * 4);
115 ConvertBits<8, 6, true>([&](
int v) { str += pbase64[v]; }, input.
begin(), input.
end());
116 while (str.size() % 4) str +=
'=';
120 std::optional<std::vector<unsigned char>>
DecodeBase64(std::string_view str)
122 static const int8_t decode64_table[256]{
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, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1,
126 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
127 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28,
128 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
129 49, 50, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
130 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
131 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
132 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
133 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
134 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
135 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
138 if (str.size() % 4 != 0)
return {};
140 if (str.size() >= 1 && str.back() ==
'=') str.remove_suffix(1);
141 if (str.size() >= 1 && str.back() ==
'=') str.remove_suffix(1);
143 std::vector<unsigned char>
ret;
144 ret.reserve((str.size() * 3) / 4);
145 bool valid = ConvertBits<6, 8, false>(
146 [&](
unsigned char c) {
ret.push_back(c); },
147 str.begin(), str.end(),
148 [](
char c) {
return decode64_table[uint8_t(c)]; }
150 if (!valid)
return {};
157 static const char *pbase32 =
"abcdefghijklmnopqrstuvwxyz234567";
160 str.reserve(((input.
size() + 4) / 5) * 8);
161 ConvertBits<8, 5, true>([&](
int v) { str += pbase32[v]; }, input.
begin(), input.
end());
163 while (str.size() % 8) {
175 std::optional<std::vector<unsigned char>>
DecodeBase32(std::string_view str)
177 static const int8_t decode32_table[256]{
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, 26, 27, 28, 29, 30, 31, -1, -1, -1, -1,
181 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
182 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 0, 1, 2,
183 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
184 23, 24, 25, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
185 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
186 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
187 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
188 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
189 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
190 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
193 if (str.size() % 8 != 0)
return {};
195 if (str.size() >= 1 && str.back() ==
'=') str.remove_suffix(1);
196 if (str.size() >= 2 && str.substr(str.size() - 2) ==
"==") str.remove_suffix(2);
197 if (str.size() >= 1 && str.back() ==
'=') str.remove_suffix(1);
198 if (str.size() >= 2 && str.substr(str.size() - 2) ==
"==") str.remove_suffix(2);
200 std::vector<unsigned char>
ret;
201 ret.reserve((str.size() * 5) / 8);
202 bool valid = ConvertBits<5, 8, false>(
203 [&](
unsigned char c) {
ret.push_back(c); },
204 str.begin(), str.end(),
205 [](
char c) {
return decode32_table[uint8_t(c)]; }
208 if (!valid)
return {};
214 template <
typename T>
215 bool ParseIntegral(std::string_view str, T*
out)
217 static_assert(std::is_integral<T>::value);
220 if (str.length() >= 2 && str[0] ==
'+' && str[1] ==
'-') {
223 const std::optional<T> opt_int = ToIntegral<T>((!str.empty() && str[0] ==
'+') ? str.substr(1) : str);
227 if (
out !=
nullptr) {
236 return ParseIntegral<int32_t>(str,
out);
241 return ParseIntegral<int64_t>(str,
out);
246 return ParseIntegral<uint8_t>(str,
out);
251 return ParseIntegral<uint16_t>(str,
out);
256 return ParseIntegral<uint32_t>(str,
out);
261 return ParseIntegral<uint64_t>(str,
out);
267 std::stringstream
out;
270 while (ptr < in.size())
272 size_t lineend = in.find_first_of(
'\n', ptr);
273 if (lineend == std::string::npos) {
276 const size_t linelen = lineend - ptr;
277 const size_t rem_width = width - indented;
278 if (linelen <= rem_width) {
279 out << in.substr(ptr, linelen + 1);
283 size_t finalspace = in.find_last_of(
" \n", ptr + rem_width);
284 if (finalspace == std::string::npos || finalspace < ptr) {
286 finalspace = in.find_first_of(
"\n ", ptr);
287 if (finalspace == std::string::npos) {
289 out << in.substr(ptr);
293 out << in.substr(ptr, finalspace - ptr) <<
"\n";
294 if (in[finalspace] ==
'\n') {
297 out << std::string(indent,
' ');
300 ptr = finalspace + 1;
322 for (
int i=0; i<=mantissa_tzeros; ++i) {
327 mantissa += ch -
'0';
335 int64_t mantissa = 0;
336 int64_t exponent = 0;
337 int mantissa_tzeros = 0;
338 bool mantissa_sign =
false;
339 bool exponent_sign =
false;
341 int end = val.size();
344 if (ptr < end && val[ptr] ==
'-') {
345 mantissa_sign =
true;
350 if (val[ptr] ==
'0') {
353 }
else if (val[ptr] >=
'1' && val[ptr] <=
'9') {
354 while (ptr < end &&
IsDigit(val[ptr])) {
361 if (ptr < end && val[ptr] ==
'.')
364 if (ptr < end &&
IsDigit(val[ptr]))
366 while (ptr < end &&
IsDigit(val[ptr])) {
374 if (ptr < end && (val[ptr] ==
'e' || val[ptr] ==
'E'))
377 if (ptr < end && val[ptr] ==
'+')
379 else if (ptr < end && val[ptr] ==
'-') {
380 exponent_sign =
true;
383 if (ptr < end &&
IsDigit(val[ptr])) {
384 while (ptr < end &&
IsDigit(val[ptr])) {
387 exponent = exponent * 10 + val[ptr] -
'0';
397 exponent = -exponent;
398 exponent = exponent - point_ofs + mantissa_tzeros;
402 mantissa = -mantissa;
405 exponent += decimals;
411 for (
int i=0; i < exponent; ++i) {
420 *amount_out = mantissa;
428 r.reserve(str.size());
429 for (
auto ch : str) r +=
ToLower(ch);
436 r.reserve(str.size());
437 for (
auto ch : str) r +=
ToUpper(ch);
443 if (str.empty())
return str;
453 auto multiplier = default_multiplier;
454 char unit = str.back();
485 uint64_t unit_amount =
static_cast<uint64_t
>(multiplier);
486 auto parsed_num = ToIntegral<uint64_t>(unit ? str.substr(0, str.size() - 1) : str);
487 if (!parsed_num || parsed_num > std::numeric_limits<uint64_t>::max() / unit_amount) {
490 return *parsed_num * unit_amount;
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 SplitHostPort(std::string_view in, uint16_t &portOut, std::string &hostOut)
Splits socket address string into host string and port value.
bool IsHex(std::string_view str)
constexpr std::size_t size() const noexcept
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.
signed char HexDigit(char c)
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< 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.
std::optional< std::vector< Byte > > TryParseHex(std::string_view str)
Parse the hex string into bytes (uint8_t or std::byte).
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.