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___CXX03___FORMAT_FORMATTER_INTEGRAL_H
11 #define _LIBCPP___CXX03___FORMAT_FORMATTER_INTEGRAL_H
13 #include <__cxx03/__charconv/to_chars_integral.h>
14 #include <__cxx03/__charconv/to_chars_result.h>
15 #include <__cxx03/__charconv/traits.h>
16 #include <__cxx03/__concepts/arithmetic.h>
17 #include <__cxx03/__concepts/same_as.h>
18 #include <__cxx03/__config>
19 #include <__cxx03/__format/concepts.h>
20 #include <__cxx03/__format/format_error.h>
21 #include <__cxx03/__format/formatter_output.h>
22 #include <__cxx03/__format/parser_std_format_spec.h>
23 #include <__cxx03/__iterator/concepts.h>
24 #include <__cxx03/__iterator/iterator_traits.h>
25 #include <__cxx03/__memory/pointer_traits.h>
26 #include <__cxx03/__system_error/errc.h>
27 #include <__cxx03/__type_traits/make_unsigned.h>
28 #include <__cxx03/__utility/unreachable.h>
29 #include <__cxx03/array>
30 #include <__cxx03/limits>
31 #include <__cxx03/string>
32 #include <__cxx03/string_view>
34 #ifndef _LIBCPP_HAS_NO_LOCALIZATION
35 # include <__cxx03/__locale>
38 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
39 # pragma GCC system_header
43 #include <__cxx03/__undef_macros>
45 _LIBCPP_BEGIN_NAMESPACE_STD
47 #if _LIBCPP_STD_VER >= 20
49 namespace __formatter
{
55 template <contiguous_iterator _Iterator
>
56 requires same_as
<char, iter_value_t
<_Iterator
>>
57 _LIBCPP_HIDE_FROM_ABI
inline _Iterator
__insert_sign(_Iterator __buf
, bool __negative
, __format_spec::__sign __sign
) {
62 case __format_spec::__sign::__default
:
63 case __format_spec::__sign::__minus
:
66 case __format_spec::__sign::__plus
:
69 case __format_spec::__sign::__space
:
78 * Determines the required grouping based on the size of the input.
80 * The grouping's last element will be repeated. For simplicity this repeating
81 * is unwrapped based on the length of the input. (When the input is short some
82 * groups are not processed.)
84 * @returns The size of the groups to write. This means the number of
85 * separator characters written is size() - 1.
87 * @note Since zero-sized groups cause issues they are silently ignored.
89 * @note The grouping field of the locale is always a @c std::string,
90 * regardless whether the @c std::numpunct's type is @c char or @c wchar_t.
92 _LIBCPP_HIDE_FROM_ABI
inline string
__determine_grouping(ptrdiff_t __size
, const string
& __grouping
) {
93 _LIBCPP_ASSERT_INTERNAL(!__grouping
.empty() && __size
> __grouping
[0],
94 "The slow grouping formatting is used while there will be no separators written");
96 auto __end
= __grouping
.end() - 1;
97 auto __ptr
= __grouping
.begin();
102 __r
.push_back(*__ptr
);
104 // __size <= 0 so the value pushed will be <= *__ptr.
105 __r
.push_back(*__ptr
+ __size
);
109 // Proceed to the next group.
110 if (__ptr
!= __end
) {
113 // Skip grouping with a width of 0.
114 } while (*__ptr
== 0 && __ptr
!= __end
);
118 __libcpp_unreachable();
125 template <__fmt_char_type _CharT
>
126 _LIBCPP_HIDE_FROM_ABI
auto
127 __format_char(integral
auto __value
,
128 output_iterator
<const _CharT
&> auto __out_it
,
129 __format_spec::__parsed_specifications
<_CharT
> __specs
) -> decltype(__out_it
) {
130 using _Tp
= decltype(__value
);
131 if constexpr (!same_as
<_CharT
, _Tp
>) {
132 // cmp_less and cmp_greater can't be used for character types.
133 if constexpr (signed_integral
<_CharT
> == signed_integral
<_Tp
>) {
134 if (__value
< numeric_limits
<_CharT
>::min() || __value
> numeric_limits
<_CharT
>::max())
135 std::__throw_format_error("Integral value outside the range of the char type");
136 } else if constexpr (signed_integral
<_CharT
>) {
137 // _CharT is signed _Tp is unsigned
138 if (__value
> static_cast<make_unsigned_t
<_CharT
>>(numeric_limits
<_CharT
>::max()))
139 std::__throw_format_error("Integral value outside the range of the char type");
141 // _CharT is unsigned _Tp is signed
142 if (__value
< 0 || static_cast<make_unsigned_t
<_Tp
>>(__value
) > numeric_limits
<_CharT
>::max())
143 std::__throw_format_error("Integral value outside the range of the char type");
147 const auto __c
= static_cast<_CharT
>(__value
);
148 return __formatter::__write(std::addressof(__c
), std::addressof(__c
) + 1, std::move(__out_it
), __specs
);
155 /** Wrapper around @ref to_chars, returning the output iterator. */
156 template <contiguous_iterator _Iterator
, integral _Tp
>
157 requires same_as
<char, iter_value_t
<_Iterator
>>
158 _LIBCPP_HIDE_FROM_ABI _Iterator
__to_buffer(_Iterator __first
, _Iterator __last
, _Tp __value
, int __base
) {
159 // TODO FMT Evaluate code overhead due to not calling the internal function
160 // directly. (Should be zero overhead.)
161 to_chars_result __r
= std::to_chars(std::to_address(__first
), std::to_address(__last
), __value
, __base
);
162 _LIBCPP_ASSERT_INTERNAL(__r
.ec
== errc(0), "Internal buffer too small");
163 auto __diff
= __r
.ptr
- std::to_address(__first
);
164 return __first
+ __diff
;
168 * Helper to determine the buffer size to output a integer in Base @em x.
170 * There are several overloads for the supported bases. The function uses the
171 * base as template argument so it can be used in a constant expression.
173 template <unsigned_integral _Tp
, size_t _Base
>
174 consteval
size_t __buffer_size() noexcept
177 return numeric_limits
<_Tp
>::digits
// The number of binary digits.
178 + 2 // Reserve space for the '0[Bb]' prefix.
179 + 1; // Reserve space for the sign.
182 template <unsigned_integral _Tp
, size_t _Base
>
183 consteval
size_t __buffer_size() noexcept
186 return numeric_limits
<_Tp
>::digits
// The number of binary digits.
187 / 3 // Adjust to octal.
188 + 1 // Turn floor to ceil.
189 + 1 // Reserve space for the '0' prefix.
190 + 1; // Reserve space for the sign.
193 template <unsigned_integral _Tp
, size_t _Base
>
194 consteval
size_t __buffer_size() noexcept
195 requires(_Base
== 10)
197 return numeric_limits
<_Tp
>::digits10
// The floored value.
198 + 1 // Turn floor to ceil.
199 + 1; // Reserve space for the sign.
202 template <unsigned_integral _Tp
, size_t _Base
>
203 consteval
size_t __buffer_size() noexcept
204 requires(_Base
== 16)
206 return numeric_limits
<_Tp
>::digits
// The number of binary digits.
207 / 4 // Adjust to hexadecimal.
208 + 2 // Reserve space for the '0[Xx]' prefix.
209 + 1; // Reserve space for the sign.
212 template <class _OutIt
, contiguous_iterator _Iterator
, class _CharT
>
213 requires same_as
<char, iter_value_t
<_Iterator
>>
214 _LIBCPP_HIDE_FROM_ABI _OutIt
__write_using_decimal_separators(
221 __format_spec::__parsed_specifications
<_CharT
> __specs
) {
222 int __size
= (__first
- __begin
) + // [sign][prefix]
223 (__last
- __first
) + // data
224 (__grouping
.size() - 1); // number of separator characters
226 __padding_size_result __padding
= {0, 0};
227 if (__specs
.__alignment_
== __format_spec::__alignment::__zero_padding
) {
228 // Write [sign][prefix].
229 __out_it
= __formatter::__copy(__begin
, __first
, std::move(__out_it
));
231 if (__specs
.__width_
> __size
) {
232 // Write zero padding.
233 __padding
.__before_
= __specs
.__width_
- __size
;
234 __out_it
= __formatter::__fill(std::move(__out_it
), __specs
.__width_
- __size
, _CharT('0'));
237 if (__specs
.__width_
> __size
) {
238 // Determine padding and write padding.
239 __padding
= __formatter::__padding_size(__size
, __specs
.__width_
, __specs
.__alignment_
);
241 __out_it
= __formatter::__fill(std::move(__out_it
), __padding
.__before_
, __specs
.__fill_
);
243 // Write [sign][prefix].
244 __out_it
= __formatter::__copy(__begin
, __first
, std::move(__out_it
));
247 auto __r
= __grouping
.rbegin();
248 auto __e
= __grouping
.rend() - 1;
249 _LIBCPP_ASSERT_INTERNAL(
250 __r
!= __e
, "The slow grouping formatting is used while there will be no separators written.");
251 // The output is divided in small groups of numbers to write:
252 // - A group before the first separator.
253 // - A separator and a group, repeated for the number of separators.
254 // - A group after the last separator.
255 // This loop achieves that process by testing the termination condition
256 // midway in the loop.
258 // TODO FMT This loop evaluates the loop invariant `__parser.__type !=
259 // _Flags::_Type::__hexadecimal_upper_case` for every iteration. (This test
260 // happens in the __write call.) Benchmark whether making two loops and
261 // hoisting the invariant is worth the effort.
263 if (__specs
.__std_
.__type_
== __format_spec::__type::__hexadecimal_upper_case
) {
264 __last
= __first
+ *__r
;
265 __out_it
= __formatter::__transform(__first
, __last
, std::move(__out_it
), __hex_to_upper
);
268 __out_it
= __formatter::__copy(__first
, *__r
, std::move(__out_it
));
279 return __formatter::__fill(std::move(__out_it
), __padding
.__after_
, __specs
.__fill_
);
282 template <unsigned_integral _Tp
, contiguous_iterator _Iterator
, class _CharT
, class _FormatContext
>
283 requires same_as
<char, iter_value_t
<_Iterator
>>
284 _LIBCPP_HIDE_FROM_ABI typename
_FormatContext::iterator
__format_integer(
286 _FormatContext
& __ctx
,
287 __format_spec::__parsed_specifications
<_CharT
> __specs
,
291 const char* __prefix
,
293 _Iterator __first
= __formatter::__insert_sign(__begin
, __negative
, __specs
.__std_
.__sign_
);
294 if (__specs
.__std_
.__alternate_form_
&& __prefix
)
296 *__first
++ = *__prefix
++;
298 _Iterator __last
= __formatter::__to_buffer(__first
, __end
, __value
, __base
);
300 # ifndef _LIBCPP_HAS_NO_LOCALIZATION
301 if (__specs
.__std_
.__locale_specific_form_
) {
302 const auto& __np
= std::use_facet
<numpunct
<_CharT
>>(__ctx
.locale());
303 string __grouping
= __np
.grouping();
304 ptrdiff_t __size
= __last
- __first
;
305 // Writing the grouped form has more overhead than the normal output
306 // routines. If there will be no separators written the locale-specific
307 // form is identical to the normal routine. Test whether to grouped form
309 if (!__grouping
.empty() && __size
> __grouping
[0])
310 return __formatter::__write_using_decimal_separators(
315 __formatter::__determine_grouping(__size
, __grouping
),
316 __np
.thousands_sep(),
320 auto __out_it
= __ctx
.out();
321 if (__specs
.__alignment_
!= __format_spec::__alignment::__zero_padding
)
324 // __buf contains [sign][prefix]data
325 // ^ location of __first
326 // The zero padding is done like:
327 // - Write [sign][prefix]
328 // - Write data right aligned with '0' as fill character.
329 __out_it
= __formatter::__copy(__begin
, __first
, std::move(__out_it
));
330 __specs
.__alignment_
= __format_spec::__alignment::__right
;
331 __specs
.__fill_
.__data
[0] = _CharT('0');
332 int32_t __size
= __first
- __begin
;
334 __specs
.__width_
-= std::min(__size
, __specs
.__width_
);
337 if (__specs
.__std_
.__type_
!= __format_spec::__type::__hexadecimal_upper_case
) [[likely
]]
338 return __formatter::__write(__first
, __last
, __ctx
.out(), __specs
);
340 return __formatter::__write_transformed(__first
, __last
, __ctx
.out(), __specs
, __formatter::__hex_to_upper
);
343 template <unsigned_integral _Tp
, class _CharT
, class _FormatContext
>
344 _LIBCPP_HIDE_FROM_ABI typename
_FormatContext::iterator
345 __format_integer(_Tp __value
,
346 _FormatContext
& __ctx
,
347 __format_spec::__parsed_specifications
<_CharT
> __specs
,
348 bool __negative
= false) {
349 switch (__specs
.__std_
.__type_
) {
350 case __format_spec::__type::__binary_lower_case
: {
351 array
<char, __formatter::__buffer_size
<decltype(__value
), 2>()> __array
;
352 return __formatter::__format_integer(__value
, __ctx
, __specs
, __negative
, __array
.begin(), __array
.end(), "0b", 2);
354 case __format_spec::__type::__binary_upper_case
: {
355 array
<char, __formatter::__buffer_size
<decltype(__value
), 2>()> __array
;
356 return __formatter::__format_integer(__value
, __ctx
, __specs
, __negative
, __array
.begin(), __array
.end(), "0B", 2);
358 case __format_spec::__type::__octal
: {
359 // Octal is special; if __value == 0 there's no prefix.
360 array
<char, __formatter::__buffer_size
<decltype(__value
), 8>()> __array
;
361 return __formatter::__format_integer(
362 __value
, __ctx
, __specs
, __negative
, __array
.begin(), __array
.end(), __value
!= 0 ? "0" : nullptr, 8);
364 case __format_spec::__type::__default
:
365 case __format_spec::__type::__decimal
: {
366 array
<char, __formatter::__buffer_size
<decltype(__value
), 10>()> __array
;
367 return __formatter::__format_integer(
368 __value
, __ctx
, __specs
, __negative
, __array
.begin(), __array
.end(), nullptr, 10);
370 case __format_spec::__type::__hexadecimal_lower_case
: {
371 array
<char, __formatter::__buffer_size
<decltype(__value
), 16>()> __array
;
372 return __formatter::__format_integer(__value
, __ctx
, __specs
, __negative
, __array
.begin(), __array
.end(), "0x", 16);
374 case __format_spec::__type::__hexadecimal_upper_case
: {
375 array
<char, __formatter::__buffer_size
<decltype(__value
), 16>()> __array
;
376 return __formatter::__format_integer(__value
, __ctx
, __specs
, __negative
, __array
.begin(), __array
.end(), "0X", 16);
379 _LIBCPP_ASSERT_INTERNAL(false, "The parse function should have validated the type");
380 __libcpp_unreachable();
384 template <signed_integral _Tp
, class _CharT
, class _FormatContext
>
385 _LIBCPP_HIDE_FROM_ABI typename
_FormatContext::iterator
386 __format_integer(_Tp __value
, _FormatContext
& __ctx
, __format_spec::__parsed_specifications
<_CharT
> __specs
) {
387 // Depending on the std-format-spec string the sign and the value
388 // might not be outputted together:
389 // - alternate form may insert a prefix string.
390 // - zero-padding may insert additional '0' characters.
391 // Therefore the value is processed as a positive unsigned value.
392 // The function @ref __insert_sign will a '-' when the value was negative.
393 auto __r
= std::__to_unsigned_like(__value
);
394 bool __negative
= __value
< 0;
396 __r
= std::__complement(__r
);
398 return __formatter::__format_integer(__r
, __ctx
, __specs
, __negative
);
402 // Formatter arithmetic (bool)
405 template <class _CharT
>
406 struct _LIBCPP_TEMPLATE_VIS __bool_strings
;
409 struct _LIBCPP_TEMPLATE_VIS __bool_strings
<char> {
410 static constexpr string_view __true
{"true"};
411 static constexpr string_view __false
{"false"};
414 # ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
416 struct _LIBCPP_TEMPLATE_VIS __bool_strings
<wchar_t> {
417 static constexpr wstring_view __true
{L
"true"};
418 static constexpr wstring_view __false
{L
"false"};
422 template <class _CharT
, class _FormatContext
>
423 _LIBCPP_HIDE_FROM_ABI typename
_FormatContext::iterator
424 __format_bool(bool __value
, _FormatContext
& __ctx
, __format_spec::__parsed_specifications
<_CharT
> __specs
) {
425 # ifndef _LIBCPP_HAS_NO_LOCALIZATION
426 if (__specs
.__std_
.__locale_specific_form_
) {
427 const auto& __np
= std::use_facet
<numpunct
<_CharT
>>(__ctx
.locale());
428 basic_string
<_CharT
> __str
= __value
? __np
.truename() : __np
.falsename();
429 return __formatter::__write_string_no_precision(basic_string_view
<_CharT
>{__str
}, __ctx
.out(), __specs
);
432 basic_string_view
<_CharT
> __str
=
433 __value
? __formatter::__bool_strings
<_CharT
>::__true
: __formatter::__bool_strings
<_CharT
>::__false
;
434 return __formatter::__write(__str
.begin(), __str
.end(), __ctx
.out(), __specs
);
437 } // namespace __formatter
439 #endif //_LIBCPP_STD_VER >= 20
441 _LIBCPP_END_NAMESPACE_STD
445 #endif // _LIBCPP___CXX03___FORMAT_FORMATTER_INTEGRAL_H