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