Browse Source

Fixed printf formating.

Бранимир Караџић 5 years ago
parent
commit
f26f7feeb0
2 changed files with 27 additions and 8 deletions
  1. 9 8
      src/string.cpp
  2. 18 0
      tests/vsnprintf_test.cpp

+ 9 - 8
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);
 			}
 

+ 18 - 0
tests/vsnprintf_test.cpp

@@ -66,6 +66,18 @@ TEST_CASE("vsnprintf f")
 	REQUIRE(test("     inf", "%8f",   std::numeric_limits<double>::infinity() ) );
 	REQUIRE(test("inf     ", "%-8f",  std::numeric_limits<double>::infinity() ) );
 	REQUIRE(test("    -INF", "%8F",  -std::numeric_limits<double>::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) );