-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcomplexfmt.h
More file actions
169 lines (160 loc) · 6.11 KB
/
complexfmt.h
File metadata and controls
169 lines (160 loc) · 6.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
* =====================================================================================
*
* Filename: complexfmt.hpp
*
* Description:
*
* Version: 1.0
* Created: 2021-03-18 13:17:09
* Revision: 3
* Compiler: g++/clang++
*
* Author: Francis Grizzly Smit (FGJS), grizzly@smit.id.au
* Organization:
*
* =====================================================================================
*/
//#define FMT_USE_CONSTEXPR 0
// std::complex<t> //
#include <complex>
#include <fmt/format.h>
#ifndef FMTEXTRAS_COMPLEXFMT_H_
#define FMTEXTRAS_COMPLEXFMT_H_
FMT_BEGIN_NAMESPACE
#define FMTEXTRAS_CONSTEXPR FMT_CONSTEXPR
//#define FMTEXTRAS_CONSTEXPR
template<typename T, typename Char>
class formatter<std::complex<T>, Char> : public formatter<T, Char> {
private:
enum class style { expr, pair, star, lisp };
style style_ = style::expr;
bool bracket = true;
bool spaced = true;
using base = formatter<T, Char>;
using Context_type = basic_format_parse_context<Char>;
public:
FMTEXTRAS_CONSTEXPR auto parse(Context_type& ctx) -> decltype(ctx.begin()) {
auto it = ctx.begin(), end = ctx.end();
bool done = false;
if(it != end && *it == ':') ++it;
for(int cnt = 0;it != end && cnt < 3; it++, cnt++){
switch(*it){
case '$':
style_ = style::expr;
break;
case ',':
style_ = style::pair;
break;
case '*':
style_ = style::star;
break;
case '|':
style_ = style::lisp;
break;
case '@':
bracket = false;
break;
case '~':
bracket = true;
break;
case ';':
spaced = false;
break;
case '_':
spaced = true;
break;
default:
done = true;
}
if(done) break;
}
ctx.advance_to(it);
return base::parse(ctx);
}
template<typename FormatContext>
FMTEXTRAS_CONSTEXPR auto format(const std::complex<T>& c, FormatContext& ctx) -> decltype(ctx.out()) {
auto out = ctx.out();
if(style_ == style::lisp){
out = format_to(out, "#C(");
ctx.advance_to(out);
bracket = true;
}else if(style_ == style::pair){
out = format_to(out, "(");
ctx.advance_to(out);
bracket = true;
}else if(bracket){
out = format_to(out, "(");
ctx.advance_to(out);
}
out = base::format(c.real(), ctx);
T imag = c.imag();
char sep[4], *sep_ptr = sep;
char _tail[3], *tptr = _tail;
switch(style_){
case style::expr:
if(spaced){
*sep_ptr = ' ';
sep_ptr++;
}
if(imag < 0){
*sep_ptr = '-';
sep_ptr++;
imag = -imag;
}else{
*sep_ptr = '+';
sep_ptr++;
}
if(spaced){
*sep_ptr = ' ';
sep_ptr++;
}
*tptr = 'i';
tptr++;
break;
case style::star:
if(spaced){
*sep_ptr = ' ';
sep_ptr++;
}
if(imag < 0){
*sep_ptr = '-';
sep_ptr++;
imag = -imag;
}else{
*sep_ptr = '+';
sep_ptr++;
}
if(spaced){
*sep_ptr = ' ';
sep_ptr++;
}
*tptr = '*';
tptr++;
*tptr = 'i';
tptr++;
break;
case style::pair:
case style::lisp:
*sep_ptr = ',';
sep_ptr++;
if(spaced){
*sep_ptr = ' ';
sep_ptr++;
}
break;
default:
throw("How did I get here invalid style");
}
*sep_ptr = '\0';
*tptr = '\0';
out = format_to(out, FMT_STRING("{}"), sep);
ctx.advance_to(out);
out = base::format(imag, ctx);
out = format_to(out, FMT_STRING("{}"), _tail);
if(bracket) return format_to(out, ")");
return out;
}
};
FMT_END_NAMESPACE
#endif // FMTEXTRAS_COMPLEXFMT_H_