diff options
author | Dimitry Andric <dim@FreeBSD.org> | 2022-07-14 18:50:02 +0000 |
---|---|---|
committer | Dimitry Andric <dim@FreeBSD.org> | 2022-07-14 18:50:02 +0000 |
commit | 1f917f69ff07f09b6dbb670971f57f8efe718b84 (patch) | |
tree | 99293cbc1411737cd995dac10a99b2c40ef0944c /libcxx/include/__format/formatter_output.h | |
parent | 145449b1e420787bb99721a429341fa6be3adfb6 (diff) | |
download | src-1f917f69ff07f09b6dbb670971f57f8efe718b84.tar.gz src-1f917f69ff07f09b6dbb670971f57f8efe718b84.zip |
Vendor import of llvm-project main llvmorg-15-init-16436-g18a6ab5b8d1f.vendor/llvm-project/llvmorg-15-init-16436-g18a6ab5b8d1f
Diffstat (limited to 'libcxx/include/__format/formatter_output.h')
-rw-r--r-- | libcxx/include/__format/formatter_output.h | 59 |
1 files changed, 40 insertions, 19 deletions
diff --git a/libcxx/include/__format/formatter_output.h b/libcxx/include/__format/formatter_output.h index ab016f6f1610..c59cbbeeb5dd 100644 --- a/libcxx/include/__format/formatter_output.h +++ b/libcxx/include/__format/formatter_output.h @@ -33,8 +33,8 @@ _LIBCPP_BEGIN_NAMESPACE_STD namespace __formatter { -_LIBCPP_HIDE_FROM_ABI constexpr char __hex_to_upper(char c) { - switch (c) { +_LIBCPP_HIDE_FROM_ABI constexpr char __hex_to_upper(char __c) { + switch (__c) { case 'a': return 'A'; case 'b': @@ -48,27 +48,22 @@ _LIBCPP_HIDE_FROM_ABI constexpr char __hex_to_upper(char c) { case 'f': return 'F'; } - return c; + return __c; } -// TODO FMT remove _v2 suffix. -struct _LIBCPP_TYPE_VIS __padding_size_result_v2 { +struct _LIBCPP_TYPE_VIS __padding_size_result { size_t __before_; size_t __after_; }; -// TODO FMT remove _v2 suffix. -_LIBCPP_HIDE_FROM_ABI constexpr __padding_size_result_v2 __padding_size_v2(size_t __size, size_t __width, - __format_spec::__alignment __align) { +_LIBCPP_HIDE_FROM_ABI constexpr __padding_size_result +__padding_size(size_t __size, size_t __width, __format_spec::__alignment __align) { _LIBCPP_ASSERT(__width > __size, "don't call this function when no padding is required"); - _LIBCPP_ASSERT(__align != __format_spec::__alignment::__default, - "the caller should adjust the default to the value required by the type"); _LIBCPP_ASSERT(__align != __format_spec::__alignment::__zero_padding, "the caller should have handled the zero-padding"); size_t __fill = __width - __size; switch (__align) { - case __format_spec::__alignment::__default: case __format_spec::__alignment::__zero_padding: __libcpp_unreachable(); @@ -83,6 +78,7 @@ _LIBCPP_HIDE_FROM_ABI constexpr __padding_size_result_v2 __padding_size_v2(size_ size_t __after = __fill - __before; return {__before, __after}; } + case __format_spec::__alignment::__default: case __format_spec::__alignment::__right: return {__fill, 0}; } @@ -93,14 +89,11 @@ template <class _OutIt, class _CharT> _LIBCPP_HIDE_FROM_ABI _OutIt __write_using_decimal_separators(_OutIt __out_it, const char* __begin, const char* __first, const char* __last, string&& __grouping, _CharT __sep, __format_spec::__parsed_specifications<_CharT> __specs) { - _LIBCPP_ASSERT(__specs.__alignment_ != __format_spec::__alignment::__default, - "the caller should adjust the default to the value required by the type"); - int __size = (__first - __begin) + // [sign][prefix] (__last - __first) + // data (__grouping.size() - 1); // number of separator characters - __padding_size_result_v2 __padding = {0, 0}; + __padding_size_result __padding = {0, 0}; if (__specs.__alignment_ == __format_spec::__alignment::__zero_padding) { // Write [sign][prefix]. __out_it = _VSTD::copy(__begin, __first, _VSTD::move(__out_it)); @@ -113,7 +106,7 @@ _LIBCPP_HIDE_FROM_ABI _OutIt __write_using_decimal_separators(_OutIt __out_it, c } else { if (__specs.__width_ > __size) { // Determine padding and write padding. - __padding = __padding_size_v2(__size, __specs.__width_, __specs.__alignment_); + __padding = __padding_size(__size, __specs.__width_, __specs.__alignment_); __out_it = _VSTD::fill_n(_VSTD::move(__out_it), __padding.__before_, __specs.__fill_); } @@ -189,8 +182,7 @@ _LIBCPP_HIDE_FROM_ABI auto __write(const _CharT* __first, const _CharT* __last, if (__size >= __specs.__width_) return _VSTD::copy(__first, __last, _VSTD::move(__out_it)); - __padding_size_result_v2 __padding = - __formatter::__padding_size_v2(__size, __specs.__width_, __specs.__std_.__alignment_); + __padding_size_result __padding = __formatter::__padding_size(__size, __specs.__width_, __specs.__std_.__alignment_); __out_it = _VSTD::fill_n(_VSTD::move(__out_it), __padding.__before_, __specs.__fill_); __out_it = _VSTD::copy(__first, __last, _VSTD::move(__out_it)); return _VSTD::fill_n(_VSTD::move(__out_it), __padding.__after_, __specs.__fill_); @@ -216,12 +208,41 @@ _LIBCPP_HIDE_FROM_ABI auto __write_transformed(const _CharT* __first, const _Cha if (__size >= __specs.__width_) return _VSTD::transform(__first, __last, _VSTD::move(__out_it), __op); - __padding_size_result_v2 __padding = __padding_size_v2(__size, __specs.__width_, __specs.__alignment_); + __padding_size_result __padding = __padding_size(__size, __specs.__width_, __specs.__alignment_); __out_it = _VSTD::fill_n(_VSTD::move(__out_it), __padding.__before_, __specs.__fill_); __out_it = _VSTD::transform(__first, __last, _VSTD::move(__out_it), __op); return _VSTD::fill_n(_VSTD::move(__out_it), __padding.__after_, __specs.__fill_); } +/// Writes additional zero's for the precision before the exponent. +/// This is used when the precision requested in the format string is larger +/// than the maximum precision of the floating-point type. These precision +/// digits are always 0. +/// +/// \param __exponent The location of the exponent character. +/// \param __num_trailing_zeros The number of 0's to write before the exponent +/// character. +template <class _CharT, class _ParserCharT> +_LIBCPP_HIDE_FROM_ABI auto __write_using_trailing_zeros( + const _CharT* __first, + const _CharT* __last, + output_iterator<const _CharT&> auto __out_it, + __format_spec::__parsed_specifications<_ParserCharT> __specs, + size_t __size, + const _CharT* __exponent, + size_t __num_trailing_zeros) -> decltype(__out_it) { + _LIBCPP_ASSERT(__first <= __last, "Not a valid range"); + _LIBCPP_ASSERT(__num_trailing_zeros > 0, "The overload not writing trailing zeros should have been used"); + + __padding_size_result __padding = + __padding_size(__size + __num_trailing_zeros, __specs.__width_, __specs.__alignment_); + __out_it = _VSTD::fill_n(_VSTD::move(__out_it), __padding.__before_, __specs.__fill_); + __out_it = _VSTD::copy(__first, __exponent, _VSTD::move(__out_it)); + __out_it = _VSTD::fill_n(_VSTD::move(__out_it), __num_trailing_zeros, _CharT('0')); + __out_it = _VSTD::copy(__exponent, __last, _VSTD::move(__out_it)); + return _VSTD::fill_n(_VSTD::move(__out_it), __padding.__after_, __specs.__fill_); +} + # ifndef _LIBCPP_HAS_NO_UNICODE template <class _CharT> _LIBCPP_HIDE_FROM_ABI auto __write_unicode_no_precision(basic_string_view<_CharT> __str, |