Skip to content

Commit

Permalink
fix zero flag for char types and make zero flag ignored if a precisio…
Browse files Browse the repository at this point in the history
…n is specified
  • Loading branch information
rimathia authored and vitaut committed May 23, 2020
1 parent bc1b89d commit 8c8f74a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
5 changes: 5 additions & 0 deletions include/fmt/printf.h
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ class printf_arg_formatter : public detail::arg_formatter_base<Range> {
return (*this)(static_cast<int>(value));
fmt_specs.sign = sign::none;
fmt_specs.alt = false;
fmt_specs.fill[0] = ' '; // Ignore '0' flag for char types.
// align::numeric needs to be overwritten here since the '0' flag is
// ignored for non-numeric types
if (fmt_specs.align == align::none || fmt_specs.align == align::numeric)
Expand Down Expand Up @@ -508,6 +509,10 @@ OutputIt basic_printf_context<OutputIt, Char>::format() {
}

format_arg arg = get_arg(arg_index);
// For d, i, o, u, x, and X conversion specifiers, if a precision is
// specified, the '0' flag is ignored
if (specs.precision >= 0 && arg.is_integral())
specs.fill[0] = ' '; // Ignore '0' flag for non-numeric types or if '-' present.
if (specs.precision >= 0 && arg.type() == detail::type::cstring_type) {
auto str = visit_format_arg(detail::get_cstring<Char>(), arg);
auto str_end = str + specs.precision;
Expand Down
5 changes: 4 additions & 1 deletion test/printf-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,11 @@ TEST(PrintfTest, ZeroFlag) {

EXPECT_PRINTF("+00042", "%00+6d", 42);

EXPECT_PRINTF(" 42", "%05.d", 42);
EXPECT_PRINTF(" 0042", "%05.4d", 42);

// '0' flag is ignored for non-numeric types.
EXPECT_PRINTF("0000x", "%05c", 'x');
EXPECT_PRINTF(" x", "%05c", 'x');
}

TEST(PrintfTest, PlusFlag) {
Expand Down

0 comments on commit 8c8f74a

Please sign in to comment.