Skip to content

Commit

Permalink
Detect /utf-8 in MSVC
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Mar 22, 2020
1 parent dd97f49 commit 08ca40e
Showing 1 changed file with 25 additions and 18 deletions.
43 changes: 25 additions & 18 deletions include/fmt/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -324,6 +324,13 @@ FMT_CONSTEXPR typename std::make_unsigned<Int>::type to_unsigned(Int value) {
return static_cast<typename std::make_unsigned<Int>::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
Expand Down Expand Up @@ -1648,6 +1655,10 @@ typename buffer_context<Char>::iterator vformat_to(
basic_format_args<buffer_context<type_identity_t<Char>>> 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

/**
Expand Down Expand Up @@ -1738,14 +1749,12 @@ FMT_API void vprint(std::FILE*, string_view, format_args);
template <typename S, typename... Args,
FMT_ENABLE_IF(internal::is_string<S>::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<Args...>(format_str, args...));
#else
internal::vprint_mojibake(
f, to_string_view(format_str),
internal::make_args_checked<Args...>(format_str, args...));
#endif
return internal::is_utf8()
? vprint(f, to_string_view(format_str),
internal::make_args_checked<Args...>(format_str, args...))
: internal::vprint_mojibake(
f, to_string_view(format_str),
internal::make_args_checked<Args...>(format_str, args...));
}

/**
Expand All @@ -1762,14 +1771,12 @@ 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
internal::vprint_mojibake(
stdout, to_string_view(format_str),
internal::make_args_checked<Args...>(format_str, args...));
#endif
return internal::is_utf8()
? vprint(to_string_view(format_str),
internal::make_args_checked<Args...>(format_str, args...))
: internal::vprint_mojibake(
stdout, to_string_view(format_str),
internal::make_args_checked<Args...>(format_str, args...));
}
FMT_END_NAMESPACE

Expand Down

0 comments on commit 08ca40e

Please sign in to comment.