aboutsummaryrefslogtreecommitdiff
path: root/libcxx/include/__format
diff options
context:
space:
mode:
Diffstat (limited to 'libcxx/include/__format')
-rw-r--r--libcxx/include/__format/format_arg.h23
-rw-r--r--libcxx/include/__format/format_arg_store.h10
-rw-r--r--libcxx/include/__format/formatter.h227
-rw-r--r--libcxx/include/__format/formatter_bool.h1
-rw-r--r--libcxx/include/__format/formatter_char.h2
-rw-r--r--libcxx/include/__format/formatter_floating_point.h415
-rw-r--r--libcxx/include/__format/formatter_integer.h33
-rw-r--r--libcxx/include/__format/formatter_integral.h5
-rw-r--r--libcxx/include/__format/formatter_output.h59
-rw-r--r--libcxx/include/__format/parser_std_format_spec.h893
10 files changed, 315 insertions, 1353 deletions
diff --git a/libcxx/include/__format/format_arg.h b/libcxx/include/__format/format_arg.h
index 3f2afc898d2c..4f93024b7c69 100644
--- a/libcxx/include/__format/format_arg.h
+++ b/libcxx/include/__format/format_arg.h
@@ -147,15 +147,20 @@ public:
/// Contains the implementation for basic_format_arg::handle.
struct __handle {
template <class _Tp>
- _LIBCPP_HIDE_FROM_ABI explicit __handle(const _Tp& __v) noexcept
+ _LIBCPP_HIDE_FROM_ABI explicit __handle(_Tp&& __v) noexcept
: __ptr_(_VSTD::addressof(__v)),
__format_([](basic_format_parse_context<_CharT>& __parse_ctx, _Context& __ctx, const void* __ptr) {
- using _Formatter = typename _Context::template formatter_type<_Tp>;
- using _Qp = conditional_t<requires { _Formatter().format(declval<const _Tp&>(), declval<_Context&>()); },
- const _Tp, _Tp>;
+ using _Dp = remove_cvref_t<_Tp>;
+ using _Formatter = typename _Context::template formatter_type<_Dp>;
+ constexpr bool __const_formattable =
+ requires { _Formatter().format(declval<const _Dp&>(), declval<_Context&>()); };
+ using _Qp = conditional_t<__const_formattable, const _Dp, _Dp>;
+
+ static_assert(__const_formattable || !is_const_v<remove_reference_t<_Tp>>, "Mandated by [format.arg]/18");
+
_Formatter __f;
__parse_ctx.advance_to(__f.parse(__parse_ctx));
- __ctx.advance_to(__f.format(*const_cast<_Qp*>(static_cast<const _Tp*>(__ptr)), __ctx));
+ __ctx.advance_to(__f.format(*const_cast<_Qp*>(static_cast<const _Dp*>(__ptr)), __ctx));
}) {}
const void* __ptr_;
@@ -205,7 +210,9 @@ public:
_LIBCPP_HIDE_FROM_ABI __basic_format_arg_value(basic_string_view<_CharT> __value) noexcept
: __string_view_(__value) {}
_LIBCPP_HIDE_FROM_ABI __basic_format_arg_value(const void* __value) noexcept : __ptr_(__value) {}
- _LIBCPP_HIDE_FROM_ABI __basic_format_arg_value(__handle __value) noexcept : __handle_(__value) {}
+ _LIBCPP_HIDE_FROM_ABI __basic_format_arg_value(__handle __value) noexcept
+ // TODO FMT Investigate why it doesn't work without the forward.
+ : __handle_(std::forward<__handle>(__value)) {}
};
template <class _Context>
@@ -251,11 +258,11 @@ public:
__handle_.__format_(__parse_ctx, __ctx, __handle_.__ptr_);
}
- _LIBCPP_HIDE_FROM_ABI explicit handle(typename __basic_format_arg_value<_Context>::__handle __handle) noexcept
+ _LIBCPP_HIDE_FROM_ABI explicit handle(typename __basic_format_arg_value<_Context>::__handle& __handle) noexcept
: __handle_(__handle) {}
private:
- typename __basic_format_arg_value<_Context>::__handle __handle_;
+ typename __basic_format_arg_value<_Context>::__handle& __handle_;
};
#endif //_LIBCPP_STD_VER > 17
diff --git a/libcxx/include/__format/format_arg_store.h b/libcxx/include/__format/format_arg_store.h
index 6602dfeb956b..26a5e71b93af 100644
--- a/libcxx/include/__format/format_arg_store.h
+++ b/libcxx/include/__format/format_arg_store.h
@@ -197,7 +197,7 @@ _LIBCPP_HIDE_FROM_ABI void __create_packed_storage(uint64_t& __types, __basic_fo
int __shift = 0;
(
[&] {
- basic_format_arg<_Context> __arg = __create_format_arg<_Context>(_VSTD::forward<_Args>(__args));
+ basic_format_arg<_Context> __arg = __create_format_arg<_Context>(__args);
if (__shift != 0)
__types |= static_cast<uint64_t>(__arg.__type_) << __shift;
else
@@ -211,7 +211,7 @@ _LIBCPP_HIDE_FROM_ABI void __create_packed_storage(uint64_t& __types, __basic_fo
template <class _Context, class... _Args>
_LIBCPP_HIDE_FROM_ABI void __store_basic_format_arg(basic_format_arg<_Context>* __data, _Args&&... __args) noexcept {
- ([&] { *__data++ = __create_format_arg<_Context>(_VSTD::forward<_Args>(__args)); }(), ...);
+ ([&] { *__data++ = __create_format_arg<_Context>(__args); }(), ...);
}
template <class _Context, size_t N>
@@ -230,12 +230,12 @@ struct __unpacked_format_arg_store {
template <class _Context, class... _Args>
struct _LIBCPP_TEMPLATE_VIS __format_arg_store {
_LIBCPP_HIDE_FROM_ABI
- __format_arg_store(_Args&&... __args) noexcept {
+ __format_arg_store(_Args&... __args) noexcept {
if constexpr (sizeof...(_Args) != 0) {
if constexpr (__format::__use_packed_format_arg_store(sizeof...(_Args)))
- __format::__create_packed_storage(__storage.__types_, __storage.__values_, _VSTD::forward<_Args>(__args)...);
+ __format::__create_packed_storage(__storage.__types_, __storage.__values_, __args...);
else
- __format::__store_basic_format_arg<_Context>(__storage.__args_, _VSTD::forward<_Args>(__args)...);
+ __format::__store_basic_format_arg<_Context>(__storage.__args_, __args...);
}
}
diff --git a/libcxx/include/__format/formatter.h b/libcxx/include/__format/formatter.h
index c39e25b354eb..4816f961c445 100644
--- a/libcxx/include/__format/formatter.h
+++ b/libcxx/include/__format/formatter.h
@@ -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
@@ -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
diff --git a/libcxx/include/__format/formatter_bool.h b/libcxx/include/__format/formatter_bool.h
index 4c9d3fc77473..cdb0631f87d4 100644
--- a/libcxx/include/__format/formatter_bool.h
+++ b/libcxx/include/__format/formatter_bool.h
@@ -47,6 +47,7 @@ public:
_LIBCPP_HIDE_FROM_ABI auto format(bool __value, auto& __ctx) const -> decltype(__ctx.out()) {
switch (__parser_.__type_) {
+ case __format_spec::__type::__default:
case __format_spec::__type::__string:
return __formatter::__format_bool(__value, __ctx, __parser_.__get_parsed_std_specifications(__ctx));
diff --git a/libcxx/include/__format/formatter_char.h b/libcxx/include/__format/formatter_char.h
index cd54abba348a..a3ca36ec0a62 100644
--- a/libcxx/include/__format/formatter_char.h
+++ b/libcxx/include/__format/formatter_char.h
@@ -41,7 +41,7 @@ public:
}
_LIBCPP_HIDE_FROM_ABI auto format(_CharT __value, auto& __ctx) const -> decltype(__ctx.out()) {
- if (__parser_.__type_ == __format_spec::__type::__char)
+ if (__parser_.__type_ == __format_spec::__type::__default || __parser_.__type_ == __format_spec::__type::__char)
return __formatter::__format_char(__value, __ctx.out(), __parser_.__get_parsed_std_specifications(__ctx));
if constexpr (sizeof(_CharT) <= sizeof(int))
diff --git a/libcxx/include/__format/formatter_floating_point.h b/libcxx/include/__format/formatter_floating_point.h
index c9f5689abd8b..90a76193196e 100644
--- a/libcxx/include/__format/formatter_floating_point.h
+++ b/libcxx/include/__format/formatter_floating_point.h
@@ -17,21 +17,19 @@
#include <__algorithm/min.h>
#include <__algorithm/rotate.h>
#include <__algorithm/transform.h>
-#include <__assert>
#include <__concepts/arithmetic.h>
#include <__concepts/same_as.h>
#include <__config>
-#include <__format/format_error.h>
#include <__format/format_fwd.h>
-#include <__format/format_string.h>
+#include <__format/format_parse_context.h>
#include <__format/formatter.h>
#include <__format/formatter_integral.h>
+#include <__format/formatter_output.h>
#include <__format/parser_std_format_spec.h>
#include <__memory/allocator.h>
#include <__utility/move.h>
#include <__utility/unreachable.h>
#include <charconv>
-#include <cmath>
#ifndef _LIBCPP_HAS_NO_LOCALIZATION
# include <locale>
@@ -48,7 +46,7 @@ _LIBCPP_BEGIN_NAMESPACE_STD
#if _LIBCPP_STD_VER > 17
-namespace __format_spec {
+namespace __formatter {
template <floating_point _Tp>
_LIBCPP_HIDE_FROM_ABI char* __to_buffer(char* __first, char* __last, _Tp __value) {
@@ -164,7 +162,7 @@ public:
__precision_ = _Traits::__max_fractional;
}
- __size_ = __format_spec::__float_buffer_size<_Fp>(__precision_);
+ __size_ = __formatter::__float_buffer_size<_Fp>(__precision_);
if (__size_ > _Traits::__stack_buffer_size)
// The allocated buffer's contents don't need initialization.
__begin_ = allocator<char>{}.allocate(__size_);
@@ -233,9 +231,9 @@ _LIBCPP_HIDE_FROM_ABI __float_result __format_buffer_default(const __float_buffe
char* __integral) {
__float_result __result;
__result.__integral = __integral;
- __result.__last = __format_spec::__to_buffer(__integral, __buffer.end(), __value);
+ __result.__last = __formatter::__to_buffer(__integral, __buffer.end(), __value);
- __result.__exponent = __format_spec::__find_exponent(__result.__integral, __result.__last);
+ __result.__exponent = __formatter::__find_exponent(__result.__integral, __result.__last);
// Constrains:
// - There's at least one decimal digit before the radix point.
@@ -264,9 +262,9 @@ _LIBCPP_HIDE_FROM_ABI __float_result __format_buffer_hexadecimal_lower_case(cons
__float_result __result;
__result.__integral = __integral;
if (__precision == -1)
- __result.__last = __format_spec::__to_buffer(__integral, __buffer.end(), __value, chars_format::hex);
+ __result.__last = __formatter::__to_buffer(__integral, __buffer.end(), __value, chars_format::hex);
else
- __result.__last = __format_spec::__to_buffer(__integral, __buffer.end(), __value, chars_format::hex, __precision);
+ __result.__last = __formatter::__to_buffer(__integral, __buffer.end(), __value, chars_format::hex, __precision);
// H = one or more hex-digits
// S = sign
@@ -315,7 +313,7 @@ _LIBCPP_HIDE_FROM_ABI __float_result __format_buffer_hexadecimal_upper_case(cons
_Tp __value, int __precision,
char* __integral) {
__float_result __result =
- __format_spec::__format_buffer_hexadecimal_lower_case(__buffer, __value, __precision, __integral);
+ __formatter::__format_buffer_hexadecimal_lower_case(__buffer, __value, __precision, __integral);
_VSTD::transform(__result.__integral, __result.__exponent, __result.__integral, __hex_to_upper);
*__result.__exponent = 'P';
return __result;
@@ -328,13 +326,13 @@ _LIBCPP_HIDE_FROM_ABI __float_result __format_buffer_scientific_lower_case(const
__float_result __result;
__result.__integral = __integral;
__result.__last =
- __format_spec::__to_buffer(__integral, __buffer.end(), __value, chars_format::scientific, __precision);
+ __formatter::__to_buffer(__integral, __buffer.end(), __value, chars_format::scientific, __precision);
char* __first = __integral + 1;
_LIBCPP_ASSERT(__first != __result.__last, "No exponent present");
if (*__first == '.') {
__result.__radix_point = __first;
- __result.__exponent = __format_spec::__find_exponent(__first + 1, __result.__last);
+ __result.__exponent = __formatter::__find_exponent(__first + 1, __result.__last);
} else {
__result.__radix_point = __result.__last;
__result.__exponent = __first;
@@ -354,7 +352,7 @@ _LIBCPP_HIDE_FROM_ABI __float_result __format_buffer_scientific_upper_case(const
_Tp __value, int __precision,
char* __integral) {
__float_result __result =
- __format_spec::__format_buffer_scientific_lower_case(__buffer, __value, __precision, __integral);
+ __formatter::__format_buffer_scientific_lower_case(__buffer, __value, __precision, __integral);
*__result.__exponent = 'E';
return __result;
}
@@ -364,7 +362,7 @@ _LIBCPP_HIDE_FROM_ABI __float_result __format_buffer_fixed(const __float_buffer<
int __precision, char* __integral) {
__float_result __result;
__result.__integral = __integral;
- __result.__last = __format_spec::__to_buffer(__integral, __buffer.end(), __value, chars_format::fixed, __precision);
+ __result.__last = __formatter::__to_buffer(__integral, __buffer.end(), __value, chars_format::fixed, __precision);
// When there's no precision there's no radix point.
// Else the radix point is placed at __precision + 1 from the end.
@@ -390,14 +388,14 @@ _LIBCPP_HIDE_FROM_ABI __float_result __format_buffer_general_lower_case(__float_
__float_result __result;
__result.__integral = __integral;
- __result.__last = __format_spec::__to_buffer(__integral, __buffer.end(), __value, chars_format::general, __precision);
+ __result.__last = __formatter::__to_buffer(__integral, __buffer.end(), __value, chars_format::general, __precision);
char* __first = __integral + 1;
if (__first == __result.__last) {
__result.__radix_point = __result.__last;
__result.__exponent = __result.__last;
} else {
- __result.__exponent = __format_spec::__find_exponent(__first, __result.__last);
+ __result.__exponent = __formatter::__find_exponent(__first, __result.__last);
if (__result.__exponent != __result.__last)
// In scientific mode if there's a radix point it will always be after
// the first digit. (This is the position __first points at).
@@ -423,19 +421,79 @@ _LIBCPP_HIDE_FROM_ABI __float_result __format_buffer_general_lower_case(__float_
template <class _Fp, class _Tp>
_LIBCPP_HIDE_FROM_ABI __float_result __format_buffer_general_upper_case(__float_buffer<_Fp>& __buffer, _Tp __value,
int __precision, char* __integral) {
- __float_result __result =
- __format_spec::__format_buffer_general_lower_case(__buffer, __value, __precision, __integral);
+ __float_result __result = __formatter::__format_buffer_general_lower_case(__buffer, __value, __precision, __integral);
if (__result.__exponent != __result.__last)
*__result.__exponent = 'E';
return __result;
}
-# ifndef _LIBCPP_HAS_NO_LOCALIZATION
+/// Fills the buffer with the data based on the requested formatting.
+///
+/// This function, when needed, turns the characters to upper case and
+/// determines the "interesting" locations which are returned to the caller.
+///
+/// This means the caller never has to convert the contents of the buffer to
+/// upper case or search for radix points and the location of the exponent.
+/// This gives a bit of overhead. The original code didn't do that, but due
+/// to the number of possible additional work needed to turn this number to
+/// the proper output the code was littered with tests for upper cases and
+/// searches for radix points and exponents.
+/// - When a precision larger than the type's precision is selected
+/// additional zero characters need to be written before the exponent.
+/// - alternate form needs to add a radix point when not present.
+/// - localization needs to do grouping in the integral part.
+template <class _Fp, class _Tp>
+// TODO FMT _Fp should just be _Tp when to_chars has proper long double support.
+_LIBCPP_HIDE_FROM_ABI __float_result __format_buffer(
+ __float_buffer<_Fp>& __buffer,
+ _Tp __value,
+ bool __negative,
+ bool __has_precision,
+ __format_spec::__sign __sign,
+ __format_spec::__type __type) {
+ char* __first = __formatter::__insert_sign(__buffer.begin(), __negative, __sign);
+ switch (__type) {
+ case __format_spec::__type::__default:
+ return __formatter::__format_buffer_default(__buffer, __value, __first);
+
+ case __format_spec::__type::__hexfloat_lower_case:
+ return __formatter::__format_buffer_hexadecimal_lower_case(
+ __buffer, __value, __has_precision ? __buffer.__precision() : -1, __first);
+
+ case __format_spec::__type::__hexfloat_upper_case:
+ return __formatter::__format_buffer_hexadecimal_upper_case(
+ __buffer, __value, __has_precision ? __buffer.__precision() : -1, __first);
+
+ case __format_spec::__type::__scientific_lower_case:
+ return __formatter::__format_buffer_scientific_lower_case(__buffer, __value, __buffer.__precision(), __first);
+
+ case __format_spec::__type::__scientific_upper_case:
+ return __formatter::__format_buffer_scientific_upper_case(__buffer, __value, __buffer.__precision(), __first);
+
+ case __format_spec::__type::__fixed_lower_case:
+ case __format_spec::__type::__fixed_upper_case:
+ return __formatter::__format_buffer_fixed(__buffer, __value, __buffer.__precision(), __first);
+
+ case __format_spec::__type::__general_lower_case:
+ return __formatter::__format_buffer_general_lower_case(__buffer, __value, __buffer.__precision(), __first);
+
+ case __format_spec::__type::__general_upper_case:
+ return __formatter::__format_buffer_general_upper_case(__buffer, __value, __buffer.__precision(), __first);
+
+ default:
+ _LIBCPP_ASSERT(false, "The parser should have validated the type");
+ __libcpp_unreachable();
+ }
+}
+
+# ifndef _LIBCPP_HAS_NO_LOCALIZATION
template <class _OutIt, class _Fp, class _CharT>
-_LIBCPP_HIDE_FROM_ABI _OutIt __format_locale_specific_form(_OutIt __out_it, const __float_buffer<_Fp>& __buffer,
- const __float_result& __result, _VSTD::locale __loc,
- size_t __width, _Flags::_Alignment __alignment,
- _CharT __fill) {
+_LIBCPP_HIDE_FROM_ABI _OutIt __format_locale_specific_form(
+ _OutIt __out_it,
+ const __float_buffer<_Fp>& __buffer,
+ const __float_result& __result,
+ _VSTD::locale __loc,
+ __format_spec::__parsed_specifications<_CharT> __specs) {
const auto& __np = use_facet<numpunct<_CharT>>(__loc);
string __grouping = __np.grouping();
char* __first = __result.__integral;
@@ -450,26 +508,27 @@ _LIBCPP_HIDE_FROM_ABI _OutIt __format_locale_specific_form(_OutIt __out_it, cons
__grouping = __formatter::__determine_grouping(__digits, __grouping);
}
- size_t __size = __result.__last - __buffer.begin() + // Formatted string
- __buffer.__num_trailing_zeros() + // Not yet rendered zeros
- __grouping.size() - // Grouping contains one
- !__grouping.empty(); // additional character
+ ptrdiff_t __size =
+ __result.__last - __buffer.begin() + // Formatted string
+ __buffer.__num_trailing_zeros() + // Not yet rendered zeros
+ __grouping.size() - // Grouping contains one
+ !__grouping.empty(); // additional character
- __formatter::__padding_size_result __padding = {0, 0};
- bool __zero_padding = __alignment == _Flags::_Alignment::__default;
- if (__size < __width) {
+ __formatter::__padding_size_result __padding = {0, 0};
+ bool __zero_padding = __specs.__alignment_ == __format_spec::__alignment::__zero_padding;
+ if (__size < __specs.__width_) {
if (__zero_padding) {
- __alignment = _Flags::_Alignment::__right;
- __fill = _CharT('0');
+ __specs.__alignment_ = __format_spec::__alignment::__right;
+ __specs.__fill_ = _CharT('0');
}
- __padding = __formatter::__padding_size(__size, __width, __alignment);
+ __padding = __formatter::__padding_size(__size, __specs.__width_, __specs.__alignment_);
}
// sign and (zero padding or alignment)
if (__zero_padding && __first != __buffer.begin())
*__out_it++ = *__buffer.begin();
- __out_it = _VSTD::fill_n(_VSTD::move(__out_it), __padding.__before, __fill);
+ __out_it = _VSTD::fill_n(_VSTD::move(__out_it), __padding.__before_, __specs.__fill_);
if (!__zero_padding && __first != __buffer.begin())
*__out_it++ = *__buffer.begin();
@@ -510,198 +569,148 @@ _LIBCPP_HIDE_FROM_ABI _OutIt __format_locale_specific_form(_OutIt __out_it, cons
__out_it = _VSTD::copy(__result.__exponent, __result.__last, _VSTD::move(__out_it));
// alignment
- return _VSTD::fill_n(_VSTD::move(__out_it), __padding.__after, __fill);
+ return _VSTD::fill_n(_VSTD::move(__out_it), __padding.__after_, __specs.__fill_);
+}
+# endif // _LIBCPP_HAS_NO_LOCALIZATION
+
+template <class _OutIt, class _CharT>
+_LIBCPP_HIDE_FROM_ABI _OutIt __format_floating_point_non_finite(
+ _OutIt __out_it, __format_spec::__parsed_specifications<_CharT> __specs, bool __negative, bool __isnan) {
+ char __buffer[4];
+ char* __last = __formatter::__insert_sign(__buffer, __negative, __specs.__std_.__sign_);
+
+ // to_chars can return inf, infinity, nan, and nan(n-char-sequence).
+ // The format library requires inf and nan.
+ // All in one expression to avoid dangling references.
+ bool __upper_case =
+ __specs.__std_.__type_ == __format_spec::__type::__hexfloat_upper_case ||
+ __specs.__std_.__type_ == __format_spec::__type::__scientific_upper_case ||
+ __specs.__std_.__type_ == __format_spec::__type::__fixed_upper_case ||
+ __specs.__std_.__type_ == __format_spec::__type::__general_upper_case;
+ __last = _VSTD::copy_n(&("infnanINFNAN"[6 * __upper_case + 3 * __isnan]), 3, __last);
+
+ // [format.string.std]/13
+ // A zero (0) character preceding the width field pads the field with
+ // leading zeros (following any indication of sign or base) to the field
+ // width, except when applied to an infinity or NaN.
+ if (__specs.__alignment_ == __format_spec::__alignment::__zero_padding)
+ __specs.__alignment_ = __format_spec::__alignment::__right;
+
+ return __formatter::__write(__buffer, __last, _VSTD::move(__out_it), __specs);
}
-# endif // _LIBCPP_HAS_NO_LOCALIZATION
-
-template <__formatter::__char_type _CharT>
-class _LIBCPP_TEMPLATE_VIS __formatter_floating_point : public __parser_floating_point<_CharT> {
-public:
- template <floating_point _Tp>
- _LIBCPP_HIDE_FROM_ABI auto format(_Tp __value, auto& __ctx) -> decltype(__ctx.out()) {
- if (this->__width_needs_substitution())
- this->__substitute_width_arg_id(__ctx.arg(this->__width));
-
- bool __negative = _VSTD::signbit(__value);
-
- if (!_VSTD::isfinite(__value)) [[unlikely]]
- return __format_non_finite(__ctx.out(), __negative, _VSTD::isnan(__value));
-
- bool __has_precision = this->__has_precision_field();
- if (this->__precision_needs_substitution())
- this->__substitute_precision_arg_id(__ctx.arg(this->__precision));
-
- // Depending on the std-format-spec string the sign and the value
- // might not be outputted together:
- // - zero-padding may insert additional '0' characters.
- // Therefore the value is processed as a non negative value.
- // The function @ref __insert_sign will insert a '-' when the value was
- // negative.
-
- if (__negative)
- __value = _VSTD::copysign(__value, +1.0);
-
- // TODO FMT _Fp should just be _Tp when to_chars has proper long double support.
- using _Fp = conditional_t<same_as<_Tp, long double>, double, _Tp>;
- // Force the type of the precision to avoid -1 to become an unsigned value.
- __float_buffer<_Fp> __buffer(__has_precision ? int(this->__precision) : -1);
- __float_result __result = __format_buffer(__buffer, __value, __negative, __has_precision);
-
- if (this->__alternate_form && __result.__radix_point == __result.__last) {
- *__result.__last++ = '.';
-
- // When there is an exponent the point needs to be moved before the
- // exponent. When there's no exponent the rotate does nothing. Since
- // rotate tests whether the operation is a nop, call it unconditionally.
- _VSTD::rotate(__result.__exponent, __result.__last - 1, __result.__last);
- __result.__radix_point = __result.__exponent;
-
- // The radix point is always placed before the exponent.
- // - No exponent needs to point to the new last.
- // - An exponent needs to move one position to the right.
- // So it's safe to increment the value unconditionally.
- ++__result.__exponent;
- }
+template <floating_point _Tp, class _CharT>
+_LIBCPP_HIDE_FROM_ABI auto
+__format_floating_point(_Tp __value, auto& __ctx, __format_spec::__parsed_specifications<_CharT> __specs)
+ -> decltype(__ctx.out()) {
+ bool __negative = _VSTD::signbit(__value);
-# ifndef _LIBCPP_HAS_NO_LOCALIZATION
- if (this->__locale_specific_form)
- return __format_spec::__format_locale_specific_form(__ctx.out(), __buffer, __result, __ctx.locale(),
- this->__width, this->__alignment, this->__fill);
-# endif
-
- ptrdiff_t __size = __result.__last - __buffer.begin();
- int __num_trailing_zeros = __buffer.__num_trailing_zeros();
- if (__size + __num_trailing_zeros >= this->__width) {
- if (__num_trailing_zeros && __result.__exponent != __result.__last)
- // Insert trailing zeros before exponent character.
- return _VSTD::copy(__result.__exponent, __result.__last,
- _VSTD::fill_n(_VSTD::copy(__buffer.begin(), __result.__exponent, __ctx.out()),
- __num_trailing_zeros, _CharT('0')));
-
- return _VSTD::fill_n(_VSTD::copy(__buffer.begin(), __result.__last, __ctx.out()), __num_trailing_zeros,
- _CharT('0'));
- }
+ if (!_VSTD::isfinite(__value)) [[unlikely]]
+ return __formatter::__format_floating_point_non_finite(__ctx.out(), __specs, __negative, _VSTD::isnan(__value));
- auto __out_it = __ctx.out();
- char* __first = __buffer.begin();
- if (this->__alignment == _Flags::_Alignment::__default) {
- // When there is a sign output it before the padding. Note the __size
- // doesn't need any adjustment, regardless whether the sign is written
- // here or in __formatter::__write.
- if (__first != __result.__integral)
- *__out_it++ = *__first++;
- // After the sign is written, zero padding is the same a right alignment
- // with '0'.
- this->__alignment = _Flags::_Alignment::__right;
- this->__fill = _CharT('0');
- }
+ // Depending on the std-format-spec string the sign and the value
+ // might not be outputted together:
+ // - zero-padding may insert additional '0' characters.
+ // Therefore the value is processed as a non negative value.
+ // The function @ref __insert_sign will insert a '-' when the value was
+ // negative.
- if (__num_trailing_zeros)
- return __formatter::__write(_VSTD::move(__out_it), __first, __result.__last, __size, this->__width, this->__fill,
- this->__alignment, __result.__exponent, __num_trailing_zeros);
+ if (__negative)
+ __value = -__value;
- return __formatter::__write(_VSTD::move(__out_it), __first, __result.__last, __size, this->__width, this->__fill,
- this->__alignment);
+ // TODO FMT _Fp should just be _Tp when to_chars has proper long double support.
+ using _Fp = conditional_t<same_as<_Tp, long double>, double, _Tp>;
+ // Force the type of the precision to avoid -1 to become an unsigned value.
+ __float_buffer<_Fp> __buffer(__specs.__precision_);
+ __float_result __result = __formatter::__format_buffer(
+ __buffer, __value, __negative, (__specs.__has_precision()), __specs.__std_.__sign_, __specs.__std_.__type_);
+
+ if (__specs.__std_.__alternate_form_ && __result.__radix_point == __result.__last) {
+ *__result.__last++ = '.';
+
+ // When there is an exponent the point needs to be moved before the
+ // exponent. When there's no exponent the rotate does nothing. Since
+ // rotate tests whether the operation is a nop, call it unconditionally.
+ _VSTD::rotate(__result.__exponent, __result.__last - 1, __result.__last);
+ __result.__radix_point = __result.__exponent;
+
+ // The radix point is always placed before the exponent.
+ // - No exponent needs to point to the new last.
+ // - An exponent needs to move one position to the right.
+ // So it's safe to increment the value unconditionally.
+ ++__result.__exponent;
}
-private:
- template <class _OutIt>
- _LIBCPP_HIDE_FROM_ABI _OutIt __format_non_finite(_OutIt __out_it, bool __negative, bool __isnan) {
- char __buffer[4];
- char* __last = __insert_sign(__buffer, __negative, this->__sign);
-
- // to_char can return inf, infinity, nan, and nan(n-char-sequence).
- // The format library requires inf and nan.
- // All in one expression to avoid dangling references.
- __last = _VSTD::copy_n(&("infnanINFNAN"[6 * (this->__type == _Flags::_Type::__float_hexadecimal_upper_case ||
- this->__type == _Flags::_Type::__scientific_upper_case ||
- this->__type == _Flags::_Type::__fixed_upper_case ||
- this->__type == _Flags::_Type::__general_upper_case) +
- 3 * __isnan]),
- 3, __last);
-
- // [format.string.std]/13
- // A zero (0) character preceding the width field pads the field with
- // leading zeros (following any indication of sign or base) to the field
- // width, except when applied to an infinity or NaN.
- if (this->__alignment == _Flags::_Alignment::__default)
- this->__alignment = _Flags::_Alignment::__right;
-
- ptrdiff_t __size = __last - __buffer;
- if (__size >= this->__width)
- return _VSTD::copy_n(__buffer, __size, _VSTD::move(__out_it));
-
- return __formatter::__write(_VSTD::move(__out_it), __buffer, __last, __size, this->__width, this->__fill,
- this->__alignment);
+# ifndef _LIBCPP_HAS_NO_LOCALIZATION
+ if (__specs.__std_.__locale_specific_form_)
+ return __formatter::__format_locale_specific_form(__ctx.out(), __buffer, __result, __ctx.locale(), __specs);
+# endif
+
+ ptrdiff_t __size = __result.__last - __buffer.begin();
+ int __num_trailing_zeros = __buffer.__num_trailing_zeros();
+ if (__size + __num_trailing_zeros >= __specs.__width_) {
+ if (__num_trailing_zeros && __result.__exponent != __result.__last)
+ // Insert trailing zeros before exponent character.
+ return _VSTD::copy(
+ __result.__exponent,
+ __result.__last,
+ _VSTD::fill_n(
+ _VSTD::copy(__buffer.begin(), __result.__exponent, __ctx.out()), __num_trailing_zeros, _CharT('0')));
+
+ return _VSTD::fill_n(
+ _VSTD::copy(__buffer.begin(), __result.__last, __ctx.out()), __num_trailing_zeros, _CharT('0'));
}
- /// Fills the buffer with the data based on the requested formatting.
- ///
- /// This function, when needed, turns the characters to upper case and
- /// determines the "interesting" locations which are returned to the caller.
- ///
- /// This means the caller never has to convert the contents of the buffer to
- /// upper case or search for radix points and the location of the exponent.
- /// This gives a bit of overhead. The original code didn't do that, but due
- /// to the number of possible additional work needed to turn this number to
- /// the proper output the code was littered with tests for upper cases and
- /// searches for radix points and exponents.
- /// - When a precision larger than the type's precision is selected
- /// additional zero characters need to be written before the exponent.
- /// - alternate form needs to add a radix point when not present.
- /// - localization needs to do grouping in the integral part.
- template <class _Fp, class _Tp>
- // TODO FMT _Fp should just be _Tp when to_chars has proper long double support.
- _LIBCPP_HIDE_FROM_ABI __float_result __format_buffer(__float_buffer<_Fp>& __buffer, _Tp __value, bool __negative,
- bool __has_precision) {
- char* __first = __insert_sign(__buffer.begin(), __negative, this->__sign);
- switch (this->__type) {
- case _Flags::_Type::__default:
- return __format_spec::__format_buffer_default(__buffer, __value, __first);
-
- case _Flags::_Type::__float_hexadecimal_lower_case:
- return __format_spec::__format_buffer_hexadecimal_lower_case(
- __buffer, __value, __has_precision ? __buffer.__precision() : -1, __first);
-
- case _Flags::_Type::__float_hexadecimal_upper_case:
- return __format_spec::__format_buffer_hexadecimal_upper_case(
- __buffer, __value, __has_precision ? __buffer.__precision() : -1, __first);
-
- case _Flags::_Type::__scientific_lower_case:
- return __format_spec::__format_buffer_scientific_lower_case(__buffer, __value, __buffer.__precision(), __first);
+ auto __out_it = __ctx.out();
+ char* __first = __buffer.begin();
+ if (__specs.__alignment_ == __format_spec::__alignment ::__zero_padding) {
+ // When there is a sign output it before the padding. Note the __size
+ // doesn't need any adjustment, regardless whether the sign is written
+ // here or in __formatter::__write.
+ if (__first != __result.__integral)
+ *__out_it++ = *__first++;
+ // After the sign is written, zero padding is the same a right alignment
+ // with '0'.
+ __specs.__alignment_ = __format_spec::__alignment::__right;
+ __specs.__fill_ = _CharT('0');
+ }
- case _Flags::_Type::__scientific_upper_case:
- return __format_spec::__format_buffer_scientific_upper_case(__buffer, __value, __buffer.__precision(), __first);
+ if (__num_trailing_zeros)
+ return __formatter::__write_using_trailing_zeros(
+ __first, __result.__last, _VSTD::move(__out_it), __specs, __size, __result.__exponent, __num_trailing_zeros);
- case _Flags::_Type::__fixed_lower_case:
- case _Flags::_Type::__fixed_upper_case:
- return __format_spec::__format_buffer_fixed(__buffer, __value, __buffer.__precision(), __first);
+ return __formatter::__write(__first, __result.__last, _VSTD::move(__out_it), __specs, __size);
+}
- case _Flags::_Type::__general_lower_case:
- return __format_spec::__format_buffer_general_lower_case(__buffer, __value, __buffer.__precision(), __first);
+} // namespace __formatter
- case _Flags::_Type::__general_upper_case:
- return __format_spec::__format_buffer_general_upper_case(__buffer, __value, __buffer.__precision(), __first);
+template <__formatter::__char_type _CharT>
+struct _LIBCPP_TEMPLATE_VIS __formatter_floating_point {
+public:
+ _LIBCPP_HIDE_FROM_ABI constexpr auto
+ parse(basic_format_parse_context<_CharT>& __parse_ctx) -> decltype(__parse_ctx.begin()) {
+ auto __result = __parser_.__parse(__parse_ctx, __format_spec::__fields_floating_point);
+ __format_spec::__process_parsed_floating_point(__parser_);
+ return __result;
+ }
- default:
- _LIBCPP_ASSERT(false, "The parser should have validated the type");
- __libcpp_unreachable();
- }
+ template <floating_point _Tp>
+ _LIBCPP_HIDE_FROM_ABI auto format(_Tp __value, auto& __ctx) const -> decltype(__ctx.out()) {
+ return __formatter::__format_floating_point(__value, __ctx, __parser_.__get_parsed_std_specifications(__ctx));
}
-};
-} //namespace __format_spec
+ __format_spec::__parser<_CharT> __parser_;
+};
template <__formatter::__char_type _CharT>
struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter<float, _CharT>
- : public __format_spec::__formatter_floating_point<_CharT> {};
+ : public __formatter_floating_point<_CharT> {};
template <__formatter::__char_type _CharT>
struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter<double, _CharT>
- : public __format_spec::__formatter_floating_point<_CharT> {};
+ : public __formatter_floating_point<_CharT> {};
template <__formatter::__char_type _CharT>
struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter<long double, _CharT>
- : public __format_spec::__formatter_floating_point<_CharT> {};
+ : public __formatter_floating_point<_CharT> {};
#endif //_LIBCPP_STD_VER > 17
diff --git a/libcxx/include/__format/formatter_integer.h b/libcxx/include/__format/formatter_integer.h
index 5d11f8d1d990..0281b4f2fa67 100644
--- a/libcxx/include/__format/formatter_integer.h
+++ b/libcxx/include/__format/formatter_integer.h
@@ -13,23 +13,18 @@
#include <__availability>
#include <__concepts/arithmetic.h>
#include <__config>
-#include <__format/format_error.h> // TODO FMT Remove after adding 128-bit support
#include <__format/format_fwd.h>
#include <__format/format_parse_context.h>
#include <__format/formatter.h>
#include <__format/formatter_integral.h>
#include <__format/formatter_output.h>
#include <__format/parser_std_format_spec.h>
-#include <limits> // TODO FMT Remove after adding 128-bit support
#include <type_traits>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
#endif
-_LIBCPP_PUSH_MACROS // TODO FMT Remove after adding 128-bit support
-#include <__undef_macros>
-
_LIBCPP_BEGIN_NAMESPACE_STD
#if _LIBCPP_STD_VER > 17
@@ -79,18 +74,7 @@ struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter<long long, _Ch
# ifndef _LIBCPP_HAS_NO_INT128
template <__formatter::__char_type _CharT>
struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter<__int128_t, _CharT>
- : public __formatter_integer<_CharT> {
- using _Base = __formatter_integer<_CharT>;
-
- _LIBCPP_HIDE_FROM_ABI auto format(__int128_t __value, auto& __ctx) const -> decltype(__ctx.out()) {
- // TODO FMT Implement full 128 bit support.
- using _To = long long;
- if (__value < numeric_limits<_To>::min() || __value > numeric_limits<_To>::max())
- std::__throw_format_error("128-bit value is outside of implemented range");
-
- return _Base::format(static_cast<_To>(__value), __ctx);
- }
-};
+ : public __formatter_integer<_CharT> {};
# endif
// Unsigned integral types.
@@ -112,24 +96,11 @@ struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter<unsigned long
# ifndef _LIBCPP_HAS_NO_INT128
template <__formatter::__char_type _CharT>
struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter<__uint128_t, _CharT>
- : public __formatter_integer<_CharT> {
- using _Base = __formatter_integer<_CharT>;
-
- _LIBCPP_HIDE_FROM_ABI auto format(__uint128_t __value, auto& __ctx) const -> decltype(__ctx.out()) {
- // TODO FMT Implement full 128 bit support.
- using _To = unsigned long long;
- if (__value < numeric_limits<_To>::min() || __value > numeric_limits<_To>::max())
- std::__throw_format_error("128-bit value is outside of implemented range");
-
- return _Base::format(static_cast<_To>(__value), __ctx);
- }
-};
+ : public __formatter_integer<_CharT> {};
# endif
#endif //_LIBCPP_STD_VER > 17
_LIBCPP_END_NAMESPACE_STD
-_LIBCPP_POP_MACROS
-
#endif // _LIBCPP___FORMAT_FORMATTER_INTEGER_H
diff --git a/libcxx/include/__format/formatter_integral.h b/libcxx/include/__format/formatter_integral.h
index 4ad6de0ec66f..d6fa5ec18eb8 100644
--- a/libcxx/include/__format/formatter_integral.h
+++ b/libcxx/include/__format/formatter_integral.h
@@ -207,10 +207,6 @@ _LIBCPP_HIDE_FROM_ABI auto __format_integer(
char* __end,
const char* __prefix,
int __base) -> decltype(__ctx.out()) {
- _LIBCPP_ASSERT(
- __specs.__alignment_ != __format_spec::__alignment::__default,
- "the caller should adjust the default to the value required by the type");
-
char* __first = __formatter::__insert_sign(__begin, __negative, __specs.__std_.__sign_);
if (__specs.__std_.__alternate_form_ && __prefix)
while (*__prefix)
@@ -280,6 +276,7 @@ _LIBCPP_HIDE_FROM_ABI auto __format_integer(
return __formatter::__format_integer(
__value, __ctx, __specs, __negative, __array.begin(), __array.end(), __value != 0 ? "0" : nullptr, 8);
}
+ case __format_spec::__type::__default:
case __format_spec::__type::__decimal: {
array<char, __formatter::__buffer_size<decltype(__value), 10>()> __array;
return __formatter::__format_integer(
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,
diff --git a/libcxx/include/__format/parser_std_format_spec.h b/libcxx/include/__format/parser_std_format_spec.h
index 739bdf457e40..034fc55a44dc 100644
--- a/libcxx/include/__format/parser_std_format_spec.h
+++ b/libcxx/include/__format/parser_std_format_spec.h
@@ -44,168 +44,6 @@ _LIBCPP_BEGIN_NAMESPACE_STD
namespace __format_spec {
-/**
- * Contains the flags for the std-format-spec.
- *
- * Some format-options can only be used for specific C++ types and may depend on
- * the selected format-type.
- * * The C++type filtering can be done using the proper policies for
- * @ref __parser_std.
- * * The format-type filtering needs to be done post parsing in the parser
- * derived from @ref __parser_std.
- */
-_LIBCPP_PACKED_BYTE_FOR_AIX
-class _LIBCPP_TYPE_VIS _Flags {
-public:
- enum class _LIBCPP_ENUM_VIS _Alignment : uint8_t {
- /**
- * No alignment is set in the format string.
- *
- * Zero-padding is ignored when an alignment is selected.
- * The default alignment depends on the selected format-type.
- */
- __default,
- __left,
- __center,
- __right
- };
- enum class _LIBCPP_ENUM_VIS _Sign : uint8_t {
- /**
- * No sign is set in the format string.
- *
- * The sign isn't allowed for certain format-types. By using this value
- * it's possible to detect whether or not the user explicitly set the sign
- * flag. For formatting purposes it behaves the same as @ref __minus.
- */
- __default,
- __minus,
- __plus,
- __space
- };
-
- _Alignment __alignment : 2 {_Alignment::__default};
- _Sign __sign : 2 {_Sign::__default};
- uint8_t __alternate_form : 1 {false};
- uint8_t __zero_padding : 1 {false};
- uint8_t __locale_specific_form : 1 {false};
-
- enum class _LIBCPP_ENUM_VIS _Type : uint8_t {
- __default,
- __string,
- __binary_lower_case,
- __binary_upper_case,
- __octal,
- __decimal,
- __hexadecimal_lower_case,
- __hexadecimal_upper_case,
- __pointer,
- __char,
- __float_hexadecimal_lower_case,
- __float_hexadecimal_upper_case,
- __scientific_lower_case,
- __scientific_upper_case,
- __fixed_lower_case,
- __fixed_upper_case,
- __general_lower_case,
- __general_upper_case
- };
-
- _Type __type{_Type::__default};
-};
-_LIBCPP_PACKED_BYTE_FOR_AIX_END
-
-namespace __detail {
-template <class _CharT>
-_LIBCPP_HIDE_FROM_ABI constexpr bool
-__parse_alignment(_CharT __c, _Flags& __flags) noexcept {
- switch (__c) {
- case _CharT('<'):
- __flags.__alignment = _Flags::_Alignment::__left;
- return true;
-
- case _CharT('^'):
- __flags.__alignment = _Flags::_Alignment::__center;
- return true;
-
- case _CharT('>'):
- __flags.__alignment = _Flags::_Alignment::__right;
- return true;
- }
- return false;
-}
-} // namespace __detail
-
-template <class _CharT>
-class _LIBCPP_TEMPLATE_VIS __parser_fill_align {
-public:
- // TODO FMT The standard doesn't specify this character is a Unicode
- // character. Validate what fmt and MSVC have implemented.
- _CharT __fill{_CharT(' ')};
-
-protected:
- _LIBCPP_HIDE_FROM_ABI constexpr const _CharT*
- __parse(const _CharT* __begin, const _CharT* __end, _Flags& __flags) {
- _LIBCPP_ASSERT(__begin != __end,
- "When called with an empty input the function will cause "
- "undefined behavior by evaluating data not in the input");
- if (__begin + 1 != __end) {
- if (__detail::__parse_alignment(*(__begin + 1), __flags)) {
- if (*__begin == _CharT('{') || *__begin == _CharT('}'))
- __throw_format_error(
- "The format-spec fill field contains an invalid character");
- __fill = *__begin;
- return __begin + 2;
- }
- }
-
- if (__detail::__parse_alignment(*__begin, __flags))
- return __begin + 1;
-
- return __begin;
- }
-};
-
-template <class _CharT>
-_LIBCPP_HIDE_FROM_ABI constexpr const _CharT*
-__parse_sign(const _CharT* __begin, _Flags& __flags) noexcept {
- switch (*__begin) {
- case _CharT('-'):
- __flags.__sign = _Flags::_Sign::__minus;
- break;
- case _CharT('+'):
- __flags.__sign = _Flags::_Sign::__plus;
- break;
- case _CharT(' '):
- __flags.__sign = _Flags::_Sign::__space;
- break;
- default:
- return __begin;
- }
- return __begin + 1;
-}
-
-template <class _CharT>
-_LIBCPP_HIDE_FROM_ABI constexpr const _CharT*
-__parse_alternate_form(const _CharT* __begin, _Flags& __flags) noexcept {
- if (*__begin == _CharT('#')) {
- __flags.__alternate_form = true;
- ++__begin;
- }
-
- return __begin;
-}
-
-template <class _CharT>
-_LIBCPP_HIDE_FROM_ABI constexpr const _CharT*
-__parse_zero_padding(const _CharT* __begin, _Flags& __flags) noexcept {
- if (*__begin == _CharT('0')) {
- __flags.__zero_padding = true;
- ++__begin;
- }
-
- return __begin;
-}
-
template <class _CharT>
_LIBCPP_HIDE_FROM_ABI constexpr __format::__parse_number_result< _CharT>
__parse_arg_id(const _CharT* __begin, const _CharT* __end, auto& __parse_ctx) {
@@ -226,7 +64,7 @@ __parse_arg_id(const _CharT* __begin, const _CharT* __end, auto& __parse_ctx) {
template <class _Context>
_LIBCPP_HIDE_FROM_ABI constexpr uint32_t
-__substitute_arg_id(basic_format_arg<_Context> _Arg) {
+__substitute_arg_id(basic_format_arg<_Context> __format_arg) {
return visit_format_arg(
[](auto __arg) -> uint32_t {
using _Type = decltype(__arg);
@@ -250,685 +88,9 @@ __substitute_arg_id(basic_format_arg<_Context> _Arg) {
__throw_format_error("A format-spec arg-id replacement argument "
"isn't an integral type");
},
- _Arg);
-}
-
-class _LIBCPP_TYPE_VIS __parser_width {
-public:
- /** Contains a width or an arg-id. */
- uint32_t __width : 31 {0};
- /** Determines whether the value stored is a width or an arg-id. */
- uint32_t __width_as_arg : 1 {0};
-
- /**
- * Does the supplied width field contain an arg-id?
- *
- * If @c true the formatter needs to call @ref __substitute_width_arg_id.
- */
- constexpr bool __width_needs_substitution() const noexcept { return __width_as_arg; }
-
-protected:
- /**
- * Does the supplied std-format-spec contain a width field?
- *
- * When the field isn't present there's no padding required. This can be used
- * to optimize the formatting.
- */
- constexpr bool __has_width_field() const noexcept { return __width_as_arg || __width; }
-
- template <class _CharT>
- _LIBCPP_HIDE_FROM_ABI constexpr const _CharT*
- __parse(const _CharT* __begin, const _CharT* __end, auto& __parse_ctx) {
- if (*__begin == _CharT('0'))
- __throw_format_error(
- "A format-spec width field shouldn't have a leading zero");
-
- if (*__begin == _CharT('{')) {
- __format::__parse_number_result __r =
- __parse_arg_id(++__begin, __end, __parse_ctx);
- __width = __r.__value;
- __width_as_arg = 1;
- return __r.__ptr;
- }
-
- if (*__begin < _CharT('0') || *__begin > _CharT('9'))
- return __begin;
-
- __format::__parse_number_result __r =
- __format::__parse_number(__begin, __end);
- __width = __r.__value;
- _LIBCPP_ASSERT(__width != 0,
- "A zero value isn't allowed and should be impossible, "
- "due to validations in this function");
- return __r.__ptr;
- }
-
- _LIBCPP_HIDE_FROM_ABI constexpr void __substitute_width_arg_id(auto __arg) {
- _LIBCPP_ASSERT(__width_as_arg == 1,
- "Substitute width called when no substitution is required");
-
- // The clearing of the flag isn't required but looks better when debugging
- // the code.
- __width_as_arg = 0;
- __width = __substitute_arg_id(__arg);
- if (__width == 0)
- __throw_format_error(
- "A format-spec width field replacement should have a positive value");
- }
-};
-
-class _LIBCPP_TYPE_VIS __parser_precision {
-public:
- /** Contains a precision or an arg-id. */
- uint32_t __precision : 31 {__format::__number_max};
- /**
- * Determines whether the value stored is a precision or an arg-id.
- *
- * @note Since @ref __precision == @ref __format::__number_max is a valid
- * value, the default value contains an arg-id of INT32_MAX. (This number of
- * arguments isn't supported by compilers.) This is used to detect whether
- * the std-format-spec contains a precision field.
- */
- uint32_t __precision_as_arg : 1 {1};
-
- /**
- * Does the supplied precision field contain an arg-id?
- *
- * If @c true the formatter needs to call @ref __substitute_precision_arg_id.
- */
- constexpr bool __precision_needs_substitution() const noexcept {
- return __precision_as_arg && __precision != __format::__number_max;
- }
-
-protected:
- /**
- * Does the supplied std-format-spec contain a precision field?
- *
- * When the field isn't present there's no truncating required. This can be
- * used to optimize the formatting.
- */
- constexpr bool __has_precision_field() const noexcept {
-
- return __precision_as_arg == 0 || // Contains a value?
- __precision != __format::__number_max; // The arg-id is valid?
- }
-
- template <class _CharT>
- _LIBCPP_HIDE_FROM_ABI constexpr const _CharT*
- __parse(const _CharT* __begin, const _CharT* __end, auto& __parse_ctx) {
- if (*__begin != _CharT('.'))
- return __begin;
-
- ++__begin;
- if (__begin == __end)
- __throw_format_error("End of input while parsing format-spec precision");
-
- if (*__begin == _CharT('{')) {
- __format::__parse_number_result __arg_id =
- __parse_arg_id(++__begin, __end, __parse_ctx);
- _LIBCPP_ASSERT(__arg_id.__value != __format::__number_max,
- "Unsupported number of arguments, since this number of "
- "arguments is used a special value");
- __precision = __arg_id.__value;
- return __arg_id.__ptr;
- }
-
- if (*__begin < _CharT('0') || *__begin > _CharT('9'))
- __throw_format_error(
- "The format-spec precision field doesn't contain a value or arg-id");
-
- __format::__parse_number_result __r =
- __format::__parse_number(__begin, __end);
- __precision = __r.__value;
- __precision_as_arg = 0;
- return __r.__ptr;
- }
-
- _LIBCPP_HIDE_FROM_ABI constexpr void __substitute_precision_arg_id(
- auto __arg) {
- _LIBCPP_ASSERT(
- __precision_as_arg == 1 && __precision != __format::__number_max,
- "Substitute precision called when no substitution is required");
-
- // The clearing of the flag isn't required but looks better when debugging
- // the code.
- __precision_as_arg = 0;
- __precision = __substitute_arg_id(__arg);
- }
-};
-
-template <class _CharT>
-_LIBCPP_HIDE_FROM_ABI constexpr const _CharT*
-__parse_locale_specific_form(const _CharT* __begin, _Flags& __flags) noexcept {
- if (*__begin == _CharT('L')) {
- __flags.__locale_specific_form = true;
- ++__begin;
- }
-
- return __begin;
-}
-
-template <class _CharT>
-_LIBCPP_HIDE_FROM_ABI constexpr const _CharT*
-__parse_type(const _CharT* __begin, _Flags& __flags) {
-
- // Determines the type. It does not validate whether the selected type is
- // valid. Most formatters have optional fields that are only allowed for
- // certain types. These parsers need to do validation after the type has
- // been parsed. So its easier to implement the validation for all types in
- // the specific parse function.
- switch (*__begin) {
- case 'A':
- __flags.__type = _Flags::_Type::__float_hexadecimal_upper_case;
- break;
- case 'B':
- __flags.__type = _Flags::_Type::__binary_upper_case;
- break;
- case 'E':
- __flags.__type = _Flags::_Type::__scientific_upper_case;
- break;
- case 'F':
- __flags.__type = _Flags::_Type::__fixed_upper_case;
- break;
- case 'G':
- __flags.__type = _Flags::_Type::__general_upper_case;
- break;
- case 'X':
- __flags.__type = _Flags::_Type::__hexadecimal_upper_case;
- break;
- case 'a':
- __flags.__type = _Flags::_Type::__float_hexadecimal_lower_case;
- break;
- case 'b':
- __flags.__type = _Flags::_Type::__binary_lower_case;
- break;
- case 'c':
- __flags.__type = _Flags::_Type::__char;
- break;
- case 'd':
- __flags.__type = _Flags::_Type::__decimal;
- break;
- case 'e':
- __flags.__type = _Flags::_Type::__scientific_lower_case;
- break;
- case 'f':
- __flags.__type = _Flags::_Type::__fixed_lower_case;
- break;
- case 'g':
- __flags.__type = _Flags::_Type::__general_lower_case;
- break;
- case 'o':
- __flags.__type = _Flags::_Type::__octal;
- break;
- case 'p':
- __flags.__type = _Flags::_Type::__pointer;
- break;
- case 's':
- __flags.__type = _Flags::_Type::__string;
- break;
- case 'x':
- __flags.__type = _Flags::_Type::__hexadecimal_lower_case;
- break;
- default:
- return __begin;
- }
- return ++__begin;
-}
-
-/**
- * Process the parsed alignment and zero-padding state of arithmetic types.
- *
- * [format.string.std]/13
- * If the 0 character and an align option both appear, the 0 character is
- * ignored.
- *
- * For the formatter a @ref __default alignment means zero-padding.
- */
-_LIBCPP_HIDE_FROM_ABI constexpr void __process_arithmetic_alignment(_Flags& __flags) {
- __flags.__zero_padding &= __flags.__alignment == _Flags::_Alignment::__default;
- if (!__flags.__zero_padding && __flags.__alignment == _Flags::_Alignment::__default)
- __flags.__alignment = _Flags::_Alignment::__right;
+ __format_arg);
}
-/**
- * The parser for the std-format-spec.
- *
- * [format.string.std]/1 specifies the std-format-spec:
- * fill-and-align sign # 0 width precision L type
- *
- * All these fields are optional. Whether these fields can be used depend on:
- * - The type supplied to the format string.
- * E.g. A string never uses the sign field so the field may not be set.
- * This constrain is validated by the parsers in this file.
- * - The supplied value for the optional type field.
- * E.g. A int formatted as decimal uses the sign field.
- * When formatted as a char the sign field may no longer be set.
- * This constrain isn't validated by the parsers in this file.
- *
- * The base classes are ordered to minimize the amount of padding.
- *
- * This implements the parser for the string types.
- */
-template <class _CharT>
-class _LIBCPP_TEMPLATE_VIS __parser_string
- : public __parser_width, // provides __width(|as_arg)
- public __parser_precision, // provides __precision(|as_arg)
- public __parser_fill_align<_CharT>, // provides __fill and uses __flags
- public _Flags // provides __flags
-{
-public:
- using char_type = _CharT;
-
- _LIBCPP_HIDE_FROM_ABI constexpr __parser_string() {
- this->__alignment = _Flags::_Alignment::__left;
- }
-
- /**
- * The low-level std-format-spec parse function.
- *
- * @pre __begin points at the beginning of the std-format-spec. This means
- * directly after the ':'.
- * @pre The std-format-spec parses the entire input, or the first unmatched
- * character is a '}'.
- *
- * @returns The iterator pointing at the last parsed character.
- */
- _LIBCPP_HIDE_FROM_ABI constexpr auto parse(auto& __parse_ctx)
- -> decltype(__parse_ctx.begin()) {
- auto __it = __parse(__parse_ctx);
- __process_display_type();
- return __it;
- }
-
-private:
- /**
- * Parses the std-format-spec.
- *
- * @throws __throw_format_error When @a __parse_ctx contains an ill-formed
- * std-format-spec.
- *
- * @returns An iterator to the end of input or point at the closing '}'.
- */
- _LIBCPP_HIDE_FROM_ABI constexpr auto __parse(auto& __parse_ctx)
- -> decltype(__parse_ctx.begin()) {
-
- auto __begin = __parse_ctx.begin();
- auto __end = __parse_ctx.end();
- if (__begin == __end)
- return __begin;
-
- __begin = __parser_fill_align<_CharT>::__parse(__begin, __end,
- static_cast<_Flags&>(*this));
- if (__begin == __end)
- return __begin;
-
- __begin = __parser_width::__parse(__begin, __end, __parse_ctx);
- if (__begin == __end)
- return __begin;
-
- __begin = __parser_precision::__parse(__begin, __end, __parse_ctx);
- if (__begin == __end)
- return __begin;
-
- __begin = __parse_type(__begin, static_cast<_Flags&>(*this));
-
- if (__begin != __end && *__begin != _CharT('}'))
- __throw_format_error(
- "The format-spec should consume the input or end with a '}'");
-
- return __begin;
- }
-
- /** Processes the parsed std-format-spec based on the parsed display type. */
- _LIBCPP_HIDE_FROM_ABI constexpr void __process_display_type() {
- switch (this->__type) {
- case _Flags::_Type::__default:
- case _Flags::_Type::__string:
- break;
-
- default:
- __throw_format_error("The format-spec type has a type not supported for "
- "a string argument");
- }
- }
-};
-
-/**
- * The parser for the std-format-spec.
- *
- * This implements the parser for the integral types. This includes the
- * character type and boolean type.
- *
- * See @ref __parser_string.
- */
-template <class _CharT>
-class _LIBCPP_TEMPLATE_VIS __parser_integral
- : public __parser_width, // provides __width(|as_arg)
- public __parser_fill_align<_CharT>, // provides __fill and uses __flags
- public _Flags // provides __flags
-{
-public:
- using char_type = _CharT;
-
-protected:
- /**
- * The low-level std-format-spec parse function.
- *
- * @pre __begin points at the beginning of the std-format-spec. This means
- * directly after the ':'.
- * @pre The std-format-spec parses the entire input, or the first unmatched
- * character is a '}'.
- *
- * @returns The iterator pointing at the last parsed character.
- */
- _LIBCPP_HIDE_FROM_ABI constexpr auto __parse(auto& __parse_ctx)
- -> decltype(__parse_ctx.begin()) {
- auto __begin = __parse_ctx.begin();
- auto __end = __parse_ctx.end();
- if (__begin == __end)
- return __begin;
-
- __begin = __parser_fill_align<_CharT>::__parse(__begin, __end,
- static_cast<_Flags&>(*this));
- if (__begin == __end)
- return __begin;
-
- __begin = __parse_sign(__begin, static_cast<_Flags&>(*this));
- if (__begin == __end)
- return __begin;
-
- __begin = __parse_alternate_form(__begin, static_cast<_Flags&>(*this));
- if (__begin == __end)
- return __begin;
-
- __begin = __parse_zero_padding(__begin, static_cast<_Flags&>(*this));
- if (__begin == __end)
- return __begin;
-
- __begin = __parser_width::__parse(__begin, __end, __parse_ctx);
- if (__begin == __end)
- return __begin;
-
- __begin =
- __parse_locale_specific_form(__begin, static_cast<_Flags&>(*this));
- if (__begin == __end)
- return __begin;
-
- __begin = __parse_type(__begin, static_cast<_Flags&>(*this));
-
- if (__begin != __end && *__begin != _CharT('}'))
- __throw_format_error(
- "The format-spec should consume the input or end with a '}'");
-
- return __begin;
- }
-
- /** Handles the post-parsing updates for the integer types. */
- _LIBCPP_HIDE_FROM_ABI constexpr void __handle_integer() noexcept {
- __process_arithmetic_alignment(static_cast<_Flags&>(*this));
- }
-
- /**
- * Handles the post-parsing updates for the character types.
- *
- * Sets the alignment and validates the format flags set for a character type.
- *
- * At the moment the validation for a character and a Boolean behave the
- * same, but this may change in the future.
- * Specifically at the moment the locale-specific form is allowed for the
- * char output type, but it has no effect on the output.
- */
- _LIBCPP_HIDE_FROM_ABI constexpr void __handle_char() { __handle_bool(); }
-
- /**
- * Handles the post-parsing updates for the Boolean types.
- *
- * Sets the alignment and validates the format flags set for a Boolean type.
- */
- _LIBCPP_HIDE_FROM_ABI constexpr void __handle_bool() {
- if (this->__sign != _Flags::_Sign::__default)
- __throw_format_error("A sign field isn't allowed in this format-spec");
-
- if (this->__alternate_form)
- __throw_format_error(
- "An alternate form field isn't allowed in this format-spec");
-
- if (this->__zero_padding)
- __throw_format_error(
- "A zero-padding field isn't allowed in this format-spec");
-
- if (this->__alignment == _Flags::_Alignment::__default)
- this->__alignment = _Flags::_Alignment::__left;
- }
-};
-
-/**
- * The parser for the std-format-spec.
- *
- * This implements the parser for the floating-point types.
- *
- * See @ref __parser_string.
- */
-template <class _CharT>
-class _LIBCPP_TEMPLATE_VIS __parser_floating_point
- : public __parser_width, // provides __width(|as_arg)
- public __parser_precision, // provides __precision(|as_arg)
- public __parser_fill_align<_CharT>, // provides __fill and uses __flags
- public _Flags // provides __flags
-{
-public:
- using char_type = _CharT;
-
- /**
- * The low-level std-format-spec parse function.
- *
- * @pre __begin points at the beginning of the std-format-spec. This means
- * directly after the ':'.
- * @pre The std-format-spec parses the entire input, or the first unmatched
- * character is a '}'.
- *
- * @returns The iterator pointing at the last parsed character.
- */
- _LIBCPP_HIDE_FROM_ABI constexpr auto parse(auto& __parse_ctx)
- -> decltype(__parse_ctx.begin()) {
- auto __it = __parse(__parse_ctx);
- __process_arithmetic_alignment(static_cast<_Flags&>(*this));
- __process_display_type();
- return __it;
- }
-protected:
- /**
- * The low-level std-format-spec parse function.
- *
- * @pre __begin points at the beginning of the std-format-spec. This means
- * directly after the ':'.
- * @pre The std-format-spec parses the entire input, or the first unmatched
- * character is a '}'.
- *
- * @returns The iterator pointing at the last parsed character.
- */
- _LIBCPP_HIDE_FROM_ABI constexpr auto __parse(auto& __parse_ctx)
- -> decltype(__parse_ctx.begin()) {
- auto __begin = __parse_ctx.begin();
- auto __end = __parse_ctx.end();
- if (__begin == __end)
- return __begin;
-
- __begin = __parser_fill_align<_CharT>::__parse(__begin, __end,
- static_cast<_Flags&>(*this));
- if (__begin == __end)
- return __begin;
-
- __begin = __parse_sign(__begin, static_cast<_Flags&>(*this));
- if (__begin == __end)
- return __begin;
-
- __begin = __parse_alternate_form(__begin, static_cast<_Flags&>(*this));
- if (__begin == __end)
- return __begin;
-
- __begin = __parse_zero_padding(__begin, static_cast<_Flags&>(*this));
- if (__begin == __end)
- return __begin;
-
- __begin = __parser_width::__parse(__begin, __end, __parse_ctx);
- if (__begin == __end)
- return __begin;
-
- __begin = __parser_precision::__parse(__begin, __end, __parse_ctx);
- if (__begin == __end)
- return __begin;
-
- __begin =
- __parse_locale_specific_form(__begin, static_cast<_Flags&>(*this));
- if (__begin == __end)
- return __begin;
-
- __begin = __parse_type(__begin, static_cast<_Flags&>(*this));
-
- if (__begin != __end && *__begin != _CharT('}'))
- __throw_format_error(
- "The format-spec should consume the input or end with a '}'");
-
- return __begin;
- }
-
- /** Processes the parsed std-format-spec based on the parsed display type. */
- _LIBCPP_HIDE_FROM_ABI constexpr void __process_display_type() {
- switch (this->__type) {
- case _Flags::_Type::__default:
- // When no precision specified then it keeps default since that
- // formatting differs from the other types.
- if (this->__has_precision_field())
- this->__type = _Flags::_Type::__general_lower_case;
- break;
- case _Flags::_Type::__float_hexadecimal_lower_case:
- case _Flags::_Type::__float_hexadecimal_upper_case:
- // Precision specific behavior will be handled later.
- break;
- case _Flags::_Type::__scientific_lower_case:
- case _Flags::_Type::__scientific_upper_case:
- case _Flags::_Type::__fixed_lower_case:
- case _Flags::_Type::__fixed_upper_case:
- case _Flags::_Type::__general_lower_case:
- case _Flags::_Type::__general_upper_case:
- if (!this->__has_precision_field()) {
- // Set the default precision for the call to to_chars.
- this->__precision = 6;
- this->__precision_as_arg = false;
- }
- break;
-
- default:
- __throw_format_error("The format-spec type has a type not supported for "
- "a floating-point argument");
- }
- }
-};
-
-/**
- * The parser for the std-format-spec.
- *
- * This implements the parser for the pointer types.
- *
- * See @ref __parser_string.
- */
-template <class _CharT>
-class _LIBCPP_TEMPLATE_VIS __parser_pointer : public __parser_width, // provides __width(|as_arg)
- public __parser_fill_align<_CharT>, // provides __fill and uses __flags
- public _Flags // provides __flags
-{
-public:
- using char_type = _CharT;
-
- _LIBCPP_HIDE_FROM_ABI constexpr __parser_pointer() {
- // Implements LWG3612 Inconsistent pointer alignment in std::format.
- // The issue's current status is "Tentatively Ready" and libc++ status is
- // still experimental.
- //
- // TODO FMT Validate this with the final resolution of LWG3612.
- this->__alignment = _Flags::_Alignment::__right;
- }
-
- /**
- * The low-level std-format-spec parse function.
- *
- * @pre __begin points at the beginning of the std-format-spec. This means
- * directly after the ':'.
- * @pre The std-format-spec parses the entire input, or the first unmatched
- * character is a '}'.
- *
- * @returns The iterator pointing at the last parsed character.
- */
- _LIBCPP_HIDE_FROM_ABI constexpr auto parse(auto& __parse_ctx) -> decltype(__parse_ctx.begin()) {
- auto __it = __parse(__parse_ctx);
- __process_display_type();
- return __it;
- }
-
-protected:
- /**
- * The low-level std-format-spec parse function.
- *
- * @pre __begin points at the beginning of the std-format-spec. This means
- * directly after the ':'.
- * @pre The std-format-spec parses the entire input, or the first unmatched
- * character is a '}'.
- *
- * @returns The iterator pointing at the last parsed character.
- */
- _LIBCPP_HIDE_FROM_ABI constexpr auto __parse(auto& __parse_ctx) -> decltype(__parse_ctx.begin()) {
- auto __begin = __parse_ctx.begin();
- auto __end = __parse_ctx.end();
- if (__begin == __end)
- return __begin;
-
- __begin = __parser_fill_align<_CharT>::__parse(__begin, __end, static_cast<_Flags&>(*this));
- if (__begin == __end)
- return __begin;
-
- // An integer presentation type isn't defined in the Standard.
- // Since a pointer is formatted as an integer it can be argued it's an
- // integer presentation type. However there are two LWG-issues asserting it
- // isn't an integer presentation type:
- // - LWG3612 Inconsistent pointer alignment in std::format
- // - LWG3644 std::format does not define "integer presentation type"
- //
- // There's a paper to make additional clarifications on the status of
- // formatting pointers and proposes additional fields to be valid. That
- // paper hasn't been reviewed by the Committee yet.
- // - P2510 Formatting pointers
- //
- // The current implementation assumes formatting pointers isn't covered by
- // "integer presentation type".
- // TODO FMT Apply the LWG-issues/papers after approval/rejection by the Committee.
-
- __begin = __parser_width::__parse(__begin, __end, __parse_ctx);
- if (__begin == __end)
- return __begin;
-
- __begin = __parse_type(__begin, static_cast<_Flags&>(*this));
-
- if (__begin != __end && *__begin != _CharT('}'))
- __throw_format_error("The format-spec should consume the input or end with a '}'");
-
- return __begin;
- }
-
- /** Processes the parsed std-format-spec based on the parsed display type. */
- _LIBCPP_HIDE_FROM_ABI constexpr void __process_display_type() {
- switch (this->__type) {
- case _Flags::_Type::__default:
- this->__type = _Flags::_Type::__pointer;
- break;
- case _Flags::_Type::__pointer:
- break;
- default:
- __throw_format_error("The format-spec type has a type not supported for a pointer argument");
- }
- }
-};
-
/** Helper struct returned from @ref __get_string_alignment. */
template <class _CharT>
struct _LIBCPP_TEMPLATE_VIS __string_alignment {
@@ -1406,6 +568,13 @@ inline constexpr __fields __fields_integral{
.__zero_padding_ = true,
.__locale_specific_form_ = true,
.__type_ = true};
+inline constexpr __fields __fields_floating_point{
+ .__sign_ = true,
+ .__alternate_form_ = true,
+ .__zero_padding_ = true,
+ .__precision_ = true,
+ .__locale_specific_form_ = true,
+ .__type_ = true};
inline constexpr __fields __fields_string{.__precision_ = true, .__type_ = true};
inline constexpr __fields __fields_pointer{.__type_ = true};
@@ -1872,17 +1041,9 @@ _LIBCPP_HIDE_FROM_ABI constexpr void __process_display_type_char(__parser<_CharT
}
template <class _CharT>
-_LIBCPP_HIDE_FROM_ABI constexpr void __process_display_type_integer(__parser<_CharT>& __parser) {
- if (__parser.__alignment_ == __alignment::__default)
- __parser.__alignment_ = __alignment::__right;
-}
-
-template <class _CharT>
_LIBCPP_HIDE_FROM_ABI constexpr void __process_parsed_bool(__parser<_CharT>& __parser) {
switch (__parser.__type_) {
case __format_spec::__type::__default:
- __parser.__type_ = __format_spec::__type::__string;
- [[fallthrough]];
case __format_spec::__type::__string:
__format_spec::__process_display_type_bool_string(__parser);
break;
@@ -1893,7 +1054,6 @@ _LIBCPP_HIDE_FROM_ABI constexpr void __process_parsed_bool(__parser<_CharT>& __p
case __format_spec::__type::__decimal:
case __format_spec::__type::__hexadecimal_lower_case:
case __format_spec::__type::__hexadecimal_upper_case:
- __process_display_type_integer(__parser);
break;
default:
@@ -1905,8 +1065,6 @@ template <class _CharT>
_LIBCPP_HIDE_FROM_ABI constexpr void __process_parsed_char(__parser<_CharT>& __parser) {
switch (__parser.__type_) {
case __format_spec::__type::__default:
- __parser.__type_ = __format_spec::__type::__char;
- [[fallthrough]];
case __format_spec::__type::__char:
__format_spec::__process_display_type_char(__parser);
break;
@@ -1917,7 +1075,6 @@ _LIBCPP_HIDE_FROM_ABI constexpr void __process_parsed_char(__parser<_CharT>& __p
case __format_spec::__type::__decimal:
case __format_spec::__type::__hexadecimal_lower_case:
case __format_spec::__type::__hexadecimal_upper_case:
- __format_spec::__process_display_type_integer(__parser);
break;
default:
@@ -1929,15 +1086,12 @@ template <class _CharT>
_LIBCPP_HIDE_FROM_ABI constexpr void __process_parsed_integer(__parser<_CharT>& __parser) {
switch (__parser.__type_) {
case __format_spec::__type::__default:
- __parser.__type_ = __format_spec::__type::__decimal;
- [[fallthrough]];
case __format_spec::__type::__binary_lower_case:
case __format_spec::__type::__binary_upper_case:
case __format_spec::__type::__octal:
case __format_spec::__type::__decimal:
case __format_spec::__type::__hexadecimal_lower_case:
case __format_spec::__type::__hexadecimal_upper_case:
- __format_spec::__process_display_type_integer(__parser);
break;
case __format_spec::__type::__char:
@@ -1949,6 +1103,35 @@ _LIBCPP_HIDE_FROM_ABI constexpr void __process_parsed_integer(__parser<_CharT>&
}
}
+template <class _CharT>
+_LIBCPP_HIDE_FROM_ABI constexpr void __process_parsed_floating_point(__parser<_CharT>& __parser) {
+ switch (__parser.__type_) {
+ case __format_spec::__type::__default:
+ // When no precision specified then it keeps default since that
+ // formatting differs from the other types.
+ if (__parser.__precision_as_arg_ || __parser.__precision_ != -1)
+ __parser.__type_ = __format_spec::__type::__general_lower_case;
+ break;
+ case __format_spec::__type::__hexfloat_lower_case:
+ case __format_spec::__type::__hexfloat_upper_case:
+ // Precision specific behavior will be handled later.
+ break;
+ case __format_spec::__type::__scientific_lower_case:
+ case __format_spec::__type::__scientific_upper_case:
+ case __format_spec::__type::__fixed_lower_case:
+ case __format_spec::__type::__fixed_upper_case:
+ case __format_spec::__type::__general_lower_case:
+ case __format_spec::__type::__general_upper_case:
+ if (!__parser.__precision_as_arg_ && __parser.__precision_ == -1)
+ // Set the default precision for the call to to_chars.
+ __parser.__precision_ = 6;
+ break;
+
+ default:
+ std::__throw_format_error("The format-spec type has a type not supported for a floating-point argument");
+ }
+}
+
_LIBCPP_HIDE_FROM_ABI constexpr void __process_display_type_pointer(__format_spec::__type __type) {
switch (__type) {
case __format_spec::__type::__default: