spdlog
Loading...
Searching...
No Matches
ranges.h
Go to the documentation of this file.
1// Formatting library for C++ - experimental range support
2//
3// Copyright (c) 2012 - present, Victor Zverovich
4// All rights reserved.
5//
6// For the license information refer to format.h.
7//
8// Copyright (c) 2018 - present, Remotion (Igor Schulz)
9// All Rights Reserved
10// {fmt} support for ranges, containers and types tuple interface.
11
12#ifndef FMT_RANGES_H_
13#define FMT_RANGES_H_
14
15#include <initializer_list>
16#include <type_traits>
17
18#include "format.h"
19
21
22template <typename Char, typename Enable = void> struct formatting_range {
23#ifdef FMT_DEPRECATED_BRACED_RANGES
24 Char prefix = '{';
25 Char postfix = '}';
26#else
27 Char prefix = '[';
28 Char postfix = ']';
29#endif
30
31 template <typename ParseContext>
32 FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
33 return ctx.begin();
34 }
35};
36
37template <typename Char, typename Enable = void> struct formatting_tuple {
38 Char prefix = '(';
39 Char postfix = ')';
40
41 template <typename ParseContext>
42 FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
43 return ctx.begin();
44 }
45};
46
47namespace detail {
48
49template <typename RangeT, typename OutputIterator>
50OutputIterator copy(const RangeT& range, OutputIterator out) {
51 for (auto it = range.begin(), end = range.end(); it != end; ++it)
52 *out++ = *it;
53 return out;
54}
55
56template <typename OutputIterator>
57OutputIterator copy(const char* str, OutputIterator out) {
58 while (*str) *out++ = *str++;
59 return out;
60}
61
62template <typename OutputIterator>
63OutputIterator copy(char ch, OutputIterator out) {
64 *out++ = ch;
65 return out;
66}
67
68template <typename OutputIterator>
69OutputIterator copy(wchar_t ch, OutputIterator out) {
70 *out++ = ch;
71 return out;
72}
73
74/// Return true value if T has std::string interface, like std::string_view.
75template <typename T> class is_std_string_like {
76 template <typename U>
77 static auto check(U* p)
78 -> decltype((void)p->find('a'), p->length(), (void)p->data(), int());
79 template <typename> static void check(...);
80
81 public:
82 static FMT_CONSTEXPR_DECL const bool value =
84};
85
86template <typename Char>
88
89template <typename... Ts> struct conditional_helper {};
90
91template <typename T, typename _ = void> struct is_range_ : std::false_type {};
92
93#if !FMT_MSC_VER || FMT_MSC_VER > 1800
94
95# define FMT_DECLTYPE_RETURN(val) \
96 ->decltype(val) { return val; } \
97 static_assert( \
98 true, "") // This makes it so that a semicolon is required after the
99 // macro, which helps clang-format handle the formatting.
100
101// C array overload
102template <typename T, std::size_t N>
103auto range_begin(const T (&arr)[N]) -> const T* {
104 return arr;
105}
106template <typename T, std::size_t N>
107auto range_end(const T (&arr)[N]) -> const T* {
108 return arr + N;
109}
110
111template <typename T, typename Enable = void>
113
114template <typename T>
115struct has_member_fn_begin_end_t<T, void_t<decltype(std::declval<T>().begin()),
116 decltype(std::declval<T>().end())>>
117 : std::true_type {};
118
119// Member function overload
120template <typename T>
121auto range_begin(T&& rng) FMT_DECLTYPE_RETURN(static_cast<T&&>(rng).begin());
122template <typename T>
123auto range_end(T&& rng) FMT_DECLTYPE_RETURN(static_cast<T&&>(rng).end());
124
125// ADL overload. Only participates in overload resolution if member functions
126// are not found.
127template <typename T>
128auto range_begin(T&& rng)
129 -> enable_if_t<!has_member_fn_begin_end_t<T&&>::value,
130 decltype(begin(static_cast<T&&>(rng)))> {
131 return begin(static_cast<T&&>(rng));
132}
133template <typename T>
134auto range_end(T&& rng) -> enable_if_t<!has_member_fn_begin_end_t<T&&>::value,
135 decltype(end(static_cast<T&&>(rng)))> {
136 return end(static_cast<T&&>(rng));
137}
138
139template <typename T, typename Enable = void>
141template <typename T, typename Enable = void>
143
144template <typename T>
146 T, void_t<decltype(detail::range_begin(
147 std::declval<const remove_cvref_t<T>&>())),
148 decltype(detail::range_begin(
149 std::declval<const remove_cvref_t<T>&>()))>>
150 : std::true_type {};
151
152template <typename T>
154 T, void_t<decltype(detail::range_begin(std::declval<T>())),
155 decltype(detail::range_begin(std::declval<T>())),
156 enable_if_t<std::is_copy_constructible<T>::value>>>
157 : std::true_type {};
158
159template <typename T>
160struct is_range_<T, void>
161 : std::integral_constant<bool, (has_const_begin_end<T>::value ||
162 has_mutable_begin_end<T>::value)> {};
163
164template <typename T, typename Enable = void> struct range_to_view;
165template <typename T>
166struct range_to_view<T, enable_if_t<has_const_begin_end<T>::value>> {
167 struct view_t {
169
170 auto begin() const FMT_DECLTYPE_RETURN(detail::range_begin(*m_range_ptr));
171 auto end() const FMT_DECLTYPE_RETURN(detail::range_end(*m_range_ptr));
172 };
173 static auto view(const T& range) -> view_t { return {&range}; }
174};
175
176template <typename T>
177struct range_to_view<T, enable_if_t<!has_const_begin_end<T>::value &&
179 struct view_t {
181
182 auto begin() FMT_DECLTYPE_RETURN(detail::range_begin(m_range_copy));
183 auto end() FMT_DECLTYPE_RETURN(detail::range_end(m_range_copy));
184 };
185 static auto view(const T& range) -> view_t { return {range}; }
186};
187# undef FMT_DECLTYPE_RETURN
188#endif
189
190/// tuple_size and tuple_element check.
191template <typename T> class is_tuple_like_ {
192 template <typename U>
193 static auto check(U* p) -> decltype(std::tuple_size<U>::value, int());
194 template <typename> static void check(...);
195
196 public:
197 static FMT_CONSTEXPR_DECL const bool value =
199};
200
201// Check for integer_sequence
202#if defined(__cpp_lib_integer_sequence) || FMT_MSC_VER >= 1900
203template <typename T, T... N>
205template <size_t... N> using index_sequence = std::index_sequence<N...>;
206template <size_t N> using make_index_sequence = std::make_index_sequence<N>;
207#else
208template <typename T, T... N> struct integer_sequence {
209 using value_type = T;
210
211 static FMT_CONSTEXPR size_t size() { return sizeof...(N); }
212};
213
214template <size_t... N> using index_sequence = integer_sequence<size_t, N...>;
215
216template <typename T, size_t N, T... Ns>
217struct make_integer_sequence : make_integer_sequence<T, N - 1, N - 1, Ns...> {};
218template <typename T, T... Ns>
219struct make_integer_sequence<T, 0, Ns...> : integer_sequence<T, Ns...> {};
220
221template <size_t N>
223#endif
224
225template <class Tuple, class F, size_t... Is>
226void for_each(index_sequence<Is...>, Tuple&& tup, F&& f) FMT_NOEXCEPT {
227 using std::get;
228 // using free function get<I>(T) now.
229 const int _[] = {0, ((void)f(get<Is>(tup)), 0)...};
230 (void)_; // blocks warnings
231}
232
233template <class T>
238
239template <class Tuple, class F> void for_each(Tuple&& tup, F&& f) {
240 const auto indexes = get_indexes(tup);
241 for_each(indexes, std::forward<Tuple>(tup), std::forward<F>(f));
242}
243
244template <typename Range>
247
248template <typename OutputIt> OutputIt write_delimiter(OutputIt out) {
249 *out++ = ',';
250 *out++ = ' ';
251 return out;
252}
253
254template <
255 typename Char, typename OutputIt, typename Arg,
256 FMT_ENABLE_IF(is_std_string_like<typename std::decay<Arg>::type>::value)>
257OutputIt write_range_entry(OutputIt out, const Arg& v) {
258 *out++ = '"';
259 out = write<Char>(out, v);
260 *out++ = '"';
261 return out;
262}
263
264template <typename Char, typename OutputIt, typename Arg,
266OutputIt write_range_entry(OutputIt out, const Arg v) {
267 *out++ = '\'';
268 *out++ = v;
269 *out++ = '\'';
270 return out;
271}
272
273template <
274 typename Char, typename OutputIt, typename Arg,
275 FMT_ENABLE_IF(!is_std_string_like<typename std::decay<Arg>::type>::value &&
277OutputIt write_range_entry(OutputIt out, const Arg& v) {
278 return write<Char>(out, v);
279}
280
281} // namespace detail
282
283template <typename T> struct is_tuple_like {
284 static FMT_CONSTEXPR_DECL const bool value =
286};
287
288template <typename TupleT, typename Char>
289struct formatter<TupleT, Char, enable_if_t<fmt::is_tuple_like<TupleT>::value>> {
290 private:
291 // C++11 generic lambda for format()
292 template <typename FormatContext> struct format_each {
293 template <typename T> void operator()(const T& v) {
294 if (i > 0) out = detail::write_delimiter(out);
295 out = detail::write_range_entry<Char>(out, v);
296 ++i;
297 }
299 size_t& i;
301 decltype(std::declval<FormatContext>().out())>::type out;
302 };
303
304 public:
306
307 template <typename ParseContext>
308 FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
309 return formatting.parse(ctx);
310 }
311
312 template <typename FormatContext = format_context>
313 auto format(const TupleT& values, FormatContext& ctx) -> decltype(ctx.out()) {
314 auto out = ctx.out();
315 size_t i = 0;
316
317 detail::copy(formatting.prefix, out);
318 detail::for_each(values, format_each<FormatContext>{formatting, i, out});
319 detail::copy(formatting.postfix, out);
320
321 return ctx.out();
322 }
323};
324
331
332template <typename T, typename Char>
334 T, Char,
335 enable_if_t<
336 fmt::is_range<T, Char>::value
337// Workaround a bug in MSVC 2017 and earlier.
338#if !FMT_MSC_VER || FMT_MSC_VER >= 1927
339 && (has_formatter<detail::value_type<T>, format_context>::value ||
340 detail::has_fallback_formatter<detail::value_type<T>, Char>::value)
341#endif
342 >> {
344
345 template <typename ParseContext>
346 FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
347 return formatting.parse(ctx);
348 }
349
350 template <typename FormatContext>
351 typename FormatContext::iterator format(const T& values, FormatContext& ctx) {
352 auto out = detail::copy(formatting.prefix, ctx.out());
353 size_t i = 0;
355 auto it = view.begin();
356 auto end = view.end();
357 for (; it != end; ++it) {
358 if (i > 0) out = detail::write_delimiter(out);
359 out = detail::write_range_entry<Char>(out, *it);
360 ++i;
361 }
362 return detail::copy(formatting.postfix, out);
363 }
364};
365
366template <typename Char, typename... T> struct tuple_join_view : detail::view {
367 const std::tuple<T...>& tuple;
369
371 : tuple(t), sep{s} {}
372};
373
374template <typename Char, typename... T>
376
377template <typename Char, typename... T>
378struct formatter<tuple_join_view<Char, T...>, Char> {
379 template <typename ParseContext>
380 FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
381 return ctx.begin();
382 }
383
384 template <typename FormatContext>
385 auto format(const tuple_join_view<Char, T...>& value, FormatContext& ctx) ->
386 typename FormatContext::iterator {
387 return format(value, ctx, detail::make_index_sequence<sizeof...(T)>{});
388 }
389
390 private:
391 template <typename FormatContext, size_t... N>
392 auto format(const tuple_join_view<Char, T...>& value, FormatContext& ctx,
394 typename FormatContext::iterator {
395 using std::get;
396 return format_args(value, ctx, get<N>(value.tuple)...);
397 }
398
399 template <typename FormatContext>
400 auto format_args(const tuple_join_view<Char, T...>&, FormatContext& ctx) ->
401 typename FormatContext::iterator {
402 // NOTE: for compilers that support C++17, this empty function instantiation
403 // can be replaced with a constexpr branch in the variadic overload.
404 return ctx.out();
405 }
406
407 template <typename FormatContext, typename Arg, typename... Args>
408 auto format_args(const tuple_join_view<Char, T...>& value, FormatContext& ctx,
409 const Arg& arg, const Args&... args) ->
410 typename FormatContext::iterator {
412 auto out = base().format(arg, ctx);
413 if (sizeof...(Args) > 0) {
414 out = std::copy(value.sep.begin(), value.sep.end(), out);
415 ctx.advance_to(out);
416 return format_args(value, ctx, args...);
417 }
418 return out;
419 }
420};
421
423
424/**
425 \rst
426 Returns an object that formats `tuple` with elements separated by `sep`.
427
428 **Example**::
429
430 std::tuple<int, char> t = {1, 'a'};
431 fmt::print("{}", fmt::join(t, ", "));
432 // Output: "1, a"
433 \endrst
434 */
435template <typename... T>
437 -> tuple_join_view<char, T...> {
438 return {tuple, sep};
439}
440
441template <typename... T>
444 -> tuple_join_view<wchar_t, T...> {
445 return {tuple, sep};
446}
447
448/**
449 \rst
450 Returns an object that formats `initializer_list` with elements separated by
451 `sep`.
452
453 **Example**::
454
455 fmt::print("{}", fmt::join({1, 2, 3}, ", "));
456 // Output: "1, 2, 3"
457 \endrst
458 */
459template <typename T>
462 return join(std::begin(list), std::end(list), sep);
463}
464
467
468#endif // FMT_RANGES_H_
T begin(T... args)
Return true value if T has std::string interface, like std::string_view.
Definition ranges.h:75
static void check(...)
static auto check(U *p) -> decltype((void) p->find('a'), p->length(),(void) p->data(), int())
tuple_size and tuple_element check.
Definition ranges.h:191
static void check(...)
static auto check(U *p) -> decltype(std::tuple_size< U >::value, int())
Definition core.h:1120
std::basic_string< Char > format(const text_style &ts, const S &format_str, const Args &... args)
Definition color.h:583
T copy(T... args)
auto arg(const Char *name, const T &arg) -> detail::named_arg< Char, T >
Definition core.h:1725
#define FMT_MODULE_EXPORT_BEGIN
Definition core.h:242
#define FMT_CONSTEXPR
Definition core.h:99
type
Definition core.h:1048
#define FMT_BEGIN_NAMESPACE
Definition core.h:235
#define FMT_ENABLE_IF(...)
Definition core.h:347
void void_t
Definition core.h:1501
basic_format_args< format_context > format_args
Definition core.h:1854
typename std::remove_cv< remove_reference_t< T > >::type remove_cvref_t
Definition core.h:328
T
Definition core.h:320
#define FMT_END_NAMESPACE
Definition core.h:230
#define FMT_MODULE_EXPORT_END
Definition core.h:243
#define FMT_NOEXCEPT
Definition core.h:156
#define FMT_CONSTEXPR_DECL
Definition core.h:100
T end(T... args)
auto join(It begin, Sentinel end, string_view sep) -> join_view< It, Sentinel >
Definition format.h:2562
T make_shared(T... args)
Definition args.h:19
OutputIt write_delimiter(OutputIt out)
Definition ranges.h:248
remove_cvref_t< decltype(*detail::range_begin(std::declval< Range >()))> value_type
Definition ranges.h:246
auto range_begin(const T(&arr)[N]) -> const T *
Definition ranges.h:103
FMT_CONSTEXPR make_index_sequence< std::tuple_size< T >::value > get_indexes(T const &)
Definition ranges.h:234
OutputIterator copy(const RangeT &range, OutputIterator out)
Definition ranges.h:50
OutputIt write_range_entry(OutputIt out, const Arg &v)
Definition ranges.h:257
auto range_end(const T(&arr)[N]) -> const T *
Definition ranges.h:107
void for_each(index_sequence< Is... >, Tuple &&tup, F &&f) FMT_NOEXCEPT
Definition ranges.h:226
#define FMT_DECLTYPE_RETURN(val)
Definition ranges.h:95
static FMT_CONSTEXPR size_t size()
Definition ranges.h:211
auto format(const TupleT &values, FormatContext &ctx) -> decltype(ctx.out())
Definition ranges.h:313
FMT_CONSTEXPR auto parse(ParseContext &ctx) -> decltype(ctx.begin())
Definition ranges.h:308
auto format(const tuple_join_view< Char, T... > &value, FormatContext &ctx, detail::index_sequence< N... >) -> typename FormatContext::iterator
Definition ranges.h:392
FMT_CONSTEXPR auto parse(ParseContext &ctx) -> decltype(ctx.begin())
Definition ranges.h:380
auto format(const tuple_join_view< Char, T... > &value, FormatContext &ctx) -> typename FormatContext::iterator
Definition ranges.h:385
auto format_args(const tuple_join_view< Char, T... > &value, FormatContext &ctx, const Arg &arg, const Args &... args) -> typename FormatContext::iterator
Definition ranges.h:408
auto format_args(const tuple_join_view< Char, T... > &, FormatContext &ctx) -> typename FormatContext::iterator
Definition ranges.h:400
FMT_CONSTEXPR auto parse(ParseContext &ctx) -> decltype(ctx.begin())
Definition ranges.h:32
FMT_CONSTEXPR auto parse(ParseContext &ctx) -> decltype(ctx.begin())
Definition ranges.h:42
tuple_join_view(const std::tuple< T... > &t, basic_string_view< Char > s)
Definition ranges.h:370
const std::tuple< T... > & tuple
Definition ranges.h:367
basic_string_view< Char > sep
Definition ranges.h:368
Definition core.h:971