1
0
Fork 0

fix(printf): fixed floating point sign handling

development
Marco Paland 7 years ago
parent 98e9b0dab6
commit 85dc7cf074

@ -305,14 +305,14 @@ static size_t _ftoa(double value, char* buffer, size_t maxlen, unsigned int prec
}
// pad leading zeros
while (!(flags & FLAGS_LEFT) && (len < prec) && (len < PRINTF_FTOA_BUFFER_SIZE)) {
buf[len++] = '0';
}
while (!(flags & FLAGS_LEFT) && (flags & FLAGS_ZEROPAD) && (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++] = '-';

@ -79,6 +79,12 @@ TEST_CASE("space flag", "[]" ) {
test::sprintf(buffer, "% 15d", -42);
REQUIRE(!strcmp(buffer, " -42"));
test::sprintf(buffer, "% 15.3f", -42.987);
REQUIRE(!strcmp(buffer, " -42.987"));
test::sprintf(buffer, "% 15.3f", 42.987);
REQUIRE(!strcmp(buffer, " 42.987"));
test::sprintf(buffer, "% s", "Hello testing");
REQUIRE(!strcmp(buffer, "Hello testing"));
@ -211,6 +217,15 @@ TEST_CASE("0 flag", "[]" ) {
test::sprintf(buffer, "%015d", -42);
REQUIRE(!strcmp(buffer, "-00000000000042"));
test::sprintf(buffer, "%015.2f", 42.1234);
REQUIRE(!strcmp(buffer, "000000000042.12"));
test::sprintf(buffer, "%015.3f", 42.9876);
REQUIRE(!strcmp(buffer, "00000000042.988"));
test::sprintf(buffer, "%015.5f", -42.9876);
REQUIRE(!strcmp(buffer, "-00000042.98760"));
}
@ -485,6 +500,9 @@ TEST_CASE("width -20", "[]" ) {
test::sprintf(buffer, "%-20u", 1024);
REQUIRE(!strcmp(buffer, "1024 "));
test::sprintf(buffer, "%-20.4f", 1024.1234);
REQUIRE(!strcmp(buffer, "1024.1234 "));
test::sprintf(buffer, "%-20u", 4294966272U);
REQUIRE(!strcmp(buffer, "4294966272 "));

Loading…
Cancel
Save