8 #ifndef FMTQUILL_RANGES_H_ 9 #define FMTQUILL_RANGES_H_ 11 #ifndef FMTQUILL_MODULE 12 # include <initializer_list> 15 # include <type_traits> 21 #if FMTQUILL_HAS_CPP_ATTRIBUTE(clang::lifetimebound) 22 # define FMTQUILL_LIFETIMEBOUND [[clang::lifetimebound]] 24 # define FMTQUILL_LIFETIMEBOUND 26 FMTQUILL_PRAGMA_CLANG(diagnostic
error "-Wreturn-stack-address")
28 FMTQUILL_BEGIN_NAMESPACE
31 enum class range_format { disabled, map,
set, sequence, string, debug_string };
36 template <
typename U>
static auto check(U*) ->
typename U::mapped_type;
37 template <
typename>
static void check(...);
40 static constexpr
bool value =
41 !std::is_void<decltype(check<T>(
nullptr))>
::value;
45 template <
typename U>
static auto check(U*) ->
typename U::key_type;
46 template <
typename>
static void check(...);
49 static constexpr
bool value =
54 template <
typename T,
size_t N>
55 auto range_begin(
const T (&arr)[N]) ->
const T* {
58 template <
typename T,
size_t N>
auto range_end(
const T (&arr)[N]) ->
const T* {
62 template <
typename T,
typename Enable =
void>
67 decltype(std::declval<T>().end())>>
72 auto range_begin(T&& rng) -> decltype(static_cast<T&&>(rng).begin()) {
73 return static_cast<T&&
>(rng).begin();
76 auto range_end(T&& rng) -> decltype(static_cast<T&&>(rng).end()) {
77 return static_cast<T&&
>(rng).end();
83 auto range_begin(T&& rng)
84 -> enable_if_t<!has_member_fn_begin_end_t<T&&>::value,
85 decltype(begin(static_cast<T&&>(rng)))> {
86 return begin(static_cast<T&&>(rng));
89 auto range_end(T&& rng) -> enable_if_t<!has_member_fn_begin_end_t<T&&>::value,
90 decltype(end(static_cast<T&&>(rng)))> {
91 return end(static_cast<T&&>(rng));
94 template <
typename T,
typename Enable =
void>
96 template <
typename T,
typename Enable =
void>
101 T, void_t<decltype(*detail::range_begin(
102 std::declval<
const remove_cvref_t<T>&>())),
103 decltype(detail::range_end(
104 std::declval<
const remove_cvref_t<T>&>()))>>
107 template <
typename T>
109 T, void_t<decltype(*detail::range_begin(std::declval<T&>())),
110 decltype(detail::range_end(std::declval<T&>())),
113 int>> : std::true_type {};
115 template <
typename T,
typename _ =
void>
struct is_range_ : std::false_type {};
116 template <
typename T>
118 : std::integral_constant<bool, (has_const_begin_end<T>::value ||
119 has_mutable_begin_end<T>::value)> {};
123 template <typename U, typename V = typename std::remove_cv<U>::type>
124 static auto check(U* p) -> decltype(std::tuple_size<V>::value, 0);
125 template <
typename>
static void check(...);
128 static constexpr
bool value =
129 !std::is_void<decltype(check<T>(
nullptr))>
::value;
133 #if defined(__cpp_lib_integer_sequence) || FMTQUILL_MSC_VERSION >= 1900 134 template <
typename T, T... N>
136 template <
size_t... N>
using index_sequence = std::index_sequence<N...>;
139 template <
typename T, T... N>
struct integer_sequence {
140 using value_type = T;
142 static FMTQUILL_CONSTEXPR
auto size() ->
size_t {
return sizeof...(N); }
145 template <
size_t... N>
using index_sequence = integer_sequence<size_t, N...>;
147 template <
typename T,
size_t N, T... Ns>
149 template <
typename T, T... Ns>
156 template <
typename T>
159 template <typename T, typename C, bool = is_tuple_like_<T>::value>
162 static constexpr
bool value =
false;
165 template <
size_t... Is>
167 integer_sequence<
bool, (Is >= 0)...>) -> std::true_type;
168 static auto all_true(...) -> std::false_type;
170 template <
size_t... Is>
173 integer_sequence<bool,
174 (is_formattable<typename std::tuple_element<Is, T>::type,
178 static constexpr
bool value =
182 template <
typename Tuple,
typename F,
size_t... Is>
186 const int unused[] = {0, ((void)f(get<Is>(t)), 0)...};
187 ignore_unused(unused);
190 template <
typename Tuple,
typename F>
191 FMTQUILL_CONSTEXPR
void for_each(Tuple&& t, F&& f) {
193 std::forward<Tuple>(t), std::forward<F>(f));
196 template <
typename Tuple1,
typename Tuple2,
typename F,
size_t... Is>
199 const int unused[] = {0, ((void)f(get<Is>(t1), get<Is>(t2)), 0)...};
200 ignore_unused(unused);
203 template <
typename Tuple1,
typename Tuple2,
typename F>
204 void for_each2(Tuple1&& t1, Tuple2&& t2, F&& f) {
206 std::forward<Tuple1>(t1), std::forward<Tuple2>(t2),
212 template <
typename Char,
typename... T>
216 template <
typename Tuple,
typename Char,
size_t... Is>
218 -> result_t<Char, decltype(get<Is>(std::declval<Tuple>()))...>;
221 #if FMTQUILL_MSC_VERSION && FMTQUILL_MSC_VERSION < 1920 223 template <
typename R>
struct range_reference_type_impl {
224 using type = decltype(*detail::range_begin(std::declval<R&>()));
227 template <
typename T,
size_t N>
struct range_reference_type_impl<T[N]> {
231 template <
typename T>
232 using range_reference_type =
typename range_reference_type_impl<T>::type;
234 template <
typename Range>
235 using range_reference_type =
236 decltype(*detail::range_begin(std::declval<Range&>()));
241 template <
typename Range>
242 using uncvref_type = remove_cvref_t<range_reference_type<Range>>;
244 template <
typename T>
246 : std::integral_constant<range_format,
247 std::is_same<uncvref_type<T>, T>::value
248 ? range_format::disabled
249 : is_map<T>::value ? range_format::map
250 : is_set<T>::value ? range_format::set
251 : range_format::sequence> {};
253 template <range_format K>
254 using range_format_constant = std::integral_constant<range_format, K>;
258 template <
typename Formatter> FMTQUILL_CONSTEXPR
void operator()(Formatter& f) {
260 detail::maybe_set_debug_format(f,
true);
265 using char_type =
typename FormatContext::char_type;
267 template <
typename T>
269 if (i > 0) ctx.advance_to(detail::copy<char_type>(separator, ctx.out()));
270 ctx.advance_to(f.format(v, ctx));
283 static constexpr
bool value =
292 template <
typename Tuple,
typename Char>
295 fmtquill::is_tuple_formattable<Tuple, Char>::value>> {
297 decltype(detail::tuple::get_formatters<Tuple, Char>(
315 opening_bracket_ = open;
316 closing_bracket_ = close;
320 auto it = ctx.
begin();
321 auto end = ctx.
end();
322 if (it != end && detail::to_ascii(*it) ==
'n') {
324 set_brackets({}, {});
327 if (it != end && *it !=
'}') report_error(
"invalid format specifier");
333 template <
typename FormatContext>
334 auto format(
const Tuple& value, FormatContext& ctx)
const 335 -> decltype(ctx.out()) {
336 ctx.advance_to(detail::copy<Char>(opening_bracket_, ctx.out()));
340 return detail::copy<Char>(closing_bracket_, ctx.out());
345 template <
typename T,
typename Char>
struct is_range {
346 static constexpr
bool value =
352 template <
typename Char,
typename Element>
355 template <
typename R>
356 using maybe_const_range =
357 conditional_t<has_const_begin_end<R>::value,
const R, R>;
359 template <
typename R,
typename Char>
361 : is_formattable<uncvref_type<maybe_const_range<R>>, Char> {};
366 template <
typename P1,
typename... Pn>
368 : 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);
679 : tuple(t), sep{s} {}
685 #ifndef FMTQUILL_TUPLE_JOIN_SPECIFIERS 686 # define FMTQUILL_TUPLE_JOIN_SPECIFIERS 0 689 template <
typename Tuple,
typename Char>
693 return do_parse(ctx, std::tuple_size<Tuple>());
696 template <
typename FormatContext>
698 FormatContext& ctx)
const ->
typename FormatContext::iterator {
699 return do_format(value, ctx, std::tuple_size<Tuple>());
703 decltype(detail::tuple::get_formatters<Tuple, Char>(
707 std::integral_constant<size_t, 0>)
714 std::integral_constant<size_t, N>)
716 auto end = ctx.
begin();
717 #if FMTQUILL_TUPLE_JOIN_SPECIFIERS 718 end = std::get<std::tuple_size<Tuple>::value - N>(formatters_).parse(ctx);
720 auto end1 = do_parse(ctx, std::integral_constant<size_t, N - 1>());
722 report_error(
"incompatible format specs for tuple elements");
728 template <
typename FormatContext>
730 std::integral_constant<size_t, 0>)
const ->
731 typename FormatContext::iterator {
735 template <
typename FormatContext,
size_t N>
737 std::integral_constant<size_t, N>)
const ->
738 typename FormatContext::iterator {
741 std::get<std::tuple_size<Tuple>::value - N>(formatters_)
742 .format(
get<std::tuple_size<Tuple>::value - N>(value.tuple), ctx);
743 if (N <= 1)
return out;
744 out = detail::copy<Char>(value.sep, out);
746 return do_format(value, ctx, std::integral_constant<size_t, N - 1>());
754 template <
typename U>
static auto check(U* p) ->
typename U::container_type;
755 template <
typename>
static void check(...);
758 static constexpr
bool value =
759 !std::is_void<decltype(check<T>(
nullptr))>
::value;
762 template <
typename Container>
struct all {
764 auto begin()
const ->
typename Container::const_iterator {
return c.begin(); }
765 auto end()
const ->
typename Container::const_iterator {
return c.end(); }
769 template <
typename T,
typename Char>
773 bool_constant<range_format_kind<T, Char>::value ==
774 range_format::disabled>>::value>>
775 :
formatter<detail::all<typename T::container_type>, Char> {
777 template <
typename FormatContext>
778 auto format(
const T& value, FormatContext& ctx)
const -> decltype(ctx.out()) {
780 static auto get(
const T& v) -> all {
781 return {v.*(&getter::c)};
788 FMTQUILL_BEGIN_EXPORT
792 template <
typename It,
typename Sentinel>
794 return {std::move(begin), end, sep};
811 template <typename Range, FMTQUILL_ENABLE_IF(!is_tuple_like<Range>::value)>
813 ->
join_view<decltype(detail::range_begin(r)),
814 decltype(detail::range_end(r))> {
815 return {detail::range_begin(r), detail::range_end(r), sep};
827 template <typename Tuple, FMTQUILL_ENABLE_IF(is_tuple_like<Tuple>::value)>
828 FMTQUILL_CONSTEXPR
auto join(
const Tuple& tuple FMTQUILL_LIFETIMEBOUND,
string_view sep)
842 template <
typename T>
843 auto join(std::initializer_list<T> list,
string_view sep)
845 return join(std::begin(list), std::end(list), sep);
849 FMTQUILL_END_NAMESPACE
851 #endif // FMTQUILL_RANGES_H_
A dynamically growing memory buffer for trivially copyable/constructible types with the first SIZE el...
Definition: format.h:809
Definition: doctest.h:539
Parsing context consisting of a format string range being parsed and an argument counter for automati...
Definition: base.h:634
Definition: UserDefinedDirectFormatFuzzer.cpp:81
FMTQUILL_CONSTEXPR void advance_to(iterator it)
Advances the begin iterator to it.
Definition: base.h:886
Definition: LogFunctions.h:261
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:883
constexpr auto begin() const noexcept -> iterator
Returns an iterator to the beginning of the format string range being parsed.
Definition: base.h:880