Skip to content

Commit eb99f6e

Browse files
committed
Move std::byte formatter to std.h
1 parent b98926b commit eb99f6e

File tree

7 files changed

+27
-35
lines changed

7 files changed

+27
-35
lines changed

include/fmt/format-inl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#define FMT_FORMAT_INL_H_
1010

1111
#ifndef FMT_MODULE
12+
# include <stddef.h> // ptrdiff_t
13+
1214
# include <algorithm>
1315
# include <cerrno> // errno
1416
# include <climits>

include/fmt/format.h

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,8 @@
5151
# include <stdlib.h> // malloc, free
5252
# include <string.h> // memcpy
5353

54-
# include <cmath> // std::signbit
55-
# include <cstddef> // std::byte
56-
# include <limits> // std::numeric_limits
54+
# include <cmath> // std::signbit
55+
# include <limits> // std::numeric_limits
5756
# if defined(__GLIBCXX__) && !defined(_GLIBCXX_USE_DUAL_ABI)
5857
// Workaround for pre gcc 5 libstdc++.
5958
# include <memory> // std::allocator_traits
@@ -4069,19 +4068,6 @@ constexpr auto format_as(Enum e) noexcept -> underlying_t<Enum> {
40694068
}
40704069
} // namespace enums
40714070

4072-
#ifdef __cpp_lib_byte
4073-
template <typename Char>
4074-
struct formatter<std::byte, Char> : formatter<unsigned, Char> {
4075-
static auto format_as(std::byte b) -> unsigned char {
4076-
return static_cast<unsigned char>(b);
4077-
}
4078-
template <typename Context>
4079-
auto format(std::byte b, Context& ctx) const -> decltype(ctx.out()) {
4080-
return formatter<unsigned, Char>::format(format_as(b), ctx);
4081-
}
4082-
};
4083-
#endif
4084-
40854071
struct bytes {
40864072
string_view data;
40874073

include/fmt/std.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
# include <atomic>
1616
# include <bitset>
1717
# include <complex>
18-
# include <exception>
18+
# include <cstddef> // std::byte
19+
# include <exception> // std::exception
1920
# include <functional> // std::reference_wrapper
2021
# include <memory>
2122
# include <thread>
@@ -666,6 +667,19 @@ struct formatter<BitRef, Char,
666667
}
667668
};
668669

670+
#ifdef __cpp_lib_byte
671+
template <typename Char>
672+
struct formatter<std::byte, Char> : formatter<unsigned, Char> {
673+
static auto format_as(std::byte b) -> unsigned char {
674+
return static_cast<unsigned char>(b);
675+
}
676+
template <typename Context>
677+
auto format(std::byte b, Context& ctx) const -> decltype(ctx.out()) {
678+
return formatter<unsigned, Char>::format(format_as(b), ctx);
679+
}
680+
};
681+
#endif
682+
669683
template <typename T, typename Char>
670684
struct formatter<std::atomic<T>, Char,
671685
enable_if_t<is_formattable<T, Char>::value>>

test/compile-test.cc

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,6 @@ TEST(compile_test, format_default) {
7575
EXPECT_EQ("foo", fmt::format(FMT_COMPILE("{}"), test_formattable()));
7676
auto t = std::chrono::system_clock::now();
7777
EXPECT_EQ(fmt::format("{}", t), fmt::format(FMT_COMPILE("{}"), t));
78-
# ifdef __cpp_lib_byte
79-
EXPECT_EQ("42", fmt::format(FMT_COMPILE("{}"), std::byte{42}));
80-
# endif
8178
}
8279

8380
TEST(compile_test, format_escape) {

test/format-test.cc

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2611,14 +2611,6 @@ TEST(format_test, invalid_glibc_buffer) {
26112611
}
26122612
#endif // FMT_USE_FCNTL
26132613

2614-
#ifdef __cpp_lib_byte
2615-
TEST(base_test, format_byte) {
2616-
auto s = std::string();
2617-
fmt::format_to(std::back_inserter(s), "{}", std::byte(42));
2618-
EXPECT_EQ(s, "42");
2619-
}
2620-
#endif
2621-
26222614
// Only defined after the test case.
26232615
struct incomplete_type;
26242616
extern const incomplete_type& external_instance;

test/ranges-test.cc

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -395,13 +395,6 @@ TEST(ranges_test, join) {
395395
EXPECT_EQ(fmt::format("{}", join(v4, " ")), "0 1 0");
396396
}
397397

398-
#ifdef __cpp_lib_byte
399-
TEST(ranges_test, join_bytes) {
400-
auto v = std::vector<std::byte>{std::byte(1), std::byte(2), std::byte(3)};
401-
EXPECT_EQ(fmt::format("{}", fmt::join(v, ", ")), "1, 2, 3");
402-
}
403-
#endif
404-
405398
TEST(ranges_test, join_tuple) {
406399
// Value tuple args.
407400
auto t1 = std::tuple<char, int, float>('a', 1, 2.0f);

test/std-test.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,14 @@ TEST(std_test, format_bitset) {
460460
EXPECT_EQ(fmt::format("{:-^12}", bs), "---101010---");
461461
}
462462

463+
#ifdef __cpp_lib_byte
464+
TEST(base_test, format_byte) {
465+
auto s = std::string();
466+
fmt::format_to(std::back_inserter(s), "{}", std::byte(42));
467+
EXPECT_EQ(s, "42");
468+
}
469+
#endif
470+
463471
TEST(std_test, format_atomic) {
464472
std::atomic<bool> b(false);
465473
EXPECT_EQ(fmt::format("{}", b), "false");

0 commit comments

Comments
 (0)