1// -*- C++ -*-
2//===-------------------------- codecvt -----------------------------------===//
3//
4//                     The LLVM Compiler Infrastructure
5//
6// This file is distributed under the University of Illinois Open Source
7// License. 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 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    ~codecvt_utf8() {}
189};
190
191// codecvt_utf16
192
193template <class _Elem, bool _LittleEndian> class __codecvt_utf16;
194
195template <>
196class __codecvt_utf16<wchar_t, false>
197    : public codecvt<wchar_t, char, mbstate_t>
198{
199    unsigned long _Maxcode_;
200    codecvt_mode _Mode_;
201public:
202    typedef wchar_t   intern_type;
203    typedef char      extern_type;
204    typedef mbstate_t state_type;
205
206    _LIBCPP_ALWAYS_INLINE
207    explicit __codecvt_utf16(size_t __refs, unsigned long _Maxcode,
208                            codecvt_mode _Mode)
209        : codecvt<wchar_t, char, mbstate_t>(__refs), _Maxcode_(_Maxcode),
210          _Mode_(_Mode) {}
211protected:
212    virtual result
213        do_out(state_type& __st,
214               const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
215               extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
216    virtual result
217        do_in(state_type& __st,
218              const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
219              intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
220    virtual result
221        do_unshift(state_type& __st,
222                   extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
223    virtual int do_encoding() const throw();
224    virtual bool do_always_noconv() const throw();
225    virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end,
226                          size_t __mx) const;
227    virtual int do_max_length() const throw();
228};
229
230template <>
231class __codecvt_utf16<wchar_t, true>
232    : public codecvt<wchar_t, char, mbstate_t>
233{
234    unsigned long _Maxcode_;
235    codecvt_mode _Mode_;
236public:
237    typedef wchar_t   intern_type;
238    typedef char      extern_type;
239    typedef mbstate_t state_type;
240
241    _LIBCPP_ALWAYS_INLINE
242    explicit __codecvt_utf16(size_t __refs, unsigned long _Maxcode,
243                            codecvt_mode _Mode)
244        : codecvt<wchar_t, char, mbstate_t>(__refs), _Maxcode_(_Maxcode),
245          _Mode_(_Mode) {}
246protected:
247    virtual result
248        do_out(state_type& __st,
249               const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
250               extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
251    virtual result
252        do_in(state_type& __st,
253              const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
254              intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
255    virtual result
256        do_unshift(state_type& __st,
257                   extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
258    virtual int do_encoding() const throw();
259    virtual bool do_always_noconv() const throw();
260    virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end,
261                          size_t __mx) const;
262    virtual int do_max_length() const throw();
263};
264
265template <>
266class __codecvt_utf16<char16_t, false>
267    : public codecvt<char16_t, char, mbstate_t>
268{
269    unsigned long _Maxcode_;
270    codecvt_mode _Mode_;
271public:
272    typedef char16_t  intern_type;
273    typedef char      extern_type;
274    typedef mbstate_t state_type;
275
276    _LIBCPP_ALWAYS_INLINE
277    explicit __codecvt_utf16(size_t __refs, unsigned long _Maxcode,
278                            codecvt_mode _Mode)
279        : codecvt<char16_t, char, mbstate_t>(__refs), _Maxcode_(_Maxcode),
280          _Mode_(_Mode) {}
281protected:
282    virtual result
283        do_out(state_type& __st,
284               const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
285               extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
286    virtual result
287        do_in(state_type& __st,
288              const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
289              intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
290    virtual result
291        do_unshift(state_type& __st,
292                   extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
293    virtual int do_encoding() const throw();
294    virtual bool do_always_noconv() const throw();
295    virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end,
296                          size_t __mx) const;
297    virtual int do_max_length() const throw();
298};
299
300template <>
301class __codecvt_utf16<char16_t, true>
302    : public codecvt<char16_t, char, mbstate_t>
303{
304    unsigned long _Maxcode_;
305    codecvt_mode _Mode_;
306public:
307    typedef char16_t  intern_type;
308    typedef char      extern_type;
309    typedef mbstate_t state_type;
310
311    _LIBCPP_ALWAYS_INLINE
312    explicit __codecvt_utf16(size_t __refs, unsigned long _Maxcode,
313                            codecvt_mode _Mode)
314        : codecvt<char16_t, char, mbstate_t>(__refs), _Maxcode_(_Maxcode),
315          _Mode_(_Mode) {}
316protected:
317    virtual result
318        do_out(state_type& __st,
319               const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
320               extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
321    virtual result
322        do_in(state_type& __st,
323              const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
324              intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
325    virtual result
326        do_unshift(state_type& __st,
327                   extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
328    virtual int do_encoding() const throw();
329    virtual bool do_always_noconv() const throw();
330    virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end,
331                          size_t __mx) const;
332    virtual int do_max_length() const throw();
333};
334
335template <>
336class __codecvt_utf16<char32_t, false>
337    : public codecvt<char32_t, char, mbstate_t>
338{
339    unsigned long _Maxcode_;
340    codecvt_mode _Mode_;
341public:
342    typedef char32_t  intern_type;
343    typedef char      extern_type;
344    typedef mbstate_t state_type;
345
346    _LIBCPP_ALWAYS_INLINE
347    explicit __codecvt_utf16(size_t __refs, unsigned long _Maxcode,
348                            codecvt_mode _Mode)
349        : codecvt<char32_t, char, mbstate_t>(__refs), _Maxcode_(_Maxcode),
350          _Mode_(_Mode) {}
351protected:
352    virtual result
353        do_out(state_type& __st,
354               const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
355               extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
356    virtual result
357        do_in(state_type& __st,
358              const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
359              intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
360    virtual result
361        do_unshift(state_type& __st,
362                   extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
363    virtual int do_encoding() const throw();
364    virtual bool do_always_noconv() const throw();
365    virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end,
366                          size_t __mx) const;
367    virtual int do_max_length() const throw();
368};
369
370template <>
371class __codecvt_utf16<char32_t, true>
372    : public codecvt<char32_t, char, mbstate_t>
373{
374    unsigned long _Maxcode_;
375    codecvt_mode _Mode_;
376public:
377    typedef char32_t  intern_type;
378    typedef char      extern_type;
379    typedef mbstate_t state_type;
380
381    _LIBCPP_ALWAYS_INLINE
382    explicit __codecvt_utf16(size_t __refs, unsigned long _Maxcode,
383                            codecvt_mode _Mode)
384        : codecvt<char32_t, char, mbstate_t>(__refs), _Maxcode_(_Maxcode),
385          _Mode_(_Mode) {}
386protected:
387    virtual result
388        do_out(state_type& __st,
389               const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
390               extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
391    virtual result
392        do_in(state_type& __st,
393              const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
394              intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
395    virtual result
396        do_unshift(state_type& __st,
397                   extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
398    virtual int do_encoding() const throw();
399    virtual bool do_always_noconv() const throw();
400    virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end,
401                          size_t __mx) const;
402    virtual int do_max_length() const throw();
403};
404
405template <class _Elem, unsigned long _Maxcode = 0x10ffff,
406          codecvt_mode _Mode = (codecvt_mode)0>
407class codecvt_utf16
408    : public __codecvt_utf16<_Elem, _Mode & little_endian>
409{
410public:
411    _LIBCPP_ALWAYS_INLINE
412    explicit codecvt_utf16(size_t __refs = 0)
413        : __codecvt_utf16<_Elem, _Mode & little_endian>(__refs, _Maxcode, _Mode) {}
414
415    ~codecvt_utf16() {}
416};
417
418// codecvt_utf8_utf16
419
420template <class _Elem> class __codecvt_utf8_utf16;
421
422template <>
423class __codecvt_utf8_utf16<wchar_t>
424    : public codecvt<wchar_t, char, mbstate_t>
425{
426    unsigned long _Maxcode_;
427    codecvt_mode _Mode_;
428public:
429    typedef wchar_t   intern_type;
430    typedef char      extern_type;
431    typedef mbstate_t state_type;
432
433    _LIBCPP_ALWAYS_INLINE
434    explicit __codecvt_utf8_utf16(size_t __refs, unsigned long _Maxcode,
435                            codecvt_mode _Mode)
436        : codecvt<wchar_t, char, mbstate_t>(__refs), _Maxcode_(_Maxcode),
437          _Mode_(_Mode) {}
438protected:
439    virtual result
440        do_out(state_type& __st,
441               const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
442               extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
443    virtual result
444        do_in(state_type& __st,
445              const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
446              intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
447    virtual result
448        do_unshift(state_type& __st,
449                   extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
450    virtual int do_encoding() const throw();
451    virtual bool do_always_noconv() const throw();
452    virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end,
453                          size_t __mx) const;
454    virtual int do_max_length() const throw();
455};
456
457template <>
458class __codecvt_utf8_utf16<char32_t>
459    : public codecvt<char32_t, char, mbstate_t>
460{
461    unsigned long _Maxcode_;
462    codecvt_mode _Mode_;
463public:
464    typedef char32_t  intern_type;
465    typedef char      extern_type;
466    typedef mbstate_t state_type;
467
468    _LIBCPP_ALWAYS_INLINE
469    explicit __codecvt_utf8_utf16(size_t __refs, unsigned long _Maxcode,
470                            codecvt_mode _Mode)
471        : codecvt<char32_t, char, mbstate_t>(__refs), _Maxcode_(_Maxcode),
472          _Mode_(_Mode) {}
473protected:
474    virtual result
475        do_out(state_type& __st,
476               const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
477               extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
478    virtual result
479        do_in(state_type& __st,
480              const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
481              intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
482    virtual result
483        do_unshift(state_type& __st,
484                   extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
485    virtual int do_encoding() const throw();
486    virtual bool do_always_noconv() const throw();
487    virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end,
488                          size_t __mx) const;
489    virtual int do_max_length() const throw();
490};
491
492template <>
493class __codecvt_utf8_utf16<char16_t>
494    : public codecvt<char16_t, char, mbstate_t>
495{
496    unsigned long _Maxcode_;
497    codecvt_mode _Mode_;
498public:
499    typedef char16_t  intern_type;
500    typedef char      extern_type;
501    typedef mbstate_t state_type;
502
503    _LIBCPP_ALWAYS_INLINE
504    explicit __codecvt_utf8_utf16(size_t __refs, unsigned long _Maxcode,
505                            codecvt_mode _Mode)
506        : codecvt<char16_t, char, mbstate_t>(__refs), _Maxcode_(_Maxcode),
507          _Mode_(_Mode) {}
508protected:
509    virtual result
510        do_out(state_type& __st,
511               const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
512               extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
513    virtual result
514        do_in(state_type& __st,
515              const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
516              intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
517    virtual result
518        do_unshift(state_type& __st,
519                   extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
520    virtual int do_encoding() const throw();
521    virtual bool do_always_noconv() const throw();
522    virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end,
523                          size_t __mx) const;
524    virtual int do_max_length() const throw();
525};
526
527template <class _Elem, unsigned long _Maxcode = 0x10ffff,
528          codecvt_mode _Mode = (codecvt_mode)0>
529class codecvt_utf8_utf16
530    : public __codecvt_utf8_utf16<_Elem>
531{
532public:
533    _LIBCPP_ALWAYS_INLINE
534    explicit codecvt_utf8_utf16(size_t __refs = 0)
535        : __codecvt_utf8_utf16<_Elem>(__refs, _Maxcode, _Mode) {}
536
537    ~codecvt_utf8_utf16() {}
538};
539
540_LIBCPP_END_NAMESPACE_STD
541
542#endif  // _LIBCPP_CODECVT
543