mirror of
https://github.com/fmtlib/fmt.git
synced 2024-11-21 18:20:04 +00:00
Add 'n' specifier for tuple and pair (#4107)
This commit is contained in:
parent
9f269062a7
commit
9f0c0c468b
@ -330,7 +330,14 @@ 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({});
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
@ -170,6 +170,8 @@ 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("{:n}", p), "421.5");
|
||||
}
|
||||
|
||||
struct unformattable {};
|
||||
@ -178,6 +180,7 @@ 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_EQ(fmt::format("{}", std::tuple<>()), "()");
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user