From b7809f91e2cc42d3bb693e742a28815832c6cb89 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sat, 11 May 2024 14:50:48 -0700 Subject: [PATCH] Enable Unicode support by default --- CMakeLists.txt | 6 ++++++ include/fmt/base.h | 7 +++++-- test/CMakeLists.txt | 3 +++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 767d30aa..037b7481 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -341,6 +341,12 @@ endif () add_library(fmt-header-only INTERFACE) add_library(fmt::fmt-header-only ALIAS fmt-header-only) +if (MSVC) + # Unicode support requires compiling with /utf-8. + target_compile_options(fmt PUBLIC /utf-8) + target_compile_options(fmt-header-only INTERFACE /utf-8) +endif () + target_compile_definitions(fmt-header-only INTERFACE FMT_HEADER_ONLY=1) target_compile_features(fmt-header-only INTERFACE cxx_std_11) diff --git a/include/fmt/base.h b/include/fmt/base.h index d732d6bb..2de369f5 100644 --- a/include/fmt/base.h +++ b/include/fmt/base.h @@ -424,8 +424,11 @@ FMT_CONSTEXPR inline auto is_utf8() -> bool { FMT_MSC_WARNING(suppress : 4566) constexpr unsigned char section[] = "\u00A7"; // Avoid an MSVC sign extension bug: https://github.com/fmtlib/fmt/pull/2297. using uchar = unsigned char; - return FMT_UNICODE || (sizeof(section) == 3 && uchar(section[0]) == 0xC2 && - uchar(section[1]) == 0xA7); + constexpr bool utf8 = sizeof(section) == 3 && uchar(section[0]) == 0xC2 && + uchar(section[1]) == 0xA7; + static_assert(utf8 || !FMT_MSC_VERSION, + "Unicode support requires compiling with /utf-8"); + return FMT_UNICODE || utf8; } template FMT_CONSTEXPR auto length(const Char* s) -> size_t { diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 0e903dd9..6f7214a8 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -146,6 +146,9 @@ if (NOT MSVC_STATIC_RUNTIME) if (FMT_PEDANTIC) target_compile_options(posix-mock-test PRIVATE ${PEDANTIC_COMPILE_FLAGS}) endif () + if (MSVC) + target_compile_options(posix-mock-test PRIVATE /utf-8) + endif () add_test(NAME posix-mock-test COMMAND posix-mock-test) add_fmt_test(os-test) endif ()