[gcc r14-11837] libstdc++: Tweak localized formatting for floating-point types

Jonathan Wakely redi@gcc.gnu.org
Wed Jun 11 08:01:24 GMT 2025


https://gcc.gnu.org/g:627c08f72b4ebcbda321643622eb9c249f57869b

commit r14-11837-g627c08f72b4ebcbda321643622eb9c249f57869b
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Mon Apr 29 18:16:29 2024 +0100

    libstdc++: Tweak localized formatting for floating-point types
    
    libstdc++-v3/ChangeLog:
    
            * include/std/format (__formatter_fp::_M_localize): Add comments
            and micro-optimize string copy.
    
    (cherry picked from commit 99b8be43d7c548db127ee4f4d0918c55edc68b3f)

Diff:
---
 libstdc++-v3/include/std/format | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/libstdc++-v3/include/std/format b/libstdc++-v3/include/std/format
index 604f4cde3c47..84f21281387c 100644
--- a/libstdc++-v3/include/std/format
+++ b/libstdc++-v3/include/std/format
@@ -1831,25 +1831,28 @@ namespace __format
 	if (__grp.empty() && __point == __dot)
 	  return __lstr; // Locale uses '.' and no grouping.
 
-	size_t __d = __str.find(__dot);
-	size_t __e = min(__d, __str.find(__exp));
+	size_t __d = __str.find(__dot); // Index of radix character (if any).
+	size_t __e = min(__d, __str.find(__exp)); // First of radix or exponent
 	if (__e == __str.npos)
 	  __e = __str.size();
-	const size_t __r = __str.size() - __e;
+	const size_t __r = __str.size() - __e; // Length of remainder.
 	auto __overwrite = [&](_CharT* __p, size_t) {
+	  // Apply grouping to the digits before the radix or exponent.
 	  auto __end = std::__add_grouping(__p, __np.thousands_sep(),
 					   __grp.data(), __grp.size(),
 					   __str.data(), __str.data() + __e);
-	  if (__r)
+	  if (__r) // If there's a fractional part or exponent
 	    {
 	      if (__d != __str.npos)
 		{
-		  *__end = __point;
+		  *__end = __point; // Add the locale's radix character.
 		  ++__end;
 		  ++__e;
 		}
-	      if (__r > 1)
-		__end += __str.copy(__end, __str.npos, __e);
+	      const size_t __rlen = __str.size() - __e;
+	      // Append fractional digits and/or exponent:
+	      char_traits<_CharT>::copy(__end, __str.data() + __e, __rlen);
+	      __end += __rlen;
 	    }
 	  return (__end - __p);
 	};


More information about the Libstdc++-cvs mailing list