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