diff options
author | Vedran Grudenic <grudenic.vedran@gmail.com> | 2019-01-20 23:14:51 +0100 |
---|---|---|
committer | Marco Paland <marco@paland.com> | 2019-01-26 14:39:55 +0100 |
commit | c7fbbfd600f929505181d938a2bcdfc6bd50aa07 (patch) | |
tree | c97fd8f13f83ba6689ed43dd9e735785e1a4bfde | |
parent | c013a0e8d6bb57247074c748a05bbe7f43bdec6f (diff) | |
download | printf-c7fbbfd600f929505181d938a2bcdfc6bd50aa07.tar.gz printf-c7fbbfd600f929505181d938a2bcdfc6bd50aa07.tar.bz2 printf-c7fbbfd600f929505181d938a2bcdfc6bd50aa07.zip |
patch for issue #40 (Digit missing from negative numbers in certain cases)
-rw-r--r-- | printf.c | 14 | ||||
-rw-r--r-- | test/test_suite.cpp | 68 |
2 files changed, 74 insertions, 8 deletions
@@ -178,6 +178,9 @@ static size_t _ntoa_format(out_fct_type out, char* buffer, size_t idx, size_t ma // pad leading zeros
if (!(flags & FLAGS_LEFT)) {
+ if (width && (flags & FLAGS_ZEROPAD) && (negative || (flags & (FLAGS_PLUS | FLAGS_SPACE)))) {
+ width--;
+ }
while ((len < prec) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
buf[len++] = '0';
}
@@ -208,10 +211,6 @@ static size_t _ntoa_format(out_fct_type out, char* buffer, size_t idx, size_t ma }
}
- // handle sign
- if (len && (len == width) && (negative || (flags & FLAGS_PLUS) || (flags & FLAGS_SPACE))) {
- len--;
- }
if (len < PRINTF_NTOA_BUFFER_SIZE) {
if (negative) {
buf[len++] = '-';
@@ -395,15 +394,14 @@ static size_t _ftoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, d // pad leading zeros
if (!(flags & FLAGS_LEFT) && (flags & FLAGS_ZEROPAD)) {
+ if (width && (negative || (flags & (FLAGS_PLUS | FLAGS_SPACE)))) {
+ width--;
+ }
while ((len < width) && (len < PRINTF_FTOA_BUFFER_SIZE)) {
buf[len++] = '0';
}
}
- // handle sign
- if ((len == width) && (negative || (flags & FLAGS_PLUS) || (flags & FLAGS_SPACE))) {
- len--;
- }
if (len < PRINTF_FTOA_BUFFER_SIZE) {
if (negative) {
buf[len++] = '-';
diff --git a/test/test_suite.cpp b/test/test_suite.cpp index 100b59f..66ba5f3 100644 --- a/test/test_suite.cpp +++ b/test/test_suite.cpp @@ -864,6 +864,74 @@ TEST_CASE("padding 20.5", "[]" ) { REQUIRE(!strcmp(buffer, " 00EDCB5433"));
}
+TEST_CASE("padding neg numbers", "[]" ) {
+ char buffer[100];
+
+ // space padding
+
+ test::sprintf(buffer, "% 1d", -5);
+ REQUIRE(!strcmp(buffer, "-5"));
+
+ test::sprintf(buffer, "% 2d", -5);
+ REQUIRE(!strcmp(buffer, "-5"));
+
+ test::sprintf(buffer, "% 3d", -5);
+ REQUIRE(!strcmp(buffer, " -5"));
+
+ test::sprintf(buffer, "% 4d", -5);
+ REQUIRE(!strcmp(buffer, " -5"));
+
+ // zero padding
+
+ test::sprintf(buffer, "%01d", -5);
+ REQUIRE(!strcmp(buffer, "-5"));
+
+ test::sprintf(buffer, "%02d", -5);
+ REQUIRE(!strcmp(buffer, "-5"));
+
+ test::sprintf(buffer, "%03d", -5);
+ REQUIRE(!strcmp(buffer, "-05"));
+
+ test::sprintf(buffer, "%04d", -5);
+ REQUIRE(!strcmp(buffer, "-005"));
+}
+
+TEST_CASE("float padding neg numbers", "[]" ) {
+ char buffer[100];
+
+ // space padding
+
+ test::sprintf(buffer, "% 3.1f", -5.);
+ REQUIRE(!strcmp(buffer, "-5.0"));
+
+ test::sprintf(buffer, "% 4.1f", -5.);
+ REQUIRE(!strcmp(buffer, "-5.0"));
+
+ test::sprintf(buffer, "% 5.1f", -5.);
+ REQUIRE(!strcmp(buffer, " -5.0"));
+
+ // zero padding
+
+ test::sprintf(buffer, "%03.1f", -5.);
+ REQUIRE(!strcmp(buffer, "-5.0"));
+
+ test::sprintf(buffer, "%04.1f", -5.);
+ REQUIRE(!strcmp(buffer, "-5.0"));
+
+ test::sprintf(buffer, "%05.1f", -5.);
+ REQUIRE(!strcmp(buffer, "-05.0"));
+
+ // zero padding no decimal point
+
+ test::sprintf(buffer, "%01.0f", -5.);
+ REQUIRE(!strcmp(buffer, "-5"));
+
+ test::sprintf(buffer, "%02.0f", -5.);
+ REQUIRE(!strcmp(buffer, "-5"));
+
+ test::sprintf(buffer, "%03.0f", -5.);
+ REQUIRE(!strcmp(buffer, "-05"));
+}
TEST_CASE("length", "[]" ) {
char buffer[100];
|