Skip to content

Commit

Permalink
Improve std::complex formatter
Browse files Browse the repository at this point in the history
Signed-off-by: Vladislav Shchapov <vladislav@shchapov.ru>
  • Loading branch information
phprus committed Jul 3, 2024
1 parent c4f6fa7 commit be1ba09
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 15 deletions.
72 changes: 57 additions & 15 deletions include/fmt/std.h
Original file line number Diff line number Diff line change
Expand Up @@ -631,33 +631,75 @@ struct formatter<std::atomic_flag, Char> : formatter<bool, Char> {
#endif // __cpp_lib_atomic_flag_test

FMT_EXPORT
template <typename F, typename Char>
struct formatter<std::complex<F>, Char> : nested_formatter<F, Char> {
template <typename T, typename Char> struct formatter<std::complex<T>, Char> {
private:
detail::dynamic_format_specs<Char> specs_;

template <typename FormatContext, typename F>
auto write_padded(detail::dynamic_format_specs<Char>& specs,
FormatContext& ctx, F write) const -> decltype(ctx.out()) {
if (specs.width == 0) return write(specs, ctx, ctx.out());
auto buf = basic_memory_buffer<Char>();

auto complex_specs = format_specs();
complex_specs.width = specs.width;
complex_specs.fill = specs.fill;
complex_specs.align = specs.align;

specs.width = 0;
specs.fill = {};
specs.align = align::none;
write(specs, ctx, basic_appender<Char>(buf));
return detail::write<Char>(ctx.out(),
basic_string_view<Char>(buf.data(), buf.size()),
complex_specs);
}

// Functor because C++11 doesn't support generic lambdas.
struct writer {
const formatter<std::complex<F>, Char>* f;
const std::complex<F>& c;
const std::complex<T>& c;

template <typename OutputIt>
FMT_CONSTEXPR auto operator()(OutputIt out) -> OutputIt {
template <typename FormatContext, typename OutputIt>
FMT_CONSTEXPR auto operator()(detail::dynamic_format_specs<Char>& specs,
FormatContext& ctx, OutputIt out)
-> OutputIt {
if (c.real() != 0) {
auto format_full = detail::string_literal<Char, '(', '{', '}', '+', '{',
'}', 'i', ')'>{};
return fmt::format_to(out, basic_string_view<Char>(format_full),
f->nested(c.real()), f->nested(c.imag()));
*out++ = Char('(');
out = detail::write<Char>(out, c.real(), specs, ctx.locale());
specs.sign = sign::plus;
out = detail::write<Char>(out, c.imag(), specs, ctx.locale());
if (!detail::isfinite(c.imag())) *out++ = Char(' ');
*out++ = Char('i');
*out++ = Char(')');
return out;
}
auto format_imag = detail::string_literal<Char, '{', '}', 'i'>{};
return fmt::format_to(out, basic_string_view<Char>(format_imag),
f->nested(c.imag()));
out = detail::write<Char>(out, c.imag(), specs, ctx.locale());
if (!detail::isfinite(c.imag())) *out++ = Char(' ');
*out++ = Char('i');
return out;
}
};

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>
auto format(const std::complex<F>& c, FormatContext& ctx) const
auto format(const std::complex<T>& c, FormatContext& ctx) const
-> 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);
}
return this->write_padded(specs, ctx, writer{c});
}
};

Expand Down
22 changes: 22 additions & 0 deletions test/std-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,32 @@ TEST(std_test, thread_id) {
}

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<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>(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)),
" (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
Expand Down

0 comments on commit be1ba09

Please sign in to comment.