Skip to content

Commit

Permalink
Fixes mpaland#113 : Now properly zero-padding left-aligned values wit…
Browse files Browse the repository at this point in the history
…h precision set.

Also, added a relevant test suite check.
  • Loading branch information
eyalroz committed Jul 30, 2021
1 parent 9ebe7ff commit 438fb76
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
19 changes: 12 additions & 7 deletions printf.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,17 +230,22 @@ static size_t _out_rev(out_fct_type out, char* buffer, size_t idx, size_t maxlen
static size_t _ntoa_format(out_fct_type out, char* buffer, size_t idx, size_t maxlen, char* buf, size_t len, bool negative, unsigned int base, unsigned int precision, unsigned int width, unsigned int flags)
{
size_t unpadded_len = len;
// pad leading zeros
if (!(flags & FLAGS_LEFT)) {
if (width && (flags & FLAGS_ZEROPAD) && (negative || (flags & (FLAGS_PLUS | FLAGS_SPACE)))) {
width--;

// pad with leading zeros
{
if (!(flags & FLAGS_LEFT)) {
if (width && (flags & FLAGS_ZEROPAD) && (negative || (flags & (FLAGS_PLUS | FLAGS_SPACE)))) {
width--;
}
while ((flags & FLAGS_ZEROPAD) && (len < width) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
buf[len++] = '0';
}
}

while ((len < precision) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
buf[len++] = '0';
}
while ((flags & FLAGS_ZEROPAD) && (len < width) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
buf[len++] = '0';
}

if (base == 8U && (len > unpadded_len)) {
// Since we've written some zeros, we've satisfied the alternative format leading space requirement
flags &= ~FLAGS_HASH;
Expand Down
16 changes: 9 additions & 7 deletions test/test_suite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1010,13 +1010,15 @@ DISABLE_WARNING_POP

TEST_CASE("misc", "[]" ) {
char buffer[100];
PRINTING_CHECK("53000atest-20 bit", ==, test::sprintf_, buffer, "%u%u%ctest%d %s", 5, 3000, 'a', -20, "bit");
PRINTING_CHECK("0.33", ==, test::sprintf_, buffer, "%.*f", 2, 0.33333333);
PRINTING_CHECK("1", ==, test::sprintf_, buffer, "%.*d", -1, 1);
PRINTING_CHECK("foo", ==, test::sprintf_, buffer, "%.3s", "foobar");
PRINTING_CHECK(" ", ==, test::sprintf_, buffer, "% .0d", 0);
PRINTING_CHECK(" 00004", ==, test::sprintf_, buffer, "%10.5d", 4);
PRINTING_CHECK("hi x", ==, test::sprintf_, buffer, "%*sx", -3, "hi");
PRINTING_CHECK("53000atest-20 bit", ==, test::sprintf_, buffer, "%u%u%ctest%d %s", 5, 3000, 'a', -20, "bit");
PRINTING_CHECK("0.33", ==, test::sprintf_, buffer, "%.*f", 2, 0.33333333);
PRINTING_CHECK("1", ==, test::sprintf_, buffer, "%.*d", -1, 1);
PRINTING_CHECK("foo", ==, test::sprintf_, buffer, "%.3s", "foobar");
PRINTING_CHECK(" ", ==, test::sprintf_, buffer, "% .0d", 0);
PRINTING_CHECK(" 00004", ==, test::sprintf_, buffer, "%10.5d", 4);
PRINTING_CHECK("hi x", ==, test::sprintf_, buffer, "%*sx", -3, "hi");
PRINTING_CHECK("00123 ", ==, test::sprintf_, buffer, "%-20.5i", 123);


#if PRINTF_SUPPORT_EXPONENTIAL_SPECIFIERS
PRINTING_CHECK("0.33", ==, test::sprintf_, buffer, "%.*g", 2, 0.33333333);
Expand Down

0 comments on commit 438fb76

Please sign in to comment.