Skip to content

Commit

Permalink
Fix wide print overload (#1609)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Mar 28, 2020
1 parent 2864e84 commit 2745e79
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
19 changes: 10 additions & 9 deletions include/fmt/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,8 @@ FMT_CONSTEXPR typename std::make_unsigned<Int>::type to_unsigned(Int value) {

constexpr unsigned char micro[] = "\u00B5";

constexpr bool is_utf8() {
return FMT_UNICODE ||
template <typename Char> constexpr bool is_unicode() {
return FMT_UNICODE || sizeof(Char) != 1 ||
(sizeof(micro) == 3 && micro[0] == 0xC2 && micro[1] == 0xB5);
}

Expand Down Expand Up @@ -1659,8 +1659,11 @@ typename buffer_context<Char>::iterator vformat_to(
buffer<Char>& buf, basic_string_view<Char> format_str,
basic_format_args<buffer_context<type_identity_t<Char>>> args);

FMT_API void vprint_mojibake(std::FILE*, string_view, format_args);
template <typename Char, typename Args,
FMT_ENABLE_IF(!std::is_same<Char, char>::value)>
inline void vprint_mojibake(std::FILE*, basic_string_view<Char>, const 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
Expand Down Expand Up @@ -1751,10 +1754,9 @@ FMT_API void vprint(std::FILE*, string_view, format_args);
fmt::print(stderr, "Don't {}!", "panic");
\endrst
*/
template <typename S, typename... Args,
FMT_ENABLE_IF(internal::is_string<S>::value)>
template <typename S, typename... Args, typename Char = char_t<S>>
inline void print(std::FILE* f, const S& format_str, Args&&... args) {
return internal::is_utf8()
return internal::is_unicode<Char>()
? vprint(f, to_string_view(format_str),
internal::make_args_checked<Args...>(format_str, args...))
: internal::vprint_mojibake(
Expand All @@ -1773,10 +1775,9 @@ inline void print(std::FILE* f, const S& format_str, Args&&... args) {
fmt::print("Elapsed time: {0:.2f} seconds", 1.23);
\endrst
*/
template <typename S, typename... Args,
FMT_ENABLE_IF(internal::is_string<S>::value)>
template <typename S, typename... Args, typename Char = char_t<S>>
inline void print(const S& format_str, Args&&... args) {
return internal::is_utf8()
return internal::is_unicode<Char>()
? vprint(to_string_view(format_str),
internal::make_args_checked<Args...>(format_str, args...))
: internal::vprint_mojibake(
Expand Down
2 changes: 2 additions & 0 deletions test/format-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1757,6 +1757,8 @@ TEST(FormatTest, Print) {
EXPECT_WRITE(stderr, fmt::print(stderr, "Don't {}!", "panic"),
"Don't panic!");
#endif
// Check that the wide print overload compiles.
if (fmt::internal::const_check(false)) fmt::print(L"test");
}

TEST(FormatTest, Variadic) {
Expand Down

0 comments on commit 2745e79

Please sign in to comment.