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 // int length(stateT& state, const externT* from, const externT* from_end,
21 //            size_t max) const;
22 
23 #include <codecvt>
24 #include <cassert>
25 
main()26 int main()
27 {
28     {
29         typedef std::codecvt_utf8<wchar_t> C;
30         C c;
31         char n[4] = {char(0xF1), char(0x80), char(0x80), char(0x83)};
32         std::mbstate_t m;
33         int r = c.length(m, n, n+4, 1);
34         assert(r == 4);
35 
36         n[0] = char(0xE1);
37         n[1] = char(0x80);
38         n[2] = char(0x85);
39         r = c.length(m, n, n+3, 2);
40         assert(r == 3);
41 
42         n[0] = char(0xD1);
43         n[1] = char(0x93);
44         r = c.length(m, n, n+2, 3);
45         assert(r == 2);
46 
47         n[0] = char(0x56);
48         r = c.length(m, n, n+1, 3);
49         assert(r == 1);
50     }
51     {
52         typedef std::codecvt_utf8<wchar_t, 0x1000> C;
53         C c;
54         char n[4] = {char(0xF1), char(0x80), char(0x80), char(0x83)};
55         std::mbstate_t m;
56         int r = c.length(m, n, n+4, 1);
57         assert(r == 0);
58 
59         n[0] = char(0xE1);
60         n[1] = char(0x80);
61         n[2] = char(0x85);
62         r = c.length(m, n, n+3, 2);
63         assert(r == 0);
64 
65         n[0] = char(0xD1);
66         n[1] = char(0x93);
67         r = c.length(m, n, n+2, 3);
68         assert(r == 2);
69 
70         n[0] = char(0x56);
71         r = c.length(m, n, n+1, 3);
72         assert(r == 1);
73     }
74     {
75         typedef std::codecvt_utf8<wchar_t, 0xFFFFFFFF, std::consume_header> C;
76         C c;
77         char n[7] = {char(0xEF), char(0xBB), char(0xBF), char(0xF1), char(0x80), char(0x80), char(0x83)};
78         std::mbstate_t m;
79         int r = c.length(m, n, n+7, 1);
80         assert(r == 7);
81 
82         n[0] = char(0xE1);
83         n[1] = char(0x80);
84         n[2] = char(0x85);
85         r = c.length(m, n, n+3, 2);
86         assert(r == 3);
87 
88         n[0] = char(0xEF);
89         n[1] = char(0xBB);
90         n[2] = char(0xBF);
91         n[3] = char(0xD1);
92         n[4] = char(0x93);
93         r = c.length(m, n, n+5, 3);
94         assert(r == 5);
95 
96         n[0] = char(0x56);
97         r = c.length(m, n, n+1, 3);
98         assert(r == 1);
99     }
100     {
101         typedef std::codecvt_utf8<char32_t> C;
102         C c;
103         char n[4] = {char(0xF1), char(0x80), char(0x80), char(0x83)};
104         std::mbstate_t m;
105         int r = c.length(m, n, n+4, 1);
106         assert(r == 4);
107 
108         n[0] = char(0xE1);
109         n[1] = char(0x80);
110         n[2] = char(0x85);
111         r = c.length(m, n, n+3, 2);
112         assert(r == 3);
113 
114         n[0] = char(0xD1);
115         n[1] = char(0x93);
116         r = c.length(m, n, n+2, 3);
117         assert(r == 2);
118 
119         n[0] = char(0x56);
120         r = c.length(m, n, n+1, 3);
121         assert(r == 1);
122     }
123     {
124         typedef std::codecvt_utf8<char32_t, 0x1000> C;
125         C c;
126         char n[4] = {char(0xF1), char(0x80), char(0x80), char(0x83)};
127         std::mbstate_t m;
128         int r = c.length(m, n, n+4, 1);
129         assert(r == 0);
130 
131         n[0] = char(0xE1);
132         n[1] = char(0x80);
133         n[2] = char(0x85);
134         r = c.length(m, n, n+3, 2);
135         assert(r == 0);
136 
137         n[0] = char(0xD1);
138         n[1] = char(0x93);
139         r = c.length(m, n, n+2, 3);
140         assert(r == 2);
141 
142         n[0] = char(0x56);
143         r = c.length(m, n, n+1, 3);
144         assert(r == 1);
145     }
146     {
147         typedef std::codecvt_utf8<char32_t, 0xFFFFFFFF, std::consume_header> C;
148         C c;
149         char n[7] = {char(0xEF), char(0xBB), char(0xBF), char(0xF1), char(0x80), char(0x80), char(0x83)};
150         std::mbstate_t m;
151         int r = c.length(m, n, n+7, 1);
152         assert(r == 7);
153 
154         n[0] = char(0xE1);
155         n[1] = char(0x80);
156         n[2] = char(0x85);
157         r = c.length(m, n, n+3, 2);
158         assert(r == 3);
159 
160         n[0] = char(0xEF);
161         n[1] = char(0xBB);
162         n[2] = char(0xBF);
163         n[3] = char(0xD1);
164         n[4] = char(0x93);
165         r = c.length(m, n, n+5, 3);
166         assert(r == 5);
167 
168         n[0] = char(0x56);
169         r = c.length(m, n, n+1, 3);
170         assert(r == 1);
171     }
172     {
173         typedef std::codecvt_utf8<char16_t> C;
174         C c;
175         char n[4] = {char(0xF1), char(0x80), char(0x80), char(0x83)};
176         std::mbstate_t m;
177         int r = c.length(m, n, n+4, 1);
178         assert(r == 0);
179 
180         n[0] = char(0xE1);
181         n[1] = char(0x80);
182         n[2] = char(0x85);
183         r = c.length(m, n, n+3, 2);
184         assert(r == 3);
185 
186         n[0] = char(0xD1);
187         n[1] = char(0x93);
188         r = c.length(m, n, n+2, 3);
189         assert(r == 2);
190 
191         n[0] = char(0x56);
192         r = c.length(m, n, n+1, 3);
193         assert(r == 1);
194     }
195     {
196         typedef std::codecvt_utf8<char16_t, 0x1000> C;
197         C c;
198         char n[4] = {char(0xF1), char(0x80), char(0x80), char(0x83)};
199         std::mbstate_t m;
200         int r = c.length(m, n, n+4, 1);
201         assert(r == 0);
202 
203         n[0] = char(0xE1);
204         n[1] = char(0x80);
205         n[2] = char(0x85);
206         r = c.length(m, n, n+3, 2);
207         assert(r == 0);
208 
209         n[0] = char(0xD1);
210         n[1] = char(0x93);
211         r = c.length(m, n, n+2, 3);
212         assert(r == 2);
213 
214         n[0] = char(0x56);
215         r = c.length(m, n, n+1, 3);
216         assert(r == 1);
217     }
218     {
219         typedef std::codecvt_utf8<char16_t, 0xFFFFFFFF, std::consume_header> C;
220         C c;
221         char n[7] = {char(0xEF), char(0xBB), char(0xBF), char(0xF1), char(0x80), char(0x80), char(0x83)};
222         std::mbstate_t m;
223         int r = c.length(m, n, n+7, 1);
224         assert(r == 3);
225 
226         n[0] = char(0xE1);
227         n[1] = char(0x80);
228         n[2] = char(0x85);
229         r = c.length(m, n, n+3, 2);
230         assert(r == 3);
231 
232         n[0] = char(0xEF);
233         n[1] = char(0xBB);
234         n[2] = char(0xBF);
235         n[3] = char(0xD1);
236         n[4] = char(0x93);
237         r = c.length(m, n, n+5, 3);
238         assert(r == 5);
239 
240         n[0] = char(0x56);
241         r = c.length(m, n, n+1, 3);
242         assert(r == 1);
243     }
244 }
245