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