mirror of
https://github.com/fmtlib/fmt.git
synced 2025-07-31 03:07:36 +02:00
Fix std::byte formatting with compile-time API (#2072)
* add test for byte formatting with `FMT_COMPILE` * fix byte formatting with `FMT_COMPILE`, use `__cpp_lib_byte` macro * use is not custom mapped type check * workaround MSVC bug
This commit is contained in:
@ -2113,9 +2113,14 @@ FMT_CONSTEXPR OutputIt write(OutputIt out, T value) {
|
|||||||
return base_iterator(out, it);
|
return base_iterator(out, it);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename Char, typename OutputIt, typename T,
|
// FMT_ENABLE_IF() condition separated to workaround MSVC bug
|
||||||
FMT_ENABLE_IF(std::is_enum<T>::value &&
|
template <
|
||||||
!std::is_same<T, Char>::value)>
|
typename Char, typename OutputIt, typename T,
|
||||||
|
bool check =
|
||||||
|
std::is_enum<T>::value && !std::is_same<T, Char>::value &&
|
||||||
|
mapped_type_constant<T, basic_format_context<OutputIt, Char>>::value !=
|
||||||
|
type::custom_type,
|
||||||
|
FMT_ENABLE_IF(check)>
|
||||||
FMT_CONSTEXPR OutputIt write(OutputIt out, T value) {
|
FMT_CONSTEXPR OutputIt write(OutputIt out, T value) {
|
||||||
return write<Char>(
|
return write<Char>(
|
||||||
out, static_cast<typename std::underlying_type<T>::type>(value));
|
out, static_cast<typename std::underlying_type<T>::type>(value));
|
||||||
@ -3570,7 +3575,7 @@ FMT_FORMAT_AS(Char*, const Char*);
|
|||||||
FMT_FORMAT_AS(std::basic_string<Char>, basic_string_view<Char>);
|
FMT_FORMAT_AS(std::basic_string<Char>, basic_string_view<Char>);
|
||||||
FMT_FORMAT_AS(std::nullptr_t, const void*);
|
FMT_FORMAT_AS(std::nullptr_t, const void*);
|
||||||
FMT_FORMAT_AS(detail::std_string_view<Char>, basic_string_view<Char>);
|
FMT_FORMAT_AS(detail::std_string_view<Char>, basic_string_view<Char>);
|
||||||
#if __cplusplus >= 201703L
|
#ifdef __cpp_lib_byte
|
||||||
FMT_FORMAT_AS(std::byte, unsigned);
|
FMT_FORMAT_AS(std::byte, unsigned);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -130,6 +130,9 @@ TEST(CompileTest, FormatDefault) {
|
|||||||
EXPECT_EQ("foo", fmt::format(FMT_COMPILE("{}"), "foo"));
|
EXPECT_EQ("foo", fmt::format(FMT_COMPILE("{}"), "foo"));
|
||||||
EXPECT_EQ("foo", fmt::format(FMT_COMPILE("{}"), std::string("foo")));
|
EXPECT_EQ("foo", fmt::format(FMT_COMPILE("{}"), std::string("foo")));
|
||||||
EXPECT_EQ("foo", fmt::format(FMT_COMPILE("{}"), test_formattable()));
|
EXPECT_EQ("foo", fmt::format(FMT_COMPILE("{}"), test_formattable()));
|
||||||
|
# ifdef __cpp_lib_byte
|
||||||
|
EXPECT_EQ("42", fmt::format(FMT_COMPILE("{}"), std::byte{42}));
|
||||||
|
# endif
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(CompileTest, FormatWideString) {
|
TEST(CompileTest, FormatWideString) {
|
||||||
|
@ -1762,7 +1762,7 @@ TEST(FormatTest, JoinArg) {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
#if __cplusplus >= 201703L
|
#ifdef __cpp_lib_byte
|
||||||
TEST(FormatTest, JoinBytes) {
|
TEST(FormatTest, JoinBytes) {
|
||||||
std::vector<std::byte> v = {std::byte(1), std::byte(2), std::byte(3)};
|
std::vector<std::byte> v = {std::byte(1), std::byte(2), std::byte(3)};
|
||||||
EXPECT_EQ("1, 2, 3", fmt::format("{}", fmt::join(v, ", ")));
|
EXPECT_EQ("1, 2, 3", fmt::format("{}", fmt::join(v, ", ")));
|
||||||
|
Reference in New Issue
Block a user