diff --git a/include/fmt/format.h b/include/fmt/format.h index 4697d3c7636f..84b34cfbe460 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -1907,11 +1907,19 @@ template FMT_CONSTEXPR auto write_char(OutputIt out, Char value, const format_specs& specs) -> OutputIt { bool is_debug = specs.type() == presentation_type::debug; - return write_padded(out, specs, 1, [=](reserve_iterator it) { - if (is_debug) return write_escaped_char(it, value); - *it++ = value; - return it; - }); + size_t size = 1; + if (is_debug) { + counting_buffer buf; + write_escaped_char(basic_appender(buf), value); + size = buf.count(); + } + return write_padded(out, specs, size, + [=](reserve_iterator it) { + if (is_debug) + return write_escaped_char(it, value); + *it++ = value; + return it; + }); } template class digit_grouping { @@ -2269,6 +2277,12 @@ FMT_CONSTEXPR auto write(OutputIt out, basic_string_view s, return false; }); + if (is_debug && s.size() == 0 && specs.precision != 0 && + display_width < display_width_limit) { + ++display_width; + ++size; + } + struct bounded_output_iterator { reserve_iterator underlying_iterator; size_t bound; diff --git a/test/format-test.cc b/test/format-test.cc index ea65f0a3cec8..bd3c90f2a722 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -941,6 +941,7 @@ TEST(format_test, width) { TEST(format_test, debug_presentation) { EXPECT_EQ(fmt::format("{:?}", ""), R"("")"); + EXPECT_EQ(fmt::format("{:1?}", ""), R"("")"); EXPECT_EQ(fmt::format("{:*<5.0?}", "\n"), R"(*****)"); EXPECT_EQ(fmt::format("{:*<5.1?}", "\n"), R"("****)"); @@ -1683,6 +1684,7 @@ TEST(format_test, format_char) { EXPECT_EQ(fmt::format("{}", '\n'), "\n"); EXPECT_EQ(fmt::format("{:?}", '\n'), "'\\n'"); + EXPECT_EQ(fmt::format("{:6?}", 'a'), "'a' "); EXPECT_EQ(fmt::format("{:x}", '\xff'), "ff"); }