mirror of
https://github.com/fmtlib/fmt.git
synced 2025-03-16 01:21:23 +00:00
Add an experimental nested formatter
This commit is contained in:
parent
5bdce181f1
commit
d4987546a4
@ -4198,6 +4198,38 @@ template <typename T> struct formatter<group_digits_view<T>> : formatter<T> {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct nested_view {
|
||||||
|
const formatter<T>* fmt;
|
||||||
|
const T* value;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct formatter<nested_view<T>> {
|
||||||
|
FMT_CONSTEXPR auto parse(format_parse_context& ctx) -> const char* {
|
||||||
|
return ctx.begin();
|
||||||
|
}
|
||||||
|
auto format(nested_view<T> view, format_context& ctx) const
|
||||||
|
-> decltype(ctx.out()) {
|
||||||
|
return view.fmt->format(*view.value, ctx);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct nested_formatter {
|
||||||
|
private:
|
||||||
|
formatter<T> formatter_;
|
||||||
|
|
||||||
|
public:
|
||||||
|
FMT_CONSTEXPR auto parse(format_parse_context& ctx) -> const char* {
|
||||||
|
return formatter_.parse(ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
auto nested(const T& value) const -> nested_view<T> {
|
||||||
|
return nested_view<T>{&formatter_, &value};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// DEPRECATED! join_view will be moved to ranges.h.
|
// DEPRECATED! join_view will be moved to ranges.h.
|
||||||
template <typename It, typename Sentinel, typename Char = char>
|
template <typename It, typename Sentinel, typename Char = char>
|
||||||
struct join_view : detail::view {
|
struct join_view : detail::view {
|
||||||
|
@ -1775,6 +1775,23 @@ TEST(format_test, group_digits_view) {
|
|||||||
EXPECT_EQ(fmt::format("{:8}", fmt::group_digits(1000)), " 1,000");
|
EXPECT_EQ(fmt::format("{:8}", fmt::group_digits(1000)), " 1,000");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct point {
|
||||||
|
double x, y;
|
||||||
|
};
|
||||||
|
|
||||||
|
FMT_BEGIN_NAMESPACE
|
||||||
|
template <>
|
||||||
|
struct formatter<point> : nested_formatter<double> {
|
||||||
|
auto format(point p, format_context& ctx) const -> decltype(ctx.out()) {
|
||||||
|
return format_to(ctx.out(), "({}, {})", nested(p.x), nested(p.y));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
FMT_END_NAMESPACE
|
||||||
|
|
||||||
|
TEST(format_test, nested_formatter) {
|
||||||
|
EXPECT_EQ(fmt::format("{:.2f}", point{1, 2}), "(1.00, 2.00)");
|
||||||
|
}
|
||||||
|
|
||||||
enum test_enum { foo, bar };
|
enum test_enum { foo, bar };
|
||||||
auto format_as(test_enum e) -> int { return e; }
|
auto format_as(test_enum e) -> int { return e; }
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user