diff --git a/Source/Engine/Core/Types/Variant.cpp b/Source/Engine/Core/Types/Variant.cpp index 221f6f660..95e815b81 100644 --- a/Source/Engine/Core/Types/Variant.cpp +++ b/Source/Engine/Core/Types/Variant.cpp @@ -3082,23 +3082,23 @@ Variant Variant::Parse(const StringView& text, const VariantType& type) result.AsBool = true; break; case VariantType::Int16: - StringUtils::Parse(text.Get(), &result.AsInt16); + StringUtils::Parse(text.Get(), text.Length(), &result.AsInt16); break; case VariantType::Uint16: - StringUtils::Parse(text.Get(), &result.AsUint16); + StringUtils::Parse(text.Get(), text.Length(), &result.AsUint16); break; case VariantType::Int: - StringUtils::Parse(text.Get(), &result.AsInt); + StringUtils::Parse(text.Get(), text.Length(), &result.AsInt); break; case VariantType::Uint: - StringUtils::Parse(text.Get(), &result.AsUint); + StringUtils::Parse(text.Get(), text.Length(), &result.AsUint); break; case VariantType::Int64: - StringUtils::Parse(text.Get(), &result.AsInt64); + StringUtils::Parse(text.Get(), text.Length(), &result.AsInt64); break; case VariantType::Uint64: case VariantType::Enum: - if (!StringUtils::Parse(text.Get(), &result.AsUint64)) + if (!StringUtils::Parse(text.Get(), text.Length(), &result.AsUint64)) { } else if (type.TypeName) diff --git a/Source/Engine/Platform/StringUtils.h b/Source/Engine/Platform/StringUtils.h index af82f9f8b..eb567b651 100644 --- a/Source/Engine/Platform/StringUtils.h +++ b/Source/Engine/Platform/StringUtils.h @@ -195,30 +195,6 @@ public: // Converts hexadecimal character into the value. static int32 HexDigit(Char c); - // Parses text to unsigned integer value. Returns true if failed to convert the value. - template - static bool ParseHex(const CharType* str, uint32* result) - { - uint32 sum = 0; - const CharType* p = str; - if (*p == '0' && *(p + 1) == 'x') - p += 2; - while (*p) - { - int32 c = *p - '0'; - if (c < 0 || c > 9) - { - c = ToLower(*p) - 'a' + 10; - if (c < 10 || c > 15) - return true; - } - sum = 16 * sum + c; - p++; - } - *result = sum; - return false; - } - // Parses text to unsigned integer value. Returns true if failed to convert the value. template static bool ParseHex(const CharType* str, int32 length, uint32* result) @@ -244,28 +220,18 @@ public: return false; } - // Parses text to scalar integer value. Returns true if failed to convert the value. - template - static bool Parse(const T* str, U* result) + // Parses text to unsigned integer value. Returns true if failed to convert the value. + template + static bool ParseHex(const CharType* str, uint32* result) { - U sum = 0; - const T* p = str; - while (*p) - { - int32 c = *p++ - 48; - if (c < 0 || c > 9) - return true; - sum = 10 * sum + c; - } - *result = sum; - return false; + return ParseHex(str, Length(str), result); } - // Parses text to scalar integer value. Returns true if failed to convert the value. - template - static bool Parse(const T* str, uint32 length, U* result) + // Parses text to the unsigned integer value. Returns true if failed to convert the value. + template + static bool Parse(const T* str, int32 length, uint64* result) { - U sum = 0; + int64 sum = 0; const T* p = str; while (length--) { @@ -277,6 +243,86 @@ public: *result = sum; return false; } + template + static bool Parse(const T* str, uint32 length, uint32* result) + { + uint64 tmp; + const bool b = Parse(str, length, &tmp); + *result = (uint32)tmp; + return b; + } + template + static bool Parse(const T* str, uint32 length, uint16* result) + { + uint64 tmp; + const bool b = Parse(str, length, &tmp); + *result = (uint16)tmp; + return b; + } + template + static bool Parse(const T* str, uint32 length, uint8* result) + { + uint64 tmp; + const bool b = Parse(str, length, &tmp); + *result = (uint8)tmp; + return b; + } + + // Parses text to the integer value. Returns true if failed to convert the value. + template + static bool Parse(const T* str, int32 length, int64* result) + { + int64 sum = 0; + const T* p = str; + bool negate = false; + while (length--) + { + int32 c = *p++ - 48; + if (c == -3) + { + negate = true; + continue; + } + if (c < 0 || c > 9) + return true; + sum = 10 * sum + c; + } + if (negate) + sum = -sum; + *result = sum; + return false; + } + template + static bool Parse(const T* str, uint32 length, int32* result) + { + int64 tmp; + const bool b = Parse(str, length, &tmp); + *result = (int32)tmp; + return b; + } + template + static bool Parse(const T* str, uint32 length, int16* result) + { + int64 tmp; + const bool b = Parse(str, length, &tmp); + *result = (int16)tmp; + return b; + } + template + static bool Parse(const T* str, uint32 length, int8* result) + { + int64 tmp; + const bool b = Parse(str, length, &tmp); + *result = (int8)tmp; + return b; + } + + // Parses text to scalar integer value. Returns true if failed to convert the value. + template + static bool Parse(const T* str, U* result) + { + return Parse(str, Length(str), result); + } // Parses text to the scalar value. Returns true if failed to convert the value. static bool Parse(const Char* str, float* result);