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