mirror of
https://github.com/fmtlib/fmt.git
synced 2024-11-25 19:50:05 +00:00
Simplify join_view formatter
This commit is contained in:
parent
328d256c60
commit
d2473b7b73
@ -623,41 +623,29 @@ struct formatter<join_view<It, Sentinel, Char>, Char> {
|
|||||||
#endif
|
#endif
|
||||||
formatter<remove_cvref_t<value_type>, Char> value_formatter_;
|
formatter<remove_cvref_t<value_type>, Char> value_formatter_;
|
||||||
|
|
||||||
|
using view_ref = conditional_t<std::is_copy_constructible<It>::value,
|
||||||
|
const join_view<It, Sentinel, Char>&,
|
||||||
|
join_view<It, Sentinel, Char>&&>;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
template <typename ParseContext>
|
template <typename ParseContext>
|
||||||
FMT_CONSTEXPR auto parse(ParseContext& ctx) -> const Char* {
|
FMT_CONSTEXPR auto parse(ParseContext& ctx) -> const Char* {
|
||||||
return value_formatter_.parse(ctx);
|
return value_formatter_.parse(ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename FormatContext, typename Iter,
|
|
||||||
FMT_ENABLE_IF(std::is_copy_constructible<Iter>::value)>
|
|
||||||
auto format(const join_view<Iter, Sentinel, Char>& value,
|
|
||||||
FormatContext& ctx) const -> decltype(ctx.out()) {
|
|
||||||
auto it = value.begin;
|
|
||||||
return do_format(value, ctx, it);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename FormatContext, typename Iter,
|
|
||||||
FMT_ENABLE_IF(!std::is_copy_constructible<Iter>::value)>
|
|
||||||
auto format(join_view<Iter, Sentinel, Char>& value, FormatContext& ctx) const
|
|
||||||
-> decltype(ctx.out()) {
|
|
||||||
return do_format(value, ctx, value.begin);
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
template <typename FormatContext>
|
template <typename FormatContext>
|
||||||
auto do_format(const join_view<It, Sentinel, Char>& value, FormatContext& ctx,
|
auto format(view_ref& value, FormatContext& ctx) const
|
||||||
It& it) const -> decltype(ctx.out()) {
|
-> decltype(ctx.out()) {
|
||||||
|
auto it = std::forward<view_ref>(value).begin;
|
||||||
auto out = ctx.out();
|
auto out = ctx.out();
|
||||||
if (it != value.end) {
|
if (it == value.end) return out;
|
||||||
|
out = value_formatter_.format(*it, ctx);
|
||||||
|
++it;
|
||||||
|
while (it != value.end) {
|
||||||
|
out = detail::copy<Char>(value.sep.begin(), value.sep.end(), out);
|
||||||
|
ctx.advance_to(out);
|
||||||
out = value_formatter_.format(*it, ctx);
|
out = value_formatter_.format(*it, ctx);
|
||||||
++it;
|
++it;
|
||||||
while (it != value.end) {
|
|
||||||
out = detail::copy<Char>(value.sep.begin(), value.sep.end(), out);
|
|
||||||
ctx.advance_to(out);
|
|
||||||
out = value_formatter_.format(*it, ctx);
|
|
||||||
++it;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
@ -667,14 +667,14 @@ TEST(ranges_test, lvalue_qualified_begin_end) {
|
|||||||
|
|
||||||
#if ENABLE_INPUT_RANGE_JOIN_TEST
|
#if ENABLE_INPUT_RANGE_JOIN_TEST
|
||||||
TEST(ranges_test, input_range_join) {
|
TEST(ranges_test, input_range_join) {
|
||||||
std::istringstream iss("1 2 3 4 5");
|
auto iss = std::istringstream("1 2 3 4 5");
|
||||||
auto view = std::views::istream<std::string>(iss);
|
auto view = std::views::istream<std::string>(iss);
|
||||||
auto joined_view = fmt::join(view.begin(), view.end(), ", ");
|
EXPECT_EQ("1, 2, 3, 4, 5",
|
||||||
EXPECT_EQ("1, 2, 3, 4, 5", fmt::format("{}", std::move(joined_view)));
|
fmt::format("{}", fmt::join(view.begin(), view.end(), ", ")));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(ranges_test, input_range_join_overload) {
|
TEST(ranges_test, input_range_join_overload) {
|
||||||
std::istringstream iss("1 2 3 4 5");
|
auto iss = std::istringstream("1 2 3 4 5");
|
||||||
EXPECT_EQ(
|
EXPECT_EQ(
|
||||||
"1.2.3.4.5",
|
"1.2.3.4.5",
|
||||||
fmt::format("{}", fmt::join(std::views::istream<std::string>(iss), ".")));
|
fmt::format("{}", fmt::join(std::views::istream<std::string>(iss), ".")));
|
||||||
@ -682,26 +682,26 @@ TEST(ranges_test, input_range_join_overload) {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
TEST(ranges_test, std_istream_iterator_join) {
|
TEST(ranges_test, std_istream_iterator_join) {
|
||||||
std::istringstream iss("1 2 3 4 5");
|
auto&& iss = std::istringstream("1 2 3 4 5");
|
||||||
std::istream_iterator<int> first{iss};
|
auto first = std::istream_iterator<int>(iss);
|
||||||
std::istream_iterator<int> last{};
|
auto last = std::istream_iterator<int>();
|
||||||
auto joined_view = fmt::join(first, last, ", ");
|
EXPECT_EQ("1, 2, 3, 4, 5", fmt::format("{}", fmt::join(first, last, ", ")));
|
||||||
EXPECT_EQ("1, 2, 3, 4, 5", fmt::format("{}", std::move(joined_view)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(ranges_test, movable_only_istream_iter_join) {
|
TEST(ranges_test, movable_only_istream_iter_join) {
|
||||||
// Mirrors c++20 std::ranges::basic_istream_view::iterator
|
// Mirrors C++20 std::ranges::basic_istream_view::iterator.
|
||||||
struct UncopyableIstreamIter : std::istream_iterator<int> {
|
struct noncopyable_istream_iterator : std::istream_iterator<int> {
|
||||||
explicit UncopyableIstreamIter(std::istringstream& iss)
|
explicit noncopyable_istream_iterator(std::istringstream& iss)
|
||||||
: std::istream_iterator<int>{iss} {}
|
: std::istream_iterator<int>{iss} {}
|
||||||
UncopyableIstreamIter(const UncopyableIstreamIter&) = delete;
|
noncopyable_istream_iterator(const noncopyable_istream_iterator&) = delete;
|
||||||
UncopyableIstreamIter(UncopyableIstreamIter&&) = default;
|
noncopyable_istream_iterator(noncopyable_istream_iterator&&) = default;
|
||||||
};
|
};
|
||||||
static_assert(!std::is_copy_constructible<UncopyableIstreamIter>::value, "");
|
static_assert(
|
||||||
|
!std::is_copy_constructible<noncopyable_istream_iterator>::value, "");
|
||||||
|
|
||||||
std::istringstream iss("1 2 3 4 5");
|
auto&& iss = std::istringstream("1 2 3 4 5");
|
||||||
UncopyableIstreamIter first{iss};
|
auto first = noncopyable_istream_iterator(iss);
|
||||||
std::istream_iterator<int> last{};
|
auto last = std::istream_iterator<int>();
|
||||||
auto joined_view = fmt::join(std::move(first), last, ", ");
|
EXPECT_EQ("1, 2, 3, 4, 5",
|
||||||
EXPECT_EQ("1, 2, 3, 4, 5", fmt::format("{}", std::move(joined_view)));
|
fmt::format("{}", fmt::join(std::move(first), last, ", ")));
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user