// 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 <initializer_list> # include <iterator> # include <string> # include <tuple> # include <type_traits> # include <utility> #endif
template <typename T> struct has_mutable_begin_end<
T, void_t<decltype(*detail::range_begin(std::declval<T&>())),
decltype(detail::range_end(std::declval<T&>())), // the extra int here is because older versions of MSVC don't // SFINAE properly unless there are distinct types int>> : std::true_type {};
namespace tuple { // Workaround a bug in MSVC 2019 (v140). template <typenameChar, typename... T> using result_t = std::tuple<formatter<remove_cvref_t<T>, Char>...>;
using std::get; template <typename Tuple, typenameChar, std::size_t... Is> auto get_formatters(index_sequence<Is...>)
-> result_t<Char, decltype(get<Is>(std::declval<Tuple>()))...>;
} // namespace tuple
#if FMT_MSC_VERSION && FMT_MSC_VERSION < 1920 // Older MSVC doesn't get the reference type correctly for arrays. template <typename R> struct range_reference_type_impl { using type = decltype(*detail::range_begin(std::declval<R&>()));
};
template <typename T, std::size_t N> struct range_reference_type_impl<T[N]> { using type = T&;
};
template <typename T> using range_reference_type = typename range_reference_type_impl<T>::type; #else template <typename Range> using range_reference_type =
decltype(*detail::range_begin(std::declval<Range&>())); #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 <typename Range> using uncvref_type = remove_cvref_t<range_reference_type<Range>>;
FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) -> constChar* { auto it = ctx.begin(); auto end = ctx.end(); if (it != end && detail::to_ascii(*it) == 'n') {
++it;
set_brackets({}, {});
set_separator({});
} if (it != end && *it != '}') report_error("invalid format specifier");
ctx.advance_to(it);
detail::for_each(formatters_, detail::parse_empty_specs<Char>{ctx}); return it;
}
FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) -> constChar* { 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<T, Char>::value)
report_error("invalid format specifier"); if (!is_debug) {
set_brackets(detail::string_literal<Char, '"'>{},
detail::string_literal<Char, '"'>{});
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;
}
template <typename R, typename FormatContext> auto format(R&& range, FormatContext& ctx) const -> decltype(ctx.out()) { 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<Char>(opening_bracket_, out); int i = 0; for (; it != end; ++it) { if (i > 0) out = detail::copy<Char>(separator_, out);
ctx.advance_to(out); auto&& item = *it; // Need an lvalue
out = underlying_.format(item, ctx);
++i;
}
out = detail::copy<Char>(closing_bracket_, out); return out;
}
};
FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) -> constChar* { 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<Char>{ctx}); return it;
}
template <typename FormatContext> auto format(map_type& map, FormatContext& ctx) const -> decltype(ctx.out()) { auto out = ctx.out();
basic_string_view<Char> open = detail::string_literal<Char, '{'>{}; if (!no_delimiters_) out = detail::copy<Char>(open, out); int i = 0;
basic_string_view<Char> sep = detail::string_literal<Char, ',', ' '>{}; for (auto&& value : map) { if (i > 0) out = detail::copy<Char>(sep, out);
ctx.advance_to(out);
detail::for_each2(formatters_, value,
detail::format_tuple_element<FormatContext>{
0, ctx, detail::string_literal<Char, ':', ' '>{}});
++i;
}
basic_string_view<Char> close = detail::string_literal<Char, '}'>{}; if (!no_delimiters_) out = detail::copy<Char>(close, out); return out;
}
};
FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) -> constChar* { return value_formatter_.parse(ctx);
}
template <typename FormatContext> auto format(view_ref& value, FormatContext& ctx) const
-> decltype(ctx.out()) { auto it = std::forward<view_ref>(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<Char>(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 <typename It, typename Sentinel> auto join(It begin, Sentinel end, string_view sep) -> join_view<It, Sentinel> { return {std::move(begin), end, sep};
}
/** * Returns a view that formats `range` with elements separated by `sep`. * * **Example**: * * auto v = std::vector<int>{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 <typename Range> auto join(Range&& r, string_view sep)
-> join_view<decltype(detail::range_begin(r)),
decltype(detail::range_end(r))> { return {detail::range_begin(r), detail::range_end(r), sep};
}
// 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
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.