Demacrify UTF-8 check

This commit is contained in:
Victor Zverovich 2024-09-13 18:38:08 -07:00
parent 463fe65f17
commit 0335312320
7 changed files with 19 additions and 19 deletions

View File

@ -429,21 +429,20 @@ struct is_std_string_like<T, void_t<decltype(std::declval<T>().find_first_of(
: std::is_convertible<decltype(std::declval<T>().data()),
const typename T::value_type*> {};
// Returns true iff the literal encoding is UTF-8.
constexpr auto is_utf8_enabled() -> bool { return "\u00A7"[1] == '\xA7'; }
// It is a macro for better debug codegen without if constexpr.
#define FMT_USE_UTF8 (!FMT_MSC_VERSION || fmt::detail::is_utf8_enabled())
template <typename T> constexpr const char* narrow(const T*) { return nullptr; }
constexpr FMT_ALWAYS_INLINE const char* narrow(const char* s) { return s; }
// Check if the literal encoding is UTF-8.
enum { is_utf8_enabled = "\u00A7"[1] == '\xA7' };
enum { use_utf8 = !FMT_MSC_VERSION || is_utf8_enabled };
#ifndef FMT_UNICODE
# define FMT_UNICODE 1
#endif
static_assert(!FMT_UNICODE || FMT_USE_UTF8,
static_assert(!FMT_UNICODE || use_utf8,
"Unicode support requires compiling with /utf-8");
template <typename T> constexpr const char* narrow(const T*) { return nullptr; }
constexpr FMT_ALWAYS_INLINE const char* narrow(const char* s) { return s; }
template <typename Char>
FMT_CONSTEXPR auto compare(const Char* s1, const Char* s2, std::size_t n)
-> int {
@ -2931,7 +2930,8 @@ FMT_API void vprint_buffered(FILE* f, string_view fmt, format_args args);
template <typename... T>
FMT_INLINE void print(format_string<T...> fmt, T&&... args) {
vargs<T...> va = {{args...}};
if (!FMT_USE_UTF8) return detail::vprint_mojibake(stdout, fmt.str, va, false);
if (!detail::use_utf8)
return detail::vprint_mojibake(stdout, fmt.str, va, false);
return detail::is_locking<T...>() ? vprint_buffered(stdout, fmt.str, va)
: vprint(fmt.str, va);
}
@ -2947,7 +2947,7 @@ FMT_INLINE void print(format_string<T...> fmt, T&&... args) {
template <typename... T>
FMT_INLINE void print(FILE* f, format_string<T...> fmt, T&&... args) {
vargs<T...> va = {{args...}};
if (!FMT_USE_UTF8) return detail::vprint_mojibake(f, fmt.str, va, false);
if (!detail::use_utf8) return detail::vprint_mojibake(f, fmt.str, va, false);
return detail::is_locking<T...>() ? vprint_buffered(f, fmt.str, va)
: vprint(f, fmt.str, va);
}
@ -2957,7 +2957,7 @@ FMT_INLINE void print(FILE* f, format_string<T...> fmt, T&&... args) {
template <typename... T>
FMT_INLINE void println(FILE* f, format_string<T...> fmt, T&&... args) {
vargs<T...> va = {{args...}};
return FMT_USE_UTF8 ? vprintln(f, fmt.str, va)
return detail::use_utf8 ? vprintln(f, fmt.str, va)
: detail::vprint_mojibake(f, fmt.str, va, true);
}

View File

@ -353,7 +353,7 @@ void write_codecvt(codecvt_result<CodeUnit>& out, string_view in,
template <typename OutputIt>
auto write_encoded_tm_str(OutputIt out, string_view in, const std::locale& loc)
-> OutputIt {
if (FMT_USE_UTF8 && loc != get_classic_locale()) {
if (detail::use_utf8 && loc != get_classic_locale()) {
// char16_t and char32_t codecvts are broken in MSVC (linkage errors) and
// gcc-4.
#if FMT_MSC_VERSION != 0 || \
@ -648,7 +648,7 @@ FMT_CONSTEXPR inline auto get_units() -> const char* {
if (std::is_same<Period, std::pico>::value) return "ps";
if (std::is_same<Period, std::nano>::value) return "ns";
if (std::is_same<Period, std::micro>::value)
return FMT_USE_UTF8 ? "µs" : "us";
return detail::use_utf8 ? "µs" : "us";
if (std::is_same<Period, std::milli>::value) return "ms";
if (std::is_same<Period, std::centi>::value) return "cs";
if (std::is_same<Period, std::deci>::value) return "ds";

View File

@ -1862,7 +1862,7 @@ auto find_escape(const Char* begin, const Char* end)
inline auto find_escape(const char* begin, const char* end)
-> find_escape_result<char> {
if (!FMT_USE_UTF8) return find_escape<char>(begin, end);
if (!detail::use_utf8) return find_escape<char>(begin, end);
auto result = find_escape_result<char>{end, nullptr, 0};
for_each_codepoint(string_view(begin, to_unsigned(end - begin)),
[&](uint32_t cp, string_view sv) {

View File

@ -142,7 +142,7 @@ inline void vprint(std::ostream& os, string_view fmt, format_args args) {
FMT_EXPORT template <typename... T>
void print(std::ostream& os, format_string<T...> fmt, T&&... args) {
fmt::vargs<T...> vargs = {{args...}};
if (FMT_USE_UTF8) return vprint(os, fmt.str, vargs);
if (detail::use_utf8) return vprint(os, fmt.str, vargs);
auto buffer = memory_buffer();
detail::vformat_to(buffer, fmt.str, vargs);
detail::write_buffer(os, buffer);

View File

@ -118,7 +118,7 @@ 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()> {};
struct is_char<char8_t> : bool_constant<detail::is_utf8_enabled> {};
#endif
template <typename... T>

View File

@ -560,7 +560,7 @@ TEST(ranges_test, escape) {
EXPECT_EQ(fmt::format("{}", vec{"\x7f"}), "[\"\\x7f\"]");
EXPECT_EQ(fmt::format("{}", vec{"n\xcc\x83"}), "[\"n\xcc\x83\"]");
if (FMT_USE_UTF8) {
if (fmt::detail::use_utf8) {
EXPECT_EQ(fmt::format("{}", vec{"\xcd\xb8"}), "[\"\\u0378\"]");
// Unassigned Unicode code points.
EXPECT_EQ(fmt::format("{}", vec{"\xf0\xaa\x9b\x9e"}), "[\"\\U0002a6de\"]");

View File

@ -15,7 +15,7 @@
using testing::Contains;
TEST(unicode_test, use_utf8) { EXPECT_TRUE(FMT_USE_UTF8); }
TEST(unicode_test, use_utf8) { EXPECT_TRUE(fmt::detail::use_utf8); }
TEST(unicode_test, legacy_locale) {
auto loc = get_locale("be_BY.CP1251", "Belarusian_Belarus.1251");