-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfmt_ext.h
220 lines (179 loc) · 6.01 KB
/
fmt_ext.h
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
#pragma once
#ifndef FMT_EXT_RE_H_
#define FMT_EXT_RE_H_
// =================================================================================================
// fmt_ext.h
// #include "fmt_ext.h"
// Created: 04.8.2018
//
// BSD 2-Clause License
// Remotion (C) 2018 - All Rights Reserved
// {fmt} support for ranges, containers and types tuple interface.
// =================================================================================================
#include <type_traits>
#ifndef FMT_RANGE_OUTPUT_LENGTH_LIMIT
#define FMT_RANGE_OUTPUT_LENGTH_LIMIT 256
#endif
namespace fmt_ext {
template <typename Char>
struct formatting_base {
template <typename ParseContext>
FMT_CONSTEXPR auto parse(ParseContext &ctx) -> decltype(ctx.begin()) { return ctx.begin(); }
};
template <typename Char, typename Enable = void>
struct formatting_range : formatting_base<Char>
{
Char prefix = '{';
Char delimiter = ',';
Char postfix = '}';
bool add_spaces = true;
};
template <typename Char, typename Enable = void>
struct formatting_tuple : formatting_base<Char>
{
Char prefix = '[';
Char delimiter = ',';
Char postfix = ']';
bool add_spaces = true;
};
template<typename RangeT, typename OutputIterator>
void copy(const RangeT &range, OutputIterator out) {
for(const auto& it: range) {
*out++ = it;
}
}
template<typename OutputIterator>
void copy(const char *str, OutputIterator out) {
const char* p_curr = str;
while (*p_curr) {
*out++ = *p_curr++;
}
}
template<typename OutputIterator>
void copy(const char ch, OutputIterator out) {
*out++ = ch;
}
} // namespace fmt_ext
namespace fmt_ext {
namespace meta {
/// Return true value if T has std::string interface, like std::string_view.
template<typename T>
class is_like_std_string {
template<typename U> static auto check(U* p) -> decltype(
p->find('a')
, p->length()
, p->data()
, int());
template<typename > static void check(...);
public:
static const bool value = !std::is_void< decltype(check<T>(nullptr)) >::value;
};
template<typename T>
constexpr bool is_like_std_string_v = is_like_std_string<T>::value;
template<typename... Ts>
struct conditional_helper {};
template<typename T, typename _ = void>
struct is_range_ : std::false_type {};
template<typename T>
struct is_range_<T,
std::conditional_t< false,
conditional_helper<
decltype(std::declval<T>().begin()),
decltype(std::declval<T>().end())
>, void>
> : std::true_type{};
template<typename T>
constexpr bool is_range_v = is_range_<T>::value && !is_like_std_string<T>::value;
/// tuple_size and tuple_element check.
template<typename T>
class is_tuple_like_ {
template<typename U> static auto check(U* p) -> decltype(
std::tuple_size< U >::value,
std::declval<typename std::tuple_element<0, U>::type> (),
int());
template<typename > static void check(...);
public:
static constexpr bool value = !std::is_void_v< decltype(check<T>(nullptr)) >;
};
template<typename T>
constexpr bool is_tuple_like_v = is_tuple_like_<T>::value && !is_range_<T>::value;
//=--------------------------------------------------------------------------------------------------------------------
template<size_t... Is, class Tuple, class F>
void for_each(std::index_sequence<Is...>, Tuple&& tup, F&& f) noexcept {
using std::get;
// using free function get<I>(T) now.
const int _[] = { 0,
((void)f(get<Is>(tup)),
0)... };
(void)_; // blocks warnings
}
//=--------------------------------------------------------------------------------------------------------------------
template<class T>
constexpr std::make_index_sequence<std::tuple_size<T>::value>
get_indexes(T const&) { return {}; }
//=--------------------------------------------------------------------------------------------------------------------
template<class Tuple, class F>
void for_each(Tuple&& tup, F&& f) {
const auto indexes = get_indexes(tup);
for_each(indexes, std::forward<Tuple>(tup), std::forward<F>(f));
}
} // namespace meta
} // namespace fmt_ext
namespace fmt {
// =====================================================================================================================
template<typename TupleT, typename Char>
struct formatter< TupleT, Char
, std::enable_if_t<fmt_ext::meta::is_tuple_like_v<TupleT>> >
{
fmt_ext::formatting_tuple<Char> formating;
template <typename ParseContext>
FMT_CONSTEXPR auto parse(ParseContext &ctx) -> decltype(ctx.begin()) {
return formating.parse(ctx);
}
template <typename FormatContext = format_context>
auto format(const TupleT &values, FormatContext &ctx) -> decltype(ctx.out()) {
auto out = ctx.out();
std::ptrdiff_t i = 0;
fmt_ext::copy(formating.prefix, out);
fmt_ext::meta::for_each(values, [&](const auto &v) {
if (i++ > 0) { fmt_ext::copy(formating.delimiter, out); }
if (formating.add_spaces) { format_to(out, " {}", v); }
else { format_to(out, "{}", v); }
});
if (formating.add_spaces) { *out++ = ' '; }
fmt_ext::copy(formating.postfix, out);
return ctx.out();
}
};
} // namespace fmt
namespace fmt {
template<typename RangeT, typename Char>
struct formatter <RangeT, Char, std::enable_if_t<fmt_ext::meta::is_range_v<RangeT>> >
{
static constexpr std::ptrdiff_t range_length_limit = FMT_RANGE_OUTPUT_LENGTH_LIMIT; // show only up to N items from the range.
fmt_ext::formatting_range<Char> formating;
template <typename ParseContext>
FMT_CONSTEXPR auto parse(ParseContext &ctx) -> decltype(ctx.begin()) {
return formating.parse(ctx);
}
template <typename FormatContext>
auto format(const RangeT &values, FormatContext &ctx) -> decltype(ctx.out()) {
auto out = ctx.out();
fmt_ext::copy(formating.prefix, out);
std::ptrdiff_t i = 0;
for (const auto& it : values) {
if (i > 0) { fmt_ext::copy(formating.delimiter, out); }
if (formating.add_spaces) { format_to(out, " {}", it); }
else { format_to(out, "{}", it); }
if (++i > range_length_limit) {
format_to(out, " ... <other elements>");
break;
}
}
if (formating.add_spaces) { *out++ = ' '; }
fmt_ext::copy(formating.postfix, out);
return ctx.out();
}
};
} // namespace fmt
#endif // FMT_EXT_RE_H_