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 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
155 for (;; ++__to_next
, __from
+= __conv
)
157 __conv
= mbrtowc(__to_next
, __from
, __from_end
- __from
,
159 if (__conv
== static_cast<size_t>(-1)
160 || __conv
== static_cast<size_t>(-2))
163 __from_next
= __from
;
164 __state
= __tmp_state
;
167 else if (__from_next
&& __from_next
< __from_chunk_end
)
169 // It is unclear what to return in this case (see DR 382).
175 __from_next
= __from_chunk_end
;
179 if (__from_next
< __from_end
&& __ret
== ok
)
181 if (__to_next
< __to_end
)
183 // XXX Probably wrong for stateful encodings
184 __tmp_state
= __state
;
186 *__to_next
++ = L
'\0';
193 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
201 codecvt
<wchar_t, char, mbstate_t>::
202 do_encoding() const throw()
204 // XXX This implementation assumes that the encoding is
205 // stateless and is either single-byte or variable-width.
207 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
208 __c_locale __old
= __uselocale(_M_c_locale_codecvt
);
212 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
219 codecvt
<wchar_t, char, mbstate_t>::
220 do_max_length() const throw()
222 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
223 __c_locale __old
= __uselocale(_M_c_locale_codecvt
);
225 // XXX Probably wrong for stateful encodings.
226 int __ret
= MB_CUR_MAX
;
227 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
234 codecvt
<wchar_t, char, mbstate_t>::
235 do_length(state_type
& __state
, const extern_type
* __from
,
236 const extern_type
* __end
, size_t __max
) const
239 state_type
__tmp_state(__state
);
241 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
242 __c_locale __old
= __uselocale(_M_c_locale_codecvt
);
245 // mbsnrtowcs is *very* fast but stops if encounters NUL characters:
246 // in case we advance past it and then continue, in a loop.
247 // NB: mbsnrtowcs is a GNU extension
249 // A dummy internal buffer is needed in order for mbsnrtocws to consider
250 // its fourth parameter (it wouldn't with NULL as first parameter).
251 wchar_t* __to
= static_cast<wchar_t*>(__builtin_alloca(sizeof(wchar_t)
253 while (__from
< __end
&& __max
)
255 const extern_type
* __from_chunk_end
;
256 __from_chunk_end
= static_cast<const extern_type
*>(memchr(__from
, '\0',
259 if (!__from_chunk_end
)
260 __from_chunk_end
= __end
;
262 const extern_type
* __tmp_from
= __from
;
263 size_t __conv
= mbsnrtowcs(__to
, &__from
,
264 __from_chunk_end
- __from
,
266 if (__conv
== static_cast<size_t>(-1))
268 // In case of error, in order to stop at the exact place we
269 // have to start again from the beginning with a series of
271 for (__from
= __tmp_from
;; __from
+= __conv
)
273 __conv
= mbrtowc(NULL
, __from
, __end
- __from
,
275 if (__conv
== static_cast<size_t>(-1)
276 || __conv
== static_cast<size_t>(-2))
279 __state
= __tmp_state
;
280 __ret
+= __from
- __tmp_from
;
284 __from
= __from_chunk_end
;
286 __ret
+= __from
- __tmp_from
;
289 if (__from
< __end
&& __max
)
291 // XXX Probably wrong for stateful encodings
292 __tmp_state
= __state
;
299 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)