1 // std::codecvt implementation details, GNU version -*- C++ -*-
3 // Copyright (C) 2002, 2003 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
60 for (__from_next = __from, __to_next = __to;
61 __from_next < __from_end && __to_next < __to_end
64 const intern_type* __from_chunk_end = wmemchr(__from_next, L'\0',
65 __from_end - __from_next);
66 if (!__from_chunk_end)
67 __from_chunk_end = __from_end;
70 const size_t __conv = wcsnrtombs(__to_next, &__from_next,
71 __from_chunk_end - __from_next,
72 __to_end - __to_next, &__state);
73 if (__conv == static_cast<size_t>(-1))
75 // In case of error, in order to stop at the exact place we
76 // have to start again from the beginning with a series of
78 for (; __from < __from_next; ++__from)
79 __to_next += wcrtomb(__to_next, *__from, &__tmp_state);
80 __state = __tmp_state;
83 else if (__from_next && __from_next < __from_chunk_end)
90 __from_next = __from_chunk_end;
94 if (__from_next < __from_end && __ret == ok)
96 extern_type __buf[MB_LEN_MAX];
97 __tmp_state = __state;
98 const size_t __conv = wcrtomb(__buf, *__from_next, &__tmp_state);
99 if (__conv > static_cast<size_t>(__to_end - __to_next))
103 memcpy(__to_next, __buf, __conv);
104 __state = __tmp_state;
111 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
119 codecvt<wchar_t, char, mbstate_t>::
120 do_in(state_type& __state, const extern_type* __from,
121 const extern_type* __from_end, const extern_type*& __from_next,
122 intern_type* __to, intern_type* __to_end,
123 intern_type*& __to_next) const
126 state_type __tmp_state(__state);
128 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
129 __c_locale __old = __uselocale(_M_c_locale_codecvt);
132 // mbsnrtowcs is *very* fast but stops if encounters NUL characters:
133 // in case we store a L'\0' and then continue, in a loop.
134 // NB: mbsnrtowcs is a GNU extension
135 for (__from_next = __from, __to_next = __to;
136 __from_next < __from_end && __to_next < __to_end
139 const extern_type* __from_chunk_end;
140 __from_chunk_end = static_cast<const extern_type*>(memchr(__from_next, '\0',
143 if (!__from_chunk_end)
144 __from_chunk_end = __from_end;
146 __from = __from_next;
147 const size_t __conv = mbsnrtowcs(__to_next, &__from_next,
148 __from_chunk_end - __from_next,
149 __to_end - __to_next, &__state);
150 if (__conv == static_cast<size_t>(-1))
152 // In case of error, in order to stop at the exact place we
153 // have to start again from the beginning with a series of
157 const size_t __conv_err = mbrtowc(__to_next, __from,
160 if (__conv_err == static_cast<size_t>(-1)
161 || __conv_err == static_cast<size_t>(-2))
163 __from += __conv_err;
165 __from_next = __from;
166 __state = __tmp_state;
169 else if (__from_next && __from_next < __from_chunk_end)
171 // It is unclear what to return in this case (see DR 382).
177 __from_next = __from_chunk_end;
181 if (__from_next < __from_end && __ret == ok)
183 if (__to_next < __to_end)
185 // XXX Probably wrong for stateful encodings
186 __tmp_state = __state;
188 *__to_next++ = L'\0';
195 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
203 codecvt<wchar_t, char, mbstate_t>::
204 do_encoding() const throw()
206 // XXX This implementation assumes that the encoding is
207 // stateless and is either single-byte or variable-width.
209 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
210 __c_locale __old = __uselocale(_M_c_locale_codecvt);
214 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
221 codecvt<wchar_t, char, mbstate_t>::
222 do_max_length() const throw()
224 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
225 __c_locale __old = __uselocale(_M_c_locale_codecvt);
227 // XXX Probably wrong for stateful encodings.
228 int __ret = MB_CUR_MAX;
229 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
236 codecvt<wchar_t, char, mbstate_t>::
237 do_length(state_type& __state, const extern_type* __from,
238 const extern_type* __end, size_t __max) const
241 state_type __tmp_state(__state);
243 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
244 __c_locale __old = __uselocale(_M_c_locale_codecvt);
247 // mbsnrtowcs is *very* fast but stops if encounters NUL characters:
248 // in case we advance past it and then continue, in a loop.
249 // NB: mbsnrtowcs is a GNU extension
251 // A dummy internal buffer is needed in order for mbsnrtocws to consider
252 // its fourth parameter (it wouldn't with NULL as first parameter).
253 wchar_t* __to = static_cast<wchar_t*>(__builtin_alloca(sizeof(wchar_t)
255 while (__from < __end && __max)
257 const extern_type* __from_chunk_end;
258 __from_chunk_end = static_cast<const extern_type*>(memchr(__from, '\0',
261 if (!__from_chunk_end)
262 __from_chunk_end = __end;
264 const extern_type* __tmp_from = __from;
265 const size_t __conv = mbsnrtowcs(__to, &__from,
266 __from_chunk_end - __from,
268 if (__conv == static_cast<size_t>(-1))
270 // In case of error, in order to stop at the exact place we
271 // have to start again from the beginning with a series of
273 for (__from = __tmp_from;;)
275 const size_t __conv_err = mbrtowc(NULL, __from, __end - __from,
277 if (__conv_err == static_cast<size_t>(-1)
278 || __conv_err == static_cast<size_t>(-2))
280 __from += __conv_err;
282 __state = __tmp_state;
283 __ret += __from - __tmp_from;
287 __from = __from_chunk_end;
289 __ret += __from - __tmp_from;
292 if (__from < __end && __max)
294 // XXX Probably wrong for stateful encodings
295 __tmp_state = __state;
302 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)