diff mbox series

libstdc++: Tweak localized formatting for floating-point types

Message ID 20240912205409.495976-1-jwakely@redhat.com
State New
Headers show
Series libstdc++: Tweak localized formatting for floating-point types | expand

Commit Message

Jonathan Wakely Sept. 12, 2024, 8:49 p.m. UTC
This adds some comments to explain what this rather subtle code is
doing. It also replaces string::copy with using char_traits::copy
directly, because the bounds checks and length adjustments that
string::copy does are redundant here - we already ensure the lengths are
correct.

Tested x86_64-linux.

-- >8 --

libstdc++-v3/ChangeLog:

	* include/std/format (__formatter_fp::_M_localize): Add comments
	and micro-optimize string copy.
---
 libstdc++-v3/include/std/format | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)
diff mbox series

Patch

diff --git a/libstdc++-v3/include/std/format b/libstdc++-v3/include/std/format
index 52243eb5479..e963d7f79b3 100644
--- a/libstdc++-v3/include/std/format
+++ b/libstdc++-v3/include/std/format
@@ -1886,25 +1886,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);
 	};