// Formatting library for C++ - experimental range support // // Copyright (c) 2012 - present, Victor Zverovich // All rights reserved. // // For the license information refer to format.h. // // Copyright (c) 2018 - present, Remotion (Igor Schulz) // All Rights Reserved // {fmt} support for ranges, containers and types tuple interface. #ifndef FMT_RANGES_H_ #define FMT_RANGES_H_ #include #include #include "format.h" FMT_BEGIN_NAMESPACE template struct formatting_base { template FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) { return ctx.begin(); } }; template struct formatting_range : formatting_base { #ifdef FMT_DEPRECATED_BRACED_RANGES Char prefix = '{'; Char postfix = '}'; #else Char prefix = '['; Char postfix = ']'; #endif }; template struct formatting_tuple : formatting_base { Char prefix = '('; Char postfix = ')'; }; namespace detail { template OutputIterator copy(const RangeT& range, OutputIterator out) { for (auto it = range.begin(), end = range.end(); it != end; ++it) *out++ = *it; return out; } template OutputIterator copy(const char* str, OutputIterator out) { while (*str) *out++ = *str++; return out; } template OutputIterator copy(char ch, OutputIterator out) { *out++ = ch; return out; } template OutputIterator copy(wchar_t ch, OutputIterator out) { *out++ = ch; return out; } /// Return true value if T has std::string interface, like std::string_view. template class is_std_string_like { template static auto check(U* p) -> decltype((void)p->find('a'), p->length(), (void)p->data(), int()); template static void check(...); public: static FMT_CONSTEXPR_DECL const bool value = is_string::value || !std::is_void(nullptr))>::value; }; template struct is_std_string_like> : std::true_type {}; template struct conditional_helper {}; template struct is_range_ : std::false_type {}; #if !FMT_MSC_VER || FMT_MSC_VER > 1800 # define FMT_DECLTYPE_RETURN(val) \ ->decltype(val) { return val; } \ static_assert( \ true, "") // This makes it so that a semicolon is required after the // macro, which helps clang-format handle the formatting. // C array overload template auto range_begin(const T (&arr)[N]) -> const T* { return arr; } template auto range_end(const T (&arr)[N]) -> const T* { return arr + N; } template struct has_member_fn_begin_end_t : std::false_type {}; template struct has_member_fn_begin_end_t().begin()), decltype(std::declval().end())>> : std::true_type {}; // Member function overload template auto range_begin(T&& rng) FMT_DECLTYPE_RETURN(static_cast(rng).begin()); template auto range_end(T&& rng) FMT_DECLTYPE_RETURN(static_cast(rng).end()); // ADL overload. Only participates in overload resolution if member functions // are not found. template auto range_begin(T&& rng) -> enable_if_t::value, decltype(begin(static_cast(rng)))> { return begin(static_cast(rng)); } template auto range_end(T&& rng) -> enable_if_t::value, decltype(end(static_cast(rng)))> { return end(static_cast(rng)); } template struct has_const_begin_end : std::false_type {}; template struct has_mutable_begin_end : std::false_type {}; template struct has_const_begin_end< T, void_t&>())), decltype(detail::range_begin( std::declval&>()))>> : std::true_type {}; template struct has_mutable_begin_end< T, void_t())), decltype(detail::range_begin(std::declval())), enable_if_t::value>>> : std::true_type {}; template struct is_range_ : std::integral_constant::value || has_mutable_begin_end::value)> {}; template struct range_to_view; template struct range_to_view::value>> { struct view_t { const T* m_range_ptr; auto begin() const FMT_DECLTYPE_RETURN(detail::range_begin(*m_range_ptr)); auto end() const FMT_DECLTYPE_RETURN(detail::range_end(*m_range_ptr)); }; static auto view(const T& range) -> view_t { return {&range}; } }; template struct range_to_view::value && has_mutable_begin_end::value>> { struct view_t { T m_range_copy; auto begin() FMT_DECLTYPE_RETURN(detail::range_begin(m_range_copy)); auto end() FMT_DECLTYPE_RETURN(detail::range_end(m_range_copy)); }; static auto view(const T& range) -> view_t { return {range}; } }; # undef FMT_DECLTYPE_RETURN #endif /// tuple_size and tuple_element check. template class is_tuple_like_ { template static auto check(U* p) -> decltype(std::tuple_size::value, int()); template static void check(...); public: static FMT_CONSTEXPR_DECL const bool value = !std::is_void(nullptr))>::value; }; // Check for integer_sequence #if defined(__cpp_lib_integer_sequence) || FMT_MSC_VER >= 1900 template using integer_sequence = std::integer_sequence; template using index_sequence = std::index_sequence; template using make_index_sequence = std::make_index_sequence; #else template struct integer_sequence { using value_type = T; static FMT_CONSTEXPR size_t size() { return sizeof...(N); } }; template using index_sequence = integer_sequence; template struct make_integer_sequence : make_integer_sequence {}; template struct make_integer_sequence : integer_sequence {}; template using make_index_sequence = make_integer_sequence; #endif template void for_each(index_sequence, Tuple&& tup, F&& f) FMT_NOEXCEPT { using std::get; // using free function get(T) now. const int _[] = {0, ((void)f(get(tup)), 0)...}; (void)_; // blocks warnings } template FMT_CONSTEXPR make_index_sequence::value> get_indexes( T const&) { return {}; } template void for_each(Tuple&& tup, F&& f) { const auto indexes = get_indexes(tup); for_each(indexes, std::forward(tup), std::forward(f)); } template using value_type = remove_cvref_t()))>; template OutputIt write_delimiter(OutputIt out) { *out++ = ','; *out++ = ' '; return out; } template < typename Char, typename OutputIt, typename Arg, FMT_ENABLE_IF(is_std_string_like::type>::value)> OutputIt write_range_entry(OutputIt out, const Arg& v) { *out++ = '"'; out = write(out, v); *out++ = '"'; return out; } template ::value)> OutputIt write_range_entry(OutputIt out, const Arg v) { *out++ = '\''; *out++ = v; *out++ = '\''; return out; } template < typename Char, typename OutputIt, typename Arg, FMT_ENABLE_IF(!is_std_string_like::type>::value && !std::is_same::value)> OutputIt write_range_entry(OutputIt out, const Arg& v) { return write(out, v); } } // namespace detail template struct is_tuple_like { static FMT_CONSTEXPR_DECL const bool value = detail::is_tuple_like_::value && !detail::is_range_::value; }; template struct formatter::value>> { private: // C++11 generic lambda for format() template struct format_each { template void operator()(const T& v) { if (i > 0) out = detail::write_delimiter(out); out = detail::write_range_entry(out, v); ++i; } formatting_tuple& formatting; size_t& i; typename std::add_lvalue_reference< decltype(std::declval().out())>::type out; }; public: formatting_tuple formatting; template FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) { return formatting.parse(ctx); } template auto format(const TupleT& values, FormatContext& ctx) -> decltype(ctx.out()) { auto out = ctx.out(); size_t i = 0; detail::copy(formatting.prefix, out); detail::for_each(values, format_each{formatting, i, out}); detail::copy(formatting.postfix, out); return ctx.out(); } }; template struct is_range { static FMT_CONSTEXPR_DECL const bool value = detail::is_range_::value && !detail::is_std_string_like::value && !std::is_convertible>::value && !std::is_constructible, T>::value; }; template struct formatter< T, Char, enable_if_t< fmt::is_range::value // Workaround a bug in MSVC 2017 and earlier. #if !FMT_MSC_VER || FMT_MSC_VER >= 1927 && (has_formatter, format_context>::value || detail::has_fallback_formatter, Char>::value) #endif >> { formatting_range formatting; template FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) { return formatting.parse(ctx); } template typename FormatContext::iterator format(const T& values, FormatContext& ctx) { auto out = detail::copy(formatting.prefix, ctx.out()); size_t i = 0; auto view = detail::range_to_view::view(values); auto it = view.begin(); auto end = view.end(); for (; it != end; ++it) { if (i > 0) out = detail::write_delimiter(out); out = detail::write_range_entry(out, *it); ++i; } return detail::copy(formatting.postfix, out); } }; template struct tuple_arg_join : detail::view { const std::tuple& tuple; basic_string_view sep; tuple_arg_join(const std::tuple& t, basic_string_view s) : tuple(t), sep{s} {} }; template struct formatter, Char> { template FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) { return ctx.begin(); } template typename FormatContext::iterator format( const tuple_arg_join& value, FormatContext& ctx) { return format(value, ctx, detail::make_index_sequence{}); } private: template typename FormatContext::iterator format( const tuple_arg_join& value, FormatContext& ctx, detail::index_sequence) { return format_args(value, ctx, std::get(value.tuple)...); } template typename FormatContext::iterator format_args( const tuple_arg_join&, FormatContext& ctx) { // NOTE: for compilers that support C++17, this empty function instantiation // can be replaced with a constexpr branch in the variadic overload. return ctx.out(); } template typename FormatContext::iterator format_args( const tuple_arg_join& value, FormatContext& ctx, const Arg& arg, const Args&... args) { using base = formatter::type, Char>; auto out = ctx.out(); out = base{}.format(arg, ctx); if (sizeof...(Args) > 0) { out = std::copy(value.sep.begin(), value.sep.end(), out); ctx.advance_to(out); return format_args(value, ctx, args...); } return out; } }; FMT_MODULE_EXPORT_BEGIN /** \rst Returns an object that formats `tuple` with elements separated by `sep`. **Example**:: std::tuple t = {1, 'a'}; fmt::print("{}", fmt::join(t, ", ")); // Output: "1, a" \endrst */ template FMT_CONSTEXPR tuple_arg_join join(const std::tuple& tuple, string_view sep) { return {tuple, sep}; } template FMT_CONSTEXPR tuple_arg_join join( const std::tuple& tuple, basic_string_view sep) { return {tuple, sep}; } /** \rst Returns an object that formats `initializer_list` with elements separated by `sep`. **Example**:: fmt::print("{}", fmt::join({1, 2, 3}, ", ")); // Output: "1, 2, 3" \endrst */ template join_view join(std::initializer_list list, string_view sep) { return join(std::begin(list), std::end(list), sep); } FMT_MODULE_EXPORT_END FMT_END_NAMESPACE #endif // FMT_RANGES_H_