1// -*- C++ -*-
2//===-------------------------- codecvt -----------------------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_CODECVT
11#define _LIBCPP_CODECVT
12
13/*
14 codecvt synopsis
15
16namespace std
17{
18
19enum codecvt_mode
20{
21 consume_header = 4,
22 generate_header = 2,
23 little_endian = 1
24};
25
26template <class Elem, unsigned long Maxcode = 0x10ffff,
27 codecvt_mode Mode = (codecvt_mode)0>
28class codecvt_utf8
29 : public codecvt<Elem, char, mbstate_t>
30{
31 explicit codecvt_utf8(size_t refs = 0);
32 ~codecvt_utf8();
33};
34
35template <class Elem, unsigned long Maxcode = 0x10ffff,
36 codecvt_mode Mode = (codecvt_mode)0>
37class codecvt_utf16
38 : public codecvt<Elem, char, mbstate_t>
39{
40 explicit codecvt_utf16(size_t refs = 0);
41 ~codecvt_utf16();
42};
43
44template <class Elem, unsigned long Maxcode = 0x10ffff,
45 codecvt_mode Mode = (codecvt_mode)0>
46class codecvt_utf8_utf16
47 : public codecvt<Elem, char, mbstate_t>
48{
49 explicit codecvt_utf8_utf16(size_t refs = 0);
50 ~codecvt_utf8_utf16();
51};
52
53} // std
54
55*/
56
57#include <__config>
58#include <__locale>
59
60#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
61#pragma GCC system_header
62#endif
63
64_LIBCPP_BEGIN_NAMESPACE_STD
65
66enum codecvt_mode
67{
68 consume_header = 4,
69 generate_header = 2,
70 little_endian = 1
71};
72
73// codecvt_utf8
74
75template <class _Elem> class __codecvt_utf8;
76
77template <>
78class _LIBCPP_TYPE_VIS __codecvt_utf8<wchar_t>
79 : public codecvt<wchar_t, char, mbstate_t>
80{
81 unsigned long _Maxcode_;
82 codecvt_mode _Mode_;
83public:
84 typedef wchar_t intern_type;
85 typedef char extern_type;
86 typedef mbstate_t state_type;
87
88 _LIBCPP_INLINE_VISIBILITY
89 explicit __codecvt_utf8(size_t __refs, unsigned long _Maxcode,
90 codecvt_mode _Mode)
91 : codecvt<wchar_t, char, mbstate_t>(__refs), _Maxcode_(_Maxcode),
92 _Mode_(_Mode) {}
93protected:
94 virtual result
95 do_out(state_type& __st,
96 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
97 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
98 virtual result
99 do_in(state_type& __st,
100 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
101 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
102 virtual result
103 do_unshift(state_type& __st,
104 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
105 virtual int do_encoding() const throw();
106 virtual bool do_always_noconv() const throw();
107 virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end,
108 size_t __mx) const;
109 virtual int do_max_length() const throw();
110};
111
112template <>
113class _LIBCPP_TYPE_VIS __codecvt_utf8<char16_t>
114 : public codecvt<char16_t, char, mbstate_t>
115{
116 unsigned long _Maxcode_;
117 codecvt_mode _Mode_;
118public:
119 typedef char16_t intern_type;
120 typedef char extern_type;
121 typedef mbstate_t state_type;
122
123 _LIBCPP_INLINE_VISIBILITY
124 explicit __codecvt_utf8(size_t __refs, unsigned long _Maxcode,
125 codecvt_mode _Mode)
126 : codecvt<char16_t, char, mbstate_t>(__refs), _Maxcode_(_Maxcode),
127 _Mode_(_Mode) {}
128protected:
129 virtual result
130 do_out(state_type& __st,
131 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
132 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
133 virtual result
134 do_in(state_type& __st,
135 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
136 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
137 virtual result
138 do_unshift(state_type& __st,
139 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
140 virtual int do_encoding() const throw();
141 virtual bool do_always_noconv() const throw();
142 virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end,
143 size_t __mx) const;
144 virtual int do_max_length() const throw();
145};
146
147template <>
148class _LIBCPP_TYPE_VIS __codecvt_utf8<char32_t>
149 : public codecvt<char32_t, char, mbstate_t>
150{
151 unsigned long _Maxcode_;
152 codecvt_mode _Mode_;
153public:
154 typedef char32_t intern_type;
155 typedef char extern_type;
156 typedef mbstate_t state_type;
157
158 _LIBCPP_INLINE_VISIBILITY
159 explicit __codecvt_utf8(size_t __refs, unsigned long _Maxcode,
160 codecvt_mode _Mode)
161 : codecvt<char32_t, char, mbstate_t>(__refs), _Maxcode_(_Maxcode),
162 _Mode_(_Mode) {}
163protected:
164 virtual result
165 do_out(state_type& __st,
166 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
167 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
168 virtual result
169 do_in(state_type& __st,
170 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
171 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
172 virtual result
173 do_unshift(state_type& __st,
174 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
175 virtual int do_encoding() const throw();
176 virtual bool do_always_noconv() const throw();
177 virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end,
178 size_t __mx) const;
179 virtual int do_max_length() const throw();
180};
181
182template <class _Elem, unsigned long _Maxcode = 0x10ffff,
183 codecvt_mode _Mode = (codecvt_mode)0>
184class _LIBCPP_TEMPLATE_VIS codecvt_utf8
185 : public __codecvt_utf8<_Elem>
186{
187public:
188 _LIBCPP_INLINE_VISIBILITY
189 explicit codecvt_utf8(size_t __refs = 0)
190 : __codecvt_utf8<_Elem>(__refs, _Maxcode, _Mode) {}
191
192 _LIBCPP_INLINE_VISIBILITY
193 ~codecvt_utf8() {}
194};
195
196// codecvt_utf16
197
198template <class _Elem, bool _LittleEndian> class __codecvt_utf16;
199
200template <>
201class _LIBCPP_TYPE_VIS __codecvt_utf16<wchar_t, false>
202 : public codecvt<wchar_t, char, mbstate_t>
203{
204 unsigned long _Maxcode_;
205 codecvt_mode _Mode_;
206public:
207 typedef wchar_t intern_type;
208 typedef char extern_type;
209 typedef mbstate_t state_type;
210
211 _LIBCPP_INLINE_VISIBILITY
212 explicit __codecvt_utf16(size_t __refs, unsigned long _Maxcode,
213 codecvt_mode _Mode)
214 : codecvt<wchar_t, char, mbstate_t>(__refs), _Maxcode_(_Maxcode),
215 _Mode_(_Mode) {}
216protected:
217 virtual result
218 do_out(state_type& __st,
219 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
220 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
221 virtual result
222 do_in(state_type& __st,
223 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
224 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
225 virtual result
226 do_unshift(state_type& __st,
227 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
228 virtual int do_encoding() const throw();
229 virtual bool do_always_noconv() const throw();
230 virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end,
231 size_t __mx) const;
232 virtual int do_max_length() const throw();
233};
234
235template <>
236class _LIBCPP_TYPE_VIS __codecvt_utf16<wchar_t, true>
237 : public codecvt<wchar_t, char, mbstate_t>
238{
239 unsigned long _Maxcode_;
240 codecvt_mode _Mode_;
241public:
242 typedef wchar_t intern_type;
243 typedef char extern_type;
244 typedef mbstate_t state_type;
245
246 _LIBCPP_INLINE_VISIBILITY
247 explicit __codecvt_utf16(size_t __refs, unsigned long _Maxcode,
248 codecvt_mode _Mode)
249 : codecvt<wchar_t, char, mbstate_t>(__refs), _Maxcode_(_Maxcode),
250 _Mode_(_Mode) {}
251protected:
252 virtual result
253 do_out(state_type& __st,
254 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
255 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
256 virtual result
257 do_in(state_type& __st,
258 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
259 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
260 virtual result
261 do_unshift(state_type& __st,
262 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
263 virtual int do_encoding() const throw();
264 virtual bool do_always_noconv() const throw();
265 virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end,
266 size_t __mx) const;
267 virtual int do_max_length() const throw();
268};
269
270template <>
271class _LIBCPP_TYPE_VIS __codecvt_utf16<char16_t, false>
272 : public codecvt<char16_t, char, mbstate_t>
273{
274 unsigned long _Maxcode_;
275 codecvt_mode _Mode_;
276public:
277 typedef char16_t intern_type;
278 typedef char extern_type;
279 typedef mbstate_t state_type;
280
281 _LIBCPP_INLINE_VISIBILITY
282 explicit __codecvt_utf16(size_t __refs, unsigned long _Maxcode,
283 codecvt_mode _Mode)
284 : codecvt<char16_t, char, mbstate_t>(__refs), _Maxcode_(_Maxcode),
285 _Mode_(_Mode) {}
286protected:
287 virtual result
288 do_out(state_type& __st,
289 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
290 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
291 virtual result
292 do_in(state_type& __st,
293 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
294 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
295 virtual result
296 do_unshift(state_type& __st,
297 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
298 virtual int do_encoding() const throw();
299 virtual bool do_always_noconv() const throw();
300 virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end,
301 size_t __mx) const;
302 virtual int do_max_length() const throw();
303};
304
305template <>
306class _LIBCPP_TYPE_VIS __codecvt_utf16<char16_t, true>
307 : public codecvt<char16_t, char, mbstate_t>
308{
309 unsigned long _Maxcode_;
310 codecvt_mode _Mode_;
311public:
312 typedef char16_t intern_type;
313 typedef char extern_type;
314 typedef mbstate_t state_type;
315
316 _LIBCPP_INLINE_VISIBILITY
317 explicit __codecvt_utf16(size_t __refs, unsigned long _Maxcode,
318 codecvt_mode _Mode)
319 : codecvt<char16_t, char, mbstate_t>(__refs), _Maxcode_(_Maxcode),
320 _Mode_(_Mode) {}
321protected:
322 virtual result
323 do_out(state_type& __st,
324 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
325 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
326 virtual result
327 do_in(state_type& __st,
328 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
329 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
330 virtual result
331 do_unshift(state_type& __st,
332 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
333 virtual int do_encoding() const throw();
334 virtual bool do_always_noconv() const throw();
335 virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end,
336 size_t __mx) const;
337 virtual int do_max_length() const throw();
338};
339
340template <>
341class _LIBCPP_TYPE_VIS __codecvt_utf16<char32_t, false>
342 : public codecvt<char32_t, char, mbstate_t>
343{
344 unsigned long _Maxcode_;
345 codecvt_mode _Mode_;
346public:
347 typedef char32_t intern_type;
348 typedef char extern_type;
349 typedef mbstate_t state_type;
350
351 _LIBCPP_INLINE_VISIBILITY
352 explicit __codecvt_utf16(size_t __refs, unsigned long _Maxcode,
353 codecvt_mode _Mode)
354 : codecvt<char32_t, char, mbstate_t>(__refs), _Maxcode_(_Maxcode),
355 _Mode_(_Mode) {}
356protected:
357 virtual result
358 do_out(state_type& __st,
359 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
360 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
361 virtual result
362 do_in(state_type& __st,
363 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
364 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
365 virtual result
366 do_unshift(state_type& __st,
367 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
368 virtual int do_encoding() const throw();
369 virtual bool do_always_noconv() const throw();
370 virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end,
371 size_t __mx) const;
372 virtual int do_max_length() const throw();
373};
374
375template <>
376class _LIBCPP_TYPE_VIS __codecvt_utf16<char32_t, true>
377 : public codecvt<char32_t, char, mbstate_t>
378{
379 unsigned long _Maxcode_;
380 codecvt_mode _Mode_;
381public:
382 typedef char32_t intern_type;
383 typedef char extern_type;
384 typedef mbstate_t state_type;
385
386 _LIBCPP_INLINE_VISIBILITY
387 explicit __codecvt_utf16(size_t __refs, unsigned long _Maxcode,
388 codecvt_mode _Mode)
389 : codecvt<char32_t, char, mbstate_t>(__refs), _Maxcode_(_Maxcode),
390 _Mode_(_Mode) {}
391protected:
392 virtual result
393 do_out(state_type& __st,
394 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
395 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
396 virtual result
397 do_in(state_type& __st,
398 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
399 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
400 virtual result
401 do_unshift(state_type& __st,
402 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
403 virtual int do_encoding() const throw();
404 virtual bool do_always_noconv() const throw();
405 virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end,
406 size_t __mx) const;
407 virtual int do_max_length() const throw();
408};
409
410template <class _Elem, unsigned long _Maxcode = 0x10ffff,
411 codecvt_mode _Mode = (codecvt_mode)0>
412class _LIBCPP_TEMPLATE_VIS codecvt_utf16
413 : public __codecvt_utf16<_Elem, _Mode & little_endian>
414{
415public:
416 _LIBCPP_INLINE_VISIBILITY
417 explicit codecvt_utf16(size_t __refs = 0)
418 : __codecvt_utf16<_Elem, _Mode & little_endian>(__refs, _Maxcode, _Mode) {}
419
420 _LIBCPP_INLINE_VISIBILITY
421 ~codecvt_utf16() {}
422};
423
424// codecvt_utf8_utf16
425
426template <class _Elem> class __codecvt_utf8_utf16;
427
428template <>
429class _LIBCPP_TYPE_VIS __codecvt_utf8_utf16<wchar_t>
430 : public codecvt<wchar_t, char, mbstate_t>
431{
432 unsigned long _Maxcode_;
433 codecvt_mode _Mode_;
434public:
435 typedef wchar_t intern_type;
436 typedef char extern_type;
437 typedef mbstate_t state_type;
438
439 _LIBCPP_INLINE_VISIBILITY
440 explicit __codecvt_utf8_utf16(size_t __refs, unsigned long _Maxcode,
441 codecvt_mode _Mode)
442 : codecvt<wchar_t, char, mbstate_t>(__refs), _Maxcode_(_Maxcode),
443 _Mode_(_Mode) {}
444protected:
445 virtual result
446 do_out(state_type& __st,
447 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
448 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
449 virtual result
450 do_in(state_type& __st,
451 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
452 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
453 virtual result
454 do_unshift(state_type& __st,
455 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
456 virtual int do_encoding() const throw();
457 virtual bool do_always_noconv() const throw();
458 virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end,
459 size_t __mx) const;
460 virtual int do_max_length() const throw();
461};
462
463template <>
464class _LIBCPP_TYPE_VIS __codecvt_utf8_utf16<char32_t>
465 : public codecvt<char32_t, char, mbstate_t>
466{
467 unsigned long _Maxcode_;
468 codecvt_mode _Mode_;
469public:
470 typedef char32_t intern_type;
471 typedef char extern_type;
472 typedef mbstate_t state_type;
473
474 _LIBCPP_INLINE_VISIBILITY
475 explicit __codecvt_utf8_utf16(size_t __refs, unsigned long _Maxcode,
476 codecvt_mode _Mode)
477 : codecvt<char32_t, char, mbstate_t>(__refs), _Maxcode_(_Maxcode),
478 _Mode_(_Mode) {}
479protected:
480 virtual result
481 do_out(state_type& __st,
482 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
483 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
484 virtual result
485 do_in(state_type& __st,
486 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
487 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
488 virtual result
489 do_unshift(state_type& __st,
490 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
491 virtual int do_encoding() const throw();
492 virtual bool do_always_noconv() const throw();
493 virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end,
494 size_t __mx) const;
495 virtual int do_max_length() const throw();
496};
497
498template <>
499class _LIBCPP_TYPE_VIS __codecvt_utf8_utf16<char16_t>
500 : public codecvt<char16_t, char, mbstate_t>
501{
502 unsigned long _Maxcode_;
503 codecvt_mode _Mode_;
504public:
505 typedef char16_t intern_type;
506 typedef char extern_type;
507 typedef mbstate_t state_type;
508
509 _LIBCPP_INLINE_VISIBILITY
510 explicit __codecvt_utf8_utf16(size_t __refs, unsigned long _Maxcode,
511 codecvt_mode _Mode)
512 : codecvt<char16_t, char, mbstate_t>(__refs), _Maxcode_(_Maxcode),
513 _Mode_(_Mode) {}
514protected:
515 virtual result
516 do_out(state_type& __st,
517 const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
518 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
519 virtual result
520 do_in(state_type& __st,
521 const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
522 intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
523 virtual result
524 do_unshift(state_type& __st,
525 extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
526 virtual int do_encoding() const throw();
527 virtual bool do_always_noconv() const throw();
528 virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end,
529 size_t __mx) const;
530 virtual int do_max_length() const throw();
531};
532
533template <class _Elem, unsigned long _Maxcode = 0x10ffff,
534 codecvt_mode _Mode = (codecvt_mode)0>
535class _LIBCPP_TEMPLATE_VIS codecvt_utf8_utf16
536 : public __codecvt_utf8_utf16<_Elem>
537{
538public:
539 _LIBCPP_INLINE_VISIBILITY
540 explicit codecvt_utf8_utf16(size_t __refs = 0)
541 : __codecvt_utf8_utf16<_Elem>(__refs, _Maxcode, _Mode) {}
542
543 _LIBCPP_INLINE_VISIBILITY
544 ~codecvt_utf8_utf16() {}
545};
546
547_LIBCPP_END_NAMESPACE_STD
548
549#endif // _LIBCPP_CODECVT
550