Skip to content

Commit

Permalink
Improve utf-8 detection
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed May 12, 2024
1 parent b7809f9 commit 3460b30
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
8 changes: 6 additions & 2 deletions include/fmt/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -420,15 +420,19 @@ struct is_std_string_like<T, void_t<decltype(std::declval<T>().find_first_of(
typename T::value_type(), 0))>>
: std::true_type {};

FMT_CONSTEXPR inline auto is_utf8() -> bool {
FMT_CONSTEXPR inline auto is_utf8_enabled() -> 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;
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;
return utf8;
}

FMT_CONSTEXPR inline auto is_utf8() -> bool {
return FMT_UNICODE || is_utf8_enabled();
}

template <typename Char> FMT_CONSTEXPR auto length(const Char* s) -> size_t {
Expand Down
12 changes: 5 additions & 7 deletions include/fmt/xchar.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@
FMT_BEGIN_NAMESPACE
namespace detail {

#ifdef __cpp_char8_t
using char8_type = char8_t;
#else
enum char8_type : unsigned char {};
#endif

template <typename T>
using is_exotic_char = bool_constant<!std::is_same<T, char>::value>;

Expand Down Expand Up @@ -83,10 +77,14 @@ inline auto runtime(wstring_view s) -> runtime_format_string<wchar_t> {
#endif

template <> struct is_char<wchar_t> : std::true_type {};
template <> struct is_char<detail::char8_type> : std::true_type {};
template <> struct is_char<char16_t> : std::true_type {};
template <> struct is_char<char32_t> : std::true_type {};

#ifdef __cpp_char8_t
template <>
struct is_char<char8_t> : bool_constant<detail::is_utf8_enabled()> {};
#endif

template <typename... T>
constexpr auto make_wformat_args(T&... args)
-> decltype(fmt::make_format_args<wformat_context>(args...)) {
Expand Down

0 comments on commit 3460b30

Please sign in to comment.