From 5fd371ebe16bb2c39eb96d82fa0661ba82b7e0b2 Mon Sep 17 00:00:00 2001 From: Dimitrij Mijoski Date: Sat, 23 Jul 2022 04:09:22 +0200 Subject: [PATCH] Refactor --- include/fmt/format-inl.h | 4 ++-- include/fmt/format.h | 2 +- include/fmt/ostream.h | 44 ++++++++++++++-------------------------- 3 files changed, 18 insertions(+), 32 deletions(-) diff --git a/include/fmt/format-inl.h b/include/fmt/format-inl.h index 61256d2c..22b1ec8d 100644 --- a/include/fmt/format-inl.h +++ b/include/fmt/format-inl.h @@ -1479,7 +1479,7 @@ using dword = conditional_t; extern "C" __declspec(dllimport) int __stdcall WriteConsoleW( // void*, const void*, dword, dword*, void*); -FMT_FUNC bool write_console_on_windows(std::FILE* f, string_view text) { +FMT_FUNC bool write_console(std::FILE* f, string_view text) { auto fd = _fileno(f); if (_isatty(fd)) { detail::utf8_to_utf16 u16(string_view(text.data(), text.size())); @@ -1500,7 +1500,7 @@ FMT_FUNC bool write_console_on_windows(std::FILE* f, string_view text) { FMT_FUNC void print(std::FILE* f, string_view text) { #ifdef _WIN32 - if (write_console_on_windows(f, text)) return; + if (write_console(f, text)) return; #endif detail::fwrite_fully(text.data(), 1, text.size(), f); } diff --git a/include/fmt/format.h b/include/fmt/format.h index ab20887b..0615666d 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -922,7 +922,7 @@ struct is_contiguous> : std::true_type { namespace detail { #ifdef _WIN32 -FMT_API bool write_console_on_windows(std::FILE* f, string_view text); +FMT_API bool write_console(std::FILE* f, string_view text); #endif FMT_API void print(std::FILE*, string_view); } diff --git a/include/fmt/ostream.h b/include/fmt/ostream.h index acbf507a..fcab4dd3 100644 --- a/include/fmt/ostream.h +++ b/include/fmt/ostream.h @@ -85,22 +85,11 @@ template class filebuf_access; -inline bool write_ostream_msvc_unicode(std::ostream& os, - fmt::string_view data) { -#ifdef _WIN32 +inline bool write_ostream_unicode(std::ostream& os, fmt::string_view data) { +#if FMT_MSC_VERSION if (auto* buf = dynamic_cast(os.rdbuf())) - if (FILE* f = get_file(*buf)) return write_console_on_windows(f, data); -#endif - return false; -} -inline bool write_ostream_msvc_unicode(std::wostream&, - fmt::basic_string_view) { - return false; -} - -#if defined(_WIN32) && defined(__GLIBCXX__) -inline bool write_ostream_gcc_mingw_unicode(std::ostream& os, - fmt::string_view data) { + if (FILE* f = get_file(*buf)) return write_console(f, data); +#elif defined(_WIN32) && defined(__GLIBCXX__) auto* rdbuf = os.rdbuf(); FILE* c_file; if (auto* fbuf = dynamic_cast<__gnu_cxx::stdio_sync_filebuf*>(rdbuf)) @@ -109,26 +98,23 @@ inline bool write_ostream_gcc_mingw_unicode(std::ostream& os, c_file = fbuf->file(); else return false; - if (c_file) return write_console_on_windows(c_file, data); - return false; -} - -inline bool write_ostream_gcc_mingw_unicode(std::wostream&, - fmt::basic_string_view) { - return false; -} + if (c_file) return write_console(c_file, data); +#else + (void)os; // suppress warning + (void)data; #endif + return false; +} +inline bool write_ostream_unicode(std::wostream&, + fmt::basic_string_view) { + return false; +} // Write the content of buf to os. // It is a separate function rather than a part of vprint to simplify testing. template void write_buffer(std::basic_ostream& os, buffer& buf) { - // TODO: check detail::is_utf8(). Here or bellow in print or in vprint? -#if FMT_MSC_VERSION - if (write_ostream_msvc_unicode(os, {buf.data(), buf.size()})) return; -#elif defined(_WIN32) && defined(__GLIBCXX__) - if (write_ostream_gcc_mingw_unicode(os, {buf.data(), buf.size()})) return; -#endif + if (write_ostream_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();