File indexing completed on 2026-05-03 08:13:50
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef _LIBCPP___FORMAT_FORMATTER_OUTPUT_H
0011 #define _LIBCPP___FORMAT_FORMATTER_OUTPUT_H
0012
0013 #include <__algorithm/ranges_copy.h>
0014 #include <__algorithm/ranges_fill_n.h>
0015 #include <__algorithm/ranges_transform.h>
0016 #include <__bit/countl.h>
0017 #include <__concepts/same_as.h>
0018 #include <__config>
0019 #include <__cstddef/ptrdiff_t.h>
0020 #include <__cstddef/size_t.h>
0021 #include <__format/buffer.h>
0022 #include <__format/concepts.h>
0023 #include <__format/formatter.h>
0024 #include <__format/parser_std_format_spec.h>
0025 #include <__format/unicode.h>
0026 #include <__iterator/back_insert_iterator.h>
0027 #include <__iterator/concepts.h>
0028 #include <__iterator/iterator_traits.h>
0029 #include <__memory/addressof.h>
0030 #include <__memory/pointer_traits.h>
0031 #include <__utility/move.h>
0032 #include <__utility/unreachable.h>
0033 #include <string_view>
0034
0035 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0036 # pragma GCC system_header
0037 #endif
0038
0039 _LIBCPP_PUSH_MACROS
0040 #include <__undef_macros>
0041
0042 _LIBCPP_BEGIN_NAMESPACE_STD
0043
0044 #if _LIBCPP_STD_VER >= 20
0045
0046 namespace __formatter {
0047
0048 _LIBCPP_HIDE_FROM_ABI constexpr char __hex_to_upper(char __c) {
0049 switch (__c) {
0050 case 'a':
0051 return 'A';
0052 case 'b':
0053 return 'B';
0054 case 'c':
0055 return 'C';
0056 case 'd':
0057 return 'D';
0058 case 'e':
0059 return 'E';
0060 case 'f':
0061 return 'F';
0062 }
0063 return __c;
0064 }
0065
0066 struct _LIBCPP_EXPORTED_FROM_ABI __padding_size_result {
0067 size_t __before_;
0068 size_t __after_;
0069 };
0070
0071 _LIBCPP_HIDE_FROM_ABI constexpr __padding_size_result
0072 __padding_size(size_t __size, size_t __width, __format_spec::__alignment __align) {
0073 _LIBCPP_ASSERT_INTERNAL(__width > __size, "don't call this function when no padding is required");
0074 _LIBCPP_ASSERT_INTERNAL(
0075 __align != __format_spec::__alignment::__zero_padding, "the caller should have handled the zero-padding");
0076
0077 size_t __fill = __width - __size;
0078 switch (__align) {
0079 case __format_spec::__alignment::__zero_padding:
0080 __libcpp_unreachable();
0081
0082 case __format_spec::__alignment::__left:
0083 return {0, __fill};
0084
0085 case __format_spec::__alignment::__center: {
0086
0087
0088
0089 size_t __before = __fill / 2;
0090 size_t __after = __fill - __before;
0091 return {__before, __after};
0092 }
0093 case __format_spec::__alignment::__default:
0094 case __format_spec::__alignment::__right:
0095 return {__fill, 0};
0096 }
0097 __libcpp_unreachable();
0098 }
0099
0100
0101
0102
0103 template <__fmt_char_type _CharT, __fmt_char_type _OutCharT = _CharT>
0104 _LIBCPP_HIDE_FROM_ABI auto
0105 __copy(basic_string_view<_CharT> __str, output_iterator<const _OutCharT&> auto __out_it) -> decltype(__out_it) {
0106 if constexpr (std::same_as<decltype(__out_it), std::back_insert_iterator<__format::__output_buffer<_OutCharT>>>) {
0107 __out_it.__get_container()->__copy(__str);
0108 return __out_it;
0109 } else if constexpr (std::same_as<decltype(__out_it), typename __format::__retarget_buffer<_OutCharT>::__iterator>) {
0110 __out_it.__buffer_->__copy(__str);
0111 return __out_it;
0112 } else {
0113 return std::ranges::copy(__str, std::move(__out_it)).out;
0114 }
0115 }
0116
0117 template <contiguous_iterator _Iterator,
0118 __fmt_char_type _CharT = typename iterator_traits<_Iterator>::value_type,
0119 __fmt_char_type _OutCharT = _CharT>
0120 _LIBCPP_HIDE_FROM_ABI auto
0121 __copy(_Iterator __first, _Iterator __last, output_iterator<const _OutCharT&> auto __out_it) -> decltype(__out_it) {
0122 return __formatter::__copy(basic_string_view{__first, __last}, std::move(__out_it));
0123 }
0124
0125 template <contiguous_iterator _Iterator,
0126 __fmt_char_type _CharT = typename iterator_traits<_Iterator>::value_type,
0127 __fmt_char_type _OutCharT = _CharT>
0128 _LIBCPP_HIDE_FROM_ABI auto
0129 __copy(_Iterator __first, size_t __n, output_iterator<const _OutCharT&> auto __out_it) -> decltype(__out_it) {
0130 return __formatter::__copy(basic_string_view{std::to_address(__first), __n}, std::move(__out_it));
0131 }
0132
0133
0134
0135
0136 template <contiguous_iterator _Iterator,
0137 __fmt_char_type _CharT = typename iterator_traits<_Iterator>::value_type,
0138 __fmt_char_type _OutCharT = _CharT,
0139 class _UnaryOperation>
0140 _LIBCPP_HIDE_FROM_ABI auto
0141 __transform(_Iterator __first,
0142 _Iterator __last,
0143 output_iterator<const _OutCharT&> auto __out_it,
0144 _UnaryOperation __operation) -> decltype(__out_it) {
0145 if constexpr (std::same_as<decltype(__out_it), std::back_insert_iterator<__format::__output_buffer<_OutCharT>>>) {
0146 __out_it.__get_container()->__transform(__first, __last, std::move(__operation));
0147 return __out_it;
0148 } else if constexpr (std::same_as<decltype(__out_it), typename __format::__retarget_buffer<_OutCharT>::__iterator>) {
0149 __out_it.__buffer_->__transform(__first, __last, std::move(__operation));
0150 return __out_it;
0151 } else {
0152 return std::ranges::transform(__first, __last, std::move(__out_it), __operation).out;
0153 }
0154 }
0155
0156
0157
0158
0159 template <__fmt_char_type _CharT, output_iterator<const _CharT&> _OutIt>
0160 _LIBCPP_HIDE_FROM_ABI _OutIt __fill(_OutIt __out_it, size_t __n, _CharT __value) {
0161 if constexpr (std::same_as<decltype(__out_it), std::back_insert_iterator<__format::__output_buffer<_CharT>>>) {
0162 __out_it.__get_container()->__fill(__n, __value);
0163 return __out_it;
0164 } else if constexpr (std::same_as<decltype(__out_it), typename __format::__retarget_buffer<_CharT>::__iterator>) {
0165 __out_it.__buffer_->__fill(__n, __value);
0166 return __out_it;
0167 } else {
0168 return std::ranges::fill_n(std::move(__out_it), __n, __value);
0169 }
0170 }
0171
0172 # if _LIBCPP_HAS_UNICODE
0173 template <__fmt_char_type _CharT, output_iterator<const _CharT&> _OutIt>
0174 requires(same_as<_CharT, char>)
0175 _LIBCPP_HIDE_FROM_ABI _OutIt __fill(_OutIt __out_it, size_t __n, __format_spec::__code_point<_CharT> __value) {
0176 std::size_t __bytes = std::countl_one(static_cast<unsigned char>(__value.__data[0]));
0177 if (__bytes == 0)
0178 return __formatter::__fill(std::move(__out_it), __n, __value.__data[0]);
0179
0180 for (size_t __i = 0; __i < __n; ++__i)
0181 __out_it = __formatter::__copy(
0182 std::addressof(__value.__data[0]), std::addressof(__value.__data[0]) + __bytes, std::move(__out_it));
0183 return __out_it;
0184 }
0185
0186 # if _LIBCPP_HAS_WIDE_CHARACTERS
0187 template <__fmt_char_type _CharT, output_iterator<const _CharT&> _OutIt>
0188 requires(same_as<_CharT, wchar_t> && sizeof(wchar_t) == 2)
0189 _LIBCPP_HIDE_FROM_ABI _OutIt __fill(_OutIt __out_it, size_t __n, __format_spec::__code_point<_CharT> __value) {
0190 if (!__unicode::__is_high_surrogate(__value.__data[0]))
0191 return __formatter::__fill(std::move(__out_it), __n, __value.__data[0]);
0192
0193 for (size_t __i = 0; __i < __n; ++__i)
0194 __out_it = __formatter::__copy(
0195 std::addressof(__value.__data[0]), std::addressof(__value.__data[0]) + 2, std::move(__out_it));
0196 return __out_it;
0197 }
0198
0199 template <__fmt_char_type _CharT, output_iterator<const _CharT&> _OutIt>
0200 requires(same_as<_CharT, wchar_t> && sizeof(wchar_t) == 4)
0201 _LIBCPP_HIDE_FROM_ABI _OutIt __fill(_OutIt __out_it, size_t __n, __format_spec::__code_point<_CharT> __value) {
0202 return __formatter::__fill(std::move(__out_it), __n, __value.__data[0]);
0203 }
0204 # endif
0205 # else
0206 template <__fmt_char_type _CharT, output_iterator<const _CharT&> _OutIt>
0207 _LIBCPP_HIDE_FROM_ABI _OutIt __fill(_OutIt __out_it, size_t __n, __format_spec::__code_point<_CharT> __value) {
0208 return __formatter::__fill(std::move(__out_it), __n, __value.__data[0]);
0209 }
0210 # endif
0211
0212
0213
0214
0215
0216
0217
0218
0219
0220
0221
0222
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233 template <class _CharT, class _ParserCharT>
0234 _LIBCPP_HIDE_FROM_ABI auto
0235 __write(basic_string_view<_CharT> __str,
0236 output_iterator<const _CharT&> auto __out_it,
0237 __format_spec::__parsed_specifications<_ParserCharT> __specs,
0238 ptrdiff_t __size) -> decltype(__out_it) {
0239 if (__size >= __specs.__width_)
0240 return __formatter::__copy(__str, std::move(__out_it));
0241
0242 __padding_size_result __padding = __formatter::__padding_size(__size, __specs.__width_, __specs.__std_.__alignment_);
0243 __out_it = __formatter::__fill(std::move(__out_it), __padding.__before_, __specs.__fill_);
0244 __out_it = __formatter::__copy(__str, std::move(__out_it));
0245 return __formatter::__fill(std::move(__out_it), __padding.__after_, __specs.__fill_);
0246 }
0247
0248 template <contiguous_iterator _Iterator, class _ParserCharT>
0249 _LIBCPP_HIDE_FROM_ABI auto
0250 __write(_Iterator __first,
0251 _Iterator __last,
0252 output_iterator<const iter_value_t<_Iterator>&> auto __out_it,
0253 __format_spec::__parsed_specifications<_ParserCharT> __specs,
0254 ptrdiff_t __size) -> decltype(__out_it) {
0255 _LIBCPP_ASSERT_VALID_INPUT_RANGE(__first <= __last, "Not a valid range");
0256 return __formatter::__write(basic_string_view{__first, __last}, std::move(__out_it), __specs, __size);
0257 }
0258
0259
0260
0261
0262 template <contiguous_iterator _Iterator, class _ParserCharT>
0263 _LIBCPP_HIDE_FROM_ABI auto
0264 __write(_Iterator __first,
0265 _Iterator __last,
0266 output_iterator<const iter_value_t<_Iterator>&> auto __out_it,
0267 __format_spec::__parsed_specifications<_ParserCharT> __specs) -> decltype(__out_it) {
0268 _LIBCPP_ASSERT_VALID_INPUT_RANGE(__first <= __last, "Not a valid range");
0269 return __formatter::__write(__first, __last, std::move(__out_it), __specs, __last - __first);
0270 }
0271
0272 template <contiguous_iterator _Iterator,
0273 class _CharT = typename iterator_traits<_Iterator>::value_type,
0274 class _ParserCharT,
0275 class _UnaryOperation>
0276 _LIBCPP_HIDE_FROM_ABI auto __write_transformed(
0277 _Iterator __first,
0278 _Iterator __last,
0279 output_iterator<const _CharT&> auto __out_it,
0280 __format_spec::__parsed_specifications<_ParserCharT> __specs,
0281 _UnaryOperation __op) -> decltype(__out_it) {
0282 _LIBCPP_ASSERT_VALID_INPUT_RANGE(__first <= __last, "Not a valid range");
0283
0284 ptrdiff_t __size = __last - __first;
0285 if (__size >= __specs.__width_)
0286 return __formatter::__transform(__first, __last, std::move(__out_it), __op);
0287
0288 __padding_size_result __padding = __formatter::__padding_size(__size, __specs.__width_, __specs.__alignment_);
0289 __out_it = __formatter::__fill(std::move(__out_it), __padding.__before_, __specs.__fill_);
0290 __out_it = __formatter::__transform(__first, __last, std::move(__out_it), __op);
0291 return __formatter::__fill(std::move(__out_it), __padding.__after_, __specs.__fill_);
0292 }
0293
0294
0295
0296
0297
0298
0299 template <class _CharT>
0300 _LIBCPP_HIDE_FROM_ABI auto __write_string_no_precision(
0301 basic_string_view<_CharT> __str,
0302 output_iterator<const _CharT&> auto __out_it,
0303 __format_spec::__parsed_specifications<_CharT> __specs) -> decltype(__out_it) {
0304 _LIBCPP_ASSERT_INTERNAL(!__specs.__has_precision(), "use __write_string");
0305
0306
0307 if (!__specs.__has_width())
0308 return __formatter::__copy(__str, std::move(__out_it));
0309
0310
0311
0312
0313 size_t __size =
0314 __format_spec::__estimate_column_width(__str, __specs.__width_, __format_spec::__column_width_rounding::__up)
0315 .__width_;
0316 return __formatter::__write(__str, std::move(__out_it), __specs, __size);
0317 }
0318
0319 template <class _CharT>
0320 _LIBCPP_HIDE_FROM_ABI int __truncate(basic_string_view<_CharT>& __str, int __precision) {
0321 __format_spec::__column_width_result __result =
0322 __format_spec::__estimate_column_width(__str, __precision, __format_spec::__column_width_rounding::__down);
0323 __str = basic_string_view<_CharT>{__str.begin(), __result.__last_};
0324 return __result.__width_;
0325 }
0326
0327 }
0328
0329 #endif
0330
0331 _LIBCPP_END_NAMESPACE_STD
0332
0333 _LIBCPP_POP_MACROS
0334
0335 #endif