From f2ecefb7ee9b9e6c5daac9f44fe40ebdccbb1c76 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Sun, 28 May 2023 21:16:29 +0200 Subject: [PATCH] Refactor `StringUtils` --- .../Engine/Platform/Base/StringUtilsBase.cpp | 127 +++--------------- Source/Engine/Platform/StringUtils.h | 102 +------------- 2 files changed, 21 insertions(+), 208 deletions(-) diff --git a/Source/Engine/Platform/Base/StringUtilsBase.cpp b/Source/Engine/Platform/Base/StringUtilsBase.cpp index c18d309a5..ef4e66b72 100644 --- a/Source/Engine/Platform/Base/StringUtilsBase.cpp +++ b/Source/Engine/Platform/Base/StringUtilsBase.cpp @@ -11,9 +11,9 @@ #include #endif -const char DirectorySeparatorChar = '\\'; -const char AltDirectorySeparatorChar = '/'; -const char VolumeSeparatorChar = ':'; +constexpr char DirectorySeparatorChar = '\\'; +constexpr char AltDirectorySeparatorChar = '/'; +constexpr char VolumeSeparatorChar = ':'; const Char* StringUtils::FindIgnoreCase(const Char* str, const Char* toFind) { @@ -378,20 +378,17 @@ void StringUtils::PathRemoveRelativeParts(String& path) path.Insert(0, TEXT("/")); } -const char DigitPairs[201] = { - "00010203040506070809" - "10111213141516171819" - "20212223242526272829" - "30313233343536373839" - "40414243444546474849" - "50515253545556575859" - "60616263646566676869" - "70717273747576777879" - "80818283848586878889" - "90919293949596979899" -}; - -#define STRING_UTILS_ITOSTR_BUFFER_SIZE 15 +int32 StringUtils::HexDigit(Char c) +{ + int32 result = 0; + if (c >= '0' && c <= '9') + result = c - '0'; + else if (c >= 'a' && c <= 'f') + result = c + 10 - 'a'; + else if (c >= 'A' && c <= 'F') + result = c + 10 - 'A'; + return result; +} bool StringUtils::Parse(const Char* str, float* result) { @@ -419,108 +416,22 @@ bool StringUtils::Parse(const char* str, float* result) String StringUtils::ToString(int32 value) { - char buf[STRING_UTILS_ITOSTR_BUFFER_SIZE]; - char* it = &buf[STRING_UTILS_ITOSTR_BUFFER_SIZE - 2]; - int32 div = value / 100; - if (value >= 0) - { - while (div) - { - Platform::MemoryCopy(it, &DigitPairs[2 * (value - div * 100)], 2); - value = div; - it -= 2; - div = value / 100; - } - Platform::MemoryCopy(it, &DigitPairs[2 * value], 2); - if (value < 10) - it++; - } - else - { - while (div) - { - Platform::MemoryCopy(it, &DigitPairs[-2 * (value - div * 100)], 2); - value = div; - it -= 2; - div = value / 100; - } - Platform::MemoryCopy(it, &DigitPairs[-2 * value], 2); - if (value <= -10) - it--; - *it = '-'; - } - return String(it, (int32)(&buf[STRING_UTILS_ITOSTR_BUFFER_SIZE] - it)); + return String::Format(TEXT("{}"), value); } String StringUtils::ToString(int64 value) { - char buf[STRING_UTILS_ITOSTR_BUFFER_SIZE]; - char* it = &buf[STRING_UTILS_ITOSTR_BUFFER_SIZE - 2]; - int64 div = value / 100; - if (value >= 0) - { - while (div) - { - Platform::MemoryCopy(it, &DigitPairs[2 * (value - div * 100)], 2); - value = div; - it -= 2; - div = value / 100; - } - Platform::MemoryCopy(it, &DigitPairs[2 * value], 2); - if (value < 10) - it++; - } - else - { - while (div) - { - Platform::MemoryCopy(it, &DigitPairs[-2 * (value - div * 100)], 2); - value = div; - it -= 2; - div = value / 100; - } - Platform::MemoryCopy(it, &DigitPairs[-2 * value], 2); - if (value <= -10) - it--; - *it = '-'; - } - return String(it, (int32)(&buf[STRING_UTILS_ITOSTR_BUFFER_SIZE] - it)); + return String::Format(TEXT("{}"), value); } String StringUtils::ToString(uint32 value) { - char buf[STRING_UTILS_ITOSTR_BUFFER_SIZE]; - char* it = &buf[STRING_UTILS_ITOSTR_BUFFER_SIZE - 2]; - int32 div = value / 100; - while (div) - { - Platform::MemoryCopy(it, &DigitPairs[2 * (value - div * 100)], 2); - value = div; - it -= 2; - div = value / 100; - } - Platform::MemoryCopy(it, &DigitPairs[2 * value], 2); - if (value < 10) - it++; - return String((char*)it, (int32)((char*)&buf[STRING_UTILS_ITOSTR_BUFFER_SIZE] - (char*)it)); + return String::Format(TEXT("{}"), value); } String StringUtils::ToString(uint64 value) { - char buf[STRING_UTILS_ITOSTR_BUFFER_SIZE]; - char* it = &buf[STRING_UTILS_ITOSTR_BUFFER_SIZE - 2]; - int64 div = value / 100; - while (div) - { - Platform::MemoryCopy(it, &DigitPairs[2 * (value - div * 100)], 2); - value = div; - it -= 2; - div = value / 100; - } - Platform::MemoryCopy(it, &DigitPairs[2 * value], 2); - if (value < 10) - it++; - return String((char*)it, (int32)((char*)&buf[STRING_UTILS_ITOSTR_BUFFER_SIZE] - (char*)it)); + return String::Format(TEXT("{}"), value); } String StringUtils::ToString(float value) @@ -544,5 +455,3 @@ String StringUtils::GetZZString(const Char* str) } return String(str, (int32)(end - str)); } - -#undef STRING_UTILS_ITOSTR_BUFFER_SIZE diff --git a/Source/Engine/Platform/StringUtils.h b/Source/Engine/Platform/StringUtils.h index ea982c2ee..04e644d2e 100644 --- a/Source/Engine/Platform/StringUtils.h +++ b/Source/Engine/Platform/StringUtils.h @@ -21,12 +21,11 @@ enum class StringSearchCase }; /// -/// The string operations utilities collection. +/// The string operations utilities. /// class FLAXENGINE_API StringUtils { public: - /// /// Calculates the hash code for input string. /// @@ -65,7 +64,6 @@ public: } public: - // Returns true if character is uppercase static bool IsUpper(char c); @@ -92,7 +90,6 @@ public: static char ToLower(char c); public: - // Returns true if character is uppercase static bool IsUpper(Char c); @@ -119,7 +116,6 @@ public: static Char ToLower(Char c); public: - // Compare two strings with case sensitive. Strings must not be null. static int32 Compare(const Char* str1, const Char* str2); @@ -145,7 +141,6 @@ public: static int32 CompareIgnoreCase(const char* str1, const char* str2, int32 maxCount); public: - // Get string length. Returns 0 if str is null. static int32 Length(const Char* str); @@ -183,7 +178,6 @@ public: static const char* FindIgnoreCase(const char* str, const char* toFind); public: - // Converts characters from ANSI to UTF-16 static void ConvertANSI2UTF16(const char* from, Char* to, int32 len); @@ -203,7 +197,6 @@ public: static char* ConvertUTF162UTF8(const Char* from, int32 fromLength, int32& toLength); public: - // Returns the directory name of the specified path string // @param path The path string from which to obtain the directory name // @returns Directory name @@ -224,95 +217,8 @@ public: static void PathRemoveRelativeParts(String& path); public: - - /// - /// Convert integer value to string - /// - /// Value to convert - /// Base (8,10,16) - /// Input buffer - /// Result string length - template - static void itoa(int32 value, int32 base, CharType* buffer, int32& length) - { - // Allocate buffer - bool isNegative = false; - CharType* pos = buffer; - CharType* pos1 = buffer; - length = 0; - - // Validate input base - if (base < 8 || base > 16) - { - *pos = '\0'; - return; - } - - // Special case for zero - if (value == 0) - { - length++; - *pos++ = '0'; - *pos = '\0'; - return; - } - - // Check if value is negative - if (value < 0) - { - isNegative = true; - value = -value; - } - - // Convert. If base is power of two (2,4,8,16..) - // we could use binary and operation and shift offset instead of division - while (value) - { - length++; - int32 reminder = value % base; - *pos++ = reminder + (reminder > 9 ? 'a' - 10 : '0'); - value /= base; - } - - // Apply negative sign - if (isNegative) - *pos++ = '-'; - - // Add null terminator char - *pos-- = 0; - - // Reverse the buffer - while (pos1 < pos) - { - CharType c = *pos; - *pos-- = *pos1; - *pos1++ = c; - } - } - - static int32 HexDigit(Char c) - { - int32 result = 0; - - if (c >= '0' && c <= '9') - { - result = c - '0'; - } - else if (c >= 'a' && c <= 'f') - { - result = c + 10 - 'a'; - } - else if (c >= 'A' && c <= 'F') - { - result = c + 10 - 'A'; - } - else - { - result = 0; - } - - return result; - } + // Converts hexadecimal character into the value. + static int32 HexDigit(Char c); // Parse text to unsigned integer value // @param str String to parse @@ -431,7 +337,6 @@ public: static bool Parse(const char* str, float* result); public: - static String ToString(int32 value); static String ToString(int64 value); static String ToString(uint32 value); @@ -440,7 +345,6 @@ public: static String ToString(double value); public: - // Returns the String to double null-terminated string // @param str Double null-terminated string // @return Double null-terminated String