JSON parseNumber. added parsing of hex of the form 0x<digits>. might not be standard, but useful to me?
static Result parseNumber (String::CharPointerType& t, var& result, const bool isNegative)
{
String::CharPointerType oldT (t);
int64 intValue = t.getAndAdvance() - '0';
jassert (intValue >= 0 && intValue < 10);
bool hex = false;
if (!intValue)
{
// look for 0xHEX
if (*t == 'x')
{
t.getAndAdvance(); // skip 'x'
hex = true;
}
}
for (;;)
{
String::CharPointerType previousChar (t);
const juce_wchar c = t.getAndAdvance();
if (hex)
{
// parse hex
int d = -1;
if (c >= '0' && c <= '9')
d = c - '0';
else if (c >= 'a' && c <= 'f')
d = c - 'a' + 10;
else if (c >= 'A' && c <= 'F')
d = c - 'A' + 10;
if (d >= 0)
{
intValue = (intValue<<4) + d;
continue;
}
}
else
{
const int digit = ((int) c) - '0';
if (isPositiveAndBelow (digit, 10))
{
intValue = intValue * 10 + digit;
continue;
}
if (c == 'e' || c == 'E' || c == '.')
{
t = oldT;
const double asDouble = CharacterFunctions::readDoubleValue (t);
result = isNegative ? -asDouble : asDouble;
return Result::ok();
}
}
if (CharacterFunctions::isWhitespace (c)
|| c == ',' || c == '}' || c == ']' || c == 0)
{
t = previousChar;
break;
}
return createFail ("Syntax error in number", &oldT);
}
const int64 correctedValue = isNegative ? -intValue : intValue;
if ((intValue >> 31) != 0)
result = correctedValue;
else
result = (int) correctedValue;
return Result::ok();
}
