[libc++][format] Copy code to new location.

This is a helper patch to ease the reviewing of D128139.
The originals will be removed at a later time when all formatters are
converted to the new style. (Floating-point and pointer aren't up for
review yet.)

Reviewed By: #libc, ldionne

Differential Revision: https://reviews.llvm.org/D128367
This commit is contained in:
Mark de Wever 2021-12-28 18:48:04 +01:00
parent 316e106f49
commit 9afaa158f5
1 changed files with 116 additions and 0 deletions

View File

@ -11,12 +11,16 @@
#define _LIBCPP___FORMAT_FORMATTER_OUTPUT_H #define _LIBCPP___FORMAT_FORMATTER_OUTPUT_H
#include <__algorithm/copy.h> #include <__algorithm/copy.h>
#include <__algorithm/copy_n.h>
#include <__algorithm/fill_n.h> #include <__algorithm/fill_n.h>
#include <__algorithm/transform.h>
#include <__config> #include <__config>
#include <__format/formatter.h>
#include <__format/parser_std_format_spec.h> #include <__format/parser_std_format_spec.h>
#include <__utility/move.h> #include <__utility/move.h>
#include <__utility/unreachable.h> #include <__utility/unreachable.h>
#include <cstddef> #include <cstddef>
#include <string>
#include <string_view> #include <string_view>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
@ -29,6 +33,24 @@ _LIBCPP_BEGIN_NAMESPACE_STD
namespace __formatter { namespace __formatter {
_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;
}
// TODO FMT remove _v2 suffix. // TODO FMT remove _v2 suffix.
struct _LIBCPP_TYPE_VIS __padding_size_result_v2 { struct _LIBCPP_TYPE_VIS __padding_size_result_v2 {
size_t __before_; size_t __before_;
@ -67,6 +89,73 @@ _LIBCPP_HIDE_FROM_ABI constexpr __padding_size_result_v2 __padding_size_v2(size_
__libcpp_unreachable(); __libcpp_unreachable();
} }
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};
if (__specs.__alignment_ == __format_spec::__alignment::__zero_padding) {
// Write [sign][prefix].
__out_it = _VSTD::copy(__begin, __first, _VSTD::move(__out_it));
if (__specs.__width_ > __size) {
// Write zero padding.
__padding.__before_ = __specs.__width_ - __size;
__out_it = _VSTD::fill_n(_VSTD::move(__out_it), __specs.__width_ - __size, _CharT('0'));
}
} else {
if (__specs.__width_ > __size) {
// Determine padding and write padding.
__padding = __padding_size_v2(__size, __specs.__width_, __specs.__alignment_);
__out_it = _VSTD::fill_n(_VSTD::move(__out_it), __padding.__before_, __specs.__fill_);
}
// Write [sign][prefix].
__out_it = _VSTD::copy(__begin, __first, _VSTD::move(__out_it));
}
auto __r = __grouping.rbegin();
auto __e = __grouping.rend() - 1;
_LIBCPP_ASSERT(__r != __e, "The slow grouping formatting is used while "
"there will be no separators written.");
// The output is divided in small groups of numbers to write:
// - A group before the first separator.
// - A separator and a group, repeated for the number of separators.
// - A group after the last separator.
// This loop achieves that process by testing the termination condition
// midway in the loop.
//
// TODO FMT This loop evaluates the loop invariant `__parser.__type !=
// _Flags::_Type::__hexadecimal_upper_case` for every iteration. (This test
// happens in the __write call.) Benchmark whether making two loops and
// hoisting the invariant is worth the effort.
while (true) {
if (__specs.__std_.__type_ == __format_spec::__type::__hexadecimal_upper_case) {
__last = __first + *__r;
__out_it = _VSTD::transform(__first, __last, _VSTD::move(__out_it), __hex_to_upper);
__first = __last;
} else {
__out_it = _VSTD::copy_n(__first, *__r, _VSTD::move(__out_it));
__first += *__r;
}
if (__r == __e)
break;
++__r;
*__out_it++ = __sep;
}
return _VSTD::fill_n(_VSTD::move(__out_it), __padding.__after_, __specs.__fill_);
}
/// Writes the input to the output with the required padding. /// Writes the input to the output with the required padding.
/// ///
/// Since the output column width is specified the function can be used for /// Since the output column width is specified the function can be used for
@ -107,12 +196,39 @@ _LIBCPP_HIDE_FROM_ABI auto __write(const _CharT* __first, const _CharT* __last,
return _VSTD::fill_n(_VSTD::move(__out_it), __padding.__after_, __specs.__fill_); return _VSTD::fill_n(_VSTD::move(__out_it), __padding.__after_, __specs.__fill_);
} }
/// \overload
/// Calls the function above where \a __size = \a __last - \a __first.
template <class _CharT, class _ParserCharT>
_LIBCPP_HIDE_FROM_ABI auto __write(const _CharT* __first, const _CharT* __last,
output_iterator<const _CharT&> auto __out_it,
__format_spec::__parsed_specifications<_ParserCharT> __specs) -> decltype(__out_it) {
return __write(__first, __last, _VSTD::move(__out_it), __specs, __last - __first);
}
template <class _CharT, class _ParserCharT, class _UnaryOperation>
_LIBCPP_HIDE_FROM_ABI auto __write_transformed(const _CharT* __first, const _CharT* __last,
output_iterator<const _CharT&> auto __out_it,
__format_spec::__parsed_specifications<_ParserCharT> __specs,
_UnaryOperation __op) -> decltype(__out_it) {
_LIBCPP_ASSERT(__first <= __last, "Not a valid range");
ptrdiff_t __size = __last - __first;
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_);
__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_);
}
# ifndef _LIBCPP_HAS_NO_UNICODE # ifndef _LIBCPP_HAS_NO_UNICODE
template <class _CharT> template <class _CharT>
_LIBCPP_HIDE_FROM_ABI auto __write_unicode_no_precision(basic_string_view<_CharT> __str, _LIBCPP_HIDE_FROM_ABI auto __write_unicode_no_precision(basic_string_view<_CharT> __str,
output_iterator<const _CharT&> auto __out_it, output_iterator<const _CharT&> auto __out_it,
__format_spec::__parsed_specifications<_CharT> __specs) __format_spec::__parsed_specifications<_CharT> __specs)
-> decltype(__out_it) { -> decltype(__out_it) {
_LIBCPP_ASSERT(!__specs.__has_precision(), "use __write_unicode"); _LIBCPP_ASSERT(!__specs.__has_precision(), "use __write_unicode");
// No padding -> copy the string // No padding -> copy the string
if (!__specs.__has_width()) if (!__specs.__has_width())