20 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
36 if (
SAFE_CHARS[rule].find(c) != std::string::npos) {
43bool IsHex(std::string_view str) {
49 return (str.size() > 0) && (str.size() % 2 == 0);
53 if (str.substr(0, 2) ==
"0x") {
62 return str.size() > 0;
65template <
typename Byte>
66std::optional<std::vector<Byte>>
TryParseHex(std::string_view str) {
67 std::vector<Byte> vch;
68 auto it = str.begin();
69 while (it != str.end()) {
75 if (it == str.end()) {
79 if (c1 < 0 || c2 < 0) {
82 vch.push_back(Byte(c1 << 4) | Byte(c2));
86template std::vector<std::byte>
ParseHex(std::string_view);
87template std::vector<uint8_t>
ParseHex(std::string_view);
90 std::string &hostOut) {
92 size_t colon = in.find_last_of(
':');
95 bool fHaveColon = colon != in.npos;
98 bool fBracketed = fHaveColon && (in[0] ==
'[' && in[colon - 1] ==
']');
100 fHaveColon && (in.find_last_of(
':', colon - 1) != in.npos);
101 if (fHaveColon && (colon == 0 || fBracketed || !fMultiColon)) {
104 in = in.substr(0, colon);
106 valid = (portOut != 0);
111 if (in.size() > 0 && in[0] ==
'[' && in[in.size() - 1] ==
']') {
112 hostOut = in.substr(1, in.size() - 2);
121 static const char *pbase64 =
122 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
125 str.reserve(((input.
size() + 2) / 3) * 4);
126 ConvertBits<8, 6, true>([&](
int v) { str += pbase64[v]; }, input.
begin(),
128 while (str.size() % 4) {
134std::optional<std::vector<uint8_t>>
DecodeBase64(std::string_view str) {
135 static const int8_t decode64_table[256] = {
136 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
137 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
138 -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57,
139 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6,
140 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
141 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
142 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1,
143 -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,
145 -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,
147 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
148 -1, -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,
152 if (str.size() % 4 != 0) {
156 if (str.size() >= 1 && str.back() ==
'=') {
157 str.remove_suffix(1);
159 if (str.size() >= 1 && str.back() ==
'=') {
160 str.remove_suffix(1);
163 std::vector<uint8_t> ret;
164 ret.reserve((str.size() * 3) / 4);
165 bool valid = ConvertBits<6, 8, false>(
166 [&](uint8_t c) { ret.push_back(c); }, str.begin(), str.end(),
167 [](
char c) { return decode64_table[uint8_t(c)]; });
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(),
183 while (str.size() % 8) {
194std::optional<std::vector<uint8_t>>
DecodeBase32(std::string_view str) {
195 static const int8_t decode32_table[256] = {
196 -1, -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,
198 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29,
199 30, 31, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6,
200 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
201 25, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
202 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1,
203 -1, -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,
205 -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,
207 -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,
209 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
212 if (str.size() % 8 != 0) {
216 if (str.size() >= 1 && str.back() ==
'=') {
217 str.remove_suffix(1);
219 if (str.size() >= 2 && str.substr(str.size() - 2) ==
"==") {
220 str.remove_suffix(2);
222 if (str.size() >= 1 && str.back() ==
'=') {
223 str.remove_suffix(1);
225 if (str.size() >= 2 && str.substr(str.size() - 2) ==
"==") {
226 str.remove_suffix(2);
229 std::vector<uint8_t> ret;
230 ret.reserve((str.size() * 5) / 8);
231 bool valid = ConvertBits<5, 8, false>(
232 [&](uint8_t c) { ret.push_back(c); }, str.begin(), str.end(),
233 [](
char c) { return decode32_table[uint8_t(c)]; });
243template <
typename T>
bool ParseIntegral(std::string_view str, T *out) {
244 static_assert(std::is_integral<T>::value);
247 if (str.length() >= 2 && str[0] ==
'+' && str[1] ==
'-') {
250 const std::optional<T> opt_int =
251 ToIntegral<T>((!str.empty() && str[0] ==
'+') ? str.substr(1) : str);
255 if (out !=
nullptr) {
263 return ParseIntegral<int32_t>(str, out);
267 return ParseIntegral<int64_t>(str, out);
271 return ParseIntegral<uint8_t>(str, out);
275 return ParseIntegral<uint16_t>(str, out);
279 return ParseIntegral<uint32_t>(str, out);
283 return ParseIntegral<uint64_t>(str, out);
287 std::stringstream out;
290 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;
338 int &mantissa_tzeros) {
342 for (
int i = 0; i <= mantissa_tzeros; ++i) {
349 mantissa += ch -
'0';
356 int64_t mantissa = 0;
357 int64_t exponent = 0;
358 int mantissa_tzeros = 0;
359 bool mantissa_sign =
false;
360 bool exponent_sign =
false;
362 int end = val.size();
365 if (ptr < end && val[ptr] ==
'-') {
366 mantissa_sign =
true;
370 if (val[ptr] ==
'0') {
373 }
else if (val[ptr] >=
'1' && val[ptr] <=
'9') {
374 while (ptr < end &&
IsDigit(val[ptr])) {
390 if (ptr < end && val[ptr] ==
'.') {
392 if (ptr < end &&
IsDigit(val[ptr])) {
393 while (ptr < end &&
IsDigit(val[ptr])) {
407 if (ptr < end && (val[ptr] ==
'e' || val[ptr] ==
'E')) {
409 if (ptr < end && val[ptr] ==
'+') {
411 }
else if (ptr < end && val[ptr] ==
'-') {
412 exponent_sign =
true;
415 if (ptr < end &&
IsDigit(val[ptr])) {
416 while (ptr < end &&
IsDigit(val[ptr])) {
421 exponent = exponent * 10 + val[ptr] -
'0';
435 exponent = -exponent;
437 exponent = exponent - point_ofs + mantissa_tzeros;
441 mantissa = -mantissa;
445 exponent += decimals;
450 if (exponent >= 18) {
455 for (
int i = 0; i < exponent; ++i) {
469 *amount_out = mantissa;
477 for (
auto ch : str) {
485 for (
auto ch : str) {
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) 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 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.
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.
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.
std::optional< std::vector< uint8_t > > 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::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