1bdd1243dSDimitry Andric // -*- C++ -*- 2bdd1243dSDimitry Andric //===----------------------------------------------------------------------===// 3bdd1243dSDimitry Andric // 4bdd1243dSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5bdd1243dSDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 6bdd1243dSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7bdd1243dSDimitry Andric // 8bdd1243dSDimitry Andric //===----------------------------------------------------------------------===// 9bdd1243dSDimitry Andric 10bdd1243dSDimitry Andric #ifndef _LIBCPP___FORMAT_FORMATTER_TUPLE_H 11bdd1243dSDimitry Andric #define _LIBCPP___FORMAT_FORMATTER_TUPLE_H 12bdd1243dSDimitry Andric 13bdd1243dSDimitry Andric #include <__algorithm/ranges_copy.h> 14bdd1243dSDimitry Andric #include <__chrono/statically_widen.h> 15bdd1243dSDimitry Andric #include <__config> 16*06c3fb27SDimitry Andric #include <__format/buffer.h> 17bdd1243dSDimitry Andric #include <__format/concepts.h> 18bdd1243dSDimitry Andric #include <__format/format_context.h> 19bdd1243dSDimitry Andric #include <__format/format_error.h> 20bdd1243dSDimitry Andric #include <__format/format_parse_context.h> 21bdd1243dSDimitry Andric #include <__format/formatter.h> 22bdd1243dSDimitry Andric #include <__format/formatter_output.h> 23bdd1243dSDimitry Andric #include <__format/parser_std_format_spec.h> 24bdd1243dSDimitry Andric #include <__type_traits/remove_cvref.h> 25bdd1243dSDimitry Andric #include <__utility/integer_sequence.h> 26bdd1243dSDimitry Andric #include <__utility/pair.h> 27bdd1243dSDimitry Andric #include <string_view> 28bdd1243dSDimitry Andric #include <tuple> 29bdd1243dSDimitry Andric 30bdd1243dSDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 31bdd1243dSDimitry Andric # pragma GCC system_header 32bdd1243dSDimitry Andric #endif 33bdd1243dSDimitry Andric 34bdd1243dSDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD 35bdd1243dSDimitry Andric 36*06c3fb27SDimitry Andric #if _LIBCPP_STD_VER >= 23 37bdd1243dSDimitry Andric 38bdd1243dSDimitry Andric template <__fmt_char_type _CharT, class _Tuple, formattable<_CharT>... _Args> 39*06c3fb27SDimitry Andric struct _LIBCPP_TEMPLATE_VIS __formatter_tuple { set_separator__formatter_tuple40*06c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr void set_separator(basic_string_view<_CharT> __separator) noexcept { 41bdd1243dSDimitry Andric __separator_ = __separator; 42bdd1243dSDimitry Andric } 43bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr void set_brackets__formatter_tuple44*06c3fb27SDimitry Andric set_brackets(basic_string_view<_CharT> __opening_bracket, basic_string_view<_CharT> __closing_bracket) noexcept { 45bdd1243dSDimitry Andric __opening_bracket_ = __opening_bracket; 46bdd1243dSDimitry Andric __closing_bracket_ = __closing_bracket; 47bdd1243dSDimitry Andric } 48bdd1243dSDimitry Andric 49bdd1243dSDimitry Andric template <class _ParseContext> parse__formatter_tuple50*06c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) { 51*06c3fb27SDimitry Andric auto __begin = __parser_.__parse(__ctx, __format_spec::__fields_tuple); 52bdd1243dSDimitry Andric 53*06c3fb27SDimitry Andric auto __end = __ctx.end(); 54*06c3fb27SDimitry Andric // Note 'n' is part of the type here 55*06c3fb27SDimitry Andric if (__parser_.__clear_brackets_) 56*06c3fb27SDimitry Andric set_brackets({}, {}); 57*06c3fb27SDimitry Andric else if (__begin != __end && *__begin == _CharT('m')) { 58bdd1243dSDimitry Andric if constexpr (sizeof...(_Args) == 2) { 59bdd1243dSDimitry Andric set_separator(_LIBCPP_STATICALLY_WIDEN(_CharT, ": ")); 60bdd1243dSDimitry Andric set_brackets({}, {}); 61bdd1243dSDimitry Andric ++__begin; 62bdd1243dSDimitry Andric } else 63*06c3fb27SDimitry Andric std::__throw_format_error("Type m requires a pair or a tuple with two elements"); 64bdd1243dSDimitry Andric } 65bdd1243dSDimitry Andric 66bdd1243dSDimitry Andric if (__begin != __end && *__begin != _CharT('}')) 67*06c3fb27SDimitry Andric std::__throw_format_error("The format specifier should consume the input or end with a '}'"); 68*06c3fb27SDimitry Andric 69*06c3fb27SDimitry Andric __ctx.advance_to(__begin); 70*06c3fb27SDimitry Andric 71*06c3fb27SDimitry Andric // [format.tuple]/7 72*06c3fb27SDimitry Andric // ... For each element e in underlying_, if e.set_debug_format() 73*06c3fb27SDimitry Andric // is a valid expression, calls e.set_debug_format(). 74*06c3fb27SDimitry Andric std::__for_each_index_sequence(make_index_sequence<sizeof...(_Args)>(), [&]<size_t _Index> { 75*06c3fb27SDimitry Andric auto& __formatter = std::get<_Index>(__underlying_); 76*06c3fb27SDimitry Andric __formatter.parse(__ctx); 77*06c3fb27SDimitry Andric // Unlike the range_formatter we don't guard against evil parsers. Since 78*06c3fb27SDimitry Andric // this format-spec never has a format-spec for the underlying type 79*06c3fb27SDimitry Andric // adding the test would give additional overhead. 80*06c3fb27SDimitry Andric std::__set_debug_format(__formatter); 81*06c3fb27SDimitry Andric }); 82bdd1243dSDimitry Andric 83bdd1243dSDimitry Andric return __begin; 84bdd1243dSDimitry Andric } 85bdd1243dSDimitry Andric 86bdd1243dSDimitry Andric template <class _FormatContext> 87bdd1243dSDimitry Andric typename _FormatContext::iterator _LIBCPP_HIDE_FROM_ABI format__formatter_tuple88bdd1243dSDimitry Andric format(conditional_t<(formattable<const _Args, _CharT> && ...), const _Tuple&, _Tuple&> __tuple, 89bdd1243dSDimitry Andric _FormatContext& __ctx) const { 90bdd1243dSDimitry Andric __format_spec::__parsed_specifications<_CharT> __specs = __parser_.__get_parsed_std_specifications(__ctx); 91bdd1243dSDimitry Andric 92bdd1243dSDimitry Andric if (!__specs.__has_width()) 93bdd1243dSDimitry Andric return __format_tuple(__tuple, __ctx); 94bdd1243dSDimitry Andric 95*06c3fb27SDimitry Andric // The size of the buffer needed is: 96*06c3fb27SDimitry Andric // - open bracket characters 97*06c3fb27SDimitry Andric // - close bracket character 98*06c3fb27SDimitry Andric // - n elements where every element may have a different size 99*06c3fb27SDimitry Andric // - (n -1) separators 100*06c3fb27SDimitry Andric // The size of the element is hard to predict, knowing the type helps but 101*06c3fb27SDimitry Andric // it depends on the format-spec. As an initial estimate we guess 6 102*06c3fb27SDimitry Andric // characters. 103*06c3fb27SDimitry Andric // Typically both brackets are 1 character and the separator is 2 104*06c3fb27SDimitry Andric // characters. Which means there will be 105*06c3fb27SDimitry Andric // (n - 1) * 2 + 1 + 1 = n * 2 character 106*06c3fb27SDimitry Andric // So estimate 8 times the range size as buffer. 107*06c3fb27SDimitry Andric __format::__retarget_buffer<_CharT> __buffer{8 * tuple_size_v<_Tuple>}; 108*06c3fb27SDimitry Andric basic_format_context<typename __format::__retarget_buffer<_CharT>::__iterator, _CharT> __c{ 109*06c3fb27SDimitry Andric __buffer.__make_output_iterator(), __ctx}; 110bdd1243dSDimitry Andric 111bdd1243dSDimitry Andric __format_tuple(__tuple, __c); 112bdd1243dSDimitry Andric 113*06c3fb27SDimitry Andric return __formatter::__write_string_no_precision(basic_string_view{__buffer.__view()}, __ctx.out(), __specs); 114bdd1243dSDimitry Andric } 115bdd1243dSDimitry Andric 116bdd1243dSDimitry Andric template <class _FormatContext> __format_tuple__formatter_tuple117bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator __format_tuple(auto&& __tuple, _FormatContext& __ctx) const { 118bdd1243dSDimitry Andric __ctx.advance_to(std::ranges::copy(__opening_bracket_, __ctx.out()).out); 119bdd1243dSDimitry Andric 120bdd1243dSDimitry Andric std::__for_each_index_sequence(make_index_sequence<sizeof...(_Args)>(), [&]<size_t _Index> { 121bdd1243dSDimitry Andric if constexpr (_Index) 122bdd1243dSDimitry Andric __ctx.advance_to(std::ranges::copy(__separator_, __ctx.out()).out); 123bdd1243dSDimitry Andric __ctx.advance_to(std::get<_Index>(__underlying_).format(std::get<_Index>(__tuple), __ctx)); 124bdd1243dSDimitry Andric }); 125bdd1243dSDimitry Andric 126bdd1243dSDimitry Andric return std::ranges::copy(__closing_bracket_, __ctx.out()).out; 127bdd1243dSDimitry Andric } 128bdd1243dSDimitry Andric 129bdd1243dSDimitry Andric __format_spec::__parser<_CharT> __parser_{.__alignment_ = __format_spec::__alignment::__left}; 130bdd1243dSDimitry Andric 131bdd1243dSDimitry Andric private: 132bdd1243dSDimitry Andric tuple<formatter<remove_cvref_t<_Args>, _CharT>...> __underlying_; 133bdd1243dSDimitry Andric basic_string_view<_CharT> __separator_ = _LIBCPP_STATICALLY_WIDEN(_CharT, ", "); 134bdd1243dSDimitry Andric basic_string_view<_CharT> __opening_bracket_ = _LIBCPP_STATICALLY_WIDEN(_CharT, "("); 135bdd1243dSDimitry Andric basic_string_view<_CharT> __closing_bracket_ = _LIBCPP_STATICALLY_WIDEN(_CharT, ")"); 136bdd1243dSDimitry Andric }; 137bdd1243dSDimitry Andric 138bdd1243dSDimitry Andric template <__fmt_char_type _CharT, formattable<_CharT>... _Args> 139*06c3fb27SDimitry Andric struct _LIBCPP_TEMPLATE_VIS formatter<pair<_Args...>, _CharT> 140bdd1243dSDimitry Andric : public __formatter_tuple<_CharT, pair<_Args...>, _Args...> {}; 141bdd1243dSDimitry Andric 142bdd1243dSDimitry Andric template <__fmt_char_type _CharT, formattable<_CharT>... _Args> 143*06c3fb27SDimitry Andric struct _LIBCPP_TEMPLATE_VIS formatter<tuple<_Args...>, _CharT> 144bdd1243dSDimitry Andric : public __formatter_tuple<_CharT, tuple<_Args...>, _Args...> {}; 145bdd1243dSDimitry Andric 146*06c3fb27SDimitry Andric #endif //_LIBCPP_STD_VER >= 23 147bdd1243dSDimitry Andric 148bdd1243dSDimitry Andric _LIBCPP_END_NAMESPACE_STD 149bdd1243dSDimitry Andric 150bdd1243dSDimitry Andric #endif // _LIBCPP___FORMAT_FORMATTER_TUPLE_H 151