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