1 // Formatting library for C++ - experimental range support
3 // Copyright (c) 2012 - present, Victor Zverovich
4 // All rights reserved.
6 // For the license information refer to format.h.
8 // Copyright (c) 2018 - present, Remotion (Igor Schulz)
10 // {fmt} support for ranges, containers and types tuple interface.
15 #include <initializer_list>
16 #include <type_traits>
22 template <typename Char
, typename Enable
= void> struct formatting_range
{
23 #ifdef FMT_DEPRECATED_BRACED_RANGES
31 template <typename ParseContext
>
32 FMT_CONSTEXPR
auto parse(ParseContext
& ctx
) -> decltype(ctx
.begin()) {
37 template <typename Char
, typename Enable
= void> struct formatting_tuple
{
41 template <typename ParseContext
>
42 FMT_CONSTEXPR
auto parse(ParseContext
& ctx
) -> decltype(ctx
.begin()) {
49 template <typename RangeT
, typename OutputIterator
>
50 OutputIterator
copy(const RangeT
& range
, OutputIterator out
) {
51 for (auto it
= range
.begin(), end
= range
.end(); it
!= end
; ++it
)
56 template <typename OutputIterator
>
57 OutputIterator
copy(const char* str
, OutputIterator out
) {
58 while (*str
) *out
++ = *str
++;
62 template <typename OutputIterator
>
63 OutputIterator
copy(char ch
, OutputIterator out
) {
68 template <typename OutputIterator
>
69 OutputIterator
copy(wchar_t ch
, OutputIterator out
) {
74 /// Return true value if T has std::string interface, like std::string_view.
75 template <typename T
> class is_std_string_like
{
77 static auto check(U
* p
)
78 -> decltype((void)p
->find('a'), p
->length(), (void)p
->data(), int());
79 template <typename
> static void check(...);
82 static FMT_CONSTEXPR_DECL
const bool value
=
83 is_string
<T
>::value
|| !std::is_void
<decltype(check
<T
>(nullptr))>::value
;
86 template <typename Char
>
87 struct is_std_string_like
<fmt::basic_string_view
<Char
>> : std::true_type
{};
89 template <typename
... Ts
> struct conditional_helper
{};
91 template <typename T
, typename _
= void> struct is_range_
: std::false_type
{};
93 #if !FMT_MSC_VER || FMT_MSC_VER > 1800
95 # define FMT_DECLTYPE_RETURN(val) \
96 ->decltype(val) { return val; } \
98 true, "") // This makes it so that a semicolon is required after the
99 // macro, which helps clang-format handle the formatting.
102 template <typename T
, std::size_t N
>
103 auto range_begin(const T (&arr
)[N
]) -> const T
* {
106 template <typename T
, std::size_t N
>
107 auto range_end(const T (&arr
)[N
]) -> const T
* {
111 template <typename T
, typename Enable
= void>
112 struct has_member_fn_begin_end_t
: std::false_type
{};
114 template <typename T
>
115 struct has_member_fn_begin_end_t
<T
, void_t
<decltype(std::declval
<T
>().begin()),
116 decltype(std::declval
<T
>().end())>>
119 // Member function overload
120 template <typename T
>
121 auto range_begin(T
&& rng
) FMT_DECLTYPE_RETURN(static_cast<T
&&>(rng
).begin());
122 template <typename T
>
123 auto range_end(T
&& rng
) FMT_DECLTYPE_RETURN(static_cast<T
&&>(rng
).end());
125 // ADL overload. Only participates in overload resolution if member functions
127 template <typename T
>
128 auto 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
));
133 template <typename T
>
134 auto 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
));
139 template <typename T
, typename Enable
= void>
140 struct has_const_begin_end
: std::false_type
{};
141 template <typename T
, typename Enable
= void>
142 struct has_mutable_begin_end
: std::false_type
{};
144 template <typename T
>
145 struct has_const_begin_end
<
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
>&>()))>>
152 template <typename T
>
153 struct has_mutable_begin_end
<
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
>>>
159 template <typename T
>
160 struct is_range_
<T
, void>
161 : std::integral_constant
<bool, (has_const_begin_end
<T
>::value
||
162 has_mutable_begin_end
<T
>::value
)> {};
164 template <typename T
, typename Enable
= void> struct range_to_view
;
165 template <typename T
>
166 struct range_to_view
<T
, enable_if_t
<has_const_begin_end
<T
>::value
>> {
168 const T
* m_range_ptr
;
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
));
173 static auto view(const T
& range
) -> view_t
{ return {&range
}; }
176 template <typename T
>
177 struct range_to_view
<T
, enable_if_t
<!has_const_begin_end
<T
>::value
&&
178 has_mutable_begin_end
<T
>::value
>> {
182 auto begin() FMT_DECLTYPE_RETURN(detail::range_begin(m_range_copy
));
183 auto end() FMT_DECLTYPE_RETURN(detail::range_end(m_range_copy
));
185 static auto view(const T
& range
) -> view_t
{ return {range
}; }
187 # undef FMT_DECLTYPE_RETURN
190 /// tuple_size and tuple_element check.
191 template <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(...);
197 static FMT_CONSTEXPR_DECL
const bool value
=
198 !std::is_void
<decltype(check
<T
>(nullptr))>::value
;
201 // Check for integer_sequence
202 #if defined(__cpp_lib_integer_sequence) || FMT_MSC_VER >= 1900
203 template <typename T
, T
... N
>
204 using integer_sequence
= std::integer_sequence
<T
, N
...>;
205 template <size_t... N
> using index_sequence
= std::index_sequence
<N
...>;
206 template <size_t N
> using make_index_sequence
= std::make_index_sequence
<N
>;
208 template <typename T
, T
... N
> struct integer_sequence
{
209 using value_type
= T
;
211 static FMT_CONSTEXPR
size_t size() { return sizeof...(N
); }
214 template <size_t... N
> using index_sequence
= integer_sequence
<size_t, N
...>;
216 template <typename T
, size_t N
, T
... Ns
>
217 struct make_integer_sequence
: make_integer_sequence
<T
, N
- 1, N
- 1, Ns
...> {};
218 template <typename T
, T
... Ns
>
219 struct make_integer_sequence
<T
, 0, Ns
...> : integer_sequence
<T
, Ns
...> {};
222 using make_index_sequence
= make_integer_sequence
<size_t, N
>;
225 template <class Tuple
, class F
, size_t... Is
>
226 void for_each(index_sequence
<Is
...>, Tuple
&& tup
, F
&& f
) FMT_NOEXCEPT
{
228 // using free function get<I>(T) now.
229 const int _
[] = {0, ((void)f(get
<Is
>(tup
)), 0)...};
230 (void)_
; // blocks warnings
234 FMT_CONSTEXPR make_index_sequence
<std::tuple_size
<T
>::value
> get_indexes(
239 template <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
));
244 template <typename Range
>
246 remove_cvref_t
<decltype(*detail::range_begin(std::declval
<Range
>()))>;
248 template <typename OutputIt
> OutputIt
write_delimiter(OutputIt out
) {
255 typename Char
, typename OutputIt
, typename Arg
,
256 FMT_ENABLE_IF(is_std_string_like
<typename
std::decay
<Arg
>::type
>::value
)>
257 OutputIt
write_range_entry(OutputIt out
, const Arg
& v
) {
259 out
= write
<Char
>(out
, v
);
264 template <typename Char
, typename OutputIt
, typename Arg
,
265 FMT_ENABLE_IF(std::is_same
<Arg
, Char
>::value
)>
266 OutputIt
write_range_entry(OutputIt out
, const Arg v
) {
274 typename Char
, typename OutputIt
, typename Arg
,
275 FMT_ENABLE_IF(!is_std_string_like
<typename
std::decay
<Arg
>::type
>::value
&&
276 !std::is_same
<Arg
, Char
>::value
)>
277 OutputIt
write_range_entry(OutputIt out
, const Arg
& v
) {
278 return write
<Char
>(out
, v
);
281 } // namespace detail
283 template <typename T
> struct is_tuple_like
{
284 static FMT_CONSTEXPR_DECL
const bool value
=
285 detail::is_tuple_like_
<T
>::value
&& !detail::is_range_
<T
>::value
;
288 template <typename TupleT
, typename Char
>
289 struct formatter
<TupleT
, Char
, enable_if_t
<fmt::is_tuple_like
<TupleT
>::value
>> {
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
);
298 formatting_tuple
<Char
>& formatting
;
300 typename
std::add_lvalue_reference
<
301 decltype(std::declval
<FormatContext
>().out())>::type out
;
305 formatting_tuple
<Char
> formatting
;
307 template <typename ParseContext
>
308 FMT_CONSTEXPR
auto parse(ParseContext
& ctx
) -> decltype(ctx
.begin()) {
309 return formatting
.parse(ctx
);
312 template <typename FormatContext
= format_context
>
313 auto format(const TupleT
& values
, FormatContext
& ctx
) -> decltype(ctx
.out()) {
314 auto out
= ctx
.out();
317 detail::copy(formatting
.prefix
, out
);
318 detail::for_each(values
, format_each
<FormatContext
>{formatting
, i
, out
});
319 detail::copy(formatting
.postfix
, out
);
325 template <typename T
, typename Char
> struct is_range
{
326 static FMT_CONSTEXPR_DECL
const bool value
=
327 detail::is_range_
<T
>::value
&& !detail::is_std_string_like
<T
>::value
&&
328 !std::is_convertible
<T
, std::basic_string
<Char
>>::value
&&
329 !std::is_constructible
<detail::std_string_view
<Char
>, T
>::value
;
332 template <typename T
, typename Char
>
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
)
343 formatting_range
<Char
> formatting
;
345 template <typename ParseContext
>
346 FMT_CONSTEXPR
auto parse(ParseContext
& ctx
) -> decltype(ctx
.begin()) {
347 return formatting
.parse(ctx
);
350 template <typename FormatContext
>
351 typename
FormatContext::iterator
format(const T
& values
, FormatContext
& ctx
) {
352 auto out
= detail::copy(formatting
.prefix
, ctx
.out());
354 auto view
= detail::range_to_view
<T
>::view(values
);
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
);
362 return detail::copy(formatting
.postfix
, out
);
366 template <typename Char
, typename
... T
> struct tuple_join_view
: detail::view
{
367 const std::tuple
<T
...>& tuple
;
368 basic_string_view
<Char
> sep
;
370 tuple_join_view(const std::tuple
<T
...>& t
, basic_string_view
<Char
> s
)
371 : tuple(t
), sep
{s
} {}
374 template <typename Char
, typename
... T
>
375 using tuple_arg_join
= tuple_join_view
<Char
, T
...>;
377 template <typename Char
, typename
... T
>
378 struct formatter
<tuple_join_view
<Char
, T
...>, Char
> {
379 template <typename ParseContext
>
380 FMT_CONSTEXPR
auto parse(ParseContext
& ctx
) -> decltype(ctx
.begin()) {
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
)>{});
391 template <typename FormatContext
, size_t... N
>
392 auto format(const tuple_join_view
<Char
, T
...>& value
, FormatContext
& ctx
,
393 detail::index_sequence
<N
...>) ->
394 typename
FormatContext::iterator
{
395 return format_args(value
, ctx
, std::get
<N
>(value
.tuple
)...);
398 template <typename FormatContext
>
399 auto format_args(const tuple_join_view
<Char
, T
...>&, FormatContext
& ctx
) ->
400 typename
FormatContext::iterator
{
401 // NOTE: for compilers that support C++17, this empty function instantiation
402 // can be replaced with a constexpr branch in the variadic overload.
406 template <typename FormatContext
, typename Arg
, typename
... Args
>
407 auto format_args(const tuple_join_view
<Char
, T
...>& value
, FormatContext
& ctx
,
408 const Arg
& arg
, const Args
&... args
) ->
409 typename
FormatContext::iterator
{
410 using base
= formatter
<typename
std::decay
<Arg
>::type
, Char
>;
411 auto out
= base().format(arg
, ctx
);
412 if (sizeof...(Args
) > 0) {
413 out
= std::copy(value
.sep
.begin(), value
.sep
.end(), out
);
415 return format_args(value
, ctx
, args
...);
421 FMT_MODULE_EXPORT_BEGIN
425 Returns an object that formats `tuple` with elements separated by `sep`.
429 std::tuple<int, char> t = {1, 'a'};
430 fmt::print("{}", fmt::join(t, ", "));
434 template <typename
... T
>
435 FMT_CONSTEXPR
auto join(const std::tuple
<T
...>& tuple
, string_view sep
)
436 -> tuple_join_view
<char, T
...> {
440 template <typename
... T
>
441 FMT_CONSTEXPR
auto join(const std::tuple
<T
...>& tuple
,
442 basic_string_view
<wchar_t> sep
)
443 -> tuple_join_view
<wchar_t, T
...> {
449 Returns an object that formats `initializer_list` with elements separated by
454 fmt::print("{}", fmt::join({1, 2, 3}, ", "));
458 template <typename T
>
459 auto join(std::initializer_list
<T
> list
, string_view sep
)
460 -> join_view
<const T
*, const T
*> {
461 return join(std::begin(list
), std::end(list
), sep
);
464 FMT_MODULE_EXPORT_END
467 #endif // FMT_RANGES_H_