Lagrange
Loading...
Searching...
No Matches
join.h
1/*
2 * Copyright 2026 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12#pragma once
13
27
28// fmt is reachable whenever spdlog isn't running in pure-std::format mode.
29#if !defined(SPDLOG_USE_STD_FORMAT)
30// clang-format off
31 #include <lagrange/utils/warnoff.h>
32 #include <spdlog/fmt/fmt.h>
33 #include <lagrange/utils/warnon.h>
34// clang-format on
35#endif
36
37#include <lagrange/utils/build.h>
38
39// In C++20, <chrono> will forward-declare std::formatter and related types, so we need to include
40// <format> and define `std::formatter` to avoid compilation errors. Note that older versions of
41// Xcode (e.g. 15.3) ship with a <format> header, but do not define `__cpp_lib_format`, so we cannot
42// rely on that macro to detect availability.
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
46 #include <format>
47#endif
48
49#include <iterator>
50#include <string_view>
51#include <tuple>
52#include <type_traits>
53#include <utility>
54
55namespace lagrange {
56
58namespace fmt_detail {
59
61template <typename R>
62using range_value_t =
63 std::remove_cv_t<std::remove_reference_t<decltype(*std::begin(std::declval<const R&>()))>>;
64
69template <typename Range>
70struct range_join_view
71{
72 const Range* range;
73 std::string_view sep;
74};
75
77template <typename Tuple>
78struct tuple_join_view
79{
80 const Tuple* tuple;
81 std::string_view sep;
82};
83
86template <typename Range, typename FormatContext, typename Func>
87auto format_range(const range_join_view<Range>& jv, FormatContext& ctx, Func&& format_elem)
88{
89 auto out = ctx.out();
90 bool first = true;
91 for (const auto& elem : *jv.range) {
92 if (!first) {
93 for (auto c : jv.sep) *out++ = c;
94 ctx.advance_to(out);
95 }
96 out = format_elem(elem, ctx);
97 first = false;
98 }
99 return out;
100}
101
102template <typename Tuple, typename FormatContext, typename Func>
103auto format_tuple(const tuple_join_view<Tuple>& jv, FormatContext& ctx, Func&& format_elem)
104{
105 auto out = ctx.out();
106 bool first = true;
107 std::apply(
108 [&](const auto&... elems) {
109 (
110 [&](const auto& elem) {
111 if (!first) {
112 for (auto c : jv.sep) *out++ = c;
113 ctx.advance_to(out);
114 }
115 out = format_elem(elem, ctx);
116 first = false;
117 }(elems),
118 ...);
119 },
120 *jv.tuple);
121 return out;
122}
123
124} // namespace fmt_detail
126
132template <typename Range>
133auto join(const Range& r, std::string_view sep)
134{
135 return fmt_detail::range_join_view<Range>{&r, sep};
136}
137
139template <typename... Ts>
140auto join(const std::tuple<Ts...>& t, std::string_view sep)
141{
142 return fmt_detail::tuple_join_view<std::tuple<Ts...>>{&t, sep};
143}
144
146template <typename T, typename U>
147auto join(const std::pair<T, U>& p, std::string_view sep)
148{
149 return fmt_detail::tuple_join_view<std::pair<T, U>>{&p, sep};
150}
151
152} // namespace lagrange
153
159#if defined(LAGRANGE_JOIN_HAS_STD_FORMAT)
160
165template <typename Range>
166struct std::formatter<lagrange::fmt_detail::range_join_view<Range>, char>
167{
168 using value_type = lagrange::fmt_detail::range_value_t<Range>;
169 std::formatter<value_type, char> m_elem;
170
171 template <typename ParseContext>
172 constexpr auto parse(ParseContext& ctx)
173 {
174 return m_elem.parse(ctx);
175 }
176
177 template <typename FormatContext>
178 auto format(const lagrange::fmt_detail::range_join_view<Range>& jv, FormatContext& ctx) const
179 {
180 return lagrange::fmt_detail::format_range(
181 jv,
182 ctx,
183 [this](const auto& elem, FormatContext& c) { return m_elem.format(elem, c); });
184 }
185};
186
188template <typename Tuple>
189struct std::formatter<lagrange::fmt_detail::tuple_join_view<Tuple>, char>
190{
191 template <typename ParseContext>
192 constexpr auto parse(ParseContext& ctx)
193 {
194 if (ctx.begin() != ctx.end() && *ctx.begin() != '}') {
195 throw std::format_error("format spec not supported for tuple/pair join");
196 }
197 return ctx.begin();
198 }
199
205 template <typename FormatContext>
206 auto format(const lagrange::fmt_detail::tuple_join_view<Tuple>& jv, FormatContext& ctx) const
207 {
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);
211 });
212 }
213};
214
215#endif // defined(LAGRANGE_JOIN_HAS_STD_FORMAT)
216
220#if !defined(SPDLOG_USE_STD_FORMAT)
221
223
224template <typename Range>
225struct fmt::formatter<lagrange::fmt_detail::range_join_view<Range>, char>
226{
227 using value_type = lagrange::fmt_detail::range_value_t<Range>;
228 fmt::formatter<value_type, char> m_elem;
229
230 template <typename ParseContext>
231 constexpr auto parse(ParseContext& ctx)
232 {
233 return m_elem.parse(ctx);
234 }
235
236 template <typename FormatContext>
237 auto format(const lagrange::fmt_detail::range_join_view<Range>& jv, FormatContext& ctx) const
238 {
239 return lagrange::fmt_detail::format_range(
240 jv,
241 ctx,
242 [this](const auto& elem, FormatContext& c) { return m_elem.format(elem, c); });
243 }
244};
245
246template <typename Tuple>
247struct fmt::formatter<lagrange::fmt_detail::tuple_join_view<Tuple>, char>
248{
249 template <typename ParseContext>
250 constexpr auto parse(ParseContext& ctx)
251 {
252 if (ctx.begin() != ctx.end() && *ctx.begin() != '}') {
253 throw fmt::format_error("format spec not supported for tuple/pair join");
254 }
255 return ctx.begin();
256 }
257
258 template <typename FormatContext>
259 auto format(const lagrange::fmt_detail::tuple_join_view<Tuple>& jv, FormatContext& ctx) const
260 {
261 return lagrange::fmt_detail::format_tuple(jv, ctx, [](const auto& elem, auto& c) {
262 return fmt::format_to(c.out(), "{}", elem);
263 });
264 }
265};
266
268
269#endif // !defined(SPDLOG_USE_STD_FORMAT)
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