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