mirror of
https://github.com/fmtlib/fmt.git
synced 2025-08-05 13:44:44 +02:00
Fix formatting of std::byte via format_as
This commit is contained in:
@@ -8,6 +8,7 @@
|
|||||||
#ifndef FMT_CORE_H_
|
#ifndef FMT_CORE_H_
|
||||||
#define FMT_CORE_H_
|
#define FMT_CORE_H_
|
||||||
|
|
||||||
|
#include <cstddef> // std::byte
|
||||||
#include <cstdio> // std::FILE
|
#include <cstdio> // std::FILE
|
||||||
#include <cstring> // std::strlen
|
#include <cstring> // std::strlen
|
||||||
#include <iterator>
|
#include <iterator>
|
||||||
@@ -1275,6 +1276,12 @@ enum { long_short = sizeof(long) == sizeof(int) };
|
|||||||
using long_type = conditional_t<long_short, int, long long>;
|
using long_type = conditional_t<long_short, int, long long>;
|
||||||
using ulong_type = conditional_t<long_short, unsigned, unsigned long long>;
|
using ulong_type = conditional_t<long_short, unsigned, unsigned long long>;
|
||||||
|
|
||||||
|
#ifdef __cpp_lib_byte
|
||||||
|
inline auto format_as(std::byte b) -> unsigned char {
|
||||||
|
return static_cast<unsigned char>(b);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
// Maps formatting arguments to core types.
|
// Maps formatting arguments to core types.
|
||||||
// arg_mapper reports errors by returning unformattable instead of using
|
// arg_mapper reports errors by returning unformattable instead of using
|
||||||
// static_assert because it's used in the is_formattable trait.
|
// static_assert because it's used in the is_formattable trait.
|
||||||
|
@@ -34,7 +34,6 @@
|
|||||||
#define FMT_FORMAT_H_
|
#define FMT_FORMAT_H_
|
||||||
|
|
||||||
#include <cmath> // std::signbit
|
#include <cmath> // std::signbit
|
||||||
#include <cstddef> // std::byte
|
|
||||||
#include <cstdint> // uint32_t
|
#include <cstdint> // uint32_t
|
||||||
#include <cstring> // std::memcpy
|
#include <cstring> // std::memcpy
|
||||||
#include <limits> // std::numeric_limits
|
#include <limits> // std::numeric_limits
|
||||||
@@ -2997,11 +2996,6 @@ constexpr auto format_as(Enum e) noexcept -> underlying_t<Enum> {
|
|||||||
}
|
}
|
||||||
} // namespace enums
|
} // namespace enums
|
||||||
|
|
||||||
#ifdef __cpp_lib_byte
|
|
||||||
inline auto format_as(std::byte b) -> unsigned char { return underlying(b); }
|
|
||||||
FMT_FORMAT_AS(std::byte, unsigned char);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
class bytes {
|
class bytes {
|
||||||
private:
|
private:
|
||||||
string_view data_;
|
string_view data_;
|
||||||
|
@@ -1760,9 +1760,15 @@ TEST(format_test, join) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef __cpp_lib_byte
|
#ifdef __cpp_lib_byte
|
||||||
|
TEST(format_test, format_byte) {
|
||||||
|
using arg_mapper = fmt::detail::arg_mapper<fmt::format_context>;
|
||||||
|
EXPECT_EQ(arg_mapper().map(std::byte(42)), 42);
|
||||||
|
EXPECT_EQ(fmt::format("{}", std::byte(42)), "42");
|
||||||
|
}
|
||||||
|
|
||||||
TEST(format_test, join_bytes) {
|
TEST(format_test, join_bytes) {
|
||||||
auto v = std::vector<std::byte>{std::byte(1), std::byte(2), std::byte(3)};
|
auto v = std::vector<std::byte>{std::byte(1), std::byte(2), std::byte(3)};
|
||||||
EXPECT_EQ("1, 2, 3", fmt::format("{}", fmt::join(v, ", ")));
|
EXPECT_EQ(fmt::format("{}", fmt::join(v, ", ")), "1, 2, 3");
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user