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>
37#include <lagrange/utils/build.h>
43#if defined(SPDLOG_USE_STD_FORMAT) || (defined(__has_include) && __has_include(<format>) && \
44 LAGRANGE_CPLUSPLUS >= 202002L)
45 #define LAGRANGE_JOIN_HAS_STD_FORMAT 1
63 std::remove_cv_t<std::remove_reference_t<decltype(*std::begin(std::declval<const R&>()))>>;
69template <
typename Range>
77template <
typename Tuple>
86template <
typename Range,
typename FormatContext,
typename Func>
87auto format_range(
const range_join_view<Range>& jv, FormatContext& ctx, Func&& format_elem)
91 for (
const auto& elem : *jv.range) {
93 for (
auto c : jv.sep) *out++ = c;
96 out = format_elem(elem, ctx);
102template <
typename Tuple,
typename FormatContext,
typename Func>
103auto format_tuple(
const tuple_join_view<Tuple>& jv, FormatContext& ctx, Func&& format_elem)
105 auto out = ctx.out();
108 [&](
const auto&... elems) {
110 [&](
const auto& elem) {
112 for (
auto c : jv.sep) *out++ = c;
115 out = format_elem(elem, ctx);
132template <
typename Range>
133auto join(
const Range& r, std::string_view sep)
135 return fmt_detail::range_join_view<Range>{&r, sep};
139template <
typename... Ts>
140auto join(
const std::tuple<Ts...>& t, std::string_view sep)
142 return fmt_detail::tuple_join_view<std::tuple<Ts...>>{&t, sep};
146template <
typename T,
typename U>
147auto join(
const std::pair<T, U>& p, std::string_view sep)
149 return fmt_detail::tuple_join_view<std::pair<T, U>>{&p, sep};
159#if defined(LAGRANGE_JOIN_HAS_STD_FORMAT)
165template <
typename Range>
166struct std::formatter<
lagrange::fmt_detail::range_join_view<Range>, char>
168 using value_type = lagrange::fmt_detail::range_value_t<Range>;
169 std::formatter<value_type, char> m_elem;
171 template <
typename ParseContext>
172 constexpr auto parse(ParseContext& ctx)
174 return m_elem.parse(ctx);
177 template <
typename FormatContext>
178 auto format(
const lagrange::fmt_detail::range_join_view<Range>& jv, FormatContext& ctx)
const
180 return lagrange::fmt_detail::format_range(
183 [
this](
const auto& elem, FormatContext& c) {
return m_elem.format(elem, c); });
188template <
typename Tuple>
189struct std::formatter<
lagrange::fmt_detail::tuple_join_view<Tuple>, char>
191 template <
typename ParseContext>
192 constexpr auto parse(ParseContext& ctx)
194 if (ctx.begin() != ctx.end() && *ctx.begin() !=
'}') {
195 throw std::format_error(
"format spec not supported for tuple/pair join");
205 template <
typename FormatContext>
206 auto format(
const lagrange::fmt_detail::tuple_join_view<Tuple>& jv, FormatContext& ctx)
const
208 return lagrange::fmt_detail::format_tuple(jv, ctx, [](
const auto& elem,
auto& c) {
209 using ElemType = std::remove_cv_t<std::remove_reference_t<
decltype(elem)>>;
210 return std::formatter<ElemType, char>{}.format(elem, c);
220#if !defined(SPDLOG_USE_STD_FORMAT)
224template <
typename Range>
225struct fmt::formatter<
lagrange::fmt_detail::range_join_view<Range>, char>
227 using value_type = lagrange::fmt_detail::range_value_t<Range>;
228 fmt::formatter<value_type, char> m_elem;
230 template <
typename ParseContext>
231 constexpr auto parse(ParseContext& ctx)
233 return m_elem.parse(ctx);
236 template <
typename FormatContext>
237 auto format(
const lagrange::fmt_detail::range_join_view<Range>& jv, FormatContext& ctx)
const
239 return lagrange::fmt_detail::format_range(
242 [
this](
const auto& elem, FormatContext& c) {
return m_elem.format(elem, c); });
246template <
typename Tuple>
247struct fmt::formatter<
lagrange::fmt_detail::tuple_join_view<Tuple>, char>
249 template <
typename ParseContext>
250 constexpr auto parse(ParseContext& ctx)
252 if (ctx.begin() != ctx.end() && *ctx.begin() !=
'}') {
253 throw fmt::format_error(
"format spec not supported for tuple/pair join");
258 template <
typename FormatContext>
259 auto format(
const lagrange::fmt_detail::tuple_join_view<Tuple>& jv, FormatContext& ctx)
const
261 return lagrange::fmt_detail::format_tuple(jv, ctx, [](
const auto& elem,
auto& c) {
262 return fmt::format_to(c.out(),
"{}", elem);
Main namespace for Lagrange.
internal::Range< Index > range(Index end)
Returns an iterable object representing the range [0, end).
Definition range.h:176
auto join(const Range &r, std::string_view sep)
Join all elements of r into a formattable view separated by sep.
Definition join.h:133