// Formatting library for C++ - range and tuple support // // Copyright (c) 2012 - present, Victor Zverovich and {fmt} contributors // All rights reserved. // // For the license information refer to format.h. #ifndef FMT_RANGES_H_ #define FMT_RANGES_H_ #ifndef FMT_MODULE # include # include # include # include # include # include #endif #include "format.h" FMT_BEGIN_NAMESPACE FMT_EXPORT enum class range_format { disabled, map, set, sequence, string, debug_string }; namespace detail { template class is_map { template static auto check(U*) -> typename U::mapped_type; template static void check(...); public: static constexpr const bool value = !std::is_void(nullptr))>::value; }; template class is_set { template static auto check(U*) -> typename U::key_type; template static void check(...); public: static constexpr const bool value = !std::is_void(nullptr))>::value && !is_map::value; }; template struct conditional_helper {}; template struct is_range_ : std::false_type {}; #if !FMT_MSC_VERSION || FMT_MSC_VERSION > 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 overloads. 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 overloads. Only participate 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_end( std::declval&>()))>> : std::true_type {}; template struct has_mutable_begin_end< T, void_t())), decltype(detail::range_end(std::declval())), // the extra int here is because older versions of MSVC don't // SFINAE properly unless there are distinct types int>> : std::true_type {}; template struct is_range_ : std::integral_constant::value || has_mutable_begin_end::value)> {}; # 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 constexpr const bool value = !std::is_void(nullptr))>::value; }; // Check for integer_sequence #if defined(__cpp_lib_integer_sequence) || FMT_MSC_VERSION >= 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 auto size() -> size_t { 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 using tuple_index_sequence = make_index_sequence::value>; template ::value> class is_tuple_formattable_ { public: static constexpr const bool value = false; }; template class is_tuple_formattable_ { template static auto all_true(index_sequence, integer_sequence= 0)...>) -> std::true_type; static auto all_true(...) -> std::false_type; template static auto check(index_sequence) -> decltype(all_true( index_sequence{}, integer_sequence::type, C>::value)...>{})); public: static constexpr const bool value = decltype(check(tuple_index_sequence{}))::value; }; template FMT_CONSTEXPR void for_each(index_sequence, Tuple&& t, F&& f) { using std::get; // Using a free function get(Tuple) now. const int unused[] = {0, ((void)f(get(t)), 0)...}; ignore_unused(unused); } template FMT_CONSTEXPR void for_each(Tuple&& t, F&& f) { for_each(tuple_index_sequence>(), std::forward(t), std::forward(f)); } template void for_each2(index_sequence, Tuple1&& t1, Tuple2&& t2, F&& f) { using std::get; const int unused[] = {0, ((void)f(get(t1), get(t2)), 0)...}; ignore_unused(unused); } template void for_each2(Tuple1&& t1, Tuple2&& t2, F&& f) { for_each2(tuple_index_sequence>(), std::forward(t1), std::forward(t2), std::forward(f)); } namespace tuple { // Workaround a bug in MSVC 2019 (v140). template using result_t = std::tuple, Char>...>; using std::get; template auto get_formatters(index_sequence) -> result_t(std::declval()))...>; } // namespace tuple #if FMT_MSC_VERSION && FMT_MSC_VERSION < 1920 // Older MSVC doesn't get the reference type correctly for arrays. template struct range_reference_type_impl { using type = decltype(*detail::range_begin(std::declval())); }; template struct range_reference_type_impl { using type = T&; }; template using range_reference_type = typename range_reference_type_impl::type; #else template using range_reference_type = decltype(*detail::range_begin(std::declval())); #endif // We don't use the Range's value_type for anything, but we do need the Range's // reference type, with cv-ref stripped. template using uncvref_type = remove_cvref_t>; template FMT_CONSTEXPR auto maybe_set_debug_format(Formatter& f, bool set) -> decltype(f.set_debug_format(set)) { f.set_debug_format(set); } template FMT_CONSTEXPR void maybe_set_debug_format(Formatter&, ...) {} template struct range_format_kind_ : std::integral_constant, T>::value ? range_format::disabled : is_map::value ? range_format::map : is_set::value ? range_format::set : range_format::sequence> {}; template using range_format_constant = std::integral_constant; // These are not generic lambdas for compatibility with C++11. template struct parse_empty_specs { template FMT_CONSTEXPR void operator()(Formatter& f) { f.parse(ctx); detail::maybe_set_debug_format(f, true); } ParseContext& ctx; }; template struct format_tuple_element { using char_type = typename FormatContext::char_type; template void operator()(const formatter& f, const T& v) { if (i > 0) ctx.advance_to(detail::copy(separator, ctx.out())); ctx.advance_to(f.format(v, ctx)); ++i; } int i; FormatContext& ctx; basic_string_view separator; }; } // namespace detail template struct is_tuple_like { static constexpr const bool value = detail::is_tuple_like_::value && !detail::is_range_::value; }; template struct is_tuple_formattable { static constexpr const bool value = detail::is_tuple_formattable_::value; }; template struct formatter::value && fmt::is_tuple_formattable::value>> { private: decltype(detail::tuple::get_formatters( detail::tuple_index_sequence())) formatters_; basic_string_view separator_ = detail::string_literal{}; basic_string_view opening_bracket_ = detail::string_literal{}; basic_string_view closing_bracket_ = detail::string_literal{}; public: FMT_CONSTEXPR formatter() {} FMT_CONSTEXPR void set_separator(basic_string_view sep) { separator_ = sep; } FMT_CONSTEXPR void set_brackets(basic_string_view open, basic_string_view close) { opening_bracket_ = open; closing_bracket_ = close; } template FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) { auto it = ctx.begin(); if (it != ctx.end() && *it != '}') report_error("invalid format specifier"); detail::for_each(formatters_, detail::parse_empty_specs{ctx}); return it; } template auto format(const Tuple& value, FormatContext& ctx) const -> decltype(ctx.out()) { ctx.advance_to(detail::copy(opening_bracket_, ctx.out())); detail::for_each2( formatters_, value, detail::format_tuple_element{0, ctx, separator_}); return detail::copy(closing_bracket_, ctx.out()); } }; template struct is_range { static constexpr const bool value = detail::is_range_::value && !detail::has_to_string_view::value; }; namespace detail { template struct range_mapper { using mapper = arg_mapper; template , Context>::value)> static auto map(T&& value) -> T&& { return static_cast(value); } template , Context>::value)> static auto map(T&& value) -> decltype(mapper().map(static_cast(value))) { return mapper().map(static_cast(value)); } }; template using range_formatter_type = formatter>{} .map(std::declval()))>, Char>; template using maybe_const_range = conditional_t::value, const R, R>; // Workaround a bug in MSVC 2015 and earlier. #if !FMT_MSC_VERSION || FMT_MSC_VERSION >= 1910 template struct is_formattable_delayed : is_formattable>, Char> {}; #endif } // namespace detail template struct conjunction : std::true_type {}; template struct conjunction

