19static const std::string
CHARS_ALPHA_NUM =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
33 if (
SAFE_CHARS[rule].find(c) != std::string::npos) {
45 return (str.size() > 0) && (str.size()%2 == 0);
48template <
typename Byte>
49std::optional<std::vector<Byte>>
TryParseHex(std::string_view str)
51 std::vector<Byte> vch;
52 vch.reserve(str.size() / 2);
54 auto it = str.begin();
55 while (it != str.end()) {
61 if (it == str.end())
return std::nullopt;
63 if (c1 < 0 || c2 < 0)
return std::nullopt;
64 vch.push_back(Byte(c1 << 4) | Byte(c2));
68template std::optional<std::vector<std::byte>>
TryParseHex(std::string_view);
69template std::optional<std::vector<uint8_t>>
TryParseHex(std::string_view);
71bool SplitHostPort(std::string_view in, uint16_t& portOut, std::string& hostOut)
74 size_t colon = in.find_last_of(
':');
76 bool fHaveColon = colon != in.npos;
77 bool fBracketed = fHaveColon && (in[0] ==
'[' && in[colon - 1] ==
']');
78 bool fMultiColon{fHaveColon && colon != 0 && (in.find_last_of(
':', colon - 1) != in.npos)};
79 if (fHaveColon && (colon == 0 || fBracketed || !fMultiColon)) {
80 if (
const auto n{ToIntegral<uint16_t>(in.substr(colon + 1))}) {
81 in = in.substr(0, colon);
83 valid = (portOut != 0);
88 if (in.size() > 0 && in[0] ==
'[' && in[in.size() - 1] ==
']') {
89 hostOut = in.substr(1, in.size() - 2);
99 static const char *pbase64 =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
102 str.reserve(
CeilDiv(input.size(), 3u) * 4);
103 ConvertBits<8, 6, true>([&](
int v) { str += pbase64[v]; }, input.begin(), input.end());
104 while (str.size() % 4) str +=
'=';
108std::optional<std::vector<unsigned char>>
DecodeBase64(std::string_view str)
110 static const int8_t decode64_table[256]{
111 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
112 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
113 -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1,
114 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
115 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28,
116 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
117 49, 50, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
118 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
119 -1, -1, -1, -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
126 if (str.size() % 4 != 0)
return {};
128 if (str.size() >= 1 && str.back() ==
'=') str.remove_suffix(1);
129 if (str.size() >= 1 && str.back() ==
'=') str.remove_suffix(1);
131 std::vector<unsigned char>
ret;
132 ret.reserve((str.size() * 3) / 4);
133 bool valid = ConvertBits<6, 8, false>(
134 [&](
unsigned char c) {
ret.push_back(c); },
135 str.begin(), str.end(),
136 [](
char c) { return decode64_table[uint8_t(c)]; }
138 if (!valid)
return {};
143std::string
EncodeBase32(std::span<const unsigned char> input,
bool pad)
145 static const char *pbase32 =
"abcdefghijklmnopqrstuvwxyz234567";
148 str.reserve(
CeilDiv(input.size(), 5u) * 8);
149 ConvertBits<8, 5, true>([&](
int v) { str += pbase32[v]; }, input.begin(), input.end());
151 while (str.size() % 8) {
163std::optional<std::vector<unsigned char>>
DecodeBase32(std::string_view str)
165 static const int8_t decode32_table[256]{
166 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
167 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
168 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, -1, -1, -1, -1,
169 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
170 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 0, 1, 2,
171 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
172 23, 24, 25, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
173 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
174 -1, -1, -1, -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
181 if (str.size() % 8 != 0)
return {};
183 if (str.size() >= 1 && str.back() ==
'=') str.remove_suffix(1);
184 if (str.size() >= 2 && str.substr(str.size() - 2) ==
"==") str.remove_suffix(2);
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);
188 std::vector<unsigned char>
ret;
189 ret.reserve((str.size() * 5) / 8);
190 bool valid = ConvertBits<5, 8, false>(
191 [&](
unsigned char c) {
ret.push_back(c); },
192 str.begin(), str.end(),
193 [](
char c) { return decode32_table[uint8_t(c)]; }
196 if (!valid)
return {};
204 std::stringstream
out;
207 while (ptr < in.size())
209 size_t lineend = in.find_first_of(
'\n', ptr);
210 if (lineend == std::string::npos) {
213 const size_t linelen = lineend - ptr;
214 const size_t rem_width = width - indented;
215 if (linelen <= rem_width) {
216 out << in.substr(ptr, linelen + 1);
220 size_t finalspace = in.find_last_of(
" \n", ptr + rem_width);
221 if (finalspace == std::string::npos || finalspace < ptr) {
223 finalspace = in.find_first_of(
"\n ", ptr);
224 if (finalspace == std::string::npos) {
226 out << in.substr(ptr);
230 out << in.substr(ptr, finalspace - ptr) <<
"\n";
231 if (in[finalspace] ==
'\n') {
234 out << std::string(indent,
' ');
237 ptr = finalspace + 1;
259 for (
int i=0; i<=mantissa_tzeros; ++i) {
264 mantissa += ch -
'0';
272 int64_t mantissa = 0;
273 int64_t exponent = 0;
274 int mantissa_tzeros = 0;
275 bool mantissa_sign =
false;
276 bool exponent_sign =
false;
278 int end = val.size();
281 if (ptr < end && val[ptr] ==
'-') {
282 mantissa_sign =
true;
287 if (val[ptr] ==
'0') {
290 }
else if (val[ptr] >=
'1' && val[ptr] <=
'9') {
291 while (ptr < end &&
IsDigit(val[ptr])) {
298 if (ptr < end && val[ptr] ==
'.')
301 if (ptr < end &&
IsDigit(val[ptr]))
303 while (ptr < end &&
IsDigit(val[ptr])) {
311 if (ptr < end && (val[ptr] ==
'e' || val[ptr] ==
'E'))
314 if (ptr < end && val[ptr] ==
'+')
316 else if (ptr < end && val[ptr] ==
'-') {
317 exponent_sign =
true;
320 if (ptr < end &&
IsDigit(val[ptr])) {
321 while (ptr < end &&
IsDigit(val[ptr])) {
324 exponent = exponent * 10 + val[ptr] -
'0';
334 exponent = -exponent;
335 exponent = exponent - point_ofs + mantissa_tzeros;
339 mantissa = -mantissa;
342 exponent += decimals;
348 for (
int i=0; i < exponent; ++i) {
357 *amount_out = mantissa;
365 r.reserve(str.size());
366 for (
auto ch : str) r +=
ToLower(ch);
373 r.reserve(str.size());
374 for (
auto ch : str) r +=
ToUpper(ch);
380 if (str.empty())
return str;
390 auto multiplier = default_multiplier;
391 char unit = str.back();
422 uint64_t unit_amount =
static_cast<uint64_t
>(multiplier);
423 auto parsed_num = ToIntegral<uint64_t>(unit ? str.substr(0, str.size() - 1) : str);
424 if (!parsed_num || parsed_num > std::numeric_limits<uint64_t>::max() / unit_amount) {
427 return *parsed_num * unit_amount;
432 if (s1.size() != s2.size())
return false;
433 for (
size_t i = 0; i < s1.size(); ++i) {
435 if (c1 >=
'A' && c1 <=
'Z') c1 -= (
'A' -
'a');
437 if (c2 >=
'A' && c2 <=
'Z') c2 -= (
'A' -
'a');
438 if (c1 != c2)
return false;
signed char HexDigit(char c)
constexpr auto CeilDiv(const Dividend dividend, const Divisor divisor)
Integer ceiling division (for unsigned values).
constexpr auto MakeUCharSpan(const V &v) -> decltype(UCharSpanCast(std::span{v}))
Like the std::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[]
std::string ToUpper(std::string_view str)
Returns the uppercase equivalent of the given string.
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 CaseInsensitiveEqual(std::string_view s1, std::string_view s2)
Locale-independent, ASCII-only comparator.
std::string EncodeBase32(std::span< const unsigned char > input, bool pad)
Base32 encode.
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.
std::string EncodeBase64(std::span< const unsigned char > input)
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