Skip to content

Commit 21a282a

Browse files
committed
fix(printf): ignore 0 flag for integers if precision is specified
Fixes #27
1 parent 7075d31 commit 21a282a

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

printf.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,11 @@ static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const
551551
flags &= ~(FLAGS_PLUS | FLAGS_SPACE);
552552
}
553553

554+
// ignore '0' flag when precision is given
555+
if (flags & FLAGS_PRECISION) {
556+
flags &= ~FLAGS_ZEROPAD;
557+
}
558+
554559
// convert the integer
555560
if ((*format == 'i') || (*format == 'd')) {
556561
// signed

test/test_suite.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -934,6 +934,12 @@ TEST_CASE("length", "[]" ) {
934934

935935
test::sprintf(buffer, "%20.X", 0U);
936936
REQUIRE(!strcmp(buffer, " "));
937+
938+
test::sprintf(buffer, "%02.0u", 0U);
939+
REQUIRE(!strcmp(buffer, " "));
940+
941+
test::sprintf(buffer, "%02.0d", 0);
942+
REQUIRE(!strcmp(buffer, " "));
937943
}
938944

939945

0 commit comments

Comments
 (0)