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
24 changes: 19 additions & 5 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -1907,11 +1907,19 @@ template <typename Char, typename OutputIt>
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<Char>(out, specs, 1, [=](reserve_iterator<OutputIt> it) {
if (is_debug) return write_escaped_char(it, value);
*it++ = value;
return it;
});
size_t size = 1;
if (is_debug) {
counting_buffer<Char> buf;
write_escaped_char(basic_appender<Char>(buf), value);
size = buf.count();
}
return write_padded<Char>(out, specs, size,
[=](reserve_iterator<OutputIt> it) {
if (is_debug)
return write_escaped_char(it, value);
*it++ = value;
return it;
});
}

template <typename Char> class digit_grouping {
Expand Down Expand Up @@ -2269,6 +2277,12 @@ FMT_CONSTEXPR auto write(OutputIt out, basic_string_view<Char> 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<OutputIt> underlying_iterator;
size_t bound;
Expand Down
2 changes: 2 additions & 0 deletions test/format-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"("****)");
Expand Down Expand Up @@ -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");
}

Expand Down