locale revision 249998
1227825Stheraven// -*- C++ -*-
2227825Stheraven//===-------------------------- locale ------------------------------------===//
3227825Stheraven//
4227825Stheraven//                     The LLVM Compiler Infrastructure
5227825Stheraven//
6227825Stheraven// This file is dual licensed under the MIT and the University of Illinois Open
7227825Stheraven// Source Licenses. See LICENSE.TXT for details.
8227825Stheraven//
9227825Stheraven//===----------------------------------------------------------------------===//
10227825Stheraven
11227825Stheraven#ifndef _LIBCPP_LOCALE
12227825Stheraven#define _LIBCPP_LOCALE
13227825Stheraven
14227825Stheraven/*
15227825Stheraven    locale synopsis
16227825Stheraven
17227825Stheravennamespace std
18227825Stheraven{
19227825Stheraven
20227825Stheravenclass locale
21227825Stheraven{
22227825Stheravenpublic:
23227825Stheraven    // types:
24227825Stheraven    class facet;
25227825Stheraven    class id;
26227825Stheraven
27227825Stheraven    typedef int category;
28227825Stheraven    static const category // values assigned here are for exposition only
29227825Stheraven        none     = 0x000,
30227825Stheraven        collate  = 0x010,
31227825Stheraven        ctype    = 0x020,
32227825Stheraven        monetary = 0x040,
33227825Stheraven        numeric  = 0x080,
34227825Stheraven        time     = 0x100,
35227825Stheraven        messages = 0x200,
36227825Stheraven        all = collate | ctype | monetary | numeric | time | messages;
37227825Stheraven
38227825Stheraven    // construct/copy/destroy:
39227825Stheraven    locale() noexcept;
40227825Stheraven    locale(const locale& other) noexcept;
41227825Stheraven    explicit locale(const char* std_name);
42227825Stheraven    explicit locale(const string& std_name);
43227825Stheraven    locale(const locale& other, const char* std_name, category);
44227825Stheraven    locale(const locale& other, const string& std_name, category);
45227825Stheraven    template <class Facet> locale(const locale& other, Facet* f);
46227825Stheraven    locale(const locale& other, const locale& one, category);
47227825Stheraven
48227825Stheraven    ~locale(); // not virtual
49227825Stheraven
50227825Stheraven    const locale& operator=(const locale& other) noexcept;
51227825Stheraven
52227825Stheraven    template <class Facet> locale combine(const locale& other) const;
53227825Stheraven
54227825Stheraven    // locale operations:
55227825Stheraven    basic_string<char> name() const;
56227825Stheraven    bool operator==(const locale& other) const;
57227825Stheraven    bool operator!=(const locale& other) const;
58227825Stheraven    template <class charT, class Traits, class Allocator>
59227825Stheraven      bool operator()(const basic_string<charT,Traits,Allocator>& s1,
60227825Stheraven                      const basic_string<charT,Traits,Allocator>& s2) const;
61227825Stheraven
62227825Stheraven    // global locale objects:
63227825Stheraven    static locale global(const locale&);
64227825Stheraven    static const locale& classic();
65227825Stheraven};
66227825Stheraven
67227825Stheraventemplate <class Facet> const Facet& use_facet(const locale&);
68227825Stheraventemplate <class Facet> bool has_facet(const locale&) noexcept;
69227825Stheraven
70227825Stheraven// 22.3.3, convenience interfaces:
71227825Stheraventemplate <class charT> bool isspace (charT c, const locale& loc);
72227825Stheraventemplate <class charT> bool isprint (charT c, const locale& loc);
73227825Stheraventemplate <class charT> bool iscntrl (charT c, const locale& loc);
74227825Stheraventemplate <class charT> bool isupper (charT c, const locale& loc);
75227825Stheraventemplate <class charT> bool islower (charT c, const locale& loc);
76227825Stheraventemplate <class charT> bool isalpha (charT c, const locale& loc);
77227825Stheraventemplate <class charT> bool isdigit (charT c, const locale& loc);
78227825Stheraventemplate <class charT> bool ispunct (charT c, const locale& loc);
79227825Stheraventemplate <class charT> bool isxdigit(charT c, const locale& loc);
80227825Stheraventemplate <class charT> bool isalnum (charT c, const locale& loc);
81227825Stheraventemplate <class charT> bool isgraph (charT c, const locale& loc);
82227825Stheraventemplate <class charT> charT toupper(charT c, const locale& loc);
83227825Stheraventemplate <class charT> charT tolower(charT c, const locale& loc);
84227825Stheraven
85227825Stheraventemplate<class Codecvt, class Elem = wchar_t,
86227825Stheraven         class Wide_alloc = allocator<Elem>,
87227825Stheraven         class Byte_alloc = allocator<char>>
88227825Stheravenclass wstring_convert
89227825Stheraven{
90227825Stheravenpublic:
91227825Stheraven    typedef basic_string<char, char_traits<char>, Byte_alloc> byte_string;
92227825Stheraven    typedef basic_string<Elem, char_traits<Elem>, Wide_alloc> wide_string;
93227825Stheraven    typedef typename Codecvt::state_type                      state_type;
94227825Stheraven    typedef typename wide_string::traits_type::int_type       int_type;
95227825Stheraven
96227825Stheraven    wstring_convert(Codecvt* pcvt = new Codecvt);
97227825Stheraven    wstring_convert(Codecvt* pcvt, state_type state);
98227825Stheraven    wstring_convert(const byte_string& byte_err,
99227825Stheraven                    const wide_string& wide_err = wide_string());
100227825Stheraven    ~wstring_convert();
101227825Stheraven
102227825Stheraven    wide_string from_bytes(char byte);
103227825Stheraven    wide_string from_bytes(const char* ptr);
104227825Stheraven    wide_string from_bytes(const byte_string& str);
105227825Stheraven    wide_string from_bytes(const char* first, const char* last);
106227825Stheraven
107227825Stheraven    byte_string to_bytes(Elem wchar);
108227825Stheraven    byte_string to_bytes(const Elem* wptr);
109227825Stheraven    byte_string to_bytes(const wide_string& wstr);
110227825Stheraven    byte_string to_bytes(const Elem* first, const Elem* last);
111227825Stheraven
112227825Stheraven    size_t converted() const;
113227825Stheraven    state_type state() const;
114227825Stheraven};
115227825Stheraven
116227825Stheraventemplate <class Codecvt, class Elem = wchar_t, class Tr = char_traits<Elem>>
117227825Stheravenclass wbuffer_convert
118227825Stheraven    : public basic_streambuf<Elem, Tr>
119227825Stheraven{
120227825Stheravenpublic:
121227825Stheraven    typedef typename Tr::state_type state_type;
122227825Stheraven
123227825Stheraven    wbuffer_convert(streambuf* bytebuf = 0, Codecvt* pcvt = new Codecvt,
124227825Stheraven                    state_type state = state_type());
125227825Stheraven
126227825Stheraven    streambuf* rdbuf() const;
127227825Stheraven    streambuf* rdbuf(streambuf* bytebuf);
128227825Stheraven
129227825Stheraven    state_type state() const;
130227825Stheraven};
131227825Stheraven
132227825Stheraven// 22.4.1 and 22.4.1.3, ctype:
133227825Stheravenclass ctype_base;
134227825Stheraventemplate <class charT> class ctype;
135227825Stheraventemplate <> class ctype<char>; // specialization
136227825Stheraventemplate <class charT> class ctype_byname;
137227825Stheraventemplate <> class ctype_byname<char>; // specialization
138227825Stheraven
139227825Stheravenclass codecvt_base;
140227825Stheraventemplate <class internT, class externT, class stateT> class codecvt;
141227825Stheraventemplate <class internT, class externT, class stateT> class codecvt_byname;
142227825Stheraven
143227825Stheraven// 22.4.2 and 22.4.3, numeric:
144227825Stheraventemplate <class charT, class InputIterator> class num_get;
145227825Stheraventemplate <class charT, class OutputIterator> class num_put;
146227825Stheraventemplate <class charT> class numpunct;
147227825Stheraventemplate <class charT> class numpunct_byname;
148227825Stheraven
149227825Stheraven// 22.4.4, col lation:
150227825Stheraventemplate <class charT> class collate;
151227825Stheraventemplate <class charT> class collate_byname;
152227825Stheraven
153227825Stheraven// 22.4.5, date and time:
154227825Stheravenclass time_base;
155227825Stheraventemplate <class charT, class InputIterator> class time_get;
156227825Stheraventemplate <class charT, class InputIterator> class time_get_byname;
157227825Stheraventemplate <class charT, class OutputIterator> class time_put;
158227825Stheraventemplate <class charT, class OutputIterator> class time_put_byname;
159227825Stheraven
160227825Stheraven// 22.4.6, money:
161227825Stheravenclass money_base;
162227825Stheraventemplate <class charT, class InputIterator> class money_get;
163227825Stheraventemplate <class charT, class OutputIterator> class money_put;
164227825Stheraventemplate <class charT, bool Intl> class moneypunct;
165227825Stheraventemplate <class charT, bool Intl> class moneypunct_byname;
166227825Stheraven
167227825Stheraven// 22.4.7, message retrieval:
168227825Stheravenclass messages_base;
169227825Stheraventemplate <class charT> class messages;
170227825Stheraventemplate <class charT> class messages_byname;
171227825Stheraven
172227825Stheraven}  // std
173227825Stheraven
174227825Stheraven*/
175227825Stheraven
176227825Stheraven#include <__config>
177227825Stheraven#include <__locale>
178227825Stheraven#include <algorithm>
179227825Stheraven#include <memory>
180227825Stheraven#include <ios>
181227825Stheraven#include <streambuf>
182227825Stheraven#include <iterator>
183227825Stheraven#include <limits>
184249998Sdim#ifndef __APPLE__
185227825Stheraven#include <cstdarg>
186227825Stheraven#endif
187227825Stheraven#include <cstdlib>
188227825Stheraven#include <ctime>
189249998Sdim#ifdef _WIN32
190227825Stheraven#include <support/win32/locale_win32.h>
191227825Stheraven#else // _WIN32
192227825Stheraven#include <nl_types.h>
193227825Stheraven#endif  // !_WIN32
194227825Stheraven
195249998Sdim#ifdef __APPLE__
196243673Stheraven#include <Availability.h>
197243673Stheraven#endif
198243673Stheraven
199232950Stheraven#include <__undef_min_max>
200232950Stheraven
201227825Stheraven#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
202227825Stheraven#pragma GCC system_header
203227825Stheraven#endif
204227825Stheraven
205227825Stheraven_LIBCPP_BEGIN_NAMESPACE_STD
206227825Stheraven
207249998Sdim#if defined(__APPLE__) || defined(__FreeBSD__)
208227825Stheraven#  define _LIBCPP_GET_C_LOCALE 0
209227825Stheraven#else
210227825Stheraven#  define _LIBCPP_GET_C_LOCALE __cloc()
211227825Stheraven   // Get the C locale object
212227825Stheraven   locale_t __cloc();
213227825Stheraven#define __cloc_defined
214227825Stheraven#endif
215227825Stheraven
216227825Stheraventypedef _VSTD::remove_pointer<locale_t>::type __locale_struct;
217227825Stheraventypedef _VSTD::unique_ptr<__locale_struct, decltype(&freelocale)> __locale_unique_ptr;
218232950Stheraven#ifndef _LIBCPP_LOCALE__L_EXTENSIONS
219227825Stheraventypedef _VSTD::unique_ptr<__locale_struct, decltype(&uselocale)> __locale_raii;
220232950Stheraven#endif
221227825Stheraven
222227825Stheraven// OSX has nice foo_l() functions that let you turn off use of the global
223227825Stheraven// locale.  Linux, not so much.  The following functions avoid the locale when
224227825Stheraven// that's possible and otherwise do the wrong thing.  FIXME.
225249998Sdim#if defined(__linux__) || defined(EMSCRIPTEN)
226227825Stheraven
227227825Stheraven#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
228227825Stheravendecltype(MB_CUR_MAX_L(_VSTD::declval<locale_t>()))
229227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
230227825Stheraven__mb_cur_max_l(locale_t __l)
231227825Stheraven{
232227825Stheraven  return MB_CUR_MAX_L(__l);
233227825Stheraven}
234227825Stheraven#else  // _LIBCPP_LOCALE__L_EXTENSIONS
235227825Stheraven_LIBCPP_ALWAYS_INLINE inline
236227825Stheravendecltype(MB_CUR_MAX) __mb_cur_max_l(locale_t __l)
237227825Stheraven{
238227825Stheraven  __locale_raii __current(uselocale(__l), uselocale);
239227825Stheraven  return MB_CUR_MAX;
240227825Stheraven}
241227825Stheraven#endif // _LIBCPP_LOCALE__L_EXTENSIONS
242227825Stheraven
243227825Stheraven_LIBCPP_ALWAYS_INLINE inline
244227825Stheravenwint_t __btowc_l(int __c, locale_t __l)
245227825Stheraven{
246227825Stheraven#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
247227825Stheraven  return btowc_l(__c, __l);
248227825Stheraven#else
249227825Stheraven  __locale_raii __current(uselocale(__l), uselocale);
250227825Stheraven  return btowc(__c);
251227825Stheraven#endif
252227825Stheraven}
253227825Stheraven
254227825Stheraven_LIBCPP_ALWAYS_INLINE inline
255227825Stheravenint __wctob_l(wint_t __c, locale_t __l)
256227825Stheraven{
257227825Stheraven#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
258227825Stheraven  return wctob_l(__c, __l);
259227825Stheraven#else
260227825Stheraven  __locale_raii __current(uselocale(__l), uselocale);
261227825Stheraven  return wctob(__c);
262227825Stheraven#endif
263227825Stheraven}
264227825Stheraven
265227825Stheraven_LIBCPP_ALWAYS_INLINE inline
266227825Stheravensize_t __wcsnrtombs_l(char *__dest, const wchar_t **__src, size_t __nwc,
267227825Stheraven                      size_t __len, mbstate_t *__ps, locale_t __l)
268227825Stheraven{
269227825Stheraven#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
270227825Stheraven  return wcsnrtombs_l(__dest, __src, __nwc, __len, __ps, __l);
271227825Stheraven#else
272227825Stheraven  __locale_raii __current(uselocale(__l), uselocale);
273227825Stheraven  return wcsnrtombs(__dest, __src, __nwc, __len, __ps);
274227825Stheraven#endif
275227825Stheraven}
276227825Stheraven
277227825Stheraven_LIBCPP_ALWAYS_INLINE inline
278227825Stheravensize_t __wcrtomb_l(char *__s, wchar_t __wc, mbstate_t *__ps, locale_t __l)
279227825Stheraven{
280227825Stheraven#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
281227825Stheraven  return wcrtomb_l(__s, __wc, __ps, __l);
282227825Stheraven#else
283227825Stheraven  __locale_raii __current(uselocale(__l), uselocale);
284227825Stheraven  return wcrtomb(__s, __wc, __ps);
285227825Stheraven#endif
286227825Stheraven}
287227825Stheraven
288227825Stheraven_LIBCPP_ALWAYS_INLINE inline
289227825Stheravensize_t __mbsnrtowcs_l(wchar_t * __dest, const char **__src, size_t __nms,
290227825Stheraven                      size_t __len, mbstate_t *__ps, locale_t __l)
291227825Stheraven{
292227825Stheraven#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
293227825Stheraven  return mbsnrtowcs_l(__dest, __src, __nms, __len, __ps, __l);
294227825Stheraven#else
295227825Stheraven  __locale_raii __current(uselocale(__l), uselocale);
296227825Stheraven  return mbsnrtowcs(__dest, __src, __nms, __len, __ps);
297227825Stheraven#endif
298227825Stheraven}
299227825Stheraven
300227825Stheraven_LIBCPP_ALWAYS_INLINE inline
301227825Stheravensize_t __mbrtowc_l(wchar_t *__pwc, const char *__s, size_t __n,
302227825Stheraven                   mbstate_t *__ps, locale_t __l)
303227825Stheraven{
304227825Stheraven#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
305227825Stheraven  return mbrtowc_l(__pwc, __s, __n, __ps, __l);
306227825Stheraven#else
307227825Stheraven  __locale_raii __current(uselocale(__l), uselocale);
308227825Stheraven  return mbrtowc(__pwc, __s, __n, __ps);
309227825Stheraven#endif
310227825Stheraven}
311227825Stheraven
312227825Stheraven_LIBCPP_ALWAYS_INLINE inline
313227825Stheravenint __mbtowc_l(wchar_t *__pwc, const char *__pmb, size_t __max, locale_t __l)
314227825Stheraven{
315227825Stheraven#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
316227825Stheraven  return mbtowc_l(__pwc, __pmb, __max, __l);
317227825Stheraven#else
318227825Stheraven  __locale_raii __current(uselocale(__l), uselocale);
319227825Stheraven  return mbtowc(__pwc, __pmb, __max);
320227825Stheraven#endif
321227825Stheraven}
322227825Stheraven
323227825Stheraven_LIBCPP_ALWAYS_INLINE inline
324227825Stheravensize_t __mbrlen_l(const char *__s, size_t __n, mbstate_t *__ps, locale_t __l)
325227825Stheraven{
326227825Stheraven#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
327227825Stheraven  return mbrlen_l(__s, __n, __ps, __l);
328227825Stheraven#else
329227825Stheraven  __locale_raii __current(uselocale(__l), uselocale);
330227825Stheraven  return mbrlen(__s, __n, __ps);
331227825Stheraven#endif
332227825Stheraven}
333227825Stheraven
334227825Stheraven_LIBCPP_ALWAYS_INLINE inline
335227825Stheravenlconv *__localeconv_l(locale_t __l)
336227825Stheraven{
337227825Stheraven#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
338227825Stheraven  return localeconv_l(__l);
339227825Stheraven#else
340227825Stheraven  __locale_raii __current(uselocale(__l), uselocale);
341227825Stheraven  return localeconv();
342227825Stheraven#endif
343227825Stheraven}
344227825Stheraven
345227825Stheraven_LIBCPP_ALWAYS_INLINE inline
346227825Stheravensize_t __mbsrtowcs_l(wchar_t *__dest, const char **__src, size_t __len,
347227825Stheraven                     mbstate_t *__ps, locale_t __l)
348227825Stheraven{
349227825Stheraven#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
350227825Stheraven  return mbsrtowcs_l(__dest, __src, __len, __ps, __l);
351227825Stheraven#else
352227825Stheraven  __locale_raii __current(uselocale(__l), uselocale);
353227825Stheraven  return mbsrtowcs(__dest, __src, __len, __ps);
354227825Stheraven#endif
355227825Stheraven}
356227825Stheraven
357246487Stheraveninline
358227825Stheravenint __sprintf_l(char *__s, locale_t __l, const char *__format, ...) {
359227825Stheraven  va_list __va;
360227825Stheraven  va_start(__va, __format);
361227825Stheraven#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
362227825Stheraven  int __res = vsprintf_l(__s, __l, __format, __va);
363227825Stheraven#else
364227825Stheraven  __locale_raii __current(uselocale(__l), uselocale);
365227825Stheraven  int __res = vsprintf(__s, __format, __va);
366227825Stheraven#endif
367227825Stheraven  va_end(__va);
368227825Stheraven  return __res;
369227825Stheraven}
370227825Stheraven
371246487Stheraveninline
372227825Stheravenint __snprintf_l(char *__s, size_t __n, locale_t __l, const char *__format, ...) {
373227825Stheraven  va_list __va;
374227825Stheraven  va_start(__va, __format);
375227825Stheraven#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
376227825Stheraven  int __res = vsnprintf_l(__s, __n, __l, __format, __va);
377227825Stheraven#else
378227825Stheraven  __locale_raii __current(uselocale(__l), uselocale);
379227825Stheraven  int __res = vsnprintf(__s, __n, __format, __va);
380227825Stheraven#endif
381227825Stheraven  va_end(__va);
382227825Stheraven  return __res;
383227825Stheraven}
384227825Stheraven
385246487Stheraveninline
386227825Stheravenint __asprintf_l(char **__s, locale_t __l, const char *__format, ...) {
387227825Stheraven  va_list __va;
388227825Stheraven  va_start(__va, __format);
389227825Stheraven#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
390227825Stheraven  int __res = vasprintf_l(__s, __l, __format, __va);
391227825Stheraven#else
392227825Stheraven  __locale_raii __current(uselocale(__l), uselocale);
393227825Stheraven  int __res = vasprintf(__s, __format, __va);
394227825Stheraven#endif
395227825Stheraven  va_end(__va);
396227825Stheraven  return __res;
397227825Stheraven}
398227825Stheraven
399246487Stheraveninline
400227825Stheravenint __sscanf_l(const char *__s, locale_t __l, const char *__format, ...) {
401227825Stheraven  va_list __va;
402227825Stheraven  va_start(__va, __format);
403227825Stheraven#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
404227825Stheraven  int __res = vsscanf_l(__s, __l, __format, __va);
405227825Stheraven#else
406227825Stheraven  __locale_raii __current(uselocale(__l), uselocale);
407227825Stheraven  int __res = vsscanf(__s, __format, __va);
408227825Stheraven#endif
409227825Stheraven  va_end(__va);
410227825Stheraven  return __res;
411227825Stheraven}
412227825Stheraven
413227825Stheraven#endif  // __linux__
414227825Stheraven
415227825Stheraven// __scan_keyword
416227825Stheraven// Scans [__b, __e) until a match is found in the basic_strings range
417227825Stheraven//  [__kb, __ke) or until it can be shown that there is no match in [__kb, __ke).
418227825Stheraven//  __b will be incremented (visibly), consuming CharT until a match is found
419227825Stheraven//  or proved to not exist.  A keyword may be "", in which will match anything.
420227825Stheraven//  If one keyword is a prefix of another, and the next CharT in the input
421227825Stheraven//  might match another keyword, the algorithm will attempt to find the longest
422227825Stheraven//  matching keyword.  If the longer matching keyword ends up not matching, then
423227825Stheraven//  no keyword match is found.  If no keyword match is found, __ke is returned
424227825Stheraven//  and failbit is set in __err.
425227825Stheraven//  Else an iterator pointing to the matching keyword is found.  If more than
426227825Stheraven//  one keyword matches, an iterator to the first matching keyword is returned.
427227825Stheraven//  If on exit __b == __e, eofbit is set in __err.  If __case_senstive is false,
428227825Stheraven//  __ct is used to force to lower case before comparing characters.
429227825Stheraven//  Examples:
430227825Stheraven//  Keywords:  "a", "abb"
431227825Stheraven//  If the input is "a", the first keyword matches and eofbit is set.
432227825Stheraven//  If the input is "abc", no match is found and "ab" are consumed.
433227825Stheraventemplate <class _InputIterator, class _ForwardIterator, class _Ctype>
434227825Stheraven_LIBCPP_HIDDEN
435227825Stheraven_ForwardIterator
436227825Stheraven__scan_keyword(_InputIterator& __b, _InputIterator __e,
437227825Stheraven               _ForwardIterator __kb, _ForwardIterator __ke,
438227825Stheraven               const _Ctype& __ct, ios_base::iostate& __err,
439227825Stheraven               bool __case_sensitive = true)
440227825Stheraven{
441227825Stheraven    typedef typename iterator_traits<_InputIterator>::value_type _CharT;
442232950Stheraven    size_t __nkw = static_cast<size_t>(_VSTD::distance(__kb, __ke));
443227825Stheraven    const unsigned char __doesnt_match = '\0';
444227825Stheraven    const unsigned char __might_match = '\1';
445227825Stheraven    const unsigned char __does_match = '\2';
446227825Stheraven    unsigned char __statbuf[100];
447227825Stheraven    unsigned char* __status = __statbuf;
448227825Stheraven    unique_ptr<unsigned char, void(*)(void*)> __stat_hold(0, free);
449227825Stheraven    if (__nkw > sizeof(__statbuf))
450227825Stheraven    {
451227825Stheraven        __status = (unsigned char*)malloc(__nkw);
452227825Stheraven        if (__status == 0)
453227825Stheraven            __throw_bad_alloc();
454227825Stheraven        __stat_hold.reset(__status);
455227825Stheraven    }
456227825Stheraven    size_t __n_might_match = __nkw;  // At this point, any keyword might match
457227825Stheraven    size_t __n_does_match = 0;       // but none of them definitely do
458227825Stheraven    // Initialize all statuses to __might_match, except for "" keywords are __does_match
459227825Stheraven    unsigned char* __st = __status;
460227825Stheraven    for (_ForwardIterator __ky = __kb; __ky != __ke; ++__ky, ++__st)
461227825Stheraven    {
462227825Stheraven        if (!__ky->empty())
463227825Stheraven            *__st = __might_match;
464227825Stheraven        else
465227825Stheraven        {
466227825Stheraven            *__st = __does_match;
467227825Stheraven            --__n_might_match;
468227825Stheraven            ++__n_does_match;
469227825Stheraven        }
470227825Stheraven    }
471227825Stheraven    // While there might be a match, test keywords against the next CharT
472227825Stheraven    for (size_t __indx = 0; __b != __e && __n_might_match > 0; ++__indx)
473227825Stheraven    {
474227825Stheraven        // Peek at the next CharT but don't consume it
475227825Stheraven        _CharT __c = *__b;
476227825Stheraven        if (!__case_sensitive)
477227825Stheraven            __c = __ct.toupper(__c);
478227825Stheraven        bool __consume = false;
479227825Stheraven        // For each keyword which might match, see if the __indx character is __c
480227825Stheraven        // If a match if found, consume __c
481227825Stheraven        // If a match is found, and that is the last character in the keyword,
482227825Stheraven        //    then that keyword matches.
483227825Stheraven        // If the keyword doesn't match this character, then change the keyword
484227825Stheraven        //    to doesn't match
485227825Stheraven        __st = __status;
486227825Stheraven        for (_ForwardIterator __ky = __kb; __ky != __ke; ++__ky, ++__st)
487227825Stheraven        {
488227825Stheraven            if (*__st == __might_match)
489227825Stheraven            {
490227825Stheraven                _CharT __kc = (*__ky)[__indx];
491227825Stheraven                if (!__case_sensitive)
492227825Stheraven                    __kc = __ct.toupper(__kc);
493227825Stheraven                if (__c == __kc)
494227825Stheraven                {
495227825Stheraven                    __consume = true;
496227825Stheraven                    if (__ky->size() == __indx+1)
497227825Stheraven                    {
498227825Stheraven                        *__st = __does_match;
499227825Stheraven                        --__n_might_match;
500227825Stheraven                        ++__n_does_match;
501227825Stheraven                    }
502227825Stheraven                }
503227825Stheraven                else
504227825Stheraven                {
505227825Stheraven                    *__st = __doesnt_match;
506227825Stheraven                    --__n_might_match;
507227825Stheraven                }
508227825Stheraven            }
509227825Stheraven        }
510227825Stheraven        // consume if we matched a character
511227825Stheraven        if (__consume)
512227825Stheraven        {
513227825Stheraven            ++__b;
514227825Stheraven            // If we consumed a character and there might be a matched keyword that
515227825Stheraven            //   was marked matched on a previous iteration, then such keywords
516227825Stheraven            //   which are now marked as not matching.
517227825Stheraven            if (__n_might_match + __n_does_match > 1)
518227825Stheraven            {
519227825Stheraven                __st = __status;
520227825Stheraven                for (_ForwardIterator __ky = __kb; __ky != __ke; ++__ky, ++__st)
521227825Stheraven                {
522227825Stheraven                    if (*__st == __does_match && __ky->size() != __indx+1)
523227825Stheraven                    {
524227825Stheraven                        *__st = __doesnt_match;
525227825Stheraven                        --__n_does_match;
526227825Stheraven                    }
527227825Stheraven                }
528227825Stheraven            }
529227825Stheraven        }
530227825Stheraven    }
531227825Stheraven    // We've exited the loop because we hit eof and/or we have no more "might matches".
532227825Stheraven    if (__b == __e)
533227825Stheraven        __err |= ios_base::eofbit;
534227825Stheraven    // Return the first matching result
535227825Stheraven    for (__st = __status; __kb != __ke; ++__kb, ++__st)
536227825Stheraven        if (*__st == __does_match)
537227825Stheraven            break;
538227825Stheraven    if (__kb == __ke)
539227825Stheraven        __err |= ios_base::failbit;
540227825Stheraven    return __kb;
541227825Stheraven}
542227825Stheraven
543227825Stheravenstruct __num_get_base
544227825Stheraven{
545227825Stheraven    static const int __num_get_buf_sz = 40;
546227825Stheraven
547227825Stheraven    static int __get_base(ios_base&);
548227825Stheraven    static const char __src[33];
549227825Stheraven};
550227825Stheraven
551227825Stheravenvoid __check_grouping(const string& __grouping, unsigned* __g, unsigned* __g_end,
552227825Stheraven                      ios_base::iostate& __err);
553227825Stheraven
554227825Stheraventemplate <class _CharT>
555227825Stheravenstruct __num_get
556227825Stheraven    : protected __num_get_base
557227825Stheraven{
558227825Stheraven    static string __stage2_int_prep(ios_base& __iob, _CharT* __atoms, _CharT& __thousands_sep);
559227825Stheraven    static string __stage2_float_prep(ios_base& __iob, _CharT* __atoms, _CharT& __decimal_point,
560227825Stheraven                                      _CharT& __thousands_sep);
561227825Stheraven    static int __stage2_int_loop(_CharT __ct, int __base, char* __a, char*& __a_end,
562227825Stheraven                  unsigned& __dc, _CharT __thousands_sep, const string& __grouping,
563227825Stheraven                  unsigned* __g, unsigned*& __g_end, _CharT* __atoms);
564227825Stheraven    static int __stage2_float_loop(_CharT __ct, bool& __in_units, char& __exp,
565227825Stheraven                                   char* __a, char*& __a_end,
566227825Stheraven                                   _CharT __decimal_point, _CharT __thousands_sep,
567227825Stheraven                                   const string& __grouping, unsigned* __g,
568227825Stheraven                                   unsigned*& __g_end, unsigned& __dc, _CharT* __atoms);
569227825Stheraven};
570227825Stheraven
571227825Stheraventemplate <class _CharT>
572227825Stheravenstring
573227825Stheraven__num_get<_CharT>::__stage2_int_prep(ios_base& __iob, _CharT* __atoms, _CharT& __thousands_sep)
574227825Stheraven{
575227825Stheraven    locale __loc = __iob.getloc();
576227825Stheraven    use_facet<ctype<_CharT> >(__loc).widen(__src, __src + 26, __atoms);
577227825Stheraven    const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc);
578227825Stheraven    __thousands_sep = __np.thousands_sep();
579227825Stheraven    return __np.grouping();
580227825Stheraven}
581227825Stheraven
582227825Stheraventemplate <class _CharT>
583227825Stheravenstring
584227825Stheraven__num_get<_CharT>::__stage2_float_prep(ios_base& __iob, _CharT* __atoms, _CharT& __decimal_point,
585227825Stheraven                    _CharT& __thousands_sep)
586227825Stheraven{
587227825Stheraven    locale __loc = __iob.getloc();
588227825Stheraven    use_facet<ctype<_CharT> >(__loc).widen(__src, __src + 32, __atoms);
589227825Stheraven    const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc);
590227825Stheraven    __decimal_point = __np.decimal_point();
591227825Stheraven    __thousands_sep = __np.thousands_sep();
592227825Stheraven    return __np.grouping();
593227825Stheraven}
594227825Stheraven
595227825Stheraventemplate <class _CharT>
596227825Stheravenint
597227825Stheraven__num_get<_CharT>::__stage2_int_loop(_CharT __ct, int __base, char* __a, char*& __a_end,
598227825Stheraven                  unsigned& __dc, _CharT __thousands_sep, const string& __grouping,
599227825Stheraven                  unsigned* __g, unsigned*& __g_end, _CharT* __atoms)
600227825Stheraven{
601227825Stheraven    if (__a_end == __a && (__ct == __atoms[24] || __ct == __atoms[25]))
602227825Stheraven    {
603227825Stheraven        *__a_end++ = __ct == __atoms[24] ? '+' : '-';
604227825Stheraven        __dc = 0;
605227825Stheraven        return 0;
606227825Stheraven    }
607232950Stheraven    if (__grouping.size() != 0 && __ct == __thousands_sep)
608227825Stheraven    {
609227825Stheraven        if (__g_end-__g < __num_get_buf_sz)
610227825Stheraven        {
611227825Stheraven            *__g_end++ = __dc;
612227825Stheraven            __dc = 0;
613227825Stheraven        }
614227825Stheraven        return 0;
615227825Stheraven    }
616227825Stheraven    ptrdiff_t __f = find(__atoms, __atoms + 26, __ct) - __atoms;
617227825Stheraven    if (__f >= 24)
618227825Stheraven        return -1;
619227825Stheraven    switch (__base)
620227825Stheraven    {
621227825Stheraven    case 8:
622227825Stheraven    case 10:
623227825Stheraven        if (__f >= __base)
624227825Stheraven            return -1;
625227825Stheraven        break;
626227825Stheraven    case 16:
627227825Stheraven        if (__f < 22)
628227825Stheraven            break;
629227825Stheraven        if (__a_end != __a && __a_end - __a <= 2 && __a_end[-1] == '0')
630227825Stheraven        {
631227825Stheraven            __dc = 0;
632227825Stheraven            *__a_end++ = __src[__f];
633227825Stheraven            return 0;
634227825Stheraven        }
635227825Stheraven        return -1;
636227825Stheraven    }
637249998Sdim    *__a_end++ = __src[__f];
638227825Stheraven    ++__dc;
639227825Stheraven    return 0;
640227825Stheraven}
641227825Stheraven
642227825Stheraventemplate <class _CharT>
643227825Stheravenint
644227825Stheraven__num_get<_CharT>::__stage2_float_loop(_CharT __ct, bool& __in_units, char& __exp, char* __a, char*& __a_end,
645227825Stheraven                    _CharT __decimal_point, _CharT __thousands_sep, const string& __grouping,
646227825Stheraven                    unsigned* __g, unsigned*& __g_end, unsigned& __dc, _CharT* __atoms)
647227825Stheraven{
648227825Stheraven    if (__ct == __decimal_point)
649227825Stheraven    {
650227825Stheraven        if (!__in_units)
651227825Stheraven            return -1;
652227825Stheraven        __in_units = false;
653227825Stheraven        *__a_end++ = '.';
654227825Stheraven        if (__grouping.size() != 0 && __g_end-__g < __num_get_buf_sz)
655227825Stheraven            *__g_end++ = __dc;
656227825Stheraven        return 0;
657227825Stheraven    }
658227825Stheraven    if (__ct == __thousands_sep && __grouping.size() != 0)
659227825Stheraven    {
660227825Stheraven        if (!__in_units)
661227825Stheraven            return -1;
662227825Stheraven        if (__g_end-__g < __num_get_buf_sz)
663227825Stheraven        {
664227825Stheraven            *__g_end++ = __dc;
665227825Stheraven            __dc = 0;
666227825Stheraven        }
667227825Stheraven        return 0;
668227825Stheraven    }
669227825Stheraven    ptrdiff_t __f = find(__atoms, __atoms + 32, __ct) - __atoms;
670227825Stheraven    if (__f >= 32)
671227825Stheraven        return -1;
672227825Stheraven    char __x = __src[__f];
673232950Stheraven    if (__x == '-' || __x == '+')
674232950Stheraven    {
675249998Sdim        if (__a_end == __a || (__a_end[-1] & 0x5F) == (__exp & 0x7F))
676232950Stheraven        {
677232950Stheraven            *__a_end++ = __x;
678232950Stheraven            return 0;
679232950Stheraven        }
680232950Stheraven        return -1;
681232950Stheraven    }
682227825Stheraven    if (__x == 'x' || __x == 'X')
683227825Stheraven        __exp = 'P';
684249998Sdim    else if ((__x & 0x5F) == __exp)
685227825Stheraven    {
686249998Sdim        __exp |= 0x80;
687249998Sdim        if (__in_units)
688249998Sdim        {
689249998Sdim            __in_units = false;
690249998Sdim            if (__grouping.size() != 0 && __g_end-__g < __num_get_buf_sz)
691249998Sdim                *__g_end++ = __dc;
692249998Sdim        }
693227825Stheraven    }
694249998Sdim    *__a_end++ = __x;
695227825Stheraven    if (__f >= 22)
696227825Stheraven        return 0;
697227825Stheraven    ++__dc;
698227825Stheraven    return 0;
699227825Stheraven}
700227825Stheraven
701242945Stheraven_LIBCPP_EXTERN_TEMPLATE(struct __num_get<char>)
702242945Stheraven_LIBCPP_EXTERN_TEMPLATE(struct __num_get<wchar_t>)
703227825Stheraven
704227825Stheraventemplate <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
705249998Sdimclass _LIBCPP_TYPE_VIS num_get
706227825Stheraven    : public locale::facet,
707227825Stheraven      private __num_get<_CharT>
708227825Stheraven{
709227825Stheravenpublic:
710227825Stheraven    typedef _CharT char_type;
711227825Stheraven    typedef _InputIterator iter_type;
712227825Stheraven
713227825Stheraven    _LIBCPP_ALWAYS_INLINE
714227825Stheraven    explicit num_get(size_t __refs = 0)
715227825Stheraven        : locale::facet(__refs) {}
716227825Stheraven
717227825Stheraven    _LIBCPP_ALWAYS_INLINE
718227825Stheraven    iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
719227825Stheraven                  ios_base::iostate& __err, bool& __v) const
720227825Stheraven    {
721227825Stheraven        return do_get(__b, __e, __iob, __err, __v);
722227825Stheraven    }
723227825Stheraven
724227825Stheraven    _LIBCPP_ALWAYS_INLINE
725227825Stheraven    iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
726227825Stheraven                  ios_base::iostate& __err, long& __v) const
727227825Stheraven    {
728227825Stheraven        return do_get(__b, __e, __iob, __err, __v);
729227825Stheraven    }
730227825Stheraven
731227825Stheraven    _LIBCPP_ALWAYS_INLINE
732227825Stheraven    iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
733227825Stheraven                  ios_base::iostate& __err, long long& __v) const
734227825Stheraven    {
735227825Stheraven        return do_get(__b, __e, __iob, __err, __v);
736227825Stheraven    }
737227825Stheraven
738227825Stheraven    _LIBCPP_ALWAYS_INLINE
739227825Stheraven    iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
740227825Stheraven                  ios_base::iostate& __err, unsigned short& __v) const
741227825Stheraven    {
742227825Stheraven        return do_get(__b, __e, __iob, __err, __v);
743227825Stheraven    }
744227825Stheraven
745227825Stheraven    _LIBCPP_ALWAYS_INLINE
746227825Stheraven    iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
747227825Stheraven                  ios_base::iostate& __err, unsigned int& __v) const
748227825Stheraven    {
749227825Stheraven        return do_get(__b, __e, __iob, __err, __v);
750227825Stheraven    }
751227825Stheraven
752227825Stheraven    _LIBCPP_ALWAYS_INLINE
753227825Stheraven    iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
754227825Stheraven                  ios_base::iostate& __err, unsigned long& __v) const
755227825Stheraven    {
756227825Stheraven        return do_get(__b, __e, __iob, __err, __v);
757227825Stheraven    }
758227825Stheraven
759227825Stheraven    _LIBCPP_ALWAYS_INLINE
760227825Stheraven    iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
761227825Stheraven                  ios_base::iostate& __err, unsigned long long& __v) const
762227825Stheraven    {
763227825Stheraven        return do_get(__b, __e, __iob, __err, __v);
764227825Stheraven    }
765227825Stheraven
766227825Stheraven    _LIBCPP_ALWAYS_INLINE
767227825Stheraven    iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
768227825Stheraven                  ios_base::iostate& __err, float& __v) const
769227825Stheraven    {
770227825Stheraven        return do_get(__b, __e, __iob, __err, __v);
771227825Stheraven    }
772227825Stheraven
773227825Stheraven    _LIBCPP_ALWAYS_INLINE
774227825Stheraven    iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
775227825Stheraven                  ios_base::iostate& __err, double& __v) const
776227825Stheraven    {
777227825Stheraven        return do_get(__b, __e, __iob, __err, __v);
778227825Stheraven    }
779227825Stheraven
780227825Stheraven    _LIBCPP_ALWAYS_INLINE
781227825Stheraven    iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
782227825Stheraven                  ios_base::iostate& __err, long double& __v) const
783227825Stheraven    {
784227825Stheraven        return do_get(__b, __e, __iob, __err, __v);
785227825Stheraven    }
786227825Stheraven
787227825Stheraven    _LIBCPP_ALWAYS_INLINE
788227825Stheraven    iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
789227825Stheraven                  ios_base::iostate& __err, void*& __v) const
790227825Stheraven    {
791227825Stheraven        return do_get(__b, __e, __iob, __err, __v);
792227825Stheraven    }
793227825Stheraven
794227825Stheraven    static locale::id id;
795227825Stheraven
796227825Stheravenprotected:
797227825Stheraven    _LIBCPP_ALWAYS_INLINE
798227825Stheraven    ~num_get() {}
799227825Stheraven
800227825Stheraven    virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
801227825Stheraven                             ios_base::iostate& __err, bool& __v) const;
802227825Stheraven    virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
803227825Stheraven                             ios_base::iostate& __err, long& __v) const;
804227825Stheraven    virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
805227825Stheraven                             ios_base::iostate& __err, long long& __v) const;
806227825Stheraven    virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
807227825Stheraven                             ios_base::iostate& __err, unsigned short& __v) const;
808227825Stheraven    virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
809227825Stheraven                             ios_base::iostate& __err, unsigned int& __v) const;
810227825Stheraven    virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
811227825Stheraven                             ios_base::iostate& __err, unsigned long& __v) const;
812227825Stheraven    virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
813227825Stheraven                             ios_base::iostate& __err, unsigned long long& __v) const;
814227825Stheraven    virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
815227825Stheraven                             ios_base::iostate& __err, float& __v) const;
816227825Stheraven    virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
817227825Stheraven                             ios_base::iostate& __err, double& __v) const;
818227825Stheraven    virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
819227825Stheraven                             ios_base::iostate& __err, long double& __v) const;
820227825Stheraven    virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
821227825Stheraven                             ios_base::iostate& __err, void*& __v) const;
822227825Stheraven};
823227825Stheraven
824227825Stheraventemplate <class _CharT, class _InputIterator>
825227825Stheravenlocale::id
826227825Stheravennum_get<_CharT, _InputIterator>::id;
827227825Stheraven
828227825Stheraventemplate <class _Tp>
829227825Stheraven_Tp
830227825Stheraven__num_get_signed_integral(const char* __a, const char* __a_end,
831227825Stheraven                          ios_base::iostate& __err, int __base)
832227825Stheraven{
833227825Stheraven    if (__a != __a_end)
834227825Stheraven    {
835246487Stheraven        typename remove_reference<decltype(errno)>::type __save_errno = errno;
836227825Stheraven        errno = 0;
837227825Stheraven        char *__p2;
838227825Stheraven        long long __ll = strtoll_l(__a, &__p2, __base, _LIBCPP_GET_C_LOCALE);
839246487Stheraven        typename remove_reference<decltype(errno)>::type __current_errno = errno;
840227825Stheraven        if (__current_errno == 0)
841227825Stheraven            errno = __save_errno;
842227825Stheraven        if (__p2 != __a_end)
843227825Stheraven        {
844227825Stheraven            __err = ios_base::failbit;
845227825Stheraven            return 0;
846227825Stheraven        }
847227825Stheraven        else if (__current_errno == ERANGE         ||
848227825Stheraven                 __ll < numeric_limits<_Tp>::min() ||
849227825Stheraven                 numeric_limits<_Tp>::max() < __ll)
850227825Stheraven        {
851227825Stheraven            __err = ios_base::failbit;
852227825Stheraven            if (__ll > 0)
853227825Stheraven                return numeric_limits<_Tp>::max();
854227825Stheraven            else
855227825Stheraven                return numeric_limits<_Tp>::min();
856227825Stheraven        }
857227825Stheraven        return static_cast<_Tp>(__ll);
858227825Stheraven    }
859227825Stheraven    __err = ios_base::failbit;
860227825Stheraven    return 0;
861227825Stheraven}
862227825Stheraven
863227825Stheraventemplate <class _Tp>
864227825Stheraven_Tp
865227825Stheraven__num_get_unsigned_integral(const char* __a, const char* __a_end,
866227825Stheraven                            ios_base::iostate& __err, int __base)
867227825Stheraven{
868227825Stheraven    if (__a != __a_end)
869227825Stheraven    {
870227825Stheraven        if (*__a == '-')
871227825Stheraven        {
872227825Stheraven            __err = ios_base::failbit;
873227825Stheraven            return 0;
874227825Stheraven        }
875246487Stheraven        typename remove_reference<decltype(errno)>::type __save_errno = errno;
876227825Stheraven        errno = 0;
877227825Stheraven        char *__p2;
878227825Stheraven        unsigned long long __ll = strtoull_l(__a, &__p2, __base, _LIBCPP_GET_C_LOCALE);
879246487Stheraven        typename remove_reference<decltype(errno)>::type __current_errno = errno;
880227825Stheraven        if (__current_errno == 0)
881227825Stheraven            errno = __save_errno;
882227825Stheraven        if (__p2 != __a_end)
883227825Stheraven        {
884227825Stheraven            __err = ios_base::failbit;
885227825Stheraven            return 0;
886227825Stheraven        }
887227825Stheraven        else if (__current_errno == ERANGE ||
888227825Stheraven                 numeric_limits<_Tp>::max() < __ll)
889227825Stheraven        {
890227825Stheraven            __err = ios_base::failbit;
891227825Stheraven            return numeric_limits<_Tp>::max();
892227825Stheraven        }
893227825Stheraven        return static_cast<_Tp>(__ll);
894227825Stheraven    }
895227825Stheraven    __err = ios_base::failbit;
896227825Stheraven    return 0;
897227825Stheraven}
898227825Stheraven
899227825Stheraventemplate <class _Tp>
900227825Stheraven_Tp
901227825Stheraven__num_get_float(const char* __a, const char* __a_end, ios_base::iostate& __err)
902227825Stheraven{
903227825Stheraven    if (__a != __a_end)
904227825Stheraven    {
905249998Sdim        typename remove_reference<decltype(errno)>::type __save_errno = errno;
906249998Sdim        errno = 0;
907227825Stheraven        char *__p2;
908227825Stheraven        long double __ld = strtold_l(__a, &__p2, _LIBCPP_GET_C_LOCALE);
909249998Sdim        typename remove_reference<decltype(errno)>::type __current_errno = errno;
910249998Sdim        if (__current_errno == 0)
911249998Sdim            errno = __save_errno;
912227825Stheraven        if (__p2 != __a_end)
913227825Stheraven        {
914227825Stheraven            __err = ios_base::failbit;
915227825Stheraven            return 0;
916227825Stheraven        }
917249998Sdim        else if (__current_errno == ERANGE)
918249998Sdim            __err = ios_base::failbit;
919227825Stheraven        return static_cast<_Tp>(__ld);
920227825Stheraven    }
921227825Stheraven    __err = ios_base::failbit;
922227825Stheraven    return 0;
923227825Stheraven}
924227825Stheraven
925227825Stheraventemplate <class _CharT, class _InputIterator>
926227825Stheraven_InputIterator
927227825Stheravennum_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
928227825Stheraven                                        ios_base& __iob,
929227825Stheraven                                        ios_base::iostate& __err,
930227825Stheraven                                        bool& __v) const
931227825Stheraven{
932227825Stheraven    if ((__iob.flags() & ios_base::boolalpha) == 0)
933227825Stheraven    {
934227825Stheraven        long __lv = -1;
935227825Stheraven        __b = do_get(__b, __e, __iob, __err, __lv);
936227825Stheraven        switch (__lv)
937227825Stheraven        {
938227825Stheraven        case 0:
939227825Stheraven            __v = false;
940227825Stheraven            break;
941227825Stheraven        case 1:
942227825Stheraven            __v = true;
943227825Stheraven            break;
944227825Stheraven        default:
945227825Stheraven            __v = true;
946227825Stheraven            __err = ios_base::failbit;
947227825Stheraven            break;
948227825Stheraven        }
949227825Stheraven        return __b;
950227825Stheraven    }
951227825Stheraven    const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__iob.getloc());
952227825Stheraven    const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__iob.getloc());
953227825Stheraven    typedef typename numpunct<_CharT>::string_type string_type;
954227825Stheraven    const string_type __names[2] = {__np.truename(), __np.falsename()};
955227825Stheraven    const string_type* __i = __scan_keyword(__b, __e, __names, __names+2,
956227825Stheraven                                            __ct, __err);
957227825Stheraven    __v = __i == __names;
958227825Stheraven    return __b;
959227825Stheraven}
960227825Stheraven
961227825Stheraventemplate <class _CharT, class _InputIterator>
962227825Stheraven_InputIterator
963227825Stheravennum_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
964227825Stheraven                                        ios_base& __iob,
965227825Stheraven                                        ios_base::iostate& __err,
966227825Stheraven                                        long& __v) const
967227825Stheraven{
968227825Stheraven    // Stage 1
969227825Stheraven    int __base = this->__get_base(__iob);
970227825Stheraven    // Stage 2
971227825Stheraven    char_type __atoms[26];
972227825Stheraven    char_type __thousands_sep;
973227825Stheraven    string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
974249998Sdim    string __buf;
975249998Sdim    __buf.resize(__buf.capacity());
976249998Sdim    char* __a = &__buf[0];
977227825Stheraven    char* __a_end = __a;
978227825Stheraven    unsigned __g[__num_get_base::__num_get_buf_sz];
979227825Stheraven    unsigned* __g_end = __g;
980227825Stheraven    unsigned __dc = 0;
981227825Stheraven    for (; __b != __e; ++__b)
982249998Sdim    {
983249998Sdim        if (__a_end - __a == __buf.size())
984249998Sdim        {
985249998Sdim            size_t __tmp = __buf.size();
986249998Sdim            __buf.resize(2*__buf.size());
987249998Sdim            __buf.resize(__buf.capacity());
988249998Sdim            __a = &__buf[0];
989249998Sdim            __a_end = __a + __tmp;
990249998Sdim        }
991227825Stheraven        if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
992227825Stheraven                                    __thousands_sep, __grouping, __g, __g_end,
993227825Stheraven                                    __atoms))
994227825Stheraven            break;
995249998Sdim    }
996227825Stheraven    if (__grouping.size() != 0 && __g_end-__g < __num_get_base::__num_get_buf_sz)
997227825Stheraven        *__g_end++ = __dc;
998227825Stheraven    // Stage 3
999227825Stheraven    __v = __num_get_signed_integral<long>(__a, __a_end, __err, __base);
1000227825Stheraven    // Digit grouping checked
1001227825Stheraven    __check_grouping(__grouping, __g, __g_end, __err);
1002227825Stheraven    // EOF checked
1003227825Stheraven    if (__b == __e)
1004227825Stheraven        __err |= ios_base::eofbit;
1005227825Stheraven    return __b;
1006227825Stheraven}
1007227825Stheraven
1008227825Stheraventemplate <class _CharT, class _InputIterator>
1009227825Stheraven_InputIterator
1010227825Stheravennum_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1011227825Stheraven                                        ios_base& __iob,
1012227825Stheraven                                        ios_base::iostate& __err,
1013227825Stheraven                                        long long& __v) const
1014227825Stheraven{
1015227825Stheraven    // Stage 1
1016227825Stheraven    int __base = this->__get_base(__iob);
1017227825Stheraven    // Stage 2
1018227825Stheraven    char_type __atoms[26];
1019227825Stheraven    char_type __thousands_sep;
1020227825Stheraven    string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
1021249998Sdim    string __buf;
1022249998Sdim    __buf.resize(__buf.capacity());
1023249998Sdim    char* __a = &__buf[0];
1024227825Stheraven    char* __a_end = __a;
1025227825Stheraven    unsigned __g[__num_get_base::__num_get_buf_sz];
1026227825Stheraven    unsigned* __g_end = __g;
1027227825Stheraven    unsigned __dc = 0;
1028227825Stheraven    for (; __b != __e; ++__b)
1029249998Sdim    {
1030249998Sdim        if (__a_end - __a == __buf.size())
1031249998Sdim        {
1032249998Sdim            size_t __tmp = __buf.size();
1033249998Sdim            __buf.resize(2*__buf.size());
1034249998Sdim            __buf.resize(__buf.capacity());
1035249998Sdim            __a = &__buf[0];
1036249998Sdim            __a_end = __a + __tmp;
1037249998Sdim        }
1038227825Stheraven        if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
1039227825Stheraven                                    __thousands_sep, __grouping, __g, __g_end,
1040227825Stheraven                                    __atoms))
1041227825Stheraven            break;
1042249998Sdim    }
1043227825Stheraven    if (__grouping.size() != 0 && __g_end-__g < __num_get_base::__num_get_buf_sz)
1044227825Stheraven        *__g_end++ = __dc;
1045227825Stheraven    // Stage 3
1046227825Stheraven    __v = __num_get_signed_integral<long long>(__a, __a_end, __err, __base);
1047227825Stheraven    // Digit grouping checked
1048227825Stheraven    __check_grouping(__grouping, __g, __g_end, __err);
1049227825Stheraven    // EOF checked
1050227825Stheraven    if (__b == __e)
1051227825Stheraven        __err |= ios_base::eofbit;
1052227825Stheraven    return __b;
1053227825Stheraven}
1054227825Stheraven
1055227825Stheraventemplate <class _CharT, class _InputIterator>
1056227825Stheraven_InputIterator
1057227825Stheravennum_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1058227825Stheraven                                        ios_base& __iob,
1059227825Stheraven                                        ios_base::iostate& __err,
1060227825Stheraven                                        unsigned short& __v) const
1061227825Stheraven{
1062227825Stheraven    // Stage 1
1063227825Stheraven    int __base = this->__get_base(__iob);
1064227825Stheraven    // Stage 2
1065227825Stheraven    char_type __atoms[26];
1066227825Stheraven    char_type __thousands_sep;
1067227825Stheraven    string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
1068249998Sdim    string __buf;
1069249998Sdim    __buf.resize(__buf.capacity());
1070249998Sdim    char* __a = &__buf[0];
1071227825Stheraven    char* __a_end = __a;
1072227825Stheraven    unsigned __g[__num_get_base::__num_get_buf_sz];
1073227825Stheraven    unsigned* __g_end = __g;
1074227825Stheraven    unsigned __dc = 0;
1075227825Stheraven    for (; __b != __e; ++__b)
1076249998Sdim    {
1077249998Sdim        if (__a_end - __a == __buf.size())
1078249998Sdim        {
1079249998Sdim            size_t __tmp = __buf.size();
1080249998Sdim            __buf.resize(2*__buf.size());
1081249998Sdim            __buf.resize(__buf.capacity());
1082249998Sdim            __a = &__buf[0];
1083249998Sdim            __a_end = __a + __tmp;
1084249998Sdim        }
1085227825Stheraven        if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
1086227825Stheraven                                    __thousands_sep, __grouping, __g, __g_end,
1087227825Stheraven                                    __atoms))
1088227825Stheraven            break;
1089249998Sdim    }
1090227825Stheraven    if (__grouping.size() != 0 && __g_end-__g < __num_get_base::__num_get_buf_sz)
1091227825Stheraven        *__g_end++ = __dc;
1092227825Stheraven    // Stage 3
1093227825Stheraven    __v = __num_get_unsigned_integral<unsigned short>(__a, __a_end, __err, __base);
1094227825Stheraven    // Digit grouping checked
1095227825Stheraven    __check_grouping(__grouping, __g, __g_end, __err);
1096227825Stheraven    // EOF checked
1097227825Stheraven    if (__b == __e)
1098227825Stheraven        __err |= ios_base::eofbit;
1099227825Stheraven    return __b;
1100227825Stheraven}
1101227825Stheraven
1102227825Stheraventemplate <class _CharT, class _InputIterator>
1103227825Stheraven_InputIterator
1104227825Stheravennum_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1105227825Stheraven                                        ios_base& __iob,
1106227825Stheraven                                        ios_base::iostate& __err,
1107227825Stheraven                                        unsigned int& __v) const
1108227825Stheraven{
1109227825Stheraven    // Stage 1
1110227825Stheraven    int __base = this->__get_base(__iob);
1111227825Stheraven    // Stage 2
1112227825Stheraven    char_type __atoms[26];
1113227825Stheraven    char_type __thousands_sep;
1114227825Stheraven    string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
1115249998Sdim    string __buf;
1116249998Sdim    __buf.resize(__buf.capacity());
1117249998Sdim    char* __a = &__buf[0];
1118227825Stheraven    char* __a_end = __a;
1119227825Stheraven    unsigned __g[__num_get_base::__num_get_buf_sz];
1120227825Stheraven    unsigned* __g_end = __g;
1121227825Stheraven    unsigned __dc = 0;
1122227825Stheraven    for (; __b != __e; ++__b)
1123249998Sdim    {
1124249998Sdim        if (__a_end - __a == __buf.size())
1125249998Sdim        {
1126249998Sdim            size_t __tmp = __buf.size();
1127249998Sdim            __buf.resize(2*__buf.size());
1128249998Sdim            __buf.resize(__buf.capacity());
1129249998Sdim            __a = &__buf[0];
1130249998Sdim            __a_end = __a + __tmp;
1131249998Sdim        }
1132227825Stheraven        if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
1133227825Stheraven                                    __thousands_sep, __grouping, __g, __g_end,
1134227825Stheraven                                    __atoms))
1135227825Stheraven            break;
1136249998Sdim    }
1137227825Stheraven    if (__grouping.size() != 0 && __g_end-__g < __num_get_base::__num_get_buf_sz)
1138227825Stheraven        *__g_end++ = __dc;
1139227825Stheraven    // Stage 3
1140227825Stheraven    __v = __num_get_unsigned_integral<unsigned int>(__a, __a_end, __err, __base);
1141227825Stheraven    // Digit grouping checked
1142227825Stheraven    __check_grouping(__grouping, __g, __g_end, __err);
1143227825Stheraven    // EOF checked
1144227825Stheraven    if (__b == __e)
1145227825Stheraven        __err |= ios_base::eofbit;
1146227825Stheraven    return __b;
1147227825Stheraven}
1148227825Stheraven
1149227825Stheraventemplate <class _CharT, class _InputIterator>
1150227825Stheraven_InputIterator
1151227825Stheravennum_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1152227825Stheraven                                        ios_base& __iob,
1153227825Stheraven                                        ios_base::iostate& __err,
1154227825Stheraven                                        unsigned long& __v) const
1155227825Stheraven{
1156227825Stheraven    // Stage 1
1157227825Stheraven    int __base = this->__get_base(__iob);
1158227825Stheraven    // Stage 2
1159227825Stheraven    char_type __atoms[26];
1160227825Stheraven    char_type __thousands_sep;
1161227825Stheraven    string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
1162249998Sdim    string __buf;
1163249998Sdim    __buf.resize(__buf.capacity());
1164249998Sdim    char* __a = &__buf[0];
1165227825Stheraven    char* __a_end = __a;
1166227825Stheraven    unsigned __g[__num_get_base::__num_get_buf_sz];
1167227825Stheraven    unsigned* __g_end = __g;
1168227825Stheraven    unsigned __dc = 0;
1169227825Stheraven    for (; __b != __e; ++__b)
1170249998Sdim    {
1171249998Sdim        if (__a_end - __a == __buf.size())
1172249998Sdim        {
1173249998Sdim            size_t __tmp = __buf.size();
1174249998Sdim            __buf.resize(2*__buf.size());
1175249998Sdim            __buf.resize(__buf.capacity());
1176249998Sdim            __a = &__buf[0];
1177249998Sdim            __a_end = __a + __tmp;
1178249998Sdim        }
1179227825Stheraven        if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
1180227825Stheraven                                    __thousands_sep, __grouping, __g, __g_end,
1181227825Stheraven                                    __atoms))
1182227825Stheraven            break;
1183249998Sdim    }
1184227825Stheraven    if (__grouping.size() != 0 && __g_end-__g < __num_get_base::__num_get_buf_sz)
1185227825Stheraven        *__g_end++ = __dc;
1186227825Stheraven    // Stage 3
1187227825Stheraven    __v = __num_get_unsigned_integral<unsigned long>(__a, __a_end, __err, __base);
1188227825Stheraven    // Digit grouping checked
1189227825Stheraven    __check_grouping(__grouping, __g, __g_end, __err);
1190227825Stheraven    // EOF checked
1191227825Stheraven    if (__b == __e)
1192227825Stheraven        __err |= ios_base::eofbit;
1193227825Stheraven    return __b;
1194227825Stheraven}
1195227825Stheraven
1196227825Stheraventemplate <class _CharT, class _InputIterator>
1197227825Stheraven_InputIterator
1198227825Stheravennum_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1199227825Stheraven                                        ios_base& __iob,
1200227825Stheraven                                        ios_base::iostate& __err,
1201227825Stheraven                                        unsigned long long& __v) const
1202227825Stheraven{
1203227825Stheraven    // Stage 1
1204227825Stheraven    int __base = this->__get_base(__iob);
1205227825Stheraven    // Stage 2
1206227825Stheraven    char_type __atoms[26];
1207227825Stheraven    char_type __thousands_sep;
1208227825Stheraven    string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
1209249998Sdim    string __buf;
1210249998Sdim    __buf.resize(__buf.capacity());
1211249998Sdim    char* __a = &__buf[0];
1212227825Stheraven    char* __a_end = __a;
1213227825Stheraven    unsigned __g[__num_get_base::__num_get_buf_sz];
1214227825Stheraven    unsigned* __g_end = __g;
1215227825Stheraven    unsigned __dc = 0;
1216227825Stheraven    for (; __b != __e; ++__b)
1217249998Sdim    {
1218249998Sdim        if (__a_end - __a == __buf.size())
1219249998Sdim        {
1220249998Sdim            size_t __tmp = __buf.size();
1221249998Sdim            __buf.resize(2*__buf.size());
1222249998Sdim            __buf.resize(__buf.capacity());
1223249998Sdim            __a = &__buf[0];
1224249998Sdim            __a_end = __a + __tmp;
1225249998Sdim        }
1226227825Stheraven        if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
1227227825Stheraven                                    __thousands_sep, __grouping, __g, __g_end,
1228227825Stheraven                                    __atoms))
1229227825Stheraven            break;
1230249998Sdim    }
1231227825Stheraven    if (__grouping.size() != 0 && __g_end-__g < __num_get_base::__num_get_buf_sz)
1232227825Stheraven        *__g_end++ = __dc;
1233227825Stheraven    // Stage 3
1234227825Stheraven    __v = __num_get_unsigned_integral<unsigned long long>(__a, __a_end, __err, __base);
1235227825Stheraven    // Digit grouping checked
1236227825Stheraven    __check_grouping(__grouping, __g, __g_end, __err);
1237227825Stheraven    // EOF checked
1238227825Stheraven    if (__b == __e)
1239227825Stheraven        __err |= ios_base::eofbit;
1240227825Stheraven    return __b;
1241227825Stheraven}
1242227825Stheraven
1243227825Stheraventemplate <class _CharT, class _InputIterator>
1244227825Stheraven_InputIterator
1245227825Stheravennum_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1246227825Stheraven                                        ios_base& __iob,
1247227825Stheraven                                        ios_base::iostate& __err,
1248227825Stheraven                                        float& __v) const
1249227825Stheraven{
1250227825Stheraven    // Stage 1, nothing to do
1251227825Stheraven    // Stage 2
1252227825Stheraven    char_type __atoms[32];
1253227825Stheraven    char_type __decimal_point;
1254227825Stheraven    char_type __thousands_sep;
1255227825Stheraven    string __grouping = this->__stage2_float_prep(__iob, __atoms,
1256227825Stheraven                                                  __decimal_point,
1257227825Stheraven                                                  __thousands_sep);
1258249998Sdim    string __buf;
1259249998Sdim    __buf.resize(__buf.capacity());
1260249998Sdim    char* __a = &__buf[0];
1261227825Stheraven    char* __a_end = __a;
1262227825Stheraven    unsigned __g[__num_get_base::__num_get_buf_sz];
1263227825Stheraven    unsigned* __g_end = __g;
1264227825Stheraven    unsigned __dc = 0;
1265227825Stheraven    bool __in_units = true;
1266227825Stheraven    char __exp = 'E';
1267227825Stheraven    for (; __b != __e; ++__b)
1268249998Sdim    {
1269249998Sdim        if (__a_end - __a == __buf.size())
1270249998Sdim        {
1271249998Sdim            size_t __tmp = __buf.size();
1272249998Sdim            __buf.resize(2*__buf.size());
1273249998Sdim            __buf.resize(__buf.capacity());
1274249998Sdim            __a = &__buf[0];
1275249998Sdim            __a_end = __a + __tmp;
1276249998Sdim        }
1277227825Stheraven        if (this->__stage2_float_loop(*__b, __in_units, __exp, __a, __a_end,
1278227825Stheraven                                      __decimal_point, __thousands_sep,
1279227825Stheraven                                      __grouping, __g, __g_end,
1280227825Stheraven                                      __dc, __atoms))
1281227825Stheraven            break;
1282249998Sdim    }
1283227825Stheraven    if (__grouping.size() != 0 && __in_units && __g_end-__g < __num_get_base::__num_get_buf_sz)
1284227825Stheraven        *__g_end++ = __dc;
1285227825Stheraven    // Stage 3
1286227825Stheraven    __v = __num_get_float<float>(__a, __a_end, __err);
1287227825Stheraven    // Digit grouping checked
1288227825Stheraven    __check_grouping(__grouping, __g, __g_end, __err);
1289227825Stheraven    // EOF checked
1290227825Stheraven    if (__b == __e)
1291227825Stheraven        __err |= ios_base::eofbit;
1292227825Stheraven    return __b;
1293227825Stheraven}
1294227825Stheraven
1295227825Stheraventemplate <class _CharT, class _InputIterator>
1296227825Stheraven_InputIterator
1297227825Stheravennum_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1298227825Stheraven                                        ios_base& __iob,
1299227825Stheraven                                        ios_base::iostate& __err,
1300227825Stheraven                                        double& __v) const
1301227825Stheraven{
1302227825Stheraven    // Stage 1, nothing to do
1303227825Stheraven    // Stage 2
1304227825Stheraven    char_type __atoms[32];
1305227825Stheraven    char_type __decimal_point;
1306227825Stheraven    char_type __thousands_sep;
1307227825Stheraven    string __grouping = this->__stage2_float_prep(__iob, __atoms,
1308227825Stheraven                                                  __decimal_point,
1309227825Stheraven                                                  __thousands_sep);
1310249998Sdim    string __buf;
1311249998Sdim    __buf.resize(__buf.capacity());
1312249998Sdim    char* __a = &__buf[0];
1313227825Stheraven    char* __a_end = __a;
1314227825Stheraven    unsigned __g[__num_get_base::__num_get_buf_sz];
1315227825Stheraven    unsigned* __g_end = __g;
1316227825Stheraven    unsigned __dc = 0;
1317227825Stheraven    bool __in_units = true;
1318227825Stheraven    char __exp = 'E';
1319227825Stheraven    for (; __b != __e; ++__b)
1320249998Sdim    {
1321249998Sdim        if (__a_end - __a == __buf.size())
1322249998Sdim        {
1323249998Sdim            size_t __tmp = __buf.size();
1324249998Sdim            __buf.resize(2*__buf.size());
1325249998Sdim            __buf.resize(__buf.capacity());
1326249998Sdim            __a = &__buf[0];
1327249998Sdim            __a_end = __a + __tmp;
1328249998Sdim        }
1329227825Stheraven        if (this->__stage2_float_loop(*__b, __in_units, __exp, __a, __a_end,
1330227825Stheraven                                      __decimal_point, __thousands_sep,
1331227825Stheraven                                      __grouping, __g, __g_end,
1332227825Stheraven                                      __dc, __atoms))
1333227825Stheraven            break;
1334249998Sdim    }
1335227825Stheraven    if (__grouping.size() != 0 && __in_units && __g_end-__g < __num_get_base::__num_get_buf_sz)
1336227825Stheraven        *__g_end++ = __dc;
1337227825Stheraven    // Stage 3
1338227825Stheraven    __v = __num_get_float<double>(__a, __a_end, __err);
1339227825Stheraven    // Digit grouping checked
1340227825Stheraven    __check_grouping(__grouping, __g, __g_end, __err);
1341227825Stheraven    // EOF checked
1342227825Stheraven    if (__b == __e)
1343227825Stheraven        __err |= ios_base::eofbit;
1344227825Stheraven    return __b;
1345227825Stheraven}
1346227825Stheraven
1347227825Stheraventemplate <class _CharT, class _InputIterator>
1348227825Stheraven_InputIterator
1349227825Stheravennum_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1350227825Stheraven                                        ios_base& __iob,
1351227825Stheraven                                        ios_base::iostate& __err,
1352227825Stheraven                                        long double& __v) const
1353227825Stheraven{
1354227825Stheraven    // Stage 1, nothing to do
1355227825Stheraven    // Stage 2
1356227825Stheraven    char_type __atoms[32];
1357227825Stheraven    char_type __decimal_point;
1358227825Stheraven    char_type __thousands_sep;
1359227825Stheraven    string __grouping = this->__stage2_float_prep(__iob, __atoms,
1360227825Stheraven                                                  __decimal_point,
1361227825Stheraven                                                  __thousands_sep);
1362249998Sdim    string __buf;
1363249998Sdim    __buf.resize(__buf.capacity());
1364249998Sdim    char* __a = &__buf[0];
1365227825Stheraven    char* __a_end = __a;
1366227825Stheraven    unsigned __g[__num_get_base::__num_get_buf_sz];
1367227825Stheraven    unsigned* __g_end = __g;
1368227825Stheraven    unsigned __dc = 0;
1369227825Stheraven    bool __in_units = true;
1370227825Stheraven    char __exp = 'E';
1371227825Stheraven    for (; __b != __e; ++__b)
1372249998Sdim    {
1373249998Sdim        if (__a_end - __a == __buf.size())
1374249998Sdim        {
1375249998Sdim            size_t __tmp = __buf.size();
1376249998Sdim            __buf.resize(2*__buf.size());
1377249998Sdim            __buf.resize(__buf.capacity());
1378249998Sdim            __a = &__buf[0];
1379249998Sdim            __a_end = __a + __tmp;
1380249998Sdim        }
1381227825Stheraven        if (this->__stage2_float_loop(*__b, __in_units, __exp, __a, __a_end,
1382227825Stheraven                                      __decimal_point, __thousands_sep,
1383227825Stheraven                                      __grouping, __g, __g_end,
1384227825Stheraven                                      __dc, __atoms))
1385227825Stheraven            break;
1386249998Sdim    }
1387227825Stheraven    if (__grouping.size() != 0 && __in_units && __g_end-__g < __num_get_base::__num_get_buf_sz)
1388227825Stheraven        *__g_end++ = __dc;
1389227825Stheraven    // Stage 3
1390227825Stheraven    __v = __num_get_float<long double>(__a, __a_end, __err);
1391227825Stheraven    // Digit grouping checked
1392227825Stheraven    __check_grouping(__grouping, __g, __g_end, __err);
1393227825Stheraven    // EOF checked
1394227825Stheraven    if (__b == __e)
1395227825Stheraven        __err |= ios_base::eofbit;
1396227825Stheraven    return __b;
1397227825Stheraven}
1398227825Stheraven
1399227825Stheraventemplate <class _CharT, class _InputIterator>
1400227825Stheraven_InputIterator
1401227825Stheravennum_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1402227825Stheraven                                        ios_base& __iob,
1403227825Stheraven                                        ios_base::iostate& __err,
1404227825Stheraven                                        void*& __v) const
1405227825Stheraven{
1406227825Stheraven    // Stage 1
1407227825Stheraven    int __base = 16;
1408227825Stheraven    // Stage 2
1409227825Stheraven    char_type __atoms[26];
1410232950Stheraven    char_type __thousands_sep = 0;
1411227825Stheraven    string __grouping;
1412227825Stheraven    use_facet<ctype<_CharT> >(__iob.getloc()).widen(__num_get_base::__src,
1413227825Stheraven                                                    __num_get_base::__src + 26, __atoms);
1414249998Sdim    string __buf;
1415249998Sdim    __buf.resize(__buf.capacity());
1416249998Sdim    char* __a = &__buf[0];
1417227825Stheraven    char* __a_end = __a;
1418227825Stheraven    unsigned __g[__num_get_base::__num_get_buf_sz];
1419227825Stheraven    unsigned* __g_end = __g;
1420227825Stheraven    unsigned __dc = 0;
1421227825Stheraven    for (; __b != __e; ++__b)
1422249998Sdim    {
1423249998Sdim        if (__a_end - __a == __buf.size())
1424249998Sdim        {
1425249998Sdim            size_t __tmp = __buf.size();
1426249998Sdim            __buf.resize(2*__buf.size());
1427249998Sdim            __buf.resize(__buf.capacity());
1428249998Sdim            __a = &__buf[0];
1429249998Sdim            __a_end = __a + __tmp;
1430249998Sdim        }
1431227825Stheraven        if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
1432227825Stheraven                                    __thousands_sep, __grouping,
1433227825Stheraven                                    __g, __g_end, __atoms))
1434227825Stheraven            break;
1435249998Sdim    }
1436227825Stheraven    // Stage 3
1437227825Stheraven    __a[sizeof(__a)-1] = 0;
1438227825Stheraven#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
1439227825Stheraven    if (sscanf_l(__a, _LIBCPP_GET_C_LOCALE, "%p", &__v) != 1)
1440227825Stheraven#else
1441227825Stheraven    if (__sscanf_l(__a, __cloc(), "%p", &__v) != 1)
1442227825Stheraven#endif
1443227825Stheraven        __err = ios_base::failbit;
1444227825Stheraven    // EOF checked
1445227825Stheraven    if (__b == __e)
1446227825Stheraven        __err |= ios_base::eofbit;
1447227825Stheraven    return __b;
1448227825Stheraven}
1449227825Stheraven
1450242945Stheraven_LIBCPP_EXTERN_TEMPLATE(class num_get<char>)
1451242945Stheraven_LIBCPP_EXTERN_TEMPLATE(class num_get<wchar_t>)
1452227825Stheraven
1453227825Stheravenstruct __num_put_base
1454227825Stheraven{
1455227825Stheravenprotected:
1456227825Stheraven    static void __format_int(char* __fmt, const char* __len, bool __signd,
1457227825Stheraven                             ios_base::fmtflags __flags);
1458227825Stheraven    static bool __format_float(char* __fmt, const char* __len,
1459227825Stheraven                               ios_base::fmtflags __flags);
1460227825Stheraven    static char* __identify_padding(char* __nb, char* __ne,
1461227825Stheraven                                    const ios_base& __iob);
1462227825Stheraven};
1463227825Stheraven
1464227825Stheraventemplate <class _CharT>
1465227825Stheravenstruct __num_put
1466227825Stheraven    : protected __num_put_base
1467227825Stheraven{
1468227825Stheraven    static void __widen_and_group_int(char* __nb, char* __np, char* __ne,
1469227825Stheraven                                      _CharT* __ob, _CharT*& __op, _CharT*& __oe,
1470227825Stheraven                                      const locale& __loc);
1471227825Stheraven    static void __widen_and_group_float(char* __nb, char* __np, char* __ne,
1472227825Stheraven                                        _CharT* __ob, _CharT*& __op, _CharT*& __oe,
1473227825Stheraven                                        const locale& __loc);
1474227825Stheraven};
1475227825Stheraven
1476227825Stheraventemplate <class _CharT>
1477227825Stheravenvoid
1478227825Stheraven__num_put<_CharT>::__widen_and_group_int(char* __nb, char* __np, char* __ne,
1479227825Stheraven                                         _CharT* __ob, _CharT*& __op, _CharT*& __oe,
1480227825Stheraven                                         const locale& __loc)
1481227825Stheraven{
1482227825Stheraven    const ctype<_CharT>&    __ct = use_facet<ctype<_CharT> >   (__loc);
1483227825Stheraven    const numpunct<_CharT>& __npt = use_facet<numpunct<_CharT> >(__loc);
1484227825Stheraven    string __grouping = __npt.grouping();
1485227825Stheraven    if (__grouping.empty())
1486227825Stheraven    {
1487227825Stheraven        __ct.widen(__nb, __ne, __ob);
1488227825Stheraven        __oe = __ob + (__ne - __nb);
1489227825Stheraven    }
1490227825Stheraven    else
1491227825Stheraven    {
1492227825Stheraven        __oe = __ob;
1493227825Stheraven        char* __nf = __nb;
1494227825Stheraven        if (*__nf == '-' || *__nf == '+')
1495227825Stheraven            *__oe++ = __ct.widen(*__nf++);
1496227825Stheraven        if (__ne - __nf >= 2 && __nf[0] == '0' && (__nf[1] == 'x' ||
1497227825Stheraven                                                   __nf[1] == 'X'))
1498227825Stheraven        {
1499227825Stheraven            *__oe++ = __ct.widen(*__nf++);
1500227825Stheraven            *__oe++ = __ct.widen(*__nf++);
1501227825Stheraven        }
1502227825Stheraven        reverse(__nf, __ne);
1503227825Stheraven        _CharT __thousands_sep = __npt.thousands_sep();
1504227825Stheraven        unsigned __dc = 0;
1505227825Stheraven        unsigned __dg = 0;
1506227825Stheraven        for (char* __p = __nf; __p < __ne; ++__p)
1507227825Stheraven        {
1508227825Stheraven            if (static_cast<unsigned>(__grouping[__dg]) > 0 &&
1509227825Stheraven                __dc == static_cast<unsigned>(__grouping[__dg]))
1510227825Stheraven            {
1511227825Stheraven                *__oe++ = __thousands_sep;
1512227825Stheraven                __dc = 0;
1513227825Stheraven                if (__dg < __grouping.size()-1)
1514227825Stheraven                    ++__dg;
1515227825Stheraven            }
1516227825Stheraven            *__oe++ = __ct.widen(*__p);
1517227825Stheraven            ++__dc;
1518227825Stheraven        }
1519227825Stheraven        reverse(__ob + (__nf - __nb), __oe);
1520227825Stheraven    }
1521227825Stheraven    if (__np == __ne)
1522227825Stheraven        __op = __oe;
1523227825Stheraven    else
1524227825Stheraven        __op = __ob + (__np - __nb);
1525227825Stheraven}
1526227825Stheraven
1527227825Stheraventemplate <class _CharT>
1528227825Stheravenvoid
1529227825Stheraven__num_put<_CharT>::__widen_and_group_float(char* __nb, char* __np, char* __ne,
1530227825Stheraven                                           _CharT* __ob, _CharT*& __op, _CharT*& __oe,
1531227825Stheraven                                           const locale& __loc)
1532227825Stheraven{
1533227825Stheraven    const ctype<_CharT>&    __ct = use_facet<ctype<_CharT> >   (__loc);
1534227825Stheraven    const numpunct<_CharT>& __npt = use_facet<numpunct<_CharT> >(__loc);
1535227825Stheraven    string __grouping = __npt.grouping();
1536227825Stheraven    __oe = __ob;
1537227825Stheraven    char* __nf = __nb;
1538227825Stheraven    if (*__nf == '-' || *__nf == '+')
1539227825Stheraven        *__oe++ = __ct.widen(*__nf++);
1540227825Stheraven    char* __ns;
1541227825Stheraven    if (__ne - __nf >= 2 && __nf[0] == '0' && (__nf[1] == 'x' ||
1542227825Stheraven                                               __nf[1] == 'X'))
1543227825Stheraven    {
1544227825Stheraven        *__oe++ = __ct.widen(*__nf++);
1545227825Stheraven        *__oe++ = __ct.widen(*__nf++);
1546227825Stheraven        for (__ns = __nf; __ns < __ne; ++__ns)
1547227825Stheraven            if (!isxdigit_l(*__ns, _LIBCPP_GET_C_LOCALE))
1548227825Stheraven                break;
1549227825Stheraven    }
1550227825Stheraven    else
1551227825Stheraven    {
1552227825Stheraven        for (__ns = __nf; __ns < __ne; ++__ns)
1553227825Stheraven            if (!isdigit_l(*__ns, _LIBCPP_GET_C_LOCALE))
1554227825Stheraven                break;
1555227825Stheraven    }
1556227825Stheraven    if (__grouping.empty())
1557227825Stheraven    {
1558227825Stheraven        __ct.widen(__nf, __ns, __oe);
1559227825Stheraven        __oe += __ns - __nf;
1560227825Stheraven    }
1561227825Stheraven    else
1562227825Stheraven    {
1563227825Stheraven        reverse(__nf, __ns);
1564227825Stheraven        _CharT __thousands_sep = __npt.thousands_sep();
1565227825Stheraven        unsigned __dc = 0;
1566227825Stheraven        unsigned __dg = 0;
1567227825Stheraven        for (char* __p = __nf; __p < __ns; ++__p)
1568227825Stheraven        {
1569227825Stheraven            if (__grouping[__dg] > 0 && __dc == static_cast<unsigned>(__grouping[__dg]))
1570227825Stheraven            {
1571227825Stheraven                *__oe++ = __thousands_sep;
1572227825Stheraven                __dc = 0;
1573227825Stheraven                if (__dg < __grouping.size()-1)
1574227825Stheraven                    ++__dg;
1575227825Stheraven            }
1576227825Stheraven            *__oe++ = __ct.widen(*__p);
1577227825Stheraven            ++__dc;
1578227825Stheraven        }
1579227825Stheraven        reverse(__ob + (__nf - __nb), __oe);
1580227825Stheraven    }
1581227825Stheraven    for (__nf = __ns; __nf < __ne; ++__nf)
1582227825Stheraven    {
1583227825Stheraven        if (*__nf == '.')
1584227825Stheraven        {
1585227825Stheraven            *__oe++ = __npt.decimal_point();
1586227825Stheraven            ++__nf;
1587227825Stheraven            break;
1588227825Stheraven        }
1589227825Stheraven        else
1590227825Stheraven            *__oe++ = __ct.widen(*__nf);
1591227825Stheraven    }
1592227825Stheraven    __ct.widen(__nf, __ne, __oe);
1593227825Stheraven    __oe += __ne - __nf;
1594227825Stheraven    if (__np == __ne)
1595227825Stheraven        __op = __oe;
1596227825Stheraven    else
1597227825Stheraven        __op = __ob + (__np - __nb);
1598227825Stheraven}
1599227825Stheraven
1600242945Stheraven_LIBCPP_EXTERN_TEMPLATE(struct __num_put<char>)
1601242945Stheraven_LIBCPP_EXTERN_TEMPLATE(struct __num_put<wchar_t>)
1602227825Stheraven
1603227825Stheraventemplate <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
1604249998Sdimclass _LIBCPP_TYPE_VIS num_put
1605227825Stheraven    : public locale::facet,
1606227825Stheraven      private __num_put<_CharT>
1607227825Stheraven{
1608227825Stheravenpublic:
1609227825Stheraven    typedef _CharT char_type;
1610227825Stheraven    typedef _OutputIterator iter_type;
1611227825Stheraven
1612227825Stheraven    _LIBCPP_ALWAYS_INLINE
1613227825Stheraven    explicit num_put(size_t __refs = 0)
1614227825Stheraven        : locale::facet(__refs) {}
1615227825Stheraven
1616227825Stheraven    _LIBCPP_ALWAYS_INLINE
1617227825Stheraven    iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1618227825Stheraven                  bool __v) const
1619227825Stheraven    {
1620227825Stheraven        return do_put(__s, __iob, __fl, __v);
1621227825Stheraven    }
1622227825Stheraven
1623227825Stheraven    _LIBCPP_ALWAYS_INLINE
1624227825Stheraven    iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1625227825Stheraven                  long __v) const
1626227825Stheraven    {
1627227825Stheraven        return do_put(__s, __iob, __fl, __v);
1628227825Stheraven    }
1629227825Stheraven
1630227825Stheraven    _LIBCPP_ALWAYS_INLINE
1631227825Stheraven    iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1632227825Stheraven                  long long __v) const
1633227825Stheraven    {
1634227825Stheraven        return do_put(__s, __iob, __fl, __v);
1635227825Stheraven    }
1636227825Stheraven
1637227825Stheraven    _LIBCPP_ALWAYS_INLINE
1638227825Stheraven    iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1639227825Stheraven                  unsigned long __v) const
1640227825Stheraven    {
1641227825Stheraven        return do_put(__s, __iob, __fl, __v);
1642227825Stheraven    }
1643227825Stheraven
1644227825Stheraven    _LIBCPP_ALWAYS_INLINE
1645227825Stheraven    iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1646227825Stheraven                  unsigned long long __v) const
1647227825Stheraven    {
1648227825Stheraven        return do_put(__s, __iob, __fl, __v);
1649227825Stheraven    }
1650227825Stheraven
1651227825Stheraven    _LIBCPP_ALWAYS_INLINE
1652227825Stheraven    iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1653227825Stheraven                  double __v) const
1654227825Stheraven    {
1655227825Stheraven        return do_put(__s, __iob, __fl, __v);
1656227825Stheraven    }
1657227825Stheraven
1658227825Stheraven    _LIBCPP_ALWAYS_INLINE
1659227825Stheraven    iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1660227825Stheraven                  long double __v) const
1661227825Stheraven    {
1662227825Stheraven        return do_put(__s, __iob, __fl, __v);
1663227825Stheraven    }
1664227825Stheraven
1665227825Stheraven    _LIBCPP_ALWAYS_INLINE
1666227825Stheraven    iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1667227825Stheraven                  const void* __v) const
1668227825Stheraven    {
1669227825Stheraven        return do_put(__s, __iob, __fl, __v);
1670227825Stheraven    }
1671227825Stheraven
1672227825Stheraven    static locale::id id;
1673227825Stheraven
1674227825Stheravenprotected:
1675227825Stheraven    _LIBCPP_ALWAYS_INLINE
1676227825Stheraven    ~num_put() {}
1677227825Stheraven
1678227825Stheraven    virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1679227825Stheraven                             bool __v) const;
1680227825Stheraven    virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1681227825Stheraven                             long __v) const;
1682227825Stheraven    virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1683227825Stheraven                             long long __v) const;
1684227825Stheraven    virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1685227825Stheraven                             unsigned long) const;
1686227825Stheraven    virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1687227825Stheraven                             unsigned long long) const;
1688227825Stheraven    virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1689227825Stheraven                             double __v) const;
1690227825Stheraven    virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1691227825Stheraven                             long double __v) const;
1692227825Stheraven    virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1693227825Stheraven                             const void* __v) const;
1694227825Stheraven};
1695227825Stheraven
1696227825Stheraventemplate <class _CharT, class _OutputIterator>
1697227825Stheravenlocale::id
1698227825Stheravennum_put<_CharT, _OutputIterator>::id;
1699227825Stheraven
1700227825Stheraventemplate <class _CharT, class _OutputIterator>
1701227825Stheraven_LIBCPP_HIDDEN
1702227825Stheraven_OutputIterator
1703227825Stheraven__pad_and_output(_OutputIterator __s,
1704227825Stheraven                 const _CharT* __ob, const _CharT* __op, const _CharT* __oe,
1705227825Stheraven                 ios_base& __iob, _CharT __fl)
1706227825Stheraven{
1707227825Stheraven    streamsize __sz = __oe - __ob;
1708227825Stheraven    streamsize __ns = __iob.width();
1709227825Stheraven    if (__ns > __sz)
1710227825Stheraven        __ns -= __sz;
1711227825Stheraven    else
1712227825Stheraven        __ns = 0;
1713227825Stheraven    for (;__ob < __op; ++__ob, ++__s)
1714227825Stheraven        *__s = *__ob;
1715227825Stheraven    for (; __ns; --__ns, ++__s)
1716227825Stheraven        *__s = __fl;
1717227825Stheraven    for (; __ob < __oe; ++__ob, ++__s)
1718227825Stheraven        *__s = *__ob;
1719227825Stheraven    __iob.width(0);
1720227825Stheraven    return __s;
1721227825Stheraven}
1722227825Stheraven
1723243673Stheraven#if !defined(__APPLE__) || \
1724243673Stheraven    (defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED > __MAC_10_8) || \
1725243673Stheraven    (defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && __IPHONE_OS_VERSION_MIN_REQUIRED > __IPHONE_6_0)
1726243673Stheraven
1727241903Sdimtemplate <class _CharT, class _Traits>
1728241903Sdim_LIBCPP_HIDDEN
1729241903Sdimostreambuf_iterator<_CharT, _Traits>
1730241903Sdim__pad_and_output(ostreambuf_iterator<_CharT, _Traits> __s,
1731241903Sdim                 const _CharT* __ob, const _CharT* __op, const _CharT* __oe,
1732241903Sdim                 ios_base& __iob, _CharT __fl)
1733241903Sdim{
1734241903Sdim    if (__s.__sbuf_ == nullptr)
1735241903Sdim        return __s;
1736241903Sdim    streamsize __sz = __oe - __ob;
1737241903Sdim    streamsize __ns = __iob.width();
1738241903Sdim    if (__ns > __sz)
1739241903Sdim        __ns -= __sz;
1740241903Sdim    else
1741241903Sdim        __ns = 0;
1742241903Sdim    streamsize __np = __op - __ob;
1743241903Sdim    if (__np > 0)
1744241903Sdim    {
1745241903Sdim        if (__s.__sbuf_->sputn(__ob, __np) != __np)
1746241903Sdim        {
1747241903Sdim            __s.__sbuf_ = nullptr;
1748241903Sdim            return __s;
1749241903Sdim        }
1750241903Sdim    }
1751241903Sdim    if (__ns > 0)
1752241903Sdim    {
1753241903Sdim        basic_string<_CharT, _Traits> __sp(__ns, __fl);
1754241903Sdim        if (__s.__sbuf_->sputn(__sp.data(), __ns) != __ns)
1755241903Sdim        {
1756241903Sdim            __s.__sbuf_ = nullptr;
1757241903Sdim            return __s;
1758241903Sdim        }
1759241903Sdim    }
1760241903Sdim    __np = __oe - __op;
1761241903Sdim    if (__np > 0)
1762241903Sdim    {
1763241903Sdim        if (__s.__sbuf_->sputn(__op, __np) != __np)
1764241903Sdim        {
1765241903Sdim            __s.__sbuf_ = nullptr;
1766241903Sdim            return __s;
1767241903Sdim        }
1768241903Sdim    }
1769241903Sdim    __iob.width(0);
1770241903Sdim    return __s;
1771241903Sdim}
1772241903Sdim
1773243673Stheraven#endif
1774243673Stheraven
1775227825Stheraventemplate <class _CharT, class _OutputIterator>
1776227825Stheraven_OutputIterator
1777227825Stheravennum_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1778227825Stheraven                                         char_type __fl, bool __v) const
1779227825Stheraven{
1780227825Stheraven    if ((__iob.flags() & ios_base::boolalpha) == 0)
1781227825Stheraven        return do_put(__s, __iob, __fl, (unsigned long)__v);
1782227825Stheraven    const numpunct<char_type>& __np = use_facet<numpunct<char_type> >(__iob.getloc());
1783227825Stheraven    typedef typename numpunct<char_type>::string_type string_type;
1784227825Stheraven    string_type __nm = __v ? __np.truename() : __np.falsename();
1785227825Stheraven    for (typename string_type::iterator __i = __nm.begin(); __i != __nm.end(); ++__i, ++__s)
1786227825Stheraven        *__s = *__i;
1787227825Stheraven    return __s;
1788227825Stheraven}
1789227825Stheraven
1790227825Stheraventemplate <class _CharT, class _OutputIterator>
1791227825Stheraven_OutputIterator
1792227825Stheravennum_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1793227825Stheraven                                         char_type __fl, long __v) const
1794227825Stheraven{
1795227825Stheraven    // Stage 1 - Get number in narrow char
1796227825Stheraven    char __fmt[6] = {'%', 0};
1797227825Stheraven    const char* __len = "l";
1798227825Stheraven    this->__format_int(__fmt+1, __len, true, __iob.flags());
1799227825Stheraven    const unsigned __nbuf = (numeric_limits<long>::digits / 3)
1800227825Stheraven                          + ((numeric_limits<long>::digits % 3) != 0)
1801227825Stheraven                          + 1;
1802227825Stheraven    char __nar[__nbuf];
1803227825Stheraven#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
1804227825Stheraven    int __nc = sprintf_l(__nar, _LIBCPP_GET_C_LOCALE, __fmt, __v);
1805227825Stheraven#else
1806227825Stheraven    int __nc = __sprintf_l(__nar, __cloc(), __fmt, __v);
1807227825Stheraven#endif
1808227825Stheraven    char* __ne = __nar + __nc;
1809227825Stheraven    char* __np = this->__identify_padding(__nar, __ne, __iob);
1810227825Stheraven    // Stage 2 - Widen __nar while adding thousands separators
1811227825Stheraven    char_type __o[2*(__nbuf-1) - 1];
1812227825Stheraven    char_type* __op;  // pad here
1813227825Stheraven    char_type* __oe;  // end of output
1814227825Stheraven    this->__widen_and_group_int(__nar, __np, __ne, __o, __op, __oe, __iob.getloc());
1815227825Stheraven    // [__o, __oe) contains thousands_sep'd wide number
1816227825Stheraven    // Stage 3 & 4
1817227825Stheraven    return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1818227825Stheraven}
1819227825Stheraven
1820227825Stheraventemplate <class _CharT, class _OutputIterator>
1821227825Stheraven_OutputIterator
1822227825Stheravennum_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1823227825Stheraven                                         char_type __fl, long long __v) const
1824227825Stheraven{
1825227825Stheraven    // Stage 1 - Get number in narrow char
1826227825Stheraven    char __fmt[8] = {'%', 0};
1827227825Stheraven    const char* __len = "ll";
1828227825Stheraven    this->__format_int(__fmt+1, __len, true, __iob.flags());
1829227825Stheraven    const unsigned __nbuf = (numeric_limits<long long>::digits / 3)
1830227825Stheraven                          + ((numeric_limits<long long>::digits % 3) != 0)
1831227825Stheraven                          + 1;
1832227825Stheraven    char __nar[__nbuf];
1833227825Stheraven#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
1834227825Stheraven    int __nc = sprintf_l(__nar, _LIBCPP_GET_C_LOCALE, __fmt, __v);
1835227825Stheraven#else
1836227825Stheraven    int __nc = __sprintf_l(__nar, __cloc(), __fmt, __v);
1837227825Stheraven#endif
1838227825Stheraven    char* __ne = __nar + __nc;
1839227825Stheraven    char* __np = this->__identify_padding(__nar, __ne, __iob);
1840227825Stheraven    // Stage 2 - Widen __nar while adding thousands separators
1841227825Stheraven    char_type __o[2*(__nbuf-1) - 1];
1842227825Stheraven    char_type* __op;  // pad here
1843227825Stheraven    char_type* __oe;  // end of output
1844227825Stheraven    this->__widen_and_group_int(__nar, __np, __ne, __o, __op, __oe, __iob.getloc());
1845227825Stheraven    // [__o, __oe) contains thousands_sep'd wide number
1846227825Stheraven    // Stage 3 & 4
1847227825Stheraven    return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1848227825Stheraven}
1849227825Stheraven
1850227825Stheraventemplate <class _CharT, class _OutputIterator>
1851227825Stheraven_OutputIterator
1852227825Stheravennum_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1853227825Stheraven                                         char_type __fl, unsigned long __v) const
1854227825Stheraven{
1855227825Stheraven    // Stage 1 - Get number in narrow char
1856227825Stheraven    char __fmt[6] = {'%', 0};
1857227825Stheraven    const char* __len = "l";
1858227825Stheraven    this->__format_int(__fmt+1, __len, false, __iob.flags());
1859227825Stheraven    const unsigned __nbuf = (numeric_limits<unsigned long>::digits / 3)
1860227825Stheraven                          + ((numeric_limits<unsigned long>::digits % 3) != 0)
1861227825Stheraven                          + 1;
1862227825Stheraven    char __nar[__nbuf];
1863227825Stheraven#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
1864227825Stheraven    int __nc = sprintf_l(__nar, _LIBCPP_GET_C_LOCALE, __fmt, __v);
1865227825Stheraven#else
1866227825Stheraven    int __nc = __sprintf_l(__nar, __cloc(), __fmt, __v);
1867227825Stheraven#endif
1868227825Stheraven    char* __ne = __nar + __nc;
1869227825Stheraven    char* __np = this->__identify_padding(__nar, __ne, __iob);
1870227825Stheraven    // Stage 2 - Widen __nar while adding thousands separators
1871227825Stheraven    char_type __o[2*(__nbuf-1) - 1];
1872227825Stheraven    char_type* __op;  // pad here
1873227825Stheraven    char_type* __oe;  // end of output
1874227825Stheraven    this->__widen_and_group_int(__nar, __np, __ne, __o, __op, __oe, __iob.getloc());
1875227825Stheraven    // [__o, __oe) contains thousands_sep'd wide number
1876227825Stheraven    // Stage 3 & 4
1877227825Stheraven    return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1878227825Stheraven}
1879227825Stheraven
1880227825Stheraventemplate <class _CharT, class _OutputIterator>
1881227825Stheraven_OutputIterator
1882227825Stheravennum_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1883227825Stheraven                                         char_type __fl, unsigned long long __v) const
1884227825Stheraven{
1885227825Stheraven    // Stage 1 - Get number in narrow char
1886227825Stheraven    char __fmt[8] = {'%', 0};
1887227825Stheraven    const char* __len = "ll";
1888227825Stheraven    this->__format_int(__fmt+1, __len, false, __iob.flags());
1889227825Stheraven    const unsigned __nbuf = (numeric_limits<unsigned long long>::digits / 3)
1890227825Stheraven                          + ((numeric_limits<unsigned long long>::digits % 3) != 0)
1891227825Stheraven                          + 1;
1892227825Stheraven    char __nar[__nbuf];
1893227825Stheraven#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
1894227825Stheraven    int __nc = sprintf_l(__nar, _LIBCPP_GET_C_LOCALE, __fmt, __v);
1895227825Stheraven#else
1896227825Stheraven    int __nc = __sprintf_l(__nar, __cloc(), __fmt, __v);
1897227825Stheraven#endif
1898227825Stheraven    char* __ne = __nar + __nc;
1899227825Stheraven    char* __np = this->__identify_padding(__nar, __ne, __iob);
1900227825Stheraven    // Stage 2 - Widen __nar while adding thousands separators
1901227825Stheraven    char_type __o[2*(__nbuf-1) - 1];
1902227825Stheraven    char_type* __op;  // pad here
1903227825Stheraven    char_type* __oe;  // end of output
1904227825Stheraven    this->__widen_and_group_int(__nar, __np, __ne, __o, __op, __oe, __iob.getloc());
1905227825Stheraven    // [__o, __oe) contains thousands_sep'd wide number
1906227825Stheraven    // Stage 3 & 4
1907227825Stheraven    return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1908227825Stheraven}
1909227825Stheraven
1910227825Stheraventemplate <class _CharT, class _OutputIterator>
1911227825Stheraven_OutputIterator
1912227825Stheravennum_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1913227825Stheraven                                         char_type __fl, double __v) const
1914227825Stheraven{
1915227825Stheraven    // Stage 1 - Get number in narrow char
1916227825Stheraven    char __fmt[8] = {'%', 0};
1917227825Stheraven    const char* __len = "";
1918227825Stheraven    bool __specify_precision = this->__format_float(__fmt+1, __len, __iob.flags());
1919227825Stheraven    const unsigned __nbuf = 30;
1920227825Stheraven    char __nar[__nbuf];
1921227825Stheraven    char* __nb = __nar;
1922227825Stheraven    int __nc;
1923227825Stheraven    if (__specify_precision)
1924227825Stheraven#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
1925227825Stheraven        __nc = snprintf_l(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt,
1926227825Stheraven                                   (int)__iob.precision(), __v);
1927227825Stheraven#else
1928227825Stheraven        __nc = __snprintf_l(__nb, __nbuf, __cloc(), __fmt,
1929227825Stheraven                            (int)__iob.precision(), __v);
1930227825Stheraven#endif
1931227825Stheraven    else
1932227825Stheraven#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
1933227825Stheraven        __nc = snprintf_l(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt, __v);
1934227825Stheraven#else
1935227825Stheraven        __nc = __snprintf_l(__nb, __nbuf, __cloc(), __fmt, __v);
1936227825Stheraven#endif
1937227825Stheraven    unique_ptr<char, void(*)(void*)> __nbh(0, free);
1938227825Stheraven    if (__nc > static_cast<int>(__nbuf-1))
1939227825Stheraven    {
1940227825Stheraven        if (__specify_precision)
1941227825Stheraven#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
1942227825Stheraven            __nc = asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, (int)__iob.precision(), __v);
1943227825Stheraven#else
1944227825Stheraven            __nc = __asprintf_l(&__nb, __cloc(), __fmt,
1945227825Stheraven                              (int)__iob.precision(), __v);
1946227825Stheraven#endif
1947227825Stheraven        else
1948227825Stheraven#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
1949227825Stheraven            __nc = asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, __v);
1950227825Stheraven#else
1951227825Stheraven            __nc = __asprintf_l(&__nb, __cloc(), __fmt, (int)__iob.precision(), __v);
1952227825Stheraven#endif
1953227825Stheraven        if (__nb == 0)
1954227825Stheraven            __throw_bad_alloc();
1955227825Stheraven        __nbh.reset(__nb);
1956227825Stheraven    }
1957227825Stheraven    char* __ne = __nb + __nc;
1958227825Stheraven    char* __np = this->__identify_padding(__nb, __ne, __iob);
1959227825Stheraven    // Stage 2 - Widen __nar while adding thousands separators
1960227825Stheraven    char_type __o[2*(__nbuf-1) - 1];
1961227825Stheraven    char_type* __ob = __o;
1962227825Stheraven    unique_ptr<char_type, void(*)(void*)> __obh(0, free);
1963227825Stheraven    if (__nb != __nar)
1964227825Stheraven    {
1965232950Stheraven        __ob = (char_type*)malloc(2*static_cast<size_t>(__nc)*sizeof(char_type));
1966227825Stheraven        if (__ob == 0)
1967227825Stheraven            __throw_bad_alloc();
1968227825Stheraven        __obh.reset(__ob);
1969227825Stheraven    }
1970227825Stheraven    char_type* __op;  // pad here
1971227825Stheraven    char_type* __oe;  // end of output
1972227825Stheraven    this->__widen_and_group_float(__nb, __np, __ne, __ob, __op, __oe, __iob.getloc());
1973227825Stheraven    // [__o, __oe) contains thousands_sep'd wide number
1974227825Stheraven    // Stage 3 & 4
1975227825Stheraven    __s = __pad_and_output(__s, __ob, __op, __oe, __iob, __fl);
1976227825Stheraven    return __s;
1977227825Stheraven}
1978227825Stheraven
1979227825Stheraventemplate <class _CharT, class _OutputIterator>
1980227825Stheraven_OutputIterator
1981227825Stheravennum_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1982227825Stheraven                                         char_type __fl, long double __v) const
1983227825Stheraven{
1984227825Stheraven    // Stage 1 - Get number in narrow char
1985227825Stheraven    char __fmt[8] = {'%', 0};
1986227825Stheraven    const char* __len = "L";
1987227825Stheraven    bool __specify_precision = this->__format_float(__fmt+1, __len, __iob.flags());
1988227825Stheraven    const unsigned __nbuf = 30;
1989227825Stheraven    char __nar[__nbuf];
1990227825Stheraven    char* __nb = __nar;
1991227825Stheraven    int __nc;
1992227825Stheraven    if (__specify_precision)
1993227825Stheraven#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
1994227825Stheraven        __nc = snprintf_l(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt,
1995227825Stheraven                                   (int)__iob.precision(), __v);
1996227825Stheraven#else
1997227825Stheraven        __nc = __snprintf_l(__nb, __nbuf, __cloc(), __fmt,
1998227825Stheraven                            (int)__iob.precision(), __v);
1999227825Stheraven#endif
2000227825Stheraven    else
2001227825Stheraven#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
2002227825Stheraven        __nc = snprintf_l(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt, __v);
2003227825Stheraven#else
2004227825Stheraven        __nc = __snprintf_l(__nb, __nbuf, __cloc(), __fmt, __v);
2005227825Stheraven#endif
2006227825Stheraven    unique_ptr<char, void(*)(void*)> __nbh(0, free);
2007227825Stheraven    if (__nc > static_cast<int>(__nbuf-1))
2008227825Stheraven    {
2009227825Stheraven        if (__specify_precision)
2010227825Stheraven#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
2011227825Stheraven            __nc = asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, (int)__iob.precision(), __v);
2012227825Stheraven#else
2013227825Stheraven            __nc = __asprintf_l(&__nb, __cloc(), __fmt,
2014227825Stheraven                              (int)__iob.precision(), __v);
2015227825Stheraven#endif
2016227825Stheraven        else
2017227825Stheraven#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
2018227825Stheraven            __nc = asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, __v);
2019227825Stheraven#else
2020227825Stheraven            __nc = __asprintf_l(&__nb, __cloc(), __fmt, __v);
2021227825Stheraven#endif
2022227825Stheraven        if (__nb == 0)
2023227825Stheraven            __throw_bad_alloc();
2024227825Stheraven        __nbh.reset(__nb);
2025227825Stheraven    }
2026227825Stheraven    char* __ne = __nb + __nc;
2027227825Stheraven    char* __np = this->__identify_padding(__nb, __ne, __iob);
2028227825Stheraven    // Stage 2 - Widen __nar while adding thousands separators
2029227825Stheraven    char_type __o[2*(__nbuf-1) - 1];
2030227825Stheraven    char_type* __ob = __o;
2031227825Stheraven    unique_ptr<char_type, void(*)(void*)> __obh(0, free);
2032227825Stheraven    if (__nb != __nar)
2033227825Stheraven    {
2034232950Stheraven        __ob = (char_type*)malloc(2*static_cast<size_t>(__nc)*sizeof(char_type));
2035227825Stheraven        if (__ob == 0)
2036227825Stheraven            __throw_bad_alloc();
2037227825Stheraven        __obh.reset(__ob);
2038227825Stheraven    }
2039227825Stheraven    char_type* __op;  // pad here
2040227825Stheraven    char_type* __oe;  // end of output
2041227825Stheraven    this->__widen_and_group_float(__nb, __np, __ne, __ob, __op, __oe, __iob.getloc());
2042227825Stheraven    // [__o, __oe) contains thousands_sep'd wide number
2043227825Stheraven    // Stage 3 & 4
2044227825Stheraven    __s = __pad_and_output(__s, __ob, __op, __oe, __iob, __fl);
2045227825Stheraven    return __s;
2046227825Stheraven}
2047227825Stheraven
2048227825Stheraventemplate <class _CharT, class _OutputIterator>
2049227825Stheraven_OutputIterator
2050227825Stheravennum_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
2051227825Stheraven                                         char_type __fl, const void* __v) const
2052227825Stheraven{
2053227825Stheraven    // Stage 1 - Get pointer in narrow char
2054227825Stheraven    char __fmt[6] = "%p";
2055227825Stheraven    const unsigned __nbuf = 20;
2056227825Stheraven    char __nar[__nbuf];
2057227825Stheraven#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
2058227825Stheraven    int __nc = sprintf_l(__nar, _LIBCPP_GET_C_LOCALE, __fmt, __v);
2059227825Stheraven#else
2060227825Stheraven    int __nc = __sprintf_l(__nar, __cloc(), __fmt, __v);
2061227825Stheraven#endif
2062227825Stheraven    char* __ne = __nar + __nc;
2063227825Stheraven    char* __np = this->__identify_padding(__nar, __ne, __iob);
2064227825Stheraven    // Stage 2 - Widen __nar
2065227825Stheraven    char_type __o[2*(__nbuf-1) - 1];
2066227825Stheraven    char_type* __op;  // pad here
2067227825Stheraven    char_type* __oe;  // end of output
2068227825Stheraven    const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2069227825Stheraven    __ct.widen(__nar, __ne, __o);
2070227825Stheraven    __oe = __o + (__ne - __nar);
2071227825Stheraven    if (__np == __ne)
2072227825Stheraven        __op = __oe;
2073227825Stheraven    else
2074227825Stheraven        __op = __o + (__np - __nar);
2075227825Stheraven    // [__o, __oe) contains wide number
2076227825Stheraven    // Stage 3 & 4
2077227825Stheraven    return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
2078227825Stheraven}
2079227825Stheraven
2080242945Stheraven_LIBCPP_EXTERN_TEMPLATE(class num_put<char>)
2081242945Stheraven_LIBCPP_EXTERN_TEMPLATE(class num_put<wchar_t>)
2082227825Stheraven
2083227825Stheraventemplate <class _CharT, class _InputIterator>
2084227825Stheraven_LIBCPP_HIDDEN
2085227825Stheravenint
2086227825Stheraven__get_up_to_n_digits(_InputIterator& __b, _InputIterator __e,
2087227825Stheraven                     ios_base::iostate& __err, const ctype<_CharT>& __ct, int __n)
2088227825Stheraven{
2089227825Stheraven    // Precondition:  __n >= 1
2090227825Stheraven    if (__b == __e)
2091227825Stheraven    {
2092227825Stheraven        __err |= ios_base::eofbit | ios_base::failbit;
2093227825Stheraven        return 0;
2094227825Stheraven    }
2095227825Stheraven    // get first digit
2096227825Stheraven    _CharT __c = *__b;
2097227825Stheraven    if (!__ct.is(ctype_base::digit, __c))
2098227825Stheraven    {
2099227825Stheraven        __err |= ios_base::failbit;
2100227825Stheraven        return 0;
2101227825Stheraven    }
2102227825Stheraven    int __r = __ct.narrow(__c, 0) - '0';
2103227825Stheraven    for (++__b, --__n; __b != __e && __n > 0; ++__b, --__n)
2104227825Stheraven    {
2105227825Stheraven        // get next digit
2106227825Stheraven        __c = *__b;
2107227825Stheraven        if (!__ct.is(ctype_base::digit, __c))
2108227825Stheraven            return __r;
2109227825Stheraven        __r = __r * 10 + __ct.narrow(__c, 0) - '0';
2110227825Stheraven    }
2111227825Stheraven    if (__b == __e)
2112227825Stheraven        __err |= ios_base::eofbit;
2113227825Stheraven    return __r;
2114227825Stheraven}
2115227825Stheraven
2116249998Sdimclass _LIBCPP_TYPE_VIS time_base
2117227825Stheraven{
2118227825Stheravenpublic:
2119227825Stheraven    enum dateorder {no_order, dmy, mdy, ymd, ydm};
2120227825Stheraven};
2121227825Stheraven
2122227825Stheraventemplate <class _CharT>
2123227825Stheravenclass __time_get_c_storage  // purposefully not decorated
2124227825Stheraven{
2125227825Stheravenprotected:
2126227825Stheraven    typedef basic_string<_CharT> string_type;
2127227825Stheraven
2128227825Stheraven    virtual const string_type* __weeks() const;
2129227825Stheraven    virtual const string_type* __months() const;
2130227825Stheraven    virtual const string_type* __am_pm() const;
2131227825Stheraven    virtual const string_type& __c() const;
2132227825Stheraven    virtual const string_type& __r() const;
2133227825Stheraven    virtual const string_type& __x() const;
2134227825Stheraven    virtual const string_type& __X() const;
2135227825Stheraven};
2136227825Stheraven
2137227825Stheraventemplate <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
2138249998Sdimclass _LIBCPP_TYPE_VIS time_get
2139227825Stheraven    : public locale::facet,
2140227825Stheraven      public time_base,
2141227825Stheraven      private __time_get_c_storage<_CharT>
2142227825Stheraven{
2143227825Stheravenpublic:
2144227825Stheraven    typedef _CharT                  char_type;
2145227825Stheraven    typedef _InputIterator          iter_type;
2146227825Stheraven    typedef time_base::dateorder    dateorder;
2147227825Stheraven    typedef basic_string<char_type> string_type;
2148227825Stheraven
2149227825Stheraven    _LIBCPP_ALWAYS_INLINE
2150227825Stheraven    explicit time_get(size_t __refs = 0)
2151227825Stheraven        : locale::facet(__refs) {}
2152227825Stheraven
2153227825Stheraven    _LIBCPP_ALWAYS_INLINE
2154227825Stheraven    dateorder date_order() const
2155227825Stheraven    {
2156227825Stheraven        return this->do_date_order();
2157227825Stheraven    }
2158227825Stheraven
2159227825Stheraven    _LIBCPP_ALWAYS_INLINE
2160227825Stheraven    iter_type get_time(iter_type __b, iter_type __e, ios_base& __iob,
2161227825Stheraven                       ios_base::iostate& __err, tm* __tm) const
2162227825Stheraven    {
2163227825Stheraven        return do_get_time(__b, __e, __iob, __err, __tm);
2164227825Stheraven    }
2165227825Stheraven
2166227825Stheraven    _LIBCPP_ALWAYS_INLINE
2167227825Stheraven    iter_type get_date(iter_type __b, iter_type __e, ios_base& __iob,
2168227825Stheraven                       ios_base::iostate& __err, tm* __tm) const
2169227825Stheraven    {
2170227825Stheraven        return do_get_date(__b, __e, __iob, __err, __tm);
2171227825Stheraven    }
2172227825Stheraven
2173227825Stheraven    _LIBCPP_ALWAYS_INLINE
2174227825Stheraven    iter_type get_weekday(iter_type __b, iter_type __e, ios_base& __iob,
2175227825Stheraven                          ios_base::iostate& __err, tm* __tm) const
2176227825Stheraven    {
2177227825Stheraven        return do_get_weekday(__b, __e, __iob, __err, __tm);
2178227825Stheraven    }
2179227825Stheraven
2180227825Stheraven    _LIBCPP_ALWAYS_INLINE
2181227825Stheraven    iter_type get_monthname(iter_type __b, iter_type __e, ios_base& __iob,
2182227825Stheraven                            ios_base::iostate& __err, tm* __tm) const
2183227825Stheraven    {
2184227825Stheraven        return do_get_monthname(__b, __e, __iob, __err, __tm);
2185227825Stheraven    }
2186227825Stheraven
2187227825Stheraven    _LIBCPP_ALWAYS_INLINE
2188227825Stheraven    iter_type get_year(iter_type __b, iter_type __e, ios_base& __iob,
2189227825Stheraven                       ios_base::iostate& __err, tm* __tm) const
2190227825Stheraven    {
2191227825Stheraven        return do_get_year(__b, __e, __iob, __err, __tm);
2192227825Stheraven    }
2193227825Stheraven
2194227825Stheraven    _LIBCPP_ALWAYS_INLINE
2195227825Stheraven    iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
2196227825Stheraven                  ios_base::iostate& __err, tm *__tm,
2197227825Stheraven                  char __fmt, char __mod = 0) const
2198227825Stheraven    {
2199227825Stheraven        return do_get(__b, __e, __iob, __err, __tm, __fmt, __mod);
2200227825Stheraven    }
2201227825Stheraven
2202227825Stheraven    iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
2203227825Stheraven                  ios_base::iostate& __err, tm* __tm,
2204227825Stheraven                  const char_type* __fmtb, const char_type* __fmte) const;
2205227825Stheraven
2206227825Stheraven    static locale::id id;
2207227825Stheraven
2208227825Stheravenprotected:
2209227825Stheraven    _LIBCPP_ALWAYS_INLINE
2210227825Stheraven    ~time_get() {}
2211227825Stheraven
2212227825Stheraven    virtual dateorder do_date_order() const;
2213227825Stheraven    virtual iter_type do_get_time(iter_type __b, iter_type __e, ios_base& __iob,
2214227825Stheraven                                  ios_base::iostate& __err, tm* __tm) const;
2215227825Stheraven    virtual iter_type do_get_date(iter_type __b, iter_type __e, ios_base& __iob,
2216227825Stheraven                                  ios_base::iostate& __err, tm* __tm) const;
2217227825Stheraven    virtual iter_type do_get_weekday(iter_type __b, iter_type __e, ios_base& __iob,
2218227825Stheraven                                     ios_base::iostate& __err, tm* __tm) const;
2219227825Stheraven    virtual iter_type do_get_monthname(iter_type __b, iter_type __e, ios_base& __iob,
2220227825Stheraven                                       ios_base::iostate& __err, tm* __tm) const;
2221227825Stheraven    virtual iter_type do_get_year(iter_type __b, iter_type __e, ios_base& __iob,
2222227825Stheraven                                  ios_base::iostate& __err, tm* __tm) const;
2223227825Stheraven    virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
2224227825Stheraven                             ios_base::iostate& __err, tm* __tm,
2225227825Stheraven                             char __fmt, char __mod) const;
2226227825Stheravenprivate:
2227227825Stheraven    void __get_white_space(iter_type& __b, iter_type __e,
2228227825Stheraven                           ios_base::iostate& __err, const ctype<char_type>& __ct) const;
2229227825Stheraven    void __get_percent(iter_type& __b, iter_type __e, ios_base::iostate& __err,
2230227825Stheraven                       const ctype<char_type>& __ct) const;
2231227825Stheraven
2232227825Stheraven    void __get_weekdayname(int& __m,
2233227825Stheraven                           iter_type& __b, iter_type __e,
2234227825Stheraven                           ios_base::iostate& __err,
2235227825Stheraven                           const ctype<char_type>& __ct) const;
2236227825Stheraven    void __get_monthname(int& __m,
2237227825Stheraven                         iter_type& __b, iter_type __e,
2238227825Stheraven                         ios_base::iostate& __err,
2239227825Stheraven                         const ctype<char_type>& __ct) const;
2240227825Stheraven    void __get_day(int& __d,
2241227825Stheraven                   iter_type& __b, iter_type __e,
2242227825Stheraven                   ios_base::iostate& __err,
2243227825Stheraven                   const ctype<char_type>& __ct) const;
2244227825Stheraven    void __get_month(int& __m,
2245227825Stheraven                     iter_type& __b, iter_type __e,
2246227825Stheraven                     ios_base::iostate& __err,
2247227825Stheraven                     const ctype<char_type>& __ct) const;
2248227825Stheraven    void __get_year(int& __y,
2249227825Stheraven                   iter_type& __b, iter_type __e,
2250227825Stheraven                   ios_base::iostate& __err,
2251227825Stheraven                   const ctype<char_type>& __ct) const;
2252227825Stheraven    void __get_year4(int& __y,
2253227825Stheraven                    iter_type& __b, iter_type __e,
2254227825Stheraven                    ios_base::iostate& __err,
2255227825Stheraven                    const ctype<char_type>& __ct) const;
2256227825Stheraven    void __get_hour(int& __d,
2257227825Stheraven                    iter_type& __b, iter_type __e,
2258227825Stheraven                    ios_base::iostate& __err,
2259227825Stheraven                    const ctype<char_type>& __ct) const;
2260227825Stheraven    void __get_12_hour(int& __h,
2261227825Stheraven                       iter_type& __b, iter_type __e,
2262227825Stheraven                       ios_base::iostate& __err,
2263227825Stheraven                       const ctype<char_type>& __ct) const;
2264227825Stheraven    void __get_am_pm(int& __h,
2265227825Stheraven                     iter_type& __b, iter_type __e,
2266227825Stheraven                     ios_base::iostate& __err,
2267227825Stheraven                     const ctype<char_type>& __ct) const;
2268227825Stheraven    void __get_minute(int& __m,
2269227825Stheraven                      iter_type& __b, iter_type __e,
2270227825Stheraven                      ios_base::iostate& __err,
2271227825Stheraven                      const ctype<char_type>& __ct) const;
2272227825Stheraven    void __get_second(int& __s,
2273227825Stheraven                      iter_type& __b, iter_type __e,
2274227825Stheraven                      ios_base::iostate& __err,
2275227825Stheraven                      const ctype<char_type>& __ct) const;
2276227825Stheraven    void __get_weekday(int& __w,
2277227825Stheraven                       iter_type& __b, iter_type __e,
2278227825Stheraven                       ios_base::iostate& __err,
2279227825Stheraven                       const ctype<char_type>& __ct) const;
2280227825Stheraven    void __get_day_year_num(int& __w,
2281227825Stheraven                            iter_type& __b, iter_type __e,
2282227825Stheraven                            ios_base::iostate& __err,
2283227825Stheraven                            const ctype<char_type>& __ct) const;
2284227825Stheraven};
2285227825Stheraven
2286227825Stheraventemplate <class _CharT, class _InputIterator>
2287227825Stheravenlocale::id
2288227825Stheraventime_get<_CharT, _InputIterator>::id;
2289227825Stheraven
2290227825Stheraven// time_get primatives
2291227825Stheraven
2292227825Stheraventemplate <class _CharT, class _InputIterator>
2293227825Stheravenvoid
2294227825Stheraventime_get<_CharT, _InputIterator>::__get_weekdayname(int& __w,
2295227825Stheraven                                                    iter_type& __b, iter_type __e,
2296227825Stheraven                                                    ios_base::iostate& __err,
2297227825Stheraven                                                    const ctype<char_type>& __ct) const
2298227825Stheraven{
2299227825Stheraven    // Note:  ignoring case comes from the POSIX strptime spec
2300227825Stheraven    const string_type* __wk = this->__weeks();
2301232950Stheraven    ptrdiff_t __i = __scan_keyword(__b, __e, __wk, __wk+14, __ct, __err, false) - __wk;
2302227825Stheraven    if (__i < 14)
2303227825Stheraven        __w = __i % 7;
2304227825Stheraven}
2305227825Stheraven
2306227825Stheraventemplate <class _CharT, class _InputIterator>
2307227825Stheravenvoid
2308227825Stheraventime_get<_CharT, _InputIterator>::__get_monthname(int& __m,
2309227825Stheraven                                                  iter_type& __b, iter_type __e,
2310227825Stheraven                                                  ios_base::iostate& __err,
2311227825Stheraven                                                  const ctype<char_type>& __ct) const
2312227825Stheraven{
2313227825Stheraven    // Note:  ignoring case comes from the POSIX strptime spec
2314227825Stheraven    const string_type* __month = this->__months();
2315232950Stheraven    ptrdiff_t __i = __scan_keyword(__b, __e, __month, __month+24, __ct, __err, false) - __month;
2316227825Stheraven    if (__i < 24)
2317227825Stheraven        __m = __i % 12;
2318227825Stheraven}
2319227825Stheraven
2320227825Stheraventemplate <class _CharT, class _InputIterator>
2321227825Stheravenvoid
2322227825Stheraventime_get<_CharT, _InputIterator>::__get_day(int& __d,
2323227825Stheraven                                            iter_type& __b, iter_type __e,
2324227825Stheraven                                            ios_base::iostate& __err,
2325227825Stheraven                                            const ctype<char_type>& __ct) const
2326227825Stheraven{
2327227825Stheraven    int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
2328227825Stheraven    if (!(__err & ios_base::failbit) && 1 <= __t && __t <= 31)
2329227825Stheraven        __d = __t;
2330227825Stheraven    else
2331227825Stheraven        __err |= ios_base::failbit;
2332227825Stheraven}
2333227825Stheraven
2334227825Stheraventemplate <class _CharT, class _InputIterator>
2335227825Stheravenvoid
2336227825Stheraventime_get<_CharT, _InputIterator>::__get_month(int& __m,
2337227825Stheraven                                              iter_type& __b, iter_type __e,
2338227825Stheraven                                              ios_base::iostate& __err,
2339227825Stheraven                                              const ctype<char_type>& __ct) const
2340227825Stheraven{
2341227825Stheraven    int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2) - 1;
2342227825Stheraven    if (!(__err & ios_base::failbit) && __t <= 11)
2343227825Stheraven        __m = __t;
2344227825Stheraven    else
2345227825Stheraven        __err |= ios_base::failbit;
2346227825Stheraven}
2347227825Stheraven
2348227825Stheraventemplate <class _CharT, class _InputIterator>
2349227825Stheravenvoid
2350227825Stheraventime_get<_CharT, _InputIterator>::__get_year(int& __y,
2351227825Stheraven                                             iter_type& __b, iter_type __e,
2352227825Stheraven                                             ios_base::iostate& __err,
2353227825Stheraven                                             const ctype<char_type>& __ct) const
2354227825Stheraven{
2355227825Stheraven    int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 4);
2356227825Stheraven    if (!(__err & ios_base::failbit))
2357227825Stheraven    {
2358227825Stheraven        if (__t < 69)
2359227825Stheraven            __t += 2000;
2360227825Stheraven        else if (69 <= __t && __t <= 99)
2361227825Stheraven            __t += 1900;
2362227825Stheraven        __y = __t - 1900;
2363227825Stheraven    }
2364227825Stheraven}
2365227825Stheraven
2366227825Stheraventemplate <class _CharT, class _InputIterator>
2367227825Stheravenvoid
2368227825Stheraventime_get<_CharT, _InputIterator>::__get_year4(int& __y,
2369227825Stheraven                                              iter_type& __b, iter_type __e,
2370227825Stheraven                                              ios_base::iostate& __err,
2371227825Stheraven                                              const ctype<char_type>& __ct) const
2372227825Stheraven{
2373227825Stheraven    int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 4);
2374227825Stheraven    if (!(__err & ios_base::failbit))
2375227825Stheraven        __y = __t - 1900;
2376227825Stheraven}
2377227825Stheraven
2378227825Stheraventemplate <class _CharT, class _InputIterator>
2379227825Stheravenvoid
2380227825Stheraventime_get<_CharT, _InputIterator>::__get_hour(int& __h,
2381227825Stheraven                                             iter_type& __b, iter_type __e,
2382227825Stheraven                                             ios_base::iostate& __err,
2383227825Stheraven                                             const ctype<char_type>& __ct) const
2384227825Stheraven{
2385227825Stheraven    int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
2386227825Stheraven    if (!(__err & ios_base::failbit) && __t <= 23)
2387227825Stheraven        __h = __t;
2388227825Stheraven    else
2389227825Stheraven        __err |= ios_base::failbit;
2390227825Stheraven}
2391227825Stheraven
2392227825Stheraventemplate <class _CharT, class _InputIterator>
2393227825Stheravenvoid
2394227825Stheraventime_get<_CharT, _InputIterator>::__get_12_hour(int& __h,
2395227825Stheraven                                                iter_type& __b, iter_type __e,
2396227825Stheraven                                                ios_base::iostate& __err,
2397227825Stheraven                                                const ctype<char_type>& __ct) const
2398227825Stheraven{
2399227825Stheraven    int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
2400227825Stheraven    if (!(__err & ios_base::failbit) && 1 <= __t && __t <= 12)
2401227825Stheraven        __h = __t;
2402227825Stheraven    else
2403227825Stheraven        __err |= ios_base::failbit;
2404227825Stheraven}
2405227825Stheraven
2406227825Stheraventemplate <class _CharT, class _InputIterator>
2407227825Stheravenvoid
2408227825Stheraventime_get<_CharT, _InputIterator>::__get_minute(int& __m,
2409227825Stheraven                                               iter_type& __b, iter_type __e,
2410227825Stheraven                                               ios_base::iostate& __err,
2411227825Stheraven                                               const ctype<char_type>& __ct) const
2412227825Stheraven{
2413227825Stheraven    int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
2414227825Stheraven    if (!(__err & ios_base::failbit) && __t <= 59)
2415227825Stheraven        __m = __t;
2416227825Stheraven    else
2417227825Stheraven        __err |= ios_base::failbit;
2418227825Stheraven}
2419227825Stheraven
2420227825Stheraventemplate <class _CharT, class _InputIterator>
2421227825Stheravenvoid
2422227825Stheraventime_get<_CharT, _InputIterator>::__get_second(int& __s,
2423227825Stheraven                                               iter_type& __b, iter_type __e,
2424227825Stheraven                                               ios_base::iostate& __err,
2425227825Stheraven                                               const ctype<char_type>& __ct) const
2426227825Stheraven{
2427227825Stheraven    int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
2428227825Stheraven    if (!(__err & ios_base::failbit) && __t <= 60)
2429227825Stheraven        __s = __t;
2430227825Stheraven    else
2431227825Stheraven        __err |= ios_base::failbit;
2432227825Stheraven}
2433227825Stheraven
2434227825Stheraventemplate <class _CharT, class _InputIterator>
2435227825Stheravenvoid
2436227825Stheraventime_get<_CharT, _InputIterator>::__get_weekday(int& __w,
2437227825Stheraven                                                iter_type& __b, iter_type __e,
2438227825Stheraven                                                ios_base::iostate& __err,
2439227825Stheraven                                                const ctype<char_type>& __ct) const
2440227825Stheraven{
2441227825Stheraven    int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 1);
2442227825Stheraven    if (!(__err & ios_base::failbit) && __t <= 6)
2443227825Stheraven        __w = __t;
2444227825Stheraven    else
2445227825Stheraven        __err |= ios_base::failbit;
2446227825Stheraven}
2447227825Stheraven
2448227825Stheraventemplate <class _CharT, class _InputIterator>
2449227825Stheravenvoid
2450227825Stheraventime_get<_CharT, _InputIterator>::__get_day_year_num(int& __d,
2451227825Stheraven                                                     iter_type& __b, iter_type __e,
2452227825Stheraven                                                     ios_base::iostate& __err,
2453227825Stheraven                                                     const ctype<char_type>& __ct) const
2454227825Stheraven{
2455227825Stheraven    int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 3);
2456227825Stheraven    if (!(__err & ios_base::failbit) && __t <= 365)
2457227825Stheraven        __d = __t;
2458227825Stheraven    else
2459227825Stheraven        __err |= ios_base::failbit;
2460227825Stheraven}
2461227825Stheraven
2462227825Stheraventemplate <class _CharT, class _InputIterator>
2463227825Stheravenvoid
2464227825Stheraventime_get<_CharT, _InputIterator>::__get_white_space(iter_type& __b, iter_type __e,
2465227825Stheraven                                                    ios_base::iostate& __err,
2466227825Stheraven                                                    const ctype<char_type>& __ct) const
2467227825Stheraven{
2468227825Stheraven    for (; __b != __e && __ct.is(ctype_base::space, *__b); ++__b)
2469227825Stheraven        ;
2470227825Stheraven    if (__b == __e)
2471227825Stheraven        __err |= ios_base::eofbit;
2472227825Stheraven}
2473227825Stheraven
2474227825Stheraventemplate <class _CharT, class _InputIterator>
2475227825Stheravenvoid
2476227825Stheraventime_get<_CharT, _InputIterator>::__get_am_pm(int& __h,
2477227825Stheraven                                              iter_type& __b, iter_type __e,
2478227825Stheraven                                              ios_base::iostate& __err,
2479227825Stheraven                                              const ctype<char_type>& __ct) const
2480227825Stheraven{
2481227825Stheraven    const string_type* __ap = this->__am_pm();
2482227825Stheraven    if (__ap[0].size() + __ap[1].size() == 0)
2483227825Stheraven    {
2484227825Stheraven        __err |= ios_base::failbit;
2485227825Stheraven        return;
2486227825Stheraven    }
2487232950Stheraven    ptrdiff_t __i = __scan_keyword(__b, __e, __ap, __ap+2, __ct, __err, false) - __ap;
2488227825Stheraven    if (__i == 0 && __h == 12)
2489227825Stheraven        __h = 0;
2490227825Stheraven    else if (__i == 1 && __h < 12)
2491227825Stheraven        __h += 12;
2492227825Stheraven}
2493227825Stheraven
2494227825Stheraventemplate <class _CharT, class _InputIterator>
2495227825Stheravenvoid
2496227825Stheraventime_get<_CharT, _InputIterator>::__get_percent(iter_type& __b, iter_type __e,
2497227825Stheraven                                                ios_base::iostate& __err,
2498227825Stheraven                                                const ctype<char_type>& __ct) const
2499227825Stheraven{
2500227825Stheraven    if (__b == __e)
2501227825Stheraven    {
2502227825Stheraven        __err |= ios_base::eofbit | ios_base::failbit;
2503227825Stheraven        return;
2504227825Stheraven    }
2505227825Stheraven    if (__ct.narrow(*__b, 0) != '%')
2506227825Stheraven        __err |= ios_base::failbit;
2507227825Stheraven    else if(++__b == __e)
2508227825Stheraven        __err |= ios_base::eofbit;
2509227825Stheraven}
2510227825Stheraven
2511227825Stheraven// time_get end primatives
2512227825Stheraven
2513227825Stheraventemplate <class _CharT, class _InputIterator>
2514227825Stheraven_InputIterator
2515227825Stheraventime_get<_CharT, _InputIterator>::get(iter_type __b, iter_type __e,
2516227825Stheraven                                      ios_base& __iob,
2517227825Stheraven                                      ios_base::iostate& __err, tm* __tm,
2518227825Stheraven                                      const char_type* __fmtb, const char_type* __fmte) const
2519227825Stheraven{
2520227825Stheraven    const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2521227825Stheraven    __err = ios_base::goodbit;
2522227825Stheraven    while (__fmtb != __fmte && __err == ios_base::goodbit)
2523227825Stheraven    {
2524227825Stheraven        if (__b == __e)
2525227825Stheraven        {
2526227825Stheraven            __err = ios_base::failbit;
2527227825Stheraven            break;
2528227825Stheraven        }
2529227825Stheraven        if (__ct.narrow(*__fmtb, 0) == '%')
2530227825Stheraven        {
2531227825Stheraven            if (++__fmtb == __fmte)
2532227825Stheraven            {
2533227825Stheraven                __err = ios_base::failbit;
2534227825Stheraven                break;
2535227825Stheraven            }
2536227825Stheraven            char __cmd = __ct.narrow(*__fmtb, 0);
2537227825Stheraven            char __opt = '\0';
2538227825Stheraven            if (__cmd == 'E' || __cmd == '0')
2539227825Stheraven            {
2540227825Stheraven                if (++__fmtb == __fmte)
2541227825Stheraven                {
2542227825Stheraven                    __err = ios_base::failbit;
2543227825Stheraven                    break;
2544227825Stheraven                }
2545227825Stheraven                __opt = __cmd;
2546227825Stheraven                __cmd = __ct.narrow(*__fmtb, 0);
2547227825Stheraven            }
2548227825Stheraven            __b = do_get(__b, __e, __iob, __err, __tm, __cmd, __opt);
2549227825Stheraven            ++__fmtb;
2550227825Stheraven        }
2551227825Stheraven        else if (__ct.is(ctype_base::space, *__fmtb))
2552227825Stheraven        {
2553227825Stheraven            for (++__fmtb; __fmtb != __fmte && __ct.is(ctype_base::space, *__fmtb); ++__fmtb)
2554227825Stheraven                ;
2555227825Stheraven            for (        ;    __b != __e    && __ct.is(ctype_base::space, *__b);    ++__b)
2556227825Stheraven                ;
2557227825Stheraven        }
2558227825Stheraven        else if (__ct.toupper(*__b) == __ct.toupper(*__fmtb))
2559227825Stheraven        {
2560227825Stheraven            ++__b;
2561227825Stheraven            ++__fmtb;
2562227825Stheraven        }
2563227825Stheraven        else
2564227825Stheraven            __err = ios_base::failbit;
2565227825Stheraven    }
2566227825Stheraven    if (__b == __e)
2567227825Stheraven        __err |= ios_base::eofbit;
2568227825Stheraven    return __b;
2569227825Stheraven}
2570227825Stheraven
2571227825Stheraventemplate <class _CharT, class _InputIterator>
2572227825Stheraventypename time_get<_CharT, _InputIterator>::dateorder
2573227825Stheraventime_get<_CharT, _InputIterator>::do_date_order() const
2574227825Stheraven{
2575227825Stheraven    return mdy;
2576227825Stheraven}
2577227825Stheraven
2578227825Stheraventemplate <class _CharT, class _InputIterator>
2579227825Stheraven_InputIterator
2580227825Stheraventime_get<_CharT, _InputIterator>::do_get_time(iter_type __b, iter_type __e,
2581227825Stheraven                                              ios_base& __iob,
2582227825Stheraven                                              ios_base::iostate& __err,
2583227825Stheraven                                              tm* __tm) const
2584227825Stheraven{
2585227825Stheraven    const char_type __fmt[] = {'%', 'H', ':', '%', 'M', ':', '%', 'S'};
2586227825Stheraven    return get(__b, __e, __iob, __err, __tm, __fmt, __fmt + sizeof(__fmt)/sizeof(__fmt[0]));
2587227825Stheraven}
2588227825Stheraven
2589227825Stheraventemplate <class _CharT, class _InputIterator>
2590227825Stheraven_InputIterator
2591227825Stheraventime_get<_CharT, _InputIterator>::do_get_date(iter_type __b, iter_type __e,
2592227825Stheraven                                              ios_base& __iob,
2593227825Stheraven                                              ios_base::iostate& __err,
2594227825Stheraven                                              tm* __tm) const
2595227825Stheraven{
2596227825Stheraven    const string_type& __fmt = this->__x();
2597227825Stheraven    return get(__b, __e, __iob, __err, __tm, __fmt.data(), __fmt.data() + __fmt.size());
2598227825Stheraven}
2599227825Stheraven
2600227825Stheraventemplate <class _CharT, class _InputIterator>
2601227825Stheraven_InputIterator
2602227825Stheraventime_get<_CharT, _InputIterator>::do_get_weekday(iter_type __b, iter_type __e,
2603227825Stheraven                                                 ios_base& __iob,
2604227825Stheraven                                                 ios_base::iostate& __err,
2605227825Stheraven                                                 tm* __tm) const
2606227825Stheraven{
2607227825Stheraven    const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2608227825Stheraven    __get_weekdayname(__tm->tm_wday, __b, __e, __err, __ct);
2609227825Stheraven    return __b;
2610227825Stheraven}
2611227825Stheraven
2612227825Stheraventemplate <class _CharT, class _InputIterator>
2613227825Stheraven_InputIterator
2614227825Stheraventime_get<_CharT, _InputIterator>::do_get_monthname(iter_type __b, iter_type __e,
2615227825Stheraven                                                   ios_base& __iob,
2616227825Stheraven                                                   ios_base::iostate& __err,
2617227825Stheraven                                                   tm* __tm) const
2618227825Stheraven{
2619227825Stheraven    const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2620227825Stheraven    __get_monthname(__tm->tm_mon, __b, __e, __err, __ct);
2621227825Stheraven    return __b;
2622227825Stheraven}
2623227825Stheraven
2624227825Stheraventemplate <class _CharT, class _InputIterator>
2625227825Stheraven_InputIterator
2626227825Stheraventime_get<_CharT, _InputIterator>::do_get_year(iter_type __b, iter_type __e,
2627227825Stheraven                                              ios_base& __iob,
2628227825Stheraven                                              ios_base::iostate& __err,
2629227825Stheraven                                              tm* __tm) const
2630227825Stheraven{
2631227825Stheraven    const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2632227825Stheraven    __get_year(__tm->tm_year, __b, __e, __err, __ct);
2633227825Stheraven    return __b;
2634227825Stheraven}
2635227825Stheraven
2636227825Stheraventemplate <class _CharT, class _InputIterator>
2637227825Stheraven_InputIterator
2638227825Stheraventime_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
2639227825Stheraven                                         ios_base& __iob,
2640227825Stheraven                                         ios_base::iostate& __err, tm* __tm,
2641227825Stheraven                                         char __fmt, char) const
2642227825Stheraven{
2643227825Stheraven    __err = ios_base::goodbit;
2644227825Stheraven    const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2645227825Stheraven    switch (__fmt)
2646227825Stheraven    {
2647227825Stheraven    case 'a':
2648227825Stheraven    case 'A':
2649227825Stheraven        __get_weekdayname(__tm->tm_wday, __b, __e, __err, __ct);
2650227825Stheraven        break;
2651227825Stheraven    case 'b':
2652227825Stheraven    case 'B':
2653227825Stheraven    case 'h':
2654227825Stheraven        __get_monthname(__tm->tm_mon, __b, __e, __err, __ct);
2655227825Stheraven        break;
2656227825Stheraven    case 'c':
2657227825Stheraven        {
2658232950Stheraven        const string_type& __fm = this->__c();
2659232950Stheraven        __b = get(__b, __e, __iob, __err, __tm, __fm.data(), __fm.data() + __fm.size());
2660227825Stheraven        }
2661227825Stheraven        break;
2662227825Stheraven    case 'd':
2663227825Stheraven    case 'e':
2664227825Stheraven        __get_day(__tm->tm_mday, __b, __e, __err, __ct);
2665227825Stheraven        break;
2666227825Stheraven    case 'D':
2667227825Stheraven        {
2668232950Stheraven        const char_type __fm[] = {'%', 'm', '/', '%', 'd', '/', '%', 'y'};
2669232950Stheraven        __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
2670227825Stheraven        }
2671227825Stheraven        break;
2672227825Stheraven    case 'F':
2673227825Stheraven        {
2674232950Stheraven        const char_type __fm[] = {'%', 'Y', '-', '%', 'm', '-', '%', 'd'};
2675232950Stheraven        __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
2676227825Stheraven        }
2677227825Stheraven        break;
2678227825Stheraven    case 'H':
2679227825Stheraven        __get_hour(__tm->tm_hour, __b, __e, __err, __ct);
2680227825Stheraven        break;
2681227825Stheraven    case 'I':
2682227825Stheraven        __get_12_hour(__tm->tm_hour, __b, __e, __err, __ct);
2683227825Stheraven        break;
2684227825Stheraven    case 'j':
2685227825Stheraven        __get_day_year_num(__tm->tm_yday, __b, __e, __err, __ct);
2686227825Stheraven        break;
2687227825Stheraven    case 'm':
2688227825Stheraven        __get_month(__tm->tm_mon, __b, __e, __err, __ct);
2689227825Stheraven        break;
2690227825Stheraven    case 'M':
2691227825Stheraven        __get_minute(__tm->tm_min, __b, __e, __err, __ct);
2692227825Stheraven        break;
2693227825Stheraven    case 'n':
2694227825Stheraven    case 't':
2695227825Stheraven        __get_white_space(__b, __e, __err, __ct);
2696227825Stheraven        break;
2697227825Stheraven    case 'p':
2698227825Stheraven        __get_am_pm(__tm->tm_hour, __b, __e, __err, __ct);
2699227825Stheraven        break;
2700227825Stheraven    case 'r':
2701227825Stheraven        {
2702232950Stheraven        const char_type __fm[] = {'%', 'I', ':', '%', 'M', ':', '%', 'S', ' ', '%', 'p'};
2703232950Stheraven        __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
2704227825Stheraven        }
2705227825Stheraven        break;
2706227825Stheraven    case 'R':
2707227825Stheraven        {
2708232950Stheraven        const char_type __fm[] = {'%', 'H', ':', '%', 'M'};
2709232950Stheraven        __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
2710227825Stheraven        }
2711227825Stheraven        break;
2712227825Stheraven    case 'S':
2713227825Stheraven        __get_second(__tm->tm_sec, __b, __e, __err, __ct);
2714227825Stheraven        break;
2715227825Stheraven    case 'T':
2716227825Stheraven        {
2717232950Stheraven        const char_type __fm[] = {'%', 'H', ':', '%', 'M', ':', '%', 'S'};
2718232950Stheraven        __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
2719227825Stheraven        }
2720227825Stheraven        break;
2721227825Stheraven    case 'w':
2722227825Stheraven        __get_weekday(__tm->tm_wday, __b, __e, __err, __ct);
2723227825Stheraven        break;
2724227825Stheraven    case 'x':
2725227825Stheraven        return do_get_date(__b, __e, __iob, __err, __tm);
2726227825Stheraven    case 'X':
2727227825Stheraven        {
2728232950Stheraven        const string_type& __fm = this->__X();
2729232950Stheraven        __b = get(__b, __e, __iob, __err, __tm, __fm.data(), __fm.data() + __fm.size());
2730227825Stheraven        }
2731227825Stheraven        break;
2732227825Stheraven    case 'y':
2733227825Stheraven        __get_year(__tm->tm_year, __b, __e, __err, __ct);
2734227825Stheraven        break;
2735227825Stheraven    case 'Y':
2736227825Stheraven        __get_year4(__tm->tm_year, __b, __e, __err, __ct);
2737227825Stheraven        break;
2738227825Stheraven    case '%':
2739227825Stheraven        __get_percent(__b, __e, __err, __ct);
2740227825Stheraven        break;
2741227825Stheraven    default:
2742227825Stheraven        __err |= ios_base::failbit;
2743227825Stheraven    }
2744227825Stheraven    return __b;
2745227825Stheraven}
2746227825Stheraven
2747242945Stheraven_LIBCPP_EXTERN_TEMPLATE(class time_get<char>)
2748242945Stheraven_LIBCPP_EXTERN_TEMPLATE(class time_get<wchar_t>)
2749227825Stheraven
2750227825Stheravenclass __time_get
2751227825Stheraven{
2752227825Stheravenprotected:
2753227825Stheraven    locale_t __loc_;
2754227825Stheraven
2755227825Stheraven    __time_get(const char* __nm);
2756227825Stheraven    __time_get(const string& __nm);
2757227825Stheraven    ~__time_get();
2758227825Stheraven};
2759227825Stheraven
2760227825Stheraventemplate <class _CharT>
2761227825Stheravenclass __time_get_storage
2762227825Stheraven    : public __time_get
2763227825Stheraven{
2764227825Stheravenprotected:
2765227825Stheraven    typedef basic_string<_CharT> string_type;
2766227825Stheraven
2767227825Stheraven    string_type __weeks_[14];
2768227825Stheraven    string_type __months_[24];
2769227825Stheraven    string_type __am_pm_[2];
2770227825Stheraven    string_type __c_;
2771227825Stheraven    string_type __r_;
2772227825Stheraven    string_type __x_;
2773227825Stheraven    string_type __X_;
2774227825Stheraven
2775227825Stheraven    explicit __time_get_storage(const char* __nm);
2776227825Stheraven    explicit __time_get_storage(const string& __nm);
2777227825Stheraven
2778227825Stheraven    _LIBCPP_ALWAYS_INLINE ~__time_get_storage() {}
2779227825Stheraven
2780227825Stheraven    time_base::dateorder __do_date_order() const;
2781227825Stheraven
2782227825Stheravenprivate:
2783227825Stheraven    void init(const ctype<_CharT>&);
2784227825Stheraven    string_type __analyze(char __fmt, const ctype<_CharT>&);
2785227825Stheraven};
2786227825Stheraven
2787227825Stheraventemplate <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
2788249998Sdimclass _LIBCPP_TYPE_VIS time_get_byname
2789227825Stheraven    : public time_get<_CharT, _InputIterator>,
2790227825Stheraven      private __time_get_storage<_CharT>
2791227825Stheraven{
2792227825Stheravenpublic:
2793227825Stheraven    typedef time_base::dateorder    dateorder;
2794227825Stheraven    typedef _InputIterator          iter_type;
2795227825Stheraven    typedef _CharT                  char_type;
2796227825Stheraven    typedef basic_string<char_type> string_type;
2797227825Stheraven
2798227825Stheraven    _LIBCPP_INLINE_VISIBILITY
2799227825Stheraven    explicit time_get_byname(const char* __nm, size_t __refs = 0)
2800227825Stheraven        : time_get<_CharT, _InputIterator>(__refs),
2801227825Stheraven          __time_get_storage<_CharT>(__nm) {}
2802227825Stheraven    _LIBCPP_INLINE_VISIBILITY
2803227825Stheraven    explicit time_get_byname(const string& __nm, size_t __refs = 0)
2804227825Stheraven        : time_get<_CharT, _InputIterator>(__refs),
2805227825Stheraven          __time_get_storage<_CharT>(__nm) {}
2806227825Stheraven
2807227825Stheravenprotected:
2808227825Stheraven    _LIBCPP_INLINE_VISIBILITY
2809227825Stheraven    ~time_get_byname() {}
2810227825Stheraven
2811227825Stheraven    _LIBCPP_INLINE_VISIBILITY
2812227825Stheraven    virtual dateorder do_date_order() const {return this->__do_date_order();}
2813227825Stheravenprivate:
2814227825Stheraven    _LIBCPP_INLINE_VISIBILITY
2815227825Stheraven    virtual const string_type* __weeks() const  {return this->__weeks_;}
2816227825Stheraven    _LIBCPP_INLINE_VISIBILITY
2817227825Stheraven    virtual const string_type* __months() const {return this->__months_;}
2818227825Stheraven    _LIBCPP_INLINE_VISIBILITY
2819227825Stheraven    virtual const string_type* __am_pm() const  {return this->__am_pm_;}
2820227825Stheraven    _LIBCPP_INLINE_VISIBILITY
2821227825Stheraven    virtual const string_type& __c() const      {return this->__c_;}
2822227825Stheraven    _LIBCPP_INLINE_VISIBILITY
2823227825Stheraven    virtual const string_type& __r() const      {return this->__r_;}
2824227825Stheraven    _LIBCPP_INLINE_VISIBILITY
2825227825Stheraven    virtual const string_type& __x() const      {return this->__x_;}
2826227825Stheraven    _LIBCPP_INLINE_VISIBILITY
2827227825Stheraven    virtual const string_type& __X() const      {return this->__X_;}
2828227825Stheraven};
2829227825Stheraven
2830242945Stheraven_LIBCPP_EXTERN_TEMPLATE(class time_get_byname<char>)
2831242945Stheraven_LIBCPP_EXTERN_TEMPLATE(class time_get_byname<wchar_t>)
2832227825Stheraven
2833227825Stheravenclass __time_put
2834227825Stheraven{
2835227825Stheraven    locale_t __loc_;
2836227825Stheravenprotected:
2837227825Stheraven    _LIBCPP_ALWAYS_INLINE __time_put() : __loc_(_LIBCPP_GET_C_LOCALE) {}
2838227825Stheraven    __time_put(const char* __nm);
2839227825Stheraven    __time_put(const string& __nm);
2840227825Stheraven    ~__time_put();
2841227825Stheraven    void __do_put(char* __nb, char*& __ne, const tm* __tm,
2842227825Stheraven                  char __fmt, char __mod) const;
2843227825Stheraven    void __do_put(wchar_t* __wb, wchar_t*& __we, const tm* __tm,
2844227825Stheraven                  char __fmt, char __mod) const;
2845227825Stheraven};
2846227825Stheraven
2847227825Stheraventemplate <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
2848249998Sdimclass _LIBCPP_TYPE_VIS time_put
2849227825Stheraven    : public locale::facet,
2850227825Stheraven      private __time_put
2851227825Stheraven{
2852227825Stheravenpublic:
2853227825Stheraven    typedef _CharT char_type;
2854227825Stheraven    typedef _OutputIterator iter_type;
2855227825Stheraven
2856227825Stheraven    _LIBCPP_ALWAYS_INLINE
2857227825Stheraven    explicit time_put(size_t __refs = 0)
2858227825Stheraven        : locale::facet(__refs) {}
2859227825Stheraven
2860227825Stheraven    iter_type put(iter_type __s, ios_base& __iob, char_type __fl, const tm* __tm,
2861227825Stheraven                  const char_type* __pb, const char_type* __pe) const;
2862227825Stheraven
2863227825Stheraven    _LIBCPP_ALWAYS_INLINE
2864227825Stheraven    iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
2865227825Stheraven                  const tm* __tm, char __fmt, char __mod = 0) const
2866227825Stheraven    {
2867227825Stheraven        return do_put(__s, __iob, __fl, __tm, __fmt, __mod);
2868227825Stheraven    }
2869227825Stheraven
2870227825Stheraven    static locale::id id;
2871227825Stheraven
2872227825Stheravenprotected:
2873227825Stheraven    _LIBCPP_ALWAYS_INLINE
2874227825Stheraven    ~time_put() {}
2875227825Stheraven    virtual iter_type do_put(iter_type __s, ios_base&, char_type, const tm* __tm,
2876227825Stheraven                             char __fmt, char __mod) const;
2877227825Stheraven
2878227825Stheraven    _LIBCPP_ALWAYS_INLINE
2879227825Stheraven    explicit time_put(const char* __nm, size_t __refs)
2880227825Stheraven        : locale::facet(__refs),
2881227825Stheraven          __time_put(__nm) {}
2882227825Stheraven    _LIBCPP_ALWAYS_INLINE
2883227825Stheraven    explicit time_put(const string& __nm, size_t __refs)
2884227825Stheraven        : locale::facet(__refs),
2885227825Stheraven          __time_put(__nm) {}
2886227825Stheraven};
2887227825Stheraven
2888227825Stheraventemplate <class _CharT, class _OutputIterator>
2889227825Stheravenlocale::id
2890227825Stheraventime_put<_CharT, _OutputIterator>::id;
2891227825Stheraven
2892227825Stheraventemplate <class _CharT, class _OutputIterator>
2893227825Stheraven_OutputIterator
2894227825Stheraventime_put<_CharT, _OutputIterator>::put(iter_type __s, ios_base& __iob,
2895227825Stheraven                                       char_type __fl, const tm* __tm,
2896227825Stheraven                                       const char_type* __pb,
2897227825Stheraven                                       const char_type* __pe) const
2898227825Stheraven{
2899227825Stheraven    const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2900227825Stheraven    for (; __pb != __pe; ++__pb)
2901227825Stheraven    {
2902227825Stheraven        if (__ct.narrow(*__pb, 0) == '%')
2903227825Stheraven        {
2904227825Stheraven            if (++__pb == __pe)
2905227825Stheraven            {
2906227825Stheraven                *__s++ = __pb[-1];
2907227825Stheraven                break;
2908227825Stheraven            }
2909227825Stheraven            char __mod = 0;
2910227825Stheraven            char __fmt = __ct.narrow(*__pb, 0);
2911227825Stheraven            if (__fmt == 'E' || __fmt == 'O')
2912227825Stheraven            {
2913227825Stheraven                if (++__pb == __pe)
2914227825Stheraven                {
2915227825Stheraven                    *__s++ = __pb[-2];
2916227825Stheraven                    *__s++ = __pb[-1];
2917227825Stheraven                    break;
2918227825Stheraven                }
2919227825Stheraven                __mod = __fmt;
2920227825Stheraven                __fmt = __ct.narrow(*__pb, 0);
2921227825Stheraven            }
2922227825Stheraven            __s = do_put(__s, __iob, __fl, __tm, __fmt, __mod);
2923227825Stheraven        }
2924227825Stheraven        else
2925227825Stheraven            *__s++ = *__pb;
2926227825Stheraven    }
2927227825Stheraven    return __s;
2928227825Stheraven}
2929227825Stheraven
2930227825Stheraventemplate <class _CharT, class _OutputIterator>
2931227825Stheraven_OutputIterator
2932232950Stheraventime_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base&,
2933227825Stheraven                                          char_type, const tm* __tm,
2934227825Stheraven                                          char __fmt, char __mod) const
2935227825Stheraven{
2936227825Stheraven    char_type __nar[100];
2937227825Stheraven    char_type* __nb = __nar;
2938227825Stheraven    char_type* __ne = __nb + 100;
2939227825Stheraven    __do_put(__nb, __ne, __tm, __fmt, __mod);
2940227825Stheraven    return _VSTD::copy(__nb, __ne, __s);
2941227825Stheraven}
2942227825Stheraven
2943242945Stheraven_LIBCPP_EXTERN_TEMPLATE(class time_put<char>)
2944242945Stheraven_LIBCPP_EXTERN_TEMPLATE(class time_put<wchar_t>)
2945227825Stheraven
2946227825Stheraventemplate <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
2947249998Sdimclass _LIBCPP_TYPE_VIS time_put_byname
2948227825Stheraven    : public time_put<_CharT, _OutputIterator>
2949227825Stheraven{
2950227825Stheravenpublic:
2951227825Stheraven    _LIBCPP_ALWAYS_INLINE
2952227825Stheraven    explicit time_put_byname(const char* __nm, size_t __refs = 0)
2953227825Stheraven        : time_put<_CharT, _OutputIterator>(__nm, __refs) {}
2954227825Stheraven
2955227825Stheraven    _LIBCPP_ALWAYS_INLINE
2956227825Stheraven    explicit time_put_byname(const string& __nm, size_t __refs = 0)
2957227825Stheraven        : time_put<_CharT, _OutputIterator>(__nm, __refs) {}
2958227825Stheraven
2959227825Stheravenprotected:
2960227825Stheraven    _LIBCPP_ALWAYS_INLINE
2961227825Stheraven    ~time_put_byname() {}
2962227825Stheraven};
2963227825Stheraven
2964242945Stheraven_LIBCPP_EXTERN_TEMPLATE(class time_put_byname<char>)
2965242945Stheraven_LIBCPP_EXTERN_TEMPLATE(class time_put_byname<wchar_t>)
2966227825Stheraven
2967227825Stheraven// money_base
2968227825Stheraven
2969249998Sdimclass _LIBCPP_TYPE_VIS money_base
2970227825Stheraven{
2971227825Stheravenpublic:
2972227825Stheraven    enum part {none, space, symbol, sign, value};
2973227825Stheraven    struct pattern {char field[4];};
2974227825Stheraven
2975227825Stheraven    _LIBCPP_ALWAYS_INLINE money_base() {}
2976227825Stheraven};
2977227825Stheraven
2978227825Stheraven// moneypunct
2979227825Stheraven
2980227825Stheraventemplate <class _CharT, bool _International = false>
2981249998Sdimclass _LIBCPP_TYPE_VIS moneypunct
2982227825Stheraven    : public locale::facet,
2983227825Stheraven      public money_base
2984227825Stheraven{
2985227825Stheravenpublic:
2986227825Stheraven    typedef _CharT                  char_type;
2987227825Stheraven    typedef basic_string<char_type> string_type;
2988227825Stheraven
2989227825Stheraven    _LIBCPP_ALWAYS_INLINE
2990227825Stheraven    explicit moneypunct(size_t __refs = 0)
2991227825Stheraven        : locale::facet(__refs) {}
2992227825Stheraven
2993227825Stheraven    _LIBCPP_ALWAYS_INLINE char_type   decimal_point() const {return do_decimal_point();}
2994227825Stheraven    _LIBCPP_ALWAYS_INLINE char_type   thousands_sep() const {return do_thousands_sep();}
2995227825Stheraven    _LIBCPP_ALWAYS_INLINE string      grouping()      const {return do_grouping();}
2996227825Stheraven    _LIBCPP_ALWAYS_INLINE string_type curr_symbol()   const {return do_curr_symbol();}
2997227825Stheraven    _LIBCPP_ALWAYS_INLINE string_type positive_sign() const {return do_positive_sign();}
2998227825Stheraven    _LIBCPP_ALWAYS_INLINE string_type negative_sign() const {return do_negative_sign();}
2999227825Stheraven    _LIBCPP_ALWAYS_INLINE int         frac_digits()   const {return do_frac_digits();}
3000227825Stheraven    _LIBCPP_ALWAYS_INLINE pattern     pos_format()    const {return do_pos_format();}
3001227825Stheraven    _LIBCPP_ALWAYS_INLINE pattern     neg_format()    const {return do_neg_format();}
3002227825Stheraven
3003227825Stheraven    static locale::id id;
3004227825Stheraven    static const bool intl = _International;
3005227825Stheraven
3006227825Stheravenprotected:
3007227825Stheraven    _LIBCPP_ALWAYS_INLINE
3008227825Stheraven    ~moneypunct() {}
3009227825Stheraven
3010227825Stheraven    virtual char_type   do_decimal_point() const {return numeric_limits<char_type>::max();}
3011227825Stheraven    virtual char_type   do_thousands_sep() const {return numeric_limits<char_type>::max();}
3012227825Stheraven    virtual string      do_grouping()      const {return string();}
3013227825Stheraven    virtual string_type do_curr_symbol()   const {return string_type();}
3014227825Stheraven    virtual string_type do_positive_sign() const {return string_type();}
3015227825Stheraven    virtual string_type do_negative_sign() const {return string_type(1, '-');}
3016227825Stheraven    virtual int         do_frac_digits()   const {return 0;}
3017227825Stheraven    virtual pattern     do_pos_format()    const
3018241907Sdim        {pattern __p = {{symbol, sign, none, value}}; return __p;}
3019227825Stheraven    virtual pattern     do_neg_format()    const
3020241907Sdim        {pattern __p = {{symbol, sign, none, value}}; return __p;}
3021227825Stheraven};
3022227825Stheraven
3023227825Stheraventemplate <class _CharT, bool _International>
3024227825Stheravenlocale::id
3025227825Stheravenmoneypunct<_CharT, _International>::id;
3026227825Stheraven
3027246487Stheraventemplate <class _CharT, bool _International>
3028246487Stheravenconst bool
3029246487Stheravenmoneypunct<_CharT, _International>::intl;
3030246487Stheraven
3031242945Stheraven_LIBCPP_EXTERN_TEMPLATE(class moneypunct<char, false>)
3032242945Stheraven_LIBCPP_EXTERN_TEMPLATE(class moneypunct<char, true>)
3033242945Stheraven_LIBCPP_EXTERN_TEMPLATE(class moneypunct<wchar_t, false>)
3034242945Stheraven_LIBCPP_EXTERN_TEMPLATE(class moneypunct<wchar_t, true>)
3035227825Stheraven
3036227825Stheraven// moneypunct_byname
3037227825Stheraven
3038227825Stheraventemplate <class _CharT, bool _International = false>
3039249998Sdimclass _LIBCPP_TYPE_VIS moneypunct_byname
3040227825Stheraven    : public moneypunct<_CharT, _International>
3041227825Stheraven{
3042227825Stheravenpublic:
3043227825Stheraven    typedef money_base::pattern  pattern;
3044227825Stheraven    typedef _CharT                  char_type;
3045227825Stheraven    typedef basic_string<char_type> string_type;
3046227825Stheraven
3047227825Stheraven    _LIBCPP_ALWAYS_INLINE
3048227825Stheraven    explicit moneypunct_byname(const char* __nm, size_t __refs = 0)
3049227825Stheraven        : moneypunct<_CharT, _International>(__refs) {init(__nm);}
3050227825Stheraven
3051227825Stheraven    _LIBCPP_ALWAYS_INLINE
3052227825Stheraven    explicit moneypunct_byname(const string& __nm, size_t __refs = 0)
3053227825Stheraven        : moneypunct<_CharT, _International>(__refs) {init(__nm.c_str());}
3054227825Stheraven
3055227825Stheravenprotected:
3056227825Stheraven    _LIBCPP_ALWAYS_INLINE
3057227825Stheraven    ~moneypunct_byname() {}
3058227825Stheraven
3059227825Stheraven    virtual char_type   do_decimal_point() const {return __decimal_point_;}
3060227825Stheraven    virtual char_type   do_thousands_sep() const {return __thousands_sep_;}
3061227825Stheraven    virtual string      do_grouping()      const {return __grouping_;}
3062227825Stheraven    virtual string_type do_curr_symbol()   const {return __curr_symbol_;}
3063227825Stheraven    virtual string_type do_positive_sign() const {return __positive_sign_;}
3064227825Stheraven    virtual string_type do_negative_sign() const {return __negative_sign_;}
3065227825Stheraven    virtual int         do_frac_digits()   const {return __frac_digits_;}
3066227825Stheraven    virtual pattern     do_pos_format()    const {return __pos_format_;}
3067227825Stheraven    virtual pattern     do_neg_format()    const {return __neg_format_;}
3068227825Stheraven
3069227825Stheravenprivate:
3070227825Stheraven    char_type   __decimal_point_;
3071227825Stheraven    char_type   __thousands_sep_;
3072227825Stheraven    string      __grouping_;
3073227825Stheraven    string_type __curr_symbol_;
3074227825Stheraven    string_type __positive_sign_;
3075227825Stheraven    string_type __negative_sign_;
3076227825Stheraven    int         __frac_digits_;
3077227825Stheraven    pattern     __pos_format_;
3078227825Stheraven    pattern     __neg_format_;
3079227825Stheraven
3080227825Stheraven    void init(const char*);
3081227825Stheraven};
3082227825Stheraven
3083227825Stheraventemplate<> void moneypunct_byname<char, false>::init(const char*);
3084227825Stheraventemplate<> void moneypunct_byname<char, true>::init(const char*);
3085227825Stheraventemplate<> void moneypunct_byname<wchar_t, false>::init(const char*);
3086227825Stheraventemplate<> void moneypunct_byname<wchar_t, true>::init(const char*);
3087227825Stheraven
3088242945Stheraven_LIBCPP_EXTERN_TEMPLATE(class moneypunct_byname<char, false>)
3089242945Stheraven_LIBCPP_EXTERN_TEMPLATE(class moneypunct_byname<char, true>)
3090242945Stheraven_LIBCPP_EXTERN_TEMPLATE(class moneypunct_byname<wchar_t, false>)
3091242945Stheraven_LIBCPP_EXTERN_TEMPLATE(class moneypunct_byname<wchar_t, true>)
3092227825Stheraven
3093227825Stheraven// money_get
3094227825Stheraven
3095227825Stheraventemplate <class _CharT>
3096227825Stheravenclass __money_get
3097227825Stheraven{
3098227825Stheravenprotected:
3099227825Stheraven    typedef _CharT                  char_type;
3100227825Stheraven    typedef basic_string<char_type> string_type;
3101227825Stheraven
3102227825Stheraven    _LIBCPP_ALWAYS_INLINE __money_get() {}
3103227825Stheraven
3104227825Stheraven    static void __gather_info(bool __intl, const locale& __loc,
3105227825Stheraven                              money_base::pattern& __pat, char_type& __dp,
3106227825Stheraven                              char_type& __ts, string& __grp,
3107227825Stheraven                              string_type& __sym, string_type& __psn,
3108227825Stheraven                              string_type& __nsn, int& __fd);
3109227825Stheraven};
3110227825Stheraven
3111227825Stheraventemplate <class _CharT>
3112227825Stheravenvoid
3113227825Stheraven__money_get<_CharT>::__gather_info(bool __intl, const locale& __loc,
3114227825Stheraven                                   money_base::pattern& __pat, char_type& __dp,
3115227825Stheraven                                   char_type& __ts, string& __grp,
3116227825Stheraven                                   string_type& __sym, string_type& __psn,
3117227825Stheraven                                   string_type& __nsn, int& __fd)
3118227825Stheraven{
3119227825Stheraven    if (__intl)
3120227825Stheraven    {
3121227825Stheraven        const moneypunct<char_type, true>& __mp =
3122227825Stheraven            use_facet<moneypunct<char_type, true> >(__loc);
3123227825Stheraven        __pat = __mp.neg_format();
3124227825Stheraven        __nsn = __mp.negative_sign();
3125227825Stheraven        __psn = __mp.positive_sign();
3126227825Stheraven        __dp = __mp.decimal_point();
3127227825Stheraven        __ts = __mp.thousands_sep();
3128227825Stheraven        __grp = __mp.grouping();
3129227825Stheraven        __sym = __mp.curr_symbol();
3130227825Stheraven        __fd = __mp.frac_digits();
3131227825Stheraven    }
3132227825Stheraven    else
3133227825Stheraven    {
3134227825Stheraven        const moneypunct<char_type, false>& __mp =
3135227825Stheraven            use_facet<moneypunct<char_type, false> >(__loc);
3136227825Stheraven        __pat = __mp.neg_format();
3137227825Stheraven        __nsn = __mp.negative_sign();
3138227825Stheraven        __psn = __mp.positive_sign();
3139227825Stheraven        __dp = __mp.decimal_point();
3140227825Stheraven        __ts = __mp.thousands_sep();
3141227825Stheraven        __grp = __mp.grouping();
3142227825Stheraven        __sym = __mp.curr_symbol();
3143227825Stheraven        __fd = __mp.frac_digits();
3144227825Stheraven    }
3145227825Stheraven}
3146227825Stheraven
3147242945Stheraven_LIBCPP_EXTERN_TEMPLATE(class __money_get<char>)
3148242945Stheraven_LIBCPP_EXTERN_TEMPLATE(class __money_get<wchar_t>)
3149227825Stheraven
3150227825Stheraventemplate <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
3151249998Sdimclass _LIBCPP_TYPE_VIS money_get
3152227825Stheraven    : public locale::facet,
3153227825Stheraven      private __money_get<_CharT>
3154227825Stheraven{
3155227825Stheravenpublic:
3156227825Stheraven    typedef _CharT                  char_type;
3157227825Stheraven    typedef _InputIterator          iter_type;
3158227825Stheraven    typedef basic_string<char_type> string_type;
3159227825Stheraven
3160227825Stheraven    _LIBCPP_ALWAYS_INLINE
3161227825Stheraven    explicit money_get(size_t __refs = 0)
3162227825Stheraven        : locale::facet(__refs) {}
3163227825Stheraven
3164227825Stheraven    _LIBCPP_ALWAYS_INLINE
3165227825Stheraven    iter_type get(iter_type __b, iter_type __e, bool __intl, ios_base& __iob,
3166227825Stheraven                  ios_base::iostate& __err, long double& __v) const
3167227825Stheraven    {
3168227825Stheraven        return do_get(__b, __e, __intl, __iob, __err, __v);
3169227825Stheraven    }
3170227825Stheraven
3171227825Stheraven    _LIBCPP_ALWAYS_INLINE
3172227825Stheraven    iter_type get(iter_type __b, iter_type __e, bool __intl, ios_base& __iob,
3173227825Stheraven                  ios_base::iostate& __err, string_type& __v) const
3174227825Stheraven    {
3175227825Stheraven        return do_get(__b, __e, __intl, __iob, __err, __v);
3176227825Stheraven    }
3177227825Stheraven
3178227825Stheraven    static locale::id id;
3179227825Stheraven
3180227825Stheravenprotected:
3181227825Stheraven
3182227825Stheraven    _LIBCPP_ALWAYS_INLINE
3183227825Stheraven    ~money_get() {}
3184227825Stheraven
3185227825Stheraven    virtual iter_type do_get(iter_type __b, iter_type __e, bool __intl,
3186227825Stheraven                             ios_base& __iob, ios_base::iostate& __err,
3187227825Stheraven                             long double& __v) const;
3188227825Stheraven    virtual iter_type do_get(iter_type __b, iter_type __e, bool __intl,
3189227825Stheraven                             ios_base& __iob, ios_base::iostate& __err,
3190227825Stheraven                             string_type& __v) const;
3191227825Stheraven
3192227825Stheravenprivate:
3193227825Stheraven    static bool __do_get(iter_type& __b, iter_type __e,
3194227825Stheraven                         bool __intl, const locale& __loc,
3195227825Stheraven                         ios_base::fmtflags __flags, ios_base::iostate& __err,
3196227825Stheraven                         bool& __neg, const ctype<char_type>& __ct,
3197227825Stheraven                         unique_ptr<char_type, void(*)(void*)>& __wb,
3198227825Stheraven                         char_type*& __wn, char_type* __we);
3199227825Stheraven};
3200227825Stheraven
3201227825Stheraventemplate <class _CharT, class _InputIterator>
3202227825Stheravenlocale::id
3203227825Stheravenmoney_get<_CharT, _InputIterator>::id;
3204227825Stheraven
3205227825Stheravenvoid __do_nothing(void*);
3206227825Stheraven
3207227825Stheraventemplate <class _Tp>
3208227825Stheraven_LIBCPP_HIDDEN
3209227825Stheravenvoid
3210227825Stheraven__double_or_nothing(unique_ptr<_Tp, void(*)(void*)>& __b, _Tp*& __n, _Tp*& __e)
3211227825Stheraven{
3212227825Stheraven    bool __owns = __b.get_deleter() != __do_nothing;
3213232950Stheraven    size_t __cur_cap = static_cast<size_t>(__e-__b.get()) * sizeof(_Tp);
3214227825Stheraven    size_t __new_cap = __cur_cap < numeric_limits<size_t>::max() / 2 ?
3215227825Stheraven                       2 * __cur_cap : numeric_limits<size_t>::max();
3216232950Stheraven    size_t __n_off = static_cast<size_t>(__n - __b.get());
3217227825Stheraven    _Tp* __t = (_Tp*)realloc(__owns ? __b.get() : 0, __new_cap);
3218227825Stheraven    if (__t == 0)
3219227825Stheraven        __throw_bad_alloc();
3220227825Stheraven    if (__owns)
3221227825Stheraven        __b.release();
3222227825Stheraven    __b = unique_ptr<_Tp, void(*)(void*)>(__t, free);
3223227825Stheraven    __new_cap /= sizeof(_Tp);
3224227825Stheraven    __n = __b.get() + __n_off;
3225227825Stheraven    __e = __b.get() + __new_cap;
3226227825Stheraven}
3227227825Stheraven
3228227825Stheraven// true == success
3229227825Stheraventemplate <class _CharT, class _InputIterator>
3230227825Stheravenbool
3231227825Stheravenmoney_get<_CharT, _InputIterator>::__do_get(iter_type& __b, iter_type __e,
3232227825Stheraven                                            bool __intl, const locale& __loc,
3233227825Stheraven                                            ios_base::fmtflags __flags,
3234227825Stheraven                                            ios_base::iostate& __err,
3235227825Stheraven                                            bool& __neg,
3236227825Stheraven                                            const ctype<char_type>& __ct,
3237227825Stheraven                                            unique_ptr<char_type, void(*)(void*)>& __wb,
3238227825Stheraven                                            char_type*& __wn, char_type* __we)
3239227825Stheraven{
3240227825Stheraven    const unsigned __bz = 100;
3241227825Stheraven    unsigned __gbuf[__bz];
3242227825Stheraven    unique_ptr<unsigned, void(*)(void*)> __gb(__gbuf, __do_nothing);
3243227825Stheraven    unsigned* __gn = __gb.get();
3244227825Stheraven    unsigned* __ge = __gn + __bz;
3245227825Stheraven    money_base::pattern __pat;
3246227825Stheraven    char_type __dp;
3247227825Stheraven    char_type __ts;
3248227825Stheraven    string __grp;
3249227825Stheraven    string_type __sym;
3250227825Stheraven    string_type __psn;
3251227825Stheraven    string_type __nsn;
3252232950Stheraven    // Capture the spaces read into money_base::{space,none} so they
3253232950Stheraven    // can be compared to initial spaces in __sym.
3254232950Stheraven    string_type __spaces;
3255227825Stheraven    int __fd;
3256227825Stheraven    __money_get<_CharT>::__gather_info(__intl, __loc, __pat, __dp, __ts, __grp,
3257227825Stheraven                                       __sym, __psn, __nsn, __fd);
3258227825Stheraven    const string_type* __trailing_sign = 0;
3259227825Stheraven    __wn = __wb.get();
3260227825Stheraven    for (unsigned __p = 0; __p < 4 && __b != __e; ++__p)
3261227825Stheraven    {
3262227825Stheraven        switch (__pat.field[__p])
3263227825Stheraven        {
3264227825Stheraven        case money_base::space:
3265227825Stheraven            if (__p != 3)
3266227825Stheraven            {
3267227825Stheraven                if (__ct.is(ctype_base::space, *__b))
3268232950Stheraven                    __spaces.push_back(*__b++);
3269227825Stheraven                else
3270227825Stheraven                {
3271227825Stheraven                    __err |= ios_base::failbit;
3272227825Stheraven                    return false;
3273227825Stheraven                }
3274227825Stheraven            }
3275227825Stheraven            // drop through
3276227825Stheraven        case money_base::none:
3277227825Stheraven            if (__p != 3)
3278227825Stheraven            {
3279227825Stheraven                while (__b != __e && __ct.is(ctype_base::space, *__b))
3280232950Stheraven                    __spaces.push_back(*__b++);
3281227825Stheraven            }
3282227825Stheraven            break;
3283227825Stheraven        case money_base::sign:
3284227825Stheraven            if (__psn.size() + __nsn.size() > 0)
3285227825Stheraven            {
3286227825Stheraven                if (__psn.size() == 0 || __nsn.size() == 0)
3287227825Stheraven                {   // sign is optional
3288227825Stheraven                    if (__psn.size() > 0)
3289227825Stheraven                    {   // __nsn.size() == 0
3290227825Stheraven                        if (*__b == __psn[0])
3291227825Stheraven                        {
3292227825Stheraven                            ++__b;
3293227825Stheraven                            if (__psn.size() > 1)
3294227825Stheraven                                __trailing_sign = &__psn;
3295227825Stheraven                        }
3296227825Stheraven                        else
3297227825Stheraven                            __neg = true;
3298227825Stheraven                    }
3299227825Stheraven                    else if (*__b == __nsn[0])  // __nsn.size() > 0 &&  __psn.size() == 0
3300227825Stheraven                    {
3301227825Stheraven                        ++__b;
3302227825Stheraven                        __neg = true;
3303227825Stheraven                        if (__nsn.size() > 1)
3304227825Stheraven                            __trailing_sign = &__nsn;
3305227825Stheraven                    }
3306227825Stheraven                }
3307227825Stheraven                else  // sign is required
3308227825Stheraven                {
3309227825Stheraven                    if (*__b == __psn[0])
3310227825Stheraven                    {
3311227825Stheraven                        ++__b;
3312227825Stheraven                        if (__psn.size() > 1)
3313227825Stheraven                            __trailing_sign = &__psn;
3314227825Stheraven                    }
3315227825Stheraven                    else if (*__b == __nsn[0])
3316227825Stheraven                    {
3317227825Stheraven                        ++__b;
3318227825Stheraven                        __neg = true;
3319227825Stheraven                        if (__nsn.size() > 1)
3320227825Stheraven                            __trailing_sign = &__nsn;
3321227825Stheraven                    }
3322227825Stheraven                    else
3323227825Stheraven                    {
3324227825Stheraven                        __err |= ios_base::failbit;
3325227825Stheraven                        return false;
3326227825Stheraven                    }
3327227825Stheraven                }
3328227825Stheraven            }
3329227825Stheraven            break;
3330227825Stheraven        case money_base::symbol:
3331227825Stheraven            {
3332227825Stheraven            bool __more_needed = __trailing_sign ||
3333227825Stheraven                                 (__p < 2)       ||
3334227825Stheraven                                 (__p == 2 && __pat.field[3] != static_cast<char>(money_base::none));
3335227825Stheraven            bool __sb = __flags & ios_base::showbase;
3336227825Stheraven            if (__sb || __more_needed)
3337227825Stheraven            {
3338232950Stheraven                typename string_type::const_iterator __sym_space_end = __sym.begin();
3339232950Stheraven                if (__p > 0 && (__pat.field[__p - 1] == money_base::none ||
3340232950Stheraven                                __pat.field[__p - 1] == money_base::space)) {
3341232950Stheraven                    // Match spaces we've already read against spaces at
3342232950Stheraven                    // the beginning of __sym.
3343232950Stheraven                    while (__sym_space_end != __sym.end() &&
3344232950Stheraven                           __ct.is(ctype_base::space, *__sym_space_end))
3345232950Stheraven                        ++__sym_space_end;
3346232950Stheraven                    const size_t __num_spaces = __sym_space_end - __sym.begin();
3347232950Stheraven                    if (__num_spaces > __spaces.size() ||
3348232950Stheraven                        !equal(__spaces.end() - __num_spaces, __spaces.end(),
3349232950Stheraven                               __sym.begin())) {
3350232950Stheraven                        // No match. Put __sym_space_end back at the
3351232950Stheraven                        // beginning of __sym, which will prevent a
3352232950Stheraven                        // match in the next loop.
3353232950Stheraven                        __sym_space_end = __sym.begin();
3354232950Stheraven                    }
3355232950Stheraven                }
3356232950Stheraven                typename string_type::const_iterator __sym_curr_char = __sym_space_end;
3357232950Stheraven                while (__sym_curr_char != __sym.end() && __b != __e &&
3358232950Stheraven                       *__b == *__sym_curr_char) {
3359232950Stheraven                    ++__b;
3360232950Stheraven                    ++__sym_curr_char;
3361232950Stheraven                }
3362232950Stheraven                if (__sb && __sym_curr_char != __sym.end())
3363227825Stheraven                {
3364227825Stheraven                    __err |= ios_base::failbit;
3365227825Stheraven                    return false;
3366227825Stheraven                }
3367227825Stheraven            }
3368227825Stheraven            }
3369227825Stheraven            break;
3370227825Stheraven        case money_base::value:
3371227825Stheraven            {
3372227825Stheraven            unsigned __ng = 0;
3373227825Stheraven            for (; __b != __e; ++__b)
3374227825Stheraven            {
3375227825Stheraven                char_type __c = *__b;
3376227825Stheraven                if (__ct.is(ctype_base::digit, __c))
3377227825Stheraven                {
3378227825Stheraven                    if (__wn == __we)
3379227825Stheraven                        __double_or_nothing(__wb, __wn, __we);
3380227825Stheraven                    *__wn++ = __c;
3381227825Stheraven                    ++__ng;
3382227825Stheraven                }
3383227825Stheraven                else if (__grp.size() > 0 && __ng > 0 && __c == __ts)
3384227825Stheraven                {
3385227825Stheraven                    if (__gn == __ge)
3386227825Stheraven                        __double_or_nothing(__gb, __gn, __ge);
3387227825Stheraven                    *__gn++ = __ng;
3388227825Stheraven                    __ng = 0;
3389227825Stheraven                }
3390227825Stheraven                else
3391227825Stheraven                    break;
3392227825Stheraven            }
3393227825Stheraven            if (__gb.get() != __gn && __ng > 0)
3394227825Stheraven            {
3395227825Stheraven                if (__gn == __ge)
3396227825Stheraven                    __double_or_nothing(__gb, __gn, __ge);
3397227825Stheraven                *__gn++ = __ng;
3398227825Stheraven            }
3399227825Stheraven            if (__fd > 0)
3400227825Stheraven            {
3401227825Stheraven                if (__b == __e || *__b != __dp)
3402227825Stheraven                {
3403227825Stheraven                    __err |= ios_base::failbit;
3404227825Stheraven                    return false;
3405227825Stheraven                }
3406227825Stheraven                for (++__b; __fd > 0; --__fd, ++__b)
3407227825Stheraven                {
3408227825Stheraven                    if (__b == __e || !__ct.is(ctype_base::digit, *__b))
3409227825Stheraven                    {
3410227825Stheraven                        __err |= ios_base::failbit;
3411227825Stheraven                        return false;
3412227825Stheraven                    }
3413227825Stheraven                    if (__wn == __we)
3414227825Stheraven                        __double_or_nothing(__wb, __wn, __we);
3415227825Stheraven                    *__wn++ = *__b;
3416227825Stheraven                }
3417227825Stheraven            }
3418227825Stheraven            if (__wn == __wb.get())
3419227825Stheraven            {
3420227825Stheraven                __err |= ios_base::failbit;
3421227825Stheraven                return false;
3422227825Stheraven            }
3423227825Stheraven            }
3424227825Stheraven            break;
3425227825Stheraven        }
3426227825Stheraven    }
3427227825Stheraven    if (__trailing_sign)
3428227825Stheraven    {
3429227825Stheraven        for (unsigned __i = 1; __i < __trailing_sign->size(); ++__i, ++__b)
3430227825Stheraven        {
3431227825Stheraven            if (__b == __e || *__b != (*__trailing_sign)[__i])
3432227825Stheraven            {
3433227825Stheraven                __err |= ios_base::failbit;
3434227825Stheraven                return false;
3435227825Stheraven            }
3436227825Stheraven        }
3437227825Stheraven    }
3438227825Stheraven    if (__gb.get() != __gn)
3439227825Stheraven    {
3440227825Stheraven        ios_base::iostate __et = ios_base::goodbit;
3441227825Stheraven        __check_grouping(__grp, __gb.get(), __gn, __et);
3442227825Stheraven        if (__et)
3443227825Stheraven        {
3444227825Stheraven            __err |= ios_base::failbit;
3445227825Stheraven            return false;
3446227825Stheraven        }
3447227825Stheraven    }
3448227825Stheraven    return true;
3449227825Stheraven}
3450227825Stheraven
3451227825Stheraventemplate <class _CharT, class _InputIterator>
3452227825Stheraven_InputIterator
3453227825Stheravenmoney_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
3454227825Stheraven                                          bool __intl, ios_base& __iob,
3455227825Stheraven                                          ios_base::iostate& __err,
3456227825Stheraven                                          long double& __v) const
3457227825Stheraven{
3458232950Stheraven    const int __bz = 100;
3459227825Stheraven    char_type __wbuf[__bz];
3460227825Stheraven    unique_ptr<char_type, void(*)(void*)> __wb(__wbuf, __do_nothing);
3461227825Stheraven    char_type* __wn;
3462227825Stheraven    char_type* __we = __wbuf + __bz;
3463227825Stheraven    locale __loc = __iob.getloc();
3464227825Stheraven    const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__loc);
3465227825Stheraven    bool __neg = false;
3466227825Stheraven    if (__do_get(__b, __e, __intl, __loc, __iob.flags(), __err, __neg, __ct,
3467227825Stheraven                 __wb, __wn, __we))
3468227825Stheraven    {
3469227825Stheraven        const char __src[] = "0123456789";
3470227825Stheraven        char_type __atoms[sizeof(__src)-1];
3471227825Stheraven        __ct.widen(__src, __src + (sizeof(__src)-1), __atoms);
3472227825Stheraven        char __nbuf[__bz];
3473227825Stheraven        char* __nc = __nbuf;
3474227825Stheraven        unique_ptr<char, void(*)(void*)> __h(0, free);
3475227825Stheraven        if (__wn - __wb.get() > __bz-2)
3476227825Stheraven        {
3477232950Stheraven            __h.reset((char*)malloc(static_cast<size_t>(__wn - __wb.get() + 2)));
3478227825Stheraven            if (__h.get() == 0)
3479227825Stheraven                __throw_bad_alloc();
3480227825Stheraven            __nc = __h.get();
3481227825Stheraven        }
3482227825Stheraven        if (__neg)
3483227825Stheraven            *__nc++ = '-';
3484227825Stheraven        for (const char_type* __w = __wb.get(); __w < __wn; ++__w, ++__nc)
3485249998Sdim            *__nc = __src[find(__atoms, _VSTD::end(__atoms), *__w) - __atoms];
3486227825Stheraven        *__nc = char();
3487227825Stheraven        if (sscanf(__nbuf, "%Lf", &__v) != 1)
3488227825Stheraven            __throw_runtime_error("money_get error");
3489227825Stheraven    }
3490227825Stheraven    if (__b == __e)
3491227825Stheraven        __err |= ios_base::eofbit;
3492227825Stheraven    return __b;
3493227825Stheraven}
3494227825Stheraven
3495227825Stheraventemplate <class _CharT, class _InputIterator>
3496227825Stheraven_InputIterator
3497227825Stheravenmoney_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
3498227825Stheraven                                          bool __intl, ios_base& __iob,
3499227825Stheraven                                          ios_base::iostate& __err,
3500227825Stheraven                                          string_type& __v) const
3501227825Stheraven{
3502232950Stheraven    const int __bz = 100;
3503227825Stheraven    char_type __wbuf[__bz];
3504227825Stheraven    unique_ptr<char_type, void(*)(void*)> __wb(__wbuf, __do_nothing);
3505227825Stheraven    char_type* __wn;
3506227825Stheraven    char_type* __we = __wbuf + __bz;
3507227825Stheraven    locale __loc = __iob.getloc();
3508227825Stheraven    const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__loc);
3509227825Stheraven    bool __neg = false;
3510227825Stheraven    if (__do_get(__b, __e, __intl, __loc, __iob.flags(), __err, __neg, __ct,
3511227825Stheraven                 __wb, __wn, __we))
3512227825Stheraven    {
3513227825Stheraven        __v.clear();
3514227825Stheraven        if (__neg)
3515227825Stheraven            __v.push_back(__ct.widen('-'));
3516227825Stheraven        char_type __z = __ct.widen('0');
3517227825Stheraven        char_type* __w;
3518227825Stheraven        for (__w = __wb.get(); __w < __wn-1; ++__w)
3519227825Stheraven            if (*__w != __z)
3520227825Stheraven                break;
3521227825Stheraven        __v.append(__w, __wn);
3522227825Stheraven    }
3523227825Stheraven    if (__b == __e)
3524227825Stheraven        __err |= ios_base::eofbit;
3525227825Stheraven    return __b;
3526227825Stheraven}
3527227825Stheraven
3528242945Stheraven_LIBCPP_EXTERN_TEMPLATE(class money_get<char>)
3529242945Stheraven_LIBCPP_EXTERN_TEMPLATE(class money_get<wchar_t>)
3530227825Stheraven
3531227825Stheraven// money_put
3532227825Stheraven
3533227825Stheraventemplate <class _CharT>
3534227825Stheravenclass __money_put
3535227825Stheraven{
3536227825Stheravenprotected:
3537227825Stheraven    typedef _CharT                  char_type;
3538227825Stheraven    typedef basic_string<char_type> string_type;
3539227825Stheraven
3540227825Stheraven    _LIBCPP_ALWAYS_INLINE __money_put() {}
3541227825Stheraven
3542227825Stheraven    static void __gather_info(bool __intl, bool __neg, const locale& __loc,
3543227825Stheraven                              money_base::pattern& __pat, char_type& __dp,
3544227825Stheraven                              char_type& __ts, string& __grp,
3545227825Stheraven                              string_type& __sym, string_type& __sn,
3546227825Stheraven                              int& __fd);
3547227825Stheraven    static void __format(char_type* __mb, char_type*& __mi, char_type*& __me,
3548227825Stheraven                         ios_base::fmtflags __flags,
3549227825Stheraven                         const char_type* __db, const char_type* __de,
3550227825Stheraven                         const ctype<char_type>& __ct, bool __neg,
3551227825Stheraven                         const money_base::pattern& __pat, char_type __dp,
3552227825Stheraven                         char_type __ts, const string& __grp,
3553227825Stheraven                         const string_type& __sym, const string_type& __sn,
3554227825Stheraven                         int __fd);
3555227825Stheraven};
3556227825Stheraven
3557227825Stheraventemplate <class _CharT>
3558227825Stheravenvoid
3559227825Stheraven__money_put<_CharT>::__gather_info(bool __intl, bool __neg, const locale& __loc,
3560227825Stheraven                                   money_base::pattern& __pat, char_type& __dp,
3561227825Stheraven                                   char_type& __ts, string& __grp,
3562227825Stheraven                                   string_type& __sym, string_type& __sn,
3563227825Stheraven                                   int& __fd)
3564227825Stheraven{
3565227825Stheraven    if (__intl)
3566227825Stheraven    {
3567227825Stheraven        const moneypunct<char_type, true>& __mp =
3568227825Stheraven            use_facet<moneypunct<char_type, true> >(__loc);
3569227825Stheraven        if (__neg)
3570227825Stheraven        {
3571227825Stheraven            __pat = __mp.neg_format();
3572227825Stheraven            __sn = __mp.negative_sign();
3573227825Stheraven        }
3574227825Stheraven        else
3575227825Stheraven        {
3576227825Stheraven            __pat = __mp.pos_format();
3577227825Stheraven            __sn = __mp.positive_sign();
3578227825Stheraven        }
3579227825Stheraven        __dp = __mp.decimal_point();
3580227825Stheraven        __ts = __mp.thousands_sep();
3581227825Stheraven        __grp = __mp.grouping();
3582227825Stheraven        __sym = __mp.curr_symbol();
3583227825Stheraven        __fd = __mp.frac_digits();
3584227825Stheraven    }
3585227825Stheraven    else
3586227825Stheraven    {
3587227825Stheraven        const moneypunct<char_type, false>& __mp =
3588227825Stheraven            use_facet<moneypunct<char_type, false> >(__loc);
3589227825Stheraven        if (__neg)
3590227825Stheraven        {
3591227825Stheraven            __pat = __mp.neg_format();
3592227825Stheraven            __sn = __mp.negative_sign();
3593227825Stheraven        }
3594227825Stheraven        else
3595227825Stheraven        {
3596227825Stheraven            __pat = __mp.pos_format();
3597227825Stheraven            __sn = __mp.positive_sign();
3598227825Stheraven        }
3599227825Stheraven        __dp = __mp.decimal_point();
3600227825Stheraven        __ts = __mp.thousands_sep();
3601227825Stheraven        __grp = __mp.grouping();
3602227825Stheraven        __sym = __mp.curr_symbol();
3603227825Stheraven        __fd = __mp.frac_digits();
3604227825Stheraven    }
3605227825Stheraven}
3606227825Stheraven
3607227825Stheraventemplate <class _CharT>
3608227825Stheravenvoid
3609227825Stheraven__money_put<_CharT>::__format(char_type* __mb, char_type*& __mi, char_type*& __me,
3610227825Stheraven                              ios_base::fmtflags __flags,
3611227825Stheraven                              const char_type* __db, const char_type* __de,
3612227825Stheraven                              const ctype<char_type>& __ct, bool __neg,
3613227825Stheraven                              const money_base::pattern& __pat, char_type __dp,
3614227825Stheraven                              char_type __ts, const string& __grp,
3615227825Stheraven                              const string_type& __sym, const string_type& __sn,
3616227825Stheraven                              int __fd)
3617227825Stheraven{
3618227825Stheraven    __me = __mb;
3619227825Stheraven    for (unsigned __p = 0; __p < 4; ++__p)
3620227825Stheraven    {
3621227825Stheraven        switch (__pat.field[__p])
3622227825Stheraven        {
3623227825Stheraven        case money_base::none:
3624227825Stheraven            __mi = __me;
3625227825Stheraven            break;
3626227825Stheraven        case money_base::space:
3627227825Stheraven            __mi = __me;
3628227825Stheraven            *__me++ = __ct.widen(' ');
3629227825Stheraven            break;
3630227825Stheraven        case money_base::sign:
3631227825Stheraven            if (!__sn.empty())
3632227825Stheraven                *__me++ = __sn[0];
3633227825Stheraven            break;
3634227825Stheraven        case money_base::symbol:
3635227825Stheraven            if (!__sym.empty() && (__flags & ios_base::showbase))
3636227825Stheraven                __me = _VSTD::copy(__sym.begin(), __sym.end(), __me);
3637227825Stheraven            break;
3638227825Stheraven        case money_base::value:
3639227825Stheraven            {
3640227825Stheraven            // remember start of value so we can reverse it
3641227825Stheraven            char_type* __t = __me;
3642227825Stheraven            // find beginning of digits
3643227825Stheraven            if (__neg)
3644227825Stheraven                ++__db;
3645227825Stheraven            // find end of digits
3646227825Stheraven            const char_type* __d;
3647227825Stheraven            for (__d = __db; __d < __de; ++__d)
3648227825Stheraven                if (!__ct.is(ctype_base::digit, *__d))
3649227825Stheraven                    break;
3650227825Stheraven            // print fractional part
3651227825Stheraven            if (__fd > 0)
3652227825Stheraven            {
3653227825Stheraven                int __f;
3654227825Stheraven                for (__f = __fd; __d > __db && __f > 0; --__f)
3655227825Stheraven                    *__me++ = *--__d;
3656227825Stheraven                char_type __z = __f > 0 ? __ct.widen('0') : char_type();
3657227825Stheraven                for (; __f > 0; --__f)
3658227825Stheraven                    *__me++ = __z;
3659227825Stheraven                *__me++ = __dp;
3660227825Stheraven            }
3661227825Stheraven            // print units part
3662227825Stheraven            if (__d == __db)
3663227825Stheraven            {
3664227825Stheraven                *__me++ = __ct.widen('0');
3665227825Stheraven            }
3666227825Stheraven            else
3667227825Stheraven            {
3668227825Stheraven                unsigned __ng = 0;
3669227825Stheraven                unsigned __ig = 0;
3670227825Stheraven                unsigned __gl = __grp.empty() ? numeric_limits<unsigned>::max()
3671227825Stheraven                                              : static_cast<unsigned>(__grp[__ig]);
3672227825Stheraven                while (__d != __db)
3673227825Stheraven                {
3674227825Stheraven                    if (__ng == __gl)
3675227825Stheraven                    {
3676227825Stheraven                        *__me++ = __ts;
3677227825Stheraven                        __ng = 0;
3678227825Stheraven                        if (++__ig < __grp.size())
3679227825Stheraven                            __gl = __grp[__ig] == numeric_limits<char>::max() ?
3680227825Stheraven                                        numeric_limits<unsigned>::max() :
3681227825Stheraven                                        static_cast<unsigned>(__grp[__ig]);
3682227825Stheraven                    }
3683227825Stheraven                    *__me++ = *--__d;
3684227825Stheraven                    ++__ng;
3685227825Stheraven                }
3686227825Stheraven            }
3687227825Stheraven            // reverse it
3688227825Stheraven            reverse(__t, __me);
3689227825Stheraven            }
3690227825Stheraven            break;
3691227825Stheraven        }
3692227825Stheraven    }
3693227825Stheraven    // print rest of sign, if any
3694227825Stheraven    if (__sn.size() > 1)
3695227825Stheraven        __me = _VSTD::copy(__sn.begin()+1, __sn.end(), __me);
3696227825Stheraven    // set alignment
3697227825Stheraven    if ((__flags & ios_base::adjustfield) == ios_base::left)
3698227825Stheraven        __mi = __me;
3699227825Stheraven    else if ((__flags & ios_base::adjustfield) != ios_base::internal)
3700227825Stheraven        __mi = __mb;
3701227825Stheraven}
3702227825Stheraven
3703242945Stheraven_LIBCPP_EXTERN_TEMPLATE(class __money_put<char>)
3704242945Stheraven_LIBCPP_EXTERN_TEMPLATE(class __money_put<wchar_t>)
3705227825Stheraven
3706227825Stheraventemplate <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
3707249998Sdimclass _LIBCPP_TYPE_VIS money_put
3708227825Stheraven    : public locale::facet,
3709227825Stheraven      private __money_put<_CharT>
3710227825Stheraven{
3711227825Stheravenpublic:
3712227825Stheraven    typedef _CharT                  char_type;
3713227825Stheraven    typedef _OutputIterator         iter_type;
3714227825Stheraven    typedef basic_string<char_type> string_type;
3715227825Stheraven
3716227825Stheraven    _LIBCPP_ALWAYS_INLINE
3717227825Stheraven    explicit money_put(size_t __refs = 0)
3718227825Stheraven        : locale::facet(__refs) {}
3719227825Stheraven
3720227825Stheraven    _LIBCPP_ALWAYS_INLINE
3721227825Stheraven    iter_type put(iter_type __s, bool __intl, ios_base& __iob, char_type __fl,
3722227825Stheraven                  long double __units) const
3723227825Stheraven    {
3724227825Stheraven        return do_put(__s, __intl, __iob, __fl, __units);
3725227825Stheraven    }
3726227825Stheraven
3727227825Stheraven    _LIBCPP_ALWAYS_INLINE
3728227825Stheraven    iter_type put(iter_type __s, bool __intl, ios_base& __iob, char_type __fl,
3729227825Stheraven                  const string_type& __digits) const
3730227825Stheraven    {
3731227825Stheraven        return do_put(__s, __intl, __iob, __fl, __digits);
3732227825Stheraven    }
3733227825Stheraven
3734227825Stheraven    static locale::id id;
3735227825Stheraven
3736227825Stheravenprotected:
3737227825Stheraven    _LIBCPP_ALWAYS_INLINE
3738227825Stheraven    ~money_put() {}
3739227825Stheraven
3740227825Stheraven    virtual iter_type do_put(iter_type __s, bool __intl, ios_base& __iob,
3741227825Stheraven                             char_type __fl, long double __units) const;
3742227825Stheraven    virtual iter_type do_put(iter_type __s, bool __intl, ios_base& __iob,
3743227825Stheraven                             char_type __fl, const string_type& __digits) const;
3744227825Stheraven};
3745227825Stheraven
3746227825Stheraventemplate <class _CharT, class _OutputIterator>
3747227825Stheravenlocale::id
3748227825Stheravenmoney_put<_CharT, _OutputIterator>::id;
3749227825Stheraven
3750227825Stheraventemplate <class _CharT, class _OutputIterator>
3751227825Stheraven_OutputIterator
3752227825Stheravenmoney_put<_CharT, _OutputIterator>::do_put(iter_type __s, bool __intl,
3753227825Stheraven                                           ios_base& __iob, char_type __fl,
3754227825Stheraven                                           long double __units) const
3755227825Stheraven{
3756227825Stheraven    // convert to char
3757227825Stheraven    const size_t __bs = 100;
3758227825Stheraven    char __buf[__bs];
3759227825Stheraven    char* __bb = __buf;
3760227825Stheraven    char_type __digits[__bs];
3761227825Stheraven    char_type* __db = __digits;
3762232950Stheraven    size_t __n = static_cast<size_t>(snprintf(__bb, __bs, "%.0Lf", __units));
3763227825Stheraven    unique_ptr<char, void(*)(void*)> __hn(0, free);
3764227825Stheraven    unique_ptr<char_type, void(*)(void*)> __hd(0, free);
3765227825Stheraven    // secure memory for digit storage
3766227825Stheraven    if (__n > __bs-1)
3767227825Stheraven    {
3768227825Stheraven#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
3769232950Stheraven        __n = static_cast<size_t>(asprintf_l(&__bb, _LIBCPP_GET_C_LOCALE, "%.0Lf", __units));
3770227825Stheraven#else
3771227825Stheraven        __n = __asprintf_l(&__bb, __cloc(), "%.0Lf", __units);
3772227825Stheraven#endif
3773227825Stheraven        if (__bb == 0)
3774227825Stheraven            __throw_bad_alloc();
3775227825Stheraven        __hn.reset(__bb);
3776227825Stheraven        __hd.reset((char_type*)malloc(__n * sizeof(char_type)));
3777232950Stheraven        if (__hd == nullptr)
3778227825Stheraven            __throw_bad_alloc();
3779227825Stheraven        __db = __hd.get();
3780227825Stheraven    }
3781227825Stheraven    // gather info
3782227825Stheraven    locale __loc = __iob.getloc();
3783227825Stheraven    const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__loc);
3784227825Stheraven    __ct.widen(__bb, __bb + __n, __db);
3785227825Stheraven    bool __neg = __n > 0 && __bb[0] == '-';
3786227825Stheraven    money_base::pattern __pat;
3787227825Stheraven    char_type __dp;
3788227825Stheraven    char_type __ts;
3789227825Stheraven    string __grp;
3790227825Stheraven    string_type __sym;
3791227825Stheraven    string_type __sn;
3792227825Stheraven    int __fd;
3793227825Stheraven    this->__gather_info(__intl, __neg, __loc, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3794227825Stheraven    // secure memory for formatting
3795227825Stheraven    char_type __mbuf[__bs];
3796227825Stheraven    char_type* __mb = __mbuf;
3797227825Stheraven    unique_ptr<char_type, void(*)(void*)> __hw(0, free);
3798227825Stheraven    size_t __exn = static_cast<int>(__n) > __fd ?
3799232950Stheraven                   (__n - static_cast<size_t>(__fd)) * 2 + __sn.size() +
3800232950Stheraven                    __sym.size() + static_cast<size_t>(__fd) + 1
3801232950Stheraven                 : __sn.size() + __sym.size() + static_cast<size_t>(__fd) + 2;
3802227825Stheraven    if (__exn > __bs)
3803227825Stheraven    {
3804227825Stheraven        __hw.reset((char_type*)malloc(__exn * sizeof(char_type)));
3805227825Stheraven        __mb = __hw.get();
3806227825Stheraven        if (__mb == 0)
3807227825Stheraven            __throw_bad_alloc();
3808227825Stheraven    }
3809227825Stheraven    // format
3810227825Stheraven    char_type* __mi;
3811227825Stheraven    char_type* __me;
3812227825Stheraven    this->__format(__mb, __mi, __me, __iob.flags(),
3813227825Stheraven                   __db, __db + __n, __ct,
3814227825Stheraven                   __neg, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3815227825Stheraven    return __pad_and_output(__s, __mb, __mi, __me, __iob, __fl);
3816227825Stheraven}
3817227825Stheraven
3818227825Stheraventemplate <class _CharT, class _OutputIterator>
3819227825Stheraven_OutputIterator
3820227825Stheravenmoney_put<_CharT, _OutputIterator>::do_put(iter_type __s, bool __intl,
3821227825Stheraven                                           ios_base& __iob, char_type __fl,
3822227825Stheraven                                           const string_type& __digits) const
3823227825Stheraven{
3824227825Stheraven    // gather info
3825227825Stheraven    locale __loc = __iob.getloc();
3826227825Stheraven    const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__loc);
3827227825Stheraven    bool __neg = __digits.size() > 0 && __digits[0] == __ct.widen('-');
3828227825Stheraven    money_base::pattern __pat;
3829227825Stheraven    char_type __dp;
3830227825Stheraven    char_type __ts;
3831227825Stheraven    string __grp;
3832227825Stheraven    string_type __sym;
3833227825Stheraven    string_type __sn;
3834227825Stheraven    int __fd;
3835227825Stheraven    this->__gather_info(__intl, __neg, __loc, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3836227825Stheraven    // secure memory for formatting
3837227825Stheraven    char_type __mbuf[100];
3838227825Stheraven    char_type* __mb = __mbuf;
3839227825Stheraven    unique_ptr<char_type, void(*)(void*)> __h(0, free);
3840232950Stheraven    size_t __exn = static_cast<int>(__digits.size()) > __fd ?
3841232950Stheraven                   (__digits.size() - static_cast<size_t>(__fd)) * 2 +
3842232950Stheraven                    __sn.size() + __sym.size() + static_cast<size_t>(__fd) + 1
3843232950Stheraven                 : __sn.size() + __sym.size() + static_cast<size_t>(__fd) + 2;
3844227825Stheraven    if (__exn > 100)
3845227825Stheraven    {
3846227825Stheraven        __h.reset((char_type*)malloc(__exn * sizeof(char_type)));
3847227825Stheraven        __mb = __h.get();
3848227825Stheraven        if (__mb == 0)
3849227825Stheraven            __throw_bad_alloc();
3850227825Stheraven    }
3851227825Stheraven    // format
3852227825Stheraven    char_type* __mi;
3853227825Stheraven    char_type* __me;
3854227825Stheraven    this->__format(__mb, __mi, __me, __iob.flags(),
3855227825Stheraven                   __digits.data(), __digits.data() + __digits.size(), __ct,
3856227825Stheraven                   __neg, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3857227825Stheraven    return __pad_and_output(__s, __mb, __mi, __me, __iob, __fl);
3858227825Stheraven}
3859227825Stheraven
3860242945Stheraven_LIBCPP_EXTERN_TEMPLATE(class money_put<char>)
3861242945Stheraven_LIBCPP_EXTERN_TEMPLATE(class money_put<wchar_t>)
3862227825Stheraven
3863227825Stheraven// messages
3864227825Stheraven
3865249998Sdimclass _LIBCPP_TYPE_VIS messages_base
3866227825Stheraven{
3867227825Stheravenpublic:
3868227825Stheraven    typedef ptrdiff_t catalog;
3869227825Stheraven
3870227825Stheraven    _LIBCPP_ALWAYS_INLINE messages_base() {}
3871227825Stheraven};
3872227825Stheraven
3873227825Stheraventemplate <class _CharT>
3874249998Sdimclass _LIBCPP_TYPE_VIS messages
3875227825Stheraven    : public locale::facet,
3876227825Stheraven      public messages_base
3877227825Stheraven{
3878227825Stheravenpublic:
3879227825Stheraven    typedef _CharT               char_type;
3880227825Stheraven    typedef basic_string<_CharT> string_type;
3881227825Stheraven
3882227825Stheraven    _LIBCPP_ALWAYS_INLINE
3883227825Stheraven    explicit messages(size_t __refs = 0)
3884227825Stheraven        : locale::facet(__refs) {}
3885227825Stheraven
3886227825Stheraven    _LIBCPP_ALWAYS_INLINE
3887227825Stheraven    catalog open(const basic_string<char>& __nm, const locale& __loc) const
3888227825Stheraven    {
3889227825Stheraven        return do_open(__nm, __loc);
3890227825Stheraven    }
3891227825Stheraven
3892227825Stheraven    _LIBCPP_ALWAYS_INLINE
3893227825Stheraven    string_type get(catalog __c, int __set, int __msgid,
3894227825Stheraven                    const string_type& __dflt) const
3895227825Stheraven    {
3896227825Stheraven        return do_get(__c, __set, __msgid, __dflt);
3897227825Stheraven    }
3898227825Stheraven
3899227825Stheraven    _LIBCPP_ALWAYS_INLINE
3900227825Stheraven    void close(catalog __c) const
3901227825Stheraven    {
3902227825Stheraven        do_close(__c);
3903227825Stheraven    }
3904227825Stheraven
3905227825Stheraven    static locale::id id;
3906227825Stheraven
3907227825Stheravenprotected:
3908227825Stheraven    _LIBCPP_ALWAYS_INLINE
3909227825Stheraven    ~messages() {}
3910227825Stheraven
3911227825Stheraven    virtual catalog do_open(const basic_string<char>&, const locale&) const;
3912227825Stheraven    virtual string_type do_get(catalog, int __set, int __msgid,
3913227825Stheraven                               const string_type& __dflt) const;
3914227825Stheraven    virtual void do_close(catalog) const;
3915227825Stheraven};
3916227825Stheraven
3917227825Stheraventemplate <class _CharT>
3918227825Stheravenlocale::id
3919227825Stheravenmessages<_CharT>::id;
3920227825Stheraven
3921227825Stheraventemplate <class _CharT>
3922227825Stheraventypename messages<_CharT>::catalog
3923227825Stheravenmessages<_CharT>::do_open(const basic_string<char>& __nm, const locale&) const
3924227825Stheraven{
3925249998Sdim#ifdef _WIN32
3926227825Stheraven    return -1;
3927227825Stheraven#else // _WIN32
3928227825Stheraven    catalog __cat = (catalog)catopen(__nm.c_str(), NL_CAT_LOCALE);
3929227825Stheraven    if (__cat != -1)
3930227825Stheraven        __cat = static_cast<catalog>((static_cast<size_t>(__cat) >> 1));
3931227825Stheraven    return __cat;
3932227825Stheraven#endif // _WIN32
3933227825Stheraven}
3934227825Stheraven
3935227825Stheraventemplate <class _CharT>
3936227825Stheraventypename messages<_CharT>::string_type
3937227825Stheravenmessages<_CharT>::do_get(catalog __c, int __set, int __msgid,
3938227825Stheraven                         const string_type& __dflt) const
3939227825Stheraven{
3940249998Sdim#ifdef _WIN32
3941227825Stheraven    return __dflt;
3942227825Stheraven#else // _WIN32
3943227825Stheraven    string __ndflt;
3944227825Stheraven    __narrow_to_utf8<sizeof(char_type)*__CHAR_BIT__>()(back_inserter(__ndflt),
3945227825Stheraven                                                       __dflt.c_str(),
3946227825Stheraven                                                       __dflt.c_str() + __dflt.size());
3947227825Stheraven    if (__c != -1)
3948227825Stheraven        __c <<= 1;
3949227825Stheraven    nl_catd __cat = (nl_catd)__c;
3950227825Stheraven    char* __n = catgets(__cat, __set, __msgid, __ndflt.c_str());
3951227825Stheraven    string_type __w;
3952227825Stheraven    __widen_from_utf8<sizeof(char_type)*__CHAR_BIT__>()(back_inserter(__w),
3953227825Stheraven                                                        __n, __n + strlen(__n));
3954227825Stheraven    return __w;
3955227825Stheraven#endif // _WIN32
3956227825Stheraven}
3957227825Stheraven
3958227825Stheraventemplate <class _CharT>
3959227825Stheravenvoid
3960227825Stheravenmessages<_CharT>::do_close(catalog __c) const
3961227825Stheraven{
3962249998Sdim#if !defined(_WIN32)
3963227825Stheraven    if (__c != -1)
3964227825Stheraven        __c <<= 1;
3965227825Stheraven    nl_catd __cat = (nl_catd)__c;
3966227825Stheraven    catclose(__cat);
3967227825Stheraven#endif // !_WIN32
3968227825Stheraven}
3969227825Stheraven
3970242945Stheraven_LIBCPP_EXTERN_TEMPLATE(class messages<char>)
3971242945Stheraven_LIBCPP_EXTERN_TEMPLATE(class messages<wchar_t>)
3972227825Stheraven
3973227825Stheraventemplate <class _CharT>
3974249998Sdimclass _LIBCPP_TYPE_VIS messages_byname
3975227825Stheraven    : public messages<_CharT>
3976227825Stheraven{
3977227825Stheravenpublic:
3978227825Stheraven    typedef messages_base::catalog catalog;
3979227825Stheraven    typedef basic_string<_CharT> string_type;
3980227825Stheraven
3981227825Stheraven    _LIBCPP_ALWAYS_INLINE
3982227825Stheraven    explicit messages_byname(const char*, size_t __refs = 0)
3983227825Stheraven        : messages<_CharT>(__refs) {}
3984227825Stheraven
3985227825Stheraven    _LIBCPP_ALWAYS_INLINE
3986227825Stheraven    explicit messages_byname(const string&, size_t __refs = 0)
3987227825Stheraven        : messages<_CharT>(__refs) {}
3988227825Stheraven
3989227825Stheravenprotected:
3990227825Stheraven    _LIBCPP_ALWAYS_INLINE
3991227825Stheraven    ~messages_byname() {}
3992227825Stheraven};
3993227825Stheraven
3994242945Stheraven_LIBCPP_EXTERN_TEMPLATE(class messages_byname<char>)
3995242945Stheraven_LIBCPP_EXTERN_TEMPLATE(class messages_byname<wchar_t>)
3996227825Stheraven
3997227825Stheraventemplate<class _Codecvt, class _Elem = wchar_t,
3998227825Stheraven         class _Wide_alloc = allocator<_Elem>,
3999227825Stheraven         class _Byte_alloc = allocator<char> >
4000249998Sdimclass _LIBCPP_TYPE_VIS wstring_convert
4001227825Stheraven{
4002227825Stheravenpublic:
4003227825Stheraven    typedef basic_string<char, char_traits<char>, _Byte_alloc>   byte_string;
4004227825Stheraven    typedef basic_string<_Elem, char_traits<_Elem>, _Wide_alloc> wide_string;
4005227825Stheraven    typedef typename _Codecvt::state_type                        state_type;
4006227825Stheraven    typedef typename wide_string::traits_type::int_type          int_type;
4007227825Stheraven
4008227825Stheravenprivate:
4009227825Stheraven    byte_string __byte_err_string_;
4010227825Stheraven    wide_string __wide_err_string_;
4011227825Stheraven    _Codecvt* __cvtptr_;
4012227825Stheraven    state_type __cvtstate_;
4013227825Stheraven    size_t __cvtcount_;
4014227825Stheraven
4015227825Stheraven    wstring_convert(const wstring_convert& __wc);
4016227825Stheraven    wstring_convert& operator=(const wstring_convert& __wc);
4017227825Stheravenpublic:
4018227825Stheraven    wstring_convert(_Codecvt* __pcvt = new _Codecvt);
4019227825Stheraven    wstring_convert(_Codecvt* __pcvt, state_type __state);
4020227825Stheraven    wstring_convert(const byte_string& __byte_err,
4021227825Stheraven                    const wide_string& __wide_err = wide_string());
4022227825Stheraven#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4023227825Stheraven    wstring_convert(wstring_convert&& __wc);
4024227825Stheraven#endif
4025227825Stheraven    ~wstring_convert();
4026227825Stheraven
4027227825Stheraven    _LIBCPP_ALWAYS_INLINE
4028227825Stheraven    wide_string from_bytes(char __byte)
4029227825Stheraven        {return from_bytes(&__byte, &__byte+1);}
4030227825Stheraven    _LIBCPP_ALWAYS_INLINE
4031227825Stheraven    wide_string from_bytes(const char* __ptr)
4032227825Stheraven        {return from_bytes(__ptr, __ptr + char_traits<char>::length(__ptr));}
4033227825Stheraven    _LIBCPP_ALWAYS_INLINE
4034227825Stheraven    wide_string from_bytes(const byte_string& __str)
4035227825Stheraven        {return from_bytes(__str.data(), __str.data() + __str.size());}
4036227825Stheraven    wide_string from_bytes(const char* __first, const char* __last);
4037227825Stheraven
4038227825Stheraven    _LIBCPP_ALWAYS_INLINE
4039227825Stheraven    byte_string to_bytes(_Elem __wchar)
4040227825Stheraven        {return to_bytes(&__wchar, &__wchar+1);}
4041227825Stheraven    _LIBCPP_ALWAYS_INLINE
4042227825Stheraven    byte_string to_bytes(const _Elem* __wptr)
4043227825Stheraven        {return to_bytes(__wptr, __wptr + char_traits<_Elem>::length(__wptr));}
4044227825Stheraven    _LIBCPP_ALWAYS_INLINE
4045227825Stheraven    byte_string to_bytes(const wide_string& __wstr)
4046227825Stheraven        {return to_bytes(__wstr.data(), __wstr.data() + __wstr.size());}
4047227825Stheraven    byte_string to_bytes(const _Elem* __first, const _Elem* __last);
4048227825Stheraven
4049227825Stheraven    _LIBCPP_ALWAYS_INLINE
4050227825Stheraven    size_t converted() const {return __cvtcount_;}
4051227825Stheraven    _LIBCPP_ALWAYS_INLINE
4052227825Stheraven    state_type state() const {return __cvtstate_;}
4053227825Stheraven};
4054227825Stheraven
4055227825Stheraventemplate<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
4056227825Stheraveninline _LIBCPP_ALWAYS_INLINE
4057227825Stheravenwstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
4058227825Stheraven    wstring_convert(_Codecvt* __pcvt)
4059227825Stheraven        : __cvtptr_(__pcvt), __cvtstate_(), __cvtcount_(0)
4060227825Stheraven{
4061227825Stheraven}
4062227825Stheraven
4063227825Stheraventemplate<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
4064227825Stheraveninline _LIBCPP_ALWAYS_INLINE
4065227825Stheravenwstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
4066227825Stheraven    wstring_convert(_Codecvt* __pcvt, state_type __state)
4067227825Stheraven        : __cvtptr_(__pcvt), __cvtstate_(__state), __cvtcount_(0)
4068227825Stheraven{
4069227825Stheraven}
4070227825Stheraven
4071227825Stheraventemplate<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
4072227825Stheravenwstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
4073227825Stheraven    wstring_convert(const byte_string& __byte_err, const wide_string& __wide_err)
4074227825Stheraven        : __byte_err_string_(__byte_err), __wide_err_string_(__wide_err),
4075227825Stheraven          __cvtstate_(), __cvtcount_(0)
4076227825Stheraven{
4077227825Stheraven    __cvtptr_ = new _Codecvt;
4078227825Stheraven}
4079227825Stheraven
4080227825Stheraven#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4081227825Stheraven
4082227825Stheraventemplate<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
4083227825Stheraveninline _LIBCPP_ALWAYS_INLINE
4084227825Stheravenwstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
4085227825Stheraven    wstring_convert(wstring_convert&& __wc)
4086227825Stheraven        : __byte_err_string_(_VSTD::move(__wc.__byte_err_string_)),
4087227825Stheraven          __wide_err_string_(_VSTD::move(__wc.__wide_err_string_)),
4088227825Stheraven          __cvtptr_(__wc.__cvtptr_),
4089227825Stheraven          __cvtstate_(__wc.__cvtstate_), __cvtcount_(__wc.__cvtstate_)
4090227825Stheraven{
4091227825Stheraven    __wc.__cvtptr_ = nullptr;
4092227825Stheraven}
4093227825Stheraven
4094227825Stheraven#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4095227825Stheraven
4096227825Stheraventemplate<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
4097227825Stheravenwstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::~wstring_convert()
4098227825Stheraven{
4099227825Stheraven    delete __cvtptr_;
4100227825Stheraven}
4101227825Stheraven
4102227825Stheraventemplate<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
4103227825Stheraventypename wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::wide_string
4104227825Stheravenwstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
4105227825Stheraven    from_bytes(const char* __frm, const char* __frm_end)
4106227825Stheraven{
4107227825Stheraven    __cvtcount_ = 0;
4108227825Stheraven    if (__cvtptr_ != nullptr)
4109227825Stheraven    {
4110227825Stheraven        wide_string __ws(2*(__frm_end - __frm), _Elem());
4111241903Sdim        if (__frm != __frm_end)
4112241903Sdim            __ws.resize(__ws.capacity());
4113227825Stheraven        codecvt_base::result __r = codecvt_base::ok;
4114227825Stheraven        state_type __st = __cvtstate_;
4115227825Stheraven        if (__frm != __frm_end)
4116227825Stheraven        {
4117227825Stheraven            _Elem* __to = &__ws[0];
4118227825Stheraven            _Elem* __to_end = __to + __ws.size();
4119227825Stheraven            const char* __frm_nxt;
4120227825Stheraven            do
4121227825Stheraven            {
4122227825Stheraven                _Elem* __to_nxt;
4123227825Stheraven                __r = __cvtptr_->in(__st, __frm, __frm_end, __frm_nxt,
4124227825Stheraven                                          __to, __to_end, __to_nxt);
4125227825Stheraven                __cvtcount_ += __frm_nxt - __frm;
4126227825Stheraven                if (__frm_nxt == __frm)
4127227825Stheraven                {
4128227825Stheraven                    __r = codecvt_base::error;
4129227825Stheraven                }
4130227825Stheraven                else if (__r == codecvt_base::noconv)
4131227825Stheraven                {
4132227825Stheraven                    __ws.resize(__to - &__ws[0]);
4133227825Stheraven                    // This only gets executed if _Elem is char
4134227825Stheraven                    __ws.append((const _Elem*)__frm, (const _Elem*)__frm_end);
4135227825Stheraven                    __frm = __frm_nxt;
4136227825Stheraven                    __r = codecvt_base::ok;
4137227825Stheraven                }
4138227825Stheraven                else if (__r == codecvt_base::ok)
4139227825Stheraven                {
4140227825Stheraven                    __ws.resize(__to_nxt - &__ws[0]);
4141227825Stheraven                    __frm = __frm_nxt;
4142227825Stheraven                }
4143227825Stheraven                else if (__r == codecvt_base::partial)
4144227825Stheraven                {
4145227825Stheraven                    ptrdiff_t __s = __to_nxt - &__ws[0];
4146227825Stheraven                    __ws.resize(2 * __s);
4147227825Stheraven                    __to = &__ws[0] + __s;
4148227825Stheraven                    __to_end = &__ws[0] + __ws.size();
4149227825Stheraven                    __frm = __frm_nxt;
4150227825Stheraven                }
4151227825Stheraven            } while (__r == codecvt_base::partial && __frm_nxt < __frm_end);
4152227825Stheraven        }
4153227825Stheraven        if (__r == codecvt_base::ok)
4154227825Stheraven            return __ws;
4155227825Stheraven    }
4156227825Stheraven#ifndef _LIBCPP_NO_EXCEPTIONS
4157227825Stheraven    if (__wide_err_string_.empty())
4158227825Stheraven        throw range_error("wstring_convert: from_bytes error");
4159227825Stheraven#endif  // _LIBCPP_NO_EXCEPTIONS
4160227825Stheraven    return __wide_err_string_;
4161227825Stheraven}
4162227825Stheraven
4163227825Stheraventemplate<class _Codecvt, class _Elem, class _Wide_alloc, class _Byte_alloc>
4164227825Stheraventypename wstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::byte_string
4165227825Stheravenwstring_convert<_Codecvt, _Elem, _Wide_alloc, _Byte_alloc>::
4166227825Stheraven    to_bytes(const _Elem* __frm, const _Elem* __frm_end)
4167227825Stheraven{
4168227825Stheraven    __cvtcount_ = 0;
4169227825Stheraven    if (__cvtptr_ != nullptr)
4170227825Stheraven    {
4171227825Stheraven        byte_string __bs(2*(__frm_end - __frm), char());
4172241903Sdim        if (__frm != __frm_end)
4173241903Sdim            __bs.resize(__bs.capacity());
4174227825Stheraven        codecvt_base::result __r = codecvt_base::ok;
4175227825Stheraven        state_type __st = __cvtstate_;
4176227825Stheraven        if (__frm != __frm_end)
4177227825Stheraven        {
4178227825Stheraven            char* __to = &__bs[0];
4179227825Stheraven            char* __to_end = __to + __bs.size();
4180227825Stheraven            const _Elem* __frm_nxt;
4181227825Stheraven            do
4182227825Stheraven            {
4183227825Stheraven                char* __to_nxt;
4184227825Stheraven                __r = __cvtptr_->out(__st, __frm, __frm_end, __frm_nxt,
4185227825Stheraven                                           __to, __to_end, __to_nxt);
4186227825Stheraven                __cvtcount_ += __frm_nxt - __frm;
4187227825Stheraven                if (__frm_nxt == __frm)
4188227825Stheraven                {
4189227825Stheraven                    __r = codecvt_base::error;
4190227825Stheraven                }
4191227825Stheraven                else if (__r == codecvt_base::noconv)
4192227825Stheraven                {
4193227825Stheraven                    __bs.resize(__to - &__bs[0]);
4194227825Stheraven                    // This only gets executed if _Elem is char
4195227825Stheraven                    __bs.append((const char*)__frm, (const char*)__frm_end);
4196227825Stheraven                    __frm = __frm_nxt;
4197227825Stheraven                    __r = codecvt_base::ok;
4198227825Stheraven                }
4199227825Stheraven                else if (__r == codecvt_base::ok)
4200227825Stheraven                {
4201227825Stheraven                    __bs.resize(__to_nxt - &__bs[0]);
4202227825Stheraven                    __frm = __frm_nxt;
4203227825Stheraven                }
4204227825Stheraven                else if (__r == codecvt_base::partial)
4205227825Stheraven                {
4206227825Stheraven                    ptrdiff_t __s = __to_nxt - &__bs[0];
4207227825Stheraven                    __bs.resize(2 * __s);
4208227825Stheraven                    __to = &__bs[0] + __s;
4209227825Stheraven                    __to_end = &__bs[0] + __bs.size();
4210227825Stheraven                    __frm = __frm_nxt;
4211227825Stheraven                }
4212227825Stheraven            } while (__r == codecvt_base::partial && __frm_nxt < __frm_end);
4213227825Stheraven        }
4214227825Stheraven        if (__r == codecvt_base::ok)
4215227825Stheraven        {
4216227825Stheraven            size_t __s = __bs.size();
4217227825Stheraven            __bs.resize(__bs.capacity());
4218227825Stheraven            char* __to = &__bs[0] + __s;
4219227825Stheraven            char* __to_end = __to + __bs.size();
4220227825Stheraven            do
4221227825Stheraven            {
4222227825Stheraven                char* __to_nxt;
4223227825Stheraven                __r = __cvtptr_->unshift(__st, __to, __to_end, __to_nxt);
4224227825Stheraven                if (__r == codecvt_base::noconv)
4225227825Stheraven                {
4226227825Stheraven                    __bs.resize(__to - &__bs[0]);
4227227825Stheraven                    __r = codecvt_base::ok;
4228227825Stheraven                }
4229227825Stheraven                else if (__r == codecvt_base::ok)
4230227825Stheraven                {
4231227825Stheraven                    __bs.resize(__to_nxt - &__bs[0]);
4232227825Stheraven                }
4233227825Stheraven                else if (__r == codecvt_base::partial)
4234227825Stheraven                {
4235232950Stheraven                    ptrdiff_t __sp = __to_nxt - &__bs[0];
4236232950Stheraven                    __bs.resize(2 * __sp);
4237232950Stheraven                    __to = &__bs[0] + __sp;
4238227825Stheraven                    __to_end = &__bs[0] + __bs.size();
4239227825Stheraven                }
4240227825Stheraven            } while (__r == codecvt_base::partial);
4241227825Stheraven            if (__r == codecvt_base::ok)
4242227825Stheraven                return __bs;
4243227825Stheraven        }
4244227825Stheraven    }
4245227825Stheraven#ifndef _LIBCPP_NO_EXCEPTIONS
4246227825Stheraven    if (__byte_err_string_.empty())
4247227825Stheraven        throw range_error("wstring_convert: to_bytes error");
4248227825Stheraven#endif  // _LIBCPP_NO_EXCEPTIONS
4249227825Stheraven    return __byte_err_string_;
4250227825Stheraven}
4251227825Stheraven
4252227825Stheraventemplate <class _Codecvt, class _Elem = wchar_t, class _Tr = char_traits<_Elem> >
4253249998Sdimclass _LIBCPP_TYPE_VIS wbuffer_convert
4254227825Stheraven    : public basic_streambuf<_Elem, _Tr>
4255227825Stheraven{
4256227825Stheravenpublic:
4257227825Stheraven    // types:
4258227825Stheraven    typedef _Elem                          char_type;
4259227825Stheraven    typedef _Tr                            traits_type;
4260227825Stheraven    typedef typename traits_type::int_type int_type;
4261227825Stheraven    typedef typename traits_type::pos_type pos_type;
4262227825Stheraven    typedef typename traits_type::off_type off_type;
4263227825Stheraven    typedef typename _Codecvt::state_type  state_type;
4264227825Stheraven
4265227825Stheravenprivate:
4266227825Stheraven    char*       __extbuf_;
4267227825Stheraven    const char* __extbufnext_;
4268227825Stheraven    const char* __extbufend_;
4269227825Stheraven    char __extbuf_min_[8];
4270227825Stheraven    size_t __ebs_;
4271227825Stheraven    char_type* __intbuf_;
4272227825Stheraven    size_t __ibs_;
4273227825Stheraven    streambuf* __bufptr_;
4274227825Stheraven    _Codecvt* __cv_;
4275227825Stheraven    state_type __st_;
4276227825Stheraven    ios_base::openmode __cm_;
4277227825Stheraven    bool __owns_eb_;
4278227825Stheraven    bool __owns_ib_;
4279227825Stheraven    bool __always_noconv_;
4280227825Stheraven
4281227825Stheraven    wbuffer_convert(const wbuffer_convert&);
4282227825Stheraven    wbuffer_convert& operator=(const wbuffer_convert&);
4283227825Stheravenpublic:
4284227825Stheraven    wbuffer_convert(streambuf* __bytebuf = 0, _Codecvt* __pcvt = new _Codecvt,
4285227825Stheraven                    state_type __state = state_type());
4286227825Stheraven    ~wbuffer_convert();
4287227825Stheraven
4288227825Stheraven    _LIBCPP_INLINE_VISIBILITY
4289227825Stheraven    streambuf* rdbuf() const {return __bufptr_;}
4290227825Stheraven    _LIBCPP_INLINE_VISIBILITY
4291227825Stheraven    streambuf* rdbuf(streambuf* __bytebuf)
4292227825Stheraven    {
4293227825Stheraven        streambuf* __r = __bufptr_;
4294227825Stheraven        __bufptr_ = __bytebuf;
4295227825Stheraven        return __r;
4296227825Stheraven    }
4297227825Stheraven
4298227825Stheraven    _LIBCPP_INLINE_VISIBILITY
4299227825Stheraven    state_type state() const {return __st_;}
4300227825Stheraven
4301227825Stheravenprotected:
4302227825Stheraven    virtual int_type underflow();
4303227825Stheraven    virtual int_type pbackfail(int_type __c = traits_type::eof());
4304227825Stheraven    virtual int_type overflow (int_type __c = traits_type::eof());
4305227825Stheraven    virtual basic_streambuf<char_type, traits_type>* setbuf(char_type* __s,
4306227825Stheraven                                                            streamsize __n);
4307227825Stheraven    virtual pos_type seekoff(off_type __off, ios_base::seekdir __way,
4308227825Stheraven                             ios_base::openmode __wch = ios_base::in | ios_base::out);
4309227825Stheraven    virtual pos_type seekpos(pos_type __sp,
4310227825Stheraven                             ios_base::openmode __wch = ios_base::in | ios_base::out);
4311227825Stheraven    virtual int sync();
4312227825Stheraven
4313227825Stheravenprivate:
4314227825Stheraven    bool __read_mode();
4315227825Stheraven    void __write_mode();
4316227825Stheraven    wbuffer_convert* __close();
4317227825Stheraven};
4318227825Stheraven
4319227825Stheraventemplate <class _Codecvt, class _Elem, class _Tr>
4320227825Stheravenwbuffer_convert<_Codecvt, _Elem, _Tr>::
4321227825Stheraven    wbuffer_convert(streambuf* __bytebuf, _Codecvt* __pcvt, state_type __state)
4322227825Stheraven    : __extbuf_(0),
4323227825Stheraven      __extbufnext_(0),
4324227825Stheraven      __extbufend_(0),
4325227825Stheraven      __ebs_(0),
4326227825Stheraven      __intbuf_(0),
4327227825Stheraven      __ibs_(0),
4328227825Stheraven      __bufptr_(__bytebuf),
4329227825Stheraven      __cv_(__pcvt),
4330227825Stheraven      __st_(__state),
4331227825Stheraven      __cm_(0),
4332227825Stheraven      __owns_eb_(false),
4333227825Stheraven      __owns_ib_(false),
4334227825Stheraven      __always_noconv_(__cv_ ? __cv_->always_noconv() : false)
4335227825Stheraven{
4336227825Stheraven    setbuf(0, 4096);
4337227825Stheraven}
4338227825Stheraven
4339227825Stheraventemplate <class _Codecvt, class _Elem, class _Tr>
4340227825Stheravenwbuffer_convert<_Codecvt, _Elem, _Tr>::~wbuffer_convert()
4341227825Stheraven{
4342227825Stheraven    __close();
4343227825Stheraven    delete __cv_;
4344227825Stheraven    if (__owns_eb_)
4345227825Stheraven        delete [] __extbuf_;
4346227825Stheraven    if (__owns_ib_)
4347227825Stheraven        delete [] __intbuf_;
4348227825Stheraven}
4349227825Stheraven
4350227825Stheraventemplate <class _Codecvt, class _Elem, class _Tr>
4351227825Stheraventypename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type
4352227825Stheravenwbuffer_convert<_Codecvt, _Elem, _Tr>::underflow()
4353227825Stheraven{
4354227825Stheraven    if (__cv_ == 0 || __bufptr_ == 0)
4355227825Stheraven        return traits_type::eof();
4356227825Stheraven    bool __initial = __read_mode();
4357227825Stheraven    char_type __1buf;
4358227825Stheraven    if (this->gptr() == 0)
4359227825Stheraven        this->setg(&__1buf, &__1buf+1, &__1buf+1);
4360227825Stheraven    const size_t __unget_sz = __initial ? 0 : min<size_t>((this->egptr() - this->eback()) / 2, 4);
4361227825Stheraven    int_type __c = traits_type::eof();
4362227825Stheraven    if (this->gptr() == this->egptr())
4363227825Stheraven    {
4364227825Stheraven        memmove(this->eback(), this->egptr() - __unget_sz, __unget_sz * sizeof(char_type));
4365227825Stheraven        if (__always_noconv_)
4366227825Stheraven        {
4367227825Stheraven            streamsize __nmemb = static_cast<streamsize>(this->egptr() - this->eback() - __unget_sz);
4368227825Stheraven            __nmemb = __bufptr_->sgetn((char*)this->eback() + __unget_sz, __nmemb);
4369227825Stheraven            if (__nmemb != 0)
4370227825Stheraven            {
4371227825Stheraven                this->setg(this->eback(),
4372227825Stheraven                           this->eback() + __unget_sz,
4373227825Stheraven                           this->eback() + __unget_sz + __nmemb);
4374227825Stheraven                __c = *this->gptr();
4375227825Stheraven            }
4376227825Stheraven        }
4377227825Stheraven        else
4378227825Stheraven        {
4379227825Stheraven            memmove(__extbuf_, __extbufnext_, __extbufend_ - __extbufnext_);
4380227825Stheraven            __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_);
4381227825Stheraven            __extbufend_ = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_);
4382227825Stheraven            streamsize __nmemb = _VSTD::min(static_cast<streamsize>(this->egptr() - this->eback() - __unget_sz),
4383227825Stheraven                                 static_cast<streamsize>(__extbufend_ - __extbufnext_));
4384227825Stheraven            codecvt_base::result __r;
4385227825Stheraven            state_type __svs = __st_;
4386227825Stheraven            streamsize __nr = __bufptr_->sgetn(const_cast<char*>(__extbufnext_), __nmemb);
4387227825Stheraven            if (__nr != 0)
4388227825Stheraven            {
4389227825Stheraven                __extbufend_ = __extbufnext_ + __nr;
4390227825Stheraven                char_type*  __inext;
4391227825Stheraven                __r = __cv_->in(__st_, __extbuf_, __extbufend_, __extbufnext_,
4392227825Stheraven                                       this->eback() + __unget_sz,
4393227825Stheraven                                       this->egptr(), __inext);
4394227825Stheraven                if (__r == codecvt_base::noconv)
4395227825Stheraven                {
4396227825Stheraven                    this->setg((char_type*)__extbuf_, (char_type*)__extbuf_, (char_type*)__extbufend_);
4397227825Stheraven                    __c = *this->gptr();
4398227825Stheraven                }
4399227825Stheraven                else if (__inext != this->eback() + __unget_sz)
4400227825Stheraven                {
4401227825Stheraven                    this->setg(this->eback(), this->eback() + __unget_sz, __inext);
4402227825Stheraven                    __c = *this->gptr();
4403227825Stheraven                }
4404227825Stheraven            }
4405227825Stheraven        }
4406227825Stheraven    }
4407227825Stheraven    else
4408227825Stheraven        __c = *this->gptr();
4409227825Stheraven    if (this->eback() == &__1buf)
4410227825Stheraven        this->setg(0, 0, 0);
4411227825Stheraven    return __c;
4412227825Stheraven}
4413227825Stheraven
4414227825Stheraventemplate <class _Codecvt, class _Elem, class _Tr>
4415227825Stheraventypename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type
4416227825Stheravenwbuffer_convert<_Codecvt, _Elem, _Tr>::pbackfail(int_type __c)
4417227825Stheraven{
4418227825Stheraven    if (__cv_ != 0 && __bufptr_ != 0 && this->eback() < this->gptr())
4419227825Stheraven    {
4420227825Stheraven        if (traits_type::eq_int_type(__c, traits_type::eof()))
4421227825Stheraven        {
4422227825Stheraven            this->gbump(-1);
4423227825Stheraven            return traits_type::not_eof(__c);
4424227825Stheraven        }
4425227825Stheraven        if (traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]))
4426227825Stheraven        {
4427227825Stheraven            this->gbump(-1);
4428227825Stheraven            *this->gptr() = traits_type::to_char_type(__c);
4429227825Stheraven            return __c;
4430227825Stheraven        }
4431227825Stheraven    }
4432227825Stheraven    return traits_type::eof();
4433227825Stheraven}
4434227825Stheraven
4435227825Stheraventemplate <class _Codecvt, class _Elem, class _Tr>
4436227825Stheraventypename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type
4437227825Stheravenwbuffer_convert<_Codecvt, _Elem, _Tr>::overflow(int_type __c)
4438227825Stheraven{
4439227825Stheraven    if (__cv_ == 0 || __bufptr_ == 0)
4440227825Stheraven        return traits_type::eof();
4441227825Stheraven    __write_mode();
4442227825Stheraven    char_type __1buf;
4443227825Stheraven    char_type* __pb_save = this->pbase();
4444227825Stheraven    char_type* __epb_save = this->epptr();
4445227825Stheraven    if (!traits_type::eq_int_type(__c, traits_type::eof()))
4446227825Stheraven    {
4447227825Stheraven        if (this->pptr() == 0)
4448227825Stheraven            this->setp(&__1buf, &__1buf+1);
4449227825Stheraven        *this->pptr() = traits_type::to_char_type(__c);
4450227825Stheraven        this->pbump(1);
4451227825Stheraven    }
4452227825Stheraven    if (this->pptr() != this->pbase())
4453227825Stheraven    {
4454227825Stheraven        if (__always_noconv_)
4455227825Stheraven        {
4456227825Stheraven            streamsize __nmemb = static_cast<streamsize>(this->pptr() - this->pbase());
4457227825Stheraven            if (__bufptr_->sputn((const char*)this->pbase(), __nmemb) != __nmemb)
4458227825Stheraven                return traits_type::eof();
4459227825Stheraven        }
4460227825Stheraven        else
4461227825Stheraven        {
4462227825Stheraven            char* __extbe = __extbuf_;
4463227825Stheraven            codecvt_base::result __r;
4464227825Stheraven            do
4465227825Stheraven            {
4466227825Stheraven                const char_type* __e;
4467227825Stheraven                __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e,
4468227825Stheraven                                        __extbuf_, __extbuf_ + __ebs_, __extbe);
4469227825Stheraven                if (__e == this->pbase())
4470227825Stheraven                    return traits_type::eof();
4471227825Stheraven                if (__r == codecvt_base::noconv)
4472227825Stheraven                {
4473227825Stheraven                    streamsize __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
4474227825Stheraven                    if (__bufptr_->sputn((const char*)this->pbase(), __nmemb) != __nmemb)
4475227825Stheraven                        return traits_type::eof();
4476227825Stheraven                }
4477227825Stheraven                else if (__r == codecvt_base::ok || __r == codecvt_base::partial)
4478227825Stheraven                {
4479227825Stheraven                    streamsize __nmemb = static_cast<size_t>(__extbe - __extbuf_);
4480227825Stheraven                    if (__bufptr_->sputn(__extbuf_, __nmemb) != __nmemb)
4481227825Stheraven                        return traits_type::eof();
4482227825Stheraven                    if (__r == codecvt_base::partial)
4483227825Stheraven                    {
4484227825Stheraven                        this->setp((char_type*)__e, this->pptr());
4485227825Stheraven                        this->pbump(this->epptr() - this->pbase());
4486227825Stheraven                    }
4487227825Stheraven                }
4488227825Stheraven                else
4489227825Stheraven                    return traits_type::eof();
4490227825Stheraven            } while (__r == codecvt_base::partial);
4491227825Stheraven        }
4492227825Stheraven        this->setp(__pb_save, __epb_save);
4493227825Stheraven    }
4494227825Stheraven    return traits_type::not_eof(__c);
4495227825Stheraven}
4496227825Stheraven
4497227825Stheraventemplate <class _Codecvt, class _Elem, class _Tr>
4498227825Stheravenbasic_streambuf<_Elem, _Tr>*
4499227825Stheravenwbuffer_convert<_Codecvt, _Elem, _Tr>::setbuf(char_type* __s, streamsize __n)
4500227825Stheraven{
4501227825Stheraven    this->setg(0, 0, 0);
4502227825Stheraven    this->setp(0, 0);
4503227825Stheraven    if (__owns_eb_)
4504227825Stheraven        delete [] __extbuf_;
4505227825Stheraven    if (__owns_ib_)
4506227825Stheraven        delete [] __intbuf_;
4507227825Stheraven    __ebs_ = __n;
4508227825Stheraven    if (__ebs_ > sizeof(__extbuf_min_))
4509227825Stheraven    {
4510227825Stheraven        if (__always_noconv_ && __s)
4511227825Stheraven        {
4512227825Stheraven            __extbuf_ = (char*)__s;
4513227825Stheraven            __owns_eb_ = false;
4514227825Stheraven        }
4515227825Stheraven        else
4516227825Stheraven        {
4517227825Stheraven            __extbuf_ = new char[__ebs_];
4518227825Stheraven            __owns_eb_ = true;
4519227825Stheraven        }
4520227825Stheraven    }
4521227825Stheraven    else
4522227825Stheraven    {
4523227825Stheraven        __extbuf_ = __extbuf_min_;
4524227825Stheraven        __ebs_ = sizeof(__extbuf_min_);
4525227825Stheraven        __owns_eb_ = false;
4526227825Stheraven    }
4527227825Stheraven    if (!__always_noconv_)
4528227825Stheraven    {
4529227825Stheraven        __ibs_ = max<streamsize>(__n, sizeof(__extbuf_min_));
4530227825Stheraven        if (__s && __ibs_ >= sizeof(__extbuf_min_))
4531227825Stheraven        {
4532227825Stheraven            __intbuf_ = __s;
4533227825Stheraven            __owns_ib_ = false;
4534227825Stheraven        }
4535227825Stheraven        else
4536227825Stheraven        {
4537227825Stheraven            __intbuf_ = new char_type[__ibs_];
4538227825Stheraven            __owns_ib_ = true;
4539227825Stheraven        }
4540227825Stheraven    }
4541227825Stheraven    else
4542227825Stheraven    {
4543227825Stheraven        __ibs_ = 0;
4544227825Stheraven        __intbuf_ = 0;
4545227825Stheraven        __owns_ib_ = false;
4546227825Stheraven    }
4547227825Stheraven    return this;
4548227825Stheraven}
4549227825Stheraven
4550227825Stheraventemplate <class _Codecvt, class _Elem, class _Tr>
4551227825Stheraventypename wbuffer_convert<_Codecvt, _Elem, _Tr>::pos_type
4552227825Stheravenwbuffer_convert<_Codecvt, _Elem, _Tr>::seekoff(off_type __off, ios_base::seekdir __way,
4553227825Stheraven                                        ios_base::openmode __om)
4554227825Stheraven{
4555227825Stheraven    int __width = __cv_->encoding();
4556227825Stheraven    if (__cv_ == 0 || __bufptr_ == 0 || (__width <= 0 && __off != 0) || sync())
4557227825Stheraven        return pos_type(off_type(-1));
4558227825Stheraven    // __width > 0 || __off == 0
4559227825Stheraven    switch (__way)
4560227825Stheraven    {
4561227825Stheraven    case ios_base::beg:
4562227825Stheraven        break;
4563227825Stheraven    case ios_base::cur:
4564227825Stheraven        break;
4565227825Stheraven    case ios_base::end:
4566227825Stheraven        break;
4567227825Stheraven    default:
4568227825Stheraven        return pos_type(off_type(-1));
4569227825Stheraven    }
4570227825Stheraven    pos_type __r = __bufptr_->pubseekoff(__width * __off, __way, __om);
4571227825Stheraven    __r.state(__st_);
4572227825Stheraven    return __r;
4573227825Stheraven}
4574227825Stheraven
4575227825Stheraventemplate <class _Codecvt, class _Elem, class _Tr>
4576227825Stheraventypename wbuffer_convert<_Codecvt, _Elem, _Tr>::pos_type
4577227825Stheravenwbuffer_convert<_Codecvt, _Elem, _Tr>::seekpos(pos_type __sp, ios_base::openmode __wch)
4578227825Stheraven{
4579227825Stheraven    if (__cv_ == 0 || __bufptr_ == 0 || sync())
4580227825Stheraven        return pos_type(off_type(-1));
4581227825Stheraven    if (__bufptr_->pubseekpos(__sp, __wch) == pos_type(off_type(-1)))
4582227825Stheraven        return pos_type(off_type(-1));
4583227825Stheraven    return __sp;
4584227825Stheraven}
4585227825Stheraven
4586227825Stheraventemplate <class _Codecvt, class _Elem, class _Tr>
4587227825Stheravenint
4588227825Stheravenwbuffer_convert<_Codecvt, _Elem, _Tr>::sync()
4589227825Stheraven{
4590227825Stheraven    if (__cv_ == 0 || __bufptr_ == 0)
4591227825Stheraven        return 0;
4592227825Stheraven    if (__cm_ & ios_base::out)
4593227825Stheraven    {
4594227825Stheraven        if (this->pptr() != this->pbase())
4595227825Stheraven            if (overflow() == traits_type::eof())
4596227825Stheraven                return -1;
4597227825Stheraven        codecvt_base::result __r;
4598227825Stheraven        do
4599227825Stheraven        {
4600227825Stheraven            char* __extbe;
4601227825Stheraven            __r = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);
4602227825Stheraven            streamsize __nmemb = static_cast<streamsize>(__extbe - __extbuf_);
4603227825Stheraven            if (__bufptr_->sputn(__extbuf_, __nmemb) != __nmemb)
4604227825Stheraven                return -1;
4605227825Stheraven        } while (__r == codecvt_base::partial);
4606227825Stheraven        if (__r == codecvt_base::error)
4607227825Stheraven            return -1;
4608227825Stheraven        if (__bufptr_->pubsync())
4609227825Stheraven            return -1;
4610227825Stheraven    }
4611227825Stheraven    else if (__cm_ & ios_base::in)
4612227825Stheraven    {
4613227825Stheraven        off_type __c;
4614227825Stheraven        if (__always_noconv_)
4615227825Stheraven            __c = this->egptr() - this->gptr();
4616227825Stheraven        else
4617227825Stheraven        {
4618227825Stheraven            int __width = __cv_->encoding();
4619227825Stheraven            __c = __extbufend_ - __extbufnext_;
4620227825Stheraven            if (__width > 0)
4621227825Stheraven                __c += __width * (this->egptr() - this->gptr());
4622227825Stheraven            else
4623227825Stheraven            {
4624227825Stheraven                if (this->gptr() != this->egptr())
4625227825Stheraven                {
4626227825Stheraven                    reverse(this->gptr(), this->egptr());
4627227825Stheraven                    codecvt_base::result __r;
4628227825Stheraven                    const char_type* __e = this->gptr();
4629227825Stheraven                    char* __extbe;
4630227825Stheraven                    do
4631227825Stheraven                    {
4632227825Stheraven                        __r = __cv_->out(__st_, __e, this->egptr(), __e,
4633227825Stheraven                                         __extbuf_, __extbuf_ + __ebs_, __extbe);
4634227825Stheraven                        switch (__r)
4635227825Stheraven                        {
4636227825Stheraven                        case codecvt_base::noconv:
4637227825Stheraven                            __c += this->egptr() - this->gptr();
4638227825Stheraven                            break;
4639227825Stheraven                        case codecvt_base::ok:
4640227825Stheraven                        case codecvt_base::partial:
4641227825Stheraven                            __c += __extbe - __extbuf_;
4642227825Stheraven                            break;
4643227825Stheraven                        default:
4644227825Stheraven                            return -1;
4645227825Stheraven                        }
4646227825Stheraven                    } while (__r == codecvt_base::partial);
4647227825Stheraven                }
4648227825Stheraven            }
4649227825Stheraven        }
4650227825Stheraven        if (__bufptr_->pubseekoff(-__c, ios_base::cur, __cm_) == pos_type(off_type(-1)))
4651227825Stheraven            return -1;
4652227825Stheraven        this->setg(0, 0, 0);
4653227825Stheraven        __cm_ = 0;
4654227825Stheraven    }
4655227825Stheraven    return 0;
4656227825Stheraven}
4657227825Stheraven
4658227825Stheraventemplate <class _Codecvt, class _Elem, class _Tr>
4659227825Stheravenbool
4660227825Stheravenwbuffer_convert<_Codecvt, _Elem, _Tr>::__read_mode()
4661227825Stheraven{
4662227825Stheraven    if (!(__cm_ & ios_base::in))
4663227825Stheraven    {
4664227825Stheraven        this->setp(0, 0);
4665227825Stheraven        if (__always_noconv_)
4666227825Stheraven            this->setg((char_type*)__extbuf_,
4667227825Stheraven                       (char_type*)__extbuf_ + __ebs_,
4668227825Stheraven                       (char_type*)__extbuf_ + __ebs_);
4669227825Stheraven        else
4670227825Stheraven            this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_);
4671227825Stheraven        __cm_ = ios_base::in;
4672227825Stheraven        return true;
4673227825Stheraven    }
4674227825Stheraven    return false;
4675227825Stheraven}
4676227825Stheraven
4677227825Stheraventemplate <class _Codecvt, class _Elem, class _Tr>
4678227825Stheravenvoid
4679227825Stheravenwbuffer_convert<_Codecvt, _Elem, _Tr>::__write_mode()
4680227825Stheraven{
4681227825Stheraven    if (!(__cm_ & ios_base::out))
4682227825Stheraven    {
4683227825Stheraven        this->setg(0, 0, 0);
4684227825Stheraven        if (__ebs_ > sizeof(__extbuf_min_))
4685227825Stheraven        {
4686227825Stheraven            if (__always_noconv_)
4687227825Stheraven                this->setp((char_type*)__extbuf_,
4688227825Stheraven                           (char_type*)__extbuf_ + (__ebs_ - 1));
4689227825Stheraven            else
4690227825Stheraven                this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1));
4691227825Stheraven        }
4692227825Stheraven        else
4693227825Stheraven            this->setp(0, 0);
4694227825Stheraven        __cm_ = ios_base::out;
4695227825Stheraven    }
4696227825Stheraven}
4697227825Stheraven
4698227825Stheraventemplate <class _Codecvt, class _Elem, class _Tr>
4699227825Stheravenwbuffer_convert<_Codecvt, _Elem, _Tr>*
4700227825Stheravenwbuffer_convert<_Codecvt, _Elem, _Tr>::__close()
4701227825Stheraven{
4702227825Stheraven    wbuffer_convert* __rt = 0;
4703227825Stheraven    if (__cv_ != 0 && __bufptr_ != 0)
4704227825Stheraven    {
4705227825Stheraven        __rt = this;
4706227825Stheraven        if ((__cm_ & ios_base::out) && sync())
4707227825Stheraven            __rt = 0;
4708227825Stheraven    }
4709227825Stheraven    return __rt;
4710227825Stheraven}
4711227825Stheraven
4712227825Stheraven_LIBCPP_END_NAMESPACE_STD
4713227825Stheraven
4714227825Stheraven#endif  // _LIBCPP_LOCALE
4715