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
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<wchar_t> C;
32         C c;
33         wchar_t w = 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+1, wp);
39         assert(r == std::codecvt_base::ok);
40         assert(wp == &w+1);
41         assert(np == n+4);
42         assert(w == 0x40003);
43 
44         n[0] = char(0xE1);
45         n[1] = char(0x80);
46         n[2] = char(0x85);
47         r = c.in(m, n, n+3, np, &w, &w+1, wp);
48         assert(r == std::codecvt_base::ok);
49         assert(wp == &w+1);
50         assert(np == n+3);
51         assert(w == 0x1005);
52 
53         n[0] = char(0xD1);
54         n[1] = char(0x93);
55         r = c.in(m, n, n+2, np, &w, &w+1, wp);
56         assert(r == std::codecvt_base::ok);
57         assert(wp == &w+1);
58         assert(np == n+2);
59         assert(w == 0x453);
60 
61         w = 0x56;
62         n[0] = char(0x56);
63         r = c.in(m, n, n+1, np, &w, &w+1, wp);
64         assert(r == std::codecvt_base::ok);
65         assert(wp == &w+1);
66         assert(np == n+1);
67         assert(w == 0x56);
68     }
69     {
70         typedef std::codecvt_utf8<wchar_t, 0x1000> C;
71         C c;
72         wchar_t w = 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+1, wp);
78         assert(r == std::codecvt_base::error);
79         assert(wp == &w);
80         assert(np == n);
81         assert(w == 0);
82 
83         n[0] = char(0xE1);
84         n[1] = char(0x80);
85         n[2] = char(0x85);
86         r = c.in(m, n, n+3, np, &w, &w+1, wp);
87         assert(r == std::codecvt_base::error);
88         assert(wp == &w);
89         assert(np == n);
90         assert(w == 0);
91 
92         n[0] = char(0xD1);
93         n[1] = char(0x93);
94         r = c.in(m, n, n+2, np, &w, &w+1, wp);
95         assert(r == std::codecvt_base::ok);
96         assert(wp == &w+1);
97         assert(np == n+2);
98         assert(w == 0x453);
99 
100         w = 0x56;
101         n[0] = char(0x56);
102         r = c.in(m, n, n+1, np, &w, &w+1, wp);
103         assert(r == std::codecvt_base::ok);
104         assert(wp == &w+1);
105         assert(np == n+1);
106         assert(w == 0x56);
107     }
108     {
109         typedef std::codecvt_utf8<wchar_t, 0xFFFFFFFF, std::consume_header> C;
110         C c;
111         wchar_t w = 0;
112         char n[7] = {char(0xEF), char(0xBB), char(0xBF), char(0xF1), char(0x80), char(0x80), char(0x83)};
113         wchar_t* wp = nullptr;
114         std::mbstate_t m;
115         const char* np = nullptr;
116         std::codecvt_base::result r = c.in(m, n, n+7, np, &w, &w+1, wp);
117         assert(r == std::codecvt_base::ok);
118         assert(wp == &w+1);
119         assert(np == n+7);
120         assert(w == 0x40003);
121 
122         n[0] = char(0xE1);
123         n[1] = char(0x80);
124         n[2] = char(0x85);
125         r = c.in(m, n, n+3, np, &w, &w+1, wp);
126         assert(r == std::codecvt_base::ok);
127         assert(wp == &w+1);
128         assert(np == n+3);
129         assert(w == 0x1005);
130 
131         n[0] = char(0xEF);
132         n[1] = char(0xBB);
133         n[2] = char(0xBF);
134         n[3] = char(0xD1);
135         n[4] = char(0x93);
136         r = c.in(m, n, n+5, np, &w, &w+1, wp);
137         assert(r == std::codecvt_base::ok);
138         assert(wp == &w+1);
139         assert(np == n+5);
140         assert(w == 0x453);
141 
142         w = 0x56;
143         n[0] = char(0x56);
144         r = c.in(m, n, n+1, np, &w, &w+1, wp);
145         assert(r == std::codecvt_base::ok);
146         assert(wp == &w+1);
147         assert(np == n+1);
148         assert(w == 0x56);
149     }
150     {
151         typedef std::codecvt_utf8<char32_t> C;
152         C c;
153         char32_t w = 0;
154         char n[4] = {char(0xF1), char(0x80), char(0x80), char(0x83)};
155         char32_t* wp = nullptr;
156         std::mbstate_t m;
157         const char* np = nullptr;
158         std::codecvt_base::result r = c.in(m, n, n+4, np, &w, &w+1, wp);
159         assert(r == std::codecvt_base::ok);
160         assert(wp == &w+1);
161         assert(np == n+4);
162         assert(w == 0x40003);
163 
164         n[0] = char(0xE1);
165         n[1] = char(0x80);
166         n[2] = char(0x85);
167         r = c.in(m, n, n+3, np, &w, &w+1, wp);
168         assert(r == std::codecvt_base::ok);
169         assert(wp == &w+1);
170         assert(np == n+3);
171         assert(w == 0x1005);
172 
173         n[0] = char(0xD1);
174         n[1] = char(0x93);
175         r = c.in(m, n, n+2, np, &w, &w+1, wp);
176         assert(r == std::codecvt_base::ok);
177         assert(wp == &w+1);
178         assert(np == n+2);
179         assert(w == 0x453);
180 
181         w = 0x56;
182         n[0] = char(0x56);
183         r = c.in(m, n, n+1, np, &w, &w+1, wp);
184         assert(r == std::codecvt_base::ok);
185         assert(wp == &w+1);
186         assert(np == n+1);
187         assert(w == 0x56);
188     }
189     {
190         typedef std::codecvt_utf8<char32_t, 0x1000> C;
191         C c;
192         char32_t w = 0;
193         char n[4] = {char(0xF1), char(0x80), char(0x80), char(0x83)};
194         char32_t* wp = nullptr;
195         std::mbstate_t m;
196         const char* np = nullptr;
197         std::codecvt_base::result r = c.in(m, n, n+4, np, &w, &w+1, wp);
198         assert(r == std::codecvt_base::error);
199         assert(wp == &w);
200         assert(np == n);
201         assert(w == 0);
202 
203         n[0] = char(0xE1);
204         n[1] = char(0x80);
205         n[2] = char(0x85);
206         r = c.in(m, n, n+3, np, &w, &w+1, wp);
207         assert(r == std::codecvt_base::error);
208         assert(wp == &w);
209         assert(np == n);
210         assert(w == 0);
211 
212         n[0] = char(0xD1);
213         n[1] = char(0x93);
214         r = c.in(m, n, n+2, np, &w, &w+1, wp);
215         assert(r == std::codecvt_base::ok);
216         assert(wp == &w+1);
217         assert(np == n+2);
218         assert(w == 0x453);
219 
220         w = 0x56;
221         n[0] = char(0x56);
222         r = c.in(m, n, n+1, np, &w, &w+1, wp);
223         assert(r == std::codecvt_base::ok);
224         assert(wp == &w+1);
225         assert(np == n+1);
226         assert(w == 0x56);
227     }
228     {
229         typedef std::codecvt_utf8<char32_t, 0xFFFFFFFF, std::consume_header> C;
230         C c;
231         char32_t w = 0;
232         char n[7] = {char(0xEF), char(0xBB), char(0xBF), char(0xF1), char(0x80), char(0x80), char(0x83)};
233         char32_t* wp = nullptr;
234         std::mbstate_t m;
235         const char* np = nullptr;
236         std::codecvt_base::result r = c.in(m, n, n+7, np, &w, &w+1, wp);
237         assert(r == std::codecvt_base::ok);
238         assert(wp == &w+1);
239         assert(np == n+7);
240         assert(w == 0x40003);
241 
242         n[0] = char(0xE1);
243         n[1] = char(0x80);
244         n[2] = char(0x85);
245         r = c.in(m, n, n+3, np, &w, &w+1, wp);
246         assert(r == std::codecvt_base::ok);
247         assert(wp == &w+1);
248         assert(np == n+3);
249         assert(w == 0x1005);
250 
251         n[0] = char(0xEF);
252         n[1] = char(0xBB);
253         n[2] = char(0xBF);
254         n[3] = char(0xD1);
255         n[4] = char(0x93);
256         r = c.in(m, n, n+5, np, &w, &w+1, wp);
257         assert(r == std::codecvt_base::ok);
258         assert(wp == &w+1);
259         assert(np == n+5);
260         assert(w == 0x453);
261 
262         w = 0x56;
263         n[0] = char(0x56);
264         r = c.in(m, n, n+1, np, &w, &w+1, wp);
265         assert(r == std::codecvt_base::ok);
266         assert(wp == &w+1);
267         assert(np == n+1);
268         assert(w == 0x56);
269     }
270     {
271         typedef std::codecvt_utf8<char16_t> C;
272         C c;
273         char16_t w = 0;
274         char n[3] = {char(0xE1), char(0x80), char(0x85)};
275         char16_t* wp = nullptr;
276         std::mbstate_t m;
277         const char* np = nullptr;
278         std::codecvt_base::result r = c.in(m, n, n+3, np, &w, &w+1, wp);
279         assert(r == std::codecvt_base::ok);
280         assert(wp == &w+1);
281         assert(np == n+3);
282         assert(w == 0x1005);
283 
284         n[0] = char(0xD1);
285         n[1] = char(0x93);
286         r = c.in(m, n, n+2, np, &w, &w+1, wp);
287         assert(r == std::codecvt_base::ok);
288         assert(wp == &w+1);
289         assert(np == n+2);
290         assert(w == 0x453);
291 
292         w = 0x56;
293         n[0] = char(0x56);
294         r = c.in(m, n, n+1, np, &w, &w+1, wp);
295         assert(r == std::codecvt_base::ok);
296         assert(wp == &w+1);
297         assert(np == n+1);
298         assert(w == 0x56);
299     }
300     {
301         typedef std::codecvt_utf8<char16_t, 0x1000> C;
302         C c;
303         char16_t w = 0;
304         char n[3] = {char(0xE1), char(0x80), char(0x85)};
305         char16_t* wp = nullptr;
306         std::mbstate_t m;
307         const char* np = nullptr;
308         std::codecvt_base::result r = c.in(m, n, n+3, np, &w, &w+1, wp);
309         assert(r == std::codecvt_base::error);
310         assert(wp == &w);
311         assert(np == n);
312         assert(w == 0);
313 
314         n[0] = char(0xD1);
315         n[1] = char(0x93);
316         r = c.in(m, n, n+2, np, &w, &w+1, wp);
317         assert(r == std::codecvt_base::ok);
318         assert(wp == &w+1);
319         assert(np == n+2);
320         assert(w == 0x453);
321 
322         w = 0x56;
323         n[0] = char(0x56);
324         r = c.in(m, n, n+1, np, &w, &w+1, wp);
325         assert(r == std::codecvt_base::ok);
326         assert(wp == &w+1);
327         assert(np == n+1);
328         assert(w == 0x56);
329     }
330     {
331         typedef std::codecvt_utf8<char16_t, 0xFFFFFFFF, std::consume_header> C;
332         C c;
333         char16_t w = 0;
334         char n[6] = {char(0xEF), char(0xBB), char(0xBF), char(0xE1), char(0x80), char(0x85)};
335         char16_t* wp = nullptr;
336         std::mbstate_t m;
337         const char* np = nullptr;
338         std::codecvt_base::result r = c.in(m, n, n+6, np, &w, &w+1, wp);
339         assert(r == std::codecvt_base::ok);
340         assert(wp == &w+1);
341         assert(np == n+6);
342         assert(w == 0x1005);
343 
344         n[0] = char(0xD1);
345         n[1] = char(0x93);
346         r = c.in(m, n, n+2, np, &w, &w+1, wp);
347         assert(r == std::codecvt_base::ok);
348         assert(wp == &w+1);
349         assert(np == n+2);
350         assert(w == 0x453);
351 
352         w = 0x56;
353         n[0] = char(0x56);
354         r = c.in(m, n, n+1, np, &w, &w+1, wp);
355         assert(r == std::codecvt_base::ok);
356         assert(wp == &w+1);
357         assert(np == n+1);
358         assert(w == 0x56);
359     }
360 }
361