From f7510c593093f6bf804b0a2644b7e35b751f1e68 Mon Sep 17 00:00:00 2001 From: rbrugo Date: Fri, 11 Mar 2022 15:33:03 +0100 Subject: [PATCH 1/4] Use `range_begin`/`end` to get formatted range iterators --- include/fmt/ranges.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/fmt/ranges.h b/include/fmt/ranges.h index 77089f36a4c0..edff8f96f7cc 100644 --- a/include/fmt/ranges.h +++ b/include/fmt/ranges.h @@ -402,8 +402,8 @@ struct formatter< auto out = ctx.out(); *out++ = prefix; int i = 0; - auto it = std::begin(range); - auto end = std::end(range); + auto it = detail::range_begin(range); + auto end = detail::range_end(range); for (; it != end; ++it) { if (i > 0) out = detail::write_delimiter(out); if (custom_specs_) { From 03bf57a69efba60dae2854bc94f98cc301cb209a Mon Sep 17 00:00:00 2001 From: rbrugo Date: Fri, 11 Mar 2022 16:03:33 +0100 Subject: [PATCH 2/4] Add test for adl `begin`/`end` --- test/ranges-test.cc | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/test/ranges-test.cc b/test/ranges-test.cc index 8509ee790add..53a57fc47081 100644 --- a/test/ranges-test.cc +++ b/test/ranges-test.cc @@ -65,6 +65,34 @@ TEST(ranges_test, format_set) { "{\"one\", \"two\"}"); } +namespace adl +{ + struct box { + int value; + }; + + auto begin(box & b) noexcept -> int * { + return std::addressof(b.value); + } + + auto end(box & b) noexcept -> int * { + return std::addressof(b.value) + 1; + } + + auto begin(const box & b) noexcept -> const int * { + return std::addressof(b.value); + } + + auto end(const box & b) noexcept -> const int * { + return std::addressof(b.value) + 1; + } +} // namespace adl + +TEST(ranges_test, format_adl_begin_end) { + auto b = adl::box{42}; + EXPECT_EQ(fmt::format("{}", b), "[42]"); +} + TEST(ranges_test, format_pair) { auto p = std::pair(42, 1.5f); EXPECT_EQ(fmt::format("{}", p), "(42, 1.5)"); From d02079a40f1252d868d7306e73fd690b79d6b2e9 Mon Sep 17 00:00:00 2001 From: rbrugo Date: Fri, 11 Mar 2022 16:35:09 +0100 Subject: [PATCH 3/4] Apply clang-format --- test/ranges-test.cc | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/test/ranges-test.cc b/test/ranges-test.cc index 53a57fc47081..d7298308dbd4 100644 --- a/test/ranges-test.cc +++ b/test/ranges-test.cc @@ -65,32 +65,27 @@ TEST(ranges_test, format_set) { "{\"one\", \"two\"}"); } -namespace adl -{ - struct box { - int value; - }; - - auto begin(box & b) noexcept -> int * { - return std::addressof(b.value); - } +namespace adl { +struct box { + int value; +}; - auto end(box & b) noexcept -> int * { - return std::addressof(b.value) + 1; - } +auto begin(box& b) noexcept -> int* { return std::addressof(b.value); } - auto begin(const box & b) noexcept -> const int * { - return std::addressof(b.value); - } +auto end(box& b) noexcept -> int* { return std::addressof(b.value) + 1; } - auto end(const box & b) noexcept -> const int * { - return std::addressof(b.value) + 1; - } -} // namespace adl +auto begin(const box& b) noexcept -> const int* { + return std::addressof(b.value); +} + +auto end(const box& b) noexcept -> const int* { + return std::addressof(b.value) + 1; +} +} // namespace adl TEST(ranges_test, format_adl_begin_end) { - auto b = adl::box{42}; - EXPECT_EQ(fmt::format("{}", b), "[42]"); + auto b = adl::box{42}; + EXPECT_EQ(fmt::format("{}", b), "[42]"); } TEST(ranges_test, format_pair) { From b9f4f14b280e9c5120def0d59cd79ec495569f0e Mon Sep 17 00:00:00 2001 From: rbrugo Date: Fri, 11 Mar 2022 19:37:57 +0100 Subject: [PATCH 4/4] Simplify tests --- test/ranges-test.cc | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/test/ranges-test.cc b/test/ranges-test.cc index d7298308dbd4..aa910e794022 100644 --- a/test/ranges-test.cc +++ b/test/ranges-test.cc @@ -70,16 +70,12 @@ struct box { int value; }; -auto begin(box& b) noexcept -> int* { return std::addressof(b.value); } - -auto end(box& b) noexcept -> int* { return std::addressof(b.value) + 1; } - -auto begin(const box& b) noexcept -> const int* { - return std::addressof(b.value); +auto begin(const box& b) -> const int* { + return &b.value; } -auto end(const box& b) noexcept -> const int* { - return std::addressof(b.value) + 1; +auto end(const box& b) -> const int* { + return &b.value + 1; } } // namespace adl