mirror of
https://github.com/fmtlib/fmt.git
synced 2025-07-30 10:47:35 +02:00
Improve std::complex formatter (#4050)
Signed-off-by: Vladislav Shchapov <vladislav@shchapov.ru>
This commit is contained in:
committed by
GitHub
parent
232c6bc481
commit
a96259701e
@ -631,33 +631,67 @@ struct formatter<std::atomic_flag, Char> : formatter<bool, Char> {
|
|||||||
#endif // __cpp_lib_atomic_flag_test
|
#endif // __cpp_lib_atomic_flag_test
|
||||||
|
|
||||||
FMT_EXPORT
|
FMT_EXPORT
|
||||||
template <typename F, typename Char>
|
template <typename T, typename Char> struct formatter<std::complex<T>, Char> {
|
||||||
struct formatter<std::complex<F>, Char> : nested_formatter<F, Char> {
|
|
||||||
private:
|
private:
|
||||||
// Functor because C++11 doesn't support generic lambdas.
|
detail::dynamic_format_specs<Char> specs_;
|
||||||
struct writer {
|
|
||||||
const formatter<std::complex<F>, Char>* f;
|
|
||||||
const std::complex<F>& c;
|
|
||||||
|
|
||||||
template <typename OutputIt>
|
template <typename FormatContext, typename OutputIt>
|
||||||
FMT_CONSTEXPR auto operator()(OutputIt out) -> OutputIt {
|
FMT_CONSTEXPR auto do_format(const std::complex<T>& c,
|
||||||
if (c.real() != 0) {
|
detail::dynamic_format_specs<Char>& specs,
|
||||||
auto format_full = detail::string_literal<Char, '(', '{', '}', '+', '{',
|
FormatContext& ctx, OutputIt out) const
|
||||||
'}', 'i', ')'>{};
|
-> OutputIt {
|
||||||
return fmt::format_to(out, basic_string_view<Char>(format_full),
|
if (c.real() != 0) {
|
||||||
f->nested(c.real()), f->nested(c.imag()));
|
*out++ = Char('(');
|
||||||
}
|
out = detail::write<Char>(out, c.real(), specs, ctx.locale());
|
||||||
auto format_imag = detail::string_literal<Char, '{', '}', 'i'>{};
|
specs.sign = sign::plus;
|
||||||
return fmt::format_to(out, basic_string_view<Char>(format_imag),
|
out = detail::write<Char>(out, c.imag(), specs, ctx.locale());
|
||||||
f->nested(c.imag()));
|
if (!detail::isfinite(c.imag())) *out++ = Char(' ');
|
||||||
|
*out++ = Char('i');
|
||||||
|
*out++ = Char(')');
|
||||||
|
return out;
|
||||||
}
|
}
|
||||||
};
|
out = detail::write<Char>(out, c.imag(), specs, ctx.locale());
|
||||||
|
if (!detail::isfinite(c.imag())) *out++ = Char(' ');
|
||||||
|
*out++ = Char('i');
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
FMT_CONSTEXPR auto parse(basic_format_parse_context<Char>& ctx)
|
||||||
|
-> decltype(ctx.begin()) {
|
||||||
|
if (ctx.begin() == ctx.end() || *ctx.begin() == '}') return ctx.begin();
|
||||||
|
return parse_format_specs(ctx.begin(), ctx.end(), specs_, ctx,
|
||||||
|
detail::type_constant<T, Char>::value);
|
||||||
|
}
|
||||||
|
|
||||||
template <typename FormatContext>
|
template <typename FormatContext>
|
||||||
auto format(const std::complex<F>& c, FormatContext& ctx) const
|
auto format(const std::complex<T>& c, FormatContext& ctx) const
|
||||||
-> decltype(ctx.out()) {
|
-> decltype(ctx.out()) {
|
||||||
return this->write_padded(ctx, writer{this, c});
|
auto specs = specs_;
|
||||||
|
if (specs.width_ref.kind != detail::arg_id_kind::none ||
|
||||||
|
specs.precision_ref.kind != detail::arg_id_kind::none) {
|
||||||
|
detail::handle_dynamic_spec<detail::width_checker>(specs.width,
|
||||||
|
specs.width_ref, ctx);
|
||||||
|
detail::handle_dynamic_spec<detail::precision_checker>(
|
||||||
|
specs.precision, specs.precision_ref, ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (specs.width == 0) return do_format(c, specs, ctx, ctx.out());
|
||||||
|
auto buf = basic_memory_buffer<Char>();
|
||||||
|
|
||||||
|
auto outer_specs = format_specs();
|
||||||
|
outer_specs.width = specs.width;
|
||||||
|
outer_specs.fill = specs.fill;
|
||||||
|
outer_specs.align = specs.align;
|
||||||
|
|
||||||
|
specs.width = 0;
|
||||||
|
specs.fill = {};
|
||||||
|
specs.align = align::none;
|
||||||
|
|
||||||
|
do_format(c, specs, ctx, basic_appender<Char>(buf));
|
||||||
|
return detail::write<Char>(ctx.out(),
|
||||||
|
basic_string_view<Char>(buf.data(), buf.size()),
|
||||||
|
outer_specs);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -66,10 +66,34 @@ TEST(std_test, thread_id) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST(std_test, complex) {
|
TEST(std_test, complex) {
|
||||||
|
using limits = std::numeric_limits<double>;
|
||||||
|
EXPECT_EQ(fmt::format("{}", std::complex<double>(1, limits::quiet_NaN())),
|
||||||
|
"(1+nan i)");
|
||||||
|
EXPECT_EQ(fmt::format("{}", std::complex<double>(1, -limits::infinity())),
|
||||||
|
"(1-inf i)");
|
||||||
|
|
||||||
|
EXPECT_EQ(fmt::format("{}", std::complex<int>(1, 2)), "(1+2i)");
|
||||||
|
|
||||||
EXPECT_EQ(fmt::format("{}", std::complex<double>(1, 2.2)), "(1+2.2i)");
|
EXPECT_EQ(fmt::format("{}", std::complex<double>(1, 2.2)), "(1+2.2i)");
|
||||||
|
EXPECT_EQ(fmt::format("{}", std::complex<double>(1, -2.2)), "(1-2.2i)");
|
||||||
EXPECT_EQ(fmt::format("{}", std::complex<double>(0, 2.2)), "2.2i");
|
EXPECT_EQ(fmt::format("{}", std::complex<double>(0, 2.2)), "2.2i");
|
||||||
|
EXPECT_EQ(fmt::format("{}", std::complex<double>(0, -2.2)), "-2.2i");
|
||||||
|
|
||||||
|
EXPECT_EQ(fmt::format("{:+}", std::complex<double>(0, 2.2)), "+2.2i");
|
||||||
|
EXPECT_EQ(fmt::format("{:+}", std::complex<double>(0, -2.2)), "-2.2i");
|
||||||
|
EXPECT_EQ(fmt::format("{:+}", std::complex<double>(1, -2.2)), "(+1-2.2i)");
|
||||||
|
EXPECT_EQ(fmt::format("{:+}", std::complex<double>(1, 2.2)), "(+1+2.2i)");
|
||||||
|
EXPECT_EQ(fmt::format("{: }", std::complex<double>(1, 2.2)), "( 1+2.2i)");
|
||||||
|
EXPECT_EQ(fmt::format("{: }", std::complex<double>(1, -2.2)), "( 1-2.2i)");
|
||||||
|
|
||||||
EXPECT_EQ(fmt::format("{:>20.2f}", std::complex<double>(1, 2.2)),
|
EXPECT_EQ(fmt::format("{:>20.2f}", std::complex<double>(1, 2.2)),
|
||||||
" (1.00+2.20i)");
|
" (1.00+2.20i)");
|
||||||
|
EXPECT_EQ(fmt::format("{:<20.2f}", std::complex<double>(1, 2.2)),
|
||||||
|
"(1.00+2.20i) ");
|
||||||
|
EXPECT_EQ(fmt::format("{:<20.2f}", std::complex<double>(1, -2.2)),
|
||||||
|
"(1.00-2.20i) ");
|
||||||
|
EXPECT_EQ(fmt::format("{:<{}.{}f}", std::complex<double>(1, -2.2), 20, 2),
|
||||||
|
"(1.00-2.20i) ");
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef __cpp_lib_source_location
|
#ifdef __cpp_lib_source_location
|
||||||
|
Reference in New Issue
Block a user