Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: Add support for specifying the separator for range types #4090

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 30 additions & 8 deletions include/fmt/ranges.h
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,26 @@ struct formatter<Tuple, Char,
template <typename ParseContext>
FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
auto it = ctx.begin();
if (it != ctx.end() && *it != '}') report_error("invalid format specifier");
auto end = ctx.end();
if (it != end && detail::to_ascii(*it) == 'n') {
++it;
set_brackets({}, {});
set_separator({});
} else if (it != end && detail::to_ascii(*it) == 'm') {
if (std::tuple_size<Tuple>::value != 2) report_error("'m' format specifier can only be used for tuples with 2 elements");
++it;
set_brackets({}, {});
set_separator(detail::string_literal<Char, ':', ' '>{});
}
if (it != end && detail::to_ascii(*it) == ',') {
++it;
auto sep_end = it;
while (sep_end != end && *sep_end != '}') { ++sep_end; }
set_separator(basic_string_view<Char>(it, std::distance(it, sep_end)));
it = sep_end;
}
if (it != end && *it != '}') report_error("invalid format specifier");
ctx.advance_to(it);
detail::for_each(formatters_, detail::parse_empty_specs<ParseContext>{ctx});
return it;
}
Expand Down Expand Up @@ -564,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 @@ -586,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 @@ -601,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
30 changes: 18 additions & 12 deletions test/ranges-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include "fmt/format.h"
#include "gtest/gtest.h"
#include "gtest-extra.h"

#if !FMT_GCC_VERSION || FMT_GCC_VERSION >= 601
# define FMT_RANGES_TEST_ENABLE_C_STYLE_ARRAY
Expand Down Expand Up @@ -88,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 @@ -100,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 Expand Up @@ -170,6 +166,14 @@ TEST(ranges_test, format_adl_begin_end) {
TEST(ranges_test, format_pair) {
auto p = std::pair<int, float>(42, 1.5f);
EXPECT_EQ(fmt::format("{}", p), "(42, 1.5)");
EXPECT_EQ(fmt::format("{:}", p), "(42, 1.5)");
EXPECT_EQ(fmt::format("{:m}", p), "42: 1.5");
EXPECT_EQ(fmt::format("{:n}", p), "421.5");
EXPECT_EQ(fmt::format("{:, }", p), "(42 1.5)");
EXPECT_EQ(fmt::format("{:,}", p), "(421.5)");
EXPECT_EQ(fmt::format("{:,; }", p), "(42; 1.5)");
EXPECT_EQ(fmt::format("{:n,; }", p), "42; 1.5");
EXPECT_EQ(fmt::format("{:n,=}", p), "42=1.5");
}

struct unformattable {};
Expand All @@ -178,6 +182,8 @@ TEST(ranges_test, format_tuple) {
auto t =
std::tuple<int, float, std::string, char>(42, 1.5f, "this is tuple", 'i');
EXPECT_EQ(fmt::format("{}", t), "(42, 1.5, \"this is tuple\", 'i')");
EXPECT_EQ(fmt::format("{:n}", t), "421.5\"this is tuple\"'i'");
EXPECT_THROW_MSG((void)fmt::format(fmt::runtime("{:m}"), t), fmt::format_error, "'m' format specifier can only be used for tuples with 2 elements");

EXPECT_EQ(fmt::format("{}", std::tuple<>()), "()");

Expand Down
Loading