PR libstdc++/80041 fix codecvt_utf16<wchar_t> to use UTF-16 not UTF-8

PR libstdc++/80041
	* src/c++11/codecvt.cc (__codecvt_utf16_base<wchar_t>::do_out)
	(__codecvt_utf16_base<wchar_t>::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
This commit is contained in:
Jonathan Wakely 2017-03-16 15:28:02 +00:00 committed by Jonathan Wakely
parent 516231de73
commit a4c687d64b
3 changed files with 103 additions and 4 deletions

View File

@ -1,5 +1,11 @@
2017-03-16 Jonathan Wakely <jwakely@redhat.com>
PR libstdc++/80041
* src/c++11/codecvt.cc (__codecvt_utf16_base<wchar_t>::do_out)
(__codecvt_utf16_base<wchar_t>::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<char16_t, char, mbstate_t>)
(codecvt<char32_t, char, mbstate_t>, __codecvt_utf8_base<char16_t>)
(__codecvt_utf8_base<char32_t>, __codecvt_utf8_base<wchar_t>)

View File

@ -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<char> to{ __to, __to_end };
range<char16_t> to{
reinterpret_cast<char16_t*>(__to),
reinterpret_cast<char16_t*>(__to_end)
};
#if __SIZEOF_WCHAR_T__ == 2
range<const char16_t> from{
reinterpret_cast<const char16_t*>(__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<const wchar_t*>(from.next);
__to_next = to.next;
__to_next = reinterpret_cast<char*>(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<const char> from{ __from, __from_end };
range<const char16_t> from{
reinterpret_cast<const char16_t*>(__from),
reinterpret_cast<const char16_t*>(__from_end)
};
#if __SIZEOF_WCHAR_T__ == 2
range<char16_t> to{
reinterpret_cast<char16_t*>(__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<const char*>(from.next);
__to_next = reinterpret_cast<wchar_t*>(to.next);
return res;
}

View File

@ -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
// <http://www.gnu.org/licenses/>.
// { dg-do run { target c++11 } }
#include <codecvt>
#include <testsuite_hooks.h>
void
test01()
{
#ifdef _GLIBCXX_USE_WCHAR_T
std::codecvt_utf16<wchar_t> 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<wchar_t, 0x10FFFF, std::little_endian> 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();
}