Skip to content

Commit

Permalink
[libc++[format][NFC] Removes dead code.
Browse files Browse the repository at this point in the history
This removes a part of the now obsolete formater code.
The removal also removes the _v2 suffix where it's no longer needed.

Depends on D128785

Reviewed By: #libc, ldionne

Differential Revision: https://reviews.llvm.org/D128846
  • Loading branch information
mordante committed Jul 7, 2022
1 parent 152d922 commit 207e7e4
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 1,079 deletions.
227 changes: 0 additions & 227 deletions libcxx/include/__format/formatter.h
Expand Up @@ -10,20 +10,10 @@
#ifndef _LIBCPP___FORMAT_FORMATTER_H
#define _LIBCPP___FORMAT_FORMATTER_H

#include <__algorithm/copy.h>
#include <__algorithm/fill_n.h>
#include <__algorithm/transform.h>
#include <__assert>
#include <__availability>
#include <__concepts/same_as.h>
#include <__config>
#include <__format/format_error.h>
#include <__format/format_fwd.h>
#include <__format/format_string.h>
#include <__format/parser_std_format_spec.h>
#include <__utility/move.h>
#include <__utility/unreachable.h>
#include <string_view>

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
Expand All @@ -49,229 +39,12 @@ struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter {
formatter& operator=(const formatter&) = delete;
};

namespace __format_spec {

_LIBCPP_HIDE_FROM_ABI inline char* __insert_sign(char* __buf, bool __negative,
_Flags::_Sign __sign) {
if (__negative)
*__buf++ = '-';
else
switch (__sign) {
case _Flags::_Sign::__default:
case _Flags::_Sign::__minus:
// No sign added.
break;
case _Flags::_Sign::__plus:
*__buf++ = '+';
break;
case _Flags::_Sign::__space:
*__buf++ = ' ';
break;
}

return __buf;
}

_LIBCPP_HIDE_FROM_ABI constexpr char __hex_to_upper(char c) {
switch (c) {
case 'a':
return 'A';
case 'b':
return 'B';
case 'c':
return 'C';
case 'd':
return 'D';
case 'e':
return 'E';
case 'f':
return 'F';
}
return c;
}

} // namespace __format_spec

namespace __formatter {

/** The character types that formatters are specialized for. */
template <class _CharT>
concept __char_type = same_as<_CharT, char> || same_as<_CharT, wchar_t>;

struct _LIBCPP_TEMPLATE_VIS __padding_size_result {
size_t __before;
size_t __after;
};

_LIBCPP_HIDE_FROM_ABI constexpr __padding_size_result
__padding_size(size_t __size, size_t __width,
__format_spec::_Flags::_Alignment __align) {
_LIBCPP_ASSERT(__width > __size,
"Don't call this function when no padding is required");
_LIBCPP_ASSERT(
__align != __format_spec::_Flags::_Alignment::__default,
"Caller should adjust the default to the value required by the type");

size_t __fill = __width - __size;
switch (__align) {
case __format_spec::_Flags::_Alignment::__default:
__libcpp_unreachable();

case __format_spec::_Flags::_Alignment::__left:
return {0, __fill};

case __format_spec::_Flags::_Alignment::__center: {
// The extra padding is divided per [format.string.std]/3
// __before = floor(__fill, 2);
// __after = ceil(__fill, 2);
size_t __before = __fill / 2;
size_t __after = __fill - __before;
return {__before, __after};
}
case __format_spec::_Flags::_Alignment::__right:
return {__fill, 0};
}
__libcpp_unreachable();
}

/**
* Writes the input to the output with the required padding.
*
* Since the output column width is specified the function can be used for
* ASCII and Unicode input.
*
* @pre [@a __first, @a __last) is a valid range.
* @pre @a __size <= @a __width. Using this function when this pre-condition
* doesn't hold incurs an unwanted overhead.
*
* @param __out_it The output iterator to write to.
* @param __first Pointer to the first element to write.
* @param __last Pointer beyond the last element to write.
* @param __size The (estimated) output column width. When the elements
* to be written are ASCII the following condition holds
* @a __size == @a __last - @a __first.
* @param __width The number of output columns to write.
* @param __fill The character used for the alignment of the output.
* TODO FMT Will probably change to support Unicode grapheme
* cluster.
* @param __alignment The requested alignment.
*
* @returns An iterator pointing beyond the last element written.
*
* @note The type of the elements in range [@a __first, @a __last) can differ
* from the type of @a __fill. Integer output uses @c std::to_chars for its
* conversion, which means the [@a __first, @a __last) always contains elements
* of the type @c char.
*/
template <class _CharT, class _Fill>
_LIBCPP_HIDE_FROM_ABI auto
__write(output_iterator<const _CharT&> auto __out_it, const _CharT* __first,
const _CharT* __last, size_t __size, size_t __width, _Fill __fill,
__format_spec::_Flags::_Alignment __alignment) -> decltype(__out_it) {

_LIBCPP_ASSERT(__first <= __last, "Not a valid range");
_LIBCPP_ASSERT(__size < __width, "Precondition failure");

__padding_size_result __padding =
__padding_size(__size, __width, __alignment);
__out_it = _VSTD::fill_n(_VSTD::move(__out_it), __padding.__before, __fill);
__out_it = _VSTD::copy(__first, __last, _VSTD::move(__out_it));
return _VSTD::fill_n(_VSTD::move(__out_it), __padding.__after, __fill);
}

/**
* @overload
*
* 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 _Fill>
_LIBCPP_HIDE_FROM_ABI auto __write(output_iterator<const _CharT&> auto __out_it, const _CharT* __first,
const _CharT* __last, size_t __size, size_t __width, _Fill __fill,
__format_spec::_Flags::_Alignment __alignment, 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, __width, __alignment);
__out_it = _VSTD::fill_n(_VSTD::move(__out_it), __padding.__before, __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, __fill);
}

/**
* @overload
*
* Uses a transformation operation before writing an element.
*
* TODO FMT Fill will probably change to support Unicode grapheme cluster.
*/
template <class _CharT, class _UnaryOperation, class _Fill>
_LIBCPP_HIDE_FROM_ABI auto
__write(output_iterator<const _CharT&> auto __out_it, const _CharT* __first,
const _CharT* __last, size_t __size, _UnaryOperation __op,
size_t __width, _Fill __fill,
__format_spec::_Flags::_Alignment __alignment) -> decltype(__out_it) {

_LIBCPP_ASSERT(__first <= __last, "Not a valid range");
_LIBCPP_ASSERT(__size < __width, "Precondition failure");

__padding_size_result __padding =
__padding_size(__size, __width, __alignment);
__out_it = _VSTD::fill_n(_VSTD::move(__out_it), __padding.__before, __fill);
__out_it = _VSTD::transform(__first, __last, _VSTD::move(__out_it), __op);
return _VSTD::fill_n(_VSTD::move(__out_it), __padding.__after, __fill);
}

/**
* Writes Unicode input to the output with the required padding.
*
* This function does almost the same as the @ref __write function, but handles
* the width estimation of the Unicode input.
*
* @param __str The range [@a __first, @a __last).
* @param __precision The width to truncate the input string to, use @c -1 for
* no limit.
*/
template <class _CharT, class _Fill>
_LIBCPP_HIDE_FROM_ABI auto
__write_unicode(output_iterator<const _CharT&> auto __out_it,
basic_string_view<_CharT> __str, ptrdiff_t __width,
ptrdiff_t __precision, _Fill __fill,
__format_spec::_Flags::_Alignment __alignment)
-> decltype(__out_it) {

// This value changes when there Unicode column width limits the output
// size.
auto __last = __str.end();
if (__width != 0 || __precision != -1) {
__format_spec::__string_alignment<_CharT> __format_traits =
__format_spec::__get_string_alignment(__str.begin(), __str.end(),
__width, __precision);

if (__format_traits.__align)
return __write(_VSTD::move(__out_it), __str.begin(),
__format_traits.__last, __format_traits.__size, __width,
__fill, __alignment);

// No alignment required update the output based on the precision.
// This might be the same as __str.end().
__last = __format_traits.__last;
}

// Copy the input to the output. The output size might be limited by the
// precision.
return _VSTD::copy(__str.begin(), __last, _VSTD::move(__out_it));
}

} // namespace __formatter

#endif //_LIBCPP_STD_VER > 17
Expand Down
4 changes: 2 additions & 2 deletions libcxx/include/__format/formatter_floating_point.h
Expand Up @@ -514,15 +514,15 @@ _LIBCPP_HIDE_FROM_ABI _OutIt __format_locale_specific_form(
__grouping.size() - // Grouping contains one
!__grouping.empty(); // additional character

__formatter::__padding_size_result_v2 __padding = {0, 0};
__formatter::__padding_size_result __padding = {0, 0};
bool __zero_padding = __specs.__alignment_ == __format_spec::__alignment::__zero_padding;
if (__size < __specs.__width_) {
if (__zero_padding) {
__specs.__alignment_ = __format_spec::__alignment::__right;
__specs.__fill_ = _CharT('0');
}

__padding = __formatter::__padding_size_v2(__size, __specs.__width_, __specs.__alignment_);
__padding = __formatter::__padding_size(__size, __specs.__width_, __specs.__alignment_);
}

// sign and (zero padding or alignment)
Expand Down
21 changes: 9 additions & 12 deletions libcxx/include/__format/formatter_output.h
Expand Up @@ -51,15 +51,13 @@ _LIBCPP_HIDE_FROM_ABI constexpr char __hex_to_upper(char 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");
Expand Down Expand Up @@ -100,7 +98,7 @@ _LIBCPP_HIDE_FROM_ABI _OutIt __write_using_decimal_separators(_OutIt __out_it, c
(__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));
Expand All @@ -113,7 +111,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_);
}
Expand Down Expand Up @@ -189,8 +187,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_);
Expand All @@ -216,7 +213,7 @@ _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_);
Expand All @@ -242,8 +239,8 @@ _LIBCPP_HIDE_FROM_ABI auto __write_using_trailing_zeros(
_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_v2 __padding =
__padding_size_v2(__size + __num_trailing_zeros, __specs.__width_, __specs.__alignment_);
__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'));
Expand Down

0 comments on commit 207e7e4

Please sign in to comment.