mirror of
https://github.com/fmtlib/fmt.git
synced 2025-03-12 07:13:29 +00:00
Cleanup unicode checks
This commit is contained in:
parent
3460b30fd5
commit
7d6ae972b9
@ -420,20 +420,17 @@ 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_enabled() -> bool {
|
||||
FMT_MSC_WARNING(suppress : 4566) constexpr unsigned char section[] = "\u00A7";
|
||||
// Returns true iff the literal encoding is UTF-8.
|
||||
constexpr auto is_utf8_enabled() -> bool {
|
||||
// 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 utf8;
|
||||
return sizeof("\u00A7") == 3 && uchar("\u00A7"[0]) == 0xC2 &&
|
||||
uchar("\u00A7"[1]) == 0xA7;
|
||||
}
|
||||
constexpr auto use_utf8() -> bool { return FMT_UNICODE || is_utf8_enabled(); }
|
||||
|
||||
FMT_CONSTEXPR inline auto is_utf8() -> bool {
|
||||
return FMT_UNICODE || is_utf8_enabled();
|
||||
}
|
||||
static_assert(!(FMT_UNICODE && FMT_MSC_VERSION && !is_utf8_enabled()),
|
||||
"Unicode support requires compiling with /utf-8");
|
||||
|
||||
template <typename Char> FMT_CONSTEXPR auto length(const Char* s) -> size_t {
|
||||
size_t len = 0;
|
||||
@ -3027,7 +3024,7 @@ FMT_API void vprintln(FILE* f, string_view fmt, format_args args);
|
||||
template <typename... T>
|
||||
FMT_INLINE void print(format_string<T...> fmt, T&&... args) {
|
||||
const auto& vargs = fmt::make_format_args(args...);
|
||||
if (!detail::is_utf8()) return detail::vprint_mojibake(stdout, fmt, vargs);
|
||||
if (!detail::use_utf8()) return detail::vprint_mojibake(stdout, fmt, vargs);
|
||||
return detail::is_locking<T...>() ? vprint(fmt, vargs)
|
||||
: vprint_locked(stdout, fmt, vargs);
|
||||
}
|
||||
@ -3045,7 +3042,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) {
|
||||
const auto& vargs = fmt::make_format_args(args...);
|
||||
if (!detail::is_utf8()) return detail::vprint_mojibake(f, fmt, vargs);
|
||||
if (!detail::use_utf8()) return detail::vprint_mojibake(f, fmt, vargs);
|
||||
return detail::is_locking<T...>() ? vprint(f, fmt, vargs)
|
||||
: vprint_locked(f, fmt, vargs);
|
||||
}
|
||||
@ -3057,8 +3054,8 @@ 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) {
|
||||
const auto& vargs = fmt::make_format_args(args...);
|
||||
return detail::is_utf8() ? vprintln(f, fmt, vargs)
|
||||
: detail::vprint_mojibake(f, fmt, vargs, true);
|
||||
return detail::use_utf8() ? vprintln(f, fmt, vargs)
|
||||
: detail::vprint_mojibake(f, fmt, vargs, true);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -397,7 +397,7 @@ void write_codecvt(codecvt_result<CodeUnit>& out, string_view in_buf,
|
||||
template <typename OutputIt>
|
||||
auto write_encoded_tm_str(OutputIt out, string_view in, const std::locale& loc)
|
||||
-> OutputIt {
|
||||
if (detail::is_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 || \
|
||||
|
@ -1800,7 +1800,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 (!is_utf8()) return find_escape<char>(begin, end);
|
||||
if (!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) {
|
||||
|
@ -183,7 +183,7 @@ void vprint(std::basic_ostream<Char>& os,
|
||||
FMT_EXPORT template <typename... T>
|
||||
void print(std::ostream& os, format_string<T...> fmt, T&&... args) {
|
||||
const auto& vargs = fmt::make_format_args(args...);
|
||||
if (detail::is_utf8())
|
||||
if (detail::use_utf8())
|
||||
vprint(os, fmt, vargs);
|
||||
else
|
||||
detail::vprint_directly(os, fmt, vargs);
|
||||
|
@ -530,7 +530,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::detail::is_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\"]");
|
||||
|
@ -15,7 +15,7 @@
|
||||
|
||||
using testing::Contains;
|
||||
|
||||
TEST(unicode_test, is_utf8) { EXPECT_TRUE(fmt::detail::is_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");
|
||||
|
Loading…
x
Reference in New Issue
Block a user