libstdc++: Replace std::isdigit and std::isxdigit in <format> [PR107817]
These functions aren't usable in constant expressions. Provide our own implementations, based on __from_chars_alnum_to_val from <charconv>. libstdc++-v3/ChangeLog: PR libstdc++/107817 * include/std/charconv (__from_chars_alnum_to_val): Add constexpr for C++20. * include/std/format (__is_digit, __is_xdigit): New functions. (_Spec::_S_parse_width_or_precision): Use __is_digit. (__formatter_fp::parse): Use __is_xdigit.
This commit is contained in:
parent
6b859736bb
commit
dfc1ea414e
2 changed files with 10 additions and 4 deletions
|
@ -454,7 +454,7 @@ namespace __detail
|
|||
// If _DecOnly is false: if the character is an alphanumeric digit, then
|
||||
// return its corresponding base-36 value, otherwise return a value >= 127.
|
||||
template<bool _DecOnly = false>
|
||||
_GLIBCXX23_CONSTEXPR unsigned char
|
||||
_GLIBCXX20_CONSTEXPR unsigned char
|
||||
__from_chars_alnum_to_val(unsigned char __c)
|
||||
{
|
||||
if _GLIBCXX17_CONSTEXPR (_DecOnly)
|
||||
|
|
|
@ -358,6 +358,12 @@ namespace __format
|
|||
size_t
|
||||
__int_from_arg(const basic_format_arg<_Context>& __arg);
|
||||
|
||||
constexpr bool __is_digit(char __c)
|
||||
{ return std::__detail::__from_chars_alnum_to_val(__c) < 10; }
|
||||
|
||||
constexpr bool __is_xdigit(char __c)
|
||||
{ return std::__detail::__from_chars_alnum_to_val(__c) < 16; }
|
||||
|
||||
template<typename _CharT>
|
||||
struct _Spec
|
||||
{
|
||||
|
@ -469,7 +475,7 @@ namespace __format
|
|||
unsigned short& __val, bool& __arg_id,
|
||||
basic_format_parse_context<_CharT>& __pc)
|
||||
{
|
||||
if (std::isdigit(*__first))
|
||||
if (__format::__is_digit(*__first))
|
||||
{
|
||||
auto [__v, __ptr] = __format::__parse_integer(__first, __last);
|
||||
if (!__ptr)
|
||||
|
@ -1537,7 +1543,7 @@ namespace __format
|
|||
|
||||
if (__trailing_zeros)
|
||||
{
|
||||
if (!std::isxdigit(__s[0]))
|
||||
if (!__format::__is_xdigit(__s[0]))
|
||||
--__sigfigs;
|
||||
__z = __prec - __sigfigs;
|
||||
}
|
||||
|
@ -1627,7 +1633,7 @@ namespace __format
|
|||
{
|
||||
__fill_char = _CharT('0');
|
||||
// Write sign before zero filling.
|
||||
if (!std::isxdigit(__narrow_str[0]))
|
||||
if (!__format::__is_xdigit(__narrow_str[0]))
|
||||
{
|
||||
*__out++ = __str[0];
|
||||
__str.remove_prefix(1);
|
||||
|
|
Loading…
Add table
Reference in a new issue