1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // <codecvt>
11 
12 // template <class Elem, unsigned long Maxcode = 0x10ffff,
13 //           codecvt_mode Mode = (codecvt_mode)0>
14 // class codecvt_utf8_utf16
15 //     : public codecvt<Elem, char, mbstate_t>
16 // {
17 //     // unspecified
18 // };
19 
20 // result
21 //     in(stateT& state,
22 //        const externT* from, const externT* from_end, const externT*& from_next,
23 //        internT* to, internT* to_end, internT*& to_next) const;
24 
25 #include <codecvt>
26 #include <cassert>
27 
main()28 int main()
29 {
30     {
31         typedef std::codecvt_utf8_utf16<wchar_t> C;
32         C c;
33         wchar_t w[2] = {0};
34         char n[4] = {char(0xF1), char(0x80), char(0x80), char(0x83)};
35         wchar_t* wp = nullptr;
36         std::mbstate_t m;
37         const char* np = nullptr;
38         std::codecvt_base::result r = c.in(m, n, n+4, np, w, w+2, wp);
39         assert(r == std::codecvt_base::ok);
40         assert(wp == w+2);
41         assert(np == n+4);
42         assert(w[0] == 0xD8C0);
43         assert(w[1] == 0xDC03);
44 
45         n[0] = char(0xE1);
46         n[1] = char(0x80);
47         n[2] = char(0x85);
48         r = c.in(m, n, n+3, np, w, w+2, wp);
49         assert(r == std::codecvt_base::ok);
50         assert(wp == w+1);
51         assert(np == n+3);
52         assert(w[0] == 0x1005);
53 
54         n[0] = char(0xD1);
55         n[1] = char(0x93);
56         r = c.in(m, n, n+2, np, w, w+2, wp);
57         assert(r == std::codecvt_base::ok);
58         assert(wp == w+1);
59         assert(np == n+2);
60         assert(w[0] == 0x0453);
61 
62         n[0] = char(0x56);
63         r = c.in(m, n, n+1, np, w, w+2, wp);
64         assert(r == std::codecvt_base::ok);
65         assert(wp == w+1);
66         assert(np == n+1);
67         assert(w[0] == 0x0056);
68     }
69     {
70         typedef std::codecvt_utf8_utf16<wchar_t, 0x1000> C;
71         C c;
72         wchar_t w[2] = {0};
73         char n[4] = {char(0xF1), char(0x80), char(0x80), char(0x83)};
74         wchar_t* wp = nullptr;
75         std::mbstate_t m;
76         const char* np = nullptr;
77         std::codecvt_base::result r = c.in(m, n, n+4, np, w, w+2, wp);
78         assert(r == std::codecvt_base::error);
79         assert(wp == w);
80         assert(np == n);
81 
82         n[0] = char(0xE1);
83         n[1] = char(0x80);
84         n[2] = char(0x85);
85         r = c.in(m, n, n+3, np, w, w+2, wp);
86         assert(r == std::codecvt_base::error);
87         assert(wp == w);
88         assert(np == n);
89 
90         n[0] = char(0xD1);
91         n[1] = char(0x93);
92         r = c.in(m, n, n+2, np, w, w+2, wp);
93         assert(r == std::codecvt_base::ok);
94         assert(wp == w+1);
95         assert(np == n+2);
96         assert(w[0] == 0x0453);
97 
98         n[0] = char(0x56);
99         r = c.in(m, n, n+1, np, w, w+2, wp);
100         assert(r == std::codecvt_base::ok);
101         assert(wp == w+1);
102         assert(np == n+1);
103         assert(w[0] == 0x0056);
104     }
105     {
106         typedef std::codecvt_utf8_utf16<wchar_t, 0x10ffff, std::consume_header> C;
107         C c;
108         wchar_t w[2] = {0};
109         char n[7] = {char(0xEF), char(0xBB), char(0xBF), char(0xF1), char(0x80), char(0x80), char(0x83)};
110         wchar_t* wp = nullptr;
111         std::mbstate_t m;
112         const char* np = nullptr;
113         std::codecvt_base::result r = c.in(m, n, n+7, np, w, w+2, wp);
114         assert(r == std::codecvt_base::ok);
115         assert(wp == w+2);
116         assert(np == n+7);
117         assert(w[0] == 0xD8C0);
118         assert(w[1] == 0xDC03);
119 
120         n[0] = char(0xE1);
121         n[1] = char(0x80);
122         n[2] = char(0x85);
123         r = c.in(m, n, n+3, np, w, w+2, wp);
124         assert(r == std::codecvt_base::ok);
125         assert(wp == w+1);
126         assert(np == n+3);
127         assert(w[0] == 0x1005);
128 
129         n[0] = char(0xD1);
130         n[1] = char(0x93);
131         r = c.in(m, n, n+2, np, w, w+2, wp);
132         assert(r == std::codecvt_base::ok);
133         assert(wp == w+1);
134         assert(np == n+2);
135         assert(w[0] == 0x0453);
136 
137         n[0] = char(0x56);
138         r = c.in(m, n, n+1, np, w, w+2, wp);
139         assert(r == std::codecvt_base::ok);
140         assert(wp == w+1);
141         assert(np == n+1);
142         assert(w[0] == 0x0056);
143     }
144     {
145         typedef std::codecvt_utf8_utf16<char32_t> C;
146         C c;
147         char32_t w[2] = {0};
148         char n[4] = {char(0xF1), char(0x80), char(0x80), char(0x83)};
149         char32_t* wp = nullptr;
150         std::mbstate_t m;
151         const char* np = nullptr;
152         std::codecvt_base::result r = c.in(m, n, n+4, np, w, w+2, wp);
153         assert(r == std::codecvt_base::ok);
154         assert(wp == w+2);
155         assert(np == n+4);
156         assert(w[0] == 0xD8C0);
157         assert(w[1] == 0xDC03);
158 
159         n[0] = char(0xE1);
160         n[1] = char(0x80);
161         n[2] = char(0x85);
162         r = c.in(m, n, n+3, np, w, w+2, wp);
163         assert(r == std::codecvt_base::ok);
164         assert(wp == w+1);
165         assert(np == n+3);
166         assert(w[0] == 0x1005);
167 
168         n[0] = char(0xD1);
169         n[1] = char(0x93);
170         r = c.in(m, n, n+2, np, w, w+2, wp);
171         assert(r == std::codecvt_base::ok);
172         assert(wp == w+1);
173         assert(np == n+2);
174         assert(w[0] == 0x0453);
175 
176         n[0] = char(0x56);
177         r = c.in(m, n, n+1, np, w, w+2, wp);
178         assert(r == std::codecvt_base::ok);
179         assert(wp == w+1);
180         assert(np == n+1);
181         assert(w[0] == 0x0056);
182     }
183     {
184         typedef std::codecvt_utf8_utf16<char32_t, 0x1000> C;
185         C c;
186         char32_t w[2] = {0};
187         char n[4] = {char(0xF1), char(0x80), char(0x80), char(0x83)};
188         char32_t* wp = nullptr;
189         std::mbstate_t m;
190         const char* np = nullptr;
191         std::codecvt_base::result r = c.in(m, n, n+4, np, w, w+2, wp);
192         assert(r == std::codecvt_base::error);
193         assert(wp == w);
194         assert(np == n);
195 
196         n[0] = char(0xE1);
197         n[1] = char(0x80);
198         n[2] = char(0x85);
199         r = c.in(m, n, n+3, np, w, w+2, wp);
200         assert(r == std::codecvt_base::error);
201         assert(wp == w);
202         assert(np == n);
203 
204         n[0] = char(0xD1);
205         n[1] = char(0x93);
206         r = c.in(m, n, n+2, np, w, w+2, wp);
207         assert(r == std::codecvt_base::ok);
208         assert(wp == w+1);
209         assert(np == n+2);
210         assert(w[0] == 0x0453);
211 
212         n[0] = char(0x56);
213         r = c.in(m, n, n+1, np, w, w+2, wp);
214         assert(r == std::codecvt_base::ok);
215         assert(wp == w+1);
216         assert(np == n+1);
217         assert(w[0] == 0x0056);
218     }
219     {
220         typedef std::codecvt_utf8_utf16<char32_t, 0x10ffff, std::consume_header> C;
221         C c;
222         char32_t w[2] = {0};
223         char n[7] = {char(0xEF), char(0xBB), char(0xBF), char(0xF1), char(0x80), char(0x80), char(0x83)};
224         char32_t* wp = nullptr;
225         std::mbstate_t m;
226         const char* np = nullptr;
227         std::codecvt_base::result r = c.in(m, n, n+7, np, w, w+2, wp);
228         assert(r == std::codecvt_base::ok);
229         assert(wp == w+2);
230         assert(np == n+7);
231         assert(w[0] == 0xD8C0);
232         assert(w[1] == 0xDC03);
233 
234         n[0] = char(0xE1);
235         n[1] = char(0x80);
236         n[2] = char(0x85);
237         r = c.in(m, n, n+3, np, w, w+2, wp);
238         assert(r == std::codecvt_base::ok);
239         assert(wp == w+1);
240         assert(np == n+3);
241         assert(w[0] == 0x1005);
242 
243         n[0] = char(0xD1);
244         n[1] = char(0x93);
245         r = c.in(m, n, n+2, np, w, w+2, wp);
246         assert(r == std::codecvt_base::ok);
247         assert(wp == w+1);
248         assert(np == n+2);
249         assert(w[0] == 0x0453);
250 
251         n[0] = char(0x56);
252         r = c.in(m, n, n+1, np, w, w+2, wp);
253         assert(r == std::codecvt_base::ok);
254         assert(wp == w+1);
255         assert(np == n+1);
256         assert(w[0] == 0x0056);
257     }
258     {
259         typedef std::codecvt_utf8_utf16<char16_t> C;
260         C c;
261         char16_t w[2] = {0};
262         char n[4] = {char(0xF1), char(0x80), char(0x80), char(0x83)};
263         char16_t* wp = nullptr;
264         std::mbstate_t m;
265         const char* np = nullptr;
266         std::codecvt_base::result r = c.in(m, n, n+4, np, w, w+2, wp);
267         assert(r == std::codecvt_base::ok);
268         assert(wp == w+2);
269         assert(np == n+4);
270         assert(w[0] == 0xD8C0);
271         assert(w[1] == 0xDC03);
272 
273         n[0] = char(0xE1);
274         n[1] = char(0x80);
275         n[2] = char(0x85);
276         r = c.in(m, n, n+3, np, w, w+2, wp);
277         assert(r == std::codecvt_base::ok);
278         assert(wp == w+1);
279         assert(np == n+3);
280         assert(w[0] == 0x1005);
281 
282         n[0] = char(0xD1);
283         n[1] = char(0x93);
284         r = c.in(m, n, n+2, np, w, w+2, wp);
285         assert(r == std::codecvt_base::ok);
286         assert(wp == w+1);
287         assert(np == n+2);
288         assert(w[0] == 0x0453);
289 
290         n[0] = char(0x56);
291         r = c.in(m, n, n+1, np, w, w+2, wp);
292         assert(r == std::codecvt_base::ok);
293         assert(wp == w+1);
294         assert(np == n+1);
295         assert(w[0] == 0x0056);
296     }
297     {
298         typedef std::codecvt_utf8_utf16<char16_t, 0x1000> C;
299         C c;
300         char16_t w[2] = {0};
301         char n[4] = {char(0xF1), char(0x80), char(0x80), char(0x83)};
302         char16_t* wp = nullptr;
303         std::mbstate_t m;
304         const char* np = nullptr;
305         std::codecvt_base::result r = c.in(m, n, n+4, np, w, w+2, wp);
306         assert(r == std::codecvt_base::error);
307         assert(wp == w);
308         assert(np == n);
309 
310         n[0] = char(0xE1);
311         n[1] = char(0x80);
312         n[2] = char(0x85);
313         r = c.in(m, n, n+3, np, w, w+2, wp);
314         assert(r == std::codecvt_base::error);
315         assert(wp == w);
316         assert(np == n);
317 
318         n[0] = char(0xD1);
319         n[1] = char(0x93);
320         r = c.in(m, n, n+2, np, w, w+2, wp);
321         assert(r == std::codecvt_base::ok);
322         assert(wp == w+1);
323         assert(np == n+2);
324         assert(w[0] == 0x0453);
325 
326         n[0] = char(0x56);
327         r = c.in(m, n, n+1, np, w, w+2, wp);
328         assert(r == std::codecvt_base::ok);
329         assert(wp == w+1);
330         assert(np == n+1);
331         assert(w[0] == 0x0056);
332     }
333     {
334         typedef std::codecvt_utf8_utf16<char16_t, 0x10ffff, std::consume_header> C;
335         C c;
336         char16_t w[2] = {0};
337         char n[7] = {char(0xEF), char(0xBB), char(0xBF), char(0xF1), char(0x80), char(0x80), char(0x83)};
338         char16_t* wp = nullptr;
339         std::mbstate_t m;
340         const char* np = nullptr;
341         std::codecvt_base::result r = c.in(m, n, n+7, np, w, w+2, wp);
342         assert(r == std::codecvt_base::ok);
343         assert(wp == w+2);
344         assert(np == n+7);
345         assert(w[0] == 0xD8C0);
346         assert(w[1] == 0xDC03);
347 
348         n[0] = char(0xE1);
349         n[1] = char(0x80);
350         n[2] = char(0x85);
351         r = c.in(m, n, n+3, np, w, w+2, wp);
352         assert(r == std::codecvt_base::ok);
353         assert(wp == w+1);
354         assert(np == n+3);
355         assert(w[0] == 0x1005);
356 
357         n[0] = char(0xD1);
358         n[1] = char(0x93);
359         r = c.in(m, n, n+2, np, w, w+2, wp);
360         assert(r == std::codecvt_base::ok);
361         assert(wp == w+1);
362         assert(np == n+2);
363         assert(w[0] == 0x0453);
364 
365         n[0] = char(0x56);
366         r = c.in(m, n, n+1, np, w, w+2, wp);
367         assert(r == std::codecvt_base::ok);
368         assert(wp == w+1);
369         assert(np == n+1);
370         assert(w[0] == 0x0056);
371     }
372 }
373