Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support color printing strings with zero bytes #1356

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 1 addition & 2 deletions include/fmt/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -503,8 +503,7 @@ void vprint(std::FILE* f, const text_style& ts, const S& format,
basic_format_args<buffer_context<Char> > args) {
basic_memory_buffer<Char> buf;
internal::vformat_to(buf, ts, to_string_view(format), args);
buf.push_back(Char(0));
internal::fputs(buf.data(), f);
std::fwrite(buf.data(), 1, buf.size(), f);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To handle wchar_t correctly on Windows this should be fputws if Char is wchar_t (#1243). This will truncate output on NUL chars but I don't think we should worry about this. Also use fwrite_fully for char to handle I/O errors.

}

/**
Expand Down
2 changes: 2 additions & 0 deletions test/color-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ TEST(ColorsTest, ColorsPrint) {
EXPECT_WRITE(stdout,
fmt::print(bg(fmt::terminal_color::bright_magenta), "tbmagenta"),
"\x1b[105mtbmagenta\x1b[0m");
EXPECT_WRITE(stdout, fmt::print(fmt::emphasis::bold, "{}", std::string("a\0b", 3)),
std::string("\x1b[1ma\0b\x1b[0m", 11));
}

TEST(ColorsTest, Format) {
Expand Down