xref: /freebsd/contrib/llvm-project/libcxx/include/__locale (revision 0b57cec536236d46e3dba9bd041533462f33dbb7)
1*0b57cec5SDimitry Andric// -*- C++ -*-
2*0b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
3*0b57cec5SDimitry Andric//
4*0b57cec5SDimitry Andric// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5*0b57cec5SDimitry Andric// See https://llvm.org/LICENSE.txt for license information.
6*0b57cec5SDimitry Andric// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7*0b57cec5SDimitry Andric//
8*0b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
9*0b57cec5SDimitry Andric
10*0b57cec5SDimitry Andric#ifndef _LIBCPP___LOCALE
11*0b57cec5SDimitry Andric#define _LIBCPP___LOCALE
12*0b57cec5SDimitry Andric
13*0b57cec5SDimitry Andric#include <__config>
14*0b57cec5SDimitry Andric#include <string>
15*0b57cec5SDimitry Andric#include <memory>
16*0b57cec5SDimitry Andric#include <utility>
17*0b57cec5SDimitry Andric#include <mutex>
18*0b57cec5SDimitry Andric#include <cstdint>
19*0b57cec5SDimitry Andric#include <cctype>
20*0b57cec5SDimitry Andric#include <locale.h>
21*0b57cec5SDimitry Andric#if defined(_LIBCPP_MSVCRT_LIKE)
22*0b57cec5SDimitry Andric# include <cstring>
23*0b57cec5SDimitry Andric# include <support/win32/locale_win32.h>
24*0b57cec5SDimitry Andric#elif defined(_AIX)
25*0b57cec5SDimitry Andric# include <support/ibm/xlocale.h>
26*0b57cec5SDimitry Andric#elif defined(__ANDROID__)
27*0b57cec5SDimitry Andric# include <support/android/locale_bionic.h>
28*0b57cec5SDimitry Andric#elif defined(__sun__)
29*0b57cec5SDimitry Andric# include <xlocale.h>
30*0b57cec5SDimitry Andric# include <support/solaris/xlocale.h>
31*0b57cec5SDimitry Andric#elif defined(_NEWLIB_VERSION)
32*0b57cec5SDimitry Andric# include <support/newlib/xlocale.h>
33*0b57cec5SDimitry Andric#elif (defined(__APPLE__)      || defined(__FreeBSD__) \
34*0b57cec5SDimitry Andric    || defined(__EMSCRIPTEN__) || defined(__IBMCPP__))
35*0b57cec5SDimitry Andric# include <xlocale.h>
36*0b57cec5SDimitry Andric#elif defined(__Fuchsia__)
37*0b57cec5SDimitry Andric# include <support/fuchsia/xlocale.h>
38*0b57cec5SDimitry Andric#elif defined(__wasi__)
39*0b57cec5SDimitry Andric// WASI libc uses musl's locales support.
40*0b57cec5SDimitry Andric# include <support/musl/xlocale.h>
41*0b57cec5SDimitry Andric#elif defined(_LIBCPP_HAS_MUSL_LIBC)
42*0b57cec5SDimitry Andric# include <support/musl/xlocale.h>
43*0b57cec5SDimitry Andric#endif
44*0b57cec5SDimitry Andric
45*0b57cec5SDimitry Andric#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
46*0b57cec5SDimitry Andric#pragma GCC system_header
47*0b57cec5SDimitry Andric#endif
48*0b57cec5SDimitry Andric
49*0b57cec5SDimitry Andric_LIBCPP_BEGIN_NAMESPACE_STD
50*0b57cec5SDimitry Andric
51*0b57cec5SDimitry Andric#if !defined(_LIBCPP_LOCALE__L_EXTENSIONS)
52*0b57cec5SDimitry Andricstruct __libcpp_locale_guard {
53*0b57cec5SDimitry Andric  _LIBCPP_INLINE_VISIBILITY
54*0b57cec5SDimitry Andric  __libcpp_locale_guard(locale_t& __loc) : __old_loc_(uselocale(__loc)) {}
55*0b57cec5SDimitry Andric
56*0b57cec5SDimitry Andric  _LIBCPP_INLINE_VISIBILITY
57*0b57cec5SDimitry Andric  ~__libcpp_locale_guard() {
58*0b57cec5SDimitry Andric    if (__old_loc_)
59*0b57cec5SDimitry Andric      uselocale(__old_loc_);
60*0b57cec5SDimitry Andric  }
61*0b57cec5SDimitry Andric
62*0b57cec5SDimitry Andric  locale_t __old_loc_;
63*0b57cec5SDimitry Andricprivate:
64*0b57cec5SDimitry Andric  __libcpp_locale_guard(__libcpp_locale_guard const&);
65*0b57cec5SDimitry Andric  __libcpp_locale_guard& operator=(__libcpp_locale_guard const&);
66*0b57cec5SDimitry Andric};
67*0b57cec5SDimitry Andric#elif defined(_LIBCPP_MSVCRT_LIKE)
68*0b57cec5SDimitry Andricstruct __libcpp_locale_guard {
69*0b57cec5SDimitry Andric    __libcpp_locale_guard(locale_t __l) :
70*0b57cec5SDimitry Andric        __status(_configthreadlocale(_ENABLE_PER_THREAD_LOCALE)) {
71*0b57cec5SDimitry Andric      // Setting the locale can be expensive even when the locale given is
72*0b57cec5SDimitry Andric      // already the current locale, so do an explicit check to see if the
73*0b57cec5SDimitry Andric      // current locale is already the one we want.
74*0b57cec5SDimitry Andric      const char* __lc = __setlocale(nullptr);
75*0b57cec5SDimitry Andric      // If every category is the same, the locale string will simply be the
76*0b57cec5SDimitry Andric      // locale name, otherwise it will be a semicolon-separated string listing
77*0b57cec5SDimitry Andric      // each category.  In the second case, we know at least one category won't
78*0b57cec5SDimitry Andric      // be what we want, so we only have to check the first case.
79*0b57cec5SDimitry Andric      if (strcmp(__l.__get_locale(), __lc) != 0) {
80*0b57cec5SDimitry Andric        __locale_all = _strdup(__lc);
81*0b57cec5SDimitry Andric        if (__locale_all == nullptr)
82*0b57cec5SDimitry Andric          __throw_bad_alloc();
83*0b57cec5SDimitry Andric        __setlocale(__l.__get_locale());
84*0b57cec5SDimitry Andric      }
85*0b57cec5SDimitry Andric    }
86*0b57cec5SDimitry Andric    ~__libcpp_locale_guard() {
87*0b57cec5SDimitry Andric      // The CRT documentation doesn't explicitly say, but setlocale() does the
88*0b57cec5SDimitry Andric      // right thing when given a semicolon-separated list of locale settings
89*0b57cec5SDimitry Andric      // for the different categories in the same format as returned by
90*0b57cec5SDimitry Andric      // setlocale(LC_ALL, nullptr).
91*0b57cec5SDimitry Andric      if (__locale_all != nullptr) {
92*0b57cec5SDimitry Andric        __setlocale(__locale_all);
93*0b57cec5SDimitry Andric        free(__locale_all);
94*0b57cec5SDimitry Andric      }
95*0b57cec5SDimitry Andric      _configthreadlocale(__status);
96*0b57cec5SDimitry Andric    }
97*0b57cec5SDimitry Andric    static const char* __setlocale(const char* __locale) {
98*0b57cec5SDimitry Andric      const char* __new_locale = setlocale(LC_ALL, __locale);
99*0b57cec5SDimitry Andric      if (__new_locale == nullptr)
100*0b57cec5SDimitry Andric        __throw_bad_alloc();
101*0b57cec5SDimitry Andric      return __new_locale;
102*0b57cec5SDimitry Andric    }
103*0b57cec5SDimitry Andric    int __status;
104*0b57cec5SDimitry Andric    char* __locale_all = nullptr;
105*0b57cec5SDimitry Andric};
106*0b57cec5SDimitry Andric#endif
107*0b57cec5SDimitry Andric
108*0b57cec5SDimitry Andric
109*0b57cec5SDimitry Andricclass _LIBCPP_TYPE_VIS locale;
110*0b57cec5SDimitry Andric
111*0b57cec5SDimitry Andrictemplate <class _Facet>
112*0b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY
113*0b57cec5SDimitry Andricbool
114*0b57cec5SDimitry Andrichas_facet(const locale&) _NOEXCEPT;
115*0b57cec5SDimitry Andric
116*0b57cec5SDimitry Andrictemplate <class _Facet>
117*0b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY
118*0b57cec5SDimitry Andricconst _Facet&
119*0b57cec5SDimitry Andricuse_facet(const locale&);
120*0b57cec5SDimitry Andric
121*0b57cec5SDimitry Andricclass _LIBCPP_TYPE_VIS locale
122*0b57cec5SDimitry Andric{
123*0b57cec5SDimitry Andricpublic:
124*0b57cec5SDimitry Andric    // types:
125*0b57cec5SDimitry Andric    class _LIBCPP_TYPE_VIS facet;
126*0b57cec5SDimitry Andric    class _LIBCPP_TYPE_VIS id;
127*0b57cec5SDimitry Andric
128*0b57cec5SDimitry Andric    typedef int category;
129*0b57cec5SDimitry Andric    _LIBCPP_AVAILABILITY_LOCALE_CATEGORY
130*0b57cec5SDimitry Andric    static const category // values assigned here are for exposition only
131*0b57cec5SDimitry Andric        none     = 0,
132*0b57cec5SDimitry Andric        collate  = LC_COLLATE_MASK,
133*0b57cec5SDimitry Andric        ctype    = LC_CTYPE_MASK,
134*0b57cec5SDimitry Andric        monetary = LC_MONETARY_MASK,
135*0b57cec5SDimitry Andric        numeric  = LC_NUMERIC_MASK,
136*0b57cec5SDimitry Andric        time     = LC_TIME_MASK,
137*0b57cec5SDimitry Andric        messages = LC_MESSAGES_MASK,
138*0b57cec5SDimitry Andric        all = collate | ctype | monetary | numeric | time | messages;
139*0b57cec5SDimitry Andric
140*0b57cec5SDimitry Andric    // construct/copy/destroy:
141*0b57cec5SDimitry Andric    locale()  _NOEXCEPT;
142*0b57cec5SDimitry Andric    locale(const locale&)  _NOEXCEPT;
143*0b57cec5SDimitry Andric    explicit locale(const char*);
144*0b57cec5SDimitry Andric    explicit locale(const string&);
145*0b57cec5SDimitry Andric    locale(const locale&, const char*, category);
146*0b57cec5SDimitry Andric    locale(const locale&, const string&, category);
147*0b57cec5SDimitry Andric    template <class _Facet>
148*0b57cec5SDimitry Andric        _LIBCPP_INLINE_VISIBILITY locale(const locale&, _Facet*);
149*0b57cec5SDimitry Andric    locale(const locale&, const locale&, category);
150*0b57cec5SDimitry Andric
151*0b57cec5SDimitry Andric    ~locale();
152*0b57cec5SDimitry Andric
153*0b57cec5SDimitry Andric    const locale& operator=(const locale&)  _NOEXCEPT;
154*0b57cec5SDimitry Andric
155*0b57cec5SDimitry Andric    template <class _Facet>
156*0b57cec5SDimitry Andric      _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
157*0b57cec5SDimitry Andric      locale combine(const locale&) const;
158*0b57cec5SDimitry Andric
159*0b57cec5SDimitry Andric    // locale operations:
160*0b57cec5SDimitry Andric    string name() const;
161*0b57cec5SDimitry Andric    bool operator==(const locale&) const;
162*0b57cec5SDimitry Andric    bool operator!=(const locale& __y) const {return !(*this == __y);}
163*0b57cec5SDimitry Andric    template <class _CharT, class _Traits, class _Allocator>
164*0b57cec5SDimitry Andric      _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
165*0b57cec5SDimitry Andric      bool operator()(const basic_string<_CharT, _Traits, _Allocator>&,
166*0b57cec5SDimitry Andric                      const basic_string<_CharT, _Traits, _Allocator>&) const;
167*0b57cec5SDimitry Andric
168*0b57cec5SDimitry Andric    // global locale objects:
169*0b57cec5SDimitry Andric    static locale global(const locale&);
170*0b57cec5SDimitry Andric    static const locale& classic();
171*0b57cec5SDimitry Andric
172*0b57cec5SDimitry Andricprivate:
173*0b57cec5SDimitry Andric    class __imp;
174*0b57cec5SDimitry Andric    __imp* __locale_;
175*0b57cec5SDimitry Andric
176*0b57cec5SDimitry Andric    void __install_ctor(const locale&, facet*, long);
177*0b57cec5SDimitry Andric    static locale& __global();
178*0b57cec5SDimitry Andric    bool has_facet(id&) const;
179*0b57cec5SDimitry Andric    const facet* use_facet(id&) const;
180*0b57cec5SDimitry Andric
181*0b57cec5SDimitry Andric    template <class _Facet> friend bool has_facet(const locale&)  _NOEXCEPT;
182*0b57cec5SDimitry Andric    template <class _Facet> friend const _Facet& use_facet(const locale&);
183*0b57cec5SDimitry Andric};
184*0b57cec5SDimitry Andric
185*0b57cec5SDimitry Andricclass _LIBCPP_TYPE_VIS locale::facet
186*0b57cec5SDimitry Andric    : public __shared_count
187*0b57cec5SDimitry Andric{
188*0b57cec5SDimitry Andricprotected:
189*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
190*0b57cec5SDimitry Andric    explicit facet(size_t __refs = 0)
191*0b57cec5SDimitry Andric        : __shared_count(static_cast<long>(__refs)-1) {}
192*0b57cec5SDimitry Andric
193*0b57cec5SDimitry Andric    virtual ~facet();
194*0b57cec5SDimitry Andric
195*0b57cec5SDimitry Andric//    facet(const facet&) = delete;     // effectively done in __shared_count
196*0b57cec5SDimitry Andric//    void operator=(const facet&) = delete;
197*0b57cec5SDimitry Andricprivate:
198*0b57cec5SDimitry Andric    virtual void __on_zero_shared() _NOEXCEPT;
199*0b57cec5SDimitry Andric};
200*0b57cec5SDimitry Andric
201*0b57cec5SDimitry Andricclass _LIBCPP_TYPE_VIS locale::id
202*0b57cec5SDimitry Andric{
203*0b57cec5SDimitry Andric    once_flag      __flag_;
204*0b57cec5SDimitry Andric    int32_t        __id_;
205*0b57cec5SDimitry Andric
206*0b57cec5SDimitry Andric    static int32_t __next_id;
207*0b57cec5SDimitry Andricpublic:
208*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR id() :__id_(0) {}
209*0b57cec5SDimitry Andricprivate:
210*0b57cec5SDimitry Andric    void __init();
211*0b57cec5SDimitry Andric    void operator=(const id&); // = delete;
212*0b57cec5SDimitry Andric    id(const id&); // = delete;
213*0b57cec5SDimitry Andricpublic:  // only needed for tests
214*0b57cec5SDimitry Andric    long __get();
215*0b57cec5SDimitry Andric
216*0b57cec5SDimitry Andric    friend class locale;
217*0b57cec5SDimitry Andric    friend class locale::__imp;
218*0b57cec5SDimitry Andric};
219*0b57cec5SDimitry Andric
220*0b57cec5SDimitry Andrictemplate <class _Facet>
221*0b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
222*0b57cec5SDimitry Andriclocale::locale(const locale& __other, _Facet* __f)
223*0b57cec5SDimitry Andric{
224*0b57cec5SDimitry Andric    __install_ctor(__other, __f, __f ? __f->id.__get() : 0);
225*0b57cec5SDimitry Andric}
226*0b57cec5SDimitry Andric
227*0b57cec5SDimitry Andrictemplate <class _Facet>
228*0b57cec5SDimitry Andriclocale
229*0b57cec5SDimitry Andriclocale::combine(const locale& __other) const
230*0b57cec5SDimitry Andric{
231*0b57cec5SDimitry Andric    if (!_VSTD::has_facet<_Facet>(__other))
232*0b57cec5SDimitry Andric        __throw_runtime_error("locale::combine: locale missing facet");
233*0b57cec5SDimitry Andric
234*0b57cec5SDimitry Andric    return locale(*this, &const_cast<_Facet&>(_VSTD::use_facet<_Facet>(__other)));
235*0b57cec5SDimitry Andric}
236*0b57cec5SDimitry Andric
237*0b57cec5SDimitry Andrictemplate <class _Facet>
238*0b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
239*0b57cec5SDimitry Andricbool
240*0b57cec5SDimitry Andrichas_facet(const locale& __l)  _NOEXCEPT
241*0b57cec5SDimitry Andric{
242*0b57cec5SDimitry Andric    return __l.has_facet(_Facet::id);
243*0b57cec5SDimitry Andric}
244*0b57cec5SDimitry Andric
245*0b57cec5SDimitry Andrictemplate <class _Facet>
246*0b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
247*0b57cec5SDimitry Andricconst _Facet&
248*0b57cec5SDimitry Andricuse_facet(const locale& __l)
249*0b57cec5SDimitry Andric{
250*0b57cec5SDimitry Andric    return static_cast<const _Facet&>(*__l.use_facet(_Facet::id));
251*0b57cec5SDimitry Andric}
252*0b57cec5SDimitry Andric
253*0b57cec5SDimitry Andric// template <class _CharT> class collate;
254*0b57cec5SDimitry Andric
255*0b57cec5SDimitry Andrictemplate <class _CharT>
256*0b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS collate
257*0b57cec5SDimitry Andric    : public locale::facet
258*0b57cec5SDimitry Andric{
259*0b57cec5SDimitry Andricpublic:
260*0b57cec5SDimitry Andric    typedef _CharT char_type;
261*0b57cec5SDimitry Andric    typedef basic_string<char_type> string_type;
262*0b57cec5SDimitry Andric
263*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
264*0b57cec5SDimitry Andric    explicit collate(size_t __refs = 0)
265*0b57cec5SDimitry Andric        : locale::facet(__refs) {}
266*0b57cec5SDimitry Andric
267*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
268*0b57cec5SDimitry Andric    int compare(const char_type* __lo1, const char_type* __hi1,
269*0b57cec5SDimitry Andric                const char_type* __lo2, const char_type* __hi2) const
270*0b57cec5SDimitry Andric    {
271*0b57cec5SDimitry Andric        return do_compare(__lo1, __hi1, __lo2, __hi2);
272*0b57cec5SDimitry Andric    }
273*0b57cec5SDimitry Andric
274*0b57cec5SDimitry Andric    // FIXME(EricWF): The _LIBCPP_ALWAYS_INLINE is needed on Windows to work
275*0b57cec5SDimitry Andric    // around a dllimport bug that expects an external instantiation.
276*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
277*0b57cec5SDimitry Andric    _LIBCPP_ALWAYS_INLINE
278*0b57cec5SDimitry Andric    string_type transform(const char_type* __lo, const char_type* __hi) const
279*0b57cec5SDimitry Andric    {
280*0b57cec5SDimitry Andric        return do_transform(__lo, __hi);
281*0b57cec5SDimitry Andric    }
282*0b57cec5SDimitry Andric
283*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
284*0b57cec5SDimitry Andric    long hash(const char_type* __lo, const char_type* __hi) const
285*0b57cec5SDimitry Andric    {
286*0b57cec5SDimitry Andric        return do_hash(__lo, __hi);
287*0b57cec5SDimitry Andric    }
288*0b57cec5SDimitry Andric
289*0b57cec5SDimitry Andric    static locale::id id;
290*0b57cec5SDimitry Andric
291*0b57cec5SDimitry Andricprotected:
292*0b57cec5SDimitry Andric    ~collate();
293*0b57cec5SDimitry Andric    virtual int do_compare(const char_type* __lo1, const char_type* __hi1,
294*0b57cec5SDimitry Andric                           const char_type* __lo2, const char_type* __hi2) const;
295*0b57cec5SDimitry Andric    virtual string_type do_transform(const char_type* __lo, const char_type* __hi) const
296*0b57cec5SDimitry Andric        {return string_type(__lo, __hi);}
297*0b57cec5SDimitry Andric    virtual long do_hash(const char_type* __lo, const char_type* __hi) const;
298*0b57cec5SDimitry Andric};
299*0b57cec5SDimitry Andric
300*0b57cec5SDimitry Andrictemplate <class _CharT> locale::id collate<_CharT>::id;
301*0b57cec5SDimitry Andric
302*0b57cec5SDimitry Andrictemplate <class _CharT>
303*0b57cec5SDimitry Andriccollate<_CharT>::~collate()
304*0b57cec5SDimitry Andric{
305*0b57cec5SDimitry Andric}
306*0b57cec5SDimitry Andric
307*0b57cec5SDimitry Andrictemplate <class _CharT>
308*0b57cec5SDimitry Andricint
309*0b57cec5SDimitry Andriccollate<_CharT>::do_compare(const char_type* __lo1, const char_type* __hi1,
310*0b57cec5SDimitry Andric                            const char_type* __lo2, const char_type* __hi2) const
311*0b57cec5SDimitry Andric{
312*0b57cec5SDimitry Andric    for (; __lo2 != __hi2; ++__lo1, ++__lo2)
313*0b57cec5SDimitry Andric    {
314*0b57cec5SDimitry Andric        if (__lo1 == __hi1 || *__lo1 < *__lo2)
315*0b57cec5SDimitry Andric            return -1;
316*0b57cec5SDimitry Andric        if (*__lo2 < *__lo1)
317*0b57cec5SDimitry Andric            return 1;
318*0b57cec5SDimitry Andric    }
319*0b57cec5SDimitry Andric    return __lo1 != __hi1;
320*0b57cec5SDimitry Andric}
321*0b57cec5SDimitry Andric
322*0b57cec5SDimitry Andrictemplate <class _CharT>
323*0b57cec5SDimitry Andriclong
324*0b57cec5SDimitry Andriccollate<_CharT>::do_hash(const char_type* __lo, const char_type* __hi) const
325*0b57cec5SDimitry Andric{
326*0b57cec5SDimitry Andric    size_t __h = 0;
327*0b57cec5SDimitry Andric    const size_t __sr = __CHAR_BIT__ * sizeof(size_t) - 8;
328*0b57cec5SDimitry Andric    const size_t __mask = size_t(0xF) << (__sr + 4);
329*0b57cec5SDimitry Andric    for(const char_type* __p = __lo; __p != __hi; ++__p)
330*0b57cec5SDimitry Andric    {
331*0b57cec5SDimitry Andric        __h = (__h << 4) + static_cast<size_t>(*__p);
332*0b57cec5SDimitry Andric        size_t __g = __h & __mask;
333*0b57cec5SDimitry Andric        __h ^= __g | (__g >> __sr);
334*0b57cec5SDimitry Andric    }
335*0b57cec5SDimitry Andric    return static_cast<long>(__h);
336*0b57cec5SDimitry Andric}
337*0b57cec5SDimitry Andric
338*0b57cec5SDimitry Andric_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS collate<char>)
339*0b57cec5SDimitry Andric_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS collate<wchar_t>)
340*0b57cec5SDimitry Andric
341*0b57cec5SDimitry Andric// template <class CharT> class collate_byname;
342*0b57cec5SDimitry Andric
343*0b57cec5SDimitry Andrictemplate <class _CharT> class _LIBCPP_TEMPLATE_VIS collate_byname;
344*0b57cec5SDimitry Andric
345*0b57cec5SDimitry Andrictemplate <>
346*0b57cec5SDimitry Andricclass _LIBCPP_TYPE_VIS collate_byname<char>
347*0b57cec5SDimitry Andric    : public collate<char>
348*0b57cec5SDimitry Andric{
349*0b57cec5SDimitry Andric    locale_t __l;
350*0b57cec5SDimitry Andricpublic:
351*0b57cec5SDimitry Andric    typedef char char_type;
352*0b57cec5SDimitry Andric    typedef basic_string<char_type> string_type;
353*0b57cec5SDimitry Andric
354*0b57cec5SDimitry Andric    explicit collate_byname(const char* __n, size_t __refs = 0);
355*0b57cec5SDimitry Andric    explicit collate_byname(const string& __n, size_t __refs = 0);
356*0b57cec5SDimitry Andric
357*0b57cec5SDimitry Andricprotected:
358*0b57cec5SDimitry Andric    ~collate_byname();
359*0b57cec5SDimitry Andric    virtual int do_compare(const char_type* __lo1, const char_type* __hi1,
360*0b57cec5SDimitry Andric                           const char_type* __lo2, const char_type* __hi2) const;
361*0b57cec5SDimitry Andric    virtual string_type do_transform(const char_type* __lo, const char_type* __hi) const;
362*0b57cec5SDimitry Andric};
363*0b57cec5SDimitry Andric
364*0b57cec5SDimitry Andrictemplate <>
365*0b57cec5SDimitry Andricclass _LIBCPP_TYPE_VIS collate_byname<wchar_t>
366*0b57cec5SDimitry Andric    : public collate<wchar_t>
367*0b57cec5SDimitry Andric{
368*0b57cec5SDimitry Andric    locale_t __l;
369*0b57cec5SDimitry Andricpublic:
370*0b57cec5SDimitry Andric    typedef wchar_t char_type;
371*0b57cec5SDimitry Andric    typedef basic_string<char_type> string_type;
372*0b57cec5SDimitry Andric
373*0b57cec5SDimitry Andric    explicit collate_byname(const char* __n, size_t __refs = 0);
374*0b57cec5SDimitry Andric    explicit collate_byname(const string& __n, size_t __refs = 0);
375*0b57cec5SDimitry Andric
376*0b57cec5SDimitry Andricprotected:
377*0b57cec5SDimitry Andric    ~collate_byname();
378*0b57cec5SDimitry Andric
379*0b57cec5SDimitry Andric    virtual int do_compare(const char_type* __lo1, const char_type* __hi1,
380*0b57cec5SDimitry Andric                           const char_type* __lo2, const char_type* __hi2) const;
381*0b57cec5SDimitry Andric    virtual string_type do_transform(const char_type* __lo, const char_type* __hi) const;
382*0b57cec5SDimitry Andric};
383*0b57cec5SDimitry Andric
384*0b57cec5SDimitry Andrictemplate <class _CharT, class _Traits, class _Allocator>
385*0b57cec5SDimitry Andricbool
386*0b57cec5SDimitry Andriclocale::operator()(const basic_string<_CharT, _Traits, _Allocator>& __x,
387*0b57cec5SDimitry Andric                   const basic_string<_CharT, _Traits, _Allocator>& __y) const
388*0b57cec5SDimitry Andric{
389*0b57cec5SDimitry Andric    return _VSTD::use_facet<_VSTD::collate<_CharT> >(*this).compare(
390*0b57cec5SDimitry Andric                                       __x.data(), __x.data() + __x.size(),
391*0b57cec5SDimitry Andric                                       __y.data(), __y.data() + __y.size()) < 0;
392*0b57cec5SDimitry Andric}
393*0b57cec5SDimitry Andric
394*0b57cec5SDimitry Andric// template <class charT> class ctype
395*0b57cec5SDimitry Andric
396*0b57cec5SDimitry Andricclass _LIBCPP_TYPE_VIS ctype_base
397*0b57cec5SDimitry Andric{
398*0b57cec5SDimitry Andricpublic:
399*0b57cec5SDimitry Andric#if defined(__GLIBC__)
400*0b57cec5SDimitry Andric    typedef unsigned short mask;
401*0b57cec5SDimitry Andric    static const mask space  = _ISspace;
402*0b57cec5SDimitry Andric    static const mask print  = _ISprint;
403*0b57cec5SDimitry Andric    static const mask cntrl  = _IScntrl;
404*0b57cec5SDimitry Andric    static const mask upper  = _ISupper;
405*0b57cec5SDimitry Andric    static const mask lower  = _ISlower;
406*0b57cec5SDimitry Andric    static const mask alpha  = _ISalpha;
407*0b57cec5SDimitry Andric    static const mask digit  = _ISdigit;
408*0b57cec5SDimitry Andric    static const mask punct  = _ISpunct;
409*0b57cec5SDimitry Andric    static const mask xdigit = _ISxdigit;
410*0b57cec5SDimitry Andric    static const mask blank  = _ISblank;
411*0b57cec5SDimitry Andric#if defined(__mips__)
412*0b57cec5SDimitry Andric    static const mask __regex_word = static_cast<mask>(_ISbit(15));
413*0b57cec5SDimitry Andric#else
414*0b57cec5SDimitry Andric    static const mask __regex_word = 0x80;
415*0b57cec5SDimitry Andric#endif
416*0b57cec5SDimitry Andric#elif defined(_LIBCPP_MSVCRT_LIKE)
417*0b57cec5SDimitry Andric    typedef unsigned short mask;
418*0b57cec5SDimitry Andric    static const mask space  = _SPACE;
419*0b57cec5SDimitry Andric    static const mask print  = _BLANK|_PUNCT|_ALPHA|_DIGIT;
420*0b57cec5SDimitry Andric    static const mask cntrl  = _CONTROL;
421*0b57cec5SDimitry Andric    static const mask upper  = _UPPER;
422*0b57cec5SDimitry Andric    static const mask lower  = _LOWER;
423*0b57cec5SDimitry Andric    static const mask alpha  = _ALPHA;
424*0b57cec5SDimitry Andric    static const mask digit  = _DIGIT;
425*0b57cec5SDimitry Andric    static const mask punct  = _PUNCT;
426*0b57cec5SDimitry Andric    static const mask xdigit = _HEX;
427*0b57cec5SDimitry Andric    static const mask blank  = _BLANK;
428*0b57cec5SDimitry Andric    static const mask __regex_word = 0x80;
429*0b57cec5SDimitry Andric# define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_PRINT
430*0b57cec5SDimitry Andric#elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__EMSCRIPTEN__) || defined(__NetBSD__)
431*0b57cec5SDimitry Andric# ifdef __APPLE__
432*0b57cec5SDimitry Andric    typedef __uint32_t mask;
433*0b57cec5SDimitry Andric# elif defined(__FreeBSD__)
434*0b57cec5SDimitry Andric    typedef unsigned long mask;
435*0b57cec5SDimitry Andric# elif defined(__EMSCRIPTEN__) || defined(__NetBSD__)
436*0b57cec5SDimitry Andric    typedef unsigned short mask;
437*0b57cec5SDimitry Andric# endif
438*0b57cec5SDimitry Andric    static const mask space  = _CTYPE_S;
439*0b57cec5SDimitry Andric    static const mask print  = _CTYPE_R;
440*0b57cec5SDimitry Andric    static const mask cntrl  = _CTYPE_C;
441*0b57cec5SDimitry Andric    static const mask upper  = _CTYPE_U;
442*0b57cec5SDimitry Andric    static const mask lower  = _CTYPE_L;
443*0b57cec5SDimitry Andric    static const mask alpha  = _CTYPE_A;
444*0b57cec5SDimitry Andric    static const mask digit  = _CTYPE_D;
445*0b57cec5SDimitry Andric    static const mask punct  = _CTYPE_P;
446*0b57cec5SDimitry Andric    static const mask xdigit = _CTYPE_X;
447*0b57cec5SDimitry Andric
448*0b57cec5SDimitry Andric# if defined(__NetBSD__)
449*0b57cec5SDimitry Andric    static const mask blank  = _CTYPE_BL;
450*0b57cec5SDimitry Andric    // NetBSD defines classes up to 0x2000
451*0b57cec5SDimitry Andric    // see sys/ctype_bits.h, _CTYPE_Q
452*0b57cec5SDimitry Andric    static const mask __regex_word = 0x8000;
453*0b57cec5SDimitry Andric# else
454*0b57cec5SDimitry Andric    static const mask blank  = _CTYPE_B;
455*0b57cec5SDimitry Andric    static const mask __regex_word = 0x80;
456*0b57cec5SDimitry Andric# endif
457*0b57cec5SDimitry Andric#elif defined(__sun__) || defined(_AIX)
458*0b57cec5SDimitry Andric    typedef unsigned int mask;
459*0b57cec5SDimitry Andric    static const mask space  = _ISSPACE;
460*0b57cec5SDimitry Andric    static const mask print  = _ISPRINT;
461*0b57cec5SDimitry Andric    static const mask cntrl  = _ISCNTRL;
462*0b57cec5SDimitry Andric    static const mask upper  = _ISUPPER;
463*0b57cec5SDimitry Andric    static const mask lower  = _ISLOWER;
464*0b57cec5SDimitry Andric    static const mask alpha  = _ISALPHA;
465*0b57cec5SDimitry Andric    static const mask digit  = _ISDIGIT;
466*0b57cec5SDimitry Andric    static const mask punct  = _ISPUNCT;
467*0b57cec5SDimitry Andric    static const mask xdigit = _ISXDIGIT;
468*0b57cec5SDimitry Andric    static const mask blank  = _ISBLANK;
469*0b57cec5SDimitry Andric    static const mask __regex_word = 0x80;
470*0b57cec5SDimitry Andric#elif defined(_NEWLIB_VERSION)
471*0b57cec5SDimitry Andric    // Same type as Newlib's _ctype_ array in newlib/libc/include/ctype.h.
472*0b57cec5SDimitry Andric    typedef char mask;
473*0b57cec5SDimitry Andric    static const mask space  = _S;
474*0b57cec5SDimitry Andric    static const mask print  = _P | _U | _L | _N | _B;
475*0b57cec5SDimitry Andric    static const mask cntrl  = _C;
476*0b57cec5SDimitry Andric    static const mask upper  = _U;
477*0b57cec5SDimitry Andric    static const mask lower  = _L;
478*0b57cec5SDimitry Andric    static const mask alpha  = _U | _L;
479*0b57cec5SDimitry Andric    static const mask digit  = _N;
480*0b57cec5SDimitry Andric    static const mask punct  = _P;
481*0b57cec5SDimitry Andric    static const mask xdigit = _X | _N;
482*0b57cec5SDimitry Andric    static const mask blank  = _B;
483*0b57cec5SDimitry Andric    static const mask __regex_word = 0x80;
484*0b57cec5SDimitry Andric# define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_PRINT
485*0b57cec5SDimitry Andric# define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_ALPHA
486*0b57cec5SDimitry Andric# define _LIBCPP_CTYPE_MASK_IS_COMPOSITE_XDIGIT
487*0b57cec5SDimitry Andric#else
488*0b57cec5SDimitry Andric    typedef unsigned long mask;
489*0b57cec5SDimitry Andric    static const mask space  = 1<<0;
490*0b57cec5SDimitry Andric    static const mask print  = 1<<1;
491*0b57cec5SDimitry Andric    static const mask cntrl  = 1<<2;
492*0b57cec5SDimitry Andric    static const mask upper  = 1<<3;
493*0b57cec5SDimitry Andric    static const mask lower  = 1<<4;
494*0b57cec5SDimitry Andric    static const mask alpha  = 1<<5;
495*0b57cec5SDimitry Andric    static const mask digit  = 1<<6;
496*0b57cec5SDimitry Andric    static const mask punct  = 1<<7;
497*0b57cec5SDimitry Andric    static const mask xdigit = 1<<8;
498*0b57cec5SDimitry Andric    static const mask blank  = 1<<9;
499*0b57cec5SDimitry Andric    static const mask __regex_word = 1<<10;
500*0b57cec5SDimitry Andric#endif
501*0b57cec5SDimitry Andric    static const mask alnum  = alpha | digit;
502*0b57cec5SDimitry Andric    static const mask graph  = alnum | punct;
503*0b57cec5SDimitry Andric
504*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY ctype_base() {}
505*0b57cec5SDimitry Andric};
506*0b57cec5SDimitry Andric
507*0b57cec5SDimitry Andrictemplate <class _CharT> class _LIBCPP_TEMPLATE_VIS ctype;
508*0b57cec5SDimitry Andric
509*0b57cec5SDimitry Andrictemplate <>
510*0b57cec5SDimitry Andricclass _LIBCPP_TYPE_VIS ctype<wchar_t>
511*0b57cec5SDimitry Andric    : public locale::facet,
512*0b57cec5SDimitry Andric      public ctype_base
513*0b57cec5SDimitry Andric{
514*0b57cec5SDimitry Andricpublic:
515*0b57cec5SDimitry Andric    typedef wchar_t char_type;
516*0b57cec5SDimitry Andric
517*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
518*0b57cec5SDimitry Andric    explicit ctype(size_t __refs = 0)
519*0b57cec5SDimitry Andric        : locale::facet(__refs) {}
520*0b57cec5SDimitry Andric
521*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
522*0b57cec5SDimitry Andric    bool is(mask __m, char_type __c) const
523*0b57cec5SDimitry Andric    {
524*0b57cec5SDimitry Andric        return do_is(__m, __c);
525*0b57cec5SDimitry Andric    }
526*0b57cec5SDimitry Andric
527*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
528*0b57cec5SDimitry Andric    const char_type* is(const char_type* __low, const char_type* __high, mask* __vec) const
529*0b57cec5SDimitry Andric    {
530*0b57cec5SDimitry Andric        return do_is(__low, __high, __vec);
531*0b57cec5SDimitry Andric    }
532*0b57cec5SDimitry Andric
533*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
534*0b57cec5SDimitry Andric    const char_type* scan_is(mask __m, const char_type* __low, const char_type* __high) const
535*0b57cec5SDimitry Andric    {
536*0b57cec5SDimitry Andric        return do_scan_is(__m, __low, __high);
537*0b57cec5SDimitry Andric    }
538*0b57cec5SDimitry Andric
539*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
540*0b57cec5SDimitry Andric    const char_type* scan_not(mask __m, const char_type* __low, const char_type* __high) const
541*0b57cec5SDimitry Andric    {
542*0b57cec5SDimitry Andric        return do_scan_not(__m, __low, __high);
543*0b57cec5SDimitry Andric    }
544*0b57cec5SDimitry Andric
545*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
546*0b57cec5SDimitry Andric    char_type toupper(char_type __c) const
547*0b57cec5SDimitry Andric    {
548*0b57cec5SDimitry Andric        return do_toupper(__c);
549*0b57cec5SDimitry Andric    }
550*0b57cec5SDimitry Andric
551*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
552*0b57cec5SDimitry Andric    const char_type* toupper(char_type* __low, const char_type* __high) const
553*0b57cec5SDimitry Andric    {
554*0b57cec5SDimitry Andric        return do_toupper(__low, __high);
555*0b57cec5SDimitry Andric    }
556*0b57cec5SDimitry Andric
557*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
558*0b57cec5SDimitry Andric    char_type tolower(char_type __c) const
559*0b57cec5SDimitry Andric    {
560*0b57cec5SDimitry Andric        return do_tolower(__c);
561*0b57cec5SDimitry Andric    }
562*0b57cec5SDimitry Andric
563*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
564*0b57cec5SDimitry Andric    const char_type* tolower(char_type* __low, const char_type* __high) const
565*0b57cec5SDimitry Andric    {
566*0b57cec5SDimitry Andric        return do_tolower(__low, __high);
567*0b57cec5SDimitry Andric    }
568*0b57cec5SDimitry Andric
569*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
570*0b57cec5SDimitry Andric    char_type widen(char __c) const
571*0b57cec5SDimitry Andric    {
572*0b57cec5SDimitry Andric        return do_widen(__c);
573*0b57cec5SDimitry Andric    }
574*0b57cec5SDimitry Andric
575*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
576*0b57cec5SDimitry Andric    const char* widen(const char* __low, const char* __high, char_type* __to) const
577*0b57cec5SDimitry Andric    {
578*0b57cec5SDimitry Andric        return do_widen(__low, __high, __to);
579*0b57cec5SDimitry Andric    }
580*0b57cec5SDimitry Andric
581*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
582*0b57cec5SDimitry Andric    char narrow(char_type __c, char __dfault) const
583*0b57cec5SDimitry Andric    {
584*0b57cec5SDimitry Andric        return do_narrow(__c, __dfault);
585*0b57cec5SDimitry Andric    }
586*0b57cec5SDimitry Andric
587*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
588*0b57cec5SDimitry Andric    const char_type* narrow(const char_type* __low, const char_type* __high, char __dfault, char* __to) const
589*0b57cec5SDimitry Andric    {
590*0b57cec5SDimitry Andric        return do_narrow(__low, __high, __dfault, __to);
591*0b57cec5SDimitry Andric    }
592*0b57cec5SDimitry Andric
593*0b57cec5SDimitry Andric    static locale::id id;
594*0b57cec5SDimitry Andric
595*0b57cec5SDimitry Andricprotected:
596*0b57cec5SDimitry Andric    ~ctype();
597*0b57cec5SDimitry Andric    virtual bool do_is(mask __m, char_type __c) const;
598*0b57cec5SDimitry Andric    virtual const char_type* do_is(const char_type* __low, const char_type* __high, mask* __vec) const;
599*0b57cec5SDimitry Andric    virtual const char_type* do_scan_is(mask __m, const char_type* __low, const char_type* __high) const;
600*0b57cec5SDimitry Andric    virtual const char_type* do_scan_not(mask __m, const char_type* __low, const char_type* __high) const;
601*0b57cec5SDimitry Andric    virtual char_type do_toupper(char_type) const;
602*0b57cec5SDimitry Andric    virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
603*0b57cec5SDimitry Andric    virtual char_type do_tolower(char_type) const;
604*0b57cec5SDimitry Andric    virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
605*0b57cec5SDimitry Andric    virtual char_type do_widen(char) const;
606*0b57cec5SDimitry Andric    virtual const char* do_widen(const char* __low, const char* __high, char_type* __dest) const;
607*0b57cec5SDimitry Andric    virtual char do_narrow(char_type, char __dfault) const;
608*0b57cec5SDimitry Andric    virtual const char_type* do_narrow(const char_type* __low, const char_type* __high, char __dfault, char* __dest) const;
609*0b57cec5SDimitry Andric};
610*0b57cec5SDimitry Andric
611*0b57cec5SDimitry Andrictemplate <>
612*0b57cec5SDimitry Andricclass _LIBCPP_TYPE_VIS ctype<char>
613*0b57cec5SDimitry Andric    : public locale::facet, public ctype_base
614*0b57cec5SDimitry Andric{
615*0b57cec5SDimitry Andric    const mask* __tab_;
616*0b57cec5SDimitry Andric    bool        __del_;
617*0b57cec5SDimitry Andricpublic:
618*0b57cec5SDimitry Andric    typedef char char_type;
619*0b57cec5SDimitry Andric
620*0b57cec5SDimitry Andric    explicit ctype(const mask* __tab = 0, bool __del = false, size_t __refs = 0);
621*0b57cec5SDimitry Andric
622*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
623*0b57cec5SDimitry Andric    bool is(mask __m, char_type __c) const
624*0b57cec5SDimitry Andric    {
625*0b57cec5SDimitry Andric        return isascii(__c) ? (__tab_[static_cast<int>(__c)] & __m) !=0 : false;
626*0b57cec5SDimitry Andric    }
627*0b57cec5SDimitry Andric
628*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
629*0b57cec5SDimitry Andric    const char_type* is(const char_type* __low, const char_type* __high, mask* __vec) const
630*0b57cec5SDimitry Andric    {
631*0b57cec5SDimitry Andric        for (; __low != __high; ++__low, ++__vec)
632*0b57cec5SDimitry Andric            *__vec = isascii(*__low) ? __tab_[static_cast<int>(*__low)] : 0;
633*0b57cec5SDimitry Andric        return __low;
634*0b57cec5SDimitry Andric    }
635*0b57cec5SDimitry Andric
636*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
637*0b57cec5SDimitry Andric    const char_type* scan_is (mask __m, const char_type* __low, const char_type* __high) const
638*0b57cec5SDimitry Andric    {
639*0b57cec5SDimitry Andric        for (; __low != __high; ++__low)
640*0b57cec5SDimitry Andric            if (isascii(*__low) && (__tab_[static_cast<int>(*__low)] & __m))
641*0b57cec5SDimitry Andric                break;
642*0b57cec5SDimitry Andric        return __low;
643*0b57cec5SDimitry Andric    }
644*0b57cec5SDimitry Andric
645*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
646*0b57cec5SDimitry Andric    const char_type* scan_not(mask __m, const char_type* __low, const char_type* __high) const
647*0b57cec5SDimitry Andric    {
648*0b57cec5SDimitry Andric        for (; __low != __high; ++__low)
649*0b57cec5SDimitry Andric            if (!(isascii(*__low) && (__tab_[static_cast<int>(*__low)] & __m)))
650*0b57cec5SDimitry Andric                break;
651*0b57cec5SDimitry Andric        return __low;
652*0b57cec5SDimitry Andric    }
653*0b57cec5SDimitry Andric
654*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
655*0b57cec5SDimitry Andric    char_type toupper(char_type __c) const
656*0b57cec5SDimitry Andric    {
657*0b57cec5SDimitry Andric        return do_toupper(__c);
658*0b57cec5SDimitry Andric    }
659*0b57cec5SDimitry Andric
660*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
661*0b57cec5SDimitry Andric    const char_type* toupper(char_type* __low, const char_type* __high) const
662*0b57cec5SDimitry Andric    {
663*0b57cec5SDimitry Andric        return do_toupper(__low, __high);
664*0b57cec5SDimitry Andric    }
665*0b57cec5SDimitry Andric
666*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
667*0b57cec5SDimitry Andric    char_type tolower(char_type __c) const
668*0b57cec5SDimitry Andric    {
669*0b57cec5SDimitry Andric        return do_tolower(__c);
670*0b57cec5SDimitry Andric    }
671*0b57cec5SDimitry Andric
672*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
673*0b57cec5SDimitry Andric    const char_type* tolower(char_type* __low, const char_type* __high) const
674*0b57cec5SDimitry Andric    {
675*0b57cec5SDimitry Andric        return do_tolower(__low, __high);
676*0b57cec5SDimitry Andric    }
677*0b57cec5SDimitry Andric
678*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
679*0b57cec5SDimitry Andric    char_type widen(char __c) const
680*0b57cec5SDimitry Andric    {
681*0b57cec5SDimitry Andric        return do_widen(__c);
682*0b57cec5SDimitry Andric    }
683*0b57cec5SDimitry Andric
684*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
685*0b57cec5SDimitry Andric    const char* widen(const char* __low, const char* __high, char_type* __to) const
686*0b57cec5SDimitry Andric    {
687*0b57cec5SDimitry Andric        return do_widen(__low, __high, __to);
688*0b57cec5SDimitry Andric    }
689*0b57cec5SDimitry Andric
690*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
691*0b57cec5SDimitry Andric    char narrow(char_type __c, char __dfault) const
692*0b57cec5SDimitry Andric    {
693*0b57cec5SDimitry Andric        return do_narrow(__c, __dfault);
694*0b57cec5SDimitry Andric    }
695*0b57cec5SDimitry Andric
696*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
697*0b57cec5SDimitry Andric    const char* narrow(const char_type* __low, const char_type* __high, char __dfault, char* __to) const
698*0b57cec5SDimitry Andric    {
699*0b57cec5SDimitry Andric        return do_narrow(__low, __high, __dfault, __to);
700*0b57cec5SDimitry Andric    }
701*0b57cec5SDimitry Andric
702*0b57cec5SDimitry Andric    static locale::id id;
703*0b57cec5SDimitry Andric
704*0b57cec5SDimitry Andric#ifdef _CACHED_RUNES
705*0b57cec5SDimitry Andric    static const size_t table_size = _CACHED_RUNES;
706*0b57cec5SDimitry Andric#else
707*0b57cec5SDimitry Andric    static const size_t table_size = 256;  // FIXME: Don't hardcode this.
708*0b57cec5SDimitry Andric#endif
709*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY const mask* table() const  _NOEXCEPT {return __tab_;}
710*0b57cec5SDimitry Andric    static const mask* classic_table()  _NOEXCEPT;
711*0b57cec5SDimitry Andric#if defined(__GLIBC__) || defined(__EMSCRIPTEN__)
712*0b57cec5SDimitry Andric    static const int* __classic_upper_table() _NOEXCEPT;
713*0b57cec5SDimitry Andric    static const int* __classic_lower_table() _NOEXCEPT;
714*0b57cec5SDimitry Andric#endif
715*0b57cec5SDimitry Andric#if defined(__NetBSD__)
716*0b57cec5SDimitry Andric    static const short* __classic_upper_table() _NOEXCEPT;
717*0b57cec5SDimitry Andric    static const short* __classic_lower_table() _NOEXCEPT;
718*0b57cec5SDimitry Andric#endif
719*0b57cec5SDimitry Andric
720*0b57cec5SDimitry Andricprotected:
721*0b57cec5SDimitry Andric    ~ctype();
722*0b57cec5SDimitry Andric    virtual char_type do_toupper(char_type __c) const;
723*0b57cec5SDimitry Andric    virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
724*0b57cec5SDimitry Andric    virtual char_type do_tolower(char_type __c) const;
725*0b57cec5SDimitry Andric    virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
726*0b57cec5SDimitry Andric    virtual char_type do_widen(char __c) const;
727*0b57cec5SDimitry Andric    virtual const char* do_widen(const char* __low, const char* __high, char_type* __to) const;
728*0b57cec5SDimitry Andric    virtual char do_narrow(char_type __c, char __dfault) const;
729*0b57cec5SDimitry Andric    virtual const char* do_narrow(const char_type* __low, const char_type* __high, char __dfault, char* __to) const;
730*0b57cec5SDimitry Andric};
731*0b57cec5SDimitry Andric
732*0b57cec5SDimitry Andric// template <class CharT> class ctype_byname;
733*0b57cec5SDimitry Andric
734*0b57cec5SDimitry Andrictemplate <class _CharT> class _LIBCPP_TEMPLATE_VIS ctype_byname;
735*0b57cec5SDimitry Andric
736*0b57cec5SDimitry Andrictemplate <>
737*0b57cec5SDimitry Andricclass _LIBCPP_TYPE_VIS ctype_byname<char>
738*0b57cec5SDimitry Andric    : public ctype<char>
739*0b57cec5SDimitry Andric{
740*0b57cec5SDimitry Andric    locale_t __l;
741*0b57cec5SDimitry Andric
742*0b57cec5SDimitry Andricpublic:
743*0b57cec5SDimitry Andric    explicit ctype_byname(const char*, size_t = 0);
744*0b57cec5SDimitry Andric    explicit ctype_byname(const string&, size_t = 0);
745*0b57cec5SDimitry Andric
746*0b57cec5SDimitry Andricprotected:
747*0b57cec5SDimitry Andric    ~ctype_byname();
748*0b57cec5SDimitry Andric    virtual char_type do_toupper(char_type) const;
749*0b57cec5SDimitry Andric    virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
750*0b57cec5SDimitry Andric    virtual char_type do_tolower(char_type) const;
751*0b57cec5SDimitry Andric    virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
752*0b57cec5SDimitry Andric};
753*0b57cec5SDimitry Andric
754*0b57cec5SDimitry Andrictemplate <>
755*0b57cec5SDimitry Andricclass _LIBCPP_TYPE_VIS ctype_byname<wchar_t>
756*0b57cec5SDimitry Andric    : public ctype<wchar_t>
757*0b57cec5SDimitry Andric{
758*0b57cec5SDimitry Andric    locale_t __l;
759*0b57cec5SDimitry Andric
760*0b57cec5SDimitry Andricpublic:
761*0b57cec5SDimitry Andric    explicit ctype_byname(const char*, size_t = 0);
762*0b57cec5SDimitry Andric    explicit ctype_byname(const string&, size_t = 0);
763*0b57cec5SDimitry Andric
764*0b57cec5SDimitry Andricprotected:
765*0b57cec5SDimitry Andric    ~ctype_byname();
766*0b57cec5SDimitry Andric    virtual bool do_is(mask __m, char_type __c) const;
767*0b57cec5SDimitry Andric    virtual const char_type* do_is(const char_type* __low, const char_type* __high, mask* __vec) const;
768*0b57cec5SDimitry Andric    virtual const char_type* do_scan_is(mask __m, const char_type* __low, const char_type* __high) const;
769*0b57cec5SDimitry Andric    virtual const char_type* do_scan_not(mask __m, const char_type* __low, const char_type* __high) const;
770*0b57cec5SDimitry Andric    virtual char_type do_toupper(char_type) const;
771*0b57cec5SDimitry Andric    virtual const char_type* do_toupper(char_type* __low, const char_type* __high) const;
772*0b57cec5SDimitry Andric    virtual char_type do_tolower(char_type) const;
773*0b57cec5SDimitry Andric    virtual const char_type* do_tolower(char_type* __low, const char_type* __high) const;
774*0b57cec5SDimitry Andric    virtual char_type do_widen(char) const;
775*0b57cec5SDimitry Andric    virtual const char* do_widen(const char* __low, const char* __high, char_type* __dest) const;
776*0b57cec5SDimitry Andric    virtual char do_narrow(char_type, char __dfault) const;
777*0b57cec5SDimitry Andric    virtual const char_type* do_narrow(const char_type* __low, const char_type* __high, char __dfault, char* __dest) const;
778*0b57cec5SDimitry Andric};
779*0b57cec5SDimitry Andric
780*0b57cec5SDimitry Andrictemplate <class _CharT>
781*0b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
782*0b57cec5SDimitry Andricbool
783*0b57cec5SDimitry Andricisspace(_CharT __c, const locale& __loc)
784*0b57cec5SDimitry Andric{
785*0b57cec5SDimitry Andric    return use_facet<ctype<_CharT> >(__loc).is(ctype_base::space, __c);
786*0b57cec5SDimitry Andric}
787*0b57cec5SDimitry Andric
788*0b57cec5SDimitry Andrictemplate <class _CharT>
789*0b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
790*0b57cec5SDimitry Andricbool
791*0b57cec5SDimitry Andricisprint(_CharT __c, const locale& __loc)
792*0b57cec5SDimitry Andric{
793*0b57cec5SDimitry Andric    return use_facet<ctype<_CharT> >(__loc).is(ctype_base::print, __c);
794*0b57cec5SDimitry Andric}
795*0b57cec5SDimitry Andric
796*0b57cec5SDimitry Andrictemplate <class _CharT>
797*0b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
798*0b57cec5SDimitry Andricbool
799*0b57cec5SDimitry Andriciscntrl(_CharT __c, const locale& __loc)
800*0b57cec5SDimitry Andric{
801*0b57cec5SDimitry Andric    return use_facet<ctype<_CharT> >(__loc).is(ctype_base::cntrl, __c);
802*0b57cec5SDimitry Andric}
803*0b57cec5SDimitry Andric
804*0b57cec5SDimitry Andrictemplate <class _CharT>
805*0b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
806*0b57cec5SDimitry Andricbool
807*0b57cec5SDimitry Andricisupper(_CharT __c, const locale& __loc)
808*0b57cec5SDimitry Andric{
809*0b57cec5SDimitry Andric    return use_facet<ctype<_CharT> >(__loc).is(ctype_base::upper, __c);
810*0b57cec5SDimitry Andric}
811*0b57cec5SDimitry Andric
812*0b57cec5SDimitry Andrictemplate <class _CharT>
813*0b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
814*0b57cec5SDimitry Andricbool
815*0b57cec5SDimitry Andricislower(_CharT __c, const locale& __loc)
816*0b57cec5SDimitry Andric{
817*0b57cec5SDimitry Andric    return use_facet<ctype<_CharT> >(__loc).is(ctype_base::lower, __c);
818*0b57cec5SDimitry Andric}
819*0b57cec5SDimitry Andric
820*0b57cec5SDimitry Andrictemplate <class _CharT>
821*0b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
822*0b57cec5SDimitry Andricbool
823*0b57cec5SDimitry Andricisalpha(_CharT __c, const locale& __loc)
824*0b57cec5SDimitry Andric{
825*0b57cec5SDimitry Andric    return use_facet<ctype<_CharT> >(__loc).is(ctype_base::alpha, __c);
826*0b57cec5SDimitry Andric}
827*0b57cec5SDimitry Andric
828*0b57cec5SDimitry Andrictemplate <class _CharT>
829*0b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
830*0b57cec5SDimitry Andricbool
831*0b57cec5SDimitry Andricisdigit(_CharT __c, const locale& __loc)
832*0b57cec5SDimitry Andric{
833*0b57cec5SDimitry Andric    return use_facet<ctype<_CharT> >(__loc).is(ctype_base::digit, __c);
834*0b57cec5SDimitry Andric}
835*0b57cec5SDimitry Andric
836*0b57cec5SDimitry Andrictemplate <class _CharT>
837*0b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
838*0b57cec5SDimitry Andricbool
839*0b57cec5SDimitry Andricispunct(_CharT __c, const locale& __loc)
840*0b57cec5SDimitry Andric{
841*0b57cec5SDimitry Andric    return use_facet<ctype<_CharT> >(__loc).is(ctype_base::punct, __c);
842*0b57cec5SDimitry Andric}
843*0b57cec5SDimitry Andric
844*0b57cec5SDimitry Andrictemplate <class _CharT>
845*0b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
846*0b57cec5SDimitry Andricbool
847*0b57cec5SDimitry Andricisxdigit(_CharT __c, const locale& __loc)
848*0b57cec5SDimitry Andric{
849*0b57cec5SDimitry Andric    return use_facet<ctype<_CharT> >(__loc).is(ctype_base::xdigit, __c);
850*0b57cec5SDimitry Andric}
851*0b57cec5SDimitry Andric
852*0b57cec5SDimitry Andrictemplate <class _CharT>
853*0b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
854*0b57cec5SDimitry Andricbool
855*0b57cec5SDimitry Andricisalnum(_CharT __c, const locale& __loc)
856*0b57cec5SDimitry Andric{
857*0b57cec5SDimitry Andric    return use_facet<ctype<_CharT> >(__loc).is(ctype_base::alnum, __c);
858*0b57cec5SDimitry Andric}
859*0b57cec5SDimitry Andric
860*0b57cec5SDimitry Andrictemplate <class _CharT>
861*0b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
862*0b57cec5SDimitry Andricbool
863*0b57cec5SDimitry Andricisgraph(_CharT __c, const locale& __loc)
864*0b57cec5SDimitry Andric{
865*0b57cec5SDimitry Andric    return use_facet<ctype<_CharT> >(__loc).is(ctype_base::graph, __c);
866*0b57cec5SDimitry Andric}
867*0b57cec5SDimitry Andric
868*0b57cec5SDimitry Andrictemplate <class _CharT>
869*0b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
870*0b57cec5SDimitry Andric_CharT
871*0b57cec5SDimitry Andrictoupper(_CharT __c, const locale& __loc)
872*0b57cec5SDimitry Andric{
873*0b57cec5SDimitry Andric    return use_facet<ctype<_CharT> >(__loc).toupper(__c);
874*0b57cec5SDimitry Andric}
875*0b57cec5SDimitry Andric
876*0b57cec5SDimitry Andrictemplate <class _CharT>
877*0b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
878*0b57cec5SDimitry Andric_CharT
879*0b57cec5SDimitry Andrictolower(_CharT __c, const locale& __loc)
880*0b57cec5SDimitry Andric{
881*0b57cec5SDimitry Andric    return use_facet<ctype<_CharT> >(__loc).tolower(__c);
882*0b57cec5SDimitry Andric}
883*0b57cec5SDimitry Andric
884*0b57cec5SDimitry Andric// codecvt_base
885*0b57cec5SDimitry Andric
886*0b57cec5SDimitry Andricclass _LIBCPP_TYPE_VIS codecvt_base
887*0b57cec5SDimitry Andric{
888*0b57cec5SDimitry Andricpublic:
889*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY codecvt_base() {}
890*0b57cec5SDimitry Andric    enum result {ok, partial, error, noconv};
891*0b57cec5SDimitry Andric};
892*0b57cec5SDimitry Andric
893*0b57cec5SDimitry Andric// template <class internT, class externT, class stateT> class codecvt;
894*0b57cec5SDimitry Andric
895*0b57cec5SDimitry Andrictemplate <class _InternT, class _ExternT, class _StateT> class _LIBCPP_TEMPLATE_VIS codecvt;
896*0b57cec5SDimitry Andric
897*0b57cec5SDimitry Andric// template <> class codecvt<char, char, mbstate_t>
898*0b57cec5SDimitry Andric
899*0b57cec5SDimitry Andrictemplate <>
900*0b57cec5SDimitry Andricclass _LIBCPP_TYPE_VIS codecvt<char, char, mbstate_t>
901*0b57cec5SDimitry Andric    : public locale::facet,
902*0b57cec5SDimitry Andric      public codecvt_base
903*0b57cec5SDimitry Andric{
904*0b57cec5SDimitry Andricpublic:
905*0b57cec5SDimitry Andric    typedef char      intern_type;
906*0b57cec5SDimitry Andric    typedef char      extern_type;
907*0b57cec5SDimitry Andric    typedef mbstate_t state_type;
908*0b57cec5SDimitry Andric
909*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
910*0b57cec5SDimitry Andric    explicit codecvt(size_t __refs = 0)
911*0b57cec5SDimitry Andric        : locale::facet(__refs) {}
912*0b57cec5SDimitry Andric
913*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
914*0b57cec5SDimitry Andric    result out(state_type& __st,
915*0b57cec5SDimitry Andric               const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
916*0b57cec5SDimitry Andric               extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
917*0b57cec5SDimitry Andric    {
918*0b57cec5SDimitry Andric        return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
919*0b57cec5SDimitry Andric    }
920*0b57cec5SDimitry Andric
921*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
922*0b57cec5SDimitry Andric    result unshift(state_type& __st,
923*0b57cec5SDimitry Andric                   extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
924*0b57cec5SDimitry Andric    {
925*0b57cec5SDimitry Andric        return do_unshift(__st, __to, __to_end, __to_nxt);
926*0b57cec5SDimitry Andric    }
927*0b57cec5SDimitry Andric
928*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
929*0b57cec5SDimitry Andric    result in(state_type& __st,
930*0b57cec5SDimitry Andric              const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
931*0b57cec5SDimitry Andric              intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const
932*0b57cec5SDimitry Andric    {
933*0b57cec5SDimitry Andric        return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
934*0b57cec5SDimitry Andric    }
935*0b57cec5SDimitry Andric
936*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
937*0b57cec5SDimitry Andric    int encoding() const  _NOEXCEPT
938*0b57cec5SDimitry Andric    {
939*0b57cec5SDimitry Andric        return do_encoding();
940*0b57cec5SDimitry Andric    }
941*0b57cec5SDimitry Andric
942*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
943*0b57cec5SDimitry Andric    bool always_noconv() const  _NOEXCEPT
944*0b57cec5SDimitry Andric    {
945*0b57cec5SDimitry Andric        return do_always_noconv();
946*0b57cec5SDimitry Andric    }
947*0b57cec5SDimitry Andric
948*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
949*0b57cec5SDimitry Andric    int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const
950*0b57cec5SDimitry Andric    {
951*0b57cec5SDimitry Andric        return do_length(__st, __frm, __end, __mx);
952*0b57cec5SDimitry Andric    }
953*0b57cec5SDimitry Andric
954*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
955*0b57cec5SDimitry Andric    int max_length() const  _NOEXCEPT
956*0b57cec5SDimitry Andric    {
957*0b57cec5SDimitry Andric        return do_max_length();
958*0b57cec5SDimitry Andric    }
959*0b57cec5SDimitry Andric
960*0b57cec5SDimitry Andric    static locale::id id;
961*0b57cec5SDimitry Andric
962*0b57cec5SDimitry Andricprotected:
963*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
964*0b57cec5SDimitry Andric    explicit codecvt(const char*, size_t __refs = 0)
965*0b57cec5SDimitry Andric        : locale::facet(__refs) {}
966*0b57cec5SDimitry Andric
967*0b57cec5SDimitry Andric    ~codecvt();
968*0b57cec5SDimitry Andric
969*0b57cec5SDimitry Andric    virtual result do_out(state_type& __st,
970*0b57cec5SDimitry Andric                          const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
971*0b57cec5SDimitry Andric                          extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
972*0b57cec5SDimitry Andric    virtual result do_in(state_type& __st,
973*0b57cec5SDimitry Andric                         const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
974*0b57cec5SDimitry Andric                         intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
975*0b57cec5SDimitry Andric    virtual result do_unshift(state_type& __st,
976*0b57cec5SDimitry Andric                              extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
977*0b57cec5SDimitry Andric    virtual int do_encoding() const  _NOEXCEPT;
978*0b57cec5SDimitry Andric    virtual bool do_always_noconv() const  _NOEXCEPT;
979*0b57cec5SDimitry Andric    virtual int do_length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const;
980*0b57cec5SDimitry Andric    virtual int do_max_length() const  _NOEXCEPT;
981*0b57cec5SDimitry Andric};
982*0b57cec5SDimitry Andric
983*0b57cec5SDimitry Andric// template <> class codecvt<wchar_t, char, mbstate_t>
984*0b57cec5SDimitry Andric
985*0b57cec5SDimitry Andrictemplate <>
986*0b57cec5SDimitry Andricclass _LIBCPP_TYPE_VIS codecvt<wchar_t, char, mbstate_t>
987*0b57cec5SDimitry Andric    : public locale::facet,
988*0b57cec5SDimitry Andric      public codecvt_base
989*0b57cec5SDimitry Andric{
990*0b57cec5SDimitry Andric    locale_t __l;
991*0b57cec5SDimitry Andricpublic:
992*0b57cec5SDimitry Andric    typedef wchar_t   intern_type;
993*0b57cec5SDimitry Andric    typedef char      extern_type;
994*0b57cec5SDimitry Andric    typedef mbstate_t state_type;
995*0b57cec5SDimitry Andric
996*0b57cec5SDimitry Andric    explicit codecvt(size_t __refs = 0);
997*0b57cec5SDimitry Andric
998*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
999*0b57cec5SDimitry Andric    result out(state_type& __st,
1000*0b57cec5SDimitry Andric               const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1001*0b57cec5SDimitry Andric               extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1002*0b57cec5SDimitry Andric    {
1003*0b57cec5SDimitry Andric        return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1004*0b57cec5SDimitry Andric    }
1005*0b57cec5SDimitry Andric
1006*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1007*0b57cec5SDimitry Andric    result unshift(state_type& __st,
1008*0b57cec5SDimitry Andric                   extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1009*0b57cec5SDimitry Andric    {
1010*0b57cec5SDimitry Andric        return do_unshift(__st, __to, __to_end, __to_nxt);
1011*0b57cec5SDimitry Andric    }
1012*0b57cec5SDimitry Andric
1013*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1014*0b57cec5SDimitry Andric    result in(state_type& __st,
1015*0b57cec5SDimitry Andric              const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1016*0b57cec5SDimitry Andric              intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const
1017*0b57cec5SDimitry Andric    {
1018*0b57cec5SDimitry Andric        return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1019*0b57cec5SDimitry Andric    }
1020*0b57cec5SDimitry Andric
1021*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1022*0b57cec5SDimitry Andric    int encoding() const  _NOEXCEPT
1023*0b57cec5SDimitry Andric    {
1024*0b57cec5SDimitry Andric        return do_encoding();
1025*0b57cec5SDimitry Andric    }
1026*0b57cec5SDimitry Andric
1027*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1028*0b57cec5SDimitry Andric    bool always_noconv() const  _NOEXCEPT
1029*0b57cec5SDimitry Andric    {
1030*0b57cec5SDimitry Andric        return do_always_noconv();
1031*0b57cec5SDimitry Andric    }
1032*0b57cec5SDimitry Andric
1033*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1034*0b57cec5SDimitry Andric    int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const
1035*0b57cec5SDimitry Andric    {
1036*0b57cec5SDimitry Andric        return do_length(__st, __frm, __end, __mx);
1037*0b57cec5SDimitry Andric    }
1038*0b57cec5SDimitry Andric
1039*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1040*0b57cec5SDimitry Andric    int max_length() const  _NOEXCEPT
1041*0b57cec5SDimitry Andric    {
1042*0b57cec5SDimitry Andric        return do_max_length();
1043*0b57cec5SDimitry Andric    }
1044*0b57cec5SDimitry Andric
1045*0b57cec5SDimitry Andric    static locale::id id;
1046*0b57cec5SDimitry Andric
1047*0b57cec5SDimitry Andricprotected:
1048*0b57cec5SDimitry Andric    explicit codecvt(const char*, size_t __refs = 0);
1049*0b57cec5SDimitry Andric
1050*0b57cec5SDimitry Andric    ~codecvt();
1051*0b57cec5SDimitry Andric
1052*0b57cec5SDimitry Andric    virtual result do_out(state_type& __st,
1053*0b57cec5SDimitry Andric                          const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1054*0b57cec5SDimitry Andric                          extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
1055*0b57cec5SDimitry Andric    virtual result do_in(state_type& __st,
1056*0b57cec5SDimitry Andric                         const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1057*0b57cec5SDimitry Andric                         intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
1058*0b57cec5SDimitry Andric    virtual result do_unshift(state_type& __st,
1059*0b57cec5SDimitry Andric                              extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
1060*0b57cec5SDimitry Andric    virtual int do_encoding() const  _NOEXCEPT;
1061*0b57cec5SDimitry Andric    virtual bool do_always_noconv() const  _NOEXCEPT;
1062*0b57cec5SDimitry Andric    virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end, size_t __mx) const;
1063*0b57cec5SDimitry Andric    virtual int do_max_length() const  _NOEXCEPT;
1064*0b57cec5SDimitry Andric};
1065*0b57cec5SDimitry Andric
1066*0b57cec5SDimitry Andric// template <> class codecvt<char16_t, char, mbstate_t>
1067*0b57cec5SDimitry Andric
1068*0b57cec5SDimitry Andrictemplate <>
1069*0b57cec5SDimitry Andricclass _LIBCPP_TYPE_VIS codecvt<char16_t, char, mbstate_t>
1070*0b57cec5SDimitry Andric    : public locale::facet,
1071*0b57cec5SDimitry Andric      public codecvt_base
1072*0b57cec5SDimitry Andric{
1073*0b57cec5SDimitry Andricpublic:
1074*0b57cec5SDimitry Andric    typedef char16_t  intern_type;
1075*0b57cec5SDimitry Andric    typedef char      extern_type;
1076*0b57cec5SDimitry Andric    typedef mbstate_t state_type;
1077*0b57cec5SDimitry Andric
1078*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1079*0b57cec5SDimitry Andric    explicit codecvt(size_t __refs = 0)
1080*0b57cec5SDimitry Andric        : locale::facet(__refs) {}
1081*0b57cec5SDimitry Andric
1082*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1083*0b57cec5SDimitry Andric    result out(state_type& __st,
1084*0b57cec5SDimitry Andric               const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1085*0b57cec5SDimitry Andric               extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1086*0b57cec5SDimitry Andric    {
1087*0b57cec5SDimitry Andric        return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1088*0b57cec5SDimitry Andric    }
1089*0b57cec5SDimitry Andric
1090*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1091*0b57cec5SDimitry Andric    result unshift(state_type& __st,
1092*0b57cec5SDimitry Andric                   extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1093*0b57cec5SDimitry Andric    {
1094*0b57cec5SDimitry Andric        return do_unshift(__st, __to, __to_end, __to_nxt);
1095*0b57cec5SDimitry Andric    }
1096*0b57cec5SDimitry Andric
1097*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1098*0b57cec5SDimitry Andric    result in(state_type& __st,
1099*0b57cec5SDimitry Andric              const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1100*0b57cec5SDimitry Andric              intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const
1101*0b57cec5SDimitry Andric    {
1102*0b57cec5SDimitry Andric        return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1103*0b57cec5SDimitry Andric    }
1104*0b57cec5SDimitry Andric
1105*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1106*0b57cec5SDimitry Andric    int encoding() const  _NOEXCEPT
1107*0b57cec5SDimitry Andric    {
1108*0b57cec5SDimitry Andric        return do_encoding();
1109*0b57cec5SDimitry Andric    }
1110*0b57cec5SDimitry Andric
1111*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1112*0b57cec5SDimitry Andric    bool always_noconv() const  _NOEXCEPT
1113*0b57cec5SDimitry Andric    {
1114*0b57cec5SDimitry Andric        return do_always_noconv();
1115*0b57cec5SDimitry Andric    }
1116*0b57cec5SDimitry Andric
1117*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1118*0b57cec5SDimitry Andric    int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const
1119*0b57cec5SDimitry Andric    {
1120*0b57cec5SDimitry Andric        return do_length(__st, __frm, __end, __mx);
1121*0b57cec5SDimitry Andric    }
1122*0b57cec5SDimitry Andric
1123*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1124*0b57cec5SDimitry Andric    int max_length() const  _NOEXCEPT
1125*0b57cec5SDimitry Andric    {
1126*0b57cec5SDimitry Andric        return do_max_length();
1127*0b57cec5SDimitry Andric    }
1128*0b57cec5SDimitry Andric
1129*0b57cec5SDimitry Andric    static locale::id id;
1130*0b57cec5SDimitry Andric
1131*0b57cec5SDimitry Andricprotected:
1132*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1133*0b57cec5SDimitry Andric    explicit codecvt(const char*, size_t __refs = 0)
1134*0b57cec5SDimitry Andric        : locale::facet(__refs) {}
1135*0b57cec5SDimitry Andric
1136*0b57cec5SDimitry Andric    ~codecvt();
1137*0b57cec5SDimitry Andric
1138*0b57cec5SDimitry Andric    virtual result do_out(state_type& __st,
1139*0b57cec5SDimitry Andric                          const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1140*0b57cec5SDimitry Andric                          extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
1141*0b57cec5SDimitry Andric    virtual result do_in(state_type& __st,
1142*0b57cec5SDimitry Andric                         const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1143*0b57cec5SDimitry Andric                         intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
1144*0b57cec5SDimitry Andric    virtual result do_unshift(state_type& __st,
1145*0b57cec5SDimitry Andric                              extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
1146*0b57cec5SDimitry Andric    virtual int do_encoding() const  _NOEXCEPT;
1147*0b57cec5SDimitry Andric    virtual bool do_always_noconv() const  _NOEXCEPT;
1148*0b57cec5SDimitry Andric    virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end, size_t __mx) const;
1149*0b57cec5SDimitry Andric    virtual int do_max_length() const  _NOEXCEPT;
1150*0b57cec5SDimitry Andric};
1151*0b57cec5SDimitry Andric
1152*0b57cec5SDimitry Andric// template <> class codecvt<char32_t, char, mbstate_t>
1153*0b57cec5SDimitry Andric
1154*0b57cec5SDimitry Andrictemplate <>
1155*0b57cec5SDimitry Andricclass _LIBCPP_TYPE_VIS codecvt<char32_t, char, mbstate_t>
1156*0b57cec5SDimitry Andric    : public locale::facet,
1157*0b57cec5SDimitry Andric      public codecvt_base
1158*0b57cec5SDimitry Andric{
1159*0b57cec5SDimitry Andricpublic:
1160*0b57cec5SDimitry Andric    typedef char32_t  intern_type;
1161*0b57cec5SDimitry Andric    typedef char      extern_type;
1162*0b57cec5SDimitry Andric    typedef mbstate_t state_type;
1163*0b57cec5SDimitry Andric
1164*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1165*0b57cec5SDimitry Andric    explicit codecvt(size_t __refs = 0)
1166*0b57cec5SDimitry Andric        : locale::facet(__refs) {}
1167*0b57cec5SDimitry Andric
1168*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1169*0b57cec5SDimitry Andric    result out(state_type& __st,
1170*0b57cec5SDimitry Andric               const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1171*0b57cec5SDimitry Andric               extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1172*0b57cec5SDimitry Andric    {
1173*0b57cec5SDimitry Andric        return do_out(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1174*0b57cec5SDimitry Andric    }
1175*0b57cec5SDimitry Andric
1176*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1177*0b57cec5SDimitry Andric    result unshift(state_type& __st,
1178*0b57cec5SDimitry Andric                   extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const
1179*0b57cec5SDimitry Andric    {
1180*0b57cec5SDimitry Andric        return do_unshift(__st, __to, __to_end, __to_nxt);
1181*0b57cec5SDimitry Andric    }
1182*0b57cec5SDimitry Andric
1183*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1184*0b57cec5SDimitry Andric    result in(state_type& __st,
1185*0b57cec5SDimitry Andric              const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1186*0b57cec5SDimitry Andric              intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const
1187*0b57cec5SDimitry Andric    {
1188*0b57cec5SDimitry Andric        return do_in(__st, __frm, __frm_end, __frm_nxt, __to, __to_end, __to_nxt);
1189*0b57cec5SDimitry Andric    }
1190*0b57cec5SDimitry Andric
1191*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1192*0b57cec5SDimitry Andric    int encoding() const  _NOEXCEPT
1193*0b57cec5SDimitry Andric    {
1194*0b57cec5SDimitry Andric        return do_encoding();
1195*0b57cec5SDimitry Andric    }
1196*0b57cec5SDimitry Andric
1197*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1198*0b57cec5SDimitry Andric    bool always_noconv() const  _NOEXCEPT
1199*0b57cec5SDimitry Andric    {
1200*0b57cec5SDimitry Andric        return do_always_noconv();
1201*0b57cec5SDimitry Andric    }
1202*0b57cec5SDimitry Andric
1203*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1204*0b57cec5SDimitry Andric    int length(state_type& __st, const extern_type* __frm, const extern_type* __end, size_t __mx) const
1205*0b57cec5SDimitry Andric    {
1206*0b57cec5SDimitry Andric        return do_length(__st, __frm, __end, __mx);
1207*0b57cec5SDimitry Andric    }
1208*0b57cec5SDimitry Andric
1209*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1210*0b57cec5SDimitry Andric    int max_length() const  _NOEXCEPT
1211*0b57cec5SDimitry Andric    {
1212*0b57cec5SDimitry Andric        return do_max_length();
1213*0b57cec5SDimitry Andric    }
1214*0b57cec5SDimitry Andric
1215*0b57cec5SDimitry Andric    static locale::id id;
1216*0b57cec5SDimitry Andric
1217*0b57cec5SDimitry Andricprotected:
1218*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1219*0b57cec5SDimitry Andric    explicit codecvt(const char*, size_t __refs = 0)
1220*0b57cec5SDimitry Andric        : locale::facet(__refs) {}
1221*0b57cec5SDimitry Andric
1222*0b57cec5SDimitry Andric    ~codecvt();
1223*0b57cec5SDimitry Andric
1224*0b57cec5SDimitry Andric    virtual result do_out(state_type& __st,
1225*0b57cec5SDimitry Andric                          const intern_type* __frm, const intern_type* __frm_end, const intern_type*& __frm_nxt,
1226*0b57cec5SDimitry Andric                          extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
1227*0b57cec5SDimitry Andric    virtual result do_in(state_type& __st,
1228*0b57cec5SDimitry Andric                         const extern_type* __frm, const extern_type* __frm_end, const extern_type*& __frm_nxt,
1229*0b57cec5SDimitry Andric                         intern_type* __to, intern_type* __to_end, intern_type*& __to_nxt) const;
1230*0b57cec5SDimitry Andric    virtual result do_unshift(state_type& __st,
1231*0b57cec5SDimitry Andric                              extern_type* __to, extern_type* __to_end, extern_type*& __to_nxt) const;
1232*0b57cec5SDimitry Andric    virtual int do_encoding() const  _NOEXCEPT;
1233*0b57cec5SDimitry Andric    virtual bool do_always_noconv() const  _NOEXCEPT;
1234*0b57cec5SDimitry Andric    virtual int do_length(state_type&, const extern_type* __frm, const extern_type* __end, size_t __mx) const;
1235*0b57cec5SDimitry Andric    virtual int do_max_length() const  _NOEXCEPT;
1236*0b57cec5SDimitry Andric};
1237*0b57cec5SDimitry Andric
1238*0b57cec5SDimitry Andric// template <class _InternT, class _ExternT, class _StateT> class codecvt_byname
1239*0b57cec5SDimitry Andric
1240*0b57cec5SDimitry Andrictemplate <class _InternT, class _ExternT, class _StateT>
1241*0b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS codecvt_byname
1242*0b57cec5SDimitry Andric    : public codecvt<_InternT, _ExternT, _StateT>
1243*0b57cec5SDimitry Andric{
1244*0b57cec5SDimitry Andricpublic:
1245*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1246*0b57cec5SDimitry Andric    explicit codecvt_byname(const char* __nm, size_t __refs = 0)
1247*0b57cec5SDimitry Andric        : codecvt<_InternT, _ExternT, _StateT>(__nm, __refs) {}
1248*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1249*0b57cec5SDimitry Andric    explicit codecvt_byname(const string& __nm, size_t __refs = 0)
1250*0b57cec5SDimitry Andric        : codecvt<_InternT, _ExternT, _StateT>(__nm.c_str(), __refs) {}
1251*0b57cec5SDimitry Andricprotected:
1252*0b57cec5SDimitry Andric    ~codecvt_byname();
1253*0b57cec5SDimitry Andric};
1254*0b57cec5SDimitry Andric
1255*0b57cec5SDimitry Andrictemplate <class _InternT, class _ExternT, class _StateT>
1256*0b57cec5SDimitry Andriccodecvt_byname<_InternT, _ExternT, _StateT>::~codecvt_byname()
1257*0b57cec5SDimitry Andric{
1258*0b57cec5SDimitry Andric}
1259*0b57cec5SDimitry Andric
1260*0b57cec5SDimitry Andric_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS codecvt_byname<char, char, mbstate_t>)
1261*0b57cec5SDimitry Andric_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS codecvt_byname<wchar_t, char, mbstate_t>)
1262*0b57cec5SDimitry Andric_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS codecvt_byname<char16_t, char, mbstate_t>)
1263*0b57cec5SDimitry Andric_LIBCPP_EXTERN_TEMPLATE2(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS codecvt_byname<char32_t, char, mbstate_t>)
1264*0b57cec5SDimitry Andric
1265*0b57cec5SDimitry Andrictemplate <size_t _Np>
1266*0b57cec5SDimitry Andricstruct __narrow_to_utf8
1267*0b57cec5SDimitry Andric{
1268*0b57cec5SDimitry Andric    template <class _OutputIterator, class _CharT>
1269*0b57cec5SDimitry Andric    _OutputIterator
1270*0b57cec5SDimitry Andric    operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const;
1271*0b57cec5SDimitry Andric};
1272*0b57cec5SDimitry Andric
1273*0b57cec5SDimitry Andrictemplate <>
1274*0b57cec5SDimitry Andricstruct __narrow_to_utf8<8>
1275*0b57cec5SDimitry Andric{
1276*0b57cec5SDimitry Andric    template <class _OutputIterator, class _CharT>
1277*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1278*0b57cec5SDimitry Andric    _OutputIterator
1279*0b57cec5SDimitry Andric    operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const
1280*0b57cec5SDimitry Andric    {
1281*0b57cec5SDimitry Andric        for (; __wb < __we; ++__wb, ++__s)
1282*0b57cec5SDimitry Andric            *__s = *__wb;
1283*0b57cec5SDimitry Andric        return __s;
1284*0b57cec5SDimitry Andric    }
1285*0b57cec5SDimitry Andric};
1286*0b57cec5SDimitry Andric
1287*0b57cec5SDimitry Andrictemplate <>
1288*0b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS __narrow_to_utf8<16>
1289*0b57cec5SDimitry Andric    : public codecvt<char16_t, char, mbstate_t>
1290*0b57cec5SDimitry Andric{
1291*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1292*0b57cec5SDimitry Andric    __narrow_to_utf8() : codecvt<char16_t, char, mbstate_t>(1) {}
1293*0b57cec5SDimitry Andric
1294*0b57cec5SDimitry Andric    _LIBCPP_EXPORTED_FROM_ABI ~__narrow_to_utf8();
1295*0b57cec5SDimitry Andric
1296*0b57cec5SDimitry Andric    template <class _OutputIterator, class _CharT>
1297*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1298*0b57cec5SDimitry Andric    _OutputIterator
1299*0b57cec5SDimitry Andric    operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const
1300*0b57cec5SDimitry Andric    {
1301*0b57cec5SDimitry Andric        result __r = ok;
1302*0b57cec5SDimitry Andric        mbstate_t __mb;
1303*0b57cec5SDimitry Andric        while (__wb < __we && __r != error)
1304*0b57cec5SDimitry Andric        {
1305*0b57cec5SDimitry Andric            const int __sz = 32;
1306*0b57cec5SDimitry Andric            char __buf[__sz];
1307*0b57cec5SDimitry Andric            char* __bn;
1308*0b57cec5SDimitry Andric            const char16_t* __wn = (const char16_t*)__wb;
1309*0b57cec5SDimitry Andric            __r = do_out(__mb, (const char16_t*)__wb, (const char16_t*)__we, __wn,
1310*0b57cec5SDimitry Andric                         __buf, __buf+__sz, __bn);
1311*0b57cec5SDimitry Andric            if (__r == codecvt_base::error || __wn == (const char16_t*)__wb)
1312*0b57cec5SDimitry Andric                __throw_runtime_error("locale not supported");
1313*0b57cec5SDimitry Andric            for (const char* __p = __buf; __p < __bn; ++__p, ++__s)
1314*0b57cec5SDimitry Andric                *__s = *__p;
1315*0b57cec5SDimitry Andric            __wb = (const _CharT*)__wn;
1316*0b57cec5SDimitry Andric        }
1317*0b57cec5SDimitry Andric        return __s;
1318*0b57cec5SDimitry Andric    }
1319*0b57cec5SDimitry Andric};
1320*0b57cec5SDimitry Andric
1321*0b57cec5SDimitry Andrictemplate <>
1322*0b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS __narrow_to_utf8<32>
1323*0b57cec5SDimitry Andric    : public codecvt<char32_t, char, mbstate_t>
1324*0b57cec5SDimitry Andric{
1325*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1326*0b57cec5SDimitry Andric    __narrow_to_utf8() : codecvt<char32_t, char, mbstate_t>(1) {}
1327*0b57cec5SDimitry Andric
1328*0b57cec5SDimitry Andric    _LIBCPP_EXPORTED_FROM_ABI ~__narrow_to_utf8();
1329*0b57cec5SDimitry Andric
1330*0b57cec5SDimitry Andric    template <class _OutputIterator, class _CharT>
1331*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1332*0b57cec5SDimitry Andric    _OutputIterator
1333*0b57cec5SDimitry Andric    operator()(_OutputIterator __s, const _CharT* __wb, const _CharT* __we) const
1334*0b57cec5SDimitry Andric    {
1335*0b57cec5SDimitry Andric        result __r = ok;
1336*0b57cec5SDimitry Andric        mbstate_t __mb;
1337*0b57cec5SDimitry Andric        while (__wb < __we && __r != error)
1338*0b57cec5SDimitry Andric        {
1339*0b57cec5SDimitry Andric            const int __sz = 32;
1340*0b57cec5SDimitry Andric            char __buf[__sz];
1341*0b57cec5SDimitry Andric            char* __bn;
1342*0b57cec5SDimitry Andric            const char32_t* __wn = (const char32_t*)__wb;
1343*0b57cec5SDimitry Andric            __r = do_out(__mb, (const char32_t*)__wb, (const char32_t*)__we, __wn,
1344*0b57cec5SDimitry Andric                         __buf, __buf+__sz, __bn);
1345*0b57cec5SDimitry Andric            if (__r == codecvt_base::error || __wn == (const char32_t*)__wb)
1346*0b57cec5SDimitry Andric                __throw_runtime_error("locale not supported");
1347*0b57cec5SDimitry Andric            for (const char* __p = __buf; __p < __bn; ++__p, ++__s)
1348*0b57cec5SDimitry Andric                *__s = *__p;
1349*0b57cec5SDimitry Andric            __wb = (const _CharT*)__wn;
1350*0b57cec5SDimitry Andric        }
1351*0b57cec5SDimitry Andric        return __s;
1352*0b57cec5SDimitry Andric    }
1353*0b57cec5SDimitry Andric};
1354*0b57cec5SDimitry Andric
1355*0b57cec5SDimitry Andrictemplate <size_t _Np>
1356*0b57cec5SDimitry Andricstruct __widen_from_utf8
1357*0b57cec5SDimitry Andric{
1358*0b57cec5SDimitry Andric    template <class _OutputIterator>
1359*0b57cec5SDimitry Andric    _OutputIterator
1360*0b57cec5SDimitry Andric    operator()(_OutputIterator __s, const char* __nb, const char* __ne) const;
1361*0b57cec5SDimitry Andric};
1362*0b57cec5SDimitry Andric
1363*0b57cec5SDimitry Andrictemplate <>
1364*0b57cec5SDimitry Andricstruct __widen_from_utf8<8>
1365*0b57cec5SDimitry Andric{
1366*0b57cec5SDimitry Andric    template <class _OutputIterator>
1367*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1368*0b57cec5SDimitry Andric    _OutputIterator
1369*0b57cec5SDimitry Andric    operator()(_OutputIterator __s, const char* __nb, const char* __ne) const
1370*0b57cec5SDimitry Andric    {
1371*0b57cec5SDimitry Andric        for (; __nb < __ne; ++__nb, ++__s)
1372*0b57cec5SDimitry Andric            *__s = *__nb;
1373*0b57cec5SDimitry Andric        return __s;
1374*0b57cec5SDimitry Andric    }
1375*0b57cec5SDimitry Andric};
1376*0b57cec5SDimitry Andric
1377*0b57cec5SDimitry Andrictemplate <>
1378*0b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS __widen_from_utf8<16>
1379*0b57cec5SDimitry Andric    : public codecvt<char16_t, char, mbstate_t>
1380*0b57cec5SDimitry Andric{
1381*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1382*0b57cec5SDimitry Andric    __widen_from_utf8() : codecvt<char16_t, char, mbstate_t>(1) {}
1383*0b57cec5SDimitry Andric
1384*0b57cec5SDimitry Andric    _LIBCPP_EXPORTED_FROM_ABI ~__widen_from_utf8();
1385*0b57cec5SDimitry Andric
1386*0b57cec5SDimitry Andric    template <class _OutputIterator>
1387*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1388*0b57cec5SDimitry Andric    _OutputIterator
1389*0b57cec5SDimitry Andric    operator()(_OutputIterator __s, const char* __nb, const char* __ne) const
1390*0b57cec5SDimitry Andric    {
1391*0b57cec5SDimitry Andric        result __r = ok;
1392*0b57cec5SDimitry Andric        mbstate_t __mb;
1393*0b57cec5SDimitry Andric        while (__nb < __ne && __r != error)
1394*0b57cec5SDimitry Andric        {
1395*0b57cec5SDimitry Andric            const int __sz = 32;
1396*0b57cec5SDimitry Andric            char16_t __buf[__sz];
1397*0b57cec5SDimitry Andric            char16_t* __bn;
1398*0b57cec5SDimitry Andric            const char* __nn = __nb;
1399*0b57cec5SDimitry Andric            __r = do_in(__mb, __nb, __ne - __nb > __sz ? __nb+__sz : __ne, __nn,
1400*0b57cec5SDimitry Andric                        __buf, __buf+__sz, __bn);
1401*0b57cec5SDimitry Andric            if (__r == codecvt_base::error || __nn == __nb)
1402*0b57cec5SDimitry Andric                __throw_runtime_error("locale not supported");
1403*0b57cec5SDimitry Andric            for (const char16_t* __p = __buf; __p < __bn; ++__p, ++__s)
1404*0b57cec5SDimitry Andric                *__s = (wchar_t)*__p;
1405*0b57cec5SDimitry Andric            __nb = __nn;
1406*0b57cec5SDimitry Andric        }
1407*0b57cec5SDimitry Andric        return __s;
1408*0b57cec5SDimitry Andric    }
1409*0b57cec5SDimitry Andric};
1410*0b57cec5SDimitry Andric
1411*0b57cec5SDimitry Andrictemplate <>
1412*0b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS __widen_from_utf8<32>
1413*0b57cec5SDimitry Andric    : public codecvt<char32_t, char, mbstate_t>
1414*0b57cec5SDimitry Andric{
1415*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1416*0b57cec5SDimitry Andric    __widen_from_utf8() : codecvt<char32_t, char, mbstate_t>(1) {}
1417*0b57cec5SDimitry Andric
1418*0b57cec5SDimitry Andric    _LIBCPP_EXPORTED_FROM_ABI ~__widen_from_utf8();
1419*0b57cec5SDimitry Andric
1420*0b57cec5SDimitry Andric    template <class _OutputIterator>
1421*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1422*0b57cec5SDimitry Andric    _OutputIterator
1423*0b57cec5SDimitry Andric    operator()(_OutputIterator __s, const char* __nb, const char* __ne) const
1424*0b57cec5SDimitry Andric    {
1425*0b57cec5SDimitry Andric        result __r = ok;
1426*0b57cec5SDimitry Andric        mbstate_t __mb;
1427*0b57cec5SDimitry Andric        while (__nb < __ne && __r != error)
1428*0b57cec5SDimitry Andric        {
1429*0b57cec5SDimitry Andric            const int __sz = 32;
1430*0b57cec5SDimitry Andric            char32_t __buf[__sz];
1431*0b57cec5SDimitry Andric            char32_t* __bn;
1432*0b57cec5SDimitry Andric            const char* __nn = __nb;
1433*0b57cec5SDimitry Andric            __r = do_in(__mb, __nb, __ne - __nb > __sz ? __nb+__sz : __ne, __nn,
1434*0b57cec5SDimitry Andric                        __buf, __buf+__sz, __bn);
1435*0b57cec5SDimitry Andric            if (__r == codecvt_base::error || __nn == __nb)
1436*0b57cec5SDimitry Andric                __throw_runtime_error("locale not supported");
1437*0b57cec5SDimitry Andric            for (const char32_t* __p = __buf; __p < __bn; ++__p, ++__s)
1438*0b57cec5SDimitry Andric                *__s = (wchar_t)*__p;
1439*0b57cec5SDimitry Andric            __nb = __nn;
1440*0b57cec5SDimitry Andric        }
1441*0b57cec5SDimitry Andric        return __s;
1442*0b57cec5SDimitry Andric    }
1443*0b57cec5SDimitry Andric};
1444*0b57cec5SDimitry Andric
1445*0b57cec5SDimitry Andric// template <class charT> class numpunct
1446*0b57cec5SDimitry Andric
1447*0b57cec5SDimitry Andrictemplate <class _CharT> class _LIBCPP_TEMPLATE_VIS numpunct;
1448*0b57cec5SDimitry Andric
1449*0b57cec5SDimitry Andrictemplate <>
1450*0b57cec5SDimitry Andricclass _LIBCPP_TYPE_VIS numpunct<char>
1451*0b57cec5SDimitry Andric    : public locale::facet
1452*0b57cec5SDimitry Andric{
1453*0b57cec5SDimitry Andricpublic:
1454*0b57cec5SDimitry Andric    typedef char char_type;
1455*0b57cec5SDimitry Andric    typedef basic_string<char_type> string_type;
1456*0b57cec5SDimitry Andric
1457*0b57cec5SDimitry Andric    explicit numpunct(size_t __refs = 0);
1458*0b57cec5SDimitry Andric
1459*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY char_type decimal_point() const {return do_decimal_point();}
1460*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY char_type thousands_sep() const {return do_thousands_sep();}
1461*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY string grouping() const         {return do_grouping();}
1462*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY string_type truename() const    {return do_truename();}
1463*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY string_type falsename() const   {return do_falsename();}
1464*0b57cec5SDimitry Andric
1465*0b57cec5SDimitry Andric    static locale::id id;
1466*0b57cec5SDimitry Andric
1467*0b57cec5SDimitry Andricprotected:
1468*0b57cec5SDimitry Andric    ~numpunct();
1469*0b57cec5SDimitry Andric    virtual char_type do_decimal_point() const;
1470*0b57cec5SDimitry Andric    virtual char_type do_thousands_sep() const;
1471*0b57cec5SDimitry Andric    virtual string do_grouping() const;
1472*0b57cec5SDimitry Andric    virtual string_type do_truename() const;
1473*0b57cec5SDimitry Andric    virtual string_type do_falsename() const;
1474*0b57cec5SDimitry Andric
1475*0b57cec5SDimitry Andric    char_type __decimal_point_;
1476*0b57cec5SDimitry Andric    char_type __thousands_sep_;
1477*0b57cec5SDimitry Andric    string __grouping_;
1478*0b57cec5SDimitry Andric};
1479*0b57cec5SDimitry Andric
1480*0b57cec5SDimitry Andrictemplate <>
1481*0b57cec5SDimitry Andricclass _LIBCPP_TYPE_VIS numpunct<wchar_t>
1482*0b57cec5SDimitry Andric    : public locale::facet
1483*0b57cec5SDimitry Andric{
1484*0b57cec5SDimitry Andricpublic:
1485*0b57cec5SDimitry Andric    typedef wchar_t char_type;
1486*0b57cec5SDimitry Andric    typedef basic_string<char_type> string_type;
1487*0b57cec5SDimitry Andric
1488*0b57cec5SDimitry Andric    explicit numpunct(size_t __refs = 0);
1489*0b57cec5SDimitry Andric
1490*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY char_type decimal_point() const {return do_decimal_point();}
1491*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY char_type thousands_sep() const {return do_thousands_sep();}
1492*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY string grouping() const         {return do_grouping();}
1493*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY string_type truename() const    {return do_truename();}
1494*0b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY string_type falsename() const   {return do_falsename();}
1495*0b57cec5SDimitry Andric
1496*0b57cec5SDimitry Andric    static locale::id id;
1497*0b57cec5SDimitry Andric
1498*0b57cec5SDimitry Andricprotected:
1499*0b57cec5SDimitry Andric    ~numpunct();
1500*0b57cec5SDimitry Andric    virtual char_type do_decimal_point() const;
1501*0b57cec5SDimitry Andric    virtual char_type do_thousands_sep() const;
1502*0b57cec5SDimitry Andric    virtual string do_grouping() const;
1503*0b57cec5SDimitry Andric    virtual string_type do_truename() const;
1504*0b57cec5SDimitry Andric    virtual string_type do_falsename() const;
1505*0b57cec5SDimitry Andric
1506*0b57cec5SDimitry Andric    char_type __decimal_point_;
1507*0b57cec5SDimitry Andric    char_type __thousands_sep_;
1508*0b57cec5SDimitry Andric    string __grouping_;
1509*0b57cec5SDimitry Andric};
1510*0b57cec5SDimitry Andric
1511*0b57cec5SDimitry Andric// template <class charT> class numpunct_byname
1512*0b57cec5SDimitry Andric
1513*0b57cec5SDimitry Andrictemplate <class _CharT> class _LIBCPP_TEMPLATE_VIS numpunct_byname;
1514*0b57cec5SDimitry Andric
1515*0b57cec5SDimitry Andrictemplate <>
1516*0b57cec5SDimitry Andricclass _LIBCPP_TYPE_VIS numpunct_byname<char>
1517*0b57cec5SDimitry Andric: public numpunct<char>
1518*0b57cec5SDimitry Andric{
1519*0b57cec5SDimitry Andricpublic:
1520*0b57cec5SDimitry Andric    typedef char char_type;
1521*0b57cec5SDimitry Andric    typedef basic_string<char_type> string_type;
1522*0b57cec5SDimitry Andric
1523*0b57cec5SDimitry Andric    explicit numpunct_byname(const char* __nm, size_t __refs = 0);
1524*0b57cec5SDimitry Andric    explicit numpunct_byname(const string& __nm, size_t __refs = 0);
1525*0b57cec5SDimitry Andric
1526*0b57cec5SDimitry Andricprotected:
1527*0b57cec5SDimitry Andric    ~numpunct_byname();
1528*0b57cec5SDimitry Andric
1529*0b57cec5SDimitry Andricprivate:
1530*0b57cec5SDimitry Andric    void __init(const char*);
1531*0b57cec5SDimitry Andric};
1532*0b57cec5SDimitry Andric
1533*0b57cec5SDimitry Andrictemplate <>
1534*0b57cec5SDimitry Andricclass _LIBCPP_TYPE_VIS numpunct_byname<wchar_t>
1535*0b57cec5SDimitry Andric: public numpunct<wchar_t>
1536*0b57cec5SDimitry Andric{
1537*0b57cec5SDimitry Andricpublic:
1538*0b57cec5SDimitry Andric    typedef wchar_t char_type;
1539*0b57cec5SDimitry Andric    typedef basic_string<char_type> string_type;
1540*0b57cec5SDimitry Andric
1541*0b57cec5SDimitry Andric    explicit numpunct_byname(const char* __nm, size_t __refs = 0);
1542*0b57cec5SDimitry Andric    explicit numpunct_byname(const string& __nm, size_t __refs = 0);
1543*0b57cec5SDimitry Andric
1544*0b57cec5SDimitry Andricprotected:
1545*0b57cec5SDimitry Andric    ~numpunct_byname();
1546*0b57cec5SDimitry Andric
1547*0b57cec5SDimitry Andricprivate:
1548*0b57cec5SDimitry Andric    void __init(const char*);
1549*0b57cec5SDimitry Andric};
1550*0b57cec5SDimitry Andric
1551*0b57cec5SDimitry Andric_LIBCPP_END_NAMESPACE_STD
1552*0b57cec5SDimitry Andric
1553*0b57cec5SDimitry Andric#endif  // _LIBCPP___LOCALE
1554