]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Simplify some chrono formatters
authorJonathan Wakely <jwakely@redhat.com>
Thu, 14 Dec 2023 15:29:13 +0000 (15:29 +0000)
committerJonathan Wakely <jwakely@redhat.com>
Tue, 9 Jan 2024 17:20:29 +0000 (17:20 +0000)
I don't remember exactly why I made these bits of code reserve space in
a COW string and append to it, rather than just use the string returned
from std::format (which will undergo copy elision). The _Str_sink type
used by std::format means the string only performs a single allocation
for the formatted output, and the returned string's reference count will
be one, so won't reallocate when indexing into it. We can remove these
non-optimizations.

libstdc++-v3/ChangeLog:

* include/bits/chrono_io.h (__formatter_chrono::_M_F): Simplify
handling of string returned from std::format.
(__formatter_chrono::_M_R_T): Likewise.

libstdc++-v3/include/bits/chrono_io.h

index ec2ae9d53cc2fed78878b6b82e62c1cf38c202c3..7b5876b24e6b872bc73661ec63952a484b9427df 100644 (file)
@@ -898,11 +898,8 @@ namespace __format
             _FormatContext&) const
        {
          auto __ymd = _S_date(__t);
-         basic_string<_CharT> __s;
-#if ! _GLIBCXX_USE_CXX11_ABI
-         __s.reserve(11);
-#endif
-         __s += std::format(_GLIBCXX_WIDEN("{:04d}-  -  "), (int)__ymd.year());
+         auto __s = std::format(_GLIBCXX_WIDEN("{:04d}-  -  "),
+                                (int)__ymd.year());
          auto __sv = _S_two_digits((unsigned)__ymd.month());
          __s[__s.size() - 5] = __sv[0];
          __s[__s.size() - 4] = __sv[1];
@@ -1093,11 +1090,8 @@ namespace __format
          // %T Equivalent to %H:%M:%S
          auto __hms = _S_hms(__t);
 
-         basic_string<_CharT> __s;
-#if ! _GLIBCXX_USE_CXX11_ABI
-         __s.reserve(11);
-#endif
-         __s = std::format(_GLIBCXX_WIDEN("{:02d}:00"), __hms.hours().count());
+         auto __s = std::format(_GLIBCXX_WIDEN("{:02d}:00"),
+                                __hms.hours().count());
          auto __sv = _S_two_digits(__hms.minutes().count());
          __s[__s.size() - 2] = __sv[0];
          __s[__s.size() - 1] = __sv[1];