From 2cb9a5803e5123efcc0a08f7c3f52dd521917a34 Mon Sep 17 00:00:00 2001 From: dota17 <50514813+dota17@users.noreply.github.com> Date: Tue, 17 Sep 2019 02:25:22 +0800 Subject: [PATCH] reinforce readToken function and add simple tests (#1012) --- src/lib_json/json_reader.cpp | 8 ++++++++ src/test_lib_json/main.cpp | 11 +++++++---- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/lib_json/json_reader.cpp b/src/lib_json/json_reader.cpp index 8dad1d1..6a1c765 100644 --- a/src/lib_json/json_reader.cpp +++ b/src/lib_json/json_reader.cpp @@ -1227,6 +1227,14 @@ bool OurReader::readToken(Token& token) { ok = features_.allowSpecialFloats_ && match("nfinity", 7); } break; + case '+': + if (readNumber(true)) { + token.type_ = tokenNumber; + } else { + token.type_ = tokenPosInf; + ok = features_.allowSpecialFloats_ && match("nfinity", 7); + } + break; case 't': token.type_ = tokenTrue; ok = match("rue", 3); diff --git a/src/test_lib_json/main.cpp b/src/test_lib_json/main.cpp index cc5ebf2..6d3a022 100644 --- a/src/test_lib_json/main.cpp +++ b/src/test_lib_json/main.cpp @@ -2451,17 +2451,19 @@ JSONTEST_FIXTURE(CharReaderAllowSpecialFloatsTest, issue209) { Json::String errs; Json::CharReader* reader(b.newCharReader()); { - char const doc[] = "{\"a\":NaN,\"b\":Infinity,\"c\":-Infinity}"; + char const doc[] = "{\"a\":NaN,\"b\":Infinity,\"c\":-Infinity,\"d\":+Infinity}"; bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs); JSONTEST_ASSERT(ok); JSONTEST_ASSERT_STRING_EQUAL("", errs); - JSONTEST_ASSERT_EQUAL(3u, root.size()); + JSONTEST_ASSERT_EQUAL(4u, root.size()); double n = root["a"].asDouble(); JSONTEST_ASSERT(std::isnan(n)); JSONTEST_ASSERT_EQUAL(std::numeric_limits::infinity(), root.get("b", 0.0)); JSONTEST_ASSERT_EQUAL(-std::numeric_limits::infinity(), root.get("c", 0.0)); + JSONTEST_ASSERT_EQUAL(std::numeric_limits::infinity(), + root.get("d", 0.0)); } struct TestData { @@ -2485,7 +2487,8 @@ JSONTEST_FIXTURE(CharReaderAllowSpecialFloatsTest, issue209) { {__LINE__, false, "{\"a\":.Infinity}"}, // {__LINE__, false, "{\"a\":_Infinity}"}, // {__LINE__, false, "{\"a\":_nfinity}"}, // - {__LINE__, true, "{\"a\":-Infinity}"} // + {__LINE__, true, "{\"a\":-Infinity}"}, // + {__LINE__, true, "{\"a\":+Infinity}"} // }; for (const auto& td : test_data) { bool ok = reader->parse(&*td.in.begin(), &*td.in.begin() + td.in.size(), @@ -2499,7 +2502,7 @@ JSONTEST_FIXTURE(CharReaderAllowSpecialFloatsTest, issue209) { } { - char const doc[] = "{\"posInf\": Infinity, \"NegInf\": -Infinity}"; + char const doc[] = "{\"posInf\": +Infinity, \"NegInf\": -Infinity}"; bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs); JSONTEST_ASSERT(ok); JSONTEST_ASSERT_STRING_EQUAL("", errs);