From a4c687d64b5e1144ef7f61b6daf7efb209f83d35 Mon Sep 17 00:00:00 2001 From: Jonathan Wakely Date: Thu, 16 Mar 2017 15:28:02 +0000 Subject: [PATCH] PR libstdc++/80041 fix codecvt_utf16 to use UTF-16 not UTF-8 PR libstdc++/80041 * src/c++11/codecvt.cc (__codecvt_utf16_base::do_out) (__codecvt_utf16_base::do_in): Convert char arguments to char16_t to work with UTF-16 instead of UTF-8. * testsuite/22_locale/codecvt/codecvt_utf16/80041.cc: New test. From-SVN: r246202 --- libstdc++-v3/ChangeLog | 6 ++ libstdc++-v3/src/c++11/codecvt.cc | 14 +++- .../22_locale/codecvt/codecvt_utf16/80041.cc | 87 ++++++++++++++++++++++ 3 files changed, 103 insertions(+), 4 deletions(-) create mode 100644 libstdc++-v3/testsuite/22_locale/codecvt/codecvt_utf16/80041.cc diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 9facce8..cf94ab5 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,5 +1,11 @@ 2017-03-16 Jonathan Wakely + PR libstdc++/80041 + * src/c++11/codecvt.cc (__codecvt_utf16_base::do_out) + (__codecvt_utf16_base::do_in): Convert char arguments to + char16_t to work with UTF-16 instead of UTF-8. + * testsuite/22_locale/codecvt/codecvt_utf16/80041.cc: New test. + * src/c++11/codecvt.cc (codecvt) (codecvt, __codecvt_utf8_base) (__codecvt_utf8_base, __codecvt_utf8_base) diff --git a/libstdc++-v3/src/c++11/codecvt.cc b/libstdc++-v3/src/c++11/codecvt.cc index 9c91725..ef38267 100644 --- a/libstdc++-v3/src/c++11/codecvt.cc +++ b/libstdc++-v3/src/c++11/codecvt.cc @@ -1217,7 +1217,10 @@ do_out(state_type&, const intern_type* __from, const intern_type* __from_end, extern_type* __to, extern_type* __to_end, extern_type*& __to_next) const { - range to{ __to, __to_end }; + range to{ + reinterpret_cast(__to), + reinterpret_cast(__to_end) + }; #if __SIZEOF_WCHAR_T__ == 2 range from{ reinterpret_cast(__from), @@ -1234,7 +1237,7 @@ do_out(state_type&, const intern_type* __from, const intern_type* __from_end, return codecvt_base::error; #endif __from_next = reinterpret_cast(from.next); - __to_next = to.next; + __to_next = reinterpret_cast(to.next); return res; } @@ -1254,7 +1257,10 @@ do_in(state_type&, const extern_type* __from, const extern_type* __from_end, intern_type* __to, intern_type* __to_end, intern_type*& __to_next) const { - range from{ __from, __from_end }; + range from{ + reinterpret_cast(__from), + reinterpret_cast(__from_end) + }; #if __SIZEOF_WCHAR_T__ == 2 range to{ reinterpret_cast(__to), @@ -1270,7 +1276,7 @@ do_in(state_type&, const extern_type* __from, const extern_type* __from_end, #else return codecvt_base::error; #endif - __from_next = from.next; + __from_next = reinterpret_cast(from.next); __to_next = reinterpret_cast(to.next); return res; } diff --git a/libstdc++-v3/testsuite/22_locale/codecvt/codecvt_utf16/80041.cc b/libstdc++-v3/testsuite/22_locale/codecvt/codecvt_utf16/80041.cc new file mode 100644 index 0000000..a78b194 --- /dev/null +++ b/libstdc++-v3/testsuite/22_locale/codecvt/codecvt_utf16/80041.cc @@ -0,0 +1,87 @@ +// Copyright (C) 2017 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 +// . + +// { dg-do run { target c++11 } } + +#include +#include + +void +test01() +{ +#ifdef _GLIBCXX_USE_WCHAR_T + std::codecvt_utf16 conv; + const wchar_t wc = 0x6557; + char bytes[2] = {0}; + const wchar_t* wcnext; + std::mbstate_t st{}; + char* next = nullptr; + auto res = conv.out(st, &wc, &wc+ 1, wcnext, bytes, std::end(bytes), next); + VERIFY( res == std::codecvt_base::ok ); + VERIFY( wcnext == &wc + 1 ); + VERIFY( next == std::end(bytes) ); + VERIFY( bytes[0] == 0x65 ); + VERIFY( bytes[1] == 0x57 ); + VERIFY( conv.length(st, bytes, next, 1) == (next - bytes) ); + + wchar_t w; + wchar_t* wnext; + const char* cnext; + st = {}; + res = conv.in(st, bytes, next, cnext, &w, &w + 1, wnext); + VERIFY( res == std::codecvt_base::ok ); + VERIFY( wnext == &w + 1 ); + VERIFY( cnext == next ); + VERIFY( w == wc ); +#endif +} + +void +test02() +{ +#ifdef _GLIBCXX_USE_WCHAR_T + std::codecvt_utf16 conv; + wchar_t wc = 0x6557; + char bytes[2] = {0}; + const wchar_t* wcnext; + std::mbstate_t st{}; + char* next = nullptr; + auto res = conv.out(st, &wc, &wc+ 1, wcnext, bytes, std::end(bytes), next); + VERIFY( res == std::codecvt_base::ok ); + VERIFY( wcnext == &wc + 1 ); + VERIFY( next == std::end(bytes) ); + VERIFY( bytes[0] == 0x57 ); + VERIFY( bytes[1] == 0x65 ); + VERIFY( conv.length(st, bytes, next, 1) == (next - bytes) ); + + wchar_t w; + wchar_t* wnext; + const char* cnext; + st = {}; + res = conv.in(st, bytes, next, cnext, &w, &w + 1, wnext); + VERIFY( res == std::codecvt_base::ok ); + VERIFY( wnext == &w + 1 ); + VERIFY( cnext == next ); + VERIFY( w == wc ); +#endif +} + +int main() +{ + test01(); + test02(); +} -- 2.7.4