Fix parsing numbers to support sign
This commit is contained in:
@@ -3082,23 +3082,23 @@ Variant Variant::Parse(const StringView& text, const VariantType& type)
|
|||||||
result.AsBool = true;
|
result.AsBool = true;
|
||||||
break;
|
break;
|
||||||
case VariantType::Int16:
|
case VariantType::Int16:
|
||||||
StringUtils::Parse(text.Get(), &result.AsInt16);
|
StringUtils::Parse(text.Get(), text.Length(), &result.AsInt16);
|
||||||
break;
|
break;
|
||||||
case VariantType::Uint16:
|
case VariantType::Uint16:
|
||||||
StringUtils::Parse(text.Get(), &result.AsUint16);
|
StringUtils::Parse(text.Get(), text.Length(), &result.AsUint16);
|
||||||
break;
|
break;
|
||||||
case VariantType::Int:
|
case VariantType::Int:
|
||||||
StringUtils::Parse(text.Get(), &result.AsInt);
|
StringUtils::Parse(text.Get(), text.Length(), &result.AsInt);
|
||||||
break;
|
break;
|
||||||
case VariantType::Uint:
|
case VariantType::Uint:
|
||||||
StringUtils::Parse(text.Get(), &result.AsUint);
|
StringUtils::Parse(text.Get(), text.Length(), &result.AsUint);
|
||||||
break;
|
break;
|
||||||
case VariantType::Int64:
|
case VariantType::Int64:
|
||||||
StringUtils::Parse(text.Get(), &result.AsInt64);
|
StringUtils::Parse(text.Get(), text.Length(), &result.AsInt64);
|
||||||
break;
|
break;
|
||||||
case VariantType::Uint64:
|
case VariantType::Uint64:
|
||||||
case VariantType::Enum:
|
case VariantType::Enum:
|
||||||
if (!StringUtils::Parse(text.Get(), &result.AsUint64))
|
if (!StringUtils::Parse(text.Get(), text.Length(), &result.AsUint64))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
else if (type.TypeName)
|
else if (type.TypeName)
|
||||||
|
|||||||
@@ -195,30 +195,6 @@ public:
|
|||||||
// Converts hexadecimal character into the value.
|
// Converts hexadecimal character into the value.
|
||||||
static int32 HexDigit(Char c);
|
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.
|
// Parses text to unsigned integer value. Returns true if failed to convert the value.
|
||||||
template<typename CharType>
|
template<typename CharType>
|
||||||
static bool ParseHex(const CharType* str, int32 length, uint32* result)
|
static bool ParseHex(const CharType* str, int32 length, uint32* result)
|
||||||
@@ -244,28 +220,18 @@ public:
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parses text to scalar integer value. Returns true if failed to convert the value.
|
// Parses text to unsigned integer value. Returns true if failed to convert the value.
|
||||||
template<typename T, typename U>
|
template<typename CharType>
|
||||||
static bool Parse(const T* str, U* result)
|
static bool ParseHex(const CharType* str, uint32* result)
|
||||||
{
|
{
|
||||||
U sum = 0;
|
return ParseHex(str, Length(str), result);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parses text to scalar integer value. Returns true if failed to convert the value.
|
// Parses text to the unsigned integer value. Returns true if failed to convert the value.
|
||||||
template<typename T, typename U>
|
template<typename T>
|
||||||
static bool Parse(const T* str, uint32 length, U* result)
|
static bool Parse(const T* str, int32 length, uint64* result)
|
||||||
{
|
{
|
||||||
U sum = 0;
|
int64 sum = 0;
|
||||||
const T* p = str;
|
const T* p = str;
|
||||||
while (length--)
|
while (length--)
|
||||||
{
|
{
|
||||||
@@ -277,6 +243,86 @@ public:
|
|||||||
*result = sum;
|
*result = sum;
|
||||||
return false;
|
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.
|
// Parses text to the scalar value. Returns true if failed to convert the value.
|
||||||
static bool Parse(const Char* str, float* result);
|
static bool Parse(const Char* str, float* result);
|
||||||
|
|||||||
Reference in New Issue
Block a user