mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-23 07:17:30 +02:00
Simplified the implementation of parseNumber()
This commit is contained in:
@ -4,6 +4,7 @@
|
||||
|
||||
add_executable(NumbersTests
|
||||
parseFloat.cpp
|
||||
parseDouble.cpp
|
||||
parseInteger.cpp
|
||||
parseNumber.cpp
|
||||
)
|
||||
|
97
extras/tests/Numbers/parseDouble.cpp
Normal file
97
extras/tests/Numbers/parseDouble.cpp
Normal file
@ -0,0 +1,97 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2020
|
||||
// MIT License
|
||||
|
||||
#define ARDUINOJSON_USE_DOUBLE 1
|
||||
#define ARDUINOJSON_ENABLE_NAN 1
|
||||
#define ARDUINOJSON_ENABLE_INFINITY 1
|
||||
|
||||
#include <ArduinoJson/Numbers/parseNumber.hpp>
|
||||
#include <ArduinoJson/Variant/VariantImpl.hpp>
|
||||
#include <catch.hpp>
|
||||
|
||||
using namespace ARDUINOJSON_NAMESPACE;
|
||||
|
||||
void checkDouble(const char* input, double expected) {
|
||||
CAPTURE(input);
|
||||
REQUIRE(parseNumber<double>(input) == Approx(expected));
|
||||
}
|
||||
|
||||
void checkDoubleNaN(const char* input) {
|
||||
CAPTURE(input);
|
||||
double result = parseNumber<double>(input);
|
||||
REQUIRE(result != result);
|
||||
}
|
||||
|
||||
void checkDoubleInf(const char* input, bool negative) {
|
||||
CAPTURE(input);
|
||||
double x = parseNumber<double>(input);
|
||||
if (negative)
|
||||
REQUIRE(x < 0);
|
||||
else
|
||||
REQUIRE(x > 0);
|
||||
REQUIRE(x == x); // not a NaN
|
||||
REQUIRE(x * 2 == x); // a property of infinity
|
||||
}
|
||||
|
||||
TEST_CASE("parseNumber<double>()") {
|
||||
SECTION("Short_NoExponent") {
|
||||
checkDouble("3.14", 3.14);
|
||||
checkDouble("-3.14", -3.14);
|
||||
checkDouble("+3.14", +3.14);
|
||||
}
|
||||
|
||||
SECTION("Short_NoDot") {
|
||||
checkDouble("1E+308", 1E+308);
|
||||
checkDouble("-1E+308", -1E+308);
|
||||
checkDouble("+1E-308", +1E-308);
|
||||
checkDouble("+1e+308", +1e+308);
|
||||
checkDouble("-1e-308", -1e-308);
|
||||
}
|
||||
|
||||
SECTION("Max") {
|
||||
checkDouble(".017976931348623147e+310", 1.7976931348623147e+308);
|
||||
checkDouble(".17976931348623147e+309", 1.7976931348623147e+308);
|
||||
checkDouble("1.7976931348623147e+308", 1.7976931348623147e+308);
|
||||
checkDouble("17.976931348623147e+307", 1.7976931348623147e+308);
|
||||
checkDouble("179.76931348623147e+306", 1.7976931348623147e+308);
|
||||
}
|
||||
|
||||
SECTION("Min") {
|
||||
checkDouble(".022250738585072014e-306", 2.2250738585072014e-308);
|
||||
checkDouble(".22250738585072014e-307", 2.2250738585072014e-308);
|
||||
checkDouble("2.2250738585072014e-308", 2.2250738585072014e-308);
|
||||
checkDouble("22.250738585072014e-309", 2.2250738585072014e-308);
|
||||
checkDouble("222.50738585072014e-310", 2.2250738585072014e-308);
|
||||
}
|
||||
|
||||
SECTION("VeryLong") {
|
||||
checkDouble("0.00000000000000000000000000000001", 1e-32);
|
||||
checkDouble("100000000000000000000000000000000.0", 1e+32);
|
||||
checkDouble(
|
||||
"100000000000000000000000000000000.00000000000000000000000000000",
|
||||
1e+32);
|
||||
}
|
||||
|
||||
SECTION("MantissaTooLongToFit") {
|
||||
checkDouble("0.179769313486231571111111111111", 0.17976931348623157);
|
||||
checkDouble("17976931348623157.11111111111111", 17976931348623157.0);
|
||||
checkDouble("1797693.134862315711111111111111", 1797693.1348623157);
|
||||
|
||||
checkDouble("-0.179769313486231571111111111111", -0.17976931348623157);
|
||||
checkDouble("-17976931348623157.11111111111111", -17976931348623157.0);
|
||||
checkDouble("-1797693.134862315711111111111111", -1797693.1348623157);
|
||||
}
|
||||
|
||||
SECTION("ExponentTooBig") {
|
||||
checkDoubleInf("1e309", false);
|
||||
checkDoubleInf("-1e309", true);
|
||||
checkDoubleInf("1e65535", false);
|
||||
checkDouble("1e-65535", 0.0);
|
||||
}
|
||||
|
||||
SECTION("NaN") {
|
||||
checkDoubleNaN("NaN");
|
||||
checkDoubleNaN("nan");
|
||||
}
|
||||
}
|
@ -6,28 +6,26 @@
|
||||
#define ARDUINOJSON_ENABLE_NAN 1
|
||||
#define ARDUINOJSON_ENABLE_INFINITY 1
|
||||
|
||||
#include <ArduinoJson/Numbers/parseFloat.hpp>
|
||||
#include <ArduinoJson/Numbers/parseNumber.hpp>
|
||||
#include <ArduinoJson/Variant/VariantImpl.hpp>
|
||||
#include <catch.hpp>
|
||||
|
||||
using namespace ARDUINOJSON_NAMESPACE;
|
||||
|
||||
template <typename T>
|
||||
void checkFloat(const char* input, T expected) {
|
||||
void checkFloat(const char* input, float expected) {
|
||||
CAPTURE(input);
|
||||
REQUIRE(parseFloat<T>(input) == Approx(expected));
|
||||
REQUIRE(parseNumber<float>(input) == Approx(expected));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void checkNaN(const char* input) {
|
||||
void checkFloatNaN(const char* input) {
|
||||
CAPTURE(input);
|
||||
T result = parseFloat<T>(input);
|
||||
float result = parseNumber<float>(input);
|
||||
REQUIRE(result != result);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void checkInf(const char* input, bool negative) {
|
||||
void checkFloatInf(const char* input, bool negative) {
|
||||
CAPTURE(input);
|
||||
T x = parseFloat<T>(input);
|
||||
float x = parseNumber<float>(input);
|
||||
if (negative)
|
||||
REQUIRE(x < 0);
|
||||
else
|
||||
@ -36,137 +34,69 @@ void checkInf(const char* input, bool negative) {
|
||||
REQUIRE(x * 2 == x); // a property of infinity
|
||||
}
|
||||
|
||||
TEST_CASE("parseFloat<float>()") {
|
||||
TEST_CASE("parseNumber<float>()") {
|
||||
SECTION("Float_Short_NoExponent") {
|
||||
checkFloat<float>("3.14", 3.14f);
|
||||
checkFloat<float>("-3.14", -3.14f);
|
||||
checkFloat<float>("+3.14", +3.14f);
|
||||
checkFloat("3.14", 3.14f);
|
||||
checkFloat("-3.14", -3.14f);
|
||||
checkFloat("+3.14", +3.14f);
|
||||
}
|
||||
|
||||
SECTION("Short_NoDot") {
|
||||
checkFloat<float>("1E+38", 1E+38f);
|
||||
checkFloat<float>("-1E+38", -1E+38f);
|
||||
checkFloat<float>("+1E-38", +1E-38f);
|
||||
checkFloat<float>("+1e+38", +1e+38f);
|
||||
checkFloat<float>("-1e-38", -1e-38f);
|
||||
checkFloat("1E+38", 1E+38f);
|
||||
checkFloat("-1E+38", -1E+38f);
|
||||
checkFloat("+1E-38", +1E-38f);
|
||||
checkFloat("+1e+38", +1e+38f);
|
||||
checkFloat("-1e-38", -1e-38f);
|
||||
}
|
||||
|
||||
SECTION("Max") {
|
||||
checkFloat<float>("340.2823e+36", 3.402823e+38f);
|
||||
checkFloat<float>("34.02823e+37", 3.402823e+38f);
|
||||
checkFloat<float>("3.402823e+38", 3.402823e+38f);
|
||||
checkFloat<float>("0.3402823e+39", 3.402823e+38f);
|
||||
checkFloat<float>("0.03402823e+40", 3.402823e+38f);
|
||||
checkFloat<float>("0.003402823e+41", 3.402823e+38f);
|
||||
checkFloat("340.2823e+36", 3.402823e+38f);
|
||||
checkFloat("34.02823e+37", 3.402823e+38f);
|
||||
checkFloat("3.402823e+38", 3.402823e+38f);
|
||||
checkFloat("0.3402823e+39", 3.402823e+38f);
|
||||
checkFloat("0.03402823e+40", 3.402823e+38f);
|
||||
checkFloat("0.003402823e+41", 3.402823e+38f);
|
||||
}
|
||||
|
||||
SECTION("VeryLong") {
|
||||
checkFloat<float>("0.00000000000000000000000000000001", 1e-32f);
|
||||
checkFloat<float>("100000000000000000000000000000000.0", 1e+32f);
|
||||
checkFloat<float>(
|
||||
checkFloat("0.00000000000000000000000000000001", 1e-32f);
|
||||
checkFloat("100000000000000000000000000000000.0", 1e+32f);
|
||||
checkFloat(
|
||||
"100000000000000000000000000000000.00000000000000000000000000000",
|
||||
1e+32f);
|
||||
}
|
||||
|
||||
SECTION("MantissaTooLongToFit") {
|
||||
checkFloat<float>("0.340282346638528861111111111111", 0.34028234663852886f);
|
||||
checkFloat<float>("34028234663852886.11111111111111", 34028234663852886.0f);
|
||||
checkFloat<float>("34028234.66385288611111111111111", 34028234.663852886f);
|
||||
checkFloat("0.340282346638528861111111111111", 0.34028234663852886f);
|
||||
checkFloat("34028234663852886.11111111111111", 34028234663852886.0f);
|
||||
checkFloat("34028234.66385288611111111111111", 34028234.663852886f);
|
||||
|
||||
checkFloat<float>("-0.340282346638528861111111111111",
|
||||
-0.34028234663852886f);
|
||||
checkFloat<float>("-34028234663852886.11111111111111",
|
||||
-34028234663852886.0f);
|
||||
checkFloat<float>("-34028234.66385288611111111111111",
|
||||
-34028234.663852886f);
|
||||
checkFloat("-0.340282346638528861111111111111", -0.34028234663852886f);
|
||||
checkFloat("-34028234663852886.11111111111111", -34028234663852886.0f);
|
||||
checkFloat("-34028234.66385288611111111111111", -34028234.663852886f);
|
||||
}
|
||||
|
||||
SECTION("ExponentTooBig") {
|
||||
checkInf<float>("1e39", false);
|
||||
checkInf<float>("-1e39", true);
|
||||
checkInf<float>("1e255", false);
|
||||
checkFloat<float>("1e-255", 0.0f);
|
||||
checkFloatInf("1e39", false);
|
||||
checkFloatInf("-1e39", true);
|
||||
checkFloatInf("1e255", false);
|
||||
checkFloat("1e-255", 0.0f);
|
||||
}
|
||||
|
||||
SECTION("NaN") {
|
||||
checkNaN<float>("NaN");
|
||||
checkNaN<float>("nan");
|
||||
checkFloatNaN("NaN");
|
||||
checkFloatNaN("nan");
|
||||
}
|
||||
|
||||
SECTION("Infinity") {
|
||||
checkInf<float>("Infinity", false);
|
||||
checkInf<float>("+Infinity", false);
|
||||
checkInf<float>("-Infinity", true);
|
||||
checkInf<float>("inf", false);
|
||||
checkInf<float>("+inf", false);
|
||||
checkInf<float>("-inf", true);
|
||||
checkFloatInf("Infinity", false);
|
||||
checkFloatInf("+Infinity", false);
|
||||
checkFloatInf("-Infinity", true);
|
||||
checkFloatInf("inf", false);
|
||||
checkFloatInf("+inf", false);
|
||||
checkFloatInf("-inf", true);
|
||||
|
||||
checkInf<float>("1e300", false);
|
||||
checkInf<float>("-1e300", true);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("parseFloat<double>()") {
|
||||
SECTION("Short_NoExponent") {
|
||||
checkFloat<double>("3.14", 3.14);
|
||||
checkFloat<double>("-3.14", -3.14);
|
||||
checkFloat<double>("+3.14", +3.14);
|
||||
}
|
||||
|
||||
SECTION("Short_NoDot") {
|
||||
checkFloat<double>("1E+308", 1E+308);
|
||||
checkFloat<double>("-1E+308", -1E+308);
|
||||
checkFloat<double>("+1E-308", +1E-308);
|
||||
checkFloat<double>("+1e+308", +1e+308);
|
||||
checkFloat<double>("-1e-308", -1e-308);
|
||||
}
|
||||
|
||||
SECTION("Max") {
|
||||
checkFloat<double>(".017976931348623147e+310", 1.7976931348623147e+308);
|
||||
checkFloat<double>(".17976931348623147e+309", 1.7976931348623147e+308);
|
||||
checkFloat<double>("1.7976931348623147e+308", 1.7976931348623147e+308);
|
||||
checkFloat<double>("17.976931348623147e+307", 1.7976931348623147e+308);
|
||||
checkFloat<double>("179.76931348623147e+306", 1.7976931348623147e+308);
|
||||
}
|
||||
|
||||
SECTION("Min") {
|
||||
checkFloat<double>(".022250738585072014e-306", 2.2250738585072014e-308);
|
||||
checkFloat<double>(".22250738585072014e-307", 2.2250738585072014e-308);
|
||||
checkFloat<double>("2.2250738585072014e-308", 2.2250738585072014e-308);
|
||||
checkFloat<double>("22.250738585072014e-309", 2.2250738585072014e-308);
|
||||
checkFloat<double>("222.50738585072014e-310", 2.2250738585072014e-308);
|
||||
}
|
||||
|
||||
SECTION("VeryLong") {
|
||||
checkFloat<double>("0.00000000000000000000000000000001", 1e-32);
|
||||
checkFloat<double>("100000000000000000000000000000000.0", 1e+32);
|
||||
checkFloat<double>(
|
||||
"100000000000000000000000000000000.00000000000000000000000000000",
|
||||
1e+32);
|
||||
}
|
||||
|
||||
SECTION("MantissaTooLongToFit") {
|
||||
checkFloat<double>("0.179769313486231571111111111111", 0.17976931348623157);
|
||||
checkFloat<double>("17976931348623157.11111111111111", 17976931348623157.0);
|
||||
checkFloat<double>("1797693.134862315711111111111111", 1797693.1348623157);
|
||||
|
||||
checkFloat<double>("-0.179769313486231571111111111111",
|
||||
-0.17976931348623157);
|
||||
checkFloat<double>("-17976931348623157.11111111111111",
|
||||
-17976931348623157.0);
|
||||
checkFloat<double>("-1797693.134862315711111111111111",
|
||||
-1797693.1348623157);
|
||||
}
|
||||
|
||||
SECTION("ExponentTooBig") {
|
||||
checkInf<double>("1e309", false);
|
||||
checkInf<double>("-1e309", true);
|
||||
checkInf<double>("1e65535", false);
|
||||
checkFloat<double>("1e-65535", 0.0);
|
||||
}
|
||||
|
||||
SECTION("NaN") {
|
||||
checkNaN<double>("NaN");
|
||||
checkNaN<double>("nan");
|
||||
checkFloatInf("1e300", false);
|
||||
checkFloatInf("-1e300", true);
|
||||
}
|
||||
}
|
||||
|
@ -3,7 +3,8 @@
|
||||
// MIT License
|
||||
|
||||
#include <stdint.h>
|
||||
#include <ArduinoJson/Numbers/parseInteger.hpp>
|
||||
#include <ArduinoJson/Numbers/parseNumber.hpp>
|
||||
#include <ArduinoJson/Variant/VariantImpl.hpp>
|
||||
#include <catch.hpp>
|
||||
|
||||
using namespace ARDUINOJSON_NAMESPACE;
|
||||
@ -11,11 +12,11 @@ using namespace ARDUINOJSON_NAMESPACE;
|
||||
template <typename T>
|
||||
void checkInteger(const char* input, T expected) {
|
||||
CAPTURE(input);
|
||||
T actual = parseInteger<T>(input);
|
||||
T actual = parseNumber<T>(input);
|
||||
REQUIRE(expected == actual);
|
||||
}
|
||||
|
||||
TEST_CASE("parseInteger<int8_t>()") {
|
||||
TEST_CASE("parseNumber<int8_t>()") {
|
||||
checkInteger<int8_t>("-128", -128);
|
||||
checkInteger<int8_t>("127", 127);
|
||||
checkInteger<int8_t>("+127", 127);
|
||||
@ -25,7 +26,7 @@ TEST_CASE("parseInteger<int8_t>()") {
|
||||
checkInteger<int8_t>("-129", 0); // overflow
|
||||
}
|
||||
|
||||
TEST_CASE("parseInteger<int16_t>()") {
|
||||
TEST_CASE("parseNumber<int16_t>()") {
|
||||
checkInteger<int16_t>("-32768", -32768);
|
||||
checkInteger<int16_t>("32767", 32767);
|
||||
checkInteger<int16_t>("+32767", 32767);
|
||||
@ -35,7 +36,7 @@ TEST_CASE("parseInteger<int16_t>()") {
|
||||
checkInteger<int16_t>("32768", 0); // overflow
|
||||
}
|
||||
|
||||
TEST_CASE("parseInteger<int32_t>()") {
|
||||
TEST_CASE("parseNumber<int32_t>()") {
|
||||
checkInteger<int32_t>("-2147483648", (-2147483647 - 1));
|
||||
checkInteger<int32_t>("2147483647", 2147483647);
|
||||
checkInteger<int32_t>("+2147483647", 2147483647);
|
||||
@ -45,7 +46,7 @@ TEST_CASE("parseInteger<int32_t>()") {
|
||||
checkInteger<int32_t>("2147483648", 0); // overflow
|
||||
}
|
||||
|
||||
TEST_CASE("parseInteger<uint8_t>()") {
|
||||
TEST_CASE("parseNumber<uint8_t>()") {
|
||||
checkInteger<uint8_t>("0", 0);
|
||||
checkInteger<uint8_t>("255", 255);
|
||||
checkInteger<uint8_t>("+255", 255);
|
||||
@ -55,7 +56,7 @@ TEST_CASE("parseInteger<uint8_t>()") {
|
||||
checkInteger<uint8_t>("256", 0);
|
||||
}
|
||||
|
||||
TEST_CASE("parseInteger<uint16_t>()") {
|
||||
TEST_CASE("parseNumber<uint16_t>()") {
|
||||
checkInteger<uint16_t>("0", 0);
|
||||
checkInteger<uint16_t>("65535", 65535);
|
||||
checkInteger<uint16_t>("+65535", 65535);
|
||||
|
@ -2,22 +2,37 @@
|
||||
// Copyright Benoit Blanchon 2014-2020
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson/Numbers/Integer.hpp>
|
||||
#include <ArduinoJson/Numbers/parseNumber.hpp>
|
||||
#include <ArduinoJson/Variant/VariantImpl.hpp>
|
||||
#include <catch.hpp>
|
||||
|
||||
using namespace ARDUINOJSON_NAMESPACE;
|
||||
|
||||
TEST_CASE("Test uint32_t overflow") {
|
||||
ParsedNumber<float, uint32_t> first, second;
|
||||
TEST_CASE("Test unsigned integer overflow") {
|
||||
VariantData first, second;
|
||||
first.init();
|
||||
second.init();
|
||||
|
||||
// Avoids MSVC warning C4127 (conditional expression is constant)
|
||||
size_t integerSize = sizeof(Integer);
|
||||
|
||||
if (integerSize == 8) {
|
||||
parseNumber("18446744073709551615", first);
|
||||
parseNumber("18446744073709551616", second);
|
||||
} else {
|
||||
parseNumber("4294967295", first);
|
||||
parseNumber("4294967296", second);
|
||||
}
|
||||
|
||||
REQUIRE(first.type() == uint8_t(VALUE_IS_POSITIVE_INTEGER));
|
||||
REQUIRE(second.type() == uint8_t(VALUE_IS_FLOAT));
|
||||
}
|
||||
|
||||
TEST_CASE("Invalid value") {
|
||||
ParsedNumber<float, uint32_t> result;
|
||||
VariantData result;
|
||||
result.init();
|
||||
|
||||
parseNumber("6a3", result);
|
||||
|
||||
REQUIRE(result.type() == uint8_t(VALUE_IS_NULL));
|
||||
|
@ -499,26 +499,12 @@ class JsonDeserializer {
|
||||
return true;
|
||||
}
|
||||
|
||||
ParsedNumber<Float, UInt> num;
|
||||
parseNumber<Float, UInt>(_buffer, num);
|
||||
|
||||
switch (num.type()) {
|
||||
case VALUE_IS_NEGATIVE_INTEGER:
|
||||
result.setNegativeInteger(num.uintValue);
|
||||
return true;
|
||||
|
||||
case VALUE_IS_POSITIVE_INTEGER:
|
||||
result.setPositiveInteger(num.uintValue);
|
||||
return true;
|
||||
|
||||
case VALUE_IS_FLOAT:
|
||||
result.setFloat(num.floatValue);
|
||||
return true;
|
||||
|
||||
default:
|
||||
if (!parseNumber(_buffer, result)) {
|
||||
_error = DeserializationError::InvalidInput;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool skipNumericValue() {
|
||||
|
@ -1,20 +0,0 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2020
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <ArduinoJson/Numbers/convertNumber.hpp>
|
||||
#include <ArduinoJson/Numbers/parseNumber.hpp>
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
template <typename T>
|
||||
inline T parseFloat(const char* s) {
|
||||
// try to reuse the same parameters as JsonDeserializer
|
||||
typedef typename choose_largest<Float, T>::type TFloat;
|
||||
ParsedNumber<TFloat, UInt> value;
|
||||
parseNumber(s, value);
|
||||
return value.template as<T>();
|
||||
}
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
@ -1,21 +0,0 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2020
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <ArduinoJson/Numbers/convertNumber.hpp>
|
||||
#include <ArduinoJson/Numbers/parseNumber.hpp>
|
||||
#include <ArduinoJson/Polyfills/type_traits.hpp>
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
template <typename T>
|
||||
T parseInteger(const char *s) {
|
||||
// try to reuse the same parameters as JsonDeserializer
|
||||
typedef typename choose_largest<UInt, typename make_unsigned<T>::type>::type
|
||||
TUInt;
|
||||
ParsedNumber<Float, TUInt> value;
|
||||
parseNumber(s, value);
|
||||
return value.template as<T>();
|
||||
}
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
@ -10,59 +10,18 @@
|
||||
#include <ArduinoJson/Polyfills/ctype.hpp>
|
||||
#include <ArduinoJson/Polyfills/math.hpp>
|
||||
#include <ArduinoJson/Polyfills/type_traits.hpp>
|
||||
#include <ArduinoJson/Variant/VariantContent.hpp>
|
||||
#include <ArduinoJson/Variant/VariantAs.hpp>
|
||||
#include <ArduinoJson/Variant/VariantData.hpp>
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
template <typename TFloat, typename TUInt>
|
||||
struct ParsedNumber {
|
||||
ParsedNumber() : _type(VALUE_IS_NULL) {}
|
||||
|
||||
void setInteger(TUInt value, bool is_negative) {
|
||||
uintValue = value;
|
||||
_type = uint8_t(is_negative ? VALUE_IS_NEGATIVE_INTEGER
|
||||
: VALUE_IS_POSITIVE_INTEGER);
|
||||
}
|
||||
|
||||
void setFloat(TFloat value) {
|
||||
floatValue = value;
|
||||
_type = VALUE_IS_FLOAT;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T as() const {
|
||||
switch (_type) {
|
||||
case VALUE_IS_NEGATIVE_INTEGER:
|
||||
return convertNegativeInteger<T>(uintValue);
|
||||
case VALUE_IS_POSITIVE_INTEGER:
|
||||
return convertPositiveInteger<T>(uintValue);
|
||||
case VALUE_IS_FLOAT:
|
||||
return convertFloat<T>(floatValue);
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t type() const {
|
||||
return _type;
|
||||
}
|
||||
|
||||
union {
|
||||
TUInt uintValue;
|
||||
TFloat floatValue;
|
||||
};
|
||||
uint8_t _type;
|
||||
}; // namespace ARDUINOJSON_NAMESPACE
|
||||
|
||||
template <typename A, typename B>
|
||||
struct choose_largest : conditional<(sizeof(A) > sizeof(B)), A, B> {};
|
||||
|
||||
template <typename TFloat, typename TUInt>
|
||||
inline void parseNumber(const char* s, ParsedNumber<TFloat, TUInt>& result) {
|
||||
typedef FloatTraits<TFloat> traits;
|
||||
typedef typename choose_largest<typename traits::mantissa_type, TUInt>::type
|
||||
mantissa_t;
|
||||
typedef typename traits::exponent_type exponent_t;
|
||||
inline bool parseNumber(const char* s, VariantData& result) {
|
||||
typedef FloatTraits<Float> traits;
|
||||
typedef choose_largest<traits::mantissa_type, UInt>::type mantissa_t;
|
||||
typedef traits::exponent_type exponent_t;
|
||||
|
||||
ARDUINOJSON_ASSERT(s != 0);
|
||||
|
||||
@ -80,24 +39,23 @@ inline void parseNumber(const char* s, ParsedNumber<TFloat, TUInt>& result) {
|
||||
#if ARDUINOJSON_ENABLE_NAN
|
||||
if (*s == 'n' || *s == 'N') {
|
||||
result.setFloat(traits::nan());
|
||||
return;
|
||||
return true;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#if ARDUINOJSON_ENABLE_INFINITY
|
||||
if (*s == 'i' || *s == 'I') {
|
||||
result.setFloat(is_negative ? -traits::inf() : traits::inf());
|
||||
return;
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (!isdigit(*s) && *s != '.')
|
||||
return;
|
||||
return false;
|
||||
|
||||
mantissa_t mantissa = 0;
|
||||
exponent_t exponent_offset = 0;
|
||||
const mantissa_t maxUint = TUInt(-1);
|
||||
const mantissa_t maxUint = UInt(-1);
|
||||
|
||||
while (isdigit(*s)) {
|
||||
uint8_t digit = uint8_t(*s - '0');
|
||||
@ -111,8 +69,11 @@ inline void parseNumber(const char* s, ParsedNumber<TFloat, TUInt>& result) {
|
||||
}
|
||||
|
||||
if (*s == '\0') {
|
||||
result.setInteger(TUInt(mantissa), is_negative);
|
||||
return;
|
||||
if (is_negative)
|
||||
result.setNegativeInteger(UInt(mantissa));
|
||||
else
|
||||
result.setPositiveInteger(UInt(mantissa));
|
||||
return true;
|
||||
}
|
||||
|
||||
// avoid mantissa overflow
|
||||
@ -156,7 +117,7 @@ inline void parseNumber(const char* s, ParsedNumber<TFloat, TUInt>& result) {
|
||||
result.setFloat(is_negative ? -0.0f : 0.0f);
|
||||
else
|
||||
result.setFloat(is_negative ? -traits::inf() : traits::inf());
|
||||
return;
|
||||
return true;
|
||||
}
|
||||
s++;
|
||||
}
|
||||
@ -167,11 +128,20 @@ inline void parseNumber(const char* s, ParsedNumber<TFloat, TUInt>& result) {
|
||||
|
||||
// we should be at the end of the string, otherwise it's an error
|
||||
if (*s != '\0')
|
||||
return;
|
||||
return false;
|
||||
|
||||
TFloat final_result =
|
||||
traits::make_float(static_cast<TFloat>(mantissa), exponent);
|
||||
Float final_result =
|
||||
traits::make_float(static_cast<Float>(mantissa), exponent);
|
||||
|
||||
result.setFloat(is_negative ? -final_result : final_result);
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline T parseNumber(const char* s) {
|
||||
VariantData value;
|
||||
value.init(); // VariantData is a POD, so it has no constructor
|
||||
parseNumber(s, value);
|
||||
return variantAs<T>(&value);
|
||||
}
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
||||
|
@ -33,6 +33,9 @@ class VariantData {
|
||||
// - no destructor
|
||||
// - no virtual
|
||||
// - no inheritance
|
||||
void init() {
|
||||
_flags = 0;
|
||||
}
|
||||
|
||||
template <typename TVisitor>
|
||||
typename TVisitor::result_type accept(TVisitor &visitor) const {
|
||||
@ -365,11 +368,11 @@ class VariantData {
|
||||
_content.asCollection.movePointers(stringDistance, variantDistance);
|
||||
}
|
||||
|
||||
private:
|
||||
uint8_t type() const {
|
||||
return _flags & VALUE_MASK;
|
||||
}
|
||||
|
||||
private:
|
||||
void setType(uint8_t t) {
|
||||
_flags &= KEY_IS_OWNED;
|
||||
_flags |= t;
|
||||
|
@ -4,10 +4,11 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <ArduinoJson/Array/ArrayRef.hpp>
|
||||
#include <ArduinoJson/Configuration.hpp>
|
||||
#include <ArduinoJson/Numbers/convertNumber.hpp>
|
||||
#include <ArduinoJson/Numbers/parseFloat.hpp>
|
||||
#include <ArduinoJson/Numbers/parseInteger.hpp>
|
||||
#include <ArduinoJson/Numbers/parseNumber.hpp>
|
||||
#include <ArduinoJson/Object/ObjectRef.hpp>
|
||||
#include <ArduinoJson/Variant/VariantRef.hpp>
|
||||
|
||||
#include <string.h> // for strcmp
|
||||
@ -24,7 +25,7 @@ inline T VariantData::asIntegral() const {
|
||||
return convertNegativeInteger<T>(_content.asInteger);
|
||||
case VALUE_IS_LINKED_STRING:
|
||||
case VALUE_IS_OWNED_STRING:
|
||||
return parseInteger<T>(_content.asString);
|
||||
return parseNumber<T>(_content.asString);
|
||||
case VALUE_IS_FLOAT:
|
||||
return convertFloat<T>(_content.asFloat);
|
||||
default:
|
||||
@ -58,7 +59,7 @@ inline T VariantData::asFloat() const {
|
||||
return -static_cast<T>(_content.asInteger);
|
||||
case VALUE_IS_LINKED_STRING:
|
||||
case VALUE_IS_OWNED_STRING:
|
||||
return parseFloat<T>(_content.asString);
|
||||
return parseNumber<T>(_content.asString);
|
||||
case VALUE_IS_FLOAT:
|
||||
return static_cast<T>(_content.asFloat);
|
||||
default:
|
||||
|
Reference in New Issue
Block a user