Repository: fmtlib/fmt
File: include/fmt/ostream.h (C/C++)

Code before:
struct filebuf_access_tag {};
}  // namespace
template <typename Tag, typename FileMemberPtr, FileMemberPtr file>
class filebuf_access {
  friend FILE* get_file(filebuf_type& buf) { return buf.*file; }
};
template class filebuf_access<filebuf_access_tag,
                              decltype(&filebuf_type::_Myfile),
                              &filebuf_type::_Myfile>;

inline bool write_ostream_msvc_unicode(std::ostream& os,
                                       fmt::string_view data) {
#ifdef _WIN32
  if (auto* buf = dynamic_cast<std::filebuf*>(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<wchar_t>) {
  return false;
}

#if defined(_WIN32) && defined(__GLIBCXX__)
inline bool write_ostream_gcc_mingw_unicode(std::ostream& os,
                                            fmt::string_view data) {
  auto* rdbuf = os.rdbuf();
  FILE* c_file;
  if (auto* fbuf = dynamic_cast<__gnu_cxx::stdio_sync_filebuf<char>*>(rdbuf))
    c_file = fbuf->file();
  else if (auto* fbuf = dynamic_cast<__gnu_cxx::stdio_filebuf<char>*>(rdbuf))
    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<wchar_t>) {
  return false;
}
#endif

// Write the content of buf to os.
// It is a separate function rather than a part of vprint to simplify testing.
template <typename Char>
void write_buffer(std::basic_ostream<Char>& os, buffer<Char>& 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;

Review comment (suggestion):
Please merge this function into `write_ostream_msvc_unicode` and keep the old name `write`. This should reduce the scope of conditional compilation a bit and remove redundant copy for wchar_t.

Code after:
struct filebuf_access_tag {};
}  // namespace
template <typename Tag, typename FileMemberPtr, FileMemberPtr file>
class filebuf_access {
  friend FILE* get_file(filebuf_type& buf) { return buf.*file; }
};
template class filebuf_access<filebuf_access_tag,
                              decltype(&filebuf_type::_Myfile),
                              &filebuf_type::_Myfile>;

inline bool write_ostream_unicode(std::ostream& os, fmt::string_view data) {
#if FMT_MSC_VERSION
  if (auto* buf = dynamic_cast<std::filebuf*>(os.rdbuf()))
    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<char>*>(rdbuf))
    c_file = fbuf->file();
  else if (auto* fbuf = dynamic_cast<__gnu_cxx::stdio_filebuf<char>*>(rdbuf))
    c_file = fbuf->file();
  else
    return false;
  if (c_file) return write_console(c_file, data);
#else
  ignore_unused(os);
  ignore_unused(data);
#endif
  return false;
}
inline bool write_ostream_unicode(std::wostream&,
                                  fmt::basic_string_view<wchar_t>) {
  return false;
}

// Write the content of buf to os.
// It is a separate function rather than a part of vprint to simplify testing.
template <typename Char>
void write_buffer(std::basic_ostream<Char>& os, buffer<Char>& buf) {
  if (write_ostream_unicode(os, {buf.data(), buf.size()})) return;
  const Char* buf_data = buf.data();
  using unsigned_streamsize = std::make_unsigned<std::streamsize>::type;
  unsigned_streamsize size = buf.size();
  unsigned_streamsize max_size = to_unsigned(max_value<std::streamsize>());
  do {
    unsigned_streamsize n = size <= max_size ? size : max_size;
    os.write(buf_data, static_cast<std::streamsize>(n));
    buf_data += n;
    size -= n;
  } while (size != 0);
}