Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion include/fmt/printf.h
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,19 @@ class printf_arg_formatter : public arg_formatter<Char> {
detail::write<Char>(this->out, value, this->specs, this->locale);
}

// C requires no characters when converting a zero value with a precision of
// zero, so only the sign and the padding remain.
void write_zero_with_zero_precision() {
auto s = this->specs;
const char* sign_str = s.sign() == sign::plus ? "+"
: s.sign() == sign::space ? " "
: "";
s.set_type(presentation_type::none);
if (s.align() == align::none || s.align() == align::numeric)
s.set_align(align::right);
write_bytes<Char>(this->out, sign_str, s);
}

public:
printf_arg_formatter(basic_appender<Char> iter, format_specs& s,
context_type& ctx)
Expand All @@ -258,6 +271,13 @@ class printf_arg_formatter : public arg_formatter<Char> {
// MSVC2013 fails to compile separate overloads for bool and Char so use
// std::is_same instead.
if (!std::is_same<T, Char>::value) {
auto t = this->specs.type();
if (value == 0 && this->specs.precision == 0 &&
(t == presentation_type::dec || t == presentation_type::oct ||
t == presentation_type::hex)) {
write_zero_with_zero_precision();
return;
}
write(value);
return;
}
Expand Down Expand Up @@ -490,7 +510,10 @@ void vprintf(buffer<Char>& buf, basic_string_view<Char> format,
str, to_unsigned(nul != str_end ? nul - str : specs.precision));
arg = sv;
}
if (specs.alt() && arg.visit(is_zero_int())) specs.clear_alt();
// '#' has no effect on a zero value except for octal, where it forces a
// single '0'. The conversion specifier is not parsed yet, so remember it.
bool alt_zero = specs.alt() && arg.visit(is_zero_int());
if (alt_zero) specs.clear_alt();
if (specs.fill_unit<Char>() == '0') {
if (is_arithmetic_type(arg.type()) && specs.align() != align::left) {
specs.set_align(align::numeric);
Expand Down Expand Up @@ -550,6 +573,11 @@ void vprintf(buffer<Char>& buf, basic_string_view<Char> format,
if (specs.type() == presentation_type::none)
report_error("invalid format specifier");
if (upper) specs.set_upper();
// For '#o', C requires a single '0' when the value and the precision are
// both zero.
if (alt_zero && specs.type() == presentation_type::oct &&
specs.precision == 0)
specs.precision = 1;

start = it;

Expand Down
33 changes: 33 additions & 0 deletions test/printf-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,39 @@ TEST(printf_test, int_precision) {
EXPECT_PRINTF("00042 ", "%-#10.5o", 042);
}

// C99 7.21.6.1: the result of converting a zero value with a precision of zero
// is no characters.
TEST(printf_test, zero_int_with_zero_precision) {
EXPECT_PRINTF("", "%.0d", 0);
EXPECT_PRINTF("", "%.d", 0);
EXPECT_PRINTF("", "%.0i", 0);
EXPECT_PRINTF("", "%.0o", 0);
EXPECT_PRINTF("", "%.0u", 0);
EXPECT_PRINTF("", "%.0x", 0);
EXPECT_PRINTF("", "%.0X", 0);

// '#' forces a single '0' for octal, but has no effect on other conversions.
EXPECT_PRINTF("0", "%#.0o", 0);
EXPECT_PRINTF("", "%#.0x", 0);
EXPECT_PRINTF("", "%#.0X", 0);

// The sign, space and width still apply.
EXPECT_PRINTF("+", "%+.0d", 0);
EXPECT_PRINTF(" ", "% .0d", 0);
EXPECT_PRINTF(" ", "%5.0d", 0);
EXPECT_PRINTF(" ", "%-5.0d", 0);
EXPECT_PRINTF(" ", "%05.0d", 0);
EXPECT_PRINTF(" 0", "%#5.0o", 0);
EXPECT_PRINTF("0 ", "%#-5.0o", 0);

// A nonzero value or a nonzero precision is unaffected.
EXPECT_PRINTF("42", "%.0d", 42);
EXPECT_PRINTF("-42", "%.0d", -42);
EXPECT_PRINTF("ff", "%.0x", 255);
EXPECT_PRINTF("0", "%.1d", 0);
EXPECT_PRINTF("00", "%.2d", 0);
}

TEST(printf_test, float_precision) {
char buffer[256];
safe_sprintf(buffer, "%.3e", 1234.5678);
Expand Down
Loading