xref: /freebsd/contrib/llvm-project/libcxx/include/locale (revision a03411e84728e9b267056fd31c7d1d9d1dc1b01e)
1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_LOCALE
11#define _LIBCPP_LOCALE
12
13/*
14    locale synopsis
15
16namespace std
17{
18
19class locale
20{
21public:
22    // types:
23    class facet;
24    class id;
25
26    typedef int category;
27    static const category // values assigned here are for exposition only
28        none     = 0x000,
29        collate  = 0x010,
30        ctype    = 0x020,
31        monetary = 0x040,
32        numeric  = 0x080,
33        time     = 0x100,
34        messages = 0x200,
35        all = collate | ctype | monetary | numeric | time | messages;
36
37    // construct/copy/destroy:
38    locale() noexcept;
39    locale(const locale& other) noexcept;
40    explicit locale(const char* std_name);
41    explicit locale(const string& std_name);
42    locale(const locale& other, const char* std_name, category);
43    locale(const locale& other, const string& std_name, category);
44    template <class Facet> locale(const locale& other, Facet* f);
45    locale(const locale& other, const locale& one, category);
46
47    ~locale(); // not virtual
48
49    const locale& operator=(const locale& other) noexcept;
50
51    template <class Facet> locale combine(const locale& other) const;
52
53    // locale operations:
54    basic_string<char> name() const;
55    bool operator==(const locale& other) const;
56    bool operator!=(const locale& other) const;                              // removed C++20
57    template <class charT, class Traits, class Allocator>
58      bool operator()(const basic_string<charT,Traits,Allocator>& s1,
59                      const basic_string<charT,Traits,Allocator>& s2) const;
60
61    // global locale objects:
62    static locale global(const locale&);
63    static const locale& classic();
64};
65
66template <class Facet> const Facet& use_facet(const locale&);
67template <class Facet> bool has_facet(const locale&) noexcept;
68
69// 22.3.3, convenience interfaces:
70template <class charT> bool isspace (charT c, const locale& loc);
71template <class charT> bool isprint (charT c, const locale& loc);
72template <class charT> bool iscntrl (charT c, const locale& loc);
73template <class charT> bool isupper (charT c, const locale& loc);
74template <class charT> bool islower (charT c, const locale& loc);
75template <class charT> bool isalpha (charT c, const locale& loc);
76template <class charT> bool isdigit (charT c, const locale& loc);
77template <class charT> bool ispunct (charT c, const locale& loc);
78template <class charT> bool isxdigit(charT c, const locale& loc);
79template <class charT> bool isalnum (charT c, const locale& loc);
80template <class charT> bool isgraph (charT c, const locale& loc);
81template <class charT> charT toupper(charT c, const locale& loc);
82template <class charT> charT tolower(charT c, const locale& loc);
83
84template<class Codecvt, class Elem = wchar_t,
85         class Wide_alloc = allocator<Elem>,
86         class Byte_alloc = allocator<char>>
87class wstring_convert
88{
89public:
90    typedef basic_string<char, char_traits<char>, Byte_alloc> byte_string;
91    typedef basic_string<Elem, char_traits<Elem>, Wide_alloc> wide_string;
92    typedef typename Codecvt::state_type                      state_type;
93    typedef typename wide_string::traits_type::int_type       int_type;
94
95    wstring_convert(Codecvt* pcvt = new Codecvt);          // before C++14
96    explicit wstring_convert(Codecvt* pcvt = new Codecvt); // before C++20
97    wstring_convert() : wstring_convert(new Codecvt) {}    // C++20
98    explicit wstring_convert(Codecvt* pcvt);               // C++20
99
100    wstring_convert(Codecvt* pcvt, state_type state);
101    explicit wstring_convert(const byte_string& byte_err,           // explicit in C++14
102                    const wide_string& wide_err = wide_string());
103    wstring_convert(const wstring_convert&) = delete;               // C++14
104    wstring_convert & operator=(const wstring_convert &) = delete;  // C++14
105    ~wstring_convert();
106
107    wide_string from_bytes(char byte);
108    wide_string from_bytes(const char* ptr);
109    wide_string from_bytes(const byte_string& str);
110    wide_string from_bytes(const char* first, const char* last);
111
112    byte_string to_bytes(Elem wchar);
113    byte_string to_bytes(const Elem* wptr);
114    byte_string to_bytes(const wide_string& wstr);
115    byte_string to_bytes(const Elem* first, const Elem* last);
116
117    size_t converted() const; // noexcept in C++14
118    state_type state() const;
119};
120
121template <class Codecvt, class Elem = wchar_t, class Tr = char_traits<Elem>>
122class wbuffer_convert
123    : public basic_streambuf<Elem, Tr>
124{
125public:
126    typedef typename Tr::state_type state_type;
127
128    wbuffer_convert(streambuf* bytebuf = 0, Codecvt* pcvt = new Codecvt,
129                    state_type state = state_type());          // before C++14
130    explicit wbuffer_convert(streambuf* bytebuf = nullptr, Codecvt* pcvt = new Codecvt,
131                            state_type state = state_type()); // before C++20
132    wbuffer_convert() : wbuffer_convert(nullptr) {} // C++20
133    explicit wbuffer_convert(streambuf* bytebuf, Codecvt* pcvt = new Codecvt,
134                            state_type state = state_type()); // C++20
135
136    wbuffer_convert(const wbuffer_convert&) = delete;               // C++14
137    wbuffer_convert & operator=(const wbuffer_convert &) = delete;  // C++14
138    ~wbuffer_convert();                                             // C++14
139
140    streambuf* rdbuf() const;
141    streambuf* rdbuf(streambuf* bytebuf);
142
143    state_type state() const;
144};
145
146// 22.4.1 and 22.4.1.3, ctype:
147class ctype_base;
148template <class charT> class ctype;
149template <> class ctype<char>; // specialization
150template <class charT> class ctype_byname;
151template <> class ctype_byname<char>; // specialization
152
153class codecvt_base;
154template <class internT, class externT, class stateT> class codecvt;
155template <class internT, class externT, class stateT> class codecvt_byname;
156
157// 22.4.2 and 22.4.3, numeric:
158template <class charT, class InputIterator> class num_get;
159template <class charT, class OutputIterator> class num_put;
160template <class charT> class numpunct;
161template <class charT> class numpunct_byname;
162
163// 22.4.4, col lation:
164template <class charT> class collate;
165template <class charT> class collate_byname;
166
167// 22.4.5, date and time:
168class time_base;
169template <class charT, class InputIterator> class time_get;
170template <class charT, class InputIterator> class time_get_byname;
171template <class charT, class OutputIterator> class time_put;
172template <class charT, class OutputIterator> class time_put_byname;
173
174// 22.4.6, money:
175class money_base;
176template <class charT, class InputIterator> class money_get;
177template <class charT, class OutputIterator> class money_put;
178template <class charT, bool Intl> class moneypunct;
179template <class charT, bool Intl> class moneypunct_byname;
180
181// 22.4.7, message retrieval:
182class messages_base;
183template <class charT> class messages;
184template <class charT> class messages_byname;
185
186}  // std
187
188*/
189
190#include <__algorithm/copy.h>
191#include <__algorithm/equal.h>
192#include <__algorithm/find.h>
193#include <__algorithm/max.h>
194#include <__algorithm/reverse.h>
195#include <__algorithm/unwrap_iter.h>
196#include <__assert> // all public C++ headers provide the assertion handler
197#include <__config>
198#include <__iterator/access.h>
199#include <__iterator/back_insert_iterator.h>
200#include <__iterator/istreambuf_iterator.h>
201#include <__iterator/ostreambuf_iterator.h>
202#include <__locale>
203#include <__memory/unique_ptr.h>
204#include <__type_traits/make_unsigned.h>
205#include <cerrno>
206#include <cstdio>
207#include <cstdlib>
208#include <ctime>
209#include <ios>
210#include <limits>
211#include <new>
212#include <streambuf>
213#include <version>
214
215// TODO: Fix __bsd_locale_defaults.h
216// NOLINTBEGIN(libcpp-robust-against-adl)
217
218#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
219// Most unix variants have catopen.  These are the specific ones that don't.
220#  if !defined(__BIONIC__) && !defined(_NEWLIB_VERSION) && !defined(__EMSCRIPTEN__)
221#    define _LIBCPP_HAS_CATOPEN 1
222#    include <nl_types.h>
223#  endif
224#endif
225
226#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
227#  include <__locale_dir/locale_base_api/bsd_locale_defaults.h>
228#else
229#  include <__locale_dir/locale_base_api/bsd_locale_fallbacks.h>
230#endif
231
232#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
233#  pragma GCC system_header
234#endif
235
236_LIBCPP_PUSH_MACROS
237#include <__undef_macros>
238
239
240_LIBCPP_BEGIN_NAMESPACE_STD
241
242#if defined(__APPLE__) || defined(__FreeBSD__)
243#  define _LIBCPP_GET_C_LOCALE 0
244#elif defined(__NetBSD__)
245#  define _LIBCPP_GET_C_LOCALE LC_C_LOCALE
246#else
247#  define _LIBCPP_GET_C_LOCALE __cloc()
248   // Get the C locale object
249   _LIBCPP_EXPORTED_FROM_ABI  locale_t __cloc();
250#define __cloc_defined
251#endif
252
253// __scan_keyword
254// Scans [__b, __e) until a match is found in the basic_strings range
255//  [__kb, __ke) or until it can be shown that there is no match in [__kb, __ke).
256//  __b will be incremented (visibly), consuming CharT until a match is found
257//  or proved to not exist.  A keyword may be "", in which will match anything.
258//  If one keyword is a prefix of another, and the next CharT in the input
259//  might match another keyword, the algorithm will attempt to find the longest
260//  matching keyword.  If the longer matching keyword ends up not matching, then
261//  no keyword match is found.  If no keyword match is found, __ke is returned
262//  and failbit is set in __err.
263//  Else an iterator pointing to the matching keyword is found.  If more than
264//  one keyword matches, an iterator to the first matching keyword is returned.
265//  If on exit __b == __e, eofbit is set in __err.  If __case_sensitive is false,
266//  __ct is used to force to lower case before comparing characters.
267//  Examples:
268//  Keywords:  "a", "abb"
269//  If the input is "a", the first keyword matches and eofbit is set.
270//  If the input is "abc", no match is found and "ab" are consumed.
271template <class _InputIterator, class _ForwardIterator, class _Ctype>
272_LIBCPP_HIDE_FROM_ABI
273_ForwardIterator
274__scan_keyword(_InputIterator& __b, _InputIterator __e,
275               _ForwardIterator __kb, _ForwardIterator __ke,
276               const _Ctype& __ct, ios_base::iostate& __err,
277               bool __case_sensitive = true)
278{
279    typedef typename iterator_traits<_InputIterator>::value_type _CharT;
280    size_t __nkw = static_cast<size_t>(_VSTD::distance(__kb, __ke));
281    const unsigned char __doesnt_match = '\0';
282    const unsigned char __might_match = '\1';
283    const unsigned char __does_match = '\2';
284    unsigned char __statbuf[100];
285    unsigned char* __status = __statbuf;
286    unique_ptr<unsigned char, void(*)(void*)> __stat_hold(nullptr, free);
287    if (__nkw > sizeof(__statbuf))
288    {
289        __status = (unsigned char*)malloc(__nkw);
290        if (__status == nullptr)
291            __throw_bad_alloc();
292        __stat_hold.reset(__status);
293    }
294    size_t __n_might_match = __nkw;  // At this point, any keyword might match
295    size_t __n_does_match = 0;       // but none of them definitely do
296    // Initialize all statuses to __might_match, except for "" keywords are __does_match
297    unsigned char* __st = __status;
298    for (_ForwardIterator __ky = __kb; __ky != __ke; ++__ky, (void) ++__st)
299    {
300        if (!__ky->empty())
301            *__st = __might_match;
302        else
303        {
304            *__st = __does_match;
305            --__n_might_match;
306            ++__n_does_match;
307        }
308    }
309    // While there might be a match, test keywords against the next CharT
310    for (size_t __indx = 0; __b != __e && __n_might_match > 0; ++__indx)
311    {
312        // Peek at the next CharT but don't consume it
313        _CharT __c = *__b;
314        if (!__case_sensitive)
315            __c = __ct.toupper(__c);
316        bool __consume = false;
317        // For each keyword which might match, see if the __indx character is __c
318        // If a match if found, consume __c
319        // If a match is found, and that is the last character in the keyword,
320        //    then that keyword matches.
321        // If the keyword doesn't match this character, then change the keyword
322        //    to doesn't match
323        __st = __status;
324        for (_ForwardIterator __ky = __kb; __ky != __ke; ++__ky, (void) ++__st)
325        {
326            if (*__st == __might_match)
327            {
328                _CharT __kc = (*__ky)[__indx];
329                if (!__case_sensitive)
330                    __kc = __ct.toupper(__kc);
331                if (__c == __kc)
332                {
333                    __consume = true;
334                    if (__ky->size() == __indx+1)
335                    {
336                        *__st = __does_match;
337                        --__n_might_match;
338                        ++__n_does_match;
339                    }
340                }
341                else
342                {
343                    *__st = __doesnt_match;
344                    --__n_might_match;
345                }
346            }
347        }
348        // consume if we matched a character
349        if (__consume)
350        {
351            ++__b;
352            // If we consumed a character and there might be a matched keyword that
353            //   was marked matched on a previous iteration, then such keywords
354            //   which are now marked as not matching.
355            if (__n_might_match + __n_does_match > 1)
356            {
357                __st = __status;
358                for (_ForwardIterator __ky = __kb; __ky != __ke; ++__ky, (void) ++__st)
359                {
360                    if (*__st == __does_match && __ky->size() != __indx+1)
361                    {
362                        *__st = __doesnt_match;
363                        --__n_does_match;
364                    }
365                }
366            }
367        }
368    }
369    // We've exited the loop because we hit eof and/or we have no more "might matches".
370    if (__b == __e)
371        __err |= ios_base::eofbit;
372    // Return the first matching result
373    for (__st = __status; __kb != __ke; ++__kb, (void) ++__st)
374        if (*__st == __does_match)
375            break;
376    if (__kb == __ke)
377        __err |= ios_base::failbit;
378    return __kb;
379}
380
381struct _LIBCPP_EXPORTED_FROM_ABI __num_get_base
382{
383    static const int __num_get_buf_sz = 40;
384
385    static int __get_base(ios_base&);
386    static const char __src[33];
387};
388
389_LIBCPP_EXPORTED_FROM_ABI void __check_grouping(const string& __grouping, unsigned* __g, unsigned* __g_end,
390                      ios_base::iostate& __err);
391
392template <class _CharT>
393struct __num_get
394    : protected __num_get_base
395{
396    static string __stage2_float_prep(ios_base& __iob, _CharT* __atoms, _CharT& __decimal_point,
397                                      _CharT& __thousands_sep);
398
399    static int __stage2_float_loop(_CharT __ct, bool& __in_units, char& __exp,
400                                   char* __a, char*& __a_end,
401                                   _CharT __decimal_point, _CharT __thousands_sep,
402                                   const string& __grouping, unsigned* __g,
403                                   unsigned*& __g_end, unsigned& __dc, _CharT* __atoms);
404#ifndef _LIBCPP_ABI_OPTIMIZED_LOCALE_NUM_GET
405    static string __stage2_int_prep(ios_base& __iob, _CharT* __atoms, _CharT& __thousands_sep);
406    static int __stage2_int_loop(_CharT __ct, int __base, char* __a, char*& __a_end,
407                  unsigned& __dc, _CharT __thousands_sep, const string& __grouping,
408                  unsigned* __g, unsigned*& __g_end, _CharT* __atoms);
409
410#else
411    static string __stage2_int_prep(ios_base& __iob, _CharT& __thousands_sep)
412    {
413        locale __loc = __iob.getloc();
414        const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc);
415        __thousands_sep = __np.thousands_sep();
416        return __np.grouping();
417    }
418
419    const _CharT* __do_widen(ios_base& __iob, _CharT* __atoms) const
420    {
421      return __do_widen_p(__iob, __atoms);
422    }
423
424
425    static int __stage2_int_loop(_CharT __ct, int __base, char* __a, char*& __a_end,
426                  unsigned& __dc, _CharT __thousands_sep, const string& __grouping,
427                  unsigned* __g, unsigned*& __g_end, const _CharT* __atoms);
428private:
429    template<typename _Tp>
430    const _Tp* __do_widen_p(ios_base& __iob, _Tp* __atoms) const
431    {
432      locale __loc = __iob.getloc();
433      use_facet<ctype<_Tp> >(__loc).widen(__src, __src + 26, __atoms);
434      return __atoms;
435    }
436
437    const char* __do_widen_p(ios_base& __iob, char* __atoms) const
438    {
439      (void)__iob;
440      (void)__atoms;
441      return __src;
442    }
443#endif
444};
445
446#ifndef _LIBCPP_ABI_OPTIMIZED_LOCALE_NUM_GET
447template <class _CharT>
448string
449__num_get<_CharT>::__stage2_int_prep(ios_base& __iob, _CharT* __atoms, _CharT& __thousands_sep)
450{
451    locale __loc = __iob.getloc();
452    std::use_facet<ctype<_CharT> >(__loc).widen(__src, __src + 26, __atoms);
453    const numpunct<_CharT>& __np = std::use_facet<numpunct<_CharT> >(__loc);
454    __thousands_sep = __np.thousands_sep();
455    return __np.grouping();
456}
457#endif
458
459template <class _CharT>
460string
461__num_get<_CharT>::__stage2_float_prep(ios_base& __iob, _CharT* __atoms, _CharT& __decimal_point,
462                    _CharT& __thousands_sep)
463{
464    locale __loc = __iob.getloc();
465    std::use_facet<ctype<_CharT> >(__loc).widen(__src, __src + 32, __atoms);
466    const numpunct<_CharT>& __np = std::use_facet<numpunct<_CharT> >(__loc);
467    __decimal_point = __np.decimal_point();
468    __thousands_sep = __np.thousands_sep();
469    return __np.grouping();
470}
471
472template <class _CharT>
473int
474#ifndef _LIBCPP_ABI_OPTIMIZED_LOCALE_NUM_GET
475__num_get<_CharT>::__stage2_int_loop(_CharT __ct, int __base, char* __a, char*& __a_end,
476                  unsigned& __dc, _CharT __thousands_sep, const string& __grouping,
477                  unsigned* __g, unsigned*& __g_end, _CharT* __atoms)
478#else
479__num_get<_CharT>::__stage2_int_loop(_CharT __ct, int __base, char* __a, char*& __a_end,
480                  unsigned& __dc, _CharT __thousands_sep, const string& __grouping,
481                  unsigned* __g, unsigned*& __g_end, const _CharT* __atoms)
482
483#endif
484{
485    if (__a_end == __a && (__ct == __atoms[24] || __ct == __atoms[25]))
486    {
487        *__a_end++ = __ct == __atoms[24] ? '+' : '-';
488        __dc = 0;
489        return 0;
490    }
491    if (__grouping.size() != 0 && __ct == __thousands_sep)
492    {
493        if (__g_end-__g < __num_get_buf_sz)
494        {
495            *__g_end++ = __dc;
496            __dc = 0;
497        }
498        return 0;
499    }
500    ptrdiff_t __f = std::find(__atoms, __atoms + 26, __ct) - __atoms;
501    if (__f >= 24)
502        return -1;
503    switch (__base)
504    {
505    case 8:
506    case 10:
507        if (__f >= __base)
508            return -1;
509        break;
510    case 16:
511        if (__f < 22)
512            break;
513        if (__a_end != __a && __a_end - __a <= 2 && __a_end[-1] == '0')
514        {
515            __dc = 0;
516            *__a_end++ = __src[__f];
517            return 0;
518        }
519        return -1;
520    }
521    *__a_end++ = __src[__f];
522    ++__dc;
523    return 0;
524}
525
526template <class _CharT>
527int
528__num_get<_CharT>::__stage2_float_loop(_CharT __ct, bool& __in_units, char& __exp, char* __a, char*& __a_end,
529                    _CharT __decimal_point, _CharT __thousands_sep, const string& __grouping,
530                    unsigned* __g, unsigned*& __g_end, unsigned& __dc, _CharT* __atoms)
531{
532    if (__ct == __decimal_point)
533    {
534        if (!__in_units)
535            return -1;
536        __in_units = false;
537        *__a_end++ = '.';
538        if (__grouping.size() != 0 && __g_end-__g < __num_get_buf_sz)
539            *__g_end++ = __dc;
540        return 0;
541    }
542    if (__ct == __thousands_sep && __grouping.size() != 0)
543    {
544        if (!__in_units)
545            return -1;
546        if (__g_end-__g < __num_get_buf_sz)
547        {
548            *__g_end++ = __dc;
549            __dc = 0;
550        }
551        return 0;
552    }
553    ptrdiff_t __f = std::find(__atoms, __atoms + 32, __ct) - __atoms;
554    if (__f >= 32)
555        return -1;
556    char __x = __src[__f];
557    if (__x == '-' || __x == '+')
558    {
559        if (__a_end == __a || (std::toupper(__a_end[-1]) == std::toupper(__exp)))
560        {
561            *__a_end++ = __x;
562            return 0;
563        }
564        return -1;
565    }
566    if (__x == 'x' || __x == 'X')
567        __exp = 'P';
568    else if (std::toupper(__x) == __exp)
569    {
570        __exp = std::tolower(__exp);
571        if (__in_units)
572        {
573            __in_units = false;
574            if (__grouping.size() != 0 && __g_end-__g < __num_get_buf_sz)
575                *__g_end++ = __dc;
576        }
577    }
578    *__a_end++ = __x;
579    if (__f >= 22)
580        return 0;
581    ++__dc;
582    return 0;
583}
584
585extern template struct _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __num_get<char>;
586#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
587extern template struct _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __num_get<wchar_t>;
588#endif
589
590template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
591class _LIBCPP_TEMPLATE_VIS num_get
592    : public locale::facet,
593      private __num_get<_CharT>
594{
595public:
596    typedef _CharT char_type;
597    typedef _InputIterator iter_type;
598
599    _LIBCPP_INLINE_VISIBILITY
600    explicit num_get(size_t __refs = 0)
601        : locale::facet(__refs) {}
602
603    _LIBCPP_INLINE_VISIBILITY
604    iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
605                  ios_base::iostate& __err, bool& __v) const
606    {
607        return do_get(__b, __e, __iob, __err, __v);
608    }
609
610    _LIBCPP_INLINE_VISIBILITY
611    iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
612                  ios_base::iostate& __err, long& __v) const
613    {
614        return do_get(__b, __e, __iob, __err, __v);
615    }
616
617    _LIBCPP_INLINE_VISIBILITY
618    iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
619                  ios_base::iostate& __err, long long& __v) const
620    {
621        return do_get(__b, __e, __iob, __err, __v);
622    }
623
624    _LIBCPP_INLINE_VISIBILITY
625    iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
626                  ios_base::iostate& __err, unsigned short& __v) const
627    {
628        return do_get(__b, __e, __iob, __err, __v);
629    }
630
631    _LIBCPP_INLINE_VISIBILITY
632    iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
633                  ios_base::iostate& __err, unsigned int& __v) const
634    {
635        return do_get(__b, __e, __iob, __err, __v);
636    }
637
638    _LIBCPP_INLINE_VISIBILITY
639    iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
640                  ios_base::iostate& __err, unsigned long& __v) const
641    {
642        return do_get(__b, __e, __iob, __err, __v);
643    }
644
645    _LIBCPP_INLINE_VISIBILITY
646    iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
647                  ios_base::iostate& __err, unsigned long long& __v) const
648    {
649        return do_get(__b, __e, __iob, __err, __v);
650    }
651
652    _LIBCPP_INLINE_VISIBILITY
653    iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
654                  ios_base::iostate& __err, float& __v) const
655    {
656        return do_get(__b, __e, __iob, __err, __v);
657    }
658
659    _LIBCPP_INLINE_VISIBILITY
660    iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
661                  ios_base::iostate& __err, double& __v) const
662    {
663        return do_get(__b, __e, __iob, __err, __v);
664    }
665
666    _LIBCPP_INLINE_VISIBILITY
667    iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
668                  ios_base::iostate& __err, long double& __v) const
669    {
670        return do_get(__b, __e, __iob, __err, __v);
671    }
672
673    _LIBCPP_INLINE_VISIBILITY
674    iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
675                  ios_base::iostate& __err, void*& __v) const
676    {
677        return do_get(__b, __e, __iob, __err, __v);
678    }
679
680    static locale::id id;
681
682protected:
683    _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~num_get() override {}
684
685    template <class _Fp>
686    _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
687    iter_type __do_get_floating_point
688                            (iter_type __b, iter_type __e, ios_base& __iob,
689                             ios_base::iostate& __err, _Fp& __v) const;
690
691    template <class _Signed>
692    _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
693    iter_type __do_get_signed
694                            (iter_type __b, iter_type __e, ios_base& __iob,
695                             ios_base::iostate& __err, _Signed& __v) const;
696
697    template <class _Unsigned>
698    _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
699    iter_type __do_get_unsigned
700                            (iter_type __b, iter_type __e, ios_base& __iob,
701                             ios_base::iostate& __err, _Unsigned& __v) const;
702
703
704    virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
705                             ios_base::iostate& __err, bool& __v) const;
706
707    virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
708                             ios_base::iostate& __err, long& __v) const
709    { return this->__do_get_signed ( __b, __e, __iob, __err, __v ); }
710
711    virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
712                             ios_base::iostate& __err, long long& __v) const
713    { return this->__do_get_signed ( __b, __e, __iob, __err, __v ); }
714
715    virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
716                             ios_base::iostate& __err, unsigned short& __v) const
717    { return this->__do_get_unsigned ( __b, __e, __iob, __err, __v ); }
718
719    virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
720                             ios_base::iostate& __err, unsigned int& __v) const
721    { return this->__do_get_unsigned ( __b, __e, __iob, __err, __v ); }
722
723    virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
724                             ios_base::iostate& __err, unsigned long& __v) const
725    { return this->__do_get_unsigned ( __b, __e, __iob, __err, __v ); }
726
727    virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
728                             ios_base::iostate& __err, unsigned long long& __v) const
729    { return this->__do_get_unsigned ( __b, __e, __iob, __err, __v ); }
730
731    virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
732                             ios_base::iostate& __err, float& __v) const
733    { return this->__do_get_floating_point ( __b, __e, __iob, __err, __v ); }
734
735    virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
736                             ios_base::iostate& __err, double& __v) const
737    { return this->__do_get_floating_point ( __b, __e, __iob, __err, __v ); }
738
739    virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
740                             ios_base::iostate& __err, long double& __v) const
741    { return this->__do_get_floating_point ( __b, __e, __iob, __err, __v ); }
742
743    virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
744                             ios_base::iostate& __err, void*& __v) const;
745};
746
747template <class _CharT, class _InputIterator>
748locale::id
749num_get<_CharT, _InputIterator>::id;
750
751template <class _Tp>
752_LIBCPP_HIDE_FROM_ABI _Tp
753__num_get_signed_integral(const char* __a, const char* __a_end,
754                          ios_base::iostate& __err, int __base)
755{
756    if (__a != __a_end)
757    {
758        __libcpp_remove_reference_t<decltype(errno)> __save_errno = errno;
759        errno = 0;
760        char *__p2;
761        long long __ll = strtoll_l(__a, &__p2, __base, _LIBCPP_GET_C_LOCALE);
762        __libcpp_remove_reference_t<decltype(errno)> __current_errno = errno;
763        if (__current_errno == 0)
764            errno = __save_errno;
765        if (__p2 != __a_end)
766        {
767            __err = ios_base::failbit;
768            return 0;
769        }
770        else if (__current_errno == ERANGE         ||
771                 __ll < numeric_limits<_Tp>::min() ||
772                 numeric_limits<_Tp>::max() < __ll)
773        {
774            __err = ios_base::failbit;
775            if (__ll > 0)
776                return numeric_limits<_Tp>::max();
777            else
778                return numeric_limits<_Tp>::min();
779        }
780        return static_cast<_Tp>(__ll);
781    }
782    __err = ios_base::failbit;
783    return 0;
784}
785
786template <class _Tp>
787_LIBCPP_HIDE_FROM_ABI _Tp
788__num_get_unsigned_integral(const char* __a, const char* __a_end,
789                            ios_base::iostate& __err, int __base)
790{
791    if (__a != __a_end)
792    {
793        const bool __negate = *__a == '-';
794        if (__negate && ++__a == __a_end) {
795          __err = ios_base::failbit;
796          return 0;
797        }
798        __libcpp_remove_reference_t<decltype(errno)> __save_errno = errno;
799        errno = 0;
800        char *__p2;
801        unsigned long long __ll = strtoull_l(__a, &__p2, __base, _LIBCPP_GET_C_LOCALE);
802        __libcpp_remove_reference_t<decltype(errno)> __current_errno = errno;
803        if (__current_errno == 0)
804            errno = __save_errno;
805        if (__p2 != __a_end)
806        {
807            __err = ios_base::failbit;
808            return 0;
809        }
810        else if (__current_errno == ERANGE || numeric_limits<_Tp>::max() < __ll)
811        {
812            __err = ios_base::failbit;
813            return numeric_limits<_Tp>::max();
814        }
815        _Tp __res = static_cast<_Tp>(__ll);
816        if (__negate) __res = -__res;
817        return __res;
818    }
819    __err = ios_base::failbit;
820    return 0;
821}
822
823template <class _Tp>
824_LIBCPP_INLINE_VISIBILITY
825_Tp __do_strtod(const char* __a, char** __p2);
826
827template <>
828inline _LIBCPP_INLINE_VISIBILITY
829float __do_strtod<float>(const char* __a, char** __p2) {
830    return strtof_l(__a, __p2, _LIBCPP_GET_C_LOCALE);
831}
832
833template <>
834inline _LIBCPP_INLINE_VISIBILITY
835double __do_strtod<double>(const char* __a, char** __p2) {
836    return strtod_l(__a, __p2, _LIBCPP_GET_C_LOCALE);
837}
838
839template <>
840inline _LIBCPP_INLINE_VISIBILITY
841long double __do_strtod<long double>(const char* __a, char** __p2) {
842    return strtold_l(__a, __p2, _LIBCPP_GET_C_LOCALE);
843}
844
845template <class _Tp>
846_LIBCPP_HIDE_FROM_ABI
847_Tp
848__num_get_float(const char* __a, const char* __a_end, ios_base::iostate& __err)
849{
850    if (__a != __a_end)
851    {
852        __libcpp_remove_reference_t<decltype(errno)> __save_errno = errno;
853        errno = 0;
854        char *__p2;
855        _Tp __ld = std::__do_strtod<_Tp>(__a, &__p2);
856        __libcpp_remove_reference_t<decltype(errno)> __current_errno = errno;
857        if (__current_errno == 0)
858            errno = __save_errno;
859        if (__p2 != __a_end)
860        {
861            __err = ios_base::failbit;
862            return 0;
863        }
864        else if (__current_errno == ERANGE)
865            __err = ios_base::failbit;
866        return __ld;
867    }
868    __err = ios_base::failbit;
869    return 0;
870}
871
872template <class _CharT, class _InputIterator>
873_InputIterator
874num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
875                                        ios_base& __iob,
876                                        ios_base::iostate& __err,
877                                        bool& __v) const
878{
879    if ((__iob.flags() & ios_base::boolalpha) == 0)
880    {
881        long __lv = -1;
882        __b = do_get(__b, __e, __iob, __err, __lv);
883        switch (__lv)
884        {
885        case 0:
886            __v = false;
887            break;
888        case 1:
889            __v = true;
890            break;
891        default:
892            __v = true;
893            __err = ios_base::failbit;
894            break;
895        }
896        return __b;
897    }
898    const ctype<_CharT>& __ct = std::use_facet<ctype<_CharT> >(__iob.getloc());
899    const numpunct<_CharT>& __np = std::use_facet<numpunct<_CharT> >(__iob.getloc());
900    typedef typename numpunct<_CharT>::string_type string_type;
901    const string_type __names[2] = {__np.truename(), __np.falsename()};
902    const string_type* __i = _VSTD::__scan_keyword(__b, __e, __names, __names+2,
903                                                   __ct, __err);
904    __v = __i == __names;
905    return __b;
906}
907
908// signed
909
910template <class _CharT, class _InputIterator>
911template <class _Signed>
912_InputIterator
913num_get<_CharT, _InputIterator>::__do_get_signed(iter_type __b, iter_type __e,
914                                        ios_base& __iob,
915                                        ios_base::iostate& __err,
916                                        _Signed& __v) const
917{
918    // Stage 1
919    int __base = this->__get_base(__iob);
920    // Stage 2
921    char_type __thousands_sep;
922    const int __atoms_size = 26;
923#ifdef _LIBCPP_ABI_OPTIMIZED_LOCALE_NUM_GET
924    char_type __atoms1[__atoms_size];
925    const char_type *__atoms = this->__do_widen(__iob, __atoms1);
926    string __grouping = this->__stage2_int_prep(__iob, __thousands_sep);
927#else
928    char_type __atoms[__atoms_size];
929    string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
930#endif
931    string __buf;
932    __buf.resize(__buf.capacity());
933    char* __a = &__buf[0];
934    char* __a_end = __a;
935    unsigned __g[__num_get_base::__num_get_buf_sz];
936    unsigned* __g_end = __g;
937    unsigned __dc = 0;
938    for (; __b != __e; ++__b)
939    {
940        if (__a_end == __a + __buf.size())
941        {
942            size_t __tmp = __buf.size();
943            __buf.resize(2*__buf.size());
944            __buf.resize(__buf.capacity());
945            __a = &__buf[0];
946            __a_end = __a + __tmp;
947        }
948        if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
949                                    __thousands_sep, __grouping, __g, __g_end,
950                                    __atoms))
951            break;
952    }
953    if (__grouping.size() != 0 && __g_end-__g < __num_get_base::__num_get_buf_sz)
954        *__g_end++ = __dc;
955    // Stage 3
956    __v = std::__num_get_signed_integral<_Signed>(__a, __a_end, __err, __base);
957    // Digit grouping checked
958    __check_grouping(__grouping, __g, __g_end, __err);
959    // EOF checked
960    if (__b == __e)
961        __err |= ios_base::eofbit;
962    return __b;
963}
964
965// unsigned
966
967template <class _CharT, class _InputIterator>
968template <class _Unsigned>
969_InputIterator
970num_get<_CharT, _InputIterator>::__do_get_unsigned(iter_type __b, iter_type __e,
971                                        ios_base& __iob,
972                                        ios_base::iostate& __err,
973                                        _Unsigned& __v) const
974{
975    // Stage 1
976    int __base = this->__get_base(__iob);
977    // Stage 2
978    char_type __thousands_sep;
979    const int __atoms_size = 26;
980#ifdef _LIBCPP_ABI_OPTIMIZED_LOCALE_NUM_GET
981    char_type __atoms1[__atoms_size];
982    const char_type *__atoms = this->__do_widen(__iob, __atoms1);
983    string __grouping = this->__stage2_int_prep(__iob, __thousands_sep);
984#else
985    char_type __atoms[__atoms_size];
986    string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
987#endif
988    string __buf;
989    __buf.resize(__buf.capacity());
990    char* __a = &__buf[0];
991    char* __a_end = __a;
992    unsigned __g[__num_get_base::__num_get_buf_sz];
993    unsigned* __g_end = __g;
994    unsigned __dc = 0;
995    for (; __b != __e; ++__b)
996    {
997        if (__a_end == __a + __buf.size())
998        {
999            size_t __tmp = __buf.size();
1000            __buf.resize(2*__buf.size());
1001            __buf.resize(__buf.capacity());
1002            __a = &__buf[0];
1003            __a_end = __a + __tmp;
1004        }
1005        if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
1006                                    __thousands_sep, __grouping, __g, __g_end,
1007                                    __atoms))
1008            break;
1009    }
1010    if (__grouping.size() != 0 && __g_end-__g < __num_get_base::__num_get_buf_sz)
1011        *__g_end++ = __dc;
1012    // Stage 3
1013    __v = std::__num_get_unsigned_integral<_Unsigned>(__a, __a_end, __err, __base);
1014    // Digit grouping checked
1015    __check_grouping(__grouping, __g, __g_end, __err);
1016    // EOF checked
1017    if (__b == __e)
1018        __err |= ios_base::eofbit;
1019    return __b;
1020}
1021
1022// floating point
1023
1024template <class _CharT, class _InputIterator>
1025template <class _Fp>
1026_InputIterator
1027num_get<_CharT, _InputIterator>::__do_get_floating_point(iter_type __b, iter_type __e,
1028                                        ios_base& __iob,
1029                                        ios_base::iostate& __err,
1030                                        _Fp& __v) const
1031{
1032    // Stage 1, nothing to do
1033    // Stage 2
1034    char_type __atoms[32];
1035    char_type __decimal_point;
1036    char_type __thousands_sep;
1037    string __grouping = this->__stage2_float_prep(__iob, __atoms,
1038                                                  __decimal_point,
1039                                                  __thousands_sep);
1040    string __buf;
1041    __buf.resize(__buf.capacity());
1042    char* __a = &__buf[0];
1043    char* __a_end = __a;
1044    unsigned __g[__num_get_base::__num_get_buf_sz];
1045    unsigned* __g_end = __g;
1046    unsigned __dc = 0;
1047    bool __in_units = true;
1048    char __exp = 'E';
1049    for (; __b != __e; ++__b)
1050    {
1051        if (__a_end == __a + __buf.size())
1052        {
1053            size_t __tmp = __buf.size();
1054            __buf.resize(2*__buf.size());
1055            __buf.resize(__buf.capacity());
1056            __a = &__buf[0];
1057            __a_end = __a + __tmp;
1058        }
1059        if (this->__stage2_float_loop(*__b, __in_units, __exp, __a, __a_end,
1060                                      __decimal_point, __thousands_sep,
1061                                      __grouping, __g, __g_end,
1062                                      __dc, __atoms))
1063            break;
1064    }
1065    if (__grouping.size() != 0 && __in_units && __g_end-__g < __num_get_base::__num_get_buf_sz)
1066        *__g_end++ = __dc;
1067    // Stage 3
1068    __v = std::__num_get_float<_Fp>(__a, __a_end, __err);
1069    // Digit grouping checked
1070    __check_grouping(__grouping, __g, __g_end, __err);
1071    // EOF checked
1072    if (__b == __e)
1073        __err |= ios_base::eofbit;
1074    return __b;
1075}
1076
1077template <class _CharT, class _InputIterator>
1078_InputIterator
1079num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1080                                        ios_base& __iob,
1081                                        ios_base::iostate& __err,
1082                                        void*& __v) const
1083{
1084    // Stage 1
1085    int __base = 16;
1086    // Stage 2
1087    char_type __atoms[26];
1088    char_type __thousands_sep = 0;
1089    string __grouping;
1090    std::use_facet<ctype<_CharT> >(__iob.getloc()).widen(__num_get_base::__src,
1091                                                         __num_get_base::__src + 26, __atoms);
1092    string __buf;
1093    __buf.resize(__buf.capacity());
1094    char* __a = &__buf[0];
1095    char* __a_end = __a;
1096    unsigned __g[__num_get_base::__num_get_buf_sz];
1097    unsigned* __g_end = __g;
1098    unsigned __dc = 0;
1099    for (; __b != __e; ++__b)
1100    {
1101        if (__a_end == __a + __buf.size())
1102        {
1103            size_t __tmp = __buf.size();
1104            __buf.resize(2*__buf.size());
1105            __buf.resize(__buf.capacity());
1106            __a = &__buf[0];
1107            __a_end = __a + __tmp;
1108        }
1109        if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
1110                                    __thousands_sep, __grouping,
1111                                    __g, __g_end, __atoms))
1112            break;
1113    }
1114    // Stage 3
1115    __buf.resize(__a_end - __a);
1116    if (__libcpp_sscanf_l(__buf.c_str(), _LIBCPP_GET_C_LOCALE, "%p", &__v) != 1)
1117        __err = ios_base::failbit;
1118    // EOF checked
1119    if (__b == __e)
1120        __err |= ios_base::eofbit;
1121    return __b;
1122}
1123
1124extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS num_get<char>;
1125#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1126extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS num_get<wchar_t>;
1127#endif
1128
1129struct _LIBCPP_EXPORTED_FROM_ABI __num_put_base
1130{
1131protected:
1132    static void __format_int(char* __fmt, const char* __len, bool __signd,
1133                             ios_base::fmtflags __flags);
1134    static bool __format_float(char* __fmt, const char* __len,
1135                               ios_base::fmtflags __flags);
1136    static char* __identify_padding(char* __nb, char* __ne,
1137                                    const ios_base& __iob);
1138};
1139
1140template <class _CharT>
1141struct __num_put
1142    : protected __num_put_base
1143{
1144    static void __widen_and_group_int(char* __nb, char* __np, char* __ne,
1145                                      _CharT* __ob, _CharT*& __op, _CharT*& __oe,
1146                                      const locale& __loc);
1147    static void __widen_and_group_float(char* __nb, char* __np, char* __ne,
1148                                        _CharT* __ob, _CharT*& __op, _CharT*& __oe,
1149                                        const locale& __loc);
1150};
1151
1152template <class _CharT>
1153void
1154__num_put<_CharT>::__widen_and_group_int(char* __nb, char* __np, char* __ne,
1155                                         _CharT* __ob, _CharT*& __op, _CharT*& __oe,
1156                                         const locale& __loc)
1157{
1158    const ctype<_CharT>&    __ct = std::use_facet<ctype<_CharT> >   (__loc);
1159    const numpunct<_CharT>& __npt = std::use_facet<numpunct<_CharT> >(__loc);
1160    string __grouping = __npt.grouping();
1161    if (__grouping.empty())
1162    {
1163        __ct.widen(__nb, __ne, __ob);
1164        __oe = __ob + (__ne - __nb);
1165    }
1166    else
1167    {
1168        __oe = __ob;
1169        char* __nf = __nb;
1170        if (*__nf == '-' || *__nf == '+')
1171            *__oe++ = __ct.widen(*__nf++);
1172        if (__ne - __nf >= 2 && __nf[0] == '0' && (__nf[1] == 'x' ||
1173                                                   __nf[1] == 'X'))
1174        {
1175            *__oe++ = __ct.widen(*__nf++);
1176            *__oe++ = __ct.widen(*__nf++);
1177        }
1178        std::reverse(__nf, __ne);
1179        _CharT __thousands_sep = __npt.thousands_sep();
1180        unsigned __dc = 0;
1181        unsigned __dg = 0;
1182        for (char* __p = __nf; __p < __ne; ++__p)
1183        {
1184            if (static_cast<unsigned>(__grouping[__dg]) > 0 &&
1185                __dc == static_cast<unsigned>(__grouping[__dg]))
1186            {
1187                *__oe++ = __thousands_sep;
1188                __dc = 0;
1189                if (__dg < __grouping.size()-1)
1190                    ++__dg;
1191            }
1192            *__oe++ = __ct.widen(*__p);
1193            ++__dc;
1194        }
1195        std::reverse(__ob + (__nf - __nb), __oe);
1196    }
1197    if (__np == __ne)
1198        __op = __oe;
1199    else
1200        __op = __ob + (__np - __nb);
1201}
1202
1203template <class _CharT>
1204void
1205__num_put<_CharT>::__widen_and_group_float(char* __nb, char* __np, char* __ne,
1206                                           _CharT* __ob, _CharT*& __op, _CharT*& __oe,
1207                                           const locale& __loc)
1208{
1209    const ctype<_CharT>&    __ct = std::use_facet<ctype<_CharT> >   (__loc);
1210    const numpunct<_CharT>& __npt = std::use_facet<numpunct<_CharT> >(__loc);
1211    string __grouping = __npt.grouping();
1212    __oe = __ob;
1213    char* __nf = __nb;
1214    if (*__nf == '-' || *__nf == '+')
1215        *__oe++ = __ct.widen(*__nf++);
1216    char* __ns;
1217    if (__ne - __nf >= 2 && __nf[0] == '0' && (__nf[1] == 'x' ||
1218                                               __nf[1] == 'X'))
1219    {
1220        *__oe++ = __ct.widen(*__nf++);
1221        *__oe++ = __ct.widen(*__nf++);
1222        for (__ns = __nf; __ns < __ne; ++__ns)
1223            if (!isxdigit_l(*__ns, _LIBCPP_GET_C_LOCALE))
1224                break;
1225    }
1226    else
1227    {
1228        for (__ns = __nf; __ns < __ne; ++__ns)
1229            if (!isdigit_l(*__ns, _LIBCPP_GET_C_LOCALE))
1230                break;
1231    }
1232    if (__grouping.empty())
1233    {
1234        __ct.widen(__nf, __ns, __oe);
1235        __oe += __ns - __nf;
1236    }
1237    else
1238    {
1239        std::reverse(__nf, __ns);
1240        _CharT __thousands_sep = __npt.thousands_sep();
1241        unsigned __dc = 0;
1242        unsigned __dg = 0;
1243        for (char* __p = __nf; __p < __ns; ++__p)
1244        {
1245            if (__grouping[__dg] > 0 && __dc == static_cast<unsigned>(__grouping[__dg]))
1246            {
1247                *__oe++ = __thousands_sep;
1248                __dc = 0;
1249                if (__dg < __grouping.size()-1)
1250                    ++__dg;
1251            }
1252            *__oe++ = __ct.widen(*__p);
1253            ++__dc;
1254        }
1255        std::reverse(__ob + (__nf - __nb), __oe);
1256    }
1257    for (__nf = __ns; __nf < __ne; ++__nf)
1258    {
1259        if (*__nf == '.')
1260        {
1261            *__oe++ = __npt.decimal_point();
1262            ++__nf;
1263            break;
1264        }
1265        else
1266            *__oe++ = __ct.widen(*__nf);
1267    }
1268    __ct.widen(__nf, __ne, __oe);
1269    __oe += __ne - __nf;
1270    if (__np == __ne)
1271        __op = __oe;
1272    else
1273        __op = __ob + (__np - __nb);
1274}
1275
1276extern template struct _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __num_put<char>;
1277#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1278extern template struct _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __num_put<wchar_t>;
1279#endif
1280
1281template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
1282class _LIBCPP_TEMPLATE_VIS num_put
1283    : public locale::facet,
1284      private __num_put<_CharT>
1285{
1286public:
1287    typedef _CharT char_type;
1288    typedef _OutputIterator iter_type;
1289
1290    _LIBCPP_INLINE_VISIBILITY
1291    explicit num_put(size_t __refs = 0)
1292        : locale::facet(__refs) {}
1293
1294    _LIBCPP_INLINE_VISIBILITY
1295    iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1296                  bool __v) const
1297    {
1298        return do_put(__s, __iob, __fl, __v);
1299    }
1300
1301    _LIBCPP_INLINE_VISIBILITY
1302    iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1303                  long __v) const
1304    {
1305        return do_put(__s, __iob, __fl, __v);
1306    }
1307
1308    _LIBCPP_INLINE_VISIBILITY
1309    iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1310                  long long __v) const
1311    {
1312        return do_put(__s, __iob, __fl, __v);
1313    }
1314
1315    _LIBCPP_INLINE_VISIBILITY
1316    iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1317                  unsigned long __v) const
1318    {
1319        return do_put(__s, __iob, __fl, __v);
1320    }
1321
1322    _LIBCPP_INLINE_VISIBILITY
1323    iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1324                  unsigned long long __v) const
1325    {
1326        return do_put(__s, __iob, __fl, __v);
1327    }
1328
1329    _LIBCPP_INLINE_VISIBILITY
1330    iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1331                  double __v) const
1332    {
1333        return do_put(__s, __iob, __fl, __v);
1334    }
1335
1336    _LIBCPP_INLINE_VISIBILITY
1337    iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1338                  long double __v) const
1339    {
1340        return do_put(__s, __iob, __fl, __v);
1341    }
1342
1343    _LIBCPP_INLINE_VISIBILITY
1344    iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1345                  const void* __v) const
1346    {
1347        return do_put(__s, __iob, __fl, __v);
1348    }
1349
1350    static locale::id id;
1351
1352protected:
1353    _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~num_put() override {}
1354
1355    virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1356                             bool __v) const;
1357    virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1358                             long __v) const;
1359    virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1360                             long long __v) const;
1361    virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1362                             unsigned long) const;
1363    virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1364                             unsigned long long) const;
1365    virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1366                             double __v) const;
1367    virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1368                             long double __v) const;
1369    virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1370                             const void* __v) const;
1371
1372    template <class _Integral>
1373    _LIBCPP_HIDE_FROM_ABI inline
1374    _OutputIterator __do_put_integral(iter_type __s, ios_base& __iob,
1375                                      char_type __fl, _Integral __v,
1376                                      char const* __len) const;
1377
1378    template <class _Float>
1379    _LIBCPP_HIDE_FROM_ABI inline
1380    _OutputIterator __do_put_floating_point(iter_type __s, ios_base& __iob,
1381                                            char_type __fl, _Float __v,
1382                                            char const* __len) const;
1383};
1384
1385template <class _CharT, class _OutputIterator>
1386locale::id
1387num_put<_CharT, _OutputIterator>::id;
1388
1389template <class _CharT, class _OutputIterator>
1390_LIBCPP_HIDE_FROM_ABI
1391_OutputIterator
1392__pad_and_output(_OutputIterator __s,
1393                 const _CharT* __ob, const _CharT* __op, const _CharT* __oe,
1394                 ios_base& __iob, _CharT __fl)
1395{
1396    streamsize __sz = __oe - __ob;
1397    streamsize __ns = __iob.width();
1398    if (__ns > __sz)
1399        __ns -= __sz;
1400    else
1401        __ns = 0;
1402    for (;__ob < __op; ++__ob, ++__s)
1403        *__s = *__ob;
1404    for (; __ns; --__ns, ++__s)
1405        *__s = __fl;
1406    for (; __ob < __oe; ++__ob, ++__s)
1407        *__s = *__ob;
1408    __iob.width(0);
1409    return __s;
1410}
1411
1412template <class _CharT, class _Traits>
1413_LIBCPP_HIDE_FROM_ABI
1414ostreambuf_iterator<_CharT, _Traits>
1415__pad_and_output(ostreambuf_iterator<_CharT, _Traits> __s,
1416                 const _CharT* __ob, const _CharT* __op, const _CharT* __oe,
1417                 ios_base& __iob, _CharT __fl)
1418{
1419    if (__s.__sbuf_ == nullptr)
1420        return __s;
1421    streamsize __sz = __oe - __ob;
1422    streamsize __ns = __iob.width();
1423    if (__ns > __sz)
1424        __ns -= __sz;
1425    else
1426        __ns = 0;
1427    streamsize __np = __op - __ob;
1428    if (__np > 0)
1429    {
1430        if (__s.__sbuf_->sputn(__ob, __np) != __np)
1431        {
1432            __s.__sbuf_ = nullptr;
1433            return __s;
1434        }
1435    }
1436    if (__ns > 0)
1437    {
1438        basic_string<_CharT, _Traits> __sp(__ns, __fl);
1439        if (__s.__sbuf_->sputn(__sp.data(), __ns) != __ns)
1440        {
1441            __s.__sbuf_ = nullptr;
1442            return __s;
1443        }
1444    }
1445    __np = __oe - __op;
1446    if (__np > 0)
1447    {
1448        if (__s.__sbuf_->sputn(__op, __np) != __np)
1449        {
1450            __s.__sbuf_ = nullptr;
1451            return __s;
1452        }
1453    }
1454    __iob.width(0);
1455    return __s;
1456}
1457
1458template <class _CharT, class _OutputIterator>
1459_OutputIterator
1460num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1461                                         char_type __fl, bool __v) const
1462{
1463    if ((__iob.flags() & ios_base::boolalpha) == 0)
1464        return do_put(__s, __iob, __fl, (unsigned long)__v);
1465    const numpunct<char_type>& __np = std::use_facet<numpunct<char_type> >(__iob.getloc());
1466    typedef typename numpunct<char_type>::string_type string_type;
1467    string_type __nm = __v ? __np.truename() : __np.falsename();
1468    for (typename string_type::iterator __i = __nm.begin(); __i != __nm.end(); ++__i, ++__s)
1469        *__s = *__i;
1470    return __s;
1471}
1472
1473template <class _CharT, class _OutputIterator>
1474template <class _Integral>
1475_LIBCPP_HIDE_FROM_ABI inline
1476_OutputIterator
1477num_put<_CharT, _OutputIterator>::__do_put_integral(iter_type __s, ios_base& __iob,
1478                                                    char_type __fl, _Integral __v,
1479                                                    char const* __len) const
1480{
1481    // Stage 1 - Get number in narrow char
1482    char __fmt[8] = {'%', 0};
1483    this->__format_int(__fmt+1, __len, is_signed<_Integral>::value, __iob.flags());
1484    // Worst case is octal, with showbase enabled. Note that octal is always
1485    // printed as an unsigned value.
1486    using _Unsigned = typename make_unsigned<_Integral>::type;
1487    _LIBCPP_CONSTEXPR const unsigned __nbuf
1488        = (numeric_limits<_Unsigned>::digits / 3)        // 1 char per 3 bits
1489        + ((numeric_limits<_Unsigned>::digits % 3) != 0) // round up
1490        + 2; // base prefix + terminating null character
1491    char __nar[__nbuf];
1492    _LIBCPP_DIAGNOSTIC_PUSH
1493    _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wformat-nonliteral")
1494    _LIBCPP_GCC_DIAGNOSTIC_IGNORED("-Wformat-nonliteral")
1495    int __nc = __libcpp_snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v);
1496    _LIBCPP_DIAGNOSTIC_POP
1497    char* __ne = __nar + __nc;
1498    char* __np = this->__identify_padding(__nar, __ne, __iob);
1499    // Stage 2 - Widen __nar while adding thousands separators
1500    char_type __o[2*(__nbuf-1) - 1];
1501    char_type* __op;  // pad here
1502    char_type* __oe;  // end of output
1503    this->__widen_and_group_int(__nar, __np, __ne, __o, __op, __oe, __iob.getloc());
1504    // [__o, __oe) contains thousands_sep'd wide number
1505    // Stage 3 & 4
1506    return std::__pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1507}
1508
1509template <class _CharT, class _OutputIterator>
1510_OutputIterator
1511num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1512                                         char_type __fl, long __v) const
1513{
1514    return this->__do_put_integral(__s, __iob, __fl, __v, "l");
1515}
1516
1517template <class _CharT, class _OutputIterator>
1518_OutputIterator
1519num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1520                                         char_type __fl, long long __v) const
1521{
1522    return this->__do_put_integral(__s, __iob, __fl, __v, "ll");
1523}
1524
1525template <class _CharT, class _OutputIterator>
1526_OutputIterator
1527num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1528                                         char_type __fl, unsigned long __v) const
1529{
1530    return this->__do_put_integral(__s, __iob, __fl, __v, "l");
1531}
1532
1533template <class _CharT, class _OutputIterator>
1534_OutputIterator
1535num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1536                                         char_type __fl, unsigned long long __v) const
1537{
1538    return this->__do_put_integral(__s, __iob, __fl, __v, "ll");
1539}
1540
1541template <class _CharT, class _OutputIterator>
1542template <class _Float>
1543_LIBCPP_HIDE_FROM_ABI inline
1544_OutputIterator
1545num_put<_CharT, _OutputIterator>::__do_put_floating_point(iter_type __s, ios_base& __iob,
1546                                                          char_type __fl, _Float __v,
1547                                                          char const* __len) const
1548{
1549    // Stage 1 - Get number in narrow char
1550    char __fmt[8] = {'%', 0};
1551    bool __specify_precision = this->__format_float(__fmt+1, __len, __iob.flags());
1552    const unsigned __nbuf = 30;
1553    char __nar[__nbuf];
1554    char* __nb = __nar;
1555    int __nc;
1556    _LIBCPP_DIAGNOSTIC_PUSH
1557    _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wformat-nonliteral")
1558    _LIBCPP_GCC_DIAGNOSTIC_IGNORED("-Wformat-nonliteral")
1559    if (__specify_precision)
1560        __nc = __libcpp_snprintf_l(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt,
1561                                   (int)__iob.precision(), __v);
1562    else
1563        __nc = __libcpp_snprintf_l(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt, __v);
1564    unique_ptr<char, void(*)(void*)> __nbh(nullptr, free);
1565    if (__nc > static_cast<int>(__nbuf-1))
1566    {
1567        if (__specify_precision)
1568            __nc = __libcpp_asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, (int)__iob.precision(), __v);
1569        else
1570            __nc = __libcpp_asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, __v);
1571        if (__nc == -1)
1572            __throw_bad_alloc();
1573        __nbh.reset(__nb);
1574    }
1575    _LIBCPP_DIAGNOSTIC_POP
1576    char* __ne = __nb + __nc;
1577    char* __np = this->__identify_padding(__nb, __ne, __iob);
1578    // Stage 2 - Widen __nar while adding thousands separators
1579    char_type __o[2*(__nbuf-1) - 1];
1580    char_type* __ob = __o;
1581    unique_ptr<char_type, void(*)(void*)> __obh(0, free);
1582    if (__nb != __nar)
1583    {
1584        __ob = (char_type*)malloc(2*static_cast<size_t>(__nc)*sizeof(char_type));
1585        if (__ob == 0)
1586            __throw_bad_alloc();
1587        __obh.reset(__ob);
1588    }
1589    char_type* __op;  // pad here
1590    char_type* __oe;  // end of output
1591    this->__widen_and_group_float(__nb, __np, __ne, __ob, __op, __oe, __iob.getloc());
1592    // [__o, __oe) contains thousands_sep'd wide number
1593    // Stage 3 & 4
1594    __s = std::__pad_and_output(__s, __ob, __op, __oe, __iob, __fl);
1595    return __s;
1596}
1597
1598template <class _CharT, class _OutputIterator>
1599_OutputIterator
1600num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1601                                         char_type __fl, double __v) const
1602{
1603    return this->__do_put_floating_point(__s, __iob, __fl, __v, "");
1604}
1605
1606template <class _CharT, class _OutputIterator>
1607_OutputIterator
1608num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1609                                         char_type __fl, long double __v) const
1610{
1611    return this->__do_put_floating_point(__s, __iob, __fl, __v, "L");
1612}
1613
1614template <class _CharT, class _OutputIterator>
1615_OutputIterator
1616num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1617                                         char_type __fl, const void* __v) const
1618{
1619    // Stage 1 - Get pointer in narrow char
1620    const unsigned __nbuf = 20;
1621    char __nar[__nbuf];
1622    int __nc = __libcpp_snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, "%p", __v);
1623    char* __ne = __nar + __nc;
1624    char* __np = this->__identify_padding(__nar, __ne, __iob);
1625    // Stage 2 - Widen __nar
1626    char_type __o[2*(__nbuf-1) - 1];
1627    char_type* __op;  // pad here
1628    char_type* __oe;  // end of output
1629    const ctype<char_type>& __ct = std::use_facet<ctype<char_type> >(__iob.getloc());
1630    __ct.widen(__nar, __ne, __o);
1631    __oe = __o + (__ne - __nar);
1632    if (__np == __ne)
1633        __op = __oe;
1634    else
1635        __op = __o + (__np - __nar);
1636    // [__o, __oe) contains wide number
1637    // Stage 3 & 4
1638    return std::__pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1639}
1640
1641extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS num_put<char>;
1642#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1643extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS num_put<wchar_t>;
1644#endif
1645
1646template <class _CharT, class _InputIterator>
1647_LIBCPP_HIDE_FROM_ABI
1648int
1649__get_up_to_n_digits(_InputIterator& __b, _InputIterator __e,
1650                     ios_base::iostate& __err, const ctype<_CharT>& __ct, int __n)
1651{
1652    // Precondition:  __n >= 1
1653    if (__b == __e)
1654    {
1655        __err |= ios_base::eofbit | ios_base::failbit;
1656        return 0;
1657    }
1658    // get first digit
1659    _CharT __c = *__b;
1660    if (!__ct.is(ctype_base::digit, __c))
1661    {
1662        __err |= ios_base::failbit;
1663        return 0;
1664    }
1665    int __r = __ct.narrow(__c, 0) - '0';
1666    for (++__b, (void) --__n; __b != __e && __n > 0; ++__b, (void) --__n)
1667    {
1668        // get next digit
1669        __c = *__b;
1670        if (!__ct.is(ctype_base::digit, __c))
1671            return __r;
1672        __r = __r * 10 + __ct.narrow(__c, 0) - '0';
1673    }
1674    if (__b == __e)
1675        __err |= ios_base::eofbit;
1676    return __r;
1677}
1678
1679class _LIBCPP_EXPORTED_FROM_ABI time_base
1680{
1681public:
1682    enum dateorder {no_order, dmy, mdy, ymd, ydm};
1683};
1684
1685template <class _CharT>
1686class _LIBCPP_TEMPLATE_VIS __time_get_c_storage
1687{
1688protected:
1689    typedef basic_string<_CharT> string_type;
1690
1691    virtual const string_type* __weeks() const;
1692    virtual const string_type* __months() const;
1693    virtual const string_type* __am_pm() const;
1694    virtual const string_type& __c() const;
1695    virtual const string_type& __r() const;
1696    virtual const string_type& __x() const;
1697    virtual const string_type& __X() const;
1698
1699    _LIBCPP_INLINE_VISIBILITY
1700    ~__time_get_c_storage() {}
1701};
1702
1703template <> _LIBCPP_EXPORTED_FROM_ABI const string* __time_get_c_storage<char>::__weeks() const;
1704template <> _LIBCPP_EXPORTED_FROM_ABI const string* __time_get_c_storage<char>::__months() const;
1705template <> _LIBCPP_EXPORTED_FROM_ABI const string* __time_get_c_storage<char>::__am_pm() const;
1706template <> _LIBCPP_EXPORTED_FROM_ABI const string& __time_get_c_storage<char>::__c() const;
1707template <> _LIBCPP_EXPORTED_FROM_ABI const string& __time_get_c_storage<char>::__r() const;
1708template <> _LIBCPP_EXPORTED_FROM_ABI const string& __time_get_c_storage<char>::__x() const;
1709template <> _LIBCPP_EXPORTED_FROM_ABI const string& __time_get_c_storage<char>::__X() const;
1710
1711#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1712template <> _LIBCPP_EXPORTED_FROM_ABI const wstring* __time_get_c_storage<wchar_t>::__weeks() const;
1713template <> _LIBCPP_EXPORTED_FROM_ABI const wstring* __time_get_c_storage<wchar_t>::__months() const;
1714template <> _LIBCPP_EXPORTED_FROM_ABI const wstring* __time_get_c_storage<wchar_t>::__am_pm() const;
1715template <> _LIBCPP_EXPORTED_FROM_ABI const wstring& __time_get_c_storage<wchar_t>::__c() const;
1716template <> _LIBCPP_EXPORTED_FROM_ABI const wstring& __time_get_c_storage<wchar_t>::__r() const;
1717template <> _LIBCPP_EXPORTED_FROM_ABI const wstring& __time_get_c_storage<wchar_t>::__x() const;
1718template <> _LIBCPP_EXPORTED_FROM_ABI const wstring& __time_get_c_storage<wchar_t>::__X() const;
1719#endif
1720
1721template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
1722class _LIBCPP_TEMPLATE_VIS time_get
1723    : public locale::facet,
1724      public time_base,
1725      private __time_get_c_storage<_CharT>
1726{
1727public:
1728    typedef _CharT                  char_type;
1729    typedef _InputIterator          iter_type;
1730    typedef time_base::dateorder    dateorder;
1731    typedef basic_string<char_type> string_type;
1732
1733    _LIBCPP_INLINE_VISIBILITY
1734    explicit time_get(size_t __refs = 0)
1735        : locale::facet(__refs) {}
1736
1737    _LIBCPP_INLINE_VISIBILITY
1738    dateorder date_order() const
1739    {
1740        return this->do_date_order();
1741    }
1742
1743    _LIBCPP_INLINE_VISIBILITY
1744    iter_type get_time(iter_type __b, iter_type __e, ios_base& __iob,
1745                       ios_base::iostate& __err, tm* __tm) const
1746    {
1747        return do_get_time(__b, __e, __iob, __err, __tm);
1748    }
1749
1750    _LIBCPP_INLINE_VISIBILITY
1751    iter_type get_date(iter_type __b, iter_type __e, ios_base& __iob,
1752                       ios_base::iostate& __err, tm* __tm) const
1753    {
1754        return do_get_date(__b, __e, __iob, __err, __tm);
1755    }
1756
1757    _LIBCPP_INLINE_VISIBILITY
1758    iter_type get_weekday(iter_type __b, iter_type __e, ios_base& __iob,
1759                          ios_base::iostate& __err, tm* __tm) const
1760    {
1761        return do_get_weekday(__b, __e, __iob, __err, __tm);
1762    }
1763
1764    _LIBCPP_INLINE_VISIBILITY
1765    iter_type get_monthname(iter_type __b, iter_type __e, ios_base& __iob,
1766                            ios_base::iostate& __err, tm* __tm) const
1767    {
1768        return do_get_monthname(__b, __e, __iob, __err, __tm);
1769    }
1770
1771    _LIBCPP_INLINE_VISIBILITY
1772    iter_type get_year(iter_type __b, iter_type __e, ios_base& __iob,
1773                       ios_base::iostate& __err, tm* __tm) const
1774    {
1775        return do_get_year(__b, __e, __iob, __err, __tm);
1776    }
1777
1778    _LIBCPP_INLINE_VISIBILITY
1779    iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
1780                  ios_base::iostate& __err, tm *__tm,
1781                  char __fmt, char __mod = 0) const
1782    {
1783        return do_get(__b, __e, __iob, __err, __tm, __fmt, __mod);
1784    }
1785
1786    iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
1787                  ios_base::iostate& __err, tm* __tm,
1788                  const char_type* __fmtb, const char_type* __fmte) const;
1789
1790    static locale::id id;
1791
1792protected:
1793    _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~time_get() override {}
1794
1795    virtual dateorder do_date_order() const;
1796    virtual iter_type do_get_time(iter_type __b, iter_type __e, ios_base& __iob,
1797                                  ios_base::iostate& __err, tm* __tm) const;
1798    virtual iter_type do_get_date(iter_type __b, iter_type __e, ios_base& __iob,
1799                                  ios_base::iostate& __err, tm* __tm) const;
1800    virtual iter_type do_get_weekday(iter_type __b, iter_type __e, ios_base& __iob,
1801                                     ios_base::iostate& __err, tm* __tm) const;
1802    virtual iter_type do_get_monthname(iter_type __b, iter_type __e, ios_base& __iob,
1803                                       ios_base::iostate& __err, tm* __tm) const;
1804    virtual iter_type do_get_year(iter_type __b, iter_type __e, ios_base& __iob,
1805                                  ios_base::iostate& __err, tm* __tm) const;
1806    virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
1807                             ios_base::iostate& __err, tm* __tm,
1808                             char __fmt, char __mod) const;
1809private:
1810    void __get_white_space(iter_type& __b, iter_type __e,
1811                           ios_base::iostate& __err, const ctype<char_type>& __ct) const;
1812    void __get_percent(iter_type& __b, iter_type __e, ios_base::iostate& __err,
1813                       const ctype<char_type>& __ct) const;
1814
1815    void __get_weekdayname(int& __m,
1816                           iter_type& __b, iter_type __e,
1817                           ios_base::iostate& __err,
1818                           const ctype<char_type>& __ct) const;
1819    void __get_monthname(int& __m,
1820                         iter_type& __b, iter_type __e,
1821                         ios_base::iostate& __err,
1822                         const ctype<char_type>& __ct) const;
1823    void __get_day(int& __d,
1824                   iter_type& __b, iter_type __e,
1825                   ios_base::iostate& __err,
1826                   const ctype<char_type>& __ct) const;
1827    void __get_month(int& __m,
1828                     iter_type& __b, iter_type __e,
1829                     ios_base::iostate& __err,
1830                     const ctype<char_type>& __ct) const;
1831    void __get_year(int& __y,
1832                   iter_type& __b, iter_type __e,
1833                   ios_base::iostate& __err,
1834                   const ctype<char_type>& __ct) const;
1835    void __get_year4(int& __y,
1836                    iter_type& __b, iter_type __e,
1837                    ios_base::iostate& __err,
1838                    const ctype<char_type>& __ct) const;
1839    void __get_hour(int& __d,
1840                    iter_type& __b, iter_type __e,
1841                    ios_base::iostate& __err,
1842                    const ctype<char_type>& __ct) const;
1843    void __get_12_hour(int& __h,
1844                       iter_type& __b, iter_type __e,
1845                       ios_base::iostate& __err,
1846                       const ctype<char_type>& __ct) const;
1847    void __get_am_pm(int& __h,
1848                     iter_type& __b, iter_type __e,
1849                     ios_base::iostate& __err,
1850                     const ctype<char_type>& __ct) const;
1851    void __get_minute(int& __m,
1852                      iter_type& __b, iter_type __e,
1853                      ios_base::iostate& __err,
1854                      const ctype<char_type>& __ct) const;
1855    void __get_second(int& __s,
1856                      iter_type& __b, iter_type __e,
1857                      ios_base::iostate& __err,
1858                      const ctype<char_type>& __ct) const;
1859    void __get_weekday(int& __w,
1860                       iter_type& __b, iter_type __e,
1861                       ios_base::iostate& __err,
1862                       const ctype<char_type>& __ct) const;
1863    void __get_day_year_num(int& __w,
1864                            iter_type& __b, iter_type __e,
1865                            ios_base::iostate& __err,
1866                            const ctype<char_type>& __ct) const;
1867};
1868
1869template <class _CharT, class _InputIterator>
1870locale::id
1871time_get<_CharT, _InputIterator>::id;
1872
1873// time_get primitives
1874
1875template <class _CharT, class _InputIterator>
1876void
1877time_get<_CharT, _InputIterator>::__get_weekdayname(int& __w,
1878                                                    iter_type& __b, iter_type __e,
1879                                                    ios_base::iostate& __err,
1880                                                    const ctype<char_type>& __ct) const
1881{
1882    // Note:  ignoring case comes from the POSIX strptime spec
1883    const string_type* __wk = this->__weeks();
1884    ptrdiff_t __i = _VSTD::__scan_keyword(__b, __e, __wk, __wk+14, __ct, __err, false) - __wk;
1885    if (__i < 14)
1886        __w = __i % 7;
1887}
1888
1889template <class _CharT, class _InputIterator>
1890void
1891time_get<_CharT, _InputIterator>::__get_monthname(int& __m,
1892                                                  iter_type& __b, iter_type __e,
1893                                                  ios_base::iostate& __err,
1894                                                  const ctype<char_type>& __ct) const
1895{
1896    // Note:  ignoring case comes from the POSIX strptime spec
1897    const string_type* __month = this->__months();
1898    ptrdiff_t __i = _VSTD::__scan_keyword(__b, __e, __month, __month+24, __ct, __err, false) - __month;
1899    if (__i < 24)
1900        __m = __i % 12;
1901}
1902
1903template <class _CharT, class _InputIterator>
1904void
1905time_get<_CharT, _InputIterator>::__get_day(int& __d,
1906                                            iter_type& __b, iter_type __e,
1907                                            ios_base::iostate& __err,
1908                                            const ctype<char_type>& __ct) const
1909{
1910    int __t = _VSTD::__get_up_to_n_digits(__b, __e, __err, __ct, 2);
1911    if (!(__err & ios_base::failbit) && 1 <= __t && __t <= 31)
1912        __d = __t;
1913    else
1914        __err |= ios_base::failbit;
1915}
1916
1917template <class _CharT, class _InputIterator>
1918void
1919time_get<_CharT, _InputIterator>::__get_month(int& __m,
1920                                              iter_type& __b, iter_type __e,
1921                                              ios_base::iostate& __err,
1922                                              const ctype<char_type>& __ct) const
1923{
1924    int __t = std::__get_up_to_n_digits(__b, __e, __err, __ct, 2) - 1;
1925    if (!(__err & ios_base::failbit) && 0 <= __t && __t <= 11)
1926        __m = __t;
1927    else
1928        __err |= ios_base::failbit;
1929}
1930
1931template <class _CharT, class _InputIterator>
1932void
1933time_get<_CharT, _InputIterator>::__get_year(int& __y,
1934                                             iter_type& __b, iter_type __e,
1935                                             ios_base::iostate& __err,
1936                                             const ctype<char_type>& __ct) const
1937{
1938    int __t = std::__get_up_to_n_digits(__b, __e, __err, __ct, 4);
1939    if (!(__err & ios_base::failbit))
1940    {
1941        if (__t < 69)
1942            __t += 2000;
1943        else if (69 <= __t && __t <= 99)
1944            __t += 1900;
1945        __y = __t - 1900;
1946    }
1947}
1948
1949template <class _CharT, class _InputIterator>
1950void
1951time_get<_CharT, _InputIterator>::__get_year4(int& __y,
1952                                              iter_type& __b, iter_type __e,
1953                                              ios_base::iostate& __err,
1954                                              const ctype<char_type>& __ct) const
1955{
1956    int __t = std::__get_up_to_n_digits(__b, __e, __err, __ct, 4);
1957    if (!(__err & ios_base::failbit))
1958        __y = __t - 1900;
1959}
1960
1961template <class _CharT, class _InputIterator>
1962void
1963time_get<_CharT, _InputIterator>::__get_hour(int& __h,
1964                                             iter_type& __b, iter_type __e,
1965                                             ios_base::iostate& __err,
1966                                             const ctype<char_type>& __ct) const
1967{
1968    int __t = std::__get_up_to_n_digits(__b, __e, __err, __ct, 2);
1969    if (!(__err & ios_base::failbit) && __t <= 23)
1970        __h = __t;
1971    else
1972        __err |= ios_base::failbit;
1973}
1974
1975template <class _CharT, class _InputIterator>
1976void
1977time_get<_CharT, _InputIterator>::__get_12_hour(int& __h,
1978                                                iter_type& __b, iter_type __e,
1979                                                ios_base::iostate& __err,
1980                                                const ctype<char_type>& __ct) const
1981{
1982    int __t = std::__get_up_to_n_digits(__b, __e, __err, __ct, 2);
1983    if (!(__err & ios_base::failbit) && 1 <= __t && __t <= 12)
1984        __h = __t;
1985    else
1986        __err |= ios_base::failbit;
1987}
1988
1989template <class _CharT, class _InputIterator>
1990void
1991time_get<_CharT, _InputIterator>::__get_minute(int& __m,
1992                                               iter_type& __b, iter_type __e,
1993                                               ios_base::iostate& __err,
1994                                               const ctype<char_type>& __ct) const
1995{
1996    int __t = std::__get_up_to_n_digits(__b, __e, __err, __ct, 2);
1997    if (!(__err & ios_base::failbit) && __t <= 59)
1998        __m = __t;
1999    else
2000        __err |= ios_base::failbit;
2001}
2002
2003template <class _CharT, class _InputIterator>
2004void
2005time_get<_CharT, _InputIterator>::__get_second(int& __s,
2006                                               iter_type& __b, iter_type __e,
2007                                               ios_base::iostate& __err,
2008                                               const ctype<char_type>& __ct) const
2009{
2010    int __t = std::__get_up_to_n_digits(__b, __e, __err, __ct, 2);
2011    if (!(__err & ios_base::failbit) && __t <= 60)
2012        __s = __t;
2013    else
2014        __err |= ios_base::failbit;
2015}
2016
2017template <class _CharT, class _InputIterator>
2018void
2019time_get<_CharT, _InputIterator>::__get_weekday(int& __w,
2020                                                iter_type& __b, iter_type __e,
2021                                                ios_base::iostate& __err,
2022                                                const ctype<char_type>& __ct) const
2023{
2024    int __t = std::__get_up_to_n_digits(__b, __e, __err, __ct, 1);
2025    if (!(__err & ios_base::failbit) && __t <= 6)
2026        __w = __t;
2027    else
2028        __err |= ios_base::failbit;
2029}
2030
2031template <class _CharT, class _InputIterator>
2032void
2033time_get<_CharT, _InputIterator>::__get_day_year_num(int& __d,
2034                                                     iter_type& __b, iter_type __e,
2035                                                     ios_base::iostate& __err,
2036                                                     const ctype<char_type>& __ct) const
2037{
2038    int __t = std::__get_up_to_n_digits(__b, __e, __err, __ct, 3);
2039    if (!(__err & ios_base::failbit) && __t <= 365)
2040        __d = __t;
2041    else
2042        __err |= ios_base::failbit;
2043}
2044
2045template <class _CharT, class _InputIterator>
2046void
2047time_get<_CharT, _InputIterator>::__get_white_space(iter_type& __b, iter_type __e,
2048                                                    ios_base::iostate& __err,
2049                                                    const ctype<char_type>& __ct) const
2050{
2051    for (; __b != __e && __ct.is(ctype_base::space, *__b); ++__b)
2052        ;
2053    if (__b == __e)
2054        __err |= ios_base::eofbit;
2055}
2056
2057template <class _CharT, class _InputIterator>
2058void
2059time_get<_CharT, _InputIterator>::__get_am_pm(int& __h,
2060                                              iter_type& __b, iter_type __e,
2061                                              ios_base::iostate& __err,
2062                                              const ctype<char_type>& __ct) const
2063{
2064    const string_type* __ap = this->__am_pm();
2065    if (__ap[0].size() + __ap[1].size() == 0)
2066    {
2067        __err |= ios_base::failbit;
2068        return;
2069    }
2070    ptrdiff_t __i = _VSTD::__scan_keyword(__b, __e, __ap, __ap+2, __ct, __err, false) - __ap;
2071    if (__i == 0 && __h == 12)
2072        __h = 0;
2073    else if (__i == 1 && __h < 12)
2074        __h += 12;
2075}
2076
2077template <class _CharT, class _InputIterator>
2078void
2079time_get<_CharT, _InputIterator>::__get_percent(iter_type& __b, iter_type __e,
2080                                                ios_base::iostate& __err,
2081                                                const ctype<char_type>& __ct) const
2082{
2083    if (__b == __e)
2084    {
2085        __err |= ios_base::eofbit | ios_base::failbit;
2086        return;
2087    }
2088    if (__ct.narrow(*__b, 0) != '%')
2089        __err |= ios_base::failbit;
2090    else if(++__b == __e)
2091        __err |= ios_base::eofbit;
2092}
2093
2094// time_get end primitives
2095
2096template <class _CharT, class _InputIterator>
2097_InputIterator
2098time_get<_CharT, _InputIterator>::get(iter_type __b, iter_type __e,
2099                                      ios_base& __iob,
2100                                      ios_base::iostate& __err, tm* __tm,
2101                                      const char_type* __fmtb, const char_type* __fmte) const
2102{
2103    const ctype<char_type>& __ct = std::use_facet<ctype<char_type> >(__iob.getloc());
2104    __err = ios_base::goodbit;
2105    while (__fmtb != __fmte && __err == ios_base::goodbit)
2106    {
2107        if (__b == __e)
2108        {
2109            __err = ios_base::failbit;
2110            break;
2111        }
2112        if (__ct.narrow(*__fmtb, 0) == '%')
2113        {
2114            if (++__fmtb == __fmte)
2115            {
2116                __err = ios_base::failbit;
2117                break;
2118            }
2119            char __cmd = __ct.narrow(*__fmtb, 0);
2120            char __opt = '\0';
2121            if (__cmd == 'E' || __cmd == '0')
2122            {
2123                if (++__fmtb == __fmte)
2124                {
2125                    __err = ios_base::failbit;
2126                    break;
2127                }
2128                __opt = __cmd;
2129                __cmd = __ct.narrow(*__fmtb, 0);
2130            }
2131            __b = do_get(__b, __e, __iob, __err, __tm, __cmd, __opt);
2132            ++__fmtb;
2133        }
2134        else if (__ct.is(ctype_base::space, *__fmtb))
2135        {
2136            for (++__fmtb; __fmtb != __fmte && __ct.is(ctype_base::space, *__fmtb); ++__fmtb)
2137                ;
2138            for (        ;    __b != __e    && __ct.is(ctype_base::space, *__b);    ++__b)
2139                ;
2140        }
2141        else if (__ct.toupper(*__b) == __ct.toupper(*__fmtb))
2142        {
2143            ++__b;
2144            ++__fmtb;
2145        }
2146        else
2147            __err = ios_base::failbit;
2148    }
2149    if (__b == __e)
2150        __err |= ios_base::eofbit;
2151    return __b;
2152}
2153
2154template <class _CharT, class _InputIterator>
2155typename time_get<_CharT, _InputIterator>::dateorder
2156time_get<_CharT, _InputIterator>::do_date_order() const
2157{
2158    return mdy;
2159}
2160
2161template <class _CharT, class _InputIterator>
2162_InputIterator
2163time_get<_CharT, _InputIterator>::do_get_time(iter_type __b, iter_type __e,
2164                                              ios_base& __iob,
2165                                              ios_base::iostate& __err,
2166                                              tm* __tm) const
2167{
2168    const char_type __fmt[] = {'%', 'H', ':', '%', 'M', ':', '%', 'S'};
2169    return get(__b, __e, __iob, __err, __tm, __fmt, __fmt + sizeof(__fmt)/sizeof(__fmt[0]));
2170}
2171
2172template <class _CharT, class _InputIterator>
2173_InputIterator
2174time_get<_CharT, _InputIterator>::do_get_date(iter_type __b, iter_type __e,
2175                                              ios_base& __iob,
2176                                              ios_base::iostate& __err,
2177                                              tm* __tm) const
2178{
2179    const string_type& __fmt = this->__x();
2180    return get(__b, __e, __iob, __err, __tm, __fmt.data(), __fmt.data() + __fmt.size());
2181}
2182
2183template <class _CharT, class _InputIterator>
2184_InputIterator
2185time_get<_CharT, _InputIterator>::do_get_weekday(iter_type __b, iter_type __e,
2186                                                 ios_base& __iob,
2187                                                 ios_base::iostate& __err,
2188                                                 tm* __tm) const
2189{
2190    const ctype<char_type>& __ct = std::use_facet<ctype<char_type> >(__iob.getloc());
2191    __get_weekdayname(__tm->tm_wday, __b, __e, __err, __ct);
2192    return __b;
2193}
2194
2195template <class _CharT, class _InputIterator>
2196_InputIterator
2197time_get<_CharT, _InputIterator>::do_get_monthname(iter_type __b, iter_type __e,
2198                                                   ios_base& __iob,
2199                                                   ios_base::iostate& __err,
2200                                                   tm* __tm) const
2201{
2202    const ctype<char_type>& __ct = std::use_facet<ctype<char_type> >(__iob.getloc());
2203    __get_monthname(__tm->tm_mon, __b, __e, __err, __ct);
2204    return __b;
2205}
2206
2207template <class _CharT, class _InputIterator>
2208_InputIterator
2209time_get<_CharT, _InputIterator>::do_get_year(iter_type __b, iter_type __e,
2210                                              ios_base& __iob,
2211                                              ios_base::iostate& __err,
2212                                              tm* __tm) const
2213{
2214    const ctype<char_type>& __ct = std::use_facet<ctype<char_type> >(__iob.getloc());
2215    __get_year(__tm->tm_year, __b, __e, __err, __ct);
2216    return __b;
2217}
2218
2219template <class _CharT, class _InputIterator>
2220_InputIterator
2221time_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
2222                                         ios_base& __iob,
2223                                         ios_base::iostate& __err, tm* __tm,
2224                                         char __fmt, char) const
2225{
2226    __err = ios_base::goodbit;
2227    const ctype<char_type>& __ct = std::use_facet<ctype<char_type> >(__iob.getloc());
2228    switch (__fmt)
2229    {
2230    case 'a':
2231    case 'A':
2232        __get_weekdayname(__tm->tm_wday, __b, __e, __err, __ct);
2233        break;
2234    case 'b':
2235    case 'B':
2236    case 'h':
2237        __get_monthname(__tm->tm_mon, __b, __e, __err, __ct);
2238        break;
2239    case 'c':
2240        {
2241        const string_type& __fm = this->__c();
2242        __b = get(__b, __e, __iob, __err, __tm, __fm.data(), __fm.data() + __fm.size());
2243        }
2244        break;
2245    case 'd':
2246    case 'e':
2247        __get_day(__tm->tm_mday, __b, __e, __err, __ct);
2248        break;
2249    case 'D':
2250        {
2251        const char_type __fm[] = {'%', 'm', '/', '%', 'd', '/', '%', 'y'};
2252        __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
2253        }
2254        break;
2255    case 'F':
2256        {
2257        const char_type __fm[] = {'%', 'Y', '-', '%', 'm', '-', '%', 'd'};
2258        __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
2259        }
2260        break;
2261    case 'H':
2262        __get_hour(__tm->tm_hour, __b, __e, __err, __ct);
2263        break;
2264    case 'I':
2265        __get_12_hour(__tm->tm_hour, __b, __e, __err, __ct);
2266        break;
2267    case 'j':
2268        __get_day_year_num(__tm->tm_yday, __b, __e, __err, __ct);
2269        break;
2270    case 'm':
2271        __get_month(__tm->tm_mon, __b, __e, __err, __ct);
2272        break;
2273    case 'M':
2274        __get_minute(__tm->tm_min, __b, __e, __err, __ct);
2275        break;
2276    case 'n':
2277    case 't':
2278        __get_white_space(__b, __e, __err, __ct);
2279        break;
2280    case 'p':
2281        __get_am_pm(__tm->tm_hour, __b, __e, __err, __ct);
2282        break;
2283    case 'r':
2284        {
2285        const char_type __fm[] = {'%', 'I', ':', '%', 'M', ':', '%', 'S', ' ', '%', 'p'};
2286        __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
2287        }
2288        break;
2289    case 'R':
2290        {
2291        const char_type __fm[] = {'%', 'H', ':', '%', 'M'};
2292        __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
2293        }
2294        break;
2295    case 'S':
2296        __get_second(__tm->tm_sec, __b, __e, __err, __ct);
2297        break;
2298    case 'T':
2299        {
2300        const char_type __fm[] = {'%', 'H', ':', '%', 'M', ':', '%', 'S'};
2301        __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
2302        }
2303        break;
2304    case 'w':
2305        __get_weekday(__tm->tm_wday, __b, __e, __err, __ct);
2306        break;
2307    case 'x':
2308        return do_get_date(__b, __e, __iob, __err, __tm);
2309    case 'X':
2310        {
2311        const string_type& __fm = this->__X();
2312        __b = get(__b, __e, __iob, __err, __tm, __fm.data(), __fm.data() + __fm.size());
2313        }
2314        break;
2315    case 'y':
2316        __get_year(__tm->tm_year, __b, __e, __err, __ct);
2317        break;
2318    case 'Y':
2319        __get_year4(__tm->tm_year, __b, __e, __err, __ct);
2320        break;
2321    case '%':
2322        __get_percent(__b, __e, __err, __ct);
2323        break;
2324    default:
2325        __err |= ios_base::failbit;
2326    }
2327    return __b;
2328}
2329
2330extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_get<char>;
2331#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
2332extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_get<wchar_t>;
2333#endif
2334
2335class _LIBCPP_EXPORTED_FROM_ABI __time_get
2336{
2337protected:
2338    locale_t __loc_;
2339
2340    __time_get(const char* __nm);
2341    __time_get(const string& __nm);
2342    ~__time_get();
2343};
2344
2345template <class _CharT>
2346class _LIBCPP_TEMPLATE_VIS __time_get_storage
2347    : public __time_get
2348{
2349protected:
2350    typedef basic_string<_CharT> string_type;
2351
2352    string_type __weeks_[14];
2353    string_type __months_[24];
2354    string_type __am_pm_[2];
2355    string_type __c_;
2356    string_type __r_;
2357    string_type __x_;
2358    string_type __X_;
2359
2360    explicit __time_get_storage(const char* __nm);
2361    explicit __time_get_storage(const string& __nm);
2362
2363    _LIBCPP_INLINE_VISIBILITY ~__time_get_storage() {}
2364
2365    time_base::dateorder __do_date_order() const;
2366
2367private:
2368    void init(const ctype<_CharT>&);
2369    string_type __analyze(char __fmt, const ctype<_CharT>&);
2370};
2371
2372#define _LIBCPP_TIME_GET_STORAGE_EXPLICIT_INSTANTIATION(_CharT) \
2373template <> _LIBCPP_EXPORTED_FROM_ABI time_base::dateorder __time_get_storage<_CharT>::__do_date_order() const; \
2374template <> _LIBCPP_EXPORTED_FROM_ABI __time_get_storage<_CharT>::__time_get_storage(const char*); \
2375template <> _LIBCPP_EXPORTED_FROM_ABI __time_get_storage<_CharT>::__time_get_storage(const string&); \
2376template <> _LIBCPP_EXPORTED_FROM_ABI void __time_get_storage<_CharT>::init(const ctype<_CharT>&); \
2377template <> _LIBCPP_EXPORTED_FROM_ABI __time_get_storage<_CharT>::string_type __time_get_storage<_CharT>::__analyze(char, const ctype<_CharT>&); \
2378extern template _LIBCPP_EXPORTED_FROM_ABI time_base::dateorder __time_get_storage<_CharT>::__do_date_order() const; \
2379extern template _LIBCPP_EXPORTED_FROM_ABI __time_get_storage<_CharT>::__time_get_storage(const char*); \
2380extern template _LIBCPP_EXPORTED_FROM_ABI __time_get_storage<_CharT>::__time_get_storage(const string&); \
2381extern template _LIBCPP_EXPORTED_FROM_ABI void __time_get_storage<_CharT>::init(const ctype<_CharT>&); \
2382extern template _LIBCPP_EXPORTED_FROM_ABI __time_get_storage<_CharT>::string_type __time_get_storage<_CharT>::__analyze(char, const ctype<_CharT>&); \
2383/**/
2384
2385_LIBCPP_TIME_GET_STORAGE_EXPLICIT_INSTANTIATION(char)
2386#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
2387_LIBCPP_TIME_GET_STORAGE_EXPLICIT_INSTANTIATION(wchar_t)
2388#endif
2389#undef _LIBCPP_TIME_GET_STORAGE_EXPLICIT_INSTANTIATION
2390
2391template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
2392class _LIBCPP_TEMPLATE_VIS time_get_byname
2393    : public time_get<_CharT, _InputIterator>,
2394      private __time_get_storage<_CharT>
2395{
2396public:
2397    typedef time_base::dateorder    dateorder;
2398    typedef _InputIterator          iter_type;
2399    typedef _CharT                  char_type;
2400    typedef basic_string<char_type> string_type;
2401
2402    _LIBCPP_INLINE_VISIBILITY
2403    explicit time_get_byname(const char* __nm, size_t __refs = 0)
2404        : time_get<_CharT, _InputIterator>(__refs),
2405          __time_get_storage<_CharT>(__nm) {}
2406    _LIBCPP_INLINE_VISIBILITY
2407    explicit time_get_byname(const string& __nm, size_t __refs = 0)
2408        : time_get<_CharT, _InputIterator>(__refs),
2409          __time_get_storage<_CharT>(__nm) {}
2410
2411protected:
2412    _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~time_get_byname() override {}
2413
2414    _LIBCPP_HIDE_FROM_ABI_VIRTUAL dateorder do_date_order() const override {return this->__do_date_order();}
2415private:
2416    _LIBCPP_HIDE_FROM_ABI_VIRTUAL const string_type* __weeks() const override  {return this->__weeks_;}
2417    _LIBCPP_HIDE_FROM_ABI_VIRTUAL const string_type* __months() const override {return this->__months_;}
2418    _LIBCPP_HIDE_FROM_ABI_VIRTUAL const string_type* __am_pm() const override  {return this->__am_pm_;}
2419    _LIBCPP_HIDE_FROM_ABI_VIRTUAL const string_type& __c() const override      {return this->__c_;}
2420    _LIBCPP_HIDE_FROM_ABI_VIRTUAL const string_type& __r() const override      {return this->__r_;}
2421    _LIBCPP_HIDE_FROM_ABI_VIRTUAL const string_type& __x() const override      {return this->__x_;}
2422    _LIBCPP_HIDE_FROM_ABI_VIRTUAL const string_type& __X() const override      {return this->__X_;}
2423};
2424
2425extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_get_byname<char>;
2426#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
2427extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_get_byname<wchar_t>;
2428#endif
2429
2430class _LIBCPP_EXPORTED_FROM_ABI __time_put
2431{
2432    locale_t __loc_;
2433protected:
2434    _LIBCPP_INLINE_VISIBILITY __time_put() : __loc_(_LIBCPP_GET_C_LOCALE) {}
2435    __time_put(const char* __nm);
2436    __time_put(const string& __nm);
2437    ~__time_put();
2438    void __do_put(char* __nb, char*& __ne, const tm* __tm,
2439                  char __fmt, char __mod) const;
2440#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
2441    void __do_put(wchar_t* __wb, wchar_t*& __we, const tm* __tm,
2442                  char __fmt, char __mod) const;
2443#endif
2444};
2445
2446template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
2447class _LIBCPP_TEMPLATE_VIS time_put
2448    : public locale::facet,
2449      private __time_put
2450{
2451public:
2452    typedef _CharT char_type;
2453    typedef _OutputIterator iter_type;
2454
2455    _LIBCPP_INLINE_VISIBILITY
2456    explicit time_put(size_t __refs = 0)
2457        : locale::facet(__refs) {}
2458
2459    iter_type put(iter_type __s, ios_base& __iob, char_type __fl, const tm* __tm,
2460                  const char_type* __pb, const char_type* __pe) const;
2461
2462    _LIBCPP_INLINE_VISIBILITY
2463    iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
2464                  const tm* __tm, char __fmt, char __mod = 0) const
2465    {
2466        return do_put(__s, __iob, __fl, __tm, __fmt, __mod);
2467    }
2468
2469    static locale::id id;
2470
2471protected:
2472    _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~time_put() override {}
2473    virtual iter_type do_put(iter_type __s, ios_base&, char_type, const tm* __tm,
2474                             char __fmt, char __mod) const;
2475
2476    _LIBCPP_INLINE_VISIBILITY
2477    explicit time_put(const char* __nm, size_t __refs)
2478        : locale::facet(__refs),
2479          __time_put(__nm) {}
2480    _LIBCPP_INLINE_VISIBILITY
2481    explicit time_put(const string& __nm, size_t __refs)
2482        : locale::facet(__refs),
2483          __time_put(__nm) {}
2484};
2485
2486template <class _CharT, class _OutputIterator>
2487locale::id
2488time_put<_CharT, _OutputIterator>::id;
2489
2490template <class _CharT, class _OutputIterator>
2491_OutputIterator
2492time_put<_CharT, _OutputIterator>::put(iter_type __s, ios_base& __iob,
2493                                       char_type __fl, const tm* __tm,
2494                                       const char_type* __pb,
2495                                       const char_type* __pe) const
2496{
2497    const ctype<char_type>& __ct = std::use_facet<ctype<char_type> >(__iob.getloc());
2498    for (; __pb != __pe; ++__pb)
2499    {
2500        if (__ct.narrow(*__pb, 0) == '%')
2501        {
2502            if (++__pb == __pe)
2503            {
2504                *__s++ = __pb[-1];
2505                break;
2506            }
2507            char __mod = 0;
2508            char __fmt = __ct.narrow(*__pb, 0);
2509            if (__fmt == 'E' || __fmt == 'O')
2510            {
2511                if (++__pb == __pe)
2512                {
2513                    *__s++ = __pb[-2];
2514                    *__s++ = __pb[-1];
2515                    break;
2516                }
2517                __mod = __fmt;
2518                __fmt = __ct.narrow(*__pb, 0);
2519            }
2520            __s = do_put(__s, __iob, __fl, __tm, __fmt, __mod);
2521        }
2522        else
2523            *__s++ = *__pb;
2524    }
2525    return __s;
2526}
2527
2528template <class _CharT, class _OutputIterator>
2529_OutputIterator
2530time_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base&,
2531                                          char_type, const tm* __tm,
2532                                          char __fmt, char __mod) const
2533{
2534    char_type __nar[100];
2535    char_type* __nb = __nar;
2536    char_type* __ne = __nb + 100;
2537    __do_put(__nb, __ne, __tm, __fmt, __mod);
2538    return _VSTD::copy(__nb, __ne, __s);
2539}
2540
2541extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_put<char>;
2542#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
2543extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_put<wchar_t>;
2544#endif
2545
2546template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
2547class _LIBCPP_TEMPLATE_VIS time_put_byname
2548    : public time_put<_CharT, _OutputIterator>
2549{
2550public:
2551    _LIBCPP_INLINE_VISIBILITY
2552    explicit time_put_byname(const char* __nm, size_t __refs = 0)
2553        : time_put<_CharT, _OutputIterator>(__nm, __refs) {}
2554
2555    _LIBCPP_INLINE_VISIBILITY
2556    explicit time_put_byname(const string& __nm, size_t __refs = 0)
2557        : time_put<_CharT, _OutputIterator>(__nm, __refs) {}
2558
2559protected:
2560    _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~time_put_byname() override {}
2561};
2562
2563extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_put_byname<char>;
2564#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
2565extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_put_byname<wchar_t>;
2566#endif
2567
2568// money_base
2569
2570class _LIBCPP_EXPORTED_FROM_ABI money_base
2571{
2572public:
2573    enum part {none, space, symbol, sign, value};
2574    struct pattern {char field[4];};
2575
2576    _LIBCPP_INLINE_VISIBILITY money_base() {}
2577};
2578
2579// moneypunct
2580
2581template <class _CharT, bool _International = false>
2582class _LIBCPP_TEMPLATE_VIS moneypunct
2583    : public locale::facet,
2584      public money_base
2585{
2586public:
2587    typedef _CharT                  char_type;
2588    typedef basic_string<char_type> string_type;
2589
2590    _LIBCPP_INLINE_VISIBILITY
2591    explicit moneypunct(size_t __refs = 0)
2592        : locale::facet(__refs) {}
2593
2594    _LIBCPP_INLINE_VISIBILITY char_type   decimal_point() const {return do_decimal_point();}
2595    _LIBCPP_INLINE_VISIBILITY char_type   thousands_sep() const {return do_thousands_sep();}
2596    _LIBCPP_INLINE_VISIBILITY string      grouping()      const {return do_grouping();}
2597    _LIBCPP_INLINE_VISIBILITY string_type curr_symbol()   const {return do_curr_symbol();}
2598    _LIBCPP_INLINE_VISIBILITY string_type positive_sign() const {return do_positive_sign();}
2599    _LIBCPP_INLINE_VISIBILITY string_type negative_sign() const {return do_negative_sign();}
2600    _LIBCPP_INLINE_VISIBILITY int         frac_digits()   const {return do_frac_digits();}
2601    _LIBCPP_INLINE_VISIBILITY pattern     pos_format()    const {return do_pos_format();}
2602    _LIBCPP_INLINE_VISIBILITY pattern     neg_format()    const {return do_neg_format();}
2603
2604    static locale::id id;
2605    static const bool intl = _International;
2606
2607protected:
2608    _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~moneypunct() override {}
2609
2610    virtual char_type   do_decimal_point() const {return numeric_limits<char_type>::max();}
2611    virtual char_type   do_thousands_sep() const {return numeric_limits<char_type>::max();}
2612    virtual string      do_grouping()      const {return string();}
2613    virtual string_type do_curr_symbol()   const {return string_type();}
2614    virtual string_type do_positive_sign() const {return string_type();}
2615    virtual string_type do_negative_sign() const {return string_type(1, '-');}
2616    virtual int         do_frac_digits()   const {return 0;}
2617    virtual pattern     do_pos_format()    const
2618        {pattern __p = {{symbol, sign, none, value}}; return __p;}
2619    virtual pattern     do_neg_format()    const
2620        {pattern __p = {{symbol, sign, none, value}}; return __p;}
2621};
2622
2623template <class _CharT, bool _International>
2624locale::id
2625moneypunct<_CharT, _International>::id;
2626
2627template <class _CharT, bool _International>
2628const bool
2629moneypunct<_CharT, _International>::intl;
2630
2631extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct<char, false>;
2632extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct<char, true>;
2633#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
2634extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct<wchar_t, false>;
2635extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct<wchar_t, true>;
2636#endif
2637
2638// moneypunct_byname
2639
2640template <class _CharT, bool _International = false>
2641class _LIBCPP_TEMPLATE_VIS moneypunct_byname
2642    : public moneypunct<_CharT, _International>
2643{
2644public:
2645    typedef money_base::pattern  pattern;
2646    typedef _CharT                  char_type;
2647    typedef basic_string<char_type> string_type;
2648
2649    _LIBCPP_INLINE_VISIBILITY
2650    explicit moneypunct_byname(const char* __nm, size_t __refs = 0)
2651        : moneypunct<_CharT, _International>(__refs) {init(__nm);}
2652
2653    _LIBCPP_INLINE_VISIBILITY
2654    explicit moneypunct_byname(const string& __nm, size_t __refs = 0)
2655        : moneypunct<_CharT, _International>(__refs) {init(__nm.c_str());}
2656
2657protected:
2658    _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~moneypunct_byname() override {}
2659
2660     char_type   do_decimal_point() const override {return __decimal_point_;}
2661     char_type   do_thousands_sep() const override {return __thousands_sep_;}
2662     string      do_grouping()      const override {return __grouping_;}
2663     string_type do_curr_symbol()   const override {return __curr_symbol_;}
2664     string_type do_positive_sign() const override {return __positive_sign_;}
2665     string_type do_negative_sign() const override {return __negative_sign_;}
2666     int         do_frac_digits()   const override {return __frac_digits_;}
2667     pattern     do_pos_format()    const override {return __pos_format_;}
2668     pattern     do_neg_format()    const override {return __neg_format_;}
2669
2670private:
2671    char_type   __decimal_point_;
2672    char_type   __thousands_sep_;
2673    string      __grouping_;
2674    string_type __curr_symbol_;
2675    string_type __positive_sign_;
2676    string_type __negative_sign_;
2677    int         __frac_digits_;
2678    pattern     __pos_format_;
2679    pattern     __neg_format_;
2680
2681    void init(const char*);
2682};
2683
2684template<> _LIBCPP_EXPORTED_FROM_ABI void moneypunct_byname<char, false>::init(const char*);
2685template<> _LIBCPP_EXPORTED_FROM_ABI void moneypunct_byname<char, true>::init(const char*);
2686extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct_byname<char, false>;
2687extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct_byname<char, true>;
2688
2689#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
2690template<> _LIBCPP_EXPORTED_FROM_ABI void moneypunct_byname<wchar_t, false>::init(const char*);
2691template<> _LIBCPP_EXPORTED_FROM_ABI void moneypunct_byname<wchar_t, true>::init(const char*);
2692extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct_byname<wchar_t, false>;
2693extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct_byname<wchar_t, true>;
2694#endif
2695
2696// money_get
2697
2698template <class _CharT>
2699class __money_get
2700{
2701protected:
2702    typedef _CharT                  char_type;
2703    typedef basic_string<char_type> string_type;
2704
2705    _LIBCPP_INLINE_VISIBILITY __money_get() {}
2706
2707    static void __gather_info(bool __intl, const locale& __loc,
2708                              money_base::pattern& __pat, char_type& __dp,
2709                              char_type& __ts, string& __grp,
2710                              string_type& __sym, string_type& __psn,
2711                              string_type& __nsn, int& __fd);
2712};
2713
2714template <class _CharT>
2715void
2716__money_get<_CharT>::__gather_info(bool __intl, const locale& __loc,
2717                                   money_base::pattern& __pat, char_type& __dp,
2718                                   char_type& __ts, string& __grp,
2719                                   string_type& __sym, string_type& __psn,
2720                                   string_type& __nsn, int& __fd)
2721{
2722    if (__intl)
2723    {
2724        const moneypunct<char_type, true>& __mp =
2725            std::use_facet<moneypunct<char_type, true> >(__loc);
2726        __pat = __mp.neg_format();
2727        __nsn = __mp.negative_sign();
2728        __psn = __mp.positive_sign();
2729        __dp = __mp.decimal_point();
2730        __ts = __mp.thousands_sep();
2731        __grp = __mp.grouping();
2732        __sym = __mp.curr_symbol();
2733        __fd = __mp.frac_digits();
2734    }
2735    else
2736    {
2737        const moneypunct<char_type, false>& __mp =
2738            std::use_facet<moneypunct<char_type, false> >(__loc);
2739        __pat = __mp.neg_format();
2740        __nsn = __mp.negative_sign();
2741        __psn = __mp.positive_sign();
2742        __dp = __mp.decimal_point();
2743        __ts = __mp.thousands_sep();
2744        __grp = __mp.grouping();
2745        __sym = __mp.curr_symbol();
2746        __fd = __mp.frac_digits();
2747    }
2748}
2749
2750extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __money_get<char>;
2751#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
2752extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __money_get<wchar_t>;
2753#endif
2754
2755template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
2756class _LIBCPP_TEMPLATE_VIS money_get
2757    : public locale::facet,
2758      private __money_get<_CharT>
2759{
2760public:
2761    typedef _CharT                  char_type;
2762    typedef _InputIterator          iter_type;
2763    typedef basic_string<char_type> string_type;
2764
2765    _LIBCPP_INLINE_VISIBILITY
2766    explicit money_get(size_t __refs = 0)
2767        : locale::facet(__refs) {}
2768
2769    _LIBCPP_INLINE_VISIBILITY
2770    iter_type get(iter_type __b, iter_type __e, bool __intl, ios_base& __iob,
2771                  ios_base::iostate& __err, long double& __v) const
2772    {
2773        return do_get(__b, __e, __intl, __iob, __err, __v);
2774    }
2775
2776    _LIBCPP_INLINE_VISIBILITY
2777    iter_type get(iter_type __b, iter_type __e, bool __intl, ios_base& __iob,
2778                  ios_base::iostate& __err, string_type& __v) const
2779    {
2780        return do_get(__b, __e, __intl, __iob, __err, __v);
2781    }
2782
2783    static locale::id id;
2784
2785protected:
2786    _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~money_get() override {}
2787
2788    virtual iter_type do_get(iter_type __b, iter_type __e, bool __intl,
2789                             ios_base& __iob, ios_base::iostate& __err,
2790                             long double& __v) const;
2791    virtual iter_type do_get(iter_type __b, iter_type __e, bool __intl,
2792                             ios_base& __iob, ios_base::iostate& __err,
2793                             string_type& __v) const;
2794
2795private:
2796    static bool __do_get(iter_type& __b, iter_type __e,
2797                         bool __intl, const locale& __loc,
2798                         ios_base::fmtflags __flags, ios_base::iostate& __err,
2799                         bool& __neg, const ctype<char_type>& __ct,
2800                         unique_ptr<char_type, void(*)(void*)>& __wb,
2801                         char_type*& __wn, char_type* __we);
2802};
2803
2804template <class _CharT, class _InputIterator>
2805locale::id
2806money_get<_CharT, _InputIterator>::id;
2807
2808_LIBCPP_EXPORTED_FROM_ABI void __do_nothing(void*);
2809
2810template <class _Tp>
2811_LIBCPP_HIDE_FROM_ABI
2812void
2813__double_or_nothing(unique_ptr<_Tp, void(*)(void*)>& __b, _Tp*& __n, _Tp*& __e)
2814{
2815    bool __owns = __b.get_deleter() != __do_nothing;
2816    size_t __cur_cap = static_cast<size_t>(__e-__b.get()) * sizeof(_Tp);
2817    size_t __new_cap = __cur_cap < numeric_limits<size_t>::max() / 2 ?
2818                       2 * __cur_cap : numeric_limits<size_t>::max();
2819    if (__new_cap == 0)
2820        __new_cap = sizeof(_Tp);
2821    size_t __n_off = static_cast<size_t>(__n - __b.get());
2822    _Tp* __t = (_Tp*)std::realloc(__owns ? __b.get() : 0, __new_cap);
2823    if (__t == 0)
2824        __throw_bad_alloc();
2825    if (__owns)
2826        __b.release();
2827    __b = unique_ptr<_Tp, void(*)(void*)>(__t, free);
2828    __new_cap /= sizeof(_Tp);
2829    __n = __b.get() + __n_off;
2830    __e = __b.get() + __new_cap;
2831}
2832
2833// true == success
2834template <class _CharT, class _InputIterator>
2835bool
2836money_get<_CharT, _InputIterator>::__do_get(iter_type& __b, iter_type __e,
2837                                            bool __intl, const locale& __loc,
2838                                            ios_base::fmtflags __flags,
2839                                            ios_base::iostate& __err,
2840                                            bool& __neg,
2841                                            const ctype<char_type>& __ct,
2842                                            unique_ptr<char_type, void(*)(void*)>& __wb,
2843                                            char_type*& __wn, char_type* __we)
2844{
2845    if (__b == __e) {
2846        __err |= ios_base::failbit;
2847        return false;
2848    }
2849    const unsigned __bz = 100;
2850    unsigned __gbuf[__bz];
2851    unique_ptr<unsigned, void(*)(void*)> __gb(__gbuf, __do_nothing);
2852    unsigned* __gn = __gb.get();
2853    unsigned* __ge = __gn + __bz;
2854    money_base::pattern __pat;
2855    char_type __dp;
2856    char_type __ts;
2857    string __grp;
2858    string_type __sym;
2859    string_type __psn;
2860    string_type __nsn;
2861    // Capture the spaces read into money_base::{space,none} so they
2862    // can be compared to initial spaces in __sym.
2863    string_type __spaces;
2864    int __fd;
2865    __money_get<_CharT>::__gather_info(__intl, __loc, __pat, __dp, __ts, __grp,
2866                                       __sym, __psn, __nsn, __fd);
2867    const string_type* __trailing_sign = 0;
2868    __wn = __wb.get();
2869    for (unsigned __p = 0; __p < 4 && __b != __e; ++__p)
2870    {
2871        switch (__pat.field[__p])
2872        {
2873        case money_base::space:
2874            if (__p != 3)
2875            {
2876                if (__ct.is(ctype_base::space, *__b))
2877                    __spaces.push_back(*__b++);
2878                else
2879                {
2880                    __err |= ios_base::failbit;
2881                    return false;
2882                }
2883            }
2884            _LIBCPP_FALLTHROUGH();
2885        case money_base::none:
2886            if (__p != 3)
2887            {
2888                while (__b != __e && __ct.is(ctype_base::space, *__b))
2889                    __spaces.push_back(*__b++);
2890            }
2891            break;
2892        case money_base::sign:
2893            if (__psn.size() > 0 && *__b == __psn[0])
2894            {
2895                ++__b;
2896                __neg = false;
2897                if (__psn.size() > 1)
2898                  __trailing_sign = &__psn;
2899                break;
2900            }
2901            if (__nsn.size() > 0 && *__b == __nsn[0])
2902            {
2903                ++__b;
2904                __neg = true;
2905                if (__nsn.size() > 1)
2906                    __trailing_sign = &__nsn;
2907                break;
2908            }
2909            if (__psn.size() > 0 && __nsn.size() > 0)
2910            {   // sign is required
2911                __err |= ios_base::failbit;
2912                return false;
2913            }
2914            if (__psn.size() == 0 && __nsn.size() == 0)
2915                // locale has no way of specifying a sign. Use the initial value of __neg as a default
2916                break;
2917            __neg = (__nsn.size() == 0);
2918            break;
2919        case money_base::symbol:
2920            {
2921            bool __more_needed = __trailing_sign ||
2922                                 (__p < 2)       ||
2923                                 (__p == 2 && __pat.field[3] != static_cast<char>(money_base::none));
2924            bool __sb = (__flags & ios_base::showbase) != 0;
2925            if (__sb || __more_needed)
2926            {
2927                typename string_type::const_iterator __sym_space_end = __sym.begin();
2928                if (__p > 0 && (__pat.field[__p - 1] == money_base::none ||
2929                                __pat.field[__p - 1] == money_base::space)) {
2930                    // Match spaces we've already read against spaces at
2931                    // the beginning of __sym.
2932                    while (__sym_space_end != __sym.end() &&
2933                           __ct.is(ctype_base::space, *__sym_space_end))
2934                        ++__sym_space_end;
2935                    const size_t __num_spaces = __sym_space_end - __sym.begin();
2936                    if (__num_spaces > __spaces.size() ||
2937                        !std::equal(__spaces.end() - __num_spaces, __spaces.end(),
2938                               __sym.begin())) {
2939                        // No match. Put __sym_space_end back at the
2940                        // beginning of __sym, which will prevent a
2941                        // match in the next loop.
2942                        __sym_space_end = __sym.begin();
2943                    }
2944                }
2945                typename string_type::const_iterator __sym_curr_char = __sym_space_end;
2946                while (__sym_curr_char != __sym.end() && __b != __e &&
2947                       *__b == *__sym_curr_char) {
2948                    ++__b;
2949                    ++__sym_curr_char;
2950                }
2951                if (__sb && __sym_curr_char != __sym.end())
2952                {
2953                    __err |= ios_base::failbit;
2954                    return false;
2955                }
2956            }
2957            }
2958            break;
2959        case money_base::value:
2960            {
2961            unsigned __ng = 0;
2962            for (; __b != __e; ++__b)
2963            {
2964                char_type __c = *__b;
2965                if (__ct.is(ctype_base::digit, __c))
2966                {
2967                    if (__wn == __we)
2968                        std::__double_or_nothing(__wb, __wn, __we);
2969                    *__wn++ = __c;
2970                    ++__ng;
2971                }
2972                else if (__grp.size() > 0 && __ng > 0 && __c == __ts)
2973                {
2974                    if (__gn == __ge)
2975                        std::__double_or_nothing(__gb, __gn, __ge);
2976                    *__gn++ = __ng;
2977                    __ng = 0;
2978                }
2979                else
2980                    break;
2981            }
2982            if (__gb.get() != __gn && __ng > 0)
2983            {
2984                if (__gn == __ge)
2985                    std::__double_or_nothing(__gb, __gn, __ge);
2986                *__gn++ = __ng;
2987            }
2988            if (__fd > 0)
2989            {
2990                if (__b == __e || *__b != __dp)
2991                {
2992                    __err |= ios_base::failbit;
2993                    return false;
2994                }
2995                for (++__b; __fd > 0; --__fd, ++__b)
2996                {
2997                    if (__b == __e || !__ct.is(ctype_base::digit, *__b))
2998                    {
2999                        __err |= ios_base::failbit;
3000                        return false;
3001                    }
3002                    if (__wn == __we)
3003                        std::__double_or_nothing(__wb, __wn, __we);
3004                    *__wn++ = *__b;
3005                }
3006            }
3007            if (__wn == __wb.get())
3008            {
3009                __err |= ios_base::failbit;
3010                return false;
3011            }
3012            }
3013            break;
3014        }
3015    }
3016    if (__trailing_sign)
3017    {
3018        for (unsigned __i = 1; __i < __trailing_sign->size(); ++__i, ++__b)
3019        {
3020            if (__b == __e || *__b != (*__trailing_sign)[__i])
3021            {
3022                __err |= ios_base::failbit;
3023                return false;
3024            }
3025        }
3026    }
3027    if (__gb.get() != __gn)
3028    {
3029        ios_base::iostate __et = ios_base::goodbit;
3030        __check_grouping(__grp, __gb.get(), __gn, __et);
3031        if (__et)
3032        {
3033            __err |= ios_base::failbit;
3034            return false;
3035        }
3036    }
3037    return true;
3038}
3039
3040template <class _CharT, class _InputIterator>
3041_InputIterator
3042money_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
3043                                          bool __intl, ios_base& __iob,
3044                                          ios_base::iostate& __err,
3045                                          long double& __v) const
3046{
3047    const int __bz = 100;
3048    char_type __wbuf[__bz];
3049    unique_ptr<char_type, void(*)(void*)> __wb(__wbuf, __do_nothing);
3050    char_type* __wn;
3051    char_type* __we = __wbuf + __bz;
3052    locale __loc = __iob.getloc();
3053    const ctype<char_type>& __ct = std::use_facet<ctype<char_type> >(__loc);
3054    bool __neg = false;
3055    if (__do_get(__b, __e, __intl, __loc, __iob.flags(), __err, __neg, __ct,
3056                 __wb, __wn, __we))
3057    {
3058        const char __src[] = "0123456789";
3059        char_type __atoms[sizeof(__src)-1];
3060        __ct.widen(__src, __src + (sizeof(__src)-1), __atoms);
3061        char __nbuf[__bz];
3062        char* __nc = __nbuf;
3063        unique_ptr<char, void(*)(void*)> __h(nullptr, free);
3064        if (__wn - __wb.get() > __bz-2)
3065        {
3066            __h.reset((char*)malloc(static_cast<size_t>(__wn - __wb.get() + 2)));
3067            if (__h.get() == nullptr)
3068                __throw_bad_alloc();
3069            __nc = __h.get();
3070        }
3071        if (__neg)
3072            *__nc++ = '-';
3073        for (const char_type* __w = __wb.get(); __w < __wn; ++__w, ++__nc)
3074            *__nc = __src[std::find(__atoms, _VSTD::end(__atoms), *__w) - __atoms];
3075        *__nc = char();
3076        if (sscanf(__nbuf, "%Lf", &__v) != 1)
3077            __throw_runtime_error("money_get error");
3078    }
3079    if (__b == __e)
3080        __err |= ios_base::eofbit;
3081    return __b;
3082}
3083
3084template <class _CharT, class _InputIterator>
3085_InputIterator
3086money_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
3087                                          bool __intl, ios_base& __iob,
3088                                          ios_base::iostate& __err,
3089                                          string_type& __v) const
3090{
3091    const int __bz = 100;
3092    char_type __wbuf[__bz];
3093    unique_ptr<char_type, void(*)(void*)> __wb(__wbuf, __do_nothing);
3094    char_type* __wn;
3095    char_type* __we = __wbuf + __bz;
3096    locale __loc = __iob.getloc();
3097    const ctype<char_type>& __ct = std::use_facet<ctype<char_type> >(__loc);
3098    bool __neg = false;
3099    if (__do_get(__b, __e, __intl, __loc, __iob.flags(), __err, __neg, __ct,
3100                 __wb, __wn, __we))
3101    {
3102        __v.clear();
3103        if (__neg)
3104            __v.push_back(__ct.widen('-'));
3105        char_type __z = __ct.widen('0');
3106        char_type* __w;
3107        for (__w = __wb.get(); __w < __wn-1; ++__w)
3108            if (*__w != __z)
3109                break;
3110        __v.append(__w, __wn);
3111    }
3112    if (__b == __e)
3113        __err |= ios_base::eofbit;
3114    return __b;
3115}
3116
3117extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS money_get<char>;
3118#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
3119extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS money_get<wchar_t>;
3120#endif
3121
3122// money_put
3123
3124template <class _CharT>
3125class __money_put
3126{
3127protected:
3128    typedef _CharT                  char_type;
3129    typedef basic_string<char_type> string_type;
3130
3131    _LIBCPP_INLINE_VISIBILITY __money_put() {}
3132
3133    static void __gather_info(bool __intl, bool __neg, const locale& __loc,
3134                              money_base::pattern& __pat, char_type& __dp,
3135                              char_type& __ts, string& __grp,
3136                              string_type& __sym, string_type& __sn,
3137                              int& __fd);
3138    static void __format(char_type* __mb, char_type*& __mi, char_type*& __me,
3139                         ios_base::fmtflags __flags,
3140                         const char_type* __db, const char_type* __de,
3141                         const ctype<char_type>& __ct, bool __neg,
3142                         const money_base::pattern& __pat, char_type __dp,
3143                         char_type __ts, const string& __grp,
3144                         const string_type& __sym, const string_type& __sn,
3145                         int __fd);
3146};
3147
3148template <class _CharT>
3149void
3150__money_put<_CharT>::__gather_info(bool __intl, bool __neg, const locale& __loc,
3151                                   money_base::pattern& __pat, char_type& __dp,
3152                                   char_type& __ts, string& __grp,
3153                                   string_type& __sym, string_type& __sn,
3154                                   int& __fd)
3155{
3156    if (__intl)
3157    {
3158        const moneypunct<char_type, true>& __mp =
3159            std::use_facet<moneypunct<char_type, true> >(__loc);
3160        if (__neg)
3161        {
3162            __pat = __mp.neg_format();
3163            __sn = __mp.negative_sign();
3164        }
3165        else
3166        {
3167            __pat = __mp.pos_format();
3168            __sn = __mp.positive_sign();
3169        }
3170        __dp = __mp.decimal_point();
3171        __ts = __mp.thousands_sep();
3172        __grp = __mp.grouping();
3173        __sym = __mp.curr_symbol();
3174        __fd = __mp.frac_digits();
3175    }
3176    else
3177    {
3178        const moneypunct<char_type, false>& __mp =
3179            std::use_facet<moneypunct<char_type, false> >(__loc);
3180        if (__neg)
3181        {
3182            __pat = __mp.neg_format();
3183            __sn = __mp.negative_sign();
3184        }
3185        else
3186        {
3187            __pat = __mp.pos_format();
3188            __sn = __mp.positive_sign();
3189        }
3190        __dp = __mp.decimal_point();
3191        __ts = __mp.thousands_sep();
3192        __grp = __mp.grouping();
3193        __sym = __mp.curr_symbol();
3194        __fd = __mp.frac_digits();
3195    }
3196}
3197
3198template <class _CharT>
3199void
3200__money_put<_CharT>::__format(char_type* __mb, char_type*& __mi, char_type*& __me,
3201                              ios_base::fmtflags __flags,
3202                              const char_type* __db, const char_type* __de,
3203                              const ctype<char_type>& __ct, bool __neg,
3204                              const money_base::pattern& __pat, char_type __dp,
3205                              char_type __ts, const string& __grp,
3206                              const string_type& __sym, const string_type& __sn,
3207                              int __fd)
3208{
3209    __me = __mb;
3210    for (char __p : __pat.field)
3211    {
3212        switch (__p)
3213        {
3214        case money_base::none:
3215            __mi = __me;
3216            break;
3217        case money_base::space:
3218            __mi = __me;
3219            *__me++ = __ct.widen(' ');
3220            break;
3221        case money_base::sign:
3222            if (!__sn.empty())
3223                *__me++ = __sn[0];
3224            break;
3225        case money_base::symbol:
3226            if (!__sym.empty() && (__flags & ios_base::showbase))
3227                __me = _VSTD::copy(__sym.begin(), __sym.end(), __me);
3228            break;
3229        case money_base::value:
3230            {
3231            // remember start of value so we can reverse it
3232            char_type* __t = __me;
3233            // find beginning of digits
3234            if (__neg)
3235                ++__db;
3236            // find end of digits
3237            const char_type* __d;
3238            for (__d = __db; __d < __de; ++__d)
3239                if (!__ct.is(ctype_base::digit, *__d))
3240                    break;
3241            // print fractional part
3242            if (__fd > 0)
3243            {
3244                int __f;
3245                for (__f = __fd; __d > __db && __f > 0; --__f)
3246                    *__me++ = *--__d;
3247                char_type __z = __f > 0 ? __ct.widen('0') : char_type();
3248                for (; __f > 0; --__f)
3249                    *__me++ = __z;
3250                *__me++ = __dp;
3251            }
3252            // print units part
3253            if (__d == __db)
3254            {
3255                *__me++ = __ct.widen('0');
3256            }
3257            else
3258            {
3259                unsigned __ng = 0;
3260                unsigned __ig = 0;
3261                unsigned __gl = __grp.empty() ? numeric_limits<unsigned>::max()
3262                                              : static_cast<unsigned>(__grp[__ig]);
3263                while (__d != __db)
3264                {
3265                    if (__ng == __gl)
3266                    {
3267                        *__me++ = __ts;
3268                        __ng = 0;
3269                        if (++__ig < __grp.size())
3270                            __gl = __grp[__ig] == numeric_limits<char>::max() ?
3271                                        numeric_limits<unsigned>::max() :
3272                                        static_cast<unsigned>(__grp[__ig]);
3273                    }
3274                    *__me++ = *--__d;
3275                    ++__ng;
3276                }
3277            }
3278            // reverse it
3279            std::reverse(__t, __me);
3280            }
3281            break;
3282        }
3283    }
3284    // print rest of sign, if any
3285    if (__sn.size() > 1)
3286        __me = _VSTD::copy(__sn.begin()+1, __sn.end(), __me);
3287    // set alignment
3288    if ((__flags & ios_base::adjustfield) == ios_base::left)
3289        __mi = __me;
3290    else if ((__flags & ios_base::adjustfield) != ios_base::internal)
3291        __mi = __mb;
3292}
3293
3294extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __money_put<char>;
3295#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
3296extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __money_put<wchar_t>;
3297#endif
3298
3299template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
3300class _LIBCPP_TEMPLATE_VIS money_put
3301    : public locale::facet,
3302      private __money_put<_CharT>
3303{
3304public:
3305    typedef _CharT                  char_type;
3306    typedef _OutputIterator         iter_type;
3307    typedef basic_string<char_type> string_type;
3308
3309    _LIBCPP_INLINE_VISIBILITY
3310    explicit money_put(size_t __refs = 0)
3311        : locale::facet(__refs) {}
3312
3313    _LIBCPP_INLINE_VISIBILITY
3314    iter_type put(iter_type __s, bool __intl, ios_base& __iob, char_type __fl,
3315                  long double __units) const
3316    {
3317        return do_put(__s, __intl, __iob, __fl, __units);
3318    }
3319
3320    _LIBCPP_INLINE_VISIBILITY
3321    iter_type put(iter_type __s, bool __intl, ios_base& __iob, char_type __fl,
3322                  const string_type& __digits) const
3323    {
3324        return do_put(__s, __intl, __iob, __fl, __digits);
3325    }
3326
3327    static locale::id id;
3328
3329protected:
3330    _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~money_put() override {}
3331
3332    virtual iter_type do_put(iter_type __s, bool __intl, ios_base& __iob,
3333                             char_type __fl, long double __units) const;
3334    virtual iter_type do_put(iter_type __s, bool __intl, ios_base& __iob,
3335                             char_type __fl, const string_type& __digits) const;
3336};
3337
3338template <class _CharT, class _OutputIterator>
3339locale::id
3340money_put<_CharT, _OutputIterator>::id;
3341
3342template <class _CharT, class _OutputIterator>
3343_OutputIterator
3344money_put<_CharT, _OutputIterator>::do_put(iter_type __s, bool __intl,
3345                                           ios_base& __iob, char_type __fl,
3346                                           long double __units) const
3347{
3348    // convert to char
3349    const size_t __bs = 100;
3350    char __buf[__bs];
3351    char* __bb = __buf;
3352    char_type __digits[__bs];
3353    char_type* __db = __digits;
3354    int __n = snprintf(__bb, __bs, "%.0Lf", __units);
3355    unique_ptr<char, void(*)(void*)> __hn(nullptr, free);
3356    unique_ptr<char_type, void(*)(void*)> __hd(0, free);
3357    // secure memory for digit storage
3358    if (static_cast<size_t>(__n) > __bs-1)
3359    {
3360        __n = __libcpp_asprintf_l(&__bb, _LIBCPP_GET_C_LOCALE, "%.0Lf", __units);
3361        if (__n == -1)
3362            __throw_bad_alloc();
3363        __hn.reset(__bb);
3364        __hd.reset((char_type*)malloc(static_cast<size_t>(__n) * sizeof(char_type)));
3365        if (__hd == nullptr)
3366            __throw_bad_alloc();
3367        __db = __hd.get();
3368    }
3369    // gather info
3370    locale __loc = __iob.getloc();
3371    const ctype<char_type>& __ct = std::use_facet<ctype<char_type> >(__loc);
3372    __ct.widen(__bb, __bb + __n, __db);
3373    bool __neg = __n > 0 && __bb[0] == '-';
3374    money_base::pattern __pat;
3375    char_type __dp;
3376    char_type __ts;
3377    string __grp;
3378    string_type __sym;
3379    string_type __sn;
3380    int __fd;
3381    this->__gather_info(__intl, __neg, __loc, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3382    // secure memory for formatting
3383    char_type __mbuf[__bs];
3384    char_type* __mb = __mbuf;
3385    unique_ptr<char_type, void(*)(void*)> __hw(0, free);
3386    size_t __exn = __n > __fd ?
3387                   (static_cast<size_t>(__n) - static_cast<size_t>(__fd)) * 2 +
3388                    __sn.size() + __sym.size() + static_cast<size_t>(__fd) + 1
3389                 : __sn.size() + __sym.size() + static_cast<size_t>(__fd) + 2;
3390    if (__exn > __bs)
3391    {
3392        __hw.reset((char_type*)malloc(__exn * sizeof(char_type)));
3393        __mb = __hw.get();
3394        if (__mb == 0)
3395            __throw_bad_alloc();
3396    }
3397    // format
3398    char_type* __mi;
3399    char_type* __me;
3400    this->__format(__mb, __mi, __me, __iob.flags(),
3401                   __db, __db + __n, __ct,
3402                   __neg, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3403    return std::__pad_and_output(__s, __mb, __mi, __me, __iob, __fl);
3404}
3405
3406template <class _CharT, class _OutputIterator>
3407_OutputIterator
3408money_put<_CharT, _OutputIterator>::do_put(iter_type __s, bool __intl,
3409                                           ios_base& __iob, char_type __fl,
3410                                           const string_type& __digits) const
3411{
3412    // gather info
3413    locale __loc = __iob.getloc();
3414    const ctype<char_type>& __ct = std::use_facet<ctype<char_type> >(__loc);
3415    bool __neg = __digits.size() > 0 && __digits[0] == __ct.widen('-');
3416    money_base::pattern __pat;
3417    char_type __dp;
3418    char_type __ts;
3419    string __grp;
3420    string_type __sym;
3421    string_type __sn;
3422    int __fd;
3423    this->__gather_info(__intl, __neg, __loc, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3424    // secure memory for formatting
3425    char_type __mbuf[100];
3426    char_type* __mb = __mbuf;
3427    unique_ptr<char_type, void(*)(void*)> __h(0, free);
3428    size_t __exn = static_cast<int>(__digits.size()) > __fd ?
3429                   (__digits.size() - static_cast<size_t>(__fd)) * 2 +
3430                    __sn.size() + __sym.size() + static_cast<size_t>(__fd) + 1
3431                 : __sn.size() + __sym.size() + static_cast<size_t>(__fd) + 2;
3432    if (__exn > 100)
3433    {
3434        __h.reset((char_type*)malloc(__exn * sizeof(char_type)));
3435        __mb = __h.get();
3436        if (__mb == 0)
3437            __throw_bad_alloc();
3438    }
3439    // format
3440    char_type* __mi;
3441    char_type* __me;
3442    this->__format(__mb, __mi, __me, __iob.flags(),
3443                   __digits.data(), __digits.data() + __digits.size(), __ct,
3444                   __neg, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3445    return std::__pad_and_output(__s, __mb, __mi, __me, __iob, __fl);
3446}
3447
3448extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS money_put<char>;
3449#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
3450extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS money_put<wchar_t>;
3451#endif
3452
3453// messages
3454
3455class _LIBCPP_EXPORTED_FROM_ABI messages_base
3456{
3457public:
3458    typedef ptrdiff_t catalog;
3459
3460    _LIBCPP_INLINE_VISIBILITY messages_base() {}
3461};
3462
3463template <class _CharT>
3464class _LIBCPP_TEMPLATE_VIS messages
3465    : public locale::facet,
3466      public messages_base
3467{
3468public:
3469    typedef _CharT               char_type;
3470    typedef basic_string<_CharT> string_type;
3471
3472    _LIBCPP_INLINE_VISIBILITY
3473    explicit messages(size_t __refs = 0)
3474        : locale::facet(__refs) {}
3475
3476    _LIBCPP_INLINE_VISIBILITY
3477    catalog open(const basic_string<char>& __nm, const locale& __loc) const
3478    {
3479        return do_open(__nm, __loc);
3480    }
3481
3482    _LIBCPP_INLINE_VISIBILITY
3483    string_type get(catalog __c, int __set, int __msgid,
3484                    const string_type& __dflt) const
3485    {
3486        return do_get(__c, __set, __msgid, __dflt);
3487    }
3488
3489    _LIBCPP_INLINE_VISIBILITY
3490    void close(catalog __c) const
3491    {
3492        do_close(__c);
3493    }
3494
3495    static locale::id id;
3496
3497protected:
3498    _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~messages() override {}
3499
3500    virtual catalog do_open(const basic_string<char>&, const locale&) const;
3501    virtual string_type do_get(catalog, int __set, int __msgid,
3502                               const string_type& __dflt) const;
3503    virtual void do_close(catalog) const;
3504};
3505
3506template <class _CharT>
3507locale::id
3508messages<_CharT>::id;
3509
3510template <class _CharT>
3511typename messages<_CharT>::catalog
3512messages<_CharT>::do_open(const basic_string<char>& __nm, const locale&) const
3513{
3514#ifdef _LIBCPP_HAS_CATOPEN
3515    catalog __cat = (catalog)catopen(__nm.c_str(), NL_CAT_LOCALE);
3516    if (__cat != -1)
3517        __cat = static_cast<catalog>((static_cast<size_t>(__cat) >> 1));
3518    return __cat;
3519#else // !_LIBCPP_HAS_CATOPEN
3520    (void)__nm;
3521    return -1;
3522#endif // _LIBCPP_HAS_CATOPEN
3523}
3524
3525template <class _CharT>
3526typename messages<_CharT>::string_type
3527messages<_CharT>::do_get(catalog __c, int __set, int __msgid,
3528                         const string_type& __dflt) const
3529{
3530#ifdef _LIBCPP_HAS_CATOPEN
3531    string __ndflt;
3532    __narrow_to_utf8<sizeof(char_type)*__CHAR_BIT__>()(std::back_inserter(__ndflt),
3533                                                       __dflt.c_str(),
3534                                                       __dflt.c_str() + __dflt.size());
3535    if (__c != -1)
3536        __c <<= 1;
3537    nl_catd __cat = (nl_catd)__c;
3538    char* __n = catgets(__cat, __set, __msgid, __ndflt.c_str());
3539    string_type __w;
3540    __widen_from_utf8<sizeof(char_type)*__CHAR_BIT__>()(std::back_inserter(__w),
3541                                                        __n, __n + _VSTD::strlen(__n));
3542    return __w;
3543#else // !_LIBCPP_HAS_CATOPEN
3544    (void)__c;
3545    (void)__set;
3546    (void)__msgid;
3547    return __dflt;
3548#endif // _LIBCPP_HAS_CATOPEN
3549}
3550
3551template <class _CharT>
3552void
3553messages<_CharT>::do_close(catalog __c) const
3554{
3555#ifdef _LIBCPP_HAS_CATOPEN
3556    if (__c != -1)
3557        __c <<= 1;
3558    nl_catd __cat = (nl_catd)__c;
3559    catclose(__cat);
3560#else // !_LIBCPP_HAS_CATOPEN
3561    (void)__c;
3562#endif // _LIBCPP_HAS_CATOPEN
3563}
3564
3565extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS messages<char>;
3566#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
3567extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS messages<wchar_t>;
3568#endif
3569
3570template <class _CharT>
3571class _LIBCPP_TEMPLATE_VIS messages_byname
3572    : public messages<_CharT>
3573{
3574public:
3575    typedef messages_base::catalog catalog;
3576    typedef basic_string<_CharT> string_type;
3577
3578    _LIBCPP_INLINE_VISIBILITY
3579    explicit messages_byname(const char*, size_t __refs = 0)
3580        : messages<_CharT>(__refs) {}
3581
3582    _LIBCPP_INLINE_VISIBILITY
3583    explicit messages_byname(const string&, size_t __refs = 0)
3584        : messages<_CharT>(__refs) {}
3585
3586protected:
3587    _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~messages_byname() override {}
3588};
3589
3590extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS messages_byname<char>;
3591#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
3592extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS messages_byname<wchar_t>;
3593#endif
3594
3595template<class _Codecvt, class _Elem = wchar_t,
3596         class _WideAlloc = allocator<_Elem>,
3597         class _ByteAlloc = allocator<char> >
3598class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 wstring_convert
3599{
3600public:
3601    typedef basic_string<char, char_traits<char>, _ByteAlloc>   byte_string;
3602    typedef basic_string<_Elem, char_traits<_Elem>, _WideAlloc> wide_string;
3603    typedef typename _Codecvt::state_type                       state_type;
3604    typedef typename wide_string::traits_type::int_type         int_type;
3605
3606private:
3607    byte_string __byte_err_string_;
3608    wide_string __wide_err_string_;
3609    _Codecvt* __cvtptr_;
3610    state_type __cvtstate_;
3611    size_t __cvtcount_;
3612
3613    wstring_convert(const wstring_convert& __wc);
3614    wstring_convert& operator=(const wstring_convert& __wc);
3615public:
3616#ifndef _LIBCPP_CXX03_LANG
3617    _LIBCPP_INLINE_VISIBILITY
3618    wstring_convert() : wstring_convert(new _Codecvt) {}
3619    _LIBCPP_INLINE_VISIBILITY
3620    explicit wstring_convert(_Codecvt* __pcvt);
3621#else
3622    _LIBCPP_INLINE_VISIBILITY
3623    _LIBCPP_EXPLICIT_SINCE_CXX14
3624    wstring_convert(_Codecvt* __pcvt = new _Codecvt);
3625#endif
3626
3627    _LIBCPP_INLINE_VISIBILITY
3628    wstring_convert(_Codecvt* __pcvt, state_type __state);
3629    _LIBCPP_EXPLICIT_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI wstring_convert(const byte_string& __byte_err,
3630                    const wide_string& __wide_err = wide_string());
3631#ifndef _LIBCPP_CXX03_LANG
3632    _LIBCPP_INLINE_VISIBILITY
3633    wstring_convert(wstring_convert&& __wc);
3634#endif
3635    _LIBCPP_HIDE_FROM_ABI ~wstring_convert();
3636
3637    _LIBCPP_INLINE_VISIBILITY
3638    wide_string from_bytes(char __byte)
3639        {return from_bytes(&__byte, &__byte+1);}
3640    _LIBCPP_INLINE_VISIBILITY
3641    wide_string from_bytes(const char* __ptr)
3642        {return from_bytes(__ptr, __ptr + char_traits<char>::length(__ptr));}
3643    _LIBCPP_INLINE_VISIBILITY
3644    wide_string from_bytes(const byte_string& __str)
3645        {return from_bytes(__str.data(), __str.data() + __str.size());}
3646    _LIBCPP_HIDE_FROM_ABI wide_string from_bytes(const char* __first, const char* __last);
3647
3648    _LIBCPP_INLINE_VISIBILITY
3649    byte_string to_bytes(_Elem __wchar)
3650        {return to_bytes(&__wchar, &__wchar+1);}
3651    _LIBCPP_INLINE_VISIBILITY
3652    byte_string to_bytes(const _Elem* __wptr)
3653        {return to_bytes(__wptr, __wptr + char_traits<_Elem>::length(__wptr));}
3654    _LIBCPP_INLINE_VISIBILITY
3655    byte_string to_bytes(const wide_string& __wstr)
3656        {return to_bytes(__wstr.data(), __wstr.data() + __wstr.size());}
3657    _LIBCPP_HIDE_FROM_ABI byte_string to_bytes(const _Elem* __first, const _Elem* __last);
3658
3659    _LIBCPP_INLINE_VISIBILITY
3660    size_t converted() const _NOEXCEPT {return __cvtcount_;}
3661    _LIBCPP_INLINE_VISIBILITY
3662    state_type state() const {return __cvtstate_;}
3663};
3664
3665_LIBCPP_SUPPRESS_DEPRECATED_PUSH
3666template<class _Codecvt, class _Elem, class _WideAlloc, class _ByteAlloc>
3667inline
3668wstring_convert<_Codecvt, _Elem, _WideAlloc, _ByteAlloc>::
3669    wstring_convert(_Codecvt* __pcvt)
3670        : __cvtptr_(__pcvt), __cvtstate_(), __cvtcount_(0)
3671{
3672}
3673_LIBCPP_SUPPRESS_DEPRECATED_POP
3674
3675template<class _Codecvt, class _Elem, class _WideAlloc, class _ByteAlloc>
3676inline
3677wstring_convert<_Codecvt, _Elem, _WideAlloc, _ByteAlloc>::
3678    wstring_convert(_Codecvt* __pcvt, state_type __state)
3679        : __cvtptr_(__pcvt), __cvtstate_(__state), __cvtcount_(0)
3680{
3681}
3682
3683template<class _Codecvt, class _Elem, class _WideAlloc, class _ByteAlloc>
3684wstring_convert<_Codecvt, _Elem, _WideAlloc, _ByteAlloc>::
3685    wstring_convert(const byte_string& __byte_err, const wide_string& __wide_err)
3686        : __byte_err_string_(__byte_err), __wide_err_string_(__wide_err),
3687          __cvtstate_(), __cvtcount_(0)
3688{
3689    __cvtptr_ = new _Codecvt;
3690}
3691
3692#ifndef _LIBCPP_CXX03_LANG
3693
3694template<class _Codecvt, class _Elem, class _WideAlloc, class _ByteAlloc>
3695inline
3696wstring_convert<_Codecvt, _Elem, _WideAlloc, _ByteAlloc>::
3697    wstring_convert(wstring_convert&& __wc)
3698        : __byte_err_string_(_VSTD::move(__wc.__byte_err_string_)),
3699          __wide_err_string_(_VSTD::move(__wc.__wide_err_string_)),
3700          __cvtptr_(__wc.__cvtptr_),
3701          __cvtstate_(__wc.__cvtstate_), __cvtcount_(__wc.__cvtcount_)
3702{
3703    __wc.__cvtptr_ = nullptr;
3704}
3705
3706#endif // _LIBCPP_CXX03_LANG
3707
3708_LIBCPP_SUPPRESS_DEPRECATED_PUSH
3709template<class _Codecvt, class _Elem, class _WideAlloc, class _ByteAlloc>
3710wstring_convert<_Codecvt, _Elem, _WideAlloc, _ByteAlloc>::~wstring_convert()
3711{
3712    delete __cvtptr_;
3713}
3714
3715template<class _Codecvt, class _Elem, class _WideAlloc, class _ByteAlloc>
3716typename wstring_convert<_Codecvt, _Elem, _WideAlloc, _ByteAlloc>::wide_string
3717wstring_convert<_Codecvt, _Elem, _WideAlloc, _ByteAlloc>::
3718    from_bytes(const char* __frm, const char* __frm_end)
3719{
3720_LIBCPP_SUPPRESS_DEPRECATED_POP
3721    __cvtcount_ = 0;
3722    if (__cvtptr_ != nullptr)
3723    {
3724        wide_string __ws(2*(__frm_end - __frm), _Elem());
3725        if (__frm != __frm_end)
3726            __ws.resize(__ws.capacity());
3727        codecvt_base::result __r = codecvt_base::ok;
3728        state_type __st = __cvtstate_;
3729        if (__frm != __frm_end)
3730        {
3731            _Elem* __to = &__ws[0];
3732            _Elem* __to_end = __to + __ws.size();
3733            const char* __frm_nxt;
3734            do
3735            {
3736                _Elem* __to_nxt;
3737                __r = __cvtptr_->in(__st, __frm, __frm_end, __frm_nxt,
3738                                          __to, __to_end, __to_nxt);
3739                __cvtcount_ += __frm_nxt - __frm;
3740                if (__frm_nxt == __frm)
3741                {
3742                    __r = codecvt_base::error;
3743                }
3744                else if (__r == codecvt_base::noconv)
3745                {
3746                    __ws.resize(__to - &__ws[0]);
3747                    // This only gets executed if _Elem is char
3748                    __ws.append((const _Elem*)__frm, (const _Elem*)__frm_end);
3749                    __frm = __frm_nxt;
3750                    __r = codecvt_base::ok;
3751                }
3752                else if (__r == codecvt_base::ok)
3753                {
3754                    __ws.resize(__to_nxt - &__ws[0]);
3755                    __frm = __frm_nxt;
3756                }
3757                else if (__r == codecvt_base::partial)
3758                {
3759                    ptrdiff_t __s = __to_nxt - &__ws[0];
3760                    __ws.resize(2 * __s);
3761                    __to = &__ws[0] + __s;
3762                    __to_end = &__ws[0] + __ws.size();
3763                    __frm = __frm_nxt;
3764                }
3765            } while (__r == codecvt_base::partial && __frm_nxt < __frm_end);
3766        }
3767        if (__r == codecvt_base::ok)
3768            return __ws;
3769    }
3770
3771    if (__wide_err_string_.empty())
3772        __throw_range_error("wstring_convert: from_bytes error");
3773
3774    return __wide_err_string_;
3775}
3776
3777template<class _Codecvt, class _Elem, class _WideAlloc, class _ByteAlloc>
3778typename wstring_convert<_Codecvt, _Elem, _WideAlloc, _ByteAlloc>::byte_string
3779wstring_convert<_Codecvt, _Elem, _WideAlloc, _ByteAlloc>::
3780    to_bytes(const _Elem* __frm, const _Elem* __frm_end)
3781{
3782    __cvtcount_ = 0;
3783    if (__cvtptr_ != nullptr)
3784    {
3785        byte_string __bs(2*(__frm_end - __frm), char());
3786        if (__frm != __frm_end)
3787            __bs.resize(__bs.capacity());
3788        codecvt_base::result __r = codecvt_base::ok;
3789        state_type __st = __cvtstate_;
3790        if (__frm != __frm_end)
3791        {
3792            char* __to = &__bs[0];
3793            char* __to_end = __to + __bs.size();
3794            const _Elem* __frm_nxt;
3795            do
3796            {
3797                char* __to_nxt;
3798                __r = __cvtptr_->out(__st, __frm, __frm_end, __frm_nxt,
3799                                           __to, __to_end, __to_nxt);
3800                __cvtcount_ += __frm_nxt - __frm;
3801                if (__frm_nxt == __frm)
3802                {
3803                    __r = codecvt_base::error;
3804                }
3805                else if (__r == codecvt_base::noconv)
3806                {
3807                    __bs.resize(__to - &__bs[0]);
3808                    // This only gets executed if _Elem is char
3809                    __bs.append((const char*)__frm, (const char*)__frm_end);
3810                    __frm = __frm_nxt;
3811                    __r = codecvt_base::ok;
3812                }
3813                else if (__r == codecvt_base::ok)
3814                {
3815                    __bs.resize(__to_nxt - &__bs[0]);
3816                    __frm = __frm_nxt;
3817                }
3818                else if (__r == codecvt_base::partial)
3819                {
3820                    ptrdiff_t __s = __to_nxt - &__bs[0];
3821                    __bs.resize(2 * __s);
3822                    __to = &__bs[0] + __s;
3823                    __to_end = &__bs[0] + __bs.size();
3824                    __frm = __frm_nxt;
3825                }
3826            } while (__r == codecvt_base::partial && __frm_nxt < __frm_end);
3827        }
3828        if (__r == codecvt_base::ok)
3829        {
3830            size_t __s = __bs.size();
3831            __bs.resize(__bs.capacity());
3832            char* __to = &__bs[0] + __s;
3833            char* __to_end = __to + __bs.size();
3834            do
3835            {
3836                char* __to_nxt;
3837                __r = __cvtptr_->unshift(__st, __to, __to_end, __to_nxt);
3838                if (__r == codecvt_base::noconv)
3839                {
3840                    __bs.resize(__to - &__bs[0]);
3841                    __r = codecvt_base::ok;
3842                }
3843                else if (__r == codecvt_base::ok)
3844                {
3845                    __bs.resize(__to_nxt - &__bs[0]);
3846                }
3847                else if (__r == codecvt_base::partial)
3848                {
3849                    ptrdiff_t __sp = __to_nxt - &__bs[0];
3850                    __bs.resize(2 * __sp);
3851                    __to = &__bs[0] + __sp;
3852                    __to_end = &__bs[0] + __bs.size();
3853                }
3854            } while (__r == codecvt_base::partial);
3855            if (__r == codecvt_base::ok)
3856                return __bs;
3857        }
3858    }
3859
3860    if (__byte_err_string_.empty())
3861        __throw_range_error("wstring_convert: to_bytes error");
3862
3863    return __byte_err_string_;
3864}
3865
3866template <class _Codecvt, class _Elem = wchar_t, class _Tr = char_traits<_Elem> >
3867class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 wbuffer_convert
3868    : public basic_streambuf<_Elem, _Tr>
3869{
3870public:
3871    // types:
3872    typedef _Elem                          char_type;
3873    typedef _Tr                            traits_type;
3874    typedef typename traits_type::int_type int_type;
3875    typedef typename traits_type::pos_type pos_type;
3876    typedef typename traits_type::off_type off_type;
3877    typedef typename _Codecvt::state_type  state_type;
3878
3879private:
3880    char*       __extbuf_;
3881    const char* __extbufnext_;
3882    const char* __extbufend_;
3883    char __extbuf_min_[8];
3884    size_t __ebs_;
3885    char_type* __intbuf_;
3886    size_t __ibs_;
3887    streambuf* __bufptr_;
3888    _Codecvt* __cv_;
3889    state_type __st_;
3890    ios_base::openmode __cm_;
3891    bool __owns_eb_;
3892    bool __owns_ib_;
3893    bool __always_noconv_;
3894
3895    wbuffer_convert(const wbuffer_convert&);
3896    wbuffer_convert& operator=(const wbuffer_convert&);
3897
3898public:
3899#ifndef _LIBCPP_CXX03_LANG
3900    _LIBCPP_HIDE_FROM_ABI wbuffer_convert() : wbuffer_convert(nullptr) {}
3901    explicit _LIBCPP_HIDE_FROM_ABI wbuffer_convert(streambuf* __bytebuf,
3902                             _Codecvt* __pcvt = new _Codecvt,
3903                             state_type __state = state_type());
3904#else
3905    _LIBCPP_EXPLICIT_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI
3906    wbuffer_convert(streambuf* __bytebuf = nullptr,
3907                    _Codecvt* __pcvt = new _Codecvt,
3908                    state_type __state = state_type());
3909#endif
3910
3911    _LIBCPP_HIDE_FROM_ABI ~wbuffer_convert();
3912
3913    _LIBCPP_INLINE_VISIBILITY
3914    streambuf* rdbuf() const {return __bufptr_;}
3915    _LIBCPP_INLINE_VISIBILITY
3916    streambuf* rdbuf(streambuf* __bytebuf)
3917    {
3918        streambuf* __r = __bufptr_;
3919        __bufptr_ = __bytebuf;
3920        return __r;
3921    }
3922
3923    _LIBCPP_INLINE_VISIBILITY
3924    state_type state() const {return __st_;}
3925
3926protected:
3927    _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual int_type underflow();
3928    _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual int_type pbackfail(int_type __c = traits_type::eof());
3929    _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual int_type overflow (int_type __c = traits_type::eof());
3930    _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual basic_streambuf<char_type, traits_type>* setbuf(char_type* __s,
3931                                                            streamsize __n);
3932    _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual pos_type seekoff(off_type __off, ios_base::seekdir __way,
3933                             ios_base::openmode __wch = ios_base::in | ios_base::out);
3934    _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual pos_type seekpos(pos_type __sp,
3935                             ios_base::openmode __wch = ios_base::in | ios_base::out);
3936    _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual int sync();
3937
3938private:
3939    _LIBCPP_HIDE_FROM_ABI_VIRTUAL bool __read_mode();
3940    _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __write_mode();
3941    _LIBCPP_HIDE_FROM_ABI_VIRTUAL wbuffer_convert* __close();
3942};
3943
3944_LIBCPP_SUPPRESS_DEPRECATED_PUSH
3945template <class _Codecvt, class _Elem, class _Tr>
3946wbuffer_convert<_Codecvt, _Elem, _Tr>::
3947    wbuffer_convert(streambuf* __bytebuf, _Codecvt* __pcvt, state_type __state)
3948    : __extbuf_(nullptr),
3949      __extbufnext_(nullptr),
3950      __extbufend_(nullptr),
3951      __ebs_(0),
3952      __intbuf_(0),
3953      __ibs_(0),
3954      __bufptr_(__bytebuf),
3955      __cv_(__pcvt),
3956      __st_(__state),
3957      __cm_(0),
3958      __owns_eb_(false),
3959      __owns_ib_(false),
3960      __always_noconv_(__cv_ ? __cv_->always_noconv() : false)
3961{
3962    setbuf(0, 4096);
3963}
3964
3965template <class _Codecvt, class _Elem, class _Tr>
3966wbuffer_convert<_Codecvt, _Elem, _Tr>::~wbuffer_convert()
3967{
3968    __close();
3969    delete __cv_;
3970    if (__owns_eb_)
3971        delete [] __extbuf_;
3972    if (__owns_ib_)
3973        delete [] __intbuf_;
3974}
3975
3976template <class _Codecvt, class _Elem, class _Tr>
3977typename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type
3978wbuffer_convert<_Codecvt, _Elem, _Tr>::underflow()
3979{
3980_LIBCPP_SUPPRESS_DEPRECATED_POP
3981    if (__cv_ == 0 || __bufptr_ == 0)
3982        return traits_type::eof();
3983    bool __initial = __read_mode();
3984    char_type __1buf;
3985    if (this->gptr() == 0)
3986        this->setg(&__1buf, &__1buf+1, &__1buf+1);
3987    const size_t __unget_sz = __initial ? 0 : std::min<size_t>((this->egptr() - this->eback()) / 2, 4);
3988    int_type __c = traits_type::eof();
3989    if (this->gptr() == this->egptr())
3990    {
3991        _VSTD::memmove(this->eback(), this->egptr() - __unget_sz, __unget_sz * sizeof(char_type));
3992        if (__always_noconv_)
3993        {
3994            streamsize __nmemb = static_cast<streamsize>(this->egptr() - this->eback() - __unget_sz);
3995            __nmemb = __bufptr_->sgetn((char*)this->eback() + __unget_sz, __nmemb);
3996            if (__nmemb != 0)
3997            {
3998                this->setg(this->eback(),
3999                           this->eback() + __unget_sz,
4000                           this->eback() + __unget_sz + __nmemb);
4001                __c = *this->gptr();
4002            }
4003        }
4004        else
4005        {
4006             if (__extbufend_ != __extbufnext_) {
4007                _LIBCPP_ASSERT_UNCATEGORIZED(__extbufnext_ != nullptr, "underflow moving from nullptr");
4008                _LIBCPP_ASSERT_UNCATEGORIZED(__extbuf_ != nullptr, "underflow moving into nullptr");
4009                _VSTD::memmove(__extbuf_, __extbufnext_, __extbufend_ - __extbufnext_);
4010             }
4011            __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_);
4012            __extbufend_ = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_);
4013            streamsize __nmemb = _VSTD::min(static_cast<streamsize>(this->egptr() - this->eback() - __unget_sz),
4014                                 static_cast<streamsize>(__extbufend_ - __extbufnext_));
4015            codecvt_base::result __r;
4016            // FIXME: Do we ever need to restore the state here?
4017            //state_type __svs = __st_;
4018            streamsize __nr = __bufptr_->sgetn(const_cast<char*>(__extbufnext_), __nmemb);
4019            if (__nr != 0)
4020            {
4021                __extbufend_ = __extbufnext_ + __nr;
4022                char_type*  __inext;
4023                __r = __cv_->in(__st_, __extbuf_, __extbufend_, __extbufnext_,
4024                                       this->eback() + __unget_sz,
4025                                       this->egptr(), __inext);
4026                if (__r == codecvt_base::noconv)
4027                {
4028                    this->setg((char_type*)__extbuf_, (char_type*)__extbuf_,
4029                               (char_type*) const_cast<char *>(__extbufend_));
4030                    __c = *this->gptr();
4031                }
4032                else if (__inext != this->eback() + __unget_sz)
4033                {
4034                    this->setg(this->eback(), this->eback() + __unget_sz, __inext);
4035                    __c = *this->gptr();
4036                }
4037            }
4038        }
4039    }
4040    else
4041        __c = *this->gptr();
4042    if (this->eback() == &__1buf)
4043        this->setg(0, 0, 0);
4044    return __c;
4045}
4046
4047_LIBCPP_SUPPRESS_DEPRECATED_PUSH
4048template <class _Codecvt, class _Elem, class _Tr>
4049typename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type
4050wbuffer_convert<_Codecvt, _Elem, _Tr>::pbackfail(int_type __c)
4051{
4052_LIBCPP_SUPPRESS_DEPRECATED_POP
4053    if (__cv_ != 0 && __bufptr_ != 0 && this->eback() < this->gptr())
4054    {
4055        if (traits_type::eq_int_type(__c, traits_type::eof()))
4056        {
4057            this->gbump(-1);
4058            return traits_type::not_eof(__c);
4059        }
4060        if (traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]))
4061        {
4062            this->gbump(-1);
4063            *this->gptr() = traits_type::to_char_type(__c);
4064            return __c;
4065        }
4066    }
4067    return traits_type::eof();
4068}
4069
4070_LIBCPP_SUPPRESS_DEPRECATED_PUSH
4071template <class _Codecvt, class _Elem, class _Tr>
4072typename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type
4073wbuffer_convert<_Codecvt, _Elem, _Tr>::overflow(int_type __c)
4074{
4075_LIBCPP_SUPPRESS_DEPRECATED_POP
4076    if (__cv_ == 0 || __bufptr_ == 0)
4077        return traits_type::eof();
4078    __write_mode();
4079    char_type __1buf;
4080    char_type* __pb_save = this->pbase();
4081    char_type* __epb_save = this->epptr();
4082    if (!traits_type::eq_int_type(__c, traits_type::eof()))
4083    {
4084        if (this->pptr() == 0)
4085            this->setp(&__1buf, &__1buf+1);
4086        *this->pptr() = traits_type::to_char_type(__c);
4087        this->pbump(1);
4088    }
4089    if (this->pptr() != this->pbase())
4090    {
4091        if (__always_noconv_)
4092        {
4093            streamsize __nmemb = static_cast<streamsize>(this->pptr() - this->pbase());
4094            if (__bufptr_->sputn((const char*)this->pbase(), __nmemb) != __nmemb)
4095                return traits_type::eof();
4096        }
4097        else
4098        {
4099            char* __extbe = __extbuf_;
4100            codecvt_base::result __r;
4101            do
4102            {
4103                const char_type* __e;
4104                __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e,
4105                                        __extbuf_, __extbuf_ + __ebs_, __extbe);
4106                if (__e == this->pbase())
4107                    return traits_type::eof();
4108                if (__r == codecvt_base::noconv)
4109                {
4110                    streamsize __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
4111                    if (__bufptr_->sputn((const char*)this->pbase(), __nmemb) != __nmemb)
4112                        return traits_type::eof();
4113                }
4114                else if (__r == codecvt_base::ok || __r == codecvt_base::partial)
4115                {
4116                    streamsize __nmemb = static_cast<size_t>(__extbe - __extbuf_);
4117                    if (__bufptr_->sputn(__extbuf_, __nmemb) != __nmemb)
4118                        return traits_type::eof();
4119                    if (__r == codecvt_base::partial)
4120                    {
4121                        this->setp(const_cast<char_type *>(__e), this->pptr());
4122                        this->__pbump(this->epptr() - this->pbase());
4123                    }
4124                }
4125                else
4126                    return traits_type::eof();
4127            } while (__r == codecvt_base::partial);
4128        }
4129        this->setp(__pb_save, __epb_save);
4130    }
4131    return traits_type::not_eof(__c);
4132}
4133
4134_LIBCPP_SUPPRESS_DEPRECATED_PUSH
4135template <class _Codecvt, class _Elem, class _Tr>
4136basic_streambuf<_Elem, _Tr>*
4137wbuffer_convert<_Codecvt, _Elem, _Tr>::setbuf(char_type* __s, streamsize __n)
4138{
4139_LIBCPP_SUPPRESS_DEPRECATED_POP
4140    this->setg(0, 0, 0);
4141    this->setp(0, 0);
4142    if (__owns_eb_)
4143        delete [] __extbuf_;
4144    if (__owns_ib_)
4145        delete [] __intbuf_;
4146    __ebs_ = __n;
4147    if (__ebs_ > sizeof(__extbuf_min_))
4148    {
4149        if (__always_noconv_ && __s)
4150        {
4151            __extbuf_ = (char*)__s;
4152            __owns_eb_ = false;
4153        }
4154        else
4155        {
4156            __extbuf_ = new char[__ebs_];
4157            __owns_eb_ = true;
4158        }
4159    }
4160    else
4161    {
4162        __extbuf_ = __extbuf_min_;
4163        __ebs_ = sizeof(__extbuf_min_);
4164        __owns_eb_ = false;
4165    }
4166    if (!__always_noconv_)
4167    {
4168        __ibs_ = max<streamsize>(__n, sizeof(__extbuf_min_));
4169        if (__s && __ibs_ >= sizeof(__extbuf_min_))
4170        {
4171            __intbuf_ = __s;
4172            __owns_ib_ = false;
4173        }
4174        else
4175        {
4176            __intbuf_ = new char_type[__ibs_];
4177            __owns_ib_ = true;
4178        }
4179    }
4180    else
4181    {
4182        __ibs_ = 0;
4183        __intbuf_ = 0;
4184        __owns_ib_ = false;
4185    }
4186    return this;
4187}
4188
4189_LIBCPP_SUPPRESS_DEPRECATED_PUSH
4190template <class _Codecvt, class _Elem, class _Tr>
4191typename wbuffer_convert<_Codecvt, _Elem, _Tr>::pos_type
4192wbuffer_convert<_Codecvt, _Elem, _Tr>::seekoff(off_type __off, ios_base::seekdir __way,
4193                                        ios_base::openmode __om)
4194{
4195    int __width = __cv_->encoding();
4196    if (__cv_ == 0 || __bufptr_ == 0 || (__width <= 0 && __off != 0) || sync())
4197        return pos_type(off_type(-1));
4198    // __width > 0 || __off == 0, now check __way
4199    if (__way != ios_base::beg && __way != ios_base::cur && __way != ios_base::end)
4200        return pos_type(off_type(-1));
4201    pos_type __r = __bufptr_->pubseekoff(__width * __off, __way, __om);
4202    __r.state(__st_);
4203    return __r;
4204}
4205
4206template <class _Codecvt, class _Elem, class _Tr>
4207typename wbuffer_convert<_Codecvt, _Elem, _Tr>::pos_type
4208wbuffer_convert<_Codecvt, _Elem, _Tr>::seekpos(pos_type __sp, ios_base::openmode __wch)
4209{
4210    if (__cv_ == 0 || __bufptr_ == 0 || sync())
4211        return pos_type(off_type(-1));
4212    if (__bufptr_->pubseekpos(__sp, __wch) == pos_type(off_type(-1)))
4213        return pos_type(off_type(-1));
4214    return __sp;
4215}
4216
4217template <class _Codecvt, class _Elem, class _Tr>
4218int
4219wbuffer_convert<_Codecvt, _Elem, _Tr>::sync()
4220{
4221_LIBCPP_SUPPRESS_DEPRECATED_POP
4222    if (__cv_ == 0 || __bufptr_ == 0)
4223        return 0;
4224    if (__cm_ & ios_base::out)
4225    {
4226        if (this->pptr() != this->pbase())
4227            if (overflow() == traits_type::eof())
4228                return -1;
4229        codecvt_base::result __r;
4230        do
4231        {
4232            char* __extbe;
4233            __r = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);
4234            streamsize __nmemb = static_cast<streamsize>(__extbe - __extbuf_);
4235            if (__bufptr_->sputn(__extbuf_, __nmemb) != __nmemb)
4236                return -1;
4237        } while (__r == codecvt_base::partial);
4238        if (__r == codecvt_base::error)
4239            return -1;
4240        if (__bufptr_->pubsync())
4241            return -1;
4242    }
4243    else if (__cm_ & ios_base::in)
4244    {
4245        off_type __c;
4246        if (__always_noconv_)
4247            __c = this->egptr() - this->gptr();
4248        else
4249        {
4250            int __width = __cv_->encoding();
4251            __c = __extbufend_ - __extbufnext_;
4252            if (__width > 0)
4253                __c += __width * (this->egptr() - this->gptr());
4254            else
4255            {
4256                if (this->gptr() != this->egptr())
4257                {
4258                    std::reverse(this->gptr(), this->egptr());
4259                    codecvt_base::result __r;
4260                    const char_type* __e = this->gptr();
4261                    char* __extbe;
4262                    do
4263                    {
4264                        __r = __cv_->out(__st_, __e, this->egptr(), __e,
4265                                         __extbuf_, __extbuf_ + __ebs_, __extbe);
4266                        switch (__r)
4267                        {
4268                        case codecvt_base::noconv:
4269                            __c += this->egptr() - this->gptr();
4270                            break;
4271                        case codecvt_base::ok:
4272                        case codecvt_base::partial:
4273                            __c += __extbe - __extbuf_;
4274                            break;
4275                        default:
4276                            return -1;
4277                        }
4278                    } while (__r == codecvt_base::partial);
4279                }
4280            }
4281        }
4282        if (__bufptr_->pubseekoff(-__c, ios_base::cur, __cm_) == pos_type(off_type(-1)))
4283            return -1;
4284        this->setg(0, 0, 0);
4285        __cm_ = 0;
4286    }
4287    return 0;
4288}
4289
4290_LIBCPP_SUPPRESS_DEPRECATED_PUSH
4291template <class _Codecvt, class _Elem, class _Tr>
4292bool
4293wbuffer_convert<_Codecvt, _Elem, _Tr>::__read_mode()
4294{
4295    if (!(__cm_ & ios_base::in))
4296    {
4297        this->setp(0, 0);
4298        if (__always_noconv_)
4299            this->setg((char_type*)__extbuf_,
4300                       (char_type*)__extbuf_ + __ebs_,
4301                       (char_type*)__extbuf_ + __ebs_);
4302        else
4303            this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_);
4304        __cm_ = ios_base::in;
4305        return true;
4306    }
4307    return false;
4308}
4309
4310template <class _Codecvt, class _Elem, class _Tr>
4311void
4312wbuffer_convert<_Codecvt, _Elem, _Tr>::__write_mode()
4313{
4314    if (!(__cm_ & ios_base::out))
4315    {
4316        this->setg(0, 0, 0);
4317        if (__ebs_ > sizeof(__extbuf_min_))
4318        {
4319            if (__always_noconv_)
4320                this->setp((char_type*)__extbuf_,
4321                           (char_type*)__extbuf_ + (__ebs_ - 1));
4322            else
4323                this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1));
4324        }
4325        else
4326            this->setp(0, 0);
4327        __cm_ = ios_base::out;
4328    }
4329}
4330
4331template <class _Codecvt, class _Elem, class _Tr>
4332wbuffer_convert<_Codecvt, _Elem, _Tr>*
4333wbuffer_convert<_Codecvt, _Elem, _Tr>::__close()
4334{
4335    wbuffer_convert* __rt = nullptr;
4336    if (__cv_ != nullptr && __bufptr_ != nullptr)
4337    {
4338        __rt = this;
4339        if ((__cm_ & ios_base::out) && sync())
4340            __rt = nullptr;
4341    }
4342    return __rt;
4343}
4344
4345_LIBCPP_SUPPRESS_DEPRECATED_POP
4346
4347_LIBCPP_END_NAMESPACE_STD
4348
4349_LIBCPP_POP_MACROS
4350
4351// NOLINTEND(libcpp-robust-against-adl)
4352
4353#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
4354#  include <atomic>
4355#  include <concepts>
4356#  include <cstdarg>
4357#  include <iterator>
4358#  include <stdexcept>
4359#  include <type_traits>
4360#  include <typeinfo>
4361#endif
4362
4363#endif // _LIBCPP_LOCALE
4364