From cef31cca445c5c7827fd3aec74500827100570bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Sun, 22 Oct 2017 19:24:26 -0700 Subject: [PATCH] Fixed fromString double. --- src/dtoa.cpp | 38 +++++++++++++++++++++++--------------- tests/string_test.cpp | 2 ++ 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/src/dtoa.cpp b/src/dtoa.cpp index 5b1858c..48f3d5c 100644 --- a/src/dtoa.cpp +++ b/src/dtoa.cpp @@ -703,6 +703,14 @@ namespace bx #define PARSER_PINF 3 // number is higher than +HUGE_VAL #define PARSER_MINF 4 // number is lower than -HUGE_VAL + inline char next(const char*& _s, const char* _term) + { + return _s != _term + ? *_s++ + : '\0' + ; + } + static int parser(const char* _s, const char* _term, PrepNumber* _pn) { int state = FSM_A; @@ -712,14 +720,14 @@ namespace bx int expneg = 0; int32_t expexp = 0; - while (state != FSM_STOP && _s != _term) + while (state != FSM_STOP) // && _s != _term) { switch (state) { case FSM_A: if (isSpace(c) ) { - c = *_s++; + c = next(_s, _term); } else { @@ -732,12 +740,12 @@ namespace bx if (c == '+') { - c = *_s++; + c = next(_s, _term); } else if (c == '-') { _pn->negative = 1; - c = *_s++; + c = next(_s, _term); } else if (isNumeric(c) ) { @@ -754,11 +762,11 @@ namespace bx case FSM_C: if (c == '0') { - c = *_s++; + c = next(_s, _term); } else if (c == '.') { - c = *_s++; + c = next(_s, _term); state = FSM_D; } else @@ -770,7 +778,7 @@ namespace bx case FSM_D: if (c == '0') { - c = *_s++; + c = next(_s, _term); if (_pn->exponent > -2147483647) _pn->exponent--; } else @@ -793,11 +801,11 @@ namespace bx _pn->exponent++; } - c = *_s++; + c = next(_s, _term); } else if (c == '.') { - c = *_s++; + c = next(_s, _term); state = FSM_F; } else @@ -817,11 +825,11 @@ namespace bx digx++; } - c = *_s++; + c = next(_s, _term); } else if ('e' == toLower(c) ) { - c = *_s++; + c = next(_s, _term); state = FSM_G; } else @@ -833,12 +841,12 @@ namespace bx case FSM_G: if (c == '+') { - c = *_s++; + c = next(_s, _term); } else if (c == '-') { expneg = 1; - c = *_s++; + c = next(_s, _term); } state = FSM_H; @@ -847,7 +855,7 @@ namespace bx case FSM_H: if (c == '0') { - c = *_s++; + c = next(_s, _term); } else { @@ -864,7 +872,7 @@ namespace bx expexp += c - '0'; } - c = *_s++; + c = next(_s, _term); } else { diff --git a/tests/string_test.cpp b/tests/string_test.cpp index 3664964..38b2bf4 100644 --- a/tests/string_test.cpp +++ b/tests/string_test.cpp @@ -248,6 +248,7 @@ TEST_CASE("toString double", "") REQUIRE(testToString(1231231.23, "1231231.23") ); REQUIRE(testToString(0.000000000123123, "1.23123e-10") ); REQUIRE(testToString(0.0000000001, "1e-10") ); + REQUIRE(testToString(-270.000000, "-270.0") ); } static bool testFromString(double _value, const char* _input) @@ -295,6 +296,7 @@ TEST_CASE("fromString double", "") REQUIRE(testFromString(1231231.23, "1231231.23") ); REQUIRE(testFromString(0.000000000123123, "1.23123e-10") ); REQUIRE(testFromString(0.0000000001, "1e-10") ); + REQUIRE(testFromString(-270.000000, "-270.0") ); } static bool testFromString(int32_t _value, const char* _input)