From f26f7feeb01a58e8727473b21a64d17e8a7463eb 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: Thu, 30 Apr 2020 23:00:16 -0700 Subject: [PATCH] Fixed printf formating. --- src/string.cpp | 17 +++++++++-------- tests/vsnprintf_test.cpp | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/string.cpp b/src/string.cpp index 1b85120..669cd2a 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -736,11 +736,12 @@ namespace bx len = min(_param.width, len); } - const bool hasSign = _param.sign || _str[0] == '-'; - char sign = hasSign ? _str[0] == '-' ? '-' : '+' : '\0'; + const bool hasMinus = (NULL != _str && '-' == _str[0]); + const bool hasSign = _param.sign || hasMinus; + char sign = hasSign ? hasMinus ? '-' : '+' : '\0'; const char* str = _str; - if (str[0] == '-') + if (hasMinus) { str++; len--; @@ -760,6 +761,11 @@ namespace bx size += writeRep(_writer, _param.fill, max(0, padding), _err); } + if ('\0' != sign) + { + size += write(_writer, sign, _err); + } + if (NULL == _str) { size += write(_writer, "(null)", 6, _err); @@ -773,11 +779,6 @@ namespace bx } else { - if ('\0' != sign) - { - size += write(_writer, sign, _err); - } - size += write(_writer, str, len, _err); } diff --git a/tests/vsnprintf_test.cpp b/tests/vsnprintf_test.cpp index ae4bb28..a7be774 100644 --- a/tests/vsnprintf_test.cpp +++ b/tests/vsnprintf_test.cpp @@ -66,6 +66,18 @@ TEST_CASE("vsnprintf f") REQUIRE(test(" inf", "%8f", std::numeric_limits::infinity() ) ); REQUIRE(test("inf ", "%-8f", std::numeric_limits::infinity() ) ); REQUIRE(test(" -INF", "%8F", -std::numeric_limits::infinity() ) ); + + REQUIRE(test(" 1.0", "%4.1f", 1.0) ); + REQUIRE(test(" 1.500", "%6.3f", 1.5) ); + 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", "%.8f", 0.00390625) ); + REQUIRE(test("-0.00390625", "%.8f", -0.00390625) ); + REQUIRE(test("1.50000000000000000", "%.17f", 1.5) ); } TEST_CASE("vsnprintf d/i/o/u/x") @@ -82,6 +94,8 @@ TEST_CASE("vsnprintf d/i/o/u/x") REQUIRE(test("2471", "%o", 1337) ); REQUIRE(test("1337 ", "%-20o", 01337) ); REQUIRE(test("37777776441 ", "%-20o", -01337) ); + REQUIRE(test(" 2471", "%20o", 1337) ); + REQUIRE(test("00000000000000002471", "%020o", 1337) ); REQUIRE(test("1337", "%u", 1337) ); REQUIRE(test("1337 ", "%-20u", 1337) ); @@ -114,11 +128,15 @@ TEST_CASE("vsnprintf d/i/o/u/x") REQUIRE(test(" -1", "% 4i", -1) ); REQUIRE(test(" 0", "% 4i", 0) ); REQUIRE(test(" 1", "% 4i", 1) ); + REQUIRE(test(" 1", "% 4o", 1) ); REQUIRE(test(" +1", "%+4i", 1) ); + REQUIRE(test(" +1", "%+4o", 1) ); REQUIRE(test(" +0", "%+4i", 0) ); REQUIRE(test(" -1", "%+4i", -1) ); REQUIRE(test("0001", "%04i", 1) ); + REQUIRE(test("0001", "%04o", 1) ); REQUIRE(test("0000", "%04i", 0) ); + REQUIRE(test("0000", "%04o", 0) ); REQUIRE(test("-001", "%04i", -1) ); REQUIRE(test("+001", "%+04i", 1) );