1 // std::codecvt implementation details, DragonFly version -*- C++ -*-
3 // Copyright (C) 2015 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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
26 // ISO C++ 14882: 22.2.1.5 - Template class codecvt
29 // Written by Benjamin Kosnik <bkoz@redhat.com>
30 // Modified for DragonFly by John Marino <gnugcc@marino.st>
34 #include <cstdlib> // For MB_CUR_MAX
35 #include <climits> // For MB_LEN_MAX
37 namespace std _GLIBCXX_VISIBILITY(default)
39 _GLIBCXX_BEGIN_NAMESPACE_VERSION
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 __c_locale __old = (__c_locale)uselocale((locale_t)_M_c_locale_codecvt);
55 // wcsnrtombs is *very* fast but stops if encounters NUL characters:
56 // in case we fall back to wcrtomb and then continue, in a loop.
57 // NB: wcsnrtombs is a GNU extension
58 for (__from_next = __from, __to_next = __to;
59 __from_next < __from_end && __to_next < __to_end
62 const intern_type* __from_chunk_end = wmemchr(__from_next, L'\0',
63 __from_end - __from_next);
64 if (!__from_chunk_end)
65 __from_chunk_end = __from_end;
68 const size_t __conv = wcsnrtombs(__to_next, &__from_next,
69 __from_chunk_end - __from_next,
70 __to_end - __to_next, &__state);
71 if (__conv == static_cast<size_t>(-1))
73 // In case of error, in order to stop at the exact place we
74 // have to start again from the beginning with a series of
76 for (; __from < __from_next; ++__from)
77 __to_next += wcrtomb(__to_next, *__from, &__tmp_state);
78 __state = __tmp_state;
81 else if (__from_next && __from_next < __from_chunk_end)
88 __from_next = __from_chunk_end;
92 if (__from_next < __from_end && __ret == ok)
94 extern_type __buf[MB_LEN_MAX];
95 __tmp_state = __state;
96 const size_t __conv2 = wcrtomb(__buf, *__from_next, &__tmp_state);
97 if (__conv2 > static_cast<size_t>(__to_end - __to_next))
101 memcpy(__to_next, __buf, __conv2);
102 __state = __tmp_state;
103 __to_next += __conv2;
109 uselocale((locale_t)__old);
115 codecvt<wchar_t, char, mbstate_t>::
116 do_in(state_type& __state, const extern_type* __from,
117 const extern_type* __from_end, const extern_type*& __from_next,
118 intern_type* __to, intern_type* __to_end,
119 intern_type*& __to_next) const
122 state_type __tmp_state(__state);
124 __c_locale __old = (__c_locale)uselocale((locale_t)_M_c_locale_codecvt);
126 // mbsnrtowcs is *very* fast but stops if encounters NUL characters:
127 // in case we store a L'\0' and then continue, in a loop.
128 // NB: mbsnrtowcs is a GNU extension
129 for (__from_next = __from, __to_next = __to;
130 __from_next < __from_end && __to_next < __to_end
133 const extern_type* __from_chunk_end;
134 __from_chunk_end = static_cast<const extern_type*>(memchr(__from_next, '\0',
137 if (!__from_chunk_end)
138 __from_chunk_end = __from_end;
140 __from = __from_next;
141 size_t __conv = mbsnrtowcs(__to_next, &__from_next,
142 __from_chunk_end - __from_next,
143 __to_end - __to_next, &__state);
144 if (__conv == static_cast<size_t>(-1))
146 // In case of error, in order to stop at the exact place we
147 // have to start again from the beginning with a series of
149 for (;; ++__to_next, __from += __conv)
151 __conv = mbrtowc(__to_next, __from, __from_end - __from,
153 if (__conv == static_cast<size_t>(-1)
154 || __conv == static_cast<size_t>(-2))
157 __from_next = __from;
158 __state = __tmp_state;
161 else if (__from_next && __from_next < __from_chunk_end)
163 // It is unclear what to return in this case (see DR 382).
169 __from_next = __from_chunk_end;
173 if (__from_next < __from_end && __ret == ok)
175 if (__to_next < __to_end)
177 // XXX Probably wrong for stateful encodings
178 __tmp_state = __state;
180 *__to_next++ = L'\0';
187 uselocale((locale_t)__old);
193 codecvt<wchar_t, char, mbstate_t>::
194 do_encoding() const throw()
196 // XXX This implementation assumes that the encoding is
197 // stateless and is either single-byte or variable-width.
199 __c_locale __old = (__c_locale)uselocale((locale_t)_M_c_locale_codecvt);
202 uselocale((locale_t)__old);
207 codecvt<wchar_t, char, mbstate_t>::
208 do_max_length() const throw()
210 __c_locale __old = (__c_locale)uselocale((locale_t)_M_c_locale_codecvt);
211 // XXX Probably wrong for stateful encodings.
212 int __ret = MB_CUR_MAX;
213 uselocale((locale_t)__old);
218 codecvt<wchar_t, char, mbstate_t>::
219 do_length(state_type& __state, const extern_type* __from,
220 const extern_type* __end, size_t __max) const
223 state_type __tmp_state(__state);
225 __c_locale __old = (__c_locale)uselocale((locale_t)_M_c_locale_codecvt);
227 // mbsnrtowcs is *very* fast but stops if encounters NUL characters:
228 // in case we advance past it and then continue, in a loop.
229 // NB: mbsnrtowcs is a GNU extension
231 // A dummy internal buffer is needed in order for mbsnrtocws to consider
232 // its fourth parameter (it wouldn't with NULL as first parameter).
233 wchar_t* __to = static_cast<wchar_t*>(__builtin_alloca(sizeof(wchar_t)
235 while (__from < __end && __max)
237 const extern_type* __from_chunk_end;
238 __from_chunk_end = static_cast<const extern_type*>(memchr(__from, '\0',
241 if (!__from_chunk_end)
242 __from_chunk_end = __end;
244 const extern_type* __tmp_from = __from;
245 size_t __conv = mbsnrtowcs(__to, &__from,
246 __from_chunk_end - __from,
248 if (__conv == static_cast<size_t>(-1))
250 // In case of error, in order to stop at the exact place we
251 // have to start again from the beginning with a series of
253 for (__from = __tmp_from;; __from += __conv)
255 __conv = mbrtowc(0, __from, __end - __from,
257 if (__conv == static_cast<size_t>(-1)
258 || __conv == static_cast<size_t>(-2))
261 __state = __tmp_state;
262 __ret += __from - __tmp_from;
266 __from = __from_chunk_end;
268 __ret += __from - __tmp_from;
271 if (__from < __end && __max)
273 // XXX Probably wrong for stateful encodings
274 __tmp_state = __state;
281 uselocale((locale_t)__old);
287 _GLIBCXX_END_NAMESPACE_VERSION