Xped
Loading...
Searching...
No Matches
FmtHelpers.hpp
Go to the documentation of this file.
1#ifndef XPED_FMT_HELPERS_HPP_
2#define XPED_FMT_HELPERS_HPP_
3
4#include "fmt/core.h"
5#include "fmt/ostream.h"
6#include "fmt/ranges.h"
7#include "fmt/std.h"
8
9// template <typename T, typename Char>
10// struct fmt::formatter<std::complex<T>, Char> : ostream_formatter
11// {};
12
13template <typename T, typename Char>
14struct fmt::formatter<std::complex<T>, Char> : public fmt::formatter<T, Char>
15{
16 typedef fmt::formatter<T, Char> base;
17 enum style
18 {
19 expr,
20 star,
21 pair
22 } style_ = expr;
23 fmt::detail::dynamic_format_specs<Char> specs_;
24 FMT_CONSTEXPR auto parse(format_parse_context& ctx) -> decltype(ctx.begin())
25 {
26 auto it = ctx.begin();
27 if(it != ctx.end()) {
28 switch(*it) {
29 case '$':
30 style_ = style::expr;
31 ctx.advance_to(++it);
32 break;
33 case '*':
34 style_ = style::star;
35 ctx.advance_to(++it);
36 break;
37 case ',':
38 style_ = style::pair;
39 ctx.advance_to(++it);
40 break;
41 default: break;
42 }
43 }
44 auto type = fmt::detail::type_constant<T, Char>::value;
45 parse_format_specs(ctx.begin(), ctx.end(), specs_, ctx, type);
46 // todo: fixup alignment
47 return base::parse(ctx);
48 }
49 template <typename FormatCtx>
50 auto format(const std::complex<T>& x, FormatCtx& ctx) const -> decltype(ctx.out())
51 {
52 format_to(ctx.out(), "(");
53 if(style_ == style::pair) {
54 base::format(x.real(), ctx);
55 format_to(ctx.out(), ",");
56 base::format(x.imag(), ctx);
57 return format_to(ctx.out(), ")");
58 }
59 if(x.real() || !x.imag()) base::format(x.real(), ctx);
60 if(x.imag()) {
61 if(x.real() && x.imag() >= 0 && specs_.sign != sign::plus) format_to(ctx.out(), "+");
62 base::format(x.imag(), ctx);
63 if(style_ == style::star)
64 format_to(ctx.out(), "*i");
65 else
66 format_to(ctx.out(), "i");
67 if(std::is_same<typename std::decay<T>::type, float>::value) format_to(ctx.out(), "f");
68 if(std::is_same<typename std::decay<T>::type, long double>::value) format_to(ctx.out(), "l");
69 }
70 return format_to(ctx.out(), ")");
71 }
72};
73
74#endif
std::string format(qarray< Symmetry::Nq > qnum)
Definition: functions.hpp:53