Debian Codegraph @

podofo/3rdparty/fmtlib/fmt/ranges.h

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