WPILibC++ 2025.2.1
Loading...
Searching...
No Matches
ranges.h
Go to the documentation of this file.
1// Formatting library for C++ - range and tuple support
2//
3// Copyright (c) 2012 - present, Victor Zverovich and {fmt} contributors
4// All rights reserved.
5//
6// For the license information refer to format.h.
7
8#ifndef FMT_RANGES_H_
9#define FMT_RANGES_H_
10
11#ifndef FMT_MODULE
12# include <initializer_list>
13# include <iterator>
14# include <string>
15# include <tuple>
16# include <type_traits>
17# include <utility>
18#endif
19
20#include "format.h"
21
23
26
27namespace detail {
28
29template <typename T> class is_map {
30 template <typename U> static auto check(U*) -> typename U::mapped_type;
31 template <typename> static void check(...);
32
33 public:
34 static constexpr const bool value =
35 !std::is_void<decltype(check<T>(nullptr))>::value;
36};
37
38template <typename T> class is_set {
39 template <typename U> static auto check(U*) -> typename U::key_type;
40 template <typename> static void check(...);
41
42 public:
43 static constexpr const bool value =
44 !std::is_void<decltype(check<T>(nullptr))>::value && !is_map<T>::value;
45};
46
47// C array overload
48template <typename T, std::size_t N>
49auto range_begin(const T (&arr)[N]) -> const T* {
50 return arr;
51}
52template <typename T, std::size_t N>
53auto range_end(const T (&arr)[N]) -> const T* {
54 return arr + N;
55}
56
57template <typename T, typename Enable = void>
58struct has_member_fn_begin_end_t : std::false_type {};
59
60template <typename T>
61struct has_member_fn_begin_end_t<T, void_t<decltype(*std::declval<T>().begin()),
62 decltype(std::declval<T>().end())>>
63 : std::true_type {};
64
65// Member function overloads.
66template <typename T>
67auto range_begin(T&& rng) -> decltype(static_cast<T&&>(rng).begin()) {
68 return static_cast<T&&>(rng).begin();
69}
70template <typename T>
71auto range_end(T&& rng) -> decltype(static_cast<T&&>(rng).end()) {
72 return static_cast<T&&>(rng).end();
73}
74
75// ADL overloads. Only participate in overload resolution if member functions
76// are not found.
77template <typename T>
78auto range_begin(T&& rng)
80 decltype(begin(static_cast<T&&>(rng)))> {
81 return begin(static_cast<T&&>(rng));
82}
83template <typename T>
85 decltype(end(static_cast<T&&>(rng)))> {
86 return end(static_cast<T&&>(rng));
87}
88
89template <typename T, typename Enable = void>
90struct has_const_begin_end : std::false_type {};
91template <typename T, typename Enable = void>
92struct has_mutable_begin_end : std::false_type {};
93
94template <typename T>
96 T, void_t<decltype(*detail::range_begin(
97 std::declval<const remove_cvref_t<T>&>())),
98 decltype(detail::range_end(
99 std::declval<const remove_cvref_t<T>&>()))>>
100 : std::true_type {};
101
102template <typename T>
104 T, void_t<decltype(*detail::range_begin(std::declval<T&>())),
105 decltype(detail::range_end(std::declval<T&>())),
106 // the extra int here is because older versions of MSVC don't
107 // SFINAE properly unless there are distinct types
108 int>> : std::true_type {};
109
110template <typename T, typename _ = void> struct is_range_ : std::false_type {};
111template <typename T>
112struct is_range_<T, void>
113 : std::integral_constant<bool, (has_const_begin_end<T>::value ||
114 has_mutable_begin_end<T>::value)> {};
115
116// tuple_size and tuple_element check.
117template <typename T> class is_tuple_like_ {
118 template <typename U, typename V = typename std::remove_cv<U>::type>
119 static auto check(U* p) -> decltype(std::tuple_size<V>::value, 0);
120 template <typename> static void check(...);
121
122 public:
123 static constexpr const bool value =
124 !std::is_void<decltype(check<T>(nullptr))>::value;
125};
126
127// Check for integer_sequence
128#if defined(__cpp_lib_integer_sequence) || FMT_MSC_VERSION >= 1900
129template <typename T, T... N>
130using integer_sequence = std::integer_sequence<T, N...>;
131template <size_t... N> using index_sequence = std::index_sequence<N...>;
132template <size_t N> using make_index_sequence = std::make_index_sequence<N>;
133#else
134template <typename T, T... N> struct integer_sequence {
135 using value_type = T;
136
137 static FMT_CONSTEXPR auto size() -> size_t { return sizeof...(N); }
138};
139
140template <size_t... N> using index_sequence = integer_sequence<size_t, N...>;
141
142template <typename T, size_t N, T... Ns>
143struct make_integer_sequence : make_integer_sequence<T, N - 1, N - 1, Ns...> {};
144template <typename T, T... Ns>
145struct make_integer_sequence<T, 0, Ns...> : integer_sequence<T, Ns...> {};
146
147template <size_t N>
149#endif
150
151template <typename T>
153
154template <typename T, typename C, bool = is_tuple_like_<T>::value>
156 public:
157 static constexpr const bool value = false;
158};
159template <typename T, typename C> class is_tuple_formattable_<T, C, true> {
160 template <size_t... Is>
161 static auto all_true(index_sequence<Is...>,
162 integer_sequence<bool, (Is >= 0)...>) -> std::true_type;
163 static auto all_true(...) -> std::false_type;
164
165 template <size_t... Is>
166 static auto check(index_sequence<Is...>) -> decltype(all_true(
168 integer_sequence<bool,
170 C>::value)...>{}));
171
172 public:
173 static constexpr const bool value =
174 decltype(check(tuple_index_sequence<T>{}))::value;
175};
176
177template <typename Tuple, typename F, size_t... Is>
179 using std::get;
180 // Using a free function get<Is>(Tuple) now.
181 const int unused[] = {0, ((void)f(get<Is>(t)), 0)...};
182 ignore_unused(unused);
183}
184
185template <typename Tuple, typename F>
186FMT_CONSTEXPR void for_each(Tuple&& t, F&& f) {
188 std::forward<Tuple>(t), std::forward<F>(f));
189}
190
191template <typename Tuple1, typename Tuple2, typename F, size_t... Is>
192void for_each2(index_sequence<Is...>, Tuple1&& t1, Tuple2&& t2, F&& f) {
193 using std::get;
194 const int unused[] = {0, ((void)f(get<Is>(t1), get<Is>(t2)), 0)...};
195 ignore_unused(unused);
196}
197
198template <typename Tuple1, typename Tuple2, typename F>
199void for_each2(Tuple1&& t1, Tuple2&& t2, F&& f) {
201 std::forward<Tuple1>(t1), std::forward<Tuple2>(t2),
202 std::forward<F>(f));
203}
204
205namespace tuple {
206// Workaround a bug in MSVC 2019 (v140).
207template <typename Char, typename... T>
208using result_t = std::tuple<formatter<remove_cvref_t<T>, Char>...>;
209
210using std::get;
211template <typename Tuple, typename Char, std::size_t... Is>
213 -> result_t<Char, decltype(get<Is>(std::declval<Tuple>()))...>;
214} // namespace tuple
215
216#if FMT_MSC_VERSION && FMT_MSC_VERSION < 1920
217// Older MSVC doesn't get the reference type correctly for arrays.
218template <typename R> struct range_reference_type_impl {
219 using type = decltype(*detail::range_begin(std::declval<R&>()));
220};
221
222template <typename T, std::size_t N> struct range_reference_type_impl<T[N]> {
223 using type = T&;
224};
225
226template <typename T>
227using range_reference_type = typename range_reference_type_impl<T>::type;
228#else
229template <typename Range>
231 decltype(*detail::range_begin(std::declval<Range&>()));
232#endif
233
234// We don't use the Range's value_type for anything, but we do need the Range's
235// reference type, with cv-ref stripped.
236template <typename Range>
238
239template <typename Formatter>
241 -> decltype(f.set_debug_format(set)) {
242 f.set_debug_format(set);
243}
244template <typename Formatter>
245FMT_CONSTEXPR void maybe_set_debug_format(Formatter&, ...) {}
246
247template <typename T>
249 : std::integral_constant<range_format,
250 std::is_same<uncvref_type<T>, T>::value
251 ? range_format::disabled
252 : is_map<T>::value ? range_format::map
253 : is_set<T>::value ? range_format::set
254 : range_format::sequence> {};
255
256template <range_format K>
257using range_format_constant = std::integral_constant<range_format, K>;
258
259// These are not generic lambdas for compatibility with C++11.
260template <typename Char> struct parse_empty_specs {
261 template <typename Formatter> FMT_CONSTEXPR void operator()(Formatter& f) {
262 f.parse(ctx);
264 }
266};
267template <typename FormatContext> struct format_tuple_element {
268 using char_type = typename FormatContext::char_type;
269
270 template <typename T>
271 void operator()(const formatter<T, char_type>& f, const T& v) {
272 if (i > 0) ctx.advance_to(detail::copy<char_type>(separator, ctx.out()));
273 ctx.advance_to(f.format(v, ctx));
274 ++i;
275 }
276
277 int i;
278 FormatContext& ctx;
280};
281
282} // namespace detail
283
284template <typename T> struct is_tuple_like {
285 static constexpr const bool value =
287};
288
289template <typename T, typename C> struct is_tuple_formattable {
290 static constexpr const bool value =
292};
293
294template <typename Tuple, typename Char>
295struct formatter<Tuple, Char,
296 enable_if_t<fmt::is_tuple_like<Tuple>::value &&
297 fmt::is_tuple_formattable<Tuple, Char>::value>> {
298 private:
301
302 basic_string_view<Char> separator_ = detail::string_literal<Char, ',', ' '>{};
303 basic_string_view<Char> opening_bracket_ =
304 detail::string_literal<Char, '('>{};
305 basic_string_view<Char> closing_bracket_ =
306 detail::string_literal<Char, ')'>{};
307
308 public:
310
312 separator_ = sep;
313 }
314
317 opening_bracket_ = open;
318 closing_bracket_ = close;
319 }
320
321 FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) -> const Char* {
322 auto it = ctx.begin();
323 auto end = ctx.end();
324 if (it != end && detail::to_ascii(*it) == 'n') {
325 ++it;
326 set_brackets({}, {});
327 set_separator({});
328 }
329 if (it != end && *it != '}') report_error("invalid format specifier");
330 ctx.advance_to(it);
332 return it;
333 }
334
335 template <typename FormatContext>
336 auto format(const Tuple& value, FormatContext& ctx) const
337 -> decltype(ctx.out()) {
338 ctx.advance_to(detail::copy<Char>(opening_bracket_, ctx.out()));
340 formatters_, value,
342 return detail::copy<Char>(closing_bracket_, ctx.out());
343 }
344};
345
346template <typename T, typename Char> struct is_range {
347 static constexpr const bool value =
349};
350
351namespace detail {
352
353template <typename Char, typename Element>
355
356template <typename R>
359
360template <typename R, typename Char>
362 : is_formattable<uncvref_type<maybe_const_range<R>>, Char> {};
363} // namespace detail
364
365template <typename...> struct conjunction : std::true_type {};
366template <typename P> struct conjunction<P> : P {};
367template <typename P1, typename... Pn>
368struct conjunction<P1, Pn...>
369 : conditional_t<bool(P1::value), conjunction<Pn...>, P1> {};
370
371template <typename T, typename Char, typename Enable = void>
373
374template <typename T, typename Char>
376 T, Char,
377 enable_if_t<conjunction<std::is_same<T, remove_cvref_t<T>>,
378 is_formattable<T, Char>>::value>> {
379 private:
381 basic_string_view<Char> separator_ = detail::string_literal<Char, ',', ' '>{};
382 basic_string_view<Char> opening_bracket_ =
383 detail::string_literal<Char, '['>{};
384 basic_string_view<Char> closing_bracket_ =
385 detail::string_literal<Char, ']'>{};
386 bool is_debug = false;
387
388 template <typename Output, typename It, typename Sentinel, typename U = T,
389 FMT_ENABLE_IF(std::is_same<U, Char>::value)>
390 auto write_debug_string(Output& out, It it, Sentinel end) const -> Output {
391 auto buf = basic_memory_buffer<Char>();
392 for (; it != end; ++it) buf.push_back(*it);
393 auto specs = format_specs();
394 specs.set_type(presentation_type::debug);
395 return detail::write<Char>(
396 out, basic_string_view<Char>(buf.data(), buf.size()), specs);
397 }
398
399 template <typename Output, typename It, typename Sentinel, typename U = T,
400 FMT_ENABLE_IF(!std::is_same<U, Char>::value)>
401 auto write_debug_string(Output& out, It, Sentinel) const -> Output {
402 return out;
403 }
404
405 public:
407
409 return underlying_;
410 }
411
413 separator_ = sep;
414 }
415
418 opening_bracket_ = open;
419 closing_bracket_ = close;
420 }
421
422 FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) -> const Char* {
423 auto it = ctx.begin();
424 auto end = ctx.end();
425 detail::maybe_set_debug_format(underlying_, true);
426 if (it == end) return underlying_.parse(ctx);
427
428 switch (detail::to_ascii(*it)) {
429 case 'n':
430 set_brackets({}, {});
431 ++it;
432 break;
433 case '?':
434 is_debug = true;
435 set_brackets({}, {});
436 ++it;
437 if (it == end || *it != 's') report_error("invalid format specifier");
439 case 's':
440 if (!std::is_same<T, Char>::value)
441 report_error("invalid format specifier");
442 if (!is_debug) {
443 set_brackets(detail::string_literal<Char, '"'>{},
444 detail::string_literal<Char, '"'>{});
445 set_separator({});
446 detail::maybe_set_debug_format(underlying_, false);
447 }
448 ++it;
449 return it;
450 }
451
452 if (it != end && *it != '}') {
453 if (*it != ':') report_error("invalid format specifier");
454 detail::maybe_set_debug_format(underlying_, false);
455 ++it;
456 }
457
458 ctx.advance_to(it);
459 return underlying_.parse(ctx);
460 }
461
462 template <typename R, typename FormatContext>
463 auto format(R&& range, FormatContext& ctx) const -> decltype(ctx.out()) {
464 auto out = ctx.out();
465 auto it = detail::range_begin(range);
466 auto end = detail::range_end(range);
467 if (is_debug) return write_debug_string(out, std::move(it), end);
468
469 out = detail::copy<Char>(opening_bracket_, out);
470 int i = 0;
471 for (; it != end; ++it) {
472 if (i > 0) out = detail::copy<Char>(separator_, out);
473 ctx.advance_to(out);
474 auto&& item = *it; // Need an lvalue
475 out = underlying_.format(item, ctx);
476 ++i;
477 }
478 out = detail::copy<Char>(closing_bracket_, out);
479 return out;
480 }
481};
482
484template <typename T, typename Char, typename Enable = void>
487 is_range<T, Char>::value, detail::range_format_kind_<T>,
488 std::integral_constant<range_format, range_format::disabled>> {};
489
490template <typename R, typename Char>
492 R, Char,
495 range_format_kind<R, Char>::value != range_format::disabled &&
496 range_format_kind<R, Char>::value != range_format::map &&
497 range_format_kind<R, Char>::value != range_format::string &&
499 detail::is_formattable_delayed<R, Char>>::value>> {
500 private:
501 using range_type = detail::maybe_const_range<R>;
503
504 public:
505 using nonlocking = void;
506
510 return;
511 range_formatter_.set_brackets(detail::string_literal<Char, '{'>{},
512 detail::string_literal<Char, '}'>{});
513 }
514
515 FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) -> const Char* {
516 return range_formatter_.parse(ctx);
517 }
518
519 template <typename FormatContext>
520 auto format(range_type& range, FormatContext& ctx) const
521 -> decltype(ctx.out()) {
522 return range_formatter_.format(range, ctx);
523 }
524};
525
526// A map formatter.
527template <typename R, typename Char>
529 R, Char,
530 enable_if_t<range_format_kind<R, Char>::value == range_format::map>> {
531 private:
532 using map_type = detail::maybe_const_range<R>;
533 using element_type = detail::uncvref_type<map_type>;
534
537 bool no_delimiters_ = false;
538
539 public:
541
542 FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) -> const Char* {
543 auto it = ctx.begin();
544 auto end = ctx.end();
545 if (it != end) {
546 if (detail::to_ascii(*it) == 'n') {
547 no_delimiters_ = true;
548 ++it;
549 }
550 if (it != end && *it != '}') {
551 if (*it != ':') report_error("invalid format specifier");
552 ++it;
553 }
554 ctx.advance_to(it);
555 }
557 return it;
558 }
559
560 template <typename FormatContext>
561 auto format(map_type& map, FormatContext& ctx) const -> decltype(ctx.out()) {
562 auto out = ctx.out();
564 if (!no_delimiters_) out = detail::copy<Char>(open, out);
565 int i = 0;
566 basic_string_view<Char> sep = detail::string_literal<Char, ',', ' '>{};
567 for (auto&& value : map) {
568 if (i > 0) out = detail::copy<Char>(sep, out);
569 ctx.advance_to(out);
570 detail::for_each2(formatters_, value,
572 0, ctx, detail::string_literal<Char, ':', ' '>{}});
573 ++i;
574 }
576 if (!no_delimiters_) out = detail::copy<Char>(close, out);
577 return out;
578 }
579};
580
581// A (debug_)string formatter.
582template <typename R, typename Char>
584 R, Char,
586 range_format_kind<R, Char>::value ==
588 private:
589 using range_type = detail::maybe_const_range<R>;
590 using string_type =
591 conditional_t<std::is_constructible<
593 decltype(detail::range_begin(std::declval<R>())),
594 decltype(detail::range_end(std::declval<R>()))>::value,
595 detail::std_string_view<Char>, std::basic_string<Char>>;
596
598
599 public:
600 FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) -> const Char* {
601 return underlying_.parse(ctx);
602 }
603
604 template <typename FormatContext>
605 auto format(range_type& range, FormatContext& ctx) const
606 -> decltype(ctx.out()) {
607 auto out = ctx.out();
610 *out++ = '"';
611 out = underlying_.format(
612 string_type{detail::range_begin(range), detail::range_end(range)}, ctx);
615 *out++ = '"';
616 return out;
617 }
618};
619
620template <typename It, typename Sentinel, typename Char = char>
623 Sentinel end;
625
627 : begin(std::move(b)), end(e), sep(s) {}
628};
629
630template <typename It, typename Sentinel, typename Char>
631struct formatter<join_view<It, Sentinel, Char>, Char> {
632 private:
633 using value_type =
634#ifdef __cpp_lib_ranges
635 std::iter_value_t<It>;
636#else
637 typename std::iterator_traits<It>::value_type;
638#endif
639 formatter<remove_cvref_t<value_type>, Char> value_formatter_;
640
644
645 public:
646 using nonlocking = void;
647
648 FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) -> const Char* {
649 return value_formatter_.parse(ctx);
650 }
651
652 template <typename FormatContext>
653 auto format(view& value, FormatContext& ctx) const -> decltype(ctx.out()) {
654 using iter =
656 iter it = value.begin;
657 auto out = ctx.out();
658 if (it == value.end) return out;
659 out = value_formatter_.format(*it, ctx);
660 ++it;
661 while (it != value.end) {
662 out = detail::copy<Char>(value.sep.begin(), value.sep.end(), out);
663 ctx.advance_to(out);
664 out = value_formatter_.format(*it, ctx);
665 ++it;
666 }
667 return out;
668 }
669};
670
671template <typename Char, typename Tuple> struct tuple_join_view : detail::view {
672 const Tuple& tuple;
674
676 : tuple(t), sep{s} {}
677};
678
679// Define FMT_TUPLE_JOIN_SPECIFIERS to enable experimental format specifiers
680// support in tuple_join. It is disabled by default because of issues with
681// the dynamic width and precision.
682#ifndef FMT_TUPLE_JOIN_SPECIFIERS
683# define FMT_TUPLE_JOIN_SPECIFIERS 0
684#endif
685
686template <typename Char, typename Tuple>
687struct formatter<tuple_join_view<Char, Tuple>, Char,
688 enable_if_t<is_tuple_like<Tuple>::value>> {
689 FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) -> const Char* {
690 return do_parse(ctx, std::tuple_size<Tuple>());
691 }
692
693 template <typename FormatContext>
695 FormatContext& ctx) const -> typename FormatContext::iterator {
696 return do_format(value, ctx, std::tuple_size<Tuple>());
697 }
698
699 private:
702
703 FMT_CONSTEXPR auto do_parse(parse_context<Char>& ctx,
704 std::integral_constant<size_t, 0>)
705 -> const Char* {
706 return ctx.begin();
707 }
708
709 template <size_t N>
710 FMT_CONSTEXPR auto do_parse(parse_context<Char>& ctx,
711 std::integral_constant<size_t, N>)
712 -> const Char* {
713 auto end = ctx.begin();
714#if FMT_TUPLE_JOIN_SPECIFIERS
715 end = std::get<std::tuple_size<Tuple>::value - N>(formatters_).parse(ctx);
716 if (N > 1) {
717 auto end1 = do_parse(ctx, std::integral_constant<size_t, N - 1>());
718 if (end != end1)
719 report_error("incompatible format specs for tuple elements");
720 }
721#endif
722 return end;
723 }
724
725 template <typename FormatContext>
726 auto do_format(const tuple_join_view<Char, Tuple>&, FormatContext& ctx,
727 std::integral_constant<size_t, 0>) const ->
728 typename FormatContext::iterator {
729 return ctx.out();
730 }
731
732 template <typename FormatContext, size_t N>
733 auto do_format(const tuple_join_view<Char, Tuple>& value, FormatContext& ctx,
734 std::integral_constant<size_t, N>) const ->
735 typename FormatContext::iterator {
736 using std::get;
737 auto out =
738 std::get<std::tuple_size<Tuple>::value - N>(formatters_)
739 .format(get<std::tuple_size<Tuple>::value - N>(value.tuple), ctx);
740 if (N <= 1) return out;
741 out = detail::copy<Char>(value.sep, out);
742 ctx.advance_to(out);
743 return do_format(value, ctx, std::integral_constant<size_t, N - 1>());
744 }
745};
746
747namespace detail {
748// Check if T has an interface like a container adaptor (e.g. std::stack,
749// std::queue, std::priority_queue).
750template <typename T> class is_container_adaptor_like {
751 template <typename U> static auto check(U* p) -> typename U::container_type;
752 template <typename> static void check(...);
753
754 public:
755 static constexpr const bool value =
756 !std::is_void<decltype(check<T>(nullptr))>::value;
757};
758
759template <typename Container> struct all {
760 const Container& c;
761 auto begin() const -> typename Container::const_iterator { return c.begin(); }
762 auto end() const -> typename Container::const_iterator { return c.end(); }
763};
764} // namespace detail
765
766template <typename T, typename Char>
768 T, Char,
769 enable_if_t<conjunction<detail::is_container_adaptor_like<T>,
770 bool_constant<range_format_kind<T, Char>::value ==
771 range_format::disabled>>::value>>
772 : formatter<detail::all<typename T::container_type>, Char> {
774 template <typename FormatContext>
775 auto format(const T& t, FormatContext& ctx) const -> decltype(ctx.out()) {
776 struct getter : T {
777 static auto get(const T& t) -> all {
778 return {t.*(&getter::c)}; // Access c through the derived class.
779 }
780 };
781 return formatter<all>::format(getter::get(t), ctx);
782 }
783};
784
786
787/// Returns a view that formats the iterator range `[begin, end)` with elements
788/// separated by `sep`.
789template <typename It, typename Sentinel>
790auto join(It begin, Sentinel end, string_view sep) -> join_view<It, Sentinel> {
791 return {std::move(begin), end, sep};
792}
793
794/**
795 * Returns a view that formats `range` with elements separated by `sep`.
796 *
797 * **Example**:
798 *
799 * auto v = std::vector<int>{1, 2, 3};
800 * fmt::print("{}", fmt::join(v, ", "));
801 * // Output: 1, 2, 3
802 *
803 * `fmt::join` applies passed format specifiers to the range elements:
804 *
805 * fmt::print("{:02}", fmt::join(v, ", "));
806 * // Output: 01, 02, 03
807 */
808template <typename Range, FMT_ENABLE_IF(!is_tuple_like<Range>::value)>
809auto join(Range&& r, string_view sep)
810 -> join_view<decltype(detail::range_begin(r)),
811 decltype(detail::range_end(r))> {
812 return {detail::range_begin(r), detail::range_end(r), sep};
813}
814
815/**
816 * Returns an object that formats `std::tuple` with elements separated by `sep`.
817 *
818 * **Example**:
819 *
820 * auto t = std::tuple<int, char>{1, 'a'};
821 * fmt::print("{}", fmt::join(t, ", "));
822 * // Output: 1, a
823 */
824template <typename Tuple, FMT_ENABLE_IF(is_tuple_like<Tuple>::value)>
825FMT_CONSTEXPR auto join(const Tuple& tuple, string_view sep)
827 return {tuple, sep};
828}
829
830/**
831 * Returns an object that formats `std::initializer_list` with elements
832 * separated by `sep`.
833 *
834 * **Example**:
835 *
836 * fmt::print("{}", fmt::join({1, 2, 3}, ", "));
837 * // Output: "1, 2, 3"
838 */
839template <typename T>
840auto join(std::initializer_list<T> list, string_view sep)
842 return join(std::begin(list), std::end(list), sep);
843}
844
847
848#endif // FMT_RANGES_H_
constexpr T & get(wpi::array< T, N > &arr) noexcept
Definition array.h:66
A dynamically growing memory buffer for trivially copyable/constructible types with the first SIZE el...
Definition format.h:790
An implementation of std::basic_string_view for pre-C++17.
Definition base.h:504
Definition ranges.h:750
static constexpr const bool value
Definition ranges.h:755
Definition ranges.h:29
static constexpr const bool value
Definition ranges.h:34
Definition ranges.h:38
static constexpr const bool value
Definition ranges.h:43
Definition ranges.h:155
static constexpr const bool value
Definition ranges.h:157
Definition ranges.h:117
static constexpr const bool value
Definition ranges.h:123
Definition base.h:2081
Parsing context consisting of a format string range being parsed and an argument counter for automati...
Definition base.h:845
FMT_FUNC void report_error(const char *message)
Reports a format error at compile time or, via a format_error exception, at runtime.
Definition format-inl.h:135
FMT_INLINE auto format(detail::locale_ref loc, format_string< T... > fmt, T &&... args) -> std::string
Definition format.h:4146
std::tuple< formatter< remove_cvref_t< T >, Char >... > result_t
Definition ranges.h:208
auto get_formatters(index_sequence< Is... >) -> result_t< Char, decltype(get< Is >(std::declval< Tuple >()))... >
detail namespace with internal helper functions
Definition input_adapters.h:32
constexpr auto to_ascii(Char c) -> char
Definition base.h:1242
remove_cvref_t< range_reference_type< Range > > uncvref_type
Definition ranges.h:237
FMT_CONSTEXPR void ignore_unused(const T &...)
Definition base.h:348
decltype(*detail::range_begin(std::declval< Range & >())) range_reference_type
Definition ranges.h:230
FMT_CONSTEXPR auto write(OutputIt out, Char value, const format_specs &specs, locale_ref loc={}) -> OutputIt
Definition format.h:1824
auto range_begin(const T(&arr)[N]) -> const T *
Definition ranges.h:49
@ value
the parser finished reading a JSON value
FMT_CONSTEXPR void for_each(index_sequence< Is... >, Tuple &&t, F &&f)
Definition ranges.h:178
FMT_ALWAYS_INLINE constexpr auto const_check(T val) -> T
Definition base.h:367
conditional_t< has_const_begin_end< R >::value, const R, R > maybe_const_range
Definition ranges.h:357
FMT_CONSTEXPR20 auto copy(InputIt begin, InputIt end, OutputIt out) -> OutputIt
Definition base.h:1992
auto range_end(const T(&arr)[N]) -> const T *
Definition ranges.h:53
typename make_void< Ts... >::type void_t
Definition void_t.h:21
void for_each2(index_sequence< Is... >, Tuple1 &&t1, Tuple2 &&t2, F &&f)
Definition ranges.h:192
std::integral_constant< range_format, K > range_format_constant
Definition ranges.h:257
type
Definition base.h:937
FMT_CONSTEXPR auto maybe_set_debug_format(Formatter &f, bool set) -> decltype(f.set_debug_format(set))
Definition ranges.h:240
Implement std::hash so that hash_code can be used in STL containers.
Definition PointerIntPair.h:280
range_format
Definition ranges.h:25
FMT_BEGIN_EXPORT auto join(It begin, Sentinel end, string_view sep) -> join_view< It, Sentinel >
Returns a view that formats the iterator range [begin, end) with elements separated by sep.
Definition ranges.h:790
Definition ranges.h:365
Definition ranges.h:759
auto begin() const -> typename Container::const_iterator
Definition ranges.h:761
auto end() const -> typename Container::const_iterator
Definition ranges.h:762
const Container & c
Definition ranges.h:760
Definition ranges.h:267
int i
Definition ranges.h:277
FormatContext & ctx
Definition ranges.h:278
void operator()(const formatter< T, char_type > &f, const T &v)
Definition ranges.h:271
typename FormatContext::char_type char_type
Definition ranges.h:268
basic_string_view< char_type > separator
Definition ranges.h:279
Definition ranges.h:90
Definition ranges.h:58
Definition ranges.h:92
Definition base.h:925
Definition cpp_future.h:66
static FMT_CONSTEXPR auto size() -> size_t
Definition ranges.h:137
T value_type
Definition ranges.h:135
Definition ranges.h:362
Definition ranges.h:110
Definition ranges.h:143
Definition ranges.h:260
parse_context< Char > & ctx
Definition ranges.h:265
FMT_CONSTEXPR void operator()(Formatter &f)
Definition ranges.h:261
Definition ranges.h:254
Definition format.h:244
Definition format.h:247
Definition base.h:1011
Definition base.h:834
FMT_CONSTEXPR auto parse(parse_context< Char > &ctx) -> const Char *
Definition ranges.h:542
auto format(map_type &map, FormatContext &ctx) const -> decltype(ctx.out())
Definition ranges.h:561
FMT_CONSTEXPR void set_brackets(basic_string_view< Char > open, basic_string_view< Char > close)
Definition ranges.h:315
auto format(const Tuple &value, FormatContext &ctx) const -> decltype(ctx.out())
Definition ranges.h:336
FMT_CONSTEXPR auto parse(parse_context< Char > &ctx) -> const Char *
Definition ranges.h:321
FMT_CONSTEXPR auto parse(parse_context< Char > &ctx) -> const Char *
Definition ranges.h:648
auto format(view &value, FormatContext &ctx) const -> decltype(ctx.out())
Definition ranges.h:653
FMT_CONSTEXPR auto parse(parse_context< Char > &ctx) -> const Char *
Definition ranges.h:689
auto format(const tuple_join_view< Char, Tuple > &value, FormatContext &ctx) const -> typename FormatContext::iterator
Definition ranges.h:694
Definition base.h:651
Definition ranges.h:346
static constexpr const bool value
Definition ranges.h:347
Definition ranges.h:289
static constexpr const bool value
Definition ranges.h:290
Definition ranges.h:284
static constexpr const bool value
Definition ranges.h:285
Definition ranges.h:621
It begin
Definition ranges.h:622
join_view(It b, Sentinel e, basic_string_view< Char > s)
Definition ranges.h:626
Sentinel end
Definition ranges.h:623
basic_string_view< Char > sep
Definition ranges.h:624
Definition ranges.h:488
auto format(R &&range, FormatContext &ctx) const -> decltype(ctx.out())
Definition ranges.h:463
FMT_CONSTEXPR auto underlying() -> detail::range_formatter_type< Char, T > &
Definition ranges.h:408
FMT_CONSTEXPR void set_brackets(basic_string_view< Char > open, basic_string_view< Char > close)
Definition ranges.h:416
Definition ranges.h:372
Definition ranges.h:671
const Tuple & tuple
Definition ranges.h:672
basic_string_view< Char > sep
Definition ranges.h:673
tuple_join_view(const Tuple &t, basic_string_view< Char > s)
Definition ranges.h:675
typename std::enable_if< B, T >::type enable_if_t
Definition base.h:297
#define FMT_END_EXPORT
Definition base.h:250
std::integral_constant< bool, B > bool_constant
Definition base.h:300
#define FMT_FALLTHROUGH
Definition base.h:172
typename std::remove_cv< remove_reference_t< T > >::type remove_cvref_t
Definition base.h:306
#define FMT_CONSTEXPR
Definition base.h:113
FMT_NORETURN FMT_API void report_error(const char *message)
Reports a format error at compile time or, via a format_error exception, at runtime.
Definition format-inl.h:135
#define FMT_BEGIN_NAMESPACE
Definition base.h:239
#define FMT_ENABLE_IF(...)
Definition base.h:334
#define FMT_BEGIN_EXPORT
Definition base.h:249
typename std::conditional< B, T, F >::type conditional_t
Definition base.h:299
#define FMT_END_NAMESPACE
Definition base.h:242
#define FMT_EXPORT
Definition base.h:248
bool_constant<!std::is_same< detail::mapped_t< conditional_t< std::is_void< T >::value, int *, T >, Char >, void >::value > is_formattable
Definition base.h:2723