[libc++][format] Adds integer formatter.
Implements the formatter for all fundamental integer types
(except `char`, `wchar_t`, and `bool`).
[format.formatter.spec]/2.3
For each charT, for each cv-unqualified arithmetic type ArithmeticT other
than char, wchar_t, char8_t, char16_t, or char32_t, a specialization
```
template<> struct formatter<ArithmeticT, charT>;
```
This removes the stub implemented in D96664.
As an extension it adds partial support for 128-bit integer types.
Implements parts of:
- P0645 Text Formatting
- P1652 Printf corner cases in std::format
Completes:
- LWG-3248 #b, #B, #o, #x, and #X presentation types misformat negative numbers
Reviewed By: #libc, ldionne, vitaut
Differential Revision: https://reviews.llvm.org/D103433
2020-12-14 17:39:15 +01:00
|
|
|
// -*- C++ -*-
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef _LIBCPP___FORMAT_FORMATTER_INTEGRAL_H
|
|
|
|
#define _LIBCPP___FORMAT_FORMATTER_INTEGRAL_H
|
|
|
|
|
2023-03-16 17:59:42 +01:00
|
|
|
#include <__charconv/to_chars_integral.h>
|
|
|
|
#include <__charconv/to_chars_result.h>
|
|
|
|
#include <__charconv/traits.h>
|
2021-12-28 18:48:04 +01:00
|
|
|
#include <__concepts/arithmetic.h>
|
|
|
|
#include <__concepts/same_as.h>
|
[libc++][format] Adds integer formatter.
Implements the formatter for all fundamental integer types
(except `char`, `wchar_t`, and `bool`).
[format.formatter.spec]/2.3
For each charT, for each cv-unqualified arithmetic type ArithmeticT other
than char, wchar_t, char8_t, char16_t, or char32_t, a specialization
```
template<> struct formatter<ArithmeticT, charT>;
```
This removes the stub implemented in D96664.
As an extension it adds partial support for 128-bit integer types.
Implements parts of:
- P0645 Text Formatting
- P1652 Printf corner cases in std::format
Completes:
- LWG-3248 #b, #B, #o, #x, and #X presentation types misformat negative numbers
Reviewed By: #libc, ldionne, vitaut
Differential Revision: https://reviews.llvm.org/D103433
2020-12-14 17:39:15 +01:00
|
|
|
#include <__config>
|
2022-08-04 17:54:20 +02:00
|
|
|
#include <__format/concepts.h>
|
[libc++][format] Adds integer formatter.
Implements the formatter for all fundamental integer types
(except `char`, `wchar_t`, and `bool`).
[format.formatter.spec]/2.3
For each charT, for each cv-unqualified arithmetic type ArithmeticT other
than char, wchar_t, char8_t, char16_t, or char32_t, a specialization
```
template<> struct formatter<ArithmeticT, charT>;
```
This removes the stub implemented in D96664.
As an extension it adds partial support for 128-bit integer types.
Implements parts of:
- P0645 Text Formatting
- P1652 Printf corner cases in std::format
Completes:
- LWG-3248 #b, #B, #o, #x, and #X presentation types misformat negative numbers
Reviewed By: #libc, ldionne, vitaut
Differential Revision: https://reviews.llvm.org/D103433
2020-12-14 17:39:15 +01:00
|
|
|
#include <__format/format_error.h>
|
2021-12-28 18:48:04 +01:00
|
|
|
#include <__format/formatter_output.h>
|
[libc++][format] Adds integer formatter.
Implements the formatter for all fundamental integer types
(except `char`, `wchar_t`, and `bool`).
[format.formatter.spec]/2.3
For each charT, for each cv-unqualified arithmetic type ArithmeticT other
than char, wchar_t, char8_t, char16_t, or char32_t, a specialization
```
template<> struct formatter<ArithmeticT, charT>;
```
This removes the stub implemented in D96664.
As an extension it adds partial support for 128-bit integer types.
Implements parts of:
- P0645 Text Formatting
- P1652 Printf corner cases in std::format
Completes:
- LWG-3248 #b, #B, #o, #x, and #X presentation types misformat negative numbers
Reviewed By: #libc, ldionne, vitaut
Differential Revision: https://reviews.llvm.org/D103433
2020-12-14 17:39:15 +01:00
|
|
|
#include <__format/parser_std_format_spec.h>
|
2023-12-18 10:00:47 -05:00
|
|
|
#include <__iterator/concepts.h>
|
|
|
|
#include <__iterator/iterator_traits.h>
|
|
|
|
#include <__memory/pointer_traits.h>
|
2023-03-19 23:10:37 +01:00
|
|
|
#include <__system_error/errc.h>
|
2023-03-08 17:08:04 +01:00
|
|
|
#include <__type_traits/make_unsigned.h>
|
2022-02-14 18:26:02 +01:00
|
|
|
#include <__utility/unreachable.h>
|
2022-09-05 00:01:15 +02:00
|
|
|
#include <array>
|
[libc++][modules] Rewrite the modulemap to have fewer top-level modules (#110501)
This is a re-application of bc6bd3bc1e9 which was reverted in
f11abac6524 because it broke the Clang pre-commit CI.
Original commit message:
This patch rewrites the modulemap to have fewer top-level modules.
Previously, our modulemap had one top level module for each header in
the library, including private headers. This had the well-known problem
of making compilation times terrible, in addition to being somewhat
against the design principles of Clang modules.
This patch provides almost an order of magnitude compilation time
improvement when building modularized code (certainly subject to
variations). For example, including <ccomplex> without a module cache
went from 22.4 seconds to 1.6 seconds, a 14x improvement.
To achieve this, one might be tempted to simply put all the headers in a
single top-level module. Unfortunately, this doesn't work because libc++
provides C compatibility headers (e.g. stdlib.h) which create cycles
when the C Standard Library headers are modularized too. This is
especially tricky since base systems are usually not modularized: as far
as I know, only Xcode 16 beta contains a modularized SDK that makes this
issue visible. To understand it, imagine we have the following setup:
// in libc++'s include/c++/v1/module.modulemap
module std {
header stddef.h
header stdlib.h
}
// in the C library's include/module.modulemap
module clib {
header stddef.h
header stdlib.h
}
Now, imagine that the C library's <stdlib.h> includes <stddef.h>,
perhaps as an implementation detail. When building the `std` module,
libc++'s <stdlib.h> header does `#include_next <stdlib.h>` to get the C
library's <stdlib.h>, so libc++ depends on the `clib` module.
However, remember that the C library's <stdlib.h> header includes
<stddef.h> as an implementation detail. Since the header search paths
for libc++ are (and must be) before the search paths for the C library,
the C library ends up including libc++'s <stddef.h>, which means it
depends on the `std` module. That's a cycle.
To solve this issue, this patch creates one top-level module for each C
compatibility header. The rest of the libc++ headers are located in a
single top-level `std` module, with two main exceptions. First, the
module containing configuration headers (e.g. <__config>) has its own
top-level module too, because those headers are included by the C
compatibility headers.
Second, we create a top-level std_core module that contains several
dependency-free utilities used (directly or indirectly) from the __math
subdirectory. This is needed because __math pulls in a bunch of stuff,
and __math is used from the C compatibility header <math.h>.
As a direct benefit of this change, we don't need to generate an
artificial __std_clang_module header anymore to provide a monolithic
`std` module, since our modulemap does it naturally by construction.
A next step after this change would be to look into whether math.h
really needs to include the contents of __math, and if so, whether
libc++'s math.h truly needs to include the C library's math.h header.
Removing either dependency would break this annoying cycle.
Thanks to Eric Fiselier for pointing out this approach during a recent
meeting. This wasn't viable before some recent refactoring, but wrapping
everything (except the C headers) in a large module is by far the
simplest and the most effective way of doing this.
Fixes #86193
2024-09-30 14:17:05 -04:00
|
|
|
#include <cstdint>
|
[libc++][format] Adds integer formatter.
Implements the formatter for all fundamental integer types
(except `char`, `wchar_t`, and `bool`).
[format.formatter.spec]/2.3
For each charT, for each cv-unqualified arithmetic type ArithmeticT other
than char, wchar_t, char8_t, char16_t, or char32_t, a specialization
```
template<> struct formatter<ArithmeticT, charT>;
```
This removes the stub implemented in D96664.
As an extension it adds partial support for 128-bit integer types.
Implements parts of:
- P0645 Text Formatting
- P1652 Printf corner cases in std::format
Completes:
- LWG-3248 #b, #B, #o, #x, and #X presentation types misformat negative numbers
Reviewed By: #libc, ldionne, vitaut
Differential Revision: https://reviews.llvm.org/D103433
2020-12-14 17:39:15 +01:00
|
|
|
#include <limits>
|
|
|
|
#include <string>
|
2023-03-01 20:13:55 +01:00
|
|
|
#include <string_view>
|
[libc++][format] Adds integer formatter.
Implements the formatter for all fundamental integer types
(except `char`, `wchar_t`, and `bool`).
[format.formatter.spec]/2.3
For each charT, for each cv-unqualified arithmetic type ArithmeticT other
than char, wchar_t, char8_t, char16_t, or char32_t, a specialization
```
template<> struct formatter<ArithmeticT, charT>;
```
This removes the stub implemented in D96664.
As an extension it adds partial support for 128-bit integer types.
Implements parts of:
- P0645 Text Formatting
- P1652 Printf corner cases in std::format
Completes:
- LWG-3248 #b, #B, #o, #x, and #X presentation types misformat negative numbers
Reviewed By: #libc, ldionne, vitaut
Differential Revision: https://reviews.llvm.org/D103433
2020-12-14 17:39:15 +01:00
|
|
|
|
2024-11-06 10:39:19 +01:00
|
|
|
#if _LIBCPP_HAS_LOCALIZATION
|
2024-02-02 11:33:08 +01:00
|
|
|
# include <__locale>
|
[libc++][format] Adds integer formatter.
Implements the formatter for all fundamental integer types
(except `char`, `wchar_t`, and `bool`).
[format.formatter.spec]/2.3
For each charT, for each cv-unqualified arithmetic type ArithmeticT other
than char, wchar_t, char8_t, char16_t, or char32_t, a specialization
```
template<> struct formatter<ArithmeticT, charT>;
```
This removes the stub implemented in D96664.
As an extension it adds partial support for 128-bit integer types.
Implements parts of:
- P0645 Text Formatting
- P1652 Printf corner cases in std::format
Completes:
- LWG-3248 #b, #B, #o, #x, and #X presentation types misformat negative numbers
Reviewed By: #libc, ldionne, vitaut
Differential Revision: https://reviews.llvm.org/D103433
2020-12-14 17:39:15 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
|
2022-02-01 20:16:40 -05:00
|
|
|
# pragma GCC system_header
|
[libc++][format] Adds integer formatter.
Implements the formatter for all fundamental integer types
(except `char`, `wchar_t`, and `bool`).
[format.formatter.spec]/2.3
For each charT, for each cv-unqualified arithmetic type ArithmeticT other
than char, wchar_t, char8_t, char16_t, or char32_t, a specialization
```
template<> struct formatter<ArithmeticT, charT>;
```
This removes the stub implemented in D96664.
As an extension it adds partial support for 128-bit integer types.
Implements parts of:
- P0645 Text Formatting
- P1652 Printf corner cases in std::format
Completes:
- LWG-3248 #b, #B, #o, #x, and #X presentation types misformat negative numbers
Reviewed By: #libc, ldionne, vitaut
Differential Revision: https://reviews.llvm.org/D103433
2020-12-14 17:39:15 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
_LIBCPP_PUSH_MACROS
|
|
|
|
#include <__undef_macros>
|
|
|
|
|
|
|
|
_LIBCPP_BEGIN_NAMESPACE_STD
|
|
|
|
|
2023-02-14 00:56:09 +01:00
|
|
|
#if _LIBCPP_STD_VER >= 20
|
[libc++][format] Adds integer formatter.
Implements the formatter for all fundamental integer types
(except `char`, `wchar_t`, and `bool`).
[format.formatter.spec]/2.3
For each charT, for each cv-unqualified arithmetic type ArithmeticT other
than char, wchar_t, char8_t, char16_t, or char32_t, a specialization
```
template<> struct formatter<ArithmeticT, charT>;
```
This removes the stub implemented in D96664.
As an extension it adds partial support for 128-bit integer types.
Implements parts of:
- P0645 Text Formatting
- P1652 Printf corner cases in std::format
Completes:
- LWG-3248 #b, #B, #o, #x, and #X presentation types misformat negative numbers
Reviewed By: #libc, ldionne, vitaut
Differential Revision: https://reviews.llvm.org/D103433
2020-12-14 17:39:15 +01:00
|
|
|
|
2021-12-28 18:48:04 +01:00
|
|
|
namespace __formatter {
|
[libc++][format] Adds integer formatter.
Implements the formatter for all fundamental integer types
(except `char`, `wchar_t`, and `bool`).
[format.formatter.spec]/2.3
For each charT, for each cv-unqualified arithmetic type ArithmeticT other
than char, wchar_t, char8_t, char16_t, or char32_t, a specialization
```
template<> struct formatter<ArithmeticT, charT>;
```
This removes the stub implemented in D96664.
As an extension it adds partial support for 128-bit integer types.
Implements parts of:
- P0645 Text Formatting
- P1652 Printf corner cases in std::format
Completes:
- LWG-3248 #b, #B, #o, #x, and #X presentation types misformat negative numbers
Reviewed By: #libc, ldionne, vitaut
Differential Revision: https://reviews.llvm.org/D103433
2020-12-14 17:39:15 +01:00
|
|
|
|
2021-12-28 18:48:04 +01:00
|
|
|
//
|
|
|
|
// Generic
|
|
|
|
//
|
[libc++][format] Adds integer formatter.
Implements the formatter for all fundamental integer types
(except `char`, `wchar_t`, and `bool`).
[format.formatter.spec]/2.3
For each charT, for each cv-unqualified arithmetic type ArithmeticT other
than char, wchar_t, char8_t, char16_t, or char32_t, a specialization
```
template<> struct formatter<ArithmeticT, charT>;
```
This removes the stub implemented in D96664.
As an extension it adds partial support for 128-bit integer types.
Implements parts of:
- P0645 Text Formatting
- P1652 Printf corner cases in std::format
Completes:
- LWG-3248 #b, #B, #o, #x, and #X presentation types misformat negative numbers
Reviewed By: #libc, ldionne, vitaut
Differential Revision: https://reviews.llvm.org/D103433
2020-12-14 17:39:15 +01:00
|
|
|
|
2023-12-18 10:00:47 -05:00
|
|
|
template <contiguous_iterator _Iterator>
|
|
|
|
requires same_as<char, iter_value_t<_Iterator>>
|
|
|
|
_LIBCPP_HIDE_FROM_ABI inline _Iterator __insert_sign(_Iterator __buf, bool __negative, __format_spec::__sign __sign) {
|
2021-12-28 18:48:04 +01:00
|
|
|
if (__negative)
|
|
|
|
*__buf++ = '-';
|
|
|
|
else
|
|
|
|
switch (__sign) {
|
|
|
|
case __format_spec::__sign::__default:
|
|
|
|
case __format_spec::__sign::__minus:
|
|
|
|
// No sign added.
|
|
|
|
break;
|
|
|
|
case __format_spec::__sign::__plus:
|
|
|
|
*__buf++ = '+';
|
|
|
|
break;
|
|
|
|
case __format_spec::__sign::__space:
|
|
|
|
*__buf++ = ' ';
|
|
|
|
break;
|
|
|
|
}
|
[libc++][format] Adds integer formatter.
Implements the formatter for all fundamental integer types
(except `char`, `wchar_t`, and `bool`).
[format.formatter.spec]/2.3
For each charT, for each cv-unqualified arithmetic type ArithmeticT other
than char, wchar_t, char8_t, char16_t, or char32_t, a specialization
```
template<> struct formatter<ArithmeticT, charT>;
```
This removes the stub implemented in D96664.
As an extension it adds partial support for 128-bit integer types.
Implements parts of:
- P0645 Text Formatting
- P1652 Printf corner cases in std::format
Completes:
- LWG-3248 #b, #B, #o, #x, and #X presentation types misformat negative numbers
Reviewed By: #libc, ldionne, vitaut
Differential Revision: https://reviews.llvm.org/D103433
2020-12-14 17:39:15 +01:00
|
|
|
|
2021-12-28 18:48:04 +01:00
|
|
|
return __buf;
|
[libc++][format] Adds integer formatter.
Implements the formatter for all fundamental integer types
(except `char`, `wchar_t`, and `bool`).
[format.formatter.spec]/2.3
For each charT, for each cv-unqualified arithmetic type ArithmeticT other
than char, wchar_t, char8_t, char16_t, or char32_t, a specialization
```
template<> struct formatter<ArithmeticT, charT>;
```
This removes the stub implemented in D96664.
As an extension it adds partial support for 128-bit integer types.
Implements parts of:
- P0645 Text Formatting
- P1652 Printf corner cases in std::format
Completes:
- LWG-3248 #b, #B, #o, #x, and #X presentation types misformat negative numbers
Reviewed By: #libc, ldionne, vitaut
Differential Revision: https://reviews.llvm.org/D103433
2020-12-14 17:39:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines the required grouping based on the size of the input.
|
|
|
|
*
|
|
|
|
* The grouping's last element will be repeated. For simplicity this repeating
|
|
|
|
* is unwrapped based on the length of the input. (When the input is short some
|
|
|
|
* groups are not processed.)
|
|
|
|
*
|
|
|
|
* @returns The size of the groups to write. This means the number of
|
|
|
|
* separator characters written is size() - 1.
|
|
|
|
*
|
|
|
|
* @note Since zero-sized groups cause issues they are silently ignored.
|
|
|
|
*
|
|
|
|
* @note The grouping field of the locale is always a @c std::string,
|
|
|
|
* regardless whether the @c std::numpunct's type is @c char or @c wchar_t.
|
|
|
|
*/
|
2021-12-28 18:48:04 +01:00
|
|
|
_LIBCPP_HIDE_FROM_ABI inline string __determine_grouping(ptrdiff_t __size, const string& __grouping) {
|
2024-01-05 16:29:23 -08:00
|
|
|
_LIBCPP_ASSERT_INTERNAL(!__grouping.empty() && __size > __grouping[0],
|
|
|
|
"The slow grouping formatting is used while there will be no separators written");
|
[libc++][format] Adds integer formatter.
Implements the formatter for all fundamental integer types
(except `char`, `wchar_t`, and `bool`).
[format.formatter.spec]/2.3
For each charT, for each cv-unqualified arithmetic type ArithmeticT other
than char, wchar_t, char8_t, char16_t, or char32_t, a specialization
```
template<> struct formatter<ArithmeticT, charT>;
```
This removes the stub implemented in D96664.
As an extension it adds partial support for 128-bit integer types.
Implements parts of:
- P0645 Text Formatting
- P1652 Printf corner cases in std::format
Completes:
- LWG-3248 #b, #B, #o, #x, and #X presentation types misformat negative numbers
Reviewed By: #libc, ldionne, vitaut
Differential Revision: https://reviews.llvm.org/D103433
2020-12-14 17:39:15 +01:00
|
|
|
string __r;
|
|
|
|
auto __end = __grouping.end() - 1;
|
|
|
|
auto __ptr = __grouping.begin();
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
__size -= *__ptr;
|
|
|
|
if (__size > 0)
|
|
|
|
__r.push_back(*__ptr);
|
|
|
|
else {
|
|
|
|
// __size <= 0 so the value pushed will be <= *__ptr.
|
|
|
|
__r.push_back(*__ptr + __size);
|
|
|
|
return __r;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Proceed to the next group.
|
|
|
|
if (__ptr != __end) {
|
|
|
|
do {
|
|
|
|
++__ptr;
|
|
|
|
// Skip grouping with a width of 0.
|
|
|
|
} while (*__ptr == 0 && __ptr != __end);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-14 18:26:02 +01:00
|
|
|
__libcpp_unreachable();
|
[libc++][format] Adds integer formatter.
Implements the formatter for all fundamental integer types
(except `char`, `wchar_t`, and `bool`).
[format.formatter.spec]/2.3
For each charT, for each cv-unqualified arithmetic type ArithmeticT other
than char, wchar_t, char8_t, char16_t, or char32_t, a specialization
```
template<> struct formatter<ArithmeticT, charT>;
```
This removes the stub implemented in D96664.
As an extension it adds partial support for 128-bit integer types.
Implements parts of:
- P0645 Text Formatting
- P1652 Printf corner cases in std::format
Completes:
- LWG-3248 #b, #B, #o, #x, and #X presentation types misformat negative numbers
Reviewed By: #libc, ldionne, vitaut
Differential Revision: https://reviews.llvm.org/D103433
2020-12-14 17:39:15 +01:00
|
|
|
}
|
|
|
|
|
2021-12-28 18:48:04 +01:00
|
|
|
//
|
|
|
|
// Char
|
|
|
|
//
|
[libc++][format] Adds integer formatter.
Implements the formatter for all fundamental integer types
(except `char`, `wchar_t`, and `bool`).
[format.formatter.spec]/2.3
For each charT, for each cv-unqualified arithmetic type ArithmeticT other
than char, wchar_t, char8_t, char16_t, or char32_t, a specialization
```
template<> struct formatter<ArithmeticT, charT>;
```
This removes the stub implemented in D96664.
As an extension it adds partial support for 128-bit integer types.
Implements parts of:
- P0645 Text Formatting
- P1652 Printf corner cases in std::format
Completes:
- LWG-3248 #b, #B, #o, #x, and #X presentation types misformat negative numbers
Reviewed By: #libc, ldionne, vitaut
Differential Revision: https://reviews.llvm.org/D103433
2020-12-14 17:39:15 +01:00
|
|
|
|
2022-08-04 17:54:20 +02:00
|
|
|
template <__fmt_char_type _CharT>
|
2021-12-28 18:48:04 +01:00
|
|
|
_LIBCPP_HIDE_FROM_ABI auto
|
|
|
|
__format_char(integral auto __value,
|
|
|
|
output_iterator<const _CharT&> auto __out_it,
|
|
|
|
__format_spec::__parsed_specifications<_CharT> __specs) -> decltype(__out_it) {
|
|
|
|
using _Tp = decltype(__value);
|
|
|
|
if constexpr (!same_as<_CharT, _Tp>) {
|
|
|
|
// cmp_less and cmp_greater can't be used for character types.
|
|
|
|
if constexpr (signed_integral<_CharT> == signed_integral<_Tp>) {
|
|
|
|
if (__value < numeric_limits<_CharT>::min() || __value > numeric_limits<_CharT>::max())
|
|
|
|
std::__throw_format_error("Integral value outside the range of the char type");
|
|
|
|
} else if constexpr (signed_integral<_CharT>) {
|
|
|
|
// _CharT is signed _Tp is unsigned
|
|
|
|
if (__value > static_cast<make_unsigned_t<_CharT>>(numeric_limits<_CharT>::max()))
|
|
|
|
std::__throw_format_error("Integral value outside the range of the char type");
|
|
|
|
} else {
|
|
|
|
// _CharT is unsigned _Tp is signed
|
|
|
|
if (__value < 0 || static_cast<make_unsigned_t<_Tp>>(__value) > numeric_limits<_CharT>::max())
|
|
|
|
std::__throw_format_error("Integral value outside the range of the char type");
|
[libc++][format] Adds integer formatter.
Implements the formatter for all fundamental integer types
(except `char`, `wchar_t`, and `bool`).
[format.formatter.spec]/2.3
For each charT, for each cv-unqualified arithmetic type ArithmeticT other
than char, wchar_t, char8_t, char16_t, or char32_t, a specialization
```
template<> struct formatter<ArithmeticT, charT>;
```
This removes the stub implemented in D96664.
As an extension it adds partial support for 128-bit integer types.
Implements parts of:
- P0645 Text Formatting
- P1652 Printf corner cases in std::format
Completes:
- LWG-3248 #b, #B, #o, #x, and #X presentation types misformat negative numbers
Reviewed By: #libc, ldionne, vitaut
Differential Revision: https://reviews.llvm.org/D103433
2020-12-14 17:39:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-28 18:48:04 +01:00
|
|
|
const auto __c = static_cast<_CharT>(__value);
|
|
|
|
return __formatter::__write(std::addressof(__c), std::addressof(__c) + 1, std::move(__out_it), __specs);
|
|
|
|
}
|
[libc++][format] Adds integer formatter.
Implements the formatter for all fundamental integer types
(except `char`, `wchar_t`, and `bool`).
[format.formatter.spec]/2.3
For each charT, for each cv-unqualified arithmetic type ArithmeticT other
than char, wchar_t, char8_t, char16_t, or char32_t, a specialization
```
template<> struct formatter<ArithmeticT, charT>;
```
This removes the stub implemented in D96664.
As an extension it adds partial support for 128-bit integer types.
Implements parts of:
- P0645 Text Formatting
- P1652 Printf corner cases in std::format
Completes:
- LWG-3248 #b, #B, #o, #x, and #X presentation types misformat negative numbers
Reviewed By: #libc, ldionne, vitaut
Differential Revision: https://reviews.llvm.org/D103433
2020-12-14 17:39:15 +01:00
|
|
|
|
2021-12-28 18:48:04 +01:00
|
|
|
//
|
|
|
|
// Integer
|
|
|
|
//
|
[libc++][format] Adds integer formatter.
Implements the formatter for all fundamental integer types
(except `char`, `wchar_t`, and `bool`).
[format.formatter.spec]/2.3
For each charT, for each cv-unqualified arithmetic type ArithmeticT other
than char, wchar_t, char8_t, char16_t, or char32_t, a specialization
```
template<> struct formatter<ArithmeticT, charT>;
```
This removes the stub implemented in D96664.
As an extension it adds partial support for 128-bit integer types.
Implements parts of:
- P0645 Text Formatting
- P1652 Printf corner cases in std::format
Completes:
- LWG-3248 #b, #B, #o, #x, and #X presentation types misformat negative numbers
Reviewed By: #libc, ldionne, vitaut
Differential Revision: https://reviews.llvm.org/D103433
2020-12-14 17:39:15 +01:00
|
|
|
|
2023-12-18 10:00:47 -05:00
|
|
|
/** Wrapper around @ref to_chars, returning the output iterator. */
|
|
|
|
template <contiguous_iterator _Iterator, integral _Tp>
|
|
|
|
requires same_as<char, iter_value_t<_Iterator>>
|
|
|
|
_LIBCPP_HIDE_FROM_ABI _Iterator __to_buffer(_Iterator __first, _Iterator __last, _Tp __value, int __base) {
|
2021-12-28 18:48:04 +01:00
|
|
|
// TODO FMT Evaluate code overhead due to not calling the internal function
|
|
|
|
// directly. (Should be zero overhead.)
|
2023-12-18 10:00:47 -05:00
|
|
|
to_chars_result __r = std::to_chars(std::to_address(__first), std::to_address(__last), __value, __base);
|
2024-01-05 16:29:23 -08:00
|
|
|
_LIBCPP_ASSERT_INTERNAL(__r.ec == errc(0), "Internal buffer too small");
|
2023-12-18 10:00:47 -05:00
|
|
|
auto __diff = __r.ptr - std::to_address(__first);
|
|
|
|
return __first + __diff;
|
2021-12-28 18:48:04 +01:00
|
|
|
}
|
[libc++][format] Adds integer formatter.
Implements the formatter for all fundamental integer types
(except `char`, `wchar_t`, and `bool`).
[format.formatter.spec]/2.3
For each charT, for each cv-unqualified arithmetic type ArithmeticT other
than char, wchar_t, char8_t, char16_t, or char32_t, a specialization
```
template<> struct formatter<ArithmeticT, charT>;
```
This removes the stub implemented in D96664.
As an extension it adds partial support for 128-bit integer types.
Implements parts of:
- P0645 Text Formatting
- P1652 Printf corner cases in std::format
Completes:
- LWG-3248 #b, #B, #o, #x, and #X presentation types misformat negative numbers
Reviewed By: #libc, ldionne, vitaut
Differential Revision: https://reviews.llvm.org/D103433
2020-12-14 17:39:15 +01:00
|
|
|
|
2021-12-28 18:48:04 +01:00
|
|
|
/**
|
|
|
|
* Helper to determine the buffer size to output a integer in Base @em x.
|
|
|
|
*
|
|
|
|
* There are several overloads for the supported bases. The function uses the
|
|
|
|
* base as template argument so it can be used in a constant expression.
|
|
|
|
*/
|
|
|
|
template <unsigned_integral _Tp, size_t _Base>
|
|
|
|
consteval size_t __buffer_size() noexcept
|
|
|
|
requires(_Base == 2)
|
|
|
|
{
|
|
|
|
return numeric_limits<_Tp>::digits // The number of binary digits.
|
|
|
|
+ 2 // Reserve space for the '0[Bb]' prefix.
|
|
|
|
+ 1; // Reserve space for the sign.
|
|
|
|
}
|
[libc++][format] Adds integer formatter.
Implements the formatter for all fundamental integer types
(except `char`, `wchar_t`, and `bool`).
[format.formatter.spec]/2.3
For each charT, for each cv-unqualified arithmetic type ArithmeticT other
than char, wchar_t, char8_t, char16_t, or char32_t, a specialization
```
template<> struct formatter<ArithmeticT, charT>;
```
This removes the stub implemented in D96664.
As an extension it adds partial support for 128-bit integer types.
Implements parts of:
- P0645 Text Formatting
- P1652 Printf corner cases in std::format
Completes:
- LWG-3248 #b, #B, #o, #x, and #X presentation types misformat negative numbers
Reviewed By: #libc, ldionne, vitaut
Differential Revision: https://reviews.llvm.org/D103433
2020-12-14 17:39:15 +01:00
|
|
|
|
2021-12-28 18:48:04 +01:00
|
|
|
template <unsigned_integral _Tp, size_t _Base>
|
|
|
|
consteval size_t __buffer_size() noexcept
|
|
|
|
requires(_Base == 8)
|
|
|
|
{
|
|
|
|
return numeric_limits<_Tp>::digits // The number of binary digits.
|
|
|
|
/ 3 // Adjust to octal.
|
|
|
|
+ 1 // Turn floor to ceil.
|
|
|
|
+ 1 // Reserve space for the '0' prefix.
|
|
|
|
+ 1; // Reserve space for the sign.
|
|
|
|
}
|
[libc++][format] Adds integer formatter.
Implements the formatter for all fundamental integer types
(except `char`, `wchar_t`, and `bool`).
[format.formatter.spec]/2.3
For each charT, for each cv-unqualified arithmetic type ArithmeticT other
than char, wchar_t, char8_t, char16_t, or char32_t, a specialization
```
template<> struct formatter<ArithmeticT, charT>;
```
This removes the stub implemented in D96664.
As an extension it adds partial support for 128-bit integer types.
Implements parts of:
- P0645 Text Formatting
- P1652 Printf corner cases in std::format
Completes:
- LWG-3248 #b, #B, #o, #x, and #X presentation types misformat negative numbers
Reviewed By: #libc, ldionne, vitaut
Differential Revision: https://reviews.llvm.org/D103433
2020-12-14 17:39:15 +01:00
|
|
|
|
2021-12-28 18:48:04 +01:00
|
|
|
template <unsigned_integral _Tp, size_t _Base>
|
|
|
|
consteval size_t __buffer_size() noexcept
|
|
|
|
requires(_Base == 10)
|
|
|
|
{
|
|
|
|
return numeric_limits<_Tp>::digits10 // The floored value.
|
|
|
|
+ 1 // Turn floor to ceil.
|
|
|
|
+ 1; // Reserve space for the sign.
|
|
|
|
}
|
|
|
|
|
|
|
|
template <unsigned_integral _Tp, size_t _Base>
|
|
|
|
consteval size_t __buffer_size() noexcept
|
|
|
|
requires(_Base == 16)
|
|
|
|
{
|
|
|
|
return numeric_limits<_Tp>::digits // The number of binary digits.
|
|
|
|
/ 4 // Adjust to hexadecimal.
|
|
|
|
+ 2 // Reserve space for the '0[Xx]' prefix.
|
|
|
|
+ 1; // Reserve space for the sign.
|
|
|
|
}
|
[libc++][format] Adds integer formatter.
Implements the formatter for all fundamental integer types
(except `char`, `wchar_t`, and `bool`).
[format.formatter.spec]/2.3
For each charT, for each cv-unqualified arithmetic type ArithmeticT other
than char, wchar_t, char8_t, char16_t, or char32_t, a specialization
```
template<> struct formatter<ArithmeticT, charT>;
```
This removes the stub implemented in D96664.
As an extension it adds partial support for 128-bit integer types.
Implements parts of:
- P0645 Text Formatting
- P1652 Printf corner cases in std::format
Completes:
- LWG-3248 #b, #B, #o, #x, and #X presentation types misformat negative numbers
Reviewed By: #libc, ldionne, vitaut
Differential Revision: https://reviews.llvm.org/D103433
2020-12-14 17:39:15 +01:00
|
|
|
|
2023-12-18 10:00:47 -05:00
|
|
|
template <class _OutIt, contiguous_iterator _Iterator, class _CharT>
|
|
|
|
requires same_as<char, iter_value_t<_Iterator>>
|
|
|
|
_LIBCPP_HIDE_FROM_ABI _OutIt __write_using_decimal_separators(
|
|
|
|
_OutIt __out_it,
|
|
|
|
_Iterator __begin,
|
|
|
|
_Iterator __first,
|
|
|
|
_Iterator __last,
|
|
|
|
string&& __grouping,
|
|
|
|
_CharT __sep,
|
2023-07-01 18:51:00 +02:00
|
|
|
__format_spec::__parsed_specifications<_CharT> __specs) {
|
|
|
|
int __size = (__first - __begin) + // [sign][prefix]
|
|
|
|
(__last - __first) + // data
|
|
|
|
(__grouping.size() - 1); // number of separator characters
|
|
|
|
|
|
|
|
__padding_size_result __padding = {0, 0};
|
|
|
|
if (__specs.__alignment_ == __format_spec::__alignment::__zero_padding) {
|
|
|
|
// Write [sign][prefix].
|
|
|
|
__out_it = __formatter::__copy(__begin, __first, std::move(__out_it));
|
|
|
|
|
|
|
|
if (__specs.__width_ > __size) {
|
|
|
|
// Write zero padding.
|
|
|
|
__padding.__before_ = __specs.__width_ - __size;
|
|
|
|
__out_it = __formatter::__fill(std::move(__out_it), __specs.__width_ - __size, _CharT('0'));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (__specs.__width_ > __size) {
|
|
|
|
// Determine padding and write padding.
|
|
|
|
__padding = __formatter::__padding_size(__size, __specs.__width_, __specs.__alignment_);
|
|
|
|
|
|
|
|
__out_it = __formatter::__fill(std::move(__out_it), __padding.__before_, __specs.__fill_);
|
|
|
|
}
|
|
|
|
// Write [sign][prefix].
|
|
|
|
__out_it = __formatter::__copy(__begin, __first, std::move(__out_it));
|
|
|
|
}
|
|
|
|
|
|
|
|
auto __r = __grouping.rbegin();
|
|
|
|
auto __e = __grouping.rend() - 1;
|
2024-01-05 16:29:23 -08:00
|
|
|
_LIBCPP_ASSERT_INTERNAL(
|
|
|
|
__r != __e, "The slow grouping formatting is used while there will be no separators written.");
|
2023-07-01 18:51:00 +02:00
|
|
|
// The output is divided in small groups of numbers to write:
|
|
|
|
// - A group before the first separator.
|
|
|
|
// - A separator and a group, repeated for the number of separators.
|
|
|
|
// - A group after the last separator.
|
|
|
|
// This loop achieves that process by testing the termination condition
|
|
|
|
// midway in the loop.
|
|
|
|
//
|
|
|
|
// TODO FMT This loop evaluates the loop invariant `__parser.__type !=
|
|
|
|
// _Flags::_Type::__hexadecimal_upper_case` for every iteration. (This test
|
|
|
|
// happens in the __write call.) Benchmark whether making two loops and
|
|
|
|
// hoisting the invariant is worth the effort.
|
|
|
|
while (true) {
|
|
|
|
if (__specs.__std_.__type_ == __format_spec::__type::__hexadecimal_upper_case) {
|
|
|
|
__last = __first + *__r;
|
|
|
|
__out_it = __formatter::__transform(__first, __last, std::move(__out_it), __hex_to_upper);
|
|
|
|
__first = __last;
|
|
|
|
} else {
|
|
|
|
__out_it = __formatter::__copy(__first, *__r, std::move(__out_it));
|
|
|
|
__first += *__r;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (__r == __e)
|
|
|
|
break;
|
|
|
|
|
|
|
|
++__r;
|
|
|
|
*__out_it++ = __sep;
|
|
|
|
}
|
|
|
|
|
|
|
|
return __formatter::__fill(std::move(__out_it), __padding.__after_, __specs.__fill_);
|
|
|
|
}
|
|
|
|
|
2023-12-18 10:00:47 -05:00
|
|
|
template <unsigned_integral _Tp, contiguous_iterator _Iterator, class _CharT, class _FormatContext>
|
|
|
|
requires same_as<char, iter_value_t<_Iterator>>
|
2023-05-06 13:28:46 +02:00
|
|
|
_LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator __format_integer(
|
2021-12-28 18:48:04 +01:00
|
|
|
_Tp __value,
|
2023-05-06 13:28:46 +02:00
|
|
|
_FormatContext& __ctx,
|
2021-12-28 18:48:04 +01:00
|
|
|
__format_spec::__parsed_specifications<_CharT> __specs,
|
|
|
|
bool __negative,
|
2023-12-18 10:00:47 -05:00
|
|
|
_Iterator __begin,
|
|
|
|
_Iterator __end,
|
2021-12-28 18:48:04 +01:00
|
|
|
const char* __prefix,
|
2023-05-06 13:28:46 +02:00
|
|
|
int __base) {
|
2023-12-18 10:00:47 -05:00
|
|
|
_Iterator __first = __formatter::__insert_sign(__begin, __negative, __specs.__std_.__sign_);
|
2021-12-28 18:48:04 +01:00
|
|
|
if (__specs.__std_.__alternate_form_ && __prefix)
|
|
|
|
while (*__prefix)
|
|
|
|
*__first++ = *__prefix++;
|
|
|
|
|
2023-12-18 10:00:47 -05:00
|
|
|
_Iterator __last = __formatter::__to_buffer(__first, __end, __value, __base);
|
2021-12-28 18:48:04 +01:00
|
|
|
|
2024-11-06 10:39:19 +01:00
|
|
|
# if _LIBCPP_HAS_LOCALIZATION
|
2021-12-28 18:48:04 +01:00
|
|
|
if (__specs.__std_.__locale_specific_form_) {
|
[libc++] Add custom clang-tidy checks
Reviewed By: #libc, ldionne
Spies: jwakely, beanz, smeenai, cfe-commits, tschuett, avogelsgesang, Mordante, sstefan1, libcxx-commits, ldionne, mgorny, arichardson, miyuki
Differential Revision: https://reviews.llvm.org/D131963
2022-08-13 22:33:12 +02:00
|
|
|
const auto& __np = std::use_facet<numpunct<_CharT>>(__ctx.locale());
|
2021-12-28 18:48:04 +01:00
|
|
|
string __grouping = __np.grouping();
|
|
|
|
ptrdiff_t __size = __last - __first;
|
|
|
|
// Writing the grouped form has more overhead than the normal output
|
|
|
|
// routines. If there will be no separators written the locale-specific
|
|
|
|
// form is identical to the normal routine. Test whether to grouped form
|
|
|
|
// is required.
|
|
|
|
if (!__grouping.empty() && __size > __grouping[0])
|
|
|
|
return __formatter::__write_using_decimal_separators(
|
|
|
|
__ctx.out(),
|
|
|
|
__begin,
|
|
|
|
__first,
|
|
|
|
__last,
|
|
|
|
__formatter::__determine_grouping(__size, __grouping),
|
|
|
|
__np.thousands_sep(),
|
|
|
|
__specs);
|
|
|
|
}
|
|
|
|
# endif
|
|
|
|
auto __out_it = __ctx.out();
|
|
|
|
if (__specs.__alignment_ != __format_spec::__alignment::__zero_padding)
|
|
|
|
__first = __begin;
|
|
|
|
else {
|
|
|
|
// __buf contains [sign][prefix]data
|
|
|
|
// ^ location of __first
|
|
|
|
// The zero padding is done like:
|
|
|
|
// - Write [sign][prefix]
|
|
|
|
// - Write data right aligned with '0' as fill character.
|
2022-07-16 17:03:27 +02:00
|
|
|
__out_it = __formatter::__copy(__begin, __first, std::move(__out_it));
|
2021-12-28 18:48:04 +01:00
|
|
|
__specs.__alignment_ = __format_spec::__alignment::__right;
|
2023-02-09 21:38:42 +01:00
|
|
|
__specs.__fill_.__data[0] = _CharT('0');
|
2021-12-28 18:48:04 +01:00
|
|
|
int32_t __size = __first - __begin;
|
|
|
|
|
|
|
|
__specs.__width_ -= std::min(__size, __specs.__width_);
|
[libc++][format] Adds integer formatter.
Implements the formatter for all fundamental integer types
(except `char`, `wchar_t`, and `bool`).
[format.formatter.spec]/2.3
For each charT, for each cv-unqualified arithmetic type ArithmeticT other
than char, wchar_t, char8_t, char16_t, or char32_t, a specialization
```
template<> struct formatter<ArithmeticT, charT>;
```
This removes the stub implemented in D96664.
As an extension it adds partial support for 128-bit integer types.
Implements parts of:
- P0645 Text Formatting
- P1652 Printf corner cases in std::format
Completes:
- LWG-3248 #b, #B, #o, #x, and #X presentation types misformat negative numbers
Reviewed By: #libc, ldionne, vitaut
Differential Revision: https://reviews.llvm.org/D103433
2020-12-14 17:39:15 +01:00
|
|
|
}
|
|
|
|
|
2021-12-28 18:48:04 +01:00
|
|
|
if (__specs.__std_.__type_ != __format_spec::__type::__hexadecimal_upper_case) [[likely]]
|
|
|
|
return __formatter::__write(__first, __last, __ctx.out(), __specs);
|
|
|
|
|
2025-03-05 14:18:49 +01:00
|
|
|
return __formatter::__write_transformed(__first, __last, __ctx.out(), __specs, std::__hex_to_upper);
|
2021-12-28 18:48:04 +01:00
|
|
|
}
|
[libc++][format] Adds integer formatter.
Implements the formatter for all fundamental integer types
(except `char`, `wchar_t`, and `bool`).
[format.formatter.spec]/2.3
For each charT, for each cv-unqualified arithmetic type ArithmeticT other
than char, wchar_t, char8_t, char16_t, or char32_t, a specialization
```
template<> struct formatter<ArithmeticT, charT>;
```
This removes the stub implemented in D96664.
As an extension it adds partial support for 128-bit integer types.
Implements parts of:
- P0645 Text Formatting
- P1652 Printf corner cases in std::format
Completes:
- LWG-3248 #b, #B, #o, #x, and #X presentation types misformat negative numbers
Reviewed By: #libc, ldionne, vitaut
Differential Revision: https://reviews.llvm.org/D103433
2020-12-14 17:39:15 +01:00
|
|
|
|
2023-05-06 13:28:46 +02:00
|
|
|
template <unsigned_integral _Tp, class _CharT, class _FormatContext>
|
|
|
|
_LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator
|
|
|
|
__format_integer(_Tp __value,
|
|
|
|
_FormatContext& __ctx,
|
|
|
|
__format_spec::__parsed_specifications<_CharT> __specs,
|
|
|
|
bool __negative = false) {
|
2021-12-28 18:48:04 +01:00
|
|
|
switch (__specs.__std_.__type_) {
|
|
|
|
case __format_spec::__type::__binary_lower_case: {
|
|
|
|
array<char, __formatter::__buffer_size<decltype(__value), 2>()> __array;
|
|
|
|
return __formatter::__format_integer(__value, __ctx, __specs, __negative, __array.begin(), __array.end(), "0b", 2);
|
[libc++][format] Adds integer formatter.
Implements the formatter for all fundamental integer types
(except `char`, `wchar_t`, and `bool`).
[format.formatter.spec]/2.3
For each charT, for each cv-unqualified arithmetic type ArithmeticT other
than char, wchar_t, char8_t, char16_t, or char32_t, a specialization
```
template<> struct formatter<ArithmeticT, charT>;
```
This removes the stub implemented in D96664.
As an extension it adds partial support for 128-bit integer types.
Implements parts of:
- P0645 Text Formatting
- P1652 Printf corner cases in std::format
Completes:
- LWG-3248 #b, #B, #o, #x, and #X presentation types misformat negative numbers
Reviewed By: #libc, ldionne, vitaut
Differential Revision: https://reviews.llvm.org/D103433
2020-12-14 17:39:15 +01:00
|
|
|
}
|
2021-12-28 18:48:04 +01:00
|
|
|
case __format_spec::__type::__binary_upper_case: {
|
|
|
|
array<char, __formatter::__buffer_size<decltype(__value), 2>()> __array;
|
|
|
|
return __formatter::__format_integer(__value, __ctx, __specs, __negative, __array.begin(), __array.end(), "0B", 2);
|
|
|
|
}
|
|
|
|
case __format_spec::__type::__octal: {
|
|
|
|
// Octal is special; if __value == 0 there's no prefix.
|
|
|
|
array<char, __formatter::__buffer_size<decltype(__value), 8>()> __array;
|
|
|
|
return __formatter::__format_integer(
|
|
|
|
__value, __ctx, __specs, __negative, __array.begin(), __array.end(), __value != 0 ? "0" : nullptr, 8);
|
|
|
|
}
|
2022-07-09 16:14:40 +02:00
|
|
|
case __format_spec::__type::__default:
|
2021-12-28 18:48:04 +01:00
|
|
|
case __format_spec::__type::__decimal: {
|
|
|
|
array<char, __formatter::__buffer_size<decltype(__value), 10>()> __array;
|
|
|
|
return __formatter::__format_integer(
|
|
|
|
__value, __ctx, __specs, __negative, __array.begin(), __array.end(), nullptr, 10);
|
|
|
|
}
|
|
|
|
case __format_spec::__type::__hexadecimal_lower_case: {
|
|
|
|
array<char, __formatter::__buffer_size<decltype(__value), 16>()> __array;
|
|
|
|
return __formatter::__format_integer(__value, __ctx, __specs, __negative, __array.begin(), __array.end(), "0x", 16);
|
|
|
|
}
|
|
|
|
case __format_spec::__type::__hexadecimal_upper_case: {
|
|
|
|
array<char, __formatter::__buffer_size<decltype(__value), 16>()> __array;
|
|
|
|
return __formatter::__format_integer(__value, __ctx, __specs, __negative, __array.begin(), __array.end(), "0X", 16);
|
|
|
|
}
|
|
|
|
default:
|
2024-01-05 16:29:23 -08:00
|
|
|
_LIBCPP_ASSERT_INTERNAL(false, "The parse function should have validated the type");
|
2021-12-28 18:48:04 +01:00
|
|
|
__libcpp_unreachable();
|
|
|
|
}
|
|
|
|
}
|
[libc++][format] Adds integer formatter.
Implements the formatter for all fundamental integer types
(except `char`, `wchar_t`, and `bool`).
[format.formatter.spec]/2.3
For each charT, for each cv-unqualified arithmetic type ArithmeticT other
than char, wchar_t, char8_t, char16_t, or char32_t, a specialization
```
template<> struct formatter<ArithmeticT, charT>;
```
This removes the stub implemented in D96664.
As an extension it adds partial support for 128-bit integer types.
Implements parts of:
- P0645 Text Formatting
- P1652 Printf corner cases in std::format
Completes:
- LWG-3248 #b, #B, #o, #x, and #X presentation types misformat negative numbers
Reviewed By: #libc, ldionne, vitaut
Differential Revision: https://reviews.llvm.org/D103433
2020-12-14 17:39:15 +01:00
|
|
|
|
2023-05-06 13:28:46 +02:00
|
|
|
template <signed_integral _Tp, class _CharT, class _FormatContext>
|
|
|
|
_LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator
|
|
|
|
__format_integer(_Tp __value, _FormatContext& __ctx, __format_spec::__parsed_specifications<_CharT> __specs) {
|
2021-12-28 18:48:04 +01:00
|
|
|
// Depending on the std-format-spec string the sign and the value
|
|
|
|
// might not be outputted together:
|
|
|
|
// - alternate form may insert a prefix string.
|
|
|
|
// - zero-padding may insert additional '0' characters.
|
|
|
|
// Therefore the value is processed as a positive unsigned value.
|
|
|
|
// The function @ref __insert_sign will a '-' when the value was negative.
|
|
|
|
auto __r = std::__to_unsigned_like(__value);
|
|
|
|
bool __negative = __value < 0;
|
|
|
|
if (__negative)
|
[libc++] Add custom clang-tidy checks
Reviewed By: #libc, ldionne
Spies: jwakely, beanz, smeenai, cfe-commits, tschuett, avogelsgesang, Mordante, sstefan1, libcxx-commits, ldionne, mgorny, arichardson, miyuki
Differential Revision: https://reviews.llvm.org/D131963
2022-08-13 22:33:12 +02:00
|
|
|
__r = std::__complement(__r);
|
2021-12-28 18:48:04 +01:00
|
|
|
|
|
|
|
return __formatter::__format_integer(__r, __ctx, __specs, __negative);
|
|
|
|
}
|
[libc++][format] Adds integer formatter.
Implements the formatter for all fundamental integer types
(except `char`, `wchar_t`, and `bool`).
[format.formatter.spec]/2.3
For each charT, for each cv-unqualified arithmetic type ArithmeticT other
than char, wchar_t, char8_t, char16_t, or char32_t, a specialization
```
template<> struct formatter<ArithmeticT, charT>;
```
This removes the stub implemented in D96664.
As an extension it adds partial support for 128-bit integer types.
Implements parts of:
- P0645 Text Formatting
- P1652 Printf corner cases in std::format
Completes:
- LWG-3248 #b, #B, #o, #x, and #X presentation types misformat negative numbers
Reviewed By: #libc, ldionne, vitaut
Differential Revision: https://reviews.llvm.org/D103433
2020-12-14 17:39:15 +01:00
|
|
|
|
2021-12-28 18:48:04 +01:00
|
|
|
//
|
|
|
|
// Formatter arithmetic (bool)
|
|
|
|
//
|
[libc++][format] Adds integer formatter.
Implements the formatter for all fundamental integer types
(except `char`, `wchar_t`, and `bool`).
[format.formatter.spec]/2.3
For each charT, for each cv-unqualified arithmetic type ArithmeticT other
than char, wchar_t, char8_t, char16_t, or char32_t, a specialization
```
template<> struct formatter<ArithmeticT, charT>;
```
This removes the stub implemented in D96664.
As an extension it adds partial support for 128-bit integer types.
Implements parts of:
- P0645 Text Formatting
- P1652 Printf corner cases in std::format
Completes:
- LWG-3248 #b, #B, #o, #x, and #X presentation types misformat negative numbers
Reviewed By: #libc, ldionne, vitaut
Differential Revision: https://reviews.llvm.org/D103433
2020-12-14 17:39:15 +01:00
|
|
|
|
2021-12-28 18:48:04 +01:00
|
|
|
template <class _CharT>
|
|
|
|
struct _LIBCPP_TEMPLATE_VIS __bool_strings;
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct _LIBCPP_TEMPLATE_VIS __bool_strings<char> {
|
|
|
|
static constexpr string_view __true{"true"};
|
|
|
|
static constexpr string_view __false{"false"};
|
|
|
|
};
|
|
|
|
|
2024-11-06 10:39:19 +01:00
|
|
|
# if _LIBCPP_HAS_WIDE_CHARACTERS
|
2021-12-28 18:48:04 +01:00
|
|
|
template <>
|
|
|
|
struct _LIBCPP_TEMPLATE_VIS __bool_strings<wchar_t> {
|
|
|
|
static constexpr wstring_view __true{L"true"};
|
|
|
|
static constexpr wstring_view __false{L"false"};
|
[libc++][format] Adds integer formatter.
Implements the formatter for all fundamental integer types
(except `char`, `wchar_t`, and `bool`).
[format.formatter.spec]/2.3
For each charT, for each cv-unqualified arithmetic type ArithmeticT other
than char, wchar_t, char8_t, char16_t, or char32_t, a specialization
```
template<> struct formatter<ArithmeticT, charT>;
```
This removes the stub implemented in D96664.
As an extension it adds partial support for 128-bit integer types.
Implements parts of:
- P0645 Text Formatting
- P1652 Printf corner cases in std::format
Completes:
- LWG-3248 #b, #B, #o, #x, and #X presentation types misformat negative numbers
Reviewed By: #libc, ldionne, vitaut
Differential Revision: https://reviews.llvm.org/D103433
2020-12-14 17:39:15 +01:00
|
|
|
};
|
2021-12-28 18:48:04 +01:00
|
|
|
# endif
|
|
|
|
|
2023-05-06 13:28:46 +02:00
|
|
|
template <class _CharT, class _FormatContext>
|
|
|
|
_LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator
|
|
|
|
__format_bool(bool __value, _FormatContext& __ctx, __format_spec::__parsed_specifications<_CharT> __specs) {
|
2024-11-06 10:39:19 +01:00
|
|
|
# if _LIBCPP_HAS_LOCALIZATION
|
2021-12-28 18:48:04 +01:00
|
|
|
if (__specs.__std_.__locale_specific_form_) {
|
[libc++] Add custom clang-tidy checks
Reviewed By: #libc, ldionne
Spies: jwakely, beanz, smeenai, cfe-commits, tschuett, avogelsgesang, Mordante, sstefan1, libcxx-commits, ldionne, mgorny, arichardson, miyuki
Differential Revision: https://reviews.llvm.org/D131963
2022-08-13 22:33:12 +02:00
|
|
|
const auto& __np = std::use_facet<numpunct<_CharT>>(__ctx.locale());
|
2021-12-28 18:48:04 +01:00
|
|
|
basic_string<_CharT> __str = __value ? __np.truename() : __np.falsename();
|
2022-05-28 15:30:10 +02:00
|
|
|
return __formatter::__write_string_no_precision(basic_string_view<_CharT>{__str}, __ctx.out(), __specs);
|
2021-12-28 18:48:04 +01:00
|
|
|
}
|
|
|
|
# endif
|
|
|
|
basic_string_view<_CharT> __str =
|
|
|
|
__value ? __formatter::__bool_strings<_CharT>::__true : __formatter::__bool_strings<_CharT>::__false;
|
|
|
|
return __formatter::__write(__str.begin(), __str.end(), __ctx.out(), __specs);
|
|
|
|
}
|
[libc++][format] Adds integer formatter.
Implements the formatter for all fundamental integer types
(except `char`, `wchar_t`, and `bool`).
[format.formatter.spec]/2.3
For each charT, for each cv-unqualified arithmetic type ArithmeticT other
than char, wchar_t, char8_t, char16_t, or char32_t, a specialization
```
template<> struct formatter<ArithmeticT, charT>;
```
This removes the stub implemented in D96664.
As an extension it adds partial support for 128-bit integer types.
Implements parts of:
- P0645 Text Formatting
- P1652 Printf corner cases in std::format
Completes:
- LWG-3248 #b, #B, #o, #x, and #X presentation types misformat negative numbers
Reviewed By: #libc, ldionne, vitaut
Differential Revision: https://reviews.llvm.org/D103433
2020-12-14 17:39:15 +01:00
|
|
|
|
2021-12-28 18:48:04 +01:00
|
|
|
} // namespace __formatter
|
[libc++][format] Adds integer formatter.
Implements the formatter for all fundamental integer types
(except `char`, `wchar_t`, and `bool`).
[format.formatter.spec]/2.3
For each charT, for each cv-unqualified arithmetic type ArithmeticT other
than char, wchar_t, char8_t, char16_t, or char32_t, a specialization
```
template<> struct formatter<ArithmeticT, charT>;
```
This removes the stub implemented in D96664.
As an extension it adds partial support for 128-bit integer types.
Implements parts of:
- P0645 Text Formatting
- P1652 Printf corner cases in std::format
Completes:
- LWG-3248 #b, #B, #o, #x, and #X presentation types misformat negative numbers
Reviewed By: #libc, ldionne, vitaut
Differential Revision: https://reviews.llvm.org/D103433
2020-12-14 17:39:15 +01:00
|
|
|
|
2024-08-30 12:07:07 -04:00
|
|
|
#endif // _LIBCPP_STD_VER >= 20
|
[libc++][format] Adds integer formatter.
Implements the formatter for all fundamental integer types
(except `char`, `wchar_t`, and `bool`).
[format.formatter.spec]/2.3
For each charT, for each cv-unqualified arithmetic type ArithmeticT other
than char, wchar_t, char8_t, char16_t, or char32_t, a specialization
```
template<> struct formatter<ArithmeticT, charT>;
```
This removes the stub implemented in D96664.
As an extension it adds partial support for 128-bit integer types.
Implements parts of:
- P0645 Text Formatting
- P1652 Printf corner cases in std::format
Completes:
- LWG-3248 #b, #B, #o, #x, and #X presentation types misformat negative numbers
Reviewed By: #libc, ldionne, vitaut
Differential Revision: https://reviews.llvm.org/D103433
2020-12-14 17:39:15 +01:00
|
|
|
|
|
|
|
_LIBCPP_END_NAMESPACE_STD
|
|
|
|
|
|
|
|
_LIBCPP_POP_MACROS
|
|
|
|
|
|
|
|
#endif // _LIBCPP___FORMAT_FORMATTER_INTEGRAL_H
|