blob: fbe9411c16a9dd886fa789ded2827c4693de158a [file] [log] [blame]
//===------------------------- locale.cpp ---------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "string"
#include "locale"
#include "codecvt"
#include "vector"
#include "algorithm"
#include "algorithm"
#include "typeinfo"
#include "type_traits"
#include "clocale"
#include "cstring"
#include "cwctype"
#include "__sso_allocator"
#include <langinfo.h>
#include <stdlib.h>
namespace {
typedef std::remove_pointer<locale_t>::type locale_struct;
typedef std::unique_ptr<locale_struct, decltype(&freelocale)> locale_unique_ptr;
typedef std::unique_ptr<locale_struct, decltype(&uselocale)> locale_raii;
}
namespace with_locale { namespace {
#ifdef __APPLE__
using ::btowc_l;
using ::wctob_l;
using ::wcsnrtombs_l;
using ::wcrtomb_l;
using ::mbsnrtowcs_l;
using ::mbrtowc_l;
using ::mbtowc_l;
using ::mbrlen_l;
using ::localeconv_l;
using ::mbsrtowcs_l;
decltype(MB_CUR_MAX_L(_VSTD::declval<locale_t>()))
inline _LIBCPP_INLINE_VISIBILITY
mb_cur_max_l(locale_t loc)
{
return MB_CUR_MAX_L(loc);
}
#else
template
<typename Function, typename ...Args>
auto using_locale(Function f, locale_t loc, Args&&... params) -> decltype(f(std::forward<Args>(params)...))
{
locale_raii current(uselocale(loc), uselocale);
return f(std::forward<Args>(params)...);
}
decltype(MB_CUR_MAX)
mb_cur_max_l(locale_t loc)
{
locale_raii current(uselocale(loc), uselocale);
return MB_CUR_MAX;
}
wint_t btowc_l(int c, locale_t l) { return using_locale(&btowc, l, c); }
int wctob_l(wint_t c, locale_t l) { return using_locale(&wctob, l, c); }
size_t wcsnrtombs_l(char * dest,
const wchar_t * * src,
size_t nwc,
size_t len,
mbstate_t * ps,
locale_t l)
{
return using_locale(&wcsnrtombs, l, dest, src, nwc, len, ps);
}
size_t wcrtomb_l(char *s, wchar_t wc, mbstate_t *ps, locale_t l)
{
return using_locale(&wcrtomb, l, s, wc, ps);
}
size_t mbsnrtowcs_l(wchar_t * dest,
const char * * src,
size_t nms,
size_t len,
mbstate_t * ps,
locale_t l)
{
return using_locale(&mbsnrtowcs, l, dest, src, nms, len, ps);
}
size_t mbrtowc_l(wchar_t *pwc, const char *s, size_t n, mbstate_t *ps, locale_t l)
{
return using_locale(&mbrtowc, l, pwc, s, n, ps);
}
int mbtowc_l(wchar_t * pwc, const char * pmb, size_t max, locale_t l)
{
return using_locale(&mbtowc, l, pwc, pmb, max);
}
size_t mbrlen_l(const char *s, size_t n, mbstate_t *ps, locale_t l)
{
return using_locale(&mbrlen, l, s, n, ps);
}
struct lconv *localeconv_l(locale_t l)
{
return using_locale(&localeconv, l);
}
size_t mbsrtowcs_l(wchar_t * dest,
const char * * src,
size_t len,
mbstate_t * ps,
locale_t l)
{
return using_locale(&mbsrtowcs, l, dest, src, len, ps);
}
#endif
} }
_LIBCPP_BEGIN_NAMESPACE_STD
#ifndef _LIBCPP_STABLE_APPLE_ABI
locale_t __cloc() {
// In theory this could create a race condition. In practice
// the race condition is non-fatal since it will just create
// a little resource leak. Better approach would be appreciated.
#ifdef __APPLE__
return 0;
#else
static locale_t result = newlocale(LC_ALL_MASK, "C", 0);
return result;
#endif
}
#endif // _LIBCPP_STABLE_APPLE_ABI
namespace {
struct release
{
void operator()(locale::facet* p) {p->__release_shared();}
};
template <class T, class A0>
inline
T&
make(A0 a0)
{
static typename aligned_storage<sizeof(T)>::type buf;
::new (&buf) T(a0);
return *(T*)&buf;
}
template <class T, class A0, class A1>
inline
T&
make(A0 a0, A1 a1)
{
static typename aligned_storage<sizeof(T)>::type buf;
::new (&buf) T(a0, a1);
return *(T*)&buf;
}
template <class T, class A0, class A1, class A2>
inline
T&
make(A0 a0, A1 a1, A2 a2)
{
static typename aligned_storage<sizeof(T)>::type buf;
::new (&buf) T(a0, a1, a2);
return *(T*)&buf;
}
}
class _LIBCPP_HIDDEN locale::__imp
: public facet
{
enum {N = 28};
string name_;
vector<facet*, __sso_allocator<facet*, N> > facets_;
public:
explicit __imp(size_t refs = 0);
explicit __imp(const string& name, size_t refs = 0);
__imp(const __imp&);
__imp(const __imp&, const string&, locale::category c);
__imp(const __imp& other, const __imp& one, locale::category c);
__imp(const __imp&, facet* f, long id);
~__imp();
const string& name() const {return name_;}
bool has_facet(long id) const {return id < facets_.size() && facets_[id];}
const locale::facet* use_facet(long id) const;
static const locale& make_classic();
static locale& make_global();
private:
void install(facet* f, long id);
template <class F> void install(F* f) {install(f, f->id.__get());}
template <class F> void install_from(const __imp& other);
};
locale::__imp::__imp(size_t refs)
: facet(refs),
name_("C"),
facets_(N)
{
facets_.clear();
install(&make<_VSTD::collate<char> >(1));
install(&make<_VSTD::collate<wchar_t> >(1));
install(&make<_VSTD::ctype<char> >((ctype_base::mask*)0, false, 1));
install(&make<_VSTD::ctype<wchar_t> >(1));
install(&make<codecvt<char, char, mbstate_t> >(1));
install(&make<codecvt<wchar_t, char, mbstate_t> >(1));
install(&make<codecvt<char16_t, char, mbstate_t> >(1));
install(&make<codecvt<char32_t, char, mbstate_t> >(1));
install(&make<numpunct<char> >(1));
install(&make<numpunct<wchar_t> >(1));
install(&make<num_get<char> >(1));
install(&make<num_get<wchar_t> >(1));
install(&make<num_put<char> >(1));
install(&make<num_put<wchar_t> >(1));
install(&make<moneypunct<char, false> >(1));
install(&make<moneypunct<char, true> >(1));
install(&make<moneypunct<wchar_t, false> >(1));
install(&make<moneypunct<wchar_t, true> >(1));
install(&make<money_get<char> >(1));
install(&make<money_get<wchar_t> >(1));
install(&make<money_put<char> >(1));
install(&make<money_put<wchar_t> >(1));
install(&make<time_get<char> >(1));
install(&make<time_get<wchar_t> >(1));
install(&make<time_put<char> >(1));
install(&make<time_put<wchar_t> >(1));
install(&make<_VSTD::messages<char> >(1));
install(&make<_VSTD::messages<wchar_t> >(1));
}
locale::__imp::__imp(const string& name, size_t refs)
: facet(refs),
name_(name),
facets_(N)
{
#ifndef _LIBCPP_NO_EXCEPTIONS
try
{
#endif // _LIBCPP_NO_EXCEPTIONS
facets_ = locale::classic().__locale_->facets_;
for (unsigned i = 0; i < facets_.size(); ++i)
if (facets_[i])
facets_[i]->__add_shared();
install(new collate_byname<char>(name_));
install(new collate_byname<wchar_t>(name_));
install(new ctype_byname<char>(name_));
install(new ctype_byname<wchar_t>(name_));
install(new codecvt_byname<char, char, mbstate_t>(name_));
install(new codecvt_byname<wchar_t, char, mbstate_t>(name_));
install(new codecvt_byname<char16_t, char, mbstate_t>(name_));
install(new codecvt_byname<char32_t, char, mbstate_t>(name_));
install(new numpunct_byname<char>(name_));
install(new numpunct_byname<wchar_t>(name_));
install(new moneypunct_byname<char, false>(name_));
install(new moneypunct_byname<char, true>(name_));
install(new moneypunct_byname<wchar_t, false>(name_));
install(new moneypunct_byname<wchar_t, true>(name_));
install(new time_get_byname<char>(name_));
install(new time_get_byname<wchar_t>(name_));
install(new time_put_byname<char>(name_));
install(new time_put_byname<wchar_t>(name_));
install(new messages_byname<char>(name_));
install(new messages_byname<wchar_t>(name_));
#ifndef _LIBCPP_NO_EXCEPTIONS
}
catch (...)
{
for (unsigned i = 0; i < facets_.size(); ++i)
if (facets_[i])
facets_[i]->__release_shared();
throw;
}
#endif // _LIBCPP_NO_EXCEPTIONS
}
locale::__imp::__imp(const __imp& other)
: name_(other.name_),
facets_(max<size_t>(N, other.facets_.size()))
{
facets_ = other.facets_;
for (unsigned i = 0; i < facets_.size(); ++i)
if (facets_[i])
facets_[i]->__add_shared();
}
locale::__imp::__imp(const __imp& other, const string& name, locale::category c)
: name_("*"),
facets_(N)
{
facets_ = other.facets_;
for (unsigned i = 0; i < facets_.size(); ++i)
if (facets_[i])
facets_[i]->__add_shared();
#ifndef _LIBCPP_NO_EXCEPTIONS
try
{
#endif // _LIBCPP_NO_EXCEPTIONS
if (c & locale::collate)
{
install(new collate_byname<char>(name));
install(new collate_byname<wchar_t>(name));
}
if (c & locale::ctype)
{
install(new ctype_byname<char>(name));
install(new ctype_byname<wchar_t>(name));
install(new codecvt_byname<char, char, mbstate_t>(name));
install(new codecvt_byname<wchar_t, char, mbstate_t>(name));
install(new codecvt_byname<char16_t, char, mbstate_t>(name));
install(new codecvt_byname<char32_t, char, mbstate_t>(name));
}
if (c & locale::monetary)
{
install(new moneypunct_byname<char, false>(name));
install(new moneypunct_byname<char, true>(name));
install(new moneypunct_byname<wchar_t, false>(name));
install(new moneypunct_byname<wchar_t, true>(name));
}
if (c & locale::numeric)
{
install(new numpunct_byname<char>(name));
install(new numpunct_byname<wchar_t>(name));
}
if (c & locale::time)
{
install(new time_get_byname<char>(name));
install(new time_get_byname<wchar_t>(name));
install(new time_put_byname<char>(name));
install(new time_put_byname<wchar_t>(name));
}
if (c & locale::messages)
{
install(new messages_byname<char>(name));
install(new messages_byname<wchar_t>(name));
}
#ifndef _LIBCPP_NO_EXCEPTIONS
}
catch (...)
{
for (unsigned i = 0; i < facets_.size(); ++i)
if (facets_[i])
facets_[i]->__release_shared();
throw;
}
#endif // _LIBCPP_NO_EXCEPTIONS
}
template<class F>
inline
void
locale::__imp::install_from(const locale::__imp& one)
{
long id = F::id.__get();
install(const_cast<F*>(static_cast<const F*>(one.use_facet(id))), id);
}
locale::__imp::__imp(const __imp& other, const __imp& one, locale::category c)
: name_("*"),
facets_(N)
{
facets_ = other.facets_;
for (unsigned i = 0; i < facets_.size(); ++i)
if (facets_[i])
facets_[i]->__add_shared();
#ifndef _LIBCPP_NO_EXCEPTIONS
try
{
#endif // _LIBCPP_NO_EXCEPTIONS
if (c & locale::collate)
{
install_from<_VSTD::collate<char> >(one);
install_from<_VSTD::collate<wchar_t> >(one);
}
if (c & locale::ctype)
{
install_from<_VSTD::ctype<char> >(one);
install_from<_VSTD::ctype<wchar_t> >(one);
install_from<_VSTD::codecvt<char, char, mbstate_t> >(one);
install_from<_VSTD::codecvt<char16_t, char, mbstate_t> >(one);
install_from<_VSTD::codecvt<char32_t, char, mbstate_t> >(one);
install_from<_VSTD::codecvt<wchar_t, char, mbstate_t> >(one);
}
if (c & locale::monetary)
{
install_from<moneypunct<char, false> >(one);
install_from<moneypunct<char, true> >(one);
install_from<moneypunct<wchar_t, false> >(one);
install_from<moneypunct<wchar_t, true> >(one);
install_from<money_get<char> >(one);
install_from<money_get<wchar_t> >(one);
install_from<money_put<char> >(one);
install_from<money_put<wchar_t> >(one);
}
if (c & locale::numeric)
{
install_from<numpunct<char> >(one);
install_from<numpunct<wchar_t> >(one);
install_from<num_get<char> >(one);
install_from<num_get<wchar_t> >(one);
install_from<num_put<char> >(one);
install_from<num_put<wchar_t> >(one);
}
if (c & locale::time)
{
install_from<time_get<char> >(one);
install_from<time_get<wchar_t> >(one);
install_from<time_put<char> >(one);
install_from<time_put<wchar_t> >(one);
}
if (c & locale::messages)
{
install_from<_VSTD::messages<char> >(one);
install_from<_VSTD::messages<wchar_t> >(one);
}
#ifndef _LIBCPP_NO_EXCEPTIONS
}
catch (...)
{
for (unsigned i = 0; i < facets_.size(); ++i)
if (facets_[i])
facets_[i]->__release_shared();
throw;
}
#endif // _LIBCPP_NO_EXCEPTIONS
}
locale::__imp::__imp(const __imp& other, facet* f, long id)
: name_("*"),
facets_(max<size_t>(N, other.facets_.size()+1))
{
f->__add_shared();
unique_ptr<facet, release> hold(f);
facets_ = other.facets_;
for (unsigned i = 0; i < other.facets_.size(); ++i)
if (facets_[i])
facets_[i]->__add_shared();
install(hold.get(), id);
}
locale::__imp::~__imp()
{
for (unsigned i = 0; i < facets_.size(); ++i)
if (facets_[i])
facets_[i]->__release_shared();
}
void
locale::__imp::install(facet* f, long id)
{
f->__add_shared();
unique_ptr<facet, release> hold(f);
if (id >= facets_.size())
facets_.resize(id+1);
if (facets_[id])
facets_[id]->__release_shared();
facets_[id] = hold.release();
}
const locale::facet*
locale::__imp::use_facet(long id) const
{
#ifndef _LIBCPP_NO_EXCEPTIONS
if (!has_facet(id))
throw bad_cast();
#endif // _LIBCPP_NO_EXCEPTIONS
return facets_[id];
}
// locale
const locale&
locale::__imp::make_classic()
{
// only one thread can get in here and it only gets in once
static aligned_storage<sizeof(locale)>::type buf;
locale* c = (locale*)&buf;
c->__locale_ = &make<__imp>(1);
return *c;
}
const locale&
locale::classic()
{
static const locale& c = __imp::make_classic();
return c;
}
locale&
locale::__imp::make_global()
{
// only one thread can get in here and it only gets in once
static aligned_storage<sizeof(locale)>::type buf;
locale* g = (locale*)&buf;
::new (&buf) locale(locale::classic());
return *(locale*)&buf;
}
locale&
locale::__global()
{
static locale& g = __imp::make_global();
return g;
}
locale::locale() _NOEXCEPT
: __locale_(__global().__locale_)
{
__locale_->__add_shared();
}
locale::locale(const locale& l) _NOEXCEPT
: __locale_(l.__locale_)
{
__locale_->__add_shared();
}
locale::~locale()
{
__locale_->__release_shared();
}
const locale&
locale::operator=(const locale& other) _NOEXCEPT
{
other.__locale_->__add_shared();
__locale_->__release_shared();
__locale_ = other.__locale_;
return *this;
}
locale::locale(const char* name)
#ifndef _LIBCPP_NO_EXCEPTIONS
: __locale_(name ? new __imp(name)
: throw runtime_error("locale constructed with null"))
#else // _LIBCPP_NO_EXCEPTIONS
: __locale_(new __imp(name))
#endif
{
__locale_->__add_shared();
}
locale::locale(const string& name)
: __locale_(new __imp(name))
{
__locale_->__add_shared();
}
locale::locale(const locale& other, const char* name, category c)
#ifndef _LIBCPP_NO_EXCEPTIONS
: __locale_(name ? new __imp(*other.__locale_, name, c)
: throw runtime_error("locale constructed with null"))
#else // _LIBCPP_NO_EXCEPTIONS
: __locale_(new __imp(*other.__locale_, name, c))
#endif
{
__locale_->__add_shared();
}
locale::locale(const locale& other, const string& name, category c)
: __locale_(new __imp(*other.__locale_, name, c))
{
__locale_->__add_shared();
}
locale::locale(const locale& other, const locale& one, category c)
: __locale_(new __imp(*other.__locale_, *one.__locale_, c))
{
__locale_->__add_shared();
}
string
locale::name() const
{
return __locale_->name();
}
void
locale::__install_ctor(const locale& other, facet* f, long id)
{
if (f)
__locale_ = new __imp(*other.__locale_, f, id);
else
__locale_ = other.__locale_;
__locale_->__add_shared();
}
locale
locale::global(const locale& loc)
{
locale& g = __global();
locale r = g;
g = loc;
if (g.name() != "*")
setlocale(LC_ALL, g.name().c_str());
return r;
}
bool
locale::has_facet(id& x) const
{
return __locale_->has_facet(x.__get());
}
const locale::facet*
locale::use_facet(id& x) const
{
return __locale_->use_facet(x.__get());
}
bool
locale::operator==(const locale& y) const
{
return (__locale_ == y.__locale_)
|| (__locale_->name() != "*" && __locale_->name() == y.__locale_->name());
}
// locale::facet
locale::facet::~facet()
{
}
void
locale::facet::__on_zero_shared() _NOEXCEPT
{
delete this;
}
// locale::id
int32_t locale::id::__next_id = 0;
namespace
{
class __fake_bind
{
locale::id* id_;
void (locale::id::* pmf_)();
public:
__fake_bind(void (locale::id::* pmf)(), locale::id* id)
: id_(id), pmf_(pmf) {}
void operator()() const
{
(id_->*pmf_)();
}
};
}
long
locale::id::__get()
{
call_once(__flag_, __fake_bind(&locale::id::__init, this));
return __id_ - 1;
}
void
locale::id::__init()
{
__id_ = __sync_add_and_fetch(&__next_id, 1);
}
// template <> class collate_byname<char>
collate_byname<char>::collate_byname(const char* n, size_t refs)
: collate<char>(refs),
__l(newlocale(LC_ALL_MASK, n, 0))
{
#ifndef _LIBCPP_NO_EXCEPTIONS
if (__l == 0)
throw runtime_error("collate_byname<char>::collate_byname"
" failed to construct for " + string(n));
#endif // _LIBCPP_NO_EXCEPTIONS
}
collate_byname<char>::collate_byname(const string& name, size_t refs)
: collate<char>(refs),
__l(newlocale(LC_ALL_MASK, name.c_str(), 0))
{
#ifndef _LIBCPP_NO_EXCEPTIONS
if (__l == 0)
throw runtime_error("collate_byname<char>::collate_byname"
" failed to construct for " + name);
#endif // _LIBCPP_NO_EXCEPTIONS
}
collate_byname<char>::~collate_byname()
{
freelocale(__l);
}
int
collate_byname<char>::do_compare(const char_type* __lo1, const char_type* __hi1,
const char_type* __lo2, const char_type* __hi2) const
{
string_type lhs(__lo1, __hi1);
string_type rhs(__lo2, __hi2);
int r = strcoll_l(lhs.c_str(), rhs.c_str(), __l);
if (r < 0)
return -1;
if (r > 0)
return 1;
return r;
}
collate_byname<char>::string_type
collate_byname<char>::do_transform(const char_type* lo, const char_type* hi) const
{
const string_type in(lo, hi);
string_type out(strxfrm_l(0, in.c_str(), 0, __l), char());
strxfrm_l(const_cast<char*>(out.c_str()), in.c_str(), out.size()+1, __l);
return out;
}
// template <> class collate_byname<wchar_t>
collate_byname<wchar_t>::collate_byname(const char* n, size_t refs)
: collate<wchar_t>(refs),
__l(newlocale(LC_ALL_MASK, n, 0))
{
#ifndef _LIBCPP_NO_EXCEPTIONS
if (__l == 0)
throw runtime_error("collate_byname<wchar_t>::collate_byname(size_t refs)"
" failed to construct for " + string(n));
#endif // _LIBCPP_NO_EXCEPTIONS
}
collate_byname<wchar_t>::collate_byname(const string& name, size_t refs)
: collate<wchar_t>(refs),
__l(newlocale(LC_ALL_MASK, name.c_str(), 0))
{
#ifndef _LIBCPP_NO_EXCEPTIONS
if (__l == 0)
throw runtime_error("collate_byname<wchar_t>::collate_byname(size_t refs)"
" failed to construct for " + name);
#endif // _LIBCPP_NO_EXCEPTIONS
}
collate_byname<wchar_t>::~collate_byname()
{
freelocale(__l);
}
int
collate_byname<wchar_t>::do_compare(const char_type* __lo1, const char_type* __hi1,
const char_type* __lo2, const char_type* __hi2) const
{
string_type lhs(__lo1, __hi1);
string_type rhs(__lo2, __hi2);
int r = wcscoll_l(lhs.c_str(), rhs.c_str(), __l);
if (r < 0)
return -1;
if (r > 0)
return 1;
return r;
}
collate_byname<wchar_t>::string_type
collate_byname<wchar_t>::do_transform(const char_type* lo, const char_type* hi) const
{
const string_type in(lo, hi);
string_type out(wcsxfrm_l(0, in.c_str(), 0, __l), wchar_t());
wcsxfrm_l(const_cast<wchar_t*>(out.c_str()), in.c_str(), out.size()+1, __l);
return out;
}
// template <> class ctype<wchar_t>;
locale::id ctype<wchar_t>::id;
ctype<wchar_t>::~ctype()
{
}
bool
ctype<wchar_t>::do_is(mask m, char_type c) const
{
return isascii(c) ? ctype<char>::classic_table()[c] & m : false;
}
const wchar_t*
ctype<wchar_t>::do_is(const char_type* low, const char_type* high, mask* vec) const
{
for (; low != high; ++low, ++vec)
*vec = static_cast<mask>(isascii(*low) ?
ctype<char>::classic_table()[*low] : 0);
return low;
}
const wchar_t*
ctype<wchar_t>::do_scan_is(mask m, const char_type* low, const char_type* high) const
{
for (; low != high; ++low)
if (isascii(*low) && (ctype<char>::classic_table()[*low] & m))
break;
return low;
}
const wchar_t*
ctype<wchar_t>::do_scan_not(mask m, const char_type* low, const char_type* high) const
{
for (; low != high; ++low)
if (!(isascii(*low) && (ctype<char>::classic_table()[*low] & m)))
break;
return low;
}
wchar_t
ctype<wchar_t>::do_toupper(char_type c) const
{
#ifndef _LIBCPP_STABLE_APPLE_ABI
return isascii(c) ? ctype<char>::__classic_upper_table()[c] : c;
#else
return isascii(c) ? _DefaultRuneLocale.__mapupper[c] : c;
#endif
}
const wchar_t*
ctype<wchar_t>::do_toupper(char_type* low, const char_type* high) const
{
for (; low != high; ++low)
#ifndef _LIBCPP_STABLE_APPLE_ABI
*low = isascii(*low) ? ctype<char>::__classic_upper_table()[*low]
: *low;
#else
*low = isascii(*low) ? _DefaultRuneLocale.__mapupper[*low] : *low;
#endif
return low;
}
wchar_t
ctype<wchar_t>::do_tolower(char_type c) const
{
#ifndef _LIBCPP_STABLE_APPLE_ABI
return isascii(c) ? ctype<char>::__classic_lower_table()[c] : c;
#else
return isascii(c) ? _DefaultRuneLocale.__maplower[c] : c;
#endif
}
const wchar_t*
ctype<wchar_t>::do_tolower(char_type* low, const char_type* high) const
{
for (; low != high; ++low)
#ifndef _LIBCPP_STABLE_APPLE_ABI
*low = isascii(*low) ? ctype<char>::__classic_lower_table()[*low]
: *low;
#else
*low = isascii(*low) ? _DefaultRuneLocale.__maplower[*low] : *low;
#endif
return low;
}
wchar_t
ctype<wchar_t>::do_widen(char c) const
{
return c;
}
const char*
ctype<wchar_t>::do_widen(const char* low, const char* high, char_type* dest) const
{
for (; low != high; ++low, ++dest)
*dest = *low;
return low;
}
char
ctype<wchar_t>::do_narrow(char_type c, char dfault) const
{
if (isascii(c))
return static_cast<char>(c);
return dfault;
}
const wchar_t*
ctype<wchar_t>::do_narrow(const char_type* low, const char_type* high, char dfault, char* dest) const
{
for (; low != high; ++low, ++dest)
if (isascii(*low))
*dest = *low;
else
*dest = dfault;
return low;
}
// template <> class ctype<char>;
locale::id ctype<char>::id;
ctype<char>::ctype(const mask* tab, bool del, size_t refs)
: locale::facet(refs),
__tab_(tab),
__del_(del)
{
if (__tab_ == 0)
__tab_ = classic_table();
}
ctype<char>::~ctype()
{
if (__tab_ && __del_)
delete [] __tab_;
}
char
ctype<char>::do_toupper(char_type c) const
{
#ifndef _LIBCPP_STABLE_APPLE_ABI
return isascii(c) ? __classic_upper_table()[c] : c;
#else
return isascii(c) ? _DefaultRuneLocale.__mapupper[c] : c;
#endif
}
const char*
ctype<char>::do_toupper(char_type* low, const char_type* high) const
{
for (; low != high; ++low)
#ifndef _LIBCPP_STABLE_APPLE_ABI
*low = isascii(*low) ? __classic_upper_table()[*low] : *low;
#else
*low = isascii(*low) ? _DefaultRuneLocale.__mapupper[*low] : *low;
#endif
return low;
}
char
ctype<char>::do_tolower(char_type c) const
{
#ifndef _LIBCPP_STABLE_APPLE_ABI
return isascii(c) ? __classic_lower_table()[c] : c;
#else
return isascii(c) ? _DefaultRuneLocale.__maplower[c] : c;
#endif
}
const char*
ctype<char>::do_tolower(char_type* low, const char_type* high) const
{
for (; low != high; ++low)
#ifndef _LIBCPP_STABLE_APPLE_ABI
*low = isascii(*low) ? __classic_lower_table()[*low] : *low;
#else
*low = isascii(*low) ? _DefaultRuneLocale.__maplower[*low] : *low;
#endif
return low;
}
char
ctype<char>::do_widen(char c) const
{
return c;
}
const char*
ctype<char>::do_widen(const char* low, const char* high, char_type* dest) const
{
for (; low != high; ++low, ++dest)
*dest = *low;
return low;
}
char
ctype<char>::do_narrow(char_type c, char dfault) const
{
if (isascii(c))
return static_cast<char>(c);
return dfault;
}
const char*
ctype<char>::do_narrow(const char_type* low, const char_type* high, char dfault, char* dest) const
{
for (; low != high; ++low, ++dest)
if (isascii(*low))
*dest = *low;
else
*dest = dfault;
return low;
}
const ctype<char>::mask*
ctype<char>::classic_table() _NOEXCEPT
{
#ifdef __APPLE__
return _DefaultRuneLocale.__runetype;
#elif defined(__GLIBC__)
return __cloc()->__ctype_b;
// This is assumed to be safe.
#else
return NULL;
#endif
}
#ifndef _LIBCPP_STABLE_APPLE_ABI
const int*
ctype<char>::__classic_lower_table() _NOEXCEPT
{
#ifdef __APPLE__
return _DefaultRuneLocale.__maplower;
#elif defined(__GLIBC__)
return __cloc()->__ctype_tolower;
#else
return NULL;
#endif
}
const int*
ctype<char>::__classic_upper_table() _NOEXCEPT
{
#ifdef __APPLE__
return _DefaultRuneLocale.__mapupper;
#elif defined(__GLIBC__)
return __cloc()->__ctype_toupper;
#else
return NULL;
#endif
}
#endif // _LIBCPP_STABLE_APPLE_ABI
// template <> class ctype_byname<char>
ctype_byname<char>::ctype_byname(const char* name, size_t refs)
: ctype<char>(0, false, refs),
__l(newlocale(LC_ALL_MASK, name, 0))
{
#ifndef _LIBCPP_NO_EXCEPTIONS
if (__l == 0)
throw runtime_error("ctype_byname<char>::ctype_byname"
" failed to construct for " + string(name));
#endif // _LIBCPP_NO_EXCEPTIONS
}
ctype_byname<char>::ctype_byname(const string& name, size_t refs)
: ctype<char>(0, false, refs),
__l(newlocale(LC_ALL_MASK, name.c_str(), 0))
{
#ifndef _LIBCPP_NO_EXCEPTIONS
if (__l == 0)
throw runtime_error("ctype_byname<char>::ctype_byname"
" failed to construct for " + name);
#endif // _LIBCPP_NO_EXCEPTIONS
}
ctype_byname<char>::~ctype_byname()
{
freelocale(__l);
}
char
ctype_byname<char>::do_toupper(char_type c) const
{
return toupper_l(c, __l);
}
const char*
ctype_byname<char>::do_toupper(char_type* low, const char_type* high) const
{
for (; low != high; ++low)
*low = toupper_l(*low, __l);
return low;
}
char
ctype_byname<char>::do_tolower(char_type c) const
{
return tolower_l(c, __l);
}
const char*
ctype_byname<char>::do_tolower(char_type* low, const char_type* high) const
{
for (; low != high; ++low)
*low = tolower_l(*low, __l);
return low;
}
// template <> class ctype_byname<wchar_t>
ctype_byname<wchar_t>::ctype_byname(const char* name, size_t refs)
: ctype<wchar_t>(refs),
__l(newlocale(LC_ALL_MASK, name, 0))
{
#ifndef _LIBCPP_NO_EXCEPTIONS
if (__l == 0)
throw runtime_error("ctype_byname<wchar_t>::ctype_byname"
" failed to construct for " + string(name));
#endif // _LIBCPP_NO_EXCEPTIONS
}
ctype_byname<wchar_t>::ctype_byname(const string& name, size_t refs)
: ctype<wchar_t>(refs),
__l(newlocale(LC_ALL_MASK, name.c_str(), 0))
{
#ifndef _LIBCPP_NO_EXCEPTIONS
if (__l == 0)
throw runtime_error("ctype_byname<wchar_t>::ctype_byname"
" failed to construct for " + name);
#endif // _LIBCPP_NO_EXCEPTIONS
}
ctype_byname<wchar_t>::~ctype_byname()
{
freelocale(__l);
}
bool
ctype_byname<wchar_t>::do_is(mask m, char_type c) const
{
#ifdef _LIBCPP_WCTYPE_IS_MASK
return static_cast<bool>(iswctype_l(c, m, __l));
#else
if (m & space && !iswspace_l(c, __l)) return false;
if (m & print && !iswprint_l(c, __l)) return false;
if (m & cntrl && !iswcntrl_l(c, __l)) return false;
if (m & upper && !iswupper_l(c, __l)) return false;
if (m & lower && !iswlower_l(c, __l)) return false;
if (m & alpha && !iswalpha_l(c, __l)) return false;
if (m & digit && !iswdigit_l(c, __l)) return false;
if (m & punct && !iswpunct_l(c, __l)) return false;
if (m & xdigit && !iswxdigit_l(c, __l)) return false;
if (m & blank && !iswblank_l(c, __l)) return false;
return true;
#endif
}
const wchar_t*
ctype_byname<wchar_t>::do_is(const char_type* low, const char_type* high, mask* vec) const
{
for (; low != high; ++low, ++vec)
{
if (isascii(*low))
*vec = static_cast<mask>(ctype<char>::classic_table()[*low]);
else
{
*vec = 0;
if (iswspace_l(*low, __l))
*vec |= space;
if (iswprint_l(*low, __l))
*vec |= print;
if (iswcntrl_l(*low, __l))
*vec |= cntrl;
if (iswupper_l(*low, __l))
*vec |= upper;
if (iswlower_l(*low, __l))
*vec |= lower;
if (iswalpha_l(*low, __l))
*vec |= alpha;
if (iswdigit_l(*low, __l))
*vec |= digit;
if (iswpunct_l(*low, __l))
*vec |= punct;
if (iswxdigit_l(*low, __l))
*vec |= xdigit;
}
}
return low;
}
const wchar_t*
ctype_byname<wchar_t>::do_scan_is(mask m, const char_type* low, const char_type* high) const
{
for (; low != high; ++low)
{
#ifdef _LIBCPP_WCTYPE_IS_MASK
if (iswctype_l(*low, m, __l))
break;
#else
if (m & space && !iswspace_l(*low, __l)) continue;
if (m & print && !iswprint_l(*low, __l)) continue;
if (m & cntrl && !iswcntrl_l(*low, __l)) continue;
if (m & upper && !iswupper_l(*low, __l)) continue;
if (m & lower && !iswlower_l(*low, __l)) continue;
if (m & alpha && !iswalpha_l(*low, __l)) continue;
if (m & digit && !iswdigit_l(*low, __l)) continue;
if (m & punct && !iswpunct_l(*low, __l)) continue;
if (m & xdigit && !iswxdigit_l(*low, __l)) continue;
if (m & blank && !iswblank_l(*low, __l)) continue;
break;
#endif
}
return low;
}
const wchar_t*
ctype_byname<wchar_t>::do_scan_not(mask m, const char_type* low, const char_type* high) const
{
for (; low != high; ++low)
{
#ifdef _LIBCPP_WCTYPE_IS_MASK
if (!iswctype_l(*low, m, __l))
break;
#else
if (m & space && iswspace_l(*low, __l)) continue;
if (m & print && iswprint_l(*low, __l)) continue;
if (m & cntrl && iswcntrl_l(*low, __l)) continue;
if (m & upper && iswupper_l(*low, __l)) continue;
if (m & lower && iswlower_l(*low, __l)) continue;
if (m & alpha && iswalpha_l(*low, __l)) continue;
if (m & digit && iswdigit_l(*low, __l)) continue;
if (m & punct && iswpunct_l(*low, __l)) continue;
if (m & xdigit && iswxdigit_l(*low, __l)) continue;
if (m & blank && iswblank_l(*low, __l)) continue;
break;
#endif
}
return low;
}
wchar_t
ctype_byname<wchar_t>::do_toupper(char_type c) const
{
return towupper_l(c, __l);
}
const wchar_t*
ctype_byname<wchar_t>::do_toupper(char_type* low, const char_type* high) const
{
for (; low != high; ++low)
*low = towupper_l(*low, __l);
return low;
}
wchar_t
ctype_byname<wchar_t>::do_tolower(char_type c) const
{
return towlower_l(c, __l);
}
const wchar_t*
ctype_byname<wchar_t>::do_tolower(char_type* low, const char_type* high) const
{
for (; low != high; ++low)
*low = towlower_l(*low, __l);
return low;
}
wchar_t
ctype_byname<wchar_t>::do_widen(char c) const
{
return with_locale::btowc_l(c, __l);
}
const char*
ctype_byname<wchar_t>::do_widen(const char* low, const char* high, char_type* dest) const
{
for (; low != high; ++low, ++dest)
*dest = with_locale::btowc_l(*low, __l);
return low;
}
char
ctype_byname<wchar_t>::do_narrow(char_type c, char dfault) const
{
int r = with_locale::wctob_l(c, __l);
return r != WEOF ? static_cast<char>(r) : dfault;
}
const wchar_t*
ctype_byname<wchar_t>::do_narrow(const char_type* low, const char_type* high, char dfault, char* dest) const
{
for (; low != high; ++low, ++dest)
{
int r = with_locale::wctob_l(*low, __l);
*dest = r != WEOF ? static_cast<char>(r) : dfault;
}
return low;
}
// template <> class codecvt<char, char, mbstate_t>
locale::id codecvt<char, char, mbstate_t>::id;
codecvt<char, char, mbstate_t>::~codecvt()
{
}
codecvt<char, char, mbstate_t>::result
codecvt<char, char, mbstate_t>::do_out(state_type&,
const intern_type* frm, const intern_type*, const intern_type*& frm_nxt,
extern_type* to, extern_type*, extern_type*& to_nxt) const
{
frm_nxt = frm;
to_nxt = to;
return noconv;
}
codecvt<char, char, mbstate_t>::result
codecvt<char, char, mbstate_t>::do_in(state_type&,
const extern_type* frm, const extern_type*, const extern_type*& frm_nxt,
intern_type* to, intern_type*, intern_type*& to_nxt) const
{
frm_nxt = frm;
to_nxt = to;
return noconv;
}
codecvt<char, char, mbstate_t>::result
codecvt<char, char, mbstate_t>::do_unshift(state_type&,
extern_type* to, extern_type*, extern_type*& to_nxt) const
{
to_nxt = to;
return noconv;
}
int
codecvt<char, char, mbstate_t>::do_encoding() const _NOEXCEPT
{
return 1;
}
bool
codecvt<char, char, mbstate_t>::do_always_noconv() const _NOEXCEPT
{
return true;
}
int
codecvt<char, char, mbstate_t>::do_length(state_type&,
const extern_type* frm, const extern_type* end, size_t mx) const
{
return static_cast<int>(min<size_t>(mx, end-frm));
}
int
codecvt<char, char, mbstate_t>::do_max_length() const _NOEXCEPT
{
return 1;
}
// template <> class codecvt<wchar_t, char, mbstate_t>
locale::id codecvt<wchar_t, char, mbstate_t>::id;
codecvt<wchar_t, char, mbstate_t>::codecvt(size_t refs)
: locale::facet(refs),
__l(0)
{
}
codecvt<wchar_t, char, mbstate_t>::codecvt(const char* nm, size_t refs)
: locale::facet(refs),
__l(newlocale(LC_ALL_MASK, nm, 0))
{
#ifndef _LIBCPP_NO_EXCEPTIONS
if (__l == 0)
throw runtime_error("codecvt_byname<wchar_t, char, mbstate_t>::codecvt_byname"
" failed to construct for " + string(nm));
#endif // _LIBCPP_NO_EXCEPTIONS
}
codecvt<wchar_t, char, mbstate_t>::~codecvt()
{
if (__l != 0)
freelocale(__l);
}
codecvt<wchar_t, char, mbstate_t>::result
codecvt<wchar_t, char, mbstate_t>::do_out(state_type& st,
const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt,
extern_type* to, extern_type* to_end, extern_type*& to_nxt) const
{
// look for first internal null in frm
const intern_type* fend = frm;
for (; fend != frm_end; ++fend)
if (*fend == 0)
break;
// loop over all null-terminated sequences in frm
to_nxt = to;
for (frm_nxt = frm; frm != frm_end && to != to_end; frm = frm_nxt, to = to_nxt)
{
// save state in case needed to reover to_nxt on error
mbstate_t save_state = st;
size_t n = with_locale::wcsnrtombs_l(to, &frm_nxt, fend-frm, to_end-to, &st, __l);
if (n == size_t(-1))
{
// need to recover to_nxt
for (to_nxt = to; frm != frm_nxt; ++frm)
{
n = with_locale::wcrtomb_l(to_nxt, *frm, &save_state, __l);
if (n == size_t(-1))
break;
to_nxt += n;
}
frm_nxt = frm;
return error;
}
if (n == 0)
return partial;
to_nxt += n;
if (to_nxt == to_end)
break;
if (fend != frm_end) // set up next null terminated sequence
{
// Try to write the terminating null
extern_type tmp[MB_LEN_MAX];
n = with_locale::wcrtomb_l(tmp, intern_type(), &st, __l);
if (n == size_t(-1)) // on error
return error;
if (n > to_end-to_nxt) // is there room?
return partial;
for (extern_type* p = tmp; n; --n) // write it
*to_nxt++ = *p++;
++frm_nxt;
// look for next null in frm
for (fend = frm_nxt; fend != frm_end; ++fend)
if (*fend == 0)
break;
}
}
return frm_nxt == frm_end ? ok : partial;
}
codecvt<wchar_t, char, mbstate_t>::result
codecvt<wchar_t, char, mbstate_t>::do_in(state_type& st,
const extern_type* frm, const extern_type* frm_end, const extern_type*& frm_nxt,
intern_type* to, intern_type* to_end, intern_type*& to_nxt) const
{
// look for first internal null in frm
const extern_type* fend = frm;
for (; fend != frm_end; ++fend)
if (*fend == 0)
break;
// loop over all null-terminated sequences in frm
to_nxt = to;
for (frm_nxt = frm; frm != frm_end && to != to_end; frm = frm_nxt, to = to_nxt)
{
// save state in case needed to reover to_nxt on error
mbstate_t save_state = st;
size_t n = with_locale::mbsnrtowcs_l(to, &frm_nxt, fend-frm, to_end-to, &st, __l);
if (n == size_t(-1))
{
// need to recover to_nxt
for (to_nxt = to; frm != frm_nxt; ++to_nxt)
{
n = with_locale::mbrtowc_l(to_nxt, frm, fend-frm, &save_state, __l);
switch (n)
{
case 0:
++frm;
break;
case -1:
frm_nxt = frm;
return error;
case -2:
frm_nxt = frm;
return partial;
default:
frm += n;
break;
}
}
frm_nxt = frm;
return frm_nxt == frm_end ? ok : partial;
}
if (n == 0)
return error;
to_nxt += n;
if (to_nxt == to_end)
break;
if (fend != frm_end) // set up next null terminated sequence
{
// Try to write the terminating null
n = with_locale::mbrtowc_l(to_nxt, frm_nxt, 1, &st, __l);
if (n != 0) // on error
return error;
++to_nxt;
++frm_nxt;
// look for next null in frm
for (fend = frm_nxt; fend != frm_end; ++fend)
if (*fend == 0)
break;
}
}
return frm_nxt == frm_end ? ok : partial;
}
codecvt<wchar_t, char, mbstate_t>::result
codecvt<wchar_t, char, mbstate_t>::do_unshift(state_type& st,
extern_type* to, extern_type* to_end, extern_type*& to_nxt) const
{
to_nxt = to;
extern_type tmp[MB_LEN_MAX];
size_t n = with_locale::wcrtomb_l(tmp, intern_type(), &st, __l);
if (n == size_t(-1) || n == 0) // on error
return error;
--n;
if (n > to_end-to_nxt) // is there room?
return partial;
for (extern_type* p = tmp; n; --n) // write it
*to_nxt++ = *p++;
return ok;
}
int
codecvt<wchar_t, char, mbstate_t>::do_encoding() const _NOEXCEPT
{
if (with_locale::mbtowc_l((wchar_t*) 0, (const char*) 0, MB_LEN_MAX, __l) == 0)
{
// stateless encoding
if (__l == 0 || with_locale::mb_cur_max_l(__l) == 1) // there are no known constant length encodings
return 1; // which take more than 1 char to form a wchar_t
return 0;
}
return -1;
}
bool
codecvt<wchar_t, char, mbstate_t>::do_always_noconv() const _NOEXCEPT
{
return false;
}
int
codecvt<wchar_t, char, mbstate_t>::do_length(state_type& st,
const extern_type* frm, const extern_type* frm_end, size_t mx) const
{
int nbytes = 0;
for (size_t nwchar_t = 0; nwchar_t < mx && frm != frm_end; ++nwchar_t)
{
size_t n = with_locale::mbrlen_l(frm, frm_end-frm, &st, __l);
switch (n)
{
case 0:
++nbytes;
++frm;
break;
case -1:
case -2:
return nbytes;
default:
nbytes += n;
frm += n;
break;
}
}
return nbytes;
}
int
codecvt<wchar_t, char, mbstate_t>::do_max_length() const _NOEXCEPT
{
return __l == 0 ? 1 : with_locale::mb_cur_max_l(__l);
}
// Valid UTF ranges
// UTF-32 UTF-16 UTF-8 # of code points
// first second first second third fourth
// 000000 - 00007F 0000 - 007F 00 - 7F 127
// 000080 - 0007FF 0080 - 07FF C2 - DF, 80 - BF 1920
// 000800 - 000FFF 0800 - 0FFF E0 - E0, A0 - BF, 80 - BF 2048
// 001000 - 00CFFF 1000 - CFFF E1 - EC, 80 - BF, 80 - BF 49152
// 00D000 - 00D7FF D000 - D7FF ED - ED, 80 - 9F, 80 - BF 2048
// 00D800 - 00DFFF invalid
// 00E000 - 00FFFF E000 - FFFF EE - EF, 80 - BF, 80 - BF 8192
// 010000 - 03FFFF D800 - D8BF, DC00 - DFFF F0 - F0, 90 - BF, 80 - BF, 80 - BF 196608
// 040000 - 0FFFFF D8C0 - DBBF, DC00 - DFFF F1 - F3, 80 - BF, 80 - BF, 80 - BF 786432
// 100000 - 10FFFF DBC0 - DBFF, DC00 - DFFF F4 - F4, 80 - 8F, 80 - BF, 80 - BF 65536
static
codecvt_base::result
utf16_to_utf8(const uint16_t* frm, const uint16_t* frm_end, const uint16_t*& frm_nxt,
uint8_t* to, uint8_t* to_end, uint8_t*& to_nxt,
unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0))
{
frm_nxt = frm;
to_nxt = to;
if (mode & generate_header)
{
if (to_end-to_nxt < 3)
return codecvt_base::partial;
*to_nxt++ = static_cast<uint8_t>(0xEF);
*to_nxt++ = static_cast<uint8_t>(0xBB);
*to_nxt++ = static_cast<uint8_t>(0xBF);
}
for (; frm_nxt < frm_end; ++frm_nxt)
{
uint16_t wc1 = *frm_nxt;
if (wc1 > Maxcode)
return codecvt_base::error;
if (wc1 < 0x0080)
{
if (to_end-to_nxt < 1)
return codecvt_base::partial;
*to_nxt++ = static_cast<uint8_t>(wc1);
}
else if (wc1 < 0x0800)
{
if (to_end-to_nxt < 2)
return codecvt_base::partial;
*to_nxt++ = static_cast<uint8_t>(0xC0 | (wc1 >> 6));
*to_nxt++ = static_cast<uint8_t>(0x80 | (wc1 & 0x03F));
}
else if (wc1 < 0xD800)
{
if (to_end-to_nxt < 3)
return codecvt_base::partial;
*to_nxt++ = static_cast<uint8_t>(0xE0 | (wc1 >> 12));
*to_nxt++ = static_cast<uint8_t>(0x80 | ((wc1 & 0x0FC0) >> 6));
*to_nxt++ = static_cast<uint8_t>(0x80 | (wc1 & 0x003F));
}
else if (wc1 < 0xDC00)
{
if (frm_end-frm_nxt < 2)
return codecvt_base::partial;
uint16_t wc2 = frm_nxt[1];
if ((wc2 & 0xFC00) != 0xDC00)
return codecvt_base::error;
if (to_end-to_nxt < 4)
return codecvt_base::partial;
if ((((((unsigned long)wc1 & 0x03C0) >> 6) + 1) << 16) +
(((unsigned long)wc1 & 0x003F) << 10) + (wc2 & 0x03FF) > Maxcode)
return codecvt_base::error;
++frm_nxt;
uint8_t z = ((wc1 & 0x03C0) >> 6) + 1;
*to_nxt++ = static_cast<uint8_t>(0xF0 | (z >> 2));
*to_nxt++ = static_cast<uint8_t>(0x80 | ((z & 0x03) << 4) | ((wc1 & 0x003C) >> 2));
*to_nxt++ = static_cast<uint8_t>(0x80 | ((wc1 & 0x0003) << 4) | ((wc2 & 0x03C0) >> 6));
*to_nxt++ = static_cast<uint8_t>(0x80 | (wc2 & 0x003F));
}
else if (wc1 < 0xE000)
{
return codecvt_base::error;
}
else
{
if (to_end-to_nxt < 3)
return codecvt_base::partial;
*to_nxt++ = static_cast<uint8_t>(0xE0 | (wc1 >> 12));
*to_nxt++ = static_cast<uint8_t>(0x80 | ((wc1 & 0x0FC0) >> 6));
*to_nxt++ = static_cast<uint8_t>(0x80 | (wc1 & 0x003F));
}
}
return codecvt_base::ok;
}
static
codecvt_base::result
utf16_to_utf8(const uint32_t* frm, const uint32_t* frm_end, const uint32_t*& frm_nxt,
uint8_t* to, uint8_t* to_end, uint8_t*& to_nxt,
unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0))
{
frm_nxt = frm;
to_nxt = to;
if (mode & generate_header)
{
if (to_end-to_nxt < 3)
return codecvt_base::partial;
*to_nxt++ = static_cast<uint8_t>(0xEF);
*to_nxt++ = static_cast<uint8_t>(0xBB);
*to_nxt++ = static_cast<uint8_t>(0xBF);
}
for (; frm_nxt < frm_end; ++frm_nxt)
{
uint16_t wc1 = static_cast<uint16_t>(*frm_nxt);
if (wc1 > Maxcode)
return codecvt_base::error;
if (wc1 < 0x0080)
{
if (to_end-to_nxt < 1)
return codecvt_base::partial;
*to_nxt++ = static_cast<uint8_t>(wc1);
}
else if (wc1 < 0x0800)
{
if (to_end-to_nxt < 2)
return codecvt_base::partial;
*to_nxt++ = static_cast<uint8_t>(0xC0 | (wc1 >> 6));
*to_nxt++ = static_cast<uint8_t>(0x80 | (wc1 & 0x03F));
}
else if (wc1 < 0xD800)
{
if (to_end-to_nxt < 3)
return codecvt_base::partial;
*to_nxt++ = static_cast<uint8_t>(0xE0 | (wc1 >> 12));
*to_nxt++ = static_cast<uint8_t>(0x80 | ((wc1 & 0x0FC0) >> 6));
*to_nxt++ = static_cast<uint8_t>(0x80 | (wc1 & 0x003F));
}
else if (wc1 < 0xDC00)
{
if (frm_end-frm_nxt < 2)
return codecvt_base::partial;
uint16_t wc2 = static_cast<uint16_t>(frm_nxt[1]);
if ((wc2 & 0xFC00) != 0xDC00)
return codecvt_base::error;
if (to_end-to_nxt < 4)
return codecvt_base::partial;
if ((((((unsigned long)wc1 & 0x03C0) >> 6) + 1) << 16) +
(((unsigned long)wc1 & 0x003F) << 10) + (wc2 & 0x03FF) > Maxcode)
return codecvt_base::error;
++frm_nxt;
uint8_t z = ((wc1 & 0x03C0) >> 6) + 1;
*to_nxt++ = static_cast<uint8_t>(0xF0 | (z >> 2));
*to_nxt++ = static_cast<uint8_t>(0x80 | ((z & 0x03) << 4) | ((wc1 & 0x003C) >> 2));
*to_nxt++ = static_cast<uint8_t>(0x80 | ((wc1 & 0x0003) << 4) | ((wc2 & 0x03C0) >> 6));
*to_nxt++ = static_cast<uint8_t>(0x80 | (wc2 & 0x003F));
}
else if (wc1 < 0xE000)
{
return codecvt_base::error;
}
else
{
if (to_end-to_nxt < 3)
return codecvt_base::partial;
*to_nxt++ = static_cast<uint8_t>(0xE0 | (wc1 >> 12));
*to_nxt++ = static_cast<uint8_t>(0x80 | ((wc1 & 0x0FC0) >> 6));
*to_nxt++ = static_cast<uint8_t>(0x80 | (wc1 & 0x003F));
}
}
return codecvt_base::ok;
}
static
codecvt_base::result
utf8_to_utf16(const uint8_t* frm, const uint8_t* frm_end, const uint8_t*& frm_nxt,
uint16_t* to, uint16_t* to_end, uint16_t*& to_nxt,
unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0))
{
frm_nxt = frm;
to_nxt = to;
if (mode & consume_header)
{
if (frm_end-frm_nxt >= 3 && frm_nxt[0] == 0xEF && frm_nxt[1] == 0xBB &&
frm_nxt[2] == 0xBF)
frm_nxt += 3;
}
for (; frm_nxt < frm_end && to_nxt < to_end; ++to_nxt)
{
uint8_t c1 = *frm_nxt;
if (c1 > Maxcode)
return codecvt_base::error;
if (c1 < 0x80)
{
*to_nxt = static_cast<uint16_t>(c1);
++frm_nxt;
}
else if (c1 < 0xC2)
{
return codecvt_base::error;
}
else if (c1 < 0xE0)
{
if (frm_end-frm_nxt < 2)
return codecvt_base::partial;
uint8_t c2 = frm_nxt[1];
if ((c2 & 0xC0) != 0x80)
return codecvt_base::error;
uint16_t t = static_cast<uint16_t>(((c1 & 0x1F) << 6) | (c2 & 0x3F));
if (t > Maxcode)
return codecvt_base::error;
*to_nxt = t;
frm_nxt += 2;
}
else if (c1 < 0xF0)
{
if (frm_end-frm_nxt < 3)
return codecvt_base::partial;
uint8_t c2 = frm_nxt[1];
uint8_t c3 = frm_nxt[2];
switch (c1)
{
case 0xE0:
if ((c2 & 0xE0) != 0xA0)
return codecvt_base::error;
break;
case 0xED:
if ((c2 & 0xE0) != 0x80)
return codecvt_base::error;
break;
default:
if ((c2 & 0xC0) != 0x80)
return codecvt_base::error;
break;
}
if ((c3 & 0xC0) != 0x80)
return codecvt_base::error;
uint16_t t = static_cast<uint16_t>(((c1 & 0x0F) << 12)
| ((c2 & 0x3F) << 6)
| (c3 & 0x3F));
if (t > Maxcode)
return codecvt_base::error;
*to_nxt = t;
frm_nxt += 3;
}
else if (c1 < 0xF5)
{
if (frm_end-frm_nxt < 4)
return codecvt_base::partial;
uint8_t c2 = frm_nxt[1];
uint8_t c3 = frm_nxt[2];
uint8_t c4 = frm_nxt[3];
switch (c1)
{
case 0xF0:
if (!(0x90 <= c2 && c2 <= 0xBF))
return codecvt_base::error;
break;
case 0xF4:
if ((c2 & 0xF0) != 0x80)
return codecvt_base::error;
break;
default:
if ((c2 & 0xC0) != 0x80)
return codecvt_base::error;
break;
}
if ((c3 & 0xC0) != 0x80 || (c4 & 0xC0) != 0x80)
return codecvt_base::error;
if (to_end-to_nxt < 2)
return codecvt_base::partial;
if (((((unsigned long)c1 & 7) << 18) +
(((unsigned long)c2 & 0x3F) << 12) +
(((unsigned long)c3 & 0x3F) << 6) + (c4 & 0x3F)) > Maxcode)
return codecvt_base::error;
*to_nxt = static_cast<uint16_t>(
0xD800
| (((((c1 & 0x07) << 2) | ((c2 & 0x30) >> 4)) - 1) << 6)
| ((c2 & 0x0F) << 2)
| ((c3 & 0x30) >> 4));
*++to_nxt = static_cast<uint16_t>(
0xDC00
| ((c3 & 0x0F) << 6)
| (c4 & 0x3F));
frm_nxt += 4;
}
else
{
return codecvt_base::error;
}
}
return frm_nxt < frm_end ? codecvt_base::partial : codecvt_base::ok;
}
static
codecvt_base::result
utf8_to_utf16(const uint8_t* frm, const uint8_t* frm_end, const uint8_t*& frm_nxt,
uint32_t* to, uint32_t* to_end, uint32_t*& to_nxt,
unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0))
{
frm_nxt = frm;
to_nxt = to;
if (mode & consume_header)
{
if (frm_end-frm_nxt >= 3 && frm_nxt[0] == 0xEF && frm_nxt[1] == 0xBB &&
frm_nxt[2] == 0xBF)
frm_nxt += 3;
}
for (; frm_nxt < frm_end && to_nxt < to_end; ++to_nxt)
{
uint8_t c1 = *frm_nxt;
if (c1 > Maxcode)
return codecvt_base::error;
if (c1 < 0x80)
{
*to_nxt = static_cast<uint32_t>(c1);
++frm_nxt;
}
else if (c1 < 0xC2)
{
return codecvt_base::error;
}
else if (c1 < 0xE0)
{
if (frm_end-frm_nxt < 2)
return codecvt_base::partial;
uint8_t c2 = frm_nxt[1];
if ((c2 & 0xC0) != 0x80)
return codecvt_base::error;
uint16_t t = static_cast<uint16_t>(((c1 & 0x1F) << 6) | (c2 & 0x3F));
if (t > Maxcode)
return codecvt_base::error;
*to_nxt = static_cast<uint32_t>(t);
frm_nxt += 2;
}
else if (c1 < 0xF0)
{
if (frm_end-frm_nxt < 3)
return codecvt_base::partial;
uint8_t c2 = frm_nxt[1];
uint8_t c3 = frm_nxt[2];
switch (c1)
{
case 0xE0:
if ((c2 & 0xE0) != 0xA0)
return codecvt_base::error;
break;
case 0xED:
if ((c2 & 0xE0) != 0x80)
return codecvt_base::error;
break;
default:
if ((c2 & 0xC0) != 0x80)
return codecvt_base::error;
break;
}
if ((c3 & 0xC0) != 0x80)
return codecvt_base::error;
uint16_t t = static_cast<uint16_t>(((c1 & 0x0F) << 12)
| ((c2 & 0x3F) << 6)
| (c3 & 0x3F));
if (t > Maxcode)
return codecvt_base::error;
*to_nxt = static_cast<uint32_t>(t);
frm_nxt += 3;
}
else if (c1 < 0xF5)
{
if (frm_end-frm_nxt < 4)
return codecvt_base::partial;
uint8_t c2 = frm_nxt[1];
uint8_t c3 = frm_nxt[2];
uint8_t c4 = frm_nxt[3];
switch (c1)
{
case 0xF0:
if (!(0x90 <= c2 && c2 <= 0xBF))
return codecvt_base::error;
break;
case 0xF4:
if ((c2 & 0xF0) != 0x80)
return codecvt_base::error;
break;
default:
if ((c2 & 0xC0) != 0x80)
return codecvt_base::error;
break;
}
if ((c3 & 0xC0) != 0x80 || (c4 & 0xC0) != 0x80)
return codecvt_base::error;
if (to_end-to_nxt < 2)
return codecvt_base::partial;
if (((((unsigned long)c1 & 7) << 18) +
(((unsigned long)c2 & 0x3F) << 12) +
(((unsigned long)c3 & 0x3F) << 6) + (c4 & 0x3F)) > Maxcode)
return codecvt_base::error;
*to_nxt = static_cast<uint32_t>(
0xD800
| (((((c1 & 0x07) << 2) | ((c2 & 0x30) >> 4)) - 1) << 6)
| ((c2 & 0x0F) << 2)
| ((c3 & 0x30) >> 4));
*++to_nxt = static_cast<uint32_t>(
0xDC00
| ((c3 & 0x0F) << 6)
| (c4 & 0x3F));
frm_nxt += 4;
}
else
{
return codecvt_base::error;
}
}
return frm_nxt < frm_end ? codecvt_base::partial : codecvt_base::ok;
}
static
int
utf8_to_utf16_length(const uint8_t* frm, const uint8_t* frm_end,
size_t mx, unsigned long Maxcode = 0x10FFFF,
codecvt_mode mode = codecvt_mode(0))
{
const uint8_t* frm_nxt = frm;
if (mode & consume_header)
{
if (frm_end-frm_nxt >= 3 && frm_nxt[0] == 0xEF && frm_nxt[1] == 0xBB &&
frm_nxt[2] == 0xBF)
frm_nxt += 3;
}
for (size_t nchar16_t = 0; frm_nxt < frm_end && nchar16_t < mx; ++nchar16_t)
{
uint8_t c1 = *frm_nxt;
if (c1 > Maxcode)
break;
if (c1 < 0x80)
{
++frm_nxt;
}
else if (c1 < 0xC2)
{
break;
}
else if (c1 < 0xE0)
{
if ((frm_end-frm_nxt < 2) || (frm_nxt[1] & 0xC0) != 0x80)
break;
uint16_t t = static_cast<uint16_t>(((c1 & 0x1F) << 6) | (frm_nxt[1] & 0x3F));
if (t > Maxcode)
break;
frm_nxt += 2;
}
else if (c1 < 0xF0)
{
if (frm_end-frm_nxt < 3)
break;
uint8_t c2 = frm_nxt[1];
uint8_t c3 = frm_nxt[2];
uint16_t t = static_cast<uint16_t>(((c1 & 0x0F) << 12)
| ((c2 & 0x3F) << 6)
| (c3 & 0x3F));
switch (c1)
{
case 0xE0:
if ((c2 & 0xE0) != 0xA0)
return static_cast<int>(frm_nxt - frm);
break;
case 0xED:
if ((c2 & 0xE0) != 0x80)
return static_cast<int>(frm_nxt - frm);
break;
default:
if ((c2 & 0xC0) != 0x80)
return static_cast<int>(frm_nxt - frm);
break;
}
if ((c3 & 0xC0) != 0x80)
break;
if ((((c1 & 0x0F) << 12) | ((c2 & 0x3F) << 6) | (c3 & 0x3F)) > Maxcode)
break;
frm_nxt += 3;
}
else if (c1 < 0xF5)
{
if (frm_end-frm_nxt < 4 || mx-nchar16_t < 2)
break;
uint8_t c2 = frm_nxt[1];
uint8_t c3 = frm_nxt[2];
uint8_t c4 = frm_nxt[3];
switch (c1)
{
case 0xF0:
if (!(0x90 <= c2 && c2 <= 0xBF))
return static_cast<int>(frm_nxt - frm);
break;
case 0xF4:
if ((c2 & 0xF0) != 0x80)
return static_cast<int>(frm_nxt - frm);
break;
default:
if ((c2 & 0xC0) != 0x80)
return static_cast<int>(frm_nxt - frm);
break;
}
if ((c3 & 0xC0) != 0x80 || (c4 & 0xC0) != 0x80)
break;
if (((((unsigned long)c1 & 7) << 18) +
(((unsigned long)c2 & 0x3F) << 12) +
(((unsigned long)c3 & 0x3F) << 6) + (c4 & 0x3F)) > Maxcode)
break;
++nchar16_t;
frm_nxt += 4;
}
else
{
break;
}
}
return static_cast<int>(frm_nxt - frm);
}
static
codecvt_base::result
ucs4_to_utf8(const uint32_t* frm, const uint32_t* frm_end, const uint32_t*& frm_nxt,
uint8_t* to, uint8_t* to_end, uint8_t*& to_nxt,
unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0))
{
frm_nxt = frm;
to_nxt = to;
if (mode & generate_header)
{
if (to_end-to_nxt < 3)
return codecvt_base::partial;
*to_nxt++ = static_cast<uint8_t>(0xEF);
*to_nxt++ = static_cast<uint8_t>(0xBB);
*to_nxt++ = static_cast<uint8_t>(0xBF);
}
for (; frm_nxt < frm_end; ++frm_nxt)
{
uint32_t wc = *frm_nxt;
if ((wc & 0xFFFFF800) == 0x00D800 || wc > Maxcode)
return codecvt_base::error;
if (wc < 0x000080)
{
if (to_end-to_nxt < 1)
return codecvt_base::partial;
*to_nxt++ = static_cast<uint8_t>(wc);
}
else if (wc < 0x000800)
{
if (to_end-to_nxt < 2)
return codecvt_base::partial;
*to_nxt++ = static_cast<uint8_t>(0xC0 | (wc >> 6));
*to_nxt++ = static_cast<uint8_t>(0x80 | (wc & 0x03F));
}
else if (wc < 0x010000)
{
if (to_end-to_nxt < 3)
return codecvt_base::partial;
*to_nxt++ = static_cast<uint8_t>(0xE0 | (wc >> 12));
*to_nxt++ = static_cast<uint8_t>(0x80 | ((wc & 0x0FC0) >> 6));
*to_nxt++ = static_cast<uint8_t>(0x80 | (wc & 0x003F));
}
else // if (wc < 0x110000)
{
if (to_end-to_nxt < 4)
return codecvt_base::partial;
*to_nxt++ = static_cast<uint8_t>(0xF0 | (wc >> 18));
*to_nxt++ = static_cast<uint8_t>(0x80 | ((wc & 0x03F000) >> 12));
*to_nxt++ = static_cast<uint8_t>(0x80 | ((wc & 0x000FC0) >> 6));
*to_nxt++ = static_cast<uint8_t>(0x80 | (wc & 0x00003F));
}
}
return codecvt_base::ok;
}
static
codecvt_base::result
utf8_to_ucs4(const uint8_t* frm, const uint8_t* frm_end, const uint8_t*& frm_nxt,
uint32_t* to, uint32_t* to_end, uint32_t*& to_nxt,
unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0))
{
frm_nxt = frm;
to_nxt = to;
if (mode & consume_header)
{
if (frm_end-frm_nxt >= 3 && frm_nxt[0] == 0xEF && frm_nxt[1] == 0xBB &&
frm_nxt[2] == 0xBF)
frm_nxt += 3;
}
for (; frm_nxt < frm_end && to_nxt < to_end; ++to_nxt)
{
uint8_t c1 = static_cast<uint8_t>(*frm_nxt);
if (c1 < 0x80)
{
if (c1 > Maxcode)
return codecvt_base::error;
*to_nxt = static_cast<uint32_t>(c1);
++frm_nxt;
}
else if (c1 < 0xC2)
{
return codecvt_base::error;
}
else if (c1 < 0xE0)
{
if (frm_end-frm_nxt < 2)
return codecvt_base::partial;
uint8_t c2 = frm_nxt[1];
if ((c2 & 0xC0) != 0x80)
return codecvt_base::error;
uint32_t t = static_cast<uint32_t>(((c1 & 0x1F) << 6)
| (c2 & 0x3F));
if (t > Maxcode)
return codecvt_base::error;
*to_nxt = t;
frm_nxt += 2;
}
else if (c1 < 0xF0)
{
if (frm_end-frm_nxt < 3)
return codecvt_base::partial;
uint8_t c2 = frm_nxt[1];
uint8_t c3 = frm_nxt[2];
switch (c1)
{
case 0xE0:
if ((c2 & 0xE0) != 0xA0)
return codecvt_base::error;
break;
case 0xED:
if ((c2 & 0xE0) != 0x80)
return codecvt_base::error;
break;
default:
if ((c2 & 0xC0) != 0x80)
return codecvt_base::error;
break;
}
if ((c3 & 0xC0) != 0x80)
return codecvt_base::error;
uint32_t t = static_cast<uint32_t>(((c1 & 0x0F) << 12)
| ((c2 & 0x3F) << 6)
| (c3 & 0x3F));
if (t > Maxcode)
return codecvt_base::error;
*to_nxt = t;
frm_nxt += 3;
}
else if (c1 < 0xF5)
{
if (frm_end-frm_nxt < 4)
return codecvt_base::partial;
uint8_t c2 = frm_nxt[1];
uint8_t c3 = frm_nxt[2];
uint8_t c4 = frm_nxt[3];
switch (c1)
{
case 0xF0:
if (!(0x90 <= c2 && c2 <= 0xBF))
return codecvt_base::error;
break;
case 0xF4:
if ((c2 & 0xF0) != 0x80)
return codecvt_base::error;
break;
default:
if ((c2 & 0xC0) != 0x80)
return codecvt_base::error;
break;
}
if ((c3 & 0xC0) != 0x80 || (c4 & 0xC0) != 0x80)
return codecvt_base::error;
uint32_t t = static_cast<uint32_t>(((c1 & 0x07) << 18)
| ((c2 & 0x3F) << 12)
| ((c3 & 0x3F) << 6)
| (c4 & 0x3F));
if (t > Maxcode)
return codecvt_base::error;
*to_nxt = t;
frm_nxt += 4;
}
else
{
return codecvt_base::error;
}
}
return frm_nxt < frm_end ? codecvt_base::partial : codecvt_base::ok;
}
static
int
utf8_to_ucs4_length(const uint8_t* frm, const uint8_t* frm_end,
size_t mx, unsigned long Maxcode = 0x10FFFF,
codecvt_mode mode = codecvt_mode(0))
{
const uint8_t* frm_nxt = frm;
if (mode & consume_header)
{
if (frm_end-frm_nxt >= 3 && frm_nxt[0] == 0xEF && frm_nxt[1] == 0xBB &&
frm_nxt[2] == 0xBF)
frm_nxt += 3;
}
for (size_t nchar32_t = 0; frm_nxt < frm_end && nchar32_t < mx; ++nchar32_t)
{
uint8_t c1 = static_cast<uint8_t>(*frm_nxt);
if (c1 < 0x80)
{
if (c1 > Maxcode)
break;
++frm_nxt;
}
else if (c1 < 0xC2)
{
break;
}
else if (c1 < 0xE0)
{
if ((frm_end-frm_nxt < 2) || ((frm_nxt[1] & 0xC0) != 0x80))
break;
if ((((c1 & 0x1F) << 6) | (frm_nxt[1] & 0x3F)) > Maxcode)
break;
frm_nxt += 2;
}
else if (c1 < 0xF0)
{
if (frm_end-frm_nxt < 3)
break;
uint8_t c2 = frm_nxt[1];
uint8_t c3 = frm_nxt[2];
switch (c1)
{
case 0xE0:
if ((c2 & 0xE0) != 0xA0)
return static_cast<int>(frm_nxt - frm);
break;
case 0xED:
if ((c2 & 0xE0) != 0x80)
return static_cast<int>(frm_nxt - frm);
break;
default:
if ((c2 & 0xC0) != 0x80)
return static_cast<int>(frm_nxt - frm);
break;
}
if ((c3 & 0xC0) != 0x80)
break;
if ((((c1 & 0x0F) << 12) | ((c2 & 0x3F) << 6) | (c3 & 0x3F)) > Maxcode)
break;
frm_nxt += 3;
}
else if (c1 < 0xF5)
{
if (frm_end-frm_nxt < 4)
break;
uint8_t c2 = frm_nxt[1];
uint8_t c3 = frm_nxt[2];
uint8_t c4 = frm_nxt[3];
switch (c1)
{
case 0xF0:
if (!(0x90 <= c2 && c2 <= 0xBF))
return static_cast<int>(frm_nxt - frm);
break;
case 0xF4:
if ((c2 & 0xF0) != 0x80)
return static_cast<int>(frm_nxt - frm);
break;
default:
if ((c2 & 0xC0) != 0x80)
return static_cast<int>(frm_nxt - frm);
break;
}
if ((c3 & 0xC0) != 0x80 || (c4 & 0xC0) != 0x80)
break;
uint32_t t = static_cast<uint32_t>(((c1 & 0x07) << 18)
| ((c2 & 0x3F) << 12)
| ((c3 & 0x3F) << 6)
| (c4 & 0x3F));
if ((((c1 & 0x07) << 18) | ((c2 & 0x3F) << 12) |
((c3 & 0x3F) << 6) | (c4 & 0x3F)) > Maxcode)
break;
frm_nxt += 4;
}
else
{
break;
}
}
return static_cast<int>(frm_nxt - frm);
}
static
codecvt_base::result
ucs2_to_utf8(const uint16_t* frm, const uint16_t* frm_end, const uint16_t*& frm_nxt,
uint8_t* to, uint8_t* to_end, uint8_t*& to_nxt,
unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0))
{
frm_nxt = frm;
to_nxt = to;
if (mode & generate_header)
{
if (to_end-to_nxt < 3)
return codecvt_base::partial;
*to_nxt++ = static_cast<uint8_t>(0xEF);
*to_nxt++ = static_cast<uint8_t>(0xBB);
*to_nxt++ = static_cast<uint8_t>(0xBF);
}
for (; frm_nxt < frm_end; ++frm_nxt)
{
uint16_t wc = *frm_nxt;
if ((wc & 0xF800) == 0xD800 || wc > Maxcode)
return codecvt_base::error;
if (wc < 0x0080)
{
if (to_end-to_nxt < 1)
return codecvt_base::partial;
*to_nxt++ = static_cast<uint8_t>(wc);
}
else if (wc < 0x0800)
{
if (to_end-to_nxt < 2)
return codecvt_base::partial;
*to_nxt++ = static_cast<uint8_t>(0xC0 | (wc >> 6));
*to_nxt++ = static_cast<uint8_t>(0x80 | (wc & 0x03F));
}
else // if (wc <= 0xFFFF)
{
if (to_end-to_nxt < 3)
return codecvt_base::partial;
*to_nxt++ = static_cast<uint8_t>(0xE0 | (wc >> 12));
*to_nxt++ = static_cast<uint8_t>(0x80 | ((wc & 0x0FC0) >> 6));
*to_nxt++ = static_cast<uint8_t>(0x80 | (wc & 0x003F));
}
}
return codecvt_base::ok;
}
static
codecvt_base::result
utf8_to_ucs2(const uint8_t* frm, const uint8_t* frm_end, const uint8_t*& frm_nxt,
uint16_t* to, uint16_t* to_end, uint16_t*& to_nxt,
unsigned long Maxcode = 0x10FFFF, codecvt_mode mode = codecvt_mode(0))
{
frm_nxt = frm;
to_nxt = to;
if (mode & consume_header)
{
if (frm_end-frm_nxt >= 3 && frm_nxt[0] == 0xEF && frm_nxt[1] == 0xBB &&
frm_nxt[2] == 0xBF)
frm_nxt += 3;
}
for (; frm_nxt < frm_end && to_nxt < to_end; ++to_nxt)
{
uint8_t c1 = static_cast<uint8_t>(*frm_nxt);
if (c1 < 0x80)
{
if (c1 > Maxcode)
return codecvt_base::error;
*to_nxt = static_cast<uint16_t>(c1);
++frm_nxt;
}
else if (c1 < 0xC2)
{
return codecvt_base::error;
}
else if (c1 < 0xE0)
{
if (frm_end-frm_nxt < 2)
return codecvt_base::partial;
uint8_t c2 = frm_nxt[1];
if ((c2 & 0xC0) != 0x80)
return codecvt_base::error;
uint16_t t = static_cast<uint16_t>(((c1 & 0x1F) << 6)
| (c2 & 0x3F));
if (t > Maxcode)
return codecvt_base::error;