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 <__charconv/to_chars_integral.h>
14 #include <__charconv/to_chars_result.h>
15 #include <__charconv/traits.h>
16 #include <__concepts/arithmetic.h>
17 #include <__concepts/same_as.h>
19 #include <__format/concepts.h>
20 #include <__format/format_error.h>
21 #include <__format/formatter_output.h>
22 #include <__format/parser_std_format_spec.h>
23 #include <__system_error/errc.h>
24 #include <__type_traits/make_unsigned.h>
25 #include <__utility/unreachable.h>
29 #include <string_view>
31 #ifndef _LIBCPP_HAS_NO_LOCALIZATION
35 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
36 # pragma GCC system_header
40 #include <__undef_macros>
42 _LIBCPP_BEGIN_NAMESPACE_STD
44 #if _LIBCPP_STD_VER >= 20
46 namespace __formatter
{
52 _LIBCPP_HIDE_FROM_ABI
inline char* __insert_sign(char* __buf
, bool __negative
, __format_spec::__sign __sign
) {
57 case __format_spec::__sign::__default
:
58 case __format_spec::__sign::__minus
:
61 case __format_spec::__sign::__plus
:
64 case __format_spec::__sign::__space
:
73 * Determines the required grouping based on the size of the input.
75 * The grouping's last element will be repeated. For simplicity this repeating
76 * is unwrapped based on the length of the input. (When the input is short some
77 * groups are not processed.)
79 * @returns The size of the groups to write. This means the number of
80 * separator characters written is size() - 1.
82 * @note Since zero-sized groups cause issues they are silently ignored.
84 * @note The grouping field of the locale is always a @c std::string,
85 * regardless whether the @c std::numpunct's type is @c char or @c wchar_t.
87 _LIBCPP_HIDE_FROM_ABI
inline string
__determine_grouping(ptrdiff_t __size
, const string
& __grouping
) {
88 _LIBCPP_ASSERT_UNCATEGORIZED(!__grouping
.empty() && __size
> __grouping
[0],
89 "The slow grouping formatting is used while there will be no "
90 "separators written");
92 auto __end
= __grouping
.end() - 1;
93 auto __ptr
= __grouping
.begin();
98 __r
.push_back(*__ptr
);
100 // __size <= 0 so the value pushed will be <= *__ptr.
101 __r
.push_back(*__ptr
+ __size
);
105 // Proceed to the next group.
106 if (__ptr
!= __end
) {
109 // Skip grouping with a width of 0.
110 } while (*__ptr
== 0 && __ptr
!= __end
);
114 __libcpp_unreachable();
121 template <__fmt_char_type _CharT
>
122 _LIBCPP_HIDE_FROM_ABI
auto __format_char(
123 integral
auto __value
,
124 output_iterator
<const _CharT
&> auto __out_it
,
125 __format_spec::__parsed_specifications
<_CharT
> __specs
) -> decltype(__out_it
) {
126 using _Tp
= decltype(__value
);
127 if constexpr (!same_as
<_CharT
, _Tp
>) {
128 // cmp_less and cmp_greater can't be used for character types.
129 if constexpr (signed_integral
<_CharT
> == signed_integral
<_Tp
>) {
130 if (__value
< numeric_limits
<_CharT
>::min() || __value
> numeric_limits
<_CharT
>::max())
131 std::__throw_format_error("Integral value outside the range of the char type");
132 } else if constexpr (signed_integral
<_CharT
>) {
133 // _CharT is signed _Tp is unsigned
134 if (__value
> static_cast<make_unsigned_t
<_CharT
>>(numeric_limits
<_CharT
>::max()))
135 std::__throw_format_error("Integral value outside the range of the char type");
137 // _CharT is unsigned _Tp is signed
138 if (__value
< 0 || static_cast<make_unsigned_t
<_Tp
>>(__value
) > numeric_limits
<_CharT
>::max())
139 std::__throw_format_error("Integral value outside the range of the char type");
143 const auto __c
= static_cast<_CharT
>(__value
);
144 return __formatter::__write(_VSTD::addressof(__c
), _VSTD::addressof(__c
) + 1, _VSTD::move(__out_it
), __specs
);
151 /** Wrapper around @ref to_chars, returning the output pointer. */
152 template <integral _Tp
>
153 _LIBCPP_HIDE_FROM_ABI
char* __to_buffer(char* __first
, char* __last
, _Tp __value
, int __base
) {
154 // TODO FMT Evaluate code overhead due to not calling the internal function
155 // directly. (Should be zero overhead.)
156 to_chars_result __r
= _VSTD::to_chars(__first
, __last
, __value
, __base
);
157 _LIBCPP_ASSERT_UNCATEGORIZED(__r
.ec
== errc(0), "Internal buffer too small");
162 * Helper to determine the buffer size to output a integer in Base @em x.
164 * There are several overloads for the supported bases. The function uses the
165 * base as template argument so it can be used in a constant expression.
167 template <unsigned_integral _Tp
, size_t _Base
>
168 consteval
size_t __buffer_size() noexcept
171 return numeric_limits
<_Tp
>::digits
// The number of binary digits.
172 + 2 // Reserve space for the '0[Bb]' prefix.
173 + 1; // Reserve space for the sign.
176 template <unsigned_integral _Tp
, size_t _Base
>
177 consteval
size_t __buffer_size() noexcept
180 return numeric_limits
<_Tp
>::digits
// The number of binary digits.
181 / 3 // Adjust to octal.
182 + 1 // Turn floor to ceil.
183 + 1 // Reserve space for the '0' prefix.
184 + 1; // Reserve space for the sign.
187 template <unsigned_integral _Tp
, size_t _Base
>
188 consteval
size_t __buffer_size() noexcept
189 requires(_Base
== 10)
191 return numeric_limits
<_Tp
>::digits10
// The floored value.
192 + 1 // Turn floor to ceil.
193 + 1; // Reserve space for the sign.
196 template <unsigned_integral _Tp
, size_t _Base
>
197 consteval
size_t __buffer_size() noexcept
198 requires(_Base
== 16)
200 return numeric_limits
<_Tp
>::digits
// The number of binary digits.
201 / 4 // Adjust to hexadecimal.
202 + 2 // Reserve space for the '0[Xx]' prefix.
203 + 1; // Reserve space for the sign.
206 template <class _OutIt
, class _CharT
>
207 _LIBCPP_HIDE_FROM_ABI _OutIt
__write_using_decimal_separators(_OutIt __out_it
, const char* __begin
, const char* __first
,
208 const char* __last
, string
&& __grouping
, _CharT __sep
,
209 __format_spec::__parsed_specifications
<_CharT
> __specs
) {
210 int __size
= (__first
- __begin
) + // [sign][prefix]
211 (__last
- __first
) + // data
212 (__grouping
.size() - 1); // number of separator characters
214 __padding_size_result __padding
= {0, 0};
215 if (__specs
.__alignment_
== __format_spec::__alignment::__zero_padding
) {
216 // Write [sign][prefix].
217 __out_it
= __formatter::__copy(__begin
, __first
, _VSTD::move(__out_it
));
219 if (__specs
.__width_
> __size
) {
220 // Write zero padding.
221 __padding
.__before_
= __specs
.__width_
- __size
;
222 __out_it
= __formatter::__fill(_VSTD::move(__out_it
), __specs
.__width_
- __size
, _CharT('0'));
225 if (__specs
.__width_
> __size
) {
226 // Determine padding and write padding.
227 __padding
= __formatter::__padding_size(__size
, __specs
.__width_
, __specs
.__alignment_
);
229 __out_it
= __formatter::__fill(_VSTD::move(__out_it
), __padding
.__before_
, __specs
.__fill_
);
231 // Write [sign][prefix].
232 __out_it
= __formatter::__copy(__begin
, __first
, _VSTD::move(__out_it
));
235 auto __r
= __grouping
.rbegin();
236 auto __e
= __grouping
.rend() - 1;
237 _LIBCPP_ASSERT_UNCATEGORIZED(__r
!= __e
, "The slow grouping formatting is used while "
238 "there will be no separators written.");
239 // The output is divided in small groups of numbers to write:
240 // - A group before the first separator.
241 // - A separator and a group, repeated for the number of separators.
242 // - A group after the last separator.
243 // This loop achieves that process by testing the termination condition
244 // midway in the loop.
246 // TODO FMT This loop evaluates the loop invariant `__parser.__type !=
247 // _Flags::_Type::__hexadecimal_upper_case` for every iteration. (This test
248 // happens in the __write call.) Benchmark whether making two loops and
249 // hoisting the invariant is worth the effort.
251 if (__specs
.__std_
.__type_
== __format_spec::__type::__hexadecimal_upper_case
) {
252 __last
= __first
+ *__r
;
253 __out_it
= __formatter::__transform(__first
, __last
, _VSTD::move(__out_it
), __hex_to_upper
);
256 __out_it
= __formatter::__copy(__first
, *__r
, _VSTD::move(__out_it
));
267 return __formatter::__fill(_VSTD::move(__out_it
), __padding
.__after_
, __specs
.__fill_
);
272 template <unsigned_integral _Tp
, class _CharT
, class _FormatContext
>
273 _LIBCPP_HIDE_FROM_ABI typename
_FormatContext::iterator
__format_integer(
275 _FormatContext
& __ctx
,
276 __format_spec::__parsed_specifications
<_CharT
> __specs
,
280 const char* __prefix
,
282 char* __first
= __formatter::__insert_sign(__begin
, __negative
, __specs
.__std_
.__sign_
);
283 if (__specs
.__std_
.__alternate_form_
&& __prefix
)
285 *__first
++ = *__prefix
++;
287 char* __last
= __formatter::__to_buffer(__first
, __end
, __value
, __base
);
289 # ifndef _LIBCPP_HAS_NO_LOCALIZATION
290 if (__specs
.__std_
.__locale_specific_form_
) {
291 const auto& __np
= std::use_facet
<numpunct
<_CharT
>>(__ctx
.locale());
292 string __grouping
= __np
.grouping();
293 ptrdiff_t __size
= __last
- __first
;
294 // Writing the grouped form has more overhead than the normal output
295 // routines. If there will be no separators written the locale-specific
296 // form is identical to the normal routine. Test whether to grouped form
298 if (!__grouping
.empty() && __size
> __grouping
[0])
299 return __formatter::__write_using_decimal_separators(
304 __formatter::__determine_grouping(__size
, __grouping
),
305 __np
.thousands_sep(),
309 auto __out_it
= __ctx
.out();
310 if (__specs
.__alignment_
!= __format_spec::__alignment::__zero_padding
)
313 // __buf contains [sign][prefix]data
314 // ^ location of __first
315 // The zero padding is done like:
316 // - Write [sign][prefix]
317 // - Write data right aligned with '0' as fill character.
318 __out_it
= __formatter::__copy(__begin
, __first
, _VSTD::move(__out_it
));
319 __specs
.__alignment_
= __format_spec::__alignment::__right
;
320 __specs
.__fill_
.__data
[0] = _CharT('0');
321 int32_t __size
= __first
- __begin
;
323 __specs
.__width_
-= _VSTD::min(__size
, __specs
.__width_
);
326 if (__specs
.__std_
.__type_
!= __format_spec::__type::__hexadecimal_upper_case
) [[likely
]]
327 return __formatter::__write(__first
, __last
, __ctx
.out(), __specs
);
329 return __formatter::__write_transformed(__first
, __last
, __ctx
.out(), __specs
, __formatter::__hex_to_upper
);
332 template <unsigned_integral _Tp
, class _CharT
, class _FormatContext
>
333 _LIBCPP_HIDE_FROM_ABI typename
_FormatContext::iterator
334 __format_integer(_Tp __value
,
335 _FormatContext
& __ctx
,
336 __format_spec::__parsed_specifications
<_CharT
> __specs
,
337 bool __negative
= false) {
338 switch (__specs
.__std_
.__type_
) {
339 case __format_spec::__type::__binary_lower_case
: {
340 array
<char, __formatter::__buffer_size
<decltype(__value
), 2>()> __array
;
341 return __formatter::__format_integer(__value
, __ctx
, __specs
, __negative
, __array
.begin(), __array
.end(), "0b", 2);
343 case __format_spec::__type::__binary_upper_case
: {
344 array
<char, __formatter::__buffer_size
<decltype(__value
), 2>()> __array
;
345 return __formatter::__format_integer(__value
, __ctx
, __specs
, __negative
, __array
.begin(), __array
.end(), "0B", 2);
347 case __format_spec::__type::__octal
: {
348 // Octal is special; if __value == 0 there's no prefix.
349 array
<char, __formatter::__buffer_size
<decltype(__value
), 8>()> __array
;
350 return __formatter::__format_integer(
351 __value
, __ctx
, __specs
, __negative
, __array
.begin(), __array
.end(), __value
!= 0 ? "0" : nullptr, 8);
353 case __format_spec::__type::__default
:
354 case __format_spec::__type::__decimal
: {
355 array
<char, __formatter::__buffer_size
<decltype(__value
), 10>()> __array
;
356 return __formatter::__format_integer(
357 __value
, __ctx
, __specs
, __negative
, __array
.begin(), __array
.end(), nullptr, 10);
359 case __format_spec::__type::__hexadecimal_lower_case
: {
360 array
<char, __formatter::__buffer_size
<decltype(__value
), 16>()> __array
;
361 return __formatter::__format_integer(__value
, __ctx
, __specs
, __negative
, __array
.begin(), __array
.end(), "0x", 16);
363 case __format_spec::__type::__hexadecimal_upper_case
: {
364 array
<char, __formatter::__buffer_size
<decltype(__value
), 16>()> __array
;
365 return __formatter::__format_integer(__value
, __ctx
, __specs
, __negative
, __array
.begin(), __array
.end(), "0X", 16);
368 _LIBCPP_ASSERT_UNCATEGORIZED(false, "The parse function should have validated the type");
369 __libcpp_unreachable();
373 template <signed_integral _Tp
, class _CharT
, class _FormatContext
>
374 _LIBCPP_HIDE_FROM_ABI typename
_FormatContext::iterator
375 __format_integer(_Tp __value
, _FormatContext
& __ctx
, __format_spec::__parsed_specifications
<_CharT
> __specs
) {
376 // Depending on the std-format-spec string the sign and the value
377 // might not be outputted together:
378 // - alternate form may insert a prefix string.
379 // - zero-padding may insert additional '0' characters.
380 // Therefore the value is processed as a positive unsigned value.
381 // The function @ref __insert_sign will a '-' when the value was negative.
382 auto __r
= std::__to_unsigned_like(__value
);
383 bool __negative
= __value
< 0;
385 __r
= std::__complement(__r
);
387 return __formatter::__format_integer(__r
, __ctx
, __specs
, __negative
);
391 // Formatter arithmetic (bool)
394 template <class _CharT
>
395 struct _LIBCPP_TEMPLATE_VIS __bool_strings
;
398 struct _LIBCPP_TEMPLATE_VIS __bool_strings
<char> {
399 static constexpr string_view __true
{"true"};
400 static constexpr string_view __false
{"false"};
403 # ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
405 struct _LIBCPP_TEMPLATE_VIS __bool_strings
<wchar_t> {
406 static constexpr wstring_view __true
{L
"true"};
407 static constexpr wstring_view __false
{L
"false"};
411 template <class _CharT
, class _FormatContext
>
412 _LIBCPP_HIDE_FROM_ABI typename
_FormatContext::iterator
413 __format_bool(bool __value
, _FormatContext
& __ctx
, __format_spec::__parsed_specifications
<_CharT
> __specs
) {
414 # ifndef _LIBCPP_HAS_NO_LOCALIZATION
415 if (__specs
.__std_
.__locale_specific_form_
) {
416 const auto& __np
= std::use_facet
<numpunct
<_CharT
>>(__ctx
.locale());
417 basic_string
<_CharT
> __str
= __value
? __np
.truename() : __np
.falsename();
418 return __formatter::__write_string_no_precision(basic_string_view
<_CharT
>{__str
}, __ctx
.out(), __specs
);
421 basic_string_view
<_CharT
> __str
=
422 __value
? __formatter::__bool_strings
<_CharT
>::__true
: __formatter::__bool_strings
<_CharT
>::__false
;
423 return __formatter::__write(__str
.begin(), __str
.end(), __ctx
.out(), __specs
);
426 } // namespace __formatter
428 #endif //_LIBCPP_STD_VER >= 20
430 _LIBCPP_END_NAMESPACE_STD
434 #endif // _LIBCPP___FORMAT_FORMATTER_INTEGRAL_H