8 #ifndef FMTQUILL_RANGES_H_ 9 #define FMTQUILL_RANGES_H_ 11 #ifndef FMTQUILL_MODULE 12 # include <initializer_list> 16 # include <type_traits> 22 FMTQUILL_BEGIN_NAMESPACE
25 enum class range_format { disabled, map,
set, sequence, string, debug_string };
30 template <
typename U>
static auto check(U*) ->
typename U::mapped_type;
31 template <
typename>
static void check(...);
34 static constexpr
const bool value =
35 !std::is_void<decltype(check<T>(
nullptr))>
::value;
39 template <
typename U>
static auto check(U*) ->
typename U::key_type;
40 template <
typename>
static void check(...);
43 static constexpr
const bool value =
48 template <
typename T, std::
size_t N>
49 auto range_begin(
const T (&arr)[N]) ->
const T* {
52 template <
typename T, std::
size_t N>
53 auto range_end(
const T (&arr)[N]) ->
const T* {
57 template <
typename T,
typename Enable =
void>
62 decltype(std::declval<T>().end())>>
67 auto range_begin(T&& rng) -> decltype(static_cast<T&&>(rng).begin()) {
68 return static_cast<T&&
>(rng).begin();
71 auto range_end(T&& rng) -> decltype(static_cast<T&&>(rng).end()) {
72 return static_cast<T&&
>(rng).end();
78 auto range_begin(T&& rng)
79 -> enable_if_t<!has_member_fn_begin_end_t<T&&>::value,
80 decltype(begin(static_cast<T&&>(rng)))> {
81 return begin(static_cast<T&&>(rng));
84 auto range_end(T&& rng) -> enable_if_t<!has_member_fn_begin_end_t<T&&>::value,
85 decltype(end(static_cast<T&&>(rng)))> {
86 return end(static_cast<T&&>(rng));
89 template <
typename T,
typename Enable =
void>
91 template <
typename T,
typename Enable =
void>
96 T, void_t<decltype(*detail::range_begin(
97 std::declval<
const remove_cvref_t<T>&>())),
98 decltype(detail::range_end(
99 std::declval<
const remove_cvref_t<T>&>()))>>
102 template <
typename T>
104 T, void_t<decltype(*detail::range_begin(std::declval<T&>())),
105 decltype(detail::range_end(std::declval<T&>())),
108 int>> : std::true_type {};
110 template <
typename T,
typename _ =
void>
struct is_range_ : std::false_type {};
111 template <
typename T>
113 : std::integral_constant<bool, (has_const_begin_end<T>::value ||
114 has_mutable_begin_end<T>::value)> {};
118 template <typename U, typename V = typename std::remove_cv<U>::type>
119 static auto check(U* p) -> decltype(std::tuple_size<V>::value, 0);
120 template <
typename>
static void check(...);
123 static constexpr
const bool value =
124 !std::is_void<decltype(check<T>(
nullptr))>
::value;
128 #if defined(__cpp_lib_integer_sequence) || FMTQUILL_MSC_VERSION >= 1900 129 template <
typename T, T... N>
131 template <
size_t... N>
using index_sequence = std::index_sequence<N...>;
134 template <
typename T, T... N>
struct integer_sequence {
135 using value_type = T;
137 static FMTQUILL_CONSTEXPR
auto size() ->
size_t {
return sizeof...(N); }
140 template <
size_t... N>
using index_sequence = integer_sequence<size_t, N...>;
142 template <
typename T,
size_t N, T... Ns>
144 template <
typename T, T... Ns>
151 template <
typename T>
154 template <typename T, typename C, bool = is_tuple_like_<T>::value>
157 static constexpr
const bool value =
false;
160 template <
size_t... Is>
162 integer_sequence<
bool, (Is >= 0)...>) -> std::true_type;
163 static auto all_true(...) -> std::false_type;
165 template <
size_t... Is>
168 integer_sequence<bool,
169 (is_formattable<typename std::tuple_element<Is, T>::type,
173 static constexpr
const bool value =
177 template <
typename Tuple,
typename F,
size_t... Is>
181 const int unused[] = {0, ((void)f(get<Is>(t)), 0)...};
182 ignore_unused(unused);
185 template <
typename Tuple,
typename F>
186 FMTQUILL_CONSTEXPR
void for_each(Tuple&& t, F&& f) {
188 std::forward<Tuple>(t), std::forward<F>(f));
191 template <
typename Tuple1,
typename Tuple2,
typename F,
size_t... Is>
194 const int unused[] = {0, ((void)f(get<Is>(t1), get<Is>(t2)), 0)...};
195 ignore_unused(unused);
198 template <
typename Tuple1,
typename Tuple2,
typename F>
199 void for_each2(Tuple1&& t1, Tuple2&& t2, F&& f) {
201 std::forward<Tuple1>(t1), std::forward<Tuple2>(t2),
207 template <
typename Char,
typename... T>
211 template <
typename Tuple,
typename Char, std::size_t... Is>
213 -> result_t<Char, decltype(get<Is>(std::declval<Tuple>()))...>;
216 #if FMTQUILL_MSC_VERSION && FMTQUILL_MSC_VERSION < 1920 218 template <
typename R>
struct range_reference_type_impl {
219 using type = decltype(*detail::range_begin(std::declval<R&>()));
222 template <
typename T, std::
size_t N>
struct range_reference_type_impl<T[N]> {
226 template <
typename T>
227 using range_reference_type =
typename range_reference_type_impl<T>::type;
229 template <
typename Range>
230 using range_reference_type =
231 decltype(*detail::range_begin(std::declval<Range&>()));
236 template <
typename Range>
237 using uncvref_type = remove_cvref_t<range_reference_type<Range>>;
239 template <
typename Formatter>
240 FMTQUILL_CONSTEXPR
auto maybe_set_debug_format(Formatter& f,
bool set)
241 -> decltype(f.set_debug_format(
set)) {
242 f.set_debug_format(
set);
244 template <
typename Formatter>
245 FMTQUILL_CONSTEXPR
void maybe_set_debug_format(Formatter&, ...) {}
247 template <
typename T>
249 : std::integral_constant<range_format,
250 std::is_same<uncvref_type<T>, T>::value
251 ? range_format::disabled
252 : is_map<T>::value ? range_format::map
253 : is_set<T>::value ? range_format::set
254 : range_format::sequence> {};
256 template <range_format K>
257 using range_format_constant = std::integral_constant<range_format, K>;
261 template <
typename Formatter> FMTQUILL_CONSTEXPR
void operator()(Formatter& f) {
263 detail::maybe_set_debug_format(f,
true);
268 using char_type =
typename FormatContext::char_type;
270 template <
typename T>
272 if (i > 0) ctx.advance_to(detail::copy<char_type>(separator, ctx.out()));
273 ctx.advance_to(f.format(v, ctx));
285 static constexpr
const bool value =
290 static constexpr
const bool value =
294 template <
typename Tuple,
typename Char>
297 fmtquill::is_tuple_formattable<Tuple, Char>::value>> {
299 decltype(detail::tuple::get_formatters<Tuple, Char>(
317 opening_bracket_ = open;
318 closing_bracket_ = close;
322 auto it = ctx.
begin();
323 auto end = ctx.
end();
324 if (it != end && detail::to_ascii(*it) ==
'n') {
326 set_brackets({}, {});
329 if (it != end && *it !=
'}') report_error(
"invalid format specifier");
335 template <
typename FormatContext>
336 auto format(
const Tuple& value, FormatContext& ctx)
const 337 -> decltype(ctx.out()) {
338 ctx.advance_to(detail::copy<Char>(opening_bracket_, ctx.out()));
342 return detail::copy<Char>(closing_bracket_, ctx.out());
346 template <
typename T,
typename Char>
struct is_range {
347 static constexpr
const bool value =
353 template <
typename Char,
typename Element>
356 template <
typename R>
357 using maybe_const_range =
358 conditional_t<has_const_begin_end<R>::value,
const R, R>;
360 template <
typename R,
typename Char>
362 : is_formattable<uncvref_type<maybe_const_range<R>>, Char> {};
367 template <
typename P1,
typename... Pn>
369 : conditional_t<bool(P1::value), conjunction<Pn...>, P1> {};
371 template <
typename T,
typename Char,
typename Enable =
void>
374 template <
typename T,
typename Char>
378 is_formattable<T, Char>>::value>> {
386 bool is_debug =
false;
388 template <
typename Output,
typename It,
typename Sentinel,
typename U = T,
389 FMTQUILL_ENABLE_IF(std::is_same<U, Char>::value)>
390 auto write_debug_string(Output& out, It it, Sentinel end)
const -> Output {
392 for (; it != end; ++it) buf.push_back(*it);
394 specs.set_type(presentation_type::debug);
395 return detail::write<Char>(
399 template <
typename Output,
typename It,
typename Sentinel,
typename U = T,
400 FMTQUILL_ENABLE_IF(!std::is_same<U, Char>::value)>
401 auto write_debug_string(Output& out, It, Sentinel)
const -> Output {
418 opening_bracket_ = open;
419 closing_bracket_ = close;
423 auto it = ctx.
begin();
424 auto end = ctx.
end();
425 detail::maybe_set_debug_format(underlying_,
true);
426 if (it == end)
return underlying_.parse(ctx);
428 switch (detail::to_ascii(*it)) {
430 set_brackets({}, {});
435 set_brackets({}, {});
437 if (it == end || *it !=
's') report_error(
"invalid format specifier");
438 FMTQUILL_FALLTHROUGH;
440 if (!std::is_same<T, Char>::value)
441 report_error(
"invalid format specifier");
446 detail::maybe_set_debug_format(underlying_,
false);
452 if (it != end && *it !=
'}') {
453 if (*it !=
':') report_error(
"invalid format specifier");
454 detail::maybe_set_debug_format(underlying_,
false);
459 return underlying_.parse(ctx);
462 template <
typename R,
typename FormatContext>
463 auto format(R&& range, FormatContext& ctx)
const -> decltype(ctx.out()) {
464 auto out = ctx.out();
465 auto it = detail::range_begin(range);
466 auto end = detail::range_end(range);
467 if (is_debug)
return write_debug_string(out, std::move(it), end);
469 out = detail::copy<Char>(opening_bracket_, out);
471 for (; it != end; ++it) {
472 if (i > 0) out = detail::copy<Char>(separator_, out);
475 out = underlying_.format(item, ctx);
478 out = detail::copy<Char>(closing_bracket_, out);
484 template <
typename T,
typename Char,
typename Enable =
void>
487 is_range<T, Char>::value, detail::range_format_kind_<T>,
488 std::integral_constant<range_format, range_format::disabled>> {};
490 template <
typename R,
typename Char>
501 using range_type = detail::maybe_const_range<R>;
505 using nonlocking = void;
516 return range_formatter_.parse(ctx);
519 template <
typename FormatContext>
520 auto format(range_type& range, FormatContext& ctx)
const 521 -> decltype(ctx.out()) {
522 return range_formatter_.format(range, ctx);
527 template <
typename R,
typename Char>
534 using map_type = detail::maybe_const_range<R>;
535 using element_type = detail::uncvref_type<map_type>;
537 decltype(detail::tuple::get_formatters<element_type, Char>(
539 bool no_delimiters_ =
false;
545 auto it = ctx.
begin();
546 auto end = ctx.
end();
548 if (detail::to_ascii(*it) ==
'n') {
549 no_delimiters_ =
true;
552 if (it != end && *it !=
'}') {
553 if (*it !=
':') report_error(
"invalid format specifier");
562 template <
typename FormatContext>
563 auto format(map_type& map, FormatContext& ctx)
const -> decltype(ctx.out()) {
564 auto out = ctx.out();
566 if (!no_delimiters_) out = detail::copy<Char>(open, out);
569 for (
auto&& value : map) {
570 if (i > 0) out = detail::copy<Char>(sep, out);
572 detail::for_each2(formatters_, value,
578 if (!no_delimiters_) out = detail::copy<Char>(close, out);
584 template <
typename R,
typename Char>
589 range_format::debug_string>> {
591 using range_type = detail::maybe_const_range<R>;
593 conditional_t<std::is_constructible<
595 decltype(detail::range_begin(std::declval<R>())),
596 decltype(detail::range_end(std::declval<R>()))>::value,
597 detail::std_string_view<Char>, std::basic_string<Char>>;
603 return underlying_.parse(ctx);
606 template <
typename FormatContext>
607 auto format(range_type& range, FormatContext& ctx)
const 608 -> decltype(ctx.out()) {
609 auto out = ctx.out();
611 range_format::debug_string))
613 out = underlying_.format(
614 string_type{detail::range_begin(range), detail::range_end(range)}, ctx);
616 range_format::debug_string))
622 template <
typename It,
typename Sentinel,
typename Char =
char>
629 : begin(std::move(b)), end(e), sep(s) {}
632 template <
typename It,
typename Sentinel,
typename Char>
636 #ifdef __cpp_lib_ranges 637 std::iter_value_t<It>;
639 typename std::iterator_traits<It>::value_type;
643 using view = conditional_t<std::is_copy_constructible<It>::value,
645 join_view<It, Sentinel, Char>>;
648 using nonlocking = void;
651 return value_formatter_.parse(ctx);
654 template <
typename FormatContext>
655 auto format(view& value, FormatContext& ctx)
const -> decltype(ctx.out()) {
657 conditional_t<std::is_copy_constructible<view>::value, It, It&>;
658 iter it = value.begin;
659 auto out = ctx.out();
660 if (it == value.end)
return out;
661 out = value_formatter_.format(*it, ctx);
663 while (it != value.end) {
664 out = detail::copy<Char>(value.sep.begin(), value.sep.end(), out);
666 out = value_formatter_.format(*it, ctx);
678 : tuple(t), sep{s} {}
684 #ifndef FMTQUILL_TUPLE_JOIN_SPECIFIERS 685 # define FMTQUILL_TUPLE_JOIN_SPECIFIERS 0 688 template <
typename Char,
typename Tuple>
692 return do_parse(ctx, std::tuple_size<Tuple>());
695 template <
typename FormatContext>
697 FormatContext& ctx)
const ->
typename FormatContext::iterator {
698 return do_format(value, ctx, std::tuple_size<Tuple>());
702 decltype(detail::tuple::get_formatters<Tuple, Char>(
706 std::integral_constant<size_t, 0>)
713 std::integral_constant<size_t, N>)
715 auto end = ctx.
begin();
716 #if FMTQUILL_TUPLE_JOIN_SPECIFIERS 717 end = std::get<std::tuple_size<Tuple>::value - N>(formatters_).parse(ctx);
719 auto end1 = do_parse(ctx, std::integral_constant<size_t, N - 1>());
721 report_error(
"incompatible format specs for tuple elements");
727 template <
typename FormatContext>
729 std::integral_constant<size_t, 0>)
const ->
730 typename FormatContext::iterator {
734 template <
typename FormatContext,
size_t N>
736 std::integral_constant<size_t, N>)
const ->
737 typename FormatContext::iterator {
740 std::get<std::tuple_size<Tuple>::value - N>(formatters_)
741 .format(
get<std::tuple_size<Tuple>::value - N>(value.tuple), ctx);
742 if (N <= 1)
return out;
743 out = detail::copy<Char>(value.sep, out);
745 return do_format(value, ctx, std::integral_constant<size_t, N - 1>());
753 template <
typename U>
static auto check(U* p) ->
typename U::container_type;
754 template <
typename>
static void check(...);
757 static constexpr
const bool value =
758 !std::is_void<decltype(check<T>(
nullptr))>
::value;
761 template <
typename Container>
struct all {
763 auto begin()
const ->
typename Container::const_iterator {
return c.begin(); }
764 auto end()
const ->
typename Container::const_iterator {
return c.end(); }
768 template <
typename T,
typename Char>
772 bool_constant<range_format_kind<T, Char>::value ==
773 range_format::disabled>>::value>>
774 :
formatter<detail::all<typename T::container_type>, Char> {
776 template <
typename FormatContext>
777 auto format(
const T& value, FormatContext& ctx)
const -> decltype(ctx.out()) {
779 static auto get(
const T& v) -> all {
780 return {v.*(&getter::c)};
787 FMTQUILL_BEGIN_EXPORT
791 template <
typename It,
typename Sentinel>
793 return {std::move(begin), end, sep};
810 template <typename Range, FMTQUILL_ENABLE_IF(!is_tuple_like<Range>::value)>
812 ->
join_view<decltype(detail::range_begin(r)),
813 decltype(detail::range_end(r))> {
814 return {detail::range_begin(r), detail::range_end(r), sep};
826 template <typename Tuple, FMTQUILL_ENABLE_IF(is_tuple_like<Tuple>::value)>
827 FMTQUILL_CONSTEXPR
auto join(
const Tuple& tuple,
string_view sep)
841 template <
typename T>
842 auto join(std::initializer_list<T> list,
string_view sep)
844 return join(std::begin(list), std::end(list), sep);
848 FMTQUILL_END_NAMESPACE
850 #endif // FMTQUILL_RANGES_H_
A dynamically growing memory buffer for trivially copyable/constructible types with the first SIZE el...
Definition: format.h:790
Definition: doctest.h:539
Parsing context consisting of a format string range being parsed and an argument counter for automati...
Definition: base.h:644
FMTQUILL_CONSTEXPR void advance_to(iterator it)
Advances the begin iterator to it.
Definition: base.h:896
Setups a signal handler to handle fatal signals.
Definition: BackendManager.h:24
constexpr auto end() const noexcept -> iterator
Returns an iterator past the end of the format string range being parsed.
Definition: base.h:893
constexpr auto begin() const noexcept -> iterator
Returns an iterator to the beginning of the format string range being parsed.
Definition: base.h:890