blob: 284bee7a06008546b5f5e25e67a8c6eda8dbc3cb [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001//===------------------------- locale.cpp ---------------------------------===//
2//
Howard Hinnantf5256e12010-05-11 21:36:01 +00003// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
David Chisnall997e4542012-02-29 13:05:08 +000010// On Solaris, we need to define something to make the C99 parts of localeconv
11// visible.
12#ifdef __sun__
13#define _LCONV_C99
14#endif
15
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000016#include "string"
17#include "locale"
Howard Hinnant87d1a8a2010-05-30 21:39:41 +000018#include "codecvt"
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000019#include "vector"
20#include "algorithm"
21#include "algorithm"
22#include "typeinfo"
Howard Hinnantd318d492011-06-30 14:21:55 +000023#include "type_traits"
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000024#include "clocale"
25#include "cstring"
26#include "cwctype"
27#include "__sso_allocator"
Howard Hinnant6cd05ee2011-09-23 16:11:27 +000028#if _WIN32
Howard Hinnant14fa9f92011-09-29 20:33:10 +000029#include <support/win32/locale_win32.h>
Howard Hinnant6cd05ee2011-09-23 16:11:27 +000030#else // _WIN32
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000031#include <langinfo.h>
Howard Hinnant6cd05ee2011-09-23 16:11:27 +000032#endif // _!WIN32
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000033#include <stdlib.h>
34
35_LIBCPP_BEGIN_NAMESPACE_STD
36
Howard Hinnant866569b2011-09-28 23:39:33 +000037#ifdef __cloc_defined
Sean Hunt62a6ac32011-07-09 00:56:23 +000038locale_t __cloc() {
39 // In theory this could create a race condition. In practice
40 // the race condition is non-fatal since it will just create
41 // a little resource leak. Better approach would be appreciated.
Sean Hunt62a6ac32011-07-09 00:56:23 +000042 static locale_t result = newlocale(LC_ALL_MASK, "C", 0);
43 return result;
Sean Hunt62a6ac32011-07-09 00:56:23 +000044}
Howard Hinnant866569b2011-09-28 23:39:33 +000045#endif // __cloc_defined
Sean Hunt62a6ac32011-07-09 00:56:23 +000046
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000047namespace {
48
49struct release
50{
51 void operator()(locale::facet* p) {p->__release_shared();}
52};
53
54template <class T, class A0>
55inline
56T&
57make(A0 a0)
58{
59 static typename aligned_storage<sizeof(T)>::type buf;
60 ::new (&buf) T(a0);
61 return *(T*)&buf;
62}
63
64template <class T, class A0, class A1>
65inline
66T&
67make(A0 a0, A1 a1)
68{
69 static typename aligned_storage<sizeof(T)>::type buf;
70 ::new (&buf) T(a0, a1);
71 return *(T*)&buf;
72}
73
74template <class T, class A0, class A1, class A2>
75inline
76T&
77make(A0 a0, A1 a1, A2 a2)
78{
79 static typename aligned_storage<sizeof(T)>::type buf;
80 ::new (&buf) T(a0, a1, a2);
81 return *(T*)&buf;
82}
83
84}
85
Howard Hinnant0a69fa12012-12-12 21:14:28 +000086const locale::category locale::none;
87const locale::category locale::collate;
88const locale::category locale::ctype;
89const locale::category locale::monetary;
90const locale::category locale::numeric;
91const locale::category locale::time;
92const locale::category locale::messages;
93const locale::category locale::all;
94
Howard Hinnantec3773c2011-12-01 20:21:04 +000095#pragma clang diagnostic push
96#pragma clang diagnostic ignored "-Wpadded"
97
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000098class _LIBCPP_HIDDEN locale::__imp
99 : public facet
100{
101 enum {N = 28};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000102 vector<facet*, __sso_allocator<facet*, N> > facets_;
Howard Hinnantec3773c2011-12-01 20:21:04 +0000103 string name_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000104public:
105 explicit __imp(size_t refs = 0);
106 explicit __imp(const string& name, size_t refs = 0);
107 __imp(const __imp&);
108 __imp(const __imp&, const string&, locale::category c);
109 __imp(const __imp& other, const __imp& one, locale::category c);
110 __imp(const __imp&, facet* f, long id);
111 ~__imp();
112
113 const string& name() const {return name_;}
Howard Hinnantec3773c2011-12-01 20:21:04 +0000114 bool has_facet(long id) const
115 {return static_cast<size_t>(id) < facets_.size() && facets_[static_cast<size_t>(id)];}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000116 const locale::facet* use_facet(long id) const;
117
118 static const locale& make_classic();
119 static locale& make_global();
120private:
121 void install(facet* f, long id);
122 template <class F> void install(F* f) {install(f, f->id.__get());}
123 template <class F> void install_from(const __imp& other);
124};
125
Howard Hinnantec3773c2011-12-01 20:21:04 +0000126#pragma clang diagnostic pop
127
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000128locale::__imp::__imp(size_t refs)
129 : facet(refs),
Howard Hinnantec3773c2011-12-01 20:21:04 +0000130 facets_(N),
131 name_("C")
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000132{
133 facets_.clear();
Howard Hinnantec3773c2011-12-01 20:21:04 +0000134 install(&make<_VSTD::collate<char> >(1u));
135 install(&make<_VSTD::collate<wchar_t> >(1u));
136 install(&make<_VSTD::ctype<char> >((ctype_base::mask*)0, false, 1u));
137 install(&make<_VSTD::ctype<wchar_t> >(1u));
138 install(&make<codecvt<char, char, mbstate_t> >(1u));
139 install(&make<codecvt<wchar_t, char, mbstate_t> >(1u));
140 install(&make<codecvt<char16_t, char, mbstate_t> >(1u));
141 install(&make<codecvt<char32_t, char, mbstate_t> >(1u));
142 install(&make<numpunct<char> >(1u));
143 install(&make<numpunct<wchar_t> >(1u));
144 install(&make<num_get<char> >(1u));
145 install(&make<num_get<wchar_t> >(1u));
146 install(&make<num_put<char> >(1u));
147 install(&make<num_put<wchar_t> >(1u));
148 install(&make<moneypunct<char, false> >(1u));
149 install(&make<moneypunct<char, true> >(1u));
150 install(&make<moneypunct<wchar_t, false> >(1u));
151 install(&make<moneypunct<wchar_t, true> >(1u));
152 install(&make<money_get<char> >(1u));
153 install(&make<money_get<wchar_t> >(1u));
154 install(&make<money_put<char> >(1u));
155 install(&make<money_put<wchar_t> >(1u));
156 install(&make<time_get<char> >(1u));
157 install(&make<time_get<wchar_t> >(1u));
158 install(&make<time_put<char> >(1u));
159 install(&make<time_put<wchar_t> >(1u));
160 install(&make<_VSTD::messages<char> >(1u));
161 install(&make<_VSTD::messages<wchar_t> >(1u));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000162}
163
164locale::__imp::__imp(const string& name, size_t refs)
165 : facet(refs),
Howard Hinnantec3773c2011-12-01 20:21:04 +0000166 facets_(N),
167 name_(name)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000168{
Howard Hinnantd4444702010-08-11 17:04:31 +0000169#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000170 try
171 {
Howard Hinnant16e6e1d2010-08-22 00:03:27 +0000172#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000173 facets_ = locale::classic().__locale_->facets_;
174 for (unsigned i = 0; i < facets_.size(); ++i)
175 if (facets_[i])
176 facets_[i]->__add_shared();
177 install(new collate_byname<char>(name_));
178 install(new collate_byname<wchar_t>(name_));
179 install(new ctype_byname<char>(name_));
180 install(new ctype_byname<wchar_t>(name_));
181 install(new codecvt_byname<char, char, mbstate_t>(name_));
182 install(new codecvt_byname<wchar_t, char, mbstate_t>(name_));
183 install(new codecvt_byname<char16_t, char, mbstate_t>(name_));
184 install(new codecvt_byname<char32_t, char, mbstate_t>(name_));
185 install(new numpunct_byname<char>(name_));
186 install(new numpunct_byname<wchar_t>(name_));
187 install(new moneypunct_byname<char, false>(name_));
188 install(new moneypunct_byname<char, true>(name_));
189 install(new moneypunct_byname<wchar_t, false>(name_));
190 install(new moneypunct_byname<wchar_t, true>(name_));
191 install(new time_get_byname<char>(name_));
192 install(new time_get_byname<wchar_t>(name_));
193 install(new time_put_byname<char>(name_));
194 install(new time_put_byname<wchar_t>(name_));
195 install(new messages_byname<char>(name_));
196 install(new messages_byname<wchar_t>(name_));
Howard Hinnantd4444702010-08-11 17:04:31 +0000197#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000198 }
199 catch (...)
200 {
201 for (unsigned i = 0; i < facets_.size(); ++i)
202 if (facets_[i])
203 facets_[i]->__release_shared();
204 throw;
205 }
Howard Hinnant16e6e1d2010-08-22 00:03:27 +0000206#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000207}
208
209locale::__imp::__imp(const __imp& other)
Howard Hinnantec3773c2011-12-01 20:21:04 +0000210 : facets_(max<size_t>(N, other.facets_.size())),
211 name_(other.name_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000212{
213 facets_ = other.facets_;
214 for (unsigned i = 0; i < facets_.size(); ++i)
215 if (facets_[i])
216 facets_[i]->__add_shared();
217}
218
219locale::__imp::__imp(const __imp& other, const string& name, locale::category c)
Howard Hinnantec3773c2011-12-01 20:21:04 +0000220 : facets_(N),
221 name_("*")
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000222{
223 facets_ = other.facets_;
224 for (unsigned i = 0; i < facets_.size(); ++i)
225 if (facets_[i])
226 facets_[i]->__add_shared();
Howard Hinnantd4444702010-08-11 17:04:31 +0000227#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000228 try
229 {
Howard Hinnant16e6e1d2010-08-22 00:03:27 +0000230#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000231 if (c & locale::collate)
232 {
233 install(new collate_byname<char>(name));
234 install(new collate_byname<wchar_t>(name));
235 }
236 if (c & locale::ctype)
237 {
238 install(new ctype_byname<char>(name));
239 install(new ctype_byname<wchar_t>(name));
240 install(new codecvt_byname<char, char, mbstate_t>(name));
241 install(new codecvt_byname<wchar_t, char, mbstate_t>(name));
242 install(new codecvt_byname<char16_t, char, mbstate_t>(name));
243 install(new codecvt_byname<char32_t, char, mbstate_t>(name));
244 }
245 if (c & locale::monetary)
246 {
247 install(new moneypunct_byname<char, false>(name));
248 install(new moneypunct_byname<char, true>(name));
249 install(new moneypunct_byname<wchar_t, false>(name));
250 install(new moneypunct_byname<wchar_t, true>(name));
251 }
252 if (c & locale::numeric)
253 {
254 install(new numpunct_byname<char>(name));
255 install(new numpunct_byname<wchar_t>(name));
256 }
257 if (c & locale::time)
258 {
259 install(new time_get_byname<char>(name));
260 install(new time_get_byname<wchar_t>(name));
261 install(new time_put_byname<char>(name));
262 install(new time_put_byname<wchar_t>(name));
263 }
264 if (c & locale::messages)
265 {
266 install(new messages_byname<char>(name));
267 install(new messages_byname<wchar_t>(name));
268 }
Howard Hinnantd4444702010-08-11 17:04:31 +0000269#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000270 }
271 catch (...)
272 {
273 for (unsigned i = 0; i < facets_.size(); ++i)
274 if (facets_[i])
275 facets_[i]->__release_shared();
276 throw;
277 }
Howard Hinnant16e6e1d2010-08-22 00:03:27 +0000278#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000279}
280
281template<class F>
282inline
283void
284locale::__imp::install_from(const locale::__imp& one)
285{
286 long id = F::id.__get();
287 install(const_cast<F*>(static_cast<const F*>(one.use_facet(id))), id);
288}
289
290locale::__imp::__imp(const __imp& other, const __imp& one, locale::category c)
Howard Hinnantec3773c2011-12-01 20:21:04 +0000291 : facets_(N),
292 name_("*")
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000293{
294 facets_ = other.facets_;
295 for (unsigned i = 0; i < facets_.size(); ++i)
296 if (facets_[i])
297 facets_[i]->__add_shared();
Howard Hinnantd4444702010-08-11 17:04:31 +0000298#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000299 try
300 {
Howard Hinnant16e6e1d2010-08-22 00:03:27 +0000301#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000302 if (c & locale::collate)
303 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000304 install_from<_VSTD::collate<char> >(one);
305 install_from<_VSTD::collate<wchar_t> >(one);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000306 }
307 if (c & locale::ctype)
308 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000309 install_from<_VSTD::ctype<char> >(one);
310 install_from<_VSTD::ctype<wchar_t> >(one);
311 install_from<_VSTD::codecvt<char, char, mbstate_t> >(one);
312 install_from<_VSTD::codecvt<char16_t, char, mbstate_t> >(one);
313 install_from<_VSTD::codecvt<char32_t, char, mbstate_t> >(one);
314 install_from<_VSTD::codecvt<wchar_t, char, mbstate_t> >(one);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000315 }
316 if (c & locale::monetary)
317 {
318 install_from<moneypunct<char, false> >(one);
319 install_from<moneypunct<char, true> >(one);
320 install_from<moneypunct<wchar_t, false> >(one);
321 install_from<moneypunct<wchar_t, true> >(one);
322 install_from<money_get<char> >(one);
323 install_from<money_get<wchar_t> >(one);
324 install_from<money_put<char> >(one);
325 install_from<money_put<wchar_t> >(one);
326 }
327 if (c & locale::numeric)
328 {
329 install_from<numpunct<char> >(one);
330 install_from<numpunct<wchar_t> >(one);
331 install_from<num_get<char> >(one);
332 install_from<num_get<wchar_t> >(one);
333 install_from<num_put<char> >(one);
334 install_from<num_put<wchar_t> >(one);
335 }
336 if (c & locale::time)
337 {
338 install_from<time_get<char> >(one);
339 install_from<time_get<wchar_t> >(one);
340 install_from<time_put<char> >(one);
341 install_from<time_put<wchar_t> >(one);
342 }
343 if (c & locale::messages)
344 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000345 install_from<_VSTD::messages<char> >(one);
346 install_from<_VSTD::messages<wchar_t> >(one);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000347 }
Howard Hinnantd4444702010-08-11 17:04:31 +0000348#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000349 }
350 catch (...)
351 {
352 for (unsigned i = 0; i < facets_.size(); ++i)
353 if (facets_[i])
354 facets_[i]->__release_shared();
355 throw;
356 }
Howard Hinnant16e6e1d2010-08-22 00:03:27 +0000357#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000358}
359
360locale::__imp::__imp(const __imp& other, facet* f, long id)
Howard Hinnantec3773c2011-12-01 20:21:04 +0000361 : facets_(max<size_t>(N, other.facets_.size()+1)),
362 name_("*")
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000363{
364 f->__add_shared();
365 unique_ptr<facet, release> hold(f);
366 facets_ = other.facets_;
367 for (unsigned i = 0; i < other.facets_.size(); ++i)
368 if (facets_[i])
369 facets_[i]->__add_shared();
370 install(hold.get(), id);
371}
372
373locale::__imp::~__imp()
374{
375 for (unsigned i = 0; i < facets_.size(); ++i)
376 if (facets_[i])
377 facets_[i]->__release_shared();
378}
379
380void
381locale::__imp::install(facet* f, long id)
382{
383 f->__add_shared();
384 unique_ptr<facet, release> hold(f);
Howard Hinnantec3773c2011-12-01 20:21:04 +0000385 if (static_cast<size_t>(id) >= facets_.size())
386 facets_.resize(static_cast<size_t>(id+1));
387 if (facets_[static_cast<size_t>(id)])
388 facets_[static_cast<size_t>(id)]->__release_shared();
389 facets_[static_cast<size_t>(id)] = hold.release();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000390}
391
392const locale::facet*
393locale::__imp::use_facet(long id) const
394{
Howard Hinnantd4444702010-08-11 17:04:31 +0000395#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000396 if (!has_facet(id))
397 throw bad_cast();
Howard Hinnant16e6e1d2010-08-22 00:03:27 +0000398#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantec3773c2011-12-01 20:21:04 +0000399 return facets_[static_cast<size_t>(id)];
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000400}
401
402// locale
403
404const locale&
405locale::__imp::make_classic()
406{
407 // only one thread can get in here and it only gets in once
408 static aligned_storage<sizeof(locale)>::type buf;
409 locale* c = (locale*)&buf;
Howard Hinnantec3773c2011-12-01 20:21:04 +0000410 c->__locale_ = &make<__imp>(1u);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000411 return *c;
412}
413
414const locale&
415locale::classic()
416{
417 static const locale& c = __imp::make_classic();
418 return c;
419}
420
421locale&
422locale::__imp::make_global()
423{
424 // only one thread can get in here and it only gets in once
425 static aligned_storage<sizeof(locale)>::type buf;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000426 ::new (&buf) locale(locale::classic());
427 return *(locale*)&buf;
428}
429
430locale&
431locale::__global()
432{
433 static locale& g = __imp::make_global();
434 return g;
435}
436
Howard Hinnantc9834542011-05-31 15:34:58 +0000437locale::locale() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000438 : __locale_(__global().__locale_)
439{
440 __locale_->__add_shared();
441}
442
Howard Hinnantc9834542011-05-31 15:34:58 +0000443locale::locale(const locale& l) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000444 : __locale_(l.__locale_)
445{
446 __locale_->__add_shared();
447}
448
Howard Hinnantc9834542011-05-31 15:34:58 +0000449locale::~locale()
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000450{
451 __locale_->__release_shared();
452}
453
454const locale&
Howard Hinnantc9834542011-05-31 15:34:58 +0000455locale::operator=(const locale& other) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000456{
457 other.__locale_->__add_shared();
458 __locale_->__release_shared();
459 __locale_ = other.__locale_;
460 return *this;
461}
462
463locale::locale(const char* name)
Howard Hinnantd4444702010-08-11 17:04:31 +0000464#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000465 : __locale_(name ? new __imp(name)
466 : throw runtime_error("locale constructed with null"))
Howard Hinnant16e6e1d2010-08-22 00:03:27 +0000467#else // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantd4444702010-08-11 17:04:31 +0000468 : __locale_(new __imp(name))
469#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000470{
471 __locale_->__add_shared();
472}
473
474locale::locale(const string& name)
475 : __locale_(new __imp(name))
476{
477 __locale_->__add_shared();
478}
479
480locale::locale(const locale& other, const char* name, category c)
Howard Hinnantd4444702010-08-11 17:04:31 +0000481#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000482 : __locale_(name ? new __imp(*other.__locale_, name, c)
483 : throw runtime_error("locale constructed with null"))
Howard Hinnant16e6e1d2010-08-22 00:03:27 +0000484#else // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantd4444702010-08-11 17:04:31 +0000485 : __locale_(new __imp(*other.__locale_, name, c))
486#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000487{
488 __locale_->__add_shared();
489}
490
491locale::locale(const locale& other, const string& name, category c)
492 : __locale_(new __imp(*other.__locale_, name, c))
493{
494 __locale_->__add_shared();
495}
496
497locale::locale(const locale& other, const locale& one, category c)
498 : __locale_(new __imp(*other.__locale_, *one.__locale_, c))
499{
500 __locale_->__add_shared();
501}
502
503string
504locale::name() const
505{
506 return __locale_->name();
507}
508
509void
510locale::__install_ctor(const locale& other, facet* f, long id)
511{
512 if (f)
513 __locale_ = new __imp(*other.__locale_, f, id);
514 else
515 __locale_ = other.__locale_;
516 __locale_->__add_shared();
517}
518
519locale
520locale::global(const locale& loc)
521{
522 locale& g = __global();
523 locale r = g;
524 g = loc;
525 if (g.name() != "*")
526 setlocale(LC_ALL, g.name().c_str());
527 return r;
528}
529
530bool
531locale::has_facet(id& x) const
532{
533 return __locale_->has_facet(x.__get());
534}
535
536const locale::facet*
537locale::use_facet(id& x) const
538{
539 return __locale_->use_facet(x.__get());
540}
541
542bool
543locale::operator==(const locale& y) const
544{
545 return (__locale_ == y.__locale_)
546 || (__locale_->name() != "*" && __locale_->name() == y.__locale_->name());
547}
548
549// locale::facet
550
551locale::facet::~facet()
552{
553}
554
555void
Howard Hinnant1694d232011-05-28 14:41:13 +0000556locale::facet::__on_zero_shared() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000557{
558 delete this;
559}
560
561// locale::id
562
563int32_t locale::id::__next_id = 0;
564
565namespace
566{
567
568class __fake_bind
569{
570 locale::id* id_;
571 void (locale::id::* pmf_)();
572public:
573 __fake_bind(void (locale::id::* pmf)(), locale::id* id)
574 : id_(id), pmf_(pmf) {}
575
576 void operator()() const
577 {
578 (id_->*pmf_)();
579 }
580};
581
582}
583
584long
585locale::id::__get()
586{
587 call_once(__flag_, __fake_bind(&locale::id::__init, this));
588 return __id_ - 1;
589}
590
591void
592locale::id::__init()
593{
Howard Hinnantadff4892010-05-24 17:49:41 +0000594 __id_ = __sync_add_and_fetch(&__next_id, 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000595}
596
597// template <> class collate_byname<char>
598
599collate_byname<char>::collate_byname(const char* n, size_t refs)
600 : collate<char>(refs),
601 __l(newlocale(LC_ALL_MASK, n, 0))
602{
Howard Hinnantd4444702010-08-11 17:04:31 +0000603#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000604 if (__l == 0)
605 throw runtime_error("collate_byname<char>::collate_byname"
606 " failed to construct for " + string(n));
Howard Hinnant16e6e1d2010-08-22 00:03:27 +0000607#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000608}
609
610collate_byname<char>::collate_byname(const string& name, size_t refs)
611 : collate<char>(refs),
612 __l(newlocale(LC_ALL_MASK, name.c_str(), 0))
613{
Howard Hinnantd4444702010-08-11 17:04:31 +0000614#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000615 if (__l == 0)
616 throw runtime_error("collate_byname<char>::collate_byname"
617 " failed to construct for " + name);
Howard Hinnant16e6e1d2010-08-22 00:03:27 +0000618#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000619}
620
621collate_byname<char>::~collate_byname()
622{
623 freelocale(__l);
624}
625
626int
627collate_byname<char>::do_compare(const char_type* __lo1, const char_type* __hi1,
628 const char_type* __lo2, const char_type* __hi2) const
629{
630 string_type lhs(__lo1, __hi1);
631 string_type rhs(__lo2, __hi2);
632 int r = strcoll_l(lhs.c_str(), rhs.c_str(), __l);
633 if (r < 0)
634 return -1;
635 if (r > 0)
636 return 1;
637 return r;
638}
639
640collate_byname<char>::string_type
641collate_byname<char>::do_transform(const char_type* lo, const char_type* hi) const
642{
643 const string_type in(lo, hi);
644 string_type out(strxfrm_l(0, in.c_str(), 0, __l), char());
645 strxfrm_l(const_cast<char*>(out.c_str()), in.c_str(), out.size()+1, __l);
646 return out;
647}
648
649// template <> class collate_byname<wchar_t>
650
651collate_byname<wchar_t>::collate_byname(const char* n, size_t refs)
652 : collate<wchar_t>(refs),
653 __l(newlocale(LC_ALL_MASK, n, 0))
654{
Howard Hinnantd4444702010-08-11 17:04:31 +0000655#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000656 if (__l == 0)
657 throw runtime_error("collate_byname<wchar_t>::collate_byname(size_t refs)"
658 " failed to construct for " + string(n));
Howard Hinnant16e6e1d2010-08-22 00:03:27 +0000659#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000660}
661
662collate_byname<wchar_t>::collate_byname(const string& name, size_t refs)
663 : collate<wchar_t>(refs),
664 __l(newlocale(LC_ALL_MASK, name.c_str(), 0))
665{
Howard Hinnantd4444702010-08-11 17:04:31 +0000666#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000667 if (__l == 0)
668 throw runtime_error("collate_byname<wchar_t>::collate_byname(size_t refs)"
669 " failed to construct for " + name);
Howard Hinnant16e6e1d2010-08-22 00:03:27 +0000670#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000671}
672
673collate_byname<wchar_t>::~collate_byname()
674{
675 freelocale(__l);
676}
677
678int
679collate_byname<wchar_t>::do_compare(const char_type* __lo1, const char_type* __hi1,
680 const char_type* __lo2, const char_type* __hi2) const
681{
682 string_type lhs(__lo1, __hi1);
683 string_type rhs(__lo2, __hi2);
684 int r = wcscoll_l(lhs.c_str(), rhs.c_str(), __l);
685 if (r < 0)
686 return -1;
687 if (r > 0)
688 return 1;
689 return r;
690}
691
692collate_byname<wchar_t>::string_type
693collate_byname<wchar_t>::do_transform(const char_type* lo, const char_type* hi) const
694{
695 const string_type in(lo, hi);
696 string_type out(wcsxfrm_l(0, in.c_str(), 0, __l), wchar_t());
697 wcsxfrm_l(const_cast<wchar_t*>(out.c_str()), in.c_str(), out.size()+1, __l);
698 return out;
699}
700
701// template <> class ctype<wchar_t>;
702
Howard Hinnant0a69fa12012-12-12 21:14:28 +0000703const ctype_base::mask ctype_base::space;
704const ctype_base::mask ctype_base::print;
705const ctype_base::mask ctype_base::cntrl;
706const ctype_base::mask ctype_base::upper;
707const ctype_base::mask ctype_base::lower;
708const ctype_base::mask ctype_base::alpha;
709const ctype_base::mask ctype_base::digit;
710const ctype_base::mask ctype_base::punct;
711const ctype_base::mask ctype_base::xdigit;
712const ctype_base::mask ctype_base::blank;
713const ctype_base::mask ctype_base::alnum;
714const ctype_base::mask ctype_base::graph;
715
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000716locale::id ctype<wchar_t>::id;
717
718ctype<wchar_t>::~ctype()
719{
720}
721
722bool
723ctype<wchar_t>::do_is(mask m, char_type c) const
724{
Sean Hunt62a6ac32011-07-09 00:56:23 +0000725 return isascii(c) ? ctype<char>::classic_table()[c] & m : false;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000726}
727
728const wchar_t*
729ctype<wchar_t>::do_is(const char_type* low, const char_type* high, mask* vec) const
730{
731 for (; low != high; ++low, ++vec)
Sean Hunt62a6ac32011-07-09 00:56:23 +0000732 *vec = static_cast<mask>(isascii(*low) ?
733 ctype<char>::classic_table()[*low] : 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000734 return low;
735}
736
737const wchar_t*
738ctype<wchar_t>::do_scan_is(mask m, const char_type* low, const char_type* high) const
739{
740 for (; low != high; ++low)
Sean Hunt62a6ac32011-07-09 00:56:23 +0000741 if (isascii(*low) && (ctype<char>::classic_table()[*low] & m))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000742 break;
743 return low;
744}
745
746const wchar_t*
747ctype<wchar_t>::do_scan_not(mask m, const char_type* low, const char_type* high) const
748{
749 for (; low != high; ++low)
Sean Hunt62a6ac32011-07-09 00:56:23 +0000750 if (!(isascii(*low) && (ctype<char>::classic_table()[*low] & m)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000751 break;
752 return low;
753}
754
755wchar_t
756ctype<wchar_t>::do_toupper(char_type c) const
757{
Howard Hinnant3c466fc2011-09-29 13:33:15 +0000758#ifdef _LIBCPP_HAS_DEFAULTRUNELOCALE
759 return isascii(c) ? _DefaultRuneLocale.__mapupper[c] : c;
760#elif defined(__GLIBC__)
Sean Hunt62a6ac32011-07-09 00:56:23 +0000761 return isascii(c) ? ctype<char>::__classic_upper_table()[c] : c;
Sean Hunte59f7242011-07-09 01:09:31 +0000762#else
Howard Hinnant3c466fc2011-09-29 13:33:15 +0000763 return (isascii(c) && iswlower_l(c, __cloc())) ? c-L'a'+L'A' : c;
Sean Hunte59f7242011-07-09 01:09:31 +0000764#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000765}
766
767const wchar_t*
768ctype<wchar_t>::do_toupper(char_type* low, const char_type* high) const
769{
770 for (; low != high; ++low)
Howard Hinnant3c466fc2011-09-29 13:33:15 +0000771#ifdef _LIBCPP_HAS_DEFAULTRUNELOCALE
772 *low = isascii(*low) ? _DefaultRuneLocale.__mapupper[*low] : *low;
773#elif defined(__GLIBC__)
Sean Hunt62a6ac32011-07-09 00:56:23 +0000774 *low = isascii(*low) ? ctype<char>::__classic_upper_table()[*low]
775 : *low;
Sean Hunte59f7242011-07-09 01:09:31 +0000776#else
Howard Hinnant3c466fc2011-09-29 13:33:15 +0000777 *low = (isascii(*low) && islower_l(*low, __cloc())) ? (*low-L'a'+L'A') : *low;
Sean Hunte59f7242011-07-09 01:09:31 +0000778#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000779 return low;
780}
781
782wchar_t
783ctype<wchar_t>::do_tolower(char_type c) const
784{
Howard Hinnant3c466fc2011-09-29 13:33:15 +0000785#ifdef _LIBCPP_HAS_DEFAULTRUNELOCALE
786 return isascii(c) ? _DefaultRuneLocale.__maplower[c] : c;
787#elif defined(__GLIBC__)
Sean Hunt62a6ac32011-07-09 00:56:23 +0000788 return isascii(c) ? ctype<char>::__classic_lower_table()[c] : c;
Sean Hunte59f7242011-07-09 01:09:31 +0000789#else
Howard Hinnant3c466fc2011-09-29 13:33:15 +0000790 return (isascii(c) && isupper_l(c, __cloc())) ? c-L'A'+'a' : c;
Sean Hunte59f7242011-07-09 01:09:31 +0000791#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000792}
793
794const wchar_t*
795ctype<wchar_t>::do_tolower(char_type* low, const char_type* high) const
796{
797 for (; low != high; ++low)
Howard Hinnant3c466fc2011-09-29 13:33:15 +0000798#ifdef _LIBCPP_HAS_DEFAULTRUNELOCALE
799 *low = isascii(*low) ? _DefaultRuneLocale.__maplower[*low] : *low;
800#elif defined(__GLIBC__)
Sean Hunt62a6ac32011-07-09 00:56:23 +0000801 *low = isascii(*low) ? ctype<char>::__classic_lower_table()[*low]
802 : *low;
Sean Hunte59f7242011-07-09 01:09:31 +0000803#else
Howard Hinnant3c466fc2011-09-29 13:33:15 +0000804 *low = (isascii(*low) && isupper_l(*low, __cloc())) ? *low-L'A'+L'a' : *low;
Sean Hunte59f7242011-07-09 01:09:31 +0000805#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000806 return low;
807}
808
809wchar_t
810ctype<wchar_t>::do_widen(char c) const
811{
812 return c;
813}
814
815const char*
816ctype<wchar_t>::do_widen(const char* low, const char* high, char_type* dest) const
817{
818 for (; low != high; ++low, ++dest)
819 *dest = *low;
820 return low;
821}
822
823char
824ctype<wchar_t>::do_narrow(char_type c, char dfault) const
825{
826 if (isascii(c))
827 return static_cast<char>(c);
828 return dfault;
829}
830
831const wchar_t*
832ctype<wchar_t>::do_narrow(const char_type* low, const char_type* high, char dfault, char* dest) const
833{
834 for (; low != high; ++low, ++dest)
835 if (isascii(*low))
Howard Hinnantec3773c2011-12-01 20:21:04 +0000836 *dest = static_cast<char>(*low);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000837 else
838 *dest = dfault;
839 return low;
840}
841
842// template <> class ctype<char>;
843
844locale::id ctype<char>::id;
845
846ctype<char>::ctype(const mask* tab, bool del, size_t refs)
847 : locale::facet(refs),
848 __tab_(tab),
849 __del_(del)
850{
Sean Hunt62a6ac32011-07-09 00:56:23 +0000851 if (__tab_ == 0)
852 __tab_ = classic_table();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000853}
854
855ctype<char>::~ctype()
856{
857 if (__tab_ && __del_)
858 delete [] __tab_;
859}
860
861char
862ctype<char>::do_toupper(char_type c) const
863{
Howard Hinnant3c466fc2011-09-29 13:33:15 +0000864#ifdef _LIBCPP_HAS_DEFAULTRUNELOCALE
Howard Hinnantec3773c2011-12-01 20:21:04 +0000865 return isascii(c) ?
866 static_cast<char>(_DefaultRuneLocale.__mapupper[static_cast<ptrdiff_t>(c)]) : c;
Howard Hinnant3c466fc2011-09-29 13:33:15 +0000867#elif defined(__GLIBC__)
Sean Hunt62a6ac32011-07-09 00:56:23 +0000868 return isascii(c) ? __classic_upper_table()[c] : c;
Sean Hunte59f7242011-07-09 01:09:31 +0000869#else
Howard Hinnant3c466fc2011-09-29 13:33:15 +0000870 return (isascii(c) && islower_l(c, __cloc())) ? c-'a'+'A' : c;
Sean Hunte59f7242011-07-09 01:09:31 +0000871#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000872}
873
874const char*
875ctype<char>::do_toupper(char_type* low, const char_type* high) const
876{
877 for (; low != high; ++low)
Howard Hinnant3c466fc2011-09-29 13:33:15 +0000878#ifdef _LIBCPP_HAS_DEFAULTRUNELOCALE
Howard Hinnantec3773c2011-12-01 20:21:04 +0000879 *low = isascii(*low) ?
880 static_cast<char>(_DefaultRuneLocale.__mapupper[static_cast<ptrdiff_t>(*low)]) : *low;
Howard Hinnant3c466fc2011-09-29 13:33:15 +0000881#elif defined(__GLIBC__)
Sean Hunt62a6ac32011-07-09 00:56:23 +0000882 *low = isascii(*low) ? __classic_upper_table()[*low] : *low;
Sean Hunte59f7242011-07-09 01:09:31 +0000883#else
Howard Hinnant3c466fc2011-09-29 13:33:15 +0000884 *low = (isascii(*low) && islower_l(*low, __cloc())) ? *low-'a'+'A' : *low;
Sean Hunte59f7242011-07-09 01:09:31 +0000885#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000886 return low;
887}
888
889char
890ctype<char>::do_tolower(char_type c) const
891{
Howard Hinnant3c466fc2011-09-29 13:33:15 +0000892#ifdef _LIBCPP_HAS_DEFAULTRUNELOCALE
Howard Hinnantec3773c2011-12-01 20:21:04 +0000893 return isascii(c) ?
894 static_cast<char>(_DefaultRuneLocale.__maplower[static_cast<ptrdiff_t>(c)]) : c;
Howard Hinnant3c466fc2011-09-29 13:33:15 +0000895#elif defined(__GLIBC__)
Sean Hunt62a6ac32011-07-09 00:56:23 +0000896 return isascii(c) ? __classic_lower_table()[c] : c;
Sean Hunte59f7242011-07-09 01:09:31 +0000897#else
Howard Hinnant3c466fc2011-09-29 13:33:15 +0000898 return (isascii(c) && isupper_l(c, __cloc())) ? c-'A'+'a' : c;
Sean Hunte59f7242011-07-09 01:09:31 +0000899#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000900}
901
902const char*
903ctype<char>::do_tolower(char_type* low, const char_type* high) const
904{
905 for (; low != high; ++low)
Howard Hinnant3c466fc2011-09-29 13:33:15 +0000906#ifdef _LIBCPP_HAS_DEFAULTRUNELOCALE
Howard Hinnantec3773c2011-12-01 20:21:04 +0000907 *low = isascii(*low) ? static_cast<char>(_DefaultRuneLocale.__maplower[static_cast<ptrdiff_t>(*low)]) : *low;
Howard Hinnant3c466fc2011-09-29 13:33:15 +0000908#elif defined(__GLIBC__)
Sean Hunt62a6ac32011-07-09 00:56:23 +0000909 *low = isascii(*low) ? __classic_lower_table()[*low] : *low;
Sean Hunte59f7242011-07-09 01:09:31 +0000910#else
Howard Hinnant3c466fc2011-09-29 13:33:15 +0000911 *low = (isascii(*low) && isupper_l(*low, __cloc())) ? *low-'A'+'a' : *low;
Sean Hunte59f7242011-07-09 01:09:31 +0000912#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000913 return low;
914}
915
916char
917ctype<char>::do_widen(char c) const
918{
919 return c;
920}
921
922const char*
923ctype<char>::do_widen(const char* low, const char* high, char_type* dest) const
924{
925 for (; low != high; ++low, ++dest)
926 *dest = *low;
927 return low;
928}
929
930char
931ctype<char>::do_narrow(char_type c, char dfault) const
932{
933 if (isascii(c))
934 return static_cast<char>(c);
935 return dfault;
936}
937
938const char*
939ctype<char>::do_narrow(const char_type* low, const char_type* high, char dfault, char* dest) const
940{
941 for (; low != high; ++low, ++dest)
942 if (isascii(*low))
943 *dest = *low;
944 else
945 *dest = dfault;
946 return low;
947}
948
949const ctype<char>::mask*
Howard Hinnantc9834542011-05-31 15:34:58 +0000950ctype<char>::classic_table() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000951{
David Chisnallc512df12011-09-21 08:39:44 +0000952#if defined(__APPLE__) || defined(__FreeBSD__)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000953 return _DefaultRuneLocale.__runetype;
Sean Hunt62a6ac32011-07-09 00:56:23 +0000954#elif defined(__GLIBC__)
955 return __cloc()->__ctype_b;
David Chisnall997e4542012-02-29 13:05:08 +0000956#elif __sun__
957 return __ctype_mask;
Howard Hinnant3c466fc2011-09-29 13:33:15 +0000958#elif _WIN32
959 return _ctype+1; // internal ctype mask table defined in msvcrt.dll
David Chisnallc512df12011-09-21 08:39:44 +0000960// This is assumed to be safe, which is a nonsense assumption because we're
961// going to end up dereferencing it later...
Sean Hunt62a6ac32011-07-09 00:56:23 +0000962#else
David Chisnall997e4542012-02-29 13:05:08 +0000963 // Platform not supported: abort so the person doing the port knows what to
964 // fix
Howard Hinnantfcbaf482012-02-29 16:08:57 +0000965# warning ctype<char>::classic_table() is not implemented
David Chisnall997e4542012-02-29 13:05:08 +0000966 abort();
Sean Hunt62a6ac32011-07-09 00:56:23 +0000967 return NULL;
968#endif
969}
970
Howard Hinnant3c466fc2011-09-29 13:33:15 +0000971#if defined(__GLIBC__)
Sean Hunt62a6ac32011-07-09 00:56:23 +0000972const int*
973ctype<char>::__classic_lower_table() _NOEXCEPT
974{
Sean Hunt62a6ac32011-07-09 00:56:23 +0000975 return __cloc()->__ctype_tolower;
Sean Hunt62a6ac32011-07-09 00:56:23 +0000976}
977
978const int*
979ctype<char>::__classic_upper_table() _NOEXCEPT
980{
Sean Hunt62a6ac32011-07-09 00:56:23 +0000981 return __cloc()->__ctype_toupper;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000982}
Howard Hinnant3c466fc2011-09-29 13:33:15 +0000983#endif // __GLIBC__
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000984
985// template <> class ctype_byname<char>
986
987ctype_byname<char>::ctype_byname(const char* name, size_t refs)
988 : ctype<char>(0, false, refs),
989 __l(newlocale(LC_ALL_MASK, name, 0))
990{
Howard Hinnantd4444702010-08-11 17:04:31 +0000991#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000992 if (__l == 0)
993 throw runtime_error("ctype_byname<char>::ctype_byname"
994 " failed to construct for " + string(name));
Howard Hinnant16e6e1d2010-08-22 00:03:27 +0000995#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000996}
997
998ctype_byname<char>::ctype_byname(const string& name, size_t refs)
999 : ctype<char>(0, false, refs),
1000 __l(newlocale(LC_ALL_MASK, name.c_str(), 0))
1001{
Howard Hinnantd4444702010-08-11 17:04:31 +00001002#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001003 if (__l == 0)
1004 throw runtime_error("ctype_byname<char>::ctype_byname"
1005 " failed to construct for " + name);
Howard Hinnant16e6e1d2010-08-22 00:03:27 +00001006#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001007}
1008
1009ctype_byname<char>::~ctype_byname()
1010{
1011 freelocale(__l);
1012}
1013
1014char
1015ctype_byname<char>::do_toupper(char_type c) const
1016{
Howard Hinnantec3773c2011-12-01 20:21:04 +00001017 return static_cast<char>(toupper_l(c, __l));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001018}
1019
1020const char*
1021ctype_byname<char>::do_toupper(char_type* low, const char_type* high) const
1022{
1023 for (; low != high; ++low)
Howard Hinnantec3773c2011-12-01 20:21:04 +00001024 *low = static_cast<char>(toupper_l(*low, __l));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001025 return low;
1026}
1027
1028char
1029ctype_byname<char>::do_tolower(char_type c) const
1030{
Howard Hinnantec3773c2011-12-01 20:21:04 +00001031 return static_cast<char>(tolower_l(c, __l));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001032}
1033
1034const char*
1035ctype_byname<char>::do_tolower(char_type* low, const char_type* high) const
1036{
1037 for (; low != high; ++low)
Howard Hinnantec3773c2011-12-01 20:21:04 +00001038 *low = static_cast<char>(tolower_l(*low, __l));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001039 return low;
1040}
1041
1042// template <> class ctype_byname<wchar_t>
1043
1044ctype_byname<wchar_t>::ctype_byname(const char* name, size_t refs)
1045 : ctype<wchar_t>(refs),
1046 __l(newlocale(LC_ALL_MASK, name, 0))
1047{
Howard Hinnantd4444702010-08-11 17:04:31 +00001048#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001049 if (__l == 0)
1050 throw runtime_error("ctype_byname<wchar_t>::ctype_byname"
1051 " failed to construct for " + string(name));
Howard Hinnant16e6e1d2010-08-22 00:03:27 +00001052#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001053}
1054
1055ctype_byname<wchar_t>::ctype_byname(const string& name, size_t refs)
1056 : ctype<wchar_t>(refs),
1057 __l(newlocale(LC_ALL_MASK, name.c_str(), 0))
1058{
Howard Hinnantd4444702010-08-11 17:04:31 +00001059#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001060 if (__l == 0)
1061 throw runtime_error("ctype_byname<wchar_t>::ctype_byname"
1062 " failed to construct for " + name);
Howard Hinnant16e6e1d2010-08-22 00:03:27 +00001063#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001064}
1065
1066ctype_byname<wchar_t>::~ctype_byname()
1067{
1068 freelocale(__l);
1069}
1070
1071bool
1072ctype_byname<wchar_t>::do_is(mask m, char_type c) const
1073{
Sean Hunt6f0342c2011-07-09 03:40:04 +00001074#ifdef _LIBCPP_WCTYPE_IS_MASK
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001075 return static_cast<bool>(iswctype_l(c, m, __l));
Sean Hunt6f0342c2011-07-09 03:40:04 +00001076#else
Howard Hinnantef793f22012-08-02 18:35:07 +00001077 bool result = false;
1078 if (m & space) result |= (iswspace_l(c, __l) != 0);
1079 if (m & print) result |= (iswprint_l(c, __l) != 0);
1080 if (m & cntrl) result |= (iswcntrl_l(c, __l) != 0);
1081 if (m & upper) result |= (iswupper_l(c, __l) != 0);
1082 if (m & lower) result |= (iswlower_l(c, __l) != 0);
1083 if (m & alpha) result |= (iswalpha_l(c, __l) != 0);
1084 if (m & digit) result |= (iswdigit_l(c, __l) != 0);
1085 if (m & punct) result |= (iswpunct_l(c, __l) != 0);
1086 if (m & xdigit) result |= (iswxdigit_l(c, __l) != 0);
1087 if (m & blank) result |= (iswblank_l(c, __l) != 0);
Howard Hinnant3c466fc2011-09-29 13:33:15 +00001088 return result;
Sean Hunt6f0342c2011-07-09 03:40:04 +00001089#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001090}
1091
1092const wchar_t*
1093ctype_byname<wchar_t>::do_is(const char_type* low, const char_type* high, mask* vec) const
1094{
1095 for (; low != high; ++low, ++vec)
1096 {
1097 if (isascii(*low))
Sean Hunt62a6ac32011-07-09 00:56:23 +00001098 *vec = static_cast<mask>(ctype<char>::classic_table()[*low]);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001099 else
1100 {
1101 *vec = 0;
1102 if (iswspace_l(*low, __l))
1103 *vec |= space;
1104 if (iswprint_l(*low, __l))
1105 *vec |= print;
1106 if (iswcntrl_l(*low, __l))
1107 *vec |= cntrl;
1108 if (iswupper_l(*low, __l))
1109 *vec |= upper;
1110 if (iswlower_l(*low, __l))
1111 *vec |= lower;
1112 if (iswalpha_l(*low, __l))
1113 *vec |= alpha;
1114 if (iswdigit_l(*low, __l))
1115 *vec |= digit;
1116 if (iswpunct_l(*low, __l))
1117 *vec |= punct;
1118 if (iswxdigit_l(*low, __l))
1119 *vec |= xdigit;
1120 }
1121 }
1122 return low;
1123}
1124
1125const wchar_t*
1126ctype_byname<wchar_t>::do_scan_is(mask m, const char_type* low, const char_type* high) const
1127{
1128 for (; low != high; ++low)
Sean Hunt6f0342c2011-07-09 03:40:04 +00001129 {
1130#ifdef _LIBCPP_WCTYPE_IS_MASK
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001131 if (iswctype_l(*low, m, __l))
1132 break;
Sean Hunt6f0342c2011-07-09 03:40:04 +00001133#else
Howard Hinnantef793f22012-08-02 18:35:07 +00001134 if (m & space && iswspace_l(*low, __l)) break;
1135 if (m & print && iswprint_l(*low, __l)) break;
1136 if (m & cntrl && iswcntrl_l(*low, __l)) break;
1137 if (m & upper && iswupper_l(*low, __l)) break;
1138 if (m & lower && iswlower_l(*low, __l)) break;
1139 if (m & alpha && iswalpha_l(*low, __l)) break;
1140 if (m & digit && iswdigit_l(*low, __l)) break;
1141 if (m & punct && iswpunct_l(*low, __l)) break;
1142 if (m & xdigit && iswxdigit_l(*low, __l)) break;
1143 if (m & blank && iswblank_l(*low, __l)) break;
Sean Hunt6f0342c2011-07-09 03:40:04 +00001144#endif
1145 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001146 return low;
1147}
1148
1149const wchar_t*
1150ctype_byname<wchar_t>::do_scan_not(mask m, const char_type* low, const char_type* high) const
1151{
1152 for (; low != high; ++low)
Sean Hunt6f0342c2011-07-09 03:40:04 +00001153 {
1154#ifdef _LIBCPP_WCTYPE_IS_MASK
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001155 if (!iswctype_l(*low, m, __l))
1156 break;
Sean Hunt6f0342c2011-07-09 03:40:04 +00001157#else
1158 if (m & space && iswspace_l(*low, __l)) continue;
1159 if (m & print && iswprint_l(*low, __l)) continue;
1160 if (m & cntrl && iswcntrl_l(*low, __l)) continue;
1161 if (m & upper && iswupper_l(*low, __l)) continue;
1162 if (m & lower && iswlower_l(*low, __l)) continue;
1163 if (m & alpha && iswalpha_l(*low, __l)) continue;
1164 if (m & digit && iswdigit_l(*low, __l)) continue;
1165 if (m & punct && iswpunct_l(*low, __l)) continue;
1166 if (m & xdigit && iswxdigit_l(*low, __l)) continue;
1167 if (m & blank && iswblank_l(*low, __l)) continue;
1168 break;
1169#endif
1170 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001171 return low;
1172}
1173
1174wchar_t
1175ctype_byname<wchar_t>::do_toupper(char_type c) const
1176{
1177 return towupper_l(c, __l);
1178}
1179
1180const wchar_t*
1181ctype_byname<wchar_t>::do_toupper(char_type* low, const char_type* high) const
1182{
1183 for (; low != high; ++low)
1184 *low = towupper_l(*low, __l);
1185 return low;
1186}
1187
1188wchar_t
1189ctype_byname<wchar_t>::do_tolower(char_type c) const
1190{
1191 return towlower_l(c, __l);
1192}
1193
1194const wchar_t*
1195ctype_byname<wchar_t>::do_tolower(char_type* low, const char_type* high) const
1196{
1197 for (; low != high; ++low)
1198 *low = towlower_l(*low, __l);
1199 return low;
1200}
1201
1202wchar_t
1203ctype_byname<wchar_t>::do_widen(char c) const
1204{
Howard Hinnant866569b2011-09-28 23:39:33 +00001205#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Sean Huntf3907e62011-07-15 05:40:33 +00001206 return btowc_l(c, __l);
1207#else
1208 return __btowc_l(c, __l);
1209#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001210}
1211
1212const char*
1213ctype_byname<wchar_t>::do_widen(const char* low, const char* high, char_type* dest) const
1214{
1215 for (; low != high; ++low, ++dest)
Howard Hinnant866569b2011-09-28 23:39:33 +00001216#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Sean Huntf3907e62011-07-15 05:40:33 +00001217 *dest = btowc_l(*low, __l);
1218#else
1219 *dest = __btowc_l(*low, __l);
1220#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001221 return low;
1222}
1223
1224char
1225ctype_byname<wchar_t>::do_narrow(char_type c, char dfault) const
1226{
Howard Hinnant866569b2011-09-28 23:39:33 +00001227#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Sean Huntf3907e62011-07-15 05:40:33 +00001228 int r = wctob_l(c, __l);
1229#else
1230 int r = __wctob_l(c, __l);
1231#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001232 return r != WEOF ? static_cast<char>(r) : dfault;
1233}
1234
1235const wchar_t*
1236ctype_byname<wchar_t>::do_narrow(const char_type* low, const char_type* high, char dfault, char* dest) const
1237{
1238 for (; low != high; ++low, ++dest)
1239 {
Howard Hinnant866569b2011-09-28 23:39:33 +00001240#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Sean Huntf3907e62011-07-15 05:40:33 +00001241 int r = wctob_l(*low, __l);
1242#else
1243 int r = __wctob_l(*low, __l);
1244#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001245 *dest = r != WEOF ? static_cast<char>(r) : dfault;
1246 }
1247 return low;
1248}
1249
1250// template <> class codecvt<char, char, mbstate_t>
1251
Howard Hinnant16e6e1d2010-08-22 00:03:27 +00001252locale::id codecvt<char, char, mbstate_t>::id;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001253
1254codecvt<char, char, mbstate_t>::~codecvt()
1255{
1256}
1257
1258codecvt<char, char, mbstate_t>::result
Howard Hinnant16e6e1d2010-08-22 00:03:27 +00001259codecvt<char, char, mbstate_t>::do_out(state_type&,
1260 const intern_type* frm, const intern_type*, const intern_type*& frm_nxt,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001261 extern_type* to, extern_type*, extern_type*& to_nxt) const
1262{
1263 frm_nxt = frm;
1264 to_nxt = to;
1265 return noconv;
1266}
1267
1268codecvt<char, char, mbstate_t>::result
Howard Hinnant16e6e1d2010-08-22 00:03:27 +00001269codecvt<char, char, mbstate_t>::do_in(state_type&,
1270 const extern_type* frm, const extern_type*, const extern_type*& frm_nxt,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001271 intern_type* to, intern_type*, intern_type*& to_nxt) const
1272{
1273 frm_nxt = frm;
1274 to_nxt = to;
1275 return noconv;
1276}
1277
1278codecvt<char, char, mbstate_t>::result
Howard Hinnant16e6e1d2010-08-22 00:03:27 +00001279codecvt<char, char, mbstate_t>::do_unshift(state_type&,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001280 extern_type* to, extern_type*, extern_type*& to_nxt) const
1281{
1282 to_nxt = to;
1283 return noconv;
1284}
1285
1286int
Howard Hinnantc9834542011-05-31 15:34:58 +00001287codecvt<char, char, mbstate_t>::do_encoding() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001288{
1289 return 1;
1290}
1291
1292bool
Howard Hinnantc9834542011-05-31 15:34:58 +00001293codecvt<char, char, mbstate_t>::do_always_noconv() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001294{
1295 return true;
1296}
1297
1298int
1299codecvt<char, char, mbstate_t>::do_length(state_type&,
1300 const extern_type* frm, const extern_type* end, size_t mx) const
1301{
Howard Hinnantec3773c2011-12-01 20:21:04 +00001302 return static_cast<int>(min<size_t>(mx, static_cast<size_t>(end-frm)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001303}
1304
1305int
Howard Hinnantc9834542011-05-31 15:34:58 +00001306codecvt<char, char, mbstate_t>::do_max_length() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001307{
1308 return 1;
1309}
1310
1311// template <> class codecvt<wchar_t, char, mbstate_t>
1312
Howard Hinnant16e6e1d2010-08-22 00:03:27 +00001313locale::id codecvt<wchar_t, char, mbstate_t>::id;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001314
1315codecvt<wchar_t, char, mbstate_t>::codecvt(size_t refs)
1316 : locale::facet(refs),
1317 __l(0)
1318{
1319}
1320
1321codecvt<wchar_t, char, mbstate_t>::codecvt(const char* nm, size_t refs)
1322 : locale::facet(refs),
1323 __l(newlocale(LC_ALL_MASK, nm, 0))
1324{
Howard Hinnantd4444702010-08-11 17:04:31 +00001325#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001326 if (__l == 0)
1327 throw runtime_error("codecvt_byname<wchar_t, char, mbstate_t>::codecvt_byname"
1328 " failed to construct for " + string(nm));
Howard Hinnant16e6e1d2010-08-22 00:03:27 +00001329#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001330}
1331
1332codecvt<wchar_t, char, mbstate_t>::~codecvt()
1333{
1334 if (__l != 0)
1335 freelocale(__l);
1336}
1337
1338codecvt<wchar_t, char, mbstate_t>::result
1339codecvt<wchar_t, char, mbstate_t>::do_out(state_type& st,
Howard Hinnant16e6e1d2010-08-22 00:03:27 +00001340 const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001341 extern_type* to, extern_type* to_end, extern_type*& to_nxt) const
1342{
1343 // look for first internal null in frm
1344 const intern_type* fend = frm;
1345 for (; fend != frm_end; ++fend)
1346 if (*fend == 0)
1347 break;
1348 // loop over all null-terminated sequences in frm
1349 to_nxt = to;
1350 for (frm_nxt = frm; frm != frm_end && to != to_end; frm = frm_nxt, to = to_nxt)
1351 {
1352 // save state in case needed to reover to_nxt on error
1353 mbstate_t save_state = st;
Howard Hinnant866569b2011-09-28 23:39:33 +00001354#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnantec3773c2011-12-01 20:21:04 +00001355 size_t n = wcsnrtombs_l(to, &frm_nxt, static_cast<size_t>(fend-frm),
1356 static_cast<size_t>(to_end-to), &st, __l);
Sean Huntf3907e62011-07-15 05:40:33 +00001357#else
1358 size_t n = __wcsnrtombs_l(to, &frm_nxt, fend-frm, to_end-to, &st, __l);
1359#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001360 if (n == size_t(-1))
1361 {
1362 // need to recover to_nxt
1363 for (to_nxt = to; frm != frm_nxt; ++frm)
1364 {
Howard Hinnant866569b2011-09-28 23:39:33 +00001365#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Sean Huntf3907e62011-07-15 05:40:33 +00001366 n = wcrtomb_l(to_nxt, *frm, &save_state, __l);
1367#else
1368 n = __wcrtomb_l(to_nxt, *frm, &save_state, __l);
1369#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001370 if (n == size_t(-1))
1371 break;
1372 to_nxt += n;
1373 }
1374 frm_nxt = frm;
1375 return error;
1376 }
1377 if (n == 0)
1378 return partial;
1379 to_nxt += n;
1380 if (to_nxt == to_end)
1381 break;
1382 if (fend != frm_end) // set up next null terminated sequence
1383 {
1384 // Try to write the terminating null
1385 extern_type tmp[MB_LEN_MAX];
Howard Hinnant866569b2011-09-28 23:39:33 +00001386#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Sean Huntf3907e62011-07-15 05:40:33 +00001387 n = wcrtomb_l(tmp, intern_type(), &st, __l);
1388#else
1389 n = __wcrtomb_l(tmp, intern_type(), &st, __l);
1390#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001391 if (n == size_t(-1)) // on error
1392 return error;
Howard Hinnantec3773c2011-12-01 20:21:04 +00001393 if (n > static_cast<size_t>(to_end-to_nxt)) // is there room?
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001394 return partial;
1395 for (extern_type* p = tmp; n; --n) // write it
1396 *to_nxt++ = *p++;
1397 ++frm_nxt;
1398 // look for next null in frm
1399 for (fend = frm_nxt; fend != frm_end; ++fend)
1400 if (*fend == 0)
1401 break;
1402 }
1403 }
1404 return frm_nxt == frm_end ? ok : partial;
1405}
1406
1407codecvt<wchar_t, char, mbstate_t>::result
1408codecvt<wchar_t, char, mbstate_t>::do_in(state_type& st,
Howard Hinnant16e6e1d2010-08-22 00:03:27 +00001409 const extern_type* frm, const extern_type* frm_end, const extern_type*& frm_nxt,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001410 intern_type* to, intern_type* to_end, intern_type*& to_nxt) const
1411{
1412 // look for first internal null in frm
1413 const extern_type* fend = frm;
1414 for (; fend != frm_end; ++fend)
1415 if (*fend == 0)
1416 break;
1417 // loop over all null-terminated sequences in frm
1418 to_nxt = to;
1419 for (frm_nxt = frm; frm != frm_end && to != to_end; frm = frm_nxt, to = to_nxt)
1420 {
1421 // save state in case needed to reover to_nxt on error
1422 mbstate_t save_state = st;
Howard Hinnant866569b2011-09-28 23:39:33 +00001423#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnantec3773c2011-12-01 20:21:04 +00001424 size_t n = mbsnrtowcs_l(to, &frm_nxt, static_cast<size_t>(fend-frm),
1425 static_cast<size_t>(to_end-to), &st, __l);
Sean Huntf3907e62011-07-15 05:40:33 +00001426#else
1427 size_t n = __mbsnrtowcs_l(to, &frm_nxt, fend-frm, to_end-to, &st, __l);
1428#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001429 if (n == size_t(-1))
1430 {
1431 // need to recover to_nxt
1432 for (to_nxt = to; frm != frm_nxt; ++to_nxt)
1433 {
Howard Hinnant866569b2011-09-28 23:39:33 +00001434#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnantec3773c2011-12-01 20:21:04 +00001435 n = mbrtowc_l(to_nxt, frm, static_cast<size_t>(fend-frm),
1436 &save_state, __l);
Sean Huntf3907e62011-07-15 05:40:33 +00001437#else
1438 n = __mbrtowc_l(to_nxt, frm, fend-frm, &save_state, __l);
1439#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001440 switch (n)
1441 {
1442 case 0:
1443 ++frm;
1444 break;
Howard Hinnant982331b2012-02-08 19:15:06 +00001445 case size_t(-1):
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001446 frm_nxt = frm;
1447 return error;
Howard Hinnant982331b2012-02-08 19:15:06 +00001448 case size_t(-2):
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001449 frm_nxt = frm;
1450 return partial;
1451 default:
1452 frm += n;
1453 break;
1454 }
1455 }
1456 frm_nxt = frm;
1457 return frm_nxt == frm_end ? ok : partial;
1458 }
1459 if (n == 0)
1460 return error;
1461 to_nxt += n;
1462 if (to_nxt == to_end)
1463 break;
1464 if (fend != frm_end) // set up next null terminated sequence
1465 {
1466 // Try to write the terminating null
Howard Hinnant866569b2011-09-28 23:39:33 +00001467#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Sean Huntf3907e62011-07-15 05:40:33 +00001468 n = mbrtowc_l(to_nxt, frm_nxt, 1, &st, __l);
1469#else
1470 n = __mbrtowc_l(to_nxt, frm_nxt, 1, &st, __l);
1471#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001472 if (n != 0) // on error
1473 return error;
1474 ++to_nxt;
1475 ++frm_nxt;
1476 // look for next null in frm
1477 for (fend = frm_nxt; fend != frm_end; ++fend)
1478 if (*fend == 0)
1479 break;
1480 }
1481 }
1482 return frm_nxt == frm_end ? ok : partial;
1483}
1484
1485codecvt<wchar_t, char, mbstate_t>::result
1486codecvt<wchar_t, char, mbstate_t>::do_unshift(state_type& st,
1487 extern_type* to, extern_type* to_end, extern_type*& to_nxt) const
1488{
1489 to_nxt = to;
1490 extern_type tmp[MB_LEN_MAX];
Howard Hinnant866569b2011-09-28 23:39:33 +00001491#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Sean Huntf3907e62011-07-15 05:40:33 +00001492 size_t n = wcrtomb_l(tmp, intern_type(), &st, __l);
1493#else
1494 size_t n = __wcrtomb_l(tmp, intern_type(), &st, __l);
1495#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001496 if (n == size_t(-1) || n == 0) // on error
1497 return error;
1498 --n;
Howard Hinnantec3773c2011-12-01 20:21:04 +00001499 if (n > static_cast<size_t>(to_end-to_nxt)) // is there room?
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001500 return partial;
1501 for (extern_type* p = tmp; n; --n) // write it
1502 *to_nxt++ = *p++;
1503 return ok;
1504}
1505
1506int
Howard Hinnantc9834542011-05-31 15:34:58 +00001507codecvt<wchar_t, char, mbstate_t>::do_encoding() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001508{
Howard Hinnant866569b2011-09-28 23:39:33 +00001509#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Sean Huntf3907e62011-07-15 05:40:33 +00001510 if (mbtowc_l((wchar_t*) 0, (const char*) 0, MB_LEN_MAX, __l) == 0)
1511#else
1512 if (__mbtowc_l((wchar_t*) 0, (const char*) 0, MB_LEN_MAX, __l) == 0)
1513#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001514 {
1515 // stateless encoding
Howard Hinnant866569b2011-09-28 23:39:33 +00001516#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Sean Huntf3907e62011-07-15 05:40:33 +00001517 if (__l == 0 || MB_CUR_MAX_L(__l) == 1) // there are no known constant length encodings
1518#else
1519 if (__l == 0 || __mb_cur_max_l(__l) == 1) // there are no known constant length encodings
1520#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001521 return 1; // which take more than 1 char to form a wchar_t
1522 return 0;
1523 }
1524 return -1;
1525}
1526
1527bool
Howard Hinnantc9834542011-05-31 15:34:58 +00001528codecvt<wchar_t, char, mbstate_t>::do_always_noconv() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001529{
1530 return false;
1531}
1532
1533int
1534codecvt<wchar_t, char, mbstate_t>::do_length(state_type& st,
1535 const extern_type* frm, const extern_type* frm_end, size_t mx) const
1536{
1537 int nbytes = 0;
1538 for (size_t nwchar_t = 0; nwchar_t < mx && frm != frm_end; ++nwchar_t)
1539 {
Howard Hinnant866569b2011-09-28 23:39:33 +00001540#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnantec3773c2011-12-01 20:21:04 +00001541 size_t n = mbrlen_l(frm, static_cast<size_t>(frm_end-frm), &st, __l);
Sean Huntf3907e62011-07-15 05:40:33 +00001542#else
1543 size_t n = __mbrlen_l(frm, frm_end-frm, &st, __l);
1544#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001545 switch (n)
1546 {
1547 case 0:
1548 ++nbytes;
1549 ++frm;
1550 break;
Howard Hinnant982331b2012-02-08 19:15:06 +00001551 case size_t(-1):
1552 case size_t(-2):
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001553 return nbytes;
1554 default:
1555 nbytes += n;
1556 frm += n;
1557 break;
1558 }
1559 }
1560 return nbytes;
1561}
1562
1563int
Howard Hinnantc9834542011-05-31 15:34:58 +00001564codecvt<wchar_t, char, mbstate_t>::do_max_length() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001565{
Howard Hinnant866569b2011-09-28 23:39:33 +00001566#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Sean Huntf3907e62011-07-15 05:40:33 +00001567 return __l == 0 ? 1 : MB_CUR_MAX_L(__l);
1568#else
1569 return __l == 0 ? 1 : __mb_cur_max_l(__l);
1570#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001571}
1572
1573// Valid UTF ranges
1574// UTF-32 UTF-16 UTF-8 # of code points
1575// first second first second third fourth
1576// 000000 - 00007F 0000 - 007F 00 - 7F 127
1577// 000080 - 0007FF 0080 - 07FF C2 - DF, 80 - BF 1920
1578// 000800 - 000FFF 0800 - 0FFF E0 - E0, A0 - BF, 80 - BF 2048
1579// 001000 - 00CFFF 1000 - CFFF E1 - EC, 80 - BF, 80 - BF 49152
1580// 00D000 - 00D7FF D000 - D7FF ED - ED, 80 - 9F, 80 - BF 2048
1581// 00D800 - 00DFFF invalid
1582// 00E000 - 00FFFF E000 - FFFF EE - EF, 80 - BF, 80 - BF 8192
1583// 010000 - 03FFFF D800 - D8BF, DC00 - DFFF F0 - F0, 90 - BF, 80 - BF, 80 - BF 196608
1584// 040000 - 0FFFFF D8C0 - DBBF, DC00 - DFFF F1 - F3, 80 - BF, 80 - BF, 80 - BF 786432
1585// 100000 - 10FFFF DBC0 - DBFF, DC00 - DFFF F4 - F4, 80 - 8F, 80 - BF, 80 - BF 65536
1586
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00001587static
1588codecvt_base::result
1589utf16_to_utf8(const uint16_t* frm, const uint16_t* frm_end, const uint16_t*& frm_nxt,
1590 uint8_t* to, uint8_t* to_end, uint8_t*& to_nxt,
1591 unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0))
1592{
1593 frm_nxt = frm;
1594 to_nxt = to;
1595 if (mode & generate_header)
1596 {
1597 if (to_end-to_nxt < 3)
1598 return codecvt_base::partial;
1599 *to_nxt++ = static_cast<uint8_t>(0xEF);
1600 *to_nxt++ = static_cast<uint8_t>(0xBB);
1601 *to_nxt++ = static_cast<uint8_t>(0xBF);
1602 }
1603 for (; frm_nxt < frm_end; ++frm_nxt)
1604 {
1605 uint16_t wc1 = *frm_nxt;
1606 if (wc1 > Maxcode)
1607 return codecvt_base::error;
1608 if (wc1 < 0x0080)
1609 {
1610 if (to_end-to_nxt < 1)
1611 return codecvt_base::partial;
1612 *to_nxt++ = static_cast<uint8_t>(wc1);
1613 }
1614 else if (wc1 < 0x0800)
1615 {
1616 if (to_end-to_nxt < 2)
1617 return codecvt_base::partial;
1618 *to_nxt++ = static_cast<uint8_t>(0xC0 | (wc1 >> 6));
1619 *to_nxt++ = static_cast<uint8_t>(0x80 | (wc1 & 0x03F));
1620 }
1621 else if (wc1 < 0xD800)
1622 {
1623 if (to_end-to_nxt < 3)
1624 return codecvt_base::partial;
1625 *to_nxt++ = static_cast<uint8_t>(0xE0 | (wc1 >> 12));
1626 *to_nxt++ = static_cast<uint8_t>(0x80 | ((wc1 & 0x0FC0) >> 6));
1627 *to_nxt++ = static_cast<uint8_t>(0x80 | (wc1 & 0x003F));
1628 }
1629 else if (wc1 < 0xDC00)
1630 {
1631 if (frm_end-frm_nxt < 2)
1632 return codecvt_base::partial;
1633 uint16_t wc2 = frm_nxt[1];
1634 if ((wc2 & 0xFC00) != 0xDC00)
1635 return codecvt_base::error;
1636 if (to_end-to_nxt < 4)
1637 return codecvt_base::partial;
1638 if ((((((unsigned long)wc1 & 0x03C0) >> 6) + 1) << 16) +
1639 (((unsigned long)wc1 & 0x003F) << 10) + (wc2 & 0x03FF) > Maxcode)
1640 return codecvt_base::error;
1641 ++frm_nxt;
1642 uint8_t z = ((wc1 & 0x03C0) >> 6) + 1;
1643 *to_nxt++ = static_cast<uint8_t>(0xF0 | (z >> 2));
1644 *to_nxt++ = static_cast<uint8_t>(0x80 | ((z & 0x03) << 4) | ((wc1 & 0x003C) >> 2));
1645 *to_nxt++ = static_cast<uint8_t>(0x80 | ((wc1 & 0x0003) << 4) | ((wc2 & 0x03C0) >> 6));
1646 *to_nxt++ = static_cast<uint8_t>(0x80 | (wc2 & 0x003F));
1647 }
1648 else if (wc1 < 0xE000)
1649 {
1650 return codecvt_base::error;
1651 }
1652 else
1653 {
1654 if (to_end-to_nxt < 3)
1655 return codecvt_base::partial;
1656 *to_nxt++ = static_cast<uint8_t>(0xE0 | (wc1 >> 12));
1657 *to_nxt++ = static_cast<uint8_t>(0x80 | ((wc1 & 0x0FC0) >> 6));
1658 *to_nxt++ = static_cast<uint8_t>(0x80 | (wc1 & 0x003F));
1659 }
1660 }
1661 return codecvt_base::ok;
1662}
1663
1664static
1665codecvt_base::result
1666utf16_to_utf8(const uint32_t* frm, const uint32_t* frm_end, const uint32_t*& frm_nxt,
1667 uint8_t* to, uint8_t* to_end, uint8_t*& to_nxt,
1668 unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0))
1669{
1670 frm_nxt = frm;
1671 to_nxt = to;
1672 if (mode & generate_header)
1673 {
1674 if (to_end-to_nxt < 3)
1675 return codecvt_base::partial;
1676 *to_nxt++ = static_cast<uint8_t>(0xEF);
1677 *to_nxt++ = static_cast<uint8_t>(0xBB);
1678 *to_nxt++ = static_cast<uint8_t>(0xBF);
1679 }
1680 for (; frm_nxt < frm_end; ++frm_nxt)
1681 {
1682 uint16_t wc1 = static_cast<uint16_t>(*frm_nxt);
1683 if (wc1 > Maxcode)
1684 return codecvt_base::error;
1685 if (wc1 < 0x0080)
1686 {
1687 if (to_end-to_nxt < 1)
1688 return codecvt_base::partial;
1689 *to_nxt++ = static_cast<uint8_t>(wc1);
1690 }
1691 else if (wc1 < 0x0800)
1692 {
1693 if (to_end-to_nxt < 2)
1694 return codecvt_base::partial;
1695 *to_nxt++ = static_cast<uint8_t>(0xC0 | (wc1 >> 6));
1696 *to_nxt++ = static_cast<uint8_t>(0x80 | (wc1 & 0x03F));
1697 }
1698 else if (wc1 < 0xD800)
1699 {
1700 if (to_end-to_nxt < 3)
1701 return codecvt_base::partial;
1702 *to_nxt++ = static_cast<uint8_t>(0xE0 | (wc1 >> 12));
1703 *to_nxt++ = static_cast<uint8_t>(0x80 | ((wc1 & 0x0FC0) >> 6));
1704 *to_nxt++ = static_cast<uint8_t>(0x80 | (wc1 & 0x003F));
1705 }
1706 else if (wc1 < 0xDC00)
1707 {
1708 if (frm_end-frm_nxt < 2)
1709 return codecvt_base::partial;
1710 uint16_t wc2 = static_cast<uint16_t>(frm_nxt[1]);
1711 if ((wc2 & 0xFC00) != 0xDC00)
1712 return codecvt_base::error;
1713 if (to_end-to_nxt < 4)
1714 return codecvt_base::partial;
1715 if ((((((unsigned long)wc1 & 0x03C0) >> 6) + 1) << 16) +
1716 (((unsigned long)wc1 & 0x003F) << 10) + (wc2 & 0x03FF) > Maxcode)
1717 return codecvt_base::error;
1718 ++frm_nxt;
1719 uint8_t z = ((wc1 & 0x03C0) >> 6) + 1;
1720 *to_nxt++ = static_cast<uint8_t>(0xF0 | (z >> 2));
1721 *to_nxt++ = static_cast<uint8_t>(0x80 | ((z & 0x03) << 4) | ((wc1 & 0x003C) >> 2));
1722 *to_nxt++ = static_cast<uint8_t>(0x80 | ((wc1 & 0x0003) << 4) | ((wc2 & 0x03C0) >> 6));
1723 *to_nxt++ = static_cast<uint8_t>(0x80 | (wc2 & 0x003F));
1724 }
1725 else if (wc1 < 0xE000)
1726 {
1727 return codecvt_base::error;
1728 }
1729 else
1730 {
1731 if (to_end-to_nxt < 3)
1732 return codecvt_base::partial;
1733 *to_nxt++ = static_cast<uint8_t>(0xE0 | (wc1 >> 12));
1734 *to_nxt++ = static_cast<uint8_t>(0x80 | ((wc1 & 0x0FC0) >> 6));
1735 *to_nxt++ = static_cast<uint8_t>(0x80 | (wc1 & 0x003F));
1736 }
1737 }
1738 return codecvt_base::ok;
1739}
1740
1741static
1742codecvt_base::result
1743utf8_to_utf16(const uint8_t* frm, const uint8_t* frm_end, const uint8_t*& frm_nxt,
1744 uint16_t* to, uint16_t* to_end, uint16_t*& to_nxt,
1745 unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0))
1746{
1747 frm_nxt = frm;
1748 to_nxt = to;
1749 if (mode & consume_header)
1750 {
1751 if (frm_end-frm_nxt >= 3 && frm_nxt[0] == 0xEF && frm_nxt[1] == 0xBB &&
1752 frm_nxt[2] == 0xBF)
1753 frm_nxt += 3;
1754 }
1755 for (; frm_nxt < frm_end && to_nxt < to_end; ++to_nxt)
1756 {
1757 uint8_t c1 = *frm_nxt;
1758 if (c1 > Maxcode)
1759 return codecvt_base::error;
1760 if (c1 < 0x80)
1761 {
1762 *to_nxt = static_cast<uint16_t>(c1);
1763 ++frm_nxt;
1764 }
1765 else if (c1 < 0xC2)
1766 {
1767 return codecvt_base::error;
1768 }
1769 else if (c1 < 0xE0)
1770 {
1771 if (frm_end-frm_nxt < 2)
1772 return codecvt_base::partial;
1773 uint8_t c2 = frm_nxt[1];
1774 if ((c2 & 0xC0) != 0x80)
1775 return codecvt_base::error;
1776 uint16_t t = static_cast<uint16_t>(((c1 & 0x1F) << 6) | (c2 & 0x3F));
1777 if (t > Maxcode)
1778 return codecvt_base::error;
1779 *to_nxt = t;
1780 frm_nxt += 2;
1781 }
1782 else if (c1 < 0xF0)
1783 {
1784 if (frm_end-frm_nxt < 3)
1785 return codecvt_base::partial;
1786 uint8_t c2 = frm_nxt[1];
1787 uint8_t c3 = frm_nxt[2];
1788 switch (c1)
1789 {
1790 case 0xE0:
1791 if ((c2 & 0xE0) != 0xA0)
1792 return codecvt_base::error;
1793 break;
1794 case 0xED:
1795 if ((c2 & 0xE0) != 0x80)
1796 return codecvt_base::error;
1797 break;
1798 default:
1799 if ((c2 & 0xC0) != 0x80)
1800 return codecvt_base::error;
1801 break;
1802 }
1803 if ((c3 & 0xC0) != 0x80)
1804 return codecvt_base::error;
1805 uint16_t t = static_cast<uint16_t>(((c1 & 0x0F) << 12)
1806 | ((c2 & 0x3F) << 6)
1807 | (c3 & 0x3F));
1808 if (t > Maxcode)
1809 return codecvt_base::error;
1810 *to_nxt = t;
1811 frm_nxt += 3;
1812 }
1813 else if (c1 < 0xF5)
1814 {
1815 if (frm_end-frm_nxt < 4)
1816 return codecvt_base::partial;
1817 uint8_t c2 = frm_nxt[1];
1818 uint8_t c3 = frm_nxt[2];
1819 uint8_t c4 = frm_nxt[3];
1820 switch (c1)
1821 {
1822 case 0xF0:
1823 if (!(0x90 <= c2 && c2 <= 0xBF))
1824 return codecvt_base::error;
1825 break;
1826 case 0xF4:
1827 if ((c2 & 0xF0) != 0x80)
1828 return codecvt_base::error;
1829 break;
1830 default:
1831 if ((c2 & 0xC0) != 0x80)
1832 return codecvt_base::error;
1833 break;
1834 }
1835 if ((c3 & 0xC0) != 0x80 || (c4 & 0xC0) != 0x80)
1836 return codecvt_base::error;
1837 if (to_end-to_nxt < 2)
1838 return codecvt_base::partial;
1839 if (((((unsigned long)c1 & 7) << 18) +
1840 (((unsigned long)c2 & 0x3F) << 12) +
1841 (((unsigned long)c3 & 0x3F) << 6) + (c4 & 0x3F)) > Maxcode)
1842 return codecvt_base::error;
1843 *to_nxt = static_cast<uint16_t>(
1844 0xD800
1845 | (((((c1 & 0x07) << 2) | ((c2 & 0x30) >> 4)) - 1) << 6)
1846 | ((c2 & 0x0F) << 2)
1847 | ((c3 & 0x30) >> 4));
1848 *++to_nxt = static_cast<uint16_t>(
1849 0xDC00
1850 | ((c3 & 0x0F) << 6)
1851 | (c4 & 0x3F));
1852 frm_nxt += 4;
1853 }
1854 else
1855 {
1856 return codecvt_base::error;
1857 }
1858 }
1859 return frm_nxt < frm_end ? codecvt_base::partial : codecvt_base::ok;
1860}
1861
1862static
1863codecvt_base::result
1864utf8_to_utf16(const uint8_t* frm, const uint8_t* frm_end, const uint8_t*& frm_nxt,
1865 uint32_t* to, uint32_t* to_end, uint32_t*& to_nxt,
1866 unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0))
1867{
1868 frm_nxt = frm;
1869 to_nxt = to;
1870 if (mode & consume_header)
1871 {
1872 if (frm_end-frm_nxt >= 3 && frm_nxt[0] == 0xEF && frm_nxt[1] == 0xBB &&
1873 frm_nxt[2] == 0xBF)
1874 frm_nxt += 3;
1875 }
1876 for (; frm_nxt < frm_end && to_nxt < to_end; ++to_nxt)
1877 {
1878 uint8_t c1 = *frm_nxt;
1879 if (c1 > Maxcode)
1880 return codecvt_base::error;
1881 if (c1 < 0x80)
1882 {
1883 *to_nxt = static_cast<uint32_t>(c1);
1884 ++frm_nxt;
1885 }
1886 else if (c1 < 0xC2)
1887 {
1888 return codecvt_base::error;
1889 }
1890 else if (c1 < 0xE0)
1891 {
1892 if (frm_end-frm_nxt < 2)
1893 return codecvt_base::partial;
1894 uint8_t c2 = frm_nxt[1];
1895 if ((c2 & 0xC0) != 0x80)
1896 return codecvt_base::error;
1897 uint16_t t = static_cast<uint16_t>(((c1 & 0x1F) << 6) | (c2 & 0x3F));
1898 if (t > Maxcode)
1899 return codecvt_base::error;
1900 *to_nxt = static_cast<uint32_t>(t);
1901 frm_nxt += 2;
1902 }
1903 else if (c1 < 0xF0)
1904 {
1905 if (frm_end-frm_nxt < 3)
1906 return codecvt_base::partial;
1907 uint8_t c2 = frm_nxt[1];
1908 uint8_t c3 = frm_nxt[2];
1909 switch (c1)
1910 {
1911 case 0xE0:
1912 if ((c2 & 0xE0) != 0xA0)
1913 return codecvt_base::error;
1914 break;
1915 case 0xED:
1916 if ((c2 & 0xE0) != 0x80)
1917 return codecvt_base::error;
1918 break;
1919 default:
1920 if ((c2 & 0xC0) != 0x80)
1921 return codecvt_base::error;
1922 break;
1923 }
1924 if ((c3 & 0xC0) != 0x80)
1925 return codecvt_base::error;
1926 uint16_t t = static_cast<uint16_t>(((c1 & 0x0F) << 12)
1927 | ((c2 & 0x3F) << 6)
1928 | (c3 & 0x3F));
1929 if (t > Maxcode)
1930 return codecvt_base::error;
1931 *to_nxt = static_cast<uint32_t>(t);
1932 frm_nxt += 3;
1933 }
1934 else if (c1 < 0xF5)
1935 {
1936 if (frm_end-frm_nxt < 4)
1937 return codecvt_base::partial;
1938 uint8_t c2 = frm_nxt[1];
1939 uint8_t c3 = frm_nxt[2];
1940 uint8_t c4 = frm_nxt[3];
1941 switch (c1)
1942 {
1943 case 0xF0:
1944 if (!(0x90 <= c2 && c2 <= 0xBF))
1945 return codecvt_base::error;
1946 break;
1947 case 0xF4:
1948 if ((c2 & 0xF0) != 0x80)
1949 return codecvt_base::error;
1950 break;
1951 default:
1952 if ((c2 & 0xC0) != 0x80)
1953 return codecvt_base::error;
1954 break;
1955 }
1956 if ((c3 & 0xC0) != 0x80 || (c4 & 0xC0) != 0x80)
1957 return codecvt_base::error;
1958 if (to_end-to_nxt < 2)
1959 return codecvt_base::partial;
1960 if (((((unsigned long)c1 & 7) << 18) +
1961 (((unsigned long)c2 & 0x3F) << 12) +
1962 (((unsigned long)c3 & 0x3F) << 6) + (c4 & 0x3F)) > Maxcode)
1963 return codecvt_base::error;
1964 *to_nxt = static_cast<uint32_t>(
1965 0xD800
1966 | (((((c1 & 0x07) << 2) | ((c2 & 0x30) >> 4)) - 1) << 6)
1967 | ((c2 & 0x0F) << 2)
1968 | ((c3 & 0x30) >> 4));
1969 *++to_nxt = static_cast<uint32_t>(
1970 0xDC00
1971 | ((c3 & 0x0F) << 6)
1972 | (c4 & 0x3F));
1973 frm_nxt += 4;
1974 }
1975 else
1976 {
1977 return codecvt_base::error;
1978 }
1979 }
1980 return frm_nxt < frm_end ? codecvt_base::partial : codecvt_base::ok;
1981}
1982
1983static
1984int
1985utf8_to_utf16_length(const uint8_t* frm, const uint8_t* frm_end,
1986 size_t mx, unsigned long Maxcode = 0x10FFFF,
1987 codecvt_mode mode = codecvt_mode(0))
1988{
1989 const uint8_t* frm_nxt = frm;
1990 if (mode & consume_header)
1991 {
1992 if (frm_end-frm_nxt >= 3 && frm_nxt[0] == 0xEF && frm_nxt[1] == 0xBB &&
1993 frm_nxt[2] == 0xBF)
1994 frm_nxt += 3;
1995 }
1996 for (size_t nchar16_t = 0; frm_nxt < frm_end && nchar16_t < mx; ++nchar16_t)
1997 {
1998 uint8_t c1 = *frm_nxt;
1999 if (c1 > Maxcode)
2000 break;
2001 if (c1 < 0x80)
2002 {
2003 ++frm_nxt;
2004 }
2005 else if (c1 < 0xC2)
2006 {
2007 break;
2008 }
2009 else if (c1 < 0xE0)
2010 {
2011 if ((frm_end-frm_nxt < 2) || (frm_nxt[1] & 0xC0) != 0x80)
2012 break;
2013 uint16_t t = static_cast<uint16_t>(((c1 & 0x1F) << 6) | (frm_nxt[1] & 0x3F));
2014 if (t > Maxcode)
2015 break;
2016 frm_nxt += 2;
2017 }
2018 else if (c1 < 0xF0)
2019 {
2020 if (frm_end-frm_nxt < 3)
2021 break;
2022 uint8_t c2 = frm_nxt[1];
2023 uint8_t c3 = frm_nxt[2];
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00002024 switch (c1)
2025 {
2026 case 0xE0:
2027 if ((c2 & 0xE0) != 0xA0)
2028 return static_cast<int>(frm_nxt - frm);
2029 break;
2030 case 0xED:
2031 if ((c2 & 0xE0) != 0x80)
2032 return static_cast<int>(frm_nxt - frm);
2033 break;
2034 default:
2035 if ((c2 & 0xC0) != 0x80)
2036 return static_cast<int>(frm_nxt - frm);
2037 break;
2038 }
2039 if ((c3 & 0xC0) != 0x80)
2040 break;
Howard Hinnantec3773c2011-12-01 20:21:04 +00002041 if ((((c1 & 0x0Fu) << 12) | ((c2 & 0x3Fu) << 6) | (c3 & 0x3Fu)) > Maxcode)
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00002042 break;
2043 frm_nxt += 3;
2044 }
2045 else if (c1 < 0xF5)
2046 {
2047 if (frm_end-frm_nxt < 4 || mx-nchar16_t < 2)
2048 break;
2049 uint8_t c2 = frm_nxt[1];
2050 uint8_t c3 = frm_nxt[2];
2051 uint8_t c4 = frm_nxt[3];
2052 switch (c1)
2053 {
2054 case 0xF0:
2055 if (!(0x90 <= c2 && c2 <= 0xBF))
2056 return static_cast<int>(frm_nxt - frm);
2057 break;
2058 case 0xF4:
2059 if ((c2 & 0xF0) != 0x80)
2060 return static_cast<int>(frm_nxt - frm);
2061 break;
2062 default:
2063 if ((c2 & 0xC0) != 0x80)
2064 return static_cast<int>(frm_nxt - frm);
2065 break;
2066 }
2067 if ((c3 & 0xC0) != 0x80 || (c4 & 0xC0) != 0x80)
2068 break;
2069 if (((((unsigned long)c1 & 7) << 18) +
2070 (((unsigned long)c2 & 0x3F) << 12) +
2071 (((unsigned long)c3 & 0x3F) << 6) + (c4 & 0x3F)) > Maxcode)
2072 break;
2073 ++nchar16_t;
2074 frm_nxt += 4;
2075 }
2076 else
2077 {
2078 break;
2079 }
2080 }
2081 return static_cast<int>(frm_nxt - frm);
2082}
2083
2084static
2085codecvt_base::result
2086ucs4_to_utf8(const uint32_t* frm, const uint32_t* frm_end, const uint32_t*& frm_nxt,
2087 uint8_t* to, uint8_t* to_end, uint8_t*& to_nxt,
2088 unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0))
2089{
2090 frm_nxt = frm;
2091 to_nxt = to;
2092 if (mode & generate_header)
2093 {
2094 if (to_end-to_nxt < 3)
2095 return codecvt_base::partial;
2096 *to_nxt++ = static_cast<uint8_t>(0xEF);
2097 *to_nxt++ = static_cast<uint8_t>(0xBB);
2098 *to_nxt++ = static_cast<uint8_t>(0xBF);
2099 }
2100 for (; frm_nxt < frm_end; ++frm_nxt)
2101 {
2102 uint32_t wc = *frm_nxt;
2103 if ((wc & 0xFFFFF800) == 0x00D800 || wc > Maxcode)
2104 return codecvt_base::error;
2105 if (wc < 0x000080)
2106 {
2107 if (to_end-to_nxt < 1)
2108 return codecvt_base::partial;
2109 *to_nxt++ = static_cast<uint8_t>(wc);
2110 }
2111 else if (wc < 0x000800)
2112 {
2113 if (to_end-to_nxt < 2)
2114 return codecvt_base::partial;
2115 *to_nxt++ = static_cast<uint8_t>(0xC0 | (wc >> 6));
2116 *to_nxt++ = static_cast<uint8_t>(0x80 | (wc & 0x03F));
2117 }
2118 else if (wc < 0x010000)
2119 {
2120 if (to_end-to_nxt < 3)
2121 return codecvt_base::partial;
2122 *to_nxt++ = static_cast<uint8_t>(0xE0 | (wc >> 12));
2123 *to_nxt++ = static_cast<uint8_t>(0x80 | ((wc & 0x0FC0) >> 6));
2124 *to_nxt++ = static_cast<uint8_t>(0x80 | (wc & 0x003F));
2125 }
2126 else // if (wc < 0x110000)
2127 {
2128 if (to_end-to_nxt < 4)
2129 return codecvt_base::partial;
2130 *to_nxt++ = static_cast<uint8_t>(0xF0 | (wc >> 18));
2131 *to_nxt++ = static_cast<uint8_t>(0x80 | ((wc & 0x03F000) >> 12));
2132 *to_nxt++ = static_cast<uint8_t>(0x80 | ((wc & 0x000FC0) >> 6));
2133 *to_nxt++ = static_cast<uint8_t>(0x80 | (wc & 0x00003F));
2134 }
2135 }
2136 return codecvt_base::ok;
2137}
2138
2139static
2140codecvt_base::result
2141utf8_to_ucs4(const uint8_t* frm, const uint8_t* frm_end, const uint8_t*& frm_nxt,
2142 uint32_t* to, uint32_t* to_end, uint32_t*& to_nxt,
2143 unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0))
2144{
2145 frm_nxt = frm;
2146 to_nxt = to;
2147 if (mode & consume_header)
2148 {
2149 if (frm_end-frm_nxt >= 3 && frm_nxt[0] == 0xEF && frm_nxt[1] == 0xBB &&
2150 frm_nxt[2] == 0xBF)
2151 frm_nxt += 3;
2152 }
2153 for (; frm_nxt < frm_end && to_nxt < to_end; ++to_nxt)
2154 {
2155 uint8_t c1 = static_cast<uint8_t>(*frm_nxt);
2156 if (c1 < 0x80)
2157 {
2158 if (c1 > Maxcode)
2159 return codecvt_base::error;
2160 *to_nxt = static_cast<uint32_t>(c1);
2161 ++frm_nxt;
2162 }
2163 else if (c1 < 0xC2)
2164 {
2165 return codecvt_base::error;
2166 }
2167 else if (c1 < 0xE0)
2168 {
2169 if (frm_end-frm_nxt < 2)
2170 return codecvt_base::partial;
2171 uint8_t c2 = frm_nxt[1];
2172 if ((c2 & 0xC0) != 0x80)
2173 return codecvt_base::error;
2174 uint32_t t = static_cast<uint32_t>(((c1 & 0x1F) << 6)
2175 | (c2 & 0x3F));
2176 if (t > Maxcode)
2177 return codecvt_base::error;
2178 *to_nxt = t;
2179 frm_nxt += 2;
2180 }
2181 else if (c1 < 0xF0)
2182 {
2183 if (frm_end-frm_nxt < 3)
2184 return codecvt_base::partial;
2185 uint8_t c2 = frm_nxt[1];
2186 uint8_t c3 = frm_nxt[2];
2187 switch (c1)
2188 {
2189 case 0xE0:
2190 if ((c2 & 0xE0) != 0xA0)
2191 return codecvt_base::error;
2192 break;
2193 case 0xED:
2194 if ((c2 & 0xE0) != 0x80)
2195 return codecvt_base::error;
2196 break;
2197 default:
2198 if ((c2 & 0xC0) != 0x80)
2199 return codecvt_base::error;
2200 break;
2201 }
2202 if ((c3 & 0xC0) != 0x80)
2203 return codecvt_base::error;
2204 uint32_t t = static_cast<uint32_t>(((c1 & 0x0F) << 12)
2205 | ((c2 & 0x3F) << 6)
2206 | (c3 & 0x3F));
2207 if (t > Maxcode)
2208 return codecvt_base::error;
2209 *to_nxt = t;
2210 frm_nxt += 3;
2211 }
2212 else if (c1 < 0xF5)
2213 {
2214 if (frm_end-frm_nxt < 4)
2215 return codecvt_base::partial;
2216 uint8_t c2 = frm_nxt[1];
2217 uint8_t c3 = frm_nxt[2];
2218 uint8_t c4 = frm_nxt[3];
2219 switch (c1)
2220 {
2221 case 0xF0:
2222 if (!(0x90 <= c2 && c2 <= 0xBF))
2223 return codecvt_base::error;
2224 break;
2225 case 0xF4:
2226 if ((c2 & 0xF0) != 0x80)
2227 return codecvt_base::error;
2228 break;
2229 default:
2230 if ((c2 & 0xC0) != 0x80)
2231 return codecvt_base::error;
2232 break;
2233 }
2234 if ((c3 & 0xC0) != 0x80 || (c4 & 0xC0) != 0x80)
2235 return codecvt_base::error;
2236 uint32_t t = static_cast<uint32_t>(((c1 & 0x07) << 18)
2237 | ((c2 & 0x3F) << 12)
2238 | ((c3 & 0x3F) << 6)
2239 | (c4 & 0x3F));
2240 if (t > Maxcode)
2241 return codecvt_base::error;
2242 *to_nxt = t;
2243 frm_nxt += 4;
2244 }
2245 else
2246 {
2247 return codecvt_base::error;
2248 }
2249 }
2250 return frm_nxt < frm_end ? codecvt_base::partial : codecvt_base::ok;
2251}
2252
2253static
2254int
2255utf8_to_ucs4_length(const uint8_t* frm, const uint8_t* frm_end,
2256 size_t mx, unsigned long Maxcode = 0x10FFFF,
2257 codecvt_mode mode = codecvt_mode(0))
2258{
2259 const uint8_t* frm_nxt = frm;
2260 if (mode & consume_header)
2261 {
2262 if (frm_end-frm_nxt >= 3 && frm_nxt[0] == 0xEF && frm_nxt[1] == 0xBB &&
2263 frm_nxt[2] == 0xBF)
2264 frm_nxt += 3;
2265 }
2266 for (size_t nchar32_t = 0; frm_nxt < frm_end && nchar32_t < mx; ++nchar32_t)
2267 {
2268 uint8_t c1 = static_cast<uint8_t>(*frm_nxt);
2269 if (c1 < 0x80)
2270 {
2271 if (c1 > Maxcode)
2272 break;
2273 ++frm_nxt;
2274 }
2275 else if (c1 < 0xC2)
2276 {
2277 break;
2278 }
2279 else if (c1 < 0xE0)
2280 {
2281 if ((frm_end-frm_nxt < 2) || ((frm_nxt[1] & 0xC0) != 0x80))
2282 break;
Howard Hinnantec3773c2011-12-01 20:21:04 +00002283 if ((((c1 & 0x1Fu) << 6) | (frm_nxt[1] & 0x3Fu)) > Maxcode)
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00002284 break;
2285 frm_nxt += 2;
2286 }
2287 else if (c1 < 0xF0)
2288 {
2289 if (frm_end-frm_nxt < 3)
2290 break;
2291 uint8_t c2 = frm_nxt[1];
2292 uint8_t c3 = frm_nxt[2];
2293 switch (c1)
2294 {
2295 case 0xE0:
2296 if ((c2 & 0xE0) != 0xA0)
2297 return static_cast<int>(frm_nxt - frm);
2298 break;
2299 case 0xED:
2300 if ((c2 & 0xE0) != 0x80)
2301 return static_cast<int>(frm_nxt - frm);
2302 break;
2303 default:
2304 if ((c2 & 0xC0) != 0x80)
2305 return static_cast<int>(frm_nxt - frm);
2306 break;
2307 }
2308 if ((c3 & 0xC0) != 0x80)
2309 break;
Howard Hinnantec3773c2011-12-01 20:21:04 +00002310 if ((((c1 & 0x0Fu) << 12) | ((c2 & 0x3Fu) << 6) | (c3 & 0x3Fu)) > Maxcode)
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00002311 break;
2312 frm_nxt += 3;
2313 }
2314 else if (c1 < 0xF5)
2315 {
2316 if (frm_end-frm_nxt < 4)
2317 break;
2318 uint8_t c2 = frm_nxt[1];
2319 uint8_t c3 = frm_nxt[2];
2320 uint8_t c4 = frm_nxt[3];
2321 switch (c1)
2322 {
2323 case 0xF0:
2324 if (!(0x90 <= c2 && c2 <= 0xBF))
2325 return static_cast<int>(frm_nxt - frm);
2326 break;
2327 case 0xF4:
2328 if ((c2 & 0xF0) != 0x80)
2329 return static_cast<int>(frm_nxt - frm);
2330 break;
2331 default:
2332 if ((c2 & 0xC0) != 0x80)
2333 return static_cast<int>(frm_nxt - frm);
2334 break;
2335 }
2336 if ((c3 & 0xC0) != 0x80 || (c4 & 0xC0) != 0x80)
2337 break;
Howard Hinnantec3773c2011-12-01 20:21:04 +00002338 if ((((c1 & 0x07u) << 18) | ((c2 & 0x3Fu) << 12) |
2339 ((c3 & 0x3Fu) << 6) | (c4 & 0x3Fu)) > Maxcode)
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00002340 break;
2341 frm_nxt += 4;
2342 }
2343 else
2344 {
2345 break;
2346 }
2347 }
2348 return static_cast<int>(frm_nxt - frm);
2349}
2350
2351static
2352codecvt_base::result
2353ucs2_to_utf8(const uint16_t* frm, const uint16_t* frm_end, const uint16_t*& frm_nxt,
2354 uint8_t* to, uint8_t* to_end, uint8_t*& to_nxt,
2355 unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0))
2356{
2357 frm_nxt = frm;
2358 to_nxt = to;
2359 if (mode & generate_header)
2360 {
2361 if (to_end-to_nxt < 3)
2362 return codecvt_base::partial;
2363 *to_nxt++ = static_cast<uint8_t>(0xEF);
2364 *to_nxt++ = static_cast<uint8_t>(0xBB);
2365 *to_nxt++ = static_cast<uint8_t>(0xBF);
2366 }
2367 for (; frm_nxt < frm_end; ++frm_nxt)
2368 {
2369 uint16_t wc = *frm_nxt;
2370 if ((wc & 0xF800) == 0xD800 || wc > Maxcode)
2371 return codecvt_base::error;
2372 if (wc < 0x0080)
2373 {
2374 if (to_end-to_nxt < 1)
2375 return codecvt_base::partial;
2376 *to_nxt++ = static_cast<uint8_t>(wc);
2377 }
2378 else if (wc < 0x0800)
2379 {
2380 if (to_end-to_nxt < 2)
2381 return codecvt_base::partial;
2382 *to_nxt++ = static_cast<uint8_t>(0xC0 | (wc >> 6));
2383 *to_nxt++ = static_cast<uint8_t>(0x80 | (wc & 0x03F));
2384 }
2385 else // if (wc <= 0xFFFF)
2386 {
2387 if (to_end-to_nxt < 3)
2388 return codecvt_base::partial;
2389 *to_nxt++ = static_cast<uint8_t>(0xE0 | (wc >> 12));
2390 *to_nxt++ = static_cast<uint8_t>(0x80 | ((wc & 0x0FC0) >> 6));
2391 *to_nxt++ = static_cast<uint8_t>(0x80 | (wc & 0x003F));
2392 }
2393 }
2394 return codecvt_base::ok;
2395}
2396
2397static
2398codecvt_base::result
2399utf8_to_ucs2(const uint8_t* frm, const uint8_t* frm_end, const uint8_t*& frm_nxt,
2400 uint16_t* to, uint16_t* to_end, uint16_t*& to_nxt,
2401 unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0))
2402{
2403 frm_nxt = frm;
2404 to_nxt = to;
2405 if (mode & consume_header)
2406 {
2407 if (frm_end-frm_nxt >= 3 && frm_nxt[0] == 0xEF && frm_nxt[1] == 0xBB &&
2408 frm_nxt[2] == 0xBF)
2409 frm_nxt += 3;
2410 }
2411 for (; frm_nxt < frm_end && to_nxt < to_end; ++to_nxt)
2412 {
2413 uint8_t c1 = static_cast<uint8_t>(*frm_nxt);
2414 if (c1 < 0x80)
2415 {
2416 if (c1 > Maxcode)
2417 return codecvt_base::error;
2418 *to_nxt = static_cast<uint16_t>(c1);
2419 ++frm_nxt;
2420 }
2421 else if (c1 < 0xC2)
2422 {
2423 return codecvt_base::error;
2424 }
2425 else if (c1 < 0xE0)
2426 {
2427 if (frm_end-frm_nxt < 2)
2428 return codecvt_base::partial;
2429 uint8_t c2 = frm_nxt[1];
2430 if ((c2 & 0xC0) != 0x80)
2431 return codecvt_base::error;
2432 uint16_t t = static_cast<uint16_t>(((c1 & 0x1F) << 6)
2433 | (c2 & 0x3F));
2434 if (t > Maxcode)
2435 return codecvt_base::error;
2436 *to_nxt = t;
2437 frm_nxt += 2;
2438 }
2439 else if (c1 < 0xF0)
2440 {
2441 if (frm_end-frm_nxt < 3)
2442 return codecvt_base::partial;
2443 uint8_t c2 = frm_nxt[1];
2444 uint8_t c3 = frm_nxt[2];
2445 switch (c1)
2446 {
2447 case 0xE0:
2448 if ((c2 & 0xE0) != 0xA0)
2449 return codecvt_base::error;
2450 break;
2451 case 0xED:
2452 if ((c2 & 0xE0) != 0x80)
2453 return codecvt_base::error;
2454 break;
2455 default:
2456 if ((c2 & 0xC0) != 0x80)
2457 return codecvt_base::error;
2458 break;
2459 }
2460 if ((c3 & 0xC0) != 0x80)
2461 return codecvt_base::error;
2462 uint16_t t = static_cast<uint16_t>(((c1 & 0x0F) << 12)
2463 | ((c2 & 0x3F) << 6)
2464 | (c3 & 0x3F));
2465 if (t > Maxcode)
2466 return codecvt_base::error;
2467 *to_nxt = t;
2468 frm_nxt += 3;
2469 }
2470 else
2471 {
2472 return codecvt_base::error;
2473 }
2474 }
2475 return frm_nxt < frm_end ? codecvt_base::partial : codecvt_base::ok;
2476}
2477
2478static
2479int
2480utf8_to_ucs2_length(const uint8_t* frm, const uint8_t* frm_end,
2481 size_t mx, unsigned long Maxcode = 0x10FFFF,
2482 codecvt_mode mode = codecvt_mode(0))
2483{
2484 const uint8_t* frm_nxt = frm;
2485 if (mode & consume_header)
2486 {
2487 if (frm_end-frm_nxt >= 3 && frm_nxt[0] == 0xEF && frm_nxt[1] == 0xBB &&
2488 frm_nxt[2] == 0xBF)
2489 frm_nxt += 3;
2490 }
2491 for (size_t nchar32_t = 0; frm_nxt < frm_end && nchar32_t < mx; ++nchar32_t)
2492 {
2493 uint8_t c1 = static_cast<uint8_t>(*frm_nxt);
2494 if (c1 < 0x80)
2495 {
2496 if (c1 > Maxcode)
2497 break;
2498 ++frm_nxt;
2499 }
2500 else if (c1 < 0xC2)
2501 {
2502 break;
2503 }
2504 else if (c1 < 0xE0)
2505 {
2506 if ((frm_end-frm_nxt < 2) || ((frm_nxt[1] & 0xC0) != 0x80))
2507 break;
Howard Hinnantec3773c2011-12-01 20:21:04 +00002508 if ((((c1 & 0x1Fu) << 6) | (frm_nxt[1] & 0x3Fu)) > Maxcode)
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00002509 break;
2510 frm_nxt += 2;
2511 }
2512 else if (c1 < 0xF0)
2513 {
2514 if (frm_end-frm_nxt < 3)
2515 break;
2516 uint8_t c2 = frm_nxt[1];
2517 uint8_t c3 = frm_nxt[2];
2518 switch (c1)
2519 {
2520 case 0xE0:
2521 if ((c2 & 0xE0) != 0xA0)
2522 return static_cast<int>(frm_nxt - frm);
2523 break;
2524 case 0xED:
2525 if ((c2 & 0xE0) != 0x80)
2526 return static_cast<int>(frm_nxt - frm);
2527 break;
2528 default:
2529 if ((c2 & 0xC0) != 0x80)
2530 return static_cast<int>(frm_nxt - frm);
2531 break;
2532 }
2533 if ((c3 & 0xC0) != 0x80)
2534 break;
Howard Hinnantec3773c2011-12-01 20:21:04 +00002535 if ((((c1 & 0x0Fu) << 12) | ((c2 & 0x3Fu) << 6) | (c3 & 0x3Fu)) > Maxcode)
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00002536 break;
2537 frm_nxt += 3;
2538 }
2539 else
2540 {
2541 break;
2542 }
2543 }
2544 return static_cast<int>(frm_nxt - frm);
2545}
2546
2547static
2548codecvt_base::result
2549ucs4_to_utf16be(const uint32_t* frm, const uint32_t* frm_end, const uint32_t*& frm_nxt,
2550 uint8_t* to, uint8_t* to_end, uint8_t*& to_nxt,
2551 unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0))
2552{
2553 frm_nxt = frm;
2554 to_nxt = to;
2555 if (mode & generate_header)
2556 {
2557 if (to_end-to_nxt < 2)
2558 return codecvt_base::partial;
2559 *to_nxt++ = static_cast<uint8_t>(0xFE);
2560 *to_nxt++ = static_cast<uint8_t>(0xFF);
2561 }
2562 for (; frm_nxt < frm_end; ++frm_nxt)
2563 {
2564 uint32_t wc = *frm_nxt;
2565 if ((wc & 0xFFFFF800) == 0x00D800 || wc > Maxcode)
2566 return codecvt_base::error;
2567 if (wc < 0x010000)
2568 {
2569 if (to_end-to_nxt < 2)
2570 return codecvt_base::partial;
2571 *to_nxt++ = static_cast<uint8_t>(wc >> 8);
2572 *to_nxt++ = static_cast<uint8_t>(wc);
2573 }
2574 else
2575 {
2576 if (to_end-to_nxt < 4)
2577 return codecvt_base::partial;
2578 uint16_t t = static_cast<uint16_t>(
2579 0xD800
2580 | ((((wc & 0x1F0000) >> 16) - 1) << 6)
2581 | ((wc & 0x00FC00) >> 10));
2582 *to_nxt++ = static_cast<uint8_t>(t >> 8);
2583 *to_nxt++ = static_cast<uint8_t>(t);
2584 t = static_cast<uint16_t>(0xDC00 | (wc & 0x03FF));
2585 *to_nxt++ = static_cast<uint8_t>(t >> 8);
2586 *to_nxt++ = static_cast<uint8_t>(t);
2587 }
2588 }
2589 return codecvt_base::ok;
2590}
2591
2592static
2593codecvt_base::result
2594utf16be_to_ucs4(const uint8_t* frm, const uint8_t* frm_end, const uint8_t*& frm_nxt,
2595 uint32_t* to, uint32_t* to_end, uint32_t*& to_nxt,
2596 unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0))
2597{
2598 frm_nxt = frm;
2599 to_nxt = to;
2600 if (mode & consume_header)
2601 {
2602 if (frm_end-frm_nxt >= 2 && frm_nxt[0] == 0xFE && frm_nxt[1] == 0xFF)
2603 frm_nxt += 2;
2604 }
2605 for (; frm_nxt < frm_end - 1 && to_nxt < to_end; ++to_nxt)
2606 {
Howard Hinnantec3773c2011-12-01 20:21:04 +00002607 uint16_t c1 = static_cast<uint16_t>(frm_nxt[0] << 8 | frm_nxt[1]);
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00002608 if ((c1 & 0xFC00) == 0xDC00)
2609 return codecvt_base::error;
2610 if ((c1 & 0xFC00) != 0xD800)
2611 {
2612 if (c1 > Maxcode)
2613 return codecvt_base::error;
2614 *to_nxt = static_cast<uint32_t>(c1);
2615 frm_nxt += 2;
2616 }
2617 else
2618 {
2619 if (frm_end-frm_nxt < 4)
2620 return codecvt_base::partial;
Howard Hinnantec3773c2011-12-01 20:21:04 +00002621 uint16_t c2 = static_cast<uint16_t>(frm_nxt[2] << 8 | frm_nxt[3]);
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00002622 if ((c2 & 0xFC00) != 0xDC00)
2623 return codecvt_base::error;
2624 uint32_t t = static_cast<uint32_t>(
2625 ((((c1 & 0x03C0) >> 6) + 1) << 16)
2626 | ((c1 & 0x003F) << 10)
2627 | (c2 & 0x03FF));
2628 if (t > Maxcode)
2629 return codecvt_base::error;
2630 *to_nxt = t;
2631 frm_nxt += 4;
2632 }
2633 }
2634 return frm_nxt < frm_end ? codecvt_base::partial : codecvt_base::ok;
2635}
2636
2637static
2638int
2639utf16be_to_ucs4_length(const uint8_t* frm, const uint8_t* frm_end,
2640 size_t mx, unsigned long Maxcode = 0x10FFFF,
2641 codecvt_mode mode = codecvt_mode(0))
2642{
2643 const uint8_t* frm_nxt = frm;
2644 frm_nxt = frm;
2645 if (mode & consume_header)
2646 {
2647 if (frm_end-frm_nxt >= 2 && frm_nxt[0] == 0xFE && frm_nxt[1] == 0xFF)
2648 frm_nxt += 2;
2649 }
2650 for (size_t nchar32_t = 0; frm_nxt < frm_end - 1 && nchar32_t < mx; ++nchar32_t)
2651 {
Howard Hinnantec3773c2011-12-01 20:21:04 +00002652 uint16_t c1 = static_cast<uint16_t>(frm_nxt[0] << 8 | frm_nxt[1]);
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00002653 if ((c1 & 0xFC00) == 0xDC00)
2654 break;
2655 if ((c1 & 0xFC00) != 0xD800)
2656 {
2657 if (c1 > Maxcode)
2658 break;
2659 frm_nxt += 2;
2660 }
2661 else
2662 {
2663 if (frm_end-frm_nxt < 4)
2664 break;
Howard Hinnantec3773c2011-12-01 20:21:04 +00002665 uint16_t c2 = static_cast<uint16_t>(frm_nxt[2] << 8 | frm_nxt[3]);
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00002666 if ((c2 & 0xFC00) != 0xDC00)
2667 break;
2668 uint32_t t = static_cast<uint32_t>(
2669 ((((c1 & 0x03C0) >> 6) + 1) << 16)
2670 | ((c1 & 0x003F) << 10)
2671 | (c2 & 0x03FF));
2672 if (t > Maxcode)
2673 break;
2674 frm_nxt += 4;
2675 }
2676 }
2677 return static_cast<int>(frm_nxt - frm);
2678}
2679
2680static
2681codecvt_base::result
2682ucs4_to_utf16le(const uint32_t* frm, const uint32_t* frm_end, const uint32_t*& frm_nxt,
2683 uint8_t* to, uint8_t* to_end, uint8_t*& to_nxt,
2684 unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0))
2685{
2686 frm_nxt = frm;
2687 to_nxt = to;
2688 if (mode & generate_header)
2689 {
2690 if (to_end-to_nxt < 2)
2691 return codecvt_base::partial;
2692 *to_nxt++ = static_cast<uint8_t>(0xFF);
2693 *to_nxt++ = static_cast<uint8_t>(0xFE);
2694 }
2695 for (; frm_nxt < frm_end; ++frm_nxt)
2696 {
2697 uint32_t wc = *frm_nxt;
2698 if ((wc & 0xFFFFF800) == 0x00D800 || wc > Maxcode)
2699 return codecvt_base::error;
2700 if (wc < 0x010000)
2701 {
2702 if (to_end-to_nxt < 2)
2703 return codecvt_base::partial;
2704 *to_nxt++ = static_cast<uint8_t>(wc);
2705 *to_nxt++ = static_cast<uint8_t>(wc >> 8);
2706 }
2707 else
2708 {
2709 if (to_end-to_nxt < 4)
2710 return codecvt_base::partial;
2711 uint16_t t = static_cast<uint16_t>(
2712 0xD800
2713 | ((((wc & 0x1F0000) >> 16) - 1) << 6)
2714 | ((wc & 0x00FC00) >> 10));
2715 *to_nxt++ = static_cast<uint8_t>(t);
2716 *to_nxt++ = static_cast<uint8_t>(t >> 8);
2717 t = static_cast<uint16_t>(0xDC00 | (wc & 0x03FF));
2718 *to_nxt++ = static_cast<uint8_t>(t);
2719 *to_nxt++ = static_cast<uint8_t>(t >> 8);
2720 }
2721 }
2722 return codecvt_base::ok;
2723}
2724
2725static
2726codecvt_base::result
2727utf16le_to_ucs4(const uint8_t* frm, const uint8_t* frm_end, const uint8_t*& frm_nxt,
2728 uint32_t* to, uint32_t* to_end, uint32_t*& to_nxt,
2729 unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0))
2730{
2731 frm_nxt = frm;
2732 to_nxt = to;
2733 if (mode & consume_header)
2734 {
2735 if (frm_end-frm_nxt >= 2 && frm_nxt[0] == 0xFF && frm_nxt[1] == 0xFE)
2736 frm_nxt += 2;
2737 }
2738 for (; frm_nxt < frm_end - 1 && to_nxt < to_end; ++to_nxt)
2739 {
Howard Hinnantec3773c2011-12-01 20:21:04 +00002740 uint16_t c1 = static_cast<uint16_t>(frm_nxt[1] << 8 | frm_nxt[0]);
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00002741 if ((c1 & 0xFC00) == 0xDC00)
2742 return codecvt_base::error;
2743 if ((c1 & 0xFC00) != 0xD800)
2744 {
2745 if (c1 > Maxcode)
2746 return codecvt_base::error;
2747 *to_nxt = static_cast<uint32_t>(c1);
2748 frm_nxt += 2;
2749 }
2750 else
2751 {
2752 if (frm_end-frm_nxt < 4)
2753 return codecvt_base::partial;
Howard Hinnantec3773c2011-12-01 20:21:04 +00002754 uint16_t c2 = static_cast<uint16_t>(frm_nxt[3] << 8 | frm_nxt[2]);
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00002755 if ((c2 & 0xFC00) != 0xDC00)
2756 return codecvt_base::error;
2757 uint32_t t = static_cast<uint32_t>(
2758 ((((c1 & 0x03C0) >> 6) + 1) << 16)
2759 | ((c1 & 0x003F) << 10)
2760 | (c2 & 0x03FF));
2761 if (t > Maxcode)
2762 return codecvt_base::error;
2763 *to_nxt = t;
2764 frm_nxt += 4;
2765 }
2766 }
2767 return frm_nxt < frm_end ? codecvt_base::partial : codecvt_base::ok;
2768}
2769
2770static
2771int
2772utf16le_to_ucs4_length(const uint8_t* frm, const uint8_t* frm_end,
2773 size_t mx, unsigned long Maxcode = 0x10FFFF,
2774 codecvt_mode mode = codecvt_mode(0))
2775{
2776 const uint8_t* frm_nxt = frm;
2777 frm_nxt = frm;
2778 if (mode & consume_header)
2779 {
2780 if (frm_end-frm_nxt >= 2 && frm_nxt[0] == 0xFF && frm_nxt[1] == 0xFE)
2781 frm_nxt += 2;
2782 }
2783 for (size_t nchar32_t = 0; frm_nxt < frm_end - 1 && nchar32_t < mx; ++nchar32_t)
2784 {
Howard Hinnantec3773c2011-12-01 20:21:04 +00002785 uint16_t c1 = static_cast<uint16_t>(frm_nxt[1] << 8 | frm_nxt[0]);
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00002786 if ((c1 & 0xFC00) == 0xDC00)
2787 break;
2788 if ((c1 & 0xFC00) != 0xD800)
2789 {
2790 if (c1 > Maxcode)
2791 break;
2792 frm_nxt += 2;
2793 }
2794 else
2795 {
2796 if (frm_end-frm_nxt < 4)
2797 break;
Howard Hinnantec3773c2011-12-01 20:21:04 +00002798 uint16_t c2 = static_cast<uint16_t>(frm_nxt[3] << 8 | frm_nxt[2]);
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00002799 if ((c2 & 0xFC00) != 0xDC00)
2800 break;
2801 uint32_t t = static_cast<uint32_t>(
2802 ((((c1 & 0x03C0) >> 6) + 1) << 16)
2803 | ((c1 & 0x003F) << 10)
2804 | (c2 & 0x03FF));
2805 if (t > Maxcode)
2806 break;
2807 frm_nxt += 4;
2808 }
2809 }
2810 return static_cast<int>(frm_nxt - frm);
2811}
2812
2813static
2814codecvt_base::result
2815ucs2_to_utf16be(const uint16_t* frm, const uint16_t* frm_end, const uint16_t*& frm_nxt,
2816 uint8_t* to, uint8_t* to_end, uint8_t*& to_nxt,
2817 unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0))
2818{
2819 frm_nxt = frm;
2820 to_nxt = to;
2821 if (mode & generate_header)
2822 {
2823 if (to_end-to_nxt < 2)
2824 return codecvt_base::partial;
2825 *to_nxt++ = static_cast<uint8_t>(0xFE);
2826 *to_nxt++ = static_cast<uint8_t>(0xFF);
2827 }
2828 for (; frm_nxt < frm_end; ++frm_nxt)
2829 {
2830 uint16_t wc = *frm_nxt;
2831 if ((wc & 0xF800) == 0xD800 || wc > Maxcode)
2832 return codecvt_base::error;
2833 if (to_end-to_nxt < 2)
2834 return codecvt_base::partial;
2835 *to_nxt++ = static_cast<uint8_t>(wc >> 8);
2836 *to_nxt++ = static_cast<uint8_t>(wc);
2837 }
2838 return codecvt_base::ok;
2839}
2840
2841static
2842codecvt_base::result
2843utf16be_to_ucs2(const uint8_t* frm, const uint8_t* frm_end, const uint8_t*& frm_nxt,
2844 uint16_t* to, uint16_t* to_end, uint16_t*& to_nxt,
2845 unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0))
2846{
2847 frm_nxt = frm;
2848 to_nxt = to;
2849 if (mode & consume_header)
2850 {
2851 if (frm_end-frm_nxt >= 2 && frm_nxt[0] == 0xFE && frm_nxt[1] == 0xFF)
2852 frm_nxt += 2;
2853 }
2854 for (; frm_nxt < frm_end - 1 && to_nxt < to_end; ++to_nxt)
2855 {
Howard Hinnantec3773c2011-12-01 20:21:04 +00002856 uint16_t c1 = static_cast<uint16_t>(frm_nxt[0] << 8 | frm_nxt[1]);
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00002857 if ((c1 & 0xF800) == 0xD800 || c1 > Maxcode)
2858 return codecvt_base::error;
2859 *to_nxt = c1;
2860 frm_nxt += 2;
2861 }
2862 return frm_nxt < frm_end ? codecvt_base::partial : codecvt_base::ok;
2863}
2864
2865static
2866int
2867utf16be_to_ucs2_length(const uint8_t* frm, const uint8_t* frm_end,
2868 size_t mx, unsigned long Maxcode = 0x10FFFF,
2869 codecvt_mode mode = codecvt_mode(0))
2870{
2871 const uint8_t* frm_nxt = frm;
2872 frm_nxt = frm;
2873 if (mode & consume_header)
2874 {
2875 if (frm_end-frm_nxt >= 2 && frm_nxt[0] == 0xFE && frm_nxt[1] == 0xFF)
2876 frm_nxt += 2;
2877 }
2878 for (size_t nchar16_t = 0; frm_nxt < frm_end - 1 && nchar16_t < mx; ++nchar16_t)
2879 {
Howard Hinnantec3773c2011-12-01 20:21:04 +00002880 uint16_t c1 = static_cast<uint16_t>(frm_nxt[0] << 8 | frm_nxt[1]);
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00002881 if ((c1 & 0xF800) == 0xD800 || c1 > Maxcode)
2882 break;
2883 frm_nxt += 2;
2884 }
2885 return static_cast<int>(frm_nxt - frm);
2886}
2887
2888static
2889codecvt_base::result
2890ucs2_to_utf16le(const uint16_t* frm, const uint16_t* frm_end, const uint16_t*& frm_nxt,
2891 uint8_t* to, uint8_t* to_end, uint8_t*& to_nxt,
2892 unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0))
2893{
2894 frm_nxt = frm;
2895 to_nxt = to;
2896 if (mode & generate_header)
2897 {
2898 if (to_end-to_nxt < 2)
2899 return codecvt_base::partial;
2900 *to_nxt++ = static_cast<uint8_t>(0xFF);
2901 *to_nxt++ = static_cast<uint8_t>(0xFE);
2902 }
2903 for (; frm_nxt < frm_end; ++frm_nxt)
2904 {
2905 uint16_t wc = *frm_nxt;
2906 if ((wc & 0xF800) == 0xD800 || wc > Maxcode)
2907 return codecvt_base::error;
2908 if (to_end-to_nxt < 2)
2909 return codecvt_base::partial;
2910 *to_nxt++ = static_cast<uint8_t>(wc);
2911 *to_nxt++ = static_cast<uint8_t>(wc >> 8);
2912 }
2913 return codecvt_base::ok;
2914}
2915
2916static
2917codecvt_base::result
2918utf16le_to_ucs2(const uint8_t* frm, const uint8_t* frm_end, const uint8_t*& frm_nxt,
2919 uint16_t* to, uint16_t* to_end, uint16_t*& to_nxt,
2920 unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0))
2921{
2922 frm_nxt = frm;
2923 to_nxt = to;
2924 if (mode & consume_header)
2925 {
2926 if (frm_end-frm_nxt >= 2 && frm_nxt[0] == 0xFF && frm_nxt[1] == 0xFE)
2927 frm_nxt += 2;
2928 }
2929 for (; frm_nxt < frm_end - 1 && to_nxt < to_end; ++to_nxt)
2930 {
Howard Hinnantec3773c2011-12-01 20:21:04 +00002931 uint16_t c1 = static_cast<uint16_t>(frm_nxt[1] << 8 | frm_nxt[0]);
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00002932 if ((c1 & 0xF800) == 0xD800 || c1 > Maxcode)
2933 return codecvt_base::error;
2934 *to_nxt = c1;
2935 frm_nxt += 2;
2936 }
2937 return frm_nxt < frm_end ? codecvt_base::partial : codecvt_base::ok;
2938}
2939
2940static
2941int
2942utf16le_to_ucs2_length(const uint8_t* frm, const uint8_t* frm_end,
2943 size_t mx, unsigned long Maxcode = 0x10FFFF,
2944 codecvt_mode mode = codecvt_mode(0))
2945{
2946 const uint8_t* frm_nxt = frm;
2947 frm_nxt = frm;
2948 if (mode & consume_header)
2949 {
2950 if (frm_end-frm_nxt >= 2 && frm_nxt[0] == 0xFF && frm_nxt[1] == 0xFE)
2951 frm_nxt += 2;
2952 }
2953 for (size_t nchar16_t = 0; frm_nxt < frm_end - 1 && nchar16_t < mx; ++nchar16_t)
2954 {
Howard Hinnantec3773c2011-12-01 20:21:04 +00002955 uint16_t c1 = static_cast<uint16_t>(frm_nxt[1] << 8 | frm_nxt[0]);
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00002956 if ((c1 & 0xF800) == 0xD800 || c1 > Maxcode)
2957 break;
2958 frm_nxt += 2;
2959 }
2960 return static_cast<int>(frm_nxt - frm);
2961}
2962
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002963// template <> class codecvt<char16_t, char, mbstate_t>
2964
Howard Hinnant16e6e1d2010-08-22 00:03:27 +00002965locale::id codecvt<char16_t, char, mbstate_t>::id;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002966
2967codecvt<char16_t, char, mbstate_t>::~codecvt()
2968{
2969}
2970
2971codecvt<char16_t, char, mbstate_t>::result
2972codecvt<char16_t, char, mbstate_t>::do_out(state_type&,
Howard Hinnant16e6e1d2010-08-22 00:03:27 +00002973 const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002974 extern_type* to, extern_type* to_end, extern_type*& to_nxt) const
2975{
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00002976 const uint16_t* _frm = reinterpret_cast<const uint16_t*>(frm);
2977 const uint16_t* _frm_end = reinterpret_cast<const uint16_t*>(frm_end);
2978 const uint16_t* _frm_nxt = _frm;
2979 uint8_t* _to = reinterpret_cast<uint8_t*>(to);
2980 uint8_t* _to_end = reinterpret_cast<uint8_t*>(to_end);
2981 uint8_t* _to_nxt = _to;
2982 result r = utf16_to_utf8(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt);
2983 frm_nxt = frm + (_frm_nxt - _frm);
2984 to_nxt = to + (_to_nxt - _to);
2985 return r;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002986}
2987
2988codecvt<char16_t, char, mbstate_t>::result
2989codecvt<char16_t, char, mbstate_t>::do_in(state_type&,
Howard Hinnant16e6e1d2010-08-22 00:03:27 +00002990 const extern_type* frm, const extern_type* frm_end, const extern_type*& frm_nxt,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002991 intern_type* to, intern_type* to_end, intern_type*& to_nxt) const
2992{
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00002993 const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
2994 const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
2995 const uint8_t* _frm_nxt = _frm;
2996 uint16_t* _to = reinterpret_cast<uint16_t*>(to);
2997 uint16_t* _to_end = reinterpret_cast<uint16_t*>(to_end);
2998 uint16_t* _to_nxt = _to;
2999 result r = utf8_to_utf16(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt);
3000 frm_nxt = frm + (_frm_nxt - _frm);
3001 to_nxt = to + (_to_nxt - _to);
3002 return r;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003003}
3004
3005codecvt<char16_t, char, mbstate_t>::result
3006codecvt<char16_t, char, mbstate_t>::do_unshift(state_type&,
3007 extern_type* to, extern_type*, extern_type*& to_nxt) const
3008{
3009 to_nxt = to;
3010 return noconv;
3011}
3012
3013int
Howard Hinnantc9834542011-05-31 15:34:58 +00003014codecvt<char16_t, char, mbstate_t>::do_encoding() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003015{
3016 return 0;
3017}
3018
3019bool
Howard Hinnantc9834542011-05-31 15:34:58 +00003020codecvt<char16_t, char, mbstate_t>::do_always_noconv() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003021{
3022 return false;
3023}
3024
3025int
3026codecvt<char16_t, char, mbstate_t>::do_length(state_type&,
3027 const extern_type* frm, const extern_type* frm_end, size_t mx) const
3028{
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00003029 const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3030 const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3031 return utf8_to_utf16_length(_frm, _frm_end, mx);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003032}
3033
3034int
Howard Hinnantc9834542011-05-31 15:34:58 +00003035codecvt<char16_t, char, mbstate_t>::do_max_length() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003036{
3037 return 4;
3038}
3039
3040// template <> class codecvt<char32_t, char, mbstate_t>
3041
Howard Hinnant16e6e1d2010-08-22 00:03:27 +00003042locale::id codecvt<char32_t, char, mbstate_t>::id;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003043
3044codecvt<char32_t, char, mbstate_t>::~codecvt()
3045{
3046}
3047
3048codecvt<char32_t, char, mbstate_t>::result
3049codecvt<char32_t, char, mbstate_t>::do_out(state_type&,
Howard Hinnant16e6e1d2010-08-22 00:03:27 +00003050 const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003051 extern_type* to, extern_type* to_end, extern_type*& to_nxt) const
3052{
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00003053 const uint32_t* _frm = reinterpret_cast<const uint32_t*>(frm);
3054 const uint32_t* _frm_end = reinterpret_cast<const uint32_t*>(frm_end);
3055 const uint32_t* _frm_nxt = _frm;
3056 uint8_t* _to = reinterpret_cast<uint8_t*>(to);
3057 uint8_t* _to_end = reinterpret_cast<uint8_t*>(to_end);
3058 uint8_t* _to_nxt = _to;
3059 result r = ucs4_to_utf8(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt);
3060 frm_nxt = frm + (_frm_nxt - _frm);
3061 to_nxt = to + (_to_nxt - _to);
3062 return r;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003063}
3064
3065codecvt<char32_t, char, mbstate_t>::result
3066codecvt<char32_t, char, mbstate_t>::do_in(state_type&,
Howard Hinnant16e6e1d2010-08-22 00:03:27 +00003067 const extern_type* frm, const extern_type* frm_end, const extern_type*& frm_nxt,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003068 intern_type* to, intern_type* to_end, intern_type*& to_nxt) const
3069{
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00003070 const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3071 const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3072 const uint8_t* _frm_nxt = _frm;
3073 uint32_t* _to = reinterpret_cast<uint32_t*>(to);
3074 uint32_t* _to_end = reinterpret_cast<uint32_t*>(to_end);
3075 uint32_t* _to_nxt = _to;
3076 result r = utf8_to_ucs4(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt);
3077 frm_nxt = frm + (_frm_nxt - _frm);
3078 to_nxt = to + (_to_nxt - _to);
3079 return r;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003080}
3081
3082codecvt<char32_t, char, mbstate_t>::result
3083codecvt<char32_t, char, mbstate_t>::do_unshift(state_type&,
3084 extern_type* to, extern_type*, extern_type*& to_nxt) const
3085{
3086 to_nxt = to;
3087 return noconv;
3088}
3089
3090int
Howard Hinnantc9834542011-05-31 15:34:58 +00003091codecvt<char32_t, char, mbstate_t>::do_encoding() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003092{
3093 return 0;
3094}
3095
3096bool
Howard Hinnantc9834542011-05-31 15:34:58 +00003097codecvt<char32_t, char, mbstate_t>::do_always_noconv() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003098{
3099 return false;
3100}
3101
3102int
3103codecvt<char32_t, char, mbstate_t>::do_length(state_type&,
3104 const extern_type* frm, const extern_type* frm_end, size_t mx) const
3105{
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00003106 const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3107 const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3108 return utf8_to_ucs4_length(_frm, _frm_end, mx);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003109}
3110
3111int
Howard Hinnantc9834542011-05-31 15:34:58 +00003112codecvt<char32_t, char, mbstate_t>::do_max_length() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003113{
3114 return 4;
3115}
3116
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00003117// __codecvt_utf8<wchar_t>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003118
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00003119__codecvt_utf8<wchar_t>::result
3120__codecvt_utf8<wchar_t>::do_out(state_type&,
3121 const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003122 extern_type* to, extern_type* to_end, extern_type*& to_nxt) const
3123{
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00003124 const uint32_t* _frm = reinterpret_cast<const uint32_t*>(frm);
3125 const uint32_t* _frm_end = reinterpret_cast<const uint32_t*>(frm_end);
3126 const uint32_t* _frm_nxt = _frm;
3127 uint8_t* _to = reinterpret_cast<uint8_t*>(to);
3128 uint8_t* _to_end = reinterpret_cast<uint8_t*>(to_end);
3129 uint8_t* _to_nxt = _to;
3130 result r = ucs4_to_utf8(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3131 _Maxcode_, _Mode_);
3132 frm_nxt = frm + (_frm_nxt - _frm);
3133 to_nxt = to + (_to_nxt - _to);
3134 return r;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003135}
3136
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00003137__codecvt_utf8<wchar_t>::result
3138__codecvt_utf8<wchar_t>::do_in(state_type&,
3139 const extern_type* frm, const extern_type* frm_end, const extern_type*& frm_nxt,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003140 intern_type* to, intern_type* to_end, intern_type*& to_nxt) const
3141{
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00003142 const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3143 const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3144 const uint8_t* _frm_nxt = _frm;
3145 uint32_t* _to = reinterpret_cast<uint32_t*>(to);
3146 uint32_t* _to_end = reinterpret_cast<uint32_t*>(to_end);
3147 uint32_t* _to_nxt = _to;
3148 result r = utf8_to_ucs4(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3149 _Maxcode_, _Mode_);
3150 frm_nxt = frm + (_frm_nxt - _frm);
3151 to_nxt = to + (_to_nxt - _to);
3152 return r;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003153}
3154
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00003155__codecvt_utf8<wchar_t>::result
3156__codecvt_utf8<wchar_t>::do_unshift(state_type&,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003157 extern_type* to, extern_type*, extern_type*& to_nxt) const
3158{
3159 to_nxt = to;
3160 return noconv;
3161}
3162
3163int
Howard Hinnantc9834542011-05-31 15:34:58 +00003164__codecvt_utf8<wchar_t>::do_encoding() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003165{
3166 return 0;
3167}
3168
3169bool
Howard Hinnantc9834542011-05-31 15:34:58 +00003170__codecvt_utf8<wchar_t>::do_always_noconv() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003171{
3172 return false;
3173}
3174
3175int
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00003176__codecvt_utf8<wchar_t>::do_length(state_type&,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003177 const extern_type* frm, const extern_type* frm_end, size_t mx) const
3178{
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00003179 const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3180 const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3181 return utf8_to_ucs4_length(_frm, _frm_end, mx, _Maxcode_, _Mode_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003182}
3183
3184int
Howard Hinnantc9834542011-05-31 15:34:58 +00003185__codecvt_utf8<wchar_t>::do_max_length() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003186{
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00003187 if (_Mode_ & consume_header)
3188 return 7;
3189 return 4;
3190}
3191
3192// __codecvt_utf8<char16_t>
3193
3194__codecvt_utf8<char16_t>::result
3195__codecvt_utf8<char16_t>::do_out(state_type&,
3196 const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt,
3197 extern_type* to, extern_type* to_end, extern_type*& to_nxt) const
3198{
3199 const uint16_t* _frm = reinterpret_cast<const uint16_t*>(frm);
3200 const uint16_t* _frm_end = reinterpret_cast<const uint16_t*>(frm_end);
3201 const uint16_t* _frm_nxt = _frm;
3202 uint8_t* _to = reinterpret_cast<uint8_t*>(to);
3203 uint8_t* _to_end = reinterpret_cast<uint8_t*>(to_end);
3204 uint8_t* _to_nxt = _to;
3205 result r = ucs2_to_utf8(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3206 _Maxcode_, _Mode_);
3207 frm_nxt = frm + (_frm_nxt - _frm);
3208 to_nxt = to + (_to_nxt - _to);
3209 return r;
3210}
3211
3212__codecvt_utf8<char16_t>::result
3213__codecvt_utf8<char16_t>::do_in(state_type&,
3214 const extern_type* frm, const extern_type* frm_end, const extern_type*& frm_nxt,
3215 intern_type* to, intern_type* to_end, intern_type*& to_nxt) const
3216{
3217 const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3218 const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3219 const uint8_t* _frm_nxt = _frm;
3220 uint16_t* _to = reinterpret_cast<uint16_t*>(to);
3221 uint16_t* _to_end = reinterpret_cast<uint16_t*>(to_end);
3222 uint16_t* _to_nxt = _to;
3223 result r = utf8_to_ucs2(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3224 _Maxcode_, _Mode_);
3225 frm_nxt = frm + (_frm_nxt - _frm);
3226 to_nxt = to + (_to_nxt - _to);
3227 return r;
3228}
3229
3230__codecvt_utf8<char16_t>::result
3231__codecvt_utf8<char16_t>::do_unshift(state_type&,
3232 extern_type* to, extern_type*, extern_type*& to_nxt) const
3233{
3234 to_nxt = to;
3235 return noconv;
3236}
3237
3238int
Howard Hinnantc9834542011-05-31 15:34:58 +00003239__codecvt_utf8<char16_t>::do_encoding() const _NOEXCEPT
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00003240{
3241 return 0;
3242}
3243
3244bool
Howard Hinnantc9834542011-05-31 15:34:58 +00003245__codecvt_utf8<char16_t>::do_always_noconv() const _NOEXCEPT
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00003246{
3247 return false;
3248}
3249
3250int
3251__codecvt_utf8<char16_t>::do_length(state_type&,
3252 const extern_type* frm, const extern_type* frm_end, size_t mx) const
3253{
3254 const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3255 const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3256 return utf8_to_ucs2_length(_frm, _frm_end, mx, _Maxcode_, _Mode_);
3257}
3258
3259int
Howard Hinnantc9834542011-05-31 15:34:58 +00003260__codecvt_utf8<char16_t>::do_max_length() const _NOEXCEPT
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00003261{
3262 if (_Mode_ & consume_header)
3263 return 6;
3264 return 3;
3265}
3266
3267// __codecvt_utf8<char32_t>
3268
3269__codecvt_utf8<char32_t>::result
3270__codecvt_utf8<char32_t>::do_out(state_type&,
3271 const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt,
3272 extern_type* to, extern_type* to_end, extern_type*& to_nxt) const
3273{
3274 const uint32_t* _frm = reinterpret_cast<const uint32_t*>(frm);
3275 const uint32_t* _frm_end = reinterpret_cast<const uint32_t*>(frm_end);
3276 const uint32_t* _frm_nxt = _frm;
3277 uint8_t* _to = reinterpret_cast<uint8_t*>(to);
3278 uint8_t* _to_end = reinterpret_cast<uint8_t*>(to_end);
3279 uint8_t* _to_nxt = _to;
3280 result r = ucs4_to_utf8(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3281 _Maxcode_, _Mode_);
3282 frm_nxt = frm + (_frm_nxt - _frm);
3283 to_nxt = to + (_to_nxt - _to);
3284 return r;
3285}
3286
3287__codecvt_utf8<char32_t>::result
3288__codecvt_utf8<char32_t>::do_in(state_type&,
3289 const extern_type* frm, const extern_type* frm_end, const extern_type*& frm_nxt,
3290 intern_type* to, intern_type* to_end, intern_type*& to_nxt) const
3291{
3292 const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3293 const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3294 const uint8_t* _frm_nxt = _frm;
3295 uint32_t* _to = reinterpret_cast<uint32_t*>(to);
3296 uint32_t* _to_end = reinterpret_cast<uint32_t*>(to_end);
3297 uint32_t* _to_nxt = _to;
3298 result r = utf8_to_ucs4(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3299 _Maxcode_, _Mode_);
3300 frm_nxt = frm + (_frm_nxt - _frm);
3301 to_nxt = to + (_to_nxt - _to);
3302 return r;
3303}
3304
3305__codecvt_utf8<char32_t>::result
3306__codecvt_utf8<char32_t>::do_unshift(state_type&,
3307 extern_type* to, extern_type*, extern_type*& to_nxt) const
3308{
3309 to_nxt = to;
3310 return noconv;
3311}
3312
3313int
Howard Hinnantc9834542011-05-31 15:34:58 +00003314__codecvt_utf8<char32_t>::do_encoding() const _NOEXCEPT
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00003315{
3316 return 0;
3317}
3318
3319bool
Howard Hinnantc9834542011-05-31 15:34:58 +00003320__codecvt_utf8<char32_t>::do_always_noconv() const _NOEXCEPT
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00003321{
3322 return false;
3323}
3324
3325int
3326__codecvt_utf8<char32_t>::do_length(state_type&,
3327 const extern_type* frm, const extern_type* frm_end, size_t mx) const
3328{
3329 const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3330 const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3331 return utf8_to_ucs4_length(_frm, _frm_end, mx, _Maxcode_, _Mode_);
3332}
3333
3334int
Howard Hinnantc9834542011-05-31 15:34:58 +00003335__codecvt_utf8<char32_t>::do_max_length() const _NOEXCEPT
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00003336{
3337 if (_Mode_ & consume_header)
3338 return 7;
3339 return 4;
3340}
3341
3342// __codecvt_utf16<wchar_t, false>
3343
3344__codecvt_utf16<wchar_t, false>::result
3345__codecvt_utf16<wchar_t, false>::do_out(state_type&,
3346 const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt,
3347 extern_type* to, extern_type* to_end, extern_type*& to_nxt) const
3348{
3349 const uint32_t* _frm = reinterpret_cast<const uint32_t*>(frm);
3350 const uint32_t* _frm_end = reinterpret_cast<const uint32_t*>(frm_end);
3351 const uint32_t* _frm_nxt = _frm;
3352 uint8_t* _to = reinterpret_cast<uint8_t*>(to);
3353 uint8_t* _to_end = reinterpret_cast<uint8_t*>(to_end);
3354 uint8_t* _to_nxt = _to;
3355 result r = ucs4_to_utf16be(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3356 _Maxcode_, _Mode_);
3357 frm_nxt = frm + (_frm_nxt - _frm);
3358 to_nxt = to + (_to_nxt - _to);
3359 return r;
3360}
3361
3362__codecvt_utf16<wchar_t, false>::result
3363__codecvt_utf16<wchar_t, false>::do_in(state_type&,
3364 const extern_type* frm, const extern_type* frm_end, const extern_type*& frm_nxt,
3365 intern_type* to, intern_type* to_end, intern_type*& to_nxt) const
3366{
3367 const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3368 const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3369 const uint8_t* _frm_nxt = _frm;
3370 uint32_t* _to = reinterpret_cast<uint32_t*>(to);
3371 uint32_t* _to_end = reinterpret_cast<uint32_t*>(to_end);
3372 uint32_t* _to_nxt = _to;
3373 result r = utf16be_to_ucs4(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3374 _Maxcode_, _Mode_);
3375 frm_nxt = frm + (_frm_nxt - _frm);
3376 to_nxt = to + (_to_nxt - _to);
3377 return r;
3378}
3379
3380__codecvt_utf16<wchar_t, false>::result
3381__codecvt_utf16<wchar_t, false>::do_unshift(state_type&,
3382 extern_type* to, extern_type*, extern_type*& to_nxt) const
3383{
3384 to_nxt = to;
3385 return noconv;
3386}
3387
3388int
Howard Hinnantc9834542011-05-31 15:34:58 +00003389__codecvt_utf16<wchar_t, false>::do_encoding() const _NOEXCEPT
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00003390{
3391 return 0;
3392}
3393
3394bool
Howard Hinnantc9834542011-05-31 15:34:58 +00003395__codecvt_utf16<wchar_t, false>::do_always_noconv() const _NOEXCEPT
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00003396{
3397 return false;
3398}
3399
3400int
3401__codecvt_utf16<wchar_t, false>::do_length(state_type&,
3402 const extern_type* frm, const extern_type* frm_end, size_t mx) const
3403{
3404 const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3405 const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3406 return utf16be_to_ucs4_length(_frm, _frm_end, mx, _Maxcode_, _Mode_);
3407}
3408
3409int
Howard Hinnantc9834542011-05-31 15:34:58 +00003410__codecvt_utf16<wchar_t, false>::do_max_length() const _NOEXCEPT
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00003411{
3412 if (_Mode_ & consume_header)
3413 return 6;
3414 return 4;
3415}
3416
3417// __codecvt_utf16<wchar_t, true>
3418
3419__codecvt_utf16<wchar_t, true>::result
3420__codecvt_utf16<wchar_t, true>::do_out(state_type&,
3421 const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt,
3422 extern_type* to, extern_type* to_end, extern_type*& to_nxt) const
3423{
3424 const uint32_t* _frm = reinterpret_cast<const uint32_t*>(frm);
3425 const uint32_t* _frm_end = reinterpret_cast<const uint32_t*>(frm_end);
3426 const uint32_t* _frm_nxt = _frm;
3427 uint8_t* _to = reinterpret_cast<uint8_t*>(to);
3428 uint8_t* _to_end = reinterpret_cast<uint8_t*>(to_end);
3429 uint8_t* _to_nxt = _to;
3430 result r = ucs4_to_utf16le(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3431 _Maxcode_, _Mode_);
3432 frm_nxt = frm + (_frm_nxt - _frm);
3433 to_nxt = to + (_to_nxt - _to);
3434 return r;
3435}
3436
3437__codecvt_utf16<wchar_t, true>::result
3438__codecvt_utf16<wchar_t, true>::do_in(state_type&,
3439 const extern_type* frm, const extern_type* frm_end, const extern_type*& frm_nxt,
3440 intern_type* to, intern_type* to_end, intern_type*& to_nxt) const
3441{
3442 const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3443 const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3444 const uint8_t* _frm_nxt = _frm;
3445 uint32_t* _to = reinterpret_cast<uint32_t*>(to);
3446 uint32_t* _to_end = reinterpret_cast<uint32_t*>(to_end);
3447 uint32_t* _to_nxt = _to;
3448 result r = utf16le_to_ucs4(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3449 _Maxcode_, _Mode_);
3450 frm_nxt = frm + (_frm_nxt - _frm);
3451 to_nxt = to + (_to_nxt - _to);
3452 return r;
3453}
3454
3455__codecvt_utf16<wchar_t, true>::result
3456__codecvt_utf16<wchar_t, true>::do_unshift(state_type&,
3457 extern_type* to, extern_type*, extern_type*& to_nxt) const
3458{
3459 to_nxt = to;
3460 return noconv;
3461}
3462
3463int
Howard Hinnantc9834542011-05-31 15:34:58 +00003464__codecvt_utf16<wchar_t, true>::do_encoding() const _NOEXCEPT
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00003465{
3466 return 0;
3467}
3468
3469bool
Howard Hinnantc9834542011-05-31 15:34:58 +00003470__codecvt_utf16<wchar_t, true>::do_always_noconv() const _NOEXCEPT
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00003471{
Howard Hinnantd23b4642010-05-31 20:58:54 +00003472 return false;
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00003473}
3474
3475int
3476__codecvt_utf16<wchar_t, true>::do_length(state_type&,
3477 const extern_type* frm, const extern_type* frm_end, size_t mx) const
3478{
3479 const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3480 const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3481 return utf16le_to_ucs4_length(_frm, _frm_end, mx, _Maxcode_, _Mode_);
3482}
3483
3484int
Howard Hinnantc9834542011-05-31 15:34:58 +00003485__codecvt_utf16<wchar_t, true>::do_max_length() const _NOEXCEPT
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00003486{
3487 if (_Mode_ & consume_header)
3488 return 6;
3489 return 4;
3490}
3491
3492// __codecvt_utf16<char16_t, false>
3493
3494__codecvt_utf16<char16_t, false>::result
3495__codecvt_utf16<char16_t, false>::do_out(state_type&,
3496 const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt,
3497 extern_type* to, extern_type* to_end, extern_type*& to_nxt) const
3498{
3499 const uint16_t* _frm = reinterpret_cast<const uint16_t*>(frm);
3500 const uint16_t* _frm_end = reinterpret_cast<const uint16_t*>(frm_end);
3501 const uint16_t* _frm_nxt = _frm;
3502 uint8_t* _to = reinterpret_cast<uint8_t*>(to);
3503 uint8_t* _to_end = reinterpret_cast<uint8_t*>(to_end);
3504 uint8_t* _to_nxt = _to;
3505 result r = ucs2_to_utf16be(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3506 _Maxcode_, _Mode_);
3507 frm_nxt = frm + (_frm_nxt - _frm);
3508 to_nxt = to + (_to_nxt - _to);
3509 return r;
3510}
3511
3512__codecvt_utf16<char16_t, false>::result
3513__codecvt_utf16<char16_t, false>::do_in(state_type&,
3514 const extern_type* frm, const extern_type* frm_end, const extern_type*& frm_nxt,
3515 intern_type* to, intern_type* to_end, intern_type*& to_nxt) const
3516{
3517 const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3518 const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3519 const uint8_t* _frm_nxt = _frm;
3520 uint16_t* _to = reinterpret_cast<uint16_t*>(to);
3521 uint16_t* _to_end = reinterpret_cast<uint16_t*>(to_end);
3522 uint16_t* _to_nxt = _to;
3523 result r = utf16be_to_ucs2(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3524 _Maxcode_, _Mode_);
3525 frm_nxt = frm + (_frm_nxt - _frm);
3526 to_nxt = to + (_to_nxt - _to);
3527 return r;
3528}
3529
3530__codecvt_utf16<char16_t, false>::result
3531__codecvt_utf16<char16_t, false>::do_unshift(state_type&,
3532 extern_type* to, extern_type*, extern_type*& to_nxt) const
3533{
3534 to_nxt = to;
3535 return noconv;
3536}
3537
3538int
Howard Hinnantc9834542011-05-31 15:34:58 +00003539__codecvt_utf16<char16_t, false>::do_encoding() const _NOEXCEPT
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00003540{
3541 return 0;
3542}
3543
3544bool
Howard Hinnantc9834542011-05-31 15:34:58 +00003545__codecvt_utf16<char16_t, false>::do_always_noconv() const _NOEXCEPT
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00003546{
3547 return false;
3548}
3549
3550int
3551__codecvt_utf16<char16_t, false>::do_length(state_type&,
3552 const extern_type* frm, const extern_type* frm_end, size_t mx) const
3553{
3554 const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3555 const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3556 return utf16be_to_ucs2_length(_frm, _frm_end, mx, _Maxcode_, _Mode_);
3557}
3558
3559int
Howard Hinnantc9834542011-05-31 15:34:58 +00003560__codecvt_utf16<char16_t, false>::do_max_length() const _NOEXCEPT
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00003561{
3562 if (_Mode_ & consume_header)
3563 return 4;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003564 return 2;
3565}
3566
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00003567// __codecvt_utf16<char16_t, true>
3568
3569__codecvt_utf16<char16_t, true>::result
3570__codecvt_utf16<char16_t, true>::do_out(state_type&,
3571 const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt,
3572 extern_type* to, extern_type* to_end, extern_type*& to_nxt) const
3573{
3574 const uint16_t* _frm = reinterpret_cast<const uint16_t*>(frm);
3575 const uint16_t* _frm_end = reinterpret_cast<const uint16_t*>(frm_end);
3576 const uint16_t* _frm_nxt = _frm;
3577 uint8_t* _to = reinterpret_cast<uint8_t*>(to);
3578 uint8_t* _to_end = reinterpret_cast<uint8_t*>(to_end);
3579 uint8_t* _to_nxt = _to;
3580 result r = ucs2_to_utf16le(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3581 _Maxcode_, _Mode_);
3582 frm_nxt = frm + (_frm_nxt - _frm);
3583 to_nxt = to + (_to_nxt - _to);
3584 return r;
3585}
3586
3587__codecvt_utf16<char16_t, true>::result
3588__codecvt_utf16<char16_t, true>::do_in(state_type&,
3589 const extern_type* frm, const extern_type* frm_end, const extern_type*& frm_nxt,
3590 intern_type* to, intern_type* to_end, intern_type*& to_nxt) const
3591{
3592 const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3593 const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3594 const uint8_t* _frm_nxt = _frm;
3595 uint16_t* _to = reinterpret_cast<uint16_t*>(to);
3596 uint16_t* _to_end = reinterpret_cast<uint16_t*>(to_end);
3597 uint16_t* _to_nxt = _to;
3598 result r = utf16le_to_ucs2(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3599 _Maxcode_, _Mode_);
3600 frm_nxt = frm + (_frm_nxt - _frm);
3601 to_nxt = to + (_to_nxt - _to);
3602 return r;
3603}
3604
3605__codecvt_utf16<char16_t, true>::result
3606__codecvt_utf16<char16_t, true>::do_unshift(state_type&,
3607 extern_type* to, extern_type*, extern_type*& to_nxt) const
3608{
3609 to_nxt = to;
3610 return noconv;
3611}
3612
3613int
Howard Hinnantc9834542011-05-31 15:34:58 +00003614__codecvt_utf16<char16_t, true>::do_encoding() const _NOEXCEPT
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00003615{
3616 return 0;
3617}
3618
3619bool
Howard Hinnantc9834542011-05-31 15:34:58 +00003620__codecvt_utf16<char16_t, true>::do_always_noconv() const _NOEXCEPT
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00003621{
Howard Hinnantd23b4642010-05-31 20:58:54 +00003622 return false;
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00003623}
3624
3625int
3626__codecvt_utf16<char16_t, true>::do_length(state_type&,
3627 const extern_type* frm, const extern_type* frm_end, size_t mx) const
3628{
3629 const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3630 const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3631 return utf16le_to_ucs2_length(_frm, _frm_end, mx, _Maxcode_, _Mode_);
3632}
3633
3634int
Howard Hinnantc9834542011-05-31 15:34:58 +00003635__codecvt_utf16<char16_t, true>::do_max_length() const _NOEXCEPT
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00003636{
3637 if (_Mode_ & consume_header)
3638 return 4;
3639 return 2;
3640}
3641
3642// __codecvt_utf16<char32_t, false>
3643
3644__codecvt_utf16<char32_t, false>::result
3645__codecvt_utf16<char32_t, false>::do_out(state_type&,
3646 const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt,
3647 extern_type* to, extern_type* to_end, extern_type*& to_nxt) const
3648{
3649 const uint32_t* _frm = reinterpret_cast<const uint32_t*>(frm);
3650 const uint32_t* _frm_end = reinterpret_cast<const uint32_t*>(frm_end);
3651 const uint32_t* _frm_nxt = _frm;
3652 uint8_t* _to = reinterpret_cast<uint8_t*>(to);
3653 uint8_t* _to_end = reinterpret_cast<uint8_t*>(to_end);
3654 uint8_t* _to_nxt = _to;
3655 result r = ucs4_to_utf16be(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3656 _Maxcode_, _Mode_);
3657 frm_nxt = frm + (_frm_nxt - _frm);
3658 to_nxt = to + (_to_nxt - _to);
3659 return r;
3660}
3661
3662__codecvt_utf16<char32_t, false>::result
3663__codecvt_utf16<char32_t, false>::do_in(state_type&,
3664 const extern_type* frm, const extern_type* frm_end, const extern_type*& frm_nxt,
3665 intern_type* to, intern_type* to_end, intern_type*& to_nxt) const
3666{
3667 const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3668 const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3669 const uint8_t* _frm_nxt = _frm;
3670 uint32_t* _to = reinterpret_cast<uint32_t*>(to);
3671 uint32_t* _to_end = reinterpret_cast<uint32_t*>(to_end);
3672 uint32_t* _to_nxt = _to;
3673 result r = utf16be_to_ucs4(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3674 _Maxcode_, _Mode_);
3675 frm_nxt = frm + (_frm_nxt - _frm);
3676 to_nxt = to + (_to_nxt - _to);
3677 return r;
3678}
3679
3680__codecvt_utf16<char32_t, false>::result
3681__codecvt_utf16<char32_t, false>::do_unshift(state_type&,
3682 extern_type* to, extern_type*, extern_type*& to_nxt) const
3683{
3684 to_nxt = to;
3685 return noconv;
3686}
3687
3688int
Howard Hinnantc9834542011-05-31 15:34:58 +00003689__codecvt_utf16<char32_t, false>::do_encoding() const _NOEXCEPT
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00003690{
3691 return 0;
3692}
3693
3694bool
Howard Hinnantc9834542011-05-31 15:34:58 +00003695__codecvt_utf16<char32_t, false>::do_always_noconv() const _NOEXCEPT
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00003696{
3697 return false;
3698}
3699
3700int
3701__codecvt_utf16<char32_t, false>::do_length(state_type&,
3702 const extern_type* frm, const extern_type* frm_end, size_t mx) const
3703{
3704 const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3705 const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3706 return utf16be_to_ucs4_length(_frm, _frm_end, mx, _Maxcode_, _Mode_);
3707}
3708
3709int
Howard Hinnantc9834542011-05-31 15:34:58 +00003710__codecvt_utf16<char32_t, false>::do_max_length() const _NOEXCEPT
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00003711{
3712 if (_Mode_ & consume_header)
3713 return 6;
3714 return 4;
3715}
3716
3717// __codecvt_utf16<char32_t, true>
3718
3719__codecvt_utf16<char32_t, true>::result
3720__codecvt_utf16<char32_t, true>::do_out(state_type&,
3721 const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt,
3722 extern_type* to, extern_type* to_end, extern_type*& to_nxt) const
3723{
3724 const uint32_t* _frm = reinterpret_cast<const uint32_t*>(frm);
3725 const uint32_t* _frm_end = reinterpret_cast<const uint32_t*>(frm_end);
3726 const uint32_t* _frm_nxt = _frm;
3727 uint8_t* _to = reinterpret_cast<uint8_t*>(to);
3728 uint8_t* _to_end = reinterpret_cast<uint8_t*>(to_end);
3729 uint8_t* _to_nxt = _to;
3730 result r = ucs4_to_utf16le(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3731 _Maxcode_, _Mode_);
3732 frm_nxt = frm + (_frm_nxt - _frm);
3733 to_nxt = to + (_to_nxt - _to);
3734 return r;
3735}
3736
3737__codecvt_utf16<char32_t, true>::result
3738__codecvt_utf16<char32_t, true>::do_in(state_type&,
3739 const extern_type* frm, const extern_type* frm_end, const extern_type*& frm_nxt,
3740 intern_type* to, intern_type* to_end, intern_type*& to_nxt) const
3741{
3742 const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3743 const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3744 const uint8_t* _frm_nxt = _frm;
3745 uint32_t* _to = reinterpret_cast<uint32_t*>(to);
3746 uint32_t* _to_end = reinterpret_cast<uint32_t*>(to_end);
3747 uint32_t* _to_nxt = _to;
3748 result r = utf16le_to_ucs4(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3749 _Maxcode_, _Mode_);
3750 frm_nxt = frm + (_frm_nxt - _frm);
3751 to_nxt = to + (_to_nxt - _to);
3752 return r;
3753}
3754
3755__codecvt_utf16<char32_t, true>::result
3756__codecvt_utf16<char32_t, true>::do_unshift(state_type&,
3757 extern_type* to, extern_type*, extern_type*& to_nxt) const
3758{
3759 to_nxt = to;
3760 return noconv;
3761}
3762
3763int
Howard Hinnantc9834542011-05-31 15:34:58 +00003764__codecvt_utf16<char32_t, true>::do_encoding() const _NOEXCEPT
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00003765{
3766 return 0;
3767}
3768
3769bool
Howard Hinnantc9834542011-05-31 15:34:58 +00003770__codecvt_utf16<char32_t, true>::do_always_noconv() const _NOEXCEPT
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00003771{
Howard Hinnantd23b4642010-05-31 20:58:54 +00003772 return false;
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00003773}
3774
3775int
3776__codecvt_utf16<char32_t, true>::do_length(state_type&,
3777 const extern_type* frm, const extern_type* frm_end, size_t mx) const
3778{
3779 const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3780 const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3781 return utf16le_to_ucs4_length(_frm, _frm_end, mx, _Maxcode_, _Mode_);
3782}
3783
3784int
Howard Hinnantc9834542011-05-31 15:34:58 +00003785__codecvt_utf16<char32_t, true>::do_max_length() const _NOEXCEPT
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00003786{
3787 if (_Mode_ & consume_header)
3788 return 6;
3789 return 4;
3790}
3791
3792// __codecvt_utf8_utf16<wchar_t>
3793
3794__codecvt_utf8_utf16<wchar_t>::result
3795__codecvt_utf8_utf16<wchar_t>::do_out(state_type&,
3796 const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt,
3797 extern_type* to, extern_type* to_end, extern_type*& to_nxt) const
3798{
3799 const uint32_t* _frm = reinterpret_cast<const uint32_t*>(frm);
3800 const uint32_t* _frm_end = reinterpret_cast<const uint32_t*>(frm_end);
3801 const uint32_t* _frm_nxt = _frm;
3802 uint8_t* _to = reinterpret_cast<uint8_t*>(to);
3803 uint8_t* _to_end = reinterpret_cast<uint8_t*>(to_end);
3804 uint8_t* _to_nxt = _to;
3805 result r = utf16_to_utf8(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3806 _Maxcode_, _Mode_);
3807 frm_nxt = frm + (_frm_nxt - _frm);
3808 to_nxt = to + (_to_nxt - _to);
3809 return r;
3810}
3811
3812__codecvt_utf8_utf16<wchar_t>::result
3813__codecvt_utf8_utf16<wchar_t>::do_in(state_type&,
3814 const extern_type* frm, const extern_type* frm_end, const extern_type*& frm_nxt,
3815 intern_type* to, intern_type* to_end, intern_type*& to_nxt) const
3816{
3817 const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3818 const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3819 const uint8_t* _frm_nxt = _frm;
3820 uint32_t* _to = reinterpret_cast<uint32_t*>(to);
3821 uint32_t* _to_end = reinterpret_cast<uint32_t*>(to_end);
3822 uint32_t* _to_nxt = _to;
3823 result r = utf8_to_utf16(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3824 _Maxcode_, _Mode_);
3825 frm_nxt = frm + (_frm_nxt - _frm);
3826 to_nxt = to + (_to_nxt - _to);
3827 return r;
3828}
3829
3830__codecvt_utf8_utf16<wchar_t>::result
3831__codecvt_utf8_utf16<wchar_t>::do_unshift(state_type&,
3832 extern_type* to, extern_type*, extern_type*& to_nxt) const
3833{
3834 to_nxt = to;
3835 return noconv;
3836}
3837
3838int
Howard Hinnantc9834542011-05-31 15:34:58 +00003839__codecvt_utf8_utf16<wchar_t>::do_encoding() const _NOEXCEPT
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00003840{
3841 return 0;
3842}
3843
3844bool
Howard Hinnantc9834542011-05-31 15:34:58 +00003845__codecvt_utf8_utf16<wchar_t>::do_always_noconv() const _NOEXCEPT
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00003846{
3847 return false;
3848}
3849
3850int
3851__codecvt_utf8_utf16<wchar_t>::do_length(state_type&,
3852 const extern_type* frm, const extern_type* frm_end, size_t mx) const
3853{
3854 const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3855 const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3856 return utf8_to_utf16_length(_frm, _frm_end, mx, _Maxcode_, _Mode_);
3857}
3858
3859int
Howard Hinnantc9834542011-05-31 15:34:58 +00003860__codecvt_utf8_utf16<wchar_t>::do_max_length() const _NOEXCEPT
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00003861{
3862 if (_Mode_ & consume_header)
3863 return 7;
3864 return 4;
3865}
3866
3867// __codecvt_utf8_utf16<char16_t>
3868
3869__codecvt_utf8_utf16<char16_t>::result
3870__codecvt_utf8_utf16<char16_t>::do_out(state_type&,
3871 const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt,
3872 extern_type* to, extern_type* to_end, extern_type*& to_nxt) const
3873{
3874 const uint16_t* _frm = reinterpret_cast<const uint16_t*>(frm);
3875 const uint16_t* _frm_end = reinterpret_cast<const uint16_t*>(frm_end);
3876 const uint16_t* _frm_nxt = _frm;
3877 uint8_t* _to = reinterpret_cast<uint8_t*>(to);
3878 uint8_t* _to_end = reinterpret_cast<uint8_t*>(to_end);
3879 uint8_t* _to_nxt = _to;
3880 result r = utf16_to_utf8(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3881 _Maxcode_, _Mode_);
3882 frm_nxt = frm + (_frm_nxt - _frm);
3883 to_nxt = to + (_to_nxt - _to);
3884 return r;
3885}
3886
3887__codecvt_utf8_utf16<char16_t>::result
3888__codecvt_utf8_utf16<char16_t>::do_in(state_type&,
3889 const extern_type* frm, const extern_type* frm_end, const extern_type*& frm_nxt,
3890 intern_type* to, intern_type* to_end, intern_type*& to_nxt) const
3891{
3892 const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3893 const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3894 const uint8_t* _frm_nxt = _frm;
3895 uint16_t* _to = reinterpret_cast<uint16_t*>(to);
3896 uint16_t* _to_end = reinterpret_cast<uint16_t*>(to_end);
3897 uint16_t* _to_nxt = _to;
3898 result r = utf8_to_utf16(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3899 _Maxcode_, _Mode_);
3900 frm_nxt = frm + (_frm_nxt - _frm);
3901 to_nxt = to + (_to_nxt - _to);
3902 return r;
3903}
3904
3905__codecvt_utf8_utf16<char16_t>::result
3906__codecvt_utf8_utf16<char16_t>::do_unshift(state_type&,
3907 extern_type* to, extern_type*, extern_type*& to_nxt) const
3908{
3909 to_nxt = to;
3910 return noconv;
3911}
3912
3913int
Howard Hinnantc9834542011-05-31 15:34:58 +00003914__codecvt_utf8_utf16<char16_t>::do_encoding() const _NOEXCEPT
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00003915{
3916 return 0;
3917}
3918
3919bool
Howard Hinnantc9834542011-05-31 15:34:58 +00003920__codecvt_utf8_utf16<char16_t>::do_always_noconv() const _NOEXCEPT
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00003921{
3922 return false;
3923}
3924
3925int
3926__codecvt_utf8_utf16<char16_t>::do_length(state_type&,
3927 const extern_type* frm, const extern_type* frm_end, size_t mx) const
3928{
3929 const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3930 const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3931 return utf8_to_utf16_length(_frm, _frm_end, mx, _Maxcode_, _Mode_);
3932}
3933
3934int
Howard Hinnantc9834542011-05-31 15:34:58 +00003935__codecvt_utf8_utf16<char16_t>::do_max_length() const _NOEXCEPT
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00003936{
3937 if (_Mode_ & consume_header)
3938 return 7;
3939 return 4;
3940}
3941
3942// __codecvt_utf8_utf16<char32_t>
3943
3944__codecvt_utf8_utf16<char32_t>::result
3945__codecvt_utf8_utf16<char32_t>::do_out(state_type&,
3946 const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt,
3947 extern_type* to, extern_type* to_end, extern_type*& to_nxt) const
3948{
3949 const uint32_t* _frm = reinterpret_cast<const uint32_t*>(frm);
3950 const uint32_t* _frm_end = reinterpret_cast<const uint32_t*>(frm_end);
3951 const uint32_t* _frm_nxt = _frm;
3952 uint8_t* _to = reinterpret_cast<uint8_t*>(to);
3953 uint8_t* _to_end = reinterpret_cast<uint8_t*>(to_end);
3954 uint8_t* _to_nxt = _to;
3955 result r = utf16_to_utf8(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3956 _Maxcode_, _Mode_);
3957 frm_nxt = frm + (_frm_nxt - _frm);
3958 to_nxt = to + (_to_nxt - _to);
3959 return r;
3960}
3961
3962__codecvt_utf8_utf16<char32_t>::result
3963__codecvt_utf8_utf16<char32_t>::do_in(state_type&,
3964 const extern_type* frm, const extern_type* frm_end, const extern_type*& frm_nxt,
3965 intern_type* to, intern_type* to_end, intern_type*& to_nxt) const
3966{
3967 const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3968 const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3969 const uint8_t* _frm_nxt = _frm;
3970 uint32_t* _to = reinterpret_cast<uint32_t*>(to);
3971 uint32_t* _to_end = reinterpret_cast<uint32_t*>(to_end);
3972 uint32_t* _to_nxt = _to;
3973 result r = utf8_to_utf16(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3974 _Maxcode_, _Mode_);
3975 frm_nxt = frm + (_frm_nxt - _frm);
3976 to_nxt = to + (_to_nxt - _to);
3977 return r;
3978}
3979
3980__codecvt_utf8_utf16<char32_t>::result
3981__codecvt_utf8_utf16<char32_t>::do_unshift(state_type&,
3982 extern_type* to, extern_type*, extern_type*& to_nxt) const
3983{
3984 to_nxt = to;
3985 return noconv;
3986}
3987
3988int
Howard Hinnantc9834542011-05-31 15:34:58 +00003989__codecvt_utf8_utf16<char32_t>::do_encoding() const _NOEXCEPT
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00003990{
3991 return 0;
3992}
3993
3994bool
Howard Hinnantc9834542011-05-31 15:34:58 +00003995__codecvt_utf8_utf16<char32_t>::do_always_noconv() const _NOEXCEPT
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00003996{
3997 return false;
3998}
3999
4000int
4001__codecvt_utf8_utf16<char32_t>::do_length(state_type&,
4002 const extern_type* frm, const extern_type* frm_end, size_t mx) const
4003{
4004 const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
4005 const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
4006 return utf8_to_utf16_length(_frm, _frm_end, mx, _Maxcode_, _Mode_);
4007}
4008
4009int
Howard Hinnantc9834542011-05-31 15:34:58 +00004010__codecvt_utf8_utf16<char32_t>::do_max_length() const _NOEXCEPT
Howard Hinnant87d1a8a2010-05-30 21:39:41 +00004011{
4012 if (_Mode_ & consume_header)
4013 return 7;
4014 return 4;
4015}
4016
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004017// __narrow_to_utf8<16>
4018
4019__narrow_to_utf8<16>::~__narrow_to_utf8()
4020{
4021}
4022
4023// __narrow_to_utf8<32>
4024
4025__narrow_to_utf8<32>::~__narrow_to_utf8()
4026{
4027}
4028
4029// __widen_from_utf8<16>
4030
4031__widen_from_utf8<16>::~__widen_from_utf8()
4032{
4033}
4034
4035// __widen_from_utf8<32>
4036
4037__widen_from_utf8<32>::~__widen_from_utf8()
4038{
4039}
4040
4041// numpunct<char> && numpunct<wchar_t>
4042
4043locale::id numpunct< char >::id;
4044locale::id numpunct<wchar_t>::id;
4045
4046numpunct<char>::numpunct(size_t refs)
4047 : locale::facet(refs),
4048 __decimal_point_('.'),
4049 __thousands_sep_(',')
4050{
4051}
4052
4053numpunct<wchar_t>::numpunct(size_t refs)
4054 : locale::facet(refs),
4055 __decimal_point_(L'.'),
4056 __thousands_sep_(L',')
4057{
4058}
4059
4060numpunct<char>::~numpunct()
4061{
4062}
4063
4064numpunct<wchar_t>::~numpunct()
4065{
4066}
4067
4068 char numpunct< char >::do_decimal_point() const {return __decimal_point_;}
4069wchar_t numpunct<wchar_t>::do_decimal_point() const {return __decimal_point_;}
4070
4071 char numpunct< char >::do_thousands_sep() const {return __thousands_sep_;}
4072wchar_t numpunct<wchar_t>::do_thousands_sep() const {return __thousands_sep_;}
4073
4074string numpunct< char >::do_grouping() const {return __grouping_;}
4075string numpunct<wchar_t>::do_grouping() const {return __grouping_;}
4076
4077 string numpunct< char >::do_truename() const {return "true";}
4078wstring numpunct<wchar_t>::do_truename() const {return L"true";}
4079
4080 string numpunct< char >::do_falsename() const {return "false";}
4081wstring numpunct<wchar_t>::do_falsename() const {return L"false";}
4082
4083// numpunct_byname<char>
4084
4085numpunct_byname<char>::numpunct_byname(const char* nm, size_t refs)
4086 : numpunct<char>(refs)
4087{
4088 __init(nm);
4089}
4090
4091numpunct_byname<char>::numpunct_byname(const string& nm, size_t refs)
4092 : numpunct<char>(refs)
4093{
4094 __init(nm.c_str());
4095}
4096
4097numpunct_byname<char>::~numpunct_byname()
4098{
4099}
4100
4101void
4102numpunct_byname<char>::__init(const char* nm)
4103{
4104 if (strcmp(nm, "C") != 0)
4105 {
Sean Huntf3907e62011-07-15 05:40:33 +00004106 __locale_unique_ptr loc(newlocale(LC_ALL_MASK, nm, 0), freelocale);
Howard Hinnantd4444702010-08-11 17:04:31 +00004107#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant05b57d52012-03-07 20:37:43 +00004108 if (loc == nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004109 throw runtime_error("numpunct_byname<char>::numpunct_byname"
4110 " failed to construct for " + string(nm));
Howard Hinnant16e6e1d2010-08-22 00:03:27 +00004111#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant866569b2011-09-28 23:39:33 +00004112#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Sean Huntf3907e62011-07-15 05:40:33 +00004113 lconv* lc = localeconv_l(loc.get());
4114#else
4115 lconv* lc = __localeconv_l(loc.get());
4116#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004117 if (*lc->decimal_point)
4118 __decimal_point_ = *lc->decimal_point;
4119 if (*lc->thousands_sep)
4120 __thousands_sep_ = *lc->thousands_sep;
4121 __grouping_ = lc->grouping;
Sean Huntcb05a082011-07-07 22:45:07 +00004122 // localization for truename and falsename is not available
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004123 }
4124}
4125
4126// numpunct_byname<wchar_t>
4127
4128numpunct_byname<wchar_t>::numpunct_byname(const char* nm, size_t refs)
4129 : numpunct<wchar_t>(refs)
4130{
4131 __init(nm);
4132}
4133
4134numpunct_byname<wchar_t>::numpunct_byname(const string& nm, size_t refs)
4135 : numpunct<wchar_t>(refs)
4136{
4137 __init(nm.c_str());
4138}
4139
4140numpunct_byname<wchar_t>::~numpunct_byname()
4141{
4142}
4143
4144void
4145numpunct_byname<wchar_t>::__init(const char* nm)
4146{
4147 if (strcmp(nm, "C") != 0)
4148 {
Sean Huntf3907e62011-07-15 05:40:33 +00004149 __locale_unique_ptr loc(newlocale(LC_ALL_MASK, nm, 0), freelocale);
Howard Hinnantd4444702010-08-11 17:04:31 +00004150#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant05b57d52012-03-07 20:37:43 +00004151 if (loc == nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004152 throw runtime_error("numpunct_byname<char>::numpunct_byname"
4153 " failed to construct for " + string(nm));
Howard Hinnant16e6e1d2010-08-22 00:03:27 +00004154#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant866569b2011-09-28 23:39:33 +00004155#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Sean Huntf3907e62011-07-15 05:40:33 +00004156 lconv* lc = localeconv_l(loc.get());
4157#else
4158 lconv* lc = __localeconv_l(loc.get());
4159#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004160 if (*lc->decimal_point)
4161 __decimal_point_ = *lc->decimal_point;
4162 if (*lc->thousands_sep)
4163 __thousands_sep_ = *lc->thousands_sep;
4164 __grouping_ = lc->grouping;
4165 // locallization for truename and falsename is not available
4166 }
4167}
4168
4169// num_get helpers
4170
4171int
4172__num_get_base::__get_base(ios_base& iob)
4173{
4174 ios_base::fmtflags __basefield = iob.flags() & ios_base::basefield;
4175 if (__basefield == ios_base::oct)
4176 return 8;
4177 else if (__basefield == ios_base::hex)
4178 return 16;
4179 else if (__basefield == 0)
4180 return 0;
4181 return 10;
4182}
4183
4184const char __num_get_base::__src[33] = "0123456789abcdefABCDEFxX+-pPiInN";
4185
4186void
4187__check_grouping(const string& __grouping, unsigned* __g, unsigned* __g_end,
4188 ios_base::iostate& __err)
4189{
4190 if (__grouping.size() != 0)
4191 {
4192 reverse(__g, __g_end);
4193 const char* __ig = __grouping.data();
4194 const char* __eg = __ig + __grouping.size();
4195 for (unsigned* __r = __g; __r < __g_end-1; ++__r)
4196 {
4197 if (0 < *__ig && *__ig < numeric_limits<char>::max())
4198 {
Howard Hinnantec3773c2011-12-01 20:21:04 +00004199 if (static_cast<unsigned>(*__ig) != *__r)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004200 {
4201 __err = ios_base::failbit;
4202 return;
4203 }
4204 }
4205 if (__eg - __ig > 1)
4206 ++__ig;
4207 }
4208 if (0 < *__ig && *__ig < numeric_limits<char>::max())
4209 {
Howard Hinnantec3773c2011-12-01 20:21:04 +00004210 if (static_cast<unsigned>(*__ig) < __g_end[-1] || __g_end[-1] == 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004211 __err = ios_base::failbit;
4212 }
4213 }
4214}
4215
4216void
4217__num_put_base::__format_int(char* __fmtp, const char* __len, bool __signd,
4218 ios_base::fmtflags __flags)
4219{
4220 if (__flags & ios_base::showpos)
4221 *__fmtp++ = '+';
4222 if (__flags & ios_base::showbase)
4223 *__fmtp++ = '#';
4224 while(*__len)
4225 *__fmtp++ = *__len++;
4226 if ((__flags & ios_base::basefield) == ios_base::oct)
4227 *__fmtp = 'o';
4228 else if ((__flags & ios_base::basefield) == ios_base::hex)
4229 {
4230 if (__flags & ios_base::uppercase)
4231 *__fmtp = 'X';
4232 else
4233 *__fmtp = 'x';
4234 }
4235 else if (__signd)
4236 *__fmtp = 'd';
4237 else
4238 *__fmtp = 'u';
4239}
4240
4241bool
4242__num_put_base::__format_float(char* __fmtp, const char* __len,
4243 ios_base::fmtflags __flags)
4244{
4245 bool specify_precision = true;
4246 if (__flags & ios_base::showpos)
4247 *__fmtp++ = '+';
4248 if (__flags & ios_base::showpoint)
4249 *__fmtp++ = '#';
4250 ios_base::fmtflags floatfield = __flags & ios_base::floatfield;
4251 bool uppercase = __flags & ios_base::uppercase;
4252 if (floatfield == (ios_base::fixed | ios_base::scientific))
4253 specify_precision = false;
4254 else
4255 {
4256 *__fmtp++ = '.';
4257 *__fmtp++ = '*';
4258 }
4259 while(*__len)
4260 *__fmtp++ = *__len++;
4261 if (floatfield == ios_base::fixed)
4262 {
4263 if (uppercase)
4264 *__fmtp = 'F';
4265 else
4266 *__fmtp = 'f';
4267 }
4268 else if (floatfield == ios_base::scientific)
4269 {
4270 if (uppercase)
4271 *__fmtp = 'E';
4272 else
4273 *__fmtp = 'e';
4274 }
4275 else if (floatfield == (ios_base::fixed | ios_base::scientific))
4276 {
4277 if (uppercase)
4278 *__fmtp = 'A';
4279 else
4280 *__fmtp = 'a';
4281 }
4282 else
4283 {
4284 if (uppercase)
4285 *__fmtp = 'G';
4286 else
4287 *__fmtp = 'g';
4288 }
4289 return specify_precision;
4290}
4291
4292char*
4293__num_put_base::__identify_padding(char* __nb, char* __ne,
4294 const ios_base& __iob)
4295{
4296 switch (__iob.flags() & ios_base::adjustfield)
4297 {
4298 case ios_base::internal:
4299 if (__nb[0] == '-' || __nb[0] == '+')
4300 return __nb+1;
4301 if (__ne - __nb >= 2 && __nb[0] == '0'
4302 && (__nb[1] == 'x' || __nb[1] == 'X'))
4303 return __nb+2;
4304 break;
4305 case ios_base::left:
4306 return __ne;
4307 case ios_base::right:
4308 default:
4309 break;
4310 }
4311 return __nb;
4312}
4313
4314// time_get
4315
4316static
4317string*
4318init_weeks()
4319{
4320 static string weeks[14];
4321 weeks[0] = "Sunday";
4322 weeks[1] = "Monday";
4323 weeks[2] = "Tuesday";
4324 weeks[3] = "Wednesday";
4325 weeks[4] = "Thursday";
4326 weeks[5] = "Friday";
4327 weeks[6] = "Saturday";
4328 weeks[7] = "Sun";
4329 weeks[8] = "Mon";
4330 weeks[9] = "Tue";
4331 weeks[10] = "Wed";
4332 weeks[11] = "Thu";
4333 weeks[12] = "Fri";
4334 weeks[13] = "Sat";
4335 return weeks;
4336}
4337
4338static
4339wstring*
4340init_wweeks()
4341{
4342 static wstring weeks[14];
4343 weeks[0] = L"Sunday";
4344 weeks[1] = L"Monday";
4345 weeks[2] = L"Tuesday";
4346 weeks[3] = L"Wednesday";
4347 weeks[4] = L"Thursday";
4348 weeks[5] = L"Friday";
4349 weeks[6] = L"Saturday";
4350 weeks[7] = L"Sun";
4351 weeks[8] = L"Mon";
4352 weeks[9] = L"Tue";
4353 weeks[10] = L"Wed";
4354 weeks[11] = L"Thu";
4355 weeks[12] = L"Fri";
4356 weeks[13] = L"Sat";
4357 return weeks;
4358}
4359
4360template <>
4361const string*
4362__time_get_c_storage<char>::__weeks() const
4363{
4364 static const string* weeks = init_weeks();
4365 return weeks;
4366}
4367
4368template <>
4369const wstring*
4370__time_get_c_storage<wchar_t>::__weeks() const
4371{
4372 static const wstring* weeks = init_wweeks();
4373 return weeks;
4374}
4375
4376static
4377string*
4378init_months()
4379{
4380 static string months[24];
4381 months[0] = "January";
4382 months[1] = "February";
4383 months[2] = "March";
4384 months[3] = "April";
4385 months[4] = "May";
4386 months[5] = "June";
4387 months[6] = "July";
4388 months[7] = "August";
4389 months[8] = "September";
4390 months[9] = "October";
4391 months[10] = "November";
4392 months[11] = "December";
4393 months[12] = "Jan";
4394 months[13] = "Feb";
4395 months[14] = "Mar";
4396 months[15] = "Apr";
4397 months[16] = "May";
4398 months[17] = "Jun";
4399 months[18] = "Jul";
4400 months[19] = "Aug";
4401 months[20] = "Sep";
4402 months[21] = "Oct";
4403 months[22] = "Nov";
4404 months[23] = "Dec";
4405 return months;
4406}
4407
4408static
4409wstring*
4410init_wmonths()
4411{
4412 static wstring months[24];
4413 months[0] = L"January";
4414 months[1] = L"February";
4415 months[2] = L"March";
4416 months[3] = L"April";
4417 months[4] = L"May";
4418 months[5] = L"June";
4419 months[6] = L"July";
4420 months[7] = L"August";
4421 months[8] = L"September";
4422 months[9] = L"October";
4423 months[10] = L"November";
4424 months[11] = L"December";
4425 months[12] = L"Jan";
4426 months[13] = L"Feb";
4427 months[14] = L"Mar";
4428 months[15] = L"Apr";
4429 months[16] = L"May";
4430 months[17] = L"Jun";
4431 months[18] = L"Jul";
4432 months[19] = L"Aug";
4433 months[20] = L"Sep";
4434 months[21] = L"Oct";
4435 months[22] = L"Nov";
4436 months[23] = L"Dec";
4437 return months;
4438}
4439
4440template <>
4441const string*
4442__time_get_c_storage<char>::__months() const
4443{
4444 static const string* months = init_months();
4445 return months;
4446}
4447
4448template <>
4449const wstring*
4450__time_get_c_storage<wchar_t>::__months() const
4451{
4452 static const wstring* months = init_wmonths();
4453 return months;
4454}
4455
4456static
4457string*
4458init_am_pm()
4459{
4460 static string am_pm[24];
4461 am_pm[0] = "AM";
4462 am_pm[1] = "PM";
4463 return am_pm;
4464}
4465
4466static
4467wstring*
4468init_wam_pm()
4469{
4470 static wstring am_pm[24];
4471 am_pm[0] = L"AM";
4472 am_pm[1] = L"PM";
4473 return am_pm;
4474}
4475
4476template <>
4477const string*
4478__time_get_c_storage<char>::__am_pm() const
4479{
4480 static const string* am_pm = init_am_pm();
4481 return am_pm;
4482}
4483
4484template <>
4485const wstring*
4486__time_get_c_storage<wchar_t>::__am_pm() const
4487{
4488 static const wstring* am_pm = init_wam_pm();
4489 return am_pm;
4490}
4491
4492template <>
4493const string&
4494__time_get_c_storage<char>::__x() const
4495{
4496 static string s("%m/%d/%y");
4497 return s;
4498}
4499
4500template <>
4501const wstring&
4502__time_get_c_storage<wchar_t>::__x() const
4503{
4504 static wstring s(L"%m/%d/%y");
4505 return s;
4506}
4507
4508template <>
4509const string&
4510__time_get_c_storage<char>::__X() const
4511{
4512 static string s("%H:%M:%S");
4513 return s;
4514}
4515
4516template <>
4517const wstring&
4518__time_get_c_storage<wchar_t>::__X() const
4519{
4520 static wstring s(L"%H:%M:%S");
4521 return s;
4522}
4523
4524template <>
4525const string&
4526__time_get_c_storage<char>::__c() const
4527{
4528 static string s("%a %b %d %H:%M:%S %Y");
4529 return s;
4530}
4531
4532template <>
4533const wstring&
4534__time_get_c_storage<wchar_t>::__c() const
4535{
4536 static wstring s(L"%a %b %d %H:%M:%S %Y");
4537 return s;
4538}
4539
4540template <>
4541const string&
4542__time_get_c_storage<char>::__r() const
4543{
4544 static string s("%I:%M:%S %p");
4545 return s;
4546}
4547
4548template <>
4549const wstring&
4550__time_get_c_storage<wchar_t>::__r() const
4551{
4552 static wstring s(L"%I:%M:%S %p");
4553 return s;
4554}
4555
4556// time_get_byname
4557
4558__time_get::__time_get(const char* nm)
4559 : __loc_(newlocale(LC_ALL_MASK, nm, 0))
4560{
Howard Hinnantd4444702010-08-11 17:04:31 +00004561#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004562 if (__loc_ == 0)
4563 throw runtime_error("time_get_byname"
4564 " failed to construct for " + string(nm));
Howard Hinnant16e6e1d2010-08-22 00:03:27 +00004565#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004566}
4567
4568__time_get::__time_get(const string& nm)
4569 : __loc_(newlocale(LC_ALL_MASK, nm.c_str(), 0))
4570{
Howard Hinnantd4444702010-08-11 17:04:31 +00004571#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004572 if (__loc_ == 0)
4573 throw runtime_error("time_get_byname"
4574 " failed to construct for " + nm);
Howard Hinnant16e6e1d2010-08-22 00:03:27 +00004575#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004576}
4577
4578__time_get::~__time_get()
4579{
4580 freelocale(__loc_);
4581}
4582
Howard Hinnant335b1512012-02-20 16:51:43 +00004583#pragma clang diagnostic ignored "-Wmissing-field-initializers"
4584
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004585template <>
4586string
4587__time_get_storage<char>::__analyze(char fmt, const ctype<char>& ct)
4588{
Howard Hinnant3074a052012-02-19 14:55:32 +00004589 tm t = {0};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004590 t.tm_sec = 59;
4591 t.tm_min = 55;
4592 t.tm_hour = 23;
4593 t.tm_mday = 31;
4594 t.tm_mon = 11;
4595 t.tm_year = 161;
4596 t.tm_wday = 6;
4597 t.tm_yday = 364;
4598 t.tm_isdst = -1;
4599 char buf[100];
4600 char f[3] = {0};
4601 f[0] = '%';
4602 f[1] = fmt;
4603 size_t n = strftime_l(buf, 100, f, &t, __loc_);
4604 char* bb = buf;
4605 char* be = buf + n;
4606 string result;
4607 while (bb != be)
4608 {
4609 if (ct.is(ctype_base::space, *bb))
4610 {
4611 result.push_back(' ');
4612 for (++bb; bb != be && ct.is(ctype_base::space, *bb); ++bb)
4613 ;
4614 continue;
4615 }
4616 char* w = bb;
4617 ios_base::iostate err = ios_base::goodbit;
Howard Hinnantec3773c2011-12-01 20:21:04 +00004618 ptrdiff_t i = __scan_keyword(w, be, this->__weeks_, this->__weeks_+14,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004619 ct, err, false)
4620 - this->__weeks_;
4621 if (i < 14)
4622 {
4623 result.push_back('%');
4624 if (i < 7)
4625 result.push_back('A');
4626 else
4627 result.push_back('a');
4628 bb = w;
4629 continue;
4630 }
4631 w = bb;
4632 i = __scan_keyword(w, be, this->__months_, this->__months_+24,
4633 ct, err, false)
4634 - this->__months_;
4635 if (i < 24)
4636 {
4637 result.push_back('%');
4638 if (i < 12)
4639 result.push_back('B');
4640 else
4641 result.push_back('b');
4642 if (fmt == 'x' && ct.is(ctype_base::digit, this->__months_[i][0]))
4643 result.back() = 'm';
4644 bb = w;
4645 continue;
4646 }
4647 if (this->__am_pm_[0].size() + this->__am_pm_[1].size() > 0)
4648 {
4649 w = bb;
4650 i = __scan_keyword(w, be, this->__am_pm_, this->__am_pm_+2,
4651 ct, err, false) - this->__am_pm_;
4652 if (i < 2)
4653 {
4654 result.push_back('%');
4655 result.push_back('p');
4656 bb = w;
4657 continue;
4658 }
4659 }
4660 w = bb;
4661 if (ct.is(ctype_base::digit, *bb))
4662 {
4663 switch(__get_up_to_n_digits(bb, be, err, ct, 4))
4664 {
4665 case 6:
4666 result.push_back('%');
4667 result.push_back('w');
4668 break;
4669 case 7:
4670 result.push_back('%');
4671 result.push_back('u');
4672 break;
4673 case 11:
4674 result.push_back('%');
4675 result.push_back('I');
4676 break;
4677 case 12:
4678 result.push_back('%');
4679 result.push_back('m');
4680 break;
4681 case 23:
4682 result.push_back('%');
4683 result.push_back('H');
4684 break;
4685 case 31:
4686 result.push_back('%');
4687 result.push_back('d');
4688 break;
4689 case 55:
4690 result.push_back('%');
4691 result.push_back('M');
4692 break;
4693 case 59:
4694 result.push_back('%');
4695 result.push_back('S');
4696 break;
4697 case 61:
4698 result.push_back('%');
4699 result.push_back('y');
4700 break;
4701 case 364:
4702 result.push_back('%');
4703 result.push_back('j');
4704 break;
4705 case 2061:
4706 result.push_back('%');
4707 result.push_back('Y');
4708 break;
4709 default:
4710 for (; w != bb; ++w)
4711 result.push_back(*w);
4712 break;
4713 }
4714 continue;
4715 }
4716 if (*bb == '%')
4717 {
4718 result.push_back('%');
4719 result.push_back('%');
4720 ++bb;
4721 continue;
4722 }
4723 result.push_back(*bb);
4724 ++bb;
4725 }
4726 return result;
4727}
4728
Howard Hinnantec3773c2011-12-01 20:21:04 +00004729#pragma clang diagnostic ignored "-Wmissing-braces"
Howard Hinnantec3773c2011-12-01 20:21:04 +00004730
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004731template <>
4732wstring
4733__time_get_storage<wchar_t>::__analyze(char fmt, const ctype<wchar_t>& ct)
4734{
Howard Hinnant3074a052012-02-19 14:55:32 +00004735 tm t = {0};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004736 t.tm_sec = 59;
4737 t.tm_min = 55;
4738 t.tm_hour = 23;
4739 t.tm_mday = 31;
4740 t.tm_mon = 11;
4741 t.tm_year = 161;
4742 t.tm_wday = 6;
4743 t.tm_yday = 364;
4744 t.tm_isdst = -1;
4745 char buf[100];
4746 char f[3] = {0};
4747 f[0] = '%';
4748 f[1] = fmt;
Howard Hinnantec3773c2011-12-01 20:21:04 +00004749 strftime_l(buf, 100, f, &t, __loc_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004750 wchar_t wbuf[100];
4751 wchar_t* wbb = wbuf;
4752 mbstate_t mb = {0};
4753 const char* bb = buf;
Howard Hinnant866569b2011-09-28 23:39:33 +00004754#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Howard Hinnantec3773c2011-12-01 20:21:04 +00004755 size_t j = mbsrtowcs_l( wbb, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_);
Sean Huntf3907e62011-07-15 05:40:33 +00004756#else
Howard Hinnantec3773c2011-12-01 20:21:04 +00004757 size_t j = __mbsrtowcs_l( wbb, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_);
Sean Huntf3907e62011-07-15 05:40:33 +00004758#endif
Howard Hinnantec3773c2011-12-01 20:21:04 +00004759 if (j == size_t(-1))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004760 __throw_runtime_error("locale not supported");
Howard Hinnantec3773c2011-12-01 20:21:04 +00004761 wchar_t* wbe = wbb + j;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004762 wstring result;
4763 while (wbb != wbe)
4764 {
4765 if (ct.is(ctype_base::space, *wbb))
4766 {
4767 result.push_back(L' ');
4768 for (++wbb; wbb != wbe && ct.is(ctype_base::space, *wbb); ++wbb)
4769 ;
4770 continue;
4771 }
4772 wchar_t* w = wbb;
4773 ios_base::iostate err = ios_base::goodbit;
Howard Hinnantec3773c2011-12-01 20:21:04 +00004774 ptrdiff_t i = __scan_keyword(w, wbe, this->__weeks_, this->__weeks_+14,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004775 ct, err, false)
4776 - this->__weeks_;
4777 if (i < 14)
4778 {
4779 result.push_back(L'%');
4780 if (i < 7)
4781 result.push_back(L'A');
4782 else
4783 result.push_back(L'a');
4784 wbb = w;
4785 continue;
4786 }
4787 w = wbb;
4788 i = __scan_keyword(w, wbe, this->__months_, this->__months_+24,
4789 ct, err, false)
4790 - this->__months_;
4791 if (i < 24)
4792 {
4793 result.push_back(L'%');
4794 if (i < 12)
4795 result.push_back(L'B');
4796 else
4797 result.push_back(L'b');
4798 if (fmt == 'x' && ct.is(ctype_base::digit, this->__months_[i][0]))
4799 result.back() = L'm';
4800 wbb = w;
4801 continue;
4802 }
4803 if (this->__am_pm_[0].size() + this->__am_pm_[1].size() > 0)
4804 {
4805 w = wbb;
4806 i = __scan_keyword(w, wbe, this->__am_pm_, this->__am_pm_+2,
4807 ct, err, false) - this->__am_pm_;
4808 if (i < 2)
4809 {
4810 result.push_back(L'%');
4811 result.push_back(L'p');
4812 wbb = w;
4813 continue;
4814 }
4815 }
4816 w = wbb;
4817 if (ct.is(ctype_base::digit, *wbb))
4818 {
4819 switch(__get_up_to_n_digits(wbb, wbe, err, ct, 4))
4820 {
4821 case 6:
4822 result.push_back(L'%');
4823 result.push_back(L'w');
4824 break;
4825 case 7:
4826 result.push_back(L'%');
4827 result.push_back(L'u');
4828 break;
4829 case 11:
4830 result.push_back(L'%');
4831 result.push_back(L'I');
4832 break;
4833 case 12:
4834 result.push_back(L'%');
4835 result.push_back(L'm');
4836 break;
4837 case 23:
4838 result.push_back(L'%');
4839 result.push_back(L'H');
4840 break;
4841 case 31:
4842 result.push_back(L'%');
4843 result.push_back(L'd');
4844 break;
4845 case 55:
4846 result.push_back(L'%');
4847 result.push_back(L'M');
4848 break;
4849 case 59:
4850 result.push_back(L'%');
4851 result.push_back(L'S');
4852 break;
4853 case 61:
4854 result.push_back(L'%');
4855 result.push_back(L'y');
4856 break;
4857 case 364:
4858 result.push_back(L'%');
4859 result.push_back(L'j');
4860 break;
4861 case 2061:
4862 result.push_back(L'%');
4863 result.push_back(L'Y');
4864 break;
4865 default:
4866 for (; w != wbb; ++w)
4867 result.push_back(*w);
4868 break;
4869 }
4870 continue;
4871 }
4872 if (ct.narrow(*wbb, 0) == '%')
4873 {
4874 result.push_back(L'%');
4875 result.push_back(L'%');
4876 ++wbb;
4877 continue;
4878 }
4879 result.push_back(*wbb);
4880 ++wbb;
4881 }
4882 return result;
4883}
4884
4885template <>
4886void
4887__time_get_storage<char>::init(const ctype<char>& ct)
4888{
Howard Hinnantcd992362012-08-02 18:44:17 +00004889 tm t = {0};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004890 char buf[100];
4891 // __weeks_
4892 for (int i = 0; i < 7; ++i)
4893 {
4894 t.tm_wday = i;
4895 strftime_l(buf, 100, "%A", &t, __loc_);
4896 __weeks_[i] = buf;
4897 strftime_l(buf, 100, "%a", &t, __loc_);
4898 __weeks_[i+7] = buf;
4899 }
4900 // __months_
4901 for (int i = 0; i < 12; ++i)
4902 {
4903 t.tm_mon = i;
4904 strftime_l(buf, 100, "%B", &t, __loc_);
4905 __months_[i] = buf;
4906 strftime_l(buf, 100, "%b", &t, __loc_);
4907 __months_[i+12] = buf;
4908 }
4909 // __am_pm_
4910 t.tm_hour = 1;
4911 strftime_l(buf, 100, "%p", &t, __loc_);
4912 __am_pm_[0] = buf;
4913 t.tm_hour = 13;
4914 strftime_l(buf, 100, "%p", &t, __loc_);
4915 __am_pm_[1] = buf;
4916 __c_ = __analyze('c', ct);
4917 __r_ = __analyze('r', ct);
4918 __x_ = __analyze('x', ct);
4919 __X_ = __analyze('X', ct);
4920}
4921
4922template <>
4923void
4924__time_get_storage<wchar_t>::init(const ctype<wchar_t>& ct)
4925{
4926 tm t = {0};
4927 char buf[100];
4928 size_t be;
4929 wchar_t wbuf[100];
4930 wchar_t* wbe;
4931 mbstate_t mb = {0};
4932 // __weeks_
4933 for (int i = 0; i < 7; ++i)
4934 {
4935 t.tm_wday = i;
4936 be = strftime_l(buf, 100, "%A", &t, __loc_);
4937 mb = mbstate_t();
4938 const char* bb = buf;
Howard Hinnant866569b2011-09-28 23:39:33 +00004939#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Sean Huntf3907e62011-07-15 05:40:33 +00004940 size_t j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_);
4941#else
4942 size_t j = __mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_);
4943#endif
Howard Hinnantec3773c2011-12-01 20:21:04 +00004944 if (j == size_t(-1))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004945 __throw_runtime_error("locale not supported");
4946 wbe = wbuf + j;
4947 __weeks_[i].assign(wbuf, wbe);
4948 be = strftime_l(buf, 100, "%a", &t, __loc_);
4949 mb = mbstate_t();
4950 bb = buf;
Howard Hinnant866569b2011-09-28 23:39:33 +00004951#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Sean Huntf3907e62011-07-15 05:40:33 +00004952 j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_);
4953#else
4954 j = __mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_);
4955#endif
Howard Hinnantec3773c2011-12-01 20:21:04 +00004956 if (j == size_t(-1))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004957 __throw_runtime_error("locale not supported");
4958 wbe = wbuf + j;
4959 __weeks_[i+7].assign(wbuf, wbe);
4960 }
4961 // __months_
4962 for (int i = 0; i < 12; ++i)
4963 {
4964 t.tm_mon = i;
4965 be = strftime_l(buf, 100, "%B", &t, __loc_);
4966 mb = mbstate_t();
4967 const char* bb = buf;
Howard Hinnant866569b2011-09-28 23:39:33 +00004968#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Sean Huntf3907e62011-07-15 05:40:33 +00004969 size_t j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_);
4970#else
4971 size_t j = __mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_);
4972#endif
Howard Hinnantec3773c2011-12-01 20:21:04 +00004973 if (j == size_t(-1))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004974 __throw_runtime_error("locale not supported");
4975 wbe = wbuf + j;
4976 __months_[i].assign(wbuf, wbe);
4977 be = strftime_l(buf, 100, "%b", &t, __loc_);
4978 mb = mbstate_t();
4979 bb = buf;
Howard Hinnant866569b2011-09-28 23:39:33 +00004980#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Sean Huntf3907e62011-07-15 05:40:33 +00004981 j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_);
4982#else
4983 j = __mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_);
4984#endif
Howard Hinnantec3773c2011-12-01 20:21:04 +00004985 if (j == size_t(-1))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004986 __throw_runtime_error("locale not supported");
4987 wbe = wbuf + j;
4988 __months_[i+12].assign(wbuf, wbe);
4989 }
4990 // __am_pm_
4991 t.tm_hour = 1;
4992 be = strftime_l(buf, 100, "%p", &t, __loc_);
4993 mb = mbstate_t();
4994 const char* bb = buf;
Howard Hinnant866569b2011-09-28 23:39:33 +00004995#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Sean Huntf3907e62011-07-15 05:40:33 +00004996 size_t j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_);
4997#else
4998 size_t j = __mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_);
4999#endif
Howard Hinnantec3773c2011-12-01 20:21:04 +00005000 if (j == size_t(-1))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005001 __throw_runtime_error("locale not supported");
5002 wbe = wbuf + j;
5003 __am_pm_[0].assign(wbuf, wbe);
5004 t.tm_hour = 13;
5005 be = strftime_l(buf, 100, "%p", &t, __loc_);
5006 mb = mbstate_t();
5007 bb = buf;
Howard Hinnant866569b2011-09-28 23:39:33 +00005008#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Sean Huntf3907e62011-07-15 05:40:33 +00005009 j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_);
5010#else
5011 j = __mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_);
5012#endif
Howard Hinnantec3773c2011-12-01 20:21:04 +00005013 if (j == size_t(-1))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005014 __throw_runtime_error("locale not supported");
5015 wbe = wbuf + j;
5016 __am_pm_[1].assign(wbuf, wbe);
5017 __c_ = __analyze('c', ct);
5018 __r_ = __analyze('r', ct);
5019 __x_ = __analyze('x', ct);
5020 __X_ = __analyze('X', ct);
5021}
5022
5023template <class CharT>
5024struct _LIBCPP_HIDDEN __time_get_temp
5025 : public ctype_byname<CharT>
5026{
5027 explicit __time_get_temp(const char* nm)
5028 : ctype_byname<CharT>(nm, 1) {}
5029 explicit __time_get_temp(const string& nm)
5030 : ctype_byname<CharT>(nm, 1) {}
5031};
5032
5033template <>
5034__time_get_storage<char>::__time_get_storage(const char* __nm)
5035 : __time_get(__nm)
5036{
5037 const __time_get_temp<char> ct(__nm);
5038 init(ct);
5039}
5040
5041template <>
5042__time_get_storage<char>::__time_get_storage(const string& __nm)
5043 : __time_get(__nm)
5044{
5045 const __time_get_temp<char> ct(__nm);
5046 init(ct);
5047}
5048
5049template <>
5050__time_get_storage<wchar_t>::__time_get_storage(const char* __nm)
5051 : __time_get(__nm)
5052{
5053 const __time_get_temp<wchar_t> ct(__nm);
5054 init(ct);
5055}
5056
5057template <>
5058__time_get_storage<wchar_t>::__time_get_storage(const string& __nm)
5059 : __time_get(__nm)
5060{
5061 const __time_get_temp<wchar_t> ct(__nm);
5062 init(ct);
5063}
5064
5065template <>
5066time_base::dateorder
5067__time_get_storage<char>::__do_date_order() const
5068{
5069 unsigned i;
5070 for (i = 0; i < __x_.size(); ++i)
5071 if (__x_[i] == '%')
5072 break;
5073 ++i;
5074 switch (__x_[i])
5075 {
5076 case 'y':
5077 case 'Y':
5078 for (++i; i < __x_.size(); ++i)
5079 if (__x_[i] == '%')
5080 break;
5081 if (i == __x_.size())
5082 break;
5083 ++i;
5084 switch (__x_[i])
5085 {
5086 case 'm':
5087 for (++i; i < __x_.size(); ++i)
5088 if (__x_[i] == '%')
5089 break;
5090 if (i == __x_.size())
5091 break;
5092 ++i;
5093 if (__x_[i] == 'd')
5094 return time_base::ymd;
5095 break;
5096 case 'd':
5097 for (++i; i < __x_.size(); ++i)
5098 if (__x_[i] == '%')
5099 break;
5100 if (i == __x_.size())
5101 break;
5102 ++i;
5103 if (__x_[i] == 'm')
5104 return time_base::ydm;
5105 break;
5106 }
5107 break;
5108 case 'm':
5109 for (++i; i < __x_.size(); ++i)
5110 if (__x_[i] == '%')
5111 break;
5112 if (i == __x_.size())
5113 break;
5114 ++i;
5115 if (__x_[i] == 'd')
5116 {
5117 for (++i; i < __x_.size(); ++i)
5118 if (__x_[i] == '%')
5119 break;
5120 if (i == __x_.size())
5121 break;
5122 ++i;
5123 if (__x_[i] == 'y' || __x_[i] == 'Y')
5124 return time_base::mdy;
5125 break;
5126 }
5127 break;
5128 case 'd':
5129 for (++i; i < __x_.size(); ++i)
5130 if (__x_[i] == '%')
5131 break;
5132 if (i == __x_.size())
5133 break;
5134 ++i;
5135 if (__x_[i] == 'm')
5136 {
5137 for (++i; i < __x_.size(); ++i)
5138 if (__x_[i] == '%')
5139 break;
5140 if (i == __x_.size())
5141 break;
5142 ++i;
5143 if (__x_[i] == 'y' || __x_[i] == 'Y')
5144 return time_base::dmy;
5145 break;
5146 }
5147 break;
5148 }
5149 return time_base::no_order;
5150}
5151
5152template <>
5153time_base::dateorder
5154__time_get_storage<wchar_t>::__do_date_order() const
5155{
5156 unsigned i;
5157 for (i = 0; i < __x_.size(); ++i)
5158 if (__x_[i] == L'%')
5159 break;
5160 ++i;
5161 switch (__x_[i])
5162 {
5163 case L'y':
5164 case L'Y':
5165 for (++i; i < __x_.size(); ++i)
5166 if (__x_[i] == L'%')
5167 break;
5168 if (i == __x_.size())
5169 break;
5170 ++i;
5171 switch (__x_[i])
5172 {
5173 case L'm':
5174 for (++i; i < __x_.size(); ++i)
5175 if (__x_[i] == L'%')
5176 break;
5177 if (i == __x_.size())
5178 break;
5179 ++i;
5180 if (__x_[i] == L'd')
5181 return time_base::ymd;
5182 break;
5183 case L'd':
5184 for (++i; i < __x_.size(); ++i)
5185 if (__x_[i] == L'%')
5186 break;
5187 if (i == __x_.size())
5188 break;
5189 ++i;
5190 if (__x_[i] == L'm')
5191 return time_base::ydm;
5192 break;
5193 }
5194 break;
5195 case L'm':
5196 for (++i; i < __x_.size(); ++i)
5197 if (__x_[i] == L'%')
5198 break;
5199 if (i == __x_.size())
5200 break;
5201 ++i;
5202 if (__x_[i] == L'd')
5203 {
5204 for (++i; i < __x_.size(); ++i)
5205 if (__x_[i] == L'%')
5206 break;
5207 if (i == __x_.size())
5208 break;
5209 ++i;
5210 if (__x_[i] == L'y' || __x_[i] == L'Y')
5211 return time_base::mdy;
5212 break;
5213 }
5214 break;
5215 case L'd':
5216 for (++i; i < __x_.size(); ++i)
5217 if (__x_[i] == L'%')
5218 break;
5219 if (i == __x_.size())
5220 break;
5221 ++i;
5222 if (__x_[i] == L'm')
5223 {
5224 for (++i; i < __x_.size(); ++i)
5225 if (__x_[i] == L'%')
5226 break;
5227 if (i == __x_.size())
5228 break;
5229 ++i;
5230 if (__x_[i] == L'y' || __x_[i] == L'Y')
5231 return time_base::dmy;
5232 break;
5233 }
5234 break;
5235 }
5236 return time_base::no_order;
5237}
5238
5239// time_put
5240
5241__time_put::__time_put(const char* nm)
5242 : __loc_(newlocale(LC_ALL_MASK, nm, 0))
5243{
Howard Hinnantd4444702010-08-11 17:04:31 +00005244#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005245 if (__loc_ == 0)
5246 throw runtime_error("time_put_byname"
5247 " failed to construct for " + string(nm));
Howard Hinnant16e6e1d2010-08-22 00:03:27 +00005248#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005249}
5250
5251__time_put::__time_put(const string& nm)
5252 : __loc_(newlocale(LC_ALL_MASK, nm.c_str(), 0))
5253{
Howard Hinnantd4444702010-08-11 17:04:31 +00005254#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005255 if (__loc_ == 0)
5256 throw runtime_error("time_put_byname"
5257 " failed to construct for " + nm);
Howard Hinnant16e6e1d2010-08-22 00:03:27 +00005258#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005259}
5260
5261__time_put::~__time_put()
5262{
5263 if (__loc_)
5264 freelocale(__loc_);
5265}
5266
5267void
5268__time_put::__do_put(char* __nb, char*& __ne, const tm* __tm,
5269 char __fmt, char __mod) const
5270{
5271 char fmt[] = {'%', __fmt, __mod, 0};
5272 if (__mod != 0)
5273 swap(fmt[1], fmt[2]);
Howard Hinnantec3773c2011-12-01 20:21:04 +00005274 size_t n = strftime_l(__nb, static_cast<size_t>(__ne-__nb), fmt, __tm, __loc_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005275 __ne = __nb + n;
5276}
5277
5278void
5279__time_put::__do_put(wchar_t* __wb, wchar_t*& __we, const tm* __tm,
5280 char __fmt, char __mod) const
5281{
5282 char __nar[100];
5283 char* __ne = __nar + 100;
5284 __do_put(__nar, __ne, __tm, __fmt, __mod);
5285 mbstate_t mb = {0};
5286 const char* __nb = __nar;
Howard Hinnant866569b2011-09-28 23:39:33 +00005287#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Sean Huntf3907e62011-07-15 05:40:33 +00005288 size_t j = mbsrtowcs_l(__wb, &__nb, 100, &mb, __loc_);
5289#else
5290 size_t j = __mbsrtowcs_l(__wb, &__nb, 100, &mb, __loc_);
5291#endif
Howard Hinnantec3773c2011-12-01 20:21:04 +00005292 if (j == size_t(-1))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005293 __throw_runtime_error("locale not supported");
5294 __we = __wb + j;
5295}
5296
5297// moneypunct_byname
5298
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005299template <class charT>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005300static
5301void
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005302__init_pat(money_base::pattern& pat, basic_string<charT>& __curr_symbol_,
5303 bool intl, char cs_precedes, char sep_by_space, char sign_posn,
5304 charT space_char)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005305{
5306 const char sign = static_cast<char>(money_base::sign);
5307 const char space = static_cast<char>(money_base::space);
5308 const char none = static_cast<char>(money_base::none);
5309 const char symbol = static_cast<char>(money_base::symbol);
5310 const char value = static_cast<char>(money_base::value);
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005311 const bool symbol_contains_sep = intl && __curr_symbol_.size() == 4;
5312
5313 // Comments on case branches reflect 'C11 7.11.2.1 The localeconv
5314 // function'. "Space between sign and symbol or value" means that
5315 // if the sign is adjacent to the symbol, there's a space between
5316 // them, and otherwise there's a space between the sign and value.
5317 //
5318 // C11's localeconv specifies that the fourth character of an
5319 // international curr_symbol is used to separate the sign and
5320 // value when sep_by_space says to do so. C++ can't represent
5321 // that, so we just use a space. When sep_by_space says to
5322 // separate the symbol and value-or-sign with a space, we rearrange the
5323 // curr_symbol to put its spacing character on the correct side of
5324 // the symbol.
5325 //
5326 // We also need to avoid adding an extra space between the sign
5327 // and value when the currency symbol is suppressed (by not
5328 // setting showbase). We match glibc's strfmon by interpreting
5329 // sep_by_space==1 as "omit the space when the currency symbol is
5330 // absent".
5331 //
5332 // Users who want to get this right should use ICU instead.
5333
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005334 switch (cs_precedes)
5335 {
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005336 case 0: // value before curr_symbol
5337 if (symbol_contains_sep) {
5338 // Move the separator to before the symbol, to place it
5339 // between the value and symbol.
5340 rotate(__curr_symbol_.begin(), __curr_symbol_.begin() + 3,
5341 __curr_symbol_.end());
5342 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005343 switch (sign_posn)
5344 {
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005345 case 0: // Parentheses surround the quantity and currency symbol.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005346 pat.field[0] = sign;
5347 pat.field[1] = value;
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005348 pat.field[2] = none; // Any space appears in the symbol.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005349 pat.field[3] = symbol;
5350 switch (sep_by_space)
5351 {
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005352 case 0: // No space separates the currency symbol and value.
5353 // This case may have changed between C99 and C11;
5354 // assume the currency symbol matches the intention.
5355 case 2: // Space between sign and currency or value.
5356 // The "sign" is two parentheses, so no space here either.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005357 return;
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005358 case 1: // Space between currency-and-sign or currency and value.
5359 if (!symbol_contains_sep) {
5360 // We insert the space into the symbol instead of
5361 // setting pat.field[2]=space so that when
5362 // showbase is not set, the space goes away too.
5363 __curr_symbol_.insert(0, 1, space_char);
5364 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005365 return;
5366 default:
5367 break;
5368 }
5369 break;
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005370 case 1: // The sign string precedes the quantity and currency symbol.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005371 pat.field[0] = sign;
5372 pat.field[3] = symbol;
5373 switch (sep_by_space)
5374 {
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005375 case 0: // No space separates the currency symbol and value.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005376 pat.field[1] = value;
5377 pat.field[2] = none;
5378 return;
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005379 case 1: // Space between currency-and-sign or currency and value.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005380 pat.field[1] = value;
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005381 pat.field[2] = none;
5382 if (!symbol_contains_sep) {
5383 // We insert the space into the symbol instead of
5384 // setting pat.field[2]=space so that when
5385 // showbase is not set, the space goes away too.
5386 __curr_symbol_.insert(0, 1, space_char);
5387 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005388 return;
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005389 case 2: // Space between sign and currency or value.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005390 pat.field[1] = space;
5391 pat.field[2] = value;
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005392 if (symbol_contains_sep) {
5393 // Remove the separator from the symbol, since it
5394 // has already appeared after the sign.
5395 __curr_symbol_.erase(__curr_symbol_.begin());
5396 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005397 return;
5398 default:
5399 break;
5400 }
5401 break;
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005402 case 2: // The sign string succeeds the quantity and currency symbol.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005403 pat.field[0] = value;
5404 pat.field[3] = sign;
5405 switch (sep_by_space)
5406 {
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005407 case 0: // No space separates the currency symbol and value.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005408 pat.field[1] = none;
5409 pat.field[2] = symbol;
5410 return;
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005411 case 1: // Space between currency-and-sign or currency and value.
5412 if (!symbol_contains_sep) {
5413 // We insert the space into the symbol instead of
5414 // setting pat.field[1]=space so that when
5415 // showbase is not set, the space goes away too.
5416 __curr_symbol_.insert(0, 1, space_char);
5417 }
5418 pat.field[1] = none;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005419 pat.field[2] = symbol;
5420 return;
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005421 case 2: // Space between sign and currency or value.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005422 pat.field[1] = symbol;
5423 pat.field[2] = space;
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005424 if (symbol_contains_sep) {
5425 // Remove the separator from the symbol, since it
5426 // should not be removed if showbase is absent.
5427 __curr_symbol_.erase(__curr_symbol_.begin());
5428 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005429 return;
5430 default:
5431 break;
5432 }
5433 break;
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005434 case 3: // The sign string immediately precedes the currency symbol.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005435 pat.field[0] = value;
5436 pat.field[3] = symbol;
5437 switch (sep_by_space)
5438 {
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005439 case 0: // No space separates the currency symbol and value.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005440 pat.field[1] = none;
5441 pat.field[2] = sign;
5442 return;
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005443 case 1: // Space between currency-and-sign or currency and value.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005444 pat.field[1] = space;
5445 pat.field[2] = sign;
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005446 if (symbol_contains_sep) {
5447 // Remove the separator from the symbol, since it
5448 // has already appeared before the sign.
5449 __curr_symbol_.erase(__curr_symbol_.begin());
5450 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005451 return;
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005452 case 2: // Space between sign and currency or value.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005453 pat.field[1] = sign;
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005454 pat.field[2] = none;
5455 if (!symbol_contains_sep) {
5456 // We insert the space into the symbol instead of
5457 // setting pat.field[2]=space so that when
5458 // showbase is not set, the space goes away too.
5459 __curr_symbol_.insert(0, 1, space_char);
5460 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005461 return;
5462 default:
5463 break;
5464 }
5465 break;
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005466 case 4: // The sign string immediately succeeds the currency symbol.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005467 pat.field[0] = value;
5468 pat.field[3] = sign;
5469 switch (sep_by_space)
5470 {
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005471 case 0: // No space separates the currency symbol and value.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005472 pat.field[1] = none;
5473 pat.field[2] = symbol;
5474 return;
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005475 case 1: // Space between currency-and-sign or currency and value.
5476 pat.field[1] = none;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005477 pat.field[2] = symbol;
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005478 if (!symbol_contains_sep) {
5479 // We insert the space into the symbol instead of
5480 // setting pat.field[1]=space so that when
5481 // showbase is not set, the space goes away too.
5482 __curr_symbol_.insert(0, 1, space_char);
5483 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005484 return;
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005485 case 2: // Space between sign and currency or value.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005486 pat.field[1] = symbol;
5487 pat.field[2] = space;
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005488 if (symbol_contains_sep) {
5489 // Remove the separator from the symbol, since it
5490 // should not disappear when showbase is absent.
5491 __curr_symbol_.erase(__curr_symbol_.begin());
5492 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005493 return;
5494 default:
5495 break;
5496 }
5497 break;
5498 default:
5499 break;
5500 }
5501 break;
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005502 case 1: // curr_symbol before value
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005503 switch (sign_posn)
5504 {
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005505 case 0: // Parentheses surround the quantity and currency symbol.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005506 pat.field[0] = sign;
5507 pat.field[1] = symbol;
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005508 pat.field[2] = none; // Any space appears in the symbol.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005509 pat.field[3] = value;
5510 switch (sep_by_space)
5511 {
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005512 case 0: // No space separates the currency symbol and value.
5513 // This case may have changed between C99 and C11;
5514 // assume the currency symbol matches the intention.
5515 case 2: // Space between sign and currency or value.
5516 // The "sign" is two parentheses, so no space here either.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005517 return;
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005518 case 1: // Space between currency-and-sign or currency and value.
5519 if (!symbol_contains_sep) {
5520 // We insert the space into the symbol instead of
5521 // setting pat.field[2]=space so that when
5522 // showbase is not set, the space goes away too.
5523 __curr_symbol_.insert(0, 1, space_char);
5524 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005525 return;
5526 default:
5527 break;
5528 }
5529 break;
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005530 case 1: // The sign string precedes the quantity and currency symbol.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005531 pat.field[0] = sign;
5532 pat.field[3] = value;
5533 switch (sep_by_space)
5534 {
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005535 case 0: // No space separates the currency symbol and value.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005536 pat.field[1] = symbol;
5537 pat.field[2] = none;
5538 return;
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005539 case 1: // Space between currency-and-sign or currency and value.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005540 pat.field[1] = symbol;
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005541 pat.field[2] = none;
5542 if (!symbol_contains_sep) {
5543 // We insert the space into the symbol instead of
5544 // setting pat.field[2]=space so that when
5545 // showbase is not set, the space goes away too.
5546 __curr_symbol_.push_back(space_char);
5547 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005548 return;
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005549 case 2: // Space between sign and currency or value.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005550 pat.field[1] = space;
5551 pat.field[2] = symbol;
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005552 if (symbol_contains_sep) {
5553 // Remove the separator from the symbol, since it
5554 // has already appeared after the sign.
5555 __curr_symbol_.pop_back();
5556 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005557 return;
5558 default:
5559 break;
5560 }
5561 break;
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005562 case 2: // The sign string succeeds the quantity and currency symbol.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005563 pat.field[0] = symbol;
5564 pat.field[3] = sign;
5565 switch (sep_by_space)
5566 {
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005567 case 0: // No space separates the currency symbol and value.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005568 pat.field[1] = none;
5569 pat.field[2] = value;
5570 return;
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005571 case 1: // Space between currency-and-sign or currency and value.
5572 pat.field[1] = none;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005573 pat.field[2] = value;
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005574 if (!symbol_contains_sep) {
5575 // We insert the space into the symbol instead of
5576 // setting pat.field[1]=space so that when
5577 // showbase is not set, the space goes away too.
5578 __curr_symbol_.push_back(space_char);
5579 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005580 return;
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005581 case 2: // Space between sign and currency or value.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005582 pat.field[1] = value;
5583 pat.field[2] = space;
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005584 if (symbol_contains_sep) {
5585 // Remove the separator from the symbol, since it
5586 // will appear before the sign.
5587 __curr_symbol_.pop_back();
5588 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005589 return;
5590 default:
5591 break;
5592 }
5593 break;
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005594 case 3: // The sign string immediately precedes the currency symbol.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005595 pat.field[0] = sign;
5596 pat.field[3] = value;
5597 switch (sep_by_space)
5598 {
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005599 case 0: // No space separates the currency symbol and value.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005600 pat.field[1] = symbol;
5601 pat.field[2] = none;
5602 return;
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005603 case 1: // Space between currency-and-sign or currency and value.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005604 pat.field[1] = symbol;
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005605 pat.field[2] = none;
5606 if (!symbol_contains_sep) {
5607 // We insert the space into the symbol instead of
5608 // setting pat.field[2]=space so that when
5609 // showbase is not set, the space goes away too.
5610 __curr_symbol_.push_back(space_char);
5611 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005612 return;
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005613 case 2: // Space between sign and currency or value.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005614 pat.field[1] = space;
5615 pat.field[2] = symbol;
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005616 if (symbol_contains_sep) {
5617 // Remove the separator from the symbol, since it
5618 // has already appeared after the sign.
5619 __curr_symbol_.pop_back();
5620 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005621 return;
5622 default:
5623 break;
5624 }
5625 break;
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005626 case 4: // The sign string immediately succeeds the currency symbol.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005627 pat.field[0] = symbol;
5628 pat.field[3] = value;
5629 switch (sep_by_space)
5630 {
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005631 case 0: // No space separates the currency symbol and value.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005632 pat.field[1] = sign;
5633 pat.field[2] = none;
5634 return;
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005635 case 1: // Space between currency-and-sign or currency and value.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005636 pat.field[1] = sign;
5637 pat.field[2] = space;
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005638 if (symbol_contains_sep) {
5639 // Remove the separator from the symbol, since it
5640 // should not disappear when showbase is absent.
5641 __curr_symbol_.pop_back();
5642 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005643 return;
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005644 case 2: // Space between sign and currency or value.
5645 pat.field[1] = none;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005646 pat.field[2] = sign;
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005647 if (!symbol_contains_sep) {
5648 // We insert the space into the symbol instead of
5649 // setting pat.field[1]=space so that when
5650 // showbase is not set, the space goes away too.
5651 __curr_symbol_.push_back(space_char);
5652 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005653 return;
5654 default:
5655 break;
5656 }
5657 break;
5658 default:
5659 break;
5660 }
5661 break;
5662 default:
5663 break;
5664 }
5665 pat.field[0] = symbol;
5666 pat.field[1] = sign;
5667 pat.field[2] = none;
5668 pat.field[3] = value;
5669}
5670
5671template<>
5672void
5673moneypunct_byname<char, false>::init(const char* nm)
5674{
5675 typedef moneypunct<char, false> base;
Sean Huntf3907e62011-07-15 05:40:33 +00005676 __locale_unique_ptr loc(newlocale(LC_ALL_MASK, nm, 0), freelocale);
Howard Hinnantd4444702010-08-11 17:04:31 +00005677#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant05b57d52012-03-07 20:37:43 +00005678 if (loc == nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005679 throw runtime_error("moneypunct_byname"
5680 " failed to construct for " + string(nm));
Howard Hinnant16e6e1d2010-08-22 00:03:27 +00005681#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant866569b2011-09-28 23:39:33 +00005682#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Sean Huntf3907e62011-07-15 05:40:33 +00005683 lconv* lc = localeconv_l(loc.get());
5684#else
5685 lconv* lc = __localeconv_l(loc.get());
5686#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005687 if (*lc->mon_decimal_point)
5688 __decimal_point_ = *lc->mon_decimal_point;
5689 else
5690 __decimal_point_ = base::do_decimal_point();
5691 if (*lc->mon_thousands_sep)
5692 __thousands_sep_ = *lc->mon_thousands_sep;
5693 else
5694 __thousands_sep_ = base::do_thousands_sep();
5695 __grouping_ = lc->mon_grouping;
5696 __curr_symbol_ = lc->currency_symbol;
5697 if (lc->frac_digits != CHAR_MAX)
5698 __frac_digits_ = lc->frac_digits;
5699 else
5700 __frac_digits_ = base::do_frac_digits();
5701 if (lc->p_sign_posn == 0)
5702 __positive_sign_ = "()";
5703 else
5704 __positive_sign_ = lc->positive_sign;
5705 if (lc->n_sign_posn == 0)
5706 __negative_sign_ = "()";
5707 else
5708 __negative_sign_ = lc->negative_sign;
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005709 // Assume the positive and negative formats will want spaces in
5710 // the same places in curr_symbol since there's no way to
5711 // represent anything else.
5712 string_type __dummy_curr_symbol = __curr_symbol_;
5713 __init_pat(__pos_format_, __dummy_curr_symbol, false,
5714 lc->p_cs_precedes, lc->p_sep_by_space, lc->p_sign_posn, ' ');
5715 __init_pat(__neg_format_, __curr_symbol_, false,
5716 lc->n_cs_precedes, lc->n_sep_by_space, lc->n_sign_posn, ' ');
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005717}
5718
5719template<>
5720void
5721moneypunct_byname<char, true>::init(const char* nm)
5722{
5723 typedef moneypunct<char, true> base;
Sean Huntf3907e62011-07-15 05:40:33 +00005724 __locale_unique_ptr loc(newlocale(LC_ALL_MASK, nm, 0), freelocale);
Howard Hinnantd4444702010-08-11 17:04:31 +00005725#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant05b57d52012-03-07 20:37:43 +00005726 if (loc == nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005727 throw runtime_error("moneypunct_byname"
5728 " failed to construct for " + string(nm));
Howard Hinnant16e6e1d2010-08-22 00:03:27 +00005729#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant866569b2011-09-28 23:39:33 +00005730#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Sean Huntf3907e62011-07-15 05:40:33 +00005731 lconv* lc = localeconv_l(loc.get());
5732#else
5733 lconv* lc = __localeconv_l(loc.get());
5734#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005735 if (*lc->mon_decimal_point)
5736 __decimal_point_ = *lc->mon_decimal_point;
5737 else
5738 __decimal_point_ = base::do_decimal_point();
5739 if (*lc->mon_thousands_sep)
5740 __thousands_sep_ = *lc->mon_thousands_sep;
5741 else
5742 __thousands_sep_ = base::do_thousands_sep();
5743 __grouping_ = lc->mon_grouping;
5744 __curr_symbol_ = lc->int_curr_symbol;
5745 if (lc->int_frac_digits != CHAR_MAX)
5746 __frac_digits_ = lc->int_frac_digits;
5747 else
5748 __frac_digits_ = base::do_frac_digits();
Howard Hinnant6cd05ee2011-09-23 16:11:27 +00005749#if _WIN32
5750 if (lc->p_sign_posn == 0)
5751#else // _WIN32
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005752 if (lc->int_p_sign_posn == 0)
Howard Hinnant6cd05ee2011-09-23 16:11:27 +00005753#endif //_WIN32
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005754 __positive_sign_ = "()";
5755 else
5756 __positive_sign_ = lc->positive_sign;
Howard Hinnant6cd05ee2011-09-23 16:11:27 +00005757#if _WIN32
5758 if(lc->n_sign_posn == 0)
5759#else // _WIN32
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005760 if (lc->int_n_sign_posn == 0)
Howard Hinnant6cd05ee2011-09-23 16:11:27 +00005761#endif // _WIN32
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005762 __negative_sign_ = "()";
5763 else
5764 __negative_sign_ = lc->negative_sign;
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005765 // Assume the positive and negative formats will want spaces in
5766 // the same places in curr_symbol since there's no way to
5767 // represent anything else.
5768 string_type __dummy_curr_symbol = __curr_symbol_;
Howard Hinnant6cd05ee2011-09-23 16:11:27 +00005769#if _WIN32
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005770 __init_pat(__pos_format_, __dummy_curr_symbol, true,
5771 lc->p_cs_precedes, lc->p_sep_by_space, lc->p_sign_posn, ' ');
5772 __init_pat(__neg_format_, __curr_symbol_, true,
5773 lc->n_cs_precedes, lc->n_sep_by_space, lc->n_sign_posn, ' ');
Howard Hinnant6cd05ee2011-09-23 16:11:27 +00005774#else
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005775 __init_pat(__pos_format_, __dummy_curr_symbol, true,
5776 lc->int_p_cs_precedes, lc->int_p_sep_by_space,
5777 lc->int_p_sign_posn, ' ');
5778 __init_pat(__neg_format_, __curr_symbol_, true,
5779 lc->int_n_cs_precedes, lc->int_n_sep_by_space,
5780 lc->int_n_sign_posn, ' ');
Howard Hinnant6cd05ee2011-09-23 16:11:27 +00005781#endif // _WIN32
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005782}
5783
5784template<>
5785void
5786moneypunct_byname<wchar_t, false>::init(const char* nm)
5787{
5788 typedef moneypunct<wchar_t, false> base;
Sean Huntf3907e62011-07-15 05:40:33 +00005789 __locale_unique_ptr loc(newlocale(LC_ALL_MASK, nm, 0), freelocale);
Howard Hinnantd4444702010-08-11 17:04:31 +00005790#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant05b57d52012-03-07 20:37:43 +00005791 if (loc == nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005792 throw runtime_error("moneypunct_byname"
5793 " failed to construct for " + string(nm));
Howard Hinnant16e6e1d2010-08-22 00:03:27 +00005794#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant866569b2011-09-28 23:39:33 +00005795#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Sean Huntf3907e62011-07-15 05:40:33 +00005796 lconv* lc = localeconv_l(loc.get());
5797#else
5798 lconv* lc = __localeconv_l(loc.get());
5799#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005800 if (*lc->mon_decimal_point)
5801 __decimal_point_ = static_cast<wchar_t>(*lc->mon_decimal_point);
5802 else
5803 __decimal_point_ = base::do_decimal_point();
5804 if (*lc->mon_thousands_sep)
5805 __thousands_sep_ = static_cast<wchar_t>(*lc->mon_thousands_sep);
5806 else
5807 __thousands_sep_ = base::do_thousands_sep();
5808 __grouping_ = lc->mon_grouping;
5809 wchar_t wbuf[100];
5810 mbstate_t mb = {0};
5811 const char* bb = lc->currency_symbol;
Howard Hinnant866569b2011-09-28 23:39:33 +00005812#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Sean Huntf3907e62011-07-15 05:40:33 +00005813 size_t j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get());
5814#else
5815 size_t j = __mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get());
5816#endif
Howard Hinnantec3773c2011-12-01 20:21:04 +00005817 if (j == size_t(-1))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005818 __throw_runtime_error("locale not supported");
5819 wchar_t* wbe = wbuf + j;
5820 __curr_symbol_.assign(wbuf, wbe);
5821 if (lc->frac_digits != CHAR_MAX)
5822 __frac_digits_ = lc->frac_digits;
5823 else
5824 __frac_digits_ = base::do_frac_digits();
5825 if (lc->p_sign_posn == 0)
5826 __positive_sign_ = L"()";
5827 else
5828 {
5829 mb = mbstate_t();
5830 bb = lc->positive_sign;
Howard Hinnant866569b2011-09-28 23:39:33 +00005831#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Sean Huntf3907e62011-07-15 05:40:33 +00005832 j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get());
5833#else
5834 j = __mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get());
5835#endif
Howard Hinnantec3773c2011-12-01 20:21:04 +00005836 if (j == size_t(-1))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005837 __throw_runtime_error("locale not supported");
5838 wbe = wbuf + j;
5839 __positive_sign_.assign(wbuf, wbe);
5840 }
5841 if (lc->n_sign_posn == 0)
5842 __negative_sign_ = L"()";
5843 else
5844 {
5845 mb = mbstate_t();
5846 bb = lc->negative_sign;
Howard Hinnant866569b2011-09-28 23:39:33 +00005847#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Sean Huntf3907e62011-07-15 05:40:33 +00005848 j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get());
5849#else
5850 j = __mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get());
5851#endif
Howard Hinnantec3773c2011-12-01 20:21:04 +00005852 if (j == size_t(-1))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005853 __throw_runtime_error("locale not supported");
5854 wbe = wbuf + j;
5855 __negative_sign_.assign(wbuf, wbe);
5856 }
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005857 // Assume the positive and negative formats will want spaces in
5858 // the same places in curr_symbol since there's no way to
5859 // represent anything else.
5860 string_type __dummy_curr_symbol = __curr_symbol_;
5861 __init_pat(__pos_format_, __dummy_curr_symbol, false,
5862 lc->p_cs_precedes, lc->p_sep_by_space, lc->p_sign_posn, L' ');
5863 __init_pat(__neg_format_, __curr_symbol_, false,
5864 lc->n_cs_precedes, lc->n_sep_by_space, lc->n_sign_posn, L' ');
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005865}
5866
5867template<>
5868void
5869moneypunct_byname<wchar_t, true>::init(const char* nm)
5870{
5871 typedef moneypunct<wchar_t, true> base;
Sean Huntf3907e62011-07-15 05:40:33 +00005872 __locale_unique_ptr loc(newlocale(LC_ALL_MASK, nm, 0), freelocale);
Howard Hinnantd4444702010-08-11 17:04:31 +00005873#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant05b57d52012-03-07 20:37:43 +00005874 if (loc == nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005875 throw runtime_error("moneypunct_byname"
5876 " failed to construct for " + string(nm));
Howard Hinnant16e6e1d2010-08-22 00:03:27 +00005877#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant866569b2011-09-28 23:39:33 +00005878#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Sean Huntf3907e62011-07-15 05:40:33 +00005879 lconv* lc = localeconv_l(loc.get());
5880#else
5881 lconv* lc = __localeconv_l(loc.get());
5882#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005883 if (*lc->mon_decimal_point)
5884 __decimal_point_ = static_cast<wchar_t>(*lc->mon_decimal_point);
5885 else
5886 __decimal_point_ = base::do_decimal_point();
5887 if (*lc->mon_thousands_sep)
5888 __thousands_sep_ = static_cast<wchar_t>(*lc->mon_thousands_sep);
5889 else
5890 __thousands_sep_ = base::do_thousands_sep();
5891 __grouping_ = lc->mon_grouping;
5892 wchar_t wbuf[100];
5893 mbstate_t mb = {0};
5894 const char* bb = lc->int_curr_symbol;
Howard Hinnant866569b2011-09-28 23:39:33 +00005895#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Sean Huntf3907e62011-07-15 05:40:33 +00005896 size_t j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get());
5897#else
5898 size_t j = __mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get());
5899#endif
Howard Hinnantec3773c2011-12-01 20:21:04 +00005900 if (j == size_t(-1))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005901 __throw_runtime_error("locale not supported");
5902 wchar_t* wbe = wbuf + j;
5903 __curr_symbol_.assign(wbuf, wbe);
5904 if (lc->int_frac_digits != CHAR_MAX)
5905 __frac_digits_ = lc->int_frac_digits;
5906 else
5907 __frac_digits_ = base::do_frac_digits();
Howard Hinnant6cd05ee2011-09-23 16:11:27 +00005908#if _WIN32
5909 if (lc->p_sign_posn == 0)
5910#else // _WIN32
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005911 if (lc->int_p_sign_posn == 0)
Howard Hinnant6cd05ee2011-09-23 16:11:27 +00005912#endif // _WIN32
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005913 __positive_sign_ = L"()";
5914 else
5915 {
5916 mb = mbstate_t();
5917 bb = lc->positive_sign;
Howard Hinnant866569b2011-09-28 23:39:33 +00005918#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Sean Huntf3907e62011-07-15 05:40:33 +00005919 j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get());
5920#else
5921 j = __mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get());
5922#endif
Howard Hinnantec3773c2011-12-01 20:21:04 +00005923 if (j == size_t(-1))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005924 __throw_runtime_error("locale not supported");
5925 wbe = wbuf + j;
5926 __positive_sign_.assign(wbuf, wbe);
5927 }
Howard Hinnant6cd05ee2011-09-23 16:11:27 +00005928#if _WIN32
5929 if (lc->n_sign_posn == 0)
5930#else // _WIN32
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005931 if (lc->int_n_sign_posn == 0)
Howard Hinnant6cd05ee2011-09-23 16:11:27 +00005932#endif // _WIN32
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005933 __negative_sign_ = L"()";
5934 else
5935 {
5936 mb = mbstate_t();
5937 bb = lc->negative_sign;
Howard Hinnant866569b2011-09-28 23:39:33 +00005938#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
Sean Huntf3907e62011-07-15 05:40:33 +00005939 j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get());
5940#else
5941 j = __mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get());
5942#endif
Howard Hinnantec3773c2011-12-01 20:21:04 +00005943 if (j == size_t(-1))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005944 __throw_runtime_error("locale not supported");
5945 wbe = wbuf + j;
5946 __negative_sign_.assign(wbuf, wbe);
5947 }
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005948 // Assume the positive and negative formats will want spaces in
5949 // the same places in curr_symbol since there's no way to
5950 // represent anything else.
5951 string_type __dummy_curr_symbol = __curr_symbol_;
Howard Hinnant6cd05ee2011-09-23 16:11:27 +00005952#if _WIN32
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005953 __init_pat(__pos_format_, __dummy_curr_symbol, true,
5954 lc->p_cs_precedes, lc->p_sep_by_space, lc->p_sign_posn, L' ');
5955 __init_pat(__neg_format_, __curr_symbol_, true,
5956 lc->n_cs_precedes, lc->n_sep_by_space, lc->n_sign_posn, L' ');
Howard Hinnant6cd05ee2011-09-23 16:11:27 +00005957#else // _WIN32
Jeffrey Yasskin558ae172012-03-10 18:31:43 +00005958 __init_pat(__pos_format_, __dummy_curr_symbol, true,
5959 lc->int_p_cs_precedes, lc->int_p_sep_by_space,
5960 lc->int_p_sign_posn, L' ');
5961 __init_pat(__neg_format_, __curr_symbol_, true,
5962 lc->int_n_cs_precedes, lc->int_n_sep_by_space,
5963 lc->int_n_sign_posn, L' ');
Howard Hinnant6cd05ee2011-09-23 16:11:27 +00005964#endif // _WIN32
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005965}
5966
5967void __do_nothing(void*) {}
5968
5969void __throw_runtime_error(const char* msg)
5970{
Howard Hinnantd4444702010-08-11 17:04:31 +00005971#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005972 throw runtime_error(msg);
Howard Hinnantd4444702010-08-11 17:04:31 +00005973#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005974}
5975
5976template class collate<char>;
5977template class collate<wchar_t>;
5978
5979template class num_get<char>;
5980template class num_get<wchar_t>;
5981
Howard Hinnantec3773c2011-12-01 20:21:04 +00005982template struct __num_get<char>;
5983template struct __num_get<wchar_t>;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005984
5985template class num_put<char>;
5986template class num_put<wchar_t>;
5987
Howard Hinnantec3773c2011-12-01 20:21:04 +00005988template struct __num_put<char>;
5989template struct __num_put<wchar_t>;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005990
5991template class time_get<char>;
5992template class time_get<wchar_t>;
5993
5994template class time_get_byname<char>;
5995template class time_get_byname<wchar_t>;
5996
5997template class time_put<char>;
5998template class time_put<wchar_t>;
5999
6000template class time_put_byname<char>;
6001template class time_put_byname<wchar_t>;
6002
6003template class moneypunct<char, false>;
6004template class moneypunct<char, true>;
6005template class moneypunct<wchar_t, false>;
6006template class moneypunct<wchar_t, true>;
6007
6008template class moneypunct_byname<char, false>;
6009template class moneypunct_byname<char, true>;
6010template class moneypunct_byname<wchar_t, false>;
6011template class moneypunct_byname<wchar_t, true>;
6012
6013template class money_get<char>;
6014template class money_get<wchar_t>;
6015
6016template class __money_get<char>;
6017template class __money_get<wchar_t>;
6018
6019template class money_put<char>;
6020template class money_put<wchar_t>;
6021
6022template class __money_put<char>;
6023template class __money_put<wchar_t>;
6024
6025template class messages<char>;
6026template class messages<wchar_t>;
6027
6028template class messages_byname<char>;
6029template class messages_byname<wchar_t>;
6030
6031template class codecvt_byname<char, char, mbstate_t>;
6032template class codecvt_byname<wchar_t, char, mbstate_t>;
6033template class codecvt_byname<char16_t, char, mbstate_t>;
6034template class codecvt_byname<char32_t, char, mbstate_t>;
6035
6036template class __vector_base_common<true>;
6037
6038_LIBCPP_END_NAMESPACE_STD