// -*- C++ -*-
//===----------------------------------------------------------------------===//
//
// 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.
//
//===----------------------------------------------------------------------===//
#ifndef _LIBCPP___LOCALE
#define _LIBCPP___LOCALE
#include <__config>
#include <string>
#include <memory>
#include <utility>
#include <mutex>
#include <cstdint>
#include <cctype>
#include <locale.h>
#if defined(_LIBCPP_MSVCRT) || defined(__MINGW32__)
# include <support/win32/locale_win32.h>
#elif _AIX
# include <support/ibm/xlocale.h>
#elif (defined(__GLIBC__) || defined(__APPLE__) || defined(__FreeBSD__) || defined(__sun__)) || defined(__EMSCRIPTEN__) || defined(__IBMCPP__)
# include <xlocale.h>
#endif // _WIN32 || __GLIBC__ || __APPLE__ || __FreeBSD__ || __sun__ || __EMSCRIPTEN__ || __IBMCPP__
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
#pragma GCC system_header
#endif
_LIBCPP_BEGIN_NAMESPACE_STD
class _LIBCPP_TYPE_VIS locale;
template <class _Facet>
_LIBCPP_INLINE_VISIBILITY
bool
has_facet(const locale&) _NOEXCEPT;
template <class _Facet>
_LIBCPP_INLINE_VISIBILITY
const _Facet&
use_facet(const locale&);
class _LIBCPP_TYPE_VIS locale
{
public:
// types:
class _LIBCPP_TYPE_VIS facet;
class _LIBCPP_TYPE_VIS id;
typedef int category;
static const category // values assigned here are for exposition only
none = 0,
collate = LC_COLLATE_MASK,
ctype = LC_CTYPE_MASK,
monetary = LC_MONETARY_MASK,
numeric = LC_NUMERIC_MASK,
time = LC_TIME_MASK,
messages = LC_MESSAGES_MASK,
all = collate | ctype | monetary | numeric | time | messages;
// construct/copy/destroy:
locale() _NOEXCEPT;
locale(const locale&) _NOEXCEPT;
explicit locale(const char*);
explicit locale(const string&);
locale(const locale&, const char*, category);
locale(const locale&, const string&, category);
template <class _Facet>
_LIBCPP_INLINE_VISIBILITY locale(const locale&, _Facet*);
locale(const locale&, const locale&, category);
~locale();
const locale& operator=(const locale&) _NOEXCEPT;
template <class _Facet> locale combine(const locale&) const;
// locale operations:
string name() const;
bool operator==(const locale&) const;
bool operator!=(const locale& __y) const {return !(*this == __y);}
template <class _CharT, class _Traits, class _Allocator>
bool operator()(const basic_string<_CharT, _Traits, _Allocator>&,
const basic_string<_CharT, _Traits, _Allocator>&) const;
// global locale objects:
static locale global(const locale&);
static const locale& classic();
private:
class __imp;
__imp* __locale_;
void __install_ctor(const locale&, facet*, long);
static locale& __global();
bool has_facet(id&) const;
const facet* use_facet(id&) const;
template <class _Facet> friend bool has_facet(const locale&) _NOEXCEPT;
template <class _Facet> friend const _Facet& use_facet(const locale&);
};
class _LIBCPP_TYPE_VIS locale::facet
: public __shared_count
{
protected:
_LIBCPP_INLINE_VISIBILITY
explicit facet(size_t __refs = 0)
: __shared_count(static_cast<long>(__refs)-1) {}
virtual ~facet();
// facet(const facet&) = delete; // effectively done in __shared_count
// void operator=(const facet&) = delete;
private:
virtual void __on_zero_shared() _NOEXCEPT;
};
class _LIBCPP_TYPE_VIS locale::id
{
once_flag __flag_;
int32_t __id_;
static int32_t __next_id;
public:
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR id() :__id_(0) {}
private:
void __init();
void operator=(const id&); // = delete;
id(const id&); // = delete;
public: // only needed for tests
long __get();
friend class locale;
friend class locale::__imp;
};
template <class _Facet>
inline _LIBCPP_INLINE_VISIBILITY
locale::locale(const locale& __other, _Facet* __f)
{
__install_ctor(__other, __f, __f ? __f->id.__get() : 0);
}
template <class _Facet>
locale
locale::combine(const locale& __other) const
{
#ifndef _LIBCPP_NO_EXCEPTIONS
if (!_VSTD::has_facet<_Facet>(__other))
throw runtime_error("locale::combine: locale missing facet");
#endif // _LIBCPP_NO_EXCEPTIONS
return locale(*this, &const_cast<_Facet&>(_VSTD::use_facet<_Facet>(__other)));
}
template <class _Facet>
inline _LIBCPP_INLINE_VISIBILITY
bool
has_facet(const locale& __l) _NOEXCEPT
{
return __l.has_facet(_Facet::id);
}
template <class _Facet>
inline _LIBCPP_INLINE_VISIBILITY
const _Facet&
use_facet(const locale& __l)
{
return static_cast<const _Facet&>(*__l.use_facet(_Facet::id));
}
// template <class _CharT> class collate;
template <class _CharT>
class _LIBCPP_TYPE_VIS_ONLY collate
: public locale::facet
{
public:
typedef _CharT char_type;
typedef basic_string<char_type> string_type;
_LIBCPP_INLINE_VISIBILITY
explicit collate(size_t __refs = 0)
: locale::facet(__refs) {}
_LIBCPP_INLINE_VISIBILITY
int compare(const char_type* __lo1, const char_type* __hi1,
const char_type* __lo2, const char_type* __hi2) const
{
return do_compare(__lo1, __hi1, __lo2, __hi2);
}
_LIBCPP_INLINE_VISIBILITY
string_type transform(const char_type* __lo, const char_type* __hi) const
{
return do_transform(__lo, __hi);
}
_LIBCPP_INLINE_VISIBILITY
long hash(const char_type* __lo, const char_type* __hi) const
{
return do_hash(__lo, __hi);
}
static locale::id id;
protected:
~collate();
virtual int do_compare(const char_type* __lo1, const char_type* __hi1,
const char_type* __lo2, const char_type* __hi2) const;
virtual string_type do_transform(const char_type* __lo, const char_type* __hi) const
{return string_type(__lo, __hi);}
virtual long do_hash(const char_type* __lo, const char_type* __hi) const;
};
template <class _CharT> locale::id collate<_CharT>::id;
template <class _CharT>
collate<_CharT>::~collate()
{
}
template <class _CharT>
int
collate<_CharT>::do_comp