1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef _LIBCPP___FORMAT_FORMATTER_OUTPUT_H
11 #define _LIBCPP___FORMAT_FORMATTER_OUTPUT_H
12
13 #include <__algorithm/ranges_copy.h>
14 #include <__algorithm/ranges_fill_n.h>
15 #include <__algorithm/ranges_transform.h>
16 #include <__bit/countl.h>
17 #include <__concepts/same_as.h>
18 #include <__config>
19 #include <__cstddef/ptrdiff_t.h>
20 #include <__cstddef/size_t.h>
21 #include <__format/buffer.h>
22 #include <__format/concepts.h>
23 #include <__format/formatter.h>
24 #include <__format/parser_std_format_spec.h>
25 #include <__format/unicode.h>
26 #include <__iterator/back_insert_iterator.h>
27 #include <__iterator/concepts.h>
28 #include <__iterator/iterator_traits.h>
29 #include <__memory/addressof.h>
30 #include <__memory/pointer_traits.h>
31 #include <__utility/move.h>
32 #include <__utility/unreachable.h>
33 #include <string_view>
34
35 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
36 # pragma GCC system_header
37 #endif
38
39 _LIBCPP_PUSH_MACROS
40 #include <__undef_macros>
41
42 _LIBCPP_BEGIN_NAMESPACE_STD
43
44 #if _LIBCPP_STD_VER >= 20
45
46 namespace __formatter {
47
48 struct _LIBCPP_EXPORTED_FROM_ABI __padding_size_result {
49 size_t __before_;
50 size_t __after_;
51 };
52
53 _LIBCPP_HIDE_FROM_ABI constexpr __padding_size_result
__padding_size(size_t __size,size_t __width,__format_spec::__alignment __align)54 __padding_size(size_t __size, size_t __width, __format_spec::__alignment __align) {
55 _LIBCPP_ASSERT_INTERNAL(__width > __size, "don't call this function when no padding is required");
56 _LIBCPP_ASSERT_INTERNAL(
57 __align != __format_spec::__alignment::__zero_padding, "the caller should have handled the zero-padding");
58
59 size_t __fill = __width - __size;
60 switch (__align) {
61 case __format_spec::__alignment::__zero_padding:
62 __libcpp_unreachable();
63
64 case __format_spec::__alignment::__left:
65 return {0, __fill};
66
67 case __format_spec::__alignment::__center: {
68 // The extra padding is divided per [format.string.std]/3
69 // __before = floor(__fill, 2);
70 // __after = ceil(__fill, 2);
71 size_t __before = __fill / 2;
72 size_t __after = __fill - __before;
73 return {__before, __after};
74 }
75 case __format_spec::__alignment::__default:
76 case __format_spec::__alignment::__right:
77 return {__fill, 0};
78 }
79 __libcpp_unreachable();
80 }
81
82 /// Copy wrapper.
83 ///
84 /// This uses a "mass output function" of __format::__output_buffer when possible.
85 template <__fmt_char_type _CharT, __fmt_char_type _OutCharT = _CharT>
86 _LIBCPP_HIDE_FROM_ABI auto
87 __copy(basic_string_view<_CharT> __str, output_iterator<const _OutCharT&> auto __out_it) -> decltype(__out_it) {
88 if constexpr (std::same_as<decltype(__out_it), std::back_insert_iterator<__format::__output_buffer<_OutCharT>>>) {
89 __out_it.__get_container()->__copy(__str);
90 return __out_it;
91 } else if constexpr (std::same_as<decltype(__out_it), typename __format::__retarget_buffer<_OutCharT>::__iterator>) {
92 __out_it.__buffer_->__copy(__str);
93 return __out_it;
94 } else {
95 return std::ranges::copy(__str, std::move(__out_it)).out;
96 }
97 }
98
99 template <contiguous_iterator _Iterator,
100 __fmt_char_type _CharT = typename iterator_traits<_Iterator>::value_type,
101 __fmt_char_type _OutCharT = _CharT>
102 _LIBCPP_HIDE_FROM_ABI auto
103 __copy(_Iterator __first, _Iterator __last, output_iterator<const _OutCharT&> auto __out_it) -> decltype(__out_it) {
104 return __formatter::__copy(basic_string_view{__first, __last}, std::move(__out_it));
105 }
106
107 template <contiguous_iterator _Iterator,
108 __fmt_char_type _CharT = typename iterator_traits<_Iterator>::value_type,
109 __fmt_char_type _OutCharT = _CharT>
110 _LIBCPP_HIDE_FROM_ABI auto
111 __copy(_Iterator __first, size_t __n, output_iterator<const _OutCharT&> auto __out_it) -> decltype(__out_it) {
112 return __formatter::__copy(basic_string_view{std::to_address(__first), __n}, std::move(__out_it));
113 }
114
115 /// Transform wrapper.
116 ///
117 /// This uses a "mass output function" of __format::__output_buffer when possible.
118 template <contiguous_iterator _Iterator,
119 __fmt_char_type _CharT = typename iterator_traits<_Iterator>::value_type,
120 __fmt_char_type _OutCharT = _CharT,
121 class _UnaryOperation>
122 _LIBCPP_HIDE_FROM_ABI auto
123 __transform(_Iterator __first,
124 _Iterator __last,
125 output_iterator<const _OutCharT&> auto __out_it,
126 _UnaryOperation __operation) -> decltype(__out_it) {
127 if constexpr (std::same_as<decltype(__out_it), std::back_insert_iterator<__format::__output_buffer<_OutCharT>>>) {
128 __out_it.__get_container()->__transform(__first, __last, std::move(__operation));
129 return __out_it;
130 } else if constexpr (std::same_as<decltype(__out_it), typename __format::__retarget_buffer<_OutCharT>::__iterator>) {
131 __out_it.__buffer_->__transform(__first, __last, std::move(__operation));
132 return __out_it;
133 } else {
134 return std::ranges::transform(__first, __last, std::move(__out_it), __operation).out;
135 }
136 }
137
138 /// Fill wrapper.
139 ///
140 /// This uses a "mass output function" of __format::__output_buffer when possible.
141 template <__fmt_char_type _CharT, output_iterator<const _CharT&> _OutIt>
__fill(_OutIt __out_it,size_t __n,_CharT __value)142 _LIBCPP_HIDE_FROM_ABI _OutIt __fill(_OutIt __out_it, size_t __n, _CharT __value) {
143 if constexpr (std::same_as<decltype(__out_it), std::back_insert_iterator<__format::__output_buffer<_CharT>>>) {
144 __out_it.__get_container()->__fill(__n, __value);
145 return __out_it;
146 } else if constexpr (std::same_as<decltype(__out_it), typename __format::__retarget_buffer<_CharT>::__iterator>) {
147 __out_it.__buffer_->__fill(__n, __value);
148 return __out_it;
149 } else {
150 return std::ranges::fill_n(std::move(__out_it), __n, __value);
151 }
152 }
153
154 # if _LIBCPP_HAS_UNICODE
155 template <__fmt_char_type _CharT, output_iterator<const _CharT&> _OutIt>
requires(same_as<_CharT,char>)156 requires(same_as<_CharT, char>)
157 _LIBCPP_HIDE_FROM_ABI _OutIt __fill(_OutIt __out_it, size_t __n, __format_spec::__code_point<_CharT> __value) {
158 std::size_t __bytes = std::countl_one(static_cast<unsigned char>(__value.__data[0]));
159 if (__bytes == 0)
160 return __formatter::__fill(std::move(__out_it), __n, __value.__data[0]);
161
162 for (size_t __i = 0; __i < __n; ++__i)
163 __out_it = __formatter::__copy(
164 std::addressof(__value.__data[0]), std::addressof(__value.__data[0]) + __bytes, std::move(__out_it));
165 return __out_it;
166 }
167
168 # if _LIBCPP_HAS_WIDE_CHARACTERS
169 template <__fmt_char_type _CharT, output_iterator<const _CharT&> _OutIt>
170 requires(same_as<_CharT, wchar_t> && sizeof(wchar_t) == 2)
__fill(_OutIt __out_it,size_t __n,__format_spec::__code_point<_CharT> __value)171 _LIBCPP_HIDE_FROM_ABI _OutIt __fill(_OutIt __out_it, size_t __n, __format_spec::__code_point<_CharT> __value) {
172 if (!__unicode::__is_high_surrogate(__value.__data[0]))
173 return __formatter::__fill(std::move(__out_it), __n, __value.__data[0]);
174
175 for (size_t __i = 0; __i < __n; ++__i)
176 __out_it = __formatter::__copy(
177 std::addressof(__value.__data[0]), std::addressof(__value.__data[0]) + 2, std::move(__out_it));
178 return __out_it;
179 }
180
181 template <__fmt_char_type _CharT, output_iterator<const _CharT&> _OutIt>
182 requires(same_as<_CharT, wchar_t> && sizeof(wchar_t) == 4)
__fill(_OutIt __out_it,size_t __n,__format_spec::__code_point<_CharT> __value)183 _LIBCPP_HIDE_FROM_ABI _OutIt __fill(_OutIt __out_it, size_t __n, __format_spec::__code_point<_CharT> __value) {
184 return __formatter::__fill(std::move(__out_it), __n, __value.__data[0]);
185 }
186 # endif // _LIBCPP_HAS_WIDE_CHARACTERS
187 # else // _LIBCPP_HAS_UNICODE
188 template <__fmt_char_type _CharT, output_iterator<const _CharT&> _OutIt>
__fill(_OutIt __out_it,size_t __n,__format_spec::__code_point<_CharT> __value)189 _LIBCPP_HIDE_FROM_ABI _OutIt __fill(_OutIt __out_it, size_t __n, __format_spec::__code_point<_CharT> __value) {
190 return __formatter::__fill(std::move(__out_it), __n, __value.__data[0]);
191 }
192 # endif // _LIBCPP_HAS_UNICODE
193
194 /// Writes the input to the output with the required padding.
195 ///
196 /// Since the output column width is specified the function can be used for
197 /// ASCII and Unicode output.
198 ///
199 /// \pre \a __size <= \a __width. Using this function when this pre-condition
200 /// doesn't hold incurs an unwanted overhead.
201 ///
202 /// \param __str The string to write.
203 /// \param __out_it The output iterator to write to.
204 /// \param __specs The parsed formatting specifications.
205 /// \param __size The (estimated) output column width. When the elements
206 /// to be written are ASCII the following condition holds
207 /// \a __size == \a __last - \a __first.
208 ///
209 /// \returns An iterator pointing beyond the last element written.
210 ///
211 /// \note The type of the elements in range [\a __first, \a __last) can differ
212 /// from the type of \a __specs. Integer output uses \c std::to_chars for its
213 /// conversion, which means the [\a __first, \a __last) always contains elements
214 /// of the type \c char.
215 template <class _CharT, class _ParserCharT>
216 _LIBCPP_HIDE_FROM_ABI auto
217 __write(basic_string_view<_CharT> __str,
218 output_iterator<const _CharT&> auto __out_it,
219 __format_spec::__parsed_specifications<_ParserCharT> __specs,
220 ptrdiff_t __size) -> decltype(__out_it) {
221 if (__size >= __specs.__width_)
222 return __formatter::__copy(__str, std::move(__out_it));
223
224 __padding_size_result __padding = __formatter::__padding_size(__size, __specs.__width_, __specs.__std_.__alignment_);
225 __out_it = __formatter::__fill(std::move(__out_it), __padding.__before_, __specs.__fill_);
226 __out_it = __formatter::__copy(__str, std::move(__out_it));
227 return __formatter::__fill(std::move(__out_it), __padding.__after_, __specs.__fill_);
228 }
229
230 template <contiguous_iterator _Iterator, class _ParserCharT>
231 _LIBCPP_HIDE_FROM_ABI auto
232 __write(_Iterator __first,
233 _Iterator __last,
234 output_iterator<const iter_value_t<_Iterator>&> auto __out_it,
235 __format_spec::__parsed_specifications<_ParserCharT> __specs,
236 ptrdiff_t __size) -> decltype(__out_it) {
237 _LIBCPP_ASSERT_VALID_INPUT_RANGE(__first <= __last, "Not a valid range");
238 return __formatter::__write(basic_string_view{__first, __last}, std::move(__out_it), __specs, __size);
239 }
240
241 /// \overload
242 ///
243 /// Calls the function above where \a __size = \a __last - \a __first.
244 template <contiguous_iterator _Iterator, class _ParserCharT>
245 _LIBCPP_HIDE_FROM_ABI auto
246 __write(_Iterator __first,
247 _Iterator __last,
248 output_iterator<const iter_value_t<_Iterator>&> auto __out_it,
249 __format_spec::__parsed_specifications<_ParserCharT> __specs) -> decltype(__out_it) {
250 _LIBCPP_ASSERT_VALID_INPUT_RANGE(__first <= __last, "Not a valid range");
251 return __formatter::__write(__first, __last, std::move(__out_it), __specs, __last - __first);
252 }
253
254 template <contiguous_iterator _Iterator,
255 class _CharT = typename iterator_traits<_Iterator>::value_type,
256 class _ParserCharT,
257 class _UnaryOperation>
258 _LIBCPP_HIDE_FROM_ABI auto __write_transformed(
259 _Iterator __first,
260 _Iterator __last,
261 output_iterator<const _CharT&> auto __out_it,
262 __format_spec::__parsed_specifications<_ParserCharT> __specs,
263 _UnaryOperation __op) -> decltype(__out_it) {
264 _LIBCPP_ASSERT_VALID_INPUT_RANGE(__first <= __last, "Not a valid range");
265
266 ptrdiff_t __size = __last - __first;
267 if (__size >= __specs.__width_)
268 return __formatter::__transform(__first, __last, std::move(__out_it), __op);
269
270 __padding_size_result __padding = __formatter::__padding_size(__size, __specs.__width_, __specs.__alignment_);
271 __out_it = __formatter::__fill(std::move(__out_it), __padding.__before_, __specs.__fill_);
272 __out_it = __formatter::__transform(__first, __last, std::move(__out_it), __op);
273 return __formatter::__fill(std::move(__out_it), __padding.__after_, __specs.__fill_);
274 }
275
276 /// Writes a string using format's width estimation algorithm.
277 ///
278 /// \pre !__specs.__has_precision()
279 ///
280 /// \note When \c _LIBCPP_HAS_UNICODE is false the function assumes the input is ASCII.
281 template <class _CharT>
282 _LIBCPP_HIDE_FROM_ABI auto __write_string_no_precision(
283 basic_string_view<_CharT> __str,
284 output_iterator<const _CharT&> auto __out_it,
285 __format_spec::__parsed_specifications<_CharT> __specs) -> decltype(__out_it) {
286 _LIBCPP_ASSERT_INTERNAL(!__specs.__has_precision(), "use __write_string");
287
288 // No padding -> copy the string
289 if (!__specs.__has_width())
290 return __formatter::__copy(__str, std::move(__out_it));
291
292 // Note when the estimated width is larger than size there's no padding. So
293 // there's no reason to get the real size when the estimate is larger than or
294 // equal to the minimum field width.
295 size_t __size =
296 __format_spec::__estimate_column_width(__str, __specs.__width_, __format_spec::__column_width_rounding::__up)
297 .__width_;
298 return __formatter::__write(__str, std::move(__out_it), __specs, __size);
299 }
300
301 template <class _CharT>
__truncate(basic_string_view<_CharT> & __str,int __precision)302 _LIBCPP_HIDE_FROM_ABI int __truncate(basic_string_view<_CharT>& __str, int __precision) {
303 __format_spec::__column_width_result __result =
304 __format_spec::__estimate_column_width(__str, __precision, __format_spec::__column_width_rounding::__down);
305 __str = basic_string_view<_CharT>{__str.begin(), __result.__last_};
306 return __result.__width_;
307 }
308
309 } // namespace __formatter
310
311 #endif // _LIBCPP_STD_VER >= 20
312
313 _LIBCPP_END_NAMESPACE_STD
314
315 _LIBCPP_POP_MACROS
316
317 #endif // _LIBCPP___FORMAT_FORMATTER_OUTPUT_H
318