Skip to content

Commit

Permalink
Fix Unicode handling when writing to an ostream
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Apr 10, 2022
1 parent c55175a commit 8751a03
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions include/fmt/ostream.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#ifndef FMT_OSTREAM_H_
#define FMT_OSTREAM_H_

#include <fstream>
#include <ostream>

#include "format.h"
Expand Down Expand Up @@ -50,10 +51,42 @@ struct is_streamable<
(std::is_convertible<T, int>::value && !std::is_enum<T>::value)>>
: std::false_type {};

template <typename Char> FILE* get_file(std::basic_filebuf<Char>&) {
return nullptr;
}

#if FMT_MSC_VER
FILE* get_file(std::filebuf& buf);

// Generate a unique explicit instantion in every translation unit using a tag
// type in an anonymous namespace.
namespace {
struct filebuf_access_tag {};
} // namespace
template <typename Tag, typename FileMemberPtr, FileMemberPtr file>
class filebuf_access {
friend FILE* get_file(std::filebuf& buf) { return buf.*file; }
};
template class filebuf_access<filebuf_access_tag,
decltype(&std::filebuf::_Myfile),
&std::filebuf::_Myfile>;
#endif

inline bool write(std::filebuf& buf, fmt::string_view data) {
print(get_file(buf), data);
return true;
}
inline bool write(std::wfilebuf&, 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 (auto filebuf = dynamic_cast<std::basic_filebuf<Char>*>(os.rdbuf())) {
if (write(*filebuf, {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();
Expand Down

0 comments on commit 8751a03

Please sign in to comment.