[PATCH] Fix std::codecvt_utf8<wchar_t> for Mingw

Jonathan Wakely jwakely@redhat.com
Sat May 19 03:13:00 GMT 2018


While testing a series of patches to add Mingw support for
std::filesystem I discovered that codecvt_utf8<wchar_t> was producing
wchar_t values with the wrong endianness. This fixes it.

	* src/c++11/codecvt.cc (__codecvt_utf8_base<wchar_t>::do_in)
	[__SIZEOF_WCHAR_T__==2 && __BYTE_ORDER__!=__ORDER_BIG_ENDIAN__]: Set
	little_endian element in bitmask.
	* testsuite/22_locale/codecvt/codecvt_utf8/69703.cc: Run all tests.
	* testsuite/22_locale/codecvt/codecvt_utf8/wchar_t/1.cc: New.

Tested powerpc64le-linux and x86_64-w64-mingw32, committed to trunk.

I think I'll backport this too, as it only affects Mingw.


-------------- next part --------------
commit b4404efe99cb8690d6ef3edd3ae42fa86b543ef2
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Sat May 19 02:30:36 2018 +0100

    Fix std::codecvt_utf8<wchar_t> for Mingw
    
            * src/c++11/codecvt.cc (__codecvt_utf8_base<wchar_t>::do_in)
            [__SIZEOF_WCHAR_T__==2 && __BYTE_ORDER__!=__ORDER_BIG_ENDIAN__]: Set
            little_endian element in bitmask.
            * testsuite/22_locale/codecvt/codecvt_utf8/69703.cc: Run all tests.
            * testsuite/22_locale/codecvt/codecvt_utf8/wchar_t/1.cc: New.

diff --git a/libstdc++-v3/src/c++11/codecvt.cc b/libstdc++-v3/src/c++11/codecvt.cc
index 259de807758..3a1a825070c 100644
--- a/libstdc++-v3/src/c++11/codecvt.cc
+++ b/libstdc++-v3/src/c++11/codecvt.cc
@@ -1086,7 +1086,12 @@ do_in(state_type&, const extern_type* __from, const extern_type* __from_end,
     reinterpret_cast<char16_t*>(__to),
     reinterpret_cast<char16_t*>(__to_end)
   };
-  auto res = ucs2_in(from, to, _M_maxcode, _M_mode);
+#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
+  codecvt_mode mode = {};
+#else
+  codecvt_mode mode = little_endian;
+#endif
+  auto res = ucs2_in(from, to, _M_maxcode, mode);
 #elif __SIZEOF_WCHAR_T__ == 4
   range<char32_t> to{
     reinterpret_cast<char32_t*>(__to),
diff --git a/libstdc++-v3/testsuite/22_locale/codecvt/codecvt_utf8/69703.cc b/libstdc++-v3/testsuite/22_locale/codecvt/codecvt_utf8/69703.cc
index 36f18a59947..56872267d1b 100644
--- a/libstdc++-v3/testsuite/22_locale/codecvt/codecvt_utf8/69703.cc
+++ b/libstdc++-v3/testsuite/22_locale/codecvt/codecvt_utf8/69703.cc
@@ -68,7 +68,6 @@ test03()
   VERIFY( in[2] == U'c' );
 }
 
-
 void
 test04()
 {
@@ -90,6 +89,6 @@ main()
 {
   test01();
   test02();
-  test01();
-  test02();
+  test03();
+  test04();
 }
diff --git a/libstdc++-v3/testsuite/22_locale/codecvt/codecvt_utf8/wchar_t/1.cc b/libstdc++-v3/testsuite/22_locale/codecvt/codecvt_utf8/wchar_t/1.cc
new file mode 100644
index 00000000000..c44f91f357e
--- /dev/null
+++ b/libstdc++-v3/testsuite/22_locale/codecvt/codecvt_utf8/wchar_t/1.cc
@@ -0,0 +1,52 @@
+// Copyright (C) 2018 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-do run { target c++11 } }
+
+#include <string>
+#include <codecvt>
+#include <testsuite_hooks.h>
+
+void
+test01()
+{
+  const char out[] = u8"\u00A33.50";
+  wchar_t in[8] = {};
+  std::codecvt_utf8<wchar_t> cvt;
+  std::mbstate_t st;
+  const char* no;
+  wchar_t* ni;
+  auto res = cvt.in(st, out, out+6, no, in, in+8, ni);
+  VERIFY( res == std::codecvt_base::ok );
+  VERIFY( in[1] == L'3' );
+  VERIFY( in[2] == L'.' );
+  VERIFY( in[3] == L'5' );
+  VERIFY( in[4] == L'0' );
+
+  char out2[8] = {};
+  char* no2;
+  const wchar_t* ni2;
+  res = cvt.out(st, in, ni, ni2, out2, out2+8, no2);
+  VERIFY( res == std::codecvt_base::ok );
+  VERIFY( out2 == std::string(out) );
+}
+
+int
+main()
+{
+  test01();
+}


More information about the Gcc-patches mailing list