8 #include "fmt/printf.h" 12 #pragma comment(lib, "ws2_32.lib") 58 str = fmt::sprintf(format, args);
69 return fmt::sprintf(format, args);
81 while ((cutAt = str.find_first_of(delim, Prev)) != str.npos)
83 results.push_back(str.substr(Prev, cutAt - Prev));
86 if (Prev < str.length())
88 results.push_back(str.substr(Prev));
103 size_t cutAtQuote = 0;
105 while ((cutAt = str.find_first_of(delim, Prev)) != str.npos)
114 AString current = str.substr(Prev, cutAt - Prev);
115 if ((current.front() ==
'"') || (current.front() ==
'\''))
118 cutAtQuote = str.find_first_of(current.front(), Prev);
119 if (cutAtQuote != str.npos)
121 current = str.substr(Prev, cutAtQuote - Prev);
122 cutAt = cutAtQuote + 1;
126 results.push_back(std::move(current));
130 if (Prev < str.length())
132 AString current = str.substr(Prev);
136 (current.length() >= 2) &&
137 ((current.front() ==
'"') || (current.front() ==
'\'')) &&
138 (current.front() == current.back())
141 current = current.substr(1, current.length() - 2);
144 results.push_back(current);
156 if (a_Strings.empty())
162 const auto DelimSize = a_Delimeter.size();
163 size_t ResultSize = a_Strings[0].size();
164 std::for_each(a_Strings.begin() + 1, a_Strings.end(),
167 ResultSize += DelimSize;
168 ResultSize += a_String.size();
174 Result.reserve(ResultSize);
175 Result.append(a_Strings[0]);
176 std::for_each(a_Strings.begin() + 1, a_Strings.end(),
179 Result += a_Delimeter;
195 while ((cutAt = str.find_first_of(delim, Prev)) != str.npos)
197 results.push_back(
TrimString(str.substr(Prev, cutAt - Prev)));
200 if (Prev < str.length())
202 results.push_back(
TrimString(str.substr(Prev)));
213 size_t len = str.length();
217 if (static_cast<unsigned char>(str[start]) > 32)
231 if (static_cast<unsigned char>(str[end]) > 32)
238 return str.substr(start, end - start + 1);
247 std::transform(s.begin(), s.end(), s.begin(), ::tolower);
257 std::transform(s.begin(), s.end(), s.begin(), ::toupper);
268 res.resize(s.size());
269 std::transform(s.begin(), s.end(), res.begin(), ::tolower);
280 res.resize(s.size());
281 std::transform(s.begin(), s.end(), res.begin(), ::toupper);
292 return _stricmp(s1.c_str(), s2.c_str());
294 return strcasecmp(s1.c_str(), s2.c_str());
295 #endif // else _MSC_VER 304 size_t MatchedLetters = 0;
305 size_t s1Length = s1.length();
307 if (s1Length > s2.length())
313 for (
size_t i = 0; i < s1Length; i++)
315 char c1 =
static_cast<char>(toupper(s1[i]));
316 char c2 =
static_cast<char>(toupper(s2[i]));
326 return MatchedLetters;
341 size_t pos1 = iHayStack.find(iNeedle);
342 while (pos1 != AString::npos)
344 iHayStack.replace( pos1, iNeedle.size(), iReplaceWith);
345 pos1 = iHayStack.find(iNeedle, pos1 + iReplaceWith.size());
356 a_UTF8.reserve(3 * a_NumShorts / 2);
357 for (
size_t i = 0; i < a_NumShorts; i++)
370 if (a_UnicodeChar < 0x80)
372 return AString{
static_cast<char>(a_UnicodeChar)};
374 else if (a_UnicodeChar < 0x800)
378 static_cast<char>(192 + a_UnicodeChar / 64),
379 static_cast<char>(128 + a_UnicodeChar % 64),
382 else if (a_UnicodeChar - 0xd800 < 0x800)
387 else if (a_UnicodeChar < 0x10000)
391 static_cast<char>(224 + a_UnicodeChar / 4096),
392 static_cast<char>(128 + (a_UnicodeChar / 64) % 64),
393 static_cast<char>(128 + a_UnicodeChar % 64)
396 else if (a_UnicodeChar < 0x110000)
400 static_cast<char>(240 + a_UnicodeChar / 262144),
401 static_cast<char>(128 + (a_UnicodeChar / 4096) % 64),
402 static_cast<char>(128 + (a_UnicodeChar / 64) % 64),
403 static_cast<char>(128 + a_UnicodeChar % 64),
418 #pragma GCC diagnostic push 419 #pragma GCC diagnostic ignored "-Wimplicit-fallthrough" 451 #define UNI_MAX_BMP 0x0000FFFF 452 #define UNI_MAX_UTF16 0x0010FFFF 453 #define UNI_SUR_HIGH_START 0xD800 454 #define UNI_SUR_LOW_START 0xDC00 455 #define UNI_SUR_LOW_END 0xDFFF 463 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,
464 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,
465 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,
466 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,
467 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,
468 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,
469 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,
470 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
479 0x00000000UL, 0x00003080UL, 0x000E2080UL,
480 0x03C82080UL, 0xFA082080UL, 0x82082080UL
490 const unsigned char * srcptr = source + length;
493 default:
return false;
495 case 4:
if (((a = (*--srcptr)) < 0x80) || (a > 0xbf))
return false;
496 case 3:
if (((a = (*--srcptr)) < 0x80) || (a > 0xbf))
return false;
499 if ((a = (*--srcptr)) > 0xbf)
506 case 0xe0:
if (a < 0xa0)
return false;
break;
507 case 0xed:
if (a > 0x9f)
return false;
break;
508 case 0xf0:
if (a < 0x90)
return false;
break;
509 case 0xf4:
if (a > 0x8f)
return false;
break;
510 default:
if (a < 0x80)
return false;
513 case 1:
if ((*source >= 0x80) && (*source < 0xc2))
return false;
528 std::u16string UTF16;
529 UTF16.reserve(a_UTF8.size() * 2);
531 const unsigned char * source =
reinterpret_cast<const unsigned char *
>(a_UTF8.data());
532 const unsigned char * sourceEnd = source + a_UTF8.size();
533 const int halfShift = 10;
534 const unsigned int halfBase = 0x0010000UL;
535 const unsigned int halfMask = 0x3ffUL;
537 while (source < sourceEnd)
541 if (source + extraBytesToRead >= sourceEnd)
552 switch (extraBytesToRead)
554 case 5: ch += *source++; ch <<= 6;
555 case 4: ch += *source++; ch <<= 6;
556 case 3: ch += *source++; ch <<= 6;
557 case 2: ch += *source++; ch <<= 6;
558 case 1: ch += *source++; ch <<= 6;
559 case 0: ch += *source++;
571 unsigned short v = htons(static_cast<unsigned short>(ch));
572 UTF16.push_back(static_cast<char16_t>(v));
577 unsigned short v = htons(
' ');
578 UTF16.push_back(static_cast<char16_t>(v));
586 UTF16.push_back(static_cast<char16_t>(v1));
587 UTF16.push_back(static_cast<char16_t>(v2));
618 #pragma GCC diagnostic pop 625 #define HEX(x) static_cast<char>((x) > 9 ? (x) + 'A' - 10 : (x) + '0') 633 fmt::MemoryWriter Output;
637 fmt::MemoryWriter Hex, Chars;
642 const size_t NumLines = a_Size / a_BytesPerLine + (a_Size % a_BytesPerLine != 0);
643 const size_t CharsPerLine = 14 + 4 * a_BytesPerLine;
644 Output.buffer().reserve(NumLines * CharsPerLine);
647 for (
size_t i = 0; i < a_Size; i += a_BytesPerLine)
649 size_t k = std::min(a_Size - i, a_BytesPerLine);
650 for (
size_t j = 0; j < k; j++)
652 Byte c = (
static_cast<const Byte *
>(a_Data))[i + j];
653 Hex << HEX(c >> 4) <<
HEX(c & 0xf) <<
' ';
654 Chars << ((c >=
' ') ? static_cast<char>(c) :
'.');
658 Output.write(
"{0:08x}: {1:{2}} {3}\n", i, Hex.c_str(), a_BytesPerLine * 3, Chars.c_str());
662 a_Out.append(Output.data(), Output.size());
673 size_t len = a_Message.size();
675 EscapedMsg.reserve(len);
676 for (
size_t i = 0; i < len; i++)
678 char ch = a_Message[i];
687 EscapedMsg.append(a_Message, last, i - last);
689 EscapedMsg.push_back(
'\\');
690 EscapedMsg.push_back(ch);
698 EscapedMsg.append(a_Message, last, len - last);
713 idx = res.find(
"\xc2\xa7", idx);
714 if (idx == AString::npos)
729 auto len = a_Text.size();
731 for (
size_t i = 0; i < len; i++)
733 if (a_Text[i] ==
'+')
738 if (a_Text[i] !=
'%')
740 res.push_back(a_Text[i]);
746 return std::make_pair(
false,
AString());
748 if ((a_Text[i + 1] ==
'u') || (a_Text[i + 1] ==
'U'))
753 return std::make_pair(
false,
AString());
755 if (a_Text[i + 2] !=
'0')
757 return std::make_pair(
false,
AString());
759 unsigned v1 =
HexToDec(a_Text[i + 3]);
760 unsigned v2 =
HexToDec(a_Text[i + 4]);
761 unsigned v3 =
HexToDec(a_Text[i + 5]);
762 unsigned v4 =
HexToDec(a_Text[i + 6]);
763 if ((v1 == 0xff) || (v2 == 0xff) || (v4 == 0xff) || (v3 == 0xff))
766 return std::make_pair(
false,
AString());
776 return std::make_pair(
false,
AString());
780 if ((v1 == 0xff) || (v2 == 0xff))
783 return std::make_pair(
false,
AString());
785 res.push_back(static_cast<char>((v1 << 4) | v2));
789 return std::make_pair(
true, res);
799 auto len = a_Text.size();
801 static const char HEX[] =
"0123456789abcdef";
802 for (
size_t i = 0; i < len; ++i)
804 if (isalnum(a_Text[i]))
806 res.push_back(a_Text[i]);
808 else if (a_Text[i] ==
' ')
815 res.push_back(HEX[static_cast<unsigned char>(a_Text[i]) >> 4]);
816 res.push_back(HEX[static_cast<unsigned char>(a_Text[i]) & 0x0f]);
829 std::replace(res.begin(), res.end(), a_From, a_To);
840 if ((c >=
'A') && (c <=
'Z'))
844 if ((c >=
'a') && (c <=
'z'))
848 if ((c >=
'0') && (c <=
'9'))
874 size_t i, len = a_Base64String.size();
877 res.resize((len * 4) / 3 + 5, 0);
878 for (o = 0, i = 0; i < len; i++)
885 case 0: res[o >> 3] |= (c << 2);
break;
886 case 6: res[o >> 3] |= (c >> 4); res[(o >> 3) + 1] |= (c << 4);
break;
887 case 4: res[o >> 3] |= (c >> 2); res[(o >> 3) + 1] |= (c << 6);
break;
888 case 2: res[o >> 3] |= c;
break;
909 static const char BASE64[64] =
911 'A',
'B',
'C',
'D',
'E',
'F',
'G',
'H',
'I',
'J',
'K',
'L',
'M',
'N',
'O',
'P',
912 'Q',
'R',
'S',
'T',
'U',
'V',
'W',
'X',
'Y',
'Z',
'a',
'b',
'c',
'd',
'e',
'f',
913 'g',
'h',
'i',
'j',
'k',
'l',
'm',
'n',
'o',
'p',
'q',
'r',
's',
't',
'u',
'v',
914 'w',
'x',
'y',
'z',
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'+',
'/' 918 output.resize(((a_Input.size() + 2) / 3) * 4);
920 size_t output_index = 0;
921 size_t size_full24 = (a_Input.size() / 3) * 3;
923 for (
size_t i = 0; i < size_full24; i += 3)
925 output[output_index++] = BASE64[
static_cast<unsigned char>(a_Input[i]) >> 2];
926 output[output_index++] = BASE64[(
static_cast<unsigned char>(a_Input[i]) << 4 | static_cast<unsigned char>(a_Input[i + 1]) >> 4) & 63];
927 output[output_index++] = BASE64[(
static_cast<unsigned char>(a_Input[i + 1]) << 2 | static_cast<unsigned char>(a_Input[i + 2]) >> 6) & 63];
928 output[output_index++] = BASE64[
static_cast<unsigned char>(a_Input[i + 2]) & 63];
931 if (size_full24 < a_Input.size())
933 output[output_index++] = BASE64[
static_cast<unsigned char>(a_Input[size_full24]) >> 2];
934 if (size_full24 + 1 == a_Input.size())
936 output[output_index++] = BASE64[(
static_cast<unsigned char>(a_Input[size_full24]) << 4) & 63];
937 output[output_index++] =
'=';
941 output[output_index++] = BASE64[(
static_cast<unsigned char>(a_Input[size_full24]) << 4 | static_cast<unsigned char>(a_Input[size_full24 + 1]) >> 4) & 63];
942 output[output_index++] = BASE64[(
static_cast<unsigned char>(a_Input[size_full24 + 1]) << 2) & 63];
945 output[output_index++] =
'=';
947 ASSERT(output_index == output.size());
958 const Byte * Bytes =
reinterpret_cast<const Byte *
>(a_Mem);
959 return static_cast<short>((Bytes[0] << 8) | Bytes[1]);
968 const Byte * Bytes =
reinterpret_cast<const Byte *
>(a_Mem);
969 return static_cast<unsigned short>((Bytes[0] << 8) | Bytes[1]);
978 const Byte * Bytes =
reinterpret_cast<const Byte *
>(a_Mem);
979 return (Bytes[0] << 24) | (Bytes[1] << 16) | (Bytes[2] << 8) | Bytes[3];
988 a_Mem[0] = a_Value >> 24;
989 a_Mem[1] =
static_cast<char>((a_Value >> 16) & 0xff);
990 a_Mem[2] =
static_cast<char>((a_Value >> 8) & 0xff);
991 a_Mem[3] =
static_cast<char>(a_Value & 0xff);
1001 size_t size = a_Strings.size();
1004 for (
size_t i = 0; i < size; i++)
1006 if (a_Strings[i] == 0)
1008 a_Output.push_back(a_Strings.substr(start, i - start));
1015 a_Output.push_back(a_Strings.substr(start, size - start));
1032 for (
auto item : a_Strings2)
1034 if (std::find(res.begin(), res.end(), item) == res.end())
1036 res.push_back(item);
1050 if (a_Strings.empty())
1057 res.append(a_Strings[0]);
1058 for (
auto itr = a_Strings.cbegin() + 1, end = a_Strings.cend(); itr != end; ++itr)
1060 res.push_back(a_Separator);
1073 a_Num = strtof(a_String.c_str(), &err);
1087 return std::all_of(a_String.cbegin(), a_String.cend(), isspace);
static const unsigned int offsetsFromUTF8[6]
bool StringToFloat(const AString &a_String, float &a_Num)
Converts a string into a float.
AString & RawBEToUTF8(const char *a_RawData, size_t a_NumShorts, AString &a_UTF8)
Converts a stream of BE shorts into UTF-8 string; returns a_UTF8.
AString StripColorCodes(const AString &a_Message)
Removes all control codes used by MC for colors and styles.
AString StringsConcat(const AStringVector &a_Strings, char a_Separator)
Concatenates the specified strings into a single string, separated by the specified separator charact...
AString StringJoin(const AStringVector &a_Strings, const AString &a_Delimeter)
Join a list of strings with the given delimiter between entries.
#define UNI_SUR_LOW_START
AStringVector StringSplitWithQuotes(const AString &str, const AString &delim)
Split the string at any of the listed delimiters.
AString & InPlaceUppercase(AString &s)
In-place string conversion to uppercase.
unsigned short GetBEUShort(const char *a_Mem)
Reads two bytes from the specified memory location and interprets them as BigEndian unsigned short...
bool IsOnlyWhitespace(const AString &a_String)
Returns true if only whitespace characters are present in the string.
void ReplaceString(AString &iHayStack, const AString &iNeedle, const AString &iReplaceWith)
Replaces each occurence of iNeedle in iHayStack with iReplaceWith.
AString EscapeString(const AString &a_Message)
Returns a copy of a_Message with all quotes and backslashes escaped by a backslash.
AString & InPlaceLowercase(AString &s)
In-place string conversion to lowercase.
static bool isLegalUTF8(const unsigned char *source, int length)
std::vector< AString > AStringVector
#define UNI_SUR_HIGH_START
void SetBEInt(char *a_Mem, Int32 a_Value)
Writes four bytes to the specified memory location so that they interpret as BigEndian int...
AString ReplaceAllCharOccurrences(const AString &a_String, char a_From, char a_To)
Replaces all occurrences of char a_From inside a_String with char a_To.
AString Base64Decode(const AString &a_Base64String)
Decodes a Base64-encoded string into the raw data.
std::pair< bool, AString > URLDecode(const AString &a_Text)
URL-Decodes the given string.
int NoCaseCompare(const AString &s1, const AString &s2)
Case-insensitive string comparison.
AStringVector StringSplitAndTrim(const AString &str, const AString &delim)
Split the string at any of the listed delimiters and trim each value.
short GetBEShort(const char *a_Mem)
Reads two bytes from the specified memory location and interprets them as BigEndian short...
AString & Printf(AString &str, const char *format, fmt::ArgList args)
Output the formatted text into the string.
AString UnicodeCharToUtf8(unsigned a_UnicodeChar)
Converts a unicode character to its UTF8 representation.
AString TrimString(const AString &str)
Trims whitespace at both ends of the string.
static unsigned char HexToDec(char a_HexChar)
Returns the value of the single hex digit.
AString URLEncode(const AString &a_Text)
URL-encodes the given string.
static const Byte trailingBytesForUTF8[256]
std::u16string UTF8ToRawBEUTF16(const AString &a_UTF8)
Converts a UTF-8 string into a UTF-16 BE string.
AString StrToUpper(const AString &s)
Returns an upper-cased copy of the string.
AString & CreateHexDump(AString &a_Out, const void *a_Data, size_t a_Size, size_t a_BytesPerLine)
format binary data this way: 00001234: 31 32 33 34 35 36 37 38 39 30 61 62 63 64 65 66 1234567890abcd...
AStringVector StringSplit(const AString &str, const AString &delim)
Split the string at any of the listed delimiters.
static int UnBase64(char c)
Converts one Hex character in a Base64 encoding into the data value.
size_t RateCompareString(const AString &s1, const AString &s2)
Case-insensitive string comparison that returns a rating of equal-ness between [0 - s1...
AStringVector MergeStringVectors(const AStringVector &a_Strings1, const AStringVector &a_Strings2)
Merges the two vectors of strings, removing duplicate entries from the second vector.
AString StrToLower(const AString &s)
Returns a lower-cased copy of the string.
int GetBEInt(const char *a_Mem)
Reads four bytes from the specified memory location and interprets them as BigEndian int...
AString Base64Encode(const AString &a_Input)
Encodes a string into Base64.
bool SplitZeroTerminatedStrings(const AString &a_Strings, AStringVector &a_Output)
Splits a string that has embedded \0 characters, on those characters.