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