8 #include "fmt/printf.h"
12 #pragma comment(lib, "ws2_32.lib")
57 ASSERT(a_Format !=
nullptr);
58 fmt::memory_buffer Buffer;
59 fmt::vprintf(Buffer, fmt::to_string_view(a_Format), a_ArgList);
60 a_String.assign(Buffer.data(), Buffer.size());
70 ASSERT(a_Format !=
nullptr);
71 return fmt::vsprintf(a_Format, a_ArgList);
80 ASSERT(a_Format !=
nullptr);
81 fmt::memory_buffer Buffer;
82 fmt::vprintf(Buffer, fmt::to_string_view(a_Format), a_ArgList);
83 a_String.append(Buffer.data(), Buffer.size());
96 while ((cutAt = str.find_first_of(delim, Prev)) != str.npos)
98 results.push_back(str.substr(Prev, cutAt - Prev));
101 if (Prev < str.length())
103 results.push_back(str.substr(Prev));
118 size_t cutAtQuote = 0;
120 while ((cutAt = str.find_first_of(delim, Prev)) != str.npos)
129 AString current = str.substr(Prev, cutAt - Prev);
130 if ((current.front() ==
'"') || (current.front() ==
'\''))
133 cutAtQuote = str.find_first_of(current.front(), Prev);
134 if (cutAtQuote != str.npos)
136 current = str.substr(Prev, cutAtQuote - Prev);
137 cutAt = cutAtQuote + 1;
141 results.push_back(std::move(current));
145 if (Prev < str.length())
147 AString current = str.substr(Prev);
151 (current.length() >= 2) &&
152 ((current.front() ==
'"') || (current.front() ==
'\'')) &&
153 (current.front() == current.back())
156 current = current.substr(1, current.length() - 2);
159 results.push_back(current);
171 if (a_Strings.empty())
177 const auto DelimSize = a_Delimeter.size();
178 size_t ResultSize = a_Strings[0].size();
179 std::for_each(a_Strings.begin() + 1, a_Strings.end(),
182 ResultSize += DelimSize;
183 ResultSize += a_String.size();
189 Result.reserve(ResultSize);
190 Result.append(a_Strings[0]);
191 std::for_each(a_Strings.begin() + 1, a_Strings.end(),
194 Result += a_Delimeter;
210 while ((cutAt = str.find_first_of(delim, Prev)) != str.npos)
212 results.push_back(
TrimString(str.substr(Prev, cutAt - Prev)));
215 if (Prev < str.length())
217 results.push_back(
TrimString(str.substr(Prev)));
228 size_t len = str.length();
232 if (
static_cast<unsigned char>(str[start]) > 32)
246 if (
static_cast<unsigned char>(str[end]) > 32)
253 return str.substr(start, end - start + 1);
262 std::transform(s.begin(), s.end(), s.begin(), ::tolower);
272 std::transform(s.begin(), s.end(), s.begin(), ::toupper);
283 res.resize(s.size());
284 std::transform(s.begin(), s.end(), res.begin(), ::tolower);
295 res.resize(s.size());
296 std::transform(s.begin(), s.end(), res.begin(), ::toupper);
307 return _stricmp(s1.c_str(), s2.c_str());
309 return strcasecmp(s1.c_str(), s2.c_str());
310 #endif // else _MSC_VER
319 size_t MatchedLetters = 0;
320 size_t s1Length = s1.length();
322 if (s1Length > s2.length())
328 for (
size_t i = 0; i < s1Length; i++)
330 char c1 =
static_cast<char>(toupper(s1[i]));
331 char c2 =
static_cast<char>(toupper(s2[i]));
341 return MatchedLetters;
356 size_t pos1 = iHayStack.find(iNeedle);
357 while (pos1 != AString::npos)
359 iHayStack.replace( pos1, iNeedle.size(), iReplaceWith);
360 pos1 = iHayStack.find(iNeedle, pos1 + iReplaceWith.size());
370 auto ReplaceWith =
URLEncode(iReplaceWith);
381 a_UTF8.reserve(3 * a_NumShorts / 2);
382 for (
size_t i = 0; i < a_NumShorts; i++)
395 if (a_UnicodeChar < 0x80)
397 return AString{
static_cast<char>(a_UnicodeChar)};
399 else if (a_UnicodeChar < 0x800)
403 static_cast<char>(192 + a_UnicodeChar / 64),
404 static_cast<char>(128 + a_UnicodeChar % 64),
407 else if (a_UnicodeChar - 0xd800 < 0x800)
412 else if (a_UnicodeChar < 0x10000)
416 static_cast<char>(224 + a_UnicodeChar / 4096),
417 static_cast<char>(128 + (a_UnicodeChar / 64) % 64),
418 static_cast<char>(128 + a_UnicodeChar % 64)
421 else if (a_UnicodeChar < 0x110000)
425 static_cast<char>(240 + a_UnicodeChar / 262144),
426 static_cast<char>(128 + (a_UnicodeChar / 4096) % 64),
427 static_cast<char>(128 + (a_UnicodeChar / 64) % 64),
428 static_cast<char>(128 + a_UnicodeChar % 64),
443 #pragma GCC diagnostic push
444 #pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
476 #define UNI_MAX_BMP 0x0000FFFF
477 #define UNI_MAX_UTF16 0x0010FFFF
478 #define UNI_SUR_HIGH_START 0xD800
479 #define UNI_SUR_LOW_START 0xDC00
480 #define UNI_SUR_LOW_END 0xDFFF
488 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
489 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
490 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
491 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
492 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
493 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
494 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
495 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5
504 0x00000000UL, 0x00003080UL, 0x000E2080UL,
505 0x03C82080UL, 0xFA082080UL, 0x82082080UL
515 const unsigned char * srcptr = source + length;
518 default:
return false;
520 case 4:
if (((a = (*--srcptr)) < 0x80) || (a > 0xbf))
return false;
521 case 3:
if (((a = (*--srcptr)) < 0x80) || (a > 0xbf))
return false;
524 if ((a = (*--srcptr)) > 0xbf)
531 case 0xe0:
if (a < 0xa0)
return false;
break;
532 case 0xed:
if (a > 0x9f)
return false;
break;
533 case 0xf0:
if (a < 0x90)
return false;
break;
534 case 0xf4:
if (a > 0x8f)
return false;
break;
535 default:
if (a < 0x80)
return false;
538 case 1:
if ((*source >= 0x80) && (*source < 0xc2))
return false;
540 return (*source <= 0xf4);
549 std::u16string UTF16;
550 UTF16.reserve(a_UTF8.size() * 2);
552 const unsigned char * source =
reinterpret_cast<const unsigned char *
>(a_UTF8.data());
553 const unsigned char * sourceEnd = source + a_UTF8.size();
554 const int halfShift = 10;
555 const unsigned int halfBase = 0x0010000UL;
556 const unsigned int halfMask = 0x3ffUL;
558 while (source < sourceEnd)
562 if (source + extraBytesToRead >= sourceEnd)
573 switch (extraBytesToRead)
575 case 5: ch += *source++; ch <<= 6;
576 case 4: ch += *source++; ch <<= 6;
577 case 3: ch += *source++; ch <<= 6;
578 case 2: ch += *source++; ch <<= 6;
579 case 1: ch += *source++; ch <<= 6;
580 case 0: ch += *source++;
592 unsigned short v = htons(
static_cast<unsigned short>(ch));
593 UTF16.push_back(
static_cast<char16_t
>(v));
598 unsigned short v = htons(
' ');
599 UTF16.push_back(
static_cast<char16_t
>(v));
607 UTF16.push_back(
static_cast<char16_t
>(v1));
608 UTF16.push_back(
static_cast<char16_t
>(v2));
639 #pragma GCC diagnostic pop
646 #define HEX(x) static_cast<char>((x) > 9 ? (x) + 'A' - 10 : (x) + '0')
654 fmt::memory_buffer Output;
658 fmt::memory_buffer Hex, Chars;
663 const size_t NumLines = a_Size / a_BytesPerLine + (a_Size % a_BytesPerLine != 0);
664 const size_t CharsPerLine = 14 + 4 * a_BytesPerLine;
665 Output.reserve(NumLines * CharsPerLine);
668 for (
size_t i = 0; i < a_Size; i += a_BytesPerLine)
670 size_t k = std::min(a_Size - i, a_BytesPerLine);
671 for (
size_t j = 0; j < k; j++)
673 Byte c = (
static_cast<const Byte *
>(a_Data))[i + j];
674 Hex.push_back(
HEX(c >> 4));
675 Hex.push_back(
HEX(c & 0xf));
677 Chars.push_back((c >=
' ') ?
static_cast<char>(c) :
'.');
681 auto HexStr = fmt::string_view(Hex.data(), Hex.size());
682 auto CharsStr = fmt::string_view(Chars.data(), Chars.size());
684 Output,
"{0:08x}: {1:{2}} {3}\n",
685 i, HexStr, a_BytesPerLine * 3, CharsStr
691 a_Out.append(Output.data(), Output.size());
702 size_t len = a_Message.size();
704 EscapedMsg.reserve(len);
705 for (
size_t i = 0; i < len; i++)
707 char ch = a_Message[i];
716 EscapedMsg.append(a_Message, last, i - last);
718 EscapedMsg.push_back(
'\\');
719 EscapedMsg.push_back(ch);
727 EscapedMsg.append(a_Message, last, len - last);
742 idx = res.find(
"\xc2\xa7", idx);
743 if (idx == AString::npos)
758 auto len = a_Text.size();
760 for (
size_t i = 0; i < len; i++)
762 if (a_Text[i] ==
'+')
767 if (a_Text[i] !=
'%')
769 res.push_back(a_Text[i]);
775 return std::make_pair(
false,
AString());
777 if ((a_Text[i + 1] ==
'u') || (a_Text[i + 1] ==
'U'))
782 return std::make_pair(
false,
AString());
784 if (a_Text[i + 2] !=
'0')
786 return std::make_pair(
false,
AString());
788 unsigned v1 =
HexToDec(a_Text[i + 3]);
789 unsigned v2 =
HexToDec(a_Text[i + 4]);
790 unsigned v3 =
HexToDec(a_Text[i + 5]);
791 unsigned v4 =
HexToDec(a_Text[i + 6]);
792 if ((v1 == 0xff) || (v2 == 0xff) || (v4 == 0xff) || (v3 == 0xff))
795 return std::make_pair(
false,
AString());
805 return std::make_pair(
false,
AString());
809 if ((v1 == 0xff) || (v2 == 0xff))
812 return std::make_pair(
false,
AString());
814 res.push_back(
static_cast<char>((v1 << 4) | v2));
818 return std::make_pair(
true, res);
828 auto len = a_Text.size();
830 static const char HEX[] =
"0123456789ABCDEF";
831 for (
size_t i = 0; i < len; ++i)
833 if (isalnum(a_Text[i]))
835 res.push_back(a_Text[i]);
837 else if (a_Text[i] ==
' ')
844 res.push_back(
HEX[
static_cast<unsigned char>(a_Text[i]) >> 4]);
845 res.push_back(
HEX[
static_cast<unsigned char>(a_Text[i]) & 0x0f]);
858 std::replace(res.begin(), res.end(), a_From, a_To);
869 if ((c >=
'A') && (c <=
'Z'))
873 if ((c >=
'a') && (c <=
'z'))
877 if ((c >=
'0') && (c <=
'9'))
903 size_t i, len = a_Base64String.size();
906 res.resize((len * 4) / 3 + 5, 0);
907 for (o = 0, i = 0; i < len; i++)
914 case 0: res[o >> 3] |= (c << 2);
break;
915 case 6: res[o >> 3] |= (c >> 4); res[(o >> 3) + 1] |= (c << 4);
break;
916 case 4: res[o >> 3] |= (c >> 2); res[(o >> 3) + 1] |= (c << 6);
break;
917 case 2: res[o >> 3] |= c;
break;
938 static const char BASE64[64] =
940 'A',
'B',
'C',
'D',
'E',
'F',
'G',
'H',
'I',
'J',
'K',
'L',
'M',
'N',
'O',
'P',
941 'Q',
'R',
'S',
'T',
'U',
'V',
'W',
'X',
'Y',
'Z',
'a',
'b',
'c',
'd',
'e',
'f',
942 'g',
'h',
'i',
'j',
'k',
'l',
'm',
'n',
'o',
'p',
'q',
'r',
's',
't',
'u',
'v',
943 'w',
'x',
'y',
'z',
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'+',
'/'
947 output.resize(((a_Input.size() + 2) / 3) * 4);
949 size_t output_index = 0;
950 size_t size_full24 = (a_Input.size() / 3) * 3;
952 for (
size_t i = 0; i < size_full24; i += 3)
954 output[output_index++] = BASE64[
static_cast<unsigned char>(a_Input[i]) >> 2];
955 output[output_index++] = BASE64[(
static_cast<unsigned char>(a_Input[i]) << 4 |
static_cast<unsigned char>(a_Input[i + 1]) >> 4) & 63];
956 output[output_index++] = BASE64[(
static_cast<unsigned char>(a_Input[i + 1]) << 2 |
static_cast<unsigned char>(a_Input[i + 2]) >> 6) & 63];
957 output[output_index++] = BASE64[
static_cast<unsigned char>(a_Input[i + 2]) & 63];
960 if (size_full24 < a_Input.size())
962 output[output_index++] = BASE64[
static_cast<unsigned char>(a_Input[size_full24]) >> 2];
963 if (size_full24 + 1 == a_Input.size())
965 output[output_index++] = BASE64[(
static_cast<unsigned char>(a_Input[size_full24]) << 4) & 63];
966 output[output_index++] =
'=';
970 output[output_index++] = BASE64[(
static_cast<unsigned char>(a_Input[size_full24]) << 4 |
static_cast<unsigned char>(a_Input[size_full24 + 1]) >> 4) & 63];
971 output[output_index++] = BASE64[(
static_cast<unsigned char>(a_Input[size_full24 + 1]) << 2) & 63];
974 output[output_index++] =
'=';
976 ASSERT(output_index == output.size());
987 return static_cast<short>(
988 (
static_cast<short>(a_Mem[0]) << 8) |
989 static_cast<short>(a_Mem[1])
999 const Byte * Bytes =
reinterpret_cast<const Byte *
>(a_Mem);
1000 return static_cast<unsigned short>((Bytes[0] << 8) | Bytes[1]);
1010 (
static_cast<int>(a_Mem[0]) << 24) |
1011 (
static_cast<int>(a_Mem[1]) << 16) |
1012 (
static_cast<int>(a_Mem[2]) << 8) |
1013 static_cast<int>(a_Mem[3])
1023 a_Mem[0] = std::byte(a_Value >> 24);
1024 a_Mem[1] = std::byte((a_Value >> 16) & 0xff);
1025 a_Mem[2] = std::byte((a_Value >> 8) & 0xff);
1026 a_Mem[3] = std::byte(a_Value & 0xff);
1036 size_t size = a_Strings.size();
1039 for (
size_t i = 0; i < size; i++)
1041 if (a_Strings[i] == 0)
1043 a_Output.push_back(a_Strings.substr(start, i - start));
1050 a_Output.push_back(a_Strings.substr(start, size - start));
1067 for (
const auto & item : a_Strings2)
1069 if (std::find(res.begin(), res.end(), item) == res.end())
1071 res.push_back(item);
1085 if (a_Strings.empty())
1092 res.append(a_Strings[0]);
1093 for (
auto itr = a_Strings.cbegin() + 1, end = a_Strings.cend(); itr != end; ++itr)
1095 res.push_back(a_Separator);
1108 a_Num = strtof(a_String.c_str(), &err);
1118 return std::all_of(a_String.cbegin(), a_String.cend(), isspace);