From e17de67514f826d2902822349fcff00d441afd28 Mon Sep 17 00:00:00 2001 From: Dimitrij Mijoski Date: Mon, 18 Jul 2022 15:58:17 +0200 Subject: [PATCH] Make Unicode handing when writing to std::ostream more robust. Calls to print(ostream&) in the special Unicode case on Windows fallback to writing via ostream::write instead of fwrite(). --- include/fmt/ostream.h | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/include/fmt/ostream.h b/include/fmt/ostream.h index ced05f75..e6c8256e 100644 --- a/include/fmt/ostream.h +++ b/include/fmt/ostream.h @@ -81,13 +81,14 @@ template class filebuf_access; -inline bool write(std::filebuf& buf, fmt::string_view data) { - FILE* f = get_file(buf); - if (!f) return false; - print(f, data); - return true; +inline bool write_ostream_msvc_unicode(std::ostream& os, + fmt::string_view data) { + if (auto* buf = dynamic_cast(os.rdbuf())) + if (FILE* f = get_file(*buf)) return write_console_on_windows(f, data); + return false; } -inline bool write(std::wfilebuf&, fmt::basic_string_view) { +inline bool write_ostream_msvc_unicode(std::wostream&, + fmt::basic_string_view) { return false; } @@ -95,10 +96,8 @@ inline bool write(std::wfilebuf&, fmt::basic_string_view) { // It is a separate function rather than a part of vprint to simplify testing. template void write_buffer(std::basic_ostream& os, buffer& buf) { - if (const_check(FMT_MSC_VERSION)) { - auto filebuf = dynamic_cast*>(os.rdbuf()); - if (filebuf && write(*filebuf, {buf.data(), buf.size()})) return; - } + if (const_check(FMT_MSC_VERSION)) + if (write_ostream_msvc_unicode(os, {buf.data(), buf.size()})) return; const Char* buf_data = buf.data(); using unsigned_streamsize = std::make_unsigned::type; unsigned_streamsize size = buf.size();