Fix parsing numbers to support sign

This commit is contained in:
Wojtek Figat
2024-10-04 15:58:27 +02:00
parent 6fecf1d58a
commit b9849e2b5c
2 changed files with 94 additions and 48 deletions

View File

@@ -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)

View File

@@ -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<typename CharType>
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<typename CharType>
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<typename T, typename U>
static bool Parse(const T* str, U* result)
// Parses text to unsigned integer value. Returns true if failed to convert the value.
template<typename CharType>
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<typename T, typename U>
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<typename T>
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<typename T>
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<typename T>
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<typename T>
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<typename T>
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<typename T>
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<typename T>
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<typename T>
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<typename T, typename U>
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);