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