xref: /freebsd/contrib/llvm-project/libcxx/src/locale.cpp (revision 0b57cec536236d46e3dba9bd041533462f33dbb7)
1*0b57cec5SDimitry Andric //===------------------------- locale.cpp ---------------------------------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric 
9*0b57cec5SDimitry Andric // On Solaris, we need to define something to make the C99 parts of localeconv
10*0b57cec5SDimitry Andric // visible.
11*0b57cec5SDimitry Andric #ifdef __sun__
12*0b57cec5SDimitry Andric #define _LCONV_C99
13*0b57cec5SDimitry Andric #endif
14*0b57cec5SDimitry Andric 
15*0b57cec5SDimitry Andric #include "string"
16*0b57cec5SDimitry Andric #include "locale"
17*0b57cec5SDimitry Andric #include "codecvt"
18*0b57cec5SDimitry Andric #include "vector"
19*0b57cec5SDimitry Andric #include "algorithm"
20*0b57cec5SDimitry Andric #include "typeinfo"
21*0b57cec5SDimitry Andric #ifndef _LIBCPP_NO_EXCEPTIONS
22*0b57cec5SDimitry Andric #  include "type_traits"
23*0b57cec5SDimitry Andric #endif
24*0b57cec5SDimitry Andric #include "clocale"
25*0b57cec5SDimitry Andric #include "cstring"
26*0b57cec5SDimitry Andric #if defined(_LIBCPP_MSVCRT)
27*0b57cec5SDimitry Andric #define _CTYPE_DISABLE_MACROS
28*0b57cec5SDimitry Andric #endif
29*0b57cec5SDimitry Andric #include "cwctype"
30*0b57cec5SDimitry Andric #include "__sso_allocator"
31*0b57cec5SDimitry Andric #if defined(_LIBCPP_MSVCRT) || defined(__MINGW32__)
32*0b57cec5SDimitry Andric #include "support/win32/locale_win32.h"
33*0b57cec5SDimitry Andric #elif !defined(__BIONIC__)
34*0b57cec5SDimitry Andric #include <langinfo.h>
35*0b57cec5SDimitry Andric #endif
36*0b57cec5SDimitry Andric #include <stdlib.h>
37*0b57cec5SDimitry Andric #include <stdio.h>
38*0b57cec5SDimitry Andric #include "include/atomic_support.h"
39*0b57cec5SDimitry Andric #include "__undef_macros"
40*0b57cec5SDimitry Andric 
41*0b57cec5SDimitry Andric // On Linux, wint_t and wchar_t have different signed-ness, and this causes
42*0b57cec5SDimitry Andric // lots of noise in the build log, but no bugs that I know of.
43*0b57cec5SDimitry Andric #if defined(__clang__)
44*0b57cec5SDimitry Andric #pragma clang diagnostic ignored "-Wsign-conversion"
45*0b57cec5SDimitry Andric #endif
46*0b57cec5SDimitry Andric 
47*0b57cec5SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
48*0b57cec5SDimitry Andric 
49*0b57cec5SDimitry Andric struct __libcpp_unique_locale {
50*0b57cec5SDimitry Andric   __libcpp_unique_locale(const char* nm) : __loc_(newlocale(LC_ALL_MASK, nm, 0)) {}
51*0b57cec5SDimitry Andric 
52*0b57cec5SDimitry Andric   ~__libcpp_unique_locale() {
53*0b57cec5SDimitry Andric     if (__loc_)
54*0b57cec5SDimitry Andric       freelocale(__loc_);
55*0b57cec5SDimitry Andric   }
56*0b57cec5SDimitry Andric 
57*0b57cec5SDimitry Andric   explicit operator bool() const { return __loc_; }
58*0b57cec5SDimitry Andric 
59*0b57cec5SDimitry Andric   locale_t& get() { return __loc_; }
60*0b57cec5SDimitry Andric 
61*0b57cec5SDimitry Andric   locale_t __loc_;
62*0b57cec5SDimitry Andric private:
63*0b57cec5SDimitry Andric   __libcpp_unique_locale(__libcpp_unique_locale const&);
64*0b57cec5SDimitry Andric   __libcpp_unique_locale& operator=(__libcpp_unique_locale const&);
65*0b57cec5SDimitry Andric };
66*0b57cec5SDimitry Andric 
67*0b57cec5SDimitry Andric #ifdef __cloc_defined
68*0b57cec5SDimitry Andric locale_t __cloc() {
69*0b57cec5SDimitry Andric   // In theory this could create a race condition. In practice
70*0b57cec5SDimitry Andric   // the race condition is non-fatal since it will just create
71*0b57cec5SDimitry Andric   // a little resource leak. Better approach would be appreciated.
72*0b57cec5SDimitry Andric   static locale_t result = newlocale(LC_ALL_MASK, "C", 0);
73*0b57cec5SDimitry Andric   return result;
74*0b57cec5SDimitry Andric }
75*0b57cec5SDimitry Andric #endif // __cloc_defined
76*0b57cec5SDimitry Andric 
77*0b57cec5SDimitry Andric namespace {
78*0b57cec5SDimitry Andric 
79*0b57cec5SDimitry Andric struct release
80*0b57cec5SDimitry Andric {
81*0b57cec5SDimitry Andric     void operator()(locale::facet* p) {p->__release_shared();}
82*0b57cec5SDimitry Andric };
83*0b57cec5SDimitry Andric 
84*0b57cec5SDimitry Andric template <class T, class A0>
85*0b57cec5SDimitry Andric inline
86*0b57cec5SDimitry Andric T&
87*0b57cec5SDimitry Andric make(A0 a0)
88*0b57cec5SDimitry Andric {
89*0b57cec5SDimitry Andric     static typename aligned_storage<sizeof(T)>::type buf;
90*0b57cec5SDimitry Andric     auto *obj = ::new (&buf) T(a0);
91*0b57cec5SDimitry Andric     return *obj;
92*0b57cec5SDimitry Andric }
93*0b57cec5SDimitry Andric 
94*0b57cec5SDimitry Andric template <class T, class A0, class A1>
95*0b57cec5SDimitry Andric inline
96*0b57cec5SDimitry Andric T&
97*0b57cec5SDimitry Andric make(A0 a0, A1 a1)
98*0b57cec5SDimitry Andric {
99*0b57cec5SDimitry Andric     static typename aligned_storage<sizeof(T)>::type buf;
100*0b57cec5SDimitry Andric     ::new (&buf) T(a0, a1);
101*0b57cec5SDimitry Andric     return *reinterpret_cast<T*>(&buf);
102*0b57cec5SDimitry Andric }
103*0b57cec5SDimitry Andric 
104*0b57cec5SDimitry Andric template <class T, class A0, class A1, class A2>
105*0b57cec5SDimitry Andric inline
106*0b57cec5SDimitry Andric T&
107*0b57cec5SDimitry Andric make(A0 a0, A1 a1, A2 a2)
108*0b57cec5SDimitry Andric {
109*0b57cec5SDimitry Andric     static typename aligned_storage<sizeof(T)>::type buf;
110*0b57cec5SDimitry Andric     auto *obj = ::new (&buf) T(a0, a1, a2);
111*0b57cec5SDimitry Andric     return *obj;
112*0b57cec5SDimitry Andric }
113*0b57cec5SDimitry Andric 
114*0b57cec5SDimitry Andric template <typename T, size_t N>
115*0b57cec5SDimitry Andric inline
116*0b57cec5SDimitry Andric _LIBCPP_CONSTEXPR
117*0b57cec5SDimitry Andric size_t
118*0b57cec5SDimitry Andric countof(const T (&)[N])
119*0b57cec5SDimitry Andric {
120*0b57cec5SDimitry Andric     return N;
121*0b57cec5SDimitry Andric }
122*0b57cec5SDimitry Andric 
123*0b57cec5SDimitry Andric template <typename T>
124*0b57cec5SDimitry Andric inline
125*0b57cec5SDimitry Andric _LIBCPP_CONSTEXPR
126*0b57cec5SDimitry Andric size_t
127*0b57cec5SDimitry Andric countof(const T * const begin, const T * const end)
128*0b57cec5SDimitry Andric {
129*0b57cec5SDimitry Andric     return static_cast<size_t>(end - begin);
130*0b57cec5SDimitry Andric }
131*0b57cec5SDimitry Andric 
132*0b57cec5SDimitry Andric _LIBCPP_NORETURN static void __throw_runtime_error(const string &msg)
133*0b57cec5SDimitry Andric {
134*0b57cec5SDimitry Andric #ifndef _LIBCPP_NO_EXCEPTIONS
135*0b57cec5SDimitry Andric     throw runtime_error(msg);
136*0b57cec5SDimitry Andric #else
137*0b57cec5SDimitry Andric     (void)msg;
138*0b57cec5SDimitry Andric     _VSTD::abort();
139*0b57cec5SDimitry Andric #endif
140*0b57cec5SDimitry Andric }
141*0b57cec5SDimitry Andric 
142*0b57cec5SDimitry Andric }
143*0b57cec5SDimitry Andric 
144*0b57cec5SDimitry Andric #if defined(_AIX)
145*0b57cec5SDimitry Andric // Set priority to INT_MIN + 256 + 150
146*0b57cec5SDimitry Andric # pragma priority ( -2147483242 )
147*0b57cec5SDimitry Andric #endif
148*0b57cec5SDimitry Andric 
149*0b57cec5SDimitry Andric const locale::category locale::none;
150*0b57cec5SDimitry Andric const locale::category locale::collate;
151*0b57cec5SDimitry Andric const locale::category locale::ctype;
152*0b57cec5SDimitry Andric const locale::category locale::monetary;
153*0b57cec5SDimitry Andric const locale::category locale::numeric;
154*0b57cec5SDimitry Andric const locale::category locale::time;
155*0b57cec5SDimitry Andric const locale::category locale::messages;
156*0b57cec5SDimitry Andric const locale::category locale::all;
157*0b57cec5SDimitry Andric 
158*0b57cec5SDimitry Andric class _LIBCPP_HIDDEN locale::__imp
159*0b57cec5SDimitry Andric     : public facet
160*0b57cec5SDimitry Andric {
161*0b57cec5SDimitry Andric     enum {N = 28};
162*0b57cec5SDimitry Andric #if defined(_LIBCPP_COMPILER_MSVC)
163*0b57cec5SDimitry Andric // FIXME: MSVC doesn't support aligned parameters by value.
164*0b57cec5SDimitry Andric // I can't get the __sso_allocator to work here
165*0b57cec5SDimitry Andric // for MSVC I think for this reason.
166*0b57cec5SDimitry Andric     vector<facet*> facets_;
167*0b57cec5SDimitry Andric #else
168*0b57cec5SDimitry Andric     vector<facet*, __sso_allocator<facet*, N> > facets_;
169*0b57cec5SDimitry Andric #endif
170*0b57cec5SDimitry Andric     string         name_;
171*0b57cec5SDimitry Andric public:
172*0b57cec5SDimitry Andric     explicit __imp(size_t refs = 0);
173*0b57cec5SDimitry Andric     explicit __imp(const string& name, size_t refs = 0);
174*0b57cec5SDimitry Andric     __imp(const __imp&);
175*0b57cec5SDimitry Andric     __imp(const __imp&, const string&, locale::category c);
176*0b57cec5SDimitry Andric     __imp(const __imp& other, const __imp& one, locale::category c);
177*0b57cec5SDimitry Andric     __imp(const __imp&, facet* f, long id);
178*0b57cec5SDimitry Andric     ~__imp();
179*0b57cec5SDimitry Andric 
180*0b57cec5SDimitry Andric     const string& name() const {return name_;}
181*0b57cec5SDimitry Andric     bool has_facet(long id) const
182*0b57cec5SDimitry Andric         {return static_cast<size_t>(id) < facets_.size() && facets_[static_cast<size_t>(id)];}
183*0b57cec5SDimitry Andric     const locale::facet* use_facet(long id) const;
184*0b57cec5SDimitry Andric 
185*0b57cec5SDimitry Andric     static const locale& make_classic();
186*0b57cec5SDimitry Andric     static       locale& make_global();
187*0b57cec5SDimitry Andric private:
188*0b57cec5SDimitry Andric     void install(facet* f, long id);
189*0b57cec5SDimitry Andric     template <class F> void install(F* f) {install(f, f->id.__get());}
190*0b57cec5SDimitry Andric     template <class F> void install_from(const __imp& other);
191*0b57cec5SDimitry Andric };
192*0b57cec5SDimitry Andric 
193*0b57cec5SDimitry Andric locale::__imp::__imp(size_t refs)
194*0b57cec5SDimitry Andric     : facet(refs),
195*0b57cec5SDimitry Andric       facets_(N),
196*0b57cec5SDimitry Andric       name_("C")
197*0b57cec5SDimitry Andric {
198*0b57cec5SDimitry Andric     facets_.clear();
199*0b57cec5SDimitry Andric     install(&make<_VSTD::collate<char> >(1u));
200*0b57cec5SDimitry Andric     install(&make<_VSTD::collate<wchar_t> >(1u));
201*0b57cec5SDimitry Andric     install(&make<_VSTD::ctype<char> >(nullptr, false, 1u));
202*0b57cec5SDimitry Andric     install(&make<_VSTD::ctype<wchar_t> >(1u));
203*0b57cec5SDimitry Andric     install(&make<codecvt<char, char, mbstate_t> >(1u));
204*0b57cec5SDimitry Andric     install(&make<codecvt<wchar_t, char, mbstate_t> >(1u));
205*0b57cec5SDimitry Andric     install(&make<codecvt<char16_t, char, mbstate_t> >(1u));
206*0b57cec5SDimitry Andric     install(&make<codecvt<char32_t, char, mbstate_t> >(1u));
207*0b57cec5SDimitry Andric     install(&make<numpunct<char> >(1u));
208*0b57cec5SDimitry Andric     install(&make<numpunct<wchar_t> >(1u));
209*0b57cec5SDimitry Andric     install(&make<num_get<char> >(1u));
210*0b57cec5SDimitry Andric     install(&make<num_get<wchar_t> >(1u));
211*0b57cec5SDimitry Andric     install(&make<num_put<char> >(1u));
212*0b57cec5SDimitry Andric     install(&make<num_put<wchar_t> >(1u));
213*0b57cec5SDimitry Andric     install(&make<moneypunct<char, false> >(1u));
214*0b57cec5SDimitry Andric     install(&make<moneypunct<char, true> >(1u));
215*0b57cec5SDimitry Andric     install(&make<moneypunct<wchar_t, false> >(1u));
216*0b57cec5SDimitry Andric     install(&make<moneypunct<wchar_t, true> >(1u));
217*0b57cec5SDimitry Andric     install(&make<money_get<char> >(1u));
218*0b57cec5SDimitry Andric     install(&make<money_get<wchar_t> >(1u));
219*0b57cec5SDimitry Andric     install(&make<money_put<char> >(1u));
220*0b57cec5SDimitry Andric     install(&make<money_put<wchar_t> >(1u));
221*0b57cec5SDimitry Andric     install(&make<time_get<char> >(1u));
222*0b57cec5SDimitry Andric     install(&make<time_get<wchar_t> >(1u));
223*0b57cec5SDimitry Andric     install(&make<time_put<char> >(1u));
224*0b57cec5SDimitry Andric     install(&make<time_put<wchar_t> >(1u));
225*0b57cec5SDimitry Andric     install(&make<_VSTD::messages<char> >(1u));
226*0b57cec5SDimitry Andric     install(&make<_VSTD::messages<wchar_t> >(1u));
227*0b57cec5SDimitry Andric }
228*0b57cec5SDimitry Andric 
229*0b57cec5SDimitry Andric locale::__imp::__imp(const string& name, size_t refs)
230*0b57cec5SDimitry Andric     : facet(refs),
231*0b57cec5SDimitry Andric       facets_(N),
232*0b57cec5SDimitry Andric       name_(name)
233*0b57cec5SDimitry Andric {
234*0b57cec5SDimitry Andric #ifndef _LIBCPP_NO_EXCEPTIONS
235*0b57cec5SDimitry Andric     try
236*0b57cec5SDimitry Andric     {
237*0b57cec5SDimitry Andric #endif  // _LIBCPP_NO_EXCEPTIONS
238*0b57cec5SDimitry Andric         facets_ = locale::classic().__locale_->facets_;
239*0b57cec5SDimitry Andric         for (unsigned i = 0; i < facets_.size(); ++i)
240*0b57cec5SDimitry Andric             if (facets_[i])
241*0b57cec5SDimitry Andric                 facets_[i]->__add_shared();
242*0b57cec5SDimitry Andric         install(new collate_byname<char>(name_));
243*0b57cec5SDimitry Andric         install(new collate_byname<wchar_t>(name_));
244*0b57cec5SDimitry Andric         install(new ctype_byname<char>(name_));
245*0b57cec5SDimitry Andric         install(new ctype_byname<wchar_t>(name_));
246*0b57cec5SDimitry Andric         install(new codecvt_byname<char, char, mbstate_t>(name_));
247*0b57cec5SDimitry Andric         install(new codecvt_byname<wchar_t, char, mbstate_t>(name_));
248*0b57cec5SDimitry Andric         install(new codecvt_byname<char16_t, char, mbstate_t>(name_));
249*0b57cec5SDimitry Andric         install(new codecvt_byname<char32_t, char, mbstate_t>(name_));
250*0b57cec5SDimitry Andric         install(new numpunct_byname<char>(name_));
251*0b57cec5SDimitry Andric         install(new numpunct_byname<wchar_t>(name_));
252*0b57cec5SDimitry Andric         install(new moneypunct_byname<char, false>(name_));
253*0b57cec5SDimitry Andric         install(new moneypunct_byname<char, true>(name_));
254*0b57cec5SDimitry Andric         install(new moneypunct_byname<wchar_t, false>(name_));
255*0b57cec5SDimitry Andric         install(new moneypunct_byname<wchar_t, true>(name_));
256*0b57cec5SDimitry Andric         install(new time_get_byname<char>(name_));
257*0b57cec5SDimitry Andric         install(new time_get_byname<wchar_t>(name_));
258*0b57cec5SDimitry Andric         install(new time_put_byname<char>(name_));
259*0b57cec5SDimitry Andric         install(new time_put_byname<wchar_t>(name_));
260*0b57cec5SDimitry Andric         install(new messages_byname<char>(name_));
261*0b57cec5SDimitry Andric         install(new messages_byname<wchar_t>(name_));
262*0b57cec5SDimitry Andric #ifndef _LIBCPP_NO_EXCEPTIONS
263*0b57cec5SDimitry Andric     }
264*0b57cec5SDimitry Andric     catch (...)
265*0b57cec5SDimitry Andric     {
266*0b57cec5SDimitry Andric         for (unsigned i = 0; i < facets_.size(); ++i)
267*0b57cec5SDimitry Andric             if (facets_[i])
268*0b57cec5SDimitry Andric                 facets_[i]->__release_shared();
269*0b57cec5SDimitry Andric         throw;
270*0b57cec5SDimitry Andric     }
271*0b57cec5SDimitry Andric #endif  // _LIBCPP_NO_EXCEPTIONS
272*0b57cec5SDimitry Andric }
273*0b57cec5SDimitry Andric 
274*0b57cec5SDimitry Andric // NOTE avoid the `base class should be explicitly initialized in the
275*0b57cec5SDimitry Andric // copy constructor` warning emitted by GCC
276*0b57cec5SDimitry Andric #if defined(__clang__) || _GNUC_VER >= 406
277*0b57cec5SDimitry Andric #pragma GCC diagnostic push
278*0b57cec5SDimitry Andric #pragma GCC diagnostic ignored "-Wextra"
279*0b57cec5SDimitry Andric #endif
280*0b57cec5SDimitry Andric 
281*0b57cec5SDimitry Andric locale::__imp::__imp(const __imp& other)
282*0b57cec5SDimitry Andric     : facets_(max<size_t>(N, other.facets_.size())),
283*0b57cec5SDimitry Andric       name_(other.name_)
284*0b57cec5SDimitry Andric {
285*0b57cec5SDimitry Andric     facets_ = other.facets_;
286*0b57cec5SDimitry Andric     for (unsigned i = 0; i < facets_.size(); ++i)
287*0b57cec5SDimitry Andric         if (facets_[i])
288*0b57cec5SDimitry Andric             facets_[i]->__add_shared();
289*0b57cec5SDimitry Andric }
290*0b57cec5SDimitry Andric 
291*0b57cec5SDimitry Andric #if defined(__clang__) || _GNUC_VER >= 406
292*0b57cec5SDimitry Andric #pragma GCC diagnostic pop
293*0b57cec5SDimitry Andric #endif
294*0b57cec5SDimitry Andric 
295*0b57cec5SDimitry Andric locale::__imp::__imp(const __imp& other, const string& name, locale::category c)
296*0b57cec5SDimitry Andric     : facets_(N),
297*0b57cec5SDimitry Andric       name_("*")
298*0b57cec5SDimitry Andric {
299*0b57cec5SDimitry Andric     facets_ = other.facets_;
300*0b57cec5SDimitry Andric     for (unsigned i = 0; i < facets_.size(); ++i)
301*0b57cec5SDimitry Andric         if (facets_[i])
302*0b57cec5SDimitry Andric             facets_[i]->__add_shared();
303*0b57cec5SDimitry Andric #ifndef _LIBCPP_NO_EXCEPTIONS
304*0b57cec5SDimitry Andric     try
305*0b57cec5SDimitry Andric     {
306*0b57cec5SDimitry Andric #endif  // _LIBCPP_NO_EXCEPTIONS
307*0b57cec5SDimitry Andric         if (c & locale::collate)
308*0b57cec5SDimitry Andric         {
309*0b57cec5SDimitry Andric             install(new collate_byname<char>(name));
310*0b57cec5SDimitry Andric             install(new collate_byname<wchar_t>(name));
311*0b57cec5SDimitry Andric         }
312*0b57cec5SDimitry Andric         if (c & locale::ctype)
313*0b57cec5SDimitry Andric         {
314*0b57cec5SDimitry Andric             install(new ctype_byname<char>(name));
315*0b57cec5SDimitry Andric             install(new ctype_byname<wchar_t>(name));
316*0b57cec5SDimitry Andric             install(new codecvt_byname<char, char, mbstate_t>(name));
317*0b57cec5SDimitry Andric             install(new codecvt_byname<wchar_t, char, mbstate_t>(name));
318*0b57cec5SDimitry Andric             install(new codecvt_byname<char16_t, char, mbstate_t>(name));
319*0b57cec5SDimitry Andric             install(new codecvt_byname<char32_t, char, mbstate_t>(name));
320*0b57cec5SDimitry Andric         }
321*0b57cec5SDimitry Andric         if (c & locale::monetary)
322*0b57cec5SDimitry Andric         {
323*0b57cec5SDimitry Andric             install(new moneypunct_byname<char, false>(name));
324*0b57cec5SDimitry Andric             install(new moneypunct_byname<char, true>(name));
325*0b57cec5SDimitry Andric             install(new moneypunct_byname<wchar_t, false>(name));
326*0b57cec5SDimitry Andric             install(new moneypunct_byname<wchar_t, true>(name));
327*0b57cec5SDimitry Andric         }
328*0b57cec5SDimitry Andric         if (c & locale::numeric)
329*0b57cec5SDimitry Andric         {
330*0b57cec5SDimitry Andric             install(new numpunct_byname<char>(name));
331*0b57cec5SDimitry Andric             install(new numpunct_byname<wchar_t>(name));
332*0b57cec5SDimitry Andric         }
333*0b57cec5SDimitry Andric         if (c & locale::time)
334*0b57cec5SDimitry Andric         {
335*0b57cec5SDimitry Andric             install(new time_get_byname<char>(name));
336*0b57cec5SDimitry Andric             install(new time_get_byname<wchar_t>(name));
337*0b57cec5SDimitry Andric             install(new time_put_byname<char>(name));
338*0b57cec5SDimitry Andric             install(new time_put_byname<wchar_t>(name));
339*0b57cec5SDimitry Andric         }
340*0b57cec5SDimitry Andric         if (c & locale::messages)
341*0b57cec5SDimitry Andric         {
342*0b57cec5SDimitry Andric             install(new messages_byname<char>(name));
343*0b57cec5SDimitry Andric             install(new messages_byname<wchar_t>(name));
344*0b57cec5SDimitry Andric         }
345*0b57cec5SDimitry Andric #ifndef _LIBCPP_NO_EXCEPTIONS
346*0b57cec5SDimitry Andric     }
347*0b57cec5SDimitry Andric     catch (...)
348*0b57cec5SDimitry Andric     {
349*0b57cec5SDimitry Andric         for (unsigned i = 0; i < facets_.size(); ++i)
350*0b57cec5SDimitry Andric             if (facets_[i])
351*0b57cec5SDimitry Andric                 facets_[i]->__release_shared();
352*0b57cec5SDimitry Andric         throw;
353*0b57cec5SDimitry Andric     }
354*0b57cec5SDimitry Andric #endif  // _LIBCPP_NO_EXCEPTIONS
355*0b57cec5SDimitry Andric }
356*0b57cec5SDimitry Andric 
357*0b57cec5SDimitry Andric template<class F>
358*0b57cec5SDimitry Andric inline
359*0b57cec5SDimitry Andric void
360*0b57cec5SDimitry Andric locale::__imp::install_from(const locale::__imp& one)
361*0b57cec5SDimitry Andric {
362*0b57cec5SDimitry Andric     long id = F::id.__get();
363*0b57cec5SDimitry Andric     install(const_cast<F*>(static_cast<const F*>(one.use_facet(id))), id);
364*0b57cec5SDimitry Andric }
365*0b57cec5SDimitry Andric 
366*0b57cec5SDimitry Andric locale::__imp::__imp(const __imp& other, const __imp& one, locale::category c)
367*0b57cec5SDimitry Andric     : facets_(N),
368*0b57cec5SDimitry Andric       name_("*")
369*0b57cec5SDimitry Andric {
370*0b57cec5SDimitry Andric     facets_ = other.facets_;
371*0b57cec5SDimitry Andric     for (unsigned i = 0; i < facets_.size(); ++i)
372*0b57cec5SDimitry Andric         if (facets_[i])
373*0b57cec5SDimitry Andric             facets_[i]->__add_shared();
374*0b57cec5SDimitry Andric #ifndef _LIBCPP_NO_EXCEPTIONS
375*0b57cec5SDimitry Andric     try
376*0b57cec5SDimitry Andric     {
377*0b57cec5SDimitry Andric #endif  // _LIBCPP_NO_EXCEPTIONS
378*0b57cec5SDimitry Andric         if (c & locale::collate)
379*0b57cec5SDimitry Andric         {
380*0b57cec5SDimitry Andric             install_from<_VSTD::collate<char> >(one);
381*0b57cec5SDimitry Andric             install_from<_VSTD::collate<wchar_t> >(one);
382*0b57cec5SDimitry Andric         }
383*0b57cec5SDimitry Andric         if (c & locale::ctype)
384*0b57cec5SDimitry Andric         {
385*0b57cec5SDimitry Andric             install_from<_VSTD::ctype<char> >(one);
386*0b57cec5SDimitry Andric             install_from<_VSTD::ctype<wchar_t> >(one);
387*0b57cec5SDimitry Andric             install_from<_VSTD::codecvt<char, char, mbstate_t> >(one);
388*0b57cec5SDimitry Andric             install_from<_VSTD::codecvt<char16_t, char, mbstate_t> >(one);
389*0b57cec5SDimitry Andric             install_from<_VSTD::codecvt<char32_t, char, mbstate_t> >(one);
390*0b57cec5SDimitry Andric             install_from<_VSTD::codecvt<wchar_t, char, mbstate_t> >(one);
391*0b57cec5SDimitry Andric         }
392*0b57cec5SDimitry Andric         if (c & locale::monetary)
393*0b57cec5SDimitry Andric         {
394*0b57cec5SDimitry Andric             install_from<moneypunct<char, false> >(one);
395*0b57cec5SDimitry Andric             install_from<moneypunct<char, true> >(one);
396*0b57cec5SDimitry Andric             install_from<moneypunct<wchar_t, false> >(one);
397*0b57cec5SDimitry Andric             install_from<moneypunct<wchar_t, true> >(one);
398*0b57cec5SDimitry Andric             install_from<money_get<char> >(one);
399*0b57cec5SDimitry Andric             install_from<money_get<wchar_t> >(one);
400*0b57cec5SDimitry Andric             install_from<money_put<char> >(one);
401*0b57cec5SDimitry Andric             install_from<money_put<wchar_t> >(one);
402*0b57cec5SDimitry Andric         }
403*0b57cec5SDimitry Andric         if (c & locale::numeric)
404*0b57cec5SDimitry Andric         {
405*0b57cec5SDimitry Andric             install_from<numpunct<char> >(one);
406*0b57cec5SDimitry Andric             install_from<numpunct<wchar_t> >(one);
407*0b57cec5SDimitry Andric             install_from<num_get<char> >(one);
408*0b57cec5SDimitry Andric             install_from<num_get<wchar_t> >(one);
409*0b57cec5SDimitry Andric             install_from<num_put<char> >(one);
410*0b57cec5SDimitry Andric             install_from<num_put<wchar_t> >(one);
411*0b57cec5SDimitry Andric         }
412*0b57cec5SDimitry Andric         if (c & locale::time)
413*0b57cec5SDimitry Andric         {
414*0b57cec5SDimitry Andric             install_from<time_get<char> >(one);
415*0b57cec5SDimitry Andric             install_from<time_get<wchar_t> >(one);
416*0b57cec5SDimitry Andric             install_from<time_put<char> >(one);
417*0b57cec5SDimitry Andric             install_from<time_put<wchar_t> >(one);
418*0b57cec5SDimitry Andric         }
419*0b57cec5SDimitry Andric         if (c & locale::messages)
420*0b57cec5SDimitry Andric         {
421*0b57cec5SDimitry Andric             install_from<_VSTD::messages<char> >(one);
422*0b57cec5SDimitry Andric             install_from<_VSTD::messages<wchar_t> >(one);
423*0b57cec5SDimitry Andric         }
424*0b57cec5SDimitry Andric #ifndef _LIBCPP_NO_EXCEPTIONS
425*0b57cec5SDimitry Andric     }
426*0b57cec5SDimitry Andric     catch (...)
427*0b57cec5SDimitry Andric     {
428*0b57cec5SDimitry Andric         for (unsigned i = 0; i < facets_.size(); ++i)
429*0b57cec5SDimitry Andric             if (facets_[i])
430*0b57cec5SDimitry Andric                 facets_[i]->__release_shared();
431*0b57cec5SDimitry Andric         throw;
432*0b57cec5SDimitry Andric     }
433*0b57cec5SDimitry Andric #endif  // _LIBCPP_NO_EXCEPTIONS
434*0b57cec5SDimitry Andric }
435*0b57cec5SDimitry Andric 
436*0b57cec5SDimitry Andric locale::__imp::__imp(const __imp& other, facet* f, long id)
437*0b57cec5SDimitry Andric     : facets_(max<size_t>(N, other.facets_.size()+1)),
438*0b57cec5SDimitry Andric       name_("*")
439*0b57cec5SDimitry Andric {
440*0b57cec5SDimitry Andric     f->__add_shared();
441*0b57cec5SDimitry Andric     unique_ptr<facet, release> hold(f);
442*0b57cec5SDimitry Andric     facets_ = other.facets_;
443*0b57cec5SDimitry Andric     for (unsigned i = 0; i < other.facets_.size(); ++i)
444*0b57cec5SDimitry Andric         if (facets_[i])
445*0b57cec5SDimitry Andric             facets_[i]->__add_shared();
446*0b57cec5SDimitry Andric     install(hold.get(), id);
447*0b57cec5SDimitry Andric }
448*0b57cec5SDimitry Andric 
449*0b57cec5SDimitry Andric locale::__imp::~__imp()
450*0b57cec5SDimitry Andric {
451*0b57cec5SDimitry Andric     for (unsigned i = 0; i < facets_.size(); ++i)
452*0b57cec5SDimitry Andric         if (facets_[i])
453*0b57cec5SDimitry Andric             facets_[i]->__release_shared();
454*0b57cec5SDimitry Andric }
455*0b57cec5SDimitry Andric 
456*0b57cec5SDimitry Andric void
457*0b57cec5SDimitry Andric locale::__imp::install(facet* f, long id)
458*0b57cec5SDimitry Andric {
459*0b57cec5SDimitry Andric     f->__add_shared();
460*0b57cec5SDimitry Andric     unique_ptr<facet, release> hold(f);
461*0b57cec5SDimitry Andric     if (static_cast<size_t>(id) >= facets_.size())
462*0b57cec5SDimitry Andric         facets_.resize(static_cast<size_t>(id+1));
463*0b57cec5SDimitry Andric     if (facets_[static_cast<size_t>(id)])
464*0b57cec5SDimitry Andric         facets_[static_cast<size_t>(id)]->__release_shared();
465*0b57cec5SDimitry Andric     facets_[static_cast<size_t>(id)] = hold.release();
466*0b57cec5SDimitry Andric }
467*0b57cec5SDimitry Andric 
468*0b57cec5SDimitry Andric const locale::facet*
469*0b57cec5SDimitry Andric locale::__imp::use_facet(long id) const
470*0b57cec5SDimitry Andric {
471*0b57cec5SDimitry Andric     if (!has_facet(id))
472*0b57cec5SDimitry Andric         __throw_bad_cast();
473*0b57cec5SDimitry Andric     return facets_[static_cast<size_t>(id)];
474*0b57cec5SDimitry Andric }
475*0b57cec5SDimitry Andric 
476*0b57cec5SDimitry Andric // locale
477*0b57cec5SDimitry Andric 
478*0b57cec5SDimitry Andric const locale&
479*0b57cec5SDimitry Andric locale::__imp::make_classic()
480*0b57cec5SDimitry Andric {
481*0b57cec5SDimitry Andric     // only one thread can get in here and it only gets in once
482*0b57cec5SDimitry Andric     static aligned_storage<sizeof(locale)>::type buf;
483*0b57cec5SDimitry Andric     locale* c = reinterpret_cast<locale*>(&buf);
484*0b57cec5SDimitry Andric     c->__locale_ = &make<__imp>(1u);
485*0b57cec5SDimitry Andric     return *c;
486*0b57cec5SDimitry Andric }
487*0b57cec5SDimitry Andric 
488*0b57cec5SDimitry Andric const locale&
489*0b57cec5SDimitry Andric locale::classic()
490*0b57cec5SDimitry Andric {
491*0b57cec5SDimitry Andric     static const locale& c = __imp::make_classic();
492*0b57cec5SDimitry Andric     return c;
493*0b57cec5SDimitry Andric }
494*0b57cec5SDimitry Andric 
495*0b57cec5SDimitry Andric locale&
496*0b57cec5SDimitry Andric locale::__imp::make_global()
497*0b57cec5SDimitry Andric {
498*0b57cec5SDimitry Andric     // only one thread can get in here and it only gets in once
499*0b57cec5SDimitry Andric     static aligned_storage<sizeof(locale)>::type buf;
500*0b57cec5SDimitry Andric     auto *obj = ::new (&buf) locale(locale::classic());
501*0b57cec5SDimitry Andric     return *obj;
502*0b57cec5SDimitry Andric }
503*0b57cec5SDimitry Andric 
504*0b57cec5SDimitry Andric locale&
505*0b57cec5SDimitry Andric locale::__global()
506*0b57cec5SDimitry Andric {
507*0b57cec5SDimitry Andric     static locale& g = __imp::make_global();
508*0b57cec5SDimitry Andric     return g;
509*0b57cec5SDimitry Andric }
510*0b57cec5SDimitry Andric 
511*0b57cec5SDimitry Andric locale::locale()  _NOEXCEPT
512*0b57cec5SDimitry Andric     : __locale_(__global().__locale_)
513*0b57cec5SDimitry Andric {
514*0b57cec5SDimitry Andric     __locale_->__add_shared();
515*0b57cec5SDimitry Andric }
516*0b57cec5SDimitry Andric 
517*0b57cec5SDimitry Andric locale::locale(const locale& l)  _NOEXCEPT
518*0b57cec5SDimitry Andric     : __locale_(l.__locale_)
519*0b57cec5SDimitry Andric {
520*0b57cec5SDimitry Andric     __locale_->__add_shared();
521*0b57cec5SDimitry Andric }
522*0b57cec5SDimitry Andric 
523*0b57cec5SDimitry Andric locale::~locale()
524*0b57cec5SDimitry Andric {
525*0b57cec5SDimitry Andric     __locale_->__release_shared();
526*0b57cec5SDimitry Andric }
527*0b57cec5SDimitry Andric 
528*0b57cec5SDimitry Andric const locale&
529*0b57cec5SDimitry Andric locale::operator=(const locale& other)  _NOEXCEPT
530*0b57cec5SDimitry Andric {
531*0b57cec5SDimitry Andric     other.__locale_->__add_shared();
532*0b57cec5SDimitry Andric     __locale_->__release_shared();
533*0b57cec5SDimitry Andric     __locale_ = other.__locale_;
534*0b57cec5SDimitry Andric     return *this;
535*0b57cec5SDimitry Andric }
536*0b57cec5SDimitry Andric 
537*0b57cec5SDimitry Andric locale::locale(const char* name)
538*0b57cec5SDimitry Andric     : __locale_(name ? new __imp(name)
539*0b57cec5SDimitry Andric                      : (__throw_runtime_error("locale constructed with null"), (__imp*)0))
540*0b57cec5SDimitry Andric {
541*0b57cec5SDimitry Andric     __locale_->__add_shared();
542*0b57cec5SDimitry Andric }
543*0b57cec5SDimitry Andric 
544*0b57cec5SDimitry Andric locale::locale(const string& name)
545*0b57cec5SDimitry Andric     : __locale_(new __imp(name))
546*0b57cec5SDimitry Andric {
547*0b57cec5SDimitry Andric     __locale_->__add_shared();
548*0b57cec5SDimitry Andric }
549*0b57cec5SDimitry Andric 
550*0b57cec5SDimitry Andric locale::locale(const locale& other, const char* name, category c)
551*0b57cec5SDimitry Andric     : __locale_(name ? new __imp(*other.__locale_, name, c)
552*0b57cec5SDimitry Andric                      : (__throw_runtime_error("locale constructed with null"), (__imp*)0))
553*0b57cec5SDimitry Andric {
554*0b57cec5SDimitry Andric     __locale_->__add_shared();
555*0b57cec5SDimitry Andric }
556*0b57cec5SDimitry Andric 
557*0b57cec5SDimitry Andric locale::locale(const locale& other, const string& name, category c)
558*0b57cec5SDimitry Andric     : __locale_(new __imp(*other.__locale_, name, c))
559*0b57cec5SDimitry Andric {
560*0b57cec5SDimitry Andric     __locale_->__add_shared();
561*0b57cec5SDimitry Andric }
562*0b57cec5SDimitry Andric 
563*0b57cec5SDimitry Andric locale::locale(const locale& other, const locale& one, category c)
564*0b57cec5SDimitry Andric     : __locale_(new __imp(*other.__locale_, *one.__locale_, c))
565*0b57cec5SDimitry Andric {
566*0b57cec5SDimitry Andric     __locale_->__add_shared();
567*0b57cec5SDimitry Andric }
568*0b57cec5SDimitry Andric 
569*0b57cec5SDimitry Andric string
570*0b57cec5SDimitry Andric locale::name() const
571*0b57cec5SDimitry Andric {
572*0b57cec5SDimitry Andric     return __locale_->name();
573*0b57cec5SDimitry Andric }
574*0b57cec5SDimitry Andric 
575*0b57cec5SDimitry Andric void
576*0b57cec5SDimitry Andric locale::__install_ctor(const locale& other, facet* f, long id)
577*0b57cec5SDimitry Andric {
578*0b57cec5SDimitry Andric     if (f)
579*0b57cec5SDimitry Andric         __locale_ = new __imp(*other.__locale_, f, id);
580*0b57cec5SDimitry Andric     else
581*0b57cec5SDimitry Andric         __locale_ = other.__locale_;
582*0b57cec5SDimitry Andric     __locale_->__add_shared();
583*0b57cec5SDimitry Andric }
584*0b57cec5SDimitry Andric 
585*0b57cec5SDimitry Andric locale
586*0b57cec5SDimitry Andric locale::global(const locale& loc)
587*0b57cec5SDimitry Andric {
588*0b57cec5SDimitry Andric     locale& g = __global();
589*0b57cec5SDimitry Andric     locale r = g;
590*0b57cec5SDimitry Andric     g = loc;
591*0b57cec5SDimitry Andric     if (g.name() != "*")
592*0b57cec5SDimitry Andric         setlocale(LC_ALL, g.name().c_str());
593*0b57cec5SDimitry Andric     return r;
594*0b57cec5SDimitry Andric }
595*0b57cec5SDimitry Andric 
596*0b57cec5SDimitry Andric bool
597*0b57cec5SDimitry Andric locale::has_facet(id& x) const
598*0b57cec5SDimitry Andric {
599*0b57cec5SDimitry Andric     return __locale_->has_facet(x.__get());
600*0b57cec5SDimitry Andric }
601*0b57cec5SDimitry Andric 
602*0b57cec5SDimitry Andric const locale::facet*
603*0b57cec5SDimitry Andric locale::use_facet(id& x) const
604*0b57cec5SDimitry Andric {
605*0b57cec5SDimitry Andric     return __locale_->use_facet(x.__get());
606*0b57cec5SDimitry Andric }
607*0b57cec5SDimitry Andric 
608*0b57cec5SDimitry Andric bool
609*0b57cec5SDimitry Andric locale::operator==(const locale& y) const
610*0b57cec5SDimitry Andric {
611*0b57cec5SDimitry Andric     return (__locale_ == y.__locale_)
612*0b57cec5SDimitry Andric         || (__locale_->name() != "*" && __locale_->name() == y.__locale_->name());
613*0b57cec5SDimitry Andric }
614*0b57cec5SDimitry Andric 
615*0b57cec5SDimitry Andric // locale::facet
616*0b57cec5SDimitry Andric 
617*0b57cec5SDimitry Andric locale::facet::~facet()
618*0b57cec5SDimitry Andric {
619*0b57cec5SDimitry Andric }
620*0b57cec5SDimitry Andric 
621*0b57cec5SDimitry Andric void
622*0b57cec5SDimitry Andric locale::facet::__on_zero_shared() _NOEXCEPT
623*0b57cec5SDimitry Andric {
624*0b57cec5SDimitry Andric     delete this;
625*0b57cec5SDimitry Andric }
626*0b57cec5SDimitry Andric 
627*0b57cec5SDimitry Andric // locale::id
628*0b57cec5SDimitry Andric 
629*0b57cec5SDimitry Andric int32_t locale::id::__next_id = 0;
630*0b57cec5SDimitry Andric 
631*0b57cec5SDimitry Andric namespace
632*0b57cec5SDimitry Andric {
633*0b57cec5SDimitry Andric 
634*0b57cec5SDimitry Andric class __fake_bind
635*0b57cec5SDimitry Andric {
636*0b57cec5SDimitry Andric     locale::id* id_;
637*0b57cec5SDimitry Andric     void (locale::id::* pmf_)();
638*0b57cec5SDimitry Andric public:
639*0b57cec5SDimitry Andric     __fake_bind(void (locale::id::* pmf)(), locale::id* id)
640*0b57cec5SDimitry Andric         : id_(id), pmf_(pmf) {}
641*0b57cec5SDimitry Andric 
642*0b57cec5SDimitry Andric     void operator()() const
643*0b57cec5SDimitry Andric     {
644*0b57cec5SDimitry Andric         (id_->*pmf_)();
645*0b57cec5SDimitry Andric     }
646*0b57cec5SDimitry Andric };
647*0b57cec5SDimitry Andric 
648*0b57cec5SDimitry Andric }
649*0b57cec5SDimitry Andric 
650*0b57cec5SDimitry Andric long
651*0b57cec5SDimitry Andric locale::id::__get()
652*0b57cec5SDimitry Andric {
653*0b57cec5SDimitry Andric     call_once(__flag_, __fake_bind(&locale::id::__init, this));
654*0b57cec5SDimitry Andric     return __id_ - 1;
655*0b57cec5SDimitry Andric }
656*0b57cec5SDimitry Andric 
657*0b57cec5SDimitry Andric void
658*0b57cec5SDimitry Andric locale::id::__init()
659*0b57cec5SDimitry Andric {
660*0b57cec5SDimitry Andric     __id_ = __libcpp_atomic_add(&__next_id, 1);
661*0b57cec5SDimitry Andric }
662*0b57cec5SDimitry Andric 
663*0b57cec5SDimitry Andric // template <> class collate_byname<char>
664*0b57cec5SDimitry Andric 
665*0b57cec5SDimitry Andric collate_byname<char>::collate_byname(const char* n, size_t refs)
666*0b57cec5SDimitry Andric     : collate<char>(refs),
667*0b57cec5SDimitry Andric       __l(newlocale(LC_ALL_MASK, n, 0))
668*0b57cec5SDimitry Andric {
669*0b57cec5SDimitry Andric     if (__l == 0)
670*0b57cec5SDimitry Andric         __throw_runtime_error("collate_byname<char>::collate_byname"
671*0b57cec5SDimitry Andric                             " failed to construct for " + string(n));
672*0b57cec5SDimitry Andric }
673*0b57cec5SDimitry Andric 
674*0b57cec5SDimitry Andric collate_byname<char>::collate_byname(const string& name, size_t refs)
675*0b57cec5SDimitry Andric     : collate<char>(refs),
676*0b57cec5SDimitry Andric       __l(newlocale(LC_ALL_MASK, name.c_str(), 0))
677*0b57cec5SDimitry Andric {
678*0b57cec5SDimitry Andric     if (__l == 0)
679*0b57cec5SDimitry Andric         __throw_runtime_error("collate_byname<char>::collate_byname"
680*0b57cec5SDimitry Andric                             " failed to construct for " + name);
681*0b57cec5SDimitry Andric }
682*0b57cec5SDimitry Andric 
683*0b57cec5SDimitry Andric collate_byname<char>::~collate_byname()
684*0b57cec5SDimitry Andric {
685*0b57cec5SDimitry Andric     freelocale(__l);
686*0b57cec5SDimitry Andric }
687*0b57cec5SDimitry Andric 
688*0b57cec5SDimitry Andric int
689*0b57cec5SDimitry Andric collate_byname<char>::do_compare(const char_type* __lo1, const char_type* __hi1,
690*0b57cec5SDimitry Andric                                  const char_type* __lo2, const char_type* __hi2) const
691*0b57cec5SDimitry Andric {
692*0b57cec5SDimitry Andric     string_type lhs(__lo1, __hi1);
693*0b57cec5SDimitry Andric     string_type rhs(__lo2, __hi2);
694*0b57cec5SDimitry Andric     int r = strcoll_l(lhs.c_str(), rhs.c_str(), __l);
695*0b57cec5SDimitry Andric     if (r < 0)
696*0b57cec5SDimitry Andric         return -1;
697*0b57cec5SDimitry Andric     if (r > 0)
698*0b57cec5SDimitry Andric         return 1;
699*0b57cec5SDimitry Andric     return r;
700*0b57cec5SDimitry Andric }
701*0b57cec5SDimitry Andric 
702*0b57cec5SDimitry Andric collate_byname<char>::string_type
703*0b57cec5SDimitry Andric collate_byname<char>::do_transform(const char_type* lo, const char_type* hi) const
704*0b57cec5SDimitry Andric {
705*0b57cec5SDimitry Andric     const string_type in(lo, hi);
706*0b57cec5SDimitry Andric     string_type out(strxfrm_l(0, in.c_str(), 0, __l), char());
707*0b57cec5SDimitry Andric     strxfrm_l(const_cast<char*>(out.c_str()), in.c_str(), out.size()+1, __l);
708*0b57cec5SDimitry Andric     return out;
709*0b57cec5SDimitry Andric }
710*0b57cec5SDimitry Andric 
711*0b57cec5SDimitry Andric // template <> class collate_byname<wchar_t>
712*0b57cec5SDimitry Andric 
713*0b57cec5SDimitry Andric collate_byname<wchar_t>::collate_byname(const char* n, size_t refs)
714*0b57cec5SDimitry Andric     : collate<wchar_t>(refs),
715*0b57cec5SDimitry Andric       __l(newlocale(LC_ALL_MASK, n, 0))
716*0b57cec5SDimitry Andric {
717*0b57cec5SDimitry Andric     if (__l == 0)
718*0b57cec5SDimitry Andric         __throw_runtime_error("collate_byname<wchar_t>::collate_byname(size_t refs)"
719*0b57cec5SDimitry Andric                             " failed to construct for " + string(n));
720*0b57cec5SDimitry Andric }
721*0b57cec5SDimitry Andric 
722*0b57cec5SDimitry Andric collate_byname<wchar_t>::collate_byname(const string& name, size_t refs)
723*0b57cec5SDimitry Andric     : collate<wchar_t>(refs),
724*0b57cec5SDimitry Andric       __l(newlocale(LC_ALL_MASK, name.c_str(), 0))
725*0b57cec5SDimitry Andric {
726*0b57cec5SDimitry Andric     if (__l == 0)
727*0b57cec5SDimitry Andric         __throw_runtime_error("collate_byname<wchar_t>::collate_byname(size_t refs)"
728*0b57cec5SDimitry Andric                             " failed to construct for " + name);
729*0b57cec5SDimitry Andric }
730*0b57cec5SDimitry Andric 
731*0b57cec5SDimitry Andric collate_byname<wchar_t>::~collate_byname()
732*0b57cec5SDimitry Andric {
733*0b57cec5SDimitry Andric     freelocale(__l);
734*0b57cec5SDimitry Andric }
735*0b57cec5SDimitry Andric 
736*0b57cec5SDimitry Andric int
737*0b57cec5SDimitry Andric collate_byname<wchar_t>::do_compare(const char_type* __lo1, const char_type* __hi1,
738*0b57cec5SDimitry Andric                                  const char_type* __lo2, const char_type* __hi2) const
739*0b57cec5SDimitry Andric {
740*0b57cec5SDimitry Andric     string_type lhs(__lo1, __hi1);
741*0b57cec5SDimitry Andric     string_type rhs(__lo2, __hi2);
742*0b57cec5SDimitry Andric     int r = wcscoll_l(lhs.c_str(), rhs.c_str(), __l);
743*0b57cec5SDimitry Andric     if (r < 0)
744*0b57cec5SDimitry Andric         return -1;
745*0b57cec5SDimitry Andric     if (r > 0)
746*0b57cec5SDimitry Andric         return 1;
747*0b57cec5SDimitry Andric     return r;
748*0b57cec5SDimitry Andric }
749*0b57cec5SDimitry Andric 
750*0b57cec5SDimitry Andric collate_byname<wchar_t>::string_type
751*0b57cec5SDimitry Andric collate_byname<wchar_t>::do_transform(const char_type* lo, const char_type* hi) const
752*0b57cec5SDimitry Andric {
753*0b57cec5SDimitry Andric     const string_type in(lo, hi);
754*0b57cec5SDimitry Andric     string_type out(wcsxfrm_l(0, in.c_str(), 0, __l), wchar_t());
755*0b57cec5SDimitry Andric     wcsxfrm_l(const_cast<wchar_t*>(out.c_str()), in.c_str(), out.size()+1, __l);
756*0b57cec5SDimitry Andric     return out;
757*0b57cec5SDimitry Andric }
758*0b57cec5SDimitry Andric 
759*0b57cec5SDimitry Andric // template <> class ctype<wchar_t>;
760*0b57cec5SDimitry Andric 
761*0b57cec5SDimitry Andric const ctype_base::mask ctype_base::space;
762*0b57cec5SDimitry Andric const ctype_base::mask ctype_base::print;
763*0b57cec5SDimitry Andric const ctype_base::mask ctype_base::cntrl;
764*0b57cec5SDimitry Andric const ctype_base::mask ctype_base::upper;
765*0b57cec5SDimitry Andric const ctype_base::mask ctype_base::lower;
766*0b57cec5SDimitry Andric const ctype_base::mask ctype_base::alpha;
767*0b57cec5SDimitry Andric const ctype_base::mask ctype_base::digit;
768*0b57cec5SDimitry Andric const ctype_base::mask ctype_base::punct;
769*0b57cec5SDimitry Andric const ctype_base::mask ctype_base::xdigit;
770*0b57cec5SDimitry Andric const ctype_base::mask ctype_base::blank;
771*0b57cec5SDimitry Andric const ctype_base::mask ctype_base::alnum;
772*0b57cec5SDimitry Andric const ctype_base::mask ctype_base::graph;
773*0b57cec5SDimitry Andric 
774*0b57cec5SDimitry Andric locale::id ctype<wchar_t>::id;
775*0b57cec5SDimitry Andric 
776*0b57cec5SDimitry Andric ctype<wchar_t>::~ctype()
777*0b57cec5SDimitry Andric {
778*0b57cec5SDimitry Andric }
779*0b57cec5SDimitry Andric 
780*0b57cec5SDimitry Andric bool
781*0b57cec5SDimitry Andric ctype<wchar_t>::do_is(mask m, char_type c) const
782*0b57cec5SDimitry Andric {
783*0b57cec5SDimitry Andric     return isascii(c) ? (ctype<char>::classic_table()[c] & m) != 0 : false;
784*0b57cec5SDimitry Andric }
785*0b57cec5SDimitry Andric 
786*0b57cec5SDimitry Andric const wchar_t*
787*0b57cec5SDimitry Andric ctype<wchar_t>::do_is(const char_type* low, const char_type* high, mask* vec) const
788*0b57cec5SDimitry Andric {
789*0b57cec5SDimitry Andric     for (; low != high; ++low, ++vec)
790*0b57cec5SDimitry Andric         *vec = static_cast<mask>(isascii(*low) ?
791*0b57cec5SDimitry Andric                                    ctype<char>::classic_table()[*low] : 0);
792*0b57cec5SDimitry Andric     return low;
793*0b57cec5SDimitry Andric }
794*0b57cec5SDimitry Andric 
795*0b57cec5SDimitry Andric const wchar_t*
796*0b57cec5SDimitry Andric ctype<wchar_t>::do_scan_is(mask m, const char_type* low, const char_type* high) const
797*0b57cec5SDimitry Andric {
798*0b57cec5SDimitry Andric     for (; low != high; ++low)
799*0b57cec5SDimitry Andric         if (isascii(*low) && (ctype<char>::classic_table()[*low] & m))
800*0b57cec5SDimitry Andric             break;
801*0b57cec5SDimitry Andric     return low;
802*0b57cec5SDimitry Andric }
803*0b57cec5SDimitry Andric 
804*0b57cec5SDimitry Andric const wchar_t*
805*0b57cec5SDimitry Andric ctype<wchar_t>::do_scan_not(mask m, const char_type* low, const char_type* high) const
806*0b57cec5SDimitry Andric {
807*0b57cec5SDimitry Andric     for (; low != high; ++low)
808*0b57cec5SDimitry Andric         if (!(isascii(*low) && (ctype<char>::classic_table()[*low] & m)))
809*0b57cec5SDimitry Andric             break;
810*0b57cec5SDimitry Andric     return low;
811*0b57cec5SDimitry Andric }
812*0b57cec5SDimitry Andric 
813*0b57cec5SDimitry Andric wchar_t
814*0b57cec5SDimitry Andric ctype<wchar_t>::do_toupper(char_type c) const
815*0b57cec5SDimitry Andric {
816*0b57cec5SDimitry Andric #ifdef _LIBCPP_HAS_DEFAULTRUNELOCALE
817*0b57cec5SDimitry Andric     return isascii(c) ? _DefaultRuneLocale.__mapupper[c] : c;
818*0b57cec5SDimitry Andric #elif defined(__GLIBC__) || defined(__EMSCRIPTEN__) || \
819*0b57cec5SDimitry Andric       defined(__NetBSD__)
820*0b57cec5SDimitry Andric     return isascii(c) ? ctype<char>::__classic_upper_table()[c] : c;
821*0b57cec5SDimitry Andric #else
822*0b57cec5SDimitry Andric     return (isascii(c) && iswlower_l(c, _LIBCPP_GET_C_LOCALE)) ? c-L'a'+L'A' : c;
823*0b57cec5SDimitry Andric #endif
824*0b57cec5SDimitry Andric }
825*0b57cec5SDimitry Andric 
826*0b57cec5SDimitry Andric const wchar_t*
827*0b57cec5SDimitry Andric ctype<wchar_t>::do_toupper(char_type* low, const char_type* high) const
828*0b57cec5SDimitry Andric {
829*0b57cec5SDimitry Andric     for (; low != high; ++low)
830*0b57cec5SDimitry Andric #ifdef _LIBCPP_HAS_DEFAULTRUNELOCALE
831*0b57cec5SDimitry Andric         *low = isascii(*low) ? _DefaultRuneLocale.__mapupper[*low] : *low;
832*0b57cec5SDimitry Andric #elif defined(__GLIBC__) || defined(__EMSCRIPTEN__) || \
833*0b57cec5SDimitry Andric       defined(__NetBSD__)
834*0b57cec5SDimitry Andric         *low = isascii(*low) ? ctype<char>::__classic_upper_table()[*low]
835*0b57cec5SDimitry Andric                              : *low;
836*0b57cec5SDimitry Andric #else
837*0b57cec5SDimitry Andric         *low = (isascii(*low) && islower_l(*low, _LIBCPP_GET_C_LOCALE)) ? (*low-L'a'+L'A') : *low;
838*0b57cec5SDimitry Andric #endif
839*0b57cec5SDimitry Andric     return low;
840*0b57cec5SDimitry Andric }
841*0b57cec5SDimitry Andric 
842*0b57cec5SDimitry Andric wchar_t
843*0b57cec5SDimitry Andric ctype<wchar_t>::do_tolower(char_type c) const
844*0b57cec5SDimitry Andric {
845*0b57cec5SDimitry Andric #ifdef _LIBCPP_HAS_DEFAULTRUNELOCALE
846*0b57cec5SDimitry Andric     return isascii(c) ? _DefaultRuneLocale.__maplower[c] : c;
847*0b57cec5SDimitry Andric #elif defined(__GLIBC__) || defined(__EMSCRIPTEN__) || \
848*0b57cec5SDimitry Andric       defined(__NetBSD__)
849*0b57cec5SDimitry Andric     return isascii(c) ? ctype<char>::__classic_lower_table()[c] : c;
850*0b57cec5SDimitry Andric #else
851*0b57cec5SDimitry Andric     return (isascii(c) && isupper_l(c, _LIBCPP_GET_C_LOCALE)) ? c-L'A'+'a' : c;
852*0b57cec5SDimitry Andric #endif
853*0b57cec5SDimitry Andric }
854*0b57cec5SDimitry Andric 
855*0b57cec5SDimitry Andric const wchar_t*
856*0b57cec5SDimitry Andric ctype<wchar_t>::do_tolower(char_type* low, const char_type* high) const
857*0b57cec5SDimitry Andric {
858*0b57cec5SDimitry Andric     for (; low != high; ++low)
859*0b57cec5SDimitry Andric #ifdef _LIBCPP_HAS_DEFAULTRUNELOCALE
860*0b57cec5SDimitry Andric         *low = isascii(*low) ? _DefaultRuneLocale.__maplower[*low] : *low;
861*0b57cec5SDimitry Andric #elif defined(__GLIBC__) || defined(__EMSCRIPTEN__) || \
862*0b57cec5SDimitry Andric       defined(__NetBSD__)
863*0b57cec5SDimitry Andric         *low = isascii(*low) ? ctype<char>::__classic_lower_table()[*low]
864*0b57cec5SDimitry Andric                              : *low;
865*0b57cec5SDimitry Andric #else
866*0b57cec5SDimitry Andric         *low = (isascii(*low) && isupper_l(*low, _LIBCPP_GET_C_LOCALE)) ? *low-L'A'+L'a' : *low;
867*0b57cec5SDimitry Andric #endif
868*0b57cec5SDimitry Andric     return low;
869*0b57cec5SDimitry Andric }
870*0b57cec5SDimitry Andric 
871*0b57cec5SDimitry Andric wchar_t
872*0b57cec5SDimitry Andric ctype<wchar_t>::do_widen(char c) const
873*0b57cec5SDimitry Andric {
874*0b57cec5SDimitry Andric     return c;
875*0b57cec5SDimitry Andric }
876*0b57cec5SDimitry Andric 
877*0b57cec5SDimitry Andric const char*
878*0b57cec5SDimitry Andric ctype<wchar_t>::do_widen(const char* low, const char* high, char_type* dest) const
879*0b57cec5SDimitry Andric {
880*0b57cec5SDimitry Andric     for (; low != high; ++low, ++dest)
881*0b57cec5SDimitry Andric         *dest = *low;
882*0b57cec5SDimitry Andric     return low;
883*0b57cec5SDimitry Andric }
884*0b57cec5SDimitry Andric 
885*0b57cec5SDimitry Andric char
886*0b57cec5SDimitry Andric ctype<wchar_t>::do_narrow(char_type c, char dfault) const
887*0b57cec5SDimitry Andric {
888*0b57cec5SDimitry Andric     if (isascii(c))
889*0b57cec5SDimitry Andric         return static_cast<char>(c);
890*0b57cec5SDimitry Andric     return dfault;
891*0b57cec5SDimitry Andric }
892*0b57cec5SDimitry Andric 
893*0b57cec5SDimitry Andric const wchar_t*
894*0b57cec5SDimitry Andric ctype<wchar_t>::do_narrow(const char_type* low, const char_type* high, char dfault, char* dest) const
895*0b57cec5SDimitry Andric {
896*0b57cec5SDimitry Andric     for (; low != high; ++low, ++dest)
897*0b57cec5SDimitry Andric         if (isascii(*low))
898*0b57cec5SDimitry Andric             *dest = static_cast<char>(*low);
899*0b57cec5SDimitry Andric         else
900*0b57cec5SDimitry Andric             *dest = dfault;
901*0b57cec5SDimitry Andric     return low;
902*0b57cec5SDimitry Andric }
903*0b57cec5SDimitry Andric 
904*0b57cec5SDimitry Andric // template <> class ctype<char>;
905*0b57cec5SDimitry Andric 
906*0b57cec5SDimitry Andric locale::id ctype<char>::id;
907*0b57cec5SDimitry Andric 
908*0b57cec5SDimitry Andric ctype<char>::ctype(const mask* tab, bool del, size_t refs)
909*0b57cec5SDimitry Andric     : locale::facet(refs),
910*0b57cec5SDimitry Andric       __tab_(tab),
911*0b57cec5SDimitry Andric       __del_(del)
912*0b57cec5SDimitry Andric {
913*0b57cec5SDimitry Andric   if (__tab_ == 0)
914*0b57cec5SDimitry Andric       __tab_ = classic_table();
915*0b57cec5SDimitry Andric }
916*0b57cec5SDimitry Andric 
917*0b57cec5SDimitry Andric ctype<char>::~ctype()
918*0b57cec5SDimitry Andric {
919*0b57cec5SDimitry Andric     if (__tab_ && __del_)
920*0b57cec5SDimitry Andric         delete [] __tab_;
921*0b57cec5SDimitry Andric }
922*0b57cec5SDimitry Andric 
923*0b57cec5SDimitry Andric char
924*0b57cec5SDimitry Andric ctype<char>::do_toupper(char_type c) const
925*0b57cec5SDimitry Andric {
926*0b57cec5SDimitry Andric #ifdef _LIBCPP_HAS_DEFAULTRUNELOCALE
927*0b57cec5SDimitry Andric     return isascii(c) ?
928*0b57cec5SDimitry Andric       static_cast<char>(_DefaultRuneLocale.__mapupper[static_cast<ptrdiff_t>(c)]) : c;
929*0b57cec5SDimitry Andric #elif defined(__NetBSD__)
930*0b57cec5SDimitry Andric     return static_cast<char>(__classic_upper_table()[static_cast<unsigned char>(c)]);
931*0b57cec5SDimitry Andric #elif defined(__GLIBC__) || defined(__EMSCRIPTEN__)
932*0b57cec5SDimitry Andric     return isascii(c) ?
933*0b57cec5SDimitry Andric       static_cast<char>(__classic_upper_table()[static_cast<unsigned char>(c)]) : c;
934*0b57cec5SDimitry Andric #else
935*0b57cec5SDimitry Andric     return (isascii(c) && islower_l(c, _LIBCPP_GET_C_LOCALE)) ? c-'a'+'A' : c;
936*0b57cec5SDimitry Andric #endif
937*0b57cec5SDimitry Andric }
938*0b57cec5SDimitry Andric 
939*0b57cec5SDimitry Andric const char*
940*0b57cec5SDimitry Andric ctype<char>::do_toupper(char_type* low, const char_type* high) const
941*0b57cec5SDimitry Andric {
942*0b57cec5SDimitry Andric     for (; low != high; ++low)
943*0b57cec5SDimitry Andric #ifdef _LIBCPP_HAS_DEFAULTRUNELOCALE
944*0b57cec5SDimitry Andric         *low = isascii(*low) ?
945*0b57cec5SDimitry Andric           static_cast<char>(_DefaultRuneLocale.__mapupper[static_cast<ptrdiff_t>(*low)]) : *low;
946*0b57cec5SDimitry Andric #elif defined(__NetBSD__)
947*0b57cec5SDimitry Andric         *low = static_cast<char>(__classic_upper_table()[static_cast<unsigned char>(*low)]);
948*0b57cec5SDimitry Andric #elif defined(__GLIBC__) || defined(__EMSCRIPTEN__)
949*0b57cec5SDimitry Andric         *low = isascii(*low) ?
950*0b57cec5SDimitry Andric           static_cast<char>(__classic_upper_table()[static_cast<size_t>(*low)]) : *low;
951*0b57cec5SDimitry Andric #else
952*0b57cec5SDimitry Andric         *low = (isascii(*low) && islower_l(*low, _LIBCPP_GET_C_LOCALE)) ? *low-'a'+'A' : *low;
953*0b57cec5SDimitry Andric #endif
954*0b57cec5SDimitry Andric     return low;
955*0b57cec5SDimitry Andric }
956*0b57cec5SDimitry Andric 
957*0b57cec5SDimitry Andric char
958*0b57cec5SDimitry Andric ctype<char>::do_tolower(char_type c) const
959*0b57cec5SDimitry Andric {
960*0b57cec5SDimitry Andric #ifdef _LIBCPP_HAS_DEFAULTRUNELOCALE
961*0b57cec5SDimitry Andric     return isascii(c) ?
962*0b57cec5SDimitry Andric       static_cast<char>(_DefaultRuneLocale.__maplower[static_cast<ptrdiff_t>(c)]) : c;
963*0b57cec5SDimitry Andric #elif defined(__NetBSD__)
964*0b57cec5SDimitry Andric     return static_cast<char>(__classic_lower_table()[static_cast<unsigned char>(c)]);
965*0b57cec5SDimitry Andric #elif defined(__GLIBC__) || defined(__EMSCRIPTEN__)
966*0b57cec5SDimitry Andric     return isascii(c) ?
967*0b57cec5SDimitry Andric       static_cast<char>(__classic_lower_table()[static_cast<size_t>(c)]) : c;
968*0b57cec5SDimitry Andric #else
969*0b57cec5SDimitry Andric     return (isascii(c) && isupper_l(c, _LIBCPP_GET_C_LOCALE)) ? c-'A'+'a' : c;
970*0b57cec5SDimitry Andric #endif
971*0b57cec5SDimitry Andric }
972*0b57cec5SDimitry Andric 
973*0b57cec5SDimitry Andric const char*
974*0b57cec5SDimitry Andric ctype<char>::do_tolower(char_type* low, const char_type* high) const
975*0b57cec5SDimitry Andric {
976*0b57cec5SDimitry Andric     for (; low != high; ++low)
977*0b57cec5SDimitry Andric #ifdef _LIBCPP_HAS_DEFAULTRUNELOCALE
978*0b57cec5SDimitry Andric         *low = isascii(*low) ? static_cast<char>(_DefaultRuneLocale.__maplower[static_cast<ptrdiff_t>(*low)]) : *low;
979*0b57cec5SDimitry Andric #elif defined(__NetBSD__)
980*0b57cec5SDimitry Andric         *low = static_cast<char>(__classic_lower_table()[static_cast<unsigned char>(*low)]);
981*0b57cec5SDimitry Andric #elif defined(__GLIBC__) || defined(__EMSCRIPTEN__)
982*0b57cec5SDimitry Andric         *low = isascii(*low) ? static_cast<char>(__classic_lower_table()[static_cast<size_t>(*low)]) : *low;
983*0b57cec5SDimitry Andric #else
984*0b57cec5SDimitry Andric         *low = (isascii(*low) && isupper_l(*low, _LIBCPP_GET_C_LOCALE)) ? *low-'A'+'a' : *low;
985*0b57cec5SDimitry Andric #endif
986*0b57cec5SDimitry Andric     return low;
987*0b57cec5SDimitry Andric }
988*0b57cec5SDimitry Andric 
989*0b57cec5SDimitry Andric char
990*0b57cec5SDimitry Andric ctype<char>::do_widen(char c) const
991*0b57cec5SDimitry Andric {
992*0b57cec5SDimitry Andric     return c;
993*0b57cec5SDimitry Andric }
994*0b57cec5SDimitry Andric 
995*0b57cec5SDimitry Andric const char*
996*0b57cec5SDimitry Andric ctype<char>::do_widen(const char* low, const char* high, char_type* dest) const
997*0b57cec5SDimitry Andric {
998*0b57cec5SDimitry Andric     for (; low != high; ++low, ++dest)
999*0b57cec5SDimitry Andric         *dest = *low;
1000*0b57cec5SDimitry Andric     return low;
1001*0b57cec5SDimitry Andric }
1002*0b57cec5SDimitry Andric 
1003*0b57cec5SDimitry Andric char
1004*0b57cec5SDimitry Andric ctype<char>::do_narrow(char_type c, char dfault) const
1005*0b57cec5SDimitry Andric {
1006*0b57cec5SDimitry Andric     if (isascii(c))
1007*0b57cec5SDimitry Andric         return static_cast<char>(c);
1008*0b57cec5SDimitry Andric     return dfault;
1009*0b57cec5SDimitry Andric }
1010*0b57cec5SDimitry Andric 
1011*0b57cec5SDimitry Andric const char*
1012*0b57cec5SDimitry Andric ctype<char>::do_narrow(const char_type* low, const char_type* high, char dfault, char* dest) const
1013*0b57cec5SDimitry Andric {
1014*0b57cec5SDimitry Andric     for (; low != high; ++low, ++dest)
1015*0b57cec5SDimitry Andric         if (isascii(*low))
1016*0b57cec5SDimitry Andric             *dest = *low;
1017*0b57cec5SDimitry Andric         else
1018*0b57cec5SDimitry Andric             *dest = dfault;
1019*0b57cec5SDimitry Andric     return low;
1020*0b57cec5SDimitry Andric }
1021*0b57cec5SDimitry Andric 
1022*0b57cec5SDimitry Andric #if defined(__EMSCRIPTEN__)
1023*0b57cec5SDimitry Andric extern "C" const unsigned short ** __ctype_b_loc();
1024*0b57cec5SDimitry Andric extern "C" const int ** __ctype_tolower_loc();
1025*0b57cec5SDimitry Andric extern "C" const int ** __ctype_toupper_loc();
1026*0b57cec5SDimitry Andric #endif
1027*0b57cec5SDimitry Andric 
1028*0b57cec5SDimitry Andric #ifdef _LIBCPP_PROVIDES_DEFAULT_RUNE_TABLE
1029*0b57cec5SDimitry Andric const ctype<char>::mask*
1030*0b57cec5SDimitry Andric ctype<char>::classic_table()  _NOEXCEPT
1031*0b57cec5SDimitry Andric {
1032*0b57cec5SDimitry Andric     static _LIBCPP_CONSTEXPR const ctype<char>::mask builtin_table[table_size] = {
1033*0b57cec5SDimitry Andric         cntrl,                          cntrl,
1034*0b57cec5SDimitry Andric         cntrl,                          cntrl,
1035*0b57cec5SDimitry Andric         cntrl,                          cntrl,
1036*0b57cec5SDimitry Andric         cntrl,                          cntrl,
1037*0b57cec5SDimitry Andric         cntrl,                          cntrl | space | blank,
1038*0b57cec5SDimitry Andric         cntrl | space,                  cntrl | space,
1039*0b57cec5SDimitry Andric         cntrl | space,                  cntrl | space,
1040*0b57cec5SDimitry Andric         cntrl,                          cntrl,
1041*0b57cec5SDimitry Andric         cntrl,                          cntrl,
1042*0b57cec5SDimitry Andric         cntrl,                          cntrl,
1043*0b57cec5SDimitry Andric         cntrl,                          cntrl,
1044*0b57cec5SDimitry Andric         cntrl,                          cntrl,
1045*0b57cec5SDimitry Andric         cntrl,                          cntrl,
1046*0b57cec5SDimitry Andric         cntrl,                          cntrl,
1047*0b57cec5SDimitry Andric         cntrl,                          cntrl,
1048*0b57cec5SDimitry Andric         cntrl,                          cntrl,
1049*0b57cec5SDimitry Andric         space | blank | print,          punct | print,
1050*0b57cec5SDimitry Andric         punct | print,                  punct | print,
1051*0b57cec5SDimitry Andric         punct | print,                  punct | print,
1052*0b57cec5SDimitry Andric         punct | print,                  punct | print,
1053*0b57cec5SDimitry Andric         punct | print,                  punct | print,
1054*0b57cec5SDimitry Andric         punct | print,                  punct | print,
1055*0b57cec5SDimitry Andric         punct | print,                  punct | print,
1056*0b57cec5SDimitry Andric         punct | print,                  punct | print,
1057*0b57cec5SDimitry Andric         digit | print | xdigit,         digit | print | xdigit,
1058*0b57cec5SDimitry Andric         digit | print | xdigit,         digit | print | xdigit,
1059*0b57cec5SDimitry Andric         digit | print | xdigit,         digit | print | xdigit,
1060*0b57cec5SDimitry Andric         digit | print | xdigit,         digit | print | xdigit,
1061*0b57cec5SDimitry Andric         digit | print | xdigit,         digit | print | xdigit,
1062*0b57cec5SDimitry Andric         punct | print,                  punct | print,
1063*0b57cec5SDimitry Andric         punct | print,                  punct | print,
1064*0b57cec5SDimitry Andric         punct | print,                  punct | print,
1065*0b57cec5SDimitry Andric         punct | print,                  upper | xdigit | print | alpha,
1066*0b57cec5SDimitry Andric         upper | xdigit | print | alpha, upper | xdigit | print | alpha,
1067*0b57cec5SDimitry Andric         upper | xdigit | print | alpha, upper | xdigit | print | alpha,
1068*0b57cec5SDimitry Andric         upper | xdigit | print | alpha, upper | print | alpha,
1069*0b57cec5SDimitry Andric         upper | print | alpha,          upper | print | alpha,
1070*0b57cec5SDimitry Andric         upper | print | alpha,          upper | print | alpha,
1071*0b57cec5SDimitry Andric         upper | print | alpha,          upper | print | alpha,
1072*0b57cec5SDimitry Andric         upper | print | alpha,          upper | print | alpha,
1073*0b57cec5SDimitry Andric         upper | print | alpha,          upper | print | alpha,
1074*0b57cec5SDimitry Andric         upper | print | alpha,          upper | print | alpha,
1075*0b57cec5SDimitry Andric         upper | print | alpha,          upper | print | alpha,
1076*0b57cec5SDimitry Andric         upper | print | alpha,          upper | print | alpha,
1077*0b57cec5SDimitry Andric         upper | print | alpha,          upper | print | alpha,
1078*0b57cec5SDimitry Andric         upper | print | alpha,          punct | print,
1079*0b57cec5SDimitry Andric         punct | print,                  punct | print,
1080*0b57cec5SDimitry Andric         punct | print,                  punct | print,
1081*0b57cec5SDimitry Andric         punct | print,                  lower | xdigit | print | alpha,
1082*0b57cec5SDimitry Andric         lower | xdigit | print | alpha, lower | xdigit | print | alpha,
1083*0b57cec5SDimitry Andric         lower | xdigit | print | alpha, lower | xdigit | print | alpha,
1084*0b57cec5SDimitry Andric         lower | xdigit | print | alpha, lower | print | alpha,
1085*0b57cec5SDimitry Andric         lower | print | alpha,          lower | print | alpha,
1086*0b57cec5SDimitry Andric         lower | print | alpha,          lower | print | alpha,
1087*0b57cec5SDimitry Andric         lower | print | alpha,          lower | print | alpha,
1088*0b57cec5SDimitry Andric         lower | print | alpha,          lower | print | alpha,
1089*0b57cec5SDimitry Andric         lower | print | alpha,          lower | print | alpha,
1090*0b57cec5SDimitry Andric         lower | print | alpha,          lower | print | alpha,
1091*0b57cec5SDimitry Andric         lower | print | alpha,          lower | print | alpha,
1092*0b57cec5SDimitry Andric         lower | print | alpha,          lower | print | alpha,
1093*0b57cec5SDimitry Andric         lower | print | alpha,          lower | print | alpha,
1094*0b57cec5SDimitry Andric         lower | print | alpha,          punct | print,
1095*0b57cec5SDimitry Andric         punct | print,                  punct | print,
1096*0b57cec5SDimitry Andric         punct | print,                  cntrl,
1097*0b57cec5SDimitry Andric         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1098*0b57cec5SDimitry Andric         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1099*0b57cec5SDimitry Andric         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1100*0b57cec5SDimitry Andric         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1101*0b57cec5SDimitry Andric         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1102*0b57cec5SDimitry Andric         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1103*0b57cec5SDimitry Andric         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1104*0b57cec5SDimitry Andric         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
1105*0b57cec5SDimitry Andric     };
1106*0b57cec5SDimitry Andric     return builtin_table;
1107*0b57cec5SDimitry Andric }
1108*0b57cec5SDimitry Andric #else
1109*0b57cec5SDimitry Andric const ctype<char>::mask*
1110*0b57cec5SDimitry Andric ctype<char>::classic_table()  _NOEXCEPT
1111*0b57cec5SDimitry Andric {
1112*0b57cec5SDimitry Andric #if defined(__APPLE__) || defined(__FreeBSD__)
1113*0b57cec5SDimitry Andric     return _DefaultRuneLocale.__runetype;
1114*0b57cec5SDimitry Andric #elif defined(__NetBSD__)
1115*0b57cec5SDimitry Andric     return _C_ctype_tab_ + 1;
1116*0b57cec5SDimitry Andric #elif defined(__GLIBC__)
1117*0b57cec5SDimitry Andric     return _LIBCPP_GET_C_LOCALE->__ctype_b;
1118*0b57cec5SDimitry Andric #elif __sun__
1119*0b57cec5SDimitry Andric     return __ctype_mask;
1120*0b57cec5SDimitry Andric #elif defined(_LIBCPP_MSVCRT) || defined(__MINGW32__)
1121*0b57cec5SDimitry Andric     return __pctype_func();
1122*0b57cec5SDimitry Andric #elif defined(__EMSCRIPTEN__)
1123*0b57cec5SDimitry Andric     return *__ctype_b_loc();
1124*0b57cec5SDimitry Andric #elif defined(_NEWLIB_VERSION)
1125*0b57cec5SDimitry Andric     // Newlib has a 257-entry table in ctype_.c, where (char)0 starts at [1].
1126*0b57cec5SDimitry Andric     return _ctype_ + 1;
1127*0b57cec5SDimitry Andric #elif defined(_AIX)
1128*0b57cec5SDimitry Andric     return (const unsigned int *)__lc_ctype_ptr->obj->mask;
1129*0b57cec5SDimitry Andric #else
1130*0b57cec5SDimitry Andric     // Platform not supported: abort so the person doing the port knows what to
1131*0b57cec5SDimitry Andric     // fix
1132*0b57cec5SDimitry Andric # warning  ctype<char>::classic_table() is not implemented
1133*0b57cec5SDimitry Andric     printf("ctype<char>::classic_table() is not implemented\n");
1134*0b57cec5SDimitry Andric     abort();
1135*0b57cec5SDimitry Andric     return NULL;
1136*0b57cec5SDimitry Andric #endif
1137*0b57cec5SDimitry Andric }
1138*0b57cec5SDimitry Andric #endif
1139*0b57cec5SDimitry Andric 
1140*0b57cec5SDimitry Andric #if defined(__GLIBC__)
1141*0b57cec5SDimitry Andric const int*
1142*0b57cec5SDimitry Andric ctype<char>::__classic_lower_table() _NOEXCEPT
1143*0b57cec5SDimitry Andric {
1144*0b57cec5SDimitry Andric     return _LIBCPP_GET_C_LOCALE->__ctype_tolower;
1145*0b57cec5SDimitry Andric }
1146*0b57cec5SDimitry Andric 
1147*0b57cec5SDimitry Andric const int*
1148*0b57cec5SDimitry Andric ctype<char>::__classic_upper_table() _NOEXCEPT
1149*0b57cec5SDimitry Andric {
1150*0b57cec5SDimitry Andric     return _LIBCPP_GET_C_LOCALE->__ctype_toupper;
1151*0b57cec5SDimitry Andric }
1152*0b57cec5SDimitry Andric #elif __NetBSD__
1153*0b57cec5SDimitry Andric const short*
1154*0b57cec5SDimitry Andric ctype<char>::__classic_lower_table() _NOEXCEPT
1155*0b57cec5SDimitry Andric {
1156*0b57cec5SDimitry Andric     return _C_tolower_tab_ + 1;
1157*0b57cec5SDimitry Andric }
1158*0b57cec5SDimitry Andric 
1159*0b57cec5SDimitry Andric const short*
1160*0b57cec5SDimitry Andric ctype<char>::__classic_upper_table() _NOEXCEPT
1161*0b57cec5SDimitry Andric {
1162*0b57cec5SDimitry Andric     return _C_toupper_tab_ + 1;
1163*0b57cec5SDimitry Andric }
1164*0b57cec5SDimitry Andric 
1165*0b57cec5SDimitry Andric #elif defined(__EMSCRIPTEN__)
1166*0b57cec5SDimitry Andric const int*
1167*0b57cec5SDimitry Andric ctype<char>::__classic_lower_table() _NOEXCEPT
1168*0b57cec5SDimitry Andric {
1169*0b57cec5SDimitry Andric     return *__ctype_tolower_loc();
1170*0b57cec5SDimitry Andric }
1171*0b57cec5SDimitry Andric 
1172*0b57cec5SDimitry Andric const int*
1173*0b57cec5SDimitry Andric ctype<char>::__classic_upper_table() _NOEXCEPT
1174*0b57cec5SDimitry Andric {
1175*0b57cec5SDimitry Andric     return *__ctype_toupper_loc();
1176*0b57cec5SDimitry Andric }
1177*0b57cec5SDimitry Andric #endif // __GLIBC__ || __NETBSD__ || __EMSCRIPTEN__
1178*0b57cec5SDimitry Andric 
1179*0b57cec5SDimitry Andric // template <> class ctype_byname<char>
1180*0b57cec5SDimitry Andric 
1181*0b57cec5SDimitry Andric ctype_byname<char>::ctype_byname(const char* name, size_t refs)
1182*0b57cec5SDimitry Andric     : ctype<char>(0, false, refs),
1183*0b57cec5SDimitry Andric       __l(newlocale(LC_ALL_MASK, name, 0))
1184*0b57cec5SDimitry Andric {
1185*0b57cec5SDimitry Andric     if (__l == 0)
1186*0b57cec5SDimitry Andric         __throw_runtime_error("ctype_byname<char>::ctype_byname"
1187*0b57cec5SDimitry Andric                             " failed to construct for " + string(name));
1188*0b57cec5SDimitry Andric }
1189*0b57cec5SDimitry Andric 
1190*0b57cec5SDimitry Andric ctype_byname<char>::ctype_byname(const string& name, size_t refs)
1191*0b57cec5SDimitry Andric     : ctype<char>(0, false, refs),
1192*0b57cec5SDimitry Andric       __l(newlocale(LC_ALL_MASK, name.c_str(), 0))
1193*0b57cec5SDimitry Andric {
1194*0b57cec5SDimitry Andric     if (__l == 0)
1195*0b57cec5SDimitry Andric         __throw_runtime_error("ctype_byname<char>::ctype_byname"
1196*0b57cec5SDimitry Andric                             " failed to construct for " + name);
1197*0b57cec5SDimitry Andric }
1198*0b57cec5SDimitry Andric 
1199*0b57cec5SDimitry Andric ctype_byname<char>::~ctype_byname()
1200*0b57cec5SDimitry Andric {
1201*0b57cec5SDimitry Andric     freelocale(__l);
1202*0b57cec5SDimitry Andric }
1203*0b57cec5SDimitry Andric 
1204*0b57cec5SDimitry Andric char
1205*0b57cec5SDimitry Andric ctype_byname<char>::do_toupper(char_type c) const
1206*0b57cec5SDimitry Andric {
1207*0b57cec5SDimitry Andric     return static_cast<char>(toupper_l(static_cast<unsigned char>(c), __l));
1208*0b57cec5SDimitry Andric }
1209*0b57cec5SDimitry Andric 
1210*0b57cec5SDimitry Andric const char*
1211*0b57cec5SDimitry Andric ctype_byname<char>::do_toupper(char_type* low, const char_type* high) const
1212*0b57cec5SDimitry Andric {
1213*0b57cec5SDimitry Andric     for (; low != high; ++low)
1214*0b57cec5SDimitry Andric         *low = static_cast<char>(toupper_l(static_cast<unsigned char>(*low), __l));
1215*0b57cec5SDimitry Andric     return low;
1216*0b57cec5SDimitry Andric }
1217*0b57cec5SDimitry Andric 
1218*0b57cec5SDimitry Andric char
1219*0b57cec5SDimitry Andric ctype_byname<char>::do_tolower(char_type c) const
1220*0b57cec5SDimitry Andric {
1221*0b57cec5SDimitry Andric     return static_cast<char>(tolower_l(static_cast<unsigned char>(c), __l));
1222*0b57cec5SDimitry Andric }
1223*0b57cec5SDimitry Andric 
1224*0b57cec5SDimitry Andric const char*
1225*0b57cec5SDimitry Andric ctype_byname<char>::do_tolower(char_type* low, const char_type* high) const
1226*0b57cec5SDimitry Andric {
1227*0b57cec5SDimitry Andric     for (; low != high; ++low)
1228*0b57cec5SDimitry Andric         *low = static_cast<char>(tolower_l(static_cast<unsigned char>(*low), __l));
1229*0b57cec5SDimitry Andric     return low;
1230*0b57cec5SDimitry Andric }
1231*0b57cec5SDimitry Andric 
1232*0b57cec5SDimitry Andric // template <> class ctype_byname<wchar_t>
1233*0b57cec5SDimitry Andric 
1234*0b57cec5SDimitry Andric ctype_byname<wchar_t>::ctype_byname(const char* name, size_t refs)
1235*0b57cec5SDimitry Andric     : ctype<wchar_t>(refs),
1236*0b57cec5SDimitry Andric       __l(newlocale(LC_ALL_MASK, name, 0))
1237*0b57cec5SDimitry Andric {
1238*0b57cec5SDimitry Andric     if (__l == 0)
1239*0b57cec5SDimitry Andric         __throw_runtime_error("ctype_byname<wchar_t>::ctype_byname"
1240*0b57cec5SDimitry Andric                             " failed to construct for " + string(name));
1241*0b57cec5SDimitry Andric }
1242*0b57cec5SDimitry Andric 
1243*0b57cec5SDimitry Andric ctype_byname<wchar_t>::ctype_byname(const string& name, size_t refs)
1244*0b57cec5SDimitry Andric     : ctype<wchar_t>(refs),
1245*0b57cec5SDimitry Andric       __l(newlocale(LC_ALL_MASK, name.c_str(), 0))
1246*0b57cec5SDimitry Andric {
1247*0b57cec5SDimitry Andric     if (__l == 0)
1248*0b57cec5SDimitry Andric         __throw_runtime_error("ctype_byname<wchar_t>::ctype_byname"
1249*0b57cec5SDimitry Andric                             " failed to construct for " + name);
1250*0b57cec5SDimitry Andric }
1251*0b57cec5SDimitry Andric 
1252*0b57cec5SDimitry Andric ctype_byname<wchar_t>::~ctype_byname()
1253*0b57cec5SDimitry Andric {
1254*0b57cec5SDimitry Andric     freelocale(__l);
1255*0b57cec5SDimitry Andric }
1256*0b57cec5SDimitry Andric 
1257*0b57cec5SDimitry Andric bool
1258*0b57cec5SDimitry Andric ctype_byname<wchar_t>::do_is(mask m, char_type c) const
1259*0b57cec5SDimitry Andric {
1260*0b57cec5SDimitry Andric #ifdef _LIBCPP_WCTYPE_IS_MASK
1261*0b57cec5SDimitry Andric     return static_cast<bool>(iswctype_l(c, m, __l));
1262*0b57cec5SDimitry Andric #else
1263*0b57cec5SDimitry Andric     bool result = false;
1264*0b57cec5SDimitry Andric     wint_t ch = static_cast<wint_t>(c);
1265*0b57cec5SDimitry Andric     if ((m & space) == space) result |= (iswspace_l(ch, __l) != 0);
1266*0b57cec5SDimitry Andric     if ((m & print) == print) result |= (iswprint_l(ch, __l) != 0);
1267*0b57cec5SDimitry Andric     if ((m & cntrl) == cntrl) result |= (iswcntrl_l(ch, __l) != 0);
1268*0b57cec5SDimitry Andric     if ((m & upper) == upper) result |= (iswupper_l(ch, __l) != 0);
1269*0b57cec5SDimitry Andric     if ((m & lower) == lower) result |= (iswlower_l(ch, __l) != 0);
1270*0b57cec5SDimitry Andric     if ((m & alpha) == alpha) result |= (iswalpha_l(ch, __l) != 0);
1271*0b57cec5SDimitry Andric     if ((m & digit) == digit) result |= (iswdigit_l(ch, __l) != 0);
1272*0b57cec5SDimitry Andric     if ((m & punct) == punct) result |= (iswpunct_l(ch, __l) != 0);
1273*0b57cec5SDimitry Andric     if ((m & xdigit) == xdigit) result |= (iswxdigit_l(ch, __l) != 0);
1274*0b57cec5SDimitry Andric     if ((m & blank) == blank) result |= (iswblank_l(ch, __l) != 0);
1275*0b57cec5SDimitry Andric     return result;
1276*0b57cec5SDimitry Andric #endif
1277*0b57cec5SDimitry Andric }
1278*0b57cec5SDimitry Andric 
1279*0b57cec5SDimitry Andric const wchar_t*
1280*0b57cec5SDimitry Andric ctype_byname<wchar_t>::do_is(const char_type* low, const char_type* high, mask* vec) const
1281*0b57cec5SDimitry Andric {
1282*0b57cec5SDimitry Andric     for (; low != high; ++low, ++vec)
1283*0b57cec5SDimitry Andric     {
1284*0b57cec5SDimitry Andric         if (isascii(*low))
1285*0b57cec5SDimitry Andric             *vec = static_cast<mask>(ctype<char>::classic_table()[*low]);
1286*0b57cec5SDimitry Andric         else
1287*0b57cec5SDimitry Andric         {
1288*0b57cec5SDimitry Andric             *vec = 0;
1289*0b57cec5SDimitry Andric             wint_t ch = static_cast<wint_t>(*low);
1290*0b57cec5SDimitry Andric             if (iswspace_l(ch, __l))
1291*0b57cec5SDimitry Andric                 *vec |= space;
1292*0b57cec5SDimitry Andric #ifndef _LIBCPP_CTYPE_MASK_IS_COMPOSITE_PRINT
1293*0b57cec5SDimitry Andric             if (iswprint_l(ch, __l))
1294*0b57cec5SDimitry Andric                 *vec |= print;
1295*0b57cec5SDimitry Andric #endif
1296*0b57cec5SDimitry Andric             if (iswcntrl_l(ch, __l))
1297*0b57cec5SDimitry Andric                 *vec |= cntrl;
1298*0b57cec5SDimitry Andric             if (iswupper_l(ch, __l))
1299*0b57cec5SDimitry Andric                 *vec |= upper;
1300*0b57cec5SDimitry Andric             if (iswlower_l(ch, __l))
1301*0b57cec5SDimitry Andric                 *vec |= lower;
1302*0b57cec5SDimitry Andric #ifndef _LIBCPP_CTYPE_MASK_IS_COMPOSITE_ALPHA
1303*0b57cec5SDimitry Andric             if (iswalpha_l(ch, __l))
1304*0b57cec5SDimitry Andric                 *vec |= alpha;
1305*0b57cec5SDimitry Andric #endif
1306*0b57cec5SDimitry Andric             if (iswdigit_l(ch, __l))
1307*0b57cec5SDimitry Andric                 *vec |= digit;
1308*0b57cec5SDimitry Andric             if (iswpunct_l(ch, __l))
1309*0b57cec5SDimitry Andric                 *vec |= punct;
1310*0b57cec5SDimitry Andric #ifndef _LIBCPP_CTYPE_MASK_IS_COMPOSITE_XDIGIT
1311*0b57cec5SDimitry Andric             if (iswxdigit_l(ch, __l))
1312*0b57cec5SDimitry Andric                 *vec |= xdigit;
1313*0b57cec5SDimitry Andric #endif
1314*0b57cec5SDimitry Andric #if !defined(__sun__)
1315*0b57cec5SDimitry Andric             if (iswblank_l(ch, __l))
1316*0b57cec5SDimitry Andric                 *vec |= blank;
1317*0b57cec5SDimitry Andric #endif
1318*0b57cec5SDimitry Andric         }
1319*0b57cec5SDimitry Andric     }
1320*0b57cec5SDimitry Andric     return low;
1321*0b57cec5SDimitry Andric }
1322*0b57cec5SDimitry Andric 
1323*0b57cec5SDimitry Andric const wchar_t*
1324*0b57cec5SDimitry Andric ctype_byname<wchar_t>::do_scan_is(mask m, const char_type* low, const char_type* high) const
1325*0b57cec5SDimitry Andric {
1326*0b57cec5SDimitry Andric     for (; low != high; ++low)
1327*0b57cec5SDimitry Andric     {
1328*0b57cec5SDimitry Andric #ifdef _LIBCPP_WCTYPE_IS_MASK
1329*0b57cec5SDimitry Andric         if (iswctype_l(*low, m, __l))
1330*0b57cec5SDimitry Andric             break;
1331*0b57cec5SDimitry Andric #else
1332*0b57cec5SDimitry Andric         wint_t ch = static_cast<wint_t>(*low);
1333*0b57cec5SDimitry Andric         if ((m & space) == space && iswspace_l(ch, __l)) break;
1334*0b57cec5SDimitry Andric         if ((m & print) == print && iswprint_l(ch, __l)) break;
1335*0b57cec5SDimitry Andric         if ((m & cntrl) == cntrl && iswcntrl_l(ch, __l)) break;
1336*0b57cec5SDimitry Andric         if ((m & upper) == upper && iswupper_l(ch, __l)) break;
1337*0b57cec5SDimitry Andric         if ((m & lower) == lower && iswlower_l(ch, __l)) break;
1338*0b57cec5SDimitry Andric         if ((m & alpha) == alpha && iswalpha_l(ch, __l)) break;
1339*0b57cec5SDimitry Andric         if ((m & digit) == digit && iswdigit_l(ch, __l)) break;
1340*0b57cec5SDimitry Andric         if ((m & punct) == punct && iswpunct_l(ch, __l)) break;
1341*0b57cec5SDimitry Andric         if ((m & xdigit) == xdigit && iswxdigit_l(ch, __l)) break;
1342*0b57cec5SDimitry Andric         if ((m & blank) == blank && iswblank_l(ch, __l)) break;
1343*0b57cec5SDimitry Andric #endif
1344*0b57cec5SDimitry Andric     }
1345*0b57cec5SDimitry Andric     return low;
1346*0b57cec5SDimitry Andric }
1347*0b57cec5SDimitry Andric 
1348*0b57cec5SDimitry Andric const wchar_t*
1349*0b57cec5SDimitry Andric ctype_byname<wchar_t>::do_scan_not(mask m, const char_type* low, const char_type* high) const
1350*0b57cec5SDimitry Andric {
1351*0b57cec5SDimitry Andric     for (; low != high; ++low)
1352*0b57cec5SDimitry Andric     {
1353*0b57cec5SDimitry Andric #ifdef _LIBCPP_WCTYPE_IS_MASK
1354*0b57cec5SDimitry Andric         if (!iswctype_l(*low, m, __l))
1355*0b57cec5SDimitry Andric             break;
1356*0b57cec5SDimitry Andric #else
1357*0b57cec5SDimitry Andric         wint_t ch = static_cast<wint_t>(*low);
1358*0b57cec5SDimitry Andric         if ((m & space) == space && iswspace_l(ch, __l)) continue;
1359*0b57cec5SDimitry Andric         if ((m & print) == print && iswprint_l(ch, __l)) continue;
1360*0b57cec5SDimitry Andric         if ((m & cntrl) == cntrl && iswcntrl_l(ch, __l)) continue;
1361*0b57cec5SDimitry Andric         if ((m & upper) == upper && iswupper_l(ch, __l)) continue;
1362*0b57cec5SDimitry Andric         if ((m & lower) == lower && iswlower_l(ch, __l)) continue;
1363*0b57cec5SDimitry Andric         if ((m & alpha) == alpha && iswalpha_l(ch, __l)) continue;
1364*0b57cec5SDimitry Andric         if ((m & digit) == digit && iswdigit_l(ch, __l)) continue;
1365*0b57cec5SDimitry Andric         if ((m & punct) == punct && iswpunct_l(ch, __l)) continue;
1366*0b57cec5SDimitry Andric         if ((m & xdigit) == xdigit && iswxdigit_l(ch, __l)) continue;
1367*0b57cec5SDimitry Andric         if ((m & blank) == blank && iswblank_l(ch, __l)) continue;
1368*0b57cec5SDimitry Andric         break;
1369*0b57cec5SDimitry Andric #endif
1370*0b57cec5SDimitry Andric     }
1371*0b57cec5SDimitry Andric     return low;
1372*0b57cec5SDimitry Andric }
1373*0b57cec5SDimitry Andric 
1374*0b57cec5SDimitry Andric wchar_t
1375*0b57cec5SDimitry Andric ctype_byname<wchar_t>::do_toupper(char_type c) const
1376*0b57cec5SDimitry Andric {
1377*0b57cec5SDimitry Andric     return towupper_l(c, __l);
1378*0b57cec5SDimitry Andric }
1379*0b57cec5SDimitry Andric 
1380*0b57cec5SDimitry Andric const wchar_t*
1381*0b57cec5SDimitry Andric ctype_byname<wchar_t>::do_toupper(char_type* low, const char_type* high) const
1382*0b57cec5SDimitry Andric {
1383*0b57cec5SDimitry Andric     for (; low != high; ++low)
1384*0b57cec5SDimitry Andric         *low = towupper_l(*low, __l);
1385*0b57cec5SDimitry Andric     return low;
1386*0b57cec5SDimitry Andric }
1387*0b57cec5SDimitry Andric 
1388*0b57cec5SDimitry Andric wchar_t
1389*0b57cec5SDimitry Andric ctype_byname<wchar_t>::do_tolower(char_type c) const
1390*0b57cec5SDimitry Andric {
1391*0b57cec5SDimitry Andric     return towlower_l(c, __l);
1392*0b57cec5SDimitry Andric }
1393*0b57cec5SDimitry Andric 
1394*0b57cec5SDimitry Andric const wchar_t*
1395*0b57cec5SDimitry Andric ctype_byname<wchar_t>::do_tolower(char_type* low, const char_type* high) const
1396*0b57cec5SDimitry Andric {
1397*0b57cec5SDimitry Andric     for (; low != high; ++low)
1398*0b57cec5SDimitry Andric         *low = towlower_l(*low, __l);
1399*0b57cec5SDimitry Andric     return low;
1400*0b57cec5SDimitry Andric }
1401*0b57cec5SDimitry Andric 
1402*0b57cec5SDimitry Andric wchar_t
1403*0b57cec5SDimitry Andric ctype_byname<wchar_t>::do_widen(char c) const
1404*0b57cec5SDimitry Andric {
1405*0b57cec5SDimitry Andric     return __libcpp_btowc_l(c, __l);
1406*0b57cec5SDimitry Andric }
1407*0b57cec5SDimitry Andric 
1408*0b57cec5SDimitry Andric const char*
1409*0b57cec5SDimitry Andric ctype_byname<wchar_t>::do_widen(const char* low, const char* high, char_type* dest) const
1410*0b57cec5SDimitry Andric {
1411*0b57cec5SDimitry Andric     for (; low != high; ++low, ++dest)
1412*0b57cec5SDimitry Andric         *dest = __libcpp_btowc_l(*low, __l);
1413*0b57cec5SDimitry Andric     return low;
1414*0b57cec5SDimitry Andric }
1415*0b57cec5SDimitry Andric 
1416*0b57cec5SDimitry Andric char
1417*0b57cec5SDimitry Andric ctype_byname<wchar_t>::do_narrow(char_type c, char dfault) const
1418*0b57cec5SDimitry Andric {
1419*0b57cec5SDimitry Andric     int r = __libcpp_wctob_l(c, __l);
1420*0b57cec5SDimitry Andric     return r != static_cast<int>(WEOF) ? static_cast<char>(r) : dfault;
1421*0b57cec5SDimitry Andric }
1422*0b57cec5SDimitry Andric 
1423*0b57cec5SDimitry Andric const wchar_t*
1424*0b57cec5SDimitry Andric ctype_byname<wchar_t>::do_narrow(const char_type* low, const char_type* high, char dfault, char* dest) const
1425*0b57cec5SDimitry Andric {
1426*0b57cec5SDimitry Andric     for (; low != high; ++low, ++dest)
1427*0b57cec5SDimitry Andric     {
1428*0b57cec5SDimitry Andric         int r = __libcpp_wctob_l(*low, __l);
1429*0b57cec5SDimitry Andric         *dest = r != static_cast<int>(WEOF) ? static_cast<char>(r) : dfault;
1430*0b57cec5SDimitry Andric     }
1431*0b57cec5SDimitry Andric     return low;
1432*0b57cec5SDimitry Andric }
1433*0b57cec5SDimitry Andric 
1434*0b57cec5SDimitry Andric // template <> class codecvt<char, char, mbstate_t>
1435*0b57cec5SDimitry Andric 
1436*0b57cec5SDimitry Andric locale::id codecvt<char, char, mbstate_t>::id;
1437*0b57cec5SDimitry Andric 
1438*0b57cec5SDimitry Andric codecvt<char, char, mbstate_t>::~codecvt()
1439*0b57cec5SDimitry Andric {
1440*0b57cec5SDimitry Andric }
1441*0b57cec5SDimitry Andric 
1442*0b57cec5SDimitry Andric codecvt<char, char, mbstate_t>::result
1443*0b57cec5SDimitry Andric codecvt<char, char, mbstate_t>::do_out(state_type&,
1444*0b57cec5SDimitry Andric     const intern_type* frm, const intern_type*, const intern_type*& frm_nxt,
1445*0b57cec5SDimitry Andric     extern_type* to, extern_type*, extern_type*& to_nxt) const
1446*0b57cec5SDimitry Andric {
1447*0b57cec5SDimitry Andric     frm_nxt = frm;
1448*0b57cec5SDimitry Andric     to_nxt = to;
1449*0b57cec5SDimitry Andric     return noconv;
1450*0b57cec5SDimitry Andric }
1451*0b57cec5SDimitry Andric 
1452*0b57cec5SDimitry Andric codecvt<char, char, mbstate_t>::result
1453*0b57cec5SDimitry Andric codecvt<char, char, mbstate_t>::do_in(state_type&,
1454*0b57cec5SDimitry Andric     const extern_type* frm, const extern_type*, const extern_type*& frm_nxt,
1455*0b57cec5SDimitry Andric     intern_type* to, intern_type*, intern_type*& to_nxt) const
1456*0b57cec5SDimitry Andric {
1457*0b57cec5SDimitry Andric     frm_nxt = frm;
1458*0b57cec5SDimitry Andric     to_nxt = to;
1459*0b57cec5SDimitry Andric     return noconv;
1460*0b57cec5SDimitry Andric }
1461*0b57cec5SDimitry Andric 
1462*0b57cec5SDimitry Andric codecvt<char, char, mbstate_t>::result
1463*0b57cec5SDimitry Andric codecvt<char, char, mbstate_t>::do_unshift(state_type&,
1464*0b57cec5SDimitry Andric     extern_type* to, extern_type*, extern_type*& to_nxt) const
1465*0b57cec5SDimitry Andric {
1466*0b57cec5SDimitry Andric     to_nxt = to;
1467*0b57cec5SDimitry Andric     return noconv;
1468*0b57cec5SDimitry Andric }
1469*0b57cec5SDimitry Andric 
1470*0b57cec5SDimitry Andric int
1471*0b57cec5SDimitry Andric codecvt<char, char, mbstate_t>::do_encoding() const  _NOEXCEPT
1472*0b57cec5SDimitry Andric {
1473*0b57cec5SDimitry Andric     return 1;
1474*0b57cec5SDimitry Andric }
1475*0b57cec5SDimitry Andric 
1476*0b57cec5SDimitry Andric bool
1477*0b57cec5SDimitry Andric codecvt<char, char, mbstate_t>::do_always_noconv() const  _NOEXCEPT
1478*0b57cec5SDimitry Andric {
1479*0b57cec5SDimitry Andric     return true;
1480*0b57cec5SDimitry Andric }
1481*0b57cec5SDimitry Andric 
1482*0b57cec5SDimitry Andric int
1483*0b57cec5SDimitry Andric codecvt<char, char, mbstate_t>::do_length(state_type&,
1484*0b57cec5SDimitry Andric     const extern_type* frm, const extern_type* end, size_t mx) const
1485*0b57cec5SDimitry Andric {
1486*0b57cec5SDimitry Andric     return static_cast<int>(min<size_t>(mx, static_cast<size_t>(end-frm)));
1487*0b57cec5SDimitry Andric }
1488*0b57cec5SDimitry Andric 
1489*0b57cec5SDimitry Andric int
1490*0b57cec5SDimitry Andric codecvt<char, char, mbstate_t>::do_max_length() const  _NOEXCEPT
1491*0b57cec5SDimitry Andric {
1492*0b57cec5SDimitry Andric     return 1;
1493*0b57cec5SDimitry Andric }
1494*0b57cec5SDimitry Andric 
1495*0b57cec5SDimitry Andric // template <> class codecvt<wchar_t, char, mbstate_t>
1496*0b57cec5SDimitry Andric 
1497*0b57cec5SDimitry Andric locale::id codecvt<wchar_t, char, mbstate_t>::id;
1498*0b57cec5SDimitry Andric 
1499*0b57cec5SDimitry Andric codecvt<wchar_t, char, mbstate_t>::codecvt(size_t refs)
1500*0b57cec5SDimitry Andric     : locale::facet(refs),
1501*0b57cec5SDimitry Andric       __l(_LIBCPP_GET_C_LOCALE)
1502*0b57cec5SDimitry Andric {
1503*0b57cec5SDimitry Andric }
1504*0b57cec5SDimitry Andric 
1505*0b57cec5SDimitry Andric codecvt<wchar_t, char, mbstate_t>::codecvt(const char* nm, size_t refs)
1506*0b57cec5SDimitry Andric     : locale::facet(refs),
1507*0b57cec5SDimitry Andric       __l(newlocale(LC_ALL_MASK, nm, 0))
1508*0b57cec5SDimitry Andric {
1509*0b57cec5SDimitry Andric     if (__l == 0)
1510*0b57cec5SDimitry Andric         __throw_runtime_error("codecvt_byname<wchar_t, char, mbstate_t>::codecvt_byname"
1511*0b57cec5SDimitry Andric                             " failed to construct for " + string(nm));
1512*0b57cec5SDimitry Andric }
1513*0b57cec5SDimitry Andric 
1514*0b57cec5SDimitry Andric codecvt<wchar_t, char, mbstate_t>::~codecvt()
1515*0b57cec5SDimitry Andric {
1516*0b57cec5SDimitry Andric     if (__l != _LIBCPP_GET_C_LOCALE)
1517*0b57cec5SDimitry Andric         freelocale(__l);
1518*0b57cec5SDimitry Andric }
1519*0b57cec5SDimitry Andric 
1520*0b57cec5SDimitry Andric codecvt<wchar_t, char, mbstate_t>::result
1521*0b57cec5SDimitry Andric codecvt<wchar_t, char, mbstate_t>::do_out(state_type& st,
1522*0b57cec5SDimitry Andric     const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt,
1523*0b57cec5SDimitry Andric     extern_type* to, extern_type* to_end, extern_type*& to_nxt) const
1524*0b57cec5SDimitry Andric {
1525*0b57cec5SDimitry Andric     // look for first internal null in frm
1526*0b57cec5SDimitry Andric     const intern_type* fend = frm;
1527*0b57cec5SDimitry Andric     for (; fend != frm_end; ++fend)
1528*0b57cec5SDimitry Andric         if (*fend == 0)
1529*0b57cec5SDimitry Andric             break;
1530*0b57cec5SDimitry Andric     // loop over all null-terminated sequences in frm
1531*0b57cec5SDimitry Andric     to_nxt = to;
1532*0b57cec5SDimitry Andric     for (frm_nxt = frm; frm != frm_end && to != to_end; frm = frm_nxt, to = to_nxt)
1533*0b57cec5SDimitry Andric     {
1534*0b57cec5SDimitry Andric         // save state in case it is needed to recover to_nxt on error
1535*0b57cec5SDimitry Andric         mbstate_t save_state = st;
1536*0b57cec5SDimitry Andric         size_t n = __libcpp_wcsnrtombs_l(to, &frm_nxt, static_cast<size_t>(fend-frm),
1537*0b57cec5SDimitry Andric                                      static_cast<size_t>(to_end-to), &st, __l);
1538*0b57cec5SDimitry Andric         if (n == size_t(-1))
1539*0b57cec5SDimitry Andric         {
1540*0b57cec5SDimitry Andric             // need to recover to_nxt
1541*0b57cec5SDimitry Andric             for (to_nxt = to; frm != frm_nxt; ++frm)
1542*0b57cec5SDimitry Andric             {
1543*0b57cec5SDimitry Andric                 n = __libcpp_wcrtomb_l(to_nxt, *frm, &save_state, __l);
1544*0b57cec5SDimitry Andric                 if (n == size_t(-1))
1545*0b57cec5SDimitry Andric                     break;
1546*0b57cec5SDimitry Andric                 to_nxt += n;
1547*0b57cec5SDimitry Andric             }
1548*0b57cec5SDimitry Andric             frm_nxt = frm;
1549*0b57cec5SDimitry Andric             return error;
1550*0b57cec5SDimitry Andric         }
1551*0b57cec5SDimitry Andric         if (n == 0)
1552*0b57cec5SDimitry Andric             return partial;
1553*0b57cec5SDimitry Andric         to_nxt += n;
1554*0b57cec5SDimitry Andric         if (to_nxt == to_end)
1555*0b57cec5SDimitry Andric             break;
1556*0b57cec5SDimitry Andric         if (fend != frm_end)  // set up next null terminated sequence
1557*0b57cec5SDimitry Andric         {
1558*0b57cec5SDimitry Andric             // Try to write the terminating null
1559*0b57cec5SDimitry Andric             extern_type tmp[MB_LEN_MAX];
1560*0b57cec5SDimitry Andric             n = __libcpp_wcrtomb_l(tmp, intern_type(), &st, __l);
1561*0b57cec5SDimitry Andric             if (n == size_t(-1))  // on error
1562*0b57cec5SDimitry Andric                 return error;
1563*0b57cec5SDimitry Andric             if (n > static_cast<size_t>(to_end-to_nxt))  // is there room?
1564*0b57cec5SDimitry Andric                 return partial;
1565*0b57cec5SDimitry Andric             for (extern_type* p = tmp; n; --n)  // write it
1566*0b57cec5SDimitry Andric                 *to_nxt++ = *p++;
1567*0b57cec5SDimitry Andric             ++frm_nxt;
1568*0b57cec5SDimitry Andric             // look for next null in frm
1569*0b57cec5SDimitry Andric             for (fend = frm_nxt; fend != frm_end; ++fend)
1570*0b57cec5SDimitry Andric                 if (*fend == 0)
1571*0b57cec5SDimitry Andric                     break;
1572*0b57cec5SDimitry Andric         }
1573*0b57cec5SDimitry Andric     }
1574*0b57cec5SDimitry Andric     return frm_nxt == frm_end ? ok : partial;
1575*0b57cec5SDimitry Andric }
1576*0b57cec5SDimitry Andric 
1577*0b57cec5SDimitry Andric codecvt<wchar_t, char, mbstate_t>::result
1578*0b57cec5SDimitry Andric codecvt<wchar_t, char, mbstate_t>::do_in(state_type& st,
1579*0b57cec5SDimitry Andric     const extern_type* frm, const extern_type* frm_end, const extern_type*& frm_nxt,
1580*0b57cec5SDimitry Andric     intern_type* to, intern_type* to_end, intern_type*& to_nxt) const
1581*0b57cec5SDimitry Andric {
1582*0b57cec5SDimitry Andric     // look for first internal null in frm
1583*0b57cec5SDimitry Andric     const extern_type* fend = frm;
1584*0b57cec5SDimitry Andric     for (; fend != frm_end; ++fend)
1585*0b57cec5SDimitry Andric         if (*fend == 0)
1586*0b57cec5SDimitry Andric             break;
1587*0b57cec5SDimitry Andric     // loop over all null-terminated sequences in frm
1588*0b57cec5SDimitry Andric     to_nxt = to;
1589*0b57cec5SDimitry Andric     for (frm_nxt = frm; frm != frm_end && to != to_end; frm = frm_nxt, to = to_nxt)
1590*0b57cec5SDimitry Andric     {
1591*0b57cec5SDimitry Andric         // save state in case it is needed to recover to_nxt on error
1592*0b57cec5SDimitry Andric         mbstate_t save_state = st;
1593*0b57cec5SDimitry Andric         size_t n = __libcpp_mbsnrtowcs_l(to, &frm_nxt, static_cast<size_t>(fend-frm),
1594*0b57cec5SDimitry Andric                                      static_cast<size_t>(to_end-to), &st, __l);
1595*0b57cec5SDimitry Andric         if (n == size_t(-1))
1596*0b57cec5SDimitry Andric         {
1597*0b57cec5SDimitry Andric             // need to recover to_nxt
1598*0b57cec5SDimitry Andric             for (to_nxt = to; frm != frm_nxt; ++to_nxt)
1599*0b57cec5SDimitry Andric             {
1600*0b57cec5SDimitry Andric                 n = __libcpp_mbrtowc_l(to_nxt, frm, static_cast<size_t>(fend-frm),
1601*0b57cec5SDimitry Andric                                    &save_state, __l);
1602*0b57cec5SDimitry Andric                 switch (n)
1603*0b57cec5SDimitry Andric                 {
1604*0b57cec5SDimitry Andric                 case 0:
1605*0b57cec5SDimitry Andric                     ++frm;
1606*0b57cec5SDimitry Andric                     break;
1607*0b57cec5SDimitry Andric                 case size_t(-1):
1608*0b57cec5SDimitry Andric                     frm_nxt = frm;
1609*0b57cec5SDimitry Andric                     return error;
1610*0b57cec5SDimitry Andric                 case size_t(-2):
1611*0b57cec5SDimitry Andric                     frm_nxt = frm;
1612*0b57cec5SDimitry Andric                     return partial;
1613*0b57cec5SDimitry Andric                 default:
1614*0b57cec5SDimitry Andric                     frm += n;
1615*0b57cec5SDimitry Andric                     break;
1616*0b57cec5SDimitry Andric                 }
1617*0b57cec5SDimitry Andric             }
1618*0b57cec5SDimitry Andric             frm_nxt = frm;
1619*0b57cec5SDimitry Andric             return frm_nxt == frm_end ? ok : partial;
1620*0b57cec5SDimitry Andric         }
1621*0b57cec5SDimitry Andric         if (n == size_t(-1))
1622*0b57cec5SDimitry Andric             return error;
1623*0b57cec5SDimitry Andric         to_nxt += n;
1624*0b57cec5SDimitry Andric         if (to_nxt == to_end)
1625*0b57cec5SDimitry Andric             break;
1626*0b57cec5SDimitry Andric         if (fend != frm_end)  // set up next null terminated sequence
1627*0b57cec5SDimitry Andric         {
1628*0b57cec5SDimitry Andric             // Try to write the terminating null
1629*0b57cec5SDimitry Andric             n = __libcpp_mbrtowc_l(to_nxt, frm_nxt, 1, &st, __l);
1630*0b57cec5SDimitry Andric             if (n != 0)  // on error
1631*0b57cec5SDimitry Andric                 return error;
1632*0b57cec5SDimitry Andric             ++to_nxt;
1633*0b57cec5SDimitry Andric             ++frm_nxt;
1634*0b57cec5SDimitry Andric             // look for next null in frm
1635*0b57cec5SDimitry Andric             for (fend = frm_nxt; fend != frm_end; ++fend)
1636*0b57cec5SDimitry Andric                 if (*fend == 0)
1637*0b57cec5SDimitry Andric                     break;
1638*0b57cec5SDimitry Andric         }
1639*0b57cec5SDimitry Andric     }
1640*0b57cec5SDimitry Andric     return frm_nxt == frm_end ? ok : partial;
1641*0b57cec5SDimitry Andric }
1642*0b57cec5SDimitry Andric 
1643*0b57cec5SDimitry Andric codecvt<wchar_t, char, mbstate_t>::result
1644*0b57cec5SDimitry Andric codecvt<wchar_t, char, mbstate_t>::do_unshift(state_type& st,
1645*0b57cec5SDimitry Andric     extern_type* to, extern_type* to_end, extern_type*& to_nxt) const
1646*0b57cec5SDimitry Andric {
1647*0b57cec5SDimitry Andric     to_nxt = to;
1648*0b57cec5SDimitry Andric     extern_type tmp[MB_LEN_MAX];
1649*0b57cec5SDimitry Andric     size_t n = __libcpp_wcrtomb_l(tmp, intern_type(), &st, __l);
1650*0b57cec5SDimitry Andric     if (n == size_t(-1) || n == 0)  // on error
1651*0b57cec5SDimitry Andric         return error;
1652*0b57cec5SDimitry Andric     --n;
1653*0b57cec5SDimitry Andric     if (n > static_cast<size_t>(to_end-to_nxt))  // is there room?
1654*0b57cec5SDimitry Andric         return partial;
1655*0b57cec5SDimitry Andric     for (extern_type* p = tmp; n; --n)  // write it
1656*0b57cec5SDimitry Andric         *to_nxt++ = *p++;
1657*0b57cec5SDimitry Andric     return ok;
1658*0b57cec5SDimitry Andric }
1659*0b57cec5SDimitry Andric 
1660*0b57cec5SDimitry Andric int
1661*0b57cec5SDimitry Andric codecvt<wchar_t, char, mbstate_t>::do_encoding() const  _NOEXCEPT
1662*0b57cec5SDimitry Andric {
1663*0b57cec5SDimitry Andric     if (__libcpp_mbtowc_l(nullptr, nullptr, MB_LEN_MAX, __l) != 0)
1664*0b57cec5SDimitry Andric         return -1;
1665*0b57cec5SDimitry Andric 
1666*0b57cec5SDimitry Andric     // stateless encoding
1667*0b57cec5SDimitry Andric     if (__l == 0 || __libcpp_mb_cur_max_l(__l) == 1)  // there are no known constant length encodings
1668*0b57cec5SDimitry Andric         return 1;                // which take more than 1 char to form a wchar_t
1669*0b57cec5SDimitry Andric     return 0;
1670*0b57cec5SDimitry Andric }
1671*0b57cec5SDimitry Andric 
1672*0b57cec5SDimitry Andric bool
1673*0b57cec5SDimitry Andric codecvt<wchar_t, char, mbstate_t>::do_always_noconv() const  _NOEXCEPT
1674*0b57cec5SDimitry Andric {
1675*0b57cec5SDimitry Andric     return false;
1676*0b57cec5SDimitry Andric }
1677*0b57cec5SDimitry Andric 
1678*0b57cec5SDimitry Andric int
1679*0b57cec5SDimitry Andric codecvt<wchar_t, char, mbstate_t>::do_length(state_type& st,
1680*0b57cec5SDimitry Andric     const extern_type* frm, const extern_type* frm_end, size_t mx) const
1681*0b57cec5SDimitry Andric {
1682*0b57cec5SDimitry Andric     int nbytes = 0;
1683*0b57cec5SDimitry Andric     for (size_t nwchar_t = 0; nwchar_t < mx && frm != frm_end; ++nwchar_t)
1684*0b57cec5SDimitry Andric     {
1685*0b57cec5SDimitry Andric         size_t n = __libcpp_mbrlen_l(frm, static_cast<size_t>(frm_end-frm), &st, __l);
1686*0b57cec5SDimitry Andric         switch (n)
1687*0b57cec5SDimitry Andric         {
1688*0b57cec5SDimitry Andric         case 0:
1689*0b57cec5SDimitry Andric             ++nbytes;
1690*0b57cec5SDimitry Andric             ++frm;
1691*0b57cec5SDimitry Andric             break;
1692*0b57cec5SDimitry Andric         case size_t(-1):
1693*0b57cec5SDimitry Andric         case size_t(-2):
1694*0b57cec5SDimitry Andric             return nbytes;
1695*0b57cec5SDimitry Andric         default:
1696*0b57cec5SDimitry Andric             nbytes += n;
1697*0b57cec5SDimitry Andric             frm += n;
1698*0b57cec5SDimitry Andric             break;
1699*0b57cec5SDimitry Andric         }
1700*0b57cec5SDimitry Andric     }
1701*0b57cec5SDimitry Andric     return nbytes;
1702*0b57cec5SDimitry Andric }
1703*0b57cec5SDimitry Andric 
1704*0b57cec5SDimitry Andric int
1705*0b57cec5SDimitry Andric codecvt<wchar_t, char, mbstate_t>::do_max_length() const  _NOEXCEPT
1706*0b57cec5SDimitry Andric {
1707*0b57cec5SDimitry Andric     return __l == 0 ? 1 : static_cast<int>(__libcpp_mb_cur_max_l(__l));
1708*0b57cec5SDimitry Andric }
1709*0b57cec5SDimitry Andric 
1710*0b57cec5SDimitry Andric //                                     Valid UTF ranges
1711*0b57cec5SDimitry Andric //     UTF-32               UTF-16                          UTF-8               # of code points
1712*0b57cec5SDimitry Andric //                     first      second       first   second    third   fourth
1713*0b57cec5SDimitry Andric // 000000 - 00007F  0000 - 007F               00 - 7F                                 127
1714*0b57cec5SDimitry Andric // 000080 - 0007FF  0080 - 07FF               C2 - DF, 80 - BF                       1920
1715*0b57cec5SDimitry Andric // 000800 - 000FFF  0800 - 0FFF               E0 - E0, A0 - BF, 80 - BF              2048
1716*0b57cec5SDimitry Andric // 001000 - 00CFFF  1000 - CFFF               E1 - EC, 80 - BF, 80 - BF             49152
1717*0b57cec5SDimitry Andric // 00D000 - 00D7FF  D000 - D7FF               ED - ED, 80 - 9F, 80 - BF              2048
1718*0b57cec5SDimitry Andric // 00D800 - 00DFFF                invalid
1719*0b57cec5SDimitry Andric // 00E000 - 00FFFF  E000 - FFFF               EE - EF, 80 - BF, 80 - BF              8192
1720*0b57cec5SDimitry Andric // 010000 - 03FFFF  D800 - D8BF, DC00 - DFFF  F0 - F0, 90 - BF, 80 - BF, 80 - BF   196608
1721*0b57cec5SDimitry Andric // 040000 - 0FFFFF  D8C0 - DBBF, DC00 - DFFF  F1 - F3, 80 - BF, 80 - BF, 80 - BF   786432
1722*0b57cec5SDimitry Andric // 100000 - 10FFFF  DBC0 - DBFF, DC00 - DFFF  F4 - F4, 80 - 8F, 80 - BF, 80 - BF    65536
1723*0b57cec5SDimitry Andric 
1724*0b57cec5SDimitry Andric static
1725*0b57cec5SDimitry Andric codecvt_base::result
1726*0b57cec5SDimitry Andric utf16_to_utf8(const uint16_t* frm, const uint16_t* frm_end, const uint16_t*& frm_nxt,
1727*0b57cec5SDimitry Andric               uint8_t* to, uint8_t* to_end, uint8_t*& to_nxt,
1728*0b57cec5SDimitry Andric               unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0))
1729*0b57cec5SDimitry Andric {
1730*0b57cec5SDimitry Andric     frm_nxt = frm;
1731*0b57cec5SDimitry Andric     to_nxt = to;
1732*0b57cec5SDimitry Andric     if (mode & generate_header)
1733*0b57cec5SDimitry Andric     {
1734*0b57cec5SDimitry Andric         if (to_end-to_nxt < 3)
1735*0b57cec5SDimitry Andric             return codecvt_base::partial;
1736*0b57cec5SDimitry Andric         *to_nxt++ = static_cast<uint8_t>(0xEF);
1737*0b57cec5SDimitry Andric         *to_nxt++ = static_cast<uint8_t>(0xBB);
1738*0b57cec5SDimitry Andric         *to_nxt++ = static_cast<uint8_t>(0xBF);
1739*0b57cec5SDimitry Andric     }
1740*0b57cec5SDimitry Andric     for (; frm_nxt < frm_end; ++frm_nxt)
1741*0b57cec5SDimitry Andric     {
1742*0b57cec5SDimitry Andric         uint16_t wc1 = *frm_nxt;
1743*0b57cec5SDimitry Andric         if (wc1 > Maxcode)
1744*0b57cec5SDimitry Andric             return codecvt_base::error;
1745*0b57cec5SDimitry Andric         if (wc1 < 0x0080)
1746*0b57cec5SDimitry Andric         {
1747*0b57cec5SDimitry Andric             if (to_end-to_nxt < 1)
1748*0b57cec5SDimitry Andric                 return codecvt_base::partial;
1749*0b57cec5SDimitry Andric             *to_nxt++ = static_cast<uint8_t>(wc1);
1750*0b57cec5SDimitry Andric         }
1751*0b57cec5SDimitry Andric         else if (wc1 < 0x0800)
1752*0b57cec5SDimitry Andric         {
1753*0b57cec5SDimitry Andric             if (to_end-to_nxt < 2)
1754*0b57cec5SDimitry Andric                 return codecvt_base::partial;
1755*0b57cec5SDimitry Andric             *to_nxt++ = static_cast<uint8_t>(0xC0 | (wc1 >> 6));
1756*0b57cec5SDimitry Andric             *to_nxt++ = static_cast<uint8_t>(0x80 | (wc1 & 0x03F));
1757*0b57cec5SDimitry Andric         }
1758*0b57cec5SDimitry Andric         else if (wc1 < 0xD800)
1759*0b57cec5SDimitry Andric         {
1760*0b57cec5SDimitry Andric             if (to_end-to_nxt < 3)
1761*0b57cec5SDimitry Andric                 return codecvt_base::partial;
1762*0b57cec5SDimitry Andric             *to_nxt++ = static_cast<uint8_t>(0xE0 |  (wc1 >> 12));
1763*0b57cec5SDimitry Andric             *to_nxt++ = static_cast<uint8_t>(0x80 | ((wc1 & 0x0FC0) >> 6));
1764*0b57cec5SDimitry Andric             *to_nxt++ = static_cast<uint8_t>(0x80 |  (wc1 & 0x003F));
1765*0b57cec5SDimitry Andric         }
1766*0b57cec5SDimitry Andric         else if (wc1 < 0xDC00)
1767*0b57cec5SDimitry Andric         {
1768*0b57cec5SDimitry Andric             if (frm_end-frm_nxt < 2)
1769*0b57cec5SDimitry Andric                 return codecvt_base::partial;
1770*0b57cec5SDimitry Andric             uint16_t wc2 = frm_nxt[1];
1771*0b57cec5SDimitry Andric             if ((wc2 & 0xFC00) != 0xDC00)
1772*0b57cec5SDimitry Andric                 return codecvt_base::error;
1773*0b57cec5SDimitry Andric             if (to_end-to_nxt < 4)
1774*0b57cec5SDimitry Andric                 return codecvt_base::partial;
1775*0b57cec5SDimitry Andric             if (((((wc1 & 0x03C0UL) >> 6) + 1) << 16) +
1776*0b57cec5SDimitry Andric                 ((wc1 & 0x003FUL) << 10) + (wc2 & 0x03FF) > Maxcode)
1777*0b57cec5SDimitry Andric                 return codecvt_base::error;
1778*0b57cec5SDimitry Andric             ++frm_nxt;
1779*0b57cec5SDimitry Andric             uint8_t z = ((wc1 & 0x03C0) >> 6) + 1;
1780*0b57cec5SDimitry Andric             *to_nxt++ = static_cast<uint8_t>(0xF0 | (z >> 2));
1781*0b57cec5SDimitry Andric             *to_nxt++ = static_cast<uint8_t>(0x80 | ((z & 0x03) << 4)     | ((wc1 & 0x003C) >> 2));
1782*0b57cec5SDimitry Andric             *to_nxt++ = static_cast<uint8_t>(0x80 | ((wc1 & 0x0003) << 4) | ((wc2 & 0x03C0) >> 6));
1783*0b57cec5SDimitry Andric             *to_nxt++ = static_cast<uint8_t>(0x80 |  (wc2 & 0x003F));
1784*0b57cec5SDimitry Andric         }
1785*0b57cec5SDimitry Andric         else if (wc1 < 0xE000)
1786*0b57cec5SDimitry Andric         {
1787*0b57cec5SDimitry Andric             return codecvt_base::error;
1788*0b57cec5SDimitry Andric         }
1789*0b57cec5SDimitry Andric         else
1790*0b57cec5SDimitry Andric         {
1791*0b57cec5SDimitry Andric             if (to_end-to_nxt < 3)
1792*0b57cec5SDimitry Andric                 return codecvt_base::partial;
1793*0b57cec5SDimitry Andric             *to_nxt++ = static_cast<uint8_t>(0xE0 |  (wc1 >> 12));
1794*0b57cec5SDimitry Andric             *to_nxt++ = static_cast<uint8_t>(0x80 | ((wc1 & 0x0FC0) >> 6));
1795*0b57cec5SDimitry Andric             *to_nxt++ = static_cast<uint8_t>(0x80 |  (wc1 & 0x003F));
1796*0b57cec5SDimitry Andric         }
1797*0b57cec5SDimitry Andric     }
1798*0b57cec5SDimitry Andric     return codecvt_base::ok;
1799*0b57cec5SDimitry Andric }
1800*0b57cec5SDimitry Andric 
1801*0b57cec5SDimitry Andric static
1802*0b57cec5SDimitry Andric codecvt_base::result
1803*0b57cec5SDimitry Andric utf16_to_utf8(const uint32_t* frm, const uint32_t* frm_end, const uint32_t*& frm_nxt,
1804*0b57cec5SDimitry Andric               uint8_t* to, uint8_t* to_end, uint8_t*& to_nxt,
1805*0b57cec5SDimitry Andric               unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0))
1806*0b57cec5SDimitry Andric {
1807*0b57cec5SDimitry Andric     frm_nxt = frm;
1808*0b57cec5SDimitry Andric     to_nxt = to;
1809*0b57cec5SDimitry Andric     if (mode & generate_header)
1810*0b57cec5SDimitry Andric     {
1811*0b57cec5SDimitry Andric         if (to_end-to_nxt < 3)
1812*0b57cec5SDimitry Andric             return codecvt_base::partial;
1813*0b57cec5SDimitry Andric         *to_nxt++ = static_cast<uint8_t>(0xEF);
1814*0b57cec5SDimitry Andric         *to_nxt++ = static_cast<uint8_t>(0xBB);
1815*0b57cec5SDimitry Andric         *to_nxt++ = static_cast<uint8_t>(0xBF);
1816*0b57cec5SDimitry Andric     }
1817*0b57cec5SDimitry Andric     for (; frm_nxt < frm_end; ++frm_nxt)
1818*0b57cec5SDimitry Andric     {
1819*0b57cec5SDimitry Andric         uint16_t wc1 = static_cast<uint16_t>(*frm_nxt);
1820*0b57cec5SDimitry Andric         if (wc1 > Maxcode)
1821*0b57cec5SDimitry Andric             return codecvt_base::error;
1822*0b57cec5SDimitry Andric         if (wc1 < 0x0080)
1823*0b57cec5SDimitry Andric         {
1824*0b57cec5SDimitry Andric             if (to_end-to_nxt < 1)
1825*0b57cec5SDimitry Andric                 return codecvt_base::partial;
1826*0b57cec5SDimitry Andric             *to_nxt++ = static_cast<uint8_t>(wc1);
1827*0b57cec5SDimitry Andric         }
1828*0b57cec5SDimitry Andric         else if (wc1 < 0x0800)
1829*0b57cec5SDimitry Andric         {
1830*0b57cec5SDimitry Andric             if (to_end-to_nxt < 2)
1831*0b57cec5SDimitry Andric                 return codecvt_base::partial;
1832*0b57cec5SDimitry Andric             *to_nxt++ = static_cast<uint8_t>(0xC0 | (wc1 >> 6));
1833*0b57cec5SDimitry Andric             *to_nxt++ = static_cast<uint8_t>(0x80 | (wc1 & 0x03F));
1834*0b57cec5SDimitry Andric         }
1835*0b57cec5SDimitry Andric         else if (wc1 < 0xD800)
1836*0b57cec5SDimitry Andric         {
1837*0b57cec5SDimitry Andric             if (to_end-to_nxt < 3)
1838*0b57cec5SDimitry Andric                 return codecvt_base::partial;
1839*0b57cec5SDimitry Andric             *to_nxt++ = static_cast<uint8_t>(0xE0 |  (wc1 >> 12));
1840*0b57cec5SDimitry Andric             *to_nxt++ = static_cast<uint8_t>(0x80 | ((wc1 & 0x0FC0) >> 6));
1841*0b57cec5SDimitry Andric             *to_nxt++ = static_cast<uint8_t>(0x80 |  (wc1 & 0x003F));
1842*0b57cec5SDimitry Andric         }
1843*0b57cec5SDimitry Andric         else if (wc1 < 0xDC00)
1844*0b57cec5SDimitry Andric         {
1845*0b57cec5SDimitry Andric             if (frm_end-frm_nxt < 2)
1846*0b57cec5SDimitry Andric                 return codecvt_base::partial;
1847*0b57cec5SDimitry Andric             uint16_t wc2 = static_cast<uint16_t>(frm_nxt[1]);
1848*0b57cec5SDimitry Andric             if ((wc2 & 0xFC00) != 0xDC00)
1849*0b57cec5SDimitry Andric                 return codecvt_base::error;
1850*0b57cec5SDimitry Andric             if (to_end-to_nxt < 4)
1851*0b57cec5SDimitry Andric                 return codecvt_base::partial;
1852*0b57cec5SDimitry Andric             if (((((wc1 & 0x03C0UL) >> 6) + 1) << 16) +
1853*0b57cec5SDimitry Andric                 ((wc1 & 0x003FUL) << 10) + (wc2 & 0x03FF) > Maxcode)
1854*0b57cec5SDimitry Andric                 return codecvt_base::error;
1855*0b57cec5SDimitry Andric             ++frm_nxt;
1856*0b57cec5SDimitry Andric             uint8_t z = ((wc1 & 0x03C0) >> 6) + 1;
1857*0b57cec5SDimitry Andric             *to_nxt++ = static_cast<uint8_t>(0xF0 | (z >> 2));
1858*0b57cec5SDimitry Andric             *to_nxt++ = static_cast<uint8_t>(0x80 | ((z & 0x03) << 4)     | ((wc1 & 0x003C) >> 2));
1859*0b57cec5SDimitry Andric             *to_nxt++ = static_cast<uint8_t>(0x80 | ((wc1 & 0x0003) << 4) | ((wc2 & 0x03C0) >> 6));
1860*0b57cec5SDimitry Andric             *to_nxt++ = static_cast<uint8_t>(0x80 |  (wc2 & 0x003F));
1861*0b57cec5SDimitry Andric         }
1862*0b57cec5SDimitry Andric         else if (wc1 < 0xE000)
1863*0b57cec5SDimitry Andric         {
1864*0b57cec5SDimitry Andric             return codecvt_base::error;
1865*0b57cec5SDimitry Andric         }
1866*0b57cec5SDimitry Andric         else
1867*0b57cec5SDimitry Andric         {
1868*0b57cec5SDimitry Andric             if (to_end-to_nxt < 3)
1869*0b57cec5SDimitry Andric                 return codecvt_base::partial;
1870*0b57cec5SDimitry Andric             *to_nxt++ = static_cast<uint8_t>(0xE0 |  (wc1 >> 12));
1871*0b57cec5SDimitry Andric             *to_nxt++ = static_cast<uint8_t>(0x80 | ((wc1 & 0x0FC0) >> 6));
1872*0b57cec5SDimitry Andric             *to_nxt++ = static_cast<uint8_t>(0x80 |  (wc1 & 0x003F));
1873*0b57cec5SDimitry Andric         }
1874*0b57cec5SDimitry Andric     }
1875*0b57cec5SDimitry Andric     return codecvt_base::ok;
1876*0b57cec5SDimitry Andric }
1877*0b57cec5SDimitry Andric 
1878*0b57cec5SDimitry Andric static
1879*0b57cec5SDimitry Andric codecvt_base::result
1880*0b57cec5SDimitry Andric utf8_to_utf16(const uint8_t* frm, const uint8_t* frm_end, const uint8_t*& frm_nxt,
1881*0b57cec5SDimitry Andric               uint16_t* to, uint16_t* to_end, uint16_t*& to_nxt,
1882*0b57cec5SDimitry Andric               unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0))
1883*0b57cec5SDimitry Andric {
1884*0b57cec5SDimitry Andric     frm_nxt = frm;
1885*0b57cec5SDimitry Andric     to_nxt = to;
1886*0b57cec5SDimitry Andric     if (mode & consume_header)
1887*0b57cec5SDimitry Andric     {
1888*0b57cec5SDimitry Andric         if (frm_end-frm_nxt >= 3 && frm_nxt[0] == 0xEF && frm_nxt[1] == 0xBB &&
1889*0b57cec5SDimitry Andric                                                           frm_nxt[2] == 0xBF)
1890*0b57cec5SDimitry Andric             frm_nxt += 3;
1891*0b57cec5SDimitry Andric     }
1892*0b57cec5SDimitry Andric     for (; frm_nxt < frm_end && to_nxt < to_end; ++to_nxt)
1893*0b57cec5SDimitry Andric     {
1894*0b57cec5SDimitry Andric         uint8_t c1 = *frm_nxt;
1895*0b57cec5SDimitry Andric         if (c1 > Maxcode)
1896*0b57cec5SDimitry Andric             return codecvt_base::error;
1897*0b57cec5SDimitry Andric         if (c1 < 0x80)
1898*0b57cec5SDimitry Andric         {
1899*0b57cec5SDimitry Andric             *to_nxt = static_cast<uint16_t>(c1);
1900*0b57cec5SDimitry Andric             ++frm_nxt;
1901*0b57cec5SDimitry Andric         }
1902*0b57cec5SDimitry Andric         else if (c1 < 0xC2)
1903*0b57cec5SDimitry Andric         {
1904*0b57cec5SDimitry Andric             return codecvt_base::error;
1905*0b57cec5SDimitry Andric         }
1906*0b57cec5SDimitry Andric         else if (c1 < 0xE0)
1907*0b57cec5SDimitry Andric         {
1908*0b57cec5SDimitry Andric             if (frm_end-frm_nxt < 2)
1909*0b57cec5SDimitry Andric                 return codecvt_base::partial;
1910*0b57cec5SDimitry Andric             uint8_t c2 = frm_nxt[1];
1911*0b57cec5SDimitry Andric             if ((c2 & 0xC0) != 0x80)
1912*0b57cec5SDimitry Andric                 return codecvt_base::error;
1913*0b57cec5SDimitry Andric             uint16_t t = static_cast<uint16_t>(((c1 & 0x1F) << 6) | (c2 & 0x3F));
1914*0b57cec5SDimitry Andric             if (t > Maxcode)
1915*0b57cec5SDimitry Andric                 return codecvt_base::error;
1916*0b57cec5SDimitry Andric             *to_nxt = t;
1917*0b57cec5SDimitry Andric             frm_nxt += 2;
1918*0b57cec5SDimitry Andric         }
1919*0b57cec5SDimitry Andric         else if (c1 < 0xF0)
1920*0b57cec5SDimitry Andric         {
1921*0b57cec5SDimitry Andric             if (frm_end-frm_nxt < 3)
1922*0b57cec5SDimitry Andric                 return codecvt_base::partial;
1923*0b57cec5SDimitry Andric             uint8_t c2 = frm_nxt[1];
1924*0b57cec5SDimitry Andric             uint8_t c3 = frm_nxt[2];
1925*0b57cec5SDimitry Andric             switch (c1)
1926*0b57cec5SDimitry Andric             {
1927*0b57cec5SDimitry Andric             case 0xE0:
1928*0b57cec5SDimitry Andric                 if ((c2 & 0xE0) != 0xA0)
1929*0b57cec5SDimitry Andric                     return codecvt_base::error;
1930*0b57cec5SDimitry Andric                  break;
1931*0b57cec5SDimitry Andric             case 0xED:
1932*0b57cec5SDimitry Andric                 if ((c2 & 0xE0) != 0x80)
1933*0b57cec5SDimitry Andric                     return codecvt_base::error;
1934*0b57cec5SDimitry Andric                  break;
1935*0b57cec5SDimitry Andric             default:
1936*0b57cec5SDimitry Andric                 if ((c2 & 0xC0) != 0x80)
1937*0b57cec5SDimitry Andric                     return codecvt_base::error;
1938*0b57cec5SDimitry Andric                  break;
1939*0b57cec5SDimitry Andric             }
1940*0b57cec5SDimitry Andric             if ((c3 & 0xC0) != 0x80)
1941*0b57cec5SDimitry Andric                 return codecvt_base::error;
1942*0b57cec5SDimitry Andric             uint16_t t = static_cast<uint16_t>(((c1 & 0x0F) << 12)
1943*0b57cec5SDimitry Andric                                              | ((c2 & 0x3F) << 6)
1944*0b57cec5SDimitry Andric                                              |  (c3 & 0x3F));
1945*0b57cec5SDimitry Andric             if (t > Maxcode)
1946*0b57cec5SDimitry Andric                 return codecvt_base::error;
1947*0b57cec5SDimitry Andric             *to_nxt = t;
1948*0b57cec5SDimitry Andric             frm_nxt += 3;
1949*0b57cec5SDimitry Andric         }
1950*0b57cec5SDimitry Andric         else if (c1 < 0xF5)
1951*0b57cec5SDimitry Andric         {
1952*0b57cec5SDimitry Andric             if (frm_end-frm_nxt < 4)
1953*0b57cec5SDimitry Andric                 return codecvt_base::partial;
1954*0b57cec5SDimitry Andric             uint8_t c2 = frm_nxt[1];
1955*0b57cec5SDimitry Andric             uint8_t c3 = frm_nxt[2];
1956*0b57cec5SDimitry Andric             uint8_t c4 = frm_nxt[3];
1957*0b57cec5SDimitry Andric             switch (c1)
1958*0b57cec5SDimitry Andric             {
1959*0b57cec5SDimitry Andric             case 0xF0:
1960*0b57cec5SDimitry Andric                 if (!(0x90 <= c2 && c2 <= 0xBF))
1961*0b57cec5SDimitry Andric                     return codecvt_base::error;
1962*0b57cec5SDimitry Andric                  break;
1963*0b57cec5SDimitry Andric             case 0xF4:
1964*0b57cec5SDimitry Andric                 if ((c2 & 0xF0) != 0x80)
1965*0b57cec5SDimitry Andric                     return codecvt_base::error;
1966*0b57cec5SDimitry Andric                  break;
1967*0b57cec5SDimitry Andric             default:
1968*0b57cec5SDimitry Andric                 if ((c2 & 0xC0) != 0x80)
1969*0b57cec5SDimitry Andric                     return codecvt_base::error;
1970*0b57cec5SDimitry Andric                  break;
1971*0b57cec5SDimitry Andric             }
1972*0b57cec5SDimitry Andric             if ((c3 & 0xC0) != 0x80 || (c4 & 0xC0) != 0x80)
1973*0b57cec5SDimitry Andric                 return codecvt_base::error;
1974*0b57cec5SDimitry Andric             if (to_end-to_nxt < 2)
1975*0b57cec5SDimitry Andric                 return codecvt_base::partial;
1976*0b57cec5SDimitry Andric             if ((((c1 & 7UL) << 18) +
1977*0b57cec5SDimitry Andric                 ((c2 & 0x3FUL) << 12) +
1978*0b57cec5SDimitry Andric                 ((c3 & 0x3FUL) << 6) + (c4 & 0x3F)) > Maxcode)
1979*0b57cec5SDimitry Andric                 return codecvt_base::error;
1980*0b57cec5SDimitry Andric             *to_nxt = static_cast<uint16_t>(
1981*0b57cec5SDimitry Andric                     0xD800
1982*0b57cec5SDimitry Andric                   | (((((c1 & 0x07) << 2) | ((c2 & 0x30) >> 4)) - 1) << 6)
1983*0b57cec5SDimitry Andric                   | ((c2 & 0x0F) << 2)
1984*0b57cec5SDimitry Andric                   | ((c3 & 0x30) >> 4));
1985*0b57cec5SDimitry Andric             *++to_nxt = static_cast<uint16_t>(
1986*0b57cec5SDimitry Andric                     0xDC00
1987*0b57cec5SDimitry Andric                   | ((c3 & 0x0F) << 6)
1988*0b57cec5SDimitry Andric                   |  (c4 & 0x3F));
1989*0b57cec5SDimitry Andric             frm_nxt += 4;
1990*0b57cec5SDimitry Andric         }
1991*0b57cec5SDimitry Andric         else
1992*0b57cec5SDimitry Andric         {
1993*0b57cec5SDimitry Andric             return codecvt_base::error;
1994*0b57cec5SDimitry Andric         }
1995*0b57cec5SDimitry Andric     }
1996*0b57cec5SDimitry Andric     return frm_nxt < frm_end ? codecvt_base::partial : codecvt_base::ok;
1997*0b57cec5SDimitry Andric }
1998*0b57cec5SDimitry Andric 
1999*0b57cec5SDimitry Andric static
2000*0b57cec5SDimitry Andric codecvt_base::result
2001*0b57cec5SDimitry Andric utf8_to_utf16(const uint8_t* frm, const uint8_t* frm_end, const uint8_t*& frm_nxt,
2002*0b57cec5SDimitry Andric               uint32_t* to, uint32_t* to_end, uint32_t*& to_nxt,
2003*0b57cec5SDimitry Andric               unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0))
2004*0b57cec5SDimitry Andric {
2005*0b57cec5SDimitry Andric     frm_nxt = frm;
2006*0b57cec5SDimitry Andric     to_nxt = to;
2007*0b57cec5SDimitry Andric     if (mode & consume_header)
2008*0b57cec5SDimitry Andric     {
2009*0b57cec5SDimitry Andric         if (frm_end-frm_nxt >= 3 && frm_nxt[0] == 0xEF && frm_nxt[1] == 0xBB &&
2010*0b57cec5SDimitry Andric                                                           frm_nxt[2] == 0xBF)
2011*0b57cec5SDimitry Andric             frm_nxt += 3;
2012*0b57cec5SDimitry Andric     }
2013*0b57cec5SDimitry Andric     for (; frm_nxt < frm_end && to_nxt < to_end; ++to_nxt)
2014*0b57cec5SDimitry Andric     {
2015*0b57cec5SDimitry Andric         uint8_t c1 = *frm_nxt;
2016*0b57cec5SDimitry Andric         if (c1 > Maxcode)
2017*0b57cec5SDimitry Andric             return codecvt_base::error;
2018*0b57cec5SDimitry Andric         if (c1 < 0x80)
2019*0b57cec5SDimitry Andric         {
2020*0b57cec5SDimitry Andric             *to_nxt = static_cast<uint32_t>(c1);
2021*0b57cec5SDimitry Andric             ++frm_nxt;
2022*0b57cec5SDimitry Andric         }
2023*0b57cec5SDimitry Andric         else if (c1 < 0xC2)
2024*0b57cec5SDimitry Andric         {
2025*0b57cec5SDimitry Andric             return codecvt_base::error;
2026*0b57cec5SDimitry Andric         }
2027*0b57cec5SDimitry Andric         else if (c1 < 0xE0)
2028*0b57cec5SDimitry Andric         {
2029*0b57cec5SDimitry Andric             if (frm_end-frm_nxt < 2)
2030*0b57cec5SDimitry Andric                 return codecvt_base::partial;
2031*0b57cec5SDimitry Andric             uint8_t c2 = frm_nxt[1];
2032*0b57cec5SDimitry Andric             if ((c2 & 0xC0) != 0x80)
2033*0b57cec5SDimitry Andric                 return codecvt_base::error;
2034*0b57cec5SDimitry Andric             uint16_t t = static_cast<uint16_t>(((c1 & 0x1F) << 6) | (c2 & 0x3F));
2035*0b57cec5SDimitry Andric             if (t > Maxcode)
2036*0b57cec5SDimitry Andric                 return codecvt_base::error;
2037*0b57cec5SDimitry Andric             *to_nxt = static_cast<uint32_t>(t);
2038*0b57cec5SDimitry Andric             frm_nxt += 2;
2039*0b57cec5SDimitry Andric         }
2040*0b57cec5SDimitry Andric         else if (c1 < 0xF0)
2041*0b57cec5SDimitry Andric         {
2042*0b57cec5SDimitry Andric             if (frm_end-frm_nxt < 3)
2043*0b57cec5SDimitry Andric                 return codecvt_base::partial;
2044*0b57cec5SDimitry Andric             uint8_t c2 = frm_nxt[1];
2045*0b57cec5SDimitry Andric             uint8_t c3 = frm_nxt[2];
2046*0b57cec5SDimitry Andric             switch (c1)
2047*0b57cec5SDimitry Andric             {
2048*0b57cec5SDimitry Andric             case 0xE0:
2049*0b57cec5SDimitry Andric                 if ((c2 & 0xE0) != 0xA0)
2050*0b57cec5SDimitry Andric                     return codecvt_base::error;
2051*0b57cec5SDimitry Andric                  break;
2052*0b57cec5SDimitry Andric             case 0xED:
2053*0b57cec5SDimitry Andric                 if ((c2 & 0xE0) != 0x80)
2054*0b57cec5SDimitry Andric                     return codecvt_base::error;
2055*0b57cec5SDimitry Andric                  break;
2056*0b57cec5SDimitry Andric             default:
2057*0b57cec5SDimitry Andric                 if ((c2 & 0xC0) != 0x80)
2058*0b57cec5SDimitry Andric                     return codecvt_base::error;
2059*0b57cec5SDimitry Andric                  break;
2060*0b57cec5SDimitry Andric             }
2061*0b57cec5SDimitry Andric             if ((c3 & 0xC0) != 0x80)
2062*0b57cec5SDimitry Andric                 return codecvt_base::error;
2063*0b57cec5SDimitry Andric             uint16_t t = static_cast<uint16_t>(((c1 & 0x0F) << 12)
2064*0b57cec5SDimitry Andric                                              | ((c2 & 0x3F) << 6)
2065*0b57cec5SDimitry Andric                                              |  (c3 & 0x3F));
2066*0b57cec5SDimitry Andric             if (t > Maxcode)
2067*0b57cec5SDimitry Andric                 return codecvt_base::error;
2068*0b57cec5SDimitry Andric             *to_nxt = static_cast<uint32_t>(t);
2069*0b57cec5SDimitry Andric             frm_nxt += 3;
2070*0b57cec5SDimitry Andric         }
2071*0b57cec5SDimitry Andric         else if (c1 < 0xF5)
2072*0b57cec5SDimitry Andric         {
2073*0b57cec5SDimitry Andric             if (frm_end-frm_nxt < 4)
2074*0b57cec5SDimitry Andric                 return codecvt_base::partial;
2075*0b57cec5SDimitry Andric             uint8_t c2 = frm_nxt[1];
2076*0b57cec5SDimitry Andric             uint8_t c3 = frm_nxt[2];
2077*0b57cec5SDimitry Andric             uint8_t c4 = frm_nxt[3];
2078*0b57cec5SDimitry Andric             switch (c1)
2079*0b57cec5SDimitry Andric             {
2080*0b57cec5SDimitry Andric             case 0xF0:
2081*0b57cec5SDimitry Andric                 if (!(0x90 <= c2 && c2 <= 0xBF))
2082*0b57cec5SDimitry Andric                     return codecvt_base::error;
2083*0b57cec5SDimitry Andric                  break;
2084*0b57cec5SDimitry Andric             case 0xF4:
2085*0b57cec5SDimitry Andric                 if ((c2 & 0xF0) != 0x80)
2086*0b57cec5SDimitry Andric                     return codecvt_base::error;
2087*0b57cec5SDimitry Andric                  break;
2088*0b57cec5SDimitry Andric             default:
2089*0b57cec5SDimitry Andric                 if ((c2 & 0xC0) != 0x80)
2090*0b57cec5SDimitry Andric                     return codecvt_base::error;
2091*0b57cec5SDimitry Andric                  break;
2092*0b57cec5SDimitry Andric             }
2093*0b57cec5SDimitry Andric             if ((c3 & 0xC0) != 0x80 || (c4 & 0xC0) != 0x80)
2094*0b57cec5SDimitry Andric                 return codecvt_base::error;
2095*0b57cec5SDimitry Andric             if (to_end-to_nxt < 2)
2096*0b57cec5SDimitry Andric                 return codecvt_base::partial;
2097*0b57cec5SDimitry Andric             if ((((c1 & 7UL) << 18) +
2098*0b57cec5SDimitry Andric                 ((c2 & 0x3FUL) << 12) +
2099*0b57cec5SDimitry Andric                 ((c3 & 0x3FUL) << 6) + (c4 & 0x3F)) > Maxcode)
2100*0b57cec5SDimitry Andric                 return codecvt_base::error;
2101*0b57cec5SDimitry Andric             *to_nxt = static_cast<uint32_t>(
2102*0b57cec5SDimitry Andric                     0xD800
2103*0b57cec5SDimitry Andric                   | (((((c1 & 0x07) << 2) | ((c2 & 0x30) >> 4)) - 1) << 6)
2104*0b57cec5SDimitry Andric                   | ((c2 & 0x0F) << 2)
2105*0b57cec5SDimitry Andric                   | ((c3 & 0x30) >> 4));
2106*0b57cec5SDimitry Andric             *++to_nxt = static_cast<uint32_t>(
2107*0b57cec5SDimitry Andric                     0xDC00
2108*0b57cec5SDimitry Andric                   | ((c3 & 0x0F) << 6)
2109*0b57cec5SDimitry Andric                   |  (c4 & 0x3F));
2110*0b57cec5SDimitry Andric             frm_nxt += 4;
2111*0b57cec5SDimitry Andric         }
2112*0b57cec5SDimitry Andric         else
2113*0b57cec5SDimitry Andric         {
2114*0b57cec5SDimitry Andric             return codecvt_base::error;
2115*0b57cec5SDimitry Andric         }
2116*0b57cec5SDimitry Andric     }
2117*0b57cec5SDimitry Andric     return frm_nxt < frm_end ? codecvt_base::partial : codecvt_base::ok;
2118*0b57cec5SDimitry Andric }
2119*0b57cec5SDimitry Andric 
2120*0b57cec5SDimitry Andric static
2121*0b57cec5SDimitry Andric int
2122*0b57cec5SDimitry Andric utf8_to_utf16_length(const uint8_t* frm, const uint8_t* frm_end,
2123*0b57cec5SDimitry Andric                      size_t mx, unsigned long Maxcode = 0x10FFFF,
2124*0b57cec5SDimitry Andric                      codecvt_mode mode = codecvt_mode(0))
2125*0b57cec5SDimitry Andric {
2126*0b57cec5SDimitry Andric     const uint8_t* frm_nxt = frm;
2127*0b57cec5SDimitry Andric     if (mode & consume_header)
2128*0b57cec5SDimitry Andric     {
2129*0b57cec5SDimitry Andric         if (frm_end-frm_nxt >= 3 && frm_nxt[0] == 0xEF && frm_nxt[1] == 0xBB &&
2130*0b57cec5SDimitry Andric                                                           frm_nxt[2] == 0xBF)
2131*0b57cec5SDimitry Andric             frm_nxt += 3;
2132*0b57cec5SDimitry Andric     }
2133*0b57cec5SDimitry Andric     for (size_t nchar16_t = 0; frm_nxt < frm_end && nchar16_t < mx; ++nchar16_t)
2134*0b57cec5SDimitry Andric     {
2135*0b57cec5SDimitry Andric         uint8_t c1 = *frm_nxt;
2136*0b57cec5SDimitry Andric         if (c1 > Maxcode)
2137*0b57cec5SDimitry Andric             break;
2138*0b57cec5SDimitry Andric         if (c1 < 0x80)
2139*0b57cec5SDimitry Andric         {
2140*0b57cec5SDimitry Andric             ++frm_nxt;
2141*0b57cec5SDimitry Andric         }
2142*0b57cec5SDimitry Andric         else if (c1 < 0xC2)
2143*0b57cec5SDimitry Andric         {
2144*0b57cec5SDimitry Andric             break;
2145*0b57cec5SDimitry Andric         }
2146*0b57cec5SDimitry Andric         else if (c1 < 0xE0)
2147*0b57cec5SDimitry Andric         {
2148*0b57cec5SDimitry Andric             if ((frm_end-frm_nxt < 2) || (frm_nxt[1] & 0xC0) != 0x80)
2149*0b57cec5SDimitry Andric                 break;
2150*0b57cec5SDimitry Andric             uint16_t t = static_cast<uint16_t>(((c1 & 0x1F) << 6) | (frm_nxt[1] & 0x3F));
2151*0b57cec5SDimitry Andric             if (t > Maxcode)
2152*0b57cec5SDimitry Andric                 break;
2153*0b57cec5SDimitry Andric             frm_nxt += 2;
2154*0b57cec5SDimitry Andric         }
2155*0b57cec5SDimitry Andric         else if (c1 < 0xF0)
2156*0b57cec5SDimitry Andric         {
2157*0b57cec5SDimitry Andric             if (frm_end-frm_nxt < 3)
2158*0b57cec5SDimitry Andric                 break;
2159*0b57cec5SDimitry Andric             uint8_t c2 = frm_nxt[1];
2160*0b57cec5SDimitry Andric             uint8_t c3 = frm_nxt[2];
2161*0b57cec5SDimitry Andric             switch (c1)
2162*0b57cec5SDimitry Andric             {
2163*0b57cec5SDimitry Andric             case 0xE0:
2164*0b57cec5SDimitry Andric                 if ((c2 & 0xE0) != 0xA0)
2165*0b57cec5SDimitry Andric                     return static_cast<int>(frm_nxt - frm);
2166*0b57cec5SDimitry Andric                 break;
2167*0b57cec5SDimitry Andric             case 0xED:
2168*0b57cec5SDimitry Andric                 if ((c2 & 0xE0) != 0x80)
2169*0b57cec5SDimitry Andric                     return static_cast<int>(frm_nxt - frm);
2170*0b57cec5SDimitry Andric                  break;
2171*0b57cec5SDimitry Andric             default:
2172*0b57cec5SDimitry Andric                 if ((c2 & 0xC0) != 0x80)
2173*0b57cec5SDimitry Andric                     return static_cast<int>(frm_nxt - frm);
2174*0b57cec5SDimitry Andric                  break;
2175*0b57cec5SDimitry Andric             }
2176*0b57cec5SDimitry Andric             if ((c3 & 0xC0) != 0x80)
2177*0b57cec5SDimitry Andric                 break;
2178*0b57cec5SDimitry Andric             if ((((c1 & 0x0Fu) << 12) | ((c2 & 0x3Fu) << 6) | (c3 & 0x3Fu)) > Maxcode)
2179*0b57cec5SDimitry Andric                 break;
2180*0b57cec5SDimitry Andric             frm_nxt += 3;
2181*0b57cec5SDimitry Andric         }
2182*0b57cec5SDimitry Andric         else if (c1 < 0xF5)
2183*0b57cec5SDimitry Andric         {
2184*0b57cec5SDimitry Andric             if (frm_end-frm_nxt < 4 || mx-nchar16_t < 2)
2185*0b57cec5SDimitry Andric                 break;
2186*0b57cec5SDimitry Andric             uint8_t c2 = frm_nxt[1];
2187*0b57cec5SDimitry Andric             uint8_t c3 = frm_nxt[2];
2188*0b57cec5SDimitry Andric             uint8_t c4 = frm_nxt[3];
2189*0b57cec5SDimitry Andric             switch (c1)
2190*0b57cec5SDimitry Andric             {
2191*0b57cec5SDimitry Andric             case 0xF0:
2192*0b57cec5SDimitry Andric                 if (!(0x90 <= c2 && c2 <= 0xBF))
2193*0b57cec5SDimitry Andric                     return static_cast<int>(frm_nxt - frm);
2194*0b57cec5SDimitry Andric                  break;
2195*0b57cec5SDimitry Andric             case 0xF4:
2196*0b57cec5SDimitry Andric                 if ((c2 & 0xF0) != 0x80)
2197*0b57cec5SDimitry Andric                     return static_cast<int>(frm_nxt - frm);
2198*0b57cec5SDimitry Andric                  break;
2199*0b57cec5SDimitry Andric             default:
2200*0b57cec5SDimitry Andric                 if ((c2 & 0xC0) != 0x80)
2201*0b57cec5SDimitry Andric                     return static_cast<int>(frm_nxt - frm);
2202*0b57cec5SDimitry Andric                  break;
2203*0b57cec5SDimitry Andric             }
2204*0b57cec5SDimitry Andric             if ((c3 & 0xC0) != 0x80 || (c4 & 0xC0) != 0x80)
2205*0b57cec5SDimitry Andric                 break;
2206*0b57cec5SDimitry Andric             if ((((c1 & 7UL) << 18) +
2207*0b57cec5SDimitry Andric                 ((c2 & 0x3FUL) << 12) +
2208*0b57cec5SDimitry Andric                 ((c3 & 0x3FUL) << 6) + (c4 & 0x3F)) > Maxcode)
2209*0b57cec5SDimitry Andric                 break;
2210*0b57cec5SDimitry Andric             ++nchar16_t;
2211*0b57cec5SDimitry Andric             frm_nxt += 4;
2212*0b57cec5SDimitry Andric         }
2213*0b57cec5SDimitry Andric         else
2214*0b57cec5SDimitry Andric         {
2215*0b57cec5SDimitry Andric             break;
2216*0b57cec5SDimitry Andric         }
2217*0b57cec5SDimitry Andric     }
2218*0b57cec5SDimitry Andric     return static_cast<int>(frm_nxt - frm);
2219*0b57cec5SDimitry Andric }
2220*0b57cec5SDimitry Andric 
2221*0b57cec5SDimitry Andric static
2222*0b57cec5SDimitry Andric codecvt_base::result
2223*0b57cec5SDimitry Andric ucs4_to_utf8(const uint32_t* frm, const uint32_t* frm_end, const uint32_t*& frm_nxt,
2224*0b57cec5SDimitry Andric              uint8_t* to, uint8_t* to_end, uint8_t*& to_nxt,
2225*0b57cec5SDimitry Andric              unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0))
2226*0b57cec5SDimitry Andric {
2227*0b57cec5SDimitry Andric     frm_nxt = frm;
2228*0b57cec5SDimitry Andric     to_nxt = to;
2229*0b57cec5SDimitry Andric     if (mode & generate_header)
2230*0b57cec5SDimitry Andric     {
2231*0b57cec5SDimitry Andric         if (to_end-to_nxt < 3)
2232*0b57cec5SDimitry Andric             return codecvt_base::partial;
2233*0b57cec5SDimitry Andric         *to_nxt++ = static_cast<uint8_t>(0xEF);
2234*0b57cec5SDimitry Andric         *to_nxt++ = static_cast<uint8_t>(0xBB);
2235*0b57cec5SDimitry Andric         *to_nxt++ = static_cast<uint8_t>(0xBF);
2236*0b57cec5SDimitry Andric     }
2237*0b57cec5SDimitry Andric     for (; frm_nxt < frm_end; ++frm_nxt)
2238*0b57cec5SDimitry Andric     {
2239*0b57cec5SDimitry Andric         uint32_t wc = *frm_nxt;
2240*0b57cec5SDimitry Andric         if ((wc & 0xFFFFF800) == 0x00D800 || wc > Maxcode)
2241*0b57cec5SDimitry Andric             return codecvt_base::error;
2242*0b57cec5SDimitry Andric         if (wc < 0x000080)
2243*0b57cec5SDimitry Andric         {
2244*0b57cec5SDimitry Andric             if (to_end-to_nxt < 1)
2245*0b57cec5SDimitry Andric                 return codecvt_base::partial;
2246*0b57cec5SDimitry Andric             *to_nxt++ = static_cast<uint8_t>(wc);
2247*0b57cec5SDimitry Andric         }
2248*0b57cec5SDimitry Andric         else if (wc < 0x000800)
2249*0b57cec5SDimitry Andric         {
2250*0b57cec5SDimitry Andric             if (to_end-to_nxt < 2)
2251*0b57cec5SDimitry Andric                 return codecvt_base::partial;
2252*0b57cec5SDimitry Andric             *to_nxt++ = static_cast<uint8_t>(0xC0 | (wc >> 6));
2253*0b57cec5SDimitry Andric             *to_nxt++ = static_cast<uint8_t>(0x80 | (wc & 0x03F));
2254*0b57cec5SDimitry Andric         }
2255*0b57cec5SDimitry Andric         else if (wc < 0x010000)
2256*0b57cec5SDimitry Andric         {
2257*0b57cec5SDimitry Andric             if (to_end-to_nxt < 3)
2258*0b57cec5SDimitry Andric                 return codecvt_base::partial;
2259*0b57cec5SDimitry Andric             *to_nxt++ = static_cast<uint8_t>(0xE0 |  (wc >> 12));
2260*0b57cec5SDimitry Andric             *to_nxt++ = static_cast<uint8_t>(0x80 | ((wc & 0x0FC0) >> 6));
2261*0b57cec5SDimitry Andric             *to_nxt++ = static_cast<uint8_t>(0x80 |  (wc & 0x003F));
2262*0b57cec5SDimitry Andric         }
2263*0b57cec5SDimitry Andric         else // if (wc < 0x110000)
2264*0b57cec5SDimitry Andric         {
2265*0b57cec5SDimitry Andric             if (to_end-to_nxt < 4)
2266*0b57cec5SDimitry Andric                 return codecvt_base::partial;
2267*0b57cec5SDimitry Andric             *to_nxt++ = static_cast<uint8_t>(0xF0 |  (wc >> 18));
2268*0b57cec5SDimitry Andric             *to_nxt++ = static_cast<uint8_t>(0x80 | ((wc & 0x03F000) >> 12));
2269*0b57cec5SDimitry Andric             *to_nxt++ = static_cast<uint8_t>(0x80 | ((wc & 0x000FC0) >> 6));
2270*0b57cec5SDimitry Andric             *to_nxt++ = static_cast<uint8_t>(0x80 |  (wc & 0x00003F));
2271*0b57cec5SDimitry Andric         }
2272*0b57cec5SDimitry Andric     }
2273*0b57cec5SDimitry Andric     return codecvt_base::ok;
2274*0b57cec5SDimitry Andric }
2275*0b57cec5SDimitry Andric 
2276*0b57cec5SDimitry Andric static
2277*0b57cec5SDimitry Andric codecvt_base::result
2278*0b57cec5SDimitry Andric utf8_to_ucs4(const uint8_t* frm, const uint8_t* frm_end, const uint8_t*& frm_nxt,
2279*0b57cec5SDimitry Andric              uint32_t* to, uint32_t* to_end, uint32_t*& to_nxt,
2280*0b57cec5SDimitry Andric              unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0))
2281*0b57cec5SDimitry Andric {
2282*0b57cec5SDimitry Andric     frm_nxt = frm;
2283*0b57cec5SDimitry Andric     to_nxt = to;
2284*0b57cec5SDimitry Andric     if (mode & consume_header)
2285*0b57cec5SDimitry Andric     {
2286*0b57cec5SDimitry Andric         if (frm_end-frm_nxt >= 3 && frm_nxt[0] == 0xEF && frm_nxt[1] == 0xBB &&
2287*0b57cec5SDimitry Andric                                                           frm_nxt[2] == 0xBF)
2288*0b57cec5SDimitry Andric             frm_nxt += 3;
2289*0b57cec5SDimitry Andric     }
2290*0b57cec5SDimitry Andric     for (; frm_nxt < frm_end && to_nxt < to_end; ++to_nxt)
2291*0b57cec5SDimitry Andric     {
2292*0b57cec5SDimitry Andric         uint8_t c1 = static_cast<uint8_t>(*frm_nxt);
2293*0b57cec5SDimitry Andric         if (c1 < 0x80)
2294*0b57cec5SDimitry Andric         {
2295*0b57cec5SDimitry Andric             if (c1 > Maxcode)
2296*0b57cec5SDimitry Andric                 return codecvt_base::error;
2297*0b57cec5SDimitry Andric             *to_nxt = static_cast<uint32_t>(c1);
2298*0b57cec5SDimitry Andric             ++frm_nxt;
2299*0b57cec5SDimitry Andric         }
2300*0b57cec5SDimitry Andric         else if (c1 < 0xC2)
2301*0b57cec5SDimitry Andric         {
2302*0b57cec5SDimitry Andric             return codecvt_base::error;
2303*0b57cec5SDimitry Andric         }
2304*0b57cec5SDimitry Andric         else if (c1 < 0xE0)
2305*0b57cec5SDimitry Andric         {
2306*0b57cec5SDimitry Andric             if (frm_end-frm_nxt < 2)
2307*0b57cec5SDimitry Andric                 return codecvt_base::partial;
2308*0b57cec5SDimitry Andric             uint8_t c2 = frm_nxt[1];
2309*0b57cec5SDimitry Andric             if ((c2 & 0xC0) != 0x80)
2310*0b57cec5SDimitry Andric                 return codecvt_base::error;
2311*0b57cec5SDimitry Andric             uint32_t t = static_cast<uint32_t>(((c1 & 0x1F) << 6)
2312*0b57cec5SDimitry Andric                                               | (c2 & 0x3F));
2313*0b57cec5SDimitry Andric             if (t > Maxcode)
2314*0b57cec5SDimitry Andric                 return codecvt_base::error;
2315*0b57cec5SDimitry Andric             *to_nxt = t;
2316*0b57cec5SDimitry Andric             frm_nxt += 2;
2317*0b57cec5SDimitry Andric         }
2318*0b57cec5SDimitry Andric         else if (c1 < 0xF0)
2319*0b57cec5SDimitry Andric         {
2320*0b57cec5SDimitry Andric             if (frm_end-frm_nxt < 3)
2321*0b57cec5SDimitry Andric                 return codecvt_base::partial;
2322*0b57cec5SDimitry Andric             uint8_t c2 = frm_nxt[1];
2323*0b57cec5SDimitry Andric             uint8_t c3 = frm_nxt[2];
2324*0b57cec5SDimitry Andric             switch (c1)
2325*0b57cec5SDimitry Andric             {
2326*0b57cec5SDimitry Andric             case 0xE0:
2327*0b57cec5SDimitry Andric                 if ((c2 & 0xE0) != 0xA0)
2328*0b57cec5SDimitry Andric                     return codecvt_base::error;
2329*0b57cec5SDimitry Andric                  break;
2330*0b57cec5SDimitry Andric             case 0xED:
2331*0b57cec5SDimitry Andric                 if ((c2 & 0xE0) != 0x80)
2332*0b57cec5SDimitry Andric                     return codecvt_base::error;
2333*0b57cec5SDimitry Andric                  break;
2334*0b57cec5SDimitry Andric             default:
2335*0b57cec5SDimitry Andric                 if ((c2 & 0xC0) != 0x80)
2336*0b57cec5SDimitry Andric                     return codecvt_base::error;
2337*0b57cec5SDimitry Andric                  break;
2338*0b57cec5SDimitry Andric             }
2339*0b57cec5SDimitry Andric             if ((c3 & 0xC0) != 0x80)
2340*0b57cec5SDimitry Andric                 return codecvt_base::error;
2341*0b57cec5SDimitry Andric             uint32_t t = static_cast<uint32_t>(((c1 & 0x0F) << 12)
2342*0b57cec5SDimitry Andric                                              | ((c2 & 0x3F) << 6)
2343*0b57cec5SDimitry Andric                                              |  (c3 & 0x3F));
2344*0b57cec5SDimitry Andric             if (t > Maxcode)
2345*0b57cec5SDimitry Andric                 return codecvt_base::error;
2346*0b57cec5SDimitry Andric             *to_nxt = t;
2347*0b57cec5SDimitry Andric             frm_nxt += 3;
2348*0b57cec5SDimitry Andric         }
2349*0b57cec5SDimitry Andric         else if (c1 < 0xF5)
2350*0b57cec5SDimitry Andric         {
2351*0b57cec5SDimitry Andric             if (frm_end-frm_nxt < 4)
2352*0b57cec5SDimitry Andric                 return codecvt_base::partial;
2353*0b57cec5SDimitry Andric             uint8_t c2 = frm_nxt[1];
2354*0b57cec5SDimitry Andric             uint8_t c3 = frm_nxt[2];
2355*0b57cec5SDimitry Andric             uint8_t c4 = frm_nxt[3];
2356*0b57cec5SDimitry Andric             switch (c1)
2357*0b57cec5SDimitry Andric             {
2358*0b57cec5SDimitry Andric             case 0xF0:
2359*0b57cec5SDimitry Andric                 if (!(0x90 <= c2 && c2 <= 0xBF))
2360*0b57cec5SDimitry Andric                     return codecvt_base::error;
2361*0b57cec5SDimitry Andric                  break;
2362*0b57cec5SDimitry Andric             case 0xF4:
2363*0b57cec5SDimitry Andric                 if ((c2 & 0xF0) != 0x80)
2364*0b57cec5SDimitry Andric                     return codecvt_base::error;
2365*0b57cec5SDimitry Andric                  break;
2366*0b57cec5SDimitry Andric             default:
2367*0b57cec5SDimitry Andric                 if ((c2 & 0xC0) != 0x80)
2368*0b57cec5SDimitry Andric                     return codecvt_base::error;
2369*0b57cec5SDimitry Andric                  break;
2370*0b57cec5SDimitry Andric             }
2371*0b57cec5SDimitry Andric             if ((c3 & 0xC0) != 0x80 || (c4 & 0xC0) != 0x80)
2372*0b57cec5SDimitry Andric                 return codecvt_base::error;
2373*0b57cec5SDimitry Andric             uint32_t t = static_cast<uint32_t>(((c1 & 0x07) << 18)
2374*0b57cec5SDimitry Andric                                              | ((c2 & 0x3F) << 12)
2375*0b57cec5SDimitry Andric                                              | ((c3 & 0x3F) << 6)
2376*0b57cec5SDimitry Andric                                              |  (c4 & 0x3F));
2377*0b57cec5SDimitry Andric             if (t > Maxcode)
2378*0b57cec5SDimitry Andric                 return codecvt_base::error;
2379*0b57cec5SDimitry Andric             *to_nxt = t;
2380*0b57cec5SDimitry Andric             frm_nxt += 4;
2381*0b57cec5SDimitry Andric         }
2382*0b57cec5SDimitry Andric         else
2383*0b57cec5SDimitry Andric         {
2384*0b57cec5SDimitry Andric             return codecvt_base::error;
2385*0b57cec5SDimitry Andric         }
2386*0b57cec5SDimitry Andric     }
2387*0b57cec5SDimitry Andric     return frm_nxt < frm_end ? codecvt_base::partial : codecvt_base::ok;
2388*0b57cec5SDimitry Andric }
2389*0b57cec5SDimitry Andric 
2390*0b57cec5SDimitry Andric static
2391*0b57cec5SDimitry Andric int
2392*0b57cec5SDimitry Andric utf8_to_ucs4_length(const uint8_t* frm, const uint8_t* frm_end,
2393*0b57cec5SDimitry Andric                     size_t mx, unsigned long Maxcode = 0x10FFFF,
2394*0b57cec5SDimitry Andric                     codecvt_mode mode = codecvt_mode(0))
2395*0b57cec5SDimitry Andric {
2396*0b57cec5SDimitry Andric     const uint8_t* frm_nxt = frm;
2397*0b57cec5SDimitry Andric     if (mode & consume_header)
2398*0b57cec5SDimitry Andric     {
2399*0b57cec5SDimitry Andric         if (frm_end-frm_nxt >= 3 && frm_nxt[0] == 0xEF && frm_nxt[1] == 0xBB &&
2400*0b57cec5SDimitry Andric                                                           frm_nxt[2] == 0xBF)
2401*0b57cec5SDimitry Andric             frm_nxt += 3;
2402*0b57cec5SDimitry Andric     }
2403*0b57cec5SDimitry Andric     for (size_t nchar32_t = 0; frm_nxt < frm_end && nchar32_t < mx; ++nchar32_t)
2404*0b57cec5SDimitry Andric     {
2405*0b57cec5SDimitry Andric         uint8_t c1 = static_cast<uint8_t>(*frm_nxt);
2406*0b57cec5SDimitry Andric         if (c1 < 0x80)
2407*0b57cec5SDimitry Andric         {
2408*0b57cec5SDimitry Andric             if (c1 > Maxcode)
2409*0b57cec5SDimitry Andric                 break;
2410*0b57cec5SDimitry Andric             ++frm_nxt;
2411*0b57cec5SDimitry Andric         }
2412*0b57cec5SDimitry Andric         else if (c1 < 0xC2)
2413*0b57cec5SDimitry Andric         {
2414*0b57cec5SDimitry Andric             break;
2415*0b57cec5SDimitry Andric         }
2416*0b57cec5SDimitry Andric         else if (c1 < 0xE0)
2417*0b57cec5SDimitry Andric         {
2418*0b57cec5SDimitry Andric             if ((frm_end-frm_nxt < 2) || ((frm_nxt[1] & 0xC0) != 0x80))
2419*0b57cec5SDimitry Andric                 break;
2420*0b57cec5SDimitry Andric             if ((((c1 & 0x1Fu) << 6) | (frm_nxt[1] & 0x3Fu)) > Maxcode)
2421*0b57cec5SDimitry Andric                 break;
2422*0b57cec5SDimitry Andric             frm_nxt += 2;
2423*0b57cec5SDimitry Andric         }
2424*0b57cec5SDimitry Andric         else if (c1 < 0xF0)
2425*0b57cec5SDimitry Andric         {
2426*0b57cec5SDimitry Andric             if (frm_end-frm_nxt < 3)
2427*0b57cec5SDimitry Andric                 break;
2428*0b57cec5SDimitry Andric             uint8_t c2 = frm_nxt[1];
2429*0b57cec5SDimitry Andric             uint8_t c3 = frm_nxt[2];
2430*0b57cec5SDimitry Andric             switch (c1)
2431*0b57cec5SDimitry Andric             {
2432*0b57cec5SDimitry Andric             case 0xE0:
2433*0b57cec5SDimitry Andric                 if ((c2 & 0xE0) != 0xA0)
2434*0b57cec5SDimitry Andric                     return static_cast<int>(frm_nxt - frm);
2435*0b57cec5SDimitry Andric                 break;
2436*0b57cec5SDimitry Andric             case 0xED:
2437*0b57cec5SDimitry Andric                 if ((c2 & 0xE0) != 0x80)
2438*0b57cec5SDimitry Andric                     return static_cast<int>(frm_nxt - frm);
2439*0b57cec5SDimitry Andric                  break;
2440*0b57cec5SDimitry Andric             default:
2441*0b57cec5SDimitry Andric                 if ((c2 & 0xC0) != 0x80)
2442*0b57cec5SDimitry Andric                     return static_cast<int>(frm_nxt - frm);
2443*0b57cec5SDimitry Andric                  break;
2444*0b57cec5SDimitry Andric             }
2445*0b57cec5SDimitry Andric             if ((c3 & 0xC0) != 0x80)
2446*0b57cec5SDimitry Andric                 break;
2447*0b57cec5SDimitry Andric             if ((((c1 & 0x0Fu) << 12) | ((c2 & 0x3Fu) << 6) | (c3 & 0x3Fu)) > Maxcode)
2448*0b57cec5SDimitry Andric                 break;
2449*0b57cec5SDimitry Andric             frm_nxt += 3;
2450*0b57cec5SDimitry Andric         }
2451*0b57cec5SDimitry Andric         else if (c1 < 0xF5)
2452*0b57cec5SDimitry Andric         {
2453*0b57cec5SDimitry Andric             if (frm_end-frm_nxt < 4)
2454*0b57cec5SDimitry Andric                 break;
2455*0b57cec5SDimitry Andric             uint8_t c2 = frm_nxt[1];
2456*0b57cec5SDimitry Andric             uint8_t c3 = frm_nxt[2];
2457*0b57cec5SDimitry Andric             uint8_t c4 = frm_nxt[3];
2458*0b57cec5SDimitry Andric             switch (c1)
2459*0b57cec5SDimitry Andric             {
2460*0b57cec5SDimitry Andric             case 0xF0:
2461*0b57cec5SDimitry Andric                 if (!(0x90 <= c2 && c2 <= 0xBF))
2462*0b57cec5SDimitry Andric                     return static_cast<int>(frm_nxt - frm);
2463*0b57cec5SDimitry Andric                  break;
2464*0b57cec5SDimitry Andric             case 0xF4:
2465*0b57cec5SDimitry Andric                 if ((c2 & 0xF0) != 0x80)
2466*0b57cec5SDimitry Andric                     return static_cast<int>(frm_nxt - frm);
2467*0b57cec5SDimitry Andric                  break;
2468*0b57cec5SDimitry Andric             default:
2469*0b57cec5SDimitry Andric                 if ((c2 & 0xC0) != 0x80)
2470*0b57cec5SDimitry Andric                     return static_cast<int>(frm_nxt - frm);
2471*0b57cec5SDimitry Andric                  break;
2472*0b57cec5SDimitry Andric             }
2473*0b57cec5SDimitry Andric             if ((c3 & 0xC0) != 0x80 || (c4 & 0xC0) != 0x80)
2474*0b57cec5SDimitry Andric                 break;
2475*0b57cec5SDimitry Andric             if ((((c1 & 0x07u) << 18) | ((c2 & 0x3Fu) << 12) |
2476*0b57cec5SDimitry Andric                  ((c3 & 0x3Fu) << 6)  |  (c4 & 0x3Fu)) > Maxcode)
2477*0b57cec5SDimitry Andric                 break;
2478*0b57cec5SDimitry Andric             frm_nxt += 4;
2479*0b57cec5SDimitry Andric         }
2480*0b57cec5SDimitry Andric         else
2481*0b57cec5SDimitry Andric         {
2482*0b57cec5SDimitry Andric             break;
2483*0b57cec5SDimitry Andric         }
2484*0b57cec5SDimitry Andric     }
2485*0b57cec5SDimitry Andric     return static_cast<int>(frm_nxt - frm);
2486*0b57cec5SDimitry Andric }
2487*0b57cec5SDimitry Andric 
2488*0b57cec5SDimitry Andric static
2489*0b57cec5SDimitry Andric codecvt_base::result
2490*0b57cec5SDimitry Andric ucs2_to_utf8(const uint16_t* frm, const uint16_t* frm_end, const uint16_t*& frm_nxt,
2491*0b57cec5SDimitry Andric              uint8_t* to, uint8_t* to_end, uint8_t*& to_nxt,
2492*0b57cec5SDimitry Andric              unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0))
2493*0b57cec5SDimitry Andric {
2494*0b57cec5SDimitry Andric     frm_nxt = frm;
2495*0b57cec5SDimitry Andric     to_nxt = to;
2496*0b57cec5SDimitry Andric     if (mode & generate_header)
2497*0b57cec5SDimitry Andric     {
2498*0b57cec5SDimitry Andric         if (to_end-to_nxt < 3)
2499*0b57cec5SDimitry Andric             return codecvt_base::partial;
2500*0b57cec5SDimitry Andric         *to_nxt++ = static_cast<uint8_t>(0xEF);
2501*0b57cec5SDimitry Andric         *to_nxt++ = static_cast<uint8_t>(0xBB);
2502*0b57cec5SDimitry Andric         *to_nxt++ = static_cast<uint8_t>(0xBF);
2503*0b57cec5SDimitry Andric     }
2504*0b57cec5SDimitry Andric     for (; frm_nxt < frm_end; ++frm_nxt)
2505*0b57cec5SDimitry Andric     {
2506*0b57cec5SDimitry Andric         uint16_t wc = *frm_nxt;
2507*0b57cec5SDimitry Andric         if ((wc & 0xF800) == 0xD800 || wc > Maxcode)
2508*0b57cec5SDimitry Andric             return codecvt_base::error;
2509*0b57cec5SDimitry Andric         if (wc < 0x0080)
2510*0b57cec5SDimitry Andric         {
2511*0b57cec5SDimitry Andric             if (to_end-to_nxt < 1)
2512*0b57cec5SDimitry Andric                 return codecvt_base::partial;
2513*0b57cec5SDimitry Andric             *to_nxt++ = static_cast<uint8_t>(wc);
2514*0b57cec5SDimitry Andric         }
2515*0b57cec5SDimitry Andric         else if (wc < 0x0800)
2516*0b57cec5SDimitry Andric         {
2517*0b57cec5SDimitry Andric             if (to_end-to_nxt < 2)
2518*0b57cec5SDimitry Andric                 return codecvt_base::partial;
2519*0b57cec5SDimitry Andric             *to_nxt++ = static_cast<uint8_t>(0xC0 | (wc >> 6));
2520*0b57cec5SDimitry Andric             *to_nxt++ = static_cast<uint8_t>(0x80 | (wc & 0x03F));
2521*0b57cec5SDimitry Andric         }
2522*0b57cec5SDimitry Andric         else // if (wc <= 0xFFFF)
2523*0b57cec5SDimitry Andric         {
2524*0b57cec5SDimitry Andric             if (to_end-to_nxt < 3)
2525*0b57cec5SDimitry Andric                 return codecvt_base::partial;
2526*0b57cec5SDimitry Andric             *to_nxt++ = static_cast<uint8_t>(0xE0 |  (wc >> 12));
2527*0b57cec5SDimitry Andric             *to_nxt++ = static_cast<uint8_t>(0x80 | ((wc & 0x0FC0) >> 6));
2528*0b57cec5SDimitry Andric             *to_nxt++ = static_cast<uint8_t>(0x80 |  (wc & 0x003F));
2529*0b57cec5SDimitry Andric         }
2530*0b57cec5SDimitry Andric     }
2531*0b57cec5SDimitry Andric     return codecvt_base::ok;
2532*0b57cec5SDimitry Andric }
2533*0b57cec5SDimitry Andric 
2534*0b57cec5SDimitry Andric static
2535*0b57cec5SDimitry Andric codecvt_base::result
2536*0b57cec5SDimitry Andric utf8_to_ucs2(const uint8_t* frm, const uint8_t* frm_end, const uint8_t*& frm_nxt,
2537*0b57cec5SDimitry Andric              uint16_t* to, uint16_t* to_end, uint16_t*& to_nxt,
2538*0b57cec5SDimitry Andric              unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0))
2539*0b57cec5SDimitry Andric {
2540*0b57cec5SDimitry Andric     frm_nxt = frm;
2541*0b57cec5SDimitry Andric     to_nxt = to;
2542*0b57cec5SDimitry Andric     if (mode & consume_header)
2543*0b57cec5SDimitry Andric     {
2544*0b57cec5SDimitry Andric         if (frm_end-frm_nxt >= 3 && frm_nxt[0] == 0xEF && frm_nxt[1] == 0xBB &&
2545*0b57cec5SDimitry Andric                                                           frm_nxt[2] == 0xBF)
2546*0b57cec5SDimitry Andric             frm_nxt += 3;
2547*0b57cec5SDimitry Andric     }
2548*0b57cec5SDimitry Andric     for (; frm_nxt < frm_end && to_nxt < to_end; ++to_nxt)
2549*0b57cec5SDimitry Andric     {
2550*0b57cec5SDimitry Andric         uint8_t c1 = static_cast<uint8_t>(*frm_nxt);
2551*0b57cec5SDimitry Andric         if (c1 < 0x80)
2552*0b57cec5SDimitry Andric         {
2553*0b57cec5SDimitry Andric             if (c1 > Maxcode)
2554*0b57cec5SDimitry Andric                 return codecvt_base::error;
2555*0b57cec5SDimitry Andric             *to_nxt = static_cast<uint16_t>(c1);
2556*0b57cec5SDimitry Andric             ++frm_nxt;
2557*0b57cec5SDimitry Andric         }
2558*0b57cec5SDimitry Andric         else if (c1 < 0xC2)
2559*0b57cec5SDimitry Andric         {
2560*0b57cec5SDimitry Andric             return codecvt_base::error;
2561*0b57cec5SDimitry Andric         }
2562*0b57cec5SDimitry Andric         else if (c1 < 0xE0)
2563*0b57cec5SDimitry Andric         {
2564*0b57cec5SDimitry Andric             if (frm_end-frm_nxt < 2)
2565*0b57cec5SDimitry Andric                 return codecvt_base::partial;
2566*0b57cec5SDimitry Andric             uint8_t c2 = frm_nxt[1];
2567*0b57cec5SDimitry Andric             if ((c2 & 0xC0) != 0x80)
2568*0b57cec5SDimitry Andric                 return codecvt_base::error;
2569*0b57cec5SDimitry Andric             uint16_t t = static_cast<uint16_t>(((c1 & 0x1F) << 6)
2570*0b57cec5SDimitry Andric                                               | (c2 & 0x3F));
2571*0b57cec5SDimitry Andric             if (t > Maxcode)
2572*0b57cec5SDimitry Andric                 return codecvt_base::error;
2573*0b57cec5SDimitry Andric             *to_nxt = t;
2574*0b57cec5SDimitry Andric             frm_nxt += 2;
2575*0b57cec5SDimitry Andric         }
2576*0b57cec5SDimitry Andric         else if (c1 < 0xF0)
2577*0b57cec5SDimitry Andric         {
2578*0b57cec5SDimitry Andric             if (frm_end-frm_nxt < 3)
2579*0b57cec5SDimitry Andric                 return codecvt_base::partial;
2580*0b57cec5SDimitry Andric             uint8_t c2 = frm_nxt[1];
2581*0b57cec5SDimitry Andric             uint8_t c3 = frm_nxt[2];
2582*0b57cec5SDimitry Andric             switch (c1)
2583*0b57cec5SDimitry Andric             {
2584*0b57cec5SDimitry Andric             case 0xE0:
2585*0b57cec5SDimitry Andric                 if ((c2 & 0xE0) != 0xA0)
2586*0b57cec5SDimitry Andric                     return codecvt_base::error;
2587*0b57cec5SDimitry Andric                  break;
2588*0b57cec5SDimitry Andric             case 0xED:
2589*0b57cec5SDimitry Andric                 if ((c2 & 0xE0) != 0x80)
2590*0b57cec5SDimitry Andric                     return codecvt_base::error;
2591*0b57cec5SDimitry Andric                  break;
2592*0b57cec5SDimitry Andric             default:
2593*0b57cec5SDimitry Andric                 if ((c2 & 0xC0) != 0x80)
2594*0b57cec5SDimitry Andric                     return codecvt_base::error;
2595*0b57cec5SDimitry Andric                  break;
2596*0b57cec5SDimitry Andric             }
2597*0b57cec5SDimitry Andric             if ((c3 & 0xC0) != 0x80)
2598*0b57cec5SDimitry Andric                 return codecvt_base::error;
2599*0b57cec5SDimitry Andric             uint16_t t = static_cast<uint16_t>(((c1 & 0x0F) << 12)
2600*0b57cec5SDimitry Andric                                              | ((c2 & 0x3F) << 6)
2601*0b57cec5SDimitry Andric                                              |  (c3 & 0x3F));
2602*0b57cec5SDimitry Andric             if (t > Maxcode)
2603*0b57cec5SDimitry Andric                 return codecvt_base::error;
2604*0b57cec5SDimitry Andric             *to_nxt = t;
2605*0b57cec5SDimitry Andric             frm_nxt += 3;
2606*0b57cec5SDimitry Andric         }
2607*0b57cec5SDimitry Andric         else
2608*0b57cec5SDimitry Andric         {
2609*0b57cec5SDimitry Andric             return codecvt_base::error;
2610*0b57cec5SDimitry Andric         }
2611*0b57cec5SDimitry Andric     }
2612*0b57cec5SDimitry Andric     return frm_nxt < frm_end ? codecvt_base::partial : codecvt_base::ok;
2613*0b57cec5SDimitry Andric }
2614*0b57cec5SDimitry Andric 
2615*0b57cec5SDimitry Andric static
2616*0b57cec5SDimitry Andric int
2617*0b57cec5SDimitry Andric utf8_to_ucs2_length(const uint8_t* frm, const uint8_t* frm_end,
2618*0b57cec5SDimitry Andric                     size_t mx, unsigned long Maxcode = 0x10FFFF,
2619*0b57cec5SDimitry Andric                     codecvt_mode mode = codecvt_mode(0))
2620*0b57cec5SDimitry Andric {
2621*0b57cec5SDimitry Andric     const uint8_t* frm_nxt = frm;
2622*0b57cec5SDimitry Andric     if (mode & consume_header)
2623*0b57cec5SDimitry Andric     {
2624*0b57cec5SDimitry Andric         if (frm_end-frm_nxt >= 3 && frm_nxt[0] == 0xEF && frm_nxt[1] == 0xBB &&
2625*0b57cec5SDimitry Andric                                                           frm_nxt[2] == 0xBF)
2626*0b57cec5SDimitry Andric             frm_nxt += 3;
2627*0b57cec5SDimitry Andric     }
2628*0b57cec5SDimitry Andric     for (size_t nchar32_t = 0; frm_nxt < frm_end && nchar32_t < mx; ++nchar32_t)
2629*0b57cec5SDimitry Andric     {
2630*0b57cec5SDimitry Andric         uint8_t c1 = static_cast<uint8_t>(*frm_nxt);
2631*0b57cec5SDimitry Andric         if (c1 < 0x80)
2632*0b57cec5SDimitry Andric         {
2633*0b57cec5SDimitry Andric             if (c1 > Maxcode)
2634*0b57cec5SDimitry Andric                 break;
2635*0b57cec5SDimitry Andric             ++frm_nxt;
2636*0b57cec5SDimitry Andric         }
2637*0b57cec5SDimitry Andric         else if (c1 < 0xC2)
2638*0b57cec5SDimitry Andric         {
2639*0b57cec5SDimitry Andric             break;
2640*0b57cec5SDimitry Andric         }
2641*0b57cec5SDimitry Andric         else if (c1 < 0xE0)
2642*0b57cec5SDimitry Andric         {
2643*0b57cec5SDimitry Andric             if ((frm_end-frm_nxt < 2) || ((frm_nxt[1] & 0xC0) != 0x80))
2644*0b57cec5SDimitry Andric                 break;
2645*0b57cec5SDimitry Andric             if ((((c1 & 0x1Fu) << 6) | (frm_nxt[1] & 0x3Fu)) > Maxcode)
2646*0b57cec5SDimitry Andric                 break;
2647*0b57cec5SDimitry Andric             frm_nxt += 2;
2648*0b57cec5SDimitry Andric         }
2649*0b57cec5SDimitry Andric         else if (c1 < 0xF0)
2650*0b57cec5SDimitry Andric         {
2651*0b57cec5SDimitry Andric             if (frm_end-frm_nxt < 3)
2652*0b57cec5SDimitry Andric                 break;
2653*0b57cec5SDimitry Andric             uint8_t c2 = frm_nxt[1];
2654*0b57cec5SDimitry Andric             uint8_t c3 = frm_nxt[2];
2655*0b57cec5SDimitry Andric             switch (c1)
2656*0b57cec5SDimitry Andric             {
2657*0b57cec5SDimitry Andric             case 0xE0:
2658*0b57cec5SDimitry Andric                 if ((c2 & 0xE0) != 0xA0)
2659*0b57cec5SDimitry Andric                     return static_cast<int>(frm_nxt - frm);
2660*0b57cec5SDimitry Andric                 break;
2661*0b57cec5SDimitry Andric             case 0xED:
2662*0b57cec5SDimitry Andric                 if ((c2 & 0xE0) != 0x80)
2663*0b57cec5SDimitry Andric                     return static_cast<int>(frm_nxt - frm);
2664*0b57cec5SDimitry Andric                  break;
2665*0b57cec5SDimitry Andric             default:
2666*0b57cec5SDimitry Andric                 if ((c2 & 0xC0) != 0x80)
2667*0b57cec5SDimitry Andric                     return static_cast<int>(frm_nxt - frm);
2668*0b57cec5SDimitry Andric                  break;
2669*0b57cec5SDimitry Andric             }
2670*0b57cec5SDimitry Andric             if ((c3 & 0xC0) != 0x80)
2671*0b57cec5SDimitry Andric                 break;
2672*0b57cec5SDimitry Andric             if ((((c1 & 0x0Fu) << 12) | ((c2 & 0x3Fu) << 6) | (c3 & 0x3Fu)) > Maxcode)
2673*0b57cec5SDimitry Andric                 break;
2674*0b57cec5SDimitry Andric             frm_nxt += 3;
2675*0b57cec5SDimitry Andric         }
2676*0b57cec5SDimitry Andric         else
2677*0b57cec5SDimitry Andric         {
2678*0b57cec5SDimitry Andric             break;
2679*0b57cec5SDimitry Andric         }
2680*0b57cec5SDimitry Andric     }
2681*0b57cec5SDimitry Andric     return static_cast<int>(frm_nxt - frm);
2682*0b57cec5SDimitry Andric }
2683*0b57cec5SDimitry Andric 
2684*0b57cec5SDimitry Andric static
2685*0b57cec5SDimitry Andric codecvt_base::result
2686*0b57cec5SDimitry Andric ucs4_to_utf16be(const uint32_t* frm, const uint32_t* frm_end, const uint32_t*& frm_nxt,
2687*0b57cec5SDimitry Andric                 uint8_t* to, uint8_t* to_end, uint8_t*& to_nxt,
2688*0b57cec5SDimitry Andric                 unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0))
2689*0b57cec5SDimitry Andric {
2690*0b57cec5SDimitry Andric     frm_nxt = frm;
2691*0b57cec5SDimitry Andric     to_nxt = to;
2692*0b57cec5SDimitry Andric     if (mode & generate_header)
2693*0b57cec5SDimitry Andric     {
2694*0b57cec5SDimitry Andric         if (to_end-to_nxt < 2)
2695*0b57cec5SDimitry Andric             return codecvt_base::partial;
2696*0b57cec5SDimitry Andric         *to_nxt++ = static_cast<uint8_t>(0xFE);
2697*0b57cec5SDimitry Andric         *to_nxt++ = static_cast<uint8_t>(0xFF);
2698*0b57cec5SDimitry Andric     }
2699*0b57cec5SDimitry Andric     for (; frm_nxt < frm_end; ++frm_nxt)
2700*0b57cec5SDimitry Andric     {
2701*0b57cec5SDimitry Andric         uint32_t wc = *frm_nxt;
2702*0b57cec5SDimitry Andric         if ((wc & 0xFFFFF800) == 0x00D800 || wc > Maxcode)
2703*0b57cec5SDimitry Andric             return codecvt_base::error;
2704*0b57cec5SDimitry Andric         if (wc < 0x010000)
2705*0b57cec5SDimitry Andric         {
2706*0b57cec5SDimitry Andric             if (to_end-to_nxt < 2)
2707*0b57cec5SDimitry Andric                 return codecvt_base::partial;
2708*0b57cec5SDimitry Andric             *to_nxt++ = static_cast<uint8_t>(wc >> 8);
2709*0b57cec5SDimitry Andric             *to_nxt++ = static_cast<uint8_t>(wc);
2710*0b57cec5SDimitry Andric         }
2711*0b57cec5SDimitry Andric         else
2712*0b57cec5SDimitry Andric         {
2713*0b57cec5SDimitry Andric             if (to_end-to_nxt < 4)
2714*0b57cec5SDimitry Andric                 return codecvt_base::partial;
2715*0b57cec5SDimitry Andric             uint16_t t = static_cast<uint16_t>(
2716*0b57cec5SDimitry Andric                     0xD800
2717*0b57cec5SDimitry Andric                   | ((((wc & 0x1F0000) >> 16) - 1) << 6)
2718*0b57cec5SDimitry Andric                   |   ((wc & 0x00FC00) >> 10));
2719*0b57cec5SDimitry Andric             *to_nxt++ = static_cast<uint8_t>(t >> 8);
2720*0b57cec5SDimitry Andric             *to_nxt++ = static_cast<uint8_t>(t);
2721*0b57cec5SDimitry Andric             t = static_cast<uint16_t>(0xDC00 | (wc & 0x03FF));
2722*0b57cec5SDimitry Andric             *to_nxt++ = static_cast<uint8_t>(t >> 8);
2723*0b57cec5SDimitry Andric             *to_nxt++ = static_cast<uint8_t>(t);
2724*0b57cec5SDimitry Andric         }
2725*0b57cec5SDimitry Andric     }
2726*0b57cec5SDimitry Andric     return codecvt_base::ok;
2727*0b57cec5SDimitry Andric }
2728*0b57cec5SDimitry Andric 
2729*0b57cec5SDimitry Andric static
2730*0b57cec5SDimitry Andric codecvt_base::result
2731*0b57cec5SDimitry Andric utf16be_to_ucs4(const uint8_t* frm, const uint8_t* frm_end, const uint8_t*& frm_nxt,
2732*0b57cec5SDimitry Andric                 uint32_t* to, uint32_t* to_end, uint32_t*& to_nxt,
2733*0b57cec5SDimitry Andric                 unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0))
2734*0b57cec5SDimitry Andric {
2735*0b57cec5SDimitry Andric     frm_nxt = frm;
2736*0b57cec5SDimitry Andric     to_nxt = to;
2737*0b57cec5SDimitry Andric     if (mode & consume_header)
2738*0b57cec5SDimitry Andric     {
2739*0b57cec5SDimitry Andric         if (frm_end-frm_nxt >= 2 && frm_nxt[0] == 0xFE && frm_nxt[1] == 0xFF)
2740*0b57cec5SDimitry Andric             frm_nxt += 2;
2741*0b57cec5SDimitry Andric     }
2742*0b57cec5SDimitry Andric     for (; frm_nxt < frm_end - 1 && to_nxt < to_end; ++to_nxt)
2743*0b57cec5SDimitry Andric     {
2744*0b57cec5SDimitry Andric         uint16_t c1 = static_cast<uint16_t>(frm_nxt[0] << 8 | frm_nxt[1]);
2745*0b57cec5SDimitry Andric         if ((c1 & 0xFC00) == 0xDC00)
2746*0b57cec5SDimitry Andric             return codecvt_base::error;
2747*0b57cec5SDimitry Andric         if ((c1 & 0xFC00) != 0xD800)
2748*0b57cec5SDimitry Andric         {
2749*0b57cec5SDimitry Andric             if (c1 > Maxcode)
2750*0b57cec5SDimitry Andric                 return codecvt_base::error;
2751*0b57cec5SDimitry Andric             *to_nxt = static_cast<uint32_t>(c1);
2752*0b57cec5SDimitry Andric             frm_nxt += 2;
2753*0b57cec5SDimitry Andric         }
2754*0b57cec5SDimitry Andric         else
2755*0b57cec5SDimitry Andric         {
2756*0b57cec5SDimitry Andric             if (frm_end-frm_nxt < 4)
2757*0b57cec5SDimitry Andric                 return codecvt_base::partial;
2758*0b57cec5SDimitry Andric             uint16_t c2 = static_cast<uint16_t>(frm_nxt[2] << 8 | frm_nxt[3]);
2759*0b57cec5SDimitry Andric             if ((c2 & 0xFC00) != 0xDC00)
2760*0b57cec5SDimitry Andric                 return codecvt_base::error;
2761*0b57cec5SDimitry Andric             uint32_t t = static_cast<uint32_t>(
2762*0b57cec5SDimitry Andric                     ((((c1 & 0x03C0) >> 6) + 1) << 16)
2763*0b57cec5SDimitry Andric                   |   ((c1 & 0x003F) << 10)
2764*0b57cec5SDimitry Andric                   |    (c2 & 0x03FF));
2765*0b57cec5SDimitry Andric             if (t > Maxcode)
2766*0b57cec5SDimitry Andric                 return codecvt_base::error;
2767*0b57cec5SDimitry Andric             *to_nxt = t;
2768*0b57cec5SDimitry Andric             frm_nxt += 4;
2769*0b57cec5SDimitry Andric         }
2770*0b57cec5SDimitry Andric     }
2771*0b57cec5SDimitry Andric     return frm_nxt < frm_end ? codecvt_base::partial : codecvt_base::ok;
2772*0b57cec5SDimitry Andric }
2773*0b57cec5SDimitry Andric 
2774*0b57cec5SDimitry Andric static
2775*0b57cec5SDimitry Andric int
2776*0b57cec5SDimitry Andric utf16be_to_ucs4_length(const uint8_t* frm, const uint8_t* frm_end,
2777*0b57cec5SDimitry Andric                        size_t mx, unsigned long Maxcode = 0x10FFFF,
2778*0b57cec5SDimitry Andric                        codecvt_mode mode = codecvt_mode(0))
2779*0b57cec5SDimitry Andric {
2780*0b57cec5SDimitry Andric     const uint8_t* frm_nxt = frm;
2781*0b57cec5SDimitry Andric     if (mode & consume_header)
2782*0b57cec5SDimitry Andric     {
2783*0b57cec5SDimitry Andric         if (frm_end-frm_nxt >= 2 && frm_nxt[0] == 0xFE && frm_nxt[1] == 0xFF)
2784*0b57cec5SDimitry Andric             frm_nxt += 2;
2785*0b57cec5SDimitry Andric     }
2786*0b57cec5SDimitry Andric     for (size_t nchar32_t = 0; frm_nxt < frm_end - 1 && nchar32_t < mx; ++nchar32_t)
2787*0b57cec5SDimitry Andric     {
2788*0b57cec5SDimitry Andric         uint16_t c1 = static_cast<uint16_t>(frm_nxt[0] << 8 | frm_nxt[1]);
2789*0b57cec5SDimitry Andric         if ((c1 & 0xFC00) == 0xDC00)
2790*0b57cec5SDimitry Andric             break;
2791*0b57cec5SDimitry Andric         if ((c1 & 0xFC00) != 0xD800)
2792*0b57cec5SDimitry Andric         {
2793*0b57cec5SDimitry Andric             if (c1 > Maxcode)
2794*0b57cec5SDimitry Andric                 break;
2795*0b57cec5SDimitry Andric             frm_nxt += 2;
2796*0b57cec5SDimitry Andric         }
2797*0b57cec5SDimitry Andric         else
2798*0b57cec5SDimitry Andric         {
2799*0b57cec5SDimitry Andric             if (frm_end-frm_nxt < 4)
2800*0b57cec5SDimitry Andric                 break;
2801*0b57cec5SDimitry Andric             uint16_t c2 = static_cast<uint16_t>(frm_nxt[2] << 8 | frm_nxt[3]);
2802*0b57cec5SDimitry Andric             if ((c2 & 0xFC00) != 0xDC00)
2803*0b57cec5SDimitry Andric                 break;
2804*0b57cec5SDimitry Andric             uint32_t t = static_cast<uint32_t>(
2805*0b57cec5SDimitry Andric                     ((((c1 & 0x03C0) >> 6) + 1) << 16)
2806*0b57cec5SDimitry Andric                   |   ((c1 & 0x003F) << 10)
2807*0b57cec5SDimitry Andric                   |    (c2 & 0x03FF));
2808*0b57cec5SDimitry Andric             if (t > Maxcode)
2809*0b57cec5SDimitry Andric                 break;
2810*0b57cec5SDimitry Andric             frm_nxt += 4;
2811*0b57cec5SDimitry Andric         }
2812*0b57cec5SDimitry Andric     }
2813*0b57cec5SDimitry Andric     return static_cast<int>(frm_nxt - frm);
2814*0b57cec5SDimitry Andric }
2815*0b57cec5SDimitry Andric 
2816*0b57cec5SDimitry Andric static
2817*0b57cec5SDimitry Andric codecvt_base::result
2818*0b57cec5SDimitry Andric ucs4_to_utf16le(const uint32_t* frm, const uint32_t* frm_end, const uint32_t*& frm_nxt,
2819*0b57cec5SDimitry Andric                 uint8_t* to, uint8_t* to_end, uint8_t*& to_nxt,
2820*0b57cec5SDimitry Andric                 unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0))
2821*0b57cec5SDimitry Andric {
2822*0b57cec5SDimitry Andric     frm_nxt = frm;
2823*0b57cec5SDimitry Andric     to_nxt = to;
2824*0b57cec5SDimitry Andric     if (mode & generate_header)
2825*0b57cec5SDimitry Andric     {
2826*0b57cec5SDimitry Andric         if (to_end - to_nxt < 2)
2827*0b57cec5SDimitry Andric             return codecvt_base::partial;
2828*0b57cec5SDimitry Andric         *to_nxt++ = static_cast<uint8_t>(0xFF);
2829*0b57cec5SDimitry Andric         *to_nxt++ = static_cast<uint8_t>(0xFE);
2830*0b57cec5SDimitry Andric     }
2831*0b57cec5SDimitry Andric     for (; frm_nxt < frm_end; ++frm_nxt)
2832*0b57cec5SDimitry Andric     {
2833*0b57cec5SDimitry Andric         uint32_t wc = *frm_nxt;
2834*0b57cec5SDimitry Andric         if ((wc & 0xFFFFF800) == 0x00D800 || wc > Maxcode)
2835*0b57cec5SDimitry Andric             return codecvt_base::error;
2836*0b57cec5SDimitry Andric         if (wc < 0x010000)
2837*0b57cec5SDimitry Andric         {
2838*0b57cec5SDimitry Andric             if (to_end-to_nxt < 2)
2839*0b57cec5SDimitry Andric                 return codecvt_base::partial;
2840*0b57cec5SDimitry Andric             *to_nxt++ = static_cast<uint8_t>(wc);
2841*0b57cec5SDimitry Andric             *to_nxt++ = static_cast<uint8_t>(wc >> 8);
2842*0b57cec5SDimitry Andric         }
2843*0b57cec5SDimitry Andric         else
2844*0b57cec5SDimitry Andric         {
2845*0b57cec5SDimitry Andric             if (to_end-to_nxt < 4)
2846*0b57cec5SDimitry Andric                 return codecvt_base::partial;
2847*0b57cec5SDimitry Andric             uint16_t t = static_cast<uint16_t>(
2848*0b57cec5SDimitry Andric                     0xD800
2849*0b57cec5SDimitry Andric                   | ((((wc & 0x1F0000) >> 16) - 1) << 6)
2850*0b57cec5SDimitry Andric                   |   ((wc & 0x00FC00) >> 10));
2851*0b57cec5SDimitry Andric             *to_nxt++ = static_cast<uint8_t>(t);
2852*0b57cec5SDimitry Andric             *to_nxt++ = static_cast<uint8_t>(t >> 8);
2853*0b57cec5SDimitry Andric             t = static_cast<uint16_t>(0xDC00 | (wc & 0x03FF));
2854*0b57cec5SDimitry Andric             *to_nxt++ = static_cast<uint8_t>(t);
2855*0b57cec5SDimitry Andric             *to_nxt++ = static_cast<uint8_t>(t >> 8);
2856*0b57cec5SDimitry Andric         }
2857*0b57cec5SDimitry Andric     }
2858*0b57cec5SDimitry Andric     return codecvt_base::ok;
2859*0b57cec5SDimitry Andric }
2860*0b57cec5SDimitry Andric 
2861*0b57cec5SDimitry Andric static
2862*0b57cec5SDimitry Andric codecvt_base::result
2863*0b57cec5SDimitry Andric utf16le_to_ucs4(const uint8_t* frm, const uint8_t* frm_end, const uint8_t*& frm_nxt,
2864*0b57cec5SDimitry Andric                 uint32_t* to, uint32_t* to_end, uint32_t*& to_nxt,
2865*0b57cec5SDimitry Andric                 unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0))
2866*0b57cec5SDimitry Andric {
2867*0b57cec5SDimitry Andric     frm_nxt = frm;
2868*0b57cec5SDimitry Andric     to_nxt = to;
2869*0b57cec5SDimitry Andric     if (mode & consume_header)
2870*0b57cec5SDimitry Andric     {
2871*0b57cec5SDimitry Andric         if (frm_end-frm_nxt >= 2 && frm_nxt[0] == 0xFF && frm_nxt[1] == 0xFE)
2872*0b57cec5SDimitry Andric             frm_nxt += 2;
2873*0b57cec5SDimitry Andric     }
2874*0b57cec5SDimitry Andric     for (; frm_nxt < frm_end - 1 && to_nxt < to_end; ++to_nxt)
2875*0b57cec5SDimitry Andric     {
2876*0b57cec5SDimitry Andric         uint16_t c1 = static_cast<uint16_t>(frm_nxt[1] << 8 | frm_nxt[0]);
2877*0b57cec5SDimitry Andric         if ((c1 & 0xFC00) == 0xDC00)
2878*0b57cec5SDimitry Andric             return codecvt_base::error;
2879*0b57cec5SDimitry Andric         if ((c1 & 0xFC00) != 0xD800)
2880*0b57cec5SDimitry Andric         {
2881*0b57cec5SDimitry Andric             if (c1 > Maxcode)
2882*0b57cec5SDimitry Andric                 return codecvt_base::error;
2883*0b57cec5SDimitry Andric             *to_nxt = static_cast<uint32_t>(c1);
2884*0b57cec5SDimitry Andric             frm_nxt += 2;
2885*0b57cec5SDimitry Andric         }
2886*0b57cec5SDimitry Andric         else
2887*0b57cec5SDimitry Andric         {
2888*0b57cec5SDimitry Andric             if (frm_end-frm_nxt < 4)
2889*0b57cec5SDimitry Andric                 return codecvt_base::partial;
2890*0b57cec5SDimitry Andric             uint16_t c2 = static_cast<uint16_t>(frm_nxt[3] << 8 | frm_nxt[2]);
2891*0b57cec5SDimitry Andric             if ((c2 & 0xFC00) != 0xDC00)
2892*0b57cec5SDimitry Andric                 return codecvt_base::error;
2893*0b57cec5SDimitry Andric             uint32_t t = static_cast<uint32_t>(
2894*0b57cec5SDimitry Andric                     ((((c1 & 0x03C0) >> 6) + 1) << 16)
2895*0b57cec5SDimitry Andric                   |   ((c1 & 0x003F) << 10)
2896*0b57cec5SDimitry Andric                   |    (c2 & 0x03FF));
2897*0b57cec5SDimitry Andric             if (t > Maxcode)
2898*0b57cec5SDimitry Andric                 return codecvt_base::error;
2899*0b57cec5SDimitry Andric             *to_nxt = t;
2900*0b57cec5SDimitry Andric             frm_nxt += 4;
2901*0b57cec5SDimitry Andric         }
2902*0b57cec5SDimitry Andric     }
2903*0b57cec5SDimitry Andric     return frm_nxt < frm_end ? codecvt_base::partial : codecvt_base::ok;
2904*0b57cec5SDimitry Andric }
2905*0b57cec5SDimitry Andric 
2906*0b57cec5SDimitry Andric static
2907*0b57cec5SDimitry Andric int
2908*0b57cec5SDimitry Andric utf16le_to_ucs4_length(const uint8_t* frm, const uint8_t* frm_end,
2909*0b57cec5SDimitry Andric                        size_t mx, unsigned long Maxcode = 0x10FFFF,
2910*0b57cec5SDimitry Andric                        codecvt_mode mode = codecvt_mode(0))
2911*0b57cec5SDimitry Andric {
2912*0b57cec5SDimitry Andric     const uint8_t* frm_nxt = frm;
2913*0b57cec5SDimitry Andric     if (mode & consume_header)
2914*0b57cec5SDimitry Andric     {
2915*0b57cec5SDimitry Andric         if (frm_end-frm_nxt >= 2 && frm_nxt[0] == 0xFF && frm_nxt[1] == 0xFE)
2916*0b57cec5SDimitry Andric             frm_nxt += 2;
2917*0b57cec5SDimitry Andric     }
2918*0b57cec5SDimitry Andric     for (size_t nchar32_t = 0; frm_nxt < frm_end - 1 && nchar32_t < mx; ++nchar32_t)
2919*0b57cec5SDimitry Andric     {
2920*0b57cec5SDimitry Andric         uint16_t c1 = static_cast<uint16_t>(frm_nxt[1] << 8 | frm_nxt[0]);
2921*0b57cec5SDimitry Andric         if ((c1 & 0xFC00) == 0xDC00)
2922*0b57cec5SDimitry Andric             break;
2923*0b57cec5SDimitry Andric         if ((c1 & 0xFC00) != 0xD800)
2924*0b57cec5SDimitry Andric         {
2925*0b57cec5SDimitry Andric             if (c1 > Maxcode)
2926*0b57cec5SDimitry Andric                 break;
2927*0b57cec5SDimitry Andric             frm_nxt += 2;
2928*0b57cec5SDimitry Andric         }
2929*0b57cec5SDimitry Andric         else
2930*0b57cec5SDimitry Andric         {
2931*0b57cec5SDimitry Andric             if (frm_end-frm_nxt < 4)
2932*0b57cec5SDimitry Andric                 break;
2933*0b57cec5SDimitry Andric             uint16_t c2 = static_cast<uint16_t>(frm_nxt[3] << 8 | frm_nxt[2]);
2934*0b57cec5SDimitry Andric             if ((c2 & 0xFC00) != 0xDC00)
2935*0b57cec5SDimitry Andric                 break;
2936*0b57cec5SDimitry Andric             uint32_t t = static_cast<uint32_t>(
2937*0b57cec5SDimitry Andric                     ((((c1 & 0x03C0) >> 6) + 1) << 16)
2938*0b57cec5SDimitry Andric                   |   ((c1 & 0x003F) << 10)
2939*0b57cec5SDimitry Andric                   |    (c2 & 0x03FF));
2940*0b57cec5SDimitry Andric             if (t > Maxcode)
2941*0b57cec5SDimitry Andric                 break;
2942*0b57cec5SDimitry Andric             frm_nxt += 4;
2943*0b57cec5SDimitry Andric         }
2944*0b57cec5SDimitry Andric     }
2945*0b57cec5SDimitry Andric     return static_cast<int>(frm_nxt - frm);
2946*0b57cec5SDimitry Andric }
2947*0b57cec5SDimitry Andric 
2948*0b57cec5SDimitry Andric static
2949*0b57cec5SDimitry Andric codecvt_base::result
2950*0b57cec5SDimitry Andric ucs2_to_utf16be(const uint16_t* frm, const uint16_t* frm_end, const uint16_t*& frm_nxt,
2951*0b57cec5SDimitry Andric                 uint8_t* to, uint8_t* to_end, uint8_t*& to_nxt,
2952*0b57cec5SDimitry Andric                 unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0))
2953*0b57cec5SDimitry Andric {
2954*0b57cec5SDimitry Andric     frm_nxt = frm;
2955*0b57cec5SDimitry Andric     to_nxt = to;
2956*0b57cec5SDimitry Andric     if (mode & generate_header)
2957*0b57cec5SDimitry Andric     {
2958*0b57cec5SDimitry Andric         if (to_end-to_nxt < 2)
2959*0b57cec5SDimitry Andric             return codecvt_base::partial;
2960*0b57cec5SDimitry Andric         *to_nxt++ = static_cast<uint8_t>(0xFE);
2961*0b57cec5SDimitry Andric         *to_nxt++ = static_cast<uint8_t>(0xFF);
2962*0b57cec5SDimitry Andric     }
2963*0b57cec5SDimitry Andric     for (; frm_nxt < frm_end; ++frm_nxt)
2964*0b57cec5SDimitry Andric     {
2965*0b57cec5SDimitry Andric         uint16_t wc = *frm_nxt;
2966*0b57cec5SDimitry Andric         if ((wc & 0xF800) == 0xD800 || wc > Maxcode)
2967*0b57cec5SDimitry Andric             return codecvt_base::error;
2968*0b57cec5SDimitry Andric         if (to_end-to_nxt < 2)
2969*0b57cec5SDimitry Andric             return codecvt_base::partial;
2970*0b57cec5SDimitry Andric         *to_nxt++ = static_cast<uint8_t>(wc >> 8);
2971*0b57cec5SDimitry Andric         *to_nxt++ = static_cast<uint8_t>(wc);
2972*0b57cec5SDimitry Andric     }
2973*0b57cec5SDimitry Andric     return codecvt_base::ok;
2974*0b57cec5SDimitry Andric }
2975*0b57cec5SDimitry Andric 
2976*0b57cec5SDimitry Andric static
2977*0b57cec5SDimitry Andric codecvt_base::result
2978*0b57cec5SDimitry Andric utf16be_to_ucs2(const uint8_t* frm, const uint8_t* frm_end, const uint8_t*& frm_nxt,
2979*0b57cec5SDimitry Andric                 uint16_t* to, uint16_t* to_end, uint16_t*& to_nxt,
2980*0b57cec5SDimitry Andric                 unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0))
2981*0b57cec5SDimitry Andric {
2982*0b57cec5SDimitry Andric     frm_nxt = frm;
2983*0b57cec5SDimitry Andric     to_nxt = to;
2984*0b57cec5SDimitry Andric     if (mode & consume_header)
2985*0b57cec5SDimitry Andric     {
2986*0b57cec5SDimitry Andric         if (frm_end-frm_nxt >= 2 && frm_nxt[0] == 0xFE && frm_nxt[1] == 0xFF)
2987*0b57cec5SDimitry Andric             frm_nxt += 2;
2988*0b57cec5SDimitry Andric     }
2989*0b57cec5SDimitry Andric     for (; frm_nxt < frm_end - 1 && to_nxt < to_end; ++to_nxt)
2990*0b57cec5SDimitry Andric     {
2991*0b57cec5SDimitry Andric         uint16_t c1 = static_cast<uint16_t>(frm_nxt[0] << 8 | frm_nxt[1]);
2992*0b57cec5SDimitry Andric         if ((c1 & 0xF800) == 0xD800 || c1 > Maxcode)
2993*0b57cec5SDimitry Andric             return codecvt_base::error;
2994*0b57cec5SDimitry Andric         *to_nxt = c1;
2995*0b57cec5SDimitry Andric         frm_nxt += 2;
2996*0b57cec5SDimitry Andric     }
2997*0b57cec5SDimitry Andric     return frm_nxt < frm_end ? codecvt_base::partial : codecvt_base::ok;
2998*0b57cec5SDimitry Andric }
2999*0b57cec5SDimitry Andric 
3000*0b57cec5SDimitry Andric static
3001*0b57cec5SDimitry Andric int
3002*0b57cec5SDimitry Andric utf16be_to_ucs2_length(const uint8_t* frm, const uint8_t* frm_end,
3003*0b57cec5SDimitry Andric                        size_t mx, unsigned long Maxcode = 0x10FFFF,
3004*0b57cec5SDimitry Andric                        codecvt_mode mode = codecvt_mode(0))
3005*0b57cec5SDimitry Andric {
3006*0b57cec5SDimitry Andric     const uint8_t* frm_nxt = frm;
3007*0b57cec5SDimitry Andric     if (mode & consume_header)
3008*0b57cec5SDimitry Andric     {
3009*0b57cec5SDimitry Andric         if (frm_end-frm_nxt >= 2 && frm_nxt[0] == 0xFE && frm_nxt[1] == 0xFF)
3010*0b57cec5SDimitry Andric             frm_nxt += 2;
3011*0b57cec5SDimitry Andric     }
3012*0b57cec5SDimitry Andric     for (size_t nchar16_t = 0; frm_nxt < frm_end - 1 && nchar16_t < mx; ++nchar16_t)
3013*0b57cec5SDimitry Andric     {
3014*0b57cec5SDimitry Andric         uint16_t c1 = static_cast<uint16_t>(frm_nxt[0] << 8 | frm_nxt[1]);
3015*0b57cec5SDimitry Andric         if ((c1 & 0xF800) == 0xD800 || c1 > Maxcode)
3016*0b57cec5SDimitry Andric             break;
3017*0b57cec5SDimitry Andric         frm_nxt += 2;
3018*0b57cec5SDimitry Andric     }
3019*0b57cec5SDimitry Andric     return static_cast<int>(frm_nxt - frm);
3020*0b57cec5SDimitry Andric }
3021*0b57cec5SDimitry Andric 
3022*0b57cec5SDimitry Andric static
3023*0b57cec5SDimitry Andric codecvt_base::result
3024*0b57cec5SDimitry Andric ucs2_to_utf16le(const uint16_t* frm, const uint16_t* frm_end, const uint16_t*& frm_nxt,
3025*0b57cec5SDimitry Andric                 uint8_t* to, uint8_t* to_end, uint8_t*& to_nxt,
3026*0b57cec5SDimitry Andric                 unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0))
3027*0b57cec5SDimitry Andric {
3028*0b57cec5SDimitry Andric     frm_nxt = frm;
3029*0b57cec5SDimitry Andric     to_nxt = to;
3030*0b57cec5SDimitry Andric     if (mode & generate_header)
3031*0b57cec5SDimitry Andric     {
3032*0b57cec5SDimitry Andric         if (to_end-to_nxt < 2)
3033*0b57cec5SDimitry Andric             return codecvt_base::partial;
3034*0b57cec5SDimitry Andric         *to_nxt++ = static_cast<uint8_t>(0xFF);
3035*0b57cec5SDimitry Andric         *to_nxt++ = static_cast<uint8_t>(0xFE);
3036*0b57cec5SDimitry Andric     }
3037*0b57cec5SDimitry Andric     for (; frm_nxt < frm_end; ++frm_nxt)
3038*0b57cec5SDimitry Andric     {
3039*0b57cec5SDimitry Andric         uint16_t wc = *frm_nxt;
3040*0b57cec5SDimitry Andric         if ((wc & 0xF800) == 0xD800 || wc > Maxcode)
3041*0b57cec5SDimitry Andric             return codecvt_base::error;
3042*0b57cec5SDimitry Andric         if (to_end-to_nxt < 2)
3043*0b57cec5SDimitry Andric             return codecvt_base::partial;
3044*0b57cec5SDimitry Andric         *to_nxt++ = static_cast<uint8_t>(wc);
3045*0b57cec5SDimitry Andric         *to_nxt++ = static_cast<uint8_t>(wc >> 8);
3046*0b57cec5SDimitry Andric     }
3047*0b57cec5SDimitry Andric     return codecvt_base::ok;
3048*0b57cec5SDimitry Andric }
3049*0b57cec5SDimitry Andric 
3050*0b57cec5SDimitry Andric static
3051*0b57cec5SDimitry Andric codecvt_base::result
3052*0b57cec5SDimitry Andric utf16le_to_ucs2(const uint8_t* frm, const uint8_t* frm_end, const uint8_t*& frm_nxt,
3053*0b57cec5SDimitry Andric                 uint16_t* to, uint16_t* to_end, uint16_t*& to_nxt,
3054*0b57cec5SDimitry Andric                 unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0))
3055*0b57cec5SDimitry Andric {
3056*0b57cec5SDimitry Andric     frm_nxt = frm;
3057*0b57cec5SDimitry Andric     to_nxt = to;
3058*0b57cec5SDimitry Andric     if (mode & consume_header)
3059*0b57cec5SDimitry Andric     {
3060*0b57cec5SDimitry Andric         if (frm_end-frm_nxt >= 2 && frm_nxt[0] == 0xFF && frm_nxt[1] == 0xFE)
3061*0b57cec5SDimitry Andric             frm_nxt += 2;
3062*0b57cec5SDimitry Andric     }
3063*0b57cec5SDimitry Andric     for (; frm_nxt < frm_end - 1 && to_nxt < to_end; ++to_nxt)
3064*0b57cec5SDimitry Andric     {
3065*0b57cec5SDimitry Andric         uint16_t c1 = static_cast<uint16_t>(frm_nxt[1] << 8 | frm_nxt[0]);
3066*0b57cec5SDimitry Andric         if ((c1 & 0xF800) == 0xD800 || c1 > Maxcode)
3067*0b57cec5SDimitry Andric             return codecvt_base::error;
3068*0b57cec5SDimitry Andric         *to_nxt = c1;
3069*0b57cec5SDimitry Andric         frm_nxt += 2;
3070*0b57cec5SDimitry Andric     }
3071*0b57cec5SDimitry Andric     return frm_nxt < frm_end ? codecvt_base::partial : codecvt_base::ok;
3072*0b57cec5SDimitry Andric }
3073*0b57cec5SDimitry Andric 
3074*0b57cec5SDimitry Andric static
3075*0b57cec5SDimitry Andric int
3076*0b57cec5SDimitry Andric utf16le_to_ucs2_length(const uint8_t* frm, const uint8_t* frm_end,
3077*0b57cec5SDimitry Andric                        size_t mx, unsigned long Maxcode = 0x10FFFF,
3078*0b57cec5SDimitry Andric                        codecvt_mode mode = codecvt_mode(0))
3079*0b57cec5SDimitry Andric {
3080*0b57cec5SDimitry Andric     const uint8_t* frm_nxt = frm;
3081*0b57cec5SDimitry Andric     frm_nxt = frm;
3082*0b57cec5SDimitry Andric     if (mode & consume_header)
3083*0b57cec5SDimitry Andric     {
3084*0b57cec5SDimitry Andric         if (frm_end-frm_nxt >= 2 && frm_nxt[0] == 0xFF && frm_nxt[1] == 0xFE)
3085*0b57cec5SDimitry Andric             frm_nxt += 2;
3086*0b57cec5SDimitry Andric     }
3087*0b57cec5SDimitry Andric     for (size_t nchar16_t = 0; frm_nxt < frm_end - 1 && nchar16_t < mx; ++nchar16_t)
3088*0b57cec5SDimitry Andric     {
3089*0b57cec5SDimitry Andric         uint16_t c1 = static_cast<uint16_t>(frm_nxt[1] << 8 | frm_nxt[0]);
3090*0b57cec5SDimitry Andric         if ((c1 & 0xF800) == 0xD800 || c1 > Maxcode)
3091*0b57cec5SDimitry Andric             break;
3092*0b57cec5SDimitry Andric         frm_nxt += 2;
3093*0b57cec5SDimitry Andric     }
3094*0b57cec5SDimitry Andric     return static_cast<int>(frm_nxt - frm);
3095*0b57cec5SDimitry Andric }
3096*0b57cec5SDimitry Andric 
3097*0b57cec5SDimitry Andric // template <> class codecvt<char16_t, char, mbstate_t>
3098*0b57cec5SDimitry Andric 
3099*0b57cec5SDimitry Andric locale::id codecvt<char16_t, char, mbstate_t>::id;
3100*0b57cec5SDimitry Andric 
3101*0b57cec5SDimitry Andric codecvt<char16_t, char, mbstate_t>::~codecvt()
3102*0b57cec5SDimitry Andric {
3103*0b57cec5SDimitry Andric }
3104*0b57cec5SDimitry Andric 
3105*0b57cec5SDimitry Andric codecvt<char16_t, char, mbstate_t>::result
3106*0b57cec5SDimitry Andric codecvt<char16_t, char, mbstate_t>::do_out(state_type&,
3107*0b57cec5SDimitry Andric     const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt,
3108*0b57cec5SDimitry Andric     extern_type* to, extern_type* to_end, extern_type*& to_nxt) const
3109*0b57cec5SDimitry Andric {
3110*0b57cec5SDimitry Andric     const uint16_t* _frm = reinterpret_cast<const uint16_t*>(frm);
3111*0b57cec5SDimitry Andric     const uint16_t* _frm_end = reinterpret_cast<const uint16_t*>(frm_end);
3112*0b57cec5SDimitry Andric     const uint16_t* _frm_nxt = _frm;
3113*0b57cec5SDimitry Andric     uint8_t* _to = reinterpret_cast<uint8_t*>(to);
3114*0b57cec5SDimitry Andric     uint8_t* _to_end = reinterpret_cast<uint8_t*>(to_end);
3115*0b57cec5SDimitry Andric     uint8_t* _to_nxt = _to;
3116*0b57cec5SDimitry Andric     result r = utf16_to_utf8(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt);
3117*0b57cec5SDimitry Andric     frm_nxt = frm + (_frm_nxt - _frm);
3118*0b57cec5SDimitry Andric     to_nxt = to + (_to_nxt - _to);
3119*0b57cec5SDimitry Andric     return r;
3120*0b57cec5SDimitry Andric }
3121*0b57cec5SDimitry Andric 
3122*0b57cec5SDimitry Andric codecvt<char16_t, char, mbstate_t>::result
3123*0b57cec5SDimitry Andric codecvt<char16_t, char, mbstate_t>::do_in(state_type&,
3124*0b57cec5SDimitry Andric     const extern_type* frm, const extern_type* frm_end, const extern_type*& frm_nxt,
3125*0b57cec5SDimitry Andric     intern_type* to, intern_type* to_end, intern_type*& to_nxt) const
3126*0b57cec5SDimitry Andric {
3127*0b57cec5SDimitry Andric     const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3128*0b57cec5SDimitry Andric     const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3129*0b57cec5SDimitry Andric     const uint8_t* _frm_nxt = _frm;
3130*0b57cec5SDimitry Andric     uint16_t* _to = reinterpret_cast<uint16_t*>(to);
3131*0b57cec5SDimitry Andric     uint16_t* _to_end = reinterpret_cast<uint16_t*>(to_end);
3132*0b57cec5SDimitry Andric     uint16_t* _to_nxt = _to;
3133*0b57cec5SDimitry Andric     result r = utf8_to_utf16(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt);
3134*0b57cec5SDimitry Andric     frm_nxt = frm + (_frm_nxt - _frm);
3135*0b57cec5SDimitry Andric     to_nxt = to + (_to_nxt - _to);
3136*0b57cec5SDimitry Andric     return r;
3137*0b57cec5SDimitry Andric }
3138*0b57cec5SDimitry Andric 
3139*0b57cec5SDimitry Andric codecvt<char16_t, char, mbstate_t>::result
3140*0b57cec5SDimitry Andric codecvt<char16_t, char, mbstate_t>::do_unshift(state_type&,
3141*0b57cec5SDimitry Andric     extern_type* to, extern_type*, extern_type*& to_nxt) const
3142*0b57cec5SDimitry Andric {
3143*0b57cec5SDimitry Andric     to_nxt = to;
3144*0b57cec5SDimitry Andric     return noconv;
3145*0b57cec5SDimitry Andric }
3146*0b57cec5SDimitry Andric 
3147*0b57cec5SDimitry Andric int
3148*0b57cec5SDimitry Andric codecvt<char16_t, char, mbstate_t>::do_encoding() const  _NOEXCEPT
3149*0b57cec5SDimitry Andric {
3150*0b57cec5SDimitry Andric     return 0;
3151*0b57cec5SDimitry Andric }
3152*0b57cec5SDimitry Andric 
3153*0b57cec5SDimitry Andric bool
3154*0b57cec5SDimitry Andric codecvt<char16_t, char, mbstate_t>::do_always_noconv() const  _NOEXCEPT
3155*0b57cec5SDimitry Andric {
3156*0b57cec5SDimitry Andric     return false;
3157*0b57cec5SDimitry Andric }
3158*0b57cec5SDimitry Andric 
3159*0b57cec5SDimitry Andric int
3160*0b57cec5SDimitry Andric codecvt<char16_t, char, mbstate_t>::do_length(state_type&,
3161*0b57cec5SDimitry Andric     const extern_type* frm, const extern_type* frm_end, size_t mx) const
3162*0b57cec5SDimitry Andric {
3163*0b57cec5SDimitry Andric     const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3164*0b57cec5SDimitry Andric     const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3165*0b57cec5SDimitry Andric     return utf8_to_utf16_length(_frm, _frm_end, mx);
3166*0b57cec5SDimitry Andric }
3167*0b57cec5SDimitry Andric 
3168*0b57cec5SDimitry Andric int
3169*0b57cec5SDimitry Andric codecvt<char16_t, char, mbstate_t>::do_max_length() const  _NOEXCEPT
3170*0b57cec5SDimitry Andric {
3171*0b57cec5SDimitry Andric     return 4;
3172*0b57cec5SDimitry Andric }
3173*0b57cec5SDimitry Andric 
3174*0b57cec5SDimitry Andric // template <> class codecvt<char32_t, char, mbstate_t>
3175*0b57cec5SDimitry Andric 
3176*0b57cec5SDimitry Andric locale::id codecvt<char32_t, char, mbstate_t>::id;
3177*0b57cec5SDimitry Andric 
3178*0b57cec5SDimitry Andric codecvt<char32_t, char, mbstate_t>::~codecvt()
3179*0b57cec5SDimitry Andric {
3180*0b57cec5SDimitry Andric }
3181*0b57cec5SDimitry Andric 
3182*0b57cec5SDimitry Andric codecvt<char32_t, char, mbstate_t>::result
3183*0b57cec5SDimitry Andric codecvt<char32_t, char, mbstate_t>::do_out(state_type&,
3184*0b57cec5SDimitry Andric     const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt,
3185*0b57cec5SDimitry Andric     extern_type* to, extern_type* to_end, extern_type*& to_nxt) const
3186*0b57cec5SDimitry Andric {
3187*0b57cec5SDimitry Andric     const uint32_t* _frm = reinterpret_cast<const uint32_t*>(frm);
3188*0b57cec5SDimitry Andric     const uint32_t* _frm_end = reinterpret_cast<const uint32_t*>(frm_end);
3189*0b57cec5SDimitry Andric     const uint32_t* _frm_nxt = _frm;
3190*0b57cec5SDimitry Andric     uint8_t* _to = reinterpret_cast<uint8_t*>(to);
3191*0b57cec5SDimitry Andric     uint8_t* _to_end = reinterpret_cast<uint8_t*>(to_end);
3192*0b57cec5SDimitry Andric     uint8_t* _to_nxt = _to;
3193*0b57cec5SDimitry Andric     result r = ucs4_to_utf8(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt);
3194*0b57cec5SDimitry Andric     frm_nxt = frm + (_frm_nxt - _frm);
3195*0b57cec5SDimitry Andric     to_nxt = to + (_to_nxt - _to);
3196*0b57cec5SDimitry Andric     return r;
3197*0b57cec5SDimitry Andric }
3198*0b57cec5SDimitry Andric 
3199*0b57cec5SDimitry Andric codecvt<char32_t, char, mbstate_t>::result
3200*0b57cec5SDimitry Andric codecvt<char32_t, char, mbstate_t>::do_in(state_type&,
3201*0b57cec5SDimitry Andric     const extern_type* frm, const extern_type* frm_end, const extern_type*& frm_nxt,
3202*0b57cec5SDimitry Andric     intern_type* to, intern_type* to_end, intern_type*& to_nxt) const
3203*0b57cec5SDimitry Andric {
3204*0b57cec5SDimitry Andric     const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3205*0b57cec5SDimitry Andric     const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3206*0b57cec5SDimitry Andric     const uint8_t* _frm_nxt = _frm;
3207*0b57cec5SDimitry Andric     uint32_t* _to = reinterpret_cast<uint32_t*>(to);
3208*0b57cec5SDimitry Andric     uint32_t* _to_end = reinterpret_cast<uint32_t*>(to_end);
3209*0b57cec5SDimitry Andric     uint32_t* _to_nxt = _to;
3210*0b57cec5SDimitry Andric     result r = utf8_to_ucs4(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt);
3211*0b57cec5SDimitry Andric     frm_nxt = frm + (_frm_nxt - _frm);
3212*0b57cec5SDimitry Andric     to_nxt = to + (_to_nxt - _to);
3213*0b57cec5SDimitry Andric     return r;
3214*0b57cec5SDimitry Andric }
3215*0b57cec5SDimitry Andric 
3216*0b57cec5SDimitry Andric codecvt<char32_t, char, mbstate_t>::result
3217*0b57cec5SDimitry Andric codecvt<char32_t, char, mbstate_t>::do_unshift(state_type&,
3218*0b57cec5SDimitry Andric     extern_type* to, extern_type*, extern_type*& to_nxt) const
3219*0b57cec5SDimitry Andric {
3220*0b57cec5SDimitry Andric     to_nxt = to;
3221*0b57cec5SDimitry Andric     return noconv;
3222*0b57cec5SDimitry Andric }
3223*0b57cec5SDimitry Andric 
3224*0b57cec5SDimitry Andric int
3225*0b57cec5SDimitry Andric codecvt<char32_t, char, mbstate_t>::do_encoding() const  _NOEXCEPT
3226*0b57cec5SDimitry Andric {
3227*0b57cec5SDimitry Andric     return 0;
3228*0b57cec5SDimitry Andric }
3229*0b57cec5SDimitry Andric 
3230*0b57cec5SDimitry Andric bool
3231*0b57cec5SDimitry Andric codecvt<char32_t, char, mbstate_t>::do_always_noconv() const  _NOEXCEPT
3232*0b57cec5SDimitry Andric {
3233*0b57cec5SDimitry Andric     return false;
3234*0b57cec5SDimitry Andric }
3235*0b57cec5SDimitry Andric 
3236*0b57cec5SDimitry Andric int
3237*0b57cec5SDimitry Andric codecvt<char32_t, char, mbstate_t>::do_length(state_type&,
3238*0b57cec5SDimitry Andric     const extern_type* frm, const extern_type* frm_end, size_t mx) const
3239*0b57cec5SDimitry Andric {
3240*0b57cec5SDimitry Andric     const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3241*0b57cec5SDimitry Andric     const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3242*0b57cec5SDimitry Andric     return utf8_to_ucs4_length(_frm, _frm_end, mx);
3243*0b57cec5SDimitry Andric }
3244*0b57cec5SDimitry Andric 
3245*0b57cec5SDimitry Andric int
3246*0b57cec5SDimitry Andric codecvt<char32_t, char, mbstate_t>::do_max_length() const  _NOEXCEPT
3247*0b57cec5SDimitry Andric {
3248*0b57cec5SDimitry Andric     return 4;
3249*0b57cec5SDimitry Andric }
3250*0b57cec5SDimitry Andric 
3251*0b57cec5SDimitry Andric // __codecvt_utf8<wchar_t>
3252*0b57cec5SDimitry Andric 
3253*0b57cec5SDimitry Andric __codecvt_utf8<wchar_t>::result
3254*0b57cec5SDimitry Andric __codecvt_utf8<wchar_t>::do_out(state_type&,
3255*0b57cec5SDimitry Andric     const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt,
3256*0b57cec5SDimitry Andric     extern_type* to, extern_type* to_end, extern_type*& to_nxt) const
3257*0b57cec5SDimitry Andric {
3258*0b57cec5SDimitry Andric #if defined(_LIBCPP_SHORT_WCHAR)
3259*0b57cec5SDimitry Andric     const uint16_t* _frm = reinterpret_cast<const uint16_t*>(frm);
3260*0b57cec5SDimitry Andric     const uint16_t* _frm_end = reinterpret_cast<const uint16_t*>(frm_end);
3261*0b57cec5SDimitry Andric     const uint16_t* _frm_nxt = _frm;
3262*0b57cec5SDimitry Andric #else
3263*0b57cec5SDimitry Andric     const uint32_t* _frm = reinterpret_cast<const uint32_t*>(frm);
3264*0b57cec5SDimitry Andric     const uint32_t* _frm_end = reinterpret_cast<const uint32_t*>(frm_end);
3265*0b57cec5SDimitry Andric     const uint32_t* _frm_nxt = _frm;
3266*0b57cec5SDimitry Andric #endif
3267*0b57cec5SDimitry Andric     uint8_t* _to = reinterpret_cast<uint8_t*>(to);
3268*0b57cec5SDimitry Andric     uint8_t* _to_end = reinterpret_cast<uint8_t*>(to_end);
3269*0b57cec5SDimitry Andric     uint8_t* _to_nxt = _to;
3270*0b57cec5SDimitry Andric #if defined(_LIBCPP_SHORT_WCHAR)
3271*0b57cec5SDimitry Andric     result r = ucs2_to_utf8(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3272*0b57cec5SDimitry Andric                             _Maxcode_, _Mode_);
3273*0b57cec5SDimitry Andric #else
3274*0b57cec5SDimitry Andric     result r = ucs4_to_utf8(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3275*0b57cec5SDimitry Andric                             _Maxcode_, _Mode_);
3276*0b57cec5SDimitry Andric #endif
3277*0b57cec5SDimitry Andric     frm_nxt = frm + (_frm_nxt - _frm);
3278*0b57cec5SDimitry Andric     to_nxt = to + (_to_nxt - _to);
3279*0b57cec5SDimitry Andric     return r;
3280*0b57cec5SDimitry Andric }
3281*0b57cec5SDimitry Andric 
3282*0b57cec5SDimitry Andric __codecvt_utf8<wchar_t>::result
3283*0b57cec5SDimitry Andric __codecvt_utf8<wchar_t>::do_in(state_type&,
3284*0b57cec5SDimitry Andric     const extern_type* frm, const extern_type* frm_end, const extern_type*& frm_nxt,
3285*0b57cec5SDimitry Andric     intern_type* to, intern_type* to_end, intern_type*& to_nxt) const
3286*0b57cec5SDimitry Andric {
3287*0b57cec5SDimitry Andric     const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3288*0b57cec5SDimitry Andric     const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3289*0b57cec5SDimitry Andric     const uint8_t* _frm_nxt = _frm;
3290*0b57cec5SDimitry Andric #if defined(_LIBCPP_SHORT_WCHAR)
3291*0b57cec5SDimitry Andric     uint16_t* _to = reinterpret_cast<uint16_t*>(to);
3292*0b57cec5SDimitry Andric     uint16_t* _to_end = reinterpret_cast<uint16_t*>(to_end);
3293*0b57cec5SDimitry Andric     uint16_t* _to_nxt = _to;
3294*0b57cec5SDimitry Andric     result r = utf8_to_ucs2(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3295*0b57cec5SDimitry Andric                             _Maxcode_, _Mode_);
3296*0b57cec5SDimitry Andric #else
3297*0b57cec5SDimitry Andric     uint32_t* _to = reinterpret_cast<uint32_t*>(to);
3298*0b57cec5SDimitry Andric     uint32_t* _to_end = reinterpret_cast<uint32_t*>(to_end);
3299*0b57cec5SDimitry Andric     uint32_t* _to_nxt = _to;
3300*0b57cec5SDimitry Andric     result r = utf8_to_ucs4(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3301*0b57cec5SDimitry Andric                             _Maxcode_, _Mode_);
3302*0b57cec5SDimitry Andric #endif
3303*0b57cec5SDimitry Andric     frm_nxt = frm + (_frm_nxt - _frm);
3304*0b57cec5SDimitry Andric     to_nxt = to + (_to_nxt - _to);
3305*0b57cec5SDimitry Andric     return r;
3306*0b57cec5SDimitry Andric }
3307*0b57cec5SDimitry Andric 
3308*0b57cec5SDimitry Andric __codecvt_utf8<wchar_t>::result
3309*0b57cec5SDimitry Andric __codecvt_utf8<wchar_t>::do_unshift(state_type&,
3310*0b57cec5SDimitry Andric     extern_type* to, extern_type*, extern_type*& to_nxt) const
3311*0b57cec5SDimitry Andric {
3312*0b57cec5SDimitry Andric     to_nxt = to;
3313*0b57cec5SDimitry Andric     return noconv;
3314*0b57cec5SDimitry Andric }
3315*0b57cec5SDimitry Andric 
3316*0b57cec5SDimitry Andric int
3317*0b57cec5SDimitry Andric __codecvt_utf8<wchar_t>::do_encoding() const  _NOEXCEPT
3318*0b57cec5SDimitry Andric {
3319*0b57cec5SDimitry Andric     return 0;
3320*0b57cec5SDimitry Andric }
3321*0b57cec5SDimitry Andric 
3322*0b57cec5SDimitry Andric bool
3323*0b57cec5SDimitry Andric __codecvt_utf8<wchar_t>::do_always_noconv() const  _NOEXCEPT
3324*0b57cec5SDimitry Andric {
3325*0b57cec5SDimitry Andric     return false;
3326*0b57cec5SDimitry Andric }
3327*0b57cec5SDimitry Andric 
3328*0b57cec5SDimitry Andric int
3329*0b57cec5SDimitry Andric __codecvt_utf8<wchar_t>::do_length(state_type&,
3330*0b57cec5SDimitry Andric     const extern_type* frm, const extern_type* frm_end, size_t mx) const
3331*0b57cec5SDimitry Andric {
3332*0b57cec5SDimitry Andric     const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3333*0b57cec5SDimitry Andric     const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3334*0b57cec5SDimitry Andric     return utf8_to_ucs4_length(_frm, _frm_end, mx, _Maxcode_, _Mode_);
3335*0b57cec5SDimitry Andric }
3336*0b57cec5SDimitry Andric 
3337*0b57cec5SDimitry Andric int
3338*0b57cec5SDimitry Andric __codecvt_utf8<wchar_t>::do_max_length() const  _NOEXCEPT
3339*0b57cec5SDimitry Andric {
3340*0b57cec5SDimitry Andric     if (_Mode_ & consume_header)
3341*0b57cec5SDimitry Andric         return 7;
3342*0b57cec5SDimitry Andric     return 4;
3343*0b57cec5SDimitry Andric }
3344*0b57cec5SDimitry Andric 
3345*0b57cec5SDimitry Andric // __codecvt_utf8<char16_t>
3346*0b57cec5SDimitry Andric 
3347*0b57cec5SDimitry Andric __codecvt_utf8<char16_t>::result
3348*0b57cec5SDimitry Andric __codecvt_utf8<char16_t>::do_out(state_type&,
3349*0b57cec5SDimitry Andric     const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt,
3350*0b57cec5SDimitry Andric     extern_type* to, extern_type* to_end, extern_type*& to_nxt) const
3351*0b57cec5SDimitry Andric {
3352*0b57cec5SDimitry Andric     const uint16_t* _frm = reinterpret_cast<const uint16_t*>(frm);
3353*0b57cec5SDimitry Andric     const uint16_t* _frm_end = reinterpret_cast<const uint16_t*>(frm_end);
3354*0b57cec5SDimitry Andric     const uint16_t* _frm_nxt = _frm;
3355*0b57cec5SDimitry Andric     uint8_t* _to = reinterpret_cast<uint8_t*>(to);
3356*0b57cec5SDimitry Andric     uint8_t* _to_end = reinterpret_cast<uint8_t*>(to_end);
3357*0b57cec5SDimitry Andric     uint8_t* _to_nxt = _to;
3358*0b57cec5SDimitry Andric     result r = ucs2_to_utf8(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3359*0b57cec5SDimitry Andric                             _Maxcode_, _Mode_);
3360*0b57cec5SDimitry Andric     frm_nxt = frm + (_frm_nxt - _frm);
3361*0b57cec5SDimitry Andric     to_nxt = to + (_to_nxt - _to);
3362*0b57cec5SDimitry Andric     return r;
3363*0b57cec5SDimitry Andric }
3364*0b57cec5SDimitry Andric 
3365*0b57cec5SDimitry Andric __codecvt_utf8<char16_t>::result
3366*0b57cec5SDimitry Andric __codecvt_utf8<char16_t>::do_in(state_type&,
3367*0b57cec5SDimitry Andric     const extern_type* frm, const extern_type* frm_end, const extern_type*& frm_nxt,
3368*0b57cec5SDimitry Andric     intern_type* to, intern_type* to_end, intern_type*& to_nxt) const
3369*0b57cec5SDimitry Andric {
3370*0b57cec5SDimitry Andric     const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3371*0b57cec5SDimitry Andric     const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3372*0b57cec5SDimitry Andric     const uint8_t* _frm_nxt = _frm;
3373*0b57cec5SDimitry Andric     uint16_t* _to = reinterpret_cast<uint16_t*>(to);
3374*0b57cec5SDimitry Andric     uint16_t* _to_end = reinterpret_cast<uint16_t*>(to_end);
3375*0b57cec5SDimitry Andric     uint16_t* _to_nxt = _to;
3376*0b57cec5SDimitry Andric     result r = utf8_to_ucs2(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3377*0b57cec5SDimitry Andric                             _Maxcode_, _Mode_);
3378*0b57cec5SDimitry Andric     frm_nxt = frm + (_frm_nxt - _frm);
3379*0b57cec5SDimitry Andric     to_nxt = to + (_to_nxt - _to);
3380*0b57cec5SDimitry Andric     return r;
3381*0b57cec5SDimitry Andric }
3382*0b57cec5SDimitry Andric 
3383*0b57cec5SDimitry Andric __codecvt_utf8<char16_t>::result
3384*0b57cec5SDimitry Andric __codecvt_utf8<char16_t>::do_unshift(state_type&,
3385*0b57cec5SDimitry Andric     extern_type* to, extern_type*, extern_type*& to_nxt) const
3386*0b57cec5SDimitry Andric {
3387*0b57cec5SDimitry Andric     to_nxt = to;
3388*0b57cec5SDimitry Andric     return noconv;
3389*0b57cec5SDimitry Andric }
3390*0b57cec5SDimitry Andric 
3391*0b57cec5SDimitry Andric int
3392*0b57cec5SDimitry Andric __codecvt_utf8<char16_t>::do_encoding() const  _NOEXCEPT
3393*0b57cec5SDimitry Andric {
3394*0b57cec5SDimitry Andric     return 0;
3395*0b57cec5SDimitry Andric }
3396*0b57cec5SDimitry Andric 
3397*0b57cec5SDimitry Andric bool
3398*0b57cec5SDimitry Andric __codecvt_utf8<char16_t>::do_always_noconv() const  _NOEXCEPT
3399*0b57cec5SDimitry Andric {
3400*0b57cec5SDimitry Andric     return false;
3401*0b57cec5SDimitry Andric }
3402*0b57cec5SDimitry Andric 
3403*0b57cec5SDimitry Andric int
3404*0b57cec5SDimitry Andric __codecvt_utf8<char16_t>::do_length(state_type&,
3405*0b57cec5SDimitry Andric     const extern_type* frm, const extern_type* frm_end, size_t mx) const
3406*0b57cec5SDimitry Andric {
3407*0b57cec5SDimitry Andric     const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3408*0b57cec5SDimitry Andric     const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3409*0b57cec5SDimitry Andric     return utf8_to_ucs2_length(_frm, _frm_end, mx, _Maxcode_, _Mode_);
3410*0b57cec5SDimitry Andric }
3411*0b57cec5SDimitry Andric 
3412*0b57cec5SDimitry Andric int
3413*0b57cec5SDimitry Andric __codecvt_utf8<char16_t>::do_max_length() const  _NOEXCEPT
3414*0b57cec5SDimitry Andric {
3415*0b57cec5SDimitry Andric     if (_Mode_ & consume_header)
3416*0b57cec5SDimitry Andric         return 6;
3417*0b57cec5SDimitry Andric     return 3;
3418*0b57cec5SDimitry Andric }
3419*0b57cec5SDimitry Andric 
3420*0b57cec5SDimitry Andric // __codecvt_utf8<char32_t>
3421*0b57cec5SDimitry Andric 
3422*0b57cec5SDimitry Andric __codecvt_utf8<char32_t>::result
3423*0b57cec5SDimitry Andric __codecvt_utf8<char32_t>::do_out(state_type&,
3424*0b57cec5SDimitry Andric     const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt,
3425*0b57cec5SDimitry Andric     extern_type* to, extern_type* to_end, extern_type*& to_nxt) const
3426*0b57cec5SDimitry Andric {
3427*0b57cec5SDimitry Andric     const uint32_t* _frm = reinterpret_cast<const uint32_t*>(frm);
3428*0b57cec5SDimitry Andric     const uint32_t* _frm_end = reinterpret_cast<const uint32_t*>(frm_end);
3429*0b57cec5SDimitry Andric     const uint32_t* _frm_nxt = _frm;
3430*0b57cec5SDimitry Andric     uint8_t* _to = reinterpret_cast<uint8_t*>(to);
3431*0b57cec5SDimitry Andric     uint8_t* _to_end = reinterpret_cast<uint8_t*>(to_end);
3432*0b57cec5SDimitry Andric     uint8_t* _to_nxt = _to;
3433*0b57cec5SDimitry Andric     result r = ucs4_to_utf8(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3434*0b57cec5SDimitry Andric                             _Maxcode_, _Mode_);
3435*0b57cec5SDimitry Andric     frm_nxt = frm + (_frm_nxt - _frm);
3436*0b57cec5SDimitry Andric     to_nxt = to + (_to_nxt - _to);
3437*0b57cec5SDimitry Andric     return r;
3438*0b57cec5SDimitry Andric }
3439*0b57cec5SDimitry Andric 
3440*0b57cec5SDimitry Andric __codecvt_utf8<char32_t>::result
3441*0b57cec5SDimitry Andric __codecvt_utf8<char32_t>::do_in(state_type&,
3442*0b57cec5SDimitry Andric     const extern_type* frm, const extern_type* frm_end, const extern_type*& frm_nxt,
3443*0b57cec5SDimitry Andric     intern_type* to, intern_type* to_end, intern_type*& to_nxt) const
3444*0b57cec5SDimitry Andric {
3445*0b57cec5SDimitry Andric     const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3446*0b57cec5SDimitry Andric     const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3447*0b57cec5SDimitry Andric     const uint8_t* _frm_nxt = _frm;
3448*0b57cec5SDimitry Andric     uint32_t* _to = reinterpret_cast<uint32_t*>(to);
3449*0b57cec5SDimitry Andric     uint32_t* _to_end = reinterpret_cast<uint32_t*>(to_end);
3450*0b57cec5SDimitry Andric     uint32_t* _to_nxt = _to;
3451*0b57cec5SDimitry Andric     result r = utf8_to_ucs4(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3452*0b57cec5SDimitry Andric                             _Maxcode_, _Mode_);
3453*0b57cec5SDimitry Andric     frm_nxt = frm + (_frm_nxt - _frm);
3454*0b57cec5SDimitry Andric     to_nxt = to + (_to_nxt - _to);
3455*0b57cec5SDimitry Andric     return r;
3456*0b57cec5SDimitry Andric }
3457*0b57cec5SDimitry Andric 
3458*0b57cec5SDimitry Andric __codecvt_utf8<char32_t>::result
3459*0b57cec5SDimitry Andric __codecvt_utf8<char32_t>::do_unshift(state_type&,
3460*0b57cec5SDimitry Andric     extern_type* to, extern_type*, extern_type*& to_nxt) const
3461*0b57cec5SDimitry Andric {
3462*0b57cec5SDimitry Andric     to_nxt = to;
3463*0b57cec5SDimitry Andric     return noconv;
3464*0b57cec5SDimitry Andric }
3465*0b57cec5SDimitry Andric 
3466*0b57cec5SDimitry Andric int
3467*0b57cec5SDimitry Andric __codecvt_utf8<char32_t>::do_encoding() const  _NOEXCEPT
3468*0b57cec5SDimitry Andric {
3469*0b57cec5SDimitry Andric     return 0;
3470*0b57cec5SDimitry Andric }
3471*0b57cec5SDimitry Andric 
3472*0b57cec5SDimitry Andric bool
3473*0b57cec5SDimitry Andric __codecvt_utf8<char32_t>::do_always_noconv() const  _NOEXCEPT
3474*0b57cec5SDimitry Andric {
3475*0b57cec5SDimitry Andric     return false;
3476*0b57cec5SDimitry Andric }
3477*0b57cec5SDimitry Andric 
3478*0b57cec5SDimitry Andric int
3479*0b57cec5SDimitry Andric __codecvt_utf8<char32_t>::do_length(state_type&,
3480*0b57cec5SDimitry Andric     const extern_type* frm, const extern_type* frm_end, size_t mx) const
3481*0b57cec5SDimitry Andric {
3482*0b57cec5SDimitry Andric     const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3483*0b57cec5SDimitry Andric     const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3484*0b57cec5SDimitry Andric     return utf8_to_ucs4_length(_frm, _frm_end, mx, _Maxcode_, _Mode_);
3485*0b57cec5SDimitry Andric }
3486*0b57cec5SDimitry Andric 
3487*0b57cec5SDimitry Andric int
3488*0b57cec5SDimitry Andric __codecvt_utf8<char32_t>::do_max_length() const  _NOEXCEPT
3489*0b57cec5SDimitry Andric {
3490*0b57cec5SDimitry Andric     if (_Mode_ & consume_header)
3491*0b57cec5SDimitry Andric         return 7;
3492*0b57cec5SDimitry Andric     return 4;
3493*0b57cec5SDimitry Andric }
3494*0b57cec5SDimitry Andric 
3495*0b57cec5SDimitry Andric // __codecvt_utf16<wchar_t, false>
3496*0b57cec5SDimitry Andric 
3497*0b57cec5SDimitry Andric __codecvt_utf16<wchar_t, false>::result
3498*0b57cec5SDimitry Andric __codecvt_utf16<wchar_t, false>::do_out(state_type&,
3499*0b57cec5SDimitry Andric     const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt,
3500*0b57cec5SDimitry Andric     extern_type* to, extern_type* to_end, extern_type*& to_nxt) const
3501*0b57cec5SDimitry Andric {
3502*0b57cec5SDimitry Andric     const uint32_t* _frm = reinterpret_cast<const uint32_t*>(frm);
3503*0b57cec5SDimitry Andric     const uint32_t* _frm_end = reinterpret_cast<const uint32_t*>(frm_end);
3504*0b57cec5SDimitry Andric     const uint32_t* _frm_nxt = _frm;
3505*0b57cec5SDimitry Andric     uint8_t* _to = reinterpret_cast<uint8_t*>(to);
3506*0b57cec5SDimitry Andric     uint8_t* _to_end = reinterpret_cast<uint8_t*>(to_end);
3507*0b57cec5SDimitry Andric     uint8_t* _to_nxt = _to;
3508*0b57cec5SDimitry Andric     result r = ucs4_to_utf16be(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3509*0b57cec5SDimitry Andric                                _Maxcode_, _Mode_);
3510*0b57cec5SDimitry Andric     frm_nxt = frm + (_frm_nxt - _frm);
3511*0b57cec5SDimitry Andric     to_nxt = to + (_to_nxt - _to);
3512*0b57cec5SDimitry Andric     return r;
3513*0b57cec5SDimitry Andric }
3514*0b57cec5SDimitry Andric 
3515*0b57cec5SDimitry Andric __codecvt_utf16<wchar_t, false>::result
3516*0b57cec5SDimitry Andric __codecvt_utf16<wchar_t, false>::do_in(state_type&,
3517*0b57cec5SDimitry Andric     const extern_type* frm, const extern_type* frm_end, const extern_type*& frm_nxt,
3518*0b57cec5SDimitry Andric     intern_type* to, intern_type* to_end, intern_type*& to_nxt) const
3519*0b57cec5SDimitry Andric {
3520*0b57cec5SDimitry Andric     const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3521*0b57cec5SDimitry Andric     const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3522*0b57cec5SDimitry Andric     const uint8_t* _frm_nxt = _frm;
3523*0b57cec5SDimitry Andric     uint32_t* _to = reinterpret_cast<uint32_t*>(to);
3524*0b57cec5SDimitry Andric     uint32_t* _to_end = reinterpret_cast<uint32_t*>(to_end);
3525*0b57cec5SDimitry Andric     uint32_t* _to_nxt = _to;
3526*0b57cec5SDimitry Andric     result r = utf16be_to_ucs4(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3527*0b57cec5SDimitry Andric                                _Maxcode_, _Mode_);
3528*0b57cec5SDimitry Andric     frm_nxt = frm + (_frm_nxt - _frm);
3529*0b57cec5SDimitry Andric     to_nxt = to + (_to_nxt - _to);
3530*0b57cec5SDimitry Andric     return r;
3531*0b57cec5SDimitry Andric }
3532*0b57cec5SDimitry Andric 
3533*0b57cec5SDimitry Andric __codecvt_utf16<wchar_t, false>::result
3534*0b57cec5SDimitry Andric __codecvt_utf16<wchar_t, false>::do_unshift(state_type&,
3535*0b57cec5SDimitry Andric     extern_type* to, extern_type*, extern_type*& to_nxt) const
3536*0b57cec5SDimitry Andric {
3537*0b57cec5SDimitry Andric     to_nxt = to;
3538*0b57cec5SDimitry Andric     return noconv;
3539*0b57cec5SDimitry Andric }
3540*0b57cec5SDimitry Andric 
3541*0b57cec5SDimitry Andric int
3542*0b57cec5SDimitry Andric __codecvt_utf16<wchar_t, false>::do_encoding() const  _NOEXCEPT
3543*0b57cec5SDimitry Andric {
3544*0b57cec5SDimitry Andric     return 0;
3545*0b57cec5SDimitry Andric }
3546*0b57cec5SDimitry Andric 
3547*0b57cec5SDimitry Andric bool
3548*0b57cec5SDimitry Andric __codecvt_utf16<wchar_t, false>::do_always_noconv() const  _NOEXCEPT
3549*0b57cec5SDimitry Andric {
3550*0b57cec5SDimitry Andric     return false;
3551*0b57cec5SDimitry Andric }
3552*0b57cec5SDimitry Andric 
3553*0b57cec5SDimitry Andric int
3554*0b57cec5SDimitry Andric __codecvt_utf16<wchar_t, false>::do_length(state_type&,
3555*0b57cec5SDimitry Andric     const extern_type* frm, const extern_type* frm_end, size_t mx) const
3556*0b57cec5SDimitry Andric {
3557*0b57cec5SDimitry Andric     const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3558*0b57cec5SDimitry Andric     const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3559*0b57cec5SDimitry Andric     return utf16be_to_ucs4_length(_frm, _frm_end, mx, _Maxcode_, _Mode_);
3560*0b57cec5SDimitry Andric }
3561*0b57cec5SDimitry Andric 
3562*0b57cec5SDimitry Andric int
3563*0b57cec5SDimitry Andric __codecvt_utf16<wchar_t, false>::do_max_length() const  _NOEXCEPT
3564*0b57cec5SDimitry Andric {
3565*0b57cec5SDimitry Andric     if (_Mode_ & consume_header)
3566*0b57cec5SDimitry Andric         return 6;
3567*0b57cec5SDimitry Andric     return 4;
3568*0b57cec5SDimitry Andric }
3569*0b57cec5SDimitry Andric 
3570*0b57cec5SDimitry Andric // __codecvt_utf16<wchar_t, true>
3571*0b57cec5SDimitry Andric 
3572*0b57cec5SDimitry Andric __codecvt_utf16<wchar_t, true>::result
3573*0b57cec5SDimitry Andric __codecvt_utf16<wchar_t, true>::do_out(state_type&,
3574*0b57cec5SDimitry Andric     const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt,
3575*0b57cec5SDimitry Andric     extern_type* to, extern_type* to_end, extern_type*& to_nxt) const
3576*0b57cec5SDimitry Andric {
3577*0b57cec5SDimitry Andric     const uint32_t* _frm = reinterpret_cast<const uint32_t*>(frm);
3578*0b57cec5SDimitry Andric     const uint32_t* _frm_end = reinterpret_cast<const uint32_t*>(frm_end);
3579*0b57cec5SDimitry Andric     const uint32_t* _frm_nxt = _frm;
3580*0b57cec5SDimitry Andric     uint8_t* _to = reinterpret_cast<uint8_t*>(to);
3581*0b57cec5SDimitry Andric     uint8_t* _to_end = reinterpret_cast<uint8_t*>(to_end);
3582*0b57cec5SDimitry Andric     uint8_t* _to_nxt = _to;
3583*0b57cec5SDimitry Andric     result r = ucs4_to_utf16le(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3584*0b57cec5SDimitry Andric                                _Maxcode_, _Mode_);
3585*0b57cec5SDimitry Andric     frm_nxt = frm + (_frm_nxt - _frm);
3586*0b57cec5SDimitry Andric     to_nxt = to + (_to_nxt - _to);
3587*0b57cec5SDimitry Andric     return r;
3588*0b57cec5SDimitry Andric }
3589*0b57cec5SDimitry Andric 
3590*0b57cec5SDimitry Andric __codecvt_utf16<wchar_t, true>::result
3591*0b57cec5SDimitry Andric __codecvt_utf16<wchar_t, true>::do_in(state_type&,
3592*0b57cec5SDimitry Andric     const extern_type* frm, const extern_type* frm_end, const extern_type*& frm_nxt,
3593*0b57cec5SDimitry Andric     intern_type* to, intern_type* to_end, intern_type*& to_nxt) const
3594*0b57cec5SDimitry Andric {
3595*0b57cec5SDimitry Andric     const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3596*0b57cec5SDimitry Andric     const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3597*0b57cec5SDimitry Andric     const uint8_t* _frm_nxt = _frm;
3598*0b57cec5SDimitry Andric     uint32_t* _to = reinterpret_cast<uint32_t*>(to);
3599*0b57cec5SDimitry Andric     uint32_t* _to_end = reinterpret_cast<uint32_t*>(to_end);
3600*0b57cec5SDimitry Andric     uint32_t* _to_nxt = _to;
3601*0b57cec5SDimitry Andric     result r = utf16le_to_ucs4(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3602*0b57cec5SDimitry Andric                                _Maxcode_, _Mode_);
3603*0b57cec5SDimitry Andric     frm_nxt = frm + (_frm_nxt - _frm);
3604*0b57cec5SDimitry Andric     to_nxt = to + (_to_nxt - _to);
3605*0b57cec5SDimitry Andric     return r;
3606*0b57cec5SDimitry Andric }
3607*0b57cec5SDimitry Andric 
3608*0b57cec5SDimitry Andric __codecvt_utf16<wchar_t, true>::result
3609*0b57cec5SDimitry Andric __codecvt_utf16<wchar_t, true>::do_unshift(state_type&,
3610*0b57cec5SDimitry Andric     extern_type* to, extern_type*, extern_type*& to_nxt) const
3611*0b57cec5SDimitry Andric {
3612*0b57cec5SDimitry Andric     to_nxt = to;
3613*0b57cec5SDimitry Andric     return noconv;
3614*0b57cec5SDimitry Andric }
3615*0b57cec5SDimitry Andric 
3616*0b57cec5SDimitry Andric int
3617*0b57cec5SDimitry Andric __codecvt_utf16<wchar_t, true>::do_encoding() const  _NOEXCEPT
3618*0b57cec5SDimitry Andric {
3619*0b57cec5SDimitry Andric     return 0;
3620*0b57cec5SDimitry Andric }
3621*0b57cec5SDimitry Andric 
3622*0b57cec5SDimitry Andric bool
3623*0b57cec5SDimitry Andric __codecvt_utf16<wchar_t, true>::do_always_noconv() const  _NOEXCEPT
3624*0b57cec5SDimitry Andric {
3625*0b57cec5SDimitry Andric     return false;
3626*0b57cec5SDimitry Andric }
3627*0b57cec5SDimitry Andric 
3628*0b57cec5SDimitry Andric int
3629*0b57cec5SDimitry Andric __codecvt_utf16<wchar_t, true>::do_length(state_type&,
3630*0b57cec5SDimitry Andric     const extern_type* frm, const extern_type* frm_end, size_t mx) const
3631*0b57cec5SDimitry Andric {
3632*0b57cec5SDimitry Andric     const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3633*0b57cec5SDimitry Andric     const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3634*0b57cec5SDimitry Andric     return utf16le_to_ucs4_length(_frm, _frm_end, mx, _Maxcode_, _Mode_);
3635*0b57cec5SDimitry Andric }
3636*0b57cec5SDimitry Andric 
3637*0b57cec5SDimitry Andric int
3638*0b57cec5SDimitry Andric __codecvt_utf16<wchar_t, true>::do_max_length() const  _NOEXCEPT
3639*0b57cec5SDimitry Andric {
3640*0b57cec5SDimitry Andric     if (_Mode_ & consume_header)
3641*0b57cec5SDimitry Andric         return 6;
3642*0b57cec5SDimitry Andric     return 4;
3643*0b57cec5SDimitry Andric }
3644*0b57cec5SDimitry Andric 
3645*0b57cec5SDimitry Andric // __codecvt_utf16<char16_t, false>
3646*0b57cec5SDimitry Andric 
3647*0b57cec5SDimitry Andric __codecvt_utf16<char16_t, false>::result
3648*0b57cec5SDimitry Andric __codecvt_utf16<char16_t, false>::do_out(state_type&,
3649*0b57cec5SDimitry Andric     const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt,
3650*0b57cec5SDimitry Andric     extern_type* to, extern_type* to_end, extern_type*& to_nxt) const
3651*0b57cec5SDimitry Andric {
3652*0b57cec5SDimitry Andric     const uint16_t* _frm = reinterpret_cast<const uint16_t*>(frm);
3653*0b57cec5SDimitry Andric     const uint16_t* _frm_end = reinterpret_cast<const uint16_t*>(frm_end);
3654*0b57cec5SDimitry Andric     const uint16_t* _frm_nxt = _frm;
3655*0b57cec5SDimitry Andric     uint8_t* _to = reinterpret_cast<uint8_t*>(to);
3656*0b57cec5SDimitry Andric     uint8_t* _to_end = reinterpret_cast<uint8_t*>(to_end);
3657*0b57cec5SDimitry Andric     uint8_t* _to_nxt = _to;
3658*0b57cec5SDimitry Andric     result r = ucs2_to_utf16be(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3659*0b57cec5SDimitry Andric                                _Maxcode_, _Mode_);
3660*0b57cec5SDimitry Andric     frm_nxt = frm + (_frm_nxt - _frm);
3661*0b57cec5SDimitry Andric     to_nxt = to + (_to_nxt - _to);
3662*0b57cec5SDimitry Andric     return r;
3663*0b57cec5SDimitry Andric }
3664*0b57cec5SDimitry Andric 
3665*0b57cec5SDimitry Andric __codecvt_utf16<char16_t, false>::result
3666*0b57cec5SDimitry Andric __codecvt_utf16<char16_t, false>::do_in(state_type&,
3667*0b57cec5SDimitry Andric     const extern_type* frm, const extern_type* frm_end, const extern_type*& frm_nxt,
3668*0b57cec5SDimitry Andric     intern_type* to, intern_type* to_end, intern_type*& to_nxt) const
3669*0b57cec5SDimitry Andric {
3670*0b57cec5SDimitry Andric     const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3671*0b57cec5SDimitry Andric     const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3672*0b57cec5SDimitry Andric     const uint8_t* _frm_nxt = _frm;
3673*0b57cec5SDimitry Andric     uint16_t* _to = reinterpret_cast<uint16_t*>(to);
3674*0b57cec5SDimitry Andric     uint16_t* _to_end = reinterpret_cast<uint16_t*>(to_end);
3675*0b57cec5SDimitry Andric     uint16_t* _to_nxt = _to;
3676*0b57cec5SDimitry Andric     result r = utf16be_to_ucs2(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3677*0b57cec5SDimitry Andric                                _Maxcode_, _Mode_);
3678*0b57cec5SDimitry Andric     frm_nxt = frm + (_frm_nxt - _frm);
3679*0b57cec5SDimitry Andric     to_nxt = to + (_to_nxt - _to);
3680*0b57cec5SDimitry Andric     return r;
3681*0b57cec5SDimitry Andric }
3682*0b57cec5SDimitry Andric 
3683*0b57cec5SDimitry Andric __codecvt_utf16<char16_t, false>::result
3684*0b57cec5SDimitry Andric __codecvt_utf16<char16_t, false>::do_unshift(state_type&,
3685*0b57cec5SDimitry Andric     extern_type* to, extern_type*, extern_type*& to_nxt) const
3686*0b57cec5SDimitry Andric {
3687*0b57cec5SDimitry Andric     to_nxt = to;
3688*0b57cec5SDimitry Andric     return noconv;
3689*0b57cec5SDimitry Andric }
3690*0b57cec5SDimitry Andric 
3691*0b57cec5SDimitry Andric int
3692*0b57cec5SDimitry Andric __codecvt_utf16<char16_t, false>::do_encoding() const  _NOEXCEPT
3693*0b57cec5SDimitry Andric {
3694*0b57cec5SDimitry Andric     return 0;
3695*0b57cec5SDimitry Andric }
3696*0b57cec5SDimitry Andric 
3697*0b57cec5SDimitry Andric bool
3698*0b57cec5SDimitry Andric __codecvt_utf16<char16_t, false>::do_always_noconv() const  _NOEXCEPT
3699*0b57cec5SDimitry Andric {
3700*0b57cec5SDimitry Andric     return false;
3701*0b57cec5SDimitry Andric }
3702*0b57cec5SDimitry Andric 
3703*0b57cec5SDimitry Andric int
3704*0b57cec5SDimitry Andric __codecvt_utf16<char16_t, false>::do_length(state_type&,
3705*0b57cec5SDimitry Andric     const extern_type* frm, const extern_type* frm_end, size_t mx) const
3706*0b57cec5SDimitry Andric {
3707*0b57cec5SDimitry Andric     const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3708*0b57cec5SDimitry Andric     const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3709*0b57cec5SDimitry Andric     return utf16be_to_ucs2_length(_frm, _frm_end, mx, _Maxcode_, _Mode_);
3710*0b57cec5SDimitry Andric }
3711*0b57cec5SDimitry Andric 
3712*0b57cec5SDimitry Andric int
3713*0b57cec5SDimitry Andric __codecvt_utf16<char16_t, false>::do_max_length() const  _NOEXCEPT
3714*0b57cec5SDimitry Andric {
3715*0b57cec5SDimitry Andric     if (_Mode_ & consume_header)
3716*0b57cec5SDimitry Andric         return 4;
3717*0b57cec5SDimitry Andric     return 2;
3718*0b57cec5SDimitry Andric }
3719*0b57cec5SDimitry Andric 
3720*0b57cec5SDimitry Andric // __codecvt_utf16<char16_t, true>
3721*0b57cec5SDimitry Andric 
3722*0b57cec5SDimitry Andric __codecvt_utf16<char16_t, true>::result
3723*0b57cec5SDimitry Andric __codecvt_utf16<char16_t, true>::do_out(state_type&,
3724*0b57cec5SDimitry Andric     const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt,
3725*0b57cec5SDimitry Andric     extern_type* to, extern_type* to_end, extern_type*& to_nxt) const
3726*0b57cec5SDimitry Andric {
3727*0b57cec5SDimitry Andric     const uint16_t* _frm = reinterpret_cast<const uint16_t*>(frm);
3728*0b57cec5SDimitry Andric     const uint16_t* _frm_end = reinterpret_cast<const uint16_t*>(frm_end);
3729*0b57cec5SDimitry Andric     const uint16_t* _frm_nxt = _frm;
3730*0b57cec5SDimitry Andric     uint8_t* _to = reinterpret_cast<uint8_t*>(to);
3731*0b57cec5SDimitry Andric     uint8_t* _to_end = reinterpret_cast<uint8_t*>(to_end);
3732*0b57cec5SDimitry Andric     uint8_t* _to_nxt = _to;
3733*0b57cec5SDimitry Andric     result r = ucs2_to_utf16le(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3734*0b57cec5SDimitry Andric                                _Maxcode_, _Mode_);
3735*0b57cec5SDimitry Andric     frm_nxt = frm + (_frm_nxt - _frm);
3736*0b57cec5SDimitry Andric     to_nxt = to + (_to_nxt - _to);
3737*0b57cec5SDimitry Andric     return r;
3738*0b57cec5SDimitry Andric }
3739*0b57cec5SDimitry Andric 
3740*0b57cec5SDimitry Andric __codecvt_utf16<char16_t, true>::result
3741*0b57cec5SDimitry Andric __codecvt_utf16<char16_t, true>::do_in(state_type&,
3742*0b57cec5SDimitry Andric     const extern_type* frm, const extern_type* frm_end, const extern_type*& frm_nxt,
3743*0b57cec5SDimitry Andric     intern_type* to, intern_type* to_end, intern_type*& to_nxt) const
3744*0b57cec5SDimitry Andric {
3745*0b57cec5SDimitry Andric     const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3746*0b57cec5SDimitry Andric     const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3747*0b57cec5SDimitry Andric     const uint8_t* _frm_nxt = _frm;
3748*0b57cec5SDimitry Andric     uint16_t* _to = reinterpret_cast<uint16_t*>(to);
3749*0b57cec5SDimitry Andric     uint16_t* _to_end = reinterpret_cast<uint16_t*>(to_end);
3750*0b57cec5SDimitry Andric     uint16_t* _to_nxt = _to;
3751*0b57cec5SDimitry Andric     result r = utf16le_to_ucs2(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3752*0b57cec5SDimitry Andric                                _Maxcode_, _Mode_);
3753*0b57cec5SDimitry Andric     frm_nxt = frm + (_frm_nxt - _frm);
3754*0b57cec5SDimitry Andric     to_nxt = to + (_to_nxt - _to);
3755*0b57cec5SDimitry Andric     return r;
3756*0b57cec5SDimitry Andric }
3757*0b57cec5SDimitry Andric 
3758*0b57cec5SDimitry Andric __codecvt_utf16<char16_t, true>::result
3759*0b57cec5SDimitry Andric __codecvt_utf16<char16_t, true>::do_unshift(state_type&,
3760*0b57cec5SDimitry Andric     extern_type* to, extern_type*, extern_type*& to_nxt) const
3761*0b57cec5SDimitry Andric {
3762*0b57cec5SDimitry Andric     to_nxt = to;
3763*0b57cec5SDimitry Andric     return noconv;
3764*0b57cec5SDimitry Andric }
3765*0b57cec5SDimitry Andric 
3766*0b57cec5SDimitry Andric int
3767*0b57cec5SDimitry Andric __codecvt_utf16<char16_t, true>::do_encoding() const  _NOEXCEPT
3768*0b57cec5SDimitry Andric {
3769*0b57cec5SDimitry Andric     return 0;
3770*0b57cec5SDimitry Andric }
3771*0b57cec5SDimitry Andric 
3772*0b57cec5SDimitry Andric bool
3773*0b57cec5SDimitry Andric __codecvt_utf16<char16_t, true>::do_always_noconv() const  _NOEXCEPT
3774*0b57cec5SDimitry Andric {
3775*0b57cec5SDimitry Andric     return false;
3776*0b57cec5SDimitry Andric }
3777*0b57cec5SDimitry Andric 
3778*0b57cec5SDimitry Andric int
3779*0b57cec5SDimitry Andric __codecvt_utf16<char16_t, true>::do_length(state_type&,
3780*0b57cec5SDimitry Andric     const extern_type* frm, const extern_type* frm_end, size_t mx) const
3781*0b57cec5SDimitry Andric {
3782*0b57cec5SDimitry Andric     const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3783*0b57cec5SDimitry Andric     const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3784*0b57cec5SDimitry Andric     return utf16le_to_ucs2_length(_frm, _frm_end, mx, _Maxcode_, _Mode_);
3785*0b57cec5SDimitry Andric }
3786*0b57cec5SDimitry Andric 
3787*0b57cec5SDimitry Andric int
3788*0b57cec5SDimitry Andric __codecvt_utf16<char16_t, true>::do_max_length() const  _NOEXCEPT
3789*0b57cec5SDimitry Andric {
3790*0b57cec5SDimitry Andric     if (_Mode_ & consume_header)
3791*0b57cec5SDimitry Andric         return 4;
3792*0b57cec5SDimitry Andric     return 2;
3793*0b57cec5SDimitry Andric }
3794*0b57cec5SDimitry Andric 
3795*0b57cec5SDimitry Andric // __codecvt_utf16<char32_t, false>
3796*0b57cec5SDimitry Andric 
3797*0b57cec5SDimitry Andric __codecvt_utf16<char32_t, false>::result
3798*0b57cec5SDimitry Andric __codecvt_utf16<char32_t, false>::do_out(state_type&,
3799*0b57cec5SDimitry Andric     const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt,
3800*0b57cec5SDimitry Andric     extern_type* to, extern_type* to_end, extern_type*& to_nxt) const
3801*0b57cec5SDimitry Andric {
3802*0b57cec5SDimitry Andric     const uint32_t* _frm = reinterpret_cast<const uint32_t*>(frm);
3803*0b57cec5SDimitry Andric     const uint32_t* _frm_end = reinterpret_cast<const uint32_t*>(frm_end);
3804*0b57cec5SDimitry Andric     const uint32_t* _frm_nxt = _frm;
3805*0b57cec5SDimitry Andric     uint8_t* _to = reinterpret_cast<uint8_t*>(to);
3806*0b57cec5SDimitry Andric     uint8_t* _to_end = reinterpret_cast<uint8_t*>(to_end);
3807*0b57cec5SDimitry Andric     uint8_t* _to_nxt = _to;
3808*0b57cec5SDimitry Andric     result r = ucs4_to_utf16be(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3809*0b57cec5SDimitry Andric                                _Maxcode_, _Mode_);
3810*0b57cec5SDimitry Andric     frm_nxt = frm + (_frm_nxt - _frm);
3811*0b57cec5SDimitry Andric     to_nxt = to + (_to_nxt - _to);
3812*0b57cec5SDimitry Andric     return r;
3813*0b57cec5SDimitry Andric }
3814*0b57cec5SDimitry Andric 
3815*0b57cec5SDimitry Andric __codecvt_utf16<char32_t, false>::result
3816*0b57cec5SDimitry Andric __codecvt_utf16<char32_t, false>::do_in(state_type&,
3817*0b57cec5SDimitry Andric     const extern_type* frm, const extern_type* frm_end, const extern_type*& frm_nxt,
3818*0b57cec5SDimitry Andric     intern_type* to, intern_type* to_end, intern_type*& to_nxt) const
3819*0b57cec5SDimitry Andric {
3820*0b57cec5SDimitry Andric     const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3821*0b57cec5SDimitry Andric     const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3822*0b57cec5SDimitry Andric     const uint8_t* _frm_nxt = _frm;
3823*0b57cec5SDimitry Andric     uint32_t* _to = reinterpret_cast<uint32_t*>(to);
3824*0b57cec5SDimitry Andric     uint32_t* _to_end = reinterpret_cast<uint32_t*>(to_end);
3825*0b57cec5SDimitry Andric     uint32_t* _to_nxt = _to;
3826*0b57cec5SDimitry Andric     result r = utf16be_to_ucs4(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3827*0b57cec5SDimitry Andric                                _Maxcode_, _Mode_);
3828*0b57cec5SDimitry Andric     frm_nxt = frm + (_frm_nxt - _frm);
3829*0b57cec5SDimitry Andric     to_nxt = to + (_to_nxt - _to);
3830*0b57cec5SDimitry Andric     return r;
3831*0b57cec5SDimitry Andric }
3832*0b57cec5SDimitry Andric 
3833*0b57cec5SDimitry Andric __codecvt_utf16<char32_t, false>::result
3834*0b57cec5SDimitry Andric __codecvt_utf16<char32_t, false>::do_unshift(state_type&,
3835*0b57cec5SDimitry Andric     extern_type* to, extern_type*, extern_type*& to_nxt) const
3836*0b57cec5SDimitry Andric {
3837*0b57cec5SDimitry Andric     to_nxt = to;
3838*0b57cec5SDimitry Andric     return noconv;
3839*0b57cec5SDimitry Andric }
3840*0b57cec5SDimitry Andric 
3841*0b57cec5SDimitry Andric int
3842*0b57cec5SDimitry Andric __codecvt_utf16<char32_t, false>::do_encoding() const  _NOEXCEPT
3843*0b57cec5SDimitry Andric {
3844*0b57cec5SDimitry Andric     return 0;
3845*0b57cec5SDimitry Andric }
3846*0b57cec5SDimitry Andric 
3847*0b57cec5SDimitry Andric bool
3848*0b57cec5SDimitry Andric __codecvt_utf16<char32_t, false>::do_always_noconv() const  _NOEXCEPT
3849*0b57cec5SDimitry Andric {
3850*0b57cec5SDimitry Andric     return false;
3851*0b57cec5SDimitry Andric }
3852*0b57cec5SDimitry Andric 
3853*0b57cec5SDimitry Andric int
3854*0b57cec5SDimitry Andric __codecvt_utf16<char32_t, false>::do_length(state_type&,
3855*0b57cec5SDimitry Andric     const extern_type* frm, const extern_type* frm_end, size_t mx) const
3856*0b57cec5SDimitry Andric {
3857*0b57cec5SDimitry Andric     const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3858*0b57cec5SDimitry Andric     const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3859*0b57cec5SDimitry Andric     return utf16be_to_ucs4_length(_frm, _frm_end, mx, _Maxcode_, _Mode_);
3860*0b57cec5SDimitry Andric }
3861*0b57cec5SDimitry Andric 
3862*0b57cec5SDimitry Andric int
3863*0b57cec5SDimitry Andric __codecvt_utf16<char32_t, false>::do_max_length() const  _NOEXCEPT
3864*0b57cec5SDimitry Andric {
3865*0b57cec5SDimitry Andric     if (_Mode_ & consume_header)
3866*0b57cec5SDimitry Andric         return 6;
3867*0b57cec5SDimitry Andric     return 4;
3868*0b57cec5SDimitry Andric }
3869*0b57cec5SDimitry Andric 
3870*0b57cec5SDimitry Andric // __codecvt_utf16<char32_t, true>
3871*0b57cec5SDimitry Andric 
3872*0b57cec5SDimitry Andric __codecvt_utf16<char32_t, true>::result
3873*0b57cec5SDimitry Andric __codecvt_utf16<char32_t, true>::do_out(state_type&,
3874*0b57cec5SDimitry Andric     const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt,
3875*0b57cec5SDimitry Andric     extern_type* to, extern_type* to_end, extern_type*& to_nxt) const
3876*0b57cec5SDimitry Andric {
3877*0b57cec5SDimitry Andric     const uint32_t* _frm = reinterpret_cast<const uint32_t*>(frm);
3878*0b57cec5SDimitry Andric     const uint32_t* _frm_end = reinterpret_cast<const uint32_t*>(frm_end);
3879*0b57cec5SDimitry Andric     const uint32_t* _frm_nxt = _frm;
3880*0b57cec5SDimitry Andric     uint8_t* _to = reinterpret_cast<uint8_t*>(to);
3881*0b57cec5SDimitry Andric     uint8_t* _to_end = reinterpret_cast<uint8_t*>(to_end);
3882*0b57cec5SDimitry Andric     uint8_t* _to_nxt = _to;
3883*0b57cec5SDimitry Andric     result r = ucs4_to_utf16le(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3884*0b57cec5SDimitry Andric                                _Maxcode_, _Mode_);
3885*0b57cec5SDimitry Andric     frm_nxt = frm + (_frm_nxt - _frm);
3886*0b57cec5SDimitry Andric     to_nxt = to + (_to_nxt - _to);
3887*0b57cec5SDimitry Andric     return r;
3888*0b57cec5SDimitry Andric }
3889*0b57cec5SDimitry Andric 
3890*0b57cec5SDimitry Andric __codecvt_utf16<char32_t, true>::result
3891*0b57cec5SDimitry Andric __codecvt_utf16<char32_t, true>::do_in(state_type&,
3892*0b57cec5SDimitry Andric     const extern_type* frm, const extern_type* frm_end, const extern_type*& frm_nxt,
3893*0b57cec5SDimitry Andric     intern_type* to, intern_type* to_end, intern_type*& to_nxt) const
3894*0b57cec5SDimitry Andric {
3895*0b57cec5SDimitry Andric     const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3896*0b57cec5SDimitry Andric     const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3897*0b57cec5SDimitry Andric     const uint8_t* _frm_nxt = _frm;
3898*0b57cec5SDimitry Andric     uint32_t* _to = reinterpret_cast<uint32_t*>(to);
3899*0b57cec5SDimitry Andric     uint32_t* _to_end = reinterpret_cast<uint32_t*>(to_end);
3900*0b57cec5SDimitry Andric     uint32_t* _to_nxt = _to;
3901*0b57cec5SDimitry Andric     result r = utf16le_to_ucs4(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3902*0b57cec5SDimitry Andric                                _Maxcode_, _Mode_);
3903*0b57cec5SDimitry Andric     frm_nxt = frm + (_frm_nxt - _frm);
3904*0b57cec5SDimitry Andric     to_nxt = to + (_to_nxt - _to);
3905*0b57cec5SDimitry Andric     return r;
3906*0b57cec5SDimitry Andric }
3907*0b57cec5SDimitry Andric 
3908*0b57cec5SDimitry Andric __codecvt_utf16<char32_t, true>::result
3909*0b57cec5SDimitry Andric __codecvt_utf16<char32_t, true>::do_unshift(state_type&,
3910*0b57cec5SDimitry Andric     extern_type* to, extern_type*, extern_type*& to_nxt) const
3911*0b57cec5SDimitry Andric {
3912*0b57cec5SDimitry Andric     to_nxt = to;
3913*0b57cec5SDimitry Andric     return noconv;
3914*0b57cec5SDimitry Andric }
3915*0b57cec5SDimitry Andric 
3916*0b57cec5SDimitry Andric int
3917*0b57cec5SDimitry Andric __codecvt_utf16<char32_t, true>::do_encoding() const  _NOEXCEPT
3918*0b57cec5SDimitry Andric {
3919*0b57cec5SDimitry Andric     return 0;
3920*0b57cec5SDimitry Andric }
3921*0b57cec5SDimitry Andric 
3922*0b57cec5SDimitry Andric bool
3923*0b57cec5SDimitry Andric __codecvt_utf16<char32_t, true>::do_always_noconv() const  _NOEXCEPT
3924*0b57cec5SDimitry Andric {
3925*0b57cec5SDimitry Andric     return false;
3926*0b57cec5SDimitry Andric }
3927*0b57cec5SDimitry Andric 
3928*0b57cec5SDimitry Andric int
3929*0b57cec5SDimitry Andric __codecvt_utf16<char32_t, true>::do_length(state_type&,
3930*0b57cec5SDimitry Andric     const extern_type* frm, const extern_type* frm_end, size_t mx) const
3931*0b57cec5SDimitry Andric {
3932*0b57cec5SDimitry Andric     const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3933*0b57cec5SDimitry Andric     const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3934*0b57cec5SDimitry Andric     return utf16le_to_ucs4_length(_frm, _frm_end, mx, _Maxcode_, _Mode_);
3935*0b57cec5SDimitry Andric }
3936*0b57cec5SDimitry Andric 
3937*0b57cec5SDimitry Andric int
3938*0b57cec5SDimitry Andric __codecvt_utf16<char32_t, true>::do_max_length() const  _NOEXCEPT
3939*0b57cec5SDimitry Andric {
3940*0b57cec5SDimitry Andric     if (_Mode_ & consume_header)
3941*0b57cec5SDimitry Andric         return 6;
3942*0b57cec5SDimitry Andric     return 4;
3943*0b57cec5SDimitry Andric }
3944*0b57cec5SDimitry Andric 
3945*0b57cec5SDimitry Andric // __codecvt_utf8_utf16<wchar_t>
3946*0b57cec5SDimitry Andric 
3947*0b57cec5SDimitry Andric __codecvt_utf8_utf16<wchar_t>::result
3948*0b57cec5SDimitry Andric __codecvt_utf8_utf16<wchar_t>::do_out(state_type&,
3949*0b57cec5SDimitry Andric     const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt,
3950*0b57cec5SDimitry Andric     extern_type* to, extern_type* to_end, extern_type*& to_nxt) const
3951*0b57cec5SDimitry Andric {
3952*0b57cec5SDimitry Andric     const uint32_t* _frm = reinterpret_cast<const uint32_t*>(frm);
3953*0b57cec5SDimitry Andric     const uint32_t* _frm_end = reinterpret_cast<const uint32_t*>(frm_end);
3954*0b57cec5SDimitry Andric     const uint32_t* _frm_nxt = _frm;
3955*0b57cec5SDimitry Andric     uint8_t* _to = reinterpret_cast<uint8_t*>(to);
3956*0b57cec5SDimitry Andric     uint8_t* _to_end = reinterpret_cast<uint8_t*>(to_end);
3957*0b57cec5SDimitry Andric     uint8_t* _to_nxt = _to;
3958*0b57cec5SDimitry Andric     result r = utf16_to_utf8(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3959*0b57cec5SDimitry Andric                              _Maxcode_, _Mode_);
3960*0b57cec5SDimitry Andric     frm_nxt = frm + (_frm_nxt - _frm);
3961*0b57cec5SDimitry Andric     to_nxt = to + (_to_nxt - _to);
3962*0b57cec5SDimitry Andric     return r;
3963*0b57cec5SDimitry Andric }
3964*0b57cec5SDimitry Andric 
3965*0b57cec5SDimitry Andric __codecvt_utf8_utf16<wchar_t>::result
3966*0b57cec5SDimitry Andric __codecvt_utf8_utf16<wchar_t>::do_in(state_type&,
3967*0b57cec5SDimitry Andric     const extern_type* frm, const extern_type* frm_end, const extern_type*& frm_nxt,
3968*0b57cec5SDimitry Andric     intern_type* to, intern_type* to_end, intern_type*& to_nxt) const
3969*0b57cec5SDimitry Andric {
3970*0b57cec5SDimitry Andric     const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3971*0b57cec5SDimitry Andric     const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3972*0b57cec5SDimitry Andric     const uint8_t* _frm_nxt = _frm;
3973*0b57cec5SDimitry Andric     uint32_t* _to = reinterpret_cast<uint32_t*>(to);
3974*0b57cec5SDimitry Andric     uint32_t* _to_end = reinterpret_cast<uint32_t*>(to_end);
3975*0b57cec5SDimitry Andric     uint32_t* _to_nxt = _to;
3976*0b57cec5SDimitry Andric     result r = utf8_to_utf16(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3977*0b57cec5SDimitry Andric                              _Maxcode_, _Mode_);
3978*0b57cec5SDimitry Andric     frm_nxt = frm + (_frm_nxt - _frm);
3979*0b57cec5SDimitry Andric     to_nxt = to + (_to_nxt - _to);
3980*0b57cec5SDimitry Andric     return r;
3981*0b57cec5SDimitry Andric }
3982*0b57cec5SDimitry Andric 
3983*0b57cec5SDimitry Andric __codecvt_utf8_utf16<wchar_t>::result
3984*0b57cec5SDimitry Andric __codecvt_utf8_utf16<wchar_t>::do_unshift(state_type&,
3985*0b57cec5SDimitry Andric     extern_type* to, extern_type*, extern_type*& to_nxt) const
3986*0b57cec5SDimitry Andric {
3987*0b57cec5SDimitry Andric     to_nxt = to;
3988*0b57cec5SDimitry Andric     return noconv;
3989*0b57cec5SDimitry Andric }
3990*0b57cec5SDimitry Andric 
3991*0b57cec5SDimitry Andric int
3992*0b57cec5SDimitry Andric __codecvt_utf8_utf16<wchar_t>::do_encoding() const  _NOEXCEPT
3993*0b57cec5SDimitry Andric {
3994*0b57cec5SDimitry Andric     return 0;
3995*0b57cec5SDimitry Andric }
3996*0b57cec5SDimitry Andric 
3997*0b57cec5SDimitry Andric bool
3998*0b57cec5SDimitry Andric __codecvt_utf8_utf16<wchar_t>::do_always_noconv() const  _NOEXCEPT
3999*0b57cec5SDimitry Andric {
4000*0b57cec5SDimitry Andric     return false;
4001*0b57cec5SDimitry Andric }
4002*0b57cec5SDimitry Andric 
4003*0b57cec5SDimitry Andric int
4004*0b57cec5SDimitry Andric __codecvt_utf8_utf16<wchar_t>::do_length(state_type&,
4005*0b57cec5SDimitry Andric     const extern_type* frm, const extern_type* frm_end, size_t mx) const
4006*0b57cec5SDimitry Andric {
4007*0b57cec5SDimitry Andric     const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
4008*0b57cec5SDimitry Andric     const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
4009*0b57cec5SDimitry Andric     return utf8_to_utf16_length(_frm, _frm_end, mx, _Maxcode_, _Mode_);
4010*0b57cec5SDimitry Andric }
4011*0b57cec5SDimitry Andric 
4012*0b57cec5SDimitry Andric int
4013*0b57cec5SDimitry Andric __codecvt_utf8_utf16<wchar_t>::do_max_length() const  _NOEXCEPT
4014*0b57cec5SDimitry Andric {
4015*0b57cec5SDimitry Andric     if (_Mode_ & consume_header)
4016*0b57cec5SDimitry Andric         return 7;
4017*0b57cec5SDimitry Andric     return 4;
4018*0b57cec5SDimitry Andric }
4019*0b57cec5SDimitry Andric 
4020*0b57cec5SDimitry Andric // __codecvt_utf8_utf16<char16_t>
4021*0b57cec5SDimitry Andric 
4022*0b57cec5SDimitry Andric __codecvt_utf8_utf16<char16_t>::result
4023*0b57cec5SDimitry Andric __codecvt_utf8_utf16<char16_t>::do_out(state_type&,
4024*0b57cec5SDimitry Andric     const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt,
4025*0b57cec5SDimitry Andric     extern_type* to, extern_type* to_end, extern_type*& to_nxt) const
4026*0b57cec5SDimitry Andric {
4027*0b57cec5SDimitry Andric     const uint16_t* _frm = reinterpret_cast<const uint16_t*>(frm);
4028*0b57cec5SDimitry Andric     const uint16_t* _frm_end = reinterpret_cast<const uint16_t*>(frm_end);
4029*0b57cec5SDimitry Andric     const uint16_t* _frm_nxt = _frm;
4030*0b57cec5SDimitry Andric     uint8_t* _to = reinterpret_cast<uint8_t*>(to);
4031*0b57cec5SDimitry Andric     uint8_t* _to_end = reinterpret_cast<uint8_t*>(to_end);
4032*0b57cec5SDimitry Andric     uint8_t* _to_nxt = _to;
4033*0b57cec5SDimitry Andric     result r = utf16_to_utf8(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
4034*0b57cec5SDimitry Andric                              _Maxcode_, _Mode_);
4035*0b57cec5SDimitry Andric     frm_nxt = frm + (_frm_nxt - _frm);
4036*0b57cec5SDimitry Andric     to_nxt = to + (_to_nxt - _to);
4037*0b57cec5SDimitry Andric     return r;
4038*0b57cec5SDimitry Andric }
4039*0b57cec5SDimitry Andric 
4040*0b57cec5SDimitry Andric __codecvt_utf8_utf16<char16_t>::result
4041*0b57cec5SDimitry Andric __codecvt_utf8_utf16<char16_t>::do_in(state_type&,
4042*0b57cec5SDimitry Andric     const extern_type* frm, const extern_type* frm_end, const extern_type*& frm_nxt,
4043*0b57cec5SDimitry Andric     intern_type* to, intern_type* to_end, intern_type*& to_nxt) const
4044*0b57cec5SDimitry Andric {
4045*0b57cec5SDimitry Andric     const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
4046*0b57cec5SDimitry Andric     const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
4047*0b57cec5SDimitry Andric     const uint8_t* _frm_nxt = _frm;
4048*0b57cec5SDimitry Andric     uint16_t* _to = reinterpret_cast<uint16_t*>(to);
4049*0b57cec5SDimitry Andric     uint16_t* _to_end = reinterpret_cast<uint16_t*>(to_end);
4050*0b57cec5SDimitry Andric     uint16_t* _to_nxt = _to;
4051*0b57cec5SDimitry Andric     result r = utf8_to_utf16(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
4052*0b57cec5SDimitry Andric                              _Maxcode_, _Mode_);
4053*0b57cec5SDimitry Andric     frm_nxt = frm + (_frm_nxt - _frm);
4054*0b57cec5SDimitry Andric     to_nxt = to + (_to_nxt - _to);
4055*0b57cec5SDimitry Andric     return r;
4056*0b57cec5SDimitry Andric }
4057*0b57cec5SDimitry Andric 
4058*0b57cec5SDimitry Andric __codecvt_utf8_utf16<char16_t>::result
4059*0b57cec5SDimitry Andric __codecvt_utf8_utf16<char16_t>::do_unshift(state_type&,
4060*0b57cec5SDimitry Andric     extern_type* to, extern_type*, extern_type*& to_nxt) const
4061*0b57cec5SDimitry Andric {
4062*0b57cec5SDimitry Andric     to_nxt = to;
4063*0b57cec5SDimitry Andric     return noconv;
4064*0b57cec5SDimitry Andric }
4065*0b57cec5SDimitry Andric 
4066*0b57cec5SDimitry Andric int
4067*0b57cec5SDimitry Andric __codecvt_utf8_utf16<char16_t>::do_encoding() const  _NOEXCEPT
4068*0b57cec5SDimitry Andric {
4069*0b57cec5SDimitry Andric     return 0;
4070*0b57cec5SDimitry Andric }
4071*0b57cec5SDimitry Andric 
4072*0b57cec5SDimitry Andric bool
4073*0b57cec5SDimitry Andric __codecvt_utf8_utf16<char16_t>::do_always_noconv() const  _NOEXCEPT
4074*0b57cec5SDimitry Andric {
4075*0b57cec5SDimitry Andric     return false;
4076*0b57cec5SDimitry Andric }
4077*0b57cec5SDimitry Andric 
4078*0b57cec5SDimitry Andric int
4079*0b57cec5SDimitry Andric __codecvt_utf8_utf16<char16_t>::do_length(state_type&,
4080*0b57cec5SDimitry Andric     const extern_type* frm, const extern_type* frm_end, size_t mx) const
4081*0b57cec5SDimitry Andric {
4082*0b57cec5SDimitry Andric     const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
4083*0b57cec5SDimitry Andric     const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
4084*0b57cec5SDimitry Andric     return utf8_to_utf16_length(_frm, _frm_end, mx, _Maxcode_, _Mode_);
4085*0b57cec5SDimitry Andric }
4086*0b57cec5SDimitry Andric 
4087*0b57cec5SDimitry Andric int
4088*0b57cec5SDimitry Andric __codecvt_utf8_utf16<char16_t>::do_max_length() const  _NOEXCEPT
4089*0b57cec5SDimitry Andric {
4090*0b57cec5SDimitry Andric     if (_Mode_ & consume_header)
4091*0b57cec5SDimitry Andric         return 7;
4092*0b57cec5SDimitry Andric     return 4;
4093*0b57cec5SDimitry Andric }
4094*0b57cec5SDimitry Andric 
4095*0b57cec5SDimitry Andric // __codecvt_utf8_utf16<char32_t>
4096*0b57cec5SDimitry Andric 
4097*0b57cec5SDimitry Andric __codecvt_utf8_utf16<char32_t>::result
4098*0b57cec5SDimitry Andric __codecvt_utf8_utf16<char32_t>::do_out(state_type&,
4099*0b57cec5SDimitry Andric     const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt,
4100*0b57cec5SDimitry Andric     extern_type* to, extern_type* to_end, extern_type*& to_nxt) const
4101*0b57cec5SDimitry Andric {
4102*0b57cec5SDimitry Andric     const uint32_t* _frm = reinterpret_cast<const uint32_t*>(frm);
4103*0b57cec5SDimitry Andric     const uint32_t* _frm_end = reinterpret_cast<const uint32_t*>(frm_end);
4104*0b57cec5SDimitry Andric     const uint32_t* _frm_nxt = _frm;
4105*0b57cec5SDimitry Andric     uint8_t* _to = reinterpret_cast<uint8_t*>(to);
4106*0b57cec5SDimitry Andric     uint8_t* _to_end = reinterpret_cast<uint8_t*>(to_end);
4107*0b57cec5SDimitry Andric     uint8_t* _to_nxt = _to;
4108*0b57cec5SDimitry Andric     result r = utf16_to_utf8(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
4109*0b57cec5SDimitry Andric                              _Maxcode_, _Mode_);
4110*0b57cec5SDimitry Andric     frm_nxt = frm + (_frm_nxt - _frm);
4111*0b57cec5SDimitry Andric     to_nxt = to + (_to_nxt - _to);
4112*0b57cec5SDimitry Andric     return r;
4113*0b57cec5SDimitry Andric }
4114*0b57cec5SDimitry Andric 
4115*0b57cec5SDimitry Andric __codecvt_utf8_utf16<char32_t>::result
4116*0b57cec5SDimitry Andric __codecvt_utf8_utf16<char32_t>::do_in(state_type&,
4117*0b57cec5SDimitry Andric     const extern_type* frm, const extern_type* frm_end, const extern_type*& frm_nxt,
4118*0b57cec5SDimitry Andric     intern_type* to, intern_type* to_end, intern_type*& to_nxt) const
4119*0b57cec5SDimitry Andric {
4120*0b57cec5SDimitry Andric     const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
4121*0b57cec5SDimitry Andric     const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
4122*0b57cec5SDimitry Andric     const uint8_t* _frm_nxt = _frm;
4123*0b57cec5SDimitry Andric     uint32_t* _to = reinterpret_cast<uint32_t*>(to);
4124*0b57cec5SDimitry Andric     uint32_t* _to_end = reinterpret_cast<uint32_t*>(to_end);
4125*0b57cec5SDimitry Andric     uint32_t* _to_nxt = _to;
4126*0b57cec5SDimitry Andric     result r = utf8_to_utf16(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
4127*0b57cec5SDimitry Andric                              _Maxcode_, _Mode_);
4128*0b57cec5SDimitry Andric     frm_nxt = frm + (_frm_nxt - _frm);
4129*0b57cec5SDimitry Andric     to_nxt = to + (_to_nxt - _to);
4130*0b57cec5SDimitry Andric     return r;
4131*0b57cec5SDimitry Andric }
4132*0b57cec5SDimitry Andric 
4133*0b57cec5SDimitry Andric __codecvt_utf8_utf16<char32_t>::result
4134*0b57cec5SDimitry Andric __codecvt_utf8_utf16<char32_t>::do_unshift(state_type&,
4135*0b57cec5SDimitry Andric     extern_type* to, extern_type*, extern_type*& to_nxt) const
4136*0b57cec5SDimitry Andric {
4137*0b57cec5SDimitry Andric     to_nxt = to;
4138*0b57cec5SDimitry Andric     return noconv;
4139*0b57cec5SDimitry Andric }
4140*0b57cec5SDimitry Andric 
4141*0b57cec5SDimitry Andric int
4142*0b57cec5SDimitry Andric __codecvt_utf8_utf16<char32_t>::do_encoding() const  _NOEXCEPT
4143*0b57cec5SDimitry Andric {
4144*0b57cec5SDimitry Andric     return 0;
4145*0b57cec5SDimitry Andric }
4146*0b57cec5SDimitry Andric 
4147*0b57cec5SDimitry Andric bool
4148*0b57cec5SDimitry Andric __codecvt_utf8_utf16<char32_t>::do_always_noconv() const  _NOEXCEPT
4149*0b57cec5SDimitry Andric {
4150*0b57cec5SDimitry Andric     return false;
4151*0b57cec5SDimitry Andric }
4152*0b57cec5SDimitry Andric 
4153*0b57cec5SDimitry Andric int
4154*0b57cec5SDimitry Andric __codecvt_utf8_utf16<char32_t>::do_length(state_type&,
4155*0b57cec5SDimitry Andric     const extern_type* frm, const extern_type* frm_end, size_t mx) const
4156*0b57cec5SDimitry Andric {
4157*0b57cec5SDimitry Andric     const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
4158*0b57cec5SDimitry Andric     const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
4159*0b57cec5SDimitry Andric     return utf8_to_utf16_length(_frm, _frm_end, mx, _Maxcode_, _Mode_);
4160*0b57cec5SDimitry Andric }
4161*0b57cec5SDimitry Andric 
4162*0b57cec5SDimitry Andric int
4163*0b57cec5SDimitry Andric __codecvt_utf8_utf16<char32_t>::do_max_length() const  _NOEXCEPT
4164*0b57cec5SDimitry Andric {
4165*0b57cec5SDimitry Andric     if (_Mode_ & consume_header)
4166*0b57cec5SDimitry Andric         return 7;
4167*0b57cec5SDimitry Andric     return 4;
4168*0b57cec5SDimitry Andric }
4169*0b57cec5SDimitry Andric 
4170*0b57cec5SDimitry Andric // __narrow_to_utf8<16>
4171*0b57cec5SDimitry Andric 
4172*0b57cec5SDimitry Andric __narrow_to_utf8<16>::~__narrow_to_utf8()
4173*0b57cec5SDimitry Andric {
4174*0b57cec5SDimitry Andric }
4175*0b57cec5SDimitry Andric 
4176*0b57cec5SDimitry Andric // __narrow_to_utf8<32>
4177*0b57cec5SDimitry Andric 
4178*0b57cec5SDimitry Andric __narrow_to_utf8<32>::~__narrow_to_utf8()
4179*0b57cec5SDimitry Andric {
4180*0b57cec5SDimitry Andric }
4181*0b57cec5SDimitry Andric 
4182*0b57cec5SDimitry Andric // __widen_from_utf8<16>
4183*0b57cec5SDimitry Andric 
4184*0b57cec5SDimitry Andric __widen_from_utf8<16>::~__widen_from_utf8()
4185*0b57cec5SDimitry Andric {
4186*0b57cec5SDimitry Andric }
4187*0b57cec5SDimitry Andric 
4188*0b57cec5SDimitry Andric // __widen_from_utf8<32>
4189*0b57cec5SDimitry Andric 
4190*0b57cec5SDimitry Andric __widen_from_utf8<32>::~__widen_from_utf8()
4191*0b57cec5SDimitry Andric {
4192*0b57cec5SDimitry Andric }
4193*0b57cec5SDimitry Andric 
4194*0b57cec5SDimitry Andric 
4195*0b57cec5SDimitry Andric static bool checked_string_to_wchar_convert(wchar_t& dest,
4196*0b57cec5SDimitry Andric                                             const char* ptr,
4197*0b57cec5SDimitry Andric                                             locale_t loc) {
4198*0b57cec5SDimitry Andric   if (*ptr == '\0')
4199*0b57cec5SDimitry Andric     return false;
4200*0b57cec5SDimitry Andric   mbstate_t mb = {};
4201*0b57cec5SDimitry Andric   wchar_t out;
4202*0b57cec5SDimitry Andric   size_t ret = __libcpp_mbrtowc_l(&out, ptr, strlen(ptr), &mb, loc);
4203*0b57cec5SDimitry Andric   if (ret == static_cast<size_t>(-1) || ret == static_cast<size_t>(-2)) {
4204*0b57cec5SDimitry Andric     return false;
4205*0b57cec5SDimitry Andric   }
4206*0b57cec5SDimitry Andric   dest = out;
4207*0b57cec5SDimitry Andric   return true;
4208*0b57cec5SDimitry Andric }
4209*0b57cec5SDimitry Andric 
4210*0b57cec5SDimitry Andric static bool checked_string_to_char_convert(char& dest,
4211*0b57cec5SDimitry Andric                                            const char* ptr,
4212*0b57cec5SDimitry Andric                                            locale_t __loc) {
4213*0b57cec5SDimitry Andric   if (*ptr == '\0')
4214*0b57cec5SDimitry Andric     return false;
4215*0b57cec5SDimitry Andric   if (!ptr[1]) {
4216*0b57cec5SDimitry Andric     dest = *ptr;
4217*0b57cec5SDimitry Andric     return true;
4218*0b57cec5SDimitry Andric   }
4219*0b57cec5SDimitry Andric   // First convert the MBS into a wide char then attempt to narrow it using
4220*0b57cec5SDimitry Andric   // wctob_l.
4221*0b57cec5SDimitry Andric   wchar_t wout;
4222*0b57cec5SDimitry Andric   if (!checked_string_to_wchar_convert(wout, ptr, __loc))
4223*0b57cec5SDimitry Andric     return false;
4224*0b57cec5SDimitry Andric   int res;
4225*0b57cec5SDimitry Andric   if ((res = __libcpp_wctob_l(wout, __loc)) != char_traits<char>::eof()) {
4226*0b57cec5SDimitry Andric     dest = res;
4227*0b57cec5SDimitry Andric     return true;
4228*0b57cec5SDimitry Andric   }
4229*0b57cec5SDimitry Andric   // FIXME: Work around specific multibyte sequences that we can reasonable
4230*0b57cec5SDimitry Andric   // translate into a different single byte.
4231*0b57cec5SDimitry Andric   switch (wout) {
4232*0b57cec5SDimitry Andric   case L'\u202F': // narrow non-breaking space
4233*0b57cec5SDimitry Andric   case L'\u00A0': // non-breaking space
4234*0b57cec5SDimitry Andric     dest = ' ';
4235*0b57cec5SDimitry Andric     return true;
4236*0b57cec5SDimitry Andric   default:
4237*0b57cec5SDimitry Andric     return false;
4238*0b57cec5SDimitry Andric   }
4239*0b57cec5SDimitry Andric   _LIBCPP_UNREACHABLE();
4240*0b57cec5SDimitry Andric }
4241*0b57cec5SDimitry Andric 
4242*0b57cec5SDimitry Andric 
4243*0b57cec5SDimitry Andric // numpunct<char> && numpunct<wchar_t>
4244*0b57cec5SDimitry Andric 
4245*0b57cec5SDimitry Andric locale::id numpunct< char  >::id;
4246*0b57cec5SDimitry Andric locale::id numpunct<wchar_t>::id;
4247*0b57cec5SDimitry Andric 
4248*0b57cec5SDimitry Andric numpunct<char>::numpunct(size_t refs)
4249*0b57cec5SDimitry Andric     : locale::facet(refs),
4250*0b57cec5SDimitry Andric       __decimal_point_('.'),
4251*0b57cec5SDimitry Andric       __thousands_sep_(',')
4252*0b57cec5SDimitry Andric {
4253*0b57cec5SDimitry Andric }
4254*0b57cec5SDimitry Andric 
4255*0b57cec5SDimitry Andric numpunct<wchar_t>::numpunct(size_t refs)
4256*0b57cec5SDimitry Andric     : locale::facet(refs),
4257*0b57cec5SDimitry Andric       __decimal_point_(L'.'),
4258*0b57cec5SDimitry Andric       __thousands_sep_(L',')
4259*0b57cec5SDimitry Andric {
4260*0b57cec5SDimitry Andric }
4261*0b57cec5SDimitry Andric 
4262*0b57cec5SDimitry Andric numpunct<char>::~numpunct()
4263*0b57cec5SDimitry Andric {
4264*0b57cec5SDimitry Andric }
4265*0b57cec5SDimitry Andric 
4266*0b57cec5SDimitry Andric numpunct<wchar_t>::~numpunct()
4267*0b57cec5SDimitry Andric {
4268*0b57cec5SDimitry Andric }
4269*0b57cec5SDimitry Andric 
4270*0b57cec5SDimitry Andric  char   numpunct< char  >::do_decimal_point() const {return __decimal_point_;}
4271*0b57cec5SDimitry Andric wchar_t numpunct<wchar_t>::do_decimal_point() const {return __decimal_point_;}
4272*0b57cec5SDimitry Andric 
4273*0b57cec5SDimitry Andric  char   numpunct< char  >::do_thousands_sep() const {return __thousands_sep_;}
4274*0b57cec5SDimitry Andric wchar_t numpunct<wchar_t>::do_thousands_sep() const {return __thousands_sep_;}
4275*0b57cec5SDimitry Andric 
4276*0b57cec5SDimitry Andric string numpunct< char  >::do_grouping() const {return __grouping_;}
4277*0b57cec5SDimitry Andric string numpunct<wchar_t>::do_grouping() const {return __grouping_;}
4278*0b57cec5SDimitry Andric 
4279*0b57cec5SDimitry Andric  string numpunct< char  >::do_truename() const {return "true";}
4280*0b57cec5SDimitry Andric wstring numpunct<wchar_t>::do_truename() const {return L"true";}
4281*0b57cec5SDimitry Andric 
4282*0b57cec5SDimitry Andric  string numpunct< char  >::do_falsename() const {return "false";}
4283*0b57cec5SDimitry Andric wstring numpunct<wchar_t>::do_falsename() const {return L"false";}
4284*0b57cec5SDimitry Andric 
4285*0b57cec5SDimitry Andric // numpunct_byname<char>
4286*0b57cec5SDimitry Andric 
4287*0b57cec5SDimitry Andric numpunct_byname<char>::numpunct_byname(const char* nm, size_t refs)
4288*0b57cec5SDimitry Andric     : numpunct<char>(refs)
4289*0b57cec5SDimitry Andric {
4290*0b57cec5SDimitry Andric     __init(nm);
4291*0b57cec5SDimitry Andric }
4292*0b57cec5SDimitry Andric 
4293*0b57cec5SDimitry Andric numpunct_byname<char>::numpunct_byname(const string& nm, size_t refs)
4294*0b57cec5SDimitry Andric     : numpunct<char>(refs)
4295*0b57cec5SDimitry Andric {
4296*0b57cec5SDimitry Andric     __init(nm.c_str());
4297*0b57cec5SDimitry Andric }
4298*0b57cec5SDimitry Andric 
4299*0b57cec5SDimitry Andric numpunct_byname<char>::~numpunct_byname()
4300*0b57cec5SDimitry Andric {
4301*0b57cec5SDimitry Andric }
4302*0b57cec5SDimitry Andric 
4303*0b57cec5SDimitry Andric void
4304*0b57cec5SDimitry Andric numpunct_byname<char>::__init(const char* nm)
4305*0b57cec5SDimitry Andric {
4306*0b57cec5SDimitry Andric     if (strcmp(nm, "C") != 0)
4307*0b57cec5SDimitry Andric     {
4308*0b57cec5SDimitry Andric         __libcpp_unique_locale loc(nm);
4309*0b57cec5SDimitry Andric         if (!loc)
4310*0b57cec5SDimitry Andric             __throw_runtime_error("numpunct_byname<char>::numpunct_byname"
4311*0b57cec5SDimitry Andric                                 " failed to construct for " + string(nm));
4312*0b57cec5SDimitry Andric 
4313*0b57cec5SDimitry Andric         lconv* lc = __libcpp_localeconv_l(loc.get());
4314*0b57cec5SDimitry Andric         checked_string_to_char_convert(__decimal_point_, lc->decimal_point,
4315*0b57cec5SDimitry Andric                                        loc.get());
4316*0b57cec5SDimitry Andric         checked_string_to_char_convert(__thousands_sep_, lc->thousands_sep,
4317*0b57cec5SDimitry Andric                                        loc.get());
4318*0b57cec5SDimitry Andric         __grouping_ = lc->grouping;
4319*0b57cec5SDimitry Andric         // localization for truename and falsename is not available
4320*0b57cec5SDimitry Andric     }
4321*0b57cec5SDimitry Andric }
4322*0b57cec5SDimitry Andric 
4323*0b57cec5SDimitry Andric // numpunct_byname<wchar_t>
4324*0b57cec5SDimitry Andric 
4325*0b57cec5SDimitry Andric numpunct_byname<wchar_t>::numpunct_byname(const char* nm, size_t refs)
4326*0b57cec5SDimitry Andric     : numpunct<wchar_t>(refs)
4327*0b57cec5SDimitry Andric {
4328*0b57cec5SDimitry Andric     __init(nm);
4329*0b57cec5SDimitry Andric }
4330*0b57cec5SDimitry Andric 
4331*0b57cec5SDimitry Andric numpunct_byname<wchar_t>::numpunct_byname(const string& nm, size_t refs)
4332*0b57cec5SDimitry Andric     : numpunct<wchar_t>(refs)
4333*0b57cec5SDimitry Andric {
4334*0b57cec5SDimitry Andric     __init(nm.c_str());
4335*0b57cec5SDimitry Andric }
4336*0b57cec5SDimitry Andric 
4337*0b57cec5SDimitry Andric numpunct_byname<wchar_t>::~numpunct_byname()
4338*0b57cec5SDimitry Andric {
4339*0b57cec5SDimitry Andric }
4340*0b57cec5SDimitry Andric 
4341*0b57cec5SDimitry Andric void
4342*0b57cec5SDimitry Andric numpunct_byname<wchar_t>::__init(const char* nm)
4343*0b57cec5SDimitry Andric {
4344*0b57cec5SDimitry Andric     if (strcmp(nm, "C") != 0)
4345*0b57cec5SDimitry Andric     {
4346*0b57cec5SDimitry Andric         __libcpp_unique_locale loc(nm);
4347*0b57cec5SDimitry Andric         if (!loc)
4348*0b57cec5SDimitry Andric             __throw_runtime_error("numpunct_byname<wchar_t>::numpunct_byname"
4349*0b57cec5SDimitry Andric                                 " failed to construct for " + string(nm));
4350*0b57cec5SDimitry Andric 
4351*0b57cec5SDimitry Andric         lconv* lc = __libcpp_localeconv_l(loc.get());
4352*0b57cec5SDimitry Andric         checked_string_to_wchar_convert(__decimal_point_, lc->decimal_point,
4353*0b57cec5SDimitry Andric                                         loc.get());
4354*0b57cec5SDimitry Andric         checked_string_to_wchar_convert(__thousands_sep_, lc->thousands_sep,
4355*0b57cec5SDimitry Andric                                         loc.get());
4356*0b57cec5SDimitry Andric         __grouping_ = lc->grouping;
4357*0b57cec5SDimitry Andric         // localization for truename and falsename is not available
4358*0b57cec5SDimitry Andric     }
4359*0b57cec5SDimitry Andric }
4360*0b57cec5SDimitry Andric 
4361*0b57cec5SDimitry Andric // num_get helpers
4362*0b57cec5SDimitry Andric 
4363*0b57cec5SDimitry Andric int
4364*0b57cec5SDimitry Andric __num_get_base::__get_base(ios_base& iob)
4365*0b57cec5SDimitry Andric {
4366*0b57cec5SDimitry Andric     ios_base::fmtflags __basefield = iob.flags() & ios_base::basefield;
4367*0b57cec5SDimitry Andric     if (__basefield == ios_base::oct)
4368*0b57cec5SDimitry Andric         return 8;
4369*0b57cec5SDimitry Andric     else if (__basefield == ios_base::hex)
4370*0b57cec5SDimitry Andric         return 16;
4371*0b57cec5SDimitry Andric     else if (__basefield == 0)
4372*0b57cec5SDimitry Andric         return 0;
4373*0b57cec5SDimitry Andric     return 10;
4374*0b57cec5SDimitry Andric }
4375*0b57cec5SDimitry Andric 
4376*0b57cec5SDimitry Andric const char __num_get_base::__src[33] = "0123456789abcdefABCDEFxX+-pPiInN";
4377*0b57cec5SDimitry Andric 
4378*0b57cec5SDimitry Andric void
4379*0b57cec5SDimitry Andric __check_grouping(const string& __grouping, unsigned* __g, unsigned* __g_end,
4380*0b57cec5SDimitry Andric                  ios_base::iostate& __err)
4381*0b57cec5SDimitry Andric {
4382*0b57cec5SDimitry Andric //  if the grouping pattern is empty _or_ there are no grouping bits, then do nothing
4383*0b57cec5SDimitry Andric //  we always have at least a single entry in [__g, __g_end); the end of the input sequence
4384*0b57cec5SDimitry Andric 	if (__grouping.size() != 0 && __g_end - __g > 1)
4385*0b57cec5SDimitry Andric     {
4386*0b57cec5SDimitry Andric         reverse(__g, __g_end);
4387*0b57cec5SDimitry Andric         const char* __ig = __grouping.data();
4388*0b57cec5SDimitry Andric         const char* __eg = __ig + __grouping.size();
4389*0b57cec5SDimitry Andric         for (unsigned* __r = __g; __r < __g_end-1; ++__r)
4390*0b57cec5SDimitry Andric         {
4391*0b57cec5SDimitry Andric             if (0 < *__ig && *__ig < numeric_limits<char>::max())
4392*0b57cec5SDimitry Andric             {
4393*0b57cec5SDimitry Andric                 if (static_cast<unsigned>(*__ig) != *__r)
4394*0b57cec5SDimitry Andric                 {
4395*0b57cec5SDimitry Andric                     __err = ios_base::failbit;
4396*0b57cec5SDimitry Andric                     return;
4397*0b57cec5SDimitry Andric                 }
4398*0b57cec5SDimitry Andric             }
4399*0b57cec5SDimitry Andric             if (__eg - __ig > 1)
4400*0b57cec5SDimitry Andric                 ++__ig;
4401*0b57cec5SDimitry Andric         }
4402*0b57cec5SDimitry Andric         if (0 < *__ig && *__ig < numeric_limits<char>::max())
4403*0b57cec5SDimitry Andric         {
4404*0b57cec5SDimitry Andric             if (static_cast<unsigned>(*__ig) < __g_end[-1] || __g_end[-1] == 0)
4405*0b57cec5SDimitry Andric                 __err = ios_base::failbit;
4406*0b57cec5SDimitry Andric         }
4407*0b57cec5SDimitry Andric     }
4408*0b57cec5SDimitry Andric }
4409*0b57cec5SDimitry Andric 
4410*0b57cec5SDimitry Andric void
4411*0b57cec5SDimitry Andric __num_put_base::__format_int(char* __fmtp, const char* __len, bool __signd,
4412*0b57cec5SDimitry Andric                              ios_base::fmtflags __flags)
4413*0b57cec5SDimitry Andric {
4414*0b57cec5SDimitry Andric     if (__flags & ios_base::showpos)
4415*0b57cec5SDimitry Andric         *__fmtp++ = '+';
4416*0b57cec5SDimitry Andric     if (__flags & ios_base::showbase)
4417*0b57cec5SDimitry Andric         *__fmtp++ = '#';
4418*0b57cec5SDimitry Andric     while(*__len)
4419*0b57cec5SDimitry Andric         *__fmtp++ = *__len++;
4420*0b57cec5SDimitry Andric     if ((__flags & ios_base::basefield) == ios_base::oct)
4421*0b57cec5SDimitry Andric         *__fmtp = 'o';
4422*0b57cec5SDimitry Andric     else if ((__flags & ios_base::basefield) == ios_base::hex)
4423*0b57cec5SDimitry Andric     {
4424*0b57cec5SDimitry Andric         if (__flags & ios_base::uppercase)
4425*0b57cec5SDimitry Andric             *__fmtp = 'X';
4426*0b57cec5SDimitry Andric         else
4427*0b57cec5SDimitry Andric             *__fmtp = 'x';
4428*0b57cec5SDimitry Andric     }
4429*0b57cec5SDimitry Andric     else if (__signd)
4430*0b57cec5SDimitry Andric         *__fmtp = 'd';
4431*0b57cec5SDimitry Andric     else
4432*0b57cec5SDimitry Andric         *__fmtp = 'u';
4433*0b57cec5SDimitry Andric }
4434*0b57cec5SDimitry Andric 
4435*0b57cec5SDimitry Andric bool
4436*0b57cec5SDimitry Andric __num_put_base::__format_float(char* __fmtp, const char* __len,
4437*0b57cec5SDimitry Andric                                ios_base::fmtflags __flags)
4438*0b57cec5SDimitry Andric {
4439*0b57cec5SDimitry Andric     bool specify_precision = true;
4440*0b57cec5SDimitry Andric     if (__flags & ios_base::showpos)
4441*0b57cec5SDimitry Andric         *__fmtp++ = '+';
4442*0b57cec5SDimitry Andric     if (__flags & ios_base::showpoint)
4443*0b57cec5SDimitry Andric         *__fmtp++ = '#';
4444*0b57cec5SDimitry Andric     ios_base::fmtflags floatfield = __flags & ios_base::floatfield;
4445*0b57cec5SDimitry Andric     bool uppercase = (__flags & ios_base::uppercase) != 0;
4446*0b57cec5SDimitry Andric     if (floatfield == (ios_base::fixed | ios_base::scientific))
4447*0b57cec5SDimitry Andric         specify_precision = false;
4448*0b57cec5SDimitry Andric     else
4449*0b57cec5SDimitry Andric     {
4450*0b57cec5SDimitry Andric         *__fmtp++ = '.';
4451*0b57cec5SDimitry Andric         *__fmtp++ = '*';
4452*0b57cec5SDimitry Andric     }
4453*0b57cec5SDimitry Andric     while(*__len)
4454*0b57cec5SDimitry Andric         *__fmtp++ = *__len++;
4455*0b57cec5SDimitry Andric     if (floatfield == ios_base::fixed)
4456*0b57cec5SDimitry Andric     {
4457*0b57cec5SDimitry Andric         if (uppercase)
4458*0b57cec5SDimitry Andric             *__fmtp = 'F';
4459*0b57cec5SDimitry Andric         else
4460*0b57cec5SDimitry Andric             *__fmtp = 'f';
4461*0b57cec5SDimitry Andric     }
4462*0b57cec5SDimitry Andric     else if (floatfield == ios_base::scientific)
4463*0b57cec5SDimitry Andric     {
4464*0b57cec5SDimitry Andric         if (uppercase)
4465*0b57cec5SDimitry Andric             *__fmtp = 'E';
4466*0b57cec5SDimitry Andric         else
4467*0b57cec5SDimitry Andric             *__fmtp = 'e';
4468*0b57cec5SDimitry Andric     }
4469*0b57cec5SDimitry Andric     else if (floatfield == (ios_base::fixed | ios_base::scientific))
4470*0b57cec5SDimitry Andric     {
4471*0b57cec5SDimitry Andric         if (uppercase)
4472*0b57cec5SDimitry Andric             *__fmtp = 'A';
4473*0b57cec5SDimitry Andric         else
4474*0b57cec5SDimitry Andric             *__fmtp = 'a';
4475*0b57cec5SDimitry Andric     }
4476*0b57cec5SDimitry Andric     else
4477*0b57cec5SDimitry Andric     {
4478*0b57cec5SDimitry Andric         if (uppercase)
4479*0b57cec5SDimitry Andric             *__fmtp = 'G';
4480*0b57cec5SDimitry Andric         else
4481*0b57cec5SDimitry Andric             *__fmtp = 'g';
4482*0b57cec5SDimitry Andric     }
4483*0b57cec5SDimitry Andric     return specify_precision;
4484*0b57cec5SDimitry Andric }
4485*0b57cec5SDimitry Andric 
4486*0b57cec5SDimitry Andric char*
4487*0b57cec5SDimitry Andric __num_put_base::__identify_padding(char* __nb, char* __ne,
4488*0b57cec5SDimitry Andric                                    const ios_base& __iob)
4489*0b57cec5SDimitry Andric {
4490*0b57cec5SDimitry Andric     switch (__iob.flags() & ios_base::adjustfield)
4491*0b57cec5SDimitry Andric     {
4492*0b57cec5SDimitry Andric     case ios_base::internal:
4493*0b57cec5SDimitry Andric         if (__nb[0] == '-' || __nb[0] == '+')
4494*0b57cec5SDimitry Andric             return __nb+1;
4495*0b57cec5SDimitry Andric         if (__ne - __nb >= 2 && __nb[0] == '0'
4496*0b57cec5SDimitry Andric                             && (__nb[1] == 'x' || __nb[1] == 'X'))
4497*0b57cec5SDimitry Andric             return __nb+2;
4498*0b57cec5SDimitry Andric         break;
4499*0b57cec5SDimitry Andric     case ios_base::left:
4500*0b57cec5SDimitry Andric         return __ne;
4501*0b57cec5SDimitry Andric     case ios_base::right:
4502*0b57cec5SDimitry Andric     default:
4503*0b57cec5SDimitry Andric         break;
4504*0b57cec5SDimitry Andric     }
4505*0b57cec5SDimitry Andric     return __nb;
4506*0b57cec5SDimitry Andric }
4507*0b57cec5SDimitry Andric 
4508*0b57cec5SDimitry Andric // time_get
4509*0b57cec5SDimitry Andric 
4510*0b57cec5SDimitry Andric static
4511*0b57cec5SDimitry Andric string*
4512*0b57cec5SDimitry Andric init_weeks()
4513*0b57cec5SDimitry Andric {
4514*0b57cec5SDimitry Andric     static string weeks[14];
4515*0b57cec5SDimitry Andric     weeks[0]  = "Sunday";
4516*0b57cec5SDimitry Andric     weeks[1]  = "Monday";
4517*0b57cec5SDimitry Andric     weeks[2]  = "Tuesday";
4518*0b57cec5SDimitry Andric     weeks[3]  = "Wednesday";
4519*0b57cec5SDimitry Andric     weeks[4]  = "Thursday";
4520*0b57cec5SDimitry Andric     weeks[5]  = "Friday";
4521*0b57cec5SDimitry Andric     weeks[6]  = "Saturday";
4522*0b57cec5SDimitry Andric     weeks[7]  = "Sun";
4523*0b57cec5SDimitry Andric     weeks[8]  = "Mon";
4524*0b57cec5SDimitry Andric     weeks[9]  = "Tue";
4525*0b57cec5SDimitry Andric     weeks[10] = "Wed";
4526*0b57cec5SDimitry Andric     weeks[11] = "Thu";
4527*0b57cec5SDimitry Andric     weeks[12] = "Fri";
4528*0b57cec5SDimitry Andric     weeks[13] = "Sat";
4529*0b57cec5SDimitry Andric     return weeks;
4530*0b57cec5SDimitry Andric }
4531*0b57cec5SDimitry Andric 
4532*0b57cec5SDimitry Andric static
4533*0b57cec5SDimitry Andric wstring*
4534*0b57cec5SDimitry Andric init_wweeks()
4535*0b57cec5SDimitry Andric {
4536*0b57cec5SDimitry Andric     static wstring weeks[14];
4537*0b57cec5SDimitry Andric     weeks[0]  = L"Sunday";
4538*0b57cec5SDimitry Andric     weeks[1]  = L"Monday";
4539*0b57cec5SDimitry Andric     weeks[2]  = L"Tuesday";
4540*0b57cec5SDimitry Andric     weeks[3]  = L"Wednesday";
4541*0b57cec5SDimitry Andric     weeks[4]  = L"Thursday";
4542*0b57cec5SDimitry Andric     weeks[5]  = L"Friday";
4543*0b57cec5SDimitry Andric     weeks[6]  = L"Saturday";
4544*0b57cec5SDimitry Andric     weeks[7]  = L"Sun";
4545*0b57cec5SDimitry Andric     weeks[8]  = L"Mon";
4546*0b57cec5SDimitry Andric     weeks[9]  = L"Tue";
4547*0b57cec5SDimitry Andric     weeks[10] = L"Wed";
4548*0b57cec5SDimitry Andric     weeks[11] = L"Thu";
4549*0b57cec5SDimitry Andric     weeks[12] = L"Fri";
4550*0b57cec5SDimitry Andric     weeks[13] = L"Sat";
4551*0b57cec5SDimitry Andric     return weeks;
4552*0b57cec5SDimitry Andric }
4553*0b57cec5SDimitry Andric 
4554*0b57cec5SDimitry Andric template <>
4555*0b57cec5SDimitry Andric const string*
4556*0b57cec5SDimitry Andric __time_get_c_storage<char>::__weeks() const
4557*0b57cec5SDimitry Andric {
4558*0b57cec5SDimitry Andric     static const string* weeks = init_weeks();
4559*0b57cec5SDimitry Andric     return weeks;
4560*0b57cec5SDimitry Andric }
4561*0b57cec5SDimitry Andric 
4562*0b57cec5SDimitry Andric template <>
4563*0b57cec5SDimitry Andric const wstring*
4564*0b57cec5SDimitry Andric __time_get_c_storage<wchar_t>::__weeks() const
4565*0b57cec5SDimitry Andric {
4566*0b57cec5SDimitry Andric     static const wstring* weeks = init_wweeks();
4567*0b57cec5SDimitry Andric     return weeks;
4568*0b57cec5SDimitry Andric }
4569*0b57cec5SDimitry Andric 
4570*0b57cec5SDimitry Andric static
4571*0b57cec5SDimitry Andric string*
4572*0b57cec5SDimitry Andric init_months()
4573*0b57cec5SDimitry Andric {
4574*0b57cec5SDimitry Andric     static string months[24];
4575*0b57cec5SDimitry Andric     months[0]  = "January";
4576*0b57cec5SDimitry Andric     months[1]  = "February";
4577*0b57cec5SDimitry Andric     months[2]  = "March";
4578*0b57cec5SDimitry Andric     months[3]  = "April";
4579*0b57cec5SDimitry Andric     months[4]  = "May";
4580*0b57cec5SDimitry Andric     months[5]  = "June";
4581*0b57cec5SDimitry Andric     months[6]  = "July";
4582*0b57cec5SDimitry Andric     months[7]  = "August";
4583*0b57cec5SDimitry Andric     months[8]  = "September";
4584*0b57cec5SDimitry Andric     months[9]  = "October";
4585*0b57cec5SDimitry Andric     months[10] = "November";
4586*0b57cec5SDimitry Andric     months[11] = "December";
4587*0b57cec5SDimitry Andric     months[12] = "Jan";
4588*0b57cec5SDimitry Andric     months[13] = "Feb";
4589*0b57cec5SDimitry Andric     months[14] = "Mar";
4590*0b57cec5SDimitry Andric     months[15] = "Apr";
4591*0b57cec5SDimitry Andric     months[16] = "May";
4592*0b57cec5SDimitry Andric     months[17] = "Jun";
4593*0b57cec5SDimitry Andric     months[18] = "Jul";
4594*0b57cec5SDimitry Andric     months[19] = "Aug";
4595*0b57cec5SDimitry Andric     months[20] = "Sep";
4596*0b57cec5SDimitry Andric     months[21] = "Oct";
4597*0b57cec5SDimitry Andric     months[22] = "Nov";
4598*0b57cec5SDimitry Andric     months[23] = "Dec";
4599*0b57cec5SDimitry Andric     return months;
4600*0b57cec5SDimitry Andric }
4601*0b57cec5SDimitry Andric 
4602*0b57cec5SDimitry Andric static
4603*0b57cec5SDimitry Andric wstring*
4604*0b57cec5SDimitry Andric init_wmonths()
4605*0b57cec5SDimitry Andric {
4606*0b57cec5SDimitry Andric     static wstring months[24];
4607*0b57cec5SDimitry Andric     months[0]  = L"January";
4608*0b57cec5SDimitry Andric     months[1]  = L"February";
4609*0b57cec5SDimitry Andric     months[2]  = L"March";
4610*0b57cec5SDimitry Andric     months[3]  = L"April";
4611*0b57cec5SDimitry Andric     months[4]  = L"May";
4612*0b57cec5SDimitry Andric     months[5]  = L"June";
4613*0b57cec5SDimitry Andric     months[6]  = L"July";
4614*0b57cec5SDimitry Andric     months[7]  = L"August";
4615*0b57cec5SDimitry Andric     months[8]  = L"September";
4616*0b57cec5SDimitry Andric     months[9]  = L"October";
4617*0b57cec5SDimitry Andric     months[10] = L"November";
4618*0b57cec5SDimitry Andric     months[11] = L"December";
4619*0b57cec5SDimitry Andric     months[12] = L"Jan";
4620*0b57cec5SDimitry Andric     months[13] = L"Feb";
4621*0b57cec5SDimitry Andric     months[14] = L"Mar";
4622*0b57cec5SDimitry Andric     months[15] = L"Apr";
4623*0b57cec5SDimitry Andric     months[16] = L"May";
4624*0b57cec5SDimitry Andric     months[17] = L"Jun";
4625*0b57cec5SDimitry Andric     months[18] = L"Jul";
4626*0b57cec5SDimitry Andric     months[19] = L"Aug";
4627*0b57cec5SDimitry Andric     months[20] = L"Sep";
4628*0b57cec5SDimitry Andric     months[21] = L"Oct";
4629*0b57cec5SDimitry Andric     months[22] = L"Nov";
4630*0b57cec5SDimitry Andric     months[23] = L"Dec";
4631*0b57cec5SDimitry Andric     return months;
4632*0b57cec5SDimitry Andric }
4633*0b57cec5SDimitry Andric 
4634*0b57cec5SDimitry Andric template <>
4635*0b57cec5SDimitry Andric const string*
4636*0b57cec5SDimitry Andric __time_get_c_storage<char>::__months() const
4637*0b57cec5SDimitry Andric {
4638*0b57cec5SDimitry Andric     static const string* months = init_months();
4639*0b57cec5SDimitry Andric     return months;
4640*0b57cec5SDimitry Andric }
4641*0b57cec5SDimitry Andric 
4642*0b57cec5SDimitry Andric template <>
4643*0b57cec5SDimitry Andric const wstring*
4644*0b57cec5SDimitry Andric __time_get_c_storage<wchar_t>::__months() const
4645*0b57cec5SDimitry Andric {
4646*0b57cec5SDimitry Andric     static const wstring* months = init_wmonths();
4647*0b57cec5SDimitry Andric     return months;
4648*0b57cec5SDimitry Andric }
4649*0b57cec5SDimitry Andric 
4650*0b57cec5SDimitry Andric static
4651*0b57cec5SDimitry Andric string*
4652*0b57cec5SDimitry Andric init_am_pm()
4653*0b57cec5SDimitry Andric {
4654*0b57cec5SDimitry Andric     static string am_pm[2];
4655*0b57cec5SDimitry Andric     am_pm[0]  = "AM";
4656*0b57cec5SDimitry Andric     am_pm[1]  = "PM";
4657*0b57cec5SDimitry Andric     return am_pm;
4658*0b57cec5SDimitry Andric }
4659*0b57cec5SDimitry Andric 
4660*0b57cec5SDimitry Andric static
4661*0b57cec5SDimitry Andric wstring*
4662*0b57cec5SDimitry Andric init_wam_pm()
4663*0b57cec5SDimitry Andric {
4664*0b57cec5SDimitry Andric     static wstring am_pm[2];
4665*0b57cec5SDimitry Andric     am_pm[0]  = L"AM";
4666*0b57cec5SDimitry Andric     am_pm[1]  = L"PM";
4667*0b57cec5SDimitry Andric     return am_pm;
4668*0b57cec5SDimitry Andric }
4669*0b57cec5SDimitry Andric 
4670*0b57cec5SDimitry Andric template <>
4671*0b57cec5SDimitry Andric const string*
4672*0b57cec5SDimitry Andric __time_get_c_storage<char>::__am_pm() const
4673*0b57cec5SDimitry Andric {
4674*0b57cec5SDimitry Andric     static const string* am_pm = init_am_pm();
4675*0b57cec5SDimitry Andric     return am_pm;
4676*0b57cec5SDimitry Andric }
4677*0b57cec5SDimitry Andric 
4678*0b57cec5SDimitry Andric template <>
4679*0b57cec5SDimitry Andric const wstring*
4680*0b57cec5SDimitry Andric __time_get_c_storage<wchar_t>::__am_pm() const
4681*0b57cec5SDimitry Andric {
4682*0b57cec5SDimitry Andric     static const wstring* am_pm = init_wam_pm();
4683*0b57cec5SDimitry Andric     return am_pm;
4684*0b57cec5SDimitry Andric }
4685*0b57cec5SDimitry Andric 
4686*0b57cec5SDimitry Andric template <>
4687*0b57cec5SDimitry Andric const string&
4688*0b57cec5SDimitry Andric __time_get_c_storage<char>::__x() const
4689*0b57cec5SDimitry Andric {
4690*0b57cec5SDimitry Andric     static string s("%m/%d/%y");
4691*0b57cec5SDimitry Andric     return s;
4692*0b57cec5SDimitry Andric }
4693*0b57cec5SDimitry Andric 
4694*0b57cec5SDimitry Andric template <>
4695*0b57cec5SDimitry Andric const wstring&
4696*0b57cec5SDimitry Andric __time_get_c_storage<wchar_t>::__x() const
4697*0b57cec5SDimitry Andric {
4698*0b57cec5SDimitry Andric     static wstring s(L"%m/%d/%y");
4699*0b57cec5SDimitry Andric     return s;
4700*0b57cec5SDimitry Andric }
4701*0b57cec5SDimitry Andric 
4702*0b57cec5SDimitry Andric template <>
4703*0b57cec5SDimitry Andric const string&
4704*0b57cec5SDimitry Andric __time_get_c_storage<char>::__X() const
4705*0b57cec5SDimitry Andric {
4706*0b57cec5SDimitry Andric     static string s("%H:%M:%S");
4707*0b57cec5SDimitry Andric     return s;
4708*0b57cec5SDimitry Andric }
4709*0b57cec5SDimitry Andric 
4710*0b57cec5SDimitry Andric template <>
4711*0b57cec5SDimitry Andric const wstring&
4712*0b57cec5SDimitry Andric __time_get_c_storage<wchar_t>::__X() const
4713*0b57cec5SDimitry Andric {
4714*0b57cec5SDimitry Andric     static wstring s(L"%H:%M:%S");
4715*0b57cec5SDimitry Andric     return s;
4716*0b57cec5SDimitry Andric }
4717*0b57cec5SDimitry Andric 
4718*0b57cec5SDimitry Andric template <>
4719*0b57cec5SDimitry Andric const string&
4720*0b57cec5SDimitry Andric __time_get_c_storage<char>::__c() const
4721*0b57cec5SDimitry Andric {
4722*0b57cec5SDimitry Andric     static string s("%a %b %d %H:%M:%S %Y");
4723*0b57cec5SDimitry Andric     return s;
4724*0b57cec5SDimitry Andric }
4725*0b57cec5SDimitry Andric 
4726*0b57cec5SDimitry Andric template <>
4727*0b57cec5SDimitry Andric const wstring&
4728*0b57cec5SDimitry Andric __time_get_c_storage<wchar_t>::__c() const
4729*0b57cec5SDimitry Andric {
4730*0b57cec5SDimitry Andric     static wstring s(L"%a %b %d %H:%M:%S %Y");
4731*0b57cec5SDimitry Andric     return s;
4732*0b57cec5SDimitry Andric }
4733*0b57cec5SDimitry Andric 
4734*0b57cec5SDimitry Andric template <>
4735*0b57cec5SDimitry Andric const string&
4736*0b57cec5SDimitry Andric __time_get_c_storage<char>::__r() const
4737*0b57cec5SDimitry Andric {
4738*0b57cec5SDimitry Andric     static string s("%I:%M:%S %p");
4739*0b57cec5SDimitry Andric     return s;
4740*0b57cec5SDimitry Andric }
4741*0b57cec5SDimitry Andric 
4742*0b57cec5SDimitry Andric template <>
4743*0b57cec5SDimitry Andric const wstring&
4744*0b57cec5SDimitry Andric __time_get_c_storage<wchar_t>::__r() const
4745*0b57cec5SDimitry Andric {
4746*0b57cec5SDimitry Andric     static wstring s(L"%I:%M:%S %p");
4747*0b57cec5SDimitry Andric     return s;
4748*0b57cec5SDimitry Andric }
4749*0b57cec5SDimitry Andric 
4750*0b57cec5SDimitry Andric // time_get_byname
4751*0b57cec5SDimitry Andric 
4752*0b57cec5SDimitry Andric __time_get::__time_get(const char* nm)
4753*0b57cec5SDimitry Andric     : __loc_(newlocale(LC_ALL_MASK, nm, 0))
4754*0b57cec5SDimitry Andric {
4755*0b57cec5SDimitry Andric     if (__loc_ == 0)
4756*0b57cec5SDimitry Andric         __throw_runtime_error("time_get_byname"
4757*0b57cec5SDimitry Andric                             " failed to construct for " + string(nm));
4758*0b57cec5SDimitry Andric }
4759*0b57cec5SDimitry Andric 
4760*0b57cec5SDimitry Andric __time_get::__time_get(const string& nm)
4761*0b57cec5SDimitry Andric     : __loc_(newlocale(LC_ALL_MASK, nm.c_str(), 0))
4762*0b57cec5SDimitry Andric {
4763*0b57cec5SDimitry Andric     if (__loc_ == 0)
4764*0b57cec5SDimitry Andric         __throw_runtime_error("time_get_byname"
4765*0b57cec5SDimitry Andric                             " failed to construct for " + nm);
4766*0b57cec5SDimitry Andric }
4767*0b57cec5SDimitry Andric 
4768*0b57cec5SDimitry Andric __time_get::~__time_get()
4769*0b57cec5SDimitry Andric {
4770*0b57cec5SDimitry Andric     freelocale(__loc_);
4771*0b57cec5SDimitry Andric }
4772*0b57cec5SDimitry Andric #if defined(__clang__)
4773*0b57cec5SDimitry Andric #pragma clang diagnostic ignored "-Wmissing-field-initializers"
4774*0b57cec5SDimitry Andric #endif
4775*0b57cec5SDimitry Andric #if defined(__GNUG__)
4776*0b57cec5SDimitry Andric #pragma GCC   diagnostic ignored "-Wmissing-field-initializers"
4777*0b57cec5SDimitry Andric #endif
4778*0b57cec5SDimitry Andric 
4779*0b57cec5SDimitry Andric template <>
4780*0b57cec5SDimitry Andric string
4781*0b57cec5SDimitry Andric __time_get_storage<char>::__analyze(char fmt, const ctype<char>& ct)
4782*0b57cec5SDimitry Andric {
4783*0b57cec5SDimitry Andric     tm t = {0};
4784*0b57cec5SDimitry Andric     t.tm_sec = 59;
4785*0b57cec5SDimitry Andric     t.tm_min = 55;
4786*0b57cec5SDimitry Andric     t.tm_hour = 23;
4787*0b57cec5SDimitry Andric     t.tm_mday = 31;
4788*0b57cec5SDimitry Andric     t.tm_mon = 11;
4789*0b57cec5SDimitry Andric     t.tm_year = 161;
4790*0b57cec5SDimitry Andric     t.tm_wday = 6;
4791*0b57cec5SDimitry Andric     t.tm_yday = 364;
4792*0b57cec5SDimitry Andric     t.tm_isdst = -1;
4793*0b57cec5SDimitry Andric     char buf[100];
4794*0b57cec5SDimitry Andric     char f[3] = {0};
4795*0b57cec5SDimitry Andric     f[0] = '%';
4796*0b57cec5SDimitry Andric     f[1] = fmt;
4797*0b57cec5SDimitry Andric     size_t n = strftime_l(buf, countof(buf), f, &t, __loc_);
4798*0b57cec5SDimitry Andric     char* bb = buf;
4799*0b57cec5SDimitry Andric     char* be = buf + n;
4800*0b57cec5SDimitry Andric     string result;
4801*0b57cec5SDimitry Andric     while (bb != be)
4802*0b57cec5SDimitry Andric     {
4803*0b57cec5SDimitry Andric         if (ct.is(ctype_base::space, *bb))
4804*0b57cec5SDimitry Andric         {
4805*0b57cec5SDimitry Andric             result.push_back(' ');
4806*0b57cec5SDimitry Andric             for (++bb; bb != be && ct.is(ctype_base::space, *bb); ++bb)
4807*0b57cec5SDimitry Andric                 ;
4808*0b57cec5SDimitry Andric             continue;
4809*0b57cec5SDimitry Andric         }
4810*0b57cec5SDimitry Andric         char* w = bb;
4811*0b57cec5SDimitry Andric         ios_base::iostate err = ios_base::goodbit;
4812*0b57cec5SDimitry Andric         ptrdiff_t i = __scan_keyword(w, be, this->__weeks_, this->__weeks_+14,
4813*0b57cec5SDimitry Andric                                ct, err, false)
4814*0b57cec5SDimitry Andric                                - this->__weeks_;
4815*0b57cec5SDimitry Andric         if (i < 14)
4816*0b57cec5SDimitry Andric         {
4817*0b57cec5SDimitry Andric             result.push_back('%');
4818*0b57cec5SDimitry Andric             if (i < 7)
4819*0b57cec5SDimitry Andric                 result.push_back('A');
4820*0b57cec5SDimitry Andric             else
4821*0b57cec5SDimitry Andric                 result.push_back('a');
4822*0b57cec5SDimitry Andric             bb = w;
4823*0b57cec5SDimitry Andric             continue;
4824*0b57cec5SDimitry Andric         }
4825*0b57cec5SDimitry Andric         w = bb;
4826*0b57cec5SDimitry Andric         i = __scan_keyword(w, be, this->__months_, this->__months_+24,
4827*0b57cec5SDimitry Andric                            ct, err, false)
4828*0b57cec5SDimitry Andric                            - this->__months_;
4829*0b57cec5SDimitry Andric         if (i < 24)
4830*0b57cec5SDimitry Andric         {
4831*0b57cec5SDimitry Andric             result.push_back('%');
4832*0b57cec5SDimitry Andric             if (i < 12)
4833*0b57cec5SDimitry Andric                 result.push_back('B');
4834*0b57cec5SDimitry Andric             else
4835*0b57cec5SDimitry Andric                 result.push_back('b');
4836*0b57cec5SDimitry Andric             if (fmt == 'x' && ct.is(ctype_base::digit, this->__months_[i][0]))
4837*0b57cec5SDimitry Andric                 result.back() = 'm';
4838*0b57cec5SDimitry Andric             bb = w;
4839*0b57cec5SDimitry Andric             continue;
4840*0b57cec5SDimitry Andric         }
4841*0b57cec5SDimitry Andric         if (this->__am_pm_[0].size() + this->__am_pm_[1].size() > 0)
4842*0b57cec5SDimitry Andric         {
4843*0b57cec5SDimitry Andric             w = bb;
4844*0b57cec5SDimitry Andric             i = __scan_keyword(w, be, this->__am_pm_, this->__am_pm_+2,
4845*0b57cec5SDimitry Andric                                ct, err, false) - this->__am_pm_;
4846*0b57cec5SDimitry Andric             if (i < 2)
4847*0b57cec5SDimitry Andric             {
4848*0b57cec5SDimitry Andric                 result.push_back('%');
4849*0b57cec5SDimitry Andric                 result.push_back('p');
4850*0b57cec5SDimitry Andric                 bb = w;
4851*0b57cec5SDimitry Andric                 continue;
4852*0b57cec5SDimitry Andric             }
4853*0b57cec5SDimitry Andric         }
4854*0b57cec5SDimitry Andric         w = bb;
4855*0b57cec5SDimitry Andric         if (ct.is(ctype_base::digit, *bb))
4856*0b57cec5SDimitry Andric         {
4857*0b57cec5SDimitry Andric             switch(__get_up_to_n_digits(bb, be, err, ct, 4))
4858*0b57cec5SDimitry Andric             {
4859*0b57cec5SDimitry Andric             case 6:
4860*0b57cec5SDimitry Andric                 result.push_back('%');
4861*0b57cec5SDimitry Andric                 result.push_back('w');
4862*0b57cec5SDimitry Andric                 break;
4863*0b57cec5SDimitry Andric             case 7:
4864*0b57cec5SDimitry Andric                 result.push_back('%');
4865*0b57cec5SDimitry Andric                 result.push_back('u');
4866*0b57cec5SDimitry Andric                 break;
4867*0b57cec5SDimitry Andric             case 11:
4868*0b57cec5SDimitry Andric                 result.push_back('%');
4869*0b57cec5SDimitry Andric                 result.push_back('I');
4870*0b57cec5SDimitry Andric                 break;
4871*0b57cec5SDimitry Andric             case 12:
4872*0b57cec5SDimitry Andric                 result.push_back('%');
4873*0b57cec5SDimitry Andric                 result.push_back('m');
4874*0b57cec5SDimitry Andric                 break;
4875*0b57cec5SDimitry Andric             case 23:
4876*0b57cec5SDimitry Andric                 result.push_back('%');
4877*0b57cec5SDimitry Andric                 result.push_back('H');
4878*0b57cec5SDimitry Andric                 break;
4879*0b57cec5SDimitry Andric             case 31:
4880*0b57cec5SDimitry Andric                 result.push_back('%');
4881*0b57cec5SDimitry Andric                 result.push_back('d');
4882*0b57cec5SDimitry Andric                 break;
4883*0b57cec5SDimitry Andric             case 55:
4884*0b57cec5SDimitry Andric                 result.push_back('%');
4885*0b57cec5SDimitry Andric                 result.push_back('M');
4886*0b57cec5SDimitry Andric                 break;
4887*0b57cec5SDimitry Andric             case 59:
4888*0b57cec5SDimitry Andric                 result.push_back('%');
4889*0b57cec5SDimitry Andric                 result.push_back('S');
4890*0b57cec5SDimitry Andric                 break;
4891*0b57cec5SDimitry Andric             case 61:
4892*0b57cec5SDimitry Andric                 result.push_back('%');
4893*0b57cec5SDimitry Andric                 result.push_back('y');
4894*0b57cec5SDimitry Andric                 break;
4895*0b57cec5SDimitry Andric             case 364:
4896*0b57cec5SDimitry Andric                 result.push_back('%');
4897*0b57cec5SDimitry Andric                 result.push_back('j');
4898*0b57cec5SDimitry Andric                 break;
4899*0b57cec5SDimitry Andric             case 2061:
4900*0b57cec5SDimitry Andric                 result.push_back('%');
4901*0b57cec5SDimitry Andric                 result.push_back('Y');
4902*0b57cec5SDimitry Andric                 break;
4903*0b57cec5SDimitry Andric             default:
4904*0b57cec5SDimitry Andric                 for (; w != bb; ++w)
4905*0b57cec5SDimitry Andric                     result.push_back(*w);
4906*0b57cec5SDimitry Andric                 break;
4907*0b57cec5SDimitry Andric             }
4908*0b57cec5SDimitry Andric             continue;
4909*0b57cec5SDimitry Andric         }
4910*0b57cec5SDimitry Andric         if (*bb == '%')
4911*0b57cec5SDimitry Andric         {
4912*0b57cec5SDimitry Andric             result.push_back('%');
4913*0b57cec5SDimitry Andric             result.push_back('%');
4914*0b57cec5SDimitry Andric             ++bb;
4915*0b57cec5SDimitry Andric             continue;
4916*0b57cec5SDimitry Andric         }
4917*0b57cec5SDimitry Andric         result.push_back(*bb);
4918*0b57cec5SDimitry Andric         ++bb;
4919*0b57cec5SDimitry Andric     }
4920*0b57cec5SDimitry Andric     return result;
4921*0b57cec5SDimitry Andric }
4922*0b57cec5SDimitry Andric 
4923*0b57cec5SDimitry Andric #if defined(__clang__)
4924*0b57cec5SDimitry Andric #pragma clang diagnostic ignored "-Wmissing-braces"
4925*0b57cec5SDimitry Andric #endif
4926*0b57cec5SDimitry Andric 
4927*0b57cec5SDimitry Andric template <>
4928*0b57cec5SDimitry Andric wstring
4929*0b57cec5SDimitry Andric __time_get_storage<wchar_t>::__analyze(char fmt, const ctype<wchar_t>& ct)
4930*0b57cec5SDimitry Andric {
4931*0b57cec5SDimitry Andric     tm t = {0};
4932*0b57cec5SDimitry Andric     t.tm_sec = 59;
4933*0b57cec5SDimitry Andric     t.tm_min = 55;
4934*0b57cec5SDimitry Andric     t.tm_hour = 23;
4935*0b57cec5SDimitry Andric     t.tm_mday = 31;
4936*0b57cec5SDimitry Andric     t.tm_mon = 11;
4937*0b57cec5SDimitry Andric     t.tm_year = 161;
4938*0b57cec5SDimitry Andric     t.tm_wday = 6;
4939*0b57cec5SDimitry Andric     t.tm_yday = 364;
4940*0b57cec5SDimitry Andric     t.tm_isdst = -1;
4941*0b57cec5SDimitry Andric     char buf[100];
4942*0b57cec5SDimitry Andric     char f[3] = {0};
4943*0b57cec5SDimitry Andric     f[0] = '%';
4944*0b57cec5SDimitry Andric     f[1] = fmt;
4945*0b57cec5SDimitry Andric     strftime_l(buf, countof(buf), f, &t, __loc_);
4946*0b57cec5SDimitry Andric     wchar_t wbuf[100];
4947*0b57cec5SDimitry Andric     wchar_t* wbb = wbuf;
4948*0b57cec5SDimitry Andric     mbstate_t mb = {0};
4949*0b57cec5SDimitry Andric     const char* bb = buf;
4950*0b57cec5SDimitry Andric     size_t j = __libcpp_mbsrtowcs_l( wbb, &bb, countof(wbuf), &mb, __loc_);
4951*0b57cec5SDimitry Andric     if (j == size_t(-1))
4952*0b57cec5SDimitry Andric         __throw_runtime_error("locale not supported");
4953*0b57cec5SDimitry Andric     wchar_t* wbe = wbb + j;
4954*0b57cec5SDimitry Andric     wstring result;
4955*0b57cec5SDimitry Andric     while (wbb != wbe)
4956*0b57cec5SDimitry Andric     {
4957*0b57cec5SDimitry Andric         if (ct.is(ctype_base::space, *wbb))
4958*0b57cec5SDimitry Andric         {
4959*0b57cec5SDimitry Andric             result.push_back(L' ');
4960*0b57cec5SDimitry Andric             for (++wbb; wbb != wbe && ct.is(ctype_base::space, *wbb); ++wbb)
4961*0b57cec5SDimitry Andric                 ;
4962*0b57cec5SDimitry Andric             continue;
4963*0b57cec5SDimitry Andric         }
4964*0b57cec5SDimitry Andric         wchar_t* w = wbb;
4965*0b57cec5SDimitry Andric         ios_base::iostate err = ios_base::goodbit;
4966*0b57cec5SDimitry Andric         ptrdiff_t i = __scan_keyword(w, wbe, this->__weeks_, this->__weeks_+14,
4967*0b57cec5SDimitry Andric                                ct, err, false)
4968*0b57cec5SDimitry Andric                                - this->__weeks_;
4969*0b57cec5SDimitry Andric         if (i < 14)
4970*0b57cec5SDimitry Andric         {
4971*0b57cec5SDimitry Andric             result.push_back(L'%');
4972*0b57cec5SDimitry Andric             if (i < 7)
4973*0b57cec5SDimitry Andric                 result.push_back(L'A');
4974*0b57cec5SDimitry Andric             else
4975*0b57cec5SDimitry Andric                 result.push_back(L'a');
4976*0b57cec5SDimitry Andric             wbb = w;
4977*0b57cec5SDimitry Andric             continue;
4978*0b57cec5SDimitry Andric         }
4979*0b57cec5SDimitry Andric         w = wbb;
4980*0b57cec5SDimitry Andric         i = __scan_keyword(w, wbe, this->__months_, this->__months_+24,
4981*0b57cec5SDimitry Andric                            ct, err, false)
4982*0b57cec5SDimitry Andric                            - this->__months_;
4983*0b57cec5SDimitry Andric         if (i < 24)
4984*0b57cec5SDimitry Andric         {
4985*0b57cec5SDimitry Andric             result.push_back(L'%');
4986*0b57cec5SDimitry Andric             if (i < 12)
4987*0b57cec5SDimitry Andric                 result.push_back(L'B');
4988*0b57cec5SDimitry Andric             else
4989*0b57cec5SDimitry Andric                 result.push_back(L'b');
4990*0b57cec5SDimitry Andric             if (fmt == 'x' && ct.is(ctype_base::digit, this->__months_[i][0]))
4991*0b57cec5SDimitry Andric                 result.back() = L'm';
4992*0b57cec5SDimitry Andric             wbb = w;
4993*0b57cec5SDimitry Andric             continue;
4994*0b57cec5SDimitry Andric         }
4995*0b57cec5SDimitry Andric         if (this->__am_pm_[0].size() + this->__am_pm_[1].size() > 0)
4996*0b57cec5SDimitry Andric         {
4997*0b57cec5SDimitry Andric             w = wbb;
4998*0b57cec5SDimitry Andric             i = __scan_keyword(w, wbe, this->__am_pm_, this->__am_pm_+2,
4999*0b57cec5SDimitry Andric                                ct, err, false) - this->__am_pm_;
5000*0b57cec5SDimitry Andric             if (i < 2)
5001*0b57cec5SDimitry Andric             {
5002*0b57cec5SDimitry Andric                 result.push_back(L'%');
5003*0b57cec5SDimitry Andric                 result.push_back(L'p');
5004*0b57cec5SDimitry Andric                 wbb = w;
5005*0b57cec5SDimitry Andric                 continue;
5006*0b57cec5SDimitry Andric             }
5007*0b57cec5SDimitry Andric         }
5008*0b57cec5SDimitry Andric         w = wbb;
5009*0b57cec5SDimitry Andric         if (ct.is(ctype_base::digit, *wbb))
5010*0b57cec5SDimitry Andric         {
5011*0b57cec5SDimitry Andric             switch(__get_up_to_n_digits(wbb, wbe, err, ct, 4))
5012*0b57cec5SDimitry Andric             {
5013*0b57cec5SDimitry Andric             case 6:
5014*0b57cec5SDimitry Andric                 result.push_back(L'%');
5015*0b57cec5SDimitry Andric                 result.push_back(L'w');
5016*0b57cec5SDimitry Andric                 break;
5017*0b57cec5SDimitry Andric             case 7:
5018*0b57cec5SDimitry Andric                 result.push_back(L'%');
5019*0b57cec5SDimitry Andric                 result.push_back(L'u');
5020*0b57cec5SDimitry Andric                 break;
5021*0b57cec5SDimitry Andric             case 11:
5022*0b57cec5SDimitry Andric                 result.push_back(L'%');
5023*0b57cec5SDimitry Andric                 result.push_back(L'I');
5024*0b57cec5SDimitry Andric                 break;
5025*0b57cec5SDimitry Andric             case 12:
5026*0b57cec5SDimitry Andric                 result.push_back(L'%');
5027*0b57cec5SDimitry Andric                 result.push_back(L'm');
5028*0b57cec5SDimitry Andric                 break;
5029*0b57cec5SDimitry Andric             case 23:
5030*0b57cec5SDimitry Andric                 result.push_back(L'%');
5031*0b57cec5SDimitry Andric                 result.push_back(L'H');
5032*0b57cec5SDimitry Andric                 break;
5033*0b57cec5SDimitry Andric             case 31:
5034*0b57cec5SDimitry Andric                 result.push_back(L'%');
5035*0b57cec5SDimitry Andric                 result.push_back(L'd');
5036*0b57cec5SDimitry Andric                 break;
5037*0b57cec5SDimitry Andric             case 55:
5038*0b57cec5SDimitry Andric                 result.push_back(L'%');
5039*0b57cec5SDimitry Andric                 result.push_back(L'M');
5040*0b57cec5SDimitry Andric                 break;
5041*0b57cec5SDimitry Andric             case 59:
5042*0b57cec5SDimitry Andric                 result.push_back(L'%');
5043*0b57cec5SDimitry Andric                 result.push_back(L'S');
5044*0b57cec5SDimitry Andric                 break;
5045*0b57cec5SDimitry Andric             case 61:
5046*0b57cec5SDimitry Andric                 result.push_back(L'%');
5047*0b57cec5SDimitry Andric                 result.push_back(L'y');
5048*0b57cec5SDimitry Andric                 break;
5049*0b57cec5SDimitry Andric             case 364:
5050*0b57cec5SDimitry Andric                 result.push_back(L'%');
5051*0b57cec5SDimitry Andric                 result.push_back(L'j');
5052*0b57cec5SDimitry Andric                 break;
5053*0b57cec5SDimitry Andric             case 2061:
5054*0b57cec5SDimitry Andric                 result.push_back(L'%');
5055*0b57cec5SDimitry Andric                 result.push_back(L'Y');
5056*0b57cec5SDimitry Andric                 break;
5057*0b57cec5SDimitry Andric             default:
5058*0b57cec5SDimitry Andric                 for (; w != wbb; ++w)
5059*0b57cec5SDimitry Andric                     result.push_back(*w);
5060*0b57cec5SDimitry Andric                 break;
5061*0b57cec5SDimitry Andric             }
5062*0b57cec5SDimitry Andric             continue;
5063*0b57cec5SDimitry Andric         }
5064*0b57cec5SDimitry Andric         if (ct.narrow(*wbb, 0) == '%')
5065*0b57cec5SDimitry Andric         {
5066*0b57cec5SDimitry Andric             result.push_back(L'%');
5067*0b57cec5SDimitry Andric             result.push_back(L'%');
5068*0b57cec5SDimitry Andric             ++wbb;
5069*0b57cec5SDimitry Andric             continue;
5070*0b57cec5SDimitry Andric         }
5071*0b57cec5SDimitry Andric         result.push_back(*wbb);
5072*0b57cec5SDimitry Andric         ++wbb;
5073*0b57cec5SDimitry Andric     }
5074*0b57cec5SDimitry Andric     return result;
5075*0b57cec5SDimitry Andric }
5076*0b57cec5SDimitry Andric 
5077*0b57cec5SDimitry Andric template <>
5078*0b57cec5SDimitry Andric void
5079*0b57cec5SDimitry Andric __time_get_storage<char>::init(const ctype<char>& ct)
5080*0b57cec5SDimitry Andric {
5081*0b57cec5SDimitry Andric     tm t = {0};
5082*0b57cec5SDimitry Andric     char buf[100];
5083*0b57cec5SDimitry Andric     // __weeks_
5084*0b57cec5SDimitry Andric     for (int i = 0; i < 7; ++i)
5085*0b57cec5SDimitry Andric     {
5086*0b57cec5SDimitry Andric         t.tm_wday = i;
5087*0b57cec5SDimitry Andric         strftime_l(buf, countof(buf), "%A", &t, __loc_);
5088*0b57cec5SDimitry Andric         __weeks_[i] = buf;
5089*0b57cec5SDimitry Andric         strftime_l(buf, countof(buf), "%a", &t, __loc_);
5090*0b57cec5SDimitry Andric         __weeks_[i+7] = buf;
5091*0b57cec5SDimitry Andric     }
5092*0b57cec5SDimitry Andric     // __months_
5093*0b57cec5SDimitry Andric     for (int i = 0; i < 12; ++i)
5094*0b57cec5SDimitry Andric     {
5095*0b57cec5SDimitry Andric         t.tm_mon = i;
5096*0b57cec5SDimitry Andric         strftime_l(buf, countof(buf), "%B", &t, __loc_);
5097*0b57cec5SDimitry Andric         __months_[i] = buf;
5098*0b57cec5SDimitry Andric         strftime_l(buf, countof(buf), "%b", &t, __loc_);
5099*0b57cec5SDimitry Andric         __months_[i+12] = buf;
5100*0b57cec5SDimitry Andric     }
5101*0b57cec5SDimitry Andric     // __am_pm_
5102*0b57cec5SDimitry Andric     t.tm_hour = 1;
5103*0b57cec5SDimitry Andric     strftime_l(buf, countof(buf), "%p", &t, __loc_);
5104*0b57cec5SDimitry Andric     __am_pm_[0] = buf;
5105*0b57cec5SDimitry Andric     t.tm_hour = 13;
5106*0b57cec5SDimitry Andric     strftime_l(buf, countof(buf), "%p", &t, __loc_);
5107*0b57cec5SDimitry Andric     __am_pm_[1] = buf;
5108*0b57cec5SDimitry Andric     __c_ = __analyze('c', ct);
5109*0b57cec5SDimitry Andric     __r_ = __analyze('r', ct);
5110*0b57cec5SDimitry Andric     __x_ = __analyze('x', ct);
5111*0b57cec5SDimitry Andric     __X_ = __analyze('X', ct);
5112*0b57cec5SDimitry Andric }
5113*0b57cec5SDimitry Andric 
5114*0b57cec5SDimitry Andric template <>
5115*0b57cec5SDimitry Andric void
5116*0b57cec5SDimitry Andric __time_get_storage<wchar_t>::init(const ctype<wchar_t>& ct)
5117*0b57cec5SDimitry Andric {
5118*0b57cec5SDimitry Andric     tm t = {0};
5119*0b57cec5SDimitry Andric     char buf[100];
5120*0b57cec5SDimitry Andric     wchar_t wbuf[100];
5121*0b57cec5SDimitry Andric     wchar_t* wbe;
5122*0b57cec5SDimitry Andric     mbstate_t mb = {0};
5123*0b57cec5SDimitry Andric     // __weeks_
5124*0b57cec5SDimitry Andric     for (int i = 0; i < 7; ++i)
5125*0b57cec5SDimitry Andric     {
5126*0b57cec5SDimitry Andric         t.tm_wday = i;
5127*0b57cec5SDimitry Andric         strftime_l(buf, countof(buf), "%A", &t, __loc_);
5128*0b57cec5SDimitry Andric         mb = mbstate_t();
5129*0b57cec5SDimitry Andric         const char* bb = buf;
5130*0b57cec5SDimitry Andric         size_t j = __libcpp_mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_);
5131*0b57cec5SDimitry Andric         if (j == size_t(-1))
5132*0b57cec5SDimitry Andric             __throw_runtime_error("locale not supported");
5133*0b57cec5SDimitry Andric         wbe = wbuf + j;
5134*0b57cec5SDimitry Andric         __weeks_[i].assign(wbuf, wbe);
5135*0b57cec5SDimitry Andric         strftime_l(buf, countof(buf), "%a", &t, __loc_);
5136*0b57cec5SDimitry Andric         mb = mbstate_t();
5137*0b57cec5SDimitry Andric         bb = buf;
5138*0b57cec5SDimitry Andric         j = __libcpp_mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_);
5139*0b57cec5SDimitry Andric         if (j == size_t(-1))
5140*0b57cec5SDimitry Andric             __throw_runtime_error("locale not supported");
5141*0b57cec5SDimitry Andric         wbe = wbuf + j;
5142*0b57cec5SDimitry Andric         __weeks_[i+7].assign(wbuf, wbe);
5143*0b57cec5SDimitry Andric     }
5144*0b57cec5SDimitry Andric     // __months_
5145*0b57cec5SDimitry Andric     for (int i = 0; i < 12; ++i)
5146*0b57cec5SDimitry Andric     {
5147*0b57cec5SDimitry Andric         t.tm_mon = i;
5148*0b57cec5SDimitry Andric         strftime_l(buf, countof(buf), "%B", &t, __loc_);
5149*0b57cec5SDimitry Andric         mb = mbstate_t();
5150*0b57cec5SDimitry Andric         const char* bb = buf;
5151*0b57cec5SDimitry Andric         size_t j = __libcpp_mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_);
5152*0b57cec5SDimitry Andric         if (j == size_t(-1))
5153*0b57cec5SDimitry Andric             __throw_runtime_error("locale not supported");
5154*0b57cec5SDimitry Andric         wbe = wbuf + j;
5155*0b57cec5SDimitry Andric         __months_[i].assign(wbuf, wbe);
5156*0b57cec5SDimitry Andric         strftime_l(buf, countof(buf), "%b", &t, __loc_);
5157*0b57cec5SDimitry Andric         mb = mbstate_t();
5158*0b57cec5SDimitry Andric         bb = buf;
5159*0b57cec5SDimitry Andric         j = __libcpp_mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_);
5160*0b57cec5SDimitry Andric         if (j == size_t(-1))
5161*0b57cec5SDimitry Andric             __throw_runtime_error("locale not supported");
5162*0b57cec5SDimitry Andric         wbe = wbuf + j;
5163*0b57cec5SDimitry Andric         __months_[i+12].assign(wbuf, wbe);
5164*0b57cec5SDimitry Andric     }
5165*0b57cec5SDimitry Andric     // __am_pm_
5166*0b57cec5SDimitry Andric     t.tm_hour = 1;
5167*0b57cec5SDimitry Andric     strftime_l(buf, countof(buf), "%p", &t, __loc_);
5168*0b57cec5SDimitry Andric     mb = mbstate_t();
5169*0b57cec5SDimitry Andric     const char* bb = buf;
5170*0b57cec5SDimitry Andric     size_t j = __libcpp_mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_);
5171*0b57cec5SDimitry Andric     if (j == size_t(-1))
5172*0b57cec5SDimitry Andric         __throw_runtime_error("locale not supported");
5173*0b57cec5SDimitry Andric     wbe = wbuf + j;
5174*0b57cec5SDimitry Andric     __am_pm_[0].assign(wbuf, wbe);
5175*0b57cec5SDimitry Andric     t.tm_hour = 13;
5176*0b57cec5SDimitry Andric     strftime_l(buf, countof(buf), "%p", &t, __loc_);
5177*0b57cec5SDimitry Andric     mb = mbstate_t();
5178*0b57cec5SDimitry Andric     bb = buf;
5179*0b57cec5SDimitry Andric     j = __libcpp_mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_);
5180*0b57cec5SDimitry Andric     if (j == size_t(-1))
5181*0b57cec5SDimitry Andric         __throw_runtime_error("locale not supported");
5182*0b57cec5SDimitry Andric     wbe = wbuf + j;
5183*0b57cec5SDimitry Andric     __am_pm_[1].assign(wbuf, wbe);
5184*0b57cec5SDimitry Andric     __c_ = __analyze('c', ct);
5185*0b57cec5SDimitry Andric     __r_ = __analyze('r', ct);
5186*0b57cec5SDimitry Andric     __x_ = __analyze('x', ct);
5187*0b57cec5SDimitry Andric     __X_ = __analyze('X', ct);
5188*0b57cec5SDimitry Andric }
5189*0b57cec5SDimitry Andric 
5190*0b57cec5SDimitry Andric template <class CharT>
5191*0b57cec5SDimitry Andric struct _LIBCPP_HIDDEN __time_get_temp
5192*0b57cec5SDimitry Andric     : public ctype_byname<CharT>
5193*0b57cec5SDimitry Andric {
5194*0b57cec5SDimitry Andric     explicit __time_get_temp(const char* nm)
5195*0b57cec5SDimitry Andric         : ctype_byname<CharT>(nm, 1) {}
5196*0b57cec5SDimitry Andric     explicit __time_get_temp(const string& nm)
5197*0b57cec5SDimitry Andric         : ctype_byname<CharT>(nm, 1) {}
5198*0b57cec5SDimitry Andric };
5199*0b57cec5SDimitry Andric 
5200*0b57cec5SDimitry Andric template <>
5201*0b57cec5SDimitry Andric __time_get_storage<char>::__time_get_storage(const char* __nm)
5202*0b57cec5SDimitry Andric     : __time_get(__nm)
5203*0b57cec5SDimitry Andric {
5204*0b57cec5SDimitry Andric     const __time_get_temp<char> ct(__nm);
5205*0b57cec5SDimitry Andric     init(ct);
5206*0b57cec5SDimitry Andric }
5207*0b57cec5SDimitry Andric 
5208*0b57cec5SDimitry Andric template <>
5209*0b57cec5SDimitry Andric __time_get_storage<char>::__time_get_storage(const string& __nm)
5210*0b57cec5SDimitry Andric     : __time_get(__nm)
5211*0b57cec5SDimitry Andric {
5212*0b57cec5SDimitry Andric     const __time_get_temp<char> ct(__nm);
5213*0b57cec5SDimitry Andric     init(ct);
5214*0b57cec5SDimitry Andric }
5215*0b57cec5SDimitry Andric 
5216*0b57cec5SDimitry Andric template <>
5217*0b57cec5SDimitry Andric __time_get_storage<wchar_t>::__time_get_storage(const char* __nm)
5218*0b57cec5SDimitry Andric     : __time_get(__nm)
5219*0b57cec5SDimitry Andric {
5220*0b57cec5SDimitry Andric     const __time_get_temp<wchar_t> ct(__nm);
5221*0b57cec5SDimitry Andric     init(ct);
5222*0b57cec5SDimitry Andric }
5223*0b57cec5SDimitry Andric 
5224*0b57cec5SDimitry Andric template <>
5225*0b57cec5SDimitry Andric __time_get_storage<wchar_t>::__time_get_storage(const string& __nm)
5226*0b57cec5SDimitry Andric     : __time_get(__nm)
5227*0b57cec5SDimitry Andric {
5228*0b57cec5SDimitry Andric     const __time_get_temp<wchar_t> ct(__nm);
5229*0b57cec5SDimitry Andric     init(ct);
5230*0b57cec5SDimitry Andric }
5231*0b57cec5SDimitry Andric 
5232*0b57cec5SDimitry Andric template <>
5233*0b57cec5SDimitry Andric time_base::dateorder
5234*0b57cec5SDimitry Andric __time_get_storage<char>::__do_date_order() const
5235*0b57cec5SDimitry Andric {
5236*0b57cec5SDimitry Andric     unsigned i;
5237*0b57cec5SDimitry Andric     for (i = 0; i < __x_.size(); ++i)
5238*0b57cec5SDimitry Andric         if (__x_[i] == '%')
5239*0b57cec5SDimitry Andric             break;
5240*0b57cec5SDimitry Andric     ++i;
5241*0b57cec5SDimitry Andric     switch (__x_[i])
5242*0b57cec5SDimitry Andric     {
5243*0b57cec5SDimitry Andric     case 'y':
5244*0b57cec5SDimitry Andric     case 'Y':
5245*0b57cec5SDimitry Andric         for (++i; i < __x_.size(); ++i)
5246*0b57cec5SDimitry Andric             if (__x_[i] == '%')
5247*0b57cec5SDimitry Andric                 break;
5248*0b57cec5SDimitry Andric         if (i == __x_.size())
5249*0b57cec5SDimitry Andric             break;
5250*0b57cec5SDimitry Andric         ++i;
5251*0b57cec5SDimitry Andric         switch (__x_[i])
5252*0b57cec5SDimitry Andric         {
5253*0b57cec5SDimitry Andric         case 'm':
5254*0b57cec5SDimitry Andric             for (++i; i < __x_.size(); ++i)
5255*0b57cec5SDimitry Andric                 if (__x_[i] == '%')
5256*0b57cec5SDimitry Andric                     break;
5257*0b57cec5SDimitry Andric             if (i == __x_.size())
5258*0b57cec5SDimitry Andric                 break;
5259*0b57cec5SDimitry Andric             ++i;
5260*0b57cec5SDimitry Andric             if (__x_[i] == 'd')
5261*0b57cec5SDimitry Andric                 return time_base::ymd;
5262*0b57cec5SDimitry Andric             break;
5263*0b57cec5SDimitry Andric         case 'd':
5264*0b57cec5SDimitry Andric             for (++i; i < __x_.size(); ++i)
5265*0b57cec5SDimitry Andric                 if (__x_[i] == '%')
5266*0b57cec5SDimitry Andric                     break;
5267*0b57cec5SDimitry Andric             if (i == __x_.size())
5268*0b57cec5SDimitry Andric                 break;
5269*0b57cec5SDimitry Andric             ++i;
5270*0b57cec5SDimitry Andric             if (__x_[i] == 'm')
5271*0b57cec5SDimitry Andric                 return time_base::ydm;
5272*0b57cec5SDimitry Andric             break;
5273*0b57cec5SDimitry Andric         }
5274*0b57cec5SDimitry Andric         break;
5275*0b57cec5SDimitry Andric     case 'm':
5276*0b57cec5SDimitry Andric         for (++i; i < __x_.size(); ++i)
5277*0b57cec5SDimitry Andric             if (__x_[i] == '%')
5278*0b57cec5SDimitry Andric                 break;
5279*0b57cec5SDimitry Andric         if (i == __x_.size())
5280*0b57cec5SDimitry Andric             break;
5281*0b57cec5SDimitry Andric         ++i;
5282*0b57cec5SDimitry Andric         if (__x_[i] == 'd')
5283*0b57cec5SDimitry Andric         {
5284*0b57cec5SDimitry Andric             for (++i; i < __x_.size(); ++i)
5285*0b57cec5SDimitry Andric                 if (__x_[i] == '%')
5286*0b57cec5SDimitry Andric                     break;
5287*0b57cec5SDimitry Andric             if (i == __x_.size())
5288*0b57cec5SDimitry Andric                 break;
5289*0b57cec5SDimitry Andric             ++i;
5290*0b57cec5SDimitry Andric             if (__x_[i] == 'y' || __x_[i] == 'Y')
5291*0b57cec5SDimitry Andric                 return time_base::mdy;
5292*0b57cec5SDimitry Andric             break;
5293*0b57cec5SDimitry Andric         }
5294*0b57cec5SDimitry Andric         break;
5295*0b57cec5SDimitry Andric     case 'd':
5296*0b57cec5SDimitry Andric         for (++i; i < __x_.size(); ++i)
5297*0b57cec5SDimitry Andric             if (__x_[i] == '%')
5298*0b57cec5SDimitry Andric                 break;
5299*0b57cec5SDimitry Andric         if (i == __x_.size())
5300*0b57cec5SDimitry Andric             break;
5301*0b57cec5SDimitry Andric         ++i;
5302*0b57cec5SDimitry Andric         if (__x_[i] == 'm')
5303*0b57cec5SDimitry Andric         {
5304*0b57cec5SDimitry Andric             for (++i; i < __x_.size(); ++i)
5305*0b57cec5SDimitry Andric                 if (__x_[i] == '%')
5306*0b57cec5SDimitry Andric                     break;
5307*0b57cec5SDimitry Andric             if (i == __x_.size())
5308*0b57cec5SDimitry Andric                 break;
5309*0b57cec5SDimitry Andric             ++i;
5310*0b57cec5SDimitry Andric             if (__x_[i] == 'y' || __x_[i] == 'Y')
5311*0b57cec5SDimitry Andric                 return time_base::dmy;
5312*0b57cec5SDimitry Andric             break;
5313*0b57cec5SDimitry Andric         }
5314*0b57cec5SDimitry Andric         break;
5315*0b57cec5SDimitry Andric     }
5316*0b57cec5SDimitry Andric     return time_base::no_order;
5317*0b57cec5SDimitry Andric }
5318*0b57cec5SDimitry Andric 
5319*0b57cec5SDimitry Andric template <>
5320*0b57cec5SDimitry Andric time_base::dateorder
5321*0b57cec5SDimitry Andric __time_get_storage<wchar_t>::__do_date_order() const
5322*0b57cec5SDimitry Andric {
5323*0b57cec5SDimitry Andric     unsigned i;
5324*0b57cec5SDimitry Andric     for (i = 0; i < __x_.size(); ++i)
5325*0b57cec5SDimitry Andric         if (__x_[i] == L'%')
5326*0b57cec5SDimitry Andric             break;
5327*0b57cec5SDimitry Andric     ++i;
5328*0b57cec5SDimitry Andric     switch (__x_[i])
5329*0b57cec5SDimitry Andric     {
5330*0b57cec5SDimitry Andric     case L'y':
5331*0b57cec5SDimitry Andric     case L'Y':
5332*0b57cec5SDimitry Andric         for (++i; i < __x_.size(); ++i)
5333*0b57cec5SDimitry Andric             if (__x_[i] == L'%')
5334*0b57cec5SDimitry Andric                 break;
5335*0b57cec5SDimitry Andric         if (i == __x_.size())
5336*0b57cec5SDimitry Andric             break;
5337*0b57cec5SDimitry Andric         ++i;
5338*0b57cec5SDimitry Andric         switch (__x_[i])
5339*0b57cec5SDimitry Andric         {
5340*0b57cec5SDimitry Andric         case L'm':
5341*0b57cec5SDimitry Andric             for (++i; i < __x_.size(); ++i)
5342*0b57cec5SDimitry Andric                 if (__x_[i] == L'%')
5343*0b57cec5SDimitry Andric                     break;
5344*0b57cec5SDimitry Andric             if (i == __x_.size())
5345*0b57cec5SDimitry Andric                 break;
5346*0b57cec5SDimitry Andric             ++i;
5347*0b57cec5SDimitry Andric             if (__x_[i] == L'd')
5348*0b57cec5SDimitry Andric                 return time_base::ymd;
5349*0b57cec5SDimitry Andric             break;
5350*0b57cec5SDimitry Andric         case L'd':
5351*0b57cec5SDimitry Andric             for (++i; i < __x_.size(); ++i)
5352*0b57cec5SDimitry Andric                 if (__x_[i] == L'%')
5353*0b57cec5SDimitry Andric                     break;
5354*0b57cec5SDimitry Andric             if (i == __x_.size())
5355*0b57cec5SDimitry Andric                 break;
5356*0b57cec5SDimitry Andric             ++i;
5357*0b57cec5SDimitry Andric             if (__x_[i] == L'm')
5358*0b57cec5SDimitry Andric                 return time_base::ydm;
5359*0b57cec5SDimitry Andric             break;
5360*0b57cec5SDimitry Andric         }
5361*0b57cec5SDimitry Andric         break;
5362*0b57cec5SDimitry Andric     case L'm':
5363*0b57cec5SDimitry Andric         for (++i; i < __x_.size(); ++i)
5364*0b57cec5SDimitry Andric             if (__x_[i] == L'%')
5365*0b57cec5SDimitry Andric                 break;
5366*0b57cec5SDimitry Andric         if (i == __x_.size())
5367*0b57cec5SDimitry Andric             break;
5368*0b57cec5SDimitry Andric         ++i;
5369*0b57cec5SDimitry Andric         if (__x_[i] == L'd')
5370*0b57cec5SDimitry Andric         {
5371*0b57cec5SDimitry Andric             for (++i; i < __x_.size(); ++i)
5372*0b57cec5SDimitry Andric                 if (__x_[i] == L'%')
5373*0b57cec5SDimitry Andric                     break;
5374*0b57cec5SDimitry Andric             if (i == __x_.size())
5375*0b57cec5SDimitry Andric                 break;
5376*0b57cec5SDimitry Andric             ++i;
5377*0b57cec5SDimitry Andric             if (__x_[i] == L'y' || __x_[i] == L'Y')
5378*0b57cec5SDimitry Andric                 return time_base::mdy;
5379*0b57cec5SDimitry Andric             break;
5380*0b57cec5SDimitry Andric         }
5381*0b57cec5SDimitry Andric         break;
5382*0b57cec5SDimitry Andric     case L'd':
5383*0b57cec5SDimitry Andric         for (++i; i < __x_.size(); ++i)
5384*0b57cec5SDimitry Andric             if (__x_[i] == L'%')
5385*0b57cec5SDimitry Andric                 break;
5386*0b57cec5SDimitry Andric         if (i == __x_.size())
5387*0b57cec5SDimitry Andric             break;
5388*0b57cec5SDimitry Andric         ++i;
5389*0b57cec5SDimitry Andric         if (__x_[i] == L'm')
5390*0b57cec5SDimitry Andric         {
5391*0b57cec5SDimitry Andric             for (++i; i < __x_.size(); ++i)
5392*0b57cec5SDimitry Andric                 if (__x_[i] == L'%')
5393*0b57cec5SDimitry Andric                     break;
5394*0b57cec5SDimitry Andric             if (i == __x_.size())
5395*0b57cec5SDimitry Andric                 break;
5396*0b57cec5SDimitry Andric             ++i;
5397*0b57cec5SDimitry Andric             if (__x_[i] == L'y' || __x_[i] == L'Y')
5398*0b57cec5SDimitry Andric                 return time_base::dmy;
5399*0b57cec5SDimitry Andric             break;
5400*0b57cec5SDimitry Andric         }
5401*0b57cec5SDimitry Andric         break;
5402*0b57cec5SDimitry Andric     }
5403*0b57cec5SDimitry Andric     return time_base::no_order;
5404*0b57cec5SDimitry Andric }
5405*0b57cec5SDimitry Andric 
5406*0b57cec5SDimitry Andric // time_put
5407*0b57cec5SDimitry Andric 
5408*0b57cec5SDimitry Andric __time_put::__time_put(const char* nm)
5409*0b57cec5SDimitry Andric     : __loc_(newlocale(LC_ALL_MASK, nm, 0))
5410*0b57cec5SDimitry Andric {
5411*0b57cec5SDimitry Andric     if (__loc_ == 0)
5412*0b57cec5SDimitry Andric         __throw_runtime_error("time_put_byname"
5413*0b57cec5SDimitry Andric                             " failed to construct for " + string(nm));
5414*0b57cec5SDimitry Andric }
5415*0b57cec5SDimitry Andric 
5416*0b57cec5SDimitry Andric __time_put::__time_put(const string& nm)
5417*0b57cec5SDimitry Andric     : __loc_(newlocale(LC_ALL_MASK, nm.c_str(), 0))
5418*0b57cec5SDimitry Andric {
5419*0b57cec5SDimitry Andric     if (__loc_ == 0)
5420*0b57cec5SDimitry Andric         __throw_runtime_error("time_put_byname"
5421*0b57cec5SDimitry Andric                             " failed to construct for " + nm);
5422*0b57cec5SDimitry Andric }
5423*0b57cec5SDimitry Andric 
5424*0b57cec5SDimitry Andric __time_put::~__time_put()
5425*0b57cec5SDimitry Andric {
5426*0b57cec5SDimitry Andric     if (__loc_ != _LIBCPP_GET_C_LOCALE)
5427*0b57cec5SDimitry Andric         freelocale(__loc_);
5428*0b57cec5SDimitry Andric }
5429*0b57cec5SDimitry Andric 
5430*0b57cec5SDimitry Andric void
5431*0b57cec5SDimitry Andric __time_put::__do_put(char* __nb, char*& __ne, const tm* __tm,
5432*0b57cec5SDimitry Andric                      char __fmt, char __mod) const
5433*0b57cec5SDimitry Andric {
5434*0b57cec5SDimitry Andric     char fmt[] = {'%', __fmt, __mod, 0};
5435*0b57cec5SDimitry Andric     if (__mod != 0)
5436*0b57cec5SDimitry Andric         swap(fmt[1], fmt[2]);
5437*0b57cec5SDimitry Andric     size_t n = strftime_l(__nb, countof(__nb, __ne), fmt, __tm, __loc_);
5438*0b57cec5SDimitry Andric     __ne = __nb + n;
5439*0b57cec5SDimitry Andric }
5440*0b57cec5SDimitry Andric 
5441*0b57cec5SDimitry Andric void
5442*0b57cec5SDimitry Andric __time_put::__do_put(wchar_t* __wb, wchar_t*& __we, const tm* __tm,
5443*0b57cec5SDimitry Andric                      char __fmt, char __mod) const
5444*0b57cec5SDimitry Andric {
5445*0b57cec5SDimitry Andric     char __nar[100];
5446*0b57cec5SDimitry Andric     char* __ne = __nar + 100;
5447*0b57cec5SDimitry Andric     __do_put(__nar, __ne, __tm, __fmt, __mod);
5448*0b57cec5SDimitry Andric     mbstate_t mb = {0};
5449*0b57cec5SDimitry Andric     const char* __nb = __nar;
5450*0b57cec5SDimitry Andric     size_t j = __libcpp_mbsrtowcs_l(__wb, &__nb, countof(__wb, __we), &mb, __loc_);
5451*0b57cec5SDimitry Andric     if (j == size_t(-1))
5452*0b57cec5SDimitry Andric         __throw_runtime_error("locale not supported");
5453*0b57cec5SDimitry Andric     __we = __wb + j;
5454*0b57cec5SDimitry Andric }
5455*0b57cec5SDimitry Andric 
5456*0b57cec5SDimitry Andric // moneypunct_byname
5457*0b57cec5SDimitry Andric 
5458*0b57cec5SDimitry Andric template <class charT>
5459*0b57cec5SDimitry Andric static
5460*0b57cec5SDimitry Andric void
5461*0b57cec5SDimitry Andric __init_pat(money_base::pattern& pat, basic_string<charT>& __curr_symbol_,
5462*0b57cec5SDimitry Andric            bool intl, char cs_precedes, char sep_by_space, char sign_posn,
5463*0b57cec5SDimitry Andric            charT space_char)
5464*0b57cec5SDimitry Andric {
5465*0b57cec5SDimitry Andric     const char sign = static_cast<char>(money_base::sign);
5466*0b57cec5SDimitry Andric     const char space = static_cast<char>(money_base::space);
5467*0b57cec5SDimitry Andric     const char none = static_cast<char>(money_base::none);
5468*0b57cec5SDimitry Andric     const char symbol = static_cast<char>(money_base::symbol);
5469*0b57cec5SDimitry Andric     const char value = static_cast<char>(money_base::value);
5470*0b57cec5SDimitry Andric     const bool symbol_contains_sep = intl && __curr_symbol_.size() == 4;
5471*0b57cec5SDimitry Andric 
5472*0b57cec5SDimitry Andric     // Comments on case branches reflect 'C11 7.11.2.1 The localeconv
5473*0b57cec5SDimitry Andric     // function'. "Space between sign and symbol or value" means that
5474*0b57cec5SDimitry Andric     // if the sign is adjacent to the symbol, there's a space between
5475*0b57cec5SDimitry Andric     // them, and otherwise there's a space between the sign and value.
5476*0b57cec5SDimitry Andric     //
5477*0b57cec5SDimitry Andric     // C11's localeconv specifies that the fourth character of an
5478*0b57cec5SDimitry Andric     // international curr_symbol is used to separate the sign and
5479*0b57cec5SDimitry Andric     // value when sep_by_space says to do so. C++ can't represent
5480*0b57cec5SDimitry Andric     // that, so we just use a space.  When sep_by_space says to
5481*0b57cec5SDimitry Andric     // separate the symbol and value-or-sign with a space, we rearrange the
5482*0b57cec5SDimitry Andric     // curr_symbol to put its spacing character on the correct side of
5483*0b57cec5SDimitry Andric     // the symbol.
5484*0b57cec5SDimitry Andric     //
5485*0b57cec5SDimitry Andric     // We also need to avoid adding an extra space between the sign
5486*0b57cec5SDimitry Andric     // and value when the currency symbol is suppressed (by not
5487*0b57cec5SDimitry Andric     // setting showbase).  We match glibc's strfmon by interpreting
5488*0b57cec5SDimitry Andric     // sep_by_space==1 as "omit the space when the currency symbol is
5489*0b57cec5SDimitry Andric     // absent".
5490*0b57cec5SDimitry Andric     //
5491*0b57cec5SDimitry Andric     // Users who want to get this right should use ICU instead.
5492*0b57cec5SDimitry Andric 
5493*0b57cec5SDimitry Andric     switch (cs_precedes)
5494*0b57cec5SDimitry Andric     {
5495*0b57cec5SDimitry Andric     case 0:  // value before curr_symbol
5496*0b57cec5SDimitry Andric         if (symbol_contains_sep) {
5497*0b57cec5SDimitry Andric             // Move the separator to before the symbol, to place it
5498*0b57cec5SDimitry Andric             // between the value and symbol.
5499*0b57cec5SDimitry Andric             rotate(__curr_symbol_.begin(), __curr_symbol_.begin() + 3,
5500*0b57cec5SDimitry Andric                    __curr_symbol_.end());
5501*0b57cec5SDimitry Andric         }
5502*0b57cec5SDimitry Andric         switch (sign_posn)
5503*0b57cec5SDimitry Andric         {
5504*0b57cec5SDimitry Andric         case 0:  // Parentheses surround the quantity and currency symbol.
5505*0b57cec5SDimitry Andric             pat.field[0] = sign;
5506*0b57cec5SDimitry Andric             pat.field[1] = value;
5507*0b57cec5SDimitry Andric             pat.field[2] = none;  // Any space appears in the symbol.
5508*0b57cec5SDimitry Andric             pat.field[3] = symbol;
5509*0b57cec5SDimitry Andric             switch (sep_by_space)
5510*0b57cec5SDimitry Andric             {
5511*0b57cec5SDimitry Andric             case 0:  // No space separates the currency symbol and value.
5512*0b57cec5SDimitry Andric                 // This case may have changed between C99 and C11;
5513*0b57cec5SDimitry Andric                 // assume the currency symbol matches the intention.
5514*0b57cec5SDimitry Andric             case 2:  // Space between sign and currency or value.
5515*0b57cec5SDimitry Andric                 // The "sign" is two parentheses, so no space here either.
5516*0b57cec5SDimitry Andric                 return;
5517*0b57cec5SDimitry Andric             case 1:  // Space between currency-and-sign or currency and value.
5518*0b57cec5SDimitry Andric                 if (!symbol_contains_sep) {
5519*0b57cec5SDimitry Andric                     // We insert the space into the symbol instead of
5520*0b57cec5SDimitry Andric                     // setting pat.field[2]=space so that when
5521*0b57cec5SDimitry Andric                     // showbase is not set, the space goes away too.
5522*0b57cec5SDimitry Andric                     __curr_symbol_.insert(0, 1, space_char);
5523*0b57cec5SDimitry Andric                 }
5524*0b57cec5SDimitry Andric                 return;
5525*0b57cec5SDimitry Andric             default:
5526*0b57cec5SDimitry Andric                 break;
5527*0b57cec5SDimitry Andric             }
5528*0b57cec5SDimitry Andric             break;
5529*0b57cec5SDimitry Andric         case 1:  // The sign string precedes the quantity and currency symbol.
5530*0b57cec5SDimitry Andric             pat.field[0] = sign;
5531*0b57cec5SDimitry Andric             pat.field[3] = symbol;
5532*0b57cec5SDimitry Andric             switch (sep_by_space)
5533*0b57cec5SDimitry Andric             {
5534*0b57cec5SDimitry Andric             case 0:  // No space separates the currency symbol and value.
5535*0b57cec5SDimitry Andric                 pat.field[1] = value;
5536*0b57cec5SDimitry Andric                 pat.field[2] = none;
5537*0b57cec5SDimitry Andric                 return;
5538*0b57cec5SDimitry Andric             case 1:  // Space between currency-and-sign or currency and value.
5539*0b57cec5SDimitry Andric                 pat.field[1] = value;
5540*0b57cec5SDimitry Andric                 pat.field[2] = none;
5541*0b57cec5SDimitry Andric                 if (!symbol_contains_sep) {
5542*0b57cec5SDimitry Andric                     // We insert the space into the symbol instead of
5543*0b57cec5SDimitry Andric                     // setting pat.field[2]=space so that when
5544*0b57cec5SDimitry Andric                     // showbase is not set, the space goes away too.
5545*0b57cec5SDimitry Andric                     __curr_symbol_.insert(0, 1, space_char);
5546*0b57cec5SDimitry Andric                 }
5547*0b57cec5SDimitry Andric                 return;
5548*0b57cec5SDimitry Andric             case 2:  // Space between sign and currency or value.
5549*0b57cec5SDimitry Andric                 pat.field[1] = space;
5550*0b57cec5SDimitry Andric                 pat.field[2] = value;
5551*0b57cec5SDimitry Andric                 if (symbol_contains_sep) {
5552*0b57cec5SDimitry Andric                     // Remove the separator from the symbol, since it
5553*0b57cec5SDimitry Andric                     // has already appeared after the sign.
5554*0b57cec5SDimitry Andric                     __curr_symbol_.erase(__curr_symbol_.begin());
5555*0b57cec5SDimitry Andric                 }
5556*0b57cec5SDimitry Andric                 return;
5557*0b57cec5SDimitry Andric             default:
5558*0b57cec5SDimitry Andric                 break;
5559*0b57cec5SDimitry Andric             }
5560*0b57cec5SDimitry Andric             break;
5561*0b57cec5SDimitry Andric         case 2:  // The sign string succeeds the quantity and currency symbol.
5562*0b57cec5SDimitry Andric             pat.field[0] = value;
5563*0b57cec5SDimitry Andric             pat.field[3] = sign;
5564*0b57cec5SDimitry Andric             switch (sep_by_space)
5565*0b57cec5SDimitry Andric             {
5566*0b57cec5SDimitry Andric             case 0:  // No space separates the currency symbol and value.
5567*0b57cec5SDimitry Andric                 pat.field[1] = none;
5568*0b57cec5SDimitry Andric                 pat.field[2] = symbol;
5569*0b57cec5SDimitry Andric                 return;
5570*0b57cec5SDimitry Andric             case 1:  // Space between currency-and-sign or currency and value.
5571*0b57cec5SDimitry Andric                 if (!symbol_contains_sep) {
5572*0b57cec5SDimitry Andric                     // We insert the space into the symbol instead of
5573*0b57cec5SDimitry Andric                     // setting pat.field[1]=space so that when
5574*0b57cec5SDimitry Andric                     // showbase is not set, the space goes away too.
5575*0b57cec5SDimitry Andric                     __curr_symbol_.insert(0, 1, space_char);
5576*0b57cec5SDimitry Andric                 }
5577*0b57cec5SDimitry Andric                 pat.field[1] = none;
5578*0b57cec5SDimitry Andric                 pat.field[2] = symbol;
5579*0b57cec5SDimitry Andric                 return;
5580*0b57cec5SDimitry Andric             case 2:  // Space between sign and currency or value.
5581*0b57cec5SDimitry Andric                 pat.field[1] = symbol;
5582*0b57cec5SDimitry Andric                 pat.field[2] = space;
5583*0b57cec5SDimitry Andric                 if (symbol_contains_sep) {
5584*0b57cec5SDimitry Andric                     // Remove the separator from the symbol, since it
5585*0b57cec5SDimitry Andric                     // should not be removed if showbase is absent.
5586*0b57cec5SDimitry Andric                     __curr_symbol_.erase(__curr_symbol_.begin());
5587*0b57cec5SDimitry Andric                 }
5588*0b57cec5SDimitry Andric                 return;
5589*0b57cec5SDimitry Andric             default:
5590*0b57cec5SDimitry Andric                 break;
5591*0b57cec5SDimitry Andric             }
5592*0b57cec5SDimitry Andric             break;
5593*0b57cec5SDimitry Andric         case 3:  // The sign string immediately precedes the currency symbol.
5594*0b57cec5SDimitry Andric             pat.field[0] = value;
5595*0b57cec5SDimitry Andric             pat.field[3] = symbol;
5596*0b57cec5SDimitry Andric             switch (sep_by_space)
5597*0b57cec5SDimitry Andric             {
5598*0b57cec5SDimitry Andric             case 0:  // No space separates the currency symbol and value.
5599*0b57cec5SDimitry Andric                 pat.field[1] = none;
5600*0b57cec5SDimitry Andric                 pat.field[2] = sign;
5601*0b57cec5SDimitry Andric                 return;
5602*0b57cec5SDimitry Andric             case 1:  // Space between currency-and-sign or currency and value.
5603*0b57cec5SDimitry Andric                 pat.field[1] = space;
5604*0b57cec5SDimitry Andric                 pat.field[2] = sign;
5605*0b57cec5SDimitry Andric                 if (symbol_contains_sep) {
5606*0b57cec5SDimitry Andric                     // Remove the separator from the symbol, since it
5607*0b57cec5SDimitry Andric                     // has already appeared before the sign.
5608*0b57cec5SDimitry Andric                     __curr_symbol_.erase(__curr_symbol_.begin());
5609*0b57cec5SDimitry Andric                 }
5610*0b57cec5SDimitry Andric                 return;
5611*0b57cec5SDimitry Andric             case 2:  // Space between sign and currency or value.
5612*0b57cec5SDimitry Andric                 pat.field[1] = sign;
5613*0b57cec5SDimitry Andric                 pat.field[2] = none;
5614*0b57cec5SDimitry Andric                 if (!symbol_contains_sep) {
5615*0b57cec5SDimitry Andric                     // We insert the space into the symbol instead of
5616*0b57cec5SDimitry Andric                     // setting pat.field[2]=space so that when
5617*0b57cec5SDimitry Andric                     // showbase is not set, the space goes away too.
5618*0b57cec5SDimitry Andric                     __curr_symbol_.insert(0, 1, space_char);
5619*0b57cec5SDimitry Andric                 }
5620*0b57cec5SDimitry Andric                 return;
5621*0b57cec5SDimitry Andric             default:
5622*0b57cec5SDimitry Andric                 break;
5623*0b57cec5SDimitry Andric             }
5624*0b57cec5SDimitry Andric             break;
5625*0b57cec5SDimitry Andric         case 4:  // The sign string immediately succeeds the currency symbol.
5626*0b57cec5SDimitry Andric             pat.field[0] = value;
5627*0b57cec5SDimitry Andric             pat.field[3] = sign;
5628*0b57cec5SDimitry Andric             switch (sep_by_space)
5629*0b57cec5SDimitry Andric             {
5630*0b57cec5SDimitry Andric             case 0:  // No space separates the currency symbol and value.
5631*0b57cec5SDimitry Andric                 pat.field[1] = none;
5632*0b57cec5SDimitry Andric                 pat.field[2] = symbol;
5633*0b57cec5SDimitry Andric                 return;
5634*0b57cec5SDimitry Andric             case 1:  // Space between currency-and-sign or currency and value.
5635*0b57cec5SDimitry Andric                 pat.field[1] = none;
5636*0b57cec5SDimitry Andric                 pat.field[2] = symbol;
5637*0b57cec5SDimitry Andric                 if (!symbol_contains_sep) {
5638*0b57cec5SDimitry Andric                     // We insert the space into the symbol instead of
5639*0b57cec5SDimitry Andric                     // setting pat.field[1]=space so that when
5640*0b57cec5SDimitry Andric                     // showbase is not set, the space goes away too.
5641*0b57cec5SDimitry Andric                     __curr_symbol_.insert(0, 1, space_char);
5642*0b57cec5SDimitry Andric                 }
5643*0b57cec5SDimitry Andric                 return;
5644*0b57cec5SDimitry Andric             case 2:  // Space between sign and currency or value.
5645*0b57cec5SDimitry Andric                 pat.field[1] = symbol;
5646*0b57cec5SDimitry Andric                 pat.field[2] = space;
5647*0b57cec5SDimitry Andric                 if (symbol_contains_sep) {
5648*0b57cec5SDimitry Andric                     // Remove the separator from the symbol, since it
5649*0b57cec5SDimitry Andric                     // should not disappear when showbase is absent.
5650*0b57cec5SDimitry Andric                     __curr_symbol_.erase(__curr_symbol_.begin());
5651*0b57cec5SDimitry Andric                 }
5652*0b57cec5SDimitry Andric                 return;
5653*0b57cec5SDimitry Andric             default:
5654*0b57cec5SDimitry Andric                 break;
5655*0b57cec5SDimitry Andric             }
5656*0b57cec5SDimitry Andric             break;
5657*0b57cec5SDimitry Andric         default:
5658*0b57cec5SDimitry Andric             break;
5659*0b57cec5SDimitry Andric         }
5660*0b57cec5SDimitry Andric         break;
5661*0b57cec5SDimitry Andric     case 1:  // curr_symbol before value
5662*0b57cec5SDimitry Andric         switch (sign_posn)
5663*0b57cec5SDimitry Andric         {
5664*0b57cec5SDimitry Andric         case 0:  // Parentheses surround the quantity and currency symbol.
5665*0b57cec5SDimitry Andric             pat.field[0] = sign;
5666*0b57cec5SDimitry Andric             pat.field[1] = symbol;
5667*0b57cec5SDimitry Andric             pat.field[2] = none;  // Any space appears in the symbol.
5668*0b57cec5SDimitry Andric             pat.field[3] = value;
5669*0b57cec5SDimitry Andric             switch (sep_by_space)
5670*0b57cec5SDimitry Andric             {
5671*0b57cec5SDimitry Andric             case 0:  // No space separates the currency symbol and value.
5672*0b57cec5SDimitry Andric                 // This case may have changed between C99 and C11;
5673*0b57cec5SDimitry Andric                 // assume the currency symbol matches the intention.
5674*0b57cec5SDimitry Andric             case 2:  // Space between sign and currency or value.
5675*0b57cec5SDimitry Andric                 // The "sign" is two parentheses, so no space here either.
5676*0b57cec5SDimitry Andric                 return;
5677*0b57cec5SDimitry Andric             case 1:  // Space between currency-and-sign or currency and value.
5678*0b57cec5SDimitry Andric                 if (!symbol_contains_sep) {
5679*0b57cec5SDimitry Andric                     // We insert the space into the symbol instead of
5680*0b57cec5SDimitry Andric                     // setting pat.field[2]=space so that when
5681*0b57cec5SDimitry Andric                     // showbase is not set, the space goes away too.
5682*0b57cec5SDimitry Andric                     __curr_symbol_.insert(0, 1, space_char);
5683*0b57cec5SDimitry Andric                 }
5684*0b57cec5SDimitry Andric                 return;
5685*0b57cec5SDimitry Andric             default:
5686*0b57cec5SDimitry Andric                 break;
5687*0b57cec5SDimitry Andric             }
5688*0b57cec5SDimitry Andric             break;
5689*0b57cec5SDimitry Andric         case 1:  // The sign string precedes the quantity and currency symbol.
5690*0b57cec5SDimitry Andric             pat.field[0] = sign;
5691*0b57cec5SDimitry Andric             pat.field[3] = value;
5692*0b57cec5SDimitry Andric             switch (sep_by_space)
5693*0b57cec5SDimitry Andric             {
5694*0b57cec5SDimitry Andric             case 0:  // No space separates the currency symbol and value.
5695*0b57cec5SDimitry Andric                 pat.field[1] = symbol;
5696*0b57cec5SDimitry Andric                 pat.field[2] = none;
5697*0b57cec5SDimitry Andric                 return;
5698*0b57cec5SDimitry Andric             case 1:  // Space between currency-and-sign or currency and value.
5699*0b57cec5SDimitry Andric                 pat.field[1] = symbol;
5700*0b57cec5SDimitry Andric                 pat.field[2] = none;
5701*0b57cec5SDimitry Andric                 if (!symbol_contains_sep) {
5702*0b57cec5SDimitry Andric                     // We insert the space into the symbol instead of
5703*0b57cec5SDimitry Andric                     // setting pat.field[2]=space so that when
5704*0b57cec5SDimitry Andric                     // showbase is not set, the space goes away too.
5705*0b57cec5SDimitry Andric                     __curr_symbol_.push_back(space_char);
5706*0b57cec5SDimitry Andric                 }
5707*0b57cec5SDimitry Andric                 return;
5708*0b57cec5SDimitry Andric             case 2:  // Space between sign and currency or value.
5709*0b57cec5SDimitry Andric                 pat.field[1] = space;
5710*0b57cec5SDimitry Andric                 pat.field[2] = symbol;
5711*0b57cec5SDimitry Andric                 if (symbol_contains_sep) {
5712*0b57cec5SDimitry Andric                     // Remove the separator from the symbol, since it
5713*0b57cec5SDimitry Andric                     // has already appeared after the sign.
5714*0b57cec5SDimitry Andric                     __curr_symbol_.pop_back();
5715*0b57cec5SDimitry Andric                 }
5716*0b57cec5SDimitry Andric                 return;
5717*0b57cec5SDimitry Andric             default:
5718*0b57cec5SDimitry Andric                 break;
5719*0b57cec5SDimitry Andric             }
5720*0b57cec5SDimitry Andric             break;
5721*0b57cec5SDimitry Andric         case 2:  // The sign string succeeds the quantity and currency symbol.
5722*0b57cec5SDimitry Andric             pat.field[0] = symbol;
5723*0b57cec5SDimitry Andric             pat.field[3] = sign;
5724*0b57cec5SDimitry Andric             switch (sep_by_space)
5725*0b57cec5SDimitry Andric             {
5726*0b57cec5SDimitry Andric             case 0:  // No space separates the currency symbol and value.
5727*0b57cec5SDimitry Andric                 pat.field[1] = none;
5728*0b57cec5SDimitry Andric                 pat.field[2] = value;
5729*0b57cec5SDimitry Andric                 return;
5730*0b57cec5SDimitry Andric             case 1:  // Space between currency-and-sign or currency and value.
5731*0b57cec5SDimitry Andric                 pat.field[1] = none;
5732*0b57cec5SDimitry Andric                 pat.field[2] = value;
5733*0b57cec5SDimitry Andric                 if (!symbol_contains_sep) {
5734*0b57cec5SDimitry Andric                     // We insert the space into the symbol instead of
5735*0b57cec5SDimitry Andric                     // setting pat.field[1]=space so that when
5736*0b57cec5SDimitry Andric                     // showbase is not set, the space goes away too.
5737*0b57cec5SDimitry Andric                     __curr_symbol_.push_back(space_char);
5738*0b57cec5SDimitry Andric                 }
5739*0b57cec5SDimitry Andric                 return;
5740*0b57cec5SDimitry Andric             case 2:  // Space between sign and currency or value.
5741*0b57cec5SDimitry Andric                 pat.field[1] = value;
5742*0b57cec5SDimitry Andric                 pat.field[2] = space;
5743*0b57cec5SDimitry Andric                 if (symbol_contains_sep) {
5744*0b57cec5SDimitry Andric                     // Remove the separator from the symbol, since it
5745*0b57cec5SDimitry Andric                     // will appear before the sign.
5746*0b57cec5SDimitry Andric                     __curr_symbol_.pop_back();
5747*0b57cec5SDimitry Andric                 }
5748*0b57cec5SDimitry Andric                 return;
5749*0b57cec5SDimitry Andric             default:
5750*0b57cec5SDimitry Andric                 break;
5751*0b57cec5SDimitry Andric             }
5752*0b57cec5SDimitry Andric             break;
5753*0b57cec5SDimitry Andric         case 3:  // The sign string immediately precedes the currency symbol.
5754*0b57cec5SDimitry Andric             pat.field[0] = sign;
5755*0b57cec5SDimitry Andric             pat.field[3] = value;
5756*0b57cec5SDimitry Andric             switch (sep_by_space)
5757*0b57cec5SDimitry Andric             {
5758*0b57cec5SDimitry Andric             case 0:  // No space separates the currency symbol and value.
5759*0b57cec5SDimitry Andric                 pat.field[1] = symbol;
5760*0b57cec5SDimitry Andric                 pat.field[2] = none;
5761*0b57cec5SDimitry Andric                 return;
5762*0b57cec5SDimitry Andric             case 1:  // Space between currency-and-sign or currency and value.
5763*0b57cec5SDimitry Andric                 pat.field[1] = symbol;
5764*0b57cec5SDimitry Andric                 pat.field[2] = none;
5765*0b57cec5SDimitry Andric                 if (!symbol_contains_sep) {
5766*0b57cec5SDimitry Andric                     // We insert the space into the symbol instead of
5767*0b57cec5SDimitry Andric                     // setting pat.field[2]=space so that when
5768*0b57cec5SDimitry Andric                     // showbase is not set, the space goes away too.
5769*0b57cec5SDimitry Andric                     __curr_symbol_.push_back(space_char);
5770*0b57cec5SDimitry Andric                 }
5771*0b57cec5SDimitry Andric                 return;
5772*0b57cec5SDimitry Andric             case 2:  // Space between sign and currency or value.
5773*0b57cec5SDimitry Andric                 pat.field[1] = space;
5774*0b57cec5SDimitry Andric                 pat.field[2] = symbol;
5775*0b57cec5SDimitry Andric                 if (symbol_contains_sep) {
5776*0b57cec5SDimitry Andric                     // Remove the separator from the symbol, since it
5777*0b57cec5SDimitry Andric                     // has already appeared after the sign.
5778*0b57cec5SDimitry Andric                     __curr_symbol_.pop_back();
5779*0b57cec5SDimitry Andric                 }
5780*0b57cec5SDimitry Andric                 return;
5781*0b57cec5SDimitry Andric             default:
5782*0b57cec5SDimitry Andric                 break;
5783*0b57cec5SDimitry Andric             }
5784*0b57cec5SDimitry Andric             break;
5785*0b57cec5SDimitry Andric         case 4:  // The sign string immediately succeeds the currency symbol.
5786*0b57cec5SDimitry Andric             pat.field[0] = symbol;
5787*0b57cec5SDimitry Andric             pat.field[3] = value;
5788*0b57cec5SDimitry Andric             switch (sep_by_space)
5789*0b57cec5SDimitry Andric             {
5790*0b57cec5SDimitry Andric             case 0:  // No space separates the currency symbol and value.
5791*0b57cec5SDimitry Andric                 pat.field[1] = sign;
5792*0b57cec5SDimitry Andric                 pat.field[2] = none;
5793*0b57cec5SDimitry Andric                 return;
5794*0b57cec5SDimitry Andric             case 1:  // Space between currency-and-sign or currency and value.
5795*0b57cec5SDimitry Andric                 pat.field[1] = sign;
5796*0b57cec5SDimitry Andric                 pat.field[2] = space;
5797*0b57cec5SDimitry Andric                 if (symbol_contains_sep) {
5798*0b57cec5SDimitry Andric                     // Remove the separator from the symbol, since it
5799*0b57cec5SDimitry Andric                     // should not disappear when showbase is absent.
5800*0b57cec5SDimitry Andric                     __curr_symbol_.pop_back();
5801*0b57cec5SDimitry Andric                 }
5802*0b57cec5SDimitry Andric                 return;
5803*0b57cec5SDimitry Andric             case 2:  // Space between sign and currency or value.
5804*0b57cec5SDimitry Andric                 pat.field[1] = none;
5805*0b57cec5SDimitry Andric                 pat.field[2] = sign;
5806*0b57cec5SDimitry Andric                 if (!symbol_contains_sep) {
5807*0b57cec5SDimitry Andric                     // We insert the space into the symbol instead of
5808*0b57cec5SDimitry Andric                     // setting pat.field[1]=space so that when
5809*0b57cec5SDimitry Andric                     // showbase is not set, the space goes away too.
5810*0b57cec5SDimitry Andric                     __curr_symbol_.push_back(space_char);
5811*0b57cec5SDimitry Andric                 }
5812*0b57cec5SDimitry Andric                 return;
5813*0b57cec5SDimitry Andric            default:
5814*0b57cec5SDimitry Andric                 break;
5815*0b57cec5SDimitry Andric             }
5816*0b57cec5SDimitry Andric             break;
5817*0b57cec5SDimitry Andric         default:
5818*0b57cec5SDimitry Andric             break;
5819*0b57cec5SDimitry Andric         }
5820*0b57cec5SDimitry Andric         break;
5821*0b57cec5SDimitry Andric     default:
5822*0b57cec5SDimitry Andric         break;
5823*0b57cec5SDimitry Andric     }
5824*0b57cec5SDimitry Andric     pat.field[0] = symbol;
5825*0b57cec5SDimitry Andric     pat.field[1] = sign;
5826*0b57cec5SDimitry Andric     pat.field[2] = none;
5827*0b57cec5SDimitry Andric     pat.field[3] = value;
5828*0b57cec5SDimitry Andric }
5829*0b57cec5SDimitry Andric 
5830*0b57cec5SDimitry Andric template<>
5831*0b57cec5SDimitry Andric void
5832*0b57cec5SDimitry Andric moneypunct_byname<char, false>::init(const char* nm)
5833*0b57cec5SDimitry Andric {
5834*0b57cec5SDimitry Andric     typedef moneypunct<char, false> base;
5835*0b57cec5SDimitry Andric     __libcpp_unique_locale loc(nm);
5836*0b57cec5SDimitry Andric     if (!loc)
5837*0b57cec5SDimitry Andric         __throw_runtime_error("moneypunct_byname"
5838*0b57cec5SDimitry Andric                             " failed to construct for " + string(nm));
5839*0b57cec5SDimitry Andric 
5840*0b57cec5SDimitry Andric     lconv* lc = __libcpp_localeconv_l(loc.get());
5841*0b57cec5SDimitry Andric     if (!checked_string_to_char_convert(__decimal_point_,
5842*0b57cec5SDimitry Andric                                         lc->mon_decimal_point,
5843*0b57cec5SDimitry Andric                                         loc.get()))
5844*0b57cec5SDimitry Andric       __decimal_point_ = base::do_decimal_point();
5845*0b57cec5SDimitry Andric     if (!checked_string_to_char_convert(__thousands_sep_,
5846*0b57cec5SDimitry Andric                                         lc->mon_thousands_sep,
5847*0b57cec5SDimitry Andric                                         loc.get()))
5848*0b57cec5SDimitry Andric       __thousands_sep_ = base::do_thousands_sep();
5849*0b57cec5SDimitry Andric 
5850*0b57cec5SDimitry Andric     __grouping_ = lc->mon_grouping;
5851*0b57cec5SDimitry Andric     __curr_symbol_ = lc->currency_symbol;
5852*0b57cec5SDimitry Andric     if (lc->frac_digits != CHAR_MAX)
5853*0b57cec5SDimitry Andric         __frac_digits_ = lc->frac_digits;
5854*0b57cec5SDimitry Andric     else
5855*0b57cec5SDimitry Andric         __frac_digits_ = base::do_frac_digits();
5856*0b57cec5SDimitry Andric     if (lc->p_sign_posn == 0)
5857*0b57cec5SDimitry Andric         __positive_sign_ = "()";
5858*0b57cec5SDimitry Andric     else
5859*0b57cec5SDimitry Andric         __positive_sign_ = lc->positive_sign;
5860*0b57cec5SDimitry Andric     if (lc->n_sign_posn == 0)
5861*0b57cec5SDimitry Andric         __negative_sign_ = "()";
5862*0b57cec5SDimitry Andric     else
5863*0b57cec5SDimitry Andric         __negative_sign_ = lc->negative_sign;
5864*0b57cec5SDimitry Andric     // Assume the positive and negative formats will want spaces in
5865*0b57cec5SDimitry Andric     // the same places in curr_symbol since there's no way to
5866*0b57cec5SDimitry Andric     // represent anything else.
5867*0b57cec5SDimitry Andric     string_type __dummy_curr_symbol = __curr_symbol_;
5868*0b57cec5SDimitry Andric     __init_pat(__pos_format_, __dummy_curr_symbol, false,
5869*0b57cec5SDimitry Andric                lc->p_cs_precedes, lc->p_sep_by_space, lc->p_sign_posn, ' ');
5870*0b57cec5SDimitry Andric     __init_pat(__neg_format_, __curr_symbol_, false,
5871*0b57cec5SDimitry Andric                lc->n_cs_precedes, lc->n_sep_by_space, lc->n_sign_posn, ' ');
5872*0b57cec5SDimitry Andric }
5873*0b57cec5SDimitry Andric 
5874*0b57cec5SDimitry Andric template<>
5875*0b57cec5SDimitry Andric void
5876*0b57cec5SDimitry Andric moneypunct_byname<char, true>::init(const char* nm)
5877*0b57cec5SDimitry Andric {
5878*0b57cec5SDimitry Andric     typedef moneypunct<char, true> base;
5879*0b57cec5SDimitry Andric     __libcpp_unique_locale loc(nm);
5880*0b57cec5SDimitry Andric     if (!loc)
5881*0b57cec5SDimitry Andric         __throw_runtime_error("moneypunct_byname"
5882*0b57cec5SDimitry Andric                             " failed to construct for " + string(nm));
5883*0b57cec5SDimitry Andric 
5884*0b57cec5SDimitry Andric     lconv* lc = __libcpp_localeconv_l(loc.get());
5885*0b57cec5SDimitry Andric     if (!checked_string_to_char_convert(__decimal_point_,
5886*0b57cec5SDimitry Andric                                         lc->mon_decimal_point,
5887*0b57cec5SDimitry Andric                                         loc.get()))
5888*0b57cec5SDimitry Andric       __decimal_point_ = base::do_decimal_point();
5889*0b57cec5SDimitry Andric     if (!checked_string_to_char_convert(__thousands_sep_,
5890*0b57cec5SDimitry Andric                                         lc->mon_thousands_sep,
5891*0b57cec5SDimitry Andric                                         loc.get()))
5892*0b57cec5SDimitry Andric       __thousands_sep_ = base::do_thousands_sep();
5893*0b57cec5SDimitry Andric     __grouping_ = lc->mon_grouping;
5894*0b57cec5SDimitry Andric     __curr_symbol_ = lc->int_curr_symbol;
5895*0b57cec5SDimitry Andric     if (lc->int_frac_digits != CHAR_MAX)
5896*0b57cec5SDimitry Andric         __frac_digits_ = lc->int_frac_digits;
5897*0b57cec5SDimitry Andric     else
5898*0b57cec5SDimitry Andric         __frac_digits_ = base::do_frac_digits();
5899*0b57cec5SDimitry Andric #if defined(_LIBCPP_MSVCRT) || defined(__MINGW32__)
5900*0b57cec5SDimitry Andric     if (lc->p_sign_posn == 0)
5901*0b57cec5SDimitry Andric #else // _LIBCPP_MSVCRT
5902*0b57cec5SDimitry Andric     if (lc->int_p_sign_posn == 0)
5903*0b57cec5SDimitry Andric #endif // !_LIBCPP_MSVCRT
5904*0b57cec5SDimitry Andric         __positive_sign_ = "()";
5905*0b57cec5SDimitry Andric     else
5906*0b57cec5SDimitry Andric         __positive_sign_ = lc->positive_sign;
5907*0b57cec5SDimitry Andric #if defined(_LIBCPP_MSVCRT) || defined(__MINGW32__)
5908*0b57cec5SDimitry Andric     if(lc->n_sign_posn == 0)
5909*0b57cec5SDimitry Andric #else // _LIBCPP_MSVCRT
5910*0b57cec5SDimitry Andric     if (lc->int_n_sign_posn == 0)
5911*0b57cec5SDimitry Andric #endif // !_LIBCPP_MSVCRT
5912*0b57cec5SDimitry Andric         __negative_sign_ = "()";
5913*0b57cec5SDimitry Andric     else
5914*0b57cec5SDimitry Andric         __negative_sign_ = lc->negative_sign;
5915*0b57cec5SDimitry Andric     // Assume the positive and negative formats will want spaces in
5916*0b57cec5SDimitry Andric     // the same places in curr_symbol since there's no way to
5917*0b57cec5SDimitry Andric     // represent anything else.
5918*0b57cec5SDimitry Andric     string_type __dummy_curr_symbol = __curr_symbol_;
5919*0b57cec5SDimitry Andric #if defined(_LIBCPP_MSVCRT) || defined(__MINGW32__)
5920*0b57cec5SDimitry Andric     __init_pat(__pos_format_, __dummy_curr_symbol, true,
5921*0b57cec5SDimitry Andric                lc->p_cs_precedes, lc->p_sep_by_space, lc->p_sign_posn, ' ');
5922*0b57cec5SDimitry Andric     __init_pat(__neg_format_, __curr_symbol_, true,
5923*0b57cec5SDimitry Andric                lc->n_cs_precedes, lc->n_sep_by_space, lc->n_sign_posn, ' ');
5924*0b57cec5SDimitry Andric #else // _LIBCPP_MSVCRT
5925*0b57cec5SDimitry Andric     __init_pat(__pos_format_, __dummy_curr_symbol, true,
5926*0b57cec5SDimitry Andric                lc->int_p_cs_precedes, lc->int_p_sep_by_space,
5927*0b57cec5SDimitry Andric                lc->int_p_sign_posn, ' ');
5928*0b57cec5SDimitry Andric     __init_pat(__neg_format_, __curr_symbol_, true,
5929*0b57cec5SDimitry Andric                lc->int_n_cs_precedes, lc->int_n_sep_by_space,
5930*0b57cec5SDimitry Andric                lc->int_n_sign_posn, ' ');
5931*0b57cec5SDimitry Andric #endif // !_LIBCPP_MSVCRT
5932*0b57cec5SDimitry Andric }
5933*0b57cec5SDimitry Andric 
5934*0b57cec5SDimitry Andric template<>
5935*0b57cec5SDimitry Andric void
5936*0b57cec5SDimitry Andric moneypunct_byname<wchar_t, false>::init(const char* nm)
5937*0b57cec5SDimitry Andric {
5938*0b57cec5SDimitry Andric     typedef moneypunct<wchar_t, false> base;
5939*0b57cec5SDimitry Andric     __libcpp_unique_locale loc(nm);
5940*0b57cec5SDimitry Andric     if (!loc)
5941*0b57cec5SDimitry Andric         __throw_runtime_error("moneypunct_byname"
5942*0b57cec5SDimitry Andric                             " failed to construct for " + string(nm));
5943*0b57cec5SDimitry Andric     lconv* lc = __libcpp_localeconv_l(loc.get());
5944*0b57cec5SDimitry Andric     if (!checked_string_to_wchar_convert(__decimal_point_,
5945*0b57cec5SDimitry Andric                                          lc->mon_decimal_point,
5946*0b57cec5SDimitry Andric                                          loc.get()))
5947*0b57cec5SDimitry Andric       __decimal_point_ = base::do_decimal_point();
5948*0b57cec5SDimitry Andric     if (!checked_string_to_wchar_convert(__thousands_sep_,
5949*0b57cec5SDimitry Andric                                          lc->mon_thousands_sep,
5950*0b57cec5SDimitry Andric                                          loc.get()))
5951*0b57cec5SDimitry Andric       __thousands_sep_ = base::do_thousands_sep();
5952*0b57cec5SDimitry Andric     __grouping_ = lc->mon_grouping;
5953*0b57cec5SDimitry Andric     wchar_t wbuf[100];
5954*0b57cec5SDimitry Andric     mbstate_t mb = {0};
5955*0b57cec5SDimitry Andric     const char* bb = lc->currency_symbol;
5956*0b57cec5SDimitry Andric     size_t j = __libcpp_mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get());
5957*0b57cec5SDimitry Andric     if (j == size_t(-1))
5958*0b57cec5SDimitry Andric         __throw_runtime_error("locale not supported");
5959*0b57cec5SDimitry Andric     wchar_t* wbe = wbuf + j;
5960*0b57cec5SDimitry Andric     __curr_symbol_.assign(wbuf, wbe);
5961*0b57cec5SDimitry Andric     if (lc->frac_digits != CHAR_MAX)
5962*0b57cec5SDimitry Andric         __frac_digits_ = lc->frac_digits;
5963*0b57cec5SDimitry Andric     else
5964*0b57cec5SDimitry Andric         __frac_digits_ = base::do_frac_digits();
5965*0b57cec5SDimitry Andric     if (lc->p_sign_posn == 0)
5966*0b57cec5SDimitry Andric         __positive_sign_ = L"()";
5967*0b57cec5SDimitry Andric     else
5968*0b57cec5SDimitry Andric     {
5969*0b57cec5SDimitry Andric         mb = mbstate_t();
5970*0b57cec5SDimitry Andric         bb = lc->positive_sign;
5971*0b57cec5SDimitry Andric         j = __libcpp_mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get());
5972*0b57cec5SDimitry Andric         if (j == size_t(-1))
5973*0b57cec5SDimitry Andric             __throw_runtime_error("locale not supported");
5974*0b57cec5SDimitry Andric         wbe = wbuf + j;
5975*0b57cec5SDimitry Andric         __positive_sign_.assign(wbuf, wbe);
5976*0b57cec5SDimitry Andric     }
5977*0b57cec5SDimitry Andric     if (lc->n_sign_posn == 0)
5978*0b57cec5SDimitry Andric         __negative_sign_ = L"()";
5979*0b57cec5SDimitry Andric     else
5980*0b57cec5SDimitry Andric     {
5981*0b57cec5SDimitry Andric         mb = mbstate_t();
5982*0b57cec5SDimitry Andric         bb = lc->negative_sign;
5983*0b57cec5SDimitry Andric         j = __libcpp_mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get());
5984*0b57cec5SDimitry Andric         if (j == size_t(-1))
5985*0b57cec5SDimitry Andric             __throw_runtime_error("locale not supported");
5986*0b57cec5SDimitry Andric         wbe = wbuf + j;
5987*0b57cec5SDimitry Andric         __negative_sign_.assign(wbuf, wbe);
5988*0b57cec5SDimitry Andric     }
5989*0b57cec5SDimitry Andric     // Assume the positive and negative formats will want spaces in
5990*0b57cec5SDimitry Andric     // the same places in curr_symbol since there's no way to
5991*0b57cec5SDimitry Andric     // represent anything else.
5992*0b57cec5SDimitry Andric     string_type __dummy_curr_symbol = __curr_symbol_;
5993*0b57cec5SDimitry Andric     __init_pat(__pos_format_, __dummy_curr_symbol, false,
5994*0b57cec5SDimitry Andric                lc->p_cs_precedes, lc->p_sep_by_space, lc->p_sign_posn, L' ');
5995*0b57cec5SDimitry Andric     __init_pat(__neg_format_, __curr_symbol_, false,
5996*0b57cec5SDimitry Andric                lc->n_cs_precedes, lc->n_sep_by_space, lc->n_sign_posn, L' ');
5997*0b57cec5SDimitry Andric }
5998*0b57cec5SDimitry Andric 
5999*0b57cec5SDimitry Andric template<>
6000*0b57cec5SDimitry Andric void
6001*0b57cec5SDimitry Andric moneypunct_byname<wchar_t, true>::init(const char* nm)
6002*0b57cec5SDimitry Andric {
6003*0b57cec5SDimitry Andric     typedef moneypunct<wchar_t, true> base;
6004*0b57cec5SDimitry Andric     __libcpp_unique_locale loc(nm);
6005*0b57cec5SDimitry Andric     if (!loc)
6006*0b57cec5SDimitry Andric         __throw_runtime_error("moneypunct_byname"
6007*0b57cec5SDimitry Andric                             " failed to construct for " + string(nm));
6008*0b57cec5SDimitry Andric 
6009*0b57cec5SDimitry Andric     lconv* lc = __libcpp_localeconv_l(loc.get());
6010*0b57cec5SDimitry Andric     if (!checked_string_to_wchar_convert(__decimal_point_,
6011*0b57cec5SDimitry Andric                                          lc->mon_decimal_point,
6012*0b57cec5SDimitry Andric                                          loc.get()))
6013*0b57cec5SDimitry Andric       __decimal_point_ = base::do_decimal_point();
6014*0b57cec5SDimitry Andric     if (!checked_string_to_wchar_convert(__thousands_sep_,
6015*0b57cec5SDimitry Andric                                          lc->mon_thousands_sep,
6016*0b57cec5SDimitry Andric                                          loc.get()))
6017*0b57cec5SDimitry Andric       __thousands_sep_ = base::do_thousands_sep();
6018*0b57cec5SDimitry Andric     __grouping_ = lc->mon_grouping;
6019*0b57cec5SDimitry Andric     wchar_t wbuf[100];
6020*0b57cec5SDimitry Andric     mbstate_t mb = {0};
6021*0b57cec5SDimitry Andric     const char* bb = lc->int_curr_symbol;
6022*0b57cec5SDimitry Andric     size_t j = __libcpp_mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get());
6023*0b57cec5SDimitry Andric     if (j == size_t(-1))
6024*0b57cec5SDimitry Andric         __throw_runtime_error("locale not supported");
6025*0b57cec5SDimitry Andric     wchar_t* wbe = wbuf + j;
6026*0b57cec5SDimitry Andric     __curr_symbol_.assign(wbuf, wbe);
6027*0b57cec5SDimitry Andric     if (lc->int_frac_digits != CHAR_MAX)
6028*0b57cec5SDimitry Andric         __frac_digits_ = lc->int_frac_digits;
6029*0b57cec5SDimitry Andric     else
6030*0b57cec5SDimitry Andric         __frac_digits_ = base::do_frac_digits();
6031*0b57cec5SDimitry Andric #if defined(_LIBCPP_MSVCRT) || defined(__MINGW32__)
6032*0b57cec5SDimitry Andric     if (lc->p_sign_posn == 0)
6033*0b57cec5SDimitry Andric #else // _LIBCPP_MSVCRT
6034*0b57cec5SDimitry Andric     if (lc->int_p_sign_posn == 0)
6035*0b57cec5SDimitry Andric #endif // !_LIBCPP_MSVCRT
6036*0b57cec5SDimitry Andric         __positive_sign_ = L"()";
6037*0b57cec5SDimitry Andric     else
6038*0b57cec5SDimitry Andric     {
6039*0b57cec5SDimitry Andric         mb = mbstate_t();
6040*0b57cec5SDimitry Andric         bb = lc->positive_sign;
6041*0b57cec5SDimitry Andric         j = __libcpp_mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get());
6042*0b57cec5SDimitry Andric         if (j == size_t(-1))
6043*0b57cec5SDimitry Andric             __throw_runtime_error("locale not supported");
6044*0b57cec5SDimitry Andric         wbe = wbuf + j;
6045*0b57cec5SDimitry Andric         __positive_sign_.assign(wbuf, wbe);
6046*0b57cec5SDimitry Andric     }
6047*0b57cec5SDimitry Andric #if defined(_LIBCPP_MSVCRT) || defined(__MINGW32__)
6048*0b57cec5SDimitry Andric     if (lc->n_sign_posn == 0)
6049*0b57cec5SDimitry Andric #else // _LIBCPP_MSVCRT
6050*0b57cec5SDimitry Andric     if (lc->int_n_sign_posn == 0)
6051*0b57cec5SDimitry Andric #endif // !_LIBCPP_MSVCRT
6052*0b57cec5SDimitry Andric         __negative_sign_ = L"()";
6053*0b57cec5SDimitry Andric     else
6054*0b57cec5SDimitry Andric     {
6055*0b57cec5SDimitry Andric         mb = mbstate_t();
6056*0b57cec5SDimitry Andric         bb = lc->negative_sign;
6057*0b57cec5SDimitry Andric         j = __libcpp_mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get());
6058*0b57cec5SDimitry Andric         if (j == size_t(-1))
6059*0b57cec5SDimitry Andric             __throw_runtime_error("locale not supported");
6060*0b57cec5SDimitry Andric         wbe = wbuf + j;
6061*0b57cec5SDimitry Andric         __negative_sign_.assign(wbuf, wbe);
6062*0b57cec5SDimitry Andric     }
6063*0b57cec5SDimitry Andric     // Assume the positive and negative formats will want spaces in
6064*0b57cec5SDimitry Andric     // the same places in curr_symbol since there's no way to
6065*0b57cec5SDimitry Andric     // represent anything else.
6066*0b57cec5SDimitry Andric     string_type __dummy_curr_symbol = __curr_symbol_;
6067*0b57cec5SDimitry Andric #if defined(_LIBCPP_MSVCRT) || defined(__MINGW32__)
6068*0b57cec5SDimitry Andric     __init_pat(__pos_format_, __dummy_curr_symbol, true,
6069*0b57cec5SDimitry Andric                lc->p_cs_precedes, lc->p_sep_by_space, lc->p_sign_posn, L' ');
6070*0b57cec5SDimitry Andric     __init_pat(__neg_format_, __curr_symbol_, true,
6071*0b57cec5SDimitry Andric                lc->n_cs_precedes, lc->n_sep_by_space, lc->n_sign_posn, L' ');
6072*0b57cec5SDimitry Andric #else // _LIBCPP_MSVCRT
6073*0b57cec5SDimitry Andric     __init_pat(__pos_format_, __dummy_curr_symbol, true,
6074*0b57cec5SDimitry Andric                lc->int_p_cs_precedes, lc->int_p_sep_by_space,
6075*0b57cec5SDimitry Andric                lc->int_p_sign_posn, L' ');
6076*0b57cec5SDimitry Andric     __init_pat(__neg_format_, __curr_symbol_, true,
6077*0b57cec5SDimitry Andric                lc->int_n_cs_precedes, lc->int_n_sep_by_space,
6078*0b57cec5SDimitry Andric                lc->int_n_sign_posn, L' ');
6079*0b57cec5SDimitry Andric #endif // !_LIBCPP_MSVCRT
6080*0b57cec5SDimitry Andric }
6081*0b57cec5SDimitry Andric 
6082*0b57cec5SDimitry Andric void __do_nothing(void*) {}
6083*0b57cec5SDimitry Andric 
6084*0b57cec5SDimitry Andric void __throw_runtime_error(const char* msg)
6085*0b57cec5SDimitry Andric {
6086*0b57cec5SDimitry Andric #ifndef _LIBCPP_NO_EXCEPTIONS
6087*0b57cec5SDimitry Andric     throw runtime_error(msg);
6088*0b57cec5SDimitry Andric #else
6089*0b57cec5SDimitry Andric     (void)msg;
6090*0b57cec5SDimitry Andric     _VSTD::abort();
6091*0b57cec5SDimitry Andric #endif
6092*0b57cec5SDimitry Andric }
6093*0b57cec5SDimitry Andric 
6094*0b57cec5SDimitry Andric template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS collate<char>;
6095*0b57cec5SDimitry Andric template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS collate<wchar_t>;
6096*0b57cec5SDimitry Andric 
6097*0b57cec5SDimitry Andric template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS num_get<char>;
6098*0b57cec5SDimitry Andric template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS num_get<wchar_t>;
6099*0b57cec5SDimitry Andric 
6100*0b57cec5SDimitry Andric template struct _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS __num_get<char>;
6101*0b57cec5SDimitry Andric template struct _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS __num_get<wchar_t>;
6102*0b57cec5SDimitry Andric 
6103*0b57cec5SDimitry Andric template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS num_put<char>;
6104*0b57cec5SDimitry Andric template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS num_put<wchar_t>;
6105*0b57cec5SDimitry Andric 
6106*0b57cec5SDimitry Andric template struct _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS __num_put<char>;
6107*0b57cec5SDimitry Andric template struct _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS __num_put<wchar_t>;
6108*0b57cec5SDimitry Andric 
6109*0b57cec5SDimitry Andric template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS time_get<char>;
6110*0b57cec5SDimitry Andric template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS time_get<wchar_t>;
6111*0b57cec5SDimitry Andric 
6112*0b57cec5SDimitry Andric template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS time_get_byname<char>;
6113*0b57cec5SDimitry Andric template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS time_get_byname<wchar_t>;
6114*0b57cec5SDimitry Andric 
6115*0b57cec5SDimitry Andric template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS time_put<char>;
6116*0b57cec5SDimitry Andric template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS time_put<wchar_t>;
6117*0b57cec5SDimitry Andric 
6118*0b57cec5SDimitry Andric template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS time_put_byname<char>;
6119*0b57cec5SDimitry Andric template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS time_put_byname<wchar_t>;
6120*0b57cec5SDimitry Andric 
6121*0b57cec5SDimitry Andric template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS moneypunct<char, false>;
6122*0b57cec5SDimitry Andric template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS moneypunct<char, true>;
6123*0b57cec5SDimitry Andric template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS moneypunct<wchar_t, false>;
6124*0b57cec5SDimitry Andric template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS moneypunct<wchar_t, true>;
6125*0b57cec5SDimitry Andric 
6126*0b57cec5SDimitry Andric template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS moneypunct_byname<char, false>;
6127*0b57cec5SDimitry Andric template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS moneypunct_byname<char, true>;
6128*0b57cec5SDimitry Andric template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS moneypunct_byname<wchar_t, false>;
6129*0b57cec5SDimitry Andric template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS moneypunct_byname<wchar_t, true>;
6130*0b57cec5SDimitry Andric 
6131*0b57cec5SDimitry Andric template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS money_get<char>;
6132*0b57cec5SDimitry Andric template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS money_get<wchar_t>;
6133*0b57cec5SDimitry Andric 
6134*0b57cec5SDimitry Andric template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS __money_get<char>;
6135*0b57cec5SDimitry Andric template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS __money_get<wchar_t>;
6136*0b57cec5SDimitry Andric 
6137*0b57cec5SDimitry Andric template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS money_put<char>;
6138*0b57cec5SDimitry Andric template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS money_put<wchar_t>;
6139*0b57cec5SDimitry Andric 
6140*0b57cec5SDimitry Andric template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS __money_put<char>;
6141*0b57cec5SDimitry Andric template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS __money_put<wchar_t>;
6142*0b57cec5SDimitry Andric 
6143*0b57cec5SDimitry Andric template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS messages<char>;
6144*0b57cec5SDimitry Andric template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS messages<wchar_t>;
6145*0b57cec5SDimitry Andric 
6146*0b57cec5SDimitry Andric template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS messages_byname<char>;
6147*0b57cec5SDimitry Andric template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS messages_byname<wchar_t>;
6148*0b57cec5SDimitry Andric 
6149*0b57cec5SDimitry Andric template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS codecvt_byname<char, char, mbstate_t>;
6150*0b57cec5SDimitry Andric template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS codecvt_byname<wchar_t, char, mbstate_t>;
6151*0b57cec5SDimitry Andric template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS codecvt_byname<char16_t, char, mbstate_t>;
6152*0b57cec5SDimitry Andric template class _LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS codecvt_byname<char32_t, char, mbstate_t>;
6153*0b57cec5SDimitry Andric 
6154*0b57cec5SDimitry Andric _LIBCPP_END_NAMESPACE_STD
6155