xref: /freebsd-src/contrib/llvm-project/libcxx/include/__format/range_formatter.h (revision 1db9f3b21e39176dd5b67cf8ac378633b172463e)
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_RANGE_FORMATTER_H
11bdd1243dSDimitry Andric #define _LIBCPP___FORMAT_RANGE_FORMATTER_H
12bdd1243dSDimitry Andric 
13bdd1243dSDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
14bdd1243dSDimitry Andric #  pragma GCC system_header
15bdd1243dSDimitry Andric #endif
16bdd1243dSDimitry Andric 
17bdd1243dSDimitry Andric #include <__algorithm/ranges_copy.h>
18bdd1243dSDimitry Andric #include <__chrono/statically_widen.h>
19bdd1243dSDimitry Andric #include <__concepts/same_as.h>
20bdd1243dSDimitry Andric #include <__config>
21bdd1243dSDimitry Andric #include <__format/buffer.h>
22bdd1243dSDimitry Andric #include <__format/concepts.h>
23bdd1243dSDimitry Andric #include <__format/format_context.h>
24bdd1243dSDimitry Andric #include <__format/format_error.h>
25bdd1243dSDimitry Andric #include <__format/formatter.h>
26bdd1243dSDimitry Andric #include <__format/formatter_output.h>
27bdd1243dSDimitry Andric #include <__format/parser_std_format_spec.h>
28bdd1243dSDimitry Andric #include <__iterator/back_insert_iterator.h>
29bdd1243dSDimitry Andric #include <__ranges/concepts.h>
30bdd1243dSDimitry Andric #include <__ranges/data.h>
315f757f3fSDimitry Andric #include <__ranges/from_range.h>
32bdd1243dSDimitry Andric #include <__ranges/size.h>
33bdd1243dSDimitry Andric #include <__type_traits/remove_cvref.h>
34bdd1243dSDimitry Andric #include <string_view>
35bdd1243dSDimitry Andric 
36bdd1243dSDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
37bdd1243dSDimitry Andric 
3806c3fb27SDimitry Andric #if _LIBCPP_STD_VER >= 23
39bdd1243dSDimitry Andric 
40bdd1243dSDimitry Andric template <class _Tp, class _CharT = char>
41bdd1243dSDimitry Andric   requires same_as<remove_cvref_t<_Tp>, _Tp> && formattable<_Tp, _CharT>
4206c3fb27SDimitry Andric struct _LIBCPP_TEMPLATE_VIS range_formatter {
set_separatorrange_formatter4306c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr void set_separator(basic_string_view<_CharT> __separator) noexcept {
44bdd1243dSDimitry Andric     __separator_ = __separator;
45bdd1243dSDimitry Andric   }
46bdd1243dSDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr void
set_bracketsrange_formatter4706c3fb27SDimitry Andric   set_brackets(basic_string_view<_CharT> __opening_bracket, basic_string_view<_CharT> __closing_bracket) noexcept {
48bdd1243dSDimitry Andric     __opening_bracket_ = __opening_bracket;
49bdd1243dSDimitry Andric     __closing_bracket_ = __closing_bracket;
50bdd1243dSDimitry Andric   }
51bdd1243dSDimitry Andric 
underlyingrange_formatter5206c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr formatter<_Tp, _CharT>& underlying() noexcept { return __underlying_; }
underlyingrange_formatter5306c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr const formatter<_Tp, _CharT>& underlying() const noexcept { return __underlying_; }
54bdd1243dSDimitry Andric 
55bdd1243dSDimitry Andric   template <class _ParseContext>
parserange_formatter5606c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) {
5706c3fb27SDimitry Andric     auto __begin = __parser_.__parse(__ctx, __format_spec::__fields_range);
5806c3fb27SDimitry Andric     auto __end   = __ctx.end();
5906c3fb27SDimitry Andric     // Note the cases where __begin == __end in this code only happens when the
6006c3fb27SDimitry Andric     // replacement-field has no terminating }, or when the parse is manually
6106c3fb27SDimitry Andric     // called with a format-spec. The former is an error and the latter means
6206c3fb27SDimitry Andric     // using a formatter without the format functions or print.
6306c3fb27SDimitry Andric     if (__begin == __end) [[unlikely]]
6406c3fb27SDimitry Andric       return __parse_empty_range_underlying_spec(__ctx, __begin);
65bdd1243dSDimitry Andric 
66bdd1243dSDimitry Andric     // The n field overrides a possible m type, therefore delay applying the
67bdd1243dSDimitry Andric     // effect of n until the type has been procesed.
68bdd1243dSDimitry Andric     __parse_type(__begin, __end);
6906c3fb27SDimitry Andric     if (__parser_.__clear_brackets_)
70bdd1243dSDimitry Andric       set_brackets({}, {});
7106c3fb27SDimitry Andric     if (__begin == __end) [[unlikely]]
7206c3fb27SDimitry Andric       return __parse_empty_range_underlying_spec(__ctx, __begin);
73bdd1243dSDimitry Andric 
74bdd1243dSDimitry Andric     bool __has_range_underlying_spec = *__begin == _CharT(':');
7506c3fb27SDimitry Andric     if (__has_range_underlying_spec) {
7606c3fb27SDimitry Andric       // range-underlying-spec:
7706c3fb27SDimitry Andric       //   :  format-spec
7806c3fb27SDimitry Andric       ++__begin;
7906c3fb27SDimitry Andric     } else if (__begin != __end && *__begin != _CharT('}'))
8006c3fb27SDimitry Andric       // When there is no underlaying range the current parse should have
8106c3fb27SDimitry Andric       // consumed the format-spec. If not, the not consumed input will be
8206c3fb27SDimitry Andric       // processed by the underlying. For example {:-} for a range in invalid,
8306c3fb27SDimitry Andric       // the sign field is not present. Without this check the underlying_ will
8406c3fb27SDimitry Andric       // get -} as input which my be valid.
8506c3fb27SDimitry Andric       std::__throw_format_error("The format specifier should consume the input or end with a '}'");
8606c3fb27SDimitry Andric 
8706c3fb27SDimitry Andric     __ctx.advance_to(__begin);
8806c3fb27SDimitry Andric     __begin = __underlying_.parse(__ctx);
8906c3fb27SDimitry Andric 
9006c3fb27SDimitry Andric     // This test should not be required if __has_range_underlying_spec is false.
9106c3fb27SDimitry Andric     // However this test makes sure the underlying formatter left the parser in
9206c3fb27SDimitry Andric     // a valid state. (Note this is not a full protection against evil parsers.
9306c3fb27SDimitry Andric     // For example
9406c3fb27SDimitry Andric     //   } this is test for the next argument {}
9506c3fb27SDimitry Andric     //   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
9606c3fb27SDimitry Andric     // could consume more than it should.
9706c3fb27SDimitry Andric     if (__begin != __end && *__begin != _CharT('}'))
9806c3fb27SDimitry Andric       std::__throw_format_error("The format specifier should consume the input or end with a '}'");
9906c3fb27SDimitry Andric 
100bdd1243dSDimitry Andric     if (__parser_.__type_ != __format_spec::__type::__default) {
101bdd1243dSDimitry Andric       // [format.range.formatter]/6
102bdd1243dSDimitry Andric       //   If the range-type is s or ?s, then there shall be no n option and no
103bdd1243dSDimitry Andric       //   range-underlying-spec.
10406c3fb27SDimitry Andric       if (__parser_.__clear_brackets_) {
105bdd1243dSDimitry Andric         if (__parser_.__type_ == __format_spec::__type::__string)
106bdd1243dSDimitry Andric           std::__throw_format_error("The n option and type s can't be used together");
107bdd1243dSDimitry Andric         std::__throw_format_error("The n option and type ?s can't be used together");
108bdd1243dSDimitry Andric       }
109bdd1243dSDimitry Andric       if (__has_range_underlying_spec) {
110bdd1243dSDimitry Andric         if (__parser_.__type_ == __format_spec::__type::__string)
111bdd1243dSDimitry Andric           std::__throw_format_error("Type s and an underlying format specification can't be used together");
112bdd1243dSDimitry Andric         std::__throw_format_error("Type ?s and an underlying format specification can't be used together");
113bdd1243dSDimitry Andric       }
114bdd1243dSDimitry Andric     } else if (!__has_range_underlying_spec)
115bdd1243dSDimitry Andric       std::__set_debug_format(__underlying_);
116bdd1243dSDimitry Andric 
117bdd1243dSDimitry Andric     return __begin;
118bdd1243dSDimitry Andric   }
119bdd1243dSDimitry Andric 
120bdd1243dSDimitry Andric   template <ranges::input_range _Rp, class _FormatContext>
121bdd1243dSDimitry Andric     requires formattable<ranges::range_reference_t<_Rp>, _CharT> &&
122bdd1243dSDimitry Andric              same_as<remove_cvref_t<ranges::range_reference_t<_Rp>>, _Tp>
formatrange_formatter123bdd1243dSDimitry Andric   _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator format(_Rp&& __range, _FormatContext& __ctx) const {
124bdd1243dSDimitry Andric     __format_spec::__parsed_specifications<_CharT> __specs = __parser_.__get_parsed_std_specifications(__ctx);
125bdd1243dSDimitry Andric 
126bdd1243dSDimitry Andric     if (!__specs.__has_width())
127bdd1243dSDimitry Andric       return __format_range(__range, __ctx, __specs);
128bdd1243dSDimitry Andric 
129bdd1243dSDimitry Andric     // The size of the buffer needed is:
130bdd1243dSDimitry Andric     // - open bracket characters
131bdd1243dSDimitry Andric     // - close bracket character
132bdd1243dSDimitry Andric     // - n elements where every element may have a different size
133bdd1243dSDimitry Andric     // - (n -1) separators
134bdd1243dSDimitry Andric     // The size of the element is hard to predict, knowing the type helps but
135bdd1243dSDimitry Andric     // it depends on the format-spec. As an initial estimate we guess 6
136bdd1243dSDimitry Andric     // characters.
137bdd1243dSDimitry Andric     // Typically both brackets are 1 character and the separator is 2
138bdd1243dSDimitry Andric     // characters. Which means there will be
139bdd1243dSDimitry Andric     //   (n - 1) * 2 + 1 + 1 = n * 2 character
140bdd1243dSDimitry Andric     // So estimate 8 times the range size as buffer.
141bdd1243dSDimitry Andric     std::size_t __capacity_hint = 0;
142bdd1243dSDimitry Andric     if constexpr (std::ranges::sized_range<_Rp>)
143bdd1243dSDimitry Andric       __capacity_hint = 8 * ranges::size(__range);
144bdd1243dSDimitry Andric     __format::__retarget_buffer<_CharT> __buffer{__capacity_hint};
145bdd1243dSDimitry Andric     basic_format_context<typename __format::__retarget_buffer<_CharT>::__iterator, _CharT> __c{
146bdd1243dSDimitry Andric         __buffer.__make_output_iterator(), __ctx};
147bdd1243dSDimitry Andric 
148bdd1243dSDimitry Andric     __format_range(__range, __c, __specs);
149bdd1243dSDimitry Andric 
150bdd1243dSDimitry Andric     return __formatter::__write_string_no_precision(__buffer.__view(), __ctx.out(), __specs);
151bdd1243dSDimitry Andric   }
152bdd1243dSDimitry Andric 
153bdd1243dSDimitry Andric   template <ranges::input_range _Rp, class _FormatContext>
154bdd1243dSDimitry Andric   typename _FormatContext::iterator _LIBCPP_HIDE_FROM_ABI
__format_rangerange_formatter155bdd1243dSDimitry Andric   __format_range(_Rp&& __range, _FormatContext& __ctx, __format_spec::__parsed_specifications<_CharT> __specs) const {
156bdd1243dSDimitry Andric     if constexpr (same_as<_Tp, _CharT>) {
157bdd1243dSDimitry Andric       switch (__specs.__std_.__type_) {
158bdd1243dSDimitry Andric       case __format_spec::__type::__string:
159bdd1243dSDimitry Andric       case __format_spec::__type::__debug:
160bdd1243dSDimitry Andric         return __format_as_string(__range, __ctx, __specs.__std_.__type_ == __format_spec::__type::__debug);
161bdd1243dSDimitry Andric       default:
162bdd1243dSDimitry Andric         return __format_as_sequence(__range, __ctx);
163bdd1243dSDimitry Andric       }
164bdd1243dSDimitry Andric     } else
165bdd1243dSDimitry Andric       return __format_as_sequence(__range, __ctx);
166bdd1243dSDimitry Andric   }
167bdd1243dSDimitry Andric 
168bdd1243dSDimitry Andric   template <ranges::input_range _Rp, class _FormatContext>
169bdd1243dSDimitry Andric   _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator
__format_as_stringrange_formatter170bdd1243dSDimitry Andric   __format_as_string(_Rp&& __range, _FormatContext& __ctx, bool __debug_format) const {
171bdd1243dSDimitry Andric     // When the range is contiguous use a basic_string_view instead to avoid a
172bdd1243dSDimitry Andric     // copy of the underlying data. The basic_string_view formatter
173bdd1243dSDimitry Andric     // specialization is the "basic" string formatter in libc++.
174bdd1243dSDimitry Andric     if constexpr (ranges::contiguous_range<_Rp> && std::ranges::sized_range<_Rp>) {
175bdd1243dSDimitry Andric       std::formatter<basic_string_view<_CharT>, _CharT> __formatter;
176bdd1243dSDimitry Andric       if (__debug_format)
177bdd1243dSDimitry Andric         __formatter.set_debug_format();
178bdd1243dSDimitry Andric       return __formatter.format(
179bdd1243dSDimitry Andric           basic_string_view<_CharT>{
180bdd1243dSDimitry Andric               ranges::data(__range),
181bdd1243dSDimitry Andric               ranges::size(__range),
182bdd1243dSDimitry Andric           },
183bdd1243dSDimitry Andric           __ctx);
184bdd1243dSDimitry Andric     } else {
185bdd1243dSDimitry Andric       std::formatter<basic_string<_CharT>, _CharT> __formatter;
186bdd1243dSDimitry Andric       if (__debug_format)
187bdd1243dSDimitry Andric         __formatter.set_debug_format();
1885f757f3fSDimitry Andric       return __formatter.format(basic_string<_CharT>{from_range, __range}, __ctx);
189bdd1243dSDimitry Andric     }
190bdd1243dSDimitry Andric   }
191bdd1243dSDimitry Andric 
192bdd1243dSDimitry Andric   template <ranges::input_range _Rp, class _FormatContext>
193bdd1243dSDimitry Andric   _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator
__format_as_sequencerange_formatter194bdd1243dSDimitry Andric   __format_as_sequence(_Rp&& __range, _FormatContext& __ctx) const {
195bdd1243dSDimitry Andric     __ctx.advance_to(ranges::copy(__opening_bracket_, __ctx.out()).out);
196bdd1243dSDimitry Andric     bool __use_separator = false;
197bdd1243dSDimitry Andric     for (auto&& __e : __range) {
198bdd1243dSDimitry Andric       if (__use_separator)
199bdd1243dSDimitry Andric         __ctx.advance_to(ranges::copy(__separator_, __ctx.out()).out);
200bdd1243dSDimitry Andric       else
201bdd1243dSDimitry Andric         __use_separator = true;
202bdd1243dSDimitry Andric 
203bdd1243dSDimitry Andric       __ctx.advance_to(__underlying_.format(__e, __ctx));
204bdd1243dSDimitry Andric     }
205bdd1243dSDimitry Andric 
206bdd1243dSDimitry Andric     return ranges::copy(__closing_bracket_, __ctx.out()).out;
207bdd1243dSDimitry Andric   }
208bdd1243dSDimitry Andric 
209bdd1243dSDimitry Andric   __format_spec::__parser<_CharT> __parser_{.__alignment_ = __format_spec::__alignment::__left};
210bdd1243dSDimitry Andric 
211bdd1243dSDimitry Andric private:
21206c3fb27SDimitry Andric   template <contiguous_iterator _Iterator>
__parse_typerange_formatter21306c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr void __parse_type(_Iterator& __begin, _Iterator __end) {
214bdd1243dSDimitry Andric     switch (*__begin) {
215bdd1243dSDimitry Andric     case _CharT('m'):
216bdd1243dSDimitry Andric       if constexpr (__fmt_pair_like<_Tp>) {
217bdd1243dSDimitry Andric         set_brackets(_LIBCPP_STATICALLY_WIDEN(_CharT, "{"), _LIBCPP_STATICALLY_WIDEN(_CharT, "}"));
218bdd1243dSDimitry Andric         set_separator(_LIBCPP_STATICALLY_WIDEN(_CharT, ", "));
219bdd1243dSDimitry Andric         ++__begin;
220bdd1243dSDimitry Andric       } else
22106c3fb27SDimitry Andric         std::__throw_format_error("Type m requires a pair or a tuple with two elements");
222bdd1243dSDimitry Andric       break;
223bdd1243dSDimitry Andric 
224bdd1243dSDimitry Andric     case _CharT('s'):
225bdd1243dSDimitry Andric       if constexpr (same_as<_Tp, _CharT>) {
226bdd1243dSDimitry Andric         __parser_.__type_ = __format_spec::__type::__string;
227bdd1243dSDimitry Andric         ++__begin;
228bdd1243dSDimitry Andric       } else
22906c3fb27SDimitry Andric         std::__throw_format_error("Type s requires character type as formatting argument");
230bdd1243dSDimitry Andric       break;
231bdd1243dSDimitry Andric 
232bdd1243dSDimitry Andric     case _CharT('?'):
233bdd1243dSDimitry Andric       ++__begin;
234bdd1243dSDimitry Andric       if (__begin == __end || *__begin != _CharT('s'))
23506c3fb27SDimitry Andric         std::__throw_format_error("The format specifier should consume the input or end with a '}'");
236bdd1243dSDimitry Andric       if constexpr (same_as<_Tp, _CharT>) {
237bdd1243dSDimitry Andric         __parser_.__type_ = __format_spec::__type::__debug;
238bdd1243dSDimitry Andric         ++__begin;
239bdd1243dSDimitry Andric       } else
24006c3fb27SDimitry Andric         std::__throw_format_error("Type ?s requires character type as formatting argument");
241bdd1243dSDimitry Andric     }
242bdd1243dSDimitry Andric   }
243bdd1243dSDimitry Andric 
24406c3fb27SDimitry Andric   template <class _ParseContext>
24506c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator
__parse_empty_range_underlying_specrange_formatter24606c3fb27SDimitry Andric   __parse_empty_range_underlying_spec(_ParseContext& __ctx, typename _ParseContext::iterator __begin) {
24706c3fb27SDimitry Andric     __ctx.advance_to(__begin);
24806c3fb27SDimitry Andric     [[maybe_unused]] typename _ParseContext::iterator __result = __underlying_.parse(__ctx);
249*1db9f3b2SDimitry Andric     _LIBCPP_ASSERT_INTERNAL(__result == __begin,
25006c3fb27SDimitry Andric                             "the underlying's parse function should not advance the input beyond the end of the input");
25106c3fb27SDimitry Andric     return __begin;
25206c3fb27SDimitry Andric   }
25306c3fb27SDimitry Andric 
254bdd1243dSDimitry Andric   formatter<_Tp, _CharT> __underlying_;
255bdd1243dSDimitry Andric   basic_string_view<_CharT> __separator_       = _LIBCPP_STATICALLY_WIDEN(_CharT, ", ");
256bdd1243dSDimitry Andric   basic_string_view<_CharT> __opening_bracket_ = _LIBCPP_STATICALLY_WIDEN(_CharT, "[");
257bdd1243dSDimitry Andric   basic_string_view<_CharT> __closing_bracket_ = _LIBCPP_STATICALLY_WIDEN(_CharT, "]");
258bdd1243dSDimitry Andric };
259bdd1243dSDimitry Andric 
26006c3fb27SDimitry Andric #endif //_LIBCPP_STD_VER >= 23
261bdd1243dSDimitry Andric 
262bdd1243dSDimitry Andric _LIBCPP_END_NAMESPACE_STD
263bdd1243dSDimitry Andric 
264bdd1243dSDimitry Andric #endif // _LIBCPP___FORMAT_RANGE_FORMATTER_H
265