Clean up fixed parser a bit

This was SVN commit r7588.
This commit is contained in:
Ykkrosh 2010-05-27 23:13:07 +00:00
parent 5b51ecacac
commit 079ea554b9

View File

@ -26,8 +26,6 @@ CFixed_15_16 CFixed_15_16::FromString(const CStr8& s)
{
// Parse a superset of the xsd:decimal syntax: [-+]?\d*(\.\d*)?
// TODO: this could be made more precise
if (s.empty())
return CFixed_15_16::Zero();
@ -45,18 +43,22 @@ CFixed_15_16 CFixed_15_16::FromString(const CStr8& s)
neg = true;
}
// Integer part
while (true)
{
if (*c == '.')
// Integer part:
if (*c >= '0' && *c <= '9')
{
r = r * 10; // TODO: handle overflow gracefully, maybe
r += CFixed_15_16::FromInt(*c - '0');
++c;
}
else if (*c == '.')
{
++c;
// Fractional part
u32 frac = 0;
u32 div = 1;
while (true)
{
if (*c >= '0' && *c <= '9')
// Fractional part
while (*c >= '0' && *c <= '9')
{
frac *= 10;
frac += (*c - '0');
@ -64,26 +66,14 @@ CFixed_15_16 CFixed_15_16::FromString(const CStr8& s)
++c;
if (div >= 100000)
{
// any further digits will be too small to have any effect
// any further digits will be too small to have any major effect
break;
}
}
// too many digits or invalid character or end of string - add the fractional part and stop
r += CFixed_15_16(((u64)frac << 16) / div);
break;
}
}
else
{
// invalid character or end of string
r += CFixed_15_16(((u64)frac << 16) / div);
break;
}
}
break;
}
else if (*c >= '0' && *c <= '9')
{
r = r * 10; // TODO: handle overflow gracefully, maybe
r += CFixed_15_16::FromInt(*c - '0');
++c;
}
else
{
// invalid character or end of string