Skip to content

Commit a962597

Browse files
authored
Improve std::complex formatter (#4050)
Signed-off-by: Vladislav Shchapov <[email protected]>
1 parent 232c6bc commit a962597

File tree

2 files changed

+79
-21
lines changed

2 files changed

+79
-21
lines changed

include/fmt/std.h

+55-21
Original file line numberDiff line numberDiff line change
@@ -631,33 +631,67 @@ struct formatter<std::atomic_flag, Char> : formatter<bool, Char> {
631631
#endif // __cpp_lib_atomic_flag_test
632632

633633
FMT_EXPORT
634-
template <typename F, typename Char>
635-
struct formatter<std::complex<F>, Char> : nested_formatter<F, Char> {
634+
template <typename T, typename Char> struct formatter<std::complex<T>, Char> {
636635
private:
637-
// Functor because C++11 doesn't support generic lambdas.
638-
struct writer {
639-
const formatter<std::complex<F>, Char>* f;
640-
const std::complex<F>& c;
641-
642-
template <typename OutputIt>
643-
FMT_CONSTEXPR auto operator()(OutputIt out) -> OutputIt {
644-
if (c.real() != 0) {
645-
auto format_full = detail::string_literal<Char, '(', '{', '}', '+', '{',
646-
'}', 'i', ')'>{};
647-
return fmt::format_to(out, basic_string_view<Char>(format_full),
648-
f->nested(c.real()), f->nested(c.imag()));
649-
}
650-
auto format_imag = detail::string_literal<Char, '{', '}', 'i'>{};
651-
return fmt::format_to(out, basic_string_view<Char>(format_imag),
652-
f->nested(c.imag()));
636+
detail::dynamic_format_specs<Char> specs_;
637+
638+
template <typename FormatContext, typename OutputIt>
639+
FMT_CONSTEXPR auto do_format(const std::complex<T>& c,
640+
detail::dynamic_format_specs<Char>& specs,
641+
FormatContext& ctx, OutputIt out) const
642+
-> OutputIt {
643+
if (c.real() != 0) {
644+
*out++ = Char('(');
645+
out = detail::write<Char>(out, c.real(), specs, ctx.locale());
646+
specs.sign = sign::plus;
647+
out = detail::write<Char>(out, c.imag(), specs, ctx.locale());
648+
if (!detail::isfinite(c.imag())) *out++ = Char(' ');
649+
*out++ = Char('i');
650+
*out++ = Char(')');
651+
return out;
653652
}
654-
};
653+
out = detail::write<Char>(out, c.imag(), specs, ctx.locale());
654+
if (!detail::isfinite(c.imag())) *out++ = Char(' ');
655+
*out++ = Char('i');
656+
return out;
657+
}
655658

656659
public:
660+
FMT_CONSTEXPR auto parse(basic_format_parse_context<Char>& ctx)
661+
-> decltype(ctx.begin()) {
662+
if (ctx.begin() == ctx.end() || *ctx.begin() == '}') return ctx.begin();
663+
return parse_format_specs(ctx.begin(), ctx.end(), specs_, ctx,
664+
detail::type_constant<T, Char>::value);
665+
}
666+
657667
template <typename FormatContext>
658-
auto format(const std::complex<F>& c, FormatContext& ctx) const
668+
auto format(const std::complex<T>& c, FormatContext& ctx) const
659669
-> decltype(ctx.out()) {
660-
return this->write_padded(ctx, writer{this, c});
670+
auto specs = specs_;
671+
if (specs.width_ref.kind != detail::arg_id_kind::none ||
672+
specs.precision_ref.kind != detail::arg_id_kind::none) {
673+
detail::handle_dynamic_spec<detail::width_checker>(specs.width,
674+
specs.width_ref, ctx);
675+
detail::handle_dynamic_spec<detail::precision_checker>(
676+
specs.precision, specs.precision_ref, ctx);
677+
}
678+
679+
if (specs.width == 0) return do_format(c, specs, ctx, ctx.out());
680+
auto buf = basic_memory_buffer<Char>();
681+
682+
auto outer_specs = format_specs();
683+
outer_specs.width = specs.width;
684+
outer_specs.fill = specs.fill;
685+
outer_specs.align = specs.align;
686+
687+
specs.width = 0;
688+
specs.fill = {};
689+
specs.align = align::none;
690+
691+
do_format(c, specs, ctx, basic_appender<Char>(buf));
692+
return detail::write<Char>(ctx.out(),
693+
basic_string_view<Char>(buf.data(), buf.size()),
694+
outer_specs);
661695
}
662696
};
663697

test/std-test.cc

+24
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,34 @@ TEST(std_test, thread_id) {
6666
}
6767

6868
TEST(std_test, complex) {
69+
using limits = std::numeric_limits<double>;
70+
EXPECT_EQ(fmt::format("{}", std::complex<double>(1, limits::quiet_NaN())),
71+
"(1+nan i)");
72+
EXPECT_EQ(fmt::format("{}", std::complex<double>(1, -limits::infinity())),
73+
"(1-inf i)");
74+
75+
EXPECT_EQ(fmt::format("{}", std::complex<int>(1, 2)), "(1+2i)");
76+
6977
EXPECT_EQ(fmt::format("{}", std::complex<double>(1, 2.2)), "(1+2.2i)");
78+
EXPECT_EQ(fmt::format("{}", std::complex<double>(1, -2.2)), "(1-2.2i)");
7079
EXPECT_EQ(fmt::format("{}", std::complex<double>(0, 2.2)), "2.2i");
80+
EXPECT_EQ(fmt::format("{}", std::complex<double>(0, -2.2)), "-2.2i");
81+
82+
EXPECT_EQ(fmt::format("{:+}", std::complex<double>(0, 2.2)), "+2.2i");
83+
EXPECT_EQ(fmt::format("{:+}", std::complex<double>(0, -2.2)), "-2.2i");
84+
EXPECT_EQ(fmt::format("{:+}", std::complex<double>(1, -2.2)), "(+1-2.2i)");
85+
EXPECT_EQ(fmt::format("{:+}", std::complex<double>(1, 2.2)), "(+1+2.2i)");
86+
EXPECT_EQ(fmt::format("{: }", std::complex<double>(1, 2.2)), "( 1+2.2i)");
87+
EXPECT_EQ(fmt::format("{: }", std::complex<double>(1, -2.2)), "( 1-2.2i)");
88+
7189
EXPECT_EQ(fmt::format("{:>20.2f}", std::complex<double>(1, 2.2)),
7290
" (1.00+2.20i)");
91+
EXPECT_EQ(fmt::format("{:<20.2f}", std::complex<double>(1, 2.2)),
92+
"(1.00+2.20i) ");
93+
EXPECT_EQ(fmt::format("{:<20.2f}", std::complex<double>(1, -2.2)),
94+
"(1.00-2.20i) ");
95+
EXPECT_EQ(fmt::format("{:<{}.{}f}", std::complex<double>(1, -2.2), 20, 2),
96+
"(1.00-2.20i) ");
7397
}
7498

7599
#ifdef __cpp_lib_source_location

0 commit comments

Comments
 (0)