: P {}; template struct conjunction : conditional_t, P1> {}; template struct range_formatter; template struct range_formatter< T, Char, enable_if_t>, is_formattable>::value>> { private: detail::range_formatter_type underlying_; basic_string_view separator_ = detail::string_literal{}; basic_string_view opening_bracket_ = detail::string_literal{}; basic_string_view closing_bracket_ = detail::string_literal{}; bool is_debug = false; template ::value)> auto write_debug_string(Output& out, It it, Sentinel end) const -> Output { auto buf = basic_memory_buffer(); for (; it != end; ++it) buf.push_back(*it); auto specs = format_specs(); specs.type = presentation_type::debug; return detail::write( out, basic_string_view(buf.data(), buf.size()), specs); } template ::value)> auto write_debug_string(Output& out, It, Sentinel) const -> Output { return out; } public: FMT_CONSTEXPR range_formatter() {} FMT_CONSTEXPR auto underlying() -> detail::range_formatter_type& { return underlying_; } FMT_CONSTEXPR void set_separator(basic_string_view sep) { separator_ = sep; } FMT_CONSTEXPR void set_brackets(basic_string_view open, basic_string_view close) { opening_bracket_ = open; closing_bracket_ = close; } template FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) { auto it = ctx.begin(); auto end = ctx.end(); detail::maybe_set_debug_format(underlying_, true); if (it == end) return underlying_.parse(ctx); switch (detail::to_ascii(*it)) { case 'n': set_brackets({}, {}); ++it; break; case '?': is_debug = true; set_brackets({}, {}); ++it; if (it == end || *it != 's') report_error("invalid format specifier"); FMT_FALLTHROUGH; case 's': if (!std::is_same::value) report_error("invalid format specifier"); if (!is_debug) { set_brackets(detail::string_literal{}, detail::string_literal{}); set_separator({}); detail::maybe_set_debug_format(underlying_, false); } ++it; return it; } if (it != end && *it != '}') { if (*it != ':') report_error("invalid format specifier"); detail::maybe_set_debug_format(underlying_, false); ++it; } ctx.advance_to(it); return underlying_.parse(ctx); } template auto format(R&& range, FormatContext& ctx) const -> decltype(ctx.out()) { auto mapper = detail::range_mapper>(); auto out = ctx.out(); auto it = detail::range_begin(range); auto end = detail::range_end(range); if (is_debug) return write_debug_string(out, std::move(it), end); out = detail::copy(opening_bracket_, out); int i = 0; for (; it != end; ++it) { if (i > 0) out = detail::copy(separator_, out); ctx.advance_to(out); auto&& item = *it; // Need an lvalue out = underlying_.format(mapper.map(item), ctx); ++i; } out = detail::copy(closing_bracket_, out); return out; } }; FMT_EXPORT template struct range_format_kind : conditional_t< is_range::value, detail::range_format_kind_, std::integral_constant> {}; template struct formatter< R, Char, enable_if_t::value != range_format::disabled && range_format_kind::value != range_format::map && range_format_kind::value != range_format::string && range_format_kind::value != range_format::debug_string> // Workaround a bug in MSVC 2015 and earlier. #if !FMT_MSC_VERSION || FMT_MSC_VERSION >= 1910 , detail::is_formattable_delayed #endif >::value>> { private: using range_type = detail::maybe_const_range; range_formatter, Char> range_formatter_; public: using nonlocking = void; FMT_CONSTEXPR formatter() { if (detail::const_check(range_format_kind::value != range_format::set)) return; range_formatter_.set_brackets(detail::string_literal{}, detail::string_literal{}); } template FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) { return range_formatter_.parse(ctx); } template auto format(range_type& range, FormatContext& ctx) const -> decltype(ctx.out()) { return range_formatter_.format(range, ctx); } }; // A map formatter. template struct formatter< R, Char, enable_if_t::value == range_format::map>> { private: using map_type = detail::maybe_const_range; using element_type = detail::uncvref_type; decltype(detail::tuple::get_formatters( detail::tuple_index_sequence())) formatters_; bool no_delimiters_ = false; public: FMT_CONSTEXPR formatter() {} template FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) { auto it = ctx.begin(); auto end = ctx.end(); if (it != end) { if (detail::to_ascii(*it) == 'n') { no_delimiters_ = true; ++it; } if (it != end && *it != '}') { if (*it != ':') report_error("invalid format specifier"); ++it; } ctx.advance_to(it); } detail::for_each(formatters_, detail::parse_empty_specs{ctx}); return it; } template auto format(map_type& map, FormatContext& ctx) const -> decltype(ctx.out()) { auto out = ctx.out(); basic_string_view open = detail::string_literal{}; if (!no_delimiters_) out = detail::copy(open, out); int i = 0; auto mapper = detail::range_mapper>(); basic_string_view sep = detail::string_literal{}; for (auto&& value : map) { if (i > 0) out = detail::copy(sep, out); ctx.advance_to(out); detail::for_each2(formatters_, mapper.map(value), detail::format_tuple_element{ 0, ctx, detail::string_literal{}}); ++i; } basic_string_view close = detail::string_literal{}; if (!no_delimiters_) out = detail::copy(close, out); return out; } }; // A (debug_)string formatter. template struct formatter< R, Char, enable_if_t::value == range_format::string || range_format_kind::value == range_format::debug_string>> { private: using range_type = detail::maybe_const_range; using string_type = conditional_t, decltype(detail::range_begin(std::declval())), decltype(detail::range_end(std::declval()))>::value, detail::std_string_view, std::basic_string>; formatter underlying_; public: template FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) { return underlying_.parse(ctx); } template auto format(range_type& range, FormatContext& ctx) const -> decltype(ctx.out()) { auto out = ctx.out(); if (detail::const_check(range_format_kind::value == range_format::debug_string)) *out++ = '"'; out = underlying_.format( string_type{detail::range_begin(range), detail::range_end(range)}, ctx); if (detail::const_check(range_format_kind::value == range_format::debug_string)) *out++ = '"'; return out; } }; template struct join_view : detail::view { It begin; Sentinel end; basic_string_view sep; join_view(It b, Sentinel e, basic_string_view s) : begin(std::move(b)), end(e), sep(s) {} }; template struct formatter, Char> { private: using value_type = #ifdef __cpp_lib_ranges std::iter_value_t; #else typename std::iterator_traits::value_type; #endif formatter, Char> value_formatter_; using view_ref = conditional_t::value, const join_view&, join_view&&>; public: using nonlocking = void; template FMT_CONSTEXPR auto parse(ParseContext& ctx) -> const Char* { return value_formatter_.parse(ctx); } template auto format(view_ref& value, FormatContext& ctx) const -> decltype(ctx.out()) { auto it = std::forward(value).begin; auto out = ctx.out(); if (it == value.end) return out; out = value_formatter_.format(*it, ctx); ++it; while (it != value.end) { out = detail::copy(value.sep.begin(), value.sep.end(), out); ctx.advance_to(out); out = value_formatter_.format(*it, ctx); ++it; } return out; } }; /// Returns a view that formats the iterator range `[begin, end)` with elements /// separated by `sep`. template auto join(It begin, Sentinel end, string_view sep) -> join_view { return {std::move(begin), end, sep}; } /** * Returns a view that formats `range` with elements separated by `sep`. * * **Example**: * * auto v = std::vector{1, 2, 3}; * fmt::print("{}", fmt::join(v, ", ")); * // Output: 1, 2, 3 * * `fmt::join` applies passed format specifiers to the range elements: * * fmt::print("{:02}", fmt::join(v, ", ")); * // Output: 01, 02, 03 */ template auto join(Range&& r, string_view sep) -> join_view { return {detail::range_begin(r), detail::range_end(r), sep}; } template struct tuple_join_view : detail::view { const std::tuple& tuple; basic_string_view sep; tuple_join_view(const std::tuple& t, basic_string_view s) : tuple(t), sep{s} {} }; // Define FMT_TUPLE_JOIN_SPECIFIERS to enable experimental format specifiers // support in tuple_join. It is disabled by default because of issues with // the dynamic width and precision. #ifndef FMT_TUPLE_JOIN_SPECIFIERS # define FMT_TUPLE_JOIN_SPECIFIERS 0 #endif template struct formatter, Char> { template FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) { return do_parse(ctx, std::integral_constant()); } template auto format(const tuple_join_view& value, FormatContext& ctx) const -> typename FormatContext::iterator { return do_format(value, ctx, std::integral_constant()); } private: std::tuple::type, Char>...> formatters_; template FMT_CONSTEXPR auto do_parse(ParseContext& ctx, std::integral_constant) -> decltype(ctx.begin()) { return ctx.begin(); } template FMT_CONSTEXPR auto do_parse(ParseContext& ctx, std::integral_constant) -> decltype(ctx.begin()) { auto end = ctx.begin(); #if FMT_TUPLE_JOIN_SPECIFIERS end = std::get(formatters_).parse(ctx); if (N > 1) { auto end1 = do_parse(ctx, std::integral_constant()); if (end != end1) report_error("incompatible format specs for tuple elements"); } #endif return end; } template auto do_format(const tuple_join_view&, FormatContext& ctx, std::integral_constant) const -> typename FormatContext::iterator { return ctx.out(); } template auto do_format(const tuple_join_view& value, FormatContext& ctx, std::integral_constant) const -> typename FormatContext::iterator { auto out = std::get(formatters_) .format(std::get(value.tuple), ctx); if (N <= 1) return out; out = detail::copy(value.sep, out); ctx.advance_to(out); return do_format(value, ctx, std::integral_constant()); } }; namespace detail { // Check if T has an interface like a container adaptor (e.g. std::stack, // std::queue, std::priority_queue). template class is_container_adaptor_like { template static auto check(U* p) -> typename U::container_type; template static void check(...); public: static constexpr const bool value = !std::is_void(nullptr))>::value; }; template struct all { const Container& c; auto begin() const -> typename Container::const_iterator { return c.begin(); } auto end() const -> typename Container::const_iterator { return c.end(); } }; } // namespace detail template struct formatter< T, Char, enable_if_t, bool_constant::value == range_format::disabled>>::value>> : formatter, Char> { using all = detail::all; template auto format(const T& t, FormatContext& ctx) const -> decltype(ctx.out()) { struct getter : T { static auto get(const T& t) -> all { return {t.*(&getter::c)}; // Access c through the derived class. } }; return formatter::format(getter::get(t), ctx); } }; FMT_BEGIN_EXPORT /** * Returns an object that formats `std::tuple` with elements separated by `sep`. * * **Example**: * * auto t = std::tuple{1, 'a'}; * fmt::print("{}", fmt::join(t, ", ")); * // Output: 1, a */ template FMT_CONSTEXPR auto join(const std::tuple& tuple, string_view sep) -> tuple_join_view { return {tuple, sep}; } /** * Returns an object that formats `std::initializer_list` with elements * separated by `sep`. * * **Example**: * * fmt::print("{}", fmt::join({1, 2, 3}, ", ")); * // Output: "1, 2, 3" */ template auto join(std::initializer_list list, string_view sep) -> join_view { return join(std::begin(list), std::end(list), sep); } FMT_END_EXPORT FMT_END_NAMESPACE #endif // FMT_RANGES_H_