29#if !defined(SPDLOG_USE_STD_FORMAT)
31 #include <lagrange/utils/warnoff.h>
32 #include <spdlog/fmt/fmt.h>
33 #include <lagrange/utils/warnon.h>
39#if defined(SPDLOG_USE_STD_FORMAT) || defined(__cpp_lib_format)
57 std::remove_cv_t<std::remove_reference_t<decltype(*std::begin(std::declval<const R&>()))>>;
63template <
typename Range>
71template <
typename Tuple>
80template <
typename Range,
typename FormatContext,
typename Func>
81auto format_range(
const range_join_view<Range>& jv, FormatContext& ctx, Func&& format_elem)
85 for (
const auto& elem : *jv.range) {
87 for (
auto c : jv.sep) *out++ = c;
90 out = format_elem(elem, ctx);
96template <
typename Tuple,
typename FormatContext,
typename Func>
97auto format_tuple(
const tuple_join_view<Tuple>& jv, FormatContext& ctx, Func&& format_elem)
102 [&](
const auto&... elems) {
104 [&](
const auto& elem) {
106 for (
auto c : jv.sep) *out++ = c;
109 out = format_elem(elem, ctx);
126template <
typename Range>
127auto join(
const Range& r, std::string_view sep)
129 return fmt_detail::range_join_view<Range>{&r, sep};
133template <
typename... Ts>
134auto join(
const std::tuple<Ts...>& t, std::string_view sep)
136 return fmt_detail::tuple_join_view<std::tuple<Ts...>>{&t, sep};
140template <
typename T,
typename U>
141auto join(
const std::pair<T, U>& p, std::string_view sep)
143 return fmt_detail::tuple_join_view<std::pair<T, U>>{&p, sep};
153#if defined(SPDLOG_USE_STD_FORMAT) || defined(__cpp_lib_format)
155template <
typename Range>
156struct std::formatter<
lagrange::fmt_detail::range_join_view<Range>, char>
158 using value_type = lagrange::fmt_detail::range_value_t<Range>;
159 std::formatter<value_type, char> m_elem;
161 constexpr auto parse(std::format_parse_context& ctx) {
return m_elem.parse(ctx); }
163 auto format(
const lagrange::fmt_detail::range_join_view<Range>& jv, std::format_context& ctx)
166 return lagrange::fmt_detail::format_range(
169 [
this](
const auto& elem, std::format_context& c) {
return m_elem.format(elem, c); });
174template <
typename Tuple>
175struct std::formatter<
lagrange::fmt_detail::tuple_join_view<Tuple>, char>
177 constexpr auto parse(std::format_parse_context& ctx)
179 if (ctx.begin() != ctx.end() && *ctx.begin() !=
'}') {
180 throw std::format_error(
"format spec not supported for tuple/pair join");
185 auto format(
const lagrange::fmt_detail::tuple_join_view<Tuple>& jv, std::format_context& ctx)
188 return lagrange::fmt_detail::format_tuple(jv, ctx, [](
const auto& elem,
auto& c) {
189 return std::format_to(c.out(),
"{}", elem);
199#if !defined(SPDLOG_USE_STD_FORMAT)
201template <
typename Range>
202struct fmt::formatter<
lagrange::fmt_detail::range_join_view<Range>, char>
204 using value_type = lagrange::fmt_detail::range_value_t<Range>;
205 fmt::formatter<value_type, char> m_elem;
207 template <
typename ParseContext>
208 constexpr auto parse(ParseContext& ctx)
210 return m_elem.parse(ctx);
213 template <
typename FormatContext>
214 auto format(
const lagrange::fmt_detail::range_join_view<Range>& jv, FormatContext& ctx)
const
216 return lagrange::fmt_detail::format_range(
219 [
this](
const auto& elem, FormatContext& c) {
return m_elem.format(elem, c); });
223template <
typename Tuple>
224struct fmt::formatter<
lagrange::fmt_detail::tuple_join_view<Tuple>, char>
226 template <
typename ParseContext>
227 constexpr auto parse(ParseContext& ctx)
229 if (ctx.begin() != ctx.end() && *ctx.begin() !=
'}') {
230 throw fmt::format_error(
"format spec not supported for tuple/pair join");
235 template <
typename FormatContext>
236 auto format(
const lagrange::fmt_detail::tuple_join_view<Tuple>& jv, FormatContext& ctx)
const
238 return lagrange::fmt_detail::format_tuple(jv, ctx, [](
const auto& elem,
auto& c) {
239 return fmt::format_to(c.out(),
"{}", elem);
internal::Range< Index > range(Index end)
Returns an iterable object representing the range [0, end).
Definition range.h:176
Main namespace for Lagrange.
auto join(const Range &r, std::string_view sep)
Join all elements of r into a formattable view separated by sep.
Definition join.h:127