]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Fix chrono::parse to read from wide strings [PR123147]
authorJonathan Wakely <jwakely@redhat.com>
Thu, 18 Dec 2025 16:39:46 +0000 (16:39 +0000)
committerJonathan Wakely <redi@gcc.gnu.org>
Fri, 19 Dec 2025 14:00:07 +0000 (14:00 +0000)
When we extract wide characters and insert them into a stringstream to
be parsed as a floating-point value, we should use a stringstream of
char, not wchar_t.

libstdc++-v3/ChangeLog:

PR libstdc++/123147
* include/bits/chrono_io.h (_Parser::operator()) <%S>: Use a
buffer of narrow characters to be parsed by std::from_chars.
* testsuite/std/time/parse/parse.cc: Check wchar_t parsing.

libstdc++-v3/include/bits/chrono_io.h
libstdc++-v3/testsuite/std/time/parse/parse.cc

index 75dd532a8cb396cb601e829c48c67c4e61ae0976..37a296fed7ecd729d0272c56e9d9ed31471d9d10 100644 (file)
@@ -4977,14 +4977,14 @@ namespace __detail
                    }
                  else // Read fractional seconds
                    {
-                     basic_stringstream<_CharT> __buf;
+                     stringstream __buf;
                      auto __digit = _S_try_read_digit(__is, __err);
                      if (__digit != -1)
                        {
-                         __buf.put(_CharT('0') + __digit);
+                         __buf.put('0' + __digit);
                          __digit = _S_try_read_digit(__is, __err);
                          if (__digit != -1)
-                           __buf.put(_CharT('0') + __digit);
+                           __buf.put('0' + __digit);
                        }
 
                      auto __i = __is.peek();
@@ -5009,7 +5009,7 @@ namespace __detail
                                {
                                  __digit = _S_try_read_digit(__is, __err);
                                  if (__digit != -1)
-                                   __buf.put(_CharT('0') + __digit);
+                                   __buf.put('0' + __digit);
                                  else
                                    break;
                                }
index 78c761c115f6e009c188a33b0e2e57fbf08a4851..d7c73a1150fcb75ca10d15a803b03b5633194a83 100644 (file)
@@ -317,6 +317,19 @@ test_modifiers()
   VERIFY( s == 12s );
 }
 
+void
+test_wchar()
+{
+  std::wistringstream is;
+  std::chrono::duration<double, std::milli> ms;
+
+  is.clear();
+  is.str(L"0.125");
+  is >> parse(L"%S", ms);
+  VERIFY( is.good() );
+  VERIFY( ms.count() == 125 );
+}
+
 int main()
 {
   test_recommended_practice();
@@ -324,4 +337,5 @@ int main()
   test_whitespace();
   test_errors();
   test_modifiers();
+  test_wchar();
 }