1 // std::codecvt implementation details, GNU version -*- C++ -*-
3 // Copyright (C) 2002 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
31 // ISO C++ 14882: 22.2.1.5 - Template class codecvt
34 // Written by Benjamin Kosnik <bkoz@redhat.com>
37 #include <bits/c++locale_internal.h>
42 #ifdef _GLIBCXX_USE_WCHAR_T
44 codecvt<wchar_t, char, mbstate_t>::
45 do_out(state_type& __state, const intern_type* __from,
46 const intern_type* __from_end, const intern_type*& __from_next,
47 extern_type* __to, extern_type* __to_end,
48 extern_type*& __to_next) const
51 state_type __tmp_state(__state);
53 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
54 __c_locale __old = __uselocale(_M_c_locale_codecvt);
57 // wcsnrtombs is *very* fast but stops if encounters NUL characters:
58 // in case we fall back to wcrtomb and then continue, in a loop.
59 // NB: wcsnrtombs is a GNU extension
62 while (__from_next < __from_end && __to_next < __to_end
65 const intern_type* __from_chunk_end = wmemchr(__from_next, L'\0',
66 __from_end - __from_next);
67 if (!__from_chunk_end)
68 __from_chunk_end = __from_end;
70 const intern_type* __tmp_from = __from_next;
71 const size_t __conv = wcsnrtombs(__to_next, &__from_next,
72 __from_chunk_end - __from_next,
73 __to_end - __to_next, &__state);
74 if (__conv == static_cast<size_t>(-1))
76 // In case of error, in order to stop at the exact place we
77 // have to start again from the beginning with a series of
79 while (__tmp_from < __from_next)
80 __to_next += wcrtomb(__to_next, *__tmp_from++, &__tmp_state);
81 __state = __tmp_state;
84 else if (__from_next && __from_next < __from_chunk_end)
91 __from_next = __from_chunk_end;
95 if (__from_next < __from_end && __ret == ok)
97 extern_type __buf[MB_LEN_MAX];
98 __tmp_state = __state;
99 const size_t __conv = wcrtomb(__buf, *__from_next, &__tmp_state);
100 if (__conv == static_cast<size_t>(-1))
102 else if (__conv > static_cast<size_t>(__to_end - __to_next))
106 memcpy(__to_next, __buf, __conv);
107 __state = __tmp_state;
114 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
122 codecvt<wchar_t, char, mbstate_t>::
123 do_in(state_type& __state, const extern_type* __from,
124 const extern_type* __from_end, const extern_type*& __from_next,
125 intern_type* __to, intern_type* __to_end,
126 intern_type*& __to_next) const
129 // This temporary state object is neccessary so __state won't be modified
130 // if [__from, __from_end) is a partial multibyte character.
131 state_type __tmp_state(__state);
132 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
133 __c_locale __old = __uselocale(_M_c_locale_codecvt);
136 // Conversion must be done by calling mbrtowc in a loop rather than
137 // by calling mbsrtowcs because mbsrtowcs assumes that the input
138 // sequence is zero-terminated.
139 while (__from < __from_end && __to < __to_end)
141 size_t __conv = mbrtowc(__to, __from, __from_end - __from,
143 if (__conv == static_cast<size_t>(-1))
148 else if (__conv == static_cast<size_t>(-2))
150 // It is unclear what to return in this case (see DR 382).
154 else if (__conv == 0)
156 // XXX Probably wrong for stateful encodings
161 __state = __tmp_state;
166 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
170 // It is not clear that __from < __from_end implies __ret != ok
172 if (__ret == ok && __from < __from_end)
175 __from_next = __from;
181 codecvt<wchar_t, char, mbstate_t>::
182 do_encoding() const throw()
184 // XXX This implementation assumes that the encoding is
185 // stateless and is either single-byte or variable-width.
187 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
188 __c_locale __old = __uselocale(_M_c_locale_codecvt);
192 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
199 codecvt<wchar_t, char, mbstate_t>::
200 do_max_length() const throw()
202 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
203 __c_locale __old = __uselocale(_M_c_locale_codecvt);
205 // XXX Probably wrong for stateful encodings.
206 int __ret = MB_CUR_MAX;
207 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
214 codecvt<wchar_t, char, mbstate_t>::
215 do_length(state_type& __state, const extern_type* __from,
216 const extern_type* __end, size_t __max) const
219 state_type __tmp_state(__state);
220 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
221 __c_locale __old = __uselocale(_M_c_locale_codecvt);
224 while (__from < __end && __max)
226 size_t __conv = mbrtowc(NULL, __from, __end - __from, &__tmp_state);
227 if (__conv == static_cast<size_t>(-1))
229 // Invalid source character
232 else if (__conv == static_cast<size_t>(-2))
234 // Remainder of input does not form a complete destination
238 else if (__conv == 0)
240 // XXX Probably wrong for stateful encodings
244 __state = __tmp_state;
250 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)