1 //===------------------------- locale.cpp ---------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // On Solaris, we need to define something to make the C99 parts of localeconv
22 #ifndef _LIBCPP_NO_EXCEPTIONS
23 # include "type_traits"
28 #include "__sso_allocator"
29 #if defined(_LIBCPP_MSVCRT) || defined(__MINGW32__)
30 #include "support/win32/locale_win32.h"
31 #elif !defined(__ANDROID__)
37 // On Linux, wint_t and wchar_t have different signed-ness, and this causes
38 // lots of noise in the build log, but no bugs that I know of.
39 #if defined(__clang__)
40 #pragma clang diagnostic ignored "-Wsign-conversion"
43 _LIBCPP_BEGIN_NAMESPACE_STD
47 // In theory this could create a race condition. In practice
48 // the race condition is non-fatal since it will just create
49 // a little resource leak. Better approach would be appreciated.
50 static locale_t result = newlocale(LC_ALL_MASK, "C", 0);
53 #endif // __cloc_defined
59 void operator()(locale::facet* p) {p->__release_shared();}
62 template <class T, class A0>
67 static typename aligned_storage<sizeof(T)>::type buf;
69 return *reinterpret_cast<T*>(&buf);
72 template <class T, class A0, class A1>
77 static typename aligned_storage<sizeof(T)>::type buf;
78 ::new (&buf) T(a0, a1);
79 return *reinterpret_cast<T*>(&buf);
82 template <class T, class A0, class A1, class A2>
85 make(A0 a0, A1 a1, A2 a2)
87 static typename aligned_storage<sizeof(T)>::type buf;
88 ::new (&buf) T(a0, a1, a2);
89 return *reinterpret_cast<T*>(&buf);
92 template <typename T, size_t N>
96 countof(const T (&)[N])
101 template <typename T>
105 countof(const T * const begin, const T * const end)
107 return static_cast<size_t>(end - begin);
113 // Set priority to INT_MIN + 256 + 150
114 # pragma priority ( -2147483242 )
117 const locale::category locale::none;
118 const locale::category locale::collate;
119 const locale::category locale::ctype;
120 const locale::category locale::monetary;
121 const locale::category locale::numeric;
122 const locale::category locale::time;
123 const locale::category locale::messages;
124 const locale::category locale::all;
126 #if defined(__clang__)
127 #pragma clang diagnostic push
128 #pragma clang diagnostic ignored "-Wpadded"
131 class _LIBCPP_HIDDEN locale::__imp
135 #if defined(_LIBCPP_MSVC)
136 // FIXME: MSVC doesn't support aligned parameters by value.
137 // I can't get the __sso_allocator to work here
138 // for MSVC I think for this reason.
139 vector<facet*> facets_;
141 vector<facet*, __sso_allocator<facet*, N> > facets_;
145 explicit __imp(size_t refs = 0);
146 explicit __imp(const string& name, size_t refs = 0);
148 __imp(const __imp&, const string&, locale::category c);
149 __imp(const __imp& other, const __imp& one, locale::category c);
150 __imp(const __imp&, facet* f, long id);
153 const string& name() const {return name_;}
154 bool has_facet(long id) const
155 {return static_cast<size_t>(id) < facets_.size() && facets_[static_cast<size_t>(id)];}
156 const locale::facet* use_facet(long id) const;
158 static const locale& make_classic();
159 static locale& make_global();
161 void install(facet* f, long id);
162 template <class F> void install(F* f) {install(f, f->id.__get());}
163 template <class F> void install_from(const __imp& other);
166 #if defined(__clang__)
167 #pragma clang diagnostic pop
170 locale::__imp::__imp(size_t refs)
176 install(&make<_VSTD::collate<char> >(1u));
177 install(&make<_VSTD::collate<wchar_t> >(1u));
178 install(&make<_VSTD::ctype<char> >(nullptr, false, 1u));
179 install(&make<_VSTD::ctype<wchar_t> >(1u));
180 install(&make<codecvt<char, char, mbstate_t> >(1u));
181 install(&make<codecvt<wchar_t, char, mbstate_t> >(1u));
182 install(&make<codecvt<char16_t, char, mbstate_t> >(1u));
183 install(&make<codecvt<char32_t, char, mbstate_t> >(1u));
184 install(&make<numpunct<char> >(1u));
185 install(&make<numpunct<wchar_t> >(1u));
186 install(&make<num_get<char> >(1u));
187 install(&make<num_get<wchar_t> >(1u));
188 install(&make<num_put<char> >(1u));
189 install(&make<num_put<wchar_t> >(1u));
190 install(&make<moneypunct<char, false> >(1u));
191 install(&make<moneypunct<char, true> >(1u));
192 install(&make<moneypunct<wchar_t, false> >(1u));
193 install(&make<moneypunct<wchar_t, true> >(1u));
194 install(&make<money_get<char> >(1u));
195 install(&make<money_get<wchar_t> >(1u));
196 install(&make<money_put<char> >(1u));
197 install(&make<money_put<wchar_t> >(1u));
198 install(&make<time_get<char> >(1u));
199 install(&make<time_get<wchar_t> >(1u));
200 install(&make<time_put<char> >(1u));
201 install(&make<time_put<wchar_t> >(1u));
202 install(&make<_VSTD::messages<char> >(1u));
203 install(&make<_VSTD::messages<wchar_t> >(1u));
206 locale::__imp::__imp(const string& name, size_t refs)
211 #ifndef _LIBCPP_NO_EXCEPTIONS
214 #endif // _LIBCPP_NO_EXCEPTIONS
215 facets_ = locale::classic().__locale_->facets_;
216 for (unsigned i = 0; i < facets_.size(); ++i)
218 facets_[i]->__add_shared();
219 install(new collate_byname<char>(name_));
220 install(new collate_byname<wchar_t>(name_));
221 install(new ctype_byname<char>(name_));
222 install(new ctype_byname<wchar_t>(name_));
223 install(new codecvt_byname<char, char, mbstate_t>(name_));
224 install(new codecvt_byname<wchar_t, char, mbstate_t>(name_));
225 install(new codecvt_byname<char16_t, char, mbstate_t>(name_));
226 install(new codecvt_byname<char32_t, char, mbstate_t>(name_));
227 install(new numpunct_byname<char>(name_));
228 install(new numpunct_byname<wchar_t>(name_));
229 install(new moneypunct_byname<char, false>(name_));
230 install(new moneypunct_byname<char, true>(name_));
231 install(new moneypunct_byname<wchar_t, false>(name_));
232 install(new moneypunct_byname<wchar_t, true>(name_));
233 install(new time_get_byname<char>(name_));
234 install(new time_get_byname<wchar_t>(name_));
235 install(new time_put_byname<char>(name_));
236 install(new time_put_byname<wchar_t>(name_));
237 install(new messages_byname<char>(name_));
238 install(new messages_byname<wchar_t>(name_));
239 #ifndef _LIBCPP_NO_EXCEPTIONS
243 for (unsigned i = 0; i < facets_.size(); ++i)
245 facets_[i]->__release_shared();
248 #endif // _LIBCPP_NO_EXCEPTIONS
251 // NOTE avoid the `base class should be explicitly initialized in the
252 // copy constructor` warning emitted by GCC
253 #if defined(__clang__) || _GNUC_VER >= 406
254 #pragma GCC diagnostic push
255 #pragma GCC diagnostic ignored "-Wextra"
258 locale::__imp::__imp(const __imp& other)
259 : facets_(max<size_t>(N, other.facets_.size())),
262 facets_ = other.facets_;
263 for (unsigned i = 0; i < facets_.size(); ++i)
265 facets_[i]->__add_shared();
268 #if defined(__clang__) || _GNUC_VER >= 406
269 #pragma GCC diagnostic pop
272 locale::__imp::__imp(const __imp& other, const string& name, locale::category c)
276 facets_ = other.facets_;
277 for (unsigned i = 0; i < facets_.size(); ++i)
279 facets_[i]->__add_shared();
280 #ifndef _LIBCPP_NO_EXCEPTIONS
283 #endif // _LIBCPP_NO_EXCEPTIONS
284 if (c & locale::collate)
286 install(new collate_byname<char>(name));
287 install(new collate_byname<wchar_t>(name));
289 if (c & locale::ctype)
291 install(new ctype_byname<char>(name));
292 install(new ctype_byname<wchar_t>(name));
293 install(new codecvt_byname<char, char, mbstate_t>(name));
294 install(new codecvt_byname<wchar_t, char, mbstate_t>(name));
295 install(new codecvt_byname<char16_t, char, mbstate_t>(name));
296 install(new codecvt_byname<char32_t, char, mbstate_t>(name));
298 if (c & locale::monetary)
300 install(new moneypunct_byname<char, false>(name));
301 install(new moneypunct_byname<char, true>(name));
302 install(new moneypunct_byname<wchar_t, false>(name));
303 install(new moneypunct_byname<wchar_t, true>(name));
305 if (c & locale::numeric)
307 install(new numpunct_byname<char>(name));
308 install(new numpunct_byname<wchar_t>(name));
310 if (c & locale::time)
312 install(new time_get_byname<char>(name));
313 install(new time_get_byname<wchar_t>(name));
314 install(new time_put_byname<char>(name));
315 install(new time_put_byname<wchar_t>(name));
317 if (c & locale::messages)
319 install(new messages_byname<char>(name));
320 install(new messages_byname<wchar_t>(name));
322 #ifndef _LIBCPP_NO_EXCEPTIONS
326 for (unsigned i = 0; i < facets_.size(); ++i)
328 facets_[i]->__release_shared();
331 #endif // _LIBCPP_NO_EXCEPTIONS
337 locale::__imp::install_from(const locale::__imp& one)
339 long id = F::id.__get();
340 install(const_cast<F*>(static_cast<const F*>(one.use_facet(id))), id);
343 locale::__imp::__imp(const __imp& other, const __imp& one, locale::category c)
347 facets_ = other.facets_;
348 for (unsigned i = 0; i < facets_.size(); ++i)
350 facets_[i]->__add_shared();
351 #ifndef _LIBCPP_NO_EXCEPTIONS
354 #endif // _LIBCPP_NO_EXCEPTIONS
355 if (c & locale::collate)
357 install_from<_VSTD::collate<char> >(one);
358 install_from<_VSTD::collate<wchar_t> >(one);
360 if (c & locale::ctype)
362 install_from<_VSTD::ctype<char> >(one);
363 install_from<_VSTD::ctype<wchar_t> >(one);
364 install_from<_VSTD::codecvt<char, char, mbstate_t> >(one);
365 install_from<_VSTD::codecvt<char16_t, char, mbstate_t> >(one);
366 install_from<_VSTD::codecvt<char32_t, char, mbstate_t> >(one);
367 install_from<_VSTD::codecvt<wchar_t, char, mbstate_t> >(one);
369 if (c & locale::monetary)
371 install_from<moneypunct<char, false> >(one);
372 install_from<moneypunct<char, true> >(one);
373 install_from<moneypunct<wchar_t, false> >(one);
374 install_from<moneypunct<wchar_t, true> >(one);
375 install_from<money_get<char> >(one);
376 install_from<money_get<wchar_t> >(one);
377 install_from<money_put<char> >(one);
378 install_from<money_put<wchar_t> >(one);
380 if (c & locale::numeric)
382 install_from<numpunct<char> >(one);
383 install_from<numpunct<wchar_t> >(one);
384 install_from<num_get<char> >(one);
385 install_from<num_get<wchar_t> >(one);
386 install_from<num_put<char> >(one);
387 install_from<num_put<wchar_t> >(one);
389 if (c & locale::time)
391 install_from<time_get<char> >(one);
392 install_from<time_get<wchar_t> >(one);
393 install_from<time_put<char> >(one);
394 install_from<time_put<wchar_t> >(one);
396 if (c & locale::messages)
398 install_from<_VSTD::messages<char> >(one);
399 install_from<_VSTD::messages<wchar_t> >(one);
401 #ifndef _LIBCPP_NO_EXCEPTIONS
405 for (unsigned i = 0; i < facets_.size(); ++i)
407 facets_[i]->__release_shared();
410 #endif // _LIBCPP_NO_EXCEPTIONS
413 locale::__imp::__imp(const __imp& other, facet* f, long id)
414 : facets_(max<size_t>(N, other.facets_.size()+1)),
418 unique_ptr<facet, release> hold(f);
419 facets_ = other.facets_;
420 for (unsigned i = 0; i < other.facets_.size(); ++i)
422 facets_[i]->__add_shared();
423 install(hold.get(), id);
426 locale::__imp::~__imp()
428 for (unsigned i = 0; i < facets_.size(); ++i)
430 facets_[i]->__release_shared();
434 locale::__imp::install(facet* f, long id)
437 unique_ptr<facet, release> hold(f);
438 if (static_cast<size_t>(id) >= facets_.size())
439 facets_.resize(static_cast<size_t>(id+1));
440 if (facets_[static_cast<size_t>(id)])
441 facets_[static_cast<size_t>(id)]->__release_shared();
442 facets_[static_cast<size_t>(id)] = hold.release();
446 locale::__imp::use_facet(long id) const
448 #ifndef _LIBCPP_NO_EXCEPTIONS
451 #endif // _LIBCPP_NO_EXCEPTIONS
452 return facets_[static_cast<size_t>(id)];
458 locale::__imp::make_classic()
460 // only one thread can get in here and it only gets in once
461 static aligned_storage<sizeof(locale)>::type buf;
462 locale* c = reinterpret_cast<locale*>(&buf);
463 c->__locale_ = &make<__imp>(1u);
470 static const locale& c = __imp::make_classic();
475 locale::__imp::make_global()
477 // only one thread can get in here and it only gets in once
478 static aligned_storage<sizeof(locale)>::type buf;
479 ::new (&buf) locale(locale::classic());
480 return *reinterpret_cast<locale*>(&buf);
486 static locale& g = __imp::make_global();
490 locale::locale() _NOEXCEPT
491 : __locale_(__global().__locale_)
493 __locale_->__add_shared();
496 locale::locale(const locale& l) _NOEXCEPT
497 : __locale_(l.__locale_)
499 __locale_->__add_shared();
504 __locale_->__release_shared();
508 locale::operator=(const locale& other) _NOEXCEPT
510 other.__locale_->__add_shared();
511 __locale_->__release_shared();
512 __locale_ = other.__locale_;
516 locale::locale(const char* name)
517 #ifndef _LIBCPP_NO_EXCEPTIONS
518 : __locale_(name ? new __imp(name)
519 : throw runtime_error("locale constructed with null"))
520 #else // _LIBCPP_NO_EXCEPTIONS
521 : __locale_(new __imp(name))
524 __locale_->__add_shared();
527 locale::locale(const string& name)
528 : __locale_(new __imp(name))
530 __locale_->__add_shared();
533 locale::locale(const locale& other, const char* name, category c)
534 #ifndef _LIBCPP_NO_EXCEPTIONS
535 : __locale_(name ? new __imp(*other.__locale_, name, c)
536 : throw runtime_error("locale constructed with null"))
537 #else // _LIBCPP_NO_EXCEPTIONS
538 : __locale_(new __imp(*other.__locale_, name, c))
541 __locale_->__add_shared();
544 locale::locale(const locale& other, const string& name, category c)
545 : __locale_(new __imp(*other.__locale_, name, c))
547 __locale_->__add_shared();
550 locale::locale(const locale& other, const locale& one, category c)
551 : __locale_(new __imp(*other.__locale_, *one.__locale_, c))
553 __locale_->__add_shared();
559 return __locale_->name();
563 locale::__install_ctor(const locale& other, facet* f, long id)
566 __locale_ = new __imp(*other.__locale_, f, id);
568 __locale_ = other.__locale_;
569 __locale_->__add_shared();
573 locale::global(const locale& loc)
575 locale& g = __global();
580 setlocale(LC_ALL, g.name().c_str());
586 locale::has_facet(id& x) const
588 return __locale_->has_facet(x.__get());
592 locale::use_facet(id& x) const
594 return __locale_->use_facet(x.__get());
598 locale::operator==(const locale& y) const
600 return (__locale_ == y.__locale_)
601 || (__locale_->name() != "*" && __locale_->name() == y.__locale_->name());
606 locale::facet::~facet()
611 locale::facet::__on_zero_shared() _NOEXCEPT
618 int32_t locale::id::__next_id = 0;
626 void (locale::id::* pmf_)();
628 __fake_bind(void (locale::id::* pmf)(), locale::id* id)
629 : id_(id), pmf_(pmf) {}
631 void operator()() const
642 call_once(__flag_, __fake_bind(&locale::id::__init, this));
649 __id_ = __sync_add_and_fetch(&__next_id, 1);
652 // template <> class collate_byname<char>
654 collate_byname<char>::collate_byname(const char* n, size_t refs)
655 : collate<char>(refs),
656 __l(newlocale(LC_ALL_MASK, n, 0))
658 #ifndef _LIBCPP_NO_EXCEPTIONS
660 throw runtime_error("collate_byname<char>::collate_byname"
661 " failed to construct for " + string(n));
662 #endif // _LIBCPP_NO_EXCEPTIONS
665 collate_byname<char>::collate_byname(const string& name, size_t refs)
666 : collate<char>(refs),
667 __l(newlocale(LC_ALL_MASK, name.c_str(), 0))
669 #ifndef _LIBCPP_NO_EXCEPTIONS
671 throw runtime_error("collate_byname<char>::collate_byname"
672 " failed to construct for " + name);
673 #endif // _LIBCPP_NO_EXCEPTIONS
676 collate_byname<char>::~collate_byname()
682 collate_byname<char>::do_compare(const char_type* __lo1, const char_type* __hi1,
683 const char_type* __lo2, const char_type* __hi2) const
685 string_type lhs(__lo1, __hi1);
686 string_type rhs(__lo2, __hi2);
687 int r = strcoll_l(lhs.c_str(), rhs.c_str(), __l);
695 collate_byname<char>::string_type
696 collate_byname<char>::do_transform(const char_type* lo, const char_type* hi) const
698 const string_type in(lo, hi);
699 string_type out(strxfrm_l(0, in.c_str(), 0, __l), char());
700 strxfrm_l(const_cast<char*>(out.c_str()), in.c_str(), out.size()+1, __l);
704 // template <> class collate_byname<wchar_t>
706 collate_byname<wchar_t>::collate_byname(const char* n, size_t refs)
707 : collate<wchar_t>(refs),
708 __l(newlocale(LC_ALL_MASK, n, 0))
710 #ifndef _LIBCPP_NO_EXCEPTIONS
712 throw runtime_error("collate_byname<wchar_t>::collate_byname(size_t refs)"
713 " failed to construct for " + string(n));
714 #endif // _LIBCPP_NO_EXCEPTIONS
717 collate_byname<wchar_t>::collate_byname(const string& name, size_t refs)
718 : collate<wchar_t>(refs),
719 __l(newlocale(LC_ALL_MASK, name.c_str(), 0))
721 #ifndef _LIBCPP_NO_EXCEPTIONS
723 throw runtime_error("collate_byname<wchar_t>::collate_byname(size_t refs)"
724 " failed to construct for " + name);
725 #endif // _LIBCPP_NO_EXCEPTIONS
728 collate_byname<wchar_t>::~collate_byname()
734 collate_byname<wchar_t>::do_compare(const char_type* __lo1, const char_type* __hi1,
735 const char_type* __lo2, const char_type* __hi2) const
737 string_type lhs(__lo1, __hi1);
738 string_type rhs(__lo2, __hi2);
739 int r = wcscoll_l(lhs.c_str(), rhs.c_str(), __l);
747 collate_byname<wchar_t>::string_type
748 collate_byname<wchar_t>::do_transform(const char_type* lo, const char_type* hi) const
750 const string_type in(lo, hi);
751 string_type out(wcsxfrm_l(0, in.c_str(), 0, __l), wchar_t());
752 wcsxfrm_l(const_cast<wchar_t*>(out.c_str()), in.c_str(), out.size()+1, __l);
756 // template <> class ctype<wchar_t>;
758 const ctype_base::mask ctype_base::space;
759 const ctype_base::mask ctype_base::print;
760 const ctype_base::mask ctype_base::cntrl;
761 const ctype_base::mask ctype_base::upper;
762 const ctype_base::mask ctype_base::lower;
763 const ctype_base::mask ctype_base::alpha;
764 const ctype_base::mask ctype_base::digit;
765 const ctype_base::mask ctype_base::punct;
766 const ctype_base::mask ctype_base::xdigit;
767 const ctype_base::mask ctype_base::blank;
768 const ctype_base::mask ctype_base::alnum;
769 const ctype_base::mask ctype_base::graph;
771 locale::id ctype<wchar_t>::id;
773 ctype<wchar_t>::~ctype()
778 ctype<wchar_t>::do_is(mask m, char_type c) const
780 return isascii(c) ? (ctype<char>::classic_table()[c] & m) != 0 : false;
784 ctype<wchar_t>::do_is(const char_type* low, const char_type* high, mask* vec) const
786 for (; low != high; ++low, ++vec)
787 *vec = static_cast<mask>(isascii(*low) ?
788 ctype<char>::classic_table()[*low] : 0);
793 ctype<wchar_t>::do_scan_is(mask m, const char_type* low, const char_type* high) const
795 for (; low != high; ++low)
796 if (isascii(*low) && (ctype<char>::classic_table()[*low] & m))
802 ctype<wchar_t>::do_scan_not(mask m, const char_type* low, const char_type* high) const
804 for (; low != high; ++low)
805 if (!(isascii(*low) && (ctype<char>::classic_table()[*low] & m)))
811 ctype<wchar_t>::do_toupper(char_type c) const
813 #ifdef _LIBCPP_HAS_DEFAULTRUNELOCALE
814 return isascii(c) ? _DefaultRuneLocale.__mapupper[c] : c;
815 #elif defined(__GLIBC__) || defined(__EMSCRIPTEN__) || \
817 return isascii(c) ? ctype<char>::__classic_upper_table()[c] : c;
819 return (isascii(c) && iswlower_l(c, _LIBCPP_GET_C_LOCALE)) ? c-L'a'+L'A' : c;
824 ctype<wchar_t>::do_toupper(char_type* low, const char_type* high) const
826 for (; low != high; ++low)
827 #ifdef _LIBCPP_HAS_DEFAULTRUNELOCALE
828 *low = isascii(*low) ? _DefaultRuneLocale.__mapupper[*low] : *low;
829 #elif defined(__GLIBC__) || defined(__EMSCRIPTEN__) || \
831 *low = isascii(*low) ? ctype<char>::__classic_upper_table()[*low]
834 *low = (isascii(*low) && islower_l(*low, _LIBCPP_GET_C_LOCALE)) ? (*low-L'a'+L'A') : *low;
840 ctype<wchar_t>::do_tolower(char_type c) const
842 #ifdef _LIBCPP_HAS_DEFAULTRUNELOCALE
843 return isascii(c) ? _DefaultRuneLocale.__maplower[c] : c;
844 #elif defined(__GLIBC__) || defined(__EMSCRIPTEN__) || \
846 return isascii(c) ? ctype<char>::__classic_lower_table()[c] : c;
848 return (isascii(c) && isupper_l(c, _LIBCPP_GET_C_LOCALE)) ? c-L'A'+'a' : c;
853 ctype<wchar_t>::do_tolower(char_type* low, const char_type* high) const
855 for (; low != high; ++low)
856 #ifdef _LIBCPP_HAS_DEFAULTRUNELOCALE
857 *low = isascii(*low) ? _DefaultRuneLocale.__maplower[*low] : *low;
858 #elif defined(__GLIBC__) || defined(__EMSCRIPTEN__) || \
860 *low = isascii(*low) ? ctype<char>::__classic_lower_table()[*low]
863 *low = (isascii(*low) && isupper_l(*low, _LIBCPP_GET_C_LOCALE)) ? *low-L'A'+L'a' : *low;
869 ctype<wchar_t>::do_widen(char c) const
875 ctype<wchar_t>::do_widen(const char* low, const char* high, char_type* dest) const
877 for (; low != high; ++low, ++dest)
883 ctype<wchar_t>::do_narrow(char_type c, char dfault) const
886 return static_cast<char>(c);
891 ctype<wchar_t>::do_narrow(const char_type* low, const char_type* high, char dfault, char* dest) const
893 for (; low != high; ++low, ++dest)
895 *dest = static_cast<char>(*low);
901 // template <> class ctype<char>;
903 locale::id ctype<char>::id;
905 ctype<char>::ctype(const mask* tab, bool del, size_t refs)
906 : locale::facet(refs),
911 __tab_ = classic_table();
914 ctype<char>::~ctype()
916 if (__tab_ && __del_)
921 ctype<char>::do_toupper(char_type c) const
923 #ifdef _LIBCPP_HAS_DEFAULTRUNELOCALE
925 static_cast<char>(_DefaultRuneLocale.__mapupper[static_cast<ptrdiff_t>(c)]) : c;
926 #elif defined(__NetBSD__)
927 return static_cast<char>(__classic_upper_table()[static_cast<unsigned char>(c)]);
928 #elif defined(__GLIBC__) || defined(__EMSCRIPTEN__)
930 static_cast<char>(__classic_upper_table()[static_cast<unsigned char>(c)]) : c;
932 return (isascii(c) && islower_l(c, _LIBCPP_GET_C_LOCALE)) ? c-'a'+'A' : c;
937 ctype<char>::do_toupper(char_type* low, const char_type* high) const
939 for (; low != high; ++low)
940 #ifdef _LIBCPP_HAS_DEFAULTRUNELOCALE
941 *low = isascii(*low) ?
942 static_cast<char>(_DefaultRuneLocale.__mapupper[static_cast<ptrdiff_t>(*low)]) : *low;
943 #elif defined(__NetBSD__)
944 *low = static_cast<char>(__classic_upper_table()[static_cast<unsigned char>(*low)]);
945 #elif defined(__GLIBC__) || defined(__EMSCRIPTEN__)
946 *low = isascii(*low) ?
947 static_cast<char>(__classic_upper_table()[static_cast<size_t>(*low)]) : *low;
949 *low = (isascii(*low) && islower_l(*low, _LIBCPP_GET_C_LOCALE)) ? *low-'a'+'A' : *low;
955 ctype<char>::do_tolower(char_type c) const
957 #ifdef _LIBCPP_HAS_DEFAULTRUNELOCALE
959 static_cast<char>(_DefaultRuneLocale.__maplower[static_cast<ptrdiff_t>(c)]) : c;
960 #elif defined(__NetBSD__)
961 return static_cast<char>(__classic_lower_table()[static_cast<unsigned char>(c)]);
962 #elif defined(__GLIBC__) || defined(__EMSCRIPTEN__)
964 static_cast<char>(__classic_lower_table()[static_cast<size_t>(c)]) : c;
966 return (isascii(c) && isupper_l(c, _LIBCPP_GET_C_LOCALE)) ? c-'A'+'a' : c;
971 ctype<char>::do_tolower(char_type* low, const char_type* high) const
973 for (; low != high; ++low)
974 #ifdef _LIBCPP_HAS_DEFAULTRUNELOCALE
975 *low = isascii(*low) ? static_cast<char>(_DefaultRuneLocale.__maplower[static_cast<ptrdiff_t>(*low)]) : *low;
976 #elif defined(__NetBSD__)
977 *low = static_cast<char>(__classic_lower_table()[static_cast<unsigned char>(*low)]);
978 #elif defined(__GLIBC__) || defined(__EMSCRIPTEN__)
979 *low = isascii(*low) ? static_cast<char>(__classic_lower_table()[static_cast<size_t>(*low)]) : *low;
981 *low = (isascii(*low) && isupper_l(*low, _LIBCPP_GET_C_LOCALE)) ? *low-'A'+'a' : *low;
987 ctype<char>::do_widen(char c) const
993 ctype<char>::do_widen(const char* low, const char* high, char_type* dest) const
995 for (; low != high; ++low, ++dest)
1001 ctype<char>::do_narrow(char_type c, char dfault) const
1004 return static_cast<char>(c);
1009 ctype<char>::do_narrow(const char_type* low, const char_type* high, char dfault, char* dest) const
1011 for (; low != high; ++low, ++dest)
1019 #if defined(__EMSCRIPTEN__)
1020 extern "C" const unsigned short ** __ctype_b_loc();
1021 extern "C" const int ** __ctype_tolower_loc();
1022 extern "C" const int ** __ctype_toupper_loc();
1025 #ifdef _LIBCPP_PROVIDES_DEFAULT_RUNE_TABLE
1026 const ctype<char>::mask*
1027 ctype<char>::classic_table() _NOEXCEPT
1029 static _LIBCPP_CONSTEXPR const ctype<char>::mask builtin_table[table_size] = {
1034 cntrl, cntrl | space | blank,
1035 cntrl | space, cntrl | space,
1036 cntrl | space, cntrl | space,
1046 space | blank | print, punct | print,
1047 punct | print, punct | print,
1048 punct | print, punct | print,
1049 punct | print, punct | print,
1050 punct | print, punct | print,
1051 punct | print, punct | print,
1052 punct | print, punct | print,
1053 punct | print, punct | print,
1054 digit | print | xdigit, digit | print | xdigit,
1055 digit | print | xdigit, digit | print | xdigit,
1056 digit | print | xdigit, digit | print | xdigit,
1057 digit | print | xdigit, digit | print | xdigit,
1058 digit | print | xdigit, digit | print | xdigit,
1059 punct | print, punct | print,
1060 punct | print, punct | print,
1061 punct | print, punct | print,
1062 punct | print, upper | xdigit | print | alpha,
1063 upper | xdigit | print | alpha, upper | xdigit | print | alpha,
1064 upper | xdigit | print | alpha, upper | xdigit | print | alpha,
1065 upper | xdigit | print | alpha, upper | print | alpha,
1066 upper | print | alpha, upper | print | alpha,
1067 upper | print | alpha, upper | print | alpha,
1068 upper | print | alpha, upper | print | alpha,
1069 upper | print | alpha, upper | print | alpha,
1070 upper | print | alpha, upper | print | alpha,
1071 upper | print | alpha, upper | print | alpha,
1072 upper | print | alpha, upper | print | alpha,
1073 upper | print | alpha, upper | print | alpha,
1074 upper | print | alpha, upper | print | alpha,
1075 upper | print | alpha, punct | print,
1076 punct | print, punct | print,
1077 punct | print, punct | print,
1078 punct | print, lower | xdigit | print | alpha,
1079 lower | xdigit | print | alpha, lower | xdigit | print | alpha,
1080 lower | xdigit | print | alpha, lower | xdigit | print | alpha,
1081 lower | xdigit | print | alpha, lower | print | alpha,
1082 lower | print | alpha, lower | print | alpha,
1083 lower | print | alpha, lower | print | alpha,
1084 lower | print | alpha, lower | print | alpha,
1085 lower | print | alpha, lower | print | alpha,
1086 lower | print | alpha, lower | print | alpha,
1087 lower | print | alpha, lower | print | alpha,
1088 lower | print | alpha, lower | print | alpha,
1089 lower | print | alpha, lower | print | alpha,
1090 lower | print | alpha, lower | print | alpha,
1091 lower | print | alpha, punct | print,
1092 punct | print, punct | print,
1093 punct | print, cntrl,
1094 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1095 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1096 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1097 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1098 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1099 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1100 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1101 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
1103 return builtin_table;
1106 const ctype<char>::mask*
1107 ctype<char>::classic_table() _NOEXCEPT
1109 #if defined(__APPLE__) || defined(__FreeBSD__)
1110 return _DefaultRuneLocale.__runetype;
1111 #elif defined(__NetBSD__)
1112 return _C_ctype_tab_ + 1;
1113 #elif defined(__GLIBC__)
1114 return _LIBCPP_GET_C_LOCALE->__ctype_b;
1116 return __ctype_mask;
1117 #elif defined(_LIBCPP_MSVCRT) || defined(__MINGW32__)
1118 return _ctype+1; // internal ctype mask table defined in msvcrt.dll
1119 // This is assumed to be safe, which is a nonsense assumption because we're
1120 // going to end up dereferencing it later...
1121 #elif defined(__EMSCRIPTEN__)
1122 return *__ctype_b_loc();
1123 #elif defined(_NEWLIB_VERSION)
1124 // Newlib has a 257-entry table in ctype_.c, where (char)0 starts at [1].
1127 return (const unsigned int *)__lc_ctype_ptr->obj->mask;
1129 // Platform not supported: abort so the person doing the port knows what to
1131 # warning ctype<char>::classic_table() is not implemented
1132 printf("ctype<char>::classic_table() is not implemented\n");
1139 #if defined(__GLIBC__)
1141 ctype<char>::__classic_lower_table() _NOEXCEPT
1143 return _LIBCPP_GET_C_LOCALE->__ctype_tolower;
1147 ctype<char>::__classic_upper_table() _NOEXCEPT
1149 return _LIBCPP_GET_C_LOCALE->__ctype_toupper;
1153 ctype<char>::__classic_lower_table() _NOEXCEPT
1155 return _C_tolower_tab_ + 1;
1159 ctype<char>::__classic_upper_table() _NOEXCEPT
1161 return _C_toupper_tab_ + 1;
1164 #elif defined(__EMSCRIPTEN__)
1166 ctype<char>::__classic_lower_table() _NOEXCEPT
1168 return *__ctype_tolower_loc();
1172 ctype<char>::__classic_upper_table() _NOEXCEPT
1174 return *__ctype_toupper_loc();
1176 #endif // __GLIBC__ || __NETBSD__ || __EMSCRIPTEN__
1178 // template <> class ctype_byname<char>
1180 ctype_byname<char>::ctype_byname(const char* name, size_t refs)
1181 : ctype<char>(0, false, refs),
1182 __l(newlocale(LC_ALL_MASK, name, 0))
1184 #ifndef _LIBCPP_NO_EXCEPTIONS
1186 throw runtime_error("ctype_byname<char>::ctype_byname"
1187 " failed to construct for " + string(name));
1188 #endif // _LIBCPP_NO_EXCEPTIONS
1191 ctype_byname<char>::ctype_byname(const string& name, size_t refs)
1192 : ctype<char>(0, false, refs),
1193 __l(newlocale(LC_ALL_MASK, name.c_str(), 0))
1195 #ifndef _LIBCPP_NO_EXCEPTIONS
1197 throw runtime_error("ctype_byname<char>::ctype_byname"
1198 " failed to construct for " + name);
1199 #endif // _LIBCPP_NO_EXCEPTIONS
1202 ctype_byname<char>::~ctype_byname()
1208 ctype_byname<char>::do_toupper(char_type c) const
1210 return static_cast<char>(toupper_l(static_cast<unsigned char>(c), __l));
1214 ctype_byname<char>::do_toupper(char_type* low, const char_type* high) const
1216 for (; low != high; ++low)
1217 *low = static_cast<char>(toupper_l(static_cast<unsigned char>(*low), __l));
1222 ctype_byname<char>::do_tolower(char_type c) const
1224 return static_cast<char>(tolower_l(static_cast<unsigned char>(c), __l));
1228 ctype_byname<char>::do_tolower(char_type* low, const char_type* high) const
1230 for (; low != high; ++low)
1231 *low = static_cast<char>(tolower_l(static_cast<unsigned char>(*low), __l));
1235 // template <> class ctype_byname<wchar_t>
1237 ctype_byname<wchar_t>::ctype_byname(const char* name, size_t refs)
1238 : ctype<wchar_t>(refs),
1239 __l(newlocale(LC_ALL_MASK, name, 0))
1241 #ifndef _LIBCPP_NO_EXCEPTIONS
1243 throw runtime_error("ctype_byname<wchar_t>::ctype_byname"
1244 " failed to construct for " + string(name));
1245 #endif // _LIBCPP_NO_EXCEPTIONS
1248 ctype_byname<wchar_t>::ctype_byname(const string& name, size_t refs)
1249 : ctype<wchar_t>(refs),
1250 __l(newlocale(LC_ALL_MASK, name.c_str(), 0))
1252 #ifndef _LIBCPP_NO_EXCEPTIONS
1254 throw runtime_error("ctype_byname<wchar_t>::ctype_byname"
1255 " failed to construct for " + name);
1256 #endif // _LIBCPP_NO_EXCEPTIONS
1259 ctype_byname<wchar_t>::~ctype_byname()
1265 ctype_byname<wchar_t>::do_is(mask m, char_type c) const
1267 #ifdef _LIBCPP_WCTYPE_IS_MASK
1268 return static_cast<bool>(iswctype_l(c, m, __l));
1270 bool result = false;
1271 wint_t ch = static_cast<wint_t>(c);
1272 if ((m & space) == space) result |= (iswspace_l(ch, __l) != 0);
1273 if ((m & print) == print) result |= (iswprint_l(ch, __l) != 0);
1274 if ((m & cntrl) == cntrl) result |= (iswcntrl_l(ch, __l) != 0);
1275 if ((m & upper) == upper) result |= (iswupper_l(ch, __l) != 0);
1276 if ((m & lower) == lower) result |= (iswlower_l(ch, __l) != 0);
1277 if ((m & alpha) == alpha) result |= (iswalpha_l(ch, __l) != 0);
1278 if ((m & digit) == digit) result |= (iswdigit_l(ch, __l) != 0);
1279 if ((m & punct) == punct) result |= (iswpunct_l(ch, __l) != 0);
1280 if ((m & xdigit) == xdigit) result |= (iswxdigit_l(ch, __l) != 0);
1281 if ((m & blank) == blank) result |= (iswblank_l(ch, __l) != 0);
1287 ctype_byname<wchar_t>::do_is(const char_type* low, const char_type* high, mask* vec) const
1289 for (; low != high; ++low, ++vec)
1292 *vec = static_cast<mask>(ctype<char>::classic_table()[*low]);
1296 wint_t ch = static_cast<wint_t>(*low);
1297 if (iswspace_l(ch, __l))
1299 #ifndef _LIBCPP_CTYPE_MASK_IS_COMPOSITE_PRINT
1300 if (iswprint_l(ch, __l))
1303 if (iswcntrl_l(ch, __l))
1305 if (iswupper_l(ch, __l))
1307 if (iswlower_l(ch, __l))
1309 #ifndef _LIBCPP_CTYPE_MASK_IS_COMPOSITE_ALPHA
1310 if (iswalpha_l(ch, __l))
1313 if (iswdigit_l(ch, __l))
1315 if (iswpunct_l(ch, __l))
1317 #ifndef _LIBCPP_CTYPE_MASK_IS_COMPOSITE_XDIGIT
1318 if (iswxdigit_l(ch, __l))
1321 #if !defined(__sun__)
1322 if (iswblank_l(ch, __l))
1331 ctype_byname<wchar_t>::do_scan_is(mask m, const char_type* low, const char_type* high) const
1333 for (; low != high; ++low)
1335 #ifdef _LIBCPP_WCTYPE_IS_MASK
1336 if (iswctype_l(*low, m, __l))
1339 wint_t ch = static_cast<wint_t>(*low);
1340 if ((m & space) == space && iswspace_l(ch, __l)) break;
1341 if ((m & print) == print && iswprint_l(ch, __l)) break;
1342 if ((m & cntrl) == cntrl && iswcntrl_l(ch, __l)) break;
1343 if ((m & upper) == upper && iswupper_l(ch, __l)) break;
1344 if ((m & lower) == lower && iswlower_l(ch, __l)) break;
1345 if ((m & alpha) == alpha && iswalpha_l(ch, __l)) break;
1346 if ((m & digit) == digit && iswdigit_l(ch, __l)) break;
1347 if ((m & punct) == punct && iswpunct_l(ch, __l)) break;
1348 if ((m & xdigit) == xdigit && iswxdigit_l(ch, __l)) break;
1349 if ((m & blank) == blank && iswblank_l(ch, __l)) break;
1356 ctype_byname<wchar_t>::do_scan_not(mask m, const char_type* low, const char_type* high) const
1358 for (; low != high; ++low)
1360 #ifdef _LIBCPP_WCTYPE_IS_MASK
1361 if (!iswctype_l(*low, m, __l))
1364 wint_t ch = static_cast<wint_t>(*low);
1365 if ((m & space) == space && iswspace_l(ch, __l)) continue;
1366 if ((m & print) == print && iswprint_l(ch, __l)) continue;
1367 if ((m & cntrl) == cntrl && iswcntrl_l(ch, __l)) continue;
1368 if ((m & upper) == upper && iswupper_l(ch, __l)) continue;
1369 if ((m & lower) == lower && iswlower_l(ch, __l)) continue;
1370 if ((m & alpha) == alpha && iswalpha_l(ch, __l)) continue;
1371 if ((m & digit) == digit && iswdigit_l(ch, __l)) continue;
1372 if ((m & punct) == punct && iswpunct_l(ch, __l)) continue;
1373 if ((m & xdigit) == xdigit && iswxdigit_l(ch, __l)) continue;
1374 if ((m & blank) == blank && iswblank_l(ch, __l)) continue;
1382 ctype_byname<wchar_t>::do_toupper(char_type c) const
1384 return towupper_l(c, __l);
1388 ctype_byname<wchar_t>::do_toupper(char_type* low, const char_type* high) const
1390 for (; low != high; ++low)
1391 *low = towupper_l(*low, __l);
1396 ctype_byname<wchar_t>::do_tolower(char_type c) const
1398 return towlower_l(c, __l);
1402 ctype_byname<wchar_t>::do_tolower(char_type* low, const char_type* high) const
1404 for (; low != high; ++low)
1405 *low = towlower_l(*low, __l);
1410 ctype_byname<wchar_t>::do_widen(char c) const
1412 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
1413 return btowc_l(c, __l);
1415 return __btowc_l(c, __l);
1420 ctype_byname<wchar_t>::do_widen(const char* low, const char* high, char_type* dest) const
1422 for (; low != high; ++low, ++dest)
1423 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
1424 *dest = btowc_l(*low, __l);
1426 *dest = __btowc_l(*low, __l);
1432 ctype_byname<wchar_t>::do_narrow(char_type c, char dfault) const
1434 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
1435 int r = wctob_l(c, __l);
1437 int r = __wctob_l(c, __l);
1439 return r != static_cast<int>(WEOF) ? static_cast<char>(r) : dfault;
1443 ctype_byname<wchar_t>::do_narrow(const char_type* low, const char_type* high, char dfault, char* dest) const
1445 for (; low != high; ++low, ++dest)
1447 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
1448 int r = wctob_l(*low, __l);
1450 int r = __wctob_l(*low, __l);
1452 *dest = r != static_cast<int>(WEOF) ? static_cast<char>(r) : dfault;
1457 // template <> class codecvt<char, char, mbstate_t>
1459 locale::id codecvt<char, char, mbstate_t>::id;
1461 codecvt<char, char, mbstate_t>::~codecvt()
1465 codecvt<char, char, mbstate_t>::result
1466 codecvt<char, char, mbstate_t>::do_out(state_type&,
1467 const intern_type* frm, const intern_type*, const intern_type*& frm_nxt,
1468 extern_type* to, extern_type*, extern_type*& to_nxt) const
1475 codecvt<char, char, mbstate_t>::result
1476 codecvt<char, char, mbstate_t>::do_in(state_type&,
1477 const extern_type* frm, const extern_type*, const extern_type*& frm_nxt,
1478 intern_type* to, intern_type*, intern_type*& to_nxt) const
1485 codecvt<char, char, mbstate_t>::result
1486 codecvt<char, char, mbstate_t>::do_unshift(state_type&,
1487 extern_type* to, extern_type*, extern_type*& to_nxt) const
1494 codecvt<char, char, mbstate_t>::do_encoding() const _NOEXCEPT
1500 codecvt<char, char, mbstate_t>::do_always_noconv() const _NOEXCEPT
1506 codecvt<char, char, mbstate_t>::do_length(state_type&,
1507 const extern_type* frm, const extern_type* end, size_t mx) const
1509 return static_cast<int>(min<size_t>(mx, static_cast<size_t>(end-frm)));
1513 codecvt<char, char, mbstate_t>::do_max_length() const _NOEXCEPT
1518 // template <> class codecvt<wchar_t, char, mbstate_t>
1520 locale::id codecvt<wchar_t, char, mbstate_t>::id;
1522 codecvt<wchar_t, char, mbstate_t>::codecvt(size_t refs)
1523 : locale::facet(refs),
1524 __l(_LIBCPP_GET_C_LOCALE)
1528 codecvt<wchar_t, char, mbstate_t>::codecvt(const char* nm, size_t refs)
1529 : locale::facet(refs),
1530 __l(newlocale(LC_ALL_MASK, nm, 0))
1532 #ifndef _LIBCPP_NO_EXCEPTIONS
1534 throw runtime_error("codecvt_byname<wchar_t, char, mbstate_t>::codecvt_byname"
1535 " failed to construct for " + string(nm));
1536 #endif // _LIBCPP_NO_EXCEPTIONS
1539 codecvt<wchar_t, char, mbstate_t>::~codecvt()
1541 if (__l != _LIBCPP_GET_C_LOCALE)
1545 codecvt<wchar_t, char, mbstate_t>::result
1546 codecvt<wchar_t, char, mbstate_t>::do_out(state_type& st,
1547 const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt,
1548 extern_type* to, extern_type* to_end, extern_type*& to_nxt) const
1550 // look for first internal null in frm
1551 const intern_type* fend = frm;
1552 for (; fend != frm_end; ++fend)
1555 // loop over all null-terminated sequences in frm
1557 for (frm_nxt = frm; frm != frm_end && to != to_end; frm = frm_nxt, to = to_nxt)
1559 // save state in case it is needed to recover to_nxt on error
1560 mbstate_t save_state = st;
1561 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
1562 size_t n = wcsnrtombs_l(to, &frm_nxt, static_cast<size_t>(fend-frm),
1563 static_cast<size_t>(to_end-to), &st, __l);
1565 size_t n = __wcsnrtombs_l(to, &frm_nxt, fend-frm, to_end-to, &st, __l);
1567 if (n == size_t(-1))
1569 // need to recover to_nxt
1570 for (to_nxt = to; frm != frm_nxt; ++frm)
1572 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
1573 n = wcrtomb_l(to_nxt, *frm, &save_state, __l);
1575 n = __wcrtomb_l(to_nxt, *frm, &save_state, __l);
1577 if (n == size_t(-1))
1587 if (to_nxt == to_end)
1589 if (fend != frm_end) // set up next null terminated sequence
1591 // Try to write the terminating null
1592 extern_type tmp[MB_LEN_MAX];
1593 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
1594 n = wcrtomb_l(tmp, intern_type(), &st, __l);
1596 n = __wcrtomb_l(tmp, intern_type(), &st, __l);
1598 if (n == size_t(-1)) // on error
1600 if (n > static_cast<size_t>(to_end-to_nxt)) // is there room?
1602 for (extern_type* p = tmp; n; --n) // write it
1605 // look for next null in frm
1606 for (fend = frm_nxt; fend != frm_end; ++fend)
1611 return frm_nxt == frm_end ? ok : partial;
1614 codecvt<wchar_t, char, mbstate_t>::result
1615 codecvt<wchar_t, char, mbstate_t>::do_in(state_type& st,
1616 const extern_type* frm, const extern_type* frm_end, const extern_type*& frm_nxt,
1617 intern_type* to, intern_type* to_end, intern_type*& to_nxt) const
1619 // look for first internal null in frm
1620 const extern_type* fend = frm;
1621 for (; fend != frm_end; ++fend)
1624 // loop over all null-terminated sequences in frm
1626 for (frm_nxt = frm; frm != frm_end && to != to_end; frm = frm_nxt, to = to_nxt)
1628 // save state in case it is needed to recover to_nxt on error
1629 mbstate_t save_state = st;
1630 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
1631 size_t n = mbsnrtowcs_l(to, &frm_nxt, static_cast<size_t>(fend-frm),
1632 static_cast<size_t>(to_end-to), &st, __l);
1634 size_t n = __mbsnrtowcs_l(to, &frm_nxt, fend-frm, to_end-to, &st, __l);
1636 if (n == size_t(-1))
1638 // need to recover to_nxt
1639 for (to_nxt = to; frm != frm_nxt; ++to_nxt)
1641 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
1642 n = mbrtowc_l(to_nxt, frm, static_cast<size_t>(fend-frm),
1645 n = __mbrtowc_l(to_nxt, frm, fend-frm, &save_state, __l);
1664 return frm_nxt == frm_end ? ok : partial;
1666 if (n == size_t(-1))
1669 if (to_nxt == to_end)
1671 if (fend != frm_end) // set up next null terminated sequence
1673 // Try to write the terminating null
1674 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
1675 n = mbrtowc_l(to_nxt, frm_nxt, 1, &st, __l);
1677 n = __mbrtowc_l(to_nxt, frm_nxt, 1, &st, __l);
1679 if (n != 0) // on error
1683 // look for next null in frm
1684 for (fend = frm_nxt; fend != frm_end; ++fend)
1689 return frm_nxt == frm_end ? ok : partial;
1692 codecvt<wchar_t, char, mbstate_t>::result
1693 codecvt<wchar_t, char, mbstate_t>::do_unshift(state_type& st,
1694 extern_type* to, extern_type* to_end, extern_type*& to_nxt) const
1697 extern_type tmp[MB_LEN_MAX];
1698 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
1699 size_t n = wcrtomb_l(tmp, intern_type(), &st, __l);
1701 size_t n = __wcrtomb_l(tmp, intern_type(), &st, __l);
1703 if (n == size_t(-1) || n == 0) // on error
1706 if (n > static_cast<size_t>(to_end-to_nxt)) // is there room?
1708 for (extern_type* p = tmp; n; --n) // write it
1714 codecvt<wchar_t, char, mbstate_t>::do_encoding() const _NOEXCEPT
1716 #ifndef __CloudABI__
1717 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
1718 if (mbtowc_l(nullptr, nullptr, MB_LEN_MAX, __l) != 0)
1720 if (__mbtowc_l(nullptr, nullptr, MB_LEN_MAX, __l) != 0)
1725 // stateless encoding
1726 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
1727 if (__l == 0 || MB_CUR_MAX_L(__l) == 1) // there are no known constant length encodings
1729 if (__l == 0 || __mb_cur_max_l(__l) == 1) // there are no known constant length encodings
1731 return 1; // which take more than 1 char to form a wchar_t
1736 codecvt<wchar_t, char, mbstate_t>::do_always_noconv() const _NOEXCEPT
1742 codecvt<wchar_t, char, mbstate_t>::do_length(state_type& st,
1743 const extern_type* frm, const extern_type* frm_end, size_t mx) const
1746 for (size_t nwchar_t = 0; nwchar_t < mx && frm != frm_end; ++nwchar_t)
1748 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
1749 size_t n = mbrlen_l(frm, static_cast<size_t>(frm_end-frm), &st, __l);
1751 size_t n = __mbrlen_l(frm, frm_end-frm, &st, __l);
1772 codecvt<wchar_t, char, mbstate_t>::do_max_length() const _NOEXCEPT
1774 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
1775 return __l == 0 ? 1 : static_cast<int>( MB_CUR_MAX_L(__l));
1777 return __l == 0 ? 1 : static_cast<int>(__mb_cur_max_l(__l));
1782 // UTF-32 UTF-16 UTF-8 # of code points
1783 // first second first second third fourth
1784 // 000000 - 00007F 0000 - 007F 00 - 7F 127
1785 // 000080 - 0007FF 0080 - 07FF C2 - DF, 80 - BF 1920
1786 // 000800 - 000FFF 0800 - 0FFF E0 - E0, A0 - BF, 80 - BF 2048
1787 // 001000 - 00CFFF 1000 - CFFF E1 - EC, 80 - BF, 80 - BF 49152
1788 // 00D000 - 00D7FF D000 - D7FF ED - ED, 80 - 9F, 80 - BF 2048
1789 // 00D800 - 00DFFF invalid
1790 // 00E000 - 00FFFF E000 - FFFF EE - EF, 80 - BF, 80 - BF 8192
1791 // 010000 - 03FFFF D800 - D8BF, DC00 - DFFF F0 - F0, 90 - BF, 80 - BF, 80 - BF 196608
1792 // 040000 - 0FFFFF D8C0 - DBBF, DC00 - DFFF F1 - F3, 80 - BF, 80 - BF, 80 - BF 786432
1793 // 100000 - 10FFFF DBC0 - DBFF, DC00 - DFFF F4 - F4, 80 - 8F, 80 - BF, 80 - BF 65536
1796 codecvt_base::result
1797 utf16_to_utf8(const uint16_t* frm, const uint16_t* frm_end, const uint16_t*& frm_nxt,
1798 uint8_t* to, uint8_t* to_end, uint8_t*& to_nxt,
1799 unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0))
1803 if (mode & generate_header)
1805 if (to_end-to_nxt < 3)
1806 return codecvt_base::partial;
1807 *to_nxt++ = static_cast<uint8_t>(0xEF);
1808 *to_nxt++ = static_cast<uint8_t>(0xBB);
1809 *to_nxt++ = static_cast<uint8_t>(0xBF);
1811 for (; frm_nxt < frm_end; ++frm_nxt)
1813 uint16_t wc1 = *frm_nxt;
1815 return codecvt_base::error;
1818 if (to_end-to_nxt < 1)
1819 return codecvt_base::partial;
1820 *to_nxt++ = static_cast<uint8_t>(wc1);
1822 else if (wc1 < 0x0800)
1824 if (to_end-to_nxt < 2)
1825 return codecvt_base::partial;
1826 *to_nxt++ = static_cast<uint8_t>(0xC0 | (wc1 >> 6));
1827 *to_nxt++ = static_cast<uint8_t>(0x80 | (wc1 & 0x03F));
1829 else if (wc1 < 0xD800)
1831 if (to_end-to_nxt < 3)
1832 return codecvt_base::partial;
1833 *to_nxt++ = static_cast<uint8_t>(0xE0 | (wc1 >> 12));
1834 *to_nxt++ = static_cast<uint8_t>(0x80 | ((wc1 & 0x0FC0) >> 6));
1835 *to_nxt++ = static_cast<uint8_t>(0x80 | (wc1 & 0x003F));
1837 else if (wc1 < 0xDC00)
1839 if (frm_end-frm_nxt < 2)
1840 return codecvt_base::partial;
1841 uint16_t wc2 = frm_nxt[1];
1842 if ((wc2 & 0xFC00) != 0xDC00)
1843 return codecvt_base::error;
1844 if (to_end-to_nxt < 4)
1845 return codecvt_base::partial;
1846 if (((((wc1 & 0x03C0UL) >> 6) + 1) << 16) +
1847 ((wc1 & 0x003FUL) << 10) + (wc2 & 0x03FF) > Maxcode)
1848 return codecvt_base::error;
1850 uint8_t z = ((wc1 & 0x03C0) >> 6) + 1;
1851 *to_nxt++ = static_cast<uint8_t>(0xF0 | (z >> 2));
1852 *to_nxt++ = static_cast<uint8_t>(0x80 | ((z & 0x03) << 4) | ((wc1 & 0x003C) >> 2));
1853 *to_nxt++ = static_cast<uint8_t>(0x80 | ((wc1 & 0x0003) << 4) | ((wc2 & 0x03C0) >> 6));
1854 *to_nxt++ = static_cast<uint8_t>(0x80 | (wc2 & 0x003F));
1856 else if (wc1 < 0xE000)
1858 return codecvt_base::error;
1862 if (to_end-to_nxt < 3)
1863 return codecvt_base::partial;
1864 *to_nxt++ = static_cast<uint8_t>(0xE0 | (wc1 >> 12));
1865 *to_nxt++ = static_cast<uint8_t>(0x80 | ((wc1 & 0x0FC0) >> 6));
1866 *to_nxt++ = static_cast<uint8_t>(0x80 | (wc1 & 0x003F));
1869 return codecvt_base::ok;
1873 codecvt_base::result
1874 utf16_to_utf8(const uint32_t* frm, const uint32_t* frm_end, const uint32_t*& frm_nxt,
1875 uint8_t* to, uint8_t* to_end, uint8_t*& to_nxt,
1876 unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0))
1880 if (mode & generate_header)
1882 if (to_end-to_nxt < 3)
1883 return codecvt_base::partial;
1884 *to_nxt++ = static_cast<uint8_t>(0xEF);
1885 *to_nxt++ = static_cast<uint8_t>(0xBB);
1886 *to_nxt++ = static_cast<uint8_t>(0xBF);
1888 for (; frm_nxt < frm_end; ++frm_nxt)
1890 uint16_t wc1 = static_cast<uint16_t>(*frm_nxt);
1892 return codecvt_base::error;
1895 if (to_end-to_nxt < 1)
1896 return codecvt_base::partial;
1897 *to_nxt++ = static_cast<uint8_t>(wc1);
1899 else if (wc1 < 0x0800)
1901 if (to_end-to_nxt < 2)
1902 return codecvt_base::partial;
1903 *to_nxt++ = static_cast<uint8_t>(0xC0 | (wc1 >> 6));
1904 *to_nxt++ = static_cast<uint8_t>(0x80 | (wc1 & 0x03F));
1906 else if (wc1 < 0xD800)
1908 if (to_end-to_nxt < 3)
1909 return codecvt_base::partial;
1910 *to_nxt++ = static_cast<uint8_t>(0xE0 | (wc1 >> 12));
1911 *to_nxt++ = static_cast<uint8_t>(0x80 | ((wc1 & 0x0FC0) >> 6));
1912 *to_nxt++ = static_cast<uint8_t>(0x80 | (wc1 & 0x003F));
1914 else if (wc1 < 0xDC00)
1916 if (frm_end-frm_nxt < 2)
1917 return codecvt_base::partial;
1918 uint16_t wc2 = static_cast<uint16_t>(frm_nxt[1]);
1919 if ((wc2 & 0xFC00) != 0xDC00)
1920 return codecvt_base::error;
1921 if (to_end-to_nxt < 4)
1922 return codecvt_base::partial;
1923 if (((((wc1 & 0x03C0UL) >> 6) + 1) << 16) +
1924 ((wc1 & 0x003FUL) << 10) + (wc2 & 0x03FF) > Maxcode)
1925 return codecvt_base::error;
1927 uint8_t z = ((wc1 & 0x03C0) >> 6) + 1;
1928 *to_nxt++ = static_cast<uint8_t>(0xF0 | (z >> 2));
1929 *to_nxt++ = static_cast<uint8_t>(0x80 | ((z & 0x03) << 4) | ((wc1 & 0x003C) >> 2));
1930 *to_nxt++ = static_cast<uint8_t>(0x80 | ((wc1 & 0x0003) << 4) | ((wc2 & 0x03C0) >> 6));
1931 *to_nxt++ = static_cast<uint8_t>(0x80 | (wc2 & 0x003F));
1933 else if (wc1 < 0xE000)
1935 return codecvt_base::error;
1939 if (to_end-to_nxt < 3)
1940 return codecvt_base::partial;
1941 *to_nxt++ = static_cast<uint8_t>(0xE0 | (wc1 >> 12));
1942 *to_nxt++ = static_cast<uint8_t>(0x80 | ((wc1 & 0x0FC0) >> 6));
1943 *to_nxt++ = static_cast<uint8_t>(0x80 | (wc1 & 0x003F));
1946 return codecvt_base::ok;
1950 codecvt_base::result
1951 utf8_to_utf16(const uint8_t* frm, const uint8_t* frm_end, const uint8_t*& frm_nxt,
1952 uint16_t* to, uint16_t* to_end, uint16_t*& to_nxt,
1953 unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0))
1957 if (mode & consume_header)
1959 if (frm_end-frm_nxt >= 3 && frm_nxt[0] == 0xEF && frm_nxt[1] == 0xBB &&
1963 for (; frm_nxt < frm_end && to_nxt < to_end; ++to_nxt)
1965 uint8_t c1 = *frm_nxt;
1967 return codecvt_base::error;
1970 *to_nxt = static_cast<uint16_t>(c1);
1975 return codecvt_base::error;
1979 if (frm_end-frm_nxt < 2)
1980 return codecvt_base::partial;
1981 uint8_t c2 = frm_nxt[1];
1982 if ((c2 & 0xC0) != 0x80)
1983 return codecvt_base::error;
1984 uint16_t t = static_cast<uint16_t>(((c1 & 0x1F) << 6) | (c2 & 0x3F));
1986 return codecvt_base::error;
1992 if (frm_end-frm_nxt < 3)
1993 return codecvt_base::partial;
1994 uint8_t c2 = frm_nxt[1];
1995 uint8_t c3 = frm_nxt[2];
1999 if ((c2 & 0xE0) != 0xA0)
2000 return codecvt_base::error;
2003 if ((c2 & 0xE0) != 0x80)
2004 return codecvt_base::error;
2007 if ((c2 & 0xC0) != 0x80)
2008 return codecvt_base::error;
2011 if ((c3 & 0xC0) != 0x80)
2012 return codecvt_base::error;
2013 uint16_t t = static_cast<uint16_t>(((c1 & 0x0F) << 12)
2014 | ((c2 & 0x3F) << 6)
2017 return codecvt_base::error;
2023 if (frm_end-frm_nxt < 4)
2024 return codecvt_base::partial;
2025 uint8_t c2 = frm_nxt[1];
2026 uint8_t c3 = frm_nxt[2];
2027 uint8_t c4 = frm_nxt[3];
2031 if (!(0x90 <= c2 && c2 <= 0xBF))
2032 return codecvt_base::error;
2035 if ((c2 & 0xF0) != 0x80)
2036 return codecvt_base::error;
2039 if ((c2 & 0xC0) != 0x80)
2040 return codecvt_base::error;
2043 if ((c3 & 0xC0) != 0x80 || (c4 & 0xC0) != 0x80)
2044 return codecvt_base::error;
2045 if (to_end-to_nxt < 2)
2046 return codecvt_base::partial;
2047 if ((((c1 & 7UL) << 18) +
2048 ((c2 & 0x3FUL) << 12) +
2049 ((c3 & 0x3FUL) << 6) + (c4 & 0x3F)) > Maxcode)
2050 return codecvt_base::error;
2051 *to_nxt = static_cast<uint16_t>(
2053 | (((((c1 & 0x07) << 2) | ((c2 & 0x30) >> 4)) - 1) << 6)
2054 | ((c2 & 0x0F) << 2)
2055 | ((c3 & 0x30) >> 4));
2056 *++to_nxt = static_cast<uint16_t>(
2058 | ((c3 & 0x0F) << 6)
2064 return codecvt_base::error;
2067 return frm_nxt < frm_end ? codecvt_base::partial : codecvt_base::ok;
2071 codecvt_base::result
2072 utf8_to_utf16(const uint8_t* frm, const uint8_t* frm_end, const uint8_t*& frm_nxt,
2073 uint32_t* to, uint32_t* to_end, uint32_t*& to_nxt,
2074 unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0))
2078 if (mode & consume_header)
2080 if (frm_end-frm_nxt >= 3 && frm_nxt[0] == 0xEF && frm_nxt[1] == 0xBB &&
2084 for (; frm_nxt < frm_end && to_nxt < to_end; ++to_nxt)
2086 uint8_t c1 = *frm_nxt;
2088 return codecvt_base::error;
2091 *to_nxt = static_cast<uint32_t>(c1);
2096 return codecvt_base::error;
2100 if (frm_end-frm_nxt < 2)
2101 return codecvt_base::partial;
2102 uint8_t c2 = frm_nxt[1];
2103 if ((c2 & 0xC0) != 0x80)
2104 return codecvt_base::error;
2105 uint16_t t = static_cast<uint16_t>(((c1 & 0x1F) << 6) | (c2 & 0x3F));
2107 return codecvt_base::error;
2108 *to_nxt = static_cast<uint32_t>(t);
2113 if (frm_end-frm_nxt < 3)
2114 return codecvt_base::partial;
2115 uint8_t c2 = frm_nxt[1];
2116 uint8_t c3 = frm_nxt[2];
2120 if ((c2 & 0xE0) != 0xA0)
2121 return codecvt_base::error;
2124 if ((c2 & 0xE0) != 0x80)
2125 return codecvt_base::error;
2128 if ((c2 & 0xC0) != 0x80)
2129 return codecvt_base::error;
2132 if ((c3 & 0xC0) != 0x80)
2133 return codecvt_base::error;
2134 uint16_t t = static_cast<uint16_t>(((c1 & 0x0F) << 12)
2135 | ((c2 & 0x3F) << 6)
2138 return codecvt_base::error;
2139 *to_nxt = static_cast<uint32_t>(t);
2144 if (frm_end-frm_nxt < 4)
2145 return codecvt_base::partial;
2146 uint8_t c2 = frm_nxt[1];
2147 uint8_t c3 = frm_nxt[2];
2148 uint8_t c4 = frm_nxt[3];
2152 if (!(0x90 <= c2 && c2 <= 0xBF))
2153 return codecvt_base::error;
2156 if ((c2 & 0xF0) != 0x80)
2157 return codecvt_base::error;
2160 if ((c2 & 0xC0) != 0x80)
2161 return codecvt_base::error;
2164 if ((c3 & 0xC0) != 0x80 || (c4 & 0xC0) != 0x80)
2165 return codecvt_base::error;
2166 if (to_end-to_nxt < 2)
2167 return codecvt_base::partial;
2168 if ((((c1 & 7UL) << 18) +
2169 ((c2 & 0x3FUL) << 12) +
2170 ((c3 & 0x3FUL) << 6) + (c4 & 0x3F)) > Maxcode)
2171 return codecvt_base::error;
2172 *to_nxt = static_cast<uint32_t>(
2174 | (((((c1 & 0x07) << 2) | ((c2 & 0x30) >> 4)) - 1) << 6)
2175 | ((c2 & 0x0F) << 2)
2176 | ((c3 & 0x30) >> 4));
2177 *++to_nxt = static_cast<uint32_t>(
2179 | ((c3 & 0x0F) << 6)
2185 return codecvt_base::error;
2188 return frm_nxt < frm_end ? codecvt_base::partial : codecvt_base::ok;
2193 utf8_to_utf16_length(const uint8_t* frm, const uint8_t* frm_end,
2194 size_t mx, unsigned long Maxcode = 0x10FFFF,
2195 codecvt_mode mode = codecvt_mode(0))
2197 const uint8_t* frm_nxt = frm;
2198 if (mode & consume_header)
2200 if (frm_end-frm_nxt >= 3 && frm_nxt[0] == 0xEF && frm_nxt[1] == 0xBB &&
2204 for (size_t nchar16_t = 0; frm_nxt < frm_end && nchar16_t < mx; ++nchar16_t)
2206 uint8_t c1 = *frm_nxt;
2219 if ((frm_end-frm_nxt < 2) || (frm_nxt[1] & 0xC0) != 0x80)
2221 uint16_t t = static_cast<uint16_t>(((c1 & 0x1F) << 6) | (frm_nxt[1] & 0x3F));
2228 if (frm_end-frm_nxt < 3)
2230 uint8_t c2 = frm_nxt[1];
2231 uint8_t c3 = frm_nxt[2];
2235 if ((c2 & 0xE0) != 0xA0)
2236 return static_cast<int>(frm_nxt - frm);
2239 if ((c2 & 0xE0) != 0x80)
2240 return static_cast<int>(frm_nxt - frm);
2243 if ((c2 & 0xC0) != 0x80)
2244 return static_cast<int>(frm_nxt - frm);
2247 if ((c3 & 0xC0) != 0x80)
2249 if ((((c1 & 0x0Fu) << 12) | ((c2 & 0x3Fu) << 6) | (c3 & 0x3Fu)) > Maxcode)
2255 if (frm_end-frm_nxt < 4 || mx-nchar16_t < 2)
2257 uint8_t c2 = frm_nxt[1];
2258 uint8_t c3 = frm_nxt[2];
2259 uint8_t c4 = frm_nxt[3];
2263 if (!(0x90 <= c2 && c2 <= 0xBF))
2264 return static_cast<int>(frm_nxt - frm);
2267 if ((c2 & 0xF0) != 0x80)
2268 return static_cast<int>(frm_nxt - frm);
2271 if ((c2 & 0xC0) != 0x80)
2272 return static_cast<int>(frm_nxt - frm);
2275 if ((c3 & 0xC0) != 0x80 || (c4 & 0xC0) != 0x80)
2277 if ((((c1 & 7UL) << 18) +
2278 ((c2 & 0x3FUL) << 12) +
2279 ((c3 & 0x3FUL) << 6) + (c4 & 0x3F)) > Maxcode)
2289 return static_cast<int>(frm_nxt - frm);
2293 codecvt_base::result
2294 ucs4_to_utf8(const uint32_t* frm, const uint32_t* frm_end, const uint32_t*& frm_nxt,
2295 uint8_t* to, uint8_t* to_end, uint8_t*& to_nxt,
2296 unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0))
2300 if (mode & generate_header)
2302 if (to_end-to_nxt < 3)
2303 return codecvt_base::partial;
2304 *to_nxt++ = static_cast<uint8_t>(0xEF);
2305 *to_nxt++ = static_cast<uint8_t>(0xBB);
2306 *to_nxt++ = static_cast<uint8_t>(0xBF);
2308 for (; frm_nxt < frm_end; ++frm_nxt)
2310 uint32_t wc = *frm_nxt;
2311 if ((wc & 0xFFFFF800) == 0x00D800 || wc > Maxcode)
2312 return codecvt_base::error;
2315 if (to_end-to_nxt < 1)
2316 return codecvt_base::partial;
2317 *to_nxt++ = static_cast<uint8_t>(wc);
2319 else if (wc < 0x000800)
2321 if (to_end-to_nxt < 2)
2322 return codecvt_base::partial;
2323 *to_nxt++ = static_cast<uint8_t>(0xC0 | (wc >> 6));
2324 *to_nxt++ = static_cast<uint8_t>(0x80 | (wc & 0x03F));
2326 else if (wc < 0x010000)
2328 if (to_end-to_nxt < 3)
2329 return codecvt_base::partial;
2330 *to_nxt++ = static_cast<uint8_t>(0xE0 | (wc >> 12));
2331 *to_nxt++ = static_cast<uint8_t>(0x80 | ((wc & 0x0FC0) >> 6));
2332 *to_nxt++ = static_cast<uint8_t>(0x80 | (wc & 0x003F));
2334 else // if (wc < 0x110000)
2336 if (to_end-to_nxt < 4)
2337 return codecvt_base::partial;
2338 *to_nxt++ = static_cast<uint8_t>(0xF0 | (wc >> 18));
2339 *to_nxt++ = static_cast<uint8_t>(0x80 | ((wc & 0x03F000) >> 12));
2340 *to_nxt++ = static_cast<uint8_t>(0x80 | ((wc & 0x000FC0) >> 6));
2341 *to_nxt++ = static_cast<uint8_t>(0x80 | (wc & 0x00003F));
2344 return codecvt_base::ok;
2348 codecvt_base::result
2349 utf8_to_ucs4(const uint8_t* frm, const uint8_t* frm_end, const uint8_t*& frm_nxt,
2350 uint32_t* to, uint32_t* to_end, uint32_t*& to_nxt,
2351 unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0))
2355 if (mode & consume_header)
2357 if (frm_end-frm_nxt >= 3 && frm_nxt[0] == 0xEF && frm_nxt[1] == 0xBB &&
2361 for (; frm_nxt < frm_end && to_nxt < to_end; ++to_nxt)
2363 uint8_t c1 = static_cast<uint8_t>(*frm_nxt);
2367 return codecvt_base::error;
2368 *to_nxt = static_cast<uint32_t>(c1);
2373 return codecvt_base::error;
2377 if (frm_end-frm_nxt < 2)
2378 return codecvt_base::partial;
2379 uint8_t c2 = frm_nxt[1];
2380 if ((c2 & 0xC0) != 0x80)
2381 return codecvt_base::error;
2382 uint32_t t = static_cast<uint32_t>(((c1 & 0x1F) << 6)
2385 return codecvt_base::error;
2391 if (frm_end-frm_nxt < 3)
2392 return codecvt_base::partial;
2393 uint8_t c2 = frm_nxt[1];
2394 uint8_t c3 = frm_nxt[2];
2398 if ((c2 & 0xE0) != 0xA0)
2399 return codecvt_base::error;
2402 if ((c2 & 0xE0) != 0x80)
2403 return codecvt_base::error;
2406 if ((c2 & 0xC0) != 0x80)
2407 return codecvt_base::error;
2410 if ((c3 & 0xC0) != 0x80)
2411 return codecvt_base::error;
2412 uint32_t t = static_cast<uint32_t>(((c1 & 0x0F) << 12)
2413 | ((c2 & 0x3F) << 6)
2416 return codecvt_base::error;
2422 if (frm_end-frm_nxt < 4)
2423 return codecvt_base::partial;
2424 uint8_t c2 = frm_nxt[1];
2425 uint8_t c3 = frm_nxt[2];
2426 uint8_t c4 = frm_nxt[3];
2430 if (!(0x90 <= c2 && c2 <= 0xBF))
2431 return codecvt_base::error;
2434 if ((c2 & 0xF0) != 0x80)
2435 return codecvt_base::error;
2438 if ((c2 & 0xC0) != 0x80)
2439 return codecvt_base::error;
2442 if ((c3 & 0xC0) != 0x80 || (c4 & 0xC0) != 0x80)
2443 return codecvt_base::error;
2444 uint32_t t = static_cast<uint32_t>(((c1 & 0x07) << 18)
2445 | ((c2 & 0x3F) << 12)
2446 | ((c3 & 0x3F) << 6)
2449 return codecvt_base::error;
2455 return codecvt_base::error;
2458 return frm_nxt < frm_end ? codecvt_base::partial : codecvt_base::ok;
2463 utf8_to_ucs4_length(const uint8_t* frm, const uint8_t* frm_end,
2464 size_t mx, unsigned long Maxcode = 0x10FFFF,
2465 codecvt_mode mode = codecvt_mode(0))
2467 const uint8_t* frm_nxt = frm;
2468 if (mode & consume_header)
2470 if (frm_end-frm_nxt >= 3 && frm_nxt[0] == 0xEF && frm_nxt[1] == 0xBB &&
2474 for (size_t nchar32_t = 0; frm_nxt < frm_end && nchar32_t < mx; ++nchar32_t)
2476 uint8_t c1 = static_cast<uint8_t>(*frm_nxt);
2489 if ((frm_end-frm_nxt < 2) || ((frm_nxt[1] & 0xC0) != 0x80))
2491 if ((((c1 & 0x1Fu) << 6) | (frm_nxt[1] & 0x3Fu)) > Maxcode)
2497 if (frm_end-frm_nxt < 3)
2499 uint8_t c2 = frm_nxt[1];
2500 uint8_t c3 = frm_nxt[2];
2504 if ((c2 & 0xE0) != 0xA0)
2505 return static_cast<int>(frm_nxt - frm);
2508 if ((c2 & 0xE0) != 0x80)
2509 return static_cast<int>(frm_nxt - frm);
2512 if ((c2 & 0xC0) != 0x80)
2513 return static_cast<int>(frm_nxt - frm);
2516 if ((c3 & 0xC0) != 0x80)
2518 if ((((c1 & 0x0Fu) << 12) | ((c2 & 0x3Fu) << 6) | (c3 & 0x3Fu)) > Maxcode)
2524 if (frm_end-frm_nxt < 4)
2526 uint8_t c2 = frm_nxt[1];
2527 uint8_t c3 = frm_nxt[2];
2528 uint8_t c4 = frm_nxt[3];
2532 if (!(0x90 <= c2 && c2 <= 0xBF))
2533 return static_cast<int>(frm_nxt - frm);
2536 if ((c2 & 0xF0) != 0x80)
2537 return static_cast<int>(frm_nxt - frm);
2540 if ((c2 & 0xC0) != 0x80)
2541 return static_cast<int>(frm_nxt - frm);
2544 if ((c3 & 0xC0) != 0x80 || (c4 & 0xC0) != 0x80)
2546 if ((((c1 & 0x07u) << 18) | ((c2 & 0x3Fu) << 12) |
2547 ((c3 & 0x3Fu) << 6) | (c4 & 0x3Fu)) > Maxcode)
2556 return static_cast<int>(frm_nxt - frm);
2560 codecvt_base::result
2561 ucs2_to_utf8(const uint16_t* frm, const uint16_t* frm_end, const uint16_t*& frm_nxt,
2562 uint8_t* to, uint8_t* to_end, uint8_t*& to_nxt,
2563 unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0))
2567 if (mode & generate_header)
2569 if (to_end-to_nxt < 3)
2570 return codecvt_base::partial;
2571 *to_nxt++ = static_cast<uint8_t>(0xEF);
2572 *to_nxt++ = static_cast<uint8_t>(0xBB);
2573 *to_nxt++ = static_cast<uint8_t>(0xBF);
2575 for (; frm_nxt < frm_end; ++frm_nxt)
2577 uint16_t wc = *frm_nxt;
2578 if ((wc & 0xF800) == 0xD800 || wc > Maxcode)
2579 return codecvt_base::error;
2582 if (to_end-to_nxt < 1)
2583 return codecvt_base::partial;
2584 *to_nxt++ = static_cast<uint8_t>(wc);
2586 else if (wc < 0x0800)
2588 if (to_end-to_nxt < 2)
2589 return codecvt_base::partial;
2590 *to_nxt++ = static_cast<uint8_t>(0xC0 | (wc >> 6));
2591 *to_nxt++ = static_cast<uint8_t>(0x80 | (wc & 0x03F));
2593 else // if (wc <= 0xFFFF)
2595 if (to_end-to_nxt < 3)
2596 return codecvt_base::partial;
2597 *to_nxt++ = static_cast<uint8_t>(0xE0 | (wc >> 12));
2598 *to_nxt++ = static_cast<uint8_t>(0x80 | ((wc & 0x0FC0) >> 6));
2599 *to_nxt++ = static_cast<uint8_t>(0x80 | (wc & 0x003F));
2602 return codecvt_base::ok;
2606 codecvt_base::result
2607 utf8_to_ucs2(const uint8_t* frm, const uint8_t* frm_end, const uint8_t*& frm_nxt,
2608 uint16_t* to, uint16_t* to_end, uint16_t*& to_nxt,
2609 unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0))
2613 if (mode & consume_header)
2615 if (frm_end-frm_nxt >= 3 && frm_nxt[0] == 0xEF && frm_nxt[1] == 0xBB &&
2619 for (; frm_nxt < frm_end && to_nxt < to_end; ++to_nxt)
2621 uint8_t c1 = static_cast<uint8_t>(*frm_nxt);
2625 return codecvt_base::error;
2626 *to_nxt = static_cast<uint16_t>(c1);
2631 return codecvt_base::error;
2635 if (frm_end-frm_nxt < 2)
2636 return codecvt_base::partial;
2637 uint8_t c2 = frm_nxt[1];
2638 if ((c2 & 0xC0) != 0x80)
2639 return codecvt_base::error;
2640 uint16_t t = static_cast<uint16_t>(((c1 & 0x1F) << 6)
2643 return codecvt_base::error;
2649 if (frm_end-frm_nxt < 3)
2650 return codecvt_base::partial;
2651 uint8_t c2 = frm_nxt[1];
2652 uint8_t c3 = frm_nxt[2];
2656 if ((c2 & 0xE0) != 0xA0)
2657 return codecvt_base::error;
2660 if ((c2 & 0xE0) != 0x80)
2661 return codecvt_base::error;
2664 if ((c2 & 0xC0) != 0x80)
2665 return codecvt_base::error;
2668 if ((c3 & 0xC0) != 0x80)
2669 return codecvt_base::error;
2670 uint16_t t = static_cast<uint16_t>(((c1 & 0x0F) << 12)
2671 | ((c2 & 0x3F) << 6)
2674 return codecvt_base::error;
2680 return codecvt_base::error;
2683 return frm_nxt < frm_end ? codecvt_base::partial : codecvt_base::ok;
2688 utf8_to_ucs2_length(const uint8_t* frm, const uint8_t* frm_end,
2689 size_t mx, unsigned long Maxcode = 0x10FFFF,
2690 codecvt_mode mode = codecvt_mode(0))
2692 const uint8_t* frm_nxt = frm;
2693 if (mode & consume_header)
2695 if (frm_end-frm_nxt >= 3 && frm_nxt[0] == 0xEF && frm_nxt[1] == 0xBB &&
2699 for (size_t nchar32_t = 0; frm_nxt < frm_end && nchar32_t < mx; ++nchar32_t)
2701 uint8_t c1 = static_cast<uint8_t>(*frm_nxt);
2714 if ((frm_end-frm_nxt < 2) || ((frm_nxt[1] & 0xC0) != 0x80))
2716 if ((((c1 & 0x1Fu) << 6) | (frm_nxt[1] & 0x3Fu)) > Maxcode)
2722 if (frm_end-frm_nxt < 3)
2724 uint8_t c2 = frm_nxt[1];
2725 uint8_t c3 = frm_nxt[2];
2729 if ((c2 & 0xE0) != 0xA0)
2730 return static_cast<int>(frm_nxt - frm);
2733 if ((c2 & 0xE0) != 0x80)
2734 return static_cast<int>(frm_nxt - frm);
2737 if ((c2 & 0xC0) != 0x80)
2738 return static_cast<int>(frm_nxt - frm);
2741 if ((c3 & 0xC0) != 0x80)
2743 if ((((c1 & 0x0Fu) << 12) | ((c2 & 0x3Fu) << 6) | (c3 & 0x3Fu)) > Maxcode)
2752 return static_cast<int>(frm_nxt - frm);
2756 codecvt_base::result
2757 ucs4_to_utf16be(const uint32_t* frm, const uint32_t* frm_end, const uint32_t*& frm_nxt,
2758 uint8_t* to, uint8_t* to_end, uint8_t*& to_nxt,
2759 unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0))
2763 if (mode & generate_header)
2765 if (to_end-to_nxt < 2)
2766 return codecvt_base::partial;
2767 *to_nxt++ = static_cast<uint8_t>(0xFE);
2768 *to_nxt++ = static_cast<uint8_t>(0xFF);
2770 for (; frm_nxt < frm_end; ++frm_nxt)
2772 uint32_t wc = *frm_nxt;
2773 if ((wc & 0xFFFFF800) == 0x00D800 || wc > Maxcode)
2774 return codecvt_base::error;
2777 if (to_end-to_nxt < 2)
2778 return codecvt_base::partial;
2779 *to_nxt++ = static_cast<uint8_t>(wc >> 8);
2780 *to_nxt++ = static_cast<uint8_t>(wc);
2784 if (to_end-to_nxt < 4)
2785 return codecvt_base::partial;
2786 uint16_t t = static_cast<uint16_t>(
2788 | ((((wc & 0x1F0000) >> 16) - 1) << 6)
2789 | ((wc & 0x00FC00) >> 10));
2790 *to_nxt++ = static_cast<uint8_t>(t >> 8);
2791 *to_nxt++ = static_cast<uint8_t>(t);
2792 t = static_cast<uint16_t>(0xDC00 | (wc & 0x03FF));
2793 *to_nxt++ = static_cast<uint8_t>(t >> 8);
2794 *to_nxt++ = static_cast<uint8_t>(t);
2797 return codecvt_base::ok;
2801 codecvt_base::result
2802 utf16be_to_ucs4(const uint8_t* frm, const uint8_t* frm_end, const uint8_t*& frm_nxt,
2803 uint32_t* to, uint32_t* to_end, uint32_t*& to_nxt,
2804 unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0))
2808 if (mode & consume_header)
2810 if (frm_end-frm_nxt >= 2 && frm_nxt[0] == 0xFE && frm_nxt[1] == 0xFF)
2813 for (; frm_nxt < frm_end - 1 && to_nxt < to_end; ++to_nxt)
2815 uint16_t c1 = static_cast<uint16_t>(frm_nxt[0] << 8 | frm_nxt[1]);
2816 if ((c1 & 0xFC00) == 0xDC00)
2817 return codecvt_base::error;
2818 if ((c1 & 0xFC00) != 0xD800)
2821 return codecvt_base::error;
2822 *to_nxt = static_cast<uint32_t>(c1);
2827 if (frm_end-frm_nxt < 4)
2828 return codecvt_base::partial;
2829 uint16_t c2 = static_cast<uint16_t>(frm_nxt[2] << 8 | frm_nxt[3]);
2830 if ((c2 & 0xFC00) != 0xDC00)
2831 return codecvt_base::error;
2832 uint32_t t = static_cast<uint32_t>(
2833 ((((c1 & 0x03C0) >> 6) + 1) << 16)
2834 | ((c1 & 0x003F) << 10)
2837 return codecvt_base::error;
2842 return frm_nxt < frm_end ? codecvt_base::partial : codecvt_base::ok;
2847 utf16be_to_ucs4_length(const uint8_t* frm, const uint8_t* frm_end,
2848 size_t mx, unsigned long Maxcode = 0x10FFFF,
2849 codecvt_mode mode = codecvt_mode(0))
2851 const uint8_t* frm_nxt = frm;
2852 if (mode & consume_header)
2854 if (frm_end-frm_nxt >= 2 && frm_nxt[0] == 0xFE && frm_nxt[1] == 0xFF)
2857 for (size_t nchar32_t = 0; frm_nxt < frm_end - 1 && nchar32_t < mx; ++nchar32_t)
2859 uint16_t c1 = static_cast<uint16_t>(frm_nxt[0] << 8 | frm_nxt[1]);
2860 if ((c1 & 0xFC00) == 0xDC00)
2862 if ((c1 & 0xFC00) != 0xD800)
2870 if (frm_end-frm_nxt < 4)
2872 uint16_t c2 = static_cast<uint16_t>(frm_nxt[2] << 8 | frm_nxt[3]);
2873 if ((c2 & 0xFC00) != 0xDC00)
2875 uint32_t t = static_cast<uint32_t>(
2876 ((((c1 & 0x03C0) >> 6) + 1) << 16)
2877 | ((c1 & 0x003F) << 10)
2884 return static_cast<int>(frm_nxt - frm);
2888 codecvt_base::result
2889 ucs4_to_utf16le(const uint32_t* frm, const uint32_t* frm_end, const uint32_t*& frm_nxt,
2890 uint8_t* to, uint8_t* to_end, uint8_t*& to_nxt,
2891 unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0))
2895 if (mode & generate_header)
2897 if (to_end-to_nxt < 2)
2898 return codecvt_base::partial;
2899 *to_nxt++ = static_cast<uint8_t>(0xFF);
2900 *to_nxt++ = static_cast<uint8_t>(0xFE);
2902 for (; frm_nxt < frm_end; ++frm_nxt)
2904 uint32_t wc = *frm_nxt;
2905 if ((wc & 0xFFFFF800) == 0x00D800 || wc > Maxcode)
2906 return codecvt_base::error;
2909 if (to_end-to_nxt < 2)
2910 return codecvt_base::partial;
2911 *to_nxt++ = static_cast<uint8_t>(wc);
2912 *to_nxt++ = static_cast<uint8_t>(wc >> 8);
2916 if (to_end-to_nxt < 4)
2917 return codecvt_base::partial;
2918 uint16_t t = static_cast<uint16_t>(
2920 | ((((wc & 0x1F0000) >> 16) - 1) << 6)
2921 | ((wc & 0x00FC00) >> 10));
2922 *to_nxt++ = static_cast<uint8_t>(t);
2923 *to_nxt++ = static_cast<uint8_t>(t >> 8);
2924 t = static_cast<uint16_t>(0xDC00 | (wc & 0x03FF));
2925 *to_nxt++ = static_cast<uint8_t>(t);
2926 *to_nxt++ = static_cast<uint8_t>(t >> 8);
2929 return codecvt_base::ok;
2933 codecvt_base::result
2934 utf16le_to_ucs4(const uint8_t* frm, const uint8_t* frm_end, const uint8_t*& frm_nxt,
2935 uint32_t* to, uint32_t* to_end, uint32_t*& to_nxt,
2936 unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0))
2940 if (mode & consume_header)
2942 if (frm_end-frm_nxt >= 2 && frm_nxt[0] == 0xFF && frm_nxt[1] == 0xFE)
2945 for (; frm_nxt < frm_end - 1 && to_nxt < to_end; ++to_nxt)
2947 uint16_t c1 = static_cast<uint16_t>(frm_nxt[1] << 8 | frm_nxt[0]);
2948 if ((c1 & 0xFC00) == 0xDC00)
2949 return codecvt_base::error;
2950 if ((c1 & 0xFC00) != 0xD800)
2953 return codecvt_base::error;
2954 *to_nxt = static_cast<uint32_t>(c1);
2959 if (frm_end-frm_nxt < 4)
2960 return codecvt_base::partial;
2961 uint16_t c2 = static_cast<uint16_t>(frm_nxt[3] << 8 | frm_nxt[2]);
2962 if ((c2 & 0xFC00) != 0xDC00)
2963 return codecvt_base::error;
2964 uint32_t t = static_cast<uint32_t>(
2965 ((((c1 & 0x03C0) >> 6) + 1) << 16)
2966 | ((c1 & 0x003F) << 10)
2969 return codecvt_base::error;
2974 return frm_nxt < frm_end ? codecvt_base::partial : codecvt_base::ok;
2979 utf16le_to_ucs4_length(const uint8_t* frm, const uint8_t* frm_end,
2980 size_t mx, unsigned long Maxcode = 0x10FFFF,
2981 codecvt_mode mode = codecvt_mode(0))
2983 const uint8_t* frm_nxt = frm;
2984 if (mode & consume_header)
2986 if (frm_end-frm_nxt >= 2 && frm_nxt[0] == 0xFF && frm_nxt[1] == 0xFE)
2989 for (size_t nchar32_t = 0; frm_nxt < frm_end - 1 && nchar32_t < mx; ++nchar32_t)
2991 uint16_t c1 = static_cast<uint16_t>(frm_nxt[1] << 8 | frm_nxt[0]);
2992 if ((c1 & 0xFC00) == 0xDC00)
2994 if ((c1 & 0xFC00) != 0xD800)
3002 if (frm_end-frm_nxt < 4)
3004 uint16_t c2 = static_cast<uint16_t>(frm_nxt[3] << 8 | frm_nxt[2]);
3005 if ((c2 & 0xFC00) != 0xDC00)
3007 uint32_t t = static_cast<uint32_t>(
3008 ((((c1 & 0x03C0) >> 6) + 1) << 16)
3009 | ((c1 & 0x003F) << 10)
3016 return static_cast<int>(frm_nxt - frm);
3020 codecvt_base::result
3021 ucs2_to_utf16be(const uint16_t* frm, const uint16_t* frm_end, const uint16_t*& frm_nxt,
3022 uint8_t* to, uint8_t* to_end, uint8_t*& to_nxt,
3023 unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0))
3027 if (mode & generate_header)
3029 if (to_end-to_nxt < 2)
3030 return codecvt_base::partial;
3031 *to_nxt++ = static_cast<uint8_t>(0xFE);
3032 *to_nxt++ = static_cast<uint8_t>(0xFF);
3034 for (; frm_nxt < frm_end; ++frm_nxt)
3036 uint16_t wc = *frm_nxt;
3037 if ((wc & 0xF800) == 0xD800 || wc > Maxcode)
3038 return codecvt_base::error;
3039 if (to_end-to_nxt < 2)
3040 return codecvt_base::partial;
3041 *to_nxt++ = static_cast<uint8_t>(wc >> 8);
3042 *to_nxt++ = static_cast<uint8_t>(wc);
3044 return codecvt_base::ok;
3048 codecvt_base::result
3049 utf16be_to_ucs2(const uint8_t* frm, const uint8_t* frm_end, const uint8_t*& frm_nxt,
3050 uint16_t* to, uint16_t* to_end, uint16_t*& to_nxt,
3051 unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0))
3055 if (mode & consume_header)
3057 if (frm_end-frm_nxt >= 2 && frm_nxt[0] == 0xFE && frm_nxt[1] == 0xFF)
3060 for (; frm_nxt < frm_end - 1 && to_nxt < to_end; ++to_nxt)
3062 uint16_t c1 = static_cast<uint16_t>(frm_nxt[0] << 8 | frm_nxt[1]);
3063 if ((c1 & 0xF800) == 0xD800 || c1 > Maxcode)
3064 return codecvt_base::error;
3068 return frm_nxt < frm_end ? codecvt_base::partial : codecvt_base::ok;
3073 utf16be_to_ucs2_length(const uint8_t* frm, const uint8_t* frm_end,
3074 size_t mx, unsigned long Maxcode = 0x10FFFF,
3075 codecvt_mode mode = codecvt_mode(0))
3077 const uint8_t* frm_nxt = frm;
3078 if (mode & consume_header)
3080 if (frm_end-frm_nxt >= 2 && frm_nxt[0] == 0xFE && frm_nxt[1] == 0xFF)
3083 for (size_t nchar16_t = 0; frm_nxt < frm_end - 1 && nchar16_t < mx; ++nchar16_t)
3085 uint16_t c1 = static_cast<uint16_t>(frm_nxt[0] << 8 | frm_nxt[1]);
3086 if ((c1 & 0xF800) == 0xD800 || c1 > Maxcode)
3090 return static_cast<int>(frm_nxt - frm);
3094 codecvt_base::result
3095 ucs2_to_utf16le(const uint16_t* frm, const uint16_t* frm_end, const uint16_t*& frm_nxt,
3096 uint8_t* to, uint8_t* to_end, uint8_t*& to_nxt,
3097 unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0))
3101 if (mode & generate_header)
3103 if (to_end-to_nxt < 2)
3104 return codecvt_base::partial;
3105 *to_nxt++ = static_cast<uint8_t>(0xFF);
3106 *to_nxt++ = static_cast<uint8_t>(0xFE);
3108 for (; frm_nxt < frm_end; ++frm_nxt)
3110 uint16_t wc = *frm_nxt;
3111 if ((wc & 0xF800) == 0xD800 || wc > Maxcode)
3112 return codecvt_base::error;
3113 if (to_end-to_nxt < 2)
3114 return codecvt_base::partial;
3115 *to_nxt++ = static_cast<uint8_t>(wc);
3116 *to_nxt++ = static_cast<uint8_t>(wc >> 8);
3118 return codecvt_base::ok;
3122 codecvt_base::result
3123 utf16le_to_ucs2(const uint8_t* frm, const uint8_t* frm_end, const uint8_t*& frm_nxt,
3124 uint16_t* to, uint16_t* to_end, uint16_t*& to_nxt,
3125 unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0))
3129 if (mode & consume_header)
3131 if (frm_end-frm_nxt >= 2 && frm_nxt[0] == 0xFF && frm_nxt[1] == 0xFE)
3134 for (; frm_nxt < frm_end - 1 && to_nxt < to_end; ++to_nxt)
3136 uint16_t c1 = static_cast<uint16_t>(frm_nxt[1] << 8 | frm_nxt[0]);
3137 if ((c1 & 0xF800) == 0xD800 || c1 > Maxcode)
3138 return codecvt_base::error;
3142 return frm_nxt < frm_end ? codecvt_base::partial : codecvt_base::ok;
3147 utf16le_to_ucs2_length(const uint8_t* frm, const uint8_t* frm_end,
3148 size_t mx, unsigned long Maxcode = 0x10FFFF,
3149 codecvt_mode mode = codecvt_mode(0))
3151 const uint8_t* frm_nxt = frm;
3153 if (mode & consume_header)
3155 if (frm_end-frm_nxt >= 2 && frm_nxt[0] == 0xFF && frm_nxt[1] == 0xFE)
3158 for (size_t nchar16_t = 0; frm_nxt < frm_end - 1 && nchar16_t < mx; ++nchar16_t)
3160 uint16_t c1 = static_cast<uint16_t>(frm_nxt[1] << 8 | frm_nxt[0]);
3161 if ((c1 & 0xF800) == 0xD800 || c1 > Maxcode)
3165 return static_cast<int>(frm_nxt - frm);
3168 // template <> class codecvt<char16_t, char, mbstate_t>
3170 locale::id codecvt<char16_t, char, mbstate_t>::id;
3172 codecvt<char16_t, char, mbstate_t>::~codecvt()
3176 codecvt<char16_t, char, mbstate_t>::result
3177 codecvt<char16_t, char, mbstate_t>::do_out(state_type&,
3178 const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt,
3179 extern_type* to, extern_type* to_end, extern_type*& to_nxt) const
3181 const uint16_t* _frm = reinterpret_cast<const uint16_t*>(frm);
3182 const uint16_t* _frm_end = reinterpret_cast<const uint16_t*>(frm_end);
3183 const uint16_t* _frm_nxt = _frm;
3184 uint8_t* _to = reinterpret_cast<uint8_t*>(to);
3185 uint8_t* _to_end = reinterpret_cast<uint8_t*>(to_end);
3186 uint8_t* _to_nxt = _to;
3187 result r = utf16_to_utf8(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt);
3188 frm_nxt = frm + (_frm_nxt - _frm);
3189 to_nxt = to + (_to_nxt - _to);
3193 codecvt<char16_t, char, mbstate_t>::result
3194 codecvt<char16_t, char, mbstate_t>::do_in(state_type&,
3195 const extern_type* frm, const extern_type* frm_end, const extern_type*& frm_nxt,
3196 intern_type* to, intern_type* to_end, intern_type*& to_nxt) const
3198 const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3199 const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3200 const uint8_t* _frm_nxt = _frm;
3201 uint16_t* _to = reinterpret_cast<uint16_t*>(to);
3202 uint16_t* _to_end = reinterpret_cast<uint16_t*>(to_end);
3203 uint16_t* _to_nxt = _to;
3204 result r = utf8_to_utf16(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt);
3205 frm_nxt = frm + (_frm_nxt - _frm);
3206 to_nxt = to + (_to_nxt - _to);
3210 codecvt<char16_t, char, mbstate_t>::result
3211 codecvt<char16_t, char, mbstate_t>::do_unshift(state_type&,
3212 extern_type* to, extern_type*, extern_type*& to_nxt) const
3219 codecvt<char16_t, char, mbstate_t>::do_encoding() const _NOEXCEPT
3225 codecvt<char16_t, char, mbstate_t>::do_always_noconv() const _NOEXCEPT
3231 codecvt<char16_t, char, mbstate_t>::do_length(state_type&,
3232 const extern_type* frm, const extern_type* frm_end, size_t mx) const
3234 const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3235 const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3236 return utf8_to_utf16_length(_frm, _frm_end, mx);
3240 codecvt<char16_t, char, mbstate_t>::do_max_length() const _NOEXCEPT
3245 // template <> class codecvt<char32_t, char, mbstate_t>
3247 locale::id codecvt<char32_t, char, mbstate_t>::id;
3249 codecvt<char32_t, char, mbstate_t>::~codecvt()
3253 codecvt<char32_t, char, mbstate_t>::result
3254 codecvt<char32_t, char, mbstate_t>::do_out(state_type&,
3255 const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt,
3256 extern_type* to, extern_type* to_end, extern_type*& to_nxt) const
3258 const uint32_t* _frm = reinterpret_cast<const uint32_t*>(frm);
3259 const uint32_t* _frm_end = reinterpret_cast<const uint32_t*>(frm_end);
3260 const uint32_t* _frm_nxt = _frm;
3261 uint8_t* _to = reinterpret_cast<uint8_t*>(to);
3262 uint8_t* _to_end = reinterpret_cast<uint8_t*>(to_end);
3263 uint8_t* _to_nxt = _to;
3264 result r = ucs4_to_utf8(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt);
3265 frm_nxt = frm + (_frm_nxt - _frm);
3266 to_nxt = to + (_to_nxt - _to);
3270 codecvt<char32_t, char, mbstate_t>::result
3271 codecvt<char32_t, char, mbstate_t>::do_in(state_type&,
3272 const extern_type* frm, const extern_type* frm_end, const extern_type*& frm_nxt,
3273 intern_type* to, intern_type* to_end, intern_type*& to_nxt) const
3275 const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3276 const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3277 const uint8_t* _frm_nxt = _frm;
3278 uint32_t* _to = reinterpret_cast<uint32_t*>(to);
3279 uint32_t* _to_end = reinterpret_cast<uint32_t*>(to_end);
3280 uint32_t* _to_nxt = _to;
3281 result r = utf8_to_ucs4(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt);
3282 frm_nxt = frm + (_frm_nxt - _frm);
3283 to_nxt = to + (_to_nxt - _to);
3287 codecvt<char32_t, char, mbstate_t>::result
3288 codecvt<char32_t, char, mbstate_t>::do_unshift(state_type&,
3289 extern_type* to, extern_type*, extern_type*& to_nxt) const
3296 codecvt<char32_t, char, mbstate_t>::do_encoding() const _NOEXCEPT
3302 codecvt<char32_t, char, mbstate_t>::do_always_noconv() const _NOEXCEPT
3308 codecvt<char32_t, char, mbstate_t>::do_length(state_type&,
3309 const extern_type* frm, const extern_type* frm_end, size_t mx) const
3311 const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3312 const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3313 return utf8_to_ucs4_length(_frm, _frm_end, mx);
3317 codecvt<char32_t, char, mbstate_t>::do_max_length() const _NOEXCEPT
3322 // __codecvt_utf8<wchar_t>
3324 __codecvt_utf8<wchar_t>::result
3325 __codecvt_utf8<wchar_t>::do_out(state_type&,
3326 const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt,
3327 extern_type* to, extern_type* to_end, extern_type*& to_nxt) const
3330 const uint16_t* _frm = reinterpret_cast<const uint16_t*>(frm);
3331 const uint16_t* _frm_end = reinterpret_cast<const uint16_t*>(frm_end);
3332 const uint16_t* _frm_nxt = _frm;
3334 const uint32_t* _frm = reinterpret_cast<const uint32_t*>(frm);
3335 const uint32_t* _frm_end = reinterpret_cast<const uint32_t*>(frm_end);
3336 const uint32_t* _frm_nxt = _frm;
3338 uint8_t* _to = reinterpret_cast<uint8_t*>(to);
3339 uint8_t* _to_end = reinterpret_cast<uint8_t*>(to_end);
3340 uint8_t* _to_nxt = _to;
3342 result r = ucs2_to_utf8(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3345 result r = ucs4_to_utf8(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3348 frm_nxt = frm + (_frm_nxt - _frm);
3349 to_nxt = to + (_to_nxt - _to);
3353 __codecvt_utf8<wchar_t>::result
3354 __codecvt_utf8<wchar_t>::do_in(state_type&,
3355 const extern_type* frm, const extern_type* frm_end, const extern_type*& frm_nxt,
3356 intern_type* to, intern_type* to_end, intern_type*& to_nxt) const
3358 const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3359 const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3360 const uint8_t* _frm_nxt = _frm;
3362 uint16_t* _to = reinterpret_cast<uint16_t*>(to);
3363 uint16_t* _to_end = reinterpret_cast<uint16_t*>(to_end);
3364 uint16_t* _to_nxt = _to;
3365 result r = utf8_to_ucs2(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3368 uint32_t* _to = reinterpret_cast<uint32_t*>(to);
3369 uint32_t* _to_end = reinterpret_cast<uint32_t*>(to_end);
3370 uint32_t* _to_nxt = _to;
3371 result r = utf8_to_ucs4(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3374 frm_nxt = frm + (_frm_nxt - _frm);
3375 to_nxt = to + (_to_nxt - _to);
3379 __codecvt_utf8<wchar_t>::result
3380 __codecvt_utf8<wchar_t>::do_unshift(state_type&,
3381 extern_type* to, extern_type*, extern_type*& to_nxt) const
3388 __codecvt_utf8<wchar_t>::do_encoding() const _NOEXCEPT
3394 __codecvt_utf8<wchar_t>::do_always_noconv() const _NOEXCEPT
3400 __codecvt_utf8<wchar_t>::do_length(state_type&,
3401 const extern_type* frm, const extern_type* frm_end, size_t mx) const
3403 const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3404 const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3405 return utf8_to_ucs4_length(_frm, _frm_end, mx, _Maxcode_, _Mode_);
3409 __codecvt_utf8<wchar_t>::do_max_length() const _NOEXCEPT
3411 if (_Mode_ & consume_header)
3416 // __codecvt_utf8<char16_t>
3418 __codecvt_utf8<char16_t>::result
3419 __codecvt_utf8<char16_t>::do_out(state_type&,
3420 const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt,
3421 extern_type* to, extern_type* to_end, extern_type*& to_nxt) const
3423 const uint16_t* _frm = reinterpret_cast<const uint16_t*>(frm);
3424 const uint16_t* _frm_end = reinterpret_cast<const uint16_t*>(frm_end);
3425 const uint16_t* _frm_nxt = _frm;
3426 uint8_t* _to = reinterpret_cast<uint8_t*>(to);
3427 uint8_t* _to_end = reinterpret_cast<uint8_t*>(to_end);
3428 uint8_t* _to_nxt = _to;
3429 result r = ucs2_to_utf8(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3431 frm_nxt = frm + (_frm_nxt - _frm);
3432 to_nxt = to + (_to_nxt - _to);
3436 __codecvt_utf8<char16_t>::result
3437 __codecvt_utf8<char16_t>::do_in(state_type&,
3438 const extern_type* frm, const extern_type* frm_end, const extern_type*& frm_nxt,
3439 intern_type* to, intern_type* to_end, intern_type*& to_nxt) const
3441 const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3442 const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3443 const uint8_t* _frm_nxt = _frm;
3444 uint16_t* _to = reinterpret_cast<uint16_t*>(to);
3445 uint16_t* _to_end = reinterpret_cast<uint16_t*>(to_end);
3446 uint16_t* _to_nxt = _to;
3447 result r = utf8_to_ucs2(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3449 frm_nxt = frm + (_frm_nxt - _frm);
3450 to_nxt = to + (_to_nxt - _to);
3454 __codecvt_utf8<char16_t>::result
3455 __codecvt_utf8<char16_t>::do_unshift(state_type&,
3456 extern_type* to, extern_type*, extern_type*& to_nxt) const
3463 __codecvt_utf8<char16_t>::do_encoding() const _NOEXCEPT
3469 __codecvt_utf8<char16_t>::do_always_noconv() const _NOEXCEPT
3475 __codecvt_utf8<char16_t>::do_length(state_type&,
3476 const extern_type* frm, const extern_type* frm_end, size_t mx) const
3478 const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3479 const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3480 return utf8_to_ucs2_length(_frm, _frm_end, mx, _Maxcode_, _Mode_);
3484 __codecvt_utf8<char16_t>::do_max_length() const _NOEXCEPT
3486 if (_Mode_ & consume_header)
3491 // __codecvt_utf8<char32_t>
3493 __codecvt_utf8<char32_t>::result
3494 __codecvt_utf8<char32_t>::do_out(state_type&,
3495 const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt,
3496 extern_type* to, extern_type* to_end, extern_type*& to_nxt) const
3498 const uint32_t* _frm = reinterpret_cast<const uint32_t*>(frm);
3499 const uint32_t* _frm_end = reinterpret_cast<const uint32_t*>(frm_end);
3500 const uint32_t* _frm_nxt = _frm;
3501 uint8_t* _to = reinterpret_cast<uint8_t*>(to);
3502 uint8_t* _to_end = reinterpret_cast<uint8_t*>(to_end);
3503 uint8_t* _to_nxt = _to;
3504 result r = ucs4_to_utf8(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3506 frm_nxt = frm + (_frm_nxt - _frm);
3507 to_nxt = to + (_to_nxt - _to);
3511 __codecvt_utf8<char32_t>::result
3512 __codecvt_utf8<char32_t>::do_in(state_type&,
3513 const extern_type* frm, const extern_type* frm_end, const extern_type*& frm_nxt,
3514 intern_type* to, intern_type* to_end, intern_type*& to_nxt) const
3516 const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3517 const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3518 const uint8_t* _frm_nxt = _frm;
3519 uint32_t* _to = reinterpret_cast<uint32_t*>(to);
3520 uint32_t* _to_end = reinterpret_cast<uint32_t*>(to_end);
3521 uint32_t* _to_nxt = _to;
3522 result r = utf8_to_ucs4(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3524 frm_nxt = frm + (_frm_nxt - _frm);
3525 to_nxt = to + (_to_nxt - _to);
3529 __codecvt_utf8<char32_t>::result
3530 __codecvt_utf8<char32_t>::do_unshift(state_type&,
3531 extern_type* to, extern_type*, extern_type*& to_nxt) const
3538 __codecvt_utf8<char32_t>::do_encoding() const _NOEXCEPT
3544 __codecvt_utf8<char32_t>::do_always_noconv() const _NOEXCEPT
3550 __codecvt_utf8<char32_t>::do_length(state_type&,
3551 const extern_type* frm, const extern_type* frm_end, size_t mx) const
3553 const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3554 const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3555 return utf8_to_ucs4_length(_frm, _frm_end, mx, _Maxcode_, _Mode_);
3559 __codecvt_utf8<char32_t>::do_max_length() const _NOEXCEPT
3561 if (_Mode_ & consume_header)
3566 // __codecvt_utf16<wchar_t, false>
3568 __codecvt_utf16<wchar_t, false>::result
3569 __codecvt_utf16<wchar_t, false>::do_out(state_type&,
3570 const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt,
3571 extern_type* to, extern_type* to_end, extern_type*& to_nxt) const
3573 const uint32_t* _frm = reinterpret_cast<const uint32_t*>(frm);
3574 const uint32_t* _frm_end = reinterpret_cast<const uint32_t*>(frm_end);
3575 const uint32_t* _frm_nxt = _frm;
3576 uint8_t* _to = reinterpret_cast<uint8_t*>(to);
3577 uint8_t* _to_end = reinterpret_cast<uint8_t*>(to_end);
3578 uint8_t* _to_nxt = _to;
3579 result r = ucs4_to_utf16be(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3581 frm_nxt = frm + (_frm_nxt - _frm);
3582 to_nxt = to + (_to_nxt - _to);
3586 __codecvt_utf16<wchar_t, false>::result
3587 __codecvt_utf16<wchar_t, false>::do_in(state_type&,
3588 const extern_type* frm, const extern_type* frm_end, const extern_type*& frm_nxt,
3589 intern_type* to, intern_type* to_end, intern_type*& to_nxt) const
3591 const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3592 const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3593 const uint8_t* _frm_nxt = _frm;
3594 uint32_t* _to = reinterpret_cast<uint32_t*>(to);
3595 uint32_t* _to_end = reinterpret_cast<uint32_t*>(to_end);
3596 uint32_t* _to_nxt = _to;
3597 result r = utf16be_to_ucs4(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3599 frm_nxt = frm + (_frm_nxt - _frm);
3600 to_nxt = to + (_to_nxt - _to);
3604 __codecvt_utf16<wchar_t, false>::result
3605 __codecvt_utf16<wchar_t, false>::do_unshift(state_type&,
3606 extern_type* to, extern_type*, extern_type*& to_nxt) const
3613 __codecvt_utf16<wchar_t, false>::do_encoding() const _NOEXCEPT
3619 __codecvt_utf16<wchar_t, false>::do_always_noconv() const _NOEXCEPT
3625 __codecvt_utf16<wchar_t, false>::do_length(state_type&,
3626 const extern_type* frm, const extern_type* frm_end, size_t mx) const
3628 const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3629 const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3630 return utf16be_to_ucs4_length(_frm, _frm_end, mx, _Maxcode_, _Mode_);
3634 __codecvt_utf16<wchar_t, false>::do_max_length() const _NOEXCEPT
3636 if (_Mode_ & consume_header)
3641 // __codecvt_utf16<wchar_t, true>
3643 __codecvt_utf16<wchar_t, true>::result
3644 __codecvt_utf16<wchar_t, true>::do_out(state_type&,
3645 const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt,
3646 extern_type* to, extern_type* to_end, extern_type*& to_nxt) const
3648 const uint32_t* _frm = reinterpret_cast<const uint32_t*>(frm);
3649 const uint32_t* _frm_end = reinterpret_cast<const uint32_t*>(frm_end);
3650 const uint32_t* _frm_nxt = _frm;
3651 uint8_t* _to = reinterpret_cast<uint8_t*>(to);
3652 uint8_t* _to_end = reinterpret_cast<uint8_t*>(to_end);
3653 uint8_t* _to_nxt = _to;
3654 result r = ucs4_to_utf16le(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3656 frm_nxt = frm + (_frm_nxt - _frm);
3657 to_nxt = to + (_to_nxt - _to);
3661 __codecvt_utf16<wchar_t, true>::result
3662 __codecvt_utf16<wchar_t, true>::do_in(state_type&,
3663 const extern_type* frm, const extern_type* frm_end, const extern_type*& frm_nxt,
3664 intern_type* to, intern_type* to_end, intern_type*& to_nxt) const
3666 const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3667 const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3668 const uint8_t* _frm_nxt = _frm;
3669 uint32_t* _to = reinterpret_cast<uint32_t*>(to);
3670 uint32_t* _to_end = reinterpret_cast<uint32_t*>(to_end);
3671 uint32_t* _to_nxt = _to;
3672 result r = utf16le_to_ucs4(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3674 frm_nxt = frm + (_frm_nxt - _frm);
3675 to_nxt = to + (_to_nxt - _to);
3679 __codecvt_utf16<wchar_t, true>::result
3680 __codecvt_utf16<wchar_t, true>::do_unshift(state_type&,
3681 extern_type* to, extern_type*, extern_type*& to_nxt) const
3688 __codecvt_utf16<wchar_t, true>::do_encoding() const _NOEXCEPT
3694 __codecvt_utf16<wchar_t, true>::do_always_noconv() const _NOEXCEPT
3700 __codecvt_utf16<wchar_t, true>::do_length(state_type&,
3701 const extern_type* frm, const extern_type* frm_end, size_t mx) const
3703 const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3704 const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3705 return utf16le_to_ucs4_length(_frm, _frm_end, mx, _Maxcode_, _Mode_);
3709 __codecvt_utf16<wchar_t, true>::do_max_length() const _NOEXCEPT
3711 if (_Mode_ & consume_header)
3716 // __codecvt_utf16<char16_t, false>
3718 __codecvt_utf16<char16_t, false>::result
3719 __codecvt_utf16<char16_t, false>::do_out(state_type&,
3720 const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt,
3721 extern_type* to, extern_type* to_end, extern_type*& to_nxt) const
3723 const uint16_t* _frm = reinterpret_cast<const uint16_t*>(frm);
3724 const uint16_t* _frm_end = reinterpret_cast<const uint16_t*>(frm_end);
3725 const uint16_t* _frm_nxt = _frm;
3726 uint8_t* _to = reinterpret_cast<uint8_t*>(to);
3727 uint8_t* _to_end = reinterpret_cast<uint8_t*>(to_end);
3728 uint8_t* _to_nxt = _to;
3729 result r = ucs2_to_utf16be(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3731 frm_nxt = frm + (_frm_nxt - _frm);
3732 to_nxt = to + (_to_nxt - _to);
3736 __codecvt_utf16<char16_t, false>::result
3737 __codecvt_utf16<char16_t, false>::do_in(state_type&,
3738 const extern_type* frm, const extern_type* frm_end, const extern_type*& frm_nxt,
3739 intern_type* to, intern_type* to_end, intern_type*& to_nxt) const
3741 const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3742 const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3743 const uint8_t* _frm_nxt = _frm;
3744 uint16_t* _to = reinterpret_cast<uint16_t*>(to);
3745 uint16_t* _to_end = reinterpret_cast<uint16_t*>(to_end);
3746 uint16_t* _to_nxt = _to;
3747 result r = utf16be_to_ucs2(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3749 frm_nxt = frm + (_frm_nxt - _frm);
3750 to_nxt = to + (_to_nxt - _to);
3754 __codecvt_utf16<char16_t, false>::result
3755 __codecvt_utf16<char16_t, false>::do_unshift(state_type&,
3756 extern_type* to, extern_type*, extern_type*& to_nxt) const
3763 __codecvt_utf16<char16_t, false>::do_encoding() const _NOEXCEPT
3769 __codecvt_utf16<char16_t, false>::do_always_noconv() const _NOEXCEPT
3775 __codecvt_utf16<char16_t, false>::do_length(state_type&,
3776 const extern_type* frm, const extern_type* frm_end, size_t mx) const
3778 const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3779 const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3780 return utf16be_to_ucs2_length(_frm, _frm_end, mx, _Maxcode_, _Mode_);
3784 __codecvt_utf16<char16_t, false>::do_max_length() const _NOEXCEPT
3786 if (_Mode_ & consume_header)
3791 // __codecvt_utf16<char16_t, true>
3793 __codecvt_utf16<char16_t, true>::result
3794 __codecvt_utf16<char16_t, true>::do_out(state_type&,
3795 const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt,
3796 extern_type* to, extern_type* to_end, extern_type*& to_nxt) const
3798 const uint16_t* _frm = reinterpret_cast<const uint16_t*>(frm);
3799 const uint16_t* _frm_end = reinterpret_cast<const uint16_t*>(frm_end);
3800 const uint16_t* _frm_nxt = _frm;
3801 uint8_t* _to = reinterpret_cast<uint8_t*>(to);
3802 uint8_t* _to_end = reinterpret_cast<uint8_t*>(to_end);
3803 uint8_t* _to_nxt = _to;
3804 result r = ucs2_to_utf16le(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3806 frm_nxt = frm + (_frm_nxt - _frm);
3807 to_nxt = to + (_to_nxt - _to);
3811 __codecvt_utf16<char16_t, true>::result
3812 __codecvt_utf16<char16_t, true>::do_in(state_type&,
3813 const extern_type* frm, const extern_type* frm_end, const extern_type*& frm_nxt,
3814 intern_type* to, intern_type* to_end, intern_type*& to_nxt) const
3816 const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3817 const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3818 const uint8_t* _frm_nxt = _frm;
3819 uint16_t* _to = reinterpret_cast<uint16_t*>(to);
3820 uint16_t* _to_end = reinterpret_cast<uint16_t*>(to_end);
3821 uint16_t* _to_nxt = _to;
3822 result r = utf16le_to_ucs2(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3824 frm_nxt = frm + (_frm_nxt - _frm);
3825 to_nxt = to + (_to_nxt - _to);
3829 __codecvt_utf16<char16_t, true>::result
3830 __codecvt_utf16<char16_t, true>::do_unshift(state_type&,
3831 extern_type* to, extern_type*, extern_type*& to_nxt) const
3838 __codecvt_utf16<char16_t, true>::do_encoding() const _NOEXCEPT
3844 __codecvt_utf16<char16_t, true>::do_always_noconv() const _NOEXCEPT
3850 __codecvt_utf16<char16_t, true>::do_length(state_type&,
3851 const extern_type* frm, const extern_type* frm_end, size_t mx) const
3853 const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3854 const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3855 return utf16le_to_ucs2_length(_frm, _frm_end, mx, _Maxcode_, _Mode_);
3859 __codecvt_utf16<char16_t, true>::do_max_length() const _NOEXCEPT
3861 if (_Mode_ & consume_header)
3866 // __codecvt_utf16<char32_t, false>
3868 __codecvt_utf16<char32_t, false>::result
3869 __codecvt_utf16<char32_t, false>::do_out(state_type&,
3870 const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt,
3871 extern_type* to, extern_type* to_end, extern_type*& to_nxt) const
3873 const uint32_t* _frm = reinterpret_cast<const uint32_t*>(frm);
3874 const uint32_t* _frm_end = reinterpret_cast<const uint32_t*>(frm_end);
3875 const uint32_t* _frm_nxt = _frm;
3876 uint8_t* _to = reinterpret_cast<uint8_t*>(to);
3877 uint8_t* _to_end = reinterpret_cast<uint8_t*>(to_end);
3878 uint8_t* _to_nxt = _to;
3879 result r = ucs4_to_utf16be(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3881 frm_nxt = frm + (_frm_nxt - _frm);
3882 to_nxt = to + (_to_nxt - _to);
3886 __codecvt_utf16<char32_t, false>::result
3887 __codecvt_utf16<char32_t, false>::do_in(state_type&,
3888 const extern_type* frm, const extern_type* frm_end, const extern_type*& frm_nxt,
3889 intern_type* to, intern_type* to_end, intern_type*& to_nxt) const
3891 const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3892 const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3893 const uint8_t* _frm_nxt = _frm;
3894 uint32_t* _to = reinterpret_cast<uint32_t*>(to);
3895 uint32_t* _to_end = reinterpret_cast<uint32_t*>(to_end);
3896 uint32_t* _to_nxt = _to;
3897 result r = utf16be_to_ucs4(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3899 frm_nxt = frm + (_frm_nxt - _frm);
3900 to_nxt = to + (_to_nxt - _to);
3904 __codecvt_utf16<char32_t, false>::result
3905 __codecvt_utf16<char32_t, false>::do_unshift(state_type&,
3906 extern_type* to, extern_type*, extern_type*& to_nxt) const
3913 __codecvt_utf16<char32_t, false>::do_encoding() const _NOEXCEPT
3919 __codecvt_utf16<char32_t, false>::do_always_noconv() const _NOEXCEPT
3925 __codecvt_utf16<char32_t, false>::do_length(state_type&,
3926 const extern_type* frm, const extern_type* frm_end, size_t mx) const
3928 const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3929 const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3930 return utf16be_to_ucs4_length(_frm, _frm_end, mx, _Maxcode_, _Mode_);
3934 __codecvt_utf16<char32_t, false>::do_max_length() const _NOEXCEPT
3936 if (_Mode_ & consume_header)
3941 // __codecvt_utf16<char32_t, true>
3943 __codecvt_utf16<char32_t, true>::result
3944 __codecvt_utf16<char32_t, true>::do_out(state_type&,
3945 const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt,
3946 extern_type* to, extern_type* to_end, extern_type*& to_nxt) const
3948 const uint32_t* _frm = reinterpret_cast<const uint32_t*>(frm);
3949 const uint32_t* _frm_end = reinterpret_cast<const uint32_t*>(frm_end);
3950 const uint32_t* _frm_nxt = _frm;
3951 uint8_t* _to = reinterpret_cast<uint8_t*>(to);
3952 uint8_t* _to_end = reinterpret_cast<uint8_t*>(to_end);
3953 uint8_t* _to_nxt = _to;
3954 result r = ucs4_to_utf16le(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3956 frm_nxt = frm + (_frm_nxt - _frm);
3957 to_nxt = to + (_to_nxt - _to);
3961 __codecvt_utf16<char32_t, true>::result
3962 __codecvt_utf16<char32_t, true>::do_in(state_type&,
3963 const extern_type* frm, const extern_type* frm_end, const extern_type*& frm_nxt,
3964 intern_type* to, intern_type* to_end, intern_type*& to_nxt) const
3966 const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
3967 const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
3968 const uint8_t* _frm_nxt = _frm;
3969 uint32_t* _to = reinterpret_cast<uint32_t*>(to);
3970 uint32_t* _to_end = reinterpret_cast<uint32_t*>(to_end);
3971 uint32_t* _to_nxt = _to;
3972 result r = utf16le_to_ucs4(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
3974 frm_nxt = frm + (_frm_nxt - _frm);
3975 to_nxt = to + (_to_nxt - _to);
3979 __codecvt_utf16<char32_t, true>::result
3980 __codecvt_utf16<char32_t, true>::do_unshift(state_type&,
3981 extern_type* to, extern_type*, extern_type*& to_nxt) const
3988 __codecvt_utf16<char32_t, true>::do_encoding() const _NOEXCEPT
3994 __codecvt_utf16<char32_t, true>::do_always_noconv() const _NOEXCEPT
4000 __codecvt_utf16<char32_t, true>::do_length(state_type&,
4001 const extern_type* frm, const extern_type* frm_end, size_t mx) const
4003 const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
4004 const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
4005 return utf16le_to_ucs4_length(_frm, _frm_end, mx, _Maxcode_, _Mode_);
4009 __codecvt_utf16<char32_t, true>::do_max_length() const _NOEXCEPT
4011 if (_Mode_ & consume_header)
4016 // __codecvt_utf8_utf16<wchar_t>
4018 __codecvt_utf8_utf16<wchar_t>::result
4019 __codecvt_utf8_utf16<wchar_t>::do_out(state_type&,
4020 const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt,
4021 extern_type* to, extern_type* to_end, extern_type*& to_nxt) const
4023 const uint32_t* _frm = reinterpret_cast<const uint32_t*>(frm);
4024 const uint32_t* _frm_end = reinterpret_cast<const uint32_t*>(frm_end);
4025 const uint32_t* _frm_nxt = _frm;
4026 uint8_t* _to = reinterpret_cast<uint8_t*>(to);
4027 uint8_t* _to_end = reinterpret_cast<uint8_t*>(to_end);
4028 uint8_t* _to_nxt = _to;
4029 result r = utf16_to_utf8(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
4031 frm_nxt = frm + (_frm_nxt - _frm);
4032 to_nxt = to + (_to_nxt - _to);
4036 __codecvt_utf8_utf16<wchar_t>::result
4037 __codecvt_utf8_utf16<wchar_t>::do_in(state_type&,
4038 const extern_type* frm, const extern_type* frm_end, const extern_type*& frm_nxt,
4039 intern_type* to, intern_type* to_end, intern_type*& to_nxt) const
4041 const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
4042 const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
4043 const uint8_t* _frm_nxt = _frm;
4044 uint32_t* _to = reinterpret_cast<uint32_t*>(to);
4045 uint32_t* _to_end = reinterpret_cast<uint32_t*>(to_end);
4046 uint32_t* _to_nxt = _to;
4047 result r = utf8_to_utf16(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
4049 frm_nxt = frm + (_frm_nxt - _frm);
4050 to_nxt = to + (_to_nxt - _to);
4054 __codecvt_utf8_utf16<wchar_t>::result
4055 __codecvt_utf8_utf16<wchar_t>::do_unshift(state_type&,
4056 extern_type* to, extern_type*, extern_type*& to_nxt) const
4063 __codecvt_utf8_utf16<wchar_t>::do_encoding() const _NOEXCEPT
4069 __codecvt_utf8_utf16<wchar_t>::do_always_noconv() const _NOEXCEPT
4075 __codecvt_utf8_utf16<wchar_t>::do_length(state_type&,
4076 const extern_type* frm, const extern_type* frm_end, size_t mx) const
4078 const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
4079 const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
4080 return utf8_to_utf16_length(_frm, _frm_end, mx, _Maxcode_, _Mode_);
4084 __codecvt_utf8_utf16<wchar_t>::do_max_length() const _NOEXCEPT
4086 if (_Mode_ & consume_header)
4091 // __codecvt_utf8_utf16<char16_t>
4093 __codecvt_utf8_utf16<char16_t>::result
4094 __codecvt_utf8_utf16<char16_t>::do_out(state_type&,
4095 const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt,
4096 extern_type* to, extern_type* to_end, extern_type*& to_nxt) const
4098 const uint16_t* _frm = reinterpret_cast<const uint16_t*>(frm);
4099 const uint16_t* _frm_end = reinterpret_cast<const uint16_t*>(frm_end);
4100 const uint16_t* _frm_nxt = _frm;
4101 uint8_t* _to = reinterpret_cast<uint8_t*>(to);
4102 uint8_t* _to_end = reinterpret_cast<uint8_t*>(to_end);
4103 uint8_t* _to_nxt = _to;
4104 result r = utf16_to_utf8(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
4106 frm_nxt = frm + (_frm_nxt - _frm);
4107 to_nxt = to + (_to_nxt - _to);
4111 __codecvt_utf8_utf16<char16_t>::result
4112 __codecvt_utf8_utf16<char16_t>::do_in(state_type&,
4113 const extern_type* frm, const extern_type* frm_end, const extern_type*& frm_nxt,
4114 intern_type* to, intern_type* to_end, intern_type*& to_nxt) const
4116 const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
4117 const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
4118 const uint8_t* _frm_nxt = _frm;
4119 uint16_t* _to = reinterpret_cast<uint16_t*>(to);
4120 uint16_t* _to_end = reinterpret_cast<uint16_t*>(to_end);
4121 uint16_t* _to_nxt = _to;
4122 result r = utf8_to_utf16(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
4124 frm_nxt = frm + (_frm_nxt - _frm);
4125 to_nxt = to + (_to_nxt - _to);
4129 __codecvt_utf8_utf16<char16_t>::result
4130 __codecvt_utf8_utf16<char16_t>::do_unshift(state_type&,
4131 extern_type* to, extern_type*, extern_type*& to_nxt) const
4138 __codecvt_utf8_utf16<char16_t>::do_encoding() const _NOEXCEPT
4144 __codecvt_utf8_utf16<char16_t>::do_always_noconv() const _NOEXCEPT
4150 __codecvt_utf8_utf16<char16_t>::do_length(state_type&,
4151 const extern_type* frm, const extern_type* frm_end, size_t mx) const
4153 const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
4154 const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
4155 return utf8_to_utf16_length(_frm, _frm_end, mx, _Maxcode_, _Mode_);
4159 __codecvt_utf8_utf16<char16_t>::do_max_length() const _NOEXCEPT
4161 if (_Mode_ & consume_header)
4166 // __codecvt_utf8_utf16<char32_t>
4168 __codecvt_utf8_utf16<char32_t>::result
4169 __codecvt_utf8_utf16<char32_t>::do_out(state_type&,
4170 const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt,
4171 extern_type* to, extern_type* to_end, extern_type*& to_nxt) const
4173 const uint32_t* _frm = reinterpret_cast<const uint32_t*>(frm);
4174 const uint32_t* _frm_end = reinterpret_cast<const uint32_t*>(frm_end);
4175 const uint32_t* _frm_nxt = _frm;
4176 uint8_t* _to = reinterpret_cast<uint8_t*>(to);
4177 uint8_t* _to_end = reinterpret_cast<uint8_t*>(to_end);
4178 uint8_t* _to_nxt = _to;
4179 result r = utf16_to_utf8(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
4181 frm_nxt = frm + (_frm_nxt - _frm);
4182 to_nxt = to + (_to_nxt - _to);
4186 __codecvt_utf8_utf16<char32_t>::result
4187 __codecvt_utf8_utf16<char32_t>::do_in(state_type&,
4188 const extern_type* frm, const extern_type* frm_end, const extern_type*& frm_nxt,
4189 intern_type* to, intern_type* to_end, intern_type*& to_nxt) const
4191 const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
4192 const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
4193 const uint8_t* _frm_nxt = _frm;
4194 uint32_t* _to = reinterpret_cast<uint32_t*>(to);
4195 uint32_t* _to_end = reinterpret_cast<uint32_t*>(to_end);
4196 uint32_t* _to_nxt = _to;
4197 result r = utf8_to_utf16(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
4199 frm_nxt = frm + (_frm_nxt - _frm);
4200 to_nxt = to + (_to_nxt - _to);
4204 __codecvt_utf8_utf16<char32_t>::result
4205 __codecvt_utf8_utf16<char32_t>::do_unshift(state_type&,
4206 extern_type* to, extern_type*, extern_type*& to_nxt) const
4213 __codecvt_utf8_utf16<char32_t>::do_encoding() const _NOEXCEPT
4219 __codecvt_utf8_utf16<char32_t>::do_always_noconv() const _NOEXCEPT
4225 __codecvt_utf8_utf16<char32_t>::do_length(state_type&,
4226 const extern_type* frm, const extern_type* frm_end, size_t mx) const
4228 const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
4229 const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
4230 return utf8_to_utf16_length(_frm, _frm_end, mx, _Maxcode_, _Mode_);
4234 __codecvt_utf8_utf16<char32_t>::do_max_length() const _NOEXCEPT
4236 if (_Mode_ & consume_header)
4241 // __narrow_to_utf8<16>
4243 __narrow_to_utf8<16>::~__narrow_to_utf8()
4247 // __narrow_to_utf8<32>
4249 __narrow_to_utf8<32>::~__narrow_to_utf8()
4253 // __widen_from_utf8<16>
4255 __widen_from_utf8<16>::~__widen_from_utf8()
4259 // __widen_from_utf8<32>
4261 __widen_from_utf8<32>::~__widen_from_utf8()
4265 // numpunct<char> && numpunct<wchar_t>
4267 locale::id numpunct< char >::id;
4268 locale::id numpunct<wchar_t>::id;
4270 numpunct<char>::numpunct(size_t refs)
4271 : locale::facet(refs),
4272 __decimal_point_('.'),
4273 __thousands_sep_(',')
4277 numpunct<wchar_t>::numpunct(size_t refs)
4278 : locale::facet(refs),
4279 __decimal_point_(L'.'),
4280 __thousands_sep_(L',')
4284 numpunct<char>::~numpunct()
4288 numpunct<wchar_t>::~numpunct()
4292 char numpunct< char >::do_decimal_point() const {return __decimal_point_;}
4293 wchar_t numpunct<wchar_t>::do_decimal_point() const {return __decimal_point_;}
4295 char numpunct< char >::do_thousands_sep() const {return __thousands_sep_;}
4296 wchar_t numpunct<wchar_t>::do_thousands_sep() const {return __thousands_sep_;}
4298 string numpunct< char >::do_grouping() const {return __grouping_;}
4299 string numpunct<wchar_t>::do_grouping() const {return __grouping_;}
4301 string numpunct< char >::do_truename() const {return "true";}
4302 wstring numpunct<wchar_t>::do_truename() const {return L"true";}
4304 string numpunct< char >::do_falsename() const {return "false";}
4305 wstring numpunct<wchar_t>::do_falsename() const {return L"false";}
4307 // numpunct_byname<char>
4309 numpunct_byname<char>::numpunct_byname(const char* nm, size_t refs)
4310 : numpunct<char>(refs)
4315 numpunct_byname<char>::numpunct_byname(const string& nm, size_t refs)
4316 : numpunct<char>(refs)
4321 numpunct_byname<char>::~numpunct_byname()
4326 numpunct_byname<char>::__init(const char* nm)
4328 if (strcmp(nm, "C") != 0)
4330 __locale_unique_ptr loc(newlocale(LC_ALL_MASK, nm, 0), freelocale);
4331 #ifndef _LIBCPP_NO_EXCEPTIONS
4333 throw runtime_error("numpunct_byname<char>::numpunct_byname"
4334 " failed to construct for " + string(nm));
4335 #endif // _LIBCPP_NO_EXCEPTIONS
4336 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
4337 lconv* lc = localeconv_l(loc.get());
4339 lconv* lc = __localeconv_l(loc.get());
4341 if (*lc->decimal_point)
4342 __decimal_point_ = *lc->decimal_point;
4343 if (*lc->thousands_sep)
4344 __thousands_sep_ = *lc->thousands_sep;
4345 __grouping_ = lc->grouping;
4346 // localization for truename and falsename is not available
4350 // numpunct_byname<wchar_t>
4352 numpunct_byname<wchar_t>::numpunct_byname(const char* nm, size_t refs)
4353 : numpunct<wchar_t>(refs)
4358 numpunct_byname<wchar_t>::numpunct_byname(const string& nm, size_t refs)
4359 : numpunct<wchar_t>(refs)
4364 numpunct_byname<wchar_t>::~numpunct_byname()
4369 numpunct_byname<wchar_t>::__init(const char* nm)
4371 if (strcmp(nm, "C") != 0)
4373 __locale_unique_ptr loc(newlocale(LC_ALL_MASK, nm, 0), freelocale);
4374 #ifndef _LIBCPP_NO_EXCEPTIONS
4376 throw runtime_error("numpunct_byname<char>::numpunct_byname"
4377 " failed to construct for " + string(nm));
4378 #endif // _LIBCPP_NO_EXCEPTIONS
4379 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
4380 lconv* lc = localeconv_l(loc.get());
4382 lconv* lc = __localeconv_l(loc.get());
4384 if (*lc->decimal_point)
4385 __decimal_point_ = *lc->decimal_point;
4386 if (*lc->thousands_sep)
4387 __thousands_sep_ = *lc->thousands_sep;
4388 __grouping_ = lc->grouping;
4389 // locallization for truename and falsename is not available
4396 __num_get_base::__get_base(ios_base& iob)
4398 ios_base::fmtflags __basefield = iob.flags() & ios_base::basefield;
4399 if (__basefield == ios_base::oct)
4401 else if (__basefield == ios_base::hex)
4403 else if (__basefield == 0)
4408 const char __num_get_base::__src[33] = "0123456789abcdefABCDEFxX+-pPiInN";
4411 __check_grouping(const string& __grouping, unsigned* __g, unsigned* __g_end,
4412 ios_base::iostate& __err)
4414 if (__grouping.size() != 0)
4416 reverse(__g, __g_end);
4417 const char* __ig = __grouping.data();
4418 const char* __eg = __ig + __grouping.size();
4419 for (unsigned* __r = __g; __r < __g_end-1; ++__r)
4421 if (0 < *__ig && *__ig < numeric_limits<char>::max())
4423 if (static_cast<unsigned>(*__ig) != *__r)
4425 __err = ios_base::failbit;
4429 if (__eg - __ig > 1)
4432 if (0 < *__ig && *__ig < numeric_limits<char>::max())
4434 if (static_cast<unsigned>(*__ig) < __g_end[-1] || __g_end[-1] == 0)
4435 __err = ios_base::failbit;
4441 __num_put_base::__format_int(char* __fmtp, const char* __len, bool __signd,
4442 ios_base::fmtflags __flags)
4444 if (__flags & ios_base::showpos)
4446 if (__flags & ios_base::showbase)
4449 *__fmtp++ = *__len++;
4450 if ((__flags & ios_base::basefield) == ios_base::oct)
4452 else if ((__flags & ios_base::basefield) == ios_base::hex)
4454 if (__flags & ios_base::uppercase)
4466 __num_put_base::__format_float(char* __fmtp, const char* __len,
4467 ios_base::fmtflags __flags)
4469 bool specify_precision = true;
4470 if (__flags & ios_base::showpos)
4472 if (__flags & ios_base::showpoint)
4474 ios_base::fmtflags floatfield = __flags & ios_base::floatfield;
4475 bool uppercase = (__flags & ios_base::uppercase) != 0;
4476 if (floatfield == (ios_base::fixed | ios_base::scientific))
4477 specify_precision = false;
4484 *__fmtp++ = *__len++;
4485 if (floatfield == ios_base::fixed)
4492 else if (floatfield == ios_base::scientific)
4499 else if (floatfield == (ios_base::fixed | ios_base::scientific))
4513 return specify_precision;
4517 __num_put_base::__identify_padding(char* __nb, char* __ne,
4518 const ios_base& __iob)
4520 switch (__iob.flags() & ios_base::adjustfield)
4522 case ios_base::internal:
4523 if (__nb[0] == '-' || __nb[0] == '+')
4525 if (__ne - __nb >= 2 && __nb[0] == '0'
4526 && (__nb[1] == 'x' || __nb[1] == 'X'))
4529 case ios_base::left:
4531 case ios_base::right:
4544 static string weeks[14];
4545 weeks[0] = "Sunday";
4546 weeks[1] = "Monday";
4547 weeks[2] = "Tuesday";
4548 weeks[3] = "Wednesday";
4549 weeks[4] = "Thursday";
4550 weeks[5] = "Friday";
4551 weeks[6] = "Saturday";
4566 static wstring weeks[14];
4567 weeks[0] = L"Sunday";
4568 weeks[1] = L"Monday";
4569 weeks[2] = L"Tuesday";
4570 weeks[3] = L"Wednesday";
4571 weeks[4] = L"Thursday";
4572 weeks[5] = L"Friday";
4573 weeks[6] = L"Saturday";
4586 __time_get_c_storage<char>::__weeks() const
4588 static const string* weeks = init_weeks();
4594 __time_get_c_storage<wchar_t>::__weeks() const
4596 static const wstring* weeks = init_wweeks();
4604 static string months[24];
4605 months[0] = "January";
4606 months[1] = "February";
4607 months[2] = "March";
4608 months[3] = "April";
4612 months[7] = "August";
4613 months[8] = "September";
4614 months[9] = "October";
4615 months[10] = "November";
4616 months[11] = "December";
4636 static wstring months[24];
4637 months[0] = L"January";
4638 months[1] = L"February";
4639 months[2] = L"March";
4640 months[3] = L"April";
4642 months[5] = L"June";
4643 months[6] = L"July";
4644 months[7] = L"August";
4645 months[8] = L"September";
4646 months[9] = L"October";
4647 months[10] = L"November";
4648 months[11] = L"December";
4649 months[12] = L"Jan";
4650 months[13] = L"Feb";
4651 months[14] = L"Mar";
4652 months[15] = L"Apr";
4653 months[16] = L"May";
4654 months[17] = L"Jun";
4655 months[18] = L"Jul";
4656 months[19] = L"Aug";
4657 months[20] = L"Sep";
4658 months[21] = L"Oct";
4659 months[22] = L"Nov";
4660 months[23] = L"Dec";
4666 __time_get_c_storage<char>::__months() const
4668 static const string* months = init_months();
4674 __time_get_c_storage<wchar_t>::__months() const
4676 static const wstring* months = init_wmonths();
4684 static string am_pm[24];
4694 static wstring am_pm[24];
4702 __time_get_c_storage<char>::__am_pm() const
4704 static const string* am_pm = init_am_pm();
4710 __time_get_c_storage<wchar_t>::__am_pm() const
4712 static const wstring* am_pm = init_wam_pm();
4718 __time_get_c_storage<char>::__x() const
4720 static string s("%m/%d/%y");
4726 __time_get_c_storage<wchar_t>::__x() const
4728 static wstring s(L"%m/%d/%y");
4734 __time_get_c_storage<char>::__X() const
4736 static string s("%H:%M:%S");
4742 __time_get_c_storage<wchar_t>::__X() const
4744 static wstring s(L"%H:%M:%S");
4750 __time_get_c_storage<char>::__c() const
4752 static string s("%a %b %d %H:%M:%S %Y");
4758 __time_get_c_storage<wchar_t>::__c() const
4760 static wstring s(L"%a %b %d %H:%M:%S %Y");
4766 __time_get_c_storage<char>::__r() const
4768 static string s("%I:%M:%S %p");
4774 __time_get_c_storage<wchar_t>::__r() const
4776 static wstring s(L"%I:%M:%S %p");
4782 __time_get::__time_get(const char* nm)
4783 : __loc_(newlocale(LC_ALL_MASK, nm, 0))
4785 #ifndef _LIBCPP_NO_EXCEPTIONS
4787 throw runtime_error("time_get_byname"
4788 " failed to construct for " + string(nm));
4789 #endif // _LIBCPP_NO_EXCEPTIONS
4792 __time_get::__time_get(const string& nm)
4793 : __loc_(newlocale(LC_ALL_MASK, nm.c_str(), 0))
4795 #ifndef _LIBCPP_NO_EXCEPTIONS
4797 throw runtime_error("time_get_byname"
4798 " failed to construct for " + nm);
4799 #endif // _LIBCPP_NO_EXCEPTIONS
4802 __time_get::~__time_get()
4806 #if defined(__clang__)
4807 #pragma clang diagnostic ignored "-Wmissing-field-initializers"
4809 #if defined(__GNUG__)
4810 #pragma GCC diagnostic ignored "-Wmissing-field-initializers"
4815 __time_get_storage<char>::__analyze(char fmt, const ctype<char>& ct)
4831 size_t n = strftime_l(buf, countof(buf), f, &t, __loc_);
4837 if (ct.is(ctype_base::space, *bb))
4839 result.push_back(' ');
4840 for (++bb; bb != be && ct.is(ctype_base::space, *bb); ++bb)
4845 ios_base::iostate err = ios_base::goodbit;
4846 ptrdiff_t i = __scan_keyword(w, be, this->__weeks_, this->__weeks_+14,
4851 result.push_back('%');
4853 result.push_back('A');
4855 result.push_back('a');
4860 i = __scan_keyword(w, be, this->__months_, this->__months_+24,
4865 result.push_back('%');
4867 result.push_back('B');
4869 result.push_back('b');
4870 if (fmt == 'x' && ct.is(ctype_base::digit, this->__months_[i][0]))
4871 result.back() = 'm';
4875 if (this->__am_pm_[0].size() + this->__am_pm_[1].size() > 0)
4878 i = __scan_keyword(w, be, this->__am_pm_, this->__am_pm_+2,
4879 ct, err, false) - this->__am_pm_;
4882 result.push_back('%');
4883 result.push_back('p');
4889 if (ct.is(ctype_base::digit, *bb))
4891 switch(__get_up_to_n_digits(bb, be, err, ct, 4))
4894 result.push_back('%');
4895 result.push_back('w');
4898 result.push_back('%');
4899 result.push_back('u');
4902 result.push_back('%');
4903 result.push_back('I');
4906 result.push_back('%');
4907 result.push_back('m');
4910 result.push_back('%');
4911 result.push_back('H');
4914 result.push_back('%');
4915 result.push_back('d');
4918 result.push_back('%');
4919 result.push_back('M');
4922 result.push_back('%');
4923 result.push_back('S');
4926 result.push_back('%');
4927 result.push_back('y');
4930 result.push_back('%');
4931 result.push_back('j');
4934 result.push_back('%');
4935 result.push_back('Y');
4938 for (; w != bb; ++w)
4939 result.push_back(*w);
4946 result.push_back('%');
4947 result.push_back('%');
4951 result.push_back(*bb);
4957 #if defined(__clang__)
4958 #pragma clang diagnostic ignored "-Wmissing-braces"
4963 __time_get_storage<wchar_t>::__analyze(char fmt, const ctype<wchar_t>& ct)
4979 strftime_l(buf, countof(buf), f, &t, __loc_);
4981 wchar_t* wbb = wbuf;
4983 const char* bb = buf;
4984 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
4985 size_t j = mbsrtowcs_l( wbb, &bb, countof(wbuf), &mb, __loc_);
4987 size_t j = __mbsrtowcs_l( wbb, &bb, countof(wbuf), &mb, __loc_);
4989 if (j == size_t(-1))
4990 __throw_runtime_error("locale not supported");
4991 wchar_t* wbe = wbb + j;
4995 if (ct.is(ctype_base::space, *wbb))
4997 result.push_back(L' ');
4998 for (++wbb; wbb != wbe && ct.is(ctype_base::space, *wbb); ++wbb)
5003 ios_base::iostate err = ios_base::goodbit;
5004 ptrdiff_t i = __scan_keyword(w, wbe, this->__weeks_, this->__weeks_+14,
5009 result.push_back(L'%');
5011 result.push_back(L'A');
5013 result.push_back(L'a');
5018 i = __scan_keyword(w, wbe, this->__months_, this->__months_+24,
5023 result.push_back(L'%');
5025 result.push_back(L'B');
5027 result.push_back(L'b');
5028 if (fmt == 'x' && ct.is(ctype_base::digit, this->__months_[i][0]))
5029 result.back() = L'm';
5033 if (this->__am_pm_[0].size() + this->__am_pm_[1].size() > 0)
5036 i = __scan_keyword(w, wbe, this->__am_pm_, this->__am_pm_+2,
5037 ct, err, false) - this->__am_pm_;
5040 result.push_back(L'%');
5041 result.push_back(L'p');
5047 if (ct.is(ctype_base::digit, *wbb))
5049 switch(__get_up_to_n_digits(wbb, wbe, err, ct, 4))
5052 result.push_back(L'%');
5053 result.push_back(L'w');
5056 result.push_back(L'%');
5057 result.push_back(L'u');
5060 result.push_back(L'%');
5061 result.push_back(L'I');
5064 result.push_back(L'%');
5065 result.push_back(L'm');
5068 result.push_back(L'%');
5069 result.push_back(L'H');
5072 result.push_back(L'%');
5073 result.push_back(L'd');
5076 result.push_back(L'%');
5077 result.push_back(L'M');
5080 result.push_back(L'%');
5081 result.push_back(L'S');
5084 result.push_back(L'%');
5085 result.push_back(L'y');
5088 result.push_back(L'%');
5089 result.push_back(L'j');
5092 result.push_back(L'%');
5093 result.push_back(L'Y');
5096 for (; w != wbb; ++w)
5097 result.push_back(*w);
5102 if (ct.narrow(*wbb, 0) == '%')
5104 result.push_back(L'%');
5105 result.push_back(L'%');
5109 result.push_back(*wbb);
5117 __time_get_storage<char>::init(const ctype<char>& ct)
5122 for (int i = 0; i < 7; ++i)
5125 strftime_l(buf, countof(buf), "%A", &t, __loc_);
5127 strftime_l(buf, countof(buf), "%a", &t, __loc_);
5128 __weeks_[i+7] = buf;
5131 for (int i = 0; i < 12; ++i)
5134 strftime_l(buf, countof(buf), "%B", &t, __loc_);
5136 strftime_l(buf, countof(buf), "%b", &t, __loc_);
5137 __months_[i+12] = buf;
5141 strftime_l(buf, countof(buf), "%p", &t, __loc_);
5144 strftime_l(buf, countof(buf), "%p", &t, __loc_);
5146 __c_ = __analyze('c', ct);
5147 __r_ = __analyze('r', ct);
5148 __x_ = __analyze('x', ct);
5149 __X_ = __analyze('X', ct);
5154 __time_get_storage<wchar_t>::init(const ctype<wchar_t>& ct)
5162 for (int i = 0; i < 7; ++i)
5165 strftime_l(buf, countof(buf), "%A", &t, __loc_);
5167 const char* bb = buf;
5168 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
5169 size_t j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_);
5171 size_t j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_);
5173 if (j == size_t(-1))
5174 __throw_runtime_error("locale not supported");
5176 __weeks_[i].assign(wbuf, wbe);
5177 strftime_l(buf, countof(buf), "%a", &t, __loc_);
5180 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
5181 j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_);
5183 j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_);
5185 if (j == size_t(-1))
5186 __throw_runtime_error("locale not supported");
5188 __weeks_[i+7].assign(wbuf, wbe);
5191 for (int i = 0; i < 12; ++i)
5194 strftime_l(buf, countof(buf), "%B", &t, __loc_);
5196 const char* bb = buf;
5197 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
5198 size_t j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_);
5200 size_t j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_);
5202 if (j == size_t(-1))
5203 __throw_runtime_error("locale not supported");
5205 __months_[i].assign(wbuf, wbe);
5206 strftime_l(buf, countof(buf), "%b", &t, __loc_);
5209 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
5210 j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_);
5212 j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_);
5214 if (j == size_t(-1))
5215 __throw_runtime_error("locale not supported");
5217 __months_[i+12].assign(wbuf, wbe);
5221 strftime_l(buf, countof(buf), "%p", &t, __loc_);
5223 const char* bb = buf;
5224 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
5225 size_t j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_);
5227 size_t j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_);
5229 if (j == size_t(-1))
5230 __throw_runtime_error("locale not supported");
5232 __am_pm_[0].assign(wbuf, wbe);
5234 strftime_l(buf, countof(buf), "%p", &t, __loc_);
5237 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
5238 j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_);
5240 j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_);
5242 if (j == size_t(-1))
5243 __throw_runtime_error("locale not supported");
5245 __am_pm_[1].assign(wbuf, wbe);
5246 __c_ = __analyze('c', ct);
5247 __r_ = __analyze('r', ct);
5248 __x_ = __analyze('x', ct);
5249 __X_ = __analyze('X', ct);
5252 template <class CharT>
5253 struct _LIBCPP_HIDDEN __time_get_temp
5254 : public ctype_byname<CharT>
5256 explicit __time_get_temp(const char* nm)
5257 : ctype_byname<CharT>(nm, 1) {}
5258 explicit __time_get_temp(const string& nm)
5259 : ctype_byname<CharT>(nm, 1) {}
5263 __time_get_storage<char>::__time_get_storage(const char* __nm)
5266 const __time_get_temp<char> ct(__nm);
5271 __time_get_storage<char>::__time_get_storage(const string& __nm)
5274 const __time_get_temp<char> ct(__nm);
5279 __time_get_storage<wchar_t>::__time_get_storage(const char* __nm)
5282 const __time_get_temp<wchar_t> ct(__nm);
5287 __time_get_storage<wchar_t>::__time_get_storage(const string& __nm)
5290 const __time_get_temp<wchar_t> ct(__nm);
5295 time_base::dateorder
5296 __time_get_storage<char>::__do_date_order() const
5299 for (i = 0; i < __x_.size(); ++i)
5307 for (++i; i < __x_.size(); ++i)
5310 if (i == __x_.size())
5316 for (++i; i < __x_.size(); ++i)
5319 if (i == __x_.size())
5323 return time_base::ymd;
5326 for (++i; i < __x_.size(); ++i)
5329 if (i == __x_.size())
5333 return time_base::ydm;
5338 for (++i; i < __x_.size(); ++i)
5341 if (i == __x_.size())
5346 for (++i; i < __x_.size(); ++i)
5349 if (i == __x_.size())
5352 if (__x_[i] == 'y' || __x_[i] == 'Y')
5353 return time_base::mdy;
5358 for (++i; i < __x_.size(); ++i)
5361 if (i == __x_.size())
5366 for (++i; i < __x_.size(); ++i)
5369 if (i == __x_.size())
5372 if (__x_[i] == 'y' || __x_[i] == 'Y')
5373 return time_base::dmy;
5378 return time_base::no_order;
5382 time_base::dateorder
5383 __time_get_storage<wchar_t>::__do_date_order() const
5386 for (i = 0; i < __x_.size(); ++i)
5387 if (__x_[i] == L'%')
5394 for (++i; i < __x_.size(); ++i)
5395 if (__x_[i] == L'%')
5397 if (i == __x_.size())
5403 for (++i; i < __x_.size(); ++i)
5404 if (__x_[i] == L'%')
5406 if (i == __x_.size())
5409 if (__x_[i] == L'd')
5410 return time_base::ymd;
5413 for (++i; i < __x_.size(); ++i)
5414 if (__x_[i] == L'%')
5416 if (i == __x_.size())
5419 if (__x_[i] == L'm')
5420 return time_base::ydm;
5425 for (++i; i < __x_.size(); ++i)
5426 if (__x_[i] == L'%')
5428 if (i == __x_.size())
5431 if (__x_[i] == L'd')
5433 for (++i; i < __x_.size(); ++i)
5434 if (__x_[i] == L'%')
5436 if (i == __x_.size())
5439 if (__x_[i] == L'y' || __x_[i] == L'Y')
5440 return time_base::mdy;
5445 for (++i; i < __x_.size(); ++i)
5446 if (__x_[i] == L'%')
5448 if (i == __x_.size())
5451 if (__x_[i] == L'm')
5453 for (++i; i < __x_.size(); ++i)
5454 if (__x_[i] == L'%')
5456 if (i == __x_.size())
5459 if (__x_[i] == L'y' || __x_[i] == L'Y')
5460 return time_base::dmy;
5465 return time_base::no_order;
5470 __time_put::__time_put(const char* nm)
5471 : __loc_(newlocale(LC_ALL_MASK, nm, 0))
5473 #ifndef _LIBCPP_NO_EXCEPTIONS
5475 throw runtime_error("time_put_byname"
5476 " failed to construct for " + string(nm));
5477 #endif // _LIBCPP_NO_EXCEPTIONS
5480 __time_put::__time_put(const string& nm)
5481 : __loc_(newlocale(LC_ALL_MASK, nm.c_str(), 0))
5483 #ifndef _LIBCPP_NO_EXCEPTIONS
5485 throw runtime_error("time_put_byname"
5486 " failed to construct for " + nm);
5487 #endif // _LIBCPP_NO_EXCEPTIONS
5490 __time_put::~__time_put()
5492 if (__loc_ != _LIBCPP_GET_C_LOCALE)
5497 __time_put::__do_put(char* __nb, char*& __ne, const tm* __tm,
5498 char __fmt, char __mod) const
5500 char fmt[] = {'%', __fmt, __mod, 0};
5502 swap(fmt[1], fmt[2]);
5503 size_t n = strftime_l(__nb, countof(__nb, __ne), fmt, __tm, __loc_);
5508 __time_put::__do_put(wchar_t* __wb, wchar_t*& __we, const tm* __tm,
5509 char __fmt, char __mod) const
5512 char* __ne = __nar + 100;
5513 __do_put(__nar, __ne, __tm, __fmt, __mod);
5515 const char* __nb = __nar;
5516 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
5517 size_t j = mbsrtowcs_l(__wb, &__nb, countof(__wb, __we), &mb, __loc_);
5519 size_t j = __mbsrtowcs_l(__wb, &__nb, countof(__wb, __we), &mb, __loc_);
5521 if (j == size_t(-1))
5522 __throw_runtime_error("locale not supported");
5526 // moneypunct_byname
5528 template <class charT>
5531 __init_pat(money_base::pattern& pat, basic_string<charT>& __curr_symbol_,
5532 bool intl, char cs_precedes, char sep_by_space, char sign_posn,
5535 const char sign = static_cast<char>(money_base::sign);
5536 const char space = static_cast<char>(money_base::space);
5537 const char none = static_cast<char>(money_base::none);
5538 const char symbol = static_cast<char>(money_base::symbol);
5539 const char value = static_cast<char>(money_base::value);
5540 const bool symbol_contains_sep = intl && __curr_symbol_.size() == 4;
5542 // Comments on case branches reflect 'C11 7.11.2.1 The localeconv
5543 // function'. "Space between sign and symbol or value" means that
5544 // if the sign is adjacent to the symbol, there's a space between
5545 // them, and otherwise there's a space between the sign and value.
5547 // C11's localeconv specifies that the fourth character of an
5548 // international curr_symbol is used to separate the sign and
5549 // value when sep_by_space says to do so. C++ can't represent
5550 // that, so we just use a space. When sep_by_space says to
5551 // separate the symbol and value-or-sign with a space, we rearrange the
5552 // curr_symbol to put its spacing character on the correct side of
5555 // We also need to avoid adding an extra space between the sign
5556 // and value when the currency symbol is suppressed (by not
5557 // setting showbase). We match glibc's strfmon by interpreting
5558 // sep_by_space==1 as "omit the space when the currency symbol is
5561 // Users who want to get this right should use ICU instead.
5563 switch (cs_precedes)
5565 case 0: // value before curr_symbol
5566 if (symbol_contains_sep) {
5567 // Move the separator to before the symbol, to place it
5568 // between the value and symbol.
5569 rotate(__curr_symbol_.begin(), __curr_symbol_.begin() + 3,
5570 __curr_symbol_.end());
5574 case 0: // Parentheses surround the quantity and currency symbol.
5575 pat.field[0] = sign;
5576 pat.field[1] = value;
5577 pat.field[2] = none; // Any space appears in the symbol.
5578 pat.field[3] = symbol;
5579 switch (sep_by_space)
5581 case 0: // No space separates the currency symbol and value.
5582 // This case may have changed between C99 and C11;
5583 // assume the currency symbol matches the intention.
5584 case 2: // Space between sign and currency or value.
5585 // The "sign" is two parentheses, so no space here either.
5587 case 1: // Space between currency-and-sign or currency and value.
5588 if (!symbol_contains_sep) {
5589 // We insert the space into the symbol instead of
5590 // setting pat.field[2]=space so that when
5591 // showbase is not set, the space goes away too.
5592 __curr_symbol_.insert(0, 1, space_char);
5599 case 1: // The sign string precedes the quantity and currency symbol.
5600 pat.field[0] = sign;
5601 pat.field[3] = symbol;
5602 switch (sep_by_space)
5604 case 0: // No space separates the currency symbol and value.
5605 pat.field[1] = value;
5606 pat.field[2] = none;
5608 case 1: // Space between currency-and-sign or currency and value.
5609 pat.field[1] = value;
5610 pat.field[2] = none;
5611 if (!symbol_contains_sep) {
5612 // We insert the space into the symbol instead of
5613 // setting pat.field[2]=space so that when
5614 // showbase is not set, the space goes away too.
5615 __curr_symbol_.insert(0, 1, space_char);
5618 case 2: // Space between sign and currency or value.
5619 pat.field[1] = space;
5620 pat.field[2] = value;
5621 if (symbol_contains_sep) {
5622 // Remove the separator from the symbol, since it
5623 // has already appeared after the sign.
5624 __curr_symbol_.erase(__curr_symbol_.begin());
5631 case 2: // The sign string succeeds the quantity and currency symbol.
5632 pat.field[0] = value;
5633 pat.field[3] = sign;
5634 switch (sep_by_space)
5636 case 0: // No space separates the currency symbol and value.
5637 pat.field[1] = none;
5638 pat.field[2] = symbol;
5640 case 1: // Space between currency-and-sign or currency and value.
5641 if (!symbol_contains_sep) {
5642 // We insert the space into the symbol instead of
5643 // setting pat.field[1]=space so that when
5644 // showbase is not set, the space goes away too.
5645 __curr_symbol_.insert(0, 1, space_char);
5647 pat.field[1] = none;
5648 pat.field[2] = symbol;
5650 case 2: // Space between sign and currency or value.
5651 pat.field[1] = symbol;
5652 pat.field[2] = space;
5653 if (symbol_contains_sep) {
5654 // Remove the separator from the symbol, since it
5655 // should not be removed if showbase is absent.
5656 __curr_symbol_.erase(__curr_symbol_.begin());
5663 case 3: // The sign string immediately precedes the currency symbol.
5664 pat.field[0] = value;
5665 pat.field[3] = symbol;
5666 switch (sep_by_space)
5668 case 0: // No space separates the currency symbol and value.
5669 pat.field[1] = none;
5670 pat.field[2] = sign;
5672 case 1: // Space between currency-and-sign or currency and value.
5673 pat.field[1] = space;
5674 pat.field[2] = sign;
5675 if (symbol_contains_sep) {
5676 // Remove the separator from the symbol, since it
5677 // has already appeared before the sign.
5678 __curr_symbol_.erase(__curr_symbol_.begin());
5681 case 2: // Space between sign and currency or value.
5682 pat.field[1] = sign;
5683 pat.field[2] = none;
5684 if (!symbol_contains_sep) {
5685 // We insert the space into the symbol instead of
5686 // setting pat.field[2]=space so that when
5687 // showbase is not set, the space goes away too.
5688 __curr_symbol_.insert(0, 1, space_char);
5695 case 4: // The sign string immediately succeeds the currency symbol.
5696 pat.field[0] = value;
5697 pat.field[3] = sign;
5698 switch (sep_by_space)
5700 case 0: // No space separates the currency symbol and value.
5701 pat.field[1] = none;
5702 pat.field[2] = symbol;
5704 case 1: // Space between currency-and-sign or currency and value.
5705 pat.field[1] = none;
5706 pat.field[2] = symbol;
5707 if (!symbol_contains_sep) {
5708 // We insert the space into the symbol instead of
5709 // setting pat.field[1]=space so that when
5710 // showbase is not set, the space goes away too.
5711 __curr_symbol_.insert(0, 1, space_char);
5714 case 2: // Space between sign and currency or value.
5715 pat.field[1] = symbol;
5716 pat.field[2] = space;
5717 if (symbol_contains_sep) {
5718 // Remove the separator from the symbol, since it
5719 // should not disappear when showbase is absent.
5720 __curr_symbol_.erase(__curr_symbol_.begin());
5731 case 1: // curr_symbol before value
5734 case 0: // Parentheses surround the quantity and currency symbol.
5735 pat.field[0] = sign;
5736 pat.field[1] = symbol;
5737 pat.field[2] = none; // Any space appears in the symbol.
5738 pat.field[3] = value;
5739 switch (sep_by_space)
5741 case 0: // No space separates the currency symbol and value.
5742 // This case may have changed between C99 and C11;
5743 // assume the currency symbol matches the intention.
5744 case 2: // Space between sign and currency or value.
5745 // The "sign" is two parentheses, so no space here either.
5747 case 1: // Space between currency-and-sign or currency and value.
5748 if (!symbol_contains_sep) {
5749 // We insert the space into the symbol instead of
5750 // setting pat.field[2]=space so that when
5751 // showbase is not set, the space goes away too.
5752 __curr_symbol_.insert(0, 1, space_char);
5759 case 1: // The sign string precedes the quantity and currency symbol.
5760 pat.field[0] = sign;
5761 pat.field[3] = value;
5762 switch (sep_by_space)
5764 case 0: // No space separates the currency symbol and value.
5765 pat.field[1] = symbol;
5766 pat.field[2] = none;
5768 case 1: // Space between currency-and-sign or currency and value.
5769 pat.field[1] = symbol;
5770 pat.field[2] = none;
5771 if (!symbol_contains_sep) {
5772 // We insert the space into the symbol instead of
5773 // setting pat.field[2]=space so that when
5774 // showbase is not set, the space goes away too.
5775 __curr_symbol_.push_back(space_char);
5778 case 2: // Space between sign and currency or value.
5779 pat.field[1] = space;
5780 pat.field[2] = symbol;
5781 if (symbol_contains_sep) {
5782 // Remove the separator from the symbol, since it
5783 // has already appeared after the sign.
5784 __curr_symbol_.pop_back();
5791 case 2: // The sign string succeeds the quantity and currency symbol.
5792 pat.field[0] = symbol;
5793 pat.field[3] = sign;
5794 switch (sep_by_space)
5796 case 0: // No space separates the currency symbol and value.
5797 pat.field[1] = none;
5798 pat.field[2] = value;
5800 case 1: // Space between currency-and-sign or currency and value.
5801 pat.field[1] = none;
5802 pat.field[2] = value;
5803 if (!symbol_contains_sep) {
5804 // We insert the space into the symbol instead of
5805 // setting pat.field[1]=space so that when
5806 // showbase is not set, the space goes away too.
5807 __curr_symbol_.push_back(space_char);
5810 case 2: // Space between sign and currency or value.
5811 pat.field[1] = value;
5812 pat.field[2] = space;
5813 if (symbol_contains_sep) {
5814 // Remove the separator from the symbol, since it
5815 // will appear before the sign.
5816 __curr_symbol_.pop_back();
5823 case 3: // The sign string immediately precedes the currency symbol.
5824 pat.field[0] = sign;
5825 pat.field[3] = value;
5826 switch (sep_by_space)
5828 case 0: // No space separates the currency symbol and value.
5829 pat.field[1] = symbol;
5830 pat.field[2] = none;
5832 case 1: // Space between currency-and-sign or currency and value.
5833 pat.field[1] = symbol;
5834 pat.field[2] = none;
5835 if (!symbol_contains_sep) {
5836 // We insert the space into the symbol instead of
5837 // setting pat.field[2]=space so that when
5838 // showbase is not set, the space goes away too.
5839 __curr_symbol_.push_back(space_char);
5842 case 2: // Space between sign and currency or value.
5843 pat.field[1] = space;
5844 pat.field[2] = symbol;
5845 if (symbol_contains_sep) {
5846 // Remove the separator from the symbol, since it
5847 // has already appeared after the sign.
5848 __curr_symbol_.pop_back();
5855 case 4: // The sign string immediately succeeds the currency symbol.
5856 pat.field[0] = symbol;
5857 pat.field[3] = value;
5858 switch (sep_by_space)
5860 case 0: // No space separates the currency symbol and value.
5861 pat.field[1] = sign;
5862 pat.field[2] = none;
5864 case 1: // Space between currency-and-sign or currency and value.
5865 pat.field[1] = sign;
5866 pat.field[2] = space;
5867 if (symbol_contains_sep) {
5868 // Remove the separator from the symbol, since it
5869 // should not disappear when showbase is absent.
5870 __curr_symbol_.pop_back();
5873 case 2: // Space between sign and currency or value.
5874 pat.field[1] = none;
5875 pat.field[2] = sign;
5876 if (!symbol_contains_sep) {
5877 // We insert the space into the symbol instead of
5878 // setting pat.field[1]=space so that when
5879 // showbase is not set, the space goes away too.
5880 __curr_symbol_.push_back(space_char);
5894 pat.field[0] = symbol;
5895 pat.field[1] = sign;
5896 pat.field[2] = none;
5897 pat.field[3] = value;
5902 moneypunct_byname<char, false>::init(const char* nm)
5904 typedef moneypunct<char, false> base;
5905 __locale_unique_ptr loc(newlocale(LC_ALL_MASK, nm, 0), freelocale);
5906 #ifndef _LIBCPP_NO_EXCEPTIONS
5908 throw runtime_error("moneypunct_byname"
5909 " failed to construct for " + string(nm));
5910 #endif // _LIBCPP_NO_EXCEPTIONS
5911 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
5912 lconv* lc = localeconv_l(loc.get());
5914 lconv* lc = __localeconv_l(loc.get());
5916 if (*lc->mon_decimal_point)
5917 __decimal_point_ = *lc->mon_decimal_point;
5919 __decimal_point_ = base::do_decimal_point();
5920 if (*lc->mon_thousands_sep)
5921 __thousands_sep_ = *lc->mon_thousands_sep;
5923 __thousands_sep_ = base::do_thousands_sep();
5924 __grouping_ = lc->mon_grouping;
5925 __curr_symbol_ = lc->currency_symbol;
5926 if (lc->frac_digits != CHAR_MAX)
5927 __frac_digits_ = lc->frac_digits;
5929 __frac_digits_ = base::do_frac_digits();
5930 if (lc->p_sign_posn == 0)
5931 __positive_sign_ = "()";
5933 __positive_sign_ = lc->positive_sign;
5934 if (lc->n_sign_posn == 0)
5935 __negative_sign_ = "()";
5937 __negative_sign_ = lc->negative_sign;
5938 // Assume the positive and negative formats will want spaces in
5939 // the same places in curr_symbol since there's no way to
5940 // represent anything else.
5941 string_type __dummy_curr_symbol = __curr_symbol_;
5942 __init_pat(__pos_format_, __dummy_curr_symbol, false,
5943 lc->p_cs_precedes, lc->p_sep_by_space, lc->p_sign_posn, ' ');
5944 __init_pat(__neg_format_, __curr_symbol_, false,
5945 lc->n_cs_precedes, lc->n_sep_by_space, lc->n_sign_posn, ' ');
5950 moneypunct_byname<char, true>::init(const char* nm)
5952 typedef moneypunct<char, true> base;
5953 __locale_unique_ptr loc(newlocale(LC_ALL_MASK, nm, 0), freelocale);
5954 #ifndef _LIBCPP_NO_EXCEPTIONS
5956 throw runtime_error("moneypunct_byname"
5957 " failed to construct for " + string(nm));
5958 #endif // _LIBCPP_NO_EXCEPTIONS
5959 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
5960 lconv* lc = localeconv_l(loc.get());
5962 lconv* lc = __localeconv_l(loc.get());
5964 if (*lc->mon_decimal_point)
5965 __decimal_point_ = *lc->mon_decimal_point;
5967 __decimal_point_ = base::do_decimal_point();
5968 if (*lc->mon_thousands_sep)
5969 __thousands_sep_ = *lc->mon_thousands_sep;
5971 __thousands_sep_ = base::do_thousands_sep();
5972 __grouping_ = lc->mon_grouping;
5973 __curr_symbol_ = lc->int_curr_symbol;
5974 if (lc->int_frac_digits != CHAR_MAX)
5975 __frac_digits_ = lc->int_frac_digits;
5977 __frac_digits_ = base::do_frac_digits();
5978 #if defined(_LIBCPP_MSVCRT) || defined(__MINGW32__)
5979 if (lc->p_sign_posn == 0)
5980 #else // _LIBCPP_MSVCRT
5981 if (lc->int_p_sign_posn == 0)
5982 #endif // !_LIBCPP_MSVCRT
5983 __positive_sign_ = "()";
5985 __positive_sign_ = lc->positive_sign;
5986 #if defined(_LIBCPP_MSVCRT) || defined(__MINGW32__)
5987 if(lc->n_sign_posn == 0)
5988 #else // _LIBCPP_MSVCRT
5989 if (lc->int_n_sign_posn == 0)
5990 #endif // !_LIBCPP_MSVCRT
5991 __negative_sign_ = "()";
5993 __negative_sign_ = lc->negative_sign;
5994 // Assume the positive and negative formats will want spaces in
5995 // the same places in curr_symbol since there's no way to
5996 // represent anything else.
5997 string_type __dummy_curr_symbol = __curr_symbol_;
5998 #if defined(_LIBCPP_MSVCRT) || defined(__MINGW32__)
5999 __init_pat(__pos_format_, __dummy_curr_symbol, true,
6000 lc->p_cs_precedes, lc->p_sep_by_space, lc->p_sign_posn, ' ');
6001 __init_pat(__neg_format_, __curr_symbol_, true,
6002 lc->n_cs_precedes, lc->n_sep_by_space, lc->n_sign_posn, ' ');
6003 #else // _LIBCPP_MSVCRT
6004 __init_pat(__pos_format_, __dummy_curr_symbol, true,
6005 lc->int_p_cs_precedes, lc->int_p_sep_by_space,
6006 lc->int_p_sign_posn, ' ');
6007 __init_pat(__neg_format_, __curr_symbol_, true,
6008 lc->int_n_cs_precedes, lc->int_n_sep_by_space,
6009 lc->int_n_sign_posn, ' ');
6010 #endif // !_LIBCPP_MSVCRT
6015 moneypunct_byname<wchar_t, false>::init(const char* nm)
6017 typedef moneypunct<wchar_t, false> base;
6018 __locale_unique_ptr loc(newlocale(LC_ALL_MASK, nm, 0), freelocale);
6019 #ifndef _LIBCPP_NO_EXCEPTIONS
6021 throw runtime_error("moneypunct_byname"
6022 " failed to construct for " + string(nm));
6023 #endif // _LIBCPP_NO_EXCEPTIONS
6024 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
6025 lconv* lc = localeconv_l(loc.get());
6027 lconv* lc = __localeconv_l(loc.get());
6029 if (*lc->mon_decimal_point)
6030 __decimal_point_ = static_cast<wchar_t>(*lc->mon_decimal_point);
6032 __decimal_point_ = base::do_decimal_point();
6033 if (*lc->mon_thousands_sep)
6034 __thousands_sep_ = static_cast<wchar_t>(*lc->mon_thousands_sep);
6036 __thousands_sep_ = base::do_thousands_sep();
6037 __grouping_ = lc->mon_grouping;
6040 const char* bb = lc->currency_symbol;
6041 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
6042 size_t j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get());
6044 size_t j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get());
6046 if (j == size_t(-1))
6047 __throw_runtime_error("locale not supported");
6048 wchar_t* wbe = wbuf + j;
6049 __curr_symbol_.assign(wbuf, wbe);
6050 if (lc->frac_digits != CHAR_MAX)
6051 __frac_digits_ = lc->frac_digits;
6053 __frac_digits_ = base::do_frac_digits();
6054 if (lc->p_sign_posn == 0)
6055 __positive_sign_ = L"()";
6059 bb = lc->positive_sign;
6060 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
6061 j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get());
6063 j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get());
6065 if (j == size_t(-1))
6066 __throw_runtime_error("locale not supported");
6068 __positive_sign_.assign(wbuf, wbe);
6070 if (lc->n_sign_posn == 0)
6071 __negative_sign_ = L"()";
6075 bb = lc->negative_sign;
6076 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
6077 j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get());
6079 j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get());
6081 if (j == size_t(-1))
6082 __throw_runtime_error("locale not supported");
6084 __negative_sign_.assign(wbuf, wbe);
6086 // Assume the positive and negative formats will want spaces in
6087 // the same places in curr_symbol since there's no way to
6088 // represent anything else.
6089 string_type __dummy_curr_symbol = __curr_symbol_;
6090 __init_pat(__pos_format_, __dummy_curr_symbol, false,
6091 lc->p_cs_precedes, lc->p_sep_by_space, lc->p_sign_posn, L' ');
6092 __init_pat(__neg_format_, __curr_symbol_, false,
6093 lc->n_cs_precedes, lc->n_sep_by_space, lc->n_sign_posn, L' ');
6098 moneypunct_byname<wchar_t, true>::init(const char* nm)
6100 typedef moneypunct<wchar_t, true> base;
6101 __locale_unique_ptr loc(newlocale(LC_ALL_MASK, nm, 0), freelocale);
6102 #ifndef _LIBCPP_NO_EXCEPTIONS
6104 throw runtime_error("moneypunct_byname"
6105 " failed to construct for " + string(nm));
6106 #endif // _LIBCPP_NO_EXCEPTIONS
6107 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
6108 lconv* lc = localeconv_l(loc.get());
6110 lconv* lc = __localeconv_l(loc.get());
6112 if (*lc->mon_decimal_point)
6113 __decimal_point_ = static_cast<wchar_t>(*lc->mon_decimal_point);
6115 __decimal_point_ = base::do_decimal_point();
6116 if (*lc->mon_thousands_sep)
6117 __thousands_sep_ = static_cast<wchar_t>(*lc->mon_thousands_sep);
6119 __thousands_sep_ = base::do_thousands_sep();
6120 __grouping_ = lc->mon_grouping;
6123 const char* bb = lc->int_curr_symbol;
6124 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
6125 size_t j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get());
6127 size_t j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get());
6129 if (j == size_t(-1))
6130 __throw_runtime_error("locale not supported");
6131 wchar_t* wbe = wbuf + j;
6132 __curr_symbol_.assign(wbuf, wbe);
6133 if (lc->int_frac_digits != CHAR_MAX)
6134 __frac_digits_ = lc->int_frac_digits;
6136 __frac_digits_ = base::do_frac_digits();
6137 #if defined(_LIBCPP_MSVCRT) || defined(__MINGW32__)
6138 if (lc->p_sign_posn == 0)
6139 #else // _LIBCPP_MSVCRT
6140 if (lc->int_p_sign_posn == 0)
6141 #endif // !_LIBCPP_MSVCRT
6142 __positive_sign_ = L"()";
6146 bb = lc->positive_sign;
6147 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
6148 j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get());
6150 j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get());
6152 if (j == size_t(-1))
6153 __throw_runtime_error("locale not supported");
6155 __positive_sign_.assign(wbuf, wbe);
6157 #if defined(_LIBCPP_MSVCRT) || defined(__MINGW32__)
6158 if (lc->n_sign_posn == 0)
6159 #else // _LIBCPP_MSVCRT
6160 if (lc->int_n_sign_posn == 0)
6161 #endif // !_LIBCPP_MSVCRT
6162 __negative_sign_ = L"()";
6166 bb = lc->negative_sign;
6167 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
6168 j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get());
6170 j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get());
6172 if (j == size_t(-1))
6173 __throw_runtime_error("locale not supported");
6175 __negative_sign_.assign(wbuf, wbe);
6177 // Assume the positive and negative formats will want spaces in
6178 // the same places in curr_symbol since there's no way to
6179 // represent anything else.
6180 string_type __dummy_curr_symbol = __curr_symbol_;
6181 #if defined(_LIBCPP_MSVCRT) || defined(__MINGW32__)
6182 __init_pat(__pos_format_, __dummy_curr_symbol, true,
6183 lc->p_cs_precedes, lc->p_sep_by_space, lc->p_sign_posn, L' ');
6184 __init_pat(__neg_format_, __curr_symbol_, true,
6185 lc->n_cs_precedes, lc->n_sep_by_space, lc->n_sign_posn, L' ');
6186 #else // _LIBCPP_MSVCRT
6187 __init_pat(__pos_format_, __dummy_curr_symbol, true,
6188 lc->int_p_cs_precedes, lc->int_p_sep_by_space,
6189 lc->int_p_sign_posn, L' ');
6190 __init_pat(__neg_format_, __curr_symbol_, true,
6191 lc->int_n_cs_precedes, lc->int_n_sep_by_space,
6192 lc->int_n_sign_posn, L' ');
6193 #endif // !_LIBCPP_MSVCRT
6196 void __do_nothing(void*) {}
6198 void __throw_runtime_error(const char* msg)
6200 #ifndef _LIBCPP_NO_EXCEPTIONS
6201 throw runtime_error(msg);
6207 template class collate<char>;
6208 template class collate<wchar_t>;
6210 template class num_get<char>;
6211 template class num_get<wchar_t>;
6213 template struct __num_get<char>;
6214 template struct __num_get<wchar_t>;
6216 template class num_put<char>;
6217 template class num_put<wchar_t>;
6219 template struct __num_put<char>;
6220 template struct __num_put<wchar_t>;
6222 template class time_get<char>;
6223 template class time_get<wchar_t>;
6225 template class time_get_byname<char>;
6226 template class time_get_byname<wchar_t>;
6228 template class time_put<char>;
6229 template class time_put<wchar_t>;
6231 template class time_put_byname<char>;
6232 template class time_put_byname<wchar_t>;
6234 template class moneypunct<char, false>;
6235 template class moneypunct<char, true>;
6236 template class moneypunct<wchar_t, false>;
6237 template class moneypunct<wchar_t, true>;
6239 template class moneypunct_byname<char, false>;
6240 template class moneypunct_byname<char, true>;
6241 template class moneypunct_byname<wchar_t, false>;
6242 template class moneypunct_byname<wchar_t, true>;
6244 template class money_get<char>;
6245 template class money_get<wchar_t>;
6247 template class __money_get<char>;
6248 template class __money_get<wchar_t>;
6250 template class money_put<char>;
6251 template class money_put<wchar_t>;
6253 template class __money_put<char>;
6254 template class __money_put<wchar_t>;
6256 template class messages<char>;
6257 template class messages<wchar_t>;
6259 template class messages_byname<char>;
6260 template class messages_byname<wchar_t>;
6262 template class codecvt_byname<char, char, mbstate_t>;
6263 template class codecvt_byname<wchar_t, char, mbstate_t>;
6264 template class codecvt_byname<char16_t, char, mbstate_t>;
6265 template class codecvt_byname<char32_t, char, mbstate_t>;
6267 template class __vector_base_common<true>;
6269 _LIBCPP_END_NAMESPACE_STD