mirror of
https://github.com/fmtlib/fmt.git
synced 2025-07-30 10:47:35 +02:00
Add 'n' specifier for tuple and pair (#4107)
This commit is contained in:
@ -330,7 +330,14 @@ struct formatter<Tuple, Char,
|
|||||||
template <typename ParseContext>
|
template <typename ParseContext>
|
||||||
FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
|
FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
|
||||||
auto it = 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});
|
detail::for_each(formatters_, detail::parse_empty_specs<ParseContext>{ctx});
|
||||||
return it;
|
return it;
|
||||||
}
|
}
|
||||||
|
@ -170,6 +170,8 @@ TEST(ranges_test, format_adl_begin_end) {
|
|||||||
TEST(ranges_test, format_pair) {
|
TEST(ranges_test, format_pair) {
|
||||||
auto p = std::pair<int, float>(42, 1.5f);
|
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("{:}", p), "(42, 1.5)");
|
||||||
|
EXPECT_EQ(fmt::format("{:n}", p), "421.5");
|
||||||
}
|
}
|
||||||
|
|
||||||
struct unformattable {};
|
struct unformattable {};
|
||||||
@ -178,6 +180,7 @@ TEST(ranges_test, format_tuple) {
|
|||||||
auto t =
|
auto t =
|
||||||
std::tuple<int, float, std::string, char>(42, 1.5f, "this is tuple", 'i');
|
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("{}", 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<>()), "()");
|
EXPECT_EQ(fmt::format("{}", std::tuple<>()), "()");
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user