-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfmt.hpp
More file actions
314 lines (288 loc) · 10.4 KB
/
fmt.hpp
File metadata and controls
314 lines (288 loc) · 10.4 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#ifndef __$CENXUANTIAN_FMT_HPP__
#define __$CENXUANTIAN_FMT_HPP__
#pragma once
#include <iostream>
#include <sstream>
#include <type_traits>
#include <tuple>
#define FMT_DEBUG 1
#define DBG_DIE(msg) \
std::cout << "Error: " << msg << __FILE__ << ':' << __LINE__ << '\n'; \
exit(-1)
namespace fmt
{
namespace detail
{
// C++ version check
#if __cplusplus >= 202004
#define FMT_IF_CONSTEXPR constexpr
constexpr int cxx_standard = 20;
#elif __cplusplus >= 201703
#define FMT_IF_CONSTEXPR constexpr
constexpr int cxx_standard = 17;
#elif __cplusplus >= 201402
#define FMT_IF_CONSTEXPR
constexpr int cxx_standard = 14;
#else
#error "C++ standard is not supported by fmt"
#endif
// Traits
template <class T>
struct ostream_traits
{
static constexpr bool value = true;
};
template <class T>
struct istring_traits
{
static constexpr bool value = true;
};
constexpr size_t npos = ~(size_t{0});
template <class InputIter>
inline size_t find_first_of(
InputIter beg,
InputIter end,
typename std::iterator_traits<InputIter>::value_type const &item)
{
size_t cur = 0;
while (beg != end)
{
if (*beg == item)
{
return cur;
}
++beg;
++cur;
}
return fmt::detail::npos;
}
}
template <
class OStreamT,
class IStringT,
size_t Limit>
class formatter
{
private:
struct item_pos
{
size_t beg; // pos of {
size_t end; // pos of }
};
OStreamT &ostrem_; // output string
IStringT const &istring_; // input string
item_pos items_[Limit]; // stores items' begin and end positions
size_t num_items_;
template <class Iter>
inline void render_plain_text(Iter beg, Iter end)
{
while (beg != end)
{
auto cur_c = *beg;
if (cur_c == '{' || cur_c == '}')
{
auto next = beg + 1;
if (next != end && *next == cur_c)
{
// We want to skip this, because we don't want to print {{ or }}
++beg;
continue;
}
}
ostrem_ << *beg;
++beg;
}
}
/// @brief
/// @tparam TupleT Tuple type
/// @tparam Pos index of this item
/// @param t the tuple that stores all items
template <size_t Pos, class TupleT>
inline void render_one(TupleT &t)
{
// get the begin and end positions of text
size_t text_beg = 0;
size_t text_end = items_[Pos].beg;
if FMT_IF_CONSTEXPR (Pos != 0)
{ // In this case the position should be the end of last item + 1
text_beg = items_[Pos - 1].end + 1;
}
// Print text
render_plain_text(std::begin(istring_) + text_beg, std::begin(istring_) + text_end);
// Get the begin and end positions of the formatter (item)
size_t formatter_beg = items_[Pos].beg + 1;
size_t formatter_end = items_[Pos].end;
if (formatter_beg == formatter_end)
{ // In this case there is nothing included in {}
ostrem_ << std::get<Pos>(t);
}
else
{ // Parse the language included in {...}
// Split the formatter
const auto split_pos = fmt::detail::find_first_of(
std::begin(istring_) + formatter_beg,
std::begin(istring_) + formatter_end,
':'
);
}
}
// TODO: need to check the 0 case
template <size_t Pos, size_t End, class TupleT, typename std::enable_if<Pos<End>::type * = nullptr> inline void render_all(std::integral_constant<size_t, Pos>, TupleT &t)
{
render_one<Pos>(t);
render_all<Pos + 1, End>(std::integral_constant<size_t, Pos + 1>{}, t);
}
template <size_t Pos, size_t End, class TupleT, typename std::enable_if<Pos == End>::type * = nullptr>
inline void render_all(std::integral_constant<size_t, Pos>, TupleT &t)
{
auto beg = std::begin(istring_) + items_[Pos - 1].end + 1;
auto end = std::end(istring_);
render_plain_text(beg, end);
}
public:
static_assert(detail::ostream_traits<OStreamT>::value, "OStreamT is not supported");
static_assert(detail::istring_traits<IStringT>::value, "IStringT is not supported");
static_assert(Limit > 0, "Limit must greater than 0");
constexpr explicit formatter(OStreamT &ostrem, IStringT const &istring)
: ostrem_(ostrem), istring_(istring), items_(), num_items_(0)
{
auto beg = std::begin(istring_);
auto end = std::end(istring_);
size_t pos = 0;
size_t temp_beg = ~size_t(0);
auto record_beg = [&]()
{
if (temp_beg == ~size_t(0))
{
temp_beg = pos;
}
else
{
// TODO: remove std::string from here
std::string what = "invalid input string formatter at pos " + std::to_string(pos) + std::string(__FILE__) + ":" + std::to_string(__LINE__);
// throw std::exception(what.c_str());
DBG_DIE(what);
}
};
auto record_end = [&]()
{
if (temp_beg != ~size_t(0))
{
items_[num_items_].beg = temp_beg;
items_[num_items_].end = pos;
++num_items_;
temp_beg = ~size_t(0); // reset temp_beg
}
else
{
// TODO: remove std::string from here
std::string what = "invalid input string formatter at pos " + std::to_string(pos) + std::string(__FILE__) + ":" + std::to_string(__LINE__);
// throw std::exception(what.c_str());
DBG_DIE(what);
}
};
while (beg != end)
{
if (*beg == '{')
{
++beg;
if (beg != end)
{
const auto next_c = *beg;
if (next_c != '{')
{
// in this case, we need to record this pos
record_beg();
--beg; // go back
}
else
{
// in this case, we need to skip these 2 char
// beg will ++ later
}
}
else
{
// { should not be placed at the end
// TODO: remove std::string from here
std::string what = "invalid input string formatter at pos " + std::to_string(pos) + std::string(__FILE__) + ":" + std::to_string(__LINE__);
// throw std::exception(what.c_str());
DBG_DIE(what);
}
}
else if (*beg == '}')
{
++beg;
if (beg != end)
{
const auto next_c = *beg;
if (next_c != '}')
{
// in this case, we need to record this pos
record_end();
--beg; // go back
}
else
{
// in this case, we need to skip these 2 char
// beg will ++ later
}
}
else
{
// it's ok to place } at the end, we record this pos and go back
// in this case, we need to record this pos
record_end();
--beg; // go back
}
}
++pos;
++beg;
}
#if defined(FMT_DEBUG) && FMT_DEBUG == 1
for (size_t i = 0; i < num_items_; ++i)
{
printf("%lld [%lld, %lld]\n", i, items_[i].beg, items_[i].end);
}
#endif
}
template <class... Args>
inline void format(Args &&...args)
{
constexpr size_t num_args = sizeof...(args);
if (num_items_ != num_args)
{
// TODO: remove std::string from here
std::string what = "wrong number of arguments, expected " + std::to_string(num_items_) + ", got " + std::to_string(num_args);
// throw std::exception(what.c_str());
DBG_DIE(what);
}
else
{
// TODO: need to remove tuple from here
std::tuple<Args...> t(std::forward<Args>(args)...);
render_all<0, num_args>(std::integral_constant<size_t, 0>{}, t);
}
}
inline void format()
{
if (num_items_ != 0)
{
// TODO: remove std::string from here
// TODO: maybe use a different error handling method
std::string what = "wrong number of arguments, expected " + std::to_string(num_items_) + ", got 0";
// throw std::exception(what.c_str());
DBG_DIE(what);
}
}
};
template <
class OStreamT,
class IStringT,
size_t Limit = 20>
constexpr inline formatter<OStreamT, IStringT, Limit> f(OStreamT &ostrem, IStringT &&istring)
{
return formatter<OStreamT, IStringT, Limit>(ostrem, std::forward<IStringT>(istring));
}
}
#endif