From 0f8bd8dfd5561f1e7e3ad4408071c9248cac4bd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D1=80=D0=B0=D0=BD=D0=B8=D0=BC=D0=B8=D1=80=20=D0=9A?= =?UTF-8?q?=D0=B0=D1=80=D0=B0=D1=9F=D0=B8=D1=9B?= Date: Wed, 1 Dec 2021 18:57:24 -0800 Subject: [PATCH] Fixed issue #267. --- src/string.cpp | 7 +++++-- tests/string_test.cpp | 1 + tests/vsnprintf_test.cpp | 4 +++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/string.cpp b/src/string.cpp index b95c69c..d5caf6f 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -887,10 +887,11 @@ namespace bx const char* dot = strFind(str, INT32_MAX, '.'); if (NULL != dot) { - const int32_t prec = INT32_MAX == _param.prec ? 6 : _param.prec; + const int32_t prec = INT32_MAX == _param.prec ? len-(dot+1-str) : _param.prec; + const int32_t precLen = int32_t( dot - + uint32_min(prec + _param.spec, 1) + + min(prec + _param.spec, 1) + prec - str ); @@ -900,8 +901,10 @@ namespace bx { str[ii] = '0'; } + str[precLen] = '\0'; } + len = precLen; } diff --git a/tests/string_test.cpp b/tests/string_test.cpp index 5524d91..48231bf 100644 --- a/tests/string_test.cpp +++ b/tests/string_test.cpp @@ -333,6 +333,7 @@ TEST_CASE("toString double", "") REQUIRE(testToString(-270.000000, "-270.0") ); REQUIRE(testToString(2.225073858507201e-308, "2.225073858507201e-308") ); REQUIRE(testToString(-79.39773355813419, "-79.39773355813419") ); + REQUIRE(testToString(-1.234567e-9, "-1.234567e-9") ); } template diff --git a/tests/vsnprintf_test.cpp b/tests/vsnprintf_test.cpp index 775d6d2..4b5e2c0 100644 --- a/tests/vsnprintf_test.cpp +++ b/tests/vsnprintf_test.cpp @@ -72,9 +72,11 @@ TEST_CASE("vsnprintf f") REQUIRE(test("0001.500", "%08.3f", 1.5) ); REQUIRE(test("+001.500", "%+08.3f", 1.5) ); REQUIRE(test("-001.500", "%+08.3f", -1.5) ); - REQUIRE(test("0.003906", "%f", 0.00390625) ); REQUIRE(test("0.0039", "%.4f", 0.00390625) ); + REQUIRE(test("0.00390625", "%f", 0.00390625) ); + REQUIRE(test("-1.234567e-9", "%f", -1.234567e-9) ); + REQUIRE(test("0.00390625", "%.8f", 0.00390625) ); REQUIRE(test("-0.00390625", "%.8f", -0.00390625) ); REQUIRE(test("1.50000000000000000", "%.17f", 1.5) );