Parser now correctly reads floats in scientific notation.

Change-Id: I7abb14a4b6c596674d6aff2b9de6e63603c0d2dc
Tested: on Windows and Linux.
parent 2208de06
...@@ -200,6 +200,13 @@ void Parser::Next() { ...@@ -200,6 +200,13 @@ void Parser::Next() {
if (*cursor_ == '.') { if (*cursor_ == '.') {
cursor_++; cursor_++;
while (isdigit(static_cast<unsigned char>(*cursor_))) cursor_++; while (isdigit(static_cast<unsigned char>(*cursor_))) cursor_++;
// See if this float has a scientific notation suffix. Both JSON
// and C++ (through strtod() we use) have the same format:
if (*cursor_ == 'e' || *cursor_ == 'E') {
cursor_++;
if (*cursor_ == '+' || *cursor_ == '-') cursor_++;
while (isdigit(static_cast<unsigned char>(*cursor_))) cursor_++;
}
token_ = kTokenFloatConstant; token_ = kTokenFloatConstant;
} else { } else {
token_ = kTokenIntegerConstant; token_ = kTokenIntegerConstant;
......
...@@ -481,6 +481,21 @@ void ErrorTest() { ...@@ -481,6 +481,21 @@ void ErrorTest() {
TestError("union Z { X } struct X { Y:int; }", "only tables"); TestError("union Z { X } struct X { Y:int; }", "only tables");
} }
// Additional parser testing not covered elsewhere.
void TokenTest() {
flatbuffers::Parser parser;
// Simple schema.
TEST_EQ(parser.Parse("table X { Y:float; } root_type X;"), true);
// Test scientific notation numbers.
TEST_EQ(parser.Parse("{ Y:0.0314159e+2 }"), true);
auto root = flatbuffers::GetRoot<float>(parser.builder_.GetBufferPointer());
// root will point to the table, which is a 32bit vtable offset followed
// by a float:
TEST_EQ(fabs(root[1] - 3.14159) < 0.001, true);
}
int main(int /*argc*/, const char * /*argv*/[]) { int main(int /*argc*/, const char * /*argv*/[]) {
// Run our various test suites: // Run our various test suites:
...@@ -495,6 +510,7 @@ int main(int /*argc*/, const char * /*argv*/[]) { ...@@ -495,6 +510,7 @@ int main(int /*argc*/, const char * /*argv*/[]) {
FuzzTest2(); FuzzTest2();
ErrorTest(); ErrorTest();
TokenTest();
if (!testing_fails) { if (!testing_fails) {
TEST_OUTPUT_LINE("ALL TESTS PASSED"); TEST_OUTPUT_LINE("ALL TESTS PASSED");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment