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