Improve UTF-8 handling on Windows

This commit is contained in:
Victor Zverovich 2019-12-23 16:18:48 -08:00
parent dac9a7f99d
commit 9f6434dcde
2 changed files with 22 additions and 6 deletions

View File

@ -1507,8 +1507,10 @@ inline std::basic_string<Char> format(const S& format_str, Args&&... args) {
{internal::make_args_checked<Args...>(format_str, args...)});
}
FMT_API void vprint(std::FILE* f, string_view format_str, format_args args);
FMT_API void vprint(string_view format_str, format_args args);
FMT_API void vprint(std::FILE* f, string_view format_str, format_args args);
FMT_API void vprint_mojibake(std::FILE* f, string_view format_str,
format_args args);
/**
\rst
@ -1539,8 +1541,13 @@ inline void print(std::FILE* f, const S& format_str, Args&&... args) {
template <typename S, typename... Args,
FMT_ENABLE_IF(internal::is_string<S>::value)>
inline void print(const S& format_str, Args&&... args) {
#if !defined(_WIN32) || FMT_UNICODE
vprint(to_string_view(format_str),
internal::make_args_checked<Args...>(format_str, args...));
#else
vprint_mojibake(stdout, to_string_view(format_str),
internal::make_args_checked<Args...>(format_str, args...));
#endif
}
FMT_END_NAMESPACE

View File

@ -22,8 +22,8 @@
#endif
#if FMT_UNICODE
#include <windows.h>
#include <io.h>
# include <io.h>
# include <windows.h>
#endif
#ifdef _MSC_VER
@ -1350,9 +1350,8 @@ FMT_FUNC void vprint(std::FILE* f, string_view format_str, format_args args) {
if (_isatty(fd)) {
internal::utf8_to_utf16 u16(string_view(buffer.data(), buffer.size()));
auto written = DWORD();
if (!WriteConsoleW(
reinterpret_cast<HANDLE>(_get_osfhandle(fd)),
u16.c_str(), u16.size(), &written, nullptr)) {
if (!WriteConsoleW(reinterpret_cast<HANDLE>(_get_osfhandle(fd)),
u16.c_str(), u16.size(), &written, nullptr)) {
throw format_error("failed to write to console");
}
return;
@ -1361,6 +1360,16 @@ FMT_FUNC void vprint(std::FILE* f, string_view format_str, format_args args) {
internal::fwrite_fully(buffer.data(), 1, buffer.size(), f);
}
#ifdef _WIN32
FMT_FUNC void vprint_mojibake(std::FILE* f, string_view format_str,
format_args args) {
memory_buffer buffer;
internal::vformat_to(buffer, format_str,
basic_format_args<buffer_context<char>>(args));
internal::fwrite_fully(buffer.data(), 1, buffer.size(), f);
}
#endif
FMT_FUNC void vprint(string_view format_str, format_args args) {
vprint(stdout, format_str, args);
}