// Formatting library for C++ - the core API // // 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 "format.h" #include // output only up to N items from the range. #ifndef FMT_RANGE_OUTPUT_LENGTH_LIMIT # define FMT_RANGE_OUTPUT_LENGTH_LIMIT 256 #endif 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 { static FMT_CONSTEXPR_DECL const std::size_t range_length_limit = FMT_RANGE_OUTPUT_LENGTH_LIMIT; // output only up to N items from the range. Char prefix; Char delimiter; Char postfix; formatting_range() : prefix('{'), delimiter(','), postfix('}') {} static FMT_CONSTEXPR_DECL const bool add_delimiter_spaces = true; static FMT_CONSTEXPR_DECL const bool add_prepostfix_space = false; }; template struct formatting_tuple : formatting_base { Char prefix; Char delimiter; Char postfix; formatting_tuple() : prefix('('), delimiter(','), postfix(')') {} static FMT_CONSTEXPR_DECL const bool add_delimiter_spaces = true; static FMT_CONSTEXPR_DECL const bool add_prepostfix_space = false; }; namespace internal { template void copy(const RangeT &range, OutputIterator out) { for (auto it = range.begin(), end = range.end(); it != end; ++it) *out++ = *it; } template void copy(const char *str, OutputIterator out) { const char *p_curr = str; while (*p_curr) { *out++ = *p_curr++; } } template void copy(char ch, OutputIterator out) { *out++ = ch; } /// Return true value if T has std::string interface, like std::string_view. template class is_like_std_string { template static auto check(U *p) -> decltype(p->find('a'), p->length(), p->data(), int()); template static void check(...); public: static FMT_CONSTEXPR_DECL const bool value = !std::is_void(FMT_NULL))>::value; }; template struct conditional_helper {}; template struct is_range_ : std::false_type {}; #if !FMT_MSC_VER || FMT_MSC_VER > 1800 template struct is_range_().begin()), decltype(internal::declval().end())>, void>::type> : std::true_type {}; #endif /// tuple_size and tuple_element check. template class is_tuple_like_ { template static auto check(U *p) -> decltype(std::tuple_size::value, internal::declval::type>(), int()); template static void check(...); public: static FMT_CONSTEXPR_DECL const bool value = !std::is_void(FMT_NULL))>::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 { typedef T value_type; static FMT_CONSTEXPR std::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 FMT_CONSTEXPR const char* format_str_quoted(bool add_space, const Arg&, typename std::enable_if< !is_like_std_string::type>::value>::type* = nullptr) { return add_space ? " {}" : "{}"; } template FMT_CONSTEXPR const char* format_str_quoted(bool add_space, const Arg&, typename std::enable_if< is_like_std_string::type>::value>::type* = nullptr) { return add_space ? " \"{}\"" : "\"{}\""; } FMT_CONSTEXPR const char* format_str_quoted(bool add_space, const char*) { return add_space ? " \"{}\"" : "\"{}\""; } FMT_CONSTEXPR const wchar_t* format_str_quoted(bool add_space, const wchar_t*) { return add_space ? L" \"{}\"" : L"\"{}\""; } FMT_CONSTEXPR const char* format_str_quoted(bool add_space, const char) { return add_space ? " '{}'" : "'{}'"; } FMT_CONSTEXPR const wchar_t* format_str_quoted(bool add_space, const wchar_t) { return add_space ? L" '{}'" : L"'{}'"; } } // namespace internal template struct is_tuple_like { static FMT_CONSTEXPR_DECL const bool value = internal::is_tuple_like_::value && !internal::is_range_::value; }; template struct formatter::value>::type> { private: // C++11 generic lambda for format() template struct format_each { template void operator()(const T& v) { if (i > 0) { if (formatting.add_prepostfix_space) { *out++ = ' '; } internal::copy(formatting.delimiter, out); } format_to(out, internal::format_str_quoted( (formatting.add_delimiter_spaces && i > 0), v), v); ++i; } formatting_tuple& formatting; std::size_t& i; typename std::add_lvalue_reference().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(); std::size_t i = 0; internal::copy(formatting.prefix, out); internal::for_each(values, format_each{formatting, i, out}); if (formatting.add_prepostfix_space) { *out++ = ' '; } internal::copy(formatting.postfix, out); return ctx.out(); } }; template struct is_range { static FMT_CONSTEXPR_DECL const bool value = internal::is_range_::value && !internal::is_like_std_string::value; }; template struct formatter::value>::type> { formatting_range formatting; template FMT_CONSTEXPR auto parse(ParseContext &ctx) -> decltype(ctx.begin()) { return formatting.parse(ctx); } template typename FormatContext::iterator format( const RangeT &values, FormatContext &ctx) { auto out = ctx.out(); internal::copy(formatting.prefix, out); std::size_t i = 0; for (auto it = values.begin(), end = values.end(); it != end; ++it) { if (i > 0) { if (formatting.add_prepostfix_space) { *out++ = ' '; } internal::copy(formatting.delimiter, out); } format_to(out, internal::format_str_quoted( (formatting.add_delimiter_spaces && i > 0), *it), *it); if (++i > formatting.range_length_limit) { format_to(out, " ... "); break; } } if (formatting.add_prepostfix_space) { *out++ = ' '; } internal::copy(formatting.postfix, out); return ctx.out(); } }; FMT_END_NAMESPACE #endif // FMT_RANGES_H_