1 |
424 |
jeremybenn |
// { dg-require-iconv "UCS-2BE" }
|
2 |
|
|
// { dg-require-iconv "ISO-8859-15" }
|
3 |
|
|
|
4 |
|
|
// 2000-08-22 Benjamin Kosnik <bkoz@cygnus.com>
|
5 |
|
|
|
6 |
|
|
// Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2009
|
7 |
|
|
// Free Software Foundation
|
8 |
|
|
//
|
9 |
|
|
// This file is part of the GNU ISO C++ Library. This library is free
|
10 |
|
|
// software; you can redistribute it and/or modify it under the
|
11 |
|
|
// terms of the GNU General Public License as published by the
|
12 |
|
|
// Free Software Foundation; either version 3, or (at your option)
|
13 |
|
|
// any later version.
|
14 |
|
|
|
15 |
|
|
// This library is distributed in the hope that it will be useful,
|
16 |
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17 |
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
18 |
|
|
// GNU General Public License for more details.
|
19 |
|
|
|
20 |
|
|
// You should have received a copy of the GNU General Public License along
|
21 |
|
|
// with this library; see the file COPYING3. If not see
|
22 |
|
|
// <http://www.gnu.org/licenses/>.
|
23 |
|
|
|
24 |
|
|
// 22.2.1.5 - Template class codecvt [lib.locale.codecvt]
|
25 |
|
|
#include <locale>
|
26 |
|
|
#include <cstring>
|
27 |
|
|
#include <testsuite_hooks.h>
|
28 |
|
|
#include <ext/codecvt_specializations.h>
|
29 |
|
|
|
30 |
|
|
/*
|
31 |
|
|
> how do I check that these conversions are correct?
|
32 |
|
|
Very easy. Since all the characters are from ASCII you simply
|
33 |
|
|
zero-extend the values.
|
34 |
|
|
|
35 |
|
|
drepper$ echo 'black pearl jasmine tea' | od -t x1
|
36 |
|
|
0000000 62 6c 61 63 6b 20 70 65 61 72 6c 20 6a 61 73 6d
|
37 |
|
|
0000020 69 6e 65 20 74 65 61 0a
|
38 |
|
|
|
39 |
|
|
So the UCS-2 string is
|
40 |
|
|
|
41 |
|
|
0x0062, 0x006c, 0x0061, ...
|
42 |
|
|
|
43 |
|
|
You get the idea. With iconv() you have to take care of the
|
44 |
|
|
byte-order, though. UCS-2 can mean little- or big endian. Looking at
|
45 |
|
|
your result
|
46 |
|
|
|
47 |
|
|
> $9 = 25856
|
48 |
|
|
|
49 |
|
|
it shows that the other byte-order is used (25856 == 0x6500).
|
50 |
|
|
*/
|
51 |
|
|
|
52 |
|
|
// Partial specialization using encoding_state.
|
53 |
|
|
// codecvt<unicode_t, char, encoding_state>
|
54 |
|
|
// UNICODE - UCS2 (big endian)
|
55 |
|
|
void test01()
|
56 |
|
|
{
|
57 |
|
|
using namespace std;
|
58 |
|
|
typedef codecvt_base::result result;
|
59 |
|
|
typedef unsigned short int_type;
|
60 |
|
|
typedef char ext_type;
|
61 |
|
|
typedef __gnu_cxx::encoding_state state_type;
|
62 |
|
|
typedef codecvt<int_type, ext_type, state_type> unicode_codecvt;
|
63 |
|
|
typedef char_traits<int_type> int_traits;
|
64 |
|
|
typedef char_traits<ext_type> ext_traits;
|
65 |
|
|
|
66 |
|
|
bool test __attribute__((unused)) = true;
|
67 |
|
|
const ext_type* e_lit = "black pearl jasmine tea";
|
68 |
|
|
int size = strlen(e_lit);
|
69 |
|
|
|
70 |
|
|
char i_lit_base[50] __attribute__((aligned(__alignof__(int_type)))) =
|
71 |
|
|
{
|
72 |
|
|
0x00, 0x62, 0x00, 0x6c, 0x00, 0x61, 0x00, 0x63, 0x00, 0x6b, 0x00, 0x20,
|
73 |
|
|
0x00, 0x70, 0x00, 0x65, 0x00, 0x61, 0x00, 0x72, 0x00, 0x6c, 0x00, 0x20,
|
74 |
|
|
0x00, 0x6a, 0x00, 0x61, 0x00, 0x73, 0x00, 0x6d, 0x00, 0x69, 0x00, 0x6e,
|
75 |
|
|
0x00, 0x65, 0x00, 0x20, 0x00, 0x74, 0x00, 0x65, 0x00, 0x61, 0x00, 0xa0
|
76 |
|
|
};
|
77 |
|
|
const int_type* i_lit = reinterpret_cast<int_type*>(i_lit_base);
|
78 |
|
|
|
79 |
|
|
const ext_type* efrom_next;
|
80 |
|
|
const int_type* ifrom_next;
|
81 |
|
|
ext_type* e_arr = new ext_type[size + 1];
|
82 |
|
|
ext_type* eto_next;
|
83 |
|
|
int_type* i_arr = new int_type[size + 1];
|
84 |
|
|
int_type* ito_next;
|
85 |
|
|
|
86 |
|
|
// construct a locale object with the specialized facet.
|
87 |
|
|
locale loc(locale::classic(), new unicode_codecvt);
|
88 |
|
|
// sanity check the constructed locale has the specialized facet.
|
89 |
|
|
VERIFY( has_facet<unicode_codecvt>(loc) );
|
90 |
|
|
const unicode_codecvt& cvt = use_facet<unicode_codecvt>(loc);
|
91 |
|
|
|
92 |
|
|
// in
|
93 |
|
|
// unicode_codecvt::state_type state01("UCS-2BE", "ISO-8859-15", 0xfeff, 0);
|
94 |
|
|
unicode_codecvt::state_type state01("UCS-2BE", "ISO-8859-15", 0, 0);
|
95 |
|
|
|
96 |
|
|
// internal encoding is bigger because of bom
|
97 |
|
|
result r1 = cvt.in(state01, e_lit, e_lit + size, efrom_next,
|
98 |
|
|
i_arr, i_arr + size + 1, ito_next);
|
99 |
|
|
VERIFY( r1 == codecvt_base::ok );
|
100 |
|
|
VERIFY( !int_traits::compare(i_arr, i_lit, size) );
|
101 |
|
|
VERIFY( efrom_next == e_lit + size );
|
102 |
|
|
VERIFY( ito_next == i_arr + size );
|
103 |
|
|
|
104 |
|
|
// out
|
105 |
|
|
unicode_codecvt::state_type state02("UCS-2BE", "ISO-8859-15", 0, 0);
|
106 |
|
|
result r2 = cvt.out(state02, i_lit, i_lit + size, ifrom_next,
|
107 |
|
|
e_arr, e_arr + size, eto_next);
|
108 |
|
|
VERIFY( r2 == codecvt_base::ok );
|
109 |
|
|
VERIFY( !ext_traits::compare(e_arr, e_lit, size) );
|
110 |
|
|
VERIFY( ifrom_next == i_lit + size );
|
111 |
|
|
VERIFY( eto_next == e_arr + size );
|
112 |
|
|
|
113 |
|
|
// unshift
|
114 |
|
|
ext_traits::copy(e_arr, e_lit, size);
|
115 |
|
|
unicode_codecvt::state_type state03("UCS-2BE", "ISO-8859-15", 0, 0);
|
116 |
|
|
result r3 = cvt.unshift(state03, e_arr, e_arr + size, eto_next);
|
117 |
|
|
VERIFY( r3 == codecvt_base::noconv );
|
118 |
|
|
VERIFY( !ext_traits::compare(e_arr, e_lit, size) );
|
119 |
|
|
VERIFY( eto_next == e_arr );
|
120 |
|
|
|
121 |
|
|
int i = cvt.encoding();
|
122 |
|
|
VERIFY( i == 2 ); // Target-dependent.
|
123 |
|
|
|
124 |
|
|
VERIFY( !cvt.always_noconv() );
|
125 |
|
|
|
126 |
|
|
unicode_codecvt::state_type state04("UCS-2BE", "ISO-8859-15", 0, 0);
|
127 |
|
|
int j = cvt.length(state03, e_lit, e_lit + size, 5);
|
128 |
|
|
VERIFY( j == 5 );
|
129 |
|
|
|
130 |
|
|
int k = cvt.max_length();
|
131 |
|
|
VERIFY( k == 1 );
|
132 |
|
|
|
133 |
|
|
delete [] e_arr;
|
134 |
|
|
delete [] i_arr;
|
135 |
|
|
}
|
136 |
|
|
|
137 |
|
|
int main ()
|
138 |
|
|
{
|
139 |
|
|
test01();
|
140 |
|
|
return 0;
|
141 |
|
|
}
|