2 //===----------------------------------------------------------------------===//
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP___FORMAT_FORMATTER_INTEGRAL_H
11 #define _LIBCPP___FORMAT_FORMATTER_INTEGRAL_H
13 #include <__concepts/arithmetic.h>
14 #include <__concepts/same_as.h>
16 #include <__format/format_error.h>
17 #include <__format/formatter.h> // for __char_type TODO FMT Move the concept?
18 #include <__format/formatter_output.h>
19 #include <__format/parser_std_format_spec.h>
20 #include <__utility/unreachable.h>
25 #ifndef _LIBCPP_HAS_NO_LOCALIZATION
29 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
30 # pragma GCC system_header
34 #include <__undef_macros>
36 _LIBCPP_BEGIN_NAMESPACE_STD
38 #if _LIBCPP_STD_VER > 17
40 namespace __formatter {
46 _LIBCPP_HIDE_FROM_ABI inline char* __insert_sign(char* __buf, bool __negative, __format_spec::__sign __sign) {
51 case __format_spec::__sign::__default:
52 case __format_spec::__sign::__minus:
55 case __format_spec::__sign::__plus:
58 case __format_spec::__sign::__space:
67 * Determines the required grouping based on the size of the input.
69 * The grouping's last element will be repeated. For simplicity this repeating
70 * is unwrapped based on the length of the input. (When the input is short some
71 * groups are not processed.)
73 * @returns The size of the groups to write. This means the number of
74 * separator characters written is size() - 1.
76 * @note Since zero-sized groups cause issues they are silently ignored.
78 * @note The grouping field of the locale is always a @c std::string,
79 * regardless whether the @c std::numpunct's type is @c char or @c wchar_t.
81 _LIBCPP_HIDE_FROM_ABI inline string __determine_grouping(ptrdiff_t __size, const string& __grouping) {
82 _LIBCPP_ASSERT(!__grouping.empty() && __size > __grouping[0],
83 "The slow grouping formatting is used while there will be no "
84 "separators written");
86 auto __end = __grouping.end() - 1;
87 auto __ptr = __grouping.begin();
92 __r.push_back(*__ptr);
94 // __size <= 0 so the value pushed will be <= *__ptr.
95 __r.push_back(*__ptr + __size);
99 // Proceed to the next group.
100 if (__ptr != __end) {
103 // Skip grouping with a width of 0.
104 } while (*__ptr == 0 && __ptr != __end);
108 __libcpp_unreachable();
115 template <__formatter::__char_type _CharT>
116 _LIBCPP_HIDE_FROM_ABI auto __format_char(
117 integral auto __value,
118 output_iterator<const _CharT&> auto __out_it,
119 __format_spec::__parsed_specifications<_CharT> __specs) -> decltype(__out_it) {
120 using _Tp = decltype(__value);
121 if constexpr (!same_as<_CharT, _Tp>) {
122 // cmp_less and cmp_greater can't be used for character types.
123 if constexpr (signed_integral<_CharT> == signed_integral<_Tp>) {
124 if (__value < numeric_limits<_CharT>::min() || __value > numeric_limits<_CharT>::max())
125 std::__throw_format_error("Integral value outside the range of the char type");
126 } else if constexpr (signed_integral<_CharT>) {
127 // _CharT is signed _Tp is unsigned
128 if (__value > static_cast<make_unsigned_t<_CharT>>(numeric_limits<_CharT>::max()))
129 std::__throw_format_error("Integral value outside the range of the char type");
131 // _CharT is unsigned _Tp is signed
132 if (__value < 0 || static_cast<make_unsigned_t<_Tp>>(__value) > numeric_limits<_CharT>::max())
133 std::__throw_format_error("Integral value outside the range of the char type");
137 const auto __c = static_cast<_CharT>(__value);
138 return __formatter::__write(_VSTD::addressof(__c), _VSTD::addressof(__c) + 1, _VSTD::move(__out_it), __specs);
145 /** Wrapper around @ref to_chars, returning the output pointer. */
146 template <integral _Tp>
147 _LIBCPP_HIDE_FROM_ABI char* __to_buffer(char* __first, char* __last, _Tp __value, int __base) {
148 // TODO FMT Evaluate code overhead due to not calling the internal function
149 // directly. (Should be zero overhead.)
150 to_chars_result __r = _VSTD::to_chars(__first, __last, __value, __base);
151 _LIBCPP_ASSERT(__r.ec == errc(0), "Internal buffer too small");
156 * Helper to determine the buffer size to output a integer in Base @em x.
158 * There are several overloads for the supported bases. The function uses the
159 * base as template argument so it can be used in a constant expression.
161 template <unsigned_integral _Tp, size_t _Base>
162 consteval size_t __buffer_size() noexcept
165 return numeric_limits<_Tp>::digits // The number of binary digits.
166 + 2 // Reserve space for the '0[Bb]' prefix.
167 + 1; // Reserve space for the sign.
170 template <unsigned_integral _Tp, size_t _Base>
171 consteval size_t __buffer_size() noexcept
174 return numeric_limits<_Tp>::digits // The number of binary digits.
175 / 3 // Adjust to octal.
176 + 1 // Turn floor to ceil.
177 + 1 // Reserve space for the '0' prefix.
178 + 1; // Reserve space for the sign.
181 template <unsigned_integral _Tp, size_t _Base>
182 consteval size_t __buffer_size() noexcept
183 requires(_Base == 10)
185 return numeric_limits<_Tp>::digits10 // The floored value.
186 + 1 // Turn floor to ceil.
187 + 1; // Reserve space for the sign.
190 template <unsigned_integral _Tp, size_t _Base>
191 consteval size_t __buffer_size() noexcept
192 requires(_Base == 16)
194 return numeric_limits<_Tp>::digits // The number of binary digits.
195 / 4 // Adjust to hexadecimal.
196 + 2 // Reserve space for the '0[Xx]' prefix.
197 + 1; // Reserve space for the sign.
200 template <unsigned_integral _Tp, class _CharT>
201 _LIBCPP_HIDE_FROM_ABI auto __format_integer(
204 __format_spec::__parsed_specifications<_CharT> __specs,
208 const char* __prefix,
209 int __base) -> decltype(__ctx.out()) {
210 char* __first = __formatter::__insert_sign(__begin, __negative, __specs.__std_.__sign_);
211 if (__specs.__std_.__alternate_form_ && __prefix)
213 *__first++ = *__prefix++;
215 char* __last = __formatter::__to_buffer(__first, __end, __value, __base);
217 # ifndef _LIBCPP_HAS_NO_LOCALIZATION
218 if (__specs.__std_.__locale_specific_form_) {
219 const auto& __np = use_facet<numpunct<_CharT>>(__ctx.locale());
220 string __grouping = __np.grouping();
221 ptrdiff_t __size = __last - __first;
222 // Writing the grouped form has more overhead than the normal output
223 // routines. If there will be no separators written the locale-specific
224 // form is identical to the normal routine. Test whether to grouped form
226 if (!__grouping.empty() && __size > __grouping[0])
227 return __formatter::__write_using_decimal_separators(
232 __formatter::__determine_grouping(__size, __grouping),
233 __np.thousands_sep(),
237 auto __out_it = __ctx.out();
238 if (__specs.__alignment_ != __format_spec::__alignment::__zero_padding)
241 // __buf contains [sign][prefix]data
242 // ^ location of __first
243 // The zero padding is done like:
244 // - Write [sign][prefix]
245 // - Write data right aligned with '0' as fill character.
246 __out_it = _VSTD::copy(__begin, __first, _VSTD::move(__out_it));
247 __specs.__alignment_ = __format_spec::__alignment::__right;
248 __specs.__fill_ = _CharT('0');
249 int32_t __size = __first - __begin;
251 __specs.__width_ -= _VSTD::min(__size, __specs.__width_);
254 if (__specs.__std_.__type_ != __format_spec::__type::__hexadecimal_upper_case) [[likely]]
255 return __formatter::__write(__first, __last, __ctx.out(), __specs);
257 return __formatter::__write_transformed(__first, __last, __ctx.out(), __specs, __formatter::__hex_to_upper);
260 template <unsigned_integral _Tp, class _CharT>
261 _LIBCPP_HIDE_FROM_ABI auto __format_integer(
262 _Tp __value, auto& __ctx, __format_spec::__parsed_specifications<_CharT> __specs, bool __negative = false)
263 -> decltype(__ctx.out()) {
264 switch (__specs.__std_.__type_) {
265 case __format_spec::__type::__binary_lower_case: {
266 array<char, __formatter::__buffer_size<decltype(__value), 2>()> __array;
267 return __formatter::__format_integer(__value, __ctx, __specs, __negative, __array.begin(), __array.end(), "0b", 2);
269 case __format_spec::__type::__binary_upper_case: {
270 array<char, __formatter::__buffer_size<decltype(__value), 2>()> __array;
271 return __formatter::__format_integer(__value, __ctx, __specs, __negative, __array.begin(), __array.end(), "0B", 2);
273 case __format_spec::__type::__octal: {
274 // Octal is special; if __value == 0 there's no prefix.
275 array<char, __formatter::__buffer_size<decltype(__value), 8>()> __array;
276 return __formatter::__format_integer(
277 __value, __ctx, __specs, __negative, __array.begin(), __array.end(), __value != 0 ? "0" : nullptr, 8);
279 case __format_spec::__type::__default:
280 case __format_spec::__type::__decimal: {
281 array<char, __formatter::__buffer_size<decltype(__value), 10>()> __array;
282 return __formatter::__format_integer(
283 __value, __ctx, __specs, __negative, __array.begin(), __array.end(), nullptr, 10);
285 case __format_spec::__type::__hexadecimal_lower_case: {
286 array<char, __formatter::__buffer_size<decltype(__value), 16>()> __array;
287 return __formatter::__format_integer(__value, __ctx, __specs, __negative, __array.begin(), __array.end(), "0x", 16);
289 case __format_spec::__type::__hexadecimal_upper_case: {
290 array<char, __formatter::__buffer_size<decltype(__value), 16>()> __array;
291 return __formatter::__format_integer(__value, __ctx, __specs, __negative, __array.begin(), __array.end(), "0X", 16);
294 _LIBCPP_ASSERT(false, "The parse function should have validated the type");
295 __libcpp_unreachable();
299 template <signed_integral _Tp, class _CharT>
300 _LIBCPP_HIDE_FROM_ABI auto
301 __format_integer(_Tp __value, auto& __ctx, __format_spec::__parsed_specifications<_CharT> __specs)
302 -> decltype(__ctx.out()) {
303 // Depending on the std-format-spec string the sign and the value
304 // might not be outputted together:
305 // - alternate form may insert a prefix string.
306 // - zero-padding may insert additional '0' characters.
307 // Therefore the value is processed as a positive unsigned value.
308 // The function @ref __insert_sign will a '-' when the value was negative.
309 auto __r = std::__to_unsigned_like(__value);
310 bool __negative = __value < 0;
312 __r = __complement(__r);
314 return __formatter::__format_integer(__r, __ctx, __specs, __negative);
318 // Formatter arithmetic (bool)
321 template <class _CharT>
322 struct _LIBCPP_TEMPLATE_VIS __bool_strings;
325 struct _LIBCPP_TEMPLATE_VIS __bool_strings<char> {
326 static constexpr string_view __true{"true"};
327 static constexpr string_view __false{"false"};
330 # ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
332 struct _LIBCPP_TEMPLATE_VIS __bool_strings<wchar_t> {
333 static constexpr wstring_view __true{L"true"};
334 static constexpr wstring_view __false{L"false"};
338 template <class _CharT>
339 _LIBCPP_HIDE_FROM_ABI auto
340 __format_bool(bool __value, auto& __ctx, __format_spec::__parsed_specifications<_CharT> __specs)
341 -> decltype(__ctx.out()) {
342 # ifndef _LIBCPP_HAS_NO_LOCALIZATION
343 if (__specs.__std_.__locale_specific_form_) {
344 const auto& __np = use_facet<numpunct<_CharT>>(__ctx.locale());
345 basic_string<_CharT> __str = __value ? __np.truename() : __np.falsename();
346 return __formatter::__write_unicode_no_precision(basic_string_view<_CharT>{__str}, __ctx.out(), __specs);
349 basic_string_view<_CharT> __str =
350 __value ? __formatter::__bool_strings<_CharT>::__true : __formatter::__bool_strings<_CharT>::__false;
351 return __formatter::__write(__str.begin(), __str.end(), __ctx.out(), __specs);
354 } // namespace __formatter
356 #endif //_LIBCPP_STD_VER > 17
358 _LIBCPP_END_NAMESPACE_STD
362 #endif // _LIBCPP___FORMAT_FORMATTER_INTEGRAL_H