Skip to content

Commit

Permalink
Make map formatter use tuple formatter
Browse files Browse the repository at this point in the history
This reverts parts of 16cec4f
  • Loading branch information
someonewithpc committed Jul 28, 2024
1 parent a17b8c3 commit a95c4ea
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
17 changes: 10 additions & 7 deletions include/fmt/ranges.h
Original file line number Diff line number Diff line change
Expand Up @@ -583,8 +583,8 @@ struct formatter<
using map_type = detail::maybe_const_range<R>;
using element_type = detail::uncvref_type<map_type>;

decltype(detail::tuple::get_formatters<element_type, Char>(
detail::tuple_index_sequence<element_type>())) formatters_;
formatter<element_type, Char> underlying_;

bool no_delimiters_ = false;

public:
Expand All @@ -605,8 +605,13 @@ struct formatter<
}
ctx.advance_to(it);
}
detail::for_each(formatters_, detail::parse_empty_specs<ParseContext>{ctx});
return it;
auto it2 = underlying_.parse(ctx);
if (it2 != end && *it2 != '}') report_error("invalid format specifier");
if (it == it2) {
underlying_.set_separator(detail::string_literal<Char, ':', ' '>{});
underlying_.set_brackets({}, {});
}
return it2;
}

template <typename FormatContext>
Expand All @@ -620,9 +625,7 @@ struct formatter<
for (auto&& value : map) {
if (i > 0) out = detail::copy<Char>(sep, out);
ctx.advance_to(out);
detail::for_each2(formatters_, mapper.map(value),
detail::format_tuple_element<FormatContext>{
0, ctx, detail::string_literal<Char, ':', ' '>{}});
underlying_.format(mapper.map(value), ctx);
++i;
}
basic_string_view<Char> close = detail::string_literal<Char, '}'>{};
Expand Down
19 changes: 7 additions & 12 deletions test/ranges-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ TEST(ranges_test, format_map) {
auto m = std::map<std::string, int>{{"one", 1}, {"two", 2}};
EXPECT_EQ(fmt::format("{}", m), "{\"one\": 1, \"two\": 2}");
EXPECT_EQ(fmt::format("{:n}", m), "\"one\": 1, \"two\": 2");
EXPECT_EQ(fmt::format("{:n:}", m), "\"one\": 1, \"two\": 2");
EXPECT_EQ(fmt::format("{:n:m}", m), "\"one\": 1, \"two\": 2");
EXPECT_EQ(fmt::format("{:n:n}", m), "\"one\"1, \"two\"2");
EXPECT_EQ(fmt::format("{::n}", m), "{\"one\"1, \"two\"2}");
EXPECT_EQ(fmt::format("{:n:m}", m), "\"one\": 1, \"two\": 2");
EXPECT_EQ(fmt::format("{:n:n,;}", m), "\"one\";1, \"two\";2");
EXPECT_EQ(fmt::format("{:n:,;}", m), "(\"one\";1), (\"two\";2)");
}

struct test_map_value {};
Expand All @@ -101,18 +108,6 @@ template <> struct formatter<test_map_value> : formatter<string_view> {
}
};

template <typename K>
struct formatter<std::pair<K, test_map_value>> : formatter<string_view> {
auto format(std::pair<K, test_map_value>, format_context& ctx) const
-> format_context::iterator {
return ctx.out();
}
};

template <typename K>
struct is_tuple_formattable<std::pair<K, test_map_value>, char>
: std::false_type {};

FMT_END_NAMESPACE

TEST(ranges_test, format_map_custom_pair) {
Expand Down

0 comments on commit a95c4ea

Please sign in to comment.