From 08ca40ea9139b9066843e93e308927dc7bb69819 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sun, 22 Mar 2020 07:57:56 -0700 Subject: [PATCH] Detect /utf-8 in MSVC --- include/fmt/core.h | 43 +++++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/include/fmt/core.h b/include/fmt/core.h index d1242b59..74b9b08d 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -240,9 +240,9 @@ #endif #ifndef FMT_UNICODE -# define FMT_UNICODE 0 +# define FMT_UNICODE !FMT_MSC_VER #endif -#if FMT_UNICODE +#if FMT_UNICODE && FMT_MSC_VER # pragma execution_character_set("utf-8") #endif @@ -324,6 +324,13 @@ FMT_CONSTEXPR typename std::make_unsigned::type to_unsigned(Int value) { return static_cast::type>(value); } +constexpr unsigned char micro[] = "\u00B5"; + +constexpr bool is_utf8() { + return FMT_UNICODE || + (sizeof(micro) == 3 && micro[0] == 0xC2 && micro[1] == 0xB5); +} + #ifdef __cpp_char8_t using char8_type = char8_t; #else @@ -1648,6 +1655,10 @@ typename buffer_context::iterator vformat_to( basic_format_args>> args); FMT_API void vprint_mojibake(std::FILE*, string_view, format_args); + +#ifndef _WIN32 +inline void vprint_mojibake(std::FILE*, string_view, format_args) {} +#endif } // namespace internal /** @@ -1738,14 +1749,12 @@ FMT_API void vprint(std::FILE*, string_view, format_args); template ::value)> inline void print(std::FILE* f, const S& format_str, Args&&... args) { -#if !defined(_WIN32) || FMT_UNICODE - vprint(f, to_string_view(format_str), - internal::make_args_checked(format_str, args...)); -#else - internal::vprint_mojibake( - f, to_string_view(format_str), - internal::make_args_checked(format_str, args...)); -#endif + return internal::is_utf8() + ? vprint(f, to_string_view(format_str), + internal::make_args_checked(format_str, args...)) + : internal::vprint_mojibake( + f, to_string_view(format_str), + internal::make_args_checked(format_str, args...)); } /** @@ -1762,14 +1771,12 @@ inline void print(std::FILE* f, const S& format_str, Args&&... args) { template ::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(format_str, args...)); -#else - internal::vprint_mojibake( - stdout, to_string_view(format_str), - internal::make_args_checked(format_str, args...)); -#endif + return internal::is_utf8() + ? vprint(to_string_view(format_str), + internal::make_args_checked(format_str, args...)) + : internal::vprint_mojibake( + stdout, to_string_view(format_str), + internal::make_args_checked(format_str, args...)); } FMT_END_NAMESPACE