[committed] libstdc++: Replace std::isdigit and std::isxdigit in <format> [PR107817]

Message ID 20221122175502.2141235-1-jwakely@redhat.com
State New
Headers
Series [committed] libstdc++: Replace std::isdigit and std::isxdigit in <format> [PR107817] |

Commit Message

Jonathan Wakely Nov. 22, 2022, 5:55 p.m. UTC
  Tested x86_64-linux. Pushed to trunk.

-- >8 --

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.
---
 libstdc++-v3/include/std/charconv |  2 +-
 libstdc++-v3/include/std/format   | 12 +++++++++---
 2 files changed, 10 insertions(+), 4 deletions(-)
  

Patch

diff --git a/libstdc++-v3/include/std/charconv b/libstdc++-v3/include/std/charconv
index 8f02395172f..8b2acc5bf8d 100644
--- a/libstdc++-v3/include/std/charconv
+++ b/libstdc++-v3/include/std/charconv
@@ -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)
diff --git a/libstdc++-v3/include/std/format b/libstdc++-v3/include/std/format
index 7ae58eb2416..23ffbdabed8 100644
--- a/libstdc++-v3/include/std/format
+++ b/libstdc++-v3/include/std/format
@@ -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);